Hex Artifact Content
Not logged in

Artifact ba35fa988d8a096e5774c5e05e3cb5a2332f5813:

File src/sqlite3.c part of check-in [c002a008e6] - Update SQLite to version 3.6.10. by drh on 2009-01-15 16:03:17.

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 30 2e 20 20  ersion 3.6.10.  
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c  By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20   the individual 
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74  C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c  o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68  e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61  e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73  n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69   a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69  on.** unit.  Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f  s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70  mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74  timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a   would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65   possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70   files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d    Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66  provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65   5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77   commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f  hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67  mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f  le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79  is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69  ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75  le SQLite.  To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68  se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20  er.** programs, 
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69  you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69  le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69  te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a  le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69  ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  ng interface to 
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20  ry.  (If you do 
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65  not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61   "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64  der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20  , you will find 
0340: 61 20 63 6f 70 79 20 69 6e 20 74 68 65 20 66 69  a copy in the fi
0350: 72 73 74 0a 2a 2a 20 36 37 33 36 20 6c 69 6e 65  rst.** 6736 line
0360: 73 20 70 61 73 74 20 74 68 69 73 20 68 65 61 64  s past this head
0370: 65 72 20 63 6f 6d 6d 65 6e 74 2e 29 20 20 41 64  er comment.)  Ad
0380: 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 20 66 69  ditional code fi
0390: 6c 65 73 20 6d 61 79 20 62 65 0a 2a 2a 20 6e 65  les may be.** ne
03a0: 65 64 65 64 20 69 66 20 79 6f 75 20 77 61 6e 74  eded if you want
03b0: 20 61 20 77 72 61 70 70 65 72 20 74 6f 20 69 6e   a wrapper to in
03c0: 74 65 72 66 61 63 65 20 53 51 4c 69 74 65 20 77  terface SQLite w
03d0: 69 74 68 20 79 6f 75 72 20 63 68 6f 69 63 65 20  ith your choice 
03e0: 6f 66 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 69 6e  of.** programmin
03f0: 67 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65  g language.  The
0400: 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 22 73   code for the "s
0410: 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e 64 2d  qlite3" command-
0420: 6c 69 6e 65 20 73 68 65 6c 6c 0a 2a 2a 20 69 73  line shell.** is
0430: 20 61 6c 73 6f 20 69 6e 20 61 20 73 65 70 61 72   also in a separ
0440: 61 74 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20  ate file.  This 
0450: 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  file contains on
0460: 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  ly code for the 
0470: 63 6f 72 65 0a 2a 2a 20 53 51 4c 69 74 65 20 6c  core.** SQLite l
0480: 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  ibrary..**.** Th
0490: 69 73 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  is amalgamation 
04a0: 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e  was generated on
04b0: 20 32 30 30 39 2d 30 31 2d 31 35 20 31 36 3a 30   2009-01-15 16:0
04c0: 30 3a 33 39 20 55 54 43 2e 0a 2a 2f 0a 23 64 65  0:39 UTC..*/.#de
04d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  fine SQLITE_CORE
04e0: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
04f0: 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31  E_AMALGAMATION 1
0500: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
0510: 50 52 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65  PRIVATE.# define
0520: 20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20   SQLITE_PRIVATE 
0530: 73 74 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69  static.#endif.#i
0540: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49  fndef SQLITE_API
0550: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
0560: 5f 41 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a  _API.#endif./***
0570: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
0580: 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74  n file sqliteInt
0590: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
05a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
05c0: 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
05d0: 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
05e0: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
05f0: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
0600: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
0610: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
0620: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
0630: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
0640: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
0650: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
0660: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
0670: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
0680: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
0690: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
06a0: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
06b0: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
06c0: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
06d0: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
06e0: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  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 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69  **.** Internal i
0740: 6e 74 65 72 66 61 63 65 20 64 65 66 69 6e 69 74  nterface definit
0750: 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e  ions for SQLite.
0760: 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a  .**.** @(#) $Id:
0770: 20 73 71 6c 69 74 65 49 6e 74 2e 68 2c 76 20 31   sqliteInt.h,v 1
0780: 2e 38 32 34 20 32 30 30 39 2f 30 31 2f 31 34 20  .824 2009/01/14 
0790: 32 33 3a 30 33 3a 34 31 20 64 72 68 20 45 78 70  23:03:41 drh Exp
07a0: 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53   $.*/.#ifndef _S
07b0: 51 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66  QLITEINT_H_.#def
07c0: 69 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48  ine _SQLITEINT_H
07d0: 5f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65  _../*.** Include
07e0: 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69   the configurati
07f0: 6f 6e 20 68 65 61 64 65 72 20 6f 75 74 70 75 74  on header output
0800: 20 62 79 20 27 63 6f 6e 66 69 67 75 72 65 27 20   by 'configure' 
0810: 69 66 20 77 65 27 72 65 20 75 73 69 6e 67 20 74  if we're using t
0820: 68 65 0a 2a 2a 20 61 75 74 6f 63 6f 6e 66 2d 62  he.** autoconf-b
0830: 61 73 65 64 20 62 75 69 6c 64 0a 2a 2f 0a 23 69  ased build.*/.#i
0840: 66 64 65 66 20 5f 48 41 56 45 5f 53 51 4c 49 54  fdef _HAVE_SQLIT
0850: 45 5f 43 4f 4e 46 49 47 5f 48 0a 23 69 6e 63 6c  E_CONFIG_H.#incl
0860: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23  ude "config.h".#
0870: 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
0880: 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73  ****** Include s
0890: 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 69 6e 20  qliteLimit.h in 
08a0: 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71  the middle of sq
08b0: 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
08c0: 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
08d0: 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
08e0: 65 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20  e sqliteLimit.h 
08f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0910: 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
0920: 37 20 4d 61 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68  7 May 7.**.** Th
0930: 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
0940: 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
0950: 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
0960: 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
0970: 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
0980: 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
0990: 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
09a0: 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
09b0: 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
09c0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
09d0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
09e0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
09f0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
0a00: 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
0a10: 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
0a20: 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
0a30: 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
0a40: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a80: 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68  ******.** .** Th
0a90: 69 73 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20  is file defines 
0aa0: 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 6f  various limits o
0ab0: 66 20 77 68 61 74 20 53 51 4c 69 74 65 20 63 61  f what SQLite ca
0ac0: 6e 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a  n process..**.**
0ad0: 20 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74   @(#) $Id: sqlit
0ae0: 65 4c 69 6d 69 74 2e 68 2c 76 20 31 2e 31 30 20  eLimit.h,v 1.10 
0af0: 32 30 30 39 2f 30 31 2f 31 30 20 31 36 3a 31 35  2009/01/10 16:15
0b00: 3a 30 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :09 danielk1977 
0b10: 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
0b20: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67  The maximum leng
0b30: 74 68 20 6f 66 20 61 20 54 45 58 54 20 6f 72 20  th of a TEXT or 
0b40: 42 4c 4f 42 20 69 6e 20 62 79 74 65 73 2e 20 20  BLOB in bytes.  
0b50: 20 54 68 69 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69   This also.** li
0b60: 6d 69 74 73 20 74 68 65 20 73 69 7a 65 20 6f 66  mits the size of
0b70: 20 61 20 72 6f 77 20 69 6e 20 61 20 74 61 62 6c   a row in a tabl
0b80: 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  e or index..**.*
0b90: 2a 20 54 68 65 20 68 61 72 64 20 6c 69 6d 69 74  * The hard limit
0ba0: 20 69 73 20 74 68 65 20 61 62 69 6c 69 74 79 20   is the ability 
0bb0: 6f 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e  of a 32-bit sign
0bc0: 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 6f  ed integer.** to
0bd0: 20 63 6f 75 6e 74 20 74 68 65 20 73 69 7a 65 3a   count the size:
0be0: 20 32 5e 33 31 2d 31 20 6f 72 20 32 31 34 37 34   2^31-1 or 21474
0bf0: 38 33 36 34 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65  83647..*/.#ifnde
0c00: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  f SQLITE_MAX_LEN
0c10: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  GTH.# define SQL
0c20: 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 31  ITE_MAX_LENGTH 1
0c30: 30 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69 66  000000000.#endif
0c40: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
0c50: 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  the maximum numb
0c60: 65 72 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  er of.**.**    *
0c70: 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61   Columns in a ta
0c80: 62 6c 65 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75  ble.**    * Colu
0c90: 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a  mns in an index.
0ca0: 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20  **    * Columns 
0cb0: 69 6e 20 61 20 76 69 65 77 0a 2a 2a 20 20 20 20  in a view.**    
0cc0: 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 53  * Terms in the S
0cd0: 45 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20  ET clause of an 
0ce0: 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
0cf0: 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69  .**    * Terms i
0d00: 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
0d10: 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
0d20: 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 54  tement.**    * T
0d30: 65 72 6d 73 20 69 6e 20 74 68 65 20 47 52 4f 55  erms in the GROU
0d40: 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59  P BY or ORDER BY
0d50: 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 53 45   clauses of a SE
0d60: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
0d70: 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e  **    * Terms in
0d80: 20 74 68 65 20 56 41 4c 55 45 53 20 63 6c 61 75   the VALUES clau
0d90: 73 65 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20  se of an INSERT 
0da0: 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20  statement.**.** 
0db0: 54 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c  The hard upper l
0dc0: 69 6d 69 74 20 68 65 72 65 20 69 73 20 33 32 36  imit here is 326
0dd0: 37 36 2e 20 20 4d 6f 73 74 20 64 61 74 61 62 61  76.  Most databa
0de0: 73 65 20 70 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a  se people will.*
0df0: 2a 20 74 65 6c 6c 20 79 6f 75 20 74 68 61 74 20  * tell you that 
0e00: 69 6e 20 61 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c  in a well-normal
0e10: 69 7a 65 64 20 64 61 74 61 62 61 73 65 2c 20 79  ized database, y
0e20: 6f 75 20 75 73 75 61 6c 6c 79 20 73 68 6f 75 6c  ou usually shoul
0e30: 64 0a 2a 2a 20 6e 6f 74 20 68 61 76 65 20 6d 6f  d.** not have mo
0e40: 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20  re than a dozen 
0e50: 6f 72 20 73 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e  or so columns in
0e60: 20 61 6e 79 20 74 61 62 6c 65 2e 20 20 41 6e 64   any table.  And
0e70: 20 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 74   if.** that is t
0e80: 68 65 20 63 61 73 65 2c 20 74 68 65 72 65 20 69  he case, there i
0e90: 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 68 61  s no point in ha
0ea0: 76 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 61  ving more than a
0eb0: 20 66 65 77 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61   few.** dozen va
0ec0: 6c 75 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74  lues in any of t
0ed0: 68 65 20 6f 74 68 65 72 20 73 69 74 75 61 74 69  he other situati
0ee0: 6f 6e 73 20 64 65 73 63 72 69 62 65 64 20 61 62  ons described ab
0ef0: 6f 76 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ove..*/.#ifndef 
0f00: 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
0f10: 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  N.# define SQLIT
0f20: 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 32 30 30  E_MAX_COLUMN 200
0f30: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
0f40: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67  The maximum leng
0f50: 74 68 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 53  th of a single S
0f60: 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  QL statement in 
0f70: 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  bytes..**.** It 
0f80: 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 63  used to be the c
0f90: 61 73 65 20 74 68 61 74 20 73 65 74 74 69 6e 67  ase that setting
0fa0: 20 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 7a   this value to z
0fb0: 65 72 6f 20 77 6f 75 6c 64 0a 2a 2a 20 74 75 72  ero would.** tur
0fc0: 6e 20 74 68 65 20 6c 69 6d 69 74 20 6f 66 66 2e  n the limit off.
0fd0: 20 20 54 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e    That is no lon
0fe0: 67 65 72 20 74 72 75 65 2e 20 20 49 74 20 69 73  ger true.  It is
0ff0: 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a 2a   not possible.**
1000: 20 74 6f 20 74 75 72 6e 20 74 68 69 73 20 6c 69   to turn this li
1010: 6d 69 74 20 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e  mit off..*/.#ifn
1020: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53  def SQLITE_MAX_S
1030: 51 4c 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69  QL_LENGTH.# defi
1040: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  ne SQLITE_MAX_SQ
1050: 4c 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30  L_LENGTH 1000000
1060: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  000.#endif../*.*
1070: 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65  * The maximum de
1080: 70 74 68 20 6f 66 20 61 6e 20 65 78 70 72 65 73  pth of an expres
1090: 73 69 6f 6e 20 74 72 65 65 2e 20 54 68 69 73 20  sion tree. This 
10a0: 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 0a 2a  is limited to .*
10b0: 2a 20 73 6f 6d 65 20 65 78 74 65 6e 74 20 62 79  * some extent by
10c0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
10d0: 4c 45 4e 47 54 48 2e 20 42 75 74 20 73 6f 6d 65  LENGTH. But some
10e0: 74 69 6d 65 20 79 6f 75 20 6d 69 67 68 74 20 0a  time you might .
10f0: 2a 2a 20 77 61 6e 74 20 74 6f 20 70 6c 61 63 65  ** want to place
1100: 20 6d 6f 72 65 20 73 65 76 65 72 65 20 6c 69 6d   more severe lim
1110: 69 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 6c  its on the compl
1120: 65 78 69 74 79 20 6f 66 20 61 6e 20 0a 2a 2a 20  exity of an .** 
1130: 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a  expression..**.*
1140: 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 75  * A value of 0 u
1150: 73 65 64 20 74 6f 20 6d 65 61 6e 20 74 68 61 74  sed to mean that
1160: 20 74 68 65 20 6c 69 6d 69 74 20 77 61 73 20 6e   the limit was n
1170: 6f 74 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20  ot enforced..** 
1180: 42 75 74 20 74 68 61 74 20 69 73 20 6e 6f 20 6c  But that is no l
1190: 6f 6e 67 65 72 20 74 72 75 65 2e 20 20 54 68 65  onger true.  The
11a0: 20 6c 69 6d 69 74 20 69 73 20 6e 6f 77 20 73 74   limit is now st
11b0: 72 69 63 74 6c 79 20 65 6e 66 6f 72 63 65 64 0a  rictly enforced.
11c0: 2a 2a 20 61 74 20 61 6c 6c 20 74 69 6d 65 73 2e  ** at all times.
11d0: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
11e0: 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
11f0: 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  H.# define SQLIT
1200: 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
1210: 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a   1000.#endif../*
1220: 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20  .** The maximum 
1230: 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  number of terms 
1240: 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45  in a compound SE
1250: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
1260: 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  ** The code gene
1270: 72 61 74 6f 72 20 66 6f 72 20 63 6f 6d 70 6f 75  rator for compou
1280: 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  nd SELECT statem
1290: 65 6e 74 73 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a  ents does one.**
12a0: 20 6c 65 76 65 6c 20 6f 66 20 72 65 63 75 72 73   level of recurs
12b0: 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 65 72  ion for each ter
12c0: 6d 2e 20 20 41 20 73 74 61 63 6b 20 6f 76 65 72  m.  A stack over
12d0: 66 6c 6f 77 20 63 61 6e 20 72 65 73 75 6c 74 0a  flow can result.
12e0: 2a 2a 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72  ** if the number
12f0: 20 6f 66 20 74 65 72 6d 73 20 69 73 20 74 6f 6f   of terms is too
1300: 20 6c 61 72 67 65 2e 20 20 49 6e 20 70 72 61 63   large.  In prac
1310: 74 69 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a  tice, most SQL.*
1320: 2a 20 6e 65 76 65 72 20 68 61 73 20 6d 6f 72 65  * never has more
1330: 20 74 68 61 6e 20 33 20 6f 72 20 34 20 74 65 72   than 3 or 4 ter
1340: 6d 73 2e 20 20 55 73 65 20 61 20 76 61 6c 75 65  ms.  Use a value
1350: 20 6f 66 20 30 20 74 6f 20 64 69 73 61 62 6c 65   of 0 to disable
1360: 0a 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f 6e  .** any limit on
1370: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
1380: 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75  erms in a compou
1390: 6e 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69  nt SELECT..*/.#i
13a0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
13b0: 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
13c0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
13d0: 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  _MAX_COMPOUND_SE
13e0: 4c 45 43 54 20 35 30 30 0a 23 65 6e 64 69 66 0a  LECT 500.#endif.
13f0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1400: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 63  um number of opc
1410: 6f 64 65 73 20 69 6e 20 61 20 56 44 42 45 20 70  odes in a VDBE p
1420: 72 6f 67 72 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63  rogram..** Not c
1430: 75 72 72 65 6e 74 6c 79 20 65 6e 66 6f 72 63 65  urrently enforce
1440: 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  d..*/.#ifndef SQ
1450: 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50  LITE_MAX_VDBE_OP
1460: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1470: 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20 32 35 30  _MAX_VDBE_OP 250
1480: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1490: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
14a0: 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
14b0: 20 74 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63 74   to an SQL funct
14c0: 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ion..*/.#ifndef 
14d0: 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
14e0: 49 4f 4e 5f 41 52 47 0a 23 20 64 65 66 69 6e 65  ION_ARG.# define
14f0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43   SQLITE_MAX_FUNC
1500: 54 49 4f 4e 5f 41 52 47 20 31 32 37 0a 23 65 6e  TION_ARG 127.#en
1510: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  dif../*.** The m
1520: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
1530: 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73   in-memory pages
1540: 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20   to use for the 
1550: 6d 61 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a  main database.**
1560: 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 74   table and for t
1570: 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
1580: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46    The SQLITE_DEF
1590: 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a  AULT_CACHE_SIZE.
15a0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
15b0: 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f  E_DEFAULT_CACHE_
15c0: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
15d0: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
15e0: 48 45 5f 53 49 5a 45 20 20 32 30 30 30 0a 23 65  HE_SIZE  2000.#e
15f0: 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
1600: 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50  ITE_DEFAULT_TEMP
1610: 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 65  _CACHE_SIZE.# de
1620: 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
1630: 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53  ULT_TEMP_CACHE_S
1640: 49 5a 45 20 20 35 30 30 0a 23 65 6e 64 69 66 0a  IZE  500.#endif.
1650: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1660: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 74 74  um number of att
1670: 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e  ached databases.
1680: 20 20 54 68 69 73 20 6d 75 73 74 20 62 65 20 62    This must be b
1690: 65 74 77 65 65 6e 20 30 0a 2a 2a 20 61 6e 64 20  etween 0.** and 
16a0: 33 30 2e 20 20 54 68 65 20 75 70 70 65 72 20 62  30.  The upper b
16b0: 6f 75 6e 64 20 6f 6e 20 33 30 20 69 73 20 62 65  ound on 30 is be
16c0: 63 61 75 73 65 20 61 20 33 32 2d 62 69 74 20 69  cause a 32-bit i
16d0: 6e 74 65 67 65 72 20 62 69 74 6d 61 70 0a 2a 2a  nteger bitmap.**
16e0: 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61   is used interna
16f0: 6c 6c 79 20 74 6f 20 74 72 61 63 6b 20 61 74 74  lly to track att
1700: 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e  ached databases.
1710: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1720: 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 0a  TE_MAX_ATTACHED.
1730: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1740: 4d 41 58 5f 41 54 54 41 43 48 45 44 20 31 30 0a  MAX_ATTACHED 10.
1750: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54  #endif.../*.** T
1760: 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65  he maximum value
1770: 20 6f 66 20 61 20 3f 6e 6e 6e 20 77 69 6c 64 63   of a ?nnn wildc
1780: 61 72 64 20 74 68 61 74 20 74 68 65 20 70 61 72  ard that the par
1790: 73 65 72 20 77 69 6c 6c 20 61 63 63 65 70 74 2e  ser will accept.
17a0: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
17b0: 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f  TE_MAX_VARIABLE_
17c0: 4e 55 4d 42 45 52 0a 23 20 64 65 66 69 6e 65 20  NUMBER.# define 
17d0: 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
17e0: 42 4c 45 5f 4e 55 4d 42 45 52 20 39 39 39 0a 23  BLE_NUMBER 999.#
17f0: 65 6e 64 69 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75  endif../* Maximu
1800: 6d 20 70 61 67 65 20 73 69 7a 65 2e 20 20 54 68  m page size.  Th
1810: 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e  e upper bound on
1820: 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 33   this value is 3
1830: 32 37 36 38 2e 20 20 54 68 69 73 20 61 20 6c 69  2768.  This a li
1840: 6d 69 74 0a 2a 2a 20 69 6d 70 6f 73 65 64 20 62  mit.** imposed b
1850: 79 20 74 68 65 20 6e 65 63 65 73 73 69 74 79 20  y the necessity 
1860: 6f 66 20 73 74 6f 72 69 6e 67 20 74 68 65 20 76  of storing the v
1870: 61 6c 75 65 20 69 6e 20 61 20 32 2d 62 79 74 65  alue in a 2-byte
1880: 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
1890: 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 61 63  r.** and the fac
18a0: 74 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  t that the page 
18b0: 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70  size must be a p
18c0: 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a  ower of 2..**.**
18d0: 20 49 66 20 74 68 69 73 20 6c 69 6d 69 74 20 69   If this limit i
18e0: 73 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20  s changed, then 
18f0: 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 6c 69 62  the compiled lib
1900: 72 61 72 79 20 69 73 20 74 65 63 68 6e 69 63 61  rary is technica
1910: 6c 6c 79 0a 2a 2a 20 69 6e 63 6f 6d 70 61 74 69  lly.** incompati
1920: 62 6c 65 20 77 69 74 68 20 61 6e 20 53 51 4c 69  ble with an SQLi
1930: 74 65 20 6c 69 62 72 61 72 79 20 63 6f 6d 70 69  te library compi
1940: 6c 65 64 20 77 69 74 68 20 61 20 64 69 66 66 65  led with a diffe
1950: 72 65 6e 74 20 6c 69 6d 69 74 2e 20 49 66 0a 2a  rent limit. If.*
1960: 2a 20 61 20 70 72 6f 63 65 73 73 20 6f 70 65 72  * a process oper
1970: 61 74 69 6e 67 20 6f 6e 20 61 20 64 61 74 61 62  ating on a datab
1980: 61 73 65 20 77 69 74 68 20 61 20 70 61 67 65 2d  ase with a page-
1990: 73 69 7a 65 20 6f 66 20 36 35 35 33 36 20 62 79  size of 65536 by
19a0: 74 65 73 20 0a 2a 2a 20 63 72 61 73 68 65 73 2c  tes .** crashes,
19b0: 20 74 68 65 6e 20 61 6e 20 69 6e 73 74 61 6e 63   then an instanc
19c0: 65 20 6f 66 20 53 51 4c 69 74 65 20 63 6f 6d 70  e of SQLite comp
19d0: 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 64 65  iled with the de
19e0: 66 61 75 6c 74 20 70 61 67 65 2d 73 69 7a 65 20  fault page-size 
19f0: 0a 2a 2a 20 6c 69 6d 69 74 20 77 69 6c 6c 20 6e  .** limit will n
1a00: 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f  ot be able to ro
1a10: 6c 6c 62 61 63 6b 20 74 68 65 20 61 62 6f 72 74  llback the abort
1a20: 65 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ed transaction. 
1a30: 54 68 69 73 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65  This could.** le
1a40: 61 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63  ad to database c
1a50: 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69  orruption..*/.#i
1a60: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
1a70: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66  _PAGE_SIZE.# def
1a80: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  ine SQLITE_MAX_P
1a90: 41 47 45 5f 53 49 5a 45 20 33 32 37 36 38 0a 23  AGE_SIZE 32768.#
1aa0: 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
1ab0: 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f  e default size o
1ac0: 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67  f a database pag
1ad0: 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
1ae0: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
1af0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20  E_SIZE.# define 
1b00: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
1b10: 41 47 45 5f 53 49 5a 45 20 31 30 32 34 0a 23 65  AGE_SIZE 1024.#e
1b20: 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
1b30: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
1b40: 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  E>SQLITE_MAX_PAG
1b50: 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53  E_SIZE.# undef S
1b60: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
1b70: 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65  GE_SIZE.# define
1b80: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
1b90: 50 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54 45  PAGE_SIZE SQLITE
1ba0: 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23  _MAX_PAGE_SIZE.#
1bb0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64  endif../*.** Ord
1bc0: 69 6e 61 72 69 6c 79 2c 20 69 66 20 6e 6f 20 76  inarily, if no v
1bd0: 61 6c 75 65 20 69 73 20 65 78 70 6c 69 63 69 74  alue is explicit
1be0: 6c 79 20 70 72 6f 76 69 64 65 64 2c 20 53 51 4c  ly provided, SQL
1bf0: 69 74 65 20 63 72 65 61 74 65 73 20 64 61 74 61  ite creates data
1c00: 62 61 73 65 73 0a 2a 2a 20 77 69 74 68 20 70 61  bases.** with pa
1c10: 67 65 20 73 69 7a 65 20 53 51 4c 49 54 45 5f 44  ge size SQLITE_D
1c20: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1c30: 2e 20 48 6f 77 65 76 65 72 2c 20 62 61 73 65 64  . However, based
1c40: 20 6f 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 64   on certain.** d
1c50: 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69  evice characteri
1c60: 73 74 69 63 73 20 28 73 65 63 74 6f 72 2d 73 69  stics (sector-si
1c70: 7a 65 20 61 6e 64 20 61 74 6f 6d 69 63 20 77 72  ze and atomic wr
1c80: 69 74 65 28 29 20 73 75 70 70 6f 72 74 29 2c 0a  ite() support),.
1c90: 2a 2a 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68  ** SQLite may ch
1ca0: 6f 6f 73 65 20 61 20 6c 61 72 67 65 72 20 76 61  oose a larger va
1cb0: 6c 75 65 2e 20 54 68 69 73 20 63 6f 6e 73 74 61  lue. This consta
1cc0: 6e 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75  nt is the maximu
1cd0: 6d 20 76 61 6c 75 65 0a 2a 2a 20 53 51 4c 69 74  m value.** SQLit
1ce0: 65 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e  e will choose on
1cf0: 20 69 74 73 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66   its own..*/.#if
1d00: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1d10: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
1d20: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
1d30: 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
1d40: 47 45 5f 53 49 5a 45 20 38 31 39 32 0a 23 65 6e  GE_SIZE 8192.#en
1d50: 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
1d60: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
1d70: 53 49 5a 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  SIZE>SQLITE_MAX_
1d80: 50 41 47 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65  PAGE_SIZE.# unde
1d90: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  f SQLITE_MAX_DEF
1da0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23  AULT_PAGE_SIZE.#
1db0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
1dc0: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
1dd0: 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f  SIZE SQLITE_MAX_
1de0: 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66  PAGE_SIZE.#endif
1df0: 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d  .../*.** Maximum
1e00: 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
1e10: 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65   in one database
1e20: 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   file..**.** Thi
1e30: 73 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74  s is really just
1e40: 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   the default val
1e50: 75 65 20 66 6f 72 20 74 68 65 20 6d 61 78 5f 70  ue for the max_p
1e60: 61 67 65 5f 63 6f 75 6e 74 20 70 72 61 67 6d 61  age_count pragma
1e70: 2e 0a 2a 2a 20 54 68 69 73 20 76 61 6c 75 65 20  ..** This value 
1e80: 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 28  can be lowered (
1e90: 6f 72 20 72 61 69 73 65 64 29 20 61 74 20 72 75  or raised) at ru
1ea0: 6e 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 61  n-time using tha
1eb0: 74 20 74 68 65 0a 2a 2a 20 6d 61 78 5f 70 61 67  t the.** max_pag
1ec0: 65 5f 63 6f 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a  e_count macro..*
1ed0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1ee0: 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 0a  _MAX_PAGE_COUNT.
1ef0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1f00: 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 20 31  MAX_PAGE_COUNT 1
1f10: 30 37 33 37 34 31 38 32 33 0a 23 65 6e 64 69 66  073741823.#endif
1f20: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20  ../*.** Maximum 
1f30: 6c 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73  length (in bytes
1f40: 29 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e  ) of the pattern
1f50: 20 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c   in a LIKE or GL
1f60: 4f 42 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a  OB.** operator..
1f70: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1f80: 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45  E_MAX_LIKE_PATTE
1f90: 52 4e 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69  RN_LENGTH.# defi
1fa0: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  ne SQLITE_MAX_LI
1fb0: 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
1fc0: 48 20 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a  H 50000.#endif..
1fd0: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1fe0: 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 4c 69 6d  End of sqliteLim
1ff0: 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  it.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 2f 0a  **************/.
2020: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2030: 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
2040: 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
2050: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
2060: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2070: 0a 2f 2a 20 44 69 73 61 62 6c 65 20 6e 75 69 73  ./* Disable nuis
2080: 61 6e 63 65 20 77 61 72 6e 69 6e 67 73 20 6f 6e  ance warnings on
2090: 20 42 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c 65   Borland compile
20a0: 72 73 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  rs */.#if define
20b0: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a  d(__BORLANDC__).
20c0: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 72 63  #pragma warn -rc
20d0: 68 20 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c 65  h /* unreachable
20e0: 20 63 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d 61   code */.#pragma
20f0: 20 77 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43 6f   warn -ccc /* Co
2100: 6e 64 69 74 69 6f 6e 20 69 73 20 61 6c 77 61 79  ndition is alway
2110: 73 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20  s true or false 
2120: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20  */.#pragma warn 
2130: 2d 61 75 73 20 2f 2a 20 41 73 73 69 67 6e 65 64  -aus /* Assigned
2140: 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20   value is never 
2150: 75 73 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20  used */.#pragma 
2160: 77 61 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f 6d  warn -csu /* Com
2170: 70 61 72 69 6e 67 20 73 69 67 6e 65 64 20 61 6e  paring signed an
2180: 64 20 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23 70  d unsigned */.#p
2190: 72 61 67 6d 61 20 77 61 72 6e 20 2d 73 70 61 20  ragma warn -spa 
21a0: 2f 2a 20 53 75 73 70 69 63 69 6f 75 73 20 70 6f  /* Suspicious po
21b0: 69 6e 74 65 72 20 61 72 69 74 68 6d 65 74 69 63  inter arithmetic
21c0: 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4e   */.#endif../* N
21d0: 65 65 64 65 64 20 66 6f 72 20 76 61 72 69 6f 75  eeded for variou
21e0: 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e 2e  s definitions...
21f0: 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e 55   */.#ifndef _GNU
2200: 5f 53 4f 55 52 43 45 0a 23 20 64 65 66 69 6e 65  _SOURCE.# define
2210: 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65 6e   _GNU_SOURCE.#en
2220: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75  dif../*.** Inclu
2230: 64 65 20 73 74 61 6e 64 61 72 64 20 68 65 61 64  de standard head
2240: 65 72 20 66 69 6c 65 73 20 61 73 20 6e 65 63 65  er files as nece
2250: 73 73 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66 20  ssary.*/.#ifdef 
2260: 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23 69  HAVE_STDINT_H.#i
2270: 6e 63 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e 68  nclude <stdint.h
2280: 3e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  >.#endif.#ifdef 
2290: 48 41 56 45 5f 49 4e 54 54 59 50 45 53 5f 48 0a  HAVE_INTTYPES_H.
22a0: 23 69 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79 70  #include <inttyp
22b0: 65 73 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a  es.h>.#endif../*
22c0: 0a 20 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69  . * This macro i
22d0: 73 20 75 73 65 64 20 74 6f 20 22 68 69 64 65 22  s used to "hide"
22e0: 20 73 6f 6d 65 20 75 67 6c 69 6e 65 73 73 20 69   some ugliness i
22f0: 6e 20 63 61 73 74 69 6e 67 20 61 6e 20 69 6e 74  n casting an int
2300: 0a 20 2a 20 76 61 6c 75 65 20 74 6f 20 61 20 70  . * value to a p
2310: 74 72 20 76 61 6c 75 65 20 75 6e 64 65 72 20 74  tr value under t
2320: 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20 63  he MSVC 64-bit c
2330: 6f 6d 70 69 6c 65 72 2e 20 20 20 43 61 73 74 69  ompiler.   Casti
2340: 6e 67 0a 20 2a 20 6e 6f 6e 20 36 34 2d 62 69 74  ng. * non 64-bit
2350: 20 76 61 6c 75 65 73 20 74 6f 20 70 74 72 20 74   values to ptr t
2360: 79 70 65 73 20 72 65 73 75 6c 74 73 20 69 6e 20  ypes results in 
2370: 61 20 22 68 61 72 64 22 20 65 72 72 6f 72 20 77  a "hard" error w
2380: 69 74 68 20 0a 20 2a 20 74 68 65 20 4d 53 56 43  ith . * the MSVC
2390: 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72   64-bit compiler
23a0: 20 77 68 69 63 68 20 74 68 69 73 20 61 74 74 65   which this atte
23b0: 6d 70 74 73 20 74 6f 20 61 76 6f 69 64 2e 20 20  mpts to avoid.  
23c0: 0a 20 2a 0a 20 2a 20 41 20 73 69 6d 70 6c 65 20  . *. * A simple 
23d0: 63 6f 6d 70 69 6c 65 72 20 70 72 61 67 6d 61 20  compiler pragma 
23e0: 6f 72 20 63 61 73 74 69 6e 67 20 73 65 71 75 65  or casting seque
23f0: 6e 63 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  nce could not be
2400: 20 66 6f 75 6e 64 0a 20 2a 20 74 6f 20 63 6f 72   found. * to cor
2410: 72 65 63 74 20 74 68 69 73 20 69 6e 20 61 6c 6c  rect this in all
2420: 20 73 69 74 75 61 74 69 6f 6e 73 2c 20 73 6f 20   situations, so 
2430: 74 68 69 73 20 6d 61 63 72 6f 20 77 61 73 20 69  this macro was i
2440: 6e 74 72 6f 64 75 63 65 64 2e 0a 20 2a 0a 20 2a  ntroduced.. *. *
2450: 20 49 74 20 63 6f 75 6c 64 20 62 65 20 61 72 67   It could be arg
2460: 75 65 64 20 74 68 61 74 20 74 68 65 20 69 6e 74  ued that the int
2470: 70 74 72 5f 74 20 74 79 70 65 20 63 6f 75 6c 64  ptr_t type could
2480: 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 69 73   be used in this
2490: 0a 20 2a 20 63 61 73 65 2c 20 62 75 74 20 74 68  . * case, but th
24a0: 61 74 20 74 79 70 65 20 69 73 20 6e 6f 74 20 61  at type is not a
24b0: 76 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c 6c 20  vailable on all 
24c0: 63 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 20 0a 20  compilers, or . 
24d0: 2a 20 72 65 71 75 69 72 65 73 20 74 68 65 20 23  * requires the #
24e0: 69 6e 63 6c 75 64 65 20 6f 66 20 73 70 65 63 69  include of speci
24f0: 66 69 63 20 68 65 61 64 65 72 73 20 77 68 69 63  fic headers whic
2500: 68 20 64 69 66 66 65 72 73 20 62 65 74 77 65 65  h differs betwee
2510: 6e 0a 20 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 0a  n. * platforms..
2520: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
2530: 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29  TE_INT_TO_PTR(X)
2540: 20 20 20 28 28 76 6f 69 64 2a 29 26 28 28 63 68     ((void*)&((ch
2550: 61 72 2a 29 30 29 5b 58 5d 29 0a 23 64 65 66 69  ar*)0)[X]).#defi
2560: 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f  ne SQLITE_PTR_TO
2570: 5f 49 4e 54 28 58 29 20 20 20 28 28 69 6e 74 29  _INT(X)   ((int)
2580: 28 28 28 63 68 61 72 2a 29 58 29 2d 28 63 68 61  (((char*)X)-(cha
2590: 72 2a 29 30 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  r*)0))../*.** Th
25a0: 65 73 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f  ese #defines sho
25b0: 75 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20  uld enable >2GB 
25c0: 66 69 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20  file support on 
25d0: 50 4f 53 49 58 20 69 66 20 74 68 65 0a 2a 2a 20  POSIX if the.** 
25e0: 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61  underlying opera
25f0: 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70  ting system supp
2600: 6f 72 74 73 20 69 74 2e 20 20 49 66 20 74 68 65  orts it.  If the
2610: 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72   OS lacks.** lar
2620: 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c  ge file support,
2630: 20 6f 72 20 69 66 20 74 68 65 20 4f 53 20 69 73   or if the OS is
2640: 20 77 69 6e 64 6f 77 73 2c 20 74 68 65 73 65 20   windows, these 
2650: 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 73  should be no-ops
2660: 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23  ..**.** Ticket #
2670: 32 37 33 39 3a 20 20 54 68 65 20 5f 4c 41 52 47  2739:  The _LARG
2680: 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 6d 61 63  EFILE_SOURCE mac
2690: 72 6f 20 6d 75 73 74 20 61 70 70 65 61 72 20 62  ro must appear b
26a0: 65 66 6f 72 65 20 61 6e 79 0a 2a 2a 20 73 79 73  efore any.** sys
26b0: 74 65 6d 20 23 69 6e 63 6c 75 64 65 73 2e 20 20  tem #includes.  
26c0: 48 65 6e 63 65 2c 20 74 68 69 73 20 62 6c 6f 63  Hence, this bloc
26d0: 6b 20 6f 66 20 63 6f 64 65 20 6d 75 73 74 20 62  k of code must b
26e0: 65 20 74 68 65 20 76 65 72 79 20 66 69 72 73 74  e the very first
26f0: 0a 2a 2a 20 63 6f 64 65 20 69 6e 20 61 6c 6c 20  .** code in all 
2700: 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 0a 2a 2a  source files..**
2710: 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65 20 73  .** Large file s
2720: 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20 64 69  upport can be di
2730: 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74 68 65  sabled using the
2740: 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41 42 4c   -DSQLITE_DISABL
2750: 45 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a 2a 20  E_LFS switch.** 
2760: 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  on the compiler 
2770: 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20 20 54  command line.  T
2780: 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79  his is necessary
2790: 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f 6d 70   if you are comp
27a0: 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20 72 65  iling.** on a re
27b0: 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28 65 78  cent machine (ex
27c0: 3a 20 52 65 64 20 48 61 74 20 37 2e 32 29 20 62  : Red Hat 7.2) b
27d0: 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f 75 72  ut you want your
27e0: 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a   code to work.**
27f0: 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61 63   on an older mac
2800: 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 48 61  hine (ex: Red Ha
2810: 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75 20  t 6.0).  If you 
2820: 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 20 48  compile on Red H
2830: 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75  at 7.2.** withou
2840: 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c  t this option, L
2850: 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42  FS is enable.  B
2860: 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20  ut LFS does not 
2870: 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72  exist in the ker
2880: 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 20 48 61  nel.** in Red Ha
2890: 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f  t 6.0, so the co
28a0: 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20  de won't work.  
28b0: 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d  Hence, for maxim
28c0: 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72  um binary.** por
28d0: 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f  tability you sho
28e0: 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a  uld omit LFS..**
28f0: 0a 2a 2a 20 53 69 6d 69 6c 61 72 20 69 73 20 74  .** Similar is t
2900: 72 75 65 20 66 6f 72 20 4d 61 63 20 4f 53 20 58  rue for Mac OS X
2910: 2e 20 20 4c 46 53 20 69 73 20 6f 6e 6c 79 20 73  .  LFS is only s
2920: 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63 20  upported on Mac 
2930: 4f 53 20 58 20 39 20 61 6e 64 20 6c 61 74 65 72  OS X 9 and later
2940: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
2950: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a  ITE_DISABLE_LFS.
2960: 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f  # define _LARGE_
2970: 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69  FILE       1.# i
2980: 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53  fndef _FILE_OFFS
2990: 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69  ET_BITS.#   defi
29a0: 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f  ne _FILE_OFFSET_
29b0: 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a  BITS 64.# endif.
29c0: 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46  # define _LARGEF
29d0: 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e  ILE_SOURCE 1.#en
29e0: 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  dif.../*.** The 
29f0: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
2a00: 45 20 6d 61 63 72 6f 20 6d 75 73 74 20 62 65 20  E macro must be 
2a10: 64 65 66 69 6e 65 64 20 61 73 20 65 69 74 68 65  defined as eithe
2a20: 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 4f 6c 64  r 0 or 1..** Old
2a30: 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  er versions of S
2a40: 51 4c 69 74 65 20 75 73 65 64 20 61 6e 20 6f 70  QLite used an op
2a50: 74 69 6f 6e 61 6c 20 54 48 52 45 41 44 53 41 46  tional THREADSAF
2a60: 45 20 6d 61 63 72 6f 2e 0a 2a 2a 20 57 65 20 73  E macro..** We s
2a70: 75 70 70 6f 72 74 20 74 68 61 74 20 66 6f 72 20  upport that for 
2a80: 6c 65 67 61 63 79 0a 2a 2f 0a 23 69 66 20 21 64  legacy.*/.#if !d
2a90: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48  efined(SQLITE_TH
2aa0: 52 45 41 44 53 41 46 45 29 0a 23 69 66 20 64 65  READSAFE).#if de
2ab0: 66 69 6e 65 64 28 54 48 52 45 41 44 53 41 46 45  fined(THREADSAFE
2ac0: 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
2ad0: 45 5f 54 48 52 45 41 44 53 41 46 45 20 54 48 52  E_THREADSAFE THR
2ae0: 45 41 44 53 41 46 45 0a 23 65 6c 73 65 0a 23 20  EADSAFE.#else.# 
2af0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48  define SQLITE_TH
2b00: 52 45 41 44 53 41 46 45 20 31 0a 23 65 6e 64 69  READSAFE 1.#endi
2b10: 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  f.#endif../*.** 
2b20: 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  The SQLITE_DEFAU
2b30: 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 6d 61 63  LT_MEMSTATUS mac
2b40: 72 6f 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e  ro must be defin
2b50: 65 64 20 61 73 20 65 69 74 68 65 72 20 30 20 6f  ed as either 0 o
2b60: 72 20 31 2e 0a 2a 2a 20 49 74 20 64 65 74 65 72  r 1..** It deter
2b70: 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72  mines whether or
2b80: 20 6e 6f 74 20 74 68 65 20 66 65 61 74 75 72 65   not the feature
2b90: 73 20 72 65 6c 61 74 65 64 20 74 6f 20 0a 2a 2a  s related to .**
2ba0: 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
2bb0: 45 4d 53 54 41 54 55 53 20 61 72 65 20 61 76 61  EMSTATUS are ava
2bc0: 69 6c 61 62 6c 65 20 62 79 20 64 65 66 61 75 6c  ilable by defaul
2bd0: 74 20 6f 72 20 6e 6f 74 2e 20 54 68 69 73 20 76  t or not. This v
2be0: 61 6c 75 65 20 63 61 6e 0a 2a 2a 20 62 65 20 6f  alue can.** be o
2bf0: 76 65 72 72 69 64 64 65 6e 20 61 74 20 72 75 6e  verridden at run
2c00: 74 69 6d 65 20 75 73 69 6e 67 20 74 68 65 20 73  time using the s
2c10: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
2c20: 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  API..*/.#if !def
2c30: 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 46 41  ined(SQLITE_DEFA
2c40: 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 29 0a 23  ULT_MEMSTATUS).#
2c50: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
2c60: 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53  EFAULT_MEMSTATUS
2c70: 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   1.#endif../*.**
2c80: 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20   Exactly one of 
2c90: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
2ca0: 63 72 6f 73 20 6d 75 73 74 20 62 65 20 64 65 66  cros must be def
2cb0: 69 6e 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f  ined in order to
2cc0: 0a 2a 2a 20 73 70 65 63 69 66 79 20 77 68 69 63  .** specify whic
2cd0: 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  h memory allocat
2ce0: 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 74 6f  ion subsystem to
2cf0: 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20   use..**.**     
2d00: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
2d10: 4c 4c 4f 43 20 20 20 20 20 20 20 20 20 20 2f 2f  LLOC          //
2d20: 20 55 73 65 20 6e 6f 72 6d 61 6c 20 73 79 73 74   Use normal syst
2d30: 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20  em malloc().**  
2d40: 20 20 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42     SQLITE_MEMDEB
2d50: 55 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20  UG              
2d60: 20 2f 2f 20 44 65 62 75 67 67 69 6e 67 20 76 65   // Debugging ve
2d70: 72 73 69 6f 6e 20 6f 66 20 73 79 73 74 65 6d 20  rsion of system 
2d80: 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20  malloc().**     
2d90: 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49  SQLITE_MEMORY_SI
2da0: 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f  ZE            //
2db0: 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61   internal alloca
2dc0: 74 6f 72 20 23 31 0a 2a 2a 20 20 20 20 20 53 51  tor #1.**     SQ
2dd0: 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53  LITE_MMAP_HEAP_S
2de0: 49 5a 45 20 20 20 20 20 20 20 20 20 2f 2f 20 69  IZE         // i
2df0: 6e 74 65 72 6e 61 6c 20 6d 6d 61 70 28 29 20 61  nternal mmap() a
2e00: 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 20 20 20 20 20  llocator.**     
2e10: 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f  SQLITE_POW2_MEMO
2e20: 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20 2f 2f  RY_SIZE       //
2e30: 20 69 6e 74 65 72 6e 61 6c 20 70 6f 77 65 72 2d   internal power-
2e40: 6f 66 2d 74 77 6f 20 61 6c 6c 6f 63 61 74 6f 72  of-two allocator
2e50: 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f  .**.** If none o
2e60: 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 20  f the above are 
2e70: 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 73 65  defined, then se
2e80: 74 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f  t SQLITE_SYSTEM_
2e90: 4d 41 4c 4c 4f 43 20 61 73 0a 2a 2a 20 74 68 65  MALLOC as.** the
2ea0: 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 23 69 66   default..*/.#if
2eb0: 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
2ec0: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64  SYSTEM_MALLOC)+d
2ed0: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45  efined(SQLITE_ME
2ee0: 4d 44 45 42 55 47 29 2b 5c 0a 20 20 20 20 64 65  MDEBUG)+\.    de
2ef0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d  fined(SQLITE_MEM
2f00: 4f 52 59 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65  ORY_SIZE)+define
2f10: 64 28 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45  d(SQLITE_MMAP_HE
2f20: 41 50 5f 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64  AP_SIZE)+\.    d
2f30: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f  efined(SQLITE_PO
2f40: 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3e  W2_MEMORY_SIZE)>
2f50: 31 0a 23 20 65 72 72 6f 72 20 22 41 74 20 6d 6f  1.# error "At mo
2f60: 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  st one of the fo
2f70: 6c 6c 6f 77 69 6e 67 20 63 6f 6d 70 69 6c 65 2d  llowing compile-
2f80: 74 69 6d 65 20 63 6f 6e 66 69 67 75 72 61 74 69  time configurati
2f90: 6f 6e 20 6f 70 74 69 6f 6e 73 5c 0a 20 69 73 20  on options\. is 
2fa0: 61 6c 6c 6f 77 73 3a 20 53 51 4c 49 54 45 5f 53  allows: SQLITE_S
2fb0: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 2c 20 53 51  YSTEM_MALLOC, SQ
2fc0: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 2c 20 53  LITE_MEMDEBUG, S
2fd0: 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a  QLITE_MEMORY_SIZ
2fe0: 45 2c 5c 0a 20 53 51 4c 49 54 45 5f 4d 4d 41 50  E,\. SQLITE_MMAP
2ff0: 5f 48 45 41 50 5f 53 49 5a 45 2c 20 53 51 4c 49  _HEAP_SIZE, SQLI
3000: 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53  TE_POW2_MEMORY_S
3010: 49 5a 45 22 0a 23 65 6e 64 69 66 0a 23 69 66 20  IZE".#endif.#if 
3020: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53  defined(SQLITE_S
3030: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65  YSTEM_MALLOC)+de
3040: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d  fined(SQLITE_MEM
3050: 44 45 42 55 47 29 2b 5c 0a 20 20 20 20 64 65 66  DEBUG)+\.    def
3060: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f  ined(SQLITE_MEMO
3070: 52 59 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65 64  RY_SIZE)+defined
3080: 28 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41  (SQLITE_MMAP_HEA
3090: 50 5f 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65  P_SIZE)+\.    de
30a0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f 57  fined(SQLITE_POW
30b0: 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3d 3d  2_MEMORY_SIZE)==
30c0: 30 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  0.# define SQLIT
30d0: 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20  E_SYSTEM_MALLOC 
30e0: 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  1.#endif../*.** 
30f0: 49 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43  If SQLITE_MALLOC
3100: 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 69 73 20 64  _SOFT_LIMIT is d
3110: 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 72 79  efined, then try
3120: 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20   to keep the.** 
3130: 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  sizes of memory 
3140: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f  allocations belo
3150: 77 20 74 68 69 73 20 76 61 6c 75 65 20 77 68 65  w this value whe
3160: 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a  re possible..*/.
3170: 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
3180: 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53  TE_POW2_MEMORY_S
3190: 49 5a 45 29 20 26 26 20 21 64 65 66 69 6e 65 64  IZE) && !defined
31a0: 28 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53  (SQLITE_MALLOC_S
31b0: 4f 46 54 5f 4c 49 4d 49 54 29 0a 23 20 64 65 66  OFT_LIMIT).# def
31c0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  ine SQLITE_MALLO
31d0: 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 31 30 32  C_SOFT_LIMIT 102
31e0: 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  4.#endif../*.** 
31f0: 57 65 20 6e 65 65 64 20 74 6f 20 64 65 66 69 6e  We need to defin
3200: 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20  e _XOPEN_SOURCE 
3210: 61 73 20 66 6f 6c 6c 6f 77 73 20 69 6e 20 6f 72  as follows in or
3220: 64 65 72 20 74 6f 20 65 6e 61 62 6c 65 0a 2a 2a  der to enable.**
3230: 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
3240: 65 73 20 6f 6e 20 6d 6f 73 74 20 55 6e 69 78 20  es on most Unix 
3250: 73 79 73 74 65 6d 73 2e 20 20 42 75 74 20 4d 61  systems.  But Ma
3260: 63 20 4f 53 20 58 20 69 73 20 64 69 66 66 65 72  c OS X is differ
3270: 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 5f 58 4f 50  ent..** The _XOP
3280: 45 4e 5f 53 4f 55 52 43 45 20 64 65 66 69 6e 65  EN_SOURCE define
3290: 20 63 61 75 73 65 73 20 70 72 6f 62 6c 65 6d 73   causes problems
32a0: 20 66 6f 72 20 4d 61 63 20 4f 53 20 58 20 77 65   for Mac OS X we
32b0: 20 61 72 65 20 74 6f 6c 64 2c 0a 2a 2a 20 73 6f   are told,.** so
32c0: 20 69 74 20 69 73 20 6f 6d 69 74 74 65 64 20 74   it is omitted t
32d0: 68 65 72 65 2e 20 20 53 65 65 20 74 69 63 6b 65  here.  See ticke
32e0: 74 20 23 32 36 37 33 2e 0a 2a 2a 0a 2a 2a 20 4c  t #2673..**.** L
32f0: 61 74 65 72 20 77 65 20 6c 65 61 72 6e 20 74 68  ater we learn th
3300: 61 74 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45  at _XOPEN_SOURCE
3310: 20 69 73 20 70 6f 6f 72 6c 79 20 6f 72 20 69 6e   is poorly or in
3320: 63 6f 72 72 65 63 74 6c 79 0a 2a 2a 20 69 6d 70  correctly.** imp
3330: 6c 65 6d 65 6e 74 65 64 20 6f 6e 20 73 6f 6d 65  lemented on some
3340: 20 73 79 73 74 65 6d 73 2e 20 20 53 6f 20 77 65   systems.  So we
3350: 20 61 76 6f 69 64 20 64 65 66 69 6e 69 6e 67 20   avoid defining 
3360: 69 74 20 61 74 20 61 6c 6c 0a 2a 2a 20 69 66 20  it at all.** if 
3370: 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 64 65  it is already de
3380: 66 69 6e 65 64 20 6f 72 20 69 66 20 69 74 20 69  fined or if it i
3390: 73 20 75 6e 6e 65 65 64 65 64 20 62 65 63 61 75  s unneeded becau
33a0: 73 65 20 77 65 20 61 72 65 0a 2a 2a 20 6e 6f 74  se we are.** not
33b0: 20 64 6f 69 6e 67 20 61 20 74 68 72 65 61 64 73   doing a threads
33c0: 61 66 65 20 62 75 69 6c 64 2e 20 20 54 69 63 6b  afe build.  Tick
33d0: 65 74 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20  et #2681..**.** 
33e0: 53 65 65 20 61 6c 73 6f 20 74 69 63 6b 65 74 20  See also ticket 
33f0: 23 32 37 34 31 2e 0a 2a 2f 0a 23 69 66 20 21 64  #2741..*/.#if !d
3400: 65 66 69 6e 65 64 28 5f 58 4f 50 45 4e 5f 53 4f  efined(_XOPEN_SO
3410: 55 52 43 45 29 20 26 26 20 21 64 65 66 69 6e 65  URCE) && !define
3420: 64 28 5f 5f 44 41 52 57 49 4e 5f 5f 29 20 26 26  d(__DARWIN__) &&
3430: 20 21 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c   !defined(__APPL
3440: 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 54  E__) && SQLITE_T
3450: 48 52 45 41 44 53 41 46 45 0a 23 20 20 64 65 66  HREADSAFE.#  def
3460: 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43  ine _XOPEN_SOURC
3470: 45 20 35 30 30 20 20 2f 2a 20 4e 65 65 64 65 64  E 500  /* Needed
3480: 20 74 6f 20 65 6e 61 62 6c 65 20 70 74 68 72 65   to enable pthre
3490: 61 64 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ad recursive mut
34a0: 65 78 65 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  exes */.#endif..
34b0: 2f 2a 0a 2a 2a 20 54 68 65 20 54 43 4c 20 68 65  /*.** The TCL he
34c0: 61 64 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 6e  aders are only n
34d0: 65 65 64 65 64 20 77 68 65 6e 20 63 6f 6d 70 69  eeded when compi
34e0: 6c 69 6e 67 20 74 68 65 20 54 43 4c 20 62 69 6e  ling the TCL bin
34f0: 64 69 6e 67 73 2e 0a 2a 2f 0a 23 69 66 20 64 65  dings..*/.#if de
3500: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 43 4c  fined(SQLITE_TCL
3510: 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 54 43 4c  ) || defined(TCL
3520: 53 48 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c 74  SH).# include <t
3530: 63 6c 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a  cl.h>.#endif../*
3540: 0a 2a 2a 20 4d 61 6e 79 20 70 65 6f 70 6c 65 20  .** Many people 
3550: 61 72 65 20 66 61 69 6c 69 6e 67 20 74 6f 20 73  are failing to s
3560: 65 74 20 2d 44 4e 44 45 42 55 47 3d 31 20 77 68  et -DNDEBUG=1 wh
3570: 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 53 51 4c  en compiling SQL
3580: 69 74 65 2e 0a 2a 2a 20 53 65 74 74 69 6e 67 20  ite..** Setting 
3590: 4e 44 45 42 55 47 20 6d 61 6b 65 73 20 74 68 65  NDEBUG makes the
35a0: 20 63 6f 64 65 20 73 6d 61 6c 6c 65 72 20 61 6e   code smaller an
35b0: 64 20 72 75 6e 20 66 61 73 74 65 72 2e 20 20 53  d run faster.  S
35c0: 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  o the following.
35d0: 2a 2a 20 6c 69 6e 65 73 20 61 72 65 20 61 64 64  ** lines are add
35e0: 65 64 20 74 6f 20 61 75 74 6f 6d 61 74 69 63 61  ed to automatica
35f0: 6c 6c 79 20 73 65 74 20 4e 44 45 42 55 47 20 75  lly set NDEBUG u
3600: 6e 6c 65 73 73 20 74 68 65 20 2d 44 53 51 4c 49  nless the -DSQLI
3610: 54 45 5f 44 45 42 55 47 3d 31 0a 2a 2a 20 6f 70  TE_DEBUG=1.** op
3620: 74 69 6f 6e 20 69 73 20 73 65 74 2e 20 20 54 68  tion is set.  Th
3630: 75 73 20 4e 44 45 42 55 47 20 62 65 63 6f 6d 65  us NDEBUG become
3640: 73 20 61 6e 20 6f 70 74 2d 69 6e 20 72 61 74 68  s an opt-in rath
3650: 65 72 20 74 68 61 6e 20 61 6e 20 6f 70 74 2d 6f  er than an opt-o
3660: 75 74 0a 2a 2a 20 66 65 61 74 75 72 65 2e 0a 2a  ut.** feature..*
3670: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  /.#if !defined(N
3680: 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e  DEBUG) && !defin
3690: 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
36a0: 20 0a 23 20 64 65 66 69 6e 65 20 4e 44 45 42 55   .# define NDEBU
36b0: 47 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  G 1.#endif../*.*
36c0: 2a 20 54 68 65 20 74 65 73 74 63 61 73 65 28 29  * The testcase()
36d0: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74   macro is used t
36e0: 6f 20 61 69 64 20 69 6e 20 63 6f 76 65 72 61 67  o aid in coverag
36f0: 65 20 74 65 73 74 69 6e 67 2e 20 20 57 68 65 6e  e testing.  When
3700: 20 0a 2a 2a 20 64 6f 69 6e 67 20 63 6f 76 65 72   .** doing cover
3710: 61 67 65 20 74 65 73 74 69 6e 67 2c 20 74 68 65  age testing, the
3720: 20 63 6f 6e 64 69 74 69 6f 6e 20 69 6e 73 69 64   condition insid
3730: 65 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74  e the argument t
3740: 6f 0a 2a 2a 20 74 65 73 74 63 61 73 65 28 29 20  o.** testcase() 
3750: 6d 75 73 74 20 62 65 20 65 76 61 6c 75 61 74 65  must be evaluate
3760: 64 20 62 6f 74 68 20 74 72 75 65 20 61 6e 64 20  d both true and 
3770: 66 61 6c 73 65 20 69 6e 20 6f 72 64 65 72 20 74  false in order t
3780: 6f 0a 2a 2a 20 67 65 74 20 66 75 6c 6c 20 62 72  o.** get full br
3790: 61 6e 63 68 20 63 6f 76 65 72 61 67 65 2e 20 20  anch coverage.  
37a0: 54 68 65 20 74 65 73 74 63 61 73 65 28 29 20 6d  The testcase() m
37b0: 61 63 72 6f 20 69 73 20 69 6e 73 65 72 74 65 64  acro is inserted
37c0: 0a 2a 2a 20 74 6f 20 68 65 6c 70 20 65 6e 73 75  .** to help ensu
37d0: 72 65 20 61 64 65 71 75 61 74 65 20 74 65 73 74  re adequate test
37e0: 20 63 6f 76 65 72 61 67 65 20 69 6e 20 70 6c 61   coverage in pla
37f0: 63 65 73 20 77 68 65 72 65 20 73 69 6d 70 6c 65  ces where simple
3800: 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 2f 64 65  .** condition/de
3810: 63 69 73 69 6f 6e 20 63 6f 76 65 72 61 67 65 20  cision coverage 
3820: 69 73 20 69 6e 61 64 65 71 75 61 74 65 2e 20 20  is inadequate.  
3830: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 65 73  For example, tes
3840: 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e 20 62  tcase().** can b
3850: 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73  e used to make s
3860: 75 72 65 20 62 6f 75 6e 64 61 72 79 20 76 61 6c  ure boundary val
3870: 75 65 73 20 61 72 65 20 74 65 73 74 65 64 2e 20  ues are tested. 
3880: 20 46 6f 72 0a 2a 2a 20 62 69 74 6d 61 73 6b 20   For.** bitmask 
3890: 74 65 73 74 73 2c 20 74 65 73 74 63 61 73 65 28  tests, testcase(
38a0: 29 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  ) can be used to
38b0: 20 6d 61 6b 65 20 73 75 72 65 20 65 61 63 68 20   make sure each 
38c0: 62 69 74 0a 2a 2a 20 69 73 20 73 69 67 6e 69 66  bit.** is signif
38d0: 69 63 61 6e 74 20 61 6e 64 20 75 73 65 64 20 61  icant and used a
38e0: 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20 4f  t least once.  O
38f0: 6e 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65  n switch stateme
3900: 6e 74 73 0a 2a 2a 20 77 68 65 72 65 20 6d 75 6c  nts.** where mul
3910: 74 69 70 6c 65 20 63 61 73 65 73 20 67 6f 20 74  tiple cases go t
3920: 6f 20 74 68 65 20 73 61 6d 65 20 62 6c 6f 63 6b  o the same block
3930: 20 6f 66 20 63 6f 64 65 2c 20 74 65 73 74 63 61   of code, testca
3940: 73 65 28 29 0a 2a 2a 20 63 61 6e 20 69 6e 73 75  se().** can insu
3950: 72 65 20 74 68 61 74 20 61 6c 6c 20 63 61 73 65  re that all case
3960: 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 2e  s are evaluated.
3970: 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  .**.*/.#ifdef SQ
3980: 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45  LITE_COVERAGE_TE
3990: 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
39a0: 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
39b0: 43 6f 76 65 72 61 67 65 28 69 6e 74 29 3b 0a 23  Coverage(int);.#
39c0: 20 64 65 66 69 6e 65 20 74 65 73 74 63 61 73 65   define testcase
39d0: 28 58 29 20 20 69 66 28 20 58 20 29 7b 20 73 71  (X)  if( X ){ sq
39e0: 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 5f 5f  lite3Coverage(__
39f0: 4c 49 4e 45 5f 5f 29 3b 20 7d 0a 23 65 6c 73 65  LINE__); }.#else
3a00: 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74 63 61  .# define testca
3a10: 73 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  se(X).#endif../*
3a20: 0a 2a 2a 20 54 68 65 20 54 45 53 54 4f 4e 4c 59  .** The TESTONLY
3a30: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74   macro is used t
3a40: 6f 20 65 6e 63 6c 6f 73 65 20 76 61 72 69 61 62  o enclose variab
3a50: 6c 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20  le declarations 
3a60: 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 62 69 74 73  or.** other bits
3a70: 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 61 72   of code that ar
3a80: 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70  e needed to supp
3a90: 6f 72 74 20 74 68 65 20 61 72 67 75 6d 65 6e 74  ort the argument
3aa0: 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 65 73 74  s.** within test
3ab0: 63 61 73 65 28 29 20 61 6e 64 20 61 73 73 65 72  case() and asser
3ac0: 74 28 29 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23  t() macros..*/.#
3ad0: 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  if !defined(NDEB
3ae0: 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  UG) || defined(S
3af0: 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54  QLITE_COVERAGE_T
3b00: 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 54 45  EST).# define TE
3b10: 53 54 4f 4e 4c 59 28 58 29 20 20 58 0a 23 65 6c  STONLY(X)  X.#el
3b20: 73 65 0a 23 20 64 65 66 69 6e 65 20 54 45 53 54  se.# define TEST
3b30: 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 0a 0a  ONLY(X).#endif..
3b40: 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 41 59 53  /*.** The ALWAYS
3b50: 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 63 72 6f   and NEVER macro
3b60: 73 20 73 75 72 72 6f 75 6e 64 20 62 6f 6f 6c 65  s surround boole
3b70: 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77  an expressions w
3b80: 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 69 6e 74  hich .** are int
3b90: 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 79 73 20  ended to always 
3ba0: 62 65 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65  be true or false
3bb0: 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  , respectively. 
3bc0: 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 65 73 73   Such.** express
3bd0: 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 20 6f 6d  ions could be om
3be0: 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 63  itted from the c
3bf0: 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c 79 2e 20  ode completely. 
3c00: 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 61 72 65   But they.** are
3c10: 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 61 20 66   included in a f
3c20: 65 77 20 63 61 73 65 73 20 69 6e 20 6f 72 64 65  ew cases in orde
3c30: 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 74 68 65  r to enhance the
3c40: 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a 2a 20 6f   resilience.** o
3c50: 66 20 53 51 4c 69 74 65 20 74 6f 20 75 6e 65 78  f SQLite to unex
3c60: 70 65 63 74 65 64 20 62 65 68 61 76 69 6f 72 20  pected behavior 
3c70: 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 63 6f  - to make the co
3c80: 64 65 20 22 73 65 6c 66 2d 68 65 61 6c 69 6e 67  de "self-healing
3c90: 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 69 6c 65  ".** or "ductile
3ca0: 22 20 72 61 74 68 65 72 20 74 68 61 6e 20 62 65  " rather than be
3cb0: 69 6e 67 20 22 62 72 69 74 74 6c 65 22 20 61 6e  ing "brittle" an
3cc0: 64 20 63 72 61 73 68 69 6e 67 20 61 74 20 74 68  d crashing at th
3cd0: 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 6e 74 20  e first.** hint 
3ce0: 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 62 65 68  of unplanned beh
3cf0: 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  avior..**.** In 
3d00: 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 41 4c 57  other words, ALW
3d10: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61 72  AYS and NEVER ar
3d20: 65 20 61 64 64 65 64 20 66 6f 72 20 64 65 66 65  e added for defe
3d30: 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  nsive code..**.*
3d40: 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 63 6f 76  * When doing cov
3d50: 65 72 61 67 65 20 74 65 73 74 69 6e 67 20 41 4c  erage testing AL
3d60: 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61  WAYS and NEVER a
3d70: 72 65 20 68 61 72 64 2d 63 6f 64 65 64 20 74 6f  re hard-coded to
3d80: 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 6e 64 20  .** be true and 
3d90: 66 61 6c 73 65 20 73 6f 20 74 68 61 74 20 74 68  false so that th
3da0: 65 20 75 6e 72 65 61 63 68 61 62 6c 65 20 63 6f  e unreachable co
3db0: 64 65 20 74 68 65 6e 20 73 70 65 63 69 66 79 20  de then specify 
3dc0: 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65 20 63  will.** not be c
3dd0: 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 65 73 74  ounted as untest
3de0: 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 20  ed code..*/.#if 
3df0: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
3e00: 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20  OVERAGE_TEST).# 
3e10: 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29  define ALWAYS(X)
3e20: 20 20 20 20 20 20 28 31 29 0a 23 20 64 65 66 69        (1).# defi
3e30: 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20  ne NEVER(X)     
3e40: 20 20 28 30 29 0a 23 65 6c 69 66 20 21 64 65 66    (0).#elif !def
3e50: 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 53 51 4c  ined(NDEBUG).SQL
3e60: 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
3e70: 74 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 28  t sqlite3Assert(
3e80: 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20  void);.# define 
3e90: 41 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28  ALWAYS(X)      (
3ea0: 28 58 29 3f 31 3a 73 71 6c 69 74 65 33 41 73 73  (X)?1:sqlite3Ass
3eb0: 65 72 74 28 29 29 0a 23 20 64 65 66 69 6e 65 20  ert()).# define 
3ec0: 4e 45 56 45 52 28 58 29 20 20 20 20 20 20 20 28  NEVER(X)       (
3ed0: 28 58 29 3f 73 71 6c 69 74 65 33 41 73 73 65 72  (X)?sqlite3Asser
3ee0: 74 28 29 3a 30 29 0a 23 65 6c 73 65 0a 23 20 64  t():0).#else.# d
3ef0: 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 20  efine ALWAYS(X) 
3f00: 20 20 20 20 20 28 58 29 0a 23 20 64 65 66 69 6e       (X).# defin
3f10: 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20 20  e NEVER(X)      
3f20: 20 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   (X).#endif../*.
3f30: 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 75 6e 6c  ** The macro unl
3f40: 69 6b 65 6c 79 28 29 20 69 73 20 61 20 68 69 6e  ikely() is a hin
3f50: 74 20 74 68 61 74 20 73 75 72 72 6f 75 6e 64 73  t that surrounds
3f60: 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 65 78   a boolean.** ex
3f70: 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73  pression that is
3f80: 20 75 73 75 61 6c 6c 79 20 66 61 6c 73 65 2e 20   usually false. 
3f90: 20 4d 61 63 72 6f 20 6c 69 6b 65 6c 79 28 29 20   Macro likely() 
3fa0: 73 75 72 72 6f 75 6e 64 73 0a 2a 2a 20 61 20 62  surrounds.** a b
3fb0: 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f  oolean expressio
3fc0: 6e 20 74 68 61 74 20 69 73 20 75 73 75 61 6c 6c  n that is usuall
3fd0: 79 20 74 72 75 65 2e 20 20 47 43 43 20 69 73 20  y true.  GCC is 
3fe0: 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 20 74  able to.** use t
3ff0: 68 65 73 65 20 68 69 6e 74 73 20 74 6f 20 67 65  hese hints to ge
4000: 6e 65 72 61 74 65 20 62 65 74 74 65 72 20 63 6f  nerate better co
4010: 64 65 2c 20 73 6f 6d 65 74 69 6d 65 73 2e 0a 2a  de, sometimes..*
4020: 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  /.#if defined(__
4030: 47 4e 55 43 5f 5f 29 20 26 26 20 30 0a 23 20 64  GNUC__) && 0.# d
4040: 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 58 29 20  efine likely(X) 
4050: 20 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 70     __builtin_exp
4060: 65 63 74 28 28 58 29 2c 31 29 0a 23 20 64 65 66  ect((X),1).# def
4070: 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 29 20  ine unlikely(X) 
4080: 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 70 65 63   __builtin_expec
4090: 74 28 28 58 29 2c 30 29 0a 23 65 6c 73 65 0a 23  t((X),0).#else.#
40a0: 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 58   define likely(X
40b0: 29 20 20 20 20 21 21 28 58 29 0a 23 20 64 65 66  )    !!(X).# def
40c0: 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 29 20  ine unlikely(X) 
40d0: 20 21 21 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f   !!(X).#endif../
40e0: 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
40f0: 65 20 6e 65 65 64 20 61 20 73 6d 61 6c 6c 20 61  e need a small a
4100: 6d 6f 75 6e 74 20 6f 66 20 63 6f 64 65 20 73 75  mount of code su
4110: 63 68 20 61 73 20 61 20 76 61 72 69 61 62 6c 65  ch as a variable
4120: 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a   initialization.
4130: 2a 2a 20 74 6f 20 73 65 74 75 70 20 66 6f 72 20  ** to setup for 
4140: 61 20 6c 61 74 65 72 20 61 73 73 65 72 74 28 29  a later assert()
4150: 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 57 65 20   statement.  We 
4160: 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 69 73  do not want this
4170: 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 61 70 70 65   code to.** appe
4180: 61 72 20 77 68 65 6e 20 61 73 73 65 72 74 28 29  ar when assert()
4190: 20 69 73 20 64 69 73 61 62 6c 65 64 2e 20 20 54   is disabled.  T
41a0: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
41b0: 72 6f 20 69 73 20 74 68 65 72 65 66 6f 72 65 0a  ro is therefore.
41c0: 2a 2a 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 61  ** used to conta
41d0: 69 6e 20 74 68 61 74 20 73 65 74 75 70 20 63 6f  in that setup co
41e0: 64 65 2e 20 20 54 68 65 20 22 56 56 41 22 20 61  de.  The "VVA" a
41f0: 63 72 6f 6e 79 6d 20 73 74 61 6e 64 73 20 66 6f  cronym stands fo
4200: 72 0a 2a 2a 20 22 56 65 72 69 66 69 63 61 74 69  r.** "Verificati
4210: 6f 6e 2c 20 56 61 6c 69 64 61 74 69 6f 6e 2c 20  on, Validation, 
4220: 61 6e 64 20 41 63 63 72 65 64 69 74 61 74 69 6f  and Accreditatio
4230: 6e 22 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  n".  In other wo
4240: 72 64 73 2c 20 74 68 65 0a 2a 2a 20 63 6f 64 65  rds, the.** code
4250: 20 77 69 74 68 69 6e 20 56 56 41 5f 4f 4e 4c 59   within VVA_ONLY
4260: 28 29 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 75 6e  () will only run
4270: 20 64 75 72 69 6e 67 20 76 65 72 69 66 69 63 61   during verifica
4280: 74 69 6f 6e 20 70 72 6f 63 65 73 73 65 73 2e 0a  tion processes..
4290: 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  */.#ifndef NDEBU
42a0: 47 0a 23 20 64 65 66 69 6e 65 20 56 56 41 5f 4f  G.# define VVA_O
42b0: 4e 4c 59 28 58 29 20 20 58 0a 23 65 6c 73 65 0a  NLY(X)  X.#else.
42c0: 23 20 64 65 66 69 6e 65 20 56 56 41 5f 4f 4e 4c  # define VVA_ONL
42d0: 59 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  Y(X).#endif../**
42e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
42f0: 6c 75 64 65 20 73 71 6c 69 74 65 33 2e 68 20 69  lude sqlite3.h i
4300: 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
4310: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
4320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
4330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
4340: 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33 2e  in file sqlite3.
4350: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4360: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4370: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
4380: 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
4390: 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
43a0: 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
43b0: 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
43c0: 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
43d0: 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
43e0: 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
43f0: 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
4400: 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
4410: 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
4420: 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
4430: 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
4440: 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
4450: 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
4460: 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
4470: 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
4480: 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
4490: 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
44a0: 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
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 2a 2a 2a 2a 2a 2a  ****************
44e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44f0: 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  ***.** This head
4500: 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20  er file defines 
4510: 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68  the interface th
4520: 61 74 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  at the SQLite li
4530: 62 72 61 72 79 0a 2a 2a 20 70 72 65 73 65 6e 74  brary.** present
4540: 73 20 74 6f 20 63 6c 69 65 6e 74 20 70 72 6f 67  s to client prog
4550: 72 61 6d 73 2e 20 20 49 66 20 61 20 43 2d 66 75  rams.  If a C-fu
4560: 6e 63 74 69 6f 6e 2c 20 73 74 72 75 63 74 75 72  nction, structur
4570: 65 2c 20 64 61 74 61 74 79 70 65 2c 0a 2a 2a 20  e, datatype,.** 
4580: 6f 72 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69  or constant defi
4590: 6e 69 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  nition does not 
45a0: 61 70 70 65 61 72 20 69 6e 20 74 68 69 73 20 66  appear in this f
45b0: 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a  ile, then it is.
45c0: 2a 2a 20 6e 6f 74 20 61 20 70 75 62 6c 69 73 68  ** not a publish
45d0: 65 64 20 41 50 49 20 6f 66 20 53 51 4c 69 74 65  ed API of SQLite
45e0: 2c 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20  , is subject to 
45f0: 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 0a 2a  change without.*
4600: 2a 20 6e 6f 74 69 63 65 2c 20 61 6e 64 20 73 68  * notice, and sh
4610: 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 66 65  ould not be refe
4620: 72 65 6e 63 65 64 20 62 79 20 70 72 6f 67 72 61  renced by progra
4630: 6d 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69  ms that use SQLi
4640: 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f  te..**.** Some o
4650: 66 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  f the definition
4660: 73 20 74 68 61 74 20 61 72 65 20 69 6e 20 74 68  s that are in th
4670: 69 73 20 66 69 6c 65 20 61 72 65 20 6d 61 72 6b  is file are mark
4680: 65 64 20 61 73 0a 2a 2a 20 22 65 78 70 65 72 69  ed as.** "experi
4690: 6d 65 6e 74 61 6c 22 2e 20 20 45 78 70 65 72 69  mental".  Experi
46a0: 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65  mental interface
46b0: 73 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 6e  s are normally n
46c0: 65 77 0a 2a 2a 20 66 65 61 74 75 72 65 73 20 72  ew.** features r
46d0: 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 74 6f  ecently added to
46e0: 20 53 51 4c 69 74 65 2e 20 20 57 65 20 64 6f 20   SQLite.  We do 
46f0: 6e 6f 74 20 61 6e 74 69 63 69 70 61 74 65 20 63  not anticipate c
4700: 68 61 6e 67 65 73 0a 2a 2a 20 74 6f 20 65 78 70  hanges.** to exp
4710: 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66  erimental interf
4720: 61 63 65 73 20 62 75 74 20 72 65 73 65 72 76 65  aces but reserve
4730: 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63   to make minor c
4740: 68 61 6e 67 65 73 20 69 66 0a 2a 2a 20 65 78 70  hanges if.** exp
4750: 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 65  erience from use
4760: 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20 73   "in the wild" s
4770: 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61 6e  uggest such chan
4780: 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74 2e  ges are prudent.
4790: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 63  .**.** The offic
47a0: 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 41  ial C-language A
47b0: 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  PI documentation
47c0: 20 66 6f 72 20 53 51 4c 69 74 65 20 69 73 20 64   for SQLite is d
47d0: 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 63  erived.** from c
47e0: 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 20  omments in this 
47f0: 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65  file.  This file
4800: 20 69 73 20 74 68 65 20 61 75 74 68 6f 72 69 74   is the authorit
4810: 61 74 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a 20  ative source.** 
4820: 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 20 69 6e  on how SQLite in
4830: 74 65 72 66 61 63 65 73 20 61 72 65 20 73 75 70  terfaces are sup
4840: 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 74 65 2e  pose to operate.
4850: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20  .**.** The name 
4860: 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64  of this file und
4870: 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  er configuration
4880: 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 22   management is "
4890: 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a  sqlite.h.in"..**
48a0: 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d 61   The makefile ma
48b0: 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 63  kes some minor c
48c0: 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73 20 66  hanges to this f
48d0: 69 6c 65 20 28 73 75 63 68 20 61 73 20 69 6e 73  ile (such as ins
48e0: 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 65  erting.** the ve
48f0: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 6e  rsion number) an
4900: 64 20 63 68 61 6e 67 65 73 20 69 74 73 20 6e 61  d changes its na
4910: 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 33 2e 68  me to "sqlite3.h
4920: 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  " as.** part of 
4930: 74 68 65 20 62 75 69 6c 64 20 70 72 6f 63 65 73  the build proces
4940: 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  s..**.** @(#) $I
4950: 64 3a 20 73 71 6c 69 74 65 2e 68 2e 69 6e 2c 76  d: sqlite.h.in,v
4960: 20 31 2e 34 32 31 20 32 30 30 38 2f 31 32 2f 33   1.421 2008/12/3
4970: 30 20 30 36 3a 32 34 3a 35 38 20 64 61 6e 69 65  0 06:24:58 danie
4980: 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
4990: 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 33  #ifndef _SQLITE3
49a0: 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
49b0: 49 54 45 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65  ITE3_H_.#include
49c0: 20 3c 73 74 64 61 72 67 2e 68 3e 20 20 20 20 20   <stdarg.h>     
49d0: 2f 2a 20 4e 65 65 64 65 64 20 66 6f 72 20 74 68  /* Needed for th
49e0: 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  e definition of 
49f0: 76 61 5f 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a  va_list */../*.*
4a00: 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 63  * Make sure we c
4a10: 61 6e 20 63 61 6c 6c 20 74 68 69 73 20 73 74 75  an call this stu
4a20: 66 66 20 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a  ff from C++..*/.
4a30: 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22  #if 0.extern "C"
4a40: 20 7b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a   {.#endif.../*.*
4a50: 2a 20 41 64 64 20 74 68 65 20 61 62 69 6c 69 74  * Add the abilit
4a60: 79 20 74 6f 20 6f 76 65 72 72 69 64 65 20 27 65  y to override 'e
4a70: 78 74 65 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65  xtern'.*/.#ifnde
4a80: 66 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a  f SQLITE_EXTERN.
4a90: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
4aa0: 45 58 54 45 52 4e 20 65 78 74 65 72 6e 0a 23 65  EXTERN extern.#e
4ab0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  ndif../*.** Thes
4ac0: 65 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61  e no-op macros a
4ad0: 72 65 20 75 73 65 64 20 69 6e 20 66 72 6f 6e 74  re used in front
4ae0: 20 6f 66 20 69 6e 74 65 72 66 61 63 65 73 20 74   of interfaces t
4af0: 6f 20 6d 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20  o mark those.** 
4b00: 69 6e 74 65 72 66 61 63 65 73 20 61 73 20 65 69  interfaces as ei
4b10: 74 68 65 72 20 64 65 70 72 65 63 61 74 65 64 20  ther deprecated 
4b20: 6f 72 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e  or experimental.
4b30: 20 20 4e 65 77 20 61 70 70 6c 69 63 61 74 69 6f    New applicatio
4b40: 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74  ns.** should not
4b50: 20 75 73 65 20 64 65 70 72 65 63 61 74 65 64 20   use deprecated 
4b60: 69 6e 74 72 66 61 63 65 73 20 2d 20 74 68 65 79  intrfaces - they
4b70: 20 61 72 65 20 73 75 70 70 6f 72 74 20 66 6f 72   are support for
4b80: 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f   backwards.** co
4b90: 6d 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79  mpatibility only
4ba0: 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 77  .  Application w
4bb0: 72 69 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65  riters should be
4bc0: 20 61 77 61 72 65 20 74 68 61 74 0a 2a 2a 20 65   aware that.** e
4bd0: 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65  xperimental inte
4be0: 72 66 61 63 65 73 20 61 72 65 20 73 75 62 6a 65  rfaces are subje
4bf0: 63 74 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20  ct to change in 
4c00: 70 6f 69 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a  point releases..
4c10: 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72  **.** These macr
4c20: 6f 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c  os used to resol
4c30: 76 65 20 74 6f 20 76 61 72 69 6f 75 73 20 6b 69  ve to various ki
4c40: 6e 64 73 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20  nds of compiler 
4c50: 6d 61 67 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f  magic that.** wo
4c60: 75 6c 64 20 67 65 6e 65 72 61 74 65 20 77 61 72  uld generate war
4c70: 6e 69 6e 67 20 6d 65 73 73 61 67 65 73 20 77 68  ning messages wh
4c80: 65 6e 20 74 68 65 79 20 77 65 72 65 20 75 73 65  en they were use
4c90: 64 2e 20 20 42 75 74 20 74 68 61 74 0a 2a 2a 20  d.  But that.** 
4ca0: 63 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 65  compiler magic e
4cb0: 6e 64 65 64 20 75 70 20 67 65 6e 65 72 61 74 69  nded up generati
4cc0: 6e 67 20 73 75 63 68 20 61 20 66 6c 75 72 72 79  ng such a flurry
4cd0: 20 6f 66 20 62 75 67 20 72 65 70 6f 72 74 73 0a   of bug reports.
4ce0: 2a 2a 20 74 68 61 74 20 77 65 20 68 61 76 65 20  ** that we have 
4cf0: 74 61 6b 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74  taken it all out
4d00: 20 61 6e 64 20 67 6f 6e 65 20 62 61 63 6b 20 74   and gone back t
4d10: 6f 20 75 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a  o using simple.*
4d20: 2a 20 6e 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a  * noop macros..*
4d30: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
4d40: 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64 65 66  _DEPRECATED.#def
4d50: 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 50 45 52  ine SQLITE_EXPER
4d60: 49 4d 45 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45  IMENTAL../*.** E
4d70: 6e 73 75 72 65 20 74 68 65 73 65 20 73 79 6d 62  nsure these symb
4d80: 6f 6c 73 20 77 65 72 65 20 6e 6f 74 20 64 65 66  ols were not def
4d90: 69 6e 65 64 20 62 79 20 73 6f 6d 65 20 70 72 65  ined by some pre
4da0: 76 69 6f 75 73 20 68 65 61 64 65 72 20 66 69 6c  vious header fil
4db0: 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
4dc0: 49 54 45 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e  ITE_VERSION.# un
4dd0: 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49  def SQLITE_VERSI
4de0: 4f 4e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ON.#endif.#ifdef
4df0: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f   SQLITE_VERSION_
4e00: 4e 55 4d 42 45 52 0a 23 20 75 6e 64 65 66 20 53  NUMBER.# undef S
4e10: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
4e20: 4d 42 45 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  MBER.#endif../*.
4e30: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d  ** CAPI3REF: Com
4e40: 70 69 6c 65 2d 54 69 6d 65 20 4c 69 62 72 61 72  pile-Time Librar
4e50: 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72  y Version Number
4e60: 73 20 7b 48 31 30 30 31 30 7d 20 3c 53 36 30 31  s {H10010} <S601
4e70: 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  00>.**.** The SQ
4e80: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64  LITE_VERSION and
4e90: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f   SQLITE_VERSION_
4ea0: 4e 55 4d 42 45 52 20 23 64 65 66 69 6e 65 73 20  NUMBER #defines 
4eb0: 69 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65  in.** the sqlite
4ec0: 33 2e 68 20 66 69 6c 65 20 73 70 65 63 69 66 79  3.h file specify
4ed0: 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20   the version of 
4ee0: 53 51 4c 69 74 65 20 77 69 74 68 20 77 68 69 63  SQLite with whic
4ef0: 68 0a 2a 2a 20 74 68 61 74 20 68 65 61 64 65 72  h.** that header
4f00: 20 66 69 6c 65 20 69 73 20 61 73 73 6f 63 69 61   file is associa
4f10: 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22  ted..**.** The "
4f20: 76 65 72 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69  version" of SQLi
4f30: 74 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f  te is a string o
4f40: 66 20 74 68 65 20 66 6f 72 6d 20 22 58 2e 59 2e  f the form "X.Y.
4f50: 5a 22 2e 0a 2a 2a 20 54 68 65 20 70 68 72 61 73  Z"..** The phras
4f60: 65 20 22 61 6c 70 68 61 22 20 6f 72 20 22 62 65  e "alpha" or "be
4f70: 74 61 22 20 6d 69 67 68 74 20 62 65 20 61 70 70  ta" might be app
4f80: 65 6e 64 65 64 20 61 66 74 65 72 20 74 68 65 20  ended after the 
4f90: 5a 2e 0a 2a 2a 20 54 68 65 20 58 20 76 61 6c 75  Z..** The X valu
4fa0: 65 20 69 73 20 6d 61 6a 6f 72 20 76 65 72 73 69  e is major versi
4fb0: 6f 6e 20 6e 75 6d 62 65 72 20 61 6c 77 61 79 73  on number always
4fc0: 20 33 20 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a   3 in SQLite3..*
4fd0: 2a 20 54 68 65 20 58 20 76 61 6c 75 65 20 6f 6e  * The X value on
4fe0: 6c 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e 20  ly changes when 
4ff0: 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
5000: 69 62 69 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72  ibility is.** br
5010: 6f 6b 65 6e 20 61 6e 64 20 77 65 20 69 6e 74 65  oken and we inte
5020: 6e 64 20 74 6f 20 6e 65 76 65 72 20 62 72 65 61  nd to never brea
5030: 6b 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  k backwards comp
5040: 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68  atibility..** Th
5050: 65 20 59 20 76 61 6c 75 65 20 69 73 20 74 68 65  e Y value is the
5060: 20 6d 69 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e   minor version n
5070: 75 6d 62 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63  umber and only c
5080: 68 61 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74  hanges when.** t
5090: 68 65 72 65 20 61 72 65 20 6d 61 6a 6f 72 20 66  here are major f
50a0: 65 61 74 75 72 65 20 65 6e 68 61 6e 63 65 6d 65  eature enhanceme
50b0: 6e 74 73 20 74 68 61 74 20 61 72 65 20 66 6f 72  nts that are for
50c0: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65  wards compatible
50d0: 0a 2a 2a 20 62 75 74 20 6e 6f 74 20 62 61 63 6b  .** but not back
50e0: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65  wards compatible
50f0: 2e 0a 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65  ..** The Z value
5100: 20 69 73 20 74 68 65 20 72 65 6c 65 61 73 65 20   is the release 
5110: 6e 75 6d 62 65 72 20 61 6e 64 20 69 73 20 69 6e  number and is in
5120: 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a  cremented with.*
5130: 2a 20 65 61 63 68 20 72 65 6c 65 61 73 65 20 62  * each release b
5140: 75 74 20 72 65 73 65 74 73 20 62 61 63 6b 20 74  ut resets back t
5150: 6f 20 30 20 77 68 65 6e 65 76 65 72 20 59 20 69  o 0 whenever Y i
5160: 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a  s incremented..*
5170: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
5180: 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
5190: 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  on()] and [sqlit
51a0: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  e3_libversion_nu
51b0: 6d 62 65 72 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49  mber()]..**.** I
51c0: 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
51d0: 20 7b 48 31 30 30 31 31 7d 20 54 68 65 20 53 51   {H10011} The SQ
51e0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 20 23 64 65  LITE_VERSION #de
51f0: 66 69 6e 65 20 69 6e 20 74 68 65 20 73 71 6c 69  fine in the sqli
5200: 74 65 33 2e 68 20 68 65 61 64 65 72 20 66 69 6c  te3.h header fil
5210: 65 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20  e shall.**      
5220: 20 20 20 20 65 76 61 6c 75 61 74 65 20 74 6f 20      evaluate to 
5230: 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c  a string literal
5240: 20 74 68 61 74 20 69 73 20 74 68 65 20 53 51 4c   that is the SQL
5250: 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20  ite version.**  
5260: 20 20 20 20 20 20 20 20 77 69 74 68 20 77 68 69          with whi
5270: 63 68 20 74 68 65 20 68 65 61 64 65 72 20 66 69  ch the header fi
5280: 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64  le is associated
5290: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 30 31 34 7d  ..**.** {H10014}
52a0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53   The SQLITE_VERS
52b0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64 65 66 69  ION_NUMBER #defi
52c0: 6e 65 20 73 68 61 6c 6c 20 72 65 73 6f 6c 76 65  ne shall resolve
52d0: 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a   to an integer.*
52e0: 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68 20  *          with 
52f0: 74 68 65 20 76 61 6c 75 65 20 28 58 2a 31 30 30  the value (X*100
5300: 30 30 30 30 20 2b 20 59 2a 31 30 30 30 20 2b 20  0000 + Y*1000 + 
5310: 5a 29 20 77 68 65 72 65 20 58 2c 20 59 2c 20 61  Z) where X, Y, a
5320: 6e 64 20 5a 0a 2a 2a 20 20 20 20 20 20 20 20 20  nd Z.**         
5330: 20 61 72 65 20 74 68 65 20 6d 61 6a 6f 72 20 76   are the major v
5340: 65 72 73 69 6f 6e 2c 20 6d 69 6e 6f 72 20 76 65  ersion, minor ve
5350: 72 73 69 6f 6e 2c 20 61 6e 64 20 72 65 6c 65 61  rsion, and relea
5360: 73 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 23 64  se number..*/.#d
5370: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 45 52  efine SQLITE_VER
5380: 53 49 4f 4e 20 20 20 20 20 20 20 20 20 22 33 2e  SION         "3.
5390: 36 2e 31 30 22 0a 23 64 65 66 69 6e 65 20 53 51  6.10".#define SQ
53a0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d  LITE_VERSION_NUM
53b0: 42 45 52 20 20 33 30 30 36 30 31 30 0a 0a 2f 2a  BER  3006010../*
53c0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75  .** CAPI3REF: Ru
53d0: 6e 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56  n-Time Library V
53e0: 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b  ersion Numbers {
53f0: 48 31 30 30 32 30 7d 20 3c 53 36 30 31 30 30 3e  H10020} <S60100>
5400: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71  .** KEYWORDS: sq
5410: 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a  lite3_version.**
5420: 0a 2a 2a 20 54 68 65 73 65 20 66 65 61 74 75 72  .** These featur
5430: 65 73 20 70 72 6f 76 69 64 65 20 74 68 65 20 73  es provide the s
5440: 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ame information 
5450: 61 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 56  as the [SQLITE_V
5460: 45 52 53 49 4f 4e 5d 0a 2a 2a 20 61 6e 64 20 5b  ERSION].** and [
5470: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e  SQLITE_VERSION_N
5480: 55 4d 42 45 52 5d 20 23 64 65 66 69 6e 65 73 20  UMBER] #defines 
5490: 69 6e 20 74 68 65 20 68 65 61 64 65 72 2c 20 62  in the header, b
54a0: 75 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65  ut are associate
54b0: 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6c 69  d.** with the li
54c0: 62 72 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66  brary instead of
54d0: 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65   the header file
54e0: 2e 20 20 43 61 75 74 69 6f 75 73 20 70 72 6f 67  .  Cautious prog
54f0: 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 0a 2a 2a  rammers might.**
5500: 20 69 6e 63 6c 75 64 65 20 61 20 63 68 65 63 6b   include a check
5510: 20 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69 63   in their applic
5520: 61 74 69 6f 6e 20 74 6f 20 76 65 72 69 66 79 20  ation to verify 
5530: 74 68 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  that.** sqlite3_
5540: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
5550: 72 28 29 20 61 6c 77 61 79 73 20 72 65 74 75 72  r() always retur
5560: 6e 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  ns the value.** 
5570: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f  [SQLITE_VERSION_
5580: 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 54  NUMBER]..**.** T
5590: 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  he sqlite3_libve
55a0: 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e  rsion() function
55b0: 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d   returns the sam
55c0: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73  e information as
55d0: 20 69 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71   is.** in the sq
55e0: 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20  lite3_version[] 
55f0: 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 2e  string constant.
5600: 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 69    The function i
5610: 73 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 66 6f  s provided.** fo
5620: 72 20 75 73 65 20 69 6e 20 44 4c 4c 73 20 73 69  r use in DLLs si
5630: 6e 63 65 20 44 4c 4c 20 75 73 65 72 73 20 75 73  nce DLL users us
5640: 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 20 68 61 76  ually do not hav
5650: 65 20 64 69 72 65 63 74 20 61 63 63 65 73 73 20  e direct access 
5660: 74 6f 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e  to string.** con
5670: 73 74 61 6e 74 73 20 77 69 74 68 69 6e 20 74 68  stants within th
5680: 65 20 44 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  e DLL..**.** INV
5690: 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
56a0: 48 31 30 30 32 31 7d 20 54 68 65 20 5b 73 71 6c  H10021} The [sql
56b0: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
56c0: 6e 75 6d 62 65 72 28 29 5d 20 69 6e 74 65 72 66  number()] interf
56d0: 61 63 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e  ace shall return
56e0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 20  .**          an 
56f0: 69 6e 74 65 67 65 72 20 65 71 75 61 6c 20 74 6f  integer equal to
5700: 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e   [SQLITE_VERSION
5710: 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20  _NUMBER]..**.** 
5720: 7b 48 31 30 30 32 32 7d 20 54 68 65 20 5b 73 71  {H10022} The [sq
5730: 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5d 20 73  lite3_version] s
5740: 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 73  tring constant s
5750: 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20  hall contain.** 
5760: 20 20 20 20 20 20 20 20 20 74 68 65 20 74 65 78           the tex
5770: 74 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45  t of the [SQLITE
5780: 5f 56 45 52 53 49 4f 4e 5d 20 73 74 72 69 6e 67  _VERSION] string
5790: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 30 32 33 7d  ..**.** {H10023}
57a0: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69   The [sqlite3_li
57b0: 62 76 65 72 73 69 6f 6e 28 29 5d 20 66 75 6e 63  bversion()] func
57c0: 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72  tion shall retur
57d0: 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20  n.**          a 
57e0: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b  pointer to the [
57f0: 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5d  sqlite3_version]
5800: 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74   string constant
5810: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
5820: 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
5830: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53  e3_version[] = S
5840: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53  QLITE_VERSION;.S
5850: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
5860: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69  char *sqlite3_li
5870: 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a  bversion(void);.
5880: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
5890: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
58a0: 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a  n_number(void);.
58b0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
58c0: 20 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20   Test To See If 
58d0: 54 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54  The Library Is T
58e0: 68 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30  hreadsafe {H1010
58f0: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S60100>.**.*
5900: 2a 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20  * SQLite can be 
5910: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72  compiled with or
5920: 20 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73   without mutexes
5930: 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b  .  When.** the [
5940: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
5950: 45 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f  E] C preprocesso
5960: 72 20 6d 61 63 72 6f 20 31 20 6f 72 20 32 2c 20  r macro 1 or 2, 
5970: 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65  mutexes.** are e
5980: 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c 69 74  nabled and SQLit
5990: 65 20 69 73 20 74 68 72 65 61 64 73 61 66 65 2e  e is threadsafe.
59a0: 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53    When the.** [S
59b0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
59c0: 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a  ] macro is 0, .*
59d0: 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 72  * the mutexes ar
59e0: 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 74 68  e omitted.  With
59f0: 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 73 2c  out the mutexes,
5a00: 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a   it is not safe.
5a10: 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 74 65  ** to use SQLite
5a20: 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 66 72   concurrently fr
5a30: 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  om more than one
5a40: 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45   thread..**.** E
5a50: 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65 73 20  nabling mutexes 
5a60: 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75 72 61  incurs a measura
5a70: 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ble performance 
5a80: 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69  penalty..** So i
5a90: 66 20 73 70 65 65 64 20 69 73 20 6f 66 20 75 74  f speed is of ut
5aa0: 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c  most importance,
5ab0: 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73 65 20   it makes sense 
5ac0: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68  to disable.** th
5ad0: 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75 74 20  e mutexes.  But 
5ae0: 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61 66 65  for maximum safe
5af0: 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68 6f 75  ty, mutexes shou
5b00: 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a  ld be enabled..*
5b10: 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 65  * The default be
5b20: 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75  havior is for mu
5b30: 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62  texes to be enab
5b40: 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  led..**.** This 
5b50: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65  interface can be
5b60: 20 75 73 65 64 20 62 79 20 61 20 70 72 6f 67 72   used by a progr
5b70: 61 6d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  am to make sure 
5b80: 74 68 61 74 20 74 68 65 0a 2a 2a 20 76 65 72 73  that the.** vers
5b90: 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68  ion of SQLite th
5ba0: 61 74 20 69 74 20 69 73 20 6c 69 6e 6b 69 6e 67  at it is linking
5bb0: 20 61 67 61 69 6e 73 74 20 77 61 73 20 63 6f 6d   against was com
5bc0: 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68  piled with.** th
5bd0: 65 20 64 65 73 69 72 65 64 20 73 65 74 74 69 6e  e desired settin
5be0: 67 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45  g of the [SQLITE
5bf0: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63  _THREADSAFE] mac
5c00: 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ro..**.** This i
5c10: 6e 74 65 72 66 61 63 65 20 6f 6e 6c 79 20 72 65  nterface only re
5c20: 70 6f 72 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d  ports on the com
5c30: 70 69 6c 65 2d 74 69 6d 65 20 6d 75 74 65 78 20  pile-time mutex 
5c40: 73 65 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68  setting.** of th
5c50: 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44  e [SQLITE_THREAD
5c60: 53 41 46 45 5d 20 66 6c 61 67 2e 20 20 49 66 20  SAFE] flag.  If 
5c70: 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
5c80: 65 64 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54  ed with.** SQLIT
5c90: 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31 20 74  E_THREADSAFE=1 t
5ca0: 68 65 6e 20 6d 75 74 65 78 65 73 20 61 72 65 20  hen mutexes are 
5cb0: 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 75  enabled by defau
5cc0: 6c 74 20 62 75 74 0a 2a 2a 20 63 61 6e 20 62 65  lt but.** can be
5cd0: 20 66 75 6c 6c 79 20 6f 72 20 70 61 72 74 69 61   fully or partia
5ce0: 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 75 73 69  lly disabled usi
5cf0: 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71  ng a call to [sq
5d00: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 0a  lite3_config()].
5d10: 2a 2a 20 77 69 74 68 20 74 68 65 20 76 65 72 62  ** with the verb
5d20: 73 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  s [SQLITE_CONFIG
5d30: 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 2c 20  _SINGLETHREAD], 
5d40: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
5d50: 55 4c 54 49 54 48 52 45 41 44 5d 2c 0a 2a 2a 20  ULTITHREAD],.** 
5d60: 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  or [SQLITE_CONFI
5d70: 47 5f 4d 55 54 45 58 5d 2e 20 20 54 68 65 20 72  G_MUTEX].  The r
5d80: 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74  eturn value of t
5d90: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  his function sho
5da0: 77 73 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65 20 64  ws.** only the d
5db0: 65 66 61 75 6c 74 20 63 6f 6d 70 69 6c 65 2d 74  efault compile-t
5dc0: 69 6d 65 20 73 65 74 74 69 6e 67 2c 20 6e 6f 74  ime setting, not
5dd0: 20 61 6e 79 20 72 75 6e 2d 74 69 6d 65 20 63 68   any run-time ch
5de0: 61 6e 67 65 73 0a 2a 2a 20 74 6f 20 74 68 61 74  anges.** to that
5df0: 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   setting..**.** 
5e00: 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69  See the [threadi
5e10: 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e  ng mode] documen
5e20: 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74  tation for addit
5e30: 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
5e40: 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  n..**.** INVARIA
5e50: 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 31  NTS:.**.** {H101
5e60: 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  01} The [sqlite3
5e70: 5f 74 68 72 65 61 64 73 61 66 65 28 29 5d 20 66  _threadsafe()] f
5e80: 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65  unction shall re
5e90: 74 75 72 6e 20 7a 65 72 6f 20 69 66 0a 2a 2a 20  turn zero if.** 
5ea0: 20 20 20 20 20 20 20 20 20 61 6e 64 20 6f 6e 6c           and onl
5eb0: 79 20 69 66 20 53 51 4c 69 74 65 20 77 61 73 20  y if SQLite was 
5ec0: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6d 75  compiled with mu
5ed0: 74 65 78 69 6e 67 20 63 6f 64 65 20 6f 6d 69 74  texing code omit
5ee0: 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 31  ted..**.** {H101
5ef0: 30 32 7d 20 54 68 65 20 76 61 6c 75 65 20 72 65  02} The value re
5f00: 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 5b 73  turned by the [s
5f10: 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66  qlite3_threadsaf
5f20: 65 28 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  e()] function.**
5f30: 20 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20            shall 
5f40: 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d 65 20  remain the same 
5f50: 61 63 72 6f 73 73 20 63 61 6c 6c 73 20 74 6f 20  across calls to 
5f60: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
5f70: 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
5f80: 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 68  I int sqlite3_th
5f90: 72 65 61 64 73 61 66 65 28 76 6f 69 64 29 3b 0a  readsafe(void);.
5fa0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
5fb0: 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63   Database Connec
5fc0: 74 69 6f 6e 20 48 61 6e 64 6c 65 20 7b 48 31 32  tion Handle {H12
5fd0: 30 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a  000} <S40200>.**
5fe0: 20 4b 45 59 57 4f 52 44 53 3a 20 7b 64 61 74 61   KEYWORDS: {data
5ff0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d  base connection}
6000: 20 7b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   {database conne
6010: 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61  ctions}.**.** Ea
6020: 63 68 20 6f 70 65 6e 20 53 51 4c 69 74 65 20 64  ch open SQLite d
6030: 61 74 61 62 61 73 65 20 69 73 20 72 65 70 72 65  atabase is repre
6040: 73 65 6e 74 65 64 20 62 79 20 61 20 70 6f 69 6e  sented by a poin
6050: 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e  ter to an instan
6060: 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 61  ce of.** the opa
6070: 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e 61  que structure na
6080: 6d 65 64 20 22 73 71 6c 69 74 65 33 22 2e 20 20  med "sqlite3".  
6090: 49 74 20 69 73 20 75 73 65 66 75 6c 20 74 6f 20  It is useful to 
60a0: 74 68 69 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69  think of an sqli
60b0: 74 65 33 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61  te3.** pointer a
60c0: 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68  s an object.  Th
60d0: 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  e [sqlite3_open(
60e0: 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
60f0: 6e 31 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b  n16()], and.** [
6100: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
6110: 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  )] interfaces ar
6120: 65 20 69 74 73 20 63 6f 6e 73 74 72 75 63 74 6f  e its constructo
6130: 72 73 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  rs, and [sqlite3
6140: 5f 63 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 20  _close()].** is 
6150: 69 74 73 20 64 65 73 74 72 75 63 74 6f 72 2e 20  its destructor. 
6160: 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20   There are many 
6170: 6f 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73  other interfaces
6180: 20 28 73 75 63 68 20 61 73 0a 2a 2a 20 5b 73 71   (such as.** [sq
6190: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
61a0: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72  ()], [sqlite3_cr
61b0: 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d  eate_function()]
61c0: 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65  , and.** [sqlite
61d0: 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29  3_busy_timeout()
61e0: 5d 20 74 6f 20 6e 61 6d 65 20 62 75 74 20 74 68  ] to name but th
61f0: 72 65 65 29 20 74 68 61 74 20 61 72 65 20 6d 65  ree) that are me
6200: 74 68 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73  thods on an.** s
6210: 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 2e 0a 2a  qlite3 object..*
6220: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
6230: 20 73 71 6c 69 74 65 33 20 73 71 6c 69 74 65 33   sqlite3 sqlite3
6240: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
6250: 46 3a 20 36 34 2d 42 69 74 20 49 6e 74 65 67 65  F: 64-Bit Intege
6260: 72 20 54 79 70 65 73 20 7b 48 31 30 32 30 30 7d  r Types {H10200}
6270: 20 3c 53 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 59   <S10110>.** KEY
6280: 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 5f 69 6e  WORDS: sqlite_in
6290: 74 36 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  t64 sqlite_uint6
62a0: 34 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20  4.**.** Because 
62b0: 74 68 65 72 65 20 69 73 20 6e 6f 20 63 72 6f 73  there is no cros
62c0: 73 2d 70 6c 61 74 66 6f 72 6d 20 77 61 79 20 74  s-platform way t
62d0: 6f 20 73 70 65 63 69 66 79 20 36 34 2d 62 69 74  o specify 64-bit
62e0: 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 0a 2a   integer types.*
62f0: 2a 20 53 51 4c 69 74 65 20 69 6e 63 6c 75 64 65  * SQLite include
6300: 73 20 74 79 70 65 64 65 66 73 20 66 6f 72 20 36  s typedefs for 6
6310: 34 2d 62 69 74 20 73 69 67 6e 65 64 20 61 6e 64  4-bit signed and
6320: 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
6330: 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  rs..**.** The sq
6340: 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 6e 64 20  lite3_int64 and 
6350: 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 61  sqlite3_uint64 a
6360: 72 65 20 74 68 65 20 70 72 65 66 65 72 72 65 64  re the preferred
6370: 20 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e   type definition
6380: 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  s..** The sqlite
6390: 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74  _int64 and sqlit
63a0: 65 5f 75 69 6e 74 36 34 20 74 79 70 65 73 20 61  e_uint64 types a
63b0: 72 65 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72  re supported for
63c0: 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f   backwards.** co
63d0: 6d 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79  mpatibility only
63e0: 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
63f0: 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30  TS:.**.** {H1020
6400: 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 5f 69  1} The [sqlite_i
6410: 6e 74 36 34 5d 20 61 6e 64 20 5b 73 71 6c 69 74  nt64] and [sqlit
6420: 65 33 5f 69 6e 74 36 34 5d 20 74 79 70 65 20 73  e3_int64] type s
6430: 68 61 6c 6c 20 73 70 65 63 69 66 79 0a 2a 2a 20  hall specify.** 
6440: 20 20 20 20 20 20 20 20 20 61 20 36 34 2d 62 69           a 64-bi
6450: 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
6460: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30 32 7d  ..**.** {H10202}
6470: 20 54 68 65 20 5b 73 71 6c 69 74 65 5f 75 69 6e   The [sqlite_uin
6480: 74 36 34 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  t64] and [sqlite
6490: 33 5f 75 69 6e 74 36 34 5d 20 74 79 70 65 20 73  3_uint64] type s
64a0: 68 61 6c 6c 20 73 70 65 63 69 66 79 0a 2a 2a 20  hall specify.** 
64b0: 20 20 20 20 20 20 20 20 20 61 20 36 34 2d 62 69           a 64-bi
64c0: 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  t unsigned integ
64d0: 65 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  er..*/.#ifdef SQ
64e0: 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a  LITE_INT64_TYPE.
64f0: 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45    typedef SQLITE
6500: 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69  _INT64_TYPE sqli
6510: 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65  te_int64;.  type
6520: 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c  def unsigned SQL
6530: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73  ITE_INT64_TYPE s
6540: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65  qlite_uint64;.#e
6550: 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43  lif defined(_MSC
6560: 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64  _VER) || defined
6570: 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20  (__BORLANDC__). 
6580: 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34   typedef __int64
6590: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20   sqlite_int64;. 
65a0: 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65   typedef unsigne
65b0: 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  d __int64 sqlite
65c0: 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20  _uint64;.#else. 
65d0: 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f   typedef long lo
65e0: 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e  ng int sqlite_in
65f0: 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75  t64;.  typedef u
6600: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
6610: 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e  g int sqlite_uin
6620: 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65  t64;.#endif.type
6630: 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  def sqlite_int64
6640: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a   sqlite3_int64;.
6650: 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75  typedef sqlite_u
6660: 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69  int64 sqlite3_ui
6670: 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  nt64;../*.** If 
6680: 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20  compiling for a 
6690: 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c  processor that l
66a0: 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f  acks floating po
66b0: 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20  int support,.** 
66c0: 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67  substitute integ
66d0: 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d  er for floating-
66e0: 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66  point..*/.#ifdef
66f0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
6700: 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65  ATING_POINT.# de
6710: 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69  fine double sqli
6720: 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66  te3_int64.#endif
6730: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
6740: 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61  : Closing A Data
6750: 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20  base Connection 
6760: 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30  {H12010} <S30100
6770: 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20  ><S40200>.**.** 
6780: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
6790: 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  the destructor f
67a0: 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d  or the [sqlite3]
67b0: 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41   object..**.** A
67c0: 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75  pplications shou
67d0: 6c 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61  ld [sqlite3_fina
67e0: 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d  lize | finalize]
67f0: 20 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73   all [prepared s
6800: 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e  tatements].** an
6810: 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  d [sqlite3_blob_
6820: 63 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61  close | close] a
6830: 6c 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73  ll [BLOB handles
6840: 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  ] associated wit
6850: 68 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65  h.** the [sqlite
6860: 33 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20  3] object prior 
6870: 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f  to attempting to
6880: 20 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63   close the objec
6890: 74 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74  t..** The [sqlit
68a0: 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 29 5d 20  e3_next_stmt()] 
68b0: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65  interface can be
68c0: 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20   used to locate 
68d0: 61 6c 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64  all.** [prepared
68e0: 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 61 73 73   statements] ass
68f0: 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 5b  ociated with a [
6900: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
6910: 69 6f 6e 5d 20 69 66 20 64 65 73 69 72 65 64 2e  ion] if desired.
6920: 0a 2a 2a 20 54 79 70 69 63 61 6c 20 63 6f 64 65  .** Typical code
6930: 20 6d 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 6b 65   might look like
6940: 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c   this:.**.** <bl
6950: 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a  ockquote><pre>.*
6960: 2a 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  * sqlite3_stmt *
6970: 70 53 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c 65 28  pStmt;.** while(
6980: 20 28 70 53 74 6d 74 20 3d 20 73 71 6c 69 74 65   (pStmt = sqlite
6990: 33 5f 6e 65 78 74 5f 73 74 6d 74 28 64 62 2c 20  3_next_stmt(db, 
69a0: 30 29 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 6e 62  0))!=0 ){.** &nb
69b0: 73 70 3b 20 20 20 73 71 6c 69 74 65 33 5f 66 69  sp;   sqlite3_fi
69c0: 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 2a  nalize(pStmt);.*
69d0: 2a 20 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  * }.** </pre></b
69e0: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
69f0: 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f   If [sqlite3_clo
6a00: 73 65 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64  se()] is invoked
6a10: 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63   while a transac
6a20: 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a 2a  tion is open,.**
6a30: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
6a40: 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
6a50: 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  y rolled back..*
6a60: 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
6a70: 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30 31 31 7d 20  .**.** {H12011} 
6a80: 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c  A successful cal
6a90: 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c  l to [sqlite3_cl
6aa0: 6f 73 65 28 43 29 5d 20 73 68 61 6c 6c 20 64 65  ose(C)] shall de
6ab0: 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20 20  stroy the.**    
6ac0: 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20        [database 
6ad0: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 6a 65  connection] obje
6ae0: 63 74 20 43 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  ct C..**.** {H12
6af0: 30 31 32 7d 20 41 20 73 75 63 63 65 73 73 66 75  012} A successfu
6b00: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
6b10: 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 73 68 61  e3_close(C)] sha
6b20: 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ll return SQLITE
6b30: 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30  _OK..**.** {H120
6b40: 31 33 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  13} A successful
6b50: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
6b60: 33 5f 63 6c 6f 73 65 28 43 29 5d 20 73 68 61 6c  3_close(C)] shal
6b70: 6c 20 72 65 6c 65 61 73 65 20 61 6c 6c 0a 2a 2a  l release all.**
6b80: 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79            memory
6b90: 20 61 6e 64 20 73 79 73 74 65 6d 20 72 65 73 6f   and system reso
6ba0: 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64  urces associated
6bb0: 20 77 69 74 68 20 5b 64 61 74 61 62 61 73 65 20   with [database 
6bc0: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 20  connection].**  
6bd0: 20 20 20 20 20 20 20 20 43 2e 0a 2a 2a 0a 2a 2a          C..**.**
6be0: 20 7b 48 31 32 30 31 34 7d 20 41 20 63 61 6c 6c   {H12014} A call
6bf0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f   to [sqlite3_clo
6c00: 73 65 28 43 29 5d 20 6f 6e 20 61 20 5b 64 61 74  se(C)] on a [dat
6c10: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
6c20: 5d 20 43 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  ] C that.**     
6c30: 20 20 20 20 20 68 61 73 20 6f 6e 65 20 6f 72 20       has one or 
6c40: 6d 6f 72 65 20 6f 70 65 6e 20 5b 70 72 65 70 61  more open [prepa
6c50: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20  red statements] 
6c60: 73 68 61 6c 6c 20 66 61 69 6c 20 77 69 74 68 0a  shall fail with.
6c70: 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 20 5b  **          an [
6c80: 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72  SQLITE_BUSY] err
6c90: 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b  or code..**.** {
6ca0: 48 31 32 30 31 35 7d 20 41 20 63 61 6c 6c 20 74  H12015} A call t
6cb0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  o [sqlite3_close
6cc0: 28 43 29 5d 20 77 68 65 72 65 20 43 20 69 73 20  (C)] where C is 
6cd0: 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73  a NULL pointer s
6ce0: 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  hall.**         
6cf0: 20 62 65 20 61 20 68 61 72 6d 6c 65 73 73 20 6e   be a harmless n
6d00: 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 53  o-op returning S
6d10: 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20  QLITE_OK..**.** 
6d20: 7b 48 31 32 30 31 39 7d 20 57 68 65 6e 20 5b 73  {H12019} When [s
6d30: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d  qlite3_close(C)]
6d40: 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61   is invoked on a
6d50: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
6d60: 63 74 69 6f 6e 5d 20 43 0a 2a 2a 20 20 20 20 20  ction] C.**     
6d70: 20 20 20 20 20 74 68 61 74 20 68 61 73 20 61 20       that has a 
6d80: 70 65 6e 64 69 6e 67 20 74 72 61 6e 73 61 63 74  pending transact
6d90: 69 6f 6e 2c 20 74 68 65 20 74 72 61 6e 73 61 63  ion, the transac
6da0: 74 69 6f 6e 20 73 68 61 6c 6c 20 62 65 0a 2a 2a  tion shall be.**
6db0: 20 20 20 20 20 20 20 20 20 20 72 6f 6c 6c 65 64            rolled
6dc0: 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 41 53 53   back..**.** ASS
6dd0: 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20  UMPTIONS:.**.** 
6de0: 7b 41 31 32 30 31 36 7d 20 54 68 65 20 43 20 70  {A12016} The C p
6df0: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
6e00: 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 6d  ite3_close(C)] m
6e10: 75 73 74 20 62 65 20 65 69 74 68 65 72 20 61 20  ust be either a 
6e20: 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20  NULL.**         
6e30: 20 70 6f 69 6e 74 65 72 20 6f 72 20 61 6e 20 5b   pointer or an [
6e40: 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 20  sqlite3] object 
6e50: 70 6f 69 6e 74 65 72 20 6f 62 74 61 69 6e 65 64  pointer obtained
6e60: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f  .**          fro
6e70: 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  m [sqlite3_open(
6e80: 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
6e90: 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20  n16()], or.**   
6ea0: 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
6eb0: 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20  open_v2()], and 
6ec0: 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63  not previously c
6ed0: 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  losed..*/.SQLITE
6ee0: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
6ef0: 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a  _close(sqlite3 *
6f00: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79  );../*.** The ty
6f10: 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63  pe for a callbac
6f20: 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54  k function..** T
6f30: 68 69 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e  his is legacy an
6f40: 64 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 49  d deprecated.  I
6f50: 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f  t is included fo
6f60: 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20  r historical.** 
6f70: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e  compatibility an
6f80: 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e  d is not documen
6f90: 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ted..*/.typedef 
6fa0: 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61  int (*sqlite3_ca
6fb0: 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e  llback)(void*,in
6fc0: 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a  t,char**, char**
6fd0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
6fe0: 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65  EF: One-Step Que
6ff0: 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74  ry Execution Int
7000: 65 72 66 61 63 65 20 7b 48 31 32 31 30 30 7d 20  erface {H12100} 
7010: 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10000>.**.** T
7020: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  he sqlite3_exec(
7030: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 61  ) interface is a
7040: 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20   convenient way 
7050: 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f  of running one o
7060: 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 73 74  r more.** SQL st
7070: 61 74 65 6d 65 6e 74 73 20 77 69 74 68 6f 75 74  atements without
7080: 20 68 61 76 69 6e 67 20 74 6f 20 77 72 69 74 65   having to write
7090: 20 61 20 6c 6f 74 20 6f 66 20 43 20 63 6f 64 65   a lot of C code
70a0: 2e 20 20 54 68 65 20 55 54 46 2d 38 20 65 6e 63  .  The UTF-8 enc
70b0: 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74  oded.** SQL stat
70c0: 65 6d 65 6e 74 73 20 61 72 65 20 70 61 73 73 65  ements are passe
70d0: 64 20 69 6e 20 61 73 20 74 68 65 20 73 65 63 6f  d in as the seco
70e0: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
70f0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a  sqlite3_exec()..
7100: 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74  ** The statement
7110: 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20  s are evaluated 
7120: 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e 74 69 6c  one by one until
7130: 20 65 69 74 68 65 72 20 61 6e 20 65 72 72 6f 72   either an error
7140: 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 72   or.** an interr
7150: 75 70 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  upt is encounter
7160: 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20 74 68 65  ed, or until the
7170: 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e 65 2e 20  y are all done. 
7180: 20 54 68 65 20 33 72 64 20 70 61 72 61 6d 65 74   The 3rd paramet
7190: 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 74 69  er.** is an opti
71a0: 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 74 68  onal callback th
71b0: 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  at is invoked on
71c0: 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20  ce for each row 
71d0: 6f 66 20 61 6e 79 20 71 75 65 72 79 0a 2a 2a 20  of any query.** 
71e0: 72 65 73 75 6c 74 73 20 70 72 6f 64 75 63 65 64  results produced
71f0: 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74   by the SQL stat
7200: 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 35 74 68  ements.  The 5th
7210: 20 70 61 72 61 6d 65 74 65 72 20 74 65 6c 6c 73   parameter tells
7220: 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20 77 72 69   where.** to wri
7230: 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73  te any error mes
7240: 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  sages..**.** The
7250: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70   error message p
7260: 61 73 73 65 64 20 62 61 63 6b 20 74 68 72 6f 75  assed back throu
7270: 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61 6d  gh the 5th param
7280: 65 74 65 72 20 69 73 20 68 65 6c 64 0a 2a 2a 20  eter is held.** 
7290: 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  in memory obtain
72a0: 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
72b0: 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 6f 20  _malloc()].  To 
72c0: 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c  avoid a memory l
72d0: 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c  eak,.** the call
72e0: 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  ing application 
72f0: 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 71 6c  should call [sql
7300: 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20  ite3_free()] on 
7310: 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73  any error.** mes
7320: 73 61 67 65 20 72 65 74 75 72 6e 65 64 20 74 68  sage returned th
7330: 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61  rough the 5th pa
7340: 72 61 6d 65 74 65 72 20 77 68 65 6e 20 69 74 20  rameter when it 
7350: 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69  has finished usi
7360: 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20  ng.** the error 
7370: 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49  message..**.** I
7380: 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  f the SQL statem
7390: 65 6e 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70  ent in the 2nd p
73a0: 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c  arameter is NULL
73b0: 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72   or an empty str
73c0: 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73 74 72 69  ing.** or a stri
73d0: 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e  ng containing on
73e0: 6c 79 20 77 68 69 74 65 73 70 61 63 65 20 61 6e  ly whitespace an
73f0: 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65 6e  d comments, then
7400: 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65   no SQL.** state
7410: 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61  ments are evalua
7420: 74 65 64 20 61 6e 64 20 74 68 65 20 64 61 74 61  ted and the data
7430: 62 61 73 65 20 69 73 20 6e 6f 74 20 63 68 61 6e  base is not chan
7440: 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ged..**.** The s
7450: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e  qlite3_exec() in
7460: 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65  terface is imple
7470: 6d 65 6e 74 65 64 20 69 6e 20 74 65 72 6d 73 20  mented in terms 
7480: 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70  of.** [sqlite3_p
7490: 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73  repare_v2()], [s
74a0: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20  qlite3_step()], 
74b0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  and [sqlite3_fin
74c0: 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 68 65  alize()]..** The
74d0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
74e0: 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74  routine does not
74f0: 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61  hing to the data
7500: 62 61 73 65 20 74 68 61 74 20 63 61 6e 6e 6f 74  base that cannot
7510: 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b   be done.** by [
7520: 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
7530: 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  v2()], [sqlite3_
7540: 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71  step()], and [sq
7550: 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
7560: 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  ]..**.** INVARIA
7570: 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31  NTS:.**.** {H121
7580: 30 31 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  01} A successful
7590: 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b   invocation of [
75a0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53  sqlite3_exec(D,S
75b0: 2c 43 2c 41 2c 45 29 5d 0a 2a 2a 20 20 20 20 20  ,C,A,E)].**     
75c0: 20 20 20 20 20 73 68 61 6c 6c 20 73 65 71 75 65       shall seque
75d0: 6e 74 69 61 6c 6c 79 20 65 76 61 6c 75 61 74 65  ntially evaluate
75e0: 20 61 6c 6c 20 6f 66 20 74 68 65 20 55 54 46 2d   all of the UTF-
75f0: 38 20 65 6e 63 6f 64 65 64 2c 0a 2a 2a 20 20 20  8 encoded,.**   
7600: 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c 6f 6e         semicolon
7610: 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c 20 73  -separated SQL s
7620: 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65  tatements in the
7630: 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
7640: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 72  .**          str
7650: 69 6e 67 20 53 20 77 69 74 68 69 6e 20 74 68 65  ing S within the
7660: 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   context of the 
7670: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
7680: 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b  tion] D..**.** {
7690: 48 31 32 31 30 32 7d 20 49 66 20 74 68 65 20 53  H12102} If the S
76a0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
76b0: 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c  qlite3_exec(D,S,
76c0: 43 2c 41 2c 45 29 5d 20 69 73 20 4e 55 4c 4c 20  C,A,E)] is NULL 
76d0: 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  then.**         
76e0: 20 74 68 65 20 61 63 74 69 6f 6e 73 20 6f 66 20   the actions of 
76f0: 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 73 68  the interface sh
7700: 61 6c 6c 20 62 65 20 74 68 65 20 73 61 6d 65 20  all be the same 
7710: 61 73 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 20  as if the.**    
7720: 20 20 20 20 20 20 53 20 70 61 72 61 6d 65 74 65        S paramete
7730: 72 20 77 65 72 65 20 61 6e 20 65 6d 70 74 79 20  r were an empty 
7740: 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48  string..**.** {H
7750: 31 32 31 30 34 7d 20 54 68 65 20 72 65 74 75 72  12104} The retur
7760: 6e 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69  n value of [sqli
7770: 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c  te3_exec()] shal
7780: 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  l be [SQLITE_OK]
7790: 20 69 66 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20   if all.**      
77a0: 20 20 20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e      SQL statemen
77b0: 74 73 20 72 75 6e 20 73 75 63 63 65 73 73 66 75  ts run successfu
77c0: 6c 6c 79 20 61 6e 64 20 74 6f 20 63 6f 6d 70 6c  lly and to compl
77d0: 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  etion..**.** {H1
77e0: 32 31 30 35 7d 20 54 68 65 20 72 65 74 75 72 6e  2105} The return
77f0: 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74   value of [sqlit
7800: 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c  e3_exec()] shall
7810: 20 62 65 20 61 6e 20 61 70 70 72 6f 70 72 69 61   be an appropria
7820: 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e  te.**          n
7830: 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63  on-zero [error c
7840: 6f 64 65 5d 20 69 66 20 61 6e 79 20 53 51 4c 20  ode] if any SQL 
7850: 73 74 61 74 65 6d 65 6e 74 20 66 61 69 6c 73 2e  statement fails.
7860: 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 30 37 7d 20  .**.** {H12107} 
7870: 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  If one or more o
7880: 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  f the SQL statem
7890: 65 6e 74 73 20 68 61 6e 64 65 64 20 74 6f 20 5b  ents handed to [
78a0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a  sqlite3_exec()].
78b0: 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  **          retu
78c0: 72 6e 20 72 65 73 75 6c 74 73 20 61 6e 64 20 74  rn results and t
78d0: 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72  he 3rd parameter
78e0: 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
78f0: 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  en.**          t
7900: 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
7910: 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 62  tion specified b
7920: 79 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65  y the 3rd parame
7930: 74 65 72 20 73 68 61 6c 6c 20 62 65 0a 2a 2a 20  ter shall be.** 
7940: 20 20 20 20 20 20 20 20 20 69 6e 76 6f 6b 65 64           invoked
7950: 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72   once for each r
7960: 6f 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a 2a 2a  ow of result..**
7970: 0a 2a 2a 20 7b 48 31 32 31 31 30 7d 20 49 66 20  .** {H12110} If 
7980: 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  the callback ret
7990: 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  urns a non-zero 
79a0: 76 61 6c 75 65 20 74 68 65 6e 20 5b 73 71 6c 69  value then [sqli
79b0: 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a 20 20  te3_exec()].**  
79c0: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 61 62          shall ab
79d0: 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 61 74  ort the SQL stat
79e0: 65 6d 65 6e 74 20 69 74 20 69 73 20 63 75 72 72  ement it is curr
79f0: 65 6e 74 6c 79 20 65 76 61 6c 75 61 74 69 6e 67  ently evaluating
7a00: 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 6b  ,.**          sk
7a10: 69 70 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  ip all subsequen
7a20: 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  t SQL statements
7a30: 2c 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51  , and return [SQ
7a40: 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a  LITE_ABORT]..**.
7a50: 2a 2a 20 7b 48 31 32 31 31 33 7d 20 54 68 65 20  ** {H12113} The 
7a60: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
7a70: 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 70   routine shall p
7a80: 61 73 73 20 69 74 73 20 34 74 68 20 70 61 72 61  ass its 4th para
7a90: 6d 65 74 65 72 20 74 68 72 6f 75 67 68 0a 2a 2a  meter through.**
7aa0: 20 20 20 20 20 20 20 20 20 20 61 73 20 74 68 65            as the
7ab0: 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 20 6f   1st parameter o
7ac0: 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a  f the callback..
7ad0: 2a 2a 0a 2a 2a 20 7b 48 31 32 31 31 36 7d 20 54  **.** {H12116} T
7ae0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  he [sqlite3_exec
7af0: 28 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61 6c  ()] routine shal
7b00: 6c 20 73 65 74 20 74 68 65 20 32 6e 64 20 70 61  l set the 2nd pa
7b10: 72 61 6d 65 74 65 72 20 6f 66 20 69 74 73 0a 2a  rameter of its.*
7b20: 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62  *          callb
7b30: 61 63 6b 20 74 6f 20 62 65 20 74 68 65 20 6e 75  ack to be the nu
7b40: 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
7b50: 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  in the current r
7b60: 6f 77 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ow of.**        
7b70: 20 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20    result..**.** 
7b80: 7b 48 31 32 31 31 39 7d 20 54 68 65 20 5b 73 71  {H12119} The [sq
7b90: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 72 6f  lite3_exec()] ro
7ba0: 75 74 69 6e 65 20 73 68 61 6c 6c 20 73 65 74 20  utine shall set 
7bb0: 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65  the 3rd paramete
7bc0: 72 20 6f 66 20 69 74 73 0a 2a 2a 20 20 20 20 20  r of its.**     
7bd0: 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f       callback to
7be0: 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20   be an array of 
7bf0: 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 69  pointers to stri
7c00: 6e 67 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a  ngs holding the.
7c10: 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75  **          valu
7c20: 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  es for each colu
7c30: 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e  mn in the curren
7c40: 74 20 72 65 73 75 6c 74 20 73 65 74 20 72 6f 77  t result set row
7c50: 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   as.**          
7c60: 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73  obtained from [s
7c70: 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
7c80: 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  xt()]..**.** {H1
7c90: 32 31 32 32 7d 20 54 68 65 20 5b 73 71 6c 69 74  2122} The [sqlit
7ca0: 65 33 5f 65 78 65 63 28 29 5d 20 72 6f 75 74 69  e3_exec()] routi
7cb0: 6e 65 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65  ne shall set the
7cc0: 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 6f   4th parameter o
7cd0: 66 20 69 74 73 0a 2a 2a 20 20 20 20 20 20 20 20  f its.**        
7ce0: 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65    callback to be
7cf0: 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69   an array of poi
7d00: 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73  nters to strings
7d10: 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20   holding the.** 
7d20: 20 20 20 20 20 20 20 20 20 6e 61 6d 65 73 20 6f           names o
7d30: 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
7d40: 20 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   as obtained fro
7d50: 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  m [sqlite3_colum
7d60: 6e 5f 6e 61 6d 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a  n_name()]..**.**
7d70: 20 7b 48 31 32 31 32 35 7d 20 49 66 20 74 68 65   {H12125} If the
7d80: 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74   3rd parameter t
7d90: 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  o [sqlite3_exec(
7da0: 29 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a  )] is NULL then.
7db0: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
7dc0: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61  ite3_exec()] sha
7dd0: 6c 6c 20 73 69 6c 65 6e 74 6c 79 20 64 69 73 63  ll silently disc
7de0: 61 72 64 20 71 75 65 72 79 20 72 65 73 75 6c 74  ard query result
7df0: 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 33 31  s..**.** {H12131
7e00: 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  } If an error oc
7e10: 63 75 72 73 20 77 68 69 6c 65 20 70 61 72 73 69  curs while parsi
7e20: 6e 67 20 6f 72 20 65 76 61 6c 75 61 74 69 6e 67  ng or evaluating
7e30: 20 61 6e 79 20 6f 66 20 74 68 65 20 53 51 4c 0a   any of the SQL.
7e40: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 61 74  **          stat
7e50: 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 53 20  ements in the S 
7e60: 70 61 72 61 6d 65 74 65 72 20 6f 66 20 5b 73 71  parameter of [sq
7e70: 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43  lite3_exec(D,S,C
7e80: 2c 41 2c 45 29 5d 20 61 6e 64 20 69 66 0a 2a 2a  ,A,E)] and if.**
7e90: 20 20 20 20 20 20 20 20 20 20 74 68 65 20 45 20            the E 
7ea0: 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74  parameter is not
7eb0: 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b 73 71 6c   NULL, then [sql
7ec0: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61  ite3_exec()] sha
7ed0: 6c 6c 20 73 74 6f 72 65 0a 2a 2a 20 20 20 20 20  ll store.**     
7ee0: 20 20 20 20 20 69 6e 20 2a 45 20 61 6e 20 61 70       in *E an ap
7ef0: 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20  propriate error 
7f00: 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20  message written 
7f10: 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
7f20: 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  ined.**         
7f30: 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
7f40: 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  alloc()]..**.** 
7f50: 7b 48 31 32 31 33 34 7d 20 54 68 65 20 5b 73 71  {H12134} The [sq
7f60: 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43  lite3_exec(D,S,C
7f70: 2c 41 2c 45 29 5d 20 72 6f 75 74 69 6e 65 20 73  ,A,E)] routine s
7f80: 68 61 6c 6c 20 73 65 74 20 74 68 65 20 76 61 6c  hall set the val
7f90: 75 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ue of.**        
7fa0: 20 20 2a 45 20 74 6f 20 4e 55 4c 4c 20 69 66 20    *E to NULL if 
7fb0: 45 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e  E is not NULL an
7fc0: 64 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 65  d there are no e
7fd0: 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  rrors..**.** {H1
7fe0: 32 31 33 37 7d 20 54 68 65 20 5b 73 71 6c 69 74  2137} The [sqlit
7ff0: 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c 41 2c  e3_exec(D,S,C,A,
8000: 45 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61  E)] function sha
8010: 6c 6c 20 73 65 74 20 74 68 65 20 5b 65 72 72 6f  ll set the [erro
8020: 72 20 63 6f 64 65 5d 0a 2a 2a 20 20 20 20 20 20  r code].**      
8030: 20 20 20 20 61 6e 64 20 6d 65 73 73 61 67 65 20      and message 
8040: 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b  accessible via [
8050: 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
8060: 29 5d 2c 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  )], .**         
8070: 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64   [sqlite3_extend
8080: 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a  ed_errcode()],.*
8090: 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
80a0: 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 61  te3_errmsg()], a
80b0: 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  nd [sqlite3_errm
80c0: 73 67 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  sg16()]..**.** {
80d0: 48 31 32 31 33 38 7d 20 49 66 20 74 68 65 20 53  H12138} If the S
80e0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
80f0: 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c  qlite3_exec(D,S,
8100: 43 2c 41 2c 45 29 5d 20 69 73 20 4e 55 4c 4c 20  C,A,E)] is NULL 
8110: 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20  or an.**        
8120: 20 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 6f    empty string o
8130: 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 68 69  r contains nothi
8140: 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 77 68  ng other than wh
8150: 69 74 65 73 70 61 63 65 2c 20 63 6f 6d 6d 65 6e  itespace, commen
8160: 74 73 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ts,.**          
8170: 61 6e 64 2f 6f 72 20 73 65 6d 69 63 6f 6c 6f 6e  and/or semicolon
8180: 73 2c 20 74 68 65 6e 20 72 65 73 75 6c 74 73 20  s, then results 
8190: 6f 66 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63  of [sqlite3_errc
81a0: 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  ode()],.**      
81b0: 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74      [sqlite3_ext
81c0: 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d  ended_errcode()]
81d0: 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  ,.**          [s
81e0: 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d  qlite3_errmsg()]
81f0: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65  , and [sqlite3_e
8200: 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a 20 20 20  rrmsg16()].**   
8210: 20 20 20 20 20 20 20 73 68 61 6c 6c 20 72 65 73         shall res
8220: 65 74 20 74 6f 20 69 6e 64 69 63 61 74 65 20 6e  et to indicate n
8230: 6f 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  o errors..**.** 
8240: 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a  ASSUMPTIONS:.**.
8250: 2a 2a 20 7b 41 31 32 31 34 31 7d 20 54 68 65 20  ** {A12141} The 
8260: 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
8270: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  to [sqlite3_exec
8280: 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e 20 76  ()] must be an v
8290: 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a  alid and open.**
82a0: 20 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62            [datab
82b0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
82c0: 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34 32 7d 20  .**.** {A12142} 
82d0: 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
82e0: 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74  nection must not
82f0: 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65   be closed while
8300: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
8310: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73  lite3_exec()] is
8320: 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   running..**.** 
8330: 7b 41 31 32 31 34 33 7d 20 54 68 65 20 63 61 6c  {A12143} The cal
8340: 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68  ling function sh
8350: 6f 75 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65  ould use [sqlite
8360: 33 5f 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65  3_free()] to fre
8370: 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  e.**          th
8380: 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 2a 65  e memory that *e
8390: 72 72 6d 73 67 20 69 73 20 6c 65 66 74 20 70 6f  rrmsg is left po
83a0: 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63 65 20 74  inting at once t
83b0: 68 65 20 65 72 72 6f 72 0a 2a 2a 20 20 20 20 20  he error.**     
83c0: 20 20 20 20 20 6d 65 73 73 61 67 65 20 69 73 20       message is 
83d0: 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65 64  no longer needed
83e0: 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34 35 7d  ..**.** {A12145}
83f0: 20 54 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   The SQL stateme
8400: 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65 20 32  nt text in the 2
8410: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
8420: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
8430: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 75 73  .**          mus
8440: 74 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67  t remain unchang
8450: 65 64 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65  ed while [sqlite
8460: 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72 75 6e  3_exec()] is run
8470: 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ning..*/.SQLITE_
8480: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
8490: 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65 33 2a  exec(.  sqlite3*
84a0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
84b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
84c0: 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61     /* An open da
84d0: 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73  tabase */.  cons
84e0: 74 20 63 68 61 72 20 2a 73 71 6c 2c 20 20 20 20  t char *sql,    
84f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8500: 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f         /* SQL to
8510: 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f   be evaluated */
8520: 0a 20 20 69 6e 74 20 28 2a 63 61 6c 6c 62 61 63  .  int (*callbac
8530: 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61  k)(void*,int,cha
8540: 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c 20 20 2f 2a  r**,char**),  /*
8550: 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   Callback functi
8560: 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2c 20  on */.  void *, 
8570: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8580: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8590: 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 6d 65     /* 1st argume
85a0: 6e 74 20 74 6f 20 63 61 6c 6c 62 61 63 6b 20 2a  nt to callback *
85b0: 2f 0a 20 20 63 68 61 72 20 2a 2a 65 72 72 6d 73  /.  char **errms
85c0: 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
85d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
85e0: 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74  * Error msg writ
85f0: 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 0a  ten here */.);..
8600: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
8610: 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31  Result Codes {H1
8620: 30 32 31 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a  0210} <S10700>.*
8630: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c 49  * KEYWORDS: SQLI
8640: 54 45 5f 4f 4b 20 7b 65 72 72 6f 72 20 63 6f 64  TE_OK {error cod
8650: 65 7d 20 7b 65 72 72 6f 72 20 63 6f 64 65 73 7d  e} {error codes}
8660: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 72  .** KEYWORDS: {r
8670: 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 72 65 73  esult code} {res
8680: 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a  ult codes}.**.**
8690: 20 4d 61 6e 79 20 53 51 4c 69 74 65 20 66 75 6e   Many SQLite fun
86a0: 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 61 6e  ctions return an
86b0: 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 20   integer result 
86c0: 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 73 65  code from the se
86d0: 74 20 73 68 6f 77 6e 0a 2a 2a 20 68 65 72 65 20  t shown.** here 
86e0: 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 64 69  in order to indi
86f0: 63 61 74 65 73 20 73 75 63 63 65 73 73 20 6f 72  cates success or
8700: 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20   failure..**.** 
8710: 4e 65 77 20 65 72 72 6f 72 20 63 6f 64 65 73 20  New error codes 
8720: 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20  may be added in 
8730: 66 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  future versions 
8740: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  of SQLite..**.**
8750: 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49   See also: [SQLI
8760: 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c 20  TE_IOERR_READ | 
8770: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
8780: 63 6f 64 65 73 5d 0a 2a 2f 0a 23 64 65 66 69 6e  codes].*/.#defin
8790: 65 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20  e SQLITE_OK     
87a0: 20 20 20 20 20 20 30 20 20 20 2f 2a 20 53 75 63        0   /* Suc
87b0: 63 65 73 73 66 75 6c 20 72 65 73 75 6c 74 20 2a  cessful result *
87c0: 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69 6e 67 2d 6f  /./* beginning-o
87d0: 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f  f-error-codes */
87e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
87f0: 45 52 52 4f 52 20 20 20 20 20 20 20 20 31 20 20  ERROR        1  
8800: 20 2f 2a 20 53 51 4c 20 65 72 72 6f 72 20 6f 72   /* SQL error or
8810: 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 61 73   missing databas
8820: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
8830: 49 54 45 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20  ITE_INTERNAL    
8840: 20 32 20 20 20 2f 2a 20 49 6e 74 65 72 6e 61 6c   2   /* Internal
8850: 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20   logic error in 
8860: 53 51 4c 69 74 65 20 2a 2f 0a 23 64 65 66 69 6e  SQLite */.#defin
8870: 65 20 53 51 4c 49 54 45 5f 50 45 52 4d 20 20 20  e SQLITE_PERM   
8880: 20 20 20 20 20 20 33 20 20 20 2f 2a 20 41 63 63        3   /* Acc
8890: 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 64  ess permission d
88a0: 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  enied */.#define
88b0: 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 20 20   SQLITE_ABORT   
88c0: 20 20 20 20 20 34 20 20 20 2f 2a 20 43 61 6c 6c       4   /* Call
88d0: 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 71  back routine req
88e0: 75 65 73 74 65 64 20 61 6e 20 61 62 6f 72 74 20  uested an abort 
88f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8900: 45 5f 42 55 53 59 20 20 20 20 20 20 20 20 20 35  E_BUSY         5
8910: 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
8920: 73 65 20 66 69 6c 65 20 69 73 20 6c 6f 63 6b 65  se file is locke
8930: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
8940: 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20  ITE_LOCKED      
8950: 20 36 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20   6   /* A table 
8960: 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
8970: 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65  is locked */.#de
8980: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  fine SQLITE_NOME
8990: 4d 20 20 20 20 20 20 20 20 37 20 20 20 2f 2a 20  M        7   /* 
89a0: 41 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65  A malloc() faile
89b0: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
89c0: 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20  ITE_READONLY    
89d0: 20 38 20 20 20 2f 2a 20 41 74 74 65 6d 70 74 20   8   /* Attempt 
89e0: 74 6f 20 77 72 69 74 65 20 61 20 72 65 61 64 6f  to write a reado
89f0: 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  nly database */.
8a00: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8a10: 4e 54 45 52 52 55 50 54 20 20 20 20 39 20 20 20  NTERRUPT    9   
8a20: 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 74 65 72  /* Operation ter
8a30: 6d 69 6e 61 74 65 64 20 62 79 20 73 71 6c 69 74  minated by sqlit
8a40: 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 2a 2f  e3_interrupt()*/
8a50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8a60: 49 4f 45 52 52 20 20 20 20 20 20 20 31 30 20 20  IOERR       10  
8a70: 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e 64 20 6f 66   /* Some kind of
8a80: 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20   disk I/O error 
8a90: 6f 63 63 75 72 72 65 64 20 2a 2f 0a 23 64 65 66  occurred */.#def
8aa0: 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  ine SQLITE_CORRU
8ab0: 50 54 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54  PT     11   /* T
8ac0: 68 65 20 64 61 74 61 62 61 73 65 20 64 69 73 6b  he database disk
8ad0: 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72   image is malfor
8ae0: 6d 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  med */.#define S
8af0: 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20  QLITE_NOTFOUND  
8b00: 20 20 31 32 20 20 20 2f 2a 20 4e 4f 54 20 55 53    12   /* NOT US
8b10: 45 44 2e 20 54 61 62 6c 65 20 6f 72 20 72 65 63  ED. Table or rec
8b20: 6f 72 64 20 6e 6f 74 20 66 6f 75 6e 64 20 2a 2f  ord not found */
8b30: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8b40: 46 55 4c 4c 20 20 20 20 20 20 20 20 31 33 20 20  FULL        13  
8b50: 20 2f 2a 20 49 6e 73 65 72 74 69 6f 6e 20 66 61   /* Insertion fa
8b60: 69 6c 65 64 20 62 65 63 61 75 73 65 20 64 61 74  iled because dat
8b70: 61 62 61 73 65 20 69 73 20 66 75 6c 6c 20 2a 2f  abase is full */
8b80: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8b90: 43 41 4e 54 4f 50 45 4e 20 20 20 20 31 34 20 20  CANTOPEN    14  
8ba0: 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 70   /* Unable to op
8bb0: 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
8bc0: 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  file */.#define 
8bd0: 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20  SQLITE_PROTOCOL 
8be0: 20 20 20 31 35 20 20 20 2f 2a 20 4e 4f 54 20 55     15   /* NOT U
8bf0: 53 45 44 2e 20 44 61 74 61 62 61 73 65 20 6c 6f  SED. Database lo
8c00: 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f  ck protocol erro
8c10: 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  r */.#define SQL
8c20: 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20 20 20  ITE_EMPTY       
8c30: 31 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  16   /* Database
8c40: 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65   is empty */.#de
8c50: 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 43 48 45  fine SQLITE_SCHE
8c60: 4d 41 20 20 20 20 20 20 31 37 20 20 20 2f 2a 20  MA      17   /* 
8c70: 54 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  The database sch
8c80: 65 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23  ema changed */.#
8c90: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 4f  define SQLITE_TO
8ca0: 4f 42 49 47 20 20 20 20 20 20 31 38 20 20 20 2f  OBIG      18   /
8cb0: 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42  * String or BLOB
8cc0: 20 65 78 63 65 65 64 73 20 73 69 7a 65 20 6c 69   exceeds size li
8cd0: 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  mit */.#define S
8ce0: 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
8cf0: 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72 74 20    19   /* Abort 
8d00: 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61 69 6e  due to constrain
8d10: 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f 0a 23  t violation */.#
8d20: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49  define SQLITE_MI
8d30: 53 4d 41 54 43 48 20 20 20 20 32 30 20 20 20 2f  SMATCH    20   /
8d40: 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69 73 6d  * Data type mism
8d50: 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20  atch */.#define 
8d60: 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 20 20  SQLITE_MISUSE   
8d70: 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62 72 61     21   /* Libra
8d80: 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72 65 63  ry used incorrec
8d90: 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  tly */.#define S
8da0: 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20 20 20  QLITE_NOLFS     
8db0: 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73 20 4f    22   /* Uses O
8dc0: 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74 20 73  S features not s
8dd0: 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f 73 74  upported on host
8de0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8df0: 54 45 5f 41 55 54 48 20 20 20 20 20 20 20 20 32  TE_AUTH        2
8e00: 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61  3   /* Authoriza
8e10: 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23  tion denied */.#
8e20: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 4f  define SQLITE_FO
8e30: 52 4d 41 54 20 20 20 20 20 20 32 34 20 20 20 2f  RMAT      24   /
8e40: 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61 74 61  * Auxiliary data
8e50: 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72 72 6f  base format erro
8e60: 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  r */.#define SQL
8e70: 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20 20 20  ITE_RANGE       
8e80: 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61 72 61  25   /* 2nd para
8e90: 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
8ea0: 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72 61 6e  _bind out of ran
8eb0: 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ge */.#define SQ
8ec0: 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20 20 20  LITE_NOTADB     
8ed0: 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20 6f 70   26   /* File op
8ee0: 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e 6f 74  ened that is not
8ef0: 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
8f00: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8f10: 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20 20 31  TE_ROW         1
8f20: 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73  00  /* sqlite3_s
8f30: 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74 68 65  tep() has anothe
8f40: 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f 0a 23  r row ready */.#
8f50: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 4f  define SQLITE_DO
8f60: 4e 45 20 20 20 20 20 20 20 20 31 30 31 20 20 2f  NE        101  /
8f70: 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
8f80: 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 65 78   has finished ex
8f90: 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20 65 6e  ecuting */./* en
8fa0: 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73  d-of-error-codes
8fb0: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33   */../*.** CAPI3
8fc0: 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20 52 65  REF: Extended Re
8fd0: 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 30 32  sult Codes {H102
8fe0: 32 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20  20} <S10700>.** 
8ff0: 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e  KEYWORDS: {exten
9000: 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 7d 20  ded error code} 
9010: 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20  {extended error 
9020: 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52  codes}.** KEYWOR
9030: 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 72 65  DS: {extended re
9040: 73 75 6c 74 20 63 6f 64 65 7d 20 7b 65 78 74 65  sult code} {exte
9050: 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65  nded result code
9060: 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74 73 20  s}.**.** In its 
9070: 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72  default configur
9080: 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 41 50  ation, SQLite AP
9090: 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  I routines retur
90a0: 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e 74 65  n one of 26 inte
90b0: 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f  ger.** [SQLITE_O
90c0: 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 73  K | result codes
90d0: 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65 78 70  ].  However, exp
90e0: 65 72 69 65 6e 63 65 20 68 61 73 20 73 68 6f 77  erience has show
90f0: 6e 20 74 68 61 74 20 6d 61 6e 79 20 6f 66 0a 2a  n that many of.*
9100: 2a 20 74 68 65 73 65 20 72 65 73 75 6c 74 20 63  * these result c
9110: 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63 6f 61  odes are too coa
9120: 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20 54 68  rse-grained.  Th
9130: 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64  ey do not provid
9140: 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69 6e 66  e as.** much inf
9150: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 70  ormation about p
9160: 72 6f 62 6c 65 6d 73 20 61 73 20 70 72 6f 67 72  roblems as progr
9170: 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 6c 69 6b  ammers might lik
9180: 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f 72 74  e.  In an effort
9190: 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73 20 74   to.** address t
91a0: 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72 73 69  his, newer versi
91b0: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 28 76  ons of SQLite (v
91c0: 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61 6e 64  ersion 3.3.8 and
91d0: 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64 65 0a   later) include.
91e0: 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72 20 61  ** support for a
91f0: 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75 6c 74  dditional result
9200: 20 63 6f 64 65 73 20 74 68 61 74 20 70 72 6f 76   codes that prov
9210: 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69 6c 65  ide more detaile
9220: 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  d information.**
9230: 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 20 54   about errors. T
9240: 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75  he extended resu
9250: 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65 6e 61  lt codes are ena
9260: 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64  bled or disabled
9270: 0a 2a 2a 20 6f 6e 20 61 20 70 65 72 20 64 61 74  .** on a per dat
9280: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
9290: 20 62 61 73 69 73 20 75 73 69 6e 67 20 74 68 65   basis using the
92a0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 74  .** [sqlite3_ext
92b0: 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64  ended_result_cod
92c0: 65 73 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a 2a 2a  es()] API..**.**
92d0: 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 76 61   Some of the ava
92e0: 69 6c 61 62 6c 65 20 65 78 74 65 6e 64 65 64 20  ilable extended 
92f0: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65  result codes are
9300: 20 6c 69 73 74 65 64 20 68 65 72 65 2e 0a 2a 2a   listed here..**
9310: 20 4f 6e 65 20 6d 61 79 20 65 78 70 65 63 74 20   One may expect 
9320: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 78  the number of ex
9330: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
9340: 64 65 73 20 77 69 6c 6c 20 62 65 20 65 78 70 61  des will be expa
9350: 6e 64 0a 2a 2a 20 6f 76 65 72 20 74 69 6d 65 2e  nd.** over time.
9360: 20 20 53 6f 66 74 77 61 72 65 20 74 68 61 74 20    Software that 
9370: 75 73 65 73 20 65 78 74 65 6e 64 65 64 20 72 65  uses extended re
9380: 73 75 6c 74 20 63 6f 64 65 73 20 73 68 6f 75 6c  sult codes shoul
9390: 64 20 65 78 70 65 63 74 0a 2a 2a 20 74 6f 20 73  d expect.** to s
93a0: 65 65 20 6e 65 77 20 72 65 73 75 6c 74 20 63 6f  ee new result co
93b0: 64 65 73 20 69 6e 20 66 75 74 75 72 65 20 72 65  des in future re
93c0: 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
93d0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
93e0: 54 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63 6f 64  TE_OK result cod
93f0: 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  e will never be 
9400: 65 78 74 65 6e 64 65 64 2e 20 20 49 74 20 77 69  extended.  It wi
9410: 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20  ll always.** be 
9420: 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a  exactly zero..**
9430: 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
9440: 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 33 7d 20 54  **.** {H10223} T
9450: 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65  he symbolic name
9460: 20 66 6f 72 20 61 6e 20 65 78 74 65 6e 64 65 64   for an extended
9470: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 73 68 61   result code sha
9480: 6c 6c 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20  ll contains.**  
9490: 20 20 20 20 20 20 20 20 61 20 72 65 6c 61 74 65          a relate
94a0: 64 20 70 72 69 6d 61 72 79 20 72 65 73 75 6c 74  d primary result
94b0: 20 63 6f 64 65 20 61 73 20 61 20 70 72 65 66 69   code as a prefi
94c0: 78 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 34  x..**.** {H10224
94d0: 7d 20 50 72 69 6d 61 72 79 20 72 65 73 75 6c 74  } Primary result
94e0: 20 63 6f 64 65 20 6e 61 6d 65 73 20 73 68 61 6c   code names shal
94f0: 6c 20 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67  l contain a sing
9500: 6c 65 20 22 5f 22 20 63 68 61 72 61 63 74 65 72  le "_" character
9510: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 35 7d  ..**.** {H10225}
9520: 20 45 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   Extended result
9530: 20 63 6f 64 65 20 6e 61 6d 65 73 20 73 68 61 6c   code names shal
9540: 6c 20 63 6f 6e 74 61 69 6e 20 74 77 6f 20 6f 72  l contain two or
9550: 20 6d 6f 72 65 20 22 5f 22 20 63 68 61 72 61 63   more "_" charac
9560: 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30  ters..**.** {H10
9570: 32 32 36 7d 20 54 68 65 20 6e 75 6d 65 72 69 63  226} The numeric
9580: 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 65 78 74   value of an ext
9590: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
95a0: 65 20 73 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 20  e shall contain 
95b0: 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
95c0: 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 6f 66  numeric value of
95d0: 20 69 74 73 20 63 6f 72 72 65 73 70 6f 6e 64 69   its correspondi
95e0: 6e 67 20 70 72 69 6d 61 72 79 20 72 65 73 75 6c  ng primary resul
95f0: 74 20 63 6f 64 65 20 69 6e 0a 2a 2a 20 20 20 20  t code in.**    
9600: 20 20 20 20 20 20 69 74 73 20 6c 65 61 73 74 20        its least 
9610: 73 69 67 6e 69 66 69 63 61 6e 74 20 38 20 62 69  significant 8 bi
9620: 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ts..*/.#define S
9630: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
9640: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53                (S
9650: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31  QLITE_IOERR | (1
9660: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
9670: 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
9680: 5f 52 45 41 44 20 20 20 20 20 20 20 20 28 53 51  _READ        (SQ
9690: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c  LITE_IOERR | (2<
96a0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
96b0: 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20  ITE_IOERR_WRITE 
96c0: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
96d0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c  ITE_IOERR | (3<<
96e0: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
96f0: 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20  TE_IOERR_FSYNC  
9700: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49             (SQLI
9710: 54 45 5f 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38  TE_IOERR | (4<<8
9720: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
9730: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e  E_IOERR_DIR_FSYN
9740: 43 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54  C         (SQLIT
9750: 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38 29  E_IOERR | (5<<8)
9760: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
9770: 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20  _IOERR_TRUNCATE 
9780: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
9790: 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c 38 29 29  _IOERR | (6<<8))
97a0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
97b0: 49 4f 45 52 52 5f 46 53 54 41 54 20 20 20 20 20  IOERR_FSTAT     
97c0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
97d0: 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29 29 0a  IOERR | (7<<8)).
97e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
97f0: 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20  OERR_UNLOCK     
9800: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
9810: 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29 29 0a 23  OERR | (8<<8)).#
9820: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
9830: 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20 20 20 20  ERR_RDLOCK      
9840: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
9850: 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 0a 23 64  ERR | (9<<8)).#d
9860: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
9870: 52 52 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20  RR_DELETE       
9880: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
9890: 52 52 20 7c 20 28 31 30 3c 3c 38 29 29 0a 23 64  RR | (10<<8)).#d
98a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
98b0: 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20 20 20 20  RR_BLOCKED      
98c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
98d0: 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a 23 64  RR | (11<<8)).#d
98e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
98f0: 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20  RR_NOMEM        
9900: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
9910: 52 52 20 7c 20 28 31 32 3c 3c 38 29 29 0a 23 64  RR | (12<<8)).#d
9920: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
9930: 52 52 5f 41 43 43 45 53 53 20 20 20 20 20 20 20  RR_ACCESS       
9940: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
9950: 52 52 20 7c 20 28 31 33 3c 3c 38 29 29 0a 23 64  RR | (13<<8)).#d
9960: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
9970: 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44  RR_CHECKRESERVED
9980: 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f 49 4f 45  LOCK (SQLITE_IOE
9990: 52 52 20 7c 20 28 31 34 3c 3c 38 29 29 0a 23 64  RR | (14<<8)).#d
99a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
99b0: 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20  RR_LOCK         
99c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
99d0: 52 52 20 7c 20 28 31 35 3c 3c 38 29 29 0a 23 64  RR | (15<<8)).#d
99e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
99f0: 52 52 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20  RR_CLOSE        
9a00: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
9a10: 52 52 20 7c 20 28 31 36 3c 3c 38 29 29 0a 23 64  RR | (16<<8)).#d
9a20: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
9a30: 52 52 5f 44 49 52 5f 43 4c 4f 53 45 20 20 20 20  RR_DIR_CLOSE    
9a40: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
9a50: 52 52 20 7c 20 28 31 37 3c 3c 38 29 29 0a 0a 2f  RR | (17<<8))../
9a60: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46  *.** CAPI3REF: F
9a70: 6c 61 67 73 20 46 6f 72 20 46 69 6c 65 20 4f 70  lags For File Op
9a80: 65 6e 20 4f 70 65 72 61 74 69 6f 6e 73 20 7b 48  en Operations {H
9a90: 31 30 32 33 30 7d 20 3c 48 31 31 31 32 30 3e 20  10230} <H11120> 
9aa0: 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <H12700>.**.** T
9ab0: 68 65 73 65 20 62 69 74 20 76 61 6c 75 65 73 20  hese bit values 
9ac0: 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72  are intended for
9ad0: 20 75 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20 33   use in the.** 3
9ae0: 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
9af0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  the [sqlite3_ope
9b00: 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63  n_v2()] interfac
9b10: 65 20 61 6e 64 0a 2a 2a 20 69 6e 20 74 68 65 20  e and.** in the 
9b20: 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  4th parameter to
9b30: 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f   the xOpen metho
9b40: 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  d of the.** [sql
9b50: 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
9b60: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
9b70: 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
9b80: 59 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30  Y         0x0000
9b90: 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51 4c  0001.#define SQL
9ba0: 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
9bb0: 54 45 20 20 20 20 20 20 20 20 30 78 30 30 30 30  TE        0x0000
9bc0: 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c  0002.#define SQL
9bd0: 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20  ITE_OPEN_CREATE 
9be0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30            0x0000
9bf0: 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51 4c  0004.#define SQL
9c00: 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
9c10: 4e 43 4c 4f 53 45 20 20 20 20 30 78 30 30 30 30  NCLOSE    0x0000
9c20: 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c  0008.#define SQL
9c30: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
9c40: 56 45 20 20 20 20 20 20 20 20 30 78 30 30 30 30  VE        0x0000
9c50: 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0010.#define SQL
9c60: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
9c70: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30            0x0000
9c80: 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0100.#define SQL
9c90: 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42  ITE_OPEN_TEMP_DB
9ca0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30            0x0000
9cb0: 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0200.#define SQL
9cc0: 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45  ITE_OPEN_TRANSIE
9cd0: 4e 54 5f 44 42 20 20 20 20 20 30 78 30 30 30 30  NT_DB     0x0000
9ce0: 30 34 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0400.#define SQL
9cf0: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
9d00: 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30  URNAL     0x0000
9d10: 30 38 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0800.#define SQL
9d20: 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f  ITE_OPEN_TEMP_JO
9d30: 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30  URNAL     0x0000
9d40: 31 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  1000.#define SQL
9d50: 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52  ITE_OPEN_SUBJOUR
9d60: 4e 41 4c 20 20 20 20 20 20 20 30 78 30 30 30 30  NAL       0x0000
9d70: 32 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  2000.#define SQL
9d80: 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f  ITE_OPEN_MASTER_
9d90: 4a 4f 55 52 4e 41 4c 20 20 20 30 78 30 30 30 30  JOURNAL   0x0000
9da0: 34 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  4000.#define SQL
9db0: 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58  ITE_OPEN_NOMUTEX
9dc0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30            0x0000
9dd0: 38 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  8000.#define SQL
9de0: 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54  ITE_OPEN_FULLMUT
9df0: 45 58 20 20 20 20 20 20 20 20 30 78 30 30 30 31  EX        0x0001
9e00: 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  0000../*.** CAPI
9e10: 33 52 45 46 3a 20 44 65 76 69 63 65 20 43 68 61  3REF: Device Cha
9e20: 72 61 63 74 65 72 69 73 74 69 63 73 20 7b 48 31  racteristics {H1
9e30: 30 32 34 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a  0240} <H11120>.*
9e40: 2a 0a 2a 2a 20 54 68 65 20 78 44 65 76 69 63 65  *.** The xDevice
9e50: 43 61 70 61 62 69 6c 69 74 69 65 73 20 6d 65 74  Capabilities met
9e60: 68 6f 64 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  hod of the [sqli
9e70: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a  te3_io_methods].
9e80: 2a 2a 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e  ** object return
9e90: 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  s an integer whi
9ea0: 63 68 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f  ch is a vector o
9eb0: 66 20 74 68 65 20 74 68 65 73 65 0a 2a 2a 20 62  f the these.** b
9ec0: 69 74 20 76 61 6c 75 65 73 20 65 78 70 72 65 73  it values expres
9ed0: 73 69 6e 67 20 49 2f 4f 20 63 68 61 72 61 63 74  sing I/O charact
9ee0: 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20  eristics of the 
9ef0: 6d 61 73 73 20 73 74 6f 72 61 67 65 0a 2a 2a 20  mass storage.** 
9f00: 64 65 76 69 63 65 20 74 68 61 74 20 68 6f 6c 64  device that hold
9f10: 73 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20  s the file that 
9f20: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  the [sqlite3_io_
9f30: 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 65  methods].** refe
9f40: 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rs to..**.** The
9f50: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
9f60: 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65  OMIC property me
9f70: 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69  ans that all wri
9f80: 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69  tes of.** any si
9f90: 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20  ze are atomic.  
9fa0: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  The SQLITE_IOCAP
9fb0: 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65  _ATOMICnnn value
9fc0: 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77  s.** mean that w
9fd0: 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20  rites of blocks 
9fe0: 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74  that are nnn byt
9ff0: 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a  es in size and.*
a000: 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f  * are aligned to
a010: 20 61 6e 20 61 64 64 72 65 73 73 20 77 68 69 63   an address whic
a020: 68 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  h is an integer 
a030: 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e  multiple of.** n
a040: 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20  nn are atomic.  
a050: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  The SQLITE_IOCAP
a060: 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c  _SAFE_APPEND val
a070: 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74  ue means.** that
a080: 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 61 70   when data is ap
a090: 70 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65  pended to a file
a0a0: 2c 20 74 68 65 20 64 61 74 61 20 69 73 20 61 70  , the data is ap
a0b0: 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20  pended.** first 
a0c0: 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  then the size of
a0d0: 20 74 68 65 20 66 69 6c 65 20 69 73 20 65 78 74   the file is ext
a0e0: 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65  ended, never the
a0f0: 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72   other.** way ar
a100: 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54  ound.  The SQLIT
a110: 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  E_IOCAP_SEQUENTI
a120: 41 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e  AL property mean
a130: 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d  s that.** inform
a140: 61 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e  ation is written
a150: 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20   to disk in the 
a160: 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61  same order as ca
a170: 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65  lls.** to xWrite
a180: 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ()..*/.#define S
a190: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a1a0: 49 43 20 20 20 20 20 20 20 20 20 20 30 78 30 30  IC          0x00
a1b0: 30 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53  000001.#define S
a1c0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a1d0: 49 43 35 31 32 20 20 20 20 20 20 20 30 78 30 30  IC512       0x00
a1e0: 30 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53  000002.#define S
a1f0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a200: 49 43 31 4b 20 20 20 20 20 20 20 20 30 78 30 30  IC1K        0x00
a210: 30 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53  000004.#define S
a220: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a230: 49 43 32 4b 20 20 20 20 20 20 20 20 30 78 30 30  IC2K        0x00
a240: 30 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53  000008.#define S
a250: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a260: 49 43 34 4b 20 20 20 20 20 20 20 20 30 78 30 30  IC4K        0x00
a270: 30 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53  000010.#define S
a280: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a290: 49 43 38 4b 20 20 20 20 20 20 20 20 30 78 30 30  IC8K        0x00
a2a0: 30 30 30 30 32 30 0a 23 64 65 66 69 6e 65 20 53  000020.#define S
a2b0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a2c0: 49 43 31 36 4b 20 20 20 20 20 20 20 30 78 30 30  IC16K       0x00
a2d0: 30 30 30 30 34 30 0a 23 64 65 66 69 6e 65 20 53  000040.#define S
a2e0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a2f0: 49 43 33 32 4b 20 20 20 20 20 20 20 30 78 30 30  IC32K       0x00
a300: 30 30 30 30 38 30 0a 23 64 65 66 69 6e 65 20 53  000080.#define S
a310: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a320: 49 43 36 34 4b 20 20 20 20 20 20 20 30 78 30 30  IC64K       0x00
a330: 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53  000100.#define S
a340: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45  QLITE_IOCAP_SAFE
a350: 5f 41 50 50 45 4e 44 20 20 20 20 20 30 78 30 30  _APPEND     0x00
a360: 30 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53  000200.#define S
a370: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55  QLITE_IOCAP_SEQU
a380: 45 4e 54 49 41 4c 20 20 20 20 20 20 30 78 30 30  ENTIAL      0x00
a390: 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41  000400../*.** CA
a3a0: 50 49 33 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63  PI3REF: File Loc
a3b0: 6b 69 6e 67 20 4c 65 76 65 6c 73 20 7b 48 31 30  king Levels {H10
a3c0: 32 35 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48  250} <H11120> <H
a3d0: 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c  11310>.**.** SQL
a3e0: 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f 66 20  ite uses one of 
a3f0: 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 61  these integer va
a400: 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63 6f  lues as the seco
a410: 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74  nd.** argument t
a420: 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65 73  o calls it makes
a430: 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29 20   to the xLock() 
a440: 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d 65  and xUnlock() me
a450: 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 5b  thods.** of an [
a460: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
a470: 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23  ds] object..*/.#
a480: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f  define SQLITE_LO
a490: 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20 20  CK_NONE         
a4a0: 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   0.#define SQLIT
a4b0: 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20 20  E_LOCK_SHARED   
a4c0: 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53       1.#define S
a4d0: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52  QLITE_LOCK_RESER
a4e0: 56 45 44 20 20 20 20 20 20 32 0a 23 64 65 66 69  VED      2.#defi
a4f0: 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50  ne SQLITE_LOCK_P
a500: 45 4e 44 49 4e 47 20 20 20 20 20 20 20 33 0a 23  ENDING       3.#
a510: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f  define SQLITE_LO
a520: 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20  CK_EXCLUSIVE    
a530: 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52   4../*.** CAPI3R
a540: 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61 74  EF: Synchronizat
a550: 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 20 7b  ion Type Flags {
a560: 48 31 30 32 36 30 7d 20 3c 48 31 31 31 32 30 3e  H10260} <H11120>
a570: 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69  .**.** When SQLi
a580: 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78  te invokes the x
a590: 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f 66  Sync() method of
a5a0: 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   an.** [sqlite3_
a5b0: 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65  io_methods] obje
a5c0: 63 74 20 69 74 20 75 73 65 73 20 61 20 63 6f 6d  ct it uses a com
a5d0: 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74  bination of.** t
a5e0: 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c  hese integer val
a5f0: 75 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e  ues as the secon
a600: 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  d argument..**.*
a610: 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54  * When the SQLIT
a620: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20  E_SYNC_DATAONLY 
a630: 66 6c 61 67 20 69 73 20 75 73 65 64 2c 20 69 74  flag is used, it
a640: 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a   means that the.
a650: 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f  ** sync operatio
a660: 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20  n only needs to 
a670: 66 6c 75 73 68 20 64 61 74 61 20 74 6f 20 6d 61  flush data to ma
a680: 73 73 20 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f  ss storage.  Ino
a690: 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  de.** informatio
a6a0: 6e 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 6c  n need not be fl
a6b0: 75 73 68 65 64 2e 20 54 68 65 20 53 51 4c 49 54  ushed. The SQLIT
a6c0: 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 66 6c  E_SYNC_NORMAL fl
a6d0: 61 67 20 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75  ag means.** to u
a6e0: 73 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28  se normal fsync(
a6f0: 29 20 73 65 6d 61 6e 74 69 63 73 2e 20 54 68 65  ) semantics. The
a700: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c   SQLITE_SYNC_FUL
a710: 4c 20 66 6c 61 67 20 6d 65 61 6e 73 0a 2a 2a 20  L flag means.** 
a720: 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 20 58 20  to use Mac OS X 
a730: 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 20 69  style fullsync i
a740: 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e 63 28  nstead of fsync(
a750: 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  )..*/.#define SQ
a760: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
a770: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 32 0a          0x00002.
a780: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
a790: 59 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20 20 20  YNC_FULL        
a7a0: 20 20 30 78 30 30 30 30 33 0a 23 64 65 66 69 6e    0x00003.#defin
a7b0: 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41  e SQLITE_SYNC_DA
a7c0: 54 41 4f 4e 4c 59 20 20 20 20 20 20 30 78 30 30  TAONLY      0x00
a7d0: 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  010../*.** CAPI3
a7e0: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63  REF: OS Interfac
a7f0: 65 20 4f 70 65 6e 20 46 69 6c 65 20 48 61 6e 64  e Open File Hand
a800: 6c 65 20 7b 48 31 31 31 31 30 7d 20 3c 53 32 30  le {H11110} <S20
a810: 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b 73  110>.**.** An [s
a820: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a  qlite3_file] obj
a830: 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 61  ect represents a
a840: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 6e 20 74  n open file in t
a850: 68 65 20 4f 53 0a 2a 2a 20 69 6e 74 65 72 66 61  he OS.** interfa
a860: 63 65 20 6c 61 79 65 72 2e 20 20 49 6e 64 69 76  ce layer.  Indiv
a870: 69 64 75 61 6c 20 4f 53 20 69 6e 74 65 72 66 61  idual OS interfa
a880: 63 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ce implementatio
a890: 6e 73 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20  ns will.** want 
a8a0: 74 6f 20 73 75 62 63 6c 61 73 73 20 74 68 69 73  to subclass this
a8b0: 20 6f 62 6a 65 63 74 20 62 79 20 61 70 70 65 6e   object by appen
a8c0: 64 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c 20  ding additional 
a8d0: 66 69 65 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68  fields.** for th
a8e0: 65 69 72 20 6f 77 6e 20 75 73 65 2e 20 20 54 68  eir own use.  Th
a8f0: 65 20 70 4d 65 74 68 6f 64 73 20 65 6e 74 72 79  e pMethods entry
a900: 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
a910: 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   an.** [sqlite3_
a920: 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65  io_methods] obje
a930: 63 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  ct that defines 
a940: 6d 65 74 68 6f 64 73 20 66 6f 72 20 70 65 72 66  methods for perf
a950: 6f 72 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70  orming.** I/O op
a960: 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20  erations on the 
a970: 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79  open file..*/.ty
a980: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
a990: 69 74 65 33 5f 66 69 6c 65 20 73 71 6c 69 74 65  ite3_file sqlite
a9a0: 33 5f 66 69 6c 65 3b 0a 73 74 72 75 63 74 20 73  3_file;.struct s
a9b0: 71 6c 69 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20  qlite3_file {.  
a9c0: 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c  const struct sql
a9d0: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
a9e0: 2a 70 4d 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d  *pMethods;  /* M
a9f0: 65 74 68 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70  ethods for an op
aa00: 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f  en file */.};../
aa10: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
aa20: 53 20 49 6e 74 65 72 66 61 63 65 20 46 69 6c 65  S Interface File
aa30: 20 56 69 72 74 75 61 6c 20 4d 65 74 68 6f 64 73   Virtual Methods
aa40: 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 32 30 7d   Object {H11120}
aa50: 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20   <S20110>.**.** 
aa60: 45 76 65 72 79 20 66 69 6c 65 20 6f 70 65 6e 65  Every file opene
aa70: 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65  d by the [sqlite
aa80: 33 5f 76 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74  3_vfs] xOpen met
aa90: 68 6f 64 20 70 6f 70 75 6c 61 74 65 73 20 61 6e  hod populates an
aaa0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  .** [sqlite3_fil
aab0: 65 5d 20 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d  e] object (or, m
aac0: 6f 72 65 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20  ore commonly, a 
aad0: 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a  subclass of the.
aae0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65  ** [sqlite3_file
aaf0: 5d 20 6f 62 6a 65 63 74 29 20 77 69 74 68 20 61  ] object) with a
ab00: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
ab10: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
ab20: 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20  object..** This 
ab30: 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74  object defines t
ab40: 68 65 20 6d 65 74 68 6f 64 73 20 75 73 65 64 20  he methods used 
ab50: 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f  to perform vario
ab60: 75 73 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a  us operations.**
ab70: 20 61 67 61 69 6e 73 74 20 74 68 65 20 6f 70 65   against the ope
ab80: 6e 20 66 69 6c 65 20 72 65 70 72 65 73 65 6e 74  n file represent
ab90: 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74  ed by the [sqlit
aba0: 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e  e3_file] object.
abb0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73  .**.** The flags
abc0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 79   argument to xSy
abd0: 6e 63 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66  nc may be one of
abe0: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f   [SQLITE_SYNC_NO
abf0: 52 4d 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c  RMAL] or.** [SQL
ac00: 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20  ITE_SYNC_FULL]. 
ac10: 20 54 68 65 20 66 69 72 73 74 20 63 68 6f 69 63   The first choic
ac20: 65 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20  e is the normal 
ac30: 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 54 68 65 20  fsync()..** The 
ac40: 73 65 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73  second choice is
ac50: 20 61 20 4d 61 63 20 4f 53 20 58 20 73 74 79 6c   a Mac OS X styl
ac60: 65 20 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65  e fullsync.  The
ac70: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41   [SQLITE_SYNC_DA
ac80: 54 41 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20  TAONLY].** flag 
ac90: 6d 61 79 20 62 65 20 4f 52 65 64 20 69 6e 20 74  may be ORed in t
aca0: 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  o indicate that 
acb0: 6f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66  only the data of
acc0: 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64   the file.** and
acd0: 20 6e 6f 74 20 69 74 73 20 69 6e 6f 64 65 20 6e   not its inode n
ace0: 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 65  eeds to be synce
acf0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74  d..**.** The int
ad00: 65 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20 78  eger values to x
ad10: 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f  Lock() and xUnlo
ad20: 63 6b 28 29 20 61 72 65 20 6f 6e 65 20 6f 66 0a  ck() are one of.
ad30: 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
ad40: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e  [SQLITE_LOCK_NON
ad50: 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  E],.** <li> [SQL
ad60: 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d  ITE_LOCK_SHARED]
ad70: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ,.** <li> [SQLIT
ad80: 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d  E_LOCK_RESERVED]
ad90: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ,.** <li> [SQLIT
ada0: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c  E_LOCK_PENDING],
adb0: 20 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c   or.** <li> [SQL
adc0: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49  ITE_LOCK_EXCLUSI
add0: 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  VE]..** </ul>.**
ade0: 20 78 4c 6f 63 6b 28 29 20 69 6e 63 72 65 61 73   xLock() increas
adf0: 65 73 20 74 68 65 20 6c 6f 63 6b 2e 20 78 55 6e  es the lock. xUn
ae00: 6c 6f 63 6b 28 29 20 64 65 63 72 65 61 73 65 73  lock() decreases
ae10: 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68   the lock..** Th
ae20: 65 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  e xCheckReserved
ae30: 4c 6f 63 6b 28 29 20 6d 65 74 68 6f 64 20 63 68  Lock() method ch
ae40: 65 63 6b 73 20 77 68 65 74 68 65 72 20 61 6e 79  ecks whether any
ae50: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
ae60: 74 69 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65 72 20  tion,.** either 
ae70: 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
ae80: 6f 72 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72  or in some other
ae90: 20 70 72 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c   process, is hol
aea0: 64 69 6e 67 20 61 20 52 45 53 45 52 56 45 44 2c  ding a RESERVED,
aeb0: 0a 2a 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20  .** PENDING, or 
aec0: 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
aed0: 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20  n the file.  It 
aee0: 72 65 74 75 72 6e 73 20 74 72 75 65 0a 2a 2a 20  returns true.** 
aef0: 69 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65  if such a lock e
af00: 78 69 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20  xists and false 
af10: 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a  otherwise..**.**
af20: 20 54 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   The xFileContro
af30: 6c 28 29 20 6d 65 74 68 6f 64 20 69 73 20 61 20  l() method is a 
af40: 67 65 6e 65 72 69 63 20 69 6e 74 65 72 66 61 63  generic interfac
af50: 65 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 63 75  e that allows cu
af60: 73 74 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d 70 6c  stom.** VFS impl
af70: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f 20 64  ementations to d
af80: 69 72 65 63 74 6c 79 20 63 6f 6e 74 72 6f 6c 20  irectly control 
af90: 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 75 73 69  an open file usi
afa0: 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  ng the.** [sqlit
afb0: 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
afc0: 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54  )] interface.  T
afd0: 68 65 20 73 65 63 6f 6e 64 20 22 6f 70 22 20 61  he second "op" a
afe0: 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 0a 2a 2a  rgument is an.**
aff0: 20 69 6e 74 65 67 65 72 20 6f 70 63 6f 64 65 2e   integer opcode.
b000: 20 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75    The third argu
b010: 6d 65 6e 74 20 69 73 20 61 20 67 65 6e 65 72 69  ment is a generi
b020: 63 20 70 6f 69 6e 74 65 72 20 69 6e 74 65 6e 64  c pointer intend
b030: 65 64 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74  ed to.** point t
b040: 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68  o a structure th
b050: 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61  at may contain a
b060: 72 67 75 6d 65 6e 74 73 20 6f 72 20 73 70 61 63  rguments or spac
b070: 65 20 69 6e 20 77 68 69 63 68 20 74 6f 0a 2a 2a  e in which to.**
b080: 20 77 72 69 74 65 20 72 65 74 75 72 6e 20 76 61   write return va
b090: 6c 75 65 73 2e 20 20 50 6f 74 65 6e 74 69 61 6c  lues.  Potential
b0a0: 20 75 73 65 73 20 66 6f 72 20 78 46 69 6c 65 43   uses for xFileC
b0b0: 6f 6e 74 72 6f 6c 28 29 20 6d 69 67 68 74 20 62  ontrol() might b
b0c0: 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74  e.** functions t
b0d0: 6f 20 65 6e 61 62 6c 65 20 62 6c 6f 63 6b 69 6e  o enable blockin
b0e0: 67 20 6c 6f 63 6b 73 20 77 69 74 68 20 74 69 6d  g locks with tim
b0f0: 65 6f 75 74 73 2c 20 74 6f 20 63 68 61 6e 67 65  eouts, to change
b100: 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20   the.** locking 
b110: 73 74 72 61 74 65 67 79 20 28 66 6f 72 20 65 78  strategy (for ex
b120: 61 6d 70 6c 65 20 74 6f 20 75 73 65 20 64 6f 74  ample to use dot
b130: 2d 66 69 6c 65 20 6c 6f 63 6b 73 29 2c 20 74 6f  -file locks), to
b140: 20 69 6e 71 75 69 72 65 0a 2a 2a 20 61 62 6f 75   inquire.** abou
b150: 74 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20  t the status of 
b160: 61 20 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20 62 72  a lock, or to br
b170: 65 61 6b 20 73 74 61 6c 65 20 6c 6f 63 6b 73 2e  eak stale locks.
b180: 20 20 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20    The SQLite.** 
b190: 63 6f 72 65 20 72 65 73 65 72 76 65 73 20 61 6c  core reserves al
b1a0: 6c 20 6f 70 63 6f 64 65 73 20 6c 65 73 73 20 74  l opcodes less t
b1b0: 68 61 6e 20 31 30 30 20 66 6f 72 20 69 74 73 20  han 100 for its 
b1c0: 6f 77 6e 20 75 73 65 2e 0a 2a 2a 20 41 20 5b 53  own use..** A [S
b1d0: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b  QLITE_FCNTL_LOCK
b1e0: 53 54 41 54 45 20 7c 20 6c 69 73 74 20 6f 66 20  STATE | list of 
b1f0: 6f 70 63 6f 64 65 73 5d 20 6c 65 73 73 20 74 68  opcodes] less th
b200: 61 6e 20 31 30 30 20 69 73 20 61 76 61 69 6c 61  an 100 is availa
b210: 62 6c 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74  ble..** Applicat
b220: 69 6f 6e 73 20 74 68 61 74 20 64 65 66 69 6e 65  ions that define
b230: 20 61 20 63 75 73 74 6f 6d 20 78 46 69 6c 65 43   a custom xFileC
b240: 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 73 68  ontrol method sh
b250: 6f 75 6c 64 20 75 73 65 20 6f 70 63 6f 64 65 73  ould use opcodes
b260: 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
b270: 20 31 30 30 20 74 6f 20 61 76 6f 69 64 20 63 6f   100 to avoid co
b280: 6e 66 6c 69 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  nflicts..**.** T
b290: 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29  he xSectorSize()
b2a0: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
b2b0: 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
b2c0: 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 69 63 65  of the.** device
b2d0: 20 74 68 61 74 20 75 6e 64 65 72 6c 69 65 73 20   that underlies 
b2e0: 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 73  the file.  The s
b2f0: 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 74 68  ector size is th
b300: 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 72 69  e.** minimum wri
b310: 74 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 70  te that can be p
b320: 65 72 66 6f 72 6d 65 64 20 77 69 74 68 6f 75 74  erformed without
b330: 20 64 69 73 74 75 72 62 69 6e 67 0a 2a 2a 20 6f   disturbing.** o
b340: 74 68 65 72 20 62 79 74 65 73 20 69 6e 20 74 68  ther bytes in th
b350: 65 20 66 69 6c 65 2e 20 20 54 68 65 20 78 44 65  e file.  The xDe
b360: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
b370: 69 63 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  ics().** method 
b380: 72 65 74 75 72 6e 73 20 61 20 62 69 74 20 76 65  returns a bit ve
b390: 63 74 6f 72 20 64 65 73 63 72 69 62 69 6e 67 20  ctor describing 
b3a0: 62 65 68 61 76 69 6f 72 73 20 6f 66 20 74 68 65  behaviors of the
b3b0: 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 64  .** underlying d
b3c0: 65 76 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  evice:.**.** <ul
b3d0: 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  >.** <li> [SQLIT
b3e0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 5d 0a  E_IOCAP_ATOMIC].
b3f0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
b400: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 5d  IOCAP_ATOMIC512]
b410: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
b420: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 5d  _IOCAP_ATOMIC1K]
b430: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
b440: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 5d  _IOCAP_ATOMIC2K]
b450: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
b460: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d  _IOCAP_ATOMIC4K]
b470: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
b480: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d  _IOCAP_ATOMIC8K]
b490: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
b4a0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b  _IOCAP_ATOMIC16K
b4b0: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ].** <li> [SQLIT
b4c0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32  E_IOCAP_ATOMIC32
b4d0: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  K].** <li> [SQLI
b4e0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36  TE_IOCAP_ATOMIC6
b4f0: 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  4K].** <li> [SQL
b500: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
b510: 50 50 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b  PPEND].** <li> [
b520: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
b530: 55 45 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c  UENTIAL].** </ul
b540: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  >.**.** The SQLI
b550: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20  TE_IOCAP_ATOMIC 
b560: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74  property means t
b570: 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f  hat all writes o
b580: 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72  f.** any size ar
b590: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53  e atomic.  The S
b5a0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
b5b0: 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20  ICnnn values.** 
b5c0: 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74 65 73  mean that writes
b5d0: 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20   of blocks that 
b5e0: 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e  are nnn bytes in
b5f0: 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65   size and.** are
b600: 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61   aligned to an a
b610: 64 64 72 65 73 73 20 77 68 69 63 68 20 69 73 20  ddress which is 
b620: 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69  an integer multi
b630: 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72  ple of.** nnn ar
b640: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53  e atomic.  The S
b650: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45  QLITE_IOCAP_SAFE
b660: 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65  _APPEND value me
b670: 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e  ans.** that when
b680: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65   data is appende
b690: 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65  d to a file, the
b6a0: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65   data is appende
b6b0: 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20  d.** first then 
b6c0: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
b6d0: 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64  file is extended
b6e0: 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65  , never the othe
b6f0: 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e  r.** way around.
b700: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
b710: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72  AP_SEQUENTIAL pr
b720: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61  operty means tha
b730: 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  t.** information
b740: 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64   is written to d
b750: 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20  isk in the same 
b760: 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a  order as calls.*
b770: 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a  * to xWrite()..*
b780: 2a 0a 2a 2a 20 49 66 20 78 52 65 61 64 28 29 20  *.** If xRead() 
b790: 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49  returns SQLITE_I
b7a0: 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20  OERR_SHORT_READ 
b7b0: 69 74 20 6d 75 73 74 20 61 6c 73 6f 20 66 69 6c  it must also fil
b7c0: 6c 0a 2a 2a 20 69 6e 20 74 68 65 20 75 6e 72 65  l.** in the unre
b7d0: 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74  ad portions of t
b7e0: 68 65 20 62 75 66 66 65 72 20 77 69 74 68 20 7a  he buffer with z
b7f0: 65 72 6f 73 2e 20 20 41 20 56 46 53 20 74 68 61  eros.  A VFS tha
b800: 74 0a 2a 2a 20 66 61 69 6c 73 20 74 6f 20 7a 65  t.** fails to ze
b810: 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65  ro-fill short re
b820: 61 64 73 20 6d 69 67 68 74 20 73 65 65 6d 20 74  ads might seem t
b830: 6f 20 77 6f 72 6b 2e 20 20 48 6f 77 65 76 65 72  o work.  However
b840: 2c 0a 2a 2a 20 66 61 69 6c 75 72 65 20 74 6f 20  ,.** failure to 
b850: 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20  zero-fill short 
b860: 72 65 61 64 73 20 77 69 6c 6c 20 65 76 65 6e 74  reads will event
b870: 75 61 6c 6c 79 20 6c 65 61 64 20 74 6f 0a 2a 2a  ually lead to.**
b880: 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
b890: 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tion..*/.typedef
b8a0: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
b8b0: 69 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74  io_methods sqlit
b8c0: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73  e3_io_methods;.s
b8d0: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f  truct sqlite3_io
b8e0: 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74  _methods {.  int
b8f0: 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74   iVersion;.  int
b900: 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74   (*xClose)(sqlit
b910: 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74  e3_file*);.  int
b920: 20 28 2a 78 52 65 61 64 29 28 73 71 6c 69 74 65   (*xRead)(sqlite
b930: 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20  3_file*, void*, 
b940: 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65  int iAmt, sqlite
b950: 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a  3_int64 iOfst);.
b960: 20 20 69 6e 74 20 28 2a 78 57 72 69 74 65 29 28    int (*xWrite)(
b970: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63  sqlite3_file*, c
b980: 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20  onst void*, int 
b990: 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e  iAmt, sqlite3_in
b9a0: 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e  t64 iOfst);.  in
b9b0: 74 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28 73  t (*xTruncate)(s
b9c0: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71  qlite3_file*, sq
b9d0: 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 69 7a 65  lite3_int64 size
b9e0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63  );.  int (*xSync
b9f0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  )(sqlite3_file*,
ba00: 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 20 20 69   int flags);.  i
ba10: 6e 74 20 28 2a 78 46 69 6c 65 53 69 7a 65 29 28  nt (*xFileSize)(
ba20: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73  sqlite3_file*, s
ba30: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53  qlite3_int64 *pS
ba40: 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4c  ize);.  int (*xL
ba50: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock)(sqlite3_fil
ba60: 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20  e*, int);.  int 
ba70: 28 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69 74  (*xUnlock)(sqlit
ba80: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a  e3_file*, int);.
ba90: 20 20 69 6e 74 20 28 2a 78 43 68 65 63 6b 52 65    int (*xCheckRe
baa0: 73 65 72 76 65 64 4c 6f 63 6b 29 28 73 71 6c 69  servedLock)(sqli
bab0: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 2a  te3_file*, int *
bac0: 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20  pResOut);.  int 
bad0: 28 2a 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 29 28  (*xFileControl)(
bae0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
baf0: 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72  nt op, void *pAr
bb00: 67 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63  g);.  int (*xSec
bb10: 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33  torSize)(sqlite3
bb20: 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28  _file*);.  int (
bb30: 2a 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65  *xDeviceCharacte
bb40: 72 69 73 74 69 63 73 29 28 73 71 6c 69 74 65 33  ristics)(sqlite3
bb50: 5f 66 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20 41 64  _file*);.  /* Ad
bb60: 64 69 74 69 6f 6e 61 6c 20 6d 65 74 68 6f 64 73  ditional methods
bb70: 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e   may be added in
bb80: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
bb90: 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.};../*.** CA
bba0: 50 49 33 52 45 46 3a 20 53 74 61 6e 64 61 72 64  PI3REF: Standard
bbb0: 20 46 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 4f 70   File Control Op
bbc0: 63 6f 64 65 73 20 7b 48 31 31 33 31 30 7d 20 3c  codes {H11310} <
bbd0: 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30800>.**.** Th
bbe0: 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  ese integer cons
bbf0: 74 61 6e 74 73 20 61 72 65 20 6f 70 63 6f 64 65  tants are opcode
bc00: 73 20 66 6f 72 20 74 68 65 20 78 46 69 6c 65 43  s for the xFileC
bc10: 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a  ontrol method.**
bc20: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
bc30: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  _io_methods] obj
bc40: 65 63 74 20 61 6e 64 20 66 6f 72 20 74 68 65 20  ect and for the 
bc50: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f  [sqlite3_file_co
bc60: 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65  ntrol()].** inte
bc70: 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rface..**.** The
bc80: 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c   [SQLITE_FCNTL_L
bc90: 4f 43 4b 53 54 41 54 45 5d 20 6f 70 63 6f 64 65  OCKSTATE] opcode
bca0: 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62   is used for deb
bcb0: 75 67 67 69 6e 67 2e 20 20 54 68 69 73 0a 2a 2a  ugging.  This.**
bcc0: 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20 74   opcode causes t
bcd0: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  he xFileControl 
bce0: 6d 65 74 68 6f 64 20 74 6f 20 77 72 69 74 65 20  method to write 
bcf0: 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
bd00: 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b  e of.** the lock
bd10: 20 28 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45   (one of [SQLITE
bd20: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51  _LOCK_NONE], [SQ
bd30: 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44  LITE_LOCK_SHARED
bd40: 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f  ],.** [SQLITE_LO
bd50: 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 20 5b 53  CK_RESERVED], [S
bd60: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49  QLITE_LOCK_PENDI
bd70: 4e 47 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  NG], or [SQLITE_
bd80: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 29  LOCK_EXCLUSIVE])
bd90: 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65  .** into an inte
bda0: 67 65 72 20 74 68 61 74 20 74 68 65 20 70 41 72  ger that the pAr
bdb0: 67 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74  g argument point
bdc0: 73 20 74 6f 2e 20 54 68 69 73 20 63 61 70 61 62  s to. This capab
bdd0: 69 6c 69 74 79 0a 2a 2a 20 69 73 20 75 73 65 64  ility.** is used
bde0: 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 20   during testing 
bdf0: 61 6e 64 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74  and only needs t
be00: 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 20 77  o be supported w
be10: 68 65 6e 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  hen SQLITE_TEST.
be20: 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  ** is defined..*
be30: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
be40: 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
be50: 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
be60: 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43  e SQLITE_GET_LOC
be70: 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20 20  KPROXYFILE      
be80: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
be90: 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
bea0: 4c 45 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e  LE      3.#defin
beb0: 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52  e SQLITE_LAST_ER
bec0: 52 4e 4f 20 20 20 20 20 20 20 20 20 20 20 20 20  RNO             
bed0: 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  4../*.** CAPI3RE
bee0: 46 3a 20 4d 75 74 65 78 20 48 61 6e 64 6c 65 20  F: Mutex Handle 
bef0: 7b 48 31 37 31 31 30 7d 20 3c 53 32 30 31 33 30  {H17110} <S20130
bf00: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65  >.**.** The mute
bf10: 78 20 6d 6f 64 75 6c 65 20 77 69 74 68 69 6e 20  x module within 
bf20: 53 51 4c 69 74 65 20 64 65 66 69 6e 65 73 20 5b  SQLite defines [
bf30: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 74  sqlite3_mutex] t
bf40: 6f 20 62 65 20 61 6e 0a 2a 2a 20 61 62 73 74 72  o be an.** abstr
bf50: 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 6d  act type for a m
bf60: 75 74 65 78 20 6f 62 6a 65 63 74 2e 20 20 54 68  utex object.  Th
bf70: 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6e 65  e SQLite core ne
bf80: 76 65 72 20 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20  ver looks.** at 
bf90: 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
bfa0: 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  resentation of a
bfb0: 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  n [sqlite3_mutex
bfc0: 5d 2e 20 20 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64  ].  It only.** d
bfd0: 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65  eals with pointe
bfe0: 72 73 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  rs to the [sqlit
bff0: 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74  e3_mutex] object
c000: 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65 78 65 73 20  ..**.** Mutexes 
c010: 61 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e  are created usin
c020: 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  g [sqlite3_mutex
c030: 5f 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79  _alloc()]..*/.ty
c040: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
c050: 69 74 65 33 5f 6d 75 74 65 78 20 73 71 6c 69 74  ite3_mutex sqlit
c060: 65 33 5f 6d 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a  e3_mutex;../*.**
c070: 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e   CAPI3REF: OS In
c080: 74 65 72 66 61 63 65 20 4f 62 6a 65 63 74 20 7b  terface Object {
c090: 48 31 31 31 34 30 7d 20 3c 53 32 30 31 30 30 3e  H11140} <S20100>
c0a0: 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  .**.** An instan
c0b0: 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ce of the sqlite
c0c0: 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 64 65 66  3_vfs object def
c0d0: 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61  ines the interfa
c0e0: 63 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68  ce between.** th
c0f0: 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 61 6e  e SQLite core an
c100: 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  d the underlying
c110: 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
c120: 6d 2e 20 20 54 68 65 20 22 76 66 73 22 0a 2a 2a  m.  The "vfs".**
c130: 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   in the name of 
c140: 74 68 65 20 6f 62 6a 65 63 74 20 73 74 61 6e 64  the object stand
c150: 73 20 66 6f 72 20 22 76 69 72 74 75 61 6c 20 66  s for "virtual f
c160: 69 6c 65 20 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a  ile system"..**.
c170: 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
c180: 74 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65  the iVersion fie
c190: 6c 64 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20  ld is initially 
c1a0: 31 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72  1 but may be lar
c1b0: 67 65 72 20 69 6e 0a 2a 2a 20 66 75 74 75 72 65  ger in.** future
c1c0: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
c1d0: 69 74 65 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  ite.  Additional
c1e0: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61   fields may be a
c1f0: 70 70 65 6e 64 65 64 20 74 6f 20 74 68 69 73 0a  ppended to this.
c200: 2a 2a 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 74  ** object when t
c210: 68 65 20 69 56 65 72 73 69 6f 6e 20 76 61 6c 75  he iVersion valu
c220: 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 2e 20  e is increased. 
c230: 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73   Note that the s
c240: 74 72 75 63 74 75 72 65 0a 2a 2a 20 6f 66 20 74  tructure.** of t
c250: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f  he sqlite3_vfs o
c260: 62 6a 65 63 74 20 63 68 61 6e 67 65 73 20 69 6e  bject changes in
c270: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
c280: 20 62 65 74 77 65 65 6e 0a 2a 2a 20 53 51 4c 69   between.** SQLi
c290: 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 39  te version 3.5.9
c2a0: 20 61 6e 64 20 33 2e 36 2e 30 20 61 6e 64 20 79   and 3.6.0 and y
c2b0: 65 74 20 74 68 65 20 69 56 65 72 73 69 6f 6e 20  et the iVersion 
c2c0: 66 69 65 6c 64 20 77 61 73 20 6e 6f 74 0a 2a 2a  field was not.**
c2d0: 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a   modified..**.**
c2e0: 20 54 68 65 20 73 7a 4f 73 46 69 6c 65 20 66 69   The szOsFile fi
c2f0: 65 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20  eld is the size 
c300: 6f 66 20 74 68 65 20 73 75 62 63 6c 61 73 73 65  of the subclasse
c310: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d  d [sqlite3_file]
c320: 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 75 73  .** structure us
c330: 65 64 20 62 79 20 74 68 69 73 20 56 46 53 2e 20  ed by this VFS. 
c340: 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 74   mxPathname is t
c350: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
c360: 68 20 6f 66 0a 2a 2a 20 61 20 70 61 74 68 6e 61  h of.** a pathna
c370: 6d 65 20 69 6e 20 74 68 69 73 20 56 46 53 2e 0a  me in this VFS..
c380: 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 65 64  **.** Registered
c390: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a   sqlite3_vfs obj
c3a0: 65 63 74 73 20 61 72 65 20 6b 65 70 74 20 6f 6e  ects are kept on
c3b0: 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 66   a linked list f
c3c0: 6f 72 6d 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  ormed by.** the 
c3d0: 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e 20 20  pNext pointer.  
c3e0: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  The [sqlite3_vfs
c3f0: 5f 72 65 67 69 73 74 65 72 28 29 5d 0a 2a 2a 20  _register()].** 
c400: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  and [sqlite3_vfs
c410: 5f 75 6e 72 65 67 69 73 74 65 72 28 29 5d 20 69  _unregister()] i
c420: 6e 74 65 72 66 61 63 65 73 20 6d 61 6e 61 67 65  nterfaces manage
c430: 20 74 68 69 73 20 6c 69 73 74 0a 2a 2a 20 69 6e   this list.** in
c440: 20 61 20 74 68 72 65 61 64 2d 73 61 66 65 20 77   a thread-safe w
c450: 61 79 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65  ay.  The [sqlite
c460: 33 5f 76 66 73 5f 66 69 6e 64 28 29 5d 20 69 6e  3_vfs_find()] in
c470: 74 65 72 66 61 63 65 0a 2a 2a 20 73 65 61 72 63  terface.** searc
c480: 68 65 73 20 74 68 65 20 6c 69 73 74 2e 20 20 4e  hes the list.  N
c490: 65 69 74 68 65 72 20 74 68 65 20 61 70 70 6c 69  either the appli
c4a0: 63 61 74 69 6f 6e 20 63 6f 64 65 20 6e 6f 72 20  cation code nor 
c4b0: 74 68 65 20 56 46 53 0a 2a 2a 20 69 6d 70 6c 65  the VFS.** imple
c4c0: 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64  mentation should
c4d0: 20 75 73 65 20 74 68 65 20 70 4e 65 78 74 20 70   use the pNext p
c4e0: 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ointer..**.** Th
c4f0: 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20 69 73  e pNext field is
c500: 20 74 68 65 20 6f 6e 6c 79 20 66 69 65 6c 64 20   the only field 
c510: 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  in the sqlite3_v
c520: 66 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  fs.** structure 
c530: 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c  that SQLite will
c540: 20 65 76 65 72 20 6d 6f 64 69 66 79 2e 20 20 53   ever modify.  S
c550: 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  QLite will only 
c560: 61 63 63 65 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64  access.** or mod
c570: 69 66 79 20 74 68 69 73 20 66 69 65 6c 64 20 77  ify this field w
c580: 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 61 20 70  hile holding a p
c590: 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 69 63  articular static
c5a0: 20 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 61   mutex..** The a
c5b0: 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c  pplication shoul
c5c0: 64 20 6e 65 76 65 72 20 6d 6f 64 69 66 79 20 61  d never modify a
c5d0: 6e 79 74 68 69 6e 67 20 77 69 74 68 69 6e 20 74  nything within t
c5e0: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a  he sqlite3_vfs.*
c5f0: 2a 20 6f 62 6a 65 63 74 20 6f 6e 63 65 20 74 68  * object once th
c600: 65 20 6f 62 6a 65 63 74 20 68 61 73 20 62 65 65  e object has bee
c610: 6e 20 72 65 67 69 73 74 65 72 65 64 2e 0a 2a 2a  n registered..**
c620: 0a 2a 2a 20 54 68 65 20 7a 4e 61 6d 65 20 66 69  .** The zName fi
c630: 65 6c 64 20 68 6f 6c 64 73 20 74 68 65 20 6e 61  eld holds the na
c640: 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20 6d 6f  me of the VFS mo
c650: 64 75 6c 65 2e 20 20 54 68 65 20 6e 61 6d 65 20  dule.  The name 
c660: 6d 75 73 74 0a 2a 2a 20 62 65 20 75 6e 69 71 75  must.** be uniqu
c670: 65 20 61 63 72 6f 73 73 20 61 6c 6c 20 56 46 53  e across all VFS
c680: 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20   modules..**.** 
c690: 53 51 4c 69 74 65 20 77 69 6c 6c 20 67 75 61 72  SQLite will guar
c6a0: 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 7a  antee that the z
c6b0: 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74  Filename paramet
c6c0: 65 72 20 74 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69  er to xOpen.** i
c6d0: 73 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 20  s either a NULL 
c6e0: 70 6f 69 6e 74 65 72 20 6f 72 20 73 74 72 69 6e  pointer or strin
c6f0: 67 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  g obtained.** fr
c700: 6f 6d 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65  om xFullPathname
c710: 28 29 2e 20 20 53 51 4c 69 74 65 20 66 75 72 74  ().  SQLite furt
c720: 68 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74  her guarantees t
c730: 68 61 74 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e  hat.** the strin
c740: 67 20 77 69 6c 6c 20 62 65 20 76 61 6c 69 64 20  g will be valid 
c750: 61 6e 64 20 75 6e 63 68 61 6e 67 65 64 20 75 6e  and unchanged un
c760: 74 69 6c 20 78 43 6c 6f 73 65 28 29 20 69 73 0a  til xClose() is.
c770: 2a 2a 20 63 61 6c 6c 65 64 2e 20 42 65 63 61 75  ** called. Becau
c780: 73 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f  se of the previo
c790: 75 73 20 73 65 6e 74 65 6e 73 65 2c 0a 2a 2a 20  us sentense,.** 
c7a0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  the [sqlite3_fil
c7b0: 65 5d 20 63 61 6e 20 73 61 66 65 6c 79 20 73 74  e] can safely st
c7c0: 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ore a pointer to
c7d0: 20 74 68 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65   the.** filename
c7e0: 20 69 66 20 69 74 20 6e 65 65 64 73 20 74 6f 20   if it needs to 
c7f0: 72 65 6d 65 6d 62 65 72 20 74 68 65 20 66 69 6c  remember the fil
c800: 65 6e 61 6d 65 20 66 6f 72 20 73 6f 6d 65 20 72  ename for some r
c810: 65 61 73 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65  eason..** If the
c820: 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d   zFilename param
c830: 65 74 65 72 20 69 73 20 78 4f 70 65 6e 20 69 73  eter is xOpen is
c840: 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
c850: 74 68 65 6e 20 78 4f 70 65 6e 0a 2a 2a 20 6d 75  then xOpen.** mu
c860: 73 74 20 69 6e 76 69 74 65 20 69 74 73 20 6f 77  st invite its ow
c870: 6e 20 74 65 6d 70 6f 72 61 72 79 20 6e 61 6d 65  n temporary name
c880: 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 20   for the file.  
c890: 57 68 65 6e 65 76 65 72 20 74 68 65 20 0a 2a 2a  Whenever the .**
c8a0: 20 78 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d   xFilename param
c8b0: 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 69 74 20  eter is NULL it 
c8c0: 77 69 6c 6c 20 61 6c 73 6f 20 62 65 20 74 68 65  will also be the
c8d0: 20 63 61 73 65 20 74 68 61 74 20 74 68 65 0a 2a   case that the.*
c8e0: 2a 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  * flags paramete
c8f0: 72 20 77 69 6c 6c 20 69 6e 63 6c 75 64 65 20 5b  r will include [
c900: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
c910: 54 45 4f 4e 43 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a  TEONCLOSE]..**.*
c920: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75  * The flags argu
c930: 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28 29 20  ment to xOpen() 
c940: 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20 62 69 74  includes all bit
c950: 73 20 73 65 74 20 69 6e 0a 2a 2a 20 74 68 65 20  s set in.** the 
c960: 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74  flags argument t
c970: 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  o [sqlite3_open_
c980: 76 32 28 29 5d 2e 20 20 4f 72 20 69 66 20 5b 73  v2()].  Or if [s
c990: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a  qlite3_open()].*
c9a0: 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70  * or [sqlite3_op
c9b0: 65 6e 31 36 28 29 5d 20 69 73 20 75 73 65 64 2c  en16()] is used,
c9c0: 20 74 68 65 6e 20 66 6c 61 67 73 20 69 6e 63 6c   then flags incl
c9d0: 75 64 65 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a  udes at least.**
c9e0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   [SQLITE_OPEN_RE
c9f0: 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49  ADWRITE] | [SQLI
ca00: 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 2e  TE_OPEN_CREATE].
ca10: 20 0a 2a 2a 20 49 66 20 78 4f 70 65 6e 28 29 20   .** If xOpen() 
ca20: 6f 70 65 6e 73 20 61 20 66 69 6c 65 20 72 65 61  opens a file rea
ca30: 64 2d 6f 6e 6c 79 20 74 68 65 6e 20 69 74 20 73  d-only then it s
ca40: 65 74 73 20 2a 70 4f 75 74 46 6c 61 67 73 20 74  ets *pOutFlags t
ca50: 6f 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 5b 53 51  o.** include [SQ
ca60: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
ca70: 4c 59 5d 2e 20 20 4f 74 68 65 72 20 62 69 74 73  LY].  Other bits
ca80: 20 69 6e 20 2a 70 4f 75 74 46 6c 61 67 73 20 6d   in *pOutFlags m
ca90: 61 79 20 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a  ay be set..**.**
caa0: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73   SQLite will als
cab0: 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68 65  o add one of the
cac0: 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73   following flags
cad0: 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 28 29 0a   to the xOpen().
cae0: 2a 2a 20 63 61 6c 6c 2c 20 64 65 70 65 6e 64 69  ** call, dependi
caf0: 6e 67 20 6f 6e 20 74 68 65 20 6f 62 6a 65 63 74  ng on the object
cb00: 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a   being opened:.*
cb10: 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
cb20: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  >  [SQLITE_OPEN_
cb30: 4d 41 49 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e  MAIN_DB].** <li>
cb40: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d    [SQLITE_OPEN_M
cb50: 41 49 4e 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20  AIN_JOURNAL].** 
cb60: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
cb70: 45 4e 5f 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c  EN_TEMP_DB].** <
cb80: 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45  li>  [SQLITE_OPE
cb90: 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a  N_TEMP_JOURNAL].
cba0: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45  ** <li>  [SQLITE
cbb0: 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f  _OPEN_TRANSIENT_
cbc0: 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51  DB].** <li>  [SQ
cbd0: 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55  LITE_OPEN_SUBJOU
cbe0: 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b  RNAL].** <li>  [
cbf0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54  SQLITE_OPEN_MAST
cc00: 45 52 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c  ER_JOURNAL].** <
cc10: 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  /ul>.**.** The f
cc20: 69 6c 65 20 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e  ile I/O implemen
cc30: 74 61 74 69 6f 6e 20 63 61 6e 20 75 73 65 20 74  tation can use t
cc40: 68 65 20 6f 62 6a 65 63 74 20 74 79 70 65 20 66  he object type f
cc50: 6c 61 67 73 20 74 6f 0a 2a 2a 20 63 68 61 6e 67  lags to.** chang
cc60: 65 20 74 68 65 20 77 61 79 20 69 74 20 64 65 61  e the way it dea
cc70: 6c 73 20 77 69 74 68 20 66 69 6c 65 73 2e 20 20  ls with files.  
cc80: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20  For example, an 
cc90: 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 74  application.** t
cca0: 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 63 61 72  hat does not car
ccb0: 65 20 61 62 6f 75 74 20 63 72 61 73 68 20 72 65  e about crash re
ccc0: 63 6f 76 65 72 79 20 6f 72 20 72 6f 6c 6c 62 61  covery or rollba
ccd0: 63 6b 20 6d 69 67 68 74 20 6d 61 6b 65 0a 2a 2a  ck might make.**
cce0: 20 74 68 65 20 6f 70 65 6e 20 6f 66 20 61 20 6a   the open of a j
ccf0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 20 6e 6f  ournal file a no
cd00: 2d 6f 70 2e 20 20 57 72 69 74 65 73 20 74 6f 20  -op.  Writes to 
cd10: 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 6f 75  this journal wou
cd20: 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 6e 6f  ld.** also be no
cd30: 2d 6f 70 73 2c 20 61 6e 64 20 61 6e 79 20 61 74  -ops, and any at
cd40: 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68  tempt to read th
cd50: 65 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 20  e journal would 
cd60: 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
cd70: 5f 49 4f 45 52 52 2e 20 20 4f 72 20 74 68 65 20  _IOERR.  Or the 
cd80: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d  implementation m
cd90: 69 67 68 74 20 72 65 63 6f 67 6e 69 7a 65 20 74  ight recognize t
cda0: 68 61 74 20 61 20 64 61 74 61 62 61 73 65 0a 2a  hat a database.*
cdb0: 2a 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64  * file will be d
cdc0: 6f 69 6e 67 20 70 61 67 65 2d 61 6c 69 67 6e 65  oing page-aligne
cdd0: 64 20 73 65 63 74 6f 72 20 72 65 61 64 73 20 61  d sector reads a
cde0: 6e 64 20 77 72 69 74 65 73 20 69 6e 20 61 20 72  nd writes in a r
cdf0: 61 6e 64 6f 6d 0a 2a 2a 20 6f 72 64 65 72 20 61  andom.** order a
ce00: 6e 64 20 73 65 74 20 75 70 20 69 74 73 20 49 2f  nd set up its I/
ce10: 4f 20 73 75 62 73 79 73 74 65 6d 20 61 63 63 6f  O subsystem acco
ce20: 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53  rdingly..**.** S
ce30: 51 4c 69 74 65 20 6d 69 67 68 74 20 61 6c 73 6f  QLite might also
ce40: 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 20   add one of the 
ce50: 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20  following flags 
ce60: 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74  to the xOpen met
ce70: 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  hod:.**.** <ul>.
ce80: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
ce90: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
cea0: 53 45 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  SE].** <li> [SQL
ceb0: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
cec0: 56 45 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  VE].** </ul>.**.
ced0: 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f  ** The [SQLITE_O
cee0: 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
cef0: 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68  E] flag means th
cf00: 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  e file should be
cf10: 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e  .** deleted when
cf20: 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 20 20   it is closed.  
cf30: 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  The [SQLITE_OPEN
cf40: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a  _DELETEONCLOSE].
cf50: 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 66  ** will be set f
cf60: 6f 72 20 54 45 4d 50 20 20 64 61 74 61 62 61 73  or TEMP  databas
cf70: 65 73 2c 20 6a 6f 75 72 6e 61 6c 73 20 61 6e 64  es, journals and
cf80: 20 66 6f 72 20 73 75 62 6a 6f 75 72 6e 61 6c 73   for subjournals
cf90: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c  ..**.** The [SQL
cfa0: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
cfb0: 56 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74  VE] flag means t
cfc0: 68 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  he file should b
cfd0: 65 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20  e opened.** for 
cfe0: 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73  exclusive access
cff0: 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 20  .  This flag is 
d000: 73 65 74 20 66 6f 72 20 61 6c 6c 20 66 69 6c 65  set for all file
d010: 73 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20  s except.** for 
d020: 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
d030: 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 74  e file..**.** At
d040: 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65 20   least szOsFile 
d050: 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
d060: 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  are allocated by
d070: 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68 6f   SQLite.** to ho
d080: 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65 33  ld the  [sqlite3
d090: 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72 65  _file] structure
d0a0: 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74   passed as the t
d0b0: 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  hird.** argument
d0c0: 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65 20   to xOpen.  The 
d0d0: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f 65  xOpen method doe
d0e0: 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a 2a  s not have to.**
d0f0: 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 74   allocate the st
d100: 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f 75  ructure; it shou
d110: 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74 20  ld just fill it 
d120: 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c  in..**.** The fl
d130: 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ags argument to 
d140: 78 41 63 63 65 73 73 28 29 20 6d 61 79 20 62 65  xAccess() may be
d150: 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f   [SQLITE_ACCESS_
d160: 45 58 49 53 54 53 5d 0a 2a 2a 20 74 6f 20 74 65  EXISTS].** to te
d170: 73 74 20 66 6f 72 20 74 68 65 20 65 78 69 73 74  st for the exist
d180: 65 6e 63 65 20 6f 66 20 61 20 66 69 6c 65 2c 20  ence of a file, 
d190: 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53  or [SQLITE_ACCES
d1a0: 53 5f 52 45 41 44 57 52 49 54 45 5d 20 74 6f 0a  S_READWRITE] to.
d1b0: 2a 2a 20 74 65 73 74 20 77 68 65 74 68 65 72 20  ** test whether 
d1c0: 61 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62  a file is readab
d1d0: 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2c  le and writable,
d1e0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45   or [SQLITE_ACCE
d1f0: 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 74 6f 20 74  SS_READ].** to t
d200: 65 73 74 20 77 68 65 74 68 65 72 20 61 20 66 69  est whether a fi
d210: 6c 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 72  le is at least r
d220: 65 61 64 61 62 6c 65 2e 20 20 20 54 68 65 20 66  eadable.   The f
d230: 69 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a 2a 20  ile can be a.** 
d240: 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a  directory..**.**
d250: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 77   SQLite will alw
d260: 61 79 73 20 61 6c 6c 6f 63 61 74 65 20 61 74 20  ays allocate at 
d270: 6c 65 61 73 74 20 6d 78 50 61 74 68 6e 61 6d 65  least mxPathname
d280: 2b 31 20 62 79 74 65 73 20 66 6f 72 20 74 68 65  +1 bytes for the
d290: 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65  .** output buffe
d2a0: 72 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2e  r xFullPathname.
d2b0: 20 20 54 68 65 20 65 78 61 63 74 20 73 69 7a 65    The exact size
d2c0: 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62   of the output b
d2d0: 75 66 66 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f  uffer.** is also
d2e0: 20 70 61 73 73 65 64 20 61 73 20 61 20 70 61 72   passed as a par
d2f0: 61 6d 65 74 65 72 20 74 6f 20 62 6f 74 68 20 20  ameter to both  
d300: 6d 65 74 68 6f 64 73 2e 20 49 66 20 74 68 65 20  methods. If the 
d310: 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a  output buffer.**
d320: 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65 6e   is not large en
d330: 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45 5f 43 41  ough, [SQLITE_CA
d340: 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c 64 20 62  NTOPEN] should b
d350: 65 20 72 65 74 75 72 6e 65 64 2e 20 53 69 6e 63  e returned. Sinc
d360: 65 20 74 68 69 73 20 69 73 0a 2a 2a 20 68 61 6e  e this is.** han
d370: 64 6c 65 64 20 61 73 20 61 20 66 61 74 61 6c 20  dled as a fatal 
d380: 65 72 72 6f 72 20 62 79 20 53 51 4c 69 74 65 2c  error by SQLite,
d390: 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74   vfs implementat
d3a0: 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65 6e 64 65  ions should ende
d3b0: 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72 65 76 65  avor.** to preve
d3c0: 6e 74 20 74 68 69 73 20 62 79 20 73 65 74 74 69  nt this by setti
d3d0: 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65 20 74 6f  ng mxPathname to
d3e0: 20 61 20 73 75 66 66 69 63 69 65 6e 74 6c 79 20   a sufficiently 
d3f0: 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a  large value..**.
d400: 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65  ** The xRandomne
d410: 73 73 28 29 2c 20 78 53 6c 65 65 70 28 29 2c 20  ss(), xSleep(), 
d420: 61 6e 64 20 78 43 75 72 72 65 6e 74 54 69 6d 65  and xCurrentTime
d430: 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  () interfaces.**
d440: 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c   are not strictl
d450: 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 20  y a part of the 
d460: 66 69 6c 65 73 79 73 74 65 6d 2c 20 62 75 74 20  filesystem, but 
d470: 74 68 65 79 20 61 72 65 0a 2a 2a 20 69 6e 63 6c  they are.** incl
d480: 75 64 65 64 20 69 6e 20 74 68 65 20 56 46 53 20  uded in the VFS 
d490: 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 63 6f  structure for co
d4a0: 6d 70 6c 65 74 65 6e 65 73 73 2e 0a 2a 2a 20 54  mpleteness..** T
d4b0: 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 28 29  he xRandomness()
d4c0: 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70   function attemp
d4d0: 74 73 20 74 6f 20 72 65 74 75 72 6e 20 6e 42 79  ts to return nBy
d4e0: 74 65 73 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20  tes bytes.** of 
d4f0: 67 6f 6f 64 2d 71 75 61 6c 69 74 79 20 72 61 6e  good-quality ran
d500: 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 4f 75  domness into zOu
d510: 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  t.  The return v
d520: 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 61  alue is.** the a
d530: 63 74 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  ctual number of 
d540: 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e  bytes of randomn
d550: 65 73 73 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2a  ess obtained..**
d560: 20 54 68 65 20 78 53 6c 65 65 70 28 29 20 6d 65   The xSleep() me
d570: 74 68 6f 64 20 63 61 75 73 65 73 20 74 68 65 20  thod causes the 
d580: 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 74  calling thread t
d590: 6f 20 73 6c 65 65 70 20 66 6f 72 20 61 74 0a 2a  o sleep for at.*
d5a0: 2a 20 6c 65 61 73 74 20 74 68 65 20 6e 75 6d 62  * least the numb
d5b0: 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e  er of microsecon
d5c0: 64 73 20 67 69 76 65 6e 2e 20 20 54 68 65 20 78  ds given.  The x
d5d0: 43 75 72 72 65 6e 74 54 69 6d 65 28 29 0a 2a 2a  CurrentTime().**
d5e0: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
d5f0: 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d  a Julian Day Num
d600: 62 65 72 20 66 6f 72 20 74 68 65 20 63 75 72 72  ber for the curr
d610: 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 6d  ent date and tim
d620: 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79 70 65 64 65 66  e..**.*/.typedef
d630: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
d640: 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73 3b  vfs sqlite3_vfs;
d650: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
d660: 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72  vfs {.  int iVer
d670: 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  sion;           
d680: 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76 65   /* Structure ve
d690: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  rsion number */.
d6a0: 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b 20    int szOsFile; 
d6b0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
d6c0: 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65 64  ze of subclassed
d6d0: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2f   sqlite3_file */
d6e0: 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61 6d  .  int mxPathnam
d6f0: 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  e;          /* M
d700: 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74 68  aximum file path
d710: 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20  name length */. 
d720: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e   sqlite3_vfs *pN
d730: 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
d740: 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53  t registered VFS
d750: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
d760: 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f   *zName;       /
d770: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 76  * Name of this v
d780: 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74  irtual file syst
d790: 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  em */.  void *pA
d7a0: 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  ppData;         
d7b0: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
d7c0: 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63 69  pplication-speci
d7d0: 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  fic data */.  in
d7e0: 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74  t (*xOpen)(sqlit
d7f0: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
d800: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c 69  har *zName, sqli
d810: 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20 20  te3_file*,.     
d820: 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c            int fl
d830: 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46 6c  ags, int *pOutFl
d840: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44  ags);.  int (*xD
d850: 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f 76  elete)(sqlite3_v
d860: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
d870: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e 63  *zName, int sync
d880: 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 41  Dir);.  int (*xA
d890: 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f 76  ccess)(sqlite3_v
d8a0: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
d8b0: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67  *zName, int flag
d8c0: 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  s, int *pResOut)
d8d0: 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c 6c 50  ;.  int (*xFullP
d8e0: 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  athname)(sqlite3
d8f0: 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61  _vfs*, const cha
d900: 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4f  r *zName, int nO
d910: 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b  ut, char *zOut);
d920: 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 4f 70  .  void *(*xDlOp
d930: 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  en)(sqlite3_vfs*
d940: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
d950: 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f 69 64  ilename);.  void
d960: 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 71 6c   (*xDlError)(sql
d970: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e  ite3_vfs*, int n
d980: 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72 72  Byte, char *zErr
d990: 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 28  Msg);.  void (*(
d9a0: 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 65 33  *xDlSym)(sqlite3
d9b0: 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 6f 6e  _vfs*,void*, con
d9c0: 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c  st char *zSymbol
d9d0: 29 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64  ))(void);.  void
d9e0: 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73 71 6c   (*xDlClose)(sql
d9f0: 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69 64 2a  ite3_vfs*, void*
da00: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 61 6e 64  );.  int (*xRand
da10: 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74 65 33 5f  omness)(sqlite3_
da20: 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c  vfs*, int nByte,
da30: 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a 20 20   char *zOut);.  
da40: 69 6e 74 20 28 2a 78 53 6c 65 65 70 29 28 73 71  int (*xSleep)(sq
da50: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20  lite3_vfs*, int 
da60: 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b 0a 20  microseconds);. 
da70: 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e 74 54   int (*xCurrentT
da80: 69 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ime)(sqlite3_vfs
da90: 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 20 20 69  *, double*);.  i
daa0: 6e 74 20 28 2a 78 47 65 74 4c 61 73 74 45 72 72  nt (*xGetLastErr
dab0: 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  or)(sqlite3_vfs*
dac0: 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a  , int, char *);.
dad0: 20 20 2f 2a 20 4e 65 77 20 66 69 65 6c 64 73 20    /* New fields 
dae0: 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64 20  may be appended 
daf0: 69 6e 20 66 69 67 75 72 65 20 76 65 72 73 69 6f  in figure versio
db00: 6e 73 2e 20 20 54 68 65 20 69 56 65 72 73 69 6f  ns.  The iVersio
db10: 6e 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c  n.  ** value wil
db20: 6c 20 69 6e 63 72 65 6d 65 6e 74 20 77 68 65 6e  l increment when
db30: 65 76 65 72 20 74 68 69 73 20 68 61 70 70 65 6e  ever this happen
db40: 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  s. */.};../*.** 
db50: 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20  CAPI3REF: Flags 
db60: 66 6f 72 20 74 68 65 20 78 41 63 63 65 73 73 20  for the xAccess 
db70: 56 46 53 20 6d 65 74 68 6f 64 20 7b 48 31 31 31  VFS method {H111
db80: 39 30 7d 20 3c 48 31 31 31 34 30 3e 0a 2a 2a 0a  90} <H11140>.**.
db90: 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72  ** These integer
dba0: 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 6e 20 62   constants can b
dbb0: 65 20 75 73 65 64 20 61 73 20 74 68 65 20 74 68  e used as the th
dbc0: 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ird parameter to
dbd0: 0a 2a 2a 20 74 68 65 20 78 41 63 63 65 73 73 20  .** the xAccess 
dbe0: 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b 73 71  method of an [sq
dbf0: 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63  lite3_vfs] objec
dc00: 74 2e 20 7b 45 4e 44 7d 20 20 54 68 65 79 20 64  t. {END}  They d
dc10: 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 61 74  etermine.** what
dc20: 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69 73 73   kind of permiss
dc30: 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65 73 73  ions the xAccess
dc40: 20 6d 65 74 68 6f 64 20 69 73 20 6c 6f 6f 6b 69   method is looki
dc50: 6e 67 20 66 6f 72 2e 0a 2a 2a 20 57 69 74 68 20  ng for..** With 
dc60: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
dc70: 49 53 54 53 2c 20 74 68 65 20 78 41 63 63 65 73  ISTS, the xAcces
dc80: 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 73 69 6d 70  s method.** simp
dc90: 6c 79 20 63 68 65 63 6b 73 20 77 68 65 74 68 65  ly checks whethe
dca0: 72 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74  r the file exist
dcb0: 73 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54  s..** With SQLIT
dcc0: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
dcd0: 54 45 2c 20 74 68 65 20 78 41 63 63 65 73 73 20  TE, the xAccess 
dce0: 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73  method.** checks
dcf0: 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c   whether the fil
dd00: 65 20 69 73 20 62 6f 74 68 20 72 65 61 64 61 62  e is both readab
dd10: 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2e  le and writable.
dd20: 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f  .** With SQLITE_
dd30: 41 43 43 45 53 53 5f 52 45 41 44 2c 20 74 68 65  ACCESS_READ, the
dd40: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a   xAccess method.
dd50: 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65  ** checks whethe
dd60: 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65  r the file is re
dd70: 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69  adable..*/.#defi
dd80: 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  ne SQLITE_ACCESS
dd90: 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64 65  _EXISTS    0.#de
dda0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45  fine SQLITE_ACCE
ddb0: 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 0a 23  SS_READWRITE 1.#
ddc0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43  define SQLITE_AC
ddd0: 43 45 53 53 5f 52 45 41 44 20 20 20 20 20 20 32  CESS_READ      2
dde0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
ddf0: 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68 65  : Initialize The
de00: 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 20   SQLite Library 
de10: 7b 48 31 30 31 33 30 7d 20 3c 53 32 30 30 30 30  {H10130} <S20000
de20: 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20  ><S30100>.**.** 
de30: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  The sqlite3_init
de40: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65  ialize() routine
de50: 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65   initializes the
de60: 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 62 72 61  .** SQLite libra
de70: 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  ry.  The sqlite3
de80: 5f 73 68 75 74 64 6f 77 6e 28 29 20 72 6f 75 74  _shutdown() rout
de90: 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74  ine.** deallocat
dea0: 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73  es any resources
deb0: 20 74 68 61 74 20 77 65 72 65 20 61 6c 6c 6f 63   that were alloc
dec0: 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  ated by sqlite3_
ded0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a  initialize()..**
dee0: 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71  .** A call to sq
def0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
df00: 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74  () is an "effect
df10: 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20  ive" call if it 
df20: 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  is.** the first 
df30: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  time sqlite3_ini
df40: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76  tialize() is inv
df50: 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20  oked during the 
df60: 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74  lifetime of.** t
df70: 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69  he process, or i
df80: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73  f it is the firs
df90: 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69  t time sqlite3_i
dfa0: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69  nitialize() is i
dfb0: 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77  nvoked.** follow
dfc0: 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
dfd0: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
dfe0: 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63  .  Only an effec
dff0: 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20  tive call.** of 
e000: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
e010: 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e  ze() does any in
e020: 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41  itialization.  A
e030: 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a  ll other calls.*
e040: 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e  * are harmless n
e050: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f  o-ops..**.** Amo
e060: 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c  ng other things,
e070: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
e080: 69 7a 65 28 29 20 73 68 61 6c 6c 20 69 6e 76 6f  ize() shall invo
e090: 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73  ke.** sqlite3_os
e0a0: 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61  _init().  Simila
e0b0: 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f 73 68 75  rly, sqlite3_shu
e0c0: 74 64 6f 77 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c  tdown().** shall
e0d0: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
e0e0: 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20  os_end()..**.** 
e0f0: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  The sqlite3_init
e100: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65  ialize() routine
e110: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
e120: 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 2e  _OK] on success.
e130: 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d 65 20  .** If for some 
e140: 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65 33 5f  reason, sqlite3_
e150: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20  initialize() is 
e160: 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61  unable to initia
e170: 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72  lize.** the libr
e180: 61 72 79 20 28 70 65 72 68 61 70 73 20 69 74 20  ary (perhaps it 
e190: 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c  is unable to all
e1a0: 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64 20 72  ocate a needed r
e1b0: 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a 2a 20  esource such.** 
e1c0: 61 73 20 61 20 6d 75 74 65 78 29 20 69 74 20 72  as a mutex) it r
e1d0: 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72 6f 72  eturns an [error
e1e0: 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74 68 61   code] other tha
e1f0: 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a  n [SQLITE_OK]..*
e200: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
e210: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f  _initialize() ro
e220: 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
e230: 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 6d 61  internally by ma
e240: 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69  ny other.** SQLi
e250: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 73 6f  te interfaces so
e260: 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61   that an applica
e270: 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64 6f 65  tion usually doe
e280: 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 2a 2a  s not need to.**
e290: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
e2a0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72  initialize() dir
e2b0: 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78 61 6d  ectly.  For exam
e2c0: 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ple, [sqlite3_op
e2d0: 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73  en()].** calls s
e2e0: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
e2f0: 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c 69 74  e() so the SQLit
e300: 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 62  e library will b
e310: 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a  e automatically.
e320: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77  ** initialized w
e330: 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  hen [sqlite3_ope
e340: 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 69  n()] is called i
e350: 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 20  f it has not be 
e360: 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61  initialized.** a
e370: 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76 65 72  lready.  However
e380: 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63  , if SQLite is c
e390: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
e3a0: 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55   [SQLITE_OMIT_AU
e3b0: 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69  TOINIT].** compi
e3c0: 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20  le-time option, 
e3d0: 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d 61 74  then the automat
e3e0: 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ic calls to sqli
e3f0: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
e400: 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65 64 20  .** are omitted 
e410: 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63 61 74  and the applicat
e420: 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20 73 71  ion must call sq
e430: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
e440: 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 70  () directly.** p
e450: 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e  rior to using an
e460: 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69  y other SQLite i
e470: 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72 20 6d  nterface.  For m
e480: 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69 6c 69  aximum portabili
e490: 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72 65 63  ty,.** it is rec
e4a0: 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70  ommended that ap
e4b0: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77 61 79  plications alway
e4c0: 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33  s invoke sqlite3
e4d0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a  _initialize().**
e4e0: 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f 72 20   directly prior 
e4f0: 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68  to using any oth
e500: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
e510: 61 63 65 2e 20 20 46 75 74 75 72 65 20 72 65 6c  ace.  Future rel
e520: 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51 4c 69  eases.** of SQLi
e530: 74 65 20 6d 61 79 20 72 65 71 75 69 72 65 20 74  te may require t
e540: 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  his.  In other w
e550: 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61 76 69  ords, the behavi
e560: 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a 2a 20  or exhibited.** 
e570: 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63  when SQLite is c
e580: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51  ompiled with [SQ
e590: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
e5a0: 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f 6d 65  IT] might become
e5b0: 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c 74 20   the.** default 
e5c0: 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f 6d 65  behavior in some
e5d0: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 20   future release 
e5e0: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  of SQLite..**.**
e5f0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f   The sqlite3_os_
e600: 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65 20 64  init() routine d
e610: 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d 73 79  oes operating-sy
e620: 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a 2a 2a  stem specific.**
e630: 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
e640: 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  of the SQLite li
e650: 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69  brary.  The sqli
e660: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20  te3_os_end().** 
e670: 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73 20 74  routine undoes t
e680: 68 65 20 65 66 66 65 63 74 20 6f 66 20 73 71 6c  he effect of sql
e690: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20  ite3_os_init(). 
e6a0: 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73 0a 2a   Typical tasks.*
e6b0: 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74  * performed by t
e6c0: 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6e  hese routines in
e6d0: 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  clude allocation
e6e0: 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6f 6e   or deallocation
e6f0: 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20 72 65  .** of static re
e700: 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69 61 6c  sources, initial
e710: 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f 62 61  ization of globa
e720: 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a 2a 20  l variables,.** 
e730: 73 65 74 74 69 6e 67 20 75 70 20 61 20 64 65 66  setting up a def
e740: 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66  ault [sqlite3_vf
e750: 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 73 65  s] module, or se
e760: 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20 64 65  tting up.** a de
e770: 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74  fault configurat
e780: 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ion using [sqlit
e790: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a  e3_config()]..**
e7a0: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74  .** The applicat
e7b0: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  ion should never
e7c0: 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72 20 73   invoke either s
e7d0: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
e7e0: 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f  .** or sqlite3_o
e7f0: 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74 6c 79  s_end() directly
e800: 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  .  The applicati
e810: 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 69  on should only i
e820: 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33  nvoke.** sqlite3
e830: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e  _initialize() an
e840: 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  d sqlite3_shutdo
e850: 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c 69 74  wn().  The sqlit
e860: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20  e3_os_init().** 
e870: 69 6e 74 65 72 66 61 63 65 20 69 73 20 63 61 6c  interface is cal
e880: 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  led automaticall
e890: 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69  y by sqlite3_ini
e8a0: 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a 2a 2a  tialize() and.**
e8b0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
e8c0: 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 73  ) is called by s
e8d0: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
e8e0: 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65 0a  ).  Appropriate.
e8f0: 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
e900: 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f  ns for sqlite3_o
e910: 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c  s_init() and sql
e920: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a  ite3_os_end().**
e930: 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20   are built into 
e940: 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74 20 69  SQLite when it i
e950: 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 20 75  s compiled for u
e960: 6e 69 78 2c 20 77 69 6e 64 6f 77 73 2c 20 6f 72  nix, windows, or
e970: 20 6f 73 2f 32 2e 0a 2a 2a 20 57 68 65 6e 20 62   os/2..** When b
e980: 75 69 6c 74 20 66 6f 72 20 6f 74 68 65 72 20 70  uilt for other p
e990: 6c 61 74 66 6f 72 6d 73 20 28 75 73 69 6e 67 20  latforms (using 
e9a0: 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f  the [SQLITE_OS_O
e9b0: 54 48 45 52 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d  THER=1] compile-
e9c0: 74 69 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20  time.** option) 
e9d0: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
e9e0: 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 73 75  must supply a su
e9f0: 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  itable implement
ea00: 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c  ation for.** sql
ea10: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61  ite3_os_init() a
ea20: 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nd sqlite3_os_en
ea30: 64 28 29 2e 20 20 41 6e 20 61 70 70 6c 69 63 61  d().  An applica
ea40: 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 0a 2a 2a  tion-supplied.**
ea50: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
ea60: 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  of sqlite3_os_in
ea70: 69 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  it() or sqlite3_
ea80: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 6d 75 73 74  os_end().** must
ea90: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
eaa0: 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61  OK] on success a
eab0: 6e 64 20 73 6f 6d 65 20 6f 74 68 65 72 20 5b 65  nd some other [e
eac0: 72 72 6f 72 20 63 6f 64 65 5d 20 75 70 6f 6e 0a  rror code] upon.
ead0: 2a 2a 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53  ** failure..*/.S
eae0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
eaf0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
eb00: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41  (void);.SQLITE_A
eb10: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
eb20: 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 53  hutdown(void);.S
eb30: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
eb40: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f  lite3_os_init(vo
eb50: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
eb60: 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65  int sqlite3_os_e
eb70: 6e 64 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a  nd(void);../*.**
eb80: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69   CAPI3REF: Confi
eb90: 67 75 72 69 6e 67 20 54 68 65 20 53 51 4c 69 74  guring The SQLit
eba0: 65 20 4c 69 62 72 61 72 79 20 7b 48 31 34 31 30  e Library {H1410
ebb0: 30 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 32  0} <S20000><S302
ebc0: 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
ebd0: 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  TAL.**.** The sq
ebe0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69  lite3_config() i
ebf0: 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64  nterface is used
ec00: 20 74 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20   to make global 
ec10: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a  configuration.**
ec20: 20 63 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69   changes to SQLi
ec30: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74  te in order to t
ec40: 75 6e 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68  une SQLite to th
ec50: 65 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73  e specific needs
ec60: 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69   of.** the appli
ec70: 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66  cation.  The def
ec80: 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69  ault configurati
ec90: 6f 6e 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  on is recommende
eca0: 64 20 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70  d for most.** ap
ecb0: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73  plications and s
ecc0: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
ecd0: 73 20 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65  s usually not ne
ece0: 63 65 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a  cessary.  It is.
ecf0: 2a 2a 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73  ** provided to s
ed00: 75 70 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c  upport rare appl
ed10: 69 63 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e  ications with un
ed20: 75 73 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a  usual needs..**.
ed30: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
ed40: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63  onfig() interfac
ed50: 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73  e is not threads
ed60: 61 66 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63  afe.  The applic
ed70: 61 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e  ation.** must in
ed80: 73 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68  sure that no oth
ed90: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
eda0: 61 63 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64  aces are invoked
edb0: 20 62 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72   by other.** thr
edc0: 65 61 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74  eads while sqlit
edd0: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72  e3_config() is r
ede0: 75 6e 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72  unning.  Further
edf0: 6d 6f 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f  more, sqlite3_co
ee00: 6e 66 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e  nfig().** may on
ee10: 6c 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72  ly be invoked pr
ee20: 69 6f 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69  ior to library i
ee30: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73  nitialization us
ee40: 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ing.** [sqlite3_
ee50: 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72  initialize()] or
ee60: 20 61 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20   after shutdown 
ee70: 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74  by [sqlite3_shut
ee80: 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65  down()]..** Note
ee90: 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 61 74 20  , however, that 
eea0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
eeb0: 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61   can be called a
eec0: 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a  s part of the.**
eed0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
eee0: 6f 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  of an applicatio
eef0: 6e 2d 64 65 66 69 6e 65 64 20 5b 73 71 6c 69 74  n-defined [sqlit
ef00: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a  e3_os_init()]..*
ef10: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
ef20: 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
ef30: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 61  e3_config() is a
ef40: 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51  n integer.** [SQ
ef50: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47  LITE_CONFIG_SING
ef60: 4c 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69  LETHREAD | confi
ef70: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d  guration option]
ef80: 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73   that determines
ef90: 0a 2a 2a 20 77 68 61 74 20 70 72 6f 70 65 72 74  .** what propert
efa0: 79 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 74  y of SQLite is t
efb0: 6f 20 62 65 20 63 6f 6e 66 69 67 75 72 65 64 2e  o be configured.
efc0: 20 20 53 75 62 73 65 71 75 65 6e 74 20 61 72 67    Subsequent arg
efd0: 75 6d 65 6e 74 73 0a 2a 2a 20 76 61 72 79 20 64  uments.** vary d
efe0: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
eff0: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  [SQLITE_CONFIG_S
f000: 49 4e 47 4c 45 54 48 52 45 41 44 20 7c 20 63 6f  INGLETHREAD | co
f010: 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
f020: 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69  on].** in the fi
f030: 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  rst argument..**
f040: 0a 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 66 69  .** When a confi
f050: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20  guration option 
f060: 69 73 20 73 65 74 2c 20 73 71 6c 69 74 65 33 5f  is set, sqlite3_
f070: 63 6f 6e 66 69 67 28 29 20 72 65 74 75 72 6e 73  config() returns
f080: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a   [SQLITE_OK]..**
f090: 20 49 66 20 74 68 65 20 6f 70 74 69 6f 6e 20 69   If the option i
f0a0: 73 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c  s unknown or SQL
f0b0: 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  ite is unable to
f0c0: 20 73 65 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a   set the option.
f0d0: 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  ** then this rou
f0e0: 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6e  tine returns a n
f0f0: 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63  on-zero [error c
f100: 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  ode]..**.** INVA
f110: 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
f120: 31 34 31 30 33 7d 20 41 20 73 75 63 63 65 73 73  14103} A success
f130: 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ful invocation o
f140: 66 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  f [sqlite3_confi
f150: 67 28 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72  g()] shall retur
f160: 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53  n.**          [S
f170: 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a  QLITE_OK]..**.**
f180: 20 7b 48 31 34 31 30 36 7d 20 54 68 65 20 5b 73   {H14106} The [s
f190: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
f1a0: 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c   interface shall
f1b0: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
f1c0: 4d 49 53 55 53 45 5d 0a 2a 2a 20 20 20 20 20 20  MISUSE].**      
f1d0: 20 20 20 20 69 66 20 69 74 20 69 73 20 69 6e 76      if it is inv
f1e0: 6f 6b 65 64 20 69 6e 20 62 65 74 77 65 65 6e 20  oked in between 
f1f0: 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
f200: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20  3_initialize()] 
f210: 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
f220: 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77  [sqlite3_shutdow
f230: 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34  n()]..**.** {H14
f240: 31 32 30 7d 20 41 20 73 75 63 63 65 73 73 66 75  120} A successfu
f250: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
f260: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49  e3_config]([SQLI
f270: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
f280: 54 48 52 45 41 44 5d 29 0a 2a 2a 20 20 20 20 20  THREAD]).**     
f290: 20 20 20 20 20 73 68 61 6c 6c 20 73 65 74 20 74       shall set t
f2a0: 68 65 20 64 65 66 61 75 6c 74 20 5b 74 68 72 65  he default [thre
f2b0: 61 64 69 6e 67 20 6d 6f 64 65 5d 20 74 6f 20 53  ading mode] to S
f2c0: 69 6e 67 6c 65 2d 74 68 72 65 61 64 2e 0a 2a 2a  ingle-thread..**
f2d0: 0a 2a 2a 20 7b 48 31 34 31 32 33 7d 20 41 20 73  .** {H14123} A s
f2e0: 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
f2f0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  o [sqlite3_confi
f300: 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  g]([SQLITE_CONFI
f310: 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d 29 0a  G_MULTITHREAD]).
f320: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c  **          shal
f330: 6c 20 73 65 74 20 74 68 65 20 64 65 66 61 75 6c  l set the defaul
f340: 74 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  t [threading mod
f350: 65 5d 20 74 6f 20 4d 75 6c 74 69 2d 74 68 72 65  e] to Multi-thre
f360: 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32  ad..**.** {H1412
f370: 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  6} A successful 
f380: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
f390: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
f3a0: 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a  _CONFIG_SERIALIZ
f3b0: 45 44 5d 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  ED]).**         
f3c0: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 64   shall set the d
f3d0: 65 66 61 75 6c 74 20 5b 74 68 72 65 61 64 69 6e  efault [threadin
f3e0: 67 20 6d 6f 64 65 5d 20 74 6f 20 53 65 72 69 61  g mode] to Seria
f3f0: 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  lized..**.** {H1
f400: 34 31 32 39 7d 20 41 20 73 75 63 63 65 73 73 66  4129} A successf
f410: 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ul call to [sqli
f420: 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c  te3_config]([SQL
f430: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
f440: 5d 2c 58 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  ],X).**         
f450: 20 77 68 65 72 65 20 58 20 69 73 20 61 20 70 6f   where X is a po
f460: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 69 74  inter to an init
f470: 69 61 6c 69 7a 65 64 20 5b 73 71 6c 69 74 65 33  ialized [sqlite3
f480: 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a  _mutex_methods].
f490: 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65  **          obje
f4a0: 63 74 20 73 68 61 6c 6c 20 63 61 75 73 65 20 61  ct shall cause a
f4b0: 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 6d 75  ll subsequent mu
f4c0: 74 65 78 20 6f 70 65 72 61 74 69 6f 6e 73 20 70  tex operations p
f4d0: 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 20 20 20 20  erformed.**     
f4e0: 20 20 20 20 20 62 79 20 53 51 4c 69 74 65 20 74       by SQLite t
f4f0: 6f 20 75 73 65 20 74 68 65 20 6d 75 74 65 78 20  o use the mutex 
f500: 6d 65 74 68 6f 64 73 20 74 68 61 74 20 77 65 72  methods that wer
f510: 65 20 70 72 65 73 65 6e 74 20 69 6e 20 58 0a 2a  e present in X.*
f520: 2a 20 20 20 20 20 20 20 20 20 20 64 75 72 69 6e  *          durin
f530: 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73  g the call to [s
f540: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
f550: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 32 7d  ..**.** {H14132}
f560: 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61   A successful ca
f570: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
f580: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43  onfig]([SQLITE_C
f590: 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 5d 2c  ONFIG_GETMUTEX],
f5a0: 58 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  X).**          w
f5b0: 68 65 72 65 20 58 20 69 73 20 61 20 70 6f 69 6e  here X is a poin
f5c0: 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74  ter to an [sqlit
f5d0: 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
f5e0: 5d 20 6f 62 6a 65 63 74 20 0a 2a 2a 20 20 20 20  ] object .**    
f5f0: 20 20 20 20 20 20 73 68 61 6c 6c 20 6f 76 65 72        shall over
f600: 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
f610: 74 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 75  t of [sqlite3_mu
f620: 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  tex_methods] obj
f630: 65 63 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ect.**          
f640: 77 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 6d  with the mutex m
f650: 65 74 68 6f 64 73 20 63 75 72 72 65 6e 74 6c 79  ethods currently
f660: 20 69 6e 20 75 73 65 20 62 79 20 53 51 4c 69 74   in use by SQLit
f670: 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 35  e..**.** {H14135
f680: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
f690: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
f6a0: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f  config]([SQLITE_
f6b0: 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2c 4d  CONFIG_MALLOC],M
f6c0: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68  ).**          wh
f6d0: 65 72 65 20 4d 20 69 73 20 61 20 70 6f 69 6e 74  ere M is a point
f6e0: 65 72 20 74 6f 20 61 6e 20 69 6e 69 74 69 61 6c  er to an initial
f6f0: 69 7a 65 64 20 5b 73 71 6c 69 74 65 33 5f 6d 65  ized [sqlite3_me
f700: 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 20 20  m_methods].**   
f710: 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 73 68         object sh
f720: 61 6c 6c 20 63 61 75 73 65 20 61 6c 6c 20 73 75  all cause all su
f730: 62 73 65 71 75 65 6e 74 20 6d 65 6d 6f 72 79 20  bsequent memory 
f740: 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 70 65 72 61  allocation opera
f750: 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  tions.**        
f760: 20 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 53    performed by S
f770: 51 4c 69 74 65 20 74 6f 20 75 73 65 20 74 68 65  QLite to use the
f780: 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 77 65   methods that we
f790: 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 0a 2a  re present in .*
f7a0: 2a 20 20 20 20 20 20 20 20 20 20 4d 20 64 75 72  *          M dur
f7b0: 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  ing the call to 
f7c0: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
f7d0: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33  )]..**.** {H1413
f7e0: 38 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  8} A successful 
f7f0: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
f800: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
f810: 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f  _CONFIG_GETMALLO
f820: 43 5d 2c 4d 29 0a 2a 2a 20 20 20 20 20 20 20 20  C],M).**        
f830: 20 20 77 68 65 72 65 20 4d 20 69 73 20 61 20 70    where M is a p
f840: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 5b 73 71  ointer to an [sq
f850: 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
f860: 73 5d 20 6f 62 6a 65 63 74 20 73 68 61 6c 6c 0a  s] object shall.
f870: 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 76 65 72  **          over
f880: 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
f890: 74 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 65  t of [sqlite3_me
f8a0: 6d 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63  m_methods] objec
f8b0: 74 20 77 69 74 68 20 0a 2a 2a 20 20 20 20 20 20  t with .**      
f8c0: 20 20 20 20 74 68 65 20 6d 65 6d 6f 72 79 20 61      the memory a
f8d0: 6c 6c 6f 63 61 74 69 6f 6e 20 6d 65 74 68 6f 64  llocation method
f8e0: 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  s currently in u
f8f0: 73 65 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20  se by.**        
f900: 20 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20    SQLite..**.** 
f910: 7b 48 31 34 31 34 31 7d 20 41 20 73 75 63 63 65  {H14141} A succe
f920: 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
f930: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b  qlite3_config]([
f940: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45  SQLITE_CONFIG_ME
f950: 4d 53 54 41 54 55 53 5d 2c 31 29 0a 2a 2a 20 20  MSTATUS],1).**  
f960: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 65 6e          shall en
f970: 61 62 6c 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  able the memory 
f980: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 74 61 74 75  allocation statu
f990: 73 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6c 6f 67  s collection log
f9a0: 69 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 34  ic..**.** {H1414
f9b0: 34 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  4} A successful 
f9c0: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
f9d0: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
f9e0: 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55  _CONFIG_MEMSTATU
f9f0: 53 5d 2c 30 29 0a 2a 2a 20 20 20 20 20 20 20 20  S],0).**        
fa00: 20 20 73 68 61 6c 6c 20 64 69 73 61 62 6c 65 20    shall disable 
fa10: 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
fa20: 61 74 69 6f 6e 20 73 74 61 74 75 73 20 63 6f 6c  ation status col
fa30: 6c 65 63 74 69 6f 6e 20 6c 6f 67 69 63 2e 0a 2a  lection logic..*
fa40: 2a 0a 2a 2a 20 7b 48 31 34 31 34 37 7d 20 54 68  *.** {H14147} Th
fa50: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
fa60: 69 6f 6e 20 73 74 61 74 75 73 20 63 6f 6c 6c 65  ion status colle
fa70: 63 74 69 6f 6e 20 6c 6f 67 69 63 20 73 68 61 6c  ction logic shal
fa80: 6c 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  l be.**         
fa90: 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61   enabled by defa
faa0: 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31  ult..**.** {H141
fab0: 35 30 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  50} A successful
fac0: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
fad0: 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54  3_config]([SQLIT
fae0: 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
faf0: 5d 2c 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20  ],S,Z,N).**     
fb00: 20 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e 64       where Z and
fb10: 20 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74   N are non-negat
fb20: 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64  ive integers and
fb30: 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 20   .**          S 
fb40: 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
fb50: 61 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72  an aligned memor
fb60: 79 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65 73  y buffer not les
fb70: 73 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20  s than.**       
fb80: 20 20 20 5a 2a 4e 20 62 79 74 65 73 20 69 6e 20     Z*N bytes in 
fb90: 73 69 7a 65 20 73 68 61 6c 6c 20 63 61 75 73 65  size shall cause
fba0: 20 53 20 74 6f 20 62 65 20 75 73 65 64 20 62 79   S to be used by
fbb0: 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
fbc0: 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79   [scratch memory
fbd0: 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f 72 20   allocator] for 
fbe0: 61 73 20 6d 61 6e 79 20 61 73 20 4e 20 73 69 6d  as many as N sim
fbf0: 75 6c 61 74 61 6e 65 6f 75 73 0a 2a 2a 20 20 20  ulataneous.**   
fc00: 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f         allocatio
fc10: 6e 73 20 65 61 63 68 20 6f 66 20 73 69 7a 65 20  ns each of size 
fc20: 28 5a 20 26 20 7e 37 29 2e 0a 2a 2a 0a 2a 2a 20  (Z & ~7)..**.** 
fc30: 7b 48 31 34 31 35 33 7d 20 41 20 73 75 63 63 65  {H14153} A succe
fc40: 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
fc50: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b  qlite3_config]([
fc60: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43  SQLITE_CONFIG_SC
fc70: 52 41 54 43 48 5d 2c 53 2c 5a 2c 4e 29 0a 2a 2a  RATCH],S,Z,N).**
fc80: 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65 20            where 
fc90: 53 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  S is a NULL poin
fca0: 74 65 72 20 73 68 61 6c 6c 20 64 69 73 61 62 6c  ter shall disabl
fcb0: 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  e the.**        
fcc0: 20 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72    [scratch memor
fcd0: 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 0a 2a 2a  y allocator]..**
fce0: 0a 2a 2a 20 7b 48 31 34 31 35 36 7d 20 41 20 73  .** {H14156} A s
fcf0: 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
fd00: 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  o.**          [s
fd10: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b  qlite3_config]([
fd20: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
fd30: 47 45 43 41 43 48 45 5d 2c 53 2c 5a 2c 4e 29 0a  GECACHE],S,Z,N).
fd40: 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 72  **          wher
fd50: 65 20 5a 20 61 6e 64 20 4e 20 61 72 65 20 6e 6f  e Z and N are no
fd60: 6e 2d 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67  n-negative integ
fd70: 65 72 73 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20  ers and .**     
fd80: 20 20 20 20 20 53 20 69 73 20 61 20 70 6f 69 6e       S is a poin
fd90: 74 65 72 20 74 6f 20 61 6e 20 61 6c 69 67 6e 65  ter to an aligne
fda0: 64 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  d memory buffer 
fdb0: 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a  not less than.**
fdc0: 20 20 20 20 20 20 20 20 20 20 5a 2a 4e 20 62 79            Z*N by
fdd0: 74 65 73 20 69 6e 20 73 69 7a 65 20 73 68 61 6c  tes in size shal
fde0: 6c 20 63 61 75 73 65 20 53 20 74 6f 20 62 65 20  l cause S to be 
fdf0: 75 73 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 20  used by the.**  
fe00: 20 20 20 20 20 20 20 20 5b 70 61 67 65 63 61 63          [pagecac
fe10: 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
fe20: 74 6f 72 5d 20 66 6f 72 20 61 73 20 6d 61 6e 79  tor] for as many
fe30: 20 61 73 20 4e 20 73 69 6d 75 6c 61 74 61 6e 65   as N simulatane
fe40: 6f 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ous.**          
fe50: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 65 61 63 68  allocations each
fe60: 20 6f 66 20 73 69 7a 65 20 28 5a 20 26 20 7e 37   of size (Z & ~7
fe70: 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 39  )..**.** {H14159
fe80: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
fe90: 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  all to.**       
fea0: 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66     [sqlite3_conf
feb0: 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  ig]([SQLITE_CONF
fec0: 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2c 53 2c  IG_PAGECACHE],S,
fed0: 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  Z,N).**         
fee0: 20 77 68 65 72 65 20 53 20 69 73 20 61 20 4e 55   where S is a NU
fef0: 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c  LL pointer shall
ff00: 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20   disable the.** 
ff10: 20 20 20 20 20 20 20 20 20 5b 70 61 67 65 63 61           [pageca
ff20: 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  che memory alloc
ff30: 61 74 6f 72 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ator]..**.** {H1
ff40: 34 31 36 32 7d 20 41 20 73 75 63 63 65 73 73 66  4162} A successf
ff50: 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ul call to [sqli
ff60: 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c  te3_config]([SQL
ff70: 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 5d  ITE_CONFIG_HEAP]
ff80: 2c 48 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20  ,H,Z,N).**      
ff90: 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e 64 20      where Z and 
ffa0: 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 69  N are non-negati
ffb0: 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20  ve integers and 
ffc0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 48 20 69  .**          H i
ffd0: 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
ffe0: 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79  n aligned memory
fff0: 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65 73 73   buffer not less
10000 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20   than.**        
10010 20 20 5a 20 62 79 74 65 73 20 69 6e 20 73 69 7a    Z bytes in siz
10020 65 20 73 68 61 6c 6c 20 65 6e 61 62 6c 65 20 74  e shall enable t
10030 68 65 20 5b 6d 65 6d 73 79 73 35 5d 20 6d 65 6d  he [memsys5] mem
10040 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a  ory allocator.**
10050 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 63 61            and ca
10060 75 73 65 20 69 74 20 74 6f 20 75 73 65 20 62 75  use it to use bu
10070 66 66 65 72 20 53 20 61 73 20 69 74 73 20 6d 65  ffer S as its me
10080 6d 6f 72 79 20 73 6f 75 72 63 65 20 61 6e 64 20  mory source and 
10090 74 6f 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20  to use.**       
100a0 20 20 20 61 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c     a minimum all
100b0 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 6f 66 20  ocation size of 
100c0 4e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 36 35  N..**.** {H14165
100d0 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
100e0 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
100f0 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f  config]([SQLITE_
10100 43 4f 4e 46 49 47 5f 48 45 41 50 5d 2c 48 2c 5a  CONFIG_HEAP],H,Z
10110 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ,N).**          
10120 77 68 65 72 65 20 48 20 69 73 20 61 20 4e 55 4c  where H is a NUL
10130 4c 20 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20  L pointer shall 
10140 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 20  disable the.**  
10150 20 20 20 20 20 20 20 20 5b 6d 65 6d 73 79 73 35          [memsys5
10160 5d 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  ] memory allocat
10170 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 36  or..**.** {H1416
10180 38 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  8} A successful 
10190 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
101a0 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
101b0 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  _CONFIG_LOOKASID
101c0 45 5d 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20  E],Z,N).**      
101d0 20 20 20 20 73 68 61 6c 6c 20 63 61 75 73 65 20      shall cause 
101e0 74 68 65 20 64 65 66 61 75 6c 74 20 5b 6c 6f 6f  the default [loo
101f0 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c  kaside memory al
10200 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75  locator] configu
10210 72 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ration.**       
10220 20 20 20 66 6f 72 20 6e 65 77 20 5b 64 61 74 61     for new [data
10230 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
10240 5d 20 74 6f 20 62 65 20 4e 20 73 6c 6f 74 73 20  ] to be N slots 
10250 6f 66 20 5a 20 62 79 74 65 73 20 65 61 63 68 2e  of Z bytes each.
10260 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
10270 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
10280 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  AL int sqlite3_c
10290 6f 6e 66 69 67 28 69 6e 74 2c 20 2e 2e 2e 29 3b  onfig(int, ...);
102a0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
102b0 3a 20 43 6f 6e 66 69 67 75 72 65 20 64 61 74 61  : Configure data
102c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
102d0 20 20 7b 48 31 34 32 30 30 7d 20 3c 53 32 30 30    {H14200} <S200
102e0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
102f0 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  TAL.**.** The sq
10300 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28  lite3_db_config(
10310 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75  ) interface is u
10320 73 65 64 20 74 6f 20 6d 61 6b 65 20 63 6f 6e 66  sed to make conf
10330 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61  iguration.** cha
10340 6e 67 65 73 20 74 6f 20 61 20 5b 64 61 74 61 62  nges to a [datab
10350 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
10360 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20    The interface 
10370 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 0a 2a 2a  is similar to.**
10380 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67   [sqlite3_config
10390 28 29 5d 20 65 78 63 65 70 74 20 74 68 61 74 20  ()] except that 
103a0 74 68 65 20 63 68 61 6e 67 65 73 20 61 70 70 6c  the changes appl
103b0 79 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  y to a single.**
103c0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
103d0 63 74 69 6f 6e 5d 20 28 73 70 65 63 69 66 69 65  ction] (specifie
103e0 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  d in the first a
103f0 72 67 75 6d 65 6e 74 29 2e 20 20 54 68 65 0a 2a  rgument).  The.*
10400 2a 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e  * sqlite3_db_con
10410 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  fig() interface 
10420 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  can only be used
10430 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74   immediately aft
10440 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  er.** the databa
10450 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
10460 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b   created using [
10470 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c  sqlite3_open()],
10480 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  .** [sqlite3_ope
10490 6e 31 36 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69  n16()], or [sqli
104a0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20  te3_open_v2()]. 
104b0 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f   .**.** The seco
104c0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  nd argument to s
104d0 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
104e0 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 73 20 74 68  (D,V,...)  is th
104f0 65 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61 74 69  e.** configurati
10500 6f 6e 20 76 65 72 62 20 2d 20 61 6e 20 69 6e 74  on verb - an int
10510 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 20 69  eger code that i
10520 6e 64 69 63 61 74 65 73 20 77 68 61 74 0a 2a 2a  ndicates what.**
10530 20 61 73 70 65 63 74 20 6f 66 20 74 68 65 20 5b   aspect of the [
10540 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
10550 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 63 6f  ion] is being co
10560 6e 66 69 67 75 72 65 64 2e 0a 2a 2a 20 54 68 65  nfigured..** The
10570 20 6f 6e 6c 79 20 63 68 6f 69 63 65 20 66 6f 72   only choice for
10580 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 5b   this value is [
10590 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f  SQLITE_DBCONFIG_
105a0 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a 2a 2a 20 4e  LOOKASIDE]..** N
105b0 65 77 20 76 65 72 62 73 20 61 72 65 20 6c 69 6b  ew verbs are lik
105c0 65 6c 79 20 74 6f 20 62 65 20 61 64 64 65 64 20  ely to be added 
105d0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
105e0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a  es of SQLite..**
105f0 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72 67 75   Additional argu
10600 6d 65 6e 74 73 20 64 65 70 65 6e 64 20 6f 6e 20  ments depend on 
10610 74 68 65 20 76 65 72 62 2e 0a 2a 2a 0a 2a 2a 20  the verb..**.** 
10620 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
10630 2a 20 7b 48 31 34 32 30 33 7d 20 41 20 63 61 6c  * {H14203} A cal
10640 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 64 62  l to [sqlite3_db
10650 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29  _config(D,V,...)
10660 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 5b  ] shall return [
10670 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 20 20  SQLITE_OK].**   
10680 20 20 20 20 20 20 20 69 66 20 61 6e 64 20 6f 6e         if and on
10690 6c 79 20 69 66 20 74 68 65 20 63 61 6c 6c 20 69  ly if the call i
106a0 73 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a  s successful..**
106b0 0a 2a 2a 20 7b 48 31 34 32 30 36 7d 20 49 66 20  .** {H14206} If 
106c0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 6c 6f 74  one or more slot
106d0 73 20 6f 66 20 74 68 65 20 5b 6c 6f 6f 6b 61 73  s of the [lookas
106e0 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ide memory alloc
106f0 61 74 6f 72 5d 20 66 6f 72 0a 2a 2a 20 20 20 20  ator] for.**    
10700 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20        [database 
10710 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 61 72  connection] D ar
10720 65 20 69 6e 20 75 73 65 2c 20 74 68 65 6e 20 61  e in use, then a
10730 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20   call to.**     
10740 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 62       [sqlite3_db
10750 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49  _config](D,[SQLI
10760 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  TE_DBCONFIG_LOOK
10770 41 53 49 44 45 5d 2c 2e 2e 2e 29 20 73 68 61 6c  ASIDE],...) shal
10780 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61  l.**          fa
10790 69 6c 20 77 69 74 68 20 61 6e 20 5b 53 51 4c 49  il with an [SQLI
107a0 54 45 5f 42 55 53 59 5d 20 72 65 74 75 72 6e 20  TE_BUSY] return 
107b0 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34  code..**.** {H14
107c0 32 30 39 7d 20 41 20 73 75 63 63 65 73 73 66 75  209} A successfu
107d0 6c 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20  l call to .**   
107e0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
107f0 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51  db_config](D,[SQ
10800 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f  LITE_DBCONFIG_LO
10810 4f 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20  OKASIDE],B,Z,N) 
10820 77 68 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 20  where.**        
10830 20 20 44 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b    D is an open [
10840 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
10850 69 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e  ion] and Z and N
10860 20 61 72 65 20 70 6f 73 69 74 69 76 65 0a 2a 2a   are positive.**
10870 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 67 65            intege
10880 72 73 20 61 6e 64 20 42 20 69 73 20 61 6e 20 61  rs and B is an a
10890 6c 69 67 6e 65 64 20 62 75 66 66 65 72 20 61 74  ligned buffer at
108a0 20 6c 65 61 73 74 20 5a 2a 4e 20 62 79 74 65 73   least Z*N bytes
108b0 20 69 6e 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20   in size.**     
108c0 20 20 20 20 20 73 68 61 6c 6c 20 63 61 75 73 65       shall cause
108d0 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 69 64 65 20   the [lookaside 
108e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
108f0 5d 20 66 6f 72 20 44 20 74 6f 20 75 73 65 20 62  ] for D to use b
10900 75 66 66 65 72 20 42 20 0a 2a 2a 20 20 20 20 20  uffer B .**     
10910 20 20 20 20 20 77 69 74 68 20 4e 20 73 6c 6f 74       with N slot
10920 73 20 6f 66 20 5a 20 62 79 74 65 73 20 65 61 63  s of Z bytes eac
10930 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32 31 32  h..**.** {H14212
10940 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
10950 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20 20 20 20  all to .**      
10960 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f      [sqlite3_db_
10970 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49 54  config](D,[SQLIT
10980 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  E_DBCONFIG_LOOKA
10990 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77 68 65  SIDE],B,Z,N) whe
109a0 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 44  re.**          D
109b0 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b 64 61 74   is an open [dat
109c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
109d0 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e 20 61 72  ] and Z and N ar
109e0 65 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 20 20  e positive.**   
109f0 20 20 20 20 20 20 20 69 6e 74 65 67 65 72 73 20         integers 
10a00 61 6e 64 20 42 20 69 73 20 4e 55 4c 4c 20 70 6f  and B is NULL po
10a10 69 6e 74 65 72 20 73 68 61 6c 6c 20 63 61 75 73  inter shall caus
10a20 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  e the.**        
10a30 20 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d    [lookaside mem
10a40 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66  ory allocator] f
10a50 6f 72 20 44 20 74 6f 20 61 20 6f 62 74 61 69 6e  or D to a obtain
10a60 20 5a 2a 4e 20 62 79 74 65 20 62 75 66 66 65 72   Z*N byte buffer
10a70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f  .**          fro
10a80 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 6d 65  m the primary me
10a90 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 61  mory allocator a
10aa0 6e 64 20 75 73 65 20 74 68 61 74 20 62 75 66 66  nd use that buff
10ab0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  er.**          w
10ac0 69 74 68 20 4e 20 6c 6f 6f 6b 61 73 69 64 65 20  ith N lookaside 
10ad0 73 6c 6f 74 73 20 6f 66 20 5a 20 62 79 74 65 73  slots of Z bytes
10ae0 20 65 61 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31   each..**.** {H1
10af0 34 32 31 35 7d 20 41 20 73 75 63 63 65 73 73 66  4215} A successf
10b00 75 6c 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20  ul call to .**  
10b10 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
10b20 5f 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53  _db_config](D,[S
10b30 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c  QLITE_DBCONFIG_L
10b40 4f 4f 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29  OOKASIDE],B,Z,N)
10b50 20 77 68 65 72 65 0a 2a 2a 20 20 20 20 20 20 20   where.**       
10b60 20 20 20 44 20 69 73 20 61 6e 20 6f 70 65 6e 20     D is an open 
10b70 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
10b80 74 69 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20  tion] and Z and 
10b90 4e 20 61 72 65 20 7a 65 72 6f 20 73 68 61 6c 6c  N are zero shall
10ba0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 69 73  .**          dis
10bb0 61 62 6c 65 20 74 68 65 20 5b 6c 6f 6f 6b 61 73  able the [lookas
10bc0 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ide memory alloc
10bd0 61 74 6f 72 5d 20 66 6f 72 20 44 2e 0a 2a 2a 0a  ator] for D..**.
10be0 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  **.*/.SQLITE_API
10bf0 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45   SQLITE_EXPERIME
10c00 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33  NTAL int sqlite3
10c10 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74  _db_config(sqlit
10c20 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e  e3*, int op, ...
10c30 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
10c40 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63  EF: Memory Alloc
10c50 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b  ation Routines {
10c60 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30 3e  H10155} <S20120>
10c70 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
10c80 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  .**.** An instan
10c90 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
10ca0 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e  t defines the in
10cb0 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 20  terface between 
10cc0 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f  SQLite.** and lo
10cd0 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
10ce0 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e  llocation routin
10cf0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  es..**.** This o
10d00 62 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 6e  bject is used in
10d10 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20   only one place 
10d20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e  in the SQLite in
10d30 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f  terface..** A po
10d40 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74  inter to an inst
10d50 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
10d60 65 63 74 20 69 73 20 74 68 65 20 61 72 67 75 6d  ect is the argum
10d70 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74  ent to.** [sqlit
10d80 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65  e3_config()] whe
10d90 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74  n the configurat
10da0 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a  ion option is.**
10db0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
10dc0 4d 41 4c 4c 4f 43 5d 2e 20 20 42 79 20 63 72 65  MALLOC].  By cre
10dd0 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63  ating an instanc
10de0 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
10df0 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20  .** and passing 
10e00 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  it to [sqlite3_c
10e10 6f 6e 66 69 67 28 29 5d 20 64 75 72 69 6e 67 20  onfig()] during 
10e20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 61  configuration, a
10e30 6e 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  n.** application
10e40 20 63 61 6e 20 73 70 65 63 69 66 79 20 61 6e 20   can specify an 
10e50 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f  alternative memo
10e60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
10e70 62 73 79 73 74 65 6d 0a 2a 2a 20 66 6f 72 20 53  bsystem.** for S
10e80 51 4c 69 74 65 20 74 6f 20 75 73 65 20 66 6f 72  QLite to use for
10e90 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61   all of its dyna
10ea0 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73  mic memory needs
10eb0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
10ec0 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73 20 77  t SQLite comes w
10ed0 69 74 68 20 61 20 62 75 69 6c 74 2d 69 6e 20 6d  ith a built-in m
10ee0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
10ef0 74 68 61 74 20 69 73 0a 2a 2a 20 70 65 72 66 65  that is.** perfe
10f00 63 74 6c 79 20 61 64 65 71 75 61 74 65 20 66 6f  ctly adequate fo
10f10 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c 6d 69  r the overwhelmi
10f20 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 61  ng majority of a
10f30 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61  pplications.** a
10f40 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f 62 6a  nd that this obj
10f50 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73 65 66  ect is only usef
10f60 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d 69 6e  ul to a tiny min
10f70 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 63 61  ority of applica
10f80 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 73 70  tions.** with sp
10f90 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f 72 79  ecialized memory
10fa0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75   allocation requ
10fb0 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 20  irements.  This 
10fc0 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61 6c 73  object is.** als
10fd0 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65  o used during te
10fe0 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74 65 20  sting of SQLite 
10ff0 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65 63  in order to spec
11000 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69  ify an alternati
11010 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c  ve.** memory all
11020 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69 6d 75  ocator that simu
11030 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f 75 74  lates memory out
11040 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69  -of-memory condi
11050 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 64 65  tions in.** orde
11060 72 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74  r to verify that
11070 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65 72 73   SQLite recovers
11080 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72 6f 6d   gracefully from
11090 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 74 69   such.** conditi
110a0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ons..**.** The x
110b0 4d 61 6c 6c 6f 63 2c 20 78 46 72 65 65 2c 20 61  Malloc, xFree, a
110c0 6e 64 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74 68  nd xRealloc meth
110d0 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69  ods must work li
110e0 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c 6f 63  ke the.** malloc
110f0 28 29 2c 20 66 72 65 65 28 29 2c 20 61 6e 64 20  (), free(), and 
11100 72 65 61 6c 6c 6f 63 28 29 20 66 75 6e 63 74 69  realloc() functi
11110 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ons from the sta
11120 6e 64 61 72 64 20 6c 69 62 72 61 72 79 2e 0a 2a  ndard library..*
11130 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f 75 6c  *.** xSize shoul
11140 64 20 72 65 74 75 72 6e 20 74 68 65 20 61 6c 6c  d return the all
11150 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61  ocated size of a
11160 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
11170 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  on.** previously
11180 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 78   obtained from x
11190 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61 6c 6c  Malloc or xReall
111a0 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74  oc.  The allocat
111b0 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20 61 6c  ed size.** is al
111c0 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 61 73  ways at least as
111d0 20 62 69 67 20 61 73 20 74 68 65 20 72 65 71 75   big as the requ
111e0 65 73 74 65 64 20 73 69 7a 65 20 62 75 74 20 6d  ested size but m
111f0 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a 2a 2a  ay be larger..**
11200 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64 75 70  .** The xRoundup
11210 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
11220 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20 74 68  what would be th
11230 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65  e allocated size
11240 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 79 20   of.** a memory 
11250 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76 65 6e  allocation given
11260 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65   a particular re
11270 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20 20 4d  quested size.  M
11280 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  ost memory.** al
11290 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75  locators round u
112a0 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  p memory allocat
112b0 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20 74 6f  ions at least to
112c0 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74 69 70   the next multip
112d0 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 6f 6d  le.** of 8.  Som
112e0 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75  e allocators rou
112f0 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72 67 65  nd up to a large
11300 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20 74 6f  r multiple or to
11310 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a   a power of 2..*
11320 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 20 6d  *.** The xInit m
11330 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69 7a 65  ethod initialize
11340 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  s the memory all
11350 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20 65 78  ocator.  (For ex
11360 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d 69 67  ample,.** it mig
11370 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20  ht allocate any 
11380 72 65 71 75 69 72 65 20 6d 75 74 65 78 65 73 20  require mutexes 
11390 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e  or initialize in
113a0 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 73  ternal data.** s
113b0 74 72 75 63 74 75 72 65 73 2e 20 20 54 68 65 20  tructures.  The 
113c0 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64  xShutdown method
113d0 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 6e 64   is invoked (ind
113e0 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a 20 5b  irectly) by.** [
113f0 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
11400 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64 20 64  ()] and should d
11410 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72 65  eallocate any re
11420 73 6f 75 72 63 65 73 20 61 63 71 75 69 72 65 64  sources acquired
11430 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20 20 54  .** by xInit.  T
11440 68 65 20 70 41 70 70 44 61 74 61 20 70 6f 69 6e  he pAppData poin
11450 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20 74  ter is used as t
11460 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65 74 65  he only paramete
11470 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20 61 6e  r to.** xInit an
11480 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a 2f 0a  d xShutdown..*/.
11490 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
114a0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
114b0 64 73 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  ds sqlite3_mem_m
114c0 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73  ethods;.struct s
114d0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
114e0 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 28 2a 78  ds {.  void *(*x
114f0 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 20 20  Malloc)(int);   
11500 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
11510 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74  allocation funct
11520 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ion */.  void (*
11530 78 46 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 20  xFree)(void*);  
11540 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20          /* Free 
11550 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69  a prior allocati
11560 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 2a  on */.  void *(*
11570 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c  xRealloc)(void*,
11580 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73 69 7a 65  int);  /* Resize
11590 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a   an allocation *
115a0 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69 7a 65 29  /.  int (*xSize)
115b0 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20  (void*);        
115c0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65     /* Return the
115d0 20 73 69 7a 65 20 6f 66 20 61 6e 20 61 6c 6c 6f   size of an allo
115e0 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  cation */.  int 
115f0 28 2a 78 52 6f 75 6e 64 75 70 29 28 69 6e 74 29  (*xRoundup)(int)
11600 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  ;          /* Ro
11610 75 6e 64 20 75 70 20 72 65 71 75 65 73 74 20 73  und up request s
11620 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61 74 69 6f  ize to allocatio
11630 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20  n size */.  int 
11640 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b  (*xInit)(void*);
11650 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
11660 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
11670 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f  ory allocator */
11680 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64  .  void (*xShutd
11690 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20 20 20 20  own)(void*);    
116a0 20 20 2f 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a    /* Deinitializ
116b0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  e the memory all
116c0 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64  ocator */.  void
116d0 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20 20   *pAppData;     
116e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
116f0 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e 69 74 28  gument to xInit(
11700 29 20 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 28  ) and xShutdown(
11710 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ) */.};../*.** C
11720 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75  API3REF: Configu
11730 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b  ration Options {
11740 48 31 30 31 36 30 7d 20 3c 53 32 30 30 30 30 3e  H10160} <S20000>
11750 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
11760 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  .**.** These con
11770 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 61  stants are the a
11780 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72  vailable integer
11790 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
117a0 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63  ptions that.** c
117b0 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 73 20  an be passed as 
117c0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
117d0 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  nt to the [sqlit
117e0 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74  e3_config()] int
117f0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65  erface..**.** Ne
11800 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  w configuration 
11810 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61  options may be a
11820 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72  dded in future r
11830 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74  eleases of SQLit
11840 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63  e..** Existing c
11850 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
11860 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69  ions might be di
11870 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70  scontinued.  App
11880 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f  lications.** sho
11890 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 65  uld check the re
118a0 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b  turn code from [
118b0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
118c0 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  ] to make sure t
118d0 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20  hat.** the call 
118e0 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71  worked.  The [sq
118f0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20  lite3_config()] 
11900 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72  interface will r
11910 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a  eturn a.** non-z
11920 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d  ero [error code]
11930 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75   if a discontinu
11940 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65  ed or unsupporte
11950 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  d configuration 
11960 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76  option.** is inv
11970 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  oked..**.** <dl>
11980 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
11990 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
119a0 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  AD</dt>.** <dd>T
119b0 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75  here are no argu
119c0 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70  ments to this op
119d0 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69  tion.  This opti
119e0 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 61  on disables.** a
119f0 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e 64 20  ll mutexing and 
11a00 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e 74 6f  puts SQLite into
11a10 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20 69 74   a mode where it
11a20 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65   can only be use
11a30 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67 6c 65  d.** by a single
11a40 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e 0a 2a 2a   thread.</dd>.**
11a50 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
11a60 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41  ONFIG_MULTITHREA
11a70 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
11a80 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d  ere are no argum
11a90 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74  ents to this opt
11aa0 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f  ion.  This optio
11ab0 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 6d 75  n disables.** mu
11ac0 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61 74 61 62  texing on [datab
11ad0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
11ae0 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74  and [prepared st
11af0 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73  atement] objects
11b00 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61  ..** The applica
11b10 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69  tion is responsi
11b20 62 6c 65 20 66 6f 72 20 73 65 72 69 61 6c 69 7a  ble for serializ
11b30 69 6e 67 20 61 63 63 65 73 73 20 74 6f 0a 2a 2a  ing access to.**
11b40 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
11b50 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65  ctions] and [pre
11b60 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
11b70 5d 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d 75  ].  But other mu
11b80 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61  texes.** are ena
11b90 62 6c 65 64 20 73 6f 20 74 68 61 74 20 53 51 4c  bled so that SQL
11ba0 69 74 65 20 77 69 6c 6c 20 62 65 20 73 61 66 65  ite will be safe
11bb0 20 74 6f 20 75 73 65 20 69 6e 20 61 20 6d 75 6c   to use in a mul
11bc0 74 69 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 65  ti-threaded.** e
11bd0 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f  nvironment as lo
11be0 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20 74 68 72  ng as no two thr
11bf0 65 61 64 73 20 61 74 74 65 6d 70 74 20 74 6f 20  eads attempt to 
11c00 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20  use the same.** 
11c10 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
11c20 74 69 6f 6e 5d 20 61 74 20 74 68 65 20 73 61 6d  tion] at the sam
11c30 65 20 74 69 6d 65 2e 20 20 53 65 65 20 74 68 65  e time.  See the
11c40 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
11c50 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69  ].** documentati
11c60 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  on for additiona
11c70 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f  l information.</
11c80 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
11c90 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49  LITE_CONFIG_SERI
11ca0 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c  ALIZED</dt>.** <
11cb0 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20  dd>There are no 
11cc0 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69  arguments to thi
11cd0 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20  s option.  This 
11ce0 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65 73 0a 2a  option enables.*
11cf0 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73 20 69 6e  * all mutexes in
11d00 63 6c 75 64 69 6e 67 20 74 68 65 20 72 65 63 75  cluding the recu
11d10 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65 78 65 73  rsive.** mutexes
11d20 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f   on [database co
11d30 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70  nnection] and [p
11d40 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
11d50 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 49  t] objects..** I
11d60 6e 20 74 68 69 73 20 6d 6f 64 65 20 28 77 68 69  n this mode (whi
11d70 63 68 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  ch is the defaul
11d80 74 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73  t when SQLite is
11d90 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a   compiled with.*
11da0 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44  * [SQLITE_THREAD
11db0 53 41 46 45 3d 31 5d 29 20 74 68 65 20 53 51 4c  SAFE=1]) the SQL
11dc0 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c  ite library will
11dd0 20 69 74 73 65 6c 66 20 73 65 72 69 61 6c 69 7a   itself serializ
11de0 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 5b  e access.** to [
11df0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
11e00 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61  ions] and [prepa
11e10 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20  red statements] 
11e20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61  so that the.** a
11e30 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20 66 72  pplication is fr
11e40 65 65 20 74 6f 20 75 73 65 20 74 68 65 20 73 61  ee to use the sa
11e50 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  me [database con
11e60 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74 68 65 0a  nection] or the.
11e70 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65  ** same [prepare
11e80 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20  d statement] in 
11e90 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
11ea0 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  s at the same ti
11eb0 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b  me..** See the [
11ec0 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20  threading mode] 
11ed0 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  documentation fo
11ee0 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
11ef0 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a  ormation.</dd>.*
11f00 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
11f10 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64  CONFIG_MALLOC</d
11f20 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
11f30 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ption takes a si
11f40 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68  ngle argument wh
11f50 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ich is a pointer
11f60 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   to an.** instan
11f70 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ce of the [sqlit
11f80 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20  e3_mem_methods] 
11f90 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
11fa0 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69  argument specifi
11fb0 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76  es.** alternativ
11fc0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  e low-level memo
11fd0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f  ry allocation ro
11fe0 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65  utines to be use
11ff0 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a  d in place of.**
12000 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
12010 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20  cation routines 
12020 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74  built into SQLit
12030 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  e.</dd>.**.** <d
12040 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
12050 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a  GETMALLOC</dt>.*
12060 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
12070 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65  n takes a single
12080 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
12090 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
120a0 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
120b0 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  f the [sqlite3_m
120c0 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75  em_methods] stru
120d0 63 74 75 72 65 2e 20 20 54 68 65 20 5b 73 71 6c  cture.  The [sql
120e0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
120f0 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69  ].** structure i
12100 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68  s filled with th
12110 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69  e currently defi
12120 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ned memory alloc
12130 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a  ation routines..
12140 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63  ** This option c
12150 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76  an be used to ov
12160 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75  erload the defau
12170 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  lt memory alloca
12180 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  tion.** routines
12190 20 77 69 74 68 20 61 20 77 72 61 70 70 65 72 20   with a wrapper 
121a0 74 68 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e 73  that simulations
121b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
121c0 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a 2a  on failure or.**
121d0 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 75   tracks memory u
121e0 73 61 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c  sage, for exampl
121f0 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  e.</dd>.**.** <d
12200 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
12210 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74 3e 0a 2a  MEMSTATUS</dt>.*
12220 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
12230 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 61  n takes single a
12240 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20  rgument of type 
12250 69 6e 74 2c 20 69 6e 74 65 72 70 72 65 74 65 64  int, interpreted
12260 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61   as a .** boolea
12270 6e 2c 20 77 68 69 63 68 20 65 6e 61 62 6c 65 73  n, which enables
12280 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65   or disables the
12290 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 6d   collection of m
122a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
122b0 20 0a 2a 2a 20 73 74 61 74 69 73 74 69 63 73 2e   .** statistics.
122c0 20 57 68 65 6e 20 64 69 73 61 62 6c 65 64 2c 20   When disabled, 
122d0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51  the following SQ
122e0 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20  Lite interfaces 
122f0 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d 6f  become .** non-o
12300 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 20  perational:.**  
12310 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20   <ul>.**   <li> 
12320 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f  [sqlite3_memory_
12330 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69  used()].**   <li
12340 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  > [sqlite3_memor
12350 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d 0a 2a  y_highwater()].*
12360 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65  *   <li> [sqlite
12370 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
12380 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b  t()].**   <li> [
12390 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29  sqlite3_status()
123a0 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a 2a 20  ].**   </ul>.** 
123b0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
123c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43  SQLITE_CONFIG_SC
123d0 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  RATCH</dt>.** <d
123e0 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70  d>This option sp
123f0 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69 63  ecifies a static
12400 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74   memory buffer t
12410 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75  hat SQLite can u
12420 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 63  se for.** scratc
12430 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 65  h memory.  There
12440 20 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d   are three argum
12450 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 72  ents:  A pointer
12460 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20   to the memory, 
12470 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65  the.** size of e
12480 61 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66  ach scratch buff
12490 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 74 68 65  er (sz), and the
124a0 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 66 66 65   number of buffe
124b0 72 73 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0a  rs (N).  The sz.
124c0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74  ** argument must
124d0 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f   be a multiple o
124e0 66 20 31 36 2e 20 54 68 65 20 73 7a 20 70 61 72  f 16. The sz par
124f0 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65  ameter should be
12500 20 61 20 66 65 77 20 62 79 74 65 73 0a 2a 2a 20   a few bytes.** 
12510 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
12520 61 63 74 75 61 6c 20 73 63 72 61 74 63 68 20 73  actual scratch s
12530 70 61 63 65 20 72 65 71 75 69 72 65 64 20 64 75  pace required du
12540 65 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68  e internal overh
12550 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73  ead..** The firs
12560 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 73 68  t.** argument sh
12570 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 61 6e  ould point to an
12580 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61   allocation of a
12590 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 74  t least sz*N byt
125a0 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  es of memory..**
125b0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65   SQLite will use
125c0 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e   no more than on
125d0 65 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72  e scratch buffer
125e0 20 61 74 20 6f 6e 63 65 20 70 65 72 20 74 68 72   at once per thr
125f0 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 68 6f  ead, so.** N sho
12600 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 74 68  uld be set to th
12610 65 20 65 78 70 65 63 74 65 64 20 6d 61 78 69 6d  e expected maxim
12620 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72  um number of thr
12630 65 61 64 73 2e 20 20 54 68 65 20 73 7a 0a 2a 2a  eads.  The sz.**
12640 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c   parameter shoul
12650 64 20 62 65 20 36 20 74 69 6d 65 73 20 74 68 65  d be 6 times the
12660 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72   size of the lar
12670 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70 61  gest database pa
12680 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 72 61  ge size..** Scra
12690 74 63 68 20 62 75 66 66 65 72 73 20 61 72 65 20  tch buffers are 
126a0 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
126b0 74 68 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63  the btree balanc
126c0 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66  e operation.  If
126d0 0a 2a 2a 20 54 68 65 20 62 74 72 65 65 20 62 61  .** The btree ba
126e0 6c 61 6e 63 65 72 20 6e 65 65 64 73 20 61 64 64  lancer needs add
126f0 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 62  itional memory b
12700 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70 72  eyond what is pr
12710 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 73 63 72  ovided by.** scr
12720 61 74 63 68 20 62 75 66 66 65 72 73 20 6f 72 20  atch buffers or 
12730 69 66 20 6e 6f 20 73 63 72 61 74 63 68 20 62 75  if no scratch bu
12740 66 66 65 72 20 73 70 61 63 65 20 69 73 20 73 70  ffer space is sp
12750 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 53 51  ecified, then SQ
12760 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20 74 6f 20  Lite.** goes to 
12770 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
12780 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  )] to obtain the
12790 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73   memory it needs
127a0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
127b0 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  >SQLITE_CONFIG_P
127c0 41 47 45 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a  AGECACHE</dt>.**
127d0 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
127e0 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61   specifies a sta
127f0 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65  tic memory buffe
12800 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61  r that SQLite ca
12810 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65  n use for.** the
12820 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63   database page c
12830 61 63 68 65 20 77 69 74 68 20 74 68 65 20 64 65  ache with the de
12840 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
12850 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 2e 20   implemenation. 
12860 20 0a 2a 2a 20 54 68 69 73 20 63 6f 6e 66 69 67   .** This config
12870 75 72 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e  uration should n
12880 6f 74 20 62 65 20 75 73 65 64 20 69 66 20 61 6e  ot be used if an
12890 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
128a0 69 6e 65 20 70 61 67 65 0a 2a 2a 20 63 61 63 68  ine page.** cach
128b0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
128c0 20 69 73 20 6c 6f 61 64 65 64 20 75 73 69 6e 67   is loaded using
128d0 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46   the SQLITE_CONF
128e0 49 47 5f 50 43 41 43 48 45 20 6f 70 74 69 6f 6e  IG_PCACHE option
128f0 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74  ..** There are t
12900 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74  hree arguments t
12910 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 3a 20 41  o this option: A
12920 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a   pointer to the.
12930 2a 2a 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 73  ** memory, the s
12940 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 67 65  ize of each page
12950 20 62 75 66 66 65 72 20 28 73 7a 29 2c 20 61 6e   buffer (sz), an
12960 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  d the number of 
12970 70 61 67 65 73 20 28 4e 29 2e 0a 2a 2a 20 54 68  pages (N)..** Th
12980 65 20 73 7a 20 61 72 67 75 6d 65 6e 74 20 6d 75  e sz argument mu
12990 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66  st be a power of
129a0 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32   two between 512
129b0 20 61 6e 64 20 33 32 37 36 38 2e 20 20 54 68 65   and 32768.  The
129c0 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65   first.** argume
129d0 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20  nt should point 
129e0 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  to an allocation
129f0 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a   of at least sz*
12a00 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  N bytes of memor
12a10 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  y..** SQLite wil
12a20 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79  l use the memory
12a30 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 65   provided by the
12a40 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
12a50 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a  to satisfy its.*
12a60 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66  * memory needs f
12a70 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 70  or the first N p
12a80 61 67 65 73 20 74 68 61 74 20 69 74 20 61 64 64  ages that it add
12a90 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20  s to cache.  If 
12aa0 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61  additional.** pa
12ab0 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  ge cache memory 
12ac0 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64  is needed beyond
12ad0 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 65   what is provide
12ae0 64 20 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e  d by this option
12af0 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65  , then.** SQLite
12b00 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65   goes to [sqlite
12b10 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20  3_malloc()] for 
12b20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  the additional s
12b30 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a  torage space..**
12b40 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
12b50 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e  ion might use on
12b60 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65  e or more of the
12b70 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f   N buffers to ho
12b80 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63  ld .** memory ac
12b90 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61  counting informa
12ba0 74 69 6f 6e 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a  tion. </dd>.**.*
12bb0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
12bc0 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a  FIG_HEAP</dt>.**
12bd0 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
12be0 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61   specifies a sta
12bf0 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65  tic memory buffe
12c00 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69  r that SQLite wi
12c10 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c  ll use.** for al
12c20 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63  l of its dynamic
12c30 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
12c40 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 20  on needs beyond 
12c50 74 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a 2a  those provided.*
12c60 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 45  * for by [SQLITE
12c70 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d  _CONFIG_SCRATCH]
12c80 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e   and [SQLITE_CON
12c90 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a  FIG_PAGECACHE]..
12ca0 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72  ** There are thr
12cb0 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 20  ee arguments: A 
12cc0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d  pointer to the m
12cd0 65 6d 6f 72 79 2c 20 74 68 65 20 6e 75 6d 62 65  emory, the numbe
12ce0 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e  r of.** bytes in
12cf0 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 66   the memory buff
12d00 65 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e 69  er, and the mini
12d10 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  mum allocation s
12d20 69 7a 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20  ize.  If.** the 
12d30 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74  first pointer (t
12d40 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65  he memory pointe
12d50 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  r) is NULL, then
12d60 20 53 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a   SQLite reverts.
12d70 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20  ** to using its 
12d80 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61  default memory a
12d90 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79  llocator (the sy
12da0 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d  stem malloc() im
12db0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a  plementation),.*
12dc0 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72  * undoing any pr
12dd0 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ior invocation o
12de0 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  f [SQLITE_CONFIG
12df0 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68  _MALLOC].  If th
12e00 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e  e.** memory poin
12e10 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  ter is not NULL 
12e20 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49  and either [SQLI
12e30 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
12e40 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45  3] or.** [SQLITE
12e50 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d  _ENABLE_MEMSYS5]
12e60 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68   are defined, th
12e70 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69  en the alternati
12e80 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c  ve memory.** all
12e90 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65  ocator is engage
12ea0 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20  d to handle all 
12eb0 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72  of SQLites memor
12ec0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65  y allocation nee
12ed0 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ds.</dd>.**.** <
12ee0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
12ef0 5f 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c  _MUTEX</dt>.** <
12f00 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74  dd>This option t
12f10 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72  akes a single ar
12f20 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20  gument which is 
12f30 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a  a pointer to an.
12f40 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ** instance of t
12f50 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  he [sqlite3_mute
12f60 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63  x_methods] struc
12f70 74 75 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d  ture.  The argum
12f80 65 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a  ent specifies.**
12f90 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77   alternative low
12fa0 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 72 6f 75  -level mutex rou
12fb0 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64  tines to be used
12fc0 20 69 6e 20 70 6c 61 63 65 0a 2a 2a 20 74 68 65   in place.** the
12fd0 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20   mutex routines 
12fe0 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74  built into SQLit
12ff0 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  e.</dd>.**.** <d
13000 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
13010 47 45 54 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a  GETMUTEX</dt>.**
13020 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
13030 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20   takes a single 
13040 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69  argument which i
13050 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
13060 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  n.** instance of
13070 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75   the [sqlite3_mu
13080 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72  tex_methods] str
13090 75 63 74 75 72 65 2e 20 20 54 68 65 0a 2a 2a 20  ucture.  The.** 
130a0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d  [sqlite3_mutex_m
130b0 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63  ethods].** struc
130c0 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64 20 77  ture is filled w
130d0 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 6c  ith the currentl
130e0 79 20 64 65 66 69 6e 65 64 20 6d 75 74 65 78 20  y defined mutex 
130f0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69  routines..** Thi
13100 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20  s option can be 
13110 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64  used to overload
13120 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74   the default mut
13130 65 78 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ex allocation.**
13140 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20 61   routines with a
13150 20 77 72 61 70 70 65 72 20 75 73 65 64 20 74 6f   wrapper used to
13160 20 74 72 61 63 6b 20 6d 75 74 65 78 20 75 73 61   track mutex usa
13170 67 65 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e  ge for performan
13180 63 65 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 20  ce.** profiling 
13190 6f 72 20 74 65 73 74 69 6e 67 2c 20 66 6f 72 20  or testing, for 
131a0 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a  example.</dd>.**
131b0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
131c0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c  ONFIG_LOOKASIDE<
131d0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
131e0 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 77   option takes tw
131f0 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74  o arguments that
13200 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64   determine the d
13210 65 66 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f 72 79  efault.** memory
13220 20 61 6c 6c 63 61 74 69 6f 6e 20 6c 6f 6f 6b 61   allcation looka
13230 73 69 64 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  side optimizatio
13240 6e 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72  n.  The first ar
13250 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a  gument is the.**
13260 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f   size of each lo
13270 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73  okaside buffer s
13280 6c 6f 74 20 61 6e 64 20 74 68 65 20 73 65 63 6f  lot and the seco
13290 6e 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  nd is the number
132a0 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 20 61 6c 6c   of.** slots all
132b0 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68 20 64  ocated to each d
132c0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
132d0 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  on.</dd>.**.** <
132e0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
132f0 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20  _PCACHE</dt>.** 
13300 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
13310 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
13320 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73  rgument which is
13330 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
13340 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61   an [sqlite3_pca
13350 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  che_methods] obj
13360 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63  ect.  This objec
13370 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  t specifies the 
13380 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20  interface.** to 
13390 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61  a custom page ca
133a0 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
133b0 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65  on.  SQLite make
133c0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a  s a copy of the.
133d0 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73  ** object and us
133e0 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 20 63  es it for page c
133f0 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ache memory allo
13400 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a  cations.</dd>.**
13410 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
13420 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c  ONFIG_GETPCACHE<
13430 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
13440 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
13450 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
13460 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
13470 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c  er to an.** [sql
13480 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
13490 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51  ods] object.  SQ
134a0 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74  Lite copies of t
134b0 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61  he current.** pa
134c0 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  ge cache impleme
134d0 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61  ntation into tha
134e0 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a  t object.</dd>.*
134f0 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64  *.** </dl>.*/.#d
13500 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
13510 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
13520 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23    1  /* nil */.#
13530 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
13540 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44  NFIG_MULTITHREAD
13550 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a     2  /* nil */.
13560 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
13570 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44  ONFIG_SERIALIZED
13580 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f      3  /* nil */
13590 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
135a0 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20  CONFIG_MALLOC   
135b0 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74       4  /* sqlit
135c0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20  e3_mem_methods* 
135d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
135e0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c  E_CONFIG_GETMALL
135f0 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c  OC     5  /* sql
13600 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
13610 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  * */.#define SQL
13620 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
13630 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76  CH       6  /* v
13640 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e  oid*, int sz, in
13650 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  t N */.#define S
13660 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
13670 45 43 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a  ECACHE     7  /*
13680 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20   void*, int sz, 
13690 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65  int N */.#define
136a0 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48   SQLITE_CONFIG_H
136b0 45 41 50 20 20 20 20 20 20 20 20 20 20 38 20 20  EAP          8  
136c0 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42  /* void*, int nB
136d0 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a  yte, int min */.
136e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
136f0 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20  ONFIG_MEMSTATUS 
13700 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61      9  /* boolea
13710 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  n */.#define SQL
13720 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
13730 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73          10  /* s
13740 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
13750 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65  hods* */.#define
13760 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47   SQLITE_CONFIG_G
13770 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 20  ETMUTEX     11  
13780 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  /* sqlite3_mutex
13790 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20  _methods* */./* 
137a0 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54  previously SQLIT
137b0 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c  E_CONFIG_CHUNKAL
137c0 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 73 20  LOC 12 which is 
137d0 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a  now unused. */ .
137e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
137f0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20  ONFIG_LOOKASIDE 
13800 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e     13  /* int in
13810 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  t */.#define SQL
13820 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
13830 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73  E       14  /* s
13840 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
13850 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e  thods* */.#defin
13860 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
13870 47 45 54 50 43 41 43 48 45 20 20 20 20 31 35 20  GETPCACHE    15 
13880 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63   /* sqlite3_pcac
13890 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a  he_methods* */..
138a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
138b0 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70  Configuration Op
138c0 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c  tions {H10170} <
138d0 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S20000>.** EXPER
138e0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
138f0 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  ese constants ar
13900 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  e the available 
13910 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72  integer configur
13920 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68  ation options th
13930 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73  at.** can be pas
13940 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
13950 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
13960 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f  e [sqlite3_db_co
13970 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63  nfig()] interfac
13980 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e  e..**.** New con
13990 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
139a0 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20  ns may be added 
139b0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
139c0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a  es of SQLite..**
139d0 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67   Existing config
139e0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
139f0 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74  might be discont
13a00 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74  inued.  Applicat
13a10 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63  ions.** should c
13a20 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
13a30 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74  code from [sqlit
13a40 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20  e3_db_config()] 
13a50 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
13a60 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f  t.** the call wo
13a70 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69  rked.  The [sqli
13a80 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d  te3_db_config()]
13a90 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20   interface will 
13aa0 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d  return a.** non-
13ab0 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65  zero [error code
13ac0 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e  ] if a discontin
13ad0 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  ued or unsupport
13ae0 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ed configuration
13af0 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e   option.** is in
13b00 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c  voked..**.** <dl
13b10 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  >.** <dt>SQLITE_
13b20 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49  DBCONFIG_LOOKASI
13b30 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  DE</dt>.** <dd>T
13b40 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73  his option takes
13b50 20 74 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61   three additiona
13b60 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74  l arguments that
13b70 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a   determine the .
13b80 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  ** [lookaside me
13b90 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20  mory allocator] 
13ba0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f  configuration fo
13bb0 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  r the [database 
13bc0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20  connection]..** 
13bd0 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
13be0 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 70 61  nt (the third pa
13bf0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
13c00 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d  te3_db_config()]
13c10 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72   is a.** pointer
13c20 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 62 75 66   to a memory buf
13c30 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20 6c  fer to use for l
13c40 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e  ookaside memory.
13c50 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 61    The first.** a
13c60 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e  rgument may be N
13c70 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73  ULL in which cas
13c80 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c  e SQLite will al
13c90 6c 6f 63 61 74 65 20 74 68 65 20 6c 6f 6f 6b 61  locate the looka
13ca0 73 69 64 65 0a 2a 2a 20 62 75 66 66 65 72 20 69  side.** buffer i
13cb0 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c  tself using [sql
13cc0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20  ite3_malloc()]. 
13cd0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
13ce0 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73  ment is the.** s
13cf0 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b  ize of each look
13d00 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f  aside buffer slo
13d10 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20  t and the third 
13d20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
13d30 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f  number of.** slo
13d40 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66  ts.  The size of
13d50 20 74 68 65 20 62 75 66 66 65 72 20 69 6e 20 74   the buffer in t
13d60 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
13d70 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65  t must be greate
13d80 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75  r than.** or equ
13d90 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63  al to the produc
13da0 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20  t of the second 
13db0 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 6d 65  and third argume
13dc0 6e 74 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  nts.</dd>.**.** 
13dd0 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65  </dl>.*/.#define
13de0 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47   SQLITE_DBCONFIG
13df0 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20 31 30  _LOOKASIDE    10
13e00 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69 6e 74  01  /* void* int
13e10 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20   int */.../*.** 
13e20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65  CAPI3REF: Enable
13e30 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65   Or Disable Exte
13e40 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64 65  nded Result Code
13e50 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31 30 37  s {H12200} <S107
13e60 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
13e70 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72  lite3_extended_r
13e80 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 72 6f  esult_codes() ro
13e90 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72  utine enables or
13ea0 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a 2a 2a   disables the.**
13eb0 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c   [extended resul
13ec0 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 72 65  t codes] feature
13ed0 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68 65 20   of SQLite. The 
13ee0 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 0a  extended result.
13ef0 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64 69 73  ** codes are dis
13f00 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
13f10 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20   for historical 
13f20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 63 6f  compatibility co
13f30 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a  nsiderations..**
13f40 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
13f50 2a 2a 0a 2a 2a 20 7b 48 31 32 32 30 31 7d 20 45  **.** {H12201} E
13f60 61 63 68 20 6e 65 77 20 5b 64 61 74 61 62 61 73  ach new [databas
13f70 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 68  e connection] sh
13f80 61 6c 6c 20 68 61 76 65 20 74 68 65 0a 2a 2a 20  all have the.** 
13f90 20 20 20 20 20 20 20 20 20 5b 65 78 74 65 6e 64           [extend
13fa0 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d  ed result codes]
13fb0 20 66 65 61 74 75 72 65 20 64 69 73 61 62 6c 65   feature disable
13fc0 64 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a  d by default..**
13fd0 0a 2a 2a 20 7b 48 31 32 32 30 32 7d 20 54 68 65  .** {H12202} The
13fe0 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64   [sqlite3_extend
13ff0 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28  ed_result_codes(
14000 44 2c 46 29 5d 20 69 6e 74 65 72 66 61 63 65 20  D,F)] interface 
14010 73 68 61 6c 6c 20 65 6e 61 62 6c 65 0a 2a 2a 20  shall enable.** 
14020 20 20 20 20 20 20 20 20 20 5b 65 78 74 65 6e 64           [extend
14030 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d  ed result codes]
14040 20 66 6f 72 20 74 68 65 20 20 5b 64 61 74 61 62   for the  [datab
14050 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
14060 44 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66  D.**          if
14070 20 74 68 65 20 46 20 70 61 72 61 6d 65 74 65 72   the F parameter
14080 20 69 73 20 74 72 75 65 2c 20 6f 72 20 64 69 73   is true, or dis
14090 61 62 6c 65 20 74 68 65 6d 20 69 66 20 46 20 69  able them if F i
140a0 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49  s false..*/.SQLI
140b0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
140c0 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75  e3_extended_resu
140d0 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33  lt_codes(sqlite3
140e0 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a  *, int onoff);..
140f0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
14100 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69  Last Insert Rowi
14110 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37  d {H12220} <S107
14120 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65  00>.**.** Each e
14130 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74  ntry in an SQLit
14140 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e  e table has a un
14150 69 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e  ique 64-bit sign
14160 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65  ed.** integer ke
14170 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f  y called the [RO
14180 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20  WID | "rowid"]. 
14190 54 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77  The rowid is alw
141a0 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  ays available.**
141b0 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65   as an undeclare
141c0 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52  d column named R
141d0 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52  OWID, OID, or _R
141e0 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73  OWID_ as long as
141f0 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20   those.** names 
14200 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65  are not also use
14210 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20  d by explicitly 
14220 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73  declared columns
14230 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  . If.** the tabl
14240 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f  e has a column o
14250 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20  f type [INTEGER 
14260 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65  PRIMARY KEY] the
14270 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a  n that column.**
14280 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61   is another alia
14290 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e  s for the rowid.
142a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
142b0 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
142c0 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d  [rowid] of the m
142d0 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75  ost recent.** su
142e0 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54  ccessful [INSERT
142f0 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  ] into the datab
14300 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61  ase from the [da
14310 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14320 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72  n].** in the fir
14330 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66  st argument.  If
14340 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b   no successful [
14350 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65  INSERT]s.** have
14360 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f   ever occurred o
14370 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20  n that database 
14380 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f  connection, zero
14390 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
143a0 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52  .** If an [INSER
143b0 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  T] occurs within
143c0 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e   a trigger, then
143d0 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20   the [rowid] of 
143e0 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20  the inserted.** 
143f0 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20  row is returned 
14400 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
14410 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74  as long as the t
14420 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e  rigger is runnin
14430 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74  g..** But once t
14440 68 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69  he trigger termi
14450 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65  nates, the value
14460 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69   returned by thi
14470 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76  s routine.** rev
14480 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74  erts to the last
14490 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20   value inserted 
144a0 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67  before the trigg
144b0 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  er fired..**.** 
144c0 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74  An [INSERT] that
144d0 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20   fails due to a 
144e0 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
144f0 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a  tion is not a.**
14500 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53   successful [INS
14510 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f  ERT] and does no
14520 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  t change the val
14530 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ue returned by t
14540 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20  his.** routine. 
14550 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20   Thus INSERT OR 
14560 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20  FAIL, INSERT OR 
14570 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f  IGNORE, INSERT O
14580 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61  R ROLLBACK,.** a
14590 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f  nd INSERT OR ABO
145a0 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67  RT make no chang
145b0 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e  es to the return
145c0 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a   value of this.*
145d0 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74  * routine when t
145e0 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66  heir insertion f
145f0 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45  ails.  When INSE
14600 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a  RT OR REPLACE.**
14610 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f   encounters a co
14620 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
14630 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20  on, it does not 
14640 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e  fail.  The.** IN
14650 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74  SERT continues t
14660 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74  o completion aft
14670 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73  er deleting rows
14680 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20   that caused.** 
14690 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70  the constraint p
146a0 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54  roblem so INSERT
146b0 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c   OR REPLACE will
146c0 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a   always change.*
146d0 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * the return val
146e0 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72  ue of this inter
146f0 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  face..**.** For 
14700 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
14710 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e  this routine, an
14720 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e   [INSERT] is con
14730 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65  sidered to.** be
14740 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e   successful even
14750 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71   if it is subseq
14760 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61  uently rolled ba
14770 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ck..**.** INVARI
14780 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  ANTS:.**.** {H12
14790 32 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  221} The [sqlite
147a0 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
147b0 77 69 64 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20  wid()] function 
147c0 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20  shall return.** 
147d0 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 72 6f           the [ro
147e0 77 69 64 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  wid].**         
147f0 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
14800 65 6e 74 20 73 75 63 63 65 73 73 66 75 6c 20 5b  ent successful [
14810 49 4e 53 45 52 54 5d 20 70 65 72 66 6f 72 6d 65  INSERT] performe
14820 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a  d on the same.**
14830 20 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62            [datab
14840 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
14850 61 6e 64 20 77 69 74 68 69 6e 20 74 68 65 20 73  and within the s
14860 61 6d 65 20 6f 72 20 68 69 67 68 65 72 20 6c 65  ame or higher le
14870 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  vel.**          
14880 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c  trigger context,
14890 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72   or zero if ther
148a0 65 20 68 61 76 65 20 62 65 65 6e 20 6e 6f 20 71  e have been no q
148b0 75 61 6c 69 66 79 69 6e 67 0a 2a 2a 20 20 20 20  ualifying.**    
148c0 20 20 20 20 20 20 5b 49 4e 53 45 52 54 5d 20 73        [INSERT] s
148d0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
148e0 20 7b 48 31 32 32 32 33 7d 20 54 68 65 20 5b 73   {H12223} The [s
148f0 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
14900 72 74 5f 72 6f 77 69 64 28 29 5d 20 66 75 6e 63  rt_rowid()] func
14910 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72  tion shall retur
14920 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  n the.**        
14930 20 20 73 61 6d 65 20 76 61 6c 75 65 20 77 68 65    same value whe
14940 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68  n called from th
14950 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 20 63  e same trigger c
14960 6f 6e 74 65 78 74 0a 2a 2a 20 20 20 20 20 20 20  ontext.**       
14970 20 20 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62     immediately b
14980 65 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 20  efore and after 
14990 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a  a [ROLLBACK]..**
149a0 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a  .** ASSUMPTIONS:
149b0 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 33 32 7d 20  .**.** {A12232} 
149c0 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68  If a separate th
149d0 72 65 61 64 20 70 65 72 66 6f 72 6d 73 20 61 20  read performs a 
149e0 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e 20  new [INSERT] on 
149f0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20  the same.**     
14a00 20 20 20 20 20 64 61 74 61 62 61 73 65 20 63 6f       database co
14a10 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74  nnection while t
14a20 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74  he [sqlite3_last
14a30 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d  _insert_rowid()]
14a40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e  .**          fun
14a50 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67  ction is running
14a60 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65   and thus change
14a70 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72  s the last inser
14a80 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 20 20  t [rowid],.**   
14a90 20 20 20 20 20 20 20 74 68 65 6e 20 74 68 65 20         then the 
14aa0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
14ab0 79 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  y [sqlite3_last_
14ac0 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 20  insert_rowid()] 
14ad0 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75  is.**          u
14ae0 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64  npredictable and
14af0 20 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c   might not equal
14b00 20 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20   either the old 
14b10 6f 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 20 20  or the new.**   
14b20 20 20 20 20 20 20 20 6c 61 73 74 20 69 6e 73 65         last inse
14b30 72 74 20 5b 72 6f 77 69 64 5d 2e 0a 2a 2f 0a 53  rt [rowid]..*/.S
14b40 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
14b50 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
14b60 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
14b70 64 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a  d(sqlite3*);../*
14b80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f  .** CAPI3REF: Co
14b90 75 6e 74 20 54 68 65 20 4e 75 6d 62 65 72 20 4f  unt The Number O
14ba0 66 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64 20  f Rows Modified 
14bb0 7b 48 31 32 32 34 30 7d 20 3c 53 31 30 36 30 30  {H12240} <S10600
14bc0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  >.**.** This fun
14bd0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
14be0 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 74 61  e number of data
14bf0 62 61 73 65 20 72 6f 77 73 20 74 68 61 74 20 77  base rows that w
14c00 65 72 65 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f  ere changed.** o
14c10 72 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65  r inserted or de
14c20 6c 65 74 65 64 20 62 79 20 74 68 65 20 6d 6f 73  leted by the mos
14c30 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c  t recently compl
14c40 65 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  eted SQL stateme
14c50 6e 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 5b 64 61  nt.** on the [da
14c60 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14c70 6e 5d 20 73 70 65 63 69 66 69 65 64 20 62 79 20  n] specified by 
14c80 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  the first parame
14c90 74 65 72 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 68 61  ter..** Only cha
14ca0 6e 67 65 73 20 74 68 61 74 20 61 72 65 20 64 69  nges that are di
14cb0 72 65 63 74 6c 79 20 73 70 65 63 69 66 69 65 64  rectly specified
14cc0 20 62 79 20 74 68 65 20 5b 49 4e 53 45 52 54 5d   by the [INSERT]
14cd0 2c 20 5b 55 50 44 41 54 45 5d 2c 0a 2a 2a 20 6f  , [UPDATE],.** o
14ce0 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65  r [DELETE] state
14cf0 6d 65 6e 74 20 61 72 65 20 63 6f 75 6e 74 65 64  ment are counted
14d00 2e 20 20 41 75 78 69 6c 69 61 72 79 20 63 68 61  .  Auxiliary cha
14d10 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 0a 2a  nges caused by.*
14d20 2a 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6e  * triggers are n
14d30 6f 74 20 63 6f 75 6e 74 65 64 2e 20 55 73 65 20  ot counted. Use 
14d40 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74  the [sqlite3_tot
14d50 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 66 75  al_changes()] fu
14d60 6e 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 66 69 6e  nction.** to fin
14d70 64 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  d the total numb
14d80 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e  er of changes in
14d90 63 6c 75 64 69 6e 67 20 63 68 61 6e 67 65 73 20  cluding changes 
14da0 63 61 75 73 65 64 20 62 79 20 74 72 69 67 67 65  caused by trigge
14db0 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 72 6f 77  rs..**.** A "row
14dc0 20 63 68 61 6e 67 65 22 20 69 73 20 61 20 63 68   change" is a ch
14dd0 61 6e 67 65 20 74 6f 20 61 20 73 69 6e 67 6c 65  ange to a single
14de0 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65   row of a single
14df0 20 74 61 62 6c 65 0a 2a 2a 20 63 61 75 73 65 64   table.** caused
14e00 20 62 79 20 61 6e 20 49 4e 53 45 52 54 2c 20 44   by an INSERT, D
14e10 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45  ELETE, or UPDATE
14e20 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 6f 77   statement.  Row
14e30 73 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 68  s that.** are ch
14e40 61 6e 67 65 64 20 61 73 20 73 69 64 65 20 65 66  anged as side ef
14e50 66 65 63 74 73 20 6f 66 20 52 45 50 4c 41 43 45  fects of REPLACE
14e60 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f   constraint reso
14e70 6c 75 74 69 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62  lution,.** rollb
14e80 61 63 6b 2c 20 41 42 4f 52 54 20 70 72 6f 63 65  ack, ABORT proce
14e90 73 73 69 6e 67 2c 20 44 52 4f 50 20 54 41 42 4c  ssing, DROP TABL
14ea0 45 2c 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68  E, or by any oth
14eb0 65 72 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73  er.** mechanisms
14ec0 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73   do not count as
14ed0 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e   direct row chan
14ee0 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72  ges..**.** A "tr
14ef0 69 67 67 65 72 20 63 6f 6e 74 65 78 74 22 20 69  igger context" i
14f00 73 20 61 20 73 63 6f 70 65 20 6f 66 20 65 78 65  s a scope of exe
14f10 63 75 74 69 6f 6e 20 74 68 61 74 20 62 65 67 69  cution that begi
14f20 6e 73 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 77  ns and.** ends w
14f30 69 74 68 20 74 68 65 20 73 63 72 69 70 74 20 6f  ith the script o
14f40 66 20 61 20 74 72 69 67 67 65 72 2e 20 20 4d 6f  f a trigger.  Mo
14f50 73 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  st SQL statement
14f60 73 20 61 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74  s are.** evaluat
14f70 65 64 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e  ed outside of an
14f80 79 20 74 72 69 67 67 65 72 2e 20 20 54 68 69 73  y trigger.  This
14f90 20 69 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76   is the "top lev
14fa0 65 6c 22 0a 2a 2a 20 74 72 69 67 67 65 72 20 63  el".** trigger c
14fb0 6f 6e 74 65 78 74 2e 20 20 49 66 20 61 20 74 72  ontext.  If a tr
14fc0 69 67 67 65 72 20 66 69 72 65 73 20 66 72 6f 6d  igger fires from
14fd0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20   the top level, 
14fe0 61 0a 2a 2a 20 6e 65 77 20 74 72 69 67 67 65 72  a.** new trigger
14ff0 20 63 6f 6e 74 65 78 74 20 69 73 20 65 6e 74 65   context is ente
15000 72 65 64 20 66 6f 72 20 74 68 65 20 64 75 72 61  red for the dura
15010 74 69 6f 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65  tion of that one
15020 0a 2a 2a 20 74 72 69 67 67 65 72 2e 20 20 53 75  .** trigger.  Su
15030 62 74 72 69 67 67 65 72 73 20 63 72 65 61 74 65  btriggers create
15040 20 73 75 62 63 6f 6e 74 65 78 74 73 20 66 6f 72   subcontexts for
15050 20 74 68 65 69 72 20 64 75 72 61 74 69 6f 6e 2e   their duration.
15060 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b  .**.** Calling [
15070 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
15080 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  or [sqlite3_step
15090 28 29 5d 20 72 65 63 75 72 73 69 76 65 6c 79 20  ()] recursively 
150a0 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61  does.** not crea
150b0 74 65 20 61 20 6e 65 77 20 74 72 69 67 67 65 72  te a new trigger
150c0 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20   context..**.** 
150d0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
150e0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
150f0 20 6f 66 20 64 69 72 65 63 74 20 72 6f 77 20 63   of direct row c
15100 68 61 6e 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a  hanges in the.**
15110 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53   most recent INS
15120 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
15130 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
15140 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
15150 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74  .** trigger cont
15160 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c  ext..**.** Thus,
15170 20 77 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f   when called fro
15180 6d 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c  m the top level,
15190 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
151a0 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75  eturns the.** nu
151b0 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20  mber of changes 
151c0 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  in the most rece
151d0 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  nt INSERT, UPDAT
151e0 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20  E, or DELETE.** 
151f0 74 68 61 74 20 61 6c 73 6f 20 6f 63 63 75 72 72  that also occurr
15200 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6c 65  ed at the top le
15210 76 65 6c 2e 20 20 57 69 74 68 69 6e 20 74 68 65  vel.  Within the
15220 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67   body of a trigg
15230 65 72 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74  er,.** the sqlit
15240 65 33 5f 63 68 61 6e 67 65 73 28 29 20 69 6e 74  e3_changes() int
15250 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 63 61  erface can be ca
15260 6c 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65  lled to find the
15270 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68   number of.** ch
15280 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73  anges in the mos
15290 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c  t recently compl
152a0 65 74 65 64 20 49 4e 53 45 52 54 2c 20 55 50 44  eted INSERT, UPD
152b0 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a  ATE, or DELETE.*
152c0 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  * statement with
152d0 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74  in the body of t
152e0 68 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 2e  he same trigger.
152f0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65  .** However, the
15300 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64   number returned
15310 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
15320 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75  e changes.** cau
15330 73 65 64 20 62 79 20 73 75 62 74 72 69 67 67 65  sed by subtrigge
15340 72 73 20 73 69 6e 63 65 20 74 68 6f 73 65 20 68  rs since those h
15350 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 63 6f  ave their own co
15360 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  ntext..**.** SQL
15370 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ite implements t
15380 68 65 20 63 6f 6d 6d 61 6e 64 20 22 44 45 4c 45  he command "DELE
15390 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 22 20 77  TE FROM table" w
153a0 69 74 68 6f 75 74 20 61 20 57 48 45 52 45 20 63  ithout a WHERE c
153b0 6c 61 75 73 65 0a 2a 2a 20 62 79 20 64 72 6f 70  lause.** by drop
153c0 70 69 6e 67 20 61 6e 64 20 72 65 63 72 65 61 74  ping and recreat
153d0 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20  ing the table.  
153e0 44 6f 69 6e 67 20 73 6f 20 69 73 20 6d 75 63 68  Doing so is much
153f0 20 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f 69   faster than goi
15400 6e 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e  ng.** through an
15410 64 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69 76  d deleting indiv
15420 69 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20 66  idual elements f
15430 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 2e 20 20  rom the table.  
15440 42 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 0a  Because of this.
15450 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c  ** optimization,
15460 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 73 20 69   the deletions i
15470 6e 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74  n "DELETE FROM t
15480 61 62 6c 65 22 20 61 72 65 20 6e 6f 74 20 72 6f  able" are not ro
15490 77 20 63 68 61 6e 67 65 73 20 61 6e 64 0a 2a 2a  w changes and.**
154a0 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 75   will not be cou
154b0 6e 74 65 64 20 62 79 20 74 68 65 20 73 71 6c 69  nted by the sqli
154c0 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 72  te3_changes() or
154d0 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f   [sqlite3_total_
154e0 63 68 61 6e 67 65 73 28 29 5d 0a 2a 2a 20 66 75  changes()].** fu
154f0 6e 63 74 69 6f 6e 73 2c 20 72 65 67 61 72 64 6c  nctions, regardl
15500 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ess of the numbe
15510 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 74 68  r of elements th
15520 61 74 20 77 65 72 65 20 6f 72 69 67 69 6e 61 6c  at were original
15530 6c 79 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62  ly.** in the tab
15540 6c 65 2e 20 20 54 6f 20 67 65 74 20 61 6e 20 61  le.  To get an a
15550 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20 6f 66  ccurate count of
15560 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
15570 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75 73 65  ows deleted, use
15580 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52 4f 4d  .** "DELETE FROM
15590 20 74 61 62 6c 65 20 57 48 45 52 45 20 31 22 20   table WHERE 1" 
155a0 69 6e 73 74 65 61 64 2e 20 20 4f 72 20 72 65 63  instead.  Or rec
155b0 6f 6d 70 69 6c 65 20 75 73 69 6e 67 20 74 68 65  ompile using the
155c0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54  .** [SQLITE_OMIT
155d0 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d 49  _TRUNCATE_OPTIMI
155e0 5a 41 54 49 4f 4e 5d 20 63 6f 6d 70 69 6c 65 2d  ZATION] compile-
155f0 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74 6f 20 64  time option to d
15600 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 6f 70  isable the.** op
15610 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 6c  timization on al
15620 6c 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a  l queries..**.**
15630 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
15640 2a 2a 20 7b 48 31 32 32 34 31 7d 20 54 68 65 20  ** {H12241} The 
15650 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73  [sqlite3_changes
15660 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61  ()] function sha
15670 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  ll return the nu
15680 6d 62 65 72 20 6f 66 0a 2a 2a 20 20 20 20 20 20  mber of.**      
15690 20 20 20 20 72 6f 77 20 63 68 61 6e 67 65 73 20      row changes 
156a0 63 61 75 73 65 64 20 62 79 20 74 68 65 20 6d 6f  caused by the mo
156b0 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54  st recent INSERT
156c0 2c 20 55 50 44 41 54 45 2c 0a 2a 2a 20 20 20 20  , UPDATE,.**    
156d0 20 20 20 20 20 20 6f 72 20 44 45 4c 45 54 45 20        or DELETE 
156e0 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 74 68 65  statement on the
156f0 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63   same database c
15700 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a  onnection and.**
15710 20 20 20 20 20 20 20 20 20 20 77 69 74 68 69 6e            within
15720 20 74 68 65 20 73 61 6d 65 20 6f 72 20 68 69 67   the same or hig
15730 68 65 72 20 74 72 69 67 67 65 72 20 63 6f 6e 74  her trigger cont
15740 65 78 74 2c 20 6f 72 20 7a 65 72 6f 20 69 66 20  ext, or zero if 
15750 74 68 65 72 65 20 68 61 76 65 0a 2a 2a 20 20 20  there have.**   
15760 20 20 20 20 20 20 20 6e 6f 74 20 62 65 65 6e 20         not been 
15770 61 6e 79 20 71 75 61 6c 69 66 79 69 6e 67 20 72  any qualifying r
15780 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a  ow changes..**.*
15790 2a 20 7b 48 31 32 32 34 33 7d 20 53 74 61 74 65  * {H12243} State
157a0 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72  ments of the for
157b0 6d 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74  m "DELETE FROM t
157c0 61 62 6c 65 6e 61 6d 65 22 20 77 69 74 68 20 6e  ablename" with n
157d0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 57 48  o.**          WH
157e0 45 52 45 20 63 6c 61 75 73 65 20 73 68 61 6c 6c  ERE clause shall
157f0 20 63 61 75 73 65 20 73 75 62 73 65 71 75 65 6e   cause subsequen
15800 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20  t calls to.**   
15810 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
15820 63 68 61 6e 67 65 73 28 29 5d 20 74 6f 20 72 65  changes()] to re
15830 74 75 72 6e 20 7a 65 72 6f 2c 20 72 65 67 61 72  turn zero, regar
15840 64 6c 65 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20  dless of the.** 
15850 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20           number 
15860 6f 66 20 72 6f 77 73 20 6f 72 69 67 69 6e 61 6c  of rows original
15870 6c 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ly in the table.
15880 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f  .**.** ASSUMPTIO
15890 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 35  NS:.**.** {A1225
158a0 32 7d 20 49 66 20 61 20 73 65 70 61 72 61 74 65  2} If a separate
158b0 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68   thread makes ch
158c0 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d  anges on the sam
158d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
158e0 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ction.**        
158f0 20 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33    while [sqlite3
15900 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73 20 72  _changes()] is r
15910 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68 65 20  unning then the 
15920 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a  value returned.*
15930 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 75 6e  *          is un
15940 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20  predictable and 
15950 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a  not meaningful..
15960 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
15970 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65  t sqlite3_change
15980 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a  s(sqlite3*);../*
15990 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 6f  .** CAPI3REF: To
159a0 74 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f  tal Number Of Ro
159b0 77 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32  ws Modified {H12
159c0 32 36 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a  260} <S10600>.**
159d0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
159e0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  n returns the nu
159f0 6d 62 65 72 20 6f 66 20 72 6f 77 20 63 68 61 6e  mber of row chan
15a00 67 65 73 20 63 61 75 73 65 64 20 62 79 20 49 4e  ges caused by IN
15a10 53 45 52 54 2c 0a 2a 2a 20 55 50 44 41 54 45 20  SERT,.** UPDATE 
15a20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d  or DELETE statem
15a30 65 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20 5b  ents since the [
15a40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
15a50 69 6f 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64 2e  ion] was opened.
15a60 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 20 69 6e  .** The count in
15a70 63 6c 75 64 65 73 20 61 6c 6c 20 63 68 61 6e 67  cludes all chang
15a80 65 73 20 66 72 6f 6d 20 61 6c 6c 20 74 72 69 67  es from all trig
15a90 67 65 72 20 63 6f 6e 74 65 78 74 73 2e 20 20 48  ger contexts.  H
15aa0 6f 77 65 76 65 72 2c 0a 2a 2a 20 74 68 65 20 63  owever,.** the c
15ab0 6f 75 6e 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e  ount does not in
15ac0 63 6c 75 64 65 20 63 68 61 6e 67 65 73 20 75 73  clude changes us
15ad0 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
15ae0 52 45 50 4c 41 43 45 20 63 6f 6e 73 74 72 61 69  REPLACE constrai
15af0 6e 74 73 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62  nts,.** do rollb
15b00 61 63 6b 73 20 6f 72 20 41 42 4f 52 54 20 70 72  acks or ABORT pr
15b10 6f 63 65 73 73 69 6e 67 2c 20 6f 72 20 44 52 4f  ocessing, or DRO
15b20 50 20 74 61 62 6c 65 20 70 72 6f 63 65 73 73 69  P table processi
15b30 6e 67 2e 0a 2a 2a 20 54 68 65 20 63 68 61 6e 67  ng..** The chang
15b40 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 61  es are counted a
15b50 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 73 74  s soon as the st
15b60 61 74 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 6b  atement that mak
15b70 65 73 20 74 68 65 6d 20 69 73 0a 2a 2a 20 63 6f  es them is.** co
15b80 6d 70 6c 65 74 65 64 20 28 77 68 65 6e 20 74 68  mpleted (when th
15b90 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  e statement hand
15ba0 6c 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20  le is passed to 
15bb0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
15bc0 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ] or.** [sqlite3
15bd0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a  _finalize()])..*
15be0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c  *.** SQLite impl
15bf0 65 6d 65 6e 74 73 20 74 68 65 20 63 6f 6d 6d 61  ements the comma
15c00 6e 64 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20  nd "DELETE FROM 
15c10 74 61 62 6c 65 22 20 77 69 74 68 6f 75 74 20 61  table" without a
15c20 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a   WHERE clause.**
15c30 20 62 79 20 64 72 6f 70 70 69 6e 67 20 61 6e 64   by dropping and
15c40 20 72 65 63 72 65 61 74 69 6e 67 20 74 68 65 20   recreating the 
15c50 74 61 62 6c 65 2e 20 20 28 54 68 69 73 20 69 73  table.  (This is
15c60 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 61   much faster tha
15c70 6e 20 67 6f 69 6e 67 0a 2a 2a 20 74 68 72 6f 75  n going.** throu
15c80 67 68 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 20  gh and deleting 
15c90 69 6e 64 69 76 69 64 75 61 6c 20 65 6c 65 6d 65  individual eleme
15ca0 6e 74 73 20 66 72 6f 6d 20 74 68 65 20 74 61 62  nts from the tab
15cb0 6c 65 2e 29 20 20 42 65 63 61 75 73 65 20 6f 66  le.)  Because of
15cc0 20 74 68 69 73 0a 2a 2a 20 6f 70 74 69 6d 69 7a   this.** optimiz
15cd0 61 74 69 6f 6e 2c 20 74 68 65 20 64 65 6c 65 74  ation, the delet
15ce0 69 6f 6e 73 20 69 6e 20 22 44 45 4c 45 54 45 20  ions in "DELETE 
15cf0 46 52 4f 4d 20 74 61 62 6c 65 22 20 61 72 65 20  FROM table" are 
15d00 6e 6f 74 20 72 6f 77 20 63 68 61 6e 67 65 73 20  not row changes 
15d10 61 6e 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  and.** will not 
15d20 62 65 20 63 6f 75 6e 74 65 64 20 62 79 20 74 68  be counted by th
15d30 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65  e sqlite3_change
15d40 73 28 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  s() or [sqlite3_
15d50 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d  total_changes()]
15d60 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2c 20 72  .** functions, r
15d70 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
15d80 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   number of eleme
15d90 6e 74 73 20 74 68 61 74 20 77 65 72 65 20 6f 72  nts that were or
15da0 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 69 6e 20 74  iginally.** in t
15db0 68 65 20 74 61 62 6c 65 2e 20 20 54 6f 20 67 65  he table.  To ge
15dc0 74 20 61 6e 20 61 63 63 75 72 61 74 65 20 63 6f  t an accurate co
15dd0 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  unt of the numbe
15de0 72 20 6f 66 20 72 6f 77 73 20 64 65 6c 65 74 65  r of rows delete
15df0 64 2c 20 75 73 65 0a 2a 2a 20 22 44 45 4c 45 54  d, use.** "DELET
15e00 45 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45  E FROM table WHE
15e10 52 45 20 31 22 20 69 6e 73 74 65 61 64 2e 20 20  RE 1" instead.  
15e20 20 4f 72 20 72 65 63 6f 6d 70 69 6c 65 20 75 73   Or recompile us
15e30 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49  ing the.** [SQLI
15e40 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45  TE_OMIT_TRUNCATE
15e50 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 5d 20 63  _OPTIMIZATION] c
15e60 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
15e70 6f 6e 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68  on to disable th
15e80 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f  e.** optimizatio
15e90 6e 20 6f 6e 20 61 6c 6c 20 71 75 65 72 69 65 73  n on all queries
15ea0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
15eb0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 68   the [sqlite3_ch
15ec0 61 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66 61  anges()] interfa
15ed0 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ce..**.** INVARI
15ee0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  ANTS:.**.** {H12
15ef0 32 36 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  261} The [sqlite
15f00 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  3_total_changes(
15f10 29 5d 20 72 65 74 75 72 6e 73 20 74 68 65 20 74  )] returns the t
15f20 6f 74 61 6c 20 6e 75 6d 62 65 72 0a 2a 2a 20 20  otal number.**  
15f30 20 20 20 20 20 20 20 20 6f 66 20 72 6f 77 20 63          of row c
15f40 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79  hanges caused by
15f50 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
15f60 20 61 6e 64 2f 6f 72 20 44 45 4c 45 54 45 0a 2a   and/or DELETE.*
15f70 2a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65  *          state
15f80 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 73 61 6d  ments on the sam
15f90 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
15fa0 65 63 74 69 6f 6e 5d 2c 20 69 6e 20 61 6e 79 0a  ection], in any.
15fb0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 72 69 67  **          trig
15fc0 67 65 72 20 63 6f 6e 74 65 78 74 2c 20 73 69 6e  ger context, sin
15fd0 63 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ce the database 
15fe0 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 61 73 20 63  connection was c
15ff0 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48  reated..**.** {H
16000 31 32 32 36 33 7d 20 53 74 61 74 65 6d 65 6e 74  12263} Statement
16010 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 44  s of the form "D
16020 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
16030 6e 61 6d 65 22 20 77 69 74 68 20 6e 6f 0a 2a 2a  name" with no.**
16040 20 20 20 20 20 20 20 20 20 20 57 48 45 52 45 20            WHERE 
16050 63 6c 61 75 73 65 20 73 68 61 6c 6c 20 6e 6f 74  clause shall not
16060 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   change the valu
16070 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 20 20  e returned.**   
16080 20 20 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74         by [sqlit
16090 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
160a0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d  ()]..**.** ASSUM
160b0 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41  PTIONS:.**.** {A
160c0 31 32 32 36 34 7d 20 49 66 20 61 20 73 65 70 61  12264} If a sepa
160d0 72 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b 65  rate thread make
160e0 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68 65  s changes on the
160f0 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63   same database c
16100 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20  onnection.**    
16110 20 20 20 20 20 20 77 68 69 6c 65 20 5b 73 71 6c        while [sql
16120 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
16130 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67  es()] is running
16140 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 0a   then the value.
16150 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  **          retu
16160 72 6e 65 64 20 69 73 20 75 6e 70 72 65 64 69 63  rned is unpredic
16170 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65  table and not me
16180 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c  aningful..*/.SQL
16190 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
161a0 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65  te3_total_change
161b0 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a  s(sqlite3*);../*
161c0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e  .** CAPI3REF: In
161d0 74 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d 52  terrupt A Long-R
161e0 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b 48 31  unning Query {H1
161f0 32 32 37 30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a  2270} <S30500>.*
16200 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
16210 6f 6e 20 63 61 75 73 65 73 20 61 6e 79 20 70 65  on causes any pe
16220 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 20 6f  nding database o
16230 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 62 6f 72  peration to abor
16240 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20  t and.** return 
16250 61 74 20 69 74 73 20 65 61 72 6c 69 65 73 74 20  at its earliest 
16260 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 54 68 69  opportunity. Thi
16270 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 79 70  s routine is typ
16280 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64  ically.** called
16290 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20   in response to 
162a0 61 20 75 73 65 72 20 61 63 74 69 6f 6e 20 73 75  a user action su
162b0 63 68 20 61 73 20 70 72 65 73 73 69 6e 67 20 22  ch as pressing "
162c0 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 74  Cancel".** or Ct
162d0 72 6c 2d 43 20 77 68 65 72 65 20 74 68 65 20 75  rl-C where the u
162e0 73 65 72 20 77 61 6e 74 73 20 61 20 6c 6f 6e 67  ser wants a long
162f0 20 71 75 65 72 79 20 6f 70 65 72 61 74 69 6f 6e   query operation
16300 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65   to halt.** imme
16310 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49  diately..**.** I
16320 74 20 69 73 20 73 61 66 65 20 74 6f 20 63 61 6c  t is safe to cal
16330 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  l this routine f
16340 72 6f 6d 20 61 20 74 68 72 65 61 64 20 64 69 66  rom a thread dif
16350 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a  ferent from the.
16360 2a 2a 20 74 68 72 65 61 64 20 74 68 61 74 20 69  ** thread that i
16370 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e  s currently runn
16380 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
16390 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74   operation.  But
163a0 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 61   it.** is not sa
163b0 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  fe to call this 
163c0 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20 5b  routine with a [
163d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
163e0 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69 73 20  ion] that.** is 
163f0 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68 74 20  closed or might 
16400 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 73 71 6c  close before sql
16410 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
16420 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20   returns..**.** 
16430 49 66 20 61 6e 20 53 51 4c 20 6f 70 65 72 61 74  If an SQL operat
16440 69 6f 6e 20 69 73 20 76 65 72 79 20 6e 65 61 72  ion is very near
16450 6c 79 20 66 69 6e 69 73 68 65 64 20 61 74 20 74  ly finished at t
16460 68 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a 2a 20  he time when.** 
16470 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
16480 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  t() is called, t
16490 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f 74  hen it might not
164a0 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75   have an opportu
164b0 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20 69 6e  nity.** to be in
164c0 74 65 72 72 75 70 74 65 64 20 61 6e 64 20 6d 69  terrupted and mi
164d0 67 68 74 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20  ght continue to 
164e0 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a  completion..**.*
164f0 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72 61 74 69  * An SQL operati
16500 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 74 65 72  on that is inter
16510 72 75 70 74 65 64 20 77 69 6c 6c 20 72 65 74 75  rupted will retu
16520 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52  rn [SQLITE_INTER
16530 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74 68 65  RUPT]..** If the
16540 20 69 6e 74 65 72 72 75 70 74 65 64 20 53 51 4c   interrupted SQL
16550 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e   operation is an
16560 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
16570 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68   or DELETE.** th
16580 61 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e 20  at is inside an 
16590 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63  explicit transac
165a0 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 65  tion, then the e
165b0 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f  ntire transactio
165c0 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 6f 6c  n.** will be rol
165d0 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 74  led back automat
165e0 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 20  ically..**.** A 
165f0 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
16600 69 6e 74 65 72 72 75 70 74 28 29 20 68 61 73 20  interrupt() has 
16610 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 53 51 4c  no effect on SQL
16620 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74   statements.** t
16630 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64 20  hat are started 
16640 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 69 6e  after sqlite3_in
16650 74 65 72 72 75 70 74 28 29 20 72 65 74 75 72 6e  terrupt() return
16660 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  s..**.** INVARIA
16670 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32  NTS:.**.** {H122
16680 37 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  71} The [sqlite3
16690 5f 69 6e 74 65 72 72 75 70 74 28 29 5d 20 69 6e  _interrupt()] in
166a0 74 65 72 66 61 63 65 20 77 69 6c 6c 20 66 6f 72  terface will for
166b0 63 65 20 61 6c 6c 20 72 75 6e 6e 69 6e 67 0a 2a  ce all running.*
166c0 2a 20 20 20 20 20 20 20 20 20 20 53 51 4c 20 73  *          SQL s
166d0 74 61 74 65 6d 65 6e 74 73 20 61 73 73 6f 63 69  tatements associ
166e0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 61  ated with the sa
166f0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
16700 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ection.**       
16710 20 20 20 74 6f 20 68 61 6c 74 20 61 66 74 65 72     to halt after
16720 20 70 72 6f 63 65 73 73 69 6e 67 20 61 74 20 6d   processing at m
16730 6f 73 74 20 6f 6e 65 20 61 64 64 69 74 69 6f 6e  ost one addition
16740 61 6c 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 0a  al row of data..
16750 2a 2a 0a 2a 2a 20 7b 48 31 32 32 37 32 7d 20 41  **.** {H12272} A
16760 6e 79 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ny SQL statement
16770 20 74 68 61 74 20 69 73 20 69 6e 74 65 72 72 75   that is interru
16780 70 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  pted by [sqlite3
16790 5f 69 6e 74 65 72 72 75 70 74 28 29 5d 0a 2a 2a  _interrupt()].**
167a0 20 20 20 20 20 20 20 20 20 20 77 69 6c 6c 20 72            will r
167b0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e  eturn [SQLITE_IN
167c0 54 45 52 52 55 50 54 5d 2e 0a 2a 2a 0a 2a 2a 20  TERRUPT]..**.** 
167d0 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a  ASSUMPTIONS:.**.
167e0 2a 2a 20 7b 41 31 32 32 37 39 7d 20 49 66 20 74  ** {A12279} If t
167f0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
16800 65 63 74 69 6f 6e 20 63 6c 6f 73 65 73 20 77 68  ection closes wh
16810 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74  ile [sqlite3_int
16820 65 72 72 75 70 74 28 29 5d 0a 2a 2a 20 20 20 20  errupt()].**    
16830 20 20 20 20 20 20 69 73 20 72 75 6e 6e 69 6e 67        is running
16840 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67 73   then bad things
16850 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70   will likely hap
16860 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  pen..*/.SQLITE_A
16870 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
16880 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65  interrupt(sqlite
16890 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  3*);../*.** CAPI
168a0 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20  3REF: Determine 
168b0 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d  If An SQL Statem
168c0 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 20  ent Is Complete 
168d0 7b 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30 30  {H10510} <S70200
168e0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  >.**.** These ro
168f0 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 75  utines are usefu
16900 6c 20 66 6f 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69  l for command-li
16910 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 74 65  ne input to dete
16920 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a 20  rmine if the.** 
16930 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65  currently entere
16940 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 6f 20  d text seems to 
16950 66 6f 72 6d 20 63 6f 6d 70 6c 65 74 65 20 61 20  form complete a 
16960 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f 72  SQL statement or
16970 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f 6e 61  .** if additiona
16980 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 64 65  l input is neede
16990 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 6e 67  d before sending
169a0 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f 0a 2a   the text into.*
169b0 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 61 72  * SQLite for par
169c0 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72 6f 75  sing.  These rou
169d0 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 72 75  tines return tru
169e0 65 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73  e if the input s
169f0 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73  tring.** appears
16a00 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74   to be a complet
16a10 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  e SQL statement.
16a20 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69 73    A statement is
16a30 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a   judged to be.**
16a40 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 20   complete if it 
16a50 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d 69  ends with a semi
16a60 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20  colon token and 
16a70 69 73 20 6e 6f 74 20 61 20 66 72 61 67 6d 65 6e  is not a fragmen
16a80 74 20 6f 66 20 61 0a 2a 2a 20 43 52 45 41 54 45  t of a.** CREATE
16a90 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
16aa0 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e 73 20  nt.  Semicolons 
16ab0 74 68 61 74 20 61 72 65 20 65 6d 62 65 64 64 65  that are embedde
16ac0 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74 72 69  d within.** stri
16ad0 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72 20 71  ng literals or q
16ae0 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65 72  uoted identifier
16af0 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d 65 6e   names or commen
16b00 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 69 6e  ts are not.** in
16b10 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 6e 73  dependent tokens
16b20 20 28 74 68 65 79 20 61 72 65 20 70 61 72 74 20   (they are part 
16b30 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e 20  of the token in 
16b40 77 68 69 63 68 20 74 68 65 79 20 61 72 65 0a 2a  which they are.*
16b50 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e 64 20  * embedded) and 
16b60 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f 75 6e  thus do not coun
16b70 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 6e 74  t as a statement
16b80 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a   terminator..**.
16b90 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
16ba0 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20 74  s do not parse t
16bb0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
16bc0 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e  s thus.** will n
16bd0 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74 61 63  ot detect syntac
16be0 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65 63  tically incorrec
16bf0 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  t SQL..**.** INV
16c00 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
16c10 48 31 30 35 31 31 7d 20 41 20 73 75 63 63 65 73  H10511} A succes
16c20 73 66 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e 20  sful evaluation 
16c30 6f 66 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70  of [sqlite3_comp
16c40 6c 65 74 65 28 29 5d 20 6f 72 0a 2a 2a 20 20 20  lete()] or.**   
16c50 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
16c60 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 66 75  complete16()] fu
16c70 6e 63 74 69 6f 6e 73 20 73 68 61 6c 6c 0a 2a 2a  nctions shall.**
16c80 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
16c90 20 61 20 6e 75 6d 65 72 69 63 20 31 20 69 66 20   a numeric 1 if 
16ca0 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20  and only if the 
16cb0 6c 61 73 74 20 6e 6f 6e 2d 77 68 69 74 65 73 70  last non-whitesp
16cc0 61 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ace.**          
16cd0 74 6f 6b 65 6e 20 69 6e 20 74 68 65 69 72 20 69  token in their i
16ce0 6e 70 75 74 20 69 73 20 61 20 73 65 6d 69 63 6f  nput is a semico
16cf0 6c 6f 6e 20 74 68 61 74 20 69 73 20 6e 6f 74 20  lon that is not 
16d00 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 20 20  in between.**   
16d10 20 20 20 20 20 20 20 74 68 65 20 42 45 47 49 4e         the BEGIN
16d20 20 61 6e 64 20 45 4e 44 20 6f 66 20 61 20 43 52   and END of a CR
16d30 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61  EATE TRIGGER sta
16d40 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48  tement..**.** {H
16d50 31 30 35 31 32 7d 20 49 66 20 61 20 6d 65 6d 6f  10512} If a memo
16d60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
16d70 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
16d80 67 20 61 6e 20 69 6e 76 6f 63 61 74 69 6f 6e 0a  g an invocation.
16d90 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 5b  **          of [
16da0 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
16db0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
16dc0 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 74 68  complete16()] th
16dd0 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  en the.**       
16de0 20 20 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c     routine shall
16df0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
16e00 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53  NOMEM]..**.** AS
16e10 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a  SUMPTIONS:.**.**
16e20 20 7b 41 31 30 35 31 32 7d 20 54 68 65 20 69 6e   {A10512} The in
16e30 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  put to [sqlite3_
16e40 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73 74  complete()] must
16e50 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69   be a zero-termi
16e60 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  nated.**        
16e70 20 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a    UTF-8 string..
16e80 2a 2a 0a 2a 2a 20 7b 41 31 30 35 31 33 7d 20 54  **.** {A10513} T
16e90 68 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c  he input to [sql
16ea0 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
16eb0 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72  )] must be a zer
16ec0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20  o-terminated.** 
16ed0 20 20 20 20 20 20 20 20 20 55 54 46 2d 31 36 20           UTF-16 
16ee0 73 74 72 69 6e 67 20 69 6e 20 6e 61 74 69 76 65  string in native
16ef0 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a   byte order..*/.
16f00 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
16f10 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
16f20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 29  const char *sql)
16f30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
16f40 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
16f50 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  e16(const void *
16f60 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  sql);../*.** CAP
16f70 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20  I3REF: Register 
16f80 41 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61  A Callback To Ha
16f90 6e 64 6c 65 20 53 51 4c 49 54 45 5f 42 55 53 59  ndle SQLITE_BUSY
16fa0 20 45 72 72 6f 72 73 20 7b 48 31 32 33 31 30 7d   Errors {H12310}
16fb0 20 3c 53 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S40400>.**.** 
16fc0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
16fd0 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  s a callback fun
16fe0 63 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74  ction that might
16ff0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   be invoked when
17000 65 76 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d  ever.** an attem
17010 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70  pt is made to op
17020 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61  en a database ta
17030 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72  ble that another
17040 20 74 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72   thread.** or pr
17050 6f 63 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 64  ocess has locked
17060 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62  ..**.** If the b
17070 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  usy callback is 
17080 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49  NULL, then [SQLI
17090 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c  TE_BUSY] or [SQL
170a0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
170b0 44 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  D].** is returne
170c0 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70  d immediately up
170d0 6f 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20  on encountering 
170e0 74 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65  the lock. If the
170f0 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a   busy callback.*
17100 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  * is not NULL, t
17110 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  hen the callback
17120 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64   will be invoked
17130 20 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65   with two argume
17140 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  nts..**.** The f
17150 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
17160 20 74 68 65 20 68 61 6e 64 6c 65 72 20 69 73 20   the handler is 
17170 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f  a copy of the vo
17180 69 64 2a 20 70 6f 69 6e 74 65 72 20 77 68 69 63  id* pointer whic
17190 68 0a 2a 2a 20 69 73 20 74 68 65 20 74 68 69 72  h.** is the thir
171a0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  d argument to sq
171b0 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
171c0 65 72 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e  er().  The secon
171d0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a  d argument to.**
171e0 20 74 68 65 20 68 61 6e 64 6c 65 72 20 63 61 6c   the handler cal
171f0 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 75 6d  lback is the num
17200 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61  ber of times tha
17210 74 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c  t the busy handl
17220 65 72 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69  er has.** been i
17230 6e 76 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 20  nvoked for this 
17240 6c 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20  locking event.  
17250 49 66 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63  If the.** busy c
17260 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
17270 30 2c 20 74 68 65 6e 20 6e 6f 20 61 64 64 69 74  0, then no addit
17280 69 6f 6e 61 6c 20 61 74 74 65 6d 70 74 73 20 61  ional attempts a
17290 72 65 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63  re made to.** ac
172a0 63 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73  cess the databas
172b0 65 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55  e and [SQLITE_BU
172c0 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49  SY] or [SQLITE_I
172d0 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73  OERR_BLOCKED] is
172e0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66   returned..** If
172f0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65   the callback re
17300 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  turns non-zero, 
17310 74 68 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74  then another att
17320 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20  empt.** is made 
17330 74 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61  to open the data
17340 62 61 73 65 20 66 6f 72 20 72 65 61 64 69 6e 67  base for reading
17350 20 61 6e 64 20 74 68 65 20 63 79 63 6c 65 20 72   and the cycle r
17360 65 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  epeats..**.** Th
17370 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20  e presence of a 
17380 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64 6f 65  busy handler doe
17390 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20  s not guarantee 
173a0 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20  that it will be 
173b0 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20  invoked.** when 
173c0 74 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f  there is lock co
173d0 6e 74 65 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c  ntention. If SQL
173e0 69 74 65 20 64 65 74 65 72 6d 69 6e 65 73 20 74  ite determines t
173f0 68 61 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65  hat invoking the
17400 20 62 75 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72   busy.** handler
17410 20 63 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e   could result in
17420 20 61 20 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20   a deadlock, it 
17430 77 69 6c 6c 20 67 6f 20 61 68 65 61 64 20 61 6e  will go ahead an
17440 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45  d return [SQLITE
17450 5f 42 55 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51  _BUSY].** or [SQ
17460 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
17470 45 44 5d 20 69 6e 73 74 65 61 64 20 6f 66 20 69  ED] instead of i
17480 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79  nvoking the busy
17490 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e   handler..** Con
174a0 73 69 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f  sider a scenario
174b0 20 77 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65   where one proce
174c0 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20  ss is holding a 
174d0 72 65 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a  read lock that.*
174e0 2a 20 69 74 20 69 73 20 74 72 79 69 6e 67 20 74  * it is trying t
174f0 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72  o promote to a r
17500 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64  eserved lock and
17510 0a 2a 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f  .** a second pro
17520 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20  cess is holding 
17530 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20  a reserved lock 
17540 74 68 61 74 20 69 74 20 69 73 20 74 72 79 69 6e  that it is tryin
17550 67 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20  g.** to promote 
17560 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  to an exclusive 
17570 6c 6f 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74  lock.  The first
17580 20 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20   process cannot 
17590 70 72 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75  proceed.** becau
175a0 73 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64  se it is blocked
175b0 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61   by the second a
175c0 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72  nd the second pr
175d0 6f 63 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20  ocess cannot.** 
175e0 70 72 6f 63 65 65 64 20 62 65 63 61 75 73 65 20  proceed because 
175f0 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79  it is blocked by
17600 20 74 68 65 20 66 69 72 73 74 2e 20 20 49 66 20   the first.  If 
17610 62 6f 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a  both processes.*
17620 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73  * invoke the bus
17630 79 20 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74  y handlers, neit
17640 68 65 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e  her will make an
17650 79 20 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65  y progress.  The
17660 72 65 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74  refore,.** SQLit
17670 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  e returns [SQLIT
17680 45 5f 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20  E_BUSY] for the 
17690 66 69 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68  first process, h
176a0 6f 70 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a  oping that this.
176b0 2a 2a 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74  ** will induce t
176c0 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 73  he first process
176d0 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20   to release its 
176e0 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c  read lock and al
176f0 6c 6f 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e  low.** the secon
17700 64 20 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f  d process to pro
17710 63 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ceed..**.** The 
17720 64 65 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c  default busy cal
17730 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a  lback is NULL..*
17740 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45  *.** The [SQLITE
17750 5f 42 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20  _BUSY] error is 
17760 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51  converted to [SQ
17770 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
17780 45 44 5d 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69  ED].** when SQLi
17790 74 65 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64  te is in the mid
177a0 64 6c 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74  dle of a large t
177b0 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65  ransaction where
177c0 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e   all the.** chan
177d0 67 65 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74  ges will not fit
177e0 20 69 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d   into the in-mem
177f0 6f 72 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69  ory cache.  SQLi
17800 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61  te will.** alrea
17810 64 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56  dy hold a RESERV
17820 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ED lock on the d
17830 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75  atabase file, bu
17840 74 20 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f  t it needs.** to
17850 20 70 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f   promote this lo
17860 63 6b 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20  ck to EXCLUSIVE 
17870 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 73  so that it can s
17880 70 69 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61  pill cache.** pa
17890 67 65 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ges into the dat
178a0 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f  abase file witho
178b0 75 74 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75  ut harm to concu
178c0 72 72 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73  rrent.** readers
178d0 2e 20 20 49 66 20 69 74 20 69 73 20 75 6e 61 62  .  If it is unab
178e0 6c 65 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68  le to promote th
178f0 65 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65  e lock, then the
17900 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61   in-memory.** ca
17910 63 68 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74  che will be left
17920 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74   in an inconsist
17930 65 6e 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f  ent state and so
17940 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f   the error.** co
17950 64 65 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66  de is promoted f
17960 72 6f 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65  rom the relative
17970 6c 79 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54  ly benign [SQLIT
17980 45 5f 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68  E_BUSY] to.** th
17990 65 20 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53  e more severe [S
179a0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43  QLITE_IOERR_BLOC
179b0 4b 45 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f  KED].  This erro
179c0 72 20 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e  r code promotion
179d0 0a 2a 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75  .** forces an au
179e0 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b  tomatic rollback
179f0 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e   of the changes.
17a00 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20    See the.** <a 
17a10 68 72 65 66 3d 22 2f 63 76 73 74 72 61 63 2f 77  href="/cvstrac/w
17a20 69 6b 69 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e  iki?p=Corruption
17a30 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72  FollowingBusyErr
17a40 6f 72 22 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69  or">.** Corrupti
17a50 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45  onFollowingBusyE
17a60 72 72 6f 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61  rror</a> wiki pa
17a70 67 65 20 66 6f 72 20 61 20 64 69 73 63 75 73 73  ge for a discuss
17a80 69 6f 6e 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68  ion of why.** th
17a90 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e  is is important.
17aa0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e  .**.** There can
17ab0 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c   only be a singl
17ac0 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64  e busy handler d
17ad0 65 66 69 6e 65 64 20 66 6f 72 20 65 61 63 68 0a  efined for each.
17ae0 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
17af0 6e 65 63 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69  nection].  Setti
17b00 6e 67 20 61 20 6e 65 77 20 62 75 73 79 20 68 61  ng a new busy ha
17b10 6e 64 6c 65 72 20 63 6c 65 61 72 73 20 61 6e 79  ndler clears any
17b20 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73  .** previously s
17b30 65 74 20 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74  et handler.  Not
17b40 65 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b  e that calling [
17b50 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d  sqlite3_busy_tim
17b60 65 6f 75 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20  eout()].** will 
17b70 61 6c 73 6f 20 73 65 74 20 6f 72 20 63 6c 65 61  also set or clea
17b80 72 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c  r the busy handl
17b90 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75  er..**.** The bu
17ba0 73 79 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75  sy callback shou
17bb0 6c 64 20 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20  ld not take any 
17bc0 61 63 74 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f  actions which mo
17bd0 64 69 66 79 20 74 68 65 0a 2a 2a 20 64 61 74 61  dify the.** data
17be0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
17bf0 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65  that invoked the
17c00 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 20 20   busy handler.  
17c10 41 6e 79 20 73 75 63 68 20 61 63 74 69 6f 6e 73  Any such actions
17c20 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 75 6e  .** result in un
17c30 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72  defined behavior
17c40 2e 0a 2a 2a 20 0a 2a 2a 20 49 4e 56 41 52 49 41  ..** .** INVARIA
17c50 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33  NTS:.**.** {H123
17c60 31 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  11} The [sqlite3
17c70 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 44 2c  _busy_handler(D,
17c80 43 2c 41 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73  C,A)] function s
17c90 68 61 6c 6c 20 72 65 70 6c 61 63 65 0a 2a 2a 20  hall replace.** 
17ca0 20 20 20 20 20 20 20 20 20 62 75 73 79 20 63 61           busy ca
17cb0 6c 6c 62 61 63 6b 20 69 6e 20 74 68 65 20 5b 64  llback in the [d
17cc0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
17cd0 6f 6e 5d 20 44 20 77 69 74 68 20 61 20 6e 65 77  on] D with a new
17ce0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 6e  .**          a n
17cf0 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  ew busy handler 
17d00 43 20 61 6e 64 20 61 70 70 6c 69 63 61 74 69 6f  C and applicatio
17d10 6e 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 41  n data pointer A
17d20 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 32 7d  ..**.** {H12312}
17d30 20 4e 65 77 6c 79 20 63 72 65 61 74 65 64 20 5b   Newly created [
17d40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
17d50 69 6f 6e 73 5d 20 73 68 61 6c 6c 20 68 61 76 65  ions] shall have
17d60 20 61 20 62 75 73 79 0a 2a 2a 20 20 20 20 20 20   a busy.**      
17d70 20 20 20 20 68 61 6e 64 6c 65 72 20 6f 66 20 4e      handler of N
17d80 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33  ULL..**.** {H123
17d90 31 34 7d 20 57 68 65 6e 20 74 77 6f 20 6f 72 20  14} When two or 
17da0 6d 6f 72 65 20 5b 64 61 74 61 62 61 73 65 20 63  more [database c
17db0 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 73 68 61 72  onnections] shar
17dc0 65 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  e a.**          
17dd0 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f  [sqlite3_enable_
17de0 73 68 61 72 65 64 5f 63 61 63 68 65 20 7c 20 63  shared_cache | c
17df0 6f 6d 6d 6f 6e 20 63 61 63 68 65 5d 2c 0a 2a 2a  ommon cache],.**
17e00 20 20 20 20 20 20 20 20 20 20 74 68 65 20 62 75            the bu
17e10 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 74  sy handler for t
17e20 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
17e30 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79  ection currently
17e40 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20   using.**       
17e50 20 20 20 74 68 65 20 63 61 63 68 65 20 73 68 61     the cache sha
17e60 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  ll be invoked wh
17e70 65 6e 20 74 68 65 20 63 61 63 68 65 20 65 6e 63  en the cache enc
17e80 6f 75 6e 74 65 72 73 20 61 20 6c 6f 63 6b 2e 0a  ounters a lock..
17e90 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 36 7d 20 49  **.** {H12316} I
17ea0 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72  f a busy handler
17eb0 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
17ec0 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  s zero, then the
17ed0 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
17ee0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  e.**          th
17ef0 61 74 20 70 72 6f 76 6f 6b 65 64 20 74 68 65 20  at provoked the 
17f00 6c 6f 63 6b 69 6e 67 20 65 76 65 6e 74 20 73 68  locking event sh
17f10 61 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49  all return [SQLI
17f20 54 45 5f 42 55 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20  TE_BUSY]..**.** 
17f30 7b 48 31 32 33 31 38 7d 20 53 51 4c 69 74 65 20  {H12318} SQLite 
17f40 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 73 20 74 68  shall invokes th
17f50 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 77  e busy handler w
17f60 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74  ith two argument
17f70 73 20 77 68 69 63 68 0a 2a 2a 20 20 20 20 20 20  s which.**      
17f80 20 20 20 20 61 72 65 20 61 20 63 6f 70 79 20 6f      are a copy o
17f90 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 73 75  f the pointer su
17fa0 70 70 6c 69 65 64 20 62 79 20 74 68 65 20 33 72  pplied by the 3r
17fb0 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a  d parameter to.*
17fc0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
17fd0 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
17fe0 28 29 5d 20 61 6e 64 20 61 20 63 6f 75 6e 74 20  ()] and a count 
17ff0 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
18000 20 70 72 69 6f 72 0a 2a 2a 20 20 20 20 20 20 20   prior.**       
18010 20 20 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f     invocations o
18020 66 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c  f the busy handl
18030 65 72 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20  er for the same 
18040 6c 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 0a 2a  locking event..*
18050 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53  *.** ASSUMPTIONS
18060 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 33 31 39 7d  :.**.** {A12319}
18070 20 41 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20   A busy handler 
18080 6d 75 73 74 20 6e 6f 74 20 63 6c 6f 73 65 20 74  must not close t
18090 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
180a0 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ection.**       
180b0 20 20 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20     or [prepared 
180c0 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20  statement] that 
180d0 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79  invoked the busy
180e0 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c   handler..*/.SQL
180f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
18100 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
18110 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a  (sqlite3*, int(*
18120 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f  )(void*,int), vo
18130 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id*);../*.** CAP
18140 49 33 52 45 46 3a 20 53 65 74 20 41 20 42 75 73  I3REF: Set A Bus
18150 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 33 34  y Timeout {H1234
18160 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a  0} <S40410>.**.*
18170 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
18180 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33 5f 62  ets a [sqlite3_b
18190 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75  usy_handler | bu
181a0 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 61 74  sy handler] that
181b0 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20 61   sleeps.** for a
181c0 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f 75 6e   specified amoun
181d0 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e 20 61  t of time when a
181e0 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64   table is locked
181f0 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72 0a 2a  .  The handler.*
18200 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 6c  * will sleep mul
18210 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 69  tiple times unti
18220 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20  l at least "ms" 
18230 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20  milliseconds of 
18240 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76 65  sleeping.** have
18250 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b 48   accumulated. {H
18260 31 32 33 34 33 7d 20 41 66 74 65 72 20 22 6d 73  12343} After "ms
18270 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f  " milliseconds o
18280 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74  f sleeping,.** t
18290 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72  he handler retur
182a0 6e 73 20 30 20 77 68 69 63 68 20 63 61 75 73 65  ns 0 which cause
182b0 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  s [sqlite3_step(
182c0 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20  )] to return.** 
182d0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72  [SQLITE_BUSY] or
182e0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42   [SQLITE_IOERR_B
182f0 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43  LOCKED]..**.** C
18300 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
18310 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67 75  ine with an argu
18320 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f  ment less than o
18330 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a  r equal to zero.
18340 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c  ** turns off all
18350 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a   busy handlers..
18360 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20  **.** There can 
18370 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65  only be a single
18380 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f   busy handler fo
18390 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a  r a particular.*
183a0 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
183b0 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 20  ection] any any 
183c0 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49  given moment.  I
183d0 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20 68  f another busy h
183e0 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 65  andler.** was de
183f0 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b 73  fined  (using [s
18400 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
18410 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74 6f  ler()]) prior to
18420 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73   calling.** this
18430 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 6f   routine, that o
18440 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65  ther busy handle
18450 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2a  r is cleared..**
18460 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
18470 2a 2a 0a 2a 2a 20 7b 48 31 32 33 34 31 7d 20 54  **.** {H12341} T
18480 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  he [sqlite3_busy
18490 5f 74 69 6d 65 6f 75 74 28 29 5d 20 66 75 6e 63  _timeout()] func
184a0 74 69 6f 6e 20 73 68 61 6c 6c 20 6f 76 65 72 72  tion shall overr
184b0 69 64 65 20 61 6e 79 20 70 72 69 6f 72 0a 2a 2a  ide any prior.**
184c0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
184d0 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28  e3_busy_timeout(
184e0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 62  )] or [sqlite3_b
184f0 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 20 73  usy_handler()] s
18500 65 74 74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  etting.**       
18510 20 20 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b     on the same [
18520 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
18530 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  ion]..**.** {H12
18540 33 34 33 7d 20 49 66 20 74 68 65 20 32 6e 64 20  343} If the 2nd 
18550 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71  parameter to [sq
18560 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f  lite3_busy_timeo
18570 75 74 28 29 5d 20 69 73 20 6c 65 73 73 20 74 68  ut()] is less th
18580 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  an.**          o
18590 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c  r equal to zero,
185a0 20 74 68 65 6e 20 74 68 65 20 62 75 73 79 20 68   then the busy h
185b0 61 6e 64 6c 65 72 20 73 68 61 6c 6c 20 62 65 20  andler shall be 
185c0 63 6c 65 61 72 65 64 20 73 6f 20 74 68 61 74 0a  cleared so that.
185d0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 20  **          all 
185e0 73 75 62 73 65 71 75 65 6e 74 20 6c 6f 63 6b 69  subsequent locki
185f0 6e 67 20 65 76 65 6e 74 73 20 69 6d 6d 65 64 69  ng events immedi
18600 61 74 65 6c 79 20 72 65 74 75 72 6e 20 5b 53 51  ately return [SQ
18610 4c 49 54 45 5f 42 55 53 59 5d 2e 0a 2a 2a 0a 2a  LITE_BUSY]..**.*
18620 2a 20 7b 48 31 32 33 34 34 7d 20 49 66 20 74 68  * {H12344} If th
18630 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  e 2nd parameter 
18640 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  to [sqlite3_busy
18650 5f 74 69 6d 65 6f 75 74 28 29 5d 20 69 73 20 61  _timeout()] is a
18660 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 20 20 20   positive.**    
18670 20 20 20 20 20 20 6e 75 6d 62 65 72 20 4e 2c 20        number N, 
18680 74 68 65 6e 20 61 20 62 75 73 79 20 68 61 6e 64  then a busy hand
18690 6c 65 72 20 73 68 61 6c 6c 20 62 65 20 73 65 74  ler shall be set
186a0 20 74 68 61 74 20 72 65 70 65 61 74 65 64 6c 79   that repeatedly
186b0 20 63 61 6c 6c 73 0a 2a 2a 20 20 20 20 20 20 20   calls.**       
186c0 20 20 20 74 68 65 20 78 53 6c 65 65 70 28 29 20     the xSleep() 
186d0 6d 65 74 68 6f 64 20 69 6e 20 74 68 65 20 5b 73  method in the [s
186e0 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 56 46 53  qlite3_vfs | VFS
186f0 20 69 6e 74 65 72 66 61 63 65 5d 20 75 6e 74 69   interface] unti
18700 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65 69  l.**          ei
18710 74 68 65 72 20 74 68 65 20 6c 6f 63 6b 20 63 6c  ther the lock cl
18720 65 61 72 73 20 6f 72 20 75 6e 74 69 6c 20 74 68  ears or until th
18730 65 20 63 75 6d 75 6c 61 74 69 76 65 20 73 6c 65  e cumulative sle
18740 65 70 20 74 69 6d 65 0a 2a 2a 20 20 20 20 20 20  ep time.**      
18750 20 20 20 20 72 65 70 6f 72 74 65 64 20 62 61 63      reported bac
18760 6b 20 62 79 20 78 53 6c 65 65 70 28 29 20 65 78  k by xSleep() ex
18770 63 65 65 64 73 20 4e 20 6d 69 6c 6c 69 73 65 63  ceeds N millisec
18780 6f 6e 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  onds..*/.SQLITE_
18790 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
187a0 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 73 71 6c  busy_timeout(sql
187b0 69 74 65 33 2a 2c 20 69 6e 74 20 6d 73 29 3b 0a  ite3*, int ms);.
187c0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
187d0 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 52 6f 75   Convenience Rou
187e0 74 69 6e 65 73 20 46 6f 72 20 52 75 6e 6e 69 6e  tines For Runnin
187f0 67 20 51 75 65 72 69 65 73 20 7b 48 31 32 33 37  g Queries {H1237
18800 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a  0} <S10000>.**.*
18810 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a 20 41 20  * Definition: A 
18820 3c 62 3e 72 65 73 75 6c 74 20 74 61 62 6c 65 3c  <b>result table<
18830 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79 20 64 61  /b> is memory da
18840 74 61 20 73 74 72 75 63 74 75 72 65 20 63 72 65  ta structure cre
18850 61 74 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 5b  ated by the.** [
18860 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
18870 65 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20  e()] interface. 
18880 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20   A result table 
18890 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20 63  records the.** c
188a0 6f 6d 70 6c 65 74 65 20 71 75 65 72 79 20 72 65  omplete query re
188b0 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e 65 20 6f  sults from one o
188c0 72 20 6d 6f 72 65 20 71 75 65 72 69 65 73 2e 0a  r more queries..
188d0 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
188e0 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 68 61 73  conceptually has
188f0 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77   a number of row
18900 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 2e 20 20  s and columns.  
18910 42 75 74 0a 2a 2a 20 74 68 65 73 65 20 6e 75 6d  But.** these num
18920 62 65 72 73 20 61 72 65 20 6e 6f 74 20 70 61 72  bers are not par
18930 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  t of the result 
18940 74 61 62 6c 65 20 69 74 73 65 6c 66 2e 20 20 54  table itself.  T
18950 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65 72 73 20  hese.** numbers 
18960 61 72 65 20 6f 62 74 61 69 6e 65 64 20 73 65 70  are obtained sep
18970 61 72 61 74 65 6c 79 2e 20 20 4c 65 74 20 4e 20  arately.  Let N 
18980 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  be the number of
18990 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20 4d 20 62   rows.** and M b
189a0 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
189b0 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41  columns..**.** A
189c0 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 69 73   result table is
189d0 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69   an array of poi
189e0 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f 2d 74 65  nters to zero-te
189f0 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73  rminated UTF-8 s
18a00 74 72 69 6e 67 73 2e 0a 2a 2a 20 54 68 65 72 65  trings..** There
18a10 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20 65 6c 65   are (N+1)*M ele
18a20 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 72 72  ments in the arr
18a30 61 79 2e 20 20 54 68 65 20 66 69 72 73 74 20 4d  ay.  The first M
18a40 20 70 6f 69 6e 74 65 72 73 20 70 6f 69 6e 74 0a   pointers point.
18a50 2a 2a 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69  ** to zero-termi
18a60 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74 68  nated strings th
18a70 61 74 20 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  at  contain the 
18a80 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f 6c  names of the col
18a90 75 6d 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65 6d  umns..** The rem
18aa0 61 69 6e 69 6e 67 20 65 6e 74 72 69 65 73 20 61  aining entries a
18ab0 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 71 75 65 72  ll point to quer
18ac0 79 20 72 65 73 75 6c 74 73 2e 20 20 4e 55 4c 4c  y results.  NULL
18ad0 20 76 61 6c 75 65 73 20 72 65 73 75 6c 74 0a 2a   values result.*
18ae0 2a 20 69 6e 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  * in NULL pointe
18af0 72 73 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 76  rs.  All other v
18b00 61 6c 75 65 73 20 61 72 65 20 69 6e 20 74 68 65  alues are in the
18b10 69 72 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 65  ir UTF-8 zero-te
18b20 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 69  rminated.** stri
18b30 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
18b40 6e 20 61 73 20 72 65 74 75 72 6e 65 64 20 62 79  n as returned by
18b50 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
18b60 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  _text()]..**.** 
18b70 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 6d  A result table m
18b80 69 67 68 74 20 63 6f 6e 73 69 73 74 20 6f 66 20  ight consist of 
18b90 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6d 65 6d 6f  one or more memo
18ba0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a  ry allocations..
18bb0 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66  ** It is not saf
18bc0 65 20 74 6f 20 70 61 73 73 20 61 20 72 65 73 75  e to pass a resu
18bd0 6c 74 20 74 61 62 6c 65 20 64 69 72 65 63 74 6c  lt table directl
18be0 79 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72  y to [sqlite3_fr
18bf0 65 65 28 29 5d 2e 0a 2a 2a 20 41 20 72 65 73 75  ee()]..** A resu
18c00 6c 74 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20  lt table should 
18c10 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 75  be deallocated u
18c20 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 72  sing [sqlite3_fr
18c30 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a 2a 2a 0a  ee_table()]..**.
18c40 2a 2a 20 41 73 20 61 6e 20 65 78 61 6d 70 6c 65  ** As an example
18c50 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 74   of the result t
18c60 61 62 6c 65 20 66 6f 72 6d 61 74 2c 20 73 75 70  able format, sup
18c70 70 6f 73 65 20 61 20 71 75 65 72 79 20 72 65 73  pose a query res
18c80 75 6c 74 0a 2a 2a 20 69 73 20 61 73 20 66 6f 6c  ult.** is as fol
18c90 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  lows:.**.** <blo
18ca0 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
18cb0 20 20 20 20 20 20 20 20 4e 61 6d 65 20 20 20 20          Name    
18cc0 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20 20 20 20      | Age.**    
18cd0 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      ------------
18ce0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
18cf0 20 20 20 20 20 20 41 6c 69 63 65 20 20 20 20 20        Alice     
18d00 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20 20 20 20    | 43.**       
18d10 20 42 6f 62 20 20 20 20 20 20 20 20 20 7c 20 32   Bob         | 2
18d20 38 0a 2a 2a 20 20 20 20 20 20 20 20 43 69 6e 64  8.**        Cind
18d30 79 20 20 20 20 20 20 20 7c 20 32 31 0a 2a 2a 20  y       | 21.** 
18d40 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f  </pre></blockquo
18d50 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  te>.**.** There 
18d60 61 72 65 20 74 77 6f 20 63 6f 6c 75 6d 6e 20 28  are two column (
18d70 4d 3d 3d 32 29 20 61 6e 64 20 74 68 72 65 65 20  M==2) and three 
18d80 72 6f 77 73 20 28 4e 3d 3d 33 29 2e 20 20 54 68  rows (N==3).  Th
18d90 75 73 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74  us the.** result
18da0 20 74 61 62 6c 65 20 68 61 73 20 38 20 65 6e 74   table has 8 ent
18db0 72 69 65 73 2e 20 20 53 75 70 70 6f 73 65 20 74  ries.  Suppose t
18dc0 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20  he result table 
18dd0 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  is stored.** in 
18de0 61 6e 20 61 72 72 61 79 20 6e 61 6d 65 73 20 61  an array names a
18df0 7a 52 65 73 75 6c 74 2e 20 20 54 68 65 6e 20 61  zResult.  Then a
18e00 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73 20 74 68  zResult holds th
18e10 69 73 20 63 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a  is content:.**.*
18e20 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
18e30 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a  re>.**        az
18e40 52 65 73 75 6c 74 26 23 39 31 3b 30 5d 20 3d 20  Result&#91;0] = 
18e50 22 4e 61 6d 65 22 3b 0a 2a 2a 20 20 20 20 20 20  "Name";.**      
18e60 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 31    azResult&#91;1
18e70 5d 20 3d 20 22 41 67 65 22 3b 0a 2a 2a 20 20 20  ] = "Age";.**   
18e80 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39       azResult&#9
18e90 31 3b 32 5d 20 3d 20 22 41 6c 69 63 65 22 3b 0a  1;2] = "Alice";.
18ea0 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
18eb0 6c 74 26 23 39 31 3b 33 5d 20 3d 20 22 34 33 22  lt&#91;3] = "43"
18ec0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65  ;.**        azRe
18ed0 73 75 6c 74 26 23 39 31 3b 34 5d 20 3d 20 22 42  sult&#91;4] = "B
18ee0 6f 62 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61  ob";.**        a
18ef0 7a 52 65 73 75 6c 74 26 23 39 31 3b 35 5d 20 3d  zResult&#91;5] =
18f00 20 22 32 38 22 3b 0a 2a 2a 20 20 20 20 20 20 20   "28";.**       
18f10 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 36 5d   azResult&#91;6]
18f20 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a 2a 20 20   = "Cindy";.**  
18f30 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23        azResult&#
18f40 39 31 3b 37 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a  91;7] = "21";.**
18f50 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75   </pre></blockqu
18f60 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ote>.**.** The s
18f70 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
18f80 28 29 20 66 75 6e 63 74 69 6f 6e 20 65 76 61 6c  () function eval
18f90 75 61 74 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  uates one or mor
18fa0 65 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73  e.** semicolon-s
18fb0 65 70 61 72 61 74 65 64 20 53 51 4c 20 73 74 61  eparated SQL sta
18fc0 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 7a  tements in the z
18fd0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  ero-terminated U
18fe0 54 46 2d 38 0a 2a 2a 20 73 74 72 69 6e 67 20 6f  TF-8.** string o
18ff0 66 20 69 74 73 20 32 6e 64 20 70 61 72 61 6d 65  f its 2nd parame
19000 74 65 72 2e 20 20 49 74 20 72 65 74 75 72 6e 73  ter.  It returns
19010 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20   a result table 
19020 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65  to the.** pointe
19030 72 20 67 69 76 65 6e 20 69 6e 20 69 74 73 20 33  r given in its 3
19040 72 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  rd parameter..**
19050 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 63 61  .** After the ca
19060 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 68  lling function h
19070 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e  as finished usin
19080 67 20 74 68 65 20 72 65 73 75 6c 74 2c 20 69 74  g the result, it
19090 20 73 68 6f 75 6c 64 0a 2a 2a 20 70 61 73 73 20   should.** pass 
190a0 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  the pointer to t
190b0 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20  he result table 
190c0 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  to sqlite3_free_
190d0 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 64 65 72  table() in order
190e0 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 73 65 20 74   to.** release t
190f0 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 77  he memory that w
19100 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20 20 42 65  as malloced.  Be
19110 63 61 75 73 65 20 6f 66 20 74 68 65 20 77 61 79  cause of the way
19120 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
19130 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61 70 70 65  _malloc()] happe
19140 6e 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65  ns within sqlite
19150 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 2c 20 74  3_get_table(), t
19160 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75  he calling.** fu
19170 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20  nction must not 
19180 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b 73 71 6c  try to call [sql
19190 69 74 65 33 5f 66 72 65 65 28 29 5d 20 64 69 72  ite3_free()] dir
191a0 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0a 2a 2a 20  ectly.  Only.** 
191b0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61  [sqlite3_free_ta
191c0 62 6c 65 28 29 5d 20 69 73 20 61 62 6c 65 20 74  ble()] is able t
191d0 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65  o release the me
191e0 6d 6f 72 79 20 70 72 6f 70 65 72 6c 79 20 61 6e  mory properly an
191f0 64 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20  d safely..**.** 
19200 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  The sqlite3_get_
19210 74 61 62 6c 65 28 29 20 69 6e 74 65 72 66 61 63  table() interfac
19220 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  e is implemented
19230 20 61 73 20 61 20 77 72 61 70 70 65 72 20 61 72   as a wrapper ar
19240 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ound.** [sqlite3
19250 5f 65 78 65 63 28 29 5d 2e 20 20 54 68 65 20 73  _exec()].  The s
19260 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
19270 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  () routine does 
19280 6e 6f 74 20 68 61 76 65 20 61 63 63 65 73 73 0a  not have access.
19290 2a 2a 20 74 6f 20 61 6e 79 20 69 6e 74 65 72 6e  ** to any intern
192a0 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72  al data structur
192b0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 49  es of SQLite.  I
192c0 74 20 75 73 65 73 20 6f 6e 6c 79 20 74 68 65 20  t uses only the 
192d0 70 75 62 6c 69 63 0a 2a 2a 20 69 6e 74 65 72 66  public.** interf
192e0 61 63 65 20 64 65 66 69 6e 65 64 20 68 65 72 65  ace defined here
192f0 2e 20 20 41 73 20 61 20 63 6f 6e 73 65 71 75 65  .  As a conseque
19300 6e 63 65 2c 20 65 72 72 6f 72 73 20 74 68 61 74  nce, errors that
19310 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a   occur in the.**
19320 20 77 72 61 70 70 65 72 20 6c 61 79 65 72 20 6f   wrapper layer o
19330 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 69 6e  utside of the in
19340 74 65 72 6e 61 6c 20 5b 73 71 6c 69 74 65 33 5f  ternal [sqlite3_
19350 65 78 65 63 28 29 5d 20 63 61 6c 6c 20 61 72 65  exec()] call are
19360 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65 63 74 65   not.** reflecte
19370 64 20 69 6e 20 73 75 62 73 65 71 75 65 6e 74 20  d in subsequent 
19380 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
19390 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 6f 72 20  3_errcode()] or 
193a0 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  [sqlite3_errmsg(
193b0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  )]..**.** INVARI
193c0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  ANTS:.**.** {H12
193d0 33 37 31 7d 20 49 66 20 61 20 5b 73 71 6c 69 74  371} If a [sqlit
193e0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20  e3_get_table()] 
193f0 66 61 69 6c 73 20 61 20 6d 65 6d 6f 72 79 20 61  fails a memory a
19400 6c 6c 6f 63 61 74 69 6f 6e 2c 20 74 68 65 6e 0a  llocation, then.
19410 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 73  **          it s
19420 68 61 6c 6c 20 66 72 65 65 20 74 68 65 20 72 65  hall free the re
19430 73 75 6c 74 20 74 61 62 6c 65 20 75 6e 64 65 72  sult table under
19440 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2c 20 61   construction, a
19450 62 6f 72 74 20 74 68 65 0a 2a 2a 20 20 20 20 20  bort the.**     
19460 20 20 20 20 20 71 75 65 72 79 20 69 6e 20 70 72       query in pr
19470 6f 63 65 73 73 2c 20 73 6b 69 70 20 61 6e 79 20  ocess, skip any 
19480 73 75 62 73 65 71 75 65 6e 74 20 71 75 65 72 69  subsequent queri
19490 65 73 2c 20 73 65 74 20 74 68 65 0a 2a 2a 20 20  es, set the.**  
194a0 20 20 20 20 20 20 20 20 2a 70 61 7a 52 65 73 75          *pazResu
194b0 6c 74 20 6f 75 74 70 75 74 20 70 6f 69 6e 74 65  lt output pointe
194c0 72 20 74 6f 20 4e 55 4c 4c 20 61 6e 64 20 72 65  r to NULL and re
194d0 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d  turn [SQLITE_NOM
194e0 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33  EM]..**.** {H123
194f0 37 33 7d 20 49 66 20 74 68 65 20 70 6e 43 6f 6c  73} If the pnCol
19500 75 6d 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f  umn parameter to
19510 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61   [sqlite3_get_ta
19520 62 6c 65 28 29 5d 20 69 73 20 6e 6f 74 20 4e 55  ble()] is not NU
19530 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  LL.**          t
19540 68 65 6e 20 61 20 73 75 63 63 65 73 73 66 75 6c  hen a successful
19550 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b   invocation of [
19560 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
19570 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20 20 20  e()] shall.**   
19580 20 20 20 20 20 20 20 77 72 69 74 65 20 74 68 65         write the
19590 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
195a0 6e 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  ns in the.**    
195b0 20 20 20 20 20 20 72 65 73 75 6c 74 20 73 65 74        result set
195c0 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 69 6e   of the query in
195d0 74 6f 20 2a 70 6e 43 6f 6c 75 6d 6e 2e 0a 2a 2a  to *pnColumn..**
195e0 0a 2a 2a 20 7b 48 31 32 33 37 34 7d 20 49 66 20  .** {H12374} If 
195f0 74 68 65 20 70 6e 52 6f 77 20 70 61 72 61 6d 65  the pnRow parame
19600 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ter to [sqlite3_
19610 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 69 73 20  get_table()] is 
19620 6e 6f 74 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20  not NULL.**     
19630 20 20 20 20 20 74 68 65 6e 20 61 20 73 75 63 63       then a succ
19640 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f  essful invocatio
19650 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 67 65  n of [sqlite3_ge
19660 74 5f 74 61 62 6c 65 28 29 5d 20 73 68 61 6c 6c  t_table()] shall
19670 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 72 69  .**          wri
19680 74 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  tes the number o
19690 66 20 72 6f 77 73 20 69 6e 20 74 68 65 0a 2a 2a  f rows in the.**
196a0 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74            result
196b0 20 73 65 74 20 6f 66 20 74 68 65 20 71 75 65 72   set of the quer
196c0 79 20 69 6e 74 6f 20 2a 70 6e 52 6f 77 2e 0a 2a  y into *pnRow..*
196d0 2a 0a 2a 2a 20 7b 48 31 32 33 37 36 7d 20 41 20  *.** {H12376} A 
196e0 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63  successful invoc
196f0 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65  ation of [sqlite
19700 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 74  3_get_table()] t
19710 68 61 74 20 63 6f 6d 70 75 74 65 73 0a 2a 2a 20  hat computes.** 
19720 20 20 20 20 20 20 20 20 20 4e 20 72 6f 77 73 20           N rows 
19730 6f 66 20 72 65 73 75 6c 74 20 77 69 74 68 20 43  of result with C
19740 20 63 6f 6c 75 6d 6e 73 20 70 65 72 20 72 6f 77   columns per row
19750 20 73 68 61 6c 6c 20 6d 61 6b 65 20 2a 70 61 7a   shall make *paz
19760 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20  Result.**       
19770 20 20 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61     point to an a
19780 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73  rray of pointers
19790 20 74 6f 20 28 4e 2b 31 29 2a 43 20 73 74 72 69   to (N+1)*C stri
197a0 6e 67 73 20 77 68 65 72 65 20 74 68 65 20 66 69  ngs where the fi
197b0 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  rst.**          
197c0 43 20 73 74 72 69 6e 67 73 20 61 72 65 20 63 6f  C strings are co
197d0 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 73 20 6f 62  lumn names as ob
197e0 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 20  tained from.**  
197f0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
19800 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20  _column_name()] 
19810 61 6e 64 20 74 68 65 20 72 65 73 74 20 61 72 65  and the rest are
19820 20 63 6f 6c 75 6d 6e 20 72 65 73 75 6c 74 20 76   column result v
19830 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20  alues.**        
19840 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20    obtained from 
19850 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
19860 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  text()]..**.** {
19870 48 31 32 33 37 39 7d 20 54 68 65 20 76 61 6c 75  H12379} The valu
19880 65 73 20 69 6e 20 74 68 65 20 70 61 7a 52 65 73  es in the pazRes
19890 75 6c 74 20 61 72 72 61 79 20 72 65 74 75 72 6e  ult array return
198a0 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 67  ed by [sqlite3_g
198b0 65 74 5f 74 61 62 6c 65 28 29 5d 0a 2a 2a 20 20  et_table()].**  
198c0 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 72 65          shall re
198d0 6d 61 69 6e 20 76 61 6c 69 64 20 75 6e 74 69 6c  main valid until
198e0 20 63 6c 65 61 72 65 64 20 62 79 20 5b 73 71 6c   cleared by [sql
198f0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
19900 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 38  )]..**.** {H1238
19910 32 7d 20 57 68 65 6e 20 61 6e 20 65 72 72 6f 72  2} When an error
19920 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 65   occurs during e
19930 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b 73 71  valuation of [sq
19940 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
19950 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  )].**          t
19960 68 65 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c  he function shal
19970 6c 20 73 65 74 20 2a 70 61 7a 52 65 73 75 6c 74  l set *pazResult
19980 20 74 6f 20 4e 55 4c 4c 2c 20 77 72 69 74 65 20   to NULL, write 
19990 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
199a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 74  .**          int
199b0 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
199c0 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
199d0 6d 61 6c 6c 6f 63 28 29 5d 2c 20 6d 61 6b 65 0a  malloc()], make.
199e0 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 2a 70 7a  **          **pz
199f0 45 72 72 6d 73 67 20 70 6f 69 6e 74 20 74 6f 20  Errmsg point to 
19a00 74 68 61 74 20 65 72 72 6f 72 20 6d 65 73 73 61  that error messa
19a10 67 65 2c 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ge, and return a
19a20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 70 70  .**          app
19a30 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20  ropriate [error 
19a40 63 6f 64 65 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  code]..*/.SQLITE
19a50 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
19a60 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20 73 71  _get_table(.  sq
19a70 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
19a80 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64      /* An open d
19a90 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e  atabase */.  con
19aa0 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
19ab0 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20     /* SQL to be 
19ac0 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 63  evaluated */.  c
19ad0 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74  har ***pazResult
19ae0 2c 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20  ,    /* Results 
19af0 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a  of the query */.
19b00 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20    int *pnRow,   
19b10 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
19b20 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f 77 73  r of result rows
19b30 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
19b40 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e  .  int *pnColumn
19b50 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ,        /* Numb
19b60 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c  er of result col
19b70 75 6d 6e 73 20 77 72 69 74 74 65 6e 20 68 65 72  umns written her
19b80 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  e */.  char **pz
19b90 45 72 72 6d 73 67 20 20 20 20 20 20 20 2f 2a 20  Errmsg       /* 
19ba0 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74 74 65  Error msg writte
19bb0 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c  n here */.);.SQL
19bc0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
19bd0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
19be0 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 29 3b 0a  char **result);.
19bf0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
19c00 20 46 6f 72 6d 61 74 74 65 64 20 53 74 72 69 6e   Formatted Strin
19c10 67 20 50 72 69 6e 74 69 6e 67 20 46 75 6e 63 74  g Printing Funct
19c20 69 6f 6e 73 20 7b 48 31 37 34 30 30 7d 20 3c 53  ions {H17400} <S
19c30 37 30 30 30 30 3e 3c 53 32 30 30 30 30 3e 0a 2a  70000><S20000>.*
19c40 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
19c50 6e 65 73 20 61 72 65 20 77 6f 72 6b 61 6c 69 6b  nes are workalik
19c60 65 73 20 6f 66 20 74 68 65 20 22 70 72 69 6e 74  es of the "print
19c70 66 28 29 22 20 66 61 6d 69 6c 79 20 6f 66 20 66  f()" family of f
19c80 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d  unctions.** from
19c90 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20   the standard C 
19ca0 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54  library..**.** T
19cb0 68 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  he sqlite3_mprin
19cc0 74 66 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  tf() and sqlite3
19cd0 5f 76 6d 70 72 69 6e 74 66 28 29 20 72 6f 75 74  _vmprintf() rout
19ce0 69 6e 65 73 20 77 72 69 74 65 20 74 68 65 69 72  ines write their
19cf0 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f  .** results into
19d00 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
19d10 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
19d20 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 54 68 65  alloc()]..** The
19d30 20 73 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65   strings returne
19d40 64 20 62 79 20 74 68 65 73 65 20 74 77 6f 20 72  d by these two r
19d50 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 62  outines should b
19d60 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 62 79  e.** released by
19d70 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
19d80 5d 2e 20 20 42 6f 74 68 20 72 6f 75 74 69 6e 65  ].  Both routine
19d90 73 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55  s return a.** NU
19da0 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 5b 73  LL pointer if [s
19db0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
19dc0 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c   is unable to al
19dd0 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 0a 2a 2a  locate enough.**
19de0 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20   memory to hold 
19df0 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74  the resulting st
19e00 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73  ring..**.** In s
19e10 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
19e20 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d  ) routine is sim
19e30 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 69 6e 74  ilar to "snprint
19e40 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  f()" from.** the
19e50 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72   standard C libr
19e60 61 72 79 2e 20 20 54 68 65 20 72 65 73 75 6c 74  ary.  The result
19e70 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
19e80 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 73   the.** buffer s
19e90 75 70 70 6c 69 65 64 20 61 73 20 74 68 65 20 73  upplied as the s
19ea0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
19eb0 77 68 6f 73 65 20 73 69 7a 65 20 69 73 20 67 69  whose size is gi
19ec0 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 69  ven by.** the fi
19ed0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 4e  rst parameter. N
19ee0 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64  ote that the ord
19ef0 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72  er of the.** fir
19f00 73 74 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72  st two parameter
19f10 73 20 69 73 20 72 65 76 65 72 73 65 64 20 66 72  s is reversed fr
19f20 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29 2e 20 20  om snprintf().  
19f30 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a 20 68 69  This is an.** hi
19f40 73 74 6f 72 69 63 61 6c 20 61 63 63 69 64 65 6e  storical acciden
19f50 74 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65  t that cannot be
19f60 20 66 69 78 65 64 20 77 69 74 68 6f 75 74 20 62   fixed without b
19f70 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61 63 6b 77  reaking.** backw
19f80 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
19f90 74 79 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74  ty.  Note also t
19fa0 68 61 74 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  hat sqlite3_snpr
19fb0 69 6e 74 66 28 29 0a 2a 2a 20 72 65 74 75 72 6e  intf().** return
19fc0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
19fd0 74 73 20 62 75 66 66 65 72 20 69 6e 73 74 65 61  ts buffer instea
19fe0 64 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  d of the number 
19ff0 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  of.** characters
1a000 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65   actually writte
1a010 6e 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65  n into the buffe
1a020 72 2e 20 20 57 65 20 61 64 6d 69 74 20 74 68 61  r.  We admit tha
1a030 74 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  t.** the number 
1a040 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 77 72  of characters wr
1a050 69 74 74 65 6e 20 77 6f 75 6c 64 20 62 65 20 61  itten would be a
1a060 20 6d 6f 72 65 20 75 73 65 66 75 6c 20 72 65 74   more useful ret
1a070 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 62 75 74  urn.** value but
1a080 20 77 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67   we cannot chang
1a090 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  e the implementa
1a0a0 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f  tion of sqlite3_
1a0b0 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f  snprintf().** no
1a0c0 77 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69  w without breaki
1a0d0 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ng compatibility
1a0e0 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20  ..**.** As long 
1a0f0 61 73 20 74 68 65 20 62 75 66 66 65 72 20 73 69  as the buffer si
1a100 7a 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ze is greater th
1a110 61 6e 20 7a 65 72 6f 2c 20 73 71 6c 69 74 65 33  an zero, sqlite3
1a120 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 67  _snprintf().** g
1a130 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74  uarantees that t
1a140 68 65 20 62 75 66 66 65 72 20 69 73 20 61 6c 77  he buffer is alw
1a150 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  ays zero-termina
1a160 74 65 64 2e 20 20 54 68 65 20 66 69 72 73 74 0a  ted.  The first.
1a170 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 22 6e 22  ** parameter "n"
1a180 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 73 69   is the total si
1a190 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  ze of the buffer
1a1a0 2c 20 69 6e 63 6c 75 64 69 6e 67 20 73 70 61 63  , including spac
1a1b0 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 7a 65 72  e for.** the zer
1a1c0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 53  o terminator.  S
1a1d0 6f 20 74 68 65 20 6c 6f 6e 67 65 73 74 20 73 74  o the longest st
1a1e0 72 69 6e 67 20 74 68 61 74 20 63 61 6e 20 62 65  ring that can be
1a1f0 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 77   completely.** w
1a200 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 6e  ritten will be n
1a210 2d 31 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a  -1 characters..*
1a220 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
1a230 6e 65 73 20 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e  nes all implemen
1a240 74 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61  t some additiona
1a250 6c 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20  l formatting.** 
1a260 6f 70 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65  options that are
1a270 20 75 73 65 66 75 6c 20 66 6f 72 20 63 6f 6e 73   useful for cons
1a280 74 72 75 63 74 69 6e 67 20 53 51 4c 20 73 74 61  tructing SQL sta
1a290 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20  tements..** All 
1a2a0 6f 66 20 74 68 65 20 75 73 75 61 6c 20 70 72 69  of the usual pri
1a2b0 6e 74 66 28 29 20 66 6f 72 6d 61 74 74 69 6e 67  ntf() formatting
1a2c0 20 6f 70 74 69 6f 6e 73 20 61 70 70 6c 79 2e 20   options apply. 
1a2d0 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 74 68   In addition, th
1a2e0 65 72 65 0a 2a 2a 20 69 73 20 61 72 65 20 22 25  ere.** is are "%
1a2f0 71 22 2c 20 22 25 51 22 2c 20 61 6e 64 20 22 25  q", "%Q", and "%
1a300 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  z" options..**.*
1a310 2a 20 54 68 65 20 25 71 20 6f 70 74 69 6f 6e 20  * The %q option 
1a320 77 6f 72 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e  works like %s in
1a330 20 74 68 61 74 20 69 74 20 73 75 62 73 74 69 74   that it substit
1a340 75 74 65 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d  utes a null-term
1a350 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67  inated.** string
1a360 20 66 72 6f 6d 20 74 68 65 20 61 72 67 75 6d 65   from the argume
1a370 6e 74 20 6c 69 73 74 2e 20 20 42 75 74 20 25 71  nt list.  But %q
1a380 20 61 6c 73 6f 20 64 6f 75 62 6c 65 73 20 65 76   also doubles ev
1a390 65 72 79 20 27 5c 27 27 20 63 68 61 72 61 63 74  ery '\'' charact
1a3a0 65 72 2e 0a 2a 2a 20 25 71 20 69 73 20 64 65 73  er..** %q is des
1a3b0 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 69 6e  igned for use in
1a3c0 73 69 64 65 20 61 20 73 74 72 69 6e 67 20 6c 69  side a string li
1a3d0 74 65 72 61 6c 2e 20 20 42 79 20 64 6f 75 62 6c  teral.  By doubl
1a3e0 69 6e 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a  ing each '\''.**
1a3f0 20 63 68 61 72 61 63 74 65 72 20 69 74 20 65 73   character it es
1a400 63 61 70 65 73 20 74 68 61 74 20 63 68 61 72 61  capes that chara
1a410 63 74 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20  cter and allows 
1a420 69 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  it to be inserte
1a430 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74  d into.** the st
1a440 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  ring..**.** For 
1a450 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20  example, assume 
1a460 74 68 65 20 73 74 72 69 6e 67 20 76 61 72 69 61  the string varia
1a470 62 6c 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69  ble zText contai
1a480 6e 73 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f  ns text as follo
1a490 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ws:.**.** <block
1a4a0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
1a4b0 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 22 49  char *zText = "I
1a4c0 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21  t's a happy day!
1a4d0 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c  ";.** </pre></bl
1a4e0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20  ockquote>.**.** 
1a4f0 4f 6e 65 20 63 61 6e 20 75 73 65 20 74 68 69 73  One can use this
1a500 20 74 65 78 74 20 69 6e 20 61 6e 20 53 51 4c 20   text in an SQL 
1a510 73 74 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c  statement as fol
1a520 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  lows:.**.** <blo
1a530 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
1a540 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73    char *zSQL = s
1a550 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
1a560 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c  INSERT INTO tabl
1a570 65 20 56 41 4c 55 45 53 28 27 25 71 27 29 22 2c  e VALUES('%q')",
1a580 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c   zText);.**  sql
1a590 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53  ite3_exec(db, zS
1a5a0 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a  QL, 0, 0, 0);.**
1a5b0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
1a5c0 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  SQL);.** </pre><
1a5d0 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
1a5e0 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 25  ** Because the %
1a5f0 71 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  q format string 
1a600 69 73 20 75 73 65 64 2c 20 74 68 65 20 27 5c 27  is used, the '\'
1a610 27 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a  ' character in z
1a620 54 65 78 74 0a 2a 2a 20 69 73 20 65 73 63 61 70  Text.** is escap
1a630 65 64 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67  ed and the SQL g
1a640 65 6e 65 72 61 74 65 64 20 69 73 20 61 73 20 66  enerated is as f
1a650 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62  ollows:.**.** <b
1a660 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a  lockquote><pre>.
1a670 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  **  INSERT INTO 
1a680 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49  table1 VALUES('I
1a690 74 27 27 73 20 61 20 68 61 70 70 79 20 64 61 79  t''s a happy day
1a6a0 21 27 29 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  !').** </pre></b
1a6b0 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
1a6c0 20 54 68 69 73 20 69 73 20 63 6f 72 72 65 63 74   This is correct
1a6d0 2e 20 20 48 61 64 20 77 65 20 75 73 65 64 20 25  .  Had we used %
1a6e0 73 20 69 6e 73 74 65 61 64 20 6f 66 20 25 71 2c  s instead of %q,
1a6f0 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 53   the generated S
1a700 51 4c 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65  QL.** would have
1a710 20 6c 6f 6f 6b 65 64 20 6c 69 6b 65 20 74 68 69   looked like thi
1a720 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  s:.**.** <blockq
1a730 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49  uote><pre>.**  I
1a740 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65  NSERT INTO table
1a750 31 20 56 41 4c 55 45 53 28 27 49 74 27 73 20 61  1 VALUES('It's a
1a760 20 68 61 70 70 79 20 64 61 79 21 27 29 3b 0a 2a   happy day!');.*
1a770 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
1a780 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  uote>.**.** This
1a790 20 73 65 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20   second example 
1a7a0 69 73 20 61 6e 20 53 51 4c 20 73 79 6e 74 61 78  is an SQL syntax
1a7b0 20 65 72 72 6f 72 2e 20 20 41 73 20 61 20 67 65   error.  As a ge
1a7c0 6e 65 72 61 6c 20 72 75 6c 65 20 79 6f 75 20 73  neral rule you s
1a7d0 68 6f 75 6c 64 0a 2a 2a 20 61 6c 77 61 79 73 20  hould.** always 
1a7e0 75 73 65 20 25 71 20 69 6e 73 74 65 61 64 20 6f  use %q instead o
1a7f0 66 20 25 73 20 77 68 65 6e 20 69 6e 73 65 72 74  f %s when insert
1a800 69 6e 67 20 74 65 78 74 20 69 6e 74 6f 20 61 20  ing text into a 
1a810 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 0a  string literal..
1a820 2a 2a 0a 2a 2a 20 54 68 65 20 25 51 20 6f 70 74  **.** The %Q opt
1a830 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25  ion works like %
1a840 71 20 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f  q except it also
1a850 20 61 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f   adds single quo
1a860 74 65 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68  tes around.** th
1a870 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65  e outside of the
1a880 20 74 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20   total string.  
1a890 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66  Additionally, if
1a8a0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
1a8b0 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  n the.** argumen
1a8c0 74 20 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c  t list is a NULL
1a8d0 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62   pointer, %Q sub
1a8e0 73 74 69 74 75 74 65 73 20 74 68 65 20 74 65 78  stitutes the tex
1a8f0 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75  t "NULL" (withou
1a900 74 0a 2a 2a 20 73 69 6e 67 6c 65 20 71 75 6f 74  t.** single quot
1a910 65 73 29 20 69 6e 20 70 6c 61 63 65 20 6f 66 20  es) in place of 
1a920 74 68 65 20 25 51 20 6f 70 74 69 6f 6e 2e 20 20  the %Q option.  
1a930 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  So, for example,
1a940 20 6f 6e 65 20 63 6f 75 6c 64 20 73 61 79 3a 0a   one could say:.
1a950 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
1a960 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72  e><pre>.**  char
1a970 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33   *zSQL = sqlite3
1a980 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54  _mprintf("INSERT
1a990 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55   INTO table VALU
1a9a0 45 53 28 25 51 29 22 2c 20 7a 54 65 78 74 29 3b  ES(%Q)", zText);
1a9b0 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65  .**  sqlite3_exe
1a9c0 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30  c(db, zSQL, 0, 0
1a9d0 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65  , 0);.**  sqlite
1a9e0 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a  3_free(zSQL);.**
1a9f0 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75   </pre></blockqu
1aa00 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ote>.**.** The c
1aa10 6f 64 65 20 61 62 6f 76 65 20 77 69 6c 6c 20 72  ode above will r
1aa20 65 6e 64 65 72 20 61 20 63 6f 72 72 65 63 74 20  ender a correct 
1aa30 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  SQL statement in
1aa40 20 74 68 65 20 7a 53 51 4c 0a 2a 2a 20 76 61 72   the zSQL.** var
1aa50 69 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68  iable even if th
1aa60 65 20 7a 54 65 78 74 20 76 61 72 69 61 62 6c 65  e zText variable
1aa70 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
1aa80 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 25  er..**.** The "%
1aa90 7a 22 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70  z" formatting op
1aaa0 74 69 6f 6e 20 77 6f 72 6b 73 20 65 78 61 63 74  tion works exact
1aab0 6c 79 20 6c 69 6b 65 20 22 25 73 22 20 77 69 74  ly like "%s" wit
1aac0 68 20 74 68 65 0a 2a 2a 20 61 64 64 69 74 69 6f  h the.** additio
1aad0 6e 20 74 68 61 74 20 61 66 74 65 72 20 74 68 65  n that after the
1aae0 20 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e   string has been
1aaf0 20 72 65 61 64 20 61 6e 64 20 63 6f 70 69 65 64   read and copied
1ab00 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 72 65 73   into.** the res
1ab10 75 6c 74 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72  ult, [sqlite3_fr
1ab20 65 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20  ee()] is called 
1ab30 6f 6e 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  on the input str
1ab40 69 6e 67 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  ing. {END}.**.**
1ab50 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
1ab60 2a 2a 20 7b 48 31 37 34 30 33 7d 20 20 54 68 65  ** {H17403}  The
1ab70 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74   [sqlite3_mprint
1ab80 66 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  f()] and [sqlite
1ab90 33 5f 76 6d 70 72 69 6e 74 66 28 29 5d 20 69 6e  3_vmprintf()] in
1aba0 74 65 72 66 61 63 65 73 0a 2a 2a 20 20 20 20 20  terfaces.**     
1abb0 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 69 74        return eit
1abc0 68 65 72 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  her pointers to 
1abd0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
1abe0 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 68 65  UTF-8 strings he
1abf0 6c 64 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ld in.**        
1ac00 20 20 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e     memory obtain
1ac10 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
1ac20 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 4e 55  _malloc()] or NU
1ac30 4c 4c 20 70 6f 69 6e 74 65 72 73 20 69 66 0a 2a  LL pointers if.*
1ac40 2a 20 20 20 20 20 20 20 20 20 20 20 61 20 63 61  *           a ca
1ac50 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d  ll to [sqlite3_m
1ac60 61 6c 6c 6f 63 28 29 5d 20 66 61 69 6c 73 2e 0a  alloc()] fails..
1ac70 2a 2a 0a 2a 2a 20 7b 48 31 37 34 30 36 7d 20 20  **.** {H17406}  
1ac80 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70  The [sqlite3_snp
1ac90 72 69 6e 74 66 28 29 5d 20 69 6e 74 65 72 66 61  rintf()] interfa
1aca0 63 65 20 77 72 69 74 65 73 20 61 20 7a 65 72 6f  ce writes a zero
1acb0 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20  -terminated.**  
1acc0 20 20 20 20 20 20 20 20 20 55 54 46 2d 38 20 73           UTF-8 s
1acd0 74 72 69 6e 67 20 69 6e 74 6f 20 74 68 65 20 62  tring into the b
1ace0 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f  uffer pointed to
1acf0 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 70   by the second p
1ad00 61 72 61 6d 65 74 65 72 0a 2a 2a 20 20 20 20 20  arameter.**     
1ad10 20 20 20 20 20 20 70 72 6f 76 69 64 65 64 20 74        provided t
1ad20 68 61 74 20 74 68 65 20 66 69 72 73 74 20 70 61  hat the first pa
1ad30 72 61 6d 65 74 65 72 20 69 73 20 67 72 65 61 74  rameter is great
1ad40 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 0a 2a 2a  er than zero..**
1ad50 0a 2a 2a 20 7b 48 31 37 34 30 37 7d 20 20 54 68  .** {H17407}  Th
1ad60 65 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  e [sqlite3_snpri
1ad70 6e 74 66 28 29 5d 20 69 6e 74 65 72 66 61 63 65  ntf()] interface
1ad80 20 64 6f 65 73 20 6e 6f 74 20 77 72 69 74 65 20   does not write 
1ad90 73 6c 6f 74 73 20 6f 66 0a 2a 2a 20 20 20 20 20  slots of.**     
1ada0 20 20 20 20 20 20 69 74 73 20 6f 75 74 70 75 74        its output
1adb0 20 62 75 66 66 65 72 20 28 74 68 65 20 73 65 63   buffer (the sec
1adc0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 29 20 6f  ond parameter) o
1add0 75 74 73 69 64 65 20 74 68 65 20 72 61 6e 67 65  utside the range
1ade0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 66  .**           of
1adf0 20 30 20 74 68 72 6f 75 67 68 20 4e 2d 31 20 28   0 through N-1 (
1ae00 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 66  where N is the f
1ae10 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a  irst parameter).
1ae20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72 65 67  **           reg
1ae30 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6c  ardless of the l
1ae40 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 72  ength of the str
1ae50 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
1ae60 20 72 65 71 75 65 73 74 65 64 20 62 79 20 74 68   requested by th
1ae70 65 20 66 6f 72 6d 61 74 20 73 70 65 63 69 66 69  e format specifi
1ae80 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  cation..*/.SQLIT
1ae90 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
1aea0 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73  te3_mprintf(cons
1aeb0 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51  t char*,...);.SQ
1aec0 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
1aed0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
1aee0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f  const char*, va_
1aef0 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  list);.SQLITE_AP
1af00 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
1af10 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c 63 68 61  snprintf(int,cha
1af20 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  r*,const char*, 
1af30 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ...);../*.** CAP
1af40 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c  I3REF: Memory Al
1af50 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73 79 73 74  location Subsyst
1af60 65 6d 20 7b 48 31 37 33 30 30 7d 20 3c 53 32 30  em {H17300} <S20
1af70 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53  000>.**.** The S
1af80 51 4c 69 74 65 20 63 6f 72 65 20 20 75 73 65 73  QLite core  uses
1af90 20 74 68 65 73 65 20 74 68 72 65 65 20 72 6f 75   these three rou
1afa0 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66  tines for all of
1afb0 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 69 6e 74 65   its own.** inte
1afc0 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  rnal memory allo
1afd0 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 22 43  cation needs. "C
1afe0 6f 72 65 22 20 69 6e 20 74 68 65 20 70 72 65 76  ore" in the prev
1aff0 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 0a 2a 2a  ious sentence.**
1b000 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
1b010 65 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74  e operating-syst
1b020 65 6d 20 73 70 65 63 69 66 69 63 20 56 46 53 20  em specific VFS 
1b030 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
1b040 20 54 68 65 0a 2a 2a 20 57 69 6e 64 6f 77 73 20   The.** Windows 
1b050 56 46 53 20 75 73 65 73 20 6e 61 74 69 76 65 20  VFS uses native 
1b060 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65  malloc() and fre
1b070 65 28 29 20 66 6f 72 20 73 6f 6d 65 20 6f 70 65  e() for some ope
1b080 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  rations..**.** T
1b090 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  he sqlite3_mallo
1b0a0 63 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75  c() routine retu
1b0b0 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
1b0c0 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 6d   a block.** of m
1b0d0 65 6d 6f 72 79 20 61 74 20 6c 65 61 73 74 20 4e  emory at least N
1b0e0 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 68   bytes in length
1b0f0 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
1b100 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 49   parameter..** I
1b110 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  f sqlite3_malloc
1b120 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  () is unable to 
1b130 6f 62 74 61 69 6e 20 73 75 66 66 69 63 69 65 6e  obtain sufficien
1b140 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d 6f 72 79  t free.** memory
1b150 2c 20 69 74 20 72 65 74 75 72 6e 73 20 61 20 4e  , it returns a N
1b160 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 49 66  ULL pointer.  If
1b170 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 4e   the parameter N
1b180 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d   to.** sqlite3_m
1b190 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20  alloc() is zero 
1b1a0 6f 72 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e  or negative then
1b1b0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
1b1c0 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 20 4e  ) returns.** a N
1b1d0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ULL pointer..**.
1b1e0 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74  ** Calling sqlit
1b1f0 65 33 5f 66 72 65 65 28 29 20 77 69 74 68 20 61  e3_free() with a
1b200 20 70 6f 69 6e 74 65 72 20 70 72 65 76 69 6f 75   pointer previou
1b210 73 6c 79 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  sly returned.** 
1b220 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  by sqlite3_mallo
1b230 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72  c() or sqlite3_r
1b240 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65 61 73 65  ealloc() release
1b250 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 73 6f  s that memory so
1b260 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d 69 67 68  .** that it migh
1b270 74 20 62 65 20 72 65 75 73 65 64 2e 20 20 54 68  t be reused.  Th
1b280 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  e sqlite3_free()
1b290 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 61   routine is.** a
1b2a0 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 63 61 6c   no-op if is cal
1b2b0 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  led with a NULL 
1b2c0 70 6f 69 6e 74 65 72 2e 20 20 50 61 73 73 69 6e  pointer.  Passin
1b2d0 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  g a NULL pointer
1b2e0 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 66  .** to sqlite3_f
1b2f0 72 65 65 28 29 20 69 73 20 68 61 72 6d 6c 65 73  ree() is harmles
1b300 73 2e 20 20 41 66 74 65 72 20 62 65 69 6e 67 20  s.  After being 
1b310 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79 0a 2a 2a  freed, memory.**
1b320 20 73 68 6f 75 6c 64 20 6e 65 69 74 68 65 72 20   should neither 
1b330 62 65 20 72 65 61 64 20 6e 6f 72 20 77 72 69 74  be read nor writ
1b340 74 65 6e 2e 20 20 45 76 65 6e 20 72 65 61 64 69  ten.  Even readi
1b350 6e 67 20 70 72 65 76 69 6f 75 73 6c 79 20 66 72  ng previously fr
1b360 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6d 69  eed.** memory mi
1b370 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ght result in a 
1b380 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75  segmentation fau
1b390 6c 74 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65  lt or other seve
1b3a0 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 4d 65 6d  re error..** Mem
1b3b0 6f 72 79 20 63 6f 72 72 75 70 74 69 6f 6e 2c 20  ory corruption, 
1b3c0 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66  a segmentation f
1b3d0 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65 72 20 73  ault, or other s
1b3e0 65 76 65 72 65 20 65 72 72 6f 72 0a 2a 2a 20 6d  evere error.** m
1b3f0 69 67 68 74 20 72 65 73 75 6c 74 20 69 66 20 73  ight result if s
1b400 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 73  qlite3_free() is
1b410 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 6e   called with a n
1b420 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20  on-NULL pointer 
1b430 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e 6f 74 20  that.** was not 
1b440 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
1b450 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f  lite3_malloc() o
1b460 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  r sqlite3_reallo
1b470 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  c()..**.** The s
1b480 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
1b490 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d   interface attem
1b4a0 70 74 73 20 74 6f 20 72 65 73 69 7a 65 20 61 0a  pts to resize a.
1b4b0 2a 2a 20 70 72 69 6f 72 20 6d 65 6d 6f 72 79 20  ** prior memory 
1b4c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65  allocation to be
1b4d0 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65   at least N byte
1b4e0 73 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  s, where N is th
1b4f0 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61  e.** second para
1b500 6d 65 74 65 72 2e 20 20 54 68 65 20 6d 65 6d 6f  meter.  The memo
1b510 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f  ry allocation to
1b520 20 62 65 20 72 65 73 69 7a 65 64 20 69 73 20 74   be resized is t
1b530 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 61  he first.** para
1b540 6d 65 74 65 72 2e 20 20 49 66 20 74 68 65 20 66  meter.  If the f
1b550 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74  irst parameter t
1b560 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  o sqlite3_reallo
1b570 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e 55 4c 4c  c().** is a NULL
1b580 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 69 74   pointer then it
1b590 73 20 62 65 68 61 76 69 6f 72 20 69 73 20 69 64  s behavior is id
1b5a0 65 6e 74 69 63 61 6c 20 74 6f 20 63 61 6c 6c 69  entical to calli
1b5b0 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61  ng.** sqlite3_ma
1b5c0 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65 20 4e 20  lloc(N) where N 
1b5d0 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  is the second pa
1b5e0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
1b5f0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  e3_realloc()..**
1b600 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70   If the second p
1b610 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
1b620 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 73  te3_realloc() is
1b630 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61   zero or.** nega
1b640 74 69 76 65 20 74 68 65 6e 20 74 68 65 20 62 65  tive then the be
1b650 68 61 76 69 6f 72 20 69 73 20 65 78 61 63 74 6c  havior is exactl
1b660 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 63 61  y the same as ca
1b670 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  lling.** sqlite3
1b680 5f 66 72 65 65 28 50 29 20 77 68 65 72 65 20 50  _free(P) where P
1b690 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61   is the first pa
1b6a0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
1b6b0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  e3_realloc()..**
1b6c0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
1b6d0 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  () returns a poi
1b6e0 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f 72 79  nter to a memory
1b6f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f   allocation.** o
1b700 66 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74  f at least N byt
1b710 65 73 20 69 6e 20 73 69 7a 65 20 6f 72 20 4e 55  es in size or NU
1b720 4c 4c 20 69 66 20 73 75 66 66 69 63 69 65 6e 74  LL if sufficient
1b730 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e 61 76 61   memory is unava
1b740 69 6c 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 4d 20  ilable..** If M 
1b750 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
1b760 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74  he prior allocat
1b770 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e 28 4e 2c  ion, then min(N,
1b780 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 74  M) bytes.** of t
1b790 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74  he prior allocat
1b7a0 69 6f 6e 20 61 72 65 20 63 6f 70 69 65 64 20 69  ion are copied i
1b7b0 6e 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  nto the beginnin
1b7c0 67 20 6f 66 20 62 75 66 66 65 72 20 72 65 74 75  g of buffer retu
1b7d0 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74  rned.** by sqlit
1b7e0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64  e3_realloc() and
1b7f0 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63   the prior alloc
1b800 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a  ation is freed..
1b810 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 72 65  ** If sqlite3_re
1b820 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 20  alloc() returns 
1b830 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 70  NULL, then the p
1b840 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  rior allocation.
1b850 2a 2a 20 69 73 20 6e 6f 74 20 66 72 65 65 64 2e  ** is not freed.
1b860 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72  .**.** The memor
1b870 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  y returned by sq
1b880 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61  lite3_malloc() a
1b890 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  nd sqlite3_reall
1b8a0 6f 63 28 29 0a 2a 2a 20 69 73 20 61 6c 77 61 79  oc().** is alway
1b8b0 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 74 20  s aligned to at 
1b8c0 6c 65 61 73 74 20 61 6e 20 38 20 62 79 74 65 20  least an 8 byte 
1b8d0 62 6f 75 6e 64 61 72 79 2e 20 7b 45 4e 44 7d 0a  boundary. {END}.
1b8e0 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  **.** The defaul
1b8f0 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
1b900 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
1b910 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
1b920 74 65 6d 20 75 73 65 73 0a 2a 2a 20 74 68 65 20  tem uses.** the 
1b930 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61 6c 6c 6f  malloc(), reallo
1b940 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 70  c() and free() p
1b950 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 73  rovided by the s
1b960 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72  tandard C librar
1b970 79 2e 0a 2a 2a 20 7b 48 31 37 33 38 32 7d 20 48  y..** {H17382} H
1b980 6f 77 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74  owever, if SQLit
1b990 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
1b9a0 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  th the.** SQLITE
1b9b0 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d 3c 69 3e  _MEMORY_SIZE=<i>
1b9c0 4e 4e 4e 3c 2f 69 3e 20 43 20 70 72 65 70 72 6f  NNN</i> C prepro
1b9d0 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 28 77 68  cessor macro (wh
1b9e0 65 72 65 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a  ere <i>NNN</i>.*
1b9f0 2a 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 29  * is an integer)
1ba00 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 63 72  , then SQLite cr
1ba10 65 61 74 65 20 61 20 73 74 61 74 69 63 20 61 72  eate a static ar
1ba20 72 61 79 20 6f 66 20 61 74 20 6c 65 61 73 74 0a  ray of at least.
1ba30 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 62 79  ** <i>NNN</i> by
1ba40 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 20  tes in size and 
1ba50 75 73 65 73 20 74 68 61 74 20 61 72 72 61 79 20  uses that array 
1ba60 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64  for all of its d
1ba70 79 6e 61 6d 69 63 0a 2a 2a 20 6d 65 6d 6f 72 79  ynamic.** memory
1ba80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64   allocation need
1ba90 73 2e 20 7b 45 4e 44 7d 20 20 41 64 64 69 74 69  s. {END}  Additi
1baa0 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  onal memory allo
1bab0 63 61 74 6f 72 20 6f 70 74 69 6f 6e 73 0a 2a 2a  cator options.**
1bac0 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e   may be added in
1bad0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
1bae0 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 53 51 4c 69 74  ..**.** In SQLit
1baf0 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 20  e version 3.5.0 
1bb00 61 6e 64 20 33 2e 35 2e 31 2c 20 69 74 20 77 61  and 3.5.1, it wa
1bb10 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65  s possible to de
1bb20 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c 49  fine.** the SQLI
1bb30 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41  TE_OMIT_MEMORY_A
1bb40 4c 4c 4f 43 41 54 49 4f 4e 20 77 68 69 63 68 20  LLOCATION which 
1bb50 77 6f 75 6c 64 20 63 61 75 73 65 20 74 68 65 20  would cause the 
1bb60 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 69 6d 70 6c  built-in.** impl
1bb70 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
1bb80 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20  ese routines to 
1bb90 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 61  be omitted.  Tha
1bba0 74 20 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a 20  t capability.** 
1bbb0 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 6f  is no longer pro
1bbc0 76 69 64 65 64 2e 20 20 4f 6e 6c 79 20 62 75 69  vided.  Only bui
1bbd0 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c  lt-in memory all
1bbe0 6f 63 61 74 6f 72 73 20 63 61 6e 20 62 65 20 75  ocators can be u
1bbf0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 57  sed..**.** The W
1bc00 69 6e 64 6f 77 73 20 4f 53 20 69 6e 74 65 72 66  indows OS interf
1bc10 61 63 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 0a  ace layer calls.
1bc20 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 6d 61  ** the system ma
1bc30 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28  lloc() and free(
1bc40 29 20 64 69 72 65 63 74 6c 79 20 77 68 65 6e 20  ) directly when 
1bc50 63 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a 20 66 69  converting.** fi
1bc60 6c 65 6e 61 6d 65 73 20 62 65 74 77 65 65 6e 20  lenames between 
1bc70 74 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 69  the UTF-8 encodi
1bc80 6e 67 20 75 73 65 64 20 62 79 20 53 51 4c 69 74  ng used by SQLit
1bc90 65 0a 2a 2a 20 61 6e 64 20 77 68 61 74 65 76 65  e.** and whateve
1bca0 72 20 66 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64  r filename encod
1bcb0 69 6e 67 20 69 73 20 75 73 65 64 20 62 79 20 74  ing is used by t
1bcc0 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 57 69  he particular Wi
1bcd0 6e 64 6f 77 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c  ndows.** install
1bce0 61 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20 61  ation.  Memory a
1bcf0 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 73  llocation errors
1bd00 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20 62   are detected, b
1bd10 75 74 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 72  ut.** they are r
1bd20 65 70 6f 72 74 65 64 20 62 61 63 6b 20 61 73 20  eported back as 
1bd30 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e  [SQLITE_CANTOPEN
1bd40 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ] or.** [SQLITE_
1bd50 49 4f 45 52 52 5d 20 72 61 74 68 65 72 20 74 68  IOERR] rather th
1bd60 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  an [SQLITE_NOMEM
1bd70 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  ]..**.** INVARIA
1bd80 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33  NTS:.**.** {H173
1bd90 30 33 7d 20 20 54 68 65 20 5b 73 71 6c 69 74 65  03}  The [sqlite
1bda0 33 5f 6d 61 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74  3_malloc(N)] int
1bdb0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 65  erface returns e
1bdc0 69 74 68 65 72 20 61 20 70 6f 69 6e 74 65 72 20  ither a pointer 
1bdd0 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  to.**           
1bde0 61 20 6e 65 77 6c 79 20 63 68 65 63 6b 65 64 2d  a newly checked-
1bdf0 6f 75 74 20 62 6c 6f 63 6b 20 6f 66 20 61 74 20  out block of at 
1be00 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 6f 66  least N bytes of
1be10 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20   memory.**      
1be20 20 20 20 20 20 74 68 61 74 20 69 73 20 38 2d 62       that is 8-b
1be30 79 74 65 20 61 6c 69 67 6e 65 64 2c 20 6f 72 20  yte aligned, or 
1be40 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20  it returns NULL 
1be50 69 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 0a  if it is unable.
1be60 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 6f 20  **           to 
1be70 66 75 6c 66 69 6c 6c 20 74 68 65 20 72 65 71 75  fulfill the requ
1be80 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33  est..**.** {H173
1be90 30 34 7d 20 20 54 68 65 20 5b 73 71 6c 69 74 65  04}  The [sqlite
1bea0 33 5f 6d 61 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74  3_malloc(N)] int
1beb0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
1bec0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66   NULL pointer if
1bed0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 4e 20  .**           N 
1bee0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  is less than or 
1bef0 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 0a 2a  equal to zero..*
1bf00 2a 0a 2a 2a 20 7b 48 31 37 33 30 35 7d 20 20 54  *.** {H17305}  T
1bf10 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  he [sqlite3_free
1bf20 28 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72  (P)] interface r
1bf30 65 6c 65 61 73 65 73 20 6d 65 6d 6f 72 79 20 70  eleases memory p
1bf40 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 20 20 20  reviously.**    
1bf50 20 20 20 20 20 20 20 72 65 74 75 72 6e 65 64 20         returned 
1bf60 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61  from [sqlite3_ma
1bf70 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69  lloc()] or [sqli
1bf80 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 2c 0a  te3_realloc()],.
1bf90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6d 61 6b  **           mak
1bfa0 69 6e 67 20 69 74 20 61 76 61 69 6c 61 62 6c 65  ing it available
1bfb0 20 66 6f 72 20 72 65 75 73 65 2e 0a 2a 2a 0a 2a   for reuse..**.*
1bfc0 2a 20 7b 48 31 37 33 30 36 7d 20 20 41 20 63 61  * {H17306}  A ca
1bfd0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66  ll to [sqlite3_f
1bfe0 72 65 65 28 4e 55 4c 4c 29 5d 20 69 73 20 61 20  ree(NULL)] is a 
1bff0 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a  harmless no-op..
1c000 2a 2a 0a 2a 2a 20 7b 48 31 37 33 31 30 7d 20 20  **.** {H17310}  
1c010 41 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  A call to [sqlit
1c020 65 33 5f 72 65 61 6c 6c 6f 63 28 30 2c 4e 29 5d  e3_realloc(0,N)]
1c030 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   is equivalent t
1c040 6f 20 61 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 20  o a call.**     
1c050 20 20 20 20 20 20 74 6f 20 5b 73 71 6c 69 74 65        to [sqlite
1c060 33 5f 6d 61 6c 6c 6f 63 28 4e 29 5d 2e 0a 2a 2a  3_malloc(N)]..**
1c070 0a 2a 2a 20 7b 48 31 37 33 31 32 7d 20 20 41 20  .** {H17312}  A 
1c080 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
1c090 5f 72 65 61 6c 6c 6f 63 28 50 2c 30 29 5d 20 69  _realloc(P,0)] i
1c0a0 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20  s equivalent to 
1c0b0 61 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20  a call.**       
1c0c0 20 20 20 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f      to [sqlite3_
1c0d0 66 72 65 65 28 50 29 5d 2e 0a 2a 2a 0a 2a 2a 20  free(P)]..**.** 
1c0e0 7b 48 31 37 33 31 35 7d 20 20 54 68 65 20 53 51  {H17315}  The SQ
1c0f0 4c 69 74 65 20 63 6f 72 65 20 75 73 65 73 20 5b  Lite core uses [
1c100 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
1c110 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c  ], [sqlite3_real
1c120 6c 6f 63 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  loc()],.**      
1c130 20 20 20 20 20 61 6e 64 20 5b 73 71 6c 69 74 65       and [sqlite
1c140 33 5f 66 72 65 65 28 29 5d 20 66 6f 72 20 61 6c  3_free()] for al
1c150 6c 20 6f 66 20 69 74 73 20 6d 65 6d 6f 72 79 20  l of its memory 
1c160 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a  allocation and.*
1c170 2a 20 20 20 20 20 20 20 20 20 20 20 64 65 61 6c  *           deal
1c180 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 0a  location needs..
1c190 2a 2a 0a 2a 2a 20 7b 48 31 37 33 31 38 7d 20 20  **.** {H17318}  
1c1a0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 61  The [sqlite3_rea
1c1b0 6c 6c 6f 63 28 50 2c 4e 29 5d 20 69 6e 74 65 72  lloc(P,N)] inter
1c1c0 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74  face returns eit
1c1d0 68 65 72 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a  her a pointer.**
1c1e0 20 20 20 20 20 20 20 20 20 20 20 74 6f 20 61 20             to a 
1c1f0 62 6c 6f 63 6b 20 6f 66 20 63 68 65 63 6b 65 64  block of checked
1c200 2d 6f 75 74 20 6d 65 6d 6f 72 79 20 6f 66 20 61  -out memory of a
1c210 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20  t least N bytes 
1c220 69 6e 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20 20  in size.**      
1c230 20 20 20 20 20 74 68 61 74 20 69 73 20 38 2d 62       that is 8-b
1c240 79 74 65 20 61 6c 69 67 6e 65 64 2c 20 6f 72 20  yte aligned, or 
1c250 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
1c260 2a 2a 0a 2a 2a 20 7b 48 31 37 33 32 31 7d 20 20  **.** {H17321}  
1c270 57 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65  When [sqlite3_re
1c280 61 6c 6c 6f 63 28 50 2c 4e 29 5d 20 72 65 74 75  alloc(P,N)] retu
1c290 72 6e 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70  rns a non-NULL p
1c2a0 6f 69 6e 74 65 72 2c 20 69 74 20 66 69 72 73 74  ointer, it first
1c2b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63 6f  .**           co
1c2c0 70 69 65 73 20 74 68 65 20 66 69 72 73 74 20 4b  pies the first K
1c2d0 20 62 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e   bytes of conten
1c2e0 74 20 66 72 6f 6d 20 50 20 69 6e 74 6f 20 74 68  t from P into th
1c2f0 65 20 6e 65 77 6c 79 0a 2a 2a 20 20 20 20 20 20  e newly.**      
1c300 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 64 20 62       allocated b
1c310 6c 6f 63 6b 2c 20 77 68 65 72 65 20 4b 20 69 73  lock, where K is
1c320 20 74 68 65 20 6c 65 73 73 65 72 20 6f 66 20 4e   the lesser of N
1c330 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66   and the size of
1c340 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68  .**           th
1c350 65 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a  e buffer P..**.*
1c360 2a 20 7b 48 31 37 33 32 32 7d 20 20 57 68 65 6e  * {H17322}  When
1c370 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
1c380 63 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20  c(P,N)] returns 
1c390 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74  a non-NULL point
1c3a0 65 72 2c 20 69 74 20 66 69 72 73 74 0a 2a 2a 20  er, it first.** 
1c3b0 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73            releas
1c3c0 65 73 20 74 68 65 20 62 75 66 66 65 72 20 50 2e  es the buffer P.
1c3d0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 32 33 7d 20  .**.** {H17323} 
1c3e0 20 57 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72   When [sqlite3_r
1c3f0 65 61 6c 6c 6f 63 28 50 2c 4e 29 5d 20 72 65 74  ealloc(P,N)] ret
1c400 75 72 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 20 62  urns NULL, the b
1c410 75 66 66 65 72 20 50 20 69 73 0a 2a 2a 20 20 20  uffer P is.**   
1c420 20 20 20 20 20 20 20 20 6e 6f 74 20 6d 6f 64 69          not modi
1c430 66 69 65 64 20 6f 72 20 72 65 6c 65 61 73 65 64  fied or released
1c440 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49  ..**.** ASSUMPTI
1c450 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 37 33  ONS:.**.** {A173
1c460 35 30 7d 20 20 54 68 65 20 70 6f 69 6e 74 65 72  50}  The pointer
1c470 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 5b 73   arguments to [s
1c480 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 61  qlite3_free()] a
1c490 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c  nd [sqlite3_real
1c4a0 6c 6f 63 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20  loc()].**       
1c4b0 20 20 20 20 6d 75 73 74 20 62 65 20 65 69 74 68      must be eith
1c4c0 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 65 20  er NULL or else 
1c4d0 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 6e 65  pointers obtaine
1c4e0 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a  d from a prior.*
1c4f0 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 76 6f  *           invo
1c500 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74  cation of [sqlit
1c510 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20  e3_malloc()] or 
1c520 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
1c530 28 29 5d 20 74 68 61 74 20 68 61 76 65 0a 2a 2a  ()] that have.**
1c540 20 20 20 20 20 20 20 20 20 20 20 6e 6f 74 20 79             not y
1c550 65 74 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64  et been released
1c560 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 37 33 35 31 7d  ..**.** {A17351}
1c570 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f    The applicatio
1c580 6e 20 6d 75 73 74 20 6e 6f 74 20 72 65 61 64 20  n must not read 
1c590 6f 72 20 77 72 69 74 65 20 61 6e 79 20 70 61 72  or write any par
1c5a0 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  t of.**         
1c5b0 20 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d    a block of mem
1c5c0 6f 72 79 20 61 66 74 65 72 20 69 74 20 68 61 73  ory after it has
1c5d0 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75   been released u
1c5e0 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  sing.**         
1c5f0 20 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28    [sqlite3_free(
1c600 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  )] or [sqlite3_r
1c610 65 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51  ealloc()]..*/.SQ
1c620 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
1c630 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e  qlite3_malloc(in
1c640 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  t);.SQLITE_API v
1c650 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61  oid *sqlite3_rea
1c660 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29  lloc(void*, int)
1c670 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
1c680 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76  d sqlite3_free(v
1c690 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oid*);../*.** CA
1c6a0 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41  PI3REF: Memory A
1c6b0 6c 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74  llocator Statist
1c6c0 69 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33  ics {H17370} <S3
1c6d0 30 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  0210>.**.** SQLi
1c6e0 74 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 73  te provides thes
1c6f0 65 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73  e two interfaces
1c700 20 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f   for reporting o
1c710 6e 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20  n the status.** 
1c720 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
1c730 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69  malloc()], [sqli
1c740 74 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64  te3_free()], and
1c750 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
1c760 63 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  c()].** routines
1c770 2c 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65  , which form the
1c780 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79   built-in memory
1c790 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
1c7a0 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  ystem..**.** INV
1c7b0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
1c7c0 48 31 37 33 37 31 7d 20 54 68 65 20 5b 73 71 6c  H17371} The [sql
1c7d0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64  ite3_memory_used
1c7e0 28 29 5d 20 72 6f 75 74 69 6e 65 20 72 65 74 75  ()] routine retu
1c7f0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
1c800 66 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 20  f bytes.**      
1c810 20 20 20 20 6f 66 20 6d 65 6d 6f 72 79 20 63 75      of memory cu
1c820 72 72 65 6e 74 6c 79 20 6f 75 74 73 74 61 6e 64  rrently outstand
1c830 69 6e 67 20 28 6d 61 6c 6c 6f 63 65 64 20 62 75  ing (malloced bu
1c840 74 20 6e 6f 74 20 66 72 65 65 64 29 2e 0a 2a 2a  t not freed)..**
1c850 0a 2a 2a 20 7b 48 31 37 33 37 33 7d 20 54 68 65  .** {H17373} The
1c860 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79   [sqlite3_memory
1c870 5f 68 69 67 68 77 61 74 65 72 28 29 5d 20 72 6f  _highwater()] ro
1c880 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
1c890 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 20 20 20  e maximum.**    
1c8a0 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 5b        value of [
1c8b0 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75  sqlite3_memory_u
1c8c0 73 65 64 28 29 5d 20 73 69 6e 63 65 20 74 68 65  sed()] since the
1c8d0 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b   high-water mark
1c8e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 61 73  .**          was
1c8f0 20 6c 61 73 74 20 72 65 73 65 74 2e 0a 2a 2a 0a   last reset..**.
1c900 2a 2a 20 7b 48 31 37 33 37 34 7d 20 54 68 65 20  ** {H17374} The 
1c910 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20  values returned 
1c920 62 79 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  by [sqlite3_memo
1c930 72 79 5f 75 73 65 64 28 29 5d 20 61 6e 64 0a 2a  ry_used()] and.*
1c940 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
1c950 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77  te3_memory_highw
1c960 61 74 65 72 28 29 5d 20 69 6e 63 6c 75 64 65 20  ater()] include 
1c970 61 6e 79 20 6f 76 65 72 68 65 61 64 0a 2a 2a 20  any overhead.** 
1c980 20 20 20 20 20 20 20 20 20 61 64 64 65 64 20 62           added b
1c990 79 20 53 51 4c 69 74 65 20 69 6e 20 69 74 73 20  y SQLite in its 
1c9a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1c9b0 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  f [sqlite3_mallo
1c9c0 63 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  c()],.**        
1c9d0 20 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 68 65    but not overhe
1c9e0 61 64 20 61 64 64 65 64 20 62 79 20 74 68 65 20  ad added by the 
1c9f0 61 6e 79 20 75 6e 64 65 72 6c 79 69 6e 67 20 73  any underlying s
1ca00 79 73 74 65 6d 20 6c 69 62 72 61 72 79 0a 2a 2a  ystem library.**
1ca10 20 20 20 20 20 20 20 20 20 20 72 6f 75 74 69 6e            routin
1ca20 65 73 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33  es that [sqlite3
1ca30 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6d 61 79 20 63  _malloc()] may c
1ca40 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33  all..**.** {H173
1ca50 37 35 7d 20 54 68 65 20 6d 65 6d 6f 72 79 20 68  75} The memory h
1ca60 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69  igh-water mark i
1ca70 73 20 72 65 73 65 74 20 74 6f 20 74 68 65 20 63  s reset to the c
1ca80 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 0a  urrent value of.
1ca90 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
1caa0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64  ite3_memory_used
1cab0 28 29 5d 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20  ()] if and only 
1cac0 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  if the parameter
1cad0 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   to.**          
1cae0 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f  [sqlite3_memory_
1caf0 68 69 67 68 77 61 74 65 72 28 29 5d 20 69 73 20  highwater()] is 
1cb00 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65  true.  The value
1cb10 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 20 20 20   returned.**    
1cb20 20 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 65        by [sqlite
1cb30 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
1cb40 65 72 28 31 29 5d 20 69 73 20 74 68 65 20 68 69  er(1)] is the hi
1cb50 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 0a 2a 2a  gh-water mark.**
1cb60 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72 20            prior 
1cb70 74 6f 20 74 68 65 20 72 65 73 65 74 2e 0a 2a 2f  to the reset..*/
1cb80 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1cb90 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
1cba0 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f  3_memory_used(vo
1cbb0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
1cbc0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
1cbd0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
1cbe0 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74  hwater(int reset
1cbf0 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Flag);../*.** CA
1cc00 50 49 33 52 45 46 3a 20 50 73 65 75 64 6f 2d 52  PI3REF: Pseudo-R
1cc10 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 20 47 65 6e  andom Number Gen
1cc20 65 72 61 74 6f 72 20 7b 48 31 37 33 39 30 7d 20  erator {H17390} 
1cc30 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 53  <S20000>.**.** S
1cc40 51 4c 69 74 65 20 63 6f 6e 74 61 69 6e 73 20 61  QLite contains a
1cc50 20 68 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73   high-quality ps
1cc60 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62  eudo-random numb
1cc70 65 72 20 67 65 6e 65 72 61 74 6f 72 20 28 50 52  er generator (PR
1cc80 4e 47 29 20 75 73 65 64 20 74 6f 0a 2a 2a 20 73  NG) used to.** s
1cc90 65 6c 65 63 74 20 72 61 6e 64 6f 6d 20 5b 52 4f  elect random [RO
1cca0 57 49 44 20 7c 20 52 4f 57 49 44 73 5d 20 77 68  WID | ROWIDs] wh
1ccb0 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77  en inserting new
1ccc0 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20   records into a 
1ccd0 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 61 6c  table that.** al
1cce0 72 65 61 64 79 20 75 73 65 73 20 74 68 65 20 6c  ready uses the l
1ccf0 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20  argest possible 
1cd00 5b 52 4f 57 49 44 5d 2e 20 20 54 68 65 20 50 52  [ROWID].  The PR
1cd10 4e 47 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  NG is also used 
1cd20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64  for.** the build
1cd30 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e 64  -in random() and
1cd40 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53 51   randomblob() SQ
1cd50 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  L functions.  Th
1cd60 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c  is interface all
1cd70 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  ows.** applicati
1cd80 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 20 74 68  ons to access th
1cd90 65 20 73 61 6d 65 20 50 52 4e 47 20 66 6f 72 20  e same PRNG for 
1cda0 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a  other purposes..
1cdb0 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  **.** A call to 
1cdc0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f  this routine sto
1cdd0 72 65 73 20 4e 20 62 79 74 65 73 20 6f 66 20 72  res N bytes of r
1cde0 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 62  andomness into b
1cdf0 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54  uffer P..**.** T
1ce00 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
1ce10 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e  is routine is in
1ce20 76 6f 6b 65 64 20 28 65 69 74 68 65 72 20 69 6e  voked (either in
1ce30 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a 2a  ternally or by.*
1ce40 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * the applicatio
1ce50 6e 29 20 74 68 65 20 50 52 4e 47 20 69 73 20 73  n) the PRNG is s
1ce60 65 65 64 65 64 20 75 73 69 6e 67 20 72 61 6e 64  eeded using rand
1ce70 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 0a  omness obtained.
1ce80 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 61 6e  ** from the xRan
1ce90 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f  domness method o
1cea0 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  f the default [s
1ceb0 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
1cec0 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75  ct..** On all su
1ced0 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74  bsequent invocat
1cee0 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75 64 6f  ions, the pseudo
1cef0 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20 67  -randomness is g
1cf00 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74 65  enerated.** inte
1cf10 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74 68 6f  rnally and witho
1cf20 75 74 20 72 65 63 6f 75 72 73 65 20 74 6f 20 74  ut recourse to t
1cf30 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  he [sqlite3_vfs]
1cf40 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20   xRandomness.** 
1cf50 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e  method..**.** IN
1cf60 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
1cf70 7b 48 31 37 33 39 32 7d 20 54 68 65 20 5b 73 71  {H17392} The [sq
1cf80 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
1cf90 28 4e 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65  (N,P)] interface
1cfa0 20 77 72 69 74 65 73 20 4e 20 62 79 74 65 73 20   writes N bytes 
1cfb0 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68  of.**          h
1cfc0 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75  igh-quality pseu
1cfd0 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e  do-randomness in
1cfe0 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2f 0a  to buffer P..*/.
1cff0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1d000 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
1d010 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a  ss(int N, void *
1d020 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  P);../*.** CAPI3
1d030 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d  REF: Compile-Tim
1d040 65 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e Authorization 
1d050 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30  Callbacks {H1250
1d060 30 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S70100>.**.*
1d070 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
1d080 65 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f  egisters a autho
1d090 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77  rizer callback w
1d0a0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72  ith a particular
1d0b0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f  .** [database co
1d0c0 6e 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c  nnection], suppl
1d0d0 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74  ied in the first
1d0e0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68   argument..** Th
1d0f0 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
1d100 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
1d110 20 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   as SQL statemen
1d120 74 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d  ts are being com
1d130 70 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c  piled.** by [sql
1d140 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
1d150 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20  or its variants 
1d160 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1d170 5f 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  _v2()],.** [sqli
1d180 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d  te3_prepare16()]
1d190 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72   and [sqlite3_pr
1d1a0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20  epare16_v2()].  
1d1b0 41 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f  At various.** po
1d1c0 69 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20  ints during the 
1d1d0 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63  compilation proc
1d1e0 65 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73  ess, as logic is
1d1f0 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a   being created.*
1d200 2a 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72  * to perform var
1d210 69 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68  ious actions, th
1d220 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
1d230 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
1d240 20 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68   to.** see if th
1d250 6f 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20  ose actions are 
1d260 61 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75  allowed.  The au
1d270 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1d280 6b 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75  k should.** retu
1d290 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74  rn [SQLITE_OK] t
1d2a0 6f 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69  o allow the acti
1d2b0 6f 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f  on, [SQLITE_IGNO
1d2c0 52 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20  RE] to disallow 
1d2d0 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20  the.** specific 
1d2e0 61 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77  action but allow
1d2f0 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
1d300 6e 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74  nt to continue t
1d310 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64  o be.** compiled
1d320 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e  , or [SQLITE_DEN
1d330 59 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20  Y] to cause the 
1d340 65 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65  entire SQL state
1d350 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65  ment to be.** re
1d360 6a 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65  jected with an e
1d370 72 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75  rror.  If the au
1d380 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1d390 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79  k returns.** any
1d3a0 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61   value other tha
1d3b0 6e 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  n [SQLITE_IGNORE
1d3c0 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20  ], [SQLITE_OK], 
1d3d0 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d  or [SQLITE_DENY]
1d3e0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71  .** then the [sq
1d3f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1d400 28 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e  ()] or equivalen
1d410 74 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67  t call that trig
1d420 67 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74  gered.** the aut
1d430 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69  horizer will fai
1d440 6c 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  l with an error 
1d450 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57  message..**.** W
1d460 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  hen the callback
1d470 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
1d480 5f 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73  _OK], that means
1d490 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a   the operation.*
1d4a0 2a 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f  * requested is o
1d4b0 6b 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c  k.  When the cal
1d4c0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53  lback returns [S
1d4d0 51 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65  QLITE_DENY], the
1d4e0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
1d4f0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71  pare_v2()] or eq
1d500 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68  uivalent call th
1d510 61 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65  at triggered the
1d520 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77  .** authorizer w
1d530 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e  ill fail with an
1d540 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65   error message e
1d550 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a  xplaining that.*
1d560 2a 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69  * access is deni
1d570 65 64 2e 20 20 49 66 20 74 68 65 20 61 75 74 68  ed.  If the auth
1d580 6f 72 69 7a 65 72 20 63 6f 64 65 20 69 73 20 5b  orizer code is [
1d590 53 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20  SQLITE_READ].** 
1d5a0 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  and the callback
1d5b0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
1d5c0 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68  _IGNORE] then th
1d5d0 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73  e.** [prepared s
1d5e0 74 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d  tatement] statem
1d5f0 65 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74  ent is construct
1d600 65 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65  ed to substitute
1d610 0a 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65  .** a NULL value
1d620 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65   in place of the
1d630 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68   table column th
1d640 61 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a  at would have.**
1d650 20 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53   been read if [S
1d660 51 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65  QLITE_OK] had be
1d670 65 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68  en returned.  Th
1d680 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  e [SQLITE_IGNORE
1d690 5d 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20  ].** return can 
1d6a0 62 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20  be used to deny 
1d6b0 61 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65  an untrusted use
1d6c0 72 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69  r access to indi
1d6d0 76 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e  vidual.** column
1d6e0 73 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a  s of a table..**
1d6f0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
1d700 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61  rameter to the a
1d710 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
1d720 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  ck is a copy of 
1d730 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72  the third.** par
1d740 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
1d750 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
1d760 69 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65  izer() interface
1d770 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72  . The second par
1d780 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ameter.** to the
1d790 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20   callback is an 
1d7a0 69 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f  integer [SQLITE_
1d7b0 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f  COPY | action co
1d7c0 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69  de] that specifi
1d7d0 65 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63  es.** the partic
1d7e0 75 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62  ular action to b
1d7f0 65 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68  e authorized. Th
1d800 65 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20  e third through 
1d810 73 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73  sixth parameters
1d820 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  .** to the callb
1d830 61 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72  ack are zero-ter
1d840 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20  minated strings 
1d850 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64  that contain add
1d860 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69  itional.** detai
1d870 6c 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74  ls about the act
1d880 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72  ion to be author
1d890 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61  ized..**.** An a
1d8a0 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65  uthorizer is use
1d8b0 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f  d when [sqlite3_
1d8c0 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72  prepare | prepar
1d8d0 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74  ing].** SQL stat
1d8e0 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75  ements from an u
1d8f0 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c  ntrusted source,
1d900 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20   to ensure that 
1d910 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
1d920 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79  ts.** do not try
1d930 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20   to access data 
1d940 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c  they are not all
1d950 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20  owed to see, or 
1d960 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74  that they do not
1d970 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75  .** try to execu
1d980 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61  te malicious sta
1d990 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d  tements that dam
1d9a0 61 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65  age the database
1d9b0 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c  .  For.** exampl
1d9c0 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  e, an applicatio
1d9d0 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73  n may allow a us
1d9e0 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69  er to enter arbi
1d9f0 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65  trary.** SQL que
1da00 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74  ries for evaluat
1da10 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73  ion by a databas
1da20 65 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c  e.  But the appl
1da30 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20  ication does.** 
1da40 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65  not want the use
1da50 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20  r to be able to 
1da60 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63  make arbitrary c
1da70 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a  hanges to the.**
1da80 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61   database.  An a
1da90 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20  uthorizer could 
1daa0 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70  then be put in p
1dab0 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a  lace while the.*
1dac0 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53  * user-entered S
1dad0 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c  QL is being [sql
1dae0 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70  ite3_prepare | p
1daf0 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a  repared] that.**
1db00 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79   disallows every
1db10 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45  thing except [SE
1db20 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73  LECT] statements
1db30 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74  ..**.** Applicat
1db40 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74  ions that need t
1db50 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72  o process SQL fr
1db60 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75  om untrusted sou
1db70 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c  rces.** might al
1db80 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65  so consider lowe
1db90 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69  ring resource li
1dba0 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69  mits using [sqli
1dbb0 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20  te3_limit()].** 
1dbc0 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74  and limiting dat
1dbd0 61 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67  abase size using
1dbe0 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63   the [max_page_c
1dbf0 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a  ount] [PRAGMA].*
1dc00 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f  * in addition to
1dc10 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72   using an author
1dc20 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79  izer..**.** Only
1dc30 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72   a single author
1dc40 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70  izer can be in p
1dc50 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61  lace on a databa
1dc60 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se connection.**
1dc70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63   at a time.  Eac
1dc80 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  h call to sqlite
1dc90 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
1dca0 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a   overrides the.*
1dcb0 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e  * previous call.
1dcc0 20 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75    Disable the au
1dcd0 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74  thorizer by inst
1dce0 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61  alling a NULL ca
1dcf0 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61  llback..** The a
1dd00 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73  uthorizer is dis
1dd10 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
1dd20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  ..**.** The auth
1dd30 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
1dd40 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74  must not do anyt
1dd50 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d  hing that will m
1dd60 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74  odify.** the dat
1dd70 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1dd80 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68   that invoked th
1dd90 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
1dda0 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74  lback..** Note t
1ddb0 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  hat [sqlite3_pre
1ddc0 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b  pare_v2()] and [
1ddd0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
1dde0 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69  both modify thei
1ddf0 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  r.** database co
1de00 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68  nnections for th
1de10 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f  e meaning of "mo
1de20 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61  dify" in this pa
1de30 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57  ragraph..**.** W
1de40 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  hen [sqlite3_pre
1de50 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73  pare_v2()] is us
1de60 65 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20  ed to prepare a 
1de70 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a  statement, the.*
1de80 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68  * statement migh
1de90 74 20 62 65 20 72 65 70 72 65 70 61 72 65 64 20  t be reprepared 
1dea0 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  during [sqlite3_
1deb0 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61  step()] due to a
1dec0 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e   .** schema chan
1ded0 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20  ge.  Hence, the 
1dee0 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75  application shou
1def0 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  ld ensure that t
1df00 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75  he.** correct au
1df10 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1df20 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61  k remains in pla
1df30 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73  ce during the [s
1df40 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a  qlite3_step()]..
1df50 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
1df60 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63  the authorizer c
1df70 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
1df80 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a  ed only during.*
1df90 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
1dfa0 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72  re()] or its var
1dfb0 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a  iants.  Authoriz
1dfc0 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20  ation is not.** 
1dfd0 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67  performed during
1dfe0 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75   statement evalu
1dff0 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65  ation in [sqlite
1e000 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a  3_step()]..**.**
1e010 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
1e020 2a 2a 20 7b 48 31 32 35 30 31 7d 20 54 68 65 20  ** {H12501} The 
1e030 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74  [sqlite3_set_aut
1e040 68 6f 72 69 7a 65 72 28 44 2c 2e 2e 2e 29 5d 20  horizer(D,...)] 
1e050 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
1e060 65 72 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20  ers a.**        
1e070 20 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c    authorizer cal
1e080 6c 62 61 63 6b 20 77 69 74 68 20 64 61 74 61 62  lback with datab
1e090 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44  ase connection D
1e0a0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 32 7d  ..**.** {H12502}
1e0b0 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   The authorizer 
1e0c0 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
1e0d0 6b 65 64 20 61 73 20 53 51 4c 20 73 74 61 74 65  ked as SQL state
1e0e0 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 20 20 20  ments are.**    
1e0f0 20 20 20 20 20 20 62 65 69 6e 67 20 70 61 72 73        being pars
1e100 65 65 64 20 61 6e 64 20 63 6f 6d 70 69 6c 65 64  eed and compiled
1e110 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 33 7d  ..**.** {H12503}
1e120 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 7a   If the authoriz
1e130 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  er callback retu
1e140 72 6e 73 20 61 6e 79 20 76 61 6c 75 65 20 6f 74  rns any value ot
1e150 68 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20  her than.**     
1e160 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 4e       [SQLITE_IGN
1e170 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b  ORE], [SQLITE_OK
1e180 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45  ], or [SQLITE_DE
1e190 4e 59 5d 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20  NY], then.**    
1e1a0 20 20 20 20 20 20 74 68 65 20 61 70 70 6c 69 63        the applic
1e1b0 61 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 20  ation interface 
1e1c0 63 61 6c 6c 20 74 68 61 74 20 63 61 75 73 65 64  call that caused
1e1d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
1e1e0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1e1f0 62 61 63 6b 20 74 6f 20 72 75 6e 20 73 68 61 6c  back to run shal
1e200 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a  l fail with an.*
1e210 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49  *          [SQLI
1e220 54 45 5f 45 52 52 4f 52 5d 20 65 72 72 6f 72 20  TE_ERROR] error 
1e230 63 6f 64 65 20 61 6e 64 20 61 6e 20 61 70 70 72  code and an appr
1e240 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65  opriate error me
1e250 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ssage..**.** {H1
1e260 32 35 30 34 7d 20 57 68 65 6e 20 74 68 65 20 61  2504} When the a
1e270 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
1e280 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  ck returns [SQLI
1e290 54 45 5f 4f 4b 5d 2c 20 74 68 65 20 6f 70 65 72  TE_OK], the oper
1e2a0 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ation.**        
1e2b0 20 20 64 65 73 63 72 69 62 65 64 20 69 73 20 70    described is p
1e2c0 72 6f 63 65 73 73 65 64 20 6e 6f 72 6d 61 6c 6c  rocessed normall
1e2d0 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 35  y..**.** {H12505
1e2e0 7d 20 57 68 65 6e 20 74 68 65 20 61 75 74 68 6f  } When the autho
1e2f0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72  rizer callback r
1e300 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44  eturns [SQLITE_D
1e310 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 20 20 20  ENY], the.**    
1e320 20 20 20 20 20 20 61 70 70 6c 69 63 61 74 69 6f        applicatio
1e330 6e 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c  n interface call
1e340 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65   that caused the
1e350 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74  .**          aut
1e360 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1e370 20 74 6f 20 72 75 6e 20 73 68 61 6c 6c 20 66 61   to run shall fa
1e380 69 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  il.**          w
1e390 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 45  ith an [SQLITE_E
1e3a0 52 52 4f 52 5d 20 65 72 72 6f 72 20 63 6f 64 65  RROR] error code
1e3b0 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   and an error me
1e3c0 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20 20  ssage.**        
1e3d0 20 20 65 78 70 6c 61 69 6e 69 6e 67 20 74 68 61    explaining tha
1e3e0 74 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69  t access is deni
1e3f0 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30  ed..**.** {H1250
1e400 36 7d 20 49 66 20 74 68 65 20 61 75 74 68 6f 72  6} If the author
1e410 69 7a 65 72 20 63 6f 64 65 20 28 74 68 65 20 32  izer code (the 2
1e420 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
1e430 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 0a 2a  the authorizer.*
1e440 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62  *          callb
1e450 61 63 6b 29 20 69 73 20 5b 53 51 4c 49 54 45 5f  ack) is [SQLITE_
1e460 52 45 41 44 5d 20 61 6e 64 20 74 68 65 20 61 75  READ] and the au
1e470 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1e480 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20  k returns.**    
1e490 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47        [SQLITE_IG
1e4a0 4e 4f 52 45 5d 2c 20 74 68 65 6e 20 74 68 65 20  NORE], then the 
1e4b0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1e4c0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65  nt is constructe
1e4d0 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  d to.**         
1e4e0 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 76   insert a NULL v
1e4f0 61 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 6f 66  alue in place of
1e500 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d   the table colum
1e510 6e 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76  n that would hav
1e520 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 65  e.**          be
1e530 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51 4c 49  en read if [SQLI
1e540 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20  TE_OK] had been 
1e550 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
1e560 7b 48 31 32 35 30 37 7d 20 49 66 20 74 68 65 20  {H12507} If the 
1e570 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 64 65 20  authorizer code 
1e580 28 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74  (the 2nd paramet
1e590 65 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72  er to the author
1e5a0 69 7a 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  izer.**         
1e5b0 20 63 61 6c 6c 62 61 63 6b 29 20 69 73 20 61 6e   callback) is an
1e5c0 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ything other tha
1e5d0 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 2c  n [SQLITE_READ],
1e5e0 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20   then.**        
1e5f0 20 20 61 20 72 65 74 75 72 6e 20 6f 66 20 5b 53    a return of [S
1e600 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 68 61  QLITE_IGNORE] ha
1e610 73 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63  s the same effec
1e620 74 20 61 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e  t as [SQLITE_DEN
1e630 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 31  Y]..**.** {H1251
1e640 30 7d 20 54 68 65 20 66 69 72 73 74 20 70 61 72  0} The first par
1e650 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75  ameter to the au
1e660 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1e670 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a  k is a copy of.*
1e680 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74  *          the t
1e690 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74  hird parameter t
1e6a0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  o the [sqlite3_s
1e6b0 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d  et_authorizer()]
1e6c0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
1e6d0 2a 20 7b 48 31 32 35 31 31 7d 20 54 68 65 20 73  * {H12511} The s
1e6e0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
1e6f0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
1e700 69 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a  is an integer.**
1e710 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54            [SQLIT
1e720 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20  E_COPY | action 
1e730 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63 69  code] that speci
1e740 66 69 65 73 20 74 68 65 20 70 61 72 74 69 63 75  fies the particu
1e750 6c 61 72 20 61 63 74 69 6f 6e 0a 2a 2a 20 20 20  lar action.**   
1e760 20 20 20 20 20 20 20 74 6f 20 62 65 20 61 75 74         to be aut
1e770 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b  horized..**.** {
1e780 48 31 32 35 31 32 7d 20 54 68 65 20 74 68 69 72  H12512} The thir
1e790 64 20 74 68 72 6f 75 67 68 20 73 69 78 74 68 20  d through sixth 
1e7a0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68  parameters to th
1e7b0 65 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 0a 2a  e callback are.*
1e7c0 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 2d  *          zero-
1e7d0 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
1e7e0 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 0a  gs that contain.
1e7f0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 64 64 69  **          addi
1e800 74 69 6f 6e 61 6c 20 64 65 74 61 69 6c 73 20 61  tional details a
1e810 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20  bout the action 
1e820 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64  to be authorized
1e830 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 30 7d  ..**.** {H12520}
1e840 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73   Each call to [s
1e850 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
1e860 72 69 7a 65 72 28 29 5d 20 6f 76 65 72 72 69 64  rizer()] overrid
1e870 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  es.**          a
1e880 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e  ny previously in
1e890 73 74 61 6c 6c 65 64 20 61 75 74 68 6f 72 69 7a  stalled authoriz
1e8a0 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32  er..**.** {H1252
1e8b0 31 7d 20 41 20 4e 55 4c 4c 20 61 75 74 68 6f 72  1} A NULL author
1e8c0 69 7a 65 72 20 6d 65 61 6e 73 20 74 68 61 74 20  izer means that 
1e8d0 6e 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  no authorization
1e8e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c  .**          cal
1e8f0 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
1e900 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 32 7d  ..**.** {H12522}
1e910 20 54 68 65 20 64 65 66 61 75 6c 74 20 61 75 74   The default aut
1e920 68 6f 72 69 7a 65 72 20 69 73 20 4e 55 4c 4c 2e  horizer is NULL.
1e930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1e940 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  nt sqlite3_set_a
1e950 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c  uthorizer(.  sql
1e960 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78  ite3*,.  int (*x
1e970 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  Auth)(void*,int,
1e980 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
1e990 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
1e9a0 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  ar*,const char*)
1e9b0 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44  ,.  void *pUserD
1e9c0 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ata.);../*.** CA
1e9d0 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a  PI3REF: Authoriz
1e9e0 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20  er Return Codes 
1e9f0 7b 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30  {H12590} <H12500
1ea00 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  >.**.** The [sql
1ea10 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
1ea20 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72  zer | authorizer
1ea30 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
1ea40 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75  on] must.** retu
1ea50 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54  rn either [SQLIT
1ea60 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20  E_OK] or one of 
1ea70 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61  these two consta
1ea80 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20  nts in order.** 
1ea90 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65  to signal SQLite
1eaa0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1eab0 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65  the action is pe
1eac0 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68  rmitted.  See th
1ead0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65  e.** [sqlite3_se
1eae0 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61  t_authorizer | a
1eaf0 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65  uthorizer docume
1eb00 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64  ntation] for add
1eb10 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72  itional.** infor
1eb20 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  mation..*/.#defi
1eb30 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20  ne SQLITE_DENY  
1eb40 20 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68   1   /* Abort th
1eb50 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
1eb60 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f  with an error */
1eb70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1eb80 49 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f  IGNORE 2   /* Do
1eb90 6e 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73  n't allow access
1eba0 2c 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65  , but don't gene
1ebb0 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f  rate an error */
1ebc0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
1ebd0 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74  : Authorizer Act
1ebe0 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35  ion Codes {H1255
1ebf0 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a  0} <H12500>.**.*
1ec00 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  * The [sqlite3_s
1ec10 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d  et_authorizer()]
1ec20 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73   interface regis
1ec30 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20  ters a callback 
1ec40 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74  function.** that
1ec50 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61   is invoked to a
1ec60 75 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e  uthorize certain
1ec70 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61   SQL statement a
1ec80 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20  ctions.  The.** 
1ec90 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
1eca0 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
1ecb0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63   is an integer c
1ecc0 6f 64 65 20 74 68 61 74 20 73 70 65 63 69 66 69  ode that specifi
1ecd0 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f  es.** what actio
1ece0 6e 20 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f  n is being autho
1ecf0 72 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72  rized.  These ar
1ed00 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 61 63  e the integer ac
1ed10 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a  tion codes that.
1ed20 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  ** the authorize
1ed30 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62  r callback may b
1ed40 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  e passed..**.** 
1ed50 54 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64  These action cod
1ed60 65 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79  e values signify
1ed70 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70   what kind of op
1ed80 65 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65  eration is to be
1ed90 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20  .** authorized. 
1eda0 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 68   The 3rd and 4th
1edb0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74   parameters to t
1edc0 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  he authorization
1edd0 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  .** callback fun
1ede0 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61  ction will be pa
1edf0 72 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c  rameters or NULL
1ee00 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68   depending on wh
1ee10 69 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20  ich of these.** 
1ee20 63 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73  codes is used as
1ee30 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
1ee40 6d 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 20  meter.  The 5th 
1ee50 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
1ee60 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63  .** authorizer c
1ee70 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e  allback is the n
1ee80 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
1ee90 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65  ase ("main", "te
1eea0 6d 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66  mp",.** etc.) if
1eeb0 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68   applicable.  Th
1eec0 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 6th parameter 
1eed0 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  to the authorize
1eee0 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73  r callback.** is
1eef0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1ef00 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67   inner-most trig
1ef10 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 74  ger or view that
1ef20 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
1ef30 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73  for.** the acces
1ef40 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c  s attempt or NUL
1ef50 4c 20 69 66 20 74 68 69 73 20 61 63 63 65 73 73  L if this access
1ef60 20 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 65   attempt is dire
1ef70 63 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70  ctly from.** top
1ef80 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e  -level SQL code.
1ef90 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
1efa0 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 31  S:.**.** {H12551
1efb0 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72  } The second par
1efc0 61 6d 65 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20  ameter to an.** 
1efd0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
1efe0 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
1eff0 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61   | authorizer ca
1f000 6c 6c 62 61 63 6b 5d 20 73 68 61 6c 6c 20 62 65  llback] shall be
1f010 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20   an integer.**  
1f020 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f          [SQLITE_
1f030 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 7a 65  COPY | authorize
1f040 72 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65  r code] that spe
1f050 63 69 66 69 65 73 20 77 68 61 74 20 61 63 74 69  cifies what acti
1f060 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  on.**          i
1f070 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72 69 7a  s being authoriz
1f080 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35  ed..**.** {H1255
1f090 32 7d 20 54 68 65 20 33 72 64 20 61 6e 64 20 34  2} The 3rd and 4
1f0a0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
1f0b0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
1f0c0 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75   [sqlite3_set_au
1f0d0 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f  thorizer | autho
1f0e0 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
1f0f0 6b 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  k].**          s
1f100 68 61 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65  hall be paramete
1f110 72 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e  rs or NULL depen
1f120 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 0a 2a 2a  ding on which.**
1f130 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54            [SQLIT
1f140 45 5f 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69  E_COPY | authori
1f150 7a 65 72 20 63 6f 64 65 5d 20 69 73 20 75 73 65  zer code] is use
1f160 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
1f170 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a  parameter..**.**
1f180 20 7b 48 31 32 35 35 33 7d 20 54 68 65 20 35 74   {H12553} The 5t
1f190 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
1f1a0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
1f1b0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
1f1c0 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69  orizer | authori
1f1d0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68  zer callback] sh
1f1e0 61 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a  all be the name.
1f1f0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74  **          of t
1f200 68 65 20 64 61 74 61 62 61 73 65 20 28 65 78 61  he database (exa
1f210 6d 70 6c 65 3a 20 22 6d 61 69 6e 22 2c 20 22 74  mple: "main", "t
1f220 65 6d 70 22 2c 20 65 74 63 2e 29 20 69 66 20 61  emp", etc.) if a
1f230 70 70 6c 69 63 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  pplicable..**.**
1f240 20 7b 48 31 32 35 35 34 7d 20 54 68 65 20 36 74   {H12554} The 6t
1f250 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
1f260 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
1f270 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
1f280 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69  orizer | authori
1f290 7a 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68  zer callback] sh
1f2a0 61 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a  all be the name.
1f2b0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74  **          of t
1f2c0 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72  he inner-most tr
1f2d0 69 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68  igger or view th
1f2e0 61 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  at is responsibl
1f2f0 65 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  e for.**        
1f300 20 20 74 68 65 20 61 63 63 65 73 73 20 61 74 74    the access att
1f310 65 6d 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20  empt or NULL if 
1f320 74 68 69 73 20 61 63 63 65 73 73 20 61 74 74 65  this access atte
1f330 6d 70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20  mpt is directly 
1f340 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20  from.**         
1f350 20 74 6f 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63   top-level SQL c
1f360 6f 64 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ode..*/./*******
1f370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f390 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a  **** 3rd *******
1f3a0 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a  ***** 4th ******
1f3b0 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  *****/.#define S
1f3c0 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44  QLITE_CREATE_IND
1f3d0 45 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20  EX          1   
1f3e0 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20  /* Index Name   
1f3f0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
1f400 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1f410 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c  LITE_CREATE_TABL
1f420 45 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f  E          2   /
1f430 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
1f440 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1f450 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1f460 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
1f470 49 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a  INDEX     3   /*
1f480 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20   Index Name     
1f490 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1f4a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1f4b0 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54  TE_CREATE_TEMP_T
1f4c0 41 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20  ABLE     4   /* 
1f4d0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1f4e0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1f4f0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1f500 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
1f510 49 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54  IGGER   5   /* T
1f520 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54  rigger Name    T
1f530 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a  able Name      *
1f540 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1f550 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45  _CREATE_TEMP_VIE
1f560 57 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69  W      6   /* Vi
1f570 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55  ew Name       NU
1f580 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1f590 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1f5a0 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20  CREATE_TRIGGER  
1f5b0 20 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69        7   /* Tri
1f5c0 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62  gger Name    Tab
1f5d0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a  le Name      */.
1f5e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
1f5f0 52 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20  REATE_VIEW      
1f600 20 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77       8   /* View
1f610 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c   Name       NULL
1f620 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1f630 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
1f640 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20  LETE            
1f650 20 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65      9   /* Table
1f660 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20   Name      NULL 
1f670 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1f680 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f  efine SQLITE_DRO
1f690 50 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  P_INDEX         
1f6a0 20 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20    10   /* Index 
1f6b0 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20  Name      Table 
1f6c0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
1f6d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
1f6e0 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  _TABLE          
1f6f0 20 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e   11   /* Table N
1f700 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  ame      NULL   
1f710 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1f720 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
1f730 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20  TEMP_INDEX      
1f740 31 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61  12   /* Index Na
1f750 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61  me      Table Na
1f760 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  me      */.#defi
1f770 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54  ne SQLITE_DROP_T
1f780 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31  EMP_TABLE      1
1f790 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  3   /* Table Nam
1f7a0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
1f7b0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
1f7c0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  e SQLITE_DROP_TE
1f7d0 4d 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34  MP_TRIGGER    14
1f7e0 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61     /* Trigger Na
1f7f0 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65  me    Table Name
1f800 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1f810 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1f820 50 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20  P_VIEW       15 
1f830 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20    /* View Name  
1f840 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
1f850 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1f860 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47  SQLITE_DROP_TRIG
1f870 47 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20  GER         16  
1f880 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65   /* Trigger Name
1f890 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20      Table Name  
1f8a0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1f8b0 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20  QLITE_DROP_VIEW 
1f8c0 20 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20             17   
1f8d0 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20  /* View Name    
1f8e0 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1f8f0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1f900 4c 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20  LITE_INSERT     
1f910 20 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f            18   /
1f920 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
1f930 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1f940 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1f950 49 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20  ITE_PRAGMA      
1f960 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a           19   /*
1f970 20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20   Pragma Name    
1f980 20 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c   1st arg or NULL
1f990 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1f9a0 54 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20  TE_READ         
1f9b0 20 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20          20   /* 
1f9c0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1f9d0 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20  Column Name     
1f9e0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1f9f0 45 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20  E_SELECT        
1fa00 20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e         21   /* N
1fa10 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e  ULL            N
1fa20 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1fa30 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1fa40 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20  _TRANSACTION    
1fa50 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70        22   /* Op
1fa60 65 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55  eration       NU
1fa70 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1fa80 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1fa90 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20  UPDATE          
1faa0 20 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62       23   /* Tab
1fab0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c  le Name      Col
1fac0 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a  umn Name     */.
1fad0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
1fae0 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20  TTACH           
1faf0 20 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65      24   /* File
1fb00 6e 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c  name        NULL
1fb10 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1fb20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
1fb30 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20  TACH            
1fb40 20 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62     25   /* Datab
1fb50 61 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20  ase Name   NULL 
1fb60 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1fb70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54  efine SQLITE_ALT
1fb80 45 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20  ER_TABLE        
1fb90 20 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61    26   /* Databa
1fba0 73 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20  se Name   Table 
1fbb0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
1fbc0 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e  fine SQLITE_REIN
1fbd0 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20  DEX             
1fbe0 20 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e   27   /* Index N
1fbf0 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  ame      NULL   
1fc00 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1fc10 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59  ine SQLITE_ANALY
1fc20 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ZE              
1fc30 32 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  28   /* Table Na
1fc40 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1fc50 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1fc60 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
1fc70 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32  _VTABLE        2
1fc80 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  9   /* Table Nam
1fc90 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61  e      Module Na
1fca0 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  me     */.#defin
1fcb0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54  e SQLITE_DROP_VT
1fcc0 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30  ABLE          30
1fcd0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1fce0 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d        Module Nam
1fcf0 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  e     */.#define
1fd00 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e   SQLITE_FUNCTION
1fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20               31 
1fd20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20    /* NULL       
1fd30 20 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61       Function Na
1fd40 6d 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  me   */.#define 
1fd50 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54  SQLITE_SAVEPOINT
1fd60 20 20 20 20 20 20 20 20 20 20 20 20 33 32 20 20              32  
1fd70 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20   /* Operation   
1fd80 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61      Savepoint Na
1fd90 6d 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  me  */.#define S
1fda0 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20  QLITE_COPY      
1fdb0 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20              0   
1fdc0 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65  /* No longer use
1fdd0 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  d */../*.** CAPI
1fde0 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e  3REF: Tracing An
1fdf0 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63  d Profiling Func
1fe00 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c  tions {H12280} <
1fe10 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S60400>.** EXPER
1fe20 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
1fe30 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67  ese routines reg
1fe40 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66  ister callback f
1fe50 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61  unctions that ca
1fe60 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a  n be used for.**
1fe70 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f   tracing and pro
1fe80 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75  filing the execu
1fe90 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74  tion of SQL stat
1fea0 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ements..**.** Th
1feb0 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
1fec0 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62  ion registered b
1fed0 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28  y sqlite3_trace(
1fee0 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a  ) is invoked at.
1fef0 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73  ** various times
1ff00 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61   when an SQL sta
1ff10 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20  tement is being 
1ff20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  run by [sqlite3_
1ff30 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20  step()]..** The 
1ff40 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
1ff50 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69   a UTF-8 renderi
1ff60 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74  ng of the SQL st
1ff70 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20  atement text.** 
1ff80 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  as the statement
1ff90 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78   first begins ex
1ffa0 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69  ecuting.  Additi
1ffb0 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f  onal callbacks o
1ffc0 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20  ccur.** as each 
1ffd0 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f  triggered subpro
1ffe0 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e  gram is entered.
1fff0 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20    The callbacks 
20000 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20  for triggers.** 
20010 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20  contain a UTF-8 
20020 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74  SQL comment that
20030 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
20040 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  trigger..**.** T
20050 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
20060 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
20070 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  by sqlite3_profi
20080 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a  le() is invoked.
20090 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73  ** as each SQL s
200a0 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65  tatement finishe
200b0 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20  s.  The profile 
200c0 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
200d0 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61  s.** the origina
200e0 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74  l statement text
200f0 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65   and an estimate
20100 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74   of wall-clock t
20110 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f  ime.** of how lo
20120 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e  ng that statemen
20130 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a  t took to run..*
20140 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
20150 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38 31 7d 20  .**.** {H12281} 
20160 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  The callback fun
20170 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
20180 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 72 61   by [sqlite3_tra
20190 63 65 28 29 5d 20 0a 2a 2a 20 20 20 20 20 20 20  ce()] .**       
201a0 20 20 20 73 68 61 6c 6c 20 62 65 20 69 6e 76 6f     shall be invo
201b0 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ked.**          
201c0 77 68 65 6e 65 76 65 72 20 61 6e 20 53 51 4c 20  whenever an SQL 
201d0 73 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 20  statement first 
201e0 62 65 67 69 6e 73 20 74 6f 20 65 78 65 63 75 74  begins to execut
201f0 65 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  e and.**        
20200 20 20 77 68 65 6e 65 76 65 72 20 61 20 74 72 69    whenever a tri
20210 67 67 65 72 20 73 75 62 70 72 6f 67 72 61 6d 20  gger subprogram 
20220 66 69 72 73 74 20 62 65 67 69 6e 73 20 74 6f 20  first begins to 
20230 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32  run..**.** {H122
20240 38 32 7d 20 45 61 63 68 20 63 61 6c 6c 20 74 6f  82} Each call to
20250 20 5b 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28   [sqlite3_trace(
20260 29 5d 20 73 68 61 6c 6c 20 6f 76 65 72 72 69 64  )] shall overrid
20270 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  e the previously
20280 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67  .**          reg
20290 69 73 74 65 72 65 64 20 74 72 61 63 65 20 63 61  istered trace ca
202a0 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 7b 48  llback..**.** {H
202b0 31 32 32 38 33 7d 20 41 20 4e 55 4c 4c 20 74 72  12283} A NULL tr
202c0 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61  ace callback sha
202d0 6c 6c 20 64 69 73 61 62 6c 65 20 74 72 61 63 69  ll disable traci
202e0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38  ng..**.** {H1228
202f0 34 7d 20 54 68 65 20 66 69 72 73 74 20 61 72 67  4} The first arg
20300 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61  ument to the tra
20310 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61 6c  ce callback shal
20320 6c 20 62 65 20 61 20 63 6f 70 79 20 6f 66 0a 2a  l be a copy of.*
20330 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 70  *          the p
20340 6f 69 6e 74 65 72 20 77 68 69 63 68 20 77 61 73  ointer which was
20350 20 74 68 65 20 33 72 64 20 61 72 67 75 6d 65 6e   the 3rd argumen
20360 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 72  t to [sqlite3_tr
20370 61 63 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ace()]..**.** {H
20380 31 32 32 38 35 7d 20 54 68 65 20 73 65 63 6f 6e  12285} The secon
20390 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
203a0 65 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b  e trace callback
203b0 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20   is a.**        
203c0 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65    zero-terminate
203d0 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63  d UTF-8 string c
203e0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72  ontaining the or
203f0 69 67 69 6e 61 6c 20 74 65 78 74 0a 2a 2a 20 20  iginal text.**  
20400 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 53          of the S
20410 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20  QL statement as 
20420 69 74 20 77 61 73 20 70 61 73 73 65 64 20 69 6e  it was passed in
20430 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  to [sqlite3_prep
20440 61 72 65 5f 76 32 28 29 5d 0a 2a 2a 20 20 20 20  are_v2()].**    
20450 20 20 20 20 20 20 6f 72 20 74 68 65 20 65 71 75        or the equ
20460 69 76 61 6c 65 6e 74 2c 20 6f 72 20 61 6e 20 53  ivalent, or an S
20470 51 4c 20 63 6f 6d 6d 65 6e 74 20 69 6e 64 69 63  QL comment indic
20480 61 74 69 6e 67 20 74 68 65 20 62 65 67 69 6e 6e  ating the beginn
20490 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
204a0 6f 66 20 61 20 74 72 69 67 67 65 72 20 73 75 62  of a trigger sub
204b0 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b  program..**.** {
204c0 48 31 32 32 38 37 7d 20 54 68 65 20 63 61 6c 6c  H12287} The call
204d0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65  back function re
204e0 67 69 73 74 65 72 65 64 20 62 79 20 5b 73 71 6c  gistered by [sql
204f0 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 5d 20  ite3_profile()] 
20500 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 20 20  is invoked.**   
20510 20 20 20 20 20 20 20 61 73 20 65 61 63 68 20 53         as each S
20520 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 69 6e  QL statement fin
20530 69 73 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ishes..**.** {H1
20540 32 32 38 38 7d 20 54 68 65 20 66 69 72 73 74 20  2288} The first 
20550 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
20560 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63   profile callbac
20570 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a  k is a copy of.*
20580 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 33  *          the 3
20590 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
205a0 5b 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65  [sqlite3_profile
205b0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32  ()]..**.** {H122
205c0 38 39 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70  89} The second p
205d0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
205e0 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b  profile callback
205f0 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20   is a.**        
20600 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65    zero-terminate
20610 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74  d UTF-8 string t
20620 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
20630 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f   complete text o
20640 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  f.**          th
20650 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
20660 61 73 20 69 74 20 77 61 73 20 70 72 6f 63 65 73  as it was proces
20670 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  sed by [sqlite3_
20680 70 72 65 70 61 72 65 5f 76 32 28 29 5d 0a 2a 2a  prepare_v2()].**
20690 20 20 20 20 20 20 20 20 20 20 6f 72 20 74 68 65            or the
206a0 20 65 71 75 69 76 61 6c 65 6e 74 2e 0a 2a 2a 0a   equivalent..**.
206b0 2a 2a 20 7b 48 31 32 32 39 30 7d 20 54 68 65 20  ** {H12290} The 
206c0 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
206d0 74 6f 20 74 68 65 20 70 72 6f 66 69 6c 65 20 63  to the profile c
206e0 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 65 73  allback is an es
206f0 74 69 6d 61 74 65 0a 2a 2a 20 20 20 20 20 20 20  timate.**       
20700 20 20 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72     of the number
20710 20 6f 66 20 6e 61 6e 6f 73 65 63 6f 6e 64 73 20   of nanoseconds 
20720 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69  of wall-clock ti
20730 6d 65 20 72 65 71 75 69 72 65 64 20 74 6f 0a 2a  me required to.*
20740 2a 20 20 20 20 20 20 20 20 20 20 72 75 6e 20 74  *          run t
20750 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
20760 20 66 72 6f 6d 20 73 74 61 72 74 20 74 6f 20 66   from start to f
20770 69 6e 69 73 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inish..*/.SQLITE
20780 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
20790 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a 73  RIMENTAL void *s
207a0 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c  qlite3_trace(sql
207b0 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72  ite3*, void(*xTr
207c0 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ace)(void*,const
207d0 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b   char*), void*);
207e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
207f0 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
20800 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72  void *sqlite3_pr
20810 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0a  ofile(sqlite3*,.
20820 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c     void(*xProfil
20830 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
20840 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e  har*,sqlite3_uin
20850 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f  t64), void*);../
20860 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 51  *.** CAPI3REF: Q
20870 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43 61  uery Progress Ca
20880 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30 7d  llbacks {H12910}
20890 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S60400>.**.** 
208a0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e  This routine con
208b0 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62 61  figures a callba
208c0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74 68  ck function - th
208d0 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63 61  e.** progress ca
208e0 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69 73  llback - that is
208f0 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64 69   invoked periodi
20900 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f 6e  cally during lon
20910 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61 6c  g.** running cal
20920 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65  ls to [sqlite3_e
20930 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  xec()], [sqlite3
20940 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a 20  _step()] and.** 
20950 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  [sqlite3_get_tab
20960 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d 70  le()].  An examp
20970 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73 0a  le use for this.
20980 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  ** interface is 
20990 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75 70  to keep a GUI up
209a0 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20 6c  dated during a l
209b0 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a  arge query..**.*
209c0 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73  * If the progres
209d0 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  s callback retur
209e0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  ns non-zero, the
209f0 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a   operation is.**
20a00 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20 54   interrupted.  T
20a10 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e 20  his feature can 
20a20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  be used to imple
20a30 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63 65  ment a.** "Cance
20a40 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20 47  l" button on a G
20a50 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61 6c  UI progress dial
20a60 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 68  og box..**.** Th
20a70 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c  e progress handl
20a80 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61  er must not do a
20a90 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c  nything that wil
20aa0 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20  l modify.** the 
20ab0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
20ac0 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  ion that invoked
20ad0 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68 61   the progress ha
20ae0 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20 74  ndler..** Note t
20af0 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  hat [sqlite3_pre
20b00 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b  pare_v2()] and [
20b10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
20b20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69  both modify thei
20b30 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  r.** database co
20b40 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68  nnections for th
20b50 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f  e meaning of "mo
20b60 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61  dify" in this pa
20b70 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 49  ragraph..**.** I
20b80 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
20b90 20 7b 48 31 32 39 31 31 7d 20 54 68 65 20 63 61   {H12911} The ca
20ba0 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
20bb0 72 65 67 69 73 74 65 72 65 64 20 62 79 20 73 71  registered by sq
20bc0 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
20bd0 61 6e 64 6c 65 72 28 29 0a 2a 2a 20 20 20 20 20  andler().**     
20be0 20 20 20 20 20 69 73 20 69 6e 76 6f 6b 65 64 20       is invoked 
20bf0 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64 75 72  periodically dur
20c00 69 6e 67 20 6c 6f 6e 67 20 72 75 6e 6e 69 6e 67  ing long running
20c10 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20 20   calls to.**    
20c20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73        [sqlite3_s
20c30 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  tep()]..**.** {H
20c40 31 32 39 31 32 7d 20 54 68 65 20 70 72 6f 67 72  12912} The progr
20c50 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ess callback is 
20c60 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72  invoked once for
20c70 20 65 76 65 72 79 20 4e 20 76 69 72 74 75 61 6c   every N virtual
20c80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 61 63  .**          mac
20c90 68 69 6e 65 20 6f 70 63 6f 64 65 73 2c 20 77 68  hine opcodes, wh
20ca0 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63  ere N is the sec
20cb0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a  ond argument to.
20cc0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
20cd0 5b 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73  [sqlite3_progres
20ce0 73 5f 68 61 6e 64 6c 65 72 28 29 5d 20 63 61 6c  s_handler()] cal
20cf0 6c 20 74 68 61 74 20 72 65 67 69 73 74 65 72 65  l that registere
20d00 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  d.**          th
20d10 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 49 66 20  e callback.  If 
20d20 4e 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 31  N is less than 1
20d30 2c 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65  , sqlite3_progre
20d40 73 73 5f 68 61 6e 64 6c 65 72 28 29 0a 2a 2a 20  ss_handler().** 
20d50 20 20 20 20 20 20 20 20 20 61 63 74 73 20 61 73           acts as
20d60 20 69 66 20 61 20 4e 55 4c 4c 20 70 72 6f 67 72   if a NULL progr
20d70 65 73 73 20 68 61 6e 64 6c 65 72 20 68 61 64 20  ess handler had 
20d80 62 65 65 6e 20 73 70 65 63 69 66 69 65 64 2e 0a  been specified..
20d90 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31 33 7d 20 54  **.** {H12913} T
20da0 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c  he progress call
20db0 62 61 63 6b 20 69 74 73 65 6c 66 20 69 73 20 69  back itself is i
20dc0 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
20dd0 20 74 68 69 72 64 0a 2a 2a 20 20 20 20 20 20 20   third.**       
20de0 20 20 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73     argument to s
20df0 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f  qlite3_progress_
20e00 68 61 6e 64 6c 65 72 28 29 2e 0a 2a 2a 0a 2a 2a  handler()..**.**
20e10 20 7b 48 31 32 39 31 34 7d 20 54 68 65 20 66 6f   {H12914} The fo
20e20 75 72 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f  urth argument to
20e30 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
20e40 73 5f 68 61 6e 64 6c 65 72 28 29 20 69 73 20 61  s_handler() is a
20e50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 6f 69  .**          voi
20e60 64 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64  d pointer passed
20e70 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73   to the progress
20e80 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20   callback.**    
20e90 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 65        function e
20ea0 61 63 68 20 74 69 6d 65 20 69 74 20 69 73 20 69  ach time it is i
20eb0 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48  nvoked..**.** {H
20ec0 31 32 39 31 35 7d 20 49 66 20 61 20 63 61 6c 6c  12915} If a call
20ed0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   to [sqlite3_ste
20ee0 70 28 29 5d 20 72 65 73 75 6c 74 73 20 69 6e 20  p()] results in 
20ef0 66 65 77 65 72 20 74 68 61 6e 20 4e 20 6f 70 63  fewer than N opc
20f00 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  odes.**         
20f10 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 2c   being executed,
20f20 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 65   then the progre
20f30 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e  ss callback is n
20f40 65 76 65 72 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a  ever invoked..**
20f50 0a 2a 2a 20 7b 48 31 32 39 31 36 7d 20 45 76 65  .** {H12916} Eve
20f60 72 79 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ry call to [sqli
20f70 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e  te3_progress_han
20f80 64 6c 65 72 28 29 5d 0a 2a 2a 20 20 20 20 20 20  dler()].**      
20f90 20 20 20 20 6f 76 65 72 77 72 69 74 65 73 20 61      overwrites a
20fa0 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  ny previously re
20fb0 67 69 73 74 65 72 65 64 20 70 72 6f 67 72 65 73  gistered progres
20fc0 73 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a  s handler..**.**
20fd0 20 7b 48 31 32 39 31 37 7d 20 49 66 20 74 68 65   {H12917} If the
20fe0 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65   progress handle
20ff0 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55  r callback is NU
21000 4c 4c 20 74 68 65 6e 20 6e 6f 20 70 72 6f 67 72  LL then no progr
21010 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ess.**          
21020 68 61 6e 64 6c 65 72 20 69 73 20 69 6e 76 6f 6b  handler is invok
21030 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31  ed..**.** {H1291
21040 38 7d 20 49 66 20 74 68 65 20 70 72 6f 67 72 65  8} If the progre
21050 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  ss callback retu
21060 72 6e 73 20 61 20 72 65 73 75 6c 74 20 6f 74 68  rns a result oth
21070 65 72 20 74 68 61 6e 20 30 2c 20 74 68 65 6e 0a  er than 0, then.
21080 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
21090 62 65 68 61 76 69 6f 72 20 69 73 20 61 20 69 66  behavior is a if
210a0 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72   [sqlite3_interr
210b0 75 70 74 28 29 5d 20 68 61 64 20 62 65 65 6e 20  upt()] had been 
210c0 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 20 20 20  called..**      
210d0 20 20 20 20 3c 53 33 30 35 30 30 3e 0a 2a 2f 0a      <S30500>.*/.
210e0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
210f0 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
21100 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33  _handler(sqlite3
21110 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76  *, int, int(*)(v
21120 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  oid*), void*);..
21130 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
21140 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61  Opening A New Da
21150 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
21160 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32  n {H12700} <S402
21170 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
21180 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e  routines open an
21190 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
211a0 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65   file whose name
211b0 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65   is given by the
211c0 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  .** filename arg
211d0 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e  ument. The filen
211e0 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ame argument is 
211f0 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55  interpreted as U
21200 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69  TF-8 for.** sqli
21210 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73  te3_open() and s
21220 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
21230 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69   and as UTF-16 i
21240 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74  n the native byt
21250 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73  e.** order for s
21260 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e  qlite3_open16().
21270 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   A [database con
21280 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20  nection] handle 
21290 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65  is usually.** re
212a0 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c  turned in *ppDb,
212b0 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f   even if an erro
212c0 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f  r occurs.  The o
212d0 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73  nly exception is
212e0 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69   that.** if SQLi
212f0 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  te is unable to 
21300 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  allocate memory 
21310 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c  to hold the [sql
21320 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a  ite3] object,.**
21330 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20   a NULL will be 
21340 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70  written into *pp
21350 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  Db instead of a 
21360 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b  pointer to the [
21370 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65  sqlite3].** obje
21380 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62  ct. If the datab
21390 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61  ase is opened (a
213a0 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73  nd/or created) s
213b0 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65  uccessfully, the
213c0 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  n.** [SQLITE_OK]
213d0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f   is returned.  O
213e0 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72  therwise an [err
213f0 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
21400 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73  rned.  The.** [s
21410 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d  qlite3_errmsg()]
21420 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   or [sqlite3_err
21430 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65  msg16()] routine
21440 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  s can be used to
21450 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e   obtain.** an En
21460 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64  glish language d
21470 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
21480 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  e error..**.** T
21490 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64  he default encod
214a0 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61  ing for the data
214b0 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46  base will be UTF
214c0 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33  -8 if.** sqlite3
214d0 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74  _open() or sqlit
214e0 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20  e3_open_v2() is 
214f0 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54  called and.** UT
21500 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69  F-16 in the nati
21510 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66  ve byte order if
21520 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
21530 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  ) is used..**.**
21540 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
21550 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
21560 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65  when it is opene
21570 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20  d, resources.** 
21580 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
21590 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
215a0 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65  nnection] handle
215b0 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61   should be relea
215c0 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e  sed by.** passin
215d0 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  g it to [sqlite3
215e0 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69  _close()] when i
215f0 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72  t is no longer r
21600 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  equired..**.** T
21610 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  he sqlite3_open_
21620 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77  v2() interface w
21630 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65  orks like sqlite
21640 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65  3_open().** exce
21650 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70  pt that it accep
21660 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61  ts two additiona
21670 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72  l parameters for
21680 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74   additional cont
21690 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20  rol.** over the 
216a0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e  new database con
216b0 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c  nection.  The fl
216c0 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61  ags parameter ca
216d0 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a  n take one of.**
216e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
216f0 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74  hree values, opt
21700 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64  ionally combined
21710 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53   with the .** [S
21720 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54  QLITE_OPEN_NOMUT
21730 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4f  EX] or [SQLITE_O
21740 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66  PEN_FULLMUTEX] f
21750 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  lags:.**.** <dl>
21760 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f  .** <dt>[SQLITE_
21770 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f  OPEN_READONLY]</
21780 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64  dt>.** <dd>The d
21790 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
217a0 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d  d in read-only m
217b0 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74  ode.  If the dat
217c0 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a  abase does not.*
217d0 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c  * already exist,
217e0 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
217f0 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  urned.</dd>.**.*
21800 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50  * <dt>[SQLITE_OP
21810 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64  EN_READWRITE]</d
21820 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61  t>.** <dd>The da
21830 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
21840 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64   for reading and
21850 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73   writing if poss
21860 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67  ible, or reading
21870 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20  .** only if the 
21880 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72  file is write pr
21890 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f  otected by the o
218a0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e  perating system.
218b0 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63    In either.** c
218c0 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ase the database
218d0 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78   must already ex
218e0 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ist, otherwise a
218f0 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
21900 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
21910 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  <dt>[SQLITE_OPEN
21920 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53  _READWRITE] | [S
21930 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
21940 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  E]</dt>.** <dd>T
21950 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f  he database is o
21960 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e  pened for readin
21970 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61  g and writing, a
21980 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74  nd is creates it
21990 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e   if.** it does n
219a0 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
219b0 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65  . This is the be
219c0 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61  havior that is a
219d0 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a  lways used for.*
219e0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
219f0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65   and sqlite3_ope
21a00 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c  n16().</dd>.** <
21a10 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  /dl>.**.** If th
21a20 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
21a30 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  to sqlite3_open_
21a40 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20  v2() is not one 
21a50 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e  of the.** combin
21a60 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f  ations shown abo
21a70 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  ve or one of the
21a80 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68   combinations sh
21a90 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e  own above combin
21aa0 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b  ed.** with the [
21ab0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
21ac0 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  TEX] or [SQLITE_
21ad0 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20  OPEN_FULLMUTEX] 
21ae0 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74  flags,.** then t
21af0 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
21b00 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
21b10 49 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f  If the [SQLITE_O
21b20 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61  PEN_NOMUTEX] fla
21b30 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74  g is set, then t
21b40 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
21b50 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20  ection.** opens 
21b60 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72  in the multi-thr
21b70 65 61 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d  ead [threading m
21b80 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20  ode] as long as 
21b90 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61  the single-threa
21ba0 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f  d.** mode has no
21bb0 74 20 62 65 65 6e 20 73 65 74 20 61 74 20 63 6f  t been set at co
21bc0 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74  mpile-time or st
21bd0 61 72 74 2d 74 69 6d 65 2e 20 20 49 66 20 74 68  art-time.  If th
21be0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45  e.** [SQLITE_OPE
21bf0 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61  N_FULLMUTEX] fla
21c00 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 74 68  g is set then th
21c10 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
21c20 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69  ction opens.** i
21c30 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64  n the serialized
21c40 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
21c50 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d  ] unless single-
21c60 74 68 72 65 61 64 20 77 61 73 0a 2a 2a 20 70 72  thread was.** pr
21c70 65 76 69 6f 75 73 6c 79 20 73 65 6c 65 63 74 65  eviously selecte
21c80 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  d at compile-tim
21c90 65 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e  e or start-time.
21ca0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
21cb0 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f  lename is ":memo
21cc0 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72 69  ry:", then a pri
21cd0 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 20  vate, temporary 
21ce0 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
21cf0 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65 64  se.** is created
21d00 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74   for the connect
21d10 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d 65  ion.  This in-me
21d20 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69  mory database wi
21d30 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a 2a  ll vanish when.*
21d40 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  * the database c
21d50 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
21d60 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 72  sed.  Future ver
21d70 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
21d80 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75 73  might.** make us
21d90 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  e of additional 
21da0 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d 65  special filename
21db0 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
21dc0 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 63  h the ":" charac
21dd0 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72 65  ter..** It is re
21de0 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 77  commended that w
21df0 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  hen a database f
21e00 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c 79  ilename actually
21e10 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74 68   does begin with
21e20 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61 63  .** a ":" charac
21e30 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20 70  ter you should p
21e40 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e 61  refix the filena
21e50 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e 61  me with a pathna
21e60 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22 2e  me such as.** ".
21e70 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62 69  /" to avoid ambi
21e80 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  guity..**.** If 
21e90 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  the filename is 
21ea0 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c  an empty string,
21eb0 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c   then a private,
21ec0 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f 6e   temporary.** on
21ed0 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 77  -disk database w
21ee0 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e 20  ill be created. 
21ef0 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64 61   This private da
21f00 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a 2a  tabase will be.*
21f10 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
21f20 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e 20  deleted as soon 
21f30 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  as the database 
21f40 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c  connection is cl
21f50 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  osed..**.** The 
21f60 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
21f70 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   to sqlite3_open
21f80 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61 6d  _v2() is the nam
21f90 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  e of the.** [sql
21fa0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
21fb0 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
21fc0 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
21fd0 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68 61  em interface tha
21fe0 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74  t.** the new dat
21ff0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
22000 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49 66   should use.  If
22010 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61   the fourth para
22020 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e 55  meter is.** a NU
22030 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20  LL pointer then 
22040 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c  the default [sql
22050 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
22060 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   is used..**.** 
22070 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64 6f  <b>Note to Windo
22080 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54  ws users:</b>  T
22090 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64  he encoding used
220a0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 6d   for the filenam
220b0 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f 66  e argument.** of
220c0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
220d0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  and sqlite3_open
220e0 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55 54  _v2() must be UT
220f0 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 65  F-8, not whateve
22100 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 73  r.** codepage is
22110 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e   currently defin
22120 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 63  ed.  Filenames c
22130 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 6e  ontaining intern
22140 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 61  ational.** chara
22150 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 6f  cters must be co
22160 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d 38  nverted to UTF-8
22170 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 6e   prior to passin
22180 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 73  g them into.** s
22190 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72  qlite3_open() or
221a0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
221b0 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ()..**.** INVARI
221c0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  ANTS:.**.** {H12
221d0 37 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  701} The [sqlite
221e0 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69  3_open()], [sqli
221f0 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 61  te3_open16()], a
22200 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  nd.**          [
22210 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
22220 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 63 72  )] interfaces cr
22230 65 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20 20  eate a new.**   
22240 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65         [database
22250 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73 73   connection] ass
22260 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20  ociated with.** 
22270 20 20 20 20 20 20 20 20 20 74 68 65 20 64 61 74           the dat
22280 61 62 61 73 65 20 66 69 6c 65 20 67 69 76 65 6e  abase file given
22290 20 69 6e 20 74 68 65 69 72 20 66 69 72 73 74 20   in their first 
222a0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a  parameter..**.**
222b0 20 7b 48 31 32 37 30 32 7d 20 54 68 65 20 66 69   {H12702} The fi
222c0 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20  lename argument 
222d0 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  is interpreted a
222e0 73 20 55 54 46 2d 38 0a 2a 2a 20 20 20 20 20 20  s UTF-8.**      
222f0 20 20 20 20 66 6f 72 20 5b 73 71 6c 69 74 65 33      for [sqlite3
22300 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 5b 73 71  _open()] and [sq
22310 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
22320 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 0a 2a   and as UTF-16.*
22330 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68  *          in th
22340 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  e native byte or
22350 64 65 72 20 66 6f 72 20 5b 73 71 6c 69 74 65 33  der for [sqlite3
22360 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a 2a  _open16()]..**.*
22370 2a 20 7b 48 31 32 37 30 33 7d 20 41 20 73 75 63  * {H12703} A suc
22380 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69  cessful invocati
22390 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6f  on of [sqlite3_o
223a0 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  pen()], [sqlite3
223b0 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20  _open16()],.**  
223c0 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c 69          or [sqli
223d0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 77  te3_open_v2()] w
223e0 72 69 74 65 73 20 61 20 70 6f 69 6e 74 65 72 20  rites a pointer 
223f0 74 6f 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20  to a new.**     
22400 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63       [database c
22410 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 6e 74 6f 20  onnection] into 
22420 2a 70 70 44 62 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  *ppDb..**.** {H1
22430 32 37 30 34 7d 20 54 68 65 20 5b 73 71 6c 69 74  2704} The [sqlit
22440 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c  e3_open()], [sql
22450 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20  ite3_open16()], 
22460 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
22470 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
22480 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 72  ()] interfaces r
22490 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b  eturn [SQLITE_OK
224a0 5d 20 75 70 6f 6e 20 73 75 63 63 65 73 73 2c 0a  ] upon success,.
224b0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 61  **          or a
224c0 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65  n appropriate [e
224d0 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66 61  rror code] on fa
224e0 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ilure..**.** {H1
224f0 32 37 30 36 7d 20 54 68 65 20 64 65 66 61 75 6c  2706} The defaul
22500 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  t text encoding 
22510 66 6f 72 20 61 20 6e 65 77 20 64 61 74 61 62 61  for a new databa
22520 73 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  se created using
22530 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
22540 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 6f 72  lite3_open()] or
22550 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
22560 32 28 29 5d 20 77 69 6c 6c 20 62 65 20 55 54 46  2()] will be UTF
22570 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 30  -8..**.** {H1270
22580 37 7d 20 54 68 65 20 64 65 66 61 75 6c 74 20 74  7} The default t
22590 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  ext encoding for
225a0 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
225b0 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a  created using.**
225c0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
225d0 65 33 5f 6f 70 65 6e 31 36 28 29 5d 20 77 69 6c  e3_open16()] wil
225e0 6c 20 62 65 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a  l be UTF-16..**.
225f0 2a 2a 20 7b 48 31 32 37 30 39 7d 20 54 68 65 20  ** {H12709} The 
22600 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 46 2c  [sqlite3_open(F,
22610 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 69 73  D)] interface is
22620 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 2a   equivalent to.*
22630 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
22640 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 2c  te3_open_v2(F,D,
22650 47 2c 30 29 5d 20 77 68 65 72 65 20 74 68 65 20  G,0)] where the 
22660 47 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a  G parameter is.*
22670 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49  *          [SQLI
22680 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
22690 45 5d 7c 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  E]|[SQLITE_OPEN_
226a0 43 52 45 41 54 45 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  CREATE]..**.** {
226b0 48 31 32 37 31 31 7d 20 49 66 20 74 68 65 20 47  H12711} If the G
226c0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
226d0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46  qlite3_open_v2(F
226e0 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74 61 69 6e  ,D,G,V)] contain
226f0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
22700 20 20 62 69 74 20 76 61 6c 75 65 20 5b 53 51 4c    bit value [SQL
22710 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
22720 59 5d 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  Y] then the data
22730 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 0a 2a  base is opened.*
22740 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 72  *          for r
22750 65 61 64 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2a 0a  eading only..**.
22760 2a 2a 20 7b 48 31 32 37 31 32 7d 20 49 66 20 74  ** {H12712} If t
22770 68 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74  he G parameter t
22780 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  o [sqlite3_open_
22790 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e  v2(F,D,G,V)] con
227a0 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20  tains the.**    
227b0 20 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20        bit value 
227c0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  [SQLITE_OPEN_REA
227d0 44 57 52 49 54 45 5d 20 74 68 65 6e 20 74 68 65  DWRITE] then the
227e0 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
227f0 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ned.**          
22800 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74  reading and writ
22810 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c 65 2c  ing if possible,
22820 20 6f 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20   or for reading 
22830 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 20  only if the.**  
22840 20 20 20 20 20 20 20 20 66 69 6c 65 20 69 73 20          file is 
22850 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64 20  write protected 
22860 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  by the operating
22870 20 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 7b   system..**.** {
22880 48 31 32 37 31 33 7d 20 49 66 20 74 68 65 20 47  H12713} If the G
22890 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
228a0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46  qlite3_open_v2(F
228b0 2c 44 2c 47 2c 56 29 5d 20 6f 6d 69 74 73 20 74  ,D,G,V)] omits t
228c0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62  he.**          b
228d0 69 74 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45  it value [SQLITE
228e0 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 20 61 6e  _OPEN_CREATE] an
228f0 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64  d the database d
22900 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20  oes not.**      
22910 20 20 20 20 70 72 65 76 69 6f 75 73 6c 79 20 65      previously e
22920 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20 69  xist, an error i
22930 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
22940 2a 20 7b 48 31 32 37 31 34 7d 20 49 66 20 74 68  * {H12714} If th
22950 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74 6f  e G parameter to
22960 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
22970 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74  2(F,D,G,V)] cont
22980 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 20  ains the.**     
22990 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20 5b       bit value [
229a0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
229b0 54 45 5d 20 61 6e 64 20 74 68 65 20 64 61 74 61  TE] and the data
229c0 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a  base does not.**
229d0 20 20 20 20 20 20 20 20 20 20 70 72 65 76 69 6f            previo
229e0 75 73 6c 79 20 65 78 69 73 74 2c 20 74 68 65 6e  usly exist, then
229f0 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
22a00 61 64 65 20 74 6f 20 63 72 65 61 74 65 20 61 6e  ade to create an
22a10 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  d.**          in
22a20 69 74 69 61 6c 69 7a 65 20 74 68 65 20 64 61 74  itialize the dat
22a30 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  abase..**.** {H1
22a40 32 37 31 37 7d 20 49 66 20 74 68 65 20 66 69 6c  2717} If the fil
22a50 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74  ename argument t
22a60 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  o [sqlite3_open(
22a70 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
22a80 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  n16()],.**      
22a90 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f      or [sqlite3_
22aa0 6f 70 65 6e 5f 76 32 28 29 5d 20 69 73 20 22 3a  open_v2()] is ":
22ab0 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61  memory:", then a
22ac0 6e 20 70 72 69 76 61 74 65 2c 0a 2a 2a 20 20 20  n private,.**   
22ad0 20 20 20 20 20 20 20 65 70 68 65 6d 65 72 61 6c         ephemeral
22ae0 2c 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  , in-memory data
22af0 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 20  base is created 
22b00 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  for the connecti
22b10 6f 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  on..**          
22b20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49 54 45 5f  <todo>Is SQLITE_
22b30 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49  OPEN_CREATE|SQLI
22b40 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
22b50 45 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 20 20  E required.**   
22b60 20 20 20 20 20 20 20 69 6e 20 73 71 6c 69 74 65         in sqlite
22b70 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c 2f 74 6f  3_open_v2()?</to
22b80 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 31  do>.**.** {H1271
22b90 39 7d 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61  9} If the filena
22ba0 6d 65 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e  me is NULL or an
22bb0 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 20 74   empty string, t
22bc0 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 0a 2a  hen a private,.*
22bd0 2a 20 20 20 20 20 20 20 20 20 20 65 70 68 65 6d  *          ephem
22be0 65 72 61 6c 20 6f 6e 2d 64 69 73 6b 20 64 61 74  eral on-disk dat
22bf0 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72  abase will be cr
22c00 65 61 74 65 64 2e 0a 2a 2a 20 20 20 20 20 20 20  eated..**       
22c10 20 20 20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49     <todo>Is SQLI
22c20 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53  TE_OPEN_CREATE|S
22c30 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
22c40 52 49 54 45 20 72 65 71 75 69 72 65 64 0a 2a 2a  RITE required.**
22c50 20 20 20 20 20 20 20 20 20 20 69 6e 20 73 71 6c            in sql
22c60 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c  ite3_open_v2()?<
22c70 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31  /todo>.**.** {H1
22c80 32 37 32 31 7d 20 54 68 65 20 5b 64 61 74 61 62  2721} The [datab
22c90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
22ca0 63 72 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69  created by [sqli
22cb0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 2c  te3_open_v2(F,D,
22cc0 47 2c 56 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20  G,V)].**        
22cd0 20 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 5b    will use the [
22ce0 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a  sqlite3_vfs] obj
22cf0 65 63 74 20 69 64 65 6e 74 69 66 69 65 64 20 62  ect identified b
22d00 79 20 74 68 65 20 56 20 70 61 72 61 6d 65 74 65  y the V paramete
22d10 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  r,.**          o
22d20 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  r the default [s
22d30 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
22d40 63 74 20 69 66 20 56 20 69 73 20 61 20 4e 55 4c  ct if V is a NUL
22d50 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
22d60 20 7b 48 31 32 37 32 33 7d 20 54 77 6f 20 5b 64   {H12723} Two [d
22d70 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
22d80 6f 6e 73 5d 20 77 69 6c 6c 20 73 68 61 72 65 20  ons] will share 
22d90 61 20 63 6f 6d 6d 6f 6e 20 63 61 63 68 65 20 69  a common cache i
22da0 66 20 62 6f 74 68 20 77 65 72 65 0a 2a 2a 20 20  f both were.**  
22db0 20 20 20 20 20 20 20 20 6f 70 65 6e 65 64 20 77          opened w
22dc0 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 46 53  ith the same VFS
22dd0 20 77 68 69 6c 65 20 5b 73 68 61 72 65 64 20 63   while [shared c
22de0 61 63 68 65 20 6d 6f 64 65 5d 20 77 61 73 20 65  ache mode] was e
22df0 6e 61 62 6c 65 64 20 61 6e 64 0a 2a 2a 20 20 20  nabled and.**   
22e00 20 20 20 20 20 20 20 69 66 20 62 6f 74 68 20 66         if both f
22e10 69 6c 65 6e 61 6d 65 73 20 63 6f 6d 70 61 72 65  ilenames compare
22e20 20 65 71 75 61 6c 20 75 73 69 6e 67 20 6d 65 6d   equal using mem
22e30 63 6d 70 28 29 20 61 66 74 65 72 20 68 61 76 69  cmp() after havi
22e40 6e 67 20 62 65 65 6e 0a 2a 2a 20 20 20 20 20 20  ng been.**      
22e50 20 20 20 20 70 72 6f 63 65 73 73 65 64 20 62 79      processed by
22e60 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   the [sqlite3_vf
22e70 73 20 7c 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d  s | xFullPathnam
22e80 65 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  e] method of the
22e90 20 56 46 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   VFS..*/.SQLITE_
22ea0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
22eb0 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68  open(.  const ch
22ec0 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20  ar *filename,   
22ed0 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65  /* Database file
22ee0 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a  name (UTF-8) */.
22ef0 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62    sqlite3 **ppDb
22f00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
22f10 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64  : SQLite db hand
22f20 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f  le */.);.SQLITE_
22f30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
22f40 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20  open16(.  const 
22f50 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c 20  void *filename, 
22f60 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69    /* Database fi
22f70 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 20  lename (UTF-16) 
22f80 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70  */.  sqlite3 **p
22f90 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20  pDb          /* 
22fa0 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68  OUT: SQLite db h
22fb0 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49  andle */.);.SQLI
22fc0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22fd0 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f  e3_open_v2(.  co
22fe0 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61  nst char *filena
22ff0 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73  me,   /* Databas
23000 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d  e filename (UTF-
23010 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  8) */.  sqlite3 
23020 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20  **ppDb,         
23030 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64  /* OUT: SQLite d
23040 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e  b handle */.  in
23050 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
23060 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a        /* Flags *
23070 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
23080 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e  zVfs        /* N
23090 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c  ame of VFS modul
230a0 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a  e to use */.);..
230b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
230c0 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 20  Error Codes And 
230d0 4d 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 30  Messages {H12800
230e0 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60200>.**.**
230f0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72   The sqlite3_err
23100 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 65  code() interface
23110 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
23120 65 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f 64  eric [result cod
23130 65 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64  e] or.** [extend
23140 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20  ed result code] 
23150 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63  for the most rec
23160 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 74  ent failed sqlit
23170 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a 2a  e3_* API call.**
23180 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
23190 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   a [database con
231a0 6e 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 70  nection]. If a p
231b0 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 61  rior API call fa
231c0 69 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20  iled.** but the 
231d0 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 20  most recent API 
231e0 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c 20  call succeeded, 
231f0 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
23200 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33   from.** sqlite3
23210 5f 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 6e  _errcode() is un
23220 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 71  defined.  The sq
23230 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65  lite3_extended_e
23240 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65  rrcode().** inte
23250 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d  rface is the sam
23260 65 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  e except that it
23270 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
23280 74 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65  the .** [extende
23290 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 65  d result code] e
232a0 76 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 65  ven when extende
232b0 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61  d result codes a
232c0 72 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a  re.** disabled..
232d0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
232e0 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73  3_errmsg() and s
232f0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
23300 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68  ) return English
23310 2d 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78  -language.** tex
23320 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  t that describes
23330 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65   the error, as e
23340 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55  ither UTF-8 or U
23350 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 65  TF-16 respective
23360 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f  ly..** Memory to
23370 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20   hold the error 
23380 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69  message string i
23390 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e  s managed intern
233a0 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70  ally..** The app
233b0 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  lication does no
233c0 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20  t need to worry 
233d0 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68  about freeing th
233e0 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77  e result..** How
233f0 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20  ever, the error 
23400 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20  string might be 
23410 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64  overwritten or d
23420 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a  eallocated by.**
23430 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
23440 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74  s to other SQLit
23450 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63  e interface func
23460 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  tions..**.** Whe
23470 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64  n the serialized
23480 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
23490 5d 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 20  ] is in use, it 
234a0 6d 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a 20  might be the.** 
234b0 63 61 73 65 20 74 68 61 74 20 61 20 73 65 63 6f  case that a seco
234c0 6e 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  nd error occurs 
234d0 6f 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 68  on a separate th
234e0 72 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e 0a  read in between.
234f0 2a 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 74  ** the time of t
23500 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 61  he first error a
23510 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74  nd the call to t
23520 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 2e  hese interfaces.
23530 0a 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 61  .** When that ha
23540 70 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f 6e  ppens, the secon
23550 64 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20  d error will be 
23560 72 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 74  reported since t
23570 68 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63  hese.** interfac
23580 65 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 74  es always report
23590 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
235a0 20 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 6f   result.  To avo
235b0 69 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 68  id.** this, each
235c0 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 61   thread can obta
235d0 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 65  in exclusive use
235e0 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73   of the [databas
235f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a  e connection] D.
23600 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 5b  ** by invoking [
23610 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
23620 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62  ter]([sqlite3_db
23630 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 6f  _mutex](D)) befo
23640 72 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20  re beginning.** 
23650 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e 76  to use D and inv
23660 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d  oking [sqlite3_m
23670 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 6c  utex_leave]([sql
23680 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44  ite3_db_mutex](D
23690 29 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c 20  )) after.** all 
236a0 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e 74  calls to the int
236b0 65 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 68  erfaces listed h
236c0 65 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 65  ere are complete
236d0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69  d..**.** If an i
236e0 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 77  nterface fails w
236f0 69 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  ith SQLITE_MISUS
23700 45 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  E, that means th
23710 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 77  e interface.** w
23720 61 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f 72  as invoked incor
23730 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 70  rectly by the ap
23740 70 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 74  plication.  In t
23750 68 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a 2a  hat case, the.**
23760 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20   error code and 
23770 6d 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 6d  message may or m
23780 61 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a  ay not be set..*
23790 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
237a0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 31 7d 20  .**.** {H12801} 
237b0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  The [sqlite3_err
237c0 63 6f 64 65 28 44 29 5d 20 69 6e 74 65 72 66 61  code(D)] interfa
237d0 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ce returns the n
237e0 75 6d 65 72 69 63 0a 2a 2a 20 20 20 20 20 20 20  umeric.**       
237f0 20 20 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 5d     [result code]
23800 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 72 65   or [extended re
23810 73 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74  sult code] for t
23820 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
23830 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61 69  .**          fai
23840 6c 65 64 20 69 6e 74 65 72 66 61 63 65 20 63 61  led interface ca
23850 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ll associated wi
23860 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  th the [database
23870 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a   connection] D..
23880 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 32 7d 20 54  **.** {H12802} T
23890 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65  he [sqlite3_exte
238a0 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 44 29 5d  nded_errcode(D)]
238b0 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
238c0 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63 0a 2a  ns the numeric.*
238d0 2a 20 20 20 20 20 20 20 20 20 20 5b 65 78 74 65  *          [exte
238e0 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65  nded result code
238f0 5d 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72  ] for the most r
23900 65 63 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20  ecently.**      
23910 20 20 20 20 66 61 69 6c 65 64 20 69 6e 74 65 72      failed inter
23920 66 61 63 65 20 63 61 6c 6c 20 61 73 73 6f 63 69  face call associ
23930 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64  ated with the [d
23940 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
23950 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  on] D..**.** {H1
23960 32 38 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74  2803} The [sqlit
23970 65 33 5f 65 72 72 6d 73 67 28 44 29 5d 20 61 6e  e3_errmsg(D)] an
23980 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  d [sqlite3_errms
23990 67 31 36 28 44 29 5d 0a 2a 2a 20 20 20 20 20 20  g16(D)].**      
239a0 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72      interfaces r
239b0 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c 61  eturn English-la
239c0 6e 67 75 61 67 65 20 74 65 78 74 20 74 68 61 74  nguage text that
239d0 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 20 20   describes.**   
239e0 20 20 20 20 20 20 20 74 68 65 20 65 72 72 6f 72         the error
239f0 20 69 6e 20 74 68 65 20 6d 6f 73 74 6c 79 20 72   in the mostly r
23a00 65 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 69  ecently failed i
23a10 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 2c 0a 2a  nterface call,.*
23a20 2a 20 20 20 20 20 20 20 20 20 20 65 6e 63 6f 64  *          encod
23a30 65 64 20 61 73 20 65 69 74 68 65 72 20 55 54 46  ed as either UTF
23a40 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73  -8 or UTF-16 res
23a50 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a  pectively..**.**
23a60 20 7b 48 31 32 38 30 37 7d 20 54 68 65 20 73 74   {H12807} The st
23a70 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62  rings returned b
23a80 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  y [sqlite3_errms
23a90 67 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  g()] and [sqlite
23aa0 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a  3_errmsg16()].**
23ab0 20 20 20 20 20 20 20 20 20 20 61 72 65 20 76 61            are va
23ac0 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65  lid until the ne
23ad0 78 74 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  xt SQLite interf
23ae0 61 63 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  ace call..**.** 
23af0 7b 48 31 32 38 30 38 7d 20 43 61 6c 6c 73 20 74  {H12808} Calls t
23b00 6f 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 74  o API routines t
23b10 68 61 74 20 64 6f 20 6e 6f 74 20 72 65 74 75 72  hat do not retur
23b20 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a  n an error code.
23b30 2a 2a 20 20 20 20 20 20 20 20 20 20 28 65 78 61  **          (exa
23b40 6d 70 6c 65 3a 20 5b 73 71 6c 69 74 65 33 5f 64  mple: [sqlite3_d
23b50 61 74 61 5f 63 6f 75 6e 74 28 29 5d 29 20 64 6f  ata_count()]) do
23b60 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   not.**         
23b70 20 63 68 61 6e 67 65 20 74 68 65 20 65 72 72 6f   change the erro
23b80 72 20 63 6f 64 65 20 6f 72 20 6d 65 73 73 61 67  r code or messag
23b90 65 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  e returned by.**
23ba0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
23bb0 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b  e3_errcode()], [
23bc0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
23bd0 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20  _errcode()],.** 
23be0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
23bf0 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 6f 72 20  3_errmsg()], or 
23c00 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31  [sqlite3_errmsg1
23c10 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  6()]..**.** {H12
23c20 38 30 39 7d 20 49 6e 74 65 72 66 61 63 65 73 20  809} Interfaces 
23c30 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 73 73  that are not ass
23c40 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73  ociated with a s
23c50 70 65 63 69 66 69 63 0a 2a 2a 20 20 20 20 20 20  pecific.**      
23c60 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f      [database co
23c70 6e 6e 65 63 74 69 6f 6e 5d 20 28 65 78 61 6d 70  nnection] (examp
23c80 6c 65 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20  les:.**         
23c90 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74   [sqlite3_mprint
23ca0 66 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  f()] or [sqlite3
23cb0 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  _enable_shared_c
23cc0 61 63 68 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20  ache()].**      
23cd0 20 20 20 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67      do not chang
23ce0 65 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 74  e the values ret
23cf0 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20 20 20  urned by.**     
23d00 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72       [sqlite3_er
23d10 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69 74  rcode()], [sqlit
23d20 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63  e3_extended_errc
23d30 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  ode()],.**      
23d40 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72      [sqlite3_err
23d50 6d 73 67 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69  msg()], or [sqli
23d60 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 2e  te3_errmsg16()].
23d70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
23d80 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  nt sqlite3_errco
23d90 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  de(sqlite3 *db);
23da0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
23db0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
23dc0 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33  _errcode(sqlite3
23dd0 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50   *db);.SQLITE_AP
23de0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
23df0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c  lite3_errmsg(sql
23e00 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  ite3*);.SQLITE_A
23e10 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
23e20 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
23e30 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
23e40 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20  * CAPI3REF: SQL 
23e50 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74  Statement Object
23e60 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30 31   {H13000} <H1301
23e70 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
23e80 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  {prepared statem
23e90 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20 73  ent} {prepared s
23ea0 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a 2a  tatements}.**.**
23eb0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
23ec0 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72  this object repr
23ed0 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20  esents a single 
23ee0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  SQL statement..*
23ef0 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73  * This object is
23f00 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e   variously known
23f10 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64 20   as a "prepared 
23f20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61 0a  statement" or a.
23f30 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51 4c  ** "compiled SQL
23f40 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 73   statement" or s
23f50 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61 74  imply as a "stat
23f60 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68  ement"..**.** Th
23f70 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61 74  e life of a stat
23f80 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f 65  ement object goe
23f90 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65  s something like
23fa0 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c   this:.**.** <ol
23fb0 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74 65  >.** <li> Create
23fc0 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e   the object usin
23fd0 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  g [sqlite3_prepa
23fe0 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72 65  re_v2()] or a re
23ff0 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66 75  lated.**      fu
24000 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e 20  nction..** <li> 
24010 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 5b  Bind values to [
24020 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 5d  host parameters]
24030 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
24040 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20 20  e3_bind_*().**  
24050 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e 0a      interfaces..
24060 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65 20  ** <li> Run the 
24070 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b  SQL by calling [
24080 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
24090 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65  one or more time
240a0 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65 74  s..** <li> Reset
240b0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 75   the statement u
240c0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65  sing [sqlite3_re
240d0 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20 62  set()] then go b
240e0 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 73  ack.**      to s
240f0 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73 20  tep 2.  Do this 
24100 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69 6d  zero or more tim
24110 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73 74  es..** <li> Dest
24120 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20 75  roy the object u
24130 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69  sing [sqlite3_fi
24140 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c 2f  nalize()]..** </
24150 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 20  ol>.**.** Refer 
24160 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  to documentation
24170 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 6d   on individual m
24180 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f 72  ethods above for
24190 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69   additional.** i
241a0 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  nformation..*/.t
241b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
241c0 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69 74  lite3_stmt sqlit
241d0 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a 20  e3_stmt;../*.** 
241e0 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74 69  CAPI3REF: Run-ti
241f0 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37 36  me Limits {H1276
24200 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a 2a  0} <S20600>.**.*
24210 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
24220 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a 65   allows the size
24230 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e 73   of various cons
24240 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69 6d  tructs to be lim
24250 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e  ited.** on a con
24260 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e 65  nection by conne
24270 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54 68  ction basis.  Th
24280 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
24290 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61 74  r is the.** [dat
242a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
242b0 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69 73  ] whose limit is
242c0 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71 75   to be set or qu
242d0 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73  eried.  The.** s
242e0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
242f0 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b 6c  is one of the [l
24300 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 5d  imit categories]
24310 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a 2a   that define a.*
24320 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73 74  * class of const
24330 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a 65  ructs to be size
24340 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20 74   limited.  The t
24350 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 69  hird parameter i
24360 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d  s the.** new lim
24370 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 73  it for that cons
24380 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e 63  truct.  The func
24390 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
243a0 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a   old limit..**.*
243b0 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69 6d  * If the new lim
243c0 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76 65  it is a negative
243d0 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 6d   number, the lim
243e0 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  it is unchanged.
243f0 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d 69  .** For the limi
24400 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53 51  t category of SQ
24410 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20 74  LITE_LIMIT_XYZ t
24420 68 65 72 65 20 69 73 20 61 20 68 61 72 64 20 75  here is a hard u
24430 70 70 65 72 0a 2a 2a 20 62 6f 75 6e 64 20 73 65  pper.** bound se
24440 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d 74  t by a compile-t
24450 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 73  ime C preprocess
24460 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 53  or macro named S
24470 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 2e 0a 2a  QLITE_MAX_XYZ..*
24480 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22  * (The "_LIMIT_"
24490 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20   in the name is 
244a0 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58  changed to "_MAX
244b0 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73  _".).** Attempts
244c0 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c   to increase a l
244d0 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 68  imit above its h
244e0 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20  ard upper bound 
244f0 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20  are.** silently 
24500 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65  truncated to the
24510 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69   hard upper limi
24520 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d  t..**.** Run tim
24530 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74  e limits are int
24540 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e  ended for use in
24550 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   applications th
24560 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74  at manage.** bot
24570 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65  h their own inte
24580 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e  rnal database an
24590 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73  d also databases
245a0 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f   that are contro
245b0 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75  lled.** by untru
245c0 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f  sted external so
245d0 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70  urces.  An examp
245e0 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  le application m
245f0 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62  ight be a.** web
24600 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 73  browser that has
24610 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 73   its own databas
24620 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 68  es for storing h
24630 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 65  istory and.** se
24640 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65 73  parate databases
24650 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 4a   controlled by J
24660 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 63  avaScript applic
24670 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 65  ations downloade
24680 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e 74  d.** off the Int
24690 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 65  ernet.  The inte
246a0 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 63  rnal databases c
246b0 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 0a  an be given the.
246c0 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 6c  ** large, defaul
246d0 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 62  t limits.  Datab
246e0 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 20  ases managed by 
246f0 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73  external sources
24700 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 6e   can.** be given
24710 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c 69   much smaller li
24720 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 6f  mits designed to
24730 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 61   prevent a denia
24740 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a 20  l of service.** 
24750 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f 70  attack.  Develop
24760 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 77  ers might also w
24770 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 5b  ant to use the [
24780 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
24790 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e 74  orizer()].** int
247a0 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 65  erface to furthe
247b0 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 73  r control untrus
247c0 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 69  ted SQL.  The si
247d0 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
247e0 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79  se.** created by
247f0 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 63   an untrusted sc
24800 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e 74  ript can be cont
24810 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 0a  ained using the.
24820 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75  ** [max_page_cou
24830 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a 2a  nt] [PRAGMA]..**
24840 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d 65  .** New run-time
24850 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65   limit categorie
24860 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  s may be added i
24870 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
24880 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  s..**.** INVARIA
24890 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37  NTS:.**.** {H127
248a0 36 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  62} A successful
248b0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
248c0 33 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20  3_limit(D,C,V)] 
248d0 77 68 65 72 65 20 56 20 69 73 0a 2a 2a 20 20 20  where V is.**   
248e0 20 20 20 20 20 20 20 70 6f 73 69 74 69 76 65 20         positive 
248f0 63 68 61 6e 67 65 73 20 74 68 65 20 6c 69 6d 69  changes the limi
24900 74 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  t on the size of
24910 20 63 6f 6e 73 74 72 75 63 74 20 43 20 69 6e 20   construct C in 
24920 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
24930 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
24940 74 69 6f 6e 5d 20 44 20 74 6f 20 74 68 65 20 6c  tion] D to the l
24950 65 73 73 65 72 20 6f 66 20 56 20 61 6e 64 20 74  esser of V and t
24960 68 65 20 68 61 72 64 20 75 70 70 65 72 0a 2a 2a  he hard upper.**
24970 20 20 20 20 20 20 20 20 20 20 62 6f 75 6e 64 20            bound 
24980 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 43  on the size of C
24990 20 74 68 61 74 20 69 73 20 73 65 74 20 61 74 20   that is set at 
249a0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2a  compile-time..**
249b0 0a 2a 2a 20 7b 48 31 32 37 36 36 7d 20 41 20 73  .** {H12766} A s
249c0 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
249d0 6f 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  o [sqlite3_limit
249e0 28 44 2c 43 2c 56 29 5d 20 77 68 65 72 65 20 56  (D,C,V)] where V
249f0 20 69 73 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20   is negative.** 
24a00 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73 20           leaves 
24a10 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65  the state of the
24a20 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
24a30 63 74 69 6f 6e 5d 20 44 20 75 6e 63 68 61 6e 67  ction] D unchang
24a40 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 36  ed..**.** {H1276
24a50 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  9} A successful 
24a60 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
24a70 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20 72  _limit(D,C,V)] r
24a80 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 20 20  eturns the.**   
24a90 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20         value of 
24aa0 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65  the limit on the
24ab0 20 73 69 7a 65 20 6f 66 20 63 6f 6e 73 74 72 75   size of constru
24ac0 63 74 20 43 20 69 6e 20 74 68 65 0a 2a 2a 20 20  ct C in the.**  
24ad0 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73          [databas
24ae0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20  e connection] D 
24af0 61 73 20 69 74 20 77 61 73 20 70 72 69 6f 72 20  as it was prior 
24b00 74 6f 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2f 0a  to the call..*/.
24b10 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
24b20 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71 6c  qlite3_limit(sql
24b30 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20 69  ite3*, int id, i
24b40 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a 0a  nt newVal);../*.
24b50 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e  ** CAPI3REF: Run
24b60 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74 65  -Time Limit Cate
24b70 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d 20  gories {H12790} 
24b80 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59 57  <H12760>.** KEYW
24b90 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61 74  ORDS: {limit cat
24ba0 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63 61  egory} {limit ca
24bb0 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a 20  tegories}.**.** 
24bc0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
24bd0 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 61  define various a
24be0 73 70 65 63 74 73 20 6f 66 20 61 20 5b 64 61 74  spects of a [dat
24bf0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
24c00 5d 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65  ].** that can be
24c10 20 6c 69 6d 69 74 65 64 20 69 6e 20 73 69 7a 65   limited in size
24c20 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71   by calls to [sq
24c30 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e 0a  lite3_limit()]..
24c40 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20  ** The meanings 
24c50 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c  of the various l
24c60 69 6d 69 74 73 20 61 72 65 20 61 73 20 66 6f 6c  imits are as fol
24c70 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  lows:.**.** <dl>
24c80 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
24c90 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e  IMIT_LENGTH</dt>
24ca0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
24cb0 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20  mum size of any 
24cc0 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f  string or BLOB o
24cd0 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e  r table row.<dd>
24ce0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
24cf0 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47  E_LIMIT_SQL_LENG
24d00 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  TH</dt>.** <dd>T
24d10 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
24d20 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74  h of an SQL stat
24d30 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ement.</dd>.**.*
24d40 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d  * <dt>SQLITE_LIM
24d50 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a  IT_COLUMN</dt>.*
24d60 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
24d70 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  m number of colu
24d80 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64  mns in a table d
24d90 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20  efinition or in 
24da0 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65  the.** result se
24db0 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 6f 72  t of a SELECT or
24dc0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
24dd0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
24de0 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72  n an index.** or
24df0 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20   in an ORDER BY 
24e00 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  or GROUP BY clau
24e10 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  se.</dd>.**.** <
24e20 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
24e30 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a  EXPR_DEPTH</dt>.
24e40 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
24e50 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  um depth of the 
24e60 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e  parse tree on an
24e70 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64  y expression.</d
24e80 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
24e90 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55  ITE_LIMIT_COMPOU
24ea0 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a  ND_SELECT</dt>.*
24eb0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
24ec0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  m number of term
24ed0 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20  s in a compound 
24ee0 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
24ef0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
24f00 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44  >SQLITE_LIMIT_VD
24f10 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  BE_OP</dt>.** <d
24f20 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
24f30 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74  mber of instruct
24f40 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61  ions in a virtua
24f50 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61  l machine progra
24f60 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70  m.** used to imp
24f70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74  lement an SQL st
24f80 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a  atement.</dd>.**
24f90 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
24fa0 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  IMIT_FUNCTION_AR
24fb0 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  G</dt>.** <dd>Th
24fc0 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
24fd0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e   of arguments on
24fe0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64   a function.</dd
24ff0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
25000 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45  TE_LIMIT_ATTACHE
25010 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
25020 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
25030 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 74   of attached dat
25040 61 62 61 73 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a  abases.</dd>.**.
25050 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
25060 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MIT_LIKE_PATTERN
25070 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20  _LENGTH</dt>.** 
25080 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
25090 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 61  length of the pa
250a0 74 74 65 72 6e 20 61 72 67 75 6d 65 6e 74 20 74  ttern argument t
250b0 6f 20 74 68 65 20 4c 49 4b 45 20 6f 72 0a 2a 2a  o the LIKE or.**
250c0 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e   GLOB operators.
250d0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
250e0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
250f0 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74  IABLE_NUMBER</dt
25100 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
25110 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76  imum number of v
25120 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53  ariables in an S
25130 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  QL statement tha
25140 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e  t can.** be boun
25150 64 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e  d.</dd>.** </dl>
25160 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
25170 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 20  TE_LIMIT_LENGTH 
25180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25190 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
251a0 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45  ITE_LIMIT_SQL_LE
251b0 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20 20  NGTH            
251c0 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
251d0 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d  LITE_LIMIT_COLUM
251e0 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
251f0 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53       2.#define S
25200 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52  QLITE_LIMIT_EXPR
25210 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20 20  _DEPTH          
25220 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20        3.#define 
25230 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d  SQLITE_LIMIT_COM
25240 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 20 20  POUND_SELECT    
25250 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65         4.#define
25260 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44   SQLITE_LIMIT_VD
25270 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 20 20  BE_OP           
25280 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e          5.#defin
25290 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46  e SQLITE_LIMIT_F
252a0 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 20 20  UNCTION_ARG     
252b0 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69           6.#defi
252c0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
252d0 41 54 54 41 43 48 45 44 20 20 20 20 20 20 20 20  ATTACHED        
252e0 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66            7.#def
252f0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
25300 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
25310 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 64 65  NGTH       8.#de
25320 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
25330 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
25340 52 20 20 20 20 20 20 20 20 20 20 20 39 0a 0a 2f  R           9../
25350 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
25360 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20  ompiling An SQL 
25370 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 30 31  Statement {H1301
25380 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 4b  0} <S10000>.** K
25390 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74  EYWORDS: {SQL st
253a0 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72  atement compiler
253b0 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75  }.**.** To execu
253c0 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2c  te an SQL query,
253d0 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 20 62   it must first b
253e0 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20  e compiled into 
253f0 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70  a byte-code.** p
25400 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65  rogram using one
25410 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
25420 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  es..**.** The fi
25430 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 22 64  rst argument, "d
25440 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61 62 61  b", is a [databa
25450 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f  se connection] o
25460 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a 2a  btained from a.*
25470 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  * prior call to 
25480 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
25490 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  , [sqlite3_open_
254a0 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  v2()] or [sqlite
254b0 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a  3_open16()]..**.
254c0 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ** The second ar
254d0 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20  gument, "zSql", 
254e0 69 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  is the statement
254f0 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c   to be compiled,
25500 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65   encoded.** as e
25510 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55  ither UTF-8 or U
25520 54 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c 69  TF-16.  The sqli
25530 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 6e  te3_prepare() an
25540 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  d sqlite3_prepar
25550 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66  e_v2().** interf
25560 61 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c 20  aces use UTF-8, 
25570 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70  and sqlite3_prep
25580 61 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c 69  are16() and sqli
25590 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
255a0 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 36  ().** use UTF-16
255b0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ..**.** If the n
255c0 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73  Byte argument is
255d0 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
255e0 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65   then zSql is re
255f0 61 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20  ad up to the.** 
25600 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69  first zero termi
25610 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 20  nator. If nByte 
25620 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c  is non-negative,
25630 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20   then it is the 
25640 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65  maximum.** numbe
25650 72 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 64  r of  bytes read
25660 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65   from zSql.  Whe
25670 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e  n nByte is non-n
25680 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20  egative, the.** 
25690 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 73  zSql string ends
256a0 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 66   at either the f
256b0 69 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 27  irst '\000' or '
256c0 5c 75 30 30 30 30 27 20 63 68 61 72 61 63 74 65  \u0000' characte
256d0 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74  r or.** the nByt
256e0 65 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 68  e-th byte, which
256f0 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74  ever comes first
25700 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  . If the caller 
25710 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68  knows.** that th
25720 65 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e  e supplied strin
25730 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  g is nul-termina
25740 74 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20  ted, then there 
25750 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65  is a small.** pe
25760 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e 74  rformance advant
25770 61 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 64  age to be gained
25780 20 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 6e   by passing an n
25790 42 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 74  Byte parameter t
257a0 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20  hat.** is equal 
257b0 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
257c0 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 6e   bytes in the in
257d0 70 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e  put string <i>in
257e0 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74  cluding</i>.** t
257f0 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  he nul-terminato
25800 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 2a  r bytes..**.** *
25810 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74  pzTail is made t
25820 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
25830 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74  irst byte past t
25840 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a  he end of the.**
25850 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 65   first SQL state
25860 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54  ment in zSql.  T
25870 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e  hese routines on
25880 6c 79 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 66  ly compile the f
25890 69 72 73 74 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  irst.** statemen
258a0 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70  t in zSql, so *p
258b0 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f  zTail is left po
258c0 69 6e 74 69 6e 67 20 74 6f 20 77 68 61 74 20 72  inting to what r
258d0 65 6d 61 69 6e 73 0a 2a 2a 20 75 6e 63 6f 6d 70  emains.** uncomp
258e0 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53  iled..**.** *ppS
258f0 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  tmt is left poin
25900 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c  ting to a compil
25910 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ed [prepared sta
25920 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e  tement] that can
25930 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20   be.** executed 
25940 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73  using [sqlite3_s
25950 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72  tep()].  If ther
25960 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a  e is an error, *
25970 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a  ppStmt is set.**
25980 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68   to NULL.  If th
25990 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e  e input text con
259a0 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66  tains no SQL (if
259b0 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e   the input is an
259c0 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67   empty.** string
259d0 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74   or a comment) t
259e0 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73  hen *ppStmt is s
259f0 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 7b  et to NULL..** {
25a00 41 31 33 30 31 38 7d 20 54 68 65 20 63 61 6c 6c  A13018} The call
25a10 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 73  ing procedure is
25a20 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
25a30 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f   deleting the co
25a40 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73 74  mpiled.** SQL st
25a50 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b 73  atement using [s
25a60 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
25a70 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 73 20  )] after it has 
25a80 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74  finished with it
25a90 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65  ..**.** On succe
25aa0 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  ss, [SQLITE_OK] 
25ab0 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 74 68  is returned, oth
25ac0 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72  erwise an [error
25ad0 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
25ae0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ed..**.** The sq
25af0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
25b00 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70  () and sqlite3_p
25b10 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 69 6e  repare16_v2() in
25b20 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a 2a 20  terfaces are.** 
25b30 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20  recommended for 
25b40 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 6d 73  all new programs
25b50 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 72 20  . The two older 
25b60 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 72  interfaces are r
25b70 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62  etained.** for b
25b80 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
25b90 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 65 69  bility, but thei
25ba0 72 20 75 73 65 20 69 73 20 64 69 73 63 6f 75 72  r use is discour
25bb0 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20  aged..** In the 
25bc0 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 73 2c  "v2" interfaces,
25bd0 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
25be0 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20  atement.** that 
25bf0 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 68 65  is returned (the
25c00 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20   [sqlite3_stmt] 
25c10 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e 73  object) contains
25c20 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a   a copy of the.*
25c30 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74  * original SQL t
25c40 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 65 73  ext. This causes
25c50 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74   the [sqlite3_st
25c60 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  ep()] interface 
25c70 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20 64  to.** behave a d
25c80 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74 77  ifferently in tw
25c90 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f  o ways:.**.** <o
25ca0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66  l>.** <li>.** If
25cb0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
25cc0 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69 6e  hema changes, in
25cd0 73 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e 69  stead of returni
25ce0 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d  ng [SQLITE_SCHEM
25cf0 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61  A] as it.** alwa
25d00 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b  ys used to do, [
25d10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
25d20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c  will automatical
25d30 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68 65  ly recompile the
25d40 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e   SQL.** statemen
25d50 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75 6e  t and try to run
25d60 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20 74   it again.  If t
25d70 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68  he schema has ch
25d80 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61  anged in.** a wa
25d90 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65  y that makes the
25da0 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f   statement no lo
25db0 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c  nger valid, [sql
25dc0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c  ite3_step()] wil
25dd0 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72  l still.** retur
25de0 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  n [SQLITE_SCHEMA
25df0 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20 74  ].  But unlike t
25e00 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69  he legacy behavi
25e10 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45  or, [SQLITE_SCHE
25e20 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20  MA] is.** now a 
25e30 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43 61  fatal error.  Ca
25e40 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70  lling [sqlite3_p
25e50 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 67 61  repare_v2()] aga
25e60 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65  in will not make
25e70 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f   the.** error go
25e80 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73   away.  Note: us
25e90 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  e [sqlite3_errms
25ea0 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 68 65  g()] to find the
25eb0 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68 65 20   text.** of the 
25ec0 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 74 68  parsing error th
25ed0 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e  at results in an
25ee0 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d   [SQLITE_SCHEMA]
25ef0 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69   return..** </li
25f00 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20  >.**.** <li>.** 
25f10 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63  When an error oc
25f20 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 5f 73  curs, [sqlite3_s
25f30 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 74 75  tep()] will retu
25f40 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65  rn one of the de
25f50 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f 72  tailed.** [error
25f60 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78 74 65   codes] or [exte
25f70 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73  nded error codes
25f80 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79 20 62  ].  The legacy b
25f90 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 61 74  ehavior was that
25fa0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  .** [sqlite3_ste
25fb0 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20  p()] would only 
25fc0 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72 69 63  return a generic
25fd0 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
25fe0 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61  result code.** a
25ff0 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68 61 76  nd you would hav
26000 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f  e to make a seco
26010 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  nd call to [sqli
26020 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e 20  te3_reset()] in 
26030 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69 6e 64  order.** to find
26040 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
26050 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72 6f  cause of the pro
26060 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65 20 22  blem. With the "
26070 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a 20 69  v2" prepare.** i
26080 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20 75  nterfaces, the u
26090 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e  nderlying reason
260a0 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 69   for the error i
260b0 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64  s returned immed
260c0 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e  iately..** </li>
260d0 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ol>.**.** 
260e0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
260f0 2a 20 7b 48 31 33 30 31 31 7d 20 54 68 65 20 5b  * {H13011} The [
26100 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
26110 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 61 6e  db,zSql,...)] an
26120 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  d.**          [s
26130 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
26140 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20  2(db,zSql,...)] 
26150 69 6e 74 65 72 66 61 63 65 73 20 69 6e 74 65 72  interfaces inter
26160 70 72 65 74 20 74 68 65 0a 2a 2a 20 20 20 20 20  pret the.**     
26170 20 20 20 20 20 74 65 78 74 20 69 6e 20 74 68 65       text in the
26180 69 72 20 7a 53 71 6c 20 70 61 72 61 6d 65 74 65  ir zSql paramete
26190 72 20 61 73 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a  r as UTF-8..**.*
261a0 2a 20 7b 48 31 33 30 31 32 7d 20 54 68 65 20 5b  * {H13012} The [
261b0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
261c0 36 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20  6(db,zSql,...)] 
261d0 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
261e0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
261f0 31 36 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e  16_v2(db,zSql,..
26200 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 69  .)] interfaces i
26210 6e 74 65 72 70 72 65 74 20 74 68 65 0a 2a 2a 20  nterpret the.** 
26220 20 20 20 20 20 20 20 20 20 74 65 78 74 20 69 6e           text in
26230 20 74 68 65 69 72 20 7a 53 71 6c 20 70 61 72 61   their zSql para
26240 6d 65 74 65 72 20 61 73 20 55 54 46 2d 31 36 20  meter as UTF-16 
26250 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79  in the native by
26260 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20  te order..**.** 
26270 7b 48 31 33 30 31 33 7d 20 49 66 20 74 68 65 20  {H13013} If the 
26280 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 74  nByte argument t
26290 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  o [sqlite3_prepa
262a0 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 6e 42  re_v2(db,zSql,nB
262b0 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20  yte,...)].**    
262c0 20 20 20 20 20 20 61 6e 64 20 69 74 73 20 76 61        and its va
262d0 72 69 61 6e 74 73 20 69 73 20 6c 65 73 73 20 74  riants is less t
262e0 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 20 53 51  han zero, the SQ
262f0 4c 20 74 65 78 74 20 69 73 0a 2a 2a 20 20 20 20  L text is.**    
26300 20 20 20 20 20 20 72 65 61 64 20 66 72 6f 6d 20        read from 
26310 7a 53 71 6c 20 69 73 20 72 65 61 64 20 75 70 20  zSql is read up 
26320 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72  to the first zer
26330 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a  o terminator..**
26340 0a 2a 2a 20 7b 48 31 33 30 31 34 7d 20 49 66 20  .** {H13014} If 
26350 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65  the nByte argume
26360 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70  nt to [sqlite3_p
26370 72 65 70 61 72 65 5f 76 32 28 64 62 2c 7a 53 71  repare_v2(db,zSq
26380 6c 2c 6e 42 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a  l,nByte,...)].**
26390 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 69 74            and it
263a0 73 20 76 61 72 69 61 6e 74 73 20 69 73 20 6e 6f  s variants is no
263b0 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e  n-negative, then
263c0 20 61 74 20 6d 6f 73 74 20 6e 42 79 74 65 73 20   at most nBytes 
263d0 62 79 74 65 73 20 6f 66 0a 2a 2a 20 20 20 20 20  bytes of.**     
263e0 20 20 20 20 20 53 51 4c 20 74 65 78 74 20 69 73       SQL text is
263f0 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e   read from zSql.
26400 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30 31 35 7d 20  .**.** {H13015} 
26410 49 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  In [sqlite3_prep
26420 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 4e  are_v2(db,zSql,N
26430 2c 50 2c 70 7a 54 61 69 6c 29 5d 20 61 6e 64 20  ,P,pzTail)] and 
26440 69 74 73 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20  its variants.** 
26450 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 20           if the 
26460 7a 53 71 6c 20 69 6e 70 75 74 20 74 65 78 74 20  zSql input text 
26470 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68  contains more th
26480 61 6e 20 6f 6e 65 20 53 51 4c 20 73 74 61 74 65  an one SQL state
26490 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  ment.**         
264a0 20 61 6e 64 20 70 7a 54 61 69 6c 20 69 73 20 6e   and pzTail is n
264b0 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a 70  ot NULL, then *p
264c0 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 6f  zTail is made to
264d0 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a   point to the.**
264e0 20 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20            first 
264f0 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  byte past the en
26500 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 53  d of the first S
26510 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  QL statement in 
26520 7a 53 71 6c 2e 0a 2a 2a 20 20 20 20 20 20 20 20  zSql..**        
26530 20 20 3c 74 6f 64 6f 3e 57 68 61 74 20 64 6f 65    <todo>What doe
26540 73 20 2a 70 7a 54 61 69 6c 20 70 6f 69 6e 74 20  s *pzTail point 
26550 74 6f 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  to if there is o
26560 6e 65 20 73 74 61 74 65 6d 65 6e 74 3f 3c 2f 74  ne statement?</t
26570 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30  odo>.**.** {H130
26580 31 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  16} A successful
26590 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
265a0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c  3_prepare_v2(db,
265b0 7a 53 71 6c 2c 4e 2c 70 70 53 74 6d 74 2c 2e 2e  zSql,N,ppStmt,..
265c0 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .)].**          
265d0 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 61  or one of its va
265e0 72 69 61 6e 74 73 20 77 72 69 74 65 73 20 69 6e  riants writes in
265f0 74 6f 20 2a 70 70 53 74 6d 74 20 61 20 70 6f 69  to *ppStmt a poi
26600 6e 74 65 72 20 74 6f 20 61 20 6e 65 77 0a 2a 2a  nter to a new.**
26610 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
26620 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f  red statement] o
26630 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 4e  r a pointer to N
26640 55 4c 4c 20 69 66 20 7a 53 71 6c 20 63 6f 6e 74  ULL if zSql cont
26650 61 69 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ains.**         
26660 20 6e 6f 74 68 69 6e 67 20 6f 74 68 65 72 20 74   nothing other t
26670 68 61 6e 20 77 68 69 74 65 73 70 61 63 65 20 6f  han whitespace o
26680 72 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  r comments..**.*
26690 2a 20 7b 48 31 33 30 31 39 7d 20 54 68 65 20 5b  * {H13019} The [
266a0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
266b0 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  v2()] interface 
266c0 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73  and its variants
266d0 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20   return.**      
266e0 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20      [SQLITE_OK] 
266f0 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  or an appropriat
26700 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 75  e [error code] u
26710 70 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a  pon failure..**.
26720 2a 2a 20 7b 48 31 33 30 32 31 7d 20 42 65 66 6f  ** {H13021} Befo
26730 72 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  re [sqlite3_prep
26740 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74  are(db,zSql,nByt
26750 65 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29  e,ppStmt,pzTail)
26760 5d 20 6f 72 20 69 74 73 0a 2a 2a 20 20 20 20 20  ] or its.**     
26770 20 20 20 20 20 76 61 72 69 61 6e 74 73 20 72 65       variants re
26780 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 28  turns an error (
26790 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20  any value other 
267a0 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  than [SQLITE_OK]
267b0 29 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  ),.**          t
267c0 68 65 79 20 66 69 72 73 74 20 73 65 74 20 2a 70  hey first set *p
267d0 70 53 74 6d 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  pStmt to NULL..*
267e0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
267f0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
26800 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
26810 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
26820 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
26830 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
26840 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53  zSql,       /* S
26850 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54  QL statement, UT
26860 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20  F-8 encoded */. 
26870 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20   int nByte,     
26880 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
26890 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53  mum length of zS
268a0 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
268b0 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
268c0 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54  *ppStmt,  /* OUT
268d0 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  : Statement hand
268e0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
268f0 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  ar **pzTail     
26900 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20  /* OUT: Pointer 
26910 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f  to unused portio
26920 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a  n of zSql */.);.
26930 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
26940 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
26950 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2(.  sqlite3 *db
26960 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
26970 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
26980 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
26990 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20  *zSql,       /* 
269a0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55  SQL statement, U
269b0 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  TF-8 encoded */.
269c0 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
269d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
269e0 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a  imum length of z
269f0 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
26a00 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
26a10 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55  **ppStmt,  /* OU
26a20 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e  T: Statement han
26a30 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
26a40 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
26a50 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72   /* OUT: Pointer
26a60 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69   to unused porti
26a70 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b  on of zSql */.);
26a80 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
26a90 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
26aa0 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  6(.  sqlite3 *db
26ab0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
26ac0 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
26ad0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
26ae0 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20  *zSql,       /* 
26af0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55  SQL statement, U
26b00 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f  TF-16 encoded */
26b10 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20  .  int nByte,   
26b20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
26b30 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
26b40 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
26b50 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
26b60 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f   **ppStmt,  /* O
26b70 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61  UT: Statement ha
26b80 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
26b90 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20  void **pzTail   
26ba0 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65    /* OUT: Pointe
26bb0 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74  r to unused port
26bc0 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29  ion of zSql */.)
26bd0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
26be0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
26bf0 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  16_v2(.  sqlite3
26c00 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
26c10 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
26c20 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  dle */.  const v
26c30 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  oid *zSql,      
26c40 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
26c50 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  t, UTF-16 encode
26c60 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  d */.  int nByte
26c70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
26c80 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
26c90 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
26ca0 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
26cb0 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
26cc0 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e  /* OUT: Statemen
26cd0 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  t handle */.  co
26ce0 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
26cf0 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f  l     /* OUT: Po
26d00 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20  inter to unused 
26d10 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20  portion of zSql 
26d20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.);../*.** CAP
26d30 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 69 6e  I3REF: Retrievin
26d40 67 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c 20  g Statement SQL 
26d50 7b 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30 30  {H13100} <H13000
26d60 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  >.**.** This int
26d70 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 73  erface can be us
26d80 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61  ed to retrieve a
26d90 20 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20 74   saved copy of t
26da0 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53  he original.** S
26db0 51 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f 20  QL text used to 
26dc0 63 72 65 61 74 65 20 61 20 5b 70 72 65 70 61 72  create a [prepar
26dd0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 66  ed statement] if
26de0 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20   that statement 
26df0 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20  was.** compiled 
26e00 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71  using either [sq
26e10 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
26e20 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
26e30 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e  prepare16_v2()].
26e40 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
26e50 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 30 31  S:.**.** {H13101
26e60 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70 61 72  } If the [prepar
26e70 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 70 61  ed statement] pa
26e80 73 73 65 64 20 61 73 20 74 68 65 20 61 72 67 75  ssed as the argu
26e90 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20  ment to.**      
26ea0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c      [sqlite3_sql
26eb0 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c 65 64  ()] was compiled
26ec0 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73   using either [s
26ed0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
26ee0 32 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20  2()] or.**      
26ef0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65      [sqlite3_pre
26f00 70 61 72 65 31 36 5f 76 32 28 29 5d 2c 20 74 68  pare16_v2()], th
26f10 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c 28  en [sqlite3_sql(
26f20 29 5d 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20  )] returns.**   
26f30 20 20 20 20 20 20 20 61 20 70 6f 69 6e 74 65 72         a pointer
26f40 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69   to a zero-termi
26f50 6e 61 74 65 64 20 73 74 72 69 6e 67 20 63 6f 6e  nated string con
26f60 74 61 69 6e 69 6e 67 20 61 20 55 54 46 2d 38 20  taining a UTF-8 
26f70 72 65 6e 64 65 72 69 6e 67 0a 2a 2a 20 20 20 20  rendering.**    
26f80 20 20 20 20 20 20 6f 66 20 74 68 65 20 6f 72 69        of the ori
26f90 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d  ginal SQL statem
26fa0 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31  ent..**.** {H131
26fb0 30 32 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70  02} If the [prep
26fc0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
26fd0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 61 72  passed as the ar
26fe0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20  gument to.**    
26ff0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73        [sqlite3_s
27000 71 6c 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c  ql()] was compil
27010 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 20  ed using either 
27020 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
27030 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  ()] or.**       
27040 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70     [sqlite3_prep
27050 61 72 65 31 36 28 29 5d 2c 20 74 68 65 6e 20 5b  are16()], then [
27060 73 71 6c 69 74 65 33 5f 73 71 6c 28 29 5d 20 72  sqlite3_sql()] r
27070 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f  eturns a NULL po
27080 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  inter..**.** {H1
27090 33 31 30 33 7d 20 54 68 65 20 73 74 72 69 6e 67  3103} The string
270a0 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
270b0 6c 69 74 65 33 5f 73 71 6c 28 53 29 5d 20 69 73  lite3_sql(S)] is
270c0 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65   valid until the
270d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
270e0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
270f0 5d 20 53 20 69 73 20 64 65 6c 65 74 65 64 20 75  ] S is deleted u
27100 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69  sing [sqlite3_fi
27110 6e 61 6c 69 7a 65 28 53 29 5d 2e 0a 2a 2f 0a 53  nalize(S)]..*/.S
27120 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
27130 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71  char *sqlite3_sq
27140 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  l(sqlite3_stmt *
27150 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  pStmt);../*.** C
27160 41 50 49 33 52 45 46 3a 20 44 79 6e 61 6d 69 63  API3REF: Dynamic
27170 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65  ally Typed Value
27180 20 4f 62 6a 65 63 74 20 7b 48 31 35 30 30 30 7d   Object {H15000}
27190 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59   <S20200>.** KEY
271a0 57 4f 52 44 53 3a 20 7b 70 72 6f 74 65 63 74 65  WORDS: {protecte
271b0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d  d sqlite3_value}
271c0 20 7b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71   {unprotected sq
271d0 6c 69 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a  lite3_value}.**.
271e0 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74  ** SQLite uses t
271f0 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  he sqlite3_value
27200 20 6f 62 6a 65 63 74 20 74 6f 20 72 65 70 72 65   object to repre
27210 73 65 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a  sent all values.
27220 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 73  ** that can be s
27230 74 6f 72 65 64 20 69 6e 20 61 20 64 61 74 61 62  tored in a datab
27240 61 73 65 20 74 61 62 6c 65 2e 20 53 51 4c 69 74  ase table. SQLit
27250 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 20 74  e uses dynamic t
27260 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65  yping.** for the
27270 20 76 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65   values it store
27280 73 2e 20 56 61 6c 75 65 73 20 73 74 6f 72 65 64  s. Values stored
27290 20 69 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   in sqlite3_valu
272a0 65 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e  e objects.** can
272b0 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66 6c   be integers, fl
272c0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
272d0 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c  ues, strings, BL
272e0 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a  OBs, or NULL..**
272f0 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f 76  .** An sqlite3_v
27300 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 20  alue object may 
27310 62 65 20 65 69 74 68 65 72 20 22 70 72 6f 74 65  be either "prote
27320 63 74 65 64 22 20 6f 72 20 22 75 6e 70 72 6f 74  cted" or "unprot
27330 65 63 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20  ected"..** Some 
27340 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69  interfaces requi
27350 72 65 20 61 20 70 72 6f 74 65 63 74 65 64 20 73  re a protected s
27360 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f  qlite3_value.  O
27370 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 0a  ther interfaces.
27380 2a 2a 20 77 69 6c 6c 20 61 63 63 65 70 74 20 65  ** will accept e
27390 69 74 68 65 72 20 61 20 70 72 6f 74 65 63 74 65  ither a protecte
273a0 64 20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63  d or an unprotec
273b0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
273c0 65 2e 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 65  e..** Every inte
273d0 72 66 61 63 65 20 74 68 61 74 20 61 63 63 65 70  rface that accep
273e0 74 73 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ts sqlite3_value
273f0 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69   arguments speci
27400 66 69 65 73 0a 2a 2a 20 77 68 65 74 68 65 72 20  fies.** whether 
27410 6f 72 20 6e 6f 74 20 69 74 20 72 65 71 75 69 72  or not it requir
27420 65 73 20 61 20 70 72 6f 74 65 63 74 65 64 20 73  es a protected s
27430 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a  qlite3_value..**
27440 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 22 70  .** The terms "p
27450 72 6f 74 65 63 74 65 64 22 20 61 6e 64 20 22 75  rotected" and "u
27460 6e 70 72 6f 74 65 63 74 65 64 22 20 72 65 66 65  nprotected" refe
27470 72 20 74 6f 20 77 68 65 74 68 65 72 20 6f 72 20  r to whether or 
27480 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69  not.** a mutex i
27490 73 20 68 65 6c 64 2e 20 20 41 20 69 6e 74 65 72  s held.  A inter
274a0 6e 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65 6c  nal mutex is hel
274b0 64 20 66 6f 72 20 61 20 70 72 6f 74 65 63 74 65  d for a protecte
274c0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c  d.** sqlite3_val
274d0 75 65 20 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f  ue object but no
274e0 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66   mutex is held f
274f0 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65  or an unprotecte
27500 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c  d.** sqlite3_val
27510 75 65 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 53  ue object.  If S
27520 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
27530 64 20 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74  d to be single-t
27540 68 72 65 61 64 65 64 0a 2a 2a 20 28 77 69 74 68  hreaded.** (with
27550 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53   [SQLITE_THREADS
27560 41 46 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 20  AFE=0] and with 
27570 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73  [sqlite3_threads
27580 61 66 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67  afe()] returning
27590 20 30 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c   0).** or if SQL
275a0 69 74 65 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e  ite is run in on
275b0 65 20 6f 66 20 72 65 64 75 63 65 64 20 6d 75 74  e of reduced mut
275c0 65 78 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51  ex modes .** [SQ
275d0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47  LITE_CONFIG_SING
275e0 4c 45 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 51  LETHREAD] or [SQ
275f0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54  LITE_CONFIG_MULT
27600 49 54 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e  ITHREAD].** then
27610 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 73   there is no dis
27620 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e  tinction between
27630 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75   protected and u
27640 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71  nprotected.** sq
27650 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
27660 63 74 73 20 61 6e 64 20 74 68 65 79 20 63 61 6e  cts and they can
27670 20 62 65 20 75 73 65 64 20 69 6e 74 65 72 63 68   be used interch
27680 61 6e 67 65 61 62 6c 79 2e 20 20 48 6f 77 65 76  angeably.  Howev
27690 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d  er,.** for maxim
276a0 75 6d 20 63 6f 64 65 20 70 6f 72 74 61 62 69 6c  um code portabil
276b0 69 74 79 20 69 74 20 69 73 20 72 65 63 6f 6d 6d  ity it is recomm
276c0 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c 69  ended that appli
276d0 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c  cations.** still
276e0 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e   make the distin
276f0 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 62 65  ction between be
27700 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20  tween protected 
27710 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a  and unprotected.
27720 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
27730 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68   objects even wh
27740 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20  en not strictly 
27750 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  required..**.** 
27760 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  The sqlite3_valu
27770 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61  e objects that a
27780 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61 72  re passed as par
27790 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68 65  ameters into the
277a0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
277b0 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 69  on of [applicati
277c0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66  on-defined SQL f
277d0 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 72  unctions] are pr
277e0 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20  otected..** The 
277f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
27800 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79  ject returned by
27810 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  .** [sqlite3_col
27820 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20  umn_value()] is 
27830 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20  unprotected..** 
27840 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  Unprotected sqli
27850 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
27860 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73  s may only be us
27870 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69  ed with.** [sqli
27880 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
27890 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
278a0 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a  _bind_value()]..
278b0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
278c0 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c  value_blob | sql
278d0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
278e0 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20  )] family of.** 
278f0 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69  interfaces requi
27900 72 65 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c  re protected sql
27910 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
27920 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ts..*/.typedef s
27930 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 65  truct Mem sqlite
27940 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20  3_value;../*.** 
27950 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 46 75  CAPI3REF: SQL Fu
27960 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f  nction Context O
27970 62 6a 65 63 74 20 7b 48 31 36 30 30 31 7d 20 3c  bject {H16001} <
27980 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20200>.**.** Th
27990 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69  e context in whi
279a0 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  ch an SQL functi
279b0 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 73  on executes is s
279c0 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73  tored in an.** s
279d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f  qlite3_context o
279e0 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 65  bject.  A pointe
279f0 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f  r to an sqlite3_
27a00 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a  context object.*
27a10 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72 73  * is always firs
27a20 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  t parameter to [
27a30 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
27a40 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ned SQL function
27a50 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  s]..** The appli
27a60 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53  cation-defined S
27a70 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  QL function impl
27a80 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20  ementation will 
27a90 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69  pass this.** poi
27aa0 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e 74  nter through int
27ab0 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69  o calls to [sqli
27ac0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c  te3_result_int |
27ad0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 28   sqlite3_result(
27ae0 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  )],.** [sqlite3_
27af0 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
27b00 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75  t()], [sqlite3_u
27b10 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20  ser_data()],.** 
27b20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  [sqlite3_context
27b30 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b  _db_handle()], [
27b40 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
27b50 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f  ata()],.** and/o
27b60 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  r [sqlite3_set_a
27b70 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79  uxdata()]..*/.ty
27b80 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
27b90 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c  ite3_context sql
27ba0 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f  ite3_context;../
27bb0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 42  *.** CAPI3REF: B
27bc0 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54 6f  inding Values To
27bd0 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d   Prepared Statem
27be0 65 6e 74 73 20 7b 48 31 33 35 30 30 7d 20 3c 53  ents {H13500} <S
27bf0 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  70300>.** KEYWOR
27c00 44 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65  DS: {host parame
27c10 74 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d  ter} {host param
27c20 65 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72  eters} {host par
27c30 61 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20  ameter name}.** 
27c40 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70  KEYWORDS: {SQL p
27c50 61 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70  arameter} {SQL p
27c60 61 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61  arameters} {para
27c70 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a  meter binding}.*
27c80 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c 20  *.** In the SQL 
27c90 73 74 72 69 6e 67 73 20 69 6e 70 75 74 20 74 6f  strings input to
27ca0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
27cb0 65 5f 76 32 28 29 5d 20 61 6e 64 20 69 74 73 20  e_v2()] and its 
27cc0 76 61 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74  variants,.** lit
27cd0 65 72 61 6c 73 20 6d 61 79 20 62 65 20 72 65 70  erals may be rep
27ce0 6c 61 63 65 64 20 62 79 20 61 20 70 61 72 61 6d  laced by a param
27cf0 65 74 65 72 20 69 6e 20 6f 6e 65 20 6f 66 20 74  eter in one of t
27d00 68 65 73 65 20 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a  hese forms:.**.*
27d10 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
27d20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a  ?.** <li>  ?NNN.
27d30 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a  ** <li>  :VVV.**
27d40 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c   <li>  @VVV.** <
27d50 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75  li>  $VVV.** </u
27d60 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  l>.**.** In the 
27d70 70 61 72 61 6d 65 74 65 72 20 66 6f 72 6d 73 20  parameter forms 
27d80 73 68 6f 77 6e 20 61 62 6f 76 65 20 4e 4e 4e 20  shown above NNN 
27d90 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c 69  is an integer li
27da0 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56  teral,.** and VV
27db0 56 20 69 73 20 61 6e 20 61 6c 70 68 61 2d 6e 75  V is an alpha-nu
27dc0 6d 65 72 69 63 20 70 61 72 61 6d 65 74 65 72 20  meric parameter 
27dd0 6e 61 6d 65 2e 20 54 68 65 20 76 61 6c 75 65 73  name. The values
27de0 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 61 72   of these.** par
27df0 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 63 61  ameters (also ca
27e00 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72 61 6d  lled "host param
27e10 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 20 22  eter names" or "
27e20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 22 29  SQL parameters")
27e30 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 75  .** can be set u
27e40 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  sing the sqlite3
27e50 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e  _bind_*() routin
27e60 65 73 20 64 65 66 69 6e 65 64 20 68 65 72 65 2e  es defined here.
27e70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
27e80 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
27e90 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28   sqlite3_bind_*(
27ea0 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20 61 6c  ) routines is al
27eb0 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  ways.** a pointe
27ec0 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  r to the [sqlite
27ed0 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72  3_stmt] object r
27ee0 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  eturned from.** 
27ef0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
27f00 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20 76 61  _v2()] or its va
27f10 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  riants..**.** Th
27f20 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
27f30 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f  t is the index o
27f40 66 20 74 68 65 20 53 51 4c 20 70 61 72 61 6d 65  f the SQL parame
27f50 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a  ter to be set..*
27f60 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 53  * The leftmost S
27f70 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68 61 73  QL parameter has
27f80 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 20   an index of 1. 
27f90 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65 20 6e   When the same n
27fa0 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61  amed.** SQL para
27fb0 6d 65 74 65 72 20 69 73 20 75 73 65 64 20 6d 6f  meter is used mo
27fc0 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65  re than once, se
27fd0 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75  cond and subsequ
27fe0 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63  ent.** occurrenc
27ff0 65 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  es have the same
28000 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 66 69   index as the fi
28010 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a  rst occurrence..
28020 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72  ** The index for
28030 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72   named parameter
28040 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20  s can be looked 
28050 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  up using the.** 
28060 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
28070 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d  rameter_index()]
28080 20 41 50 49 20 69 66 20 64 65 73 69 72 65 64 2e   API if desired.
28090 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66    The index.** f
280a0 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65  or "?NNN" parame
280b0 74 65 72 73 20 69 73 20 74 68 65 20 76 61 6c 75  ters is the valu
280c0 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65  e of NNN..** The
280d0 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 74 20   NNN value must 
280e0 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  be between 1 and
280f0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69   the [sqlite3_li
28100 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65  mit()].** parame
28110 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  ter [SQLITE_LIMI
28120 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
28130 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61 6c 75  R] (default valu
28140 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54  e: 999)..**.** T
28150 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
28160 74 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 74  t is the value t
28170 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20 70 61  o bind to the pa
28180 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49  rameter..**.** I
28190 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e 65 73  n those routines
281a0 20 74 68 61 74 20 68 61 76 65 20 61 20 66 6f 75   that have a fou
281b0 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 74  rth argument, it
281c0 73 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a  s value is the.*
281d0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * number of byte
281e0 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74  s in the paramet
281f0 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 61 72  er.  To be clear
28200 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74  : the value is t
28210 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
28220 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20  <u>bytes</u> in 
28230 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74  the value, not t
28240 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
28250 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74  racters..** If t
28260 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  he fourth parame
28270 74 65 72 20 69 73 20 6e 65 67 61 74 69 76 65 2c  ter is negative,
28280 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
28290 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20  he string is.** 
282a0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
282b0 74 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69  tes up to the fi
282c0 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  rst zero termina
282d0 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  tor..**.** The f
282e0 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f  ifth argument to
282f0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
28300 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 69  ob(), sqlite3_bi
28310 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a  nd_text(), and.*
28320 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74  * sqlite3_bind_t
28330 65 78 74 31 36 28 29 20 69 73 20 61 20 64 65 73  ext16() is a des
28340 74 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f 20  tructor used to 
28350 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 42  dispose of the B
28360 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67  LOB or.** string
28370 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 68 61   after SQLite ha
28380 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20  s finished with 
28390 69 74 2e 20 49 66 20 74 68 65 20 66 69 66 74 68  it. If the fifth
283a0 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20   argument is.** 
283b0 74 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75  the special valu
283c0 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43  e [SQLITE_STATIC
283d0 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61  ], then SQLite a
283e0 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 0a  ssumes that the.
283f0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ** information i
28400 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d  s in static, unm
28410 61 6e 61 67 65 64 20 73 70 61 63 65 20 61 6e 64  anaged space and
28420 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
28430 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49  o be freed..** I
28440 66 20 74 68 65 20 66 69 66 74 68 20 61 72 67 75  f the fifth argu
28450 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76 61 6c  ment has the val
28460 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53  ue [SQLITE_TRANS
28470 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53  IENT], then.** S
28480 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 73 20  QLite makes its 
28490 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79  own private copy
284a0 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 6d 6d   of the data imm
284b0 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65  ediately, before
284c0 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f  .** the sqlite3_
284d0 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65  bind_*() routine
284e0 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20   returns..**.** 
284f0 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  The sqlite3_bind
28500 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74  _zeroblob() rout
28510 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c 4f 42  ine binds a BLOB
28520 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 68 61   of length N tha
28530 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 77  t.** is filled w
28540 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a  ith zeroes.  A z
28550 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 20 66  eroblob uses a f
28560 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d  ixed amount of m
28570 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61  emory.** (just a
28580 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c  n integer to hol
28590 64 20 69 74 73 20 73 69 7a 65 29 20 77 68 69 6c  d its size) whil
285a0 65 20 69 74 20 69 73 20 62 65 69 6e 67 20 70 72  e it is being pr
285b0 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f  ocessed..** Zero
285c0 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 6e 64  blobs are intend
285d0 65 64 20 74 6f 20 73 65 72 76 65 20 61 73 20 70  ed to serve as p
285e0 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20  laceholders for 
285f0 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63  BLOBs whose.** c
28600 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 72 20  ontent is later 
28610 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a  written using.**
28620 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
28630 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61  pen | incrementa
28640 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74  l BLOB I/O] rout
28650 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74  ines..** A negat
28660 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ive value for th
28670 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c  e zeroblob resul
28680 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e  ts in a zero-len
28690 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20  gth BLOB..**.** 
286a0 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  The sqlite3_bind
286b0 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 6d 75  _*() routines mu
286c0 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74  st be called aft
286d0 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70  er.** [sqlite3_p
286e0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28 61 6e  repare_v2()] (an
286f0 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20  d its variants) 
28700 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  or [sqlite3_rese
28710 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f  t()] and.** befo
28720 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  re [sqlite3_step
28730 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73  ()]..** Bindings
28740 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 65 64   are not cleared
28750 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33   by the [sqlite3
28760 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74 69 6e  _reset()] routin
28770 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61  e..** Unbound pa
28780 72 61 6d 65 74 65 72 73 20 61 72 65 20 69 6e 74  rameters are int
28790 65 72 70 72 65 74 65 64 20 61 73 20 4e 55 4c 4c  erpreted as NULL
287a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
287b0 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 5b 53  utines return [S
287c0 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63  QLITE_OK] on suc
287d0 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72  cess or an error
287e0 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74   code if.** anyt
287f0 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
28800 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d    [SQLITE_RANGE]
28810 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
28820 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a  the parameter.**
28830 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66   index is out of
28840 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45   range.  [SQLITE
28850 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75 72  _NOMEM] is retur
28860 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20  ned if malloc() 
28870 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54  fails..** [SQLIT
28880 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 74 20  E_MISUSE] might 
28890 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  be returned if t
288a0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
288b0 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a  e called on a.**
288c0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
288d0 20 74 68 61 74 20 69 73 20 74 68 65 20 77 72 6f   that is the wro
288e0 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68 69 63  ng state or whic
288f0 68 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  h has already be
28900 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a  en finalized..**
28910 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69   Detection of mi
28920 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69 61 62  suse is unreliab
28930 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  le.  Application
28940 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70  s should not dep
28950 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45  end.** on SQLITE
28960 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e 73 2e  _MISUSE returns.
28970 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20    SQLITE_MISUSE 
28980 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 69  is intended to i
28990 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c  ndicate a.** a l
289a0 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 74 68  ogic error in th
289b0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20  e application.  
289c0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
289d0 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a  of SQLite might.
289e0 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65 72 20  ** panic rather 
289f0 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51 4c 49  than return SQLI
28a00 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a  TE_MISUSE..**.**
28a10 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
28a20 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
28a30 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20  er_count()],.** 
28a40 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
28a50 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c  rameter_name()],
28a60 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69   and [sqlite3_bi
28a70 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
28a80 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  ex()]..**.** INV
28a90 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
28aa0 48 31 33 35 30 36 7d 20 54 68 65 20 5b 53 51 4c  H13506} The [SQL
28ab0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69   statement compi
28ac0 6c 65 72 5d 20 72 65 63 6f 67 6e 69 7a 65 73 20  ler] recognizes 
28ad0 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 20 66 6f  tokens of the fo
28ae0 72 6d 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  rms.**          
28af0 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 24 56  "?", "?NNN", "$V
28b00 56 56 22 2c 20 22 3a 56 56 56 22 2c 20 61 6e 64  VV", ":VVV", and
28b10 20 22 40 56 56 56 22 20 61 73 20 53 51 4c 20 70   "@VVV" as SQL p
28b20 61 72 61 6d 65 74 65 72 73 2c 0a 2a 2a 20 20 20  arameters,.**   
28b30 20 20 20 20 20 20 20 77 68 65 72 65 20 4e 4e 4e         where NNN
28b40 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65   is any sequence
28b50 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   of one or more 
28b60 64 69 67 69 74 73 0a 2a 2a 20 20 20 20 20 20 20  digits.**       
28b70 20 20 20 61 6e 64 20 77 68 65 72 65 20 56 56 56     and where VVV
28b80 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65   is any sequence
28b90 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   of one or more 
28ba0 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a 20  alphanumeric.** 
28bb0 20 20 20 20 20 20 20 20 20 63 68 61 72 61 63 74           charact
28bc0 65 72 73 20 6f 72 20 22 3a 3a 22 20 6f 70 74 69  ers or "::" opti
28bd0 6f 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64 20  onally followed 
28be0 62 79 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74  by a string cont
28bf0 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  aining.**       
28c00 20 20 20 6e 6f 20 73 70 61 63 65 73 20 61 6e 64     no spaces and
28c10 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
28c20 6e 20 70 61 72 65 6e 74 68 65 73 65 73 2e 0a 2a  n parentheses..*
28c30 2a 0a 2a 2a 20 7b 48 31 33 35 30 39 7d 20 54 68  *.** {H13509} Th
28c40 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20  e initial value 
28c50 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65  of an SQL parame
28c60 74 65 72 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ter is NULL..**.
28c70 2a 2a 20 7b 48 31 33 35 31 32 7d 20 54 68 65 20  ** {H13512} The 
28c80 69 6e 64 65 78 20 6f 66 20 61 6e 20 22 3f 22 20  index of an "?" 
28c90 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73  SQL parameter is
28ca0 20 6f 6e 65 20 6c 61 72 67 65 72 20 74 68 61 6e   one larger than
28cb0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
28cc0 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f   largest index o
28cd0 66 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20  f SQL parameter 
28ce0 74 6f 20 74 68 65 20 6c 65 66 74 2c 20 6f 72 20  to the left, or 
28cf0 31 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  1 if.**         
28d00 20 74 68 65 20 22 3f 22 20 69 73 20 74 68 65 20   the "?" is the 
28d10 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61 72  leftmost SQL par
28d20 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ameter..**.** {H
28d30 31 33 35 31 35 7d 20 54 68 65 20 69 6e 64 65 78  13515} The index
28d40 20 6f 66 20 61 6e 20 22 3f 4e 4e 4e 22 20 53 51   of an "?NNN" SQ
28d50 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  L parameter is t
28d60 68 65 20 69 6e 74 65 67 65 72 20 4e 4e 4e 2e 0a  he integer NNN..
28d70 2a 2a 0a 2a 2a 20 7b 48 31 33 35 31 38 7d 20 54  **.** {H13518} T
28d80 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 22  he index of an "
28d90 3a 56 56 56 22 2c 20 22 24 56 56 56 22 2c 20 6f  :VVV", "$VVV", o
28da0 72 20 22 40 56 56 56 22 20 53 51 4c 20 70 61 72  r "@VVV" SQL par
28db0 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 20 20 20  ameter is.**    
28dc0 20 20 20 20 20 20 74 68 65 20 73 61 6d 65 20 61        the same a
28dd0 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 6c  s the index of l
28de0 65 66 74 6d 6f 73 74 20 6f 63 63 75 72 72 65 6e  eftmost occurren
28df0 63 65 73 20 6f 66 20 74 68 65 20 73 61 6d 65 0a  ces of the same.
28e00 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61  **          para
28e10 6d 65 74 65 72 2c 20 6f 72 20 6f 6e 65 20 6d 6f  meter, or one mo
28e20 72 65 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67  re than the larg
28e30 65 73 74 20 69 6e 64 65 78 20 6f 76 65 72 20 61  est index over a
28e40 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70  ll.**          p
28e50 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65  arameters to the
28e60 20 6c 65 66 74 20 69 66 20 74 68 69 73 20 69 73   left if this is
28e70 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 72   the first occur
28e80 72 65 6e 63 65 0a 2a 2a 20 20 20 20 20 20 20 20  rence.**        
28e90 20 20 6f 66 20 74 68 69 73 20 70 61 72 61 6d 65    of this parame
28ea0 74 65 72 2c 20 6f 72 20 31 20 69 66 20 74 68 69  ter, or 1 if thi
28eb0 73 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f 73  s is the leftmos
28ec0 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a  t parameter..**.
28ed0 2a 2a 20 7b 48 31 33 35 32 31 7d 20 54 68 65 20  ** {H13521} The 
28ee0 5b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63  [SQL statement c
28ef0 6f 6d 70 69 6c 65 72 5d 20 66 61 69 6c 73 20 77  ompiler] fails w
28f00 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 52  ith an [SQLITE_R
28f10 41 4e 47 45 5d 0a 2a 2a 20 20 20 20 20 20 20 20  ANGE].**        
28f20 20 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 69    error if the i
28f30 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 4c 20 70  ndex of an SQL p
28f40 61 72 61 6d 65 74 65 72 20 69 73 20 6c 65 73 73  arameter is less
28f50 20 74 68 61 6e 20 31 0a 2a 2a 20 20 20 20 20 20   than 1.**      
28f60 20 20 20 20 6f 72 20 67 72 65 61 74 65 72 20 74      or greater t
28f70 68 61 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d  han the compile-
28f80 74 69 6d 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  time SQLITE_MAX_
28f90 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a  VARIABLE_NUMBER.
28fa0 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61  **          para
28fb0 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  meter..**.** {H1
28fc0 33 35 32 34 7d 20 43 61 6c 6c 73 20 74 6f 20 5b  3524} Calls to [
28fd0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
28fe0 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  t | sqlite3_bind
28ff0 28 53 2c 4e 2c 56 2c 2e 2e 2e 29 5d 0a 2a 2a 20  (S,N,V,...)].** 
29000 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61           associa
29010 74 65 20 74 68 65 20 76 61 6c 75 65 20 56 20 77  te the value V w
29020 69 74 68 20 61 6c 6c 20 53 51 4c 20 70 61 72 61  ith all SQL para
29030 6d 65 74 65 72 73 20 68 61 76 69 6e 67 20 61 6e  meters having an
29040 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 64  .**          ind
29050 65 78 20 6f 66 20 4e 20 69 6e 20 74 68 65 20 5b  ex of N in the [
29060 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
29070 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  nt] S..**.** {H1
29080 33 35 32 37 7d 20 43 61 6c 6c 73 20 74 6f 20 5b  3527} Calls to [
29090 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
290a0 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  t | sqlite3_bind
290b0 28 53 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20  (S,N,...)].**   
290c0 20 20 20 20 20 20 20 6f 76 65 72 72 69 64 65 20         override 
290d0 70 72 69 6f 72 20 63 61 6c 6c 73 20 77 69 74 68  prior calls with
290e0 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 73   the same values
290f0 20 6f 66 20 53 20 61 6e 64 20 4e 2e 0a 2a 2a 0a   of S and N..**.
29100 2a 2a 20 7b 48 31 33 35 33 30 7d 20 42 69 6e 64  ** {H13530} Bind
29110 69 6e 67 73 20 65 73 74 61 62 6c 69 73 68 65 64  ings established
29120 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e   by [sqlite3_bin
29130 64 5f 74 65 78 74 20 7c 20 73 71 6c 69 74 65 33  d_text | sqlite3
29140 5f 62 69 6e 64 28 53 2c 2e 2e 2e 29 5d 0a 2a 2a  _bind(S,...)].**
29150 20 20 20 20 20 20 20 20 20 20 70 65 72 73 69 73            persis
29160 74 20 61 63 72 6f 73 73 20 63 61 6c 6c 73 20 74  t across calls t
29170 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  o [sqlite3_reset
29180 28 53 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  (S)]..**.** {H13
29190 35 33 33 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f  533} In calls to
291a0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
291b0 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c  lob(S,N,V,L,D)],
291c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
291d0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28  lite3_bind_text(
291e0 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a  S,N,V,L,D)], or.
291f0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
29200 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
29210 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 4c  (S,N,V,L,D)] SQL
29220 69 74 65 20 62 69 6e 64 73 20 74 68 65 20 66 69  ite binds the fi
29230 72 73 74 20 4c 0a 2a 2a 20 20 20 20 20 20 20 20  rst L.**        
29240 20 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42    bytes of the B
29250 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 20 70 6f  LOB or string po
29260 69 6e 74 65 64 20 74 6f 20 62 79 20 56 2c 20 77  inted to by V, w
29270 68 65 6e 20 4c 0a 2a 2a 20 20 20 20 20 20 20 20  hen L.**        
29280 20 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76    is non-negativ
29290 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 33 36  e..**.** {H13536
292a0 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73  } In calls to [s
292b0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
292c0 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 6f 72 0a  (S,N,V,L,D)] or.
292d0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
292e0 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
292f0 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 4c  (S,N,V,L,D)] SQL
29300 69 74 65 20 62 69 6e 64 73 20 63 68 61 72 61 63  ite binds charac
29310 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ters.**         
29320 20 66 72 6f 6d 20 56 20 74 68 72 6f 75 67 68 20   from V through 
29330 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 63  the first zero c
29340 68 61 72 61 63 74 65 72 20 77 68 65 6e 20 4c 20  haracter when L 
29350 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a  is negative..**.
29360 2a 2a 20 7b 48 31 33 35 33 39 7d 20 49 6e 20 63  ** {H13539} In c
29370 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
29380 5f 62 69 6e 64 5f 62 6c 6f 62 28 53 2c 4e 2c 56  _bind_blob(S,N,V
29390 2c 4c 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  ,L,D)],.**      
293a0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e      [sqlite3_bin
293b0 64 5f 74 65 78 74 28 53 2c 4e 2c 56 2c 4c 2c 44  d_text(S,N,V,L,D
293c0 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  )], or.**       
293d0 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64     [sqlite3_bind
293e0 5f 74 65 78 74 31 36 28 53 2c 4e 2c 56 2c 4c 2c  _text16(S,N,V,L,
293f0 44 29 5d 20 77 68 65 6e 20 44 20 69 73 20 74 68  D)] when D is th
29400 65 20 73 70 65 63 69 61 6c 0a 2a 2a 20 20 20 20  e special.**    
29410 20 20 20 20 20 20 63 6f 6e 73 74 61 6e 74 20 5b        constant [
29420 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20  SQLITE_STATIC], 
29430 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74  SQLite assumes t
29440 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 56 0a  hat the value V.
29450 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 68  **          is h
29460 65 6c 64 20 69 6e 20 73 74 61 74 69 63 20 75 6e  eld in static un
29470 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 74 68  managed space th
29480 61 74 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e  at will not chan
29490 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64  ge.**          d
294a0 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69  uring the lifeti
294b0 6d 65 20 6f 66 20 74 68 65 20 62 69 6e 64 69 6e  me of the bindin
294c0 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 34 32  g..**.** {H13542
294d0 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73  } In calls to [s
294e0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
294f0 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 0a 2a 2a  (S,N,V,L,D)],.**
29500 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
29510 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 53 2c 4e  e3_bind_text(S,N
29520 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20  ,V,L,D)], or.** 
29530 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
29540 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 53 2c  3_bind_text16(S,
29550 4e 2c 56 2c 4c 2c 44 29 5d 20 77 68 65 6e 20 44  N,V,L,D)] when D
29560 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 0a   is the special.
29570 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73  **          cons
29580 74 61 6e 74 20 5b 53 51 4c 49 54 45 5f 54 52 41  tant [SQLITE_TRA
29590 4e 53 49 45 4e 54 5d 2c 20 74 68 65 20 72 6f 75  NSIENT], the rou
295a0 74 69 6e 65 20 6d 61 6b 65 73 20 61 0a 2a 2a 20  tine makes a.** 
295b0 20 20 20 20 20 20 20 20 20 70 72 69 76 61 74 65           private
295c0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c   copy of the val
295d0 75 65 20 56 20 62 65 66 6f 72 65 20 69 74 20 72  ue V before it r
295e0 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  eturns..**.** {H
295f0 31 33 35 34 35 7d 20 49 6e 20 63 61 6c 6c 73 20  13545} In calls 
29600 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  to [sqlite3_bind
29610 5f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29  _blob(S,N,V,L,D)
29620 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  ],.**          [
29630 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
29640 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f  t(S,N,V,L,D)], o
29650 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  r.**          [s
29660 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
29670 31 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 77  16(S,N,V,L,D)] w
29680 68 65 6e 20 44 20 69 73 20 61 20 70 6f 69 6e 74  hen D is a point
29690 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  er to.**        
296a0 20 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51    a function, SQ
296b0 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 61  Lite invokes tha
296c0 74 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65  t function to de
296d0 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20 20  stroy the.**    
296e0 20 20 20 20 20 20 76 61 6c 75 65 20 56 20 61 66        value V af
296f0 74 65 72 20 69 74 20 68 61 73 20 66 69 6e 69 73  ter it has finis
29700 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 76 61  hed using the va
29710 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  lue V..**.** {H1
29720 33 35 34 38 7d 20 49 6e 20 63 61 6c 6c 73 20 74  3548} In calls t
29730 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  o [sqlite3_bind_
29740 7a 65 72 6f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c  zeroblob(S,N,V,L
29750 29 5d 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75  )] the value bou
29760 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  nd.**          i
29770 73 20 61 20 42 4c 4f 42 20 6f 66 20 4c 20 62 79  s a BLOB of L by
29780 74 65 73 2c 20 6f 72 20 61 20 7a 65 72 6f 2d 6c  tes, or a zero-l
29790 65 6e 67 74 68 20 42 4c 4f 42 20 69 66 20 4c 20  ength BLOB if L 
297a0 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a  is negative..**.
297b0 2a 2a 20 7b 48 31 33 35 35 31 7d 20 49 6e 20 63  ** {H13551} In c
297c0 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
297d0 5f 62 69 6e 64 5f 76 61 6c 75 65 28 53 2c 4e 2c  _bind_value(S,N,
297e0 56 29 5d 20 74 68 65 20 56 20 61 72 67 75 6d 65  V)] the V argume
297f0 6e 74 20 6d 61 79 0a 2a 2a 20 20 20 20 20 20 20  nt may.**       
29800 20 20 20 62 65 20 65 69 74 68 65 72 20 61 20 5b     be either a [
29810 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
29820 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
29830 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20  or an.**        
29840 20 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73    [unprotected s
29850 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
29860 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ject..*/.SQLITE_
29870 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
29880 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65  bind_blob(sqlite
29890 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
298a0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e  nst void*, int n
298b0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
298c0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
298d0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  t sqlite3_bind_d
298e0 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74  ouble(sqlite3_st
298f0 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65  mt*, int, double
29900 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
29910 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  t sqlite3_bind_i
29920 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt(sqlite3_stmt*
29930 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
29940 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
29950 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
29960 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
29970 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  t, sqlite3_int64
29980 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
29990 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e  t sqlite3_bind_n
299a0 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ull(sqlite3_stmt
299b0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
299c0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
299d0 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 65  bind_text(sqlite
299e0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
299f0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e  nst char*, int n
29a00 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
29a10 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
29a20 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74  t sqlite3_bind_t
29a30 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ext16(sqlite3_st
29a40 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  mt*, int, const 
29a50 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  void*, int, void
29a60 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
29a70 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
29a80 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73  te3_bind_value(s
29a90 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
29aa0 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  t, const sqlite3
29ab0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
29ac0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
29ad0 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73  _bind_zeroblob(s
29ae0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
29af0 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a  t, int n);../*.*
29b00 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62  * CAPI3REF: Numb
29b10 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d 65  er Of SQL Parame
29b20 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c 53  ters {H13600} <S
29b30 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  70300>.**.** Thi
29b40 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65  s routine can be
29b50 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68   used to find th
29b60 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c  e number of [SQL
29b70 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20   parameters].** 
29b80 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  in a [prepared s
29b90 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20  tatement].  SQL 
29ba0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74  parameters are t
29bb0 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20  okens of the.** 
29bc0 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22  form "?", "?NNN"
29bd0 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 22  , ":AAA", "$AAA"
29be0 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 74  , or "@AAA" that
29bf0 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61   serve as.** pla
29c00 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 61  ceholders for va
29c10 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b 73  lues that are [s
29c20 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
29c30 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20   | bound].** to 
29c40 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61  the parameters a
29c50 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a  t a later time..
29c60 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
29c70 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75  ne actually retu
29c80 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66  rns the index of
29c90 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 69   the largest (ri
29ca0 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61  ghtmost).** para
29cb0 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66  meter. For all f
29cc0 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e  orms except ?NNN
29cd0 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72  , this will corr
29ce0 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a  espond to the.**
29cf0 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75   number of uniqu
29d00 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49  e parameters.  I
29d10 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20  f parameters of 
29d20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65  the ?NNN are use
29d30 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 20  d,.** there may 
29d40 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 6c  be gaps in the l
29d50 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ist..**.** See a
29d60 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69  lso: [sqlite3_bi
29d70 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f  nd_blob|sqlite3_
29d80 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  bind()],.** [sql
29d90 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
29da0 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64  ter_name()], and
29db0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
29dc0 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
29dd0 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  x()]..**.** INVA
29de0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
29df0 31 33 36 30 31 7d 20 54 68 65 20 5b 73 71 6c 69  13601} The [sqli
29e00 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
29e10 65 72 5f 63 6f 75 6e 74 28 53 29 5d 20 69 6e 74  er_count(S)] int
29e20 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a 2a  erface returns.*
29e30 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6c  *          the l
29e40 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f 66 20  argest index of 
29e50 61 6c 6c 20 53 51 4c 20 70 61 72 61 6d 65 74 65  all SQL paramete
29e60 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  rs in the.**    
29e70 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20        [prepared 
29e80 73 74 61 74 65 6d 65 6e 74 5d 20 53 2c 20 6f 72  statement] S, or
29e90 20 30 20 69 66 20 53 20 63 6f 6e 74 61 69 6e 73   0 if S contains
29ea0 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74 65   no SQL paramete
29eb0 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  rs..*/.SQLITE_AP
29ec0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
29ed0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75  nd_parameter_cou
29ee0 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt(sqlite3_stmt*
29ef0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
29f00 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f  EF: Name Of A Ho
29f10 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b 48 31  st Parameter {H1
29f20 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a  3620} <S70300>.*
29f30 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
29f40 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
29f50 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20  ter to the name 
29f60 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b  of the n-th.** [
29f70 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 69  SQL parameter] i
29f80 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  n a [prepared st
29f90 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c  atement]..** SQL
29fa0 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74   parameters of t
29fb0 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f  he form "?NNN" o
29fc0 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41 41  r ":AAA" or "@AA
29fd0 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20  A" or "$AAA".** 
29fe0 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69 63  have a name whic
29ff0 68 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 20  h is the string 
2a000 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22  "?NNN" or ":AAA"
2a010 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22 24   or "@AAA" or "$
2a020 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74 69  AAA".** respecti
2a030 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65  vely..** In othe
2a040 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e 69  r words, the ini
2a050 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 20  tial ":" or "$" 
2a060 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a  or "@" or "?".**
2a070 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73 20   is included as 
2a080 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65  part of the name
2a090 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 20  ..** Parameters 
2a0a0 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 20  of the form "?" 
2a0b0 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77  without a follow
2a0c0 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61 76 65  ing integer have
2a0d0 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20   no name.** and 
2a0e0 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72 72 65  are also referre
2a0f0 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f  d to as "anonymo
2a100 75 73 20 70 61 72 61 6d 65 74 65 72 73 22 2e 0a  us parameters"..
2a110 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
2a120 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 68  host parameter h
2a130 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31  as an index of 1
2a140 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  , not 0..**.** I
2a150 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69 73  f the value n is
2a160 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72   out of range or
2a170 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61 72   if the n-th par
2a180 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d  ameter is.** nam
2a190 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c  eless, then NULL
2a1a0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54   is returned.  T
2a1b0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
2a1c0 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20  ng is.** always 
2a1d0 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e  in UTF-8 encodin
2a1e0 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e 61  g even if the na
2a1f0 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 77 61  med parameter wa
2a200 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20  s.** originally 
2a210 73 70 65 63 69 66 69 65 64 20 61 73 20 55 54 46  specified as UTF
2a220 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f  -16 in [sqlite3_
2a230 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72 0a  prepare16()] or.
2a240 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
2a250 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a  are16_v2()]..**.
2a260 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
2a270 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c  lite3_bind_blob|
2a280 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c  sqlite3_bind()],
2a290 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
2a2a0 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
2a2b0 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  t()], and.** [sq
2a2c0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
2a2d0 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a  eter_index()]..*
2a2e0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
2a2f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 36 32 31 7d 20  .**.** {H13621} 
2a300 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  The [sqlite3_bin
2a310 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
2a320 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  (S,N)] interface
2a330 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20   returns.**     
2a340 20 20 20 20 20 61 20 55 54 46 2d 38 20 72 65 6e       a UTF-8 ren
2a350 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 6e 61  dering of the na
2a360 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61  me of the SQL pa
2a370 72 61 6d 65 74 65 72 20 69 6e 0a 2a 2a 20 20 20  rameter in.**   
2a380 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 65 70         the [prep
2a390 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
2a3a0 53 20 68 61 76 69 6e 67 20 69 6e 64 65 78 20 4e  S having index N
2a3b0 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  , or.**         
2a3c0 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69   NULL if there i
2a3d0 73 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74  s no SQL paramet
2a3e0 65 72 20 77 69 74 68 20 69 6e 64 65 78 20 4e 20  er with index N 
2a3f0 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 20  or if the.**    
2a400 20 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 20        parameter 
2a410 77 69 74 68 20 69 6e 64 65 78 20 4e 20 69 73 20  with index N is 
2a420 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 70 61 72  an anonymous par
2a430 61 6d 65 74 65 72 20 22 3f 22 2e 0a 2a 2f 0a 53  ameter "?"..*/.S
2a440 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2a450 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69  char *sqlite3_bi
2a460 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
2a470 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
2a480 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   int);../*.** CA
2a490 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66  PI3REF: Index Of
2a4a0 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69 74   A Parameter Wit
2a4b0 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b  h A Given Name {
2a4c0 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e  H13640} <S70300>
2a4d0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
2a4e0 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51  e index of an SQ
2a4f0 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76 65  L parameter give
2a500 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65  n its name.  The
2a510 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20  .** index value 
2a520 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69 74  returned is suit
2a530 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20  able for use as 
2a540 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61  the second.** pa
2a550 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
2a560 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71  te3_bind_blob|sq
2a570 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20  lite3_bind()].  
2a580 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74  A zero.** is ret
2a590 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63  urned if no matc
2a5a0 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69  hing parameter i
2a5b0 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61  s found.  The pa
2a5c0 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20  rameter.** name 
2a5d0 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e  must be given in
2a5e0 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74   UTF-8 even if t
2a5f0 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  he original stat
2a600 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65  ement.** was pre
2a610 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31  pared from UTF-1
2a620 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71  6 text using [sq
2a630 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
2a640 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  v2()]..**.** See
2a650 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
2a660 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65  bind_blob|sqlite
2a670 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73  3_bind()],.** [s
2a680 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
2a690 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20  meter_count()], 
2a6a0 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  and.** [sqlite3_
2a6b0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
2a6c0 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49  ndex()]..**.** I
2a6d0 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
2a6e0 20 7b 48 31 33 36 34 31 7d 20 54 68 65 20 5b 73   {H13641} The [s
2a6f0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
2a700 6d 65 74 65 72 5f 69 6e 64 65 78 28 53 2c 4e 29  meter_index(S,N)
2a710 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ] interface retu
2a720 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  rns.**          
2a730 74 68 65 20 69 6e 64 65 78 20 6f 66 20 53 51 4c  the index of SQL
2a740 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68   parameter in th
2a750 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
2a760 65 6d 65 6e 74 5d 0a 2a 2a 20 20 20 20 20 20 20  ement].**       
2a770 20 20 20 53 20 77 68 6f 73 65 20 6e 61 6d 65 20     S whose name 
2a780 6d 61 74 63 68 65 73 20 74 68 65 20 55 54 46 2d  matches the UTF-
2a790 38 20 73 74 72 69 6e 67 20 4e 2c 20 6f 72 20 30  8 string N, or 0
2a7a0 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20   if there is.** 
2a7b0 20 20 20 20 20 20 20 20 20 6e 6f 20 6d 61 74 63           no matc
2a7c0 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  h..*/.SQLITE_API
2a7d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
2a7e0 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
2a7f0 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  x(sqlite3_stmt*,
2a800 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2a810 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  me);../*.** CAPI
2a820 33 52 45 46 3a 20 52 65 73 65 74 20 41 6c 6c 20  3REF: Reset All 
2a830 42 69 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50 72  Bindings On A Pr
2a840 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74  epared Statement
2a850 20 7b 48 31 33 36 36 30 7d 20 3c 53 37 30 33 30   {H13660} <S7030
2a860 30 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72  0>.**.** Contrar
2a870 79 20 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69  y to the intuiti
2a880 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c  on of many, [sql
2a890 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f  ite3_reset()] do
2a8a0 65 73 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20  es not reset.** 
2a8b0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  the [sqlite3_bin
2a8c0 64 5f 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67  d_blob | binding
2a8d0 73 5d 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65  s] on a [prepare
2a8e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a  d statement]..**
2a8f0 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e   Use this routin
2a900 65 20 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 68  e to reset all h
2a910 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 74  ost parameters t
2a920 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e  o NULL..**.** IN
2a930 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
2a940 7b 48 31 33 36 36 31 7d 20 54 68 65 20 5b 73 71  {H13661} The [sq
2a950 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64  lite3_clear_bind
2a960 69 6e 67 73 28 53 29 5d 20 69 6e 74 65 72 66 61  ings(S)] interfa
2a970 63 65 20 72 65 73 65 74 73 20 61 6c 6c 20 53 51  ce resets all SQ
2a980 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61  L.**          pa
2a990 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 73  rameter bindings
2a9a0 20 69 6e 20 74 68 65 20 5b 70 72 65 70 61 72 65   in the [prepare
2a9b0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 62  d statement] S b
2a9c0 61 63 6b 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ack to NULL..*/.
2a9d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2a9e0 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e  qlite3_clear_bin
2a9f0 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74  dings(sqlite3_st
2aa00 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  mt*);../*.** CAP
2aa10 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66  I3REF: Number Of
2aa20 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 65   Columns In A Re
2aa30 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 30  sult Set {H13710
2aa40 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10700>.**.**
2aa50 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
2aa60 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
2aa70 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2aa80 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a  returned by the.
2aa90 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ** [prepared sta
2aaa0 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 6f  tement]. This ro
2aab0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 20  utine returns 0 
2aac0 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 53  if pStmt is an S
2aad0 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  QL.** statement 
2aae0 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65  that does not re
2aaf0 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 65  turn data (for e
2ab00 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 54  xample an [UPDAT
2ab10 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  E])..**.** INVAR
2ab20 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31  IANTS:.**.** {H1
2ab30 33 37 31 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  3711} The [sqlit
2ab40 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
2ab50 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65  S)] interface re
2ab60 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
2ab70 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
2ab80 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
2ab90 65 73 75 6c 74 20 73 65 74 20 67 65 6e 65 72 61  esult set genera
2aba0 74 65 64 20 62 79 20 74 68 65 20 5b 70 72 65 70  ted by the [prep
2abb0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
2abc0 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  S,.**          o
2abd0 72 20 30 20 69 66 20 53 20 64 6f 65 73 20 6e 6f  r 0 if S does no
2abe0 74 20 67 65 6e 65 72 61 74 65 20 61 20 72 65 73  t generate a res
2abf0 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  ult set..*/.SQLI
2ac00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2ac10 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
2ac20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
2ac30 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  tmt);../*.** CAP
2ac40 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61  I3REF: Column Na
2ac50 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20  mes In A Result 
2ac60 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c 53 31  Set {H13720} <S1
2ac70 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  0700>.**.** Thes
2ac80 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  e routines retur
2ac90 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67  n the name assig
2aca0 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69 63 75  ned to a particu
2acb0 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e  lar column.** in
2acc0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2acd0 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74  of a [SELECT] st
2ace0 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 73 71  atement.  The sq
2acf0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
2ad00 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  e().** interface
2ad10 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
2ad20 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72  er to a zero-ter
2ad30 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
2ad40 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69  ring.** and sqli
2ad50 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
2ad60 36 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f  6() returns a po
2ad70 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d  inter to a zero-
2ad80 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54  terminated.** UT
2ad90 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68  F-16 string.  Th
2ada0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
2adb0 72 20 69 73 20 74 68 65 20 5b 70 72 65 70 61 72  r is the [prepar
2adc0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a  ed statement].**
2add0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
2ade0 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 73 74   the [SELECT] st
2adf0 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 63  atement. The sec
2ae00 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  ond parameter is
2ae10 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e   the.** column n
2ae20 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 66 74  umber.  The left
2ae30 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e  most column is n
2ae40 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54  umber 0..**.** T
2ae50 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
2ae60 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61  ng pointer is va
2ae70 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72  lid until either
2ae80 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73   the [prepared s
2ae90 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20  tatement].** is 
2aea0 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71  destroyed by [sq
2aeb0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
2aec0 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 6e  ] or until the n
2aed0 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73  ext call to.** s
2aee0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
2aef0 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  me() or sqlite3_
2af00 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20  column_name16() 
2af10 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75  on the same colu
2af20 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c  mn..**.** If sql
2af30 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61  ite3_malloc() fa
2af40 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65 20 70  ils during the p
2af50 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 69 74  rocessing of eit
2af60 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28  her routine.** (
2af70 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 72 69  for example duri
2af80 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ng a conversion 
2af90 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54  from UTF-8 to UT
2afa0 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20  F-16) then a.** 
2afb0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20  NULL pointer is 
2afc0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
2afd0 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 72 65  The name of a re
2afe0 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74  sult column is t
2aff0 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
2b000 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f 72 0a  "AS" clause for.
2b010 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20  ** that column, 
2b020 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 41  if there is an A
2b030 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74 68  S clause.  If th
2b040 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63 6c 61  ere is no AS cla
2b050 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  use.** then the 
2b060 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  name of the colu
2b070 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66 69 65  mn is unspecifie
2b080 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e 67 65  d and may change
2b090 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c   from.** one rel
2b0a0 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 20 74  ease of SQLite t
2b0b0 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a  o the next..**.*
2b0c0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
2b0d0 0a 2a 2a 20 7b 48 31 33 37 32 31 7d 20 41 20 73  .** {H13721} A s
2b0e0 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61  uccessful invoca
2b0f0 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c  tion of the [sql
2b100 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
2b110 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20 20  (S,N)].**       
2b120 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65 74     interface ret
2b130 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  urns the name of
2b140 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
2b150 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20 20  (where 0 is.**  
2b160 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66 74          the left
2b170 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f 72  most column) for
2b180 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2b190 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  of the.**       
2b1a0 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 61     [prepared sta
2b1b0 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20 7a  tement] S as a z
2b1c0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  ero-terminated U
2b1d0 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  TF-8 string..**.
2b1e0 2a 2a 20 7b 48 31 33 37 32 33 7d 20 41 20 73 75  ** {H13723} A su
2b1f0 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74  ccessful invocat
2b200 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  ion of the [sqli
2b210 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
2b220 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20  6(S,N)].**      
2b230 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65      interface re
2b240 74 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f  turns the name o
2b250 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  f the Nth column
2b260 20 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20   (where 0 is.** 
2b270 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66           the lef
2b280 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f  tmost column) fo
2b290 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  r the result set
2b2a0 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   of the.**      
2b2b0 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74      [prepared st
2b2c0 61 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20  atement] S as a 
2b2d0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
2b2e0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a 2a  UTF-16 string.**
2b2f0 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68 65            in the
2b300 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
2b310 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32  er..**.** {H1372
2b320 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  4} The [sqlite3_
2b330 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61  column_name()] a
2b340 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  nd [sqlite3_colu
2b350 6d 6e 5f 6e 61 6d 65 31 36 28 29 5d 0a 2a 2a 20  mn_name16()].** 
2b360 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61           interfa
2b370 63 65 73 20 72 65 74 75 72 6e 20 61 20 4e 55 4c  ces return a NUL
2b380 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68 65  L pointer if the
2b390 79 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 0a  y are unable to.
2b3a0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 6f  **          allo
2b3b0 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 68  cate memory to h
2b3c0 6f 6c 64 20 74 68 65 69 72 20 6e 6f 72 6d 61 6c  old their normal
2b3d0 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 73 2e   return strings.
2b3e0 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32 35 7d 20  .**.** {H13725} 
2b3f0 49 66 20 74 68 65 20 4e 20 70 61 72 61 6d 65 74  If the N paramet
2b400 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  er to [sqlite3_c
2b410 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d  olumn_name(S,N)]
2b420 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
2b430 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
2b440 6e 61 6d 65 31 36 28 53 2c 4e 29 5d 20 69 73 20  name16(S,N)] is 
2b450 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68  out of range, th
2b460 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  en the.**       
2b470 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72 65     interfaces re
2b480 74 75 72 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  turn a NULL poin
2b490 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  ter..**.** {H137
2b4a0 32 36 7d 20 54 68 65 20 73 74 72 69 6e 67 73 20  26} The strings 
2b4b0 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c  returned by [sql
2b4c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
2b4d0 28 53 2c 4e 29 5d 20 61 6e 64 0a 2a 2a 20 20 20  (S,N)] and.**   
2b4e0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
2b4f0 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 53 2c  column_name16(S,
2b500 4e 29 5d 20 61 72 65 20 76 61 6c 69 64 20 75 6e  N)] are valid un
2b510 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20  til the next.** 
2b520 20 20 20 20 20 20 20 20 20 63 61 6c 6c 20 74 6f           call to
2b530 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 20   either routine 
2b540 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 53 20  with the same S 
2b550 61 6e 64 20 4e 20 70 61 72 61 6d 65 74 65 72 73  and N parameters
2b560 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20  .**          or 
2b570 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f 66  until [sqlite3_f
2b580 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 63  inalize(S)] is c
2b590 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  alled..**.** {H1
2b5a0 33 37 32 37 7d 20 57 68 65 6e 20 61 20 72 65 73  3727} When a res
2b5b0 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20  ult column of a 
2b5c0 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
2b5d0 6e 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20  nt contains.**  
2b5e0 20 20 20 20 20 20 20 20 61 6e 20 41 53 20 63 6c          an AS cl
2b5f0 61 75 73 65 2c 20 74 68 65 20 6e 61 6d 65 20 6f  ause, the name o
2b600 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73  f that column is
2b610 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a   the identifier.
2b620 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 74  **          to t
2b630 68 65 20 72 69 67 68 74 20 6f 66 20 74 68 65 20  he right of the 
2b640 41 53 20 6b 65 79 77 6f 72 64 2e 0a 2a 2f 0a 53  AS keyword..*/.S
2b650 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2b660 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
2b670 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65  lumn_name(sqlite
2b680 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b  3_stmt*, int N);
2b690 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
2b6a0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
2b6b0 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71  column_name16(sq
2b6c0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
2b6d0 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49   N);../*.** CAPI
2b6e0 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20  3REF: Source Of 
2b6f0 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20  Data In A Query 
2b700 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20  Result {H13740} 
2b710 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10700>.**.** T
2b720 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72  hese routines pr
2b730 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f  ovide a means to
2b740 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20   determine what 
2b750 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a  column of what.*
2b760 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68  * table in which
2b770 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75   database a resu
2b780 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d  lt of a [SELECT]
2b790 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73   statement comes
2b7a0 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61   from..** The na
2b7b0 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
2b7c0 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63  se or table or c
2b7d0 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74  olumn can be ret
2b7e0 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68  urned as.** eith
2b7f0 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54  er a UTF-8 or UT
2b800 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68  F-16 string.  Th
2b810 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75  e _database_ rou
2b820 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20  tines return.** 
2b830 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
2b840 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72  e, the _table_ r
2b850 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74  outines return t
2b860 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61  he table name, a
2b870 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e  nd.** the origin
2b880 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  _ routines retur
2b890 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  n the column nam
2b8a0 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  e..** The return
2b8b0 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c  ed string is val
2b8c0 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72  id until the [pr
2b8d0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2b8e0 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a  ] is destroyed.*
2b8f0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
2b900 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20  _finalize()] or 
2b910 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69  until the same i
2b920 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65  nformation is re
2b930 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e  quested.** again
2b940 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20   in a different 
2b950 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  encoding..**.** 
2b960 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e  The names return
2b970 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69  ed are the origi
2b980 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e  nal un-aliased n
2b990 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64  ames of the.** d
2b9a0 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20  atabase, table, 
2b9b0 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  and column..**.*
2b9c0 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
2b9d0 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c  ment to the foll
2b9e0 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61  owing calls is a
2b9f0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2ba00 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20  ment]..** These 
2ba10 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e  functions return
2ba20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
2ba30 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d  ut the Nth colum
2ba40 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  n returned by.**
2ba50 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20   the statement, 
2ba60 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73  where N is the s
2ba70 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61  econd function a
2ba80 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49  rgument..**.** I
2ba90 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  f the Nth column
2baa0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
2bab0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e   statement is an
2bac0 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a   expression or.*
2bad0 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69  * subquery and i
2bae0 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76  s not a column v
2baf0 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f  alue, then all o
2bb00 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  f these function
2bb10 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c  s return.** NULL
2bb20 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
2bb30 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75   might also retu
2bb40 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d  rn NULL if a mem
2bb50 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
2bb60 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20  rror.** occurs. 
2bb70 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79   Otherwise, they
2bb80 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   return the name
2bb90 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64   of the attached
2bba0 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65   database, table
2bbb0 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74  .** and column t
2bbc0 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74  hat query result
2bbd0 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72   column was extr
2bbe0 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a  acted from..**.*
2bbf0 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74  * As with all ot
2bc00 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c  her SQLite APIs,
2bc10 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64   those postfixed
2bc20 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72   with "16" retur
2bc30 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f  n.** UTF-16 enco
2bc40 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65  ded strings, the
2bc50 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73   other functions
2bc60 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b   return UTF-8. {
2bc70 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  END}.**.** These
2bc80 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61   APIs are only a
2bc90 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20  vailable if the 
2bca0 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70  library was comp
2bcb0 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a  iled with the.**
2bcc0 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f   [SQLITE_ENABLE_
2bcd0 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d  COLUMN_METADATA]
2bce0 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20   C-preprocessor 
2bcf0 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a  symbol defined..
2bd00 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a  **.** {A13751}.*
2bd10 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65  * If two or more
2bd20 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e   threads call on
2bd30 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65  e or more of the
2bd40 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69  se routines agai
2bd50 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20  nst the same.** 
2bd60 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2bd70 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74  nt and column at
2bd80 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74   the same time t
2bd90 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20  hen the results 
2bda0 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64  are.** undefined
2bdb0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
2bdc0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34  TS:.**.** {H1374
2bdd0 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  1} The [sqlite3_
2bde0 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
2bdf0 6e 61 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72  name(S,N)] inter
2be00 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74  face returns eit
2be10 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  her.**          
2be20 74 68 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74  the UTF-8 zero-t
2be30 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f  erminated name o
2be40 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
2be50 72 6f 6d 20 77 68 69 63 68 20 74 68 65 0a 2a 2a  rom which the.**
2be60 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72 65            Nth re
2be70 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  sult column of t
2be80 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
2be90 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78 74  tement] S is ext
2bea0 72 61 63 74 65 64 2c 0a 2a 2a 20 20 20 20 20 20  racted,.**      
2beb0 20 20 20 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74      or NULL if t
2bec0 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
2bed0 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c 20   S is a general 
2bee0 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 20  expression.**   
2bef0 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e 61         or if una
2bf00 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ble to allocate 
2bf10 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 20  memory to store 
2bf20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  the name..**.** 
2bf30 7b 48 31 33 37 34 32 7d 20 54 68 65 20 5b 73 71  {H13742} The [sq
2bf40 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
2bf50 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 53 2c 4e  abase_name16(S,N
2bf60 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  )] interface ret
2bf70 75 72 6e 73 20 65 69 74 68 65 72 0a 2a 2a 20 20  urns either.**  
2bf80 20 20 20 20 20 20 20 20 74 68 65 20 55 54 46 2d          the UTF-
2bf90 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  16 native byte o
2bfa0 72 64 65 72 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  rder zero-termin
2bfb0 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65  ated name of the
2bfc0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20   database.**    
2bfd0 20 20 20 20 20 20 66 72 6f 6d 20 77 68 69 63 68        from which
2bfe0 20 74 68 65 20 4e 74 68 20 72 65 73 75 6c 74 20   the Nth result 
2bff0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70  column of the [p
2c000 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2c010 74 5d 20 53 20 69 73 0a 2a 2a 20 20 20 20 20 20  t] S is.**      
2c020 20 20 20 20 65 78 74 72 61 63 74 65 64 2c 20 6f      extracted, o
2c030 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74  r NULL if the Nt
2c040 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73  h column of S is
2c050 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65   a general expre
2c060 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ssion.**        
2c070 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74    or if unable t
2c080 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  o allocate memor
2c090 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e  y to store the n
2c0a0 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  ame..**.** {H137
2c0b0 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  43} The [sqlite3
2c0c0 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
2c0d0 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61  me(S,N)] interfa
2c0e0 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65  ce returns eithe
2c0f0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  r.**          th
2c100 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72  e UTF-8 zero-ter
2c110 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66 20  minated name of 
2c120 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77  the table from w
2c130 68 69 63 68 20 74 68 65 0a 2a 2a 20 20 20 20 20  hich the.**     
2c140 20 20 20 20 20 4e 74 68 20 72 65 73 75 6c 74 20       Nth result 
2c150 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70  column of the [p
2c160 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2c170 74 5d 20 53 20 69 73 20 65 78 74 72 61 63 74 65  t] S is extracte
2c180 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  d,.**          o
2c190 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74  r NULL if the Nt
2c1a0 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73  h column of S is
2c1b0 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65   a general expre
2c1c0 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ssion.**        
2c1d0 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74    or if unable t
2c1e0 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  o allocate memor
2c1f0 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e  y to store the n
2c200 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  ame..**.** {H137
2c210 34 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  44} The [sqlite3
2c220 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
2c230 6d 65 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72  me16(S,N)] inter
2c240 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74  face returns eit
2c250 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  her.**          
2c260 74 68 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76  the UTF-16 nativ
2c270 65 20 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72  e byte order zer
2c280 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d  o-terminated nam
2c290 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a  e of the table.*
2c2a0 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20  *          from 
2c2b0 77 68 69 63 68 20 74 68 65 20 4e 74 68 20 72 65  which the Nth re
2c2c0 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  sult column of t
2c2d0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
2c2e0 74 65 6d 65 6e 74 5d 20 53 20 69 73 0a 2a 2a 20  tement] S is.** 
2c2f0 20 20 20 20 20 20 20 20 20 65 78 74 72 61 63 74           extract
2c300 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  ed, or NULL if t
2c310 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
2c320 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c 20   S is a general 
2c330 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 20  expression.**   
2c340 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e 61         or if una
2c350 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ble to allocate 
2c360 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 20  memory to store 
2c370 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  the name..**.** 
2c380 7b 48 31 33 37 34 35 7d 20 54 68 65 20 5b 73 71  {H13745} The [sq
2c390 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
2c3a0 67 69 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20 69  gin_name(S,N)] i
2c3b0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
2c3c0 20 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20   either.**      
2c3d0 20 20 20 20 74 68 65 20 55 54 46 2d 38 20 7a 65      the UTF-8 ze
2c3e0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61  ro-terminated na
2c3f0 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
2c400 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63  column from whic
2c410 68 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  h the.**        
2c420 20 20 4e 74 68 20 72 65 73 75 6c 74 20 63 6f 6c    Nth result col
2c430 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 72 65 70  umn of the [prep
2c440 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
2c450 53 20 69 73 20 65 78 74 72 61 63 74 65 64 2c 0a  S is extracted,.
2c460 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 4e  **          or N
2c470 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 68 20 63  ULL if the Nth c
2c480 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 20 61 20  olumn of S is a 
2c490 67 65 6e 65 72 61 6c 20 65 78 70 72 65 73 73 69  general expressi
2c4a0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  on.**          o
2c4b0 72 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 61  r if unable to a
2c4c0 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74  llocate memory t
2c4d0 6f 20 73 74 6f 72 65 20 74 68 65 20 6e 61 6d 65  o store the name
2c4e0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34 36 7d  ..**.** {H13746}
2c4f0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f   The [sqlite3_co
2c500 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
2c510 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61  16(S,N)] interfa
2c520 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65  ce returns eithe
2c530 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  r.**          th
2c540 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20  e UTF-16 native 
2c550 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72 6f 2d  byte order zero-
2c560 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20  terminated name 
2c570 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20  of the table.** 
2c580 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
2c590 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20 4e  from which the N
2c5a0 74 68 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  th result column
2c5b0 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   of the.**      
2c5c0 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74      [prepared st
2c5d0 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78  atement] S is ex
2c5e0 74 72 61 63 74 65 64 2c 20 6f 72 20 4e 55 4c 4c  tracted, or NULL
2c5f0 20 69 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75   if the Nth colu
2c600 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  mn.**          o
2c610 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c  f S is a general
2c620 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 69   expression or i
2c630 66 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f  f unable to allo
2c640 63 61 74 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20  cate memory.**  
2c650 20 20 20 20 20 20 20 20 74 6f 20 73 74 6f 72 65          to store
2c660 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a   the name..**.**
2c670 20 7b 48 31 33 37 34 38 7d 20 54 68 65 20 72 65   {H13748} The re
2c680 74 75 72 6e 20 76 61 6c 75 65 73 20 66 72 6f 6d  turn values from
2c690 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
2c6a0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
2c6b0 61 62 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c  abase_name | col
2c6c0 75 6d 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74  umn metadata int
2c6d0 65 72 66 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20  erfaces].**     
2c6e0 20 20 20 20 20 61 72 65 20 76 61 6c 69 64 20 66       are valid f
2c6f0 6f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20  or the lifetime 
2c700 6f 66 20 74 68 65 20 5b 70 72 65 70 61 72 65 64  of the [prepared
2c710 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 20   statement].**  
2c720 20 20 20 20 20 20 20 20 6f 72 20 75 6e 74 69 6c          or until
2c730 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73   the encoding is
2c740 20 63 68 61 6e 67 65 64 20 62 79 20 61 6e 6f 74   changed by anot
2c750 68 65 72 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20  her metadata.** 
2c760 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61           interfa
2c770 63 65 20 63 61 6c 6c 20 66 6f 72 20 74 68 65 20  ce call for the 
2c780 73 61 6d 65 20 70 72 65 70 61 72 65 64 20 73 74  same prepared st
2c790 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75  atement and colu
2c7a0 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50  mn..**.** ASSUMP
2c7b0 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31  TIONS:.**.** {A1
2c7c0 33 37 35 31 7d 20 49 66 20 74 77 6f 20 6f 72 20  3751} If two or 
2c7d0 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c  more threads cal
2c7e0 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  l one or more.**
2c7f0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
2c800 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
2c810 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e  se_name | column
2c820 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 66   metadata interf
2c830 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20  aces].**        
2c840 20 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b    for the same [
2c850 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2c860 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63  nt] and result c
2c870 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20  olumn.**        
2c880 20 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69    at the same ti
2c890 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  me then the resu
2c8a0 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65  lts are undefine
2c8b0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
2c8c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
2c8d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
2c8e0 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65  base_name(sqlite
2c8f0 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51  3_stmt*,int);.SQ
2c900 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
2c910 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
2c920 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
2c930 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  e16(sqlite3_stmt
2c940 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  *,int);.SQLITE_A
2c950 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
2c960 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
2c970 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  ble_name(sqlite3
2c980 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c  _stmt*,int);.SQL
2c990 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
2c9a0 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
2c9b0 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28  mn_table_name16(
2c9c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
2c9d0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  t);.SQLITE_API c
2c9e0 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
2c9f0 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
2ca00 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
2ca10 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  mt*,int);.SQLITE
2ca20 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
2ca30 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
2ca40 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71  origin_name16(sq
2ca50 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
2ca60 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2ca70 46 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74 61  F: Declared Data
2ca80 74 79 70 65 20 4f 66 20 41 20 51 75 65 72 79 20  type Of A Query 
2ca90 52 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20  Result {H13760} 
2caa0 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10700>.**.** T
2cab0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
2cac0 65 72 20 69 73 20 61 20 5b 70 72 65 70 61 72 65  er is a [prepare
2cad0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a  d statement]..**
2cae0 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65   If this stateme
2caf0 6e 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d  nt is a [SELECT]
2cb00 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74   statement and t
2cb10 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
2cb20 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64   the.** returned
2cb30 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
2cb40 68 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20  hat [SELECT] is 
2cb50 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28  a table column (
2cb60 6e 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73  not an.** expres
2cb70 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79  sion or subquery
2cb80 29 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61  ) then the decla
2cb90 72 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20  red type of the 
2cba0 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  table.** column 
2cbb0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  is returned.  If
2cbc0 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
2cbd0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
2cbe0 74 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65  t is an.** expre
2cbf0 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72  ssion or subquer
2cc00 79 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70  y, then a NULL p
2cc10 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e  ointer is return
2cc20 65 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  ed..** The retur
2cc30 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c  ned string is al
2cc40 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64  ways UTF-8 encod
2cc50 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20  ed. {END}.**.** 
2cc60 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76  For example, giv
2cc70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
2cc80 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52  schema:.**.** CR
2cc90 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 31  EATE TABLE t1(c1
2cca0 20 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a   VARIANT);.**.**
2ccb0 20 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69   and the followi
2ccc0 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  ng statement to 
2ccd0 62 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a  be compiled:.**.
2cce0 2a 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31  ** SELECT c1 + 1
2ccf0 2c 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a  , c1 FROM t1;.**
2cd00 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
2cd10 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68   would return th
2cd20 65 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e  e string "VARIAN
2cd30 54 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  T" for the secon
2cd40 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75  d result.** colu
2cd50 6d 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61  mn (i==1), and a
2cd60 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f   NULL pointer fo
2cd70 72 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75  r the first resu
2cd80 6c 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29  lt column (i==0)
2cd90 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75  ..**.** SQLite u
2cda0 73 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d  ses dynamic run-
2cdb0 74 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f  time typing.  So
2cdc0 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20   just because a 
2cdd0 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63  column.** is dec
2cde0 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e  lared to contain
2cdf0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79   a particular ty
2ce00 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e  pe does not mean
2ce10 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74   that the.** dat
2ce20 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74  a stored in that
2ce30 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68   column is of th
2ce40 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e  e declared type.
2ce50 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73    SQLite is.** s
2ce60 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62  trongly typed, b
2ce70 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73  ut the typing is
2ce80 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61   dynamic not sta
2ce90 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73  tic.  Type.** is
2cea0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2ceb0 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75   individual valu
2cec0 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65  es, not with the
2ced0 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75   containers.** u
2cee0 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73  sed to hold thos
2cef0 65 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  e values..**.** 
2cf00 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
2cf10 2a 20 7b 48 31 33 37 36 31 7d 20 20 41 20 73 75  * {H13761}  A su
2cf20 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
2cf30 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
2cf40 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d 20  _decltype(S,N)] 
2cf50 72 65 74 75 72 6e 73 20 61 0a 2a 2a 20 20 20 20  returns a.**    
2cf60 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72 6d         zero-term
2cf70 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72  inated UTF-8 str
2cf80 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ing containing t
2cf90 68 65 20 64 65 63 6c 61 72 65 64 20 64 61 74 61  he declared data
2cfa0 74 79 70 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  type.**         
2cfb0 20 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63    of the table c
2cfc0 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61  olumn that appea
2cfd0 72 73 20 61 73 20 74 68 65 20 4e 74 68 20 63 6f  rs as the Nth co
2cfe0 6c 75 6d 6e 20 28 6e 75 6d 62 65 72 65 64 0a 2a  lumn (numbered.*
2cff0 2a 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d  *           from
2d000 20 30 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c   0) of the resul
2d010 74 20 73 65 74 20 74 6f 20 74 68 65 20 5b 70 72  t set to the [pr
2d020 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2d030 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  ] S..**.** {H137
2d040 36 32 7d 20 20 41 20 73 75 63 63 65 73 73 66 75  62}  A successfu
2d050 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
2d060 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
2d070 70 65 31 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20  pe16(S,N)].**   
2d080 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20          returns 
2d090 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  a zero-terminate
2d0a0 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20  d UTF-16 native 
2d0b0 62 79 74 65 20 6f 72 64 65 72 20 73 74 72 69 6e  byte order strin
2d0c0 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63  g.**           c
2d0d0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 64 65  ontaining the de
2d0e0 63 6c 61 72 65 64 20 64 61 74 61 74 79 70 65 20  clared datatype 
2d0f0 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
2d100 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72 73  umn that appears
2d110 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 73  .**           as
2d120 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
2d130 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 30  (numbered from 0
2d140 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  ) of the result 
2d150 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20  set to the.**   
2d160 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65          [prepare
2d170 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 2e 0a  d statement] S..
2d180 2a 2a 0a 2a 2a 20 7b 48 31 33 37 36 33 7d 20 20  **.** {H13763}  
2d190 49 66 20 4e 20 69 73 20 6c 65 73 73 20 74 68 61  If N is less tha
2d1a0 6e 20 30 20 6f 72 20 4e 20 69 73 20 67 72 65 61  n 0 or N is grea
2d1b0 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
2d1c0 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  l to.**         
2d1d0 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20    the number of 
2d1e0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 5b  columns in the [
2d1f0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2d200 6e 74 5d 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20  nt] S,.**       
2d210 20 20 20 20 6f 72 20 69 66 20 74 68 65 20 4e 74      or if the Nt
2d220 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73  h column of S is
2d230 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f   an expression o
2d240 72 20 73 75 62 71 75 65 72 79 20 72 61 74 68 65  r subquery rathe
2d250 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  r.**           t
2d260 68 61 6e 20 61 20 74 61 62 6c 65 20 63 6f 6c 75  han a table colu
2d270 6d 6e 2c 20 6f 72 20 69 66 20 61 20 6d 65 6d 6f  mn, or if a memo
2d280 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
2d290 69 6c 75 72 65 0a 2a 2a 20 20 20 20 20 20 20 20  ilure.**        
2d2a0 20 20 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67     occurs during
2d2b0 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72   encoding conver
2d2c0 73 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a 2a 20 20  sions, then.**  
2d2d0 20 20 20 20 20 20 20 20 20 63 61 6c 6c 73 20 74           calls t
2d2e0 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  o [sqlite3_colum
2d2f0 6e 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d  n_decltype(S,N)]
2d300 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
2d310 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
2d320 5f 64 65 63 6c 74 79 70 65 31 36 28 53 2c 4e 29  _decltype16(S,N)
2d330 5d 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a  ] return NULL..*
2d340 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
2d350 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
2d360 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
2d370 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2d380 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
2d390 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
2d3a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
2d3b0 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ype16(sqlite3_st
2d3c0 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  mt*,int);../*.**
2d3d0 20 43 41 50 49 33 52 45 46 3a 20 45 76 61 6c 75   CAPI3REF: Evalu
2d3e0 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61 74 65  ate An SQL State
2d3f0 6d 65 6e 74 20 7b 48 31 33 32 30 30 7d 20 3c 53  ment {H13200} <S
2d400 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 66 74  10000>.**.** Aft
2d410 65 72 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  er a [prepared s
2d420 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20 62 65  tatement] has be
2d430 65 6e 20 70 72 65 70 61 72 65 64 20 75 73 69 6e  en prepared usin
2d440 67 20 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c  g either.** [sql
2d450 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
2d460 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70  )] or [sqlite3_p
2d470 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f  repare16_v2()] o
2d480 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 65 67  r one of the leg
2d490 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  acy.** interface
2d4a0 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  s [sqlite3_prepa
2d4b0 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  re()] or [sqlite
2d4c0 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2c 20  3_prepare16()], 
2d4d0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  this function.**
2d4e0 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
2d4f0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65  one or more time
2d500 73 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68  s to evaluate th
2d510 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  e statement..**.
2d520 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f  ** The details o
2d530 66 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f  f the behavior o
2d540 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74  f the sqlite3_st
2d550 65 70 28 29 20 69 6e 74 65 72 66 61 63 65 20 64  ep() interface d
2d560 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77 68 65 74  epend.** on whet
2d570 68 65 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e  her the statemen
2d580 74 20 77 61 73 20 70 72 65 70 61 72 65 64 20 75  t was prepared u
2d590 73 69 6e 67 20 74 68 65 20 6e 65 77 65 72 20 22  sing the newer "
2d5a0 76 32 22 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a  v2" interface.**
2d5b0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
2d5c0 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c  e_v2()] and [sql
2d5d0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
2d5e0 32 28 29 5d 20 6f 72 20 74 68 65 20 6f 6c 64 65  2()] or the olde
2d5f0 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65  r legacy.** inte
2d600 72 66 61 63 65 20 5b 73 71 6c 69 74 65 33 5f 70  rface [sqlite3_p
2d610 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73  repare()] and [s
2d620 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
2d630 28 29 5d 2e 20 20 54 68 65 20 75 73 65 20 6f 66  ()].  The use of
2d640 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22 76 32 22   the.** new "v2"
2d650 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65   interface is re
2d660 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6e 65  commended for ne
2d670 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 62  w applications b
2d680 75 74 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a  ut the legacy.**
2d690 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20   interface will 
2d6a0 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 20 73  continue to be s
2d6b0 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  upported..**.** 
2d6c0 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e  In the legacy in
2d6d0 74 65 72 66 61 63 65 2c 20 74 68 65 20 72 65 74  terface, the ret
2d6e0 75 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c 20 62  urn value will b
2d6f0 65 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45  e either [SQLITE
2d700 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49  _BUSY],.** [SQLI
2d710 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54  TE_DONE], [SQLIT
2d720 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54 45 5f  E_ROW], [SQLITE_
2d730 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51 4c 49  ERROR], or [SQLI
2d740 54 45 5f 4d 49 53 55 53 45 5d 2e 0a 2a 2a 20 57  TE_MISUSE]..** W
2d750 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e 74  ith the "v2" int
2d760 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66 20 74  erface, any of t
2d770 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75 6c 74  he other [result
2d780 20 63 6f 64 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65   codes] or.** [e
2d790 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
2d7a0 6f 64 65 73 5d 20 6d 69 67 68 74 20 62 65 20 72  odes] might be r
2d7b0 65 74 75 72 6e 65 64 20 61 73 20 77 65 6c 6c 2e  eturned as well.
2d7c0 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42  .**.** [SQLITE_B
2d7d0 55 53 59 5d 20 6d 65 61 6e 73 20 74 68 61 74 20  USY] means that 
2d7e0 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67  the database eng
2d7f0 69 6e 65 20 77 61 73 20 75 6e 61 62 6c 65 20 74  ine was unable t
2d800 6f 20 61 63 71 75 69 72 65 20 74 68 65 0a 2a 2a  o acquire the.**
2d810 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 20   database locks 
2d820 69 74 20 6e 65 65 64 73 20 74 6f 20 64 6f 20 69  it needs to do i
2d830 74 73 20 6a 6f 62 2e 20 20 49 66 20 74 68 65 20  ts job.  If the 
2d840 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b  statement is a [
2d850 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63  COMMIT].** or oc
2d860 63 75 72 73 20 6f 75 74 73 69 64 65 20 6f 66 20  curs outside of 
2d870 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e  an explicit tran
2d880 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 79 6f  saction, then yo
2d890 75 20 63 61 6e 20 72 65 74 72 79 20 74 68 65 0a  u can retry the.
2d8a0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49  ** statement.  I
2d8b0 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
2d8c0 69 73 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49 54  is not a [COMMIT
2d8d0 5d 20 61 6e 64 20 6f 63 63 75 72 73 20 77 69 74  ] and occurs wit
2d8e0 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69 63 69  hin a.** explici
2d8f0 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68  t transaction th
2d900 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 72 6f  en you should ro
2d910 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73  llback the trans
2d920 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a  action before.**
2d930 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a   continuing..**.
2d940 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  ** [SQLITE_DONE]
2d950 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2d960 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66 69  statement has fi
2d970 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67  nished executing
2d980 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79  .** successfully
2d990 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
2d9a0 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ) should not be 
2d9b0 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 20  called again on 
2d9c0 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a 2a 20  this virtual.** 
2d9d0 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 74 20  machine without 
2d9e0 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 5b 73  first calling [s
2d9f0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
2da00 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72  to reset the vir
2da10 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20  tual.** machine 
2da20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74  back to its init
2da30 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  ial state..**.**
2da40 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   If the SQL stat
2da50 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 65 63  ement being exec
2da60 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 6e 79  uted returns any
2da70 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53 51 4c   data, then [SQL
2da80 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20 72  ITE_ROW].** is r
2da90 65 74 75 72 6e 65 64 20 65 61 63 68 20 74 69 6d  eturned each tim
2daa0 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 20 64  e a new row of d
2dab0 61 74 61 20 69 73 20 72 65 61 64 79 20 66 6f 72  ata is ready for
2dac0 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79 20 74   processing by t
2dad0 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 68  he.** caller. Th
2dae0 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 20  e values may be 
2daf0 61 63 63 65 73 73 65 64 20 75 73 69 6e 67 20 74  accessed using t
2db00 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73  he [column acces
2db10 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a  s functions]..**
2db20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
2db30 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20  is called again 
2db40 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20  to retrieve the 
2db50 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 74 61  next row of data
2db60 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ..**.** [SQLITE_
2db70 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74 68 61  ERROR] means tha
2db80 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72  t a run-time err
2db90 6f 72 20 28 73 75 63 68 20 61 73 20 61 20 63 6f  or (such as a co
2dba0 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69 6f 6c  nstraint.** viol
2dbb0 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63 75 72  ation) has occur
2dbc0 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f 73 74  red.  sqlite3_st
2dbd0 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20  ep() should not 
2dbe0 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20  be called again 
2dbf0 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 4d 6f  on.** the VM. Mo
2dc00 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d  re information m
2dc10 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79 20 63  ay be found by c
2dc20 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
2dc30 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 57 69  errmsg()]..** Wi
2dc40 74 68 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e  th the legacy in
2dc50 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72 65 20  terface, a more 
2dc60 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63  specific error c
2dc70 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  ode (for example
2dc80 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54  ,.** [SQLITE_INT
2dc90 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49 54 45  ERRUPT], [SQLITE
2dca0 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c 49 54  _SCHEMA], [SQLIT
2dcb0 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e 64 20  E_CORRUPT], and 
2dcc0 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63 61 6e  so forth).** can
2dcd0 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20   be obtained by 
2dce0 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33  calling [sqlite3
2dcf0 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74 68 65  _reset()] on the
2dd00 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74  .** [prepared st
2dd10 61 74 65 6d 65 6e 74 5d 2e 20 20 49 6e 20 74 68  atement].  In th
2dd20 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  e "v2" interface
2dd30 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 70  ,.** the more sp
2dd40 65 63 69 66 69 63 20 65 72 72 6f 72 20 63 6f 64  ecific error cod
2dd50 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 64 69  e is returned di
2dd60 72 65 63 74 6c 79 20 62 79 20 73 71 6c 69 74 65  rectly by sqlite
2dd70 33 5f 73 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20  3_step()..**.** 
2dd80 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20  [SQLITE_MISUSE] 
2dd90 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74  means that the t
2dda0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20  his routine was 
2ddb0 63 61 6c 6c 65 64 20 69 6e 61 70 70 72 6f 70 72  called inappropr
2ddc0 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 65 72 68 61  iately..** Perha
2ddd0 70 73 20 69 74 20 77 61 73 20 63 61 6c 6c 65 64  ps it was called
2dde0 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20   on a [prepared 
2ddf0 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20  statement] that 
2de00 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62  has.** already b
2de10 65 65 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  een [sqlite3_fin
2de20 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65  alize | finalize
2de30 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 20 74 68 61  d] or on one tha
2de40 74 20 68 61 64 0a 2a 2a 20 70 72 65 76 69 6f 75  t had.** previou
2de50 73 6c 79 20 72 65 74 75 72 6e 65 64 20 5b 53 51  sly returned [SQ
2de60 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6f 72 20 5b  LITE_ERROR] or [
2de70 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f  SQLITE_DONE].  O
2de80 72 20 69 74 20 63 6f 75 6c 64 0a 2a 2a 20 62 65  r it could.** be
2de90 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 74   the case that t
2dea0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
2deb0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 62   connection is b
2dec0 65 69 6e 67 20 75 73 65 64 20 62 79 20 74 77 6f  eing used by two
2ded0 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 68 72 65   or.** more thre
2dee0 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20  ads at the same 
2def0 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 6d 65 2e 0a  moment in time..
2df00 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20 49  **.** <b>Goofy I
2df10 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74 3a 3c  nterface Alert:<
2df20 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67 61 63  /b> In the legac
2df30 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65  y interface, the
2df40 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a   sqlite3_step().
2df50 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20 72 65  ** API always re
2df60 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69 63 20  turns a generic 
2df70 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53 51 4c  error code, [SQL
2df80 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c 6c  ITE_ERROR], foll
2df90 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a 20 65 72 72  owing any.** err
2dfa0 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53  or other than [S
2dfb0 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e 64 20  QLITE_BUSY] and 
2dfc0 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e  [SQLITE_MISUSE].
2dfd0 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c 6c 0a    You must call.
2dfe0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  ** [sqlite3_rese
2dff0 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  t()] or [sqlite3
2e000 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 6e 20  _finalize()] in 
2e010 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20 6f 6e  order to find on
2e020 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 70 65 63  e of the.** spec
2e030 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 65  ific [error code
2e040 73 5d 20 74 68 61 74 20 62 65 74 74 65 72 20 64  s] that better d
2e050 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 72  escribes the err
2e060 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d 69 74 20  or..** We admit 
2e070 74 68 61 74 20 74 68 69 73 20 69 73 20 61 20 67  that this is a g
2e080 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20 20 54 68  oofy design.  Th
2e090 65 20 70 72 6f 62 6c 65 6d 20 68 61 73 20 62 65  e problem has be
2e0a0 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77 69 74 68  en fixed.** with
2e0b0 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66   the "v2" interf
2e0c0 61 63 65 2e 20 20 49 66 20 79 6f 75 20 70 72 65  ace.  If you pre
2e0d0 70 61 72 65 20 61 6c 6c 20 6f 66 20 79 6f 75 72  pare all of your
2e0e0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a   SQL statements.
2e0f0 2a 2a 20 75 73 69 6e 67 20 65 69 74 68 65 72 20  ** using either 
2e100 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
2e110 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  _v2()] or [sqlit
2e120 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
2e130 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66  )] instead.** of
2e140 20 74 68 65 20 6c 65 67 61 63 79 20 5b 73 71 6c   the legacy [sql
2e150 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
2e160 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  and [sqlite3_pre
2e170 70 61 72 65 31 36 28 29 5d 20 69 6e 74 65 72 66  pare16()] interf
2e180 61 63 65 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  aces,.** then th
2e190 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20  e more specific 
2e1a0 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 61 72  [error codes] ar
2e1b0 65 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63  e returned direc
2e1c0 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65  tly.** by sqlite
2e1d0 33 5f 73 74 65 70 28 29 2e 20 20 54 68 65 20 75  3_step().  The u
2e1e0 73 65 20 6f 66 20 74 68 65 20 22 76 32 22 20 69  se of the "v2" i
2e1f0 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63 6f  nterface is reco
2e200 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  mmended..**.** I
2e210 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
2e220 20 7b 48 31 33 32 30 32 7d 20 20 49 66 20 74 68   {H13202}  If th
2e230 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
2e240 65 6d 65 6e 74 5d 20 53 20 69 73 20 72 65 61 64  ement] S is read
2e250 79 20 74 6f 20 62 65 20 72 75 6e 2c 20 74 68 65  y to be run, the
2e260 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b  n.**           [
2e270 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d  sqlite3_step(S)]
2e280 20 61 64 76 61 6e 63 65 73 20 74 68 61 74 20 70   advances that p
2e290 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2e2a0 74 20 75 6e 74 69 6c 0a 2a 2a 20 20 20 20 20 20  t until.**      
2e2b0 20 20 20 20 20 63 6f 6d 70 6c 65 74 69 6f 6e 20       completion 
2e2c0 6f 72 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72  or until it is r
2e2d0 65 61 64 79 20 74 6f 20 72 65 74 75 72 6e 20 61  eady to return a
2e2e0 6e 6f 74 68 65 72 20 72 6f 77 20 6f 66 20 74 68  nother row of th
2e2f0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72  e.**           r
2e300 65 73 75 6c 74 20 73 65 74 2c 20 6f 72 20 75 6e  esult set, or un
2e310 74 69 6c 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f  til an [sqlite3_
2e320 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 65  interrupt | inte
2e330 72 72 75 70 74 5d 0a 2a 2a 20 20 20 20 20 20 20  rrupt].**       
2e340 20 20 20 20 6f 72 20 61 20 72 75 6e 2d 74 69 6d      or a run-tim
2e350 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  e error occurs..
2e360 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30 34 7d 20 20  **.** {H15304}  
2e370 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b  When a call to [
2e380 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d  sqlite3_step(S)]
2e390 20 63 61 75 73 65 73 20 74 68 65 20 5b 70 72 65   causes the [pre
2e3a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
2e3b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 53 20  .**           S 
2e3c0 74 6f 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65  to run to comple
2e3d0 74 69 6f 6e 2c 20 74 68 65 20 66 75 6e 63 74 69  tion, the functi
2e3e0 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  on returns [SQLI
2e3f0 54 45 5f 44 4f 4e 45 5d 2e 0a 2a 2a 0a 2a 2a 20  TE_DONE]..**.** 
2e400 7b 48 31 35 33 30 36 7d 20 20 57 68 65 6e 20 61  {H15306}  When a
2e410 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
2e420 33 5f 73 74 65 70 28 53 29 5d 20 73 74 6f 70 73  3_step(S)] stops
2e430 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 72   because it is r
2e440 65 61 64 79 20 74 6f 0a 2a 2a 20 20 20 20 20 20  eady to.**      
2e450 20 20 20 20 20 72 65 74 75 72 6e 20 61 6e 6f 74       return anot
2e460 68 65 72 20 72 6f 77 20 6f 66 20 74 68 65 20 72  her row of the r
2e470 65 73 75 6c 74 20 73 65 74 2c 20 69 74 20 72 65  esult set, it re
2e480 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f  turns [SQLITE_RO
2e490 57 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30  W]..**.** {H1530
2e4a0 38 7d 20 20 49 66 20 61 20 63 61 6c 6c 20 74 6f  8}  If a call to
2e4b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
2e4c0 29 5d 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 6e  )] encounters an
2e4d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73  .**           [s
2e4e0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
2e4f0 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 20 6f 72   | interrupt] or
2e500 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f   a run-time erro
2e510 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  r,.**           
2e520 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20 61 70  it returns an ap
2e530 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20  propriate error 
2e540 63 6f 64 65 20 74 68 61 74 20 69 73 20 6e 6f 74  code that is not
2e550 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20   one of.**      
2e560 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d       [SQLITE_OK]
2e570 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20  , [SQLITE_ROW], 
2e580 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  or [SQLITE_DONE]
2e590 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 31 30 7d  ..**.** {H15310}
2e5a0 20 20 49 66 20 61 6e 20 5b 73 71 6c 69 74 65 33    If an [sqlite3
2e5b0 5f 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74  _interrupt | int
2e5c0 65 72 72 75 70 74 5d 20 6f 72 20 61 20 72 75 6e  errupt] or a run
2e5d0 2d 74 69 6d 65 20 65 72 72 6f 72 0a 2a 2a 20 20  -time error.**  
2e5e0 20 20 20 20 20 20 20 20 20 6f 63 63 75 72 73 20           occurs 
2e5f0 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f  during a call to
2e600 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
2e610 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  )].**           
2e620 66 6f 72 20 61 20 5b 70 72 65 70 61 72 65 64 20  for a [prepared 
2e630 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 63 72 65  statement] S cre
2e640 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20  ated using.**   
2e650 20 20 20 20 20 20 20 20 6c 65 67 61 63 79 20 69          legacy i
2e660 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69 74  nterfaces [sqlit
2e670 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72  e3_prepare()] or
2e680 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73  .**           [s
2e690 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
2e6a0 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 66 75  ()], then the fu
2e6b0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 65  nction returns e
2e6c0 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20  ither.**        
2e6d0 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52     [SQLITE_ERROR
2e6e0 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  ], [SQLITE_BUSY]
2e6f0 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53  , or [SQLITE_MIS
2e700 55 53 45 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  USE]..*/.SQLITE_
2e710 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2e720 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d  step(sqlite3_stm
2e730 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t*);../*.** CAPI
2e740 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20  3REF: Number of 
2e750 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 73  columns in a res
2e760 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 7d  ult set {H13770}
2e770 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
2e780 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  Returns the numb
2e790 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 20  er of values in 
2e7a0 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
2e7b0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
2e7c0 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  t..**.** INVARIA
2e7d0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  NTS:.**.** {H137
2e7e0 37 31 7d 20 20 41 66 74 65 72 20 61 20 63 61 6c  71}  After a cal
2e7f0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  l to [sqlite3_st
2e800 65 70 28 53 29 5d 20 74 68 61 74 20 72 65 74 75  ep(S)] that retu
2e810 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d  rns [SQLITE_ROW]
2e820 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  ,.**           t
2e830 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 61 74 61  he [sqlite3_data
2e840 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f 75 74 69  _count(S)] routi
2e850 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 74  ne will return t
2e860 68 65 20 73 61 6d 65 20 76 61 6c 75 65 0a 2a 2a  he same value.**
2e870 20 20 20 20 20 20 20 20 20 20 20 61 73 20 74 68             as th
2e880 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e [sqlite3_colum
2e890 6e 5f 63 6f 75 6e 74 28 53 29 5d 20 66 75 6e 63  n_count(S)] func
2e8a0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  tion..**.** {H13
2e8b0 37 37 32 7d 20 20 41 66 74 65 72 20 5b 73 71 6c  772}  After [sql
2e8c0 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61  ite3_step(S)] ha
2e8d0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 79 20 76  s returned any v
2e8e0 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 0a  alue other than.
2e8f0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 53 51  **           [SQ
2e900 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 62 65 66  LITE_ROW] or bef
2e910 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ore [sqlite3_ste
2e920 70 28 53 29 5d 20 68 61 73 20 62 65 65 6e 20 63  p(S)] has been c
2e930 61 6c 6c 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20  alled on the.** 
2e940 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
2e950 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 66  red statement] f
2e960 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  or the first tim
2e970 65 20 73 69 6e 63 65 20 69 74 20 77 61 73 0a 2a  e since it was.*
2e980 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  *           [sql
2e990 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70  ite3_prepare | p
2e9a0 72 65 70 61 72 65 64 5d 20 6f 72 20 5b 73 71 6c  repared] or [sql
2e9b0 69 74 65 33 5f 72 65 73 65 74 20 7c 20 72 65 73  ite3_reset | res
2e9c0 65 74 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  et],.**         
2e9d0 20 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64    the [sqlite3_d
2e9e0 61 74 61 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f  ata_count(S)] ro
2e9f0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a 65  utine returns ze
2ea00 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ro..*/.SQLITE_AP
2ea10 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 61  I int sqlite3_da
2ea20 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  ta_count(sqlite3
2ea30 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a  _stmt *pStmt);..
2ea40 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2ea50 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74 61  Fundamental Data
2ea60 74 79 70 65 73 20 7b 48 31 30 32 36 35 7d 20 3c  types {H10265} <
2ea70 53 31 30 31 31 30 3e 3c 53 31 30 31 32 30 3e 0a  S10110><S10120>.
2ea80 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c  ** KEYWORDS: SQL
2ea90 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20 7b  ITE_TEXT.**.** {
2eaa0 48 31 30 32 36 36 7d 20 45 76 65 72 79 20 76 61  H10266} Every va
2eab0 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 68 61  lue in SQLite ha
2eac0 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 66 75  s one of five fu
2ead0 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 74 79  ndamental dataty
2eae0 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  pes:.**.** <ul>.
2eaf0 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 73  ** <li> 64-bit s
2eb00 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
2eb10 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 45 45   <li> 64-bit IEE
2eb20 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  E floating point
2eb30 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e 20   number.** <li> 
2eb40 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 42  string.** <li> B
2eb50 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c 4c  LOB.** <li> NULL
2eb60 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d 0a  .** </ul> {END}.
2eb70 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73  **.** These cons
2eb80 74 61 6e 74 73 20 61 72 65 20 63 6f 64 65 73 20  tants are codes 
2eb90 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 6f 73  for each of thos
2eba0 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e  e types..**.** N
2ebb0 6f 74 65 20 74 68 61 74 20 74 68 65 20 53 51 4c  ote that the SQL
2ebc0 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 74 61 6e  ITE_TEXT constan
2ebd0 74 20 77 61 73 20 61 6c 73 6f 20 75 73 65 64 20  t was also used 
2ebe0 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  in SQLite versio
2ebf0 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d  n 2.** for a com
2ec00 70 6c 65 74 65 6c 79 20 64 69 66 66 65 72 65 6e  pletely differen
2ec10 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66 74  t meaning.  Soft
2ec20 77 61 72 65 20 74 68 61 74 20 6c 69 6e 6b 73 20  ware that links 
2ec30 61 67 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a 20  against both.** 
2ec40 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32  SQLite version 2
2ec50 20 61 6e 64 20 53 51 4c 69 74 65 20 76 65 72 73   and SQLite vers
2ec60 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 75 73 65  ion 3 should use
2ec70 20 53 51 4c 49 54 45 33 5f 54 45 58 54 2c 20 6e   SQLITE3_TEXT, n
2ec80 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 58  ot.** SQLITE_TEX
2ec90 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  T..*/.#define SQ
2eca0 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 20 31 0a  LITE_INTEGER  1.
2ecb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
2ecc0 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 66 69 6e  LOAT    2.#defin
2ecd0 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20 20  e SQLITE_BLOB   
2ece0 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    4.#define SQLI
2ecf0 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23 69  TE_NULL     5.#i
2ed00 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54  fdef SQLITE_TEXT
2ed10 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
2ed20 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 64 65 66  TEXT.#else.# def
2ed30 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58 54 20  ine SQLITE_TEXT 
2ed40 20 20 20 20 33 0a 23 65 6e 64 69 66 0a 23 64 65      3.#endif.#de
2ed50 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 54 45 58  fine SQLITE3_TEX
2ed60 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43  T     3../*.** C
2ed70 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20  API3REF: Result 
2ed80 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 20 51 75  Values From A Qu
2ed90 65 72 79 20 7b 48 31 33 38 30 30 7d 20 3c 53 31  ery {H13800} <S1
2eda0 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0700>.** KEYWORD
2edb0 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 63 65 73  S: {column acces
2edc0 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a  s functions}.**.
2edd0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
2ede0 73 20 66 6f 72 6d 20 74 68 65 20 22 72 65 73 75  s form the "resu
2edf0 6c 74 20 73 65 74 20 71 75 65 72 79 22 20 69 6e  lt set query" in
2ee00 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54  terface..**.** T
2ee10 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65  hese routines re
2ee20 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  turn information
2ee30 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20   about a single 
2ee40 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 75  column of the cu
2ee50 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 6c 74 20  rrent.** result 
2ee60 72 6f 77 20 6f 66 20 61 20 71 75 65 72 79 2e 20  row of a query. 
2ee70 20 49 6e 20 65 76 65 72 79 20 63 61 73 65 20 74   In every case t
2ee80 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
2ee90 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a  t is a pointer.*
2eea0 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 70 61 72  * to the [prepar
2eeb0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68  ed statement] th
2eec0 61 74 20 69 73 20 62 65 69 6e 67 20 65 76 61 6c  at is being eval
2eed0 75 61 74 65 64 20 28 74 68 65 20 5b 73 71 6c 69  uated (the [sqli
2eee0 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a 20 74 68  te3_stmt*].** th
2eef0 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20  at was returned 
2ef00 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70 72  from [sqlite3_pr
2ef10 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 6f  epare_v2()] or o
2ef20 6e 65 20 6f 66 20 69 74 73 20 76 61 72 69 61 6e  ne of its varian
2ef30 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73  ts).** and the s
2ef40 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
2ef50 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
2ef60 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 68  he column for wh
2ef70 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  ich information.
2ef80 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74  ** should be ret
2ef90 75 72 6e 65 64 2e 20 20 54 68 65 20 6c 65 66 74  urned.  The left
2efa0 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  most column of t
2efb0 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 68 61  he result set ha
2efc0 73 20 74 68 65 20 69 6e 64 65 78 20 30 2e 0a 2a  s the index 0..*
2efd0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20  *.** If the SQL 
2efe0 73 74 61 74 65 6d 65 6e 74 20 64 6f 65 73 20 6e  statement does n
2eff0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  ot currently poi
2f000 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f  nt to a valid ro
2f010 77 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20  w, or if the.** 
2f020 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 73 20  column index is 
2f030 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68  out of range, th
2f040 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65  e result is unde
2f050 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20  fined..** These 
2f060 72 6f 75 74 69 6e 65 73 20 6d 61 79 20 6f 6e 6c  routines may onl
2f070 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  y be called when
2f080 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
2f090 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c   call to.** [sql
2f0a0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61 73  ite3_step()] has
2f0b0 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54   returned [SQLIT
2f0c0 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 69 74 68  E_ROW] and neith
2f0d0 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72  er.** [sqlite3_r
2f0e0 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b 73 71 6c  eset()] nor [sql
2f0f0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
2f100 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65   have been calle
2f110 64 20 73 75 62 73 65 71 75 65 6e 74 6c 79 2e 0a  d subsequently..
2f120 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65  ** If any of the
2f130 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
2f140 63 61 6c 6c 65 64 20 61 66 74 65 72 20 5b 73 71  called after [sq
2f150 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
2f160 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69  r.** [sqlite3_fi
2f170 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74  nalize()] or aft
2f180 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  er [sqlite3_step
2f190 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64  ()] has returned
2f1a0 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74  .** something ot
2f1b0 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45  her than [SQLITE
2f1c0 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65 73 75 6c  _ROW], the resul
2f1d0 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  ts are undefined
2f1e0 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33  ..** If [sqlite3
2f1f0 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c  _step()] or [sql
2f200 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72  ite3_reset()] or
2f210 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
2f220 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20 63 61 6c  ze()].** are cal
2f230 6c 65 64 20 66 72 6f 6d 20 61 20 64 69 66 66 65  led from a diffe
2f240 72 65 6e 74 20 74 68 72 65 61 64 20 77 68 69 6c  rent thread whil
2f250 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72  e any of these r
2f260 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 70  outines.** are p
2f270 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 65  ending, then the
2f280 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64   results are und
2f290 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  efined..**.** Th
2f2a0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
2f2b0 5f 74 79 70 65 28 29 20 72 6f 75 74 69 6e 65 20  _type() routine 
2f2c0 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 5b  returns the.** [
2f2d0 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c  SQLITE_INTEGER |
2f2e0 20 64 61 74 61 74 79 70 65 20 63 6f 64 65 5d 20   datatype code] 
2f2f0 66 6f 72 20 74 68 65 20 69 6e 69 74 69 61 6c 20  for the initial 
2f300 64 61 74 61 20 74 79 70 65 0a 2a 2a 20 6f 66 20  data type.** of 
2f310 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  the result colum
2f320 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64  n.  The returned
2f330 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66   value is one of
2f340 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52   [SQLITE_INTEGER
2f350 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 4c  ],.** [SQLITE_FL
2f360 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45  OAT], [SQLITE_TE
2f370 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 4c 4f  XT], [SQLITE_BLO
2f380 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4e  B], or [SQLITE_N
2f390 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61 6c 75 65  ULL].  The value
2f3a0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79 20  .** returned by 
2f3b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
2f3c0 79 70 65 28 29 20 69 73 20 6f 6e 6c 79 20 6d 65  ype() is only me
2f3d0 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e 6f 20 74  aningful if no t
2f3e0 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f  ype.** conversio
2f3f0 6e 73 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  ns have occurred
2f400 20 61 73 20 64 65 73 63 72 69 62 65 64 20 62 65   as described be
2f410 6c 6f 77 2e 20 20 41 66 74 65 72 20 61 20 74 79  low.  After a ty
2f420 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a 2a  pe conversion,.*
2f430 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  * the value retu
2f440 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
2f450 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73  column_type() is
2f460 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 46 75 74   undefined.  Fut
2f470 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20  ure.** versions 
2f480 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68  of SQLite may ch
2f490 61 6e 67 65 20 74 68 65 20 62 65 68 61 76 69 6f  ange the behavio
2f4a0 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c  r of sqlite3_col
2f4b0 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a 20 66 6f  umn_type().** fo
2f4c0 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 65 20 63  llowing a type c
2f4d0 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  onversion..**.**
2f4e0 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   If the result i
2f4f0 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 54 46 2d  s a BLOB or UTF-
2f500 38 20 73 74 72 69 6e 67 20 74 68 65 6e 20 74 68  8 string then th
2f510 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
2f520 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72 6f 75 74  _bytes().** rout
2f530 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
2f540 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
2f550 69 6e 20 74 68 61 74 20 42 4c 4f 42 20 6f 72 20  in that BLOB or 
2f560 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66 20 74 68  string..** If th
2f570 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 55 54  e result is a UT
2f580 46 2d 31 36 20 73 74 72 69 6e 67 2c 20 74 68 65  F-16 string, the
2f590 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  n sqlite3_column
2f5a0 5f 62 79 74 65 73 28 29 20 63 6f 6e 76 65 72 74  _bytes() convert
2f5b0 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20  s.** the string 
2f5c0 74 6f 20 55 54 46 2d 38 20 61 6e 64 20 74 68 65  to UTF-8 and the
2f5d0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  n returns the nu
2f5e0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 2e 0a 2a  mber of bytes..*
2f5f0 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20  * If the result 
2f600 69 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c  is a numeric val
2f610 75 65 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f  ue then sqlite3_
2f620 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 75  column_bytes() u
2f630 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ses.** [sqlite3_
2f640 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 63  snprintf()] to c
2f650 6f 6e 76 65 72 74 20 74 68 61 74 20 76 61 6c 75  onvert that valu
2f660 65 20 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72  e to a UTF-8 str
2f670 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 0a  ing and returns.
2f680 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
2f690 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 73   bytes in that s
2f6a0 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 76 61  tring..** The va
2f6b0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 64 6f 65  lue returned doe
2f6c0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68  s not include th
2f6d0 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f  e zero terminato
2f6e0 72 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20  r at the end.** 
2f6f0 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20  of the string.  
2f700 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74 68 65  For clarity: the
2f710 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
2f720 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2f730 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65  .** bytes in the
2f740 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74 68 65   string, not the
2f750 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   number of chara
2f760 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72  cters..**.** Str
2f770 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79  ings returned by
2f780 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2f790 74 65 78 74 28 29 20 61 6e 64 20 73 71 6c 69 74  text() and sqlit
2f7a0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
2f7b0 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 6d 70 74  (),.** even empt
2f7c0 79 20 73 74 72 69 6e 67 73 2c 20 61 72 65 20 61  y strings, are a
2f7d0 6c 77 61 79 73 20 7a 65 72 6f 20 74 65 72 6d 69  lways zero termi
2f7e0 6e 61 74 65 64 2e 20 20 54 68 65 20 72 65 74 75  nated.  The retu
2f7f0 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66 72 6f 6d  rn.** value from
2f800 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2f810 62 6c 6f 62 28 29 20 66 6f 72 20 61 20 7a 65 72  blob() for a zer
2f820 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 20 69 73  o-length BLOB is
2f830 20 61 6e 20 61 72 62 69 74 72 61 72 79 0a 2a 2a   an arbitrary.**
2f840 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73 73 69 62   pointer, possib
2f850 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c 4c 20 70  ly even a NULL p
2f860 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ointer..**.** Th
2f870 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
2f880 5f 62 79 74 65 73 31 36 28 29 20 72 6f 75 74 69  _bytes16() routi
2f890 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f  ne is similar to
2f8a0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2f8b0 62 79 74 65 73 28 29 0a 2a 2a 20 62 75 74 20 6c  bytes().** but l
2f8c0 65 61 76 65 73 20 74 68 65 20 72 65 73 75 6c 74  eaves the result
2f8d0 20 69 6e 20 55 54 46 2d 31 36 20 69 6e 20 6e 61   in UTF-16 in na
2f8e0 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20  tive byte order 
2f8f0 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d 38  instead of UTF-8
2f900 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 20 74 65  ..** The zero te
2f910 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e 6f 74 20  rminator is not 
2f920 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 69 73  included in this
2f930 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68   count..**.** Th
2f940 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65  e object returne
2f950 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f  d by [sqlite3_co
2f960 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73  lumn_value()] is
2f970 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63   an.** [unprotec
2f980 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
2f990 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75  e] object.  An u
2f9a0 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
2f9b0 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a  e3_value object.
2f9c0 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75  ** may only be u
2f9d0 73 65 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65  sed with [sqlite
2f9e0 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20  3_bind_value()] 
2f9f0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  and [sqlite3_res
2fa00 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a  ult_value()]..**
2fa10 20 49 66 20 74 68 65 20 5b 75 6e 70 72 6f 74 65   If the [unprote
2fa20 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2fa30 75 65 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 72  ue] object retur
2fa40 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74  ned by.** [sqlit
2fa50 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
2fa60 29 5d 20 69 73 20 75 73 65 64 20 69 6e 20 61 6e  )] is used in an
2fa70 79 20 6f 74 68 65 72 20 77 61 79 2c 20 69 6e 63  y other way, inc
2fa80 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20  luding calls.** 
2fa90 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65  to routines like
2faa0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
2fab0 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  int()], [sqlite3
2fac0 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a  _value_text()],.
2fad0 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76  ** or [sqlite3_v
2fae0 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 74  alue_bytes()], t
2faf0 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72  hen the behavior
2fb00 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
2fb10 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
2fb20 6e 65 73 20 61 74 74 65 6d 70 74 20 74 6f 20 63  nes attempt to c
2fb30 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c 75 65  onvert the value
2fb40 20 77 68 65 72 65 20 61 70 70 72 6f 70 72 69 61   where appropria
2fb50 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d  te.  For.** exam
2fb60 70 6c 65 2c 20 69 66 20 74 68 65 20 69 6e 74 65  ple, if the inte
2fb70 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74  rnal representat
2fb80 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e 64  ion is FLOAT and
2fb90 20 61 20 74 65 78 74 20 72 65 73 75 6c 74 0a 2a   a text result.*
2fba0 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  * is requested, 
2fbb0 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74  [sqlite3_snprint
2fbc0 66 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e 74  f()] is used int
2fbd0 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65 72 66 6f  ernally to perfo
2fbe0 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72  rm the.** conver
2fbf0 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c  sion automatical
2fc00 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ly.  The followi
2fc10 6e 67 20 74 61 62 6c 65 20 64 65 74 61 69 6c 73  ng table details
2fc20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73   the conversions
2fc30 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 61 70 70  .** that are app
2fc40 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  lied:.**.** <blo
2fc50 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62  ckquote>.** <tab
2fc60 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a  le border="1">.*
2fc70 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72  * <tr><th> Inter
2fc80 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e  nal<br>Type <th>
2fc90 20 52 65 71 75 65 73 74 65 64 3c 62 72 3e 54 79   Requested<br>Ty
2fca0 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73  pe <th>  Convers
2fcb0 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74  ion.**.** <tr><t
2fcc0 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e  d>  NULL    <td>
2fcd0 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20   INTEGER   <td> 
2fce0 52 65 73 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c  Result is 0.** <
2fcf0 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20  tr><td>  NULL   
2fd00 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
2fd10 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30  <td> Result is 0
2fd20 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  .0.** <tr><td>  
2fd30 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 54  NULL    <td>   T
2fd40 45 58 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75  EXT    <td> Resu
2fd50 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74  lt is NULL point
2fd60 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  er.** <tr><td>  
2fd70 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 42  NULL    <td>   B
2fd80 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 65 73 75  LOB    <td> Resu
2fd90 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74  lt is NULL point
2fda0 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49  er.** <tr><td> I
2fdb0 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46 4c  NTEGER  <td>  FL
2fdc0 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76  OAT    <td> Conv
2fdd0 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65 72  ert from integer
2fde0 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72   to float.** <tr
2fdf0 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c  ><td> INTEGER  <
2fe00 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74  td>   TEXT    <t
2fe10 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69  d> ASCII renderi
2fe20 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ng of the intege
2fe30 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e  r.** <tr><td> IN
2fe40 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42 4c  TEGER  <td>   BL
2fe50 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20  OB    <td> Same 
2fe60 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 45 58 54  as INTEGER->TEXT
2fe70 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c  .** <tr><td>  FL
2fe80 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54 45 47  OAT   <td> INTEG
2fe90 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72  ER   <td> Conver
2fea0 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20  t from float to 
2feb0 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c  integer.** <tr><
2fec0 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64  td>  FLOAT   <td
2fed0 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  >   TEXT    <td>
2fee0 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67   ASCII rendering
2fef0 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a   of the float.**
2ff00 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54   <tr><td>  FLOAT
2ff10 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20     <td>   BLOB  
2ff20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 46    <td> Same as F
2ff30 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74  LOAT->TEXT.** <t
2ff40 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20  r><td>  TEXT    
2ff50 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c  <td> INTEGER   <
2ff60 74 64 3e 20 55 73 65 20 61 74 6f 69 28 29 0a 2a  td> Use atoi().*
2ff70 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54  * <tr><td>  TEXT
2ff80 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20      <td>  FLOAT 
2ff90 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 66     <td> Use atof
2ffa0 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ().** <tr><td>  
2ffb0 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 20 42  TEXT    <td>   B
2ffc0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63  LOB    <td> No c
2ffd0 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64  hange.** <tr><td
2ffe0 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20  >  BLOB    <td> 
2fff0 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43  INTEGER   <td> C
30000 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 74  onvert to TEXT t
30010 68 65 6e 20 75 73 65 20 61 74 6f 69 28 29 0a 2a  hen use atoi().*
30020 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42  * <tr><td>  BLOB
30030 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20      <td>  FLOAT 
30040 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20     <td> Convert 
30050 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 65  to TEXT then use
30060 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c   atof().** <tr><
30070 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64  td>  BLOB    <td
30080 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  >   TEXT    <td>
30090 20 41 64 64 20 61 20 7a 65 72 6f 20 74 65 72 6d   Add a zero term
300a0 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 64 65 64  inator if needed
300b0 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20  .** </table>.** 
300c0 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
300d0 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61 62  .** The table ab
300e0 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65 72 65  ove makes refere
300f0 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72 64 20  nce to standard 
30100 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69  C library functi
30110 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a 20 61 6e  ons atoi().** an
30120 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c 69 74  d atof().  SQLit
30130 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c  e does not reall
30140 79 20 75 73 65 20 74 68 65 73 65 20 66 75 6e 63  y use these func
30150 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73 20 69  tions.  It has i
30160 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 69 76 61  ts.** own equiva
30170 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20 72 6f  lent internal ro
30180 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61 74 6f  utines.  The ato
30190 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29 20 6e  i() and atof() n
301a0 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64  ames are.** used
301b0 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 66 6f   in the table fo
301c0 72 20 62 72 65 76 69 74 79 20 61 6e 64 20 62 65  r brevity and be
301d0 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20 66  cause they are f
301e0 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f 73 74 0a  amiliar to most.
301f0 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d 65 72 73  ** C programmers
30200 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
30210 74 20 77 68 65 6e 20 74 79 70 65 20 63 6f 6e 76  t when type conv
30220 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 2c 20 70  ersions occur, p
30230 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64  ointers returned
30240 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c   by prior.** cal
30250 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ls to sqlite3_co
30260 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 73 71 6c  lumn_blob(), sql
30270 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
30280 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71  (), and/or.** sq
30290 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
302a0 74 31 36 28 29 20 6d 61 79 20 62 65 20 69 6e 76  t16() may be inv
302b0 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 54 79 70  alidated..** Typ
302c0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 6e  e conversions an
302d0 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 61 6c 69  d pointer invali
302e0 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 6f 63  dations might oc
302f0 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f  cur.** in the fo
30300 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 3a 0a 2a  llowing cases:.*
30310 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
30320 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f  > The initial co
30330 6e 74 65 6e 74 20 69 73 20 61 20 42 4c 4f 42 20  ntent is a BLOB 
30340 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  and sqlite3_colu
30350 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 2a 2a 20  mn_text() or.** 
30360 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
30370 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20  umn_text16() is 
30380 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 72 6f 2d  called.  A zero-
30390 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 67 68 74  terminator might
303a0 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 20 74 6f  .**      need to
303b0 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65   be added to the
303c0 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a   string.</li>.**
303d0 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61   <li> The initia
303e0 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46  l content is UTF
303f0 2d 38 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69  -8 text and sqli
30400 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
30410 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20  16() or.**      
30420 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
30430 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c 65  ext16() is calle
30440 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  d.  The content 
30450 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65  must be converte
30460 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46  d.**      to UTF
30470 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69  -16.</li>.** <li
30480 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f  > The initial co
30490 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 31 36 20  ntent is UTF-16 
304a0 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33  text and sqlite3
304b0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20  _column_bytes() 
304c0 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  or.**      sqlit
304d0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
304e0 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65   is called.  The
304f0 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65   content must be
30500 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20   converted.**   
30510 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c 2f 6c 69     to UTF-8.</li
30520 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  >.** </ul>.**.**
30530 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65 74   Conversions bet
30540 77 65 65 6e 20 55 54 46 2d 31 36 62 65 20 61 6e  ween UTF-16be an
30550 64 20 55 54 46 2d 31 36 6c 65 20 61 72 65 20 61  d UTF-16le are a
30560 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e 20 70 6c  lways done in pl
30570 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a 20 6e 6f  ace and do.** no
30580 74 20 69 6e 76 61 6c 69 64 61 74 65 20 61 20 70  t invalidate a p
30590 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c 20 74 68  rior pointer, th
305a0 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74  ough of course t
305b0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
305c0 65 20 62 75 66 66 65 72 0a 2a 2a 20 74 68 61 74  e buffer.** that
305d0 20 74 68 65 20 70 72 69 6f 72 20 70 6f 69 6e 74   the prior point
305e0 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 77 69 6c  er points to wil
305f0 6c 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69  l have been modi
30600 66 69 65 64 2e 20 20 4f 74 68 65 72 20 6b 69 6e  fied.  Other kin
30610 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76 65 72 73  ds.** of convers
30620 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20 69 6e 20  ion are done in 
30630 70 6c 61 63 65 20 77 68 65 6e 20 69 74 20 69 73  place when it is
30640 20 70 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 73   possible, but s
30650 6f 6d 65 74 69 6d 65 73 20 74 68 65 79 0a 2a 2a  ometimes they.**
30660 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c   are not possibl
30670 65 20 61 6e 64 20 69 6e 20 74 68 6f 73 65 20 63  e and in those c
30680 61 73 65 73 20 70 72 69 6f 72 20 70 6f 69 6e 74  ases prior point
30690 65 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61  ers are invalida
306a0 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ted..**.** The s
306b0 61 66 65 73 74 20 61 6e 64 20 65 61 73 69 65 73  afest and easies
306c0 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 70 6f  t to remember po
306d0 6c 69 63 79 20 69 73 20 74 6f 20 69 6e 76 6f 6b  licy is to invok
306e0 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  e these routines
306f0 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68  .** in one of th
30700 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 61 79 73  e following ways
30710 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
30720 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c   <li>sqlite3_col
30730 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f 6c 6c 6f  umn_text() follo
30740 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  wed by sqlite3_c
30750 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c  olumn_bytes()</l
30760 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74  i>.**  <li>sqlit
30770 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29  e3_column_blob()
30780 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c   followed by sql
30790 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
307a0 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69  s()</li>.**  <li
307b0 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  >sqlite3_column_
307c0 74 65 78 74 31 36 28 29 20 66 6f 6c 6c 6f 77 65  text16() followe
307d0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d by sqlite3_col
307e0 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 3c 2f 6c  umn_bytes16()</l
307f0 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a  i>.** </ul>.**.*
30800 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
30810 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20 63 61 6c  , you should cal
30820 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  l sqlite3_column
30830 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73 71 6c 69  _text(),.** sqli
30840 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
30850 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f  ), or sqlite3_co
30860 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 69  lumn_text16() fi
30870 72 73 74 20 74 6f 20 66 6f 72 63 65 20 74 68 65  rst to force the
30880 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 74 6f 20   result.** into 
30890 74 68 65 20 64 65 73 69 72 65 64 20 66 6f 72 6d  the desired form
308a0 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20  at, then invoke 
308b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
308c0 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 73 71 6c  ytes() or.** sql
308d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
308e0 73 31 36 28 29 20 74 6f 20 66 69 6e 64 20 74 68  s16() to find th
308f0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 72 65  e size of the re
30900 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74 20 6d 69  sult.  Do not mi
30910 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71  x calls.** to sq
30920 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
30930 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63  t() or sqlite3_c
30940 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 77 69 74  olumn_blob() wit
30950 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71  h calls to.** sq
30960 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
30970 65 73 31 36 28 29 2c 20 61 6e 64 20 64 6f 20 6e  es16(), and do n
30980 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20 74 6f 20  ot mix calls to 
30990 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
309a0 65 78 74 31 36 28 29 0a 2a 2a 20 77 69 74 68 20  ext16().** with 
309b0 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
309c0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 2e  _column_bytes().
309d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74  .**.** The point
309e0 65 72 73 20 72 65 74 75 72 6e 65 64 20 61 72 65  ers returned are
309f0 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 61 20 74   valid until a t
30a00 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f  ype conversion o
30a10 63 63 75 72 73 20 61 73 0a 2a 2a 20 64 65 73 63  ccurs as.** desc
30a20 72 69 62 65 64 20 61 62 6f 76 65 2c 20 6f 72 20  ribed above, or 
30a30 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f 73  until [sqlite3_s
30a40 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  tep()] or [sqlit
30a50 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a  e3_reset()] or.*
30a60 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  * [sqlite3_final
30a70 69 7a 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 64  ize()] is called
30a80 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 73 70  .  The memory sp
30a90 61 63 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  ace used to hold
30aa0 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61 6e 64 20   strings.** and 
30ab0 42 4c 4f 42 73 20 69 73 20 66 72 65 65 64 20 61  BLOBs is freed a
30ac0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 44  utomatically.  D
30ad0 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61 73  o <b>not</b> pas
30ae0 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20 72  s the pointers r
30af0 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c 69  eturned.** [sqli
30b00 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
30b10 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  )], [sqlite3_col
30b20 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 65 74 63  umn_text()], etc
30b30 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74  . into.** [sqlit
30b40 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a  e3_free()]..**.*
30b50 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  * If a memory al
30b60 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f  location error o
30b70 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65  ccurs during the
30b80 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 61   evaluation of a
30b90 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 72  ny.** of these r
30ba0 6f 75 74 69 6e 65 73 2c 20 61 20 64 65 66 61 75  outines, a defau
30bb0 6c 74 20 76 61 6c 75 65 20 69 73 20 72 65 74 75  lt value is retu
30bc0 72 6e 65 64 2e 20 20 54 68 65 20 64 65 66 61 75  rned.  The defau
30bd0 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 65  lt value.** is e
30be0 69 74 68 65 72 20 74 68 65 20 69 6e 74 65 67 65  ither the intege
30bf0 72 20 30 2c 20 74 68 65 20 66 6c 6f 61 74 69 6e  r 0, the floatin
30c00 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 20 30  g point number 0
30c10 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c 0a 2a 2a  .0, or a NULL.**
30c20 20 70 6f 69 6e 74 65 72 2e 20 20 53 75 62 73 65   pointer.  Subse
30c30 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b  quent calls to [
30c40 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
30c50 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a  )] will return.*
30c60 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d  * [SQLITE_NOMEM]
30c70 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
30c80 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 30  TS:.**.** {H1380
30c90 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  3} The [sqlite3_
30ca0 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e 29  column_blob(S,N)
30cb0 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76  ] interface conv
30cc0 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20  erts the.**     
30cd0 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e 20       Nth column 
30ce0 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  in the current r
30cf0 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ow of the result
30d00 20 73 65 74 20 66 6f 72 0a 2a 2a 20 20 20 20 20   set for.**     
30d10 20 20 20 20 20 74 68 65 20 5b 70 72 65 70 61 72       the [prepar
30d20 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
30d30 69 6e 74 6f 20 61 20 42 4c 4f 42 20 61 6e 64 20  into a BLOB and 
30d40 74 68 65 6e 20 72 65 74 75 72 6e 73 20 61 0a 2a  then returns a.*
30d50 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74  *          point
30d60 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72  er to the conver
30d70 74 65 64 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  ted value..**.**
30d80 20 7b 48 31 33 38 30 36 7d 20 54 68 65 20 5b 73   {H13806} The [s
30d90 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
30da0 74 65 73 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  tes(S,N)] interf
30db0 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a  ace returns the.
30dc0 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62  **          numb
30dd0 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
30de0 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e  he BLOB or strin
30df0 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20  g (exclusive of 
30e00 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
30e10 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20  zero terminator 
30e20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29 20 74  on the string) t
30e30 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64  hat was returned
30e40 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   by the.**      
30e50 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e 74 20      most recent 
30e60 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
30e70 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e  _column_blob(S,N
30e80 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  )] or.**        
30e90 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d    [sqlite3_colum
30ea0 6e 5f 74 65 78 74 28 53 2c 4e 29 5d 2e 0a 2a 2a  n_text(S,N)]..**
30eb0 0a 2a 2a 20 7b 48 31 33 38 30 39 7d 20 54 68 65  .** {H13809} The
30ec0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
30ed0 5f 62 79 74 65 73 31 36 28 53 2c 4e 29 5d 20 69  _bytes16(S,N)] i
30ee0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
30ef0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
30f00 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
30f10 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 20 28   in the string (
30f20 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65  exclusive of the
30f30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72  .**          zer
30f40 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 6f 6e 20  o terminator on 
30f50 74 68 65 20 73 74 72 69 6e 67 29 20 74 68 61 74  the string) that
30f60 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 62 79   was returned by
30f70 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
30f80 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c   most recent cal
30f90 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f  l to [sqlite3_co
30fa0 6c 75 6d 6e 5f 74 65 78 74 31 36 28 53 2c 4e 29  lumn_text16(S,N)
30fb0 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 31 32  ]..**.** {H13812
30fc0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  } The [sqlite3_c
30fd0 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 53 2c 4e  olumn_double(S,N
30fe0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e  )] interface con
30ff0 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20  verts the.**    
31000 20 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e        Nth column
31010 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
31020 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
31030 74 20 73 65 74 20 66 6f 72 20 74 68 65 0a 2a 2a  t set for the.**
31040 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
31050 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
31060 20 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67   into a floating
31070 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 61 6e 64   point value and
31080 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74  .**          ret
31090 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74  urns a copy of t
310a0 68 61 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  hat value..**.**
310b0 20 7b 48 31 33 38 31 35 7d 20 54 68 65 20 5b 73   {H13815} The [s
310c0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
310d0 74 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63  t(S,N)] interfac
310e0 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a  e converts the.*
310f0 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 63  *          Nth c
31100 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72  olumn in the cur
31110 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
31120 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74  result set for t
31130 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
31140 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
31150 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 36 34 2d  nt] S into a 64-
31160 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
31170 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  er and.**       
31180 20 20 20 72 65 74 75 72 6e 73 20 74 68 65 20 6c     returns the l
31190 6f 77 65 72 20 33 32 20 62 69 74 73 20 6f 66 20  ower 32 bits of 
311a0 74 68 61 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a  that integer..**
311b0 0a 2a 2a 20 7b 48 31 33 38 31 38 7d 20 54 68 65  .** {H13818} The
311c0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
311d0 5f 69 6e 74 36 34 28 53 2c 4e 29 5d 20 69 6e 74  _int64(S,N)] int
311e0 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20  erface converts 
311f0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
31200 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  Nth column in th
31210 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
31220 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
31230 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  for the.**      
31240 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74      [prepared st
31250 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20  atement] S into 
31260 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  a 64-bit signed 
31270 69 6e 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20  integer and.**  
31280 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20          returns 
31290 61 20 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69  a copy of that i
312a0 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  nteger..**.** {H
312b0 31 33 38 32 31 7d 20 54 68 65 20 5b 73 71 6c 69  13821} The [sqli
312c0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
312d0 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  S,N)] interface 
312e0 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20  converts the.** 
312f0 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f 6c           Nth col
31300 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65  umn in the curre
31310 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65  nt row of the re
31320 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a 20  sult set for.** 
31330 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 70 72           the [pr
31340 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
31350 5d 20 53 20 69 6e 74 6f 20 61 20 7a 65 72 6f 2d  ] S into a zero-
31360 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
31370 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 72  .**          str
31380 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ing and returns 
31390 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61  a pointer to tha
313a0 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  t string..**.** 
313b0 7b 48 31 33 38 32 34 7d 20 54 68 65 20 5b 73 71  {H13824} The [sq
313c0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
313d0 74 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  t16(S,N)] interf
313e0 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65  ace converts the
313f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68  .**          Nth
31400 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63   column in the c
31410 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68  urrent row of th
31420 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72  e result set for
31430 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
31440 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
31450 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 7a  ment] S into a z
31460 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 32  ero-terminated 2
31470 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20 20  -byte.**        
31480 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31 36    aligned UTF-16
31490 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
314a0 65 72 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65  er string and re
314b0 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  turns.**        
314c0 20 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74    a pointer to t
314d0 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  hat string..**.*
314e0 2a 20 7b 48 31 33 38 32 37 7d 20 54 68 65 20 5b  * {H13827} The [
314f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
31500 79 70 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  ype(S,N)] interf
31510 61 63 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20  ace returns.**  
31520 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 5b          one of [
31530 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53  SQLITE_NULL], [S
31540 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 20  QLITE_INTEGER], 
31550 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a  [SQLITE_FLOAT],.
31560 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c  **          [SQL
31570 49 54 45 5f 54 45 58 54 5d 2c 20 6f 72 20 5b 53  ITE_TEXT], or [S
31580 51 4c 49 54 45 5f 42 4c 4f 42 5d 20 61 73 20 61  QLITE_BLOB] as a
31590 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a  ppropriate for.*
315a0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 4e  *          the N
315b0 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  th column in the
315c0 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
315d0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66  the result set f
315e0 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  or.**          t
315f0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
31600 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a  tement] S..**.**
31610 20 7b 48 31 33 38 33 30 7d 20 54 68 65 20 5b 73   {H13830} The [s
31620 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61  qlite3_column_va
31630 6c 75 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  lue(S,N)] interf
31640 61 63 65 20 72 65 74 75 72 6e 73 20 61 0a 2a 2a  ace returns a.**
31650 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74 65            pointe
31660 72 20 74 6f 20 61 6e 20 5b 75 6e 70 72 6f 74 65  r to an [unprote
31670 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
31680 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f 72 20 74  ue] object for t
31690 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e  he.**          N
316a0 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  th column in the
316b0 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
316c0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66  the result set f
316d0 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  or.**          t
316e0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
316f0 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51  tement] S..*/.SQ
31700 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
31710 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
31720 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33  umn_blob(sqlite3
31730 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
31740 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
31750 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
31760 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73  _bytes(sqlite3_s
31770 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
31780 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
31790 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
317a0 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73  ytes16(sqlite3_s
317b0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
317c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62  .SQLITE_API doub
317d0 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  le sqlite3_colum
317e0 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  n_double(sqlite3
317f0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
31800 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
31810 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
31820 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  _int(sqlite3_stm
31830 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
31840 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
31850 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
31860 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c  column_int64(sql
31870 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
31880 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  iCol);.SQLITE_AP
31890 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
318a0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
318b0 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74  olumn_text(sqlit
318c0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
318d0 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ol);.SQLITE_API 
318e0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
318f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
31900 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  6(sqlite3_stmt*,
31910 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49   int iCol);.SQLI
31920 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
31930 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73  e3_column_type(s
31940 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
31950 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
31960 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  API sqlite3_valu
31970 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e *sqlite3_colum
31980 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  n_value(sqlite3_
31990 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
319a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
319b0 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65  F: Destroy A Pre
319c0 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20  pared Statement 
319d0 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20  Object {H13300} 
319e0 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e  <S70300><S30100>
319f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
31a00 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75  e3_finalize() fu
31a10 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
31a20 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72   to delete a [pr
31a30 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
31a40 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61  ]..** If the sta
31a50 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75  tement was execu
31a60 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ted successfully
31a70 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64   or not executed
31a80 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a   at all, then.**
31a90 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
31aa0 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75  turned. If execu
31ab0 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74  tion of the stat
31ac0 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65  ement failed the
31ad0 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63  n an.** [error c
31ae0 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65  ode] or [extende
31af0 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73  d error code] is
31b00 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
31b10 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
31b20 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61  n be called at a
31b30 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20  ny point during 
31b40 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66  the execution of
31b50 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
31b60 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49  d statement].  I
31b70 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  f the virtual ma
31b80 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a  chine has not.**
31b90 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75   completed execu
31ba0 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72  tion when this r
31bb0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
31bc0 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a  , that is like.*
31bd0 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61  * encountering a
31be0 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73  n error or an [s
31bf0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
31c00 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a   | interrupt]..*
31c10 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64  * Incomplete upd
31c20 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c  ates may be roll
31c30 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e  ed back and tran
31c40 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65  sactions cancele
31c50 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20  d,.** depending 
31c60 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61  on the circumsta
31c70 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a  nces, and the.**
31c80 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65   [error code] re
31c90 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b  turned will be [
31ca0 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a  SQLITE_ABORT]..*
31cb0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
31cc0 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 32 7d 20  .**.** {H11302} 
31cd0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  The [sqlite3_fin
31ce0 61 6c 69 7a 65 28 53 29 5d 20 69 6e 74 65 72 66  alize(S)] interf
31cf0 61 63 65 20 64 65 73 74 72 6f 79 73 20 74 68 65  ace destroys the
31d00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
31d10 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
31d20 5d 20 53 20 61 6e 64 20 72 65 6c 65 61 73 65 73  ] S and releases
31d30 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20   all.**         
31d40 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69 6c 65   memory and file
31d50 20 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20   resources held 
31d60 62 79 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 0a  by that object..
31d70 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 34 7d 20 49  **.** {H11304} I
31d80 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
31d90 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
31da0 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20  e3_step(S)] for 
31db0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
31dc0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
31dd0 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20  ent] S returned 
31de0 61 6e 20 65 72 72 6f 72 2c 0a 2a 2a 20 20 20 20  an error,.**    
31df0 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c 69        then [sqli
31e00 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29 5d  te3_finalize(S)]
31e10 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 73 61   returns that sa
31e20 6d 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  me error..*/.SQL
31e30 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
31e40 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c  te3_finalize(sql
31e50 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
31e60 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
31e70 45 46 3a 20 52 65 73 65 74 20 41 20 50 72 65 70  EF: Reset A Prep
31e80 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f  ared Statement O
31e90 62 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c  bject {H13330} <
31ea0 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S70300>.**.** Th
31eb0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  e sqlite3_reset(
31ec0 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  ) function is ca
31ed0 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61 20  lled to reset a 
31ee0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
31ef0 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62  ent].** object b
31f00 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69  ack to its initi
31f10 61 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79 20  al state, ready 
31f20 74 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74 65  to be re-execute
31f30 64 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74  d..** Any SQL st
31f40 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65  atement variable
31f50 73 20 74 68 61 74 20 68 61 64 20 76 61 6c 75 65  s that had value
31f60 73 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20  s bound to them 
31f70 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71  using.** the [sq
31f80 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20  lite3_bind_blob 
31f90 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a  | sqlite3_bind_*
31fa0 28 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74  () API] retain t
31fb0 68 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20  heir values..** 
31fc0 55 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65  Use [sqlite3_cle
31fd0 61 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74  ar_bindings()] t
31fe0 6f 20 72 65 73 65 74 20 74 68 65 20 62 69 6e 64  o reset the bind
31ff0 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  ings..**.** {H11
32000 33 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  332} The [sqlite
32010 33 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65  3_reset(S)] inte
32020 72 66 61 63 65 20 72 65 73 65 74 73 20 74 68 65  rface resets the
32030 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
32040 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20  ment] S.**      
32050 20 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20      back to the 
32060 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73  beginning of its
32070 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20   program..**.** 
32080 7b 48 31 31 33 33 34 7d 20 49 66 20 74 68 65 20  {H11334} If the 
32090 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c  most recent call
320a0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   to [sqlite3_ste
320b0 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a  p(S)] for the.**
320c0 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
320d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
320e0 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54   returned [SQLIT
320f0 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54  E_ROW] or [SQLIT
32100 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20  E_DONE],.**     
32110 20 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69       or if [sqli
32120 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73  te3_step(S)] has
32130 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 65   never before be
32140 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a  en called on S,.
32150 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e  **          then
32160 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
32170 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  S)] returns [SQL
32180 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  ITE_OK]..**.** {
32190 48 31 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d  H11336} If the m
321a0 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20  ost recent call 
321b0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  to [sqlite3_step
321c0 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20  (S)] for the.** 
321d0 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72           [prepar
321e0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
321f0 69 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 72  indicated an err
32200 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20  or, then.**     
32210 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65       [sqlite3_re
32220 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20  set(S)] returns 
32230 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b  an appropriate [
32240 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a  error code]..**.
32250 2a 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20  ** {H11338} The 
32260 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53  [sqlite3_reset(S
32270 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65  )] interface doe
32280 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  s not change the
32290 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20   values.**      
322a0 20 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69      of any [sqli
322b0 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69  te3_bind_blob|bi
322c0 6e 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b  ndings] on the [
322d0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
322e0 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nt] S..*/.SQLITE
322f0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
32300 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73  _reset(sqlite3_s
32310 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a  tmt *pStmt);../*
32320 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72  .** CAPI3REF: Cr
32330 65 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65  eate Or Redefine
32340 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b   SQL Functions {
32350 48 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e  H16100} <S20200>
32360 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66  .** KEYWORDS: {f
32370 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e  unction creation
32380 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45   routines}.** KE
32390 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61  YWORDS: {applica
323a0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c  tion-defined SQL
323b0 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45   function}.** KE
323c0 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61  YWORDS: {applica
323d0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c  tion-defined SQL
323e0 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a   functions}.**.*
323f0 2a 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63  * These two func
32400 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76  tions (collectiv
32410 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75  ely known as "fu
32420 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20  nction creation 
32430 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72  routines").** ar
32440 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 51  e used to add SQ
32450 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61  L functions or a
32460 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20  ggregates or to 
32470 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 68  redefine the beh
32480 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73  avior.** of exis
32490 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f  ting SQL functio
324a0 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73  ns or aggregates
324b0 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66  .  The only diff
324c0 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74  erence between t
324d0 68 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61  he.** two is tha
324e0 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  t the second par
324f0 61 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65  ameter, the name
32500 20 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72 29   of the (scalar)
32510 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20   function or.** 
32520 61 67 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e  aggregate, is en
32530 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66  coded in UTF-8 f
32540 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  or sqlite3_creat
32550 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64  e_function() and
32560 20 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73   UTF-16.** for s
32570 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
32580 6e 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a  nction16()..**.*
32590 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
325a0 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 64 61  meter is the [da
325b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
325c0 6e 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65 20  n] to which the 
325d0 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  SQL.** function 
325e0 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20  is to be added. 
325f0 20 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f   If a single pro
32600 67 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74  gram uses more t
32610 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65  han one database
32620 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  .** connection i
32630 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20  nternally, then 
32640 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75  SQL functions mu
32650 73 74 20 62 65 20 61 64 64 65 64 20 69 6e 64 69  st be added indi
32660 76 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65  vidually to.** e
32670 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ach database con
32680 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  nection..**.** T
32690 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
326a0 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ter is the name 
326b0 6f 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  of the SQL funct
326c0 69 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65  ion to be create
326d0 64 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65  d or.** redefine
326e0 64 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f  d.  The length o
326f0 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69  f the name is li
32700 6d 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 74  mited to 255 byt
32710 65 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66  es, exclusive of
32720 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72  .** the zero-ter
32730 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74  minator.  Note t
32740 68 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e  hat the name len
32750 67 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20  gth limit is in 
32760 62 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68  bytes, not.** ch
32770 61 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61  aracters.  Any a
32780 74 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65  ttempt to create
32790 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
327a0 20 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a   a longer name.*
327b0 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e  * will result in
327c0 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
327d0 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a  being returned..
327e0 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20  **.** The third 
327f0 70 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29  parameter (nArg)
32800 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65  .** is the numbe
32810 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
32820 68 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63  hat the SQL func
32830 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65  tion or.** aggre
32840 67 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74  gate takes. If t
32850 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73  his parameter is
32860 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20   negative, then 
32870 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
32880 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65   or.** aggregate
32890 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75   may take any nu
328a0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
328b0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75  s..**.** The fou
328c0 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20 65  rth parameter, e
328d0 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66 69  TextRep, specifi
328e0 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c 49  es what.** [SQLI
328f0 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 65  TE_UTF8 | text e
32900 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 51  ncoding] this SQ
32910 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65  L function prefe
32920 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 61  rs for.** its pa
32930 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 53  rameters.  Any S
32940 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  QL function impl
32950 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c  ementation shoul
32960 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f 72  d be able to wor
32970 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 55  k.** work with U
32980 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20  TF-8, UTF-16le, 
32990 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75  or UTF-16be.  Bu
329a0 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74  t some implement
329b0 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a 2a  ations may be.**
329c0 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20   more efficient 
329d0 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e  with one encodin
329e0 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20  g than another. 
329f0 20 49 74 20 69 73 20 61 6c 6c 6f 77 65 64 20 74   It is allowed t
32a00 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69  o.** invoke sqli
32a10 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
32a20 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33  ion() or sqlite3
32a30 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
32a40 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  16() multiple.**
32a50 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 20   times with the 
32a60 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62 75  same function bu
32a70 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e 74  t with different
32a80 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78 74   values of eText
32a90 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c  Rep..** When mul
32aa0 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  tiple implementa
32ab0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d  tions of the sam
32ac0 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 61  e function are a
32ad0 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74 65  vailable, SQLite
32ae0 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74 68  .** will pick th
32af0 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f 6c  e one that invol
32b00 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 6d  ves the least am
32b10 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f 6e  ount of data con
32b20 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74  version..** If t
32b30 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73  here is only a s
32b40 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  ingle implementa
32b50 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20  tion which does 
32b60 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74 65  not care what te
32b70 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69  xt.** encoding i
32b80 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65  s used, then the
32b90 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74   fourth argument
32ba0 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c 49   should be [SQLI
32bb0 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54  TE_ANY]..**.** T
32bc0 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65 74  he fifth paramet
32bd0 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 61  er is an arbitra
32be0 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65  ry pointer.  The
32bf0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
32c00 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74 69  of the.** functi
32c10 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63 65  on can gain acce
32c20 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74  ss to this point
32c30 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  er using [sqlite
32c40 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e 0a  3_user_data()]..
32c50 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e 74  **.** The sevent
32c60 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e 69  h, eighth and ni
32c70 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c 20  nth parameters, 
32c80 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e 64  xFunc, xStep and
32c90 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a 20   xFinal, are.** 
32ca0 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c 61  pointers to C-la
32cb0 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e 73  nguage functions
32cc0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
32cd0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
32ce0 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65   or.** aggregate
32cf0 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20 66  . A scalar SQL f
32d00 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 73  unction requires
32d10 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69   an implementati
32d20 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63 0a  on of the xFunc.
32d30 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c 79  ** callback only
32d40 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20  , NULL pointers 
32d50 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
32d60 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61 6e   as the xStep an
32d70 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72 61  d xFinal.** para
32d80 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72 65  meters. An aggre
32d90 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f  gate SQL functio
32da0 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d  n requires an im
32db0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
32dc0 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46 69  xStep.** and xFi
32dd0 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68 6f  nal and NULL sho
32de0 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66 6f  uld be passed fo
32df0 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c 65  r xFunc. To dele
32e00 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a  te an existing.*
32e10 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f  * SQL function o
32e20 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61 73  r aggregate, pas
32e30 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20 74  s NULL for all t
32e40 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63 61  hree function ca
32e50 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49  llbacks..**.** I
32e60 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74  t is permitted t
32e70 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74 69  o register multi
32e80 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ple implementati
32e90 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 0a  ons of the same.
32ea0 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74  ** functions wit
32eb0 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20  h the same name 
32ec0 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72 20  but with either 
32ed0 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65 72  differing number
32ee0 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  s of.** argument
32ef0 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20 70  s or differing p
32f00 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 6e  referred text en
32f10 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74 65  codings.  SQLite
32f20 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68 65   will use.** the
32f30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
32f40 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74  most closely mat
32f50 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20  ches the way in 
32f60 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c  which the.** SQL
32f70 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
32f80 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69  d.  A function i
32f90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69  mplementation wi
32fa0 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  th a non-negativ
32fb0 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65  e.** nArg parame
32fc0 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20  ter is a better 
32fd0 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e  match than a fun
32fe0 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ction implementa
32ff0 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e  tion with.** a n
33000 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41  egative nArg.  A
33010 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20   function where 
33020 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65  the preferred te
33030 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d  xt encoding.** m
33040 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62  atches the datab
33050 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  ase encoding is 
33060 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63  a better.** matc
33070 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f  h than a functio
33080 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f  n where the enco
33090 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e  ding is differen
330a0 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69  t.  .** A functi
330b0 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63  on where the enc
330c0 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65  oding difference
330d0 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31   is between UTF1
330e0 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a  6le and UTF16be.
330f0 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d  ** is a closer m
33100 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63  atch than a func
33110 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65  tion where the e
33120 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e  ncoding differen
33130 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e  ce is.** between
33140 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e   UTF8 and UTF16.
33150 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20  .**.** Built-in 
33160 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65  functions may be
33170 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e   overloaded by n
33180 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  ew application-d
33190 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
331a0 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  ..** The first a
331b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
331c0 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  ed function with
331d0 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76   a given name ov
331e0 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62  errides all.** b
331f0 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  uilt-in function
33200 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64  s in the same [d
33210 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
33220 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d  on] with the sam
33230 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65  e name..** Subse
33240 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f  quent applicatio
33250 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
33260 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ons of the same 
33270 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69  name only overri
33280 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70  de .** prior app
33290 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
332a0 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
332b0 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74  are an exact mat
332c0 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75  ch for the.** nu
332d0 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65  mber of paramete
332e0 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64  rs and preferred
332f0 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a   encoding..**.**
33300 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   An application-
33310 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
33320 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f   is permitted to
33330 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53   call other.** S
33340 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73  QLite interfaces
33350 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68  .  However, such
33360 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a   calls must not.
33370 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74  ** close the dat
33380 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
33390 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72   nor finalize or
333a0 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61   reset the prepa
333b0 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  red.** statement
333c0 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75   in which the fu
333d0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
333e0 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  g..**.** INVARIA
333f0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31  NTS:.**.** {H161
33400 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  03} The [sqlite3
33410 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
33420 31 36 28 44 2c 58 2c 2e 2e 2e 29 5d 20 69 6e 74  16(D,X,...)] int
33430 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 68  erface shall beh
33440 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ave.**          
33450 61 73 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  as [sqlite3_crea
33460 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c  te_function(D,X,
33470 2e 2e 2e 29 5d 20 69 6e 20 65 76 65 72 79 20 77  ...)] in every w
33480 61 79 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ay except that i
33490 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  t.**          in
334a0 74 65 72 70 72 65 74 73 20 74 68 65 20 58 20 61  terprets the X a
334b0 72 67 75 6d 65 6e 74 20 61 73 20 7a 65 72 6f 2d  rgument as zero-
334c0 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 31  terminated UTF-1
334d0 36 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 61  6.**          na
334e0 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20  tive byte order 
334f0 69 6e 73 74 65 61 64 20 6f 66 20 61 73 20 7a 65  instead of as ze
33500 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ro-terminated UT
33510 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31  F-8..**.** {H161
33520 30 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  06} A successful
33530 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
33540 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
33550 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
33560 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c  unction(D,X,N,E,
33570 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  ...)] interface 
33580 73 68 61 6c 6c 20 72 65 67 69 73 74 65 72 0a 2a  shall register.*
33590 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 72 65  *          or re
335a0 70 6c 61 63 65 73 20 63 61 6c 6c 62 61 63 6b 20  places callback 
335b0 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65  functions in the
335c0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
335d0 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20  ction] D.**     
335e0 20 20 20 20 20 75 73 65 64 20 74 6f 20 69 6d 70       used to imp
335f0 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66  lement the SQL f
33600 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20  unction named X 
33610 77 69 74 68 20 4e 20 70 61 72 61 6d 65 74 65 72  with N parameter
33620 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e  s.**          an
33630 64 20 68 61 76 69 6e 67 20 61 20 70 72 65 66 65  d having a prefe
33640 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69  rred text encodi
33650 6e 67 20 6f 66 20 45 2e 0a 2a 2a 0a 2a 2a 20 7b  ng of E..**.** {
33660 48 31 36 31 30 39 7d 20 41 20 73 75 63 63 65 73  H16109} A succes
33670 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71  sful call to [sq
33680 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
33690 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c  ction(D,X,N,E,P,
336a0 46 2c 53 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20  F,S,L)].**      
336b0 20 20 20 20 73 68 61 6c 6c 20 72 65 70 6c 61 63      shall replac
336c0 65 20 74 68 65 20 50 2c 20 46 2c 20 53 2c 20 61  e the P, F, S, a
336d0 6e 64 20 4c 20 76 61 6c 75 65 73 20 66 72 6f 6d  nd L values from
336e0 20 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c 73   any prior calls
336f0 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20 20 20   with.**        
33700 20 20 74 68 65 20 73 61 6d 65 20 44 2c 20 58 2c    the same D, X,
33710 20 4e 2c 20 61 6e 64 20 45 20 76 61 6c 75 65 73   N, and E values
33720 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 31 32 7d  ..**.** {H16112}
33730 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72   The [sqlite3_cr
33740 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c  eate_function(D,
33750 58 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63  X,...)] interfac
33760 65 20 73 68 61 6c 6c 20 66 61 69 6c 0a 2a 2a 20  e shall fail.** 
33770 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 20           if the 
33780 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  SQL function nam
33790 65 20 58 20 69 73 0a 2a 2a 20 20 20 20 20 20 20  e X is.**       
337a0 20 20 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 32     longer than 2
337b0 35 35 20 62 79 74 65 73 20 65 78 63 6c 75 73 69  55 bytes exclusi
337c0 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f 20 74  ve of the zero t
337d0 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  erminator..**.**
337e0 20 7b 48 31 36 31 31 38 7d 20 54 68 65 20 5b 73   {H16118} The [s
337f0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
33800 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50  nction(D,X,N,E,P
33810 2c 46 2c 53 2c 4c 29 5d 20 69 6e 74 65 72 66 61  ,F,S,L)] interfa
33820 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  ce.**          s
33830 68 61 6c 6c 20 66 61 69 6c 20 75 6e 6c 65 73 73  hall fail unless
33840 20 65 69 74 68 65 72 20 46 20 69 73 20 4e 55 4c   either F is NUL
33850 4c 20 61 6e 64 20 53 20 61 6e 64 20 4c 20 61 72  L and S and L ar
33860 65 20 6e 6f 6e 2d 4e 55 4c 4c 20 6f 72 0a 2a 2a  e non-NULL or.**
33870 2a 20 20 20 20 20 20 20 20 20 46 20 69 73 20 6e  *         F is n
33880 6f 6e 2d 4e 55 4c 4c 20 61 6e 64 20 53 20 61 6e  on-NULL and S an
33890 64 20 4c 20 61 72 65 20 4e 55 4c 4c 2e 0a 2a 2a  d L are NULL..**
338a0 0a 2a 2a 20 7b 48 31 36 31 32 31 7d 20 54 68 65  .** {H16121} The
338b0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
338c0 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 2e 2e 2e 29  _function(D,...)
338d0 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c  ] interface shal
338e0 6c 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a  l fails with an.
338f0 2a 2a 20 20 20 20 20 20 20 20 20 20 65 72 72 6f  **          erro
33900 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54  r code of [SQLIT
33910 45 5f 42 55 53 59 5d 20 69 66 20 74 68 65 72 65  E_BUSY] if there
33920 20 65 78 69 73 74 20 5b 70 72 65 70 61 72 65 64   exist [prepared
33930 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20   statements].** 
33940 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61           associa
33950 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61  ted with the [da
33960 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
33970 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  n] D..**.** {H16
33980 31 32 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  124} The [sqlite
33990 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
339a0 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 20 69 6e  n(D,X,N,...)] in
339b0 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 66 61  terface shall fa
339c0 69 6c 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20  il with.**      
339d0 20 20 20 20 61 6e 20 65 72 72 6f 72 20 63 6f 64      an error cod
339e0 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 45 52 52  e of [SQLITE_ERR
339f0 4f 52 5d 20 69 66 20 70 61 72 61 6d 65 74 65 72  OR] if parameter
33a00 20 4e 20 69 73 20 6c 65 73 73 0a 2a 2a 20 20 20   N is less.**   
33a10 20 20 20 20 20 20 20 74 68 61 6e 20 2d 31 20 6f         than -1 o
33a20 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31  r greater than 1
33a30 32 37 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 32  27..**.** {H1612
33a40 37 7d 20 57 68 65 6e 20 4e 20 69 73 20 6e 6f 6e  7} When N is non
33a50 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 5b  -negative, the [
33a60 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
33a70 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 2e 2e  unction(D,X,N,..
33a80 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .)].**          
33a90 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20  interface shall 
33aa0 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63  register callbac
33ab0 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64  ks to be invoked
33ac0 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20   for the.**     
33ad0 20 20 20 20 20 53 51 4c 20 66 75 6e 63 74 69 6f       SQL functio
33ae0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 61  n.**          na
33af0 6d 65 64 20 58 20 77 68 65 6e 20 74 68 65 20 6e  med X when the n
33b00 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
33b10 74 73 20 74 6f 20 74 68 65 20 53 51 4c 20 66 75  ts to the SQL fu
33b20 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20  nction is.**    
33b30 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 4e 2e        exactly N.
33b40 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 30 7d 20  .**.** {H16130} 
33b50 57 68 65 6e 20 4e 20 69 73 20 2d 31 2c 20 74 68  When N is -1, th
33b60 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  e [sqlite3_creat
33b70 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e  e_function(D,X,N
33b80 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,...)].**       
33b90 20 20 20 69 6e 74 65 72 66 61 63 65 20 73 68 61     interface sha
33ba0 6c 6c 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c  ll register call
33bb0 62 61 63 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f  backs to be invo
33bc0 6b 65 64 20 66 6f 72 20 74 68 65 20 53 51 4c 0a  ked for the SQL.
33bd0 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63  **          func
33be0 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20 77 69 74  tion named X wit
33bf0 68 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20  h any number of 
33c00 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  arguments..**.**
33c10 20 7b 48 31 36 31 33 33 7d 20 57 68 65 6e 20 63   {H16133} When c
33c20 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
33c30 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
33c40 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20  (D,X,N,...)].** 
33c50 20 20 20 20 20 20 20 20 20 73 70 65 63 69 66 79           specify
33c60 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d   multiple implem
33c70 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
33c80 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 58   same function X
33c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64  .**          and
33ca0 20 77 68 65 6e 20 6f 6e 65 20 69 6d 70 6c 65 6d   when one implem
33cb0 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 4e 3e 3d  entation has N>=
33cc0 30 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20  0 and the other 
33cd0 68 61 73 20 4e 3d 28 2d 31 29 0a 2a 2a 20 20 20  has N=(-1).**   
33ce0 20 20 20 20 20 20 20 74 68 65 20 69 6d 70 6c 65         the imple
33cf0 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 20 61  mentation with a
33d00 20 6e 6f 6e 2d 7a 65 72 6f 20 4e 20 73 68 61 6c   non-zero N shal
33d10 6c 20 62 65 20 70 72 65 66 65 72 72 65 64 2e 0a  l be preferred..
33d20 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 36 7d 20 57  **.** {H16136} W
33d30 68 65 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71  hen calls to [sq
33d40 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
33d50 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 2e 2e  ction(D,X,N,E,..
33d60 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .)].**          
33d70 73 70 65 63 69 66 79 20 6d 75 6c 74 69 70 6c 65  specify multiple
33d80 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
33d90 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e   of the same fun
33da0 63 74 69 6f 6e 20 58 20 77 69 74 68 0a 2a 2a 20  ction X with.** 
33db0 20 20 20 20 20 20 20 20 20 74 68 65 20 73 61 6d           the sam
33dc0 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  e number of argu
33dd0 6d 65 6e 74 73 20 4e 20 62 75 74 20 77 69 74 68  ments N but with
33de0 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 20 20   different.**   
33df0 20 20 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73         encodings
33e00 20 45 2c 20 74 68 65 6e 20 74 68 65 20 69 6d 70   E, then the imp
33e10 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65 72  lementation wher
33e20 65 20 45 20 6d 61 74 63 68 65 73 20 74 68 65 0a  e E matches the.
33e30 2a 2a 20 20 20 20 20 20 20 20 20 20 64 61 74 61  **          data
33e40 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 73 68  base encoding sh
33e50 61 6c 6c 20 70 72 65 66 65 72 72 65 64 2e 0a 2a  all preferred..*
33e60 2a 0a 2a 2a 20 7b 48 31 36 31 33 39 7d 20 46 6f  *.** {H16139} Fo
33e70 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 53  r an aggregate S
33e80 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 72 65 61  QL function crea
33e90 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20  ted using.**    
33ea0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63        [sqlite3_c
33eb0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44  reate_function(D
33ec0 2c 58 2c 4e 2c 45 2c 50 2c 30 2c 53 2c 4c 29 5d  ,X,N,E,P,0,S,L)]
33ed0 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 0a 2a   the finalizer.*
33ee0 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 74  *          funct
33ef0 69 6f 6e 20 4c 20 73 68 61 6c 6c 20 61 6c 77 61  ion L shall alwa
33f00 79 73 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 78  ys be invoked ex
33f10 61 63 74 6c 79 20 6f 6e 63 65 20 69 66 20 74 68  actly once if th
33f20 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74  e.**          st
33f30 65 70 20 66 75 6e 63 74 69 6f 6e 20 53 20 69 73  ep function S is
33f40 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d   called one or m
33f50 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 0a 2a 2a  ore times..**.**
33f60 20 7b 48 31 36 31 34 32 7d 20 57 68 65 6e 20 53   {H16142} When S
33f70 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 65 69  QLite invokes ei
33f80 74 68 65 72 20 74 68 65 20 78 46 75 6e 63 20 6f  ther the xFunc o
33f90 72 20 78 53 74 65 70 20 66 75 6e 63 74 69 6f 6e  r xStep function
33fa0 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
33fb0 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  an application-d
33fc0 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74  efined SQL funct
33fd0 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65  ion or aggregate
33fe0 20 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20 20   created.**     
33ff0 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 65 33       by [sqlite3
34000 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
34010 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
34020 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
34030 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  6()],.**        
34040 20 20 74 68 65 6e 20 74 68 65 20 61 72 72 61 79    then the array
34050 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c   of [sqlite3_val
34060 75 65 5d 20 6f 62 6a 65 63 74 73 20 70 61 73 73  ue] objects pass
34070 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20  ed as the.**    
34080 20 20 20 20 20 20 74 68 69 72 64 20 70 61 72 61        third para
34090 6d 65 74 65 72 20 73 68 61 6c 6c 20 62 65 20 5b  meter shall be [
340a0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
340b0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73  3_value] objects
340c0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
340d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
340e0 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73  te_function(.  s
340f0 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
34100 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74  nst char *zFunct
34110 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e  ionName,.  int n
34120 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74  Arg,.  int eText
34130 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70  Rep,.  void *pAp
34140 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  p,.  void (*xFun
34150 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
34160 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
34170 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64  value**),.  void
34180 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65   (*xStep)(sqlite
34190 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
341a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
341b0 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  .  void (*xFinal
341c0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
341d0 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  t*).);.SQLITE_AP
341e0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
341f0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
34200 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
34210 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46    const void *zF
34220 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69  unctionName,.  i
34230 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65  nt nArg,.  int e
34240 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20  TextRep,.  void 
34250 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a  *pApp,.  void (*
34260 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
34270 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
34280 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
34290 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
342a0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
342b0 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
342c0 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46  **),.  void (*xF
342d0 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f  inal)(sqlite3_co
342e0 6e 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a  ntext*).);../*.*
342f0 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 74  * CAPI3REF: Text
34300 20 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30 32   Encodings {H102
34310 36 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48 31  67} <S50200> <H1
34320 36 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  6100>.**.** Thes
34330 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e  e constant defin
34340 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20  e integer codes 
34350 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 74  that represent t
34360 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65  he various.** te
34370 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 70  xt encodings sup
34380 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65  ported by SQLite
34390 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
343a0 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20  ITE_UTF8        
343b0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
343c0 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20  ITE_UTF16LE     
343d0 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c     2.#define SQL
343e0 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20  ITE_UTF16BE     
343f0 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c     3.#define SQL
34400 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 20  ITE_UTF16       
34410 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 6e     4    /* Use n
34420 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
34430 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
34440 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20  TE_ANY          
34450 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 65    5    /* sqlite
34460 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
34470 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  n only */.#defin
34480 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41  e SQLITE_UTF16_A
34490 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a 20  LIGNED  8    /* 
344a0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
344b0 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f  ollation only */
344c0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
344d0 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75 6e  : Deprecated Fun
344e0 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 43  ctions.** DEPREC
344f0 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ATED.**.** These
34500 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 5b   functions are [
34510 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 6e  deprecated].  In
34520 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61   order to mainta
34530 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20  in.** backwards 
34540 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69  compatibility wi
34550 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 74  th older code, t
34560 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 63  hese functions c
34570 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 62  ontinue .** to b
34580 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 6f  e supported.  Ho
34590 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c 69  wever, new appli
345a0 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 61  cations should a
345b0 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 20  void.** the use 
345c0 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  of these functio
345d0 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e 63  ns.  To help enc
345e0 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 6f  ourage people to
345f0 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 20   avoid.** using 
34600 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2c  these functions,
34610 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 6e   we are not goin
34620 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 68  g to tell you wh
34630 61 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23  at they do..*/.#
34640 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
34650 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 51  IT_DEPRECATED.SQ
34660 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f  LITE_API SQLITE_
34670 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 73  DEPRECATED int s
34680 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
34690 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 63  _count(sqlite3_c
346a0 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45  ontext*);.SQLITE
346b0 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52  _API SQLITE_DEPR
346c0 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74  ECATED int sqlit
346d0 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74  e3_expired(sqlit
346e0 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54  e3_stmt*);.SQLIT
346f0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
34700 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69  RECATED int sqli
34710 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e  te3_transfer_bin
34720 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74  dings(sqlite3_st
34730 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  mt*, sqlite3_stm
34740 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  t*);.SQLITE_API 
34750 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
34760 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c  D int sqlite3_gl
34770 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69  obal_recover(voi
34780 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  d);.SQLITE_API S
34790 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44  QLITE_DEPRECATED
347a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68   void sqlite3_th
347b0 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69  read_cleanup(voi
347c0 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  d);.SQLITE_API S
347d0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44  QLITE_DEPRECATED
347e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d   int sqlite3_mem
347f0 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a  ory_alarm(void(*
34800 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f  )(void*,sqlite3_
34810 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a  int64,int),void*
34820 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b  ,sqlite3_int64);
34830 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
34840 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 69  API3REF: Obtaini
34850 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20  ng SQL Function 
34860 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 73  Parameter Values
34870 20 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32 30   {H15100} <S2020
34880 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c  0>.**.** The C-l
34890 61 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e  anguage implemen
348a0 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75  tation of SQL fu
348b0 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72  nctions and aggr
348c0 65 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 74  egates uses.** t
348d0 68 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 72  his set of inter
348e0 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f  face routines to
348f0 20 61 63 63 65 73 73 20 74 68 65 20 70 61 72 61   access the para
34900 6d 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a  meter values on.
34910 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ** the function 
34920 6f 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a  or aggregate..**
34930 0a 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 66  .** The xFunc (f
34940 6f 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69  or scalar functi
34950 6f 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 66  ons) or xStep (f
34960 6f 72 20 61 67 67 72 65 67 61 74 65 73 29 20 70  or aggregates) p
34970 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20  arameters.** to 
34980 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
34990 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20  function()] and 
349a0 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
349b0 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a  function16()].**
349c0 20 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b   define callback
349d0 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
349e0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
349f0 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65  ns and aggregate
34a00 73 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70 61  s..** The 4th pa
34a10 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 65  rameter to these
34a20 20 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e   callbacks is an
34a30 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65   array of pointe
34a40 72 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63  rs to.** [protec
34a50 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
34a60 65 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65  e] objects.  The
34a70 72 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74  re is one [sqlit
34a80 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
34a90 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72   for.** each par
34aa0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 51  ameter to the SQ
34ab0 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  L function.  The
34ac0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
34ad0 75 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61  used to.** extra
34ae0 63 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74  ct values from t
34af0 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  he [sqlite3_valu
34b00 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a  e] objects..**.*
34b10 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
34b20 20 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20   work only with 
34b30 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
34b40 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
34b50 73 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70  s..** Any attemp
34b60 74 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 72  t to use these r
34b70 6f 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75  outines on an [u
34b80 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
34b90 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a  e3_value].** obj
34ba0 65 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 75  ect results in u
34bb0 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f  ndefined behavio
34bc0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  r..**.** These r
34bd0 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73  outines work jus
34be0 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 65  t like the corre
34bf0 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e  sponding [column
34c00 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e   access function
34c10 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61  s].** except tha
34c20 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  t  these routine
34c30 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 20  s take a single 
34c40 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
34c50 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
34c60 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 74  .** pointer inst
34c70 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 65  ead of a [sqlite
34c80 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72  3_stmt*] pointer
34c90 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 20   and an integer 
34ca0 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a  column number..*
34cb0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
34cc0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 20  _value_text16() 
34cd0 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 63  interface extrac
34ce0 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69  ts a UTF-16 stri
34cf0 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 74  ng.** in the nat
34d00 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 6f  ive byte-order o
34d10 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 69  f the host machi
34d20 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69  ne.  The.** sqli
34d30 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
34d40 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  be() and sqlite3
34d50 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28  _value_text16le(
34d60 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  ) interfaces.** 
34d70 65 78 74 72 61 63 74 20 55 54 46 2d 31 36 20 73  extract UTF-16 s
34d80 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65 6e  trings as big-en
34d90 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d  dian and little-
34da0 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 76  endian respectiv
34db0 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ely..**.** The s
34dc0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d  qlite3_value_num
34dd0 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74 65  eric_type() inte
34de0 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74  rface attempts t
34df0 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72  o apply.** numer
34e00 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20 74  ic affinity to t
34e10 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20  he value.  This 
34e20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61 74  means that an at
34e30 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64 65  tempt is.** made
34e40 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20   to convert the 
34e50 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 65  value to an inte
34e60 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20  ger or floating 
34e70 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 75  point.  If.** su
34e80 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ch a conversion 
34e90 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68  is possible with
34ea0 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f  out loss of info
34eb0 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68 65  rmation (in othe
34ec0 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20 74  r.** words, if t
34ed0 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74  he value is a st
34ee0 72 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73 20  ring that looks 
34ef0 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a 2a  like a number).*
34f00 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76 65  * then the conve
34f10 72 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d  rsion is perform
34f20 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6e  ed.  Otherwise n
34f30 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63  o conversion occ
34f40 75 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c  urs..** The [SQL
34f50 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61  ITE_INTEGER | da
34f60 74 61 74 79 70 65 5d 20 61 66 74 65 72 20 63 6f  tatype] after co
34f70 6e 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74 75  nversion is retu
34f80 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61  rned..**.** Plea
34f90 73 65 20 70 61 79 20 70 61 72 74 69 63 75 6c 61  se pay particula
34fa0 72 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74  r attention to t
34fb0 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65  he fact that the
34fc0 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
34fd0 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74  d.** from [sqlit
34fe0 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d  e3_value_blob()]
34ff0 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  , [sqlite3_value
35000 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20  _text()], or.** 
35010 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
35020 65 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65 20  ext16()] can be 
35030 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 61  invalidated by a
35040 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
35050 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
35060 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20  value_bytes()], 
35070 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  [sqlite3_value_b
35080 79 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69  ytes16()], [sqli
35090 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29  te3_value_text()
350a0 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65  ],.** or [sqlite
350b0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29  3_value_text16()
350c0 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  ]..**.** These r
350d0 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20  outines must be 
350e0 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20  called from the 
350f0 73 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a 2a  same thread as.*
35100 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  * the SQL functi
35110 6f 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65 64  on that supplied
35120 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61   the [sqlite3_va
35130 6c 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72 73  lue*] parameters
35140 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
35150 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30  TS:.**.** {H1510
35160 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  3} The [sqlite3_
35170 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29 5d 20 69  value_blob(V)] i
35180 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74  nterface convert
35190 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
351a0 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c    [protected sql
351b0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
351c0 63 74 20 56 20 69 6e 74 6f 20 61 20 42 4c 4f 42  ct V into a BLOB
351d0 20 61 6e 64 20 74 68 65 6e 0a 2a 2a 20 20 20 20   and then.**    
351e0 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 20        returns a 
351f0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  pointer to the c
35200 6f 6e 76 65 72 74 65 64 20 76 61 6c 75 65 2e 0a  onverted value..
35210 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30 36 7d 20 54  **.** {H15106} T
35220 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  he [sqlite3_valu
35230 65 5f 62 79 74 65 73 28 56 29 5d 20 69 6e 74 65  e_bytes(V)] inte
35240 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68  rface returns th
35250 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75  e.**          nu
35260 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
35270 20 74 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72   the BLOB or str
35280 69 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f  ing (exclusive o
35290 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  f the.**        
352a0 20 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f    zero terminato
352b0 72 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29  r on the string)
352c0 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e   that was return
352d0 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20  ed by the.**    
352e0 20 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e        most recen
352f0 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
35300 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29  e3_value_blob(V)
35310 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ] or.**         
35320 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
35330 74 65 78 74 28 56 29 5d 2e 0a 2a 2a 0a 2a 2a 20  text(V)]..**.** 
35340 7b 48 31 35 31 30 39 7d 20 54 68 65 20 5b 73 71  {H15109} The [sq
35350 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
35360 73 31 36 28 56 29 5d 20 69 6e 74 65 72 66 61 63  s16(V)] interfac
35370 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  e returns the.**
35380 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72            number
35390 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
353a0 20 73 74 72 69 6e 67 20 28 65 78 63 6c 75 73 69   string (exclusi
353b0 76 65 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20  ve of the.**    
353c0 20 20 20 20 20 20 7a 65 72 6f 20 74 65 72 6d 69        zero termi
353d0 6e 61 74 6f 72 20 6f 6e 20 74 68 65 20 73 74 72  nator on the str
353e0 69 6e 67 29 20 74 68 61 74 20 77 61 73 20 72 65  ing) that was re
353f0 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a  turned by the.**
35400 20 20 20 20 20 20 20 20 20 20 6d 6f 73 74 20 72            most r
35410 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73  ecent call to [s
35420 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
35430 74 31 36 28 56 29 5d 2c 0a 2a 2a 20 20 20 20 20  t16(V)],.**     
35440 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 61       [sqlite3_va
35450 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56 29 5d  lue_text16be(V)]
35460 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61  , or [sqlite3_va
35470 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 56 29 5d  lue_text16le(V)]
35480 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 31 32 7d  ..**.** {H15112}
35490 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61   The [sqlite3_va
354a0 6c 75 65 5f 64 6f 75 62 6c 65 28 56 29 5d 20 69  lue_double(V)] i
354b0 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74  nterface convert
354c0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
354d0 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c    [protected sql
354e0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
354f0 63 74 20 56 20 69 6e 74 6f 20 61 20 66 6c 6f 61  ct V into a floa
35500 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
35510 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
35520 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20   returns a copy 
35530 6f 66 20 74 68 61 74 20 76 61 6c 75 65 2e 0a 2a  of that value..*
35540 2a 0a 2a 2a 20 7b 48 31 35 31 31 35 7d 20 54 68  *.** {H15115} Th
35550 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
35560 5f 69 6e 74 28 56 29 5d 20 69 6e 74 65 72 66 61  _int(V)] interfa
35570 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a  ce converts the.
35580 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 6f  **          [pro
35590 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
355a0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69  alue] object V i
355b0 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69 67  nto a 64-bit sig
355c0 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 0a  ned integer and.
355d0 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  **          retu
355e0 72 6e 73 20 74 68 65 20 6c 6f 77 65 72 20 33 32  rns the lower 32
355f0 20 62 69 74 73 20 6f 66 20 74 68 61 74 20 69 6e   bits of that in
35600 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  teger..**.** {H1
35610 35 31 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 74  5118} The [sqlit
35620 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 56  e3_value_int64(V
35630 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e  )] interface con
35640 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20  verts the.**    
35650 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64        [protected
35660 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
35670 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20  object V into a 
35680 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
35690 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20  teger and.**    
356a0 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 20        returns a 
356b0 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69 6e 74  copy of that int
356c0 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35  eger..**.** {H15
356d0 31 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  121} The [sqlite
356e0 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 56 29 5d  3_value_text(V)]
356f0 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65   interface conve
35700 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  rts the.**      
35710 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73      [protected s
35720 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
35730 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 7a 65  ject V into a ze
35740 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ro-terminated UT
35750 46 2d 38 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  F-8.**          
35760 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72  string and retur
35770 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
35780 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  that string..**.
35790 2a 2a 20 7b 48 31 35 31 32 34 7d 20 54 68 65 20  ** {H15124} The 
357a0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
357b0 65 78 74 31 36 28 56 29 5d 20 69 6e 74 65 72 66  ext16(V)] interf
357c0 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65  ace converts the
357d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
357e0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
357f0 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20  value] object V 
35800 69 6e 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d  into a zero-term
35810 69 6e 61 74 65 64 20 32 2d 62 79 74 65 0a 2a 2a  inated 2-byte.**
35820 20 20 20 20 20 20 20 20 20 20 61 6c 69 67 6e 65            aligne
35830 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20  d UTF-16 native 
35840 62 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20  byte order.**   
35850 20 20 20 20 20 20 20 73 74 72 69 6e 67 20 61 6e         string an
35860 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  d returns a poin
35870 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 72 69  ter to that stri
35880 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 32  ng..**.** {H1512
35890 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  7} The [sqlite3_
358a0 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56  value_text16be(V
358b0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e  )] interface con
358c0 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20  verts the.**    
358d0 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64        [protected
358e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
358f0 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20  object V into a 
35900 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
35910 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20  2-byte.**       
35920 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31     aligned UTF-1
35930 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a 20  6 big-endian.** 
35940 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 20           string 
35950 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
35960 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73 74  inter to that st
35970 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35  ring..**.** {H15
35980 31 33 30 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  130} The [sqlite
35990 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
359a0 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  (V)] interface c
359b0 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20  onverts the.**  
359c0 20 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74          [protect
359d0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
359e0 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20  ] object V into 
359f0 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  a zero-terminate
35a00 64 20 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20  d 2-byte.**     
35a10 20 20 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46       aligned UTF
35a20 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61  -16 little-endia
35a30 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74  n.**          st
35a40 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73  ring and returns
35a50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
35a60 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  at string..**.**
35a70 20 7b 48 31 35 31 33 33 7d 20 54 68 65 20 5b 73   {H15133} The [s
35a80 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
35a90 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20  e(V)] interface 
35aa0 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20  returns.**      
35ab0 20 20 20 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49      one of [SQLI
35ac0 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54  TE_NULL], [SQLIT
35ad0 45 5f 49 4e 54 45 47 45 52 5d 2c 20 5b 53 51 4c  E_INTEGER], [SQL
35ae0 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a 2a 2a 20 20  ITE_FLOAT],.**  
35af0 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f          [SQLITE_
35b00 54 45 58 54 5d 2c 20 6f 72 20 5b 53 51 4c 49 54  TEXT], or [SQLIT
35b10 45 5f 42 4c 4f 42 5d 20 61 73 20 61 70 70 72 6f  E_BLOB] as appro
35b20 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a 20 20 20  priate for.**   
35b30 20 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c 69         the [sqli
35b40 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
35b50 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31  t V..**.** {H151
35b60 33 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  36} The [sqlite3
35b70 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
35b80 79 70 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63  ype(V)] interfac
35b90 65 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 20 20  e converts.**   
35ba0 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 6f 74         the [prot
35bb0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
35bc0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e  lue] object V in
35bd0 74 6f 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74  to either an int
35be0 65 67 65 72 20 6f 72 0a 2a 2a 20 20 20 20 20 20  eger or.**      
35bf0 20 20 20 20 61 20 66 6c 6f 61 74 69 6e 67 20 70      a floating p
35c00 6f 69 6e 74 20 76 61 6c 75 65 20 69 66 20 69 74  oint value if it
35c10 20 63 61 6e 20 64 6f 20 73 6f 20 77 69 74 68 6f   can do so witho
35c20 75 74 20 6c 6f 73 73 20 6f 66 0a 2a 2a 20 20 20  ut loss of.**   
35c30 20 20 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 69         informati
35c40 6f 6e 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20  on, and returns 
35c50 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4e  one of [SQLITE_N
35c60 55 4c 4c 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ULL],.**        
35c70 20 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45    [SQLITE_INTEGE
35c80 52 5d 2c 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41  R], [SQLITE_FLOA
35c90 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54  T], [SQLITE_TEXT
35ca0 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  ], or.**        
35cb0 20 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 20    [SQLITE_BLOB] 
35cc0 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  as appropriate f
35cd0 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  or the.**       
35ce0 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71     [protected sq
35cf0 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
35d00 65 63 74 20 56 20 61 66 74 65 72 20 74 68 65 20  ect V after the 
35d10 63 6f 6e 76 65 72 73 69 6f 6e 20 61 74 74 65 6d  conversion attem
35d20 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  pt..*/.SQLITE_AP
35d30 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
35d40 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
35d50 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
35d60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
35d70 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
35d80 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c  ytes(sqlite3_val
35d90 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
35da0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
35db0 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74  ue_bytes16(sqlit
35dc0 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
35dd0 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71  TE_API double sq
35de0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
35df0 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le(sqlite3_value
35e00 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
35e10 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
35e20 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c  _int(sqlite3_val
35e30 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
35e40 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
35e50 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
35e60 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  64(sqlite3_value
35e70 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
35e80 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
35e90 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar *sqlite3_valu
35ea0 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76  e_text(sqlite3_v
35eb0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
35ec0 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
35ed0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
35ee0 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  t16(sqlite3_valu
35ef0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
35f00 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
35f10 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
35f20 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le(sqlite3_value
35f30 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
35f40 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
35f50 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62  e3_value_text16b
35f60 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  e(sqlite3_value*
35f70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
35f80 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
35f90 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  type(sqlite3_val
35fa0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
35fb0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
35fc0 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
35fd0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
35fe0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
35ff0 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67 61  : Obtain Aggrega
36000 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74  te Function Cont
36010 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53 32  ext {H16210} <S2
36020 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0200>.**.** The 
36030 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
36040 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20  f aggregate SQL 
36050 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 68  functions use th
36060 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61 6c  is routine to al
36070 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72 75  locate.** a stru
36080 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69 6e  cture for storin
36090 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a 2a  g their state..*
360a0 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
360b0 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ime the sqlite3_
360c0 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
360d0 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63  t() routine is c
360e0 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20 70  alled for a.** p
360f0 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 67  articular aggreg
36100 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c 6f  ate, SQLite allo
36110 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66 20  cates nBytes of 
36120 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20 6f  memory, zeroes o
36130 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f 72  ut that.** memor
36140 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  y, and returns a
36150 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
36160 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75  On second and su
36170 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
36180 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67 67  o.** sqlite3_agg
36190 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29  regate_context()
361a0 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61 67   for the same ag
361b0 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
361c0 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20 73   index,.** the s
361d0 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72 65  ame buffer is re
361e0 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70 6c  turned. The impl
361f0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
36200 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e 20  e aggregate can 
36210 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72  use.** the retur
36220 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61 63  ned buffer to ac
36230 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a 2a  cumulate data..*
36240 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74 6f  *.** SQLite auto
36250 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73 20  matically frees 
36260 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75  the allocated bu
36270 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61 67  ffer when the ag
36280 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72 79  gregate.** query
36290 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a   concludes..**.*
362a0 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
362b0 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20  meter should be 
362c0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a  a copy of the.**
362d0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   [sqlite3_contex
362e0 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  t | SQL function
362f0 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20 69   context] that i
36300 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61  s the first para
36310 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  meter.** to the 
36320 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
36330 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
36340 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 66   the aggregate f
36350 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  unction..**.** T
36360 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
36370 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
36380 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
36390 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20  in which.** the 
363a0 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75  aggregate SQL fu
363b0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
363c0 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  g..**.** INVARIA
363d0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32  NTS:.**.** {H162
363e0 31 31 7d 20 54 68 65 20 66 69 72 73 74 20 69 6e  11} The first in
363f0 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c  vocation of [sql
36400 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
36410 6f 6e 74 65 78 74 28 43 2c 4e 29 5d 20 66 6f 72  ontext(C,N)] for
36420 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 70  .**          a p
36430 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e  articular instan
36440 63 65 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61  ce of an aggrega
36450 74 65 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72  te function (for
36460 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
36470 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78            contex
36480 74 20 43 29 20 63 61 75 73 65 73 20 53 51 4c 69  t C) causes SQLi
36490 74 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 4e  te to allocate N
364a0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
364b0 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65  ,.**          ze
364c0 72 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2c 20  ro that memory, 
364d0 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
364e0 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f  nter to the allo
364f0 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  cated memory..**
36500 0a 2a 2a 20 7b 48 31 36 32 31 33 7d 20 49 66 20  .** {H16213} If 
36510 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
36520 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ion error occurs
36530 20 64 75 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20   during.**      
36540 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67      [sqlite3_agg
36550 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43  regate_context(C
36560 2c 4e 29 5d 20 74 68 65 6e 20 74 68 65 20 66 75  ,N)] then the fu
36570 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 30  nction returns 0
36580 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31 35 7d  ..**.** {H16215}
36590 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73   Second and subs
365a0 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f  equent invocatio
365b0 6e 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ns of.**        
365c0 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65    [sqlite3_aggre
365d0 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c 4e  gate_context(C,N
365e0 29 5d 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20  )] for the same 
365f0 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20  context pointer 
36600 43 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 67  C.**          ig
36610 6e 6f 72 65 20 74 68 65 20 4e 20 70 61 72 61 6d  nore the N param
36620 65 74 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20  eter and return 
36630 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
36640 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20   same.**        
36650 20 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72    block of memor
36660 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  y returned by th
36670 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74 69  e first invocati
36680 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31  on..**.** {H1621
36690 37 7d 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c  7} The memory al
366a0 6c 6f 63 61 74 65 64 20 62 79 20 5b 73 71 6c 69  located by [sqli
366b0 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
366c0 6e 74 65 78 74 28 43 2c 4e 29 5d 20 69 73 0a 2a  ntext(C,N)] is.*
366d0 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d  *          autom
366e0 61 74 69 63 61 6c 6c 79 20 66 72 65 65 64 20 6f  atically freed o
366f0 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  n the next call 
36700 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  to [sqlite3_rese
36710 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  t()].**         
36720 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e   or [sqlite3_fin
36730 61 6c 69 7a 65 28 29 5d 20 66 6f 72 20 74 68 65  alize()] for the
36740 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
36750 6d 65 6e 74 5d 20 63 6f 6e 74 61 69 6e 69 6e 67  ment] containing
36760 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
36770 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
36780 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77  ion associated w
36790 69 74 68 20 63 6f 6e 74 65 78 74 20 43 2e 0a 2a  ith context C..*
367a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
367b0 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65  d *sqlite3_aggre
367c0 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c  gate_context(sql
367d0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69  ite3_context*, i
367e0 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a 0a  nt nBytes);../*.
367f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65  ** CAPI3REF: Use
36800 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74  r Data For Funct
36810 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c 53  ions {H16240} <S
36820 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20200>.**.** The
36830 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61   sqlite3_user_da
36840 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ta() interface r
36850 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66  eturns a copy of
36860 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20  .** the pointer 
36870 74 68 61 74 20 77 61 73 20 74 68 65 20 70 55 73  that was the pUs
36880 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 65 72  erData parameter
36890 20 28 74 68 65 20 35 74 68 20 70 61 72 61 6d 65   (the 5th parame
368a0 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b  ter).** of the [
368b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
368c0 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e  unction()].** an
368d0 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  d [sqlite3_creat
368e0 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20  e_function16()] 
368f0 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f 72  routines that or
36900 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69  iginally.** regi
36910 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c 69  stered the appli
36920 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 66  cation defined f
36930 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a  unction. {END}.*
36940 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
36950 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
36960 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74   from the same t
36970 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a  hread in which.*
36980 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * the applicatio
36990 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
369a0 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a  on is running..*
369b0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
369c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 34 33 7d 20  .**.** {H16243} 
369d0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 73 65  The [sqlite3_use
369e0 72 5f 64 61 74 61 28 43 29 5d 20 69 6e 74 65 72  r_data(C)] inter
369f0 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63  face returns a c
36a00 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20  opy of the.**   
36a10 20 20 20 20 20 20 20 50 20 70 6f 69 6e 74 65 72         P pointer
36a20 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74   from the [sqlit
36a30 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
36a40 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53  on(D,X,N,E,P,F,S
36a50 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ,L)].**         
36a60 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 65   or [sqlite3_cre
36a70 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44  ate_function16(D
36a80 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d  ,X,N,E,P,F,S,L)]
36a90 20 63 61 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20   call that.**   
36aa0 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65         registere
36ab0 64 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  d the SQL functi
36ac0 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  on associated wi
36ad0 74 68 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74  th [sqlite3_cont
36ae0 65 78 74 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54  ext] C..*/.SQLIT
36af0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
36b00 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71  te3_user_data(sq
36b10 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
36b20 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
36b30 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65  : Database Conne
36b40 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69  ction For Functi
36b50 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36  ons {H16250} <S6
36b60 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a  0600><S20200>.**
36b70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
36b80 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
36b90 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  e() interface re
36ba0 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a  turns a copy of.
36bb0 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ** the pointer t
36bc0 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  o the [database 
36bd0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65  connection] (the
36be0 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a   1st parameter).
36bf0 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ** of the [sqlit
36c00 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
36c10 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71  on()].** and [sq
36c20 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
36c30 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69  ction16()] routi
36c40 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61  nes that origina
36c50 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65  lly.** registere
36c60 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  d the applicatio
36c70 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n defined functi
36c80 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  on..**.** INVARI
36c90 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ANTS:.**.** {H16
36ca0 32 35 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  253} The [sqlite
36cb0 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
36cc0 64 6c 65 28 43 29 5d 20 69 6e 74 65 72 66 61 63  dle(C)] interfac
36cd0 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79  e returns a copy
36ce0 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   of the.**      
36cf0 20 20 20 20 44 20 70 6f 69 6e 74 65 72 20 66 72      D pointer fr
36d00 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  om the [sqlite3_
36d10 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
36d20 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29  D,X,N,E,P,F,S,L)
36d30 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  ].**          or
36d40 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
36d50 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44 2c 58 2c  _function16(D,X,
36d60 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d 20 63 61  N,E,P,F,S,L)] ca
36d70 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20  ll that.**      
36d80 20 20 20 20 72 65 67 69 73 74 65 72 65 64 20 74      registered t
36d90 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  he SQL function 
36da0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
36db0 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  [sqlite3_context
36dc0 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ] C..*/.SQLITE_A
36dd0 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  PI sqlite3 *sqli
36de0 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
36df0 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f  andle(sqlite3_co
36e00 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ntext*);../*.** 
36e10 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69  CAPI3REF: Functi
36e20 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74  on Auxiliary Dat
36e30 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32  a {H16270} <S202
36e40 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  00>.**.** The fo
36e50 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63  llowing two func
36e60 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65  tions may be use
36e70 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20  d by scalar SQL 
36e80 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20  functions to.** 
36e90 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61  associate metada
36ea0 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74  ta with argument
36eb0 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20   values. If the 
36ec0 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61  same value is pa
36ed0 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69  ssed to.** multi
36ee0 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20  ple invocations 
36ef0 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20  of the same SQL 
36f00 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20  function during 
36f10 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c  query execution,
36f20 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63   under.** some c
36f30 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65  ircumstances the
36f40 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61   associated meta
36f50 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73  data may be pres
36f60 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a  erved. This may.
36f70 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20  ** be used, for 
36f80 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20  example, to add 
36f90 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73  a regular-expres
36fa0 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63  sion matching sc
36fb0 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  alar.** function
36fc0 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76  . The compiled v
36fd0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65  ersion of the re
36fe0 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
36ff0 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a   is stored as.**
37000 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69   metadata associ
37010 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51  ated with the SQ
37020 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  L value passed a
37030 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  s the regular ex
37040 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74  pression.** patt
37050 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c  ern.  The compil
37060 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65  ed regular expre
37070 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75  ssion can be reu
37080 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a  sed on multiple.
37090 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f  ** invocations o
370a0 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74  f the same funct
370b0 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20  ion so that the 
370c0 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e  original pattern
370d0 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20   string.** does 
370e0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72  not need to be r
370f0 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63  ecompiled on eac
37100 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  h invocation..**
37110 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
37120 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e  get_auxdata() in
37130 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
37140 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
37150 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73   metadata.** ass
37160 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73  ociated by the s
37170 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
37180 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69  ta() function wi
37190 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d  th the Nth argum
371a0 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20  ent.** value to 
371b0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
371c0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
371d0 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61  . If no metadata
371e0 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a   has been ever.*
371f0 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74  * been set for t
37200 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20  he Nth argument 
37210 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c  of the function,
37220 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65   or if the corre
37230 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63  sponding.** func
37240 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68  tion parameter h
37250 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65  as changed since
37260 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77   the meta-data w
37270 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20  as set,.** then 
37280 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
37290 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20  ata() returns a 
372a0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
372b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
372c0 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e  set_auxdata() in
372d0 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68  terface saves th
372e0 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f  e metadata.** po
372f0 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20  inted to by its 
37300 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73  3rd parameter as
37310 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f   the metadata fo
37320 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72  r the N-th.** ar
37330 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70  gument of the ap
37340 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
37350 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62  d function.  Sub
37360 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73  sequent.** calls
37370 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f   to sqlite3_get_
37380 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20  auxdata() might 
37390 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61  return this data
373a0 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e  , if it has.** n
373b0 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65  ot been destroye
373c0 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  d..** If it is n
373d0 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20  ot NULL, SQLite 
373e0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20  will invoke the 
373f0 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75  destructor.** fu
37400 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20  nction given by 
37410 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
37420 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74  r to sqlite3_set
37430 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a  _auxdata() on.**
37440 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68   the metadata wh
37450 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  en the correspon
37460 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61  ding function pa
37470 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a  rameter changes.
37480 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53  ** or when the S
37490 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d  QL statement com
374a0 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65  pletes, whicheve
374b0 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a  r comes first..*
374c0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66  *.** SQLite is f
374d0 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  ree to call the 
374e0 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64  destructor and d
374f0 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20  rop metadata on 
37500 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  any.** parameter
37510 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e   of any function
37520 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54   at any time.  T
37530 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65  he only guarante
37540 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65  e is that.** the
37550 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c   destructor will
37560 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72   be called befor
37570 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69  e the metadata i
37580 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a  s dropped..**.**
37590 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65   In practice, me
375a0 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72  tadata is preser
375b0 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63  ved between func
375c0 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a  tion calls for.*
375d0 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  * expressions th
375e0 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20  at are constant 
375f0 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e  at compile time.
37600 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c   This includes l
37610 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73  iteral.** values
37620 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c   and SQL variabl
37630 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  es..**.** These 
37640 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65  routines must be
37650 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
37660 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20   same thread in 
37670 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c  which.** the SQL
37680 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e   function is run
37690 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  ning..**.** INVA
376a0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
376b0 31 36 32 37 32 7d 20 54 68 65 20 5b 73 71 6c 69  16272} The [sqli
376c0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
376d0 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  C,N)] interface 
376e0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
376f0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f  r.**          to
37700 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69   metadata associ
37710 61 74 65 64 20 77 69 74 68 20 74 68 65 20 4e 74  ated with the Nt
37720 68 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74  h parameter of t
37730 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 0a  he SQL function.
37740 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 6f 73  **          whos
37750 65 20 63 6f 6e 74 65 78 74 20 69 73 20 43 2c 20  e context is C, 
37760 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65  or NULL if there
37770 20 69 73 20 6e 6f 20 6d 65 74 61 64 61 74 61 20   is no metadata 
37780 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 20 20  associated.**   
37790 20 20 20 20 20 20 20 77 69 74 68 20 74 68 61 74         with that
377a0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
377b0 2a 20 7b 48 31 36 32 37 34 7d 20 54 68 65 20 5b  * {H16274} The [
377c0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
377d0 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 69 6e  ata(C,N,P,D)] in
377e0 74 65 72 66 61 63 65 20 61 73 73 69 67 6e 73 20  terface assigns 
377f0 61 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 20 20  a metadata.**   
37800 20 20 20 20 20 20 20 70 6f 69 6e 74 65 72 20 50         pointer P
37810 20 74 6f 20 74 68 65 20 4e 74 68 20 70 61 72 61   to the Nth para
37820 6d 65 74 65 72 20 6f 66 20 74 68 65 20 53 51 4c  meter of the SQL
37830 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 63   function with c
37840 6f 6e 74 65 78 74 20 43 2e 0a 2a 2a 0a 2a 2a 20  ontext C..**.** 
37850 7b 48 31 36 32 37 36 7d 20 53 51 4c 69 74 65 20  {H16276} SQLite 
37860 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20  will invoke the 
37870 64 65 73 74 72 75 63 74 6f 72 20 44 20 77 69 74  destructor D wit
37880 68 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d  h a single argum
37890 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ent.**          
378a0 77 68 69 63 68 20 69 73 20 74 68 65 20 6d 65 74  which is the met
378b0 61 64 61 74 61 20 70 6f 69 6e 74 65 72 20 50 20  adata pointer P 
378c0 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c  following a call
378d0 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   to.**          
378e0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78  [sqlite3_set_aux
378f0 64 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 77  data(C,N,P,D)] w
37900 68 65 6e 20 53 51 4c 69 74 65 20 63 65 61 73 65  hen SQLite cease
37910 73 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 20 20 20  s to hold.**    
37920 20 20 20 20 20 20 74 68 65 20 6d 65 74 61 64 61        the metada
37930 74 61 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 37  ta..**.** {H1627
37940 37 7d 20 53 51 4c 69 74 65 20 63 65 61 73 65 73  7} SQLite ceases
37950 20 74 6f 20 68 6f 6c 64 20 6d 65 74 61 64 61 74   to hold metadat
37960 61 20 66 6f 72 20 61 6e 20 53 51 4c 20 66 75 6e  a for an SQL fun
37970 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 0a  ction parameter.
37980 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 6e  **          when
37990 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
379a0 61 74 20 70 61 72 61 6d 65 74 65 72 20 63 68 61  at parameter cha
379b0 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  nges..**.** {H16
379c0 32 37 38 7d 20 57 68 65 6e 20 5b 73 71 6c 69 74  278} When [sqlit
379d0 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 43  e3_set_auxdata(C
379e0 2c 4e 2c 50 2c 44 29 5d 20 69 73 20 69 6e 76 6f  ,N,P,D)] is invo
379f0 6b 65 64 2c 20 74 68 65 20 64 65 73 74 72 75 63  ked, the destruc
37a00 74 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  tor.**          
37a10 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 6e  is called for an
37a20 79 20 70 72 69 6f 72 20 6d 65 74 61 64 61 74 61  y prior metadata
37a30 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
37a40 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69   the same functi
37a50 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63  on.**          c
37a60 6f 6e 74 65 78 74 20 43 20 61 6e 64 20 70 61 72  ontext C and par
37a70 61 6d 65 74 65 72 20 4e 2e 0a 2a 2a 0a 2a 2a 20  ameter N..**.** 
37a80 7b 48 31 36 32 37 39 7d 20 53 51 4c 69 74 65 20  {H16279} SQLite 
37a90 77 69 6c 6c 20 63 61 6c 6c 20 64 65 73 74 72 75  will call destru
37aa0 63 74 6f 72 73 20 66 6f 72 20 61 6e 79 20 6d 65  ctors for any me
37ab0 74 61 64 61 74 61 20 69 74 20 69 73 20 68 6f 6c  tadata it is hol
37ac0 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  ding.**         
37ad0 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   in a particular
37ae0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
37af0 6d 65 6e 74 5d 20 53 20 77 68 65 6e 20 65 69 74  ment] S when eit
37b00 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  her.**          
37b10 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53  [sqlite3_reset(S
37b20 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66  )] or [sqlite3_f
37b30 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 63  inalize(S)] is c
37b40 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  alled..*/.SQLITE
37b50 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
37b60 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 73  e3_get_auxdata(s
37b70 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
37b80 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
37b90 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
37ba0 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c  _set_auxdata(sql
37bb0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69  ite3_context*, i
37bc0 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f 69  nt N, void*, voi
37bd0 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 0a  d (*)(void*));..
37be0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
37bf0 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 69 6e   Constants Defin
37c00 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 73 74  ing Special Dest
37c10 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f 72 20  ructor Behavior 
37c20 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31 30 30  {H10280} <S30100
37c30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72  >.**.** These ar
37c40 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 73  e special values
37c50 20 66 6f 72 20 74 68 65 20 64 65 73 74 72 75 63   for the destruc
37c60 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 73 73  tor that is pass
37c70 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a 2a 20  ed in as the.** 
37c80 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 74  final argument t
37c90 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20  o routines like 
37ca0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
37cb0 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74 68 65  blob()].  If the
37cc0 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 61   destructor.** a
37cd0 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54  rgument is SQLIT
37ce0 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d 65 61  E_STATIC, it mea
37cf0 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74  ns that the cont
37d00 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 20 63  ent pointer is c
37d10 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 77  onstant.** and w
37d20 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e 67 65  ill never change
37d30 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 6e  .  It does not n
37d40 65 65 64 20 74 6f 20 62 65 20 64 65 73 74 72 6f  eed to be destro
37d50 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 51 4c  yed.  The.** SQL
37d60 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 76 61  ITE_TRANSIENT va
37d70 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 74  lue means that t
37d80 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20  he content will 
37d90 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 69 6e  likely change in
37da0 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 75 74  .** the near fut
37db0 75 72 65 20 61 6e 64 20 74 68 61 74 20 53 51 4c  ure and that SQL
37dc0 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20  ite should make 
37dd0 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20  its own private 
37de0 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63  copy of.** the c
37df0 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 72 65  ontent before re
37e00 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  turning..**.** T
37e10 68 65 20 74 79 70 65 64 65 66 20 69 73 20 6e 65  he typedef is ne
37e20 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72 6b 20  cessary to work 
37e30 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d 73 20  around problems 
37e40 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 43 2b  in certain.** C+
37e50 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 53 65  + compilers.  Se
37e60 65 20 74 69 63 6b 65 74 20 23 32 31 39 31 2e 0a  e ticket #2191..
37e70 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64 20  */.typedef void 
37e80 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75  (*sqlite3_destru
37e90 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64 2a  ctor_type)(void*
37ea0 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  );.#define SQLIT
37eb0 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 28 28  E_STATIC      ((
37ec0 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74  sqlite3_destruct
37ed0 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65 66 69  or_type)0).#defi
37ee0 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  ne SQLITE_TRANSI
37ef0 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33 5f  ENT   ((sqlite3_
37f00 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29  destructor_type)
37f10 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  -1)../*.** CAPI3
37f20 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54 68 65  REF: Setting The
37f30 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 53 51   Result Of An SQ
37f40 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31 36 34  L Function {H164
37f50 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a  00} <S20200>.**.
37f60 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
37f70 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68  s are used by th
37f80 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69 6e 61  e xFunc or xFina
37f90 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74  l callbacks that
37fa0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 53 51  .** implement SQ
37fb0 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20  L functions and 
37fc0 61 67 67 72 65 67 61 74 65 73 2e 20 20 53 65 65  aggregates.  See
37fd0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  .** [sqlite3_cre
37fe0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20  ate_function()] 
37ff0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  and [sqlite3_cre
38000 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
38010 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 6f  ].** for additio
38020 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
38030 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e  .**.** These fun
38040 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65 72 79  ctions work very
38050 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20 5b   much like the [
38060 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e  parameter bindin
38070 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20  g] family of.** 
38080 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  functions used t
38090 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20 74 6f  o bind values to
380a0 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73   host parameters
380b0 20 69 6e 20 70 72 65 70 61 72 65 64 20 73 74 61   in prepared sta
380c0 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65 66 65  tements..** Refe
380d0 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20 70 61  r to the [SQL pa
380e0 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d 65 6e  rameter] documen
380f0 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74  tation for addit
38100 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
38110 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  n..**.** The sql
38120 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
38130 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74  () interface set
38140 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f  s the result fro
38150 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74  m.** an applicat
38160 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
38170 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 42  tion to be the B
38180 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e  LOB whose conten
38190 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a 2a 20  t is pointed.** 
381a0 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  to by the second
381b0 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 77   parameter and w
381c0 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 73 20  hich is N bytes 
381d0 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 73 20  long where N is 
381e0 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70 61 72  the.** third par
381f0 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ameter..**.** Th
38200 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
38210 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e 74 65  _zeroblob() inte
38220 72 66 61 63 65 73 20 73 65 74 20 74 68 65 20 72  rfaces set the r
38230 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20  esult of.** the 
38240 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
38250 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  ned function to 
38260 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 61 69  be a BLOB contai
38270 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a  ning all zero.**
38280 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62 79 74   bytes and N byt
38290 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65 72  es in size, wher
382a0 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c 75 65  e N is the value
382b0 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61 72 61   of the 2nd para
382c0 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  meter..**.** The
382d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
382e0 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 66 61  double() interfa
382f0 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75  ce sets the resu
38300 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70  lt from.** an ap
38310 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
38320 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
38330 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
38340 74 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65  t value specifie
38350 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e 64 20  d.** by its 2nd 
38360 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
38370 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
38380 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73  lt_error() and s
38390 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
383a0 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69 6f 6e  ror16() function
383b0 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20 69  s.** cause the i
383c0 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20 66  mplemented SQL f
383d0 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f 77  unction to throw
383e0 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a   an exception..*
383f0 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68  * SQLite uses th
38400 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64  e string pointed
38410 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 32 6e   to by the.** 2n
38420 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73  d parameter of s
38430 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
38440 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 65 33  ror() or sqlite3
38450 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
38460 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 78 74  ).** as the text
38470 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73   of an error mes
38480 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20 69 6e  sage.  SQLite in
38490 74 65 72 70 72 65 74 73 20 74 68 65 20 65 72 72  terprets the err
384a0 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 73 74  or.** message st
384b0 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65  ring from sqlite
384c0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
384d0 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c 69 74   as UTF-8. SQLit
384e0 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74 73 20  e.** interprets 
384f0 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20  the string from 
38500 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
38510 72 72 6f 72 31 36 28 29 20 61 73 20 55 54 46 2d  rror16() as UTF-
38520 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a 2a 20  16 in native.** 
38530 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49 66 20  byte order.  If 
38540 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  the third parame
38550 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
38560 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a  esult_error().**
38570 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75   or sqlite3_resu
38580 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73 20  lt_error16() is 
38590 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 51  negative then SQ
385a0 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20 74 68  Lite takes as th
385b0 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61  e error.** messa
385c0 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70 20 74  ge all text up t
385d0 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 74  hrough the first
385e0 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e   zero character.
385f0 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 72 64  .** If the third
38600 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
38610 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
38620 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74  or() or.** sqlit
38630 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
38640 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74  6() is non-negat
38650 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20  ive then SQLite 
38660 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e 79 0a  takes that many.
38670 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 63 68  ** bytes (not ch
38680 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d 20 74  aracters) from t
38690 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  he 2nd parameter
386a0 20 61 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65   as the error me
386b0 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71  ssage..** The sq
386c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
386d0 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  or() and sqlite3
386e0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
386f0 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 6d 61  ).** routines ma
38700 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f 70  ke a private cop
38710 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 6d  y of the error m
38720 65 73 73 61 67 65 20 74 65 78 74 20 62 65 66 6f  essage text befo
38730 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74 75 72  re.** they retur
38740 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 63  n.  Hence, the c
38750 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
38760 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65 20 6f  can deallocate o
38770 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 65 20  r.** modify the 
38780 74 65 78 74 20 61 66 74 65 72 20 74 68 65 79 20  text after they 
38790 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 68  return without h
387a0 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  arm..** The sqli
387b0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
387c0 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69 6f 6e  _code() function
387d0 20 63 68 61 6e 67 65 73 20 74 68 65 20 65 72 72   changes the err
387e0 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75 72  or code.** retur
387f0 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 73  ned by SQLite as
38800 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e 20   a result of an 
38810 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e 63 74  error in a funct
38820 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75 6c 74  ion.  By default
38830 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63  ,.** the error c
38840 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45 52  ode is SQLITE_ER
38850 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71 75 65  ROR.  A subseque
38860 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  nt call to sqlit
38870 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
38880 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f  ).** or sqlite3_
38890 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29  result_error16()
388a0 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72 6f   resets the erro
388b0 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 45  r code to SQLITE
388c0 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68  _ERROR..**.** Th
388d0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
388e0 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65 72 66  _toobig() interf
388f0 61 63 65 20 63 61 75 73 65 73 20 53 51 4c 69 74  ace causes SQLit
38900 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 72  e to throw an er
38910 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  ror.** indicatin
38920 67 20 74 68 61 74 20 61 20 73 74 72 69 6e 67 20  g that a string 
38930 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20 6c 6f  or BLOB is to lo
38940 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e 74 2e  ng to represent.
38950 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
38960 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28  e3_result_nomem(
38970 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73  ) interface caus
38980 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72  es SQLite to thr
38990 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 69  ow an error.** i
389a0 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61  ndicating that a
389b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
389c0 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a  on failed..**.**
389d0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
389e0 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72 66  ult_int() interf
389f0 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74  ace sets the ret
38a00 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  urn value.** of 
38a10 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
38a20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
38a30 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62 69   to be the 32-bi
38a40 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
38a50 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e 20  .** value given 
38a60 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 6d  in the 2nd argum
38a70 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ent..** The sqli
38a80 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
38a90 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74  () interface set
38aa0 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  s the return val
38ab0 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 70  ue.** of the app
38ac0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
38ad0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
38ae0 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65  the 64-bit signe
38af0 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 6c  d integer.** val
38b00 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  ue given in the 
38b10 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  2nd argument..**
38b20 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
38b30 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e  result_null() in
38b40 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65  terface sets the
38b50 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
38b60 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   of the applicat
38b70 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
38b80 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c 4c 2e  tion to be NULL.
38b90 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
38ba0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 29  e3_result_text()
38bb0 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  , sqlite3_result
38bc0 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 73 71  _text16(),.** sq
38bd0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
38be0 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73 71 6c  t16le(), and sql
38bf0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
38c00 31 36 62 65 28 29 20 69 6e 74 65 72 66 61 63 65  16be() interface
38c10 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72 65 74  s.** set the ret
38c20 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65  urn value of the
38c30 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
38c40 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ined function to
38c50 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20 73 74   be.** a text st
38c60 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 72 65  ring which is re
38c70 70 72 65 73 65 6e 74 65 64 20 61 73 20 55 54 46  presented as UTF
38c80 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74 69 76  -8, UTF-16 nativ
38c90 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a 2a 2a  e byte order,.**
38ca0 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 65   UTF-16 little e
38cb0 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36  ndian, or UTF-16
38cc0 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72 65 73   big endian, res
38cd0 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 53 51  pectively..** SQ
38ce0 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65 20 74  Lite takes the t
38cf0 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f 6d 20  ext result from 
38d00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
38d10 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e 64 20  from.** the 2nd 
38d20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65  parameter of the
38d30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
38d40 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73  text* interfaces
38d50 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20  ..** If the 3rd 
38d60 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
38d70 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
38d80 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73  text* interfaces
38d90 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76 65 2c  .** is negative,
38da0 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b   then SQLite tak
38db0 65 73 20 72 65 73 75 6c 74 20 74 65 78 74 20 66  es result text f
38dc0 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 61  rom the 2nd para
38dd0 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75 67 68  meter.** through
38de0 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20   the first zero 
38df0 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66  character..** If
38e00 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74   the 3rd paramet
38e10 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  er to the sqlite
38e20 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69  3_result_text* i
38e30 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73 20  nterfaces.** is 
38e40 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68  non-negative, th
38e50 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74 65 73  en as many bytes
38e60 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 72 73   (not characters
38e70 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a 2a 2a  ) of the text.**
38e80 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74   pointed to by t
38e90 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  he 2nd parameter
38ea0 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20 74 68   are taken as th
38eb0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
38ec0 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f  fined.** functio
38ed0 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20  n result..** If 
38ee0 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
38ef0 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  r to the sqlite3
38f00 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e  _result_text* in
38f10 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73  terfaces.** or s
38f20 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
38f30 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c  ob is a non-NULL
38f40 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 53   pointer, then S
38f50 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 61 74  QLite calls that
38f60 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73 20  .** function as 
38f70 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 6f  the destructor o
38f80 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c  n the text or BL
38f90 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e 20 69  OB result when i
38fa0 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65  t has.** finishe
38fb0 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65 73  d using that res
38fc0 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34  ult..** If the 4
38fd0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
38fe0 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  the sqlite3_resu
38ff0 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61  lt_text* interfa
39000 63 65 73 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  ces or.** sqlite
39010 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73  3_result_blob is
39020 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e   the special con
39030 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41  stant SQLITE_STA
39040 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  TIC, then SQLite
39050 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74  .** assumes that
39060 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f   the text or BLO
39070 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63  B result is in c
39080 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e  onstant space an
39090 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f  d does not.** co
390a0 70 79 20 74 68 65 20 69 74 20 6f 72 20 63 61 6c  py the it or cal
390b0 6c 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 77  l a destructor w
390c0 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73  hen it has finis
390d0 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72  hed using that r
390e0 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65  esult..** If the
390f0 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
39100 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
39110 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
39120 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69  faces.** or sqli
39130 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
39140 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63  is the special c
39150 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54  onstant SQLITE_T
39160 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e  RANSIENT.** then
39170 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20   SQLite makes a 
39180 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75  copy of the resu
39190 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  lt into space ob
391a0 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66  tained from.** f
391b0 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
391c0 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74  loc()] before it
391d0 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20   returns..**.** 
391e0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
391f0 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72  lt_value() inter
39200 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65  face sets the re
39210 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61  sult of.** the a
39220 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
39230 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
39240 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20  e a copy the.** 
39250 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c  [unprotected sql
39260 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
39270 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ct specified by 
39280 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
39290 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74  r.  The.** sqlit
392a0 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
392b0 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65  ) interface make
392c0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
392d0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a  [sqlite3_value].
392e0 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b  ** so that the [
392f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73  sqlite3_value] s
39300 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
39310 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68  parameter may ch
39320 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65  ange or.** be de
39330 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20  allocated after 
39340 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
39350 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20 77  alue() returns w
39360 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20  ithout harm..** 
39370 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c  A [protected sql
39380 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
39390 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65  ct may always be
393a0 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a   used where an.*
393b0 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73  * [unprotected s
393c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
393d0 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65 64  ject is required
393e0 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b  , so either.** k
393f0 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f  ind of [sqlite3_
39400 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61  value] object ca
39410 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74  n be used with t
39420 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  his interface..*
39430 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f  *.** If these ro
39440 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65  utines are calle
39450 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  d from within th
39460 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65  e different thre
39470 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f  ad.** than the o
39480 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ne containing th
39490 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
394a0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
394b0 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20  hat received.** 
394c0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  the [sqlite3_con
394d0 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74  text] pointer, t
394e0 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75  he results are u
394f0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
39500 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
39510 2a 20 7b 48 31 36 34 30 33 7d 20 54 68 65 20 64  * {H16403} The d
39520 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20 76 61  efault return va
39530 6c 75 65 20 66 72 6f 6d 20 61 6e 79 20 53 51 4c  lue from any SQL
39540 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c   function is NUL
39550 4c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 30 36  L..**.** {H16406
39560 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72  } The [sqlite3_r
39570 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e  esult_blob(C,V,N
39580 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  ,D)] interface c
39590 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20  hanges the.**   
395a0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61         return va
395b0 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  lue of function 
395c0 43 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 74  C to be a BLOB t
395d0 68 61 74 20 69 73 20 4e 20 62 79 74 65 73 0a 2a  hat is N bytes.*
395e0 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 6c 65  *          in le
395f0 6e 67 74 68 20 61 6e 64 20 77 69 74 68 20 63 6f  ngth and with co
39600 6e 74 65 6e 74 20 70 6f 69 6e 74 65 64 20 74 6f  ntent pointed to
39610 20 62 79 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31   by V..**.** {H1
39620 36 34 30 39 7d 20 54 68 65 20 5b 73 71 6c 69 74  6409} The [sqlit
39630 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
39640 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 61 63 65  (C,V)] interface
39650 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20   changes the.** 
39660 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
39670 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f  value of functio
39680 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 66 6c  n C to be the fl
39690 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
396a0 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ue V..**.** {H16
396b0 34 31 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  412} The [sqlite
396c0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 43  3_result_error(C
396d0 2c 56 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  ,V,N)] interface
396e0 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 74   changes the ret
396f0 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  urn.**          
39700 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f  value of functio
39710 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63  n C to be an exc
39720 65 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f  eption with erro
39730 72 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20  r code.**       
39740 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52     [SQLITE_ERROR
39750 5d 20 61 6e 64 20 61 20 55 54 46 2d 38 20 65 72  ] and a UTF-8 er
39760 72 6f 72 20 6d 65 73 73 61 67 65 20 63 6f 70 69  ror message copi
39770 65 64 20 66 72 6f 6d 20 56 20 75 70 20 74 6f 20  ed from V up to 
39780 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
39790 66 69 72 73 74 20 7a 65 72 6f 20 62 79 74 65 20  first zero byte 
397a0 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65 73  or until N bytes
397b0 20 61 72 65 20 72 65 61 64 20 69 66 20 4e 20 69   are read if N i
397c0 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a  s positive..**.*
397d0 2a 20 7b 48 31 36 34 31 35 7d 20 54 68 65 20 5b  * {H16415} The [
397e0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
397f0 72 72 6f 72 31 36 28 43 2c 56 2c 4e 29 5d 20 69  rror16(C,V,N)] i
39800 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73  nterface changes
39810 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20   the return.**  
39820 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66          value of
39830 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62   function C to b
39840 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 77  e an exception w
39850 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a  ith error code.*
39860 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49  *          [SQLI
39870 54 45 5f 45 52 52 4f 52 5d 20 61 6e 64 20 61 20  TE_ERROR] and a 
39880 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79  UTF-16 native by
39890 74 65 20 6f 72 64 65 72 20 65 72 72 6f 72 20 6d  te order error m
398a0 65 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20  essage.**       
398b0 20 20 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 56     copied from V
398c0 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74   up to the first
398d0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
398e0 20 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65   or until N byte
398f0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72  s.**          ar
39900 65 20 72 65 61 64 20 69 66 20 4e 20 69 73 20 70  e read if N is p
39910 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b  ositive..**.** {
39920 48 31 36 34 31 38 7d 20 54 68 65 20 5b 73 71 6c  H16418} The [sql
39930 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
39940 72 5f 74 6f 6f 62 69 67 28 43 29 5d 20 69 6e 74  r_toobig(C)] int
39950 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74  erface changes t
39960 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20  he return.**    
39970 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 74        value of t
39980 68 65 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f  he function C to
39990 20 62 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e   be an exception
399a0 20 77 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65   with error code
399b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51  .**          [SQ
399c0 4c 49 54 45 5f 54 4f 4f 42 49 47 5d 20 61 6e 64  LITE_TOOBIG] and
399d0 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
399e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a  error message..*
399f0 2a 0a 2a 2a 20 7b 48 31 36 34 32 31 7d 20 54 68  *.** {H16421} Th
39a00 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e [sqlite3_resul
39a10 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 43 29  t_error_nomem(C)
39a20 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e  ] interface chan
39a30 67 65 73 20 74 68 65 20 72 65 74 75 72 6e 0a 2a  ges the return.*
39a40 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 65  *          value
39a50 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
39a60 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63 65   C to be an exce
39a70 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f 72  ption with error
39a80 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20 20   code.**        
39a90 20 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d    [SQLITE_NOMEM]
39aa0 20 61 6e 64 20 61 6e 20 61 70 70 72 6f 70 72 69   and an appropri
39ab0 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ate error messag
39ac0 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32 34  e..**.** {H16424
39ad0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72  } The [sqlite3_r
39ae0 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
39af0 28 43 2c 45 29 5d 20 69 6e 74 65 72 66 61 63 65  (C,E)] interface
39b00 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 74   changes the ret
39b10 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  urn.**          
39b20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 75 6e  value of the fun
39b30 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e  ction C to be an
39b40 20 65 78 63 65 70 74 69 6f 6e 20 77 69 74 68 20   exception with 
39b50 65 72 72 6f 72 20 63 6f 64 65 20 45 2e 0a 2a 2a  error code E..**
39b60 20 20 20 20 20 20 20 20 20 20 54 68 65 20 65 72            The er
39b70 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74  ror message text
39b80 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
39b90 2a 0a 2a 2a 20 7b 48 31 36 34 32 37 7d 20 54 68  *.** {H16427} Th
39ba0 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e [sqlite3_resul
39bb0 74 5f 69 6e 74 28 43 2c 56 29 5d 20 69 6e 74 65  t_int(C,V)] inte
39bc0 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68  rface changes th
39bd0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  e.**          re
39be0 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75  turn value of fu
39bf0 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74  nction C to be t
39c00 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65  he 32-bit intege
39c10 72 20 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a  r value V..**.**
39c20 20 7b 48 31 36 34 33 30 7d 20 54 68 65 20 5b 73   {H16430} The [s
39c30 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
39c40 74 36 34 28 43 2c 56 29 5d 20 69 6e 74 65 72 66  t64(C,V)] interf
39c50 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a  ace changes the.
39c60 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  **          retu
39c70 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63  rn value of func
39c80 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65  tion C to be the
39c90 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20   64-bit integer 
39ca0 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b  value V..**.** {
39cb0 48 31 36 34 33 33 7d 20 54 68 65 20 5b 73 71 6c  H16433} The [sql
39cc0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
39cd0 28 43 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  (C)] interface c
39ce0 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20  hanges the.**   
39cf0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61         return va
39d00 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  lue of function 
39d10 43 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a  C to be NULL..**
39d20 0a 2a 2a 20 7b 48 31 36 34 33 36 7d 20 54 68 65  .** {H16436} The
39d30 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
39d40 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 20  _text(C,V,N,D)] 
39d50 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65  interface change
39d60 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
39d70 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f    return value o
39d80 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20  f function C to 
39d90 62 65 20 74 68 65 20 55 54 46 2d 38 20 73 74 72  be the UTF-8 str
39da0 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
39db0 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73  V up to the firs
39dc0 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20 6e  t zero if N is n
39dd0 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20 20  egative.**      
39de0 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73 74      or the first
39df0 20 4e 20 62 79 74 65 73 20 6f 66 20 56 20 69 66   N bytes of V if
39e00 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69   N is non-negati
39e10 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 33  ve..**.** {H1643
39e20 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  9} The [sqlite3_
39e30 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43 2c  result_text16(C,
39e40 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 63  V,N,D)] interfac
39e50 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a  e changes the.**
39e60 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
39e70 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69   value of functi
39e80 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 55  on C to be the U
39e90 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74  TF-16 native byt
39ea0 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 20 20  e order.**      
39eb0 20 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20      string V up 
39ec0 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72  to the first zer
39ed0 6f 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69  o if N is negati
39ee0 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  ve.**          o
39ef0 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79  r the first N by
39f00 74 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73  tes of V if N is
39f10 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a   non-negative..*
39f20 2a 0a 2a 2a 20 7b 48 31 36 34 34 32 7d 20 54 68  *.** {H16442} Th
39f30 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e [sqlite3_resul
39f40 74 5f 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e  t_text16be(C,V,N
39f50 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  ,D)] interface c
39f60 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20  hanges the.**   
39f70 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61         return va
39f80 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  lue of function 
39f90 43 20 74 6f 20 62 65 20 74 68 65 20 55 54 46 2d  C to be the UTF-
39fa0 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a  16 big-endian.**
39fb0 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67            string
39fc0 20 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72   V up to the fir
39fd0 73 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20  st zero if N is 
39fe0 6e 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20  negative.**     
39ff0 20 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73       or the firs
3a000 74 20 4e 20 62 79 74 65 73 20 6f 72 20 56 20 69  t N bytes or V i
3a010 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74  f N is non-negat
3a020 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34  ive..**.** {H164
3a030 34 35 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  45} The [sqlite3
3a040 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
3a050 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72  (C,V,N,D)] inter
3a060 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65  face changes the
3a070 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74  .**          ret
3a080 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e  urn value of fun
3a090 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68  ction C to be th
3a0a0 65 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d  e UTF-16 little-
3a0b0 65 6e 64 69 61 6e 0a 2a 2a 20 20 20 20 20 20 20  endian.**       
3a0c0 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20 74     string V up t
3a0d0 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  o the first zero
3a0e0 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69 76   if N is negativ
3a0f0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  e.**          or
3a100 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 74   the first N byt
3a110 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73 20  es of V if N is 
3a120 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a  non-negative..**
3a130 0a 2a 2a 20 7b 48 31 36 34 34 38 7d 20 54 68 65  .** {H16448} The
3a140 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
3a150 5f 76 61 6c 75 65 28 43 2c 56 29 5d 20 69 6e 74  _value(C,V)] int
3a160 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74  erface changes t
3a170 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72  he.**          r
3a180 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66  eturn value of f
3a190 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20  unction C to be 
3a1a0 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 64  the [unprotected
3a1b0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a   sqlite3_value].
3a1c0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65  **          obje
3a1d0 63 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ct V..**.** {H16
3a1e0 34 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  451} The [sqlite
3a1f0 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  3_result_zeroblo
3a200 62 28 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63  b(C,N)] interfac
3a210 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a  e changes the.**
3a220 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
3a230 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69   value of functi
3a240 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 4e 2d  on C to be an N-
3a250 62 79 74 65 20 42 4c 4f 42 20 6f 66 20 61 6c 6c  byte BLOB of all
3a260 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48   zeros..**.** {H
3a270 31 36 34 35 34 7d 20 54 68 65 20 5b 73 71 6c 69  16454} The [sqli
3a280 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
3a290 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
3a2a0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
3a2b0 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  )].**          i
3a2c0 6e 74 65 72 66 61 63 65 73 20 6d 61 6b 65 20 61  nterfaces make a
3a2d0 20 63 6f 70 79 20 6f 66 20 74 68 65 69 72 20 65   copy of their e
3a2e0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72  rror message str
3a2f0 69 6e 67 73 20 62 65 66 6f 72 65 0a 2a 2a 20 20  ings before.**  
3a300 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 69 6e          returnin
3a310 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 35 37  g..**.** {H16457
3a320 7d 20 49 66 20 74 68 65 20 44 20 64 65 73 74 72  } If the D destr
3a330 75 63 74 6f 72 20 70 61 72 61 6d 65 74 65 72 20  uctor parameter 
3a340 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  to [sqlite3_resu
3a350 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e 2c 44 29  lt_blob(C,V,N,D)
3a360 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  ],.**          [
3a370 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
3a380 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 2c 20 5b  ext(C,V,N,D)], [
3a390 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
3a3a0 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44 29 5d 2c  ext16(C,V,N,D)],
3a3b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
3a3c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
3a3d0 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44 29 5d 2c  t16be(C,V,N,D)],
3a3e0 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
3a3f0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
3a400 74 65 78 74 31 36 6c 65 28 43 2c 56 2c 4e 2c 44  text16le(C,V,N,D
3a410 29 5d 20 69 73 20 74 68 65 20 63 6f 6e 73 74 61  )] is the consta
3a420 6e 74 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49  nt [SQLITE_STATI
3a430 43 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  C].**          t
3a440 68 65 6e 20 6e 6f 20 64 65 73 74 72 75 63 74 6f  hen no destructo
3a450 72 20 69 73 20 65 76 65 72 20 63 61 6c 6c 65 64  r is ever called
3a460 20 6f 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20   on the pointer 
3a470 56 20 61 6e 64 20 53 51 4c 69 74 65 0a 2a 2a 20  V and SQLite.** 
3a480 20 20 20 20 20 20 20 20 20 61 73 73 75 6d 65 73           assumes
3a490 20 74 68 61 74 20 56 20 69 73 20 69 6d 6d 75 74   that V is immut
3a4a0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  able..**.** {H16
3a4b0 34 36 30 7d 20 49 66 20 74 68 65 20 44 20 64 65  460} If the D de
3a4c0 73 74 72 75 63 74 6f 72 20 70 61 72 61 6d 65 74  structor paramet
3a4d0 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  er to [sqlite3_r
3a4e0 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e  esult_blob(C,V,N
3a4f0 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ,D)],.**        
3a500 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c    [sqlite3_resul
3a510 74 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d  t_text(C,V,N,D)]
3a520 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  , [sqlite3_resul
3a530 74 5f 74 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44  t_text16(C,V,N,D
3a540 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
3a550 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
3a560 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44  text16be(C,V,N,D
3a570 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  )], or.**       
3a580 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75     [sqlite3_resu
3a590 6c 74 5f 74 65 78 74 31 36 6c 65 28 43 2c 56 2c  lt_text16le(C,V,
3a5a0 4e 2c 44 29 5d 20 69 73 20 74 68 65 20 63 6f 6e  N,D)] is the con
3a5b0 73 74 61 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20  stant.**        
3a5c0 20 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49    [SQLITE_TRANSI
3a5d0 45 4e 54 5d 20 74 68 65 6e 20 74 68 65 20 69 6e  ENT] then the in
3a5e0 74 65 72 66 61 63 65 73 20 6d 61 6b 65 73 20 61  terfaces makes a
3a5f0 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20   copy of the.** 
3a600 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74           content
3a610 20 6f 66 20 56 20 61 6e 64 20 72 65 74 61 69 6e   of V and retain
3a620 73 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 0a 2a  s the copy..**.*
3a630 2a 20 7b 48 31 36 34 36 33 7d 20 49 66 20 74 68  * {H16463} If th
3a640 65 20 44 20 64 65 73 74 72 75 63 74 6f 72 20 70  e D destructor p
3a650 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
3a660 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
3a670 28 43 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20  (C,V,N,D)],.**  
3a680 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
3a690 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 43 2c 56  _result_text(C,V
3a6a0 2c 4e 2c 44 29 5d 2c 20 5b 73 71 6c 69 74 65 33  ,N,D)], [sqlite3
3a6b0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43  _result_text16(C
3a6c0 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20  ,V,N,D)],.**    
3a6d0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72        [sqlite3_r
3a6e0 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 43  esult_text16be(C
3a6f0 2c 56 2c 4e 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20  ,V,N,D)], or.** 
3a700 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
3a710 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
3a720 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 73 20 73  e(C,V,N,D)] is s
3a730 6f 6d 65 20 76 61 6c 75 65 20 6f 74 68 65 72 20  ome value other 
3a740 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  than.**         
3a750 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b   the constants [
3a760 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 20 61  SQLITE_STATIC] a
3a770 6e 64 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53  nd [SQLITE_TRANS
3a780 49 45 4e 54 5d 20 74 68 65 6e 0a 2a 2a 20 20 20  IENT] then.**   
3a790 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 77 69         SQLite wi
3a7a0 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65  ll invoke the de
3a7b0 73 74 72 75 63 74 6f 72 20 44 20 77 69 74 68 20  structor D with 
3a7c0 56 20 61 73 20 69 74 73 20 6f 6e 6c 79 20 61 72  V as its only ar
3a7d0 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20  gument.**       
3a7e0 20 20 20 77 68 65 6e 20 69 74 20 68 61 73 20 66     when it has f
3a7f0 69 6e 69 73 68 65 64 20 77 69 74 68 20 74 68 65  inished with the
3a800 20 56 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c   V value..*/.SQL
3a810 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
3a820 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
3a830 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3a840 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
3a850 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  int, void(*)(voi
3a860 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
3a870 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
3a880 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69  sult_double(sqli
3a890 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f  te3_context*, do
3a8a0 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50  uble);.SQLITE_AP
3a8b0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
3a8c0 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69  esult_error(sqli
3a8d0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
3a8e0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b  nst char*, int);
3a8f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3a900 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3a910 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f  error16(sqlite3_
3a920 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  context*, const 
3a930 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  void*, int);.SQL
3a940 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
3a950 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
3a960 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33  r_toobig(sqlite3
3a970 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49  _context*);.SQLI
3a980 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
3a990 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
3a9a0 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63  _nomem(sqlite3_c
3a9b0 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45  ontext*);.SQLITE
3a9c0 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
3a9d0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
3a9e0 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ode(sqlite3_cont
3a9f0 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ext*, int);.SQLI
3aa00 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
3aa10 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73  te3_result_int(s
3aa20 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
3aa30 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50   int);.SQLITE_AP
3aa40 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
3aa50 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69  esult_int64(sqli
3aa60 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71  te3_context*, sq
3aa70 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51  lite3_int64);.SQ
3aa80 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
3aa90 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
3aaa0 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  l(sqlite3_contex
3aab0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  t*);.SQLITE_API 
3aac0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
3aad0 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33  ult_text(sqlite3
3aae0 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  _context*, const
3aaf0 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69   char*, int, voi
3ab00 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
3ab10 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
3ab20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
3ab30 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  t16(sqlite3_cont
3ab40 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ext*, const void
3ab50 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28  *, int, void(*)(
3ab60 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  void*));.SQLITE_
3ab70 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
3ab80 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
3ab90 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3aba0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
3abb0 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
3abc0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *));.SQLITE_API 
3abd0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
3abe0 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c  ult_text16be(sql
3abf0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63  ite3_context*, c
3ac00 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c  onst void*, int,
3ac10 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
3ac20 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3ac30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3ac40 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f  value(sqlite3_co
3ac50 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f  ntext*, sqlite3_
3ac60 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
3ac70 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
3ac80 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
3ac90 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3aca0 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a  *, int n);../*.*
3acb0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69  * CAPI3REF: Defi
3acc0 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67  ne New Collating
3acd0 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 36   Sequences {H166
3ace0 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a  00} <S20300>.**.
3acf0 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f  ** These functio
3ad00 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61  ns are used to a
3ad10 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e  dd new collation
3ad20 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68   sequences to th
3ad30 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  e.** [database c
3ad40 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69  onnection] speci
3ad50 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 73  fied as the firs
3ad60 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  t argument..**.*
3ad70 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
3ad80 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20  e new collation 
3ad90 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 63  sequence is spec
3ada0 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d 38  ified as a UTF-8
3adb0 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73   string.** for s
3adc0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
3add0 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71  llation() and sq
3ade0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
3adf0 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61  lation_v2().** a
3ae00 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69  nd a UTF-16 stri
3ae10 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63  ng for sqlite3_c
3ae20 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
3ae30 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 65  6(). In all case
3ae40 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 73  s.** the name is
3ae50 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
3ae60 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61  econd function a
3ae70 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
3ae80 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
3ae90 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20  t may be one of 
3aea0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b 53  the constants [S
3aeb0 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20  QLITE_UTF8],.** 
3aec0 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d  [SQLITE_UTF16LE]
3aed0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46 31   or [SQLITE_UTF1
3aee0 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e 67  6BE], indicating
3aef0 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d 73   that the user-s
3af00 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74 69  upplied.** routi
3af10 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62 65  ne expects to be
3af20 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72 73   passed pointers
3af30 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63 6f   to strings enco
3af40 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38 2c  ded using UTF-8,
3af50 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c  .** UTF-16 littl
3af60 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46  e-endian, or UTF
3af70 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c 20  -16 big-endian, 
3af80 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68  respectively. Th
3af90 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 6d  e.** third argum
3afa0 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20 62  ent might also b
3afb0 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  e [SQLITE_UTF16_
3afc0 41 4c 49 47 4e 45 44 5d 20 74 6f 20 69 6e 64 69  ALIGNED] to indi
3afd0 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 74 68 65  cate that.** the
3afe0 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73   routine expects
3aff0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 2d   pointers to 16-
3b000 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 64  bit word aligned
3b010 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 55   strings.** of U
3b020 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74  TF-16 in the nat
3b030 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 6f  ive byte order o
3b040 66 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 75  f the host compu
3b050 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69  ter..**.** A poi
3b060 6e 74 65 72 20 74 6f 20 74 68 65 20 75 73 65 72  nter to the user
3b070 20 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e   supplied routin
3b080 65 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 64  e must be passed
3b090 20 61 73 20 74 68 65 20 66 69 66 74 68 0a 2a 2a   as the fifth.**
3b0a0 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 69   argument.  If i
3b0b0 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20  t is NULL, this 
3b0c0 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 64  is the same as d
3b0d0 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c  eleting the coll
3b0e0 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63  ation.** sequenc
3b0f0 65 20 28 73 6f 20 74 68 61 74 20 53 51 4c 69 74  e (so that SQLit
3b100 65 20 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74  e cannot call it
3b110 20 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61   anymore)..** Ea
3b120 63 68 20 74 69 6d 65 20 74 68 65 20 61 70 70 6c  ch time the appl
3b130 69 63 61 74 69 6f 6e 20 73 75 70 70 6c 69 65 64  ication supplied
3b140 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76   function is inv
3b150 6f 6b 65 64 2c 20 69 74 20 69 73 20 70 61 73 73  oked, it is pass
3b160 65 64 0a 2a 2a 20 61 73 20 69 74 73 20 66 69 72  ed.** as its fir
3b170 73 74 20 70 61 72 61 6d 65 74 65 72 20 61 20 63  st parameter a c
3b180 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a  opy of the void*
3b190 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
3b1a0 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a  ourth argument.*
3b1b0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65  * to sqlite3_cre
3b1c0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20  ate_collation() 
3b1d0 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  or sqlite3_creat
3b1e0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e  e_collation16().
3b1f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69  .**.** The remai
3b200 6e 69 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 74  ning arguments t
3b210 6f 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  o the applicatio
3b220 6e 2d 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69  n-supplied routi
3b230 6e 65 20 61 72 65 20 74 77 6f 20 73 74 72 69 6e  ne are two strin
3b240 67 73 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 72  gs,.** each repr
3b250 65 73 65 6e 74 65 64 20 62 79 20 61 20 28 6c 65  esented by a (le
3b260 6e 67 74 68 2c 20 64 61 74 61 29 20 70 61 69 72  ngth, data) pair
3b270 20 61 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e 20   and encoded in 
3b280 74 68 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  the encoding.** 
3b290 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20  that was passed 
3b2a0 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67  as the third arg
3b2b0 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 63  ument when the c
3b2c0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
3b2d0 65 20 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 65  e was.** registe
3b2e0 72 65 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20  red. {END}  The 
3b2f0 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69  application defi
3b300 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f  ned collation ro
3b310 75 74 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20  utine should.** 
3b320 72 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c  return negative,
3b330 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 76   zero or positiv
3b340 65 20 69 66 20 74 68 65 20 66 69 72 73 74 20 73  e if the first s
3b350 74 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68  tring is less th
3b360 61 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c  an,.** equal to,
3b370 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e   or greater than
3b380 20 74 68 65 20 73 65 63 6f 6e 64 20 73 74 72 69   the second stri
3b390 6e 67 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e 47  ng. i.e. (STRING
3b3a0 31 20 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a  1 - STRING2)..**
3b3b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
3b3c0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
3b3d0 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65  _v2() works like
3b3e0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
3b3f0 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65  collation().** e
3b400 78 63 65 70 74 20 74 68 61 74 20 69 74 20 74 61  xcept that it ta
3b410 6b 65 73 20 61 6e 20 65 78 74 72 61 20 61 72 67  kes an extra arg
3b420 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61  ument which is a
3b430 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 0a   destructor for.
3b440 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ** the collation
3b450 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74 6f  .  The destructo
3b460 72 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  r is called when
3b470 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69   the collation i
3b480 73 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 61  s.** destroyed a
3b490 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 20 63  nd is passed a c
3b4a0 6f 70 79 20 6f 66 20 74 68 65 20 66 6f 75 72 74  opy of the fourt
3b4b0 68 20 70 61 72 61 6d 65 74 65 72 20 76 6f 69 64  h parameter void
3b4c0 2a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20  * pointer.** of 
3b4d0 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  the sqlite3_crea
3b4e0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
3b4f0 29 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73  )..** Collations
3b500 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 77   are destroyed w
3b510 68 65 6e 20 74 68 65 79 20 61 72 65 20 6f 76 65  hen they are ove
3b520 72 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65 72  rridden by later
3b530 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a   calls to the.**
3b540 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74   collation creat
3b550 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72  ion functions or
3b560 20 77 68 65 6e 20 74 68 65 20 5b 64 61 74 61 62   when the [datab
3b570 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
3b580 69 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69  is closed.** usi
3b590 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73  ng [sqlite3_clos
3b5a0 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  e()]..**.** INVA
3b5b0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
3b5c0 31 36 36 30 33 7d 20 41 20 73 75 63 63 65 73 73  16603} A success
3b5d0 66 75 6c 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a  ful call to the.
3b5e0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
3b5f0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
3b600 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50  ation_v2(B,X,E,P
3b610 2c 46 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65  ,F,D)] interface
3b620 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67  .**          reg
3b630 69 73 74 65 72 73 20 66 75 6e 63 74 69 6f 6e 20  isters function 
3b640 46 20 61 73 20 74 68 65 20 63 6f 6d 70 61 72 69  F as the compari
3b650 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  son function use
3b660 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  d to.**         
3b670 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 6c 6c 61   implement colla
3b680 74 69 6f 6e 20 58 20 6f 6e 20 74 68 65 20 5b 64  tion X on the [d
3b690 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3b6a0 6f 6e 5d 20 42 20 66 6f 72 0a 2a 2a 20 20 20 20  on] B for.**    
3b6b0 20 20 20 20 20 20 64 61 74 61 62 61 73 65 73 20        databases 
3b6c0 68 61 76 69 6e 67 20 65 6e 63 6f 64 69 6e 67 20  having encoding 
3b6d0 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30 34  E..**.** {H16604
3b6e0 7d 20 53 51 4c 69 74 65 20 75 6e 64 65 72 73 74  } SQLite underst
3b6f0 61 6e 64 73 20 74 68 65 20 58 20 70 61 72 61 6d  ands the X param
3b700 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20  eter to.**      
3b710 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 72 65      [sqlite3_cre
3b720 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
3b730 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 20 61  (B,X,E,P,F,D)] a
3b740 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  s a zero-termina
3b750 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ted.**          
3b760 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 6e 20  UTF-8 string in 
3b770 77 68 69 63 68 20 63 61 73 65 20 69 73 20 69 67  which case is ig
3b780 6e 6f 72 65 64 20 66 6f 72 20 41 53 43 49 49 20  nored for ASCII 
3b790 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a  characters and.*
3b7a0 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 73 69  *          is si
3b7b0 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 20 6e 6f  gnificant for no
3b7c0 6e 2d 41 53 43 49 49 20 63 68 61 72 61 63 74 65  n-ASCII characte
3b7d0 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30  rs..**.** {H1660
3b7e0 36 7d 20 53 75 63 63 65 73 73 69 76 65 20 63 61  6} Successive ca
3b7f0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
3b800 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
3b810 5f 76 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29  _v2(B,X,E,P,F,D)
3b820 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69  ].**          wi
3b830 74 68 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  th the same valu
3b840 65 73 20 66 6f 72 20 42 2c 20 58 2c 20 61 6e 64  es for B, X, and
3b850 20 45 2c 20 6f 76 65 72 72 69 64 65 20 70 72 69   E, override pri
3b860 6f 72 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20  or values.**    
3b870 20 20 20 20 20 20 6f 66 20 50 2c 20 46 2c 20 61        of P, F, a
3b880 6e 64 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  nd D..**.** {H16
3b890 36 30 39 7d 20 49 66 20 74 68 65 20 64 65 73 74  609} If the dest
3b8a0 72 75 63 74 6f 72 20 44 20 69 6e 20 5b 73 71 6c  ructor D in [sql
3b8b0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
3b8c0 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50  ation_v2(B,X,E,P
3b8d0 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,F,D)].**       
3b8e0 20 20 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74     is not NULL t
3b8f0 68 65 6e 20 69 74 20 69 73 20 63 61 6c 6c 65 64  hen it is called
3b900 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 50   with argument P
3b910 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20   when the.**    
3b920 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67 20        collating 
3b930 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f 70  function is drop
3b940 70 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a  ped by SQLite..*
3b950 2a 0a 2a 2a 20 7b 48 31 36 36 31 32 7d 20 41 20  *.** {H16612} A 
3b960 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
3b970 6f 6e 20 69 73 20 64 72 6f 70 70 65 64 20 77 68  on is dropped wh
3b980 65 6e 20 69 74 20 69 73 20 6f 76 65 72 6c 6f 61  en it is overloa
3b990 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36  ded..**.** {H166
3b9a0 31 35 7d 20 41 20 63 6f 6c 6c 61 74 69 6e 67 20  15} A collating 
3b9b0 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f 70  function is drop
3b9c0 70 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74  ped when the dat
3b9d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3b9e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20  .**          is 
3b9f0 63 6c 6f 73 65 64 20 75 73 69 6e 67 20 5b 73 71  closed using [sq
3ba00 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a  lite3_close()]..
3ba10 2a 2a 0a 2a 2a 20 7b 48 31 36 36 31 38 7d 20 54  **.** {H16618} T
3ba20 68 65 20 70 6f 69 6e 74 65 72 20 50 20 69 6e 20  he pointer P in 
3ba30 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
3ba40 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c 58  collation_v2(B,X
3ba50 2c 45 2c 50 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20  ,E,P,F,D)].**   
3ba60 20 20 20 20 20 20 20 69 73 20 70 61 73 73 65 64         is passed
3ba70 20 74 68 72 6f 75 67 68 20 61 73 20 74 68 65 20   through as the 
3ba80 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
3ba90 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  to the compariso
3baa0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75  n.**          fu
3bab0 6e 63 74 69 6f 6e 20 46 20 66 6f 72 20 61 6c 6c  nction F for all
3bac0 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f   subsequent invo
3bad0 63 61 74 69 6f 6e 73 20 6f 66 20 46 2e 0a 2a 2a  cations of F..**
3bae0 0a 2a 2a 20 7b 48 31 36 36 32 31 7d 20 41 20 63  .** {H16621} A c
3baf0 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
3bb00 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
3bb10 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 69 73 20  (B,X,E,P,F)] is 
3bb20 65 78 61 63 74 6c 79 0a 2a 2a 20 20 20 20 20 20  exactly.**      
3bb30 20 20 20 20 74 68 65 20 73 61 6d 65 20 61 73 20      the same as 
3bb40 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  a call to [sqlit
3bb50 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3bb60 69 6f 6e 5f 76 32 28 29 5d 20 77 69 74 68 0a 2a  ion_v2()] with.*
3bb70 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73  *          the s
3bb80 61 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 61  ame parameters a
3bb90 6e 64 20 61 20 4e 55 4c 4c 20 64 65 73 74 72 75  nd a NULL destru
3bba0 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ctor..**.** {H16
3bbb0 36 32 34 7d 20 46 6f 6c 6c 6f 77 69 6e 67 20 61  624} Following a
3bbc0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
3bbd0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c  _collation_v2(B,
3bbe0 58 2c 45 2c 50 2c 46 2c 44 29 5d 2c 0a 2a 2a 20  X,E,P,F,D)],.** 
3bbf0 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20           SQLite 
3bc00 75 73 65 73 20 74 68 65 20 63 6f 6d 70 61 72 69  uses the compari
3bc10 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 46 20 66  son function F f
3bc20 6f 72 20 61 6c 6c 20 74 65 78 74 20 63 6f 6d 70  or all text comp
3bc30 61 72 69 73 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  arison.**       
3bc40 20 20 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e     operations on
3bc50 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
3bc60 6f 6e 6e 65 63 74 69 6f 6e 5d 20 42 20 6f 6e 20  onnection] B on 
3bc70 74 65 78 74 20 76 61 6c 75 65 73 20 74 68 61 74  text values that
3bc80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73 65  .**          use
3bc90 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
3bca0 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 58 2e  equence named X.
3bcb0 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 32 37 7d 20  .**.** {H16627} 
3bcc0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  The [sqlite3_cre
3bcd0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28  ate_collation16(
3bce0 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 77 6f 72 6b  B,X,E,P,F)] work
3bcf0 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20  s the same.**   
3bd00 20 20 20 20 20 20 20 61 73 20 5b 73 71 6c 69 74         as [sqlit
3bd10 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3bd20 69 6f 6e 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20  ion(B,X,E,P,F)] 
3bd30 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 0a  except that the.
3bd40 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6c 6c  **          coll
3bd50 61 74 69 6f 6e 20 6e 61 6d 65 20 58 20 69 73 20  ation name X is 
3bd60 75 6e 64 65 72 73 74 6f 6f 64 20 61 73 20 55 54  understood as UT
3bd70 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 20 62  F-16 in native b
3bd80 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20  yte order.**    
3bd90 20 20 20 20 20 20 69 6e 73 74 65 61 64 20 6f 66        instead of
3bda0 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48   UTF-8..**.** {H
3bdb0 31 36 36 33 30 7d 20 57 68 65 6e 20 6d 75 6c 74  16630} When mult
3bdc0 69 70 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20  iple comparison 
3bdd0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 61 76  functions are av
3bde0 61 69 6c 61 62 6c 65 20 66 6f 72 20 74 68 65 20  ailable for the 
3bdf0 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  same.**         
3be00 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
3be10 6e 63 65 2c 20 53 51 4c 69 74 65 20 63 68 6f 6f  nce, SQLite choo
3be20 73 65 73 20 74 68 65 20 6f 6e 65 20 77 68 6f 73  ses the one whos
3be30 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a  e text encoding.
3be40 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 71 75  **          requ
3be50 69 72 65 73 20 74 68 65 20 6c 65 61 73 74 20 61  ires the least a
3be60 6d 6f 75 6e 74 20 6f 66 20 63 6f 6e 76 65 72 73  mount of convers
3be70 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 64 65 66  ion from the def
3be80 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  ault.**         
3be90 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f   text encoding o
3bea0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  f the database..
3beb0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3bec0 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
3bed0 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71  _collation(.  sq
3bee0 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74  lite3*, .  const
3bef0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
3bf00 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a   int eTextRep, .
3bf10 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a    void*,.  int(*
3bf20 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
3bf30 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
3bf40 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
3bf50 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
3bf60 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
3bf70 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a  e_collation_v2(.
3bf80 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63    sqlite3*, .  c
3bf90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
3bfa0 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65  , .  int eTextRe
3bfb0 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69  p, .  void*,.  i
3bfc0 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
3bfd0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
3bfe0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
3bff0 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44  id*),.  void(*xD
3c000 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29  estroy)(void*).)
3c010 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
3c020 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
3c030 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73  collation16(.  s
3c040 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73  qlite3*, .  cons
3c050 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20  t void *zName,. 
3c060 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a   int eTextRep, .
3c070 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a    void*,.  int(*
3c080 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
3c090 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
3c0a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
3c0b0 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
3c0c0 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e  REF: Collation N
3c0d0 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20  eeded Callbacks 
3c0e0 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30  {H16700} <S20300
3c0f0 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64  >.**.** To avoid
3c100 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73   having to regis
3c110 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f  ter all collatio
3c120 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f  n sequences befo
3c130 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a  re a database.**
3c140 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20   can be used, a 
3c150 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20  single callback 
3c160 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20  function may be 
3c170 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20  registered with 
3c180 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  the.** [database
3c190 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20   connection] to 
3c1a0 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76  be called whenev
3c1b0 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20  er an undefined 
3c1c0 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71  collation.** seq
3c1d0 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 65  uence is require
3c1e0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
3c1f0 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69  function is regi
3c200 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65  stered using the
3c210 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
3c220 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c  on_needed() API,
3c230 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70  .** then it is p
3c240 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20  assed the names 
3c250 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c  of undefined col
3c260 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73  lation sequences
3c270 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65   as strings.** e
3c280 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e  ncoded in UTF-8.
3c290 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c   {H16703} If sql
3c2a0 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
3c2b0 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65  eeded16() is use
3c2c0 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20  d,.** the names 
3c2d0 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54  are passed as UT
3c2e0 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20  F-16 in machine 
3c2f0 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
3c300 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  r..** A call to 
3c310 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20  either function 
3c320 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69  replaces any exi
3c330 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a  sting callback..
3c340 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  **.** When the c
3c350 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
3c360 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 72  ed, the first ar
3c370 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73  gument passed is
3c380 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68   a copy.** of th
3c390 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
3c3a0 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t to sqlite3_col
3c3b0 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20  lation_needed() 
3c3c0 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  or.** sqlite3_co
3c3d0 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
3c3e0 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ().  The second 
3c3f0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
3c400 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
3c410 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69  ection.  The thi
3c420 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  rd argument is o
3c430 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54  ne of [SQLITE_UT
3c440 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46  F8], [SQLITE_UTF
3c450 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51  16BE],.** or [SQ
3c460 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69  LITE_UTF16LE], i
3c470 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f  ndicating the mo
3c480 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72  st desirable for
3c490 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  m of the collati
3c4a0 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66  on.** sequence f
3c4b0 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64  unction required
3c4c0 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 61  .  The fourth pa
3c4d0 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e  rameter is the n
3c4e0 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65  ame of the.** re
3c4f0 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  quired collation
3c500 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a   sequence..**.**
3c510 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   The callback fu
3c520 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65  nction should re
3c530 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 72  gister the desir
3c540 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69  ed collation usi
3c550 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ng.** [sqlite3_c
3c560 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28  reate_collation(
3c570 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  )], [sqlite3_cre
3c580 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28  ate_collation16(
3c590 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  )], or.** [sqlit
3c5a0 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3c5b0 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a  ion_v2()]..**.**
3c5c0 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
3c5d0 2a 2a 20 7b 48 31 36 37 30 32 7d 20 41 20 73 75  ** {H16702} A su
3c5e0 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
3c5f0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74   [sqlite3_collat
3c600 69 6f 6e 5f 6e 65 65 64 65 64 28 44 2c 50 2c 46  ion_needed(D,P,F
3c610 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  )].**          o
3c620 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  r [sqlite3_colla
3c630 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 44 2c  tion_needed16(D,
3c640 50 2c 46 29 5d 20 63 61 75 73 65 73 0a 2a 2a 20  P,F)] causes.** 
3c650 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 64 61           the [da
3c660 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3c670 6e 5d 20 44 20 74 6f 20 69 6e 76 6f 6b 65 20 63  n] D to invoke c
3c680 61 6c 6c 62 61 63 6b 20 46 20 77 69 74 68 20 66  allback F with f
3c690 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  irst.**         
3c6a0 20 70 61 72 61 6d 65 74 65 72 20 50 20 77 68 65   parameter P whe
3c6b0 6e 65 76 65 72 20 69 74 20 6e 65 65 64 73 20 61  never it needs a
3c6c0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
3c6d0 74 69 6f 6e 20 66 6f 72 20 61 0a 2a 2a 20 20 20  tion for a.**   
3c6e0 20 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67         collating
3c6f0 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 69   sequence that i
3c700 74 20 64 6f 65 73 20 6e 6f 74 20 6b 6e 6f 77 20  t does not know 
3c710 61 62 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  about..**.** {H1
3c720 36 37 30 34 7d 20 45 61 63 68 20 73 75 63 63 65  6704} Each succe
3c730 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
3c740 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
3c750 5f 6e 65 65 64 65 64 28 29 5d 20 6f 72 0a 2a 2a  _needed()] or.**
3c760 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
3c770 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
3c780 64 65 64 31 36 28 29 5d 20 6f 76 65 72 72 69 64  ded16()] overrid
3c790 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  es the callback 
3c7a0 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20  registered.**   
3c7b0 20 20 20 20 20 20 20 6f 6e 20 74 68 65 20 73 61         on the sa
3c7c0 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  me [database con
3c7d0 6e 65 63 74 69 6f 6e 5d 20 62 79 20 70 72 69 6f  nection] by prio
3c7e0 72 20 63 61 6c 6c 73 20 74 6f 20 65 69 74 68 65  r calls to eithe
3c7f0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  r.**          in
3c800 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 7b  terface..**.** {
3c810 48 31 36 37 30 36 7d 20 54 68 65 20 6e 61 6d 65  H16706} The name
3c820 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65   of the requeste
3c830 64 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  d collating func
3c840 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 20 74  tion passed in t
3c850 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 34  he.**          4
3c860 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
3c870 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  the callback is 
3c880 69 6e 20 55 54 46 2d 38 20 69 66 20 74 68 65 20  in UTF-8 if the 
3c890 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20  callback.**     
3c8a0 20 20 20 20 20 77 61 73 20 72 65 67 69 73 74 65       was registe
3c8b0 72 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  red using [sqlit
3c8c0 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
3c8d0 64 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 20  ded()] and.**   
3c8e0 20 20 20 20 20 20 20 69 73 20 69 6e 20 55 54 46         is in UTF
3c8f0 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20  -16 native byte 
3c900 6f 72 64 65 72 20 69 66 20 74 68 65 20 63 61 6c  order if the cal
3c910 6c 62 61 63 6b 20 77 61 73 0a 2a 2a 20 20 20 20  lback was.**    
3c920 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64        registered
3c930 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
3c940 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
3c950 31 36 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  16()]..*/.SQLITE
3c960 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3c970 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
3c980 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  d(.  sqlite3*, .
3c990 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64    void*, .  void
3c9a0 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
3c9b0 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
3c9c0 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a  const char*).);.
3c9d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3c9e0 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
3c9f0 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c  _needed16(.  sql
3ca00 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c  ite3*, .  void*,
3ca10 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  .  void(*)(void*
3ca20 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
3ca30 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69  extRep,const voi
3ca40 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70  d*).);../*.** Sp
3ca50 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f  ecify the key fo
3ca60 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64  r an encrypted d
3ca70 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72  atabase.  This r
3ca80 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65  outine should be
3ca90 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74  .** called right
3caa0 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f   after sqlite3_o
3cab0 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  pen()..**.** The
3cac0 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
3cad0 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e  nt this API is n
3cae0 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  ot available in 
3caf0 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61  the public relea
3cb00 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e  se.** of SQLite.
3cb10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3cb20 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a  nt sqlite3_key(.
3cb30 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
3cb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3cb50 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20   /* Database to 
3cb60 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20  be rekeyed */.  
3cb70 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
3cb80 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f  , int nKey     /
3cb90 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a  * The key */.);.
3cba0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
3cbb0 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e  e key on an open
3cbc0 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 74   database.  If t
3cbd0 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62  he current datab
3cbe0 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e  ase is not.** en
3cbf0 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72 6f  crypted, this ro
3cc00 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79  utine will encry
3cc10 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77 3d  pt it.  If pNew=
3cc20 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74  =0 or nNew==0, t
3cc30 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69  he.** database i
3cc40 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a  s decrypted..**.
3cc50 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69  ** The code to i
3cc60 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50  mplement this AP
3cc70 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  I is not availab
3cc80 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63  le in the public
3cc90 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53   release.** of S
3cca0 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  QLite..*/.SQLITE
3ccb0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3ccc0 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  _rekey(.  sqlite
3ccd0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
3cce0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
3ccf0 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79  base to be rekey
3cd00 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  ed */.  const vo
3cd10 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
3cd20 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65  ey     /* The ne
3cd30 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  w key */.);../*.
3cd40 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 75 73  ** CAPI3REF: Sus
3cd50 70 65 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46  pend Execution F
3cd60 6f 72 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20  or A Short Time 
3cd70 7b 48 31 30 35 33 30 7d 20 3c 53 34 30 34 31 30  {H10530} <S40410
3cd80 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
3cd90 74 65 33 5f 73 6c 65 65 70 28 29 20 66 75 6e 63  te3_sleep() func
3cda0 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20  tion causes the 
3cdb0 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 74  current thread t
3cdc0 6f 20 73 75 73 70 65 6e 64 20 65 78 65 63 75 74  o suspend execut
3cdd0 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c 65  ion.** for at le
3cde0 61 73 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 20  ast a number of 
3cdf0 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 65  milliseconds spe
3ce00 63 69 66 69 65 64 20 69 6e 20 69 74 73 20 70 61  cified in its pa
3ce10 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49  rameter..**.** I
3ce20 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  f the operating 
3ce30 73 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20  system does not 
3ce40 73 75 70 70 6f 72 74 20 73 6c 65 65 70 20 72 65  support sleep re
3ce50 71 75 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 6d  quests with.** m
3ce60 69 6c 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 20  illisecond time 
3ce70 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 6e  resolution, then
3ce80 20 74 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 62   the time will b
3ce90 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 0a  e rounded up to.
3cea0 2a 2a 20 74 68 65 20 6e 65 61 72 65 73 74 20 73  ** the nearest s
3ceb0 65 63 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 65  econd. The numbe
3cec0 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64  r of millisecond
3ced0 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61  s of sleep actua
3cee0 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  lly.** requested
3cef0 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 74   from the operat
3cf00 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 72 65  ing system is re
3cf10 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51  turned..**.** SQ
3cf20 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  Lite implements 
3cf30 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 62  this interface b
3cf40 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53  y calling the xS
3cf50 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  leep().** method
3cf60 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
3cf70 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
3cf80 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  ject..**.** INVA
3cf90 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
3cfa0 31 30 35 33 33 7d 20 54 68 65 20 5b 73 71 6c 69  10533} The [sqli
3cfb0 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 6e  te3_sleep(M)] in
3cfc0 74 65 72 66 61 63 65 20 69 6e 76 6f 6b 65 73 20  terface invokes 
3cfd0 74 68 65 20 78 53 6c 65 65 70 0a 2a 2a 20 20 20  the xSleep.**   
3cfe0 20 20 20 20 20 20 20 6d 65 74 68 6f 64 20 6f 66         method of
3cff0 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71   the default [sq
3d000 6c 69 74 65 33 5f 76 66 73 7c 56 46 53 5d 20 69  lite3_vfs|VFS] i
3d010 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 20 20  n order to.**   
3d020 20 20 20 20 20 20 20 73 75 73 70 65 6e 64 20 65         suspend e
3d030 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20  xecution of the 
3d040 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 66  current thread f
3d050 6f 72 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 20  or at least.**  
3d060 20 20 20 20 20 20 20 20 4d 20 6d 69 6c 6c 69 73          M millis
3d070 65 63 6f 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  econds..**.** {H
3d080 31 30 35 33 36 7d 20 54 68 65 20 5b 73 71 6c 69  10536} The [sqli
3d090 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 6e  te3_sleep(M)] in
3d0a0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
3d0b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
3d0c0 20 20 20 20 20 20 20 20 20 20 6d 69 6c 6c 69 73            millis
3d0d0 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20  econds of sleep 
3d0e0 61 63 74 75 61 6c 6c 79 20 72 65 71 75 65 73 74  actually request
3d0f0 65 64 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  ed of the operat
3d100 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
3d110 73 79 73 74 65 6d 2c 20 77 68 69 63 68 20 6d 69  system, which mi
3d120 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68  ght be larger th
3d130 61 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  an the parameter
3d140 20 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50   M..*/.SQLITE_AP
3d150 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c  I int sqlite3_sl
3d160 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  eep(int);../*.**
3d170 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20   CAPI3REF: Name 
3d180 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f  Of The Folder Ho
3d190 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20  lding Temporary 
3d1a0 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c  Files {H10310} <
3d1b0 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66  S20000>.**.** If
3d1c0 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72   this global var
3d1d0 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f  iable is made to
3d1e0 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69   point to a stri
3d1f0 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74  ng which is.** t
3d200 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c  he name of a fol
3d210 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65  der (a.k.a. dire
3d220 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c  ctory), then all
3d230 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
3d240 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53  .** created by S
3d250 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c  QLite will be pl
3d260 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72  aced in that dir
3d270 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73  ectory.  If this
3d280 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20   variable.** is 
3d290 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
3d2a0 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66  then SQLite perf
3d2b0 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f  orms a search fo
3d2c0 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  r an appropriate
3d2d0 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69  .** temporary fi
3d2e0 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a  le directory..**
3d2f0 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61  .** It is not sa
3d300 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 69  fe to modify thi
3d310 73 20 76 61 72 69 61 62 6c 65 20 6f 6e 63 65 20  s variable once 
3d320 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  a [database conn
3d330 65 63 74 69 6f 6e 5d 0a 2a 2a 20 68 61 73 20 62  ection].** has b
3d340 65 65 6e 20 6f 70 65 6e 65 64 2e 20 20 49 74 20  een opened.  It 
3d350 69 73 20 69 6e 74 65 6e 64 65 64 20 74 68 61 74  is intended that
3d360 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 62   this variable b
3d370 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a 20 61 73  e set once.** as
3d380 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73 73   part of process
3d390 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
3d3a0 61 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20 53  and before any S
3d3b0 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 0a  QLite interface.
3d3c0 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65  ** routines have
3d3d0 20 62 65 65 6e 20 63 61 6c 6c 20 61 6e 64 20 72   been call and r
3d3e0 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 20  emain unchanged 
3d3f0 74 68 65 72 65 61 66 74 65 72 2e 0a 2a 2f 0a 53  thereafter..*/.S
3d400 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
3d410 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
3d420 65 63 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ectory;../*.** C
3d430 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 46 6f  API3REF: Test Fo
3d440 72 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f  r Auto-Commit Mo
3d450 64 65 20 7b 48 31 32 39 33 30 7d 20 3c 53 36 30  de {H12930} <S60
3d460 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  200>.** KEYWORDS
3d470 3a 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  : {autocommit mo
3d480 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  de}.**.** The sq
3d490 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f  lite3_get_autoco
3d4a0 6d 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65  mmit() interface
3d4b0 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
3d4c0 6f 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20  o or.** zero if 
3d4d0 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61  the given databa
3d4e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
3d4f0 20 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75   or is not in au
3d500 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a  tocommit mode,.*
3d510 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  * respectively. 
3d520 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65   Autocommit mode
3d530 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c   is on by defaul
3d540 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74  t..** Autocommit
3d550 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c 65   mode is disable
3d560 64 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20 73  d by a [BEGIN] s
3d570 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74  tatement..** Aut
3d580 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
3d590 72 65 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 20  re-enabled by a 
3d5a0 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c  [COMMIT] or [ROL
3d5b0 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66  LBACK]..**.** If
3d5c0 20 63 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f   certain kinds o
3d5d0 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f  f errors occur o
3d5e0 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69  n a statement wi
3d5f0 74 68 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61  thin a multi-sta
3d600 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61  tement.** transa
3d610 63 74 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e  ction (errors in
3d620 63 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f  cluding [SQLITE_
3d630 46 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49  FULL], [SQLITE_I
3d640 4f 45 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  OERR],.** [SQLIT
3d650 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54  E_NOMEM], [SQLIT
3d660 45 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51  E_BUSY], and [SQ
3d670 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29  LITE_INTERRUPT])
3d680 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61   then the.** tra
3d690 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62  nsaction might b
3d6a0 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75  e rolled back au
3d6b0 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68  tomatically.  Th
3d6c0 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a  e only way to.**
3d6d0 20 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65   find out whethe
3d6e0 72 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74  r SQLite automat
3d6f0 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61  ically rolled ba
3d700 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ck the transacti
3d710 6f 6e 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65  on after.** an e
3d720 72 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20 74  rror is to use t
3d730 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  his function..**
3d740 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
3d750 2a 2a 0a 2a 2a 20 7b 48 31 32 39 33 31 7d 20 54  **.** {H12931} T
3d760 68 65 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f  he [sqlite3_get_
3d770 61 75 74 6f 63 6f 6d 6d 69 74 28 44 29 5d 20 69  autocommit(D)] i
3d780 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
3d790 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20   non-zero or.** 
3d7a0 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 69 66           zero if
3d7b0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
3d7c0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 69 73 20  onnection] D is 
3d7d0 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74  or is not in aut
3d7e0 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 20 20 20 20 20  ocommit.**      
3d7f0 20 20 20 20 6d 6f 64 65 2c 20 72 65 73 70 65 63      mode, respec
3d800 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48  tively..**.** {H
3d810 31 32 39 33 32 7d 20 41 75 74 6f 63 6f 6d 6d 69  12932} Autocommi
3d820 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20  t mode is on by 
3d830 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b  default..**.** {
3d840 48 31 32 39 33 33 7d 20 41 75 74 6f 63 6f 6d 6d  H12933} Autocomm
3d850 69 74 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62  it mode is disab
3d860 6c 65 64 20 62 79 20 61 20 73 75 63 63 65 73 73  led by a success
3d870 66 75 6c 20 5b 42 45 47 49 4e 5d 20 73 74 61 74  ful [BEGIN] stat
3d880 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ement..**.** {H1
3d890 32 39 33 34 7d 20 41 75 74 6f 63 6f 6d 6d 69 74  2934} Autocommit
3d8a0 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c 65 64   mode is enabled
3d8b0 20 62 79 20 61 20 73 75 63 63 65 73 73 66 75 6c   by a successful
3d8c0 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f   [COMMIT] or [RO
3d8d0 4c 4c 42 41 43 4b 5d 0a 2a 2a 20 20 20 20 20 20  LLBACK].**      
3d8e0 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a      statement..*
3d8f0 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53  *.** ASSUMPTIONS
3d900 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 39 33 36 7d  :.**.** {A12936}
3d910 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65   If another thre
3d920 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 61  ad changes the a
3d930 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 73  utocommit status
3d940 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
3d950 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  .**          con
3d960 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68  nection while th
3d970 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75  is routine is ru
3d980 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20  nning, then the 
3d990 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
3d9a0 20 20 20 20 20 20 20 20 20 69 73 20 75 6e 64 65           is unde
3d9b0 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fined..*/.SQLITE
3d9c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3d9d0 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28  _get_autocommit(
3d9e0 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
3d9f0 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64  * CAPI3REF: Find
3da00 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61   The Database Ha
3da10 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72  ndle Of A Prepar
3da20 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31  ed Statement {H1
3da30 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a  3120} <S60600>.*
3da40 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
3da50 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72  _db_handle inter
3da60 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65  face returns the
3da70 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3da80 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a  ction] handle.**
3da90 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65   to which a [pre
3daa0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
3dab0 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 64   belongs.  The d
3dac0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 72  atabase handle r
3dad0 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71  eturned by.** sq
3dae0 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20  lite3_db_handle 
3daf0 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  is the same data
3db00 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74  base handle that
3db10 20 77 61 73 20 74 68 65 20 66 69 72 73 74 20 61   was the first a
3db20 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
3db30 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  e [sqlite3_prepa
3db40 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f  re_v2()] call (o
3db50 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20  r its variants) 
3db60 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f  that was used to
3db70 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 20 73  .** create the s
3db80 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
3db90 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a  first place..**.
3dba0 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
3dbb0 2a 0a 2a 2a 20 7b 48 31 33 31 32 33 7d 20 54 68  *.** {H13123} Th
3dbc0 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 68 61  e [sqlite3_db_ha
3dbd0 6e 64 6c 65 28 53 29 5d 20 69 6e 74 65 72 66 61  ndle(S)] interfa
3dbe0 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
3dbf0 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  nter.**         
3dc00 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73   to the [databas
3dc10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73  e connection] as
3dc20 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
3dc30 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  e.**          [p
3dc40 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3dc50 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  t] S..*/.SQLITE_
3dc60 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  API sqlite3 *sql
3dc70 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73  ite3_db_handle(s
3dc80 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a  qlite3_stmt*);..
3dc90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3dca0 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72  Find the next pr
3dcb0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
3dcc0 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30   {H13140} <S6060
3dcd0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  0>.**.** This in
3dce0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
3dcf0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
3dd00 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20   next [prepared 
3dd10 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72  statement] after
3dd20 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69  .** pStmt associ
3dd30 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64  ated with the [d
3dd40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3dd50 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74  on] pDb.  If pSt
3dd60 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  mt is NULL.** th
3dd70 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  en this interfac
3dd80 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
3dd90 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74  ter to the first
3dda0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
3ddb0 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  ent.** associate
3ddc0 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  d with the datab
3ddd0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  ase connection p
3dde0 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61  Db.  If no prepa
3ddf0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  red statement.**
3de00 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63   satisfies the c
3de10 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69  onditions of thi
3de20 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65  s routine, it re
3de30 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  turns NULL..**.*
3de40 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
3de50 0a 2a 2a 20 7b 48 31 33 31 34 33 7d 20 49 66 20  .** {H13143} If 
3de60 44 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 65  D is a [database
3de70 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61   connection] tha
3de80 74 20 68 6f 6c 64 73 20 6f 6e 65 20 6f 72 20 6d  t holds one or m
3de90 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ore.**          
3dea0 75 6e 66 69 6e 61 6c 69 7a 65 64 20 5b 70 72 65  unfinalized [pre
3deb0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
3dec0 5d 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c  ] and S is a NUL
3ded0 4c 20 70 6f 69 6e 74 65 72 2c 0a 2a 2a 20 20 20  L pointer,.**   
3dee0 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c         then [sql
3def0 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44  ite3_next_stmt(D
3df00 2c 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68  , S)] routine sh
3df10 61 6c 6c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  all return a poi
3df20 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  nter.**         
3df30 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 70   to one of the p
3df40 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3df50 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ts associated wi
3df60 74 68 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  th D..**.** {H13
3df70 31 34 36 7d 20 49 66 20 44 20 69 73 20 61 20 5b  146} If D is a [
3df80 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3df90 69 6f 6e 5d 20 74 68 61 74 20 68 6f 6c 64 73 20  ion] that holds 
3dfa0 6e 6f 20 75 6e 66 69 6e 61 6c 69 7a 65 64 0a 2a  no unfinalized.*
3dfb0 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70  *          [prep
3dfc0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d  ared statements]
3dfd0 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c 4c   and S is a NULL
3dfe0 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 0a 2a   pointer, then.*
3dff0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
3e000 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c  te3_next_stmt(D,
3e010 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61   S)] routine sha
3e020 6c 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c  ll return a NULL
3e030 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
3e040 7b 48 31 33 31 34 39 7d 20 49 66 20 53 20 69 73  {H13149} If S is
3e050 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
3e060 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65 20 5b  tement] in the [
3e070 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3e080 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20 20 20  ion] D.**       
3e090 20 20 20 61 6e 64 20 53 20 69 73 20 6e 6f 74 20     and S is not 
3e0a0 74 68 65 20 6c 61 73 74 20 70 72 65 70 61 72 65  the last prepare
3e0b0 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 44  d statement in D
3e0c0 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20  , then.**       
3e0d0 20 20 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74     [sqlite3_next
3e0e0 5f 73 74 6d 74 28 44 2c 20 53 29 5d 20 72 6f 75  _stmt(D, S)] rou
3e0f0 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74 75 72  tine shall retur
3e100 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20  n a pointer.**  
3e110 20 20 20 20 20 20 20 20 74 6f 20 74 68 65 20 6e          to the n
3e120 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61  ext prepared sta
3e130 74 65 6d 65 6e 74 20 69 6e 20 44 20 61 66 74 65  tement in D afte
3e140 72 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31  r S..**.** {H131
3e150 35 32 7d 20 49 66 20 53 20 69 73 20 74 68 65 20  52} If S is the 
3e160 6c 61 73 74 20 5b 70 72 65 70 61 72 65 64 20 73  last [prepared s
3e170 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65  tatement] in the
3e180 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 61  .**          [da
3e190 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3e1a0 6e 5d 20 44 20 74 68 65 6e 20 74 68 65 20 5b 73  n] D then the [s
3e1b0 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
3e1c0 28 44 2c 20 53 29 5d 0a 2a 2a 20 20 20 20 20 20  (D, S)].**      
3e1d0 20 20 20 20 72 6f 75 74 69 6e 65 20 73 68 61 6c      routine shal
3e1e0 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20  l return a NULL 
3e1f0 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41  pointer..**.** A
3e200 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a  SSUMPTIONS:.**.*
3e210 2a 20 7b 41 31 33 31 35 34 7d 20 54 68 65 20 5b  * {A13154} The [
3e220 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3e230 69 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 69  ion] pointer D i
3e240 6e 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20  n a call to.**  
3e250 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
3e260 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d  _next_stmt(D,S)]
3e270 20 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20 61   must refer to a
3e280 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a  n open database.
3e290 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 6e  **          conn
3e2a0 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e 20 70 61  ection and in pa
3e2b0 72 74 69 63 75 6c 61 72 20 6d 75 73 74 20 6e 6f  rticular must no
3e2c0 74 20 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  t be a NULL poin
3e2d0 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ter..*/.SQLITE_A
3e2e0 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  PI sqlite3_stmt 
3e2f0 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74  *sqlite3_next_st
3e300 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c  mt(sqlite3 *pDb,
3e310 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
3e320 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
3e330 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41  PI3REF: Commit A
3e340 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69  nd Rollback Noti
3e350 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63  fication Callbac
3e360 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c 53 36 30  ks {H12950} <S60
3e370 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  400>.**.** The s
3e380 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
3e390 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ok() interface r
3e3a0 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62  egisters a callb
3e3b0 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ack.** function 
3e3c0 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  to be invoked wh
3e3d0 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63  enever a transac
3e3e0 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
3e3f0 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61  d..** Any callba
3e400 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76  ck set by a prev
3e410 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ious call to sql
3e420 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
3e430 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61  ().** for the sa
3e440 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
3e450 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  ection is overri
3e460 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  dden..** The sql
3e470 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f  ite3_rollback_ho
3e480 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ok() interface r
3e490 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62  egisters a callb
3e4a0 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ack.** function 
3e4b0 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  to be invoked wh
3e4c0 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63  enever a transac
3e4d0 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
3e4e0 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61  d..** Any callba
3e4f0 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76  ck set by a prev
3e500 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ious call to sql
3e510 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
3e520 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61  ().** for the sa
3e530 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
3e540 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  ection is overri
3e550 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72  dden..** The pAr
3e560 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61  g argument is pa
3e570 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20  ssed through to 
3e580 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  the callback..**
3e590 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   If the callback
3e5a0 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f   on a commit hoo
3e5b0 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  k function retur
3e5c0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20  ns non-zero,.** 
3e5d0 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20  then the commit 
3e5e0 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  is converted int
3e5f0 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a  o a rollback..**
3e600 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66  .** If another f
3e610 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76  unction was prev
3e620 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65  iously registere
3e630 64 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 76  d, its.** pArg v
3e640 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  alue is returned
3e650 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c  .  Otherwise NUL
3e660 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  L is returned..*
3e670 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63  *.** The callbac
3e680 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
3e690 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79   must not do any
3e6a0 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20  thing that will 
3e6b0 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61  modify.** the da
3e6c0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3e6d0 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74  n that invoked t
3e6e0 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e  he callback.  An
3e6f0 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20  y actions.** to 
3e700 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62  modify the datab
3e710 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d  ase connection m
3e720 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20  ust be deferred 
3e730 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a  until after the.
3e740 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66  ** completion of
3e750 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74   the [sqlite3_st
3e760 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20  ep()] call that 
3e770 74 72 69 67 67 65 72 65 64 20 74 68 65 20 63 6f  triggered the co
3e780 6d 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62  mmit.** or rollb
3e790 61 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20  ack hook in the 
3e7a0 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20  first place..** 
3e7b0 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74  Note that [sqlit
3e7c0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
3e7d0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74   and [sqlite3_st
3e7e0 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66  ep()] both modif
3e7f0 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62  y their.** datab
3e800 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
3e810 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20  for the meaning 
3e820 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74  of "modify" in t
3e830 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a  his paragraph..*
3e840 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e 67  *.** Registering
3e850 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e   a NULL function
3e860 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61   disables the ca
3e870 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f  llback..**.** Fo
3e880 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f  r the purposes o
3e890 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 72  f this API, a tr
3e8a0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 69  ansaction is sai
3e8b0 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a 2a  d to have been.*
3e8c0 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66  * rolled back if
3e8d0 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 4f   an explicit "RO
3e8e0 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 6e  LLBACK" statemen
3e8f0 74 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 6f  t is executed, o
3e900 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 72  r.** an error or
3e910 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 73   constraint caus
3e920 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 72  es an implicit r
3e930 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 72  ollback to occur
3e940 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63  ..** The rollbac
3e950 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f  k callback is no
3e960 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74  t invoked if a t
3e970 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  ransaction is.**
3e980 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72   automatically r
3e990 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75  olled back becau
3e9a0 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  se the database 
3e9b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c  connection is cl
3e9c0 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c  osed..** The rol
3e9d0 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69  lback callback i
3e9e0 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66  s not invoked if
3e9f0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
3ea00 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b  s.** rolled back
3ea10 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d 69   because a commi
3ea20 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  t callback retur
3ea30 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a  ned non-zero..**
3ea40 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f 6e   <todo> Check on
3ea50 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a   this </todo>.**
3ea60 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
3ea70 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 31 7d 20 54  **.** {H12951} T
3ea80 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d  he [sqlite3_comm
3ea90 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20  it_hook(D,F,P)] 
3eaa0 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
3eab0 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  ers the.**      
3eac0 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e      callback fun
3ead0 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 69 6e  ction F to be in
3eae0 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67 75 6d  voked with argum
3eaf0 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72 0a 2a  ent P whenever.*
3eb00 2a 20 20 20 20 20 20 20 20 20 20 61 20 74 72 61  *          a tra
3eb10 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73  nsaction commits
3eb20 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73   on the [databas
3eb30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e  e connection] D.
3eb40 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 32 7d 20  .**.** {H12952} 
3eb50 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d  The [sqlite3_com
3eb60 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d  mit_hook(D,F,P)]
3eb70 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
3eb80 6e 73 20 74 68 65 20 50 20 61 72 67 75 6d 65 6e  ns the P argumen
3eb90 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72  t.**          fr
3eba0 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  om the previous 
3ebb0 63 61 6c 6c 20 77 69 74 68 20 74 68 65 20 73 61  call with the sa
3ebc0 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  me [database con
3ebd0 6e 65 63 74 69 6f 6e 5d 20 44 2c 0a 2a 2a 20 20  nection] D,.**  
3ebe0 20 20 20 20 20 20 20 20 6f 72 20 4e 55 4c 4c 20          or NULL 
3ebf0 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  on the first cal
3ec00 6c 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c  l for a particul
3ec10 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ar database conn
3ec20 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a 2a 2a 20  ection D..**.** 
3ec30 7b 48 31 32 39 35 33 7d 20 45 61 63 68 20 63 61  {H12953} Each ca
3ec40 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
3ec50 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 6f 76  ommit_hook()] ov
3ec60 65 72 77 72 69 74 65 73 20 74 68 65 20 63 61 6c  erwrites the cal
3ec70 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20  lback.**        
3ec80 20 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20    registered by 
3ec90 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2a 0a  prior calls..**.
3eca0 2a 2a 20 7b 48 31 32 39 35 34 7d 20 49 66 20 74  ** {H12954} If t
3ecb0 68 65 20 46 20 61 72 67 75 6d 65 6e 74 20 74 6f  he F argument to
3ecc0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74   [sqlite3_commit
3ecd0 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 69 73  _hook(D,F,P)] is
3ece0 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20   NULL.**        
3ecf0 20 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69    then the commi
3ed00 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20  t hook callback 
3ed10 69 73 20 63 61 6e 63 65 6c 65 64 20 61 6e 64 20  is canceled and 
3ed20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20  no callback.**  
3ed30 20 20 20 20 20 20 20 20 69 73 20 69 6e 76 6f 6b          is invok
3ed40 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61  ed when a transa
3ed50 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 2e 0a 2a  ction commits..*
3ed60 2a 0a 2a 2a 20 7b 48 31 32 39 35 35 7d 20 49 66  *.** {H12955} If
3ed70 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 61 6c 6c   the commit call
3ed80 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e  back returns non
3ed90 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 63  -zero then the c
3eda0 6f 6d 6d 69 74 20 69 73 0a 2a 2a 20 20 20 20 20  ommit is.**     
3edb0 20 20 20 20 20 63 6f 6e 76 65 72 74 65 64 20 69       converted i
3edc0 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a  nto a rollback..
3edd0 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 31 7d 20 54  **.** {H12961} T
3ede0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c 6c  he [sqlite3_roll
3edf0 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29  back_hook(D,F,P)
3ee00 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69  ] interface regi
3ee10 73 74 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20  sters the.**    
3ee20 20 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66        callback f
3ee30 75 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20  unction F to be 
3ee40 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67  invoked with arg
3ee50 75 6d 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72  ument P whenever
3ee60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 74  .**          a t
3ee70 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 73  ransaction rolls
3ee80 20 62 61 63 6b 20 6f 6e 20 74 68 65 20 5b 64 61   back on the [da
3ee90 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3eea0 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  n] D..**.** {H12
3eeb0 39 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  962} The [sqlite
3eec0 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28  3_rollback_hook(
3eed0 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63  D,F,P)] interfac
3eee0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 50 0a  e returns the P.
3eef0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72 67 75  **          argu
3ef00 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 72  ment from the pr
3ef10 65 76 69 6f 75 73 20 63 61 6c 6c 20 77 69 74 68  evious call with
3ef20 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20   the same.**    
3ef30 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20        [database 
3ef40 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2c 20 6f  connection] D, o
3ef50 72 20 4e 55 4c 4c 20 6f 6e 20 74 68 65 20 66 69  r NULL on the fi
3ef60 72 73 74 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 20  rst call.**     
3ef70 20 20 20 20 20 66 6f 72 20 61 20 70 61 72 74 69       for a parti
3ef80 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63  cular database c
3ef90 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a  onnection D..**.
3efa0 2a 2a 20 7b 48 31 32 39 36 33 7d 20 45 61 63 68  ** {H12963} Each
3efb0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
3efc0 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28  3_rollback_hook(
3efd0 29 5d 20 6f 76 65 72 77 72 69 74 65 73 20 74 68  )] overwrites th
3efe0 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20  e callback.**   
3eff0 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65         registere
3f000 64 20 62 79 20 70 72 69 6f 72 20 63 61 6c 6c 73  d by prior calls
3f010 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 34 7d  ..**.** {H12964}
3f020 20 49 66 20 74 68 65 20 46 20 61 72 67 75 6d 65   If the F argume
3f030 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  nt to [sqlite3_r
3f040 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46  ollback_hook(D,F
3f050 2c 50 29 5d 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20  ,P)] is NULL.** 
3f060 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 74 68           then th
3f070 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20  e rollback hook 
3f080 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e 63  callback is canc
3f090 65 6c 65 64 20 61 6e 64 20 6e 6f 20 63 61 6c 6c  eled and no call
3f0a0 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20  back.**         
3f0b0 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   is invoked when
3f0c0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72   a transaction r
3f0d0 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2f 0a 53 51  olls back..*/.SQ
3f0e0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
3f0f0 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
3f100 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ok(sqlite3*, int
3f110 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64  (*)(void*), void
3f120 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
3f130 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c  oid *sqlite3_rol
3f140 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74  lback_hook(sqlit
3f150 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  e3*, void(*)(voi
3f160 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f  d *), void*);../
3f170 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
3f180 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74 69 66  ata Change Notif
3f190 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b  ication Callback
3f1a0 73 20 7b 48 31 32 39 37 30 7d 20 3c 53 36 30 34  s {H12970} <S604
3f1b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
3f1c0 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
3f1d0 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  k() interface re
3f1e0 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61  gisters a callba
3f1f0 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77  ck function.** w
3f200 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73  ith the [databas
3f210 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64  e connection] id
3f220 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
3f230 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a  first argument.*
3f240 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  * to be invoked 
3f250 77 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 69  whenever a row i
3f260 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72  s updated, inser
3f270 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a  ted or deleted..
3f280 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20  ** Any callback 
3f290 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f 75  set by a previou
3f2a0 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66  s call to this f
3f2b0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74  unction.** for t
3f2c0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
3f2d0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f   connection is o
3f2e0 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a  verridden..**.**
3f2f0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
3f300 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65  ment is a pointe
3f310 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  r to the functio
3f320 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e  n to invoke when
3f330 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 64   a.** row is upd
3f340 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f  ated, inserted o
3f350 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68  r deleted..** Th
3f360 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
3f370 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
3f380 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68   is a copy of th
3f390 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
3f3a0 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 75  .** to sqlite3_u
3f3b0 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a  pdate_hook()..**
3f3c0 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c   The second call
3f3d0 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 73  back argument is
3f3e0 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f   one of [SQLITE_
3f3f0 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45  INSERT], [SQLITE
3f400 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20  _DELETE],.** or 
3f410 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c  [SQLITE_UPDATE],
3f420 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
3f430 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74  e operation that
3f440 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c   caused the call
3f450 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e  back.** to be in
3f460 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68  voked..** The th
3f470 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61  ird and fourth a
3f480 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
3f490 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
3f4a0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
3f4b0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64  .** database and
3f4c0 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74   table name cont
3f4d0 61 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65 63  aining the affec
3f4e0 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20  ted row..** The 
3f4f0 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70  final callback p
3f500 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
3f510 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 72  [rowid] of the r
3f520 6f 77 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61  ow..** In the ca
3f530 73 65 20 6f 66 20 61 6e 20 75 70 64 61 74 65 2c  se of an update,
3f540 20 74 68 69 73 20 69 73 20 74 68 65 20 5b 72 6f   this is the [ro
3f550 77 69 64 5d 20 61 66 74 65 72 20 74 68 65 20 75  wid] after the u
3f560 70 64 61 74 65 20 74 61 6b 65 73 20 70 6c 61 63  pdate takes plac
3f570 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64  e..**.** The upd
3f580 61 74 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20  ate hook is not 
3f590 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74  invoked when int
3f5a0 65 72 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62  ernal system tab
3f5b0 6c 65 73 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66  les are.** modif
3f5c0 69 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65  ied (i.e. sqlite
3f5d0 5f 6d 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69  _master and sqli
3f5e0 74 65 5f 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a  te_sequence)..**
3f5f0 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 68  .** The update h
3f600 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ook implementati
3f610 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61  on must not do a
3f620 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c  nything that wil
3f630 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20  l modify.** the 
3f640 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3f650 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  ion that invoked
3f660 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   the update hook
3f670 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a  .  Any actions.*
3f680 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20  * to modify the 
3f690 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3f6a0 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 65  ion must be defe
3f6b0 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 72  rred until after
3f6c0 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69   the.** completi
3f6d0 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  on of the [sqlit
3f6e0 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c 20  e3_step()] call 
3f6f0 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 74  that triggered t
3f700 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a  he update hook..
3f710 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71  ** Note that [sq
3f720 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
3f730 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
3f740 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f  _step()] both mo
3f750 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61  dify their.** da
3f760 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3f770 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69  ns for the meani
3f780 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69  ng of "modify" i
3f790 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
3f7a0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68  ..**.** If anoth
3f7b0 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  er function was 
3f7c0 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73  previously regis
3f7d0 74 65 72 65 64 2c 20 69 74 73 20 70 41 72 67 20  tered, its pArg 
3f7e0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 72 65 74 75  value.** is retu
3f7f0 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65  rned.  Otherwise
3f800 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
3f810 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  d..**.** INVARIA
3f820 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39  NTS:.**.** {H129
3f830 37 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  71} The [sqlite3
3f840 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 44 2c 46  _update_hook(D,F
3f850 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  ,P)] interface c
3f860 61 75 73 65 73 20 74 68 65 20 63 61 6c 6c 62 61  auses the callba
3f870 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  ck.**          f
3f880 75 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20  unction F to be 
3f890 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 66 69 72  invoked with fir
3f8a0 73 74 20 70 61 72 61 6d 65 74 65 72 20 50 20 77  st parameter P w
3f8b0 68 65 6e 65 76 65 72 0a 2a 2a 20 20 20 20 20 20  henever.**      
3f8c0 20 20 20 20 61 20 74 61 62 6c 65 20 72 6f 77 20      a table row 
3f8d0 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 69 6e 73  is modified, ins
3f8e0 65 72 74 65 64 2c 20 6f 72 20 64 65 6c 65 74 65  erted, or delete
3f8f0 64 20 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  d on.**         
3f900 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
3f910 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a  onnection] D..**
3f920 0a 2a 2a 20 7b 48 31 32 39 37 33 7d 20 54 68 65  .** {H12973} The
3f930 20 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65   [sqlite3_update
3f940 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 69 6e  _hook(D,F,P)] in
3f950 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
3f960 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 20 20 20  the value.**    
3f970 20 20 20 20 20 20 6f 66 20 50 20 66 6f 72 20 74        of P for t
3f980 68 65 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c  he previous call
3f990 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61   on the same [da
3f9a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3f9b0 6e 5d 20 44 2c 0a 2a 2a 20 20 20 20 20 20 20 20  n] D,.**        
3f9c0 20 20 6f 72 20 4e 55 4c 4c 20 66 6f 72 20 74 68    or NULL for th
3f9d0 65 20 66 69 72 73 74 20 63 61 6c 6c 2e 0a 2a 2a  e first call..**
3f9e0 0a 2a 2a 20 7b 48 31 32 39 37 35 7d 20 49 66 20  .** {H12975} If 
3f9f0 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20  the update hook 
3fa00 63 61 6c 6c 62 61 63 6b 20 46 20 69 6e 20 5b 73  callback F in [s
3fa10 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
3fa20 6f 6b 28 44 2c 46 2c 50 29 5d 0a 2a 2a 20 20 20  ok(D,F,P)].**   
3fa30 20 20 20 20 20 20 20 69 73 20 4e 55 4c 4c 20 74         is NULL t
3fa40 68 65 6e 20 74 68 65 20 6e 6f 20 75 70 64 61 74  hen the no updat
3fa50 65 20 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20  e callbacks are 
3fa60 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  made..**.** {H12
3fa70 39 37 37 7d 20 45 61 63 68 20 63 61 6c 6c 20 74  977} Each call t
3fa80 6f 20 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74  o [sqlite3_updat
3fa90 65 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 6f  e_hook(D,F,P)] o
3faa0 76 65 72 72 69 64 65 73 20 70 72 69 6f 72 20 63  verrides prior c
3fab0 61 6c 6c 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  alls.**         
3fac0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 69 6e 74   to the same int
3fad0 65 72 66 61 63 65 20 6f 6e 20 74 68 65 20 73 61  erface on the sa
3fae0 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  me [database con
3faf0 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a  nection] D..**.*
3fb00 2a 20 7b 48 31 32 39 37 39 7d 20 54 68 65 20 75  * {H12979} The u
3fb10 70 64 61 74 65 20 68 6f 6f 6b 20 63 61 6c 6c 62  pdate hook callb
3fb20 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b  ack is not invok
3fb30 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e 61 6c  ed when internal
3fb40 20 73 79 73 74 65 6d 0a 2a 2a 20 20 20 20 20 20   system.**      
3fb50 20 20 20 20 74 61 62 6c 65 73 20 73 75 63 68 20      tables such 
3fb60 61 73 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  as sqlite_master
3fb70 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 65 71 75   and sqlite_sequ
3fb80 65 6e 63 65 20 61 72 65 20 6d 6f 64 69 66 69 65  ence are modifie
3fb90 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 38 31  d..**.** {H12981
3fba0 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72  } The second par
3fbb0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 75 70  ameter to the up
3fbc0 64 61 74 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  date callback.**
3fbd0 20 20 20 20 20 20 20 20 20 20 69 73 20 6f 6e 65            is one
3fbe0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45   of [SQLITE_INSE
3fbf0 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c  RT], [SQLITE_DEL
3fc00 45 54 45 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ETE] or [SQLITE_
3fc10 55 50 44 41 54 45 5d 2c 0a 2a 2a 20 20 20 20 20  UPDATE],.**     
3fc20 20 20 20 20 20 64 65 70 65 6e 64 69 6e 67 20 6f       depending o
3fc30 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  n the operation 
3fc40 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20  that caused the 
3fc50 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69  callback to be i
3fc60 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48  nvoked..**.** {H
3fc70 31 32 39 38 33 7d 20 54 68 65 20 74 68 69 72 64  12983} The third
3fc80 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75   and fourth argu
3fc90 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c  ments to the cal
3fca0 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f  lback contain po
3fcb0 69 6e 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20  inters.**       
3fcc0 20 20 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69     to zero-termi
3fcd0 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69  nated UTF-8 stri
3fce0 6e 67 73 20 77 68 69 63 68 20 61 72 65 20 74 68  ngs which are th
3fcf0 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a  e names of the.*
3fd00 2a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62  *          datab
3fd10 61 73 65 20 61 6e 64 20 74 61 62 6c 65 20 74 68  ase and table th
3fd20 61 74 20 69 73 20 62 65 69 6e 67 20 75 70 64 61  at is being upda
3fd30 74 65 64 2e 0a 0a 2a 2a 20 7b 48 31 32 39 38 35  ted...** {H12985
3fd40 7d 20 54 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c  } The final call
3fd50 62 61 63 6b 20 70 61 72 61 6d 65 74 65 72 20 69  back parameter i
3fd60 73 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66  s the [rowid] of
3fd70 20 74 68 65 20 72 6f 77 20 61 66 74 65 72 0a 2a   the row after.*
3fd80 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 63  *          the c
3fd90 68 61 6e 67 65 20 6f 63 63 75 72 73 2e 0a 2a 2f  hange occurs..*/
3fda0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3fdb0 20 2a 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65   *sqlite3_update
3fdc0 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33  _hook(.  sqlite3
3fdd0 2a 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f  *, .  void(*)(vo
3fde0 69 64 20 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63  id *,int ,char c
3fdf0 6f 6e 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73  onst *,char cons
3fe00 74 20 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36  t *,sqlite3_int6
3fe10 34 29 2c 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a  4),.  void*.);..
3fe20 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3fe30 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c  Enable Or Disabl
3fe40 65 20 53 68 61 72 65 64 20 50 61 67 65 72 20 43  e Shared Pager C
3fe50 61 63 68 65 20 7b 48 31 30 33 33 30 7d 20 3c 53  ache {H10330} <S
3fe60 33 30 39 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  30900>.** KEYWOR
3fe70 44 53 3a 20 7b 73 68 61 72 65 64 20 63 61 63 68  DS: {shared cach
3fe80 65 7d 20 7b 73 68 61 72 65 64 20 63 61 63 68 65  e} {shared cache
3fe90 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 69   mode}.**.** Thi
3fea0 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65  s routine enable
3feb0 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68  s or disables th
3fec0 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 68 65  e sharing of the
3fed0 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 0a   database cache.
3fee0 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 64 61  ** and schema da
3fef0 74 61 20 73 74 72 75 63 74 75 72 65 73 20 62 65  ta structures be
3ff00 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 65 20  tween [database 
3ff10 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 6f 6e  connection | con
3ff20 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f 20  nections].** to 
3ff30 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
3ff40 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 65 6e  e. Sharing is en
3ff50 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 67  abled if the arg
3ff60 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a 2a 2a  ument is true.**
3ff70 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 69 66   and disabled if
3ff80 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
3ff90 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43 61   false..**.** Ca
3ffa0 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20 65  che sharing is e
3ffb0 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 61 62  nabled and disab
3ffc0 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 69 72  led for an entir
3ffd0 65 20 70 72 6f 63 65 73 73 2e 20 7b 45 4e 44 7d  e process. {END}
3ffe0 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 63 68  .** This is a ch
3fff0 61 6e 67 65 20 61 73 20 6f 66 20 53 51 4c 69 74  ange as of SQLit
40000 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2e  e version 3.5.0.
40010 20 49 6e 20 70 72 69 6f 72 20 76 65 72 73 69 6f   In prior versio
40020 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 2a 2a  ns of SQLite,.**
40030 20 73 68 61 72 69 6e 67 20 77 61 73 20 65 6e 61   sharing was ena
40040 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64  bled or disabled
40050 20 66 6f 72 20 65 61 63 68 20 74 68 72 65 61 64   for each thread
40060 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a   separately..**.
40070 2a 2a 20 54 68 65 20 63 61 63 68 65 20 73 68 61  ** The cache sha
40080 72 69 6e 67 20 6d 6f 64 65 20 73 65 74 20 62 79  ring mode set by
40090 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
400a0 65 66 66 65 63 74 73 20 61 6c 6c 20 73 75 62 73  effects all subs
400b0 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20  equent.** calls 
400c0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  to [sqlite3_open
400d0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ()], [sqlite3_op
400e0 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 5b 73  en_v2()], and [s
400f0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
40100 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 64 61  ..** Existing da
40110 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
40120 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 73 65 20  ns continue use 
40130 74 68 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 65  the sharing mode
40140 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 69 6e 20  .** that was in 
40150 65 66 66 65 63 74 20 61 74 20 74 68 65 20 74 69  effect at the ti
40160 6d 65 20 74 68 65 79 20 77 65 72 65 20 6f 70 65  me they were ope
40170 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75  ned..**.** Virtu
40180 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 6e 6f 74  al tables cannot
40190 20 62 65 20 75 73 65 64 20 77 69 74 68 20 61 20   be used with a 
401a0 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 57  shared cache.  W
401b0 68 65 6e 20 73 68 61 72 65 64 0a 2a 2a 20 63 61  hen shared.** ca
401c0 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  che is enabled, 
401d0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  the [sqlite3_cre
401e0 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 41 50  ate_module()] AP
401f0 49 20 75 73 65 64 20 74 6f 20 72 65 67 69 73 74  I used to regist
40200 65 72 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61  er.** virtual ta
40210 62 6c 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73  bles will always
40220 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
40230 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
40240 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51  tine returns [SQ
40250 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72  LITE_OK] if shar
40260 65 64 20 63 61 63 68 65 20 77 61 73 20 65 6e 61  ed cache was ena
40270 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64  bled or disabled
40280 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79  .** successfully
40290 2e 20 20 41 6e 20 5b 65 72 72 6f 72 20 63 6f 64  .  An [error cod
402a0 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f  e] is returned o
402b0 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20  therwise..**.** 
402c0 53 68 61 72 65 64 20 63 61 63 68 65 20 69 73 20  Shared cache is 
402d0 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61  disabled by defa
402e0 75 6c 74 2e 20 42 75 74 20 74 68 69 73 20 6d 69  ult. But this mi
402f0 67 68 74 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a  ght change in.**
40300 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
40310 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41 70 70   of SQLite.  App
40320 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 63  lications that c
40330 61 72 65 20 61 62 6f 75 74 20 73 68 61 72 65 64  are about shared
40340 0a 2a 2a 20 63 61 63 68 65 20 73 65 74 74 69 6e  .** cache settin
40350 67 20 73 68 6f 75 6c 64 20 73 65 74 20 69 74 20  g should set it 
40360 65 78 70 6c 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a  explicitly..**.*
40370 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
40380 0a 2a 2a 20 7b 48 31 30 33 33 31 7d 20 41 20 73  .** {H10331} A s
40390 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61  uccessful invoca
403a0 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33  tion of [sqlite3
403b0 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  _enable_shared_c
403c0 61 63 68 65 28 42 29 5d 0a 2a 2a 20 20 20 20 20  ache(B)].**     
403d0 20 20 20 20 20 77 69 6c 6c 20 65 6e 61 62 6c 65       will enable
403e0 20 6f 72 20 64 69 73 61 62 6c 65 20 73 68 61 72   or disable shar
403f0 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20 66 6f  ed cache mode fo
40400 72 20 61 6e 79 20 73 75 62 73 65 71 75 65 6e 74  r any subsequent
40410 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63  ly.**          c
40420 72 65 61 74 65 64 20 5b 64 61 74 61 62 61 73 65  reated [database
40430 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 6e 20   connection] in 
40440 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
40450 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 33 33 36 7d  ..**.** {H10336}
40460 20 57 68 65 6e 20 73 68 61 72 65 64 20 63 61 63   When shared cac
40470 68 65 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74  he is enabled, t
40480 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  he [sqlite3_crea
40490 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 0a 2a 2a 20  te_module()].** 
404a0 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61           interfa
404b0 63 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72  ce will always r
404c0 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a  eturn an error..
404d0 2a 2a 0a 2a 2a 20 7b 48 31 30 33 33 37 7d 20 54  **.** {H10337} T
404e0 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62  he [sqlite3_enab
404f0 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28  le_shared_cache(
40500 42 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65  B)] interface re
40510 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  turns.**        
40520 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66    [SQLITE_OK] if
40530 20 73 68 61 72 65 64 20 63 61 63 68 65 20 77 61   shared cache wa
40540 73 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73  s enabled or dis
40550 61 62 6c 65 64 20 73 75 63 63 65 73 73 66 75 6c  abled successful
40560 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 33 33  ly..**.** {H1033
40570 39 7d 20 53 68 61 72 65 64 20 63 61 63 68 65 20  9} Shared cache 
40580 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 64  is disabled by d
40590 65 66 61 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54  efault..*/.SQLIT
405a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
405b0 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  3_enable_shared_
405c0 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a  cache(int);../*.
405d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 74 74  ** CAPI3REF: Att
405e0 65 6d 70 74 20 54 6f 20 46 72 65 65 20 48 65 61  empt To Free Hea
405f0 70 20 4d 65 6d 6f 72 79 20 7b 48 31 37 33 34 30  p Memory {H17340
40600 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a  } <S30220>.**.**
40610 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c   The sqlite3_rel
40620 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e  ease_memory() in
40630 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73  terface attempts
40640 20 74 6f 20 66 72 65 65 20 4e 20 62 79 74 65 73   to free N bytes
40650 0a 2a 2a 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f  .** of heap memo
40660 72 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69  ry by deallocati
40670 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c  ng non-essential
40680 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
40690 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74  ons.** held by t
406a0 68 65 20 64 61 74 61 62 61 73 65 20 6c 69 62 72  he database libr
406b0 61 72 79 2e 20 7b 45 4e 44 7d 20 20 4d 65 6d 6f  ary. {END}  Memo
406c0 72 79 20 75 73 65 64 20 74 6f 20 63 61 63 68 65  ry used to cache
406d0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67   database.** pag
406e0 65 73 20 74 6f 20 69 6d 70 72 6f 76 65 20 70 65  es to improve pe
406f0 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6e 20  rformance is an 
40700 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65  example of non-e
40710 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 2e  ssential memory.
40720 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  .** sqlite3_rele
40730 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 72 65 74  ase_memory() ret
40740 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
40750 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c  of bytes actuall
40760 79 20 66 72 65 65 64 2c 0a 2a 2a 20 77 68 69 63  y freed,.** whic
40770 68 20 6d 69 67 68 74 20 62 65 20 6d 6f 72 65 20  h might be more 
40780 6f 72 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65  or less than the
40790 20 61 6d 6f 75 6e 74 20 72 65 71 75 65 73 74 65   amount requeste
407a0 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  d..**.** INVARIA
407b0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33  NTS:.**.** {H173
407c0 34 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  41} The [sqlite3
407d0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
407e0 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 74  N)] interface at
407f0 74 65 6d 70 74 73 20 74 6f 0a 2a 2a 20 20 20 20  tempts to.**    
40800 20 20 20 20 20 20 66 72 65 65 20 4e 20 62 79 74        free N byt
40810 65 73 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72  es of heap memor
40820 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e  y by deallocatin
40830 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 0a  g non-essential.
40840 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f  **          memo
40850 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 68  ry allocations h
40860 65 6c 64 20 62 79 20 74 68 65 20 64 61 74 61 62  eld by the datab
40870 61 73 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a  ase library..**.
40880 2a 2a 20 7b 48 31 36 33 34 32 7d 20 54 68 65 20  ** {H16342} The 
40890 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65  [sqlite3_release
408a0 5f 6d 65 6d 6f 72 79 28 4e 29 5d 20 72 65 74 75  _memory(N)] retu
408b0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a  rns the number.*
408c0 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 62 79  *          of by
408d0 74 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65  tes actually fre
408e0 65 64 2c 20 77 68 69 63 68 20 6d 69 67 68 74 20  ed, which might 
408f0 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65 73 73 0a  be more or less.
40900 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 61 6e  **          than
40910 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75   the amount requ
40920 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ested..*/.SQLITE
40930 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
40940 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
40950 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  int);../*.** CAP
40960 49 33 52 45 46 3a 20 49 6d 70 6f 73 65 20 41 20  I3REF: Impose A 
40970 4c 69 6d 69 74 20 4f 6e 20 48 65 61 70 20 53 69  Limit On Heap Si
40980 7a 65 20 7b 48 31 37 33 35 30 7d 20 3c 53 33 30  ze {H17350} <S30
40990 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  220>.**.** The s
409a0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
409b0 5f 6c 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61  _limit() interfa
409c0 63 65 20 70 6c 61 63 65 73 20 61 20 22 73 6f 66  ce places a "sof
409d0 74 22 20 6c 69 6d 69 74 0a 2a 2a 20 6f 6e 20 74  t" limit.** on t
409e0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61  he amount of hea
409f0 70 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61  p memory that ma
40a00 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62  y be allocated b
40a10 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 49 66 20  y SQLite..** If 
40a20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f  an internal allo
40a30 63 61 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73  cation is reques
40a40 74 65 64 20 74 68 61 74 20 77 6f 75 6c 64 20 65  ted that would e
40a50 78 63 65 65 64 20 74 68 65 0a 2a 2a 20 73 6f 66  xceed the.** sof
40a60 74 20 68 65 61 70 20 6c 69 6d 69 74 2c 20 5b 73  t heap limit, [s
40a70 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
40a80 65 6d 6f 72 79 28 29 5d 20 69 73 20 69 6e 76 6f  emory()] is invo
40a90 6b 65 64 20 6f 6e 65 20 6f 72 0a 2a 2a 20 6d 6f  ked one or.** mo
40aa0 72 65 20 74 69 6d 65 73 20 74 6f 20 66 72 65 65  re times to free
40ab0 20 75 70 20 73 6f 6d 65 20 73 70 61 63 65 20 62   up some space b
40ac0 65 66 6f 72 65 20 74 68 65 20 61 6c 6c 6f 63 61  efore the alloca
40ad0 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65  tion is performe
40ae0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 6d  d..**.** The lim
40af0 69 74 20 69 73 20 63 61 6c 6c 65 64 20 22 73 6f  it is called "so
40b00 66 74 22 2c 20 62 65 63 61 75 73 65 20 69 66 20  ft", because if 
40b10 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65  [sqlite3_release
40b20 5f 6d 65 6d 6f 72 79 28 29 5d 0a 2a 2a 20 63 61  _memory()].** ca
40b30 6e 6e 6f 74 20 66 72 65 65 20 73 75 66 66 69 63  nnot free suffic
40b40 69 65 6e 74 20 6d 65 6d 6f 72 79 20 74 6f 20 70  ient memory to p
40b50 72 65 76 65 6e 74 20 74 68 65 20 6c 69 6d 69 74  revent the limit
40b60 20 66 72 6f 6d 20 62 65 69 6e 67 20 65 78 63 65   from being exce
40b70 65 64 65 64 2c 0a 2a 2a 20 74 68 65 20 6d 65 6d  eded,.** the mem
40b80 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ory is allocated
40b90 20 61 6e 79 77 61 79 20 61 6e 64 20 74 68 65 20   anyway and the 
40ba0 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f  current operatio
40bb0 6e 20 70 72 6f 63 65 65 64 73 2e 0a 2a 2a 0a 2a  n proceeds..**.*
40bc0 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20  * A negative or 
40bd0 7a 65 72 6f 20 76 61 6c 75 65 20 66 6f 72 20 4e  zero value for N
40be0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72   means that ther
40bf0 65 20 69 73 20 6e 6f 20 73 6f 66 74 20 68 65 61  e is no soft hea
40c00 70 20 6c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 5b  p limit and.** [
40c10 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
40c20 6d 65 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20 6f  memory()] will o
40c30 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68  nly be called wh
40c40 65 6e 20 6d 65 6d 6f 72 79 20 69 73 20 65 78 68  en memory is exh
40c50 61 75 73 74 65 64 2e 0a 2a 2a 20 54 68 65 20 64  austed..** The d
40c60 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
40c70 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   the soft heap l
40c80 69 6d 69 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a  imit is zero..**
40c90 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73  .** SQLite makes
40ca0 20 61 20 62 65 73 74 20 65 66 66 6f 72 74 20 74   a best effort t
40cb0 6f 20 68 6f 6e 6f 72 20 74 68 65 20 73 6f 66 74  o honor the soft
40cc0 20 68 65 61 70 20 6c 69 6d 69 74 2e 0a 2a 2a 20   heap limit..** 
40cd0 42 75 74 20 69 66 20 74 68 65 20 73 6f 66 74 20  But if the soft 
40ce0 68 65 61 70 20 6c 69 6d 69 74 20 63 61 6e 6e 6f  heap limit canno
40cf0 74 20 62 65 20 68 6f 6e 6f 72 65 64 2c 20 65 78  t be honored, ex
40d00 65 63 75 74 69 6f 6e 20 77 69 6c 6c 0a 2a 2a 20  ecution will.** 
40d10 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 6f 75 74  continue without
40d20 20 65 72 72 6f 72 20 6f 72 20 6e 6f 74 69 66 69   error or notifi
40d30 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73  cation.  This is
40d40 20 77 68 79 20 74 68 65 20 6c 69 6d 69 74 20 69   why the limit i
40d50 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 20 22 73  s.** called a "s
40d60 6f 66 74 22 20 6c 69 6d 69 74 2e 20 20 49 74 20  oft" limit.  It 
40d70 69 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79  is advisory only
40d80 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 6f 72 20 74 6f  ..**.** Prior to
40d90 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
40da0 33 2e 35 2e 30 2c 20 74 68 69 73 20 72 6f 75 74  3.5.0, this rout
40db0 69 6e 65 20 6f 6e 6c 79 20 63 6f 6e 73 74 72 61  ine only constra
40dc0 69 6e 65 64 20 74 68 65 20 6d 65 6d 6f 72 79 0a  ined the memory.
40dd0 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  ** allocated by 
40de0 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20  a single thread 
40df0 2d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  - the same threa
40e00 64 20 69 6e 20 77 68 69 63 68 20 74 68 69 73 20  d in which this 
40e10 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 75 6e 73 2e  routine.** runs.
40e20 20 20 42 65 67 69 6e 6e 69 6e 67 20 77 69 74 68    Beginning with
40e30 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
40e40 33 2e 35 2e 30 2c 20 74 68 65 20 73 6f 66 74 20  3.5.0, the soft 
40e50 68 65 61 70 20 6c 69 6d 69 74 20 69 73 0a 2a 2a  heap limit is.**
40e60 20 61 70 70 6c 69 65 64 20 74 6f 20 61 6c 6c 20   applied to all 
40e70 74 68 72 65 61 64 73 2e 20 54 68 65 20 76 61 6c  threads. The val
40e80 75 65 20 73 70 65 63 69 66 69 65 64 20 66 6f 72  ue specified for
40e90 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   the soft heap l
40ea0 69 6d 69 74 0a 2a 2a 20 69 73 20 61 6e 20 75 70  imit.** is an up
40eb0 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65  per bound on the
40ec0 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c   total memory al
40ed0 6c 6f 63 61 74 69 6f 6e 20 66 6f 72 20 61 6c 6c  location for all
40ee0 20 74 68 72 65 61 64 73 2e 20 49 6e 0a 2a 2a 20   threads. In.** 
40ef0 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 20 74 68  version 3.5.0 th
40f00 65 72 65 20 69 73 20 6e 6f 20 6d 65 63 68 61 6e  ere is no mechan
40f10 69 73 6d 20 66 6f 72 20 6c 69 6d 69 74 69 6e 67  ism for limiting
40f20 20 74 68 65 20 68 65 61 70 20 75 73 61 67 65 20   the heap usage 
40f30 66 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61  for.** individua
40f40 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a  l threads..**.**
40f50 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
40f60 2a 2a 20 7b 48 31 36 33 35 31 7d 20 54 68 65 20  ** {H16351} The 
40f70 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65  [sqlite3_soft_he
40f80 61 70 5f 6c 69 6d 69 74 28 4e 29 5d 20 69 6e 74  ap_limit(N)] int
40f90 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 61 20  erface places a 
40fa0 73 6f 66 74 20 6c 69 6d 69 74 0a 2a 2a 20 20 20  soft limit.**   
40fb0 20 20 20 20 20 20 20 6f 66 20 4e 20 62 79 74 65         of N byte
40fc0 73 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  s on the amount 
40fd0 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74  of heap memory t
40fe0 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63  hat may be alloc
40ff0 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  ated.**         
41000 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
41010 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71  malloc()] or [sq
41020 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d  lite3_realloc()]
41030 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 0a 2a 2a   at any point.**
41040 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 69 6d            in tim
41050 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 33 35 32  e..**.** {H16352
41060 7d 20 49 66 20 61 20 63 61 6c 6c 20 74 6f 20 5b  } If a call to [
41070 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
41080 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65  ] or [sqlite3_re
41090 61 6c 6c 6f 63 28 29 5d 20 77 6f 75 6c 64 0a 2a  alloc()] would.*
410a0 2a 20 20 20 20 20 20 20 20 20 20 63 61 75 73 65  *          cause
410b0 20 74 68 65 20 74 6f 74 61 6c 20 61 6d 6f 75 6e   the total amoun
410c0 74 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d  t of allocated m
410d0 65 6d 6f 72 79 20 74 6f 20 65 78 63 65 65 64 20  emory to exceed 
410e0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
410f0 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 2c  soft heap limit,
41100 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72   then [sqlite3_r
41110 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d  elease_memory()]
41120 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 20   is invoked.**  
41130 20 20 20 20 20 20 20 20 69 6e 20 61 6e 20 61 74          in an at
41140 74 65 6d 70 74 20 74 6f 20 72 65 64 75 63 65 20  tempt to reduce 
41150 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 61 67 65  the memory usage
41160 20 70 72 69 6f 72 20 74 6f 20 70 72 6f 63 65 65   prior to procee
41170 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  ding.**         
41180 20 77 69 74 68 20 74 68 65 20 6d 65 6d 6f 72 79   with the memory
41190 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65   allocation atte
411a0 6d 70 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 33  mpt..**.** {H163
411b0 35 33 7d 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71  53} Calls to [sq
411c0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20  lite3_malloc()] 
411d0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c  or [sqlite3_real
411e0 6c 6f 63 28 29 5d 20 74 68 61 74 20 74 72 69 67  loc()] that trig
411f0 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ger.**          
41200 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 64 75  attempts to redu
41210 63 65 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20  ce memory usage 
41220 74 68 72 6f 75 67 68 20 74 68 65 20 73 6f 66 74  through the soft
41230 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 20   heap limit.**  
41240 20 20 20 20 20 20 20 20 6d 65 63 68 61 6e 69 73          mechanis
41250 6d 20 63 6f 6e 74 69 6e 75 65 20 65 76 65 6e 20  m continue even 
41260 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74  if the attempt t
41270 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 0a  o reduce memory.
41280 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73 61 67  **          usag
41290 65 20 69 73 20 75 6e 73 75 63 63 65 73 73 66 75  e is unsuccessfu
412a0 6c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 33 35 34  l..**.** {H16354
412b0 7d 20 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20  } A negative or 
412c0 7a 65 72 6f 20 76 61 6c 75 65 20 66 6f 72 20 4e  zero value for N
412d0 20 69 6e 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a   in a call to.**
412e0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
412f0 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
41300 69 74 28 4e 29 5d 20 6d 65 61 6e 73 20 74 68 61  it(N)] means tha
41310 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f  t there is no so
41320 66 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68  ft.**          h
41330 65 61 70 20 6c 69 6d 69 74 20 61 6e 64 20 5b 73  eap limit and [s
41340 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
41350 65 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20 6f 6e  emory()] will on
41360 6c 79 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20  ly be.**        
41370 20 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65    called when me
41380 6d 6f 72 79 20 69 73 20 63 6f 6d 70 6c 65 74 65  mory is complete
41390 6c 79 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a  ly exhausted..**
413a0 0a 2a 2a 20 7b 48 31 36 33 35 35 7d 20 54 68 65  .** {H16355} The
413b0 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66   default value f
413c0 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70  or the soft heap
413d0 20 6c 69 6d 69 74 20 69 73 20 7a 65 72 6f 2e 0a   limit is zero..
413e0 2a 2a 0a 2a 2a 20 7b 48 31 36 33 35 38 7d 20 45  **.** {H16358} E
413f0 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ach call to [sql
41400 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
41410 69 6d 69 74 28 4e 29 5d 20 6f 76 65 72 72 69 64  imit(N)] overrid
41420 65 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  es the.**       
41430 20 20 20 76 61 6c 75 65 73 20 73 65 74 20 62 79     values set by
41440 20 61 6c 6c 20 70 72 69 6f 72 20 63 61 6c 6c 73   all prior calls
41450 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
41460 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66  void sqlite3_sof
41470 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74  t_heap_limit(int
41480 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
41490 45 46 3a 20 45 78 74 72 61 63 74 20 4d 65 74 61  EF: Extract Meta
414a0 64 61 74 61 20 41 62 6f 75 74 20 41 20 43 6f 6c  data About A Col
414b0 75 6d 6e 20 4f 66 20 41 20 54 61 62 6c 65 20 7b  umn Of A Table {
414c0 48 31 32 38 35 30 7d 20 3c 53 36 30 33 30 30 3e  H12850} <S60300>
414d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
414e0 69 6e 65 20 72 65 74 75 72 6e 73 20 6d 65 74 61  ine returns meta
414f0 64 61 74 61 20 61 62 6f 75 74 20 61 20 73 70 65  data about a spe
41500 63 69 66 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20  cific column of 
41510 61 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 64 61  a specific.** da
41520 74 61 62 61 73 65 20 74 61 62 6c 65 20 61 63 63  tabase table acc
41530 65 73 73 69 62 6c 65 20 75 73 69 6e 67 20 74 68  essible using th
41540 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
41550 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a  ection] handle.*
41560 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  * passed as the 
41570 66 69 72 73 74 20 66 75 6e 63 74 69 6f 6e 20 61  first function a
41580 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
41590 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 64 65  he column is ide
415a0 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73  ntified by the s
415b0 65 63 6f 6e 64 2c 20 74 68 69 72 64 20 61 6e 64  econd, third and
415c0 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65   fourth paramete
415d0 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 66 75  rs to.** this fu
415e0 6e 63 74 69 6f 6e 2e 20 54 68 65 20 73 65 63 6f  nction. The seco
415f0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nd parameter is 
41600 65 69 74 68 65 72 20 74 68 65 20 6e 61 6d 65 20  either the name 
41610 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  of the database.
41620 2a 2a 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c  ** (i.e. "main",
41630 20 22 74 65 6d 70 22 20 6f 72 20 61 6e 20 61 74   "temp" or an at
41640 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 29  tached database)
41650 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
41660 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 74 61 62  specified.** tab
41670 6c 65 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 69  le or NULL. If i
41680 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  t is NULL, then 
41690 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
416a0 61 62 61 73 65 73 20 61 72 65 20 73 65 61 72 63  abases are searc
416b0 68 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74  hed.** for the t
416c0 61 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 73  able using the s
416d0 61 6d 65 20 61 6c 67 6f 72 69 74 68 6d 20 75 73  ame algorithm us
416e0 65 64 20 62 79 20 74 68 65 20 64 61 74 61 62 61  ed by the databa
416f0 73 65 20 65 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20  se engine to.** 
41700 72 65 73 6f 6c 76 65 20 75 6e 71 75 61 6c 69 66  resolve unqualif
41710 69 65 64 20 74 61 62 6c 65 20 72 65 66 65 72 65  ied table refere
41720 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nces..**.** The 
41730 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68  third and fourth
41740 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74   parameters to t
41750 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 72 65  his function are
41760 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 63   the table and c
41770 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66  olumn.** name of
41780 20 74 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c   the desired col
41790 75 6d 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c  umn, respectivel
417a0 79 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68  y. Neither of th
417b0 65 73 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a  ese parameters.*
417c0 2a 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a  * may be NULL..*
417d0 2a 0a 2a 2a 20 4d 65 74 61 64 61 74 61 20 69 73  *.** Metadata is
417e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 77 72 69   returned by wri
417f0 74 69 6e 67 20 74 6f 20 74 68 65 20 6d 65 6d 6f  ting to the memo
41800 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 70 61 73  ry locations pas
41810 73 65 64 20 61 73 20 74 68 65 20 35 74 68 0a 2a  sed as the 5th.*
41820 2a 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  * and subsequent
41830 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74   parameters to t
41840 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 41 6e  his function. An
41850 79 20 6f 66 20 74 68 65 73 65 20 61 72 67 75 6d  y of these argum
41860 65 6e 74 73 20 6d 61 79 20 62 65 0a 2a 2a 20 4e  ents may be.** N
41870 55 4c 4c 2c 20 69 6e 20 77 68 69 63 68 20 63 61  ULL, in which ca
41880 73 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  se the correspon
41890 64 69 6e 67 20 65 6c 65 6d 65 6e 74 20 6f 66 20  ding element of 
418a0 6d 65 74 61 64 61 74 61 20 69 73 20 6f 6d 69 74  metadata is omit
418b0 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63  ted..**.** <bloc
418c0 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c  kquote>.** <tabl
418d0 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a  e border="1">.**
418e0 20 3c 74 72 3e 3c 74 68 3e 20 50 61 72 61 6d 65   <tr><th> Parame
418f0 74 65 72 20 3c 74 68 3e 20 4f 75 74 70 75 74 3c  ter <th> Output<
41900 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 44 65  br>Type <th>  De
41910 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20  scription.**.** 
41920 3c 74 72 3e 3c 74 64 3e 20 35 74 68 20 3c 74 64  <tr><td> 5th <td
41930 3e 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74  > const char* <t
41940 64 3e 20 44 61 74 61 20 74 79 70 65 0a 2a 2a 20  d> Data type.** 
41950 3c 74 72 3e 3c 74 64 3e 20 36 74 68 20 3c 74 64  <tr><td> 6th <td
41960 3e 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74  > const char* <t
41970 64 3e 20 4e 61 6d 65 20 6f 66 20 64 65 66 61 75  d> Name of defau
41980 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  lt collation seq
41990 75 65 6e 63 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64  uence.** <tr><td
419a0 3e 20 37 74 68 20 3c 74 64 3e 20 69 6e 74 20 20  > 7th <td> int  
419b0 20 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65         <td> True
419c0 20 69 66 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61   if column has a
419d0 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72   NOT NULL constr
419e0 61 69 6e 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  aint.** <tr><td>
419f0 20 38 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20   8th <td> int   
41a00 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20        <td> True 
41a10 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72  if column is par
41a20 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59  t of the PRIMARY
41a30 20 4b 45 59 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e   KEY.** <tr><td>
41a40 20 39 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20   9th <td> int   
41a50 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20        <td> True 
41a60 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 5b 41 55  if column is [AU
41a70 54 4f 49 4e 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20  TOINCREMENT].** 
41a80 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c  </table>.** </bl
41a90 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20  ockquote>.**.** 
41aa0 54 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74  The memory point
41ab0 65 64 20 74 6f 20 62 79 20 74 68 65 20 63 68 61  ed to by the cha
41ac0 72 61 63 74 65 72 20 70 6f 69 6e 74 65 72 73 20  racter pointers 
41ad0 72 65 74 75 72 6e 65 64 20 66 6f 72 20 74 68 65  returned for the
41ae0 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  .** declaration 
41af0 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69  type and collati
41b00 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 76  on sequence is v
41b10 61 6c 69 64 20 6f 6e 6c 79 20 75 6e 74 69 6c 20  alid only until 
41b20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c  the next.** call
41b30 20 74 6f 20 61 6e 79 20 53 51 4c 69 74 65 20 41   to any SQLite A
41b40 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  PI function..**.
41b50 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63 69 66  ** If the specif
41b60 69 65 64 20 74 61 62 6c 65 20 69 73 20 61 63 74  ied table is act
41b70 75 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 61 6e  ually a view, an
41b80 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
41b90 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
41ba0 20 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65   If the specifie
41bb0 64 20 63 6f 6c 75 6d 6e 20 69 73 20 22 72 6f 77  d column is "row
41bc0 69 64 22 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f  id", "oid" or "_
41bd0 72 6f 77 69 64 5f 22 20 61 6e 64 20 61 6e 0a 2a  rowid_" and an.*
41be0 2a 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41  * [INTEGER PRIMA
41bf0 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 20 68  RY KEY] column h
41c00 61 73 20 62 65 65 6e 20 65 78 70 6c 69 63 69 74  as been explicit
41c10 6c 79 20 64 65 63 6c 61 72 65 64 2c 20 74 68 65  ly declared, the
41c20 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20  n the output.** 
41c30 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 73  parameters are s
41c40 65 74 20 66 6f 72 20 74 68 65 20 65 78 70 6c 69  et for the expli
41c50 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63  citly declared c
41c60 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 72 65 20  olumn. If there 
41c70 69 73 20 6e 6f 0a 2a 2a 20 65 78 70 6c 69 63 69  is no.** explici
41c80 74 6c 79 20 64 65 63 6c 61 72 65 64 20 5b 49 4e  tly declared [IN
41c90 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
41ca0 59 5d 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20  Y] column, then 
41cb0 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61  the output.** pa
41cc0 72 61 6d 65 74 65 72 73 20 61 72 65 20 73 65 74  rameters are set
41cd0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
41ce0 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20  ** <pre>.**     
41cf0 64 61 74 61 20 74 79 70 65 3a 20 22 49 4e 54 45  data type: "INTE
41d00 47 45 52 22 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c  GER".**     coll
41d10 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20  ation sequence: 
41d20 22 42 49 4e 41 52 59 22 0a 2a 2a 20 20 20 20 20  "BINARY".**     
41d30 6e 6f 74 20 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20  not null: 0.**  
41d40 20 20 20 70 72 69 6d 61 72 79 20 6b 65 79 3a 20     primary key: 
41d50 31 0a 2a 2a 20 20 20 20 20 61 75 74 6f 20 69 6e  1.**     auto in
41d60 63 72 65 6d 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f  crement: 0.** </
41d70 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  pre>.**.** This 
41d80 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6c 6f 61  function may loa
41d90 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 63  d one or more sc
41da0 68 65 6d 61 73 20 66 72 6f 6d 20 64 61 74 61 62  hemas from datab
41db0 61 73 65 20 66 69 6c 65 73 2e 20 49 66 20 61 6e  ase files. If an
41dc0 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73  .** error occurs
41dd0 20 64 75 72 69 6e 67 20 74 68 69 73 20 70 72 6f   during this pro
41de0 63 65 73 73 2c 20 6f 72 20 69 66 20 74 68 65 20  cess, or if the 
41df0 72 65 71 75 65 73 74 65 64 20 74 61 62 6c 65 20  requested table 
41e00 6f 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e  or column.** can
41e10 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 61 6e  not be found, an
41e20 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
41e30 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e   returned and an
41e40 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6c   error message l
41e50 65 66 74 0a 2a 2a 20 69 6e 20 74 68 65 20 5b 64  eft.** in the [d
41e60 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
41e70 6f 6e 5d 20 28 74 6f 20 62 65 20 72 65 74 72 69  on] (to be retri
41e80 65 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74  eved using sqlit
41e90 65 33 5f 65 72 72 6d 73 67 28 29 29 2e 0a 2a 2a  e3_errmsg())..**
41ea0 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20  .** This API is 
41eb0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
41ec0 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 61  f the library wa
41ed0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  s compiled with 
41ee0 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45  the.** [SQLITE_E
41ef0 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54  NABLE_COLUMN_MET
41f00 41 44 41 54 41 5d 20 43 2d 70 72 65 70 72 6f 63  ADATA] C-preproc
41f10 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66  essor symbol def
41f20 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ined..*/.SQLITE_
41f30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
41f40 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
41f50 61 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65 33  adata(.  sqlite3
41f60 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
41f70 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
41f80 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  on handle */.  c
41f90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61  onst char *zDbNa
41fa0 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 61  me,        /* Da
41fb0 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e  tabase name or N
41fc0 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ULL */.  const c
41fd0 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c  har *zTableName,
41fe0 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61       /* Table na
41ff0 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  me */.  const ch
42000 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c  ar *zColumnName,
42010 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61      /* Column na
42020 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e  me */.  char con
42030 73 74 20 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c  st **pzDataType,
42040 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44      /* OUTPUT: D
42050 65 63 6c 61 72 65 64 20 64 61 74 61 20 74 79 70  eclared data typ
42060 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73  e */.  char cons
42070 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20  t **pzCollSeq,  
42080 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f     /* OUTPUT: Co
42090 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
420a0 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   name */.  int *
420b0 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20  pNotNull,       
420c0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54         /* OUTPUT
420d0 3a 20 54 72 75 65 20 69 66 20 4e 4f 54 20 4e 55  : True if NOT NU
420e0 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78  LL constraint ex
420f0 69 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ists */.  int *p
42100 50 72 69 6d 61 72 79 4b 65 79 2c 20 20 20 20 20  PrimaryKey,     
42110 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a        /* OUTPUT:
42120 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20   True if column 
42130 70 61 72 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20  part of PK */.  
42140 69 6e 74 20 2a 70 41 75 74 6f 69 6e 63 20 20 20  int *pAutoinc   
42150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
42160 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63  UTPUT: True if c
42170 6f 6c 75 6d 6e 20 69 73 20 61 75 74 6f 2d 69 6e  olumn is auto-in
42180 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f  crement */.);../
42190 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c  *.** CAPI3REF: L
421a0 6f 61 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e  oad An Extension
421b0 20 7b 48 31 32 36 30 30 7d 20 3c 53 32 30 35 30   {H12600} <S2050
421c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  0>.**.** This in
421d0 74 65 72 66 61 63 65 20 6c 6f 61 64 73 20 61 6e  terface loads an
421e0 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f   SQLite extensio
421f0 6e 20 6c 69 62 72 61 72 79 20 66 72 6f 6d 20 74  n library from t
42200 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a  he named file..*
42210 2a 0a 2a 2a 20 7b 48 31 32 36 30 31 7d 20 54 68  *.** {H12601} Th
42220 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65  e sqlite3_load_e
42230 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72  xtension() inter
42240 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f  face attempts to
42250 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20 20 20 20 20   load an.**     
42260 20 20 20 20 20 53 51 4c 69 74 65 20 65 78 74 65       SQLite exte
42270 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f  nsion library co
42280 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66  ntained in the f
42290 69 6c 65 20 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a  ile zFile..**.**
422a0 20 7b 48 31 32 36 30 32 7d 20 54 68 65 20 65 6e   {H12602} The en
422b0 74 72 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72  try point is zPr
422c0 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30  oc..**.** {H1260
422d0 33 7d 20 7a 50 72 6f 63 20 6d 61 79 20 62 65 20  3} zProc may be 
422e0 30 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  0, in which case
422f0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
42300 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a 2a 20   entry point.** 
42310 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74           default
42320 73 20 74 6f 20 22 73 71 6c 69 74 65 33 5f 65 78  s to "sqlite3_ex
42330 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a  tension_init"..*
42340 2a 0a 2a 2a 20 7b 48 31 32 36 30 34 7d 20 54 68  *.** {H12604} Th
42350 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65  e sqlite3_load_e
42360 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72  xtension() inter
42370 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 75 72  face shall retur
42380 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53  n.**          [S
42390 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63  QLITE_OK] on suc
423a0 63 65 73 73 20 61 6e 64 20 5b 53 51 4c 49 54 45  cess and [SQLITE
423b0 5f 45 52 52 4f 52 5d 20 69 66 20 73 6f 6d 65 74  _ERROR] if somet
423c0 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
423d0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 35 7d 20  .**.** {H12605} 
423e0 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
423f0 72 73 20 61 6e 64 20 70 7a 45 72 72 4d 73 67 20  rs and pzErrMsg 
42400 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74  is not 0, then t
42410 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
42420 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74  sqlite3_load_ext
42430 65 6e 73 69 6f 6e 28 29 5d 20 69 6e 74 65 72 66  ension()] interf
42440 61 63 65 20 73 68 61 6c 6c 20 61 74 74 65 6d 70  ace shall attemp
42450 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  t to.**         
42460 20 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20   fill *pzErrMsg 
42470 77 69 74 68 20 65 72 72 6f 72 20 6d 65 73 73 61  with error messa
42480 67 65 20 74 65 78 74 20 73 74 6f 72 65 64 20 69  ge text stored i
42490 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20  n memory.**     
424a0 20 20 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72       obtained fr
424b0 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  om [sqlite3_mall
424c0 6f 63 28 29 5d 2e 20 7b 45 4e 44 7d 20 20 54 68  oc()]. {END}  Th
424d0 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
424e0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  on.**          s
424f0 68 6f 75 6c 64 20 66 72 65 65 20 74 68 69 73 20  hould free this 
42500 6d 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e  memory by callin
42510 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  g [sqlite3_free(
42520 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30  )]..**.** {H1260
42530 36 7d 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  6} Extension loa
42540 64 69 6e 67 20 6d 75 73 74 20 62 65 20 65 6e 61  ding must be ena
42550 62 6c 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20  bled using.**   
42560 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
42570 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65  enable_load_exte
42580 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74  nsion()] prior t
42590 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 41  o calling this A
425a0 50 49 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  PI,.**          
425b0 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72  otherwise an err
425c0 6f 72 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  or will be retur
425d0 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ned..*/.SQLITE_A
425e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
425f0 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20  oad_extension(. 
42600 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
42610 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74         /* Load t
42620 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74  he extension int
42630 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
42640 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
42650 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
42660 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
42670 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
42680 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65  ary containing e
42690 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f  xtension */.  co
426a0 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c  nst char *zProc,
426b0 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69      /* Entry poi
426c0 6e 74 2e 20 20 44 65 72 69 76 65 64 20 66 72 6f  nt.  Derived fro
426d0 6d 20 7a 46 69 6c 65 20 69 66 20 30 20 2a 2f 0a  m zFile if 0 */.
426e0 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73    char **pzErrMs
426f0 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 65  g       /* Put e
42700 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72  rror message her
42710 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b  e if not 0 */.);
42720 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
42730 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61  : Enable Or Disa
42740 62 6c 65 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f  ble Extension Lo
42750 61 64 69 6e 67 20 7b 48 31 32 36 32 30 7d 20 3c  ading {H12620} <
42760 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f  S20500>.**.** So
42770 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70 65 6e 20   as not to open 
42780 73 65 63 75 72 69 74 79 20 68 6f 6c 65 73 20 69  security holes i
42790 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69 63 61 74  n older applicat
427a0 69 6f 6e 73 20 74 68 61 74 20 61 72 65 0a 2a 2a  ions that are.**
427b0 20 75 6e 70 72 65 70 61 72 65 64 20 74 6f 20 64   unprepared to d
427c0 65 61 6c 20 77 69 74 68 20 65 78 74 65 6e 73 69  eal with extensi
427d0 6f 6e 20 6c 6f 61 64 69 6e 67 2c 20 61 6e 64 20  on loading, and 
427e0 61 73 20 61 20 6d 65 61 6e 73 20 6f 66 20 64 69  as a means of di
427f0 73 61 62 6c 69 6e 67 0a 2a 2a 20 65 78 74 65 6e  sabling.** exten
42800 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 77 68 69  sion loading whi
42810 6c 65 20 65 76 61 6c 75 61 74 69 6e 67 20 75 73  le evaluating us
42820 65 72 2d 65 6e 74 65 72 65 64 20 53 51 4c 2c 20  er-entered SQL, 
42830 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 41 50  the following AP
42840 49 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64  I.** is provided
42850 20 74 6f 20 74 75 72 6e 20 74 68 65 20 5b 73 71   to turn the [sq
42860 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
42870 73 69 6f 6e 28 29 5d 20 6d 65 63 68 61 6e 69 73  sion()] mechanis
42880 6d 20 6f 6e 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a  m on and off..**
42890 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f  .** Extension lo
428a0 61 64 69 6e 67 20 69 73 20 6f 66 66 20 62 79 20  ading is off by 
428b0 64 65 66 61 75 6c 74 2e 20 53 65 65 20 74 69 63  default. See tic
428c0 6b 65 74 20 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a  ket #1863..**.**
428d0 20 7b 48 31 32 36 32 31 7d 20 43 61 6c 6c 20 74   {H12621} Call t
428e0 68 65 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  he sqlite3_enabl
428f0 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  e_load_extension
42900 28 29 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20  () routine with 
42910 6f 6e 6f 66 66 3d 3d 31 0a 2a 2a 20 20 20 20 20  onoff==1.**     
42920 20 20 20 20 20 74 6f 20 74 75 72 6e 20 65 78 74       to turn ext
42930 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f  ension loading o
42940 6e 20 61 6e 64 20 63 61 6c 6c 20 69 74 20 77 69  n and call it wi
42950 74 68 20 6f 6e 6f 66 66 3d 3d 30 20 74 6f 20 74  th onoff==0 to t
42960 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  urn.**          
42970 69 74 20 62 61 63 6b 20 6f 66 66 20 61 67 61 69  it back off agai
42980 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 32  n..**.** {H12622
42990 7d 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64  } Extension load
429a0 69 6e 67 20 69 73 20 6f 66 66 20 62 79 20 64 65  ing is off by de
429b0 66 61 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fault..*/.SQLITE
429c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
429d0 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74  _enable_load_ext
429e0 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a  ension(sqlite3 *
429f0 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a  db, int onoff);.
42a00 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
42a10 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 4c   Automatically L
42a20 6f 61 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e  oad An Extension
42a30 73 20 7b 48 31 32 36 34 30 7d 20 3c 53 32 30 35  s {H12640} <S205
42a40 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41  00>.**.** This A
42a50 50 49 20 63 61 6e 20 62 65 20 69 6e 76 6f 6b 65  PI can be invoke
42a60 64 20 61 74 20 70 72 6f 67 72 61 6d 20 73 74 61  d at program sta
42a70 72 74 75 70 20 69 6e 20 6f 72 64 65 72 20 74 6f  rtup in order to
42a80 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6f 6e 65   register.** one
42a90 20 6f 72 20 6d 6f 72 65 20 73 74 61 74 69 63 61   or more statica
42aa0 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74 65 6e  lly linked exten
42ab0 73 69 6f 6e 73 20 74 68 61 74 20 77 69 6c 6c 20  sions that will 
42ac0 62 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20  be available.** 
42ad0 74 6f 20 61 6c 6c 20 6e 65 77 20 5b 64 61 74 61  to all new [data
42ae0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
42af0 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54  ]. {END}.**.** T
42b00 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72  his routine stor
42b10 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  es a pointer to 
42b20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e  the extension in
42b30 20 61 6e 20 61 72 72 61 79 20 74 68 61 74 20 69   an array that i
42b40 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72  s.** obtained fr
42b50 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  om [sqlite3_mall
42b60 6f 63 28 29 5d 2e 20 20 49 66 20 79 6f 75 20 72  oc()].  If you r
42b70 75 6e 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b  un a memory leak
42b80 20 63 68 65 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79   checker.** on y
42b90 6f 75 72 20 70 72 6f 67 72 61 6d 20 61 6e 64 20  our program and 
42ba0 69 74 20 72 65 70 6f 72 74 73 20 61 20 6c 65 61  it reports a lea
42bb0 6b 20 62 65 63 61 75 73 65 20 6f 66 20 74 68 69  k because of thi
42bc0 73 20 61 72 72 61 79 2c 20 69 6e 76 6f 6b 65 0a  s array, invoke.
42bd0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  ** [sqlite3_rese
42be0 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e  t_auto_extension
42bf0 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 73 68 75  ()] prior to shu
42c00 74 64 6f 77 6e 20 74 6f 20 66 72 65 65 20 74 68  tdown to free th
42c10 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20  e memory..**.** 
42c20 7b 48 31 32 36 34 31 7d 20 54 68 69 73 20 66 75  {H12641} This fu
42c30 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 73  nction registers
42c40 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 20 65 6e   an extension en
42c50 74 72 79 20 70 6f 69 6e 74 20 74 68 61 74 20 69  try point that i
42c60 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75  s.**          au
42c70 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76 6f  tomatically invo
42c80 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 6e  ked whenever a n
42c90 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ew [database con
42ca0 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20  nection].**     
42cb0 20 20 20 20 20 69 73 20 6f 70 65 6e 65 64 20 75       is opened u
42cc0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70  sing [sqlite3_op
42cd0 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  en()], [sqlite3_
42ce0 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20  open16()],.**   
42cf0 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c 69 74         or [sqlit
42d00 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a  e3_open_v2()]..*
42d10 2a 0a 2a 2a 20 7b 48 31 32 36 34 32 7d 20 44 75  *.** {H12642} Du
42d20 70 6c 69 63 61 74 65 20 65 78 74 65 6e 73 69 6f  plicate extensio
42d30 6e 73 20 61 72 65 20 64 65 74 65 63 74 65 64 20  ns are detected 
42d40 73 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  so calling this 
42d50 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20  routine.**      
42d60 20 20 20 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d      multiple tim
42d70 65 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  es with the same
42d80 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 68 61   extension is ha
42d90 72 6d 6c 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  rmless..**.** {H
42da0 31 32 36 34 33 7d 20 54 68 69 73 20 72 6f 75 74  12643} This rout
42db0 69 6e 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69  ine stores a poi
42dc0 6e 74 65 72 20 74 6f 20 74 68 65 20 65 78 74 65  nter to the exte
42dd0 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61  nsion in an arra
42de0 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  y.**          th
42df0 61 74 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  at is obtained f
42e00 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
42e10 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  loc()]..**.** {H
42e20 31 32 36 34 34 7d 20 41 75 74 6f 6d 61 74 69 63  12644} Automatic
42e30 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 70 70 6c   extensions appl
42e40 79 20 61 63 72 6f 73 73 20 61 6c 6c 20 74 68 72  y across all thr
42e50 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eads..*/.SQLITE_
42e60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
42e70 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76  auto_extension(v
42e80 6f 69 64 20 28 2a 78 45 6e 74 72 79 50 6f 69 6e  oid (*xEntryPoin
42e90 74 29 28 76 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a  t)(void));../*.*
42ea0 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 65  * CAPI3REF: Rese
42eb0 74 20 41 75 74 6f 6d 61 74 69 63 20 45 78 74 65  t Automatic Exte
42ec0 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48  nsion Loading {H
42ed0 31 32 36 36 30 7d 20 3c 53 32 30 35 30 30 3e 0a  12660} <S20500>.
42ee0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
42ef0 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 6c 6c  ion disables all
42f00 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69   previously regi
42f10 73 74 65 72 65 64 20 61 75 74 6f 6d 61 74 69 63  stered automatic
42f20 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20  .** extensions. 
42f30 7b 45 4e 44 7d 20 20 49 74 20 75 6e 64 6f 65 73  {END}  It undoes
42f40 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 61   the effect of a
42f50 6c 6c 20 70 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c  ll prior.** [sql
42f60 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73  ite3_auto_extens
42f70 69 6f 6e 28 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a  ion()] calls..**
42f80 0a 2a 2a 20 7b 48 31 32 36 36 31 7d 20 54 68 69  .** {H12661} Thi
42f90 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62  s function disab
42fa0 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f 75 73  les all previous
42fb0 6c 79 20 72 65 67 69 73 74 65 72 65 64 0a 2a 2a  ly registered.**
42fc0 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61            automa
42fd0 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a  tic extensions..
42fe0 2a 2a 0a 2a 2a 20 7b 48 31 32 36 36 32 7d 20 54  **.** {H12662} T
42ff0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73  his function dis
43000 61 62 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 20  ables automatic 
43010 65 78 74 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c  extensions in al
43020 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51  l threads..*/.SQ
43030 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
43040 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f  lite3_reset_auto
43050 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29  _extension(void)
43060 3b 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50  ;../*.****** EXP
43070 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a  ERIMENTAL - subj
43080 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69  ect to change wi
43090 74 68 6f 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a  thout notice ***
430a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
430b0 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
430c0 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  to the virtual-t
430d0 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 69  able mechanism i
430e0 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73  s currently cons
430f0 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20  idered.** to be 
43100 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54  experimental.  T
43110 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67  he interface mig
43120 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69 6e 63  ht change in inc
43130 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73 2e 0a  ompatible ways..
43140 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  ** If this is a 
43150 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c  problem for you,
43160 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20   do not use the 
43170 69 6e 74 65 72 66 61 63 65 20 61 74 20 74 68 69  interface at thi
43180 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  s time..**.** Wh
43190 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  en the virtual-t
431a0 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 73  able mechanism s
431b0 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 20 77 69  tabilizes, we wi
431c0 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a 2a  ll declare the.*
431d0 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69 78 65  * interface fixe
431e0 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69 6e  d, support it in
431f0 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20  definitely, and 
43200 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d  remove this comm
43210 65 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53  ent..*/../*.** S
43220 74 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62  tructures used b
43230 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  y the virtual ta
43240 62 6c 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f  ble interface.*/
43250 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
43260 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 71 6c  sqlite3_vtab sql
43270 69 74 65 33 5f 76 74 61 62 3b 0a 74 79 70 65 64  ite3_vtab;.typed
43280 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
43290 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c  3_index_info sql
432a0 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b  ite3_index_info;
432b0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
432c0 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
432d0 73 6f 72 20 73 71 6c 69 74 65 33 5f 76 74 61 62  sor sqlite3_vtab
432e0 5f 63 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66  _cursor;.typedef
432f0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
43300 6d 6f 64 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d  module sqlite3_m
43310 6f 64 75 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  odule;../*.** CA
43320 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20  PI3REF: Virtual 
43330 54 61 62 6c 65 20 4f 62 6a 65 63 74 20 7b 48 31  Table Object {H1
43340 38 30 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a  8000} <S20400>.*
43350 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69  * KEYWORDS: sqli
43360 74 65 33 5f 6d 6f 64 75 6c 65 0a 2a 2a 20 45 58  te3_module.** EX
43370 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
43380 20 41 20 6d 6f 64 75 6c 65 20 69 73 20 61 20 63   A module is a c
43390 6c 61 73 73 20 6f 66 20 76 69 72 74 75 61 6c 20  lass of virtual 
433a0 74 61 62 6c 65 73 2e 20 20 45 61 63 68 20 6d 6f  tables.  Each mo
433b0 64 75 6c 65 20 69 73 20 64 65 66 69 6e 65 64 0a  dule is defined.
433c0 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ** by an instanc
433d0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
433e0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  ng structure.  T
433f0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f  his structure co
43400 6e 73 69 73 74 73 0a 2a 2a 20 6d 6f 73 74 6c 79  nsists.** mostly
43410 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20   of methods for 
43420 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a  the module..**.*
43430 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
43440 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c   is experimental
43450 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20   and is subject 
43460 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20  to change or.** 
43470 72 65 6d 6f 76 61 6c 20 69 6e 20 66 75 74 75 72  removal in futur
43480 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51  e releases of SQ
43490 4c 69 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  Lite..*/.struct 
434a0 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b  sqlite3_module {
434b0 0a 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b  .  int iVersion;
434c0 0a 20 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65  .  int (*xCreate
434d0 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64  )(sqlite3*, void
434e0 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20   *pAux,.        
434f0 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c         int argc,
43500 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
43510 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20  st*argv,.       
43520 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
43530 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63  vtab **ppVTab, c
43540 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  har**);.  int (*
43550 78 43 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65  xConnect)(sqlite
43560 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a  3*, void *pAux,.
43570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
43580 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63  nt argc, const c
43590 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
435a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
435b0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
435c0 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a  pVTab, char**);.
435d0 20 20 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64    int (*xBestInd
435e0 65 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  ex)(sqlite3_vtab
435f0 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33   *pVTab, sqlite3
43600 5f 69 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20  _index_info*);. 
43610 20 69 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65   int (*xDisconne
43620 63 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  ct)(sqlite3_vtab
43630 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20   *pVTab);.  int 
43640 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69  (*xDestroy)(sqli
43650 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29  te3_vtab *pVTab)
43660 3b 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29  ;.  int (*xOpen)
43670 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
43680 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74  VTab, sqlite3_vt
43690 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75  ab_cursor **ppCu
436a0 72 73 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78  rsor);.  int (*x
436b0 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76  Close)(sqlite3_v
436c0 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20  tab_cursor*);.  
436d0 69 6e 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73  int (*xFilter)(s
436e0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
436f0 6f 72 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c  or*, int idxNum,
43700 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78   const char *idx
43710 53 74 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Str,.           
43720 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73       int argc, s
43730 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
43740 72 67 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e  rgv);.  int (*xN
43750 65 78 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61  ext)(sqlite3_vta
43760 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e  b_cursor*);.  in
43770 74 20 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65  t (*xEof)(sqlite
43780 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b  3_vtab_cursor*);
43790 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e  .  int (*xColumn
437a0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  )(sqlite3_vtab_c
437b0 75 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f  ursor*, sqlite3_
437c0 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a  context*, int);.
437d0 20 20 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28    int (*xRowid)(
437e0 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
437f0 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e  sor*, sqlite3_in
43800 74 36 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20  t64 *pRowid);.  
43810 69 6e 74 20 28 2a 78 55 70 64 61 74 65 29 28 73  int (*xUpdate)(s
43820 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69  qlite3_vtab *, i
43830 6e 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  nt, sqlite3_valu
43840 65 20 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e  e **, sqlite3_in
43850 74 36 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  t64 *);.  int (*
43860 78 42 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f  xBegin)(sqlite3_
43870 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20  vtab *pVTab);.  
43880 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c  int (*xSync)(sql
43890 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
438a0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d  );.  int (*xComm
438b0 69 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  it)(sqlite3_vtab
438c0 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20   *pVTab);.  int 
438d0 28 2a 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c  (*xRollback)(sql
438e0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
438f0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64  );.  int (*xFind
43900 46 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65  Function)(sqlite
43910 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69  3_vtab *pVtab, i
43920 6e 74 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63  nt nArg, const c
43930 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20  har *zName,.    
43940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43950 20 20 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e     void (**pxFun
43960 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
43970 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
43980 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20  value**),.      
43990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
439a0 20 76 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a   void **ppArg);.
439b0 20 20 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29    int (*xRename)
439c0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
439d0 56 74 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72  Vtab, const char
439e0 20 2a 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a   *zNew);.};../*.
439f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72  ** CAPI3REF: Vir
43a00 74 75 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78  tual Table Index
43a10 69 6e 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing Information 
43a20 7b 48 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30  {H18100} <S20400
43a30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73  >.** KEYWORDS: s
43a40 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
43a50 6f 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  o.** EXPERIMENTA
43a60 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  L.**.** The sqli
43a70 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
43a80 74 72 75 63 74 75 72 65 20 61 6e 64 20 69 74 73  tructure and its
43a90 20 73 75 62 73 74 72 75 63 74 75 72 65 73 20 69   substructures i
43aa0 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73  s used to.** pas
43ab0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  s information in
43ac0 74 6f 20 61 6e 64 20 72 65 63 65 69 76 65 20 74  to and receive t
43ad0 68 65 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68  he reply from th
43ae0 65 20 78 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20  e xBestIndex.** 
43af0 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 73 71 6c  method of an sql
43b00 69 74 65 33 5f 6d 6f 64 75 6c 65 2e 20 20 54 68  ite3_module.  Th
43b10 65 20 66 69 65 6c 64 73 20 75 6e 64 65 72 20 2a  e fields under *
43b20 2a 49 6e 70 75 74 73 2a 2a 20 61 72 65 20 74 68  *Inputs** are th
43b30 65 0a 2a 2a 20 69 6e 70 75 74 73 20 74 6f 20 78  e.** inputs to x
43b40 42 65 73 74 49 6e 64 65 78 20 61 6e 64 20 61 72  BestIndex and ar
43b50 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42  e read-only.  xB
43b60 65 73 74 49 6e 64 65 78 20 69 6e 73 65 72 74 73  estIndex inserts
43b70 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20   its.** results 
43b80 69 6e 74 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75  into the **Outpu
43b90 74 73 2a 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a  ts** fields..**.
43ba0 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69  ** The aConstrai
43bb0 6e 74 5b 5d 20 61 72 72 61 79 20 72 65 63 6f 72  nt[] array recor
43bc0 64 73 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  ds WHERE clause 
43bd0 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74  constraints of t
43be0 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c  he form:.**.** <
43bf0 70 72 65 3e 63 6f 6c 75 6d 6e 20 4f 50 20 65 78  pre>column OP ex
43c00 70 72 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77  pr</pre>.**.** w
43c10 68 65 72 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c  here OP is =, &l
43c20 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c  t;, &lt;=, &gt;,
43c30 20 6f 72 20 26 67 74 3b 3d 2e 20 20 54 68 65 20   or &gt;=.  The 
43c40 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65 72 61  particular opera
43c50 74 6f 72 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64  tor is.** stored
43c60 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b   in aConstraint[
43c70 5d 2e 6f 70 2e 20 20 54 68 65 20 69 6e 64 65 78  ].op.  The index
43c80 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69   of the column i
43c90 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61  s stored in.** a
43ca0 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f  Constraint[].iCo
43cb0 6c 75 6d 6e 2e 20 20 61 43 6f 6e 73 74 72 61 69  lumn.  aConstrai
43cc0 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20 54  nt[].usable is T
43cd0 52 55 45 20 69 66 20 74 68 65 0a 2a 2a 20 65 78  RUE if the.** ex
43ce0 70 72 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2d  pr on the right-
43cf0 68 61 6e 64 20 73 69 64 65 20 63 61 6e 20 62 65  hand side can be
43d00 20 65 76 61 6c 75 61 74 65 64 20 28 61 6e 64 20   evaluated (and 
43d10 74 68 75 73 20 74 68 65 20 63 6f 6e 73 74 72 61  thus the constra
43d20 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61 62 6c 65  int.** is usable
43d30 29 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 69  ) and false if i
43d40 74 20 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20  t cannot..**.** 
43d50 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 61 75  The optimizer au
43d60 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76 65  tomatically inve
43d70 72 74 73 20 74 65 72 6d 73 20 6f 66 20 74 68 65  rts terms of the
43d80 20 66 6f 72 6d 20 22 65 78 70 72 20 4f 50 20 63   form "expr OP c
43d90 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61  olumn".** and ma
43da0 6b 65 73 20 6f 74 68 65 72 20 73 69 6d 70 6c 69  kes other simpli
43db0 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 74 68 65  fications to the
43dc0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e   WHERE clause in
43dd0 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a   an attempt to.*
43de0 2a 20 67 65 74 20 61 73 20 6d 61 6e 79 20 57 48  * get as many WH
43df0 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73  ERE clause terms
43e00 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 6d 20 73   into the form s
43e10 68 6f 77 6e 20 61 62 6f 76 65 20 61 73 20 70 6f  hown above as po
43e20 73 73 69 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 61  ssible..** The a
43e30 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72  Constraint[] arr
43e40 61 79 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20  ay only reports 
43e50 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
43e60 6d 73 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63  ms in the correc
43e70 74 0a 2a 2a 20 66 6f 72 6d 20 74 68 61 74 20 72  t.** form that r
43e80 65 66 65 72 20 74 6f 20 74 68 65 20 70 61 72 74  efer to the part
43e90 69 63 75 6c 61 72 20 76 69 72 74 75 61 6c 20 74  icular virtual t
43ea0 61 62 6c 65 20 62 65 69 6e 67 20 71 75 65 72 69  able being queri
43eb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d  ed..**.** Inform
43ec0 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
43ed0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
43ee0 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 4f 72  is stored in aOr
43ef0 64 65 72 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68  derBy[]..** Each
43f00 20 74 65 72 6d 20 6f 66 20 61 4f 72 64 65 72 42   term of aOrderB
43f10 79 20 72 65 63 6f 72 64 73 20 61 20 63 6f 6c 75  y records a colu
43f20 6d 6e 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  mn of the ORDER 
43f30 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  BY clause..**.**
43f40 20 54 68 65 20 78 42 65 73 74 49 6e 64 65 78 20   The xBestIndex 
43f50 6d 65 74 68 6f 64 20 6d 75 73 74 20 66 69 6c 6c  method must fill
43f60 20 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67   aConstraintUsag
43f70 65 5b 5d 20 77 69 74 68 20 69 6e 66 6f 72 6d 61  e[] with informa
43f80 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 77 68  tion.** about wh
43f90 61 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  at parameters to
43fa0 20 70 61 73 73 20 74 6f 20 78 46 69 6c 74 65 72   pass to xFilter
43fb0 2e 20 20 49 66 20 61 72 67 76 49 6e 64 65 78 3e  .  If argvIndex>
43fc0 30 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69  0 then.** the ri
43fd0 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  ght-hand side of
43fe0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
43ff0 6e 67 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  ng aConstraint[]
44000 20 69 73 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a   is evaluated.**
44010 20 61 6e 64 20 62 65 63 6f 6d 65 73 20 74 68 65   and becomes the
44020 20 61 72 67 76 49 6e 64 65 78 2d 74 68 20 65 6e   argvIndex-th en
44030 74 72 79 20 69 6e 20 61 72 67 76 2e 20 20 49 66  try in argv.  If
44040 20 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67   aConstraintUsag
44050 65 5b 5d 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74  e[].omit.** is t
44060 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  rue, then the co
44070 6e 73 74 72 61 69 6e 74 20 69 73 20 61 73 73 75  nstraint is assu
44080 6d 65 64 20 74 6f 20 62 65 20 66 75 6c 6c 79 20  med to be fully 
44090 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a  handled by the.*
440a0 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  * virtual table 
440b0 61 6e 64 20 69 73 20 6e 6f 74 20 63 68 65 63 6b  and is not check
440c0 65 64 20 61 67 61 69 6e 20 62 79 20 53 51 4c 69  ed again by SQLi
440d0 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64  te..**.** The id
440e0 78 4e 75 6d 20 61 6e 64 20 69 64 78 50 74 72 20  xNum and idxPtr 
440f0 76 61 6c 75 65 73 20 61 72 65 20 72 65 63 6f 72  values are recor
44100 64 65 64 20 61 6e 64 20 70 61 73 73 65 64 20 69  ded and passed i
44110 6e 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 20  nto xFilter..** 
44120 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69  sqlite3_free() i
44130 73 20 75 73 65 64 20 74 6f 20 66 72 65 65 20 69  s used to free i
44140 64 78 50 74 72 20 69 66 20 6e 65 65 64 54 6f 46  dxPtr if needToF
44150 72 65 65 49 64 78 50 74 72 20 69 73 20 74 72 75  reeIdxPtr is tru
44160 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 64  e..**.** The ord
44170 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 6d 65 61  erByConsumed mea
44180 6e 73 20 74 68 61 74 20 6f 75 74 70 75 74 20 66  ns that output f
44190 72 6f 6d 20 78 46 69 6c 74 65 72 20 77 69 6c 6c  rom xFilter will
441a0 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65   occur in.** the
441b0 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 74   correct order t
441c0 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52  o satisfy the OR
441d0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73 6f  DER BY clause so
441e0 20 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61 74   that no separat
441f0 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74 65  e.** sorting ste
44200 70 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a  p is required..*
44210 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74  *.** The estimat
44220 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73 20  edCost value is 
44230 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74  an estimate of t
44240 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67  he cost of doing
44250 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c   the.** particul
44260 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75  ar lookup.  A fu
44270 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61 62  ll scan of a tab
44280 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69 65  le with N entrie
44290 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a  s should have.**
442a0 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41   a cost of N.  A
442b0 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20 6f   binary search o
442c0 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20 65  f a table of N e
442d0 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61  ntries should ha
442e0 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20  ve a.** cost of 
442f0 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f  approximately lo
44300 67 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  g(N)..**.** This
44310 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78   interface is ex
44320 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69  perimental and i
44330 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  s subject to cha
44340 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61  nge or.** remova
44350 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  l in future rele
44360 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
44370 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
44380 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 7b 0a 20  3_index_info {. 
44390 20 2f 2a 20 49 6e 70 75 74 73 20 2a 2f 0a 20 20   /* Inputs */.  
443a0 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  int nConstraint;
443b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
443c0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
443d0 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a  in aConstraint *
443e0 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74  /.  struct sqlit
443f0 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
44400 69 6e 74 20 7b 0a 20 20 20 20 20 69 6e 74 20 69  int {.     int i
44410 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
44420 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f       /* Column o
44430 6e 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  n left-hand side
44440 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a   of constraint *
44450 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  /.     unsigned 
44460 63 68 61 72 20 6f 70 3b 20 20 20 20 20 20 20 20  char op;        
44470 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 6f   /* Constraint o
44480 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20  perator */.     
44490 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 73  unsigned char us
444a0 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 72 75  able;     /* Tru
444b0 65 20 69 66 20 74 68 69 73 20 63 6f 6e 73 74 72  e if this constr
444c0 61 69 6e 74 20 69 73 20 75 73 61 62 6c 65 20 2a  aint is usable *
444d0 2f 0a 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d  /.     int iTerm
444e0 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
444f0 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61   /* Used interna
44500 6c 6c 79 20 2d 20 78 42 65 73 74 49 6e 64 65 78  lly - xBestIndex
44510 20 73 68 6f 75 6c 64 20 69 67 6e 6f 72 65 20 2a   should ignore *
44520 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69  /.  } *aConstrai
44530 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nt;            /
44540 2a 20 54 61 62 6c 65 20 6f 66 20 57 48 45 52 45  * Table of WHERE
44550 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69   clause constrai
44560 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72  nts */.  int nOr
44570 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 20  derBy;          
44580 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
44590 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52   terms in the OR
445a0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
445b0 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
445c0 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20  3_index_orderby 
445d0 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75  {.     int iColu
445e0 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
445f0 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65   /* Column numbe
44600 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e  r */.     unsign
44610 65 64 20 63 68 61 72 20 64 65 73 63 3b 20 20 20  ed char desc;   
44620 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
44630 44 45 53 43 2e 20 20 46 61 6c 73 65 20 66 6f 72  DESC.  False for
44640 20 41 53 43 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f   ASC. */.  } *aO
44650 72 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 20  rderBy;         
44660 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44        /* The ORD
44670 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
44680 20 20 2f 2a 20 4f 75 74 70 75 74 73 20 2a 2f 0a    /* Outputs */.
44690 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
446a0 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
446b0 74 5f 75 73 61 67 65 20 7b 0a 20 20 20 20 69 6e  t_usage {.    in
446c0 74 20 61 72 67 76 49 6e 64 65 78 3b 20 20 20 20  t argvIndex;    
446d0 20 20 20 20 20 20 20 2f 2a 20 69 66 20 3e 30 2c         /* if >0,
446e0 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 70   constraint is p
446f0 61 72 74 20 6f 66 20 61 72 67 76 20 74 6f 20 78  art of argv to x
44700 46 69 6c 74 65 72 20 2a 2f 0a 20 20 20 20 75 6e  Filter */.    un
44710 73 69 67 6e 65 64 20 63 68 61 72 20 6f 6d 69 74  signed char omit
44720 3b 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74  ;      /* Do not
44730 20 63 6f 64 65 20 61 20 74 65 73 74 20 66 6f 72   code a test for
44740 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   this constraint
44750 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72   */.  } *aConstr
44760 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 69 6e 74  aintUsage;.  int
44770 20 69 64 78 4e 75 6d 3b 20 20 20 20 20 20 20 20   idxNum;        
44780 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
44790 72 20 75 73 65 64 20 74 6f 20 69 64 65 6e 74 69  r used to identi
447a0 66 79 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a  fy the index */.
447b0 20 20 63 68 61 72 20 2a 69 64 78 53 74 72 3b 20    char *idxStr; 
447c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
447d0 53 74 72 69 6e 67 2c 20 70 6f 73 73 69 62 6c 79  String, possibly
447e0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
447f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f  qlite3_malloc */
44800 0a 20 20 69 6e 74 20 6e 65 65 64 54 6f 46 72 65  .  int needToFre
44810 65 49 64 78 53 74 72 3b 20 20 20 20 20 20 2f 2a  eIdxStr;      /*
44820 20 46 72 65 65 20 69 64 78 53 74 72 20 75 73 69   Free idxStr usi
44830 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ng sqlite3_free(
44840 29 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 69  ) if true */.  i
44850 6e 74 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  nt orderByConsum
44860 65 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ed;       /* Tru
44870 65 20 69 66 20 6f 75 74 70 75 74 20 69 73 20 61  e if output is a
44880 6c 72 65 61 64 79 20 6f 72 64 65 72 65 64 20 2a  lready ordered *
44890 2f 0a 20 20 64 6f 75 62 6c 65 20 65 73 74 69 6d  /.  double estim
448a0 61 74 65 64 43 6f 73 74 3b 20 20 20 20 20 20 2f  atedCost;      /
448b0 2a 20 45 73 74 69 6d 61 74 65 64 20 63 6f 73 74  * Estimated cost
448c0 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20 69   of using this i
448d0 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69  ndex */.};.#defi
448e0 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  ne SQLITE_INDEX_
448f0 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 20 20  CONSTRAINT_EQ   
44900 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   2.#define SQLIT
44910 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
44920 4e 54 5f 47 54 20 20 20 20 34 0a 23 64 65 66 69  NT_GT    4.#defi
44930 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  ne SQLITE_INDEX_
44940 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 20 20  CONSTRAINT_LE   
44950 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   8.#define SQLIT
44960 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
44970 4e 54 5f 4c 54 20 20 20 20 31 36 0a 23 64 65 66  NT_LT    16.#def
44980 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ine SQLITE_INDEX
44990 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 20  _CONSTRAINT_GE  
449a0 20 20 33 32 0a 23 64 65 66 69 6e 65 20 53 51 4c    32.#define SQL
449b0 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
449c0 41 49 4e 54 5f 4d 41 54 43 48 20 36 34 0a 0a 2f  AINT_MATCH 64../
449d0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  *.** CAPI3REF: R
449e0 65 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61  egister A Virtua
449f0 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e  l Table Implemen
44a00 74 61 74 69 6f 6e 20 7b 48 31 38 32 30 30 7d 20  tation {H18200} 
44a10 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45  <S20400>.** EXPE
44a20 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
44a30 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
44a40 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20  sed to register 
44a50 61 20 6e 65 77 20 6d 6f 64 75 6c 65 20 6e 61 6d  a new module nam
44a60 65 20 77 69 74 68 20 61 0a 2a 2a 20 5b 64 61 74  e with a.** [dat
44a70 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
44a80 5d 2e 20 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73  ].  Module names
44a90 20 6d 75 73 74 20 62 65 20 72 65 67 69 73 74 65   must be registe
44aa0 72 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72  red before.** cr
44ab0 65 61 74 69 6e 67 20 6e 65 77 20 76 69 72 74 75  eating new virtu
44ac0 61 6c 20 74 61 62 6c 65 73 20 6f 6e 20 74 68 65  al tables on the
44ad0 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f   module, or befo
44ae0 72 65 20 75 73 69 6e 67 0a 2a 2a 20 70 72 65 65  re using.** pree
44af0 78 69 73 74 69 6e 67 20 76 69 72 74 75 61 6c 20  xisting virtual 
44b00 74 61 62 6c 65 73 20 6f 66 20 74 68 65 20 6d 6f  tables of the mo
44b10 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  dule..**.** This
44b20 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78   interface is ex
44b30 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69  perimental and i
44b40 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  s subject to cha
44b50 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61  nge or.** remova
44b60 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  l in future rele
44b70 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
44b80 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
44b90 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
44ba0 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  L int sqlite3_cr
44bb0 65 61 74 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73  eate_module(.  s
44bc0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
44bd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c            /* SQL
44be0 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ite connection t
44bf0 6f 20 72 65 67 69 73 74 65 72 20 6d 6f 64 75 6c  o register modul
44c00 65 20 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73  e with */.  cons
44c10 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
44c20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
44c30 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  f the module */.
44c40 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
44c50 6d 6f 64 75 6c 65 20 2a 2c 20 20 20 20 2f 2a 20  module *,    /* 
44c60 4d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20  Methods for the 
44c70 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64  module */.  void
44c80 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
44c90 20 20 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74         /* Client
44ca0 20 64 61 74 61 20 66 6f 72 20 78 43 72 65 61 74   data for xCreat
44cb0 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b  e/xConnect */.);
44cc0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
44cd0 3a 20 52 65 67 69 73 74 65 72 20 41 20 56 69 72  : Register A Vir
44ce0 74 75 61 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65  tual Table Imple
44cf0 6d 65 6e 74 61 74 69 6f 6e 20 7b 48 31 38 32 31  mentation {H1821
44d00 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45  0} <S20400>.** E
44d10 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
44d20 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
44d30 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 74  s identical to t
44d40 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  he [sqlite3_crea
44d50 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6d 65 74  te_module()] met
44d60 68 6f 64 20 61 62 6f 76 65 2c 0a 2a 2a 20 65 78  hod above,.** ex
44d70 63 65 70 74 20 74 68 61 74 20 69 74 20 61 6c 6c  cept that it all
44d80 6f 77 73 20 61 20 64 65 73 74 72 75 63 74 6f 72  ows a destructor
44d90 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
44da0 73 70 65 63 69 66 69 65 64 2e 20 49 74 20 69 73  specified. It is
44db0 0a 2a 2a 20 65 76 65 6e 20 6d 6f 72 65 20 65 78  .** even more ex
44dc0 70 65 72 69 6d 65 6e 74 61 6c 20 74 68 61 6e 20  perimental than 
44dd0 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20  the rest of the 
44de0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 41  virtual tables A
44df0 50 49 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  PI..*/.SQLITE_AP
44e00 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
44e10 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
44e20 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
44e30 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
44e40 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
44e50 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65   /* SQLite conne
44e60 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65  ction to registe
44e70 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f  r module with */
44e80 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
44e90 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  Name,         /*
44ea0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64   Name of the mod
44eb0 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
44ec0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c  qlite3_module *,
44ed0 20 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66      /* Methods f
44ee0 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  or the module */
44ef0 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20  .  void *,      
44f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
44f10 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72   Client data for
44f20 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
44f30 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65  t */.  void(*xDe
44f40 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 20 20 20  stroy)(void*)   
44f50 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74    /* Module dest
44f60 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
44f70 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.);../*.** CAP
44f80 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54  I3REF: Virtual T
44f90 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 20 4f 62  able Instance Ob
44fa0 6a 65 63 74 20 7b 48 31 38 30 31 30 7d 20 3c 53  ject {H18010} <S
44fb0 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  20400>.** KEYWOR
44fc0 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62  DS: sqlite3_vtab
44fd0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
44fe0 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 6d 6f 64  .**.** Every mod
44ff0 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
45000 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c 61  on uses a subcla
45010 73 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ss of the follow
45020 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  ing structure.**
45030 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70   to describe a p
45040 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e  articular instan
45050 63 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  ce of the module
45060 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73  .  Each subclass
45070 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c   will.** be tail
45080 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63  ored to the spec
45090 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68  ific needs of th
450a0 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  e module impleme
450b0 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20  ntation..** The 
450c0 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20  purpose of this 
450d0 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f  superclass is to
450e0 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20   define certain 
450f0 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a  fields that are.
45100 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  ** common to all
45110 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
45120 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56  tations..**.** V
45130 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65  irtual tables me
45140 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e  thods can set an
45150 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62   error message b
45160 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a  y assigning a.**
45170 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64   string obtained
45180 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
45190 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72  printf()] to zEr
451a0 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f  rMsg.  The metho
451b0 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65  d should.** take
451c0 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70   care that any p
451d0 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66  rior string is f
451e0 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  reed by a call t
451f0 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  o [sqlite3_free(
45200 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61  )].** prior to a
45210 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73  ssigning a new s
45220 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67  tring to zErrMsg
45230 2e 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72  .  After the err
45240 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73  or message.** is
45250 20 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f   delivered up to
45260 20 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c   the client appl
45270 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72  ication, the str
45280 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f  ing will be auto
45290 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65  matically.** fre
452a0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72  ed by sqlite3_fr
452b0 65 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72  ee() and the zEr
452c0 72 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20  rMsg field will 
452d0 62 65 20 7a 65 72 6f 65 64 2e 20 20 4e 6f 74 65  be zeroed.  Note
452e0 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33  .** that sqlite3
452f0 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73  _mprintf() and s
45300 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 61 72  qlite3_free() ar
45310 65 20 75 73 65 64 20 6f 6e 20 74 68 65 20 7a 45  e used on the zE
45320 72 72 4d 73 67 20 66 69 65 6c 64 0a 2a 2a 20 73  rrMsg field.** s
45330 69 6e 63 65 20 76 69 72 74 75 61 6c 20 74 61 62  ince virtual tab
45340 6c 65 73 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79  les are commonly
45350 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20   implemented in 
45360 6c 6f 61 64 61 62 6c 65 20 65 78 74 65 6e 73 69  loadable extensi
45370 6f 6e 73 20 77 68 69 63 68 0a 2a 2a 20 64 6f 20  ons which.** do 
45380 6e 6f 74 20 68 61 76 65 20 61 63 63 65 73 73 20  not have access 
45390 74 6f 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74  to sqlite3MPrint
453a0 66 28 29 20 6f 72 20 73 71 6c 69 74 65 33 46 72  f() or sqlite3Fr
453b0 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ee()..**.** This
453c0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78   interface is ex
453d0 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69  perimental and i
453e0 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  s subject to cha
453f0 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61  nge or.** remova
45400 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  l in future rele
45410 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
45420 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
45430 33 5f 76 74 61 62 20 7b 0a 20 20 63 6f 6e 73 74  3_vtab {.  const
45440 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
45450 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68  *pModule;  /* Th
45460 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 69  e module for thi
45470 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  s virtual table 
45480 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  */.  int nRef;  
45490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
454a0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 69 6e 74       /* Used int
454b0 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 63 68 61  ernally */.  cha
454c0 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
454d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
454e0 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72  Error message fr
454f0 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  om sqlite3_mprin
45500 74 66 28 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72  tf() */.  /* Vir
45510 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65  tual table imple
45520 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20  mentations will 
45530 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64  typically add ad
45540 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20  ditional fields 
45550 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.};../*.** CAP
45560 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54  I3REF: Virtual T
45570 61 62 6c 65 20 43 75 72 73 6f 72 20 4f 62 6a 65  able Cursor Obje
45580 63 74 20 20 7b 48 31 38 30 32 30 7d 20 3c 53 32  ct  {H18020} <S2
45590 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0400>.** KEYWORD
455a0 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  S: sqlite3_vtab_
455b0 63 75 72 73 6f 72 0a 2a 2a 20 45 58 50 45 52 49  cursor.** EXPERI
455c0 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65  MENTAL.**.** Eve
455d0 72 79 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d  ry module implem
455e0 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20  entation uses a 
455f0 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 20  subclass of the 
45600 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
45610 75 72 65 0a 2a 2a 20 74 6f 20 64 65 73 63 72 69  ure.** to descri
45620 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  be cursors that 
45630 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 76  point into the v
45640 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64  irtual table and
45650 20 61 72 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20   are used.** to 
45660 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65  loop through the
45670 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
45680 20 43 75 72 73 6f 72 73 20 61 72 65 20 63 72 65   Cursors are cre
45690 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a  ated using the.*
456a0 2a 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f  * xOpen method o
456b0 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45  f the module.  E
456c0 61 63 68 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  ach module imple
456d0 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64  mentation will d
456e0 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e  efine.** the con
456f0 74 65 6e 74 20 6f 66 20 61 20 63 75 72 73 6f 72  tent of a cursor
45700 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 73 75   structure to su
45710 69 74 20 69 74 73 20 6f 77 6e 20 6e 65 65 64 73  it its own needs
45720 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70  ..**.** This sup
45730 65 72 63 6c 61 73 73 20 65 78 69 73 74 73 20 69  erclass exists i
45740 6e 20 6f 72 64 65 72 20 74 6f 20 64 65 66 69 6e  n order to defin
45750 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  e fields of the 
45760 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61  cursor that.** a
45770 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  re common to all
45780 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
45790 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  ..**.** This int
457a0 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69  erface is experi
457b0 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75  mental and is su
457c0 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
457d0 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e  or.** removal in
457e0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
457f0 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73   of SQLite..*/.s
45800 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74  truct sqlite3_vt
45810 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71  ab_cursor {.  sq
45820 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
45830 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75  b;      /* Virtu
45840 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73  al table of this
45850 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20   cursor */.  /* 
45860 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d  Virtual table im
45870 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69  plementations wi
45880 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64  ll typically add
45890 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c   additional fiel
458a0 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ds */.};../*.** 
458b0 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72  CAPI3REF: Declar
458c0 65 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20  e The Schema Of 
458d0 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20  A Virtual Table 
458e0 7b 48 31 38 32 38 30 7d 20 3c 53 32 30 34 30 30  {H18280} <S20400
458f0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
45900 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65  L.**.** The xCre
45910 61 74 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74  ate and xConnect
45920 20 6d 65 74 68 6f 64 73 20 6f 66 20 61 20 6d 6f   methods of a mo
45930 64 75 6c 65 20 75 73 65 20 74 68 65 20 66 6f 6c  dule use the fol
45940 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 74 6f  lowing API.** to
45950 20 64 65 63 6c 61 72 65 20 74 68 65 20 66 6f 72   declare the for
45960 6d 61 74 20 28 74 68 65 20 6e 61 6d 65 73 20 61  mat (the names a
45970 6e 64 20 64 61 74 61 74 79 70 65 73 20 6f 66 20  nd datatypes of 
45980 74 68 65 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a  the columns) of.
45990 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ** the virtual t
459a0 61 62 6c 65 73 20 74 68 65 79 20 69 6d 70 6c 65  ables they imple
459b0 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ment..**.** This
459c0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78   interface is ex
459d0 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69  perimental and i
459e0 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  s subject to cha
459f0 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61  nge or.** remova
45a00 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  l in future rele
45a10 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
45a20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
45a30 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
45a40 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65  L int sqlite3_de
45a50 63 6c 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74  clare_vtab(sqlit
45a60 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  e3*, const char 
45a70 2a 7a 43 72 65 61 74 65 54 61 62 6c 65 29 3b 0a  *zCreateTable);.
45a80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
45a90 20 4f 76 65 72 6c 6f 61 64 20 41 20 46 75 6e 63   Overload A Func
45aa0 74 69 6f 6e 20 46 6f 72 20 41 20 56 69 72 74 75  tion For A Virtu
45ab0 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 33 30 30  al Table {H18300
45ac0 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58  } <S20400>.** EX
45ad0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
45ae0 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   Virtual tables 
45af0 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c 74 65  can provide alte
45b00 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e  rnative implemen
45b10 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e 63 74  tations of funct
45b20 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20 74 68  ions.** using th
45b30 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20  e xFindFunction 
45b40 6d 65 74 68 6f 64 2e 20 20 42 75 74 20 67 6c 6f  method.  But glo
45b50 62 61 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  bal versions of 
45b60 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a  those functions.
45b70 2a 2a 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e  ** must exist in
45b80 20 6f 72 64 65 72 20 74 6f 20 62 65 20 6f 76 65   order to be ove
45b90 72 6c 6f 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  rloaded..**.** T
45ba0 68 69 73 20 41 50 49 20 6d 61 6b 65 73 20 73 75  his API makes su
45bb0 72 65 20 61 20 67 6c 6f 62 61 6c 20 76 65 72 73  re a global vers
45bc0 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f  ion of a functio
45bd0 6e 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  n with a particu
45be0 6c 61 72 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20  lar.** name and 
45bf0 6e 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65  number of parame
45c00 74 65 72 73 20 65 78 69 73 74 73 2e 20 20 49 66  ters exists.  If
45c10 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f   no such functio
45c20 6e 20 65 78 69 73 74 73 0a 2a 2a 20 62 65 66 6f  n exists.** befo
45c30 72 65 20 74 68 69 73 20 41 50 49 20 69 73 20 63  re this API is c
45c40 61 6c 6c 65 64 2c 20 61 20 6e 65 77 20 66 75 6e  alled, a new fun
45c50 63 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64  ction is created
45c60 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  .  The implement
45c70 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  ation.** of the 
45c80 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77  new function alw
45c90 61 79 73 20 63 61 75 73 65 73 20 61 6e 20 65 78  ays causes an ex
45ca0 63 65 70 74 69 6f 6e 20 74 6f 20 62 65 20 74 68  ception to be th
45cb0 72 6f 77 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65  rown.  So.** the
45cc0 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73   new function is
45cd0 20 6e 6f 74 20 67 6f 6f 64 20 66 6f 72 20 61 6e   not good for an
45ce0 79 74 68 69 6e 67 20 62 79 20 69 74 73 65 6c 66  ything by itself
45cf0 2e 20 20 49 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70  .  Its only.** p
45d00 75 72 70 6f 73 65 20 69 73 20 74 6f 20 62 65 20  urpose is to be 
45d10 61 20 70 6c 61 63 65 68 6f 6c 64 65 72 20 66 75  a placeholder fu
45d20 6e 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6e 20  nction that can 
45d30 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a 2a 2a  be overloaded.**
45d40 20 62 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c   by virtual tabl
45d50 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41  es..**.** This A
45d60 50 49 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e  PI should be con
45d70 73 69 64 65 72 65 64 20 70 61 72 74 20 6f 66 20  sidered part of 
45d80 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
45d90 65 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20  e interface,.** 
45da0 77 68 69 63 68 20 69 73 20 65 78 70 65 72 69 6d  which is experim
45db0 65 6e 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63  ental and subjec
45dc0 74 20 74 6f 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a  t to change..*/.
45dd0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
45de0 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69  E_EXPERIMENTAL i
45df0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  nt sqlite3_overl
45e00 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c  oad_function(sql
45e10 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61  ite3*, const cha
45e20 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e  r *zFuncName, in
45e30 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t nArg);../*.** 
45e40 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  The interface to
45e50 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62   the virtual-tab
45e60 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64 65 66  le mechanism def
45e70 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61 63 6b  ined above (back
45e80 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d   up.** to a comm
45e90 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79 20 73  ent remarkably s
45ea0 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f  imilar to this o
45eb0 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ne) is currently
45ec0 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74   considered.** t
45ed0 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61  o be experimenta
45ee0 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63  l.  The interfac
45ef0 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69  e might change i
45f00 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77  n incompatible w
45f10 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20  ays..** If this 
45f20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72  is a problem for
45f30 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65   you, do not use
45f40 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 61   the interface a
45f50 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a  t this time..**.
45f60 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 72 74  ** When the virt
45f70 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e  ual-table mechan
45f80 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 2c 20  ism stabilizes, 
45f90 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 65 20  we will declare 
45fa0 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  the.** interface
45fb0 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 74 20   fixed, support 
45fc0 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c  it indefinitely,
45fd0 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 69 73   and remove this
45fe0 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a   comment..**.***
45ff0 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  *** EXPERIMENTAL
46000 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68   - subject to ch
46010 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74  ange without not
46020 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ice ************
46030 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  **.*/../*.** CAP
46040 49 33 52 45 46 3a 20 41 20 48 61 6e 64 6c 65 20  I3REF: A Handle 
46050 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20  To An Open BLOB 
46060 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 32 33 30  {H17800} <S30230
46070 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
46080 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c  BLOB handle} {BL
46090 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a  OB handles}.**.*
460a0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
460b0 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70   this object rep
460c0 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20  resents an open 
460d0 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a  BLOB on which.**
460e0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
460f0 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61  pen | incrementa
46100 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20  l BLOB I/O] can 
46110 62 65 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a  be performed..**
46120 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73   Objects of this
46130 20 74 79 70 65 20 61 72 65 20 63 72 65 61 74 65   type are create
46140 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c  d by [sqlite3_bl
46150 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e  ob_open()].** an
46160 64 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b  d destroyed by [
46170 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
46180 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73  se()]..** The [s
46190 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64  qlite3_blob_read
461a0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
461b0 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69  _blob_write()] i
461c0 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e  nterfaces.** can
461d0 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 64   be used to read
461e0 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20   or write small 
461f0 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74  subsections of t
46200 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20  he BLOB..** The 
46210 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79  [sqlite3_blob_by
46220 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tes()] interface
46230 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a   returns the siz
46240 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 69 6e  e of the BLOB in
46250 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64   bytes..*/.typed
46260 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
46270 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62  3_blob sqlite3_b
46280 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lob;../*.** CAPI
46290 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f  3REF: Open A BLO
462a0 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61  B For Incrementa
462b0 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c  l I/O {H17810} <
462c0 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30230>.**.** Th
462d0 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 6f 70  is interfaces op
462e0 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64  ens a [BLOB hand
462f0 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20  le | handle] to 
46300 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64  the BLOB located
46310 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c  .** in row iRow,
46320 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c   column zColumn,
46330 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e   table zTable in
46340 20 64 61 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a   database zDb;.*
46350 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * in other words
46360 2c 20 74 68 65 20 73 61 6d 65 20 42 4c 4f 42 20  , the same BLOB 
46370 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 73 65  that would be se
46380 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a  lected by:.**.**
46390 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45   <pre>.**     SE
463a0 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f  LECT zColumn FRO
463b0 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45  M zDb.zTable WHE
463c0 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 69 52 6f  RE [rowid] = iRo
463d0 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b 45 4e  w;.** </pre> {EN
463e0 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  D}.**.** If the 
463f0 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
46400 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
46410 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65   the BLOB is ope
46420 6e 65 64 20 66 6f 72 20 72 65 61 64 0a 2a 2a 20  ned for read.** 
46430 61 6e 64 20 77 72 69 74 65 20 61 63 63 65 73 73  and write access
46440 2e 20 49 66 20 69 74 20 69 73 20 7a 65 72 6f 2c  . If it is zero,
46450 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65   the BLOB is ope
46460 6e 65 64 20 66 6f 72 20 72 65 61 64 20 61 63 63  ned for read acc
46470 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ess..**.** Note 
46480 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73  that the databas
46490 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74 68  e name is not th
464a0 65 20 66 69 6c 65 6e 61 6d 65 20 74 68 61 74 20  e filename that 
464b0 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20  contains.** the 
464c0 64 61 74 61 62 61 73 65 20 62 75 74 20 72 61 74  database but rat
464d0 68 65 72 20 74 68 65 20 73 79 6d 62 6f 6c 69 63  her the symbolic
464e0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
464f0 61 62 61 73 65 20 74 68 61 74 0a 2a 2a 20 69 73  abase that.** is
46500 20 61 73 73 69 67 6e 65 64 20 77 68 65 6e 20 74   assigned when t
46510 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63  he database is c
46520 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e 67 20 5b  onnected using [
46530 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f 72 20  ATTACH]..** For 
46540 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
46550 65 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61  e file, the data
46560 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 6d 61  base name is "ma
46570 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 4d 50  in"..** For TEMP
46580 20 74 61 62 6c 65 73 2c 20 74 68 65 20 64 61 74   tables, the dat
46590 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 74  abase name is "t
465a0 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73  emp"..**.** On s
465b0 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f  uccess, [SQLITE_
465c0 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  OK] is returned 
465d0 61 6e 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f  and the new [BLO
465e0 42 20 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69  B handle] is wri
465f0 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c  tten.** to *ppBl
46600 6f 62 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e  ob. Otherwise an
46610 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
46620 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e   returned and an
46630 79 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 0a  y value written.
46640 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 20 73 68  ** to *ppBlob sh
46650 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64  ould not be used
46660 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   by the caller..
46670 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
46680 20 73 65 74 73 20 74 68 65 20 5b 64 61 74 61 62   sets the [datab
46690 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
466a0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d  error code and m
466b0 65 73 73 61 67 65 0a 2a 2a 20 61 63 63 65 73 73  essage.** access
466c0 69 62 6c 65 20 76 69 61 20 5b 73 71 6c 69 74 65  ible via [sqlite
466d0 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 61 6e 64  3_errcode()] and
466e0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
466f0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ()]..**.** If th
46700 65 20 72 6f 77 20 74 68 61 74 20 61 20 42 4c 4f  e row that a BLO
46710 42 20 68 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20  B handle points 
46720 74 6f 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62  to is modified b
46730 79 20 61 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d  y an.** [UPDATE]
46740 2c 20 5b 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62  , [DELETE], or b
46750 79 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20  y [ON CONFLICT] 
46760 73 69 64 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20  side-effects.** 
46770 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 68 61  then the BLOB ha
46780 6e 64 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 61  ndle is marked a
46790 73 20 22 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20  s "expired"..** 
467a0 54 68 69 73 20 69 73 20 74 72 75 65 20 69 66 20  This is true if 
467b0 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  any column of th
467c0 65 20 72 6f 77 20 69 73 20 63 68 61 6e 67 65 64  e row is changed
467d0 2c 20 65 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a  , even a column.
467e0 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68  ** other than th
467f0 65 20 6f 6e 65 20 74 68 65 20 42 4c 4f 42 20 68  e one the BLOB h
46800 61 6e 64 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e  andle is open on
46810 2e 0a 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73  ..** Calls to [s
46820 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64  qlite3_blob_read
46830 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
46840 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66  _blob_write()] f
46850 6f 72 0a 2a 2a 20 61 20 65 78 70 69 72 65 64 20  or.** a expired 
46860 42 4c 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c  BLOB handle fail
46870 20 77 69 74 68 20 61 6e 20 72 65 74 75 72 6e 20   with an return 
46880 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f  code of [SQLITE_
46890 41 42 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67  ABORT]..** Chang
468a0 65 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  es written into 
468b0 61 20 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20  a BLOB prior to 
468c0 74 68 65 20 42 4c 4f 42 20 65 78 70 69 72 69 6e  the BLOB expirin
468d0 67 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c  g are not.** rol
468e0 6c 62 61 63 6b 20 62 79 20 74 68 65 20 65 78 70  lback by the exp
468f0 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 42  iration of the B
46900 4c 4f 42 2e 20 20 53 75 63 68 20 63 68 61 6e 67  LOB.  Such chang
46910 65 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c  es will eventual
46920 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20  ly.** commit if 
46930 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
46940 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d  continues to com
46950 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  pletion..**.** I
46960 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
46970 20 7b 48 31 37 38 31 33 7d 20 41 20 73 75 63 63   {H17813} A succ
46980 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f  essful invocatio
46990 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  n of the [sqlite
469a0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 42 2c  3_blob_open(D,B,
469b0 54 2c 43 2c 52 2c 46 2c 50 29 5d 0a 2a 2a 20 20  T,C,R,F,P)].**  
469c0 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61 63          interfac
469d0 65 20 73 68 61 6c 6c 20 6f 70 65 6e 20 61 6e 20  e shall open an 
469e0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f  [sqlite3_blob] o
469f0 62 6a 65 63 74 20 50 20 6f 6e 20 74 68 65 20 42  bject P on the B
46a00 4c 4f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  LOB.**          
46a10 69 6e 20 63 6f 6c 75 6d 6e 20 43 20 6f 66 20 74  in column C of t
46a20 68 65 20 74 61 62 6c 65 20 54 20 69 6e 20 74 68  he table T in th
46a30 65 20 64 61 74 61 62 61 73 65 20 42 20 6f 6e 0a  e database B on.
46a40 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
46a50 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
46a60 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b  tion] D..**.** {
46a70 48 31 37 38 31 34 7d 20 41 20 73 75 63 63 65 73  H17814} A succes
46a80 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20  sful invocation 
46a90 6f 66 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  of [sqlite3_blob
46aa0 5f 6f 70 65 6e 28 44 2c 2e 2e 2e 29 5d 20 73 68  _open(D,...)] sh
46ab0 61 6c 6c 20 73 74 61 72 74 0a 2a 2a 20 20 20 20  all start.**    
46ac0 20 20 20 20 20 20 61 20 6e 65 77 20 74 72 61 6e        a new tran
46ad0 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 5b  saction on the [
46ae0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
46af0 69 6f 6e 5d 20 44 20 69 66 20 74 68 61 74 0a 2a  ion] D if that.*
46b00 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 6e 65  *          conne
46b10 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6c 72  ction is not alr
46b20 65 61 64 79 20 69 6e 20 61 20 74 72 61 6e 73 61  eady in a transa
46b30 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ction..**.** {H1
46b40 37 38 31 36 7d 20 54 68 65 20 5b 73 71 6c 69 74  7816} The [sqlit
46b50 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 42  e3_blob_open(D,B
46b60 2c 54 2c 43 2c 52 2c 46 2c 50 29 5d 20 69 6e 74  ,T,C,R,F,P)] int
46b70 65 72 66 61 63 65 20 73 68 61 6c 6c 20 6f 70 65  erface shall ope
46b80 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  n.**          th
46b90 65 20 42 4c 4f 42 20 66 6f 72 20 72 65 61 64 20  e BLOB for read 
46ba0 61 6e 64 20 77 72 69 74 65 20 61 63 63 65 73 73  and write access
46bb0 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20   if and only if 
46bc0 74 68 65 20 46 0a 2a 2a 20 20 20 20 20 20 20 20  the F.**        
46bd0 20 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e    parameter is n
46be0 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b  on-zero..**.** {
46bf0 48 31 37 38 31 39 7d 20 54 68 65 20 5b 73 71 6c  H17819} The [sql
46c00 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29  ite3_blob_open()
46c10 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c  ] interface shal
46c20 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45  l return [SQLITE
46c30 5f 4f 4b 5d 20 6f 6e 0a 2a 2a 20 20 20 20 20 20  _OK] on.**      
46c40 20 20 20 20 73 75 63 63 65 73 73 20 61 6e 64 20      success and 
46c50 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b  an appropriate [
46c60 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66  error code] on f
46c70 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ailure..**.** {H
46c80 31 37 38 32 31 7d 20 49 66 20 61 6e 20 65 72 72  17821} If an err
46c90 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
46ca0 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b   evaluation of [
46cb0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
46cc0 6e 28 44 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20  n(D,...)].**    
46cd0 20 20 20 20 20 20 74 68 65 6e 20 73 75 62 73 65        then subse
46ce0 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b  quent calls to [
46cf0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
46d00 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  D)],.**         
46d10 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64   [sqlite3_extend
46d20 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 0a  ed_errcode()], .
46d30 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
46d40 69 74 65 33 5f 65 72 72 6d 73 67 28 44 29 5d 2c  ite3_errmsg(D)],
46d50 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72   and [sqlite3_er
46d60 72 6d 73 67 31 36 28 44 29 5d 20 73 68 61 6c 6c  rmsg16(D)] shall
46d70 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20   return.**      
46d80 20 20 20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20      information 
46d90 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
46da0 74 68 61 74 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a  that error..**.*
46db0 2a 20 7b 48 31 37 38 32 34 7d 20 49 66 20 61 6e  * {H17824} If an
46dc0 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  y column in the 
46dd0 72 6f 77 20 74 68 61 74 20 61 20 5b 73 71 6c 69  row that a [sqli
46de0 74 65 33 5f 62 6c 6f 62 5d 20 68 61 73 20 6f 70  te3_blob] has op
46df0 65 6e 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  en is.**        
46e00 20 20 63 68 61 6e 67 65 64 20 62 79 20 61 20 73    changed by a s
46e10 65 70 61 72 61 74 65 20 5b 55 50 44 41 54 45 5d  eparate [UPDATE]
46e20 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61   or [DELETE] sta
46e30 74 65 6d 65 6e 74 20 6f 72 20 62 79 0a 2a 2a 20  tement or by.** 
46e40 20 20 20 20 20 20 20 20 20 61 6e 20 5b 4f 4e 20           an [ON 
46e50 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 20 65  CONFLICT] side e
46e60 66 66 65 63 74 2c 20 74 68 65 6e 20 74 68 65 20  ffect, then the 
46e70 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 73  [sqlite3_blob] s
46e80 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  hall.**         
46e90 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 69 6e   be marked as in
46ea0 76 61 6c 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  valid..*/.SQLITE
46eb0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
46ec0 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71  _blob_open(.  sq
46ed0 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20  lite3*,.  const 
46ee0 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e  char *zDb,.  con
46ef0 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c  st char *zTable,
46f00 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
46f10 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65  Column,.  sqlite
46f20 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20  3_int64 iRow,.  
46f30 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c  int flags,.  sql
46f40 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c  ite3_blob **ppBl
46f50 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ob.);../*.** CAP
46f60 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20 42  I3REF: Close A B
46f70 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37 38  LOB Handle {H178
46f80 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a  30} <S30230>.**.
46f90 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65  ** Closes an ope
46fa0 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e  n [BLOB handle].
46fb0 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61  .**.** Closing a
46fc0 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75 73   BLOB shall caus
46fd0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  e the current tr
46fe0 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d  ansaction to com
46ff0 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65 20  mit.** if there 
47000 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f  are no other BLO
47010 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70  Bs, no pending p
47020 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
47030 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64  ts, and the.** d
47040 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
47050 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f  on is in [autoco
47060 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49  mmit mode]..** I
47070 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 65 72  f any writes wer
47080 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42 4c  e made to the BL
47090 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20 62  OB, they might b
470a0 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 0a  e held in cache.
470b0 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f  ** until the clo
470c0 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 20  se operation if 
470d0 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 20 7b  they will fit. {
470e0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69  END}.**.** Closi
470f0 6e 67 20 74 68 65 20 42 4c 4f 42 20 6f 66 74 65  ng the BLOB ofte
47100 6e 20 66 6f 72 63 65 73 20 74 68 65 20 63 68 61  n forces the cha
47110 6e 67 65 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64  nges.** out to d
47120 69 73 6b 20 61 6e 64 20 73 6f 20 69 66 20 61 6e  isk and so if an
47130 79 20 49 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63  y I/O errors occ
47140 75 72 2c 20 74 68 65 79 20 77 69 6c 6c 20 6c 69  ur, they will li
47150 6b 65 6c 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74  kely occur.** at
47160 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20 74   the time when t
47170 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65  he BLOB is close
47180 64 2e 20 20 7b 48 31 37 38 33 33 7d 20 41 6e 79  d.  {H17833} Any
47190 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63   errors that occ
471a0 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f  ur during.** clo
471b0 73 69 6e 67 20 61 72 65 20 72 65 70 6f 72 74 65  sing are reporte
471c0 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  d as a non-zero 
471d0 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a  return value..**
471e0 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69 73 20  .** The BLOB is 
471f0 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69 74 69  closed unconditi
47200 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66  onally.  Even if
47210 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
47220 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f  turns.** an erro
47230 72 20 63 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42  r code, the BLOB
47240 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64   is still closed
47250 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
47260 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 33  TS:.**.** {H1783
47270 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  3} The [sqlite3_
47280 62 6c 6f 62 5f 63 6c 6f 73 65 28 50 29 5d 20 69  blob_close(P)] i
47290 6e 74 65 72 66 61 63 65 20 63 6c 6f 73 65 73 20  nterface closes 
472a0 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  an [sqlite3_blob
472b0 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62  ].**          ob
472c0 6a 65 63 74 20 50 20 70 72 65 76 69 6f 75 73 6c  ject P previousl
472d0 79 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 5b  y opened using [
472e0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
472f0 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  n()]..**.** {H17
47300 38 33 36 7d 20 43 6c 6f 73 69 6e 67 20 61 6e 20  836} Closing an 
47310 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f  [sqlite3_blob] o
47320 62 6a 65 63 74 20 75 73 69 6e 67 0a 2a 2a 20 20  bject using.**  
47330 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
47340 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 73  _blob_close()] s
47350 68 61 6c 6c 20 63 61 75 73 65 20 74 68 65 20 63  hall cause the c
47360 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
47370 6f 6e 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  on to.**        
47380 20 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 72    commit if ther
47390 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 6f  e are no other o
473a0 70 65 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  pen [sqlite3_blo
473b0 62 5d 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 20 20  b] objects.**   
473c0 20 20 20 20 20 20 20 6f 72 20 5b 70 72 65 70 61         or [prepa
473d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20  red statements] 
473e0 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74  on the same [dat
473f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
47400 5d 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  ] and.**        
47410 20 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63    the database c
47420 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20  onnection is in 
47430 5b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65  [autocommit mode
47440 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 33 39  ]..**.** {H17839
47450 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  } The [sqlite3_b
47460 6c 6f 62 5f 63 6c 6f 73 65 28 50 29 5d 20 69 6e  lob_close(P)] in
47470 74 65 72 66 61 63 65 73 20 73 68 61 6c 6c 20 63  terfaces shall c
47480 6c 6f 73 65 20 74 68 65 0a 2a 2a 20 20 20 20 20  lose the.**     
47490 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 6c       [sqlite3_bl
474a0 6f 62 5d 20 6f 62 6a 65 63 74 20 50 20 75 6e 63  ob] object P unc
474b0 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 65 76  onditionally, ev
474c0 65 6e 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20  en if.**        
474d0 20 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f    [sqlite3_blob_
474e0 63 6c 6f 73 65 28 50 29 5d 20 72 65 74 75 72 6e  close(P)] return
474f0 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65  s something othe
47500 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f  r than [SQLITE_O
47510 4b 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  K]..*/.SQLITE_AP
47520 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
47530 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  ob_close(sqlite3
47540 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _blob *);../*.**
47550 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 75 72   CAPI3REF: Retur
47560 6e 20 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e  n The Size Of An
47570 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38   Open BLOB {H178
47580 34 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a  40} <S30230>.**.
47590 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 73  ** Returns the s
475a0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
475b0 74 68 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69  the BLOB accessi
475c0 62 6c 65 20 76 69 61 20 74 68 65 20 6f 70 65 6e  ble via the open
475d0 0a 2a 2a 20 5b 5d 42 4c 4f 42 20 68 61 6e 64 6c  .** []BLOB handl
475e0 65 5d 20 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61  e] in its only a
475f0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49  rgument..**.** I
47600 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
47610 20 7b 48 31 37 38 34 33 7d 20 54 68 65 20 5b 73   {H17843} The [s
47620 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65  qlite3_blob_byte
47630 73 28 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20  s(P)] interface 
47640 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65  returns the size
47650 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20  .**          in 
47660 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 4c 4f  bytes of the BLO
47670 42 20 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69  B that the [sqli
47680 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74  te3_blob] object
47690 20 50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72   P.**          r
476a0 65 66 65 72 73 20 74 6f 2e 0a 2a 2f 0a 53 51 4c  efers to..*/.SQL
476b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
476c0 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73  te3_blob_bytes(s
476d0 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a  qlite3_blob *);.
476e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
476f0 20 52 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20   Read Data From 
47700 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74  A BLOB Increment
47710 61 6c 6c 79 20 7b 48 31 37 38 35 30 7d 20 3c 53  ally {H17850} <S
47720 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  30230>.**.** Thi
47730 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
47740 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20  ed to read data 
47750 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b 42 4c  from an open [BL
47760 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20  OB handle] into 
47770 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70  a.** caller-supp
47780 6c 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62  lied buffer. N b
47790 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65  ytes of data are
477a0 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66   copied into buf
477b0 66 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68  fer Z.** from th
477c0 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61  e open BLOB, sta
477d0 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20  rting at offset 
477e0 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49  iOffset..**.** I
477f0 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74  f offset iOffset
47800 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20   is less than N 
47810 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65  bytes from the e
47820 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a  nd of the BLOB,.
47830 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ** [SQLITE_ERROR
47840 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
47850 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 65 61  d no data is rea
47860 64 2e 20 20 49 66 20 4e 20 6f 72 20 69 4f 66 66  d.  If N or iOff
47870 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74  set is.** less t
47880 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51 4c 49 54  han zero, [SQLIT
47890 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75  E_ERROR] is retu
478a0 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61  rned and no data
478b0 20 69 73 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20   is read..**.** 
478c0 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65  An attempt to re
478d0 61 64 20 66 72 6f 6d 20 61 6e 20 65 78 70 69 72  ad from an expir
478e0 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  ed [BLOB handle]
478f0 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a   fails with an.*
47900 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20  * error code of 
47910 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a  [SQLITE_ABORT]..
47920 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73  **.** On success
47930 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
47940 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65  eturned..** Othe
47950 72 77 69 73 65 2c 20 61 6e 20 5b 65 72 72 6f 72  rwise, an [error
47960 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78   code] or an [ex
47970 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
47980 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  e] is returned..
47990 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
479a0 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 35 33 7d  :.**.** {H17853}
479b0 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e   A successful in
479c0 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c  vocation of [sql
479d0 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50  ite3_blob_read(P
479e0 2c 5a 2c 4e 2c 58 29 5d 20 0a 2a 2a 20 20 20 20  ,Z,N,X)] .**    
479f0 20 20 20 20 20 20 73 68 61 6c 6c 20 72 65 61 64        shall read
47a00 73 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74  s N bytes of dat
47a10 61 20 6f 75 74 20 6f 66 20 74 68 65 20 42 4c 4f  a out of the BLO
47a20 42 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 0a  B referenced by.
47a30 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 42 4c 4f  **          [BLO
47a40 42 20 68 61 6e 64 6c 65 5d 20 50 20 62 65 67 69  B handle] P begi
47a50 6e 6e 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20  nning at offset 
47a60 58 20 61 6e 64 20 73 74 6f 72 65 20 74 68 6f 73  X and store thos
47a70 65 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 20  e bytes.**      
47a80 20 20 20 20 69 6e 74 6f 20 62 75 66 66 65 72 20      into buffer 
47a90 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 35 36  Z..**.** {H17856
47aa0 7d 20 49 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c  } In [sqlite3_bl
47ab0 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 29  ob_read(P,Z,N,X)
47ac0 5d 20 69 66 20 74 68 65 20 73 69 7a 65 20 6f 66  ] if the size of
47ad0 20 74 68 65 20 42 4c 4f 42 0a 2a 2a 20 20 20 20   the BLOB.**    
47ae0 20 20 20 20 20 20 69 73 20 6c 65 73 73 20 74 68        is less th
47af0 61 6e 20 4e 2b 58 20 62 79 74 65 73 2c 20 74 68  an N+X bytes, th
47b00 65 6e 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  en the function 
47b10 73 68 61 6c 6c 20 6c 65 61 76 65 20 74 68 65 0a  shall leave the.
47b20 2a 2a 20 20 20 20 20 20 20 20 20 20 5a 20 62 75  **          Z bu
47b30 66 66 65 72 20 75 6e 63 68 61 6e 67 65 64 20 61  ffer unchanged a
47b40 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  nd return [SQLIT
47b50 45 5f 45 52 52 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20  E_ERROR]..**.** 
47b60 7b 48 31 37 38 35 39 7d 20 49 6e 20 5b 73 71 6c  {H17859} In [sql
47b70 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50  ite3_blob_read(P
47b80 2c 5a 2c 4e 2c 58 29 5d 20 69 66 20 58 20 6f 72  ,Z,N,X)] if X or
47b90 20 4e 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20   N is less than 
47ba0 7a 65 72 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  zero.**         
47bb0 20 74 68 65 6e 20 74 68 65 20 66 75 6e 63 74 69   then the functi
47bc0 6f 6e 20 73 68 61 6c 6c 20 6c 65 61 76 65 20 74  on shall leave t
47bd0 68 65 20 5a 20 62 75 66 66 65 72 20 75 6e 63 68  he Z buffer unch
47be0 61 6e 67 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  anged.**        
47bf0 20 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51    and return [SQ
47c00 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a 2a 2a 0a  LITE_ERROR]..**.
47c10 2a 2a 20 7b 48 31 37 38 36 32 7d 20 54 68 65 20  ** {H17862} The 
47c20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65  [sqlite3_blob_re
47c30 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69 6e 74  ad(P,Z,N,X)] int
47c40 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74  erface shall ret
47c50 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a  urn [SQLITE_OK].
47c60 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 4e  **          if N
47c70 20 62 79 74 65 73 20 61 72 65 20 73 75 63 63 65   bytes are succe
47c80 73 73 66 75 6c 6c 79 20 72 65 61 64 20 69 6e 74  ssfully read int
47c90 6f 20 62 75 66 66 65 72 20 5a 2e 0a 2a 2a 0a 2a  o buffer Z..**.*
47ca0 2a 20 7b 48 31 37 38 36 33 7d 20 49 66 20 74 68  * {H17863} If th
47cb0 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20  e [BLOB handle] 
47cc0 50 20 69 73 20 65 78 70 69 72 65 64 20 61 6e 64  P is expired and
47cd0 20 58 20 61 6e 64 20 4e 20 61 72 65 20 77 69 74   X and N are wit
47ce0 68 69 6e 20 62 6f 75 6e 64 73 0a 2a 2a 20 20 20  hin bounds.**   
47cf0 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c         then [sql
47d00 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50  ite3_blob_read(P
47d10 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 20 6c  ,Z,N,X)] shall l
47d20 65 61 76 65 20 74 68 65 20 5a 20 62 75 66 66 65  eave the Z buffe
47d30 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 6e  r.**          un
47d40 63 68 61 6e 67 65 64 20 61 6e 64 20 72 65 74 75  changed and retu
47d50 72 6e 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54  rn [SQLITE_ABORT
47d60 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 36 35  ]..**.** {H17865
47d70 7d 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74  } If the request
47d80 65 64 20 72 65 61 64 20 63 6f 75 6c 64 20 6e 6f  ed read could no
47d90 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 64 2c 0a  t be completed,.
47da0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
47db0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65  [sqlite3_blob_re
47dc0 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69 6e 74  ad(P,Z,N,X)] int
47dd0 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74  erface shall ret
47de0 75 72 6e 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20  urn an.**       
47df0 20 20 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b     appropriate [
47e00 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 5b  error code] or [
47e10 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
47e20 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  ode]..**.** {H17
47e30 38 36 38 7d 20 49 66 20 61 6e 20 65 72 72 6f 72  868} If an error
47e40 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 65   occurs during e
47e50 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b 73 71  valuation of [sq
47e60 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28  lite3_blob_read(
47e70 50 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20  P,...)].**      
47e80 20 20 20 20 74 68 65 6e 20 73 75 62 73 65 71 75      then subsequ
47e90 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71  ent calls to [sq
47ea0 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 44 29  lite3_errcode(D)
47eb0 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  ],.**          [
47ec0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
47ed0 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20  _errcode()],.** 
47ee0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
47ef0 33 5f 65 72 72 6d 73 67 28 44 29 5d 2c 20 61 6e  3_errmsg(D)], an
47f00 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  d [sqlite3_errms
47f10 67 31 36 28 44 29 5d 20 73 68 61 6c 6c 20 72 65  g16(D)] shall re
47f20 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  turn.**         
47f30 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70   information app
47f40 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 61  ropriate for tha
47f50 74 20 65 72 72 6f 72 2c 20 77 68 65 72 65 20 44  t error, where D
47f60 20 69 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   is the.**      
47f70 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f      [database co
47f80 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77  nnection] that w
47f90 61 73 20 75 73 65 64 20 74 6f 20 6f 70 65 6e 20  as used to open 
47fa0 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  the [BLOB handle
47fb0 5d 20 50 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ] P..*/.SQLITE_A
47fc0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
47fd0 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33  lob_read(sqlite3
47fe0 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69 64 20 2a 5a  _blob *, void *Z
47ff0 2c 20 69 6e 74 20 4e 2c 20 69 6e 74 20 69 4f 66  , int N, int iOf
48000 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  fset);../*.** CA
48010 50 49 33 52 45 46 3a 20 57 72 69 74 65 20 44 61  PI3REF: Write Da
48020 74 61 20 49 6e 74 6f 20 41 20 42 4c 4f 42 20 49  ta Into A BLOB I
48030 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31  ncrementally {H1
48040 37 38 37 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a  7870} <S30230>.*
48050 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
48060 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 77 72  on is used to wr
48070 69 74 65 20 64 61 74 61 20 69 6e 74 6f 20 61 6e  ite data into an
48080 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64   open [BLOB hand
48090 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a 20 63 61  le] from a.** ca
480a0 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62 75  ller-supplied bu
480b0 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f 66  ffer. N bytes of
480c0 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64   data are copied
480d0 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72   from the buffer
480e0 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f   Z.** into the o
480f0 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 69  pen BLOB, starti
48100 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66  ng at offset iOf
48110 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  fset..**.** If t
48120 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  he [BLOB handle]
48130 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
48140 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 77 61  irst argument wa
48150 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 66 6f 72  s not opened for
48160 0a 2a 2a 20 77 72 69 74 69 6e 67 20 28 74 68 65  .** writing (the
48170 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72   flags parameter
48180 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f   to [sqlite3_blo
48190 62 5f 6f 70 65 6e 28 29 5d 20 77 61 73 20 7a 65  b_open()] was ze
481a0 72 6f 29 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e  ro),.** this fun
481b0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53  ction returns [S
481c0 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2e  QLITE_READONLY].
481d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
481e0 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 6d 6f  tion may only mo
481f0 64 69 66 79 20 74 68 65 20 63 6f 6e 74 65 6e 74  dify the content
48200 73 20 6f 66 20 74 68 65 20 42 4c 4f 42 3b 20 69  s of the BLOB; i
48210 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73  t is.** not poss
48220 69 62 6c 65 20 74 6f 20 69 6e 63 72 65 61 73 65  ible to increase
48230 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42   the size of a B
48240 4c 4f 42 20 75 73 69 6e 67 20 74 68 69 73 20 41  LOB using this A
48250 50 49 2e 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74  PI..** If offset
48260 20 69 4f 66 66 73 65 74 20 69 73 20 6c 65 73 73   iOffset is less
48270 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 66 72   than N bytes fr
48280 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  om the end of th
48290 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49  e BLOB,.** [SQLI
482a0 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74  TE_ERROR] is ret
482b0 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74  urned and no dat
482c0 61 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49  a is written.  I
482d0 66 20 4e 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74  f N is.** less t
482e0 68 61 6e 20 7a 65 72 6f 20 5b 53 51 4c 49 54 45  han zero [SQLITE
482f0 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72  _ERROR] is retur
48300 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20  ned and no data 
48310 69 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a  is written..**.*
48320 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  * An attempt to 
48330 77 72 69 74 65 20 74 6f 20 61 6e 20 65 78 70 69  write to an expi
48340 72 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  red [BLOB handle
48350 5d 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a  ] fails with an.
48360 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66  ** error code of
48370 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e   [SQLITE_ABORT].
48380 20 20 57 72 69 74 65 73 20 74 6f 20 74 68 65 20    Writes to the 
48390 42 4c 4f 42 20 74 68 61 74 20 6f 63 63 75 72 72  BLOB that occurr
483a0 65 64 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65  ed.** before the
483b0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65   [BLOB handle] e
483c0 78 70 69 72 65 64 20 61 72 65 20 6e 6f 74 20 72  xpired are not r
483d0 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68  olled back by th
483e0 65 0a 2a 2a 20 65 78 70 69 72 61 74 69 6f 6e 20  e.** expiration 
483f0 6f 66 20 74 68 65 20 68 61 6e 64 6c 65 2c 20 74  of the handle, t
48400 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20  hough of course 
48410 74 68 6f 73 65 20 63 68 61 6e 67 65 73 20 6d 69  those changes mi
48420 67 68 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e  ght.** have been
48430 20 6f 76 65 72 77 72 69 74 74 65 6e 20 62 79 20   overwritten by 
48440 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68  the statement th
48450 61 74 20 65 78 70 69 72 65 64 20 74 68 65 20 42  at expired the B
48460 4c 4f 42 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72  LOB handle.** or
48470 20 62 79 20 6f 74 68 65 72 20 69 6e 64 65 70 65   by other indepe
48480 6e 64 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 73  ndent statements
48490 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65  ..**.** On succe
484a0 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  ss, SQLITE_OK is
484b0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74   returned..** Ot
484c0 68 65 72 77 69 73 65 2c 20 61 6e 20 20 5b 65 72  herwise, an  [er
484d0 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20  ror code] or an 
484e0 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20  [extended error 
484f0 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65  code] is returne
48500 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  d..**.** INVARIA
48510 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38  NTS:.**.** {H178
48520 37 33 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  73} A successful
48530 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b   invocation of [
48540 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69  sqlite3_blob_wri
48550 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 0a 2a 2a 20  te(P,Z,N,X)].** 
48560 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 77           shall w
48570 72 69 74 65 20 4e 20 62 79 74 65 73 20 6f 66 20  rite N bytes of 
48580 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66 65 72  data from buffer
48590 20 5a 20 69 6e 74 6f 20 74 68 65 20 42 4c 4f 42   Z into the BLOB
485a0 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65   .**          re
485b0 66 65 72 65 6e 63 65 64 20 62 79 20 5b 42 4c 4f  ferenced by [BLO
485c0 42 20 68 61 6e 64 6c 65 5d 20 50 20 62 65 67 69  B handle] P begi
485d0 6e 6e 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20  nning at offset 
485e0 58 20 69 6e 74 6f 0a 2a 2a 20 20 20 20 20 20 20  X into.**       
485f0 20 20 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 0a     the BLOB..**.
48600 2a 2a 20 7b 48 31 37 38 37 34 7d 20 49 6e 20 74  ** {H17874} In t
48610 68 65 20 61 62 73 65 6e 63 65 20 6f 66 20 6f 74  he absence of ot
48620 68 65 72 20 6f 76 65 72 72 69 64 64 69 6e 67 20  her overridding 
48630 63 68 61 6e 67 65 73 2c 20 74 68 65 20 63 68 61  changes, the cha
48640 6e 67 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  nges.**         
48650 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 42 4c   written to a BL
48660 4f 42 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62  OB by [sqlite3_b
48670 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 73 68 61  lob_write()] sha
48680 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72  ll.**          r
48690 65 6d 61 69 6e 20 69 6e 20 65 66 66 65 63 74 20  emain in effect 
486a0 61 66 74 65 72 20 74 68 65 20 61 73 73 6f 63 69  after the associ
486b0 61 74 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c  ated [BLOB handl
486c0 65 5d 20 65 78 70 69 72 65 73 2e 0a 2a 2a 0a 2a  e] expires..**.*
486d0 2a 20 7b 48 31 37 38 37 35 7d 20 49 66 20 74 68  * {H17875} If th
486e0 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20  e [BLOB handle] 
486f0 50 20 77 61 73 20 6f 70 65 6e 65 64 20 66 6f 72  P was opened for
48700 20 72 65 61 64 69 6e 67 20 6f 6e 6c 79 20 74 68   reading only th
48710 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  en.**          a
48720 6e 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  n invocation of 
48730 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72  [sqlite3_blob_wr
48740 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 20 73 68  ite(P,Z,N,X)] sh
48750 61 6c 6c 20 6c 65 61 76 65 0a 2a 2a 20 20 20 20  all leave.**    
48760 20 20 20 20 20 20 74 68 65 20 72 65 66 65 72 65        the refere
48770 6e 63 65 64 20 42 4c 4f 42 20 75 6e 63 68 61 6e  nced BLOB unchan
48780 67 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b  ged and return [
48790 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d  SQLITE_READONLY]
487a0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 36 7d  ..**.** {H17876}
487b0 20 49 66 20 74 68 65 20 73 69 7a 65 20 6f 66 20   If the size of 
487c0 74 68 65 20 42 4c 4f 42 20 72 65 66 65 72 65 6e  the BLOB referen
487d0 63 65 64 20 62 79 20 5b 42 4c 4f 42 20 68 61 6e  ced by [BLOB han
487e0 64 6c 65 5d 20 50 20 69 73 0a 2a 2a 20 20 20 20  dle] P is.**    
487f0 20 20 20 20 20 20 6c 65 73 73 20 74 68 61 6e 20        less than 
48800 4e 2b 58 20 62 79 74 65 73 20 74 68 65 6e 20 5b  N+X bytes then [
48810 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69  sqlite3_blob_wri
48820 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61  te(P,Z,N,X)] sha
48830 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6c  ll.**          l
48840 65 61 76 65 20 74 68 65 20 42 4c 4f 42 20 75 6e  eave the BLOB un
48850 63 68 61 6e 67 65 64 20 61 6e 64 20 72 65 74 75  changed and retu
48860 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  rn [SQLITE_ERROR
48870 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 37  ]..**.** {H17877
48880 7d 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68  } If the [BLOB h
48890 61 6e 64 6c 65 5d 20 50 20 69 73 20 65 78 70 69  andle] P is expi
488a0 72 65 64 20 61 6e 64 20 58 20 61 6e 64 20 4e 20  red and X and N 
488b0 61 72 65 20 77 69 74 68 69 6e 20 62 6f 75 6e 64  are within bound
488c0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  s.**          th
488d0 65 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  en [sqlite3_blob
488e0 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20  _read(P,Z,N,X)] 
488f0 73 68 61 6c 6c 20 6c 65 61 76 65 20 74 68 65 20  shall leave the 
48900 42 4c 4f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20  BLOB.**         
48910 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 72   unchanged and r
48920 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 41 42  eturn [SQLITE_AB
48930 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  ORT]..**.** {H17
48940 38 37 39 7d 20 49 66 20 58 20 6f 72 20 4e 20 61  879} If X or N a
48950 72 65 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72  re less than zer
48960 6f 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f  o then [sqlite3_
48970 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 2c 4e  blob_write(P,Z,N
48980 2c 58 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ,X)].**         
48990 20 73 68 61 6c 6c 20 6c 65 61 76 65 20 74 68 65   shall leave the
489a0 20 42 4c 4f 42 20 72 65 66 65 72 65 6e 63 65 64   BLOB referenced
489b0 20 62 79 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65   by [BLOB handle
489c0 5d 20 50 20 75 6e 63 68 61 6e 67 65 64 0a 2a 2a  ] P unchanged.**
489d0 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 72 65            and re
489e0 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52  turn [SQLITE_ERR
489f0 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38  OR]..**.** {H178
48a00 38 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  82} The [sqlite3
48a10 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 2c  _blob_write(P,Z,
48a20 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65 20  N,X)] interface 
48a30 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20  shall return.** 
48a40 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45           [SQLITE
48a50 5f 4f 4b 5d 20 69 66 20 4e 20 62 79 74 65 73 20  _OK] if N bytes 
48a60 77 68 65 72 65 20 73 75 63 63 65 73 73 66 75 6c  where successful
48a70 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  ly written into 
48a80 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20  the BLOB..**.** 
48a90 7b 48 31 37 38 38 35 7d 20 49 66 20 74 68 65 20  {H17885} If the 
48aa0 72 65 71 75 65 73 74 65 64 20 77 72 69 74 65 20  requested write 
48ab0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6d  could not be com
48ac0 70 6c 65 74 65 64 2c 0a 2a 2a 20 20 20 20 20 20  pleted,.**      
48ad0 20 20 20 20 74 68 65 20 5b 73 71 6c 69 74 65 33      the [sqlite3
48ae0 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 2c  _blob_write(P,Z,
48af0 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65 20  N,X)] interface 
48b00 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 61 6e 0a  shall return an.
48b10 2a 2a 20 20 20 20 20 20 20 20 20 20 61 70 70 72  **          appr
48b20 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20 63  opriate [error c
48b30 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65  ode] or [extende
48b40 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a  d error code]..*
48b50 2a 0a 2a 2a 20 7b 48 31 37 38 38 38 7d 20 49 66  *.** {H17888} If
48b60 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
48b70 20 64 75 72 69 6e 67 20 65 76 61 6c 75 61 74 69   during evaluati
48b80 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 62  on of [sqlite3_b
48b90 6c 6f 62 5f 77 72 69 74 65 28 44 2c 2e 2e 2e 29  lob_write(D,...)
48ba0 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  ].**          th
48bb0 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  en subsequent ca
48bc0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
48bd0 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a 2a 2a 20  errcode(D)],.** 
48be0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
48bf0 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f  3_extended_errco
48c00 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  de()],.**       
48c10 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d     [sqlite3_errm
48c20 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b 73 71 6c  sg(D)], and [sql
48c30 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 44 29  ite3_errmsg16(D)
48c40 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a  ] shall return.*
48c50 2a 20 20 20 20 20 20 20 20 20 20 69 6e 66 6f 72  *          infor
48c60 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 69 61  mation appropria
48c70 74 65 20 66 6f 72 20 74 68 61 74 20 65 72 72 6f  te for that erro
48c80 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
48c90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
48ca0 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f  b_write(sqlite3_
48cb0 62 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f  blob *, const vo
48cc0 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e  id *z, int n, in
48cd0 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a  t iOffset);../*.
48ce0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72  ** CAPI3REF: Vir
48cf0 74 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d  tual File System
48d00 20 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30   Objects {H11200
48d10 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20100>.**.**
48d20 20 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73   A virtual files
48d30 79 73 74 65 6d 20 28 56 46 53 29 20 69 73 20 61  ystem (VFS) is a
48d40 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  n [sqlite3_vfs] 
48d50 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53  object.** that S
48d60 51 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e  QLite uses to in
48d70 74 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74  teract.** with t
48d80 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70  he underlying op
48d90 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20  erating system. 
48da0 20 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69   Most SQLite bui
48db0 6c 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a  lds come with a.
48dc0 2a 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c  ** single defaul
48dd0 74 20 56 46 53 20 74 68 61 74 20 69 73 20 61 70  t VFS that is ap
48de0 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68  propriate for th
48df0 65 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e  e host computer.
48e00 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61  .** New VFSes ca
48e10 6e 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20  n be registered 
48e20 61 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46 53  and existing VFS
48e30 65 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69  es can be unregi
48e40 73 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66  stered..** The f
48e50 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61  ollowing interfa
48e60 63 65 73 20 61 72 65 20 70 72 6f 76 69 64 65 64  ces are provided
48e70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
48e80 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69  te3_vfs_find() i
48e90 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
48ea0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
48eb0 56 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61  VFS given its na
48ec0 6d 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65  me..** Names are
48ed0 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e   case sensitive.
48ee0 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65  .** Names are ze
48ef0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ro-terminated UT
48f00 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20  F-8 strings..** 
48f10 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  If there is no m
48f20 61 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69  atch, a NULL poi
48f30 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
48f40 2e 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65  ..** If zVfsName
48f50 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68   is NULL then th
48f60 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73  e default VFS is
48f70 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
48f80 20 4e 65 77 20 56 46 53 65 73 20 61 72 65 20 72   New VFSes are r
48f90 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 73  egistered with s
48fa0 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
48fb0 74 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e  ter()..** Each n
48fc0 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74  ew VFS becomes t
48fd0 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69  he default VFS i
48fe0 66 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66  f the makeDflt f
48ff0 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54  lag is set..** T
49000 68 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20  he same VFS can 
49010 62 65 20 72 65 67 69 73 74 65 72 65 64 20 6d 75  be registered mu
49020 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74  ltiple times wit
49030 68 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20  hout injury..** 
49040 54 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74  To make an exist
49050 69 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65  ing VFS into the
49060 20 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65   default VFS, re
49070 67 69 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a  gister it again.
49080 2a 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65  ** with the make
49090 44 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20  Dflt flag set.  
490a0 49 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74  If two different
490b0 20 56 46 53 65 73 20 77 69 74 68 20 74 68 65 0a   VFSes with the.
490c0 2a 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65  ** same name are
490d0 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 65   registered, the
490e0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
490f0 65 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a  efined.  If a.**
49100 20 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72   VFS is register
49110 65 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74  ed with a name t
49120 68 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61  hat is NULL or a
49130 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a  n empty string,.
49140 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 61  ** then the beha
49150 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
49160 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73  d..**.** Unregis
49170 74 65 72 20 61 20 56 46 53 20 77 69 74 68 20 74  ter a VFS with t
49180 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75  he sqlite3_vfs_u
49190 6e 72 65 67 69 73 74 65 72 28 29 20 69 6e 74 65  nregister() inte
491a0 72 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65  rface..** If the
491b0 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20   default VFS is 
491c0 75 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e  unregistered, an
491d0 6f 74 68 65 72 20 56 46 53 20 69 73 20 63 68 6f  other VFS is cho
491e0 73 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65  sen as.** the de
491f0 66 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69  fault.  The choi
49200 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56  ce for the new V
49210 46 53 20 69 73 20 61 72 62 69 74 72 61 72 79 2e  FS is arbitrary.
49220 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
49230 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 30 33  S:.**.** {H11203
49240 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  } The [sqlite3_v
49250 66 73 5f 66 69 6e 64 28 4e 29 5d 20 69 6e 74 65  fs_find(N)] inte
49260 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
49270 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a  pointer to the.*
49280 2a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73  *          regis
49290 74 65 72 65 64 20 5b 73 71 6c 69 74 65 33 5f 76  tered [sqlite3_v
492a0 66 73 5d 20 6f 62 6a 65 63 74 20 77 68 6f 73 65  fs] object whose
492b0 20 6e 61 6d 65 20 65 78 61 63 74 6c 79 20 6d 61   name exactly ma
492c0 74 63 68 65 73 0a 2a 2a 20 20 20 20 20 20 20 20  tches.**        
492d0 20 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69    the zero-termi
492e0 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69  nated UTF-8 stri
492f0 6e 67 20 4e 2c 20 6f 72 20 69 74 20 72 65 74 75  ng N, or it retu
49300 72 6e 73 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20 20  rns NULL if.**  
49310 20 20 20 20 20 20 20 20 74 68 65 72 65 20 69 73          there is
49320 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2a   no match..**.**
49330 20 7b 48 31 31 32 30 36 7d 20 49 66 20 74 68 65   {H11206} If the
49340 20 4e 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20   N parameter to 
49350 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e  [sqlite3_vfs_fin
49360 64 28 4e 29 5d 20 69 73 20 4e 55 4c 4c 20 74 68  d(N)] is NULL th
49370 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  en.**          t
49380 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
49390 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
493a0 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71   the default [sq
493b0 6c 69 74 65 33 5f 76 66 73 5d 0a 2a 2a 20 20 20  lite3_vfs].**   
493c0 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 69 66         object if
493d0 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2c 20 6f   there is one, o
493e0 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20  r NULL if there 
493f0 69 73 20 6e 6f 20 64 65 66 61 75 6c 74 0a 2a 2a  is no default.**
49400 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
49410 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a  e3_vfs] object..
49420 2a 2a 0a 2a 2a 20 7b 48 31 31 32 30 39 7d 20 54  **.** {H11209} T
49430 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f  he [sqlite3_vfs_
49440 72 65 67 69 73 74 65 72 28 50 2c 46 29 5d 20 69  register(P,F)] i
49450 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65  nterface registe
49460 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  rs the.**       
49470 20 20 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 5b     well-formed [
49480 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a  sqlite3_vfs] obj
49490 65 63 74 20 50 20 75 73 69 6e 67 20 74 68 65 20  ect P using the 
494a0 6e 61 6d 65 20 67 69 76 65 6e 0a 2a 2a 20 20 20  name given.**   
494b0 20 20 20 20 20 20 20 62 79 20 74 68 65 20 7a 4e         by the zN
494c0 61 6d 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ame field of the
494d0 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 7b   object..**.** {
494e0 48 31 31 32 31 32 7d 20 55 73 69 6e 67 20 74 68  H11212} Using th
494f0 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72  e [sqlite3_vfs_r
49500 65 67 69 73 74 65 72 28 50 2c 46 29 5d 20 69 6e  egister(P,F)] in
49510 74 65 72 66 61 63 65 20 74 6f 20 72 65 67 69 73  terface to regis
49520 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ter.**          
49530 74 68 65 20 73 61 6d 65 20 5b 73 71 6c 69 74 65  the same [sqlite
49540 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 6d 75  3_vfs] object mu
49550 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 69 73 20  ltiple times is 
49560 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70  a harmless no-op
49570 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 31 35 7d  ..**.** {H11215}
49580 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   The [sqlite3_vf
49590 73 5f 72 65 67 69 73 74 65 72 28 50 2c 46 29 5d  s_register(P,F)]
495a0 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73   interface makes
495b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   the [sqlite3_vf
495c0 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  s].**          o
495d0 62 6a 65 63 74 20 50 20 74 68 65 20 64 65 66 61  bject P the defa
495e0 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  ult [sqlite3_vfs
495f0 5d 20 6f 62 6a 65 63 74 20 69 66 20 46 20 69 73  ] object if F is
49600 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a   non-zero..**.**
49610 20 7b 48 31 31 32 31 38 7d 20 54 68 65 20 5b 73   {H11218} The [s
49620 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67  qlite3_vfs_unreg
49630 69 73 74 65 72 28 50 29 5d 20 69 6e 74 65 72 66  ister(P)] interf
49640 61 63 65 20 75 6e 72 65 67 69 73 74 65 72 73 20  ace unregisters 
49650 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
49660 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
49670 6a 65 63 74 20 50 20 73 6f 20 74 68 61 74 20 69  ject P so that i
49680 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72  t is no longer r
49690 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20  eturned by.**   
496a0 20 20 20 20 20 20 20 73 75 62 73 65 71 75 65 6e         subsequen
496b0 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69  t calls to [sqli
496c0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 5d 2e  te3_vfs_find()].
496d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
496e0 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
496f0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e  te3_vfs_find(con
49700 73 74 20 63 68 61 72 20 2a 7a 56 66 73 4e 61 6d  st char *zVfsNam
49710 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  e);.SQLITE_API i
49720 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72  nt sqlite3_vfs_r
49730 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f  egister(sqlite3_
49740 76 66 73 2a 2c 20 69 6e 74 20 6d 61 6b 65 44 66  vfs*, int makeDf
49750 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  lt);.SQLITE_API 
49760 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  int sqlite3_vfs_
49770 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 74  unregister(sqlit
49780 65 33 5f 76 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  e3_vfs*);../*.**
49790 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
497a0 65 73 20 7b 48 31 37 30 30 30 7d 20 3c 53 32 30  es {H17000} <S20
497b0 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53  000>.**.** The S
497c0 51 4c 69 74 65 20 63 6f 72 65 20 75 73 65 73 20  QLite core uses 
497d0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66  these routines f
497e0 6f 72 20 74 68 72 65 61 64 0a 2a 2a 20 73 79 6e  or thread.** syn
497f0 63 68 72 6f 6e 69 7a 61 74 69 6f 6e 2e 20 54 68  chronization. Th
49800 6f 75 67 68 20 74 68 65 79 20 61 72 65 20 69 6e  ough they are in
49810 74 65 6e 64 65 64 20 66 6f 72 20 69 6e 74 65 72  tended for inter
49820 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51  nal.** use by SQ
49830 4c 69 74 65 2c 20 63 6f 64 65 20 74 68 61 74 20  Lite, code that 
49840 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 53 51  links against SQ
49850 4c 69 74 65 20 69 73 0a 2a 2a 20 70 65 72 6d 69  Lite is.** permi
49860 74 74 65 64 20 74 6f 20 75 73 65 20 61 6e 79 20  tted to use any 
49870 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  of these routine
49880 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  s..**.** The SQL
49890 69 74 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20  ite source code 
498a0 63 6f 6e 74 61 69 6e 73 20 6d 75 6c 74 69 70 6c  contains multipl
498b0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
498c0 73 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 6d 75  s.** of these mu
498d0 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 20 20 41  tex routines.  A
498e0 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6d  n appropriate im
498f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  plementation.** 
49900 69 73 20 73 65 6c 65 63 74 65 64 20 61 75 74 6f  is selected auto
49910 6d 61 74 69 63 61 6c 6c 79 20 61 74 20 63 6f 6d  matically at com
49920 70 69 6c 65 2d 74 69 6d 65 2e 20 20 54 68 65 20  pile-time.  The 
49930 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70  following.** imp
49940 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 72 65  lementations are
49950 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
49960 65 20 53 51 4c 69 74 65 20 63 6f 72 65 3a 0a 2a  e SQLite core:.*
49970 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
49980 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  >   SQLITE_MUTEX
49990 5f 4f 53 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53  _OS2.** <li>   S
499a0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52  QLITE_MUTEX_PTHR
499b0 45 41 44 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51  EAD.** <li>   SQ
499c0 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 2a  LITE_MUTEX_W32.*
499d0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f  * <li>   SQLITE_
499e0 4d 55 54 45 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f  MUTEX_NOOP.** </
499f0 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  ul>.**.** The SQ
49a00 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20  LITE_MUTEX_NOOP 
49a10 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
49a20 73 20 61 20 73 65 74 20 6f 66 20 72 6f 75 74 69  s a set of routi
49a30 6e 65 73 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73  nes.** that does
49a40 20 6e 6f 20 72 65 61 6c 20 6c 6f 63 6b 69 6e 67   no real locking
49a50 20 61 6e 64 20 69 73 20 61 70 70 72 6f 70 72 69   and is appropri
49a60 61 74 65 20 66 6f 72 20 75 73 65 20 69 6e 0a 2a  ate for use in.*
49a70 2a 20 61 20 73 69 6e 67 6c 65 2d 74 68 72 65 61  * a single-threa
49a80 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e  ded application.
49a90 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54    The SQLITE_MUT
49aa0 45 58 5f 4f 53 32 2c 0a 2a 2a 20 53 51 4c 49 54  EX_OS2,.** SQLIT
49ab0 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 2c  E_MUTEX_PTHREAD,
49ac0 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45   and SQLITE_MUTE
49ad0 58 5f 57 33 32 20 69 6d 70 6c 65 6d 65 6e 74 61  X_W32 implementa
49ae0 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 70 70  tions.** are app
49af0 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65  ropriate for use
49b00 20 6f 6e 20 4f 53 2f 32 2c 20 55 6e 69 78 2c 20   on OS/2, Unix, 
49b10 61 6e 64 20 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a  and Windows..**.
49b20 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20  ** If SQLite is 
49b30 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
49b40 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41  e SQLITE_MUTEX_A
49b50 50 50 44 45 46 20 70 72 65 70 72 6f 63 65 73 73  PPDEF preprocess
49b60 6f 72 0a 2a 2a 20 6d 61 63 72 6f 20 64 65 66 69  or.** macro defi
49b70 6e 65 64 20 28 77 69 74 68 20 22 2d 44 53 51 4c  ned (with "-DSQL
49b80 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46  ITE_MUTEX_APPDEF
49b90 3d 31 22 29 2c 20 74 68 65 6e 20 6e 6f 20 6d 75  =1"), then no mu
49ba0 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  tex.** implement
49bb0 61 74 69 6f 6e 20 69 73 20 69 6e 63 6c 75 64 65  ation is include
49bc0 64 20 77 69 74 68 20 74 68 65 20 6c 69 62 72 61  d with the libra
49bd0 72 79 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ry. In this case
49be0 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74   the.** applicat
49bf0 69 6f 6e 20 6d 75 73 74 20 73 75 70 70 6c 79 20  ion must supply 
49c00 61 20 63 75 73 74 6f 6d 20 6d 75 74 65 78 20 69  a custom mutex i
49c10 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73  mplementation us
49c20 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49  ing the.** [SQLI
49c30 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d  TE_CONFIG_MUTEX]
49c40 20 6f 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 73   option of the s
49c50 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
49c60 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f  function.** befo
49c70 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  re calling sqlit
49c80 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
49c90 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 75 62  or any other pub
49ca0 6c 69 63 20 73 71 6c 69 74 65 33 5f 0a 2a 2a 20  lic sqlite3_.** 
49cb0 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 63 61  function that ca
49cc0 6c 6c 73 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  lls sqlite3_init
49cd0 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  ialize()..**.** 
49ce0 7b 48 31 37 30 31 31 7d 20 54 68 65 20 73 71 6c  {H17011} The sql
49cf0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
49d00 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63  () routine alloc
49d10 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75  ates a new.** mu
49d20 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20  tex and returns 
49d30 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
49d40 20 7b 48 31 37 30 31 32 7d 20 49 66 20 69 74 20   {H17012} If it 
49d50 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20  returns NULL.** 
49d60 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  that means that 
49d70 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f  a mutex could no
49d80 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20  t be allocated. 
49d90 7b 48 31 37 30 31 33 7d 20 53 51 4c 69 74 65 0a  {H17013} SQLite.
49da0 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69  ** will unwind i
49db0 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74  ts stack and ret
49dc0 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 7b 48  urn an error. {H
49dd0 31 37 30 31 34 7d 20 54 68 65 20 61 72 67 75 6d  17014} The argum
49de0 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  ent.** to sqlite
49df0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
49e00 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20  is one of these 
49e10 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
49e20 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  s:.**.** <ul>.**
49e30 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
49e40 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e  TEX_FAST.** <li>
49e50 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52    SQLITE_MUTEX_R
49e60 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e  ECURSIVE.** <li>
49e70 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
49e80 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20  TATIC_MASTER.** 
49e90 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
49ea0 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a  EX_STATIC_MEM.**
49eb0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
49ec0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a  TEX_STATIC_MEM2.
49ed0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
49ee0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e  MUTEX_STATIC_PRN
49ef0 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  G.** <li>  SQLIT
49f00 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
49f10 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  RU.** <li>  SQLI
49f20 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
49f30 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  LRU2.** </ul>.**
49f40 0a 2a 2a 20 7b 48 31 37 30 31 35 7d 20 54 68 65  .** {H17015} The
49f50 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74   first two const
49f60 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74  ants cause sqlit
49f70 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
49f80 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20   to create.** a 
49f90 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20  new mutex.  The 
49fa0 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63  new mutex is rec
49fb0 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49  ursive when SQLI
49fc0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
49fd0 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75  VE.** is used bu
49fe0 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  t not necessaril
49ff0 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45  y so when SQLITE
4a000 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75  _MUTEX_FAST is u
4a010 73 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68  sed. {END}.** Th
4a020 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
4a030 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
4a040 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
4a050 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65  istinction.** be
4a060 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54  tween SQLITE_MUT
4a070 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64  EX_RECURSIVE and
4a080 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
4a090 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a  ST if it does.**
4a0a0 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 7b   not want to.  {
4a0b0 48 31 37 30 31 36 7d 20 42 75 74 20 53 51 4c 69  H17016} But SQLi
4a0c0 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71  te will only req
4a0d0 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65  uest a recursive
4a0e0 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73   mutex in.** cas
4a0f0 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c  es where it real
4a100 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 7b  ly needs one.  {
4a110 45 4e 44 7d 20 49 66 20 61 20 66 61 73 74 65 72  END} If a faster
4a120 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d   non-recursive m
4a130 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  utex.** implemen
4a140 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61  tation is availa
4a150 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20  ble on the host 
4a160 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75  platform, the mu
4a170 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a  tex subsystem.**
4a180 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 75   might return su
4a190 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65  ch a mutex in re
4a1a0 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45  sponse to SQLITE
4a1b0 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a  _MUTEX_FAST..**.
4a1c0 2a 2a 20 7b 48 31 37 30 31 37 7d 20 54 68 65 20  ** {H17017} The 
4a1d0 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61  other allowed pa
4a1e0 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69  rameters to sqli
4a1f0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
4a200 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a  ) each return.**
4a210 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
4a220 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69  static preexisti
4a230 6e 67 20 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 20  ng mutex. {END} 
4a240 20 46 6f 75 72 20 73 74 61 74 69 63 20 6d 75 74   Four static mut
4a250 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64  exes are.** used
4a260 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20   by the current 
4a270 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74  version of SQLit
4a280 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69  e.  Future versi
4a290 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a  ons of SQLite.**
4a2a0 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f   may add additio
4a2b0 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78  nal static mutex
4a2c0 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65  es.  Static mute
4a2d0 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65  xes are for inte
4a2e0 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53  rnal.** use by S
4a2f0 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70  QLite only.  App
4a300 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75  lications that u
4a310 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65  se SQLite mutexe
4a320 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20  s should.** use 
4a330 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63  only the dynamic
4a340 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65   mutexes returne
4a350 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45  d by SQLITE_MUTE
4a360 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c  X_FAST or.** SQL
4a370 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
4a380 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  IVE..**.** {H170
4a390 31 38 7d 20 4e 6f 74 65 20 74 68 61 74 20 69 66  18} Note that if
4a3a0 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61   one of the dyna
4a3b0 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65  mic mutex parame
4a3c0 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54  ters (SQLITE_MUT
4a3d0 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51  EX_FAST.** or SQ
4a3e0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
4a3f0 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68  SIVE) is used th
4a400 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  en sqlite3_mutex
4a410 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75  _alloc().** retu
4a420 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20  rns a different 
4a430 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63  mutex on every c
4a440 61 6c 6c 2e 20 20 7b 48 31 37 30 33 34 7d 20 42  all.  {H17034} B
4a450 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69  ut for the stati
4a460 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73  c.** mutex types
4a470 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78  , the same mutex
4a480 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
4a490 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20  every call that 
4a4a0 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20  has.** the same 
4a4b0 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a  type number..**.
4a4c0 2a 2a 20 7b 48 31 37 30 31 39 7d 20 54 68 65 20  ** {H17019} The 
4a4d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72  sqlite3_mutex_fr
4a4e0 65 65 28 29 20 72 6f 75 74 69 6e 65 20 64 65 61  ee() routine dea
4a4f0 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69  llocates a previ
4a500 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  ously.** allocat
4a510 65 64 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  ed dynamic mutex
4a520 2e 20 7b 48 31 37 30 32 30 7d 20 53 51 4c 69 74  . {H17020} SQLit
4a530 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20  e is careful to 
4a540 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79  deallocate every
4a550 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  .** dynamic mute
4a560 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61  x that it alloca
4a570 74 65 73 2e 20 7b 41 31 37 30 32 31 7d 20 54 68  tes. {A17021} Th
4a580 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65  e dynamic mutexe
4a590 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 69 6e  s must not be in
4a5a0 0a 2a 2a 20 75 73 65 20 77 68 65 6e 20 74 68 65  .** use when the
4a5b0 79 20 61 72 65 20 64 65 61 6c 6c 6f 63 61 74 65  y are deallocate
4a5c0 64 2e 20 7b 41 31 37 30 32 32 7d 20 41 74 74 65  d. {A17022} Atte
4a5d0 6d 70 74 69 6e 67 20 74 6f 20 64 65 61 6c 6c 6f  mpting to deallo
4a5e0 63 61 74 65 20 61 20 73 74 61 74 69 63 0a 2a 2a  cate a static.**
4a5f0 20 6d 75 74 65 78 20 72 65 73 75 6c 74 73 20 69   mutex results i
4a600 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61  n undefined beha
4a610 76 69 6f 72 2e 20 7b 48 31 37 30 32 33 7d 20 53  vior. {H17023} S
4a620 51 4c 69 74 65 20 6e 65 76 65 72 20 64 65 61 6c  QLite never deal
4a630 6c 6f 63 61 74 65 73 0a 2a 2a 20 61 20 73 74 61  locates.** a sta
4a640 74 69 63 20 6d 75 74 65 78 2e 20 7b 45 4e 44 7d  tic mutex. {END}
4a650 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
4a660 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
4a670 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
4a680 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
4a690 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20  s attempt.** to 
4a6a0 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 7b  enter a mutex. {
4a6b0 48 31 37 30 32 34 7d 20 49 66 20 61 6e 6f 74 68  H17024} If anoth
4a6c0 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72  er thread is alr
4a6d0 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20  eady within the 
4a6e0 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65  mutex,.** sqlite
4a6f0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20  3_mutex_enter() 
4a700 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73  will block and s
4a710 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
4a720 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a  () will return.*
4a730 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 7b  * SQLITE_BUSY. {
4a740 48 31 37 30 32 35 7d 20 20 54 68 65 20 73 71 6c  H17025}  The sql
4a750 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
4a760 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
4a770 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a  ns [SQLITE_OK].*
4a780 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75  * upon successfu
4a790 6c 20 65 6e 74 72 79 2e 20 20 7b 48 31 37 30 32  l entry.  {H1702
4a7a0 36 7d 20 4d 75 74 65 78 65 73 20 63 72 65 61 74  6} Mutexes creat
4a7b0 65 64 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 49  ed using.** SQLI
4a7c0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
4a7d0 56 45 20 63 61 6e 20 62 65 20 65 6e 74 65 72 65  VE can be entere
4a7e0 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
4a7f0 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72   by the same thr
4a800 65 61 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 37 7d  ead..** {H17027}
4a810 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74   In such cases t
4a820 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73  he,.** mutex mus
4a830 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65  t be exited an e
4a840 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74  qual number of t
4a850 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74  imes before anot
4a860 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61  her thread.** ca
4a870 6e 20 65 6e 74 65 72 2e 20 20 7b 41 31 37 30 32  n enter.  {A1702
4a880 38 7d 20 49 66 20 74 68 65 20 73 61 6d 65 20 74  8} If the same t
4a890 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65  hread tries to e
4a8a0 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 0a 2a  nter any other.*
4a8b0 2a 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 20  * kind of mutex 
4a8c0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20  more than once, 
4a8d0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
4a8e0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 7b 48  undefined..** {H
4a8f0 31 37 30 32 39 7d 20 53 51 4c 69 74 65 20 77 69  17029} SQLite wi
4a900 6c 6c 20 6e 65 76 65 72 20 65 78 68 69 62 69 74  ll never exhibit
4a910 0a 2a 2a 20 73 75 63 68 20 62 65 68 61 76 69 6f  .** such behavio
4a920 72 20 69 6e 20 69 74 73 20 6f 77 6e 20 75 73 65  r in its own use
4a930 20 6f 66 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a   of mutexes..**.
4a940 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d 73 20  ** Some systems 
4a950 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 57 69  (for example, Wi
4a960 6e 64 6f 77 73 20 39 35 29 20 64 6f 20 6e 6f 74  ndows 95) do not
4a970 20 73 75 70 70 6f 72 74 20 74 68 65 20 6f 70 65   support the ope
4a980 72 61 74 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d  ration.** implem
4a990 65 6e 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  ented by sqlite3
4a9a0 5f 6d 75 74 65 78 5f 74 72 79 28 29 2e 20 20 4f  _mutex_try().  O
4a9b0 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d 73 2c  n those systems,
4a9c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
4a9d0 72 79 28 29 0a 2a 2a 20 77 69 6c 6c 20 61 6c 77  ry().** will alw
4a9e0 61 79 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ays return SQLIT
4a9f0 45 5f 42 55 53 59 2e 20 20 7b 48 31 37 30 33 30  E_BUSY.  {H17030
4aa00 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72  } The SQLite cor
4aa10 65 20 6f 6e 6c 79 20 65 76 65 72 20 75 73 65 73  e only ever uses
4aa20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
4aa30 78 5f 74 72 79 28 29 20 61 73 20 61 6e 20 6f 70  x_try() as an op
4aa40 74 69 6d 69 7a 61 74 69 6f 6e 20 73 6f 20 74 68  timization so th
4aa50 69 73 20 69 73 20 61 63 63 65 70 74 61 62 6c 65  is is acceptable
4aa60 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a   behavior..**.**
4aa70 20 7b 48 31 37 30 33 31 7d 20 54 68 65 20 73 71   {H17031} The sq
4aa80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
4aa90 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74  e() routine exit
4aaa0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77  s a mutex that w
4aab0 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  as.** previously
4aac0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20   entered by the 
4aad0 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 7b 41  same thread.  {A
4aae0 31 37 30 33 32 7d 20 54 68 65 20 62 65 68 61 76  17032} The behav
4aaf0 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69  ior.** is undefi
4ab00 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78  ned if the mutex
4ab10 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
4ab20 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65  y entered by the
4ab30 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 72 65  .** calling thre
4ab40 61 64 20 6f 72 20 69 73 20 6e 6f 74 20 63 75 72  ad or is not cur
4ab50 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
4ab60 2e 20 20 7b 48 31 37 30 33 33 7d 20 53 51 4c 69  .  {H17033} SQLi
4ab70 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72  te will.** never
4ab80 20 64 6f 20 65 69 74 68 65 72 2e 20 7b 45 4e 44   do either. {END
4ab90 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61  }.**.** If the a
4aba0 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
4abb0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
4abc0 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  , sqlite3_mutex_
4abd0 74 72 79 28 29 2c 20 6f 72 0a 2a 2a 20 73 71 6c  try(), or.** sql
4abe0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
4abf0 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  () is a NULL poi
4ac00 6e 74 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20 74  nter, then all t
4ac10 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a  hree routines.**
4ac20 20 62 65 68 61 76 65 20 61 73 20 6e 6f 2d 6f 70   behave as no-op
4ac30 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  s..**.** See als
4ac40 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  o: [sqlite3_mute
4ac50 78 5f 68 65 6c 64 28 29 5d 20 61 6e 64 20 5b 73  x_held()] and [s
4ac60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
4ac70 68 65 6c 64 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49  held()]..*/.SQLI
4ac80 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d  TE_API sqlite3_m
4ac90 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75  utex *sqlite3_mu
4aca0 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a  tex_alloc(int);.
4acb0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
4acc0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72  sqlite3_mutex_fr
4acd0 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ee(sqlite3_mutex
4ace0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
4acf0 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  oid sqlite3_mute
4ad00 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f  x_enter(sqlite3_
4ad10 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f  mutex*);.SQLITE_
4ad20 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
4ad30 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65  mutex_try(sqlite
4ad40 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54  3_mutex*);.SQLIT
4ad50 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
4ad60 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
4ad70 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
4ad80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4ad90 20 4d 75 74 65 78 20 4d 65 74 68 6f 64 73 20 4f   Mutex Methods O
4ada0 62 6a 65 63 74 20 7b 48 31 37 31 32 30 7d 20 3c  bject {H17120} <
4adb0 53 32 30 31 33 30 3e 0a 2a 2a 20 45 58 50 45 52  S20130>.** EXPER
4adc0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e  IMENTAL.**.** An
4add0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
4ade0 73 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69  s structure defi
4adf0 6e 65 73 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65  nes the low-leve
4ae00 6c 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 75 73  l routines.** us
4ae10 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  ed to allocate a
4ae20 6e 64 20 75 73 65 20 6d 75 74 65 78 65 73 2e 0a  nd use mutexes..
4ae30 2a 2a 0a 2a 2a 20 55 73 75 61 6c 6c 79 2c 20 74  **.** Usually, t
4ae40 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74 65 78  he default mutex
4ae50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
4ae60 20 70 72 6f 76 69 64 65 64 20 62 79 20 53 51 4c   provided by SQL
4ae70 69 74 65 20 61 72 65 0a 2a 2a 20 73 75 66 66 69  ite are.** suffi
4ae80 63 69 65 6e 74 2c 20 68 6f 77 65 76 65 72 20 74  cient, however t
4ae90 68 65 20 75 73 65 72 20 68 61 73 20 74 68 65 20  he user has the 
4aea0 6f 70 74 69 6f 6e 20 6f 66 20 73 75 62 73 74 69  option of substi
4aeb0 74 75 74 69 6e 67 20 61 20 63 75 73 74 6f 6d 0a  tuting a custom.
4aec0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
4aed0 6e 20 66 6f 72 20 73 70 65 63 69 61 6c 69 7a 65  n for specialize
4aee0 64 20 64 65 70 6c 6f 79 6d 65 6e 74 73 20 6f 72  d deployments or
4aef0 20 73 79 73 74 65 6d 73 20 66 6f 72 20 77 68 69   systems for whi
4af00 63 68 20 53 51 4c 69 74 65 0a 2a 2a 20 64 6f 65  ch SQLite.** doe
4af10 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 20  s not provide a 
4af20 73 75 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  suitable impleme
4af30 6e 74 61 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73  ntation. In this
4af40 20 63 61 73 65 2c 20 74 68 65 20 75 73 65 72 0a   case, the user.
4af50 2a 2a 20 63 72 65 61 74 65 73 20 61 6e 64 20 70  ** creates and p
4af60 6f 70 75 6c 61 74 65 73 20 61 6e 20 69 6e 73 74  opulates an inst
4af70 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
4af80 75 63 74 75 72 65 20 74 6f 20 70 61 73 73 0a 2a  ucture to pass.*
4af90 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e  * to sqlite3_con
4afa0 66 69 67 28 29 20 61 6c 6f 6e 67 20 77 69 74 68  fig() along with
4afb0 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e   the [SQLITE_CON
4afc0 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f  FIG_MUTEX] optio
4afd0 6e 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c  n..** Additional
4afe0 6c 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  ly, an instance 
4aff0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
4b000 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73  e can be used as
4b010 20 61 6e 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61   an.** output va
4b020 72 69 61 62 6c 65 20 77 68 65 6e 20 71 75 65 72  riable when quer
4b030 79 69 6e 67 20 74 68 65 20 73 79 73 74 65 6d 20  ying the system 
4b040 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
4b050 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65  mutex.** impleme
4b060 6e 74 61 74 69 6f 6e 2c 20 75 73 69 6e 67 20 74  ntation, using t
4b070 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  he [SQLITE_CONFI
4b080 47 5f 47 45 54 4d 55 54 45 58 5d 20 6f 70 74 69  G_GETMUTEX] opti
4b090 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d  on..**.** The xM
4b0a0 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 20  utexInit method 
4b0b0 64 65 66 69 6e 65 64 20 62 79 20 74 68 69 73 20  defined by this 
4b0c0 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 76  structure is inv
4b0d0 6f 6b 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 20  oked as.** part 
4b0e0 6f 66 20 73 79 73 74 65 6d 20 69 6e 69 74 69 61  of system initia
4b0f0 6c 69 7a 61 74 69 6f 6e 20 62 79 20 74 68 65 20  lization by the 
4b100 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
4b110 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  ze() function..*
4b120 2a 20 7b 48 31 37 30 30 31 7d 20 54 68 65 20 78  * {H17001} The x
4b130 4d 75 74 65 78 49 6e 69 74 20 72 6f 75 74 69 6e  MutexInit routin
4b140 65 20 73 68 61 6c 6c 20 62 65 20 63 61 6c 6c 65  e shall be calle
4b150 64 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 63 65  d by SQLite once
4b160 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 65 66 66   for each.** eff
4b170 65 63 74 69 76 65 20 63 61 6c 6c 20 74 6f 20 5b  ective call to [
4b180 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
4b190 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ze()]..**.** The
4b1a0 20 78 4d 75 74 65 78 45 6e 64 20 6d 65 74 68 6f   xMutexEnd metho
4b1b0 64 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 69  d defined by thi
4b1c0 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69  s structure is i
4b1d0 6e 76 6f 6b 65 64 20 61 73 0a 2a 2a 20 70 61 72  nvoked as.** par
4b1e0 74 20 6f 66 20 73 79 73 74 65 6d 20 73 68 75 74  t of system shut
4b1f0 64 6f 77 6e 20 62 79 20 74 68 65 20 73 71 6c 69  down by the sqli
4b200 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 66  te3_shutdown() f
4b210 75 6e 63 74 69 6f 6e 2e 20 54 68 65 0a 2a 2a 20  unction. The.** 
4b220 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
4b230 66 20 74 68 69 73 20 6d 65 74 68 6f 64 20 69 73  f this method is
4b240 20 65 78 70 65 63 74 65 64 20 74 6f 20 72 65 6c   expected to rel
4b250 65 61 73 65 20 61 6c 6c 20 6f 75 74 73 74 61 6e  ease all outstan
4b260 64 69 6e 67 0a 2a 2a 20 72 65 73 6f 75 72 63 65  ding.** resource
4b270 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68  s obtained by th
4b280 65 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 20  e mutex methods 
4b290 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
4b2a0 65 73 70 65 63 69 61 6c 6c 79 0a 2a 2a 20 74 68  especially.** th
4b2b0 6f 73 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20  ose obtained by 
4b2c0 74 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20 6d  the xMutexInit m
4b2d0 65 74 68 6f 64 2e 20 7b 48 31 37 30 30 33 7d 20  ethod. {H17003} 
4b2e0 54 68 65 20 78 4d 75 74 65 78 45 6e 64 28 29 0a  The xMutexEnd().
4b2f0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68 61  ** interface sha
4b300 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 6f 6e  ll be invoked on
4b310 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c  ce for each call
4b320 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 68 75   to [sqlite3_shu
4b330 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  tdown()]..**.** 
4b340 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 73 65  The remaining se
4b350 76 65 6e 20 6d 65 74 68 6f 64 73 20 64 65 66 69  ven methods defi
4b360 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75  ned by this stru
4b370 63 74 75 72 65 20 28 78 4d 75 74 65 78 41 6c 6c  cture (xMutexAll
4b380 6f 63 2c 0a 2a 2a 20 78 4d 75 74 65 78 46 72 65  oc,.** xMutexFre
4b390 65 2c 20 78 4d 75 74 65 78 45 6e 74 65 72 2c 20  e, xMutexEnter, 
4b3a0 78 4d 75 74 65 78 54 72 79 2c 20 78 4d 75 74 65  xMutexTry, xMute
4b3b0 78 4c 65 61 76 65 2c 20 78 4d 75 74 65 78 48 65  xLeave, xMutexHe
4b3c0 6c 64 20 61 6e 64 0a 2a 2a 20 78 4d 75 74 65 78  ld and.** xMutex
4b3d0 4e 6f 74 68 65 6c 64 29 20 69 6d 70 6c 65 6d 65  Notheld) impleme
4b3e0 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  nt the following
4b3f0 20 69 6e 74 65 72 66 61 63 65 73 20 28 72 65 73   interfaces (res
4b400 70 65 63 74 69 76 65 6c 79 29 3a 0a 2a 2a 0a 2a  pectively):.**.*
4b410 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e  * <ul>.**   <li>
4b420 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78    [sqlite3_mutex
4b430 5f 61 6c 6c 6f 63 28 29 5d 20 3c 2f 6c 69 3e 0a  _alloc()] </li>.
4b440 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69  **   <li>  [sqli
4b450 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29  te3_mutex_free()
4b460 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69  ] </li>.**   <li
4b470 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  >  [sqlite3_mute
4b480 78 5f 65 6e 74 65 72 28 29 5d 20 3c 2f 6c 69 3e  x_enter()] </li>
4b490 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c  .**   <li>  [sql
4b4a0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
4b4b0 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69  ] </li>.**   <li
4b4c0 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  >  [sqlite3_mute
4b4d0 78 5f 6c 65 61 76 65 28 29 5d 20 3c 2f 6c 69 3e  x_leave()] </li>
4b4e0 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c  .**   <li>  [sql
4b4f0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
4b500 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c  )] </li>.**   <l
4b510 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  i>  [sqlite3_mut
4b520 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 20 3c 2f  ex_notheld()] </
4b530 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  li>.** </ul>.**.
4b540 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66  ** The only diff
4b550 65 72 65 6e 63 65 20 69 73 20 74 68 61 74 20 74  erence is that t
4b560 68 65 20 70 75 62 6c 69 63 20 73 71 6c 69 74 65  he public sqlite
4b570 33 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 73 20  3_XXX functions 
4b580 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20 61 62  enumerated.** ab
4b590 6f 76 65 20 73 69 6c 65 6e 74 6c 79 20 69 67 6e  ove silently ign
4b5a0 6f 72 65 20 61 6e 79 20 69 6e 76 6f 63 61 74 69  ore any invocati
4b5b0 6f 6e 73 20 74 68 61 74 20 70 61 73 73 20 61 20  ons that pass a 
4b5c0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73  NULL pointer ins
4b5d0 74 65 61 64 0a 2a 2a 20 6f 66 20 61 20 76 61 6c  tead.** of a val
4b5e0 69 64 20 6d 75 74 65 78 20 68 61 6e 64 6c 65 2e  id mutex handle.
4b5f0 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
4b600 69 6f 6e 73 20 6f 66 20 74 68 65 20 6d 65 74 68  ions of the meth
4b610 6f 64 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 62  ods defined.** b
4b620 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
4b630 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72 65   are not require
4b640 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 69 73  d to handle this
4b650 20 63 61 73 65 2c 20 74 68 65 20 72 65 73 75 6c   case, the resul
4b660 74 73 0a 2a 2a 20 6f 66 20 70 61 73 73 69 6e 67  ts.** of passing
4b670 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
4b680 69 6e 73 74 65 61 64 20 6f 66 20 61 20 76 61 6c  instead of a val
4b690 69 64 20 6d 75 74 65 78 20 68 61 6e 64 6c 65 20  id mutex handle 
4b6a0 61 72 65 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a  are undefined.**
4b6b0 20 28 69 2e 65 2e 20 69 74 20 69 73 20 61 63 63   (i.e. it is acc
4b6c0 65 70 74 61 62 6c 65 20 74 6f 20 70 72 6f 76 69  eptable to provi
4b6d0 64 65 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  de an implementa
4b6e0 74 69 6f 6e 20 74 68 61 74 20 73 65 67 66 61 75  tion that segfau
4b6f0 6c 74 73 20 69 66 0a 2a 2a 20 69 74 20 69 73 20  lts if.** it is 
4b700 70 61 73 73 65 64 20 61 20 4e 55 4c 4c 20 70 6f  passed a NULL po
4b710 69 6e 74 65 72 29 2e 0a 2a 2f 0a 74 79 70 65 64  inter)..*/.typed
4b720 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
4b730 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
4b740 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
4b750 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71  thods;.struct sq
4b760 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
4b770 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d  ods {.  int (*xM
4b780 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b  utexInit)(void);
4b790 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45  .  int (*xMutexE
4b7a0 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c  nd)(void);.  sql
4b7b0 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d  ite3_mutex *(*xM
4b7c0 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b  utexAlloc)(int);
4b7d0 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78  .  void (*xMutex
4b7e0 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  Free)(sqlite3_mu
4b7f0 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28  tex *);.  void (
4b800 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71  *xMutexEnter)(sq
4b810 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a  lite3_mutex *);.
4b820 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72    int (*xMutexTr
4b830 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  y)(sqlite3_mutex
4b840 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d   *);.  void (*xM
4b850 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74  utexLeave)(sqlit
4b860 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69  e3_mutex *);.  i
4b870 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29  nt (*xMutexHeld)
4b880 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
4b890 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65  );.  int (*xMute
4b8a0 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65  xNotheld)(sqlite
4b8b0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a  3_mutex *);.};..
4b8c0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
4b8d0 4d 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69  Mutex Verificati
4b8e0 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 37  on Routines {H17
4b8f0 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c 53  080} <S20130> <S
4b900 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  30800>.**.** The
4b910 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
4b920 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65  eld() and sqlite
4b930 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
4b940 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72  ) routines.** ar
4b950 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  e intended for u
4b960 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
4b970 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 7b  () statements. {
4b980 48 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c 69  H17081} The SQLi
4b990 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72  te core.** never
4b9a0 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74   uses these rout
4b9b0 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 69  ines except insi
4b9c0 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 61  de an assert() a
4b9d0 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  nd applications.
4b9e0 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 74  ** are advised t
4b9f0 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61  o follow the lea
4ba00 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 20  d of the core.  
4ba10 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f 72  {H17082} The cor
4ba20 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64  e only.** provid
4ba30 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  es implementatio
4ba40 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f 75  ns for these rou
4ba50 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 73  tines when it is
4ba60 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74   compiled.** wit
4ba70 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 42  h the SQLITE_DEB
4ba80 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30 38  UG flag.  {A1708
4ba90 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 65  7} External mute
4baa0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
4bab0 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 65  s.** are only re
4bac0 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64  quired to provid
4bad0 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  e these routines
4bae0 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47   if SQLITE_DEBUG
4baf0 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61   is.** defined a
4bb00 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20  nd if NDEBUG is 
4bb10 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  not defined..**.
4bb20 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65 73  ** {H17083} Thes
4bb30 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c  e routines shoul
4bb40 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66  d return true if
4bb50 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68   the mutex in th
4bb60 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  eir argument.** 
4bb70 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68  is held or not h
4bb80 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c  eld, respectivel
4bb90 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  y, by the callin
4bba0 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  g thread..**.** 
4bbb0 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d 70  {X17084} The imp
4bbc0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e  lementation is n
4bbd0 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70  ot required to p
4bbe0 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e 73  rovided versions
4bbf0 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75   of these.** rou
4bc00 74 69 6e 65 73 20 74 68 61 74 20 61 63 74 75 61  tines that actua
4bc10 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68 65  lly work. If the
4bc20 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
4bc30 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65  does not provide
4bc40 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72 73   working.** vers
4bc50 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72 6f  ions of these ro
4bc60 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75 6c  utines, it shoul
4bc70 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76 69  d at least provi
4bc80 64 65 20 73 74 75 62 73 20 74 68 61 74 20 61 6c  de stubs that al
4bc90 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20 74  ways.** return t
4bca0 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65 20  rue so that one 
4bcb0 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70 75  does not get spu
4bcc0 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e 20  rious assertion 
4bcd0 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20  failures..**.** 
4bce0 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68 65 20  {H17085} If the 
4bcf0 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
4bd00 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
4bd10 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
4bd20 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72  er then.** the r
4bd30 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65  outine should re
4bd40 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20 54  turn 1.  {END} T
4bd50 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74 65  his seems counte
4bd60 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e 63  r-intuitive sinc
4bd70 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68 65  e.** clearly the
4bd80 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62 65   mutex cannot be
4bd90 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65 73   held if it does
4bda0 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75 74   not exist.  But
4bdb0 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61 73   the.** the reas
4bdc0 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f 65  on the mutex doe
4bdd0 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 62  s not exist is b
4bde0 65 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 64  ecause the build
4bdf0 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67   is not.** using
4be00 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 77   mutexes.  And w
4be10 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68  e do not want th
4be20 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 61  e assert() conta
4be30 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61 6c  ining the.** cal
4be40 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  l to sqlite3_mut
4be50 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61 69  ex_held() to fai
4be60 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  l, so a non-zero
4be70 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74 68   return is.** th
4be80 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 68  e appropriate th
4be90 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31 37  ing to do.  {H17
4bea0 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65 33  086} The sqlite3
4beb0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
4bec0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68  .** interface sh
4bed0 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72 6e  ould also return
4bee0 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61 20   1 when given a 
4bef0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
4bf00 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
4bf10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
4bf20 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
4bf30 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
4bf40 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
4bf50 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  _notheld(sqlite3
4bf60 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _mutex*);../*.**
4bf70 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
4bf80 20 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d 20   Types {H17001} 
4bf90 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <H17000>.**.** T
4bfa0 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  he [sqlite3_mute
4bfb0 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72  x_alloc()] inter
4bfc0 66 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e  face takes a sin
4bfd0 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  gle argument.** 
4bfe0 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20  which is one of 
4bff0 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  these integer co
4c000 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  nstants..**.** T
4c010 68 65 20 73 65 74 20 6f 66 20 73 74 61 74 69 63  he set of static
4c020 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68 61   mutexes may cha
4c030 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c  nge from one SQL
4c040 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74  ite release to t
4c050 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70  he.** next.  App
4c060 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f  lications that o
4c070 76 65 72 72 69 64 65 20 74 68 65 20 62 75 69 6c  verride the buil
4c080 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63  t-in mutex logic
4c090 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70   must be.** prep
4c0a0 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64  ared to accommod
4c0b0 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  ate additional s
4c0c0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a  tatic mutexes..*
4c0d0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
4c0e0 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20  _MUTEX_FAST     
4c0f0 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e          0.#defin
4c100 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  e SQLITE_MUTEX_R
4c110 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20  ECURSIVE        
4c120 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
4c130 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
4c140 53 54 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e  STER    2.#defin
4c150 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
4c160 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20  TATIC_MEM       
4c170 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61  3  /* sqlite3_ma
4c180 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e  lloc() */.#defin
4c190 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
4c1a0 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20  TATIC_MEM2      
4c1b0 34 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72 65  4  /* sqlite3_re
4c1c0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 2a  lease_memory() *
4c1d0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
4c1e0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52  _MUTEX_STATIC_PR
4c1f0 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20 73 71  NG      5  /* sq
4c200 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29 20 2a  lite3_random() *
4c210 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
4c220 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
4c230 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20 6c 72  U       6  /* lr
4c240 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 23  u page list */.#
4c250 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55  define SQLITE_MU
4c260 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20  TEX_STATIC_LRU2 
4c270 20 20 20 20 20 37 20 20 2f 2a 20 6c 72 75 20 70       7  /* lru p
4c280 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a  age list */../*.
4c290 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 74  ** CAPI3REF: Ret
4c2a0 72 69 65 76 65 20 74 68 65 20 6d 75 74 65 78 20  rieve the mutex 
4c2b0 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63  for a database c
4c2c0 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 37 30 30  onnection {H1700
4c2d0 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a  2} <H17000>.**.*
4c2e0 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
4c2f0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
4c300 65 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  er the [sqlite3_
4c310 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 20 74 68  mutex] object th
4c320 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65  at .** serialize
4c330 73 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  s access to the 
4c340 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
4c350 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74  tion] given in t
4c360 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77  he argument.** w
4c370 68 65 6e 20 74 68 65 20 5b 74 68 72 65 61 64 69  hen the [threadi
4c380 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 65 72 69  ng mode] is Seri
4c390 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 74 68  alized..** If th
4c3a0 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  e [threading mod
4c3b0 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d 74 68 72  e] is Single-thr
4c3c0 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68 72  ead or Multi-thr
4c3d0 65 61 64 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a  ead then this.**
4c3e0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
4c3f0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e   a NULL pointer.
4c400 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
4c410 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71  qlite3_mutex *sq
4c420 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73  lite3_db_mutex(s
4c430 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  qlite3*);../*.**
4c440 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c   CAPI3REF: Low-L
4c450 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20  evel Control Of 
4c460 44 61 74 61 62 61 73 65 20 46 69 6c 65 73 20 7b  Database Files {
4c470 48 31 31 33 30 30 7d 20 3c 53 33 30 38 30 30 3e  H11300} <S30800>
4c480 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 31 7d 20  .**.** {H11301} 
4c490 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  The [sqlite3_fil
4c4a0 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74  e_control()] int
4c4b0 65 72 66 61 63 65 20 6d 61 6b 65 73 20 61 20 64  erface makes a d
4c4c0 69 72 65 63 74 20 63 61 6c 6c 20 74 6f 20 74 68  irect call to th
4c4d0 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f  e.** xFileContro
4c4e0 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 65  l method for the
4c4f0 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
4c500 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 73 73  hods] object ass
4c510 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20  ociated.** with 
4c520 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74  a particular dat
4c530 61 62 61 73 65 20 69 64 65 6e 74 69 66 69 65 64  abase identified
4c540 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61   by the second a
4c550 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31 33 30 32  rgument. {H11302
4c560 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66  } The.** name of
4c570 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
4c580 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e   the name assign
4c590 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ed to the databa
4c5a0 73 65 20 62 79 20 74 68 65 0a 2a 2a 20 3c 61 20  se by the.** <a 
4c5b0 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74 74 61 63  href="lang_attac
4c5c0 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43 48 3c 2f  h.html">ATTACH</
4c5d0 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74  a> SQL command t
4c5e0 68 61 74 20 6f 70 65 6e 65 64 20 74 68 65 0a 2a  hat opened the.*
4c5f0 2a 20 64 61 74 61 62 61 73 65 2e 20 7b 48 31 31  * database. {H11
4c600 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72 6f 6c 20  303} To control 
4c610 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
4c620 65 20 66 69 6c 65 2c 20 75 73 65 20 74 68 65 20  e file, use the 
4c630 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a 20 6f  name "main".** o
4c640 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  r a NULL pointer
4c650 2e 20 7b 48 31 31 33 30 34 7d 20 54 68 65 20 74  . {H11304} The t
4c660 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20  hird and fourth 
4c670 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68  parameters to th
4c680 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 72  is routine.** ar
4c690 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74 6c  e passed directl
4c6a0 79 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  y through to the
4c6b0 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72   second and thir
4c6c0 64 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 0a  d parameters of.
4c6d0 2a 2a 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74  ** the xFileCont
4c6e0 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20 7b 48 31  rol method.  {H1
4c6f0 31 33 30 35 7d 20 54 68 65 20 72 65 74 75 72 6e  1305} The return
4c700 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 78 46   value of the xF
4c710 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d 65  ileControl.** me
4c720 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 74 68 65  thod becomes the
4c730 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
4c740 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
4c750 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d 20 49 66  *.** {H11306} If
4c760 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
4c770 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d 65 29 20  meter (zDbName) 
4c780 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74  does not match t
4c790 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79 0a 2a  he name of any.*
4c7a0 2a 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20  * open database 
4c7b0 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 54  file, then SQLIT
4c7c0 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72  E_ERROR is retur
4c7d0 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d 20 54 68  ned. {H11307} Th
4c7e0 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65  is error.** code
4c7f0 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72   is not remember
4c800 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20  ed and will not 
4c810 62 65 20 72 65 63 61 6c 6c 65 64 20 62 79 20 5b  be recalled by [
4c820 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
4c830 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65  )].** or [sqlite
4c840 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 7b 41 31  3_errmsg()]. {A1
4c850 31 33 30 38 7d 20 54 68 65 20 75 6e 64 65 72 6c  1308} The underl
4c860 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e 74 72 6f  ying xFileContro
4c870 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68 74 0a 2a  l method might.*
4c880 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 53 51  * also return SQ
4c890 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 7b 41 31  LITE_ERROR.  {A1
4c8a0 31 33 30 39 7d 20 54 68 65 72 65 20 69 73 20 6e  1309} There is n
4c8b0 6f 20 77 61 79 20 74 6f 20 64 69 73 74 69 6e 67  o way to disting
4c8c0 75 69 73 68 20 62 65 74 77 65 65 6e 0a 2a 2a 20  uish between.** 
4c8d0 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a 44 62  an incorrect zDb
4c8e0 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 51 4c 49  Name and an SQLI
4c8f0 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72 6e 20  TE_ERROR return 
4c900 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79  from the underly
4c910 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74  ing.** xFileCont
4c920 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b 45 4e 44  rol method. {END
4c930 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  }.**.** See also
4c940 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  : [SQLITE_FCNTL_
4c950 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a 53 51  LOCKSTATE].*/.SQ
4c960 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
4c970 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
4c980 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73  l(sqlite3*, cons
4c990 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c  t char *zDbName,
4c9a0 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 2a 29 3b   int op, void*);
4c9b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
4c9c0 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 72 66  : Testing Interf
4c9d0 61 63 65 20 7b 48 31 31 34 30 30 7d 20 3c 53 33  ace {H11400} <S3
4c9e0 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0800>.**.** The 
4c9f0 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
4ca00 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 65  trol() interface
4ca10 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64   is used to read
4ca20 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a   out internal.**
4ca30 20 73 74 61 74 65 20 6f 66 20 53 51 4c 69 74 65   state of SQLite
4ca40 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63 74 20 66   and to inject f
4ca50 61 75 6c 74 73 20 69 6e 74 6f 20 53 51 4c 69 74  aults into SQLit
4ca60 65 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a  e for testing.**
4ca70 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 65 20   purposes.  The 
4ca80 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
4ca90 69 73 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20  is an operation 
4caa0 63 6f 64 65 20 74 68 61 74 20 64 65 74 65 72 6d  code that determ
4cab0 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  ines.** the numb
4cac0 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 6e 64  er, meaning, and
4cad0 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 61 6c   operation of al
4cae0 6c 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72  l subsequent par
4caf0 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54  ameters..**.** T
4cb00 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73  his interface is
4cb10 20 6e 6f 74 20 66 6f 72 20 75 73 65 20 62 79 20   not for use by 
4cb20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 49  applications.  I
4cb30 74 20 65 78 69 73 74 73 20 73 6f 6c 65 6c 79 0a  t exists solely.
4cb40 2a 2a 20 66 6f 72 20 76 65 72 69 66 79 69 6e 67  ** for verifying
4cb50 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65   the correct ope
4cb60 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51  ration of the SQ
4cb70 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 44  Lite library.  D
4cb80 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 68  epending.** on h
4cb90 6f 77 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  ow the SQLite li
4cba0 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65  brary is compile
4cbb0 64 2c 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  d, this interfac
4cbc0 65 20 6d 69 67 68 74 20 6e 6f 74 20 65 78 69 73  e might not exis
4cbd0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74  t..**.** The det
4cbe0 61 69 6c 73 20 6f 66 20 74 68 65 20 6f 70 65 72  ails of the oper
4cbf0 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20 74 68 65  ation codes, the
4cc00 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20 74 68 65  ir meanings, the
4cc10 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74   parameters.** t
4cc20 68 65 79 20 74 61 6b 65 2c 20 61 6e 64 20 77 68  hey take, and wh
4cc30 61 74 20 74 68 65 79 20 64 6f 20 61 72 65 20 61  at they do are a
4cc40 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  ll subject to ch
4cc50 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74  ange without not
4cc60 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d  ice..** Unlike m
4cc70 6f 73 74 20 6f 66 20 74 68 65 20 53 51 4c 69 74  ost of the SQLit
4cc80 65 20 41 50 49 2c 20 74 68 69 73 20 66 75 6e 63  e API, this func
4cc90 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 75 61 72  tion is not guar
4cca0 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 6f 70 65  anteed to.** ope
4ccb0 72 61 74 65 20 63 6f 6e 73 69 73 74 65 6e 74 6c  rate consistentl
4ccc0 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61  y from one relea
4ccd0 73 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a  se to the next..
4cce0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
4ccf0 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  t sqlite3_test_c
4cd00 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e  ontrol(int op, .
4cd10 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ..);../*.** CAPI
4cd20 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e  3REF: Testing In
4cd30 74 65 72 66 61 63 65 20 4f 70 65 72 61 74 69 6f  terface Operatio
4cd40 6e 20 43 6f 64 65 73 20 7b 48 31 31 34 31 30 7d  n Codes {H11410}
4cd50 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a 2a 20   <H11400>.**.** 
4cd60 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
4cd70 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 6f 70  are the valid op
4cd80 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 70 61 72  eration code par
4cd90 61 6d 65 74 65 72 73 20 75 73 65 64 0a 2a 2a 20  ameters used.** 
4cda0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
4cdb0 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65  ument to [sqlite
4cdc0 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29  3_test_control()
4cdd0 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70  ]..**.** These p
4cde0 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 74 68  arameters and th
4cdf0 65 69 72 20 6d 65 61 6e 69 6e 67 73 20 61 72 65  eir meanings are
4ce00 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
4ce10 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20 6e 6f  ge.** without no
4ce20 74 69 63 65 2e 20 20 54 68 65 73 65 20 76 61 6c  tice.  These val
4ce30 75 65 73 20 61 72 65 20 66 6f 72 20 74 65 73 74  ues are for test
4ce40 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c  ing purposes onl
4ce50 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f  y..** Applicatio
4ce60 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73  ns should not us
4ce70 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 70  e any of these p
4ce80 61 72 61 6d 65 74 65 72 73 20 6f 72 20 74 68 65  arameters or the
4ce90 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74 65 73  .** [sqlite3_tes
4cea0 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74  t_control()] int
4ceb0 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 69  erface..*/.#defi
4cec0 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
4ced0 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20 20 20  RL_PRNG_SAVE    
4cee0 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64              5.#d
4cef0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
4cf00 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f  TCTRL_PRNG_RESTO
4cf10 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20 36  RE             6
4cf20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
4cf30 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45  TESTCTRL_PRNG_RE
4cf40 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20 20  SET             
4cf50 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    7.#define SQLI
4cf60 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49 54 56  TE_TESTCTRL_BITV
4cf70 45 43 5f 54 45 53 54 20 20 20 20 20 20 20 20 20  EC_TEST         
4cf80 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 53       8.#define S
4cf90 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 46  QLITE_TESTCTRL_F
4cfa0 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20 20 20 20  AULT_INSTALL    
4cfb0 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e          9.#defin
4cfc0 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
4cfd0 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f  L_BENIGN_MALLOC_
4cfe0 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a 0a 2f 2a  HOOKS     10../*
4cff0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51  .** CAPI3REF: SQ
4d000 4c 69 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61  Lite Runtime Sta
4d010 74 75 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36  tus {H17200} <S6
4d020 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0200>.** EXPERIM
4d030 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ENTAL.**.** This
4d040 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73   interface is us
4d050 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72  ed to retrieve r
4d060 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e  untime status in
4d070 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f  formation.** abo
4d080 75 74 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e  ut the preforman
4d090 63 65 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e  ce of SQLite, an
4d0a0 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20  d optionally to 
4d0b0 72 65 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a  reset various.**
4d0c0 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73   highwater marks
4d0d0 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
4d0e0 75 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65  ument is an inte
4d0f0 67 65 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20  ger code for.** 
4d100 74 68 65 20 73 70 65 63 69 66 69 63 20 70 61 72  the specific par
4d110 61 6d 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72  ameter to measur
4d120 65 2e 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69  e.  Recognized i
4d130 6e 74 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20  nteger codes.** 
4d140 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  are of the form 
4d150 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  [SQLITE_STATUS_M
4d160 45 4d 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c  EMORY_USED | SQL
4d170 49 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e  ITE_STATUS_...].
4d180 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20  .** The current 
4d190 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72  value of the par
4d1a0 61 6d 65 74 65 72 20 69 73 20 72 65 74 75 72 6e  ameter is return
4d1b0 65 64 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e  ed into *pCurren
4d1c0 74 2e 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73  t..** The highes
4d1d0 74 20 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65  t recorded value
4d1e0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
4d1f0 2a 70 48 69 67 68 77 61 74 65 72 2e 20 20 49 66  *pHighwater.  If
4d200 20 74 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61   the.** resetFla
4d210 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  g is true, then 
4d220 74 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f  the highest reco
4d230 72 64 20 76 61 6c 75 65 20 69 73 20 72 65 73 65  rd value is rese
4d240 74 20 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67  t after.** *pHig
4d250 68 77 61 74 65 72 20 69 73 20 77 72 69 74 74 65  hwater is writte
4d260 6e 2e 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65  n. Some paramete
4d270 72 73 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64  rs do not record
4d280 20 74 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20   the highest.** 
4d290 76 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73  value.  For thos
4d2a0 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  e parameters.** 
4d2b0 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74  nothing is writt
4d2c0 65 6e 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61  en into *pHighwa
4d2d0 74 65 72 20 61 6e 64 20 74 68 65 20 72 65 73 65  ter and the rese
4d2e0 74 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64  tFlag is ignored
4d2f0 2e 0a 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d  ..** Other param
4d300 65 74 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c  eters record onl
4d310 79 20 74 68 65 20 68 69 67 68 77 61 74 65 72 20  y the highwater 
4d320 6d 61 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65  mark and not the
4d330 20 63 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75   current.** valu
4d340 65 2e 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61  e.  For these la
4d350 74 74 65 72 20 70 61 72 61 6d 65 74 65 72 73 20  tter parameters 
4d360 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74  nothing is writt
4d370 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e  en into *pCurren
4d380 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  t..**.** This ro
4d390 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51  utine returns SQ
4d3a0 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
4d3b0 73 73 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72  ss and a non-zer
4d3c0 6f 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65  o.** [error code
4d3d0 5d 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a  ] on failure..**
4d3e0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
4d3f0 20 69 73 20 74 68 72 65 61 64 73 61 66 65 20 62   is threadsafe b
4d400 75 74 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63  ut is not atomic
4d410 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
4d420 63 61 6e 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68  can.** called wh
4d430 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 64  ile other thread
4d440 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 68  s are running th
4d450 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 72  e same or differ
4d460 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e  ent SQLite.** in
4d470 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76  terfaces.  Howev
4d480 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72 65  er the values re
4d490 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72 72  turned in *pCurr
4d4a0 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 67  ent and.** *pHig
4d4b0 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20 74  hwater reflect t
4d4c0 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51 4c  he status of SQL
4d4d0 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e 74  ite at different
4d4e0 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65 0a   points in time.
4d4f0 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f 73  ** and it is pos
4d500 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68  sible that anoth
4d510 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74 20  er thread might 
4d520 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61 6d  change the param
4d530 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77 65  eter.** in betwe
4d540 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68 65  en the times whe
4d550 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 20  n *pCurrent and 
4d560 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65 20  *pHighwater are 
4d570 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53  written..**.** S
4d580 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65  ee also: [sqlite
4d590 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a 2a  3_db_status()].*
4d5a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
4d5b0 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
4d5c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61   int sqlite3_sta
4d5d0 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20  tus(int op, int 
4d5e0 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a  *pCurrent, int *
4d5f0 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20  pHighwater, int 
4d600 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a  resetFlag);.../*
4d610 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74  .** CAPI3REF: St
4d620 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 20  atus Parameters 
4d630 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30 30  {H17250} <H17200
4d640 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
4d650 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e  L.**.** These in
4d660 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20  teger constants 
4d670 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f 75  designate variou
4d680 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74 75  s run-time statu
4d690 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  s parameters.** 
4d6a0 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74 75  that can be retu
4d6b0 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  rned by [sqlite3
4d6c0 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a  _status()]..**.*
4d6d0 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51  * <dl>.** <dt>SQ
4d6e0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
4d6f0 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20  RY_USED</dt>.** 
4d700 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
4d710 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65 6e  er is the curren
4d720 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  t amount of memo
4d730 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a 2a  ry checked out.*
4d740 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
4d750 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68  _malloc()], eith
4d760 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69  er directly or i
4d770 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 0a  ndirectly.  The.
4d780 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75 64  ** figure includ
4d790 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74 6f  es calls made to
4d7a0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
4d7b0 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c 69  ()] by the appli
4d7c0 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e  cation.** and in
4d7d0 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73  ternal memory us
4d7e0 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69 74  age by the SQLit
4d7f0 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72 61  e library.  Scra
4d800 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f  tch memory.** co
4d810 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c  ntrolled by [SQL
4d820 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
4d830 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61 72  CH] and auxiliar
4d840 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a 20  y page-cache.** 
4d850 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65  memory controlle
4d860 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  d by [SQLITE_CON
4d870 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20 69  FIG_PAGECACHE] i
4d880 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69  s not included i
4d890 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d 65  n.** this parame
4d8a0 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e 74  ter.  The amount
4d8b0 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
4d8c0 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c 6f   sum of the allo
4d8d0 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20  cation.** sizes 
4d8e0 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20 74  as reported by t
4d8f0 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64 20  he xSize method 
4d900 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  in [sqlite3_mem_
4d910 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a  methods].</dd>.*
4d920 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
4d930 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49  STATUS_MALLOC_SI
4d940 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ZE</dt>.** <dd>T
4d950 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
4d960 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73  cords the larges
4d970 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  t memory allocat
4d980 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68  ion request.** h
4d990 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 65  anded to [sqlite
4d9a0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b  3_malloc()] or [
4d9b0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
4d9c0 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20  )] (or their.** 
4d9d0 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61 6c  internal equival
4d9e0 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65  ents).  Only the
4d9f0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
4da00 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68  in the.** *pHigh
4da10 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20  water parameter 
4da20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74  to [sqlite3_stat
4da30 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65  us()] is of inte
4da40 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76  rest.  .** The v
4da50 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74  alue written int
4da60 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20  o the *pCurrent 
4da70 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64  parameter is und
4da80 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a  efined.</dd>.**.
4da90 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
4daa0 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
4dab0 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  SED</dt>.** <dd>
4dac0 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
4dad0 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
4dae0 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64 20  r of pages used 
4daf0 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70  out of the.** [p
4db00 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  agecache memory 
4db10 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74 20  allocator] that 
4db20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 75  was configured u
4db30 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 45  sing .** [SQLITE
4db40 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
4db50 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75  E].  The.** valu
4db60 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e  e returned is in
4db70 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20 62   pages, not in b
4db80 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ytes.</dd>.**.**
4db90 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54   <dt>SQLITE_STAT
4dba0 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45  US_PAGECACHE_OVE
4dbb0 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  RFLOW</dt>.** <d
4dbc0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
4dbd0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
4dbe0 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
4dbf0 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61 6c  page cache.** al
4dc00 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63  location which c
4dc10 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74  ould not be stat
4dc20 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53  isfied by the [S
4dc30 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
4dc40 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66 65  ECACHE].** buffe
4dc50 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63  r and where forc
4dc60 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74  ed to overflow t
4dc70 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  o [sqlite3_mallo
4dc80 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 65  c()].  The.** re
4dc90 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e 63  turned value inc
4dca0 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f 6e  ludes allocation
4dcb0 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65  s that overflowe
4dcc0 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a 2a  d because they.*
4dcd0 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72 67  * where too larg
4dce0 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61 72  e (they were lar
4dcf0 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73 7a  ger than the "sz
4dd00 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a  " parameter to.*
4dd10 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
4dd20 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e 64  _PAGECACHE]) and
4dd30 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61   allocations tha
4dd40 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63  t overflowed bec
4dd50 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 65  ause.** no space
4dd60 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68 65   was left in the
4dd70 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64 64   page cache.</dd
4dd80 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
4dd90 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41  TE_STATUS_PAGECA
4dda0 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a  CHE_SIZE</dt>.**
4ddb0 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
4ddc0 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20  ter records the 
4ddd0 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61  largest memory a
4dde0 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73  llocation reques
4ddf0 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b  t.** handed to [
4de00 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79  pagecache memory
4de10 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e   allocator].  On
4de20 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  ly the value ret
4de30 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  urned in the.** 
4de40 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61  *pHighwater para
4de50 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
4de60 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f  3_status()] is o
4de70 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a  f interest.  .**
4de80 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74   The value writt
4de90 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75  en into the *pCu
4dea0 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20  rrent parameter 
4deb0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64  is undefined.</d
4dec0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
4ded0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
4dee0 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20  CH_USED</dt>.** 
4def0 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
4df00 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  er returns the n
4df10 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74  umber of allocat
4df20 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f 66  ions used out of
4df30 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63 68   the.** [scratch
4df40 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
4df50 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73  r] configured us
4df60 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43  ing.** [SQLITE_C
4df70 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e 20  ONFIG_SCRATCH]. 
4df80 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
4df90 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63 61  ned is in alloca
4dfa0 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e  tions, not.** in
4dfb0 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20 61   bytes.  Since a
4dfc0 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 6d   single thread m
4dfd0 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e 65  ay only have one
4dfe0 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74   scratch allocat
4dff0 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69  ion.** outstandi
4e000 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69 73  ng at time, this
4e010 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f 20   parameter also 
4e020 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d 62  reports the numb
4e030 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a 2a  er of threads.**
4e040 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20 6d   using scratch m
4e050 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61 6d  emory at the sam
4e060 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a  e time.</dd>.**.
4e070 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
4e080 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45  ATUS_SCRATCH_OVE
4e090 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  RFLOW</dt>.** <d
4e0a0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
4e0b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
4e0c0 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
4e0d0 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a  scratch memory.*
4e0e0 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69  * allocation whi
4e0f0 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ch could not be 
4e100 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74 68  statisfied by th
4e110 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  e [SQLITE_CONFIG
4e120 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75 66  _SCRATCH].** buf
4e130 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f  fer and where fo
4e140 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77  rced to overflow
4e150 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c   to [sqlite3_mal
4e160 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61 6c  loc()].  The val
4e170 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  ues.** returned 
4e180 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f 77  include overflow
4e190 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65  s because the re
4e1a0 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69  quested allocati
4e1b0 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61  on was too.** la
4e1c0 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20 62  rger (that is, b
4e1d0 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65  ecause the reque
4e1e0 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  sted allocation 
4e1f0 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20  was larger than 
4e200 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72 61  the.** "sz" para
4e210 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54 45  meter to [SQLITE
4e220 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d  _CONFIG_SCRATCH]
4e230 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e 6f  ) and because no
4e240 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 0a   scratch buffer.
4e250 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61 76  ** slots were av
4e260 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64  ailable..** </dd
4e270 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
4e280 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
4e290 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c  H_SIZE</dt>.** <
4e2a0 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
4e2b0 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61  r records the la
4e2c0 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c  rgest memory all
4e2d0 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a  ocation request.
4e2e0 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 63  ** handed to [sc
4e2f0 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c  ratch memory all
4e300 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74  ocator].  Only t
4e310 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
4e320 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69  d in the.** *pHi
4e330 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65  ghwater paramete
4e340 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  r to [sqlite3_st
4e350 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e  atus()] is of in
4e360 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65  terest.  .** The
4e370 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69   value written i
4e380 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e  nto the *pCurren
4e390 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75  t parameter is u
4e3a0 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a  ndefined.</dd>.*
4e3b0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
4e3c0 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54  STATUS_PARSER_ST
4e3d0 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  ACK</dt>.** <dd>
4e3e0 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
4e3f0 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70 65  ecords the deepe
4e400 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b 2e  st parser stack.
4e410 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20    It is only.** 
4e420 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53 51  meaningful if SQ
4e430 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
4e440 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d 41   with [YYTRACKMA
4e450 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64  XSTACKDEPTH].</d
4e460 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a  d>.** </dl>.**.*
4e470 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61 72  * New status par
4e480 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20 61  ameters may be a
4e490 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20 74  dded from time t
4e4a0 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69  o time..*/.#defi
4e4b0 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53  ne SQLITE_STATUS
4e4c0 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20 20  _MEMORY_USED    
4e4d0 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
4e4e0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
4e4f0 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20 20  GECACHE_USED    
4e500 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
4e510 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
4e520 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20 20  ACHE_OVERFLOW   
4e530 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
4e540 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
4e550 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a 23  USED         3.#
4e560 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
4e570 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45  ATUS_SCRATCH_OVE
4e580 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65 66  RFLOW     4.#def
4e590 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
4e5a0 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 20  S_MALLOC_SIZE   
4e5b0 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65         5.#define
4e5c0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50   SQLITE_STATUS_P
4e5d0 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20 20  ARSER_STACK     
4e5e0 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51      6.#define SQ
4e5f0 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
4e600 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20 20  CACHE_SIZE      
4e610 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   7.#define SQLIT
4e620 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
4e630 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38 0a  _SIZE         8.
4e640 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4e650 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63   Database Connec
4e660 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31 37  tion Status {H17
4e670 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a  500} <S60200>.**
4e680 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
4e690 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
4e6a0 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ce is used to re
4e6b0 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20 73  trieve runtime s
4e6c0 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tatus informatio
4e6d0 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73 69  n .** about a si
4e6e0 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20 63  ngle [database c
4e6f0 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65  onnection].  The
4e700 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
4e710 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  is the.** databa
4e720 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62  se connection ob
4e730 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65 72  ject to be inter
4e740 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73 65  rogated.  The se
4e750 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
4e760 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 65   is the paramete
4e770 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74 65  r to interrogate
4e780 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68  .  Currently, th
4e790 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 76  e only allowed v
4e7a0 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20  alue.** for the 
4e7b0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
4e7c0 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53 54   is [SQLITE_DBST
4e7d0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55  ATUS_LOOKASIDE_U
4e7e0 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 6f  SED]..** Additio
4e7f0 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c 6c  nal options will
4e800 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20 69   likely appear i
4e810 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
4e820 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s of SQLite..**.
4e830 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76  ** The current v
4e840 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71 75  alue of the requ
4e850 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72 20  ested parameter 
4e860 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  is written into 
4e870 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68 65  *pCur.** and the
4e880 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e 74   highest instant
4e890 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73 20  aneous value is 
4e8a0 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48  written into *pH
4e8b0 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68 65  iwtr.  If.** the
4e8c0 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 75   resetFlg is tru
4e8d0 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67 68  e, then the high
4e8e0 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75  est instantaneou
4e8f0 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72 65  s value is.** re
4e900 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f  set back down to
4e910 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   the current val
4e920 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ue..**.** See al
4e930 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61  so: [sqlite3_sta
4e940 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  tus()] and [sqli
4e950 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28  te3_stmt_status(
4e960 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
4e970 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
4e980 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
4e990 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c 69  3_db_status(sqli
4e9a0 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  te3*, int op, in
4e9b0 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48  t *pCur, int *pH
4e9c0 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74 46  iwtr, int resetF
4e9d0 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lg);../*.** CAPI
4e9e0 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72  3REF: Status Par
4e9f0 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74 61  ameters for data
4ea00 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
4ea10 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35 30   {H17520} <H1750
4ea20 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
4ea30 41 4c 0a 2a 2a 0a 2a 2a 20 53 74 61 74 75 73 20  AL.**.** Status 
4ea40 76 65 72 62 73 20 66 6f 72 20 5b 73 71 6c 69 74  verbs for [sqlit
4ea50 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e  e3_db_status()].
4ea60 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c  .**.** <dl>.** <
4ea70 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54  dt>SQLITE_DBSTAT
4ea80 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45  US_LOOKASIDE_USE
4ea90 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
4eaa0 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74  is parameter ret
4eab0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
4eac0 6f 66 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d  of lookaside mem
4ead0 6f 72 79 20 73 6c 6f 74 73 20 63 75 72 72 65 6e  ory slots curren
4eae0 74 6c 79 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f  tly.** checked o
4eaf0 75 74 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c  ut.</dd>.** </dl
4eb00 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  >.*/.#define SQL
4eb10 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f  ITE_DBSTATUS_LOO
4eb20 4b 41 53 49 44 45 5f 55 53 45 44 20 20 20 20 20  KASIDE_USED     
4eb30 30 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  0.../*.** CAPI3R
4eb40 45 46 3a 20 50 72 65 70 61 72 65 64 20 53 74 61  EF: Prepared Sta
4eb50 74 65 6d 65 6e 74 20 53 74 61 74 75 73 20 7b 48  tement Status {H
4eb60 31 37 35 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a  17550} <S60200>.
4eb70 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
4eb80 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 72 65 70 61  **.** Each prepa
4eb90 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 61  red statement ma
4eba0 69 6e 74 61 69 6e 73 20 76 61 72 69 6f 75 73 0a  intains various.
4ebb0 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53  ** [SQLITE_STMTS
4ebc0 54 41 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75  TATUS_SORT | cou
4ebd0 6e 74 65 72 73 5d 20 74 68 61 74 20 6d 65 61 73  nters] that meas
4ebe0 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a  ure the number.*
4ebf0 2a 20 6f 66 20 74 69 6d 65 73 20 69 74 20 68 61  * of times it ha
4ec00 73 20 70 65 72 66 6f 72 6d 65 64 20 73 70 65 63  s performed spec
4ec10 69 66 69 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e  ific operations.
4ec20 20 20 54 68 65 73 65 20 63 6f 75 6e 74 65 72 73    These counters
4ec30 20 63 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20   can.** be used 
4ec40 74 6f 20 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70  to monitor the p
4ec50 65 72 66 6f 72 6d 61 6e 63 65 20 63 68 61 72 61  erformance chara
4ec60 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68  cteristics of th
4ec70 65 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74  e prepared.** st
4ec80 61 74 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65  atements.  For e
4ec90 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 6e  xample, if the n
4eca0 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73  umber of table s
4ecb0 74 65 70 73 20 67 72 65 61 74 6c 79 20 65 78 63  teps greatly exc
4ecc0 65 65 64 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  eeds.** the numb
4ecd0 65 72 20 6f 66 20 74 61 62 6c 65 20 73 65 61 72  er of table sear
4ece0 63 68 65 73 20 6f 72 20 72 65 73 75 6c 74 20 72  ches or result r
4ecf0 6f 77 73 2c 20 74 68 61 74 20 77 6f 75 6c 64 20  ows, that would 
4ed00 74 65 6e 64 20 74 6f 20 69 6e 64 69 63 61 74 65  tend to indicate
4ed10 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 72 65  .** that the pre
4ed20 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
4ed30 69 73 20 75 73 69 6e 67 20 61 20 66 75 6c 6c 20  is using a full 
4ed40 74 61 62 6c 65 20 73 63 61 6e 20 72 61 74 68 65  table scan rathe
4ed50 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64  r than.** an ind
4ed60 65 78 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ex.  .**.** This
4ed70 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73   interface is us
4ed80 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61  ed to retrieve a
4ed90 6e 64 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72  nd reset counter
4eda0 20 76 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20   values from.** 
4edb0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
4edc0 65 6d 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72  ement].  The fir
4edd0 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  st argument is t
4ede0 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
4edf0 65 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20  ement.** object 
4ee00 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74  to be interrogat
4ee10 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ed.  The second 
4ee20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61  argument.** is a
4ee30 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66  n integer code f
4ee40 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 5b 53  or a specific [S
4ee50 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
4ee60 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d  _SORT | counter]
4ee70 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72  .** to be interr
4ee80 6f 67 61 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20  ogated. .** The 
4ee90 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66  current value of
4eea0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63   the requested c
4eeb0 6f 75 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e  ounter is return
4eec0 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  ed..** If the re
4eed0 73 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20  setFlg is true, 
4eee0 74 68 65 6e 20 74 68 65 20 63 6f 75 6e 74 65 72  then the counter
4eef0 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72   is reset to zer
4ef00 6f 20 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20  o after this.** 
4ef10 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 20 72  interface call r
4ef20 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65  eturns..**.** Se
4ef30 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
4ef40 5f 73 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b  _status()] and [
4ef50 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75  sqlite3_db_statu
4ef60 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  s()]..*/.SQLITE_
4ef70 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52  API SQLITE_EXPER
4ef80 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69  IMENTAL int sqli
4ef90 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28  te3_stmt_status(
4efa0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
4efb0 6e 74 20 6f 70 2c 69 6e 74 20 72 65 73 65 74 46  nt op,int resetF
4efc0 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lg);../*.** CAPI
4efd0 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72  3REF: Status Par
4efe0 61 6d 65 74 65 72 73 20 66 6f 72 20 70 72 65 70  ameters for prep
4eff0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
4f000 7b 48 31 37 35 37 30 7d 20 3c 48 31 37 35 35 30  {H17570} <H17550
4f010 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
4f020 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72  L.**.** These pr
4f030 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f  eprocessor macro
4f040 73 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72  s define integer
4f050 20 63 6f 64 65 73 20 74 68 61 74 20 6e 61 6d 65   codes that name
4f060 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75   counter.** valu
4f070 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
4f080 74 68 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  th the [sqlite3_
4f090 73 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 20 69  stmt_status()] i
4f0a0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65  nterface..** The
4f0b0 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65   meanings of the
4f0c0 20 76 61 72 69 6f 75 73 20 63 6f 75 6e 74 65 72   various counter
4f0d0 73 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73  s are as follows
4f0e0 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20  :.**.** <dl>.** 
4f0f0 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53  <dt>SQLITE_STMTS
4f100 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53  TATUS_FULLSCAN_S
4f110 54 45 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TEP</dt>.** <dd>
4f120 54 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62  This is the numb
4f130 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74  er of times that
4f140 20 53 51 4c 69 74 65 20 68 61 73 20 73 74 65 70   SQLite has step
4f150 70 65 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a  ped forward in.*
4f160 2a 20 61 20 74 61 62 6c 65 20 61 73 20 70 61 72  * a table as par
4f170 74 20 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c  t of a full tabl
4f180 65 20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e  e scan.  Large n
4f190 75 6d 62 65 72 73 20 66 6f 72 20 74 68 69 73 20  umbers for this 
4f1a0 63 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69  counter.** may i
4f1b0 6e 64 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e  ndicate opportun
4f1c0 69 74 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72  ities for perfor
4f1d0 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e  mance improvemen
4f1e0 74 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61  t through .** ca
4f1f0 72 65 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64  reful use of ind
4f200 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ices.</dd>.**.**
4f210 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54   <dt>SQLITE_STMT
4f220 53 54 41 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e  STATUS_SORT</dt>
4f230 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 73 20  .** <dd>This is 
4f240 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f  the number of so
4f250 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68  rt operations th
4f260 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  at have occurred
4f270 2e 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20  ..** A non-zero 
4f280 76 61 6c 75 65 20 69 6e 20 74 68 69 73 20 63 6f  value in this co
4f290 75 6e 74 65 72 20 6d 61 79 20 69 6e 64 69 63 61  unter may indica
4f2a0 74 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74  te an opportunit
4f2b0 79 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d  y to.** improvem
4f2c0 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ent performance 
4f2d0 74 68 72 6f 75 67 68 20 63 61 72 65 66 75 6c 20  through careful 
4f2e0 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c  use of indices.<
4f2f0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e  /dd>.**.** </dl>
4f300 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
4f310 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55  TE_STMTSTATUS_FU
4f320 4c 4c 53 43 41 4e 5f 53 54 45 50 20 20 20 20 20  LLSCAN_STEP     
4f330 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
4f340 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54  _STMTSTATUS_SORT
4f350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a                2.
4f360 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4f370 20 43 75 73 74 6f 6d 20 50 61 67 65 20 43 61 63   Custom Page Cac
4f380 68 65 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50  he Object.** EXP
4f390 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
4f3a0 54 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  The sqlite3_pcac
4f3b0 68 65 20 74 79 70 65 20 69 73 20 6f 70 61 71 75  he type is opaqu
4f3c0 65 2e 20 20 49 74 20 69 73 20 69 6d 70 6c 65 6d  e.  It is implem
4f3d0 65 6e 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  ented by.** the 
4f3e0 70 6c 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65  pluggable module
4f3f0 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  .  The SQLite co
4f400 72 65 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65  re has no knowle
4f410 64 67 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69  dge of.** its si
4f420 7a 65 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73  ze or internal s
4f430 74 72 75 63 74 75 72 65 20 61 6e 64 20 6e 65 76  tructure and nev
4f440 65 72 20 64 65 61 6c 73 20 77 69 74 68 20 74 68  er deals with th
4f450 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61  e.** sqlite3_pca
4f460 63 68 65 20 6f 62 6a 65 63 74 20 65 78 63 65 70  che object excep
4f470 74 20 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64  t by holding and
4f480 20 70 61 73 73 69 6e 67 20 70 6f 69 6e 74 65 72   passing pointer
4f490 73 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65  s.** to the obje
4f4a0 63 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73  ct..**.** See [s
4f4b0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
4f4c0 74 68 6f 64 73 5d 20 66 6f 72 20 61 64 64 69 74  thods] for addit
4f4d0 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
4f4e0 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
4f4f0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61  ruct sqlite3_pca
4f500 63 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  che sqlite3_pcac
4f510 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  he;../*.** CAPI3
4f520 52 45 46 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e  REF: Application
4f530 20 44 65 66 69 6e 65 64 20 50 61 67 65 20 43 61   Defined Page Ca
4f540 63 68 65 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  che..** EXPERIME
4f550 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  NTAL.**.** The [
4f560 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28  sqlite3_config](
4f570 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  [SQLITE_CONFIG_P
4f580 43 41 43 48 45 5d 2c 20 2e 2e 2e 29 20 69 6e 74  CACHE], ...) int
4f590 65 72 66 61 63 65 20 63 61 6e 0a 2a 2a 20 72 65  erface can.** re
4f5a0 67 69 73 74 65 72 20 61 6e 20 61 6c 74 65 72 6e  gister an altern
4f5b0 61 74 69 76 65 20 70 61 67 65 20 63 61 63 68 65  ative page cache
4f5c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
4f5d0 62 79 20 70 61 73 73 69 6e 67 20 69 6e 20 61 6e  by passing in an
4f5e0 20 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66   .** instance of
4f5f0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
4f600 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 74 72 75  che_methods stru
4f610 63 74 75 72 65 2e 20 54 68 65 20 6d 61 6a 6f 72  cture. The major
4f620 69 74 79 20 6f 66 20 74 68 65 20 0a 2a 2a 20 68  ity of the .** h
4f630 65 61 70 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  eap memory used 
4f640 62 79 20 73 71 6c 69 74 65 20 69 73 20 75 73 65  by sqlite is use
4f650 64 20 62 79 20 74 68 65 20 70 61 67 65 20 63 61  d by the page ca
4f660 63 68 65 20 74 6f 20 63 61 63 68 65 20 64 61 74  che to cache dat
4f670 61 20 72 65 61 64 20 0a 2a 2a 20 66 72 6f 6d 2c  a read .** from,
4f680 20 6f 72 20 72 65 61 64 79 20 74 6f 20 62 65 20   or ready to be 
4f690 77 72 69 74 74 65 6e 20 74 6f 2c 20 74 68 65 20  written to, the 
4f6a0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 42  database file. B
4f6b0 79 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 61  y implementing a
4f6c0 20 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 61 67 65   .** custom page
4f6d0 20 63 61 63 68 65 20 75 73 69 6e 67 20 74 68 69   cache using thi
4f6e0 73 20 41 50 49 2c 20 61 6e 20 61 70 70 6c 69 63  s API, an applic
4f6f0 61 74 69 6f 6e 20 63 61 6e 20 63 6f 6e 74 72 6f  ation can contro
4f700 6c 20 6d 6f 72 65 20 0a 2a 2a 20 70 72 65 63 69  l more .** preci
4f710 73 65 6c 79 20 74 68 65 20 61 6d 6f 75 6e 74 20  sely the amount 
4f720 6f 66 20 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d  of memory consum
4f730 65 64 20 62 79 20 73 71 6c 69 74 65 2c 20 74 68  ed by sqlite, th
4f740 65 20 77 61 79 20 69 6e 20 77 68 69 63 68 20 0a  e way in which .
4f750 2a 2a 20 73 61 69 64 20 6d 65 6d 6f 72 79 20 69  ** said memory i
4f760 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  s allocated and 
4f770 72 65 6c 65 61 73 65 64 2c 20 61 6e 64 20 74 68  released, and th
4f780 65 20 70 6f 6c 69 63 69 65 73 20 75 73 65 64 20  e policies used 
4f790 74 6f 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65  to .** determine
4f7a0 20 65 78 61 63 74 6c 79 20 77 68 69 63 68 20 70   exactly which p
4f7b0 61 72 74 73 20 6f 66 20 61 20 64 61 74 61 62 61  arts of a databa
4f7c0 73 65 20 66 69 6c 65 20 61 72 65 20 63 61 63 68  se file are cach
4f7d0 65 64 20 61 6e 64 20 66 6f 72 20 0a 2a 2a 20 68  ed and for .** h
4f7e0 6f 77 20 6c 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  ow long..**.** T
4f7f0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
4f800 68 65 20 73 74 72 75 63 74 75 72 65 20 61 72 65  he structure are
4f810 20 63 6f 70 69 65 64 20 74 6f 20 61 6e 20 69 6e   copied to an in
4f820 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 79  ternal buffer by
4f830 20 73 71 6c 69 74 65 0a 2a 2a 20 77 69 74 68 69   sqlite.** withi
4f840 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73  n the call to [s
4f850 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 0a  qlite3_config]..
4f860 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 28  **.** The xInit(
4f870 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c  ) method is call
4f880 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
4f890 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
4f8a0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 0a  3_initialize()].
4f8b0 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f 6e 6c 79  ** (usually only
4f8c0 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 74 68 65   once during the
4f8d0 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65   lifetime of the
4f8e0 20 70 72 6f 63 65 73 73 29 2e 20 49 74 20 69 73   process). It is
4f8f0 20 70 61 73 73 65 64 0a 2a 2a 20 61 20 63 6f 70   passed.** a cop
4f900 79 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  y of the sqlite3
4f910 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2e  _pcache_methods.
4f920 70 41 72 67 20 76 61 6c 75 65 2e 20 49 74 20 63  pArg value. It c
4f930 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
4f940 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 6c 20 73  t.** up global s
4f950 74 72 75 63 74 75 72 65 73 20 61 6e 64 20 6d 75  tructures and mu
4f960 74 65 78 65 73 20 72 65 71 75 69 72 65 64 20 62  texes required b
4f970 79 20 74 68 65 20 63 75 73 74 6f 6d 20 70 61 67  y the custom pag
4f980 65 20 63 61 63 68 65 20 0a 2a 2a 20 69 6d 70 6c  e cache .** impl
4f990 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 65 20  ementation. The 
4f9a0 78 53 68 75 74 64 6f 77 6e 28 29 20 6d 65 74 68  xShutdown() meth
4f9b0 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f  od is called fro
4f9c0 6d 20 77 69 74 68 69 6e 20 0a 2a 2a 20 5b 73 71  m within .** [sq
4f9d0 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
4f9e0 5d 2c 20 69 66 20 74 68 65 20 61 70 70 6c 69 63  ], if the applic
4f9f0 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 74 68  ation invokes th
4fa00 69 73 20 41 50 49 2e 20 49 74 20 63 61 6e 20 62  is API. It can b
4fa10 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 63 6c 65  e used.** to cle
4fa20 61 6e 20 75 70 20 61 6e 79 20 6f 75 74 73 74 61  an up any outsta
4fa30 6e 64 69 6e 67 20 72 65 73 6f 75 72 63 65 73 20  nding resources 
4fa40 62 65 66 6f 72 65 20 70 72 6f 63 65 73 73 20 73  before process s
4fa50 68 75 74 64 6f 77 6e 2c 20 69 66 20 72 65 71 75  hutdown, if requ
4fa60 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ired..**.** The 
4fa70 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64  xCreate() method
4fa80 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73   is used to cons
4fa90 74 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68  truct a new cach
4faa0 65 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 0a  e instance. The.
4fab0 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  ** first paramet
4fac0 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73 20 74  er, szPage, is t
4fad0 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  he size in bytes
4fae0 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 74 68   of the pages th
4faf0 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6c  at must.** be al
4fb00 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 63  located by the c
4fb10 61 63 68 65 2e 20 73 7a 50 61 67 65 20 77 69 6c  ache. szPage wil
4fb20 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f 77 65 72  l not be a power
4fb30 20 6f 66 20 74 77 6f 2e 20 54 68 65 0a 2a 2a 20   of two. The.** 
4fb40 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
4fb50 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20   bPurgeable, is 
4fb60 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68  true if the cach
4fb70 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20  e being created 
4fb80 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20  will.** be used 
4fb90 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73  to cache databas
4fba0 65 20 70 61 67 65 73 20 72 65 61 64 20 66 72 6f  e pages read fro
4fbb0 6d 20 61 20 66 69 6c 65 20 73 74 6f 72 65 64 20  m a file stored 
4fbc0 6f 6e 20 64 69 73 6b 2c 20 6f 72 0a 2a 2a 20 66  on disk, or.** f
4fbd0 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 75 73  alse if it is us
4fbe0 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d  ed for an in-mem
4fbf0 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20 54 68  ory database. Th
4fc00 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
4fc10 74 61 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e  tation.** does n
4fc20 6f 74 20 68 61 76 65 20 74 6f 20 64 6f 20 61 6e  ot have to do an
4fc30 79 74 68 69 6e 67 20 73 70 65 63 69 61 6c 20 62  ything special b
4fc40 61 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c 75  ased on the valu
4fc50 65 20 6f 66 20 62 50 75 72 67 65 61 62 6c 65 2c  e of bPurgeable,
4fc60 0a 2a 2a 20 69 74 20 69 73 20 70 75 72 65 6c 79  .** it is purely
4fc70 20 61 64 76 69 73 6f 72 79 2e 20 0a 2a 2a 0a 2a   advisory. .**.*
4fc80 2a 20 54 68 65 20 78 43 61 63 68 65 73 69 7a 65  * The xCachesize
4fc90 28 29 20 6d 65 74 68 6f 64 20 6d 61 79 20 62 65  () method may be
4fca0 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79 20 74   called at any t
4fcb0 69 6d 65 20 62 79 20 53 51 4c 69 74 65 20 74 6f  ime by SQLite to
4fcc0 20 73 65 74 20 74 68 65 0a 2a 2a 20 73 75 67 67   set the.** sugg
4fcd0 65 73 74 65 64 20 6d 61 78 69 6d 75 6d 20 63 61  ested maximum ca
4fce0 63 68 65 2d 73 69 7a 65 20 28 6e 75 6d 62 65 72  che-size (number
4fcf0 20 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64   of pages stored
4fd00 20 62 79 29 20 74 68 65 20 63 61 63 68 65 0a 2a   by) the cache.*
4fd10 2a 20 69 6e 73 74 61 6e 63 65 20 70 61 73 73 65  * instance passe
4fd20 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
4fd30 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  rgument. This is
4fd40 20 74 68 65 20 76 61 6c 75 65 20 63 6f 6e 66 69   the value confi
4fd50 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74  gured using.** t
4fd60 68 65 20 53 51 4c 69 74 65 20 22 5b 50 52 41 47  he SQLite "[PRAG
4fd70 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 5d 22 20  MA cache_size]" 
4fd80 63 6f 6d 6d 61 6e 64 2e 20 41 73 20 77 69 74 68  command. As with
4fd90 20 74 68 65 20 62 50 75 72 67 65 61 62 6c 65 20   the bPurgeable 
4fda0 70 61 72 61 6d 65 74 65 72 2c 0a 2a 2a 20 74 68  parameter,.** th
4fdb0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
4fdc0 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
4fdd0 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20   to do anything 
4fde0 73 70 65 63 69 61 6c 20 77 69 74 68 20 74 68 69  special with thi
4fdf0 73 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69  s.** value, it i
4fe00 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e  s advisory only.
4fe10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65  .**.** The xPage
4fe20 63 6f 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73  count() method s
4fe30 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65  hould return the
4fe40 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
4fe50 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74   currently.** st
4fe60 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68  ored in the cach
4fe70 65 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e  e supplied as an
4fe80 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a   argument..** .*
4fe90 2a 20 54 68 65 20 78 46 65 74 63 68 28 29 20 6d  * The xFetch() m
4fea0 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f  ethod is used to
4feb0 20 66 65 74 63 68 20 61 20 70 61 67 65 20 61 6e   fetch a page an
4fec0 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  d return a point
4fed0 65 72 20 74 6f 20 69 74 2e 20 0a 2a 2a 20 41 20  er to it. .** A 
4fee0 27 70 61 67 65 27 2c 20 69 6e 20 74 68 69 73 20  'page', in this 
4fef0 63 6f 6e 74 65 78 74 2c 20 69 73 20 61 20 62 75  context, is a bu
4ff00 66 66 65 72 20 6f 66 20 73 7a 50 61 67 65 20 62  ffer of szPage b
4ff10 79 74 65 73 20 61 6c 69 67 6e 65 64 20 61 74 20  ytes aligned at 
4ff20 61 6e 0a 2a 2a 20 38 2d 62 79 74 65 20 62 6f 75  an.** 8-byte bou
4ff30 6e 64 61 72 79 2e 20 54 68 65 20 70 61 67 65 20  ndary. The page 
4ff40 74 6f 20 62 65 20 66 65 74 63 68 65 64 20 69 73  to be fetched is
4ff50 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
4ff60 68 65 20 6b 65 79 2e 20 54 68 65 0a 2a 2a 20 6d  he key. The.** m
4ff70 69 6d 69 6d 75 6d 20 6b 65 79 20 76 61 6c 75 65  imimum key value
4ff80 20 69 73 20 31 2e 20 41 66 74 65 72 20 69 74 20   is 1. After it 
4ff90 68 61 73 20 62 65 65 6e 20 72 65 74 72 69 65 76  has been retriev
4ffa0 65 64 20 75 73 69 6e 67 20 78 46 65 74 63 68 2c  ed using xFetch,
4ffb0 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 69 73   the page .** is
4ffc0 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62   considered to b
4ffd0 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  e pinned..**.** 
4ffe0 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  If the requested
4fff0 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79   page is already
50000 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63   in the page cac
50010 68 65 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74  he, then a point
50020 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 63 61 63  er to.** the cac
50030 68 65 64 20 62 75 66 66 65 72 20 73 68 6f 75 6c  hed buffer shoul
50040 64 20 62 65 20 72 65 74 75 72 6e 65 64 20 77 69  d be returned wi
50050 74 68 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20  th its contents 
50060 69 6e 74 61 63 74 2e 20 49 66 20 74 68 65 0a 2a  intact. If the.*
50070 2a 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c  * page is not al
50080 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63  ready in the cac
50090 68 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70  he, then the exp
500a0 65 63 74 65 64 20 62 65 68 61 76 69 6f 75 72 20  ected behaviour 
500b0 6f 66 20 74 68 65 0a 2a 2a 20 63 61 63 68 65 20  of the.** cache 
500c0 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
500d0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
500e0 65 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72  e createFlag par
500f0 61 6d 65 74 65 72 20 70 61 73 73 65 64 0a 2a 2a  ameter passed.**
50100 20 74 6f 20 78 46 65 74 63 68 2c 20 61 63 63 6f   to xFetch, acco
50110 72 64 69 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c  rding to the fol
50120 6c 6f 77 69 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a  lowing table:.**
50130 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65  .** <table borde
50140 72 3d 31 20 77 69 64 74 68 3d 38 35 25 20 61 6c  r=1 width=85% al
50150 69 67 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a 20 20  ign=center>.**  
50160 20 3c 74 72 3e 3c 74 68 3e 63 72 65 61 74 65 46   <tr><th>createF
50170 6c 61 67 3c 74 68 3e 45 78 70 65 63 74 65 64 20  lag<th>Expected 
50180 42 65 68 61 76 69 6f 75 72 0a 2a 2a 20 20 20 3c  Behaviour.**   <
50190 74 72 3e 3c 74 64 3e 30 3c 74 64 3e 4e 55 4c 4c  tr><td>0<td>NULL
501a0 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72   should be retur
501b0 6e 65 64 2e 20 4e 6f 20 6e 65 77 20 63 61 63 68  ned. No new cach
501c0 65 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74  e entry is creat
501d0 65 64 2e 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64  ed..**   <tr><td
501e0 3e 31 3c 74 64 3e 49 66 20 63 72 65 61 74 65 46  >1<td>If createF
501f0 6c 61 67 20 69 73 20 73 65 74 20 74 6f 20 31 2c  lag is set to 1,
50200 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   this indicates 
50210 74 68 61 74 20 0a 2a 2a 20 20 20 20 20 20 20 20  that .**        
50220 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 69          SQLite i
50230 73 20 68 6f 6c 64 69 6e 67 20 70 69 6e 6e 65 64  s holding pinned
50240 20 70 61 67 65 73 20 74 68 61 74 20 63 61 6e 20   pages that can 
50250 62 65 20 75 6e 70 69 6e 6e 65 64 0a 2a 2a 20 20  be unpinned.**  
50260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 79                by
50270 20 77 72 69 74 69 6e 67 20 74 68 65 69 72 20 63   writing their c
50280 6f 6e 74 65 6e 74 73 20 74 6f 20 74 68 65 20 64  ontents to the d
50290 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 0a  atabase file (a.
502a0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
502b0 20 20 72 65 6c 61 74 69 76 65 6c 79 20 65 78 70    relatively exp
502c0 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e  ensive operation
502d0 29 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75 61  ). In this situa
502e0 74 69 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20  tion the.**     
502f0 20 20 20 20 20 20 20 20 20 20 20 63 61 63 68 65             cache
50300 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
50310 68 61 73 20 74 77 6f 20 63 68 6f 69 63 65 73 3a  has two choices:
50320 20 69 74 20 63 61 6e 20 72 65 74 75 72 6e 20 4e   it can return N
50330 55 4c 4c 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ULL,.**         
50340 20 20 20 20 20 20 20 69 6e 20 77 68 69 63 68 20         in which 
50350 63 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c  case SQLite will
50360 20 61 74 74 65 6d 70 74 20 74 6f 20 75 6e 70 69   attempt to unpi
50370 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 0a 2a  n one or more .*
50380 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
50390 20 70 61 67 65 73 20 62 65 66 6f 72 65 20 72 65   pages before re
503a0 2d 72 65 71 75 65 73 74 69 6e 67 20 74 68 65 20  -requesting the 
503b0 73 61 6d 65 20 70 61 67 65 2c 20 6f 72 20 69 74  same page, or it
503c0 20 63 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20   can.**         
503d0 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 20         allocate 
503e0 61 20 6e 65 77 20 70 61 67 65 20 61 6e 64 20 72  a new page and r
503f0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
50400 74 6f 20 69 74 2e 20 49 66 20 61 20 6e 65 77 0a  to it. If a new.
50410 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
50420 20 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61    page is alloca
50430 74 65 64 2c 20 74 68 65 6e 20 69 74 20 6d 75 73  ted, then it mus
50440 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20  t be completely 
50450 7a 65 72 6f 65 64 20 62 65 66 6f 72 65 20 0a 2a  zeroed before .*
50460 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
50470 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e   it is returned.
50480 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64 3e 32 3c  .**   <tr><td>2<
50490 74 64 3e 49 66 20 63 72 65 61 74 65 46 6c 61 67  td>If createFlag
504a0 20 69 73 20 73 65 74 20 74 6f 20 32 2c 20 74 68   is set to 2, th
504b0 65 6e 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74  en SQLite is not
504c0 20 68 6f 6c 64 69 6e 67 20 61 6e 79 0a 2a 2a 20   holding any.** 
504d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
504e0 69 6e 6e 65 64 20 70 61 67 65 73 20 61 73 73 6f  inned pages asso
504f0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
50500 73 70 65 63 69 66 69 63 20 63 61 63 68 65 20 70  specific cache p
50510 61 73 73 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  assed.**        
50520 20 20 20 20 20 20 20 20 61 73 20 74 68 65 20 66          as the f
50530 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
50540 20 78 46 65 74 63 68 28 29 20 74 68 61 74 20 63   xFetch() that c
50550 61 6e 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20  an be unpinned. 
50560 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  The.**          
50570 20 20 20 20 20 20 63 61 63 68 65 20 69 6d 70 6c        cache impl
50580 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c  ementation shoul
50590 64 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c  d attempt to all
505a0 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20  ocate a new.**  
505b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 61                ca
505c0 63 68 65 20 65 6e 74 72 79 20 61 6e 64 20 72 65  che entry and re
505d0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
505e0 6f 20 69 74 2e 20 41 67 61 69 6e 2c 20 74 68 65  o it. Again, the
505f0 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 20   new.**         
50600 20 20 20 20 20 20 20 70 61 67 65 20 73 68 6f 75         page shou
50610 6c 64 20 62 65 20 7a 65 72 6f 65 64 20 62 65 66  ld be zeroed bef
50620 6f 72 65 20 69 74 20 69 73 20 72 65 74 75 72 6e  ore it is return
50630 65 64 2e 20 49 66 20 74 68 65 20 78 46 65 74 63  ed. If the xFetc
50640 68 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  h().**          
50650 20 20 20 20 20 20 6d 65 74 68 6f 64 20 72 65 74        method ret
50660 75 72 6e 73 20 4e 55 4c 4c 20 77 68 65 6e 20 63  urns NULL when c
50670 72 65 61 74 65 46 6c 61 67 3d 3d 32 2c 20 53 51  reateFlag==2, SQ
50680 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74 68 61  Lite assumes tha
50690 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
506a0 20 20 20 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c      a memory all
506b0 6f 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 20 61  ocation failed a
506c0 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  nd returns SQLIT
506d0 45 5f 4e 4f 4d 45 4d 20 74 6f 20 74 68 65 0a 2a  E_NOMEM to the.*
506e0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
506f0 20 75 73 65 72 2e 0a 2a 2a 20 3c 2f 74 61 62 6c   user..** </tabl
50700 65 3e 0a 2a 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28  e>.**.** xUnpin(
50710 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53  ) is called by S
50720 51 4c 69 74 65 20 77 69 74 68 20 61 20 70 6f 69  QLite with a poi
50730 6e 74 65 72 20 74 6f 20 61 20 63 75 72 72 65 6e  nter to a curren
50740 74 6c 79 20 70 69 6e 6e 65 64 20 70 61 67 65 0a  tly pinned page.
50750 2a 2a 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64  ** as its second
50760 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 68   argument. If th
50770 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
50780 72 2c 20 64 69 73 63 61 72 64 2c 20 69 73 20 6e  r, discard, is n
50790 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e  on-zero,.** then
507a0 20 74 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64   the page should
507b0 20 62 65 20 65 76 69 63 74 65 64 20 66 72 6f 6d   be evicted from
507c0 20 74 68 65 20 63 61 63 68 65 2e 20 49 6e 20 74   the cache. In t
507d0 68 69 73 20 63 61 73 65 20 53 51 4c 69 74 65 20  his case SQLite 
507e0 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74  .** assumes that
507f0 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74   the next time t
50800 68 65 20 70 61 67 65 20 69 73 20 72 65 74 72 69  he page is retri
50810 65 76 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61  eved from the ca
50820 63 68 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65  che using.** the
50830 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64   xFetch() method
50840 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 7a 65 72  , it will be zer
50850 6f 65 64 2e 20 49 66 20 74 68 65 20 64 69 73 63  oed. If the disc
50860 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  ard parameter is
50870 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  .** zero, then t
50880 68 65 20 70 61 67 65 20 69 73 20 63 6f 6e 73 69  he page is consi
50890 64 65 72 65 64 20 74 6f 20 62 65 20 75 6e 70 69  dered to be unpi
508a0 6e 6e 65 64 2e 20 54 68 65 20 63 61 63 68 65 20  nned. The cache 
508b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
508c0 2a 20 6d 61 79 20 63 68 6f 6f 73 65 20 74 6f 20  * may choose to 
508d0 72 65 63 6c 61 69 6d 20 28 66 72 65 65 20 6f 72  reclaim (free or
508e0 20 72 65 63 79 63 6c 65 29 20 75 6e 70 69 6e 6e   recycle) unpinn
508f0 65 64 20 70 61 67 65 73 20 61 74 20 61 6e 79 20  ed pages at any 
50900 74 69 6d 65 2e 0a 2a 2a 20 53 51 4c 69 74 65 20  time..** SQLite 
50910 61 73 73 75 6d 65 73 20 74 68 61 74 20 6e 65 78  assumes that nex
50920 74 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 20  t time the page 
50930 69 73 20 72 65 74 72 69 65 76 65 64 20 66 72 6f  is retrieved fro
50940 6d 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69  m the cache.** i
50950 74 20 77 69 6c 6c 20 65 69 74 68 65 72 20 62 65  t will either be
50960 20 7a 65 72 6f 65 64 2c 20 6f 72 20 63 6f 6e 74   zeroed, or cont
50970 61 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74  ain the same dat
50980 61 20 74 68 61 74 20 69 74 20 64 69 64 20 77 68  a that it did wh
50990 65 6e 20 69 74 0a 2a 2a 20 77 61 73 20 75 6e 70  en it.** was unp
509a0 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  inned..**.** The
509b0 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 72 65   cache is not re
509c0 71 75 69 72 65 64 20 74 6f 20 70 65 72 66 6f 72  quired to perfor
509d0 6d 20 61 6e 79 20 72 65 66 65 72 65 6e 63 65 20  m any reference 
509e0 63 6f 75 6e 74 69 6e 67 2e 20 41 20 73 69 6e 67  counting. A sing
509f0 6c 65 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78  le .** call to x
50a00 55 6e 70 69 6e 28 29 20 75 6e 70 69 6e 73 20 74  Unpin() unpins t
50a10 68 65 20 70 61 67 65 20 72 65 67 61 72 64 6c 65  he page regardle
50a20 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  ss of the number
50a30 20 6f 66 20 70 72 69 6f 72 20 63 61 6c 6c 73 20   of prior calls 
50a40 0a 2a 2a 20 74 6f 20 78 46 65 74 63 68 28 29 2e  .** to xFetch().
50a50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 65 6b 65  .**.** The xReke
50a60 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73  y() method is us
50a70 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  ed to change the
50a80 20 6b 65 79 20 76 61 6c 75 65 20 61 73 73 6f 63   key value assoc
50a90 69 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 2a  iated with the.*
50aa0 2a 20 70 61 67 65 20 70 61 73 73 65 64 20 61 73  * page passed as
50ab0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
50ac0 6d 65 6e 74 20 66 72 6f 6d 20 6f 6c 64 4b 65 79  ment from oldKey
50ad0 20 74 6f 20 6e 65 77 4b 65 79 2e 20 49 66 20 74   to newKey. If t
50ae0 68 65 20 63 61 63 68 65 0a 2a 2a 20 70 72 65 76  he cache.** prev
50af0 69 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e 73 20  iously contains 
50b00 61 6e 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61  an entry associa
50b10 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 2c  ted with newKey,
50b20 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a   it should be.**
50b30 20 64 69 73 63 61 72 64 65 64 2e 20 41 6e 79 20   discarded. Any 
50b40 70 72 69 6f 72 20 63 61 63 68 65 20 65 6e 74 72  prior cache entr
50b50 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
50b60 68 20 6e 65 77 4b 65 79 20 69 73 20 67 75 61 72  h newKey is guar
50b70 61 6e 74 65 65 64 20 6e 6f 74 0a 2a 2a 20 74 6f  anteed not.** to
50b80 20 62 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a   be pinned..**.*
50b90 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 20 63 61  * When SQLite ca
50ba0 6c 6c 73 20 74 68 65 20 78 54 72 75 6e 63 61 74  lls the xTruncat
50bb0 65 28 29 20 6d 65 74 68 6f 64 2c 20 74 68 65 20  e() method, the 
50bc0 63 61 63 68 65 20 6d 75 73 74 20 64 69 73 63 61  cache must disca
50bd0 72 64 20 61 6c 6c 0a 2a 2a 20 65 78 69 73 74 69  rd all.** existi
50be0 6e 67 20 63 61 63 68 65 20 65 6e 74 72 69 65 73  ng cache entries
50bf0 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65   with page numbe
50c00 72 73 20 28 6b 65 79 73 29 20 67 72 65 61 74 65  rs (keys) greate
50c10 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a  r than or equal.
50c20 2a 2a 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  ** to the value 
50c30 6f 66 20 74 68 65 20 69 4c 69 6d 69 74 20 70 61  of the iLimit pa
50c40 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74  rameter passed t
50c50 6f 20 78 54 72 75 6e 63 61 74 65 28 29 2e 20 49  o xTruncate(). I
50c60 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73  f any.** of thes
50c70 65 20 70 61 67 65 73 20 61 72 65 20 70 69 6e 6e  e pages are pinn
50c80 65 64 2c 20 74 68 65 79 20 61 72 65 20 69 6d 70  ed, they are imp
50c90 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64  licitly unpinned
50ca0 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 0a 2a  , meaning that.*
50cb0 2a 20 74 68 65 79 20 63 61 6e 20 62 65 20 73 61  * they can be sa
50cc0 66 65 6c 79 20 64 69 73 63 61 72 64 65 64 2e 0a  fely discarded..
50cd0 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 73 74 72  **.** The xDestr
50ce0 6f 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75  oy() method is u
50cf0 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20  sed to delete a 
50d00 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  cache allocated 
50d10 62 79 20 78 43 72 65 61 74 65 28 29 2e 0a 2a 2a  by xCreate()..**
50d20 20 41 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61   All resources a
50d30 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
50d40 68 65 20 73 70 65 63 69 66 69 65 64 20 63 61 63  he specified cac
50d50 68 65 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65  he should be fre
50d60 65 64 2e 20 41 66 74 65 72 0a 2a 2a 20 63 61 6c  ed. After.** cal
50d70 6c 69 6e 67 20 74 68 65 20 78 44 65 73 74 72 6f  ling the xDestro
50d80 79 28 29 20 6d 65 74 68 6f 64 2c 20 53 51 4c 69  y() method, SQLi
50d90 74 65 20 63 6f 6e 73 69 64 65 72 73 20 74 68 65  te considers the
50da0 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65   [sqlite3_pcache
50db0 2a 5d 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 6e 76  *].** handle inv
50dc0 61 6c 69 64 2c 20 61 6e 64 20 77 69 6c 6c 20 6e  alid, and will n
50dd0 6f 74 20 75 73 65 20 69 74 20 77 69 74 68 20 61  ot use it with a
50de0 6e 79 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33  ny other sqlite3
50df0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 0a  _pcache_methods.
50e00 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f  ** functions..*/
50e10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
50e20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
50e30 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 70  ethods sqlite3_p
50e40 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3b 0a 73  cache_methods;.s
50e50 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63  truct sqlite3_pc
50e60 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 7b 0a 20  ache_methods {. 
50e70 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 69   void *pArg;.  i
50e80 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64  nt (*xInit)(void
50e90 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 53 68  *);.  void (*xSh
50ea0 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 0a  utdown)(void*);.
50eb0 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65    sqlite3_pcache
50ec0 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69 6e 74   *(*xCreate)(int
50ed0 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 50 75   szPage, int bPu
50ee0 72 67 65 61 62 6c 65 29 3b 0a 20 20 76 6f 69 64  rgeable);.  void
50ef0 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29 28 73   (*xCachesize)(s
50f00 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20  qlite3_pcache*, 
50f10 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65 29 3b  int nCachesize);
50f20 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65 63 6f  .  int (*xPageco
50f30 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70 63 61  unt)(sqlite3_pca
50f40 63 68 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28  che*);.  void *(
50f50 2a 78 46 65 74 63 68 29 28 73 71 6c 69 74 65 33  *xFetch)(sqlite3
50f60 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e  _pcache*, unsign
50f70 65 64 20 6b 65 79 2c 20 69 6e 74 20 63 72 65 61  ed key, int crea
50f80 74 65 46 6c 61 67 29 3b 0a 20 20 76 6f 69 64 20  teFlag);.  void 
50f90 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69 74 65  (*xUnpin)(sqlite
50fa0 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a  3_pcache*, void*
50fb0 2c 20 69 6e 74 20 64 69 73 63 61 72 64 29 3b 0a  , int discard);.
50fc0 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65 79 29    void (*xRekey)
50fd0 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
50fe0 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65  , void*, unsigne
50ff0 64 20 6f 6c 64 4b 65 79 2c 20 75 6e 73 69 67 6e  d oldKey, unsign
51000 65 64 20 6e 65 77 4b 65 79 29 3b 0a 20 20 76 6f  ed newKey);.  vo
51010 69 64 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28  id (*xTruncate)(
51020 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c  sqlite3_pcache*,
51030 20 75 6e 73 69 67 6e 65 64 20 69 4c 69 6d 69 74   unsigned iLimit
51040 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73  );.  void (*xDes
51050 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 70 63  troy)(sqlite3_pc
51060 61 63 68 65 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  ache*);.};../*.*
51070 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20  * Undo the hack 
51080 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c  that converts fl
51090 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70  oating point typ
510a0 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f  es to integer fo
510b0 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70  r.** builds on p
510c0 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75  rocessors withou
510d0 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  t floating point
510e0 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66   support..*/.#if
510f0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
51100 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23  FLOATING_POINT.#
51110 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65   undef double.#e
51120 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f  ndif..#if 0.}  /
51130 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78  * End of the 'ex
51140 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20  tern "C"' block 
51150 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  */.#endif.#endif
51160 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
51170 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33  * End of sqlite3
51180 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
51190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
511a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
511b0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
511c0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
511d0 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
511e0 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
511f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51200 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
51210 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68  * Include hash.h
51220 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
51230 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
51240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51250 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
51260 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73  * Begin file has
51270 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.h ************
51280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
512a0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
512b0 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20  tember 22.**.** 
512c0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
512d0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
512e0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
512f0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
51300 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
51310 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
51320 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
51330 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
51340 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
51350 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
51360 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
51370 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
51380 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
51390 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
513a0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
513b0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
513c0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
513d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
513e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
513f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51410 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
51420 20 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66   is the header f
51430 69 6c 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65  ile for the gene
51440 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69  ric hash-table i
51450 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20  mplemenation.** 
51460 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a  used in SQLite..
51470 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e  **.** $Id: hash.
51480 68 2c 76 20 31 2e 31 32 20 32 30 30 38 2f 31 30  h,v 1.12 2008/10
51490 2f 31 30 20 31 37 3a 34 31 3a 32 39 20 64 72 68  /10 17:41:29 drh
514a0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
514b0 66 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48  f _SQLITE_HASH_H
514c0 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
514d0 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f  E_HASH_H_../* Fo
514e0 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
514f0 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 73  ns of structures
51500 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
51510 75 63 74 20 48 61 73 68 20 48 61 73 68 3b 0a 74  uct Hash Hash;.t
51520 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48 61  ypedef struct Ha
51530 73 68 45 6c 65 6d 20 48 61 73 68 45 6c 65 6d 3b  shElem HashElem;
51540 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20  ../* A complete 
51550 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e  hash table is an
51560 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
51570 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
51580 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74  ture..** The int
51590 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73  ernals of this s
515a0 74 72 75 63 74 75 72 65 20 61 72 65 20 69 6e 74  tructure are int
515b0 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
515c0 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20  ue -- client.** 
515d0 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  code should not 
515e0 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73  attempt to acces
515f0 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20  s or modify the 
51600 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73  fields of this s
51610 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65  tructure.** dire
51620 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68  ctly.  Change th
51630 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c  is structure onl
51640 79 20 62 79 20 75 73 69 6e 67 20 74 68 65 20 72  y by using the r
51650 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a  outines below..*
51660 2a 20 48 6f 77 65 76 65 72 2c 20 6d 61 6e 79 20  * However, many 
51670 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64 75 72  of the "procedur
51680 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f  es" and "functio
51690 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e  ns" for modifyin
516a0 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69  g and.** accessi
516b0 6e 67 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ng this structur
516c0 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63  e are really mac
516d0 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74  ros, so we can't
516e0 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20   really make.** 
516f0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
51700 70 61 71 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  paque..*/.struct
51710 20 48 61 73 68 20 7b 0a 20 20 75 6e 73 69 67 6e   Hash {.  unsign
51720 65 64 20 69 6e 74 20 63 6f 70 79 4b 65 79 3a 20  ed int copyKey: 
51730 31 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63  1;  /* True if c
51740 6f 70 79 20 6f 66 20 6b 65 79 20 6d 61 64 65 20  opy of key made 
51750 6f 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 75  on insert */.  u
51760 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 74 73 69  nsigned int htsi
51770 7a 65 20 3a 20 33 31 3b 20 2f 2a 20 4e 75 6d 62  ze : 31; /* Numb
51780 65 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e  er of buckets in
51790 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
517a0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
517b0 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f  t count;       /
517c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
517d0 69 65 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c  ies in this tabl
517e0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
517f0 2a 66 69 72 73 74 3b 20 20 20 20 20 20 20 20 20  *first;         
51800 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 65 6c   /* The first el
51810 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72  ement of the arr
51820 61 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f  ay */.  struct _
51830 68 74 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  ht {            
51840 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61    /* the hash ta
51850 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63  ble */.    int c
51860 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
51870 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
51880 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 20  of entries with 
51890 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 20  this hash */.   
518a0 20 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e   HashElem *chain
518b0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ;           /* P
518c0 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20  ointer to first 
518d0 65 6e 74 72 79 20 77 69 74 68 20 74 68 69 73 20  entry with this 
518e0 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b  hash */.  } *ht;
518f0 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65  .};../* Each ele
51900 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68  ment in the hash
51910 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73   table is an ins
51920 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
51930 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63  lowing .** struc
51940 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65  ture.  All eleme
51950 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f  nts are stored o
51960 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c  n a single doubl
51970 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a  y-linked list..*
51980 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73  *.** Again, this
51990 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e   structure is in
519a0 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61  tended to be opa
519b0 71 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27  que, but it can'
519c0 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f  t really.** be o
519d0 70 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74  paque because it
519e0 20 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72   is used by macr
519f0 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61  os..*/.struct Ha
51a00 73 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45  shElem {.  HashE
51a10 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76  lem *next, *prev
51a20 3b 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20  ;   /* Next and 
51a30 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74  previous element
51a40 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a  s in the table *
51a50 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 20  /.  void *data; 
51a60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
51a70 44 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20  Data associated 
51a80 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e  with this elemen
51a90 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65  t */.  void *pKe
51aa0 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20  y; int nKey;    
51ab0 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65  /* Key associate
51ac0 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d  d with this elem
51ad0 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ent */.};../*.**
51ae0 20 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73   Access routines
51af0 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e  .  To delete, in
51b00 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  sert a NULL poin
51b10 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ter..*/.SQLITE_P
51b20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
51b30 74 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 68  te3HashInit(Hash
51b40 2a 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 3b  *, int copyKey);
51b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
51b60 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73  void *sqlite3Has
51b70 68 49 6e 73 65 72 74 28 48 61 73 68 2a 2c 20 63  hInsert(Hash*, c
51b80 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
51b90 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20   int nKey, void 
51ba0 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f  *pData);.SQLITE_
51bb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
51bc0 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f  lite3HashFind(co
51bd0 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 74  nst Hash*, const
51be0 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
51bf0 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50   nKey);.SQLITE_P
51c00 52 49 56 41 54 45 20 48 61 73 68 45 6c 65 6d 20  RIVATE HashElem 
51c10 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64  *sqlite3HashFind
51c20 45 6c 65 6d 28 63 6f 6e 73 74 20 48 61 73 68 2a  Elem(const Hash*
51c30 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
51c40 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53  ey, int nKey);.S
51c50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
51c60 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  id sqlite3HashCl
51c70 65 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a  ear(Hash*);../*.
51c80 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f  ** Macros for lo
51c90 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65  oping over all e
51ca0 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73  lements of a has
51cb0 68 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64  h table.  The id
51cc0 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74  iom is.** like t
51cd0 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73  his:.**.**   Has
51ce0 68 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c  h h;.**   HashEl
51cf0 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a  em *p;.**   ....
51d00 2a 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74  **   for(p=sqlit
51d10 65 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20  eHashFirst(&h); 
51d20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e  p; p=sqliteHashN
51d30 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20  ext(p)){.**     
51d40 53 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70  SomeStructure *p
51d50 44 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73  Data = sqliteHas
51d60 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20  hData(p);.**    
51d70 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67   // do something
51d80 20 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20   with pData.**  
51d90 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71   }.*/.#define sq
51da0 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29  liteHashFirst(H)
51db0 20 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23    ((H)->first).#
51dc0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73  define sqliteHas
51dd0 68 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d  hNext(E)   ((E)-
51de0 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73  >next).#define s
51df0 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29  qliteHashData(E)
51e00 20 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 23     ((E)->data).#
51e10 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73  define sqliteHas
51e20 68 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d  hKey(E)    ((E)-
51e30 3e 70 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 73  >pKey).#define s
51e40 71 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65  qliteHashKeysize
51e50 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a  (E) ((E)->nKey).
51e60 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66  ./*.** Number of
51e70 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61   entries in a ha
51e80 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66  sh table.*/.#def
51e90 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 43 6f  ine sqliteHashCo
51ea0 75 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f  unt(H)  ((H)->co
51eb0 75 6e 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  unt)..#endif /* 
51ec0 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 20  _SQLITE_HASH_H_ 
51ed0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
51ee0 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e  *** End of hash.
51ef0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
51f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51f20 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
51f30 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
51f40 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
51f50 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
51f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51f70 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
51f80 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 72 73  *** Include pars
51f90 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  e.h in the middl
51fa0 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
51fb0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
51fc0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
51fd0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
51fe0 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  arse.h *********
51ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52010 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53  **/.#define TK_S
52020 45 4d 49 20 20 20 20 20 20 20 20 20 20 20 20 20  EMI             
52030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
52040 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 50 4c  .#define TK_EXPL
52050 41 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  AIN             
52060 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64              2.#d
52070 65 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 20  efine TK_QUERY  
52080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52090 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69           3.#defi
520a0 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 20  ne TK_PLAN      
520b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
520c0 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20        4.#define 
520d0 54 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20 20  TK_BEGIN        
520e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
520f0 20 20 20 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f     5.#define TK_
52100 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20  TRANSACTION     
52110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52120 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46  6.#define TK_DEF
52130 45 52 52 45 44 20 20 20 20 20 20 20 20 20 20 20  ERRED           
52140 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23               7.#
52150 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44 49  define TK_IMMEDI
52160 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ATE             
52170 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66            8.#def
52180 69 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 56 45  ine TK_EXCLUSIVE
52190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
521a0 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65         9.#define
521b0 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20 20   TK_COMMIT      
521c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
521d0 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 54 4b     10.#define TK
521e0 5f 45 4e 44 20 20 20 20 20 20 20 20 20 20 20 20  _END            
521f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52200 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f  11.#define TK_RO
52210 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 20  LLBACK          
52220 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 0a               12.
52230 23 64 65 66 69 6e 65 20 54 4b 5f 53 41 56 45 50  #define TK_SAVEP
52240 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20  OINT            
52250 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65            13.#de
52260 66 69 6e 65 20 54 4b 5f 52 45 4c 45 41 53 45 20  fine TK_RELEASE 
52270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52280 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e         14.#defin
52290 65 20 54 4b 5f 54 4f 20 20 20 20 20 20 20 20 20  e TK_TO         
522a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
522b0 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 54      15.#define T
522c0 4b 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 20  K_CREATE        
522d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
522e0 20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54   16.#define TK_T
522f0 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  ABLE            
52300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37                17
52310 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 46 20 20  .#define TK_IF  
52320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52330 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64             18.#d
52340 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 20 20 20 20  efine TK_NOT    
52350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52360 20 20 20 20 20 20 20 20 31 39 0a 23 64 65 66 69          19.#defi
52370 6e 65 20 54 4b 5f 45 58 49 53 54 53 20 20 20 20  ne TK_EXISTS    
52380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52390 20 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20       20.#define 
523a0 54 4b 5f 54 45 4d 50 20 20 20 20 20 20 20 20 20  TK_TEMP         
523b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
523c0 20 20 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    21.#define TK_
523d0 4c 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LP              
523e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
523f0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 50 20  2.#define TK_RP 
52400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52410 20 20 20 20 20 20 20 20 20 20 20 20 32 33 0a 23              23.#
52420 64 65 66 69 6e 65 20 54 4b 5f 41 53 20 20 20 20  define TK_AS    
52430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52440 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 66           24.#def
52450 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41 20 20 20 20  ine TK_COMMA    
52460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52470 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65        25.#define
52480 20 54 4b 5f 49 44 20 20 20 20 20 20 20 20 20 20   TK_ID          
52490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
524a0 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 54 4b     26.#define TK
524b0 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 20 20  _ABORT          
524c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
524d0 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 46  27.#define TK_AF
524e0 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  TER             
524f0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a               28.
52500 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 41 4c 59  #define TK_ANALY
52510 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ZE              
52520 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65            29.#de
52530 66 69 6e 65 20 54 4b 5f 41 53 43 20 20 20 20 20  fine TK_ASC     
52540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52550 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e         30.#defin
52560 65 20 54 4b 5f 41 54 54 41 43 48 20 20 20 20 20  e TK_ATTACH     
52570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52580 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20 54      31.#define T
52590 4b 5f 42 45 46 4f 52 45 20 20 20 20 20 20 20 20  K_BEFORE        
525a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
525b0 20 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   32.#define TK_C
525c0 41 53 43 41 44 45 20 20 20 20 20 20 20 20 20 20  ASCADE          
525d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33                33
525e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 54  .#define TK_CAST
525f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52600 20 20 20 20 20 20 20 20 20 20 20 33 34 0a 23 64             34.#d
52610 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43  efine TK_CONFLIC
52620 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
52630 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69          35.#defi
52640 6e 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20  ne TK_DATABASE  
52650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52660 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20       36.#define 
52670 54 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20  TK_DESC         
52680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52690 20 20 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    37.#define TK_
526a0 44 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20  DETACH          
526b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
526c0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43  8.#define TK_EAC
526d0 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
526e0 20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23              39.#
526f0 64 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20  define TK_FAIL  
52700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52710 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 65 66           40.#def
52720 69 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20  ine TK_FOR      
52730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52740 20 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65        41.#define
52750 20 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20   TK_IGNORE      
52760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52770 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 54 4b     42.#define TK
52780 5f 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20  _INITIALLY      
52790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
527a0 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  43.#define TK_IN
527b0 53 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20  STEAD           
527c0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 34 0a               44.
527d0 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f  #define TK_LIKE_
527e0 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  KW              
527f0 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65            45.#de
52800 66 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20  fine TK_MATCH   
52810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52820 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e         46.#defin
52830 65 20 54 4b 5f 4b 45 59 20 20 20 20 20 20 20 20  e TK_KEY        
52840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52850 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20 54      47.#define T
52860 4b 5f 4f 46 20 20 20 20 20 20 20 20 20 20 20 20  K_OF            
52870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52880 20 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f   48.#define TK_O
52890 46 46 53 45 54 20 20 20 20 20 20 20 20 20 20 20  FFSET           
528a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39                49
528b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 41 47  .#define TK_PRAG
528c0 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MA              
528d0 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 64             50.#d
528e0 65 66 69 6e 65 20 54 4b 5f 52 41 49 53 45 20 20  efine TK_RAISE  
528f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52900 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 66 69          51.#defi
52910 6e 65 20 54 4b 5f 52 45 50 4c 41 43 45 20 20 20  ne TK_REPLACE   
52920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52930 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20       52.#define 
52940 54 4b 5f 52 45 53 54 52 49 43 54 20 20 20 20 20  TK_RESTRICT     
52950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52960 20 20 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    53.#define TK_
52970 52 4f 57 20 20 20 20 20 20 20 20 20 20 20 20 20  ROW             
52980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
52990 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 49  4.#define TK_TRI
529a0 47 47 45 52 20 20 20 20 20 20 20 20 20 20 20 20  GGER            
529b0 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 23              55.#
529c0 64 65 66 69 6e 65 20 54 4b 5f 56 41 43 55 55 4d  define TK_VACUUM
529d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
529e0 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66           56.#def
529f0 69 6e 65 20 54 4b 5f 56 49 45 57 20 20 20 20 20  ine TK_VIEW     
52a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52a10 20 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65        57.#define
52a20 20 54 4b 5f 56 49 52 54 55 41 4c 20 20 20 20 20   TK_VIRTUAL     
52a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52a40 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 54 4b     58.#define TK
52a50 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20  _REINDEX        
52a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52a70 35 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  59.#define TK_RE
52a80 4e 41 4d 45 20 20 20 20 20 20 20 20 20 20 20 20  NAME            
52a90 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a               60.
52aa0 23 64 65 66 69 6e 65 20 54 4b 5f 43 54 49 4d 45  #define TK_CTIME
52ab0 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20  _KW             
52ac0 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65            61.#de
52ad0 66 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20 20 20  fine TK_ANY     
52ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52af0 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e         62.#defin
52b00 65 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 20 20  e TK_OR         
52b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52b20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 54      63.#define T
52b30 4b 5f 41 4e 44 20 20 20 20 20 20 20 20 20 20 20  K_AND           
52b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52b50 20 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49   64.#define TK_I
52b60 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  S               
52b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35                65
52b80 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 54 57  .#define TK_BETW
52b90 45 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  EEN             
52ba0 20 20 20 20 20 20 20 20 20 20 20 36 36 0a 23 64             66.#d
52bb0 65 66 69 6e 65 20 54 4b 5f 49 4e 20 20 20 20 20  efine TK_IN     
52bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52bd0 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 66 69          67.#defi
52be0 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20  ne TK_ISNULL    
52bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52c00 20 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20       68.#define 
52c10 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20  TK_NOTNULL      
52c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52c30 20 20 36 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    69.#define TK_
52c40 4e 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NE              
52c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
52c60 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 51 20  0.#define TK_EQ 
52c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52c80 20 20 20 20 20 20 20 20 20 20 20 20 37 31 0a 23              71.#
52c90 64 65 66 69 6e 65 20 54 4b 5f 47 54 20 20 20 20  define TK_GT    
52ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52cb0 20 20 20 20 20 20 20 20 20 37 32 0a 23 64 65 66           72.#def
52cc0 69 6e 65 20 54 4b 5f 4c 45 20 20 20 20 20 20 20  ine TK_LE       
52cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52ce0 20 20 20 20 20 20 37 33 0a 23 64 65 66 69 6e 65        73.#define
52cf0 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 20   TK_LT          
52d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d10 20 20 20 37 34 0a 23 64 65 66 69 6e 65 20 54 4b     74.#define TK
52d20 5f 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20  _GE             
52d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d40 37 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 53  75.#define TK_ES
52d50 43 41 50 45 20 20 20 20 20 20 20 20 20 20 20 20  CAPE            
52d60 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 0a               76.
52d70 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 41 4e  #define TK_BITAN
52d80 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
52d90 20 20 20 20 20 20 20 20 20 20 37 37 0a 23 64 65            77.#de
52da0 66 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20 20 20  fine TK_BITOR   
52db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52dc0 20 20 20 20 20 20 20 37 38 0a 23 64 65 66 69 6e         78.#defin
52dd0 65 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 20 20  e TK_LSHIFT     
52de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52df0 20 20 20 20 37 39 0a 23 64 65 66 69 6e 65 20 54      79.#define T
52e00 4b 5f 52 53 48 49 46 54 20 20 20 20 20 20 20 20  K_RSHIFT        
52e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52e20 20 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50   80.#define TK_P
52e30 4c 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20  LUS             
52e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31                81
52e50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 49 4e 55  .#define TK_MINU
52e60 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  S               
52e70 20 20 20 20 20 20 20 20 20 20 20 38 32 0a 23 64             82.#d
52e80 65 66 69 6e 65 20 54 4b 5f 53 54 41 52 20 20 20  efine TK_STAR   
52e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52ea0 20 20 20 20 20 20 20 20 38 33 0a 23 64 65 66 69          83.#defi
52eb0 6e 65 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 20  ne TK_SLASH     
52ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52ed0 20 20 20 20 20 38 34 0a 23 64 65 66 69 6e 65 20       84.#define 
52ee0 54 4b 5f 52 45 4d 20 20 20 20 20 20 20 20 20 20  TK_REM          
52ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52f00 20 20 38 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    85.#define TK_
52f10 43 4f 4e 43 41 54 20 20 20 20 20 20 20 20 20 20  CONCAT          
52f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
52f30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c  6.#define TK_COL
52f40 4c 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20  LATE            
52f50 20 20 20 20 20 20 20 20 20 20 20 20 38 37 0a 23              87.#
52f60 64 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53  define TK_UMINUS
52f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52f80 20 20 20 20 20 20 20 20 20 38 38 0a 23 64 65 66           88.#def
52f90 69 6e 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20  ine TK_UPLUS    
52fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52fb0 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65        89.#define
52fc0 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 20 20 20   TK_BITNOT      
52fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52fe0 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20 54 4b     90.#define TK
52ff0 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20  _STRING         
53000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53010 39 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f  91.#define TK_JO
53020 49 4e 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20  IN_KW           
53030 20 20 20 20 20 20 20 20 20 20 20 20 20 39 32 0a               92.
53040 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54  #define TK_CONST
53050 52 41 49 4e 54 20 20 20 20 20 20 20 20 20 20 20  RAINT           
53060 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 64 65            93.#de
53070 66 69 6e 65 20 54 4b 5f 44 45 46 41 55 4c 54 20  fine TK_DEFAULT 
53080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53090 20 20 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e         94.#defin
530a0 65 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20  e TK_NULL       
530b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
530c0 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 54      95.#define T
530d0 4b 5f 50 52 49 4d 41 52 59 20 20 20 20 20 20 20  K_PRIMARY       
530e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
530f0 20 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55   96.#define TK_U
53100 4e 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20  NIQUE           
53110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 37                97
53120 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43  .#define TK_CHEC
53130 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K               
53140 20 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64             98.#d
53150 65 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45 4e  efine TK_REFEREN
53160 43 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20  CES             
53170 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69          99.#defi
53180 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52 20 20  ne TK_AUTOINCR  
53190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
531a0 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65       100.#define
531b0 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20 20 20   TK_ON          
531c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
531d0 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 54     101.#define T
531e0 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20  K_DELETE        
531f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53200 20 31 30 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   102.#define TK_
53210 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20  UPDATE          
53220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
53230 30 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  03.#define TK_IN
53240 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20 20  SERT            
53250 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34               104
53260 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 54 20  .#define TK_SET 
53270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53280 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23             105.#
53290 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52  define TK_DEFERR
532a0 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  ABLE            
532b0 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 64 65           106.#de
532c0 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20  fine TK_FOREIGN 
532d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
532e0 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 69         107.#defi
532f0 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20 20 20  ne TK_DROP      
53300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53310 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65       108.#define
53320 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20   TK_UNION       
53330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53340 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 54     109.#define T
53350 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20  K_ALL           
53360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53370 20 31 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   110.#define TK_
53380 45 58 43 45 50 54 20 20 20 20 20 20 20 20 20 20  EXCEPT          
53390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
533a0 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  11.#define TK_IN
533b0 54 45 52 53 45 43 54 20 20 20 20 20 20 20 20 20  TERSECT         
533c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 32               112
533d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45  .#define TK_SELE
533e0 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CT              
533f0 20 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 23             113.#
53400 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e  define TK_DISTIN
53410 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CT              
53420 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65           114.#de
53430 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20 20  fine TK_DOT     
53440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53450 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69         115.#defi
53460 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20  ne TK_FROM      
53470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53480 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65       116.#define
53490 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20   TK_JOIN        
534a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
534b0 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 54     117.#define T
534c0 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20 20  K_INDEXED       
534d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
534e0 20 31 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f   118.#define TK_
534f0 42 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  BY              
53500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
53510 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 53  19.#define TK_US
53520 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20  ING             
53530 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 30               120
53540 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 44 45  .#define TK_ORDE
53550 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
53560 20 20 20 20 20 20 20 20 20 20 20 31 32 31 0a 23             121.#
53570 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55 50 20  define TK_GROUP 
53580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53590 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65           122.#de
535a0 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47 20 20  fine TK_HAVING  
535b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
535c0 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 66 69         123.#defi
535d0 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20 20 20  ne TK_LIMIT     
535e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
535f0 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65       124.#define
53600 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20 20 20   TK_WHERE       
53610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53620 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20 54     125.#define T
53630 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20 20 20  K_INTO          
53640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53650 20 31 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   126.#define TK_
53660 56 41 4c 55 45 53 20 20 20 20 20 20 20 20 20 20  VALUES          
53670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
53680 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  27.#define TK_IN
53690 54 45 47 45 52 20 20 20 20 20 20 20 20 20 20 20  TEGER           
536a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38               128
536b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 4f 41  .#define TK_FLOA
536c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
536d0 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 23             129.#
536e0 64 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 20 20  define TK_BLOB  
536f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53700 20 20 20 20 20 20 20 20 20 31 33 30 0a 23 64 65           130.#de
53710 66 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 45 52  fine TK_REGISTER
53720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53730 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69         131.#defi
53740 6e 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 20 20  ne TK_VARIABLE  
53750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53760 20 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65       132.#define
53770 20 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20 20   TK_CASE        
53780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53790 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 54     133.#define T
537a0 4b 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20 20  K_WHEN          
537b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
537c0 20 31 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f   134.#define TK_
537d0 54 48 45 4e 20 20 20 20 20 20 20 20 20 20 20 20  THEN            
537e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
537f0 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4c  35.#define TK_EL
53800 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SE              
53810 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36               136
53820 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45  .#define TK_INDE
53830 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  X               
53840 20 20 20 20 20 20 20 20 20 20 20 31 33 37 0a 23             137.#
53850 64 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 52 20  define TK_ALTER 
53860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53870 20 20 20 20 20 20 20 20 20 31 33 38 0a 23 64 65           138.#de
53880 66 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 20 20  fine TK_ADD     
53890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
538a0 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69         139.#defi
538b0 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20  ne TK_COLUMNKW  
538c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
538d0 20 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65       140.#define
538e0 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 20 20 20   TK_TO_TEXT     
538f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53900 20 20 20 31 34 31 0a 23 64 65 66 69 6e 65 20 54     141.#define T
53910 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 20 20 20  K_TO_BLOB       
53920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53930 20 31 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   142.#define TK_
53940 54 4f 5f 4e 55 4d 45 52 49 43 20 20 20 20 20 20  TO_NUMERIC      
53950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
53960 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f  43.#define TK_TO
53970 5f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20  _INT            
53980 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 34               144
53990 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 52  .#define TK_TO_R
539a0 45 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  EAL             
539b0 20 20 20 20 20 20 20 20 20 20 20 31 34 35 0a 23             145.#
539c0 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46  define TK_END_OF
539d0 5f 46 49 4c 45 20 20 20 20 20 20 20 20 20 20 20  _FILE           
539e0 20 20 20 20 20 20 20 20 20 31 34 36 0a 23 64 65           146.#de
539f0 66 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20  fine TK_ILLEGAL 
53a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53a10 20 20 20 20 20 20 20 31 34 37 0a 23 64 65 66 69         147.#defi
53a20 6e 65 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20  ne TK_SPACE     
53a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53a40 20 20 20 20 20 31 34 38 0a 23 64 65 66 69 6e 65       148.#define
53a50 20 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52   TK_UNCLOSED_STR
53a60 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20  ING             
53a70 20 20 20 31 34 39 0a 23 64 65 66 69 6e 65 20 54     149.#define T
53a80 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20  K_FUNCTION      
53a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53aa0 20 31 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   150.#define TK_
53ab0 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20  COLUMN          
53ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
53ad0 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47  51.#define TK_AG
53ae0 47 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20  G_FUNCTION      
53af0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 32               152
53b00 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f  .#define TK_AGG_
53b10 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20  COLUMN          
53b20 20 20 20 20 20 20 20 20 20 20 20 31 35 33 0a 23             153.#
53b30 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f  define TK_CONST_
53b40 46 55 4e 43 20 20 20 20 20 20 20 20 20 20 20 20  FUNC            
53b50 20 20 20 20 20 20 20 20 20 31 35 34 0a 0a 2f 2a           154../*
53b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
53b70 64 20 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a  d of parse.h ***
53b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
53bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
53bc0 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
53bd0 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71  e left off in sq
53be0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
53bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69  ************/.#i
53c00 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e  nclude <stdio.h>
53c10 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69  .#include <stdli
53c20 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  b.h>.#include <s
53c30 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64  tring.h>.#includ
53c40 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e  e <assert.h>.#in
53c50 63 6c 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e  clude <stddef.h>
53c60 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69  ../*.** If compi
53c70 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65  ling for a proce
53c80 73 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20  ssor that lacks 
53c90 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73  floating point s
53ca0 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74  upport,.** subst
53cb0 69 74 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f  itute integer fo
53cc0 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74  r floating-point
53cd0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
53ce0 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
53cf0 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64  POINT.# define d
53d00 6f 75 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74  ouble sqlite_int
53d10 36 34 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47  64.# define LONG
53d20 44 4f 55 42 4c 45 5f 54 59 50 45 20 73 71 6c 69  DOUBLE_TYPE sqli
53d30 74 65 5f 69 6e 74 36 34 0a 23 20 69 66 6e 64 65  te_int64.# ifnde
53d40 66 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  f SQLITE_BIG_DBL
53d50 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  .#   define SQLI
53d60 54 45 5f 42 49 47 5f 44 42 4c 20 28 30 78 37 66  TE_BIG_DBL (0x7f
53d70 66 66 66 66 66 66 66 66 66 66 66 66 66 66 29 0a  ffffffffffffff).
53d80 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65  # endif.# define
53d90 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54   SQLITE_OMIT_DAT
53da0 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20  ETIME_FUNCS 1.# 
53db0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d  define SQLITE_OM
53dc0 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64  IT_TRACE 1.# und
53dd0 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f  ef SQLITE_MIXED_
53de0 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f  ENDIAN_64BIT_FLO
53df0 41 54 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  AT.#endif.#ifnde
53e00 66 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  f SQLITE_BIG_DBL
53e10 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
53e20 5f 42 49 47 5f 44 42 4c 20 28 31 65 39 39 29 0a  _BIG_DBL (1e99).
53e30 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 4d  #endif../*.** OM
53e40 49 54 5f 54 45 4d 50 44 42 20 69 73 20 73 65 74  IT_TEMPDB is set
53e50 20 74 6f 20 31 20 69 66 20 53 51 4c 49 54 45 5f   to 1 if SQLITE_
53e60 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 20 64  OMIT_TEMPDB is d
53e70 65 66 69 6e 65 64 2c 20 6f 72 20 30 0a 2a 2a 20  efined, or 0.** 
53e80 61 66 74 65 72 77 61 72 64 2e 20 48 61 76 69 6e  afterward. Havin
53e90 67 20 74 68 69 73 20 6d 61 63 72 6f 20 61 6c 6c  g this macro all
53ea0 6f 77 73 20 75 73 20 74 6f 20 63 61 75 73 65 20  ows us to cause 
53eb0 74 68 65 20 43 20 63 6f 6d 70 69 6c 65 72 20 0a  the C compiler .
53ec0 2a 2a 20 74 6f 20 6f 6d 69 74 20 63 6f 64 65 20  ** to omit code 
53ed0 75 73 65 64 20 62 79 20 54 45 4d 50 20 74 61 62  used by TEMP tab
53ee0 6c 65 73 20 77 69 74 68 6f 75 74 20 6d 65 73 73  les without mess
53ef0 79 20 23 69 66 6e 64 65 66 20 73 74 61 74 65 6d  y #ifndef statem
53f00 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ents..*/.#ifdef 
53f10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50  SQLITE_OMIT_TEMP
53f20 44 42 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f  DB.#define OMIT_
53f30 54 45 4d 50 44 42 20 31 0a 23 65 6c 73 65 0a 23  TEMPDB 1.#else.#
53f40 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50  define OMIT_TEMP
53f50 44 42 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  DB 0.#endif../*.
53f60 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** If the follow
53f70 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74  ing macro is set
53f80 20 74 6f 20 31 2c 20 74 68 65 6e 20 4e 55 4c 4c   to 1, then NULL
53f90 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73   values are cons
53fa0 69 64 65 72 65 64 0a 2a 2a 20 64 69 73 74 69 6e  idered.** distin
53fb0 63 74 20 77 68 65 6e 20 64 65 74 65 72 6d 69 6e  ct when determin
53fc0 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ing whether or n
53fd0 6f 74 20 74 77 6f 20 65 6e 74 72 69 65 73 20 61  ot two entries a
53fe0 72 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 69  re the same.** i
53ff0 6e 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  n a UNIQUE index
54000 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 77  .  This is the w
54010 61 79 20 50 6f 73 74 67 72 65 53 51 4c 2c 20 4f  ay PostgreSQL, O
54020 72 61 63 6c 65 2c 20 44 42 32 2c 20 4d 79 53 51  racle, DB2, MySQ
54030 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 2c 20 61 6e  L,.** OCELOT, an
54040 64 20 46 69 72 65 62 69 72 64 20 61 6c 6c 20 77  d Firebird all w
54050 6f 72 6b 2e 20 20 54 68 65 20 53 51 4c 39 32 20  ork.  The SQL92 
54060 73 70 65 63 20 65 78 70 6c 69 63 69 74 6c 79 20  spec explicitly 
54070 73 61 79 73 20 74 68 69 73 0a 2a 2a 20 69 73 20  says this.** is 
54080 74 68 65 20 77 61 79 20 74 68 69 6e 67 73 20 61  the way things a
54090 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 77 6f  re suppose to wo
540a0 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  rk..**.** If the
540b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
540c0 20 69 73 20 73 65 74 20 74 6f 20 30 2c 20 74 68   is set to 0, th
540d0 65 20 4e 55 4c 4c 73 20 61 72 65 20 69 6e 64 69  e NULLs are indi
540e0 73 74 69 6e 63 74 20 66 6f 72 0a 2a 2a 20 61 20  stinct for.** a 
540f0 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 20 20 49  UNIQUE index.  I
54100 6e 20 74 68 69 73 20 6d 6f 64 65 2c 20 79 6f 75  n this mode, you
54110 20 63 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 61   can only have a
54120 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20 65 6e 74   single NULL ent
54130 72 79 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6c 75  ry.** for a colu
54140 6d 6e 20 64 65 63 6c 61 72 65 64 20 55 4e 49 51  mn declared UNIQ
54150 55 45 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  UE.  This is the
54160 20 77 61 79 20 49 6e 66 6f 72 6d 69 78 20 61 6e   way Informix an
54170 64 20 53 51 4c 20 53 65 72 76 65 72 0a 2a 2a 20  d SQL Server.** 
54180 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  work..*/.#define
54190 20 4e 55 4c 4c 5f 44 49 53 54 49 4e 43 54 5f 46   NULL_DISTINCT_F
541a0 4f 52 5f 55 4e 49 51 55 45 20 31 0a 0a 2f 2a 0a  OR_UNIQUE 1../*.
541b0 2a 2a 20 54 68 65 20 22 66 69 6c 65 20 66 6f 72  ** The "file for
541c0 6d 61 74 22 20 6e 75 6d 62 65 72 20 69 73 20 61  mat" number is a
541d0 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69  n integer that i
541e0 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68  s incremented wh
541f0 65 6e 65 76 65 72 0a 2a 2a 20 74 68 65 20 56 44  enever.** the VD
54200 42 45 2d 6c 65 76 65 6c 20 66 69 6c 65 20 66 6f  BE-level file fo
54210 72 6d 61 74 20 63 68 61 6e 67 65 73 2e 20 20 54  rmat changes.  T
54220 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
54230 72 6f 73 20 64 65 66 69 6e 65 20 74 68 65 0a 2a  ros define the.*
54240 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 66 69  * the default fi
54250 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 6e 65  le format for ne
54260 77 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20  w databases and 
54270 74 68 65 20 6d 61 78 69 6d 75 6d 20 66 69 6c 65  the maximum file
54280 20 66 6f 72 6d 61 74 0a 2a 2a 20 74 68 61 74 20   format.** that 
54290 74 68 65 20 6c 69 62 72 61 72 79 20 63 61 6e 20  the library can 
542a0 72 65 61 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  read..*/.#define
542b0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45   SQLITE_MAX_FILE
542c0 5f 46 4f 52 4d 41 54 20 34 0a 23 69 66 6e 64 65  _FORMAT 4.#ifnde
542d0 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  f SQLITE_DEFAULT
542e0 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 0a 23 20 64  _FILE_FORMAT.# d
542f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
54300 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  AULT_FILE_FORMAT
54310 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   1.#endif../*.**
54320 20 50 72 6f 76 69 64 65 20 61 20 64 65 66 61 75   Provide a defau
54330 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 53 51 4c  lt value for SQL
54340 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 69  ITE_TEMP_STORE i
54350 6e 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74  n case it is not
54360 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 6f 6e   specified.** on
54370 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e   the command-lin
54380 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  e.*/.#ifndef SQL
54390 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 23  ITE_TEMP_STORE.#
543a0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54   define SQLITE_T
543b0 45 4d 50 5f 53 54 4f 52 45 20 31 0a 23 65 6e 64  EMP_STORE 1.#end
543c0 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43 20 64 6f  if../*.** GCC do
543d0 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 74 68  es not define th
543e0 65 20 6f 66 66 73 65 74 6f 66 28 29 20 6d 61 63  e offsetof() mac
543f0 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68 61 76 65  ro so we'll have
54400 20 74 6f 20 64 6f 20 69 74 0a 2a 2a 20 6f 75 72   to do it.** our
54410 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64  selves..*/.#ifnd
54420 65 66 20 6f 66 66 73 65 74 6f 66 0a 23 64 65 66  ef offsetof.#def
54430 69 6e 65 20 6f 66 66 73 65 74 6f 66 28 53 54 52  ine offsetof(STR
54440 55 43 54 55 52 45 2c 46 49 45 4c 44 29 20 28 28  UCTURE,FIELD) ((
54450 69 6e 74 29 28 28 63 68 61 72 2a 29 26 28 28 53  int)((char*)&((S
54460 54 52 55 43 54 55 52 45 2a 29 30 29 2d 3e 46 49  TRUCTURE*)0)->FI
54470 45 4c 44 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ELD)).#endif../*
54480 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
54490 20 69 66 20 74 68 69 73 20 6d 61 63 68 69 6e 65   if this machine
544a0 20 75 73 65 73 20 45 42 43 44 49 43 2e 20 20 28   uses EBCDIC.  (
544b0 59 65 73 2c 20 62 65 6c 69 65 76 65 20 69 74 20  Yes, believe it 
544c0 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68 65 72 65  or.** not, there
544d0 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 63 68 69   are still machi
544e0 6e 65 73 20 6f 75 74 20 74 68 65 72 65 20 74 68  nes out there th
544f0 61 74 20 75 73 65 20 45 42 43 44 49 43 2e 29 0a  at use EBCDIC.).
54500 2a 2f 0a 23 69 66 20 27 41 27 20 3d 3d 20 27 5c  */.#if 'A' == '\
54510 33 30 31 27 0a 23 20 64 65 66 69 6e 65 20 53 51  301'.# define SQ
54520 4c 49 54 45 5f 45 42 43 44 49 43 20 31 0a 23 65  LITE_EBCDIC 1.#e
54530 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  lse.# define SQL
54540 49 54 45 5f 41 53 43 49 49 20 31 0a 23 65 6e 64  ITE_ASCII 1.#end
54550 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 67 65  if../*.** Intege
54560 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 73 69 7a 65  rs of known size
54570 73 2e 20 20 54 68 65 73 65 20 74 79 70 65 64 65  s.  These typede
54580 66 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20  fs might change 
54590 66 6f 72 20 61 72 63 68 69 74 65 63 74 75 72 65  for architecture
545a0 73 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 73  s.** where the s
545b0 69 7a 65 73 20 76 65 72 79 2e 20 20 50 72 65 70  izes very.  Prep
545c0 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20  rocessor macros 
545d0 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 73 6f  are available so
545e0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 79 70   that the.** typ
545f0 65 73 20 63 61 6e 20 62 65 20 63 6f 6e 76 65 6e  es can be conven
54600 69 65 6e 74 6c 79 20 72 65 64 65 66 69 6e 65 64  iently redefined
54610 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 79 70 65   at compile-type
54620 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  .  Like this:.**
54630 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 63 20 27  .**         cc '
54640 2d 44 55 49 4e 54 50 54 52 5f 54 59 50 45 3d 6c  -DUINTPTR_TYPE=l
54650 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 27 20 2e 2e  ong long int' ..
54660 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 55 49 4e  ..*/.#ifndef UIN
54670 54 33 32 5f 54 59 50 45 0a 23 20 69 66 64 65 66  T32_TYPE.# ifdef
54680 20 48 41 56 45 5f 55 49 4e 54 33 32 5f 54 0a 23   HAVE_UINT32_T.#
54690 20 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f    define UINT32_
546a0 54 59 50 45 20 75 69 6e 74 33 32 5f 74 0a 23 20  TYPE uint32_t.# 
546b0 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55  else.#  define U
546c0 49 4e 54 33 32 5f 54 59 50 45 20 75 6e 73 69 67  INT32_TYPE unsig
546d0 6e 65 64 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a  ned int.# endif.
546e0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 55  #endif.#ifndef U
546f0 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64  INT16_TYPE.# ifd
54700 65 66 20 48 41 56 45 5f 55 49 4e 54 31 36 5f 54  ef HAVE_UINT16_T
54710 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31  .#  define UINT1
54720 36 5f 54 59 50 45 20 75 69 6e 74 31 36 5f 74 0a  6_TYPE uint16_t.
54730 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
54740 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 6e 73   UINT16_TYPE uns
54750 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 0a  igned short int.
54760 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  # endif.#endif.#
54770 69 66 6e 64 65 66 20 49 4e 54 31 36 5f 54 59 50  ifndef INT16_TYP
54780 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49  E.# ifdef HAVE_I
54790 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69 6e 65  NT16_T.#  define
547a0 20 49 4e 54 31 36 5f 54 59 50 45 20 69 6e 74 31   INT16_TYPE int1
547b0 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65  6_t.# else.#  de
547c0 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20  fine INT16_TYPE 
547d0 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69  short int.# endi
547e0 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  f.#endif.#ifndef
547f0 20 55 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66   UINT8_TYPE.# if
54800 64 65 66 20 48 41 56 45 5f 55 49 4e 54 38 5f 54  def HAVE_UINT8_T
54810 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 38  .#  define UINT8
54820 5f 54 59 50 45 20 75 69 6e 74 38 5f 74 0a 23 20  _TYPE uint8_t.# 
54830 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55  else.#  define U
54840 49 4e 54 38 5f 54 59 50 45 20 75 6e 73 69 67 6e  INT8_TYPE unsign
54850 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a  ed char.# endif.
54860 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49  #endif.#ifndef I
54870 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66  NT8_TYPE.# ifdef
54880 20 48 41 56 45 5f 49 4e 54 38 5f 54 0a 23 20 20   HAVE_INT8_T.#  
54890 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 50 45  define INT8_TYPE
548a0 20 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23   int8_t.# else.#
548b0 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59    define INT8_TY
548c0 50 45 20 73 69 67 6e 65 64 20 63 68 61 72 0a 23  PE signed char.#
548d0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69   endif.#endif.#i
548e0 66 6e 64 65 66 20 4c 4f 4e 47 44 4f 55 42 4c 45  fndef LONGDOUBLE
548f0 5f 54 59 50 45 0a 23 20 64 65 66 69 6e 65 20 4c  _TYPE.# define L
54900 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 6c  ONGDOUBLE_TYPE l
54910 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69  ong double.#endi
54920 66 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65  f.typedef sqlite
54930 5f 69 6e 74 36 34 20 69 36 34 3b 20 20 20 20 20  _int64 i64;     
54940 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73       /* 8-byte s
54950 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
54960 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f  .typedef sqlite_
54970 75 69 6e 74 36 34 20 75 36 34 3b 20 20 20 20 20  uint64 u64;     
54980 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 75 6e      /* 8-byte un
54990 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
549a0 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 33 32  /.typedef UINT32
549b0 5f 54 59 50 45 20 75 33 32 3b 20 20 20 20 20 20  _TYPE u32;      
549c0 20 20 20 20 20 2f 2a 20 34 2d 62 79 74 65 20 75       /* 4-byte u
549d0 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20  nsigned integer 
549e0 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 31  */.typedef UINT1
549f0 36 5f 54 59 50 45 20 75 31 36 3b 20 20 20 20 20  6_TYPE u16;     
54a00 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 65 20        /* 2-byte 
54a10 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
54a20 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e 54 31   */.typedef INT1
54a30 36 5f 54 59 50 45 20 69 31 36 3b 20 20 20 20 20  6_TYPE i16;     
54a40 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 65         /* 2-byte
54a50 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
54a60 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 38  */.typedef UINT8
54a70 5f 54 59 50 45 20 75 38 3b 20 20 20 20 20 20 20  _TYPE u8;       
54a80 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 74 65 20        /* 1-byte 
54a90 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
54aa0 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e 54 38   */.typedef INT8
54ab0 5f 54 59 50 45 20 69 38 3b 20 20 20 20 20 20 20  _TYPE i8;       
54ac0 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 74 65         /* 1-byte
54ad0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
54ae0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73  */../*.** Macros
54af0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68   to determine wh
54b00 65 74 68 65 72 20 74 68 65 20 6d 61 63 68 69 6e  ether the machin
54b10 65 20 69 73 20 62 69 67 20 6f 72 20 6c 69 74 74  e is big or litt
54b20 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a 2a 20 65 76  le endian,.** ev
54b30 61 6c 75 61 74 65 64 20 61 74 20 72 75 6e 74 69  aluated at runti
54b40 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  me..*/.#ifdef SQ
54b50 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f  LITE_AMALGAMATIO
54b60 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  N.SQLITE_PRIVATE
54b70 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74   const int sqlit
54b80 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6c 73 65  e3one = 1;.#else
54b90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54ba0 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65  const int sqlite
54bb0 33 6f 6e 65 3b 0a 23 65 6e 64 69 66 0a 23 69 66  3one;.#endif.#if
54bc0 20 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c   defined(i386) |
54bd0 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36  | defined(__i386
54be0 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
54bf0 4d 5f 49 58 38 36 29 5c 0a 20 20 20 20 20 20 20  M_IX86)\.       
54c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54c10 20 20 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64        || defined
54c20 28 5f 5f 78 38 36 5f 36 34 29 20 7c 7c 20 64 65  (__x86_64) || de
54c30 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f  fined(__x86_64__
54c40 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
54c50 45 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 30  E_BIGENDIAN    0
54c60 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
54c70 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 31 0a  _LITTLEENDIAN 1.
54c80 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
54c90 55 54 46 31 36 4e 41 54 49 56 45 20 20 53 51 4c  UTF16NATIVE  SQL
54ca0 49 54 45 5f 55 54 46 31 36 4c 45 0a 23 65 6c 73  ITE_UTF16LE.#els
54cb0 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  e.# define SQLIT
54cc0 45 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 28  E_BIGENDIAN    (
54cd0 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74  *(char *)(&sqlit
54ce0 65 33 6f 6e 65 29 3d 3d 30 29 0a 23 20 64 65 66  e3one)==0).# def
54cf0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c  ine SQLITE_LITTL
54d00 45 45 4e 44 49 41 4e 20 28 2a 28 63 68 61 72 20  EENDIAN (*(char 
54d10 2a 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d  *)(&sqlite3one)=
54d20 3d 31 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  =1).# define SQL
54d30 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
54d40 28 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41  (SQLITE_BIGENDIA
54d50 4e 3f 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  N?SQLITE_UTF16BE
54d60 3a 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29  :SQLITE_UTF16LE)
54d70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
54d80 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65  onstants for the
54d90 20 6c 61 72 67 65 73 74 20 61 6e 64 20 73 6d 61   largest and sma
54da0 6c 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 36  llest possible 6
54db0 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
54dc0 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20  egers..** These 
54dd0 6d 61 63 72 6f 73 20 61 72 65 20 64 65 73 69 67  macros are desig
54de0 6e 65 64 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72  ned to work corr
54df0 65 63 74 6c 79 20 6f 6e 20 62 6f 74 68 20 33 32  ectly on both 32
54e00 2d 62 69 74 20 61 6e 64 20 36 34 2d 62 69 74 0a  -bit and 64-bit.
54e10 2a 2a 20 63 6f 6d 70 69 6c 65 72 73 2e 0a 2a 2f  ** compilers..*/
54e20 0a 23 64 65 66 69 6e 65 20 4c 41 52 47 45 53 54  .#define LARGEST
54e30 5f 49 4e 54 36 34 20 20 28 30 78 66 66 66 66 66  _INT64  (0xfffff
54e40 66 66 66 7c 28 28 28 69 36 34 29 30 78 37 66 66  fff|(((i64)0x7ff
54e50 66 66 66 66 66 29 3c 3c 33 32 29 29 0a 23 64 65  fffff)<<32)).#de
54e60 66 69 6e 65 20 53 4d 41 4c 4c 45 53 54 5f 49 4e  fine SMALLEST_IN
54e70 54 36 34 20 28 28 28 69 36 34 29 2d 31 29 20 2d  T64 (((i64)-1) -
54e80 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 0a   LARGEST_INT64).
54e90 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
54ea0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
54eb0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
54ec0 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
54ed0 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a  he busy-handler.
54ee0 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  ** callback for 
54ef0 61 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68  a given sqlite h
54f00 61 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  andle. .**.** Th
54f10 65 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e  e sqlite.busyHan
54f20 64 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74  dler member of t
54f30 68 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74  he sqlite struct
54f40 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75   contains the bu
54f50 73 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66  sy.** callback f
54f60 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  or the database 
54f70 68 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67  handle. Each pag
54f80 65 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68  er opened via th
54f90 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64  e sqlite.** hand
54fa0 6c 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70  le is passed a p
54fb0 6f 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ointer to sqlite
54fc0 2e 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68  .busyHandler. Th
54fd0 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a  e busy-handler.*
54fe0 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75  * callback is cu
54ff0 72 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20  rrently invoked 
55000 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e  only from within
55010 20 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70   pager.c..*/.typ
55020 65 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79  edef struct Busy
55030 48 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64  Handler BusyHand
55040 6c 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79  ler;.struct Busy
55050 48 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20  Handler {.  int 
55060 28 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c  (*xFunc)(void *,
55070 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75  int);  /* The bu
55080 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  sy callback */. 
55090 20 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20   void *pArg;    
550a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
550b0 69 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79  irst arg to busy
550c0 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69   callback */.  i
550d0 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20  nt nBusy;       
550e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63            /* Inc
550f0 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61  remented with ea
55100 63 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a  ch busy call */.
55110 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f  };../*.** Name o
55120 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74  f the master dat
55130 61 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68  abase table.  Th
55140 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73  e master databas
55150 65 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20  e table.** is a 
55160 73 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68  special table th
55170 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d  at holds the nam
55180 65 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65  es and attribute
55190 73 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72  s of all.** user
551a0 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69   tables and indi
551b0 63 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ces..*/.#define 
551c0 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20  MASTER_NAME     
551d0 20 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72    "sqlite_master
551e0 22 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d  ".#define TEMP_M
551f0 41 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c  ASTER_NAME  "sql
55200 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22  ite_temp_master"
55210 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74  ../*.** The root
55220 2d 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73  -page of the mas
55230 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62  ter database tab
55240 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  le..*/.#define M
55250 41 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20  ASTER_ROOT      
55260 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61   1../*.** The na
55270 6d 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61  me of the schema
55280 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69   table..*/.#defi
55290 6e 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  ne SCHEMA_TABLE(
552a0 78 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50  x)  ((!OMIT_TEMP
552b0 44 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50  DB)&&(x==1)?TEMP
552c0 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53  _MASTER_NAME:MAS
552d0 54 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a  TER_NAME)../*.**
552e0 20 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d   A convenience m
552f0 61 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e  acro that return
55300 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
55310 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61  elements in.** a
55320 6e 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66  n array..*/.#def
55330 69 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29  ine ArraySize(X)
55340 20 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f      ((int)(sizeo
55350 66 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d  f(X)/sizeof(X[0]
55360 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  )))../*.** The f
55370 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61  ollowing value a
55380 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d  s a destructor m
55390 65 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69  eans to use sqli
553a0 74 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20  te3DbFree()..** 
553b0 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72  This is an inter
553c0 6e 61 6c 20 65 78 74 65 6e 73 69 6f 6e 20 74 6f  nal extension to
553d0 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 61   SQLITE_STATIC a
553e0 6e 64 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  nd SQLITE_TRANSI
553f0 45 4e 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ENT..*/.#define 
55400 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20  SQLITE_DYNAMIC  
55410 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72   ((sqlite3_destr
55420 75 63 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74  uctor_type)sqlit
55430 65 33 44 62 46 72 65 65 29 0a 0a 2f 2a 0a 2a 2a  e3DbFree)../*.**
55440 20 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4d 49   When SQLITE_OMI
55450 54 5f 57 53 44 20 69 73 20 64 65 66 69 6e 65 64  T_WSD is defined
55460 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
55470 74 68 65 20 74 61 72 67 65 74 20 70 6c 61 74 66  the target platf
55480 6f 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20  orm does.** not 
55490 73 75 70 70 6f 72 74 20 57 72 69 74 61 62 6c 65  support Writable
554a0 20 53 74 61 74 69 63 20 44 61 74 61 20 28 57 53   Static Data (WS
554b0 44 29 20 73 75 63 68 20 61 73 20 67 6c 6f 62 61  D) such as globa
554c0 6c 20 61 6e 64 20 73 74 61 74 69 63 20 76 61 72  l and static var
554d0 69 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c 20 76  iables..** All v
554e0 61 72 69 61 62 6c 65 73 20 6d 75 73 74 20 65 69  ariables must ei
554f0 74 68 65 72 20 62 65 20 6f 6e 20 74 68 65 20 73  ther be on the s
55500 74 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69 63 61  tack or dynamica
55510 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 66 72  lly allocated fr
55520 6f 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70 2e 20  om.** the heap. 
55530 20 57 68 65 6e 20 57 53 44 20 69 73 20 75 6e 73   When WSD is uns
55540 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 76 61  upported, the va
55550 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69  riable declarati
55560 6f 6e 73 20 73 63 61 74 74 65 72 65 64 0a 2a 2a  ons scattered.**
55570 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20   throughout the 
55580 53 51 4c 69 74 65 20 63 6f 64 65 20 6d 75 73 74  SQLite code must
55590 20 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61 6e 74   become constant
555a0 73 20 69 6e 73 74 65 61 64 2e 20 20 54 68 65 20  s instead.  The 
555b0 53 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20 6d 61  SQLITE_WSD.** ma
555c0 63 72 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20  cro is used for 
555d0 74 68 69 73 20 70 75 72 70 6f 73 65 2e 20 20 41  this purpose.  A
555e0 6e 64 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65  nd instead of re
555f0 66 65 72 65 6e 63 69 6e 67 20 74 68 65 20 76 61  ferencing the va
55600 72 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65 63 74  riable.** direct
55610 6c 79 2c 20 77 65 20 75 73 65 20 69 74 73 20 63  ly, we use its c
55620 6f 6e 73 74 61 6e 74 20 61 73 20 61 20 6b 65 79  onstant as a key
55630 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 72   to lookup the r
55640 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65  un-time allocate
55650 64 0a 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74  d.** buffer that
55660 20 68 6f 6c 64 73 20 72 65 61 6c 20 76 61 72 69   holds real vari
55670 61 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e 73 74  able.  The const
55680 61 6e 74 20 69 73 20 61 6c 73 6f 20 74 68 65 20  ant is also the 
55690 69 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20 66  initializer.** f
556a0 6f 72 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20  or the run-time 
556b0 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
556c0 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 75  ..**.** In the u
556d0 73 75 61 6c 20 63 61 73 65 20 77 68 65 72 65 20  sual case where 
556e0 57 53 44 20 69 73 20 73 75 70 70 6f 72 74 65 64  WSD is supported
556f0 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 57 53 44  , the SQLITE_WSD
55700 20 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a 20 6d   and GLOBAL.** m
55710 61 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e 6f 2d  acros become no-
55720 6f 70 73 20 61 6e 64 20 68 61 76 65 20 7a 65 72  ops and have zer
55730 6f 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d  o performance im
55740 70 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  pact..*/.#ifdef 
55750 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a  SQLITE_OMIT_WSD.
55760 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45    #define SQLITE
55770 5f 57 53 44 20 63 6f 6e 73 74 0a 20 20 23 64 65  _WSD const.  #de
55780 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29  fine GLOBAL(t,v)
55790 20 28 2a 28 74 2a 29 73 71 6c 69 74 65 33 5f 77   (*(t*)sqlite3_w
557a0 73 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a 29 26  sd_find((void*)&
557b0 28 76 29 2c 20 73 69 7a 65 6f 66 28 76 29 29 29  (v), sizeof(v)))
557c0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
557d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 47  e3GlobalConfig G
557e0 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 53 71 6c  LOBAL(struct Sql
557f0 69 74 65 33 43 6f 6e 66 69 67 2c 20 73 71 6c 69  ite3Config, sqli
55800 74 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c 49 54  te3Config).SQLIT
55810 45 5f 41 50 49 20 20 20 69 6e 74 20 73 71 6c 69  E_API   int sqli
55820 74 65 33 5f 77 73 64 5f 69 6e 69 74 28 69 6e 74  te3_wsd_init(int
55830 20 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51 4c 49   N, int J);.SQLI
55840 54 45 5f 41 50 49 20 20 20 76 6f 69 64 20 2a 73  TE_API   void *s
55850 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28  qlite3_wsd_find(
55860 76 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c 29 3b  void *K, int L);
55870 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
55880 20 53 51 4c 49 54 45 5f 57 53 44 20 0a 20 20 23   SQLITE_WSD .  #
55890 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c  define GLOBAL(t,
558a0 76 29 20 76 0a 20 20 23 64 65 66 69 6e 65 20 73  v) v.  #define s
558b0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
558c0 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67  ig sqlite3Config
558d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
558e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
558f0 72 6f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  ros are used to 
55900 73 75 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65  suppress compile
55910 72 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 74  r warnings and t
55920 6f 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 6c 65  o.** make it cle
55930 61 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 61 64  ar to human read
55940 65 72 73 20 77 68 65 6e 20 61 20 66 75 6e 63 74  ers when a funct
55950 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73  ion parameter is
55960 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 0a 2a   deliberately .*
55970 2a 20 6c 65 66 74 20 75 6e 75 73 65 64 20 77 69  * left unused wi
55980 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66  thin the body of
55990 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69   a function. Thi
559a0 73 20 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e  s usually happen
559b0 73 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75 6e 63  s when.** a func
559c0 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 76  tion is called v
559d0 69 61 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f  ia a function po
559e0 69 6e 74 65 72 2e 20 46 6f 72 20 65 78 61 6d 70  inter. For examp
559f0 6c 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70 6c 65  le the .** imple
55a00 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20  mentation of an 
55a10 53 51 4c 20 61 67 67 72 65 67 61 74 65 20 73 74  SQL aggregate st
55a20 65 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20  ep callback may 
55a30 6e 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a 20 70  not use the.** p
55a40 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74  arameter indicat
55a50 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ing the number o
55a60 66 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73  f arguments pass
55a70 65 64 20 74 6f 20 74 68 65 20 61 67 67 72 65 67  ed to the aggreg
55a80 61 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20 6b 6e  ate,.** if it kn
55a90 6f 77 73 20 74 68 61 74 20 74 68 69 73 20 69 73  ows that this is
55aa0 20 65 6e 66 6f 72 63 65 64 20 65 6c 73 65 77 68   enforced elsewh
55ab0 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ere..**.** When 
55ac0 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d  a function param
55ad0 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64  eter is not used
55ae0 20 61 74 20 61 6c 6c 20 77 69 74 68 69 6e 20 74   at all within t
55af0 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e  he body of a fun
55b00 63 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69 73 20  ction,.** it is 
55b10 67 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65 64 20  generally named 
55b20 22 4e 6f 74 55 73 65 64 22 20 6f 72 20 22 4e 6f  "NotUsed" or "No
55b30 74 55 73 65 64 32 22 20 74 6f 20 6d 61 6b 65 20  tUsed2" to make 
55b40 74 68 69 6e 67 73 20 65 76 65 6e 20 63 6c 65 61  things even clea
55b50 72 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  rer..** However,
55b60 20 74 68 65 73 65 20 6d 61 63 72 6f 73 20 6d 61   these macros ma
55b70 79 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74  y also be used t
55b80 6f 20 73 75 70 70 72 65 73 73 20 77 61 72 6e 69  o suppress warni
55b90 6e 67 73 20 72 65 6c 61 74 65 64 20 74 6f 0a 2a  ngs related to.*
55ba0 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61  * parameters tha
55bb0 74 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74  t may or may not
55bc0 20 62 65 20 75 73 65 64 20 64 65 70 65 6e 64 69   be used dependi
55bd0 6e 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74 69 6f  ng on compilatio
55be0 6e 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20 46 6f  n options..** Fo
55bf0 72 20 65 78 61 6d 70 6c 65 20 74 68 6f 73 65 20  r example those 
55c00 70 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c 79 20  parameters only 
55c10 75 73 65 64 20 69 6e 20 61 73 73 65 72 74 28 29  used in assert()
55c20 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 6e 20   statements. In 
55c30 74 68 65 73 65 0a 2a 2a 20 63 61 73 65 73 20 74  these.** cases t
55c40 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  he parameters ar
55c50 65 20 6e 61 6d 65 64 20 61 73 20 70 65 72 20 74  e named as per t
55c60 68 65 20 75 73 75 61 6c 20 63 6f 6e 76 65 6e 74  he usual convent
55c70 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ions..*/.#define
55c80 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
55c90 52 28 78 29 20 28 76 6f 69 64 29 28 78 29 0a 23  R(x) (void)(x).#
55ca0 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41  define UNUSED_PA
55cb0 52 41 4d 45 54 45 52 32 28 78 2c 79 29 20 55 4e  RAMETER2(x,y) UN
55cc0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78  USED_PARAMETER(x
55cd0 29 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  ),UNUSED_PARAMET
55ce0 45 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72  ER(y)../*.** For
55cf0 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20  ward references 
55d00 74 6f 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2f  to structures.*/
55d10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
55d20 41 67 67 49 6e 66 6f 20 41 67 67 49 6e 66 6f 3b  AggInfo AggInfo;
55d30 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
55d40 41 75 74 68 43 6f 6e 74 65 78 74 20 41 75 74 68  AuthContext Auth
55d50 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66  Context;.typedef
55d60 20 73 74 72 75 63 74 20 42 69 74 76 65 63 20 42   struct Bitvec B
55d70 69 74 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73  itvec;.typedef s
55d80 74 72 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77  truct RowSet Row
55d90 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Set;.typedef str
55da0 75 63 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c  uct CollSeq Coll
55db0 53 65 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Seq;.typedef str
55dc0 75 63 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d  uct Column Colum
55dd0 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  n;.typedef struc
55de0 74 20 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66  t Db Db;.typedef
55df0 20 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 53   struct Schema S
55e00 63 68 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73  chema;.typedef s
55e10 74 72 75 63 74 20 45 78 70 72 20 45 78 70 72 3b  truct Expr Expr;
55e20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
55e30 45 78 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73  ExprList ExprLis
55e40 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
55e50 74 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70  t FKey FKey;.typ
55e60 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63  edef struct Func
55e70 44 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70  Def FuncDef;.typ
55e80 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63  edef struct Func
55e90 44 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48  DefHash FuncDefH
55ea0 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ash;.typedef str
55eb0 75 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73  uct IdList IdLis
55ec0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
55ed0 74 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74  t Index Index;.t
55ee0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65  ypedef struct Ke
55ef0 79 43 6c 61 73 73 20 4b 65 79 43 6c 61 73 73 3b  yClass KeyClass;
55f00 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
55f10 4b 65 79 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b  KeyInfo KeyInfo;
55f20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
55f30 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73  Lookaside Lookas
55f40 69 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ide;.typedef str
55f50 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f  uct LookasideSlo
55f60 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b  t LookasideSlot;
55f70 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
55f80 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74  Module Module;.t
55f90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4e 61  ypedef struct Na
55fa0 6d 65 43 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f  meContext NameCo
55fb0 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20 73  ntext;.typedef s
55fc0 74 72 75 63 74 20 50 61 72 73 65 20 50 61 72 73  truct Parse Pars
55fd0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
55fe0 74 20 53 61 76 65 70 6f 69 6e 74 20 53 61 76 65  t Savepoint Save
55ff0 70 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73  point;.typedef s
56000 74 72 75 63 74 20 53 65 6c 65 63 74 20 53 65 6c  truct Select Sel
56010 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ect;.typedef str
56020 75 63 74 20 53 72 63 4c 69 73 74 20 53 72 63 4c  uct SrcList SrcL
56030 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ist;.typedef str
56040 75 63 74 20 53 74 72 41 63 63 75 6d 20 53 74 72  uct StrAccum Str
56050 41 63 63 75 6d 3b 0a 74 79 70 65 64 65 66 20 73  Accum;.typedef s
56060 74 72 75 63 74 20 54 61 62 6c 65 20 54 61 62 6c  truct Table Tabl
56070 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
56080 74 20 54 61 62 6c 65 4c 6f 63 6b 20 54 61 62 6c  t TableLock Tabl
56090 65 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 66 20 73  eLock;.typedef s
560a0 74 72 75 63 74 20 54 6f 6b 65 6e 20 54 6f 6b 65  truct Token Toke
560b0 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  n;.typedef struc
560c0 74 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 54  t TriggerStack T
560d0 72 69 67 67 65 72 53 74 61 63 6b 3b 0a 74 79 70  riggerStack;.typ
560e0 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67  edef struct Trig
560f0 67 65 72 53 74 65 70 20 54 72 69 67 67 65 72 53  gerStep TriggerS
56100 74 65 70 3b 0a 74 79 70 65 64 65 66 20 73 74 72  tep;.typedef str
56110 75 63 74 20 54 72 69 67 67 65 72 20 54 72 69 67  uct Trigger Trig
56120 67 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ger;.typedef str
56130 75 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  uct UnpackedReco
56140 72 64 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  rd UnpackedRecor
56150 64 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  d;.typedef struc
56160 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65 72 3b  t Walker Walker;
56170 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
56180 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72 65 50  WherePlan WhereP
56190 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72  lan;.typedef str
561a0 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 57 68  uct WhereInfo Wh
561b0 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  ereInfo;.typedef
561c0 20 73 74 72 75 63 74 20 57 68 65 72 65 4c 65 76   struct WhereLev
561d0 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b 0a 0a  el WhereLevel;..
561e0 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f 75 72  /*.** Defer sour
561f0 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e 64 20  cing vdbe.h and 
56200 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 61 66  btree.h until af
56210 74 65 72 20 74 68 65 20 22 75 38 22 20 61 6e 64  ter the "u8" and
56220 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 6c 65   .** "BusyHandle
56230 72 22 20 74 79 70 65 64 65 66 73 2e 20 76 64 62  r" typedefs. vdb
56240 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 72 65  e.h also require
56250 73 20 61 20 66 65 77 20 6f 66 20 74 68 65 20 6f  s a few of the o
56260 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 65 72  paque.** pointer
56270 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46 75 6e   types (i.e. Fun
56280 63 44 65 66 29 20 64 65 66 69 6e 65 64 20 61 62  cDef) defined ab
56290 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ove..*/./*******
562a0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
562b0 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20 6d  btree.h in the m
562c0 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
562d0 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
562e0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
562f0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
56300 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a  le btree.h *****
56310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56330 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
56340 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
56350 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
56360 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
56370 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
56380 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
56390 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
563a0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
563b0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
563c0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
563d0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
563e0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
563f0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
56400 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
56410 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
56420 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
56430 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
56440 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
56450 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
56460 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
56470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
564a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
564b0 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
564c0 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  le defines the i
564d0 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68  nterface that th
564e0 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65 65 20  e sqlite B-Tree 
564f0 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 74 65  file.** subsyste
56500 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73  m.  See comments
56510 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 63   in the source c
56520 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61 69 6c  ode for a detail
56530 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e 0a 2a  ed description.*
56540 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68 20 69  * of what each i
56550 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65  nterface routine
56560 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23   does..**.** @(#
56570 29 20 24 49 64 3a 20 62 74 72 65 65 2e 68 2c 76  ) $Id: btree.h,v
56580 20 31 2e 31 30 36 20 32 30 30 38 2f 31 32 2f 31   1.106 2008/12/1
56590 37 20 31 37 3a 33 30 3a 32 36 20 64 61 6e 69 65  7 17:30:26 danie
565a0 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
565b0 23 69 66 6e 64 65 66 20 5f 42 54 52 45 45 5f 48  #ifndef _BTREE_H
565c0 5f 0a 23 64 65 66 69 6e 65 20 5f 42 54 52 45 45  _.#define _BTREE
565d0 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68  _H_../* TODO: Th
565e0 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 73  is definition is
565f0 20 6a 75 73 74 20 69 6e 63 6c 75 64 65 64 20 73   just included s
56600 6f 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20  o other modules 
56610 63 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e  compile. It.** n
56620 65 65 64 73 20 74 6f 20 62 65 20 72 65 76 69 73  eeds to be revis
56630 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ited..*/.#define
56640 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f   SQLITE_N_BTREE_
56650 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49  META 10../*.** I
56660 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f 6e  f defined as non
56670 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 75  -zero, auto-vacu
56680 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 79  um is enabled by
56690 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 77   default. Otherw
566a0 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 62  ise.** it must b
566b0 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 20  e turned on for 
566c0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75 73  each database us
566d0 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 6f  ing "PRAGMA auto
566e0 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f  _vacuum = 1"..*/
566f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
56700 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
56710 55 4d 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c  UM.  #define SQL
56720 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f  ITE_DEFAULT_AUTO
56730 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a  VACUUM 0.#endif.
56740 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41  .#define BTREE_A
56750 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 20 30  UTOVACUUM_NONE 0
56760 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
56770 74 20 64 6f 20 61 75 74 6f 2d 76 61 63 75 75 6d  t do auto-vacuum
56780 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
56790 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c  E_AUTOVACUUM_FUL
567a0 4c 20 31 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  L 1        /* Do
567b0 20 66 75 6c 6c 20 61 75 74 6f 2d 76 61 63 75 75   full auto-vacuu
567c0 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  m */.#define BTR
567d0 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e  EE_AUTOVACUUM_IN
567e0 43 52 20 32 20 20 20 20 20 20 20 20 2f 2a 20 49  CR 2        /* I
567f0 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75  ncremental vacuu
56800 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77  m */../*.** Forw
56810 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
56820 20 6f 66 20 73 74 72 75 63 74 75 72 65 0a 2a 2f   of structure.*/
56830 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
56840 42 74 72 65 65 20 42 74 72 65 65 3b 0a 74 79 70  Btree Btree;.typ
56850 65 64 65 66 20 73 74 72 75 63 74 20 42 74 43 75  edef struct BtCu
56860 72 73 6f 72 20 42 74 43 75 72 73 6f 72 3b 0a 74  rsor BtCursor;.t
56870 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74  ypedef struct Bt
56880 53 68 61 72 65 64 20 42 74 53 68 61 72 65 64 3b  Shared BtShared;
56890 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
568a0 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20  BtreeMutexArray 
568b0 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3b  BtreeMutexArray;
568c0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ../*.** This str
568d0 75 63 74 75 72 65 20 72 65 63 6f 72 64 73 20 61  ucture records a
568e0 6c 6c 20 6f 66 20 74 68 65 20 42 74 72 65 65 73  ll of the Btrees
568f0 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 68 6f   that need to ho
56900 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 78 20 62 65  ld.** a mutex be
56910 66 6f 72 65 20 77 65 20 65 6e 74 65 72 20 73 71  fore we enter sq
56920 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e  lite3VdbeExec().
56930 20 20 54 68 65 20 42 74 72 65 65 73 20 61 72 65    The Btrees are
56940 0a 2a 2a 20 61 72 65 20 70 6c 61 63 65 64 20 69  .** are placed i
56950 6e 20 61 42 74 72 65 65 5b 5d 20 69 6e 20 6f 72  n aBtree[] in or
56960 64 65 72 20 6f 66 20 61 42 74 72 65 65 5b 5d 2d  der of aBtree[]-
56970 3e 70 42 74 2e 20 20 54 68 61 74 20 77 61 79 2c  >pBt.  That way,
56980 0a 2a 2a 20 77 65 20 63 61 6e 20 61 6c 77 61 79  .** we can alway
56990 73 20 6c 6f 63 6b 20 61 6e 64 20 75 6e 6c 6f 63  s lock and unloc
569a0 6b 20 74 68 65 6d 20 61 6c 6c 20 71 75 69 63 6b  k them all quick
569b0 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74  ly..*/.struct Bt
569c0 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 7b 0a  reeMutexArray {.
569d0 20 20 69 6e 74 20 6e 4d 75 74 65 78 3b 0a 20 20    int nMutex;.  
569e0 42 74 72 65 65 20 2a 61 42 74 72 65 65 5b 53 51  Btree *aBtree[SQ
569f0 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45  LITE_MAX_ATTACHE
56a00 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49 54  D+1];.};...SQLIT
56a10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
56a20 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a  lite3BtreeOpen(.
56a30 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
56a40 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61  ilename,   /* Na
56a50 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66  me of database f
56a60 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20  ile to open */. 
56a70 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
56a80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73            /* Ass
56a90 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65  ociated database
56aa0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
56ab0 20 42 74 72 65 65 20 2a 2a 2c 20 20 20 20 20 20   Btree **,      
56ac0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
56ad0 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a 20  urn open Btree* 
56ae0 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  here */.  int fl
56af0 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
56b00 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20     /* Flags */. 
56b10 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20   int vfsFlags   
56b20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
56b30 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67  gs passed throug
56b40 68 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a 2f  h to VFS open */
56b50 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67  .);../* The flag
56b60 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  s parameter to s
56b70 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 20  qlite3BtreeOpen 
56b80 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69  can be the bitwi
56b90 73 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a 20  se or of the.** 
56ba0 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73  following values
56bb0 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 54  ..**.** NOTE:  T
56bc0 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 74  hese values must
56bd0 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 65   match the corre
56be0 73 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f 20  sponding PAGER_ 
56bf0 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61 67  values in.** pag
56c00 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  er.h..*/.#define
56c10 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52   BTREE_OMIT_JOUR
56c20 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e 6f  NAL  1  /* Do no
56c30 74 20 75 73 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  t use journal.  
56c40 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23  No argument */.#
56c50 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4e 4f 5f  define BTREE_NO_
56c60 52 45 41 44 4c 4f 43 4b 20 20 20 32 20 20 2f 2a  READLOCK   2  /*
56c70 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 20   Omit readlocks 
56c80 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65  on readonly file
56c90 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  s */.#define BTR
56ca0 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 20  EE_MEMORY       
56cb0 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79   4  /* In-memory
56cc0 20 44 42 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e   DB.  No argumen
56cd0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  t */.#define BTR
56ce0 45 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20  EE_READONLY     
56cf0 20 38 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20   8  /* Open the 
56d00 64 61 74 61 62 61 73 65 20 69 6e 20 72 65 61 64  database in read
56d10 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f 0a 23 64  -only mode */.#d
56d20 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 44  efine BTREE_READ
56d30 57 52 49 54 45 20 20 20 20 31 36 20 20 2f 2a 20  WRITE    16  /* 
56d40 4f 70 65 6e 20 66 6f 72 20 62 6f 74 68 20 72 65  Open for both re
56d50 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e  ading and writin
56d60 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  g */.#define BTR
56d70 45 45 5f 43 52 45 41 54 45 20 20 20 20 20 20 20  EE_CREATE       
56d80 33 32 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68  32  /* Create th
56d90 65 20 64 61 74 61 62 61 73 65 20 69 66 20 69 74  e database if it
56da0 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
56db0 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
56dc0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
56dd0 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a 29  reeClose(Btree*)
56de0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
56df0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
56e00 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74  eSetCacheSize(Bt
56e10 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ree*,int);.SQLIT
56e20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
56e30 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66  lite3BtreeSetSaf
56e40 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a 2c  etyLevel(Btree*,
56e50 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
56e60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
56e70 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73  ite3BtreeSyncDis
56e80 61 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a 53  abled(Btree*);.S
56e90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
56ea0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
56eb0 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a  tPageSize(Btree*
56ec0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
56ed0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
56ee0 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
56ef0 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b 0a 53  eSize(Btree*);.S
56f00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
56f10 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61  t sqlite3BtreeMa
56f20 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65  xPageCount(Btree
56f30 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
56f40 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
56f50 65 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76  e3BtreeGetReserv
56f60 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  e(Btree*);.SQLIT
56f70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
56f80 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74  lite3BtreeSetAut
56f90 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 2c  oVacuum(Btree *,
56fa0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
56fb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
56fc0 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63  3BtreeGetAutoVac
56fd0 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 53 51  uum(Btree *);.SQ
56fe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
56ff0 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
57000 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 2c 69  inTrans(Btree*,i
57010 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
57020 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
57030 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
57040 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e 73 74  ne(Btree*, const
57050 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 3b   char *zMaster);
57060 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
57070 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
57080 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42  CommitPhaseTwo(B
57090 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
570a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
570b0 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74  e3BtreeCommit(Bt
570c0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
570d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
570e0 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42  3BtreeRollback(B
570f0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
57100 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
57110 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74  e3BtreeBeginStmt
57120 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
57130 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
57140 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53  ite3BtreeCommitS
57150 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  tmt(Btree*);.SQL
57160 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
57170 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
57180 62 61 63 6b 53 74 6d 74 28 42 74 72 65 65 2a 29  backStmt(Btree*)
57190 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
571a0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
571b0 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72  eCreateTable(Btr
571c0 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66  ee*, int*, int f
571d0 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  lags);.SQLITE_PR
571e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
571f0 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
57200 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
57210 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
57220 74 65 33 42 74 72 65 65 49 73 49 6e 53 74 6d 74  te3BtreeIsInStmt
57230 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
57240 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
57250 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61  ite3BtreeIsInRea
57260 64 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a  dTrans(Btree*);.
57270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
57280 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65  oid *sqlite3Btre
57290 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 2c  eSchema(Btree *,
572a0 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f   int, void(*)(vo
572b0 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  id *));.SQLITE_P
572c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
572d0 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63  e3BtreeSchemaLoc
572e0 6b 65 64 28 42 74 72 65 65 20 2a 29 3b 0a 53 51  ked(Btree *);.SQ
572f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
57300 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63   sqlite3BtreeLoc
57310 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 2c 20  kTable(Btree *, 
57320 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45  int, u8);.SQLITE
57330 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
57340 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
57350 6e 74 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c  nt(Btree *, int,
57360 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
57370 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
57380 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47  r *sqlite3BtreeG
57390 65 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65  etFilename(Btree
573a0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
573b0 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
573c0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 44  sqlite3BtreeGetD
573d0 69 72 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b  irname(Btree *);
573e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
573f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
57400 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e  te3BtreeGetJourn
57410 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b  alname(Btree *);
57420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
57430 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
57440 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a  CopyFile(Btree *
57450 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51 4c  , Btree *);..SQL
57460 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
57470 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72  sqlite3BtreeIncr
57480 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b  Vacuum(Btree *);
57490 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70  ../* The flags p
574a0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
574b0 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61  te3BtreeCreateTa
574c0 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65 20 62  ble can be the b
574d0 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66 20  itwise OR.** of 
574e0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c  the following fl
574f0 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ags:.*/.#define 
57500 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20 20  BTREE_INTKEY    
57510 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68   1    /* Table h
57520 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20 73  as only 64-bit s
57530 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b 65  igned integer ke
57540 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  ys */.#define BT
57550 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 32  REE_ZERODATA   2
57560 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73      /* Table has
57570 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20   keys only - no 
57580 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65 20  data */.#define 
57590 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41 20 20  BTREE_LEAFDATA  
575a0 20 34 20 20 20 20 2f 2a 20 44 61 74 61 20 73 74   4    /* Data st
575b0 6f 72 65 64 20 69 6e 20 6c 65 61 76 65 73 20 6f  ored in leaves o
575c0 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73 20 49 4e  nly.  Implies IN
575d0 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f  TKEY */..SQLITE_
575e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
575f0 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c  te3BtreeDropTabl
57600 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69  e(Btree*, int, i
57610 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
57620 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
57630 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28  BtreeClearTable(
57640 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Btree*, int, int
57650 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
57660 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
57670 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65  reeGetMeta(Btree
57680 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20  *, int idx, u32 
57690 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45  *pValue);.SQLITE
576a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
576b0 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
576c0 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20  eta(Btree*, int 
576d0 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b  idx, u32 value);
576e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
576f0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
57700 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28  eTripAllCursors(
57710 42 74 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53  Btree*, int);..S
57720 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
57730 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
57740 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c 20  rsor(.  Btree*, 
57750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57760 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
57770 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e 67  BTree containing
57780 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a   table to open *
57790 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
577a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
577b0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
577c0 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 2a  x of root page *
577d0 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20  /.  int wrFlag, 
577e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
577f0 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f           /* 1 fo
57800 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20 66 6f  r writing.  0 fo
57810 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20  r read-only */. 
57820 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 2a   struct KeyInfo*
57830 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
57840 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
57850 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 61  rgument to compa
57860 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  re function */. 
57870 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73   BtCursor *pCurs
57880 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  or              
57890 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74        /* Space t
578a0 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 73  o write cursor s
578b0 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a 53  tructure */.);.S
578c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
578d0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
578e0 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b 0a  rsorSize(void);.
578f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
57900 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
57910 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75  CloseCursor(BtCu
57920 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
57930 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
57940 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20  e3BtreeMoveto(. 
57950 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 63 6f   BtCursor*,.  co
57960 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 0a  nst void *pKey,.
57970 20 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 69 6e    i64 nKey,.  in
57980 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70  t bias,.  int *p
57990 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Res.);.SQLITE_PR
579a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
579b0 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61  3BtreeMovetoUnpa
579c0 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72  cked(.  BtCursor
579d0 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63  *,.  UnpackedRec
579e0 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 20 69  ord *pUnKey,.  i
579f0 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20 69 6e 74  64 intKey,.  int
57a00 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 52   bias,.  int *pR
57a10 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  es.);.SQLITE_PRI
57a20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
57a30 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f  BtreeCursorHasMo
57a40 76 65 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 69  ved(BtCursor*, i
57a50 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
57a60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
57a70 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43 75  BtreeDelete(BtCu
57a80 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
57a90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
57aa0 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 42 74  e3BtreeInsert(Bt
57ab0 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 20 76  Cursor*, const v
57ac0 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e  oid *pKey, i64 n
57ad0 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Key,.           
57ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57af0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69         const voi
57b00 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  d *pData, int nD
57b10 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
57b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57b30 20 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f         int nZero
57b40 2c 20 69 6e 74 20 62 69 61 73 29 3b 0a 53 51 4c  , int bias);.SQL
57b50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
57b60 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73  sqlite3BtreeFirs
57b70 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  t(BtCursor*, int
57b80 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f   *pRes);.SQLITE_
57b90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
57ba0 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43  te3BtreeLast(BtC
57bb0 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65  ursor*, int *pRe
57bc0 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  s);.SQLITE_PRIVA
57bd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
57be0 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72  reeNext(BtCursor
57bf0 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53  *, int *pRes);.S
57c00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
57c10 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6f  t sqlite3BtreeEo
57c20 66 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51  f(BtCursor*);.SQ
57c30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
57c40 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61   sqlite3BtreeFla
57c50 67 73 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53  gs(BtCursor*);.S
57c60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
57c70 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72  t sqlite3BtreePr
57c80 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 2a  evious(BtCursor*
57c90 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51  , int *pRes);.SQ
57ca0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
57cb0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
57cc0 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20  Size(BtCursor*, 
57cd0 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c  i64 *pSize);.SQL
57ce0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
57cf0 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28  sqlite3BtreeKey(
57d00 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f  BtCursor*, u32 o
57d10 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20  ffset, u32 amt, 
57d20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  void*);.SQLITE_P
57d30 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a  RIVATE sqlite3 *
57d40 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
57d50 6f 72 44 62 28 63 6f 6e 73 74 20 42 74 43 75 72  orDb(const BtCur
57d60 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sor*);.SQLITE_PR
57d70 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64  IVATE const void
57d80 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65   *sqlite3BtreeKe
57d90 79 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a  yFetch(BtCursor*
57da0 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51  , int *pAmt);.SQ
57db0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
57dc0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
57dd0 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28 42  BtreeDataFetch(B
57de0 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70  tCursor*, int *p
57df0 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Amt);.SQLITE_PRI
57e00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
57e10 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42 74  BtreeDataSize(Bt
57e20 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 2a 70 53  Cursor*, u32 *pS
57e30 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ize);.SQLITE_PRI
57e40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
57e50 42 74 72 65 65 44 61 74 61 28 42 74 43 75 72 73  BtreeData(BtCurs
57e60 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c  or*, u32 offset,
57e70 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29   u32 amt, void*)
57e80 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
57e90 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42  E char *sqlite3B
57ea0 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65  treeIntegrityChe
57eb0 63 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a  ck(Btree*, int *
57ec0 61 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74  aRoot, int nRoot
57ed0 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51  , int, int*);.SQ
57ee0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 72  LITE_PRIVATE str
57ef0 75 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69 74  uct Pager *sqlit
57f00 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72  e3BtreePager(Btr
57f10 65 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  ee*);..SQLITE_PR
57f20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
57f30 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74  3BtreePutData(Bt
57f40 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66  Cursor*, u32 off
57f50 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f  set, u32 amt, vo
57f60 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id*);.SQLITE_PRI
57f70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
57f80 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66  3BtreeCacheOverf
57f90 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b  low(BtCursor *);
57fa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
57fb0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
57fc0 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43  eClearCursor(BtC
57fd0 75 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 64 65  ursor *);..#ifde
57fe0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
57ff0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
58000 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
58010 73 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f 72  sorInfo(BtCursor
58020 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53  *, int*, int);.S
58030 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
58040 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  id sqlite3BtreeC
58050 75 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65 2a  ursorList(Btree*
58060 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  );.#endif../*.**
58070 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 75   If we are not u
58080 73 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 68  sing shared cach
58090 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73  e, then there is
580a0 20 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 75   no need to.** u
580b0 73 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 63  se mutexes to ac
580c0 63 65 73 73 20 74 68 65 20 42 74 53 68 61 72 65  cess the BtShare
580d0 64 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 53  d structures.  S
580e0 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 6e  o make the.** En
580f0 74 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 72  ter and Leave pr
58100 6f 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 2e  ocedures no-ops.
58110 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
58120 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  (SQLITE_OMIT_SHA
58130 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 53 51  RED_CACHE) && SQ
58140 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
58150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
58160 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
58170 65 65 45 6e 74 65 72 28 42 74 72 65 65 2a 29 3b  eeEnter(Btree*);
58180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
58190 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74    void sqlite3Bt
581a0 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 2a 29  reeLeave(Btree*)
581b0 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
581c0 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69  .  /* This routi
581d0 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64  ne is used insid
581e0 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  e assert() state
581f0 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 53  ments only. */.S
58200 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
58210 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
58220 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65  HoldsMutex(Btree
58230 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  *);.#endif.SQLIT
58240 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
58250 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
58260 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f  erCursor(BtCurso
58270 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
58280 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
58290 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
582a0 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53  or(BtCursor*);.S
582b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
582c0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
582d0 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65  eEnterAll(sqlite
582e0 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  3*);.SQLITE_PRIV
582f0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
58300 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28  e3BtreeLeaveAll(
58310 73 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64  sqlite3*);.#ifnd
58320 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54  ef NDEBUG.  /* T
58330 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
58340 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72  sed inside asser
58350 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f  t() statements o
58360 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly. */.SQLITE_P
58370 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
58380 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
58390 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33  lMutexes(sqlite3
583a0 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  *);.#endif.SQLIT
583b0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
583c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
583d0 65 78 41 72 72 61 79 45 6e 74 65 72 28 42 74 72  exArrayEnter(Btr
583e0 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a  eeMutexArray*);.
583f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
58400 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
58410 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76  eeMutexArrayLeav
58420 65 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e(BtreeMutexArra
58430 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  y*);.SQLITE_PRIV
58440 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
58450 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
58460 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74  yInsert(BtreeMut
58470 65 78 41 72 72 61 79 2a 2c 20 42 74 72 65 65 2a  exArray*, Btree*
58480 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
58490 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  e sqlite3BtreeEn
584a0 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20  ter(X).# define 
584b0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
584c0 65 28 58 29 0a 23 69 66 6e 64 65 66 20 4e 44 45  e(X).#ifndef NDE
584d0 42 55 47 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f  BUG.  /* This ro
584e0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e  utine is used in
584f0 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74  side assert() st
58500 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a  atements only. *
58510 2f 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  /.# define sqlit
58520 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
58530 78 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 23 20  x(X) 1.#endif.# 
58540 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
58550 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58  reeEnterCursor(X
58560 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
58570 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
58580 6f 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  or(X).# define s
58590 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
585a0 41 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20  All(X).# define 
585b0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
585c0 65 41 6c 6c 28 58 29 0a 23 69 66 6e 64 65 66 20  eAll(X).#ifndef 
585d0 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 69 73  NDEBUG.  /* This
585e0 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
585f0 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
58600 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79   statements only
58610 2e 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 71  . */.# define sq
58620 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
58630 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a 23  llMutexes(X) 1.#
58640 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 73  endif.# define s
58650 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
58660 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20  ArrayEnter(X).# 
58670 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
58680 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
58690 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  ve(X).# define s
586a0 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
586b0 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29  ArrayInsert(X,Y)
586c0 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66  .#endif...#endif
586d0 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f   /* _BTREE_H_ */
586e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
586f0 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68  * End of btree.h
58700 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
58710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58730 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
58740 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
58750 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
58760 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
58770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58780 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
58790 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68  * Include vdbe.h
587a0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
587b0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
587c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
587d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
587e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
587f0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
58800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58820 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
58830 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
58840 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
58850 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
58860 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
58870 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
58880 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
58890 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
588a0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
588b0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
588c0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
588d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
588e0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
588f0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
58900 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
58910 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
58920 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
58930 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
58940 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
58950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58990 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64  ********.** Head
589a0 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  er file for the 
589b0 56 69 72 74 75 61 6c 20 44 61 74 61 42 61 73 65  Virtual DataBase
589c0 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a   Engine (VDBE).*
589d0 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
589e0 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
589f0 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69  erface to the vi
58a00 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
58a10 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45  ngine.** or VDBE
58a20 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c  .  The VDBE impl
58a30 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61  ements an abstra
58a40 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20  ct machine that 
58a50 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65  runs a.** simple
58a60 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65   program to acce
58a70 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68  ss and modify th
58a80 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74  e underlying dat
58a90 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  abase..**.** $Id
58aa0 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 33 39  : vdbe.h,v 1.139
58ab0 20 32 30 30 38 2f 31 30 2f 33 31 20 31 30 3a 35   2008/10/31 10:5
58ac0 33 3a 32 33 20 64 61 6e 69 65 6c 6b 31 39 37 37  3:23 danielk1977
58ad0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
58ae0 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48  f _SQLITE_VDBE_H
58af0 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
58b00 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  E_VDBE_H_../*.**
58b10 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69   A single VDBE i
58b20 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75  s an opaque stru
58b30 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62  cture named "Vdb
58b40 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e  e".  Only routin
58b50 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75  es.** in the sou
58b60 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56  rce file sqliteV
58b70 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65  dbe.c are allowe
58b80 64 20 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73  d to see the ins
58b90 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ides.** of this 
58ba0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79  structure..*/.ty
58bb0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
58bc0 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54  e Vdbe;../*.** T
58bd0 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20  he names of the 
58be0 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20  following types 
58bf0 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65  declared in vdbe
58c00 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75 69 72  Int.h are requir
58c10 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64  ed.** for the Vd
58c20 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e  beOp definition.
58c30 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
58c40 63 74 20 56 64 62 65 46 75 6e 63 20 56 64 62 65  ct VdbeFunc Vdbe
58c50 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74  Func;.typedef st
58c60 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 0a 2f  ruct Mem Mem;../
58c70 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e  *.** A single in
58c80 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65  struction of the
58c90 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
58ca0 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a   has an opcode.*
58cb0 2a 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73  * and as many as
58cc0 20 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e   three operands.
58cd0 20 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f    The instructio
58ce0 6e 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a  n is recorded.**
58cf0 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   as an instance 
58d00 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
58d10 20 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73   structure:.*/.s
58d20 74 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20  truct VdbeOp {. 
58d30 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20   u8 opcode;     
58d40 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65       /* What ope
58d50 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72  ration to perfor
58d60 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68  m */.  signed ch
58d70 61 72 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e  ar p4type; /* On
58d80 65 20 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20  e of the P4_xxx 
58d90 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34  constants for p4
58da0 20 2a 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73   */.  u8 opflags
58db0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74  ;         /* Not
58dc0 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20   currently used 
58dd0 2a 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 20 20  */.  u8 p5;     
58de0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 66 74           /* Fift
58df0 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  h parameter is a
58e00 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61  n unsigned chara
58e10 63 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31  cter */.  int p1
58e20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
58e30 20 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a   First operand *
58e40 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20  /.  int p2;     
58e50 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e          /* Secon
58e60 64 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74  d parameter (oft
58e70 65 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74  en the jump dest
58e80 69 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e  ination) */.  in
58e90 74 20 70 33 3b 20 20 20 20 20 20 20 20 20 20 20  t p3;           
58ea0 20 20 2f 2a 20 54 68 65 20 74 68 69 72 64 20 70    /* The third p
58eb0 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e  arameter */.  un
58ec0 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20  ion {           
58ed0 20 20 2f 2a 20 66 6f 72 74 68 20 70 61 72 61 6d    /* forth param
58ee0 65 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  eter */.    int 
58ef0 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
58f00 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
58f10 6c 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d 50  lue if p4type==P
58f20 34 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76  4_INT32 */.    v
58f30 6f 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20 20  oid *p;         
58f40 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69 63        /* Generic
58f50 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20   pointer */.    
58f60 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
58f70 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
58f80 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73 74  r to data for st
58f90 72 69 6e 67 20 28 63 68 61 72 20 61 72 72 61 79  ring (char array
58fa0 29 20 74 79 70 65 73 20 2a 2f 0a 20 20 20 20 69  ) types */.    i
58fb0 36 34 20 2a 70 49 36 34 3b 20 20 20 20 20 20 20  64 *pI64;       
58fc0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68        /* Used wh
58fd0 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f  en p4type is P4_
58fe0 49 4e 54 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75  INT64 */.    dou
58ff0 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20  ble *pReal;     
59000 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
59010 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 52 45   p4type is P4_RE
59020 41 4c 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65  AL */.    FuncDe
59030 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
59040 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
59050 74 79 70 65 20 69 73 20 50 34 5f 46 55 4e 43 44  type is P4_FUNCD
59060 45 46 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 75  EF */.    VdbeFu
59070 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20  nc *pVdbeFunc;  
59080 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
59090 74 79 70 65 20 69 73 20 50 34 5f 56 44 42 45 46  type is P4_VDBEF
590a0 55 4e 43 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53  UNC */.    CollS
590b0 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20  eq *pColl;      
590c0 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
590d0 34 74 79 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c  4type is P4_COLL
590e0 53 45 51 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a  SEQ */.    Mem *
590f0 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  pMem;           
59100 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
59110 34 74 79 70 65 20 69 73 20 50 34 5f 4d 45 4d 20  4type is P4_MEM 
59120 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  */.    sqlite3_v
59130 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 2f 2a  tab *pVtab;   /*
59140 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
59150 65 20 69 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a  e is P4_VTAB */.
59160 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
59170 79 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73  yInfo;     /* Us
59180 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
59190 73 20 50 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a  s P4_KEYINFO */.
591a0 20 20 20 20 69 6e 74 20 2a 61 69 3b 20 20 20 20      int *ai;    
591b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
591c0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
591d0 73 20 50 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f  s P4_INTARRAY */
591e0 0a 20 20 7d 20 70 34 3b 0a 23 69 66 64 65 66 20  .  } p4;.#ifdef 
591f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 63  SQLITE_DEBUG.  c
59200 68 61 72 20 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20  har *zComment;  
59210 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65          /* Comme
59220 6e 74 20 74 6f 20 69 6d 70 72 6f 76 65 20 72 65  nt to improve re
59230 61 64 61 62 69 6c 69 74 79 20 2a 2f 0a 23 65 6e  adability */.#en
59240 64 69 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f  dif.#ifdef VDBE_
59250 50 52 4f 46 49 4c 45 0a 20 20 69 6e 74 20 63 6e  PROFILE.  int cn
59260 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
59270 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
59280 74 69 6d 65 73 20 74 68 69 73 20 69 6e 73 74 72  times this instr
59290 75 63 74 69 6f 6e 20 77 61 73 20 65 78 65 63 75  uction was execu
592a0 74 65 64 20 2a 2f 0a 20 20 75 36 34 20 63 79 63  ted */.  u64 cyc
592b0 6c 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  les;            
592c0 20 20 2f 2a 20 54 6f 74 61 6c 20 74 69 6d 65 20    /* Total time 
592d0 73 70 65 6e 74 20 65 78 65 63 75 74 69 6e 67 20  spent executing 
592e0 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
592f0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79   */.#endif.};.ty
59300 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
59310 65 4f 70 20 56 64 62 65 4f 70 3b 0a 0a 2f 2a 0a  eOp VdbeOp;../*.
59320 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72  ** A smaller ver
59330 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75  sion of VdbeOp u
59340 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65  sed for the Vdbe
59350 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63  AddOpList() func
59360 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20  tion because.** 
59370 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73  it takes up less
59380 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63   space..*/.struc
59390 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20  t VdbeOpList {. 
593a0 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20   u8 opcode;     
593b0 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65       /* What ope
593c0 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72  ration to perfor
593d0 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68  m */.  signed ch
593e0 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69  ar p1;     /* Fi
593f0 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  rst operand */. 
59400 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b   signed char p2;
59410 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70       /* Second p
59420 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20  arameter (often 
59430 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61  the jump destina
59440 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65  tion) */.  signe
59450 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f  d char p3;     /
59460 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65  * Third paramete
59470 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20  r */.};.typedef 
59480 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73  struct VdbeOpLis
59490 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f  t VdbeOpList;../
594a0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
594b0 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 33  ues of VdbeOp.p3
594c0 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  type.*/.#define 
594d0 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20  P4_NOTUSED    0 
594e0 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61    /* The P4 para
594f0 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65  meter is not use
59500 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  d */.#define P4_
59510 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f  DYNAMIC  (-1)  /
59520 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  * Pointer to a s
59530 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66  tring obtained f
59540 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
59550 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  () */.#define P4
59560 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20  _STATIC   (-2)  
59570 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20  /* Pointer to a 
59580 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f  static string */
59590 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c  .#define P4_COLL
595a0 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34  SEQ  (-4)  /* P4
595b0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
595c0 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63   a CollSeq struc
595d0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
595e0 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29  P4_FUNCDEF  (-5)
595f0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
59600 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65  nter to a FuncDe
59610 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23  f structure */.#
59620 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
59630 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69  O  (-6)  /* P4 i
59640 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
59650 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
59660 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  re */.#define P4
59670 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20  _VDBEFUNC (-7)  
59680 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  /* P4 is a point
59690 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63  er to a VdbeFunc
596a0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
596b0 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20  efine P4_MEM    
596c0 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73    (-8)  /* P4 is
596d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
596e0 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72  Mem*    structur
596f0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
59700 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f  TRANSIENT (-9) /
59710 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
59720 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74  r to a transient
59730 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69   string */.#defi
59740 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28  ne P4_VTAB     (
59750 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -10) /* P4 is a 
59760 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71  pointer to an sq
59770 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63  lite3_vtab struc
59780 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
59790 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31  P4_MPRINTF  (-11
597a0 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72  ) /* P4 is a str
597b0 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ing obtained fro
597c0 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  m sqlite3_mprint
597d0 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  f() */.#define P
597e0 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29  4_REAL     (-12)
597f0 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62   /* P4 is a 64-b
59800 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  it floating poin
59810 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69  t value */.#defi
59820 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28  ne P4_INT64    (
59830 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -13) /* P4 is a 
59840 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
59850 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  teger */.#define
59860 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31   P4_INT32    (-1
59870 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32  4) /* P4 is a 32
59880 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
59890 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ger */.#define P
598a0 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29  4_INTARRAY (-15)
598b0 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74   /* P4 is a vect
598c0 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74  or of 32-bit int
598d0 65 67 65 72 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65  egers */../* Whe
598e0 6e 20 61 64 64 69 6e 67 20 61 20 50 34 20 61 72  n adding a P4 ar
598f0 67 75 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f  gument using P4_
59900 4b 45 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20  KEYINFO, a copy 
59910 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  of the KeyInfo s
59920 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d  tructure.** is m
59930 61 64 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20  ade.  That copy 
59940 69 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68  is freed when th
59950 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69  e Vdbe is finali
59960 7a 65 64 2e 20 20 42 75 74 20 69 66 20 74 68 65  zed.  But if the
59970 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20  .** argument is 
59980 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
59990 46 46 2c 20 74 68 65 20 70 61 73 73 65 64 20 69  FF, the passed i
599a0 6e 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65  n pointer is use
599b0 64 2e 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20  d.  It still.** 
599c0 67 65 74 73 20 66 72 65 65 64 20 77 68 65 6e 20  gets freed when 
599d0 74 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61  the Vdbe is fina
599e0 6c 69 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c  lized so it stil
599f0 6c 20 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61  l should be obta
59a00 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73  ined.** from a s
59a10 69 6e 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c  ingle sqliteMall
59a20 6f 63 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f  oc().  But no co
59a30 70 79 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74  py is made and t
59a40 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75  he calling.** fu
59a50 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e  nction should *n
59a60 6f 74 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20  ot* try to free 
59a70 74 68 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a  the KeyInfo..*/.
59a80 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e  #define P4_KEYIN
59a90 46 4f 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29  FO_HANDOFF (-16)
59aa0 0a 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49  .#define P4_KEYI
59ab0 4e 46 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37  NFO_STATIC  (-17
59ac0 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62  )../*.** The Vdb
59ad0 65 2e 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79  e.aColName array
59ae0 20 63 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d   contains 5n Mem
59af0 20 73 74 72 75 63 74 75 72 65 73 2c 20 77 68 65   structures, whe
59b00 72 65 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20  re n is the .** 
59b10 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
59b20 73 20 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e  s of data return
59b30 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d  ed by the statem
59b40 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ent..*/.#define 
59b50 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20  COLNAME_NAME    
59b60 20 30 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41   0.#define COLNA
59b70 4d 45 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64  ME_DECLTYPE 1.#d
59b80 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41  efine COLNAME_DA
59b90 54 41 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65  TABASE 2.#define
59ba0 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20   COLNAME_TABLE  
59bb0 20 20 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e    3.#define COLN
59bc0 41 4d 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23  AME_COLUMN   4.#
59bd0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
59be0 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44  BLE_COLUMN_METAD
59bf0 41 54 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c  ATA.# define COL
59c00 4e 41 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20  NAME_N        5 
59c10 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
59c20 66 20 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79  f COLNAME_xxx sy
59c30 6d 62 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23  mbols */.#else.#
59c40 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d   ifdef SQLITE_OM
59c50 49 54 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20  IT_DECLTYPE.#   
59c60 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e  define COLNAME_N
59c70 20 20 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20        1      /* 
59c80 53 74 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e  Store only the n
59c90 61 6d 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20  ame */.# else.# 
59ca0 20 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45    define COLNAME
59cb0 5f 4e 20 20 20 20 20 20 32 20 20 20 20 20 20 2f  _N      2      /
59cc0 2a 20 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65  * Store the name
59cd0 20 61 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f   and decltype */
59ce0 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
59cf0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
59d00 77 69 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65  wing macro conve
59d10 72 74 73 20 61 20 72 65 6c 61 74 69 76 65 20 61  rts a relative a
59d20 64 64 72 65 73 73 20 69 6e 20 74 68 65 20 70 32  ddress in the p2
59d30 20 66 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56   field.** of a V
59d40 64 62 65 4f 70 20 73 74 72 75 63 74 75 72 65 20  dbeOp structure 
59d50 69 6e 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20  into a negative 
59d60 6e 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a  number so that .
59d70 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  ** sqlite3VdbeAd
59d80 64 4f 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20  dOpList() knows 
59d90 74 68 61 74 20 74 68 65 20 61 64 64 72 65 73 73  that the address
59da0 20 69 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43   is relative.  C
59db0 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61  alling.** the ma
59dc0 63 72 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72  cro again restor
59dd0 65 73 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a  es the address..
59de0 2a 2f 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28  */.#define ADDR(
59df0 58 29 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a  X)  (-1-(X))../*
59e00 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65  .** The makefile
59e10 20 73 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e   scans the vdbe.
59e20 63 20 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e  c source file an
59e30 64 20 63 72 65 61 74 65 73 20 74 68 65 20 22 6f  d creates the "o
59e40 70 63 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61  pcodes.h".** hea
59e50 64 65 72 20 66 69 6c 65 20 74 68 61 74 20 64 65  der file that de
59e60 66 69 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66  fines a number f
59e70 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75  or each opcode u
59e80 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45 2e  sed by the VDBE.
59e90 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
59ea0 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f  *** Include opco
59eb0 64 65 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  des.h in the mid
59ec0 64 6c 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a  dle of vdbe.h **
59ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59ee0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
59ef0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
59f00 70 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a  pcodes.h *******
59f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59f30 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63  **/./* Automatic
59f40 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20  ally generated. 
59f50 20 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a   Do not edit */.
59f60 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63  /* See the mkopc
59f70 6f 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20  odeh.awk script 
59f80 66 6f 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23  for details */.#
59f90 64 65 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20  define OP_VNext 
59fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
59fc0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 66 66 69  .#define OP_Affi
59fd0 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20  nity            
59fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ff0 20 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f   2.#define OP_Co
5a000 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20  lumn            
5a010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a020 20 20 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f     3.#define OP_
5a030 53 65 74 43 6f 6f 6b 69 65 20 20 20 20 20 20 20  SetCookie       
5a040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a050 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f       4.#define O
5a060 50 5f 53 65 65 6b 20 20 20 20 20 20 20 20 20 20  P_Seek          
5a070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a080 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65         5.#define
5a090 20 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20   OP_Real        
5a0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a0b0 20 20 20 20 20 20 20 31 32 39 20 20 20 2f 2a 20         129   /* 
5a0c0 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54  same as TK_FLOAT
5a0d0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
5a0e0 50 5f 53 65 71 75 65 6e 63 65 20 20 20 20 20 20  P_Sequence      
5a0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a100 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65         6.#define
5a110 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 20 20   OP_Savepoint   
5a120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a130 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69           7.#defi
5a140 6e 65 20 4f 50 5f 47 65 20 20 20 20 20 20 20 20  ne OP_Ge        
5a150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a160 20 20 20 20 20 20 20 20 20 20 37 35 20 20 20 2f            75   /
5a170 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 20  * same as TK_GE 
5a180 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
5a190 20 4f 50 5f 52 6f 77 4b 65 79 20 20 20 20 20 20   OP_RowKey      
5a1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a1b0 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69           8.#defi
5a1c0 6e 65 20 4f 50 5f 53 43 6f 70 79 20 20 20 20 20  ne OP_SCopy     
5a1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a1e0 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65             9.#de
5a1f0 66 69 6e 65 20 4f 50 5f 45 71 20 20 20 20 20 20  fine OP_Eq      
5a200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a210 20 20 20 20 20 20 20 20 20 20 20 20 37 31 20 20              71  
5a220 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45   /* same as TK_E
5a230 51 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  Q       */.#defi
5a240 6e 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20  ne OP_OpenWrite 
5a250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a260 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65            10.#de
5a270 66 69 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20  fine OP_NotNull 
5a280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a290 20 20 20 20 20 20 20 20 20 20 20 20 36 39 20 20              69  
5a2a0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
5a2b0 4f 54 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69  OTNULL  */.#defi
5a2c0 6e 65 20 4f 50 5f 49 66 20 20 20 20 20 20 20 20  ne OP_If        
5a2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a2e0 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64 65            11.#de
5a2f0 66 69 6e 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20  fine OP_ToInt   
5a300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a310 20 20 20 20 20 20 20 20 20 20 20 31 34 34 20 20             144  
5a320 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
5a330 4f 5f 49 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69  O_INT   */.#defi
5a340 6e 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20  ne OP_String8   
5a350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a360 20 20 20 20 20 20 20 20 20 20 39 31 20 20 20 2f            91   /
5a370 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52  * same as TK_STR
5a380 49 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  ING   */.#define
5a390 20 4f 50 5f 56 52 6f 77 69 64 20 20 20 20 20 20   OP_VRowid      
5a3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a3b0 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69          12.#defi
5a3c0 6e 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20  ne OP_CollSeq   
5a3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a3e0 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65            13.#de
5a3f0 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64  fine OP_OpenRead
5a400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a410 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 23              14.#
5a420 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 69 72 65  define OP_Expire
5a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
5a450 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 75 74 6f  .#define OP_Auto
5a460 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20  Commit          
5a470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a480 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 74  16.#define OP_Gt
5a490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a4b0 20 20 37 32 20 20 20 2f 2a 20 73 61 6d 65 20 61    72   /* same a
5a4c0 73 20 54 4b 5f 47 54 20 20 20 20 20 20 20 2a 2f  s TK_GT       */
5a4d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 67 65  .#define OP_Page
5a4e0 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20  count           
5a4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a500 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e  17.#define OP_In
5a510 74 65 67 72 69 74 79 43 6b 20 20 20 20 20 20 20  tegrityCk       
5a520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a530 20 20 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f    18.#define OP_
5a540 53 6f 72 74 20 20 20 20 20 20 20 20 20 20 20 20  Sort            
5a550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a560 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f      20.#define O
5a570 50 5f 43 6f 70 79 20 20 20 20 20 20 20 20 20 20  P_Copy          
5a580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a590 20 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65        21.#define
5a5a0 20 4f 50 5f 54 72 61 63 65 20 20 20 20 20 20 20   OP_Trace       
5a5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a5c0 20 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69          22.#defi
5a5d0 6e 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20  ne OP_Function  
5a5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a5f0 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65            23.#de
5a600 66 69 6e 65 20 4f 50 5f 49 66 4e 65 67 20 20 20  fine OP_IfNeg   
5a610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a620 20 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23              24.#
5a630 64 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20 20 20  define OP_And   
5a640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 34                64
5a660 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
5a670 5f 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23 64 65  _AND      */.#de
5a680 66 69 6e 65 20 4f 50 5f 53 75 62 74 72 61 63 74  fine OP_Subtract
5a690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a6a0 20 20 20 20 20 20 20 20 20 20 20 20 38 32 20 20              82  
5a6b0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d   /* same as TK_M
5a6c0 49 4e 55 53 20 20 20 20 2a 2f 0a 23 64 65 66 69  INUS    */.#defi
5a6d0 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20 20 20  ne OP_Noop      
5a6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a6f0 20 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 65            25.#de
5a700 66 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20  fine OP_Return  
5a710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a720 20 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 23              26.#
5a730 64 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e  define OP_Remain
5a740 64 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20  der             
5a750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35                85
5a760 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
5a770 5f 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65  _REM      */.#de
5a780 66 69 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64  fine OP_NewRowid
5a790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a7a0 20 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23              27.#
5a7b0 64 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70  define OP_Multip
5a7c0 6c 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ly              
5a7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33                83
5a7e0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
5a7f0 5f 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65  _STAR     */.#de
5a800 66 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65  fine OP_Variable
5a810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a820 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23              28.#
5a830 64 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67  define OP_String
5a840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 39                29
5a860 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c  .#define OP_Real
5a870 41 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20  Affinity        
5a880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a890 33 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52  30.#define OP_VR
5a8a0 65 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20  ename           
5a8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a8c0 20 20 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    31.#define OP_
5a8d0 50 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20  ParseSchema     
5a8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a8f0 20 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 4f      32.#define O
5a900 50 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20  P_VOpen         
5a910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a920 20 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65        33.#define
5a930 20 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20   OP_Close       
5a940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a950 20 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 69          34.#defi
5a960 6e 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65  ne OP_CreateInde
5a970 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
5a980 20 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65            35.#de
5a990 66 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65  fine OP_IsUnique
5a9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a9b0 20 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23              36.#
5a9c0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75  define OP_NotFou
5a9d0 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nd              
5a9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37                37
5a9f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36  .#define OP_Int6
5aa00 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  4               
5aa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aa20 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75  38.#define OP_Mu
5aa30 73 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20  stBeInt         
5aa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aa50 20 20 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f    39.#define OP_
5aa60 48 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20  Halt            
5aa70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aa80 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 4f      40.#define O
5aa90 50 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 20  P_Rowid         
5aaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aab0 20 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65        41.#define
5aac0 20 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20   OP_IdxLT       
5aad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aae0 20 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 69          42.#defi
5aaf0 6e 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20  ne OP_AddImm    
5ab00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ab10 20 20 20 20 20 20 20 20 20 20 34 33 0a 23 64 65            43.#de
5ab20 66 69 6e 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e  fine OP_Statemen
5ab30 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
5ab40 20 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23              44.#
5ab50 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74  define OP_RowDat
5ab60 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a               
5ab70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 35                45
5ab80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d  .#define OP_MemM
5ab90 61 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ax              
5aba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5abb0 34 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 72  46.#define OP_Or
5abc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5abd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5abe0 20 20 36 33 20 20 20 2f 2a 20 73 61 6d 65 20 61    63   /* same a
5abf0 73 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 2a 2f  s TK_OR       */
5ac00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45  .#define OP_NotE
5ac10 78 69 73 74 73 20 20 20 20 20 20 20 20 20 20 20  xists           
5ac20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ac30 34 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f  47.#define OP_Go
5ac40 73 75 62 20 20 20 20 20 20 20 20 20 20 20 20 20  sub             
5ac50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ac60 20 20 34 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f    48.#define OP_
5ac70 44 69 76 69 64 65 20 20 20 20 20 20 20 20 20 20  Divide          
5ac80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ac90 20 20 20 20 38 34 20 20 20 2f 2a 20 73 61 6d 65      84   /* same
5aca0 20 61 73 20 54 4b 5f 53 4c 41 53 48 20 20 20 20   as TK_SLASH    
5acb0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e  */.#define OP_In
5acc0 74 65 67 65 72 20 20 20 20 20 20 20 20 20 20 20  teger           
5acd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ace0 20 20 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f    49.#define OP_
5acf0 54 6f 4e 75 6d 65 72 69 63 20 20 20 20 20 20 20  ToNumeric       
5ad00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ad10 20 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65     143   /* same
5ad20 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49   as TK_TO_NUMERI
5ad30 43 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50  C*/.#define OP_P
5ad40 72 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20  rev             
5ad50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ad60 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50     50.#define OP
5ad70 5f 52 6f 77 53 65 74 52 65 61 64 20 20 20 20 20  _RowSetRead     
5ad80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ad90 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20       51.#define 
5ada0 4f 50 5f 43 6f 6e 63 61 74 20 20 20 20 20 20 20  OP_Concat       
5adb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5adc0 20 20 20 20 20 20 20 38 36 20 20 20 2f 2a 20 73         86   /* s
5add0 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54  ame as TK_CONCAT
5ade0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
5adf0 5f 52 6f 77 53 65 74 41 64 64 20 20 20 20 20 20  _RowSetAdd      
5ae00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ae10 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20       52.#define 
5ae20 4f 50 5f 42 69 74 41 6e 64 20 20 20 20 20 20 20  OP_BitAnd       
5ae30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ae40 20 20 20 20 20 20 20 37 37 20 20 20 2f 2a 20 73         77   /* s
5ae50 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 41 4e 44  ame as TK_BITAND
5ae60 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
5ae70 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20  _VColumn        
5ae80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ae90 20 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20       53.#define 
5aea0 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 20  OP_CreateTable  
5aeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aec0 20 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e         54.#defin
5aed0 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 20 20  e OP_Last       
5aee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aef0 20 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66           55.#def
5af00 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20  ine OP_SeekLe   
5af10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af20 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64             56.#d
5af30 65 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20  efine OP_IsNull 
5af40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af50 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38 20               68 
5af60 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
5af70 49 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66  ISNULL   */.#def
5af80 69 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75  ine OP_IncrVacuu
5af90 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m               
5afa0 20 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 64             57.#d
5afb0 65 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69  efine OP_IdxRowi
5afc0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
5afd0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 38 0a               58.
5afe0 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74  #define OP_Shift
5aff0 52 69 67 68 74 20 20 20 20 20 20 20 20 20 20 20  Right           
5b000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
5b010 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  0   /* same as T
5b020 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a 23 64  K_RSHIFT   */.#d
5b030 65 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f  efine OP_ResetCo
5b040 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  unt             
5b050 20 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a               59.
5b060 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 74 65  #define OP_Conte
5b070 78 74 50 75 73 68 20 20 20 20 20 20 20 20 20 20  xtPush          
5b080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
5b090 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59 69 65  0.#define OP_Yie
5b0a0 6c 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ld              
5b0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b0c0 20 36 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44   61.#define OP_D
5b0d0 72 6f 70 54 72 69 67 67 65 72 20 20 20 20 20 20  ropTrigger      
5b0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b0f0 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 4f 50     62.#define OP
5b100 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 20 20 20  _DropIndex      
5b110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b120 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 20       65.#define 
5b130 4f 50 5f 49 64 78 47 45 20 20 20 20 20 20 20 20  OP_IdxGE        
5b140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b150 20 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e         66.#defin
5b160 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 20 20  e OP_IdxDelete  
5b170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b180 20 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 66           67.#def
5b190 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20 20 20  ine OP_Vacuum   
5b1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b1b0 20 20 20 20 20 20 20 20 20 20 20 37 36 0a 23 64             76.#d
5b1c0 65 66 69 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20  efine OP_IfNot  
5b1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 37 0a               87.
5b1f0 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54  #define OP_DropT
5b200 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20  able            
5b210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
5b220 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65  8.#define OP_See
5b230 6b 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  kLt             
5b240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b250 20 38 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d   89.#define OP_M
5b260 61 6b 65 52 65 63 6f 72 64 20 20 20 20 20 20 20  akeRecord       
5b270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b280 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 50     92.#define OP
5b290 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 20 20 20  _ToBlob         
5b2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b2b0 20 20 20 20 31 34 32 20 20 20 2f 2a 20 73 61 6d      142   /* sam
5b2c0 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20  e as TK_TO_BLOB 
5b2d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52   */.#define OP_R
5b2e0 65 73 75 6c 74 52 6f 77 20 20 20 20 20 20 20 20  esultRow        
5b2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b300 20 20 20 39 33 0a 23 64 65 66 69 6e 65 20 4f 50     93.#define OP
5b310 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20  _Delete         
5b320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b330 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e 65 20       94.#define 
5b340 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20  OP_AggFinal     
5b350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b360 20 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e         95.#defin
5b370 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 20 20 20  e OP_Compare    
5b380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b390 20 20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66           96.#def
5b3a0 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74  ine OP_ShiftLeft
5b3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b3c0 20 20 20 20 20 20 20 20 20 20 20 37 39 20 20 20             79   
5b3d0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53  /* same as TK_LS
5b3e0 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e  HIFT   */.#defin
5b3f0 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 20  e OP_Goto       
5b400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b410 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66           97.#def
5b420 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b  ine OP_TableLock
5b430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b440 20 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64             98.#d
5b450 65 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20  efine OP_Clear  
5b460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b470 20 20 20 20 20 20 20 20 20 20 20 20 20 39 39 0a               99.
5b480 23 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20  #define OP_Le   
5b490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
5b4b0 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  3   /* same as T
5b4c0 4b 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23 64  K_LE       */.#d
5b4d0 65 66 69 6e 65 20 4f 50 5f 56 65 72 69 66 79 43  efine OP_VerifyC
5b4e0 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20  ookie           
5b4f0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a              100.
5b500 23 64 65 66 69 6e 65 20 4f 50 5f 41 67 67 53 74  #define OP_AggSt
5b510 65 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ep              
5b520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
5b530 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54  1.#define OP_ToT
5b540 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ext             
5b550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b560 31 34 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73  141   /* same as
5b570 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a   TK_TO_TEXT  */.
5b580 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20  #define OP_Not  
5b590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5b5b0 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  9   /* same as T
5b5c0 4b 5f 4e 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64  K_NOT      */.#d
5b5d0 65 66 69 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20  efine OP_ToReal 
5b5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b5f0 20 20 20 20 20 20 20 20 20 20 20 20 31 34 35 20              145 
5b600 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
5b610 54 4f 5f 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66  TO_REAL  */.#def
5b620 69 6e 65 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c  ine OP_SetNumCol
5b630 75 6d 6e 73 20 20 20 20 20 20 20 20 20 20 20 20  umns            
5b640 20 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64            102.#d
5b650 65 66 69 6e 65 20 4f 50 5f 54 72 61 6e 73 61 63  efine OP_Transac
5b660 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20  tion            
5b670 20 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a              103.
5b680 23 64 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74  #define OP_VFilt
5b690 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
5b6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
5b6b0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20  4.#define OP_Ne 
5b6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b6e0 20 37 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   70   /* same as
5b6f0 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a   TK_NE       */.
5b700 23 64 65 66 69 6e 65 20 4f 50 5f 56 44 65 73 74  #define OP_VDest
5b710 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20  roy             
5b720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
5b730 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e  5.#define OP_Con
5b740 74 65 78 74 50 6f 70 20 20 20 20 20 20 20 20 20  textPop         
5b750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b760 31 30 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42  106.#define OP_B
5b770 69 74 4f 72 20 20 20 20 20 20 20 20 20 20 20 20  itOr            
5b780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b790 20 20 20 37 38 20 20 20 2f 2a 20 73 61 6d 65 20     78   /* same 
5b7a0 61 73 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 2a  as TK_BITOR    *
5b7b0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 78  /.#define OP_Nex
5b7c0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
5b7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b7e0 31 30 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49  107.#define OP_I
5b7f0 64 78 49 6e 73 65 72 74 20 20 20 20 20 20 20 20  dxInsert        
5b800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b810 20 20 31 30 38 0a 23 64 65 66 69 6e 65 20 4f 50    108.#define OP
5b820 5f 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  _Lt             
5b830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b840 20 20 20 20 20 37 34 20 20 20 2f 2a 20 73 61 6d       74   /* sam
5b850 65 20 61 73 20 54 4b 5f 4c 54 20 20 20 20 20 20  e as TK_LT      
5b860 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   */.#define OP_S
5b870 65 65 6b 47 65 20 20 20 20 20 20 20 20 20 20 20  eekGe           
5b880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b890 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 4f 50    109.#define OP
5b8a0 5f 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 20  _Insert         
5b8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b8c0 20 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20      110.#define 
5b8d0 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 20 20 20  OP_Destroy      
5b8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b8f0 20 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e        111.#defin
5b900 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20  e OP_ReadCookie 
5b910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b920 20 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66          112.#def
5b930 69 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79  ine OP_LoadAnaly
5b940 73 69 73 20 20 20 20 20 20 20 20 20 20 20 20 20  sis             
5b950 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64            113.#d
5b960 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e  efine OP_Explain
5b970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b980 20 20 20 20 20 20 20 20 20 20 20 20 31 31 34 0a              114.
5b990 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50  #define OP_OpenP
5b9a0 73 65 75 64 6f 20 20 20 20 20 20 20 20 20 20 20  seudo           
5b9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
5b9c0 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65  5.#define OP_Ope
5b9d0 6e 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20  nEphemeral      
5b9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b9f0 31 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e  116.#define OP_N
5ba00 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  ull             
5ba10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ba20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 4f 50    117.#define OP
5ba30 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20 20  _Move           
5ba40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ba50 20 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20      118.#define 
5ba60 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 20  OP_Blob         
5ba70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ba80 20 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e        119.#defin
5ba90 65 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20 20  e OP_Add        
5baa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bab0 20 20 20 20 20 20 20 20 20 38 31 20 20 20 2f 2a           81   /*
5bac0 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53   same as TK_PLUS
5bad0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
5bae0 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 20  OP_Rewind       
5baf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bb00 20 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e        120.#defin
5bb10 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 20 20 20  e OP_SeekGt     
5bb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bb30 20 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66          121.#def
5bb40 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20 20 20  ine OP_VBegin   
5bb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bb60 20 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64            122.#d
5bb70 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 65  efine OP_VUpdate
5bb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bb90 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33 0a              123.
5bba0 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72  #define OP_IfZer
5bbb0 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o               
5bbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
5bbd0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74  4.#define OP_Bit
5bbe0 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Not             
5bbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bc00 20 39 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   90   /* same as
5bc10 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a   TK_BITNOT   */.
5bc20 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65 61  #define OP_VCrea
5bc30 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
5bc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
5bc50 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75  5.#define OP_Fou
5bc60 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nd              
5bc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bc80 31 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49  126.#define OP_I
5bc90 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 20  fPos            
5bca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bcb0 20 20 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50    127.#define OP
5bcc0 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20 20  _NullRow        
5bcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bce0 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20      128.#define 
5bcf0 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 20  OP_Jump         
5bd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bd10 20 20 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e        130.#defin
5bd20 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e  e OP_Permutation
5bd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bd40 20 20 20 20 20 20 20 20 31 33 31 0a 0a 2f 2a 20          131../* 
5bd50 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70  The following op
5bd60 63 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65 20  code values are 
5bd70 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 64  never used */.#d
5bd80 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64  efine OP_NotUsed
5bd90 5f 31 33 32 20 20 20 20 20 20 20 20 20 20 20 20  _132            
5bda0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 32 0a              132.
5bdb0 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73  #define OP_NotUs
5bdc0 65 64 5f 31 33 33 20 20 20 20 20 20 20 20 20 20  ed_133          
5bdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
5bde0 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74  3.#define OP_Not
5bdf0 55 73 65 64 5f 31 33 34 20 20 20 20 20 20 20 20  Used_134        
5be00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5be10 31 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e  134.#define OP_N
5be20 6f 74 55 73 65 64 5f 31 33 35 20 20 20 20 20 20  otUsed_135      
5be30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5be40 20 20 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50    135.#define OP
5be50 5f 4e 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20  _NotUsed_136    
5be60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5be70 20 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20      136.#define 
5be80 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20  OP_NotUsed_137  
5be90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bea0 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e        137.#defin
5beb0 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38  e OP_NotUsed_138
5bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bed0 20 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66          138.#def
5bee0 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31  ine OP_NotUsed_1
5bef0 33 39 20 20 20 20 20 20 20 20 20 20 20 20 20 20  39              
5bf00 20 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64            139.#d
5bf10 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64  efine OP_NotUsed
5bf20 5f 31 34 30 20 20 20 20 20 20 20 20 20 20 20 20  _140            
5bf30 20 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a              140.
5bf40 0a 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20  ../* Properties 
5bf50 73 75 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f  such as "out2" o
5bf60 72 20 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72  r "jump" that ar
5bf70 65 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a  e specified in.*
5bf80 2a 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f  * comments follo
5bf90 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 22 20  wing the "case" 
5bfa0 66 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20  for each opcode 
5bfb0 69 6e 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a  in the vdbe.c.**
5bfc0 20 61 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74   are encoded int
5bfd0 6f 20 62 69 74 76 65 63 74 6f 72 73 20 61 73 20  o bitvectors as 
5bfe0 66 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66  follows:.*/.#def
5bff0 69 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20  ine OPFLG_JUMP  
5c000 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31            0x0001
5c010 20 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68    /* jump:  P2 h
5c020 6f 6c 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20  olds jmp target 
5c030 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47  */.#define OPFLG
5c040 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45  _OUT2_PRERELEASE
5c050 20 30 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32   0x0002  /* out2
5c060 2d 70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a  -prerelease: */.
5c070 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e  #define OPFLG_IN
5c080 31 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78  1             0x
5c090 30 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20  0004  /* in1:   
5c0a0 50 31 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a  P1 is an input *
5c0b0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
5c0c0 49 4e 32 20 20 20 20 20 20 20 20 20 20 20 20 20  IN2             
5c0d0 30 78 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20  0x0008  /* in2: 
5c0e0 20 20 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74    P2 is an input
5c0f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c   */.#define OPFL
5c100 47 5f 49 4e 33 20 20 20 20 20 20 20 20 20 20 20  G_IN3           
5c110 20 20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33    0x0010  /* in3
5c120 3a 20 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70  :   P3 is an inp
5c130 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  ut */.#define OP
5c140 46 4c 47 5f 4f 55 54 33 20 20 20 20 20 20 20 20  FLG_OUT3        
5c150 20 20 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f      0x0020  /* o
5c160 75 74 33 3a 20 20 50 33 20 69 73 20 61 6e 20 6f  ut3:  P3 is an o
5c170 75 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65  utput */.#define
5c180 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a   OPFLG_INITIALIZ
5c190 45 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20  ER {\./*   0 */ 
5c1a0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30  0x00, 0x01, 0x00
5c1b0 2c 20 30 78 30 30 2c 20 30 78 31 30 2c 20 30 78  , 0x00, 0x10, 0x
5c1c0 30 38 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 5c  08, 0x02, 0x00,\
5c1d0 0a 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30 30 2c  ./*   8 */ 0x00,
5c1e0 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30   0x04, 0x00, 0x0
5c1f0 35 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  5, 0x02, 0x00, 0
5c200 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20  x00, 0x00,\./*  
5c210 31 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32  16 */ 0x00, 0x02
5c220 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78  , 0x00, 0x04, 0x
5c230 30 31 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20  01, 0x04, 0x00, 
5c240 30 78 30 30 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f  0x00,\./*  24 */
5c250 20 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30   0x05, 0x00, 0x0
5c260 34 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  4, 0x02, 0x02, 0
5c270 78 30 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c  x02, 0x04, 0x00,
5c280 5c 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78 30 30  \./*  32 */ 0x00
5c290 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
5c2a0 30 32 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20  02, 0x11, 0x11, 
5c2b0 30 78 30 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20  0x02, 0x05,\./* 
5c2c0 20 34 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30   40 */ 0x00, 0x0
5c2d0 32 2c 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30  2, 0x11, 0x04, 0
5c2e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 63 2c  x00, 0x00, 0x0c,
5c2f0 20 30 78 31 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a   0x11,\./*  48 *
5c300 2f 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  / 0x01, 0x02, 0x
5c310 30 31 2c 20 30 78 32 31 2c 20 30 78 30 38 2c 20  01, 0x21, 0x08, 
5c320 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31  0x00, 0x02, 0x01
5c330 2c 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30 78 31  ,\./*  56 */ 0x1
5c340 31 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30  1, 0x01, 0x02, 0
5c350 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c  x00, 0x00, 0x04,
5c360 20 30 78 30 30 2c 20 30 78 32 63 2c 5c 0a 2f 2a   0x00, 0x2c,\./*
5c370 20 20 36 34 20 2a 2f 20 30 78 32 63 2c 20 30 78    64 */ 0x2c, 0x
5c380 30 30 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20  00, 0x11, 0x00, 
5c390 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 31 35  0x05, 0x05, 0x15
5c3a0 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 20 37 32 20  , 0x15,\./*  72 
5c3b0 2a 2f 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30  */ 0x15, 0x15, 0
5c3c0 78 31 35 2c 20 30 78 31 35 2c 20 30 78 30 30 2c  x15, 0x15, 0x00,
5c3d0 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32   0x2c, 0x2c, 0x2
5c3e0 63 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78  c,\./*  80 */ 0x
5c3f0 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20  2c, 0x2c, 0x2c, 
5c400 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63  0x2c, 0x2c, 0x2c
5c410 2c 20 30 78 32 63 2c 20 30 78 30 35 2c 5c 0a 2f  , 0x2c, 0x05,\./
5c420 2a 20 20 38 38 20 2a 2f 20 30 78 30 30 2c 20 30  *  88 */ 0x00, 0
5c430 78 31 31 2c 20 30 78 30 34 2c 20 30 78 30 32 2c  x11, 0x04, 0x02,
5c440 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
5c450 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36  0, 0x00,\./*  96
5c460 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20   */ 0x00, 0x01, 
5c470 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
5c480 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
5c490 30 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30  00,\./* 104 */ 0
5c4a0 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x01, 0x00, 0x00,
5c4b0 20 30 78 30 31 2c 20 30 78 30 38 2c 20 30 78 31   0x01, 0x08, 0x1
5c4c0 31 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a  1, 0x00, 0x02,\.
5c4d0 2f 2a 20 31 31 32 20 2a 2f 20 30 78 30 32 2c 20  /* 112 */ 0x02, 
5c4e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
5c4f0 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78  , 0x00, 0x02, 0x
5c500 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32  00, 0x02,\./* 12
5c510 30 20 2a 2f 20 30 78 30 31 2c 20 30 78 31 31 2c  0 */ 0x01, 0x11,
5c520 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
5c530 35 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30  5, 0x00, 0x11, 0
5c540 78 30 35 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20  x05,\./* 128 */ 
5c550 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31  0x00, 0x02, 0x01
5c560 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
5c570 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c  00, 0x00, 0x00,\
5c580 0a 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30 30 2c  ./* 136 */ 0x00,
5c590 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
5c5a0 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30  0, 0x00, 0x04, 0
5c5b0 78 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31  x04, 0x04,\./* 1
5c5c0 34 34 20 2a 2f 20 30 78 30 34 2c 20 30 78 30 34  44 */ 0x04, 0x04
5c5d0 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ,}../***********
5c5e0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64  *** End of opcod
5c5f0 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.h ***********
5c600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c620 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
5c630 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
5c640 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
5c650 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a   in vdbe.h *****
5c660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c670 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f  **/../*.** Proto
5c680 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44  types for the VD
5c690 42 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53  BE interface.  S
5c6a0 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74  ee comments on t
5c6b0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
5c6c0 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72  n.** for a descr
5c6d0 69 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65  iption of what e
5c6e0 61 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75  ach of these rou
5c6f0 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53  tines does..*/.S
5c700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
5c710 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43  be *sqlite3VdbeC
5c720 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b  reate(sqlite3*);
5c730 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5c740 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
5c750 64 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29  ddOp0(Vdbe*,int)
5c760 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5c770 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
5c780 41 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74  AddOp1(Vdbe*,int
5c790 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
5c7a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5c7b0 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65  3VdbeAddOp2(Vdbe
5c7c0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a  *,int,int,int);.
5c7d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5c7e0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
5c7f0 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69  dOp3(Vdbe*,int,i
5c800 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  nt,int,int);.SQL
5c810 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5c820 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
5c830 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c  4(Vdbe*,int,int,
5c840 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  int,int,const ch
5c850 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51  ar *zP4,int);.SQ
5c860 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5c870 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
5c880 70 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74  pList(Vdbe*, int
5c890 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74   nOp, VdbeOpList
5c8a0 20 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51   const *aOp);.SQ
5c8b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
5c8c0 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
5c8d0 6e 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74  ngeP1(Vdbe*, int
5c8e0 20 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a   addr, int P1);.
5c8f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5c900 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
5c910 68 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69  hangeP2(Vdbe*, i
5c920 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29  nt addr, int P2)
5c930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5c940 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
5c950 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c  eChangeP3(Vdbe*,
5c960 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50   int addr, int P
5c970 33 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  3);.SQLITE_PRIVA
5c980 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
5c990 64 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65  dbeChangeP5(Vdbe
5c9a0 2a 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54  *, u8 P5);.SQLIT
5c9b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
5c9c0 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
5c9d0 72 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64  re(Vdbe*, int ad
5c9e0 64 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  dr);.SQLITE_PRIV
5c9f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5ca00 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70  VdbeChangeToNoop
5ca10 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72  (Vdbe*, int addr
5ca20 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45  , int N);.SQLITE
5ca30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5ca40 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
5ca50 34 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64  4(Vdbe*, int add
5ca60 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
5ca70 50 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49  P4, int N);.SQLI
5ca80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
5ca90 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
5caa0 74 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29  tree(Vdbe*, int)
5cab0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5cac0 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33   VdbeOp *sqlite3
5cad0 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c  VdbeGetOp(Vdbe*,
5cae0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
5caf0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5cb00 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56  3VdbeMakeLabel(V
5cb10 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
5cb20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
5cb30 65 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62  e3VdbeDelete(Vdb
5cb40 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
5cb50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5cb60 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64  VdbeMakeReady(Vd
5cb70 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c  be*,int,int,int,
5cb80 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
5cb90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5cba0 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62  VdbeFinalize(Vdb
5cbb0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
5cbc0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5cbd0 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
5cbe0 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51  (Vdbe*, int);.SQ
5cbf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5cc00 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
5cc10 65 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a  entAddr(Vdbe*);.
5cc20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
5cc30 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
5cc40 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
5cc50 33 56 64 62 65 54 72 61 63 65 28 56 64 62 65 2a  3VdbeTrace(Vdbe*
5cc60 2c 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a  ,FILE*);.#endif.
5cc70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5cc80 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52  oid sqlite3VdbeR
5cc90 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56  esetStepResult(V
5cca0 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
5ccb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5ccc0 33 56 64 62 65 52 65 73 65 74 28 56 64 62 65 2a  3VdbeReset(Vdbe*
5ccd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5cce0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
5ccf0 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62  beSetNumCols(Vdb
5cd00 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.SQLITE_
5cd10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5cd20 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
5cd30 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e  e(Vdbe*, int, in
5cd40 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  t, const char *,
5cd50 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29   void(*)(void*))
5cd60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5cd70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
5cd80 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64  eCountChanges(Vd
5cd90 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
5cda0 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71  VATE sqlite3 *sq
5cdb0 6c 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65  lite3VdbeDb(Vdbe
5cdc0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5cdd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
5cde0 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 2a 2c  dbeSetSql(Vdbe*,
5cdf0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
5ce00 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50  int n);.SQLITE_P
5ce10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
5ce20 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65  te3VdbeSwap(Vdbe
5ce30 2a 2c 56 64 62 65 2a 29 3b 0a 0a 23 69 66 64 65  *,Vdbe*);..#ifde
5ce40 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
5ce50 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
5ce60 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
5ce70 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
5ce80 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e  ReleaseMemory(in
5ce90 74 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  t);.#endif.SQLIT
5cea0 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b  E_PRIVATE Unpack
5ceb0 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65  edRecord *sqlite
5cec0 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63  3VdbeRecordUnpac
5ced0 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74 2c 63  k(KeyInfo*,int,c
5cee0 6f 6e 73 74 20 76 6f 69 64 2a 2c 0a 20 20 20 20  onst void*,.    
5cef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cf10 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f      UnpackedReco
5cf20 72 64 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  rd*,int);.SQLITE
5cf30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5cf40 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55  lite3VdbeDeleteU
5cf50 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e  npackedRecord(Un
5cf60 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a  packedRecord*);.
5cf70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5cf80 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  nt sqlite3VdbeRe
5cf90 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 2c  cordCompare(int,
5cfa0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 61  const void*,Unpa
5cfb0 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a 0a  ckedRecord*);...
5cfc0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53  #ifndef NDEBUG.S
5cfd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
5cfe0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
5cff0 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63  Comment(Vdbe*, c
5d000 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
5d010 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43  ;.# define VdbeC
5d020 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74  omment(X)  sqlit
5d030 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 0a  e3VdbeComment X.
5d040 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
5d050 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
5d060 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62  eNoopComment(Vdb
5d070 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e*, const char*,
5d080 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20   ...);.# define 
5d090 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
5d0a0 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 4e  X)  sqlite3VdbeN
5d0b0 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c  oopComment X.#el
5d0c0 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65  se.# define Vdbe
5d0d0 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 66  Comment(X).# def
5d0e0 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ine VdbeNoopComm
5d0f0 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23  ent(X).#endif..#
5d100 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
5d110 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64  ****** End of vd
5d120 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  be.h ***********
5d130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d150 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
5d160 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
5d170 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
5d180 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  off in sqliteInt
5d190 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
5d1a0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
5d1b0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70  ****** Include p
5d1c0 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69  ager.h in the mi
5d1d0 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
5d1e0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
5d1f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
5d200 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
5d210 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a  e pager.h ******
5d220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d240 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
5d250 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
5d260 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
5d270 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
5d280 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
5d290 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
5d2a0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
5d2b0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
5d2c0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
5d2d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
5d2e0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
5d2f0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
5d300 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
5d310 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
5d320 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
5d330 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
5d340 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
5d350 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
5d360 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
5d370 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
5d380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
5d3c0 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
5d3d0 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e  e defines the in
5d3e0 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65  terface that the
5d3f0 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63   sqlite page cac
5d400 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e  he.** subsystem.
5d410 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65    The page cache
5d420 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 73   subsystem reads
5d430 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 69   and writes a fi
5d440 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 20  le a page.** at 
5d450 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 69  a time and provi
5d460 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 6f  des a journal fo
5d470 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a  r rollback..**.*
5d480 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65  * @(#) $Id: page
5d490 72 2e 68 2c 76 20 31 2e 39 33 20 32 30 30 39 2f  r.h,v 1.93 2009/
5d4a0 30 31 2f 30 37 20 31 35 3a 31 38 3a 32 31 20 64  01/07 15:18:21 d
5d4b0 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
5d4c0 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41  .*/..#ifndef _PA
5d4d0 47 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  GER_H_.#define _
5d4e0 50 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  PAGER_H_../*.** 
5d4f0 49 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f  If defined as no
5d500 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63  n-zero, auto-vac
5d510 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62  uum is enabled b
5d520 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72  y default. Other
5d530 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20  wise.** it must 
5d540 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72  be turned on for
5d550 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75   each database u
5d560 73 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74  sing "PRAGMA aut
5d570 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a  o_vacuum = 1"..*
5d580 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
5d590 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c  _DEFAULT_JOURNAL
5d5a0 5f 53 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64  _SIZE_LIMIT.  #d
5d5b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
5d5c0 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a  AULT_JOURNAL_SIZ
5d5d0 45 5f 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69  E_LIMIT -1.#endi
5d5e0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70  f../*.** The typ
5d5f0 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  e used to repres
5d600 65 6e 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65  ent a page numbe
5d610 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61  r.  The first pa
5d620 67 65 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20  ge in a file.** 
5d630 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31  is called page 1
5d640 2e 20 20 30 20 69 73 20 75 73 65 64 20 74 6f 20  .  0 is used to 
5d650 72 65 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61  represent "not a
5d660 20 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64   page"..*/.typed
5d670 65 66 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a  ef u32 Pgno;../*
5d680 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69  .** Each open fi
5d690 6c 65 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79  le is managed by
5d6a0 20 61 20 73 65 70 61 72 61 74 65 20 69 6e 73 74   a separate inst
5d6b0 61 6e 63 65 20 6f 66 20 74 68 65 20 22 50 61 67  ance of the "Pag
5d6c0 65 72 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  er" structure..*
5d6d0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
5d6e0 20 50 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f   Pager Pager;../
5d6f0 2a 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65  *.** Handle type
5d700 20 66 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74   for pages..*/.t
5d710 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67  ypedef struct Pg
5d720 48 64 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a  Hdr DbPage;../*.
5d730 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
5d740 73 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73 20  s for the flags 
5d750 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
5d760 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 29 2e  ite3PagerOpen().
5d770 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54 68 69  .**.** NOTE: Thi
5d780 73 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61  s values must ma
5d790 74 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f  tch the correspo
5d7a0 6e 64 69 6e 67 20 42 54 52 45 45 5f 20 76 61 6c  nding BTREE_ val
5d7b0 75 65 73 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a  ues in btree.h..
5d7c0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
5d7d0 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 30  _OMIT_JOURNAL  0
5d7e0 78 30 30 30 31 20 20 20 20 2f 2a 20 44 6f 20 6e  x0001    /* Do n
5d7f0 6f 74 20 75 73 65 20 61 20 72 6f 6c 6c 62 61 63  ot use a rollbac
5d800 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65  k journal */.#de
5d810 66 69 6e 65 20 50 41 47 45 52 5f 4e 4f 5f 52 45  fine PAGER_NO_RE
5d820 41 44 4c 4f 43 4b 20 20 20 30 78 30 30 30 32 20  ADLOCK   0x0002 
5d830 20 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c     /* Omit readl
5d840 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79  ocks on readonly
5d850 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a   files */../*.**
5d860 20 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f   Valid values fo
5d870 72 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  r the second arg
5d880 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
5d890 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65  PagerLockingMode
5d8a0 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ()..*/.#define P
5d8b0 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
5d8c0 5f 51 55 45 52 59 20 20 20 20 20 20 2d 31 0a 23  _QUERY      -1.#
5d8d0 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43  define PAGER_LOC
5d8e0 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20  KINGMODE_NORMAL 
5d8f0 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50       0.#define P
5d900 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
5d910 5f 45 58 43 4c 55 53 49 56 45 20 20 20 31 0a 0a  _EXCLUSIVE   1..
5d920 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75  /*.** Valid valu
5d930 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  es for the secon
5d940 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  d argument to sq
5d950 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
5d960 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66  lMode()..*/.#def
5d970 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ine PAGER_JOURNA
5d980 4c 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20  LMODE_QUERY     
5d990 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45   -1.#define PAGE
5d9a0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45  R_JOURNALMODE_DE
5d9b0 4c 45 54 45 20 20 20 20 20 20 30 20 20 20 2f 2a  LETE      0   /*
5d9c0 20 43 6f 6d 6d 69 74 20 62 79 20 64 65 6c 65 74   Commit by delet
5d9d0 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ing journal file
5d9e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
5d9f0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  R_JOURNALMODE_PE
5da00 52 53 49 53 54 20 20 20 20 20 31 20 20 20 2f 2a  RSIST     1   /*
5da10 20 43 6f 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69   Commit by zeroi
5da20 6e 67 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  ng journal heade
5da30 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  r */.#define PAG
5da40 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
5da50 46 46 20 20 20 20 20 20 20 20 20 32 20 20 20 2f  FF         2   /
5da60 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65  * Journal omitte
5da70 64 2e 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  d.  */.#define P
5da80 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
5da90 5f 54 52 55 4e 43 41 54 45 20 20 20 20 33 20 20  _TRUNCATE    3  
5daa0 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 74 72   /* Commit by tr
5dab0 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c  uncating journal
5dac0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
5dad0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
5dae0 4d 4f 52 59 20 20 20 20 20 20 34 20 20 20 2f 2a  MORY      4   /*
5daf0 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   In-memory journ
5db00 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a  al file */../*.*
5db10 2a 20 53 65 65 20 73 6f 75 72 63 65 20 63 6f 64  * See source cod
5db20 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 61  e comments for a
5db30 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69   detailed descri
5db40 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 6c  ption of the fol
5db50 6c 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e  lowing.** routin
5db60 65 73 3a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es:.*/.SQLITE_PR
5db70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5db80 33 50 61 67 65 72 4f 70 65 6e 28 73 71 6c 69 74  3PagerOpen(sqlit
5db90 65 33 5f 76 66 73 20 2a 2c 20 50 61 67 65 72 20  e3_vfs *, Pager 
5dba0 2a 2a 70 70 50 61 67 65 72 2c 20 63 6f 6e 73 74  **ppPager, const
5dbb0 20 63 68 61 72 2a 2c 20 69 6e 74 2c 69 6e 74 2c   char*, int,int,
5dbc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
5dbd0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5dbe0 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
5dbf0 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74  dler(Pager*, int
5dc00 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69  (*)(void *), voi
5dc10 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  d *);.SQLITE_PRI
5dc20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5dc30 33 50 61 67 65 72 53 65 74 52 65 69 6e 69 74 65  3PagerSetReinite
5dc40 72 28 50 61 67 65 72 2a 2c 20 76 6f 69 64 28 2a  r(Pager*, void(*
5dc50 29 28 44 62 50 61 67 65 2a 29 29 3b 0a 53 51 4c  )(DbPage*));.SQL
5dc60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5dc70 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
5dc80 61 67 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20  agesize(Pager*, 
5dc90 75 31 36 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  u16*);.SQLITE_PR
5dca0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5dcb0 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75  3PagerMaxPageCou
5dcc0 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b  nt(Pager*, int);
5dcd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5dce0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
5dcf0 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50  ReadFileheader(P
5dd00 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e 73 69  ager*, int, unsi
5dd10 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 53 51 4c  gned char*);.SQL
5dd20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
5dd30 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
5dd40 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a  Cachesize(Pager*
5dd50 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
5dd60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5dd70 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 67  e3PagerClose(Pag
5dd80 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c  er *pPager);.SQL
5dd90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5dda0 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75  sqlite3PagerAcqu
5ddb0 69 72 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ire(Pager *pPage
5ddc0 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62  r, Pgno pgno, Db
5ddd0 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 69  Page **ppPage, i
5dde0 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65  nt clrFlag);.#de
5ddf0 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 67 65  fine sqlite3Page
5de00 72 47 65 74 28 41 2c 42 2c 43 29 20 73 71 6c 69  rGet(A,B,C) sqli
5de10 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28  te3PagerAcquire(
5de20 41 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f  A,B,C,0).SQLITE_
5de30 50 52 49 56 41 54 45 20 44 62 50 61 67 65 20 2a  PRIVATE DbPage *
5de40 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b  sqlite3PagerLook
5de50 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  up(Pager *pPager
5de60 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51  , Pgno pgno);.SQ
5de70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5de80 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
5de90 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65  eRefcount(DbPage
5dea0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5deb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
5dec0 67 65 72 52 65 66 28 44 62 50 61 67 65 2a 29 3b  gerRef(DbPage*);
5ded0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5dee0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
5def0 55 6e 72 65 66 28 44 62 50 61 67 65 2a 29 3b 0a  Unref(DbPage*);.
5df00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5df10 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  nt sqlite3PagerW
5df20 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53  rite(DbPage*);.S
5df30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5df40 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  t sqlite3PagerPa
5df50 67 65 63 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20  gecount(Pager*, 
5df60 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
5df70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5df80 33 50 61 67 65 72 42 65 67 69 6e 28 44 62 50 61  3PagerBegin(DbPa
5df90 67 65 2a 2c 20 69 6e 74 20 65 78 46 6c 61 67 29  ge*, int exFlag)
5dfa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5dfb0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
5dfc0 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
5dfd0 50 61 67 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61  Pager*,const cha
5dfe0 72 20 2a 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29  r *zMaster, int)
5dff0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5e000 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
5e010 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
5e020 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
5e030 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5e040 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
5e050 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
5e060 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69  _PRIVATE u8 sqli
5e070 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e  te3PagerIsreadon
5e080 6c 79 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  ly(Pager*);.SQLI
5e090 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
5e0a0 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
5e0b0 52 6f 6c 6c 62 61 63 6b 28 44 62 50 61 67 65 2a  Rollback(DbPage*
5e0c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5e0d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
5e0e0 65 72 44 6f 6e 74 57 72 69 74 65 28 44 62 50 61  erDontWrite(DbPa
5e0f0 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ge*);.SQLITE_PRI
5e100 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5e110 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61  PagerRefcount(Pa
5e120 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
5e130 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
5e140 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79  e3PagerSetSafety
5e150 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74  Level(Pager*,int
5e160 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
5e170 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
5e180 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
5e190 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a  lename(Pager*);.
5e1a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
5e1b0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73  onst sqlite3_vfs
5e1c0 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66   *sqlite3PagerVf
5e1d0 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  s(Pager*);.SQLIT
5e1e0 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
5e1f0 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33 50  3_file *sqlite3P
5e200 61 67 65 72 46 69 6c 65 28 50 61 67 65 72 2a 29  agerFile(Pager*)
5e210 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5e220 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
5e230 69 74 65 33 50 61 67 65 72 44 69 72 6e 61 6d 65  ite3PagerDirname
5e240 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
5e250 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
5e260 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65  har *sqlite3Page
5e270 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67  rJournalname(Pag
5e280 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
5e290 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5e2a0 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65  PagerNosync(Page
5e2b0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
5e2c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
5e2d0 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61 67  agerMovepage(Pag
5e2e0 65 72 2a 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f  er*,DbPage*,Pgno
5e2f0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
5e300 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
5e310 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
5e320 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49  DbPage *); .SQLI
5e330 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
5e340 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  *sqlite3PagerGet
5e350 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b  Extra(DbPage *);
5e360 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45   .SQLITE_PRIVATE
5e370 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
5e380 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67  rLockingMode(Pag
5e390 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  er *, int);.SQLI
5e3a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5e3b0 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
5e3c0 61 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20  alMode(Pager *, 
5e3d0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
5e3e0 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33  VATE i64 sqlite3
5e3f0 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65  PagerJournalSize
5e400 4c 69 6d 69 74 28 50 61 67 65 72 20 2a 2c 20 69  Limit(Pager *, i
5e410 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
5e420 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
5e430 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28  3PagerTempSpace(
5e440 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
5e450 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5e460 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 61 67  te3PagerSync(Pag
5e470 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 0a 53 51  er *pPager);..SQ
5e480 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5e490 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
5e4a0 6e 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72  nSavepoint(Pager
5e4b0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 29   *pPager, int n)
5e4c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5e4d0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
5e4e0 72 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72  rSavepoint(Pager
5e4f0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6f 70   *pPager, int op
5e500 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74  , int iSavepoint
5e510 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
5e520 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
5e530 55 4d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UM.SQLITE_PRIVAT
5e540 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
5e550 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61  PagerTruncateIma
5e560 67 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f 29 3b  ge(Pager*,Pgno);
5e570 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5e580 20 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 61    Pgno sqlite3Pa
5e590 67 65 72 49 6d 61 67 65 53 69 7a 65 28 50 61 67  gerImageSize(Pag
5e5a0 65 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23  er *);.#endif..#
5e5b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
5e5c0 5f 43 4f 44 45 43 0a 53 51 4c 49 54 45 5f 50 52  _CODEC.SQLITE_PR
5e5d0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
5e5e0 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 65  ite3PagerSetCode
5e5f0 63 28 50 61 67 65 72 2a 2c 76 6f 69 64 2a 28 2a  c(Pager*,void*(*
5e600 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67  )(void*,void*,Pg
5e610 6e 6f 2c 69 6e 74 29 2c 76 6f 69 64 2a 29 3b 0a  no,int),void*);.
5e620 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66  #endif..#if !def
5e630 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20  ined(NDEBUG) || 
5e640 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
5e650 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  EST).SQLITE_PRIV
5e660 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 74  ATE   Pgno sqlit
5e670 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65  e3PagerPagenumbe
5e680 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49  r(DbPage*);.SQLI
5e690 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
5e6a0 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
5e6b0 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 2a  riteable(DbPage*
5e6c0 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  );.#endif..#ifde
5e6d0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
5e6e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
5e6f0 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  nt *sqlite3Pager
5e700 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53  Stats(Pager*);.S
5e710 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
5e720 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
5e730 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29  rRefdump(Pager*)
5e740 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5e750 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61     int sqlite3Pa
5e760 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72  gerIsMemdb(Pager
5e770 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  *);.#endif..#ifd
5e780 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 76  ef SQLITE_TEST.v
5e790 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d 75  oid disable_simu
5e7a0 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
5e7b0 76 6f 69 64 29 3b 0a 76 6f 69 64 20 65 6e 61 62  void);.void enab
5e7c0 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f  le_simulated_io_
5e7d0 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a 23 65  errors(void);.#e
5e7e0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73  lse.# define dis
5e7f0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
5e800 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66  o_errors().# def
5e810 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c  ine enable_simul
5e820 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
5e830 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
5e840 2f 2a 20 5f 50 41 47 45 52 5f 48 5f 20 2a 2f 0a  /* _PAGER_H_ */.
5e850 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
5e860 20 45 6e 64 20 6f 66 20 70 61 67 65 72 2e 68 20   End of pager.h 
5e870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
5e8a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
5e8b0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
5e8c0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
5e8d0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
5e8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
5e8f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
5e900 20 49 6e 63 6c 75 64 65 20 70 63 61 63 68 65 2e   Include pcache.
5e910 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
5e920 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  of sqliteInt.h *
5e930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
5e940 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
5e950 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 61 63   Begin file pcac
5e960 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  he.h ***********
5e970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
5e990 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75  ./*.** 2008 Augu
5e9a0 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 05.**.** The 
5e9b0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
5e9c0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
5e9d0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
5e9e0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
5e9f0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
5ea00 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
5ea10 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
5ea20 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
5ea30 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
5ea40 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
5ea50 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
5ea60 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
5ea70 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
5ea80 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
5ea90 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
5eaa0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
5eab0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
5eac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ead0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5eae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5eaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5eb00 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  ****.** This hea
5eb10 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73  der file defines
5eb20 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   the interface t
5eb30 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 70  hat the sqlite p
5eb40 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62  age cache.** sub
5eb50 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 40  system. .**.** @
5eb60 28 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 2e  (#) $Id: pcache.
5eb70 68 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31 31  h,v 1.16 2008/11
5eb80 2f 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e  /19 16:52:44 dan
5eb90 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
5eba0 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 43 41 43  /..#ifndef _PCAC
5ebb0 48 45 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20 73  HE_H_..typedef s
5ebc0 74 72 75 63 74 20 50 67 48 64 72 20 50 67 48 64  truct PgHdr PgHd
5ebd0 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
5ebe0 74 20 50 43 61 63 68 65 20 50 43 61 63 68 65 3b  t PCache PCache;
5ebf0 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 72 79 20 70 61  ../*.** Every pa
5ec00 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20  ge in the cache 
5ec10 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79  is controlled by
5ec20 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
5ec30 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
5ec40 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
5ec50 74 72 75 63 74 20 50 67 48 64 72 20 7b 0a 20 20  truct PgHdr {.  
5ec60 76 6f 69 64 20 2a 70 44 61 74 61 3b 20 20 20 20  void *pData;    
5ec70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5ec80 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69  * Content of thi
5ec90 73 20 70 61 67 65 20 2a 2f 0a 20 20 76 6f 69 64  s page */.  void
5eca0 20 2a 70 45 78 74 72 61 3b 20 20 20 20 20 20 20   *pExtra;       
5ecb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
5ecc0 74 72 61 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20  tra content */. 
5ecd0 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 3b 20   PgHdr *pDirty; 
5ece0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ecf0 2f 2a 20 54 72 61 6e 73 69 65 6e 74 20 6c 69 73  /* Transient lis
5ed00 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73  t of dirty pages
5ed10 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b   */.  Pgno pgno;
5ed20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ed30 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
5ed40 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61 67  ber for this pag
5ed50 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 50  e */.  Pager *pP
5ed60 61 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  ager;           
5ed70 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
5ed80 65 72 20 74 68 69 73 20 70 61 67 65 20 69 73 20  er this page is 
5ed90 70 61 72 74 20 6f 66 20 2a 2f 0a 23 69 66 64 65  part of */.#ifde
5eda0 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
5edb0 41 47 45 53 0a 20 20 75 33 32 20 70 61 67 65 48  AGES.  u32 pageH
5edc0 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  ash;            
5edd0 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 6f 66        /* Hash of
5ede0 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 2a 2f   page content */
5edf0 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20 66 6c  .#endif.  u16 fl
5ee00 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
5ee10 20 20 20 20 20 20 20 20 20 2f 2a 20 50 47 48 44           /* PGHD
5ee20 52 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20  R flags defined 
5ee30 62 65 6c 6f 77 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a  below */..  /***
5ee40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ee50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ee60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ee70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ee80 2a 2a 2a 0a 20 20 2a 2a 20 45 6c 65 6d 65 6e 74  ***.  ** Element
5ee90 73 20 61 62 6f 76 65 20 61 72 65 20 70 75 62 6c  s above are publ
5eea0 69 63 2e 20 20 41 6c 6c 20 74 68 61 74 20 66 6f  ic.  All that fo
5eeb0 6c 6c 6f 77 73 20 69 73 20 70 72 69 76 61 74 65  llows is private
5eec0 20 74 6f 20 70 63 61 63 68 65 2e 63 0a 20 20 2a   to pcache.c.  *
5eed0 2a 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74  * and should not
5eee0 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20   be accessed by 
5eef0 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 20  other modules.. 
5ef00 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 66 3b 20   */.  i16 nRef; 
5ef10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ef20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5ef30 66 20 75 73 65 72 73 20 6f 66 20 74 68 69 73 20  f users of this 
5ef40 70 61 67 65 20 2a 2f 0a 20 20 50 43 61 63 68 65  page */.  PCache
5ef50 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20   *pCache;       
5ef60 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68           /* Cach
5ef70 65 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73  e that owns this
5ef80 20 70 61 67 65 20 2a 2f 0a 0a 20 20 50 67 48 64   page */..  PgHd
5ef90 72 20 2a 70 44 69 72 74 79 4e 65 78 74 3b 20 20  r *pDirtyNext;  
5efa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
5efb0 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69  xt element in li
5efc0 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65  st of dirty page
5efd0 73 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44  s */.  PgHdr *pD
5efe0 69 72 74 79 50 72 65 76 3b 20 20 20 20 20 20 20  irtyPrev;       
5eff0 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75        /* Previou
5f000 73 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 73  s element in lis
5f010 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73  t of dirty pages
5f020 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 42 69 74 20 76   */.};../* Bit v
5f030 61 6c 75 65 73 20 66 6f 72 20 50 67 48 64 72 2e  alues for PgHdr.
5f040 66 6c 61 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65  flags */.#define
5f050 20 50 47 48 44 52 5f 44 49 52 54 59 20 20 20 20   PGHDR_DIRTY    
5f060 20 20 20 20 20 20 20 20 20 30 78 30 30 32 20 20           0x002  
5f070 2f 2a 20 50 61 67 65 20 68 61 73 20 63 68 61 6e  /* Page has chan
5f080 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ged */.#define P
5f090 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 20  GHDR_NEED_SYNC  
5f0a0 20 20 20 20 20 20 20 30 78 30 30 34 20 20 2f 2a         0x004  /*
5f0b0 20 46 73 79 6e 63 20 74 68 65 20 72 6f 6c 6c 62   Fsync the rollb
5f0c0 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f  ack journal befo
5f0d0 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  re.             
5f0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5f0f0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 72 69            ** wri
5f100 74 69 6e 67 20 74 68 69 73 20 70 61 67 65 20 74  ting this page t
5f110 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  o the database *
5f120 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f  /.#define PGHDR_
5f130 4e 45 45 44 5f 52 45 41 44 20 20 20 20 20 20 20  NEED_READ       
5f140 20 20 30 78 30 30 38 20 20 2f 2a 20 43 6f 6e 74    0x008  /* Cont
5f150 65 6e 74 20 69 73 20 75 6e 72 65 61 64 20 2a 2f  ent is unread */
5f160 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 52  .#define PGHDR_R
5f170 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 20 20  EUSE_UNLIKELY   
5f180 20 30 78 30 31 30 20 20 2f 2a 20 41 20 68 69 6e   0x010  /* A hin
5f190 74 20 74 68 61 74 20 72 65 75 73 65 20 69 73 20  t that reuse is 
5f1a0 75 6e 6c 69 6b 65 6c 79 20 2a 2f 0a 23 64 65 66  unlikely */.#def
5f1b0 69 6e 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57  ine PGHDR_DONT_W
5f1c0 52 49 54 45 20 20 20 20 20 20 20 20 30 78 30 32  RITE        0x02
5f1d0 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69  0  /* Do not wri
5f1e0 74 65 20 63 6f 6e 74 65 6e 74 20 74 6f 20 64 69  te content to di
5f1f0 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 6e 69 74 69 61  sk */../* Initia
5f200 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f 77  lize and shutdow
5f210 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
5f220 20 73 75 62 73 79 73 74 65 6d 20 2a 2f 0a 53 51   subsystem */.SQ
5f230 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5f240 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e   sqlite3PcacheIn
5f250 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a  itialize(void);.
5f260 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5f270 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
5f280 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b  eShutdown(void);
5f290 0a 0a 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20  ../* Page cache 
5f2a0 62 75 66 66 65 72 20 6d 61 6e 61 67 65 6d 65 6e  buffer managemen
5f2b0 74 3a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  t:.** These rout
5f2c0 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 53  ines implement S
5f2d0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
5f2e0 45 43 41 43 48 45 2e 0a 2a 2f 0a 53 51 4c 49 54  ECACHE..*/.SQLIT
5f2f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
5f300 71 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66  qlite3PCacheBuff
5f310 65 72 53 65 74 75 70 28 76 6f 69 64 20 2a 2c 20  erSetup(void *, 
5f320 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 3b 0a  int sz, int n);.
5f330 0a 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  ./* Create a new
5f340 20 70 61 67 65 72 20 63 61 63 68 65 2e 0a 2a 2a   pager cache..**
5f350 20 55 6e 64 65 72 20 6d 65 6d 6f 72 79 20 73 74   Under memory st
5f360 72 65 73 73 2c 20 69 6e 76 6f 6b 65 20 78 53 74  ress, invoke xSt
5f370 72 65 73 73 20 74 6f 20 74 72 79 20 74 6f 20 6d  ress to try to m
5f380 61 6b 65 20 70 61 67 65 73 20 63 6c 65 61 6e 2e  ake pages clean.
5f390 0a 2a 2a 20 4f 6e 6c 79 20 63 6c 65 61 6e 20 61  .** Only clean a
5f3a0 6e 64 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65  nd unpinned page
5f3b0 73 20 63 61 6e 20 62 65 20 72 65 63 6c 61 69 6d  s can be reclaim
5f3c0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
5f3d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
5f3e0 65 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20  e3PcacheOpen(.  
5f3f0 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20  int szPage,     
5f400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5f410 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20  * Size of every 
5f420 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  page */.  int sz
5f430 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 20  Extra,          
5f440 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72           /* Extr
5f450 61 20 73 70 61 63 65 20 61 73 73 6f 63 69 61 74  a space associat
5f460 65 64 20 77 69 74 68 20 65 61 63 68 20 70 61 67  ed with each pag
5f470 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67  e */.  int bPurg
5f480 65 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20  eable,          
5f490 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
5f4a0 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61   pages are on ba
5f4b0 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20  cking store */. 
5f4c0 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28   int (*xStress)(
5f4d0 76 6f 69 64 2a 2c 20 50 67 48 64 72 2a 29 2c 20  void*, PgHdr*), 
5f4e0 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74  /* Call to try t
5f4f0 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65  o make pages cle
5f500 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  an */.  void *pS
5f510 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 20  tress,          
5f520 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
5f530 6e 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f  nt to xStress */
5f540 0a 20 20 50 43 61 63 68 65 20 2a 70 54 6f 49 6e  .  PCache *pToIn
5f550 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  it              
5f560 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65    /* Preallocate
5f570 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  d space for the 
5f580 50 43 61 63 68 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a  PCache */.);../*
5f590 20 4d 6f 64 69 66 79 20 74 68 65 20 70 61 67 65   Modify the page
5f5a0 2d 73 69 7a 65 20 61 66 74 65 72 20 74 68 65 20  -size after the 
5f5b0 63 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 63  cache has been c
5f5c0 72 65 61 74 65 64 2e 20 2a 2f 0a 53 51 4c 49 54  reated. */.SQLIT
5f5d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
5f5e0 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 50  qlite3PcacheSetP
5f5f0 61 67 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a  ageSize(PCache *
5f600 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 52 65 74 75  , int);../* Retu
5f610 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62  rn the size in b
5f620 79 74 65 73 20 6f 66 20 61 20 50 43 61 63 68 65  ytes of a PCache
5f630 20 6f 62 6a 65 63 74 2e 20 20 55 73 65 64 20 74   object.  Used t
5f640 6f 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a  o preallocate.**
5f650 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a   storage space..
5f660 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
5f670 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
5f680 63 68 65 53 69 7a 65 28 76 6f 69 64 29 3b 0a 0a  cheSize(void);..
5f690 2f 2a 20 4f 6e 65 20 72 65 6c 65 61 73 65 20 70  /* One release p
5f6a0 65 72 20 73 75 63 63 65 73 73 66 75 6c 20 66 65  er successful fe
5f6b0 74 63 68 2e 20 20 50 61 67 65 20 69 73 20 70 69  tch.  Page is pi
5f6c0 6e 6e 65 64 20 75 6e 74 69 6c 20 72 65 6c 65 61  nned until relea
5f6d0 73 65 64 2e 0a 2a 2a 20 52 65 66 65 72 65 6e 63  sed..** Referenc
5f6e0 65 20 63 6f 75 6e 74 65 64 2e 20 0a 2a 2f 0a 53  e counted. .*/.S
5f6f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5f700 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46  t sqlite3PcacheF
5f710 65 74 63 68 28 50 43 61 63 68 65 2a 2c 20 50 67  etch(PCache*, Pg
5f720 6e 6f 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c  no, int createFl
5f730 61 67 2c 20 50 67 48 64 72 2a 2a 29 3b 0a 53 51  ag, PgHdr**);.SQ
5f740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
5f750 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52  d sqlite3PcacheR
5f760 65 6c 65 61 73 65 28 50 67 48 64 72 2a 29 3b 0a  elease(PgHdr*);.
5f770 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f780 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
5f790 68 65 44 72 6f 70 28 50 67 48 64 72 2a 29 3b 20  heDrop(PgHdr*); 
5f7a0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76          /* Remov
5f7b0 65 20 70 61 67 65 20 66 72 6f 6d 20 63 61 63 68  e page from cach
5f7c0 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e */.SQLITE_PRIV
5f7d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5f7e0 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28  PcacheMakeDirty(
5f7f0 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d  PgHdr*);    /* M
5f800 61 6b 65 20 73 75 72 65 20 70 61 67 65 20 69 73  ake sure page is
5f810 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 2a 2f   marked dirty */
5f820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f830 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
5f840 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64  heMakeClean(PgHd
5f850 72 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20  r*);    /* Mark 
5f860 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 61 73  a single page as
5f870 20 63 6c 65 61 6e 20 2a 2f 0a 53 51 4c 49 54 45   clean */.SQLITE
5f880 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5f890 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e  lite3PcacheClean
5f8a0 41 6c 6c 28 50 43 61 63 68 65 2a 29 3b 20 20 20  All(PCache*);   
5f8b0 20 2f 2a 20 4d 61 72 6b 20 61 6c 6c 20 64 69 72   /* Mark all dir
5f8c0 74 79 20 6c 69 73 74 20 70 61 67 65 73 20 61 73  ty list pages as
5f8d0 20 63 6c 65 61 6e 20 2a 2f 0a 0a 2f 2a 20 43 68   clean */../* Ch
5f8e0 61 6e 67 65 20 61 20 70 61 67 65 20 6e 75 6d 62  ange a page numb
5f8f0 65 72 2e 20 20 55 73 65 64 20 62 79 20 69 6e 63  er.  Used by inc
5f900 72 2d 76 61 63 75 75 6d 2e 20 2a 2f 0a 53 51 4c  r-vacuum. */.SQL
5f910 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
5f920 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f   sqlite3PcacheMo
5f930 76 65 28 50 67 48 64 72 2a 2c 20 50 67 6e 6f 29  ve(PgHdr*, Pgno)
5f940 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c  ;../* Remove all
5f950 20 70 61 67 65 73 20 77 69 74 68 20 70 67 6e 6f   pages with pgno
5f960 3e 78 2e 20 20 52 65 73 65 74 20 74 68 65 20 63  >x.  Reset the c
5f970 61 63 68 65 20 69 66 20 78 3d 3d 30 20 2a 2f 0a  ache if x==0 */.
5f980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5f990 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
5f9a0 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65  eTruncate(PCache
5f9b0 2a 2c 20 50 67 6e 6f 20 78 29 3b 0a 0a 2f 2a 20  *, Pgno x);../* 
5f9c0 47 65 74 20 61 20 6c 69 73 74 20 6f 66 20 61 6c  Get a list of al
5f9d0 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e  l dirty pages in
5f9e0 20 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74   the cache, sort
5f9f0 65 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65  ed by page numbe
5fa00 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  r */.SQLITE_PRIV
5fa10 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74  ATE PgHdr *sqlit
5fa20 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73  e3PcacheDirtyLis
5fa30 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20  t(PCache*);../* 
5fa40 52 65 73 65 74 20 61 6e 64 20 63 6c 6f 73 65 20  Reset and close 
5fa50 74 68 65 20 63 61 63 68 65 20 6f 62 6a 65 63 74  the cache object
5fa60 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
5fa70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
5fa80 63 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68  cacheClose(PCach
5fa90 65 2a 29 3b 0a 0a 2f 2a 20 43 6c 65 61 72 20 66  e*);../* Clear f
5faa0 6c 61 67 73 20 66 72 6f 6d 20 70 61 67 65 73 20  lags from pages 
5fab0 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68  of the page cach
5fac0 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e */.SQLITE_PRIV
5fad0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5fae0 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46  PcacheClearSyncF
5faf0 6c 61 67 73 28 50 43 61 63 68 65 20 2a 29 3b 0a  lags(PCache *);.
5fb00 0a 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65 20  ./* Discard the 
5fb10 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
5fb20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f  cache */.SQLITE_
5fb30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5fb40 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 50  te3PcacheClear(P
5fb50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74  Cache*);../* Ret
5fb60 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  urn the total nu
5fb70 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64  mber of outstand
5fb80 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e  ing page referen
5fb90 63 65 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ces */.SQLITE_PR
5fba0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5fbb0 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
5fbc0 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e  PCache*);../* In
5fbd0 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65  crement the refe
5fbe0 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20 61  rence count of a
5fbf0 6e 20 65 78 69 73 74 69 6e 67 20 70 61 67 65 20  n existing page 
5fc00 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
5fc10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
5fc20 61 63 68 65 52 65 66 28 50 67 48 64 72 2a 29 3b  acheRef(PgHdr*);
5fc30 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5fc40 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
5fc50 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 50  hePageRefcount(P
5fc60 67 48 64 72 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75  gHdr*);../* Retu
5fc70 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  rn the total num
5fc80 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 6f  ber of pages sto
5fc90 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65  red in the cache
5fca0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
5fcb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
5fcc0 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 50 43  achePagecount(PC
5fcd0 61 63 68 65 2a 29 3b 0a 0a 23 69 66 64 65 66 20  ache*);..#ifdef 
5fce0 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
5fcf0 45 53 0a 2f 2a 20 49 74 65 72 61 74 65 20 74 68  ES./* Iterate th
5fd00 72 6f 75 67 68 20 61 6c 6c 20 64 69 72 74 79 20  rough all dirty 
5fd10 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20  pages currently 
5fd20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61  stored in the ca
5fd30 63 68 65 2e 20 54 68 69 73 0a 2a 2a 20 69 6e 74  che. This.** int
5fd40 65 72 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 61  erface is only a
5fd50 76 61 69 6c 61 62 6c 65 20 69 66 20 53 51 4c 49  vailable if SQLI
5fd60 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 69  TE_CHECK_PAGES i
5fd70 73 20 64 65 66 69 6e 65 64 20 77 68 65 6e 20 74  s defined when t
5fd80 68 65 20 0a 2a 2a 20 6c 69 62 72 61 72 79 20 69  he .** library i
5fd90 73 20 62 75 69 6c 74 2e 0a 2a 2f 0a 53 51 4c 49  s built..*/.SQLI
5fda0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
5fdb0 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65  sqlite3PcacheIte
5fdc0 72 61 74 65 44 69 72 74 79 28 50 43 61 63 68 65  rateDirty(PCache
5fdd0 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28   *pCache, void (
5fde0 2a 78 49 74 65 72 29 28 50 67 48 64 72 20 2a 29  *xIter)(PgHdr *)
5fdf0 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 53 65  );.#endif../* Se
5fe00 74 20 61 6e 64 20 67 65 74 20 74 68 65 20 73 75  t and get the su
5fe10 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69  ggested cache-si
5fe20 7a 65 20 66 6f 72 20 74 68 65 20 73 70 65 63 69  ze for the speci
5fe30 66 69 65 64 20 70 61 67 65 72 2d 63 61 63 68 65  fied pager-cache
5fe40 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c  ..**.** If no gl
5fe50 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 69 73 20  obal maximum is 
5fe60 63 6f 6e 66 69 67 75 72 65 64 2c 20 74 68 65 6e  configured, then
5fe70 20 74 68 65 20 73 79 73 74 65 6d 20 61 74 74 65   the system atte
5fe80 6d 70 74 73 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a  mpts to limit.**
5fe90 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
5fea0 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 65  r of pages cache
5feb0 64 20 62 79 20 70 75 72 67 65 61 62 6c 65 20 70  d by purgeable p
5fec0 61 67 65 72 2d 63 61 63 68 65 73 20 74 6f 20 74  ager-caches to t
5fed0 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 65  he sum.** of the
5fee0 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68 65   suggested cache
5fef0 2d 73 69 7a 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  -sizes..*/.SQLIT
5ff00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
5ff10 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43  qlite3PcacheSetC
5ff20 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20  achesize(PCache 
5ff30 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65 66 20  *, int);.#ifdef 
5ff40 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
5ff50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5ff60 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43  qlite3PcacheGetC
5ff70 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20  achesize(PCache 
5ff80 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  *);.#endif..#ifd
5ff90 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
5ffa0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
5ffb0 4e 54 0a 2f 2a 20 54 72 79 20 74 6f 20 72 65 74  NT./* Try to ret
5ffc0 75 72 6e 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  urn memory used 
5ffd0 62 79 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f  by the pcache mo
5ffe0 64 75 6c 65 20 74 6f 20 74 68 65 20 6d 61 69 6e  dule to the main
5fff0 20 6d 65 6d 6f 72 79 20 68 65 61 70 20 2a 2f 0a   memory heap */.
60000 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
60010 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
60020 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e  ReleaseMemory(in
60030 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  t);.#endif..#ifd
60040 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
60050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
60060 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
60070 53 74 61 74 73 28 69 6e 74 2a 2c 69 6e 74 2a 2c  Stats(int*,int*,
60080 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 23 65 6e 64  int*,int*);.#end
60090 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
600a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
600b0 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28  CacheSetDefault(
600c0 76 6f 69 64 29 3b 0a 0a 23 65 6e 64 69 66 20 2f  void);..#endif /
600d0 2a 20 5f 50 43 41 43 48 45 5f 48 5f 20 2a 2f 0a  * _PCACHE_H_ */.
600e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
600f0 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 2e 68   End of pcache.h
60100 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
60110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
60130 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
60140 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
60150 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
60160 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
60170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
60180 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
60190 2a 20 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20 69  * Include os.h i
601a0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
601b0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
601c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
601d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
601e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e  * Begin file os.
601f0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
60200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60220 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
60230 74 65 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20  tember 16.**.** 
60240 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
60250 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
60260 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
60270 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
60280 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
60290 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
602a0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
602b0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
602c0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
602d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
602e0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
602f0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
60300 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
60310 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
60320 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
60330 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
60340 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
60350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
603a0 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
603b0 66 69 6c 65 20 28 74 6f 67 65 74 68 65 72 20 77  file (together w
603c0 69 74 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f 6e  ith is companion
603d0 20 43 20 73 6f 75 72 63 65 2d 63 6f 64 65 20 66   C source-code f
603e0 69 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20 61  ile.** "os.c") a
603f0 74 74 65 6d 70 74 20 74 6f 20 61 62 73 74 72 61  ttempt to abstra
60400 63 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  ct the underlyin
60410 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  g operating syst
60420 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74 68  em so that.** th
60430 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
60440 20 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f   will work on bo
60450 74 68 20 50 4f 53 49 58 20 61 6e 64 20 77 69 6e  th POSIX and win
60460 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2a  dows systems..**
60470 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
60480 66 69 6c 65 20 69 73 20 23 69 6e 63 6c 75 64 65  file is #include
60490 2d 65 64 20 62 79 20 73 71 6c 69 74 65 49 6e 74  -ed by sqliteInt
604a0 2e 68 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73  .h and thus ends
604b0 20 75 70 0a 2a 2a 20 62 65 69 6e 67 20 69 6e 63   up.** being inc
604c0 6c 75 64 65 64 20 62 79 20 65 76 65 72 79 20 73  luded by every s
604d0 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  ource file..**.*
604e0 2a 20 24 49 64 3a 20 6f 73 2e 68 2c 76 20 31 2e  * $Id: os.h,v 1.
604f0 31 30 37 20 32 30 30 39 2f 30 31 2f 31 34 20 32  107 2009/01/14 2
60500 33 3a 30 33 3a 34 31 20 64 72 68 20 45 78 70 20  3:03:41 drh Exp 
60510 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51  $.*/.#ifndef _SQ
60520 4c 49 54 45 5f 4f 53 5f 48 5f 0a 23 64 65 66 69  LITE_OS_H_.#defi
60530 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f  ne _SQLITE_OS_H_
60540 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f  ../*.** Figure o
60550 75 74 20 69 66 20 77 65 20 61 72 65 20 64 65 61  ut if we are dea
60560 6c 69 6e 67 20 77 69 74 68 20 55 6e 69 78 2c 20  ling with Unix, 
60570 57 69 6e 64 6f 77 73 2c 20 6f 72 20 73 6f 6d 65  Windows, or some
60580 20 6f 74 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74   other.** operat
60590 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 41 66 74  ing system.  Aft
605a0 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  er the following
605b0 20 62 6c 6f 63 6b 20 6f 66 20 70 72 65 70 72 6f   block of prepro
605c0 63 65 73 73 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20  cess macros,.** 
605d0 61 6c 6c 20 6f 66 20 53 51 4c 49 54 45 5f 4f 53  all of SQLITE_OS
605e0 5f 55 4e 49 58 2c 20 53 51 4c 49 54 45 5f 4f 53  _UNIX, SQLITE_OS
605f0 5f 57 49 4e 2c 20 53 51 4c 49 54 45 5f 4f 53 5f  _WIN, SQLITE_OS_
60600 4f 53 32 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f  OS2, and SQLITE_
60610 4f 53 5f 4f 54 48 45 52 20 0a 2a 2a 20 77 69 6c  OS_OTHER .** wil
60620 6c 20 64 65 66 69 6e 65 64 20 74 6f 20 65 69 74  l defined to eit
60630 68 65 72 20 31 20 6f 72 20 30 2e 20 20 4f 6e 65  her 1 or 0.  One
60640 20 6f 66 20 74 68 65 20 66 6f 75 72 20 77 69 6c   of the four wil
60650 6c 20 62 65 20 31 2e 20 20 54 68 65 20 6f 74 68  l be 1.  The oth
60660 65 72 20 0a 2a 2a 20 74 68 72 65 65 20 77 69 6c  er .** three wil
60670 6c 20 62 65 20 30 2e 0a 2a 2f 0a 23 69 66 20 64  l be 0..*/.#if d
60680 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53  efined(SQLITE_OS
60690 5f 4f 54 48 45 52 29 0a 23 20 69 66 20 53 51 4c  _OTHER).# if SQL
606a0 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a  ITE_OS_OTHER==1.
606b0 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45  #   undef SQLITE
606c0 5f 4f 53 5f 55 4e 49 58 0a 23 20 20 20 64 65 66  _OS_UNIX.#   def
606d0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e  ine SQLITE_OS_UN
606e0 49 58 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53  IX 0.#   undef S
606f0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20  QLITE_OS_WIN.#  
60700 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
60710 53 5f 57 49 4e 20 30 0a 23 20 20 20 75 6e 64 65  S_WIN 0.#   unde
60720 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a  f SQLITE_OS_OS2.
60730 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  #   define SQLIT
60740 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6c 73  E_OS_OS2 0.# els
60750 65 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49  e.#   undef SQLI
60760 54 45 5f 4f 53 5f 4f 54 48 45 52 0a 23 20 65 6e  TE_OS_OTHER.# en
60770 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 20 21  dif.#endif.#if !
60780 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
60790 53 5f 55 4e 49 58 29 20 26 26 20 21 64 65 66 69  S_UNIX) && !defi
607a0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54  ned(SQLITE_OS_OT
607b0 48 45 52 29 0a 23 20 64 65 66 69 6e 65 20 53 51  HER).# define SQ
607c0 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 30 0a  LITE_OS_OTHER 0.
607d0 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  # ifndef SQLITE_
607e0 4f 53 5f 57 49 4e 0a 23 20 20 20 69 66 20 64 65  OS_WIN.#   if de
607f0 66 69 6e 65 64 28 5f 57 49 4e 33 32 29 20 7c 7c  fined(_WIN32) ||
60800 20 64 65 66 69 6e 65 64 28 57 49 4e 33 32 29 20   defined(WIN32) 
60810 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47  || defined(__CYG
60820 57 49 4e 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  WIN__) || define
60830 64 28 5f 5f 4d 49 4e 47 57 33 32 5f 5f 29 20 7c  d(__MINGW32__) |
60840 7c 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c  | defined(__BORL
60850 41 4e 44 43 5f 5f 29 0a 23 20 20 20 20 20 64 65  ANDC__).#     de
60860 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
60870 49 4e 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e  IN 1.#     defin
60880 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  e SQLITE_OS_UNIX
60890 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20   0.#     define 
608a0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a  SQLITE_OS_OS2 0.
608b0 23 20 20 20 65 6c 69 66 20 64 65 66 69 6e 65 64  #   elif defined
608c0 28 5f 5f 45 4d 58 5f 5f 29 20 7c 7c 20 64 65 66  (__EMX__) || def
608d0 69 6e 65 64 28 5f 4f 53 32 29 20 7c 7c 20 64 65  ined(_OS2) || de
608e0 66 69 6e 65 64 28 4f 53 32 29 20 7c 7c 20 64 65  fined(OS2) || de
608f0 66 69 6e 65 64 28 5f 4f 53 32 5f 29 20 7c 7c 20  fined(_OS2_) || 
60900 64 65 66 69 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29  defined(__OS2__)
60910 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51  .#     define SQ
60920 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20  LITE_OS_WIN 0.# 
60930 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
60940 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20  E_OS_UNIX 0.#   
60950 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
60960 4f 53 5f 4f 53 32 20 31 0a 23 20 20 20 65 6c 73  OS_OS2 1.#   els
60970 65 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53  e.#     define S
60980 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23  QLITE_OS_WIN 0.#
60990 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
609a0 54 45 5f 4f 53 5f 55 4e 49 58 20 31 0a 23 20 20  TE_OS_UNIX 1.#  
609b0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
609c0 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 65 6e 64  _OS_OS2 0.#  end
609d0 69 66 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66  if.# else.#  def
609e0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e  ine SQLITE_OS_UN
609f0 49 58 20 30 0a 23 20 20 64 65 66 69 6e 65 20 53  IX 0.#  define S
60a00 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23  QLITE_OS_OS2 0.#
60a10 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23 20 69   endif.#else.# i
60a20 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f  fndef SQLITE_OS_
60a30 57 49 4e 0a 23 20 20 64 65 66 69 6e 65 20 53 51  WIN.#  define SQ
60a40 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20  LITE_OS_WIN 0.# 
60a50 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a  endif.#endif../*
60a60 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66  .** Determine if
60a70 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20   we are dealing 
60a80 77 69 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d  with WindowsCE -
60a90 20 77 68 69 63 68 20 68 61 73 20 61 20 6d 75 63   which has a muc
60aa0 68 0a 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49  h.** reduced API
60ab0 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
60ac0 28 5f 57 49 4e 33 32 5f 57 43 45 29 0a 23 20 64  (_WIN32_WCE).# d
60ad0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
60ae0 57 49 4e 43 45 20 31 0a 23 65 6c 73 65 0a 23 20  WINCE 1.#else.# 
60af0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
60b00 5f 57 49 4e 43 45 20 30 0a 23 65 6e 64 69 66 0a  _WINCE 0.#endif.
60b10 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74  ../*.** Define t
60b20 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20  he maximum size 
60b30 6f 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  of a temporary f
60b40 69 6c 65 6e 61 6d 65 0a 2a 2f 0a 23 69 66 20 53  ilename.*/.#if S
60b50 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 69  QLITE_OS_WIN.# i
60b60 6e 63 6c 75 64 65 20 3c 77 69 6e 64 6f 77 73 2e  nclude <windows.
60b70 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  h>.# define SQLI
60b80 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45  TE_TEMPNAME_SIZE
60b90 20 28 4d 41 58 5f 50 41 54 48 2b 35 30 29 0a 23   (MAX_PATH+50).#
60ba0 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  elif SQLITE_OS_O
60bb0 53 32 0a 23 20 69 66 20 28 5f 5f 47 4e 55 43 5f  S2.# if (__GNUC_
60bc0 5f 20 3e 20 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f  _ > 3 || __GNUC_
60bd0 5f 20 3d 3d 20 33 20 26 26 20 5f 5f 47 4e 55 43  _ == 3 && __GNUC
60be0 5f 4d 49 4e 4f 52 5f 5f 20 3e 3d 20 33 29 20 26  _MINOR__ >= 3) &
60bf0 26 20 64 65 66 69 6e 65 64 28 4f 53 32 5f 48 49  & defined(OS2_HI
60c00 47 48 5f 4d 45 4d 4f 52 59 29 0a 23 20 20 69 6e  GH_MEMORY).#  in
60c10 63 6c 75 64 65 20 3c 6f 73 32 73 61 66 65 2e 68  clude <os2safe.h
60c20 3e 20 2f 2a 20 68 61 73 20 74 6f 20 62 65 20 69  > /* has to be i
60c30 6e 63 6c 75 64 65 64 20 62 65 66 6f 72 65 20 6f  ncluded before o
60c40 73 32 2e 68 20 66 6f 72 20 6c 69 6e 6b 69 6e 67  s2.h for linking
60c50 20 74 6f 20 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e   to work */.# en
60c60 64 69 66 0a 23 20 64 65 66 69 6e 65 20 49 4e 43  dif.# define INC
60c70 4c 5f 44 4f 53 44 41 54 45 54 49 4d 45 0a 23 20  L_DOSDATETIME.# 
60c80 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 46  define INCL_DOSF
60c90 49 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20  ILEMGR.# define 
60ca0 49 4e 43 4c 5f 44 4f 53 45 52 52 4f 52 53 0a 23  INCL_DOSERRORS.#
60cb0 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53   define INCL_DOS
60cc0 4d 49 53 43 0a 23 20 64 65 66 69 6e 65 20 49 4e  MISC.# define IN
60cd0 43 4c 5f 44 4f 53 50 52 4f 43 45 53 53 0a 23 20  CL_DOSPROCESS.# 
60ce0 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d  define INCL_DOSM
60cf0 4f 44 55 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e  ODULEMGR.# defin
60d00 65 20 49 4e 43 4c 5f 44 4f 53 53 45 4d 41 50 48  e INCL_DOSSEMAPH
60d10 4f 52 45 53 0a 23 20 69 6e 63 6c 75 64 65 20 3c  ORES.# include <
60d20 6f 73 32 2e 68 3e 0a 23 20 69 6e 63 6c 75 64 65  os2.h>.# include
60d30 20 3c 75 63 6f 6e 76 2e 68 3e 0a 23 20 64 65 66   <uconv.h>.# def
60d40 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e  ine SQLITE_TEMPN
60d50 41 4d 45 5f 53 49 5a 45 20 28 43 43 48 4d 41 58  AME_SIZE (CCHMAX
60d60 50 41 54 48 43 4f 4d 50 29 0a 23 65 6c 73 65 0a  PATHCOMP).#else.
60d70 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
60d80 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 32 30  TEMPNAME_SIZE 20
60d90 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 66 20  0.#endif../* If 
60da0 74 68 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43  the SET_FULLSYNC
60db0 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 64 65   macro is not de
60dc0 66 69 6e 65 64 20 61 62 6f 76 65 2c 20 74 68 65  fined above, the
60dd0 6e 20 6d 61 6b 65 20 69 74 0a 2a 2a 20 61 20 6e  n make it.** a n
60de0 6f 2d 6f 70 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  o-op.*/.#ifndef 
60df0 53 45 54 5f 46 55 4c 4c 53 59 4e 43 0a 23 20 64  SET_FULLSYNC.# d
60e00 65 66 69 6e 65 20 53 45 54 5f 46 55 4c 4c 53 59  efine SET_FULLSY
60e10 4e 43 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a  NC(x,y).#endif..
60e20 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  /*.** The defaul
60e30 74 20 73 69 7a 65 20 6f 66 20 61 20 64 69 73 6b  t size of a disk
60e40 20 73 65 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64   sector.*/.#ifnd
60e50 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ef SQLITE_DEFAUL
60e60 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a 23 20  T_SECTOR_SIZE.# 
60e70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
60e80 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a  FAULT_SECTOR_SIZ
60e90 45 20 35 31 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a  E 512.#endif../*
60ea0 0a 2a 2a 20 54 65 6d 70 6f 72 61 72 79 20 66 69  .** Temporary fi
60eb0 6c 65 73 20 61 72 65 20 6e 61 6d 65 64 20 73 74  les are named st
60ec0 61 72 74 69 6e 67 20 77 69 74 68 20 74 68 69 73  arting with this
60ed0 20 70 72 65 66 69 78 20 66 6f 6c 6c 6f 77 65 64   prefix followed
60ee0 20 62 79 20 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a   by 16 random.**
60ef0 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 63 68   alphanumeric ch
60f00 61 72 61 63 74 65 72 73 2c 20 61 6e 64 20 6e 6f  aracters, and no
60f10 20 66 69 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2e   file extension.
60f20 20 54 68 65 79 20 61 72 65 20 73 74 6f 72 65 64   They are stored
60f30 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 53 27 73 20   in the.** OS's 
60f40 73 74 61 6e 64 61 72 64 20 74 65 6d 70 6f 72 61  standard tempora
60f50 72 79 20 66 69 6c 65 20 64 69 72 65 63 74 6f 72  ry file director
60f60 79 2c 20 61 6e 64 20 61 72 65 20 64 65 6c 65 74  y, and are delet
60f70 65 64 20 70 72 69 6f 72 20 74 6f 20 65 78 69 74  ed prior to exit
60f80 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 20 69  ..** If sqlite i
60f90 73 20 62 65 69 6e 67 20 65 6d 62 65 64 64 65 64  s being embedded
60fa0 20 69 6e 20 61 6e 6f 74 68 65 72 20 70 72 6f 67   in another prog
60fb0 72 61 6d 2c 20 79 6f 75 20 6d 61 79 20 77 69 73  ram, you may wis
60fc0 68 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a  h to change the.
60fd0 2a 2a 20 70 72 65 66 69 78 20 74 6f 20 72 65 66  ** prefix to ref
60fe0 6c 65 63 74 20 79 6f 75 72 20 70 72 6f 67 72 61  lect your progra
60ff0 6d 27 73 20 6e 61 6d 65 2c 20 73 6f 20 74 68 61  m's name, so tha
61000 74 20 69 66 20 79 6f 75 72 20 70 72 6f 67 72 61  t if your progra
61010 6d 20 65 78 69 74 73 0a 2a 2a 20 70 72 65 6d 61  m exits.** prema
61020 74 75 72 65 6c 79 2c 20 6f 6c 64 20 74 65 6d 70  turely, old temp
61030 6f 72 61 72 79 20 66 69 6c 65 73 20 63 61 6e 20  orary files can 
61040 62 65 20 65 61 73 69 6c 79 20 69 64 65 6e 74 69  be easily identi
61050 66 69 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62  fied. This can b
61060 65 20 64 6f 6e 65 0a 2a 2a 20 75 73 69 6e 67 20  e done.** using 
61070 2d 44 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49  -DSQLITE_TEMP_FI
61080 4c 45 5f 50 52 45 46 49 58 3d 6d 79 70 72 65 66  LE_PREFIX=mypref
61090 69 78 5f 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69  ix_ on the compi
610a0 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65  ler command line
610b0 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 36 2d 31 30 2d  ..**.** 2006-10-
610c0 33 31 3a 20 20 54 68 65 20 64 65 66 61 75 6c 74  31:  The default
610d0 20 70 72 65 66 69 78 20 75 73 65 64 20 74 6f 20   prefix used to 
610e0 62 65 20 22 73 71 6c 69 74 65 5f 22 2e 20 20 42  be "sqlite_".  B
610f0 75 74 20 74 68 65 6e 0a 2a 2a 20 4d 63 61 66 65  ut then.** Mcafe
61100 65 20 73 74 61 72 74 65 64 20 75 73 69 6e 67 20  e started using 
61110 53 51 4c 69 74 65 20 69 6e 20 74 68 65 69 72 20  SQLite in their 
61120 61 6e 74 69 2d 76 69 72 75 73 20 70 72 6f 64 75  anti-virus produ
61130 63 74 20 61 6e 64 20 69 74 0a 2a 2a 20 73 74 61  ct and it.** sta
61140 72 74 65 64 20 70 75 74 74 69 6e 67 20 66 69 6c  rted putting fil
61150 65 73 20 77 69 74 68 20 74 68 65 20 22 73 71 6c  es with the "sql
61160 69 74 65 22 20 6e 61 6d 65 20 69 6e 20 74 68 65  ite" name in the
61170 20 63 3a 2f 74 65 6d 70 20 66 6f 6c 64 65 72 2e   c:/temp folder.
61180 0a 2a 2a 20 54 68 69 73 20 61 6e 6e 6f 79 65 64  .** This annoyed
61190 20 6d 61 6e 79 20 77 69 6e 64 6f 77 73 20 75 73   many windows us
611a0 65 72 73 2e 20 20 54 68 6f 73 65 20 75 73 65 72  ers.  Those user
611b0 73 20 77 6f 75 6c 64 20 74 68 65 6e 20 64 6f 20  s would then do 
611c0 61 20 0a 2a 2a 20 47 6f 6f 67 6c 65 20 73 65 61  a .** Google sea
611d0 72 63 68 20 66 6f 72 20 22 73 71 6c 69 74 65 22  rch for "sqlite"
611e0 2c 20 66 69 6e 64 20 74 68 65 20 74 65 6c 65 70  , find the telep
611f0 68 6f 6e 65 20 6e 75 6d 62 65 72 73 20 6f 66 20  hone numbers of 
61200 74 68 65 0a 2a 2a 20 64 65 76 65 6c 6f 70 65 72  the.** developer
61210 73 20 61 6e 64 20 63 61 6c 6c 20 74 6f 20 77 61  s and call to wa
61220 6b 65 20 74 68 65 6d 20 75 70 20 61 74 20 6e 69  ke them up at ni
61230 67 68 74 20 61 6e 64 20 63 6f 6d 70 6c 61 69 6e  ght and complain
61240 2e 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 65  ..** For this re
61250 61 73 6f 6e 2c 20 74 68 65 20 64 65 66 61 75 6c  ason, the defaul
61260 74 20 6e 61 6d 65 20 70 72 65 66 69 78 20 69 73  t name prefix is
61270 20 63 68 61 6e 67 65 64 20 74 6f 20 62 65 20 22   changed to be "
61280 73 71 6c 69 74 65 22 20 0a 2a 2a 20 73 70 65 6c  sqlite" .** spel
61290 6c 65 64 20 62 61 63 6b 77 61 72 64 73 2e 20 20  led backwards.  
612a0 53 6f 20 74 68 65 20 74 65 6d 70 20 66 69 6c 65  So the temp file
612b0 73 20 61 72 65 20 73 74 69 6c 6c 20 69 64 65 6e  s are still iden
612c0 74 69 66 69 65 64 2c 20 62 75 74 0a 2a 2a 20 61  tified, but.** a
612d0 6e 79 62 6f 64 79 20 73 6d 61 72 74 20 65 6e 6f  nybody smart eno
612e0 75 67 68 20 74 6f 20 66 69 67 75 72 65 20 6f 75  ugh to figure ou
612f0 74 20 74 68 65 20 63 6f 64 65 20 69 73 20 61 6c  t the code is al
61300 73 6f 20 6c 69 6b 65 6c 79 20 73 6d 61 72 74 0a  so likely smart.
61310 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f  ** enough to kno
61320 77 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 74  w that calling t
61330 68 65 20 64 65 76 65 6c 6f 70 65 72 20 77 69 6c  he developer wil
61340 6c 20 6e 6f 74 20 68 65 6c 70 20 67 65 74 20 72  l not help get r
61350 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c  id.** of the fil
61360 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
61370 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50  LITE_TEMP_FILE_P
61380 52 45 46 49 58 0a 23 20 64 65 66 69 6e 65 20 53  REFIX.# define S
61390 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f  QLITE_TEMP_FILE_
613a0 50 52 45 46 49 58 20 22 65 74 69 6c 71 73 5f 22  PREFIX "etilqs_"
613b0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
613c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  he following val
613d0 75 65 73 20 6d 61 79 20 62 65 20 70 61 73 73 65  ues may be passe
613e0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
613f0 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73  argument to.** s
61400 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20  qlite3OsLock(). 
61410 54 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b  The various lock
61420 73 20 65 78 68 69 62 69 74 20 74 68 65 20 66 6f  s exhibit the fo
61430 6c 6c 6f 77 69 6e 67 20 73 65 6d 61 6e 74 69 63  llowing semantic
61440 73 3a 0a 2a 2a 0a 2a 2a 20 53 48 41 52 45 44 3a  s:.**.** SHARED:
61450 20 20 20 20 41 6e 79 20 6e 75 6d 62 65 72 20 6f      Any number o
61460 66 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20  f processes may 
61470 68 6f 6c 64 20 61 20 53 48 41 52 45 44 20 6c 6f  hold a SHARED lo
61480 63 6b 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c  ck simultaneousl
61490 79 2e 0a 2a 2a 20 52 45 53 45 52 56 45 44 3a 20  y..** RESERVED: 
614a0 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73   A single proces
614b0 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 52 45 53  s may hold a RES
614c0 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 61 20  ERVED lock on a 
614d0 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20  file at.**      
614e0 20 20 20 20 20 20 61 6e 79 20 74 69 6d 65 2e 20        any time. 
614f0 4f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20  Other processes 
61500 6d 61 79 20 68 6f 6c 64 20 61 6e 64 20 6f 62 74  may hold and obt
61510 61 69 6e 20 6e 65 77 20 53 48 41 52 45 44 20 6c  ain new SHARED l
61520 6f 63 6b 73 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47  ocks..** PENDING
61530 3a 20 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f  :   A single pro
61540 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20  cess may hold a 
61550 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20  PENDING lock on 
61560 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20  a file at.**    
61570 20 20 20 20 20 20 20 20 61 6e 79 20 6f 6e 65 20          any one 
61580 74 69 6d 65 2e 20 45 78 69 73 74 69 6e 67 20 53  time. Existing S
61590 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20  HARED locks may 
615a0 70 65 72 73 69 73 74 2c 20 62 75 74 20 6e 6f 20  persist, but no 
615b0 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  new.**          
615c0 20 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d    SHARED locks m
615d0 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62  ay be obtained b
615e0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  y other processe
615f0 73 2e 0a 2a 2a 20 45 58 43 4c 55 53 49 56 45 3a  s..** EXCLUSIVE:
61600 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   An EXCLUSIVE lo
61610 63 6b 20 70 72 65 63 6c 75 64 65 73 20 61 6c 6c  ck precludes all
61620 20 6f 74 68 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a   other locks..**
61630 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  .** PENDING_LOCK
61640 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 61 73 73   may not be pass
61650 65 64 20 64 69 72 65 63 74 6c 79 20 74 6f 20 73  ed directly to s
61660 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20  qlite3OsLock(). 
61670 49 6e 73 74 65 61 64 2c 20 61 0a 2a 2a 20 70 72  Instead, a.** pr
61680 6f 63 65 73 73 20 74 68 61 74 20 72 65 71 75 65  ocess that reque
61690 73 74 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45  sts an EXCLUSIVE
616a0 20 6c 6f 63 6b 20 6d 61 79 20 61 63 74 75 61 6c   lock may actual
616b0 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44  ly obtain a PEND
616c0 49 4e 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69  ING.** lock. Thi
616d0 73 20 63 61 6e 20 62 65 20 75 70 67 72 61 64 65  s can be upgrade
616e0 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56  d to an EXCLUSIV
616f0 45 20 6c 6f 63 6b 20 62 79 20 61 20 73 75 62 73  E lock by a subs
61700 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a  equent call to.*
61710 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  * sqlite3OsLock(
61720 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4f  )..*/.#define NO
61730 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 30 0a  _LOCK         0.
61740 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 4c  #define SHARED_L
61750 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 69 6e  OCK     1.#defin
61760 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  e RESERVED_LOCK 
61770 20 20 32 0a 23 64 65 66 69 6e 65 20 50 45 4e 44    2.#define PEND
61780 49 4e 47 5f 4c 4f 43 4b 20 20 20 20 33 0a 23 64  ING_LOCK    3.#d
61790 65 66 69 6e 65 20 45 58 43 4c 55 53 49 56 45 5f  efine EXCLUSIVE_
617a0 4c 4f 43 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46  LOCK  4../*.** F
617b0 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65  ile Locking Note
617c0 73 3a 20 20 28 4d 6f 73 74 6c 79 20 61 62 6f 75  s:  (Mostly abou
617d0 74 20 77 69 6e 64 6f 77 73 20 62 75 74 20 61 6c  t windows but al
617e0 73 6f 20 73 6f 6d 65 20 69 6e 66 6f 20 66 6f 72  so some info for
617f0 20 55 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20   Unix).**.** We 
61800 63 61 6e 6e 6f 74 20 75 73 65 20 4c 6f 63 6b 46  cannot use LockF
61810 69 6c 65 45 78 28 29 20 6f 72 20 55 6e 6c 6f 63  ileEx() or Unloc
61820 6b 46 69 6c 65 45 78 28 29 20 6f 6e 20 57 69 6e  kFileEx() on Win
61830 39 35 2f 39 38 2f 4d 45 20 62 65 63 61 75 73 65  95/98/ME because
61840 0a 2a 2a 20 74 68 6f 73 65 20 66 75 6e 63 74 69  .** those functi
61850 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69  ons are not avai
61860 6c 61 62 6c 65 2e 20 20 53 6f 20 77 65 20 75 73  lable.  So we us
61870 65 20 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c 65 28  e only LockFile(
61880 29 20 61 6e 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46  ) and.** UnlockF
61890 69 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63  ile()..**.** Loc
618a0 6b 46 69 6c 65 28 29 20 70 72 65 76 65 6e 74 73  kFile() prevents
618b0 20 6e 6f 74 20 6a 75 73 74 20 77 72 69 74 69 6e   not just writin
618c0 67 20 62 75 74 20 61 6c 73 6f 20 72 65 61 64 69  g but also readi
618d0 6e 67 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63  ng by other proc
618e0 65 73 73 65 73 2e 0a 2a 2a 20 41 20 53 48 41 52  esses..** A SHAR
618f0 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69  ED_LOCK is obtai
61900 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61  ned by locking a
61910 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 6c 79   single randomly
61920 2d 63 68 6f 73 65 6e 20 0a 2a 2a 20 62 79 74 65  -chosen .** byte
61930 20 6f 75 74 20 6f 66 20 61 20 73 70 65 63 69 66   out of a specif
61940 69 63 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65  ic range of byte
61950 73 2e 20 54 68 65 20 6c 6f 63 6b 20 62 79 74 65  s. The lock byte
61960 20 69 73 20 6f 62 74 61 69 6e 65 64 20 61 74 20   is obtained at 
61970 0a 2a 2a 20 72 61 6e 64 6f 6d 20 73 6f 20 74 77  .** random so tw
61980 6f 20 73 65 70 61 72 61 74 65 20 72 65 61 64 65  o separate reade
61990 72 73 20 63 61 6e 20 70 72 6f 62 61 62 6c 79 20  rs can probably 
619a0 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 20  access the file 
619b0 61 74 20 74 68 65 20 0a 2a 2a 20 73 61 6d 65 20  at the .** same 
619c0 74 69 6d 65 2c 20 75 6e 6c 65 73 73 20 74 68 65  time, unless the
619d0 79 20 61 72 65 20 75 6e 6c 75 63 6b 79 20 61 6e  y are unlucky an
619e0 64 20 63 68 6f 6f 73 65 20 74 68 65 20 73 61 6d  d choose the sam
619f0 65 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20  e lock byte..** 
61a00 41 6e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  An EXCLUSIVE_LOC
61a10 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79  K is obtained by
61a20 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 62 79 74   locking all byt
61a30 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e  es in the range.
61a40 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e  .** There can on
61a50 6c 79 20 62 65 20 6f 6e 65 20 77 72 69 74 65 72  ly be one writer
61a60 2e 20 20 41 20 52 45 53 45 52 56 45 44 5f 4c 4f  .  A RESERVED_LO
61a70 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62  CK is obtained b
61a80 79 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 61 20 73  y locking.** a s
61a90 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 74 68  ingle byte of th
61aa0 65 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 64  e file that is d
61ab0 65 73 69 67 6e 61 74 65 64 20 61 73 20 74 68 65  esignated as the
61ac0 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 62   reserved lock b
61ad0 79 74 65 2e 0a 2a 2a 20 41 20 50 45 4e 44 49 4e  yte..** A PENDIN
61ae0 47 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e  G_LOCK is obtain
61af0 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20  ed by locking a 
61b00 64 65 73 69 67 6e 61 74 65 64 20 62 79 74 65 20  designated byte 
61b10 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a  different from.*
61b20 2a 20 74 68 65 20 52 45 53 45 52 56 45 44 5f 4c  * the RESERVED_L
61b30 4f 43 4b 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20  OCK byte..**.** 
61b40 4f 6e 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73  On WinNT/2K/XP s
61b50 79 73 74 65 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65  ystems, LockFile
61b60 45 78 28 29 20 61 6e 64 20 55 6e 6c 6f 63 6b 46  Ex() and UnlockF
61b70 69 6c 65 45 78 28 29 20 61 72 65 20 61 76 61 69  ileEx() are avai
61b80 6c 61 62 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20  lable,.** which 
61b90 6d 65 61 6e 73 20 77 65 20 63 61 6e 20 75 73 65  means we can use
61ba0 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c   reader/writer l
61bb0 6f 63 6b 73 2e 20 20 57 68 65 6e 20 72 65 61 64  ocks.  When read
61bc0 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 0a  er/writer locks.
61bd0 2a 2a 20 61 72 65 20 75 73 65 64 2c 20 74 68 65  ** are used, the
61be0 20 6c 6f 63 6b 20 69 73 20 70 6c 61 63 65 64 20   lock is placed 
61bf0 6f 6e 20 74 68 65 20 73 61 6d 65 20 72 61 6e 67  on the same rang
61c00 65 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20  e of bytes that 
61c10 69 73 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 70  is used.** for p
61c20 72 6f 62 61 62 69 6c 69 73 74 69 63 20 6c 6f 63  robabilistic loc
61c30 6b 69 6e 67 20 69 6e 20 57 69 6e 39 35 2f 39 38  king in Win95/98
61c40 2f 4d 45 2e 20 20 48 65 6e 63 65 2c 20 74 68 65  /ME.  Hence, the
61c50 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 0a   locking scheme.
61c60 2a 2a 20 77 69 6c 6c 20 73 75 70 70 6f 72 74 20  ** will support 
61c70 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 39  two or more Win9
61c80 35 20 72 65 61 64 65 72 73 20 6f 72 20 74 77 6f  5 readers or two
61c90 20 6f 72 20 6d 6f 72 65 20 57 69 6e 4e 54 20 72   or more WinNT r
61ca0 65 61 64 65 72 73 2e 0a 2a 2a 20 42 75 74 20 61  eaders..** But a
61cb0 20 73 69 6e 67 6c 65 20 57 69 6e 39 35 20 72 65   single Win95 re
61cc0 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f  ader will lock o
61cd0 75 74 20 61 6c 6c 20 57 69 6e 4e 54 20 72 65 61  ut all WinNT rea
61ce0 64 65 72 73 20 61 6e 64 20 61 20 73 69 6e 67 6c  ders and a singl
61cf0 65 0a 2a 2a 20 57 69 6e 4e 54 20 72 65 61 64 65  e.** WinNT reade
61d00 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20  r will lock out 
61d10 61 6c 6c 20 6f 74 68 65 72 20 57 69 6e 39 35 20  all other Win95 
61d20 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54  readers..**.** T
61d30 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 23 64 65  he following #de
61d40 66 69 6e 65 73 20 73 70 65 63 69 66 79 20 74 68  fines specify th
61d50 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73  e range of bytes
61d60 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e   used for lockin
61d70 67 2e 0a 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a  g..** SHARED_SIZ
61d80 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  E is the number 
61d90 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c 61 62  of bytes availab
61da0 6c 65 20 69 6e 20 74 68 65 20 70 6f 6f 6c 20 66  le in the pool f
61db0 72 6f 6d 20 77 68 69 63 68 0a 2a 2a 20 61 20 72  rom which.** a r
61dc0 61 6e 64 6f 6d 20 62 79 74 65 20 69 73 20 73 65  andom byte is se
61dd0 6c 65 63 74 65 64 20 66 6f 72 20 61 20 73 68 61  lected for a sha
61de0 72 65 64 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70  red lock.  The p
61df0 6f 6f 6c 20 6f 66 20 62 79 74 65 73 20 66 6f 72  ool of bytes for
61e00 0a 2a 2a 20 73 68 61 72 65 64 20 6c 6f 63 6b 73  .** shared locks
61e10 20 62 65 67 69 6e 73 20 61 74 20 53 48 41 52 45   begins at SHARE
61e20 44 5f 46 49 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20  D_FIRST. .**.** 
61e30 54 68 65 73 65 20 23 64 65 66 69 6e 65 73 20 61  These #defines a
61e40 72 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  re available in 
61e50 73 71 6c 69 74 65 5f 61 75 78 2e 68 20 73 6f 20  sqlite_aux.h so 
61e60 74 68 61 74 20 61 64 61 70 74 6f 72 73 20 66 6f  that adaptors fo
61e70 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6e 67 20  r.** connecting 
61e80 53 51 4c 69 74 65 20 74 6f 20 6f 74 68 65 72 20  SQLite to other 
61e90 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
61ea0 73 20 63 61 6e 20 75 73 65 20 74 68 65 20 73 61  s can use the sa
61eb0 6d 65 20 62 79 74 65 0a 2a 2a 20 72 61 6e 67 65  me byte.** range
61ec0 73 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20  s for locking.  
61ed0 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c 20 74  In particular, t
61ee0 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20  he same locking 
61ef0 73 74 72 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20  strategy and.** 
61f00 62 79 74 65 20 72 61 6e 67 65 73 20 61 72 65 20  byte ranges are 
61f10 75 73 65 64 20 66 6f 72 20 55 6e 69 78 2e 20 20  used for Unix.  
61f20 54 68 69 73 20 6c 65 61 76 65 73 20 6f 70 65 6e  This leaves open
61f30 20 74 68 65 20 70 6f 73 73 69 62 6c 69 74 79 20   the possiblity 
61f40 6f 66 20 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69  of having.** cli
61f50 65 6e 74 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77  ents on win95, w
61f60 69 6e 4e 54 2c 20 61 6e 64 20 75 6e 69 78 20 61  inNT, and unix a
61f70 6c 6c 20 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68  ll talking to th
61f80 65 20 73 61 6d 65 20 73 68 61 72 65 64 20 66 69  e same shared fi
61f90 6c 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f  le.** and all lo
61fa0 63 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e  cking correctly.
61fb0 20 20 54 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64    To do so would
61fc0 20 72 65 71 75 69 72 65 20 74 68 61 74 20 73 61   require that sa
61fd0 6d 62 61 20 28 6f 72 20 77 68 61 74 65 76 65 72  mba (or whatever
61fe0 0a 2a 2a 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e  .** tool is bein
61ff0 67 20 75 73 65 64 20 66 6f 72 20 66 69 6c 65 20  g used for file 
62000 73 68 61 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65  sharing) impleme
62010 6e 74 73 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63  nts locks correc
62020 74 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77  tly between.** w
62030 69 6e 64 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e  indows and unix.
62040 20 20 49 27 6d 20 67 75 65 73 73 69 6e 67 20 74    I'm guessing t
62050 68 61 74 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79  hat isn't likely
62060 20 74 6f 20 68 61 70 70 65 6e 2c 20 62 75 74 20   to happen, but 
62070 62 79 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  by.** using the 
62080 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e  same locking ran
62090 67 65 20 77 65 20 61 72 65 20 61 74 20 6c 65 61  ge we are at lea
620a0 73 74 20 6f 70 65 6e 20 74 6f 20 74 68 65 20 70  st open to the p
620b0 6f 73 73 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a  ossibility..**.*
620c0 2a 20 4c 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e  * Locking in win
620d0 64 6f 77 73 20 69 73 20 6d 61 6e 64 69 74 6f 72  dows is manditor
620e0 79 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61  y.  For this rea
620f0 73 6f 6e 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73  son, we cannot s
62100 74 6f 72 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64  tore.** actual d
62110 61 74 61 20 69 6e 20 74 68 65 20 62 79 74 65 73  ata in the bytes
62120 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e   used for lockin
62130 67 2e 20 20 54 68 65 20 70 61 67 65 72 20 6e 65  g.  The pager ne
62140 76 65 72 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a  ver allocates.**
62150 20 74 68 65 20 70 61 67 65 73 20 69 6e 76 6f 6c   the pages invol
62160 76 65 64 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74  ved in locking t
62170 68 65 72 65 66 6f 72 65 2e 20 20 53 48 41 52 45  herefore.  SHARE
62180 44 5f 53 49 5a 45 20 69 73 20 73 65 6c 65 63 74  D_SIZE is select
62190 65 64 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c  ed so.** that al
621a0 6c 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74  l locks will fit
621b0 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67   on a single pag
621c0 65 20 65 76 65 6e 20 61 74 20 74 68 65 20 6d 69  e even at the mi
621d0 6e 69 6d 75 6d 20 70 61 67 65 20 73 69 7a 65 2e  nimum page size.
621e0 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  .** PENDING_BYTE
621f0 20 64 65 66 69 6e 65 73 20 74 68 65 20 62 65 67   defines the beg
62200 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f  inning of the lo
62210 63 6b 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74  cks.  By default
62220 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a   PENDING_BYTE.**
62230 20 69 73 20 73 65 74 20 68 69 67 68 20 73 6f 20   is set high so 
62240 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 68 61  that we don't ha
62250 76 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  ve to allocate a
62260 6e 20 75 6e 75 73 65 64 20 70 61 67 65 20 65 78  n unused page ex
62270 63 65 70 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79  cept.** for very
62280 20 6c 61 72 67 65 20 64 61 74 61 62 61 73 65 73   large databases
62290 2e 20 20 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c  .  But one shoul
622a0 64 20 74 65 73 74 20 74 68 65 20 70 61 67 65 20  d test the page 
622b0 73 6b 69 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a  skipping logic .
622c0 2a 2a 20 62 79 20 73 65 74 74 69 6e 67 20 50 45  ** by setting PE
622d0 4e 44 49 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61  NDING_BYTE low a
622e0 6e 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 65  nd running the e
622f0 6e 74 69 72 65 20 72 65 67 72 65 73 73 69 6f 6e  ntire regression
62300 20 73 75 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68   suite..**.** Ch
62310 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65  anging the value
62320 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45   of PENDING_BYTE
62330 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 73 75   results in a su
62340 62 74 6c 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c  btly incompatibl
62350 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74  e.** file format
62360 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20  .  Depending on 
62370 68 6f 77 20 69 74 20 69 73 20 63 68 61 6e 67 65  how it is change
62380 64 2c 20 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74  d, you might not
62390 20 6e 6f 74 69 63 65 0a 2a 2a 20 74 68 65 20 69   notice.** the i
623a0 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 72  ncompatibility r
623b0 69 67 68 74 20 61 77 61 79 2c 20 65 76 65 6e 20  ight away, even 
623c0 72 75 6e 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72  running a full r
623d0 65 67 72 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a  egression test..
623e0 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c  ** The default l
623f0 6f 63 61 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49  ocation of PENDI
62400 4e 47 5f 42 59 54 45 20 69 73 20 74 68 65 20 66  NG_BYTE is the f
62410 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74  irst byte past t
62420 68 65 0a 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61  he.** 1GB bounda
62430 72 79 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65  ry..**.*/.#ifnde
62440 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 23 64  f SQLITE_TEST.#d
62450 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59  efine PENDING_BY
62460 54 45 20 20 20 20 20 20 30 78 34 30 30 30 30 30  TE      0x400000
62470 30 30 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74  00  /* First byt
62480 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62  e past the 1GB b
62490 6f 75 6e 64 61 72 79 20 2a 2f 0a 23 65 6c 73 65  oundary */.#else
624a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65  .SQLITE_API exte
624b0 72 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  rn unsigned int 
624c0 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f  sqlite3_pending_
624d0 62 79 74 65 3b 0a 23 64 65 66 69 6e 65 20 50 45  byte;.#define PE
624e0 4e 44 49 4e 47 5f 42 59 54 45 20 73 71 6c 69 74  NDING_BYTE sqlit
624f0 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65 0a  e3_pending_byte.
62500 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20  #endif..#define 
62510 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 20 20  RESERVED_BYTE   
62520 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b    (PENDING_BYTE+
62530 31 29 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45  1).#define SHARE
62540 44 5f 46 49 52 53 54 20 20 20 20 20 20 28 50 45  D_FIRST      (PE
62550 4e 44 49 4e 47 5f 42 59 54 45 2b 32 29 0a 23 64  NDING_BYTE+2).#d
62560 65 66 69 6e 65 20 53 48 41 52 45 44 5f 53 49 5a  efine SHARED_SIZ
62570 45 20 20 20 20 20 20 20 35 31 30 0a 0a 2f 2a 20  E       510../* 
62580 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f  .** Functions fo
62590 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69  r accessing sqli
625a0 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
625b0 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
625c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
625d0 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  sClose(sqlite3_f
625e0 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ile*);.SQLITE_PR
625f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
62600 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f  3OsRead(sqlite3_
62610 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e  file*, void*, in
62620 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65  t amt, i64 offse
62630 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
62640 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
62650 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69  Write(sqlite3_fi
62660 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  le*, const void*
62670 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f  , int amt, i64 o
62680 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50  ffset);.SQLITE_P
62690 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
626a0 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c  e3OsTruncate(sql
626b0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20  ite3_file*, i64 
626c0 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  size);.SQLITE_PR
626d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
626e0 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  3OsSync(sqlite3_
626f0 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  file*, int);.SQL
62700 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
62710 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
62720 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  e(sqlite3_file*,
62730 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51   i64 *pSize);.SQ
62740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
62750 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73   sqlite3OsLock(s
62760 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
62770 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
62780 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
62790 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
627a0 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ile*, int);.SQLI
627b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
627c0 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73  qlite3OsCheckRes
627d0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
627e0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
627f0 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54  *pResOut);.SQLIT
62800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
62810 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
62820 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ol(sqlite3_file*
62830 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65  ,int,void*);.#de
62840 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  fine SQLITE_FCNT
62850 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30  L_DB_UNCHANGED 0
62860 78 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45  xca093fa0.SQLITE
62870 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
62880 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65  ite3OsSectorSize
62890 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
628a0 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  d);.SQLITE_PRIVA
628b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
628c0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
628d0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69  stics(sqlite3_fi
628e0 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a  le *id);../* .**
628f0 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61   Functions for a
62900 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33  ccessing sqlite3
62910 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f  _vfs methods .*/
62920 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
62930 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  int sqlite3OsOpe
62940 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  n(sqlite3_vfs *,
62950 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73   const char *, s
62960 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
62970 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54  t, int *);.SQLIT
62980 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
62990 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71  lite3OsDelete(sq
629a0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
629b0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b  st char *, int);
629c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
629d0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63  int sqlite3OsAcc
629e0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ess(sqlite3_vfs 
629f0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
62a00 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f   int, int *pResO
62a10 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ut);.SQLITE_PRIV
62a20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
62a30 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71  sFullPathname(sq
62a40 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
62a50 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  st char *, int, 
62a60 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66  char *);.#ifndef
62a70 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
62a80 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49  D_EXTENSION.SQLI
62a90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
62aa0 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e  *sqlite3OsDlOpen
62ab0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
62ac0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53  const char *);.S
62ad0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
62ae0 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72  id sqlite3OsDlEr
62af0 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
62b00 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b  *, int, char *);
62b10 0a 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f  .void (*sqlite3O
62b20 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76  sDlSym(sqlite3_v
62b30 66 73 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f  fs *, void *, co
62b40 6e 73 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69  nst char *))(voi
62b50 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  d);.SQLITE_PRIVA
62b60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f  TE void sqlite3O
62b70 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  sDlClose(sqlite3
62b80 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b  _vfs *, void *);
62b90 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
62ba0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
62bb0 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f  NSION */.SQLITE_
62bc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
62bd0 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28  te3OsRandomness(
62be0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69  sqlite3_vfs *, i
62bf0 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nt, char *);.SQL
62c00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
62c10 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73  sqlite3OsSleep(s
62c20 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e  qlite3_vfs *, in
62c30 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
62c40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
62c50 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69  CurrentTime(sqli
62c60 74 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c  te3_vfs *, doubl
62c70 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  e*);../*.** Conv
62c80 65 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e  enience function
62c90 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e  s for opening an
62ca0 64 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20  d closing files 
62cb0 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65  using .** sqlite
62cc0 33 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62  3_malloc() to ob
62cd0 74 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74  tain space for t
62ce0 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73  he file-handle s
62cf0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c  tructure..*/.SQL
62d00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
62d10 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c  sqlite3OsOpenMal
62d20 6c 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20  loc(sqlite3_vfs 
62d30 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
62d40 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a   sqlite3_file **
62d50 2c 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c  , int,int*);.SQL
62d60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
62d70 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72  sqlite3OsCloseFr
62d80 65 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ee(sqlite3_file 
62d90 2a 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f  *);..#endif /* _
62da0 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a  SQLITE_OS_H_ */.
62db0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
62dc0 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a   End of os.h ***
62dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
62de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
62df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
62e00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
62e10 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
62e20 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
62e30 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
62e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
62e50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
62e60 20 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68   Include mutex.h
62e70 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
62e80 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
62e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
62ea0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
62eb0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65   Begin file mute
62ec0 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.h ************
62ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
62ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
62ef0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
62f00 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 28.**.** The 
62f10 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
62f20 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
62f30 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
62f40 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
62f50 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
62f60 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
62f70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
62f80 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
62f90 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
62fa0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
62fb0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
62fc0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
62fd0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
62fe0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
62ff0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
63000 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
63010 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
63020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
63030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
63040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
63050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
63060 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
63070 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
63080 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20  e common header 
63090 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d  for all mutex im
630a0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  plementations..*
630b0 2a 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e  * The sqliteInt.
630c0 68 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64  h header #includ
630d0 65 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20  es this file so 
630e0 74 68 61 74 20 69 74 20 69 73 20 61 76 61 69 6c  that it is avail
630f0 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73  able.** to all s
63100 6f 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65  ource files.  We
63110 20 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e   break it out in
63120 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65   an effort to ke
63130 65 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62  ep the code.** b
63140 65 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e  etter organized.
63150 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f  .**.** NOTE:  so
63160 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c  urce files shoul
63170 64 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65  d *not* #include
63180 20 74 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   this header fil
63190 65 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53  e directly..** S
631a0 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75  ource files shou
631b0 6c 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20  ld #include the 
631c0 73 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65  sqliteInt.h file
631d0 20 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69   and let that fi
631e0 6c 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68  le.** include th
631f0 69 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c  is one indirectl
63200 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75  y..**.** $Id: mu
63210 74 65 78 2e 68 2c 76 20 31 2e 39 20 32 30 30 38  tex.h,v 1.9 2008
63220 2f 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 20  /10/07 15:25:48 
63230 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f  drh Exp $.*/.../
63240 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20  *.** Figure out 
63250 77 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20  what version of 
63260 74 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e  the code to use.
63270 20 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 72    The choices ar
63280 65 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45  e.**.**   SQLITE
63290 5f 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20  _MUTEX_OMIT     
632a0 20 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67      No mutex log
632b0 69 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74  ic.  Not even st
632c0 75 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20  ubs.  The.**    
632d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
632e0 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 73           mutexes
632f0 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61   implemention ca
63300 6e 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64  nnot be overridd
63310 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  en.**           
63320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63330 20 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e    at start-time.
63340 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  .**.**   SQLITE_
63350 4d 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20  MUTEX_NOOP      
63360 20 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68     For single-th
63370 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69  readed applicati
63380 6f 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20  ons.  No.**     
63390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
633a0 20 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65          mutual e
633b0 78 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76  xclusion is prov
633c0 69 64 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a  ided.  But this.
633d0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
633e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
633f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61  mplementation ca
63400 6e 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20  n be overridden 
63410 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  at.**           
63420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63430 20 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a    start-time..**
63440 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  .**   SQLITE_MUT
63450 45 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 20  EX_PTHREADS     
63460 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64  For multi-thread
63470 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ed applications 
63480 6f 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20  on Unix..**.**  
63490 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
634a0 32 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d  2          For m
634b0 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70  ulti-threaded ap
634c0 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69  plications on Wi
634d0 6e 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c  n32..**.**   SQL
634e0 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20  ITE_MUTEX_OS2   
634f0 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69         For multi
63500 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63  -threaded applic
63510 61 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a  ations on OS/2..
63520 2a 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54  */.#if !SQLITE_T
63530 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69  HREADSAFE.# defi
63540 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
63550 4f 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20  OMIT.#endif.#if 
63560 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
63570 45 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  E && !defined(SQ
63580 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29  LITE_MUTEX_NOOP)
63590 0a 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53  .#  if SQLITE_OS
635a0 5f 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e  _UNIX.#    defin
635b0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50  e SQLITE_MUTEX_P
635c0 54 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20  THREADS.#  elif 
635d0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20  SQLITE_OS_WIN.# 
635e0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
635f0 5f 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c  _MUTEX_W32.#  el
63600 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32  if SQLITE_OS_OS2
63610 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  .#    define SQL
63620 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20  ITE_MUTEX_OS2.# 
63630 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e   else.#    defin
63640 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e  e SQLITE_MUTEX_N
63650 4f 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e  OOP.#  endif.#en
63660 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
63670 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a  TE_MUTEX_OMIT./*
63680 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61  .** If this is a
63690 20 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74   no-op implement
636a0 61 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74  ation, implement
636b0 20 65 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d   everything as m
636c0 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  acros..*/.#defin
636d0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
636e0 61 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71  alloc(X)    ((sq
636f0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a  lite3_mutex*)8).
63700 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
63710 6d 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64  mutex_free(X).#d
63720 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75  efine sqlite3_mu
63730 74 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65  tex_enter(X).#de
63740 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
63750 65 78 5f 74 72 79 28 58 29 20 20 20 20 20 20 53  ex_try(X)      S
63760 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65  QLITE_OK.#define
63770 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
63780 65 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20  eave(X).#define 
63790 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
637a0 6c 64 28 58 29 20 20 20 20 20 31 0a 23 64 65 66  ld(X)     1.#def
637b0 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
637c0 78 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a  x_notheld(X)  1.
637d0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d  #define sqlite3M
637e0 75 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20  utexAlloc(X)    
637f0 20 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65    ((sqlite3_mute
63800 78 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71  x*)8).#define sq
63810 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29  lite3MutexInit()
63820 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
63830 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  K.#define sqlite
63840 33 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64  3MutexEnd().#end
63850 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51  if /* defined(SQ
63860 4c 49 54 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 29  LITE_OMIT_MUTEX)
63870 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
63880 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65  **** End of mute
63890 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.h ************
638a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
638b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
638c0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
638d0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
638e0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
638f0 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
63900 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
63910 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  ***/.../*.** Eac
63920 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  h database file 
63930 74 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 62  to be accessed b
63940 79 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20  y the system is 
63950 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f  an instance.** o
63960 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
63970 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72  structure.  Ther
63980 65 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74  e are normally t
63990 77 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72 75  wo of these stru
639a0 63 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65  ctures.** in the
639b0 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72   sqlite.aDb[] ar
639c0 72 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20  ray.  aDb[0] is 
639d0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
639e0 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44  e file and.** aD
639f0 62 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74 61  b[1] is the data
63a00 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74  base file used t
63a10 6f 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79  o hold temporary
63a20 20 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69   tables.  Additi
63a30 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65  onal.** database
63a40 73 20 6d 61 79 20 62 65 20 61 74 74 61 63 68 65  s may be attache
63a50 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20  d..*/.struct Db 
63a60 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  {.  char *zName;
63a70 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
63a80 20 6f 66 20 74 68 69 73 20 64 61 74 61 62 61 73   of this databas
63a90 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42  e */.  Btree *pB
63aa0 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
63ab0 68 65 20 42 2a 54 72 65 65 20 73 74 72 75 63 74  he B*Tree struct
63ac0 75 72 65 20 66 6f 72 20 74 68 69 73 20 64 61 74  ure for this dat
63ad0 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
63ae0 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20  u8 inTrans;     
63af0 20 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77       /* 0: not w
63b00 72 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61  ritable.  1: Tra
63b10 6e 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68  nsaction.  2: Ch
63b20 65 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38  eckpoint */.  u8
63b30 20 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20   safety_level;  
63b40 20 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73     /* How aggres
63b50 73 69 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20  sive at syncing 
63b60 64 61 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a  data to disk */.
63b70 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 20    void *pAux;   
63b80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
63b90 75 78 69 6c 69 61 72 79 20 64 61 74 61 2e 20 20  uxiliary data.  
63ba0 55 73 75 61 6c 6c 79 20 4e 55 4c 4c 20 2a 2f 0a  Usually NULL */.
63bb0 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 41 75    void (*xFreeAu
63bc0 78 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 52  x)(void*);  /* R
63bd0 6f 75 74 69 6e 65 20 74 6f 20 66 72 65 65 20 70  outine to free p
63be0 41 75 78 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20  Aux */.  Schema 
63bf0 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a  *pSchema;     /*
63c00 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61   Pointer to data
63c10 62 61 73 65 20 73 63 68 65 6d 61 20 28 70 6f 73  base schema (pos
63c20 73 69 62 6c 79 20 73 68 61 72 65 64 29 20 2a 2f  sibly shared) */
63c30 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  .};../*.** An in
63c40 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
63c50 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
63c60 65 20 73 74 6f 72 65 73 20 61 20 64 61 74 61 62  e stores a datab
63c70 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a  ase schema..**.*
63c80 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e  * If there are n
63c90 6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  o virtual tables
63ca0 20 63 6f 6e 66 69 67 75 72 65 64 20 69 6e 20 74   configured in t
63cb0 68 69 73 20 73 63 68 65 6d 61 2c 20 74 68 65 0a  his schema, the.
63cc0 2a 2a 20 53 63 68 65 6d 61 2e 64 62 20 76 61 72  ** Schema.db var
63cd0 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20  iable is set to 
63ce0 4e 55 4c 4c 2e 20 41 66 74 65 72 20 74 68 65 20  NULL. After the 
63cf0 66 69 72 73 74 20 76 69 72 74 75 61 6c 20 74 61  first virtual ta
63d00 62 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ble.** has been 
63d10 61 64 64 65 64 2c 20 69 74 20 69 73 20 73 65 74  added, it is set
63d20 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
63d30 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
63d40 74 69 6f 6e 20 0a 2a 2a 20 75 73 65 64 20 74 6f  tion .** used to
63d50 20 63 72 65 61 74 65 20 74 68 65 20 63 6f 6e 6e   create the conn
63d60 65 63 74 69 6f 6e 2e 20 4f 6e 63 65 20 61 20 76  ection. Once a v
63d70 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 73  irtual table has
63d80 20 62 65 65 6e 0a 2a 2a 20 61 64 64 65 64 20 74   been.** added t
63d90 6f 20 74 68 65 20 53 63 68 65 6d 61 20 73 74 72  o the Schema str
63da0 75 63 74 75 72 65 20 61 6e 64 20 74 68 65 20 53  ucture and the S
63db0 63 68 65 6d 61 2e 64 62 20 76 61 72 69 61 62 6c  chema.db variabl
63dc0 65 20 70 6f 70 75 6c 61 74 65 64 2c 20 0a 2a 2a  e populated, .**
63dd0 20 6f 6e 6c 79 20 74 68 61 74 20 64 61 74 61 62   only that datab
63de0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d  ase connection m
63df0 61 79 20 75 73 65 20 74 68 65 20 53 63 68 65 6d  ay use the Schem
63e00 61 20 74 6f 20 70 72 65 70 61 72 65 20 0a 2a 2a  a to prepare .**
63e10 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
63e20 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 7b 0a  struct Schema {.
63e30 20 20 69 6e 74 20 73 63 68 65 6d 61 5f 63 6f 6f    int schema_coo
63e40 6b 69 65 3b 20 20 20 2f 2a 20 44 61 74 61 62 61  kie;   /* Databa
63e50 73 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f  se schema versio
63e60 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69  n number for thi
63e70 73 20 66 69 6c 65 20 2a 2f 0a 20 20 48 61 73 68  s file */.  Hash
63e80 20 74 62 6c 48 61 73 68 3b 20 20 20 20 20 20 20   tblHash;       
63e90 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20 69   /* All tables i
63ea0 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 2a  ndexed by name *
63eb0 2f 0a 20 20 48 61 73 68 20 69 64 78 48 61 73 68  /.  Hash idxHash
63ec0 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20  ;        /* All 
63ed0 28 6e 61 6d 65 64 29 20 69 6e 64 69 63 65 73 20  (named) indices 
63ee0 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20  indexed by name 
63ef0 2a 2f 0a 20 20 48 61 73 68 20 74 72 69 67 48 61  */.  Hash trigHa
63f00 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c  sh;       /* All
63f10 20 74 72 69 67 67 65 72 73 20 69 6e 64 65 78 65   triggers indexe
63f20 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48  d by name */.  H
63f30 61 73 68 20 61 46 4b 65 79 3b 20 20 20 20 20 20  ash aFKey;      
63f40 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b      /* Foreign k
63f50 65 79 73 20 69 6e 64 65 78 65 64 20 62 79 20 74  eys indexed by t
63f60 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62  o-table */.  Tab
63f70 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20 20 20  le *pSeqTab;    
63f80 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f    /* The sqlite_
63f90 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75  sequence table u
63fa0 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 52 45  sed by AUTOINCRE
63fb0 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c  MENT */.  u8 fil
63fc0 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f  e_format;      /
63fd0 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74 20  * Schema format 
63fe0 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68 69 73  version for this
63ff0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e   file */.  u8 en
64000 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
64010 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67  /* Text encoding
64020 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64 61   used by this da
64030 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31 36 20  tabase */.  u16 
64040 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
64050 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 69   /* Flags associ
64060 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 73  ated with this s
64070 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63  chema */.  int c
64080 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20 20 20  ache_size;      
64090 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
640a0 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74 68 65  es to use in the
640b0 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65   cache */.#ifnde
640c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
640d0 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c  RTUALTABLE.  sql
640e0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
640f0 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e    /* "Owner" con
64100 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d  nection. See com
64110 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65  ment above */.#e
64120 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  ndif.};../*.** T
64130 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20  hese macros can 
64140 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c  be used to test,
64150 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62   set, or clear b
64160 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44  its in the .** D
64170 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a  b.flags field..*
64180 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73 50  /.#define DbHasP
64190 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20  roperty(D,I,P)  
641a0 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d     (((D)->aDb[I]
641b0 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26  .pSchema->flags&
641c0 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69  (P))==(P)).#defi
641d0 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f 70 65  ne DbHasAnyPrope
641e0 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 28 44  rty(D,I,P)  (((D
641f0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d  )->aDb[I].pSchem
64200 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30  a->flags&(P))!=0
64210 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 74 50  ).#define DbSetP
64220 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20  roperty(D,I,P)  
64230 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70     (D)->aDb[I].p
64240 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28  Schema->flags|=(
64250 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 6c 65  P).#define DbCle
64260 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50  arProperty(D,I,P
64270 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e  )   (D)->aDb[I].
64280 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d  pSchema->flags&=
64290 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  ~(P)../*.** Allo
642a0 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74  wed values for t
642b0 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69 65 6c  he DB.flags fiel
642c0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f  d..**.** The DB_
642d0 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61  SchemaLoaded fla
642e0 67 20 69 73 20 73 65 74 20 61 66 74 65 72 20 74  g is set after t
642f0 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
64300 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72  ma has been.** r
64310 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61  ead into interna
64320 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a  l hash tables..*
64330 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74 56  *.** DB_UnresetV
64340 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61 74 20  iews means that 
64350 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69 65 77  one or more view
64360 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61  s have column na
64370 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 65  mes that.** have
64380 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74   been filled out
64390 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d 61  .  If the schema
643a0 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73 65 20   changes, these 
643b0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67  column names mig
643c0 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e  ht.** changes an
643d0 64 20 73 6f 20 74 68 65 20 76 69 65 77 20 77 69  d so the view wi
643e0 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65  ll need to be re
643f0 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  set..*/.#define 
64400 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20  DB_SchemaLoaded 
64410 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 54 68     0x0001  /* Th
64420 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 65  e schema has bee
64430 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66  n loaded */.#def
64440 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74 56 69  ine DB_UnresetVi
64450 65 77 73 20 20 20 20 30 78 30 30 30 32 20 20 2f  ews    0x0002  /
64460 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68 61 76  * Some views hav
64470 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e  e defined column
64480 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e   names */.#defin
64490 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20 20 20  e DB_Empty      
644a0 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20       0x0004  /* 
644b0 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d 70 74  The file is empt
644c0 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79 74 65  y (length 0 byte
644d0 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  s) */../*.** The
644e0 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65   number of diffe
644f0 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 68  rent kinds of th
64500 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 62 65  ings that can be
64510 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e   limited.** usin
64520 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 69  g the sqlite3_li
64530 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e  mit() interface.
64540 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
64550 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49  TE_N_LIMIT (SQLI
64560 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c  TE_LIMIT_VARIABL
64570 45 5f 4e 55 4d 42 45 52 2b 31 29 0a 0a 2f 2a 0a  E_NUMBER+1)../*.
64580 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c  ** Lookaside mal
64590 6c 6f 63 20 69 73 20 61 20 73 65 74 20 6f 66 20  loc is a set of 
645a0 66 69 78 65 64 2d 73 69 7a 65 20 62 75 66 66 65  fixed-size buffe
645b0 72 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  rs that can be u
645c0 73 65 64 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66  sed.** to satisf
645d0 79 20 73 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e  y small transien
645e0 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  t memory allocat
645f0 69 6f 6e 20 72 65 71 75 65 73 74 73 20 66 6f 72  ion requests for
64600 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f   objects.** asso
64610 63 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61  ciated with a pa
64620 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
64630 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54  e connection.  T
64640 68 65 20 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f  he use of.** loo
64650 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72  kaside malloc pr
64660 6f 76 69 64 65 73 20 61 20 73 69 67 6e 69 66 69  ovides a signifi
64670 63 61 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65  cant performance
64680 20 65 6e 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20   enhancement.** 
64690 28 61 70 70 72 6f 78 20 31 30 25 29 20 62 79 20  (approx 10%) by 
646a0 61 76 6f 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75  avoiding numerou
646b0 73 20 6d 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65  s malloc/free re
646c0 71 75 65 73 74 73 20 77 68 69 6c 65 20 70 61 72  quests while par
646d0 73 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74  sing.** SQL stat
646e0 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ements..**.** Th
646f0 65 20 4c 6f 6f 6b 61 73 69 64 65 20 73 74 72 75  e Lookaside stru
64700 63 74 75 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66  cture holds conf
64710 69 67 75 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d  iguration inform
64720 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a  ation about the.
64730 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c  ** lookaside mal
64740 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 2e 20 20  loc subsystem.  
64750 45 61 63 68 20 61 76 61 69 6c 61 62 6c 65 20 6d  Each available m
64760 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
64770 20 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61   in.** the looka
64780 73 69 64 65 20 73 75 62 73 79 73 74 65 6d 20 69  side subsystem i
64790 73 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69  s stored on a li
647a0 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f  nked list of Loo
647b0 6b 61 73 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62  kasideSlot.** ob
647c0 6a 65 63 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  jects..*/.struct
647d0 20 4c 6f 6f 6b 61 73 69 64 65 20 7b 0a 20 20 75   Lookaside {.  u
647e0 31 36 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20  16 sz;          
647f0 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
64800 66 20 65 61 63 68 20 62 75 66 66 65 72 20 69 6e  f each buffer in
64810 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 38 20 62   bytes */.  u8 b
64820 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20  Enabled;        
64830 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75      /* True if u
64840 73 65 20 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 46  se lookaside.  F
64850 61 6c 73 65 20 74 6f 20 69 67 6e 6f 72 65 20 69  alse to ignore i
64860 74 20 2a 2f 0a 20 20 75 38 20 62 4d 61 6c 6c 6f  t */.  u8 bMallo
64870 63 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ced;           /
64880 2a 20 54 72 75 65 20 69 66 20 70 53 74 61 72 74  * True if pStart
64890 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
648a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
648b0 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 3b 20 20  */.  int nOut;  
648c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
648d0 4e 75 6d 62 65 72 20 6f 66 20 62 75 66 66 65 72  Number of buffer
648e0 73 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63  s currently chec
648f0 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 69 6e 74  ked out */.  int
64900 20 6d 78 4f 75 74 3b 20 20 20 20 20 20 20 20 20   mxOut;         
64910 20 20 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65       /* Highwate
64920 72 20 6d 61 72 6b 20 66 6f 72 20 6e 4f 75 74 20  r mark for nOut 
64930 2a 2f 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c  */.  LookasideSl
64940 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 2f 2a 20  ot *pFree;   /* 
64950 4c 69 73 74 20 6f 66 20 61 76 61 69 6c 61 62 6c  List of availabl
64960 65 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20 76  e buffers */.  v
64970 6f 69 64 20 2a 70 53 74 61 72 74 3b 20 20 20 20  oid *pStart;    
64980 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
64990 62 79 74 65 20 6f 66 20 61 76 61 69 6c 61 62 6c  byte of availabl
649a0 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a  e memory space *
649b0 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e 64 3b 20  /.  void *pEnd; 
649c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
649d0 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 65  irst byte past e
649e0 6e 64 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20  nd of available 
649f0 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 73 74 72 75  space */.};.stru
64a00 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74  ct LookasideSlot
64a10 20 7b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c   {.  LookasideSl
64a20 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a  ot *pNext;    /*
64a30 20 4e 65 78 74 20 62 75 66 66 65 72 20 69 6e 20   Next buffer in 
64a40 74 68 65 20 6c 69 73 74 20 6f 66 20 66 72 65 65  the list of free
64a50 20 62 75 66 66 65 72 73 20 2a 2f 0a 7d 3b 0a 0a   buffers */.};..
64a60 2f 2a 0a 2a 2a 20 41 20 68 61 73 68 20 74 61 62  /*.** A hash tab
64a70 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20  le for function 
64a80 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 0a  definitions..**.
64a90 2a 2a 20 48 61 73 68 20 65 61 63 68 20 46 75 6e  ** Hash each Fun
64aa0 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69  cDef structure i
64ab0 6e 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 46  nto one of the F
64ac0 75 6e 63 44 65 66 48 61 73 68 2e 61 5b 5d 20 73  uncDefHash.a[] s
64ad0 6c 6f 74 73 2e 0a 2a 2a 20 43 6f 6c 6c 69 73 69  lots..** Collisi
64ae0 6f 6e 73 20 61 72 65 20 6f 6e 20 74 68 65 20 46  ons are on the F
64af0 75 6e 63 44 65 66 2e 70 48 61 73 68 20 63 68 61  uncDef.pHash cha
64b00 69 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75  in..*/.struct Fu
64b10 6e 63 44 65 66 48 61 73 68 20 7b 0a 20 20 46 75  ncDefHash {.  Fu
64b20 6e 63 44 65 66 20 2a 61 5b 32 33 5d 3b 20 20 20  ncDef *a[23];   
64b30 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c      /* Hash tabl
64b40 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20  e for functions 
64b50 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  */.};../*.** Eac
64b60 68 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6e  h database is an
64b70 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
64b80 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
64b90 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ture..**.** The 
64ba0 73 71 6c 69 74 65 2e 6c 61 73 74 52 6f 77 69 64  sqlite.lastRowid
64bb0 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 73   records the las
64bc0 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 67  t insert rowid g
64bd0 65 6e 65 72 61 74 65 64 20 62 79 20 61 6e 0a 2a  enerated by an.*
64be0 2a 20 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65  * insert stateme
64bf0 6e 74 2e 20 20 49 6e 73 65 72 74 73 20 6f 6e 20  nt.  Inserts on 
64c00 76 69 65 77 73 20 64 6f 20 6e 6f 74 20 61 66 66  views do not aff
64c10 65 63 74 20 69 74 73 20 76 61 6c 75 65 2e 20 20  ect its value.  
64c20 45 61 63 68 0a 2a 2a 20 74 72 69 67 67 65 72 20  Each.** trigger 
64c30 68 61 73 20 69 74 73 20 6f 77 6e 20 63 6f 6e 74  has its own cont
64c40 65 78 74 2c 20 73 6f 20 74 68 61 74 20 6c 61 73  ext, so that las
64c50 74 52 6f 77 69 64 20 63 61 6e 20 62 65 20 75 70  tRowid can be up
64c60 64 61 74 65 64 20 69 6e 73 69 64 65 0a 2a 2a 20  dated inside.** 
64c70 74 72 69 67 67 65 72 73 20 61 73 20 75 73 75 61  triggers as usua
64c80 6c 2e 20 20 54 68 65 20 70 72 65 76 69 6f 75 73  l.  The previous
64c90 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 72   value will be r
64ca0 65 73 74 6f 72 65 64 20 6f 6e 63 65 20 74 68 65  estored once the
64cb0 20 74 72 69 67 67 65 72 0a 2a 2a 20 65 78 69 74   trigger.** exit
64cc0 73 2e 20 20 55 70 6f 6e 20 65 6e 74 65 72 69 6e  s.  Upon enterin
64cd0 67 20 61 20 62 65 66 6f 72 65 20 6f 72 20 69 6e  g a before or in
64ce0 73 74 65 61 64 20 6f 66 20 74 72 69 67 67 65 72  stead of trigger
64cf0 2c 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20 6e  , lastRowid is n
64d00 6f 0a 2a 2a 20 6c 6f 6e 67 65 72 20 28 73 69 6e  o.** longer (sin
64d10 63 65 20 61 66 74 65 72 20 76 65 72 73 69 6f 6e  ce after version
64d20 20 32 2e 38 2e 31 32 29 20 72 65 73 65 74 20 74   2.8.12) reset t
64d30 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  o -1..**.** The 
64d40 73 71 6c 69 74 65 2e 6e 43 68 61 6e 67 65 20 64  sqlite.nChange d
64d50 6f 65 73 20 6e 6f 74 20 63 6f 75 6e 74 20 63 68  oes not count ch
64d60 61 6e 67 65 73 20 77 69 74 68 69 6e 20 74 72 69  anges within tri
64d70 67 67 65 72 73 20 61 6e 64 20 6b 65 65 70 73 20  ggers and keeps 
64d80 6e 6f 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 20 20  no.** context.  
64d90 49 74 20 69 73 20 72 65 73 65 74 20 61 74 20 73  It is reset at s
64da0 74 61 72 74 20 6f 66 20 73 71 6c 69 74 65 33 5f  tart of sqlite3_
64db0 65 78 65 63 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  exec..** The sql
64dc0 69 74 65 2e 6c 73 43 68 61 6e 67 65 20 72 65 70  ite.lsChange rep
64dd0 72 65 73 65 6e 74 73 20 74 68 65 20 6e 75 6d 62  resents the numb
64de0 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 6d 61  er of changes ma
64df0 64 65 20 62 79 20 74 68 65 20 6c 61 73 74 0a 2a  de by the last.*
64e00 2a 20 69 6e 73 65 72 74 2c 20 75 70 64 61 74 65  * insert, update
64e10 2c 20 6f 72 20 64 65 6c 65 74 65 20 73 74 61 74  , or delete stat
64e20 65 6d 65 6e 74 2e 20 20 49 74 20 72 65 6d 61 69  ement.  It remai
64e30 6e 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 6f  ns constant thro
64e40 75 67 68 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 65  ughout the.** le
64e50 6e 67 74 68 20 6f 66 20 61 20 73 74 61 74 65 6d  ngth of a statem
64e60 65 6e 74 20 61 6e 64 20 69 73 20 74 68 65 6e 20  ent and is then 
64e70 75 70 64 61 74 65 64 20 62 79 20 4f 50 5f 53 65  updated by OP_Se
64e80 74 43 6f 75 6e 74 73 2e 20 20 49 74 20 6b 65 65  tCounts.  It kee
64e90 70 73 20 61 0a 2a 2a 20 63 6f 6e 74 65 78 74 20  ps a.** context 
64ea0 73 74 61 63 6b 20 6a 75 73 74 20 6c 69 6b 65 20  stack just like 
64eb0 6c 61 73 74 52 6f 77 69 64 20 73 6f 20 74 68 61  lastRowid so tha
64ec0 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 63  t the count of c
64ed0 68 61 6e 67 65 73 0a 2a 2a 20 77 69 74 68 69 6e  hanges.** within
64ee0 20 61 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f   a trigger is no
64ef0 74 20 73 65 65 6e 20 6f 75 74 73 69 64 65 20 74  t seen outside t
64f00 68 65 20 74 72 69 67 67 65 72 2e 20 20 43 68 61  he trigger.  Cha
64f10 6e 67 65 73 20 74 6f 20 76 69 65 77 73 20 64 6f  nges to views do
64f20 20 6e 6f 74 0a 2a 2a 20 61 66 66 65 63 74 20 74   not.** affect t
64f30 68 65 20 76 61 6c 75 65 20 6f 66 20 6c 73 43 68  he value of lsCh
64f40 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  ange..** The sql
64f50 69 74 65 2e 63 73 43 68 61 6e 67 65 20 6b 65 65  ite.csChange kee
64f60 70 73 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20  ps track of the 
64f70 6e 75 6d 62 65 72 20 6f 66 20 63 75 72 72 65 6e  number of curren
64f80 74 20 63 68 61 6e 67 65 73 20 28 73 69 6e 63 65  t changes (since
64f90 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 73 74 61  .** the last sta
64fa0 74 65 6d 65 6e 74 29 20 61 6e 64 20 69 73 20 75  tement) and is u
64fb0 73 65 64 20 74 6f 20 75 70 64 61 74 65 20 73 71  sed to update sq
64fc0 6c 69 74 65 5f 6c 73 43 68 61 6e 67 65 2e 0a 2a  lite_lsChange..*
64fd0 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 62 65 72 20  *.** The member 
64fe0 76 61 72 69 61 62 6c 65 73 20 73 71 6c 69 74 65  variables sqlite
64ff0 2e 65 72 72 43 6f 64 65 2c 20 73 71 6c 69 74 65  .errCode, sqlite
65000 2e 7a 45 72 72 4d 73 67 20 61 6e 64 20 73 71 6c  .zErrMsg and sql
65010 69 74 65 2e 7a 45 72 72 4d 73 67 31 36 0a 2a 2a  ite.zErrMsg16.**
65020 20 73 74 6f 72 65 20 74 68 65 20 6d 6f 73 74 20   store the most 
65030 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f 64  recent error cod
65040 65 20 61 6e 64 2c 20 69 66 20 61 70 70 6c 69 63  e and, if applic
65050 61 62 6c 65 2c 20 73 74 72 69 6e 67 2e 20 54 68  able, string. Th
65060 65 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 66 75  e.** internal fu
65070 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 45 72  nction sqlite3Er
65080 72 6f 72 28 29 20 69 73 20 75 73 65 64 20 74 6f  ror() is used to
65090 20 73 65 74 20 74 68 65 73 65 20 76 61 72 69 61   set these varia
650a0 62 6c 65 73 0a 2a 2a 20 63 6f 6e 73 69 73 74 65  bles.** consiste
650b0 6e 74 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ntly..*/.struct 
650c0 73 71 6c 69 74 65 33 20 7b 0a 20 20 73 71 6c 69  sqlite3 {.  sqli
650d0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20  te3_vfs *pVfs;  
650e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 53 20            /* OS 
650f0 49 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 69  Interface */.  i
65100 6e 74 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20  nt nDb;         
65110 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
65120 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 65 6e  Number of backen
65130 64 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  ds currently in 
65140 75 73 65 20 2a 2f 0a 20 20 44 62 20 2a 61 44 62  use */.  Db *aDb
65150 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
65160 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 62 61         /* All ba
65170 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20  ckends */.  int 
65180 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
65190 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 73            /* Mis
651a0 63 65 6c 6c 61 6e 65 6f 75 73 20 66 6c 61 67 73  cellaneous flags
651b0 2e 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20  . See below */. 
651c0 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 3b 20   int openFlags; 
651d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
651e0 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74  * Flags passed t
651f0 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f  o sqlite3_vfs.xO
65200 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65  pen() */.  int e
65210 72 72 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20  rrCode;         
65220 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74           /* Most
65230 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f   recent error co
65240 64 65 20 28 53 51 4c 49 54 45 5f 2a 29 20 2a 2f  de (SQLITE_*) */
65250 0a 20 20 69 6e 74 20 65 72 72 4d 61 73 6b 3b 20  .  int errMask; 
65260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65270 20 2f 2a 20 26 20 72 65 73 75 6c 74 20 63 6f 64   /* & result cod
65280 65 73 20 77 69 74 68 20 74 68 69 73 20 62 65 66  es with this bef
65290 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 2a 2f  ore returning */
652a0 0a 20 20 75 38 20 61 75 74 6f 43 6f 6d 6d 69 74  .  u8 autoCommit
652b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
652c0 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d   /* The auto-com
652d0 6d 69 74 20 66 6c 61 67 2e 20 2a 2f 0a 20 20 75  mit flag. */.  u
652e0 38 20 74 65 6d 70 5f 73 74 6f 72 65 3b 20 20 20  8 temp_store;   
652f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
65300 31 3a 20 66 69 6c 65 20 32 3a 20 6d 65 6d 6f 72  1: file 2: memor
65310 79 20 30 3a 20 64 65 66 61 75 6c 74 20 2a 2f 0a  y 0: default */.
65320 20 20 75 38 20 6d 61 6c 6c 6f 63 46 61 69 6c 65    u8 mallocFaile
65330 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
65340 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 68 61  /* True if we ha
65350 76 65 20 73 65 65 6e 20 61 20 6d 61 6c 6c 6f 63  ve seen a malloc
65360 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 75 38   failure */.  u8
65370 20 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b 20 20   dfltLockMode;  
65380 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
65390 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 2d 6d  efault locking-m
653a0 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68 65 64  ode for attached
653b0 20 64 62 73 20 2a 2f 0a 20 20 75 38 20 64 66 6c   dbs */.  u8 dfl
653c0 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20  tJournalMode;   
653d0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75          /* Defau
653e0 6c 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20  lt journal mode 
653f0 66 6f 72 20 61 74 74 61 63 68 65 64 20 64 62 73  for attached dbs
65400 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61   */.  signed cha
65410 72 20 6e 65 78 74 41 75 74 6f 76 61 63 3b 20 20  r nextAutovac;  
65420 20 20 20 20 2f 2a 20 41 75 74 6f 76 61 63 20 73      /* Autovac s
65430 65 74 74 69 6e 67 20 61 66 74 65 72 20 56 41 43  etting after VAC
65440 55 55 4d 20 69 66 20 3e 3d 30 20 2a 2f 0a 20 20  UUM if >=0 */.  
65450 69 6e 74 20 6e 65 78 74 50 61 67 65 73 69 7a 65  int nextPagesize
65460 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
65470 20 50 61 67 65 73 69 7a 65 20 61 66 74 65 72 20   Pagesize after 
65480 56 41 43 55 55 4d 20 69 66 20 3e 30 20 2a 2f 0a  VACUUM if >0 */.
65490 20 20 69 6e 74 20 6e 54 61 62 6c 65 3b 20 20 20    int nTable;   
654a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
654b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62  /* Number of tab
654c0 6c 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  les in the datab
654d0 61 73 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71  ase */.  CollSeq
654e0 20 2a 70 44 66 6c 74 43 6f 6c 6c 3b 20 20 20 20   *pDfltColl;    
654f0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65         /* The de
65500 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20  fault collating 
65510 73 65 71 75 65 6e 63 65 20 28 42 49 4e 41 52 59  sequence (BINARY
65520 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52  ) */.  i64 lastR
65530 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  owid;           
65540 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20 6f 66       /* ROWID of
65550 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 73   most recent ins
65560 65 72 74 20 28 73 65 65 20 61 62 6f 76 65 29 20  ert (see above) 
65570 2a 2f 0a 20 20 69 36 34 20 70 72 69 6f 72 4e 65  */.  i64 priorNe
65580 77 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  wRowid;         
65590 20 20 20 2f 2a 20 4c 61 73 74 20 72 61 6e 64 6f     /* Last rando
655a0 6d 6c 79 20 67 65 6e 65 72 61 74 65 64 20 52 4f  mly generated RO
655b0 57 49 44 20 2a 2f 0a 20 20 75 33 32 20 6d 61 67  WID */.  u32 mag
655c0 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ic;             
655d0 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20         /* Magic 
655e0 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74 65 63  number for detec
655f0 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75 73 65  t library misuse
65600 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67   */.  int nChang
65610 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
65620 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74      /* Value ret
65630 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
65640 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20  _changes() */.  
65650 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e 67 65  int nTotalChange
65660 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
65670 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   Value returned 
65680 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  by sqlite3_total
65690 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20  _changes() */.  
656a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
656b0 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a  utex;         /*
656c0 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65   Connection mute
656d0 78 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 6d 69  x */.  int aLimi
656e0 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54  t[SQLITE_N_LIMIT
656f0 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73 20 2a  ];   /* Limits *
65700 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74  /.  struct sqlit
65710 65 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20 20 20  e3InitInfo {    
65720 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
65730 20 75 73 65 64 20 64 75 72 69 6e 67 20 69 6e 69   used during ini
65740 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20  tialization */. 
65750 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20     int iDb;     
65760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
65770 2a 20 57 68 65 6e 20 62 61 63 6b 20 69 73 20 62  * When back is b
65780 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64  eing initialized
65790 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 77 54   */.    int newT
657a0 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  num;            
657b0 20 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67 65 20      /* Rootpage 
657c0 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69  of table being i
657d0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
657e0 20 20 75 38 20 62 75 73 79 3b 20 20 20 20 20 20    u8 busy;      
657f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
65800 20 54 52 55 45 20 69 66 20 63 75 72 72 65 6e 74   TRUE if current
65810 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  ly initializing 
65820 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69  */.  } init;.  i
65830 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 20  nt nExtension;  
65840 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
65850 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64 65 64  Number of loaded
65860 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20   extensions */. 
65870 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e 73 69   void **aExtensi
65880 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  on;            /
65890 2a 20 41 72 72 61 79 20 6f 66 20 73 68 61 72 65  * Array of share
658a0 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64 6c 65  d library handle
658b0 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 56 64  s */.  struct Vd
658c0 62 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20  be *pVdbe;      
658d0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
658e0 61 63 74 69 76 65 20 76 69 72 74 75 61 6c 20 6d  active virtual m
658f0 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74  achines */.  int
65900 20 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 20   activeVdbeCnt; 
65910 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
65920 6d 62 65 72 20 6f 66 20 56 44 42 45 73 20 63 75  mber of VDBEs cu
65930 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e  rrently executin
65940 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69 74 65  g */.  int write
65950 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20  VdbeCnt;        
65960 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
65970 66 20 61 63 74 69 76 65 20 56 44 42 45 73 20 74  f active VDBEs t
65980 68 61 74 20 61 72 65 20 77 72 69 74 69 6e 67 20  hat are writing 
65990 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 61  */.  void (*xTra
659a0 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  ce)(void*,const 
659b0 63 68 61 72 2a 29 3b 20 20 20 20 20 20 20 20 2f  char*);        /
659c0 2a 20 54 72 61 63 65 20 66 75 6e 63 74 69 6f 6e  * Trace function
659d0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 54 72 61   */.  void *pTra
659e0 63 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  ceArg;          
659f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65a00 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
65a10 68 65 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f  he trace functio
65a20 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 50  n */.  void (*xP
65a30 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f  rofile)(void*,co
65a40 6e 73 74 20 63 68 61 72 2a 2c 75 36 34 29 3b 20  nst char*,u64); 
65a50 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 20 66 75   /* Profiling fu
65a60 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
65a70 20 2a 70 50 72 6f 66 69 6c 65 41 72 67 3b 20 20   *pProfileArg;  
65a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65a90 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
65aa0 74 20 74 6f 20 70 72 6f 66 69 6c 65 20 66 75 6e  t to profile fun
65ab0 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
65ac0 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20 20 20 20  *pCommitArg;    
65ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
65ae0 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 6d  Argument to xCom
65af0 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f  mitCallback() */
65b00 20 20 20 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d     .  int (*xCom
65b10 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  mitCallback)(voi
65b20 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e 76 6f 6b  d*);    /* Invok
65b30 65 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d  ed at every comm
65b40 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  it. */.  void *p
65b50 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 20 20 20  RollbackArg;    
65b60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
65b70 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f 6c 6c 62  gument to xRollb
65b80 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f  ackCallback() */
65b90 20 20 20 0a 20 20 76 6f 69 64 20 28 2a 78 52 6f     .  void (*xRo
65ba0 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 29 28  llbackCallback)(
65bb0 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b  void*); /* Invok
65bc0 65 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d  ed at every comm
65bd0 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  it. */.  void *p
65be0 55 70 64 61 74 65 41 72 67 3b 0a 20 20 76 6f 69  UpdateArg;.  voi
65bf0 64 20 28 2a 78 55 70 64 61 74 65 43 61 6c 6c 62  d (*xUpdateCallb
65c00 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20  ack)(void*,int, 
65c10 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
65c20 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 69  t char*,sqlite_i
65c30 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64 28 2a 78  nt64);.  void(*x
65c40 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64  CollNeeded)(void
65c50 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
65c60 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
65c70 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43  ar*);.  void(*xC
65c80 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69  ollNeeded16)(voi
65c90 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  d*,sqlite3*,int 
65ca0 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76  eTextRep,const v
65cb0 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70  oid*);.  void *p
65cc0 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20  CollNeededArg;. 
65cd0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
65ce0 70 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 2f  pErr;          /
65cf0 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  * Most recent er
65d00 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20  ror message */. 
65d10 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20   char *zErrMsg; 
65d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
65d30 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  * Most recent er
65d40 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46  ror message (UTF
65d50 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20  -8 encoded) */. 
65d60 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 31 36   char *zErrMsg16
65d70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
65d80 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  * Most recent er
65d90 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46  ror message (UTF
65da0 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a  -16 encoded) */.
65db0 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 76 6f    union {.    vo
65dc0 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49 6e 74  latile int isInt
65dd0 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 54 72 75  errupted; /* Tru
65de0 65 20 69 66 20 73 71 6c 69 74 65 33 5f 69 6e 74  e if sqlite3_int
65df0 65 72 72 75 70 74 20 68 61 73 20 62 65 65 6e 20  errupt has been 
65e00 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 64 6f  called */.    do
65e10 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b 20 20  uble notUsed1;  
65e20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
65e30 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b 0a 20  cer */.  } u1;. 
65e40 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f 6b 61   Lookaside looka
65e50 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f  side;          /
65e60 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c  * Lookaside mall
65e70 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  oc configuration
65e80 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
65e90 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
65ea0 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a 78 41  ATION.  int (*xA
65eb0 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  uth)(void*,int,c
65ec0 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
65ed0 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
65ee0 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  r*,const char*);
65ef0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
65f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65f10 20 2f 2a 20 41 63 63 65 73 73 20 61 75 74 68 6f   /* Access autho
65f20 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
65f30 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75  n */.  void *pAu
65f40 74 68 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  thArg;          
65f50 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75       /* 1st argu
65f60 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 63 63 65  ment to the acce
65f70 73 73 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  ss auth function
65f80 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64   */.#endif.#ifnd
65f90 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
65fa0 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b  ROGRESS_CALLBACK
65fb0 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65  .  int (*xProgre
65fc0 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20  ss)(void *);    
65fd0 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73   /* The progress
65fe0 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76   callback */.  v
65ff0 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 73 41 72  oid *pProgressAr
66000 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g;           /* 
66010 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  Argument to the 
66020 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
66030 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 6f 67  k */.  int nProg
66040 72 65 73 73 4f 70 73 3b 20 20 20 20 20 20 20 20  ressOps;        
66050 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
66060 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 70 72  f opcodes for pr
66070 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
66080 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  */.#endif.#ifnde
66090 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
660a0 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 48 61 73  RTUALTABLE.  Has
660b0 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20  h aModule;      
660c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 6f             /* po
660d0 70 75 6c 61 74 65 64 20 62 79 20 73 71 6c 69 74  pulated by sqlit
660e0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
660f0 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  () */.  Table *p
66100 56 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  VTab;           
66110 20 20 20 20 20 20 2f 2a 20 76 74 61 62 20 77 69        /* vtab wi
66120 74 68 20 61 63 74 69 76 65 20 43 6f 6e 6e 65 63  th active Connec
66130 74 2f 43 72 65 61 74 65 20 6d 65 74 68 6f 64 20  t/Create method 
66140 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  */.  sqlite3_vta
66150 62 20 2a 2a 61 56 54 72 61 6e 73 3b 20 20 20 20  b **aVTrans;    
66160 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
66170 62 6c 65 73 20 77 69 74 68 20 6f 70 65 6e 20 74  bles with open t
66180 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f 0a 20  ransactions */. 
66190 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b 20 20 20   int nVTrans;   
661a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
661b0 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65  * Allocated size
661c0 20 6f 66 20 61 56 54 72 61 6e 73 20 2a 2f 0a 23   of aVTrans */.#
661d0 65 6e 64 69 66 0a 20 20 46 75 6e 63 44 65 66 48  endif.  FuncDefH
661e0 61 73 68 20 61 46 75 6e 63 3b 20 20 20 20 20 20  ash aFunc;      
661f0 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61        /* Hash ta
66200 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f  ble of connectio
66210 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20  n functions */. 
66220 20 48 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20   Hash aCollSeq; 
66230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
66240 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20  * All collating 
66250 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42  sequences */.  B
66260 75 73 79 48 61 6e 64 6c 65 72 20 62 75 73 79 48  usyHandler busyH
66270 61 6e 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20  andler;      /* 
66280 42 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  Busy callback */
66290 0a 20 20 69 6e 74 20 62 75 73 79 54 69 6d 65 6f  .  int busyTimeo
662a0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
662b0 20 2f 2a 20 42 75 73 79 20 68 61 6e 64 6c 65 72   /* Busy handler
662c0 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 65   timeout, in mse
662d0 63 20 2a 2f 0a 20 20 44 62 20 61 44 62 53 74 61  c */.  Db aDbSta
662e0 74 69 63 5b 32 5d 3b 20 20 20 20 20 20 20 20 20  tic[2];         
662f0 20 20 20 20 20 2f 2a 20 53 74 61 74 69 63 20 73       /* Static s
66300 70 61 63 65 20 66 6f 72 20 74 68 65 20 32 20 64  pace for the 2 d
66310 65 66 61 75 6c 74 20 62 61 63 6b 65 6e 64 73 20  efault backends 
66320 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
66330 5f 53 53 45 0a 20 20 73 71 6c 69 74 65 33 5f 73  _SSE.  sqlite3_s
66340 74 6d 74 20 2a 70 46 65 74 63 68 3b 20 20 20 20  tmt *pFetch;    
66350 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20       /* Used by 
66360 53 53 45 20 74 6f 20 66 65 74 63 68 20 73 74 6f  SSE to fetch sto
66370 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 2a  red statements *
66380 2f 0a 23 65 6e 64 69 66 0a 20 20 53 61 76 65 70  /.#endif.  Savep
66390 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74  oint *pSavepoint
663a0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  ;        /* List
663b0 20 6f 66 20 61 63 74 69 76 65 20 73 61 76 65 70   of active savep
663c0 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  oints */.  int n
663d0 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20  Savepoint;      
663e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
663f0 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61  er of non-transa
66400 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73  ction savepoints
66410 20 2a 2f 0a 20 20 75 38 20 69 73 54 72 61 6e 73   */.  u8 isTrans
66420 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b  actionSavepoint;
66430 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
66440 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76  he outermost sav
66450 65 70 6f 69 6e 74 20 69 73 20 61 20 54 53 20 2a  epoint is a TS *
66460 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61  /.};../*.** A ma
66470 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20  cro to discover 
66480 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
66490 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23  a database..*/.#
664a0 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28  define ENC(db) (
664b0 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63  (db)->aDb[0].pSc
664c0 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a  hema->enc)../*.*
664d0 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  * Possible value
664e0 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65  s for the sqlite
664f0 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62  .flags and or Db
66500 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a  .flags fields..*
66510 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66  *.** On sqlite.f
66520 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45  lags, the SQLITE
66530 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d  _InTrans value m
66540 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 61 76  eans that we hav
66550 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20  e.** executed a 
66560 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c  BEGIN.  On Db.fl
66570 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72  ags, SQLITE_InTr
66580 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74  ans means a stat
66590 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63  ement.** transac
665a0 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f  tion is active o
665b0 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 6c 61  n that particula
665c0 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  r database file.
665d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
665e0 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 20 20  TE_VdbeTrace    
665f0 20 20 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a    0x00000001  /*
66600 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 20 56   True to trace V
66610 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f  DBE execution */
66620 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
66630 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 20 30  InTrans        0
66640 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72  x00000008  /* Tr
66650 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73  ue if in a trans
66660 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  action */.#defin
66670 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  e SQLITE_InternC
66680 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 30 30  hanges  0x000000
66690 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74  10  /* Uncommitt
666a0 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 63 68  ed Hash table ch
666b0 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  anges */.#define
666c0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e   SQLITE_FullColN
666d0 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 30 32  ames   0x0000002
666e0 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20  0  /* Show full 
666f0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20  column names on 
66700 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e  SELECT */.#defin
66710 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f  e SQLITE_ShortCo
66720 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 30  lNames  0x000000
66730 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72  40  /* Show shor
66740 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20  t columns names 
66750 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
66760 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20  E_CountRows     
66770 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20   0x00000080  /* 
66780 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67  Count rows chang
66790 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f  ed by INSERT, */
667a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
667b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
667c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20             /*   
667d0 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54  DELETE, or UPDAT
667e0 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a  E and return */.
667f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66810 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74            /*   t
66820 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61  he count using a
66830 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64   callback. */.#d
66840 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c  efine SQLITE_Nul
66850 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30  lCallback   0x00
66860 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b  000100  /* Invok
66870 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f  e the callback o
66880 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20  nce if the */.  
66890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
668a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
668b0 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73          /*   res
668c0 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79  ult set is empty
668d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
668e0 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20  TE_SqlTrace     
668f0 20 20 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a    0x00000200  /*
66900 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c   Debug print SQL
66910 20 61 73 20 69 74 20 65 78 65 63 75 74 65 73 20   as it executes 
66920 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
66930 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20  E_VdbeListing   
66940 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20   0x00000400  /* 
66950 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f  Debug listings o
66960 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20  f VDBE programs 
66970 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
66980 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20 20  E_WriteSchema   
66990 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20   0x00000800  /* 
669a0 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c  OK to update SQL
669b0 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64  ITE_MASTER */.#d
669c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52  efine SQLITE_NoR
669d0 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30  eadlock     0x00
669e0 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c  001000  /* Readl
669f0 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64  ocks are omitted
66a00 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20   when .         
66a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66a30 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65   ** accessing re
66a40 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65  ad-only database
66a50 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
66a60 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73  ITE_IgnoreChecks
66a70 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f     0x00002000  /
66a80 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65  * Do not enforce
66a90 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e   check constrain
66aa0 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ts */.#define SQ
66ab0 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
66ac0 74 74 65 64 20 30 78 30 30 30 30 34 30 30 30 20  tted 0x00004000 
66ad0 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61  /* For shared-ca
66ae0 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66  che mode */.#def
66af0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63  ine SQLITE_Legac
66b00 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30  yFileFmt  0x0000
66b10 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20  8000  /* Create 
66b20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 69 6e  new databases in
66b30 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65   format 1 */.#de
66b40 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c  fine SQLITE_Full
66b50 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 30 30  FSync      0x000
66b60 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75  10000  /* Use fu
66b70 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20  ll fsync on the 
66b80 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69  backend */.#defi
66b90 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78  ne SQLITE_LoadEx
66ba0 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30  tension  0x00020
66bb0 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c  000  /* Enable l
66bc0 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f  oad_extension */
66bd0 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
66be0 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20  _RecoveryMode   
66bf0 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49  0x00040000  /* I
66c00 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72  gnore schema err
66c10 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ors */.#define S
66c20 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68  QLITE_SharedCach
66c30 65 20 20 20 20 30 78 30 30 30 38 30 30 30 30 20  e    0x00080000 
66c40 20 2f 2a 20 43 61 63 68 65 20 73 68 61 72 69 6e   /* Cache sharin
66c50 67 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a  g is enabled */.
66c60 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56  #define SQLITE_V
66c70 74 61 62 20 20 20 20 20 20 20 20 20 20 20 30 78  tab           0x
66c80 30 30 31 30 30 30 30 30 20 20 2f 2a 20 54 68 65  00100000  /* The
66c90 72 65 20 65 78 69 73 74 73 20 61 20 76 69 72 74  re exists a virt
66ca0 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65  ual table */.#de
66cb0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f 6d 6d  fine SQLITE_Comm
66cc0 69 74 42 75 73 79 20 20 20 20 20 30 78 30 30 32  itBusy     0x002
66cd0 30 30 30 30 30 20 20 2f 2a 20 49 6e 20 74 68 65  00000  /* In the
66ce0 20 70 72 6f 63 65 73 73 20 6f 66 20 63 6f 6d 6d   process of comm
66cf0 69 74 74 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  itting */../*.**
66d00 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73   Possible values
66d10 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e   for the sqlite.
66d20 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20  magic field..** 
66d30 54 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20  The numbers are 
66d40 6f 62 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64  obtained at rand
66d50 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73  om and have no s
66d60 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20  pecial meaning, 
66d70 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65  other.** than be
66d80 69 6e 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f  ing distinct fro
66d90 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a  m one another..*
66da0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
66db0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 20 20  _MAGIC_OPEN     
66dc0 30 78 61 30 32 39 61 36 39 37 20 20 2f 2a 20 44  0xa029a697  /* D
66dd0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20  atabase is open 
66de0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
66df0 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20 20  E_MAGIC_CLOSED  
66e00 20 30 78 39 66 33 63 32 64 33 33 20 20 2f 2a 20   0x9f3c2d33  /* 
66e10 44 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73  Database is clos
66e20 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ed */.#define SQ
66e30 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20  LITE_MAGIC_SICK 
66e40 20 20 20 20 30 78 34 62 37 37 31 32 39 30 20 20      0x4b771290  
66e50 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20 61 77 61  /* Error and awa
66e60 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23  iting close */.#
66e70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
66e80 47 49 43 5f 42 55 53 59 20 20 20 20 20 30 78 66  GIC_BUSY     0xf
66e90 30 33 62 37 39 30 36 20 20 2f 2a 20 44 61 74 61  03b7906  /* Data
66ea0 62 61 73 65 20 63 75 72 72 65 6e 74 6c 79 20 69  base currently i
66eb0 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65  n use */.#define
66ec0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52   SQLITE_MAGIC_ER
66ed0 52 4f 52 20 20 20 20 30 78 62 35 33 35 37 39 33  ROR    0xb535793
66ee0 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f  0  /* An SQLITE_
66ef0 4d 49 53 55 53 45 20 65 72 72 6f 72 20 6f 63 63  MISUSE error occ
66f00 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  urred */../*.** 
66f10 45 61 63 68 20 53 51 4c 20 66 75 6e 63 74 69 6f  Each SQL functio
66f20 6e 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20  n is defined by 
66f30 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
66f40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
66f50 73 74 72 75 63 74 75 72 65 2e 20 20 41 20 70 6f  structure.  A po
66f60 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74  inter to this st
66f70 72 75 63 74 75 72 65 20 69 73 20 73 74 6f 72 65  ructure is store
66f80 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e  d in the sqlite.
66f90 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61  aFunc.** hash ta
66fa0 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69  ble.  When multi
66fb0 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20 68 61  ple functions ha
66fc0 76 65 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  ve the same name
66fd0 2c 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  , the hash table
66fe0 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  .** points to a 
66ff0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74  linked list of t
67000 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e  hese structures.
67010 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44  .*/.struct FuncD
67020 65 66 20 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b  ef {.  i16 nArg;
67030 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
67040 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
67050 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e  ts.  -1 means un
67060 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75 38 20  limited */.  u8 
67070 69 50 72 65 66 45 6e 63 3b 20 20 20 20 20 20 20  iPrefEnc;       
67080 20 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74    /* Preferred t
67090 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 53 51  ext encoding (SQ
670a0 4c 49 54 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c  LITE_UTF8, 16LE,
670b0 20 31 36 42 45 29 20 2a 2f 0a 20 20 75 38 20 66   16BE) */.  u8 f
670c0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
670d0 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61   /* Some combina
670e0 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46  tion of SQLITE_F
670f0 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20  UNC_* */.  void 
67100 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20 20 20  *pUserData;     
67110 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70 61 72  /* User data par
67120 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63  ameter */.  Func
67130 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  Def *pNext;     
67140 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 69 6f   /* Next functio
67150 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61 6d 65  n with same name
67160 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 75   */.  void (*xFu
67170 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
67180 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
67190 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65  _value**); /* Re
671a0 67 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a  gular function *
671b0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70  /.  void (*xStep
671c0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
671d0 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
671e0 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72  alue**); /* Aggr
671f0 65 67 61 74 65 20 73 74 65 70 20 2a 2f 0a 20 20  egate step */.  
67200 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65  void (*xFinalize
67210 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
67220 74 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  t*);            
67230 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65      /* Aggregate
67240 20 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20   finalizer */.  
67250 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
67260 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65       /* SQL name
67270 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
67280 2e 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a  . */.  FuncDef *
67290 70 48 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e  pHash;      /* N
672a0 65 78 74 20 77 69 74 68 20 61 20 64 69 66 66 65  ext with a diffe
672b0 72 65 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68  rent name but th
672c0 65 20 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d  e same hash */.}
672d0 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c  ;../*.** Possibl
672e0 65 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e  e values for Fun
672f0 63 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64  cDef.flags.*/.#d
67300 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
67310 43 5f 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20  C_LIKE     0x01 
67320 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72  /* Candidate for
67330 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69   the LIKE optimi
67340 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  zation */.#defin
67350 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41  e SQLITE_FUNC_CA
67360 53 45 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43  SE     0x02 /* C
67370 61 73 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49  ase-sensitive LI
67380 4b 45 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e  KE-type function
67390 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
673a0 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20  TE_FUNC_EPHEM   
673b0 20 30 78 30 34 20 2f 2a 20 45 70 68 65 6d 65 72   0x04 /* Ephemer
673c0 61 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68  al.  Delete with
673d0 20 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65   VDBE */.#define
673e0 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45   SQLITE_FUNC_NEE
673f0 44 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71  DCOLL 0x08 /* sq
67400 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c  lite3GetFuncColl
67410 53 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63  Seq() might be c
67420 61 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  alled */.#define
67430 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49   SQLITE_FUNC_PRI
67440 56 41 54 45 20 20 30 78 31 30 20 2f 2a 20 41 6c  VATE  0x10 /* Al
67450 6c 6f 77 65 64 20 66 6f 72 20 69 6e 74 65 72 6e  lowed for intern
67460 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 0a  al use only */..
67470 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
67480 69 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f 73  ing three macros
67490 2c 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c 49  , FUNCTION(), LI
674a0 4b 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47 47  KEFUNC() and AGG
674b0 52 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a 20  REGATE() are.** 
674c0 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74  used to create t
674d0 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 20  he initializers 
674e0 66 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66 20  for the FuncDef 
674f0 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a  structures..**.*
67500 2a 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61  *   FUNCTION(zNa
67510 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20  me, nArg, iArg, 
67520 62 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20 20  bNC, xFunc).**  
67530 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74     Used to creat
67540 65 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74  e a scalar funct
67550 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f  ion definition o
67560 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61  f a function zNa
67570 6d 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c 65  me .**     imple
67580 6d 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e 63  mented by C func
67590 74 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74 20  tion xFunc that 
675a0 61 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67  accepts nArg arg
675b0 75 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20 20  uments. The.**  
675c0 20 20 20 76 61 6c 75 65 20 70 61 73 73 65 64 20     value passed 
675d0 61 73 20 69 41 72 67 20 69 73 20 63 61 73 74 20  as iArg is cast 
675e0 74 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e 64  to a (void*) and
675f0 20 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 0a   made available.
67600 2a 2a 20 20 20 20 20 61 73 20 74 68 65 20 75 73  **     as the us
67610 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33  er-data (sqlite3
67620 5f 75 73 65 72 5f 64 61 74 61 28 29 29 20 66 6f  _user_data()) fo
67630 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20  r the function. 
67640 49 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75 6d  If .**     argum
67650 65 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65 2c  ent bNC is true,
67660 20 74 68 65 6e 20 74 68 65 20 46 75 6e 63 44 65   then the FuncDe
67670 66 2e 6e 65 65 64 43 6f 6c 6c 61 74 65 20 66 6c  f.needCollate fl
67680 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a  ag is set..**.**
67690 20 20 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61     AGGREGATE(zNa
676a0 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20  me, nArg, iArg, 
676b0 62 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 69 6e  bNC, xStep, xFin
676c0 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20  al).**     Used 
676d0 74 6f 20 63 72 65 61 74 65 20 61 6e 20 61 67 67  to create an agg
676e0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
676f0 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 6c 65  definition imple
67700 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20 20 20  mented by.**    
67710 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
67720 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61   xStep and xFina
67730 6c 2e 20 54 68 65 20 66 69 72 73 74 20 66 6f 75  l. The first fou
67740 72 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  r parameters.** 
67750 20 20 20 20 61 72 65 20 69 6e 74 65 72 70 72 65      are interpre
67760 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ted in the same 
67770 77 61 79 20 61 73 20 74 68 65 20 66 69 72 73 74  way as the first
67780 20 34 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f   4 parameters to
67790 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54 49 4f 4e  .**     FUNCTION
677a0 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45  ()..**.**   LIKE
677b0 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67  FUNC(zName, nArg
677c0 2c 20 70 41 72 67 2c 20 66 6c 61 67 73 29 0a 2a  , pArg, flags).*
677d0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72  *     Used to cr
677e0 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75  eate a scalar fu
677f0 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
67800 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20  n of a function 
67810 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 74 68  zName .**     th
67820 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20  at accepts nArg 
67830 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 69 73  arguments and is
67840 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
67850 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20  a call to C .** 
67860 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b      function lik
67870 65 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e 74 20  eFunc. Argument 
67880 70 41 72 67 20 69 73 20 63 61 73 74 20 74 6f 20  pArg is cast to 
67890 61 20 28 76 6f 69 64 20 2a 29 20 61 6e 64 20 6d  a (void *) and m
678a0 61 64 65 0a 2a 2a 20 20 20 20 20 61 76 61 69 6c  ade.**     avail
678b0 61 62 6c 65 20 61 73 20 74 68 65 20 66 75 6e 63  able as the func
678c0 74 69 6f 6e 20 75 73 65 72 2d 64 61 74 61 20 28  tion user-data (
678d0 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
678e0 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20 20 20  a()). The.**    
678f0 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 20 76   FuncDef.flags v
67900 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74  ariable is set t
67910 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73  o the value pass
67920 65 64 20 61 73 20 74 68 65 20 66 6c 61 67 73 0a  ed as the flags.
67930 2a 2a 20 20 20 20 20 70 61 72 61 6d 65 74 65 72  **     parameter
67940 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 55 4e  ..*/.#define FUN
67950 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72  CTION(zName, nAr
67960 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46  g, iArg, bNC, xF
67970 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20  unc) \.  {nArg, 
67980 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43  SQLITE_UTF8, bNC
67990 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  *8, SQLITE_INT_T
679a0 4f 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20  O_PTR(iArg), 0, 
679b0 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e  xFunc, 0, 0, #zN
679c0 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20  ame, 0}.#define 
679d0 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61  STR_FUNCTION(zNa
679e0 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20  me, nArg, pArg, 
679f0 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20  bNC, xFunc) \.  
67a00 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54  {nArg, SQLITE_UT
67a10 46 38 2c 20 62 4e 43 2a 38 2c 20 70 41 72 67 2c  F8, bNC*8, pArg,
67a20 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c   0, xFunc, 0, 0,
67a30 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66   #zName, 0}.#def
67a40 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61  ine LIKEFUNC(zNa
67a50 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 66  me, nArg, arg, f
67a60 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72 67 2c  lags) \.  {nArg,
67a70 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 66 6c   SQLITE_UTF8, fl
67a80 61 67 73 2c 20 28 76 6f 69 64 20 2a 29 61 72 67  ags, (void *)arg
67a90 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30  , 0, likeFunc, 0
67aa0 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a  , 0, #zName, 0}.
67ab0 23 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54  #define AGGREGAT
67ac0 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61  E(zName, nArg, a
67ad0 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78  rg, nc, xStep, x
67ae0 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67  Final) \.  {nArg
67af0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e  , SQLITE_UTF8, n
67b00 63 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f  c*8, SQLITE_INT_
67b10 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20  TO_PTR(arg), 0, 
67b20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c  0, xStep,xFinal,
67b30 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a 2a  #zName,0}../*.**
67b40 20 41 6c 6c 20 63 75 72 72 65 6e 74 20 73 61 76   All current sav
67b50 65 70 6f 69 6e 74 73 20 61 72 65 20 73 74 6f 72  epoints are stor
67b60 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c  ed in a linked l
67b70 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 0a  ist starting at.
67b80 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65  ** sqlite3.pSave
67b90 70 6f 69 6e 74 2e 20 54 68 65 20 66 69 72 73 74  point. The first
67ba0 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20   element in the 
67bb0 6c 69 73 74 20 69 73 20 74 68 65 20 6d 6f 73 74  list is the most
67bc0 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70 65   recently.** ope
67bd0 6e 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 53  ned savepoint. S
67be0 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 61 64  avepoints are ad
67bf0 64 65 64 20 74 6f 20 74 68 65 20 6c 69 73 74 20  ded to the list 
67c00 62 79 20 74 68 65 20 76 64 62 65 0a 2a 2a 20 4f  by the vdbe.** O
67c10 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74  P_Savepoint inst
67c20 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75  ruction..*/.stru
67c30 63 74 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20  ct Savepoint {. 
67c40 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
67c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67c60 20 20 20 20 20 2f 2a 20 53 61 76 65 70 6f 69 6e       /* Savepoin
67c70 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72 6d  t name (nul-term
67c80 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20 53 61 76  inated) */.  Sav
67c90 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20  epoint *pNext;  
67ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67cb0 20 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70   /* Parent savep
67cc0 6f 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f  oint (if any) */
67cd0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .};../*.** The f
67ce0 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65  ollowing are use
67cf0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
67d00 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
67d10 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c  ite3Savepoint(),
67d20 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50  .** and as the P
67d30 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  1 argument to th
67d40 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69  e OP_Savepoint i
67d50 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23  nstruction..*/.#
67d60 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54  define SAVEPOINT
67d70 5f 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64  _BEGIN      0.#d
67d80 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f  efine SAVEPOINT_
67d90 52 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65  RELEASE    1.#de
67da0 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52  fine SAVEPOINT_R
67db0 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a  OLLBACK   2.../*
67dc0 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20  .** Each SQLite 
67dd0 6d 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20  module (virtual 
67de0 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e  table definition
67df0 29 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20  ) is defined by 
67e00 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
67e10 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
67e20 73 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65  structure, store
67e30 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  d in the sqlite3
67e40 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68  .aModule.** hash
67e50 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63   table..*/.struc
67e60 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e  t Module {.  con
67e70 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
67e80 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20  e *pModule;     
67e90 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f    /* Callback po
67ea0 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73  inters */.  cons
67eb0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
67ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67ed0 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20   /* Name passed 
67ee0 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  to create_module
67ef0 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  () */.  void *pA
67f00 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ux;             
67f10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67f20 70 41 75 78 20 70 61 73 73 65 64 20 74 6f 20 63  pAux passed to c
67f30 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a  reate_module() *
67f40 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74  /.  void (*xDest
67f50 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20  roy)(void *);   
67f60 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75           /* Modu
67f70 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75  le destructor fu
67f80 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  nction */.};../*
67f90 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
67fa0 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d  about each colum
67fb0 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c  n of an SQL tabl
67fc0 65 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20  e is held in an 
67fd0 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74  instance.** of t
67fe0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
67ff0 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20  /.struct Column 
68000 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  {.  char *zName;
68010 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
68020 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  this column */. 
68030 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20   Expr *pDflt;   
68040 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c    /* Default val
68050 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d  ue of this colum
68060 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79  n */.  char *zTy
68070 70 65 3b 20 20 20 20 20 2f 2a 20 44 61 74 61 20  pe;     /* Data 
68080 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 63 6f  type for this co
68090 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  lumn */.  char *
680a0 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f  zColl;     /* Co
680b0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
680c0 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20  .  If NULL, use 
680d0 74 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20  the default */. 
680e0 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20   u8 notNull;    
680f0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
68100 72 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c  re is a NOT NULL
68110 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20   constraint */. 
68120 20 75 38 20 69 73 50 72 69 6d 4b 65 79 3b 20 20   u8 isPrimKey;  
68130 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
68140 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74  s column is part
68150 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20   of the PRIMARY 
68160 4b 45 59 20 2a 2f 0a 20 20 63 68 61 72 20 61 66  KEY */.  char af
68170 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65  finity;   /* One
68180 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41   of the SQLITE_A
68190 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f  FF_... values */
681a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
681b0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
681c0 45 0a 20 20 75 38 20 69 73 48 69 64 64 65 6e 3b  E.  u8 isHidden;
681d0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
681e0 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27  this column is '
681f0 68 69 64 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69  hidden' */.#endi
68200 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43  f.};../*.** A "C
68210 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e 63  ollating Sequenc
68220 65 22 20 69 73 20 64 65 66 69 6e 65 64 20 62 79  e" is defined by
68230 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
68240 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
68250 20 73 74 72 75 63 74 75 72 65 2e 20 43 6f 6e 63   structure. Conc
68260 65 70 74 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c  eptually, a coll
68270 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63  ating sequence c
68280 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 6e 61 6d  onsists of a nam
68290 65 20 61 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61  e and.** a compa
682a0 72 69 73 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68  rison routine th
682b0 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6f  at defines the o
682c0 72 64 65 72 20 6f 66 20 74 68 61 74 20 73 65 71  rder of that seq
682d0 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  uence..**.** The
682e0 72 65 20 6d 61 79 20 74 77 6f 20 73 65 70 61 72  re may two separ
682f0 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ate implementati
68300 6f 6e 73 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61  ons of the colla
68310 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f  tion function, o
68320 6e 65 0a 2a 2a 20 74 68 61 74 20 70 72 6f 63 65  ne.** that proce
68330 73 73 65 73 20 74 65 78 74 20 69 6e 20 55 54 46  sses text in UTF
68340 2d 38 20 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c  -8 encoding (Col
68350 6c 53 65 71 2e 78 43 6d 70 29 20 61 6e 64 20 61  lSeq.xCmp) and a
68360 6e 6f 74 68 65 72 20 74 68 61 74 0a 2a 2a 20 70  nother that.** p
68370 72 6f 63 65 73 73 65 73 20 74 65 78 74 20 65 6e  rocesses text en
68380 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 31 36 20  coded in UTF-16 
68390 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29  (CollSeq.xCmp16)
683a0 2c 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 68  , using the mach
683b0 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79  ine.** native by
683c0 74 65 20 6f 72 64 65 72 2e 20 57 68 65 6e 20 61  te order. When a
683d0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
683e0 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20  nce is invoked, 
683f0 53 51 4c 69 74 65 20 73 65 6c 65 63 74 73 0a 2a  SQLite selects.*
68400 2a 20 74 68 65 20 76 65 72 73 69 6f 6e 20 74 68  * the version th
68410 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20  at will require 
68420 74 68 65 20 6c 65 61 73 74 20 65 78 70 65 6e 73  the least expens
68430 69 76 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  ive encoding.** 
68440 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66  translations, if
68450 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   any..**.** The 
68460 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 20 6d 65  CollSeq.pUser me
68470 6d 62 65 72 20 76 61 72 69 61 62 6c 65 20 69 73  mber variable is
68480 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d 65   an extra parame
68490 74 65 72 20 74 68 61 74 20 70 61 73 73 65 64 20  ter that passed 
684a0 69 6e 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72  in.** as the fir
684b0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
684c0 68 65 20 55 54 46 2d 38 20 63 6f 6d 70 61 72 69  he UTF-8 compari
684d0 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43  son function, xC
684e0 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70  mp..** CollSeq.p
684f0 55 73 65 72 31 36 20 69 73 20 74 68 65 20 65 71  User16 is the eq
68500 75 69 76 61 6c 65 6e 74 20 66 6f 72 20 74 68 65  uivalent for the
68510 20 55 54 46 2d 31 36 20 63 6f 6d 70 61 72 69 73   UTF-16 comparis
68520 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20  on function,.** 
68530 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66  xCmp16..**.** If
68540 20 62 6f 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43   both CollSeq.xC
68550 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78  mp and CollSeq.x
68560 43 6d 70 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20  Cmp16 are NULL, 
68570 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
68580 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73  e.** collating s
68590 65 71 75 65 6e 63 65 20 69 73 20 75 6e 64 65 66  equence is undef
685a0 69 6e 65 64 2e 20 20 49 6e 64 69 63 65 73 20 62  ined.  Indices b
685b0 75 69 6c 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66  uilt on an undef
685c0 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e  ined.** collatin
685d0 67 20 73 65 71 75 65 6e 63 65 20 6d 61 79 20 6e  g sequence may n
685e0 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72  ot be read or wr
685f0 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  itten..*/.struct
68600 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61   CollSeq {.  cha
68610 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
68620 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
68630 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
68640 65 6e 63 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f  ence, UTF-8 enco
68650 64 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b  ded */.  u8 enc;
68660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
68670 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  * Text encoding 
68680 68 61 6e 64 6c 65 64 20 62 79 20 78 43 6d 70 28  handled by xCmp(
68690 29 20 2a 2f 0a 20 20 75 38 20 74 79 70 65 3b 20  ) */.  u8 type; 
686a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
686b0 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54  One of the SQLIT
686c0 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65  E_COLL_... value
686d0 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69  s below */.  voi
686e0 64 20 2a 70 55 73 65 72 3b 20 20 20 20 20 20 20  d *pUser;       
686f0 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
68700 6d 65 6e 74 20 74 6f 20 78 43 6d 70 28 29 20 2a  ment to xCmp() *
68710 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29 28  /.  int (*xCmp)(
68720 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74  void*,int, const
68730 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e   void*, int, con
68740 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  st void*);.  voi
68750 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
68760 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72  ;  /* Destructor
68770 20 66 6f 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b   for pUser */.};
68780 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ../*.** Allowed 
68790 76 61 6c 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65  values of CollSe
687a0 71 2e 74 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69  q.type:.*/.#defi
687b0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42  ne SQLITE_COLL_B
687c0 49 4e 41 52 59 20 20 31 20 20 2f 2a 20 54 68 65  INARY  1  /* The
687d0 20 64 65 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28   default memcmp(
687e0 29 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  ) collating sequ
687f0 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ence */.#define 
68800 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41  SQLITE_COLL_NOCA
68810 53 45 20 20 32 20 20 2f 2a 20 54 68 65 20 62 75  SE  2  /* The bu
68820 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f  ilt-in NOCASE co
68830 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
68840 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
68850 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20  TE_COLL_REVERSE 
68860 33 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d  3  /* The built-
68870 69 6e 20 52 45 56 45 52 53 45 20 63 6f 6c 6c 61  in REVERSE colla
68880 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
68890 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
688a0 43 4f 4c 4c 5f 55 53 45 52 20 20 20 20 30 20 20  COLL_USER    0  
688b0 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20 75 73 65  /* Any other use
688c0 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74  r-defined collat
688d0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ing sequence */.
688e0 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72  ./*.** A sort or
688f0 64 65 72 20 63 61 6e 20 62 65 20 65 69 74 68 65  der can be eithe
68900 72 20 41 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a  r ASC or DESC..*
68910 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
68920 5f 53 4f 5f 41 53 43 20 20 20 20 20 20 20 30 20  _SO_ASC       0 
68930 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65   /* Sort in asce
68940 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23  nding order */.#
68950 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f  define SQLITE_SO
68960 5f 44 45 53 43 20 20 20 20 20 20 31 20 20 2f 2a  _DESC      1  /*
68970 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69   Sort in ascendi
68980 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a  ng order */../*.
68990 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69  ** Column affini
689a0 74 79 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20  ty types..**.** 
689b0 54 68 65 73 65 20 75 73 65 64 20 74 6f 20 68 61  These used to ha
689c0 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65  ve mnemonic name
689d0 20 6c 69 6b 65 20 27 69 27 20 66 6f 72 20 53 51   like 'i' for SQ
689e0 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
689f0 20 61 6e 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20   and.** 't' for 
68a00 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2e  SQLITE_AFF_TEXT.
68a10 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 61 76    But we can sav
68a20 65 20 61 20 6c 69 74 74 6c 65 20 73 70 61 63 65  e a little space
68a30 20 61 6e 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20   and improve.** 
68a40 74 68 65 20 73 70 65 65 64 20 61 20 6c 69 74 74  the speed a litt
68a50 6c 65 20 62 79 20 6e 75 6d 62 65 72 69 6e 67 20  le by numbering 
68a60 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 73 65  the values conse
68a70 63 75 74 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a  cutively.  .**.*
68a80 2a 20 42 75 74 20 72 61 74 68 65 72 20 74 68 61  * But rather tha
68a90 6e 20 73 74 61 72 74 20 77 69 74 68 20 30 20 6f  n start with 0 o
68aa0 72 20 31 2c 20 77 65 20 62 65 67 69 6e 20 77 69  r 1, we begin wi
68ab0 74 68 20 27 61 27 2e 20 20 54 68 61 74 20 77 61  th 'a'.  That wa
68ac0 79 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69  y,.** when multi
68ad0 70 6c 65 20 61 66 66 69 6e 69 74 79 20 74 79 70  ple affinity typ
68ae0 65 73 20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61  es are concatena
68af0 74 65 64 20 69 6e 74 6f 20 61 20 73 74 72 69 6e  ted into a strin
68b00 67 20 61 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73  g and.** used as
68b10 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 2c   the P4 operand,
68b20 20 74 68 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f   they will be mo
68b30 72 65 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a  re readable..**.
68b40 2a 2a 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61  ** Note also tha
68b50 74 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79  t the numeric ty
68b60 70 65 73 20 61 72 65 20 67 72 6f 75 70 65 64 20  pes are grouped 
68b70 74 6f 67 65 74 68 65 72 20 73 6f 20 74 68 61 74  together so that
68b80 20 74 65 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20   testing.** for 
68b90 61 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 69  a numeric type i
68ba0 73 20 61 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61  s a single compa
68bb0 72 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e  rison..*/.#defin
68bc0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  e SQLITE_AFF_TEX
68bd0 54 20 20 20 20 20 27 61 27 0a 23 64 65 66 69 6e  T     'a'.#defin
68be0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  e SQLITE_AFF_NON
68bf0 45 20 20 20 20 20 27 62 27 0a 23 64 65 66 69 6e  E     'b'.#defin
68c00 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  e SQLITE_AFF_NUM
68c10 45 52 49 43 20 20 27 63 27 0a 23 64 65 66 69 6e  ERIC  'c'.#defin
68c20 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  e SQLITE_AFF_INT
68c30 45 47 45 52 20 20 27 64 27 0a 23 64 65 66 69 6e  EGER  'd'.#defin
68c40 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  e SQLITE_AFF_REA
68c50 4c 20 20 20 20 20 27 65 27 0a 0a 23 64 65 66 69  L     'e'..#defi
68c60 6e 65 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65  ne sqlite3IsNume
68c70 72 69 63 41 66 66 69 6e 69 74 79 28 58 29 20 20  ricAffinity(X)  
68c80 28 28 58 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46  ((X)>=SQLITE_AFF
68c90 5f 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a  _NUMERIC)../*.**
68ca0 20 54 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f   The SQLITE_AFF_
68cb0 4d 41 53 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b  MASK values mask
68cc0 73 20 6f 66 66 20 74 68 65 20 73 69 67 6e 69 66  s off the signif
68cd0 69 63 61 6e 74 20 62 69 74 73 20 6f 66 20 61 6e  icant bits of an
68ce0 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 76 61 6c  .** affinity val
68cf0 75 65 2e 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ue. .*/.#define 
68d00 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20  SQLITE_AFF_MASK 
68d10 20 20 20 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20      0x67../*.** 
68d20 41 64 64 69 74 69 6f 6e 61 6c 20 62 69 74 20 76  Additional bit v
68d30 61 6c 75 65 73 20 74 68 61 74 20 63 61 6e 20 62  alues that can b
68d40 65 20 4f 52 65 64 20 77 69 74 68 20 61 6e 20 61  e ORed with an a
68d50 66 66 69 6e 69 74 79 20 77 69 74 68 6f 75 74 0a  ffinity without.
68d60 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20  ** changing the 
68d70 61 66 66 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65  affinity..*/.#de
68d80 66 69 6e 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50  fine SQLITE_JUMP
68d90 49 46 4e 55 4c 4c 20 20 20 30 78 30 38 20 20 2f  IFNULL   0x08  /
68da0 2a 20 6a 75 6d 70 73 20 69 66 20 65 69 74 68 65  * jumps if eithe
68db0 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c  r operand is NUL
68dc0 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  L */.#define SQL
68dd0 49 54 45 5f 53 54 4f 52 45 50 32 20 20 20 20 20  ITE_STOREP2     
68de0 20 30 78 31 30 20 20 2f 2a 20 53 74 6f 72 65 20   0x10  /* Store 
68df0 72 65 73 75 6c 74 20 69 6e 20 72 65 67 5b 50 32  result in reg[P2
68e00 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 6a 75  ] rather than ju
68e10 6d 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  mp */../*.** Eac
68e20 68 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20 72  h SQL table is r
68e30 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65  epresented in me
68e40 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e 73 74 61  mory by an insta
68e50 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f  nce of the.** fo
68e60 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
68e70 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a  e..**.** Table.z
68e80 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  Name is the name
68e90 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20   of the table.  
68ea0 54 68 65 20 63 61 73 65 20 6f 66 20 74 68 65 20  The case of the 
68eb0 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 52 45 41  original.** CREA
68ec0 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
68ed0 6e 74 20 69 73 20 73 74 6f 72 65 64 2c 20 62 75  nt is stored, bu
68ee0 74 20 63 61 73 65 20 69 73 20 6e 6f 74 20 73 69  t case is not si
68ef0 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 0a 2a 2a  gnificant for.**
68f00 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a   comparisons..**
68f10 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f 6c 20 69  .** Table.nCol i
68f20 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
68f30 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20  columns in this 
68f40 74 61 62 6c 65 2e 20 20 54 61 62 6c 65 2e 61 43  table.  Table.aC
68f50 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74  ol is a.** point
68f60 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f  er to an array o
68f70 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75 63 74 75  f Column structu
68f80 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20 65 61 63  res, one for eac
68f90 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  h column..**.** 
68fa0 49 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 73  If the table has
68fb0 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
68fc0 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 54 61  ARY KEY, then Ta
68fd0 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 74 68 65  ble.iPKey is the
68fe0 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65   index of.** the
68ff0 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20   column that is 
69000 74 68 61 74 20 6b 65 79 2e 20 20 20 4f 74 68 65  that key.   Othe
69010 72 77 69 73 65 20 54 61 62 6c 65 2e 69 50 4b 65  rwise Table.iPKe
69020 79 20 69 73 20 6e 65 67 61 74 69 76 65 2e 20 20  y is negative.  
69030 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20 74 68 65  Note.** that the
69040 20 64 61 74 61 74 79 70 65 20 6f 66 20 74 68 65   datatype of the
69050 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73   PRIMARY KEY mus
69060 74 20 62 65 20 49 4e 54 45 47 45 52 20 66 6f 72  t be INTEGER for
69070 20 74 68 69 73 20 66 69 65 6c 64 20 74 6f 0a 2a   this field to.*
69080 2a 20 62 65 20 73 65 74 2e 20 20 41 6e 20 49 4e  * be set.  An IN
69090 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
690a0 59 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65  Y is used as the
690b0 20 72 6f 77 69 64 20 66 6f 72 20 65 61 63 68 20   rowid for each 
690c0 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61  row of.** the ta
690d0 62 6c 65 2e 20 20 49 66 20 61 20 74 61 62 6c 65  ble.  If a table
690e0 20 68 61 73 20 6e 6f 20 49 4e 54 45 47 45 52 20   has no INTEGER 
690f0 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65  PRIMARY KEY, the
69100 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64  n a random rowid
69110 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61 74 65 64  .** is generated
69120 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66   for each row of
69130 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 46 5f   the table.  TF_
69140 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 69 73  HasPrimaryKey is
69150 20 73 65 74 20 69 66 0a 2a 2a 20 74 68 65 20 74   set if.** the t
69160 61 62 6c 65 20 68 61 73 20 61 6e 79 20 50 52 49  able has any PRI
69170 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54 45 47 45  MARY KEY, INTEGE
69180 52 20 6f 72 20 6f 74 68 65 72 77 69 73 65 2e 0a  R or otherwise..
69190 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74 6e 75 6d  **.** Table.tnum
691a0 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d   is the page num
691b0 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74  ber for the root
691c0 20 42 54 72 65 65 20 70 61 67 65 20 6f 66 20 74   BTree page of t
691d0 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 0a  he table in the.
691e0 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
691f0 2e 20 20 49 66 20 54 61 62 6c 65 2e 69 44 62 20  .  If Table.iDb 
69200 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
69210 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62  the database tab
69220 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e  le backend.** in
69230 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d 2e 20 20   sqlite.aDb[].  
69240 30 20 69 73 20 66 6f 72 20 74 68 65 20 6d 61 69  0 is for the mai
69250 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 31  n database and 1
69260 20 69 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65   is for the file
69270 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64 73 20 74   that.** holds t
69280 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20  emporary tables 
69290 61 6e 64 20 69 6e 64 69 63 65 73 2e 20 20 49 66  and indices.  If
692a0 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 69 73   TF_Ephemeral is
692b0 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20 74 68 65   set.** then the
692c0 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64   table is stored
692d0 20 69 6e 20 61 20 66 69 6c 65 20 74 68 61 74 20   in a file that 
692e0 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
692f0 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77 68 65 6e   deleted.** when
69300 20 74 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   the VDBE cursor
69310 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 69 73   to the table is
69320 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20 74 68 69   closed.  In thi
69330 73 20 63 61 73 65 20 54 61 62 6c 65 2e 74 6e 75  s case Table.tnu
69340 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20 56 44 42  m .** refers VDB
69350 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
69360 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 74  that holds the t
69370 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 74  able open, not t
69380 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61  o the root.** pa
69390 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e  ge number.  Tran
693a0 73 69 65 6e 74 20 74 61 62 6c 65 73 20 61 72 65  sient tables are
693b0 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68   used to hold th
693c0 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61 0a 2a  e results of a.*
693d0 2a 20 73 75 62 2d 71 75 65 72 79 20 74 68 61 74  * sub-query that
693e0 20 61 70 70 65 61 72 73 20 69 6e 73 74 65 61 64   appears instead
693f0 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65   of a real table
69400 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f   name in the FRO
69410 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 20  M clause .** of 
69420 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
69430 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61  nt..*/.struct Ta
69440 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20  ble {.  sqlite3 
69450 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  *db;         /* 
69460 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62  Associated datab
69470 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ase connection. 
69480 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 2e 20   Might be NULL. 
69490 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  */.  char *zName
694a0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ;         /* Nam
694b0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f  e of the table o
694c0 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20  r view */.  int 
694d0 69 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20  iPKey;          
694e0 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61 74   /* If not negat
694f0 69 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50  ive, use aCol[iP
69500 4b 65 79 5d 20 61 73 20 74 68 65 20 70 72 69 6d  Key] as the prim
69510 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74  ary key */.  int
69520 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   nCol;          
69530 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
69540 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74  olumns in this t
69550 61 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e  able */.  Column
69560 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f   *aCol;        /
69570 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  * Information ab
69580 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  out each column 
69590 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  */.  Index *pInd
695a0 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  ex;       /* Lis
695b0 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 73  t of SQL indexes
695c0 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20   on this table. 
695d0 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20  */.  int tnum;  
695e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f            /* Roo
695f0 74 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f 72  t BTree node for
69600 20 74 68 69 73 20 74 61 62 6c 65 20 28 73 65 65   this table (see
69610 20 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a   note above) */.
69620 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
69630 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66  t;     /* NULL f
69640 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e  or tables.  Poin
69650 74 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e  ts to definition
69660 20 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20   if a view. */. 
69670 20 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20   u16 nRef;      
69680 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
69690 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  of pointers to t
696a0 68 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75  his Table */.  u
696b0 38 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20 20  8 tabFlags;     
696c0 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54      /* Mask of T
696d0 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  F_* values */.  
696e0 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20  u8 keyConf;     
696f0 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20       /* What to 
69700 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75 6e  do in case of un
69710 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63  iqueness conflic
69720 74 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20  t on iPKey */.  
69730 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
69740 72 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  r;   /* List of 
69750 53 51 4c 20 74 72 69 67 67 65 72 73 20 6f 6e 20  SQL triggers on 
69760 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
69770 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20  FKey *pFKey;    
69780 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c       /* Linked l
69790 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69  ist of all forei
697a0 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73 20  gn keys in this 
697b0 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20  table */.  char 
697c0 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20 20  *zColAff;       
697d0 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69  /* String defini
697e0 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  ng the affinity 
697f0 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a  of each column *
69800 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
69810 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45 78  _OMIT_CHECK.  Ex
69820 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20 20  pr *pCheck;     
69830 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66     /* The AND of
69840 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74   all CHECK const
69850 72 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66  raints */.#endif
69860 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
69870 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a  OMIT_ALTERTABLE.
69880 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73    int addColOffs
69890 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74  et;    /* Offset
698a0 20 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c 45   in CREATE TABLE
698b0 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20 6e   stmt to add a n
698c0 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e  ew column */.#en
698d0 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
698e0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
698f0 41 42 4c 45 0a 20 20 4d 6f 64 75 6c 65 20 2a 70  ABLE.  Module *p
69900 4d 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  Mod;        /* P
69910 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69 6d  ointer to the im
69920 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
69930 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
69940 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
69950 74 61 62 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 20  tab; /* Pointer 
69960 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e  to the module in
69970 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20  stance */.  int 
69980 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20 20  nModuleArg;     
69990 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
699a0 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 6d  guments to the m
699b0 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20  odule */.  char 
699c0 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20  **azModuleArg;  
699d0 2f 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d  /* Text of all m
699e0 6f 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20  odule args. [0] 
699f0 69 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a  is module name *
69a00 2f 0a 23 65 6e 64 69 66 0a 20 20 53 63 68 65 6d  /.#endif.  Schem
69a10 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20  a *pSchema;     
69a20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20 63  /* Schema that c
69a30 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61 62  ontains this tab
69a40 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  le */.  Table *p
69a50 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20  NextZombie;  /* 
69a60 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72 73  Next on the Pars
69a70 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73  e.pZombieTab lis
69a80 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t */.};../*.** A
69a90 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f  llowed values fo
69aa0 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73 2e  r Tabe.tabFlags.
69ab0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 52  .*/.#define TF_R
69ac0 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20 30  eadonly        0
69ad0 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f  x01    /* Read-o
69ae0 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c 65  nly system table
69af0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 45   */.#define TF_E
69b00 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 30  phemeral       0
69b10 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70 68  x02    /* An eph
69b20 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  emeral table */.
69b30 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50 72  #define TF_HasPr
69b40 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34 20  imaryKey   0x04 
69b50 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20     /* Table has 
69b60 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f  a primary key */
69b70 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74 6f  .#define TF_Auto
69b80 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30 38  increment   0x08
69b90 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 70      /* Integer p
69ba0 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61 75  rimary key is au
69bb0 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23  toincrement */.#
69bc0 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75 61  define TF_Virtua
69bd0 6c 20 20 20 20 20 20 20 20 20 30 78 31 30 20 20  l         0x10  
69be0 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75 61    /* Is a virtua
69bf0 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69  l table */.#defi
69c00 6e 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64 61  ne TF_NeedMetada
69c10 74 61 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a  ta    0x20    /*
69c20 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e   aCol[].zType an
69c30 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d  d aCol[].pColl m
69c40 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a  issing */..../*.
69c50 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 77  ** Test to see w
69c60 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20  hether or not a 
69c70 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75  table is a virtu
69c80 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  al table.  This 
69c90 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20  is.** done as a 
69ca0 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 74  macro so that it
69cb0 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a   will be optimiz
69cc0 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72 74  ed out when virt
69cd0 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70  ual.** table sup
69ce0 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64 20  port is omitted 
69cf0 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a  from the build..
69d00 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
69d10 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
69d20 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49 73  BLE.#  define Is
69d30 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 20  Virtual(X)      
69d40 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73 20  (((X)->tabFlags 
69d50 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30  & TF_Virtual)!=0
69d60 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 69  ).#  define IsHi
69d70 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28  ddenColumn(X) ((
69d80 58 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23 65  X)->isHidden).#e
69d90 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 73  lse.#  define Is
69da0 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 20  Virtual(X)      
69db0 30 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 69  0.#  define IsHi
69dc0 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a  ddenColumn(X) 0.
69dd0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61  #endif../*.** Ea
69de0 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ch foreign key c
69df0 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e 20  onstraint is an 
69e00 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
69e10 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
69e20 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72  ure..**.** A for
69e30 65 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73 6f  eign key is asso
69e40 63 69 61 74 65 64 20 77 69 74 68 20 74 77 6f 20  ciated with two 
69e50 74 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66 72  tables.  The "fr
69e60 6f 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20  om" table is.** 
69e70 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 63  the table that c
69e80 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46 45  ontains the REFE
69e90 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74 68  RENCES clause th
69ea0 61 74 20 63 72 65 61 74 65 73 20 74 68 65 20 66  at creates the f
69eb0 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20  oreign.** key.  
69ec0 54 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20 69  The "to" table i
69ed0 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  s the table that
69ee0 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65   is named in the
69ef0 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75   REFERENCES clau
69f00 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20  se..** Consider 
69f10 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a  this example:.**
69f20 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54  .**     CREATE T
69f30 41 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20 20  ABLE ex1(.**    
69f40 20 20 20 61 20 49 4e 54 45 47 45 52 20 50 52 49     a INTEGER PRI
69f50 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20  MARY KEY,.**    
69f60 20 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f 4e     b INTEGER CON
69f70 53 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46 45  STRAINT fk1 REFE
69f80 52 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a 2a  RENCES ex2(x).**
69f90 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f       );.**.** Fo
69fa0 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22 66  r foreign key "f
69fb0 6b 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61  k1", the from-ta
69fc0 62 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e 64  ble is "ex1" and
69fd0 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73   the to-table is
69fe0 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61   "ex2"..**.** Ea
69ff0 63 68 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c  ch REFERENCES cl
6a000 61 75 73 65 20 67 65 6e 65 72 61 74 65 73 20 61  ause generates a
6a010 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
6a020 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
6a030 63 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20 69  cture.** which i
6a040 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  s attached to th
6a050 65 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54  e from-table.  T
6a060 68 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64  he to-table need
6a070 20 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e 0a   not exist when.
6a080 2a 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c  ** the from-tabl
6a090 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54  e is created.  T
6a0a0 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20  he existence of 
6a0b0 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20  the to-table is 
6a0c0 6e 6f 74 20 63 68 65 63 6b 65 64 0a 2a 2a 20 75  not checked.** u
6a0d0 6e 74 69 6c 20 61 6e 20 61 74 74 65 6d 70 74 20  ntil an attempt 
6a0e0 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 65 72  is made to inser
6a0f0 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20  t data into the 
6a100 66 72 6f 6d 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  from-table..**.*
6a110 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 61 46 4b  * The sqlite.aFK
6a120 65 79 20 68 61 73 68 20 74 61 62 6c 65 20 73 74  ey hash table st
6a130 6f 72 65 73 20 70 6f 69 6e 74 65 72 73 20 74 6f  ores pointers to
6a140 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   this structure.
6a150 2a 2a 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d  ** given the nam
6a160 65 20 6f 66 20 61 20 74 6f 2d 74 61 62 6c 65 2e  e of a to-table.
6a170 20 20 46 6f 72 20 65 61 63 68 20 74 6f 2d 74 61    For each to-ta
6a180 62 6c 65 2c 20 61 6c 6c 20 66 6f 72 65 69 67 6e  ble, all foreign
6a190 20 6b 65 79 73 0a 2a 2a 20 61 73 73 6f 63 69 61   keys.** associa
6a1a0 74 65 64 20 77 69 74 68 20 74 68 61 74 20 74 61  ted with that ta
6a1b0 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 6c 69 6e  ble are on a lin
6a1c0 6b 65 64 20 6c 69 73 74 20 75 73 69 6e 67 20 74  ked list using t
6a1d0 68 65 20 46 4b 65 79 2e 70 4e 65 78 74 54 6f 0a  he FKey.pNextTo.
6a1e0 2a 2a 20 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 72  ** field..*/.str
6a1f0 75 63 74 20 46 4b 65 79 20 7b 0a 20 20 54 61 62  uct FKey {.  Tab
6a200 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f  le *pFrom;     /
6a210 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74  * The table that
6a220 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45   contains the RE
6a230 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20  FERENCES clause 
6a240 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74  */.  FKey *pNext
6a250 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66  From;  /* Next f
6a260 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46  oreign key in pF
6a270 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  rom */.  char *z
6a280 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  To;        /* Na
6a290 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74  me of table that
6a2a0 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20   the key points 
6a2b0 74 6f 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e  to */.  FKey *pN
6a2c0 65 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78  extTo;    /* Nex
6a2d0 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 68  t foreign key th
6a2e0 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 7a 54 6f  at points to zTo
6a2f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20   */.  int nCol; 
6a300 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6a310 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
6a320 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 73 74  this key */.  st
6a330 72 75 63 74 20 73 43 6f 6c 4d 61 70 20 7b 20 20  ruct sColMap {  
6a340 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20 63 6f  /* Mapping of co
6a350 6c 75 6d 6e 73 20 69 6e 20 70 46 72 6f 6d 20 74  lumns in pFrom t
6a360 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f  o columns in zTo
6a370 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 46 72 6f   */.    int iFro
6a380 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  m;         /* In
6a390 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e  dex of column in
6a3a0 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 68   pFrom */.    ch
6a3b0 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20  ar *zCol;       
6a3c0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75   /* Name of colu
6a3d0 6d 6e 20 69 6e 20 7a 54 6f 2e 20 20 49 66 20 30  mn in zTo.  If 0
6a3e0 20 75 73 65 20 50 52 49 4d 41 52 59 20 4b 45 59   use PRIMARY KEY
6a3f0 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 20 20   */.  } *aCol;  
6a400 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65          /* One e
6a410 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f 66  ntry for each of
6a420 20 6e 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a   nCol column s *
6a430 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72 65  /.  u8 isDeferre
6a440 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  d;    /* True if
6a450 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63   constraint chec
6a460 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64  king is deferred
6a470 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a   till COMMIT */.
6a480 20 20 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b    u8 updateConf;
6a490 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65      /* How to re
6a4a0 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20  solve conflicts 
6a4b0 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50  that occur on UP
6a4c0 44 41 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c  DATE */.  u8 del
6a4d0 65 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48  eteConf;    /* H
6a4e0 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f  ow to resolve co
6a4f0 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63  nflicts that occ
6a500 75 72 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a  ur on DELETE */.
6a510 20 20 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b    u8 insertConf;
6a520 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65      /* How to re
6a530 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20  solve conflicts 
6a540 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e  that occur on IN
6a550 53 45 52 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  SERT */.};../*.*
6a560 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74  * SQLite support
6a570 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74  s many different
6a580 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65   ways to resolve
6a590 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a   a constraint.**
6a5a0 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43   error.  ROLLBAC
6a5b0 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61  K processing mea
6a5c0 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72  ns that a constr
6a5d0 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a  aint violation.*
6a5e0 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 65  * causes the ope
6a5f0 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73  ration in proces
6a600 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f  s to fail and fo
6a610 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  r the current tr
6a620 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20  ansaction.** to 
6a630 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  be rolled back. 
6a640 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e   ABORT processin
6a650 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72  g means the oper
6a660 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73  ation in process
6a670 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e  .** fails and an
6a680 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20  y prior changes 
6a690 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70  from that one op
6a6a0 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b  eration are back
6a6b0 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74  ed out,.** but t
6a6c0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
6a6d0 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63  s not rolled bac
6a6e0 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73  k.  FAIL process
6a6f0 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a  ing means that.*
6a700 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  * the operation 
6a710 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70  in progress stop
6a720 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e  s and returns an
6a730 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75   error code.  Bu
6a740 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67  t prior.** chang
6a750 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 61  es due to the sa
6a760 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65  me operation are
6a770 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20   not backed out 
6a780 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a  and no rollback.
6a790 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f  ** occurs.  IGNO
6a7a0 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  RE means that th
6a7b0 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77  e particular row
6a7c0 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65   that caused the
6a7d0 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65   constraint.** e
6a7e0 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65  rror is not inse
6a7f0 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e  rted or updated.
6a800 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e    Processing con
6a810 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72  tinues and no er
6a820 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  ror.** is return
6a830 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61  ed.  REPLACE mea
6a840 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73 74  ns that preexist
6a850 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f 77  ing database row
6a860 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a  s that caused.**
6a870 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72   a UNIQUE constr
6a880 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61  aint violation a
6a890 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68  re removed so th
6a8a0 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72  at the new inser
6a8b0 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63  t or.** update c
6a8c0 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f  an proceed.  Pro
6a8d0 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65  cessing continue
6a8e0 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69  s and no error i
6a8f0 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a  s reported..**.*
6a900 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e  * RESTRICT, SETN
6a910 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45  ULL, and CASCADE
6a920 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f   actions apply o
6a930 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b  nly to foreign k
6a940 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54  eys..** RESTRICT
6a950 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
6a960 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49  ABORT for IMMEDI
6a970 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73  ATE foreign keys
6a980 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65   and the.** same
6a990 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72   as ROLLBACK for
6a9a0 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20   DEFERRED keys. 
6a9b0 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74   SETNULL means t
6a9c0 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a  hat the foreign.
6a9d0 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f  ** key is set to
6a9e0 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20   NULL.  CASCADE 
6a9f0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c  means that a DEL
6aa00 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66  ETE or UPDATE of
6aa10 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63   the.** referenc
6aa20 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20  ed table row is 
6aa30 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20  propagated into 
6aa40 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c  the row that hol
6aa50 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67  ds the.** foreig
6aa60 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  n key..** .** Th
6aa70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62  e following symb
6aa80 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 20  olic values are 
6aa90 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77  used to record w
6aaa0 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20  hich type.** of 
6aab0 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a  action to take..
6aac0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f  */.#define OE_No
6aad0 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68  ne     0   /* Th
6aae0 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72  ere is no constr
6aaf0 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f  aint to check */
6ab00 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c  .#define OE_Roll
6ab10 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c  back 1   /* Fail
6ab20 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61   the operation a
6ab30 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  nd rollback the 
6ab40 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23  transaction */.#
6ab50 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20  define OE_Abort 
6ab60 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f     2   /* Back o
6ab70 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 64  ut changes but d
6ab80 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72  o no rollback tr
6ab90 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65  ansaction */.#de
6aba0 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20  fine OE_Fail    
6abb0 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65   3   /* Stop the
6abc0 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c   operation but l
6abd0 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63  eave all prior c
6abe0 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e  hanges */.#defin
6abf0 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20  e OE_Ignore   4 
6ac00 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20    /* Ignore the 
6ac10 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f  error. Do not do
6ac20 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55   the INSERT or U
6ac30 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65  PDATE */.#define
6ac40 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20   OE_Replace  5  
6ac50 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74   /* Delete exist
6ac60 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e  ing record, then
6ac70 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50   do INSERT or UP
6ac80 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65  DATE */..#define
6ac90 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 20   OE_Restrict 6  
6aca0 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72   /* OE_Abort for
6acb0 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52   IMMEDIATE, OE_R
6acc0 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45  ollback for DEFE
6acd0 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20  RRED */.#define 
6ace0 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20  OE_SetNull  7   
6acf0 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69  /* Set the forei
6ad00 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20  gn key value to 
6ad10 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NULL */.#define 
6ad20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20  OE_SetDflt  8   
6ad30 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69  /* Set the forei
6ad40 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20  gn key value to 
6ad50 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23  its default */.#
6ad60 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64  define OE_Cascad
6ad70 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64  e  9   /* Cascad
6ad80 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f  e the changes */
6ad90 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66  ..#define OE_Def
6ada0 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20  ault  99  /* Do 
6adb0 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65 66  whatever the def
6adc0 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a  ault action is *
6add0 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  /.../*.** An ins
6ade0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
6adf0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
6ae00 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
6ae10 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d  e first.** argum
6ae20 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64  ent to sqlite3Vd
6ae30 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64  beKeyCompare and
6ae40 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74   is used to cont
6ae50 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70  rol the .** comp
6ae60 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77  arison of the tw
6ae70 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f  o index keys..*/
6ae80 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20  .struct KeyInfo 
6ae90 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
6aea0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
6aeb0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
6aec0 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20  on */.  u8 enc; 
6aed0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
6aee0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f  ext encoding - o
6aef0 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55  ne of the TEXT_U
6af00 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  tf* values */.  
6af10 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20  u16 nField;     
6af20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6af30 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c   entries in aCol
6af40 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f  l[] */.  u8 *aSo
6af50 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20  rtOrder;     /* 
6af60 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53  If defined an aS
6af70 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74  ortOrder[i] is t
6af80 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a  rue, sort DESC *
6af90 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f  /.  CollSeq *aCo
6afa0 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61  ll[1];  /* Colla
6afb0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f  ting sequence fo
6afc0 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74  r each term of t
6afd0 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  he key */.};../*
6afe0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
6aff0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
6b000 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
6b010 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
6b020 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69  ut a.** single i
6b030 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 61 74  ndex record that
6b040 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
6b050 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74  n parsed out int
6b060 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20  o individual.** 
6b070 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20  values..**.** A 
6b080 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a  record is an obj
6b090 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ect that contain
6b0a0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69  s one or more fi
6b0b0 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a  elds of data..**
6b0c0 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 73 65   Records are use
6b0d0 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63  d to store the c
6b0e0 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c  ontent of a tabl
6b0f0 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f  e row and to sto
6b100 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66  re.** the key of
6b110 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c   an index.  A bl
6b120 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61  ob encoding of a
6b130 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 61 74   record is creat
6b140 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f  ed by.** the OP_
6b150 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64  MakeRecord opcod
6b160 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e  e of the VDBE an
6b170 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c 65  d is disassemble
6b180 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43  d by the.** OP_C
6b190 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a  olumn opcode..**
6b1a0 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75  .** This structu
6b1b0 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72  re holds a recor
6b1c0 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61  d that has alrea
6b1d0 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d  dy been disassem
6b1e0 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73  bled.** into its
6b1f0 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69 65   constituent fie
6b200 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55  lds..*/.struct U
6b210 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a  npackedRecord {.
6b220 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
6b230 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  nfo;  /* Collati
6b240 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65  on and sort-orde
6b250 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  r information */
6b260 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20  .  u16 nField;  
6b270 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6b280 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
6b290 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20  pMem[] */.  u16 
6b2a0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
6b2b0 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69  /* Boolean setti
6b2c0 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e  ngs.  UNPACKED_.
6b2d0 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 4d 65  .. below */.  Me
6b2e0 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *aMem;        
6b2f0 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d    /* Values */.}
6b300 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
6b310 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 63   values of Unpac
6b320 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a  kedRecord.flags.
6b330 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43  */.#define UNPAC
6b340 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 20  KED_NEED_FREE   
6b350 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d    0x0001  /* Mem
6b360 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c 69  ory is from sqli
6b370 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23  te3Malloc() */.#
6b380 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f  define UNPACKED_
6b390 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 78  NEED_DESTROY  0x
6b3a0 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d  0002  /* apMem[]
6b3b0 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20  s should all be 
6b3c0 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65  destroyed */.#de
6b3d0 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 47  fine UNPACKED_IG
6b3e0 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 30  NORE_ROWID  0x00
6b3f0 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72  04  /* Ignore tr
6b400 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20  ailing rowid on 
6b410 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20  key1 */.#define 
6b420 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
6b430 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f         0x0008  /
6b440 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20  * Make this key 
6b450 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65  an epsilon large
6b460 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50  r */.#define UNP
6b470 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
6b480 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41  CH  0x0010  /* A
6b490 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 73   prefix match is
6b4a0 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a   considered OK *
6b4b0 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51  /../*.** Each SQ
6b4c0 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72 65  L index is repre
6b4d0 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79  sented in memory
6b4e0 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   by an.** instan
6b4f0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
6b500 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
6b510 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73  *.** The columns
6b520 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68   of the table th
6b530 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64  at are to be ind
6b540 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 62  exed are describ
6b550 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43  ed.** by the aiC
6b560 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66  olumn[] field of
6b570 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
6b580 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73    For example, s
6b590 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76  uppose.** we hav
6b5a0 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
6b5b0 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a  table and index:
6b5c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  .**.**     CREAT
6b5d0 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20 69  E TABLE Ex1(c1 i
6b5e0 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74  nt, c2 int, c3 t
6b5f0 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45  ext);.**     CRE
6b600 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e  ATE INDEX Ex2 ON
6b610 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a   Ex1(c3,c1);.**.
6b620 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20  ** In the Table 
6b630 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69  structure descri
6b640 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d  bing Ex1, nCol==
6b650 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65 20  3 because there 
6b660 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c  are.** three col
6b670 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
6b680 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78  e.  In the Index
6b690 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72   structure descr
6b6a0 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43  ibing.** Ex2, nC
6b6b0 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32  olumn==2 since 2
6b6c0 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e   of the 3 column
6b6d0 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64  s of Ex1 are ind
6b6e0 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c  exed..** The val
6b6f0 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69  ue of aiColumn i
6b700 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c  s {2, 0}.  aiCol
6b710 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73  umn[0]==2 becaus
6b720 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20  e the .** first 
6b730 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64  column to be ind
6b740 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61 6e  exed (c3) has an
6b750 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45   index of 2 in E
6b760 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68  x1.aCol[]..** Th
6b770 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20  e second column 
6b780 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63  to be indexed (c
6b790 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  1) has an index 
6b7a0 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61  of 0 in.** Ex1.a
6b7b0 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32  Col[], hence Ex2
6b7c0 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e  .aiColumn[1]==0.
6b7d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78  .**.** The Index
6b7e0 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64  .onError field d
6b7f0 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
6b800 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64  r or not the ind
6b810 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  exed columns.** 
6b820 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 61  must be unique a
6b830 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66  nd what to do if
6b840 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20   they are not.  
6b850 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72  When Index.onErr
6b860 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69  or=OE_None,.** i
6b870 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20  t means this is 
6b880 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64  not a unique ind
6b890 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69  ex.  Otherwise i
6b8a0 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e  t is a unique in
6b8b0 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76  dex.** and the v
6b8c0 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e  alue of Index.on
6b8d0 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74  Error indicate t
6b8e0 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63  he which conflic
6b8f0 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a  t resolution .**
6b900 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d   algorithm to em
6b910 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e  ploy whenever an
6b920 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
6b930 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e   to insert a non
6b940 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65  -unique.** eleme
6b950 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e  nt..*/.struct In
6b960 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  dex {.  char *zN
6b970 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame;     /* Name
6b980 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a   of this index *
6b990 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
6b9a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6b9b0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
6b9c0 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74   table used by t
6b9d0 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  his index */.  i
6b9e0 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20  nt *aiColumn;   
6b9f0 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73  /* Which columns
6ba00 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 69   are used by thi
6ba10 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73  s index.  1st is
6ba20 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64   0 */.  unsigned
6ba30 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52   *aiRowEst; /* R
6ba40 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45  esult of ANALYZE
6ba50 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65  : Est. rows sele
6ba60 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c  cted by each col
6ba70 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  umn */.  Table *
6ba80 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65  pTable;   /* The
6ba90 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67   SQL table being
6baa0 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e   indexed */.  in
6bab0 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f  t tnum;        /
6bac0 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
6bad0 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69  g root of this i
6bae0 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65  ndex in database
6baf0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e   file */.  u8 on
6bb00 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f  Error;      /* O
6bb10 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f  E_Abort, OE_Igno
6bb20 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20  re, OE_Replace, 
6bb30 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20  or OE_None */.  
6bb40 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20  u8 autoIndex;   
6bb50 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61   /* True if is a
6bb60 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65  utomatically cre
6bb70 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49  ated (ex: by UNI
6bb80 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a  QUE) */.  char *
6bb90 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74  zColAff;   /* St
6bba0 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68  ring defining th
6bbb0 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61  e affinity of ea
6bbc0 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49  ch column */.  I
6bbd0 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20  ndex *pNext;    
6bbe0 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65  /* The next inde
6bbf0 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
6bc00 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  h the same table
6bc10 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53   */.  Schema *pS
6bc20 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61  chema; /* Schema
6bc30 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73   containing this
6bc40 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a   index */.  u8 *
6bc50 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20  aSortOrder;  /* 
6bc60 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e  Array of size In
6bc70 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75  dex.nColumn. Tru
6bc80 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d  e==DESC, False==
6bc90 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ASC */.  char **
6bca0 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72  azColl;   /* Arr
6bcb0 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20  ay of collation 
6bcc0 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66  sequence names f
6bcd0 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a  or index */.};..
6bce0 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e  /*.** Each token
6bcf0 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74   coming out of t
6bd00 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69  he lexer is an i
6bd10 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
6bd20 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  is structure.  T
6bd30 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75  okens are also u
6bd40 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  sed as part of a
6bd50 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  n expression..**
6bd60 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65  .** Note if Toke
6bd70 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65  n.z==0 then Toke
6bd80 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e  n.dyn and Token.
6bd90 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20  n are undefined 
6bda0 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61  and.** may conta
6bdb0 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73  in random values
6bdc0 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61  .  Do not make a
6bdd0 6e 79 20 61 73 73 75 6d 70 74 69 6f 6e 73 20 61  ny assumptions a
6bde0 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a  bout Token.dyn.*
6bdf0 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68  * and Token.n wh
6be00 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a  en Token.z==0..*
6be10 2f 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b  /.struct Token {
6be20 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
6be30 64 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65  d char *z; /* Te
6be40 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e  xt of the token.
6be50 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69    Not NULL-termi
6be60 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69  nated! */.  unsi
6be70 67 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20 20  gned dyn  : 1;  
6be80 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
6be90 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c  malloced memory,
6bea0 20 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69   false for stati
6beb0 63 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  c */.  unsigned 
6bec0 6e 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20 2f  n    : 31;     /
6bed0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  * Number of char
6bee0 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74  acters in this t
6bef0 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  oken */.};../*.*
6bf00 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
6bf10 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
6bf20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
6bf30 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67  tion needed to g
6bf40 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20  enerate.** code 
6bf50 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61  for a SELECT tha
6bf60 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65  t contains aggre
6bf70 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  gate functions..
6bf80 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70  **.** If Expr.op
6bf90 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  ==TK_AGG_COLUMN 
6bfa0 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  or TK_AGG_FUNCTI
6bfb0 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67  ON then Expr.pAg
6bfc0 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f  gInfo is a.** po
6bfd0 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74  inter to this st
6bfe0 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78  ructure.  The Ex
6bff0 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64  pr.iColumn field
6c000 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e   is the index in
6c010 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c  .** AggInfo.aCol
6c020 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46  [] or AggInfo.aF
6c030 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61  unc[] of informa
6c040 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67  tion needed to g
6c050 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20  enerate.** code 
6c060 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a  for that node..*
6c070 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72  *.** AggInfo.pGr
6c080 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66  oupBy and AggInf
6c090 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f  o.aFunc.pExpr po
6c0a0 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69  int to fields wi
6c0b0 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67  thin the.** orig
6c0c0 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75  inal Select stru
6c0d0 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72  cture that descr
6c0e0 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20  ibes the SELECT 
6c0f0 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73  statement.  Thes
6c100 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e  e.** fields do n
6c110 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72  ot need to be fr
6c120 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63  eed when dealloc
6c130 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66  ating the AggInf
6c140 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  o structure..*/.
6c150 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b  struct AggInfo {
6c160 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65  .  u8 directMode
6c170 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69  ;          /* Di
6c180 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d  rect rendering m
6c190 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64  ode means take d
6c1a0 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20  ata directly.   
6c1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c1c0 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73         ** from s
6c1d0 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74  ource tables rat
6c1e0 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63  her than from ac
6c1f0 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20  cumulators */.  
6c200 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78  u8 useSortingIdx
6c210 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69  ;       /* In di
6c220 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72  rect mode, refer
6c230 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67  ence the sorting
6c240 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20   index rather.  
6c250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c260 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20          ** than 
6c270 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65  the source table
6c280 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e   */.  int sortin
6c290 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a  gIdx;         /*
6c2a0 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   Cursor number o
6c2b0 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e  f the sorting in
6c2c0 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  dex */.  ExprLis
6c2d0 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20  t *pGroupBy;    
6c2e0 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79   /* The group by
6c2f0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
6c300 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b   nSortingColumn;
6c310 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6c320 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
6c330 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a   sorting index *
6c340 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e  /.  struct AggIn
6c350 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46  fo_col {    /* F
6c360 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75  or each column u
6c370 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61  sed in source ta
6c380 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c  bles */.    Tabl
6c390 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
6c3a0 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74       /* Source t
6c3b0 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  able */.    int 
6c3c0 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  iTable;         
6c3d0 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
6c3e0 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75  umber of the sou
6c3f0 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  rce table */.   
6c400 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
6c410 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
6c420 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69  umn number withi
6c430 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62  n the source tab
6c440 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53  le */.    int iS
6c450 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20  orterColumn;    
6c460 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
6c470 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69  ber in the sorti
6c480 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20  ng index */.    
6c490 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20  int iMem;       
6c4a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f           /* Memo
6c4b0 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74  ry location that
6c4c0 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c   acts as accumul
6c4d0 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72  ator */.    Expr
6c4e0 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20   *pExpr;        
6c4f0 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67       /* The orig
6c500 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20  inal expression 
6c510 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20  */.  } *aCol;.  
6c520 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
6c530 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6c540 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65  r of used entrie
6c550 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20  s in aCol[] */. 
6c560 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f   int nColumnAllo
6c570 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  c;       /* Numb
6c580 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f  er of slots allo
6c590 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d  cated for aCol[]
6c5a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d   */.  int nAccum
6c5b0 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a  ulator;       /*
6c5c0 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
6c5d0 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72  ns that show thr
6c5e0 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70  ough to the outp
6c5f0 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  ut..            
6c600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
6c610 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75   Additional colu
6c620 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c  mns are used onl
6c630 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20  y as parameters 
6c640 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  to.             
6c650 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
6c660 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
6c670 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ons */.  struct 
6c680 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20  AggInfo_func {  
6c690 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67   /* For each agg
6c6a0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
6c6b0 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78  */.    Expr *pEx
6c6c0 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pr;             
6c6d0 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e  /* Expression en
6c6e0 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74  coding the funct
6c6f0 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44  ion */.    FuncD
6c700 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20  ef *pFunc;      
6c710 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65      /* The aggre
6c720 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d  gate function im
6c730 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  plementation */.
6c740 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20      int iMem;   
6c750 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6c760 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  Memory location 
6c770 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63  that acts as acc
6c780 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20  umulator */.    
6c790 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20  int iDistinct;  
6c7a0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65           /* Ephe
6c7b0 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64  meral table used
6c7c0 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54   to enforce DIST
6c7d0 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75  INCT */.  } *aFu
6c7e0 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b  nc;.  int nFunc;
6c7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6c800 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
6c810 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f  es in aFunc[] */
6c820 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f  .  int nFuncAllo
6c830 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  c;         /* Nu
6c840 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c  mber of slots al
6c850 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e  located for aFun
6c860 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  c[] */.};../*.**
6c870 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e   Each node of an
6c880 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
6c890 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 73  he parse tree is
6c8a0 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20   an instance.** 
6c8b0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
6c8c0 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70  e..**.** Expr.op
6c8d0 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20   is the opcode. 
6c8e0 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   The integer par
6c8f0 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20  ser token codes 
6c900 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73  are reused.** as
6c910 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 20   opcodes here.  
6c920 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  For example, the
6c930 20 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20   parser defines 
6c940 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69  TK_GE to be an i
6c950 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72  nteger.** code r
6c960 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
6c970 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 20  ">=" operator.  
6c980 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65  This same intege
6c990 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65 64  r code is reused
6c9a0 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74  .** to represent
6c9b0 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68 61   the greater-tha
6c9c0 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70  n-or-equal-to op
6c9d0 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78  erator in the ex
6c9e0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65  pression.** tree
6c9f0 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69  ..**.** Expr.pRi
6ca00 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65  ght and Expr.pLe
6ca10 66 74 20 61 72 65 20 73 75 62 65 78 70 72 65 73  ft are subexpres
6ca20 73 69 6f 6e 73 2e 20 20 45 78 70 72 2e 70 4c 69  sions.  Expr.pLi
6ca30 73 74 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20  st is a list.** 
6ca40 6f 66 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74  of argument if t
6ca50 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
6ca60 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a   a function..**.
6ca70 2a 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 20 69 73  ** Expr.token is
6ca80 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
6ca90 6b 65 6e 20 66 6f 72 20 74 68 69 73 20 6e 6f 64  ken for this nod
6caa0 65 2e 20 20 46 6f 72 20 73 6f 6d 65 20 65 78 70  e.  For some exp
6cab0 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74  ressions.** that
6cac0 20 68 61 76 65 20 73 75 62 65 78 70 72 65 73 73   have subexpress
6cad0 69 6f 6e 73 2c 20 45 78 70 72 2e 74 6f 6b 65 6e  ions, Expr.token
6cae0 20 63 61 6e 20 62 65 20 74 68 65 20 63 6f 6d 70   can be the comp
6caf0 6c 65 74 65 20 74 65 78 74 20 74 68 61 74 20 67  lete text that g
6cb00 61 76 65 0a 2a 2a 20 72 69 73 65 20 74 6f 20 74  ave.** rise to t
6cb10 68 65 20 45 78 70 72 2e 20 20 49 6e 20 74 68 65  he Expr.  In the
6cb20 20 6c 61 74 74 65 72 20 63 61 73 65 2c 20 74 68   latter case, th
6cb30 65 20 74 6f 6b 65 6e 20 69 73 20 6d 61 72 6b 65  e token is marke
6cb40 64 20 61 73 20 62 65 69 6e 67 0a 2a 2a 20 61 20  d as being.** a 
6cb50 63 6f 6d 70 6f 75 6e 64 20 74 6f 6b 65 6e 2e 0a  compound token..
6cb60 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73  **.** An express
6cb70 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ion of the form 
6cb80 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65  ID or ID.ID refe
6cb90 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69  rs to a column i
6cba0 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f  n a table..** Fo
6cbb0 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f  r such expressio
6cbc0 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73  ns, Expr.op is s
6cbd0 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20  et to TK_COLUMN 
6cbe0 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20  and Expr.iTable 
6cbf0 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65  is.** the intege
6cc00 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  r cursor number 
6cc10 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72  of a VDBE cursor
6cc20 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61   pointing to tha
6cc30 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45  t table and.** E
6cc40 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74  xpr.iColumn is t
6cc50 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  he column number
6cc60 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
6cc70 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68  c column.  If th
6cc80 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  e.** expression 
6cc90 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73  is used as a res
6cca0 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67  ult in an aggreg
6ccb0 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e  ate SELECT, then
6ccc0 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73   the.** value is
6ccd0 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20   also stored in 
6cce0 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f  the Expr.iAgg co
6ccf0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72  lumn in the aggr
6cd00 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a  egate so that.**
6cd10 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73   it can be acces
6cd20 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67  sed after all ag
6cd30 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d  gregates are com
6cd40 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  puted..**.** If 
6cd50 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
6cd60 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  s a function, th
6cd70 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73  e Expr.iTable is
6cd80 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65   an integer code
6cd90 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 69 6e 67  .** representing
6cda0 20 77 68 69 63 68 20 66 75 6e 63 74 69 6f 6e 2e   which function.
6cdb0 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73    If the express
6cdc0 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e  ion is an unboun
6cdd0 64 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61  d variable.** ma
6cde0 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e  rker (a question
6cdf0 20 6d 61 72 6b 20 63 68 61 72 61 63 74 65 72 20   mark character 
6ce00 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69  '?' in the origi
6ce10 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68  nal SQL) then th
6ce20 65 0a 2a 2a 20 45 78 70 72 2e 69 54 61 62 6c 65  e.** Expr.iTable
6ce30 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78   holds the index
6ce40 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74   number for that
6ce50 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a   variable..**.**
6ce60 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
6ce70 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79  on is a subquery
6ce80 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75   then Expr.iColu
6ce90 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65  mn holds an inte
6cea0 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  ger.** register 
6ceb0 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e  number containin
6cec0 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  g the result of 
6ced0 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49  the subquery.  I
6cee0 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72  f the.** subquer
6cef0 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61  y gives a consta
6cf00 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20  nt result, then 
6cf10 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49  iTable is -1.  I
6cf20 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a  f the subquery.*
6cf30 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72  * gives a differ
6cf40 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69  ent answer at di
6cf50 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75  fferent times du
6cf60 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70  ring statement p
6cf70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65  rocessing.** the
6cf80 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20  n iTable is the 
6cf90 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62  address of a sub
6cfa0 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d  routine that com
6cfb0 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65  putes the subque
6cfc0 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78  ry..**.** The Ex
6cfd0 70 72 2e 70 53 65 6c 65 63 74 20 66 69 65 6c 64  pr.pSelect field
6cfe0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 53 45 4c   points to a SEL
6cff0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
6d000 54 68 65 20 53 45 4c 45 43 54 20 6d 69 67 68 74  The SELECT might
6d010 0a 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74  .** be the right
6d020 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 49   operand of an I
6d030 4e 20 6f 70 65 72 61 74 6f 72 2e 20 20 4f 72 2c  N operator.  Or,
6d040 20 69 66 20 61 20 73 63 61 6c 61 72 20 53 45 4c   if a scalar SEL
6d050 45 43 54 20 61 70 70 65 61 72 73 0a 2a 2a 20 69  ECT appears.** i
6d060 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  n an expression 
6d070 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b  the opcode is TK
6d080 5f 53 45 4c 45 43 54 20 61 6e 64 20 45 78 70 72  _SELECT and Expr
6d090 2e 70 53 65 6c 65 63 74 20 69 73 20 74 68 65 20  .pSelect is the 
6d0a0 6f 6e 6c 79 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e  only.** operand.
6d0b0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78  .**.** If the Ex
6d0c0 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50  pr is of type OP
6d0d0 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65  _Column, and the
6d0e0 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c   table it is sel
6d0f0 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69  ecting from.** i
6d100 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f  s a disk table o
6d110 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73  r the "old.*" ps
6d120 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e  eudo-table, then
6d130 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20   pTab points to 
6d140 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  the.** correspon
6d150 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e  ding table defin
6d160 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ition..*/.struct
6d170 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b   Expr {.  u8 op;
6d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d190 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65   /* Operation pe
6d1a0 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20  rformed by this 
6d1b0 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61  node */.  char a
6d1c0 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20  ffinity;        
6d1d0 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
6d1e0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   of the column o
6d1f0 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c  r 0 if not a col
6d200 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  umn */.  u16 fla
6d210 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
6d220 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73  /* Various flags
6d230 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a  .  See below */.
6d240 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
6d250 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
6d260 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f  collation type o
6d270 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20  f the column or 
6d280 30 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65  0 */.  Expr *pLe
6d290 66 74 2c 20 2a 70 52 69 67 68 74 3b 20 20 2f 2a  ft, *pRight;  /*
6d2a0 20 4c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20   Left and right 
6d2b0 73 75 62 6e 6f 64 65 73 20 2a 2f 0a 20 20 45 78  subnodes */.  Ex
6d2c0 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20  prList *pList;  
6d2d0 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f       /* A list o
6d2e0 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 75 73  f expressions us
6d2f0 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 20 61  ed as function a
6d300 72 67 75 6d 65 6e 74 73 0a 20 20 20 20 20 20 20  rguments.       
6d310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d320 20 20 2a 2a 20 6f 72 20 69 6e 20 22 3c 65 78 70    ** or in "<exp
6d330 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73  r> IN (<expr-lis
6d340 74 29 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74  t)" */.  Token t
6d350 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
6d360 2f 2a 20 41 6e 20 6f 70 65 72 61 6e 64 20 74 6f  /* An operand to
6d370 6b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73  ken */.  Token s
6d380 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  pan;            
6d390 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74  /* Complete text
6d3a0 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
6d3b0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  on */.  int iTab
6d3c0 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f  le, iColumn;   /
6d3d0 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f  * When op==TK_CO
6d3e0 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73 20  LUMN, then this 
6d3f0 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20  expr node means 
6d400 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  the.            
6d410 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
6d420 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64  iColumn-th field
6d430 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d 74   of the iTable-t
6d440 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67  h table. */.  Ag
6d450 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b  gInfo *pAggInfo;
6d460 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20       /* Used by 
6d470 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e  TK_AGG_COLUMN an
6d480 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  d TK_AGG_FUNCTIO
6d490 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b  N */.  int iAgg;
6d4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6d4b0 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20   Which entry in 
6d4c0 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
6d4d0 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f   or ->aFunc[] */
6d4e0 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69  .  int iRightJoi
6d4f0 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20  nTable;   /* If 
6d500 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65  EP_FromJoin, the
6d510 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
6d520 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 53 65  the join */.  Se
6d530 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20  lect *pSelect;  
6d540 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65       /* When the
6d550 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
6d560 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 20 41 6c   sub-select.  Al
6d570 73 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20  so the.         
6d580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d590 2a 2a 20 72 69 67 68 74 20 73 69 64 65 20 6f 66  ** right side of
6d5a0 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65   "<expr> IN (<se
6d5b0 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 54 61 62  lect>)" */.  Tab
6d5c0 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
6d5d0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72      /* Table for
6d5e0 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65   TK_COLUMN expre
6d5f0 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53  ssions. */.#if S
6d600 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
6d610 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65  EPTH>0.  int nHe
6d620 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
6d630 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65  /* Height of the
6d640 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20   tree headed by 
6d650 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e  this node */.#en
6d660 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  dif.};../*.** Th
6d670 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
6d680 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20  the meanings of 
6d690 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72  bits in the Expr
6d6a0 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f  .flags field..*/
6d6b0 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d  .#define EP_From
6d6c0 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f  Join   0x0001  /
6d6d0 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20  * Originated in 
6d6e0 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
6d6f0 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a  se of a join */.
6d700 23 64 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20  #define EP_Agg  
6d710 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a        0x0002  /*
6d720 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72   Contains one or
6d730 20 6d 6f 72 65 20 61 67 67 72 65 67 61 74 65 20   more aggregate 
6d740 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65  functions */.#de
6d750 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64  fine EP_Resolved
6d760 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44     0x0004  /* ID
6d770 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f  s have been reso
6d780 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20  lved to COLUMNs 
6d790 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72  */.#define EP_Er
6d7a0 72 6f 72 20 20 20 20 20 20 30 78 30 30 30 38 20  ror      0x0008 
6d7b0 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63   /* Expression c
6d7c0 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d  ontains one or m
6d7d0 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64  ore errors */.#d
6d7e0 65 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63  efine EP_Distinc
6d7f0 74 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41  t   0x0010  /* A
6d800 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
6d810 6e 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20  n with DISTINCT 
6d820 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69  keyword */.#defi
6d830 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20  ne EP_VarSelect 
6d840 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c   0x0020  /* pSel
6d850 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65  ect is correlate
6d860 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20  d, not constant 
6d870 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 65  */.#define EP_De
6d880 71 75 6f 74 65 64 20 20 20 30 78 30 30 34 30 20  quoted   0x0040 
6d890 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
6d8a0 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20  string has been 
6d8b0 64 65 71 75 6f 74 65 64 20 2a 2f 0a 23 64 65 66  dequoted */.#def
6d8c0 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63  ine EP_InfixFunc
6d8d0 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 75    0x0080  /* Tru
6d8e0 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66  e for an infix f
6d8f0 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47  unction: LIKE, G
6d900 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66  LOB, etc */.#def
6d910 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  ine EP_ExpCollat
6d920 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c  e 0x0100  /* Col
6d930 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
6d940 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 63  specified explic
6d950 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  itly */.#define 
6d960 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30 78  EP_AnyAff     0x
6d970 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b  0200  /* Can tak
6d980 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d  e a cached colum
6d990 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 74  n of any affinit
6d9a0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  y */.#define EP_
6d9b0 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34 30  FixedDest  0x040
6d9c0 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65  0  /* Result nee
6d9d0 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66 69  ded in a specifi
6d9e0 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 64  c register */.#d
6d9f0 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75  efine EP_IntValu
6da00 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20 49  e   0x0800  /* I
6da10 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f 6e  nteger value con
6da20 74 61 69 6e 65 64 20 69 6e 20 69 54 61 62 6c 65  tained in iTable
6da30 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20   */./*.** These 
6da40 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73  macros can be us
6da50 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c  ed to test, set,
6da60 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69   or clear bits i
6da70 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66  n the .** Expr.f
6da80 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
6da90 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72  define ExprHasPr
6daa0 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20  operty(E,P)     
6dab0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29  (((E)->flags&(P)
6dac0 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20  )==(P)).#define 
6dad0 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
6dae0 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e  ty(E,P)  (((E)->
6daf0 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23  flags&(P))!=0).#
6db00 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72  define ExprSetPr
6db10 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20  operty(E,P)     
6db20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a  (E)->flags|=(P).
6db30 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61  #define ExprClea
6db40 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  rProperty(E,P)  
6db50 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50   (E)->flags&=~(P
6db60 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20  )../*.** A list 
6db70 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  of expressions. 
6db80 20 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   Each expression
6db90 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   may optionally 
6dba0 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20  have a.** name. 
6dbb0 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f   An expr/name co
6dbc0 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65  mbination can be
6dbd0 20 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c   used in several
6dbe0 20 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61   ways, such.** a
6dbf0 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65  s the list of "e
6dc00 78 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64  xpr AS ID" field
6dc10 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53  s following a "S
6dc20 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65  ELECT" or in the
6dc30 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20  .** list of "ID 
6dc40 3d 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e  = expr" items in
6dc50 20 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c   an UPDATE.  A l
6dc60 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ist of expressio
6dc70 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62  ns can.** also b
6dc80 65 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72  e used as the ar
6dc90 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63  gument to a func
6dca0 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63  tion, in which c
6dcb0 61 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a  ase the a.zName.
6dcc0 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20  ** field is not 
6dcd0 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  used..*/.struct 
6dce0 45 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74  ExprList {.  int
6dcf0 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20   nExpr;         
6dd00 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6dd10 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20   expressions on 
6dd20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  the list */.  in
6dd30 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20  t nAlloc;       
6dd40 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6dd50 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61  f entries alloca
6dd60 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69  ted below */.  i
6dd70 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20  nt iECursor;    
6dd80 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75        /* VDBE Cu
6dd90 72 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20  rsor associated 
6dda0 77 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69  with this ExprLi
6ddb0 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45  st */.  struct E
6ddc0 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20  xprList_item {. 
6ddd0 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20     Expr *pExpr; 
6dde0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
6ddf0 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
6de00 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72  ions */.    char
6de10 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
6de20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f     /* Token asso
6de30 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
6de40 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
6de50 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b     u8 sortOrder;
6de60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66            /* 1 f
6de70 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72  or DESC or 0 for
6de80 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64   ASC */.    u8 d
6de90 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  one;            
6dea0 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20     /* A flag to 
6deb0 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72  indicate when pr
6dec0 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69  ocessing is fini
6ded0 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20  shed */.    u16 
6dee0 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  iCol;           
6def0 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20     /* For ORDER 
6df00 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  BY, column numbe
6df10 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20  r in result set 
6df20 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61  */.    u16 iAlia
6df30 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s;            /*
6df40 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73   Index into Pars
6df50 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a  e.aAlias[] for z
6df60 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20  Name */.  } *a; 
6df70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6df80 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f   /* One entry fo
6df90 72 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f  r each expressio
6dfa0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  n */.};../*.** A
6dfb0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
6dfc0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  is structure can
6dfd0 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c   hold a simple l
6dfe0 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65  ist of identifie
6dff0 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74  rs,.** such as t
6e000 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20  he list "a,b,c" 
6e010 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
6e020 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a   statements:.**.
6e030 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  **      INSERT I
6e040 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c  NTO t(a,b,c) VAL
6e050 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20  UES ...;.**     
6e060 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64   CREATE INDEX id
6e070 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a  x ON t(a,b,c);.*
6e080 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52  *      CREATE TR
6e090 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52  IGGER trig BEFOR
6e0a0 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c  E UPDATE ON t(a,
6e0b0 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20  b,c) ...;.**.** 
6e0c0 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78  The IdList.a.idx
6e0d0 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77   field is used w
6e0e0 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72  hen the IdList r
6e0f0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69  epresents the li
6e100 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  st of.** column 
6e110 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61  names after a ta
6e120 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49  ble name in an I
6e130 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e  NSERT statement.
6e140 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65    In the stateme
6e150 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53  nt.**.**     INS
6e160 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63  ERT INTO t(a,b,c
6e170 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22  ) ....**.** If "
6e180 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63  a" is the k-th c
6e190 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22  olumn of table "
6e1a0 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e  t", then IdList.
6e1b0 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a  a[0].idx==k..*/.
6e1c0 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a  struct IdList {.
6e1d0 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f    struct IdList_
6e1e0 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20  item {.    char 
6e1f0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20  *zName;      /* 
6e200 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e  Name of the iden
6e210 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e  tifier */.    in
6e220 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  t idx;          
6e230 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65  /* Index in some
6e240 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66   Table.aCol[] of
6e250 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20   a column named 
6e260 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b  zName */.  } *a;
6e270 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20  .  int nId;     
6e280 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6e290 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20   identifiers on 
6e2a0 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  the list */.  in
6e2b0 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f  t nAlloc;      /
6e2c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
6e2d0 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ies allocated fo
6e2e0 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d  r a[] below */.}
6e2f0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74  ;../*.** The bit
6e300 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65  mask datatype de
6e310 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75  fined below is u
6e320 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20  sed for various 
6e330 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a  optimizations..*
6e340 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68  *.** Changing th
6e350 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74  is from a 64-bit
6e360 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70   to a 32-bit typ
6e370 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d  e limits the num
6e380 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73  ber of.** tables
6e390 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32   in a join to 32
6e3a0 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20   instead of 64. 
6e3b0 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64   But it also red
6e3c0 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a  uces the size.**
6e3d0 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   of the library 
6e3e0 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20  by 738 bytes on 
6e3f0 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ix86..*/.typedef
6e400 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f   u64 Bitmask;../
6e410 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20  *.** The number 
6e420 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74  of bits in a Bit
6e430 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61  mask.  "BMS" mea
6e440 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65  ns "BitMask Size
6e450 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d  "..*/.#define BM
6e460 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66  S  ((int)(sizeof
6e470 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f  (Bitmask)*8))../
6e480 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
6e490 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73  ng structure des
6e4a0 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20  cribes the FROM 
6e4b0 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
6e4c0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
6e4d0 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73   Each table or s
6e4e0 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46  ubquery in the F
6e4f0 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20  ROM clause is a 
6e500 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74  separate element
6e510 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69   of.** the SrcLi
6e520 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a  st.a[] array..**
6e530 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64  .** With the add
6e540 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c  ition of multipl
6e550 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
6e560 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  rt, the followin
6e570 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63  g structure.** c
6e580 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20  an also be used 
6e590 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61  to describe a pa
6e5a0 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73  rticular table s
6e5b0 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65  uch as the table
6e5c0 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69   that.** is modi
6e5d0 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52  fied by an INSER
6e5e0 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50  T, DELETE, or UP
6e5f0 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  DATE statement. 
6e600 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c   In standard SQL
6e610 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c  ,.** such a tabl
6e620 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70  e must be a simp
6e630 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75  le name: ID.  Bu
6e640 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65  t in SQLite, the
6e650 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f   table can.** no
6e660 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20  w be identified 
6e670 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61  by a database na
6e680 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20  me, a dot, then 
6e690 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20  the table name: 
6e6a0 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ID.ID..**.** The
6e6b0 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73   jointype starts
6e6c0 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65   out showing the
6e6d0 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65   join type betwe
6e6e0 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  en the current t
6e6f0 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20  able.** and the 
6e700 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68  next table on th
6e710 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72  e list.  The par
6e720 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c  ser builds the l
6e730 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a  ist this way..**
6e740 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c   But sqlite3SrcL
6e750 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65  istShiftJoinType
6e760 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20  () later shifts 
6e770 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f  the jointypes so
6e780 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f   that each.** jo
6e790 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73  intype expresses
6e7a0 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65   the join betwee
6e7b0 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  n the table and 
6e7c0 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62  the previous tab
6e7d0 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72  le..*/.struct Sr
6e7e0 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53  cList {.  i16 nS
6e7f0 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  rc;        /* Nu
6e800 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f  mber of tables o
6e810 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  r subqueries in 
6e820 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
6e830 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b  */.  i16 nAlloc;
6e840 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6e850 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63  of entries alloc
6e860 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f  ated in a[] belo
6e870 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  w */.  struct Sr
6e880 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20  cList_item {.   
6e890 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65   char *zDatabase
6e8a0 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61  ;  /* Name of da
6e8b0 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74  tabase holding t
6e8c0 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  his table */.   
6e8d0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
6e8e0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
6e8f0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63  e table */.    c
6e900 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20  har *zAlias;    
6e910 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74   /* The "B" part
6e920 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70   of a "A AS B" p
6e930 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73  hrase.  zName is
6e940 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20   the "A" */.    
6e950 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
6e960 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c    /* An SQL tabl
6e970 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
6e980 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  to zName */.    
6e990 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b  Select *pSelect;
6e9a0 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74    /* A SELECT st
6e9b0 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20  atement used in 
6e9c0 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65  place of a table
6e9d0 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20   name */.    u8 
6e9e0 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f  isPopulated;   /
6e9f0 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  * Temporary tabl
6ea00 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
6ea10 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75  h SELECT is popu
6ea20 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20  lated */.    u8 
6ea30 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f  jointype;      /
6ea40 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62  * Type of join b
6ea50 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65  etween this able
6ea60 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75   and the previou
6ea70 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49  s */.    u8 notI
6ea80 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72  ndexed;    /* Tr
6ea90 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ue if there is a
6eaa0 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61   NOT INDEXED cla
6eab0 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  use */.    int i
6eac0 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20  Cursor;      /* 
6ead0 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
6eae0 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61  number used to a
6eaf0 63 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65  ccess this table
6eb00 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f   */.    Expr *pO
6eb10 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  n;        /* The
6eb20 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20   ON clause of a 
6eb30 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69  join */.    IdLi
6eb40 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a  st *pUsing;   /*
6eb50 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73   The USING claus
6eb60 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20  e of a join */. 
6eb70 20 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73     Bitmask colUs
6eb80 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31  ed;  /* Bit N (1
6eb90 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75  <<N) set if colu
6eba0 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20  mn N of pTab is 
6ebb0 75 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72  used */.    char
6ebc0 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a   *zIndex;     /*
6ebd0 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d   Identifier from
6ebe0 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49   "INDEXED BY <zI
6ebf0 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f  ndex>" clause */
6ec00 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  .    Index *pInd
6ec10 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20  ex;    /* Index 
6ec20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73  structure corres
6ec30 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65  ponding to zInde
6ec40 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d  x, if any */.  }
6ec50 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20   a[1];          
6ec60 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20     /* One entry 
6ec70 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66  for each identif
6ec80 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ier on the list 
6ec90 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72  */.};../*.** Per
6eca0 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66  mitted values of
6ecb0 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a   the SrcList.a.j
6ecc0 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f  ointype field.*/
6ecd0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45  .#define JT_INNE
6ece0 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20  R     0x0001    
6ecf0 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69  /* Any kind of i
6ed00 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f  nner or cross jo
6ed10 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
6ed20 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30  _CROSS     0x000
6ed30 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74  2    /* Explicit
6ed40 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53   use of the CROS
6ed50 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65  S keyword */.#de
6ed60 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20  fine JT_NATURAL 
6ed70 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54    0x0004    /* T
6ed80 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72  rue for a "natur
6ed90 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66  al" join */.#def
6eda0 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20  ine JT_LEFT     
6edb0 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65   0x0008    /* Le
6edc0 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f  ft outer join */
6edd0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48  .#define JT_RIGH
6ede0 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20  T     0x0010    
6edf0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a  /* Right outer j
6ee00 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a  oin */.#define J
6ee10 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30  T_OUTER     0x00
6ee20 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55  20    /* The "OU
6ee30 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20  TER" keyword is 
6ee40 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69  present */.#defi
6ee50 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20  ne JT_ERROR     
6ee60 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b  0x0040    /* unk
6ee70 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72  nown or unsuppor
6ee80 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f  ted join type */
6ee90 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65  .../*.** A Where
6eea0 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64  Plan object hold
6eeb0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  s information th
6eec0 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c  at describes a l
6eed0 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67  ookup.** strateg
6eee0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62  y..**.** This ob
6eef0 6a 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64  ject is intended
6ef00 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75   to be opaque ou
6ef10 74 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65  tside of the whe
6ef20 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20  re.c module..** 
6ef30 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68  It is included h
6ef40 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74  ere only so that
6ef50 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77   that compiler w
6ef60 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67  ill know how big
6ef70 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65   it.** is.  None
6ef80 20 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69   of the fields i
6ef90 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68  n this object sh
6efa0 6f 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74  ould be used out
6efb0 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77  side of.** the w
6efc0 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a  here.c module..*
6efd0 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20  *.** Within the 
6efe0 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f  union, pIdx is o
6eff0 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73  nly used when ws
6f000 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45  Flags&WHERE_INDE
6f010 58 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20  XED is true..** 
6f020 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73  pTerm is only us
6f030 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26  ed when wsFlags&
6f040 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69  WHERE_MULTI_OR i
6f050 73 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74  s true.  And pVt
6f060 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79  abIdx.** is only
6f070 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61   used when wsFla
6f080 67 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c  gs&WHERE_VIRTUAL
6f090 54 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20  TABLE is true.  
6f0a0 49 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a  It is never the.
6f0b0 2a 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72  ** case that mor
6f0c0 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68  e than one of th
6f0d0 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69  ese conditions i
6f0e0 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63  s true..*/.struc
6f0f0 74 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20  t WherePlan {.  
6f100 75 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20  u32 wsFlags;    
6f110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6f120 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20  * WHERE_* flags 
6f130 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68  that describe th
6f140 65 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20  e strategy */.  
6f150 75 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20  u32 nEq;        
6f160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6f170 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63  * Number of == c
6f180 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
6f190 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65  union {.    Inde
6f1a0 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
6f1b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
6f1c0 64 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49  dex when WHERE_I
6f1d0 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a  NDEXED is true *
6f1e0 2f 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65  /.    struct Whe
6f1f0 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20  reTerm *pTerm;  
6f200 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c       /* WHERE cl
6f210 61 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52  ause term for OR
6f220 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73  -search */.    s
6f230 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
6f240 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a  o *pVtabIdx;  /*
6f250 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   Virtual table i
6f260 6e 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20  ndex to use */. 
6f270 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   } u;.};../*.** 
6f280 46 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20  For each nested 
6f290 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20  loop in a WHERE 
6f2a0 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74  clause implement
6f2b0 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65  ation, the Where
6f2c0 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72  Info.** structur
6f2d0 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  e contains a sin
6f2e0 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  gle instance of 
6f2f0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20  this structure. 
6f300 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   This structure.
6f310 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  ** is intended t
6f320 6f 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65  o be private the
6f330 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64   the where.c mod
6f340 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e  ule and should n
6f350 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20  ot be.** access 
6f360 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f  or modified by o
6f370 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a  ther modules..**
6f380 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f  .** The pIdxInfo
6f390 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74   field is used t
6f3a0 6f 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20  o help pick the 
6f3b0 62 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a  best index on a.
6f3c0 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
6f3d0 2e 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20  .  The pIdxInfo 
6f3e0 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73  pointer contains
6f3f0 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66   indexing.** inf
6f400 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  ormation for the
6f410 20 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74   i-th table in t
6f420 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62  he FROM clause b
6f430 65 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67  efore reordering
6f440 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64  ..** All the pId
6f450 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61  xInfo pointers a
6f460 72 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72  re freed by wher
6f470 65 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77  eInfoFree() in w
6f480 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f  here.c..** All o
6f490 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ther information
6f4a0 20 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65   in the i-th Whe
6f4b0 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66  reLevel object f
6f4c0 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c  or the i-th tabl
6f4d0 65 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20  e.** after FROM 
6f4e0 63 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e  clause ordering.
6f4f0 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65  .*/.struct Where
6f500 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50  Level {.  WhereP
6f510 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20  lan plan;       
6f520 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f  /* query plan fo
6f530 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f  r this element o
6f540 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
6f550 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74  e */.  int iLeft
6f560 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20  Join;        /* 
6f570 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64  Memory cell used
6f580 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45   to implement LE
6f590 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f  FT OUTER JOIN */
6f5a0 0a 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20  .  int iTabCur; 
6f5b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
6f5c0 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64  VDBE cursor used
6f5d0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74   to access the t
6f5e0 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49  able */.  int iI
6f5f0 64 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20  dxCur;          
6f600 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73  /* The VDBE curs
6f610 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  or used to acces
6f620 73 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20  s pIdx */.  int 
6f630 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20  addrBrk;        
6f640 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
6f650 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74  o break out of t
6f660 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  he loop */.  int
6f670 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20   addrNxt;       
6f680 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
6f690 74 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78  to start the nex
6f6a0 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  t IN combination
6f6b0 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f   */.  int addrCo
6f6c0 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a  nt;         /* J
6f6d0 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74  ump here to cont
6f6e0 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65  inue with the ne
6f6f0 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f  xt loop cycle */
6f700 0a 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74  .  int addrFirst
6f710 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  ;        /* Firs
6f720 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66  t instruction of
6f730 20 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65   interior of the
6f740 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46   loop */.  u8 iF
6f750 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  rom;            
6f760 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20   /* Which entry 
6f770 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
6f780 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70  se */.  u8 op, p
6f790 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  5;            /*
6f7a0 20 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f   Opcode and P5 o
6f7b0 66 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61  f the opcode tha
6f7c0 74 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20  t ends the loop 
6f7d0 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b  */.  int p1, p2;
6f7e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
6f7f0 65 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70  erands of the op
6f800 63 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64  code used to end
6f810 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  s the loop */.  
6f820 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20  union {         
6f830 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61        /* Informa
6f840 74 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64  tion that depend
6f850 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67  s on plan.wsFlag
6f860 73 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20  s */.    struct 
6f870 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b  {.      int nIn;
6f880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6f890 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
6f8a0 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20  es in aInLoop[] 
6f8b0 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  */.      struct 
6f8c0 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20  InLoop {.       
6f8d0 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20   int iCur;      
6f8e0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
6f8f0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20  DBE cursor used 
6f900 62 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61  by this IN opera
6f910 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  tor */.        i
6f920 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20  nt addrInTop;   
6f930 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20        /* Top of 
6f940 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20  the IN loop */. 
6f950 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b       } *aInLoop;
6f960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
6f970 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
6f980 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f  each nested IN o
6f990 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d  perator */.    }
6f9a0 20 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20   in;            
6f9b0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
6f9c0 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57  n plan.wsFlags&W
6f9d0 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a  HERE_IN_ABLE */.
6f9e0 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20 20      struct {.   
6f9f0 20 20 20 57 68 65 72 65 50 6c 61 6e 20 2a 61 50     WherePlan *aP
6fa00 6c 61 6e 3b 20 20 20 20 20 2f 2a 20 50 6c 61 6e  lan;     /* Plan
6fa10 73 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20  s for each term 
6fa20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
6fa30 75 73 65 20 2a 2f 0a 20 20 20 20 7d 20 6f 72 3b  use */.    } or;
6fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fa50 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 6c   /* Used when pl
6fa60 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45  an.wsFlags&WHERE
6fa70 5f 4d 55 4c 54 49 5f 4f 52 20 2a 2f 0a 20 20 7d  _MULTI_OR */.  }
6fa80 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f   u;..  /* The fo
6fa90 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 69 73  llowing field is
6faa0 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72 74   really not part
6fab0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
6fac0 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a 2a  level.  But.  **
6fad0 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63 65   we need a place
6fae0 20 74 6f 20 63 61 63 68 65 20 76 69 72 74 75 61   to cache virtua
6faf0 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20 69 6e  l table index in
6fb00 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 65 61  formation for ea
6fb10 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20  ch.  ** virtual 
6fb20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f  table in the FRO
6fb30 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65  M clause and the
6fb40 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74 72 75   WhereLevel stru
6fb50 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20 61 20  cture is.  ** a 
6fb60 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 63 65  convenient place
6fb70 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 20   since there is 
6fb80 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c 20 66  one WhereLevel f
6fb90 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63 6c 61  or each FROM cla
6fba0 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74  use.  ** element
6fbb0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
6fbc0 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64  _index_info *pId
6fbd0 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64 65 78  xInfo;  /* Index
6fbe0 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68 20 73   info for n-th s
6fbf0 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 7d  ource table */.}
6fc00 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 20 61  ;../*.** Flags a
6fc10 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74  ppropriate for t
6fc20 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20 70 61  he wctrlFlags pa
6fc30 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74  rameter of sqlit
6fc40 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 2e 0a  e3WhereBegin()..
6fc50 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
6fc60 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 20  _ORDERBY_NORMAL 
6fc70 20 20 30 78 30 30 30 30 20 2f 2a 20 4e 6f 2d 6f    0x0000 /* No-o
6fc80 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  p */.#define WHE
6fc90 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 20  RE_ORDERBY_MIN  
6fca0 20 20 20 20 30 78 30 30 30 31 20 2f 2a 20 4f 52      0x0001 /* OR
6fcb0 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e  DER BY processin
6fcc0 67 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63  g for min() func
6fcd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
6fce0 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 20 20  E_ORDERBY_MAX   
6fcf0 20 20 20 30 78 30 30 30 32 20 2f 2a 20 4f 52 44     0x0002 /* ORD
6fd00 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67  ER BY processing
6fd10 20 66 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 20   for max() func 
6fd20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
6fd30 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44  _ONEPASS_DESIRED
6fd40 20 20 30 78 30 30 30 34 20 2f 2a 20 57 61 6e 74    0x0004 /* Want
6fd50 20 74 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20   to do one-pass 
6fd60 55 50 44 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f  UPDATE/DELETE */
6fd70 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 46  .#define WHERE_F
6fd80 49 4c 4c 5f 52 4f 57 53 45 54 20 20 20 20 20 20  ILL_ROWSET      
6fd90 30 78 30 30 30 38 20 20 2f 2a 20 53 61 76 65 20  0x0008  /* Save 
6fda0 72 65 73 75 6c 74 73 20 69 6e 20 61 20 52 6f 77  results in a Row
6fdb0 53 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64  Set object */.#d
6fdc0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54  efine WHERE_OMIT
6fdd0 5f 4f 50 45 4e 20 20 20 20 20 20 20 20 30 78 30  _OPEN        0x0
6fde0 30 31 30 20 20 2f 2a 20 54 61 62 6c 65 20 63 75  010  /* Table cu
6fdf0 72 73 6f 72 20 61 72 65 20 61 6c 72 65 61 64 79  rsor are already
6fe00 20 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65   open */.#define
6fe10 20 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53   WHERE_OMIT_CLOS
6fe20 45 20 20 20 20 20 20 20 30 78 30 30 32 30 20 20  E       0x0020  
6fe30 2f 2a 20 4f 6d 69 74 20 63 6c 6f 73 65 20 6f 66  /* Omit close of
6fe40 20 74 61 62 6c 65 20 26 20 69 6e 64 65 78 20 63   table & index c
6fe50 75 72 73 6f 72 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ursors */../*.**
6fe60 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
6fe70 65 20 70 72 6f 63 65 73 73 69 6e 67 20 72 6f 75  e processing rou
6fe80 74 69 6e 65 20 68 61 73 20 74 77 6f 20 68 61 6c  tine has two hal
6fe90 76 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 72  ves.  The.** fir
6fea0 73 74 20 70 61 72 74 20 64 6f 65 73 20 74 68 65  st part does the
6feb0 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 57 48   start of the WH
6fec0 45 52 45 20 6c 6f 6f 70 20 61 6e 64 20 74 68 65  ERE loop and the
6fed0 20 73 65 63 6f 6e 64 0a 2a 2a 20 68 61 6c 66 20   second.** half 
6fee0 64 6f 65 73 20 74 68 65 20 74 61 69 6c 20 6f 66  does the tail of
6fef0 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e   the WHERE loop.
6ff00 20 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66    An instance of
6ff10 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75  .** this structu
6ff20 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 62  re is returned b
6ff30 79 20 74 68 65 20 66 69 72 73 74 20 68 61 6c 66  y the first half
6ff40 20 61 6e 64 20 70 61 73 73 65 64 0a 2a 2a 20 69   and passed.** i
6ff50 6e 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 68  nto the second h
6ff60 61 6c 66 20 74 6f 20 67 69 76 65 20 73 6f 6d 65  alf to give some
6ff70 20 63 6f 6e 74 69 6e 75 69 74 79 2e 0a 2a 2f 0a   continuity..*/.
6ff80 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f  struct WhereInfo
6ff90 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72   {.  Parse *pPar
6ffa0 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se;       /* Par
6ffb0 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65  sing and code ge
6ffc0 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74  nerating context
6ffd0 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c 46   */.  u16 wctrlF
6ffe0 6c 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 46 6c  lags;      /* Fl
6fff0 61 67 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 70  ags originally p
70000 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
70010 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a 2f 0a  WhereBegin() */.
70020 20 20 75 38 20 6f 6b 4f 6e 65 50 61 73 73 3b 20    u8 okOnePass; 
70030 20 20 20 20 20 20 20 2f 2a 20 4f 6b 20 74 6f 20         /* Ok to 
70040 75 73 65 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67  use one-pass alg
70050 6f 72 69 74 68 6d 20 66 6f 72 20 55 50 44 41 54  orithm for UPDAT
70060 45 20 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 20  E or DELETE */. 
70070 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74 3b 20   int regRowSet; 
70080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70090 2f 2a 20 53 74 6f 72 65 20 72 6f 77 69 64 73 20  /* Store rowids 
700a0 69 6e 20 74 68 69 73 20 72 6f 77 73 65 74 20 69  in this rowset i
700b0 66 20 3e 3d 30 20 2a 2f 0a 20 20 53 72 63 4c 69  f >=0 */.  SrcLi
700c0 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20  st *pTabList;   
700d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73            /* Lis
700e0 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74  t of tables in t
700f0 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74  he join */.  int
70100 20 69 54 6f 70 3b 20 20 20 20 20 20 20 20 20 20   iTop;          
70110 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
70120 68 65 20 76 65 72 79 20 62 65 67 69 6e 6e 69 6e  he very beginnin
70130 67 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c  g of the WHERE l
70140 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  oop */.  int iCo
70150 6e 74 69 6e 75 65 3b 20 20 20 20 20 20 20 20 20  ntinue;         
70160 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
70170 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65  here to continue
70180 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f 72   with next recor
70190 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61  d */.  int iBrea
701a0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
701b0 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
701c0 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20  re to break out 
701d0 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
701e0 20 69 6e 74 20 6e 4c 65 76 65 6c 3b 20 20 20 20   int nLevel;    
701f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70200 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73  /* Number of nes
70210 74 65 64 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74  ted loop */.  st
70220 72 75 63 74 20 57 68 65 72 65 43 6c 61 75 73 65  ruct WhereClause
70230 20 2a 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20   *pWC;       /* 
70240 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66  Decomposition of
70250 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
70260 65 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65  e */.  WhereLeve
70270 6c 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20  l a[1];         
70280 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61        /* Informa
70290 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20  tion about each 
702a0 6e 65 73 74 20 6c 6f 6f 70 20 69 6e 20 57 48 45  nest loop in WHE
702b0 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  RE */.};../*.** 
702c0 41 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65  A NameContext de
702d0 66 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20  fines a context 
702e0 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f  in which to reso
702f0 6c 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  lve table and co
70300 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20  lumn.** names.  
70310 54 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73  The context cons
70320 69 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f  ists of a list o
70330 66 20 74 61 62 6c 65 73 20 28 74 68 65 20 70 53  f tables (the pS
70340 72 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e  rcList) field an
70350 64 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e  d.** a list of n
70360 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20  amed expression 
70370 28 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e  (pEList).  The n
70380 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20  amed expression 
70390 6c 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e  list may.** be N
703a0 55 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63  ULL.  The pSrc c
703b0 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68  orresponds to th
703c0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
703d0 20 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20   a SELECT or.** 
703e0 74 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  to the table bei
703f0 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62  ng operated on b
70400 79 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  y INSERT, UPDATE
70410 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68  , or DELETE.  Th
70420 65 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72  e.** pEList corr
70430 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72  esponds to the r
70440 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53  esult set of a S
70450 45 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c  ELECT and is NUL
70460 4c 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73  L for.** other s
70470 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
70480 20 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61   NameContexts ca
70490 6e 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68  n be nested.  Wh
704a0 65 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d  en resolving nam
704b0 65 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f  es, the inner-mo
704c0 73 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69  st .** context i
704d0 73 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74  s searched first
704e0 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69  .  If no match i
704f0 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78  s found, the nex
70500 74 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65  t outer.** conte
70510 78 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20  xt is checked.  
70520 49 66 20 74 68 65 72 65 20 69 73 20 73 74 69 6c  If there is stil
70530 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20  l no match, the 
70540 6e 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20  next context.** 
70550 69 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69  is checked.  Thi
70560 73 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e  s process contin
70570 75 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72  ues until either
70580 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e   a match is foun
70590 64 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74  d.** or all cont
705a0 65 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20  exts are check. 
705b0 20 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73   When a match is
705c0 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66   found, the nRef
705d0 20 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68   member of.** th
705e0 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69  e context contai
705f0 6e 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69  ning the match i
70600 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a  s incremented. .
70610 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75  **.** Each subqu
70620 65 72 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e  ery gets a new N
70630 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65  ameContext.  The
70640 20 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69   pNext field poi
70650 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61  nts to the.** Na
70660 6d 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65  meContext in the
70670 20 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20   parent query.  
70680 54 68 75 73 20 74 68 65 20 70 72 6f 63 65 73 73  Thus the process
70690 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65   of scanning the
706a0 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  .** NameContext 
706b0 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73  list corresponds
706c0 20 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68   to searching th
706d0 72 6f 75 67 68 20 73 75 63 63 65 73 73 69 76 65  rough successive
706e0 6c 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71  ly outer.** subq
706f0 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66  ueries looking f
70700 6f 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73  or a match..*/.s
70710 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78  truct NameContex
70720 74 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  t {.  Parse *pPa
70730 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  rse;       /* Th
70740 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72  e parser */.  Sr
70750 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b  cList *pSrcList;
70760 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72     /* One or mor
70770 65 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f  e tables used to
70780 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a   resolve names *
70790 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
707a0 4c 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69  List;    /* Opti
707b0 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d  onal list of nam
707c0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a  ed expressions *
707d0 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  /.  int nRef;   
707e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
707f0 65 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f  er of names reso
70800 6c 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e  lved by this con
70810 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  text */.  int nE
70820 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  rr;            /
70830 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  * Number of erro
70840 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77  rs encountered w
70850 68 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e  hile resolving n
70860 61 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c  ames */.  u8 all
70870 6f 77 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f  owAgg;         /
70880 2a 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63  * Aggregate func
70890 74 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65  tions allowed he
708a0 72 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67  re */.  u8 hasAg
708b0 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g;           /* 
708c0 54 72 75 65 20 69 66 20 61 67 67 72 65 67 61 74  True if aggregat
708d0 65 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20  es are seen */. 
708e0 20 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20   u8 isCheck;    
708f0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
70900 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73   resolving names
70910 20 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73   in a CHECK cons
70920 74 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  traint */.  int 
70930 6e 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20  nDepth;         
70940 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62   /* Depth of sub
70950 71 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e  query recursion.
70960 20 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73   1 for no recurs
70970 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f  ion */.  AggInfo
70980 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a   *pAggInfo;   /*
70990 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
709a0 75 74 20 61 67 67 72 65 67 61 74 65 73 20 61 74  ut aggregates at
709b0 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20   this level */. 
709c0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
709d0 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75  ext;  /* Next ou
709e0 74 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74  ter name context
709f0 2e 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65  .  NULL for oute
70a00 72 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  rmost */.};../*.
70a10 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
70a20 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
70a30 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
70a40 6e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69  ns all informati
70a50 6f 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20  on.** needed to 
70a60 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  generate code fo
70a70 72 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43  r a single SELEC
70a80 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  T statement..**.
70a90 2a 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74  ** nLimit is set
70aa0 20 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20   to -1 if there 
70ab0 69 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75  is no LIMIT clau
70ac0 73 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20  se.  nOffset is 
70ad0 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20  set to 0..** If 
70ae0 74 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54  there is a LIMIT
70af0 20 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72   clause, the par
70b00 73 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20  ser sets nLimit 
70b10 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  to the value of 
70b20 74 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64  the.** limit and
70b30 20 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20   nOffset to the 
70b40 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66  value of the off
70b50 73 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65  set (or 0 if the
70b60 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66  re is not.** off
70b70 73 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72  set).  But later
70b80 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20   on, nLimit and 
70b90 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74  nOffset become t
70ba0 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
70bb0 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44  ons.** in the VD
70bc0 42 45 20 74 68 61 74 20 72 65 63 6f 72 64 20 74  BE that record t
70bd0 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66  he limit and off
70be0 73 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a  set counters..**
70bf0 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d  .** addrOpenEphm
70c00 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61  [] entries conta
70c10 69 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f  in the address o
70c20 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  f OP_OpenEphemer
70c30 61 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54  al opcodes..** T
70c40 68 65 73 65 20 61 64 64 72 65 73 73 65 73 20 6d  hese addresses m
70c50 75 73 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f  ust be stored so
70c60 20 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20   that we can go 
70c70 62 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e  back and fill in
70c80 0a 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e  .** the P4_KEYIN
70c90 46 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65  FO and P2 parame
70ca0 74 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69  ters later.  Nei
70cb0 74 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f  ther the KeyInfo
70cc0 20 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62   nor.** the numb
70cd0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
70ce0 20 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75   P2 can be compu
70cf0 74 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20  ted at the same 
70d00 74 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f  time.** as the O
70d10 50 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72  P_OpenEphm instr
70d20 75 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20  uction is coded 
70d30 62 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65  because not.** e
70d40 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f  nough informatio
70d50 6e 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70  n about the comp
70d60 6f 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e  ound query is kn
70d70 6f 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e  own at that poin
70d80 74 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66  t..** The KeyInf
70d90 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72  o for addrOpenTr
70da0 61 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f  an[0] and [1] co
70db0 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67  ntains collating
70dc0 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f   sequences.** fo
70dd0 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  r the result set
70de0 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66  .  The KeyInfo f
70df0 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b  or addrOpenTran[
70e00 32 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c  2] contains coll
70e10 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63  ating.** sequenc
70e20 65 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52  es for the ORDER
70e30 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73   BY clause..*/.s
70e40 74 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20  truct Select {. 
70e50 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
70e60 74 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66  t;      /* The f
70e70 69 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73  ields of the res
70e80 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20  ult */.  u8 op; 
70e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70ea0 2f 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e  /* One of: TK_UN
70eb0 49 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e  ION TK_ALL TK_IN
70ec0 54 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50  TERSECT TK_EXCEP
70ed0 54 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69  T */.  char affi
70ee0 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a  nity;         /*
70ef0 20 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68   MakeRecord with
70f00 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 66   this affinity f
70f10 6f 72 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20  or SRT_Set */.  
70f20 75 31 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20  u16 selFlags;   
70f30 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75         /* Variou
70f40 73 20 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f  s SF_* values */
70f50 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
70f60 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
70f70 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
70f80 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20    Expr *pWhere; 
70f90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
70fa0 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
70fb0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f    ExprList *pGro
70fc0 75 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20  upBy;    /* The 
70fd0 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
70fe0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69  */.  Expr *pHavi
70ff0 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ng;         /* T
71000 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65  he HAVING clause
71010 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
71020 70 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20  pOrderBy;    /* 
71030 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
71040 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  use */.  Select 
71050 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20  *pPrior;        
71060 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20  /* Prior select 
71070 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  in a compound se
71080 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a  lect statement *
71090 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78  /.  Select *pNex
710a0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  t;         /* Ne
710b0 78 74 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65  xt select to the
710c0 20 6c 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f   left in a compo
710d0 75 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  und */.  Select 
710e0 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20  *pRightmost;    
710f0 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65  /* Right-most se
71100 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75  lect in a compou
71110 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d  nd select statem
71120 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ent */.  Expr *p
71130 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20  Limit;          
71140 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73  /* LIMIT express
71150 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20  ion. NULL means 
71160 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45  not used. */.  E
71170 78 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20  xpr *pOffset;   
71180 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20        /* OFFSET 
71190 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c  expression. NULL
711a0 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e   means not used.
711b0 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74   */.  int iLimit
711c0 2c 20 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20  , iOffset;   /* 
711d0 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73  Memory registers
711e0 20 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26   holding LIMIT &
711f0 20 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73   OFFSET counters
71200 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70   */.  int addrOp
71210 65 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20  enEphm[3];   /* 
71220 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63  OP_OpenEphem opc
71230 6f 64 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20  odes related to 
71240 74 68 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d  this select */.}
71250 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
71260 20 76 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65   values for Sele
71270 63 74 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68  ct.selFlags.  Th
71280 65 20 22 53 46 22 20 70 72 65 66 69 78 20 73 74  e "SF" prefix st
71290 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c  ands for.** "Sel
712a0 65 63 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64  ect Flag"..*/.#d
712b0 65 66 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63  efine SF_Distinc
712c0 74 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20  t        0x0001 
712d0 20 2f 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c   /* Output shoul
712e0 64 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f  d be DISTINCT */
712f0 0a 23 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f  .#define SF_Reso
71300 6c 76 65 64 20 20 20 20 20 20 20 20 30 78 30 30  lved        0x00
71310 30 32 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65  02  /* Identifie
71320 72 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73  rs have been res
71330 6f 6c 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  olved */.#define
71340 20 53 46 5f 41 67 67 72 65 67 61 74 65 20 20 20   SF_Aggregate   
71350 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43      0x0004  /* C
71360 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74  ontains aggregat
71370 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23  e functions */.#
71380 64 65 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70  define SF_UsesEp
71390 68 65 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38  hemeral   0x0008
713a0 20 20 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70    /* Uses the Op
713b0 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f  enEphemeral opco
713c0 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46  de */.#define SF
713d0 5f 45 78 70 61 6e 64 65 64 20 20 20 20 20 20 20  _Expanded       
713e0 20 30 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69   0x0010  /* sqli
713f0 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
71400 29 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73  ) called on this
71410 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48   */.#define SF_H
71420 61 73 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30  asTypeInfo     0
71430 78 30 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73  x0020  /* FROM s
71440 75 62 71 75 65 72 69 65 73 20 68 61 76 65 20 54  ubqueries have T
71450 61 62 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f  able metadata */
71460 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73  .../*.** The res
71470 75 6c 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74  ults of a select
71480 20 63 61 6e 20 62 65 20 64 69 73 74 72 69 62 75   can be distribu
71490 74 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77  ted in several w
714a0 61 79 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52  ays.  The.** "SR
714b0 54 22 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20  T" prefix means 
714c0 22 53 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54  "SELECT Result T
714d0 79 70 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ype"..*/.#define
714e0 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20   SRT_Union      
714f0 20 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65    1  /* Store re
71500 73 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20  sult as keys in 
71510 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66  an index */.#def
71520 69 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20 20  ine SRT_Except  
71530 20 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76       2  /* Remov
71540 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20  e result from a 
71550 55 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23  UNION index */.#
71560 64 65 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74  define SRT_Exist
71570 73 20 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74  s       3  /* St
71580 6f 72 65 20 31 20 69 66 20 74 68 65 20 72 65 73  ore 1 if the res
71590 75 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79  ult is not empty
715a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
715b0 44 69 73 63 61 72 64 20 20 20 20 20 20 34 20 20  Discard      4  
715c0 2f 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74  /* Do not save t
715d0 68 65 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68  he results anywh
715e0 65 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f  ere */../* The O
715f0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69  RDER BY clause i
71600 73 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c  s ignored for al
71610 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a  l of the above *
71620 2f 0a 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61  /.#define Ignora
71630 62 6c 65 4f 72 64 65 72 62 79 28 58 29 20 28 28  bleOrderby(X) ((
71640 58 2d 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44  X->eDest)<=SRT_D
71650 69 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65  iscard)..#define
71660 20 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20   SRT_Output     
71670 20 20 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65    5  /* Output e
71680 61 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c  ach row of resul
71690 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  t */.#define SRT
716a0 5f 4d 65 6d 20 20 20 20 20 20 20 20 20 20 36 20  _Mem          6 
716b0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74   /* Store result
716c0 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c   in a memory cel
716d0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  l */.#define SRT
716e0 5f 53 65 74 20 20 20 20 20 20 20 20 20 20 37 20  _Set          7 
716f0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74   /* Store result
71700 73 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20  s as keys in an 
71710 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65  index */.#define
71720 20 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20   SRT_Table      
71730 20 20 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65    8  /* Store re
71740 73 75 6c 74 20 61 73 20 64 61 74 61 20 77 69 74  sult as data wit
71750 68 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72  h an automatic r
71760 6f 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  owid */.#define 
71770 53 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20  SRT_EphemTab    
71780 20 39 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72   9  /* Create tr
71790 61 6e 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20  ansient tab and 
717a0 73 74 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54  store like SRT_T
717b0 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  able */.#define 
717c0 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20  SRT_Coroutine   
717d0 31 30 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  10  /* Generate 
717e0 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20  a single row of 
717f0 72 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  result */../*.**
71800 20 41 20 73 74 72 75 63 74 75 72 65 20 75 73 65   A structure use
71810 64 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74  d to customize t
71820 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 73  he behavior of s
71830 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20  qlite3Select(). 
71840 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20  See.** comments 
71850 61 62 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c  above sqlite3Sel
71860 65 63 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c  ect() for detail
71870 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
71880 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 20  ruct SelectDest 
71890 53 65 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75  SelectDest;.stru
718a0 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a  ct SelectDest {.
718b0 20 20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20    u8 eDest;     
718c0 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69      /* How to di
718d0 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73  spose of the res
718e0 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66  ults */.  u8 aff
718f0 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41  inity;      /* A
71900 66 66 69 6e 69 74 79 20 75 73 65 64 20 77 68 65  ffinity used whe
71910 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74  n eDest==SRT_Set
71920 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b   */.  int iParm;
71930 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72          /* A par
71940 61 6d 65 74 65 72 20 75 73 65 64 20 62 79 20 74  ameter used by t
71950 68 65 20 65 44 65 73 74 20 64 69 73 70 6f 73 61  he eDest disposa
71960 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e  l method */.  in
71970 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t iMem;         
71980 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72  /* Base register
71990 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61   where results a
719a0 72 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  re written */.  
719b0 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20  int nMem;       
719c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
719d0 65 67 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74  egisters allocat
719e0 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ed */.};../*.** 
719f0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f  An SQL parser co
71a00 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f  ntext.  A copy o
71a10 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
71a20 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75   is passed throu
71a30 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72  gh.** the parser
71a40 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61   and down into a
71a50 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63  ll the parser ac
71a60 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20  tion routine in 
71a70 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72  order to.** carr
71a80 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61  y around informa
71a90 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f  tion that is glo
71aa0 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72  bal to the entir
71ab0 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  e parse..**.** T
71ac0 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  he structure is 
71ad0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f  divided into two
71ae0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68   parts.  When th
71af0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64  e parser and cod
71b00 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61  e.** generate ca
71b10 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65  ll themselves re
71b20 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66  cursively, the f
71b30 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65  irst part of the
71b40 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73   structure.** is
71b50 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68   constant but th
71b60 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73  e second part is
71b70 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65   reset at the be
71b80 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20  ginning and end 
71b90 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72  of.** each recur
71ba0 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sion..**.** The 
71bb0 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61  nTableLock and a
71bc0 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62  TableLock variab
71bd0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  les are only use
71be0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d  d if the shared-
71bf0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72  cache .** featur
71c00 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66  e is enabled (if
71c10 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75   sqlite3Tsd()->u
71c20 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20  seSharedData is 
71c30 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a  true). They are.
71c40 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ** used to store
71c50 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c   the set of tabl
71c60 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64  e-locks required
71c70 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e   by the statemen
71c80 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69  t being.** compi
71c90 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71  led. Function sq
71ca0 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29  lite3TableLock()
71cb0 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
71cc0 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a  entries to the.*
71cd0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63  * list..*/.struc
71ce0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69  t Parse {.  sqli
71cf0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
71d00 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74   /* The main dat
71d10 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20  abase structure 
71d20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
71d30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
71d40 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78  urn code from ex
71d50 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61  ecution */.  cha
71d60 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
71d70 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65    /* An error me
71d80 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20  ssage */.  Vdbe 
71d90 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20  *pVdbe;         
71da0 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72  /* An engine for
71db0 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62   executing datab
71dc0 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a  ase bytecode */.
71dd0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74    u8 colNamesSet
71de0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61  ;      /* TRUE a
71df0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61  fter OP_ColumnNa
71e00 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75  me has been issu
71e10 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20  ed to pVdbe */. 
71e20 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20   u8 nameClash;  
71e30 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61        /* A perma
71e40 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20  nent table name 
71e50 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d  clashes with tem
71e60 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  p table name */.
71e70 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61    u8 checkSchema
71e80 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73  ;      /* Causes
71e90 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63   schema cookie c
71ea0 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72  heck after an er
71eb0 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74  ror */.  u8 nest
71ec0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ed;           /*
71ed0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
71ee0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70  d calls to the p
71ef0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72  arser/code gener
71f00 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72  ator */.  u8 par
71f10 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f  seError;       /
71f20 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70  * True after a p
71f30 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54  arsing error.  T
71f40 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20  icket #1794 */. 
71f50 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20   u8 nTempReg;   
71f60 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
71f70 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  of temporary reg
71f80 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52  isters in aTempR
71f90 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65  eg[] */.  u8 nTe
71fa0 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f  mpInUse;       /
71fb0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d  * Number of aTem
71fc0 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79  pReg[] currently
71fd0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
71fe0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38    int aTempReg[8
71ff0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e  ];     /* Holdin
72000 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f  g area for tempo
72010 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a  rary registers *
72020 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65  /.  int nRangeRe
72030 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  g;       /* Size
72040 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72   of the temporar
72050 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b  y register block
72060 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65   */.  int iRange
72070 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69  Reg;       /* Fi
72080 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20  rst register in 
72090 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
720a0 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  er block */.  in
720b0 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20  t nErr;         
720c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
720d0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20  errors seen */. 
720e0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20   int nTab;      
720f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
72100 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  of previously al
72110 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72  located VDBE cur
72120 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  sors */.  int nM
72130 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
72140 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
72150 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f  ry cells used so
72160 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53   far */.  int nS
72170 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  et;            /
72180 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73  * Number of sets
72190 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a   used so far */.
721a0 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20    int ckBase;   
721b0 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
721c0 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20  egister of data 
721d0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e  during check con
721e0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  straints */.  in
721f0 74 20 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  t disableColCach
72200 65 3b 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69  e; /* True to di
72210 73 61 62 6c 65 20 61 64 64 69 6e 67 20 74 6f 20  sable adding to 
72220 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a  column cache */.
72230 20 20 69 6e 74 20 6e 43 6f 6c 43 61 63 68 65 3b    int nColCache;
72240 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
72250 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
72260 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20  he column cache 
72270 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 43 61 63  */.  int iColCac
72280 68 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  he;       /* Nex
72290 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63  t entry of the c
722a0 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63 65 20  ache to replace 
722b0 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  */.  struct yCol
722c0 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e 74 20  Cache {.    int 
722d0 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  iTable;         
722e0 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f    /* Table curso
722f0 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  r number */.    
72300 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
72310 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63        /* Table c
72320 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  olumn number */.
72330 20 20 20 20 63 68 61 72 20 61 66 66 43 68 61 6e      char affChan
72340 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ge;       /* Tru
72350 65 20 69 66 20 74 68 69 73 20 72 65 67 69 73 74  e if this regist
72360 65 72 20 68 61 73 20 68 61 64 20 61 6e 20 61 66  er has had an af
72370 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 2a 2f  finity change */
72380 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20  .    int iReg;  
72390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
723a0 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 76  gister holding v
723b0 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c  alue of this col
723c0 75 6d 6e 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43  umn */.  } aColC
723d0 61 63 68 65 5b 31 30 5d 3b 20 20 20 20 20 2f 2a  ache[10];     /*
723e0 20 4f 6e 65 20 66 6f 72 20 65 61 63 68 20 76 61   One for each va
723f0 6c 69 64 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  lid column cache
72400 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 20   entry */.  u32 
72410 77 72 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 20  writeMask;      
72420 20 2f 2a 20 53 74 61 72 74 20 61 20 77 72 69 74   /* Start a writ
72430 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  e transaction on
72440 20 74 68 65 73 65 20 64 61 74 61 62 61 73 65 73   these databases
72450 20 2a 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65   */.  u32 cookie
72460 4d 61 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69  Mask;      /* Bi
72470 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 20  tmask of schema 
72480 76 65 72 69 66 69 65 64 20 64 61 74 61 62 61 73  verified databas
72490 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b  es */.  int cook
724a0 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20  ieGoto;      /* 
724b0 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f  Address of OP_Go
724c0 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72  to to cookie ver
724d0 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65  ifier subroutine
724e0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65   */.  int cookie
724f0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58  Value[SQLITE_MAX
72500 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f  _ATTACHED+2];  /
72510 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b  * Values of cook
72520 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f  ies to verify */
72530 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
72540 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
72550 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f  E.  int nTableLo
72560 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ck;        /* Nu
72570 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e  mber of locks in
72580 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20   aTableLock */. 
72590 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62   TableLock *aTab
725a0 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69  leLock; /* Requi
725b0 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20  red table locks 
725c0 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65  for shared-cache
725d0 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a   mode */.#endif.
725e0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20    int regRowid; 
725f0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
72600 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64  er holding rowid
72610 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45   of CREATE TABLE
72620 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20   entry */.  int 
72630 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20  regRoot;        
72640 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
72650 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e  ding root page n
72660 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62  umber for new ob
72670 6a 65 63 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41  jects */..  /* A
72680 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e 74  bove is constant
72690 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73 69   between recursi
726a0 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 72  ons.  Below is r
726b0 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64 20  eset before and 
726c0 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 20  after.  ** each 
726d0 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20  recursion */..  
726e0 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20  int nVar;       
726f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
72700 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73 20  f '?' variables 
72710 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c 20  seen in the SQL 
72720 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20  so far */.  int 
72730 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20 20  nVarExpr;       
72740 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73   /* Number of us
72750 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61  ed slots in apVa
72760 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74  rExpr[] */.  int
72770 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20   nVarExprAlloc; 
72780 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
72790 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 69  llocated slots i
727a0 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f  n apVarExpr[] */
727b0 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72 45  .  Expr **apVarE
727c0 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74  xpr;    /* Point
727d0 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 20  ers to :aaa and 
727e0 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20 65  $aaaa wildcard e
727f0 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20  xpressions */.  
72800 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20 20  int nAlias;     
72810 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
72820 66 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74  f aliased result
72830 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a   set columns */.
72840 20 20 69 6e 74 20 6e 41 6c 69 61 73 41 6c 6c 6f    int nAliasAllo
72850 63 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  c;     /* Number
72860 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c   of allocated sl
72870 6f 74 73 20 66 6f 72 20 61 41 6c 69 61 73 5b 5d  ots for aAlias[]
72880 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 41 6c 69 61   */.  int *aAlia
72890 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  s;         /* Re
728a0 67 69 73 74 65 72 20 75 73 65 64 20 74 6f 20 68  gister used to h
728b0 6f 6c 64 20 61 6c 69 61 73 65 64 20 72 65 73 75  old aliased resu
728c0 6c 74 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61  lt */.  u8 expla
728d0 69 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  in;          /* 
728e0 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50 4c  True if the EXPL
728f0 41 49 4e 20 66 6c 61 67 20 69 73 20 66 6f 75 6e  AIN flag is foun
72900 64 20 6f 6e 20 74 68 65 20 71 75 65 72 79 20 2a  d on the query *
72910 2f 0a 20 20 54 6f 6b 65 6e 20 73 45 72 72 54 6f  /.  Token sErrTo
72920 6b 65 6e 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  ken;     /* The 
72930 74 6f 6b 65 6e 20 61 74 20 77 68 69 63 68 20 74  token at which t
72940 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65  he error occurre
72950 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4e 61  d */.  Token sNa
72960 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54  meToken;    /* T
72970 6f 6b 65 6e 20 77 69 74 68 20 75 6e 71 75 61 6c  oken with unqual
72980 69 66 69 65 64 20 73 63 68 65 6d 61 20 6f 62 6a  ified schema obj
72990 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f  ect name */.  To
729a0 6b 65 6e 20 73 4c 61 73 74 54 6f 6b 65 6e 3b 20  ken sLastToken; 
729b0 20 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 74     /* The last t
729c0 6f 6b 65 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20  oken parsed */. 
729d0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
729e0 6c 3b 20 20 20 20 2f 2a 20 41 6c 6c 20 53 51 4c  l;    /* All SQL
729f0 20 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74   text */.  const
72a00 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20   char *zTail;   
72a10 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20  /* All SQL text 
72a20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65  past the last se
72a30 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a  micolon parsed *
72a40 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54  /.  Table *pNewT
72a50 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61  able;    /* A ta
72a60 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72  ble being constr
72a70 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45 20  ucted by CREATE 
72a80 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67  TABLE */.  Trigg
72a90 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b  er *pNewTrigger;
72aa0 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20       /* Trigger 
72ab0 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20  under construct 
72ac0 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49 47  by a CREATE TRIG
72ad0 47 45 52 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  GER */.  Trigger
72ae0 53 74 61 63 6b 20 2a 74 72 69 67 53 74 61 63 6b  Stack *trigStack
72af0 3b 20 20 2f 2a 20 54 72 69 67 67 65 72 20 61 63  ;  /* Trigger ac
72b00 74 69 6f 6e 73 20 62 65 69 6e 67 20 63 6f 64 65  tions being code
72b10 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
72b20 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b  r *zAuthContext;
72b30 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61 72 61   /* The 6th para
72b40 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 41 75  meter to db->xAu
72b50 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a  th callbacks */.
72b60 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
72b70 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
72b80 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b 20 20  .  Token sArg;  
72b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
72ba0 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f   Complete text o
72bb0 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d  f a module argum
72bc0 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 63 6c  ent */.  u8 decl
72bd0 61 72 65 56 74 61 62 3b 20 20 20 20 20 20 20 20  areVtab;        
72be0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
72bf0 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f 64 65  nside sqlite3_de
72c00 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a 2f 0a  clare_vtab() */.
72c10 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 6b 3b    int nVtabLock;
72c20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
72c30 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74 75 61  Number of virtua
72c40 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f 63 6b  l tables to lock
72c50 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70   */.  Table **ap
72c60 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20  VtabLock;       
72c70 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
72c80 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6e 65  irtual tables ne
72c90 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f  eding locking */
72ca0 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e 48  .#endif.  int nH
72cb0 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20  eight;          
72cc0 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
72cd0 74 72 65 65 20 68 65 69 67 68 74 20 6f 66 20 63  tree height of c
72ce0 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65 63  urrent sub-selec
72cf0 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 5a  t */.  Table *pZ
72d00 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20 20 2f  ombieTab;      /
72d10 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65 20  * List of Table 
72d20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65 74  objects to delet
72d30 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 65 6e  e after code gen
72d40 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53   */.};..#ifdef S
72d50 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
72d60 41 4c 54 41 42 4c 45 0a 20 20 23 64 65 66 69 6e  ALTABLE.  #defin
72d70 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41  e IN_DECLARE_VTA
72d80 42 20 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  B 0.#else.  #def
72d90 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56  ine IN_DECLARE_V
72da0 54 41 42 20 28 70 50 61 72 73 65 2d 3e 64 65 63  TAB (pParse->dec
72db0 6c 61 72 65 56 74 61 62 29 0a 23 65 6e 64 69 66  lareVtab).#endif
72dc0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
72dd0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
72de0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63  wing structure c
72df0 61 6e 20 62 65 20 64 65 63 6c 61 72 65 64 20 6f  an be declared o
72e00 6e 20 61 20 73 74 61 63 6b 20 61 6e 64 20 75 73  n a stack and us
72e10 65 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 68  ed.** to save th
72e20 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e  e Parse.zAuthCon
72e30 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20 74 68  text value so th
72e40 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 73  at it can be res
72e50 74 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a  tored later..*/.
72e60 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65  struct AuthConte
72e70 78 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  xt {.  const cha
72e80 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b  r *zAuthContext;
72e90 20 20 20 2f 2a 20 50 75 74 20 73 61 76 65 64 20     /* Put saved 
72ea0 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65  Parse.zAuthConte
72eb0 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 72  xt here */.  Par
72ec0 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
72ed0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
72ee0 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20  Parse structure 
72ef0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74  */.};../*.** Bit
72f00 66 69 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 20  field flags for 
72f10 50 32 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f 49  P2 value in OP_I
72f20 6e 73 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 6c  nsert and OP_Del
72f30 65 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  ete.*/.#define O
72f40 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 20  PFLAG_NCHANGE   
72f50 31 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75  1    /* Set to u
72f60 70 64 61 74 65 20 64 62 2d 3e 6e 43 68 61 6e 67  pdate db->nChang
72f70 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  e */.#define OPF
72f80 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 32 20  LAG_LASTROWID 2 
72f90 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64     /* Set to upd
72fa0 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69  ate db->lastRowi
72fb0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  d */.#define OPF
72fc0 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20 34 20  LAG_ISUPDATE  4 
72fd0 20 20 20 2f 2a 20 54 68 69 73 20 4f 50 5f 49 6e     /* This OP_In
72fe0 73 65 72 74 20 69 73 20 61 6e 20 73 71 6c 20 55  sert is an sql U
72ff0 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65  PDATE */.#define
73000 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 20 20   OPFLAG_APPEND  
73010 20 20 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69    8    /* This i
73020 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61  s likely to be a
73030 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 0a 2f 2a 0a  n append */../*.
73040 20 2a 20 45 61 63 68 20 74 72 69 67 67 65 72 20   * Each trigger 
73050 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64  present in the d
73060 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69  atabase schema i
73070 73 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20 69  s stored as an i
73080 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73 74  nstance of. * st
73090 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a 20  ruct Trigger. . 
730a0 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f  *. * Pointers to
730b0 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74   instances of st
730c0 72 75 63 74 20 54 72 69 67 67 65 72 20 61 72 65  ruct Trigger are
730d0 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20 77   stored in two w
730e0 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 68  ays.. * 1. In th
730f0 65 20 22 74 72 69 67 48 61 73 68 22 20 68 61 73  e "trigHash" has
73100 68 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f 66  h table (part of
73110 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74 68   the sqlite3* th
73120 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68  at represents th
73130 65 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61 73  e . *    databas
73140 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20  e). This allows 
73150 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75 72  Trigger structur
73160 65 73 20 74 6f 20 62 65 20 72 65 74 72 69 65 76  es to be retriev
73170 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 32  ed by name.. * 2
73180 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 61  . All triggers a
73190 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
731a0 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66 6f   single table fo
731b0 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  rm a linked list
731c0 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20 20  , using the. *  
731d0 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20 6f    pNext member o
731e0 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72  f struct Trigger
731f0 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  . A pointer to t
73200 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
73210 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c 69   of the. *    li
73220 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74 6f  nked list is sto
73230 72 65 64 20 61 73 20 74 68 65 20 22 70 54 72 69  red as the "pTri
73240 67 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66 20  gger" member of 
73250 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20  the associated. 
73260 2a 20 20 20 20 73 74 72 75 63 74 20 54 61 62 6c  *    struct Tabl
73270 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73 74  e.. *. * The "st
73280 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20  ep_list" member 
73290 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69  points to the fi
732a0 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61  rst element of a
732b0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a 20   linked list. * 
732c0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 53  containing the S
732d0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73 70  QL statements sp
732e0 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20 74  ecified as the t
732f0 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a  rigger program..
73300 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67   */.struct Trigg
73310 65 72 20 7b 0a 20 20 63 68 61 72 20 2a 6e 61 6d  er {.  char *nam
73320 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
73330 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
73340 65 20 74 72 69 67 67 65 72 20 20 20 20 20 20 20  e trigger       
73350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73360 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62 6c   */.  char *tabl
73370 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
73380 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69   The table or vi
73390 65 77 20 74 6f 20 77 68 69 63 68 20 74 68 65 20  ew to which the 
733a0 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 20  trigger applies 
733b0 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20  */.  u8 op;     
733c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
733d0 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45  One of TK_DELETE
733e0 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f  , TK_UPDATE, TK_
733f0 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 2a  INSERT         *
73400 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20 20  /.  u8 tr_tm;   
73410 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
73420 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45  ne of TRIGGER_BE
73430 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46  FORE, TRIGGER_AF
73440 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  TER */.  Expr *p
73450 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  When;           
73460 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c 61   /* The WHEN cla
73470 75 73 65 20 6f 66 20 74 68 65 20 65 78 70 72 65  use of the expre
73480 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e 55  ssion (may be NU
73490 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20  LL) */.  IdList 
734a0 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20 20  *pColumns;      
734b0 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
734c0 6e 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f 6c  n UPDATE OF <col
734d0 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67 65  umn-list> trigge
734e0 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
734f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73500 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74  the <column-list
73510 3e 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65  > is stored here
73520 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 6e 61 6d 65   */.  Token name
73530 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 2f 2a  Token;        /*
73540 20 54 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 69 6e   Token containin
73550 67 20 7a 4e 61 6d 65 2e 20 55 73 65 20 64 75 72  g zName. Use dur
73560 69 6e 67 20 70 61 72 73 69 6e 67 20 6f 6e 6c 79  ing parsing only
73570 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53   */.  Schema *pS
73580 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a  chema;        /*
73590 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69   Schema containi
735a0 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20 2a  ng the trigger *
735b0 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61 62  /.  Schema *pTab
735c0 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53  Schema;     /* S
735d0 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67  chema containing
735e0 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
735f0 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 74 65  TriggerStep *ste
73600 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b 20  p_list; /* Link 
73610 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 20  list of trigger 
73620 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20 20  program steps   
73630 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 54            */.  T
73640 72 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20 20  rigger *pNext;  
73650 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 74         /* Next t
73660 72 69 67 67 65 72 20 61 73 73 6f 63 69 61 74 65  rigger associate
73670 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65  d with the table
73680 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
73690 74 72 69 67 67 65 72 20 69 73 20 65 69 74 68 65  trigger is eithe
736a0 72 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61 6e  r a BEFORE or an
736b0 20 41 46 54 45 52 20 74 72 69 67 67 65 72 2e 20   AFTER trigger. 
736c0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
736d0 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74 65  onstants.** dete
736e0 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a 2a  rmine which. .**
736f0 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65  .** If there are
73700 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67 65   multiple trigge
73710 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f 66  rs, you might of
73720 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e 64   some BEFORE and
73730 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a 20   some AFTER..** 
73740 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20 74  In that cases, t
73750 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c  he constants bel
73760 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20 74  ow can be ORed t
73770 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 66  ogether..*/.#def
73780 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46 4f  ine TRIGGER_BEFO
73790 52 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52  RE  1.#define TR
737a0 49 47 47 45 52 5f 41 46 54 45 52 20 20 20 32 0a  IGGER_AFTER   2.
737b0 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*. * An instan
737c0 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 69  ce of struct Tri
737d0 67 67 65 72 53 74 65 70 20 69 73 20 75 73 65 64  ggerStep is used
737e0 20 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e 67   to store a sing
737f0 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  le SQL statement
73800 0a 20 2a 20 74 68 61 74 20 69 73 20 61 20 70 61  . * that is a pa
73810 72 74 20 6f 66 20 61 20 74 72 69 67 67 65 72 2d  rt of a trigger-
73820 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a 20  program. . *. * 
73830 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72  Instances of str
73840 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20  uct TriggerStep 
73850 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20  are stored in a 
73860 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c 69  singly linked li
73870 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75 73  st (linked. * us
73880 69 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22 20  ing the "pNext" 
73890 6d 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e 63  member) referenc
738a0 65 64 20 62 79 20 74 68 65 20 22 73 74 65 70 5f  ed by the "step_
738b0 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66 20  list" member of 
738c0 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61 74  the . * associat
738d0 65 64 20 73 74 72 75 63 74 20 54 72 69 67 67 65  ed struct Trigge
738e0 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 20  r instance. The 
738f0 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  first element of
73900 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
73910 20 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73 74   is. * the first
73920 20 73 74 65 70 20 6f 66 20 74 68 65 20 74 72 69   step of the tri
73930 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20 2a  gger-program.. *
73940 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d 65   . * The "op" me
73950 6d 62 65 72 20 69 6e 64 69 63 61 74 65 73 20 77  mber indicates w
73960 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61  hether this is a
73970 20 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 45   "DELETE", "INSE
73980 52 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f 72  RT", "UPDATE" or
73990 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74 61  . * "SELECT" sta
739a0 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 6e  tement. The mean
739b0 69 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68 65  ings of the othe
739c0 72 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65 74  r members is det
739d0 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 0a  ermined by the .
739e0 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 22   * value of "op"
739f0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a   as follows:. *.
73a00 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53   * (op == TK_INS
73a10 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20 20  ERT). * orconf  
73a20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65 20    -> stores the 
73a30 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f  ON CONFLICT algo
73a40 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63 74  rithm. * pSelect
73a50 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73     -> If this is
73a60 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20   an INSERT INTO 
73a70 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 73  ... SELECT ... s
73a80 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20  tatement, then. 
73a90 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74  *              t
73aa0 68 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f 69  his stores a poi
73ab0 6e 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c 45  nter to the SELE
73ac0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f 74  CT statement. Ot
73ad0 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a  herwise NULL.. *
73ae0 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20   target    -> A 
73af0 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68  token holding th
73b00 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
73b10 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e  ble to insert in
73b20 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74  to.. * pExprList
73b30 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 61   -> If this is a
73b40 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e  n INSERT INTO ..
73b50 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20 73 74 61  . VALUES ... sta
73b60 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20  tement, then. * 
73b70 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 69               thi
73b80 73 20 73 74 6f 72 65 73 20 76 61 6c 75 65 73 20  s stores values 
73b90 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20  to be inserted. 
73ba0 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a  Otherwise NULL..
73bb0 20 2a 20 70 49 64 4c 69 73 74 20 20 20 2d 3e 20   * pIdList   -> 
73bc0 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e  If this is an IN
73bd0 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 28 3c  SERT INTO ... (<
73be0 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e 29 20 56  column-names>) V
73bf0 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a 20 20 20  ALUES ... . *   
73c00 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65             state
73c10 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20  ment, then this 
73c20 73 74 6f 72 65 73 20 74 68 65 20 63 6f 6c 75 6d  stores the colum
73c30 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 0a 20 2a  n-names to be. *
73c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
73c50 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a 20 2a 0a  serted into.. *.
73c60 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 44 45 4c   * (op == TK_DEL
73c70 45 54 45 29 0a 20 2a 20 74 61 72 67 65 74 20 20  ETE). * target  
73c80 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c    -> A token hol
73c90 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66  ding the name of
73ca0 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 64 65   the table to de
73cb0 6c 65 74 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57  lete from.. * pW
73cc0 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57  here    -> The W
73cd0 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74  HERE clause of t
73ce0 68 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d  he DELETE statem
73cf0 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70  ent if one is sp
73d00 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20  ecified.. *     
73d10 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69           Otherwi
73d20 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20  se NULL.. * . * 
73d30 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45  (op == TK_UPDATE
73d40 29 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d  ). * target    -
73d50 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e  > A token holdin
73d60 67 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  g the name of th
73d70 65 20 74 61 62 6c 65 20 74 6f 20 75 70 64 61 74  e table to updat
73d80 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a 20 70 57  e rows of.. * pW
73d90 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57  here    -> The W
73da0 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74  HERE clause of t
73db0 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  he UPDATE statem
73dc0 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70  ent if one is sp
73dd0 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20  ecified.. *     
73de0 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69           Otherwi
73df0 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 45 78 70  se NULL.. * pExp
73e00 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 73 74 20  rList -> A list 
73e10 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 74  of the columns t
73e20 6f 20 75 70 64 61 74 65 20 61 6e 64 20 74 68 65  o update and the
73e30 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20   expressions to 
73e40 75 70 64 61 74 65 0a 20 2a 20 20 20 20 20 20 20  update. *       
73e50 20 20 20 20 20 20 20 74 68 65 6d 20 74 6f 2e 20         them to. 
73e60 53 65 65 20 73 71 6c 69 74 65 33 55 70 64 61 74  See sqlite3Updat
73e70 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  e() documentatio
73e80 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 73 22 0a  n of "pChanges".
73e90 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
73ea0 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 0a 20 2a  argument.. * . *
73eb0 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72  /.struct Trigger
73ec0 53 74 65 70 20 7b 0a 20 20 69 6e 74 20 6f 70 3b  Step {.  int op;
73ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
73ee0 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54   One of TK_DELET
73ef0 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  E, TK_UPDATE, TK
73f00 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c 45  _INSERT, TK_SELE
73f10 43 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f  CT */.  int orco
73f20 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf;          /* 
73f30 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74 63 2e  OE_Rollback etc.
73f40 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
73f50 54 72 69 67 3b 20 20 20 20 20 20 2f 2a 20 54 68  Trig;      /* Th
73f60 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20 74  e trigger that t
73f70 68 69 73 20 73 74 65 70 20 69 73 20 61 20 70 61  his step is a pa
73f80 72 74 20 6f 66 20 2a 2f 0a 0a 20 20 53 65 6c 65  rt of */..  Sele
73f90 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20  ct *pSelect;    
73fa0 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 53 45   /* Valid for SE
73fb0 4c 45 43 54 20 61 6e 64 20 73 6f 6d 65 74 69 6d  LECT and sometim
73fc0 65 73 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  es .            
73fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49 4e                IN
73fe0 53 45 52 54 20 73 74 65 70 73 20 28 77 68 65 6e  SERT steps (when
73ff0 20 70 45 78 70 72 4c 69 73 74 20 3d 3d 20 30 29   pExprList == 0)
74000 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 61 72 67   */.  Token targ
74010 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61  et;        /* Va
74020 6c 69 64 20 66 6f 72 20 44 45 4c 45 54 45 2c 20  lid for DELETE, 
74030 55 50 44 41 54 45 2c 20 49 4e 53 45 52 54 20 73  UPDATE, INSERT s
74040 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a  teps */.  Expr *
74050 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 2f  pWhere;        /
74060 2a 20 56 61 6c 69 64 20 66 6f 72 20 44 45 4c 45  * Valid for DELE
74070 54 45 2c 20 55 50 44 41 54 45 20 73 74 65 70 73  TE, UPDATE steps
74080 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
74090 70 45 78 70 72 4c 69 73 74 3b 20 2f 2a 20 56 61  pExprList; /* Va
740a0 6c 69 64 20 66 6f 72 20 55 50 44 41 54 45 20 73  lid for UPDATE s
740b0 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 73 6f  tatements and so
740c0 6d 65 74 69 6d 65 73 20 0a 20 20 20 20 20 20 20  metimes .       
740d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
740e0 20 20 20 20 49 4e 53 45 52 54 20 73 74 65 70 73      INSERT steps
740f0 20 28 77 68 65 6e 20 70 53 65 6c 65 63 74 20 3d   (when pSelect =
74100 3d 20 30 29 20 20 20 20 20 20 20 20 20 2a 2f 0a  = 0)         */.
74110 20 20 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73    IdList *pIdLis
74120 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20  t;     /* Valid 
74130 66 6f 72 20 49 4e 53 45 52 54 20 73 74 61 74 65  for INSERT state
74140 6d 65 6e 74 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20  ments only */.  
74150 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4e 65  TriggerStep *pNe
74160 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20  xt;  /* Next in 
74170 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f  the link-list */
74180 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
74190 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20  pLast;  /* Last 
741a0 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d  element in link-
741b0 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 6f 72 20  list. Valid for 
741c0 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f  1st elem only */
741d0 0a 7d 3b 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e  .};../*. * An in
741e0 73 74 61 6e 63 65 20 6f 66 20 73 74 72 75 63 74  stance of struct
741f0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 73 74   TriggerStack st
74200 6f 72 65 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ores information
74210 20 72 65 71 75 69 72 65 64 20 64 75 72 69 6e 67   required during
74220 20 63 6f 64 65 0a 20 2a 20 67 65 6e 65 72 61 74   code. * generat
74230 69 6f 6e 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  ion of a single 
74240 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e  trigger program.
74250 20 57 68 69 6c 65 20 74 68 65 20 74 72 69 67 67   While the trigg
74260 65 72 20 70 72 6f 67 72 61 6d 20 69 73 20 62 65  er program is be
74270 69 6e 67 0a 20 2a 20 63 6f 64 65 64 2c 20 69 74  ing. * coded, it
74280 73 20 61 73 73 6f 63 69 61 74 65 64 20 54 72 69  s associated Tri
74290 67 67 65 72 53 74 61 63 6b 20 69 6e 73 74 61 6e  ggerStack instan
742a0 63 65 20 69 73 20 70 6f 69 6e 74 65 64 20 74 6f  ce is pointed to
742b0 20 62 79 20 74 68 65 0a 20 2a 20 22 70 54 72 69   by the. * "pTri
742c0 67 67 65 72 53 74 61 63 6b 22 20 6d 65 6d 62 65  ggerStack" membe
742d0 72 20 6f 66 20 74 68 65 20 50 61 72 73 65 20 73  r of the Parse s
742e0 74 72 75 63 74 75 72 65 2e 0a 20 2a 0a 20 2a 20  tructure.. *. * 
742f0 54 68 65 20 70 54 61 62 20 6d 65 6d 62 65 72 20  The pTab member 
74300 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 74 61  points to the ta
74310 62 6c 65 20 74 68 61 74 20 74 72 69 67 67 65 72  ble that trigger
74320 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 64 65  s are being code
74330 64 20 6f 6e 2e 20 54 68 65 20 0a 20 2a 20 6e 65  d on. The . * ne
74340 77 49 64 78 20 6d 65 6d 62 65 72 20 63 6f 6e 74  wIdx member cont
74350 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f  ains the index o
74360 66 20 74 68 65 20 76 64 62 65 20 63 75 72 73 6f  f the vdbe curso
74370 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 61 74  r that points at
74380 20 74 68 65 20 74 65 6d 70 0a 20 2a 20 74 61 62   the temp. * tab
74390 6c 65 20 74 68 61 74 20 73 74 6f 72 65 73 20 74  le that stores t
743a0 68 65 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e  he new.* referen
743b0 63 65 73 2e 20 49 66 20 6e 65 77 2e 2a 20 72 65  ces. If new.* re
743c0 66 65 72 65 6e 63 65 73 20 61 72 65 20 6e 6f 74  ferences are not
743d0 20 76 61 6c 69 64 0a 20 2a 20 66 6f 72 20 74 68   valid. * for th
743e0 65 20 74 72 69 67 67 65 72 20 62 65 69 6e 67 20  e trigger being 
743f0 63 6f 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70  coded (for examp
74400 6c 65 20 61 6e 20 4f 4e 20 44 45 4c 45 54 45 20  le an ON DELETE 
74410 74 72 69 67 67 65 72 29 2c 20 74 68 65 6e 20 6e  trigger), then n
74420 65 77 49 64 78 0a 20 2a 20 69 73 20 73 65 74 20  ewIdx. * is set 
74430 74 6f 20 2d 31 2e 20 54 68 65 20 6f 6c 64 49 64  to -1. The oldId
74440 78 20 6d 65 6d 62 65 72 20 69 73 20 61 6e 61 6c  x member is anal
74450 6f 67 6f 75 73 20 74 6f 20 6e 65 77 49 64 78 2c  ogous to newIdx,
74460 20 66 6f 72 20 6f 6c 64 2e 2a 20 72 65 66 65 72   for old.* refer
74470 65 6e 63 65 73 2e 0a 20 2a 0a 20 2a 20 54 68 65  ences.. *. * The
74480 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c   ON CONFLICT pol
74490 69 63 79 20 74 6f 20 62 65 20 75 73 65 64 20 66  icy to be used f
744a0 6f 72 20 74 68 65 20 74 72 69 67 67 65 72 20 70  or the trigger p
744b0 72 6f 67 72 61 6d 20 73 74 65 70 73 20 69 73 20  rogram steps is 
744c0 73 74 6f 72 65 64 20 0a 20 2a 20 61 73 20 74 68  stored . * as th
744d0 65 20 6f 72 63 6f 6e 66 20 6d 65 6d 62 65 72 2e  e orconf member.
744e0 20 49 66 20 74 68 69 73 20 69 73 20 4f 45 5f 44   If this is OE_D
744f0 65 66 61 75 6c 74 2c 20 74 68 65 6e 20 74 68 65  efault, then the
74500 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61   ON CONFLICT cla
74510 75 73 65 20 0a 20 2a 20 73 70 65 63 69 66 69 65  use . * specifie
74520 64 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61 6c  d for individual
74530 20 74 72 69 67 67 65 72 73 20 73 74 65 70 73 20   triggers steps 
74540 69 73 20 75 73 65 64 2e 0a 20 2a 0a 20 2a 20 73  is used.. *. * s
74550 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 61  truct TriggerSta
74560 63 6b 20 68 61 73 20 61 20 22 70 4e 65 78 74 22  ck has a "pNext"
74570 20 6d 65 6d 62 65 72 2c 20 74 6f 20 61 6c 6c 6f   member, to allo
74580 77 20 6c 69 6e 6b 65 64 20 6c 69 73 74 73 20 74  w linked lists t
74590 6f 20 62 65 0a 20 2a 20 63 6f 6e 73 74 72 75 63  o be. * construc
745a0 74 65 64 2e 20 57 68 65 6e 20 63 6f 64 69 6e 67  ted. When coding
745b0 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 73   nested triggers
745c0 20 28 74 72 69 67 67 65 72 73 20 66 69 72 65 64   (triggers fired
745d0 20 62 79 20 6f 74 68 65 72 20 74 72 69 67 67 65   by other trigge
745e0 72 73 29 0a 20 2a 20 65 61 63 68 20 6e 65 73 74  rs). * each nest
745f0 65 64 20 74 72 69 67 67 65 72 20 73 74 6f 72 65  ed trigger store
74600 73 20 69 74 73 20 70 61 72 65 6e 74 20 74 72 69  s its parent tri
74610 67 67 65 72 27 73 20 54 72 69 67 67 65 72 53 74  gger's TriggerSt
74620 61 63 6b 20 61 73 20 74 68 65 20 22 70 4e 65 78  ack as the "pNex
74630 74 22 20 0a 20 2a 20 70 6f 69 6e 74 65 72 2e 20  t" . * pointer. 
74640 4f 6e 63 65 20 74 68 65 20 6e 65 73 74 65 64 20  Once the nested 
74650 74 72 69 67 67 65 72 20 68 61 73 20 62 65 65 6e  trigger has been
74660 20 63 6f 64 65 64 2c 20 74 68 65 20 70 4e 65 78   coded, the pNex
74670 74 20 76 61 6c 75 65 20 69 73 20 72 65 73 74 6f  t value is resto
74680 72 65 64 0a 20 2a 20 74 6f 20 74 68 65 20 70 54  red. * to the pT
74690 72 69 67 67 65 72 53 74 61 63 6b 20 6d 65 6d 62  riggerStack memb
746a0 65 72 20 6f 66 20 74 68 65 20 50 61 72 73 65 20  er of the Parse 
746b0 73 74 75 63 74 75 72 65 20 61 6e 64 20 63 6f 64  stucture and cod
746c0 69 6e 67 20 6f 66 20 74 68 65 20 70 61 72 65 6e  ing of the paren
746d0 74 0a 20 2a 20 74 72 69 67 67 65 72 20 63 6f 6e  t. * trigger con
746e0 74 69 6e 75 65 73 2e 0a 20 2a 0a 20 2a 20 42 65  tinues.. *. * Be
746f0 66 6f 72 65 20 61 20 6e 65 73 74 65 64 20 74 72  fore a nested tr
74700 69 67 67 65 72 20 69 73 20 63 6f 64 65 64 2c 20  igger is coded, 
74710 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  the linked list 
74720 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68  pointed to by th
74730 65 20 0a 20 2a 20 70 54 72 69 67 67 65 72 53 74  e . * pTriggerSt
74740 61 63 6b 20 69 73 20 73 63 61 6e 6e 65 64 20 74  ack is scanned t
74750 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  o ensure that th
74760 65 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74  e trigger is not
74770 20 61 62 6f 75 74 20 74 6f 20 62 65 20 63 6f 64   about to be cod
74780 65 64 0a 20 2a 20 72 65 63 75 72 73 69 76 65 6c  ed. * recursivel
74790 79 2e 20 49 66 20 74 68 69 73 20 63 6f 6e 64 69  y. If this condi
747a0 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64  tion is detected
747b0 2c 20 74 68 65 20 6e 65 73 74 65 64 20 74 72 69  , the nested tri
747c0 67 67 65 72 20 69 73 20 6e 6f 74 20 63 6f 64 65  gger is not code
747d0 64 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72  d.. */.struct Tr
747e0 69 67 67 65 72 53 74 61 63 6b 20 7b 0a 20 20 54  iggerStack {.  T
747f0 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
74800 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 68 61      /* Table tha
74810 74 20 74 72 69 67 67 65 72 73 20 61 72 65 20 63  t triggers are c
74820 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63  urrently being c
74830 6f 64 65 64 20 6f 6e 20 2a 2f 0a 20 20 69 6e 74  oded on */.  int
74840 20 6e 65 77 49 64 78 3b 20 20 20 20 20 20 20 20   newIdx;        
74850 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76 64    /* Index of vd
74860 62 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6e 65  be cursor to "ne
74870 77 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f  w" temp table */
74880 0a 20 20 69 6e 74 20 6f 6c 64 49 64 78 3b 20 20  .  int oldIdx;  
74890 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
748a0 20 6f 66 20 76 64 62 65 20 63 75 72 73 6f 72 20   of vdbe cursor 
748b0 74 6f 20 22 6f 6c 64 22 20 74 65 6d 70 20 74 61  to "old" temp ta
748c0 62 6c 65 20 2a 2f 0a 20 20 75 33 32 20 6e 65 77  ble */.  u32 new
748d0 43 6f 6c 4d 61 73 6b 3b 0a 20 20 75 33 32 20 6f  ColMask;.  u32 o
748e0 6c 64 43 6f 6c 4d 61 73 6b 3b 0a 20 20 69 6e 74  ldColMask;.  int
748f0 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20   orconf;        
74900 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 72 63    /* Current orc
74910 6f 6e 66 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20  onf policy */.  
74920 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 3b 20  int ignoreJump; 
74930 20 20 20 20 20 2f 2a 20 77 68 65 72 65 20 74 6f       /* where to
74940 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 61 20 52   jump to for a R
74950 41 49 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f 0a  AISE(IGNORE) */.
74960 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
74970 67 65 72 3b 20 20 20 2f 2a 20 54 68 65 20 74 72  ger;   /* The tr
74980 69 67 67 65 72 20 63 75 72 72 65 6e 74 6c 79 20  igger currently 
74990 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20  being coded */. 
749a0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70   TriggerStack *p
749b0 4e 65 78 74 3b 20 2f 2a 20 4e 65 78 74 20 74 72  Next; /* Next tr
749c0 69 67 67 65 72 20 64 6f 77 6e 20 6f 6e 20 74 68  igger down on th
749d0 65 20 74 72 69 67 67 65 72 20 73 74 61 63 6b 20  e trigger stack 
749e0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
749f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
74a00 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e  ture contains in
74a10 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62  formation used b
74a20 79 20 74 68 65 20 73 71 6c 69 74 65 46 69 78 2e  y the sqliteFix.
74a30 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61  ...** routines a
74a40 73 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65 20  s they walk the 
74a50 70 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d 61  parse tree to ma
74a60 6b 65 20 64 61 74 61 62 61 73 65 20 72 65 66 65  ke database refe
74a70 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69 63  rences.** explic
74a80 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 66  it.  .*/.typedef
74a90 20 73 74 72 75 63 74 20 44 62 46 69 78 65 72 20   struct DbFixer 
74aa0 44 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74 20  DbFixer;.struct 
74ab0 44 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72 73  DbFixer {.  Pars
74ac0 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
74ad0 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
74ae0 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20 6d  ontext.  Error m
74af0 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20  essages written 
74b00 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  here */.  const 
74b10 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f 2a  char *zDb;    /*
74b20 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 6f   Make sure all o
74b30 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74 61  bjects are conta
74b40 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61 74  ined in this dat
74b50 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  abase */.  const
74b60 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 2f   char *zType;  /
74b70 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63 6f  * Type of the co
74b80 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20 66  ntainer - used f
74b90 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  or error message
74ba0 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b  s */.  const Tok
74bb0 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e 61  en *pName; /* Na
74bc0 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69  me of the contai
74bd0 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65  ner - used for e
74be0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f  rror messages */
74bf0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62  .};../*.** An ob
74c00 6a 65 63 74 65 64 20 75 73 65 64 20 74 6f 20 61  jected used to a
74c10 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74 65  ccumulate the te
74c20 78 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20 77  xt of a string w
74c30 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e 6f  here we.** do no
74c40 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b 6e  t necessarily kn
74c50 6f 77 20 68 6f 77 20 62 69 67 20 74 68 65 20 73  ow how big the s
74c60 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69 6e  tring will be in
74c70 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 72   the end..*/.str
74c80 75 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a 20  uct StrAccum {. 
74c90 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
74ca0 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61        /* Optiona
74cb0 6c 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 6c  l database for l
74cc0 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20 62  ookaside.  Can b
74cd0 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72  e NULL */.  char
74ce0 20 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20 20   *zBase;        
74cf0 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f 63   /* A base alloc
74d00 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f 6d  ation.  Not from
74d10 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63 68   malloc. */.  ch
74d20 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 20  ar *zText;      
74d30 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67     /* The string
74d40 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 61   collected so fa
74d50 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 61  r */.  int  nCha
74d60 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  r;          /* L
74d70 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 72  ength of the str
74d80 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20  ing so far */.  
74d90 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20  int  nAlloc;    
74da0 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f       /* Amount o
74db0 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  f space allocate
74dc0 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20 20  d in zText */.  
74dd0 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 20  int  mxAlloc;   
74de0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
74df0 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6c  allowed string l
74e00 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20 20  ength */.  u8   
74e10 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20  mallocFailed;   
74e20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20  /* Becomes true 
74e30 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 6c  if any memory al
74e40 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 2a  location fails *
74e50 2f 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c 6c  /.  u8   useMall
74e60 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  oc;      /* True
74e70 20 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e 6c   if zText is enl
74e80 61 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20 72  argeable using r
74e90 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 20  ealloc */.  u8  
74ea0 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 20   tooBig;        
74eb0 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65   /* Becomes true
74ec0 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65 20   if string size 
74ed0 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20 2a  exceeds limits *
74ee0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f  /.};../*.** A po
74ef0 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74  inter to this st
74f00 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20  ructure is used 
74f10 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 69  to communicate i
74f20 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72  nformation.** fr
74f30 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20 61  om sqlite3Init a
74f40 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  nd OP_ParseSchem
74f50 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74  a into the sqlit
74f60 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e 0a  e3InitCallback..
74f70 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
74f80 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  t {.  sqlite3 *d
74f90 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b;        /* The
74fa0 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20   database being 
74fb0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
74fc0 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
74fd0 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 61       /* 0 for ma
74fe0 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31 20  in database.  1 
74ff0 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66 6f  for TEMP, 2.. fo
75000 72 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20 20  r ATTACHed */.  
75010 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 3b  char **pzErrMsg;
75020 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73      /* Error mes
75030 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72 65  sage stored here
75040 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
75050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
75060 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64 20  ult code stored 
75070 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 61  here */.} InitDa
75080 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63  ta;../*.** Struc
75090 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ture containing 
750a0 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61  global configura
750b0 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 68  tion data for th
750c0 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
750d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ..**.** This str
750e0 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e 74  ucture also cont
750f0 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 20  ains some state 
75100 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
75110 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f  struct Sqlite3Co
75120 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d 65  nfig {.  int bMe
75130 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20 20  mstat;          
75140 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
75150 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 6d  ue to enable mem
75160 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 20  ory status */.  
75170 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b 20  int bCoreMutex; 
75180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75190 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61    /* True to ena
751a0 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69 6e  ble core mutexin
751b0 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c 6c  g */.  int bFull
751c0 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20  Mutex;          
751d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
751e0 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c 20   to enable full 
751f0 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e  mutexing */.  in
75200 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 20  t mxStrlen;     
75210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75220 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 6e  /* Maximum strin
75230 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 6e  g length */.  in
75240 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 20  t szLookaside;  
75250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75260 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b 61  /* Default looka
75270 73 69 64 65 20 62 75 66 66 65 72 20 73 69 7a 65  side buffer size
75280 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61   */.  int nLooka
75290 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  side;           
752a0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75          /* Defau
752b0 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66  lt lookaside buf
752c0 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 73  fer count */.  s
752d0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
752e0 64 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20 20  ds m;           
752f0 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65   /* Low-level me
75300 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
75310 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73  interface */.  s
75320 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
75330 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20 20  hods mutex;     
75340 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75   /* Low-level mu
75350 74 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a 2f  tex interface */
75360 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  .  sqlite3_pcach
75370 65 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68 65  e_methods pcache
75380 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65  ;    /* Low-leve
75390 6c 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e 74  l page-cache int
753a0 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64  erface */.  void
753b0 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20 20   *pHeap;        
753c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
753d0 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73 70   Heap storage sp
753e0 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 65  ace */.  int nHe
753f0 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ap;             
75400 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
75410 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a 2f  ze of pHeap[] */
75420 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d 78  .  int mnReq, mx
75430 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 20  Req;            
75440 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 20       /* Min and 
75450 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73 74  max heap request
75460 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f 69  s sizes */.  voi
75470 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20 20  d *pScratch;    
75480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
75490 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  * Scratch memory
754a0 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61   */.  int szScra
754b0 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  tch;            
754c0 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
754d0 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68 20  of each scratch 
754e0 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20  buffer */.  int 
754f0 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20  nScratch;       
75500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
75510 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74   Number of scrat
75520 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20  ch buffers */.  
75530 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20 20  void *pPage;    
75540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75550 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20    /* Page cache 
75560 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20  memory */.  int 
75570 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  szPage;         
75580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
75590 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61   Size of each pa
755a0 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f  ge in pPage[] */
755b0 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20  .  int nPage;   
755c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
755d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
755e0 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67 65  f pages in pPage
755f0 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61  [] */.  int mxPa
75600 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20 20  rserStack;      
75610 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 78            /* max
75620 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68  imum depth of th
75630 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 2a  e parser stack *
75640 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43 61  /.  int sharedCa
75650 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 20  cheEnabled;     
75660 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66        /* true if
75670 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f   shared-cache mo
75680 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20  de enabled */.  
75690 2f 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69 67  /* The above mig
756a0 68 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ht be initialize
756b0 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20  d to non-zero.  
756c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65  The following ne
756d0 65 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a  ed to always.  *
756e0 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 7a  * initially be z
756f0 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a 2f  ero, however. */
75700 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 20  .  int isInit;  
75710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75720 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74       /* True aft
75730 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  er initializatio
75740 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 2a  n has finished *
75750 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 65  /.  int inProgre
75760 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ss;             
75770 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68        /* True wh
75780 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ile initializati
75790 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 2a  on in progress *
757a0 2f 0a 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63  /.  int isMalloc
757b0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  Init;           
757c0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66        /* True af
757d0 74 65 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e  ter malloc is in
757e0 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 73  itialized */.  s
757f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 49  qlite3_mutex *pI
75800 6e 69 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20  nitMutex;       
75810 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 62   /* Mutex used b
75820 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  y sqlite3_initia
75830 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 69 6e 74 20  lize() */.  int 
75840 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 3b 20 20  nRefInitMutex;  
75850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
75860 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73   Number of users
75870 20 6f 66 20 70 49 6e 69 74 4d 75 74 65 78 20 2a   of pInitMutex *
75880 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74  /.};../*.** Cont
75890 65 78 74 20 70 6f 69 6e 74 65 72 20 70 61 73 73  ext pointer pass
758a0 65 64 20 64 6f 77 6e 20 74 68 72 6f 75 67 68 20  ed down through 
758b0 74 68 65 20 74 72 65 65 2d 77 61 6c 6b 2e 0a 2a  the tree-walk..*
758c0 2f 0a 73 74 72 75 63 74 20 57 61 6c 6b 65 72 20  /.struct Walker 
758d0 7b 0a 20 20 69 6e 74 20 28 2a 78 45 78 70 72 43  {.  int (*xExprC
758e0 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a  allback)(Walker*
758f0 2c 20 45 78 70 72 2a 29 3b 20 20 20 20 20 2f 2a  , Expr*);     /*
75900 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65 78   Callback for ex
75910 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69  pressions */.  i
75920 6e 74 20 28 2a 78 53 65 6c 65 63 74 43 61 6c 6c  nt (*xSelectCall
75930 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a 2c 53 65  back)(Walker*,Se
75940 6c 65 63 74 2a 29 3b 20 20 2f 2a 20 43 61 6c 6c  lect*);  /* Call
75950 62 61 63 6b 20 66 6f 72 20 53 45 4c 45 43 54 73  back for SELECTs
75960 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61   */.  Parse *pPa
75970 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  rse;            
75980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75990 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
759a0 74 2e 20 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b  t.  */.  union {
759b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
759c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
759d0 20 20 20 2f 2a 20 45 78 74 72 61 20 64 61 74 61     /* Extra data
759e0 20 66 6f 72 20 63 61 6c 6c 62 61 63 6b 20 2a 2f   for callback */
759f0 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  .    NameContext
75a00 20 2a 70 4e 43 3b 20 20 20 20 20 20 20 20 20 20   *pNC;          
75a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75a20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f 6e 74 65 78  /* Naming contex
75a30 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 20  t */.    int i; 
75a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75a60 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76      /* Integer v
75a70 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d  alue */.  } u;.}
75a80 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  ;../* Forward de
75a90 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 53 51  clarations */.SQ
75aa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
75ab0 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
75ac0 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29  (Walker*, Expr*)
75ad0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75ae0 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b   int sqlite3Walk
75af0 45 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 2a  ExprList(Walker*
75b00 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51  , ExprList*);.SQ
75b10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
75b20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
75b30 63 74 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65  ct(Walker*, Sele
75b40 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ct*);.SQLITE_PRI
75b50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
75b60 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 57  WalkSelectExpr(W
75b70 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29  alker*, Select*)
75b80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75b90 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b   int sqlite3Walk
75ba0 53 65 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65  SelectFrom(Walke
75bb0 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 0a 2f  r*, Select*);../
75bc0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63 6f 64 65  *.** Return code
75bd0 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73 65 2d   from the parse-
75be0 74 72 65 65 20 77 61 6c 6b 69 6e 67 20 70 72 69  tree walking pri
75bf0 6d 69 74 69 76 65 73 20 61 6e 64 20 74 68 65 69  mitives and thei
75c00 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 2e 0a  r.** callbacks..
75c10 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 43  */.#define WRC_C
75c20 6f 6e 74 69 6e 75 65 20 20 20 20 30 20 20 20 2f  ontinue    0   /
75c30 2a 20 43 6f 6e 74 69 6e 75 65 20 64 6f 77 6e 20  * Continue down 
75c40 69 6e 74 6f 20 63 68 69 6c 64 72 65 6e 20 2a 2f  into children */
75c50 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 50 72 75  .#define WRC_Pru
75c60 6e 65 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20  ne       1   /* 
75c70 4f 6d 69 74 20 63 68 69 6c 64 72 65 6e 20 62 75  Omit children bu
75c80 74 20 63 6f 6e 74 69 6e 75 65 20 77 61 6c 6b 69  t continue walki
75c90 6e 67 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a 23  ng siblings */.#
75ca0 64 65 66 69 6e 65 20 57 52 43 5f 41 62 6f 72 74  define WRC_Abort
75cb0 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20 41 62         2   /* Ab
75cc0 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77  andon the tree w
75cd0 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 73  alk */../*.** As
75ce0 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f 69 6e 74  suming zIn point
75cf0 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62  s to the first b
75d00 79 74 65 20 6f 66 20 61 20 55 54 46 2d 38 20 63  yte of a UTF-8 c
75d10 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20 61 64 76  haracter,.** adv
75d20 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70 6f 69 6e  ance zIn to poin
75d30 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62  t to the first b
75d40 79 74 65 20 6f 66 20 74 68 65 20 6e 65 78 74 20  yte of the next 
75d50 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e  UTF-8 character.
75d60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
75d70 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 49 6e  TE_SKIP_UTF8(zIn
75d80 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  ) {             
75d90 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69             \.  i
75da0 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29 3e 3d 30  f( (*(zIn++))>=0
75db0 78 63 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  xc0 ){          
75dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75dd0 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c 65 28      \.    while(
75de0 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d   (*zIn & 0xc0)==
75df0 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b 3b 20 7d  0x80 ){ zIn++; }
75e00 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
75e10 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20   }              
75e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75e40 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a        \.}../*.**
75e50 20 54 68 65 20 53 51 4c 49 54 45 5f 43 4f 52 52   The SQLITE_CORR
75e60 55 50 54 5f 42 4b 50 54 20 6d 61 63 72 6f 20 63  UPT_BKPT macro c
75e70 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 63  an be either a c
75e80 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20 70 72 6f  onstant (for pro
75e90 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75 69 6c 64  duction.** build
75ea0 73 29 20 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e  s) or a function
75eb0 20 63 61 6c 6c 20 28 66 6f 72 20 64 65 62 75 67   call (for debug
75ec0 67 69 6e 67 29 2e 20 20 49 66 20 69 74 20 69 73  ging).  If it is
75ed0 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c   a function call
75ee0 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77 73 20 74  ,.** it allows t
75ef0 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 73  he operator to s
75f00 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74 20  et a breakpoint 
75f10 61 74 20 74 68 65 20 73 70 6f 74 20 77 68 65 72  at the spot wher
75f20 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
75f30 72 72 75 70 74 69 6f 6e 20 69 73 20 66 69 72 73  rruption is firs
75f40 74 20 64 65 74 65 63 74 65 64 2e 0a 2a 2f 0a 23  t detected..*/.#
75f50 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
75f60 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
75f70 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 43  E   int sqlite3C
75f80 6f 72 72 75 70 74 28 76 6f 69 64 29 3b 0a 23 20  orrupt(void);.# 
75f90 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
75fa0 52 52 55 50 54 5f 42 4b 50 54 20 73 71 6c 69 74  RRUPT_BKPT sqlit
75fb0 65 33 43 6f 72 72 75 70 74 28 29 0a 23 65 6c 73  e3Corrupt().#els
75fc0 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  e.# define SQLIT
75fd0 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 53  E_CORRUPT_BKPT S
75fe0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 23 65  QLITE_CORRUPT.#e
75ff0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  ndif../*.** Inte
76000 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 70 72  rnal function pr
76010 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49  ototypes.*/.SQLI
76020 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
76030 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63 6f  qlite3StrICmp(co
76040 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73  nst char *, cons
76050 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  t char *);.SQLIT
76060 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
76070 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 63 6f  lite3StrNICmp(co
76080 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73  nst char *, cons
76090 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a  t char *, int);.
760a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
760b0 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62  nt sqlite3IsNumb
760c0 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  er(const char*, 
760d0 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54  int*, u8);.SQLIT
760e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
760f0 6c 69 74 65 33 53 74 72 6c 65 6e 28 73 71 6c 69  lite3Strlen(sqli
76100 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  te3*, const char
76110 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
76120 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  TE int sqlite3St
76130 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63 68 61  rlen30(const cha
76140 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  r*);..SQLITE_PRI
76150 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
76160 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29  MallocInit(void)
76170 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76180 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c   void sqlite3Mal
76190 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51  locEnd(void);.SQ
761a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
761b0 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  d *sqlite3Malloc
761c0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
761d0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
761e0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e  te3MallocZero(in
761f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
76200 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
76210 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c  DbMallocZero(sql
76220 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ite3*, int);.SQL
76230 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
76240 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f   *sqlite3DbMallo
76250 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 69  cRaw(sqlite3*, i
76260 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
76270 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
76280 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65  3DbStrDup(sqlite
76290 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  3*,const char*);
762a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
762b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53  char *sqlite3DbS
762c0 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  trNDup(sqlite3*,
762d0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
762e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
762f0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 52  E void *sqlite3R
76300 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e  ealloc(void*, in
76310 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
76320 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
76330 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
76340 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20  sqlite3 *, void 
76350 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
76360 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
76370 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73  lite3DbRealloc(s
76380 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a  qlite3 *, void *
76390 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
763a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
763b0 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 65  te3DbFree(sqlite
763c0 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  3*, void*);.SQLI
763d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
763e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65  qlite3MallocSize
763f0 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f  (void*);.SQLITE_
76400 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
76410 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
76420 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29  sqlite3*, void*)
76430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76440 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63   void *sqlite3Sc
76450 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29  ratchMalloc(int)
76460 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76470 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72   void sqlite3Scr
76480 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29 3b  atchFree(void*);
76490 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
764a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
764b0 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51  eMalloc(int);.SQ
764c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
764d0 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65  d sqlite3PageFre
764e0 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  e(void*);.SQLITE
764f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
76500 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75  lite3MemSetDefau
76510 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  lt(void);.SQLITE
76520 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
76530 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f  lite3BenignMallo
76540 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28  cHooks(void (*)(
76550 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 28  void), void (*)(
76560 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50  void));.SQLITE_P
76570 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
76580 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f  e3MemoryAlarm(vo
76590 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71  id (*)(void*, sq
765a0 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74  lite3_int64, int
765b0 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65  ), void*, sqlite
765c0 33 5f 69 6e 74 36 34 29 3b 0a 0a 23 69 66 64 65  3_int64);..#ifde
765d0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
765e0 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50  MEMSYS3.SQLITE_P
765f0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c  RIVATE const sql
76600 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
76610 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d   *sqlite3MemGetM
76620 65 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65  emsys3(void);.#e
76630 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
76640 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
76650 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  5.SQLITE_PRIVATE
76660 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
76670 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  em_methods *sqli
76680 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35  te3MemGetMemsys5
76690 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a  (void);.#endif..
766a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
766b0 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54  MUTEX_OMIT.SQLIT
766c0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
766d0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
766e0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c  s *sqlite3Defaul
766f0 74 4d 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 51  tMutex(void);.SQ
76700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73  LITE_PRIVATE   s
76710 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71  qlite3_mutex *sq
76720 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
76730 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
76740 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
76750 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64  e3MutexInit(void
76760 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
76770 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  E   int sqlite3M
76780 75 74 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23  utexEnd(void);.#
76790 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
767a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
767b0 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74  3StatusValue(int
767c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
767d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
767e0 61 74 75 73 41 64 64 28 69 6e 74 2c 20 69 6e 74  atusAdd(int, int
767f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
76800 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
76810 61 74 75 73 53 65 74 28 69 6e 74 2c 20 69 6e 74  atusSet(int, int
76820 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
76830 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
76840 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51  NaN(double);..SQ
76850 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
76860 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74  d sqlite3VXPrint
76870 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74  f(StrAccum*, int
76880 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76  , const char*, v
76890 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f  a_list);.SQLITE_
768a0 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
768b0 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c  lite3MPrintf(sql
768c0 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
768d0 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f  *, ...);.SQLITE_
768e0 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
768f0 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71  lite3VMPrintf(sq
76900 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
76910 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51  r*, va_list);.SQ
76920 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
76930 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e  r *sqlite3MAppen
76940 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72  df(sqlite3*,char
76950 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  *,const char*,..
76960 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .);.#if defined(
76970 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20  SQLITE_TEST) || 
76980 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
76990 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49  EBUG).SQLITE_PRI
769a0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
769b0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63  te3DebugPrintf(c
769c0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
769d0 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66  ;.#endif.#if def
769e0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
769f0 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
76a00 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33     void *sqlite3
76a10 54 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f  TestTextToPtr(co
76a20 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64  nst char*);.#end
76a30 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
76a40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
76a50 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c  tString(char **,
76a60 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74   sqlite3*, const
76a70 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51   char*, ...);.SQ
76a80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
76a90 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73  d sqlite3ErrorMs
76aa0 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20  g(Parse*, const 
76ab0 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c  char*, ...);.SQL
76ac0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
76ad0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65   sqlite3ErrorCle
76ae0 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  ar(Parse*);.SQLI
76af0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
76b00 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63  sqlite3Dequote(c
76b10 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
76b20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
76b30 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 73 71  e3DequoteExpr(sq
76b40 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a  lite3*, Expr*);.
76b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
76b60 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72  nt sqlite3Keywor
76b70 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69  dCode(const unsi
76b80 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29  gned char*, int)
76b90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76ba0 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50   int sqlite3RunP
76bb0 61 72 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f  arser(Parse*, co
76bc0 6e 73 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20  nst char*, char 
76bd0 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
76be0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
76bf0 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72  FinishCoding(Par
76c00 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
76c10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
76c20 47 65 74 54 65 6d 70 52 65 67 28 50 61 72 73 65  GetTempReg(Parse
76c30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
76c40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
76c50 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61  eleaseTempReg(Pa
76c60 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  rse*,int);.SQLIT
76c70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
76c80 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
76c90 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53  e(Parse*,int);.S
76ca0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
76cb0 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  id sqlite3Releas
76cc0 65 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65  eTempRange(Parse
76cd0 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  *,int,int);.SQLI
76ce0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
76cf0 2a 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c  *sqlite3Expr(sql
76d00 69 74 65 33 2a 2c 20 69 6e 74 2c 20 45 78 70 72  ite3*, int, Expr
76d10 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20  *, Expr*, const 
76d20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
76d30 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
76d40 6c 69 74 65 33 50 45 78 70 72 28 50 61 72 73 65  lite3PExpr(Parse
76d50 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45  *, int, Expr*, E
76d60 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65  xpr*, const Toke
76d70 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
76d80 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
76d90 33 52 65 67 69 73 74 65 72 45 78 70 72 28 50 61  3RegisterExpr(Pa
76da0 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51  rse*,Token*);.SQ
76db0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
76dc0 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e  r *sqlite3ExprAn
76dd0 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a  d(sqlite3*,Expr*
76de0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
76df0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
76e00 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 45 78  lite3ExprSpan(Ex
76e10 70 72 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  pr*,Token*,Token
76e20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
76e30 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
76e40 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72  ExprFunction(Par
76e50 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54  se*,ExprList*, T
76e60 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
76e70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
76e80 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72  te3ExprAssignVar
76e90 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45  Number(Parse*, E
76ea0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
76eb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
76ec0 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69  e3ExprClear(sqli
76ed0 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  te3*, Expr*);.SQ
76ee0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
76ef0 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c  d sqlite3ExprDel
76f00 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78  ete(sqlite3*, Ex
76f10 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
76f20 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73  VATE ExprList *s
76f30 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
76f40 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72  pend(Parse*,Expr
76f50 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 54 6f 6b 65  List*,Expr*,Toke
76f60 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
76f70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
76f80 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73  ExprListDelete(s
76f90 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73  qlite3*, ExprLis
76fa0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
76fb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
76fc0 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68  nit(sqlite3*, ch
76fd0 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ar**);.SQLITE_PR
76fe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
76ff0 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f  3InitCallback(vo
77000 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a  id*, int, char**
77010 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54  , char**);.SQLIT
77020 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
77030 71 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72  qlite3Pragma(Par
77040 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
77050 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53  *,Token*,int);.S
77060 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
77070 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49  id sqlite3ResetI
77080 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71  nternalSchema(sq
77090 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
770a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
770b0 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  d sqlite3BeginPa
770c0 72 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b  rse(Parse*,int);
770d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
770e0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d  void sqlite3Comm
770f0 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65  itInternalChange
77100 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  s(sqlite3*);.SQL
77110 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
77120 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74  e *sqlite3Result
77130 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73  SetOfSelect(Pars
77140 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  e*,Select*);.SQL
77150 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
77160 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74   sqlite3OpenMast
77170 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c  erTable(Parse *,
77180 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
77190 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
771a0 65 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72  e3StartTable(Par
771b0 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
771c0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  *,int,int,int,in
771d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
771e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
771f0 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c  ddColumn(Parse*,
77200 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
77210 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
77220 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50  ite3AddNotNull(P
77230 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  arse*, int);.SQL
77240 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
77250 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61   sqlite3AddPrima
77260 72 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78  ryKey(Parse*, Ex
77270 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e  prList*, int, in
77280 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
77290 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
772a0 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73  ite3AddCheckCons
772b0 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45  traint(Parse*, E
772c0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
772d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
772e0 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28  e3AddColumnType(
772f0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a  Parse*,Token*);.
77300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
77310 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65  oid sqlite3AddDe
77320 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65  faultValue(Parse
77330 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  *,Expr*);.SQLITE
77340 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
77350 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54  lite3AddCollateT
77360 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  ype(Parse*, Toke
77370 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
77380 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
77390 45 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c  EndTable(Parse*,
773a0 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65  Token*,Token*,Se
773b0 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f  lect*);..SQLITE_
773c0 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a  PRIVATE Bitvec *
773d0 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65  sqlite3BitvecCre
773e0 61 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45  ate(u32);.SQLITE
773f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
77400 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42  ite3BitvecTest(B
77410 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51  itvec*, u32);.SQ
77420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
77430 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65   sqlite3BitvecSe
77440 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b  t(Bitvec*, u32);
77450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
77460 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76  void sqlite3Bitv
77470 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c  ecClear(Bitvec*,
77480 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52   u32);.SQLITE_PR
77490 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
774a0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
774b0 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45  Bitvec*);.SQLITE
774c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
774d0 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69  ite3BitvecBuilti
774e0 6e 54 65 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b  nTest(int,int*);
774f0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
77500 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33   RowSet *sqlite3
77510 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74  RowSetInit(sqlit
77520 65 33 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69  e3*, void*, unsi
77530 67 6e 65 64 20 69 6e 74 29 3b 0a 53 51 4c 49 54  gned int);.SQLIT
77540 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
77550 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61  qlite3RowSetClea
77560 72 28 52 6f 77 53 65 74 2a 29 3b 0a 53 51 4c 49  r(RowSet*);.SQLI
77570 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
77580 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73  sqlite3RowSetIns
77590 65 72 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 34  ert(RowSet*, i64
775a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
775b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77  E int sqlite3Row
775c0 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 2a 2c  SetNext(RowSet*,
775d0 20 69 36 34 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f   i64*);..SQLITE_
775e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
775f0 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28 50  ite3CreateView(P
77600 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b  arse*,Token*,Tok
77610 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63  en*,Token*,Selec
77620 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69  t*,int,int);..#i
77630 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
77640 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
77650 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
77660 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
77670 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  E).SQLITE_PRIVAT
77680 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56  E   int sqlite3V
77690 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
776a0 73 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29  s(Parse*,Table*)
776b0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
776c0 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
776d0 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20  olumnNames(A,B) 
776e0 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  0.#endif..SQLITE
776f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
77700 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50  lite3DropTable(P
77710 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c  arse*, SrcList*,
77720 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
77730 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
77740 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
77750 6c 65 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49  le(Table*);.SQLI
77760 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
77770 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 50 61  sqlite3Insert(Pa
77780 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
77790 45 78 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63  ExprList*, Selec
777a0 74 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74  t*, IdList*, int
777b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
777c0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41  E void *sqlite3A
777d0 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c  rrayAllocate(sql
777e0 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c  ite3*,void*,int,
777f0 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e  int,int*,int*,in
77800 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
77810 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69  ATE IdList *sqli
77820 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28  te3IdListAppend(
77830 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74  sqlite3*, IdList
77840 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
77850 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
77860 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65  qlite3IdListInde
77870 78 28 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20  x(IdList*,const 
77880 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
77890 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
778a0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e  sqlite3SrcListEn
778b0 6c 61 72 67 65 28 73 71 6c 69 74 65 33 2a 2c 20  large(sqlite3*, 
778c0 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69  SrcList*, int, i
778d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
778e0 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
778f0 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
77900 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c  d(sqlite3*, SrcL
77910 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ist*, Token*, To
77920 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
77930 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
77940 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
77950 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73  endFromTerm(Pars
77960 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f  e*, SrcList*, To
77970 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20  ken*, Token*,.  
77980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
779a0 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65      Token*, Sele
779b0 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69  ct*, Expr*, IdLi
779c0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
779d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
779e0 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42  3SrcListIndexedB
779f0 79 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69  y(Parse *, SrcLi
77a00 73 74 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a  st *, Token *);.
77a10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
77a20 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65  nt sqlite3Indexe
77a30 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20  dByLookup(Parse 
77a40 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  *, struct SrcLis
77a50 74 5f 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54  t_item *);.SQLIT
77a60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
77a70 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69  qlite3SrcListShi
77a80 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69  ftJoinType(SrcLi
77a90 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
77aa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
77ab0 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75  3SrcListAssignCu
77ac0 72 73 6f 72 73 28 50 61 72 73 65 2a 2c 20 53 72  rsors(Parse*, Sr
77ad0 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  cList*);.SQLITE_
77ae0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
77af0 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
77b00 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73  (sqlite3*, IdLis
77b10 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
77b20 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
77b30 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71  SrcListDelete(sq
77b40 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a  lite3*, SrcList*
77b50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
77b60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72  E void sqlite3Cr
77b70 65 61 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a  eateIndex(Parse*
77b80 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53  ,Token*,Token*,S
77b90 72 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74  rcList*,ExprList
77ba0 2a 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20  *,int,Token*,.  
77bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77bc0 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e        Token*, in
77bd0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
77be0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
77bf0 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61  ite3DropIndex(Pa
77c00 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
77c10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
77c20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
77c30 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53  Select(Parse*, S
77c40 65 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65  elect*, SelectDe
77c50 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
77c60 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c  VATE Select *sql
77c70 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61  ite3SelectNew(Pa
77c80 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53  rse*,ExprList*,S
77c90 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78  rcList*,Expr*,Ex
77ca0 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20  prList*,.       
77cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77cc0 20 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74    Expr*,ExprList
77cd0 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72  *,int,Expr*,Expr
77ce0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
77cf0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
77d00 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69  electDelete(sqli
77d10 74 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a  te3*, Select*);.
77d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
77d30 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63  able *sqlite3Src
77d40 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65  ListLookup(Parse
77d50 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51  *, SrcList*);.SQ
77d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
77d70 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e   sqlite3IsReadOn
77d80 6c 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65  ly(Parse*, Table
77d90 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
77da0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
77db0 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61  ite3OpenTable(Pa
77dc0 72 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20  rse*, int iCur, 
77dd0 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c  int iDb, Table*,
77de0 20 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e   int);.#if defin
77df0 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
77e00 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c  _UPDATE_DELETE_L
77e10 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65  IMIT) && !define
77e20 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
77e30 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50  BQUERY).SQLITE_P
77e40 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
77e50 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50  ite3LimitWhere(P
77e60 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20  arse *, SrcList 
77e70 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c  *, Expr *, ExprL
77e80 69 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45  ist *, Expr *, E
77e90 78 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a  xpr *, char *);.
77ea0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
77eb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
77ec0 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72  e3DeleteFrom(Par
77ed0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45  se*, SrcList*, E
77ee0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
77ef0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
77f00 65 33 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c  e3Update(Parse*,
77f10 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c   SrcList*, ExprL
77f20 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  ist*, Expr*, int
77f30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
77f40 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c  E WhereInfo *sql
77f50 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 50  ite3WhereBegin(P
77f60 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c  arse*, SrcList*,
77f70 20 45 78 70 72 2a 2c 20 45 78 70 72 4c 69 73 74   Expr*, ExprList
77f80 2a 2a 2c 20 75 38 2c 20 69 6e 74 29 3b 0a 53 51  **, u8, int);.SQ
77f90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
77fa0 64 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e  d sqlite3WhereEn
77fb0 64 28 57 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53  d(WhereInfo*);.S
77fc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
77fd0 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
77fe0 65 47 65 74 43 6f 6c 75 6d 6e 28 50 61 72 73 65  eGetColumn(Parse
77ff0 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20  *, Table*, int, 
78000 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  int, int, int);.
78010 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
78020 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
78030 6f 64 65 4d 6f 76 65 28 50 61 72 73 65 2a 2c 20  odeMove(Parse*, 
78040 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  int, int, int);.
78050 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
78060 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
78070 6f 64 65 43 6f 70 79 28 50 61 72 73 65 2a 2c 20  odeCopy(Parse*, 
78080 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  int, int, int);.
78090 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
780a0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
780b0 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28  learColumnCache(
780c0 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51  Parse*, int);.SQ
780d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
780e0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
780f0 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
78100 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Parse*, int, in
78110 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
78120 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
78130 78 70 72 57 72 69 74 61 62 6c 65 52 65 67 69 73  xprWritableRegis
78140 74 65 72 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b  ter(Parse*,int);
78150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78160 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
78170 48 61 72 64 43 6f 70 79 28 50 61 72 73 65 2a 2c  HardCopy(Parse*,
78180 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
78190 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
781a0 69 74 65 33 45 78 70 72 43 6f 64 65 28 50 61 72  ite3ExprCode(Par
781b0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  se*, Expr*, int)
781c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
781d0 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
781e0 43 6f 64 65 54 65 6d 70 28 50 61 72 73 65 2a 2c  CodeTemp(Parse*,
781f0 20 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53   Expr*, int*);.S
78200 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
78210 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
78220 65 54 61 72 67 65 74 28 50 61 72 73 65 2a 2c 20  eTarget(Parse*, 
78230 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  Expr*, int);.SQL
78240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
78250 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41  sqlite3ExprCodeA
78260 6e 64 43 61 63 68 65 28 50 61 72 73 65 2a 2c 20  ndCache(Parse*, 
78270 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  Expr*, int);.SQL
78280 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
78290 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
782a0 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73 65 2a  Constants(Parse*
782b0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
782c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
782d0 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
782e0 4c 69 73 74 28 50 61 72 73 65 2a 2c 20 45 78 70  List(Parse*, Exp
782f0 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74  rList*, int, int
78300 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
78310 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
78320 70 72 49 66 54 72 75 65 28 50 61 72 73 65 2a 2c  prIfTrue(Parse*,
78330 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74   Expr*, int, int
78340 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
78350 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
78360 70 72 49 66 46 61 6c 73 65 28 50 61 72 73 65 2a  prIfFalse(Parse*
78370 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e  , Expr*, int, in
78380 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
78390 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
783a0 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74  3FindTable(sqlit
783b0 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
783c0 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53   const char*);.S
783d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61  QLITE_PRIVATE Ta
783e0 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61  ble *sqlite3Loca
783f0 74 65 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 69  teTable(Parse*,i
78400 6e 74 20 69 73 56 69 65 77 2c 63 6f 6e 73 74 20  nt isView,const 
78410 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61  char*, const cha
78420 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
78430 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74  ATE Index *sqlit
78440 65 33 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69  e3FindIndex(sqli
78450 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
78460 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  , const char*);.
78470 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
78480 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  oid sqlite3Unlin
78490 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28  kAndDeleteTable(
784a0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e  sqlite3*,int,con
784b0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
784c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
784d0 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
784e0 65 6c 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74  eleteIndex(sqlit
784f0 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  e3*,int,const ch
78500 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
78510 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
78520 33 56 61 63 75 75 6d 28 50 61 72 73 65 2a 29 3b  3Vacuum(Parse*);
78530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78540 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 56 61  int sqlite3RunVa
78550 63 75 75 6d 28 63 68 61 72 2a 2a 2c 20 73 71 6c  cuum(char**, sql
78560 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
78570 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
78580 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
78590 6e 28 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65  n(sqlite3*, Toke
785a0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
785b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
785c0 78 70 72 43 6f 6d 70 61 72 65 28 45 78 70 72 2a  xprCompare(Expr*
785d0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
785e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
785f0 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
78600 41 67 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43  Aggregates(NameC
78610 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b  ontext*, Expr*);
78620 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78630 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
78640 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e  AnalyzeAggList(N
78650 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 45 78 70 72  ameContext*,Expr
78660 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
78670 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c  RIVATE Vdbe *sql
78680 69 74 65 33 47 65 74 56 64 62 65 28 50 61 72 73  ite3GetVdbe(Pars
78690 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
786a0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
786b0 33 43 72 65 61 74 65 49 64 45 78 70 72 28 50 61  3CreateIdExpr(Pa
786c0 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  rse *, const cha
786d0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
786e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
786f0 50 72 6e 67 53 61 76 65 53 74 61 74 65 28 76 6f  PrngSaveState(vo
78700 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
78710 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
78720 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65  PrngRestoreState
78730 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50  (void);.SQLITE_P
78740 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
78750 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61 74  te3PrngResetStat
78760 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  e(void);.SQLITE_
78770 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
78780 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28  ite3RollbackAll(
78790 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
787a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
787b0 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
787c0 53 63 68 65 6d 61 28 50 61 72 73 65 2a 2c 20 69  Schema(Parse*, i
787d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
787e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
787f0 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  BeginTransaction
78800 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53  (Parse*, int);.S
78810 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
78820 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74  id sqlite3Commit
78830 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73  Transaction(Pars
78840 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
78850 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
78860 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74  RollbackTransact
78870 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ion(Parse*);.SQL
78880 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
78890 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e   sqlite3Savepoin
788a0 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 54  t(Parse*, int, T
788b0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
788c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
788d0 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e  te3CloseSavepoin
788e0 74 73 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53  ts(sqlite3 *);.S
788f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
78900 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43  t sqlite3ExprIsC
78910 6f 6e 73 74 61 6e 74 28 45 78 70 72 2a 29 3b 0a  onstant(Expr*);.
78920 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
78930 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  nt sqlite3ExprIs
78940 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28  ConstantNotJoin(
78950 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
78960 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
78970 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
78980 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 2a  OrFunction(Expr*
78990 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
789a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
789b0 72 49 73 49 6e 74 65 67 65 72 28 45 78 70 72 2a  rIsInteger(Expr*
789c0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  , int*);.SQLITE_
789d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
789e0 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74  te3IsRowid(const
789f0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
78a00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
78a10 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44  ite3GenerateRowD
78a20 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61  elete(Parse*, Ta
78a30 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ble*, int, int, 
78a40 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
78a50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
78a60 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
78a70 78 44 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20  xDelete(Parse*, 
78a80 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Table*, int, int
78a90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
78aa0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
78ab0 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 50  nerateIndexKey(P
78ac0 61 72 73 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69  arse*, Index*, i
78ad0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  nt, int, int);.S
78ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
78af0 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  id sqlite3Genera
78b00 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
78b10 6b 73 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a  ks(Parse*,Table*
78b20 2c 69 6e 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20  ,int,int,.      
78b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
78b50 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c  nt*,int,int,int,
78b60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
78b70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
78b80 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69  3CompleteInserti
78b90 6f 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65  on(Parse*, Table
78ba0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
78bb0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
78bc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
78bd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65  E int sqlite3Ope
78be0 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73  nTableAndIndices
78bf0 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
78c00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
78c10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
78c20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
78c30 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65  eOperation(Parse
78c40 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
78c50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
78c60 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75  r *sqlite3ExprDu
78c70 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a  p(sqlite3*,Expr*
78c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
78c90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 6f  E void sqlite3To
78ca0 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33 2a  kenCopy(sqlite3*
78cb0 2c 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29  ,Token*, Token*)
78cc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
78cd0 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74   ExprList *sqlit
78ce0 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 73 71  e3ExprListDup(sq
78cf0 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69 73 74 2a  lite3*,ExprList*
78d00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
78d10 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
78d20 65 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 6c  e3SrcListDup(sql
78d30 69 74 65 33 2a 2c 53 72 63 4c 69 73 74 2a 29 3b  ite3*,SrcList*);
78d40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78d50 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49  IdList *sqlite3I
78d60 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33  dListDup(sqlite3
78d70 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49  *,IdList*);.SQLI
78d80 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63  TE_PRIVATE Selec
78d90 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74  t *sqlite3Select
78da0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c  Dup(sqlite3*,Sel
78db0 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ect*);.SQLITE_PR
78dc0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
78dd0 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28  e3FuncDefInsert(
78de0 46 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46 75  FuncDefHash*, Fu
78df0 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f  ncDef*);.SQLITE_
78e00 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20  PRIVATE FuncDef 
78e10 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63  *sqlite3FindFunc
78e20 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f  tion(sqlite3*,co
78e30 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e  nst char*,int,in
78e40 74 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  t,u8,int);.SQLIT
78e50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
78e60 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75  qlite3RegisterBu
78e70 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73  iltinFunctions(s
78e80 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
78e90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
78ea0 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74  lite3RegisterDat
78eb0 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76  eTimeFunctions(v
78ec0 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
78ed0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
78ee0 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46  3RegisterGlobalF
78ef0 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a  unctions(void);.
78f00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
78f10 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
78f20 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
78f30 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33  SafetyOn(sqlite3
78f40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
78f50 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
78f60 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65  SafetyOff(sqlite
78f70 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  3*);.#else.# def
78f80 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74  ine sqlite3Safet
78f90 79 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 6e  yOn(A) 0.# defin
78fa0 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  e sqlite3SafetyO
78fb0 66 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a 53  ff(A) 0.#endif.S
78fc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
78fd0 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  t sqlite3SafetyC
78fe0 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29  heckOk(sqlite3*)
78ff0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
79000 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65   int sqlite3Safe
79010 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
79020 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
79030 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
79040 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
79050 69 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b  ie(Parse*, int);
79060 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
79070 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
79080 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
79090 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
790a0 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
790b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74   void sqlite3Mat
790c0 65 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 72  erializeView(Par
790d0 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70  se*, Table*, Exp
790e0 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66  r*, int);.#endif
790f0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
79100 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51  _OMIT_TRIGGER.SQ
79110 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
79120 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e  oid sqlite3Begin
79130 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20  Trigger(Parse*, 
79140 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e  Token*,Token*,in
79150 74 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72  t,int,IdList*,Sr
79160 63 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20  cList*,.        
79170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79180 20 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e     Expr*,int, in
79190 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
791a0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
791b0 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 50  3FinishTrigger(P
791c0 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 74  arse*, TriggerSt
791d0 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  ep*, Token*);.SQ
791e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
791f0 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
79200 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 53  rigger(Parse*, S
79210 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53  rcList*, int);.S
79220 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
79230 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
79240 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65  TriggerPtr(Parse
79250 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51  *, Trigger*);.SQ
79260 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
79270 6e 74 20 73 71 6c 69 74 65 33 54 72 69 67 67 65  nt sqlite3Trigge
79280 72 73 45 78 69 73 74 28 54 61 62 6c 65 2a 2c 20  rsExist(Table*, 
79290 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  int, ExprList*);
792a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
792b0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64    int sqlite3Cod
792c0 65 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73  eRowTrigger(Pars
792d0 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73  e*, int, ExprLis
792e0 74 2a 2c 20 69 6e 74 2c 20 54 61 62 6c 65 20 2a  t*, int, Table *
792f0 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 0a 20 20 20  , int, int, .   
79300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79310 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74          int, int
79320 2c 20 75 33 32 2a 2c 20 75 33 32 2a 29 3b 0a 20  , u32*, u32*);. 
79330 20 76 6f 69 64 20 73 71 6c 69 74 65 56 69 65 77   void sqliteView
79340 54 72 69 67 67 65 72 73 28 50 61 72 73 65 2a 2c  Triggers(Parse*,
79350 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20   Table*, Expr*, 
79360 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  int, ExprList*);
79370 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
79380 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65    void sqlite3De
79390 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28  leteTriggerStep(
793a0 73 71 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65  sqlite3*, Trigge
793b0 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f  rStep*);.SQLITE_
793c0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65  PRIVATE   Trigge
793d0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
793e0 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70 28  iggerSelectStep(
793f0 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a  sqlite3*,Select*
79400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
79410 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20  E   TriggerStep 
79420 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 49  *sqlite3TriggerI
79430 6e 73 65 72 74 53 74 65 70 28 73 71 6c 69 74 65  nsertStep(sqlite
79440 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 49 64 4c 69 73  3*,Token*, IdLis
79450 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t*,.            
79460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79470 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72              Expr
79480 4c 69 73 74 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e  List*,Select*,in
79490 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
794a0 54 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70  TE   TriggerStep
794b0 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72   *sqlite3Trigger
794c0 55 70 64 61 74 65 53 74 65 70 28 73 71 6c 69 74  UpdateStep(sqlit
794d0 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 45 78 70 72 4c  e3*,Token*,ExprL
794e0 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  ist*, Expr*, int
794f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
79500 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20  E   TriggerStep 
79510 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 44  *sqlite3TriggerD
79520 65 6c 65 74 65 53 74 65 70 28 73 71 6c 69 74 65  eleteStep(sqlite
79530 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 2a  3*,Token*, Expr*
79540 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
79550 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
79560 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71  DeleteTrigger(sq
79570 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 72 2a  lite3*, Trigger*
79580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
79590 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
795a0 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
795b0 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c  rigger(sqlite3*,
795c0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  int,const char*)
795d0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
795e0 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73   sqlite3Triggers
795f0 45 78 69 73 74 28 42 2c 43 2c 44 2c 45 2c 46 29  Exist(B,C,D,E,F)
79600 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
79610 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
79620 28 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73  (A,B).# define s
79630 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65  qlite3DropTrigge
79640 72 50 74 72 28 41 2c 42 29 0a 23 20 64 65 66 69  rPtr(A,B).# defi
79650 6e 65 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  ne sqlite3Unlink
79660 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72  AndDeleteTrigger
79670 28 41 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e 65  (A,B,C).# define
79680 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
79690 72 69 67 67 65 72 28 41 2c 42 2c 43 2c 44 2c 45  rigger(A,B,C,D,E
796a0 2c 46 2c 47 2c 48 2c 49 2c 4a 2c 4b 29 20 30 0a  ,F,G,H,I,J,K) 0.
796b0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
796c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
796d0 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65  e3JoinType(Parse
796e0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
796f0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
79700 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
79710 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72  sqlite3CreateFor
79720 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20  eignKey(Parse*, 
79730 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e  ExprList*, Token
79740 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
79750 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
79760 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
79770 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50  eferForeignKey(P
79780 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66  arse*, int);.#if
79790 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
797a0 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 53  _AUTHORIZATION.S
797b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
797c0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68  void sqlite3Auth
797d0 52 65 61 64 28 50 61 72 73 65 2a 2c 45 78 70 72  Read(Parse*,Expr
797e0 2a 2c 53 63 68 65 6d 61 2a 2c 53 72 63 4c 69 73  *,Schema*,SrcLis
797f0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
79800 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
79810 33 41 75 74 68 43 68 65 63 6b 28 50 61 72 73 65  3AuthCheck(Parse
79820 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  *,int, const cha
79830 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  r*, const char*,
79840 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53   const char*);.S
79850 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
79860 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68  void sqlite3Auth
79870 43 6f 6e 74 65 78 74 50 75 73 68 28 50 61 72 73  ContextPush(Pars
79880 65 2a 2c 20 41 75 74 68 43 6f 6e 74 65 78 74 2a  e*, AuthContext*
79890 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  , const char*);.
798a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
798b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
798c0 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68  hContextPop(Auth
798d0 43 6f 6e 74 65 78 74 2a 29 3b 0a 23 65 6c 73 65  Context*);.#else
798e0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
798f0 33 41 75 74 68 52 65 61 64 28 61 2c 62 2c 63 2c  3AuthRead(a,b,c,
79900 64 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  d).# define sqli
79910 74 65 33 41 75 74 68 43 68 65 63 6b 28 61 2c 62  te3AuthCheck(a,b
79920 2c 63 2c 64 2c 65 29 20 20 20 20 53 51 4c 49 54  ,c,d,e)    SQLIT
79930 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71  E_OK.# define sq
79940 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
79950 50 75 73 68 28 61 2c 62 2c 63 29 0a 23 20 64 65  Push(a,b,c).# de
79960 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68  fine sqlite3Auth
79970 43 6f 6e 74 65 78 74 50 6f 70 28 61 29 20 20 28  ContextPop(a)  (
79980 28 76 6f 69 64 29 28 61 29 29 0a 23 65 6e 64 69  (void)(a)).#endi
79990 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
799a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74   void sqlite3Att
799b0 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72  ach(Parse*, Expr
799c0 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 29  *, Expr*, Expr*)
799d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
799e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74   void sqlite3Det
799f0 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72  ach(Parse*, Expr
79a00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
79a10 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
79a20 72 65 65 46 61 63 74 6f 72 79 28 63 6f 6e 73 74  reeFactory(const
79a30 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f   sqlite3 *db, co
79a40 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
79a50 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ame,.           
79a60 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
79a70 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74  omitJournal, int
79a80 20 6e 43 61 63 68 65 2c 20 69 6e 74 20 66 6c 61   nCache, int fla
79a90 67 73 2c 20 42 74 72 65 65 20 2a 2a 70 70 42 74  gs, Btree **ppBt
79aa0 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ree);.SQLITE_PRI
79ab0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
79ac0 46 69 78 49 6e 69 74 28 44 62 46 69 78 65 72 2a  FixInit(DbFixer*
79ad0 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63  , Parse*, int, c
79ae0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
79af0 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  t Token*);.SQLIT
79b00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
79b10 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28  lite3FixSrcList(
79b20 44 62 46 69 78 65 72 2a 2c 20 53 72 63 4c 69 73  DbFixer*, SrcLis
79b30 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
79b40 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
79b50 69 78 53 65 6c 65 63 74 28 44 62 46 69 78 65 72  ixSelect(DbFixer
79b60 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
79b70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
79b80 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 44  sqlite3FixExpr(D
79b90 62 46 69 78 65 72 2a 2c 20 45 78 70 72 2a 29 3b  bFixer*, Expr*);
79ba0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
79bb0 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78  int sqlite3FixEx
79bc0 70 72 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c  prList(DbFixer*,
79bd0 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c   ExprList*);.SQL
79be0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
79bf0 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65  sqlite3FixTrigge
79c00 72 53 74 65 70 28 44 62 46 69 78 65 72 2a 2c 20  rStep(DbFixer*, 
79c10 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53  TriggerStep*);.S
79c20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
79c30 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63 6f  t sqlite3AtoF(co
79c40 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75  nst char *z, dou
79c50 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ble*);.SQLITE_PR
79c60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
79c70 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20  3GetInt32(const 
79c80 63 68 61 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53  char *, int*);.S
79c90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
79ca0 74 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36  t sqlite3FitsIn6
79cb0 34 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72  4Bits(const char
79cc0 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
79cd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
79ce0 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e  ite3Utf16ByteLen
79cf0 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61  (const void *pDa
79d00 74 61 2c 20 69 6e 74 20 6e 43 68 61 72 29 3b 0a  ta, int nChar);.
79d10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
79d20 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 43 68  nt sqlite3Utf8Ch
79d30 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61 72  arLen(const char
79d40 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 42 79   *pData, int nBy
79d50 74 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  te);.SQLITE_PRIV
79d60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55  ATE int sqlite3U
79d70 74 66 38 52 65 61 64 28 63 6f 6e 73 74 20 75 38  tf8Read(const u8
79d80 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f  *, const u8*, co
79d90 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a  nst u8**);../*.*
79da0 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 65  * Routines to re
79db0 61 64 20 61 6e 64 20 77 72 69 74 65 20 76 61 72  ad and write var
79dc0 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
79dd0 65 67 65 72 73 2e 20 20 54 68 65 73 65 20 75 73  egers.  These us
79de0 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66 69  ed to.** be defi
79df0 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 74  ned locally, but
79e00 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65 20   now we use the 
79e10 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 20  varint routines 
79e20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a 2a  in the util.c.**
79e30 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68 6f   file.  Code sho
79e40 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 52  uld use the MACR
79e50 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20 61  O forms below, a
79e60 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 20 76  s the Varint32 v
79e70 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 63  ersions.** are c
79e80 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20 74  oded to assume t
79e90 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63  he single byte c
79ea0 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 68  ase is already h
79eb0 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a 2a  andled (which .*
79ec0 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d  * the MACRO form
79ed0 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54   does)..*/.SQLIT
79ee0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
79ef0 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 75  lite3PutVarint(u
79f00 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75  nsigned char*, u
79f10 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
79f20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
79f30 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67  utVarint32(unsig
79f40 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29 3b  ned char*, u32);
79f50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
79f60 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 72  u8 sqlite3GetVar
79f70 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  int(const unsign
79f80 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20 2a  ed char *, u64 *
79f90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
79fa0 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56  E u8 sqlite3GetV
79fb0 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e  arint32(const un
79fc0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75  signed char *, u
79fd0 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  32 *);.SQLITE_PR
79fe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
79ff0 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76  3VarintLen(u64 v
7a000 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65  );../*.** The he
7a010 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72 64  ader of a record
7a020 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73   consists of a s
7a030 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c 65  equence variable
7a040 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73  -length integers
7a050 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67  ..** These integ
7a060 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20 61  ers are almost a
7a070 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64 20  lways small and 
7a080 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61  are encoded as a
7a090 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a   single byte..**
7a0a0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   The following m
7a0b0 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61 6e  acros take advan
7a0c0 74 61 67 65 20 74 68 69 73 20 66 61 63 74 20 74  tage this fact t
7a0d0 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 73 74  o provide a fast
7a0e0 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 64   encode.** and d
7a0f0 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e 74  ecode of the int
7a100 65 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f 72  egers in a recor
7a110 64 20 68 65 61 64 65 72 2e 20 20 49 74 20 69 73  d header.  It is
7a120 20 66 61 73 74 65 72 20 66 6f 72 20 74 68 65 20   faster for the 
7a130 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77  common.** case w
7a140 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 65 72  here the integer
7a150 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74   is a single byt
7a160 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74 74  e.  It is a litt
7a170 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 74  le slower when t
7a180 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 73  he.** integer is
7a190 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79 74   two or more byt
7a1a0 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c 6c  es.  But overall
7a1b0 20 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a 2a   it is faster..*
7a1c0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
7a1d0 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  ng expressions a
7a1e0 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a  re equivalent:.*
7a1f0 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c  *.**     x = sql
7a200 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28  ite3GetVarint32(
7a210 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20 20   A, &B );.**    
7a220 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74 56   x = sqlite3PutV
7a230 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b  arint32( A, B );
7a240 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 67  .**.**     x = g
7a250 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42  etVarint32( A, B
7a260 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 70   );.**     x = p
7a270 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42  utVarint32( A, B
7a280 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e   );.**.*/.#defin
7a290 65 20 67 65 74 56 61 72 69 6e 74 33 32 28 41 2c  e getVarint32(A,
7a2a0 42 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c 28  B)  (u8)((*(A)<(
7a2b0 75 38 29 30 78 38 30 29 20 3f 20 28 28 42 29 20  u8)0x80) ? ((B) 
7a2c0 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20 3a  = (u32)*(A)),1 :
7a2d0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
7a2e0 74 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a 29  t32((A), (u32 *)
7a2f0 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20 70  &(B))).#define p
7a300 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20  utVarint32(A,B) 
7a310 20 28 75 38 29 28 28 28 75 33 32 29 28 42 29 3c   (u8)(((u32)(B)<
7a320 28 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a 28  (u32)0x80) ? (*(
7a330 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  A) = (unsigned c
7a340 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71 6c  har)(B)),1 : sql
7a350 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28  ite3PutVarint32(
7a360 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66 69  (A), (B))).#defi
7a370 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20 20 20  ne getVarint    
7a380 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
7a390 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 69  .#define putVari
7a3a0 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50 75 74  nt    sqlite3Put
7a3b0 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45 5f  Varint...SQLITE_
7a3c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
7a3d0 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
7a3e0 79 53 74 72 28 56 64 62 65 20 2a 2c 20 49 6e 64  yStr(Vdbe *, Ind
7a3f0 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ex *);.SQLITE_PR
7a400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
7a410 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53  e3TableAffinityS
7a420 74 72 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65  tr(Vdbe *, Table
7a430 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
7a440 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
7a450 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
7a460 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
7a470 72 20 61 66 66 32 29 3b 0a 53 51 4c 49 54 45 5f  r aff2);.SQLITE_
7a480 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
7a490 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79  te3IndexAffinity
7a4a0 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20  Ok(Expr *pExpr, 
7a4b0 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74  char idx_affinit
7a4c0 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  y);.SQLITE_PRIVA
7a4d0 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45  TE char sqlite3E
7a4e0 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72  xprAffinity(Expr
7a4f0 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45   *pExpr);.SQLITE
7a500 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
7a510 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74  ite3Atoi64(const
7a520 20 63 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a 53   char*, i64*);.S
7a530 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7a540 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  id sqlite3Error(
7a550 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63  sqlite3*, int, c
7a560 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
7a570 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7a580 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78  void *sqlite3Hex
7a590 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a 2c  ToBlob(sqlite3*,
7a5a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
7a5b0 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50  int n);.SQLITE_P
7a5c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
7a5d0 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 50 61  e3TwoPartName(Pa
7a5e0 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20  rse *, Token *, 
7a5f0 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a  Token *, Token *
7a600 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
7a610 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
7a620 71 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e 74  qlite3ErrStr(int
7a630 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7a640 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61  E int sqlite3Rea
7a650 64 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70  dSchema(Parse *p
7a660 50 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50  Parse);.SQLITE_P
7a670 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
7a680 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
7a690 65 71 28 73 71 6c 69 74 65 33 2a 2c 75 38 20 65  eq(sqlite3*,u8 e
7a6a0 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  nc, const char *
7a6b0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
7a6c0 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
7a6d0 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  q *sqlite3Locate
7a6e0 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70  CollSeq(Parse *p
7a6f0 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
7a700 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e  r *zName, int nN
7a710 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ame);.SQLITE_PRI
7a720 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
7a730 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
7a740 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
7a750 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51  Expr *pExpr);.SQ
7a760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
7a770 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65  r *sqlite3ExprSe
7a780 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61  tColl(Parse *pPa
7a790 72 73 65 2c 20 45 78 70 72 20 2a 2c 20 54 6f 6b  rse, Expr *, Tok
7a7a0 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  en *);.SQLITE_PR
7a7b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7a7c0 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61  3CheckCollSeq(Pa
7a7d0 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a  rse *, CollSeq *
7a7e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7a7f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65  E int sqlite3Che
7a800 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72  ckObjectName(Par
7a810 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  se *, const char
7a820 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
7a830 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
7a840 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 73  VdbeSetChanges(s
7a850 71 6c 69 74 65 33 20 2a 2c 20 69 6e 74 29 3b 0a  qlite3 *, int);.
7a860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7a870 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
7a880 74 65 33 56 61 6c 75 65 54 65 78 74 28 73 71 6c  te3ValueText(sql
7a890 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29  ite3_value*, u8)
7a8a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
7a8b0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75   int sqlite3Valu
7a8c0 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76  eBytes(sqlite3_v
7a8d0 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49  alue*, u8);.SQLI
7a8e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
7a8f0 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
7a900 74 72 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  tr(sqlite3_value
7a910 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f  *, int, const vo
7a920 69 64 20 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20  id *,u8, .      
7a930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7a940 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29    void(*)(void*)
7a950 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7a960 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
7a970 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f  lueFree(sqlite3_
7a980 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
7a990 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
7a9a0 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61  value *sqlite3Va
7a9b0 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a  lueNew(sqlite3 *
7a9c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7a9d0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55  E char *sqlite3U
7a9e0 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20  tf16to8(sqlite3 
7a9f0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
7aa00 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
7aa10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
7aa20 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 71  ValueFromExpr(sq
7aa30 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 20 2a 2c  lite3 *, Expr *,
7aa40 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 74 65 33   u8, u8, sqlite3
7aa50 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c 49  _value **);.SQLI
7aa60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
7aa70 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c  sqlite3ValueAppl
7aa80 79 41 66 66 69 6e 69 74 79 28 73 71 6c 69 74 65  yAffinity(sqlite
7aa90 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c 20 75  3_value *, u8, u
7aaa0 38 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  8);.#ifndef SQLI
7aab0 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a  TE_AMALGAMATION.
7aac0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
7aad0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
7aae0 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54  ar sqlite3UpperT
7aaf0 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 4c 49 54 45  oLower[];.SQLITE
7ab00 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f  _PRIVATE SQLITE_
7ab10 57 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 74  WSD struct Sqlit
7ab20 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33  e3Config sqlite3
7ab30 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f 50  Config;.SQLITE_P
7ab40 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53  RIVATE SQLITE_WS
7ab50 44 20 46 75 6e 63 44 65 66 48 61 73 68 20 73 71  D FuncDefHash sq
7ab60 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74  lite3GlobalFunct
7ab70 69 6f 6e 73 3b 0a 23 65 6e 64 69 66 0a 53 51 4c  ions;.#endif.SQL
7ab80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
7ab90 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 67 65   sqlite3RootPage
7aba0 4d 6f 76 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20  Moved(Db*, int, 
7abb0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
7abc0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
7abd0 33 52 65 69 6e 64 65 78 28 50 61 72 73 65 2a 2c  3Reindex(Parse*,
7abe0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29   Token*, Token*)
7abf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
7ac00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74   void sqlite3Alt
7ac10 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  erFunctions(sqli
7ac20 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
7ac30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
7ac40 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62  e3AlterRenameTab
7ac50 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  le(Parse*, SrcLi
7ac60 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  st*, Token*);.SQ
7ac70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
7ac80 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
7ac90 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
7aca0 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a  char *, int *);.
7acb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
7acc0 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65  oid sqlite3Neste
7acd0 64 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 63  dParse(Parse*, c
7ace0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
7acf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
7ad00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
7ad10 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
7ad20 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 2a 29 3b  ments(sqlite3*);
7ad30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7ad40 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65  void sqlite3Code
7ad50 53 75 62 73 65 6c 65 63 74 28 50 61 72 73 65 20  Subselect(Parse 
7ad60 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2c 20  *, Expr *, int, 
7ad70 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
7ad80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
7ad90 33 53 65 6c 65 63 74 50 72 65 70 28 50 61 72 73  3SelectPrep(Pars
7ada0 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d  e*, Select*, Nam
7adb0 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49  eContext*);.SQLI
7adc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
7add0 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
7ade0 72 4e 61 6d 65 73 28 4e 61 6d 65 43 6f 6e 74 65  rNames(NameConte
7adf0 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  xt*, Expr*);.SQL
7ae00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
7ae10 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53   sqlite3ResolveS
7ae20 65 6c 65 63 74 4e 61 6d 65 73 28 50 61 72 73 65  electNames(Parse
7ae30 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65  *, Select*, Name
7ae40 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  Context*);.SQLIT
7ae50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7ae60 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65  lite3ResolveOrde
7ae70 72 47 72 6f 75 70 42 79 28 50 61 72 73 65 2a 2c  rGroupBy(Parse*,
7ae80 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 4c 69   Select*, ExprLi
7ae90 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  st*, const char*
7aea0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7aeb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
7aec0 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 65  lumnDefault(Vdbe
7aed0 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74   *, Table *, int
7aee0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7aef0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
7af00 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75  terFinishAddColu
7af10 6d 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65  mn(Parse *, Toke
7af20 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  n *);.SQLITE_PRI
7af30 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
7af40 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f  3AlterBeginAddCo
7af50 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53 72  lumn(Parse *, Sr
7af60 63 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45  cList *);.SQLITE
7af70 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71  _PRIVATE CollSeq
7af80 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c   *sqlite3GetColl
7af90 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 43 6f  Seq(sqlite3*, Co
7afa0 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63  llSeq *, const c
7afb0 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  har *, int);.SQL
7afc0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
7afd0 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79   sqlite3Affinity
7afe0 54 79 70 65 28 63 6f 6e 73 74 20 54 6f 6b 65 6e  Type(const Token
7aff0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
7b000 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
7b010 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20 54  nalyze(Parse*, T
7b020 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  oken*, Token*);.
7b030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
7b040 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65  nt sqlite3Invoke
7b050 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79  BusyHandler(Busy
7b060 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c 49 54  Handler*);.SQLIT
7b070 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7b080 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69  lite3FindDb(sqli
7b090 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53  te3*, Token*);.S
7b0a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
7b0b0 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69  t sqlite3Analysi
7b0c0 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c 69  sLoad(sqlite3*,i
7b0d0 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54 45 5f  nt iDB);.SQLITE_
7b0e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
7b0f0 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73  ite3DefaultRowEs
7b100 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54  t(Index*);.SQLIT
7b110 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
7b120 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69  qlite3RegisterLi
7b130 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  keFunctions(sqli
7b140 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  te3*, int);.SQLI
7b150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
7b160 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63  qlite3IsLikeFunc
7b170 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 45 78  tion(sqlite3*,Ex
7b180 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 29 3b  pr*,int*,char*);
7b190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7b1a0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 6e 69  void sqlite3Mini
7b1b0 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 50 61  mumFileFormat(Pa
7b1c0 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  rse*, int, int);
7b1d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7b1e0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65  void sqlite3Sche
7b1f0 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29 3b 0a  maFree(void *);.
7b200 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
7b210 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53 63  chema *sqlite3Sc
7b220 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33 20  hemaGet(sqlite3 
7b230 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c  *, Btree *);.SQL
7b240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7b250 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
7b260 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62  ndex(sqlite3 *db
7b270 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 51 4c  , Schema *);.SQL
7b280 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65 79 49  ITE_PRIVATE KeyI
7b290 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64 65  nfo *sqlite3Inde
7b2a0 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 20 2a  xKeyinfo(Parse *
7b2b0 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49  , Index *);.SQLI
7b2c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
7b2d0 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
7b2e0 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73  (sqlite3 *, cons
7b2f0 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69  t char *, int, i
7b300 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 20 76  nt, void *, .  v
7b310 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f  oid (*)(sqlite3_
7b320 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
7b330 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a  ite3_value **),.
7b340 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74    void (*)(sqlit
7b350 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
7b360 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
7b370 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69  ), void (*)(sqli
7b380 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a  te3_context*));.
7b390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
7b3a0 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  nt sqlite3ApiExi
7b3b0 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  t(sqlite3 *db, i
7b3c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
7b3d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
7b3e0 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28  penTempDatabase(
7b3f0 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c 49 54  Parse *);..SQLIT
7b400 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
7b410 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
7b420 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 63 68  it(StrAccum*, ch
7b430 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  ar*, int, int);.
7b440 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
7b450 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63  oid sqlite3StrAc
7b460 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63  cumAppend(StrAcc
7b470 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  um*,const char*,
7b480 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
7b490 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
7b4a0 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68  e3StrAccumFinish
7b4b0 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c  (StrAccum*);.SQL
7b4c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
7b4d0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
7b4e0 52 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a 29  Reset(StrAccum*)
7b4f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
7b500 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c   void sqlite3Sel
7b510 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65  ectDestInit(Sele
7b520 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29  ctDest*,int,int)
7b530 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74  ;../*.** The int
7b540 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c 45  erface to the LE
7b550 4d 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 61  MON-generated pa
7b560 72 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rser.*/.SQLITE_P
7b570 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
7b580 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28  ite3ParserAlloc(
7b590 76 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29  void*(*)(size_t)
7b5a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7b5b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
7b5c0 72 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c 20  rserFree(void*, 
7b5d0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
7b5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7b5f0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73  void sqlite3Pars
7b600 65 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 54  er(void*, int, T
7b610 6f 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a 23  oken, Parse*);.#
7b620 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58  ifdef YYTRACKMAX
7b630 53 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54  STACKDEPTH.SQLIT
7b640 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
7b650 73 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61  sqlite3ParserSta
7b660 63 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a 23  ckPeak(void*);.#
7b670 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
7b680 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7b690 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69  3AutoLoadExtensi
7b6a0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23  ons(sqlite3*);.#
7b6b0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
7b6c0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
7b6d0 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  N.SQLITE_PRIVATE
7b6e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43     void sqlite3C
7b6f0 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73  loseExtensions(s
7b700 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a  qlite3*);.#else.
7b710 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
7b720 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28  CloseExtensions(
7b730 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  X).#endif..#ifnd
7b740 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
7b750 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49  HARED_CACHE.SQLI
7b760 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
7b770 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f  d sqlite3TableLo
7b780 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c  ck(Parse *, int,
7b790 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20   int, u8, const 
7b7a0 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20  char *);.#else. 
7b7b0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
7b7c0 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c  TableLock(v,w,x,
7b7d0 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66  y,z).#endif..#if
7b7e0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
7b7f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
7b800 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38   int sqlite3Utf8
7b810 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61  To8(unsigned cha
7b820 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  r*);.#endif..#if
7b830 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
7b840 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20  VIRTUALTABLE.#  
7b850 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74  define sqlite3Vt
7b860 61 62 43 6c 65 61 72 28 58 29 0a 23 20 20 64 65  abClear(X).#  de
7b870 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
7b880 53 79 6e 63 28 58 2c 59 29 20 53 51 4c 49 54 45  Sync(X,Y) SQLITE
7b890 5f 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20 73 71  _OK.#  define sq
7b8a0 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
7b8b0 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73  k(X).#  define s
7b8c0 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74  qlite3VtabCommit
7b8d0 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71  (X).#  define sq
7b8e0 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28  lite3VtabInSync(
7b8f0 64 62 29 20 30 0a 23 65 6c 73 65 0a 53 51 4c 49  db) 0.#else.SQLI
7b900 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f  TE_PRIVATE    vo
7b910 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c  id sqlite3VtabCl
7b920 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c  ear(Table*);.SQL
7b930 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69  ITE_PRIVATE    i
7b940 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79  nt sqlite3VtabSy
7b950 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  nc(sqlite3 *db, 
7b960 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45  char **);.SQLITE
7b970 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20  _PRIVATE    int 
7b980 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62  sqlite3VtabRollb
7b990 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ack(sqlite3 *db)
7b9a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
7b9b0 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56      int sqlite3V
7b9c0 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65  tabCommit(sqlite
7b9d0 33 20 2a 64 62 29 3b 0a 23 20 20 64 65 66 69 6e  3 *db);.#  defin
7b9e0 65 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53  e sqlite3VtabInS
7b9f0 79 6e 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e  ync(db) ((db)->n
7ba00 56 54 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29  VTrans>0 && (db)
7ba10 2d 3e 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65  ->aVTrans==0).#e
7ba20 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
7ba30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
7ba40 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65  VtabMakeWritable
7ba50 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b  (Parse*,Table*);
7ba60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7ba70 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
7ba80 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 76 74 61  Lock(sqlite3_vta
7ba90 62 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  b*);.SQLITE_PRIV
7baa0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
7bab0 56 74 61 62 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  VtabUnlock(sqlit
7bac0 65 33 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61  e3*, sqlite3_vta
7bad0 62 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  b*);.SQLITE_PRIV
7bae0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
7baf0 56 74 61 62 42 65 67 69 6e 50 61 72 73 65 28 50  VtabBeginParse(P
7bb00 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54  arse*, Token*, T
7bb10 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  oken*, Token*);.
7bb20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
7bb30 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 46  oid sqlite3VtabF
7bb40 69 6e 69 73 68 50 61 72 73 65 28 50 61 72 73 65  inishParse(Parse
7bb50 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
7bb60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
7bb70 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e  sqlite3VtabArgIn
7bb80 69 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  it(Parse*);.SQLI
7bb90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
7bba0 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78  sqlite3VtabArgEx
7bbb0 74 65 6e 64 28 50 61 72 73 65 2a 2c 20 54 6f 6b  tend(Parse*, Tok
7bbc0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
7bbd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
7bbe0 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 73  VtabCallCreate(s
7bbf0 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f  qlite3*, int, co
7bc00 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 68 61 72  nst char *, char
7bc10 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   **);.SQLITE_PRI
7bc20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
7bc30 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28  VtabCallConnect(
7bc40 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b  Parse*, Table*);
7bc50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7bc60 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43  int sqlite3VtabC
7bc70 61 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c 69 74  allDestroy(sqlit
7bc80 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  e3*, int, const 
7bc90 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  char *);.SQLITE_
7bca0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
7bcb0 74 65 33 56 74 61 62 42 65 67 69 6e 28 73 71 6c  te3VtabBegin(sql
7bcc0 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 5f  ite3 *, sqlite3_
7bcd0 76 74 61 62 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  vtab *);.SQLITE_
7bce0 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20  PRIVATE FuncDef 
7bcf0 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72  *sqlite3VtabOver
7bd00 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c  loadFunction(sql
7bd10 69 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a 2c  ite3 *,FuncDef*,
7bd20 20 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72 2a   int nArg, Expr*
7bd30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7bd40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e  E void sqlite3In
7bd50 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73 71  validFunction(sq
7bd60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
7bd70 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
7bd80 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
7bd90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54  ATE int sqlite3T
7bda0 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28  ransferBindings(
7bdb0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20  sqlite3_stmt *, 
7bdc0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b  sqlite3_stmt *);
7bdd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7bde0 69 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65  int sqlite3Repre
7bdf0 70 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c  pare(Vdbe*);.SQL
7be00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
7be10 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
7be20 43 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72 73  CheckLength(Pars
7be30 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63  e*, ExprList*, c
7be40 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
7be50 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c  ITE_PRIVATE Coll
7be60 53 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61  Seq *sqlite3Bina
7be70 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71  ryCompareCollSeq
7be80 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a  (Parse *, Expr *
7be90 2c 20 45 78 70 72 20 2a 29 3b 0a 0a 0a 2f 2a 0a  , Expr *);.../*.
7bea0 2a 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61 75  ** Available fau
7beb0 6c 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20 53  lt injectors.  S
7bec0 68 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72 65  hould be numbere
7bed0 64 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  d beginning with
7bee0 20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53   0..*/.#define S
7bef0 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43  QLITE_FAULTINJEC
7bf00 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 30  TOR_MALLOC     0
7bf10 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7bf20 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f  FAULTINJECTOR_CO
7bf30 55 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a  UNT      1../*.*
7bf40 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
7bf50 74 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 66  to the code in f
7bf60 61 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72 20  ault.c used for 
7bf70 69 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65 6e  identifying "ben
7bf80 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66  ign".** malloc f
7bf90 61 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69 73  ailures. This is
7bfa0 20 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69 66   only present if
7bfb0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
7bfc0 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73 20  LTIN_TEST.** is 
7bfd0 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  not defined..*/.
7bfe0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
7bff0 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
7c000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7c010 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65    void sqlite3Be
7c020 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
7c030 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
7c040 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
7c050 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
7c060 6c 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65  loc(void);.#else
7c070 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
7c080 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
7c090 6c 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65 20  loc().  #define 
7c0a0 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
7c0b0 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66 0a  Malloc().#endif.
7c0c0 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45  .#define IN_INDE
7c0d0 58 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20 20  X_ROWID         
7c0e0 20 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49    1.#define IN_I
7c0f0 4e 44 45 58 5f 45 50 48 20 20 20 20 20 20 20 20  NDEX_EPH        
7c100 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 49       2.#define I
7c110 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20 20  N_INDEX_INDEX   
7c120 20 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54 45          3.SQLITE
7c130 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
7c140 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28  ite3FindInIndex(
7c150 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c  Parse *, Expr *,
7c160 20 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66 20   int*);..#ifdef 
7c170 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
7c180 4f 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49 54  OMIC_WRITE.SQLIT
7c190 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
7c1a0 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70  sqlite3JournalOp
7c1b0 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  en(sqlite3_vfs *
7c1c0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
7c1d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c 20  sqlite3_file *, 
7c1e0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
7c1f0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
7c200 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69  sqlite3JournalSi
7c210 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ze(sqlite3_vfs *
7c220 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7c230 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  E   int sqlite3J
7c240 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c  ournalCreate(sql
7c250 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23 65  ite3_file *);.#e
7c260 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
7c270 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65  lite3JournalSize
7c280 28 70 56 66 73 29 20 28 28 70 56 66 73 29 2d 3e  (pVfs) ((pVfs)->
7c290 73 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69 66  szOsFile).#endif
7c2a0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
7c2b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
7c2c0 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69  JournalOpen(sqli
7c2d0 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51 4c  te3_file *);.SQL
7c2e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7c2f0 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
7c300 6c 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c  lSize(void);.SQL
7c310 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7c320 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72  sqlite3IsMemJour
7c330 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  nal(sqlite3_file
7c340 20 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45   *);..#if SQLITE
7c350 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e  _MAX_EXPR_DEPTH>
7c360 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  0.SQLITE_PRIVATE
7c370 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45     void sqlite3E
7c380 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72  xprSetHeight(Par
7c390 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
7c3a0 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   *p);.SQLITE_PRI
7c3b0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
7c3c0 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67  e3SelectExprHeig
7c3d0 68 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53 51  ht(Select *);.SQ
7c3e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
7c3f0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68  nt sqlite3ExprCh
7c400 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 2a  eckHeight(Parse*
7c410 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 20  , int);.#else.  
7c420 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45  #define sqlite3E
7c430 78 70 72 53 65 74 48 65 69 67 68 74 28 78 2c 79  xprSetHeight(x,y
7c440 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ).  #define sqli
7c450 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69  te3SelectExprHei
7c460 67 68 74 28 78 29 20 30 0a 20 20 23 64 65 66 69  ght(x) 0.  #defi
7c470 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 68  ne sqlite3ExprCh
7c480 65 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a 23  eckHeight(x,y).#
7c490 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
7c4a0 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
7c4b0 33 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20  3Get4byte(const 
7c4c0 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u8*);.SQLITE_PRI
7c4d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
7c4e0 33 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20 75  3Put4byte(u8*, u
7c4f0 33 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  32);..#ifdef SQL
7c500 49 54 45 5f 53 53 45 0a 23 69 6e 63 6c 75 64 65  ITE_SSE.#include
7c510 20 22 73 73 65 49 6e 74 2e 68 22 0a 23 65 6e 64   "sseInt.h".#end
7c520 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
7c530 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50  E_DEBUG.SQLITE_P
7c540 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
7c550 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65  lite3ParserTrace
7c560 28 46 49 4c 45 2a 2c 20 63 68 61 72 20 2a 29 3b  (FILE*, char *);
7c570 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
7c580 66 20 74 68 65 20 53 51 4c 49 54 45 5f 45 4e 41  f the SQLITE_ENA
7c590 42 4c 45 20 49 4f 54 52 41 43 45 20 65 78 69 73  BLE IOTRACE exis
7c5a0 74 73 20 74 68 65 6e 20 74 68 65 20 67 6c 6f 62  ts then the glob
7c5b0 61 6c 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 73  al variable.** s
7c5c0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 69 73  qlite3IoTrace is
7c5d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
7c5e0 70 72 69 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74  printf-like rout
7c5f0 69 6e 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70  ine used to.** p
7c600 72 69 6e 74 20 49 2f 4f 20 74 72 61 63 69 6e 67  rint I/O tracing
7c610 20 6d 65 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23   messages. .*/.#
7c620 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
7c630 42 4c 45 5f 49 4f 54 52 41 43 45 0a 23 20 64 65  BLE_IOTRACE.# de
7c640 66 69 6e 65 20 49 4f 54 52 41 43 45 28 41 29 20  fine IOTRACE(A) 
7c650 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72   if( sqlite3IoTr
7c660 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 49 6f  ace ){ sqlite3Io
7c670 54 72 61 63 65 20 41 3b 20 7d 0a 53 51 4c 49 54  Trace A; }.SQLIT
7c680 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
7c690 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72   sqlite3VdbeIOTr
7c6a0 61 63 65 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53  aceSql(Vdbe*);.S
7c6b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7c6c0 69 64 20 28 2a 73 71 6c 69 74 65 33 49 6f 54 72  id (*sqlite3IoTr
7c6d0 61 63 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  ace)(const char*
7c6e0 2c 2e 2e 2e 29 3b 0a 23 65 6c 73 65 0a 23 20 64  ,...);.#else.# d
7c6f0 65 66 69 6e 65 20 49 4f 54 52 41 43 45 28 41 29  efine IOTRACE(A)
7c700 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
7c710 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28  3VdbeIOTraceSql(
7c720 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  X).#endif..#endi
7c730 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
7c740 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65  ** End of sqlite
7c750 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
7c760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c780 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
7c790 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 67 6c  ** Begin file gl
7c7a0 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  obal.c *********
7c7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c7d0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75  */./*.** 2008 Ju
7c7e0 6e 65 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ne 13.**.** The 
7c7f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
7c800 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
7c810 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
7c820 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
7c830 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
7c840 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
7c850 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
7c860 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
7c870 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
7c880 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
7c890 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
7c8a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
7c8b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
7c8c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
7c8d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
7c8e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
7c8f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
7c900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c940 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
7c950 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 64 65  file contains de
7c960 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 67 6c 6f  finitions of glo
7c970 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 61 6e  bal variables an
7c980 64 20 63 6f 6e 74 61 6e 74 73 2e 0a 2a 2a 0a 2a  d contants..**.*
7c990 2a 20 24 49 64 3a 20 67 6c 6f 62 61 6c 2e 63 2c  * $Id: global.c,
7c9a0 76 20 31 2e 39 20 32 30 30 38 2f 31 32 2f 30 38  v 1.9 2008/12/08
7c9b0 20 31 38 3a 31 39 3a 31 38 20 64 72 68 20 45 78   18:19:18 drh Ex
7c9c0 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6e 20 61  p $.*/.../* An a
7c9d0 72 72 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20  rray to map all 
7c9e0 75 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61  upper-case chara
7c9f0 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72  cters into their
7ca00 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a   corresponding.*
7ca10 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20 63 68 61  * lower-case cha
7ca20 72 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53  racter. .**.** S
7ca30 51 4c 69 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69  QLite only consi
7ca40 64 65 72 73 20 55 53 2d 41 53 43 49 49 20 28 6f  ders US-ASCII (o
7ca50 72 20 45 42 43 44 49 43 29 20 63 68 61 72 61 63  r EBCDIC) charac
7ca60 74 65 72 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74  ters.  We do not
7ca70 0a 2a 2a 20 68 61 6e 64 6c 65 20 63 61 73 65 20  .** handle case 
7ca80 63 6f 6e 76 65 72 73 69 6f 6e 73 20 66 6f 72 20  conversions for 
7ca90 74 68 65 20 55 54 46 20 63 68 61 72 61 63 74 65  the UTF characte
7caa0 72 20 73 65 74 20 73 69 6e 63 65 20 74 68 65 20  r set since the 
7cab0 74 61 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76  tables.** involv
7cac0 65 64 20 61 72 65 20 6e 65 61 72 6c 79 20 61 73  ed are nearly as
7cad0 20 62 69 67 20 6f 72 20 62 69 67 67 65 72 20 74   big or bigger t
7cae0 68 61 6e 20 53 51 4c 69 74 65 20 69 74 73 65 6c  han SQLite itsel
7caf0 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  f..*/.SQLITE_PRI
7cb00 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67  VATE const unsig
7cb10 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33  ned char sqlite3
7cb20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d  UpperToLower[] =
7cb30 20 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   {.#ifdef SQLITE
7cb40 5f 41 53 43 49 49 0a 20 20 20 20 20 20 30 2c 20  _ASCII.      0, 
7cb50 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20   1,  2,  3,  4, 
7cb60 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20   5,  6,  7,  8, 
7cb70 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20   9, 10, 11, 12, 
7cb80 31 33 2c 20 31 34 2c 20 31 35 2c 20 31 36 2c 20  13, 14, 15, 16, 
7cb90 31 37 2c 0a 20 20 20 20 20 31 38 2c 20 31 39 2c  17,.     18, 19,
7cba0 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c   20, 21, 22, 23,
7cbb0 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c   24, 25, 26, 27,
7cbc0 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c   28, 29, 30, 31,
7cbd0 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c   32, 33, 34, 35,
7cbe0 0a 20 20 20 20 20 33 36 2c 20 33 37 2c 20 33 38  .     36, 37, 38
7cbf0 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32  , 39, 40, 41, 42
7cc00 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36  , 43, 44, 45, 46
7cc10 2c 20 34 37 2c 20 34 38 2c 20 34 39 2c 20 35 30  , 47, 48, 49, 50
7cc20 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20  , 51, 52, 53,.  
7cc30 20 20 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35     54, 55, 56, 5
7cc40 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36  7, 58, 59, 60, 6
7cc50 31 2c 20 36 32 2c 20 36 33 2c 20 36 34 2c 20 39  1, 62, 63, 64, 9
7cc60 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
7cc70 31 2c 31 30 32 2c 31 30 33 2c 0a 20 20 20 20 31  1,102,103,.    1
7cc80 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 31  04,105,106,107,1
7cc90 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31  08,109,110,111,1
7cca0 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31  12,113,114,115,1
7ccb0 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31  16,117,118,119,1
7ccc0 32 30 2c 31 32 31 2c 0a 20 20 20 20 31 32 32 2c  20,121,.    122,
7ccd0 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c   91, 92, 93, 94,
7cce0 20 39 35 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c   95, 96, 97, 98,
7ccf0 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c   99,100,101,102,
7cd00 31 30 33 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c  103,104,105,106,
7cd10 31 30 37 2c 0a 20 20 20 20 31 30 38 2c 31 30 39  107,.    108,109
7cd20 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33  ,110,111,112,113
7cd30 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37  ,114,115,116,117
7cd40 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31  ,118,119,120,121
7cd50 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35  ,122,123,124,125
7cd60 2c 0a 20 20 20 20 31 32 36 2c 31 32 37 2c 31 32  ,.    126,127,12
7cd70 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33  8,129,130,131,13
7cd80 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33  2,133,134,135,13
7cd90 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34  6,137,138,139,14
7cda0 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20  0,141,142,143,. 
7cdb0 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31     144,145,146,1
7cdc0 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31  47,148,149,150,1
7cdd0 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31  51,152,153,154,1
7cde0 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 38 2c 31  55,156,157,158,1
7cdf0 35 39 2c 31 36 30 2c 31 36 31 2c 0a 20 20 20 20  59,160,161,.    
7ce00 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c  162,163,164,165,
7ce10 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c  166,167,168,169,
7ce20 31 37 30 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c  170,171,172,173,
7ce30 31 37 34 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c  174,175,176,177,
7ce40 31 37 38 2c 31 37 39 2c 0a 20 20 20 20 31 38 30  178,179,.    180
7ce50 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34  ,181,182,183,184
7ce60 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38  ,185,186,187,188
7ce70 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 31 39 32  ,189,190,191,192
7ce80 2c 31 39 33 2c 31 39 34 2c 31 39 35 2c 31 39 36  ,193,194,195,196
7ce90 2c 31 39 37 2c 0a 20 20 20 20 31 39 38 2c 31 39  ,197,.    198,19
7cea0 39 2c 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30  9,200,201,202,20
7ceb0 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30  3,204,205,206,20
7cec0 37 2c 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31  7,208,209,210,21
7ced0 31 2c 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31  1,212,213,214,21
7cee0 35 2c 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32  5,.    216,217,2
7cef0 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32  18,219,220,221,2
7cf00 32 32 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32  22,223,224,225,2
7cf10 32 36 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32  26,227,228,229,2
7cf20 33 30 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a  30,231,232,233,.
7cf30 20 20 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c      234,235,236,
7cf40 32 33 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c  237,238,239,240,
7cf50 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c  241,242,243,244,
7cf60 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c  245,246,247,248,
7cf70 32 34 39 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20  249,250,251,.   
7cf80 20 32 35 32 2c 32 35 33 2c 32 35 34 2c 32 35 35   252,253,254,255
7cf90 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
7cfa0 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 20  QLITE_EBCDIC.   
7cfb0 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33     0,  1,  2,  3
7cfc0 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37  ,  4,  5,  6,  7
7cfd0 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31  ,  8,  9, 10, 11
7cfe0 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35  , 12, 13, 14, 15
7cff0 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20  , /* 0x */.     
7d000 31 36 2c 20 31 37 2c 20 31 38 2c 20 31 39 2c 20  16, 17, 18, 19, 
7d010 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20  20, 21, 22, 23, 
7d020 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20  24, 25, 26, 27, 
7d030 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20  28, 29, 30, 31, 
7d040 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 20 20 33 32  /* 1x */.     32
7d050 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 20 33 36  , 33, 34, 35, 36
7d060 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30  , 37, 38, 39, 40
7d070 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34  , 41, 42, 43, 44
7d080 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a  , 45, 46, 47, /*
7d090 20 32 78 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20   2x */.     48, 
7d0a0 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20  49, 50, 51, 52, 
7d0b0 35 33 2c 20 35 34 2c 20 35 35 2c 20 35 36 2c 20  53, 54, 55, 56, 
7d0c0 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20  57, 58, 59, 60, 
7d0d0 36 31 2c 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33  61, 62, 63, /* 3
7d0e0 78 20 2a 2f 0a 20 20 20 20 20 36 34 2c 20 36 35  x */.     64, 65
7d0f0 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39  , 66, 67, 68, 69
7d100 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33  , 70, 71, 72, 73
7d110 2c 20 37 34 2c 20 37 35 2c 20 37 36 2c 20 37 37  , 74, 75, 76, 77
7d120 2c 20 37 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20  , 78, 79, /* 4x 
7d130 2a 2f 0a 20 20 20 20 20 38 30 2c 20 38 31 2c 20  */.     80, 81, 
7d140 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20  82, 83, 84, 85, 
7d150 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 20  86, 87, 88, 89, 
7d160 39 30 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20  90, 91, 92, 93, 
7d170 39 34 2c 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f  94, 95, /* 5x */
7d180 0a 20 20 20 20 20 39 36 2c 20 39 37 2c 20 36 36  .     96, 97, 66
7d190 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30  , 67, 68, 69, 70
7d1a0 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 31 30 36  , 71, 72, 73,106
7d1b0 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30  ,107,108,109,110
7d1c0 2c 31 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20  ,111, /* 6x */. 
7d1d0 20 20 20 31 31 32 2c 20 38 31 2c 20 38 32 2c 20     112, 81, 82, 
7d1e0 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20  83, 84, 85, 86, 
7d1f0 38 37 2c 20 38 38 2c 20 38 39 2c 31 32 32 2c 31  87, 88, 89,122,1
7d200 32 33 2c 31 32 34 2c 31 32 35 2c 31 32 36 2c 31  23,124,125,126,1
7d210 32 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20  27, /* 7x */.   
7d220 20 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31   128,129,130,131
7d230 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35  ,132,133,134,135
7d240 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39  ,136,137,138,139
7d250 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33  ,140,141,142,143
7d260 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31  , /* 8x */.    1
7d270 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31  44,145,146,147,1
7d280 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31  48,149,150,151,1
7d290 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31  52,153,154,155,1
7d2a0 35 36 2c 31 35 37 2c 31 35 36 2c 31 35 39 2c 20  56,157,156,159, 
7d2b0 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 36 30  /* 9x */.    160
7d2c0 2c 31 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34  ,161,162,163,164
7d2d0 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38  ,165,166,167,168
7d2e0 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30  ,169,170,171,140
7d2f0 2c 31 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a  ,141,142,175, /*
7d300 20 41 78 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31   Ax */.    176,1
7d310 37 37 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31  77,178,179,180,1
7d320 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31  81,182,183,184,1
7d330 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31  85,186,187,188,1
7d340 38 39 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42  89,190,191, /* B
7d350 78 20 2a 2f 0a 20 20 20 20 31 39 32 2c 31 32 39  x */.    192,129
7d360 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33  ,130,131,132,133
7d370 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37  ,134,135,136,137
7d380 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35  ,202,203,204,205
7d390 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20  ,206,207, /* Cx 
7d3a0 2a 2f 0a 20 20 20 20 32 30 38 2c 31 34 35 2c 31  */.    208,145,1
7d3b0 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31  46,147,148,149,1
7d3c0 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 32  50,151,152,153,2
7d3d0 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32  18,219,220,221,2
7d3e0 32 32 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f  22,223, /* Dx */
7d3f0 0a 20 20 20 20 32 32 34 2c 32 32 35 2c 31 36 32  .    224,225,162
7d400 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36  ,163,164,165,166
7d410 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 32 33 32  ,167,168,169,232
7d420 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36  ,203,204,205,206
7d430 2c 32 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20  ,207, /* Ex */. 
7d440 20 20 20 32 33 39 2c 32 34 30 2c 32 34 31 2c 32     239,240,241,2
7d450 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32  42,243,244,245,2
7d460 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32  46,247,248,249,2
7d470 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32  19,220,221,222,2
7d480 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e  55, /* Fx */.#en
7d490 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  dif.};../*.** Th
7d4a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67  e following sing
7d4b0 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74  leton contains t
7d4c0 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67  he global config
7d4d0 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74  uration for.** t
7d4e0 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
7d4f0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
7d500 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20  VATE SQLITE_WSD 
7d510 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f  struct Sqlite3Co
7d520 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66  nfig sqlite3Conf
7d530 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45  ig = {.   SQLITE
7d540 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54  _DEFAULT_MEMSTAT
7d550 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74  US,  /* bMemstat
7d560 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 20   */.   1,       
7d570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d580 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 20    /* bCoreMutex 
7d590 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 52  */.   SQLITE_THR
7d5a0 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 20  EADSAFE==1,     
7d5b0 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a   /* bFullMutex *
7d5c0 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 65 2c  /.   0x7ffffffe,
7d5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d5e0 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20  /* mxStrlen */. 
7d5f0 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 20 20    100,          
7d600 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7d610 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20  szLookaside */. 
7d620 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 20 20    500,          
7d630 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7d640 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20  nLookaside */.  
7d650 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c   {0,0,0,0,0,0,0,
7d660 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d  0},         /* m
7d670 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c   */.   {0,0,0,0,
7d680 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20  0,0,0,0,0},     
7d690 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 20    /* mutex */.  
7d6a0 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c   {0,0,0,0,0,0,0,
7d6b0 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20 70  0,0,0,0},   /* p
7d6c0 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 6f 69  cache */.   (voi
7d6d0 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  d*)0,           
7d6e0 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70 20         /* pHeap 
7d6f0 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20  */.   0,        
7d700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d710 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20 20   /* nHeap */.   
7d720 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  0, 0,           
7d730 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 6e             /* mn
7d740 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f 0a  Heap, mxHeap */.
7d750 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20     (void*)0,    
7d760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7d770 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20   pScratch */.   
7d780 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
7d790 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a             /* sz
7d7a0 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c  Scratch */.   0,
7d7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d7c0 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63 72           /* nScr
7d7d0 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69 64  atch */.   (void
7d7e0 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  *)0,            
7d7f0 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 2a        /* pPage *
7d800 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  /.   0,         
7d810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d820 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20 20  /* szPage */.   
7d830 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
7d840 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 50             /* nP
7d850 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  age */.   0,    
7d860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d870 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65 72       /* mxParser
7d880 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 20  Stack */.   0,  
7d890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d8a0 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 64         /* shared
7d8b0 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f 0a  CacheEnabled */.
7d8c0 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65     /* All the re
7d8d0 73 74 20 6e 65 65 64 20 74 6f 20 61 6c 77 61 79  st need to alway
7d8e0 73 20 62 65 20 7a 65 72 6f 20 2a 2f 0a 20 20 20  s be zero */.   
7d8f0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
7d900 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73             /* is
7d910 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Init */.   0,   
7d920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d930 20 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72        /* inProgr
7d940 65 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  ess */.   0,    
7d950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d960 20 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63       /* isMalloc
7d970 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Init */.   0,   
7d980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d990 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75        /* pInitMu
7d9a0 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  tex */.   0,    
7d9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d9c0 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74       /* nRefInit
7d9d0 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a  Mutex */.};.../*
7d9e0 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66  .** Hash table f
7d9f0 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69  or global functi
7da00 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20  ons - functions 
7da10 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a  common to all.**
7da20 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
7da30 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e  tions.  After in
7da40 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68  itialization, th
7da50 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72  is table is.** r
7da60 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c  ead-only..*/.SQL
7da70 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49  ITE_PRIVATE SQLI
7da80 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61  TE_WSD FuncDefHa
7da90 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  sh sqlite3Global
7daa0 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 2a 2a  Functions;../***
7dab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
7dac0 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a  of global.c ****
7dad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7dae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7daf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
7db00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
7db10 6e 20 66 69 6c 65 20 73 74 61 74 75 73 2e 63 20  n file status.c 
7db20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7db30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7db40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
7db50 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 38 0a 2a  * 2008 June 18.*
7db60 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
7db70 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
7db80 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
7db90 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
7dba0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
7dbb0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
7dbc0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
7dbd0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
7dbe0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
7dbf0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
7dc00 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
7dc10 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
7dc20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
7dc30 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
7dc40 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
7dc50 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
7dc60 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
7dc70 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
7dc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7dc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7dca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7dcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
7dcc0 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20  .** This module 
7dcd0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73  implements the s
7dce0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 20  qlite3_status() 
7dcf0 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 72 65  interface and re
7dd00 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f  lated.** functio
7dd10 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49  nality..**.** $I
7dd20 64 3a 20 73 74 61 74 75 73 2e 63 2c 76 20 31 2e  d: status.c,v 1.
7dd30 39 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a  9 2008/09/02 00:
7dd40 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a  52:52 drh Exp $.
7dd50 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62  */../*.** Variab
7dd60 6c 65 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20  les in which to 
7dd70 72 65 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e  record status in
7dd80 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79  formation..*/.ty
7dd90 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
7dda0 69 74 65 33 53 74 61 74 54 79 70 65 20 73 71 6c  ite3StatType sql
7ddb0 69 74 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74  ite3StatType;.st
7ddc0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
7ddd0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74  struct sqlite3St
7dde0 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e  atType {.  int n
7ddf0 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20  owValue[9];     
7de00 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76      /* Current v
7de10 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  alue */.  int mx
7de20 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20  Value[9];       
7de30 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61     /* Maximum va
7de40 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33  lue */.} sqlite3
7de50 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b  Stat = { {0,}, {
7de60 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20  0,} };.../* The 
7de70 22 77 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20  "wsdStat" macro 
7de80 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20  will resolve to 
7de90 74 68 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72  the status infor
7dea0 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20  mation.** state 
7deb0 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74  vector.  If writ
7dec0 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
7ded0 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20   is unsupported 
7dee0 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a  on the target,.*
7def0 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63  * we have to loc
7df00 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65  ate the state ve
7df10 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65  ctor at run-time
7df20 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63  .  In the more c
7df30 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68  ommon.** case wh
7df40 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61  ere writable sta
7df50 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70  tic data is supp
7df60 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63  orted, wsdStat c
7df70 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c  an refer directl
7df80 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c  y.** to the "sql
7df90 69 74 65 33 53 74 61 74 22 20 73 74 61 74 65 20  ite3Stat" state 
7dfa0 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20  vector declared 
7dfb0 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  above..*/.#ifdef
7dfc0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
7dfd0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61  .# define wsdSta
7dfe0 74 49 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74  tInit  sqlite3St
7dff0 61 74 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f  atType *x = &GLO
7e000 42 41 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54  BAL(sqlite3StatT
7e010 79 70 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29  ype,sqlite3Stat)
7e020 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61  .# define wsdSta
7e030 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64  t x[0].#else.# d
7e040 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69  efine wsdStatIni
7e050 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74  t.# define wsdSt
7e060 61 74 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23  at sqlite3Stat.#
7e070 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
7e080 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20  urn the current 
7e090 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75  value of a statu
7e0a0 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a  s parameter..*/.
7e0b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
7e0c0 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73  nt sqlite3Status
7e0d0 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20  Value(int op){. 
7e0e0 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20   wsdStatInit;.  
7e0f0 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26  assert( op>=0 &&
7e100 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73   op<ArraySize(ws
7e110 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20  dStat.nowValue) 
7e120 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53  );.  return wsdS
7e130 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
7e140 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e  ;.}../*.** Add N
7e150 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
7e160 20 61 20 73 74 61 74 75 73 20 72 65 63 6f 72 64   a status record
7e170 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  .  It is assumed
7e180 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c   that the.** cal
7e190 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70  ler holds approp
7e1a0 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a  riate locks..*/.
7e1b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
7e1c0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
7e1d0 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74  sAdd(int op, int
7e1e0 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e   N){.  wsdStatIn
7e1f0 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70  it;.  assert( op
7e200 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53  >=0 && op<ArrayS
7e210 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56  ize(wsdStat.nowV
7e220 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74  alue) );.  wsdSt
7e230 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20  at.nowValue[op] 
7e240 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53  += N;.  if( wsdS
7e250 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
7e260 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65  >wsdStat.mxValue
7e270 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53  [op] ){.    wsdS
7e280 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20  tat.mxValue[op] 
7e290 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  = wsdStat.nowVal
7e2a0 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ue[op];.  }.}../
7e2b0 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c  *.** Set the val
7e2c0 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 74  ue of a status t
7e2d0 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  o X..*/.SQLITE_P
7e2e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
7e2f0 74 65 33 53 74 61 74 75 73 53 65 74 28 69 6e 74  te3StatusSet(int
7e300 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77   op, int X){.  w
7e310 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73  sdStatInit;.  as
7e320 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f  sert( op>=0 && o
7e330 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53  p<ArraySize(wsdS
7e340 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b  tat.nowValue) );
7e350 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61  .  wsdStat.nowVa
7e360 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69  lue[op] = X;.  i
7e370 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61  f( wsdStat.nowVa
7e380 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e  lue[op]>wsdStat.
7e390 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20  mxValue[op] ){. 
7e3a0 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c     wsdStat.mxVal
7e3b0 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74  ue[op] = wsdStat
7e3c0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20  .nowValue[op];. 
7e3d0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72   }.}../*.** Quer
7e3e0 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61  y status informa
7e3f0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tion..**.** This
7e400 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
7e410 61 73 73 75 6d 65 73 20 74 68 61 74 20 72 65 61  assumes that rea
7e420 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20  ding or writing 
7e430 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32  an aligned.** 32
7e440 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20  -bit integer is 
7e450 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74  an atomic operat
7e460 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73  ion.  If that as
7e470 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20  sumption is not 
7e480 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  true,.** then th
7e490 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f  is routine is no
7e4a0 74 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f  t threadsafe..*/
7e4b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
7e4c0 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69  sqlite3_status(i
7e4d0 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72  nt op, int *pCur
7e4e0 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68  rent, int *pHigh
7e4f0 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74  water, int reset
7e500 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74  Flag){.  wsdStat
7e510 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30  Init;.  if( op<0
7e520 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a   || op>=ArraySiz
7e530 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  e(wsdStat.nowVal
7e540 75 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ue) ){.    retur
7e550 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
7e560 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74  .  }.  *pCurrent
7e570 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61   = wsdStat.nowVa
7e580 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67  lue[op];.  *pHig
7e590 68 77 61 74 65 72 20 3d 20 77 73 64 53 74 61 74  hwater = wsdStat
7e5a0 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20  .mxValue[op];.  
7e5b0 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b  if( resetFlag ){
7e5c0 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56  .    wsdStat.mxV
7e5d0 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74  alue[op] = wsdSt
7e5e0 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b  at.nowValue[op];
7e5f0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
7e600 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
7e610 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69  * Query status i
7e620 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61  nformation for a
7e630 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
7e640 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53   connection.*/.S
7e650 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
7e660 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
7e670 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
7e680 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
7e690 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
7e6a0 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73  ion whose status
7e6b0 20 69 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20   is desired */. 
7e6c0 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
7e6d0 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73         /* Status
7e6e0 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a   verb */.  int *
7e6f0 70 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20  pCurrent,       
7e700 20 2f 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e   /* Write curren
7e710 74 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a  t value here */.
7e720 20 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65    int *pHighwate
7e730 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  r,      /* Write
7e740 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b   high-water mark
7e750 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72   here */.  int r
7e760 65 73 65 74 46 6c 61 67 20 20 20 20 20 20 20 20  esetFlag        
7e770 20 2f 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77   /* Reset high-w
7e780 61 74 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75  ater mark if tru
7e790 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68  e */.){.  switch
7e7a0 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
7e7b0 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53   SQLITE_DBSTATUS
7e7c0 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a  _LOOKASIDE_USED:
7e7d0 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65   {.      *pCurre
7e7e0 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  nt = db->lookasi
7e7f0 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a  de.nOut;.      *
7e800 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d  pHighwater = db-
7e810 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74  >lookaside.mxOut
7e820 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65  ;.      if( rese
7e830 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20  tFlag ){.       
7e840 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d   db->lookaside.m
7e850 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  xOut = db->looka
7e860 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20  side.nOut;.     
7e870 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
7e880 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
7e890 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  t: {.      retur
7e8a0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
7e8b0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
7e8c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
7e8d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
7e8e0 20 45 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63   End of status.c
7e8f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
7e900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
7e920 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
7e930 20 42 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65   Begin file date
7e940 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
7e950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
7e970 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f  ./*.** 2003 Octo
7e980 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 31.**.** The
7e990 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
7e9a0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
7e9b0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
7e9c0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
7e9d0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
7e9e0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
7e9f0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
7ea00 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
7ea10 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
7ea20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
7ea30 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
7ea40 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
7ea50 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
7ea60 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
7ea70 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
7ea80 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
7ea90 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
7eaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7eab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7eac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ead0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7eae0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
7eaf0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
7eb00 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
7eb10 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20   implement date 
7eb20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63  and time.** func
7eb30 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65  tions for SQLite
7eb40 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  .  .**.** There 
7eb50 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f  is only one expo
7eb60 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74  rted symbol in t
7eb70 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66  his file - the f
7eb80 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74  unction.** sqlit
7eb90 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69  e3RegisterDateTi
7eba0 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f  meFunctions() fo
7ebb0 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f  und at the botto
7ebc0 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a  m of the file..*
7ebd0 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65  * All other code
7ebe0 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e   has file scope.
7ebf0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 61 74 65  .**.** $Id: date
7ec00 2e 63 2c 76 20 31 2e 39 39 20 32 30 30 38 2f 31  .c,v 1.99 2008/1
7ec10 32 2f 32 30 20 31 33 3a 31 38 3a 35 30 20 64 72  2/20 13:18:50 dr
7ec20 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 53 51  h Exp $.**.** SQ
7ec30 4c 69 74 65 20 70 72 6f 63 65 73 73 65 73 20 61  Lite processes a
7ec40 6c 6c 20 74 69 6d 65 73 20 61 6e 64 20 64 61 74  ll times and dat
7ec50 65 73 20 61 73 20 4a 75 6c 69 61 6e 20 44 61 79  es as Julian Day
7ec60 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 0a 2a   numbers.  The.*
7ec70 2a 20 64 61 74 65 73 20 61 6e 64 20 74 69 6d 65  * dates and time
7ec80 73 20 61 72 65 20 73 74 6f 72 65 64 20 61 73 20  s are stored as 
7ec90 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61  the number of da
7eca0 79 73 20 73 69 6e 63 65 20 6e 6f 6f 6e 0a 2a 2a  ys since noon.**
7ecb0 20 69 6e 20 47 72 65 65 6e 77 69 63 68 20 6f 6e   in Greenwich on
7ecc0 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20 34 37   November 24, 47
7ecd0 31 34 20 42 2e 43 2e 20 61 63 63 6f 72 64 69 6e  14 B.C. accordin
7ece0 67 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69  g to the Gregori
7ecf0 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61 72 20 73  an.** calendar s
7ed00 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 31 39  ystem. .**.** 19
7ed10 37 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30  70-01-01 00:00:0
7ed20 30 20 69 73 20 4a 44 20 32 34 34 30 35 38 37 2e  0 is JD 2440587.
7ed30 35 0a 2a 2a 20 32 30 30 30 2d 30 31 2d 30 31 20  5.** 2000-01-01 
7ed40 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32  00:00:00 is JD 2
7ed50 34 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a 2a 20 54  451544.5.**.** T
7ed60 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e  his implemention
7ed70 20 72 65 71 75 69 72 65 73 20 79 65 61 72 73 20   requires years 
7ed80 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20  to be expressed 
7ed90 61 73 20 61 20 34 2d 64 69 67 69 74 20 6e 75 6d  as a 4-digit num
7eda0 62 65 72 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61  ber.** which mea
7edb0 6e 73 20 74 68 61 74 20 6f 6e 6c 79 20 64 61 74  ns that only dat
7edc0 65 73 20 62 65 74 77 65 65 6e 20 30 30 30 30 2d  es between 0000-
7edd0 30 31 2d 30 31 20 61 6e 64 20 39 39 39 39 2d 31  01-01 and 9999-1
7ede0 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 62 65 20 72  2-31 can.** be r
7edf0 65 70 72 65 73 65 6e 74 65 64 2c 20 65 76 65 6e  epresented, even
7ee00 20 74 68 6f 75 67 68 20 6a 75 6c 69 61 6e 20 64   though julian d
7ee10 61 79 20 6e 75 6d 62 65 72 73 20 61 6c 6c 6f 77  ay numbers allow
7ee20 20 61 20 6d 75 63 68 20 77 69 64 65 72 0a 2a 2a   a much wider.**
7ee30 20 72 61 6e 67 65 20 6f 66 20 64 61 74 65 73 2e   range of dates.
7ee40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 72 65 67 6f  .**.** The Grego
7ee50 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 73 79  rian calendar sy
7ee60 73 74 65 6d 20 69 73 20 75 73 65 64 20 66 6f 72  stem is used for
7ee70 20 61 6c 6c 20 64 61 74 65 73 20 61 6e 64 20 74   all dates and t
7ee80 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68  imes,.** even th
7ee90 6f 73 65 20 74 68 61 74 20 70 72 65 64 61 74 65  ose that predate
7eea0 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63   the Gregorian c
7eeb0 61 6c 65 6e 64 61 72 2e 20 20 48 69 73 74 6f 72  alendar.  Histor
7eec0 69 61 6e 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20  ians usually.** 
7eed0 75 73 65 20 74 68 65 20 4a 75 6c 69 61 6e 20 63  use the Julian c
7eee0 61 6c 65 6e 64 61 72 20 66 6f 72 20 64 61 74 65  alendar for date
7eef0 73 20 70 72 69 6f 72 20 74 6f 20 31 35 38 32 2d  s prior to 1582-
7ef00 31 30 2d 31 35 20 61 6e 64 20 66 6f 72 20 73 6f  10-15 and for so
7ef10 6d 65 0a 2a 2a 20 64 61 74 65 73 20 61 66 74 65  me.** dates afte
7ef20 72 77 61 72 64 73 2c 20 64 65 70 65 6e 64 69 6e  rwards, dependin
7ef30 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20 20 42 65  g on locale.  Be
7ef40 77 61 72 65 20 6f 66 20 74 68 69 73 20 64 69 66  ware of this dif
7ef50 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54  ference..**.** T
7ef60 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 6c  he conversion al
7ef70 67 6f 72 69 74 68 6d 73 20 61 72 65 20 69 6d 70  gorithms are imp
7ef80 6c 65 6d 65 6e 74 65 64 20 62 61 73 65 64 20 6f  lemented based o
7ef90 6e 20 64 65 73 63 72 69 70 74 69 6f 6e 73 0a 2a  n descriptions.*
7efa0 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * in the followi
7efb0 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a 2a 20 20  ng text:.**.**  
7efc0 20 20 20 20 4a 65 61 6e 20 4d 65 65 75 73 0a 2a      Jean Meeus.*
7efd0 2a 20 20 20 20 20 20 41 73 74 72 6f 6e 6f 6d 69  *      Astronomi
7efe0 63 61 6c 20 41 6c 67 6f 72 69 74 68 6d 73 2c 20  cal Algorithms, 
7eff0 32 6e 64 20 45 64 69 74 69 6f 6e 2c 20 31 39 39  2nd Edition, 199
7f000 38 0a 2a 2a 20 20 20 20 20 20 49 53 42 4d 20 30  8.**      ISBM 0
7f010 2d 39 34 33 33 39 36 2d 36 31 2d 31 0a 2a 2a 20  -943396-61-1.** 
7f020 20 20 20 20 20 57 69 6c 6c 6d 61 6e 6e 2d 42 65       Willmann-Be
7f030 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20 20 20 20  ll, Inc.**      
7f040 52 69 63 68 6d 6f 6e 64 2c 20 56 69 72 67 69 6e  Richmond, Virgin
7f050 69 61 20 28 55 53 41 29 0a 2a 2f 0a 23 69 6e 63  ia (USA).*/.#inc
7f060 6c 75 64 65 20 3c 63 74 79 70 65 2e 68 3e 0a 23  lude <ctype.h>.#
7f070 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e  include <time.h>
7f080 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
7f090 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46  _OMIT_DATETIME_F
7f0a0 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72  UNCS../*.** On r
7f0b0 65 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c  ecent Windows pl
7f0c0 61 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63  atforms, the loc
7f0d0 61 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74  altime_s() funct
7f0e0 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65  ion is available
7f0f0 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74  .** as part of t
7f100 68 65 20 22 53 65 63 75 72 65 20 43 52 54 22 2e  he "Secure CRT".
7f110 20 49 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c   It is essential
7f120 6c 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  ly equivalent to
7f130 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72   .** localtime_r
7f140 28 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64  () available und
7f150 65 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c  er most POSIX pl
7f160 61 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20  atforms, except 
7f170 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64  that the .** ord
7f180 65 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65  er of the parame
7f190 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64  ters is reversed
7f1a0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70  ..**.** See http
7f1b0 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66  ://msdn.microsof
7f1c0 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72  t.com/en-us/libr
7f1d0 61 72 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e  ary/a442x3ye(VS.
7f1e0 38 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20  80).aspx..**.** 
7f1f0 49 66 20 74 68 65 20 75 73 65 72 20 68 61 73 20  If the user has 
7f200 6e 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f  not indicated to
7f210 20 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72   use localtime_r
7f220 28 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f  () or localtime_
7f230 73 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20  s().** already, 
7f240 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56  check for an MSV
7f250 43 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d  C build environm
7f260 65 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64 65  ent that provide
7f270 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f  s .** localtime_
7f280 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  s()..*/.#if !def
7f290 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54  ined(HAVE_LOCALT
7f2a0 49 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e  IME_R) && !defin
7f2b0 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ed(HAVE_LOCALTIM
7f2c0 45 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64  E_S) && \.     d
7f2d0 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
7f2e0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54   && defined(_CRT
7f2f0 5f 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43  _INSECURE_DEPREC
7f300 41 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56  ATE).#define HAV
7f310 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a  E_LOCALTIME_S 1.
7f320 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20  #endif../*.** A 
7f330 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 68 6f  structure for ho
7f340 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 64  lding a single d
7f350 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f  ate and time..*/
7f360 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
7f370 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 69 6d  DateTime DateTim
7f380 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54 69  e;.struct DateTi
7f390 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  me {.  sqlite3_i
7f3a0 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 65  nt64 iJD; /* The
7f3b0 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62   julian day numb
7f3c0 65 72 20 74 69 6d 65 73 20 38 36 34 30 30 30 30  er times 8640000
7f3d0 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 2c  0 */.  int Y, M,
7f3e0 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 61   D;       /* Yea
7f3f0 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61  r, month, and da
7f400 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b  y */.  int h, m;
7f410 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 75            /* Hou
7f420 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a 2f  r and minutes */
7f430 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 20  .  int tz;      
7f440 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e        /* Timezon
7f450 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e 75  e offset in minu
7f460 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20  tes */.  double 
7f470 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  s;          /* S
7f480 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 72  econds */.  char
7f490 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 2f   validYMD;     /
7f4a0 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 59 2c  * True (1) if Y,
7f4b0 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a 2f  M,D are valid */
7f4c0 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d 53  .  char validHMS
7f4d0 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31  ;     /* True (1
7f4e0 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 76  ) if h,m,s are v
7f4f0 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76  alid */.  char v
7f500 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a 20  alidJD;      /* 
7f510 54 72 75 65 20 28 31 29 20 69 66 20 69 4a 44 20  True (1) if iJD 
7f520 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68  is valid */.  ch
7f530 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20 20 20  ar validTZ;     
7f540 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20   /* True (1) if 
7f550 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 7d  tz is valid */.}
7f560 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  ;.../*.** Conver
7f570 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e 65  t zDate into one
7f580 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 72   or more integer
7f590 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 61  s.  Additional a
7f5a0 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d 65  rguments.** come
7f5b0 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 20   in groups of 5 
7f5c0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
7f5d0 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 20 20  *       N       
7f5e0 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73  number of digits
7f5f0 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72 0a   in the integer.
7f600 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20 20  **       min    
7f610 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 64   minimum allowed
7f620 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e   value of the in
7f630 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d  teger.**       m
7f640 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 61  ax     maximum a
7f650 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20  llowed value of 
7f660 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  the integer.**  
7f670 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 69 72       nextC   fir
7f680 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74  st character aft
7f690 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  er the integer.*
7f6a0 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 20 20  *       pVal    
7f6b0 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74  where to write t
7f6c0 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75  he integers valu
7f6d0 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73  e..**.** Convers
7f6e0 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e  ions continue un
7f6f0 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78  til one with nex
7f700 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74  tC==0 is encount
7f710 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 6e  ered..** The fun
7f720 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
7f730 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63  e number of succ
7f740 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 6f  essful conversio
7f750 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ns..*/.static in
7f760 74 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e 73  t getDigits(cons
7f770 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 2e  t char *zDate, .
7f780 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
7f790 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 20  p;.  int val;.  
7f7a0 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 6e  int N;.  int min
7f7b0 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 69  ;.  int max;.  i
7f7c0 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 20  nt nextC;.  int 
7f7d0 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e 74  *pVal;.  int cnt
7f7e0 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74   = 0;.  va_start
7f7f0 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 64  (ap, zDate);.  d
7f800 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72  o{.    N = va_ar
7f810 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
7f820 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  min = va_arg(ap,
7f830 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 3d   int);.    max =
7f840 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
7f850 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76 61  ;.    nextC = va
7f860 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
7f870 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 67     pVal = va_arg
7f880 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20  (ap, int*);.    
7f890 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 69  val = 0;.    whi
7f8a0 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20  le( N-- ){.     
7f8b0 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a 28   if( !isdigit(*(
7f8c0 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 0a 20 20  u8*)zDate) ){.  
7f8d0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67        goto end_g
7f8e0 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20 20 20  etDigits;.      
7f8f0 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d 20 76 61  }.      val = va
7f900 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20  l*10 + *zDate - 
7f910 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 61 74 65  '0';.      zDate
7f920 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ++;.    }.    if
7f930 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c  ( val<min || val
7f940 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43 21 3d  >max || (nextC!=
7f950 30 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a 44 61  0 && nextC!=*zDa
7f960 74 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  te) ){.      got
7f970 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b  o end_getDigits;
7f980 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 56 61 6c  .    }.    *pVal
7f990 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44 61 74   = val;.    zDat
7f9a0 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a  e++;.    cnt++;.
7f9b0 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 74 43 20    }while( nextC 
7f9c0 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69 74 73  );.end_getDigits
7f9d0 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  :.  va_end(ap);.
7f9e0 20 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a    return cnt;.}.
7f9f0 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65 78 74  ./*.** Read text
7fa00 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20 63 6f   from z[] and co
7fa10 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 66 6c 6f  nvert into a flo
7fa20 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62  ating point numb
7fa30 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74  er.  Return.** t
7fa40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67  he number of dig
7fa50 69 74 73 20 63 6f 6e 76 65 72 74 65 64 2e 0a 2a  its converted..*
7fa60 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 6c  /.#define getVal
7fa70 75 65 20 73 71 6c 69 74 65 33 41 74 6f 46 0a 0a  ue sqlite3AtoF..
7fa80 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 74 69  /*.** Parse a ti
7fa90 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69 6f 6e  mezone extension
7faa0 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 61   on the end of a
7fab0 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a 20 54   date-time..** T
7fac0 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20  he extension is 
7fad0 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
7fae0 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d 29 48  **        (+/-)H
7faf0 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 20 74 68  H:MM.**.** Or th
7fb00 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74 69 6f  e "zulu" notatio
7fb10 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  n:.**.**        
7fb20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  Z.**.** If the p
7fb30 61 72 73 65 20 69 73 20 73 75 63 63 65 73 73 66  arse is successf
7fb40 75 6c 2c 20 77 72 69 74 65 20 74 68 65 20 6e 75  ul, write the nu
7fb50 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74 65 73 0a  mber of minutes.
7fb60 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20 69 6e 20  ** of change in 
7fb70 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74 75 72 6e  p->tz and return
7fb80 20 30 2e 20 20 49 66 20 61 20 70 61 72 73 65 72   0.  If a parser
7fb90 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a   error occurs,.*
7fba0 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  * return non-zer
7fbb0 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 73 73 69  o..**.** A missi
7fbc0 6e 67 20 73 70 65 63 69 66 69 65 72 20 69 73 20  ng specifier is 
7fbd0 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 20 61  not considered a
7fbe0 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74  n error..*/.stat
7fbf0 69 63 20 69 6e 74 20 70 61 72 73 65 54 69 6d 65  ic int parseTime
7fc00 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68 61 72 20  zone(const char 
7fc10 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65  *zDate, DateTime
7fc20 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 67 6e 20   *p){.  int sgn 
7fc30 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 72 2c 20  = 0;.  int nHr, 
7fc40 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b 0a 20 20  nMn;.  int c;.  
7fc50 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a  while( isspace(*
7fc60 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 20 7a  (u8*)zDate) ){ z
7fc70 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 70 2d 3e 74  Date++; }.  p->t
7fc80 7a 20 3d 20 30 3b 0a 20 20 63 20 3d 20 2a 7a 44  z = 0;.  c = *zD
7fc90 61 74 65 3b 0a 20 20 69 66 28 20 63 3d 3d 27 2d  ate;.  if( c=='-
7fca0 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2d  ' ){.    sgn = -
7fcb0 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63  1;.  }else if( c
7fcc0 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 73 67 6e  =='+' ){.    sgn
7fcd0 20 3d 20 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 69   = +1;.  }else i
7fce0 66 28 20 63 3d 3d 27 5a 27 20 7c 7c 20 63 3d 3d  f( c=='Z' || c==
7fcf0 27 7a 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65  'z' ){.    zDate
7fd00 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 7a 75 6c  ++;.    goto zul
7fd10 75 5f 74 69 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b  u_time;.  }else{
7fd20 0a 20 20 20 20 72 65 74 75 72 6e 20 63 21 3d 30  .    return c!=0
7fd30 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 2b 2b 3b  ;.  }.  zDate++;
7fd40 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73  .  if( getDigits
7fd50 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 31 34  (zDate, 2, 0, 14
7fd60 2c 20 27 3a 27 2c 20 26 6e 48 72 2c 20 32 2c 20  , ':', &nHr, 2, 
7fd70 30 2c 20 35 39 2c 20 30 2c 20 26 6e 4d 6e 29 21  0, 59, 0, &nMn)!
7fd80 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =2 ){.    return
7fd90 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20   1;.  }.  zDate 
7fda0 2b 3d 20 35 3b 0a 20 20 70 2d 3e 74 7a 20 3d 20  += 5;.  p->tz = 
7fdb0 73 67 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72 2a 36  sgn*(nMn + nHr*6
7fdc0 30 29 3b 0a 7a 75 6c 75 5f 74 69 6d 65 3a 0a 20  0);.zulu_time:. 
7fdd0 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28   while( isspace(
7fde0 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 20  *(u8*)zDate) ){ 
7fdf0 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 72 65 74  zDate++; }.  ret
7fe00 75 72 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d  urn *zDate!=0;.}
7fe10 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 69  ../*.** Parse ti
7fe20 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  mes of the form 
7fe30 48 48 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53  HH:MM or HH:MM:S
7fe40 53 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e 46 46  S or HH:MM:SS.FF
7fe50 46 46 2e 0a 2a 2a 20 54 68 65 20 48 48 2c 20 4d  FF..** The HH, M
7fe60 4d 2c 20 61 6e 64 20 53 53 20 6d 75 73 74 20 65  M, and SS must e
7fe70 61 63 68 20 62 65 20 65 78 61 63 74 6c 79 20 32  ach be exactly 2
7fe80 20 64 69 67 69 74 73 2e 20 20 54 68 65 0a 2a 2a   digits.  The.**
7fe90 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f   fractional seco
7fea0 6e 64 73 20 46 46 46 46 20 63 61 6e 20 62 65 20  nds FFFF can be 
7feb0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69  one or more digi
7fec0 74 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ts..**.** Return
7fed0 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61   1 if there is a
7fee0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61   parsing error a
7fef0 6e 64 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e  nd 0 on success.
7ff00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
7ff10 61 72 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73 74  arseHhMmSs(const
7ff20 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61   char *zDate, Da
7ff30 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  teTime *p){.  in
7ff40 74 20 68 2c 20 6d 2c 20 73 3b 0a 20 20 64 6f 75  t h, m, s;.  dou
7ff50 62 6c 65 20 6d 73 20 3d 20 30 2e 30 3b 0a 20 20  ble ms = 0.0;.  
7ff60 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44  if( getDigits(zD
7ff70 61 74 65 2c 20 32 2c 20 30 2c 20 32 34 2c 20 27  ate, 2, 0, 24, '
7ff80 3a 27 2c 20 26 68 2c 20 32 2c 20 30 2c 20 35 39  :', &h, 2, 0, 59
7ff90 2c 20 30 2c 20 26 6d 29 21 3d 32 20 29 7b 0a 20  , 0, &m)!=2 ){. 
7ffa0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
7ffb0 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20  .  zDate += 5;. 
7ffc0 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 3a 27   if( *zDate==':'
7ffd0 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b   ){.    zDate++;
7ffe0 0a 20 20 20 20 69 66 28 20 67 65 74 44 69 67 69  .    if( getDigi
7fff0 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20  ts(zDate, 2, 0, 
80000 35 39 2c 20 30 2c 20 26 73 29 21 3d 31 20 29 7b  59, 0, &s)!=1 ){
80010 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
80020 0a 20 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 65  .    }.    zDate
80030 20 2b 3d 20 32 3b 0a 20 20 20 20 69 66 28 20 2a   += 2;.    if( *
80040 7a 44 61 74 65 3d 3d 27 2e 27 20 26 26 20 69 73  zDate=='.' && is
80050 64 69 67 69 74 28 28 75 38 29 7a 44 61 74 65 5b  digit((u8)zDate[
80060 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75  1]) ){.      dou
80070 62 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30  ble rScale = 1.0
80080 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b  ;.      zDate++;
80090 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73  .      while( is
800a0 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 44 61 74  digit(*(u8*)zDat
800b0 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73  e) ){.        ms
800c0 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44   = ms*10.0 + *zD
800d0 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20  ate - '0';.     
800e0 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e     rScale *= 10.
800f0 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65  0;.        zDate
80100 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
80110 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a    ms /= rScale;.
80120 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
80130 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20     s = 0;.  }.  
80140 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a  p->validJD = 0;.
80150 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20    p->validHMS = 
80160 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20  1;.  p->h = h;. 
80170 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e   p->m = m;.  p->
80180 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66  s = s + ms;.  if
80190 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28  ( parseTimezone(
801a0 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75  zDate, p) ) retu
801b0 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64  rn 1;.  p->valid
801c0 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f  TZ = (p->tz!=0)?
801d0 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  1:0;.  return 0;
801e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
801f0 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44  t from YYYY-MM-D
80200 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75  D HH:MM:SS to ju
80210 6c 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c  lian day.  We al
80220 77 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74  ways assume.** t
80230 68 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d  hat the YYYY-MM-
80240 44 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20  DD is according 
80250 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e  to the Gregorian
80260 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a   calendar..**.**
80270 20 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65   Reference:  Mee
80280 75 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74  us page 61.*/.st
80290 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
802a0 65 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29  eJD(DateTime *p)
802b0 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c  {.  int Y, M, D,
802c0 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a   A, B, X1, X2;..
802d0 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44    if( p->validJD
802e0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
802f0 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a   p->validYMD ){.
80300 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20      Y = p->Y;.  
80310 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20    M = p->M;.    
80320 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73  D = p->D;.  }els
80330 65 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b  e{.    Y = 2000;
80340 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73    /* If no YMD s
80350 70 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65  pecified, assume
80360 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a   2000-Jan-01 */.
80370 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44      M = 1;.    D
80380 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 1;.  }.  if( 
80390 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b  M<=2 ){.    Y--;
803a0 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20  .    M += 12;.  
803b0 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20  }.  A = Y/100;. 
803c0 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f   B = 2 - A + (A/
803d0 34 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35  4);.  X1 = 36525
803e0 2a 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20  *(Y+4716)/100;. 
803f0 20 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b   X2 = 306001*(M+
80400 31 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69  1)/10000;.  p->i
80410 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  JD = (sqlite3_in
80420 74 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20  t64)((X1 + X2 + 
80430 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29  D + B - 1524.5 )
80440 20 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20   * 86400000);.  
80450 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a  p->validJD = 1;.
80460 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d    if( p->validHM
80470 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20  S ){.    p->iJD 
80480 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20  += p->h*3600000 
80490 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28  + p->m*60000 + (
804a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70  sqlite3_int64)(p
804b0 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69  ->s*1000);.    i
804c0 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b  f( p->validTZ ){
804d0 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d  .      p->iJD -=
804e0 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20   p->tz*60000;.  
804f0 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20      p->validYMD 
80500 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  = 0;.      p->va
80510 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20  lidHMS = 0;.    
80520 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30    p->validTZ = 0
80530 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
80540 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73  *.** Parse dates
80550 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a   of the form.**.
80560 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44  **     YYYY-MM-D
80570 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a  D HH:MM:SS.FFF.*
80580 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44  *     YYYY-MM-DD
80590 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20   HH:MM:SS.**    
805a0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
805b0 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d  M.**     YYYY-MM
805c0 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  -DD.**.** Write 
805d0 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20  the result into 
805e0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72  the DateTime str
805f0 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72  ucture and retur
80600 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73  n 0.** on succes
80610 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 69  s and 1 if the i
80620 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e  nput string is n
80630 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  ot a well-formed
80640 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61  .** date..*/.sta
80650 74 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 79  tic int parseYyy
80660 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72  yMmDd(const char
80670 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d   *zDate, DateTim
80680 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20  e *p){.  int Y, 
80690 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66  M, D, neg;..  if
806a0 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20  ( zDate[0]=='-' 
806b0 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a  ){.    zDate++;.
806c0 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d      neg = 1;.  }
806d0 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20  else{.    neg = 
806e0 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74  0;.  }.  if( get
806f0 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30  Digits(zDate,4,0
80700 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31  ,9999,'-',&Y,2,1
80710 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33  ,12,'-',&M,2,1,3
80720 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20  1,0,&D)!=3 ){.  
80730 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
80740 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20    zDate += 10;. 
80750 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28   while( isspace(
80760 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 7c 7c 20  *(u8*)zDate) || 
80770 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65  'T'==*(u8*)zDate
80780 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20   ){ zDate++; }. 
80790 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73   if( parseHhMmSs
807a0 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b  (zDate, p)==0 ){
807b0 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 74  .    /* We got t
807c0 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c  he time */.  }el
807d0 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30  se if( *zDate==0
807e0 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64   ){.    p->valid
807f0 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  HMS = 0;.  }else
80800 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
80810 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44    }.  p->validJD
80820 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64   = 0;.  p->valid
80830 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20  YMD = 1;.  p->Y 
80840 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a  = neg ? -Y : Y;.
80850 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d    p->M = M;.  p-
80860 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d  >D = D;.  if( p-
80870 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20  >validTZ ){.    
80880 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
80890 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
808a0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74  ./*.** Set the t
808b0 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 72 65  ime to the curre
808c0 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 65 64  nt time reported
808d0 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a 73   by the VFS.*/.s
808e0 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 44 61  tatic void setDa
808f0 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28  teTimeToCurrent(
80900 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
80910 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65 54 69  *context, DateTi
80920 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c 65  me *p){.  double
80930 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   r;.  sqlite3 *d
80940 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
80950 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
80960 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65  ntext);.  sqlite
80970 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64  3OsCurrentTime(d
80980 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20 20  b->pVfs, &r);.  
80990 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65  p->iJD = (sqlite
809a0 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30  3_int64)(r*86400
809b0 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20  000.0 + 0.5);.  
809c0 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a  p->validJD = 1;.
809d0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74  }../*.** Attempt
809e0 20 74 6f 20 70 61 72 73 65 20 74 68 65 20 67 69   to parse the gi
809f0 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f 20  ven string into 
80a00 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d  a Julian Day Num
80a10 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  ber.  Return.** 
80a20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  the number of er
80a30 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rors..**.** The 
80a40 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 63  following are ac
80a50 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 20 66  ceptable forms f
80a60 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  or the input str
80a70 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ing:.**.**      
80a80 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d  YYYY-MM-DD HH:MM
80a90 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d  :SS.FFF  +/-HH:M
80aa0 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44 2e 44  M.**      DDDD.D
80ab0 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a  D .**      now.*
80ac0 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69 72 73  *.** In the firs
80ad0 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48  t form, the +/-H
80ae0 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73 20 6f  H:MM is always o
80af0 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72  ptional.  The fr
80b00 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f  actional.** seco
80b10 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20 28 74  nds extension (t
80b20 68 65 20 22 2e 46 46 46 22 29 20 69 73 20 6f 70  he ".FFF") is op
80b30 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65 63  tional.  The sec
80b40 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20  onds portion.** 
80b50 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73 20 6f  (":SS.FFF") is o
80b60 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65 61 72  ption.  The year
80b70 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20 62 65   and date can be
80b80 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e 67   omitted as long
80b90 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73 20  .** as there is 
80ba0 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e 20 20  a time string.  
80bb0 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e 67 20  The time string 
80bc0 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 61  can be omitted a
80bd0 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65  s long.** as the
80be0 72 65 20 69 73 20 61 20 79 65 61 72 20 61 6e 64  re is a year and
80bf0 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   date..*/.static
80c00 20 69 6e 74 20 70 61 72 73 65 44 61 74 65 4f 72   int parseDateOr
80c10 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  Time(.  sqlite3_
80c20 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
80c30 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
80c40 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74 65 54  *zDate, .  DateT
80c50 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 66 28 20  ime *p.){.  if( 
80c60 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 7a 44  parseYyyyMmDd(zD
80c70 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20 20  ate,p)==0 ){.   
80c80 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
80c90 73 65 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d  se if( parseHhMm
80ca0 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20  Ss(zDate, p)==0 
80cb0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
80cc0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
80cd0 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74  ite3StrICmp(zDat
80ce0 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20 20  e,"now")==0){.  
80cf0 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43    setDateTimeToC
80d00 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c 20  urrent(context, 
80d10 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  p);.    return 0
80d20 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
80d30 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 7a 44  lite3IsNumber(zD
80d40 61 74 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55  ate, 0, SQLITE_U
80d50 54 46 38 29 20 29 7b 0a 20 20 20 20 64 6f 75 62  TF8) ){.    doub
80d60 6c 65 20 72 3b 0a 20 20 20 20 67 65 74 56 61 6c  le r;.    getVal
80d70 75 65 28 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20  ue(zDate, &r);. 
80d80 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c     p->iJD = (sql
80d90 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36  ite3_int64)(r*86
80da0 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b  400000.0 + 0.5);
80db0 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20  .    p->validJD 
80dc0 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
80dd0 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
80de0 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  1;.}../*.** Comp
80df0 75 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d 6f  ute the Year, Mo
80e00 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72 6f  nth, and Day fro
80e10 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79  m the julian day
80e20 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
80e30 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59  ic void computeY
80e40 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  MD(DateTime *p){
80e50 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20  .  int Z, A, B, 
80e60 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69  C, D, E, X1;.  i
80e70 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29  f( p->validYMD )
80e80 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21   return;.  if( !
80e90 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20  p->validJD ){.  
80ea0 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20    p->Y = 2000;. 
80eb0 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20     p->M = 1;.   
80ec0 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c   p->D = 1;.  }el
80ed0 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74  se{.    Z = (int
80ee0 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30  )((p->iJD + 4320
80ef0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b  0000)/86400000);
80f00 0a 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 28  .    A = (int)((
80f10 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f  Z - 1867216.25)/
80f20 33 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20 41  36524.25);.    A
80f30 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28   = Z + 1 + A - (
80f40 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20  A/4);.    B = A 
80f50 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20  + 1524;.    C = 
80f60 28 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e 31  (int)((B - 122.1
80f70 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20 44  )/365.25);.    D
80f80 20 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30 30   = (36525*C)/100
80f90 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29 28  ;.    E = (int)(
80fa0 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a  (B-D)/30.6001);.
80fb0 20 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28 33      X1 = (int)(3
80fc0 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20 70  0.6001*E);.    p
80fd0 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 31  ->D = B - D - X1
80fe0 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31  ;.    p->M = E<1
80ff0 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a  4 ? E-1 : E-13;.
81000 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e      p->Y = p->M>
81010 32 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 43  2 ? C - 4716 : C
81020 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70   - 4715;.  }.  p
81030 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a  ->validYMD = 1;.
81040 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  }../*.** Compute
81050 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74   the Hour, Minut
81060 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66  e, and Seconds f
81070 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64  rom the julian d
81080 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  ay number..*/.st
81090 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
810a0 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70  eHMS(DateTime *p
810b0 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 66  ){.  int s;.  if
810c0 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20  ( p->validHMS ) 
810d0 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74  return;.  comput
810e0 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28 69  eJD(p);.  s = (i
810f0 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33  nt)((p->iJD + 43
81100 32 30 30 30 30 30 29 20 25 20 38 36 34 30 30 30  200000) % 864000
81110 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f  00);.  p->s = s/
81120 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69  1000.0;.  s = (i
81130 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20  nt)p->s;.  p->s 
81140 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73  -= s;.  p->h = s
81150 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d  /3600;.  s -= p-
81160 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20  >h*3600;.  p->m 
81170 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b  = s/60;.  p->s +
81180 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20  = s - p->m*60;. 
81190 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31   p->validHMS = 1
811a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ;.}../*.** Compu
811b0 74 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20  te both YMD and 
811c0 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  HMS.*/.static vo
811d0 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d  id computeYMD_HM
811e0 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  S(DateTime *p){.
811f0 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b    computeYMD(p);
81200 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29  .  computeHMS(p)
81210 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  ;.}../*.** Clear
81220 20 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53   the YMD and HMS
81230 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73   and the TZ.*/.s
81240 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72  tatic void clear
81250 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54  YMD_HMS_TZ(DateT
81260 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61  ime *p){.  p->va
81270 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d  lidYMD = 0;.  p-
81280 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20  >validHMS = 0;. 
81290 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b   p->validTZ = 0;
812a0 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
812b0 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d  TE_OMIT_LOCALTIM
812c0 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  E./*.** Compute 
812d0 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 28  the difference (
812e0 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29  in milliseconds)
812f0 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63 61  .** between loca
81300 6c 74 69 6d 65 20 61 6e 64 20 55 54 43 20 28 61  ltime and UTC (a
81310 2e 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f  .k.a. GMT).** fo
81320 72 20 74 68 65 20 74 69 6d 65 20 76 61 6c 75 65  r the time value
81330 20 70 20 77 68 65 72 65 20 70 20 69 73 20 69 6e   p where p is in
81340 20 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   UTC..*/.static 
81350 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f  sqlite3_int64 lo
81360 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 61  caltimeOffset(Da
81370 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61  teTime *p){.  Da
81380 74 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74  teTime x, y;.  t
81390 69 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a  ime_t t;.  x = *
813a0 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f  p;.  computeYMD_
813b0 48 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20 78  HMS(&x);.  if( x
813c0 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d  .Y<1971 || x.Y>=
813d0 32 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 20  2038 ){.    x.Y 
813e0 3d 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20  = 2000;.    x.M 
813f0 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31  = 1;.    x.D = 1
81400 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20  ;.    x.h = 0;. 
81410 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20     x.m = 0;.    
81420 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65  x.s = 0.0;.  } e
81430 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 20  lse {.    int s 
81440 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e  = (int)(x.s + 0.
81450 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b  5);.    x.s = s;
81460 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b  .  }.  x.tz = 0;
81470 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30  .  x.validJD = 0
81480 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78  ;.  computeJD(&x
81490 29 3b 0a 20 20 74 20 3d 20 78 2e 69 4a 44 2f 31  );.  t = x.iJD/1
814a0 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 28  000 - 21086676*(
814b0 69 36 34 29 31 30 30 30 30 3b 0a 23 69 66 64 65  i64)10000;.#ifde
814c0 66 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45  f HAVE_LOCALTIME
814d0 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63  _R.  {.    struc
814e0 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20  t tm sLocal;.   
814f0 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 26 74 2c   localtime_r(&t,
81500 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 20 20 20 79   &sLocal);.    y
81510 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79  .Y = sLocal.tm_y
81520 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20  ear + 1900;.    
81530 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.M = sLocal.tm_
81540 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44  mon + 1;.    y.D
81550 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61   = sLocal.tm_mda
81560 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f  y;.    y.h = sLo
81570 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20  cal.tm_hour;.   
81580 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.m = sLocal.tm
81590 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20  _min;.    y.s = 
815a0 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20  sLocal.tm_sec;. 
815b0 20 7d 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64   }.#elif defined
815c0 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f  (HAVE_LOCALTIME_
815d0 53 29 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63  S).  {.    struc
815e0 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20  t tm sLocal;.   
815f0 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26 73 4c   localtime_s(&sL
81600 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20 20 79  ocal, &t);.    y
81610 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79  .Y = sLocal.tm_y
81620 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20  ear + 1900;.    
81630 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.M = sLocal.tm_
81640 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44  mon + 1;.    y.D
81650 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61   = sLocal.tm_mda
81660 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f  y;.    y.h = sLo
81670 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20  cal.tm_hour;.   
81680 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.m = sLocal.tm
81690 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20  _min;.    y.s = 
816a0 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20  sLocal.tm_sec;. 
816b0 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20   }.#else.  {.   
816c0 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b   struct tm *pTm;
816d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
816e0 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
816f0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
81700 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
81710 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d  ASTER));.    pTm
81720 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 74 29   = localtime(&t)
81730 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 6d 2d  ;.    y.Y = pTm-
81740 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b  >tm_year + 1900;
81750 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 2d 3e  .    y.M = pTm->
81760 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20  tm_mon + 1;.    
81770 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 64  y.D = pTm->tm_md
81780 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 70 54  ay;.    y.h = pT
81790 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20  m->tm_hour;.    
817a0 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 69  y.m = pTm->tm_mi
817b0 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 54 6d  n;.    y.s = pTm
817c0 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 73 71  ->tm_sec;.    sq
817d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
817e0 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  e(sqlite3MutexAl
817f0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
81800 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
81810 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79  ;.  }.#endif.  y
81820 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20  .validYMD = 1;. 
81830 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b   y.validHMS = 1;
81840 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30  .  y.validJD = 0
81850 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 3d 20  ;.  y.validTZ = 
81860 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26  0;.  computeJD(&
81870 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e 69  y);.  return y.i
81880 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65  JD - x.iJD;.}.#e
81890 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
818a0 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f  MIT_LOCALTIME */
818b0 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20  ../*.** Process 
818c0 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61 20  a modifier to a 
818d0 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e  date-time stamp.
818e0 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73 20    The modifiers 
818f0 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77  are.** as follow
81900 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e  s:.**.**     NNN
81910 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e   days.**     NNN
81920 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e   hours.**     NN
81930 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20  N minutes.**    
81940 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64   NNN.NNNN second
81950 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e  s.**     NNN mon
81960 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79  ths.**     NNN y
81970 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61 72  ears.**     star
81980 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20  t of month.**   
81990 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72 0a    start of year.
819a0 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20  **     start of 
819b0 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72  week.**     star
819c0 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20 20  t of day.**     
819d0 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20  weekday N.**    
819e0 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20   unixepoch.**   
819f0 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20    localtime.**  
81a00 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74     utc.**.** Ret
81a10 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73  urn 0 on success
81a20 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 20   and 1 if there 
81a30 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65  is any kind of e
81a40 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rror..*/.static 
81a50 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66 69 65  int parseModifie
81a60 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d  r(const char *zM
81a70 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29  od, DateTime *p)
81a80 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a  {.  int rc = 1;.
81a90 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c    int n;.  doubl
81aa0 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20  e r;.  char *z, 
81ab0 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20  zBuf[30];.  z = 
81ac0 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b  zBuf;.  for(n=0;
81ad0 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 42 75   n<ArraySize(zBu
81ae0 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b  f)-1 && zMod[n];
81af0 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20   n++){.    z[n] 
81b00 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 55  = (char)sqlite3U
81b10 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29  pperToLower[(u8)
81b20 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20  zMod[n]];.  }.  
81b30 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74  z[n] = 0;.  swit
81b40 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e  ch( z[0] ){.#ifn
81b50 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
81b60 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 63 61  LOCALTIME.    ca
81b70 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20  se 'l': {.      
81b80 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a  /*    localtime.
81b90 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
81ba0 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 63  * Assuming the c
81bb0 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61 6c 75  urrent time valu
81bc0 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e  e is UTC (a.k.a.
81bd0 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69 74 20   GMT), shift it 
81be0 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f 77  to.      ** show
81bf0 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20   local time..   
81c00 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
81c10 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c  strcmp(z, "local
81c20 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  time")==0 ){.   
81c30 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70       computeJD(p
81c40 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a  );.        p->iJ
81c50 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66  D += localtimeOf
81c60 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20  fset(p);.       
81c70 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
81c80 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  (p);.        rc 
81c90 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
81ca0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
81cb0 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20  #endif.    case 
81cc0 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a  'u': {.      /*.
81cd0 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e 69 78        **    unix
81ce0 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a 0a 20  epoch.      **. 
81cf0 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74 68       ** Treat th
81d00 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
81d10 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 68 65  of p->iJD as the
81d20 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 20   number of.     
81d30 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e 63   ** seconds sinc
81d40 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72 74  e 1970.  Convert
81d50 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69 61   to a real julia
81d60 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 20 20  n day number..  
81d70 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
81d80 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e 69 78   strcmp(z, "unix
81d90 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 70 2d  epoch")==0 && p-
81da0 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20  >validJD ){.    
81db0 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 70 2d 3e      p->iJD = p->
81dc0 69 4a 44 2f 38 36 34 30 30 20 2b 20 32 31 30 38  iJD/86400 + 2108
81dd0 36 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 30  6676*(i64)100000
81de0 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61  00;.        clea
81df0 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a  rYMD_HMS_TZ(p);.
81e00 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a          rc = 0;.
81e10 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20        }.#ifndef 
81e20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41  SQLITE_OMIT_LOCA
81e30 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 6c 73 65  LTIME.      else
81e40 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 22   if( strcmp(z, "
81e50 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  utc")==0 ){.    
81e60 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
81e70 34 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6f  4 c1;.        co
81e80 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20  mputeJD(p);.    
81e90 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c 74 69      c1 = localti
81ea0 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20  meOffset(p);.   
81eb0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 63       p->iJD -= c
81ec0 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72  1;.        clear
81ed0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20  YMD_HMS_TZ(p);. 
81ee0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d         p->iJD +=
81ef0 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d 65 4f   c1 - localtimeO
81f00 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20  ffset(p);.      
81f10 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20    rc = 0;.      
81f20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 62  }.#endif.      b
81f30 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
81f40 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 20  case 'w': {.    
81f50 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20    /*.      **   
81f60 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20 20 20   weekday N.     
81f70 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76   **.      ** Mov
81f80 65 20 74 68 65 20 64 61 74 65 20 74 6f 20 74 68  e the date to th
81f90 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e 20 74  e same time on t
81fa0 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72 65 6e  he next occurren
81fb0 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 77  ce of.      ** w
81fc0 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65 20 30  eekday N where 0
81fd0 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d 6f 6e  ==Sunday, 1==Mon
81fe0 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74  day, and so fort
81ff0 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20 20 20  h.  If the.     
82000 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c 72 65   ** date is alre
82010 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70 72 6f  ady on the appro
82020 70 72 69 61 74 65 20 77 65 65 6b 64 61 79 2c 20  priate weekday, 
82030 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  this is a no-op.
82040 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
82050 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22  if( strncmp(z, "
82060 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d 3d 30  weekday ", 8)==0
82070 20 26 26 20 67 65 74 56 61 6c 75 65 28 26 7a 5b   && getValue(&z[
82080 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20 20 20  8],&r)>0.       
82090 20 20 20 20 20 20 20 20 20 20 26 26 20 28 6e 3d            && (n=
820a0 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 20 6e 3e  (int)r)==r && n>
820b0 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a 20 20 20  =0 && r<7 ){.   
820c0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
820d0 36 34 20 5a 3b 0a 20 20 20 20 20 20 20 20 63 6f  64 Z;.        co
820e0 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b  mputeYMD_HMS(p);
820f0 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  .        p->vali
82100 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dTZ = 0;.       
82110 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b   p->validJD = 0;
82120 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
82130 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 5a  JD(p);.        Z
82140 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b 20 31 32   = ((p->iJD + 12
82150 39 36 30 30 30 30 30 29 2f 38 36 34 30 30 30 30  9600000)/8640000
82160 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20 20 20  0) % 7;.        
82170 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d 3d 20 37  if( Z>n ) Z -= 7
82180 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44  ;.        p->iJD
82190 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38 36 34 30   += (n - Z)*8640
821a0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c  0000;.        cl
821b0 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29  earYMD_HMS_TZ(p)
821c0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  ;.        rc = 0
821d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
821e0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
821f0 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20   case 's': {.   
82200 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20     /*.      **  
82210 20 20 73 74 61 72 74 20 6f 66 20 54 54 54 54 54    start of TTTTT
82220 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
82230 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 65  ** Move the date
82240 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20 74 68   backwards to th
82250 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
82260 68 65 20 63 75 72 72 65 6e 74 20 64 61 79 2c 0a  he current day,.
82270 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f 6e 74        ** or mont
82280 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 20 20 20  h or year..     
82290 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74   */.      if( st
822a0 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 72 74 20  rncmp(z, "start 
822b0 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 20 62 72  of ", 9)!=0 ) br
822c0 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20  eak;.      z += 
822d0 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65  9;.      compute
822e0 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 70 2d  YMD(p);.      p-
822f0 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20  >validHMS = 1;. 
82300 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 2d 3e 6d       p->h = p->m
82310 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 73   = 0;.      p->s
82320 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20 70 2d   = 0.0;.      p-
82330 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20  >validTZ = 0;.  
82340 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d      p->validJD =
82350 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73 74   0;.      if( st
82360 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d  rcmp(z,"month")=
82370 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  =0 ){.        p-
82380 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  >D = 1;.        
82390 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  rc = 0;.      }e
823a0 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a  lse if( strcmp(z
823b0 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20  ,"year")==0 ){. 
823c0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d         computeYM
823d0 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  D(p);.        p-
823e0 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  >M = 1;.        
823f0 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20  p->D = 1;.      
82400 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20    rc = 0;.      
82410 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70  }else if( strcmp
82420 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a  (z,"day")==0 ){.
82430 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a          rc = 0;.
82440 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
82450 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
82460 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 63 61 73  ase '+':.    cas
82470 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 73 65 20  e '-':.    case 
82480 27 30 27 3a 0a 20 20 20 20 63 61 73 65 20 27 31  '0':.    case '1
82490 27 3a 0a 20 20 20 20 63 61 73 65 20 27 32 27 3a  ':.    case '2':
824a0 0a 20 20 20 20 63 61 73 65 20 27 33 27 3a 0a 20  .    case '3':. 
824b0 20 20 20 63 61 73 65 20 27 34 27 3a 0a 20 20 20     case '4':.   
824c0 20 63 61 73 65 20 27 35 27 3a 0a 20 20 20 20 63   case '5':.    c
824d0 61 73 65 20 27 36 27 3a 0a 20 20 20 20 63 61 73  ase '6':.    cas
824e0 65 20 27 37 27 3a 0a 20 20 20 20 63 61 73 65 20  e '7':.    case 
824f0 27 38 27 3a 0a 20 20 20 20 63 61 73 65 20 27 39  '8':.    case '9
82500 27 3a 20 7b 0a 20 20 20 20 20 20 6e 20 3d 20 67  ': {.      n = g
82510 65 74 56 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a  etValue(z, &r);.
82520 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e        assert( n>
82530 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =1 );.      if( 
82540 7a 5b 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20  z[n]==':' ){.   
82550 20 20 20 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69       /* A modifi
82560 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 28  er of the form (
82570 2b 7c 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46  +|-)HH:MM:SS.FFF
82580 20 61 64 64 73 20 28 6f 72 20 73 75 62 74 72 61   adds (or subtra
82590 63 74 73 29 20 74 68 65 0a 20 20 20 20 20 20 20  cts) the.       
825a0 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75   ** specified nu
825b0 6d 62 65 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d  mber of hours, m
825c0 69 6e 75 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c  inutes, seconds,
825d0 20 61 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c 20   and fractional 
825e0 73 65 63 6f 6e 64 73 0a 20 20 20 20 20 20 20 20  seconds.        
825f0 2a 2a 20 74 6f 20 74 68 65 20 74 69 6d 65 2e 20  ** to the time. 
82600 20 54 68 65 20 22 2e 46 46 46 22 20 6d 61 79 20   The ".FFF" may 
82610 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 65  be omitted.  The
82620 20 22 3a 53 53 2e 46 46 46 22 20 6d 61 79 20 62   ":SS.FFF" may b
82630 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69  e.        ** omi
82640 74 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f  tted..        */
82650 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  .        const c
82660 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20  har *z2 = z;.   
82670 20 20 20 20 20 44 61 74 65 54 69 6d 65 20 74 78       DateTime tx
82680 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
82690 33 5f 69 6e 74 36 34 20 64 61 79 3b 0a 20 20 20  3_int64 day;.   
826a0 20 20 20 20 20 69 66 28 20 21 69 73 64 69 67 69       if( !isdigi
826b0 74 28 2a 28 75 38 2a 29 7a 32 29 20 29 20 7a 32  t(*(u8*)z2) ) z2
826c0 2b 2b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  ++;.        mems
826d0 65 74 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f  et(&tx, 0, sizeo
826e0 66 28 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20  f(tx));.        
826f0 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28  if( parseHhMmSs(
82700 7a 32 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b  z2, &tx) ) break
82710 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
82720 65 4a 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20  eJD(&tx);.      
82730 20 20 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30    tx.iJD -= 4320
82740 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61  0000;.        da
82750 79 20 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30  y = tx.iJD/86400
82760 30 30 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e  000;.        tx.
82770 69 4a 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30  iJD -= day*86400
82780 30 30 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  000;.        if(
82790 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e   z[0]=='-' ) tx.
827a0 69 4a 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20  iJD = -tx.iJD;. 
827b0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
827c0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65  (p);.        cle
827d0 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
827e0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
827f0 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20  += tx.iJD;.     
82800 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
82810 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
82820 7d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a  }.      z += n;.
82830 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73 73        while( iss
82840 70 61 63 65 28 2a 28 75 38 2a 29 7a 29 20 29 20  pace(*(u8*)z) ) 
82850 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73  z++;.      n = s
82860 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
82870 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 31  );.      if( n>1
82880 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b  0 || n<3 ) break
82890 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d  ;.      if( z[n-
828a0 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31  1]=='s' ){ z[n-1
828b0 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20  ] = 0; n--; }.  
828c0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
828d0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a  ;.      rc = 0;.
828e0 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 20 26        if( n==3 &
828f0 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22  & strcmp(z,"day"
82900 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
82910 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
82920 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30  e3_int64)(r*8640
82930 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20  0000.0 + 0.5);. 
82940 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
82950 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c  ==4 && strcmp(z,
82960 22 68 6f 75 72 22 29 3d 3d 30 20 29 7b 0a 20 20  "hour")==0 ){.  
82970 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
82980 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
82990 72 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 32 34  r*(86400000.0/24
829a0 2e 30 29 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20  .0) + 0.5);.    
829b0 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36    }else if( n==6
829c0 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69   && strcmp(z,"mi
829d0 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  nute")==0 ){.   
829e0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
829f0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72  sqlite3_int64)(r
82a00 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34  *(86400000.0/(24
82a10 2e 30 2a 36 30 2e 30 29 29 20 2b 20 30 2e 35 29  .0*60.0)) + 0.5)
82a20 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
82a30 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70  ( n==6 && strcmp
82a40 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 20  (z,"second")==0 
82a50 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a  ){.        p->iJ
82a60 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  D += (sqlite3_in
82a70 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 30  t64)(r*(86400000
82a80 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 30  .0/(24.0*60.0*60
82a90 2e 30 29 29 20 2b 20 30 2e 35 29 3b 0a 20 20 20  .0)) + 0.5);.   
82aa0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d     }else if( n==
82ab0 35 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d  5 && strcmp(z,"m
82ac0 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20  onth")==0 ){.   
82ad0 20 20 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20       int x, y;. 
82ae0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d         computeYM
82af0 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20  D_HMS(p);.      
82b00 20 20 70 2d 3e 4d 20 2b 3d 20 28 69 6e 74 29 72    p->M += (int)r
82b10 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d  ;.        x = p-
82b20 3e 4d 3e 30 20 3f 20 28 70 2d 3e 4d 2d 31 29 2f  >M>0 ? (p->M-1)/
82b30 31 32 20 3a 20 28 70 2d 3e 4d 2d 31 32 29 2f 31  12 : (p->M-12)/1
82b40 32 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20  2;.        p->Y 
82b50 2b 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 70 2d  += x;.        p-
82b60 3e 4d 20 2d 3d 20 78 2a 31 32 3b 0a 20 20 20 20  >M -= x*12;.    
82b70 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d      p->validJD =
82b80 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   0;.        comp
82b90 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20  uteJD(p);.      
82ba0 20 20 79 20 3d 20 28 69 6e 74 29 72 3b 0a 20 20    y = (int)r;.  
82bb0 20 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29        if( y!=r )
82bc0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69  {.          p->i
82bd0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69  JD += (sqlite3_i
82be0 6e 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 30  nt64)((r - y)*30
82bf0 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20  .0*86400000.0 + 
82c00 30 2e 35 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  0.5);.        }.
82c10 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
82c20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a  n==4 && strcmp(z
82c30 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20  ,"year")==0 ){. 
82c40 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d         computeYM
82c50 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20  D_HMS(p);.      
82c60 20 20 70 2d 3e 59 20 2b 3d 20 28 69 6e 74 29 72    p->Y += (int)r
82c70 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c  ;.        p->val
82c80 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20  idJD = 0;.      
82c90 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
82ca0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
82cb0 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20       rc = 1;.   
82cc0 20 20 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72     }.      clear
82cd0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20  YMD_HMS_TZ(p);. 
82ce0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
82cf0 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
82d00 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
82d10 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
82d20 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72   rc;.}../*.** Pr
82d30 6f 63 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74  ocess time funct
82d40 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20  ion arguments.  
82d50 61 72 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74  argv[0] is a dat
82d60 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a  e-time stamp..**
82d70 20 61 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c   argv[1] and fol
82d80 6c 6f 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66  lowing are modif
82d90 69 65 72 73 2e 20 20 50 61 72 73 65 20 74 68 65  iers.  Parse the
82da0 6d 20 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a  m all and write.
82db0 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  ** the resulting
82dc0 20 74 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44   time into the D
82dd0 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 72  ateTime structur
82de0 65 20 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a  e p.  Return 0.*
82df0 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  * on success and
82e00 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20   1 if there are 
82e10 61 6e 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a  any errors..**.*
82e20 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 7a  * If there are z
82e30 65 72 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28  ero parameters (
82e40 69 66 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20  if even argv[0] 
82e50 69 73 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a  is undefined).**
82e60 20 74 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64   then assume a d
82e70 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20  efault value of 
82e80 22 6e 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30  "now" for argv[0
82e90 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ]..*/.static int
82ea0 20 69 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74   isDate(.  sqlit
82eb0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
82ec0 65 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63  ext, .  int argc
82ed0 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
82ee0 75 65 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61  ue **argv, .  Da
82ef0 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69  teTime *p.){.  i
82f00 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e  nt i;.  const un
82f10 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a  signed char *z;.
82f20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d    int eType;.  m
82f30 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65  emset(p, 0, size
82f40 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61  of(*p));.  if( a
82f50 72 67 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65  rgc==0 ){.    se
82f60 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65  tDateTimeToCurre
82f70 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a  nt(context, p);.
82f80 20 20 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79    }else if( (eTy
82f90 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pe = sqlite3_val
82fa0 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
82fb0 29 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a  )==SQLITE_FLOAT.
82fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82fd0 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c     || eType==SQL
82fe0 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20  ITE_INTEGER ){. 
82ff0 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c     p->iJD = (sql
83000 69 74 65 33 5f 69 6e 74 36 34 29 28 73 71 6c 69  ite3_int64)(sqli
83010 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
83020 28 61 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30  (argv[0])*864000
83030 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20  00.0 + 0.5);.   
83040 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b   p->validJD = 1;
83050 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20  .  }else{.    z 
83060 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
83070 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
83080 20 20 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72     if( !z || par
83090 73 65 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e  seDateOrTime(con
830a0 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20  text, (char*)z, 
830b0 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  p) ){.      retu
830c0 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 1;.    }.  }.
830d0 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67    for(i=1; i<arg
830e0 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  c; i++){.    if(
830f0 20 28 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61   (z = sqlite3_va
83100 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d  lue_text(argv[i]
83110 29 29 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f  ))==0 || parseMo
83120 64 69 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c  difier((char*)z,
83130 20 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74   p) ){.      ret
83140 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
83150 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
83160 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
83170 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d  wing routines im
83180 70 6c 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69  plement the vari
83190 6f 75 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d  ous date and tim
831a0 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f  e functions.** o
831b0 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a  f SQLite..*/../*
831c0 0a 2a 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79  .**    julianday
831d0 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f  ( TIMESTRING, MO
831e0 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a  D, MOD, ...).**.
831f0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75  ** Return the ju
83200 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20  lian day number 
83210 6f 66 20 74 68 65 20 64 61 74 65 20 73 70 65 63  of the date spec
83220 69 66 69 65 64 20 69 6e 20 74 68 65 20 61 72 67  ified in the arg
83230 75 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63  uments.*/.static
83240 20 76 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46   void juliandayF
83250 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
83260 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
83270 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
83280 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
83290 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d  rgv.){.  DateTim
832a0 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74  e x;.  if( isDat
832b0 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c  e(context, argc,
832c0 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b   argv, &x)==0 ){
832d0 0a 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26  .    computeJD(&
832e0 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  x);.    sqlite3_
832f0 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f  result_double(co
83300 6e 74 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34  ntext, x.iJD/864
83310 30 30 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a  00000.0);.  }.}.
83320 0a 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69  ./*.**    dateti
83330 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20  me( TIMESTRING, 
83340 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a  MOD, MOD, ...).*
83350 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59  *.** Return YYYY
83360 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a  -MM-DD HH:MM:SS.
83370 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
83380 61 74 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73  atetimeFunc(.  s
83390 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
833a0 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
833b0 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
833c0 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
833d0 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69   DateTime x;.  i
833e0 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78  f( isDate(contex
833f0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26  t, argc, argv, &
83400 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61  x)==0 ){.    cha
83410 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20  r zBuf[100];.   
83420 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
83430 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  &x);.    sqlite3
83440 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
83450 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25  (zBuf), zBuf, "%
83460 30 34 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30  04d-%02d-%02d %0
83470 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20  2d:%02d:%02d",. 
83480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
83490 20 20 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e      x.Y, x.M, x.
834a0 44 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e  D, x.h, x.m, (in
834b0 74 29 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71  t)(x.s));.    sq
834c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
834d0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
834e0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
834f0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  SIENT);.  }.}../
83500 2a 0a 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49  *.**    time( TI
83510 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d  MESTRING, MOD, M
83520 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52  OD, ...).**.** R
83530 65 74 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a  eturn HH:MM:SS.*
83540 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 69  /.static void ti
83550 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  meFunc(.  sqlite
83560 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
83570 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
83580 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
83590 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65  **argv.){.  Date
835a0 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73  Time x;.  if( is
835b0 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  Date(context, ar
835c0 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30  gc, argv, &x)==0
835d0 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75   ){.    char zBu
835e0 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70  f[100];.    comp
835f0 75 74 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20  uteHMS(&x);.    
83600 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
83610 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a  (sizeof(zBuf), z
83620 42 75 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a  Buf, "%02d:%02d:
83630 25 30 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c  %02d", x.h, x.m,
83640 20 28 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20   (int)x.s);.    
83650 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
83660 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75  ext(context, zBu
83670 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52  f, -1, SQLITE_TR
83680 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a  ANSIENT);.  }.}.
83690 0a 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20  ./*.**    date( 
836a0 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c  TIMESTRING, MOD,
836b0 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a   MOD, ...).**.**
836c0 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d   Return YYYY-MM-
836d0 44 44 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  DD.*/.static voi
836e0 64 20 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71  d dateFunc(.  sq
836f0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
83700 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
83710 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
83720 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
83730 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66  DateTime x;.  if
83740 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74  ( isDate(context
83750 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78  , argc, argv, &x
83760 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72  )==0 ){.    char
83770 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20   zBuf[100];.    
83780 63 6f 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a  computeYMD(&x);.
83790 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
837a0 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66  intf(sizeof(zBuf
837b0 29 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25  ), zBuf, "%04d-%
837c0 30 32 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20  02d-%02d", x.Y, 
837d0 78 2e 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73  x.M, x.D);.    s
837e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
837f0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66  xt(context, zBuf
83800 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41  , -1, SQLITE_TRA
83810 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a  NSIENT);.  }.}..
83820 2f 2a 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d  /*.**    strftim
83830 65 28 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53  e( FORMAT, TIMES
83840 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c  TRING, MOD, MOD,
83850 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75   ...).**.** Retu
83860 72 6e 20 61 20 73 74 72 69 6e 67 20 64 65 73 63  rn a string desc
83870 72 69 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e  ribed by FORMAT.
83880 20 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73    Conversions as
83890 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
838a0 20 20 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e    %d  day of mon
838b0 74 68 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66  th.**   %f  ** f
838c0 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64  ractional second
838d0 73 20 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25  s  SS.SSS.**   %
838e0 48 20 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a  H  hour 00-24.**
838f0 20 20 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65     %j  day of ye
83900 61 72 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20  ar 000-366.**   
83910 25 4a 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61  %J  ** Julian da
83920 79 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d  y number.**   %m
83930 20 20 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a    month 01-12.**
83940 20 20 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30     %M  minute 00
83950 2d 35 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63  -59.**   %s  sec
83960 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d  onds since 1970-
83970 30 31 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73  01-01.**   %S  s
83980 65 63 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20  econds 00-59.** 
83990 20 20 25 77 20 20 64 61 79 20 6f 66 20 77 65 65    %w  day of wee
839a0 6b 20 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30  k 0-6  sunday==0
839b0 0a 2a 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f  .**   %W  week o
839c0 66 20 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20  f year 00-53.** 
839d0 20 20 25 59 20 20 79 65 61 72 20 30 30 30 30 2d    %Y  year 0000-
839e0 39 39 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a  9999.**   %%  %.
839f0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
83a00 74 72 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73  trftimeFunc(.  s
83a10 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
83a20 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
83a30 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
83a40 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
83a50 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75   DateTime x;.  u
83a60 36 34 20 6e 3b 0a 20 20 73 69 7a 65 5f 74 20 69  64 n;.  size_t i
83a70 2c 6a 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  ,j;.  char *z;. 
83a80 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
83a90 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74  const char *zFmt
83aa0 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
83ab0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
83ac0 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63  xt(argv[0]);.  c
83ad0 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20  har zBuf[100];. 
83ae0 20 69 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20   if( zFmt==0 || 
83af0 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20  isDate(context, 
83b00 61 72 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20  argc-1, argv+1, 
83b10 26 78 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  &x) ) return;.  
83b20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
83b30 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
83b40 6f 6e 74 65 78 74 29 3b 0a 20 20 66 6f 72 28 69  ontext);.  for(i
83b50 3d 30 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d  =0, n=1; zFmt[i]
83b60 3b 20 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20  ; i++, n++){.   
83b70 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25   if( zFmt[i]=='%
83b80 27 20 29 7b 0a 20 20 20 20 20 20 73 77 69 74 63  ' ){.      switc
83b90 68 28 20 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a  h( zFmt[i+1] ){.
83ba0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 64 27          case 'd'
83bb0 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  :.        case '
83bc0 48 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65  H':.        case
83bd0 20 27 6d 27 3a 0a 20 20 20 20 20 20 20 20 63 61   'm':.        ca
83be0 73 65 20 27 4d 27 3a 0a 20 20 20 20 20 20 20 20  se 'M':.        
83bf0 63 61 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20  case 'S':.      
83c00 20 20 63 61 73 65 20 27 57 27 3a 0a 20 20 20 20    case 'W':.    
83c10 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20        n++;.     
83c20 20 20 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72       /* fall thr
83c30 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73  u */.        cas
83c40 65 20 27 77 27 3a 0a 20 20 20 20 20 20 20 20 63  e 'w':.        c
83c50 61 73 65 20 27 25 27 3a 0a 20 20 20 20 20 20 20  ase '%':.       
83c60 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
83c70 20 20 63 61 73 65 20 27 66 27 3a 0a 20 20 20 20    case 'f':.    
83c80 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20        n += 8;.  
83c90 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
83ca0 20 20 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a         case 'j':
83cb0 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  .          n += 
83cc0 33 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  3;.          bre
83cd0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
83ce0 20 27 59 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'Y':.          
83cf0 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20  n += 8;.        
83d00 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
83d10 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20   case 's':.     
83d20 20 20 20 63 61 73 65 20 27 4a 27 3a 0a 20 20 20     case 'J':.   
83d30 20 20 20 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a         n += 50;.
83d40 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
83d50 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74  .        default
83d60 3a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  :.          retu
83d70 72 6e 3b 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20  rn;  /* ERROR.  
83d80 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f  return a NULL */
83d90 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
83da0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ++;.    }.  }.  
83db0 69 66 28 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75  if( n<sizeof(zBu
83dc0 66 29 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42  f) ){.    z = zB
83dd0 75 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  uf;.  }else if( 
83de0 6e 3e 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69  n>(u64)db->aLimi
83df0 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
83e00 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71  ENGTH] ){.    sq
83e10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
83e20 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
83e30 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
83e40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d    }else{.    z =
83e50 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
83e60 52 61 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b  Raw(db, (int)n);
83e70 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b  .    if( z==0 ){
83e80 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
83e90 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
83ea0 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  m(context);.    
83eb0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
83ec0 20 20 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28    }.  computeJD(
83ed0 26 78 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d  &x);.  computeYM
83ee0 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f 72  D_HMS(&x);.  for
83ef0 28 69 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b  (i=j=0; zFmt[i];
83f00 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a   i++){.    if( z
83f10 46 6d 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a 20  Fmt[i]!='%' ){. 
83f20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46       z[j++] = zF
83f30 6d 74 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65  mt[i];.    }else
83f40 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20  {.      i++;.   
83f50 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b     switch( zFmt[
83f60 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61  i] ){.        ca
83f70 73 65 20 27 64 27 3a 20 20 73 71 6c 69 74 65 33  se 'd':  sqlite3
83f80 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b  _snprintf(3, &z[
83f90 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b 20  j],"%02d",x.D); 
83fa0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20  j+=2; break;.   
83fb0 20 20 20 20 20 63 61 73 65 20 27 66 27 3a 20 7b       case 'f': {
83fc0 0a 20 20 20 20 20 20 20 20 20 20 64 6f 75 62 6c  .          doubl
83fd0 65 20 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20 20  e s = x.s;.     
83fe0 20 20 20 20 20 69 66 28 20 73 3e 35 39 2e 39 39       if( s>59.99
83ff0 39 20 29 20 73 20 3d 20 35 39 2e 39 39 39 3b 0a  9 ) s = 59.999;.
84000 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
84010 33 5f 73 6e 70 72 69 6e 74 66 28 37 2c 20 26 7a  3_snprintf(7, &z
84020 5b 6a 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73 29  [j],"%06.3f", s)
84030 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d  ;.          j +=
84040 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
84050 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20  (&z[j]);.       
84060 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
84070 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65    }.        case
84080 20 27 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'H':  sqlite3_s
84090 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d  nprintf(3, &z[j]
840a0 2c 22 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b  ,"%02d",x.h); j+
840b0 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
840c0 20 20 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20     case 'W': /* 
840d0 46 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20  Fall thru */.   
840e0 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b       case 'j': {
840f0 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e  .          int n
84100 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Day;            
84110 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61   /* Number of da
84120 79 73 20 73 69 6e 63 65 20 31 73 74 20 64 61 79  ys since 1st day
84130 20 6f 66 20 79 65 61 72 20 2a 2f 0a 20 20 20 20   of year */.    
84140 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20 79        DateTime y
84150 20 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20   = x;.          
84160 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  y.validJD = 0;. 
84170 20 20 20 20 20 20 20 20 20 79 2e 4d 20 3d 20 31           y.M = 1
84180 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e 44 20  ;.          y.D 
84190 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63  = 1;.          c
841a0 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20  omputeJD(&y);.  
841b0 20 20 20 20 20 20 20 20 6e 44 61 79 20 3d 20 28          nDay = (
841c0 69 6e 74 29 28 28 78 2e 69 4a 44 2d 79 2e 69 4a  int)((x.iJD-y.iJ
841d0 44 2b 34 33 32 30 30 30 30 30 29 2f 38 36 34 30  D+43200000)/8640
841e0 30 30 30 30 29 3b 0a 20 20 20 20 20 20 20 20 20  0000);.         
841f0 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57   if( zFmt[i]=='W
84200 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ' ){.           
84210 20 69 6e 74 20 77 64 3b 20 20 20 2f 2a 20 30 3d   int wd;   /* 0=
84220 4d 6f 6e 64 61 79 2c 20 31 3d 54 75 65 73 64 61  Monday, 1=Tuesda
84230 79 2c 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 79 20  y, ... 6=Sunday 
84240 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 77  */.            w
84250 64 20 3d 20 28 69 6e 74 29 28 28 28 78 2e 69 4a  d = (int)(((x.iJ
84260 44 2b 34 33 32 30 30 30 30 30 29 2f 38 36 34 30  D+43200000)/8640
84270 30 30 30 30 29 25 37 29 3b 0a 20 20 20 20 20 20  0000)%7);.      
84280 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
84290 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c  printf(3, &z[j],
842a0 22 25 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77  "%02d",(nDay+7-w
842b0 64 29 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20  d)/7);.         
842c0 20 20 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20     j += 2;.     
842d0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
842e0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
842f0 73 6e 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a  snprintf(4, &z[j
84300 5d 2c 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29  ],"%03d",nDay+1)
84310 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20  ;.            j 
84320 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20  += 3;.          
84330 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  }.          brea
84340 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
84350 20 20 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b       case 'J': {
84360 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
84370 65 33 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20  e3_snprintf(20, 
84380 26 7a 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e  &z[j],"%.16g",x.
84390 69 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b  iJD/86400000.0);
843a0 0a 20 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 71  .          j+=sq
843b0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a  lite3Strlen30(&z
843c0 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  [j]);.          
843d0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
843e0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 6d  .        case 'm
843f0 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ':  sqlite3_snpr
84400 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(3, &z[j],"%
84410 30 32 64 22 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b  02d",x.M); j+=2;
84420 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
84430 63 61 73 65 20 27 4d 27 3a 20 20 73 71 6c 69 74  case 'M':  sqlit
84440 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26  e3_snprintf(3, &
84450 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 6d 29  z[j],"%02d",x.m)
84460 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20  ; j+=2; break;. 
84470 20 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a         case 's':
84480 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c   {.          sql
84490 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 30  ite3_snprintf(30
844a0 2c 26 7a 5b 6a 5d 2c 22 25 64 22 2c 0a 20 20 20  ,&z[j],"%d",.   
844b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
844c0 20 20 20 20 20 20 20 20 28 69 6e 74 29 28 78 2e          (int)(x.
844d0 69 4a 44 2f 31 30 30 30 2e 30 20 2d 20 32 31 30  iJD/1000.0 - 210
844e0 38 36 36 37 36 30 30 30 30 2e 30 29 29 3b 0a 20  866760000.0));. 
844f0 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71           j += sq
84500 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a  lite3Strlen30(&z
84510 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  [j]);.          
84520 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
84530 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53  .        case 'S
84540 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ':  sqlite3_snpr
84550 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30  intf(3,&z[j],"%0
84560 32 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a  2d",(int)x.s); j
84570 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  +=2; break;.    
84580 20 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a      case 'w': {.
84590 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d            z[j++]
845a0 20 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69 4a   = (char)(((x.iJ
845b0 44 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36 34  D+129600000)/864
845c0 30 30 30 30 30 29 20 25 20 37 29 20 2b 20 27 30  00000) % 7) + '0
845d0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ';.          bre
845e0 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
845f0 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20        case 'Y': 
84600 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
84610 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26  te3_snprintf(5,&
84620 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29  z[j],"%04d",x.Y)
84630 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c  ; j+=sqlite3Strl
84640 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20  en30(&z[j]);.   
84650 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
84660 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
84670 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b  default:   z[j++
84680 5d 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a  ] = '%'; break;.
84690 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
846a0 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20  }.  z[j] = 0;.  
846b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
846c0 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20  ext(context, z, 
846d0 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  -1,.            
846e0 20 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75            z==zBu
846f0 66 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  f ? SQLITE_TRANS
84700 49 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59  IENT : SQLITE_DY
84710 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NAMIC);.}../*.**
84720 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a   current_time().
84730 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
84740 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  ion returns the 
84750 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69  same value as ti
84760 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74  me('now')..*/.st
84770 61 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46  atic void ctimeF
84780 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
84790 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
847a0 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
847b0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
847c0 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20  **NotUsed2.){.  
847d0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
847e0 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
847f0 65 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63  ed2);.  timeFunc
84800 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b  (context, 0, 0);
84810 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e  .}../*.** curren
84820 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54  t_date().**.** T
84830 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
84840 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61  urns the same va
84850 6c 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77  lue as date('now
84860 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ')..*/.static vo
84870 69 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20  id cdateFunc(.  
84880 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
84890 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
848a0 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
848b0 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73  e3_value **NotUs
848c0 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f  ed2.){.  UNUSED_
848d0 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
848e0 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
848f0 20 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 78   dateFunc(contex
84900 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  t, 0, 0);.}../*.
84910 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73  ** current_times
84920 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69  tamp().**.** Thi
84930 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
84940 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  ns the same valu
84950 65 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 6e  e as datetime('n
84960 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ow')..*/.static 
84970 76 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 46  void ctimestampF
84980 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
84990 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
849a0 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
849b0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
849c0 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20  **NotUsed2.){.  
849d0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
849e0 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
849f0 65 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d 65  ed2);.  datetime
84a00 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c  Func(context, 0,
84a10 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a   0);.}.#endif /*
84a20 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
84a30 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46  _OMIT_DATETIME_F
84a40 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66  UNCS) */..#ifdef
84a50 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54   SQLITE_OMIT_DAT
84a60 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a  ETIME_FUNCS./*.*
84a70 2a 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79  * If the library
84a80 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20   is compiled to 
84a90 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73 63  omit the full-sc
84aa0 61 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d  ale date and tim
84ab0 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74  e.** handling (t
84ac0 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72 20  o get a smaller 
84ad0 62 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f 6c  binary), the fol
84ae0 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76  lowing minimal v
84af0 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65  ersion.** of the
84b00 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72 65   functions curre
84b10 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72 65  nt_time(), curre
84b20 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63 75  nt_date() and cu
84b30 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28  rrent_timestamp(
84b40 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65  ).** are include
84b50 64 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73 20  d instead. This 
84b60 69 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63 6f  is to support co
84b70 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e  lumn declaration
84b80 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64  s that.** includ
84b90 65 20 22 44 45 46 41 55 4c 54 20 43 55 52 52 45  e "DEFAULT CURRE
84ba0 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a  NT_TIME" etc..**
84bb0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
84bc0 6e 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 62  n uses the C-lib
84bd0 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74  rary functions t
84be0 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a  ime(), gmtime().
84bf0 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 28  ** and strftime(
84c00 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 74  ). The format st
84c10 72 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f 20  ring to pass to 
84c20 73 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 75  strftime() is su
84c30 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68 65  pplied.** as the
84c40 20 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20 74   user-data for t
84c50 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  he function..*/.
84c60 73 74 61 74 69 63 20 76 6f 69 64 20 63 75 72 72  static void curr
84c70 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20 73  entTimeFunc(.  s
84c80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
84c90 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
84ca0 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
84cb0 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
84cc0 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61   time_t t;.  cha
84cd0 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68  r *zFormat = (ch
84ce0 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65  ar *)sqlite3_use
84cf0 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b  r_data(context);
84d00 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
84d10 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63    double rT;.  c
84d20 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20  har zBuf[20];.. 
84d30 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f   db = sqlite3_co
84d40 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
84d50 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69  context);.  sqli
84d60 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
84d70 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b  (db->pVfs, &rT);
84d80 0a 20 20 74 20 3d 20 38 36 34 30 30 2e 30 2a 28  .  t = 86400.0*(
84d90 72 54 20 2d 20 32 34 34 30 35 38 37 2e 35 29 20  rT - 2440587.5) 
84da0 2b 20 30 2e 35 3b 0a 23 69 66 64 65 66 20 48 41  + 0.5;.#ifdef HA
84db0 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a  VE_GMTIME_R.  {.
84dc0 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4e      struct tm sN
84dd0 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72  ow;.    gmtime_r
84de0 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20  (&t, &sNow);.   
84df0 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20   strftime(zBuf, 
84e00 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e  20, zFormat, &sN
84e10 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  ow);.  }.#else. 
84e20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d   {.    struct tm
84e30 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74   *pTm;.    sqlit
84e40 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73  e3_mutex_enter(s
84e50 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
84e60 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
84e70 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20  ATIC_MASTER));. 
84e80 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28     pTm = gmtime(
84e90 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74 69 6d  &t);.    strftim
84ea0 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72  e(zBuf, 20, zFor
84eb0 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73  mat, pTm);.    s
84ec0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
84ed0 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ve(sqlite3MutexA
84ee0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
84ef0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
84f00 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
84f10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
84f20 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42  text(context, zB
84f30 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54  uf, -1, SQLITE_T
84f40 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e  RANSIENT);.}.#en
84f50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
84f60 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65  function registe
84f70 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20 61  red all of the a
84f80 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e 73  bove C functions
84f90 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74   as SQL.** funct
84fa0 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f 75  ions.  This shou
84fb0 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72  ld be the only r
84fc0 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66  outine in this f
84fd0 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74 65  ile with.** exte
84fe0 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f  rnal linkage..*/
84ff0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
85000 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69  void sqlite3Regi
85010 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63  sterDateTimeFunc
85020 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73  tions(void){.  s
85030 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
85040 20 46 75 6e 63 44 65 66 20 61 44 61 74 65 54 69   FuncDef aDateTi
85050 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69  meFuncs[] = {.#i
85060 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
85070 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53  T_DATETIME_FUNCS
85080 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75  .    FUNCTION(ju
85090 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20 20 20  lianday,        
850a0 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e  -1, 0, 0, julian
850b0 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20 20 46  dayFunc ),.    F
850c0 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20 20 20  UNCTION(date,   
850d0 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c            -1, 0,
850e0 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20 20 20   0, dateFunc    
850f0 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
85100 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20  N(time,         
85110 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69      -1, 0, 0, ti
85120 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20  meFunc      ),. 
85130 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74 65     FUNCTION(date
85140 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 2d 31  time,         -1
85150 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69 6d 65  , 0, 0, datetime
85160 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 55 4e  Func  ),.    FUN
85170 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20  CTION(strftime, 
85180 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30          -1, 0, 0
85190 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 20 20  , strftimeFunc  
851a0 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
851b0 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20  current_time,   
851c0 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d     0, 0, 0, ctim
851d0 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 20 20  eFunc     ),.   
851e0 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e   FUNCTION(curren
851f0 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20  t_timestamp, 0, 
85200 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61 6d 70  0, 0, ctimestamp
85210 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43 54  Func),.    FUNCT
85220 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 65  ION(current_date
85230 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  ,      0, 0, 0, 
85240 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 29 2c  cdateFunc     ),
85250 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52 5f 46  .#else.    STR_F
85260 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f  UNCTION(current_
85270 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 22 25  time,      0, "%
85280 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 20 20  H:%M:%S",       
85290 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d     0, currentTim
852a0 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f  eFunc),.    STR_
852b0 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74  FUNCTION(current
852c0 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22  _timestamp, 0, "
852d0 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20 20 20  %Y-%m-%d",      
852e0 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69      0, currentTi
852f0 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52  meFunc),.    STR
85300 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e  _FUNCTION(curren
85310 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 2c 20  t_date,      0, 
85320 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a  "%Y-%m-%d %H:%M:
85330 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e 74 54  %S", 0, currentT
85340 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66  imeFunc),.#endif
85350 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  .  };.  int i;. 
85360 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48   FuncDefHash *pH
85370 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75  ash = &GLOBAL(Fu
85380 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74  ncDefHash, sqlit
85390 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e  e3GlobalFunction
853a0 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61  s);.  FuncDef *a
853b0 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66 2a  Func = (FuncDef*
853c0 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66  )&GLOBAL(FuncDef
853d0 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 73  , aDateTimeFuncs
853e0 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  );..  for(i=0; i
853f0 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61 74 65  <ArraySize(aDate
85400 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29  TimeFuncs); i++)
85410 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e  {.    sqlite3Fun
85420 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68  cDefInsert(pHash
85430 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20  , &aFunc[i]);.  
85440 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.}../**********
85450 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74 65  **** End of date
85460 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
85470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85490 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
854a0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
854b0 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os.c ***********
854c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
854d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
854e0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20  ***/./*.** 2005 
854f0 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a  November 29.**.*
85500 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
85510 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
85520 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
85530 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
85540 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
85550 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
85560 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
85570 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
85580 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
85590 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
855a0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
855b0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
855c0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
855d0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
855e0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
855f0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
85600 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
85610 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
85620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
85660 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
85670 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e 74 65  contains OS inte
85680 72 66 61 63 65 20 63 6f 64 65 20 74 68 61 74 20  rface code that 
85690 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  is common to all
856a0 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72 65  .** architecture
856b0 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73  s..**.** $Id: os
856c0 2e 63 2c 76 20 31 2e 31 32 35 20 32 30 30 38 2f  .c,v 1.125 2008/
856d0 31 32 2f 30 38 20 31 38 3a 31 39 3a 31 38 20 64  12/08 18:19:18 d
856e0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66  rh Exp $.*/.#def
856f0 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43  ine _SQLITE_OS_C
85700 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49  _ 1.#undef _SQLI
85710 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20  TE_OS_C_../*.** 
85720 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c 69  The default SQLi
85730 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 69  te sqlite3_vfs i
85740 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64  mplementations d
85750 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a  o not allocate.*
85760 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61 6c  * memory (actual
85770 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c  ly, os_unix.c al
85780 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c 20  locates a small 
85790 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
857a0 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  .** from within 
857b0 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20 73  OsOpen()), but s
857c0 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79 20  ome third-party 
857d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
857e0 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65  may..** So we te
857f0 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20 6f  st the effects o
85800 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
85810 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71 6c  ling and the sql
85820 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66  ite3OsXXX().** f
85830 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e  unction returnin
85840 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  g SQLITE_IOERR_N
85850 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20 44  OMEM using the D
85860 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
85870 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68   macro..**.** Th
85880 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63  e following func
85890 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72 75  tions are instru
858a0 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f  mented for mallo
858b0 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20  c() failure .** 
858c0 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  testing:.**.**  
858d0 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e     sqlite3OsOpen
858e0 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
858f0 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 20  3OsRead().**    
85900 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
85910 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
85920 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20  OsSync().**     
85930 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a  sqlite3OsLock().
85940 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  **.*/.#if define
85950 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26  d(SQLITE_TEST) &
85960 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  & (SQLITE_OS_WIN
85970 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20 44  ==0).  #define D
85980 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
85990 20 69 66 20 28 31 29 20 7b 20 20 20 20 20 20 20   if (1) {       
859a0 20 20 20 20 20 5c 0a 20 20 20 20 76 6f 69 64 20       \.    void 
859b0 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c  *pTstAlloc = sql
859c0 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20  ite3Malloc(10); 
859d0 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 20 28        \.    if (
859e0 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 75  !pTstAlloc) retu
859f0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
85a00 4e 4f 4d 45 4d 3b 20 20 5c 0a 20 20 20 20 73 71  NOMEM;  \.    sq
85a10 6c 69 74 65 33 5f 66 72 65 65 28 70 54 73 74 41  lite3_free(pTstA
85a20 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 20 20 20  lloc);          
85a30 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a            \.  }.
85a40 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
85a50 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
85a60 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  T.#endif../*.** 
85a70 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
85a80 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65  utines are conve
85a90 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20  nience wrappers 
85aa0 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a  around methods.*
85ab0 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  * of the sqlite3
85ac0 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54  _file object.  T
85ad0 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75  his is mostly ju
85ae0 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 75 67  st syntactic sug
85af0 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68  ar. All.** of th
85b00 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70  is would be comp
85b10 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 69 63  letely automatic
85b20 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 65 20   if SQLite were 
85b30 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43  coded using.** C
85b40 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 70 6c  ++ instead of pl
85b50 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51  ain old C..*/.SQ
85b60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
85b70 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
85b80 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49  sqlite3_file *pI
85b90 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  d){.  int rc = S
85ba0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
85bb0 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b  pId->pMethods ){
85bc0 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d 3e 70  .    rc = pId->p
85bd0 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28  Methods->xClose(
85be0 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70  pId);.    pId->p
85bf0 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d  Methods = 0;.  }
85c00 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
85c10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
85c20 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64  nt sqlite3OsRead
85c30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
85c40 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69  d, void *pBuf, i
85c50 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73  nt amt, i64 offs
85c60 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  et){.  DO_OS_MAL
85c70 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75  LOC_TEST;.  retu
85c80 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
85c90 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 2c  >xRead(id, pBuf,
85ca0 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d   amt, offset);.}
85cb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
85cc0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69  int sqlite3OsWri
85cd0 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
85ce0 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  *id, const void 
85cf0 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20  *pBuf, int amt, 
85d00 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44  i64 offset){.  D
85d10 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
85d20 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  ;.  return id->p
85d30 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 74 65 28  Methods->xWrite(
85d40 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f  id, pBuf, amt, o
85d50 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45  ffset);.}.SQLITE
85d60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
85d70 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73  ite3OsTruncate(s
85d80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
85d90 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 20 72 65   i64 size){.  re
85da0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
85db0 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 69 64 2c  s->xTruncate(id,
85dc0 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45   size);.}.SQLITE
85dd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
85de0 69 74 65 33 4f 73 53 79 6e 63 28 73 71 6c 69 74  ite3OsSync(sqlit
85df0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
85e00 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f 5f 4f 53   flags){.  DO_OS
85e10 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20  _MALLOC_TEST;.  
85e20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
85e30 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66  ods->xSync(id, f
85e40 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  lags);.}.SQLITE_
85e50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
85e60 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71  te3OsFileSize(sq
85e70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
85e80 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44  i64 *pSize){.  D
85e90 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
85ea0 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  ;.  return id->p
85eb0 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69  Methods->xFileSi
85ec0 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d  ze(id, pSize);.}
85ed0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
85ee0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  int sqlite3OsLoc
85ef0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
85f00 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65  id, int lockType
85f10 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f  ){.  DO_OS_MALLO
85f20 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 6e  C_TEST;.  return
85f30 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
85f40 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70  Lock(id, lockTyp
85f50 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  e);.}.SQLITE_PRI
85f60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
85f70 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  OsUnlock(sqlite3
85f80 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
85f90 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 75  ockType){.  retu
85fa0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
85fb0 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63  >xUnlock(id, loc
85fc0 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45  kType);.}.SQLITE
85fd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
85fe0 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72  ite3OsCheckReser
85ff0 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
86000 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
86010 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53  ResOut){.  DO_OS
86020 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20  _MALLOC_TEST;.  
86030 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
86040 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65 73 65 72  ods->xCheckReser
86050 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70 52 65 73  vedLock(id, pRes
86060 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  Out);.}.SQLITE_P
86070 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
86080 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  e3OsFileControl(
86090 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
860a0 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a  , int op, void *
860b0 70 41 72 67 29 7b 0a 20 20 72 65 74 75 72 6e 20  pArg){.  return 
860c0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46  id->pMethods->xF
860d0 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f  ileControl(id, o
860e0 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49  p, pArg);.}.SQLI
860f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
86100 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69  qlite3OsSectorSi
86110 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ze(sqlite3_file 
86120 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53  *id){.  int (*xS
86130 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74  ectorSize)(sqlit
86140 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 64 2d 3e  e3_file*) = id->
86150 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 63 74 6f  pMethods->xSecto
86160 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20  rSize;.  return 
86170 28 78 53 65 63 74 6f 72 53 69 7a 65 20 3f 20 78  (xSectorSize ? x
86180 53 65 63 74 6f 72 53 69 7a 65 28 69 64 29 20 3a  SectorSize(id) :
86190 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
861a0 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a  SECTOR_SIZE);.}.
861b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
861c0 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69  nt sqlite3OsDevi
861d0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
861e0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
861f0 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64  id){.  return id
86200 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 44 65 76  ->pMethods->xDev
86210 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
86220 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  cs(id);.}../*.**
86230 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20   The next group 
86240 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  of routines are 
86250 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70  convenience wrap
86260 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68 65 0a  pers around the.
86270 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a  ** VFS methods..
86280 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
86290 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f  E int sqlite3OsO
862a0 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  pen(.  sqlite3_v
862b0 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e  fs *pVfs, .  con
862c0 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
862d0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
862e0 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66  *pFile, .  int f
862f0 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70 46  lags, .  int *pF
86300 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f  lagsOut.){.  DO_
86310 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a  OS_MALLOC_TEST;.
86320 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78    return pVfs->x
86330 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68  Open(pVfs, zPath
86340 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20  , pFile, flags, 
86350 70 46 6c 61 67 73 4f 75 74 29 3b 0a 7d 0a 53 51  pFlagsOut);.}.SQ
86360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
86370 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
86380 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
86390 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  fs, const char *
863a0 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 79  zPath, int dirSy
863b0 6e 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  nc){.  return pV
863c0 66 73 2d 3e 78 44 65 6c 65 74 65 28 70 56 66 73  fs->xDelete(pVfs
863d0 2c 20 7a 50 61 74 68 2c 20 64 69 72 53 79 6e 63  , zPath, dirSync
863e0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
863f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
86400 73 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74  sAccess(.  sqlit
86410 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20  e3_vfs *pVfs, . 
86420 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
86430 74 68 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73  th, .  int flags
86440 2c 20 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75  , .  int *pResOu
86450 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  t.){.  DO_OS_MAL
86460 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75  LOC_TEST;.  retu
86470 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73  rn pVfs->xAccess
86480 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c  (pVfs, zPath, fl
86490 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d  ags, pResOut);.}
864a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
864b0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c  int sqlite3OsFul
864c0 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c  lPathname(.  sql
864d0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
864e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
864f0 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61  Path, .  int nPa
86500 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a  thOut, .  char *
86510 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20 72 65  zPathOut.){.  re
86520 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 75 6c 6c  turn pVfs->xFull
86530 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a  Pathname(pVfs, z
86540 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 74 2c 20  Path, nPathOut, 
86550 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 23 69 66  zPathOut);.}.#if
86560 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
86570 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
86580 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
86590 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c  oid *sqlite3OsDl
865a0 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73  Open(sqlite3_vfs
865b0 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68   *pVfs, const ch
865c0 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 20 72 65  ar *zPath){.  re
865d0 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c 4f 70  turn pVfs->xDlOp
865e0 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 29 3b  en(pVfs, zPath);
865f0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
86600 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73  E void sqlite3Os
86610 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  DlError(sqlite3_
86620 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
86630 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66  Byte, char *zBuf
86640 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44  Out){.  pVfs->xD
86650 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e 42 79  lError(pVfs, nBy
86660 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a  te, zBufOut);.}.
86670 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73  void (*sqlite3Os
86680 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66  DlSym(sqlite3_vf
86690 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
866a0 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  Hdle, const char
866b0 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b 0a   *zSym))(void){.
866c0 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78    return pVfs->x
866d0 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48 64 6c  DlSym(pVfs, pHdl
866e0 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51 4c 49  e, zSym);.}.SQLI
866f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
86700 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65  sqlite3OsDlClose
86710 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
86720 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c  fs, void *pHandl
86730 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43  e){.  pVfs->xDlC
86740 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61 6e 64  lose(pVfs, pHand
86750 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  le);.}.#endif /*
86760 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
86770 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53  D_EXTENSION */.S
86780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
86790 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f  t sqlite3OsRando
867a0 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66  mness(sqlite3_vf
867b0 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79  s *pVfs, int nBy
867c0 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75  te, char *zBufOu
867d0 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66  t){.  return pVf
867e0 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 28 70  s->xRandomness(p
867f0 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66  Vfs, nByte, zBuf
86800 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  Out);.}.SQLITE_P
86810 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
86820 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65  e3OsSleep(sqlite
86830 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
86840 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65 74 75   nMicro){.  retu
86850 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65 70 28  rn pVfs->xSleep(
86860 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d  pVfs, nMicro);.}
86870 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
86880 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72  int sqlite3OsCur
86890 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33  rentTime(sqlite3
868a0 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62  _vfs *pVfs, doub
868b0 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b 0a 20  le *pTimeOut){. 
868c0 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 43   return pVfs->xC
868d0 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66 73 2c  urrentTime(pVfs,
868e0 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a 53   pTimeOut);.}..S
868f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
86900 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d  t sqlite3OsOpenM
86910 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33  alloc(.  sqlite3
86920 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63  _vfs *pVfs, .  c
86930 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
86940 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  , .  sqlite3_fil
86950 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 20 69  e **ppFile, .  i
86960 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20  nt flags,.  int 
86970 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20  *pOutFlags.){.  
86980 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
86990 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33  NOMEM;.  sqlite3
869a0 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20  _file *pFile;.  
869b0 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74 65 33  pFile = (sqlite3
869c0 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d  _file *)sqlite3M
869d0 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73  alloc(pVfs->szOs
869e0 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69  File);.  if( pFi
869f0 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  le ){.    rc = s
86a00 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
86a10 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c 65 2c  s, zFile, pFile,
86a20 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 67   flags, pOutFlag
86a30 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  s);.    if( rc!=
86a40 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
86a50 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
86a60 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73  pFile);.    }els
86a70 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69 6c 65  e{.      *ppFile
86a80 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20 7d 0a   = pFile;.    }.
86a90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
86aa0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
86ab0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
86ac0 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 33  loseFree(sqlite3
86ad0 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  _file *pFile){. 
86ae0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
86af0 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70  _OK;.  assert( p
86b00 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d 20 73  File );.  rc = s
86b10 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 46  qlite3OsClose(pF
86b20 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ile);.  sqlite3_
86b30 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 72  free(pFile);.  r
86b40 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
86b50 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20 61  ** The list of a
86b60 6c 6c 20 72 65 67 69 73 74 65 72 65 64 20 56 46  ll registered VF
86b70 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  S implementation
86b80 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c  s..*/.static sql
86b90 69 74 65 33 5f 76 66 73 20 2a 20 53 51 4c 49 54  ite3_vfs * SQLIT
86ba0 45 5f 57 53 44 20 76 66 73 4c 69 73 74 20 3d 20  E_WSD vfsList = 
86bb0 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 73 4c 69  0;.#define vfsLi
86bc0 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65  st GLOBAL(sqlite
86bd0 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c 69 73 74  3_vfs *, vfsList
86be0 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20  )../*.** Locate 
86bf0 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 20  a VFS by name.  
86c00 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 69  If no name is gi
86c10 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 75  ven, simply retu
86c20 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  rn the.** first 
86c30 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e  VFS on the list.
86c40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
86c50 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
86c60 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e  te3_vfs_find(con
86c70 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29 7b 0a  st char *zVfs){.
86c80 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
86c90 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c  Vfs = 0;.#if SQL
86ca0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20  ITE_THREADSAFE. 
86cb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
86cc0 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 23 69  mutex;.#endif.#i
86cd0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
86ce0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74  T_AUTOINIT.  int
86cf0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e   rc = sqlite3_in
86d00 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66  itialize();.  if
86d10 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 30 3b  ( rc ) return 0;
86d20 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
86d30 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20  TE_THREADSAFE.  
86d40 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
86d50 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
86d60 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
86d70 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20  STER);.#endif.  
86d80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
86d90 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f  ter(mutex);.  fo
86da0 72 28 70 56 66 73 20 3d 20 76 66 73 4c 69 73 74  r(pVfs = vfsList
86db0 3b 20 70 56 66 73 3b 20 70 56 66 73 3d 70 56 66  ; pVfs; pVfs=pVf
86dc0 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  s->pNext){.    i
86dd0 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 62 72 65  f( zVfs==0 ) bre
86de0 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 74 72 63  ak;.    if( strc
86df0 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 2d 3e 7a  mp(zVfs, pVfs->z
86e00 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b  Name)==0 ) break
86e10 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
86e20 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
86e30 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66  x);.  return pVf
86e40 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69  s;.}../*.** Unli
86e50 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d 20 74 68  nk a VFS from th
86e60 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 2a 2f  e linked list.*/
86e70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 66 73  .static void vfs
86e80 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 33 5f 76  Unlink(sqlite3_v
86e90 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 61 73 73  fs *pVfs){.  ass
86ea0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
86eb0 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d  ex_held(sqlite3M
86ec0 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
86ed0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
86ee0 53 54 45 52 29 29 20 29 3b 0a 20 20 69 66 28 20  STER)) );.  if( 
86ef0 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  pVfs==0 ){.    /
86f00 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d 65 6c  * No-op */.  }el
86f10 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 3d 3d  se if( vfsList==
86f20 70 56 66 73 20 29 7b 0a 20 20 20 20 76 66 73 4c  pVfs ){.    vfsL
86f30 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78  ist = pVfs->pNex
86f40 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76  t;.  }else if( v
86f50 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 71  fsList ){.    sq
86f60 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d 20 76  lite3_vfs *p = v
86f70 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c  fsList;.    whil
86f80 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26 20 70  e( p->pNext && p
86f90 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20 29 7b  ->pNext!=pVfs ){
86fa0 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e  .      p = p->pN
86fb0 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ext;.    }.    i
86fc0 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 56 66  f( p->pNext==pVf
86fd0 73 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4e  s ){.      p->pN
86fe0 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78  ext = pVfs->pNex
86ff0 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  t;.    }.  }.}..
87000 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
87010 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73 79   VFS with the sy
87020 73 74 65 6d 2e 20 20 49 74 20 69 73 20 68 61 72  stem.  It is har
87030 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 73 74 65  mless to registe
87040 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 56 46  r the same.** VF
87050 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  S multiple times
87060 2e 20 20 54 68 65 20 6e 65 77 20 56 46 53 20 62  .  The new VFS b
87070 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 61 75  ecomes the defau
87080 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c 74 20 69  lt if makeDflt i
87090 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 53 51  s.** true..*/.SQ
870a0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
870b0 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
870c0 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  r(sqlite3_vfs *p
870d0 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c  Vfs, int makeDfl
870e0 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  t){.  sqlite3_mu
870f0 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 30 3b 0a  tex *mutex = 0;.
87100 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
87110 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69  MIT_AUTOINIT.  i
87120 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  nt rc = sqlite3_
87130 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20  initialize();.  
87140 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
87150 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 75 74  rc;.#endif.  mut
87160 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
87170 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
87180 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
87190 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  R);.  sqlite3_mu
871a0 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
871b0 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56  ;.  vfsUnlink(pV
871c0 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65 44  fs);.  if( makeD
871d0 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d 3d  flt || vfsList==
871e0 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70  0 ){.    pVfs->p
871f0 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 3b 0a  Next = vfsList;.
87200 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56      vfsList = pV
87210 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  fs;.  }else{.   
87220 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76   pVfs->pNext = v
87230 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20  fsList->pNext;. 
87240 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78     vfsList->pNex
87250 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20 20  t = pVfs;.  }.  
87260 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 29 3b  assert(vfsList);
87270 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
87280 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20  _leave(mutex);. 
87290 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
872a0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 65  K;.}../*.** Unre
872b0 67 69 73 74 65 72 20 61 20 56 46 53 20 73 6f 20  gister a VFS so 
872c0 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20 6c 6f  that it is no lo
872d0 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c 65 2e  nger accessible.
872e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
872f0 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75  nt sqlite3_vfs_u
87300 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65  nregister(sqlite
87310 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 69  3_vfs *pVfs){.#i
87320 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
87330 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  AFE.  sqlite3_mu
87340 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c  tex *mutex = sql
87350 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
87360 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
87370 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64  IC_MASTER);.#end
87380 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  if.  sqlite3_mut
87390 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b  ex_enter(mutex);
873a0 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66  .  vfsUnlink(pVf
873b0 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  s);.  sqlite3_mu
873c0 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
873d0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
873e0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  E_OK;.}../******
873f0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
87400 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os.c ***********
87410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87430 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
87440 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
87450 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a  ile fault.c ****
87460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87480 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
87490 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2a  008 Jan 22.**.**
874a0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
874b0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
874c0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
874d0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
874e0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
874f0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
87500 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
87510 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
87520 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
87530 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
87540 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
87550 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
87560 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
87570 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
87580 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
87590 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
875a0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
875b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
875c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
875d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
875e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
875f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
87600 24 49 64 3a 20 66 61 75 6c 74 2e 63 2c 76 20 31  $Id: fault.c,v 1
87610 2e 31 31 20 32 30 30 38 2f 30 39 2f 30 32 20 30  .11 2008/09/02 0
87620 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 70 20  0:52:52 drh Exp 
87630 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  $.*/../*.** This
87640 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
87650 6f 64 65 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ode to support t
87660 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 22 62  he concept of "b
87670 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c 6c 6f  enign" .** mallo
87680 63 20 66 61 69 6c 75 72 65 73 20 28 77 68 65 6e  c failures (when
87690 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 6f   the xMalloc() o
876a0 72 20 78 52 65 61 6c 6c 6f 63 28 29 20 6d 65 74  r xRealloc() met
876b0 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 73 71  hod of the.** sq
876c0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
876d0 73 20 73 74 72 75 63 74 75 72 65 20 66 61 69 6c  s structure fail
876e0 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  s to allocate a 
876f0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a  block of memory.
87700 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30  ** and returns 0
87710 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6d  ). .**.** Most m
87720 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61  alloc failures a
87730 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 20 41  re non-benign. A
87740 66 74 65 72 20 74 68 65 79 20 6f 63 63 75 72 2c  fter they occur,
87750 20 53 51 4c 69 74 65 0a 2a 2a 20 61 62 61 6e 64   SQLite.** aband
87760 6f 6e 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  ons the current 
87770 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 72 65  operation and re
87780 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63  turns an error c
87790 6f 64 65 20 28 75 73 75 61 6c 6c 79 0a 2a 2a 20  ode (usually.** 
877a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20 74 6f  SQLITE_NOMEM) to
877b0 20 74 68 65 20 75 73 65 72 2e 20 48 6f 77 65 76   the user. Howev
877c0 65 72 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 20  er, sometimes a 
877d0 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 6e 65 63  fault is not nec
877e0 65 73 73 61 72 69 6c 79 0a 2a 2a 20 66 61 74 61  essarily.** fata
877f0 6c 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  l. For example, 
87800 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  if a malloc fail
87810 73 20 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67  s while resizing
87820 20 61 20 68 61 73 68 20 74 61 62 6c 65 2c 20 74   a hash table, t
87830 68 69 73 20 0a 2a 2a 20 69 73 20 63 6f 6d 70 6c  his .** is compl
87840 65 74 65 6c 79 20 72 65 63 6f 76 65 72 61 62 6c  etely recoverabl
87850 65 20 73 69 6d 70 6c 79 20 62 79 20 6e 6f 74 20  e simply by not 
87860 63 61 72 72 79 69 6e 67 20 6f 75 74 20 74 68 65  carrying out the
87870 20 72 65 73 69 7a 65 2e 20 54 68 65 20 0a 2a 2a   resize. The .**
87880 20 68 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c   hash table will
87890 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 66 75 6e   continue to fun
878a0 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79 2e 20  ction normally. 
878b0 20 53 6f 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   So a malloc fai
878c0 6c 75 72 65 20 0a 2a 2a 20 64 75 72 69 6e 67 20  lure .** during 
878d0 61 20 68 61 73 68 20 74 61 62 6c 65 20 72 65 73  a hash table res
878e0 69 7a 65 20 69 73 20 61 20 62 65 6e 69 67 6e 20  ize is a benign 
878f0 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69 66 6e  fault..*/...#ifn
87900 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
87910 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 0a 2f 2a  BUILTIN_TEST../*
87920 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61  .** Global varia
87930 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  bles..*/.typedef
87940 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61   struct BenignMa
87950 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65 6e 69 67 6e  llocHooks Benign
87960 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73 74 61  MallocHooks;.sta
87970 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
87980 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c  truct BenignMall
87990 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f 69 64  ocHooks {.  void
879a0 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29   (*xBenignBegin)
879b0 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 28  (void);.  void (
879c0 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69  *xBenignEnd)(voi
879d0 64 29 3b 0a 7d 20 73 71 6c 69 74 65 33 48 6f 6f  d);.} sqlite3Hoo
879e0 6b 73 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a  ks = { 0, 0 };..
879f0 2f 2a 20 54 68 65 20 22 77 73 64 48 6f 6f 6b 73  /* The "wsdHooks
87a00 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73  " macro will res
87a10 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 70 70 72  olve to the appr
87a20 6f 70 72 69 61 74 65 20 42 65 6e 69 67 6e 4d 61  opriate BenignMa
87a30 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73 74 72  llocHooks.** str
87a40 75 63 74 75 72 65 2e 20 20 49 66 20 77 72 69 74  ucture.  If writ
87a50 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
87a60 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20   is unsupported 
87a70 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a  on the target,.*
87a80 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63  * we have to loc
87a90 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65  ate the state ve
87aa0 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65  ctor at run-time
87ab0 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63  .  In the more c
87ac0 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68  ommon.** case wh
87ad0 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61  ere writable sta
87ae0 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70  tic data is supp
87af0 6f 72 74 65 64 2c 20 77 73 64 48 6f 6f 6b 73 20  orted, wsdHooks 
87b00 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74  can refer direct
87b10 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71  ly.** to the "sq
87b20 6c 69 74 65 33 48 6f 6f 6b 73 22 20 73 74 61 74  lite3Hooks" stat
87b30 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65  e vector declare
87b40 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64  d above..*/.#ifd
87b50 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  ef SQLITE_OMIT_W
87b60 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48  SD.# define wsdH
87b70 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20 20 42 65 6e  ooksInit \.  Ben
87b80 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 2a  ignMallocHooks *
87b90 78 20 3d 20 26 47 4c 4f 42 41 4c 28 42 65 6e 69  x = &GLOBAL(Beni
87ba0 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c 73 71  gnMallocHooks,sq
87bb0 6c 69 74 65 33 48 6f 6f 6b 73 29 0a 23 20 64 65  lite3Hooks).# de
87bc0 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20 78 5b  fine wsdHooks x[
87bd0 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  0].#else.# defin
87be0 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 0a 23  e wsdHooksInit.#
87bf0 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73   define wsdHooks
87c00 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 0a 23 65   sqlite3Hooks.#e
87c10 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  ndif.../*.** Reg
87c20 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63  ister hooks to c
87c30 61 6c 6c 20 77 68 65 6e 20 73 71 6c 69 74 65 33  all when sqlite3
87c40 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
87c50 63 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74  c() and.** sqlit
87c60 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
87c70 63 28 29 20 61 72 65 20 63 61 6c 6c 65 64 2c 20  c() are called, 
87c80 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f  respectively..*/
87c90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
87ca0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 6e 69  void sqlite3Beni
87cb0 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 0a 20  gnMallocHooks(. 
87cc0 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42   void (*xBenignB
87cd0 65 67 69 6e 29 28 76 6f 69 64 29 2c 0a 20 20 76  egin)(void),.  v
87ce0 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64  oid (*xBenignEnd
87cf0 29 28 76 6f 69 64 29 0a 29 7b 0a 20 20 77 73 64  )(void).){.  wsd
87d00 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 77 73 64  HooksInit;.  wsd
87d10 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67  Hooks.xBenignBeg
87d20 69 6e 20 3d 20 78 42 65 6e 69 67 6e 42 65 67 69  in = xBenignBegi
87d30 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42  n;.  wsdHooks.xB
87d40 65 6e 69 67 6e 45 6e 64 20 3d 20 78 42 65 6e 69  enignEnd = xBeni
87d50 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  gnEnd;.}../*.** 
87d60 54 68 69 73 20 28 73 71 6c 69 74 65 33 45 6e 64  This (sqlite3End
87d70 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 29 20  BenignMalloc()) 
87d80 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c  is called by SQL
87d90 69 74 65 20 63 6f 64 65 20 74 6f 20 69 6e 64 69  ite code to indi
87da0 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 73 75 62  cate that.** sub
87db0 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66  sequent malloc f
87dc0 61 69 6c 75 72 65 73 20 61 72 65 20 62 65 6e 69  ailures are beni
87dd0 67 6e 2e 20 41 20 63 61 6c 6c 20 74 6f 20 73 71  gn. A call to sq
87de0 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
87df0 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e 64 69 63 61  lloc().** indica
87e00 74 65 73 20 74 68 61 74 20 73 75 62 73 65 71 75  tes that subsequ
87e10 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  ent malloc failu
87e20 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69  res are non-beni
87e30 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  gn..*/.SQLITE_PR
87e40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
87e50 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
87e60 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64  loc(void){.  wsd
87e70 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28  HooksInit;.  if(
87e80 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67   wsdHooks.xBenig
87e90 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 77 73  nBegin ){.    ws
87ea0 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65  dHooks.xBenignBe
87eb0 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c  gin();.  }.}.SQL
87ec0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
87ed0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
87ee0 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20  nMalloc(void){. 
87ef0 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20   wsdHooksInit;. 
87f00 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42   if( wsdHooks.xB
87f10 65 6e 69 67 6e 45 6e 64 20 29 7b 0a 20 20 20 20  enignEnd ){.    
87f20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e  wsdHooks.xBenign
87f30 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65  End();.  }.}..#e
87f40 6e 64 69 66 20 20 20 2f 2a 20 23 69 66 6e 64 65  ndif   /* #ifnde
87f50 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
87f60 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f  ILTIN_TEST */../
87f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
87f80 6e 64 20 6f 66 20 66 61 75 6c 74 2e 63 20 2a 2a  nd of fault.c **
87f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
87fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
87fd0 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 30 2e 63  egin file mem0.c
87fe0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
87ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
88010 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65  *.** 2008 Octobe
88020 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 28.**.** The a
88030 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
88040 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
88050 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
88060 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
88070 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
88080 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
88090 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
880a0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
880b0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
880c0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
880d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
880e0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
880f0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
88100 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
88110 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
88120 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
88130 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
88140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88180 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
88190 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 6e  ile contains a n
881a0 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  o-op memory allo
881b0 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66  cation drivers f
881c0 6f 72 20 75 73 65 20 77 68 65 6e 0a 2a 2a 20 53  or use when.** S
881d0 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f  QLITE_ZERO_MALLO
881e0 43 20 69 73 20 64 65 66 69 6e 65 64 2e 20 20 54  C is defined.  T
881f0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72  he allocation dr
88200 69 76 65 72 73 20 69 6d 70 6c 65 6d 65 6e 74 65  ivers implemente
88210 64 0a 2a 2a 20 68 65 72 65 20 61 6c 77 61 79 73  d.** here always
88220 20 66 61 69 6c 2e 20 20 53 51 4c 69 74 65 20 77   fail.  SQLite w
88230 69 6c 6c 20 6e 6f 74 20 6f 70 65 72 61 74 65 20  ill not operate 
88240 77 69 74 68 20 74 68 65 73 65 20 64 72 69 76 65  with these drive
88250 72 73 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72  rs.  These.** ar
88260 65 20 6d 65 72 65 6c 79 20 70 6c 61 63 65 68 6f  e merely placeho
88270 6c 64 65 72 73 2e 20 20 52 65 61 6c 20 64 72 69  lders.  Real dri
88280 76 65 72 73 20 6d 75 73 74 20 62 65 20 73 75 62  vers must be sub
88290 73 74 69 74 75 74 65 64 20 75 73 69 6e 67 0a 2a  stituted using.*
882a0 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  * sqlite3_config
882b0 28 29 20 62 65 66 6f 72 65 20 53 51 4c 69 74 65  () before SQLite
882c0 20 77 69 6c 6c 20 6f 70 65 72 61 74 65 2e 0a 2a   will operate..*
882d0 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 30 2e 63  *.** $Id: mem0.c
882e0 2c 76 20 31 2e 31 20 32 30 30 38 2f 31 30 2f 32  ,v 1.1 2008/10/2
882f0 38 20 31 38 3a 35 38 3a 32 30 20 64 72 68 20 45  8 18:58:20 drh E
88300 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
88310 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
88320 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
88330 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 75  tor is the defau
88340 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73  lt.  It is.** us
88350 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72  ed when no other
88360 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
88370 72 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75  r is specified u
88380 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  sing compile-tim
88390 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  e.** macros..*/.
883a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45  #ifdef SQLITE_ZE
883b0 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a  RO_MALLOC../*.**
883c0 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20   No-op versions 
883d0 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c  of all memory al
883e0 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  location routine
883f0 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s.*/.static void
88400 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c   *sqlite3MemMall
88410 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72  oc(int nByte){ r
88420 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69  eturn 0; }.stati
88430 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  c void sqlite3Me
88440 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69  mFree(void *pPri
88450 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73  or){ return; }.s
88460 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69  tatic void *sqli
88470 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f  te3MemRealloc(vo
88480 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20  id *pPrior, int 
88490 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30  nByte){ return 0
884a0 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  ; }.static int s
884b0 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f  qlite3MemSize(vo
884c0 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74  id *pPrior){ ret
884d0 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20  urn 0; }.static 
884e0 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f  int sqlite3MemRo
884f0 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65  undup(int n){ re
88500 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63  turn n; }.static
88510 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49   int sqlite3MemI
88520 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  nit(void *NotUse
88530 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
88540 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76  E_OK; }.static v
88550 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68  oid sqlite3MemSh
88560 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74  utdown(void *Not
88570 55 73 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d  Used){ return; }
88580 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
88590 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79  tine is the only
885a0 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73   routine in this
885b0 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72   file with exter
885c0 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a  nal linkage..**.
885d0 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  ** Populate the 
885e0 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
885f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63   allocation func
88600 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e  tion pointers in
88610 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  .** sqlite3Globa
88620 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70  lConfig.m with p
88630 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72  ointers to the r
88640 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20  outines in this 
88650 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  file..*/.SQLITE_
88660 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
88670 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c  ite3MemSetDefaul
88680 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  t(void){.  stati
88690 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
886a0 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61  mem_methods defa
886b0 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20  ultMethods = {. 
886c0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61      sqlite3MemMa
886d0 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74  lloc,.     sqlit
886e0 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20  e3MemFree,.     
886f0 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f  sqlite3MemReallo
88700 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  c,.     sqlite3M
88710 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c  emSize,.     sql
88720 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a  ite3MemRoundup,.
88730 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49       sqlite3MemI
88740 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  nit,.     sqlite
88750 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20  3MemShutdown,.  
88760 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69     0.  };.  sqli
88770 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54  te3_config(SQLIT
88780 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c  E_CONFIG_MALLOC,
88790 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73   &defaultMethods
887a0 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  );.}..#endif /* 
887b0 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c  SQLITE_ZERO_MALL
887c0 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  OC */../********
887d0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
887e0 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m0.c ***********
887f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88810 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
88820 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
88830 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e mem1.c *******
88840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88860 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
88870 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a  7 August 14.**.*
88880 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
88890 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
888a0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
888b0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
888c0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
888d0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
888e0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
888f0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
88900 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
88910 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
88920 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
88930 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
88940 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
88950 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
88960 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
88970 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
88980 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
88990 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
889a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
889b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
889c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
889d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
889e0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
889f0 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  ins low-level me
88a00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
88a10 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e  drivers for when
88a20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
88a30 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64  use the standard
88a40 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f   C-library mallo
88a50 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69  c/realloc/free i
88a60 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f  nterface.** to o
88a70 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79  btain the memory
88a80 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a   it needs..**.**
88a90 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
88aa0 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ins implementati
88ab0 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c  ons of the low-l
88ac0 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  evel memory allo
88ad0 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e  cation.** routin
88ae0 65 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  es specified in 
88af0 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  the sqlite3_mem_
88b00 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a  methods object..
88b10 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 31 2e  **.** $Id: mem1.
88b20 63 2c 76 20 31 2e 32 39 20 32 30 30 38 2f 31 32  c,v 1.29 2008/12
88b30 2f 31 30 20 32 31 3a 31 39 3a 35 37 20 64 72 68  /10 21:19:57 drh
88b40 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
88b50 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
88b60 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
88b70 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66  cator is the def
88b80 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20  ault.  It is.** 
88b90 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68  used when no oth
88ba0 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  er memory alloca
88bb0 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64  tor is specified
88bc0 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74   using compile-t
88bd0 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a  ime.** macros..*
88be0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
88bf0 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f  SYSTEM_MALLOC../
88c00 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f 63  *.** Like malloc
88c10 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 72  (), but remember
88c20 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
88c30 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73   allocation.** s
88c40 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 66 69  o that we can fi
88c50 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73 69 6e  nd it later usin
88c60 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65  g sqlite3MemSize
88c70 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  ()..**.** For th
88c80 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75  is low-level rou
88c90 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67 75 61  tine, we are gua
88ca0 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 42 79  ranteed that nBy
88cb0 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a 20  te>0 because.** 
88cc0 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65 3c 3d  cases of nByte<=
88cd0 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 63  0 will be interc
88ce0 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20  epted and dealt 
88cf0 77 69 74 68 20 62 79 20 68 69 67 68 65 72 20 6c  with by higher l
88d00 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  evel.** routines
88d10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
88d20 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c   *sqlite3MemMall
88d30 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  oc(int nByte){. 
88d40 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
88d50 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79  p;.  assert( nBy
88d60 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20  te>0 );.  nByte 
88d70 3d 20 28 6e 42 79 74 65 2b 37 29 26 7e 37 3b 0a  = (nByte+7)&~7;.
88d80 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42    p = malloc( nB
88d90 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70  yte+8 );.  if( p
88da0 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e   ){.    p[0] = n
88db0 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20  Byte;.    p++;. 
88dc0 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69   }.  return (voi
88dd0 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  d *)p;.}../*.** 
88de0 4c 69 6b 65 20 66 72 65 65 28 29 20 62 75 74 20  Like free() but 
88df0 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f 63 61  works for alloca
88e00 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 20 66  tions obtained f
88e10 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61  rom sqlite3MemMa
88e20 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 71 6c  lloc().** or sql
88e30 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 29  ite3MemRealloc()
88e40 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73  ..**.** For this
88e50 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69   low-level routi
88e60 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b  ne, we already k
88e70 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72 21  now that pPrior!
88e80 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 73 65  =0 since.** case
88e90 73 20 77 68 65 72 65 20 70 50 72 69 6f 72 3d 3d  s where pPrior==
88ea0 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e  0 will have been
88eb0 20 69 6e 74 65 63 65 70 74 65 64 20 61 6e 64 20   intecepted and 
88ec0 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 62 79  dealt with.** by
88ed0 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f   higher-level ro
88ee0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  utines..*/.stati
88ef0 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  c void sqlite3Me
88f00 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69  mFree(void *pPri
88f10 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  or){.  sqlite3_i
88f20 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74  nt64 *p = (sqlit
88f30 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72  e3_int64*)pPrior
88f40 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69  ;.  assert( pPri
88f50 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d 3b 0a  or!=0 );.  p--;.
88f60 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a    free(p);.}../*
88f70 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f 63  .** Like realloc
88f80 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20 61  ().  Resize an a
88f90 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f  llocation previo
88fa0 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72  usly obtained fr
88fb0 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d  om.** sqlite3Mem
88fc0 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20  Malloc()..**.** 
88fd0 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76  For this low-lev
88fe0 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77 65  el interface, we
88ff0 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f   know that pPrio
89000 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68 65  r!=0.  Cases whe
89010 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30 20  re.** pPrior==0 
89020 77 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e 20  while have been 
89030 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 20 68  intercepted by h
89040 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74  igher-level rout
89050 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69 72  ine and.** redir
89060 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f 63  ected to xMalloc
89070 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77 65  .  Similarly, we
89080 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74 65   know that nByte
89090 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20 63  >0 becauses.** c
890a0 61 73 65 73 20 77 68 65 72 65 20 6e 42 79 74 65  ases where nByte
890b0 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65  <=0 will have be
890c0 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62  en intercepted b
890d0 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a 2a  y higher-level.*
890e0 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 72  * routines and r
890f0 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 46 72  edirected to xFr
89100 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ee..*/.static vo
89110 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65  id *sqlite3MemRe
89120 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69  alloc(void *pPri
89130 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a  or, int nByte){.
89140 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
89150 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  *p = (sqlite3_in
89160 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61  t64*)pPrior;.  a
89170 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30  ssert( pPrior!=0
89180 20 26 26 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20   && nByte>0 );. 
89190 20 6e 42 79 74 65 20 3d 20 28 6e 42 79 74 65 2b   nByte = (nByte+
891a0 37 29 26 7e 37 3b 0a 20 20 70 20 3d 20 28 73 71  7)&~7;.  p = (sq
891b0 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72  lite3_int64*)pPr
891c0 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 70 20  ior;.  p--;.  p 
891d0 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 42 79  = realloc(p, nBy
891e0 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20  te+8 );.  if( p 
891f0 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42  ){.    p[0] = nB
89200 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20  yte;.    p++;.  
89210 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  }.  return (void
89220 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  *)p;.}../*.** Re
89230 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 61 74  port the allocat
89240 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 72 69  ed size of a pri
89250 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 78  or return from x
89260 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 78  Malloc().** or x
89270 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74  Realloc()..*/.st
89280 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
89290 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50  MemSize(void *pP
892a0 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  rior){.  sqlite3
892b0 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 69 66 28  _int64 *p;.  if(
892c0 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 72 65 74   pPrior==0 ) ret
892d0 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 28 73 71  urn 0;.  p = (sq
892e0 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72  lite3_int64*)pPr
892f0 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 72 65  ior;.  p--;.  re
89300 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 5d 3b 0a  turn (int)p[0];.
89310 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75  }../*.** Round u
89320 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65  p a request size
89330 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c   to the next val
89340 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69  id allocation si
89350 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ze..*/.static in
89360 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e  t sqlite3MemRoun
89370 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65  dup(int n){.  re
89380 74 75 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37 3b  turn (n+7) & ~7;
89390 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
893a0 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  lize this module
893b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
893c0 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76  sqlite3MemInit(v
893d0 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  oid *NotUsed){. 
893e0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
893f0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65  R(NotUsed);.  re
89400 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
89410 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69  }../*.** Deiniti
89420 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c  alize this modul
89430 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
89440 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74  d sqlite3MemShut
89450 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73  down(void *NotUs
89460 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ed){.  UNUSED_PA
89470 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
89480 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;.  return;.}../
89490 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
894a0 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f  e is the only ro
894b0 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69  utine in this fi
894c0 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c  le with external
894d0 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20   linkage..**.** 
894e0 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77  Populate the low
894f0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
89500 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  location functio
89510 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a  n pointers in.**
89520 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
89530 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e  nfig.m with poin
89540 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74  ters to the rout
89550 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c  ines in this fil
89560 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
89570 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
89580 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76  3MemSetDefault(v
89590 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63  oid){.  static c
895a0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d  onst sqlite3_mem
895b0 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74  _methods default
895c0 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20  Methods = {.    
895d0 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f   sqlite3MemMallo
895e0 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  c,.     sqlite3M
895f0 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 6c  emFree,.     sql
89600 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a  ite3MemRealloc,.
89610 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53       sqlite3MemS
89620 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  ize,.     sqlite
89630 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20  3MemRoundup,.   
89640 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74    sqlite3MemInit
89650 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
89660 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20  mShutdown,.     
89670 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33  0.  };.  sqlite3
89680 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43  _config(SQLITE_C
89690 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64  ONFIG_MALLOC, &d
896a0 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a  efaultMethods);.
896b0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
896c0 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f  ITE_SYSTEM_MALLO
896d0 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  C */../*********
896e0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d  ***** End of mem
896f0 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  1.c ************
89700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89720 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
89730 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
89740 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   mem2.c ********
89750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89770 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
89780 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a 2a   August 15.**.**
89790 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
897a0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
897b0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
897c0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
897d0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
897e0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
897f0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
89800 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
89810 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
89820 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
89830 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
89840 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
89850 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
89860 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
89870 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
89880 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
89890 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
898a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
898b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
898c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
898d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
898e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
898f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
89900 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d  ns low-level mem
89910 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64  ory allocation d
89920 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a  rivers for when.
89930 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  ** SQLite will u
89940 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  se the standard 
89950 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63  C-library malloc
89960 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e  /realloc/free in
89970 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62  terface.** to ob
89980 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20  tain the memory 
89990 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20 61  it needs while a
899a0 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61 64  dding lots of ad
899b0 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67 69  ditional debuggi
899c0 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  ng.** informatio
899d0 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63 61  n to each alloca
899e0 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74 6f  tion in order to
899f0 20 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e 64   help detect and
89a00 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20 6c   fix memory.** l
89a10 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79 20  eaks and memory 
89a20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a 2a  usage errors..**
89a30 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
89a40 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74  ntains implement
89a50 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f  ations of the lo
89a60 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
89a70 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75  llocation.** rou
89a80 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20  tines specified 
89a90 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d  in the sqlite3_m
89aa0 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63  em_methods objec
89ab0 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65  t..**.** $Id: me
89ac0 6d 32 2e 63 2c 76 20 31 2e 34 32 20 32 30 30 38  m2.c,v 1.42 2008
89ad0 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 20  /12/10 19:26:24 
89ae0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
89af0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
89b00 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
89b10 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64  llocator is used
89b20 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20   only if the.** 
89b30 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20  SQLITE_MEMDEBUG 
89b40 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64  macro is defined
89b50 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
89b60 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a 0a 2a  E_MEMDEBUG../*.*
89b70 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63 65 20  * The backtrace 
89b80 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73  functionality is
89b90 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
89ba0 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a 23 69  with GLIBC.*/.#i
89bb0 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f 0a 20  fdef __GLIBC__. 
89bc0 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61 63 6b   extern int back
89bd0 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 6e 74  trace(void**,int
89be0 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f 69 64  );.  extern void
89bf0 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f   backtrace_symbo
89c00 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e 73 74  ls_fd(void*const
89c10 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 6c 73  *,int,int);.#els
89c20 65 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74  e.# define backt
89c30 72 61 63 65 28 41 2c 42 29 20 31 0a 23 20 64 65  race(A,B) 1.# de
89c40 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 5f 73  fine backtrace_s
89c50 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c 43 29  ymbols_fd(A,B,C)
89c60 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45  .#endif../*.** E
89c70 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ach memory alloc
89c80 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c 69 6b 65  ation looks like
89c90 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 2d 2d   this:.**.**  --
89ca0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
89cb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
89cc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
89cd0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
89ce0 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 54 69 74  ------.**  | Tit
89cf0 6c 65 20 7c 20 20 62 61 63 6b 74 72 61 63 65 20  le |  backtrace 
89d00 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d 65 6d 42  pointers |  MemB
89d10 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c 6c 6f 63  lockHdr |  alloc
89d20 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47 75 61 72  ation |  EndGuar
89d30 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d  d |.**  --------
89d40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
89d50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
89d60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
89d70 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
89d80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  .**.** The appli
89d90 63 61 74 69 6f 6e 20 63 6f 64 65 20 73 65 65 73  cation code sees
89da0 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72 20   only a pointer 
89db0 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  to the allocatio
89dc0 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a 2a 20 74  n.  We have.** t
89dd0 6f 20 62 61 63 6b 20 75 70 20 66 72 6f 6d 20 74  o back up from t
89de0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 6f  he allocation po
89df0 69 6e 74 65 72 20 74 6f 20 66 69 6e 64 20 74 68  inter to find th
89e00 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 20 20  e MemBlockHdr.  
89e10 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48  The.** MemBlockH
89e20 64 72 20 74 65 6c 6c 73 20 75 73 20 74 68 65 20  dr tells us the 
89e30 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f  size of the allo
89e40 63 61 74 69 6f 6e 20 61 6e 64 20 74 68 65 20 6e  cation and the n
89e50 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 61 63 6b  umber of.** back
89e60 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 2e 20  trace pointers. 
89e70 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61   There is also a
89e80 20 67 75 61 72 64 20 77 6f 72 64 20 61 74 20 74   guard word at t
89e90 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a  he end of the.**
89ea0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0a 2a 2f   MemBlockHdr..*/
89eb0 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b  .struct MemBlock
89ec0 48 64 72 20 7b 0a 20 20 69 36 34 20 69 53 69 7a  Hdr {.  i64 iSiz
89ed0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
89ee0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
89ef0 69 7a 65 20 6f 66 20 74 68 69 73 20 61 6c 6c 6f  ize of this allo
89f00 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75  cation */.  stru
89f10 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
89f20 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20  pNext, *pPrev;  
89f30 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f  /* Linked list o
89f40 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65  f all unfreed me
89f50 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61 72 20 6e  mory */.  char n
89f60 42 61 63 6b 74 72 61 63 65 3b 20 20 20 20 20 20  Backtrace;      
89f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
89f80 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 74   Number of backt
89f90 72 61 63 65 73 20 6f 6e 20 74 68 69 73 20 61 6c  races on this al
89fa0 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 6e 42  loc */.  char nB
89fb0 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 20 20  acktraceSlots;  
89fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
89fd0 41 76 61 69 6c 61 62 6c 65 20 62 61 63 6b 74 72  Available backtr
89fe0 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a 20 20 73  ace slots */.  s
89ff0 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20 20 20 20  hort nTitle;    
8a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8a010 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 74     /* Bytes of t
8a020 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65 73 20 27  itle; includes '
8a030 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20 69 46 6f  \0' */.  int iFo
8a040 72 65 47 75 61 72 64 3b 20 20 20 20 20 20 20 20  reGuard;        
8a050 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
8a060 47 75 61 72 64 20 77 6f 72 64 20 66 6f 72 20 73  Guard word for s
8a070 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  anity */.};../*.
8a080 2a 2a 20 47 75 61 72 64 20 77 6f 72 64 73 0a 2a  ** Guard words.*
8a090 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52 45 47 55  /.#define FOREGU
8a0a0 41 52 44 20 30 78 38 30 46 35 45 31 35 33 0a 23  ARD 0x80F5E153.#
8a0b0 64 65 66 69 6e 65 20 52 45 41 52 47 55 41 52 44  define REARGUARD
8a0c0 20 30 78 45 34 36 37 36 42 35 33 0a 0a 2f 2a 0a   0xE4676B53../*.
8a0d0 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 6c  ** Number of mal
8a0e0 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72 65 6d 65  loc size increme
8a0f0 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e 0a 2a 2f  nts to track..*/
8a100 0a 23 64 65 66 69 6e 65 20 4e 43 53 49 5a 45 20  .#define NCSIZE 
8a110 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c   1000../*.** All
8a120 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 76   of the static v
8a130 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79  ariables used by
8a140 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65   this module are
8a150 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e   collected.** in
8a160 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75  to a single stru
8a170 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d  cture named "mem
8a180 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b  ".  This is to k
8a190 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69  eep the.** stati
8a1a0 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61  c variables orga
8a1b0 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64  nized and to red
8a1c0 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f  uce namespace po
8a1d0 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20  llution.** when 
8a1e0 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63  this module is c
8a1f0 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68  ombined with oth
8a200 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61  er in the amalga
8a210 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  mation..*/.stati
8a220 63 20 73 74 72 75 63 74 20 7b 0a 20 20 0a 20 20  c struct {.  .  
8a230 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f  /*.  ** Mutex to
8a240 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20   control access 
8a250 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  to the memory al
8a260 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
8a270 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  em..  */.  sqlit
8a280 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
8a290 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 65 61 64  ..  /*.  ** Head
8a2a0 20 61 6e 64 20 74 61 69 6c 20 6f 66 20 61 20 6c   and tail of a l
8a2b0 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c  inked list of al
8a2c0 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c  l outstanding al
8a2d0 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2f 0a 20  locations.  */. 
8a2e0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
8a2f0 48 64 72 20 2a 70 46 69 72 73 74 3b 0a 20 20 73  Hdr *pFirst;.  s
8a300 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
8a310 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a 20 20 2f  r *pLast;.  .  /
8a320 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75 6d 62 65  *.  ** The numbe
8a330 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 62  r of levels of b
8a340 61 63 6b 74 72 61 63 65 20 74 6f 20 73 61 76 65  acktrace to save
8a350 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69   in new allocati
8a360 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ons..  */.  int 
8a370 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 76 6f  nBacktrace;.  vo
8a380 69 64 20 28 2a 78 42 61 63 6b 74 72 61 63 65 29  id (*xBacktrace)
8a390 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20  (int, int, void 
8a3a0 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  **);..  /*.  ** 
8a3b0 54 69 74 6c 65 20 74 65 78 74 20 74 6f 20 69 6e  Title text to in
8a3c0 73 65 72 74 20 69 6e 20 66 72 6f 6e 74 20 6f 66  sert in front of
8a3d0 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20 20 2a 2f   each block.  */
8a3e0 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65 3b 20 20  .  int nTitle;  
8a3f0 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
8a400 66 20 7a 54 69 74 6c 65 20 74 6f 20 73 61 76 65  f zTitle to save
8a410 2e 20 20 49 6e 63 6c 75 64 65 73 20 27 5c 30 27  .  Includes '\0'
8a420 20 61 6e 64 20 70 61 64 64 69 6e 67 20 2a 2f 0a   and padding */.
8a430 20 20 63 68 61 72 20 7a 54 69 74 6c 65 5b 31 30    char zTitle[10
8a440 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74 69 74 6c  0];  /* The titl
8a450 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20 2f 2a 20  e text */..  /* 
8a460 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c  .  ** sqlite3Mal
8a470 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 20 69 6e  locDisallow() in
8a480 63 72 65 6d 65 6e 74 73 20 74 68 65 20 66 6f 6c  crements the fol
8a490 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65 72 2e 0a  lowing counter..
8a4a0 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c    ** sqlite3Mall
8a4b0 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63 72 65 6d  ocAllow() decrem
8a4c0 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f 0a 20 20  ents it..  */.  
8a4d0 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b 20 2f 2a  int disallow; /*
8a4e0 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 6d 65   Do not allow me
8a4f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
8a500 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 47 61  */..  /*.  ** Ga
8a510 74 68 65 72 20 73 74 61 74 69 73 74 69 63 73 20  ther statistics 
8a520 6f 6e 20 74 68 65 20 73 69 7a 65 73 20 6f 66 20  on the sizes of 
8a530 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
8a540 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c 6f 63 5b  ns..  ** nAlloc[
8a550 69 5d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  i] is the number
8a560 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61   of allocation a
8a570 74 74 65 6d 70 74 73 20 6f 66 20 69 2a 38 0a 20  ttempts of i*8. 
8a580 20 2a 2a 20 62 79 74 65 73 2e 20 20 69 3d 3d 4e   ** bytes.  i==N
8a590 43 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d  CSIZE is the num
8a5a0 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f  ber of allocatio
8a5b0 6e 20 61 74 74 65 6d 70 74 73 20 66 6f 72 0a 20  n attempts for. 
8a5c0 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 65 20 74   ** sizes more t
8a5d0 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20 62 79 74  han NCSIZE*8 byt
8a5e0 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e  es..  */.  int n
8a5f0 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d 3b 20 20  Alloc[NCSIZE];  
8a600 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
8a610 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f  ber of allocatio
8a620 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72  ns */.  int nCur
8a630 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 20  rent[NCSIZE];   
8a640 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62   /* Current numb
8a650 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  er of allocation
8a660 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 75 72  s */.  int mxCur
8a670 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 20  rent[NCSIZE];   
8a680 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 72  /* Highwater mar
8a690 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e 74 20 2a  k for nCurrent *
8a6a0 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f 2a 0a 2a  /..} mem;.../*.*
8a6b0 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f 72 79 20  * Adjust memory 
8a6c0 75 73 61 67 65 20 73 74 61 74 69 73 74 69 63 73  usage statistics
8a6d0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8a6e0 61 64 6a 75 73 74 53 74 61 74 73 28 69 6e 74 20  adjustStats(int 
8a6f0 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e 63 72 65  iSize, int incre
8a700 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 69 20 3d  ment){.  int i =
8a710 20 28 28 69 53 69 7a 65 2b 37 29 26 7e 37 29 2f   ((iSize+7)&~7)/
8a720 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53 49 5a  8;.  if( i>NCSIZ
8a730 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d 20 4e  E-1 ){.    i = N
8a740 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d 0a 20  CSIZE - 1;.  }. 
8a750 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74 3e 30   if( increment>0
8a760 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 6c 6c   ){.    mem.nAll
8a770 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d 65 6d  oc[i]++;.    mem
8a780 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b 3b 0a  .nCurrent[i]++;.
8a790 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 75 72      if( mem.nCur
8a7a0 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 43 75  rent[i]>mem.mxCu
8a7b0 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 20 20  rrent[i] ){.    
8a7c0 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b    mem.mxCurrent[
8a7d0 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 65 6e  i] = mem.nCurren
8a7e0 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  t[i];.    }.  }e
8a7f0 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75  lse{.    mem.nCu
8a800 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 20 20  rrent[i]--;.    
8a810 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43 75 72  assert( mem.nCur
8a820 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a 20 20  rent[i]>=0 );.  
8a830 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  }.}../*.** Given
8a840 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20   an allocation, 
8a850 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63  find the MemBloc
8a860 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20 61 6c  kHdr for that al
8a870 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  location..**.** 
8a880 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
8a890 63 6b 73 20 74 68 65 20 67 75 61 72 64 73 20 61  cks the guards a
8a8a0 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f 66 20  t either end of 
8a8b0 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  the allocation a
8a8c0 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72  nd.** if they ar
8a8d0 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74 20 61  e incorrect it a
8a8e0 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  sserts..*/.stati
8a8f0 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63  c struct MemBloc
8a900 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d 65 6d  kHdr *sqlite3Mem
8a910 73 79 73 47 65 74 48 65 61 64 65 72 28 76 6f 69  sysGetHeader(voi
8a920 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e 29 7b  d *pAllocation){
8a930 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
8a940 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e 74 20  ckHdr *p;.  int 
8a950 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70 55 38  *pInt;.  u8 *pU8
8a960 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65  ;.  int nReserve
8a970 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75 63 74  ;..  p = (struct
8a980 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 70 41   MemBlockHdr*)pA
8a990 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 2d 2d  llocation;.  p--
8a9a0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
8a9b0 46 6f 72 65 47 75 61 72 64 3d 3d 28 69 6e 74 29  ForeGuard==(int)
8a9c0 46 4f 52 45 47 55 41 52 44 20 29 3b 0a 20 20 6e  FOREGUARD );.  n
8a9d0 52 65 73 65 72 76 65 20 3d 20 28 70 2d 3e 69 53  Reserve = (p->iS
8a9e0 69 7a 65 2b 37 29 26 7e 37 3b 0a 20 20 70 49 6e  ize+7)&~7;.  pIn
8a9f0 74 20 3d 20 28 69 6e 74 2a 29 70 41 6c 6c 6f 63  t = (int*)pAlloc
8aa00 61 74 69 6f 6e 3b 0a 20 20 70 55 38 20 3d 20 28  ation;.  pU8 = (
8aa10 75 38 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b  u8*)pAllocation;
8aa20 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 74 5b  .  assert( pInt[
8aa30 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28  nReserve/sizeof(
8aa40 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 52 45 41 52  int)]==(int)REAR
8aa50 47 55 41 52 44 20 29 3b 0a 20 20 61 73 73 65 72  GUARD );.  asser
8aa60 74 28 20 28 6e 52 65 73 65 72 76 65 2d 30 29 3c  t( (nReserve-0)<
8aa70 3d 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 70 55 38  =p->iSize || pU8
8aa80 5b 6e 52 65 73 65 72 76 65 2d 31 5d 3d 3d 30 78  [nReserve-1]==0x
8aa90 36 35 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  65 );.  assert( 
8aaa0 28 6e 52 65 73 65 72 76 65 2d 31 29 3c 3d 70 2d  (nReserve-1)<=p-
8aab0 3e 69 53 69 7a 65 20 7c 7c 20 70 55 38 5b 6e 52  >iSize || pU8[nR
8aac0 65 73 65 72 76 65 2d 32 5d 3d 3d 30 78 36 35 20  eserve-2]==0x65 
8aad0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 52  );.  assert( (nR
8aae0 65 73 65 72 76 65 2d 32 29 3c 3d 70 2d 3e 69 53  eserve-2)<=p->iS
8aaf0 69 7a 65 20 7c 7c 20 70 55 38 5b 6e 52 65 73 65  ize || pU8[nRese
8ab00 72 76 65 2d 33 5d 3d 3d 30 78 36 35 20 29 3b 0a  rve-3]==0x65 );.
8ab10 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
8ab20 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
8ab30 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
8ab40 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61  currently alloca
8ab50 74 65 64 20 61 74 20 61 64 64 72 65 73 73 20 70  ted at address p
8ab60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8ab70 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76  sqlite3MemSize(v
8ab80 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74 72 75 63  oid *p){.  struc
8ab90 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
8aba0 48 64 72 3b 0a 20 20 69 66 28 20 21 70 20 29 7b  Hdr;.  if( !p ){
8abb0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
8abc0 20 7d 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69   }.  pHdr = sqli
8abd0 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64  te3MemsysGetHead
8abe0 65 72 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  er(p);.  return 
8abf0 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a  pHdr->iSize;.}..
8ac00 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
8ac10 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
8ac20 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
8ac30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8ac40 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76  sqlite3MemInit(v
8ac50 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  oid *NotUsed){. 
8ac60 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
8ac70 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66  R(NotUsed);.  if
8ac80 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ( !sqlite3Global
8ac90 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20  Config.bMemstat 
8aca0 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 65 6d  ){.    /* If mem
8acb0 6f 72 79 20 73 74 61 74 75 73 20 69 73 20 65 6e  ory status is en
8acc0 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20  abled, then the 
8acd0 6d 61 6c 6c 6f 63 2e 63 20 77 72 61 70 70 65 72  malloc.c wrapper
8ace0 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 0a 20 20   will already.  
8acf0 20 20 2a 2a 20 68 6f 6c 64 20 74 68 65 20 53 54    ** hold the ST
8ad00 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 77  ATIC_MEM mutex w
8ad10 68 65 6e 20 74 68 65 20 72 6f 75 74 69 6e 65 73  hen the routines
8ad20 20 68 65 72 65 20 61 72 65 20 69 6e 76 6f 6b 65   here are invoke
8ad30 64 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d 2e 6d 75  d. */.    mem.mu
8ad40 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
8ad50 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
8ad60 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29  UTEX_STATIC_MEM)
8ad70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
8ad80 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
8ad90 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20  ** Deinitialize 
8ada0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
8adb0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e  ation subsystem.
8adc0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8add0 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f  sqlite3MemShutdo
8ade0 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  wn(void *NotUsed
8adf0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
8ae00 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
8ae10 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 30 3b    mem.mutex = 0;
8ae20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20  .}../*.** Round 
8ae30 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a  up a request siz
8ae40 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61  e to the next va
8ae50 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  lid allocation s
8ae60 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ize..*/.static i
8ae70 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75  nt sqlite3MemRou
8ae80 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72  ndup(int n){.  r
8ae90 65 74 75 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37  eturn (n+7) & ~7
8aea0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
8aeb0 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20  ate nByte bytes 
8aec0 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74  of memory..*/.st
8aed0 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74  atic void *sqlit
8aee0 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20  e3MemMalloc(int 
8aef0 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 63 74  nByte){.  struct
8af00 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48   MemBlockHdr *pH
8af10 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74  dr;.  void **pBt
8af20 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69  ;.  char *z;.  i
8af30 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f 69 64  nt *pInt;.  void
8af40 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74   *p = 0;.  int t
8af50 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e 74 20  otalSize;.  int 
8af60 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 6c 69  nReserve;.  sqli
8af70 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
8af80 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 61 73  mem.mutex);.  as
8af90 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c  sert( mem.disall
8afa0 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 73 65  ow==0 );.  nRese
8afb0 72 76 65 20 3d 20 28 6e 42 79 74 65 2b 37 29 26  rve = (nByte+7)&
8afc0 7e 37 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20  ~7;.  totalSize 
8afd0 3d 20 6e 52 65 73 65 72 76 65 20 2b 20 73 69 7a  = nReserve + siz
8afe0 65 6f 66 28 2a 70 48 64 72 29 20 2b 20 73 69 7a  eof(*pHdr) + siz
8aff0 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20 20 20 20  eof(int) +.     
8b000 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e 42            mem.nB
8b010 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28  acktrace*sizeof(
8b020 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e 6e 54 69  void*) + mem.nTi
8b030 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f  tle;.  p = mallo
8b040 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20  c(totalSize);.  
8b050 69 66 28 20 70 20 29 7b 0a 20 20 20 20 7a 20 3d  if( p ){.    z =
8b060 20 70 3b 0a 20 20 20 20 70 42 74 20 3d 20 28 76   p;.    pBt = (v
8b070 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e 54 69  oid**)&z[mem.nTi
8b080 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64 72 20 3d  tle];.    pHdr =
8b090 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63   (struct MemBloc
8b0a0 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65 6d 2e 6e  kHdr*)&pBt[mem.n
8b0b0 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20 20 20 20  Backtrace];.    
8b0c0 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  pHdr->pNext = 0;
8b0d0 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76  .    pHdr->pPrev
8b0e0 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20   = mem.pLast;.  
8b0f0 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73 74 20    if( mem.pLast 
8b100 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 4c 61  ){.      mem.pLa
8b110 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 72  st->pNext = pHdr
8b120 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
8b130 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20     mem.pFirst = 
8b140 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  pHdr;.    }.    
8b150 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72  mem.pLast = pHdr
8b160 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69 46 6f 72  ;.    pHdr->iFor
8b170 65 47 75 61 72 64 20 3d 20 46 4f 52 45 47 55 41  eGuard = FOREGUA
8b180 52 44 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 42  RD;.    pHdr->nB
8b190 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 3d 20  acktraceSlots = 
8b1a0 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a  mem.nBacktrace;.
8b1b0 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65      pHdr->nTitle
8b1c0 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20   = mem.nTitle;. 
8b1d0 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42 61 63 6b     if( mem.nBack
8b1e0 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 76  trace ){.      v
8b1f0 6f 69 64 20 2a 61 41 64 64 72 5b 34 30 5d 3b 0a  oid *aAddr[40];.
8b200 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63        pHdr->nBac
8b210 6b 74 72 61 63 65 20 3d 20 62 61 63 6b 74 72 61  ktrace = backtra
8b220 63 65 28 61 41 64 64 72 2c 20 6d 65 6d 2e 6e 42  ce(aAddr, mem.nB
8b230 61 63 6b 74 72 61 63 65 2b 31 29 2d 31 3b 0a 20  acktrace+1)-1;. 
8b240 20 20 20 20 20 6d 65 6d 63 70 79 28 70 42 74 2c       memcpy(pBt,
8b250 20 26 61 41 64 64 72 5b 31 5d 2c 20 70 48 64 72   &aAddr[1], pHdr
8b260 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a  ->nBacktrace*siz
8b270 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a 20 20 20  eof(void*));.   
8b280 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b     if( mem.xBack
8b290 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20  trace ){.       
8b2a0 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28   mem.xBacktrace(
8b2b0 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61  nByte, pHdr->nBa
8b2c0 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64  cktrace-1, &aAdd
8b2d0 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r[1]);.      }. 
8b2e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
8b2f0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
8b300 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
8b310 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29  if( mem.nTitle )
8b320 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  {.      memcpy(z
8b330 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65  , mem.zTitle, me
8b340 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d  m.nTitle);.    }
8b350 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65  .    pHdr->iSize
8b360 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64   = nByte;.    ad
8b370 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c  justStats(nByte,
8b380 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d   +1);.    pInt =
8b390 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b   (int*)&pHdr[1];
8b3a0 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72  .    pInt[nReser
8b3b0 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20  ve/sizeof(int)] 
8b3c0 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20  = REARGUARD;.   
8b3d0 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78   memset(pInt, 0x
8b3e0 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20  65, nReserve);. 
8b3f0 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49     p = (void*)pI
8b400 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  nt;.  }.  sqlite
8b410 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
8b420 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  m.mutex);.  retu
8b430 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn p; .}../*.** 
8b440 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  Free memory..*/.
8b450 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
8b460 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20  te3MemFree(void 
8b470 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75  *pPrior){.  stru
8b480 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
8b490 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70  pHdr;.  void **p
8b4a0 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  Bt;.  char *z;. 
8b4b0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
8b4c0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
8b4d0 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74  mstat || mem.mut
8b4e0 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20  ex!=0 );.  pHdr 
8b4f0 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47  = sqlite3MemsysG
8b500 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29  etHeader(pPrior)
8b510 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a  ;.  pBt = (void*
8b520 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d  *)pHdr;.  pBt -=
8b530 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
8b540 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65  eSlots;.  sqlite
8b550 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
8b560 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  m.mutex);.  if( 
8b570 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20  pHdr->pPrev ){. 
8b580 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d     assert( pHdr-
8b590 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70  >pPrev->pNext==p
8b5a0 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d  Hdr );.    pHdr-
8b5b0 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
8b5c0 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  pHdr->pNext;.  }
8b5d0 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
8b5e0 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48  ( mem.pFirst==pH
8b5f0 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46  dr );.    mem.pF
8b600 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65  irst = pHdr->pNe
8b610 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48  xt;.  }.  if( pH
8b620 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  dr->pNext ){.   
8b630 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70   assert( pHdr->p
8b640 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64  Next->pPrev==pHd
8b650 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70  r );.    pHdr->p
8b660 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48  Next->pPrev = pH
8b670 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c  dr->pPrev;.  }el
8b680 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
8b690 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20  mem.pLast==pHdr 
8b6a0 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74  );.    mem.pLast
8b6b0 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a   = pHdr->pPrev;.
8b6c0 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a    }.  z = (char*
8b6d0 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64  )pBt;.  z -= pHd
8b6e0 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a  r->nTitle;.  adj
8b6f0 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69  ustStats(pHdr->i
8b700 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d  Size, -1);.  mem
8b710 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a  set(z, 0x2b, siz
8b720 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d  eof(void*)*pHdr-
8b730 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
8b740 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29   + sizeof(*pHdr)
8b750 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   +.             
8b760 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65       pHdr->iSize
8b770 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b   + sizeof(int) +
8b780 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a   pHdr->nTitle);.
8b790 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c    free(z);.  sql
8b7a0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
8b7b0 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d  (mem.mutex);  .}
8b7c0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
8b7d0 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78  he size of an ex
8b7e0 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c  isting memory al
8b7f0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  location..**.** 
8b800 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69  For this debuggi
8b810 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
8b820 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d  n, we *always* m
8b830 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
8b840 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e.** allocation 
8b850 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65  into a new place
8b860 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20   in memory.  In 
8b870 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65  this way, if the
8b880 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65   .** higher leve
8b890 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20  l code is using 
8b8a0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f  pointer to the o
8b8b0 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  ld allocation, i
8b8c0 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f  t is .** much mo
8b8d0 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65  re likely to bre
8b8e0 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75  ak and we are mu
8b8f0 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74  ch more liking t
8b900 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72  o find.** the er
8b910 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ror..*/.static v
8b920 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52  oid *sqlite3MemR
8b930 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
8b940 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  ior, int nByte){
8b950 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
8b960 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a  ckHdr *pOldHdr;.
8b970 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20    void *pNew;.  
8b980 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61  assert( mem.disa
8b990 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c  llow==0 );.  pOl
8b9a0 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65  dHdr = sqlite3Me
8b9b0 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50  msysGetHeader(pP
8b9c0 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20  rior);.  pNew = 
8b9d0 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
8b9e0 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70  (nByte);.  if( p
8b9f0 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  New ){.    memcp
8ba00 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20  y(pNew, pPrior, 
8ba10 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69  nByte<pOldHdr->i
8ba20 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70  Size ? nByte : p
8ba30 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a  OldHdr->iSize);.
8ba40 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f      if( nByte>pO
8ba50 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a  ldHdr->iSize ){.
8ba60 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28        memset(&((
8ba70 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64  char*)pNew)[pOld
8ba80 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32  Hdr->iSize], 0x2
8ba90 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48  b, nByte - pOldH
8baa0 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20  dr->iSize);.    
8bab0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  }.    sqlite3Mem
8bac0 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  Free(pPrior);.  
8bad0 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
8bae0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61  .}../*.** Popula
8baf0 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c  te the low-level
8bb00 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
8bb10 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e  on function poin
8bb20 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74  ters in.** sqlit
8bb30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
8bb40 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
8bb50 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69  o the routines i
8bb60 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a  n this file..*/.
8bb70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
8bb80 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65  oid sqlite3MemSe
8bb90 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a  tDefault(void){.
8bba0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
8bbb0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
8bbc0 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  ds defaultMethod
8bbd0 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74  s = {.     sqlit
8bbe0 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20  e3MemMalloc,.   
8bbf0 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65    sqlite3MemFree
8bc00 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
8bc10 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73  mRealloc,.     s
8bc20 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20  qlite3MemSize,. 
8bc30 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f      sqlite3MemRo
8bc40 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69  undup,.     sqli
8bc50 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20  te3MemInit,.    
8bc60 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64   sqlite3MemShutd
8bc70 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b  own,.     0.  };
8bc80 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  .  sqlite3_confi
8bc90 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  g(SQLITE_CONFIG_
8bca0 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74  MALLOC, &default
8bcb0 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a  Methods);.}../*.
8bcc0 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65  ** Set the numbe
8bcd0 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c  r of backtrace l
8bce0 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65  evels kept for e
8bcf0 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a  ach allocation..
8bd00 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65  ** A value of ze
8bd10 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63  ro turns off bac
8bd20 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e  ktracing.  The n
8bd30 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20  umber is always 
8bd40 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f  rounded.** up to
8bd50 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32   a multiple of 2
8bd60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
8bd70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
8bd80 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63  MemdebugBacktrac
8bd90 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20  e(int depth){.  
8bda0 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64  if( depth<0 ){ d
8bdb0 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66  epth = 0; }.  if
8bdc0 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65  ( depth>20 ){ de
8bdd0 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65  pth = 20; }.  de
8bde0 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26  pth = (depth+1)&
8bdf0 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63  0xfe;.  mem.nBac
8be00 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a  ktrace = depth;.
8be10 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
8be20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  E void sqlite3Me
8be30 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43  mdebugBacktraceC
8be40 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78  allback(void (*x
8be50 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20  Backtrace)(int, 
8be60 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a  int, void **)){.
8be70 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65    mem.xBacktrace
8be80 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d   = xBacktrace;.}
8be90 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
8bea0 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72  title string for
8beb0 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f   subsequent allo
8bec0 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  cations..*/.SQLI
8bed0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
8bee0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
8bef0 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68  ettitle(const ch
8bf00 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75  ar *zTitle){.  u
8bf10 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20  nsigned int n = 
8bf20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
8bf30 7a 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73  zTitle) + 1;.  s
8bf40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
8bf50 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20  er(mem.mutex);. 
8bf60 20 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d   if( n>=sizeof(m
8bf70 65 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d  em.zTitle) ) n =
8bf80 20 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74   sizeof(mem.zTit
8bf90 6c 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28  le)-1;.  memcpy(
8bfa0 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74  mem.zTitle, zTit
8bfb0 6c 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54  le, n);.  mem.zT
8bfc0 69 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d  itle[n] = 0;.  m
8bfd0 65 6d 2e 6e 54 69 74 6c 65 20 3d 20 28 6e 2b 37  em.nTitle = (n+7
8bfe0 29 26 7e 37 3b 0a 20 20 73 71 6c 69 74 65 33 5f  )&~7;.  sqlite3_
8bff0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e  mutex_leave(mem.
8c000 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54  mutex);.}..SQLIT
8c010 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
8c020 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79  qlite3MemdebugSy
8c030 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d  nc(){.  struct M
8c040 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72  emBlockHdr *pHdr
8c050 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d  ;.  for(pHdr=mem
8c060 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70  .pFirst; pHdr; p
8c070 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29  Hdr=pHdr->pNext)
8c080 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74  {.    void **pBt
8c090 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b   = (void**)pHdr;
8c0a0 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72  .    pBt -= pHdr
8c0b0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74  ->nBacktraceSlot
8c0c0 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b  s;.    mem.xBack
8c0d0 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a  trace(pHdr->iSiz
8c0e0 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72  e, pHdr->nBacktr
8c0f0 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b  ace-1, &pBt[1]);
8c100 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70  .  }.}../*.** Op
8c110 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69  en the file indi
8c120 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20  cated and write 
8c130 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66  a log of all unf
8c140 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20  reed memory .** 
8c150 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f  allocations into
8c160 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51   that log..*/.SQ
8c170 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
8c180 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  d sqlite3Memdebu
8c190 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  gDump(const char
8c1a0 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20   *zFilename){.  
8c1b0 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72  FILE *out;.  str
8c1c0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
8c1d0 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a  *pHdr;.  void **
8c1e0 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  pBt;.  int i;.  
8c1f0 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c  out = fopen(zFil
8c200 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69  ename, "w");.  i
8c210 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20  f( out==0 ){.   
8c220 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
8c230 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f   "** Unable to o
8c240 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62  utput memory deb
8c250 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25  ug output log: %
8c260 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20  s **\n",.       
8c270 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69               zFi
8c280 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  lename);.    ret
8c290 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70  urn;.  }.  for(p
8c2a0 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20  Hdr=mem.pFirst; 
8c2b0 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d  pHdr; pHdr=pHdr-
8c2c0 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61  >pNext){.    cha
8c2d0 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48  r *z = (char*)pH
8c2e0 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64  dr;.    z -= pHd
8c2f0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f  r->nBacktraceSlo
8c300 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29  ts*sizeof(void*)
8c310 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b   + pHdr->nTitle;
8c320 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
8c330 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74  , "**** %lld byt
8c340 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73  es at %p from %s
8c350 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20   ****\n", .     
8c360 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69         pHdr->iSi
8c370 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48  ze, &pHdr[1], pH
8c380 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a  dr->nTitle ? z :
8c390 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28   "???");.    if(
8c3a0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
8c3b0 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73  e ){.      fflus
8c3c0 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42  h(out);.      pB
8c3d0 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72  t = (void**)pHdr
8c3e0 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70  ;.      pBt -= p
8c3f0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53  Hdr->nBacktraceS
8c400 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b  lots;.      back
8c410 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64  trace_symbols_fd
8c420 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63  (pBt, pHdr->nBac
8c430 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f  ktrace, fileno(o
8c440 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69  ut));.      fpri
8c450 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a  ntf(out, "\n");.
8c460 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69      }.  }.  fpri
8c470 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53  ntf(out, "COUNTS
8c480 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30  :\n");.  for(i=0
8c490 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b  ; i<NCSIZE-1; i+
8c4a0 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e  +){.    if( mem.
8c4b0 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20  nAlloc[i] ){.   
8c4c0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
8c4d0 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31  "   %5d: %10d %1
8c4e0 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20  0d %10d\n", .   
8c4f0 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65           i*8, me
8c500 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d  m.nAlloc[i], mem
8c510 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65  .nCurrent[i], me
8c520 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b  m.mxCurrent[i]);
8c530 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
8c540 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49   mem.nAlloc[NCSI
8c550 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72  ZE-1] ){.    fpr
8c560 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35  intf(out, "   %5
8c570 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30  d: %10d %10d %10
8c580 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
8c590 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d     NCSIZE*8-8, m
8c5a0 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45  em.nAlloc[NCSIZE
8c5b0 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  -1],.           
8c5c0 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e    mem.nCurrent[N
8c5d0 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78  CSIZE-1], mem.mx
8c5e0 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31  Current[NCSIZE-1
8c5f0 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65  ]);.  }.  fclose
8c600 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (out);.}../*.** 
8c610 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
8c620 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74  r of times sqlit
8c630 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61  e3MemMalloc() ha
8c640 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a  s been called..*
8c650 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
8c660 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64   int sqlite3Memd
8c670 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28  ebugMallocCount(
8c680 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
8c690 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20  t nTotal = 0;.  
8c6a0 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a  for(i=0; i<NCSIZ
8c6b0 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f  E; i++){.    nTo
8c6c0 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f  tal += mem.nAllo
8c6d0 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  c[i];.  }.  retu
8c6e0 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23  rn nTotal;.}...#
8c6f0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
8c700 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a  MEMDEBUG */../**
8c710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
8c720 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a   of mem2.c *****
8c730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
8c760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
8c770 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a  in file mem3.c *
8c780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
8c7b0 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20  ** 2007 October 
8c7c0 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  14.**.** The aut
8c7d0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
8c7e0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
8c7f0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
8c800 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
8c810 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
8c820 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
8c830 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
8c840 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
8c850 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
8c860 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
8c870 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
8c880 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
8c890 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
8c8a0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
8c8b0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
8c8c0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
8c8d0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
8c8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c920 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
8c930 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75  ontains the C fu
8c940 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70  nctions that imp
8c950 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a  lement a memory.
8c960 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ** allocation su
8c970 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20  bsystem for use 
8c980 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a  by SQLite. .**.*
8c990 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
8c9a0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
8c9b0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
8c9c0 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75  m omits all.** u
8c9d0 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20  se of malloc(). 
8c9e0 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20  The SQLite user 
8c9f0 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b  supplies a block
8ca00 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65   of memory.** be
8ca10 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c  fore calling sql
8ca20 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
8ca30 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c  ) from which all
8ca40 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  ocations.** are 
8ca50 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65  made and returne
8ca60 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63  d by the xMalloc
8ca70 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28  () and xRealloc(
8ca80 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ) .** implementa
8ca90 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69  tions. Once sqli
8caa0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
8cab0 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
8cac0 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20  ,.** the amount 
8cad0 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61  of memory availa
8cae0 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73  ble to SQLite is
8caf0 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f   fixed and canno
8cb00 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e  t.** be changed.
8cb10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  .**.** This vers
8cb20 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
8cb30 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
8cb40 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64  system is includ
8cb50 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69  ed.** in the bui
8cb60 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54  ld only if SQLIT
8cb70 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33  E_ENABLE_MEMSYS3
8cb80 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a   is defined..**.
8cb90 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c 76  ** $Id: mem3.c,v
8cba0 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f 31 39   1.25 2008/11/19
8cbb0 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c   16:52:44 daniel
8cbc0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
8cbd0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
8cbe0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
8cbf0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e   allocator is on
8cc00 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  ly built into th
8cc10 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c  e library.** SQL
8cc20 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
8cc30 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44  S3 is defined. D
8cc40 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d  efining this sym
8cc50 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  bol does not.** 
8cc60 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69  mean that the li
8cc70 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61  brary will use a
8cc80 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20   memory-pool by 
8cc90 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68  default, just th
8cca0 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69  at.** it is avai
8ccb0 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f  lable. The mempo
8ccc0 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ol allocator is 
8ccd0 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 6c  activated by cal
8cce0 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ling.** sqlite3_
8ccf0 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66  config()..*/.#if
8cd00 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
8cd10 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a  E_MEMSYS3../*.**
8cd20 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69   Maximum size (i
8cd30 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66  n Mem3Blocks) of
8cd40 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b   a "small" chunk
8cd50 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f  ..*/.#define MX_
8cd60 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a  SMALL 10.../*.**
8cd70 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c   Number of freel
8cd80 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a  ist hash slots.*
8cd90 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48  /.#define N_HASH
8cda0 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65    61../*.** A me
8cdb0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
8cdc0 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22  (also called a "
8cdd0 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73  chunk") consists
8cde0 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d   of two or .** m
8cdf0 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65  ore blocks where
8ce00 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38   each block is 8
8ce10 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 72   bytes.  The fir
8ce20 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 0a  st 8 bytes are .
8ce30 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 74  ** a header that
8ce40 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64   is not returned
8ce50 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a   to the user..**
8ce60 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74  .** A chunk is t
8ce70 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b  wo or more block
8ce80 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 72  s that is either
8ce90 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a   checked out or.
8cea0 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 69  ** free.  The fi
8ceb0 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f  rst block has fo
8cec0 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68  rmat u.hdr.  u.h
8ced0 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74  dr.size4x is 4 t
8cee0 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65  imes the.** size
8cef0 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
8cf00 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20  on in blocks if 
8cf10 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
8cf20 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75  s free..** The u
8cf30 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69  .hdr.size4x&1 bi
8cf40 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65  t is true if the
8cf50 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
8cf60 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c  d out and.** fal
8cf70 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20  se if the chunk 
8cf80 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  is on the freeli
8cf90 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73  st.  The u.hdr.s
8cfa0 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69  ize4x&2 bit.** i
8cfb0 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72  s true if the pr
8cfc0 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20  evious chunk is 
8cfd0 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20  checked out and 
8cfe0 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20  false if the.** 
8cff0 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69  previous chunk i
8d000 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68  s free.  The u.h
8d010 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c  dr.prevSize fiel
8d020 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66  d is the size of
8d030 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73  .** the previous
8d040 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73   chunk in blocks
8d050 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
8d060 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65   chunk is on the
8d070 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66  .** freelist. If
8d080 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68   the previous ch
8d090 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f  unk is checked o
8d0a0 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64  ut, then.** u.hd
8d0b0 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62  r.prevSize can b
8d0c0 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  e part of the da
8d0d0 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e  ta for that chun
8d0e0 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20  k and should.** 
8d0f0 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77  not be read or w
8d100 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65  ritten..**.** We
8d110 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20   often identify 
8d120 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69  a chunk by its i
8d130 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f  ndex in mem3.aPo
8d140 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74  ol[].  When.** t
8d150 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65  his is done, the
8d160 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66   chunk index ref
8d170 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e  ers to the secon
8d180 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68  d block of.** th
8d190 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69  e chunk.  In thi
8d1a0 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 74  s way, the first
8d1b0 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e   chunk has an in
8d1c0 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63  dex of 1..** A c
8d1d0 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20  hunk index of 0 
8d1e0 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63  means "no such c
8d1f0 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65  hunk" and is the
8d200 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f   equivalent.** o
8d210 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  f a NULL pointer
8d220 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ..**.** The seco
8d230 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65  nd block of free
8d240 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68   chunks is of th
8d250 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20  e form u.list.  
8d260 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64  The.** two field
8d270 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d  s form a double-
8d280 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63  linked list of c
8d290 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64  hunks of related
8d2a0 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74   sizes..** Point
8d2b0 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 20  ers to the head 
8d2c0 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 20  of the list are 
8d2d0 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61  stored in mem3.a
8d2e0 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72  iSmall[] .** for
8d2f0 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20   smaller chunks 
8d300 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b  and mem3.aiHash[
8d310 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75  ] for larger chu
8d320 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  nks..**.** The s
8d330 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61  econd block of a
8d340 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64   chunk is user d
8d350 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b  ata if the chunk
8d360 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20   is checked .** 
8d370 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b  out.  If a chunk
8d380 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c   is checked out,
8d390 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 6d   the user data m
8d3a0 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a  ay extend into.*
8d3b0 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76  * the u.hdr.prev
8d3c0 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68  Size value of th
8d3d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e  e following chun
8d3e0 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  k..*/.typedef st
8d3f0 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d  ruct Mem3Block M
8d400 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74  em3Block;.struct
8d410 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75   Mem3Block {.  u
8d420 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63  nion {.    struc
8d430 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72  t {.      u32 pr
8d440 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a  evSize;   /* Siz
8d450 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68  e of previous ch
8d460 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  unk in Mem3Block
8d470 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20   elements */.   
8d480 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20     u32 size4x;  
8d490 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a     /* 4x the siz
8d4a0 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75  e of current chu
8d4b0 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20  nk in Mem3Block 
8d4c0 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  elements */.    
8d4d0 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63  } hdr;.    struc
8d4e0 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65  t {.      u32 ne
8d4f0 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  xt;       /* Ind
8d500 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ex in mem3.aPool
8d510 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20  [] of next free 
8d520 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75  chunk */.      u
8d530 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f  32 prev;       /
8d540 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e  * Index in mem3.
8d550 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69  aPool[] of previ
8d560 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a  ous free chunk *
8d570 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20  /.    } list;.  
8d580 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  } u;.};../*.** A
8d590 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63  ll of the static
8d5a0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20   variables used 
8d5b0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61  by this module a
8d5c0 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20  re collected.** 
8d5d0 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74  into a single st
8d5e0 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d  ructure named "m
8d5f0 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 74  em3".  This is t
8d600 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74  o keep the.** st
8d610 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f  atic variables o
8d620 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20  rganized and to 
8d630 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65  reduce namespace
8d640 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68   pollution.** wh
8d650 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69  en this module i
8d660 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  s combined with 
8d670 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61  other in the ama
8d680 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  lgamation..*/.st
8d690 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
8d6a0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61  struct Mem3Globa
8d6b0 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65  l {.  /*.  ** Me
8d6c0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66  mory available f
8d6d0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e  or allocation. n
8d6e0 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65  Pool is the size
8d6f0 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 20   of the array.  
8d700 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  ** (in Mem3Block
8d710 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  s) pointed to by
8d720 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20   aPool less 2.. 
8d730 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b   */.  u32 nPool;
8d740 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50  .  Mem3Block *aP
8d750 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ool;..  /*.  ** 
8d760 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 65  True if we are e
8d770 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74  valuating an out
8d780 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62  -of-memory callb
8d790 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ack..  */.  int 
8d7a0 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20  alarmBusy;.  .  
8d7b0 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f  /*.  ** Mutex to
8d7c0 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20   control access 
8d7d0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  to the memory al
8d7e0 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
8d7f0 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  em..  */.  sqlit
8d800 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
8d810 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  .  .  /*.  ** Th
8d820 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74  e minimum amount
8d830 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 74   of free space t
8d840 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 6e  hat we have seen
8d850 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d  ..  */.  u32 mnM
8d860 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  aster;..  /*.  *
8d870 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 65  * iMaster is the
8d880 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61   index of the ma
8d890 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73  ster chunk.  Mos
8d8a0 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e  t new allocation
8d8b0 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66  s.  ** occur off
8d8c0 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20   of this chunk. 
8d8d0 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65   szMaster is the
8d8e0 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c   size (in Mem3Bl
8d8f0 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68  ocks).  ** of th
8d900 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72  e current master
8d910 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 20  .  iMaster is 0 
8d920 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20  if there is not 
8d930 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20  master chunk..  
8d940 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 68  ** The master ch
8d950 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69  unk is not in ei
8d960 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 5b  ther the aiHash[
8d970 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a  ] or aiSmall[]..
8d980 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74    */.  u32 iMast
8d990 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74  er;.  u32 szMast
8d9a0 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41  er;..  /*.  ** A
8d9b0 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66  rray of lists of
8d9c0 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63   free blocks acc
8d9d0 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c  ording to the bl
8d9e0 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66  ock size .  ** f
8d9f0 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b  or smaller chunk
8da00 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20  s, or a hash on 
8da10 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66  the block size f
8da20 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63  or larger.  ** c
8da30 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33  hunks..  */.  u3
8da40 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41  2 aiSmall[MX_SMA
8da50 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20  LL-1];   /* For 
8da60 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20  sizes 2 through 
8da70 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73  MX_SMALL, inclus
8da80 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48  ive */.  u32 aiH
8da90 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20  ash[N_HASH];    
8daa0 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73      /* For sizes
8dab0 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20   MX_SMALL+1 and 
8dac0 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33  larger */.} mem3
8dad0 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b   = { 97535575 };
8dae0 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47  ..#define mem3 G
8daf0 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d  LOBAL(struct Mem
8db00 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a  3Global, mem3)..
8db10 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65  /*.** Unlink the
8db20 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61   chunk at mem3.a
8db30 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73  Pool[i] from lis
8db40 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  t it is currentl
8db50 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74  y.** on.  *pRoot
8db60 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 61   is the list tha
8db70 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20  t i is a member 
8db80 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  of..*/.static vo
8db90 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b  id memsys3Unlink
8dba0 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20  FromList(u32 i, 
8dbb0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75  u32 *pRoot){.  u
8dbc0 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61  32 next = mem3.a
8dbd0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[i].u.list.n
8dbe0 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20  ext;.  u32 prev 
8dbf0 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e  = mem3.aPool[i].
8dc00 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61  u.list.prev;.  a
8dc10 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
8dc20 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
8dc30 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
8dc40 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70  rev==0 ){.    *p
8dc50 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d  Root = next;.  }
8dc60 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61  else{.    mem3.a
8dc70 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73  Pool[prev].u.lis
8dc80 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20  t.next = next;. 
8dc90 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b   }.  if( next ){
8dca0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
8dcb0 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65  next].u.list.pre
8dcc0 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20  v = prev;.  }.  
8dcd0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
8dce0 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20  list.next = 0;. 
8dcf0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75   mem3.aPool[i].u
8dd00 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a  .list.prev = 0;.
8dd10 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  }../*.** Unlink 
8dd20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64  the chunk at ind
8dd30 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68  ex i from .** wh
8dd40 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 63  atever list is c
8dd50 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65  urrently a membe
8dd60 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  r of..*/.static 
8dd70 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69  void memsys3Unli
8dd80 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32  nk(u32 i){.  u32
8dd90 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61   size, hash;.  a
8dda0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
8ddb0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
8ddc0 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
8ddd0 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
8dde0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8ddf0 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73   & 1)==0 );.  as
8de00 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20  sert( i>=1 );.  
8de10 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  size = mem3.aPoo
8de20 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
8de30 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28  e4x/4;.  assert(
8de40 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f   size==mem3.aPoo
8de50 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64  l[i+size-1].u.hd
8de60 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20  r.prevSize );.  
8de70 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20  assert( size>=2 
8de80 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d  );.  if( size <=
8de90 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20   MX_SMALL ){.   
8dea0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
8deb0 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  omList(i, &mem3.
8dec0 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29  aiSmall[size-2])
8ded0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68  ;.  }else{.    h
8dee0 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48  ash = size % N_H
8def0 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33  ASH;.    memsys3
8df00 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69  UnlinkFromList(i
8df10 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68  , &mem3.aiHash[h
8df20 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ash]);.  }.}../*
8df30 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75  .** Link the chu
8df40 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c  nk at mem3.aPool
8df50 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f  [i] so that is o
8df60 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65  n the list roote
8df70 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a  d.** at *pRoot..
8df80 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
8df90 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69  emsys3LinkIntoLi
8dfa0 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70  st(u32 i, u32 *p
8dfb0 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28  Root){.  assert(
8dfc0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
8dfd0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
8dfe0 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  );.  mem3.aPool[
8dff0 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d  i].u.list.next =
8e000 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e   *pRoot;.  mem3.
8e010 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
8e020 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20  prev = 0;.  if( 
8e030 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65  *pRoot ){.    me
8e040 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d  m3.aPool[*pRoot]
8e050 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69  .u.list.prev = i
8e060 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d  ;.  }.  *pRoot =
8e070 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e   i;.}../*.** Lin
8e080 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69  k the chunk at i
8e090 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68  ndex i into eith
8e0a0 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  er the appropria
8e0b0 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e  te.** small chun
8e0c0 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20  k list, or into 
8e0d0 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20  the large chunk 
8e0e0 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  hash table..*/.s
8e0f0 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
8e100 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20  s3Link(u32 i){. 
8e110 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b   u32 size, hash;
8e120 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
8e130 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
8e140 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  m3.mutex) );.  a
8e150 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20  ssert( i>=1 );. 
8e160 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61   assert( (mem3.a
8e170 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
8e180 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29  size4x & 1)==0 )
8e190 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e  ;.  size = mem3.
8e1a0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
8e1b0 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73  .size4x/4;.  ass
8e1c0 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e  ert( size==mem3.
8e1d0 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e  aPool[i+size-1].
8e1e0 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29  u.hdr.prevSize )
8e1f0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
8e200 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a  >=2 );.  if( siz
8e210 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b  e <= MX_SMALL ){
8e220 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  .    memsys3Link
8e230 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d  IntoList(i, &mem
8e240 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32  3.aiSmall[size-2
8e250 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ]);.  }else{.   
8e260 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e   hash = size % N
8e270 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79  _HASH;.    memsy
8e280 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69  s3LinkIntoList(i
8e290 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68  , &mem3.aiHash[h
8e2a0 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ash]);.  }.}../*
8e2b0 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 49  .** If the STATI
8e2c0 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e  C_MEM mutex is n
8e2d0 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c  ot already held,
8e2e0 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20   obtain it now. 
8e2f0 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c  The mutex.** wil
8e300 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c  l already be hel
8e310 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63  d (obtained by c
8e320 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29  ode in malloc.c)
8e330 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c   if.** sqlite3Gl
8e340 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53  obalConfig.bMemS
8e350 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a  tat is true..*/.
8e360 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
8e370 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a  ys3Enter(void){.
8e380 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
8e390 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
8e3a0 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75  at==0 && mem3.mu
8e3b0 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65  tex==0 ){.    me
8e3c0 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  m3.mutex = sqlit
8e3d0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
8e3e0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
8e3f0 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  _MEM);.  }.  sql
8e400 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
8e410 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a  (mem3.mutex);.}.
8e420 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
8e430 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a  ys3Leave(void){.
8e440 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
8e450 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78  leave(mem3.mutex
8e460 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  );.}../*.** Call
8e470 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 75  ed when we are u
8e480 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79  nable to satisfy
8e490 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f   an allocation o
8e4a0 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  f nBytes..*/.sta
8e4b0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
8e4c0 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20  OutOfMemory(int 
8e4d0 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d  nByte){.  if( !m
8e4e0 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b  em3.alarmBusy ){
8e4f0 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42  .    mem3.alarmB
8e500 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73  usy = 1;.    ass
8e510 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
8e520 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
8e530 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ex) );.    sqlit
8e540 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
8e550 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em3.mutex);.    
8e560 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
8e570 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20  memory(nByte);. 
8e580 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
8e590 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65  _enter(mem3.mute
8e5a0 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61  x);.    mem3.ala
8e5b0 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a  rmBusy = 0;.  }.
8e5c0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20  }.../*.** Chunk 
8e5d0 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 6e  i is a free chun
8e5e0 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20  k that has been 
8e5f0 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73  unlinked.  Adjus
8e600 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70  t its .** size p
8e610 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68  arameters for ch
8e620 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75  eck-out and retu
8e630 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
8e640 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72  the .** user por
8e650 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e  tion of the chun
8e660 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
8e670 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f  d *memsys3Checko
8e680 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42  ut(u32 i, u32 nB
8e690 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a  lock){.  u32 x;.
8e6a0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
8e6b0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
8e6c0 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  3.mutex) );.  as
8e6d0 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20  sert( i>=1 );.  
8e6e0 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f  assert( mem3.aPo
8e6f0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
8e700 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29  ze4x/4==nBlock )
8e710 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33  ;.  assert( mem3
8e720 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d  .aPool[i+nBlock-
8e730 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
8e740 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78  e==nBlock );.  x
8e750 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
8e760 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b  1].u.hdr.size4x;
8e770 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d  .  mem3.aPool[i-
8e780 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8e790 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c  = nBlock*4 | 1 |
8e7a0 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61   (x&2);.  mem3.a
8e7b0 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d  Pool[i+nBlock-1]
8e7c0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
8e7d0 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33  = nBlock;.  mem3
8e7e0 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d  .aPool[i+nBlock-
8e7f0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8e800 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26  |= 2;.  return &
8e810 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d  mem3.aPool[i];.}
8e820 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20  ../*.** Carve a 
8e830 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65  piece off of the
8e840 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33   end of the mem3
8e850 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 68  .iMaster free ch
8e860 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  unk..** Return a
8e870 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
8e880 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  new allocation. 
8e890 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74   Or, if the mast
8e8a0 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e  er chunk.** is n
8e8b0 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c  ot large enough,
8e8c0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74   return 0..*/.st
8e8d0 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79  atic void *memsy
8e8e0 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32  s3FromMaster(u32
8e8f0 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65   nBlock){.  asse
8e900 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
8e910 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
8e920 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
8e930 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e  mem3.szMaster>=n
8e940 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e  Block );.  if( n
8e950 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61  Block>=mem3.szMa
8e960 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a  ster-1 ){.    /*
8e970 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 20   Use the entire 
8e980 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f  master */.    vo
8e990 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43  id *p = memsys3C
8e9a0 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61  heckout(mem3.iMa
8e9b0 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73  ster, mem3.szMas
8e9c0 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69  ter);.    mem3.i
8e9d0 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
8e9e0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20  mem3.szMaster = 
8e9f0 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61  0;.    mem3.mnMa
8ea00 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65  ster = 0;.    re
8ea10 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b  turn p;.  }else{
8ea20 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68  .    /* Split th
8ea30 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20  e master block. 
8ea40 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c   Return the tail
8ea50 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77  . */.    u32 new
8ea60 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d  i, x;.    newi =
8ea70 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20   mem3.iMaster + 
8ea80 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20  mem3.szMaster - 
8ea90 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65  nBlock;.    asse
8eaa0 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e  rt( newi > mem3.
8eab0 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20  iMaster+1 );.   
8eac0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
8ead0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
8eae0 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8eaf0 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63  prevSize = nBloc
8eb00 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  k;.    mem3.aPoo
8eb10 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d  l[mem3.iMaster+m
8eb20 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e  em3.szMaster-1].
8eb30 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20  u.hdr.size4x |= 
8eb40 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  2;.    mem3.aPoo
8eb50 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e  l[newi-1].u.hdr.
8eb60 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a  size4x = nBlock*
8eb70 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e  4 + 1;.    mem3.
8eb80 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f  szMaster -= nBlo
8eb90 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ck;.    mem3.aPo
8eba0 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72  ol[newi-1].u.hdr
8ebb0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33  .prevSize = mem3
8ebc0 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78  .szMaster;.    x
8ebd0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65   = mem3.aPool[me
8ebe0 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  m3.iMaster-1].u.
8ebf0 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a  hdr.size4x & 2;.
8ec00 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d      mem3.aPool[m
8ec10 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
8ec20 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65  .hdr.size4x = me
8ec30 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20  m3.szMaster*4 | 
8ec40 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e  x;.    if( mem3.
8ec50 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e  szMaster < mem3.
8ec60 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  mnMaster ){.    
8ec70 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20    mem3.mnMaster 
8ec80 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b  = mem3.szMaster;
8ec90 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
8eca0 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61  n (void*)&mem3.a
8ecb0 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a  Pool[newi];.  }.
8ecc0 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20  }../*.** *pRoot 
8ecd0 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 61  is the head of a
8ece0 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 68   list of free ch
8ecf0 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65  unks of the same
8ed00 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65   size.** or same
8ed10 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20   size hash.  In 
8ed20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52  other words, *pR
8ed30 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20  oot is an entry 
8ed40 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d  in either.** mem
8ed50 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d  3.aiSmall[] or m
8ed60 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a  em3.aiHash[].  .
8ed70 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
8ed80 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20  ne examines all 
8ed90 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67  entries on the g
8eda0 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72  iven list and tr
8edb0 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73  ies.** to coales
8edc0 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 20  ce each entries 
8edd0 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72  with adjacent fr
8ede0 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a  ee chunks.  .**.
8edf0 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 20  ** If it sees a 
8ee00 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61  chunk that is la
8ee10 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69  rger than mem3.i
8ee20 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61  Master, it repla
8ee30 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72  ces .** the curr
8ee40 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ent mem3.iMaster
8ee50 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61   with the new la
8ee60 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20  rger chunk.  In 
8ee70 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69  order for.** thi
8ee80 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72  s mem3.iMaster r
8ee90 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f  eplacement to wo
8eea0 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63  rk, the master c
8eeb0 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20  hunk must be.** 
8eec0 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20  linked into the 
8eed0 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68  hash tables.  Th
8eee0 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f  at is not the no
8eef0 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a  rmal state of.**
8ef00 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75   affairs, of cou
8ef10 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  rse.  The callin
8ef20 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c  g routine must l
8ef30 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a  ink the master.*
8ef40 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69  * chunk before i
8ef50 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75  nvoking this rou
8ef60 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20  tine, then must 
8ef70 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73  unlink the (poss
8ef80 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29  ibly.** changed)
8ef90 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e   master chunk on
8efa0 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ce this routine 
8efb0 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f  has finished..*/
8efc0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8efd0 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70  sys3Merge(u32 *p
8efe0 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65  Root){.  u32 iNe
8eff0 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20  xt, prev, size, 
8f000 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28  i, x;..  assert(
8f010 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
8f020 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
8f030 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f  );.  for(i=*pRoo
8f040 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29  t; i>0; i=iNext)
8f050 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65  {.    iNext = me
8f060 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69  m3.aPool[i].u.li
8f070 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a  st.next;.    siz
8f080 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  e = mem3.aPool[i
8f090 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8f0a0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73  ;.    assert( (s
8f0b0 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20  ize&1)==0 );.   
8f0c0 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30   if( (size&2)==0
8f0d0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73   ){.      memsys
8f0e0 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28  3UnlinkFromList(
8f0f0 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20  i, pRoot);.     
8f100 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d   assert( i > mem
8f110 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
8f120 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20  dr.prevSize );. 
8f130 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20       prev = i - 
8f140 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
8f150 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a  u.hdr.prevSize;.
8f160 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d        if( prev==
8f170 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20  iNext ){.       
8f180 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50   iNext = mem3.aP
8f190 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74  ool[prev].u.list
8f1a0 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  .next;.      }. 
8f1b0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
8f1c0 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20  nk(prev);.      
8f1d0 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f  size = i + size/
8f1e0 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20  4 - prev;.      
8f1f0 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70  x = mem3.aPool[p
8f200 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  rev-1].u.hdr.siz
8f210 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d  e4x & 2;.      m
8f220 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31  em3.aPool[prev-1
8f230 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
8f240 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20   size*4 | x;.   
8f250 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72     mem3.aPool[pr
8f260 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72  ev+size-1].u.hdr
8f270 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65  .prevSize = size
8f280 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c  ;.      memsys3L
8f290 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20  ink(prev);.     
8f2a0 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d   i = prev;.    }
8f2b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65  else{.      size
8f2c0 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20   /= 4;.    }.   
8f2d0 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73   if( size>mem3.s
8f2e0 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20  zMaster ){.     
8f2f0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20   mem3.iMaster = 
8f300 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  i;.      mem3.sz
8f310 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20  Master = size;. 
8f320 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
8f330 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b  * Return a block
8f340 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74   of memory of at
8f350 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e   least nBytes in
8f360 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e   size..** Return
8f370 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e   NULL if unable.
8f380 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
8f390 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
8f3a0 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20  t the necessary 
8f3b0 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c  mutexes, if any,
8f3c0 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   are.** already 
8f3d0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
8f3e0 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66  er. Hence "Unsaf
8f3f0 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e"..*/.static vo
8f400 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f  id *memsys3Mallo
8f410 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74  cUnsafe(int nByt
8f420 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75  e){.  u32 i;.  u
8f430 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32  32 nBlock;.  u32
8f440 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65   toFree;..  asse
8f450 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
8f460 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
8f470 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
8f480 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b  sizeof(Mem3Block
8f490 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42  )==8 );.  if( nB
8f4a0 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e  yte<=12 ){.    n
8f4b0 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c  Block = 2;.  }el
8f4c0 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d  se{.    nBlock =
8f4d0 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b   (nByte + 11)/8;
8f4e0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e  .  }.  assert( n
8f4f0 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f  Block>=2 );..  /
8f500 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c  * STEP 1:.  ** L
8f510 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79  ook for an entry
8f520 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20   of the correct 
8f530 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74  size in either t
8f540 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68  he small.  ** ch
8f550 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20  unk table or in 
8f560 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20  the large chunk 
8f570 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69  hash table.  Thi
8f580 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73  s is.  ** succes
8f590 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65  sful most of the
8f5a0 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74   time (about 9 t
8f5b0 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e  imes out of 10).
8f5c0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f  .  */.  if( nBlo
8f5d0 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29  ck <= MX_SMALL )
8f5e0 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61  {.    i = mem3.a
8f5f0 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d  iSmall[nBlock-2]
8f600 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b  ;.    if( i>0 ){
8f610 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  .      memsys3Un
8f620 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20  linkFromList(i, 
8f630 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42  &mem3.aiSmall[nB
8f640 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20  lock-2]);.      
8f650 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68  return memsys3Ch
8f660 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b  eckout(i, nBlock
8f670 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
8f680 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d  {.    int hash =
8f690 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48   nBlock % N_HASH
8f6a0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33  ;.    for(i=mem3
8f6b0 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69  .aiHash[hash]; i
8f6c0 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c  >0; i=mem3.aPool
8f6d0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [i].u.list.next)
8f6e0 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33  {.      if( mem3
8f6f0 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
8f700 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f  r.size4x/4==nBlo
8f710 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  ck ){.        me
8f720 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c  msys3UnlinkFromL
8f730 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48  ist(i, &mem3.aiH
8f740 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20  ash[hash]);.    
8f750 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79      return memsy
8f760 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42  s3Checkout(i, nB
8f770 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lock);.      }. 
8f780 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53     }.  }..  /* S
8f790 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20  TEP 2:.  ** Try 
8f7a0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61  to satisfy the a
8f7b0 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72  llocation by car
8f7c0 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66  ving a piece off
8f7d0 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a   of the end.  **
8f7e0 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   of the master c
8f7f0 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70  hunk.  This step
8f800 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69   usually works i
8f810 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a  f step 1 fails..
8f820 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e    */.  if( mem3.
8f830 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b  szMaster>=nBlock
8f840 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d   ){.    return m
8f850 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72  emsys3FromMaster
8f860 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a  (nBlock);.  }...
8f870 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20    /* STEP 3:  . 
8f880 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   ** Loop through
8f890 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f   the entire memo
8f8a0 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73  ry pool.  Coales
8f8b0 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65  ce adjacent free
8f8c0 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52  .  ** chunks.  R
8f8d0 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73  ecompute the mas
8f8e0 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65  ter chunk as the
8f8f0 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 68   largest free ch
8f900 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74  unk..  ** Then t
8f910 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69  ry again to sati
8f920 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  sfy the allocati
8f930 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 20  on by carving a 
8f940 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f  piece off.  ** o
8f950 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
8f960 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20   master chunk.  
8f970 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 6e  This step happen
8f980 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65  s very.  ** rare
8f990 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20  ly (we hope!).  
8f9a0 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d  */.  for(toFree=
8f9b0 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65  nBlock*16; toFre
8f9c0 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36  e<(mem3.nPool*16
8f9d0 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b  ); toFree *= 2){
8f9e0 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f  .    memsys3OutO
8f9f0 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b  fMemory(toFree);
8fa00 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d  .    if( mem3.iM
8fa10 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
8fa20 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e  emsys3Link(mem3.
8fa30 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  iMaster);.      
8fa40 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30  mem3.iMaster = 0
8fa50 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d  ;.      mem3.szM
8fa60 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d  aster = 0;.    }
8fa70 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
8fa80 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20  N_HASH; i++){.  
8fa90 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65      memsys3Merge
8faa0 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d  (&mem3.aiHash[i]
8fab0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
8fac0 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c  (i=0; i<MX_SMALL
8fad0 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  -1; i++){.      
8fae0 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65  memsys3Merge(&me
8faf0 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a  m3.aiSmall[i]);.
8fb00 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65      }.    if( me
8fb10 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20  m3.szMaster ){. 
8fb20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
8fb30 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29  nk(mem3.iMaster)
8fb40 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33  ;.      if( mem3
8fb50 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63  .szMaster>=nBloc
8fb60 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  k ){.        ret
8fb70 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d  urn memsys3FromM
8fb80 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20  aster(nBlock);. 
8fb90 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
8fba0 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f  ..  /* If none o
8fbb0 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b  f the above work
8fbc0 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c  ed, then we fail
8fbd0 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b  . */.  return 0;
8fbe0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
8fbf0 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65  n outstanding me
8fc00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  mory allocation.
8fc10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
8fc20 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
8fc30 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20  t the necessary 
8fc40 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c  mutexes, if any,
8fc50 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   are.** already 
8fc60 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
8fc70 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66  er. Hence "Unsaf
8fc80 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73  e"..*/.void mems
8fc90 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f  ys3FreeUnsafe(vo
8fca0 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d  id *pOld){.  Mem
8fcb0 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d  3Block *p = (Mem
8fcc0 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20  3Block*)pOld;.  
8fcd0 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a  int i;.  u32 siz
8fce0 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20  e, x;.  assert( 
8fcf0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
8fd00 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29  ld(mem3.mutex) )
8fd10 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65  ;.  assert( p>me
8fd20 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d  m3.aPool && p<&m
8fd30 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e  em3.aPool[mem3.n
8fd40 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70  Pool] );.  i = p
8fd50 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20   - mem3.aPool;. 
8fd60 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61   assert( (mem3.a
8fd70 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
8fd80 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a  size4x&1)==1 );.
8fd90 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50    size = mem3.aP
8fda0 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
8fdb0 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72  ize4x/4;.  asser
8fdc0 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e  t( i+size<=mem3.
8fdd0 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d  nPool+1 );.  mem
8fde0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
8fdf0 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b  dr.size4x &= ~1;
8fe00 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b  .  mem3.aPool[i+
8fe10 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  size-1].u.hdr.pr
8fe20 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20  evSize = size;. 
8fe30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69   mem3.aPool[i+si
8fe40 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  ze-1].u.hdr.size
8fe50 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73  4x &= ~2;.  mems
8fe60 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f  ys3Link(i);..  /
8fe70 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20  * Try to expand 
8fe80 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67  the master using
8fe90 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64   the newly freed
8fea0 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20   chunk */.  if( 
8feb0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a  mem3.iMaster ){.
8fec0 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33      while( (mem3
8fed0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8fee0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
8fef0 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20  e4x&2)==0 ){.   
8ff00 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61     size = mem3.a
8ff10 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8ff20 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  r-1].u.hdr.prevS
8ff30 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  ize;.      mem3.
8ff40 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b  iMaster -= size;
8ff50 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61  .      mem3.szMa
8ff60 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20  ster += size;.  
8ff70 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
8ff80 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b  k(mem3.iMaster);
8ff90 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e  .      x = mem3.
8ffa0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8ffb0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
8ffc0 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65  4x & 2;.      me
8ffd0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
8ffe0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73  aster-1].u.hdr.s
8fff0 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d  ize4x = mem3.szM
90000 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20  aster*4 | x;.   
90010 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65     mem3.aPool[me
90020 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e  m3.iMaster+mem3.
90030 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  szMaster-1].u.hd
90040 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d  r.prevSize = mem
90050 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20  3.szMaster;.    
90060 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61  }.    x = mem3.a
90070 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
90080 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
90090 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65  x & 2;.    while
900a0 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65  ( (mem3.aPool[me
900b0 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e  m3.iMaster+mem3.
900c0 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  szMaster-1].u.hd
900d0 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29  r.size4x&1)==0 )
900e0 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55  {.      memsys3U
900f0 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74  nlink(mem3.iMast
90100 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  er+mem3.szMaster
90110 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  );.      mem3.sz
90120 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61  Master += mem3.a
90130 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
90140 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d  r+mem3.szMaster-
90150 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
90160 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  4;.      mem3.aP
90170 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
90180 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
90190 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   = mem3.szMaster
901a0 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65  *4 | x;.      me
901b0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
901c0 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73  aster+mem3.szMas
901d0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ter-1].u.hdr.pre
901e0 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d  vSize = mem3.szM
901f0 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  aster;.    }.  }
90200 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
90210 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20   the size of an 
90220 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f  outstanding allo
90230 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73  cation, in bytes
90240 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72  .  The.** size r
90250 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68  eturned omits th
90260 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 20  e 8-byte header 
90270 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 20  overhead.  This 
90280 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f  only.** works fo
90290 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72  r chunks that ar
902a0 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63  e currently chec
902b0 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74  ked out..*/.stat
902c0 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69  ic int memsys3Si
902d0 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d  ze(void *p){.  M
902e0 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b  em3Block *pBlock
902f0 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
90300 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63  eturn 0;.  pBloc
90310 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29  k = (Mem3Block*)
90320 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 42  p;.  assert( (pB
90330 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73  lock[-1].u.hdr.s
90340 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20  ize4x&1)!=0 );. 
90350 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b   return (pBlock[
90360 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
90370 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f  &~3)*2 - 4;.}../
90380 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20  *.** Round up a 
90390 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20  request size to 
903a0 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61  the next valid a
903b0 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a  llocation size..
903c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
903d0 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74  msys3Roundup(int
903e0 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32   n){.  if( n<=12
903f0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
90400 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  2;.  }else{.    
90410 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e  return ((n+11)&~
90420 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f  7) - 4;.  }.}../
90430 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42  *.** Allocate nB
90440 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a  ytes of memory..
90450 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
90460 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e  memsys3Malloc(in
90470 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c  t nBytes){.  sql
90480 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20  ite3_int64 *p;. 
90490 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e   assert( nBytes>
904a0 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  0 );          /*
904b0 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72   malloc.c filter
904c0 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65 71  s out 0 byte req
904d0 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79  uests */.  memsy
904e0 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d  s3Enter();.  p =
904f0 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e   memsys3MallocUn
90500 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20  safe(nBytes);.  
90510 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a  memsys3Leave();.
90520 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
90530 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  p; .}../*.** Fre
90540 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69  e memory..*/.voi
90550 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f  d memsys3Free(vo
90560 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61  id *pPrior){.  a
90570 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b  ssert( pPrior );
90580 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28  .  memsys3Enter(
90590 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65  );.  memsys3Free
905a0 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a  Unsafe(pPrior);.
905b0 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29    memsys3Leave()
905c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
905d0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  e the size of an
905e0 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79   existing memory
905f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76   allocation.*/.v
90600 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c  oid *memsys3Real
90610 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  loc(void *pPrior
90620 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20  , int nBytes){. 
90630 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69   int nOld;.  voi
90640 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69  d *p;.  if( pPri
90650 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  or==0 ){.    ret
90660 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  urn sqlite3_mall
90670 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a  oc(nBytes);.  }.
90680 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20    if( nBytes<=0 
90690 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
906a0 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20  ree(pPrior);.   
906b0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
906c0 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53   nOld = memsys3S
906d0 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69  ize(pPrior);.  i
906e0 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20  f( nBytes<=nOld 
906f0 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d  && nBytes>=nOld-
90700 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72  128 ){.    retur
90710 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20  n pPrior;.  }.  
90720 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a  memsys3Enter();.
90730 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c    p = memsys3Mal
90740 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73  locUnsafe(nBytes
90750 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
90760 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65    if( nOld<nByte
90770 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  s ){.      memcp
90780 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c  y(p, pPrior, nOl
90790 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
907a0 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70       memcpy(p, p
907b0 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a  Prior, nBytes);.
907c0 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73      }.    memsys
907d0 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69  3FreeUnsafe(pPri
907e0 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79  or);.  }.  memsy
907f0 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74  s3Leave();.  ret
90800 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
90810 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20  Initialize this 
90820 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  module..*/.stati
90830 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69  c int memsys3Ini
90840 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
90850 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
90860 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
90870 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f   if( !sqlite3Glo
90880 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 20  balConfig.pHeap 
90890 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
908a0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
908b0 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 6f  .  /* Store a po
908c0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d  inter to the mem
908d0 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f  ory block in glo
908e0 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d 65  bal structure me
908f0 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  m3. */.  assert(
90900 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63   sizeof(Mem3Bloc
90910 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 2e  k)==8 );.  mem3.
90920 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f  aPool = (Mem3Blo
90930 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  ck *)sqlite3Glob
90940 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a  alConfig.pHeap;.
90950 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28    mem3.nPool = (
90960 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
90970 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a 65  fig.nHeap / size
90980 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 2d  of(Mem3Block)) -
90990 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61   2;..  /* Initia
909a0 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72 20  lize the master 
909b0 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33  block. */.  mem3
909c0 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33  .szMaster = mem3
909d0 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d  .nPool;.  mem3.m
909e0 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73  nMaster = mem3.s
909f0 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 2e  zMaster;.  mem3.
90a00 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 6d  iMaster = 1;.  m
90a10 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68  em3.aPool[0].u.h
90a20 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 6d  dr.size4x = (mem
90a30 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 2b  3.szMaster<<2) +
90a40 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c   2;.  mem3.aPool
90a50 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68  [mem3.nPool].u.h
90a60 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65  dr.prevSize = me
90a70 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33  m3.nPool;.  mem3
90a80 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f  .aPool[mem3.nPoo
90a90 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  l].u.hdr.size4x 
90aa0 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  = 1;..  return S
90ab0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
90ac0 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20  ** Deinitialize 
90ad0 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  this module..*/.
90ae0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
90af0 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 64  ys3Shutdown(void
90b00 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
90b10 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
90b20 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
90b30 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70  n;.}..../*.** Op
90b40 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69  en the file indi
90b50 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20  cated and write 
90b60 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66  a log of all unf
90b70 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20  reed memory .** 
90b80 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f  allocations into
90b90 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51   that log..*/.SQ
90ba0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
90bb0 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 33  d sqlite3Memsys3
90bc0 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20  Dump(const char 
90bd0 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 69 66  *zFilename){.#if
90be0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
90bf0 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20  .  FILE *out;.  
90c00 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 33 32 20  u32 i, j;.  u32 
90c10 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 46 69 6c  size;.  if( zFil
90c20 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c  ename==0 || zFil
90c30 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20  ename[0]==0 ){. 
90c40 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b     out = stdout;
90c50 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75  .  }else{.    ou
90c60 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e  t = fopen(zFilen
90c70 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69  ame, "w");.    i
90c80 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20  f( out==0 ){.   
90c90 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
90ca0 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f  r, "** Unable to
90cb0 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64   output memory d
90cc0 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a  ebug output log:
90cd0 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20   %s **\n",.     
90ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
90cf0 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20   zFilename);.   
90d00 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
90d10 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e  .  }.  memsys3En
90d20 74 65 72 28 29 3b 0a 20 20 66 70 72 69 6e 74 66  ter();.  fprintf
90d30 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e  (out, "CHUNKS:\n
90d40 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69  ");.  for(i=1; i
90d50 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b  <=mem3.nPool; i+
90d60 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 20 73 69  =size/4){.    si
90d70 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
90d80 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
90d90 78 3b 0a 20 20 20 20 69 66 28 20 73 69 7a 65 2f  x;.    if( size/
90da0 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 70  4<=1 ){.      fp
90db0 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 73  rintf(out, "%p s
90dc0 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d  ize error\n", &m
90dd0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20  em3.aPool[i]);. 
90de0 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29       assert( 0 )
90df0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
90e00 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 73 69     }.    if( (si
90e10 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d 65 6d 33  ze&1)==0 && mem3
90e20 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d  .aPool[i+size/4-
90e30 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
90e40 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 20 20  e!=size/4 ){.   
90e50 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
90e60 22 25 70 20 74 61 69 6c 20 73 69 7a 65 20 64 6f  "%p tail size do
90e70 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c 6e 22 2c  es not match\n",
90e80 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29   &mem3.aPool[i])
90e90 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
90ea0 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
90eb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
90ec0 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73  ((mem3.aPool[i+s
90ed0 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 73  ize/4-1].u.hdr.s
90ee0 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 3d 28 73  ize4x&2)>>1)!=(s
90ef0 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 20 20 20  ize&1) ){.      
90f00 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70  fprintf(out, "%p
90f10 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 74 20 62   tail checkout b
90f20 69 74 20 69 73 20 69 6e 63 6f 72 72 65 63 74 5c  it is incorrect\
90f30 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  n", &mem3.aPool[
90f40 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  i]);.      asser
90f50 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72  t( 0 );.      br
90f60 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
90f70 66 28 20 73 69 7a 65 26 31 20 29 7b 0a 20 20 20  f( size&1 ){.   
90f80 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
90f90 22 25 70 20 25 36 64 20 62 79 74 65 73 20 63 68  "%p %6d bytes ch
90fa0 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 26 6d  ecked out\n", &m
90fb0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73  em3.aPool[i], (s
90fc0 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20  ize/4)*8-8);.   
90fd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 70   }else{.      fp
90fe0 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25  rintf(out, "%p %
90ff0 36 64 20 62 79 74 65 73 20 66 72 65 65 25 73 5c  6d bytes free%s\
91000 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  n", &mem3.aPool[
91010 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38  i], (size/4)*8-8
91020 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
91030 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73      i==mem3.iMas
91040 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 74 65 72  ter ? " **master
91050 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 20 20 7d  **" : "");.    }
91060 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
91070 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b  i<MX_SMALL-1; i+
91080 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33  +){.    if( mem3
91090 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29  .aiSmall[i]==0 )
910a0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66   continue;.    f
910b0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 73 6d 61  printf(out, "sma
910c0 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20  ll(%2d):", i);. 
910d0 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e     for(j = mem3.
910e0 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b  aiSmall[i]; j>0;
910f0 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d   j=mem3.aPool[j]
91100 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20  .u.list.next){. 
91110 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
91120 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65  , " %p(%d)", &me
91130 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20  m3.aPool[j],.   
91140 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33             (mem3
91150 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64  .aPool[j-1].u.hd
91160 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29  r.size4x/4)*8-8)
91170 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69  ;.    }.    fpri
91180 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20  ntf(out, "\n"); 
91190 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
911a0 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a  i<N_HASH; i++){.
911b0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 48      if( mem3.aiH
911c0 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  ash[i]==0 ) cont
911d0 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74  inue;.    fprint
911e0 66 28 6f 75 74 2c 20 22 68 61 73 68 28 25 32 64  f(out, "hash(%2d
911f0 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72  ):", i);.    for
91200 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 61 73 68  (j = mem3.aiHash
91210 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33  [i]; j>0; j=mem3
91220 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74  .aPool[j].u.list
91230 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70  .next){.      fp
91240 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28  rintf(out, " %p(
91250 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f  %d)", &mem3.aPoo
91260 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  l[j],.          
91270 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b      (mem3.aPool[
91280 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  j-1].u.hdr.size4
91290 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d  x/4)*8-8);.    }
912a0 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
912b0 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20  , "\n"); .  }.  
912c0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 61  fprintf(out, "ma
912d0 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33  ster=%d\n", mem3
912e0 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 66 70 72  .iMaster);.  fpr
912f0 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f 77 55 73  intf(out, "nowUs
91300 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e  ed=%d\n", mem3.n
91310 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a  Pool*8 - mem3.sz
91320 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 66 70 72  Master*8);.  fpr
91330 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 55 73 65  intf(out, "mxUse
91340 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50  d=%d\n", mem3.nP
91350 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d  ool*8 - mem3.mnM
91360 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 71 6c 69  aster*8);.  sqli
91370 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
91380 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 69  mem3.mutex);.  i
91390 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29  f( out==stdout )
913a0 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 74 64  {.    fflush(std
913b0 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  out);.  }else{. 
913c0 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a     fclose(out);.
913d0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53    }.#else.  UNUS
913e0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 69  ED_PARAMETER(zFi
913f0 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a  lename);.#endif.
91400 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
91410 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c  utine is the onl
91420 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69  y routine in thi
91430 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65  s file with exte
91440 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65  rnal .** linkage
91450 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65  ..**.** Populate
91460 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d   the low-level m
91470 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
91480 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65   function pointe
91490 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  rs in.** sqlite3
914a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77  GlobalConfig.m w
914b0 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ith pointers to 
914c0 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  the routines in 
914d0 74 68 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a  this file. The.*
914e0 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63  * arguments spec
914f0 69 66 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66  ify the block of
91500 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67   memory to manag
91510 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
91520 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61  utine is only ca
91530 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  lled by sqlite3_
91540 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68  config(), and th
91550 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f  erefore.** is no
91560 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65  t required to be
91570 20 74 68 72 65 61 64 73 61 66 65 20 28 69 74 20   threadsafe (it 
91580 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49  is not)..*/.SQLI
91590 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
915a0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
915b0 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d  hods *sqlite3Mem
915c0 47 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29  GetMemsys3(void)
915d0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
915e0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
915f0 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68  hods mempoolMeth
91600 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d  ods = {.     mem
91610 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20  sys3Malloc,.    
91620 20 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20   memsys3Free,.  
91630 20 20 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f     memsys3Reallo
91640 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53  c,.     memsys3S
91650 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  ize,.     memsys
91660 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d  3Roundup,.     m
91670 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20  emsys3Init,.    
91680 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e   memsys3Shutdown
91690 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20  ,.     0.  };.  
916a0 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d  return &mempoolM
916b0 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69  ethods;.}..#endi
916c0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42  f /* SQLITE_ENAB
916d0 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f  LE_MEMSYS3 */../
916e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
916f0 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a  nd of mem3.c ***
91700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
91730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
91740 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63  egin file mem5.c
91750 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
91760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
91780 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65  *.** 2007 Octobe
91790 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 14.**.** The a
917a0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
917b0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
917c0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
917d0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
917e0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
917f0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
91800 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
91810 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
91820 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
91830 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
91840 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
91850 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
91860 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
91870 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
91880 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
91890 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
918a0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
918b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
918c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
918d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
918e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
918f0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
91900 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
91910 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
91920 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72  mplement a memor
91930 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y.** allocation 
91940 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73  subsystem for us
91950 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a  e by SQLite. .**
91960 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
91970 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
91980 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
91990 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a  tem omits all.**
919a0 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29   use of malloc()
919b0 2e 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65  . The SQLite use
919c0 72 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f  r supplies a blo
919d0 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ck of memory.** 
919e0 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73  before calling s
919f0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
91a00 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61  e() from which a
91a10 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72  llocations.** ar
91a20 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72  e made and retur
91a30 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c  ned by the xMall
91a40 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f  oc() and xReallo
91a50 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  c() .** implemen
91a60 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71  tations. Once sq
91a70 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
91a80 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  () has been call
91a90 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e  ed,.** the amoun
91aa0 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69  t of memory avai
91ab0 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20  lable to SQLite 
91ac0 69 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e  is fixed and can
91ad0 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65  not.** be change
91ae0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65  d..**.** This ve
91af0 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d  rsion of the mem
91b00 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
91b10 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c  ubsystem is incl
91b20 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62  uded.** in the b
91b30 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c  uild only if SQL
91b40 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
91b50 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  S5 is defined..*
91b60 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 35 2e 63  *.** $Id: mem5.c
91b70 2c 76 20 31 2e 31 39 20 32 30 30 38 2f 31 31 2f  ,v 1.19 2008/11/
91b80 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69  19 16:52:44 dani
91b90 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
91ba0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
91bb0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
91bc0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
91bd0 75 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a  used only when .
91be0 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ** SQLITE_ENABLE
91bf0 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69  _MEMSYS5 is defi
91c00 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ned..*/.#ifdef S
91c10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
91c20 53 59 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69  SYS5../*.** A mi
91c30 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e  nimum allocation
91c40 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
91c50 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
91c60 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c   structure..** L
91c70 61 72 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  arger allocation
91c80 73 20 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f  s are an array o
91c90 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
91ca0 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20  es where the.** 
91cb0 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61  size of the arra
91cc0 79 20 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20  y is a power of 
91cd0 32 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  2..*/.typedef st
91ce0 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65  ruct Mem5Link Me
91cf0 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d  m5Link;.struct M
91d00 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20  em5Link {.  int 
91d10 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49  next;       /* I
91d20 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65  ndex of next fre
91d30 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74  e chunk */.  int
91d40 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20   prev;       /* 
91d50 49 6e 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75  Index of previou
91d60 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a  s free chunk */.
91d70 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75  };../*.** Maximu
91d80 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c  m size of any al
91d90 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c  location is ((1<
91da0 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 6e 41  <LOGMAX)*mem5.nA
91db0 74 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d  tom). Since.** m
91dc0 65 6d 35 2e 6e 41 74 6f 6d 20 69 73 20 61 6c 77  em5.nAtom is alw
91dd0 61 79 73 20 61 74 20 6c 65 61 73 74 20 38 2c 20  ays at least 8, 
91de0 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c  this is not real
91df0 6c 79 20 61 20 70 72 61 63 74 69 63 61 6c 0a 2a  ly a practical.*
91e00 2a 20 6c 69 6d 69 74 61 74 69 6f 6e 2e 0a 2a 2f  * limitation..*/
91e10 0a 23 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20  .#define LOGMAX 
91e20 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20  30../*.** Masks 
91e30 75 73 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43  used for mem5.aC
91e40 74 72 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a  trl[] elements..
91e50 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f  */.#define CTRL_
91e60 4c 4f 47 53 49 5a 45 20 20 30 78 31 66 20 20 20  LOGSIZE  0x1f   
91e70 20 2f 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66   /* Log2 Size of
91e80 20 74 68 69 73 20 62 6c 6f 63 6b 20 72 65 6c 61   this block rela
91e90 74 69 76 65 20 74 6f 20 50 4f 57 32 5f 4d 49 4e  tive to POW2_MIN
91ea0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c   */.#define CTRL
91eb0 5f 46 52 45 45 20 20 20 20 20 30 78 32 30 20 20  _FREE     0x20  
91ec0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74    /* True if not
91ed0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
91ee0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  ./*.** All of th
91ef0 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  e static variabl
91f00 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  es used by this 
91f10 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65  module are colle
91f20 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73  cted.** into a s
91f30 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20  ingle structure 
91f40 6e 61 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54  named "mem5".  T
91f50 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74  his is to keep t
91f60 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72  he.** static var
91f70 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64  iables organized
91f80 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e   and to reduce n
91f90 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69  amespace polluti
91fa0 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  on.** when this 
91fb0 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e  module is combin
91fc0 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e  ed with other in
91fd0 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f   the amalgamatio
91fe0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c  n..*/.static SQL
91ff0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d  ITE_WSD struct M
92000 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a  em5Global {.  /*
92010 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61  .  ** Memory ava
92020 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63  ilable for alloc
92030 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74  ation.  */.  int
92040 20 6e 41 74 6f 6d 3b 20 20 20 20 20 20 20 2f 2a   nAtom;       /*
92050 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62   Smallest possib
92060 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e  le allocation in
92070 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20   bytes */.  int 
92080 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20  nBlock;      /* 
92090 4e 75 6d 62 65 72 20 6f 66 20 6e 41 74 6f 6d 20  Number of nAtom 
920a0 73 69 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20  sized blocks in 
920b0 7a 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a  zPool */.  u8 *z
920c0 50 6f 6f 6c 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20  Pool;.  .  /*.  
920d0 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74  ** Mutex to cont
920e0 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68  rol access to th
920f0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
92100 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20  ion subsystem.. 
92110 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
92120 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f  tex *mutex;..  /
92130 2a 0a 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e  *.  ** Performan
92140 63 65 20 73 74 61 74 69 73 74 69 63 73 0a 20 20  ce statistics.  
92150 2a 2f 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b  */.  u64 nAlloc;
92160 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
92170 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c  l number of call
92180 73 20 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20  s to malloc */. 
92190 20 75 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b   u64 totalAlloc;
921a0 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66       /* Total of
921b0 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c   all malloc call
921c0 73 20 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74  s - includes int
921d0 65 72 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20  ernal frag */.  
921e0 75 36 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b  u64 totalExcess;
921f0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74      /* Total int
92200 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74  ernal fragmentat
92210 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72  ion */.  u32 cur
92220 72 65 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20  rentOut;     /* 
92230 43 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74  Current checkout
92240 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65  , including inte
92250 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69  rnal fragmentati
92260 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72  on */.  u32 curr
92270 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43  entCount;   /* C
92280 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  urrent number of
92290 20 64 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f   distinct checko
922a0 75 74 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78  uts */.  u32 max
922b0 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Out;         /* 
922c0 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61  Maximum instanta
922d0 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74  neous currentOut
922e0 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75   */.  u32 maxCou
922f0 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78  nt;       /* Max
92300 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f  imum instantaneo
92310 75 73 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20  us currentCount 
92320 2a 2f 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75  */.  u32 maxRequ
92330 65 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67  est;     /* Larg
92340 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28  est allocation (
92350 65 78 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74  exclusive of int
92360 65 72 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20  ernal frag) */. 
92370 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74   .  /*.  ** List
92380 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73  s of free blocks
92390 20 6f 66 20 76 61 72 69 6f 75 73 20 73 69 7a 65   of various size
923a0 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69  s..  */.  int ai
923b0 46 72 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b  Freelist[LOGMAX+
923c0 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53  1];..  /*.  ** S
923d0 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e  pace for trackin
923e0 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61  g which blocks a
923f0 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61  re checked out a
92400 6e 64 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a  nd the size.  **
92410 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20   of each block. 
92420 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c   One byte per bl
92430 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a  ock..  */.  u8 *
92440 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d  aCtrl;..} mem5 =
92450 20 7b 20 31 39 38 30 34 31 36 37 20 7d 3b 0a 0a   { 19804167 };..
92460 23 64 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c 4f  #define mem5 GLO
92470 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 35 47  BAL(struct Mem5G
92480 6c 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 23 64  lobal, mem5)..#d
92490 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69  efine MEM5LINK(i
924a0 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a  dx) ((Mem5Link *
924b0 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69  )(&mem5.zPool[(i
924c0 64 78 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 29  dx)*mem5.nAtom])
924d0 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  )../*.** Unlink 
924e0 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d  the chunk at mem
924f0 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20  5.aPool[i] from 
92500 6c 69 73 74 20 69 74 20 69 73 20 63 75 72 72 65  list it is curre
92510 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20  ntly.** on.  It 
92520 73 68 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64 20  should be found 
92530 6f 6e 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69  on mem5.aiFreeli
92540 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f  st[iLogsize]..*/
92550 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
92560 73 79 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69  sys5Unlink(int i
92570 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b  , int iLogsize){
92580 0a 20 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 65  .  int next, pre
92590 76 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  v;.  assert( i>=
925a0 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f  0 && i<mem5.nBlo
925b0 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ck );.  assert( 
925c0 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69  iLogsize>=0 && i
925d0 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20  Logsize<=LOGMAX 
925e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65  );.  assert( (me
925f0 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54  m5.aCtrl[i] & CT
92600 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f  RL_LOGSIZE)==iLo
92610 67 73 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74  gsize );..  next
92620 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e   = MEM5LINK(i)->
92630 6e 65 78 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d  next;.  prev = M
92640 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76  EM5LINK(i)->prev
92650 3b 0a 20 20 69 66 28 20 70 72 65 76 3c 30 20 29  ;.  if( prev<0 )
92660 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65  {.    mem5.aiFre
92670 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20  elist[iLogsize] 
92680 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b  = next;.  }else{
92690 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72  .    MEM5LINK(pr
926a0 65 76 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74  ev)->next = next
926b0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74  ;.  }.  if( next
926c0 3e 3d 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c  >=0 ){.    MEM5L
926d0 49 4e 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76 20  INK(next)->prev 
926e0 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f  = prev;.  }.}../
926f0 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68  *.** Link the ch
92700 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f  unk at mem5.aPoo
92710 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20  l[i] so that is 
92720 6f 6e 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a  on the iLogsize.
92730 2a 2a 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f  ** free list..*/
92740 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
92750 73 79 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20  sys5Link(int i, 
92760 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20  int iLogsize){. 
92770 20 69 6e 74 20 78 3b 0a 20 20 61 73 73 65 72 74   int x;.  assert
92780 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
92790 68 65 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29  held(mem5.mutex)
927a0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e   );.  assert( i>
927b0 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c  =0 && i<mem5.nBl
927c0 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ock );.  assert(
927d0 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20   iLogsize>=0 && 
927e0 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58  iLogsize<=LOGMAX
927f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d   );.  assert( (m
92800 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43  em5.aCtrl[i] & C
92810 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c  TRL_LOGSIZE)==iL
92820 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d  ogsize );..  x =
92830 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65   MEM5LINK(i)->ne
92840 78 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65  xt = mem5.aiFree
92850 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a  list[iLogsize];.
92860 20 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70    MEM5LINK(i)->p
92870 72 65 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20  rev = -1;.  if( 
92880 78 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  x>=0 ){.    asse
92890 72 74 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63  rt( x<mem5.nBloc
928a0 6b 20 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e  k );.    MEM5LIN
928b0 4b 28 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a  K(x)->prev = i;.
928c0 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65    }.  mem5.aiFre
928d0 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20  elist[iLogsize] 
928e0 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  = i;.}../*.** If
928f0 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20   the STATIC_MEM 
92900 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72  mutex is not alr
92910 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69  eady held, obtai
92920 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75  n it now. The mu
92930 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65  tex.** will alre
92940 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74  ady be held (obt
92950 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e  ained by code in
92960 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a   malloc.c) if.**
92970 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
92980 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73  nfig.bMemStat is
92990 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   true..*/.static
929a0 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e 74   void memsys5Ent
929b0 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  er(void){.  if( 
929c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
929d0 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20  fig.bMemstat==0 
929e0 26 26 20 6d 65 6d 35 2e 6d 75 74 65 78 3d 3d 30  && mem5.mutex==0
929f0 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74   ){.    mem5.mut
92a00 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
92a10 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
92a20 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b  TEX_STATIC_MEM);
92a30 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
92a40 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e  utex_enter(mem5.
92a50 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63  mutex);.}.static
92a60 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 65 61   void memsys5Lea
92a70 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ve(void){.  sqli
92a80 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
92a90 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a  mem5.mutex);.}..
92aa0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
92ab0 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73   size of an outs
92ac0 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69  tanding allocati
92ad0 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54  on, in bytes.  T
92ae0 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72  he.** size retur
92af0 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d  ned omits the 8-
92b00 62 79 74 65 20 68 65 61 64 65 72 20 6f 76 65 72  byte header over
92b10 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79  head.  This only
92b20 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68  .** works for ch
92b30 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20 63 75  unks that are cu
92b40 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20  rrently checked 
92b50 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  out..*/.static i
92b60 6e 74 20 6d 65 6d 73 79 73 35 53 69 7a 65 28 76  nt memsys5Size(v
92b70 6f 69 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  oid *p){.  int i
92b80 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66 28 20  Size = 0;.  if( 
92b90 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d  p ){.    int i =
92ba0 20 28 28 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a   ((u8 *)p-mem5.z
92bb0 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d  Pool)/mem5.nAtom
92bc0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e  ;.    assert( i>
92bd0 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c  =0 && i<mem5.nBl
92be0 6f 63 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65  ock );.    iSize
92bf0 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 2a 20   = mem5.nAtom * 
92c00 28 31 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72  (1 << (mem5.aCtr
92c10 6c 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a  l[i]&CTRL_LOGSIZ
92c20 45 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  E));.  }.  retur
92c30 6e 20 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  n iSize;.}../*.*
92c40 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74  * Find the first
92c50 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72   entry on the fr
92c60 65 65 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e  eelist iLogsize.
92c70 20 20 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a    Unlink that.**
92c80 20 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72   entry and retur
92c90 6e 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f  n its index. .*/
92ca0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73  .static int mems
92cb0 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69  ys5UnlinkFirst(i
92cc0 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20  nt iLogsize){.  
92cd0 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69  int i;.  int iFi
92ce0 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  rst;..  assert( 
92cf0 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69  iLogsize>=0 && i
92d00 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20  Logsize<=LOGMAX 
92d10 29 3b 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20  );.  i = iFirst 
92d20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73  = mem5.aiFreelis
92d30 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61  t[iLogsize];.  a
92d40 73 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30  ssert( iFirst>=0
92d50 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30   );.  while( i>0
92d60 20 29 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46   ){.    if( i<iF
92d70 69 72 73 74 20 29 20 69 46 69 72 73 74 20 3d 20  irst ) iFirst = 
92d80 69 3b 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c  i;.    i = MEM5L
92d90 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20  INK(i)->next;.  
92da0 7d 0a 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e  }.  memsys5Unlin
92db0 6b 28 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69  k(iFirst, iLogsi
92dc0 7a 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46  ze);.  return iF
92dd0 69 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  irst;.}../*.** R
92de0 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66  eturn a block of
92df0 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65   memory of at le
92e00 61 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69  ast nBytes in si
92e10 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55  ze..** Return NU
92e20 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2f  LL if unable..*/
92e30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
92e40 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66  msys5MallocUnsaf
92e50 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  e(int nByte){.  
92e60 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
92e70 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d   /* Index of a m
92e80 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74  em5.aPool[] slot
92e90 20 2a 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20   */.  int iBin; 
92ea0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
92eb0 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65  into mem5.aiFree
92ec0 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  list[] */.  int 
92ed0 69 46 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20  iFullSz;     /* 
92ee0 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69  Size of allocati
92ef0 6f 6e 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f  on rounded up to
92f00 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20   power of 2 */. 
92f10 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20   int iLogsize;  
92f20 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75    /* Log2 of iFu
92f30 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f  llSz/POW2_MIN */
92f40 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 74 72 61 63  ..  /* Keep trac
92f50 6b 20 6f 66 20 74 68 65 20 6d 61 78 69 6d 75 6d  k of the maximum
92f60 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75   allocation requ
92f70 65 73 74 2e 20 20 45 76 65 6e 20 75 6e 66 75 6c  est.  Even unful
92f80 66 69 6c 6c 65 64 0a 20 20 2a 2a 20 72 65 71 75  filled.  ** requ
92f90 65 73 74 73 20 61 72 65 20 63 6f 75 6e 74 65 64  ests are counted
92fa0 20 2a 2f 0a 20 20 69 66 28 20 28 75 33 32 29 6e   */.  if( (u32)n
92fb0 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78 52 65 71  Byte>mem5.maxReq
92fc0 75 65 73 74 20 29 7b 0a 20 20 20 20 6d 65 6d 35  uest ){.    mem5
92fd0 2e 6d 61 78 52 65 71 75 65 73 74 20 3d 20 6e 42  .maxRequest = nB
92fe0 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  yte;.  }..  /* R
92ff0 6f 75 6e 64 20 6e 42 79 74 65 20 75 70 20 74 6f  ound nByte up to
93000 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20   the next valid 
93010 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a  power of two */.
93020 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65    for(iFullSz=me
93030 6d 35 2e 6e 41 74 6f 6d 2c 20 69 4c 6f 67 73 69  m5.nAtom, iLogsi
93040 7a 65 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42  ze=0; iFullSz<nB
93050 79 74 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20  yte; iFullSz *= 
93060 32 2c 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d  2, iLogsize++){}
93070 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
93080 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
93090 5b 69 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61  [iLogsize] conta
930a0 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  ins at least one
930b0 20 66 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b   free.  ** block
930c0 2e 20 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20  .  If not, then 
930d0 73 70 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66  split a block of
930e0 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72   the next larger
930f0 20 70 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74   power of.  ** t
93100 77 6f 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63  wo in order to c
93110 72 65 61 74 65 20 61 20 6e 65 77 20 66 72 65 65  reate a new free
93120 20 62 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69   block of size i
93130 4c 6f 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20  Logsize..  */.  
93140 66 6f 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a  for(iBin=iLogsiz
93150 65 3b 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69  e; mem5.aiFreeli
93160 73 74 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42  st[iBin]<0 && iB
93170 69 6e 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e  in<=LOGMAX; iBin
93180 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e  ++){}.  if( iBin
93190 3e 4c 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e  >LOGMAX ) return
931a0 20 30 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73   0;.  i = memsys
931b0 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69  5UnlinkFirst(iBi
931c0 6e 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69  n);.  while( iBi
931d0 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20  n>iLogsize ){.  
931e0 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a    int newSize;..
931f0 20 20 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20      iBin--;.    
93200 6e 65 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69  newSize = 1 << i
93210 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43  Bin;.    mem5.aC
93220 74 72 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d  trl[i+newSize] =
93230 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69   CTRL_FREE | iBi
93240 6e 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69  n;.    memsys5Li
93250 6e 6b 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42  nk(i+newSize, iB
93260 69 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e  in);.  }.  mem5.
93270 61 43 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73  aCtrl[i] = iLogs
93280 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74  ize;..  /* Updat
93290 65 20 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66  e allocator perf
932a0 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69  ormance statisti
932b0 63 73 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41  cs. */.  mem5.nA
932c0 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74  lloc++;.  mem5.t
932d0 6f 74 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75  otalAlloc += iFu
932e0 6c 6c 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74  llSz;.  mem5.tot
932f0 61 6c 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c  alExcess += iFul
93300 6c 53 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d  lSz - nByte;.  m
93310 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
93320 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65  ++;.  mem5.curre
93330 6e 74 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a  ntOut += iFullSz
93340 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78  ;.  if( mem5.max
93350 43 6f 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65  Count<mem5.curre
93360 6e 74 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d  ntCount ) mem5.m
93370 61 78 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63  axCount = mem5.c
93380 75 72 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69  urrentCount;.  i
93390 66 28 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d  f( mem5.maxOut<m
933a0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29  em5.currentOut )
933b0 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d   mem5.maxOut = m
933c0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a  em5.currentOut;.
933d0 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70  .  /* Return a p
933e0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c  ointer to the al
933f0 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20  located memory. 
93400 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69  */.  return (voi
93410 64 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69  d*)&mem5.zPool[i
93420 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 7d 0a  *mem5.nAtom];.}.
93430 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f  ./*.** Free an o
93440 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72  utstanding memor
93450 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f  y allocation..*/
93460 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
93470 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 76  sys5FreeUnsafe(v
93480 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33  oid *pOld){.  u3
93490 32 20 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65  2 size, iLogsize
934a0 3b 0a 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 20  ;.  int iBlock; 
934b0 20 20 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20              ..  
934c0 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 74 6f  /* Set iBlock to
934d0 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
934e0 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 20  e block pointed 
934f0 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a 20  to by pOld in . 
93500 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 6f 66   ** the array of
93510 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 62 79 74 65   mem5.nAtom byte
93520 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 64 20   blocks pointed 
93530 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c  to by mem5.zPool
93540 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20  ..  */.  iBlock 
93550 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65  = ((u8 *)pOld-me
93560 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e  m5.zPool)/mem5.n
93570 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  Atom;..  /* Chec
93580 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74  k that the point
93590 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74  er pOld points t
935a0 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66  o a valid, non-f
935b0 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20  ree block. */.  
935c0 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d  assert( iBlock>=
935d0 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35  0 && iBlock<mem5
935e0 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73  .nBlock );.  ass
935f0 65 72 74 28 20 28 28 75 38 20 2a 29 70 4f 6c 64  ert( ((u8 *)pOld
93600 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d  -mem5.zPool)%mem
93610 35 2e 6e 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20  5.nAtom==0 );.  
93620 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43  assert( (mem5.aC
93630 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54  trl[iBlock] & CT
93640 52 4c 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a  RL_FREE)==0 );..
93650 20 20 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d    iLogsize = mem
93660 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
93670 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a  & CTRL_LOGSIZE;.
93680 20 20 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67    size = 1<<iLog
93690 73 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20  size;.  assert( 
936a0 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75  iBlock+size-1<(u
936b0 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29  32)mem5.nBlock )
936c0 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b  ;..  mem5.aCtrl[
936d0 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f  iBlock] |= CTRL_
936e0 46 52 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74  FREE;.  mem5.aCt
936f0 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31  rl[iBlock+size-1
93700 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a  ] |= CTRL_FREE;.
93710 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63    assert( mem5.c
93720 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b  urrentCount>0 );
93730 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e  .  assert( mem5.
93740 63 75 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a  currentOut>=(siz
93750 65 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 29 20 29 3b  e*mem5.nAtom) );
93760 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43  .  mem5.currentC
93770 6f 75 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63  ount--;.  mem5.c
93780 75 72 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a  urrentOut -= siz
93790 65 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20  e*mem5.nAtom;.  
937a0 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72  assert( mem5.cur
937b0 72 65 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d  rentOut>0 || mem
937c0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d  5.currentCount==
937d0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  0 );.  assert( m
937e0 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
937f0 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65  >0 || mem5.curre
93800 6e 74 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d  ntOut==0 );..  m
93810 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b  em5.aCtrl[iBlock
93820 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20  ] = CTRL_FREE | 
93830 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c  iLogsize;.  whil
93840 65 28 20 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d  e( iLogsize<LOGM
93850 41 58 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42  AX ){.    int iB
93860 75 64 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69  uddy;.    if( (i
93870 42 6c 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29  Block>>iLogsize)
93880 20 26 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42   & 1 ){.      iB
93890 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20  uddy = iBlock - 
938a0 73 69 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  size;.    }else{
938b0 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20  .      iBuddy = 
938c0 69 42 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20  iBlock + size;. 
938d0 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
938e0 20 69 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20   iBuddy>=0 );.  
938f0 20 20 69 66 28 20 28 69 42 75 64 64 79 2b 28 31    if( (iBuddy+(1
93900 3c 3c 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d  <<iLogsize))>mem
93910 35 2e 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b  5.nBlock ) break
93920 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61  ;.    if( mem5.a
93930 43 74 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43  Ctrl[iBuddy]!=(C
93940 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73  TRL_FREE | iLogs
93950 69 7a 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  ize) ) break;.  
93960 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28    memsys5Unlink(
93970 69 42 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65  iBuddy, iLogsize
93980 29 3b 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b  );.    iLogsize+
93990 2b 3b 0a 20 20 20 20 69 66 28 20 69 42 75 64 64  +;.    if( iBudd
939a0 79 3c 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20  y<iBlock ){.    
939b0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75    mem5.aCtrl[iBu
939c0 64 64 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45  ddy] = CTRL_FREE
939d0 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20   | iLogsize;.   
939e0 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42     mem5.aCtrl[iB
939f0 6c 6f 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20  lock] = 0;.     
93a00 20 69 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79   iBlock = iBuddy
93a10 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
93a20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42     mem5.aCtrl[iB
93a30 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45  lock] = CTRL_FRE
93a40 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20  E | iLogsize;.  
93a50 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69      mem5.aCtrl[i
93a60 42 75 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20  Buddy] = 0;.    
93a70 7d 0a 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b  }.    size *= 2;
93a80 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69  .  }.  memsys5Li
93a90 6e 6b 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73  nk(iBlock, iLogs
93aa0 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ize);.}../*.** A
93ab0 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f  llocate nBytes o
93ac0 66 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74  f memory.*/.stat
93ad0 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35  ic void *memsys5
93ae0 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65  Malloc(int nByte
93af0 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  s){.  sqlite3_in
93b00 74 36 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66  t64 *p = 0;.  if
93b10 28 20 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20  ( nBytes>0 ){.  
93b20 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29    memsys5Enter()
93b30 3b 0a 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73  ;.    p = memsys
93b40 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42  5MallocUnsafe(nB
93b50 79 74 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79  ytes);.    memsy
93b60 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20  s5Leave();.  }. 
93b70 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70   return (void*)p
93b80 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  ; .}../*.** Free
93b90 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74   memory..*/.stat
93ba0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46  ic void memsys5F
93bb0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
93bc0 29 7b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d  ){.  if( pPrior=
93bd0 3d 30 20 29 7b 0a 61 73 73 65 72 74 28 30 29 3b  =0 ){.assert(0);
93be0 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
93bf0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
93c00 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65  );.  memsys5Free
93c10 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a  Unsafe(pPrior);.
93c20 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29    memsys5Leave()
93c30 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  ;  .}../*.** Cha
93c40 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20  nge the size of 
93c50 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f  an existing memo
93c60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f  ry allocation.*/
93c70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
93c80 6d 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69  msys5Realloc(voi
93c90 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e  d *pPrior, int n
93ca0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f  Bytes){.  int nO
93cb0 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  ld;.  void *p;. 
93cc0 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29   if( pPrior==0 )
93cd0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d  {.    return mem
93ce0 73 79 73 35 4d 61 6c 6c 6f 63 28 6e 42 79 74 65  sys5Malloc(nByte
93cf0 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42  s);.  }.  if( nB
93d00 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 6d  ytes<=0 ){.    m
93d10 65 6d 73 79 73 35 46 72 65 65 28 70 50 72 69 6f  emsys5Free(pPrio
93d20 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  r);.    return 0
93d30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d  ;.  }.  nOld = m
93d40 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f  emsys5Size(pPrio
93d50 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  r);.  if( nBytes
93d60 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65  <=nOld ){.    re
93d70 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d  turn pPrior;.  }
93d80 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
93d90 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35  );.  p = memsys5
93da0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79  MallocUnsafe(nBy
93db0 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  tes);.  if( p ){
93dc0 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70  .    memcpy(p, p
93dd0 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20  Prior, nOld);.  
93de0 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73    memsys5FreeUns
93df0 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d  afe(pPrior);.  }
93e00 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  .  memsys5Leave(
93e10 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
93e20 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70  ../*.** Round up
93e30 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20   a request size 
93e40 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69  to the next vali
93e50 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a  d allocation siz
93e60 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
93e70 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28   memsys5Roundup(
93e80 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46  int n){.  int iF
93e90 75 6c 6c 53 7a 3b 0a 20 20 66 6f 72 28 69 46 75  ullSz;.  for(iFu
93ea0 6c 6c 53 7a 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 3b  llSz=mem5.nAtom;
93eb0 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c   iFullSz<n; iFul
93ec0 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74  lSz *= 2);.  ret
93ed0 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a  urn iFullSz;.}..
93ee0 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79  static int memsy
93ef0 73 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65  s5Log(int iValue
93f00 29 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20  ){.  int iLog;. 
93f10 20 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c   for(iLog=0; (1<
93f20 3c 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69  <iLog)<iValue; i
93f30 4c 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e  Log++);.  return
93f40 20 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   iLog;.}../*.** 
93f50 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20  Initialize this 
93f60 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  module..*/.stati
93f70 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69  c int memsys5Ini
93f80 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
93f90 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e  {.  int ii;.  in
93fa0 74 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65  t nByte = sqlite
93fb0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48  3GlobalConfig.nH
93fc0 65 61 70 3b 0a 20 20 75 38 20 2a 7a 42 79 74 65  eap;.  u8 *zByte
93fd0 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33   = (u8 *)sqlite3
93fe0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65  GlobalConfig.pHe
93ff0 61 70 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f  ap;.  int nMinLo
94000 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
94010 20 20 20 2f 2a 20 4c 6f 67 20 6f 66 20 6d 69 6e     /* Log of min
94020 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  imum allocation 
94030 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2a 2f 0a  size in bytes*/.
94040 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 0a    int iOffset;..
94050 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
94060 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20  ER(NotUsed);..  
94070 69 66 28 20 21 7a 42 79 74 65 20 29 7b 0a 20 20  if( !zByte ){.  
94080 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
94090 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 6e 4d  ERROR;.  }..  nM
940a0 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c  inLog = memsys5L
940b0 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  og(sqlite3Global
940c0 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20  Config.mnReq);. 
940d0 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3d 20 28 31   mem5.nAtom = (1
940e0 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68  <<nMinLog);.  wh
940f0 69 6c 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66  ile( (int)sizeof
94100 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e  (Mem5Link)>mem5.
94110 6e 41 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65 6d  nAtom ){.    mem
94120 35 2e 6e 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 6e  5.nAtom = mem5.n
94130 41 74 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a  Atom << 1;.  }..
94140 20 20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20    mem5.nBlock = 
94150 28 6e 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 6e  (nByte / (mem5.n
94160 41 74 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29  Atom+sizeof(u8))
94170 29 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20  );.  mem5.zPool 
94180 3d 20 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e  = zByte;.  mem5.
94190 61 43 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d  aCtrl = (u8 *)&m
941a0 65 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e  em5.zPool[mem5.n
941b0 42 6c 6f 63 6b 2a 6d 65 6d 35 2e 6e 41 74 6f 6d  Block*mem5.nAtom
941c0 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ];..  for(ii=0; 
941d0 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b  ii<=LOGMAX; ii++
941e0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72  ){.    mem5.aiFr
941f0 65 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b  eelist[ii] = -1;
94200 0a 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20  .  }..  iOffset 
94210 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f  = 0;.  for(ii=LO
94220 47 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d  GMAX; ii>=0; ii-
94230 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c  -){.    int nAll
94240 6f 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20  oc = (1<<ii);.  
94250 20 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e    if( (iOffset+n
94260 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c  Alloc)<=mem5.nBl
94270 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ock ){.      mem
94280 35 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d  5.aCtrl[iOffset]
94290 20 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45   = ii | CTRL_FRE
942a0 45 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35  E;.      memsys5
942b0 4c 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69  Link(iOffset, ii
942c0 29 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74  );.      iOffset
942d0 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20   += nAlloc;.    
942e0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f  }.    assert((iO
942f0 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65  ffset+nAlloc)>me
94300 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a  m5.nBlock);.  }.
94310 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
94320 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
94330 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20  initialize this 
94340 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  module..*/.stati
94350 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 53 68  c void memsys5Sh
94360 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74  utdown(void *Not
94370 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
94380 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
94390 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  d);.  return;.}.
943a0 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20  ./*.** Open the 
943b0 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61  file indicated a
943c0 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f  nd write a log o
943d0 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65  f all unfreed me
943e0 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74  mory .** allocat
943f0 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c  ions into that l
94400 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  og..*/.SQLITE_PR
94410 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
94420 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 6f  e3Memsys5Dump(co
94430 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
94440 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  ame){.#ifdef SQL
94450 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45  ITE_DEBUG.  FILE
94460 20 2a 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20   *out;.  int i, 
94470 6a 2c 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e  j, n;.  int nMin
94480 4c 6f 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c  Log;..  if( zFil
94490 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c  ename==0 || zFil
944a0 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20  ename[0]==0 ){. 
944b0 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b     out = stdout;
944c0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75  .  }else{.    ou
944d0 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e  t = fopen(zFilen
944e0 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69  ame, "w");.    i
944f0 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20  f( out==0 ){.   
94500 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
94510 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f  r, "** Unable to
94520 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64   output memory d
94530 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a  ebug output log:
94540 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20   %s **\n",.     
94550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94560 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20   zFilename);.   
94570 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
94580 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e  .  }.  memsys5En
94590 74 65 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67  ter();.  nMinLog
945a0 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65   = memsys5Log(me
945b0 6d 35 2e 6e 41 74 6f 6d 29 3b 0a 20 20 66 6f 72  m5.nAtom);.  for
945c0 28 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20  (i=0; i<=LOGMAX 
945d0 26 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b  && i+nMinLog<32;
945e0 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e   i++){.    for(n
945f0 3d 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65  =0, j=mem5.aiFre
94600 65 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20  elist[i]; j>=0; 
94610 6a 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d  j = MEM5LINK(j)-
94620 3e 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20  >next, n++){}.  
94630 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
94640 66 72 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f  freelist items o
94650 66 20 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22  f size %d: %d\n"
94660 2c 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c 3c 20  , mem5.nAtom << 
94670 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72  i, n);.  }.  fpr
94680 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e  intf(out, "mem5.
94690 6e 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25  nAlloc       = %
946a0 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c  llu\n", mem5.nAl
946b0 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  loc);.  fprintf(
946c0 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c  out, "mem5.total
946d0 41 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e  Alloc   = %llu\n
946e0 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c  ", mem5.totalAll
946f0 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  oc);.  fprintf(o
94700 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45  ut, "mem5.totalE
94710 78 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22  xcess  = %llu\n"
94720 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65  , mem5.totalExce
94730 73 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  ss);.  fprintf(o
94740 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e  ut, "mem5.curren
94750 74 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20  tOut   = %u\n", 
94760 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29  mem5.currentOut)
94770 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ;.  fprintf(out,
94780 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f   "mem5.currentCo
94790 75 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d  unt = %u\n", mem
947a0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b  5.currentCount);
947b0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
947c0 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20  "mem5.maxOut    
947d0 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35     = %u\n", mem5
947e0 2e 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69  .maxOut);.  fpri
947f0 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d  ntf(out, "mem5.m
94800 61 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75  axCount     = %u
94810 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75  \n", mem5.maxCou
94820 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  nt);.  fprintf(o
94830 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71  ut, "mem5.maxReq
94840 75 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20  uest   = %u\n", 
94850 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29  mem5.maxRequest)
94860 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65  ;.  memsys5Leave
94870 28 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73  ();.  if( out==s
94880 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c  tdout ){.    ffl
94890 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d  ush(stdout);.  }
948a0 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65  else{.    fclose
948b0 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73 65  (out);.  }.#else
948c0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
948d0 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  TER(zFilename);.
948e0 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
948f0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
94900 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
94910 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
94920 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20  th external .** 
94930 6c 69 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75  linkage. It retu
94940 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
94950 20 61 20 73 74 61 74 69 63 20 73 71 6c 69 74 65   a static sqlite
94960 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a  3_mem_methods.**
94970 20 73 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65   struct populate
94980 64 20 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79  d with the memsy
94990 73 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53  s5 methods..*/.S
949a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
949b0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
949c0 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
949d0 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f  MemGetMemsys5(vo
949e0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  id){.  static co
949f0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
94a00 6d 65 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d  methods memsys5M
94a10 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20  ethods = {.     
94a20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20  memsys5Malloc,. 
94a30 20 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c      memsys5Free,
94a40 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61  .     memsys5Rea
94a50 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79  lloc,.     memsy
94a60 73 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d  s5Size,.     mem
94a70 73 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20  sys5Roundup,.   
94a80 20 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20    memsys5Init,. 
94a90 20 20 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64      memsys5Shutd
94aa0 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b  own,.     0.  };
94ab0 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79  .  return &memsy
94ac0 73 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65  s5Methods;.}..#e
94ad0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45  ndif /* SQLITE_E
94ae0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f  NABLE_MEMSYS5 */
94af0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
94b00 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20  * End of mem5.c 
94b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94b40 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
94b50 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74  * Begin file mut
94b60 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.c ***********
94b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94b90 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67  /./*.** 2007 Aug
94ba0 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 14.**.** The
94bb0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
94bc0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
94bd0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
94be0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
94bf0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
94c00 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
94c10 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
94c20 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
94c30 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
94c40 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
94c50 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
94c60 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
94c70 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
94c80 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
94c90 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
94ca0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
94cb0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
94cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94d00 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
94d10 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
94d20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
94d30 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
94d40 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  es..**.** This f
94d50 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
94d60 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e  e that is common
94d70 20 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65   across all mute
94d80 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
94d90 73 2e 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  s...**.** $Id: m
94da0 75 74 65 78 2e 63 2c 76 20 31 2e 32 39 20 32 30  utex.c,v 1.29 20
94db0 30 38 2f 31 30 2f 30 37 20 31 35 3a 32 35 3a 34  08/10/07 15:25:4
94dc0 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  8 drh Exp $.*/..
94dd0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
94de0 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20  UTEX_OMIT./*.** 
94df0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
94e00 75 74 65 78 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a  utex system..*/.
94e10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
94e20 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49  nt sqlite3MutexI
94e30 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 69 6e  nit(void){ .  in
94e40 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
94e50 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  ;.  if( sqlite3G
94e60 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
94e70 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 69 66  eMutex ){.    if
94e80 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ( !sqlite3Global
94e90 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
94ea0 74 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  texAlloc ){.    
94eb0 20 20 2f 2a 20 49 66 20 74 68 65 20 78 4d 75 74    /* If the xMut
94ec0 65 78 41 6c 6c 6f 63 20 6d 65 74 68 6f 64 20 68  exAlloc method h
94ed0 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c  as not been set,
94ee0 20 74 68 65 6e 20 74 68 65 20 75 73 65 72 20 64   then the user d
94ef0 69 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20  id not.      ** 
94f00 69 6e 73 74 61 6c 6c 20 61 20 6d 75 74 65 78 20  install a mutex 
94f10 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 76  implementation v
94f20 69 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  ia sqlite3_confi
94f30 67 28 29 20 70 72 69 6f 72 20 74 6f 20 0a 20 20  g() prior to .  
94f40 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69      ** sqlite3_i
94f50 6e 69 74 69 61 6c 69 7a 65 28 29 20 62 65 69 6e  nitialize() bein
94f60 67 20 63 61 6c 6c 65 64 2e 20 54 68 69 73 20 62  g called. This b
94f70 6c 6f 63 6b 20 63 6f 70 69 65 73 20 70 6f 69 6e  lock copies poin
94f80 74 65 72 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a  ters to.      **
94f90 20 74 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70   the default imp
94fa0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f  lementation into
94fb0 20 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62   the sqlite3Glob
94fc0 61 6c 43 6f 6e 66 69 67 20 73 74 72 75 63 74 75  alConfig structu
94fd0 72 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  re..      **.   
94fe0 20 20 20 2a 2a 20 54 68 65 20 64 61 6e 67 65 72     ** The danger
94ff0 20 69 73 20 74 68 61 74 20 61 6c 74 68 6f 75 67   is that althoug
95000 68 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  h sqlite3_config
95010 28 29 20 69 73 20 6e 6f 74 20 61 20 74 68 72 65  () is not a thre
95020 61 64 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20  adsafe.      ** 
95030 41 50 49 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69  API, sqlite3_ini
95040 74 69 61 6c 69 7a 65 28 29 20 69 73 2c 20 61 6e  tialize() is, an
95050 64 20 73 6f 20 6d 75 6c 74 69 70 6c 65 20 74 68  d so multiple th
95060 72 65 61 64 73 20 6d 61 79 20 62 65 0a 20 20 20  reads may be.   
95070 20 20 20 2a 2a 20 61 74 74 65 6d 70 74 69 6e 67     ** attempting
95080 20 74 6f 20 72 75 6e 20 74 68 69 73 20 66 75 6e   to run this fun
95090 63 74 69 6f 6e 20 73 69 6d 75 6c 74 61 6e 65 6f  ction simultaneo
950a0 75 73 6c 79 2e 20 54 6f 20 67 75 61 72 64 20 77  usly. To guard w
950b0 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 61 63  rite.      ** ac
950c0 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69  cess to the sqli
950d0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20  te3GlobalConfig 
950e0 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 20 27  structure, the '
950f0 4d 41 53 54 45 52 27 20 73 74 61 74 69 63 20 6d  MASTER' static m
95100 75 74 65 78 0a 20 20 20 20 20 20 2a 2a 20 69 73  utex.      ** is
95110 20 6f 62 74 61 69 6e 65 64 20 62 65 66 6f 72 65   obtained before
95120 20 6d 6f 64 69 66 79 69 6e 67 20 69 74 2e 0a 20   modifying it.. 
95130 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
95140 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
95150 6f 64 73 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  ods *p = sqlite3
95160 44 65 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a  DefaultMutex();.
95170 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
95180 74 65 78 20 2a 70 4d 61 73 74 65 72 20 3d 20 30  tex *pMaster = 0
95190 3b 0a 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20  ;.  .      rc = 
951a0 70 2d 3e 78 4d 75 74 65 78 49 6e 69 74 28 29 3b  p->xMutexInit();
951b0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
951c0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
951d0 20 20 20 20 70 4d 61 73 74 65 72 20 3d 20 70 2d      pMaster = p-
951e0 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  >xMutexAlloc(SQL
951f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
95200 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20  _MASTER);.      
95210 20 20 61 73 73 65 72 74 28 70 4d 61 73 74 65 72    assert(pMaster
95220 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 78 4d  );.        p->xM
95230 75 74 65 78 45 6e 74 65 72 28 70 4d 61 73 74 65  utexEnter(pMaste
95240 72 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  r);.        asse
95250 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  rt( sqlite3Globa
95260 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
95270 75 74 65 78 41 6c 6c 6f 63 3d 3d 30 20 0a 20 20  utexAlloc==0 .  
95280 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71             || sq
95290 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
952a0 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c  g.mutex.xMutexAl
952b0 6c 6f 63 3d 3d 70 2d 3e 78 4d 75 74 65 78 41 6c  loc==p->xMutexAl
952c0 6c 6f 63 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  loc.        );. 
952d0 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69         if( !sqli
952e0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
952f0 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f  mutex.xMutexAllo
95300 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  c ){.          s
95310 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
95320 69 67 2e 6d 75 74 65 78 20 3d 20 2a 70 3b 0a 20  ig.mutex = *p;. 
95330 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
95340 20 70 2d 3e 78 4d 75 74 65 78 4c 65 61 76 65 28   p->xMutexLeave(
95350 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  pMaster);.      
95360 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
95370 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47     rc = sqlite3G
95380 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
95390 78 2e 78 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a  x.xMutexInit();.
953a0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
953b0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
953c0 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6d 75   Shutdown the mu
953d0 74 65 78 20 73 79 73 74 65 6d 2e 20 54 68 69 73  tex system. This
953e0 20 63 61 6c 6c 20 66 72 65 65 73 20 72 65 73 6f   call frees reso
953f0 75 72 63 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  urces allocated 
95400 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74  by.** sqlite3Mut
95410 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c  exInit()..*/.SQL
95420 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
95430 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28  sqlite3MutexEnd(
95440 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20  void){.  int rc 
95450 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72  = SQLITE_OK;.  r
95460 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  c = sqlite3Globa
95470 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
95480 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 72 65 74  utexEnd();.  ret
95490 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
954a0 20 52 65 74 72 69 65 76 65 20 61 20 70 6f 69 6e   Retrieve a poin
954b0 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20  ter to a static 
954c0 6d 75 74 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74  mutex or allocat
954d0 65 20 61 20 6e 65 77 20 64 79 6e 61 6d 69 63 20  e a new dynamic 
954e0 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  one..*/.SQLITE_A
954f0 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  PI sqlite3_mutex
95500 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   *sqlite3_mutex_
95510 61 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23  alloc(int id){.#
95520 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
95530 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66  IT_AUTOINIT.  if
95540 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  ( sqlite3_initia
95550 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20  lize() ) return 
95560 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  0;.#endif.  retu
95570 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  rn sqlite3Global
95580 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
95590 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a  texAlloc(id);.}.
955a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
955b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73  sqlite3_mutex *s
955c0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
955d0 28 69 6e 74 20 69 64 29 7b 0a 20 20 69 66 28 20  (int id){.  if( 
955e0 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  !sqlite3GlobalCo
955f0 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20  nfig.bCoreMutex 
95600 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
95610 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
95620 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
95630 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c  g.mutex.xMutexAl
95640 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  loc(id);.}../*.*
95650 2a 20 46 72 65 65 20 61 20 64 79 6e 61 6d 69 63  * Free a dynamic
95660 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54   mutex..*/.SQLIT
95670 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
95680 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71  e3_mutex_free(sq
95690 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
956a0 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
956b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
956c0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
956d0 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  Free(p);.  }.}..
956e0 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65  /*.** Obtain the
956f0 20 6d 75 74 65 78 20 70 2e 20 49 66 20 73 6f 6d   mutex p. If som
95700 65 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61  e other thread a
95710 6c 72 65 61 64 79 20 68 61 73 20 74 68 65 20 6d  lready has the m
95720 75 74 65 78 2c 20 62 6c 6f 63 6b 0a 2a 2a 20 75  utex, block.** u
95730 6e 74 69 6c 20 69 74 20 63 61 6e 20 62 65 20 6f  ntil it can be o
95740 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  btained..*/.SQLI
95750 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
95760 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
95770 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
95780 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
95790 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
957a0 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74  onfig.mutex.xMut
957b0 65 78 45 6e 74 65 72 28 70 29 3b 0a 20 20 7d 0a  exEnter(p);.  }.
957c0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  }../*.** Obtain 
957d0 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 66 20  the mutex p. If 
957e0 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75  successful, retu
957f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74  rn SQLITE_OK. Ot
95800 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 6f 74  herwise, if anot
95810 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 20 68 6f  her.** thread ho
95820 6c 64 73 20 74 68 65 20 6d 75 74 65 78 20 61 6e  lds the mutex an
95830 64 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 6f  d it cannot be o
95840 62 74 61 69 6e 65 64 2c 20 72 65 74 75 72 6e 20  btained, return 
95850 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2f 0a  SQLITE_BUSY..*/.
95860 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
95870 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
95880 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
95890 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  p){.  int rc = S
958a0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
958b0 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  p ){.    return 
958c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
958d0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
958e0 54 72 79 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65  Try(p);.  }.  re
958f0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
95900 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
95910 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74  tex_leave() rout
95920 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65  ine exits a mute
95930 78 20 74 68 61 74 20 77 61 73 20 70 72 65 76 69  x that was previ
95940 6f 75 73 6c 79 0a 2a 2a 20 65 6e 74 65 72 65 64  ously.** entered
95950 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72   by the same thr
95960 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69  ead.  The behavi
95970 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20  or is undefined 
95980 69 66 20 74 68 65 20 6d 75 74 65 78 20 0a 2a 2a  if the mutex .**
95990 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
959a0 79 20 65 6e 74 65 72 65 64 2e 20 49 66 20 61 20  y entered. If a 
959b0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20  NULL pointer is 
959c0 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
959d0 75 6d 65 6e 74 0a 2a 2a 20 74 68 69 73 20 66 75  ument.** this fu
959e0 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
959f0 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  p..*/.SQLITE_API
95a00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
95a10 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
95a20 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
95a30 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( p ){.    sqli
95a40 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
95a50 6d 75 74 65 78 2e 78 4d 75 74 65 78 4c 65 61 76  mutex.xMutexLeav
95a60 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66  e(p);.  }.}..#if
95a70 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a  ndef NDEBUG./*.*
95a80 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
95a90 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73  tex_held() and s
95aa0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
95ab0 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61  held() routine a
95ac0 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66  re.** intended f
95ad0 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73  or use inside as
95ae0 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
95af0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
95b00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74   int sqlite3_mut
95b10 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 5f  ex_held(sqlite3_
95b20 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74  mutex *p){.  ret
95b30 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69  urn p==0 || sqli
95b40 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
95b50 6d 75 74 65 78 2e 78 4d 75 74 65 78 48 65 6c 64  mutex.xMutexHeld
95b60 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  (p);.}.SQLITE_AP
95b70 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75  I int sqlite3_mu
95b80 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69  tex_notheld(sqli
95b90 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
95ba0 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
95bb0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
95bc0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
95bd0 4e 6f 74 68 65 6c 64 28 70 29 3b 0a 7d 0a 23 65  Notheld(p);.}.#e
95be0 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  ndif..#endif /* 
95bf0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 55 54 45  SQLITE_OMIT_MUTE
95c00 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  X */../*********
95c10 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74  ***** End of mut
95c20 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.c ***********
95c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95c50 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
95c60 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
95c70 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a 2a   mutex_noop.c **
95c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95ca0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
95cb0 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a 2a 0a 2a   October 07.**.*
95cc0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
95cd0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
95ce0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
95cf0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
95d00 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
95d10 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
95d20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
95d30 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
95d40 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
95d50 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
95d60 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
95d70 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
95d80 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
95d90 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
95da0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
95db0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
95dc0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
95dd0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
95de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
95e20 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
95e30 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
95e40 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
95e50 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  mutexes..**.** T
95e60 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
95e70 6f 6e 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  on in this file 
95e80 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65  does not provide
95e90 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a 2a 20 65   any mutual.** e
95ea0 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20 69 73 20  xclusion and is 
95eb0 74 68 75 73 20 73 75 69 74 61 62 6c 65 20 66 6f  thus suitable fo
95ec0 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 20 61 70  r use only in ap
95ed0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 74 68  plications.** th
95ee0 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 69 6e  at use SQLite in
95ef0 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64   a single thread
95f00 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20  .  The routines 
95f10 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65 72 65 20  defined.** here 
95f20 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72  are place-holder
95f30 73 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  s.  Applications
95f40 20 63 61 6e 20 73 75 62 73 74 69 74 75 74 65 20   can substitute 
95f50 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75 74 65 78  working.** mutex
95f60 20 72 6f 75 74 69 6e 65 73 20 61 74 20 73 74 61   routines at sta
95f70 72 74 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68  rt-time using th
95f80 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69  e.**.**     sqli
95f90 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54  te3_config(SQLIT
95fa0 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 2c 2e  E_CONFIG_MUTEX,.
95fb0 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74 65 72 66  ..).**.** interf
95fc0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 6f  ace..**.** If co
95fd0 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49  mpiled with SQLI
95fe0 54 45 5f 44 45 42 55 47 2c 20 74 68 65 6e 20 61  TE_DEBUG, then a
95ff0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67 69 63 20  dditional logic 
96000 69 73 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74  is inserted.** t
96010 68 61 74 20 64 6f 65 73 20 65 72 72 6f 72 20 63  hat does error c
96020 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75 74 65 78  hecking on mutex
96030 65 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  es to make sure 
96040 74 68 65 79 20 61 72 65 20 62 65 69 6e 67 0a 2a  they are being.*
96050 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72 65 63 74  * called correct
96060 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  ly..**.** $Id: m
96070 75 74 65 78 5f 6e 6f 6f 70 2e 63 2c 76 20 31 2e  utex_noop.c,v 1.
96080 33 20 32 30 30 38 2f 31 32 2f 30 35 20 31 37 3a  3 2008/12/05 17:
96090 31 37 3a 30 38 20 64 72 68 20 45 78 70 20 24 0a  17:08 drh Exp $.
960a0 2a 2f 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  */...#if defined
960b0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f  (SQLITE_MUTEX_NO
960c0 4f 50 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  OP) && !defined(
960d0 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a  SQLITE_DEBUG)./*
960e0 0a 2a 2a 20 53 74 75 62 20 72 6f 75 74 69 6e 65  .** Stub routine
960f0 73 20 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20  s for all mutex 
96100 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 20 54  methods..**.** T
96110 68 69 73 20 72 6f 75 74 69 6e 65 73 20 70 72 6f  his routines pro
96120 76 69 64 65 20 6e 6f 20 6d 75 74 75 61 6c 20 65  vide no mutual e
96130 78 63 6c 75 73 69 6f 6e 20 6f 72 20 65 72 72 6f  xclusion or erro
96140 72 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73  r checking..*/.s
96150 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75  tatic int noopMu
96160 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f  texHeld(sqlite3_
96170 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72  mutex *p){ retur
96180 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  n 1; }.static in
96190 74 20 6e 6f 6f 70 4d 75 74 65 78 4e 6f 74 68 65  t noopMutexNothe
961a0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
961b0 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20   *p){ return 1; 
961c0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f  }.static int noo
961d0 70 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29  pMutexInit(void)
961e0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
961f0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74  OK; }.static int
96200 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 28 76 6f   noopMutexEnd(vo
96210 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
96220 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20  TE_OK; }.static 
96230 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6e  sqlite3_mutex *n
96240 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  oopMutexAlloc(in
96250 74 20 69 64 29 7b 20 72 65 74 75 72 6e 20 28 73  t id){ return (s
96260 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 3b  qlite3_mutex*)8;
96270 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e   }.static void n
96280 6f 6f 70 4d 75 74 65 78 46 72 65 65 28 73 71 6c  oopMutexFree(sql
96290 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20  ite3_mutex *p){ 
962a0 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63  return; }.static
962b0 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 45   void noopMutexE
962c0 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74  nter(sqlite3_mut
962d0 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20  ex *p){ return; 
962e0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f  }.static int noo
962f0 70 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65  pMutexTry(sqlite
96300 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74  3_mutex *p){ ret
96310 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
96320 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f  .static void noo
96330 70 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69  pMutexLeave(sqli
96340 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72  te3_mutex *p){ r
96350 65 74 75 72 6e 3b 20 7d 0a 0a 53 51 4c 49 54 45  eturn; }..SQLITE
96360 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
96370 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a  _mutex_methods *
96380 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75  sqlite3DefaultMu
96390 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61  tex(void){.  sta
963a0 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tic sqlite3_mute
963b0 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78  x_methods sMutex
963c0 20 3d 20 7b 0a 20 20 20 20 6e 6f 6f 70 4d 75 74   = {.    noopMut
963d0 65 78 49 6e 69 74 2c 0a 20 20 20 20 6e 6f 6f 70  exInit,.    noop
963e0 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 6e 6f  MutexEnd,.    no
963f0 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20  opMutexAlloc,.  
96400 20 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65 65 2c    noopMutexFree,
96410 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e  .    noopMutexEn
96420 74 65 72 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  ter,.    noopMut
96430 65 78 54 72 79 2c 0a 20 20 20 20 6e 6f 6f 70 4d  exTry,.    noopM
96440 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20  utexLeave,..    
96450 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 2c 0a 20  noopMutexHeld,. 
96460 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4e 6f 74 68     noopMutexNoth
96470 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75  eld.  };..  retu
96480 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65  rn &sMutex;.}.#e
96490 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28  ndif /* defined(
964a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
964b0 50 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  P) && !defined(S
964c0 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a  QLITE_DEBUG) */.
964d0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
964e0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20  ITE_MUTEX_NOOP) 
964f0 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
96500 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 49  E_DEBUG)./*.** I
96510 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  n this implement
96520 61 74 69 6f 6e 2c 20 65 72 72 6f 72 20 63 68 65  ation, error che
96530 63 6b 69 6e 67 20 69 73 20 70 72 6f 76 69 64 65  cking is provide
96540 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a  d for testing.**
96550 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 70   and debugging p
96560 75 72 70 6f 73 65 73 2e 20 20 54 68 65 20 6d 75  urposes.  The mu
96570 74 65 78 65 73 20 73 74 69 6c 6c 20 64 6f 20 6e  texes still do n
96580 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 0a 2a  ot provide any.*
96590 2a 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69  * mutual exclusi
965a0 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  on..*/../*.** Th
965b0 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a  e mutex object.*
965c0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
965d0 5f 6d 75 74 65 78 20 7b 0a 20 20 69 6e 74 20 69  _mutex {.  int i
965e0 64 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 75  d;     /* The mu
965f0 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e  tex type */.  in
96600 74 20 63 6e 74 3b 20 20 20 20 2f 2a 20 4e 75 6d  t cnt;    /* Num
96610 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77  ber of entries w
96620 69 74 68 6f 75 74 20 61 20 6d 61 74 63 68 69 6e  ithout a matchin
96630 67 20 6c 65 61 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f  g leave */.};../
96640 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
96650 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e  _mutex_held() an
96660 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
96670 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e  notheld() routin
96680 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65  e are.** intende
96690 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
966a0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
966b0 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ents..*/.static 
966c0 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 48 65  int debugMutexHe
966d0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
966e0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
966f0 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 3e 30 3b  ==0 || p->cnt>0;
96700 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65  .}.static int de
96710 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  bugMutexNotheld(
96720 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
96730 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30  ){.  return p==0
96740 20 7c 7c 20 70 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d   || p->cnt==0;.}
96750 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
96760 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c  ze and deinitial
96770 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75  ize the mutex su
96780 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74  bsystem..*/.stat
96790 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
967a0 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74  xInit(void){ ret
967b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
967c0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75  .static int debu
967d0 67 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b  gMutexEnd(void){
967e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
967f0 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K; }../*.** The 
96800 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
96810 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c  loc() routine al
96820 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a  locates a new.**
96830 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72   mutex and retur
96840 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
96850 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72  it.  If it retur
96860 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20  ns NULL.** that 
96870 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
96880 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
96890 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a 2f 0a 73  allocated. .*/.s
968a0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75  tatic sqlite3_mu
968b0 74 65 78 20 2a 64 65 62 75 67 4d 75 74 65 78 41  tex *debugMutexA
968c0 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 20  lloc(int id){.  
968d0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
968e0 75 74 65 78 20 61 53 74 61 74 69 63 5b 36 5d 3b  utex aStatic[6];
968f0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
96900 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 73 77   *pNew = 0;.  sw
96910 69 74 63 68 28 20 69 64 20 29 7b 0a 20 20 20 20  itch( id ){.    
96920 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  case SQLITE_MUTE
96930 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65  X_FAST:.    case
96940 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
96950 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20  CURSIVE: {.     
96960 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d   pNew = sqlite3M
96970 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 4e  alloc(sizeof(*pN
96980 65 77 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ew));.      if( 
96990 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
969a0 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 3b 0a 20  pNew->id = id;. 
969b0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 63 6e 74         pNew->cnt
969c0 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
969d0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
969e0 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
969f0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 64        assert( id
96a00 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20  -2 >= 0 );.     
96a10 20 61 73 73 65 72 74 28 20 69 64 2d 32 20 3c 20   assert( id-2 < 
96a20 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 61 53 74  (int)(sizeof(aSt
96a30 61 74 69 63 29 2f 73 69 7a 65 6f 66 28 61 53 74  atic)/sizeof(aSt
96a40 61 74 69 63 5b 30 5d 29 29 20 29 3b 0a 20 20 20  atic[0])) );.   
96a50 20 20 20 70 4e 65 77 20 3d 20 26 61 53 74 61 74     pNew = &aStat
96a60 69 63 5b 69 64 2d 32 5d 3b 0a 20 20 20 20 20 20  ic[id-2];.      
96a70 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 3b 0a 20  pNew->id = id;. 
96a80 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
96a90 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
96aa0 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  New;.}../*.** Th
96ab0 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c  is routine deall
96ac0 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75  ocates a previou
96ad0 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75  sly allocated mu
96ae0 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tex..*/.static v
96af0 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 46 72  oid debugMutexFr
96b00 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ee(sqlite3_mutex
96b10 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
96b20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 61  p->cnt==0 );.  a
96b30 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
96b40 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
96b50 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
96b60 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
96b70 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   );.  sqlite3_fr
96b80 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ee(p);.}../*.** 
96b90 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
96ba0 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
96bb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
96bc0 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
96bd0 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61  pt.** to enter a
96be0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74   mutex.  If anot
96bf0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c  her thread is al
96c00 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65  ready within the
96c10 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74   mutex,.** sqlit
96c20 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
96c30 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20   will block and 
96c40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
96c50 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  y() will return.
96c60 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  ** SQLITE_BUSY. 
96c70 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
96c80 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61  ex_try() interfa
96c90 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ce returns SQLIT
96ca0 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63  E_OK.** upon suc
96cb0 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20  cessful entry.  
96cc0 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20  Mutexes created 
96cd0 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54  using SQLITE_MUT
96ce0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
96cf0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d  .** be entered m
96d00 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
96d10 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
96d20 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73  .  In such cases
96d30 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d   the,.** mutex m
96d40 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e  ust be exited an
96d50 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66   equal number of
96d60 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e   times before an
96d70 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20  other thread.** 
96d80 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74  can enter.  If t
96d90 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74  he same thread t
96da0 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  ries to enter an
96db0 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20  y other kind of 
96dc0 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68  mutex.** more th
96dd0 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68  an once, the beh
96de0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
96df0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
96e00 69 64 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74  id debugMutexEnt
96e10 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
96e20 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
96e30 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
96e40 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
96e50 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65   debugMutexNothe
96e60 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e  ld(p) );.  p->cn
96e70 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  t++;.}.static in
96e80 74 20 64 65 62 75 67 4d 75 74 65 78 54 72 79 28  t debugMutexTry(
96e90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
96ea0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
96eb0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
96ec0 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 64 65  _RECURSIVE || de
96ed0 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  bugMutexNotheld(
96ee0 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b  p) );.  p->cnt++
96ef0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
96f00 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
96f10 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
96f20 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65  _leave() routine
96f30 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74   exits a mutex t
96f40 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69  hat was.** previ
96f50 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79  ously entered by
96f60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
96f70 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a  .  The behavior.
96f80 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20  ** is undefined 
96f90 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20  if the mutex is 
96fa0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e  not currently en
96fb0 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e  tered or.** is n
96fc0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c  ot currently all
96fd0 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20  ocated.  SQLite 
96fe0 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69  will never do ei
96ff0 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ther..*/.static 
97000 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 4c  void debugMutexL
97010 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
97020 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
97030 28 20 64 65 62 75 67 4d 75 74 65 78 48 65 6c 64  ( debugMutexHeld
97040 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2d  (p) );.  p->cnt-
97050 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  -;.  assert( p->
97060 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
97070 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 64 65  _RECURSIVE || de
97080 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  bugMutexNotheld(
97090 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f  p) );.}..SQLITE_
970a0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
970b0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73  mutex_methods *s
970c0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74  qlite3DefaultMut
970d0 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ex(void){.  stat
970e0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
970f0 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20  _methods sMutex 
97100 3d 20 7b 0a 20 20 20 20 64 65 62 75 67 4d 75 74  = {.    debugMut
97110 65 78 49 6e 69 74 2c 0a 20 20 20 20 64 65 62 75  exInit,.    debu
97120 67 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 64  gMutexEnd,.    d
97130 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a  ebugMutexAlloc,.
97140 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 46 72      debugMutexFr
97150 65 65 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74  ee,.    debugMut
97160 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 64 65 62  exEnter,.    deb
97170 75 67 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20  ugMutexTry,.    
97180 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76 65 2c  debugMutexLeave,
97190 0a 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78  ..    debugMutex
971a0 48 65 6c 64 2c 0a 20 20 20 20 64 65 62 75 67 4d  Held,.    debugM
971b0 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b  utexNotheld.  };
971c0 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74  ..  return &sMut
971d0 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ex;.}.#endif /* 
971e0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
971f0 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65  UTEX_NOOP) && de
97200 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
97210 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  UG) */../*******
97220 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
97230 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a  utex_noop.c ****
97240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97260 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
97270 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
97280 6c 65 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a  le mutex_os2.c *
97290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
972a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
972b0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
972c0 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a  07 August 28.**.
972d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
972e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
972f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
97300 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
97310 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
97320 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
97330 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
97340 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
97350 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
97360 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
97370 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
97380 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
97390 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
973a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
973b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
973c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
973d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
973e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
973f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
97430 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
97440 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
97450 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
97460 20 6d 75 74 65 78 65 73 20 66 6f 72 20 4f 53 2f   mutexes for OS/
97470 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74  2.**.** $Id: mut
97480 65 78 5f 6f 73 32 2e 63 2c 76 20 31 2e 31 31 20  ex_os2.c,v 1.11 
97490 32 30 30 38 2f 31 31 2f 32 32 20 31 39 3a 35 30  2008/11/22 19:50
974a0 3a 35 34 20 70 77 65 69 6c 62 61 63 68 65 72 20  :54 pweilbacher 
974b0 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
974c0 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
974d0 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73   file is only us
974e0 65 64 20 69 66 20 53 51 4c 49 54 45 5f 4d 55 54  ed if SQLITE_MUT
974f0 45 58 5f 4f 53 32 20 69 73 20 64 65 66 69 6e 65  EX_OS2 is define
97500 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 6d 75  d..** See the mu
97510 74 65 78 2e 68 20 66 69 6c 65 20 66 6f 72 20 64  tex.h file for d
97520 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64 65  etails..*/.#ifde
97530 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  f SQLITE_MUTEX_O
97540 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  S2../***********
97550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f 53 2f 32  *********** OS/2
97560 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d 65 6e 74   Mutex Implement
97570 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ation **********
97580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
97590 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  ** This implemen
975a0 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74 65 78 65  tation of mutexe
975b0 73 20 69 73 20 62 75 69 6c 74 20 75 73 69 6e 67  s is built using
975c0 20 74 68 65 20 4f 53 2f 32 20 41 50 49 2e 0a 2a   the OS/2 API..*
975d0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74  /../*.** The mut
975e0 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20 45 61 63  ex object.** Eac
975f0 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  h recursive mute
97600 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  x is an instance
97610 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
97620 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  g structure..*/.
97630 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d  struct sqlite3_m
97640 75 74 65 78 20 7b 0a 20 20 48 4d 54 58 20 6d 75  utex {.  HMTX mu
97650 74 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4d 75  tex;       /* Mu
97660 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20  tex controlling 
97670 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  the lock */.  in
97680 74 20 20 69 64 3b 20 20 20 20 20 20 20 20 20 20  t  id;          
97690 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f  /* Mutex type */
976a0 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b 20 20 20  .  int  nRef;   
976b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
976c0 66 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a  f references */.
976d0 20 20 54 49 44 20 20 6f 77 6e 65 72 3b 20 20 20    TID  owner;   
976e0 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68 6f      /* Thread ho
976f0 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65 78  lding this mutex
97700 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20   */.};..#define 
97710 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  OS2_MUTEX_INITIA
97720 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30 2c 30 0a  LIZER   0,0,0,0.
97730 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
97740 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69  e and deinitiali
97750 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 62  ze the mutex sub
97760 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69  system..*/.stati
97770 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 49 6e  c int os2MutexIn
97780 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  it(void){ return
97790 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74   SQLITE_OK; }.st
977a0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
977b0 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75  xEnd(void){ retu
977c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
977d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
977e0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
977f0 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74   routine allocat
97800 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65  es a new.** mute
97810 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  x and returns a 
97820 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20  pointer to it.  
97830 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  If it returns NU
97840 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73  LL.** that means
97850 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f   that a mutex co
97860 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63  uld not be alloc
97870 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c 69 74 65  ated. .** SQLite
97880 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73   will unwind its
97890 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72   stack and retur
978a0 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65  n an error.  The
978b0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
978c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
978d0 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20  loc() is one of 
978e0 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  these integer co
978f0 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c  nstants:.**.** <
97900 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  ul>.** <li>  SQL
97910 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 20  ITE_MUTEX_FAST  
97920 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 2a               0.*
97930 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
97940 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 20  UTEX_RECURSIVE  
97950 20 20 20 20 20 20 20 20 31 0a 2a 2a 20 3c 6c 69          1.** <li
97960 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
97970 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 20 20  STATIC_MASTER   
97980 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51     2.** <li>  SQ
97990 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
979a0 43 5f 4d 45 4d 20 20 20 20 20 20 20 20 20 33 0a  C_MEM         3.
979b0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
979c0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e  MUTEX_STATIC_PRN
979d0 47 20 20 20 20 20 20 20 20 34 0a 2a 2a 20 3c 2f  G        4.** </
979e0 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ul>.**.** The fi
979f0 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74  rst two constant
97a00 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f  s cause sqlite3_
97a10 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f  mutex_alloc() to
97a20 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77   create.** a new
97a30 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77   mutex.  The new
97a40 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73   mutex is recurs
97a50 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f  ive when SQLITE_
97a60 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a  MUTEX_RECURSIVE.
97a70 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e  ** is used but n
97a80 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73  ot necessarily s
97a90 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  o when SQLITE_MU
97aa0 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64  TEX_FAST is used
97ab0 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69  ..** The mutex i
97ac0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f  mplementation do
97ad0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d  es not need to m
97ae0 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f  ake a distinctio
97af0 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c  n.** between SQL
97b00 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
97b10 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d  IVE and SQLITE_M
97b20 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20  UTEX_FAST if it 
97b30 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74  does.** not want
97b40 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65   to.  But SQLite
97b50 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65   will only reque
97b60 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d  st a recursive m
97b70 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73  utex in.** cases
97b80 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79   where it really
97b90 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20   needs one.  If 
97ba0 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63  a faster non-rec
97bb0 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20  ursive mutex.** 
97bc0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
97bd0 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74  s available on t
97be0 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d  he host platform
97bf0 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73  , the mutex subs
97c00 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72  ystem.** might r
97c10 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74  eturn such a mut
97c20 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74  ex in response t
97c30 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  o SQLITE_MUTEX_F
97c40 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  AST..**.** The o
97c50 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72  ther allowed par
97c60 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74  ameters to sqlit
97c70 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
97c80 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20   each return.** 
97c90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
97ca0 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e  tatic preexistin
97cb0 67 20 6d 75 74 65 78 2e 20 20 54 68 72 65 65 20  g mutex.  Three 
97cc0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  static mutexes a
97cd0 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68  re.** used by th
97ce0 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f  e current versio
97cf0 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75  n of SQLite.  Fu
97d00 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  ture versions of
97d10 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61   SQLite.** may a
97d20 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74  dd additional st
97d30 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53  atic mutexes.  S
97d40 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72  tatic mutexes ar
97d50 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a  e for internal.*
97d60 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20  * use by SQLite 
97d70 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69  only.  Applicati
97d80 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 4c  ons that use SQL
97d90 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75  ite mutexes shou
97da0 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74  ld.** use only t
97db0 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  he dynamic mutex
97dc0 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53  es returned by S
97dd0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
97de0 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55   or.** SQLITE_MU
97df0 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a  TEX_RECURSIVE..*
97e00 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69  *.** Note that i
97e10 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e  f one of the dyn
97e20 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d  amic mutex param
97e30 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55  eters (SQLITE_MU
97e40 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53  TEX_FAST.** or S
97e50 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
97e60 52 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74  RSIVE) is used t
97e70 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  hen sqlite3_mute
97e80 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74  x_alloc().** ret
97e90 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74  urns a different
97ea0 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20   mutex on every 
97eb0 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74  call.  But for t
97ec0 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74  he static.** mut
97ed0 65 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61  ex types, the sa
97ee0 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75  me mutex is retu
97ef0 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61  rned on every ca
97f00 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74  ll that has.** t
97f10 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d  he same type num
97f20 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ber..*/.static s
97f30 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6f 73  qlite3_mutex *os
97f40 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20  2MutexAlloc(int 
97f50 69 54 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65  iType){.  sqlite
97f60 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20 4e 55 4c  3_mutex *p = NUL
97f70 4c 3b 0a 20 20 73 77 69 74 63 68 28 20 69 54 79  L;.  switch( iTy
97f80 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  pe ){.    case S
97f90 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
97fa0 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  :.    case SQLIT
97fb0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
97fc0 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73  E: {.      p = s
97fd0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
97fe0 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a  ( sizeof(*p) );.
97ff0 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20        if( p ){. 
98000 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69         p->id = i
98010 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 69 66  Type;.        if
98020 28 20 44 6f 73 43 72 65 61 74 65 4d 75 74 65 78  ( DosCreateMutex
98030 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d 75 74 65  Sem( 0, &p->mute
98040 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 20 21 3d  x, 0, FALSE ) !=
98050 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
98060 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
98070 72 65 65 28 20 70 20 29 3b 0a 20 20 20 20 20 20  ree( p );.      
98080 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20      p = NULL;.  
98090 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
980a0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
980b0 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
980c0 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 76  {.      static v
980d0 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49 6e  olatile int isIn
980e0 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 74  it = 0;.      st
980f0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74  atic sqlite3_mut
98100 65 78 20 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex staticMutexes
98110 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b  [] = {.        {
98120 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49   OS2_MUTEX_INITI
98130 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20  ALIZER, },.     
98140 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49     { OS2_MUTEX_I
98150 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20  NITIALIZER, },. 
98160 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54         { OS2_MUT
98170 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20  EX_INITIALIZER, 
98180 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32  },.        { OS2
98190 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
981a0 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b  ER, },.        {
981b0 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49   OS2_MUTEX_INITI
981c0 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20  ALIZER, },.     
981d0 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49     { OS2_MUTEX_I
981e0 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20  NITIALIZER, },. 
981f0 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 69 66       };.      if
98200 20 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20   ( !isInit ){.  
98210 20 20 20 20 20 20 41 50 49 52 45 54 20 72 63 3b        APIRET rc;
98220 0a 20 20 20 20 20 20 20 20 50 54 49 42 20 70 74  .        PTIB pt
98230 69 62 3b 0a 20 20 20 20 20 20 20 20 50 50 49 42  ib;.        PPIB
98240 20 70 70 69 62 3b 0a 20 20 20 20 20 20 20 20 48   ppib;.        H
98250 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20 20 20 20  MTX mutex;.     
98260 20 20 20 63 68 61 72 20 6e 61 6d 65 5b 33 32 5d     char name[32]
98270 3b 0a 20 20 20 20 20 20 20 20 44 6f 73 47 65 74  ;.        DosGet
98280 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26 70 74 69  InfoBlocks( &pti
98290 62 2c 20 26 70 70 69 62 20 29 3b 0a 20 20 20 20  b, &ppib );.    
982a0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
982b0 69 6e 74 66 28 20 73 69 7a 65 6f 66 28 6e 61 6d  intf( sizeof(nam
982c0 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c 53 45 4d  e), name, "\\SEM
982d0 33 32 5c 5c 53 51 4c 49 54 45 25 30 34 78 22 2c  32\\SQLITE%04x",
982e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
982f0 20 20 20 20 20 20 20 20 20 20 20 70 70 69 62 2d             ppib-
98300 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b 0a 20 20  >pib_ulpid );.  
98310 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 69 73        while( !is
98320 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
98330 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20    mutex = 0;.   
98340 20 20 20 20 20 20 20 72 63 20 3d 20 44 6f 73 43         rc = DosC
98350 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 6e  reateMutexSem( n
98360 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20 30 2c 20  ame, &mutex, 0, 
98370 46 41 4c 53 45 29 3b 0a 20 20 20 20 20 20 20 20  FALSE);.        
98380 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45    if( rc == NO_E
98390 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20  RROR ){.        
983a0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
983b0 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   i;.            
983c0 69 66 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20  if( !isInit ){. 
983d0 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72               for
983e0 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 73 69 7a  ( i = 0; i < siz
983f0 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65  eof(staticMutexe
98400 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63  s)/sizeof(static
98410 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20 69 2b 2b  Mutexes[0]); i++
98420 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
98430 20 20 20 20 44 6f 73 43 72 65 61 74 65 4d 75 74      DosCreateMut
98440 65 78 53 65 6d 28 20 30 2c 20 26 73 74 61 74 69  exSem( 0, &stati
98450 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65  cMutexes[i].mute
98460 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 3b 0a 20  x, 0, FALSE );. 
98470 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
98480 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 49               isI
98490 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  nit = 1;.       
984a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
984b0 20 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78     DosCloseMutex
984c0 53 65 6d 28 20 6d 75 74 65 78 20 29 3b 0a 20 20  Sem( mutex );.  
984d0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
984e0 28 20 72 63 20 3d 3d 20 45 52 52 4f 52 5f 44 55  ( rc == ERROR_DU
984f0 50 4c 49 43 41 54 45 5f 4e 41 4d 45 20 29 7b 0a  PLICATE_NAME ){.
98500 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 53              DosS
98510 6c 65 65 70 28 20 31 20 29 3b 0a 20 20 20 20 20  leep( 1 );.     
98520 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
98530 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70          return p
98540 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
98550 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
98560 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54        assert( iT
98570 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20  ype-2 >= 0 );.  
98580 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70      assert( iTyp
98590 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 73 74 61  e-2 < sizeof(sta
985a0 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65  ticMutexes)/size
985b0 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73  of(staticMutexes
985c0 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20 70 20  [0]) );.      p 
985d0 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73  = &staticMutexes
985e0 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20  [iType-2];.     
985f0 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
98600 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
98610 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
98620 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  p;.}.../*.** Thi
98630 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f  s routine deallo
98640 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73  cates a previous
98650 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74  ly allocated mut
98660 65 78 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73  ex..** SQLite is
98670 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c   careful to deal
98680 6c 6f 63 61 74 65 20 65 76 65 72 79 20 6d 75 74  locate every mut
98690 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63  ex that it alloc
986a0 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ates..*/.static 
986b0 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 46 72 65  void os2MutexFre
986c0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
986d0 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20  *p){.  if( p==0 
986e0 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
986f0 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  rt( p->nRef==0 )
98700 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
98710 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
98720 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  FAST || p->id==S
98730 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
98740 52 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 43 6c  RSIVE );.  DosCl
98750 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 70 2d 3e  oseMutexSem( p->
98760 6d 75 74 65 78 20 29 3b 0a 20 20 73 71 6c 69 74  mutex );.  sqlit
98770 65 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 7d 0a  e3_free( p );.}.
98780 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
98790 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
987a0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
987b0 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
987c0 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20  s attempt.** to 
987d0 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20  enter a mutex.  
987e0 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  If another threa
987f0 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74  d is already wit
98800 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a  hin the mutex,.*
98810 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  * sqlite3_mutex_
98820 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f  enter() will blo
98830 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  ck and sqlite3_m
98840 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20  utex_try() will 
98850 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
98860 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69  _BUSY.  The sqli
98870 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
98880 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
98890 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75  s SQLITE_OK.** u
988a0 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65  pon successful e
988b0 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63  ntry.  Mutexes c
988c0 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c  reated using SQL
988d0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
988e0 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e  IVE can.** be en
988f0 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74  tered multiple t
98900 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65  imes by the same
98910 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63   thread.  In suc
98920 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20  h cases the,.** 
98930 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78  mutex must be ex
98940 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75  ited an equal nu
98950 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65  mber of times be
98960 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72  fore another thr
98970 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72  ead.** can enter
98980 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74  .  If the same t
98990 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65  hread tries to e
989a0 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b  nter any other k
989b0 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20  ind of mutex.** 
989c0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20  more than once, 
989d0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
989e0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74  undefined..*/.st
989f0 61 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74  atic void os2Mut
98a00 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f  exEnter(sqlite3_
98a10 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44  mutex *p){.  TID
98a20 20 74 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64   tid;.  PID hold
98a30 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c  er1;.  ULONG hol
98a40 64 65 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30  der2;.  if( p==0
98a50 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
98a60 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
98a70 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
98a80 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f  VE || os2MutexNo
98a90 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 44 6f  theld(p) );.  Do
98aa0 73 52 65 71 75 65 73 74 4d 75 74 65 78 53 65 6d  sRequestMutexSem
98ab0 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f 49  (p->mutex, SEM_I
98ac0 4e 44 45 46 49 4e 49 54 45 5f 57 41 49 54 29 3b  NDEFINITE_WAIT);
98ad0 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78  .  DosQueryMutex
98ae0 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68  Sem(p->mutex, &h
98af0 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68  older1, &tid, &h
98b00 6f 6c 64 65 72 32 29 3b 0a 20 20 70 2d 3e 6f 77  older2);.  p->ow
98b10 6e 65 72 20 3d 20 74 69 64 3b 0a 20 20 70 2d 3e  ner = tid;.  p->
98b20 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63  nRef++;.}.static
98b30 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 54 72 79   int os2MutexTry
98b40 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
98b50 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  p){.  int rc;.  
98b60 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68  TID tid;.  PID h
98b70 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20  older1;.  ULONG 
98b80 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70  holder2;.  if( p
98b90 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
98ba0 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74  ITE_OK;.  assert
98bb0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
98bc0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
98bd0 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65  || os2MutexNothe
98be0 6c 64 28 70 29 20 29 3b 0a 20 20 69 66 28 20 44  ld(p) );.  if( D
98bf0 6f 73 52 65 71 75 65 73 74 4d 75 74 65 78 53 65  osRequestMutexSe
98c00 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f  m(p->mutex, SEM_
98c10 49 4d 4d 45 44 49 41 54 45 5f 52 45 54 55 52 4e  IMMEDIATE_RETURN
98c20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 7b  ) == NO_ERROR) {
98c30 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74  .    DosQueryMut
98c40 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20  exSem(p->mutex, 
98c50 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20  &holder1, &tid, 
98c60 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 20 20 70  &holder2);.    p
98c70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a 20  ->owner = tid;. 
98c80 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20     p->nRef++;.  
98c90 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
98ca0 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
98cb0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
98cc0 59 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  Y;.  }..  return
98cd0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
98ce0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
98cf0 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20  leave() routine 
98d00 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68  exits a mutex th
98d10 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f  at was.** previo
98d20 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20  usly entered by 
98d30 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e  the same thread.
98d40 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a    The behavior.*
98d50 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69  * is undefined i
98d60 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e  f the mutex is n
98d70 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ot currently ent
98d80 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f  ered or.** is no
98d90 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f  t currently allo
98da0 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77  cated.  SQLite w
98db0 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74  ill never do eit
98dc0 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  her..*/.static v
98dd0 6f 69 64 20 6f 73 32 4d 75 74 65 78 4c 65 61 76  oid os2MutexLeav
98de0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
98df0 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a  *p){.  TID tid;.
98e00 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20    PID holder1;. 
98e10 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a   ULONG holder2;.
98e20 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
98e30 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70  urn;.  assert( p
98e40 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 44 6f  ->nRef>0 );.  Do
98e50 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
98e60 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72  ->mutex, &holder
98e70 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72  1, &tid, &holder
98e80 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  2);.  assert( p-
98e90 3e 6f 77 6e 65 72 3d 3d 74 69 64 20 29 3b 0a 20  >owner==tid );. 
98ea0 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73   p->nRef--;.  as
98eb0 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30  sert( p->nRef==0
98ec0 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54   || p->id==SQLIT
98ed0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
98ee0 45 20 29 3b 0a 20 20 44 6f 73 52 65 6c 65 61 73  E );.  DosReleas
98ef0 65 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74  eMutexSem(p->mut
98f00 65 78 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  ex);.}..#ifdef S
98f10 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a  QLITE_DEBUG./*.*
98f20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
98f30 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73  tex_held() and s
98f40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
98f50 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61  held() routine a
98f60 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66  re.** intended f
98f70 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73  or use inside as
98f80 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
98f90 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
98fa0 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 28 73 71   os2MutexHeld(sq
98fb0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
98fc0 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49  .  TID tid;.  PI
98fd0 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e 47 20 75  D pid;.  ULONG u
98fe0 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49 42 20 70  lCount;.  PTIB p
98ff0 74 69 62 3b 0a 20 20 69 66 28 20 70 21 3d 30 20  tib;.  if( p!=0 
99000 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79  ) {.    DosQuery
99010 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65  MutexSem(p->mute
99020 78 2c 20 26 70 69 64 2c 20 26 74 69 64 2c 20 26  x, &pid, &tid, &
99030 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c  ulCount);.  } el
99040 73 65 20 7b 0a 20 20 20 20 44 6f 73 47 65 74 49  se {.    DosGetI
99050 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c  nfoBlocks(&ptib,
99060 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20   NULL);.    tid 
99070 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62  = ptib->tib_ptib
99080 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20  2->tib2_ultid;. 
99090 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30   }.  return p==0
990a0 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20   || (p->nRef!=0 
990b0 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64  && p->owner==tid
990c0 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
990d0 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  os2MutexNotheld(
990e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
990f0 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20  ){.  TID tid;.  
99100 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e 47  PID pid;.  ULONG
99110 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49 42   ulCount;.  PTIB
99120 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70 21 3d   ptib;.  if( p!=
99130 20 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 51 75   0 ) {.    DosQu
99140 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d  eryMutexSem(p->m
99150 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 69 64  utex, &pid, &tid
99160 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d  , &ulCount);.  }
99170 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f 73 47   else {.    DosG
99180 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74  etInfoBlocks(&pt
99190 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74  ib, NULL);.    t
991a0 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70  id = ptib->tib_p
991b0 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64  tib2->tib2_ultid
991c0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
991d0 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 66 3d 3d  ==0 || p->nRef==
991e0 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d 74  0 || p->owner!=t
991f0 69 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 53 51  id;.}.#endif..SQ
99200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
99210 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
99220 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75  ds *sqlite3Defau
99230 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20  ltMutex(void){. 
99240 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
99250 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d  mutex_methods sM
99260 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 6f 73 32  utex = {.    os2
99270 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 6f  MutexInit,.    o
99280 73 32 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20  s2MutexEnd,.    
99290 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20  os2MutexAlloc,. 
992a0 20 20 20 6f 73 32 4d 75 74 65 78 46 72 65 65 2c     os2MutexFree,
992b0 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 45 6e 74  .    os2MutexEnt
992c0 65 72 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  er,.    os2Mutex
992d0 54 72 79 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65  Try,.    os2Mute
992e0 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53  xLeave,.#ifdef S
992f0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
99300 6f 73 32 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20  os2MutexHeld,.  
99310 20 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c    os2MutexNothel
99320 64 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20  d.#endif.  };.. 
99330 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b   return &sMutex;
99340 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
99350 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 2a 2f  ITE_MUTEX_OS2 */
99360 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
99370 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6f  * End of mutex_o
99380 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s2.c ***********
99390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
993a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
993b0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
993c0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74  * Begin file mut
993d0 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a  ex_unix.c ******
993e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
993f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99400 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67  /./*.** 2007 Aug
99410 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 28.**.** The
99420 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
99430 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
99440 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
99450 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
99460 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
99470 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
99480 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
99490 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
994a0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
994b0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
994c0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
994d0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
994e0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
994f0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
99500 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
99510 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
99520 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
99530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99570 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
99580 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
99590 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
995a0 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
995b0 65 73 20 66 6f 72 20 70 74 68 72 65 61 64 73 0a  es for pthreads.
995c0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78  **.** $Id: mutex
995d0 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 31 36 20 32  _unix.c,v 1.16 2
995e0 30 30 38 2f 31 32 2f 30 38 20 31 38 3a 31 39 3a  008/12/08 18:19:
995f0 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  18 drh Exp $.*/.
99600 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  ./*.** The code 
99610 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20  in this file is 
99620 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 77 65 20  only used if we 
99630 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68  are compiling th
99640 72 65 61 64 73 61 66 65 0a 2a 2a 20 75 6e 64 65  readsafe.** unde
99650 72 20 75 6e 69 78 20 77 69 74 68 20 70 74 68 72  r unix with pthr
99660 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  eads..**.** Note
99670 20 74 68 61 74 20 74 68 69 73 20 69 6d 70 6c 65   that this imple
99680 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75 69 72  mentation requir
99690 65 73 20 61 20 76 65 72 73 69 6f 6e 20 6f 66 20  es a version of 
996a0 70 74 68 72 65 61 64 73 20 74 68 61 74 0a 2a 2a  pthreads that.**
996b0 20 73 75 70 70 6f 72 74 73 20 72 65 63 75 72 73   supports recurs
996c0 69 76 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a  ive mutexes..*/.
996d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  #ifdef SQLITE_MU
996e0 54 45 58 5f 50 54 48 52 45 41 44 53 0a 0a 23 69  TEX_PTHREADS..#i
996f0 6e 63 6c 75 64 65 20 3c 70 74 68 72 65 61 64 2e  nclude <pthread.
99700 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  h>.../*.** Each 
99710 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
99720 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
99730 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
99740 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74  structure..*/.st
99750 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74  ruct sqlite3_mut
99760 65 78 20 7b 0a 20 20 70 74 68 72 65 61 64 5f 6d  ex {.  pthread_m
99770 75 74 65 78 5f 74 20 6d 75 74 65 78 3b 20 20 20  utex_t mutex;   
99780 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72    /* Mutex contr
99790 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  olling the lock 
997a0 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20  */.  int id;    
997b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
997c0 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f  /* Mutex type */
997d0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
997e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
997f0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 61   Number of entra
99800 6e 63 65 73 20 2a 2f 0a 20 20 70 74 68 72 65 61  nces */.  pthrea
99810 64 5f 74 20 6f 77 6e 65 72 3b 20 20 20 20 20 20  d_t owner;      
99820 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 74       /* Thread t
99830 68 61 74 20 69 73 20 77 69 74 68 69 6e 20 74 68  hat is within th
99840 69 73 20 6d 75 74 65 78 20 2a 2f 0a 23 69 66 64  is mutex */.#ifd
99850 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
99860 20 20 69 6e 74 20 74 72 61 63 65 3b 20 20 20 20    int trace;    
99870 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
99880 54 72 75 65 20 74 6f 20 74 72 61 63 65 20 63 68  True to trace ch
99890 61 6e 67 65 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  anges */.#endif.
998a0 7d 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  };.#ifdef SQLITE
998b0 5f 44 45 42 55 47 0a 23 64 65 66 69 6e 65 20 53  _DEBUG.#define S
998c0 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
998d0 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48 52 45  TIALIZER { PTHRE
998e0 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  AD_MUTEX_INITIAL
998f0 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70 74 68  IZER, 0, 0, (pth
99900 72 65 61 64 5f 74 29 30 2c 20 30 20 7d 0a 23 65  read_t)0, 0 }.#e
99910 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  lse.#define SQLI
99920 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
99930 4c 49 5a 45 52 20 7b 20 50 54 48 52 45 41 44 5f  LIZER { PTHREAD_
99940 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
99950 52 2c 20 30 2c 20 30 2c 20 28 70 74 68 72 65 61  R, 0, 0, (pthrea
99960 64 5f 74 29 30 20 7d 0a 23 65 6e 64 69 66 0a 0a  d_t)0 }.#endif..
99970 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
99980 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61  3_mutex_held() a
99990 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nd sqlite3_mutex
999a0 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69  _notheld() routi
999b0 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64  ne are.** intend
999c0 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20  ed for use only 
999d0 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
999e0 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 4f 6e 20  statements.  On 
999f0 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d 73 2c 0a  some platforms,.
99a00 2a 2a 20 74 68 65 72 65 20 6d 69 67 68 74 20 62  ** there might b
99a10 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  e race condition
99a20 73 20 74 68 61 74 20 63 61 6e 20 63 61 75 73 65  s that can cause
99a30 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
99a40 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72 20 69 6e  to.** deliver in
99a50 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e  correct results.
99a60 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c    In particular,
99a70 20 69 66 20 70 74 68 72 65 61 64 5f 65 71 75 61   if pthread_equa
99a80 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 6e  l() is.** not an
99a90 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f   atomic operatio
99aa0 6e 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f  n, then these ro
99ab0 75 74 69 6e 65 73 20 6d 69 67 68 74 20 64 65 6c  utines might del
99ac0 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f 72 72 65  ivery.** incorre
99ad0 63 74 20 72 65 73 75 6c 74 73 2e 20 20 4f 6e 20  ct results.  On 
99ae0 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d 73 2c 20  most platforms, 
99af0 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20  pthread_equal() 
99b00 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70 61 72 69  is a .** compari
99b10 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e 74 65 67  son of two integ
99b20 65 72 73 20 61 6e 64 20 69 73 20 74 68 65 72 65  ers and is there
99b30 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20 20 42 75  fore atomic.  Bu
99b40 74 20 77 65 20 61 72 65 0a 2a 2a 20 74 6f 6c 64  t we are.** told
99b50 20 74 68 61 74 20 48 50 55 58 20 69 73 20 6e 6f   that HPUX is no
99b60 74 20 73 75 63 68 20 61 20 70 6c 61 74 66 6f 72  t such a platfor
99b70 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68 65 6e 20  m.  If so, then 
99b80 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a  these routines.*
99b90 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c 77 61 79  * will not alway
99ba0 73 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79  s work correctly
99bb0 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a 2a 2a 20   on HPUX..**.** 
99bc0 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74 66 6f 72  On those platfor
99bd0 6d 73 20 77 68 65 72 65 20 70 74 68 72 65 61 64  ms where pthread
99be0 5f 65 71 75 61 6c 28 29 20 69 73 20 6e 6f 74 20  _equal() is not 
99bf0 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74 65 0a 2a  atomic, SQLite.*
99c00 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70  * should be comp
99c10 69 6c 65 64 20 77 69 74 68 6f 75 74 20 2d 44 53  iled without -DS
99c20 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20  QLITE_DEBUG and 
99c30 77 69 74 68 20 2d 44 4e 44 45 42 55 47 20 74 6f  with -DNDEBUG to
99c40 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f  .** make sure no
99c50 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
99c60 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74  ents are evaluat
99c70 65 64 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65  ed and hence the
99c80 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61  se.** routines a
99c90 72 65 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e  re never called.
99ca0 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
99cb0 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  (NDEBUG) || defi
99cc0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
99cd0 29 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68  ).static int pth
99ce0 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 73 71  readMutexHeld(sq
99cf0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
99d00 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 6e 52  .  return (p->nR
99d10 65 66 21 3d 30 20 26 26 20 70 74 68 72 65 61 64  ef!=0 && pthread
99d20 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c  _equal(p->owner,
99d30 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29   pthread_self())
99d40 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
99d50 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68  pthreadMutexNoth
99d60 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  eld(sqlite3_mute
99d70 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  x *p){.  return 
99d80 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 74  p->nRef==0 || pt
99d90 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f  hread_equal(p->o
99da0 77 6e 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65  wner, pthread_se
99db0 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23 65 6e 64  lf())==0;.}.#end
99dc0 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  if../*.** Initia
99dd0 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69  lize and deiniti
99de0 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20  alize the mutex 
99df0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
99e00 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64  atic int pthread
99e10 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b  MutexInit(void){
99e20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
99e30 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  K; }.static int 
99e40 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 64 28  pthreadMutexEnd(
99e50 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
99e60 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a  LITE_OK; }../*.*
99e70 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
99e80 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74  tex_alloc() rout
99e90 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  ine allocates a 
99ea0 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64  new.** mutex and
99eb0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
99ec0 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74  er to it.  If it
99ed0 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a   returns NULL.**
99ee0 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
99ef0 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e   a mutex could n
99f00 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e  ot be allocated.
99f10 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c    SQLite.** will
99f20 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63   unwind its stac
99f30 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20  k and return an 
99f40 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75  error.  The argu
99f50 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  ment.** to sqlit
99f60 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
99f70 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65   is one of these
99f80 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
99f90 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  ts:.**.** <ul>.*
99fa0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
99fb0 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69  UTEX_FAST.** <li
99fc0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
99fd0 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69  RECURSIVE.** <li
99fe0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
99ff0 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a  STATIC_MASTER.**
9a000 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
9a010 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a  TEX_STATIC_MEM.*
9a020 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
9a030 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32  UTEX_STATIC_MEM2
9a040 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
9a050 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52  _MUTEX_STATIC_PR
9a060 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  NG.** <li>  SQLI
9a070 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
9a080 4c 52 55 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  LRU.** </ul>.**.
9a090 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f  ** The first two
9a0a0 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65   constants cause
9a0b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
9a0c0 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65  lloc() to create
9a0d0 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e  .** a new mutex.
9a0e0 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20    The new mutex 
9a0f0 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65  is recursive whe
9a100 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
9a110 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75  ECURSIVE.** is u
9a120 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65  sed but not nece
9a130 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20  ssarily so when 
9a140 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
9a150 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68  T is used..** Th
9a160 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
9a170 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
9a180 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
9a190 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65  istinction.** be
9a1a0 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54  tween SQLITE_MUT
9a1b0 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64  EX_RECURSIVE and
9a1c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
9a1d0 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a  ST if it does.**
9a1e0 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42   not want to.  B
9a1f0 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  ut SQLite will o
9a200 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65  nly request a re
9a210 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e  cursive mutex in
9a220 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20  .** cases where 
9a230 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20  it really needs 
9a240 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65  one.  If a faste
9a250 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20  r non-recursive 
9a260 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65  mutex.** impleme
9a270 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  ntation is avail
9a280 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74  able on the host
9a290 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d   platform, the m
9a2a0 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a  utex subsystem.*
9a2b0 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73  * might return s
9a2c0 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72  uch a mutex in r
9a2d0 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54  esponse to SQLIT
9a2e0 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a  E_MUTEX_FAST..**
9a2f0 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c  .** The other al
9a300 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73  lowed parameters
9a310 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
9a320 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72  x_alloc() each r
9a330 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
9a340 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70  er to a static p
9a350 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78  reexisting mutex
9a360 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20  .  Three static 
9a370 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75  mutexes are.** u
9a380 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65  sed by the curre
9a390 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  nt version of SQ
9a3a0 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65  Lite.  Future ve
9a3b0 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
9a3c0 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69  .** may add addi
9a3d0 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75  tional static mu
9a3e0 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d  texes.  Static m
9a3f0 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69  utexes are for i
9a400 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62  nternal.** use b
9a410 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20  y SQLite only.  
9a420 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  Applications tha
9a430 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74  t use SQLite mut
9a440 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75  exes should.** u
9a450 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61  se only the dyna
9a460 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75  mic mutexes retu
9a470 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d  rned by SQLITE_M
9a480 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20  UTEX_FAST or.** 
9a490 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
9a4a0 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  URSIVE..**.** No
9a4b0 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f  te that if one o
9a4c0 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  f the dynamic mu
9a4d0 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28  tex parameters (
9a4e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
9a4f0 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d  T.** or SQLITE_M
9a500 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20  UTEX_RECURSIVE) 
9a510 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c  is used then sql
9a520 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
9a530 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
9a540 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20  different mutex 
9a550 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20  on every call.  
9a560 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74  But for the stat
9a570 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70  ic .** mutex typ
9a580 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74  es, the same mut
9a590 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f  ex is returned o
9a5a0 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61  n every call tha
9a5b0 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d  t has.** the sam
9a5c0 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a  e type number..*
9a5d0 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
9a5e0 5f 6d 75 74 65 78 20 2a 70 74 68 72 65 61 64 4d  _mutex *pthreadM
9a5f0 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54  utexAlloc(int iT
9a600 79 70 65 29 7b 0a 20 20 73 74 61 74 69 63 20 73  ype){.  static s
9a610 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61  qlite3_mutex sta
9a620 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b  ticMutexes[] = {
9a630 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54  .    SQLITE3_MUT
9a640 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a  EX_INITIALIZER,.
9a650 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45      SQLITE3_MUTE
9a660 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20  X_INITIALIZER,. 
9a670 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58     SQLITE3_MUTEX
9a680 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20  _INITIALIZER,.  
9a690 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f    SQLITE3_MUTEX_
9a6a0 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20  INITIALIZER,.   
9a6b0 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49   SQLITE3_MUTEX_I
9a6c0 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20  NITIALIZER,.    
9a6d0 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e  SQLITE3_MUTEX_IN
9a6e0 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d 3b 0a 20  ITIALIZER.  };. 
9a6f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
9a700 70 3b 0a 20 20 73 77 69 74 63 68 28 20 69 54 79  p;.  switch( iTy
9a710 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  pe ){.    case S
9a720 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
9a730 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70  RSIVE: {.      p
9a740 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
9a750 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29  Zero( sizeof(*p)
9a760 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20   );.      if( p 
9a770 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
9a780 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52  _HOMEGROWN_RECUR
9a790 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 20 20 20  SIVE_MUTEX.     
9a7a0 20 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69     /* If recursi
9a7b0 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e  ve mutexes are n
9a7c0 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 77 65  ot available, we
9a7d0 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 0a 20 20   will have to.  
9a7e0 20 20 20 20 20 20 2a 2a 20 62 75 69 6c 64 20 6f        ** build o
9a7f0 75 72 20 6f 77 6e 2e 20 20 53 65 65 20 62 65 6c  ur own.  See bel
9a800 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70  ow. */.        p
9a810 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69  thread_mutex_ini
9a820 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 30 29 3b  t(&p->mutex, 0);
9a830 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f  .#else.        /
9a840 2a 20 55 73 65 20 61 20 72 65 63 75 72 73 69 76  * Use a recursiv
9a850 65 20 6d 75 74 65 78 20 69 66 20 69 74 20 69 73  e mutex if it is
9a860 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20   available */.  
9a870 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75        pthread_mu
9a880 74 65 78 61 74 74 72 5f 74 20 72 65 63 75 72 73  texattr_t recurs
9a890 69 76 65 41 74 74 72 3b 0a 20 20 20 20 20 20 20  iveAttr;.       
9a8a0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74   pthread_mutexat
9a8b0 74 72 5f 69 6e 69 74 28 26 72 65 63 75 72 73 69  tr_init(&recursi
9a8c0 76 65 41 74 74 72 29 3b 0a 20 20 20 20 20 20 20  veAttr);.       
9a8d0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74   pthread_mutexat
9a8e0 74 72 5f 73 65 74 74 79 70 65 28 26 72 65 63 75  tr_settype(&recu
9a8f0 72 73 69 76 65 41 74 74 72 2c 20 50 54 48 52 45  rsiveAttr, PTHRE
9a900 41 44 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  AD_MUTEX_RECURSI
9a910 56 45 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68  VE);.        pth
9a920 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28  read_mutex_init(
9a930 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72 65 63 75  &p->mutex, &recu
9a940 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20  rsiveAttr);.    
9a950 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
9a960 78 61 74 74 72 5f 64 65 73 74 72 6f 79 28 26 72  xattr_destroy(&r
9a970 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 23  ecursiveAttr);.#
9a980 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 70 2d  endif.        p-
9a990 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20  >id = iType;.   
9a9a0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
9a9b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
9a9c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
9a9d0 53 54 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20  ST: {.      p = 
9a9e0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
9a9f0 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b  o( sizeof(*p) );
9aa00 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a  .      if( p ){.
9aa10 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20          p->id = 
9aa20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 70  iType;.        p
9aa30 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69  thread_mutex_ini
9aa40 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 30 29 3b  t(&p->mutex, 0);
9aa50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
9aa60 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
9aa70 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
9aa80 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32   assert( iType-2
9aa90 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61   >= 0 );.      a
9aaa0 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c  ssert( iType-2 <
9aab0 20 41 72 72 61 79 53 69 7a 65 28 73 74 61 74 69   ArraySize(stati
9aac0 63 4d 75 74 65 78 65 73 29 20 29 3b 0a 20 20 20  cMutexes) );.   
9aad0 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75     p = &staticMu
9aae0 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a  texes[iType-2];.
9aaf0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54        p->id = iT
9ab00 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ype;.      break
9ab10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
9ab20 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn p;.}.../*.*
9ab30 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
9ab40 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65  eallocates a pre
9ab50 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63  viously.** alloc
9ab60 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 51 4c  ated mutex.  SQL
9ab70 69 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74  ite is careful t
9ab80 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65  o deallocate eve
9ab90 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 61 74  ry.** mutex that
9aba0 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a   it allocates..*
9abb0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74  /.static void pt
9abc0 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 28 73  hreadMutexFree(s
9abd0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
9abe0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  {.  assert( p->n
9abf0 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Ref==0 );.  asse
9ac00 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
9ac10 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20  E_MUTEX_FAST || 
9ac20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
9ac30 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b  TEX_RECURSIVE );
9ac40 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  .  pthread_mutex
9ac50 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e 6d 75 74  _destroy(&p->mut
9ac60 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ex);.  sqlite3_f
9ac70 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ree(p);.}../*.**
9ac80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
9ac90 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73  ex_enter() and s
9aca0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
9acb0 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65  () routines atte
9acc0 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20  mpt.** to enter 
9acd0 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f  a mutex.  If ano
9ace0 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61  ther thread is a
9acf0 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68  lready within th
9ad00 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69  e mutex,.** sqli
9ad10 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
9ad20 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64  ) will block and
9ad30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
9ad40 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e  ry() will return
9ad50 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  .** SQLITE_BUSY.
9ad60 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75    The sqlite3_mu
9ad70 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66  tex_try() interf
9ad80 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ace returns SQLI
9ad90 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75  TE_OK.** upon su
9ada0 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20  ccessful entry. 
9adb0 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64   Mutexes created
9adc0 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55   using SQLITE_MU
9add0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61  TEX_RECURSIVE ca
9ade0 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20  n.** be entered 
9adf0 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62  multiple times b
9ae00 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  y the same threa
9ae10 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65  d.  In such case
9ae20 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20  s the,.** mutex 
9ae30 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61  must be exited a
9ae40 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f  n equal number o
9ae50 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61  f times before a
9ae60 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a  nother thread.**
9ae70 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20   can enter.  If 
9ae80 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
9ae90 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61  tries to enter a
9aea0 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66  ny other kind of
9aeb0 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74   mutex.** more t
9aec0 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65  han once, the be
9aed0 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
9aee0 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ned..*/.static v
9aef0 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78  oid pthreadMutex
9af00 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  Enter(sqlite3_mu
9af10 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  tex *p){.  asser
9af20 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
9af30 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
9af40 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78   || pthreadMutex
9af50 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23  Notheld(p) );..#
9af60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d  ifdef SQLITE_HOM
9af70 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45  EGROWN_RECURSIVE
9af80 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72  _MUTEX.  /* If r
9af90 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
9afa0 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62   are not availab
9afb0 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65  le, then we have
9afc0 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75   to grow.  ** ou
9afd0 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70  r own.  This imp
9afe0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75  lementation assu
9aff0 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 64  mes that pthread
9b000 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73  _equal().  ** is
9b010 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69   atomic - that i
9b020 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65  t cannot be dece
9b030 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69  ived into thinki
9b040 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64  ng self.  ** and
9b050 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71   p->owner are eq
9b060 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20  ual if p->owner 
9b070 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20  changes between 
9b080 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20  two values.  ** 
9b090 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75  that are not equ
9b0a0 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65  al to self while
9b0b0 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
9b0c0 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e  is taking place.
9b0d0 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  .  ** This imple
9b0e0 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61  mentation also a
9b0f0 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e  ssumes a coheren
9b100 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a  t cache - that .
9b110 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72    ** separate pr
9b120 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72  ocesses cannot r
9b130 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 61  ead different va
9b140 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61  lues from the sa
9b150 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20  me.  ** address 
9b160 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
9b170 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 20  .  If either of 
9b180 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74  these two condit
9b190 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f  ions.  ** are no
9b1a0 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20  t met, then the 
9b1b0 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69  mutexes will fai
9b1c0 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77  l and problems w
9b1d0 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f  ill result..  */
9b1e0 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64  .  {.    pthread
9b1f0 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61  _t self = pthrea
9b200 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66  d_self();.    if
9b210 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70  ( p->nRef>0 && p
9b220 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e  thread_equal(p->
9b230 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a  owner, self) ){.
9b240 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b        p->nRef++;
9b250 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
9b260 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
9b270 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b  lock(&p->mutex);
9b280 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
9b290 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20  ->nRef==0 );.   
9b2a0 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65     p->owner = se
9b2b0 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65  lf;.      p->nRe
9b2c0 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  f = 1;.    }.  }
9b2d0 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20  .#else.  /* Use 
9b2e0 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63  the built-in rec
9b2f0 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69  ursive mutexes i
9b300 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c  f they are avail
9b310 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 74 68  able..  */.  pth
9b320 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28  read_mutex_lock(
9b330 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d  &p->mutex);.  p-
9b340 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64  >owner = pthread
9b350 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d 3e 6e 52  _self();.  p->nR
9b360 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69  ef++;.#endif..#i
9b370 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
9b380 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65  G.  if( p->trace
9b390 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22   ){.    printf("
9b3a0 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20 28  enter mutex %p (
9b3b0 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64  %d) with nRef=%d
9b3c0 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65  \n", p, p->trace
9b3d0 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a  , p->nRef);.  }.
9b3e0 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69 63 20  #endif.}.static 
9b3f0 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78  int pthreadMutex
9b400 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  Try(sqlite3_mute
9b410 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  x *p){.  int rc;
9b420 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
9b430 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
9b440 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72  ECURSIVE || pthr
9b450 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  eadMutexNotheld(
9b460 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  p) );..#ifdef SQ
9b470 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52  LITE_HOMEGROWN_R
9b480 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20  ECURSIVE_MUTEX. 
9b490 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 65   /* If recursive
9b4a0 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74   mutexes are not
9b4b0 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e   available, then
9b4c0 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77   we have to grow
9b4d0 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20  .  ** our own.  
9b4e0 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  This implementat
9b4f0 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74  ion assumes that
9b500 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29   pthread_equal()
9b510 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20  .  ** is atomic 
9b520 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74  - that it cannot
9b530 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e 74   be deceived int
9b540 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a  o thinking self.
9b550 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65    ** and p->owne
9b560 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 70  r are equal if p
9b570 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20  ->owner changes 
9b580 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75  between two valu
9b590 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65  es.  ** that are
9b5a0 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65   not equal to se
9b5b0 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d  lf while the com
9b5c0 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e  parison is takin
9b5d0 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68  g place..  ** Th
9b5e0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
9b5f0 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61  n also assumes a
9b600 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20   coherent cache 
9b610 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70  - that .  ** sep
9b620 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20  arate processes 
9b630 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66  cannot read diff
9b640 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f  erent values fro
9b650 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20  m the same.  ** 
9b660 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 73  address at the s
9b670 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69  ame time.  If ei
9b680 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77  ther of these tw
9b690 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a  o conditions.  *
9b6a0 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74  * are not met, t
9b6b0 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20  hen the mutexes 
9b6c0 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72  will fail and pr
9b6d0 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75  oblems will resu
9b6e0 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20  lt..  */.  {.   
9b6f0 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20   pthread_t self 
9b700 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
9b710 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65  ;.    if( p->nRe
9b720 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f 65  f>0 && pthread_e
9b730 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73  qual(p->owner, s
9b740 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d  elf) ){.      p-
9b750 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 72  >nRef++;.      r
9b760 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
9b770 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 74 68     }else if( pth
9b780 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 79 6c 6f  read_mutex_trylo
9b790 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3d 3d 30  ck(&p->mutex)==0
9b7a0 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
9b7b0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a  ( p->nRef==0 );.
9b7c0 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d        p->owner =
9b7d0 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e   self;.      p->
9b7e0 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  nRef = 1;.      
9b7f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
9b800 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9b810 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
9b820 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c  Y;.    }.  }.#el
9b830 73 65 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20  se.  /* Use the 
9b840 62 75 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69  built-in recursi
9b850 76 65 20 6d 75 74 65 78 65 73 20 69 66 20 74 68  ve mutexes if th
9b860 65 79 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  ey are available
9b870 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 74 68  ..  */.  if( pth
9b880 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 79 6c 6f  read_mutex_trylo
9b890 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3d 3d 30  ck(&p->mutex)==0
9b8a0 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72   ){.    p->owner
9b8b0 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28   = pthread_self(
9b8c0 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b  );.    p->nRef++
9b8d0 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
9b8e0 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
9b8f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
9b900 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  USY;.  }.#endif.
9b910 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
9b920 45 42 55 47 0a 20 20 69 66 28 20 72 63 3d 3d 53  EBUG.  if( rc==S
9b930 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 74  QLITE_OK && p->t
9b940 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e  race ){.    prin
9b950 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20  tf("enter mutex 
9b960 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65  %p (%d) with nRe
9b970 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74  f=%d\n", p, p->t
9b980 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a  race, p->nRef);.
9b990 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
9b9a0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
9b9b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
9b9c0 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
9b9d0 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
9b9e0 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
9b9f0 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
9ba00 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
9ba10 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f  ad.  The behavio
9ba20 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65  r.** is undefine
9ba30 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69  d if the mutex i
9ba40 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
9ba50 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73  entered or.** is
9ba60 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61   not currently a
9ba70 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
9ba80 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20  e will never do 
9ba90 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  either..*/.stati
9baa0 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75  c void pthreadMu
9bab0 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33  texLeave(sqlite3
9bac0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
9bad0 73 65 72 74 28 20 70 74 68 72 65 61 64 4d 75 74  sert( pthreadMut
9bae0 65 78 48 65 6c 64 28 70 29 20 29 3b 0a 20 20 70  exHeld(p) );.  p
9baf0 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65  ->nRef--;.  asse
9bb00 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c  rt( p->nRef==0 |
9bb10 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  | p->id==SQLITE_
9bb20 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
9bb30 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
9bb40 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55  E_HOMEGROWN_RECU
9bb50 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 69 66  RSIVE_MUTEX.  if
9bb60 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  ( p->nRef==0 ){.
9bb70 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
9bb80 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  x_unlock(&p->mut
9bb90 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  ex);.  }.#else. 
9bba0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75   pthread_mutex_u
9bbb0 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  nlock(&p->mutex)
9bbc0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
9bbd0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
9bbe0 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a  if( p->trace ){.
9bbf0 20 20 20 20 70 72 69 6e 74 66 28 22 6c 65 61 76      printf("leav
9bc00 65 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20  e mutex %p (%d) 
9bc10 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c  with nRef=%d\n",
9bc20 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d   p, p->trace, p-
9bc30 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64  >nRef);.  }.#end
9bc40 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  if.}..SQLITE_PRI
9bc50 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74  VATE sqlite3_mut
9bc60 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  ex_methods *sqli
9bc70 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28  te3DefaultMutex(
9bc80 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
9bc90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
9bca0 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b  thods sMutex = {
9bcb0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65  .    pthreadMute
9bcc0 78 49 6e 69 74 2c 0a 20 20 20 20 70 74 68 72 65  xInit,.    pthre
9bcd0 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20  adMutexEnd,.    
9bce0 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f  pthreadMutexAllo
9bcf0 63 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  c,.    pthreadMu
9bd00 74 65 78 46 72 65 65 2c 0a 20 20 20 20 70 74 68  texFree,.    pth
9bd10 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 2c 0a  readMutexEnter,.
9bd20 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78      pthreadMutex
9bd30 54 72 79 2c 0a 20 20 20 20 70 74 68 72 65 61 64  Try,.    pthread
9bd40 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64  MutexLeave,.#ifd
9bd50 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
9bd60 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78      pthreadMutex
9bd70 48 65 6c 64 2c 0a 20 20 20 20 70 74 68 72 65 61  Held,.    pthrea
9bd80 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65  dMutexNotheld.#e
9bd90 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20 20 20 30  lse.    0,.    0
9bda0 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20  .#endif.  };..  
9bdb0 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a  return &sMutex;.
9bdc0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
9bdd0 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
9bde0 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  D */../*********
9bdf0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74  ***** End of mut
9be00 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a  ex_unix.c ******
9be10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9be20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9be30 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
9be40 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
9be50 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a   mutex_w32.c ***
9be60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9be70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9be80 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
9be90 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a   August 14.**.**
9bea0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
9beb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
9bec0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
9bed0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
9bee0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
9bef0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
9bf00 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
9bf10 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
9bf20 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
9bf30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
9bf40 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
9bf50 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
9bf60 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
9bf70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
9bf80 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
9bf90 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
9bfa0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
9bfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9bfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9bfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9bfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9bff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
9c000 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
9c010 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20  the C functions 
9c020 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d  that implement m
9c030 75 74 65 78 65 73 20 66 6f 72 20 77 69 6e 33 32  utexes for win32
9c040 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65  .**.** $Id: mute
9c050 78 5f 77 33 32 2e 63 2c 76 20 31 2e 31 33 20 32  x_w32.c,v 1.13 2
9c060 30 30 38 2f 31 32 2f 30 38 20 31 38 3a 31 39 3a  008/12/08 18:19:
9c070 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  18 drh Exp $.*/.
9c080 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  ./*.** The code 
9c090 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20  in this file is 
9c0a0 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 77 65 20  only used if we 
9c0b0 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 20 6d 75  are compiling mu
9c0c0 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 6f  ltithreaded.** o
9c0d0 6e 20 61 20 77 69 6e 33 32 20 73 79 73 74 65 6d  n a win32 system
9c0e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
9c0f0 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 0a 2f 2a  TE_MUTEX_W32../*
9c100 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69  .** Each recursi
9c110 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69  ve mutex is an i
9c120 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
9c130 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
9c140 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  re..*/.struct sq
9c150 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20  lite3_mutex {.  
9c160 43 52 49 54 49 43 41 4c 5f 53 45 43 54 49 4f 4e  CRITICAL_SECTION
9c170 20 6d 75 74 65 78 3b 20 20 20 20 2f 2a 20 4d 75   mutex;    /* Mu
9c180 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20  tex controlling 
9c190 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  the lock */.  in
9c1a0 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  t id;           
9c1b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65           /* Mute
9c1c0 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20  x type */.  int 
9c1d0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
9c1e0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
9c1f0 20 6f 66 20 65 6e 74 65 72 61 6e 63 65 73 20 2a   of enterances *
9c200 2f 0a 20 20 44 57 4f 52 44 20 6f 77 6e 65 72 3b  /.  DWORD owner;
9c210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
9c220 2a 20 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67  * Thread holding
9c230 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d   this mutex */.}
9c240 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  ;../*.** Return 
9c250 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20  true (non-zero) 
9c260 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e  if we are runnin
9c270 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57  g under WinNT, W
9c280 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20  in2K, WinXP,.** 
9c290 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 75 72  or WinCE.  Retur
9c2a0 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 20 66  n false (zero) f
9c2b0 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c  or Win95, Win98,
9c2c0 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a   or WinME..**.**
9c2d0 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65   Here is an inte
9c2e0 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76 61 74  resting observat
9c2f0 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e  ion:  Win95, Win
9c300 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61  98, and WinME la
9c310 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69  ck.** the LockFi
9c320 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42 75 74  leEx() API.  But
9c330 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 74   we can still st
9c340 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67  atically link ag
9c350 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41 50  ainst that.** AP
9c360 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20  I as long as we 
9c370 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 69  don't call it wi
9c380 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f  n running Win95/
9c390 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74  98/ME.  A call t
9c3a0 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  o.** this routin
9c3b0 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74  e is used to det
9c3c0 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 68 6f  ermine if the ho
9c3d0 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d  st is Win95/98/M
9c3e0 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b  E or.** WinNT/2K
9c3f0 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65 20 77  /XP so that we w
9c400 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65 72  ill know whether
9c410 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e 20 73   or not we can s
9c420 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68  afely call.** th
9c430 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41  e LockFileEx() A
9c440 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49  PI..**.** mutexI
9c450 73 4e 54 28 29 20 69 73 20 6f 6e 6c 79 20 75 73  sNT() is only us
9c460 65 64 20 66 6f 72 20 74 68 65 20 54 72 79 45 6e  ed for the TryEn
9c470 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
9c480 6f 6e 28 29 20 41 50 49 20 63 61 6c 6c 2c 0a 2a  on() API call,.*
9c490 2a 20 77 68 69 63 68 20 69 73 20 6f 6e 6c 79 20  * which is only 
9c4a0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 79 6f 75  available if you
9c4b0 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 61  r application wa
9c4c0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  s compiled with 
9c4d0 0a 2a 2a 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54  .** _WIN32_WINNT
9c4e0 20 64 65 66 69 6e 65 64 20 74 6f 20 61 20 76 61   defined to a va
9c4f0 6c 75 65 20 3e 3d 20 30 78 30 34 30 30 2e 20 20  lue >= 0x0400.  
9c500 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f  Currently, the o
9c510 6e 6c 79 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 54  nly.** call to T
9c520 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53  ryEnterCriticalS
9c530 65 63 74 69 6f 6e 28 29 20 69 73 20 23 69 66 64  ection() is #ifd
9c540 65 66 27 65 64 20 6f 75 74 2c 20 73 6f 20 23 69  ef'ed out, so #i
9c550 66 64 65 66 20 0a 2a 2a 20 74 68 69 73 20 6f 75  fdef .** this ou
9c560 74 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69  t as well..*/.#i
9c570 66 20 30 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  f 0.#if SQLITE_O
9c580 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65  S_WINCE.# define
9c590 20 6d 75 74 65 78 49 73 4e 54 28 29 20 20 28 31   mutexIsNT()  (1
9c5a0 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 69 63  ).#else.  static
9c5b0 20 69 6e 74 20 6d 75 74 65 78 49 73 4e 54 28 76   int mutexIsNT(v
9c5c0 6f 69 64 29 7b 0a 20 20 20 20 73 74 61 74 69 63  oid){.    static
9c5d0 20 69 6e 74 20 6f 73 54 79 70 65 20 3d 20 30 3b   int osType = 0;
9c5e0 0a 20 20 20 20 69 66 28 20 6f 73 54 79 70 65 3d  .    if( osType=
9c5f0 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45  =0 ){.      OSVE
9c600 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b  RSIONINFO sInfo;
9c610 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f  .      sInfo.dwO
9c620 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65  SVersionInfoSize
9c630 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29   = sizeof(sInfo)
9c640 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69  ;.      GetVersi
9c650 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20  onEx(&sInfo);.  
9c660 20 20 20 20 6f 73 54 79 70 65 20 3d 20 73 49 6e      osType = sIn
9c670 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d  fo.dwPlatformId=
9c680 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49  =VER_PLATFORM_WI
9c690 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a  N32_NT ? 2 : 1;.
9c6a0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
9c6b0 20 6f 73 54 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a   osType==2;.  }.
9c6c0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
9c6d0 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 23 65 6e  _OS_WINCE */.#en
9c6e0 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
9c6f0 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54  TE_DEBUG./*.** T
9c700 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
9c710 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69  _held() and sqli
9c720 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
9c730 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a  d() routine are.
9c740 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  ** intended for 
9c750 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20  use only inside 
9c760 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
9c770 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  nts..*/.static i
9c780 6e 74 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 28  nt winMutexHeld(
9c790 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
9c7a0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  ){.  return p->n
9c7b0 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e  Ref!=0 && p->own
9c7c0 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68  er==GetCurrentTh
9c7d0 72 65 61 64 49 64 28 29 3b 0a 7d 0a 73 74 61 74  readId();.}.stat
9c7e0 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 4e  ic int winMutexN
9c7f0 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
9c800 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
9c810 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c  rn p->nRef==0 ||
9c820 20 70 2d 3e 6f 77 6e 65 72 21 3d 47 65 74 43 75   p->owner!=GetCu
9c830 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b  rrentThreadId();
9c840 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
9c850 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64  * Initialize and
9c860 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   deinitialize th
9c870 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
9c880 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m..*/.static int
9c890 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 76 6f   winMutexInit(vo
9c8a0 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
9c8b0 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20  TE_OK; }.static 
9c8c0 69 6e 74 20 77 69 6e 4d 75 74 65 78 45 6e 64 28  int winMutexEnd(
9c8d0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
9c8e0 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a  LITE_OK; }../*.*
9c8f0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
9c900 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74  tex_alloc() rout
9c910 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  ine allocates a 
9c920 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64  new.** mutex and
9c930 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
9c940 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74  er to it.  If it
9c950 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a   returns NULL.**
9c960 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
9c970 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e   a mutex could n
9c980 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e  ot be allocated.
9c990 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c    SQLite.** will
9c9a0 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63   unwind its stac
9c9b0 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20  k and return an 
9c9c0 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75  error.  The argu
9c9d0 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  ment.** to sqlit
9c9e0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
9c9f0 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65   is one of these
9ca00 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
9ca10 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  ts:.**.** <ul>.*
9ca20 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
9ca30 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 20  UTEX_FAST       
9ca40 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69          0.** <li
9ca50 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
9ca60 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20  RECURSIVE       
9ca70 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51     1.** <li>  SQ
9ca80 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
9ca90 43 5f 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a  C_MASTER      2.
9caa0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
9cab0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
9cac0 20 20 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c           3.** <l
9cad0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
9cae0 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20  _STATIC_PRNG    
9caf0 20 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a      4.** </ul>.*
9cb00 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
9cb10 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75  wo constants cau
9cb20 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  se sqlite3_mutex
9cb30 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61  _alloc() to crea
9cb40 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65  te.** a new mute
9cb50 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65  x.  The new mute
9cb60 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77  x is recursive w
9cb70 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
9cb80 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73  _RECURSIVE.** is
9cb90 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65   used but not ne
9cba0 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65  cessarily so whe
9cbb0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  n SQLITE_MUTEX_F
9cbc0 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20  AST is used..** 
9cbd0 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  The mutex implem
9cbe0 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  entation does no
9cbf0 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61  t need to make a
9cc00 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20   distinction.** 
9cc10 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d  between SQLITE_M
9cc20 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61  UTEX_RECURSIVE a
9cc30 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  nd SQLITE_MUTEX_
9cc40 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a  FAST if it does.
9cc50 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20  ** not want to. 
9cc60 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c   But SQLite will
9cc70 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20   only request a 
9cc80 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
9cc90 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72  in.** cases wher
9cca0 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64  e it really need
9ccb0 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73  s one.  If a fas
9ccc0 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76  ter non-recursiv
9ccd0 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65  e mutex.** imple
9cce0 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61  mentation is ava
9ccf0 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f  ilable on the ho
9cd00 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65  st platform, the
9cd10 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
9cd20 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e  .** might return
9cd30 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e   such a mutex in
9cd40 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c   response to SQL
9cd50 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a  ITE_MUTEX_FAST..
9cd60 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20  **.** The other 
9cd70 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65  allowed paramete
9cd80 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  rs to sqlite3_mu
9cd90 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68  tex_alloc() each
9cda0 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   return.** a poi
9cdb0 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63  nter to a static
9cdc0 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74   preexisting mut
9cdd0 65 78 2e 20 20 54 68 72 65 65 20 73 74 61 74 69  ex.  Three stati
9cde0 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a  c mutexes are.**
9cdf0 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72   used by the cur
9ce00 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20  rent version of 
9ce10 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20  SQLite.  Future 
9ce20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
9ce30 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64  te.** may add ad
9ce40 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20  ditional static 
9ce50 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63  mutexes.  Static
9ce60 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72   mutexes are for
9ce70 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65   internal.** use
9ce80 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e   by SQLite only.
9ce90 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74    Applications t
9cea0 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d  hat use SQLite m
9ceb0 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a  utexes should.**
9cec0 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79   use only the dy
9ced0 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65  namic mutexes re
9cee0 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45  turned by SQLITE
9cef0 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a  _MUTEX_FAST or.*
9cf00 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  * SQLITE_MUTEX_R
9cf10 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20  ECURSIVE..**.** 
9cf20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65  Note that if one
9cf30 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20   of the dynamic 
9cf40 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73  mutex parameters
9cf50 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46   (SQLITE_MUTEX_F
9cf60 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45  AST.** or SQLITE
9cf70 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
9cf80 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73  ) is used then s
9cf90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
9cfa0 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20  oc().** returns 
9cfb0 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65  a different mute
9cfc0 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e  x on every call.
9cfd0 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74    But for the st
9cfe0 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74  atic .** mutex t
9cff0 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d  ypes, the same m
9d000 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 64  utex is returned
9d010 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74   on every call t
9d020 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73  hat has.** the s
9d030 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e  ame type number.
9d040 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
9d050 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d 75 74  e3_mutex *winMut
9d060 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70  exAlloc(int iTyp
9d070 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  e){.  sqlite3_mu
9d080 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69 74 63  tex *p;..  switc
9d090 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20  h( iType ){.    
9d0a0 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  case SQLITE_MUTE
9d0b0 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65  X_FAST:.    case
9d0c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
9d0d0 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20  CURSIVE: {.     
9d0e0 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c   p = sqlite3Mall
9d0f0 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a  ocZero( sizeof(*
9d100 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  p) );.      if( 
9d110 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  p ){.        p->
9d120 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20  id = iType;.    
9d130 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72      InitializeCr
9d140 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70  iticalSection(&p
9d150 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
9d160 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
9d170 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74     }.    default
9d180 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63  : {.      static
9d190 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73   sqlite3_mutex s
9d1a0 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b  taticMutexes[6];
9d1b0 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 69 6e  .      static in
9d1c0 74 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20  t isInit = 0;.  
9d1d0 20 20 20 20 77 68 69 6c 65 28 20 21 69 73 49 6e      while( !isIn
9d1e0 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74  it ){.        st
9d1f0 61 74 69 63 20 6c 6f 6e 67 20 6c 6f 63 6b 20 3d  atic long lock =
9d200 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   0;.        if( 
9d210 49 6e 74 65 72 6c 6f 63 6b 65 64 49 6e 63 72 65  InterlockedIncre
9d220 6d 65 6e 74 28 26 6c 6f 63 6b 29 3d 3d 31 20 29  ment(&lock)==1 )
9d230 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
9d240 69 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  i;.          for
9d250 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 73  (i=0; i<sizeof(s
9d260 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69  taticMutexes)/si
9d270 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78  zeof(staticMutex
9d280 65 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20  es[0]); i++){.  
9d290 20 20 20 20 20 20 20 20 20 20 49 6e 69 74 69 61            Initia
9d2a0 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74  lizeCriticalSect
9d2b0 69 6f 6e 28 26 73 74 61 74 69 63 4d 75 74 65 78  ion(&staticMutex
9d2c0 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20  es[i].mutex);.  
9d2d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9d2e0 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 3b 0a      isInit = 1;.
9d2f0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
9d300 20 20 20 20 20 20 20 20 20 53 6c 65 65 70 28 31           Sleep(1
9d310 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
9d320 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
9d330 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20  t( iType-2 >= 0 
9d340 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
9d350 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f   iType-2 < sizeo
9d360 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29  f(staticMutexes)
9d370 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75  /sizeof(staticMu
9d380 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20  texes[0]) );.   
9d390 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75     p = &staticMu
9d3a0 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a  texes[iType-2];.
9d3b0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54        p->id = iT
9d3c0 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ype;.      break
9d3d0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
9d3e0 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn p;.}.../*.*
9d3f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
9d400 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65  eallocates a pre
9d410 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63  viously.** alloc
9d420 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 51 4c  ated mutex.  SQL
9d430 69 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74  ite is careful t
9d440 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65  o deallocate eve
9d450 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 61 74  ry.** mutex that
9d460 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a   it allocates..*
9d470 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69  /.static void wi
9d480 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74  nMutexFree(sqlit
9d490 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
9d4a0 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61  assert( p );.  a
9d4b0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
9d4c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
9d4d0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
9d4e0 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64  EX_FAST || p->id
9d4f0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
9d500 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44 65  ECURSIVE );.  De
9d510 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65 63 74  leteCriticalSect
9d520 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a  ion(&p->mutex);.
9d530 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
9d540 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
9d550 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
9d560 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ter() and sqlite
9d570 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
9d580 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a  utines attempt.*
9d590 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74  * to enter a mut
9d5a0 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20  ex.  If another 
9d5b0 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64  thread is alread
9d5c0 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74  y within the mut
9d5d0 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  ex,.** sqlite3_m
9d5e0 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c  utex_enter() wil
9d5f0 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69  l block and sqli
9d600 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
9d610 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53  will return.** S
9d620 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65  QLITE_BUSY.  The
9d630 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
9d640 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ry() interface r
9d650 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
9d660 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73  .** upon success
9d670 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65  ful entry.  Mute
9d680 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e  xes created usin
9d690 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  g SQLITE_MUTEX_R
9d6a0 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20  ECURSIVE can.** 
9d6b0 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69  be entered multi
9d6c0 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65  ple times by the
9d6d0 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49   same thread.  I
9d6e0 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65  n such cases the
9d6f0 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20  ,.** mutex must 
9d700 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75  be exited an equ
9d710 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  al number of tim
9d720 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65  es before anothe
9d730 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20  r thread.** can 
9d740 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73  enter.  If the s
9d750 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73  ame thread tries
9d760 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74   to enter any ot
9d770 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65  her kind of mute
9d780 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f  x.** more than o
9d790 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f  nce, the behavio
9d7a0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  r is undefined..
9d7b0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
9d7c0 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c  inMutexEnter(sql
9d7d0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
9d7e0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
9d7f0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
9d800 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75  CURSIVE || winMu
9d810 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
9d820 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63 61 6c  .  EnterCritical
9d830 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65  Section(&p->mute
9d840 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d  x);.  p->owner =
9d850 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61   GetCurrentThrea
9d860 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e 52 65  dId(); .  p->nRe
9d870 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  f++;.}.static in
9d880 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28 73 71  t winMutexTry(sq
9d890 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
9d8a0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
9d8b0 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73 65 72  TE_BUSY;.  asser
9d8c0 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
9d8d0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
9d8e0 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68   || winMutexNoth
9d8f0 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a 0a 20  eld(p) );.  /*. 
9d900 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f   ** The sqlite3_
9d910 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74  mutex_try() rout
9d920 69 6e 65 20 69 73 20 76 65 72 79 20 72 61 72 65  ine is very rare
9d930 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77 68 65  ly used, and whe
9d940 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75 73 65  n it.  ** is use
9d950 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79 20 61  d it is merely a
9d960 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  n optimization. 
9d970 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66 6f 72   So it is OK for
9d980 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a 20 20   it to always.  
9d990 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a 2a 0a  ** fail.  .  **.
9d9a0 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e 74 65    ** The TryEnte
9d9b0 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  rCriticalSection
9d9c0 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
9d9d0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f  only available o
9d9e0 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20 41 6e  n WinNT..  ** An
9d9f0 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73 20 63  d some windows c
9da00 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69  ompilers complai
9da10 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74 6f 20  n if you try to 
9da20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74 0a 20  use it without. 
9da30 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e 67 20   ** first doing 
9da40 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20 74 68  some #defines th
9da50 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c 69 74  at prevent SQLit
9da60 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e 67 20  e from building 
9da70 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a 20 46  on Win98..  ** F
9da80 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20  or that reason, 
9da90 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74 68 69  we will omit thi
9daa0 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66  s optimization f
9dab0 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20 20 2a  or now.  See.  *
9dac0 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35 2e 0a  * ticket #2685..
9dad0 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69 66 28    */.#if 0.  if(
9dae0 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26 26 20   mutexIsNT() && 
9daf0 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c  TryEnterCritical
9db00 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65  Section(&p->mute
9db10 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e  x) ){.    p->own
9db20 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54  er = GetCurrentT
9db30 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20 20 70  hreadId();.    p
9db40 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63  ->nRef++;.    rc
9db50 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
9db60 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  }.#endif.  retur
9db70 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
9db80 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
9db90 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65  _leave() routine
9dba0 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74   exits a mutex t
9dbb0 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69  hat was.** previ
9dbc0 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79  ously entered by
9dbd0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
9dbe0 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a  .  The behavior.
9dbf0 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20  ** is undefined 
9dc00 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20  if the mutex is 
9dc10 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e  not currently en
9dc20 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e  tered or.** is n
9dc30 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c  ot currently all
9dc40 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20  ocated.  SQLite 
9dc50 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69  will never do ei
9dc60 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ther..*/.static 
9dc70 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61  void winMutexLea
9dc80 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
9dc90 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
9dca0 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61  p->nRef>0 );.  a
9dcb0 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d  ssert( p->owner=
9dcc0 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61  =GetCurrentThrea
9dcd0 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e 52  dId() );.  p->nR
9dce0 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20  ef--;.  assert( 
9dcf0 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d  p->nRef==0 || p-
9dd00 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
9dd10 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20  X_RECURSIVE );. 
9dd20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 65   LeaveCriticalSe
9dd30 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29  ction(&p->mutex)
9dd40 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
9dd50 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ATE sqlite3_mute
9dd60 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74  x_methods *sqlit
9dd70 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76  e3DefaultMutex(v
9dd80 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73  oid){.  static s
9dd90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
9dda0 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a  hods sMutex = {.
9ddb0 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 74      winMutexInit
9ddc0 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e  ,.    winMutexEn
9ddd0 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 41  d,.    winMutexA
9dde0 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74  lloc,.    winMut
9ddf0 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d  exFree,.    winM
9de00 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 77  utexEnter,.    w
9de10 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20  inMutexTry,.    
9de20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23  winMutexLeave,.#
9de30 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
9de40 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 48  UG.    winMutexH
9de50 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  eld,.    winMute
9de60 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20  xNotheld.#else. 
9de70 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64     0,.    0.#end
9de80 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72  if.  };..  retur
9de90 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e  n &sMutex;.}.#en
9dea0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55  dif /* SQLITE_MU
9deb0 54 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a  TEX_W32 */../***
9dec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
9ded0 6f 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a  of mutex_w32.c *
9dee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9def0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9df00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
9df10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
9df20 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20  n file malloc.c 
9df30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9df40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
9df60 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
9df70 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
9df80 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
9df90 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
9dfa0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
9dfb0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
9dfc0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
9dfd0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
9dfe0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
9dff0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
9e000 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
9e010 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
9e020 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
9e030 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
9e040 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
9e050 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
9e060 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
9e070 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
9e080 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
9e090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e0d0 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20  **.**.** Memory 
9e0e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74  allocation funct
9e0f0 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67  ions used throug
9e100 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a  hout sqlite..**.
9e110 2a 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63  ** $Id: malloc.c
9e120 2c 76 20 31 2e 35 33 20 32 30 30 38 2f 31 32 2f  ,v 1.53 2008/12/
9e130 31 36 20 31 37 3a 32 30 3a 33 38 20 73 68 61 6e  16 17:20:38 shan
9e140 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  e Exp $.*/../*.*
9e150 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
9e160 75 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 65 6d  uns when the mem
9e170 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65  ory allocator se
9e180 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74  es that the.** t
9e190 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  otal memory allo
9e1a0 63 61 74 69 6f 6e 20 69 73 20 61 62 6f 75 74 20  cation is about 
9e1b0 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 73 6f  to exceed the so
9e1c0 66 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74  ft heap.** limit
9e1d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
9e1e0 20 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e   softHeapLimitEn
9e1f0 66 6f 72 63 65 72 28 0a 20 20 76 6f 69 64 20 2a  forcer(.  void *
9e200 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69  NotUsed, .  sqli
9e210 74 65 33 5f 69 6e 74 36 34 20 4e 6f 74 55 73 65  te3_int64 NotUse
9e220 64 32 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 53  d2,.  int allocS
9e230 69 7a 65 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f  ize.){.  UNUSED_
9e240 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
9e250 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
9e260 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
9e270 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 7a  _memory(allocSiz
9e280 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  e);.}../*.** Set
9e290 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 2d 73   the soft heap-s
9e2a0 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 74 68  ize limit for th
9e2b0 65 20 6c 69 62 72 61 72 79 2e 20 50 61 73 73 69  e library. Passi
9e2c0 6e 67 20 61 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a  ng a zero or .**
9e2d0 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20   negative value 
9e2e0 69 6e 64 69 63 61 74 65 73 20 6e 6f 20 6c 69 6d  indicates no lim
9e2f0 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  it..*/.SQLITE_AP
9e300 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73  I void sqlite3_s
9e310 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69  oft_heap_limit(i
9e320 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33  nt n){.  sqlite3
9e330 5f 75 69 6e 74 36 34 20 69 4c 69 6d 69 74 3b 0a  _uint64 iLimit;.
9e340 20 20 69 6e 74 20 6f 76 65 72 61 67 65 3b 0a 20    int overage;. 
9e350 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20   if( n<0 ){.    
9e360 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 65  iLimit = 0;.  }e
9e370 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20  lse{.    iLimit 
9e380 3d 20 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = n;.  }.  sqlit
9e390 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b  e3_initialize();
9e3a0 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 30 20  .  if( iLimit>0 
9e3b0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ){.    sqlite3Me
9e3c0 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66 74 48 65  moryAlarm(softHe
9e3d0 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 2c  apLimitEnforcer,
9e3e0 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 7d   0, iLimit);.  }
9e3f0 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
9e400 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30 2c 20  3MemoryAlarm(0, 
9e410 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6f 76 65  0, 0);.  }.  ove
9e420 72 61 67 65 20 3d 20 28 69 6e 74 29 28 73 71 6c  rage = (int)(sql
9e430 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64  ite3_memory_used
9e440 28 29 20 2d 20 28 69 36 34 29 6e 29 3b 0a 20 20  () - (i64)n);.  
9e450 69 66 28 20 6f 76 65 72 61 67 65 3e 30 20 29 7b  if( overage>0 ){
9e460 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6c  .    sqlite3_rel
9e470 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f 76 65 72  ease_memory(over
9e480 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  age);.  }.}../*.
9e490 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65  ** Attempt to re
9e4a0 6c 65 61 73 65 20 75 70 20 74 6f 20 6e 20 62 79  lease up to n by
9e4b0 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e  tes of non-essen
9e4c0 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 75 72 72  tial memory curr
9e4d0 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64 20 62 79  ently.** held by
9e4e0 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 78 61 6d   SQLite. An exam
9e4f0 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e  ple of non-essen
9e500 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 73 20 6d  tial memory is m
9e510 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 0a 2a 2a  emory used to.**
9e520 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65 20   cache database 
9e530 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 6e  pages that are n
9e540 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  ot currently in 
9e550 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  use..*/.SQLITE_A
9e560 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72  PI int sqlite3_r
9e570 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e  elease_memory(in
9e580 74 20 6e 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  t n){.#ifdef SQL
9e590 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
9e5a0 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 69  Y_MANAGEMENT.  i
9e5b0 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a 23 69 66  nt nRet = 0;.#if
9e5c0 20 30 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71 6c   0.  nRet += sql
9e5d0 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 4d  ite3VdbeReleaseM
9e5e0 65 6d 6f 72 79 28 6e 29 3b 0a 23 65 6e 64 69 66  emory(n);.#endif
9e5f0 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71 6c 69 74  .  nRet += sqlit
9e600 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d  e3PcacheReleaseM
9e610 65 6d 6f 72 79 28 6e 2d 6e 52 65 74 29 3b 0a 20  emory(n-nRet);. 
9e620 20 72 65 74 75 72 6e 20 6e 52 65 74 3b 0a 23 65   return nRet;.#e
9e630 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  lse.  UNUSED_PAR
9e640 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20 72 65 74  AMETER(n);.  ret
9e650 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23  urn SQLITE_OK;.#
9e660 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  endif.}../*.** S
9e670 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tate information
9e680 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65 20 6d 65   local to the me
9e690 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
9e6a0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
9e6b0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
9e6c0 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61  struct Mem0Globa
9e6d0 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20  l {.  /* Number 
9e6e0 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 66 6f  of free pages fo
9e6f0 72 20 73 63 72 61 74 63 68 20 61 6e 64 20 70 61  r scratch and pa
9e700 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  ge-cache memory 
9e710 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72 61 74 63  */.  u32 nScratc
9e720 68 46 72 65 65 3b 0a 20 20 75 33 32 20 6e 50 61  hFree;.  u32 nPa
9e730 67 65 46 72 65 65 3b 0a 0a 20 20 73 71 6c 69 74  geFree;..  sqlit
9e740 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
9e750 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65           /* Mute
9e760 78 20 74 6f 20 73 65 72 69 61 6c 69 7a 65 20 61  x to serialize a
9e770 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f 2a 0a 20  ccess */..  /*. 
9e780 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20 63 61   ** The alarm ca
9e790 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73 20 61  llback and its a
9e7a0 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20 6d  rguments.  The m
9e7b0 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63 6b 20 77  em0.mutex lock w
9e7c0 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65 6c 64  ill.  ** be held
9e7d0 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c 6c 62   while the callb
9e7e0 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20  ack is running. 
9e7f0 20 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73   Recursive calls
9e800 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6d   into.  ** the m
9e810 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20  emory subsystem 
9e820 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62 75 74  are allowed, but
9e830 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b   no new callback
9e840 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 69  s will be.  ** i
9e850 73 73 75 65 64 2e 20 20 54 68 65 20 61 6c 61 72  ssued.  The alar
9e860 6d 42 75 73 79 20 76 61 72 69 61 62 6c 65 20 69  mBusy variable i
9e870 73 20 73 65 74 20 74 6f 20 70 72 65 76 65 6e 74  s set to prevent
9e880 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a 2a 20   recursive.  ** 
9e890 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a  callbacks..  */.
9e8a0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
9e8b0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a  alarmThreshold;.
9e8c0 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61    void (*alarmCa
9e8d0 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73  llback)(void*, s
9e8e0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
9e8f0 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d  );.  void *alarm
9e900 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c 61 72 6d  Arg;.  int alarm
9e910 42 75 73 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  Busy;..  /*.  **
9e920 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   Pointers to the
9e930 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 47   end of sqlite3G
9e940 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72  lobalConfig.pScr
9e950 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73 71  atch and.  ** sq
9e960 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9e970 67 2e 70 50 61 67 65 20 74 6f 20 61 20 62 6c 6f  g.pPage to a blo
9e980 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61  ck of memory tha
9e990 74 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20 77  t records.  ** w
9e9a0 68 69 63 68 20 70 61 67 65 73 20 61 72 65 20 61  hich pages are a
9e9b0 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20  vailable..  */. 
9e9c0 20 75 33 32 20 2a 61 53 63 72 61 74 63 68 46 72   u32 *aScratchFr
9e9d0 65 65 3b 0a 20 20 75 33 32 20 2a 61 50 61 67 65  ee;.  u32 *aPage
9e9e0 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 7b  Free;.} mem0 = {
9e9f0 20 36 32 35 36 30 39 35 35 2c 20 30 2c 20 30 2c   62560955, 0, 0,
9ea00 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
9ea10 30 20 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65  0 };..#define me
9ea20 6d 30 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74  m0 GLOBAL(struct
9ea30 20 4d 65 6d 30 47 6c 6f 62 61 6c 2c 20 6d 65 6d   Mem0Global, mem
9ea40 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  0)../*.** Initia
9ea50 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  lize the memory 
9ea60 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
9ea70 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  stem..*/.SQLITE_
9ea80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
9ea90 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f  te3MallocInit(vo
9eaa0 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74  id){.  if( sqlit
9eab0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9eac0 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 7b 0a 20  .xMalloc==0 ){. 
9ead0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74     sqlite3MemSet
9eae0 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20  Default();.  }. 
9eaf0 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30   memset(&mem0, 0
9eb00 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b  , sizeof(mem0));
9eb10 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
9eb20 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
9eb30 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 6d 65 6d  Mutex ){.    mem
9eb40 30 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  0.mutex = sqlite
9eb50 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
9eb60 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
9eb70 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  MEM);.  }.  if( 
9eb80 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9eb90 66 69 67 2e 70 53 63 72 61 74 63 68 20 26 26 20  fig.pScratch && 
9eba0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9ebb0 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3e 3d 31  fig.szScratch>=1
9ebc0 30 30 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69  00.      && sqli
9ebd0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9ebe0 6e 53 63 72 61 74 63 68 3e 3d 30 20 29 7b 0a 20  nScratch>=0 ){. 
9ebf0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71     int i;.    sq
9ec00 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9ec10 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 28 73  g.szScratch = (s
9ec20 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ec30 69 67 2e 73 7a 53 63 72 61 74 63 68 20 2d 20 34  ig.szScratch - 4
9ec40 29 20 26 20 7e 37 3b 0a 20 20 20 20 6d 65 6d 30  ) & ~7;.    mem0
9ec50 2e 61 53 63 72 61 74 63 68 46 72 65 65 20 3d 20  .aScratchFree = 
9ec60 28 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73  (u32*)&((char*)s
9ec70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ec80 69 67 2e 70 53 63 72 61 74 63 68 29 0a 20 20 20  ig.pScratch).   
9ec90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5b                 [
9eca0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9ecb0 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2a 73 71  fig.szScratch*sq
9ecc0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9ecd0 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a 20 20 20  g.nScratch];.   
9ece0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69   for(i=0; i<sqli
9ecf0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9ed00 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b 29 7b 20  nScratch; i++){ 
9ed10 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65  mem0.aScratchFre
9ed20 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20  e[i] = i; }.    
9ed30 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65  mem0.nScratchFre
9ed40 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  e = sqlite3Globa
9ed50 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68  lConfig.nScratch
9ed60 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
9ed70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ed80 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 30 3b  ig.pScratch = 0;
9ed90 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
9eda0 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74  alConfig.szScrat
9edb0 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  ch = 0;.  }.  if
9edc0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9edd0 6f 6e 66 69 67 2e 70 50 61 67 65 20 26 26 20 73  onfig.pPage && s
9ede0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9edf0 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31 32 0a 20  ig.szPage>=512. 
9ee00 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 47       && sqlite3G
9ee10 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67  lobalConfig.nPag
9ee20 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69 6e 74 20  e>=1 ){.    int 
9ee30 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76 65 72 68  i;.    int overh
9ee40 65 61 64 3b 0a 20 20 20 20 69 6e 74 20 73 7a 20  ead;.    int sz 
9ee50 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
9ee60 6f 6e 66 69 67 2e 73 7a 50 61 67 65 20 26 20 7e  onfig.szPage & ~
9ee70 37 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73  7;.    int n = s
9ee80 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ee90 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76  ig.nPage;.    ov
9eea0 65 72 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20  erhead = (4*n + 
9eeb0 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20  sz - 1)/sz;.    
9eec0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9eed0 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65  fig.nPage -= ove
9eee0 72 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e  rhead;.    mem0.
9eef0 61 50 61 67 65 46 72 65 65 20 3d 20 28 75 33 32  aPageFree = (u32
9ef00 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74  *)&((char*)sqlit
9ef10 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9ef20 50 61 67 65 29 0a 20 20 20 20 20 20 20 20 20 20  Page).          
9ef30 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
9ef40 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
9ef50 61 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61  age*sqlite3Globa
9ef60 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a  lConfig.nPage];.
9ef70 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
9ef80 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ef90 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20  ig.nPage; i++){ 
9efa0 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69  mem0.aPageFree[i
9efb0 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d  ] = i; }.    mem
9efc0 30 2e 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71  0.nPageFree = sq
9efd0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9efe0 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65  g.nPage;.  }else
9eff0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
9f000 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20  balConfig.pPage 
9f010 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
9f020 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
9f030 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  age = 0;.  }.  r
9f040 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
9f050 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69  balConfig.m.xIni
9f060 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  t(sqlite3GlobalC
9f070 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61  onfig.m.pAppData
9f080 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e  );.}../*.** Dein
9f090 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
9f0a0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
9f0b0 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c  ubsystem..*/.SQL
9f0c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9f0d0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e   sqlite3MallocEn
9f0e0 64 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74  d(void){.  sqlit
9f0f0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9f100 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74  .xShutdown(sqlit
9f110 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9f120 2e 70 41 70 70 44 61 74 61 29 3b 0a 20 20 6d 65  .pAppData);.  me
9f130 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73  mset(&mem0, 0, s
9f140 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a  izeof(mem0));.}.
9f150 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
9f160 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  e amount of memo
9f170 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65  ry currently che
9f180 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c  cked out..*/.SQL
9f190 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
9f1a0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65  int64 sqlite3_me
9f1b0 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b  mory_used(void){
9f1c0 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20  .  int n, mx;.  
9f1d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65  sqlite3_int64 re
9f1e0 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61  s;.  sqlite3_sta
9f1f0 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55  tus(SQLITE_STATU
9f200 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26  S_MEMORY_USED, &
9f210 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65  n, &mx, 0);.  re
9f220 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  s = (sqlite3_int
9f230 36 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61  64)n;  /* Work a
9f240 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72  round bug in Bor
9f250 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23  land C. Ticket #
9f260 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e  3216 */.  return
9f270 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   res;.}../*.** R
9f280 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75  eturn the maximu
9f290 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  m amount of memo
9f2a0 72 79 20 74 68 61 74 20 68 61 73 20 65 76 65 72  ry that has ever
9f2b0 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64   been.** checked
9f2c0 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 65   out since eithe
9f2d0 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  r the beginning 
9f2e0 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a  of this process.
9f2f0 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20  ** or since the 
9f300 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 65  most recent rese
9f310 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
9f320 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
9f330 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69  qlite3_memory_hi
9f340 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65  ghwater(int rese
9f350 74 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c  tFlag){.  int n,
9f360 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69   mx;.  sqlite3_i
9f370 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69  nt64 res;.  sqli
9f380 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54  te3_status(SQLIT
9f390 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
9f3a0 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72  USED, &n, &mx, r
9f3b0 65 73 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73  esetFlag);.  res
9f3c0 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
9f3d0 34 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61  4)mx;  /* Work a
9f3e0 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72  round bug in Bor
9f3f0 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23  land C. Ticket #
9f400 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e  3216 */.  return
9f410 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   res;.}../*.** C
9f420 68 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20  hange the alarm 
9f430 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49  callback.*/.SQLI
9f440 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
9f450 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72  qlite3MemoryAlar
9f460 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c  m(.  void(*xCall
9f470 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67  back)(void *pArg
9f480 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
9f490 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76  used,int N),.  v
9f4a0 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c  oid *pArg,.  sql
9f4b0 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65  ite3_int64 iThre
9f4c0 73 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74  shold.){.  sqlit
9f4d0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
9f4e0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65  em0.mutex);.  me
9f4f0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  m0.alarmCallback
9f500 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20   = xCallback;.  
9f510 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20  mem0.alarmArg = 
9f520 70 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61  pArg;.  mem0.ala
9f530 72 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54  rmThreshold = iT
9f540 68 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69  hreshold;.  sqli
9f550 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9f560 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72  mem0.mutex);.  r
9f570 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
9f580 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
9f590 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
9f5a0 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61  ED./*.** Depreca
9f5b0 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74  ted external int
9f5c0 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61  erface.  Interna
9f5d0 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f  l/core SQLite co
9f5e0 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c  de.** should cal
9f5f0 6c 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41  l sqlite3MemoryA
9f600 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  larm..*/.SQLITE_
9f610 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
9f620 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20  memory_alarm(.  
9f630 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29  void(*xCallback)
9f640 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c  (void *pArg, sql
9f650 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c  ite3_int64 used,
9f660 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a  int N),.  void *
9f670 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f  pArg,.  sqlite3_
9f680 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64  int64 iThreshold
9f690 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  .){.  return sql
9f6a0 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28  ite3MemoryAlarm(
9f6b0 78 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c  xCallback, pArg,
9f6c0 20 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a   iThreshold);.}.
9f6d0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72  #endif../*.** Tr
9f6e0 69 67 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20  igger the alarm 
9f6f0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
9f700 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61  sqlite3MallocAla
9f710 72 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  rm(int nByte){. 
9f720 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63   void (*xCallbac
9f730 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  k)(void*,sqlite3
9f740 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73  _int64,int);.  s
9f750 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77  qlite3_int64 now
9f760 55 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41  Used;.  void *pA
9f770 72 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61  rg;.  if( mem0.a
9f780 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20  larmCallback==0 
9f790 7c 7c 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73  || mem0.alarmBus
9f7a0 79 20 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d  y  ) return;.  m
9f7b0 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20  em0.alarmBusy = 
9f7c0 31 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d  1;.  xCallback =
9f7d0 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62   mem0.alarmCallb
9f7e0 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d  ack;.  nowUsed =
9f7f0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61   sqlite3StatusVa
9f800 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55  lue(SQLITE_STATU
9f810 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a  S_MEMORY_USED);.
9f820 20 20 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c    pArg = mem0.al
9f830 61 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  armArg;.  sqlite
9f840 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9f850 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61  m0.mutex);.  xCa
9f860 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77  llback(pArg, now
9f870 55 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20  Used, nByte);.  
9f880 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
9f890 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ter(mem0.mutex);
9f8a0 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73  .  mem0.alarmBus
9f8b0 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  y = 0;.}../*.** 
9f8c0 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  Do a memory allo
9f8d0 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74  cation with stat
9f8e0 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d  istics and alarm
9f8f0 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a  s.  Assume the.*
9f900 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64  * lock is alread
9f910 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69  y held..*/.stati
9f920 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68  c int mallocWith
9f930 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69  Alarm(int n, voi
9f940 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e  d **pp){.  int n
9f950 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b  Full;.  void *p;
9f960 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
9f970 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
9f980 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e  m0.mutex) );.  n
9f990 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c  Full = sqlite3Gl
9f9a0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f  obalConfig.m.xRo
9f9b0 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69  undup(n);.  sqli
9f9c0 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c  te3StatusSet(SQL
9f9d0 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f  ITE_STATUS_MALLO
9f9e0 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66  C_SIZE, n);.  if
9f9f0 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c  ( mem0.alarmCall
9fa00 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69  back!=0 ){.    i
9fa10 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74  nt nUsed = sqlit
9fa20 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51  e3StatusValue(SQ
9fa30 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
9fa40 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66  RY_USED);.    if
9fa50 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d  ( nUsed+nFull >=
9fa60 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73   mem0.alarmThres
9fa70 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 71  hold ){.      sq
9fa80 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d  lite3MallocAlarm
9fa90 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20  (nFull);.    }. 
9faa0 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33   }.  p = sqlite3
9fab0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9fac0 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20  Malloc(nFull);. 
9fad0 20 69 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 6d   if( p==0 && mem
9fae0 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20  0.alarmCallback 
9faf0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61  ){.    sqlite3Ma
9fb00 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29  llocAlarm(nFull)
9fb10 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  ;.    p = sqlite
9fb20 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9fb30 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a  xMalloc(nFull);.
9fb40 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20    }.  if( p ){. 
9fb50 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74     nFull = sqlit
9fb60 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b  e3MallocSize(p);
9fb70 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74  .    sqlite3Stat
9fb80 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
9fb90 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c  TUS_MEMORY_USED,
9fba0 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a   nFull);.  }.  *
9fbb0 70 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e  pp = p;.  return
9fbc0 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   nFull;.}../*.**
9fbd0 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   Allocate memory
9fbe0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
9fbf0 69 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f  is like sqlite3_
9fc00 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 20  malloc() except 
9fc10 74 68 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 6d  that it.** assum
9fc20 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75  es the memory su
9fc30 62 73 79 73 74 65 6d 20 68 61 73 20 61 6c 72 65  bsystem has alre
9fc40 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  ady been initial
9fc50 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ized..*/.SQLITE_
9fc60 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
9fc70 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 20  lite3Malloc(int 
9fc80 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  n){.  void *p;. 
9fc90 20 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20   if( n<=0 ){.   
9fca0 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20   p = 0;.  }else 
9fcb0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9fcc0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
9fcd0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
9fce0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
9fcf0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c  .mutex);.    mal
9fd00 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20  locWithAlarm(n, 
9fd10 26 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  &p);.    sqlite3
9fd20 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
9fd30 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73  0.mutex);.  }els
9fd40 65 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  e{.    p = sqlit
9fd50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9fd60 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d  .xMalloc(n);.  }
9fd70 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
9fd80 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
9fd90 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
9fda0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66   allocation is f
9fdb0 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 61 70  or use by the ap
9fdc0 70 6c 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69  plication..** Fi
9fdd0 72 73 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68  rst make sure th
9fde0 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74  e memory subsyst
9fdf0 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  em is initialize
9fe00 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a  d, then do the.*
9fe10 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f  * allocation..*/
9fe20 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
9fe30 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   *sqlite3_malloc
9fe40 28 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66  (int n){.#ifndef
9fe50 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
9fe60 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69  OINIT.  if( sqli
9fe70 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
9fe80 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e   ) return 0;.#en
9fe90 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  dif.  return sql
9fea0 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d  ite3Malloc(n);.}
9feb0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72  ../*.** Each thr
9fec0 65 61 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76  ead may only hav
9fed0 65 20 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74  e a single outst
9fee0 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f  anding allocatio
9fef0 6e 20 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74  n from.** xScrat
9ff00 63 68 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20  chMalloc().  We 
9ff10 76 65 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73  verify this cons
9ff20 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69  traint in the si
9ff30 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a  ngle-threaded.**
9ff40 20 63 61 73 65 20 62 79 20 73 65 74 74 69 6e 67   case by setting
9ff50 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74   scratchAllocOut
9ff60 20 74 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c   to 1 when an al
9ff70 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f  location.** is o
9ff80 75 74 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72  utstanding clear
9ff90 69 6e 67 20 69 74 20 77 68 65 6e 20 74 68 65 20  ing it when the 
9ffa0 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72  allocation is fr
9ffb0 65 65 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  eed..*/.#if SQLI
9ffc0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30  TE_THREADSAFE==0
9ffd0 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45   && !defined(NDE
9ffe0 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20  BUG).static int 
9fff0 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20  scratchAllocOut 
a0000 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  = 0;.#endif.../*
a0010 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d  .** Allocate mem
a0020 6f 72 79 20 74 68 61 74 20 69 73 20 74 6f 20 62  ory that is to b
a0030 65 20 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61  e used and relea
a0040 73 65 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a  sed right away..
a0050 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
a0060 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c  is similar to al
a0070 6c 6f 63 61 28 29 20 69 6e 20 74 68 61 74 20 69  loca() in that i
a0080 74 20 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65  t is not intende
a0090 64 0a 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69  d.** for situati
a00a0 6f 6e 73 20 77 68 65 72 65 20 74 68 65 20 6d 65  ons where the me
a00b0 6d 6f 72 79 20 6d 69 67 68 74 20 62 65 20 68 65  mory might be he
a00c0 6c 64 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54  ld long-term.  T
a00d0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69  his.** routine i
a00e0 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65  s intended to ge
a00f0 74 20 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20  t memory to old 
a0100 6c 61 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20  large transient 
a0110 64 61 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72  data.** structur
a0120 65 73 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f  es that would no
a0130 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f  t normally fit o
a0140 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61  n the stack of a
a0150 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72  n.** embedded pr
a0160 6f 63 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  ocessor..*/.SQLI
a0170 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a0180 2a 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d  *sqlite3ScratchM
a0190 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20  alloc(int n){.  
a01a0 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72  void *p;.  asser
a01b0 74 28 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53  t( n>0 );..#if S
a01c0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
a01d0 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28  ==0 && !defined(
a01e0 4e 44 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72  NDEBUG).  /* Ver
a01f0 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65  ify that no more
a0200 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63   than one scratc
a0210 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72  h allocation per
a0220 20 74 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20   thread.  ** is 
a0230 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f  outstanding at o
a0240 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20  ne time.  (This 
a0250 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20  is only checked 
a0260 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67  in the.  ** sing
a0270 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65  le-threaded case
a0280 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20   since checking 
a0290 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72  in the multi-thr
a02a0 65 61 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20  eaded case.  ** 
a02b0 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f  would be much mo
a02c0 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29  re complicated.)
a02d0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63   */.  assert( sc
a02e0 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30  ratchAllocOut==0
a02f0 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66   );.#endif..  if
a0300 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
a0310 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c  onfig.szScratch<
a0320 6e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63  n ){.    goto sc
a0330 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a  ratch_overflow;.
a0340 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73    }else{  .    s
a0350 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
a0360 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
a0370 20 20 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63      if( mem0.nSc
a0380 72 61 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a  ratchFree==0 ){.
a0390 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
a03a0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
a03b0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74  utex);.      got
a03c0 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c  o scratch_overfl
a03d0 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ow;.    }else{. 
a03e0 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
a03f0 20 20 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61    i = mem0.aScra
a0400 74 63 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e  tchFree[--mem0.n
a0410 53 63 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20  ScratchFree];.  
a0420 20 20 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33      i *= sqlite3
a0430 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
a0440 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71  cratch;.      sq
a0450 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
a0460 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
a0470 41 54 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20  ATCH_USED, 1);. 
a0480 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
a0490 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41  usSet(SQLITE_STA
a04a0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45  TUS_SCRATCH_SIZE
a04b0 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , n);.      sqli
a04c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
a04d0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
a04e0 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28     p = (void*)&(
a04f0 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c  (char*)sqlite3Gl
a0500 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61  obalConfig.pScra
a0510 74 63 68 29 5b 69 5d 3b 0a 20 20 20 20 20 20 61  tch)[i];.      a
a0520 73 73 65 72 74 28 20 20 28 28 28 75 38 2a 29 70  ssert(  (((u8*)p
a0530 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d   - (u8*)0) & 7)=
a0540 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  =0 );.    }.  }.
a0550 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
a0560 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66  DSAFE==0 && !def
a0570 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73  ined(NDEBUG).  s
a0580 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d  cratchAllocOut =
a0590 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20   p!=0;.#endif.. 
a05a0 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 61   return p;..scra
a05b0 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20  tch_overflow:.  
a05c0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
a05d0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
a05e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
a05f0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
a0600 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c  .mutex);.    sql
a0610 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51  ite3StatusSet(SQ
a0620 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
a0630 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20  TCH_SIZE, n);.  
a0640 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 68    n = mallocWith
a0650 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20  Alarm(n, &p);.  
a0660 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74 65    if( p ) sqlite
a0670 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
a0680 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
a0690 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20  _OVERFLOW, n);. 
a06a0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
a06b0 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
a06c0 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  x);.  }else{.   
a06d0 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62   p = sqlite3Glob
a06e0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c  alConfig.m.xMall
a06f0 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 53  oc(n);.  }.#if S
a0700 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
a0710 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28  ==0 && !defined(
a0720 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 63  NDEBUG).  scratc
a0730 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30  hAllocOut = p!=0
a0740 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  ;.#endif.  retur
a0750 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54  n p;    .}.SQLIT
a0760 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a0770 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65  qlite3ScratchFre
a0780 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66  e(void *p){.  if
a0790 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49  ( p ){..#if SQLI
a07a0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30  TE_THREADSAFE==0
a07b0 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45   && !defined(NDE
a07c0 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 69  BUG).    /* Veri
a07d0 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20  fy that no more 
a07e0 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68  than one scratch
a07f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20   allocation per 
a0800 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 73  thread.    ** is
a0810 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20   outstanding at 
a0820 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73  one time.  (This
a0830 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64   is only checked
a0840 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 73   in the.    ** s
a0850 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63  ingle-threaded c
a0860 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69  ase since checki
a0870 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d  ng in the multi-
a0880 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20 20  threaded case.  
a0890 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75    ** would be mu
a08a0 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61  ch more complica
a08b0 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73  ted.) */.    ass
a08c0 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f  ert( scratchAllo
a08d0 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 73  cOut==1 );.    s
a08e0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d  cratchAllocOut =
a08f0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20   0;.#endif..    
a0900 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
a0910 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
a0920 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c  ==0.           |
a0930 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61  | p<sqlite3Globa
a0940 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
a0950 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70  .           || p
a0960 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 53  >=(void*)mem0.aS
a0970 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a 20 20  cratchFree ){.  
a0980 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47      if( sqlite3G
a0990 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
a09a0 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 20 20  stat ){.        
a09b0 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69  int iSize = sqli
a09c0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29  te3MallocSize(p)
a09d0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
a09e0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
a09f0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
a0a00 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
a0a10 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
a0a20 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c  S_SCRATCH_OVERFL
a0a30 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20  OW, -iSize);.   
a0a40 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
a0a50 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
a0a60 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c  TUS_MEMORY_USED,
a0a70 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20   -iSize);.      
a0a80 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
a0a90 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29  onfig.m.xFree(p)
a0aa0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
a0ab0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
a0ac0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
a0ad0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a0ae0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
a0af0 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a  fig.m.xFree(p);.
a0b00 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
a0b10 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  e{.      int i;.
a0b20 20 20 20 20 20 20 69 20 3d 20 28 69 6e 74 29 28        i = (int)(
a0b30 28 75 38 2a 29 70 20 2d 20 28 75 38 2a 29 73 71  (u8*)p - (u8*)sq
a0b40 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
a0b50 67 2e 70 53 63 72 61 74 63 68 29 3b 0a 20 20 20  g.pScratch);.   
a0b60 20 20 20 69 20 2f 3d 20 73 71 6c 69 74 65 33 47     i /= sqlite3G
a0b70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63  lobalConfig.szSc
a0b80 72 61 74 63 68 3b 0a 20 20 20 20 20 20 61 73 73  ratch;.      ass
a0b90 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73  ert( i>=0 && i<s
a0ba0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
a0bb0 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20  ig.nScratch );. 
a0bc0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
a0bd0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
a0be0 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65  tex);.      asse
a0bf0 72 74 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63  rt( mem0.nScratc
a0c00 68 46 72 65 65 3c 28 75 33 32 29 73 71 6c 69 74  hFree<(u32)sqlit
a0c10 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
a0c20 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20  Scratch );.     
a0c30 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72   mem0.aScratchFr
a0c40 65 65 5b 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68  ee[mem0.nScratch
a0c50 46 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20  Free++] = i;.   
a0c60 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
a0c70 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
a0c80 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20  S_SCRATCH_USED, 
a0c90 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  -1);.      sqlit
a0ca0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
a0cb0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
a0cc0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  }.  }.}../*.** A
a0cd0 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74  llocate memory t
a0ce0 6f 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65  o be used by the
a0cf0 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 4d 61   page cache.  Ma
a0d00 6b 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a  ke use of the.**
a0d10 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 70   memory buffer p
a0d20 72 6f 76 69 64 65 64 20 62 79 20 53 51 4c 49 54  rovided by SQLIT
a0d30 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
a0d40 48 45 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  HE if there is o
a0d50 6e 65 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 6d  ne.** and that m
a0d60 65 6d 6f 72 79 20 69 73 20 6f 66 20 74 68 65 20  emory is of the 
a0d70 72 69 67 68 74 20 73 69 7a 65 20 61 6e 64 20 69  right size and i
a0d80 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79  s not completely
a0d90 0a 2a 2a 20 63 6f 6e 73 75 6d 65 64 2e 20 20 4f  .** consumed.  O
a0da0 74 68 65 72 77 69 73 65 2c 20 66 61 69 6c 6f 76  therwise, failov
a0db0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c  er to sqlite3Mal
a0dc0 6c 6f 63 28 29 2e 0a 2a 2f 0a 23 69 66 20 30 0a  loc()..*/.#if 0.
a0dd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
a0de0 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65  oid *sqlite3Page
a0df0 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20  Malloc(int n){. 
a0e00 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65   void *p;.  asse
a0e10 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73  rt( n>0 );.  ass
a0e20 65 72 74 28 20 28 6e 20 26 20 28 6e 2d 31 29 29  ert( (n & (n-1))
a0e30 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
a0e40 20 6e 3e 3d 35 31 32 20 26 26 20 6e 3c 3d 33 32   n>=512 && n<=32
a0e50 37 36 38 20 29 3b 0a 0a 20 20 69 66 28 20 73 71  768 );..  if( sq
a0e60 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
a0e70 67 2e 73 7a 50 61 67 65 3c 6e 20 29 7b 0a 20 20  g.szPage<n ){.  
a0e80 20 20 67 6f 74 6f 20 70 61 67 65 5f 6f 76 65 72    goto page_over
a0e90 66 6c 6f 77 3b 0a 20 20 7d 65 6c 73 65 7b 20 20  flow;.  }else{  
a0ea0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
a0eb0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
a0ec0 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 6d 65  tex);.    if( me
a0ed0 6d 30 2e 6e 50 61 67 65 46 72 65 65 3d 3d 30 20  m0.nPageFree==0 
a0ee0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
a0ef0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
a0f00 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  0.mutex);.      
a0f10 67 6f 74 6f 20 70 61 67 65 5f 6f 76 65 72 66 6c  goto page_overfl
a0f20 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ow;.    }else{. 
a0f30 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
a0f40 20 20 69 20 3d 20 6d 65 6d 30 2e 61 50 61 67 65    i = mem0.aPage
a0f50 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 50 61 67  Free[--mem0.nPag
a0f60 65 46 72 65 65 5d 3b 0a 20 20 20 20 20 20 73 71  eFree];.      sq
a0f70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
a0f80 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  e(mem0.mutex);. 
a0f90 20 20 20 20 20 69 20 2a 3d 20 73 71 6c 69 74 65       i *= sqlite
a0fa0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
a0fb0 50 61 67 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  Page;.      sqli
a0fc0 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c  te3StatusSet(SQL
a0fd0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
a0fe0 41 43 48 45 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20  ACHE_SIZE, n);. 
a0ff0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
a1000 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
a1010 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53  TUS_PAGECACHE_US
a1020 45 44 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 20  ED, 1);.      p 
a1030 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72  = (void*)&((char
a1040 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  *)sqlite3GlobalC
a1050 6f 6e 66 69 67 2e 70 50 61 67 65 29 5b 69 5d 3b  onfig.pPage)[i];
a1060 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
a1070 75 72 6e 20 70 3b 0a 0a 70 61 67 65 5f 6f 76 65  urn p;..page_ove
a1080 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20 73 71 6c  rflow:.  if( sql
a1090 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
a10a0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20  .bMemstat ){.   
a10b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
a10c0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
a10d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
a10e0 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54  tusSet(SQLITE_ST
a10f0 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53  ATUS_PAGECACHE_S
a1100 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 3d  IZE, n);.    n =
a1110 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d   mallocWithAlarm
a1120 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66 28  (n, &p);.    if(
a1130 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 74   p ) sqlite3Stat
a1140 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
a1150 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56  TUS_PAGECACHE_OV
a1160 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20  ERFLOW, n);.    
a1170 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
a1180 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ave(mem0.mutex);
a1190 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20  .  }else{.    p 
a11a0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
a11b0 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28  onfig.m.xMalloc(
a11c0 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  n);.  }.  return
a11d0 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45   p;    .}.SQLITE
a11e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a11f0 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76 6f  lite3PageFree(vo
a1200 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  id *p){.  if( p 
a1210 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
a1220 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
a1230 50 61 67 65 3d 3d 30 0a 20 20 20 20 20 20 20 20  Page==0.        
a1240 20 20 20 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47     || p<sqlite3G
a1250 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
a1260 65 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  e.           || 
a1270 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61  p>=(void*)mem0.a
a1280 50 61 67 65 46 72 65 65 20 29 7b 0a 20 20 20 20  PageFree ){.    
a1290 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73    /* In this cas
a12a0 65 2c 20 74 68 65 20 70 61 67 65 20 61 6c 6c 6f  e, the page allo
a12b0 63 61 74 69 6f 6e 20 77 61 73 20 6f 62 74 61 69  cation was obtai
a12c0 6e 65 64 20 66 72 6f 6d 20 61 20 72 65 67 75 6c  ned from a regul
a12d0 61 72 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c  ar .      ** cal
a12e0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 65 6d  l to sqlite3_mem
a12f0 5f 6d 65 74 68 6f 64 73 2e 78 4d 61 6c 6c 6f 63  _methods.xMalloc
a1300 28 29 20 28 61 20 70 61 67 65 2d 63 61 63 68 65  () (a page-cache
a1310 2d 6d 65 6d 6f 72 79 20 0a 20 20 20 20 20 20 2a  -memory .      *
a1320 2a 20 22 6f 76 65 72 66 6c 6f 77 22 29 2e 20 46  * "overflow"). F
a1330 72 65 65 20 74 68 65 20 62 6c 6f 63 6b 20 77 69  ree the block wi
a1340 74 68 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  th sqlite3_mem_m
a1350 65 74 68 6f 64 73 2e 78 46 72 65 65 28 29 2e 0a  ethods.xFree()..
a1360 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
a1370 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
a1380 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20  Config.bMemstat 
a1390 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
a13a0 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61  Size = sqlite3Ma
a13b0 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20  llocSize(p);.   
a13c0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
a13d0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
a13e0 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71  tex);.        sq
a13f0 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
a1400 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
a1410 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c  ECACHE_OVERFLOW,
a1420 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20   -iSize);.      
a1430 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41    sqlite3StatusA
a1440 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
a1450 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69  _MEMORY_USED, -i
a1460 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73  Size);.        s
a1470 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
a1480 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20  ig.m.xFree(p);. 
a1490 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d         sqlite3_m
a14a0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
a14b0 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 65  mutex);.      }e
a14c0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
a14d0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
a14e0 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20  .m.xFree(p);.   
a14f0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
a1500 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
a1510 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73  e allocation was
a1520 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 20   allocated from 
a1530 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  the sqlite3Globa
a1540 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 0a 20 20  lConfig.pPage.  
a1550 20 20 20 20 2a 2a 20 62 75 66 66 65 72 2e 20 49      ** buffer. I
a1560 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20  n this case all 
a1570 74 68 61 74 20 69 73 20 61 64 64 20 74 68 65 20  that is add the 
a1580 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 67  index of the pag
a1590 65 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68  e in.      ** th
a15a0 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e sqlite3GlobalC
a15b0 6f 6e 66 69 67 2e 70 50 61 67 65 20 61 72 72 61  onfig.pPage arra
a15c0 79 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66 20  y to the set of 
a15d0 66 72 65 65 20 69 6e 64 65 78 65 73 20 73 74 6f  free indexes sto
a15e0 72 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20  red.      ** in 
a15f0 74 68 65 20 6d 65 6d 30 2e 61 50 61 67 65 46 72  the mem0.aPageFr
a1600 65 65 5b 5d 20 61 72 72 61 79 2e 0a 20 20 20 20  ee[] array..    
a1610 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69    */.      int i
a1620 3b 0a 20 20 20 20 20 20 69 20 3d 20 28 75 38 20  ;.      i = (u8 
a1630 2a 29 70 20 2d 20 28 75 38 20 2a 29 73 71 6c 69  *)p - (u8 *)sqli
a1640 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
a1650 70 50 61 67 65 3b 0a 20 20 20 20 20 20 69 20 2f  pPage;.      i /
a1660 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
a1670 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3b 0a 20 20  onfig.szPage;.  
a1680 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30      assert( i>=0
a1690 20 26 26 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f   && i<sqlite3Glo
a16a0 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20  balConfig.nPage 
a16b0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
a16c0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
a16d0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  0.mutex);.      
a16e0 61 73 73 65 72 74 28 20 6d 65 6d 30 2e 6e 50 61  assert( mem0.nPa
a16f0 67 65 46 72 65 65 3c 73 71 6c 69 74 65 33 47 6c  geFree<sqlite3Gl
a1700 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65  obalConfig.nPage
a1710 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 61   );.      mem0.a
a1720 50 61 67 65 46 72 65 65 5b 6d 65 6d 30 2e 6e 50  PageFree[mem0.nP
a1730 61 67 65 46 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a  ageFree++] = i;.
a1740 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
a1750 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
a1760 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
a1770 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  SED, -1);.      
a1780 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
a1790 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ave(mem0.mutex);
a17a0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
a17b0 45 42 55 47 29 20 26 26 20 30 0a 20 20 20 20 20  EBUG) && 0.     
a17c0 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20   /* Assert that 
a17d0 61 20 64 75 70 6c 69 63 61 74 65 20 77 61 73 20  a duplicate was 
a17e0 6e 6f 74 20 6a 75 73 74 20 69 6e 73 65 72 74 65  not just inserte
a17f0 64 20 69 6e 74 6f 20 61 50 61 67 65 46 72 65 65  d into aPageFree
a1800 5b 5d 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72  []. */.      for
a1810 28 69 3d 30 3b 20 69 3c 6d 65 6d 30 2e 6e 50 61  (i=0; i<mem0.nPa
a1820 67 65 46 72 65 65 2d 31 3b 20 69 2b 2b 29 7b 0a  geFree-1; i++){.
a1830 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
a1840 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69  mem0.aPageFree[i
a1850 5d 21 3d 6d 65 6d 30 2e 61 50 61 67 65 46 72 65  ]!=mem0.aPageFre
a1860 65 5b 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65 65  e[mem0.nPageFree
a1870 2d 31 5d 20 29 3b 0a 20 20 20 20 20 20 7d 0a 23  -1] );.      }.#
a1880 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 0a  endif.    }.  }.
a1890 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
a18a0 54 52 55 45 20 69 66 20 70 20 69 73 20 61 20 6c  TRUE if p is a l
a18b0 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20  ookaside memory 
a18c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 20  allocation from 
a18d0 64 62 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  db.*/.#ifndef SQ
a18e0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53  LITE_OMIT_LOOKAS
a18f0 49 44 45 0a 73 74 61 74 69 63 20 69 6e 74 20 69  IDE.static int i
a1900 73 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69 74  sLookaside(sqlit
a1910 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29  e3 *db, void *p)
a1920 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 20 26 26  {.  return db &&
a1930 20 70 20 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f   p && p>=db->loo
a1940 6b 61 73 69 64 65 2e 70 53 74 61 72 74 20 26 26  kaside.pStart &&
a1950 20 70 3c 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   p<db->lookaside
a1960 2e 70 45 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23  .pEnd;.}.#else.#
a1970 64 65 66 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 69  define isLookasi
a1980 64 65 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 66  de(A,B) 0.#endif
a1990 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
a19a0 68 65 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d  he size of a mem
a19b0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70  ory allocation p
a19c0 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e  reviously obtain
a19d0 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74  ed from.** sqlit
a19e0 65 33 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71  e3Malloc() or sq
a19f0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a  lite3_malloc()..
a1a00 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
a1a10 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c  E int sqlite3Mal
a1a20 6c 6f 63 53 69 7a 65 28 76 6f 69 64 20 2a 70 29  locSize(void *p)
a1a30 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
a1a40 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
a1a50 2e 78 53 69 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c  .xSize(p);.}.SQL
a1a60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
a1a70 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53  sqlite3DbMallocS
a1a80 69 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ize(sqlite3 *db,
a1a90 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28   void *p){.  if(
a1aa0 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74   p==0 ){.    ret
a1ab0 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 0;.  }else i
a1ac0 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64  f( isLookaside(d
a1ad0 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74  b, p) ){.    ret
a1ae0 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  urn db->lookasid
a1af0 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  e.sz;.  }else{. 
a1b00 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
a1b10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
a1b20 78 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  xSize(p);.  }.}.
a1b30 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f  ./*.** Free memo
a1b40 72 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62  ry previously ob
a1b50 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
a1b60 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  te3Malloc()..*/.
a1b70 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
a1b80 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69  sqlite3_free(voi
a1b90 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d  d *p){.  if( p==
a1ba0 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
a1bb0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
a1bc0 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
a1bd0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
a1be0 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
a1bf0 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  utex);.    sqlit
a1c00 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49  e3StatusAdd(SQLI
a1c10 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
a1c20 5f 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33 4d  _USED, -sqlite3M
a1c30 61 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a 20  allocSize(p));. 
a1c40 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
a1c50 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70  Config.m.xFree(p
a1c60 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
a1c70 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
a1c80 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b  mutex);.  }else{
a1c90 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
a1ca0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65  alConfig.m.xFree
a1cb0 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
a1cc0 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74 68  * Free memory th
a1cd0 61 74 20 6d 69 67 68 74 20 62 65 20 61 73 73 6f  at might be asso
a1ce0 63 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61  ciated with a pa
a1cf0 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
a1d00 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  e.** connection.
a1d10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a1d20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
a1d30 62 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64  bFree(sqlite3 *d
a1d40 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69  b, void *p){.  i
a1d50 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64  f( isLookaside(d
a1d60 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f 6f  b, p) ){.    Loo
a1d70 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66  kasideSlot *pBuf
a1d80 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f   = (LookasideSlo
a1d90 74 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d 3e  t*)p;.    pBuf->
a1da0 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b  pNext = db->look
a1db0 61 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20  aside.pFree;.   
a1dc0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
a1dd0 46 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20 20  Free = pBuf;.   
a1de0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
a1df0 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Out--;.  }else{.
a1e00 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
a1e10 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
a1e20 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a  * Change the siz
a1e30 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67  e of an existing
a1e40 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
a1e50 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  on.*/.SQLITE_PRI
a1e60 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
a1e70 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a  e3Realloc(void *
a1e80 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 73  pOld, int nBytes
a1e90 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 6e  ){.  int nOld, n
a1ea0 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65  New;.  void *pNe
a1eb0 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30  w;.  if( pOld==0
a1ec0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
a1ed0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79  qlite3Malloc(nBy
a1ee0 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  tes);.  }.  if( 
a1ef0 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20  nBytes<=0 ){.   
a1f00 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f   sqlite3_free(pO
a1f10 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ld);.    return 
a1f20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20  0;.  }.  nOld = 
a1f30 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
a1f40 65 28 70 4f 6c 64 29 3b 0a 20 20 69 66 28 20 73  e(pOld);.  if( s
a1f50 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
a1f60 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20  ig.bMemstat ){. 
a1f70 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
a1f80 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65  _enter(mem0.mute
a1f90 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  x);.    sqlite3S
a1fa0 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f  tatusSet(SQLITE_
a1fb0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49  STATUS_MALLOC_SI
a1fc0 5a 45 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20  ZE, nBytes);.   
a1fd0 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47   nNew = sqlite3G
a1fe0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52  lobalConfig.m.xR
a1ff0 6f 75 6e 64 75 70 28 6e 42 79 74 65 73 29 3b 0a  oundup(nBytes);.
a2000 20 20 20 20 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e      if( nOld==nN
a2010 65 77 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77  ew ){.      pNew
a2020 20 3d 20 70 4f 6c 64 3b 0a 20 20 20 20 7d 65 6c   = pOld;.    }el
a2030 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  se{.      if( sq
a2040 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65  lite3StatusValue
a2050 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
a2060 45 4d 4f 52 59 5f 55 53 45 44 29 2b 6e 4e 65 77  EMORY_USED)+nNew
a2070 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20 20 20 20  -nOld >= .      
a2080 20 20 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d        mem0.alarm
a2090 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20  Threshold ){.   
a20a0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c       sqlite3Mall
a20b0 6f 63 41 6c 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c  ocAlarm(nNew-nOl
a20c0 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  d);.      }.    
a20d0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
a20e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
a20f0 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e  Realloc(pOld, nN
a2100 65 77 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ew);.      if( p
a2110 4e 65 77 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61  New==0 && mem0.a
a2120 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a  larmCallback ){.
a2130 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d          sqlite3M
a2140 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 42 79 74 65  allocAlarm(nByte
a2150 73 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  s);.        pNew
a2160 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
a2170 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f  Config.m.xReallo
a2180 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20  c(pOld, nNew);. 
a2190 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
a21a0 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20   pNew ){.       
a21b0 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d   nNew = sqlite3M
a21c0 61 6c 6c 6f 63 53 69 7a 65 28 70 4e 65 77 29 3b  allocSize(pNew);
a21d0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
a21e0 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45  StatusAdd(SQLITE
a21f0 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
a2200 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29 3b  SED, nNew-nOld);
a2210 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
a2220 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
a2230 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
a2240 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  x);.  }else{.   
a2250 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47   pNew = sqlite3G
a2260 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52  lobalConfig.m.xR
a2270 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 42 79  ealloc(pOld, nBy
a2280 74 65 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tes);.  }.  retu
a2290 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
a22a0 2a 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e 74  * The public int
a22b0 65 72 66 61 63 65 20 74 6f 20 73 71 6c 69 74 65  erface to sqlite
a22c0 33 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 6b 65 20  3Realloc.  Make 
a22d0 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 65  sure that the me
a22e0 6d 6f 72 79 0a 2a 2a 20 73 75 62 73 79 73 74 65  mory.** subsyste
a22f0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  m is initialized
a2300 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69   prior to invoki
a2310 6e 67 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63  ng sqliteRealloc
a2320 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
a2330 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65  void *sqlite3_re
a2340 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64  alloc(void *pOld
a2350 2c 20 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65  , int n){.#ifnde
a2360 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
a2370 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c  TOINIT.  if( sql
a2380 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
a2390 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  ) ) return 0;.#e
a23a0 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71  ndif.  return sq
a23b0 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 70 4f 6c  lite3Realloc(pOl
a23c0 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  d, n);.}.../*.**
a23d0 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65   Allocate and ze
a23e0 72 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 20 0a 53  ro memory..*/ .S
a23f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
a2400 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  id *sqlite3Mallo
a2410 63 5a 65 72 6f 28 69 6e 74 20 6e 29 7b 0a 20 20  cZero(int n){.  
a2420 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65  void *p = sqlite
a2430 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66  3Malloc(n);.  if
a2440 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
a2450 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a  t(p, 0, n);.  }.
a2460 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
a2470 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e  *.** Allocate an
a2480 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20  d zero memory.  
a2490 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  If the allocatio
a24a0 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a  n fails, make.**
a24b0 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65   the mallocFaile
a24c0 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f  d flag in the co
a24d0 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  nnection pointer
a24e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a24f0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
a2500 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71  3DbMallocZero(sq
a2510 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e  lite3 *db, int n
a2520 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73  ){.  void *p = s
a2530 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
a2540 77 28 64 62 2c 20 6e 29 3b 0a 20 20 69 66 28 20  w(db, n);.  if( 
a2550 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  p ){.    memset(
a2560 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20  p, 0, n);.  }.  
a2570 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
a2580 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20  ** Allocate and 
a2590 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66  zero memory.  If
a25a0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
a25b0 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74  fails, make.** t
a25c0 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
a25d0 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e  flag in the conn
a25e0 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a  ection pointer..
a25f0 2a 2a 0a 2a 2a 20 49 66 20 64 62 21 3d 30 20 61  **.** If db!=0 a
a2600 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  nd db->mallocFai
a2610 6c 65 64 20 69 73 20 74 72 75 65 20 28 69 6e 64  led is true (ind
a2620 69 63 61 74 69 6e 67 20 61 20 70 72 69 6f 72 20  icating a prior 
a2630 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69 6c 75 72  malloc.** failur
a2640 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61  e on the same da
a2650 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
a2660 6e 29 20 74 68 65 6e 20 61 6c 77 61 79 73 20 72  n) then always r
a2670 65 74 75 72 6e 20 30 2e 0a 2a 2a 20 48 65 6e 63  eturn 0..** Henc
a2680 65 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c  e for a particul
a2690 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ar database conn
a26a0 65 63 74 69 6f 6e 2c 20 6f 6e 63 65 20 6d 61 6c  ection, once mal
a26b0 6c 6f 63 20 73 74 61 72 74 73 0a 2a 2a 20 66 61  loc starts.** fa
a26c0 69 6c 69 6e 67 2c 20 69 74 20 66 61 69 6c 73 20  iling, it fails 
a26d0 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 75 6e 74  consistently unt
a26e0 69 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  il mallocFailed 
a26f0 69 73 20 72 65 73 65 74 2e 0a 2a 2a 20 54 68 69  is reset..** Thi
a2700 73 20 69 73 20 61 6e 20 69 6d 70 6f 72 74 61 6e  s is an importan
a2710 74 20 61 73 73 75 6d 70 74 69 6f 6e 2e 20 20 54  t assumption.  T
a2720 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 70 6c  here are many pl
a2730 61 63 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 63  aces in the.** c
a2740 6f 64 65 20 74 68 61 74 20 64 6f 20 74 68 69 6e  ode that do thin
a2750 67 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  gs like this:.**
a2760 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20  .**         int 
a2770 2a 61 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74  *a = (int*)sqlit
a2780 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
a2790 2c 20 31 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20  , 100);.**      
a27a0 20 20 20 69 6e 74 20 2a 62 20 3d 20 28 69 6e 74     int *b = (int
a27b0 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
a27c0 63 52 61 77 28 64 62 2c 20 32 30 30 29 3b 0a 2a  cRaw(db, 200);.*
a27d0 2a 20 20 20 20 20 20 20 20 20 69 66 28 20 62 20  *         if( b 
a27e0 29 20 61 5b 31 30 5d 20 3d 20 39 3b 0a 2a 2a 0a  ) a[10] = 9;.**.
a27f0 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** In other word
a2800 73 2c 20 69 66 20 61 20 73 75 62 73 65 71 75 65  s, if a subseque
a2810 6e 74 20 6d 61 6c 6c 6f 63 20 28 65 78 3a 20 22  nt malloc (ex: "
a2820 62 22 29 20 77 6f 72 6b 65 64 2c 20 69 74 20 69  b") worked, it i
a2830 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 61  s assumed.** tha
a2840 74 20 61 6c 6c 20 70 72 69 6f 72 20 6d 61 6c 6c  t all prior mall
a2850 6f 63 73 20 28 65 78 3a 20 22 61 22 29 20 77 6f  ocs (ex: "a") wo
a2860 72 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a 53 51 4c  rked too..*/.SQL
a2870 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a2880 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f   *sqlite3DbMallo
a2890 63 52 61 77 28 73 71 6c 69 74 65 33 20 2a 64 62  cRaw(sqlite3 *db
a28a0 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  , int n){.  void
a28b0 20 2a 70 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   *p;.#ifndef SQL
a28c0 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49  ITE_OMIT_LOOKASI
a28d0 44 45 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20  DE.  if( db ){. 
a28e0 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74     LookasideSlot
a28f0 20 2a 70 42 75 66 3b 0a 20 20 20 20 69 66 28 20   *pBuf;.    if( 
a2900 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
a2910 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
a2920 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
a2930 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
a2940 62 45 6e 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64  bEnabled && n<=d
a2950 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a  b->lookaside.sz.
a2960 20 20 20 20 20 20 20 20 20 26 26 20 28 70 42 75           && (pBu
a2970 66 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  f = db->lookasid
a2980 65 2e 70 46 72 65 65 29 21 3d 30 20 29 7b 0a 20  e.pFree)!=0 ){. 
a2990 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69       db->lookasi
a29a0 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 2d  de.pFree = pBuf-
a29b0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 64 62  >pNext;.      db
a29c0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74  ->lookaside.nOut
a29d0 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62  ++;.      if( db
a29e0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74  ->lookaside.nOut
a29f0 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d  >db->lookaside.m
a2a00 78 4f 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20  xOut ){.        
a2a10 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78  db->lookaside.mx
a2a20 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  Out = db->lookas
a2a30 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20  ide.nOut;.      
a2a40 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 28  }.      return (
a2a50 76 6f 69 64 2a 29 70 42 75 66 3b 0a 20 20 20 20  void*)pBuf;.    
a2a60 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66  }.  }.#else.  if
a2a70 28 20 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c  ( db && db->mall
a2a80 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
a2a90 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65  return 0;.  }.#e
a2aa0 6e 64 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74  ndif.  p = sqlit
a2ab0 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69  e3Malloc(n);.  i
a2ac0 66 28 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20  f( !p && db ){. 
a2ad0 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
a2ae0 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  led = 1;.  }.  r
a2af0 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
a2b00 2a 20 52 65 73 69 7a 65 20 74 68 65 20 62 6c 6f  * Resize the blo
a2b10 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69  ck of memory poi
a2b20 6e 74 65 64 20 74 6f 20 62 79 20 70 20 74 6f 20  nted to by p to 
a2b30 6e 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 0a  n bytes. If the.
a2b40 2a 2a 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c  ** resize fails,
a2b50 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46   set the mallocF
a2b60 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68  ailed flag in th
a2b70 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a  e connection obj
a2b80 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ect..*/.SQLITE_P
a2b90 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
a2ba0 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71  ite3DbRealloc(sq
a2bb0 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
a2bc0 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f  *p, int n){.  vo
a2bd0 69 64 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20  id *pNew = 0;.  
a2be0 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
a2bf0 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 69  iled==0 ){.    i
a2c00 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( p==0 ){.     
a2c10 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 44   return sqlite3D
a2c20 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e  bMallocRaw(db, n
a2c30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
a2c40 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c   isLookaside(db,
a2c50 20 70 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28   p) ){.      if(
a2c60 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64   n<=db->lookasid
a2c70 65 2e 73 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  e.sz ){.        
a2c80 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 20  return p;.      
a2c90 7d 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73  }.      pNew = s
a2ca0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
a2cb0 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 20 20  w(db, n);.      
a2cc0 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20  if( pNew ){.    
a2cd0 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c      memcpy(pNew,
a2ce0 20 70 2c 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64   p, db->lookasid
a2cf0 65 2e 73 7a 29 3b 0a 20 20 20 20 20 20 20 20 73  e.sz);.        s
a2d00 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
a2d10 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   p);.      }.   
a2d20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4e   }else{.      pN
a2d30 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
a2d40 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20 20 20 20  lloc(p, n);.    
a2d50 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20    if( !pNew ){. 
a2d60 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
a2d70 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
a2d80 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
a2d90 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
a2da0 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
a2db0 6f 20 72 65 61 6c 6c 6f 63 61 74 65 20 70 2e 20  o reallocate p. 
a2dc0 20 49 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 61   If the realloca
a2dd0 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 6e  tion fails, then
a2de0 20 66 72 65 65 20 70 0a 2a 2a 20 61 6e 64 20 73   free p.** and s
a2df0 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69  et the mallocFai
a2e00 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20  led flag in the 
a2e10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
a2e20 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
a2e30 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
a2e40 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
a2e50 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ree(sqlite3 *db,
a2e60 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29   void *p, int n)
a2e70 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a  {.  void *pNew;.
a2e80 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
a2e90 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 2c  DbRealloc(db, p,
a2ea0 20 6e 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77   n);.  if( !pNew
a2eb0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
a2ec0 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20  bFree(db, p);.  
a2ed0 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
a2ee0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61  .}../*.** Make a
a2ef0 20 63 6f 70 79 20 6f 66 20 61 20 73 74 72 69 6e   copy of a strin
a2f00 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  g in memory obta
a2f10 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
a2f20 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 73 65 20  Malloc(). These 
a2f30 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 63 61  .** functions ca
a2f40 6c 6c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  ll sqlite3Malloc
a2f50 52 61 77 28 29 20 64 69 72 65 63 74 6c 79 20 69  Raw() directly i
a2f60 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69 74 65  nstead of sqlite
a2f70 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 69 73 0a 2a  Malloc(). This.*
a2f80 2a 20 69 73 20 62 65 63 61 75 73 65 20 77 68 65  * is because whe
a2f90 6e 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 67 69  n memory debuggi
a2fa0 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 6e 2c  ng is turned on,
a2fb0 20 74 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74   these two funct
a2fc0 69 6f 6e 73 20 61 72 65 20 0a 2a 2a 20 63 61 6c  ions are .** cal
a2fd0 6c 65 64 20 76 69 61 20 6d 61 63 72 6f 73 20 74  led via macros t
a2fe0 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 20 63  hat record the c
a2ff0 75 72 72 65 6e 74 20 66 69 6c 65 20 61 6e 64 20  urrent file and 
a3000 6c 69 6e 65 20 6e 75 6d 62 65 72 20 69 6e 20 74  line number in t
a3010 68 65 0a 2a 2a 20 54 68 72 65 61 64 44 61 74 61  he.** ThreadData
a3020 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53   structure..*/.S
a3030 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
a3040 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72  ar *sqlite3DbStr
a3050 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Dup(sqlite3 *db,
a3060 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b   const char *z){
a3070 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20  .  char *zNew;. 
a3080 20 73 69 7a 65 5f 74 20 6e 3b 0a 20 20 69 66 28   size_t n;.  if(
a3090 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74   z==0 ){.    ret
a30a0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d  urn 0;.  }.  n =
a30b0 20 28 64 62 20 3f 20 73 71 6c 69 74 65 33 53 74   (db ? sqlite3St
a30c0 72 6c 65 6e 28 64 62 2c 20 7a 29 20 3a 20 73 71  rlen(db, z) : sq
a30d0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
a30e0 29 2b 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28  )+1;.  assert( (
a30f0 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e  n&0x7fffffff)==n
a3100 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c   );.  zNew = sql
a3110 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
a3120 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69  db, (int)n);.  i
a3130 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d  f( zNew ){.    m
a3140 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e  emcpy(zNew, z, n
a3150 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
a3160 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  zNew;.}.SQLITE_P
a3170 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
a3180 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 73 71  ite3DbStrNDup(sq
a3190 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
a31a0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
a31b0 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a  {.  char *zNew;.
a31c0 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20    if( z==0 ){.  
a31d0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
a31e0 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37    assert( (n&0x7
a31f0 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20  fffffff)==n );. 
a3200 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   zNew = sqlite3D
a3210 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e  bMallocRaw(db, n
a3220 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20  +1);.  if( zNew 
a3230 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e  ){.    memcpy(zN
a3240 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a  ew, z, n);.    z
a3250 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a  New[n] = 0;.  }.
a3260 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d    return zNew;.}
a3270 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
a3280 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65   string from the
a3290 20 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e   zFromat argumen
a32a0 74 20 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73  t and the va_lis
a32b0 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a  t that follows..
a32c0 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 73 74 72  ** Store the str
a32d0 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62  ing in memory ob
a32e0 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
a32f0 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d  teMalloc() and m
a3300 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74  ake *pz.** point
a3310 20 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e   to that string.
a3320 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a3330 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
a3340 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a  etString(char **
a3350 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  pz, sqlite3 *db,
a3360 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
a3370 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
a3380 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
a3390 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74   *z;..  va_start
a33a0 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
a33b0 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72   z = sqlite3VMPr
a33c0 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74  intf(db, zFormat
a33d0 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
a33e0 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  ap);.  sqlite3Db
a33f0 46 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20  Free(db, *pz);. 
a3400 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a   *pz = z;.}.../*
a3410 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
a3420 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  n must be called
a3430 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
a3440 61 6e 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  any API function
a3450 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72   (i.e. .** retur
a3460 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20  ning control to 
a3470 74 68 65 20 75 73 65 72 29 20 74 68 61 74 20 68  the user) that h
a3480 61 73 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65  as called sqlite
a3490 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73  3_malloc or.** s
a34a0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a  qlite3_realloc..
a34b0 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
a34c0 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d  ed value is norm
a34d0 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74  ally a copy of t
a34e0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
a34f0 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75  nt to this.** fu
a3500 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c  nction. However,
a3510 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   if a malloc() f
a3520 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72  ailure has occur
a3530 65 64 20 73 69 6e 63 65 20 74 68 65 20 70 72 65  ed since the pre
a3540 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74  vious.** invocat
a3550 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  ion SQLITE_NOMEM
a3560 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73   is returned ins
a3570 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  tead. .**.** If 
a3580 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
a3590 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e  nt, db, is not N
a35a0 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63  ULL and a malloc
a35b0 28 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  () error has occ
a35c0 75 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  ured,.** then th
a35d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72  e connection err
a35e0 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c  or-code (the val
a35f0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
a3600 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
a3610 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53  ).** is set to S
a3620 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a  QLITE_NOMEM..*/.
a3630 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a3640 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  nt sqlite3ApiExi
a3650 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69  t(sqlite3* db, i
a3660 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20  nt rc){.  /* If 
a3670 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73  the db handle is
a3680 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
a3690 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65  we must hold the
a36a0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64   connection hand
a36b0 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65  le.  ** mutex he
a36c0 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68  re. Otherwise th
a36d0 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73  e read (and poss
a36e0 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64  ible write) of d
a36f0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
a3700 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c  .  ** is unsafe,
a3710 20 61 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20   as is the call 
a3720 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  to sqlite3Error(
a3730 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  )..  */.  assert
a3740 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33  ( !db || sqlite3
a3750 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
a3760 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
a3770 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f  db && (db->mallo
a3780 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53  cFailed || rc==S
a3790 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
a37a0 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  M) ){.    sqlite
a37b0 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
a37c0 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20  E_NOMEM, 0);.   
a37d0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
a37e0 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  d = 0;.    rc = 
a37f0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
a3800 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20  }.  return rc & 
a3810 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73  (db ? db->errMas
a3820 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a  k : 0xff);.}../*
a3830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
a3840 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a  d of malloc.c **
a3850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
a3880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
a3890 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e  gin file printf.
a38a0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
a38b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a38c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
a38d0 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22  .** The "printf"
a38e0 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f   code that follo
a38f0 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68  ws dates from th
a3900 65 20 31 39 38 30 27 73 2e 20 20 49 74 20 69 73  e 1980's.  It is
a3910 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69   in.** the publi
a3920 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f  c domain.  The o
a3930 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73  riginal comments
a3940 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65   are included he
a3950 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65  re for.** comple
a3960 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72  teness.  They ar
a3970 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61  e very out-of-da
a3980 74 65 20 62 75 74 20 6d 69 67 68 74 20 62 65 20  te but might be 
a3990 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20  useful as.** an 
a39a0 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72  historical refer
a39b0 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74  ence.  Most of t
a39c0 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73  he "enhancements
a39d0 22 20 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b  " have been back
a39e0 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61  ed.** out so tha
a39f0 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c  t the functional
a3a00 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73  ity is now the s
a3a10 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64 20  ame as standard 
a3a20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20  printf()..**.** 
a3a30 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20  $Id: printf.c,v 
a3a40 31 2e 39 39 20 32 30 30 38 2f 31 32 2f 31 30 20  1.99 2008/12/10 
a3a50 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45 78 70  19:26:24 drh Exp
a3a60 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   $.**.**********
a3a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3ab0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
a3ac0 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20  wing modules is 
a3ad0 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c  an enhanced repl
a3ae0 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20  acement for the 
a3af0 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74  "printf" subrout
a3b00 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e  ines.** found in
a3b10 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20   the standard C 
a3b20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f  library.  The fo
a3b30 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d  llowing enhancem
a3b40 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70  ents are.** supp
a3b50 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  orted:.**.**    
a3b60 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20    +  Additional 
a3b70 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20  functions.  The 
a3b80 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20  standard set of 
a3b90 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f  "printf" functio
a3ba0 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e  ns.**         in
a3bb0 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66  cludes printf, f
a3bc0 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c  printf, sprintf,
a3bd0 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e   vprintf, vfprin
a3be0 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  tf, and.**      
a3bf0 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68     vsprintf.  Th
a3c00 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74  is module adds t
a3c10 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
a3c20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20  .**           * 
a3c30 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72   snprintf -- Wor
a3c40 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c  ks like sprintf,
a3c50 20 62 75 74 20 68 61 73 20 61 6e 20 65 78 74 72   but has an extr
a3c60 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20  a argument.**   
a3c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3c80 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20         which is 
a3c90 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
a3ca0 62 75 66 66 65 72 20 77 72 69 74 74 65 6e 20 74  buffer written t
a3cb0 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  o..**.**        
a3cc0 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d     *  mprintf --
a3cd0 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72    Similar to spr
a3ce0 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75  intf.  Writes ou
a3cf0 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a  tput to memory.*
a3d00 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
a3d10 20 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69             obtai
a3d20 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e  ned from malloc.
a3d30 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
a3d40 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20   *  xprintf --  
a3d50 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e  Calls a function
a3d60 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f   to dispose of o
a3d70 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  utput..**.**    
a3d80 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74         *  nprint
a3d90 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c  f --  No output,
a3da0 20 62 75 74 20 72 65 74 75 72 6e 73 20 74 68 65   but returns the
a3db0 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   number of chara
a3dc0 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  cters.**        
a3dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3de0 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76    that would hav
a3df0 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79  e been output by
a3e00 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20   printf..**.**  
a3e10 20 20 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d           *  A v-
a3e20 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73   version (ex: vs
a3e30 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72  nprintf) of ever
a3e40 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c  y function is al
a3e50 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  so.**           
a3e60 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a     supplied..**.
a3e70 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66 65 77  **      +  A few
a3e80 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74   extensions to t
a3e90 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f  he formatting no
a3ea0 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f  tation are suppo
a3eb0 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  rted:.**.**     
a3ec0 20 20 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22        *  The "="
a3ed0 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74   flag (similar t
a3ee0 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20 74 68  o "-") causes th
a3ef0 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a  e output to be.*
a3f00 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  *              b
a3f10 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68  e centered in th
a3f20 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20  e appropriately 
a3f30 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a  sized field..**.
a3f40 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
a3f50 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74  The %b field out
a3f60 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20  puts an integer 
a3f70 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69  in binary notati
a3f80 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  on..**.**       
a3f90 20 20 20 20 2a 20 20 54 68 65 20 25 63 20 66 69      *  The %c fi
a3fa0 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20  eld now accepts 
a3fb0 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68  a precision.  Th
a3fc0 65 20 63 68 61 72 61 63 74 65 72 20 6f 75 74 70  e character outp
a3fd0 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ut.**           
a3fe0 20 20 20 69 73 20 72 65 70 65 61 74 65 64 20 62     is repeated b
a3ff0 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  y the number of 
a4000 74 69 6d 65 73 20 74 68 65 20 70 72 65 63 69 73  times the precis
a4010 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a  ion specifies..*
a4020 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
a4030 20 20 54 68 65 20 25 27 20 66 69 65 6c 64 20 77    The %' field w
a4040 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75  orks like %c, bu
a4050 74 20 74 61 6b 65 73 20 61 73 20 69 74 73 20 63  t takes as its c
a4060 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20  haracter the.** 
a4070 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78               nex
a4080 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74  t character of t
a4090 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67  he format string
a40a0 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  , instead of the
a40b0 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20   next.**        
a40c0 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20        argument. 
a40d0 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70   For example,  p
a40e0 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20  rintf("%.78'-") 
a40f0 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73   prints 78 minus
a4100 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
a4110 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65   signs, the same
a4120 20 61 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37   as  printf("%.7
a4130 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20  8c",'-')..**.** 
a4140 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d       +  When com
a4150 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20  piled using GCC 
a4160 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68 69 73  on a SPARC, this
a4170 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e   version of prin
a4180 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  tf is.**        
a4190 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65   faster than the
a41a0 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74 66 20   library printf 
a41b0 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a  for SUN OS 4.1..
a41c0 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c  **.**      +  Al
a41d0 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  l functions are 
a41e0 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e  fully reentrant.
a41f0 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .**.*/../*.** Co
a4200 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66  nversion types f
a4210 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73  all into various
a4220 20 63 61 74 65 67 6f 72 69 65 73 20 61 73 20 64   categories as d
a4230 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a  efined by the.**
a4240 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65   following enume
a4250 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  ration..*/.#defi
a4260 6e 65 20 65 74 52 41 44 49 58 20 20 20 20 20 20  ne etRADIX      
a4270 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79   1 /* Integer ty
a4280 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f  pes.  %d, %x, %o
a4290 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a  , and so forth *
a42a0 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41  /.#define etFLOA
a42b0 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f  T       2 /* Flo
a42c0 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66  ating point.  %f
a42d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58   */.#define etEX
a42e0 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45  P         3 /* E
a42f0 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74  xponentional not
a4300 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45  ation. %e and %E
a4310 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45   */.#define etGE
a4320 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46  NERIC     4 /* F
a4330 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e  loating or expon
a4340 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e  ential, dependin
a4350 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25  g on exponent. %
a4360 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  g */.#define etS
a4370 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20  IZE        5 /* 
a4380 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66  Return number of
a4390 20 63 68 61 72 61 63 74 65 72 73 20 70 72 6f 63   characters proc
a43a0 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e  essed so far. %n
a43b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54   */.#define etST
a43c0 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53  RING      6 /* S
a43d0 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64  trings. %s */.#d
a43e0 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e  efine etDYNSTRIN
a43f0 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63  G   7 /* Dynamic
a4400 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ally allocated s
a4410 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64  trings. %z */.#d
a4420 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20  efine etPERCENT 
a4430 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74      8 /* Percent
a4440 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23   symbol. %% */.#
a4450 64 65 66 69 6e 65 20 65 74 43 48 41 52 58 20 20  define etCHARX  
a4460 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63       9 /* Charac
a4470 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54  ters. %c */./* T
a4480 68 65 20 72 65 73 74 20 61 72 65 20 65 78 74 65  he rest are exte
a4490 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d  nsions, not norm
a44a0 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72  ally found in pr
a44b0 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e  intf() */.#defin
a44c0 65 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31  e etSQLESCAPE  1
a44d0 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74  0 /* Strings wit
a44e0 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20  h '\'' doubled. 
a44f0 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %q */.#define e
a4500 74 53 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f  tSQLESCAPE2 11 /
a4510 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27  * Strings with '
a4520 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20  \'' doubled and 
a4530 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a  enclosed in '',.
a4540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4550 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70            NULL p
a4560 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64  ointers replaced
a4570 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25   by SQL NULL.  %
a4580 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54  Q */.#define etT
a4590 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20  OKEN      12 /* 
a45a0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54  a pointer to a T
a45b0 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a  oken structure *
a45c0 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c  /.#define etSRCL
a45d0 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70  IST    13 /* a p
a45e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c  ointer to a SrcL
a45f0 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65  ist */.#define e
a4600 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f  tPOINTER    14 /
a4610 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73  * The %p convers
a4620 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65  ion */.#define e
a4630 74 53 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f  tSQLESCAPE3 15 /
a4640 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20  * %w -> Strings 
a4650 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65  with '\"' double
a4660 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f  d */.#define etO
a4670 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20  RDINAL    16 /* 
a4680 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20  %r -> 1st, 2nd, 
a4690 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20  3rd, 4th, etc.  
a46a0 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a  English only */.
a46b0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79  ../*.** An "etBy
a46c0 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20  te" is an 8-bit 
a46d0 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a  unsigned value..
a46e0 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67  */.typedef unsig
a46f0 6e 65 64 20 63 68 61 72 20 65 74 42 79 74 65 3b  ned char etByte;
a4700 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69  ../*.** Each bui
a4710 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ltin conversion 
a4720 63 68 61 72 61 63 74 65 72 20 28 65 78 3a 20 74  character (ex: t
a4730 68 65 20 27 64 27 20 69 6e 20 22 25 64 22 29 20  he 'd' in "%d") 
a4740 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20  is described.** 
a4750 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
a4760 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
a4770 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70  structure.*/.typ
a4780 65 64 65 66 20 73 74 72 75 63 74 20 65 74 5f 69  edef struct et_i
a4790 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72  nfo {   /* Infor
a47a0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63  mation about eac
a47b0 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a  h format field *
a47c0 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79 70 65  /.  char fmttype
a47d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a47e0 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64  The format field
a47f0 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a   code letter */.
a4800 20 20 65 74 42 79 74 65 20 62 61 73 65 3b 20 20    etByte base;  
a4810 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
a4820 65 20 62 61 73 65 20 66 6f 72 20 72 61 64 69 78  e base for radix
a4830 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20   conversion */. 
a4840 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20   etByte flags;  
a4850 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
a4860 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47   or more of FLAG
a4870 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f  _ constants belo
a4880 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79  w */.  etByte ty
a4890 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pe;             
a48a0 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61  /* Conversion pa
a48b0 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79  radigm */.  etBy
a48c0 74 65 20 63 68 61 72 73 65 74 3b 20 20 20 20 20  te charset;     
a48d0 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
a48e0 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66  nto aDigits[] of
a48f0 20 74 68 65 20 64 69 67 69 74 73 20 73 74 72 69   the digits stri
a4900 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70  ng */.  etByte p
a4910 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  refix;          
a4920 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
a4930 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65  aPrefix[] of the
a4940 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a   prefix string *
a4950 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a  /.} et_info;../*
a4960 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
a4970 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66  es for et_info.f
a4980 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lags.*/.#define 
a4990 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20  FLAG_SIGNED  1  
a49a0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
a49b0 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65  e value to conve
a49c0 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a  rt is signed */.
a49d0 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54  #define FLAG_INT
a49e0 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72  ERN  2     /* Tr
a49f0 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e  ue if for intern
a4a00 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23  al use only */.#
a4a10 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49  define FLAG_STRI
a4a20 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c  NG  4     /* All
a4a30 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63  ow infinity prec
a4a40 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  ision */.../*.**
a4a50 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
a4a60 61 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64  able is searched
a4a70 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74   linearly, so it
a4a80 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20   is good to put 
a4a90 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71  the.** most freq
a4aa0 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76  uently used conv
a4ab0 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72  ersion types fir
a4ac0 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  st..*/.static co
a4ad0 6e 73 74 20 63 68 61 72 20 61 44 69 67 69 74 73  nst char aDigits
a4ae0 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39  [] = "0123456789
a4af0 41 42 43 44 45 46 30 31 32 33 34 35 36 37 38 39  ABCDEF0123456789
a4b00 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20  abcdef";.static 
a4b10 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72 65 66  const char aPref
a4b20 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58  ix[] = "-x0\000X
a4b30 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  0";.static const
a4b40 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f   et_info fmtinfo
a4b50 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c  [] = {.  {  'd',
a4b60 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c   10, 1, etRADIX,
a4b70 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20        0,  0 },. 
a4b80 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20   {  's',  0, 4, 
a4b90 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c  etSTRING,     0,
a4ba0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c    0 },.  {  'g',
a4bb0 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49    0, 1, etGENERI
a4bc0 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20  C,    30, 0 },. 
a4bd0 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20   {  'z',  0, 4, 
a4be0 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c  etDYNSTRING,  0,
a4bf0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c    0 },.  {  'q',
a4c00 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43    0, 4, etSQLESC
a4c10 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20  APE,  0,  0 },. 
a4c20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20   {  'Q',  0, 4, 
a4c30 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c  etSQLESCAPE2, 0,
a4c40 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c    0 },.  {  'w',
a4c50 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43    0, 4, etSQLESC
a4c60 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20  APE3, 0,  0 },. 
a4c70 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20   {  'c',  0, 0, 
a4c80 65 74 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c  etCHARX,      0,
a4c90 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c    0 },.  {  'o',
a4ca0 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c    8, 0, etRADIX,
a4cb0 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20        0,  2 },. 
a4cc0 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20   {  'u', 10, 0, 
a4cd0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
a4ce0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c    0 },.  {  'x',
a4cf0 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c   16, 0, etRADIX,
a4d00 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20        16, 1 },. 
a4d10 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20   {  'X', 16, 0, 
a4d20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
a4d30 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53    4 },.#ifndef S
a4d40 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
a4d50 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27  ING_POINT.  {  '
a4d60 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f  f',  0, 1, etFLO
a4d70 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  AT,      0,  0 }
a4d80 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20  ,.  {  'e',  0, 
a4d90 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20  1, etEXP,       
a4da0 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27   30, 0 },.  {  '
a4db0 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50  E',  0, 1, etEXP
a4dc0 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d  ,        14, 0 }
a4dd0 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20  ,.  {  'G',  0, 
a4de0 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20  1, etGENERIC,   
a4df0 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66   14, 0 },.#endif
a4e00 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31  .  {  'i', 10, 1
a4e10 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
a4e20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e  0,  0 },.  {  'n
a4e30 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45  ',  0, 0, etSIZE
a4e40 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  ,       0,  0 },
a4e50 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30  .  {  '%',  0, 0
a4e60 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20  , etPERCENT,    
a4e70 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70  0,  0 },.  {  'p
a4e80 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e  ', 16, 0, etPOIN
a4e90 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c  TER,    0,  1 },
a4ea0 0a 20 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32  .  {  'T',  0, 2
a4eb0 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20  , etTOKEN,      
a4ec0 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53  0,  0 },.  {  'S
a4ed0 27 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c  ',  0, 2, etSRCL
a4ee0 49 53 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c  IST,    0,  0 },
a4ef0 0a 20 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33  .  {  'r', 10, 3
a4f00 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20  , etORDINAL,    
a4f10 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a  0,  0 },.};../*.
a4f20 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ** If SQLITE_OMI
a4f30 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
a4f40 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
a4f50 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c  n none of the fl
a4f60 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20  oating point.** 
a4f70 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c  conversions will
a4f80 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65   work..*/.#ifnde
a4f90 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
a4fa0 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a  OATING_POINT./*.
a4fb0 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 20 64  ** "*val" is a d
a4fc0 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61 74 20  ouble such that 
a4fd0 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30  0.1 <= *val < 10
a4fe0 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  .0.** Return the
a4ff0 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f 72 20   ascii code for 
a5000 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69 67 69  the leading digi
a5010 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a  t of *val, then.
a5020 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61  ** multiply "*va
a5030 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20 72 65  l" by 10.0 to re
a5040 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a  normalize..**.**
a5050 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20   Example:.**    
a5060 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 61 6c   input:     *val
a5070 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20   = 3.14159.**   
a5080 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a 76 61    output:    *va
a5090 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20 66 75  l = 1.4159    fu
a50a0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20  nction return = 
a50b0 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  '3'.**.** The co
a50c0 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e  unter *cnt is in
a50d0 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74  cremented each t
a50e0 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e  ime.  After coun
a50f0 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a 20 31  ter exceeds.** 1
a5100 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  6 (the number of
a5110 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67   significant dig
a5120 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20  its in a 64-bit 
a5130 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a 2a 2a  float) '0' is.**
a5140 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65 64   always returned
a5150 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
a5160 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e   et_getdigit(LON
a5170 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76 61  GDOUBLE_TYPE *va
a5180 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20  l, int *cnt){.  
a5190 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e  int digit;.  LON
a51a0 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a  GDOUBLE_TYPE d;.
a51b0 20 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e    if( (*cnt)++ >
a51c0 3d 20 31 36 20 29 20 72 65 74 75 72 6e 20 27 30  = 16 ) return '0
a51d0 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69 6e  ';.  digit = (in
a51e0 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69  t)*val;.  d = di
a51f0 67 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20  git;.  digit += 
a5200 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a  '0';.  *val = (*
a5210 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20  val - d)*10.0;. 
a5220 20 72 65 74 75 72 6e 20 28 63 68 61 72 29 64 69   return (char)di
a5230 67 69 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  git;.}.#endif /*
a5240 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
a5250 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a  ATING_POINT */..
a5260 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73  /*.** Append N s
a5270 70 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20  pace characters 
a5280 74 6f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  to the given str
a5290 69 6e 67 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73  ing buffer..*/.s
a52a0 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e  tatic void appen
a52b0 64 53 70 61 63 65 28 53 74 72 41 63 63 75 6d 20  dSpace(StrAccum 
a52c0 2a 70 41 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b  *pAccum, int N){
a52d0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
a52e0 63 68 61 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d  char zSpaces[] =
a52f0 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
a5300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
a5310 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69  ;.  while( N>=(i
a5320 6e 74 29 73 69 7a 65 6f 66 28 7a 53 70 61 63 65  nt)sizeof(zSpace
a5330 73 29 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69  s)-1 ){.    sqli
a5340 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a5350 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 65  d(pAccum, zSpace
a5360 73 2c 20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65  s, sizeof(zSpace
a5370 73 29 2d 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20  s)-1);.    N -= 
a5380 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d  sizeof(zSpaces)-
a5390 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30  1;.  }.  if( N>0
a53a0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
a53b0 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
a53c0 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e  ccum, zSpaces, N
a53d0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
a53e0 4f 6e 20 6d 61 63 68 69 6e 65 73 20 77 69 74 68  On machines with
a53f0 20 61 20 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73   a small stack s
a5400 69 7a 65 2c 20 79 6f 75 20 63 61 6e 20 72 65 64  ize, you can red
a5410 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c  efine the.** SQL
a5420 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
a5430 5a 45 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68  ZE to be less th
a5440 61 6e 20 33 35 30 2e 20 20 42 75 74 20 62 65 77  an 350.  But bew
a5450 61 72 65 20 2d 20 66 6f 72 0a 2a 2a 20 73 6d 61  are - for.** sma
a5460 6c 6c 65 72 20 76 61 6c 75 65 73 20 73 6f 6d 65  ller values some
a5470 20 25 66 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20   %f conversions 
a5480 6d 61 79 20 67 6f 20 69 6e 74 6f 20 61 6e 20 69  may go into an i
a5490 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 2e 0a 2a 2f  nfinite loop..*/
a54a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a54b0 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 0a 23  PRINT_BUF_SIZE.#
a54c0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50   define SQLITE_P
a54d0 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33 35  RINT_BUF_SIZE 35
a54e0 30 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  0.#endif.#define
a54f0 20 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49 54   etBUFSIZE SQLIT
a5500 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45  E_PRINT_BUF_SIZE
a5510 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
a5520 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a   output buffer *
a5530 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f  /../*.** The roo
a5540 74 20 70 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20  t program.  All 
a5550 76 61 72 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20  variations call 
a5560 74 68 69 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a  this core..**.**
a5570 20 49 4e 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75   INPUTS:.**   fu
a5580 6e 63 20 20 20 54 68 69 73 20 69 73 20 61 20 70  nc   This is a p
a5590 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
a55a0 74 69 6f 6e 20 74 61 6b 69 6e 67 20 74 68 72 65  tion taking thre
a55b0 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20  e arguments.**  
a55c0 20 20 20 20 20 20 20 20 20 20 31 2e 20 41 20 70            1. A p
a55d0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69  ointer to anythi
a55e0 6e 67 2e 20 20 53 61 6d 65 20 61 73 20 74 68 65  ng.  Same as the
a55f0 20 22 61 72 67 22 20 70 61 72 61 6d 65 74 65 72   "arg" parameter
a5600 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
a5610 32 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  2. A pointer to 
a5620 74 68 65 20 6c 69 73 74 20 6f 66 20 63 68 61 72  the list of char
a5630 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74  acters to be out
a5640 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  put.**          
a5650 20 20 20 20 20 28 4e 6f 74 65 2c 20 74 68 69 73       (Note, this
a5660 20 6c 69 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c   list is NOT nul
a5670 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a  l terminated.).*
a5680 2a 20 20 20 20 20 20 20 20 20 20 20 20 33 2e 20  *            3. 
a5690 41 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65  An integer numbe
a56a0 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
a56b0 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a  to be output..**
a56c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
a56d0 4e 6f 74 65 3a 20 54 68 69 73 20 6e 75 6d 62 65  Note: This numbe
a56e0 72 20 6d 69 67 68 74 20 62 65 20 7a 65 72 6f 2e  r might be zero.
a56f0 29 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20  ).**.**   arg   
a5700 20 54 68 69 73 20 69 73 20 74 68 65 20 70 6f 69   This is the poi
a5710 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67  nter to anything
a5720 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 70   which will be p
a5730 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20  assed as the.** 
a5740 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20 61           first a
a5750 72 67 75 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63  rgument to "func
a5760 22 2e 20 20 55 73 65 20 69 74 20 66 6f 72 20 77  ".  Use it for w
a5770 68 61 74 65 76 65 72 20 79 6f 75 20 6c 69 6b 65  hatever you like
a5780 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20  ..**.**   fmt   
a5790 20 54 68 69 73 20 69 73 20 74 68 65 20 66 6f 72   This is the for
a57a0 6d 61 74 20 73 74 72 69 6e 67 2c 20 61 73 20 69  mat string, as i
a57b0 6e 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e  n the usual prin
a57c0 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20  t..**.**   ap   
a57d0 20 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e    This is a poin
a57e0 74 65 72 20 74 6f 20 61 20 6c 69 73 74 20 6f 66  ter to a list of
a57f0 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d   arguments.  Sam
a5800 65 20 61 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20  e as in.**      
a5810 20 20 20 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a      vfprint..**.
a5820 2a 2a 20 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20  ** OUTPUTS:.**  
a5830 20 20 20 20 20 20 20 20 54 68 65 20 72 65 74 75          The retu
a5840 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
a5850 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
a5860 63 68 61 72 61 63 74 65 72 73 20 73 65 6e 74 20  characters sent 
a5870 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  to.**          t
a5880 68 65 20 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e  he function "fun
a5890 63 22 2e 20 20 52 65 74 75 72 6e 73 20 2d 31 20  c".  Returns -1 
a58a0 6f 6e 20 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a  on a error..**.*
a58b0 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * Note that the 
a58c0 6f 72 64 65 72 20 69 6e 20 77 68 69 63 68 20 61  order in which a
a58d0 75 74 6f 6d 61 74 69 63 20 76 61 72 69 61 62 6c  utomatic variabl
a58e0 65 73 20 61 72 65 20 64 65 63 6c 61 72 65 64 20  es are declared 
a58f0 62 65 6c 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74  below.** seems t
a5900 6f 20 6d 61 6b 65 20 61 20 62 69 67 20 64 69 66  o make a big dif
a5910 66 65 72 65 6e 63 65 20 69 6e 20 64 65 74 65 72  ference in deter
a5920 6d 69 6e 69 6e 67 20 68 6f 77 20 66 61 73 74 20  mining how fast 
a5930 74 68 69 73 20 62 65 61 73 74 0a 2a 2a 20 77 69  this beast.** wi
a5940 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ll run..*/.SQLIT
a5950 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a5960 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 0a  qlite3VXPrintf(.
a5970 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63    StrAccum *pAcc
a5980 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  um,             
a5990 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61       /* Accumula
a59a0 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  te results here 
a59b0 2a 2f 0a 20 20 69 6e 74 20 75 73 65 45 78 74 65  */.  int useExte
a59c0 6e 64 65 64 2c 20 20 20 20 20 20 20 20 20 20 20  nded,           
a59d0 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77          /* Allow
a59e0 20 65 78 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76   extended %-conv
a59f0 65 72 73 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e  ersions */.  con
a5a00 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 20 20  st char *fmt,   
a5a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5a20 2f 2a 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67  /* Format string
a5a30 20 2a 2f 0a 20 20 76 61 5f 6c 69 73 74 20 61 70   */.  va_list ap
a5a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5a50 20 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 75           /* argu
a5a60 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ments */.){.  in
a5a70 74 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  t c;            
a5a80 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
a5a90 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68   character in th
a5aa0 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  e format string 
a5ab0 2a 2f 0a 20 20 63 68 61 72 20 2a 62 75 66 70 74  */.  char *bufpt
a5ac0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a5ad0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
a5ae0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66  e conversion buf
a5af0 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65  fer */.  int pre
a5b00 63 69 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20  cision;         
a5b10 20 20 20 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e      /* Precision
a5b20 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
a5b30 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c  field */.  int l
a5b40 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 20 20  ength;          
a5b50 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
a5b60 6f 66 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a  of the field */.
a5b70 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20    int idx;      
a5b80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a5b90 41 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  A general purpos
a5ba0 65 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  e loop counter *
a5bb0 2f 0a 20 20 69 6e 74 20 77 69 64 74 68 3b 20 20  /.  int width;  
a5bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a5bd0 2a 20 57 69 64 74 68 20 6f 66 20 74 68 65 20 63  * Width of the c
a5be0 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a  urrent field */.
a5bf0 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 65    etByte flag_le
a5c00 66 74 6a 75 73 74 69 66 79 3b 20 20 20 2f 2a 20  ftjustify;   /* 
a5c10 54 72 75 65 20 69 66 20 22 2d 22 20 66 6c 61 67  True if "-" flag
a5c20 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
a5c30 20 65 74 42 79 74 65 20 66 6c 61 67 5f 70 6c 75   etByte flag_plu
a5c40 73 73 69 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54  ssign;      /* T
a5c50 72 75 65 20 69 66 20 22 2b 22 20 66 6c 61 67 20  rue if "+" flag 
a5c60 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
a5c70 65 74 42 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e  etByte flag_blan
a5c80 6b 73 69 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72  ksign;     /* Tr
a5c90 75 65 20 69 66 20 22 20 22 20 66 6c 61 67 20 69  ue if " " flag i
a5ca0 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65  s present */.  e
a5cb0 74 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 65 72  tByte flag_alter
a5cc0 6e 61 74 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75  nateform; /* Tru
a5cd0 65 20 69 66 20 22 23 22 20 66 6c 61 67 20 69 73  e if "#" flag is
a5ce0 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74   present */.  et
a5cf0 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72  Byte flag_altfor
a5d00 6d 32 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  m2;      /* True
a5d10 20 69 66 20 22 21 22 20 66 6c 61 67 20 69 73 20   if "!" flag is 
a5d20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
a5d30 79 74 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  yte flag_zeropad
a5d40 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
a5d50 69 66 20 66 69 65 6c 64 20 77 69 64 74 68 20 63  if field width c
a5d60 6f 6e 73 74 61 6e 74 20 73 74 61 72 74 73 20 77  onstant starts w
a5d70 69 74 68 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74  ith zero */.  et
a5d80 42 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20  Byte flag_long; 
a5d90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
a5da0 20 69 66 20 22 6c 22 20 66 6c 61 67 20 69 73 20   if "l" flag is 
a5db0 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
a5dc0 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e  yte flag_longlon
a5dd0 67 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  g;      /* True 
a5de0 69 66 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67  if the "ll" flag
a5df0 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
a5e00 20 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20   etByte done;   
a5e10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
a5e20 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20  oop termination 
a5e30 66 6c 61 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65  flag */.  sqlite
a5e40 5f 75 69 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75  _uint64 longvalu
a5e50 65 3b 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f  e;   /* Value fo
a5e60 72 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 20  r integer types 
a5e70 2a 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f  */.  LONGDOUBLE_
a5e80 54 59 50 45 20 72 65 61 6c 76 61 6c 75 65 3b 20  TYPE realvalue; 
a5e90 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 72 65 61  /* Value for rea
a5ea0 6c 20 74 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e  l types */.  con
a5eb0 73 74 20 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f  st et_info *info
a5ec0 70 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  p;      /* Point
a5ed0 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70  er to the approp
a5ee0 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63  riate info struc
a5ef0 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 62  ture */.  char b
a5f00 75 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 20  uf[etBUFSIZE];  
a5f10 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69       /* Conversi
a5f20 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63  on buffer */.  c
a5f30 68 61 72 20 70 72 65 66 69 78 3b 20 20 20 20 20  har prefix;     
a5f40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
a5f50 66 69 78 20 63 68 61 72 61 63 74 65 72 2e 20 20  fix character.  
a5f60 22 2b 22 20 6f 72 20 22 2d 22 20 6f 72 20 22 20  "+" or "-" or " 
a5f70 22 20 6f 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20  " or '\0'. */.  
a5f80 65 74 42 79 74 65 20 78 74 79 70 65 20 3d 20 30  etByte xtype = 0
a5f90 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ;          /* Co
a5fa0 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67  nversion paradig
a5fb0 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78  m */.  char *zEx
a5fc0 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  tra;            
a5fd0 20 20 2f 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72    /* Extra memor
a5fe0 79 20 75 73 65 64 20 66 6f 72 20 65 74 54 43 4c  y used for etTCL
a5ff0 45 53 43 41 50 45 20 63 6f 6e 76 65 72 73 69 6f  ESCAPE conversio
a6000 6e 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ns */.#ifndef SQ
a6010 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
a6020 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20  NG_POINT.  int  
a6030 65 78 70 2c 20 65 32 3b 20 20 20 20 20 20 20 20  exp, e2;        
a6040 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e        /* exponen
a6050 74 20 6f 66 20 72 65 61 6c 20 6e 75 6d 62 65 72  t of real number
a6060 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f  s */.  double ro
a6070 75 6e 64 65 72 3b 20 20 20 20 20 20 20 20 20 20  under;          
a6080 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 72 6f    /* Used for ro
a6090 75 6e 64 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20  unding floating 
a60a0 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a  point values */.
a60b0 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 64 70    etByte flag_dp
a60c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a60d0 54 72 75 65 20 69 66 20 64 65 63 69 6d 61 6c 20  True if decimal 
a60e0 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 62 65 20  point should be 
a60f0 73 68 6f 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74  shown */.  etByt
a6100 65 20 66 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20  e flag_rtz;     
a6110 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a6120 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20   trailing zeros 
a6130 73 68 6f 75 6c 64 20 62 65 20 72 65 6d 6f 76 65  should be remove
a6140 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  d */.  etByte fl
a6150 61 67 5f 65 78 70 3b 20 20 20 20 20 20 20 20 20  ag_exp;         
a6160 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 6f 72    /* True to for
a6170 63 65 20 64 69 73 70 6c 61 79 20 6f 66 20 74 68  ce display of th
a6180 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20  e exponent */.  
a6190 69 6e 74 20 6e 73 64 3b 20 20 20 20 20 20 20 20  int nsd;        
a61a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
a61b0 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63  mber of signific
a61c0 61 6e 74 20 64 69 67 69 74 73 20 72 65 74 75 72  ant digits retur
a61d0 6e 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20  ned */.#endif.. 
a61e0 20 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62   length = 0;.  b
a61f0 75 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28  ufpt = 0;.  for(
a6200 3b 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b  ; (c=(*fmt))!=0;
a6210 20 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69 66 28   ++fmt){.    if(
a6220 20 63 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20   c!='%' ){.     
a6230 20 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20 20 20   int amt;.      
a6240 62 75 66 70 74 20 3d 20 28 63 68 61 72 20 2a 29  bufpt = (char *)
a6250 66 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d  fmt;.      amt =
a6260 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   1;.      while(
a6270 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27   (c=(*++fmt))!='
a6280 25 27 20 26 26 20 63 21 3d 30 20 29 20 61 6d 74  %' && c!=0 ) amt
a6290 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ++;.      sqlite
a62a0 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
a62b0 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 61  pAccum, bufpt, a
a62c0 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63  mt);.      if( c
a62d0 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
a62e0 20 7d 0a 20 20 20 20 69 66 28 20 28 63 3d 28 2a   }.    if( (c=(*
a62f0 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20  ++fmt))==0 ){.  
a6300 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a6310 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
a6320 2c 20 22 25 22 2c 20 31 29 3b 0a 20 20 20 20 20  , "%", 1);.     
a6330 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
a6340 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68    /* Find out wh
a6350 61 74 20 66 6c 61 67 73 20 61 72 65 20 70 72 65  at flags are pre
a6360 73 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67  sent */.    flag
a6370 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 66  _leftjustify = f
a6380 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66  lag_plussign = f
a6390 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20  lag_blanksign = 
a63a0 0a 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 65 72  .     flag_alter
a63b0 6e 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f  nateform = flag_
a63c0 61 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f  altform2 = flag_
a63d0 7a 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20  zeropad = 0;.   
a63e0 20 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64   done = 0;.    d
a63f0 6f 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28  o{.      switch(
a6400 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61   c ){.        ca
a6410 73 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c  se '-':   flag_l
a6420 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 20  eftjustify = 1; 
a6430 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
a6440 20 20 20 63 61 73 65 20 27 2b 27 3a 20 20 20 66     case '+':   f
a6450 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31  lag_plussign = 1
a6460 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;        break;.
a6470 20 20 20 20 20 20 20 20 63 61 73 65 20 27 20 27          case ' '
a6480 3a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69  :   flag_blanksi
a6490 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72  gn = 1;       br
a64a0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
a64b0 65 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c  e '#':   flag_al
a64c0 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b  ternateform = 1;
a64d0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a64e0 20 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c    case '!':   fl
a64f0 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b  ag_altform2 = 1;
a6500 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a6510 20 20 20 20 20 20 20 63 61 73 65 20 27 30 27 3a         case '0':
a6520 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20     flag_zeropad 
a6530 3d 20 31 3b 20 20 20 20 20 20 20 20 20 62 72 65  = 1;         bre
a6540 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61  ak;.        defa
a6550 75 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31  ult:    done = 1
a6560 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a6570 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
a6580 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 21 64 6f  .    }while( !do
a6590 6e 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74  ne && (c=(*++fmt
a65a0 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20  ))!=0 );.    /* 
a65b0 47 65 74 20 74 68 65 20 66 69 65 6c 64 20 77 69  Get the field wi
a65c0 64 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 74 68  dth */.    width
a65d0 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 63 3d   = 0;.    if( c=
a65e0 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 77 69  ='*' ){.      wi
a65f0 64 74 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  dth = va_arg(ap,
a6600 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  int);.      if( 
a6610 77 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 20 20  width<0 ){.     
a6620 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
a6630 69 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ify = 1;.       
a6640 20 77 69 64 74 68 20 3d 20 2d 77 69 64 74 68 3b   width = -width;
a6650 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
a6660 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d   = *++fmt;.    }
a6670 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c  else{.      whil
a6680 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d  e( c>='0' && c<=
a6690 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77  '9' ){.        w
a66a0 69 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20  idth = width*10 
a66b0 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20  + c - '0';.     
a66c0 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
a66d0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
a66e0 20 69 66 28 20 77 69 64 74 68 20 3e 20 65 74 42   if( width > etB
a66f0 55 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20  UFSIZE-10 ){.   
a6700 20 20 20 77 69 64 74 68 20 3d 20 65 74 42 55 46     width = etBUF
a6710 53 49 5a 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20  SIZE-10;.    }. 
a6720 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72     /* Get the pr
a6730 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69  ecision */.    i
a6740 66 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  f( c=='.' ){.   
a6750 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30     precision = 0
a6760 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66  ;.      c = *++f
a6770 6d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d  mt;.      if( c=
a6780 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
a6790 70 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61  precision = va_a
a67a0 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20  rg(ap,int);.    
a67b0 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
a67c0 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20  n<0 ) precision 
a67d0 3d 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20  = -precision;.  
a67e0 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74        c = *++fmt
a67f0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
a6800 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e         while( c>
a6810 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29  ='0' && c<='9' )
a6820 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63  {.          prec
a6830 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f  ision = precisio
a6840 6e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a  n*10 + c - '0';.
a6850 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b            c = *+
a6860 2b 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  +fmt;.        }.
a6870 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
a6880 65 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69  e{.      precisi
a6890 6f 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  on = -1;.    }. 
a68a0 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f     /* Get the co
a68b0 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f  nversion type mo
a68c0 64 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66  difier */.    if
a68d0 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20  ( c=='l' ){.    
a68e0 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b    flag_long = 1;
a68f0 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d  .      c = *++fm
a6900 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  t;.      if( c==
a6910 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66  'l' ){.        f
a6920 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31  lag_longlong = 1
a6930 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b  ;.        c = *+
a6940 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73  +fmt;.      }els
a6950 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  e{.        flag_
a6960 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20  longlong = 0;.  
a6970 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
a6980 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67  .      flag_long
a6990 20 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67   = flag_longlong
a69a0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
a69b0 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66  /* Fetch the inf
a69c0 6f 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  o entry for the 
a69d0 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66  field */.    inf
a69e0 6f 70 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  op = 0;.    for(
a69f0 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 79  idx=0; idx<Array
a6a00 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 69  Size(fmtinfo); i
a6a10 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  dx++){.      if(
a6a20 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d   c==fmtinfo[idx]
a6a30 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 20  .fmttype ){.    
a6a40 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74      infop = &fmt
a6a50 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20  info[idx];.     
a6a60 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e 64     if( useExtend
a6a70 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c  ed || (infop->fl
a6a80 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 52  ags & FLAG_INTER
a6a90 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  N)==0 ){.       
a6aa0 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f 70     xtype = infop
a6ab0 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20 20  ->type;.        
a6ac0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a6ad0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20   return;.       
a6ae0 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
a6af0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
a6b00 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a      zExtra = 0;.
a6b10 20 20 20 20 69 66 28 20 69 6e 66 6f 70 3d 3d 30      if( infop==0
a6b20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
a6b30 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a  ;.    }...    /*
a6b40 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65 63 69   Limit the preci
a6b50 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20  sion to prevent 
a6b60 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b  overflowing buf[
a6b70 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73  ] during convers
a6b80 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ion */.    if( p
a6b90 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49  recision>etBUFSI
a6ba0 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d  ZE-40 && (infop-
a6bb0 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54  >flags & FLAG_ST
a6bc0 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20  RING)==0 ){.    
a6bd0 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74    precision = et
a6be0 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20  BUFSIZE-40;.    
a6bf0 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  }..    /*.    **
a6c00 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
a6c10 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e  variables are in
a6c20 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c  itialized as fol
a6c30 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  lows:.    **.   
a6c40 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72   **   flag_alter
a6c50 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20  nateform        
a6c60 20 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20    TRUE if a '#' 
a6c70 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20  is present..    
a6c80 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72  **   flag_altfor
a6c90 6d 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m2              
a6ca0 20 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69   TRUE if a '!' i
a6cb0 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a  s present..    *
a6cc0 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  *   flag_plussig
a6cd0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
a6ce0 54 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73  TRUE if a '+' is
a6cf0 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a   present..    **
a6d00 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
a6d10 69 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54  ify            T
a6d20 52 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20  RUE if a '-' is 
a6d30 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68  present or if th
a6d40 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  e.    **        
a6d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6d60 20 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64         field wid
a6d70 74 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e  th was negative.
a6d80 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a  .    **   flag_z
a6d90 65 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20  eropad          
a6da0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68        TRUE if th
a6db0 65 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69  e width began wi
a6dc0 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66  th 0..    **   f
a6dd0 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20  lag_long        
a6de0 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20             TRUE 
a6df0 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c  if the letter 'l
a6e00 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64  ' (ell) prefixed
a6e10 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
a6e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6e30 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72        the conver
a6e40 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a  sion character..
a6e50 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f      **   flag_lo
a6e60 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20  nglong          
a6e70 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65       TRUE if the
a6e80 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c   letter 'll' (el
a6e90 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a  l ell) prefixed.
a6ea0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
a6eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6ec0 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73       the convers
a6ed0 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20  ion character.. 
a6ee0 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61     **   flag_bla
a6ef0 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20  nksign          
a6f00 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20      TRUE if a ' 
a6f10 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  ' is present..  
a6f20 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20    **   width    
a6f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6f40 20 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64     The specified
a6f50 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54   field width.  T
a6f60 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20  his is.    **   
a6f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6f80 20 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61              alwa
a6f90 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e  ys non-negative.
a6fa0 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65    Zero is the de
a6fb0 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20  fault..    **   
a6fc0 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20  precision       
a6fd0 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
a6fe0 73 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73  specified precis
a6ff0 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c  ion.  The defaul
a7000 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  t.    **        
a7010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7020 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20         is -1..  
a7030 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20    **   xtype    
a7040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7050 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20     The class of 
a7060 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a  the conversion..
a7070 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20      **   infop  
a7080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7090 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20       Pointer to 
a70a0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
a70b0 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20  info struct..   
a70c0 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20   */.    switch( 
a70d0 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63  xtype ){.      c
a70e0 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20  ase etPOINTER:. 
a70f0 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67         flag_long
a7100 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68  long = sizeof(ch
a7110 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34  ar*)==sizeof(i64
a7120 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  );.        flag_
a7130 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68  long = sizeof(ch
a7140 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e  ar*)==sizeof(lon
a7150 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  g int);.        
a7160 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20  /* Fall through 
a7170 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61  into the next ca
a7180 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  se */.      case
a7190 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20   etORDINAL:.    
a71a0 20 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a    case etRADIX:.
a71b0 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f          if( info
a71c0 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f  p->flags & FLAG_
a71d0 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20  SIGNED ){.      
a71e0 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20      i64 v;.     
a71f0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f       if( flag_lo
a7200 6e 67 6c 6f 6e 67 20 29 20 20 20 76 20 3d 20 76  nglong )   v = v
a7210 61 5f 61 72 67 28 61 70 2c 69 36 34 29 3b 0a 20  a_arg(ap,i64);. 
a7220 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66           else if
a7230 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 20 20 76  ( flag_long )  v
a7240 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e   = va_arg(ap,lon
a7250 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  g int);.        
a7260 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20    else          
a7270 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61          v = va_a
a7280 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20  rg(ap,int);.    
a7290 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b        if( v<0 ){
a72a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e  .            lon
a72b0 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20  gvalue = -v;.   
a72c0 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
a72d0 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20  = '-';.         
a72e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a72f0 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20      longvalue = 
a7300 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  v;.            i
a7310 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e  f( flag_plussign
a7320 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78   )        prefix
a7330 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20   = '+';.        
a7340 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61      else if( fla
a7350 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70  g_blanksign )  p
a7360 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20  refix = ' ';.   
a7370 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20           else   
a7380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7390 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a      prefix = 0;.
a73a0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
a73b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a73c0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f       if( flag_lo
a73d0 6e 67 6c 6f 6e 67 20 29 20 20 20 6c 6f 6e 67 76  nglong )   longv
a73e0 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70  alue = va_arg(ap
a73f0 2c 75 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20  ,u64);.         
a7400 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c   else if( flag_l
a7410 6f 6e 67 20 29 20 20 6c 6f 6e 67 76 61 6c 75 65  ong )  longvalue
a7420 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73   = va_arg(ap,uns
a7430 69 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 29 3b  igned long int);
a7440 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20  .          else 
a7450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7460 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f   longvalue = va_
a7470 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20  arg(ap,unsigned 
a7480 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  int);.          
a7490 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20  prefix = 0;.    
a74a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
a74b0 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 29  ( longvalue==0 )
a74c0 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66   flag_alternatef
a74d0 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  orm = 0;.       
a74e0 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61   if( flag_zeropa
a74f0 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c 77  d && precision<w
a7500 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29  idth-(prefix!=0)
a7510 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72   ){.          pr
a7520 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68 2d  ecision = width-
a7530 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20 20  (prefix!=0);.   
a7540 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
a7550 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74 42 55  ufpt = &buf[etBU
a7560 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 20 20  FSIZE-1];.      
a7570 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 4f    if( xtype==etO
a7580 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20  RDINAL ){.      
a7590 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
a75a0 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22   char zOrd[] = "
a75b0 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20 20  thstndrd";.     
a75c0 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69 6e       int x = (in
a75d0 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20 31  t)(longvalue % 1
a75e0 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  0);.          if
a75f0 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 76  ( x>=4 || (longv
a7600 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 29  alue/10)%10==1 )
a7610 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 20  {.            x 
a7620 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 0;.          }
a7630 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b 65  .          buf[e
a7640 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20 7a 4f  tBUFSIZE-3] = zO
a7650 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20 20 20  rd[x*2];.       
a7660 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45     buf[etBUFSIZE
a7670 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b 31  -2] = zOrd[x*2+1
a7680 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66  ];.          buf
a7690 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20  pt -= 2;.       
a76a0 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20   }.        {.   
a76b0 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20         register 
a76c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 65 74  const char *cset
a76d0 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 72 65  ;      /* Use re
a76e0 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 65 65  gisters for spee
a76f0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72  d */.          r
a7700 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 73 65  egister int base
a7710 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 65 74  ;.          cset
a7720 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e 66 6f   = &aDigits[info
a7730 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 20  p->charset];.   
a7740 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 69 6e         base = in
a7750 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 20 20  fop->base;.     
a7760 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 20 20       do{        
a7770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7790 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f     /* Convert to
a77a0 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 20 20   ascii */.      
a77b0 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29        *(--bufpt)
a77c0 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 6c 75   = cset[longvalu
a77d0 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 20 20  e%base];.       
a77e0 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d       longvalue =
a77f0 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 65 3b   longvalue/base;
a7800 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 6c  .          }whil
a7810 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 29  e( longvalue>0 )
a7820 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a7830 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e      length = (in
a7840 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a  t)(&buf[etBUFSIZ
a7850 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20  E-1]-bufpt);.   
a7860 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65       for(idx=pre
a7870 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69  cision-length; i
a7880 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20  dx>0; idx--){.  
a7890 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70          *(--bufp
a78a0 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20  t) = '0';       
a78b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a78c0 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61        /* Zero pa
a78d0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20  d */.        }. 
a78e0 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69         if( prefi
a78f0 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d  x ) *(--bufpt) =
a7900 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20   prefix;        
a7910 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69         /* Add si
a7920 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  gn */.        if
a7930 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65  ( flag_alternate
a7940 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70  form && infop->p
a7950 72 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a  refix ){      /*
a7960 20 41 64 64 20 22 30 22 20 6f 72 20 22 30 78 22   Add "0" or "0x"
a7970 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f   */.          co
a7980 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20  nst char *pre;. 
a7990 20 20 20 20 20 20 20 20 20 63 68 61 72 20 78 3b           char x;
a79a0 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 20 3d  .          pre =
a79b0 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d   &aPrefix[infop-
a79c0 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20  >prefix];.      
a79d0 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70      for(; (x=(*p
a79e0 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20  re))!=0; pre++) 
a79f0 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a  *(--bufpt) = x;.
a7a00 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a7a10 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 29    length = (int)
a7a20 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d  (&buf[etBUFSIZE-
a7a30 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 20 20  1]-bufpt);.     
a7a40 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a7a50 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a 20 20  case etFLOAT:.  
a7a60 20 20 20 20 63 61 73 65 20 65 74 45 58 50 3a 0a      case etEXP:.
a7a70 20 20 20 20 20 20 63 61 73 65 20 65 74 47 45 4e        case etGEN
a7a80 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 72 65  ERIC:.        re
a7a90 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67  alvalue = va_arg
a7aa0 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 69 66  (ap,double);.#if
a7ab0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
a7ac0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
a7ad0 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63          if( prec
a7ae0 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73  ision<0 ) precis
a7af0 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 20  ion = 6;        
a7b00 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20   /* Set default 
a7b10 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20  precision */.   
a7b20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69       if( precisi
a7b30 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 2d 31  on>etBUFSIZE/2-1
a7b40 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20  0 ) precision = 
a7b50 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 3b 0a  etBUFSIZE/2-10;.
a7b60 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c          if( real
a7b70 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 20 20  value<0.0 ){.   
a7b80 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65         realvalue
a7b90 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0a 20   = -realvalue;. 
a7ba0 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
a7bb0 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 7d  = '-';.        }
a7bc0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a7bd0 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  if( flag_plussig
a7be0 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 72 65  n )          pre
a7bf0 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20  fix = '+';.     
a7c00 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c       else if( fl
a7c10 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20  ag_blanksign )  
a7c20 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a    prefix = ' ';.
a7c30 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20            else  
a7c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7c50 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20         prefix = 
a7c60 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
a7c70 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d       if( xtype==
a7c80 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 72 65  etGENERIC && pre
a7c90 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 63 69  cision>0 ) preci
a7ca0 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a 20 20  sion--;.#if 0.  
a7cb0 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 69 6e        /* Roundin
a7cc0 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 53 44  g works like BSD
a7cd0 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 61   when the consta
a7ce0 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 73 65  nt 0.4999 is use
a7cf0 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a 20 20  d.  Wierd! */.  
a7d00 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72        for(idx=pr
a7d10 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72  ecision, rounder
a7d20 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 3b 20  =0.4999; idx>0; 
a7d30 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d  idx--, rounder*=
a7d40 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  0.1);.#else.    
a7d50 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 73 20      /* It makes 
a7d60 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 75 73  more sense to us
a7d70 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 20 20  e 0.5 */.       
a7d80 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69   for(idx=precisi
a7d90 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 35 3b  on, rounder=0.5;
a7da0 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72   idx>0; idx--, r
a7db0 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d 0a 23  ounder*=0.1){}.#
a7dc0 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66  endif.        if
a7dd0 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41 54  ( xtype==etFLOAT
a7de0 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20   ) realvalue += 
a7df0 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20  rounder;.       
a7e00 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72 65   /* Normalize re
a7e10 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68 69  alvalue to withi
a7e20 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61 6c  n 10.0 > realval
a7e30 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 20 20  ue >= 1.0 */.   
a7e40 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a 20 20       exp = 0;.  
a7e50 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
a7e60 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65 29 72  3IsNaN((double)r
a7e70 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20  ealvalue) ){.   
a7e80 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22         bufpt = "
a7e90 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20  NaN";.          
a7ea0 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20  length = 3;.    
a7eb0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a7ec0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a7ed0 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30  f( realvalue>0.0
a7ee0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68   ){.          wh
a7ef0 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d  ile( realvalue>=
a7f00 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30  1e32 && exp<=350
a7f10 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d   ){ realvalue *=
a7f20 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b   1e-32; exp+=32;
a7f30 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69   }.          whi
a7f40 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31  le( realvalue>=1
a7f50 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29  e8 && exp<=350 )
a7f60 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31  { realvalue *= 1
a7f70 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20  e-8; exp+=8; }. 
a7f80 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
a7f90 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20  realvalue>=10.0 
a7fa0 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72  && exp<=350 ){ r
a7fb0 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b  ealvalue *= 0.1;
a7fc0 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20   exp++; }.      
a7fd0 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76      while( realv
a7fe0 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61  alue<1e-8 ){ rea
a7ff0 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65  lvalue *= 1e8; e
a8000 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20  xp-=8; }.       
a8010 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
a8020 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76  lue<1.0 ){ realv
a8030 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78  alue *= 10.0; ex
a8040 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20  p--; }.         
a8050 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a   if( exp>350 ){.
a8060 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
a8070 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20  prefix=='-' ){. 
a8080 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66               buf
a8090 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20  pt = "-Inf";.   
a80a0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
a80b0 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29  f( prefix=='+' )
a80c0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
a80d0 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a  bufpt = "+Inf";.
a80e0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
a80f0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e{.             
a8100 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a   bufpt = "Inf";.
a8110 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
a8120 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
a8130 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
a8140 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20  30(bufpt);.     
a8150 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a8160 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a8170 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70    }.        bufp
a8180 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20  t = buf;.       
a8190 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49   /*.        ** I
a81a0 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70 65  f the field type
a81b0 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20 74   is etGENERIC, t
a81c0 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20 65  hen convert to e
a81d0 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20 20  ither etEXP.    
a81e0 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f 41      ** or etFLOA
a81f0 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 74  T, as appropriat
a8200 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
a8210 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20 3d        flag_exp =
a8220 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a 20   xtype==etEXP;. 
a8230 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65         if( xtype
a8240 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20 20  !=etFLOAT ){.   
a8250 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65         realvalue
a8260 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20   += rounder;.   
a8270 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76         if( realv
a8280 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72 65  alue>=10.0 ){ re
a8290 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20  alvalue *= 0.1; 
a82a0 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  exp++; }.       
a82b0 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78   }.        if( x
a82c0 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20  type==etGENERIC 
a82d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61  ){.          fla
a82e0 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61 6c  g_rtz = !flag_al
a82f0 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20 20  ternateform;.   
a8300 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c 2d         if( exp<-
a8310 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73 69  4 || exp>precisi
a8320 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  on ){.          
a8330 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50 3b    xtype = etEXP;
a8340 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
a8350 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72  {.            pr
a8360 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73  ecision = precis
a8370 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20 20  ion - exp;.     
a8380 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65         xtype = e
a8390 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20 20  tFLOAT;.        
a83a0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
a83b0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61  e{.          fla
a83c0 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20 20  g_rtz = 0;.     
a83d0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
a83e0 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b   xtype==etEXP ){
a83f0 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d 20  .          e2 = 
a8400 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
a8410 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d  {.          e2 =
a8420 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a   exp;.        }.
a8430 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30 3b          nsd = 0;
a8440 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64 70  .        flag_dp
a8450 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30 20   = (precision>0 
a8460 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 6c 74  ?1:0) | flag_alt
a8470 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c 61  ernateform | fla
a8480 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 20  g_altform2;.    
a8490 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e 20      /* The sign 
a84a0 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65 20  in front of the 
a84b0 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20  number */.      
a84c0 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b 0a    if( prefix ){.
a84d0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a84e0 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a 20  t++) = prefix;. 
a84f0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a8500 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f 72   /* Digits prior
a8510 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c 20   to the decimal 
a8520 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20  point */.       
a8530 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20 20   if( e2<0 ){.   
a8540 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a8550 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20  ) = '0';.       
a8560 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a8570 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20 65    for(; e2>=0; e
a8580 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20  2--){.          
a8590 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65    *(bufpt++) = e
a85a0 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c  t_getdigit(&real
a85b0 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 20  value,&nsd);.   
a85c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a85d0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68   }.        /* Th
a85e0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  e decimal point 
a85f0 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  */.        if( f
a8600 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20  lag_dp ){.      
a8610 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a8620 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a   '.';.        }.
a8630 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20 64          /* "0" d
a8640 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20  igits after the 
a8650 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62 75  decimal point bu
a8660 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72  t before the fir
a8670 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69  st.        ** si
a8680 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 20  gnificant digit 
a8690 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f  of the number */
a86a0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32 2b  .        for(e2+
a86b0 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73 69  +; e2<0; precisi
a86c0 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20 20  on--, e2++){.   
a86d0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
a86e0 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20 20  recision>0 );.  
a86f0 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
a8700 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20  +) = '0';.      
a8710 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 53    }.        /* S
a8720 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74  ignificant digit
a8730 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69  s after the deci
a8740 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20  mal point */.   
a8750 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72 65       while( (pre
a8760 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a 20  cision--)>0 ){. 
a8770 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
a8780 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69  ++) = et_getdigi
a8790 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73  t(&realvalue,&ns
a87a0 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  d);.        }.  
a87b0 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20        /* Remove 
a87c0 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 61  trailing zeros a
a87d0 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e 6f  nd the "." if no
a87e0 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20 74   digits follow t
a87f0 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20 20  he "." */.      
a8800 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20 26    if( flag_rtz &
a8810 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20  & flag_dp ){.   
a8820 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62 75         while( bu
a8830 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 2a  fpt[-1]=='0' ) *
a8840 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a 20  (--bufpt) = 0;. 
a8850 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
a8860 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20 20   bufpt>buf );.  
a8870 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70          if( bufp
a8880 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20  t[-1]=='.' ){.  
a8890 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c            if( fl
a88a0 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a 20  ag_altform2 ){. 
a88b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62               *(b
a88c0 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20  ufpt++) = '0';. 
a88d0 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
a88e0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
a88f0 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a  *(--bufpt) = 0;.
a8900 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
a8910 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a8920 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41    }.        /* A
a8930 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73 75  dd the "eNNN" su
a8940 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20 20  ffix */.        
a8950 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c 20  if( flag_exp || 
a8960 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b 0a  xtype==etEXP ){.
a8970 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a8980 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b 69  t++) = aDigits[i
a8990 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a  nfop->charset];.
a89a0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78            if( ex
a89b0 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  p<0 ){.         
a89c0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
a89d0 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 3b  '-'; exp = -exp;
a89e0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
a89f0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28  {.            *(
a8a00 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b 0a  bufpt++) = '+';.
a8a10 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
a8a20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d 31        if( exp>=1
a8a30 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  00 ){.          
a8a40 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28    *(bufpt++) = (
a8a50 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29 2b  char)((exp/100)+
a8a60 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a 20  '0');        /* 
a8a70 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20  100's digit */. 
a8a80 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 25             exp %
a8a90 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 20  = 100;.         
a8aa0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62   }.          *(b
a8ab0 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29  ufpt++) = (char)
a8ac0 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20 20  (exp/10+'0');   
a8ad0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 27            /* 10'
a8ae0 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20  s digit */.     
a8af0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
a8b00 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30 2b  = (char)(exp%10+
a8b10 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20  '0');           
a8b20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 2a    /* 1's digit *
a8b30 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  /.        }.    
a8b40 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b 0a      *bufpt = 0;.
a8b50 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
a8b60 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 72  converted number
a8b70 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e 64   is in buf[] and
a8b80 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64   zero terminated
a8b90 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 20  . Output it..   
a8ba0 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61       ** Note tha
a8bb0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20  t the number is 
a8bc0 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 64  in the usual ord
a8bd0 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 64  er, not reversed
a8be0 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 20   as with.       
a8bf0 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e 76   ** integer conv
a8c00 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20  ersions. */.    
a8c10 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e      length = (in
a8c20 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b 0a 20  t)(bufpt-buf);. 
a8c30 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
a8c40 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  uf;..        /* 
a8c50 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 41  Special case:  A
a8c60 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73  dd leading zeros
a8c70 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a 65 72   if the flag_zer
a8c80 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 20 20  opad flag is.   
a8c90 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e 64 20       ** set and 
a8ca0 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 74 20  we are not left 
a8cb0 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 20 20  justified */.   
a8cc0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65       if( flag_ze
a8cd0 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 5f 6c  ropad && !flag_l
a8ce0 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 6c 65  eftjustify && le
a8cf0 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b 0a 20  ngth < width){. 
a8d00 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a           int i;.
a8d10 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 50            int nP
a8d20 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c 65 6e  ad = width - len
a8d30 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 20 66  gth;.          f
a8d40 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e 3d 6e  or(i=width; i>=n
a8d50 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20  Pad; i--){.     
a8d60 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 5d 20         bufpt[i] 
a8d70 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 5d 3b  = bufpt[i-nPad];
a8d80 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
a8d90 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 66 69         i = prefi
a8da0 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20 20  x!=0;.          
a8db0 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29 20  while( nPad-- ) 
a8dc0 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30 27  bufpt[i++] = '0'
a8dd0 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67  ;.          leng
a8de0 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 20 20  th = width;.    
a8df0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
a8e00 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a8e10 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a 0a 20    case etSIZE:. 
a8e20 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67 28         *(va_arg(
a8e30 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 63 63  ap,int*)) = pAcc
a8e40 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20  um->nChar;.     
a8e50 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74     length = widt
a8e60 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  h = 0;.        b
a8e70 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
a8e80 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20 20 20   etPERCENT:.    
a8e90 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27 25 27      buf[0] = '%'
a8ea0 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20  ;.        bufpt 
a8eb0 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 6c  = buf;.        l
a8ec0 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20 20 20  ength = 1;.     
a8ed0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a8ee0 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20 20  case etCHARX:.  
a8ef0 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61 72 67        c = va_arg
a8f00 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20  (ap,int);.      
a8f10 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68 61 72    buf[0] = (char
a8f20 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  )c;.        if( 
a8f30 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a  precision>=0 ){.
a8f40 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 64            for(id
a8f50 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69 73 69  x=1; idx<precisi
a8f60 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66 5b 69  on; idx++) buf[i
a8f70 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20  dx] = (char)c;. 
a8f80 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20           length 
a8f90 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20  = precision;.   
a8fa0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a8fb0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b        length =1;
a8fc0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a8fd0 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a     bufpt = buf;.
a8fe0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a8ff0 20 20 20 20 20 63 61 73 65 20 65 74 53 54 52 49       case etSTRI
a9000 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65  NG:.      case e
a9010 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 20 20  tDYNSTRING:.    
a9020 20 20 20 20 62 75 66 70 74 20 3d 20 76 61 5f 61      bufpt = va_a
a9030 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20  rg(ap,char*);.  
a9040 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d        if( bufpt=
a9050 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
a9060 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 20 20  bufpt = "";.    
a9070 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 78 74      }else if( xt
a9080 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 4e 47  ype==etDYNSTRING
a9090 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45   ){.          zE
a90a0 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a 20 20  xtra = bufpt;.  
a90b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a90c0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30  if( precision>=0
a90d0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   ){.          fo
a90e0 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65 6e 67  r(length=0; leng
a90f0 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26 26 20  th<precision && 
a9100 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b 20 6c  bufpt[length]; l
a9110 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20 20 20  ength++){}.     
a9120 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a9130 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c      length = sql
a9140 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66  ite3Strlen30(buf
a9150 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pt);.        }. 
a9160 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a9170 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53      case etSQLES
a9180 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 73 65  CAPE:.      case
a9190 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0a 20   etSQLESCAPE2:. 
a91a0 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45       case etSQLE
a91b0 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 20 20  SCAPE3: {.      
a91c0 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20 69    int i, j, n, i
a91d0 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69  snull;.        i
a91e0 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20  nt needQuote;.  
a91f0 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a 20        char ch;. 
a9200 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d 20         char q = 
a9210 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53  ((xtype==etSQLES
a9220 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29  CAPE3)?'"':'\'')
a9230 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61  ;   /* Quote cha
a9240 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  racter */.      
a9250 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d    char *escarg =
a9260 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a   va_arg(ap,char*
a9270 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c  );.        isnul
a9280 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20  l = escarg==0;. 
a9290 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c         if( isnul
a92a0 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 74  l ) escarg = (xt
a92b0 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45  ype==etSQLESCAPE
a92c0 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e  2 ? "NULL" : "(N
a92d0 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20  ULL)");.        
a92e0 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65  for(i=n=0; (ch=e
a92f0 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b  scarg[i])!=0; i+
a9300 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
a9310 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a  ( ch==q )  n++;.
a9320 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a9330 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69    needQuote = !i
a9340 73 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d  snull && xtype==
a9350 65 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20  etSQLESCAPE2;.  
a9360 20 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31        n += i + 1
a9370 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a   + needQuote*2;.
a9380 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74          if( n>et
a9390 42 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20  BUFSIZE ){.     
a93a0 20 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78       bufpt = zEx
a93b0 74 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  tra = sqlite3Mal
a93c0 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20  loc( n );.      
a93d0 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30      if( bufpt==0
a93e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a93f0 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61  pAccum->mallocFa
a9400 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
a9410 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
a9420 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a9430 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a9440 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a     bufpt = buf;.
a9450 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a9460 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20    j = 0;.       
a9470 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29   if( needQuote )
a9480 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b   bufpt[j++] = q;
a9490 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
a94a0 3b 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29  ; (ch=escarg[i])
a94b0 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  !=0; i++){.     
a94c0 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20       bufpt[j++] 
a94d0 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20 20  = ch;.          
a94e0 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 70  if( ch==q ) bufp
a94f0 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20  t[j++] = ch;.   
a9500 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a9510 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62  f( needQuote ) b
a9520 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20  ufpt[j++] = q;. 
a9530 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d 20         bufpt[j] 
a9540 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e  = 0;.        len
a9550 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20  gth = j;.       
a9560 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 6f   /* The precisio
a9570 6e 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20  n is ignored on 
a9580 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20  %q and %Q */.   
a9590 20 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65 63       /* if( prec
a95a0 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63  ision>=0 && prec
a95b0 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c  ision<length ) l
a95c0 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f  ength = precisio
a95d0 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72  n; */.        br
a95e0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
a95f0 20 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a     case etTOKEN:
a9600 20 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e   {.        Token
a9610 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72   *pToken = va_ar
a9620 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20  g(ap, Token*);. 
a9630 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65         if( pToke
a9640 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  n ){.          s
a9650 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
a9660 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f  pend(pAccum, (co
a9670 6e 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e  nst char*)pToken
a9680 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b  ->z, pToken->n);
a9690 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a96a0 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74     length = widt
a96b0 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  h = 0;.        b
a96c0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
a96d0 20 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49      case etSRCLI
a96e0 53 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72  ST: {.        Sr
a96f0 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61  cList *pSrc = va
a9700 5f 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74  _arg(ap, SrcList
a9710 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  *);.        int 
a9720 6b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  k = va_arg(ap, i
a9730 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72  nt);.        str
a9740 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
a9750 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d   *pItem = &pSrc-
a9760 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61  >a[k];.        a
a9770 73 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b  ssert( k>=0 && k
a9780 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20  <pSrc->nSrc );. 
a9790 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
a97a0 2d 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20  ->zDatabase ){. 
a97b0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
a97c0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
a97d0 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44  Accum, pItem->zD
a97e0 61 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20  atabase, -1);.  
a97f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
a9800 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
a9810 63 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20  ccum, ".", 1);. 
a9820 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a9830 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a9840 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70  Append(pAccum, p
a9850 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29  Item->zName, -1)
a9860 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68  ;.        length
a9870 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20   = width = 0;.  
a9880 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a9890 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64     }.    }/* End
a98a0 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 68 65   switch over the
a98b0 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a   format type */.
a98c0 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68      /*.    ** Th
a98d0 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f  e text of the co
a98e0 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e  nversion is poin
a98f0 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 70 74  ted to by "bufpt
a9900 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20  " and is.    ** 
a9910 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63 74  "length" charact
a9920 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66  ers long.  The f
a9930 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 22 77  ield width is "w
a9940 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a  idth".  Do.    *
a9950 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 20  * the output..  
a9960 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 6c    */.    if( !fl
a9970 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 29  ag_leftjustify )
a9980 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 65 72  {.      register
a9990 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 20   int nspace;.   
a99a0 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64 74     nspace = widt
a99b0 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20  h-length;.      
a99c0 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a  if( nspace>0 ){.
a99d0 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 53 70          appendSp
a99e0 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61  ace(pAccum, nspa
a99f0 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ce);.      }.   
a9a00 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74   }.    if( lengt
a9a10 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  h>0 ){.      sql
a9a20 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a9a30 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74  nd(pAccum, bufpt
a9a40 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d  , length);.    }
a9a50 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65  .    if( flag_le
a9a60 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20  ftjustify ){.   
a9a70 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20     register int 
a9a80 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73  nspace;.      ns
a9a90 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e  pace = width-len
a9aa0 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  gth;.      if( n
a9ab0 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20  space>0 ){.     
a9ac0 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28 70     appendSpace(p
a9ad0 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a  Accum, nspace);.
a9ae0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
a9af0 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a    if( zExtra ){.
a9b00 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
a9b10 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20  ee(zExtra);.    
a9b20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20  }.  }/* End for 
a9b30 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f  loop over the fo
a9b40 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d  rmat string */.}
a9b50 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 74   /* End of funct
a9b60 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70  ion */../*.** Ap
a9b70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 20  pend N bytes of 
a9b80 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74  text from z to t
a9b90 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65  he StrAccum obje
a9ba0 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ct..*/.SQLITE_PR
a9bb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a9bc0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
a9bd0 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f  (StrAccum *p, co
a9be0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
a9bf0 20 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f   N){.  if( p->to
a9c00 6f 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63  oBig | p->malloc
a9c10 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
a9c20 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  turn;.  }.  if( 
a9c30 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73  N<0 ){.    N = s
a9c40 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
a9c50 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d  );.  }.  if( N==
a9c60 30 20 7c 7c 20 7a 3d 3d 30 20 29 7b 0a 20 20 20  0 || z==0 ){.   
a9c70 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
a9c80 66 28 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d  f( p->nChar+N >=
a9c90 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20   p->nAlloc ){.  
a9ca0 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20    char *zNew;.  
a9cb0 20 20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 6c    if( !p->useMal
a9cc0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  loc ){.      p->
a9cd0 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20  tooBig = 1;.    
a9ce0 20 20 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20    N = p->nAlloc 
a9cf0 2d 20 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b 0a  - p->nChar - 1;.
a9d00 20 20 20 20 20 20 69 66 28 20 4e 3c 3d 30 20 29        if( N<=0 )
a9d10 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
a9d20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
a9d30 6c 73 65 7b 0a 20 20 20 20 20 20 69 36 34 20 73  lse{.      i64 s
a9d40 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 3b  zNew = p->nChar;
a9d50 0a 20 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d 20  .      szNew += 
a9d60 4e 20 2b 20 31 3b 0a 20 20 20 20 20 20 69 66 28  N + 1;.      if(
a9d70 20 73 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 6c   szNew > p->mxAl
a9d80 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73  loc ){.        s
a9d90 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
a9da0 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  set(p);.        
a9db0 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20  p->tooBig = 1;. 
a9dc0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
a9dd0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a9de0 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20      p->nAlloc = 
a9df0 28 69 6e 74 29 73 7a 4e 65 77 3b 0a 20 20 20 20  (int)szNew;.    
a9e00 20 20 7d 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d    }.      zNew =
a9e10 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
a9e20 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 41  Raw(p->db, p->nA
a9e30 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 20 20 69 66  lloc );.      if
a9e40 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( zNew ){.      
a9e50 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 70    memcpy(zNew, p
a9e60 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 6e 43 68 61  ->zText, p->nCha
a9e70 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  r);.        sqli
a9e80 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74  te3StrAccumReset
a9e90 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
a9ea0 7a 54 65 78 74 20 3d 20 7a 4e 65 77 3b 0a 20 20  zText = zNew;.  
a9eb0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a9ec0 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c     p->mallocFail
a9ed0 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ed = 1;.        
a9ee0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
a9ef0 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20  eset(p);.       
a9f00 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
a9f10 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d  .    }.  }.  mem
a9f20 63 70 79 28 26 70 2d 3e 7a 54 65 78 74 5b 70 2d  cpy(&p->zText[p-
a9f30 3e 6e 43 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0a  >nChar], z, N);.
a9f40 20 20 70 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e 3b    p->nChar += N;
a9f50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73 68  .}../*.** Finish
a9f60 20 6f 66 66 20 61 20 73 74 72 69 6e 67 20 62 79   off a string by
a9f70 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 69 74 20   making sure it 
a9f80 69 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  is zero-terminat
a9f90 65 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ed..** Return a 
a9fa0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  pointer to the r
a9fb0 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e  esulting string.
a9fc0 20 20 52 65 74 75 72 6e 20 61 20 4e 55 4c 4c 0a    Return a NULL.
a9fd0 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 61 6e  ** pointer if an
a9fe0 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20  y kind of error 
a9ff0 77 61 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e  was encountered.
aa000 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
aa010 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
aa020 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53  StrAccumFinish(S
aa030 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69  trAccum *p){.  i
aa040 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20  f( p->zText ){. 
aa050 20 20 20 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e     p->zText[p->n
aa060 43 68 61 72 5d 20 3d 20 30 3b 0a 20 20 20 20 69  Char] = 0;.    i
aa070 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20  f( p->useMalloc 
aa080 26 26 20 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e  && p->zText==p->
aa090 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20 70  zBase ){.      p
aa0a0 2d 3e 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65  ->zText = sqlite
aa0b0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e  3DbMallocRaw(p->
aa0c0 64 62 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 20 29  db, p->nChar+1 )
aa0d0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 7a  ;.      if( p->z
aa0e0 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Text ){.        
aa0f0 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74 2c  memcpy(p->zText,
aa100 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e 43   p->zBase, p->nC
aa110 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65  har+1);.      }e
aa120 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  lse{.        p->
aa130 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
aa140 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
aa150 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e    }.  return p->
aa160 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zText;.}../*.** 
aa170 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 75  Reset an StrAccu
aa180 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c 61  m string.  Recla
aa190 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 20  im all malloced 
aa1a0 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  memory..*/.SQLIT
aa1b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
aa1c0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
aa1d0 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70 29  set(StrAccum *p)
aa1e0 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74  {.  if( p->zText
aa1f0 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20  !=p->zBase ){.  
aa200 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
aa210 70 2d 3e 64 62 2c 20 70 2d 3e 7a 54 65 78 74 29  p->db, p->zText)
aa220 3b 0a 20 20 7d 0a 20 20 70 2d 3e 7a 54 65 78 74  ;.  }.  p->zText
aa230 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49   = 0;.}../*.** I
aa240 6e 69 74 69 61 6c 69 7a 65 20 61 20 73 74 72 69  nitialize a stri
aa250 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 2a  ng accumulator.*
aa260 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
aa270 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
aa280 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63  AccumInit(StrAcc
aa290 75 6d 20 2a 70 2c 20 63 68 61 72 20 2a 7a 42 61  um *p, char *zBa
aa2a0 73 65 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6d  se, int n, int m
aa2b0 78 29 7b 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d  x){.  p->zText =
aa2c0 20 70 2d 3e 7a 42 61 73 65 20 3d 20 7a 42 61 73   p->zBase = zBas
aa2d0 65 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b 0a  e;.  p->db = 0;.
aa2e0 20 20 70 2d 3e 6e 43 68 61 72 20 3d 20 30 3b 0a    p->nChar = 0;.
aa2f0 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b    p->nAlloc = n;
aa300 0a 20 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20  .  p->mxAlloc = 
aa310 6d 78 3b 0a 20 20 70 2d 3e 75 73 65 4d 61 6c 6c  mx;.  p->useMall
aa320 6f 63 20 3d 20 31 3b 0a 20 20 70 2d 3e 74 6f 6f  oc = 1;.  p->too
aa330 42 69 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 6d 61  Big = 0;.  p->ma
aa340 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
aa350 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69  }../*.** Print i
aa360 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
aa370 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
aa380 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68  alloc().  Use th
aa390 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d  e internal.** %-
aa3a0 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e  conversion exten
aa3b0 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sions..*/.SQLITE
aa3c0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
aa3d0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73  qlite3VMPrintf(s
aa3e0 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
aa3f0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
aa400 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20   va_list ap){.  
aa410 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20  char *z;.  char 
aa420 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49  zBase[SQLITE_PRI
aa430 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20  NT_BUF_SIZE];.  
aa440 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20  StrAccum acc;.  
aa450 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49  sqlite3StrAccumI
aa460 6e 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c  nit(&acc, zBase,
aa470 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a   sizeof(zBase),.
aa480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aa490 20 20 20 20 20 20 64 62 20 3f 20 64 62 2d 3e 61        db ? db->a
aa4a0 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
aa4b0 49 54 5f 4c 45 4e 47 54 48 5d 20 3a 20 53 51 4c  IT_LENGTH] : SQL
aa4c0 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b  ITE_MAX_LENGTH);
aa4d0 0a 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b 0a  .  acc.db = db;.
aa4e0 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74    sqlite3VXPrint
aa4f0 66 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72 6d  f(&acc, 1, zForm
aa500 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73  at, ap);.  z = s
aa510 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
aa520 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69 66  nish(&acc);.  if
aa530 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 6c  ( acc.mallocFail
aa540 65 64 20 26 26 20 64 62 20 29 7b 0a 20 20 20 20  ed && db ){.    
aa550 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
aa560 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
aa570 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn z;.}../*.** P
aa580 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
aa590 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
aa5a0 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20  qliteMalloc().  
aa5b0 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  Use the internal
aa5c0 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  .** %-conversion
aa5d0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a   extensions..*/.
aa5e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
aa5f0 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69  har *sqlite3MPri
aa600 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ntf(sqlite3 *db,
aa610 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
aa620 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
aa630 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
aa640 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28   *z;.  va_start(
aa650 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
aa660 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  z = sqlite3VMPri
aa670 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  ntf(db, zFormat,
aa680 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
aa690 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  p);.  return z;.
aa6a0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71  }../*.** Like sq
aa6b0 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20  lite3MPrintf(), 
aa6c0 62 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  but call sqlite3
aa6d0 44 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72  DbFree() on zStr
aa6e0 20 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e   after formattin
aa6f0 67 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20  g.** the string 
aa700 61 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72  and before retur
aa710 6e 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75  nning.  This rou
aa720 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64  tine is intended
aa730 20 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74   to be used.** t
aa740 6f 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73  o modify an exis
aa750 74 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f  ting string.  Fo
aa760 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  r example:.**.**
aa770 20 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74         x = sqlit
aa780 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c  e3MPrintf(db, x,
aa790 20 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66   "prefix %s suff
aa7a0 69 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53  ix", x);.**.*/.S
aa7b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
aa7c0 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65  ar *sqlite3MAppe
aa7d0 6e 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ndf(sqlite3 *db,
aa7e0 20 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e   char *zStr, con
aa7f0 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
aa800 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
aa810 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b  t ap;.  char *z;
aa820 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20  .  va_start(ap, 
aa830 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20  zFormat);.  z = 
aa840 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
aa850 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
aa860 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
aa870 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
aa880 64 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74  db, zStr);.  ret
aa890 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
aa8a0 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  Print into memor
aa8b0 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
aa8c0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
aa8d0 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65  .  Omit the inte
aa8e0 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72  rnal.** %-conver
aa8f0 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e  sion extensions.
aa900 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
aa910 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70  har *sqlite3_vmp
aa920 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72  rintf(const char
aa930 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69   *zFormat, va_li
aa940 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a  st ap){.  char *
aa950 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b  z;.  char zBase[
aa960 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46  SQLITE_PRINT_BUF
aa970 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63  _SIZE];.  StrAcc
aa980 75 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20  um acc;.#ifndef 
aa990 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
aa9a0 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74  INIT.  if( sqlit
aa9b0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
aa9c0 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64  ) return 0;.#end
aa9d0 69 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41  if.  sqlite3StrA
aa9e0 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a  ccumInit(&acc, z
aa9f0 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61  Base, sizeof(zBa
aaa00 73 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f  se), SQLITE_MAX_
aaa10 4c 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74  LENGTH);.  sqlit
aaa20 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c  e3VXPrintf(&acc,
aaa30 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29   0, zFormat, ap)
aaa40 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53  ;.  z = sqlite3S
aaa50 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61  trAccumFinish(&a
aaa60 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b  cc);.  return z;
aaa70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  .}../*.** Print 
aaa80 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
aaa90 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
aaaa0 33 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f  3_malloc()().  O
aaab0 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  mit the internal
aaac0 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  .** %-conversion
aaad0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a   extensions..*/.
aaae0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
aaaf0 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66  *sqlite3_mprintf
aab00 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f  (const char *zFo
aab10 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
aab20 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
aab30 20 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   *z;.#ifndef SQL
aab40 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
aab50 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  T.  if( sqlite3_
aab60 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72  initialize() ) r
aab70 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a  eturn 0;.#endif.
aab80 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
aab90 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73  Format);.  z = s
aaba0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
aabb0 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
aabc0 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65  va_end(ap);.  re
aabd0 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn z;.}../*.**
aabe0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
aabf0 66 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73  f() works like s
aac00 6e 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74  nprintf() except
aac10 20 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73   that it ignores
aac20 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   the.** current 
aac30 6c 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e  locale settings.
aac40 20 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74    This is import
aac50 61 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62  ant for SQLite b
aac60 65 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65  ecause we.** are
aac70 20 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65   not able to use
aac80 20 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65   a "," as the de
aac90 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70  cimal point in p
aaca0 6c 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a  lace of "." as.*
aacb0 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73  * specified by s
aacc0 6f 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a  ome locales..*/.
aacd0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
aace0 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74  *sqlite3_snprint
aacf0 66 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a  f(int n, char *z
aad00 42 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Buf, const char 
aad10 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
aad20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f    char *z;.  va_
aad30 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63  list ap;.  StrAc
aad40 63 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20  cum acc;..  if( 
aad50 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  n<=0 ){.    retu
aad60 72 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73  rn zBuf;.  }.  s
aad70 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
aad80 69 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e  it(&acc, zBuf, n
aad90 2c 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d  , 0);.  acc.useM
aada0 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f  alloc = 0;.  va_
aadb0 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74  start(ap,zFormat
aadc0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72  );.  sqlite3VXPr
aadd0 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46  intf(&acc, 0, zF
aade0 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
aadf0 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20  _end(ap);.  z = 
aae00 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
aae10 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72  inish(&acc);.  r
aae20 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20  eturn z;.}..#if 
aae30 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
aae40 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65  EBUG)./*.** A ve
aae50 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28  rsion of printf(
aae60 29 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e  ) that understan
aae70 64 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66  ds %lld.  Used f
aae80 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a  or debugging..**
aae90 20 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75   The printf() bu
aaea0 69 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65  ilt into some ve
aaeb0 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77  rsions of window
aaec0 73 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72  s does not under
aaed0 73 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e  stand %lld.** an
aaee0 64 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79  d segfaults if y
aaef0 6f 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e  ou give it a lon
aaf00 67 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53  g long int..*/.S
aaf10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
aaf20 69 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  id sqlite3DebugP
aaf30 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72  rintf(const char
aaf40 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
aaf50 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
aaf60 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20   StrAccum acc;. 
aaf70 20 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b   char zBuf[500];
aaf80 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
aaf90 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75  umInit(&acc, zBu
aafa0 66 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c  f, sizeof(zBuf),
aafb0 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61   0);.  acc.useMa
aafc0 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73  lloc = 0;.  va_s
aafd0 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29  tart(ap,zFormat)
aafe0 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  ;.  sqlite3VXPri
aaff0 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f  ntf(&acc, 0, zFo
ab000 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
ab010 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74  end(ap);.  sqlit
ab020 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68  e3StrAccumFinish
ab030 28 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74  (&acc);.  fprint
ab040 66 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a  f(stdout,"%s", z
ab050 42 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73  Buf);.  fflush(s
ab060 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66  tdout);.}.#endif
ab070 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
ab080 2a 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e  * End of printf.
ab090 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
ab0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab0c0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
ab0d0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e  * Begin file ran
ab0e0 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dom.c **********
ab0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab110 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
ab120 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
ab130 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
ab140 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
ab150 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
ab160 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
ab170 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
ab180 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
ab190 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
ab1a0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
ab1b0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
ab1c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
ab1d0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
ab1e0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
ab1f0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
ab200 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
ab210 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
ab220 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
ab230 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
ab240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab280 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
ab290 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
ab2a0 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
ab2b0 20 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d   a pseudo-random
ab2c0 20 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72   number.** gener
ab2d0 61 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20  ator (PRNG) for 
ab2e0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61  SQLite..**.** Ra
ab2f0 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65  ndom numbers are
ab300 20 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66   used by some of
ab310 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61   the database ba
ab320 63 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a  ckends in order.
ab330 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72  ** to generate r
ab340 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65  andom integer ke
ab350 79 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72  ys for tables or
ab360 20 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65   random filename
ab370 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61  s..**.** $Id: ra
ab380 6e 64 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30  ndom.c,v 1.29 20
ab390 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32  08/12/10 19:26:2
ab3a0 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  4 drh Exp $.*/..
ab3b0 0a 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20  ./* All threads 
ab3c0 73 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72  share a single r
ab3d0 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
ab3e0 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20  erator..** This 
ab3f0 73 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65  structure is the
ab400 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f   current state o
ab410 66 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e  f the generator.
ab420 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
ab430 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c  E_WSD struct sql
ab440 69 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20  ite3PrngType {. 
ab450 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69   unsigned char i
ab460 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20  sInit;          
ab470 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69  /* True if initi
ab480 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69  alized */.  unsi
ab490 67 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20  gned char i, j; 
ab4a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
ab4b0 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f  ate variables */
ab4c0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
ab4d0 20 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20   s[256];        
ab4e0 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61    /* State varia
ab4f0 62 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65  bles */.} sqlite
ab500 33 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65  3Prng;../*.** Ge
ab510 74 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74  t a single 8-bit
ab520 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72   random value fr
ab530 6f 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e  om the RC4 PRNG.
ab540 20 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d    The Mutex.** m
ab550 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c  ust be held whil
ab560 65 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73  e executing this
ab570 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
ab580 57 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65  Why not just use
ab590 20 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f   a library rando
ab5a0 6d 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65  m generator like
ab5b0 20 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74   lrand48() for t
ab5c0 68 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20  his?.** Because 
ab5d0 74 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20  the OP_NewRowid 
ab5e0 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44  opcode in the VD
ab5f0 42 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61  BE depends on ha
ab600 76 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67  ving a very.** g
ab610 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61  ood source of ra
ab620 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54  ndom numbers.  T
ab630 68 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62  he lrand48() lib
ab640 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61  rary function ma
ab650 79 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f  y.** well be goo
ab660 64 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d  d enough.  But m
ab670 61 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61  aybe not.  Or ma
ab680 79 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61  ybe lrand48() ha
ab690 73 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65  s some.** subtle
ab6a0 20 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d   problems on som
ab6b0 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63  e systems that c
ab6c0 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c  ould cause probl
ab6d0 65 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64  ems.  It is hard
ab6e0 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f  .** to know.  To
ab6f0 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69   minimize the ri
ab700 73 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64  sk of problems d
ab710 75 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34  ue to bad lrand4
ab720 38 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  8().** implement
ab730 61 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75  ations, SQLite u
ab740 73 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20  ses this random 
ab750 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
ab760 20 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34   based.** on RC4
ab770 2c 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20  , which we know 
ab780 77 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e  works very well.
ab790 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20  .**.** (Later): 
ab7a0 20 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65   Actually, OP_Ne
ab7b0 77 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20  wRowid does not 
ab7c0 64 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64  depend on a good
ab7d0 20 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61   source of.** ra
ab7e0 6e 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72  ndomness any mor
ab7f0 65 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20  e.  But we will 
ab800 6c 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20  leave this code 
ab810 69 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e  in all the same.
ab820 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61  .*/.static u8 ra
ab830 6e 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a  ndomByte(void){.
ab840 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
ab850 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77  t;...  /* The "w
ab860 73 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69  sdPrng" macro wi
ab870 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68  ll resolve to th
ab880 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  e pseudo-random 
ab890 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
ab8a0 0a 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74  .  ** state vect
ab8b0 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65  or.  If writable
ab8c0 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20   static data is 
ab8d0 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74  unsupported on t
ab8e0 68 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20  he target,.  ** 
ab8f0 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74  we have to locat
ab900 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74  e the state vect
ab910 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20  or at run-time. 
ab920 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d   In the more com
ab930 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68  mon.  ** case wh
ab940 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61  ere writable sta
ab950 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70  tic data is supp
ab960 6f 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63  orted, wsdPrng c
ab970 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c  an refer directl
ab980 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73  y.  ** to the "s
ab990 71 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74  qlite3Prng" stat
ab9a0 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65  e vector declare
ab9b0 64 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69  d above..  */.#i
ab9c0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
ab9d0 5f 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71  _WSD.  struct sq
ab9e0 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70  lite3PrngType *p
ab9f0 20 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63   = &GLOBAL(struc
aba00 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70  t sqlite3PrngTyp
aba10 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b  e, sqlite3Prng);
aba20 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e  .# define wsdPrn
aba30 67 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64  g p[0].#else.# d
aba40 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71  efine wsdPrng sq
aba50 6c 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66  lite3Prng.#endif
aba60 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ...  /* Initiali
aba70 7a 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  ze the state of 
aba80 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  the random numbe
aba90 72 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65  r generator once
abaa0 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74  ,.  ** the first
abab0 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69   time this routi
abac0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54  ne is called.  T
abad0 68 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f  he seed value do
abae0 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64  es.  ** not need
abaf0 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f   to contain a lo
abb00 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20  t of randomness 
abb10 73 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74  since we are not
abb20 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20  .  ** trying to 
abb30 64 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70  do secure encryp
abb40 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67  tion or anything
abb50 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20   like that....  
abb60 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20  **.  ** Nothing 
abb70 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20  in this file or 
abb80 61 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e  anywhere else in
abb90 20 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79   SQLite does any
abba0 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e   kind of.  ** en
abbb0 63 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52  cryption.  The R
abbc0 43 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20  C4 algorithm is 
abbd0 62 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20  being used as a 
abbe0 50 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e  PRNG (pseudo-ran
abbf0 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20  dom.  ** number 
abc00 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61  generator) not a
abc10 73 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20  s an encryption 
abc20 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69  device..  */.  i
abc30 66 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e  f( !wsdPrng.isIn
abc40 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  it ){.    int i;
abc50 0a 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d  .    char k[256]
abc60 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20  ;.    wsdPrng.j 
abc70 3d 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67  = 0;.    wsdPrng
abc80 2e 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  .i = 0;.    sqli
abc90 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28  te3OsRandomness(
abca0 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64  sqlite3_vfs_find
abcb0 28 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20  (0), 256, k);.  
abcc0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36    for(i=0; i<256
abcd0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73  ; i++){.      ws
abce0 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38  dPrng.s[i] = (u8
abcf0 29 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  )i;.    }.    fo
abd00 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b  r(i=0; i<256; i+
abd10 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e  +){.      wsdPrn
abd20 67 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73  g.j += wsdPrng.s
abd30 5b 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20  [i] + k[i];.    
abd40 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b    t = wsdPrng.s[
abd50 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20  wsdPrng.j];.    
abd60 20 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50    wsdPrng.s[wsdP
abd70 72 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67  rng.j] = wsdPrng
abd80 2e 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64  .s[i];.      wsd
abd90 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20  Prng.s[i] = t;. 
abda0 20 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67     }.    wsdPrng
abdb0 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d  .isInit = 1;.  }
abdc0 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
abdd0 61 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c  and return singl
abde0 65 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20  e random byte.  
abdf0 2a 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b  */.  wsdPrng.i++
abe00 3b 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e  ;.  t = wsdPrng.
abe10 73 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20  s[wsdPrng.i];.  
abe20 77 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a  wsdPrng.j += t;.
abe30 20 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50    wsdPrng.s[wsdP
abe40 72 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67  rng.i] = wsdPrng
abe50 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20  .s[wsdPrng.j];. 
abe60 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
abe70 6e 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b  ng.j] = t;.  t +
abe80 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50  = wsdPrng.s[wsdP
abe90 72 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e  rng.i];.  return
abea0 20 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d   wsdPrng.s[t];.}
abeb0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e  ../*.** Return N
abec0 20 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a   random bytes..*
abed0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
abee0 64 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d  d sqlite3_random
abef0 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64  ness(int N, void
abf00 20 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67   *pBuf){.  unsig
abf10 6e 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d  ned char *zBuf =
abf20 20 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54   pBuf;.#if SQLIT
abf30 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73  E_THREADSAFE.  s
abf40 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
abf50 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
abf60 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
abf70 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47  UTEX_STATIC_PRNG
abf80 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  );.#endif.  sqli
abf90 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
abfa0 6d 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28  mutex);.  while(
abfb0 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42   N-- ){.    *(zB
abfc0 75 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79  uf++) = randomBy
abfd0 74 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  te();.  }.  sqli
abfe0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
abff0 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64  mutex);.}..#ifnd
ac000 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
ac010 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a  UILTIN_TEST./*.*
ac020 2a 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75  * For testing pu
ac030 72 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74  rposes, we somet
ac040 69 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65  imes want to pre
ac050 73 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20  serve the state 
ac060 6f 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72  of.** PRNG and r
ac070 65 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20  estore the PRNG 
ac080 74 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61  to its saved sta
ac090 74 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69  te at a later ti
ac0a0 6d 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73  me, or.** to res
ac0b0 65 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69  et the PRNG to i
ac0c0 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65  ts initial state
ac0d0 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
ac0e0 73 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20  s accomplish.** 
ac0f0 74 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a  those tasks..**.
ac100 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74  ** The sqlite3_t
ac110 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e  est_control() in
ac120 74 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68  terface calls th
ac130 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a  ese routines to.
ac140 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50  ** control the P
ac150 52 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  RNG..*/.static S
ac160 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
ac170 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
ac180 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e   sqlite3SavedPrn
ac190 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  g;.SQLITE_PRIVAT
ac1a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
ac1b0 6e 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64  ngSaveState(void
ac1c0 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20  ){.  memcpy(.   
ac1d0 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20   &GLOBAL(struct 
ac1e0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c  sqlite3PrngType,
ac1f0 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e   sqlite3SavedPrn
ac200 67 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28  g),.    &GLOBAL(
ac210 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72  struct sqlite3Pr
ac220 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50  ngType, sqlite3P
ac230 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66  rng),.    sizeof
ac240 28 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20  (sqlite3Prng).  
ac250 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
ac260 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
ac270 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65  PrngRestoreState
ac280 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79  (void){.  memcpy
ac290 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74  (.    &GLOBAL(st
ac2a0 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
ac2b0 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e  Type, sqlite3Prn
ac2c0 67 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28  g),.    &GLOBAL(
ac2d0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72  struct sqlite3Pr
ac2e0 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53  ngType, sqlite3S
ac2f0 61 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73  avedPrng),.    s
ac300 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e  izeof(sqlite3Prn
ac310 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45  g).  );.}.SQLITE
ac320 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
ac330 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74  lite3PrngResetSt
ac340 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f  ate(void){.  GLO
ac350 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74  BAL(struct sqlit
ac360 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69  e3PrngType, sqli
ac370 74 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20  te3Prng).isInit 
ac380 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  = 0;.}.#endif /*
ac390 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
ac3a0 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a  LTIN_TEST */../*
ac3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
ac3c0 64 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a  d of random.c **
ac3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
ac400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
ac410 67 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a  gin file utf.c *
ac420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
ac450 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31  .** 2004 April 1
ac460 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
ac470 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
ac480 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
ac490 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
ac4a0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
ac4b0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
ac4c0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
ac4d0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
ac4e0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
ac4f0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
ac500 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
ac510 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
ac520 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
ac530 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
ac540 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
ac550 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
ac560 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
ac570 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
ac580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac5c0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
ac5d0 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20  ntains routines 
ac5e0 75 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74  used to translat
ac5f0 65 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c  e between UTF-8,
ac600 20 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46   .** UTF-16, UTF
ac610 2d 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31  -16BE, and UTF-1
ac620 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  6LE..**.** $Id: 
ac630 75 74 66 2e 63 2c 76 20 31 2e 37 30 20 32 30 30  utf.c,v 1.70 200
ac640 38 2f 31 32 2f 31 30 20 32 32 3a 33 30 3a 32 35  8/12/10 22:30:25
ac650 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2a 0a   shane Exp $.**.
ac660 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d  ** Notes on UTF-
ac670 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d  8:.**.**   Byte-
ac680 30 20 20 20 20 42 79 74 65 2d 31 20 20 20 20 42  0    Byte-1    B
ac690 79 74 65 2d 32 20 20 20 20 42 79 74 65 2d 33 20  yte-2    Byte-3 
ac6a0 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78     Value.**  0xx
ac6b0 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20  xxxxx           
ac6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac6d0 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30        00000000 0
ac6e0 30 30 30 30 30 30 30 20 30 78 78 78 78 78 78 78  0000000 0xxxxxxx
ac6f0 0a 2a 2a 20 20 31 31 30 79 79 79 79 79 20 20 31  .**  110yyyyy  1
ac700 30 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20  0xxxxxx         
ac710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30                00
ac720 30 30 30 30 30 30 20 30 30 30 30 30 79 79 79 20  000000 00000yyy 
ac730 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31  yyxxxxxx.**  111
ac740 30 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20  0zzzz  10yyyyyy 
ac750 20 31 30 78 78 78 78 78 78 20 20 20 20 20 20 20   10xxxxxx       
ac760 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 7a        00000000 z
ac770 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78  zzzyyyy yyxxxxxx
ac780 0a 2a 2a 20 20 31 31 31 31 30 75 75 75 20 20 31  .**  11110uuu  1
ac790 30 75 75 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79  0uuzzzz  10yyyyy
ac7a0 79 20 20 31 30 78 78 78 78 78 78 20 20 20 30 30  y  10xxxxxx   00
ac7b0 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20  0uuuuu zzzzyyyy 
ac7c0 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a  yyxxxxxx.**.**.*
ac7d0 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31  * Notes on UTF-1
ac7e0 36 3a 20 20 28 77 69 74 68 20 77 77 77 77 2b 31  6:  (with wwww+1
ac7f0 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20  ==uuuuu).**.**  
ac800 20 20 20 20 57 6f 72 64 2d 30 20 20 20 20 20 20      Word-0      
ac810 20 20 20 20 20 20 20 20 20 57 6f 72 64 2d 31 20           Word-1 
ac820 20 20 20 20 20 20 20 20 20 56 61 6c 75 65 0a 2a           Value.*
ac830 2a 20 20 31 31 30 31 31 30 77 77 20 77 77 7a 7a  *  110110ww wwzz
ac840 7a 7a 79 79 20 20 20 31 31 30 31 31 31 79 79 20  zzyy   110111yy 
ac850 79 79 78 78 78 78 78 78 20 20 20 20 30 30 30 75  yyxxxxxx    000u
ac860 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79  uuuu zzzzyyyy yy
ac870 78 78 78 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79  xxxxxx.**  zzzzy
ac880 79 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20  yyy yyxxxxxx    
ac890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac8a0 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a      00000000 zzz
ac8b0 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a  zyyyy yyxxxxxx.*
ac8c0 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42  *.**.** BOM or B
ac8d0 79 74 65 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a  yte Order Mark:.
ac8e0 2a 2a 20 20 20 20 20 30 78 66 66 20 30 78 66 65  **     0xff 0xfe
ac8f0 20 20 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e     little-endian
ac900 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a   utf-16 follows.
ac910 2a 2a 20 20 20 20 20 30 78 66 65 20 30 78 66 66  **     0xfe 0xff
ac920 20 20 20 62 69 67 2d 65 6e 64 69 61 6e 20 75 74     big-endian ut
ac930 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a  f-16 follows.**.
ac940 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
ac950 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 49  ** Include vdbeI
ac960 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  nt.h in the midd
ac970 6c 65 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a  le of utf.c ****
ac980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac990 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
ac9a0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
ac9b0 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  beInt.h ********
ac9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac9e0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65  */./*.** 2003 Se
ac9f0 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20  ptember 6.**.** 
aca00 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
aca10 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
aca20 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
aca30 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
aca40 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
aca50 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
aca60 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
aca70 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
aca80 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
aca90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
acaa0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
acab0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
acac0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
acad0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
acae0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
acaf0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
acb00 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
acb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
acb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
acb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
acb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
acb50 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
acb60 20 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66   is the header f
acb70 69 6c 65 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74  ile for informat
acb80 69 6f 6e 20 74 68 61 74 20 69 73 20 70 72 69 76  ion that is priv
acb90 61 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44  ate to the.** VD
acba0 42 45 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d  BE.  This inform
acbb0 61 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c  ation used to al
acbc0 6c 20 62 65 20 61 74 20 74 68 65 20 74 6f 70 20  l be at the top 
acbd0 6f 66 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a  of the single.**
acbe0 20 73 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c   source code fil
acbf0 65 20 22 76 64 62 65 2e 63 22 2e 20 20 57 68 65  e "vdbe.c".  Whe
acc00 6e 20 74 68 61 74 20 66 69 6c 65 20 62 65 63 61  n that file beca
acc10 6d 65 20 74 6f 6f 20 62 69 67 20 28 6f 76 65 72  me too big (over
acc20 0a 2a 2a 20 36 30 30 30 20 6c 69 6e 65 73 20 6c  .** 6000 lines l
acc30 6f 6e 67 29 20 69 74 20 77 61 73 20 73 70 6c 69  ong) it was spli
acc40 74 20 75 70 20 69 6e 74 6f 20 73 65 76 65 72 61  t up into severa
acc50 6c 20 73 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20  l smaller files 
acc60 61 6e 64 0a 2a 2a 20 74 68 69 73 20 68 65 61 64  and.** this head
acc70 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77  er information w
acc80 61 73 20 66 61 63 74 6f 72 65 64 20 6f 75 74 2e  as factored out.
acc90 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
acca0 49 6e 74 2e 68 2c 76 20 31 2e 31 36 31 20 32 30  Int.h,v 1.161 20
accb0 30 39 2f 30 31 2f 30 35 20 31 38 3a 30 32 3a 32  09/01/05 18:02:2
accc0 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23  7 drh Exp $.*/.#
accd0 69 66 6e 64 65 66 20 5f 56 44 42 45 49 4e 54 5f  ifndef _VDBEINT_
acce0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 44 42 45  H_.#define _VDBE
accf0 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 69 6e  INT_H_../*.** in
acd00 74 54 6f 4b 65 79 28 29 20 61 6e 64 20 6b 65 79  tToKey() and key
acd10 54 6f 49 6e 74 28 29 20 75 73 65 64 20 74 6f 20  ToInt() used to 
acd20 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 72 6f  transform the ro
acd30 77 69 64 2e 20 20 42 75 74 20 77 69 74 68 0a 2a  wid.  But with.*
acd40 2a 20 74 68 65 20 6c 61 74 65 73 74 20 76 65 72  * the latest ver
acd50 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 65 73  sions of the des
acd60 69 67 6e 20 74 68 65 79 20 61 72 65 20 6e 6f 2d  ign they are no-
acd70 6f 70 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ops..*/.#define 
acd80 6b 65 79 54 6f 49 6e 74 28 58 29 20 20 20 28 58  keyToInt(X)   (X
acd90 29 0a 23 64 65 66 69 6e 65 20 69 6e 74 54 6f 4b  ).#define intToK
acda0 65 79 28 58 29 20 20 20 28 58 29 0a 0a 0a 2f 2a  ey(X)   (X).../*
acdb0 0a 2a 2a 20 53 51 4c 20 69 73 20 74 72 61 6e 73  .** SQL is trans
acdc0 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 73 65 71  lated into a seq
acdd0 75 65 6e 63 65 20 6f 66 20 69 6e 73 74 72 75 63  uence of instruc
acde0 74 69 6f 6e 73 20 74 6f 20 62 65 0a 2a 2a 20 65  tions to be.** e
acdf0 78 65 63 75 74 65 64 20 62 79 20 61 20 76 69 72  xecuted by a vir
ace00 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 45  tual machine.  E
ace10 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ach instruction 
ace20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a  is an instance.*
ace30 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
ace40 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ng structure..*/
ace50 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
ace60 56 64 62 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a  VdbeOp Op;../*.*
ace70 2a 20 42 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 73  * Boolean values
ace80 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69  .*/.typedef unsi
ace90 67 6e 65 64 20 63 68 61 72 20 42 6f 6f 6c 3b 0a  gned char Bool;.
acea0 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20  ./*.** A cursor 
aceb0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 69 6e 74  is a pointer int
acec0 6f 20 61 20 73 69 6e 67 6c 65 20 42 54 72 65 65  o a single BTree
aced0 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62 61   within a databa
acee0 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20  se file..** The 
acef0 63 75 72 73 6f 72 20 63 61 6e 20 73 65 65 6b 20  cursor can seek 
acf00 74 6f 20 61 20 42 54 72 65 65 20 65 6e 74 72 79  to a BTree entry
acf10 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c   with a particul
acf20 61 72 20 6b 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f  ar key, or.** lo
acf30 6f 70 20 6f 76 65 72 20 61 6c 6c 20 65 6e 74 72  op over all entr
acf40 69 65 73 20 6f 66 20 74 68 65 20 42 74 72 65 65  ies of the Btree
acf50 2e 20 20 59 6f 75 20 63 61 6e 20 61 6c 73 6f 20  .  You can also 
acf60 69 6e 73 65 72 74 20 6e 65 77 20 42 54 72 65 65  insert new BTree
acf70 0a 2a 2a 20 65 6e 74 72 69 65 73 20 6f 72 20 72  .** entries or r
acf80 65 74 72 69 65 76 65 20 74 68 65 20 6b 65 79 20  etrieve the key 
acf90 6f 72 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  or data from the
acfa0 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
acfb0 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 63 75 72  cursor.** is cur
acfc0 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
acfd0 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 45 76 65 72 79  to..** .** Every
acfe0 20 63 75 72 73 6f 72 20 74 68 61 74 20 74 68 65   cursor that the
acff0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
ad000 20 68 61 73 20 6f 70 65 6e 20 69 73 20 72 65 70   has open is rep
ad010 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e 0a 2a  resented by an.*
ad020 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
ad030 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
ad040 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  cture..**.** If 
ad050 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69  the VdbeCursor.i
ad060 73 54 72 69 67 67 65 72 52 6f 77 20 66 6c 61 67  sTriggerRow flag
ad070 20 69 73 20 73 65 74 20 69 74 20 6d 65 61 6e 73   is set it means
ad080 20 74 68 61 74 20 74 68 69 73 20 63 75 72 73 6f   that this curso
ad090 72 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61  r is.** really a
ad0a0 20 73 69 6e 67 6c 65 20 72 6f 77 20 74 68 61 74   single row that
ad0b0 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20   represents the 
ad0c0 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64  NEW or OLD pseud
ad0d0 6f 2d 74 61 62 6c 65 20 6f 66 0a 2a 2a 20 61 20  o-table of.** a 
ad0e0 72 6f 77 20 74 72 69 67 67 65 72 2e 20 20 54 68  row trigger.  Th
ad0f0 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72  e data for the r
ad100 6f 77 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ow is stored in 
ad110 56 64 62 65 43 75 72 73 6f 72 2e 70 44 61 74 61  VdbeCursor.pData
ad120 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72 6f 77 69   and.** the rowi
ad130 64 20 69 73 20 69 6e 20 56 64 62 65 43 75 72 73  d is in VdbeCurs
ad140 6f 72 2e 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75  or.iKey..*/.stru
ad150 63 74 20 56 64 62 65 43 75 72 73 6f 72 20 7b 0a  ct VdbeCursor {.
ad160 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
ad170 73 6f 72 3b 20 20 20 20 2f 2a 20 54 68 65 20 63  sor;    /* The c
ad180 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 20  ursor structure 
ad190 6f 66 20 74 68 65 20 62 61 63 6b 65 6e 64 20 2a  of the backend *
ad1a0 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
ad1b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
ad1c0 65 78 20 6f 66 20 63 75 72 73 6f 72 20 64 61 74  ex of cursor dat
ad1d0 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62  abase in db->aDb
ad1e0 5b 5d 20 28 6f 72 20 2d 31 29 20 2a 2f 0a 20 20  [] (or -1) */.  
ad1f0 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20  i64 lastRowid;  
ad200 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 6f        /* Last ro
ad210 77 69 64 20 66 72 6f 6d 20 61 20 4e 65 78 74 20  wid from a Next 
ad220 6f 72 20 4e 65 78 74 49 64 78 20 6f 70 65 72 61  or NextIdx opera
ad230 74 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 65  tion */.  i64 ne
ad240 78 74 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20  xtRowid;        
ad250 2f 2a 20 4e 65 78 74 20 72 6f 77 69 64 20 72 65  /* Next rowid re
ad260 74 75 72 6e 65 64 20 62 79 20 4f 50 5f 4e 65 77  turned by OP_New
ad270 52 6f 77 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20  Rowid */.  Bool 
ad280 7a 65 72 6f 65 64 3b 20 20 20 20 20 20 20 20 20  zeroed;         
ad290 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 72 6f   /* True if zero
ad2a0 65 64 20 6f 75 74 20 61 6e 64 20 72 65 61 64 79  ed out and ready
ad2b0 20 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a 20 20   for reuse */.  
ad2c0 42 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61 6c 69  Bool rowidIsVali
ad2d0 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  d;    /* True if
ad2e0 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20 76 61   lastRowid is va
ad2f0 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74  lid */.  Bool at
ad300 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 2f  First;         /
ad310 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 69  * True if pointi
ad320 6e 67 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72  ng to first entr
ad330 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73 65 52  y */.  Bool useR
ad340 61 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f 2a 20  andomRowid;  /* 
ad350 47 65 6e 65 72 61 74 65 20 6e 65 77 20 72 65 63  Generate new rec
ad360 6f 72 64 20 6e 75 6d 62 65 72 73 20 73 65 6d 69  ord numbers semi
ad370 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42  -randomly */.  B
ad380 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20 20 20  ool nullRow;    
ad390 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
ad3a0 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f  pointing to a ro
ad3b0 77 20 77 69 74 68 20 6e 6f 20 64 61 74 61 20 2a  w with no data *
ad3c0 2f 0a 20 20 42 6f 6f 6c 20 6e 65 78 74 52 6f 77  /.  Bool nextRow
ad3d0 69 64 56 61 6c 69 64 3b 20 20 2f 2a 20 54 72 75  idValid;  /* Tru
ad3e0 65 20 69 66 20 74 68 65 20 6e 65 78 74 52 6f 77  e if the nextRow
ad3f0 69 64 20 66 69 65 6c 64 20 69 73 20 76 61 6c 69  id field is vali
ad400 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 70 73 65 75  d */.  Bool pseu
ad410 64 6f 54 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20  doTable;     /* 
ad420 54 68 69 73 20 69 73 20 61 20 4e 45 57 20 6f 72  This is a NEW or
ad430 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c   OLD pseudo-tabl
ad440 65 73 20 6f 66 20 61 20 74 72 69 67 67 65 72 20  es of a trigger 
ad450 2a 2f 0a 20 20 42 6f 6f 6c 20 65 70 68 65 6d 50  */.  Bool ephemP
ad460 73 65 75 64 6f 54 61 62 6c 65 3b 0a 20 20 42 6f  seudoTable;.  Bo
ad470 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 65 74  ol deferredMovet
ad480 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 74 6f  o;  /* A call to
ad490 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
ad4a0 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 64 20  eto() is needed 
ad4b0 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 62 6c  */.  Bool isTabl
ad4c0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  e;         /* Tr
ad4d0 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 72 65  ue if a table re
ad4e0 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 72 20  quiring integer 
ad4f0 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69  keys */.  Bool i
ad500 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20  sIndex;         
ad510 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 69 6e  /* True if an in
ad520 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6b  dex containing k
ad530 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61  eys only - no da
ad540 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f 76 65  ta */.  i64 move
ad550 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 2f 2a  toTarget;     /*
ad560 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   Argument to the
ad570 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 74 65   deferred sqlite
ad580 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 2a  3BtreeMoveto() *
ad590 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20  /.  Btree *pBt; 
ad5a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 70            /* Sep
ad5b0 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c 64 69  arate file holdi
ad5c0 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ng temporary tab
ad5d0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74  le */.  int nDat
ad5e0 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  a;            /*
ad5f0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
ad600 20 69 6e 20 70 44 61 74 61 20 2a 2f 0a 20 20 63   in pData */.  c
ad610 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20 20  har *pData;     
ad620 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72       /* Data for
ad630 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73   a NEW or OLD ps
ad640 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20  eudo-table */.  
ad650 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20  i64 iKey;       
ad660 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 66 6f 72        /* Key for
ad670 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20   the NEW or OLD 
ad680 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 72 6f 77  pseudo-table row
ad690 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
ad6a0 4b 65 79 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49  KeyInfo;    /* I
ad6b0 6e 66 6f 20 61 62 6f 75 74 20 69 6e 64 65 78 20  nfo about index 
ad6c0 6b 65 79 73 20 6e 65 65 64 65 64 20 62 79 20 69  keys needed by i
ad6d0 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a  ndex cursors */.
ad6e0 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20    int nField;   
ad6f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ad700 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
ad710 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69  he header */.  i
ad720 36 34 20 73 65 71 43 6f 75 6e 74 3b 20 20 20 20  64 seqCount;    
ad730 20 20 20 20 20 2f 2a 20 53 65 71 75 65 6e 63 65       /* Sequence
ad740 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71   counter */.  sq
ad750 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
ad760 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 20  r *pVtabCursor; 
ad770 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 66   /* The cursor f
ad780 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  or a virtual tab
ad790 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71  le */.  const sq
ad7a0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
ad7b0 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f  odule;     /* Mo
ad7c0 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f 72 20  dule for cursor 
ad7d0 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a  pVtabCursor */..
ad7e0 20 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 6f    /* Cached info
ad7f0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
ad800 65 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 65  e header for the
ad810 20 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 61   data record tha
ad820 74 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f  t the.  ** curso
ad830 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  r is currently p
ad840 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c  ointing to.  Onl
ad850 79 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 65  y valid if cache
ad860 56 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a 20  Valid is true.. 
ad870 20 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70   ** aRow might p
ad880 6f 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72  oint to (ephemer
ad890 61 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65  al) data for the
ad8a0 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72   current row, or
ad8b0 20 69 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62   it might.  ** b
ad8c0 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69  e NULL..  */.  i
ad8d0 6e 74 20 63 61 63 68 65 53 74 61 74 75 73 3b 20  nt cacheStatus; 
ad8e0 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73       /* Cache is
ad8f0 20 76 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d   valid if this m
ad900 61 74 63 68 65 73 20 56 64 62 65 2e 63 61 63 68  atches Vdbe.cach
ad910 65 43 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61  eCtr */.  int pa
ad920 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20  yloadSize;      
ad930 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  /* Total number 
ad940 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
ad950 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20  record */.  u32 
ad960 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 20  *aType;         
ad970 20 20 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73    /* Type values
ad980 20 66 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73   for all entries
ad990 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a   in the record *
ad9a0 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74  /.  u32 *aOffset
ad9b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63  ;         /* Cac
ad9c0 68 65 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74  hed offsets to t
ad9d0 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68  he start of each
ad9e0 20 63 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f   columns data */
ad9f0 0a 20 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20  .  u8 *aRow;    
ada00 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
ada10 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
ada20 20 72 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20   row, if all on 
ada30 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74  one page */.};.t
ada40 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64  ypedef struct Vd
ada50 62 65 43 75 72 73 6f 72 20 56 64 62 65 43 75 72  beCursor VdbeCur
ada60 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61  sor;../*.** A va
ada70 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75 72 73  lue for VdbeCurs
ada80 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74 68  or.cacheValid th
ada90 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61 63  at means the cac
adaa0 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 76  he is always inv
adab0 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  alid..*/.#define
adac0 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a 0a   CACHE_STALE 0..
adad0 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c 79  /*.** Internally
adae0 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69 70  , the vdbe manip
adaf0 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61 6c  ulates nearly al
adb00 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73 20  l SQL values as 
adb10 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  Mem.** structure
adb20 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72 75  s. Each Mem stru
adb30 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75 6c  ct may cache mul
adb40 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74 61  tiple representa
adb50 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a 2a  tions (string,.*
adb60 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29 20  * integer etc.) 
adb70 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  of the same valu
adb80 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61 6e 64  e.  A value (and
adb90 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d 20 73   therefore Mem s
adba0 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68 61 73  tructure).** has
adbb0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70   the following p
adbc0 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a  roperties:.**.**
adbd0 20 45 61 63 68 20 76 61 6c 75 65 20 68 61 73 20   Each value has 
adbe0 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 2e  a manifest type.
adbf0 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20 74 79   The manifest ty
adc00 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20  pe of the value 
adc10 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 20 4d  stored.** in a M
adc20 65 6d 20 73 74 72 75 63 74 20 69 73 20 72 65 74  em struct is ret
adc30 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d 65 6d  urned by the Mem
adc40 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63 72 6f  Type(Mem*) macro
adc50 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a 2a 2a  . The type is.**
adc60 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e   one of SQLITE_N
adc70 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45  ULL, SQLITE_INTE
adc80 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45 41 4c  GER, SQLITE_REAL
adc90 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20 6f 72  , SQLITE_TEXT or
adca0 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f 42 2e  .** SQLITE_BLOB.
adcb0 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 20 7b  .*/.struct Mem {
adcc0 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 69  .  union {.    i
adcd0 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  64 i;           
adce0 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
adcf0 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  lue. */.    int 
add00 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20  nZero;          
add10 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 62 69 74  /* Used when bit
add20 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 20 73 65 74   MEM_Zero is set
add30 20 69 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20   in flags */.   
add40 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20   FuncDef *pDef; 
add50 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c       /* Used onl
add60 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45  y when flags==ME
add70 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 52 6f 77  M_Agg */.    Row
add80 53 65 74 20 2a 70 52 6f 77 53 65 74 3b 20 20 20  Set *pRowSet;   
add90 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68   /* Used only wh
adda0 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 52 6f  en flags==MEM_Ro
addb0 77 53 65 74 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20  wSet */.  } u;. 
addc0 20 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20   double r;      
addd0 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c       /* Real val
adde0 75 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ue */.  sqlite3 
addf0 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  *db;        /* T
ade00 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61  he associated da
ade10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
ade20 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20  n */.  char *z; 
ade30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
ade40 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c  ring or BLOB val
ade50 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20  ue */.  int n;  
ade60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ade70 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
ade80 65 72 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61  ers in string va
ade90 6c 75 65 2c 20 65 78 63 6c 75 64 69 6e 67 20 27  lue, excluding '
adea0 5c 30 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  \0' */.  u16 fla
adeb0 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  gs;          /* 
adec0 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  Some combination
aded0 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45   of MEM_Null, ME
adee0 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20  M_Str, MEM_Dyn, 
adef0 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79  etc. */.  u8  ty
adf00 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  pe;           /*
adf10 20 4f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e   One of SQLITE_N
adf20 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54  ULL, SQLITE_TEXT
adf30 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  , SQLITE_INTEGER
adf40 2c 20 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65  , etc */.  u8  e
adf50 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nc;            /
adf60 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  * SQLITE_UTF8, S
adf70 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53  QLITE_UTF16BE, S
adf80 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f  QLITE_UTF16LE */
adf90 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
adfa0 76 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20  void *);  /* If 
adfb0 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74  not null, call t
adfc0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  his function to 
adfd0 64 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a  delete Mem.z */.
adfe0 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b    char *zMalloc;
adff0 20 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63        /* Dynamic
ae000 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65   buffer allocate
ae010 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c  d by sqlite3_mal
ae020 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  loc() */.};../* 
ae030 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  One or more of t
ae040 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61  he following fla
ae050 67 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e  gs are set to in
ae060 64 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64  dicate the valid
ae070 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61  OK.** representa
ae080 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c  tions of the val
ae090 75 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ue stored in the
ae0a0 20 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a   Mem struct..**.
ae0b0 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75  ** If the MEM_Nu
ae0c0 6c 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  ll flag is set, 
ae0d0 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69  then the value i
ae0e0 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61  s an SQL NULL va
ae0f0 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72  lue..** No other
ae100 20 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65   flags may be se
ae110 74 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  t in this case..
ae120 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d  **.** If the MEM
ae130 5f 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74  _Str flag is set
ae140 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e   then Mem.z poin
ae150 74 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72  ts at a string r
ae160 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a  epresentation..*
ae170 2a 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69  * Usually this i
ae180 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65  s encoded in the
ae190 20 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e   same unicode en
ae1a0 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61  coding as the ma
ae1b0 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28  in.** database (
ae1c0 73 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78  see below for ex
ae1d0 63 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68  ceptions). If th
ae1e0 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20  e MEM_Term flag 
ae1f0 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20  is also.** set, 
ae200 74 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20  then the string 
ae210 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65  is nul terminate
ae220 64 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61  d. The MEM_Int a
ae230 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20  nd MEM_Real .** 
ae240 66 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73  flags may coexis
ae250 74 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53  t with the MEM_S
ae260 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d  tr flag..**.** M
ae270 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65  ultiple of these
ae280 20 76 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65   values can appe
ae290 61 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e  ar in Mem.flags.
ae2a0 20 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a    But only one.*
ae2b0 2a 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20  * at a time can 
ae2c0 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79  appear in Mem.ty
ae2d0 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  pe..*/.#define M
ae2e0 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30  EM_Null      0x0
ae2f0 30 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69  001   /* Value i
ae300 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e  s NULL */.#defin
ae310 65 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20  e MEM_Str       
ae320 30 78 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75  0x0002   /* Valu
ae330 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f  e is a string */
ae340 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74  .#define MEM_Int
ae350 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 20         0x0004   
ae360 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69  /* Value is an i
ae370 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e  nteger */.#defin
ae380 65 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20  e MEM_Real      
ae390 30 78 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75  0x0008   /* Valu
ae3a0 65 20 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62  e is a real numb
ae3b0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  er */.#define ME
ae3c0 4d 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30  M_Blob      0x00
ae3d0 31 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73  10   /* Value is
ae3e0 20 61 20 42 4c 4f 42 20 2a 2f 0a 23 64 65 66 69   a BLOB */.#defi
ae3f0 6e 65 20 4d 45 4d 5f 52 6f 77 53 65 74 20 20 20  ne MEM_RowSet   
ae400 20 30 78 30 30 32 30 20 20 20 2f 2a 20 56 61 6c   0x0020   /* Val
ae410 75 65 20 69 73 20 61 20 52 6f 77 53 65 74 20 6f  ue is a RowSet o
ae420 62 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65  bject */.#define
ae430 20 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 20 20 30   MEM_TypeMask  0
ae440 78 30 30 66 66 20 20 20 2f 2a 20 4d 61 73 6b 20  x00ff   /* Mask 
ae450 6f 66 20 74 79 70 65 20 62 69 74 73 20 2a 2f 0a  of type bits */.
ae460 0a 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65 6d  ./* Whenever Mem
ae470 20 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 69   contains a vali
ae480 64 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62  d string or blob
ae490 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c   representation,
ae4a0 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66   one of.** the f
ae4b0 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 6d  ollowing flags m
ae4c0 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 64 65  ust be set to de
ae4d0 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d 6f  termine the memo
ae4e0 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a  ry management.**
ae4f0 20 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d 2e   policy for Mem.
ae500 7a 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 6d  z.  The MEM_Term
ae510 20 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 77   flag tells us w
ae520 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
ae530 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 5c  e.** string is \
ae540 30 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 65  000 or \u0000 te
ae550 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65 66  rminated.*/.#def
ae560 69 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20 20  ine MEM_Term    
ae570 20 20 30 78 30 32 30 30 20 20 20 2f 2a 20 53 74    0x0200   /* St
ae580 72 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c 20  ring rep is nul 
ae590 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23 64  terminated */.#d
ae5a0 65 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20 20  efine MEM_Dyn   
ae5b0 20 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20      0x0400   /* 
ae5c0 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c  Need to call sql
ae5d0 69 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65 6d  iteFree() on Mem
ae5e0 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  .z */.#define ME
ae5f0 4d 5f 53 74 61 74 69 63 20 20 20 20 30 78 30 38  M_Static    0x08
ae600 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f  00   /* Mem.z po
ae610 69 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69 63  ints to a static
ae620 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69   string */.#defi
ae630 6e 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20 20  ne MEM_Ephem    
ae640 20 30 78 31 30 30 30 20 20 20 2f 2a 20 4d 65 6d   0x1000   /* Mem
ae650 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20  .z points to an 
ae660 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67  ephemeral string
ae670 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
ae680 41 67 67 20 20 20 20 20 20 20 30 78 32 30 30 30  Agg       0x2000
ae690 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e     /* Mem.z poin
ae6a0 74 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75 6e  ts to an agg fun
ae6b0 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f  ction context */
ae6c0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72  .#define MEM_Zer
ae6d0 6f 20 20 20 20 20 20 30 78 34 30 30 30 20 20 20  o      0x4000   
ae6e0 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69 6e  /* Mem.i contain
ae6f0 73 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61 70  s count of 0s ap
ae700 70 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20 2a  pended to blob *
ae710 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
ae720 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
ae730 20 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72 6f   #undef MEM_Zero
ae740 0a 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a  .  #define MEM_Z
ae750 65 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64 69  ero 0x0000.#endi
ae760 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  f.../*.** Clear 
ae770 61 6e 79 20 65 78 69 73 74 69 6e 67 20 74 79 70  any existing typ
ae780 65 20 66 6c 61 67 73 20 66 72 6f 6d 20 61 20 4d  e flags from a M
ae790 65 6d 20 61 6e 64 20 72 65 70 6c 61 63 65 20 74  em and replace t
ae7a0 68 65 6d 20 77 69 74 68 20 66 0a 2a 2f 0a 23 64  hem with f.*/.#d
ae7b0 65 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65  efine MemSetType
ae7c0 46 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 20  Flag(p, f) \.   
ae7d0 28 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28  ((p)->flags = ((
ae7e0 70 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f  p)->flags&~(MEM_
ae7f0 54 79 70 65 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72  TypeMask|MEM_Zer
ae800 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20 41 20 56 64  o))|f).../* A Vd
ae810 62 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61  beFunc is just a
ae820 20 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65   FuncDef (define
ae830 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  d in sqliteInt.h
ae840 29 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a  ) that contains.
ae850 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  ** additional in
ae860 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
ae870 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d  auxiliary inform
ae880 61 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61  ation bound to a
ae890 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74  rguments.** of t
ae8a0 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  he function.  Th
ae8b0 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d  is is used to im
ae8c0 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69  plement the sqli
ae8d0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
ae8e0 29 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  ).** and sqlite3
ae8f0 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41  _set_auxdata() A
ae900 50 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61  PIs.  The "auxda
ae910 74 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69  ta" is some auxi
ae920 6c 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68  liary data.** th
ae930 61 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69  at can be associ
ae940 61 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73  ated with a cons
ae950 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  tant argument to
ae960 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68   a function.  Th
ae970 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e  is.** allows fun
ae980 63 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22  ctions such as "
ae990 72 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69  regexp" to compi
ae9a0 6c 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e  le their constan
ae9b0 74 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70  t regular.** exp
ae9c0 72 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74  ression argument
ae9d0 20 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64   once and reused
ae9e0 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f   the compiled co
ae9f0 64 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a  de for multiple.
aea00 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a  ** invocations..
aea10 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75  */.struct VdbeFu
aea20 6e 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  nc {.  FuncDef *
aea30 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20  pFunc;          
aea40 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69       /* The defi
aea50 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75  nition of the fu
aea60 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nction */.  int 
aea70 6e 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20  nAux;           
aea80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
aea90 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61  ber of entries a
aeaa0 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41  llocated for apA
aeab0 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74  ux[] */.  struct
aeac0 20 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76   AuxData {.    v
aead0 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20  oid *pAux;      
aeae0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
aeaf0 41 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65  Aux data for the
aeb00 20 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a   i-th argument *
aeb10 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65  /.    void (*xDe
aeb20 6c 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20  lete)(void *);  
aeb30 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f      /* Destructo
aeb40 72 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61  r for the aux da
aeb50 74 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b  ta */.  } apAux[
aeb60 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  1];             
aeb70 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f        /* One slo
aeb80 74 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74  t for each funct
aeb90 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  ion argument */.
aeba0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63  };../*.** The "c
aebb0 6f 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74  ontext" argument
aebc0 20 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62   for a installab
aebd0 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20  le function.  A 
aebe0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a  pointer to an.**
aebf0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
aec00 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74  s structure is t
aec10 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
aec20 74 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65  t to the routine
aec30 73 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d  s used.** implem
aec40 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63  ent the SQL func
aec50 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tions..**.** The
aec60 72 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20  re is a typedef 
aec70 66 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75  for this structu
aec80 72 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20  re in sqlite.h. 
aec90 20 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73   So all routines
aeca0 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75  ,.** even the pu
aecb0 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74  blic interface t
aecc0 6f 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73  o SQLite, can us
aecd0 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
aece0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
aecf0 2a 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20  * But this file 
aed00 69 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63  is the only plac
aed10 65 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65  e where the inte
aed20 72 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20  rnal details of 
aed30 74 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72  this.** structur
aed40 65 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a  e are known..**.
aed50 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
aed60 65 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73  e is defined ins
aed70 69 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68  ide of vdbeInt.h
aed80 20 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73   because it uses
aed90 20 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a   substructures.*
aeda0 2a 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72  * (Mem) which ar
aedb0 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74  e only defined t
aedc0 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  here..*/.struct 
aedd0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
aede0 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75  {.  FuncDef *pFu
aedf0 6e 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  nc;       /* Poi
aee00 6e 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e  nter to function
aee10 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d   information.  M
aee20 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a  UST BE FIRST */.
aee30 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62    VdbeFunc *pVdb
aee40 65 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c  eFunc;  /* Auxil
aee50 61 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65  ary data, if cre
aee60 61 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73  ated. */.  Mem s
aee70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
aee80 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76   /* The return v
aee90 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68  alue is stored h
aeea0 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ere */.  Mem *pM
aeeb0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
aeec0 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73  * Memory cell us
aeed0 65 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72  ed to store aggr
aeee0 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f  egate context */
aeef0 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b 20  .  int isError; 
aef00 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
aef10 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20  r code returned 
aef20 62 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  by the function.
aef30 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70   */.  CollSeq *p
aef40 43 6f 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43  Coll;       /* C
aef50 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
aef60 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  e */.};../*.** A
aef70 20 53 65 74 20 73 74 72 75 63 74 75 72 65 20 69   Set structure i
aef80 73 20 75 73 65 64 20 66 6f 72 20 71 75 69 63 6b  s used for quick
aef90 20 74 65 73 74 69 6e 67 20 74 6f 20 73 65 65 20   testing to see 
aefa0 69 66 20 61 20 76 61 6c 75 65 0a 2a 2a 20 69 73  if a value.** is
aefb0 20 70 61 72 74 20 6f 66 20 61 20 73 6d 61 6c 6c   part of a small
aefc0 20 73 65 74 2e 20 20 53 65 74 73 20 61 72 65 20   set.  Sets are 
aefd0 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
aefe0 74 20 63 6f 64 65 20 6c 69 6b 65 0a 2a 2a 20 74  t code like.** t
aeff0 68 69 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20  his:.**         
af000 20 20 20 78 2e 79 20 49 4e 20 28 27 68 69 27 2c     x.y IN ('hi',
af010 27 68 6f 6f 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a  'hoo','hum').*/.
af020 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
af030 65 74 20 53 65 74 3b 0a 73 74 72 75 63 74 20 53  et Set;.struct S
af040 65 74 20 7b 0a 20 20 48 61 73 68 20 68 61 73 68  et {.  Hash hash
af050 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
af060 20 41 20 73 65 74 20 69 73 20 6a 75 73 74 20 61   A set is just a
af070 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
af080 20 48 61 73 68 45 6c 65 6d 20 2a 70 72 65 76 3b   HashElem *prev;
af090 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69          /* Previ
af0a0 6f 75 73 6c 79 20 61 63 63 65 73 73 65 64 20 68  ously accessed h
af0b0 61 73 68 20 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b  ash elemen */.};
af0c0 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f 6e 74 65 78  ../*.** A Contex
af0d0 74 20 73 74 6f 72 65 73 20 74 68 65 20 6c 61 73  t stores the las
af0e0 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 2c 20  t insert rowid, 
af0f0 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65  the last stateme
af100 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c  nt change count,
af110 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 75 72 72  .** and the curr
af120 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68  ent statement ch
af130 61 6e 67 65 20 63 6f 75 6e 74 20 28 69 2e 65 2e  ange count (i.e.
af140 20 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20 6c   changes since l
af150 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 2e 0a  ast statement)..
af160 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 6b  ** The current k
af170 65 79 6c 69 73 74 20 69 73 20 61 6c 73 6f 20 73  eylist is also s
af180 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e  tored in the con
af190 74 65 78 74 2e 0a 2a 2a 20 45 6c 65 6d 65 6e 74  text..** Element
af1a0 73 20 6f 66 20 43 6f 6e 74 65 78 74 20 73 74 72  s of Context str
af1b0 75 63 74 75 72 65 20 74 79 70 65 20 6d 61 6b 65  ucture type make
af1c0 20 75 70 20 74 68 65 20 43 6f 6e 74 65 78 74 53   up the ContextS
af1d0 74 61 63 6b 2c 20 77 68 69 63 68 20 69 73 0a 2a  tack, which is.*
af1e0 2a 20 75 70 64 61 74 65 64 20 62 79 20 74 68 65  * updated by the
af1f0 20 43 6f 6e 74 65 78 74 50 75 73 68 20 61 6e 64   ContextPush and
af200 20 43 6f 6e 74 65 78 74 50 6f 70 20 6f 70 63 6f   ContextPop opco
af210 64 65 73 20 28 75 73 65 64 20 62 79 20 74 72 69  des (used by tri
af220 67 67 65 72 73 29 2e 0a 2a 2a 20 54 68 65 20 63  ggers)..** The c
af230 6f 6e 74 65 78 74 20 69 73 20 70 75 73 68 65 64  ontext is pushed
af240 20 62 65 66 6f 72 65 20 65 78 65 63 75 74 69 6e   before executin
af250 67 20 61 20 74 72 69 67 67 65 72 20 61 20 70 6f  g a trigger a po
af260 70 70 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a  pped when the.**
af270 20 74 72 69 67 67 65 72 20 66 69 6e 69 73 68 65   trigger finishe
af280 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
af290 72 75 63 74 20 43 6f 6e 74 65 78 74 20 43 6f 6e  ruct Context Con
af2a0 74 65 78 74 3b 0a 73 74 72 75 63 74 20 43 6f 6e  text;.struct Con
af2b0 74 65 78 74 20 7b 0a 20 20 69 36 34 20 6c 61 73  text {.  i64 las
af2c0 74 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 4c 61  tRowid;    /* La
af2d0 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20  st insert rowid 
af2e0 28 73 71 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77  (sqlite3.lastRow
af2f0 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  id) */.  int nCh
af300 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a 20 53 74  ange;      /* St
af310 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 73 20  atement changes 
af320 28 56 64 62 65 2e 6e 43 68 61 6e 67 65 73 29 20  (Vdbe.nChanges) 
af330 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a      */.};../*.**
af340 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
af350 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
af360 69 6e 65 2e 20 20 54 68 69 73 20 73 74 72 75 63  ine.  This struc
af370 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  ture contains th
af380 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74  e complete.** st
af390 61 74 65 20 6f 66 20 74 68 65 20 76 69 72 74 75  ate of the virtu
af3a0 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a  al machine..**.*
af3b0 2a 20 54 68 65 20 22 73 71 6c 69 74 65 33 5f 73  * The "sqlite3_s
af3c0 74 6d 74 22 20 73 74 72 75 63 74 75 72 65 20 70  tmt" structure p
af3d0 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 72  ointer that is r
af3e0 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
af3f0 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20  e3_compile().** 
af400 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e  is really a poin
af410 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e  ter to an instan
af420 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
af430 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ture..**.** The 
af440 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f  Vdbe.inVtabMetho
af450 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  d variable is se
af460 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f  t to non-zero fo
af470 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f  r the duration o
af480 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 61 6c  f.** any virtual
af490 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e   table method in
af4a0 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 20 62  vocations made b
af4b0 79 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72  y the vdbe progr
af4c0 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 65 74  am. It is.** set
af4d0 20 74 6f 20 32 20 66 6f 72 20 78 44 65 73 74 72   to 2 for xDestr
af4e0 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20  oy method calls 
af4f0 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74  and 1 for all ot
af500 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 68 69  her methods. Thi
af510 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73  s.** variable is
af520 20 75 73 65 64 20 66 6f 72 20 74 77 6f 20 70 75   used for two pu
af530 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77  rposes: to allow
af540 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64   xDestroy method
af550 73 20 74 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20  s to execute.** 
af560 22 44 52 4f 50 20 54 41 42 4c 45 22 20 73 74 61  "DROP TABLE" sta
af570 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f 20 70  tements and to p
af580 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74  revent some nast
af590 79 20 73 69 64 65 20 65 66 66 65 63 74 73 20 6f  y side effects o
af5a0 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  f.** malloc fail
af5b0 75 72 65 20 77 68 65 6e 20 53 51 4c 69 74 65 20  ure when SQLite 
af5c0 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 75 72  is invoked recur
af5d0 73 69 76 65 6c 79 20 62 79 20 61 20 76 69 72 74  sively by a virt
af5e0 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65  ual table .** me
af5f0 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  thod function..*
af600 2f 0a 73 74 72 75 63 74 20 56 64 62 65 20 7b 0a  /.struct Vdbe {.
af610 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
af620 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f        /* The who
af630 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  le database */. 
af640 20 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e   Vdbe *pPrev,*pN
af650 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c  ext; /* Linked l
af660 69 73 74 20 6f 66 20 56 44 42 45 73 20 77 69 74  ist of VDBEs wit
af670 68 20 74 68 65 20 73 61 6d 65 20 56 64 62 65 2e  h the same Vdbe.
af680 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b  db */.  int nOp;
af690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
af6a0 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63  umber of instruc
af6b0 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72 6f  tions in the pro
af6c0 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  gram */.  int nO
af6d0 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f 2a  pAlloc;       /*
af6e0 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73   Number of slots
af6f0 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61   allocated for a
af700 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f  Op[] */.  Op *aO
af710 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p;            /*
af720 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
af730 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
af740 6e 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  ne's program */.
af750 20 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20    int nLabel;   
af760 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
af770 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64 20 2a  of labels used *
af780 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 41 6c  /.  int nLabelAl
af790 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65  loc;    /* Numbe
af7a0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
af7b0 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b 5d  ated in aLabel[]
af7c0 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65   */.  int *aLabe
af7d0 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61  l;        /* Spa
af7e0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6c  ce to hold the l
af7f0 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a  abels */.  Mem *
af800 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20 2f  *apArg;        /
af810 2a 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63  * Arguments to c
af820 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69  urrently executi
af830 6e 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  ng user function
af840 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e   */.  Mem *aColN
af850 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6c  ame;      /* Col
af860 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65 74  umn names to ret
af870 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75  urn */.  int nCu
af880 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  rsor;        /* 
af890 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20  Number of slots 
af8a0 69 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 20  in apCsr[] */.  
af8b0 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43  VdbeCursor **apC
af8c0 73 72 3b 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d 65  sr; /* One eleme
af8d0 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72 61 79  nt of this array
af8e0 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 63   for each open c
af8f0 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  ursor */.  int n
af900 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Var;           /
af910 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
af920 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a 2f  ies in aVar[] */
af930 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20 20  .  Mem *aVar;   
af940 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73         /* Values
af950 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69   for the OP_Vari
af960 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a  able opcode. */.
af970 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20    char **azVar; 
af980 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
af990 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20   variables */.  
af9a0 69 6e 74 20 6f 6b 56 61 72 3b 20 20 20 20 20 20  int okVar;      
af9b0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
af9c0 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e 20  zVar[] has been 
af9d0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
af9e0 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20   u32 magic;     
af9f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69           /* Magi
afa00 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73 61 6e  c number for san
afa10 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a  ity checking */.
afa20 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20    int nMem;     
afa30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
afa40 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 6c 6f  ber of memory lo
afa50 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c  cations currentl
afa60 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20  y allocated */. 
afa70 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20   Mem *aMem;     
afa80 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
afa90 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
afaa0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 6c 6c 62   */.  int nCallb
afab0 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ack;          /*
afac0 20 4e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 62   Number of callb
afad0 61 63 6b 73 20 69 6e 76 6f 6b 65 64 20 73 6f 20  acks invoked so 
afae0 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63  far */.  int cac
afaf0 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20  heCtr;          
afb00 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72   /* VdbeCursor r
afb10 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74  ow cache generat
afb20 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  ion counter */. 
afb30 20 69 6e 74 20 63 6f 6e 74 65 78 74 53 74 61 63   int contextStac
afb40 6b 54 6f 70 3b 20 20 20 20 2f 2a 20 49 6e 64 65  kTop;    /* Inde
afb50 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74  x of top element
afb60 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 20   in the context 
afb70 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 63  stack */.  int c
afb80 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 68  ontextStackDepth
afb90 3b 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f  ;  /* The size o
afba0 66 20 74 68 65 20 22 63 6f 6e 74 65 78 74 22 20  f the "context" 
afbb0 73 74 61 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74 65  stack */.  Conte
afbc0 78 74 20 2a 63 6f 6e 74 65 78 74 53 74 61 63 6b  xt *contextStack
afbd0 3b 20 20 2f 2a 20 53 74 61 63 6b 20 75 73 65 64  ;  /* Stack used
afbe0 20 62 79 20 6f 70 63 6f 64 65 73 20 43 6f 6e 74   by opcodes Cont
afbf0 65 78 74 50 75 73 68 20 26 20 43 6f 6e 74 65 78  extPush & Contex
afc00 74 50 6f 70 2a 2f 0a 20 20 69 6e 74 20 70 63 3b  tPop*/.  int pc;
afc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afc20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20   /* The program 
afc30 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
afc40 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
afc50 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
afc60 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 75 6e 73   return */.  uns
afc70 69 67 6e 65 64 20 75 6e 69 71 75 65 43 6e 74 3b  igned uniqueCnt;
afc80 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20       /* Used by 
afc90 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77 68  OP_MakeRecord wh
afca0 65 6e 20 50 32 21 3d 30 20 2a 2f 0a 20 20 69 6e  en P2!=0 */.  in
afcb0 74 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20  t errorAction;  
afcc0 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72        /* Recover
afcd0 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69  y action to do i
afce0 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72  n case of an err
afcf0 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 54 65  or */.  int inTe
afd00 6d 70 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20  mpTrans;        
afd10 2f 2a 20 54 72 75 65 20 69 66 20 74 65 6d 70 20  /* True if temp 
afd20 64 61 74 61 62 61 73 65 20 69 73 20 74 72 61 6e  database is tran
afd30 73 61 63 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 69  sactioned */.  i
afd40 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b 20 20  nt nResColumn;  
afd50 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
afd60 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f   of columns in o
afd70 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65  ne row of the re
afd80 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 63 68  sult set */.  ch
afd90 61 72 20 2a 2a 61 7a 52 65 73 43 6f 6c 75 6d 6e  ar **azResColumn
afda0 3b 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20  ;     /* Values 
afdb0 66 6f 72 20 6f 6e 65 20 72 6f 77 20 6f 66 20 72  for one row of r
afdc0 65 73 75 6c 74 20 2a 2f 20 0a 20 20 63 68 61 72  esult */ .  char
afdd0 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20   *zErrMsg;      
afde0 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73      /* Error mes
afdf0 73 61 67 65 20 77 72 69 74 74 65 6e 20 68 65 72  sage written her
afe00 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 73  e */.  Mem *pRes
afe10 75 6c 74 53 65 74 3b 20 20 20 20 20 20 20 20 2f  ultSet;        /
afe20 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  * Pointer to an 
afe30 61 72 72 61 79 20 6f 66 20 72 65 73 75 6c 74 73  array of results
afe40 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e   */.  u8 explain
afe50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
afe60 20 54 72 75 65 20 69 66 20 45 58 50 4c 41 49 4e   True if EXPLAIN
afe70 20 70 72 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20   present on SQL 
afe80 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 75 38 20  command */.  u8 
afe90 63 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 20 20 20  changeCntOn;    
afea0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
afeb0 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
afec0 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75  e-counter */.  u
afed0 38 20 65 78 70 69 72 65 64 3b 20 20 20 20 20 20  8 expired;      
afee0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
afef0 66 20 74 68 65 20 56 4d 20 6e 65 65 64 73 20 74  f the VM needs t
aff00 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20  o be recompiled 
aff10 2a 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69 74 65  */.  u8 minWrite
aff20 46 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f 2a 20  FileFormat;  /* 
aff30 4d 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72  Minimum file for
aff40 6d 61 74 20 66 6f 72 20 77 72 69 74 61 62 6c 65  mat for writable
aff50 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
aff60 2a 2f 0a 20 20 75 38 20 69 6e 56 74 61 62 4d 65  */.  u8 inVtabMe
aff70 74 68 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  thod;        /* 
aff80 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f  See comments abo
aff90 76 65 20 2a 2f 0a 20 20 75 38 20 75 73 65 73 53  ve */.  u8 usesS
affa0 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20  tmtJournal;     
affb0 2f 2a 20 54 72 75 65 20 69 66 20 75 73 65 73 20  /* True if uses 
affc0 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  a statement jour
affd0 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 72 65 61 64  nal */.  u8 read
affe0 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20  Only;           
afff0 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 72 65 61   /* True for rea
b0000 64 2d 6f 6e 6c 79 20 73 74 61 74 65 6d 65 6e 74  d-only statement
b0010 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e  s */.  int nChan
b0020 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ge;            /
b0030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 62 20 63  * Number of db c
b0040 68 61 6e 67 65 73 20 6d 61 64 65 20 73 69 6e 63  hanges made sinc
b0050 65 20 6c 61 73 74 20 72 65 73 65 74 20 2a 2f 0a  e last reset */.
b0060 20 20 69 36 34 20 73 74 61 72 74 54 69 6d 65 3b    i64 startTime;
b0070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d            /* Tim
b0080 65 20 77 68 65 6e 20 71 75 65 72 79 20 73 74 61  e when query sta
b0090 72 74 65 64 20 2d 20 75 73 65 64 20 66 6f 72 20  rted - used for 
b00a0 70 72 6f 66 69 6c 69 6e 67 20 2a 2f 0a 20 20 69  profiling */.  i
b00b0 6e 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20  nt btreeMask;   
b00c0 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73         /* Bitmas
b00d0 6b 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65  k of db->aDb[] e
b00e0 6e 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65  ntries reference
b00f0 64 20 2a 2f 0a 20 20 42 74 72 65 65 4d 75 74 65  d */.  BtreeMute
b0100 78 41 72 72 61 79 20 61 4d 75 74 65 78 3b 20 2f  xArray aMutex; /
b0110 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 42 74  * An array of Bt
b0120 72 65 65 20 75 73 65 64 20 68 65 72 65 20 61 6e  ree used here an
b0130 64 20 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 73 20  d needing locks 
b0140 2a 2f 0a 20 20 69 6e 74 20 61 43 6f 75 6e 74 65  */.  int aCounte
b0150 72 5b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20  r[2];        /* 
b0160 43 6f 75 6e 74 65 72 73 20 75 73 65 64 20 62 79  Counters used by
b0170 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74   sqlite3_stmt_st
b0180 61 74 75 73 28 29 20 2a 2f 0a 20 20 69 6e 74 20  atus() */.  int 
b0190 6e 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20  nSql;           
b01a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
b01b0 79 74 65 73 20 69 6e 20 7a 53 71 6c 20 2a 2f 0a  ytes in zSql */.
b01c0 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20    char *zSql;   
b01d0 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20          /* Text 
b01e0 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  of the SQL state
b01f0 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65 72 61  ment that genera
b0200 74 65 64 20 74 68 69 73 20 2a 2f 0a 23 69 66 64  ted this */.#ifd
b0210 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
b0220 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b 20 20    FILE *trace;  
b0230 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
b0240 20 61 6e 20 65 78 65 63 75 74 69 6f 6e 20 74 72   an execution tr
b0250 61 63 65 20 68 65 72 65 2c 20 69 66 20 6e 6f 74  ace here, if not
b0260 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a   NULL */.#endif.
b0270 20 20 69 6e 74 20 6f 70 65 6e 65 64 53 74 61 74    int openedStat
b0280 65 6d 65 6e 74 3b 20 20 2f 2a 20 54 72 75 65 20  ement;  /* True 
b0290 69 66 20 74 68 69 73 20 56 4d 20 68 61 73 20 6f  if this VM has o
b02a0 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d 65 6e  pened a statemen
b02b0 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 66  t journal */.#if
b02c0 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20  def SQLITE_SSE. 
b02d0 20 69 6e 74 20 66 65 74 63 68 49 64 3b 20 20 20   int fetchId;   
b02e0 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d         /* Statem
b02f0 65 6e 74 20 6e 75 6d 62 65 72 20 75 73 65 64 20  ent number used 
b0300 62 79 20 73 71 6c 69 74 65 33 5f 66 65 74 63 68  by sqlite3_fetch
b0310 5f 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20  _statement */.  
b0320 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 20 20  int lru;        
b0330 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72        /* Counter
b0340 20 75 73 65 64 20 66 6f 72 20 4c 52 55 20 63 61   used for LRU ca
b0350 63 68 65 20 72 65 70 6c 61 63 65 6d 65 6e 74 20  che replacement 
b0360 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  */.#endif.#ifdef
b0370 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
b0380 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
b0390 0a 20 20 56 64 62 65 20 2a 70 4c 72 75 50 72 65  .  Vdbe *pLruPre
b03a0 76 3b 0a 20 20 56 64 62 65 20 2a 70 4c 72 75 4e  v;.  Vdbe *pLruN
b03b0 65 78 74 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  ext;.#endif.};..
b03c0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
b03d0 69 6e 67 20 61 72 65 20 61 6c 6c 6f 77 65 64 20  ing are allowed 
b03e0 76 61 6c 75 65 73 20 66 6f 72 20 56 64 62 65 2e  values for Vdbe.
b03f0 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 66 69 6e 65  magic.*/.#define
b0400 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54   VDBE_MAGIC_INIT
b0410 20 20 20 20 20 30 78 32 36 62 63 65 61 61 35 20       0x26bceaa5 
b0420 20 20 20 2f 2a 20 42 75 69 6c 64 69 6e 67 20 61     /* Building a
b0430 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 2a 2f   VDBE program */
b0440 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41  .#define VDBE_MA
b0450 47 49 43 5f 52 55 4e 20 20 20 20 20 20 30 78 62  GIC_RUN      0xb
b0460 64 66 32 30 64 61 33 20 20 20 20 2f 2a 20 56 44  df20da3    /* VD
b0470 42 45 20 69 73 20 72 65 61 64 79 20 74 6f 20 65  BE is ready to e
b0480 78 65 63 75 74 65 20 2a 2f 0a 23 64 65 66 69 6e  xecute */.#defin
b0490 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c  e VDBE_MAGIC_HAL
b04a0 54 20 20 20 20 20 30 78 35 31 39 63 32 39 37 33  T     0x519c2973
b04b0 20 20 20 20 2f 2a 20 56 44 42 45 20 68 61 73 20      /* VDBE has 
b04c0 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74  completed execut
b04d0 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56  ion */.#define V
b04e0 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 44 20 20  DBE_MAGIC_DEAD  
b04f0 20 20 20 30 78 62 36 30 36 63 33 63 38 20 20 20     0xb606c3c8   
b0500 20 2f 2a 20 54 68 65 20 56 44 42 45 20 68 61 73   /* The VDBE has
b0510 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65   been deallocate
b0520 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63  d */../*.** Func
b0530 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a  tion prototypes.
b0540 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b0550 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
b0560 62 65 46 72 65 65 43 75 72 73 6f 72 28 56 64 62  beFreeCursor(Vdb
b0570 65 20 2a 2c 20 56 64 62 65 43 75 72 73 6f 72 2a  e *, VdbeCursor*
b0580 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74 65 56 64  );.void sqliteVd
b0590 62 65 50 6f 70 53 74 61 63 6b 28 56 64 62 65 2a  bePopStack(Vdbe*
b05a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
b05b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b05c0 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74  3VdbeCursorMovet
b05d0 6f 28 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a  o(VdbeCursor*);.
b05e0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
b05f0 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66  TE_DEBUG) || def
b0600 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c  ined(VDBE_PROFIL
b0610 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  E).SQLITE_PRIVAT
b0620 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
b0630 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 2a 2c  bePrintOp(FILE*,
b0640 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a 23 65 6e 64   int, Op*);.#end
b0650 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
b0660 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
b0670 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75  eSerialTypeLen(u
b0680 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  32);.SQLITE_PRIV
b0690 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56  ATE u32 sqlite3V
b06a0 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65  dbeSerialType(Me
b06b0 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  m*, int);.SQLITE
b06c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b06d0 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75  ite3VdbeSerialPu
b06e0 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  t(unsigned char*
b06f0 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c 20 69 6e 74  , int, Mem*, int
b0700 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
b0710 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
b0720 65 53 65 72 69 61 6c 47 65 74 28 63 6f 6e 73 74  eSerialGet(const
b0730 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c   unsigned char*,
b0740 20 75 33 32 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c   u32, Mem*);.SQL
b0750 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b0760 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
b0770 74 65 41 75 78 44 61 74 61 28 56 64 62 65 46 75  teAuxData(VdbeFu
b0780 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a 69 6e 74 20  nc*, int);..int 
b0790 73 71 6c 69 74 65 32 42 74 72 65 65 4b 65 79 43  sqlite2BtreeKeyC
b07a0 6f 6d 70 61 72 65 28 42 74 43 75 72 73 6f 72 20  ompare(BtCursor 
b07b0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c  *, const void *,
b07c0 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 20 2a   int, int, int *
b07d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
b07e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
b07f0 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 56  eIdxKeyCompare(V
b0800 64 62 65 43 75 72 73 6f 72 2a 2c 55 6e 70 61 63  dbeCursor*,Unpac
b0810 6b 65 64 52 65 63 6f 72 64 2a 2c 69 6e 74 2a 29  kedRecord*,int*)
b0820 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
b0830 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
b0840 49 64 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f  IdxRowid(BtCurso
b0850 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c  r *, i64 *);.SQL
b0860 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b0870 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72  sqlite3MemCompar
b0880 65 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f  e(const Mem*, co
b0890 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20  nst Mem*, const 
b08a0 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54  CollSeq*);.SQLIT
b08b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b08c0 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 56 64  lite3VdbeExec(Vd
b08d0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
b08e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b08f0 56 64 62 65 4c 69 73 74 28 56 64 62 65 2a 29 3b  VdbeList(Vdbe*);
b0900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0910 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 48  int sqlite3VdbeH
b0920 61 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  alt(Vdbe*);.SQLI
b0930 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b0940 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
b0950 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c 20  Encoding(Mem *, 
b0960 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
b0970 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b0980 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65  VdbeMemTooBig(Me
b0990 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
b09a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
b09b0 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c  dbeMemCopy(Mem*,
b09c0 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 3b 0a 53 51   const Mem*);.SQ
b09d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b09e0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
b09f0 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a  ShallowCopy(Mem*
b0a00 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e  , const Mem*, in
b0a10 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
b0a20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
b0a30 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c  dbeMemMove(Mem*,
b0a40 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   Mem*);.SQLITE_P
b0a50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b0a60 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d  e3VdbeMemNulTerm
b0a70 69 6e 61 74 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c  inate(Mem*);.SQL
b0a80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b0a90 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
b0aa0 74 53 74 72 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74  tStr(Mem*, const
b0ab0 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 75 38 2c   char*, int, u8,
b0ac0 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29   void(*)(void*))
b0ad0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
b0ae0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
b0af0 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d  eMemSetInt64(Mem
b0b00 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f  *, i64);.SQLITE_
b0b10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
b0b20 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f  ite3VdbeMemSetDo
b0b30 75 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c  uble(Mem*, doubl
b0b40 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
b0b50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
b0b60 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65  dbeMemSetNull(Me
b0b70 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
b0b80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
b0b90 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c  VdbeMemSetZeroBl
b0ba0 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29 3b 0a 53 51  ob(Mem*,int);.SQ
b0bb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b0bc0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
b0bd0 53 65 74 52 6f 77 53 65 74 28 4d 65 6d 2a 29 3b  SetRowSet(Mem*);
b0be0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0bf0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
b0c00 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
b0c10 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
b0c20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b0c30 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66  3VdbeMemStringif
b0c40 79 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51  y(Mem*, int);.SQ
b0c50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34  LITE_PRIVATE i64
b0c60 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
b0c70 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  alue(Mem*);.SQLI
b0c80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b0c90 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
b0ca0 65 67 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53  egerify(Mem*);.S
b0cb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f  QLITE_PRIVATE do
b0cc0 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65  uble sqlite3Vdbe
b0cd0 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b  RealValue(Mem*);
b0ce0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0cf0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
b0d00 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28  IntegerAffinity(
b0d10 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
b0d20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b0d30 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28  3VdbeMemRealify(
b0d40 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
b0d50 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b0d60 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79  3VdbeMemNumerify
b0d70 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  (Mem*);.SQLITE_P
b0d80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b0d90 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72  e3VdbeMemFromBtr
b0da0 65 65 28 42 74 43 75 72 73 6f 72 2a 2c 69 6e 74  ee(BtCursor*,int
b0db0 2c 69 6e 74 2c 69 6e 74 2c 4d 65 6d 2a 29 3b 0a  ,int,int,Mem*);.
b0dc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
b0dd0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
b0de0 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70  emRelease(Mem *p
b0df0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
b0e00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
b0e10 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65  beMemReleaseExte
b0e20 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 3b 0a 53 51  rnal(Mem *p);.SQ
b0e30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b0e40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
b0e50 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75  inalize(Mem*, Fu
b0e60 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f  ncDef*);.SQLITE_
b0e70 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
b0e80 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64  ar *sqlite3Opcod
b0e90 65 4e 61 6d 65 28 69 6e 74 29 3b 0a 53 51 4c 49  eName(int);.SQLI
b0ea0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b0eb0 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65  qlite3VdbeOpcode
b0ec0 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e 74 2c  HasProperty(int,
b0ed0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
b0ee0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b0ef0 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d  3VdbeMemGrow(Mem
b0f00 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69   *pMem, int n, i
b0f10 6e 74 20 70 72 65 73 65 72 76 65 29 3b 0a 23 69  nt preserve);.#i
b0f20 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
b0f30 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
b0f40 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  MENT.SQLITE_PRIV
b0f50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
b0f60 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 65 72  dbeReleaseBuffer
b0f70 73 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e 64  s(Vdbe *p);.#end
b0f80 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  if..#ifndef NDEB
b0f90 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
b0fa0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
b0fb0 56 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65  VdbeMemSanity(Me
b0fc0 6d 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49  m*);.#endif.SQLI
b0fd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b0fe0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61  qlite3VdbeMemTra
b0ff0 6e 73 6c 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29  nslate(Mem*, u8)
b1000 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
b1010 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
b1020 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
b1030 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
b1040 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
b1050 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
b1060 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74  lite3VdbeMemPret
b1070 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65  tyPrint(Mem *pMe
b1080 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a  m, char *zBuf);.
b1090 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
b10a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b10b0 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f  3VdbeMemHandleBo
b10c0 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23  m(Mem *pMem);..#
b10d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
b10e0 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49  IT_INCRBLOB.SQLI
b10f0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
b1100 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45   sqlite3VdbeMemE
b1110 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 29  xpandBlob(Mem *)
b1120 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  ;.#else.  #defin
b1130 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  e sqlite3VdbeMem
b1140 45 78 70 61 6e 64 42 6c 6f 62 28 78 29 20 53 51  ExpandBlob(x) SQ
b1150 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a  LITE_OK.#endif..
b1160 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
b1170 65 64 28 5f 56 44 42 45 49 4e 54 5f 48 5f 29 20  ed(_VDBEINT_H_) 
b1180 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
b1190 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 49  *** End of vdbeI
b11a0 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
b11b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b11c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b11d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
b11e0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
b11f0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
b1200 20 69 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a   in utf.c ******
b1210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1220 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  **/..#ifndef SQL
b1230 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
b1240 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
b1250 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61  wing constant va
b1260 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 74  lue is used by t
b1270 68 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  he SQLITE_BIGEND
b1280 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54  IAN and.** SQLIT
b1290 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d  E_LITTLEENDIAN m
b12a0 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  acros..*/.SQLITE
b12b0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69  _PRIVATE const i
b12c0 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20  nt sqlite3one = 
b12d0 31 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  1;.#endif /* SQL
b12e0 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
b12f0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20   */../*.** This 
b1300 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20  lookup table is 
b1310 75 73 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63  used to help dec
b1320 6f 64 65 20 74 68 65 20 66 69 72 73 74 20 62 79  ode the first by
b1330 74 65 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69  te of.** a multi
b1340 2d 62 79 74 65 20 55 54 46 38 20 63 68 61 72 61  -byte UTF8 chara
b1350 63 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cter..*/.static 
b1360 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
b1370 68 61 72 20 73 71 6c 69 74 65 33 55 74 66 38 54  har sqlite3Utf8T
b1380 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78  rans1[] = {.  0x
b1390 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20  00, 0x01, 0x02, 
b13a0 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35  0x03, 0x04, 0x05
b13b0 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20  , 0x06, 0x07,.  
b13c0 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61  0x08, 0x09, 0x0a
b13d0 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78  , 0x0b, 0x0c, 0x
b13e0 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a  0d, 0x0e, 0x0f,.
b13f0 20 20 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78    0x10, 0x11, 0x
b1400 31 32 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20  12, 0x13, 0x14, 
b1410 30 78 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37  0x15, 0x16, 0x17
b1420 2c 0a 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20  ,.  0x18, 0x19, 
b1430 30 78 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63  0x1a, 0x1b, 0x1c
b1440 2c 20 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78  , 0x1d, 0x1e, 0x
b1450 31 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31  1f,.  0x00, 0x01
b1460 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78  , 0x02, 0x03, 0x
b1470 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20  04, 0x05, 0x06, 
b1480 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78  0x07,.  0x08, 0x
b1490 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20  09, 0x0a, 0x0b, 
b14a0 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65  0x0c, 0x0d, 0x0e
b14b0 2c 20 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20  , 0x0f,.  0x00, 
b14c0 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33  0x01, 0x02, 0x03
b14d0 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78  , 0x04, 0x05, 0x
b14e0 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30  06, 0x07,.  0x00
b14f0 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
b1500 30 33 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20  03, 0x00, 0x01, 
b1510 30 78 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a  0x00, 0x00,.};..
b1520 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55  .#define WRITE_U
b1530 54 46 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20  TF8(zOut, c) {  
b1540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1550 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
b1560 63 3c 30 78 30 30 30 38 30 20 29 7b 20 20 20 20  c<0x00080 ){    
b1570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1590 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
b15a0 20 28 75 38 29 28 63 26 30 78 46 46 29 3b 20 20   (u8)(c&0xFF);  
b15b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b15c0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20            \.  } 
b15d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b15e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b15f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1600 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20     \.  else if( 
b1610 63 3c 30 78 30 30 38 30 30 20 29 7b 20 20 20 20  c<0x00800 ){    
b1620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1630 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
b1640 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30    *zOut++ = 0xC0
b1650 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 26 30   + (u8)((c>>6)&0
b1660 78 31 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x1F);           
b1670 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
b1680 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29  ++ = 0x80 + (u8)
b1690 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  (c & 0x3F);     
b16a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
b16b0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
b16c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b16d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b16e0 20 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20         \.  else 
b16f0 69 66 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b  if( c<0x10000 ){
b1700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1720 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
b1730 30 78 45 30 20 2b 20 28 75 38 29 28 28 63 3e 3e  0xE0 + (u8)((c>>
b1740 31 32 29 26 30 78 30 46 29 3b 20 20 20 20 20 20  12)&0x0F);      
b1750 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
b1760 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
b1770 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78  (u8)((c>>6) & 0x
b1780 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
b1790 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
b17a0 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20  = 0x80 + (u8)(c 
b17b0 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
b17c0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
b17d0 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20  else{           
b17e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b17f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1800 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
b1810 2b 20 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28  + = 0xF0 + (u8)(
b1820 28 63 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b  (c>>18) & 0x07);
b1830 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
b1840 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
b1850 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29  0 + (u8)((c>>12)
b1860 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20   & 0x3F);       
b1870 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
b1880 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38  t++ = 0x80 + (u8
b1890 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29  )((c>>6) & 0x3F)
b18a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c  ;              \
b18b0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30  .    *zOut++ = 0
b18c0 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30  x80 + (u8)(c & 0
b18d0 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
b18e0 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
b18f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1920 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52   \.}..#define WR
b1930 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74  ITE_UTF16LE(zOut
b1940 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20  , c) {          
b1950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1960 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
b1970 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20  ( c<=0xFFFF ){  
b1980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b19a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b19b0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
b19c0 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20  (u8)(c&0x00FF); 
b19d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b19e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b19f0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
b1a00 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38  t++ = (u8)((c>>8
b1a10 29 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20  )&0x00FF);      
b1a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1a30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
b1a40 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20  }else{          
b1a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1a80 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
b1a90 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26  = (u8)(((c>>10)&
b1aa0 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30  0x003F) + (((c-0
b1ab0 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30  x10000)>>10)&0x0
b1ac0 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a  0C0));  \.    *z
b1ad0 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30  Out++ = (u8)(0x0
b1ae0 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30  0D8 + (((c-0x100
b1af0 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b  00)>>18)&0x03));
b1b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
b1b10 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
b1b20 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20  8)(c&0x00FF);   
b1b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1b50 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
b1b60 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 43 20  + = (u8)(0x00DC 
b1b70 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29  + ((c>>8)&0x03))
b1b80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
b1b90 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20            \.  } 
b1ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1be0 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49  \.}..#define WRI
b1bf0 54 45 5f 55 54 46 31 36 42 45 28 7a 4f 75 74 2c  TE_UTF16BE(zOut,
b1c00 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20   c) {           
b1c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1c20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28           \.  if(
b1c30 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20   c<=0xFFFF ){   
b1c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
b1c70 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
b1c80 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46  u8)((c>>8)&0x00F
b1c90 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
b1ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1cb0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
b1cc0 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 30  ++ = (u8)(c&0x00
b1cd0 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  FF);            
b1ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1cf0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
b1d00 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20  else{           
b1d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1d40 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
b1d50 20 28 75 38 29 28 30 78 30 30 44 38 20 2b 20 28   (u8)(0x00D8 + (
b1d60 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38  ((c-0x10000)>>18
b1d70 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20  )&0x03));       
b1d80 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
b1d90 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e  ut++ = (u8)(((c>
b1da0 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 28  >10)&0x003F) + (
b1db0 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30  ((c-0x10000)>>10
b1dc0 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20  )&0x00C0));  \. 
b1dd0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
b1de0 29 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e  )(0x00DC + ((c>>
b1df0 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20  8)&0x03));      
b1e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1e10 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
b1e20 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46   = (u8)(c&0x00FF
b1e30 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
b1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1e50 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20           \.  }  
b1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
b1ea0 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44  .}..#define READ
b1eb0 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29  _UTF16LE(zIn, c)
b1ec0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
b1ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1ee0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20            \.  c 
b1ef0 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20  = (*zIn++);     
b1f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1f30 20 20 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49    \.  c += ((*zI
b1f40 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20  n++)<<8);       
b1f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1f70 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
b1f80 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63  ( c>=0xD800 && c
b1f90 3c 30 78 45 30 30 30 20 29 7b 20 20 20 20 20 20  <0xE000 ){      
b1fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1fc0 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20   \.    int c2 = 
b1fd0 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20  (*zIn++);       
b1fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2000 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63           \.    c
b2010 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c  2 += ((*zIn++)<<
b2020 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  8);             
b2030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2050 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30   \.    c = (c2&0
b2060 78 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 30  x03FF) + ((c&0x0
b2070 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63  03F)<<10) + (((c
b2080 26 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 29  &0x03C0)+0x0040)
b2090 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 20 20 69  <<10);   \.    i
b20a0 66 28 20 28 63 20 26 20 30 78 46 46 46 46 30 30  f( (c & 0xFFFF00
b20b0 30 30 29 3d 3d 30 20 29 20 63 20 3d 20 30 78 46  00)==0 ) c = 0xF
b20c0 46 46 44 3b 20 20 20 20 20 20 20 20 20 20 20 20  FFD;            
b20d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b20e0 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
b20f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2120 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64           \.}..#d
b2130 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36  efine READ_UTF16
b2140 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20  BE(zIn, c){     
b2150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2170 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 28 2a 7a      \.  c = ((*z
b2180 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20  In++)<<8);      
b2190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b21a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b21b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
b21c0 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20  c += (*zIn++);  
b21d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b21e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b21f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2200 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30      \.  if( c>=0
b2210 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 30  xD800 && c<0xE00
b2220 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
b2230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2240 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
b2250 20 69 6e 74 20 63 32 20 3d 20 28 28 2a 7a 49 6e   int c2 = ((*zIn
b2260 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20  ++)<<8);        
b2270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2290 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28     \.    c2 += (
b22a0 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20  *zIn++);        
b22b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b22c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b22d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
b22e0 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 46 29   c = (c2&0x03FF)
b22f0 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c   + ((c&0x003F)<<
b2300 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 33 43  10) + (((c&0x03C
b2310 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b  0)+0x0040)<<10);
b2320 20 20 20 5c 0a 20 20 20 20 69 66 28 20 28 63 20     \.    if( (c 
b2330 26 20 30 78 46 46 46 46 30 30 30 30 29 3d 3d 30  & 0xFFFF0000)==0
b2340 20 29 20 63 20 3d 20 30 78 46 46 46 44 3b 20 20   ) c = 0xFFFD;  
b2350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2360 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
b2370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b23a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b23b0 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72     \.}../*.** Tr
b23c0 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65  anslate a single
b23d0 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
b23e0 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e  .  Return the un
b23f0 69 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a  icode value..**.
b2400 2a 2a 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c  ** During transl
b2410 61 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68  ation, assume th
b2420 61 74 20 74 68 65 20 62 79 74 65 20 74 68 61 74  at the byte that
b2430 20 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a   zTerm points.**
b2440 20 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a   is a 0x00..**.*
b2450 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65  * Write a pointe
b2460 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e  r to the next un
b2470 72 65 61 64 20 62 79 74 65 20 62 61 63 6b 20 69  read byte back i
b2480 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a  nto *pzNext..**.
b2490 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61  ** Notes On Inva
b24a0 6c 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a  lid UTF-8:.**.**
b24b0 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e    *  This routin
b24c0 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61  e never allows a
b24d0 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72   7-bit character
b24e0 20 28 30 78 30 30 20 74 68 72 6f 75 67 68 20 30   (0x00 through 0
b24f0 78 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62  x7f) to.**     b
b2500 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d  e encoded as a m
b2510 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63  ulti-byte charac
b2520 74 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d  ter.  Any multi-
b2530 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74  byte character t
b2540 68 61 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d  hat.**     attem
b2550 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20  pts to encode a 
b2560 76 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78  value between 0x
b2570 30 30 20 61 6e 64 20 30 78 37 66 20 69 73 20 72  00 and 0x7f is r
b2580 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66  endered as 0xfff
b2590 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69  d..**.**  *  Thi
b25a0 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20  s routine never 
b25b0 61 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73  allows a UTF16 s
b25c0 75 72 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74  urrogate value t
b25d0 6f 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a  o be encoded..**
b25e0 20 20 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d       If a multi-
b25f0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 61  byte character a
b2600 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64  ttempts to encod
b2610 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65  e a value betwee
b2620 6e 0a 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20  n.**     0xd800 
b2630 61 6e 64 20 30 78 65 30 30 30 20 74 68 65 6e 20  and 0xe000 then 
b2640 69 74 20 69 73 20 72 65 6e 64 65 72 65 64 20 61  it is rendered a
b2650 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20  s 0xfffd..**.** 
b2660 20 2a 20 20 42 79 74 65 73 20 69 6e 20 74 68 65   *  Bytes in the
b2670 20 72 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74   range of 0x80 t
b2680 68 72 6f 75 67 68 20 30 78 62 66 20 77 68 69 63  hrough 0xbf whic
b2690 68 20 6f 63 63 75 72 20 61 73 20 74 68 65 20 66  h occur as the f
b26a0 69 72 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65  irst.**     byte
b26b0 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20   of a character 
b26c0 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20  are interpreted 
b26d0 61 73 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63  as single-byte c
b26e0 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20  haracters.**    
b26f0 20 61 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73   and rendered as
b2700 20 74 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e   themselves even
b2710 20 74 68 6f 75 67 68 20 74 68 65 79 20 61 72 65   though they are
b2720 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20   technically.** 
b2730 20 20 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72      invalid char
b2740 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a  acters..**.**  *
b2750 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61    This routine a
b2760 63 63 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69  ccepts an infini
b2770 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66  te number of dif
b2780 66 65 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f  ferent UTF8 enco
b2790 64 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72  dings.**     for
b27a0 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20   unicode values 
b27b0 30 78 38 30 20 61 6e 64 20 67 72 65 61 74 65 72  0x80 and greater
b27c0 2e 20 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61  .  It do not cha
b27d0 6e 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a  nge over-length.
b27e0 2a 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73  **     encodings
b27f0 20 74 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f   to 0xfffd as so
b2800 6d 65 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d  me systems recom
b2810 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  mend..*/.#define
b2820 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20   READ_UTF8(zIn, 
b2830 7a 54 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20  zTerm, c)       
b2840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2850 20 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49      \.  c = *(zI
b2860 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
b2870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2890 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30   \.  if( c>=0xc0
b28a0 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
b28b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b28c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
b28d0 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
b28e0 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30  tf8Trans1[c-0xc0
b28f0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
b2900 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
b2910 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65   while( zIn!=zTe
b2920 72 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78  rm && (*zIn & 0x
b2930 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20  c0)==0x80 ){    
b2940 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20          \.      
b2950 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78  c = (c<<6) + (0x
b2960 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20  3f & *(zIn++)); 
b2970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2980 20 20 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20       \.    }    
b2990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b29a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b29b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b29c0 20 20 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78    \.    if( c<0x
b29d0 38 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  80              
b29e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b29f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
b2a00 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30  .        || (c&0
b2a10 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38  xFFFFF800)==0xD8
b2a20 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  00              
b2a30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
b2a40 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46        || (c&0xFF
b2a50 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20  FFFFFE)==0xFFFE 
b2a60 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20  ){  c = 0xFFFD; 
b2a70 7d 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53  }        \.  }.S
b2a80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b2a90 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  t sqlite3Utf8Rea
b2aa0 64 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  d(.  const unsig
b2ab0 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 20 20 20  ned char *z,    
b2ac0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79       /* First by
b2ad0 74 65 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72  te of UTF-8 char
b2ae0 61 63 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  acter */.  const
b2af0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
b2b00 7a 54 65 72 6d 2c 20 20 20 20 20 2f 2a 20 50 72  zTerm,     /* Pr
b2b10 65 74 65 6e 64 20 74 68 69 73 20 62 79 74 65 20  etend this byte 
b2b20 69 73 20 30 78 30 30 20 2a 2f 0a 20 20 63 6f 6e  is 0x00 */.  con
b2b30 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
b2b40 20 2a 2a 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20   **pzNext    /* 
b2b50 57 72 69 74 65 20 66 69 72 73 74 20 62 79 74 65  Write first byte
b2b60 20 70 61 73 74 20 55 54 46 2d 38 20 63 68 61 72   past UTF-8 char
b2b70 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
b2b80 74 20 63 3b 0a 20 20 52 45 41 44 5f 55 54 46 38  t c;.  READ_UTF8
b2b90 28 7a 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20  (z, zTerm, c);. 
b2ba0 20 2a 70 7a 4e 65 78 74 20 3d 20 7a 3b 0a 20 20   *pzNext = z;.  
b2bb0 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a  return c;.}.....
b2bc0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41  /*.** If the TRA
b2bd0 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63  NSLATE_TRACE mac
b2be0 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  ro is defined, t
b2bf0 68 65 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68  he value of each
b2c00 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74   Mem is.** print
b2c10 65 64 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20  ed on stderr on 
b2c20 74 68 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64  the way into and
b2c30 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56   out of sqlite3V
b2c40 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28  dbeMemTranslate(
b2c50 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e  )..*/ ./* #defin
b2c60 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43  e TRANSLATE_TRAC
b2c70 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  E 1 */..#ifndef 
b2c80 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
b2c90 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  6./*.** This rou
b2ca0 74 69 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20  tine transforms 
b2cb0 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78  the internal tex
b2cc0 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20  t encoding used 
b2cd0 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65  by pMem to.** de
b2ce0 73 69 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20  siredEnc. It is 
b2cf0 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20  an error if the 
b2d00 73 74 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64  string is alread
b2d10 79 20 6f 66 20 74 68 65 20 64 65 73 69 72 65 64  y of the desired
b2d20 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72  .** encoding, or
b2d30 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e   if *pMem does n
b2d40 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72  ot contain a str
b2d50 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51  ing value..*/.SQ
b2d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b2d70 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
b2d80 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d  ranslate(Mem *pM
b2d90 65 6d 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e  em, u8 desiredEn
b2da0 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20  c){.  int len;  
b2db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2dc0 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e    /* Maximum len
b2dd0 67 74 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74  gth of output st
b2de0 72 69 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f  ring in bytes */
b2df0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
b2e00 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20   *zOut;         
b2e10 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
b2e20 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75  ut buffer */.  u
b2e30 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
b2e40 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
b2e50 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74       /* Input it
b2e60 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69  erator */.  unsi
b2e70 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d  gned char *zTerm
b2e80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
b2e90 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75    /* End of inpu
b2ea0 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  t */.  unsigned 
b2eb0 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
b2ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
b2ed0 4f 75 74 70 75 74 20 69 74 65 72 61 74 6f 72 20  Output iterator 
b2ee0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
b2ef0 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  t c;..  assert( 
b2f00 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
b2f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
b2f20 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
b2f30 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
b2f40 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
b2f50 53 74 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Str );.  assert(
b2f60 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69   pMem->enc!=desi
b2f70 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65  redEnc );.  asse
b2f80 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30  rt( pMem->enc!=0
b2f90 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
b2fa0 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66  em->n>=0 );..#if
b2fb0 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41   defined(TRANSLA
b2fc0 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 66  TE_TRACE) && def
b2fd0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
b2fe0 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20  G).  {.    char 
b2ff0 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73  zBuf[100];.    s
b3000 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65  qlite3VdbeMemPre
b3010 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a  ttyPrint(pMem, z
b3020 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74  Buf);.    fprint
b3030 66 28 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54  f(stderr, "INPUT
b3040 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b  :  %s\n", zBuf);
b3050 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
b3060 2a 20 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61  * If the transla
b3070 74 69 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20  tion is between 
b3080 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e  UTF-16 little an
b3090 64 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68  d big endian, th
b30a0 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61  en .  ** all tha
b30b0 74 20 69 73 20 72 65 71 75 69 72 65 64 20 69 73  t is required is
b30c0 20 74 6f 20 73 77 61 70 20 74 68 65 20 62 79 74   to swap the byt
b30d0 65 20 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61  e order. This ca
b30e0 73 65 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20  se is handled.  
b30f0 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66  ** differently f
b3100 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a  rom the others..
b3110 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d    */.  if( pMem-
b3120 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46  >enc!=SQLITE_UTF
b3130 38 20 26 26 20 64 65 73 69 72 65 64 45 6e 63 21  8 && desiredEnc!
b3140 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a  =SQLITE_UTF8 ){.
b3150 20 20 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20      u8 temp;.   
b3160 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20   int rc;.    rc 
b3170 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
b3180 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d  MakeWriteable(pM
b3190 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  em);.    if( rc!
b31a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
b31b0 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
b31c0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
b31d0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
b31e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
b31f0 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29  .    zIn = (u8*)
b3200 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65  pMem->z;.    zTe
b3210 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e  rm = &zIn[pMem->
b3220 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65  n&~1];.    while
b3230 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20  ( zIn<zTerm ){. 
b3240 20 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e       temp = *zIn
b3250 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a  ;.      *zIn = *
b3260 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a  (zIn+1);.      z
b3270 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e  In++;.      *zIn
b3280 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d  ++ = temp;.    }
b3290 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d  .    pMem->enc =
b32a0 20 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20   desiredEnc;.   
b32b0 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f   goto translate_
b32c0 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  out;.  }..  /* S
b32d0 65 74 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61  et len to the ma
b32e0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
b32f0 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 69  bytes required i
b3300 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  n the output buf
b3310 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65  fer. */.  if( de
b3320 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
b3330 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20  _UTF8 ){.    /* 
b3340 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20  When converting 
b3350 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65  from UTF-16, the
b3360 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20   maximum growth 
b3370 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20  results from.   
b3380 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20   ** translating 
b3390 61 20 32 2d 62 79 74 65 20 63 68 61 72 61 63 74  a 2-byte charact
b33a0 65 72 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55  er to a 4-byte U
b33b0 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a  TF-8 character..
b33c0 20 20 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20      ** A single 
b33d0 62 79 74 65 20 69 73 20 72 65 71 75 69 72 65 64  byte is required
b33e0 20 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20   for the output 
b33f0 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75  string.    ** nu
b3400 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20  l-terminator..  
b3410 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e    */.    pMem->n
b3420 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20   &= ~1;.    len 
b3430 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20  = pMem->n * 2 + 
b3440 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
b3450 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69  /* When converti
b3460 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f  ng from UTF-8 to
b3470 20 55 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69   UTF-16 the maxi
b3480 6d 75 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61  mum growth is ca
b3490 75 73 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e  used.    ** when
b34a0 20 61 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20   a 1-byte UTF-8 
b34b0 63 68 61 72 61 63 74 65 72 20 69 73 20 74 72 61  character is tra
b34c0 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32  nslated into a 2
b34d0 2d 62 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20  -byte UTF-16.   
b34e0 20 2a 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54   ** character. T
b34f0 77 6f 20 62 79 74 65 73 20 61 72 65 20 72 65 71  wo bytes are req
b3500 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74  uired in the out
b3510 70 75 74 20 62 75 66 66 65 72 20 66 6f 72 20 74  put buffer for t
b3520 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65  he.    ** nul-te
b3530 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f  rminator..    */
b3540 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d  .    len = pMem-
b3550 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a  >n * 2 + 2;.  }.
b3560 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f  .  /* Set zIn to
b3570 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74   point at the st
b3580 61 72 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74  art of the input
b3590 20 62 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72   buffer and zTer
b35a0 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a  m to point 1.  *
b35b0 2a 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20  * byte past the 
b35c0 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56  end..  **.  ** V
b35d0 61 72 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20  ariable zOut is 
b35e0 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20  set to point at 
b35f0 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
b3600 72 2c 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  r, space obtaine
b3610 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69  d.  ** from sqli
b3620 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20  te3_malloc()..  
b3630 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29  */.  zIn = (u8*)
b3640 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d  pMem->z;.  zTerm
b3650 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d   = &zIn[pMem->n]
b3660 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74  ;.  zOut = sqlit
b3670 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d  e3DbMallocRaw(pM
b3680 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20  em->db, len);.  
b3690 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20  if( !zOut ){.   
b36a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
b36b0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20  OMEM;.  }.  z = 
b36c0 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65  zOut;..  if( pMe
b36d0 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  m->enc==SQLITE_U
b36e0 54 46 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64  TF8 ){.    if( d
b36f0 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54  esiredEnc==SQLIT
b3700 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20  E_UTF16LE ){.   
b3710 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55     /* UTF-8 -> U
b3720 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64  TF-16 Little-end
b3730 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  ian */.      whi
b3740 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b  le( zIn<zTerm ){
b3750 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20  .        /* c = 
b3760 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
b3770 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e  zIn, zTerm, (con
b3780 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a  st u8**)&zIn); *
b3790 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55  /.        READ_U
b37a0 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20  TF8(zIn, zTerm, 
b37b0 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54  c);.        WRIT
b37c0 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b  E_UTF16LE(z, c);
b37d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
b37e0 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
b37f0 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51  ( desiredEnc==SQ
b3800 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a  LITE_UTF16BE );.
b3810 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d        /* UTF-8 -
b3820 3e 20 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64  > UTF-16 Big-end
b3830 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  ian */.      whi
b3840 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b  le( zIn<zTerm ){
b3850 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20  .        /* c = 
b3860 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
b3870 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e  zIn, zTerm, (con
b3880 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a  st u8**)&zIn); *
b3890 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55  /.        READ_U
b38a0 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20  TF8(zIn, zTerm, 
b38b0 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54  c);.        WRIT
b38c0 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b  E_UTF16BE(z, c);
b38d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
b38e0 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e     pMem->n = (in
b38f0 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20  t)(z - zOut);.  
b3900 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65    *z++ = 0;.  }e
b3910 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
b3920 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
b3930 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20  ITE_UTF8 );.    
b3940 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53  if( pMem->enc==S
b3950 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b  QLITE_UTF16LE ){
b3960 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36  .      /* UTF-16
b3970 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d   Little-endian -
b3980 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20  > UTF-8 */.     
b3990 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72   while( zIn<zTer
b39a0 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41  m ){.        REA
b39b0 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63  D_UTF16LE(zIn, c
b39c0 29 3b 20 0a 20 20 20 20 20 20 20 20 57 52 49 54  ); .        WRIT
b39d0 45 5f 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20  E_UTF8(z, c);.  
b39e0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
b39f0 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36  .      /* UTF-16
b3a00 20 42 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55   Big-endian -> U
b3a10 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68  TF-8 */.      wh
b3a20 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29  ile( zIn<zTerm )
b3a30 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55  {.        READ_U
b3a40 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20  TF16BE(zIn, c); 
b3a50 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55  .        WRITE_U
b3a60 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20  TF8(z, c);.     
b3a70 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65   }.    }.    pMe
b3a80 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d  m->n = (int)(z -
b3a90 20 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a   zOut);.  }.  *z
b3aa0 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
b3ab0 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65  (pMem->n+(desire
b3ac0 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
b3ad0 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a  8?1:2))<=len );.
b3ae0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
b3af0 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
b3b00 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d    pMem->flags &=
b3b10 20 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45   ~(MEM_Static|ME
b3b20 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29  M_Dyn|MEM_Ephem)
b3b30 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20  ;.  pMem->enc = 
b3b40 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d  desiredEnc;.  pM
b3b50 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45  em->flags |= (ME
b3b60 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b  M_Term|MEM_Dyn);
b3b70 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68  .  pMem->z = (ch
b3b80 61 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d  ar*)zOut;.  pMem
b3b90 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d  ->zMalloc = pMem
b3ba0 2d 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f  ->z;..translate_
b3bb0 6f 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64  out:.#if defined
b3bc0 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45  (TRANSLATE_TRACE
b3bd0 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
b3be0 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20  ITE_DEBUG).  {. 
b3bf0 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30     char zBuf[100
b3c00 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
b3c10 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
b3c20 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20  (pMem, zBuf);.  
b3c30 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
b3c40 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22  , "OUTPUT: %s\n"
b3c50 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e  , zBuf);.  }.#en
b3c60 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  dif.  return SQL
b3c70 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
b3c80 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
b3c90 65 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d  ecks for a byte-
b3ca0 6f 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68  order mark at th
b3cb0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
b3cc0 68 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74  he .** UTF-16 st
b3cd0 72 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a  ring stored in *
b3ce0 70 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20  pMem. If one is 
b3cf0 70 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72  present, it is r
b3d00 65 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68  emoved and.** th
b3d10 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  e encoding of th
b3d20 65 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20  e Mem adjusted. 
b3d30 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
b3d40 73 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20  s not do any.** 
b3d50 62 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69  byte-swapping, i
b3d60 74 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e  t just sets Mem.
b3d70 65 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c  enc appropriatel
b3d80 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c  y..**.** The all
b3d90 6f 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c  ocation (static,
b3da0 20 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61   dynamic etc.) a
b3db0 6e 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  nd encoding of t
b3dc0 68 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a  he Mem may be.**
b3dd0 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73   changed by this
b3de0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   function..*/.SQ
b3df0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b3e00 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48   sqlite3VdbeMemH
b3e10 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d  andleBom(Mem *pM
b3e20 65 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  em){.  int rc = 
b3e30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20  SQLITE_OK;.  u8 
b3e40 62 6f 6d 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  bom = 0;..  if( 
b3e50 70 4d 65 6d 2d 3e 6e 3c 30 20 7c 7c 20 70 4d 65  pMem->n<0 || pMe
b3e60 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20 20 20 75 38  m->n>1 ){.    u8
b3e70 20 62 31 20 3d 20 2a 28 75 38 20 2a 29 70 4d 65   b1 = *(u8 *)pMe
b3e80 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38 20 62 32 20  m->z;.    u8 b2 
b3e90 3d 20 2a 28 28 28 75 38 20 2a 29 70 4d 65 6d 2d  = *(((u8 *)pMem-
b3ea0 3e 7a 29 20 2b 20 31 29 3b 0a 20 20 20 20 69 66  >z) + 1);.    if
b3eb0 28 20 62 31 3d 3d 30 78 46 45 20 26 26 20 62 32  ( b1==0xFE && b2
b3ec0 3d 3d 30 78 46 46 20 29 7b 0a 20 20 20 20 20 20  ==0xFF ){.      
b3ed0 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  bom = SQLITE_UTF
b3ee0 31 36 42 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20  16BE;.    }.    
b3ef0 69 66 28 20 62 31 3d 3d 30 78 46 46 20 26 26 20  if( b1==0xFF && 
b3f00 62 32 3d 3d 30 78 46 45 20 29 7b 0a 20 20 20 20  b2==0xFE ){.    
b3f10 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55    bom = SQLITE_U
b3f20 54 46 31 36 4c 45 3b 0a 20 20 20 20 7d 0a 20 20  TF16LE;.    }.  
b3f30 7d 0a 20 20 0a 20 20 69 66 28 20 62 6f 6d 20 29  }.  .  if( bom )
b3f40 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
b3f50 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69  e3VdbeMemMakeWri
b3f60 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20  teable(pMem);.  
b3f70 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
b3f80 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 4d 65  _OK ){.      pMe
b3f90 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20 20 20 20 20  m->n -= 2;.     
b3fa0 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 6d 2d 3e 7a   memmove(pMem->z
b3fb0 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70  , &pMem->z[2], p
b3fc0 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 70  Mem->n);.      p
b3fd0 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20  Mem->z[pMem->n] 
b3fe0 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 70 4d  = '\0';.      pM
b3ff0 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d  em->z[pMem->n+1]
b4000 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 70   = '\0';.      p
b4010 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
b4020 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d  M_Term;.      pM
b4030 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f 6d 3b 0a 20  em->enc = bom;. 
b4040 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
b4050 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  n rc;.}.#endif /
b4060 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
b4070 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a  F16 */../*.** pZ
b4080 20 69 73 20 61 20 55 54 46 2d 38 20 65 6e 63 6f   is a UTF-8 enco
b4090 64 65 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69  ded unicode stri
b40a0 6e 67 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20  ng. If nByte is 
b40b0 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 0a  less than zero,.
b40c0 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  ** return the nu
b40d0 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20  mber of unicode 
b40e0 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 70 5a  characters in pZ
b40f0 20 75 70 20 74 6f 20 28 62 75 74 20 6e 6f 74 20   up to (but not 
b4100 69 6e 63 6c 75 64 69 6e 67 29 0a 2a 2a 20 74 68  including).** th
b4110 65 20 66 69 72 73 74 20 30 78 30 30 20 62 79 74  e first 0x00 byt
b4120 65 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20 6e  e. If nByte is n
b4130 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72  ot less than zer
b4140 6f 2c 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a  o, return the.**
b4150 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f   number of unico
b4160 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  de characters in
b4170 20 74 68 65 20 66 69 72 73 74 20 6e 42 79 74 65   the first nByte
b4180 20 6f 66 20 70 5a 20 28 6f 72 20 75 70 20 74 6f   of pZ (or up to
b4190 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 30   .** the first 0
b41a0 78 30 30 2c 20 77 68 69 63 68 65 76 65 72 20 63  x00, whichever c
b41b0 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a  omes first)..*/.
b41c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b41d0 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 43 68  nt sqlite3Utf8Ch
b41e0 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61 72  arLen(const char
b41f0 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 42 79 74 65   *zIn, int nByte
b4200 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a  ){.  int r = 0;.
b4210 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 20 3d 20    const u8 *z = 
b4220 28 63 6f 6e 73 74 20 75 38 2a 29 7a 49 6e 3b 0a  (const u8*)zIn;.
b4230 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 54 65 72    const u8 *zTer
b4240 6d 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 3d  m;.  if( nByte>=
b4250 30 20 29 7b 0a 20 20 20 20 7a 54 65 72 6d 20 3d  0 ){.    zTerm =
b4260 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a 20 20 7d 65   &z[nByte];.  }e
b4270 6c 73 65 7b 0a 20 20 20 20 7a 54 65 72 6d 20 3d  lse{.    zTerm =
b4280 20 28 63 6f 6e 73 74 20 75 38 2a 29 28 2d 31 29   (const u8*)(-1)
b4290 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
b42a0 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a 20 20 77 68  z<=zTerm );.  wh
b42b0 69 6c 65 28 20 2a 7a 21 3d 30 20 26 26 20 7a 3c  ile( *z!=0 && z<
b42c0 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 53 51 4c  zTerm ){.    SQL
b42d0 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29  ITE_SKIP_UTF8(z)
b42e0 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20 20 7d 0a 20  ;.    r++;.  }. 
b42f0 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a   return r;.}../*
b4300 20 54 68 69 73 20 74 65 73 74 20 66 75 6e 63 74   This test funct
b4310 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75 72 72 65  ion is not curre
b4320 6e 74 6c 79 20 75 73 65 64 20 62 79 20 74 68 65  ntly used by the
b4330 20 61 75 74 6f 6d 61 74 65 64 20 74 65 73 74 2d   automated test-
b4340 73 75 69 74 65 2e 20 0a 2a 2a 20 48 65 6e 63 65  suite. .** Hence
b4350 20 69 74 20 69 73 20 6f 6e 6c 79 20 61 76 61 69   it is only avai
b4360 6c 61 62 6c 65 20 69 6e 20 64 65 62 75 67 20 62  lable in debug b
b4370 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65  uilds..*/.#if de
b4380 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
b4390 54 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  T) && defined(SQ
b43a0 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
b43b0 2a 20 54 72 61 6e 73 6c 61 74 65 20 55 54 46 2d  * Translate UTF-
b43c0 38 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a  8 to UTF-8..**.*
b43d0 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20 65  * This has the e
b43e0 66 66 65 63 74 20 6f 66 20 6d 61 6b 69 6e 67 20  ffect of making 
b43f0 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 74  sure that the st
b4400 72 69 6e 67 20 69 73 20 77 65 6c 6c 2d 66 6f 72  ring is well-for
b4410 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 2e 20 20 4d  med.** UTF-8.  M
b4420 69 73 63 6f 64 65 64 20 63 68 61 72 61 63 74 65  iscoded characte
b4430 72 73 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 0a  rs are removed..
b4440 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 6c  **.** The transl
b4450 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e  ation is done in
b4460 2d 70 6c 61 63 65 20 28 73 69 6e 63 65 20 69 74  -place (since it
b4470 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66   is impossible f
b4480 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63  or the.** correc
b4490 74 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67  t UTF-8 encoding
b44a0 20 74 6f 20 62 65 20 6c 6f 6e 67 65 72 20 74 68   to be longer th
b44b0 61 6e 20 61 20 6d 61 6c 66 6f 72 6d 65 64 20 65  an a malformed e
b44c0 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f 0a 53 51 4c  ncoding)..*/.SQL
b44d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b44e0 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75  sqlite3Utf8To8(u
b44f0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
b4500 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  n){.  unsigned c
b4510 68 61 72 20 2a 7a 4f 75 74 20 3d 20 7a 49 6e 3b  har *zOut = zIn;
b4520 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
b4530 20 2a 7a 53 74 61 72 74 20 3d 20 7a 49 6e 3b 0a   *zStart = zIn;.
b4540 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
b4550 2a 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 73 71  *zTerm = &zIn[sq
b4560 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 28 63  lite3Strlen30((c
b4570 68 61 72 20 2a 29 7a 49 6e 29 5d 3b 0a 20 20 75  har *)zIn)];.  u
b4580 33 32 20 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20  32 c;..  while( 
b4590 7a 49 6e 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20  zIn[0] ){.    c 
b45a0 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
b45b0 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63  d(zIn, zTerm, (c
b45c0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b  onst u8**)&zIn);
b45d0 0a 20 20 20 20 69 66 28 20 63 21 3d 30 78 66 66  .    if( c!=0xff
b45e0 66 64 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54  fd ){.      WRIT
b45f0 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b  E_UTF8(zOut, c);
b4600 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f  .    }.  }.  *zO
b4610 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  ut = 0;.  return
b4620 20 7a 4f 75 74 20 2d 20 7a 53 74 61 72 74 3b 0a   zOut - zStart;.
b4630 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
b4640 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
b4650 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  F16./*.** Conver
b4660 74 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e  t a UTF-16 strin
b4670 67 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20  g in the native 
b4680 65 6e 63 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20  encoding into a 
b4690 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a  UTF-8 string..**
b46a0 20 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20   Memory to hold 
b46b0 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67  the UTF-8 string
b46c0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
b46d0 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
b46e0 20 61 6e 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20   and must.** be 
b46f0 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c  freed by the cal
b4700 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  ling function..*
b4710 2a 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74  *.** NULL is ret
b4720 75 72 6e 65 64 20 69 66 20 74 68 65 72 65 20 69  urned if there i
b4730 73 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  s an allocation 
b4740 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
b4750 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
b4760 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 73  qlite3Utf16to8(s
b4770 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
b4780 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e  t void *z, int n
b4790 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a  Byte){.  Mem m;.
b47a0 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20    memset(&m, 0, 
b47b0 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e  sizeof(m));.  m.
b47c0 64 62 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74  db = db;.  sqlit
b47d0 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
b47e0 26 6d 2c 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51  &m, z, nByte, SQ
b47f0 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
b4800 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
b4810 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ;.  sqlite3VdbeC
b4820 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d  hangeEncoding(&m
b4830 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
b4840 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
b4850 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71  Failed ){.    sq
b4860 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
b4870 61 73 65 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a  ase(&m);.    m.z
b4880 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65   = 0;.  }.  asse
b4890 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d  rt( (m.flags & M
b48a0 45 4d 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64  EM_Term)!=0 || d
b48b0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
b48c0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e  );.  assert( (m.
b48d0 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29  flags & MEM_Str)
b48e0 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  !=0 || db->mallo
b48f0 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 65 74  cFailed );.  ret
b4900 75 72 6e 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d  urn (m.flags & M
b4910 45 4d 5f 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a  EM_Dyn)!=0 ? m.z
b4920 20 3a 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   : sqlite3DbStrD
b4930 75 70 28 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a  up(db, m.z);.}..
b4940 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54  /*.** pZ is a UT
b4950 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69  F-16 encoded uni
b4960 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20  code string. If 
b4970 6e 43 68 61 72 20 69 73 20 6c 65 73 73 20 74 68  nChar is less th
b4980 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75  an zero,.** retu
b4990 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
b49a0 20 62 79 74 65 73 20 75 70 20 74 6f 20 28 62 75   bytes up to (bu
b49b0 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29  t not including)
b49c0 2c 20 74 68 65 20 66 69 72 73 74 20 70 61 69 72  , the first pair
b49d0 0a 2a 2a 20 6f 66 20 63 6f 6e 73 65 63 75 74 69  .** of consecuti
b49e0 76 65 20 30 78 30 30 20 62 79 74 65 73 20 69 6e  ve 0x00 bytes in
b49f0 20 70 5a 2e 20 49 66 20 6e 43 68 61 72 20 69 73   pZ. If nChar is
b4a00 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 7a   not less than z
b4a10 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74  ero,.** then ret
b4a20 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
b4a30 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66  f bytes in the f
b4a40 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 63 6f  irst nChar unico
b4a50 64 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a  de characters.**
b4a60 20 69 6e 20 70 5a 20 28 6f 72 20 75 70 20 75 6e   in pZ (or up un
b4a70 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 70 61  til the first pa
b4a80 69 72 20 6f 66 20 30 78 30 30 20 62 79 74 65 73  ir of 0x00 bytes
b4a90 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65  , whichever come
b4aa0 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c  s first)..*/.SQL
b4ab0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b4ac0 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65  sqlite3Utf16Byte
b4ad0 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  Len(const void *
b4ae0 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b  zIn, int nChar){
b4af0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
b4b00 63 20 3d 20 31 3b 0a 20 20 63 68 61 72 20 63 6f  c = 1;.  char co
b4b10 6e 73 74 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20  nst *z = zIn;.  
b4b20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28  int n = 0;.  if(
b4b30 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
b4b40 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  IVE==SQLITE_UTF1
b4b50 36 42 45 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73  6BE ){.    /* Us
b4b60 69 6e 67 20 61 6e 20 22 69 66 20 28 53 51 4c 49  ing an "if (SQLI
b4b70 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d 3d  TE_UTF16NATIVE==
b4b80 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 22  SQLITE_UTF16BE)"
b4b90 20 63 6f 6e 73 74 72 75 63 74 20 68 65 72 65 0a   construct here.
b4ba0 20 20 20 20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74      ** and in ot
b4bb0 68 65 72 20 70 61 72 74 73 20 6f 66 20 74 68 69  her parts of thi
b4bc0 73 20 66 69 6c 65 20 6d 65 61 6e 73 20 74 68 61  s file means tha
b4bd0 74 20 61 74 20 6f 6e 65 20 62 72 61 6e 63 68 20  t at one branch 
b4be0 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20  will.    ** not 
b4bf0 62 65 20 63 6f 76 65 72 65 64 20 62 79 20 63 6f  be covered by co
b4c00 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 20 6f  verage testing o
b4c10 6e 20 61 6e 79 20 73 69 6e 67 6c 65 20 68 6f 73  n any single hos
b4c20 74 2e 20 42 75 74 20 63 6f 76 65 72 61 67 65 0a  t. But coverage.
b4c30 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63      ** will be c
b4c40 6f 6d 70 6c 65 74 65 20 69 66 20 74 68 65 20 74  omplete if the t
b4c50 65 73 74 73 20 61 72 65 20 72 75 6e 20 6f 6e 20  ests are run on 
b4c60 62 6f 74 68 20 61 20 6c 69 74 74 6c 65 2d 65 6e  both a little-en
b4c70 64 69 61 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a  dian and .    **
b4c80 20 62 69 67 2d 65 6e 64 69 61 6e 20 68 6f 73 74   big-endian host
b4c90 2e 20 42 65 63 61 75 73 65 20 62 6f 74 68 20 74  . Because both t
b4ca0 68 65 20 55 54 46 31 36 4e 41 54 49 56 45 20 61  he UTF16NATIVE a
b4cb0 6e 64 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  nd SQLITE_UTF16B
b4cc0 45 0a 20 20 20 20 2a 2a 20 6d 61 63 72 6f 73 20  E.    ** macros 
b4cd0 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20  are constant at 
b4ce0 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 74 68 65  compile time the
b4cf0 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e 20 64 65   compiler can de
b4d00 74 65 72 6d 69 6e 65 0a 20 20 20 20 2a 2a 20 77  termine.    ** w
b4d10 68 69 63 68 20 62 72 61 6e 63 68 20 77 69 6c 6c  hich branch will
b4d20 20 62 65 20 66 6f 6c 6c 6f 77 65 64 2e 20 49 74   be followed. It
b4d30 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 73   is therefore as
b4d40 73 75 6d 65 64 20 74 68 61 74 20 6e 6f 20 72 75  sumed that no ru
b4d50 6e 74 69 6d 65 0a 20 20 20 20 2a 2a 20 70 65 6e  ntime.    ** pen
b4d60 61 6c 74 79 20 69 73 20 70 61 69 64 20 66 6f 72  alty is paid for
b4d70 20 74 68 69 73 20 22 69 66 22 20 73 74 61 74 65   this "if" state
b4d80 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ment..    */.   
b4d90 20 77 68 69 6c 65 28 20 63 20 26 26 20 28 28 6e   while( c && ((n
b4da0 43 68 61 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68  Char<0) || n<nCh
b4db0 61 72 29 20 29 7b 0a 20 20 20 20 20 20 52 45 41  ar) ){.      REA
b4dc0 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b  D_UTF16BE(z, c);
b4dd0 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20  .      n++;.    
b4de0 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77  }.  }else{.    w
b4df0 68 69 6c 65 28 20 63 20 26 26 20 28 28 6e 43 68  hile( c && ((nCh
b4e00 61 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68 61 72  ar<0) || n<nChar
b4e10 29 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f  ) ){.      READ_
b4e20 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20  UTF16LE(z, c);. 
b4e30 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a       n++;.    }.
b4e40 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e    }.  return (in
b4e50 74 29 28 7a 2d 28 63 68 61 72 20 63 6f 6e 73 74  t)(z-(char const
b4e60 20 2a 29 7a 49 6e 29 2d 28 28 63 3d 3d 30 29 3f   *)zIn)-((c==0)?
b4e70 32 3a 30 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66  2:0);.}..#if def
b4e80 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
b4e90 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  )./*.** This rou
b4ea0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66  tine is called f
b4eb0 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74  rom the TCL test
b4ec0 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73   function "trans
b4ed0 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a  late_selftest"..
b4ee0 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61  ** It checks tha
b4ef0 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73  t the primitives
b4f00 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67   for serializing
b4f10 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69   and deserializi
b4f20 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  ng.** characters
b4f30 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e   in each encodin
b4f40 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f  g are inverses o
b4f50 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f  f each other..*/
b4f60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b4f70 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53  void sqlite3UtfS
b4f80 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20  elfTest(void){. 
b4f90 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c   unsigned int i,
b4fa0 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   t;.  unsigned c
b4fb0 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20  har zBuf[20];.  
b4fc0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
b4fd0 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
b4fe0 72 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 6e 74 20  r *zTerm;.  int 
b4ff0 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  n;.  unsigned in
b5000 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  t c;..  for(i=0;
b5010 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69   i<0x00110000; i
b5020 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75  ++){.    z = zBu
b5030 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46  f;.    WRITE_UTF
b5040 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d  8(z, i);.    n =
b5050 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a   (int)(z-zBuf);.
b5060 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20      assert( n>0 
b5070 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a  && n<=4 );.    z
b5080 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 54 65  [0] = 0;.    zTe
b5090 72 6d 20 3d 20 7a 3b 0a 20 20 20 20 7a 20 3d 20  rm = z;.    z = 
b50a0 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71  zBuf;.    c = sq
b50b0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c  lite3Utf8Read(z,
b50c0 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75   zTerm, (const u
b50d0 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20 3d  8**)&z);.    t =
b50e0 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30   i;.    if( i>=0
b50f0 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44 46  xD800 && i<=0xDF
b5100 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b  FF ) t = 0xFFFD;
b5110 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 46 46  .    if( (i&0xFF
b5120 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20  FFFFFE)==0xFFFE 
b5130 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20 20  ) t = 0xFFFD;.  
b5140 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20 29    assert( c==t )
b5150 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a  ;.    assert( (z
b5160 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d  -zBuf)==n );.  }
b5170 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78  .  for(i=0; i<0x
b5180 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a  00110000; i++){.
b5190 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30      if( i>=0xD80
b51a0 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 29 20  0 && i<0xE000 ) 
b51b0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20  continue;.    z 
b51c0 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54  = zBuf;.    WRIT
b51d0 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29 3b  E_UTF16LE(z, i);
b51e0 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a  .    n = (int)(z
b51f0 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65  -zBuf);.    asse
b5200 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20  rt( n>0 && n<=4 
b5210 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b  );.    z[0] = 0;
b5220 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20  .    z = zBuf;. 
b5230 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28     READ_UTF16LE(
b5240 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72  z, c);.    asser
b5250 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61  t( c==i );.    a
b5260 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d  ssert( (z-zBuf)=
b5270 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  =n );.  }.  for(
b5280 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30  i=0; i<0x0011000
b5290 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  0; i++){.    if(
b52a0 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c   i>=0xD800 && i<
b52b0 30 78 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75  0xE000 ) continu
b52c0 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b  e;.    z = zBuf;
b52d0 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36  .    WRITE_UTF16
b52e0 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20  BE(z, i);.    n 
b52f0 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b  = (int)(z-zBuf);
b5300 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30  .    assert( n>0
b5310 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20   && n<=4 );.    
b5320 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20  z[0] = 0;.    z 
b5330 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45 41 44  = zBuf;.    READ
b5340 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a  _UTF16BE(z, c);.
b5350 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 69      assert( c==i
b5360 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
b5370 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20  (z-zBuf)==n );. 
b5380 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53   }.}.#endif /* S
b5390 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65  QLITE_TEST */.#e
b53a0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
b53b0 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
b53c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
b53d0 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a  d of utf.c *****
b53e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b53f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b5410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
b5420 67 69 6e 20 66 69 6c 65 20 75 74 69 6c 2e 63 20  gin file util.c 
b5430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b5460 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
b5470 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
b5480 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
b5490 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
b54a0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
b54b0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
b54c0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
b54d0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
b54e0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
b54f0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
b5500 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
b5510 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
b5520 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
b5530 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
b5540 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
b5550 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
b5560 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
b5570 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
b5580 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
b5590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b55a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b55b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b55c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b55d0 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20  ****.** Utility 
b55e0 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  functions used t
b55f0 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65  hroughout sqlite
b5600 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ..**.** This fil
b5610 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74  e contains funct
b5620 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74  ions for allocat
b5630 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70  ing memory, comp
b5640 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73  aring.** strings
b5650 2c 20 61 6e 64 20 73 74 75 66 66 20 6c 69 6b 65  , and stuff like
b5660 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   that..**.** $Id
b5670 3a 20 75 74 69 6c 2e 63 2c 76 20 31 2e 32 34 36  : util.c,v 1.246
b5680 20 32 30 30 39 2f 30 31 2f 31 30 20 31 36 3a 31   2009/01/10 16:1
b5690 35 3a 32 32 20 64 72 68 20 45 78 70 20 24 0a 2a  5:22 drh Exp $.*
b56a0 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  /.../*.** Routin
b56b0 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70  e needed to supp
b56c0 6f 72 74 20 74 68 65 20 74 65 73 74 63 61 73 65  ort the testcase
b56d0 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66  () macro..*/.#if
b56e0 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52  def SQLITE_COVER
b56f0 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  AGE_TEST.SQLITE_
b5700 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
b5710 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74  ite3Coverage(int
b5720 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e   x){.  static in
b5730 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 64  t dummy = 0;.  d
b5740 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65 6e  ummy += x;.}.#en
b5750 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  dif../*.** Routi
b5760 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70  ne needed to sup
b5770 70 6f 72 74 20 74 68 65 20 41 4c 57 41 59 53 28  port the ALWAYS(
b5780 29 20 61 6e 64 20 4e 45 56 45 52 28 29 20 6d 61  ) and NEVER() ma
b5790 63 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  cros..**.** The 
b57a0 61 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c 57 41  argument to ALWA
b57b0 59 53 28 29 20 73 68 6f 75 6c 64 20 61 6c 77 61  YS() should alwa
b57c0 79 73 20 62 65 20 74 72 75 65 20 61 6e 64 20 74  ys be true and t
b57d0 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  he argument.** t
b57e0 6f 20 4e 45 56 45 52 28 29 20 73 68 6f 75 6c 64  o NEVER() should
b57f0 20 61 6c 77 61 79 73 20 62 65 20 66 61 6c 73 65   always be false
b5800 2e 20 20 49 66 20 65 69 74 68 65 72 20 69 73 20  .  If either is 
b5810 6e 6f 74 20 74 68 65 20 63 61 73 65 0a 2a 2a 20  not the case.** 
b5820 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
b5830 65 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 6f  e is called in o
b5840 72 64 65 72 20 74 6f 20 74 68 72 6f 77 20 61 6e  rder to throw an
b5850 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68   error..**.** Th
b5860 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  is routine only 
b5870 65 78 69 73 74 73 20 69 66 20 61 73 73 65 72 74  exists if assert
b5880 28 29 20 69 73 20 6f 70 65 72 61 74 69 6f 6e 61  () is operationa
b5890 6c 2e 20 20 49 74 20 61 6c 77 61 79 73 0a 2a 2a  l.  It always.**
b58a0 20 74 68 72 6f 77 73 20 61 6e 20 61 73 73 65 72   throws an asser
b58b0 74 20 6f 6e 20 69 74 73 20 66 69 72 73 74 20 69  t on its first i
b58c0 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20  nvocation.  The 
b58d0 76 61 72 69 61 62 6c 65 20 68 61 73 20 61 20 6c  variable has a l
b58e0 6f 6e 67 0a 2a 2a 20 6e 61 6d 65 20 74 6f 20 68  ong.** name to h
b58f0 65 6c 70 20 74 68 65 20 61 73 73 65 72 74 28 29  elp the assert()
b5900 20 6d 65 73 73 61 67 65 20 62 65 20 6d 6f 72 65   message be more
b5910 20 72 65 61 64 61 62 6c 65 2e 20 20 54 68 65 20   readable.  The 
b5920 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 75  variable.** is u
b5930 73 65 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61  sed to prevent a
b5940 20 74 6f 6f 2d 63 6c 65 76 65 72 20 6f 70 74 69   too-clever opti
b5950 6d 69 7a 65 72 20 66 72 6f 6d 20 6f 70 74 69 6d  mizer from optim
b5960 69 7a 69 6e 67 20 6f 75 74 20 74 68 65 0a 2a 2a  izing out the.**
b5970 20 65 6e 74 69 72 65 20 63 61 6c 6c 2e 0a 2a 2f   entire call..*/
b5980 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
b5990 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b59a0 6e 74 20 73 71 6c 69 74 65 33 41 73 73 65 72 74  nt sqlite3Assert
b59b0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
b59c0 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 41 4c   volatile int AL
b59d0 57 41 59 53 5f 77 61 73 5f 66 61 6c 73 65 5f 6f  WAYS_was_false_o
b59e0 72 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72 75 65  r_NEVER_was_true
b59f0 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
b5a00 41 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c 73 65  ALWAYS_was_false
b5a10 5f 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72  _or_NEVER_was_tr
b5a20 75 65 20 29 3b 20 20 20 20 20 20 2f 2a 20 41 6c  ue );      /* Al
b5a30 77 61 79 73 20 66 61 69 6c 73 20 2a 2f 0a 20 20  ways fails */.  
b5a40 72 65 74 75 72 6e 20 41 4c 57 41 59 53 5f 77 61  return ALWAYS_wa
b5a50 73 5f 66 61 6c 73 65 5f 6f 72 5f 4e 45 56 45 52  s_false_or_NEVER
b5a60 5f 77 61 73 5f 74 72 75 65 2b 2b 3b 20 20 20 20  _was_true++;    
b5a70 20 20 20 2f 2a 20 4e 6f 74 20 52 65 61 63 68 65     /* Not Reache
b5a80 64 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  d */.}.#endif../
b5a90 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
b5aa0 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67   if the floating
b5ab0 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20   point value is 
b5ac0 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e 61  Not a Number (Na
b5ad0 4e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  N)..*/.SQLITE_PR
b5ae0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b5af0 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29  3IsNaN(double x)
b5b00 7b 0a 20 20 2f 2a 20 54 68 69 73 20 4e 61 4e 20  {.  /* This NaN 
b5b10 74 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66  test sometimes f
b5b20 61 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64  ails if compiled
b5b30 20 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66   on GCC with -ff
b5b40 61 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f  ast-math..  ** O
b5b50 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64  n the other hand
b5b60 2c 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66  , the use of -ff
b5b70 61 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77  ast-math comes w
b5b80 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ith the followin
b5b90 67 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a  g.  ** warning:.
b5ba0 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54    **.  **      T
b5bb0 68 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61  his option [-ffa
b5bc0 73 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20  st-math] should 
b5bd0 6e 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20  never be turned 
b5be0 6f 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20  on by any.  **  
b5bf0 20 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69      -O option si
b5c00 6e 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c  nce it can resul
b5c10 74 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f  t in incorrect o
b5c20 75 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61  utput for progra
b5c30 6d 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69  ms.  **      whi
b5c40 63 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20  ch depend on an 
b5c50 65 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61  exact implementa
b5c60 74 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20  tion of IEEE or 
b5c70 49 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72  ISO .  **      r
b5c80 75 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69  ules/specificati
b5c90 6f 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e  ons for math fun
b5ca0 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ctions..  **.  *
b5cb0 2a 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68  * Under MSVC, th
b5cc0 69 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20  is NaN test may 
b5cd0 66 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64  fail if compiled
b5ce0 20 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67   with a floating
b5cf0 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65  -.  ** point pre
b5d00 63 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65  cision mode othe
b5d10 72 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69  r than /fp:preci
b5d20 73 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53  se.  From the MS
b5d30 44 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e  DN .  ** documen
b5d40 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a  tation:.  **.  *
b5d50 2a 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69  *      The compi
b5d60 6c 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72  ler [with /fp:pr
b5d70 65 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70  ecise] will prop
b5d80 65 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70  erly handle comp
b5d90 61 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20  arisons .  **   
b5da0 20 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e     involving NaN
b5db0 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78  . For example, x
b5dc0 20 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20   != x evaluates 
b5dd0 74 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20  to true if x is 
b5de0 4e 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e  NaN .  **      .
b5df0 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f  ...  */.#ifdef _
b5e00 5f 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65  _FAST_MATH__.# e
b5e10 72 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c  rror SQLite will
b5e20 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63   not work correc
b5e30 74 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66  tly with the -ff
b5e40 61 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20  ast-math option 
b5e50 6f 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20  of GCC..#endif. 
b5e60 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65   volatile double
b5e70 20 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69   y = x;.  volati
b5e80 6c 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b  le double z = y;
b5e90 0a 20 20 72 65 74 75 72 6e 20 79 21 3d 7a 3b 0a  .  return y!=z;.
b5ea0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  }../*.** Compute
b5eb0 20 61 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68   a string length
b5ec0 20 74 68 61 74 20 69 73 20 6c 69 6d 69 74 65 64   that is limited
b5ed0 20 74 6f 20 77 68 61 74 20 63 61 6e 20 62 65 20   to what can be 
b5ee0 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 6c 6f 77  stored in.** low
b5ef0 65 72 20 33 30 20 62 69 74 73 20 6f 66 20 61 20  er 30 bits of a 
b5f00 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  32-bit signed in
b5f10 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  teger..*/.SQLITE
b5f20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b5f30 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e  ite3Strlen30(con
b5f40 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63  st char *z){.  c
b5f50 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20  onst char *z2 = 
b5f60 7a 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 32 20  z;.  while( *z2 
b5f70 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74  ){ z2++; }.  ret
b5f80 75 72 6e 20 30 78 33 66 66 66 66 66 66 66 20 26  urn 0x3fffffff &
b5f90 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a   (int)(z2 - z);.
b5fa0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
b5fb0 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 61 20  the length of a 
b5fc0 73 74 72 69 6e 67 2c 20 65 78 63 65 70 74 20 64  string, except d
b5fd0 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 65 20  o not allow the 
b5fe0 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 0a 2a 2a  string length.**
b5ff0 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 53   to exceed the S
b6000 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
b6010 54 48 20 73 65 74 74 69 6e 67 2e 0a 2a 2f 0a 53  TH setting..*/.S
b6020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b6030 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28  t sqlite3Strlen(
b6040 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
b6050 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63  st char *z){.  c
b6060 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20  onst char *z2 = 
b6070 7a 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20  z;.  int len;.  
b6080 69 6e 74 20 78 3b 0a 20 20 77 68 69 6c 65 28 20  int x;.  while( 
b6090 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20  *z2 ){ z2++; }. 
b60a0 20 78 20 3d 20 28 69 6e 74 29 28 7a 32 20 2d 20   x = (int)(z2 - 
b60b0 7a 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 78 37 66  z);.  len = 0x7f
b60c0 66 66 66 66 66 66 20 26 20 78 3b 0a 20 20 69 66  ffffff & x;.  if
b60d0 28 20 6c 65 6e 21 3d 78 20 7c 7c 20 6c 65 6e 20  ( len!=x || len 
b60e0 3e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  > db->aLimit[SQL
b60f0 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
b6100 5d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ] ){.    return 
b6110 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
b6120 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b  E_LIMIT_LENGTH];
b6130 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
b6140 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 7d 0a  turn len;.  }.}.
b6150 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d  ./*.** Set the m
b6160 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
b6170 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20   code and error 
b6180 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 73  string for the s
b6190 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20  qlite.** handle 
b61a0 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f 72 20  "db". The error 
b61b0 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 22  code is set to "
b61c0 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a  err_code"..**.**
b61d0 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55   If it is not NU
b61e0 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d  LL, string zForm
b61f0 61 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65  at specifies the
b6200 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a   format of the.*
b6210 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 69  * error string i
b6220 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66 20 74  n the style of t
b6230 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63 74 69  he printf functi
b6240 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ons: The followi
b6250 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61  ng.** format cha
b6260 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f  racters are allo
b6270 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  wed:.**.**      
b6280 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61  %s      Insert a
b6290 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20   string.**      
b62a0 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67  %z      A string
b62b0 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
b62c0 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a  freed after use.
b62d0 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20  **      %d      
b62e0 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65  Insert an intege
b62f0 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20  r.**      %T    
b6300 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e    Insert a token
b6310 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20  .**      %S     
b6320 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73   Insert the firs
b6330 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53  t element of a S
b6340 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f  rcList.**.** zFo
b6350 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 74 72  rmat and any str
b6360 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 74 20  ing tokens that 
b6370 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 61 73  follow it are as
b6380 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65  sumed to be.** e
b6390 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e  ncoded in UTF-8.
b63a0 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20  .**.** To clear 
b63b0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
b63c0 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 74 65  error for sqlite
b63d0 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 73 71   handle "db", sq
b63e0 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68  lite3Error.** sh
b63f0 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77  ould be called w
b6400 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73 65 74  ith err_code set
b6410 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e   to SQLITE_OK an
b6420 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a  d zFormat set.**
b6430 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c   to NULL..*/.SQL
b6440 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b6450 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71   sqlite3Error(sq
b6460 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65  lite3 *db, int e
b6470 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63  rr_code, const c
b6480 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e  har *zFormat, ..
b6490 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20  .){.  if( db && 
b64a0 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62  (db->pErr || (db
b64b0 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 65 33  ->pErr = sqlite3
b64c0 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21 3d 30  ValueNew(db))!=0
b64d0 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72  ) ){.    db->err
b64e0 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b  Code = err_code;
b64f0 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d 61 74  .    if( zFormat
b6500 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a   ){.      char *
b6510 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 74  z;.      va_list
b6520 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f 73 74   ap;.      va_st
b6530 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
b6540 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69  ;.      z = sqli
b6550 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20  te3VMPrintf(db, 
b6560 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
b6570 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a      va_end(ap);.
b6580 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
b6590 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72  ueSetStr(db->pEr
b65a0 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45  r, -1, z, SQLITE
b65b0 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59  _UTF8, SQLITE_DY
b65c0 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73  NAMIC);.    }els
b65d0 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
b65e0 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e  ValueSetStr(db->
b65f0 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49  pErr, 0, 0, SQLI
b6600 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
b6610 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20  STATIC);.    }. 
b6620 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
b6630 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
b6640 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   to pParse->zErr
b6650 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e  Msg and incremen
b6660 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a  t pParse->nErr..
b6670 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
b6680 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 61 72   formatting char
b6690 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77  acters are allow
b66a0 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25  ed:.**.**      %
b66b0 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20  s      Insert a 
b66c0 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25  string.**      %
b66d0 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20  z      A string 
b66e0 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66  that should be f
b66f0 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a  reed after use.*
b6700 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49  *      %d      I
b6710 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72  nsert an integer
b6720 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20  .**      %T     
b6730 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a   Insert a token.
b6740 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20  **      %S      
b6750 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74  Insert the first
b6760 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72   element of a Sr
b6770 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73  cList.**.** This
b6780 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
b6790 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 70 6f   be used to repo
b67a0 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74 68 61  rt any error tha
b67b0 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73 74 0a  t occurs whilst.
b67c0 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20  ** compiling an 
b67d0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 28 69  SQL statement (i
b67e0 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c 69 74  .e. within sqlit
b67f0 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e 20 54  e3_prepare()). T
b6800 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67  he.** last thing
b6810 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   the sqlite3_pre
b6820 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f 6e 20  pare() function 
b6830 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74 68 65  does is copy the
b6840 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64   error.** stored
b6850 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
b6860 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  n into the datab
b6870 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69 6e 67  ase handle using
b6880 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e   sqlite3Error().
b6890 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c  .** Function sql
b68a0 69 74 65 33 45 72 72 6f 72 28 29 20 73 68 6f 75  ite3Error() shou
b68b0 6c 64 20 62 65 20 75 73 65 64 20 64 75 72 69 6e  ld be used durin
b68c0 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78 65 63  g statement exec
b68d0 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65  ution.** (sqlite
b68e0 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29 2e 0a  3_step() etc.)..
b68f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b6900 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72  E void sqlite3Er
b6910 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a 70 50  rorMsg(Parse *pP
b6920 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
b6930 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
b6940 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
b6950 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
b6960 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61  Parse->db;.  pPa
b6970 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 73  rse->nErr++;.  s
b6980 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
b6990 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
b69a0 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  );.  va_start(ap
b69b0 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50  , zFormat);.  pP
b69c0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
b69d0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
b69e0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
b69f0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
b6a00 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63    if( pParse->rc
b6a10 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
b6a20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
b6a30 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
b6a40 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  }.}../*.** Clear
b6a50 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
b6a60 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66  ge in pParse, if
b6a70 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50   any.*/.SQLITE_P
b6a80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
b6a90 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61  te3ErrorClear(Pa
b6aa0 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
b6ab0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50  sqlite3DbFree(pP
b6ac0 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65  arse->db, pParse
b6ad0 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50  ->zErrMsg);.  pP
b6ae0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
b6af0 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  0;.  pParse->nEr
b6b00 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r = 0;.}../*.** 
b6b10 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73  Convert an SQL-s
b6b20 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69  tyle quoted stri
b6b30 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c  ng into a normal
b6b40 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76   string by remov
b6b50 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65  ing.** the quote
b6b60 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68   characters.  Th
b6b70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  e conversion is 
b6b80 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20  done in-place.  
b6b90 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20  If the.** input 
b6ba0 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77  does not begin w
b6bb0 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72  ith a quote char
b6bc0 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73  acter, then this
b6bd0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61   routine.** is a
b6be0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 32 30   no-op..**.** 20
b6bf0 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69 73 20  02-Feb-14: This 
b6c00 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74 65 6e  routine is exten
b6c10 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 4d 53  ded to remove MS
b6c20 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a 2a 2a  -Access style.**
b6c30 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d 20 61   brackets from a
b6c40 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65 72 73  round identifers
b6c50 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 20  .  For example: 
b6c60 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63 6f 6d   "[a-b-c]" becom
b6c70 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e 0a 2a  es.** "a-b-c"..*
b6c80 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b6c90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 71   void sqlite3Deq
b6ca0 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20  uote(char *z){. 
b6cb0 20 63 68 61 72 20 71 75 6f 74 65 3b 0a 20 20 69   char quote;.  i
b6cc0 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a  nt i, j;.  if( z
b6cd0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
b6ce0 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20  quote = z[0];.  
b6cf0 73 77 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b  switch( quote ){
b6d00 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20  .    case '\'': 
b6d10 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
b6d20 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20   '"':   break;. 
b6d30 20 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 62     case '`':   b
b6d40 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20  reak;           
b6d50 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51       /* For MySQ
b6d60 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  L compatibility 
b6d70 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a  */.    case '[':
b6d80 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20     quote = ']'; 
b6d90 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20   break;  /* For 
b6da0 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d  MS SqlServer com
b6db0 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20  patibility */.  
b6dc0 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65    default:    re
b6dd0 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
b6de0 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20  i=1, j=0; z[i]; 
b6df0 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b  i++){.    if( z[
b6e00 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20  i]==quote ){.   
b6e10 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71     if( z[i+1]==q
b6e20 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  uote ){.        
b6e30 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a  z[j++] = quote;.
b6e40 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
b6e50 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
b6e60 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20    z[j++] = 0;.  
b6e70 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
b6e80 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
b6e90 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
b6ea0 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  [i];.    }.  }.}
b6eb0 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65 6e 74 20  ../* Convenient 
b6ec0 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f 0a 23 64  short-hand */.#d
b6ed0 65 66 69 6e 65 20 55 70 70 65 72 54 6f 4c 6f 77  efine UpperToLow
b6ee0 65 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54  er sqlite3UpperT
b6ef0 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f  oLower../*.** So
b6f00 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76 65 20  me systems have 
b6f10 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74 68 65  stricmp().  Othe
b6f20 72 73 20 68 61 76 65 20 73 74 72 63 61 73 65 63  rs have strcasec
b6f30 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65 0a 2a  mp().  Because.*
b6f40 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 6f  * there is no co
b6f50 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20 77 69  nsistency, we wi
b6f60 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77  ll define our ow
b6f70 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
b6f80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b6f90 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68  StrICmp(const ch
b6fa0 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74  ar *zLeft, const
b6fb0 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b 0a   char *zRight){.
b6fc0 20 20 72 65 67 69 73 74 65 72 20 75 6e 73 69 67    register unsig
b6fd0 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b  ned char *a, *b;
b6fe0 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65 64  .  a = (unsigned
b6ff0 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20   char *)zLeft;. 
b7000 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63   b = (unsigned c
b7010 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20  har *)zRight;.  
b7020 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 26 26 20  while( *a!=0 && 
b7030 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d  UpperToLower[*a]
b7040 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  ==UpperToLower[*
b7050 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d  b]){ a++; b++; }
b7060 0a 20 20 72 65 74 75 72 6e 20 55 70 70 65 72 54  .  return UpperT
b7070 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70  oLower[*a] - Upp
b7080 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d  erToLower[*b];.}
b7090 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b70a0 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 4e 49  int sqlite3StrNI
b70b0 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Cmp(const char *
b70c0 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61  zLeft, const cha
b70d0 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74 20 4e  r *zRight, int N
b70e0 29 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e  ){.  register un
b70f0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20  signed char *a, 
b7100 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67  *b;.  a = (unsig
b7110 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74  ned char *)zLeft
b7120 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65  ;.  b = (unsigne
b7130 64 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b  d char *)zRight;
b7140 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20  .  while( N-- > 
b7150 30 20 26 26 20 2a 61 21 3d 30 20 26 26 20 55 70  0 && *a!=0 && Up
b7160 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d  perToLower[*a]==
b7170 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d  UpperToLower[*b]
b7180 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20  ){ a++; b++; }. 
b7190 20 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20 30 20   return N<0 ? 0 
b71a0 3a 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  : UpperToLower[*
b71b0 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65  a] - UpperToLowe
b71c0 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r[*b];.}../*.** 
b71d0 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a  Return TRUE if z
b71e0 20 69 73 20 61 20 70 75 72 65 20 6e 75 6d 65 72   is a pure numer
b71f0 69 63 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75  ic string.  Retu
b7200 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 0a  rn FALSE if the.
b7210 2a 2a 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69  ** string contai
b7220 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74 65 72  ns any character
b7230 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 70 61   which is not pa
b7240 72 74 20 6f 66 20 61 20 6e 75 6d 62 65 72 2e 20  rt of a number. 
b7250 49 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  If.** the string
b7260 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20   is numeric and 
b7270 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 27 2e 27  contains the '.'
b7280 20 63 68 61 72 61 63 74 65 72 2c 20 73 65 74 20   character, set 
b7290 2a 72 65 61 6c 6e 75 6d 0a 2a 2a 20 74 6f 20 54  *realnum.** to T
b72a0 52 55 45 20 28 6f 74 68 65 72 77 69 73 65 20 46  RUE (otherwise F
b72b0 41 4c 53 45 29 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  ALSE)..**.** An 
b72c0 65 6d 70 74 79 20 73 74 72 69 6e 67 20 69 73 20  empty string is 
b72d0 63 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e 2d 6e  considered non-n
b72e0 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54  umeric..*/.SQLIT
b72f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b7300 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f  lite3IsNumber(co
b7310 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
b7320 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20 65 6e   *realnum, u8 en
b7330 63 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72 20 3d  c){.  int incr =
b7340 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54   (enc==SQLITE_UT
b7350 46 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28 20 65  F8?1:2);.  if( e
b7360 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
b7370 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20  BE ) z++;.  if( 
b7380 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27  *z=='-' || *z=='
b7390 2b 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a  +' ) z += incr;.
b73a0 20 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a    if( !isdigit(*
b73b0 28 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 72  (u8*)z) ){.    r
b73c0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a  eturn 0;.  }.  z
b73d0 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 28 20   += incr;.  if( 
b73e0 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65 61 6c 6e  realnum ) *realn
b73f0 75 6d 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  um = 0;.  while(
b7400 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a   isdigit(*(u8*)z
b7410 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20  ) ){ z += incr; 
b7420 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20  }.  if( *z=='.' 
b7430 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72  ){.    z += incr
b7440 3b 0a 20 20 20 20 69 66 28 20 21 69 73 64 69 67  ;.    if( !isdig
b7450 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 20 72 65  it(*(u8*)z) ) re
b7460 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c  turn 0;.    whil
b7470 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a  e( isdigit(*(u8*
b7480 29 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72  )z) ){ z += incr
b7490 3b 20 7d 0a 20 20 20 20 69 66 28 20 72 65 61 6c  ; }.    if( real
b74a0 6e 75 6d 20 29 20 2a 72 65 61 6c 6e 75 6d 20 3d  num ) *realnum =
b74b0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a   1;.  }.  if( *z
b74c0 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27  =='e' || *z=='E'
b74d0 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63   ){.    z += inc
b74e0 72 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27  r;.    if( *z=='
b74f0 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20  +' || *z=='-' ) 
b7500 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69  z += incr;.    i
b7510 66 28 20 21 69 73 64 69 67 69 74 28 2a 28 75 38  f( !isdigit(*(u8
b7520 2a 29 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b  *)z) ) return 0;
b7530 0a 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 69  .    while( isdi
b7540 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 20  git(*(u8*)z) ){ 
b7550 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20  z += incr; }.   
b7560 20 69 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a   if( realnum ) *
b7570 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d  realnum = 1;.  }
b7580 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b  .  return *z==0;
b7590 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74  .}../*.** The st
b75a0 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e 20 61  ring z[] is an a
b75b0 73 63 69 69 20 72 65 70 72 65 73 65 6e 74 61 74  scii representat
b75c0 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20 6e 75  ion of a real nu
b75d0 6d 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74  mber..** Convert
b75e0 20 74 68 69 73 20 73 74 72 69 6e 67 20 74 6f 20   this string to 
b75f0 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  a double..**.** 
b7600 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 73  This routine ass
b7610 75 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20 72 65  umes that z[] re
b7620 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20  ally is a valid 
b7630 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 0a 2a  number.  If it.*
b7640 2a 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 72 65  * is not, the re
b7650 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65  sult is undefine
b7660 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
b7670 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e  utine is used in
b7680 73 74 65 61 64 20 6f 66 20 74 68 65 20 6c 69 62  stead of the lib
b7690 72 61 72 79 20 61 74 6f 66 28 29 20 66 75 6e 63  rary atof() func
b76a0 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20  tion because.** 
b76b0 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66  the library atof
b76c0 28 29 20 6d 69 67 68 74 20 77 61 6e 74 20 74 6f  () might want to
b76d0 20 75 73 65 20 22 2c 22 20 61 73 20 74 68 65 20   use "," as the 
b76e0 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e  decimal point in
b76f0 73 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 20  stead.** of "." 
b7700 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77  depending on how
b7710 20 6c 6f 63 61 6c 65 20 69 73 20 73 65 74 2e 20   locale is set. 
b7720 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64 20   But that would 
b7730 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 0a 2a  cause problems.*
b7740 2a 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f 20 74  * for SQL.  So t
b7750 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 61  his routine alwa
b7760 79 73 20 75 73 65 73 20 22 2e 22 20 72 65 67 61  ys uses "." rega
b7770 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65  rdless of locale
b7780 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b7790 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41  ATE int sqlite3A
b77a0 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  toF(const char *
b77b0 7a 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65 73 75  z, double *pResu
b77c0 6c 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  lt){.#ifndef SQL
b77d0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
b77e0 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 73 69  G_POINT.  int si
b77f0 67 6e 20 3d 20 31 3b 0a 20 20 63 6f 6e 73 74 20  gn = 1;.  const 
b7800 63 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d 20 7a  char *zBegin = z
b7810 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54  ;.  LONGDOUBLE_T
b7820 59 50 45 20 76 31 20 3d 20 30 2e 30 3b 0a 20 20  YPE v1 = 0.0;.  
b7830 69 6e 74 20 6e 53 69 67 6e 69 66 69 63 61 6e 74  int nSignificant
b7840 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69   = 0;.  while( i
b7850 73 73 70 61 63 65 28 2a 28 75 38 2a 29 7a 29 20  sspace(*(u8*)z) 
b7860 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d  ) z++;.  if( *z=
b7870 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e  ='-' ){.    sign
b7880 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a   = -1;.    z++;.
b7890 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d    }else if( *z==
b78a0 27 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a  '+' ){.    z++;.
b78b0 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30    }.  while( z[0
b78c0 5d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 2b  ]=='0' ){.    z+
b78d0 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  +;.  }.  while( 
b78e0 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29  isdigit(*(u8*)z)
b78f0 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 76 31 2a   ){.    v1 = v1*
b7900 31 30 2e 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27  10.0 + (*z - '0'
b7910 29 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  );.    z++;.    
b7920 6e 53 69 67 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a  nSignificant++;.
b7930 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e    }.  if( *z=='.
b7940 27 20 29 7b 0a 20 20 20 20 4c 4f 4e 47 44 4f 55  ' ){.    LONGDOU
b7950 42 4c 45 5f 54 59 50 45 20 64 69 76 69 73 6f 72  BLE_TYPE divisor
b7960 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b 2b 3b   = 1.0;.    z++;
b7970 0a 20 20 20 20 69 66 28 20 6e 53 69 67 6e 69 66  .    if( nSignif
b7980 69 63 61 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  icant==0 ){.    
b7990 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27    while( z[0]=='
b79a0 30 27 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69  0' ){.        di
b79b0 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b 0a 20  visor *= 10.0;. 
b79c0 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20         z++;.    
b79d0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77 68    }.    }.    wh
b79e0 69 6c 65 28 20 69 73 64 69 67 69 74 28 2a 28 75  ile( isdigit(*(u
b79f0 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 20 20 69  8*)z) ){.      i
b7a00 66 28 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 3c  f( nSignificant<
b7a10 31 38 20 29 7b 0a 20 20 20 20 20 20 20 20 76 31  18 ){.        v1
b7a20 20 3d 20 76 31 2a 31 30 2e 30 20 2b 20 28 2a 7a   = v1*10.0 + (*z
b7a30 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 20   - '0');.       
b7a40 20 64 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30   divisor *= 10.0
b7a50 3b 0a 20 20 20 20 20 20 20 20 6e 53 69 67 6e 69  ;.        nSigni
b7a60 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 20 20 20 20  ficant++;.      
b7a70 7d 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20  }.      z++;.   
b7a80 20 7d 0a 20 20 20 20 76 31 20 2f 3d 20 64 69 76   }.    v1 /= div
b7a90 69 73 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20  isor;.  }.  if( 
b7aa0 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27  *z=='e' || *z=='
b7ab0 45 27 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 73  E' ){.    int es
b7ac0 69 67 6e 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74  ign = 1;.    int
b7ad0 20 65 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 4c   eval = 0;.    L
b7ae0 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 73  ONGDOUBLE_TYPE s
b7af0 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20  cale = 1.0;.    
b7b00 7a 2b 2b 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d  z++;.    if( *z=
b7b10 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 65 73  ='-' ){.      es
b7b20 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  ign = -1;.      
b7b30 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  z++;.    }else i
b7b40 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20  f( *z=='+' ){.  
b7b50 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20      z++;.    }. 
b7b60 20 20 20 77 68 69 6c 65 28 20 69 73 64 69 67 69     while( isdigi
b7b70 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a 20 20  t(*(u8*)z) ){.  
b7b80 20 20 20 20 65 76 61 6c 20 3d 20 65 76 61 6c 2a      eval = eval*
b7b90 31 30 20 2b 20 2a 7a 20 2d 20 27 30 27 3b 0a 20  10 + *z - '0';. 
b7ba0 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a       z++;.    }.
b7bb0 20 20 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e      while( eval>
b7bc0 3d 36 34 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20  =64 ){ scale *= 
b7bd0 31 2e 30 65 2b 36 34 3b 20 65 76 61 6c 20 2d 3d  1.0e+64; eval -=
b7be0 20 36 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65   64; }.    while
b7bf0 28 20 65 76 61 6c 3e 3d 31 36 20 29 7b 20 73 63  ( eval>=16 ){ sc
b7c00 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 36 3b 20  ale *= 1.0e+16; 
b7c10 65 76 61 6c 20 2d 3d 20 31 36 3b 20 7d 0a 20 20  eval -= 16; }.  
b7c20 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 34    while( eval>=4
b7c30 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30   ){ scale *= 1.0
b7c40 65 2b 34 3b 20 65 76 61 6c 20 2d 3d 20 34 3b 20  e+4; eval -= 4; 
b7c50 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76 61  }.    while( eva
b7c60 6c 3e 3d 31 20 29 7b 20 73 63 61 6c 65 20 2a 3d  l>=1 ){ scale *=
b7c70 20 31 2e 30 65 2b 31 3b 20 65 76 61 6c 20 2d 3d   1.0e+1; eval -=
b7c80 20 31 3b 20 7d 0a 20 20 20 20 69 66 28 20 65 73   1; }.    if( es
b7c90 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 76  ign<0 ){.      v
b7ca0 31 20 2f 3d 20 73 63 61 6c 65 3b 0a 20 20 20 20  1 /= scale;.    
b7cb0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 31 20  }else{.      v1 
b7cc0 2a 3d 20 73 63 61 6c 65 3b 0a 20 20 20 20 7d 0a  *= scale;.    }.
b7cd0 20 20 7d 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d    }.  *pResult =
b7ce0 20 28 64 6f 75 62 6c 65 29 28 73 69 67 6e 3c 30   (double)(sign<0
b7cf0 20 3f 20 2d 76 31 20 3a 20 76 31 29 3b 0a 20 20   ? -v1 : v1);.  
b7d00 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 20 2d  return (int)(z -
b7d10 20 7a 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65 0a   zBegin);.#else.
b7d20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
b7d30 41 74 6f 69 36 34 28 7a 2c 20 70 52 65 73 75 6c  Atoi64(z, pResul
b7d40 74 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  t);.#endif /* SQ
b7d50 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
b7d60 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f  NG_POINT */.}../
b7d70 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65  *.** Compare the
b7d80 20 31 39 2d 63 68 61 72 61 63 74 65 72 20 73 74   19-character st
b7d90 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e 73  ring zNum agains
b7da0 74 20 74 68 65 20 74 65 78 74 20 72 65 70 72 65  t the text repre
b7db0 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 6c  sentation.** val
b7dc0 75 65 20 32 5e 36 33 3a 20 20 39 32 32 33 33 37  ue 2^63:  922337
b7dd0 32 30 33 36 38 35 34 37 37 35 38 30 38 2e 20 20  2036854775808.  
b7de0 52 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c  Return negative,
b7df0 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69   zero, or positi
b7e00 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 73  ve.** if zNum is
b7e10 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61   less than, equa
b7e20 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72  l to, or greater
b7e30 20 74 68 61 6e 20 74 68 65 20 73 74 72 69 6e 67   than the string
b7e40 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d  ..**.** Unlike m
b7e50 65 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f 75  emcmp() this rou
b7e60 74 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74 65  tine is guarante
b7e70 65 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65  ed to return the
b7e80 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 69   difference.** i
b7e90 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  n the values of 
b7ea0 74 68 65 20 6c 61 73 74 20 64 69 67 69 74 20 69  the last digit i
b7eb0 66 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65  f the only diffe
b7ec0 72 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65 0a  rence is in the.
b7ed0 2a 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20 20  ** last digit.  
b7ee0 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  So, for example,
b7ef0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 70  .**.**      comp
b7f00 61 72 65 32 70 6f 77 36 33 28 22 39 32 32 33 33  are2pow63("92233
b7f10 37 32 30 33 36 38 35 34 37 37 35 38 30 30 22 29  72036854775800")
b7f20 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75  .**.** will retu
b7f30 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63  rn -8..*/.static
b7f40 20 69 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f 77   int compare2pow
b7f50 36 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  63(const char *z
b7f60 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20  Num){.  int c;. 
b7f70 20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 6d   c = memcmp(zNum
b7f80 2c 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37  ,"92233720368547
b7f90 37 35 38 30 22 2c 31 38 29 3b 0a 20 20 69 66 28  7580",18);.  if(
b7fa0 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20 3d   c==0 ){.    c =
b7fb0 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 38 27 3b   zNum[18] - '8';
b7fc0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 3b  .  }.  return c;
b7fd0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .}.../*.** Retur
b7fe0 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 6d 20 69  n TRUE if zNum i
b7ff0 73 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65  s a 64-bit signe
b8000 64 20 69 6e 74 65 67 65 72 20 61 6e 64 20 77 72  d integer and wr
b8010 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  ite.** the value
b8020 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 20   of the integer 
b8030 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 49 66 20  into *pNum.  If 
b8040 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69  zNum is not an i
b8050 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 69 73 20  nteger.** or is 
b8060 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20  an integer that 
b8070 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20  is too large to 
b8080 62 65 20 65 78 70 72 65 73 73 65 64 20 77 69 74  be expressed wit
b8090 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a 20 74 68  h 64 bits,.** th
b80a0 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e  en return false.
b80b0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  .**.** When this
b80c0 20 72 6f 75 74 69 6e 65 20 77 61 73 20 6f 72 69   routine was ori
b80d0 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65 6e 20  ginally written 
b80e0 69 74 20 64 65 61 6c 74 20 77 69 74 68 20 6f 6e  it dealt with on
b80f0 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 6e 75 6d  ly.** 32-bit num
b8100 62 65 72 73 2e 20 20 41 74 20 74 68 61 74 20 74  bers.  At that t
b8110 69 6d 65 2c 20 69 74 20 77 61 73 20 6d 75 63 68  ime, it was much
b8120 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65   faster than the
b8130 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 62 72 61  .** atoi() libra
b8140 72 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 52 65  ry routine in Re
b8150 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51 4c  dHat 7.2..*/.SQL
b8160 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b8170 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63 6f  sqlite3Atoi64(co
b8180 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20  nst char *zNum, 
b8190 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 20 69 36  i64 *pNum){.  i6
b81a0 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  4 v = 0;.  int n
b81b0 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a  eg;.  int i, c;.
b81c0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
b81d0 74 61 72 74 3b 0a 20 20 77 68 69 6c 65 28 20 69  tart;.  while( i
b81e0 73 73 70 61 63 65 28 2a 28 75 38 2a 29 7a 4e 75  sspace(*(u8*)zNu
b81f0 6d 29 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 69  m) ) zNum++;.  i
b8200 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b  f( *zNum=='-' ){
b8210 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20  .    neg = 1;.  
b8220 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73    zNum++;.  }els
b8230 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27  e if( *zNum=='+'
b8240 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b   ){.    neg = 0;
b8250 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d  .    zNum++;.  }
b8260 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20  else{.    neg = 
b8270 30 3b 0a 20 20 7d 0a 20 20 7a 53 74 61 72 74 20  0;.  }.  zStart 
b8280 3d 20 7a 4e 75 6d 3b 0a 20 20 77 68 69 6c 65 28  = zNum;.  while(
b8290 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 7b   zNum[0]=='0' ){
b82a0 20 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a 20 53 6b   zNum++; } /* Sk
b82b0 69 70 20 6f 76 65 72 20 6c 65 61 64 69 6e 67 20  ip over leading 
b82c0 7a 65 72 6f 73 2e 20 54 69 63 6b 65 74 20 23 32  zeros. Ticket #2
b82d0 34 35 34 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  454 */.  for(i=0
b82e0 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27  ; (c=zNum[i])>='
b82f0 30 27 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b  0' && c<='9'; i+
b8300 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30  +){.    v = v*10
b8310 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 7d 0a   + c - '0';.  }.
b8320 20 20 2a 70 4e 75 6d 20 3d 20 6e 65 67 20 3f 20    *pNum = neg ? 
b8330 2d 76 20 3a 20 76 3b 0a 20 20 69 66 28 20 63 21  -v : v;.  if( c!
b8340 3d 30 20 7c 7c 20 28 69 3d 3d 30 20 26 26 20 7a  =0 || (i==0 && z
b8350 53 74 61 72 74 3d 3d 7a 4e 75 6d 29 20 7c 7c 20  Start==zNum) || 
b8360 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 7a  i>19 ){.    /* z
b8370 4e 75 6d 20 69 73 20 65 6d 70 74 79 20 6f 72 20  Num is empty or 
b8380 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d  contains non-num
b8390 65 72 69 63 20 74 65 78 74 20 6f 72 20 69 73 20  eric text or is 
b83a0 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20 74 68  longer.    ** th
b83b0 61 6e 20 31 39 20 64 69 67 69 74 73 20 28 74 68  an 19 digits (th
b83c0 75 73 20 67 75 61 72 61 6e 74 69 6e 67 20 74 68  us guaranting th
b83d0 61 74 20 69 74 20 69 73 20 74 6f 6f 20 6c 61 72  at it is too lar
b83e0 67 65 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ge) */.    retur
b83f0 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 0;.  }else if(
b8400 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20   i<19 ){.    /* 
b8410 4c 65 73 73 20 74 68 61 6e 20 31 39 20 64 69 67  Less than 19 dig
b8420 69 74 73 2c 20 73 6f 20 77 65 20 6b 6e 6f 77 20  its, so we know 
b8430 74 68 61 74 20 69 74 20 66 69 74 73 20 69 6e 20  that it fits in 
b8440 36 34 20 62 69 74 73 20 2a 2f 0a 20 20 20 20 72  64 bits */.    r
b8450 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65  eturn 1;.  }else
b8460 7b 0a 20 20 20 20 2f 2a 20 31 39 2d 64 69 67 69  {.    /* 19-digi
b8470 74 20 6e 75 6d 62 65 72 73 20 6d 75 73 74 20 62  t numbers must b
b8480 65 20 6e 6f 20 6c 61 72 67 65 72 20 74 68 61 6e  e no larger than
b8490 20 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37   922337203685477
b84a0 35 38 30 37 20 69 66 20 70 6f 73 69 74 69 76 65  5807 if positive
b84b0 0a 20 20 20 20 2a 2a 20 6f 72 20 39 32 32 33 33  .    ** or 92233
b84c0 37 32 30 33 36 38 35 34 37 37 35 38 30 38 20 69  72036854775808 i
b84d0 66 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74  f negative.  Not
b84e0 65 20 74 68 61 74 20 39 32 32 33 33 37 32 30 33  e that 922337203
b84f0 36 38 35 34 36 36 35 38 30 38 0a 20 20 20 20 2a  6854665808.    *
b8500 2a 20 69 73 20 32 5e 36 33 2e 20 2a 2f 0a 20 20  * is 2^63. */.  
b8510 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65    return compare
b8520 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67  2pow63(zNum)<neg
b8530 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
b8540 68 65 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 72  he string zNum r
b8550 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e 74  epresents an int
b8560 65 67 65 72 2e 20 20 54 68 65 72 65 20 6d 69 67  eger.  There mig
b8570 68 74 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72  ht be some other
b8580 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
b8590 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 69 6e  following the in
b85a0 74 65 67 65 72 20 74 6f 6f 2c 20 62 75 74 20 74  teger too, but t
b85b0 68 61 74 20 70 61 72 74 20 69 73 20 69 67 6e 6f  hat part is igno
b85c0 72 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69  red..** If the i
b85d0 6e 74 65 67 65 72 20 74 68 61 74 20 74 68 65 20  nteger that the 
b85e0 70 72 65 66 69 78 20 6f 66 20 7a 4e 75 6d 20 72  prefix of zNum r
b85f0 65 70 72 65 73 65 6e 74 73 20 77 69 6c 6c 20 66  epresents will f
b8600 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34 2d 62 69  it in a.** 64-bi
b8610 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
b8620 2c 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20  , return TRUE.  
b8630 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e  Otherwise return
b8640 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20 54 68   FALSE..**.** Th
b8650 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
b8660 6e 73 20 46 41 4c 53 45 20 66 6f 72 20 74 68 65  ns FALSE for the
b8670 20 73 74 72 69 6e 67 20 2d 39 32 32 33 33 37 32   string -9223372
b8680 30 33 36 38 35 34 37 37 35 38 30 38 20 65 76 65  036854775808 eve
b8690 6e 20 74 68 61 74 0a 2a 2a 20 74 68 61 74 20 6e  n that.** that n
b86a0 75 6d 62 65 72 20 77 69 6c 6c 2c 20 69 6e 20 74  umber will, in t
b86b0 68 65 6f 72 79 20 66 69 74 20 69 6e 20 61 20 36  heory fit in a 6
b86c0 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20 20  4-bit integer.  
b86d0 50 6f 73 69 74 69 76 65 0a 2a 2a 20 39 32 32 33  Positive.** 9223
b86e0 33 37 33 30 33 36 38 35 34 37 37 35 38 30 38 20  373036854775808 
b86f0 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 20  will not fit in 
b8700 36 34 20 62 69 74 73 2e 20 20 53 6f 20 69 74 20  64 bits.  So it 
b8710 73 65 65 6d 73 20 73 61 66 65 72 20 74 6f 20 72  seems safer to r
b8720 65 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 2e 0a  eturn.** false..
b8730 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b8740 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74  E int sqlite3Fit
b8750 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20  sIn64Bits(const 
b8760 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20  char *zNum, int 
b8770 6e 65 67 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20  negFlag){.  int 
b8780 69 2c 20 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20  i, c;.  int neg 
b8790 3d 20 30 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d  = 0;.  if( *zNum
b87a0 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67  =='-' ){.    neg
b87b0 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b   = 1;.    zNum++
b87c0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a  ;.  }else if( *z
b87d0 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20  Num=='+' ){.    
b87e0 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66  zNum++;.  }.  if
b87f0 28 20 6e 65 67 46 6c 61 67 20 29 20 6e 65 67 20  ( negFlag ) neg 
b8800 3d 20 31 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65  = 1-neg;.  while
b8810 28 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a  ( *zNum=='0' ){.
b8820 20 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a      zNum++;   /*
b8830 20 53 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65   Skip leading ze
b8840 72 6f 73 2e 20 20 54 69 63 6b 65 74 20 23 32 34  ros.  Ticket #24
b8850 35 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28  54 */.  }.  for(
b8860 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29  i=0; (c=zNum[i])
b8870 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 3b  >='0' && c<='9';
b8880 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 3c   i++){}.  if( i<
b8890 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61  19 ){.    /* Gua
b88a0 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 69  ranteed to fit i
b88b0 66 20 6c 65 73 73 20 74 68 61 6e 20 31 39 20 64  f less than 19 d
b88c0 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74  igits */.    ret
b88d0 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 1;.  }else i
b88e0 66 28 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f  f( i>19 ){.    /
b88f0 2a 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20  * Guaranteed to 
b8900 62 65 20 74 6f 6f 20 62 69 67 20 69 66 20 67 72  be too big if gr
b8910 65 61 74 65 72 20 74 68 61 6e 20 31 39 20 64 69  eater than 19 di
b8920 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75  gits */.    retu
b8930 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rn 0;.  }else{. 
b8940 20 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20 61 67     /* Compare ag
b8950 61 69 6e 73 74 20 32 5e 36 33 2e 20 2a 2f 0a 20  ainst 2^63. */. 
b8960 20 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72     return compar
b8970 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65  e2pow63(zNum)<ne
b8980 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  g;.  }.}../*.** 
b8990 49 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e  If zNum represen
b89a0 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  ts an integer th
b89b0 61 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 33  at will fit in 3
b89c0 32 2d 62 69 74 73 2c 20 74 68 65 6e 20 73 65 74  2-bits, then set
b89d0 0a 2a 2a 20 2a 70 56 61 6c 75 65 20 74 6f 20 74  .** *pValue to t
b89e0 68 61 74 20 69 6e 74 65 67 65 72 20 61 6e 64 20  hat integer and 
b89f0 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20 4f 74  return true.  Ot
b8a00 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 66  herwise return f
b8a10 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20  alse..**.** Any 
b8a20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 63 68 61 72  non-numeric char
b8a30 61 63 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c  acters that foll
b8a40 6f 77 69 6e 67 20 7a 4e 75 6d 20 61 72 65 20 69  owing zNum are i
b8a50 67 6e 6f 72 65 64 2e 0a 2a 2a 20 54 68 69 73 20  gnored..** This 
b8a60 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  is different fro
b8a70 6d 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28  m sqlite3Atoi64(
b8a80 29 20 77 68 69 63 68 20 72 65 71 75 69 72 65 73  ) which requires
b8a90 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 6e 75   the.** input nu
b8aa0 6d 62 65 72 20 74 6f 20 62 65 20 7a 65 72 6f 2d  mber to be zero-
b8ab0 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53  terminated..*/.S
b8ac0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b8ad0 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33  t sqlite3GetInt3
b8ae0 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  2(const char *zN
b8af0 75 6d 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65 29  um, int *pValue)
b8b00 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  {.  sqlite_int64
b8b10 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c   v = 0;.  int i,
b8b20 20 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20   c;.  int neg = 
b8b30 30 3b 0a 20 20 69 66 28 20 7a 4e 75 6d 5b 30 5d  0;.  if( zNum[0]
b8b40 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67  =='-' ){.    neg
b8b50 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b   = 1;.    zNum++
b8b60 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 4e  ;.  }else if( zN
b8b70 75 6d 5b 30 5d 3d 3d 27 2b 27 20 29 7b 0a 20 20  um[0]=='+' ){.  
b8b80 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20    zNum++;.  }.  
b8b90 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d  while( zNum[0]==
b8ba0 27 30 27 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20  '0' ) zNum++;.  
b8bb0 66 6f 72 28 69 3d 30 3b 20 69 3c 31 31 20 26 26  for(i=0; i<11 &&
b8bc0 20 28 63 20 3d 20 7a 4e 75 6d 5b 69 5d 20 2d 20   (c = zNum[i] - 
b8bd0 27 30 27 29 3e 3d 30 20 26 26 20 63 3c 3d 39 3b  '0')>=0 && c<=9;
b8be0 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76   i++){.    v = v
b8bf0 2a 31 30 20 2b 20 63 3b 0a 20 20 7d 0a 0a 20 20  *10 + c;.  }..  
b8c00 2f 2a 20 54 68 65 20 6c 6f 6e 67 65 73 74 20 64  /* The longest d
b8c10 65 63 69 6d 61 6c 20 72 65 70 72 65 73 65 6e 74  ecimal represent
b8c20 61 74 69 6f 6e 20 6f 66 20 61 20 33 32 20 62 69  ation of a 32 bi
b8c30 74 20 69 6e 74 65 67 65 72 20 69 73 20 31 30 20  t integer is 10 
b8c40 64 69 67 69 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a  digits:.  **.  *
b8c50 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32  *             12
b8c60 33 34 35 36 37 38 39 30 0a 20 20 2a 2a 20 20 20  34567890.  **   
b8c70 20 20 32 5e 33 31 20 2d 3e 20 32 31 34 37 34 38    2^31 -> 214748
b8c80 33 36 34 38 0a 20 20 2a 2f 0a 20 20 69 66 28 20  3648.  */.  if( 
b8c90 69 3e 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75  i>10 ){.    retu
b8ca0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
b8cb0 76 2d 6e 65 67 3e 32 31 34 37 34 38 33 36 34 37  v-neg>2147483647
b8cc0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
b8cd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 20  ;.  }.  if( neg 
b8ce0 29 7b 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a 20  ){.    v = -v;. 
b8cf0 20 7d 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20 28   }.  *pValue = (
b8d00 69 6e 74 29 76 3b 0a 20 20 72 65 74 75 72 6e 20  int)v;.  return 
b8d10 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  1;.}../*.** The 
b8d20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
b8d30 69 6e 74 65 67 65 72 20 65 6e 63 6f 64 69 6e 67  integer encoding
b8d40 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a   is as follows:.
b8d50 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20  **.** KEY:.**   
b8d60 20 20 20 20 20 20 41 20 3d 20 30 78 78 78 78 78        A = 0xxxxx
b8d70 78 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20  xx    7 bits of 
b8d80 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61  data and one fla
b8d90 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20  g bit.**        
b8da0 20 42 20 3d 20 31 78 78 78 78 78 78 78 20 20 20   B = 1xxxxxxx   
b8db0 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20   7 bits of data 
b8dc0 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74  and one flag bit
b8dd0 0a 2a 2a 20 20 20 20 20 20 20 20 20 43 20 3d 20  .**         C = 
b8de0 78 78 78 78 78 78 78 78 20 20 20 20 38 20 62 69  xxxxxxxx    8 bi
b8df0 74 73 20 6f 66 20 64 61 74 61 0a 2a 2a 0a 2a 2a  ts of data.**.**
b8e00 20 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20    7 bits - A.** 
b8e10 31 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20  14 bits - BA.** 
b8e20 32 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a  21 bits - BBA.**
b8e30 20 32 38 20 62 69 74 73 20 2d 20 42 42 42 41 0a   28 bits - BBBA.
b8e40 2a 2a 20 33 35 20 62 69 74 73 20 2d 20 42 42 42  ** 35 bits - BBB
b8e50 42 41 0a 2a 2a 20 34 32 20 62 69 74 73 20 2d 20  BA.** 42 bits - 
b8e60 42 42 42 42 42 41 0a 2a 2a 20 34 39 20 62 69 74  BBBBBA.** 49 bit
b8e70 73 20 2d 20 42 42 42 42 42 42 41 0a 2a 2a 20 35  s - BBBBBBA.** 5
b8e80 36 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42  6 bits - BBBBBBB
b8e90 41 0a 2a 2a 20 36 34 20 62 69 74 73 20 2d 20 42  A.** 64 bits - B
b8ea0 42 42 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a  BBBBBBBC.*/../*.
b8eb0 2a 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69  ** Write a 64-bi
b8ec0 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
b8ed0 68 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d  h integer to mem
b8ee0 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20  ory starting at 
b8ef0 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e  p[0]..** The len
b8f00 67 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74  gth of data writ
b8f10 65 20 77 69 6c 6c 20 62 65 20 62 65 74 77 65 65  e will be betwee
b8f20 6e 20 31 20 61 6e 64 20 39 20 62 79 74 65 73 2e  n 1 and 9 bytes.
b8f30 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20    The number.** 
b8f40 6f 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e  of bytes written
b8f50 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
b8f60 0a 2a 2a 20 41 20 76 61 72 69 61 62 6c 65 2d 6c  .** A variable-l
b8f70 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63 6f  ength integer co
b8f80 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 6c 6f  nsists of the lo
b8f90 77 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 61  wer 7 bits of ea
b8fa0 63 68 20 62 79 74 65 0a 2a 2a 20 66 6f 72 20 61  ch byte.** for a
b8fb0 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 61  ll bytes that ha
b8fc0 76 65 20 74 68 65 20 38 74 68 20 62 69 74 20 73  ve the 8th bit s
b8fd0 65 74 20 61 6e 64 20 6f 6e 65 20 62 79 74 65 20  et and one byte 
b8fe0 77 69 74 68 20 74 68 65 20 38 74 68 0a 2a 2a 20  with the 8th.** 
b8ff0 62 69 74 20 63 6c 65 61 72 2e 20 20 45 78 63 65  bit clear.  Exce
b9000 70 74 2c 20 69 66 20 77 65 20 67 65 74 20 74 6f  pt, if we get to
b9010 20 74 68 65 20 39 74 68 20 62 79 74 65 2c 20 69   the 9th byte, i
b9020 74 20 73 74 6f 72 65 73 20 74 68 65 20 66 75 6c  t stores the ful
b9030 6c 0a 2a 2a 20 38 20 62 69 74 73 20 61 6e 64 20  l.** 8 bits and 
b9040 69 73 20 74 68 65 20 6c 61 73 74 20 62 79 74 65  is the last byte
b9050 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b9060 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
b9070 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65  utVarint(unsigne
b9080 64 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 76  d char *p, u64 v
b9090 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e  ){.  int i, j, n
b90a0 3b 0a 20 20 75 38 20 62 75 66 5b 31 30 5d 3b 0a  ;.  u8 buf[10];.
b90b0 20 20 69 66 28 20 76 20 26 20 28 28 28 75 36 34    if( v & (((u64
b90c0 29 30 78 66 66 30 30 30 30 30 30 29 3c 3c 33 32  )0xff000000)<<32
b90d0 29 20 29 7b 0a 20 20 20 20 70 5b 38 5d 20 3d 20  ) ){.    p[8] = 
b90e0 28 75 38 29 76 3b 0a 20 20 20 20 76 20 3e 3e 3d  (u8)v;.    v >>=
b90f0 20 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d 37 3b   8;.    for(i=7;
b9100 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20   i>=0; i--){.   
b9110 20 20 20 70 5b 69 5d 20 3d 20 28 75 38 29 28 28     p[i] = (u8)((
b9120 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30  v & 0x7f) | 0x80
b9130 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37  );.      v >>= 7
b9140 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
b9150 72 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20  rn 9;.  }    .  
b9160 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20  n = 0;.  do{.   
b9170 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 38 29   buf[n++] = (u8)
b9180 28 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78  ((v & 0x7f) | 0x
b9190 38 30 29 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37  80);.    v >>= 7
b91a0 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30  ;.  }while( v!=0
b91b0 20 29 3b 0a 20 20 62 75 66 5b 30 5d 20 26 3d 20   );.  buf[0] &= 
b91c0 30 78 37 66 3b 0a 20 20 61 73 73 65 72 74 28 20  0x7f;.  assert( 
b91d0 6e 3c 3d 39 20 29 3b 0a 20 20 66 6f 72 28 69 3d  n<=9 );.  for(i=
b91e0 30 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30 3b 20  0, j=n-1; j>=0; 
b91f0 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 70  j--, i++){.    p
b9200 5b 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a 20 20  [i] = buf[j];.  
b9210 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a  }.  return n;.}.
b9220 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
b9230 69 6e 65 20 69 73 20 61 20 66 61 73 74 65 72 20  ine is a faster 
b9240 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74  version of sqlit
b9250 65 33 50 75 74 56 61 72 69 6e 74 28 29 20 74 68  e3PutVarint() th
b9260 61 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73  at only.** works
b9270 20 66 6f 72 20 33 32 2d 62 69 74 20 70 6f 73 69   for 32-bit posi
b9280 74 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e  tive integers an
b9290 64 20 77 68 69 63 68 20 69 73 20 6f 70 74 69 6d  d which is optim
b92a0 69 7a 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20  ized for.** the 
b92b0 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 73  common case of s
b92c0 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 20 20  mall integers.  
b92d0 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c  A MACRO version,
b92e0 20 70 75 74 56 61 72 69 6e 74 33 32 2c 0a 2a 2a   putVarint32,.**
b92f0 20 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69   is provided whi
b9300 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 73  ch inlines the s
b9310 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e  ingle-byte case.
b9320 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c    All code shoul
b9330 64 20 75 73 65 0a 2a 2a 20 74 68 65 20 4d 41 43  d use.** the MAC
b9340 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 74 68  RO version as th
b9350 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75  is function assu
b9360 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62  mes the single-b
b9370 79 74 65 20 63 61 73 65 20 68 61 73 0a 2a 2a 20  yte case has.** 
b9380 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e  already been han
b9390 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dled..*/.SQLITE_
b93a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b93b0 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 75  te3PutVarint32(u
b93c0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c  nsigned char *p,
b93d0 20 75 33 32 20 76 29 7b 0a 23 69 66 6e 64 65 66   u32 v){.#ifndef
b93e0 20 70 75 74 56 61 72 69 6e 74 33 32 0a 20 20 69   putVarint32.  i
b93f0 66 28 20 28 76 20 26 20 7e 30 78 37 66 29 3d 3d  f( (v & ~0x7f)==
b9400 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20  0 ){.    p[0] = 
b9410 76 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  v;.    return 1;
b9420 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66  .  }.#endif.  if
b9430 28 20 28 76 20 26 20 7e 30 78 33 66 66 66 29 3d  ( (v & ~0x3fff)=
b9440 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d  =0 ){.    p[0] =
b9450 20 28 75 38 29 28 28 76 3e 3e 37 29 20 7c 20 30   (u8)((v>>7) | 0
b9460 78 38 30 29 3b 0a 20 20 20 20 70 5b 31 5d 20 3d  x80);.    p[1] =
b9470 20 28 75 38 29 28 76 20 26 20 30 78 37 66 29 3b   (u8)(v & 0x7f);
b9480 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20  .    return 2;. 
b9490 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69   }.  return sqli
b94a0 74 65 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20  te3PutVarint(p, 
b94b0 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  v);.}../*.** Rea
b94c0 64 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61  d a 64-bit varia
b94d0 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
b94e0 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73  er from memory s
b94f0 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e  tarting at p[0].
b9500 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
b9510 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72  umber of bytes r
b9520 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20  ead.  The value 
b9530 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e  is stored in *v.
b9540 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b9550 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74  TE u8 sqlite3Get
b9560 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73  Varint(const uns
b9570 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75  igned char *p, u
b9580 36 34 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c  64 *v){.  u32 a,
b9590 62 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a  b,s;..  a = *p;.
b95a0 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61    /* a: p0 (unma
b95b0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b95c0 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
b95d0 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
b95e0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70  turn 1;.  }..  p
b95f0 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20  ++;.  b = *p;.  
b9600 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b  /* b: p1 (unmask
b9610 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62  ed) */.  if (!(b
b9620 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20  &0x80)).  {.    
b9630 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61  a &= 0x7f;.    a
b9640 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = a<<7;.    a |
b9650 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b  = b;.    *v = a;
b9660 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20  .    return 2;. 
b9670 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d   }..  p++;.  a =
b9680 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a   a<<14;.  a |= *
b9690 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31  p;.  /* a: p0<<1
b96a0 34 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64  4 | p2 (unmasked
b96b0 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30  ) */.  if (!(a&0
b96c0 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20  x80)).  {.    a 
b96d0 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  &= (0x7f<<14)|(0
b96e0 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30  x7f);.    b &= 0
b96f0 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c  x7f;.    b = b<<
b9700 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
b9710 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72     *v = a;.    r
b9720 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20  eturn 3;.  }..  
b9730 2f 2a 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c  /* CSE1 from bel
b9740 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78  ow */.  a &= (0x
b9750 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b9760 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c    p++;.  b = b<<
b9770 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20  14;.  b |= *p;. 
b9780 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20   /* b: p1<<14 | 
b9790 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p3 (unmasked) */
b97a0 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29  .  if (!(b&0x80)
b97b0 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28  ).  {.    b &= (
b97c0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b97d0 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43  ;.    /* moved C
b97e0 53 45 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a  SE1 up */.    /*
b97f0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b9800 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20  |(0x7f); */.    
b9810 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20  a = a<<7;.    a 
b9820 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61  |= b;.    *v = a
b9830 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a  ;.    return 4;.
b9840 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c    }..  /* a: p0<
b9850 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64  <14 | p2 (masked
b9860 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c  ) */.  /* b: p1<
b9870 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b  <14 | p3 (unmask
b9880 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61  ed) */.  /* 1:sa
b9890 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20  ve off p0<<21 | 
b98a0 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c  p1<<14 | p2<<7 |
b98b0 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a   p3 (masked) */.
b98c0 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20    /* moved CSE1 
b98d0 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20  up */.  /* a &= 
b98e0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b98f0 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78  ); */.  b &= (0x
b9900 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b9910 20 20 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a    s = a;.  /* s:
b9920 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61   p0<<14 | p2 (ma
b9930 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b  sked) */..  p++;
b9940 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20  .  a = a<<14;.  
b9950 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a  a |= *p;.  /* a:
b9960 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34   p0<<28 | p2<<14
b9970 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29   | p4 (unmasked)
b9980 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
b9990 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20  80)).  {.    /* 
b99a0 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73  we can skip thes
b99b0 65 20 63 61 75 73 65 20 74 68 65 79 20 77 65 72  e cause they wer
b99c0 65 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20  e (effectively) 
b99d0 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61  done above in ca
b99e0 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20  lc'ing s */.    
b99f0 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32  /* a &= (0x7f<<2
b9a00 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
b9a10 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20  x7f); */.    /* 
b9a20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  b &= (0x7f<<14)|
b9a30 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62  (0x7f); */.    b
b9a40 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = b<<7;.    a |
b9a50 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e  = b;.    s = s>>
b9a60 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75  18;.    *v = ((u
b9a70 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20  64)s)<<32 | a;. 
b9a80 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d     return 5;.  }
b9a90 0a 0a 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66  ..  /* 2:save of
b9aa0 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31  f p0<<21 | p1<<1
b9ab0 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28  4 | p2<<7 | p3 (
b9ac0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d  masked) */.  s =
b9ad0 20 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b   s<<7;.  s |= b;
b9ae0 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20  .  /* s: p0<<21 
b9af0 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37  | p1<<14 | p2<<7
b9b00 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a   | p3 (masked) *
b9b10 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20  /..  p++;.  b = 
b9b20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70  b<<14;.  b |= *p
b9b30 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38  ;.  /* b: p1<<28
b9b40 20 7c 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28   | p3<<14 | p5 (
b9b50 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
b9b60 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20  f (!(b&0x80)).  
b9b70 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20  {.    /* we can 
b9b80 73 6b 69 70 20 74 68 69 73 20 63 61 75 73 65 20  skip this cause 
b9b90 69 74 20 77 61 73 20 28 65 66 66 65 63 74 69 76  it was (effectiv
b9ba0 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20  ely) done above 
b9bb0 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f  in calc'ing s */
b9bc0 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78  .    /* b &= (0x
b9bd0 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31  7f<<28)|(0x7f<<1
b9be0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20  4)|(0x7f); */.  
b9bf0 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    a &= (0x7f<<14
b9c00 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20  )|(0x7f);.    a 
b9c10 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = a<<7;.    a |=
b9c20 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31   b;.    s = s>>1
b9c30 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36  8;.    *v = ((u6
b9c40 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20  4)s)<<32 | a;.  
b9c50 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a    return 6;.  }.
b9c60 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c  .  p++;.  a = a<
b9c70 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a  <14;.  a |= *p;.
b9c80 20 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c    /* a: p2<<28 |
b9c90 20 70 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e   p4<<14 | p6 (un
b9ca0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20  masked) */.  if 
b9cb0 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(a&0x80)).  {.
b9cc0 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c      a &= (0x7f<<
b9cd0 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28  28)|(0x7f<<14)|(
b9ce0 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20  0x7f);.    b &= 
b9cf0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b9d00 29 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b  );.    b = b<<7;
b9d10 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20  .    a |= b;.   
b9d20 20 73 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20   s = s>>11;.    
b9d30 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33  *v = ((u64)s)<<3
b9d40 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72  2 | a;.    retur
b9d50 6e 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  n 7;.  }..  /* C
b9d60 53 45 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a  SE2 from below *
b9d70 2f 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c  /.  a &= (0x7f<<
b9d80 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b  14)|(0x7f);.  p+
b9d90 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a  +;.  b = b<<14;.
b9da0 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    b |= *p;.  /* 
b9db0 62 3a 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c  b: p3<<28 | p5<<
b9dc0 31 34 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65  14 | p7 (unmaske
b9dd0 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26  d) */.  if (!(b&
b9de0 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62  0x80)).  {.    b
b9df0 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28   &= (0x7f<<28)|(
b9e00 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b9e10 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43  ;.    /* moved C
b9e20 53 45 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a  SE2 up */.    /*
b9e30 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b9e40 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20  |(0x7f); */.    
b9e50 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20  a = a<<7;.    a 
b9e60 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e  |= b;.    s = s>
b9e70 3e 34 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75  >4;.    *v = ((u
b9e80 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20  64)s)<<32 | a;. 
b9e90 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d     return 8;.  }
b9ea0 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61  ..  p++;.  a = a
b9eb0 3c 3c 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b  <<15;.  a |= *p;
b9ec0 0a 20 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20  .  /* a: p4<<29 
b9ed0 7c 20 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75  | p6<<15 | p8 (u
b9ee0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f  nmasked) */..  /
b9ef0 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20  * moved CSE2 up 
b9f00 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78  */.  /* a &= (0x
b9f10 37 66 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31  7f<<29)|(0x7f<<1
b9f20 35 29 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20  5)|(0xff); */.  
b9f30 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  b &= (0x7f<<14)|
b9f40 28 30 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c  (0x7f);.  b = b<
b9f50 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20  <8;.  a |= b;.. 
b9f60 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d   s = s<<4;.  b =
b9f70 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30   p[-4];.  b &= 0
b9f80 78 37 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b  x7f;.  b = b>>3;
b9f90 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76  .  s |= b;..  *v
b9fa0 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20   = ((u64)s)<<32 
b9fb0 7c 20 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39  | a;..  return 9
b9fc0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
b9fd0 61 20 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c  a 32-bit variabl
b9fe0 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
b9ff0 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61   from memory sta
ba000 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a  rting at p[0]..*
ba010 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
ba020 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61  ber of bytes rea
ba030 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  d.  The value is
ba040 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a   stored in *v..*
ba050 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f  * A MACRO versio
ba060 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32 2c 20  n, getVarint32, 
ba070 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63  is provided whic
ba080 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a  h inlines the .*
ba090 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61  * single-byte ca
ba0a0 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68  se.  All code sh
ba0b0 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43  ould use the MAC
ba0c0 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a  RO version as .*
ba0d0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
ba0e0 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67  assumes the sing
ba0f0 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73  le-byte case has
ba100 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61   already been ha
ba110 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ndled..*/.SQLITE
ba120 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69  _PRIVATE u8 sqli
ba130 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 63  te3GetVarint32(c
ba140 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
ba150 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b 0a  ar *p, u32 *v){.
ba160 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 61 20    u32 a,b;..  a 
ba170 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30  = *p;.  /* a: p0
ba180 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 23   (unmasked) */.#
ba190 69 66 6e 64 65 66 20 67 65 74 56 61 72 69 6e 74  ifndef getVarint
ba1a0 33 32 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  32.  if (!(a&0x8
ba1b0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d  0)).  {.    *v =
ba1c0 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   a;.    return 1
ba1d0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
ba1e0 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20  p++;.  b = *p;. 
ba1f0 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73   /* b: p1 (unmas
ba200 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
ba210 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  b&0x80)).  {.   
ba220 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20   a &= 0x7f;.    
ba230 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76  a = a<<7;.    *v
ba240 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65   = a | b;.    re
ba250 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70  turn 2;.  }..  p
ba260 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b  ++;.  a = a<<14;
ba270 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  a |= *p;.  /*
ba280 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20   a: p0<<14 | p2 
ba290 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
ba2a0 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20  if (!(a&0x80)). 
ba2b0 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37   {.    a &= (0x7
ba2c0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
ba2d0 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20     b &= 0x7f;.  
ba2e0 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
ba2f0 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20  *v = a | b;.    
ba300 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20  return 3;.  }.. 
ba310 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31   p++;.  b = b<<1
ba320 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  b |= *p;.  
ba330 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70  /* b: p1<<14 | p
ba340 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  3 (unmasked) */.
ba350 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
ba360 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30  .  {.    b &= (0
ba370 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
ba380 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c  .    a &= (0x7f<
ba390 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20  <14)|(0x7f);.   
ba3a0 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a   a = a<<7;.    *
ba3b0 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72  v = a | b;.    r
ba3c0 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20  eturn 4;.  }..  
ba3d0 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34  p++;.  a = a<<14
ba3e0 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  a |= *p;.  /
ba3f0 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32  * a: p0<<28 | p2
ba400 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73  <<14 | p4 (unmas
ba410 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
ba420 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  a&0x80)).  {.   
ba430 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29   a &= (0x7f<<28)
ba440 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37  |(0x7f<<14)|(0x7
ba450 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 78  f);.    b &= (0x
ba460 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31  7f<<28)|(0x7f<<1
ba470 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
ba480 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20   = b<<7;.    *v 
ba490 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74  = a | b;.    ret
ba4a0 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 5;.  }..  /*
ba4b0 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61   We can only rea
ba4c0 63 68 20 74 68 69 73 20 70 6f 69 6e 74 20 77 68  ch this point wh
ba4d0 65 6e 20 72 65 61 64 69 6e 67 20 61 20 63 6f 72  en reading a cor
ba4e0 72 75 70 74 20 64 61 74 61 62 61 73 65 0a 20 20  rupt database.  
ba4f0 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 68 61  ** file.  In tha
ba500 74 20 63 61 73 65 20 77 65 20 61 72 65 20 6e 6f  t case we are no
ba510 74 20 69 6e 20 61 6e 79 20 68 75 72 72 79 2e 20  t in any hurry. 
ba520 20 55 73 65 20 74 68 65 20 28 72 65 6c 61 74 69   Use the (relati
ba530 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 20  vely.  ** slow) 
ba540 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 20  general-purpose 
ba550 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
ba560 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 65 78  () routine to ex
ba570 74 72 61 63 74 20 74 68 65 0a 20 20 2a 2a 20 76  tract the.  ** v
ba580 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20  alue. */.  {.   
ba590 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20 75 38   u64 v64;.    u8
ba5a0 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b   n;..    p -= 4;
ba5b0 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33  .    n = sqlite3
ba5c0 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 76 36  GetVarint(p, &v6
ba5d0 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  4);.    assert( 
ba5e0 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20  n>5 && n<=9 );. 
ba5f0 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34     *v = (u32)v64
ba600 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a  ;.    return n;.
ba610 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
ba620 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
ba630 66 20 62 79 74 65 73 20 74 68 61 74 20 77 69 6c  f bytes that wil
ba640 6c 20 62 65 20 6e 65 65 64 65 64 20 74 6f 20 73  l be needed to s
ba650 74 6f 72 65 20 74 68 65 20 67 69 76 65 6e 0a 2a  tore the given.*
ba660 2a 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  * 64-bit integer
ba670 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
ba680 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ba690 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b  arintLen(u64 v){
ba6a0 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20  .  int i = 0;.  
ba6b0 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20  do{.    i++;.   
ba6c0 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69   v >>= 7;.  }whi
ba6d0 6c 65 28 20 76 21 3d 30 20 26 26 20 69 3c 39 20  le( v!=0 && i<9 
ba6e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d  );.  return i;.}
ba6f0 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72  .../*.** Read or
ba700 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d 62 79   write a four-by
ba710 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
ba720 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a  teger value..*/.
ba730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
ba740 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 79  32 sqlite3Get4by
ba750 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b  te(const u8 *p){
ba760 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c  .  return (p[0]<
ba770 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36  <24) | (p[1]<<16
ba780 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20  ) | (p[2]<<8) | 
ba790 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  p[3];.}.SQLITE_P
ba7a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
ba7b0 74 65 33 50 75 74 34 62 79 74 65 28 75 6e 73 69  te3Put4byte(unsi
ba7c0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33  gned char *p, u3
ba7d0 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28  2 v){.  p[0] = (
ba7e0 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20 70 5b  u8)(v>>24);.  p[
ba7f0 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31 36 29  1] = (u8)(v>>16)
ba800 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38 29 28  ;.  p[2] = (u8)(
ba810 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 3d 20  v>>8);.  p[3] = 
ba820 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20  (u8)v;.}....#if 
ba830 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
ba840 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
ba850 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  L) || defined(SQ
ba860 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a  LITE_HAS_CODEC).
ba870 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20  /*.** Translate 
ba880 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66  a single byte of
ba890 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e 74   Hex into an int
ba8a0 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  eger..** This ro
ba8b0 75 74 69 6e 65 6e 20 6f 6e 6c 79 20 77 6f 72 6b  utinen only work
ba8c0 73 20 69 66 20 68 20 72 65 61 6c 6c 79 20 69 73  s if h really is
ba8d0 20 61 20 76 61 6c 69 64 20 68 65 78 61 64 65 63   a valid hexadec
ba8e0 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65  imal.** characte
ba8f0 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46  r:  0..9a..fA..F
ba900 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 68 65  .*/.static u8 he
ba910 78 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20  xToInt(int h){. 
ba920 20 61 73 73 65 72 74 28 20 28 68 3e 3d 27 30 27   assert( (h>='0'
ba930 20 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20   && h<='9') ||  
ba940 28 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 66  (h>='a' && h<='f
ba950 27 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26  ') ||  (h>='A' &
ba960 26 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66  & h<='F') );.#if
ba970 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49  def SQLITE_ASCII
ba980 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e  .  h += 9*(1&(h>
ba990 3e 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  >6));.#endif.#if
ba9a0 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49  def SQLITE_EBCDI
ba9b0 43 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28  C.  h += 9*(1&~(
ba9c0 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20  h>>4));.#endif. 
ba9d0 20 72 65 74 75 72 6e 20 28 75 38 29 28 68 20 26   return (u8)(h &
ba9e0 20 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66 20   0xf);.}.#endif 
ba9f0 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  /* !SQLITE_OMIT_
baa00 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20  BLOB_LITERAL || 
baa10 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
baa20 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65   */..#if !define
baa30 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c  d(SQLITE_OMIT_BL
baa40 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64  OB_LITERAL) || d
baa50 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41  efined(SQLITE_HA
baa60 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 43  S_CODEC)./*.** C
baa70 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 6c 69  onvert a BLOB li
baa80 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66 6f 72  teral of the for
baa90 6d 20 22 78 27 68 68 68 68 68 68 27 22 20 69 6e  m "x'hhhhhh'" in
baaa0 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a 2a 2a  to its binary.**
baab0 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72 6e 20   value.  Return 
baac0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73  a pointer to its
baad0 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 20   binary value.  
baae0 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
baaf0 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75  e.** binary valu
bab00 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69  e has been obtai
bab10 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20  ned from malloc 
bab20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65  and must be free
bab30 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c  d by.** the call
bab40 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  ing routine..*/.
bab50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
bab60 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54  oid *sqlite3HexT
bab70 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a 64  oBlob(sqlite3 *d
bab80 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
bab90 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72  , int n){.  char
baba0 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 69   *zBlob;.  int i
babb0 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63 68  ;..  zBlob = (ch
babc0 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  ar *)sqlite3DbMa
babd0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32 20  llocRaw(db, n/2 
babe0 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20 69  + 1);.  n--;.  i
babf0 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20 20  f( zBlob ){.    
bac00 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b  for(i=0; i<n; i+
bac10 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f 62  =2){.      zBlob
bac20 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49 6e  [i/2] = (hexToIn
bac30 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68 65  t(z[i])<<4) | he
bac40 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b 0a  xToInt(z[i+1]);.
bac50 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62 5b      }.    zBlob[
bac60 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  i/2] = 0;.  }.  
bac70 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a  return zBlob;.}.
bac80 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54  #endif /* !SQLIT
bac90 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
baca0 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41  RAL || SQLITE_HA
bacb0 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a  S_CODEC */.../*.
bacc0 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 71  ** Change the sq
bacd0 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d 20  lite.magic from 
bace0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
bacf0 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49  N to SQLITE_MAGI
bad00 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75 72  C_BUSY..** Retur
bad10 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d  n an error (non-
bad20 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67  zero) if the mag
bad30 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54  ic was not SQLIT
bad40 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20  E_MAGIC_OPEN.** 
bad50 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
bad60 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  e is called..**.
bad70 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
bad80 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 65  is called when e
bad90 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c 69 74  ntering an SQLit
bada0 65 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c 49  e API.  The SQLI
badb0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a  TE_MAGIC_OPEN.**
badc0 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73   value indicates
badd0 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
bade0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61  se connection pa
badf0 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 41 50  ssed into the AP
bae00 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e 64  I is.** open and
bae10 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 75 73   is not being us
bae20 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 74 68  ed by another th
bae30 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e 67 69  read.  By changi
bae40 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  ng the value.** 
bae50 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  to SQLITE_MAGIC_
bae60 42 55 53 59 20 77 65 20 69 6e 64 69 63 61 74 65  BUSY we indicate
bae70 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 63   that the connec
bae80 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e 0a  tion is in use..
bae90 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  ** sqlite3Safety
baea0 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c  Off() below will
baeb0 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   change the valu
baec0 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45  e back to SQLITE
baed0 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77  _MAGIC_OPEN.** w
baee0 68 65 6e 20 74 68 65 20 41 50 49 20 65 78 69 74  hen the API exit
baef0 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  s. .**.** This r
baf00 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74 74 65  outine is a atte
baf10 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20 69 66  mpt to detect if
baf20 20 74 77 6f 20 74 68 72 65 61 64 73 20 75 73 65   two threads use
baf30 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71 6c   the.** same sql
baf40 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 74 20  ite* pointer at 
baf50 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20  the same time.  
baf60 54 68 65 72 65 20 69 73 20 61 20 72 61 63 65 20  There is a race 
baf70 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73 6f  .** condition so
baf80 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
baf90 74 68 61 74 20 74 68 65 20 65 72 72 6f 72 20 69  that the error i
bafa0 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64 2e 0a  s not detected..
bafb0 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 20 74  ** But usually t
bafc0 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c 20  he problem will 
bafd0 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20 72 65  be seen.  The re
bafe0 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e 0a  sult will be an.
baff0 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68 20 63  ** error which c
bb000 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 65  an be used to de
bb010 62 75 67 20 74 68 65 20 61 70 70 6c 69 63 61 74  bug the applicat
bb020 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 75  ion that is.** u
bb030 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 63 6f  sing SQLite inco
bb040 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  rrectly..**.** T
bb050 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49 66 20  icket #202:  If 
bb060 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f 74  db->magic is not
bb070 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 76 61   a valid open va
bb080 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65 20 6e  lue, take care n
bb090 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20  ot.** to modify 
bb0a0 74 68 65 20 64 62 20 73 74 72 75 63 74 75 72 65  the db structure
bb0b0 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f 75   at all.  It cou
bb0c0 6c 64 20 62 65 20 74 68 61 74 20 64 62 20 69 73  ld be that db is
bb0d0 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69 6e   a stale.** poin
bb0e0 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ter.  In other w
bb0f0 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 20 62  ords, it could b
bb100 65 20 74 68 61 74 20 74 68 65 72 65 20 68 61 73  e that there has
bb110 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a 2a   been a prior.**
bb120 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
bb130 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 20 64  _close(db) and d
bb140 62 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c  b has been deall
bb150 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77 65 20  ocated.  And we 
bb160 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74  do.** not want t
bb170 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64 65 61  o write into dea
bb180 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e  llocated memory.
bb190 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
bb1a0 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50  E_DEBUG.SQLITE_P
bb1b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
bb1c0 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74  e3SafetyOn(sqlit
bb1d0 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64  e3 *db){.  if( d
bb1e0 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45  b->magic==SQLITE
bb1f0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a 20  _MAGIC_OPEN ){. 
bb200 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53     db->magic = S
bb210 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
bb220 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ;.    assert( sq
bb230 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
bb240 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
bb250 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
bb260 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d 61 67  else if( db->mag
bb270 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic==SQLITE_MAGIC
bb280 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d  _BUSY ){.    db-
bb290 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
bb2a0 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20  MAGIC_ERROR;.   
bb2b0 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
bb2c0 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  upted = 1;.  }. 
bb2d0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e   return 1;.}.#en
bb2e0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  dif../*.** Chang
bb2f0 65 20 74 68 65 20 6d 61 67 69 63 20 66 72 6f 6d  e the magic from
bb300 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55   SQLITE_MAGIC_BU
bb310 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47  SY to SQLITE_MAG
bb320 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75  IC_OPEN..** Retu
bb330 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e  rn an error (non
bb340 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61  -zero) if the ma
bb350 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49  gic was not SQLI
bb360 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a  TE_MAGIC_BUSY.**
bb370 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
bb380 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  ne is called..*/
bb390 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
bb3a0 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
bb3b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
bb3c0 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33  afetyOff(sqlite3
bb3d0 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d   *db){.  if( db-
bb3e0 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d  >magic==SQLITE_M
bb3f0 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20  AGIC_BUSY ){.   
bb400 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
bb410 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a  ITE_MAGIC_OPEN;.
bb420 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
bb430 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
bb440 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20  b->mutex) );.   
bb450 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
bb460 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69  se{.    db->magi
bb470 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
bb480 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e  _ERROR;.    db->
bb490 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
bb4a0 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
bb4b0 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66   1;.  }.}.#endif
bb4c0 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
bb4d0 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61   make sure we ha
bb4e0 76 65 20 61 20 76 61 6c 69 64 20 64 62 20 70 6f  ve a valid db po
bb4f0 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 73  inter.  This tes
bb500 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c  t is not.** fool
bb510 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64 6f 65  proof but it doe
bb520 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d  s provide some m
bb530 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74 65 63  easure of protec
bb540 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20  tion against.** 
bb550 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e  misuse of the in
bb560 74 65 72 66 61 63 65 20 73 75 63 68 20 61 73 20  terface such as 
bb570 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f  passing in db po
bb580 69 6e 74 65 72 73 20 74 68 61 74 20 61 72 65 0a  inters that are.
bb590 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68  ** NULL or which
bb5a0 20 68 61 76 65 20 62 65 65 6e 20 70 72 65 76 69   have been previ
bb5b0 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49  ously closed.  I
bb5c0 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  f this routine r
bb5d0 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20 6d  eturns.** 1 it m
bb5e0 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 62  eans that the db
bb5f0 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69   pointer is vali
bb600 64 20 61 6e 64 20 30 20 69 66 20 69 74 20 73 68  d and 0 if it sh
bb610 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 64  ould not be.** d
bb620 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f 72 20  ereferenced for 
bb630 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 68 65  any reason.  The
bb640 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
bb650 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 0a  n should invoke.
bb660 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  ** SQLITE_MISUSE
bb670 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a   immediately..**
bb680 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74  .** sqlite3Safet
bb690 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 75 69  yCheckOk() requi
bb6a0 72 65 73 20 74 68 61 74 20 74 68 65 20 64 62 20  res that the db 
bb6b0 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c 69 64  pointer be valid
bb6c0 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73 71   for.** use.  sq
bb6d0 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
bb6e0 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f 77  SickOrOk() allow
bb6f0 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72 20 74  s a db pointer t
bb700 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a 2a  hat failed to.**
bb710 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 20 61   open properly a
bb720 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20 66 6f  nd is not fit fo
bb730 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20 62 75  r general use bu
bb740 74 20 77 68 69 63 68 20 63 61 6e 20 62 65 0a 2a  t which can be.*
bb750 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61 72 67  * used as an arg
bb760 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
bb770 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73 71 6c  _errmsg() or sql
bb780 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a 2f  ite3_close()..*/
bb790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
bb7a0 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
bb7b0 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33  yCheckOk(sqlite3
bb7c0 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67   *db){.  u32 mag
bb7d0 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20  ic;.  if( db==0 
bb7e0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 61  ) return 0;.  ma
bb7f0 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b  gic = db->magic;
bb800 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51  .  if( magic!=SQ
bb810 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20  LITE_MAGIC_OPEN 
bb820 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d  &&.      magic!=
bb830 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
bb840 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  Y ) return 0;.  
bb850 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 53 51 4c 49  return 1;.}.SQLI
bb860 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
bb870 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
bb880 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74 65  kSickOrOk(sqlite
bb890 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61  3 *db){.  u32 ma
bb8a0 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30  gic;.  if( db==0
bb8b0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d   ) return 0;.  m
bb8c0 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63  agic = db->magic
bb8d0 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53  ;.  if( magic!=S
bb8e0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b  QLITE_MAGIC_SICK
bb8f0 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21   &&.      magic!
bb900 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50  =SQLITE_MAGIC_OP
bb910 45 4e 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69  EN &&.      magi
bb920 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c!=SQLITE_MAGIC_
bb930 42 55 53 59 20 29 20 72 65 74 75 72 6e 20 30 3b  BUSY ) return 0;
bb940 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
bb950 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
bb960 45 6e 64 20 6f 66 20 75 74 69 6c 2e 63 20 2a 2a  End of util.c **
bb970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
bb9a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
bb9b0 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e  Begin file hash.
bb9c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
bb9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
bb9f0 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
bba00 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  mber 22.**.** Th
bba10 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
bba20 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
bba30 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
bba40 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
bba50 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
bba60 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
bba70 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
bba80 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
bba90 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
bbaa0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
bbab0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
bbac0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
bbad0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
bbae0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
bbaf0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
bbb00 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
bbb10 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
bbb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbb60 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
bbb70 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
bbb80 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63 20  tion of generic 
bbb90 68 61 73 68 2d 74 61 62 6c 65 73 0a 2a 2a 20 75  hash-tables.** u
bbba0 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a  sed in SQLite..*
bbbb0 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e 63  *.** $Id: hash.c
bbbc0 2c 76 20 31 2e 33 33 20 32 30 30 39 2f 30 31 2f  ,v 1.33 2009/01/
bbbd0 30 39 20 30 31 3a 31 32 3a 32 38 20 64 72 68 20  09 01:12:28 drh 
bbbe0 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 54 75 72  Exp $.*/../* Tur
bbbf0 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e  n bulk memory in
bbc00 74 6f 20 61 20 68 61 73 68 20 74 61 62 6c 65 20  to a hash table 
bbc10 6f 62 6a 65 63 74 20 62 79 20 69 6e 69 74 69 61  object by initia
bbc20 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69  lizing the.** fi
bbc30 65 6c 64 73 20 6f 66 20 74 68 65 20 48 61 73 68  elds of the Hash
bbc40 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
bbc50 2a 20 22 70 4e 65 77 22 20 69 73 20 61 20 70 6f  * "pNew" is a po
bbc60 69 6e 74 65 72 20 74 6f 20 74 68 65 20 68 61 73  inter to the has
bbc70 68 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20  h table that is 
bbc80 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  to be initialize
bbc90 64 2e 0a 2a 2a 20 22 63 6f 70 79 4b 65 79 22 20  d..** "copyKey" 
bbca0 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 68  is true if the h
bbcb0 61 73 68 20 74 61 62 6c 65 20 73 68 6f 75 6c 64  ash table should
bbcc0 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72   make its own pr
bbcd0 69 76 61 74 65 0a 2a 2a 20 63 6f 70 79 20 6f 66  ivate.** copy of
bbce0 20 6b 65 79 73 20 61 6e 64 20 66 61 6c 73 65 20   keys and false 
bbcf0 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6a 75 73  if it should jus
bbd00 74 20 75 73 65 20 74 68 65 20 73 75 70 70 6c 69  t use the suppli
bbd10 65 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  ed pointer..*/.S
bbd20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
bbd30 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e  id sqlite3HashIn
bbd40 69 74 28 48 61 73 68 20 2a 70 4e 65 77 2c 20 69  it(Hash *pNew, i
bbd50 6e 74 20 63 6f 70 79 4b 65 79 29 7b 0a 20 20 61  nt copyKey){.  a
bbd60 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29  ssert( pNew!=0 )
bbd70 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79 4b 65  ;.  pNew->copyKe
bbd80 79 20 3d 20 63 6f 70 79 4b 65 79 21 3d 30 3b 0a  y = copyKey!=0;.
bbd90 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20    pNew->first = 
bbda0 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74  0;.  pNew->count
bbdb0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74   = 0;.  pNew->ht
bbdc0 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e 65 77  size = 0;.  pNew
bbdd0 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20  ->ht = 0;.}../* 
bbde0 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72 69  Remove all entri
bbdf0 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68 20 74  es from a hash t
bbe00 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20 61  able.  Reclaim a
bbe10 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61  ll memory..** Ca
bbe20 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ll this routine 
bbe30 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61 73 68  to delete a hash
bbe40 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72 65 73   table or to res
bbe50 65 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a  et a hash table.
bbe60 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74 79 20  ** to the empty 
bbe70 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  state..*/.SQLITE
bbe80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
bbe90 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48  lite3HashClear(H
bbea0 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 61 73 68  ash *pH){.  Hash
bbeb0 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20  Elem *elem;     
bbec0 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
bbed0 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d  ng over all elem
bbee0 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ents of the tabl
bbef0 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
bbf00 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20  pH!=0 );.  elem 
bbf10 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70  = pH->first;.  p
bbf20 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20  H->first = 0;.  
bbf30 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d  sqlite3_free(pH-
bbf40 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d  >ht);.  pH->ht =
bbf50 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65   0;.  pH->htsize
bbf60 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65   = 0;.  while( e
bbf70 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 73 68 45  lem ){.    HashE
bbf80 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d  lem *next_elem =
bbf90 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20   elem->next;.   
bbfa0 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79   if( pH->copyKey
bbfb0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
bbfc0 33 5f 66 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65  3_free(elem->pKe
bbfd0 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  y);.    }.    sq
bbfe0 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d 29  lite3_free(elem)
bbff0 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65 78  ;.    elem = nex
bc000 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70 48  t_elem;.  }.  pH
bc010 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a  ->count = 0;.}..
bc020 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63  /*.** Hash and c
bc030 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
bc040 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64  ons when the mod
bc050 65 20 69 73 20 53 51 4c 49 54 45 5f 48 41 53 48  e is SQLITE_HASH
bc060 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69  _STRING.*/.stati
bc070 63 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f  c int strHash(co
bc080 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
bc090 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e  int nKey){.  con
bc0a0 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f  st char *z = (co
bc0b0 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b  nst char *)pKey;
bc0c0 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20  .  int h = 0;.  
bc0d0 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b  if( nKey<=0 ) nK
bc0e0 65 79 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  ey = sqlite3Strl
bc0f0 65 6e 33 30 28 7a 29 3b 0a 20 20 77 68 69 6c 65  en30(z);.  while
bc100 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20  ( nKey > 0  ){. 
bc110 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20     h = (h<<3) ^ 
bc120 68 20 5e 20 73 71 6c 69 74 65 33 55 70 70 65 72  h ^ sqlite3Upper
bc130 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65  ToLower[(unsigne
bc140 64 20 63 68 61 72 29 2a 7a 2b 2b 5d 3b 0a 20 20  d char)*z++];.  
bc150 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20    nKey--;.  }.  
bc160 72 65 74 75 72 6e 20 68 20 26 20 30 78 37 66 66  return h & 0x7ff
bc170 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69 63 20  fffff;.}.static 
bc180 69 6e 74 20 73 74 72 43 6f 6d 70 61 72 65 28 63  int strCompare(c
bc190 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
bc1a0 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20  , int n1, const 
bc1b0 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74  void *pKey2, int
bc1c0 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d   n2){.  if( n1!=
bc1d0 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  n2 ) return 1;. 
bc1e0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53   return sqlite3S
bc1f0 74 72 4e 49 43 6d 70 28 28 63 6f 6e 73 74 20 63  trNICmp((const c
bc200 68 61 72 2a 29 70 4b 65 79 31 2c 28 63 6f 6e 73  har*)pKey1,(cons
bc210 74 20 63 68 61 72 2a 29 70 4b 65 79 32 2c 6e 31  t char*)pKey2,n1
bc220 29 3b 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 61  );.}.../* Link a
bc230 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74  n element into t
bc240 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f  he hash table.*/
bc250 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73  .static void ins
bc260 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20 20 48 61  ertElement(.  Ha
bc270 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20  sh *pH,         
bc280 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70       /* The comp
bc290 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20  lete hash table 
bc2a0 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20  */.  struct _ht 
bc2b0 2a 70 45 6e 74 72 79 2c 20 20 20 20 2f 2a 20 54  *pEntry,    /* T
bc2c0 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 77 68  he entry into wh
bc2d0 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e 73 65  ich pNew is inse
bc2e0 72 74 65 64 20 2a 2f 0a 20 20 48 61 73 68 45 6c  rted */.  HashEl
bc2f0 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20  em *pNew        
bc300 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20   /* The element 
bc310 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a  to be inserted *
bc320 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  /.){.  HashElem 
bc330 2a 70 48 65 61 64 3b 20 20 20 20 20 20 20 2f 2a  *pHead;       /*
bc340 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 61   First element a
bc350 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72 79  lready in pEntry
bc360 20 2a 2f 0a 20 20 70 48 65 61 64 20 3d 20 70 45   */.  pHead = pE
bc370 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 69  ntry->chain;.  i
bc380 66 28 20 70 48 65 61 64 20 29 7b 0a 20 20 20 20  f( pHead ){.    
bc390 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 65  pNew->next = pHe
bc3a0 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72  ad;.    pNew->pr
bc3b0 65 76 20 3d 20 70 48 65 61 64 2d 3e 70 72 65 76  ev = pHead->prev
bc3c0 3b 0a 20 20 20 20 69 66 28 20 70 48 65 61 64 2d  ;.    if( pHead-
bc3d0 3e 70 72 65 76 20 29 7b 20 70 48 65 61 64 2d 3e  >prev ){ pHead->
bc3e0 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 70 4e 65  prev->next = pNe
bc3f0 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65 20 20 20  w; }.    else   
bc400 20 20 20 20 20 20 20 20 20 20 7b 20 70 48 2d 3e            { pH->
bc410 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 20 7d 0a  first = pNew; }.
bc420 20 20 20 20 70 48 65 61 64 2d 3e 70 72 65 76 20      pHead->prev 
bc430 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b  = pNew;.  }else{
bc440 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20  .    pNew->next 
bc450 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20  = pH->first;.   
bc460 20 69 66 28 20 70 48 2d 3e 66 69 72 73 74 20 29   if( pH->first )
bc470 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70 72 65  { pH->first->pre
bc480 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20  v = pNew; }.    
bc490 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 30 3b 0a  pNew->prev = 0;.
bc4a0 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20      pH->first = 
bc4b0 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  pNew;.  }.  pEnt
bc4c0 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 70  ry->count++;.  p
bc4d0 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70  Entry->chain = p
bc4e0 4e 65 77 3b 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69  New;.}.../* Resi
bc4f0 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  ze the hash tabl
bc500 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e  e so that it can
bc510 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22  tains "new_size"
bc520 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 20 22 6e 65   buckets..** "ne
bc530 77 5f 73 69 7a 65 22 20 6d 75 73 74 20 62 65 20  w_size" must be 
bc540 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 20 54  a power of 2.  T
bc550 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69  he hash table mi
bc560 67 68 74 20 66 61 69 6c 20 0a 2a 2a 20 74 6f 20  ght fail .** to 
bc570 72 65 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65  resize if sqlite
bc580 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  3_malloc() fails
bc590 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
bc5a0 20 72 65 68 61 73 68 28 48 61 73 68 20 2a 70 48   rehash(Hash *pH
bc5b0 2c 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b  , int new_size){
bc5c0 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a 6e  .  struct _ht *n
bc5d0 65 77 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20  ew_ht;          
bc5e0 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 68 61 73    /* The new has
bc5f0 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73  h table */.  Has
bc600 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65  hElem *elem, *ne
bc610 78 74 5f 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 46  xt_elem;    /* F
bc620 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
bc630 65 78 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74  existing element
bc640 73 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  s */..#ifdef SQL
bc650 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f  ITE_MALLOC_SOFT_
bc660 4c 49 4d 49 54 0a 20 20 69 66 28 20 6e 65 77 5f  LIMIT.  if( new_
bc670 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75  size*sizeof(stru
bc680 63 74 20 5f 68 74 29 3e 53 51 4c 49 54 45 5f 4d  ct _ht)>SQLITE_M
bc690 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54  ALLOC_SOFT_LIMIT
bc6a0 20 29 7b 0a 20 20 20 20 6e 65 77 5f 73 69 7a 65   ){.    new_size
bc6b0 20 3d 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43   = SQLITE_MALLOC
bc6c0 5f 53 4f 46 54 5f 4c 49 4d 49 54 2f 73 69 7a 65  _SOFT_LIMIT/size
bc6d0 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a  of(struct _ht);.
bc6e0 20 20 7d 0a 20 20 69 66 28 20 6e 65 77 5f 73 69    }.  if( new_si
bc6f0 7a 65 3d 3d 70 48 2d 3e 68 74 73 69 7a 65 20 29  ze==pH->htsize )
bc700 20 72 65 74 75 72 6e 3b 0a 23 65 6e 64 69 66 0a   return;.#endif.
bc710 0a 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 61  .  /* There is a
bc720 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
bc730 5f 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65  _malloc() inside
bc740 20 72 65 68 61 73 68 28 29 2e 20 49 66 20 74 68   rehash(). If th
bc750 65 72 65 20 69 73 0a 20 20 2a 2a 20 61 6c 72 65  ere is.  ** alre
bc760 61 64 79 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f  ady an allocatio
bc770 6e 20 61 74 20 70 48 2d 3e 68 74 2c 20 74 68 65  n at pH->ht, the
bc780 6e 20 69 66 20 74 68 69 73 20 6d 61 6c 6c 6f 63  n if this malloc
bc790 28 29 20 66 61 69 6c 73 20 69 74 0a 20 20 2a 2a  () fails it.  **
bc7a0 20 69 73 20 62 65 6e 69 67 6e 20 28 73 69 6e 63   is benign (sinc
bc7b0 65 20 66 61 69 6c 69 6e 67 20 74 6f 20 72 65 73  e failing to res
bc7c0 69 7a 65 20 61 20 68 61 73 68 20 74 61 62 6c 65  ize a hash table
bc7d0 20 69 73 20 61 20 70 65 72 66 6f 72 6d 61 6e 63   is a performanc
bc7e0 65 0a 20 20 2a 2a 20 68 69 74 20 6f 6e 6c 79 2c  e.  ** hit only,
bc7f0 20 6e 6f 74 20 61 20 66 61 74 61 6c 20 65 72 72   not a fatal err
bc800 6f 72 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  or)..  */.  if( 
bc810 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 20 73  pH->htsize>0 ) s
bc820 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
bc830 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 6e 65 77  nMalloc();.  new
bc840 5f 68 74 20 3d 20 28 73 74 72 75 63 74 20 5f 68  _ht = (struct _h
bc850 74 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  t *)sqlite3Mallo
bc860 63 5a 65 72 6f 28 20 6e 65 77 5f 73 69 7a 65 2a  cZero( new_size*
bc870 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68  sizeof(struct _h
bc880 74 29 20 29 3b 0a 20 20 69 66 28 20 70 48 2d 3e  t) );.  if( pH->
bc890 68 74 73 69 7a 65 3e 30 20 29 20 73 71 6c 69 74  htsize>0 ) sqlit
bc8a0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
bc8b0 63 28 29 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f  c();..  if( new_
bc8c0 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ht==0 ) return;.
bc8d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
bc8e0 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74  H->ht);.  pH->ht
bc8f0 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d   = new_ht;.  pH-
bc900 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69  >htsize = new_si
bc910 7a 65 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70  ze;.  for(elem=p
bc920 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69  H->first, pH->fi
bc930 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65  rst=0; elem; ele
bc940 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a  m = next_elem){.
bc950 20 20 20 20 69 6e 74 20 68 20 3d 20 73 74 72 48      int h = strH
bc960 61 73 68 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20  ash(elem->pKey, 
bc970 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20 26 20 28 6e  elem->nKey) & (n
bc980 65 77 5f 73 69 7a 65 2d 31 29 3b 0a 20 20 20 20  ew_size-1);.    
bc990 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d  next_elem = elem
bc9a0 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 6e 73 65  ->next;.    inse
bc9b0 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 6e  rtElement(pH, &n
bc9c0 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b  ew_ht[h], elem);
bc9d0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  .  }.}../* This 
bc9e0 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e  function (for in
bc9f0 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29  ternal use only)
bca00 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d   locates an elem
bca10 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73  ent in an.** has
bca20 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74  h table that mat
bca30 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b  ches the given k
bca40 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f  ey.  The hash fo
bca50 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a  r this key has.*
bca60 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  * already been c
bca70 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 70  omputed and is p
bca80 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74 68  assed as the 4th
bca90 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73   parameter..*/.s
bcaa0 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a  tatic HashElem *
bcab0 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e  findElementGiven
bcac0 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61  Hash(.  const Ha
bcad0 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54  sh *pH,     /* T
bcae0 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72  he pH to be sear
bcaf0 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ched */.  const 
bcb00 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a  void *pKey,   /*
bcb10 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20   The key we are 
bcb20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f  searching for */
bcb30 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a 20 20 69  .  int nKey,.  i
bcb40 6e 74 20 68 20 20 20 20 20 20 20 20 20 20 20 20  nt h            
bcb50 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66     /* The hash f
bcb60 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a  or this key. */.
bcb70 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65  ){.  HashElem *e
bcb80 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  lem;            
bcb90 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c      /* Used to l
bcba0 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65  oop thru the ele
bcbb0 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69  ment list */.  i
bcbc0 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20  nt count;       
bcbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bcbe0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
bcbf0 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74  nts left to test
bcc00 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68   */..  if( pH->h
bcc10 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  t ){.    struct 
bcc20 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70  _ht *pEntry = &p
bcc30 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c  H->ht[h];.    el
bcc40 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61  em = pEntry->cha
bcc50 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20  in;.    count = 
bcc60 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20  pEntry->count;. 
bcc70 20 20 20 77 68 69 6c 65 28 20 63 6f 75 6e 74 2d     while( count-
bcc80 2d 20 26 26 20 65 6c 65 6d 20 29 7b 0a 20 20 20  - && elem ){.   
bcc90 20 20 20 69 66 28 20 73 74 72 43 6f 6d 70 61 72     if( strCompar
bcca0 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 65 6c 65  e(elem->pKey,ele
bccb0 6d 2d 3e 6e 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65  m->nKey,pKey,nKe
bccc0 79 29 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20  y)==0 ){ .      
bccd0 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 20    return elem;. 
bcce0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 6c 65       }.      ele
bccf0 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
bcd00 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
bcd10 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f  rn 0;.}../* Remo
bcd20 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72  ve a single entr
bcd30 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  y from the hash 
bcd40 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f  table given a po
bcd50 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a  inter to that.**
bcd60 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68   element and a h
bcd70 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65  ash on the eleme
bcd80 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61  nt's key..*/.sta
bcd90 74 69 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 45  tic void removeE
bcda0 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28  lementGivenHash(
bcdb0 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20  .  Hash *pH,    
bcdc0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63       /* The pH c
bcdd0 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22  ontaining "elem"
bcde0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20   */.  HashElem* 
bcdf0 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65  elem,   /* The e
bce00 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d  lement to be rem
bce10 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48  oved from the pH
bce20 20 2a 2f 0a 20 20 69 6e 74 20 68 20 20 20 20 20   */.  int h     
bce30 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
bce40 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c  value for the el
bce50 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74  ement */.){.  st
bce60 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79  ruct _ht *pEntry
bce70 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72  ;.  if( elem->pr
bce80 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e  ev ){.    elem->
bce90 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65  prev->next = ele
bcea0 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73  m->next; .  }els
bceb0 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74  e{.    pH->first
bcec0 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
bced0 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e   }.  if( elem->n
bcee0 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d  ext ){.    elem-
bcef0 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c  >next->prev = el
bcf00 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20  em->prev;.  }.  
bcf10 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74  pEntry = &pH->ht
bcf20 5b 68 5d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72  [h];.  if( pEntr
bcf30 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29  y->chain==elem )
bcf40 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68  {.    pEntry->ch
bcf50 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  ain = elem->next
bcf60 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e  ;.  }.  pEntry->
bcf70 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70  count--;.  if( p
bcf80 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20  Entry->count<=0 
bcf90 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63  ){.    pEntry->c
bcfa0 68 61 69 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  hain = 0;.  }.  
bcfb0 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20  if( pH->copyKey 
bcfc0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
bcfd0 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b  ree(elem->pKey);
bcfe0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
bcff0 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70  ree( elem );.  p
bd000 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66  H->count--;.  if
bd010 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29  ( pH->count<=0 )
bd020 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48  {.    assert( pH
bd030 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20  ->first==0 );.  
bd040 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f    assert( pH->co
bd050 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71  unt==0 );.    sq
bd060 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 70  lite3HashClear(p
bd070 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74  H);.  }.}../* At
bd080 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20  tempt to locate 
bd090 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  an element of th
bd0a0 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 20  e hash table pH 
bd0b0 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68  with a key.** th
bd0c0 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c  at matches pKey,
bd0d0 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 61 20  nKey.  Return a 
bd0e0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  pointer to the c
bd0f0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a 2a 2a  orresponding .**
bd100 20 48 61 73 68 45 6c 65 6d 20 73 74 72 75 63 74   HashElem struct
bd110 75 72 65 20 66 6f 72 20 74 68 69 73 20 65 6c 65  ure for this ele
bd120 6d 65 6e 74 20 69 66 20 69 74 20 69 73 20 66 6f  ment if it is fo
bd130 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 0a 2a 2a 20  und, or NULL.** 
bd140 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51  otherwise..*/.SQ
bd150 4c 49 54 45 5f 50 52 49 56 41 54 45 20 48 61 73  LITE_PRIVATE Has
bd160 68 45 6c 65 6d 20 2a 73 71 6c 69 74 65 33 48 61  hElem *sqlite3Ha
bd170 73 68 46 69 6e 64 45 6c 65 6d 28 63 6f 6e 73 74  shFindElem(const
bd180 20 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74   Hash *pH, const
bd190 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
bd1a0 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 3b   nKey){.  int h;
bd1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
bd1c0 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f  A hash on key */
bd1d0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  .  HashElem *ele
bd1e0 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65  m;    /* The ele
bd1f0 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68 65  ment that matche
bd200 73 20 6b 65 79 20 2a 2f 0a 0a 20 20 69 66 28 20  s key */..  if( 
bd210 70 48 3d 3d 30 20 7c 7c 20 70 48 2d 3e 68 74 3d  pH==0 || pH->ht=
bd220 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
bd230 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65   h = strHash(pKe
bd240 79 2c 6e 4b 65 79 29 3b 0a 20 20 65 6c 65 6d 20  y,nKey);.  elem 
bd250 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76  = findElementGiv
bd260 65 6e 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e  enHash(pH,pKey,n
bd270 4b 65 79 2c 20 68 20 25 20 70 48 2d 3e 68 74 73  Key, h % pH->hts
bd280 69 7a 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 65  ize);.  return e
bd290 6c 65 6d 3b 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d  lem;.}../* Attem
bd2a0 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20  pt to locate an 
bd2b0 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68  element of the h
bd2c0 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74  ash table pH wit
bd2d0 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20  h a key.** that 
bd2e0 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65  matches pKey,nKe
bd2f0 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 64  y.  Return the d
bd300 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c 65  ata for this ele
bd310 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a 2a  ment if it is.**
bd320 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20   found, or NULL 
bd330 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
bd340 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  atch..*/.SQLITE_
bd350 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
bd360 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f  lite3HashFind(co
bd370 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63 6f  nst Hash *pH, co
bd380 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
bd390 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61 73  int nKey){.  Has
bd3a0 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20  hElem *elem;    
bd3b0 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74  /* The element t
bd3c0 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20  hat matches key 
bd3d0 2a 2f 0a 20 20 65 6c 65 6d 20 3d 20 73 71 6c 69  */.  elem = sqli
bd3e0 74 65 33 48 61 73 68 46 69 6e 64 45 6c 65 6d 28  te3HashFindElem(
bd3f0 70 48 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b  pH, pKey, nKey);
bd400 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f  .  return elem ?
bd410 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b   elem->data : 0;
bd420 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e  .}../* Insert an
bd430 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68   element into th
bd440 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e  e hash table pH.
bd450 20 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65    The key is pKe
bd460 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68  y,nKey.** and th
bd470 65 20 64 61 74 61 20 69 73 20 22 64 61 74 61 22  e data is "data"
bd480 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c  ..**.** If no el
bd490 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69 74  ement exists wit
bd4a0 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79  h a matching key
bd4b0 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20  , then a new.** 
bd4c0 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74  element is creat
bd4d0 65 64 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74  ed.  A copy of t
bd4e0 68 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69  he key is made i
bd4f0 66 20 74 68 65 20 63 6f 70 79 4b 65 79 0a 2a 2a  f the copyKey.**
bd500 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e   flag is set.  N
bd510 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
bd520 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65  .**.** If anothe
bd530 72 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64  r element alread
bd540 79 20 65 78 69 73 74 73 20 77 69 74 68 20 74 68  y exists with th
bd550 65 20 73 61 6d 65 20 6b 65 79 2c 20 74 68 65 6e  e same key, then
bd560 20 74 68 65 0a 2a 2a 20 6e 65 77 20 64 61 74 61   the.** new data
bd570 20 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c   replaces the ol
bd580 64 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 6f  d data and the o
bd590 6c 64 20 64 61 74 61 20 69 73 20 72 65 74 75 72  ld data is retur
bd5a0 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20  ned..** The key 
bd5b0 69 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 69 6e  is not copied in
bd5c0 20 74 68 69 73 20 69 6e 73 74 61 6e 63 65 2e 20   this instance. 
bd5d0 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   If a malloc fai
bd5e0 6c 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  ls, then.** the 
bd5f0 6e 65 77 20 64 61 74 61 20 69 73 20 72 65 74 75  new data is retu
bd600 72 6e 65 64 20 61 6e 64 20 74 68 65 20 68 61 73  rned and the has
bd610 68 20 74 61 62 6c 65 20 69 73 20 75 6e 63 68 61  h table is uncha
bd620 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  nged..**.** If t
bd630 68 65 20 22 64 61 74 61 22 20 70 61 72 61 6d 65  he "data" parame
bd640 74 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ter to this func
bd650 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68  tion is NULL, th
bd660 65 6e 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e  en the.** elemen
bd670 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  t corresponding 
bd680 74 6f 20 22 6b 65 79 22 20 69 73 20 72 65 6d 6f  to "key" is remo
bd690 76 65 64 20 66 72 6f 6d 20 74 68 65 20 68 61 73  ved from the has
bd6a0 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  h table..*/.SQLI
bd6b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
bd6c0 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65  *sqlite3HashInse
bd6d0 72 74 28 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e  rt(Hash *pH, con
bd6e0 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
bd6f0 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 64  nt nKey, void *d
bd700 61 74 61 29 7b 0a 20 20 69 6e 74 20 68 72 61 77  ata){.  int hraw
bd710 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
bd720 20 52 61 77 20 68 61 73 68 20 76 61 6c 75 65 20   Raw hash value 
bd730 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20  of the key */.  
bd740 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20  int h;          
bd750 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73        /* the has
bd760 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64  h of the key mod
bd770 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73  ulo hash table s
bd780 69 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  ize */.  HashEle
bd790 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f  m *elem;       /
bd7a0 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74  * Used to loop t
bd7b0 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20  hru the element 
bd7c0 6c 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c  list */.  HashEl
bd7d0 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20  em *new_elem;   
bd7e0 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61  /* New element a
bd7f0 64 64 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a  dded to the pH *
bd800 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21  /..  assert( pH!
bd810 3d 30 20 29 3b 0a 20 20 68 72 61 77 20 3d 20 73  =0 );.  hraw = s
bd820 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65  trHash(pKey, nKe
bd830 79 29 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74  y);.  if( pH->ht
bd840 73 69 7a 65 20 29 7b 0a 20 20 20 20 68 20 3d 20  size ){.    h = 
bd850 68 72 61 77 20 25 20 70 48 2d 3e 68 74 73 69 7a  hraw % pH->htsiz
bd860 65 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 66 69  e;.    elem = fi
bd870 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61  ndElementGivenHa
bd880 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c  sh(pH,pKey,nKey,
bd890 68 29 3b 0a 20 20 20 20 69 66 28 20 65 6c 65 6d  h);.    if( elem
bd8a0 20 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a   ){.      void *
bd8b0 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d  old_data = elem-
bd8c0 3e 64 61 74 61 3b 0a 20 20 20 20 20 20 69 66 28  >data;.      if(
bd8d0 20 64 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20   data==0 ){.    
bd8e0 20 20 20 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e      removeElemen
bd8f0 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 65 6c  tGivenHash(pH,el
bd900 65 6d 2c 68 29 3b 0a 20 20 20 20 20 20 7d 65 6c  em,h);.      }el
bd910 73 65 7b 0a 20 20 20 20 20 20 20 20 65 6c 65 6d  se{.        elem
bd920 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20  ->data = data;. 
bd930 20 20 20 20 20 20 20 69 66 28 20 21 70 48 2d 3e         if( !pH->
bd940 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20 20 20  copyKey ){.     
bd950 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b 65 79 20       elem->pKey 
bd960 3d 20 28 76 6f 69 64 20 2a 29 70 4b 65 79 3b 0a  = (void *)pKey;.
bd970 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
bd980 20 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65    assert(nKey==e
bd990 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20  lem->nKey);.    
bd9a0 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
bd9b0 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 20 20 7d   old_data;.    }
bd9c0 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61 3d  .  }.  if( data=
bd9d0 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
bd9e0 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28 48 61 73   new_elem = (Has
bd9f0 68 45 6c 65 6d 2a 29 73 71 6c 69 74 65 33 4d 61  hElem*)sqlite3Ma
bda00 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 48 61 73  lloc( sizeof(Has
bda10 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66 28 20  hElem) );.  if( 
bda20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20 72 65  new_elem==0 ) re
bda30 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 69 66 28  turn data;.  if(
bda40 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20   pH->copyKey && 
bda50 70 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20 20 6e  pKey!=0 ){.    n
bda60 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20  ew_elem->pKey = 
bda70 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e  sqlite3Malloc( n
bda80 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 6e  Key );.    if( n
bda90 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 3d 3d 30  ew_elem->pKey==0
bdaa0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
bdab0 33 5f 66 72 65 65 28 6e 65 77 5f 65 6c 65 6d 29  3_free(new_elem)
bdac0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 64  ;.      return d
bdad0 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  ata;.    }.    m
bdae0 65 6d 63 70 79 28 28 76 6f 69 64 2a 29 6e 65 77  emcpy((void*)new
bdaf0 5f 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 70 4b 65  _elem->pKey, pKe
bdb00 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73  y, nKey);.  }els
bdb10 65 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d  e{.    new_elem-
bdb20 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 2a 29 70  >pKey = (void*)p
bdb30 4b 65 79 3b 0a 20 20 7d 0a 20 20 6e 65 77 5f 65  Key;.  }.  new_e
bdb40 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79  lem->nKey = nKey
bdb50 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b  ;.  pH->count++;
bdb60 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a  .  if( pH->htsiz
bdb70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 68 61  e==0 ){.    reha
bdb80 73 68 28 70 48 2c 20 31 32 38 2f 73 69 7a 65 6f  sh(pH, 128/sizeo
bdb90 66 28 70 48 2d 3e 68 74 5b 30 5d 29 29 3b 0a 20  f(pH->ht[0]));. 
bdba0 20 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a     if( pH->htsiz
bdbb0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 48  e==0 ){.      pH
bdbc0 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 20  ->count = 0;.   
bdbd0 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b     if( pH->copyK
bdbe0 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ey ){.        sq
bdbf0 6c 69 74 65 33 5f 66 72 65 65 28 6e 65 77 5f 65  lite3_free(new_e
bdc00 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20  lem->pKey);.    
bdc10 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
bdc20 33 5f 66 72 65 65 28 6e 65 77 5f 65 6c 65 6d 29  3_free(new_elem)
bdc30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 64  ;.      return d
bdc40 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ata;.    }.  }. 
bdc50 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 20 3e   if( pH->count >
bdc60 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20   pH->htsize ){. 
bdc70 20 20 20 72 65 68 61 73 68 28 70 48 2c 70 48 2d     rehash(pH,pH-
bdc80 3e 68 74 73 69 7a 65 2a 32 29 3b 0a 20 20 7d 0a  >htsize*2);.  }.
bdc90 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 68 74    assert( pH->ht
bdca0 73 69 7a 65 3e 30 20 29 3b 0a 20 20 68 20 3d 20  size>0 );.  h = 
bdcb0 68 72 61 77 20 25 20 70 48 2d 3e 68 74 73 69 7a  hraw % pH->htsiz
bdcc0 65 3b 0a 20 20 69 6e 73 65 72 74 45 6c 65 6d 65  e;.  insertEleme
bdcd0 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 5b 68  nt(pH, &pH->ht[h
bdce0 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20  ], new_elem);.  
bdcf0 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61 20 3d  new_elem->data =
bdd00 20 64 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20   data;.  return 
bdd10 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  0;.}../*********
bdd20 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73  ***** End of has
bdd30 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.c ************
bdd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd60 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
bdd70 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
bdd80 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a   opcodes.c *****
bdd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bddb0 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74  ****/./* Automat
bddc0 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
bddd0 2e 20 20 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a  .  Do not edit *
bdde0 2f 0a 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f  /./* See the mko
bddf0 70 63 6f 64 65 63 2e 61 77 6b 20 73 63 72 69 70  pcodec.awk scrip
bde00 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a  t for details. *
bde10 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
bde20 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
bde30 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  IN) || !defined(
bde40 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e  NDEBUG) || defin
bde50 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29  ed(VDBE_PROFILE)
bde60 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
bde70 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45  TE_DEBUG).SQLITE
bde80 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
bde90 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f  har *sqlite3Opco
bdea0 64 65 4e 61 6d 65 28 69 6e 74 20 69 29 7b 0a 20  deName(int i){. 
bdeb0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
bdec0 72 20 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b  r *const azName[
bded0 5d 20 3d 20 7b 20 22 3f 22 2c 0a 20 20 20 20 20  ] = { "?",.     
bdee0 2f 2a 20 20 20 31 20 2a 2f 20 22 56 4e 65 78 74  /*   1 */ "VNext
bdef0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 32 20 2a  ",.     /*   2 *
bdf00 2f 20 22 41 66 66 69 6e 69 74 79 22 2c 0a 20 20  / "Affinity",.  
bdf10 20 20 20 2f 2a 20 20 20 33 20 2a 2f 20 22 43 6f     /*   3 */ "Co
bdf20 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20  lumn",.     /*  
bdf30 20 34 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65   4 */ "SetCookie
bdf40 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35 20 2a  ",.     /*   5 *
bdf50 2f 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f  / "Seek",.     /
bdf60 2a 20 20 20 36 20 2a 2f 20 22 53 65 71 75 65 6e  *   6 */ "Sequen
bdf70 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37  ce",.     /*   7
bdf80 20 2a 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c   */ "Savepoint",
bdf90 0a 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20  .     /*   8 */ 
bdfa0 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f  "RowKey",.     /
bdfb0 2a 20 20 20 39 20 2a 2f 20 22 53 43 6f 70 79 22  *   9 */ "SCopy"
bdfc0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f  ,.     /*  10 */
bdfd0 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 20   "OpenWrite",.  
bdfe0 20 20 20 2f 2a 20 20 31 31 20 2a 2f 20 22 49 66     /*  11 */ "If
bdff0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a  ",.     /*  12 *
be000 2f 20 22 56 52 6f 77 69 64 22 2c 0a 20 20 20 20  / "VRowid",.    
be010 20 2f 2a 20 20 31 33 20 2a 2f 20 22 43 6f 6c 6c   /*  13 */ "Coll
be020 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  Seq",.     /*  1
be030 34 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64 22 2c  4 */ "OpenRead",
be040 0a 20 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f 20  .     /*  15 */ 
be050 22 45 78 70 69 72 65 22 2c 0a 20 20 20 20 20 2f  "Expire",.     /
be060 2a 20 20 31 36 20 2a 2f 20 22 41 75 74 6f 43 6f  *  16 */ "AutoCo
be070 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  mmit",.     /*  
be080 31 37 20 2a 2f 20 22 50 61 67 65 63 6f 75 6e 74  17 */ "Pagecount
be090 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38 20 2a  ",.     /*  18 *
be0a0 2f 20 22 49 6e 74 65 67 72 69 74 79 43 6b 22 2c  / "IntegrityCk",
be0b0 0a 20 20 20 20 20 2f 2a 20 20 31 39 20 2a 2f 20  .     /*  19 */ 
be0c0 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  "Not",.     /*  
be0d0 32 30 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20  20 */ "Sort",.  
be0e0 20 20 20 2f 2a 20 20 32 31 20 2a 2f 20 22 43 6f     /*  21 */ "Co
be0f0 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 32  py",.     /*  22
be100 20 2a 2f 20 22 54 72 61 63 65 22 2c 0a 20 20 20   */ "Trace",.   
be110 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 46 75 6e    /*  23 */ "Fun
be120 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20  ction",.     /* 
be130 20 32 34 20 2a 2f 20 22 49 66 4e 65 67 22 2c 0a   24 */ "IfNeg",.
be140 20 20 20 20 20 2f 2a 20 20 32 35 20 2a 2f 20 22       /*  25 */ "
be150 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Noop",.     /*  
be160 32 36 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a  26 */ "Return",.
be170 20 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22       /*  27 */ "
be180 4e 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20  NewRowid",.     
be190 2f 2a 20 20 32 38 20 2a 2f 20 22 56 61 72 69 61  /*  28 */ "Varia
be1a0 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32  ble",.     /*  2
be1b0 39 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c 0a 20  9 */ "String",. 
be1c0 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 52      /*  30 */ "R
be1d0 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20 20  ealAffinity",.  
be1e0 20 20 20 2f 2a 20 20 33 31 20 2a 2f 20 22 56 52     /*  31 */ "VR
be1f0 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20  ename",.     /* 
be200 20 33 32 20 2a 2f 20 22 50 61 72 73 65 53 63 68   32 */ "ParseSch
be210 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  ema",.     /*  3
be220 33 20 2a 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20  3 */ "VOpen",.  
be230 20 20 20 2f 2a 20 20 33 34 20 2a 2f 20 22 43 6c     /*  34 */ "Cl
be240 6f 73 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  ose",.     /*  3
be250 35 20 2a 2f 20 22 43 72 65 61 74 65 49 6e 64 65  5 */ "CreateInde
be260 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 20  x",.     /*  36 
be270 2a 2f 20 22 49 73 55 6e 69 71 75 65 22 2c 0a 20  */ "IsUnique",. 
be280 20 20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 4e      /*  37 */ "N
be290 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f  otFound",.     /
be2a0 2a 20 20 33 38 20 2a 2f 20 22 49 6e 74 36 34 22  *  38 */ "Int64"
be2b0 2c 0a 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f  ,.     /*  39 */
be2c0 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20 20   "MustBeInt",.  
be2d0 20 20 20 2f 2a 20 20 34 30 20 2a 2f 20 22 48 61     /*  40 */ "Ha
be2e0 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31  lt",.     /*  41
be2f0 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20 20 20   */ "Rowid",.   
be300 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22 49 64 78    /*  42 */ "Idx
be310 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33  LT",.     /*  43
be320 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a 20 20   */ "AddImm",.  
be330 20 20 20 2f 2a 20 20 34 34 20 2a 2f 20 22 53 74     /*  44 */ "St
be340 61 74 65 6d 65 6e 74 22 2c 0a 20 20 20 20 20 2f  atement",.     /
be350 2a 20 20 34 35 20 2a 2f 20 22 52 6f 77 44 61 74  *  45 */ "RowDat
be360 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 36 20  a",.     /*  46 
be370 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20  */ "MemMax",.   
be380 20 20 2f 2a 20 20 34 37 20 2a 2f 20 22 4e 6f 74    /*  47 */ "Not
be390 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20 2f 2a  Exists",.     /*
be3a0 20 20 34 38 20 2a 2f 20 22 47 6f 73 75 62 22 2c    48 */ "Gosub",
be3b0 0a 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20  .     /*  49 */ 
be3c0 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 20 20  "Integer",.     
be3d0 2f 2a 20 20 35 30 20 2a 2f 20 22 50 72 65 76 22  /*  50 */ "Prev"
be3e0 2c 0a 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f  ,.     /*  51 */
be3f0 20 22 52 6f 77 53 65 74 52 65 61 64 22 2c 0a 20   "RowSetRead",. 
be400 20 20 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 52      /*  52 */ "R
be410 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20 20  owSetAdd",.     
be420 2f 2a 20 20 35 33 20 2a 2f 20 22 56 43 6f 6c 75  /*  53 */ "VColu
be430 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34  mn",.     /*  54
be440 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c 65   */ "CreateTable
be450 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 35 20 2a  ",.     /*  55 *
be460 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20 20 20 2f  / "Last",.     /
be470 2a 20 20 35 36 20 2a 2f 20 22 53 65 65 6b 4c 65  *  56 */ "SeekLe
be480 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a  ",.     /*  57 *
be490 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a  / "IncrVacuum",.
be4a0 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22       /*  58 */ "
be4b0 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20  IdxRowid",.     
be4c0 2f 2a 20 20 35 39 20 2a 2f 20 22 52 65 73 65 74  /*  59 */ "Reset
be4d0 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20  Count",.     /* 
be4e0 20 36 30 20 2a 2f 20 22 43 6f 6e 74 65 78 74 50   60 */ "ContextP
be4f0 75 73 68 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  ush",.     /*  6
be500 31 20 2a 2f 20 22 59 69 65 6c 64 22 2c 0a 20 20  1 */ "Yield",.  
be510 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44 72     /*  62 */ "Dr
be520 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20 20 20  opTrigger",.    
be530 20 2f 2a 20 20 36 33 20 2a 2f 20 22 4f 72 22 2c   /*  63 */ "Or",
be540 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f 20  .     /*  64 */ 
be550 22 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  "And",.     /*  
be560 36 35 20 2a 2f 20 22 44 72 6f 70 49 6e 64 65 78  65 */ "DropIndex
be570 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 20 2a  ",.     /*  66 *
be580 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20 20 20  / "IdxGE",.     
be590 2f 2a 20 20 36 37 20 2a 2f 20 22 49 64 78 44 65  /*  67 */ "IdxDe
be5a0 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  lete",.     /*  
be5b0 36 38 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a  68 */ "IsNull",.
be5c0 20 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22       /*  69 */ "
be5d0 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f  NotNull",.     /
be5e0 2a 20 20 37 30 20 2a 2f 20 22 4e 65 22 2c 0a 20  *  70 */ "Ne",. 
be5f0 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22 45      /*  71 */ "E
be600 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 32 20  q",.     /*  72 
be610 2a 2f 20 22 47 74 22 2c 0a 20 20 20 20 20 2f 2a  */ "Gt",.     /*
be620 20 20 37 33 20 2a 2f 20 22 4c 65 22 2c 0a 20 20    73 */ "Le",.  
be630 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4c 74     /*  74 */ "Lt
be640 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 35 20 2a  ",.     /*  75 *
be650 2f 20 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20  / "Ge",.     /* 
be660 20 37 36 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c   76 */ "Vacuum",
be670 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20  .     /*  77 */ 
be680 22 42 69 74 41 6e 64 22 2c 0a 20 20 20 20 20 2f  "BitAnd",.     /
be690 2a 20 20 37 38 20 2a 2f 20 22 42 69 74 4f 72 22  *  78 */ "BitOr"
be6a0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f  ,.     /*  79 */
be6b0 20 22 53 68 69 66 74 4c 65 66 74 22 2c 0a 20 20   "ShiftLeft",.  
be6c0 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 53 68     /*  80 */ "Sh
be6d0 69 66 74 52 69 67 68 74 22 2c 0a 20 20 20 20 20  iftRight",.     
be6e0 2f 2a 20 20 38 31 20 2a 2f 20 22 41 64 64 22 2c  /*  81 */ "Add",
be6f0 0a 20 20 20 20 20 2f 2a 20 20 38 32 20 2a 2f 20  .     /*  82 */ 
be700 22 53 75 62 74 72 61 63 74 22 2c 0a 20 20 20 20  "Subtract",.    
be710 20 2f 2a 20 20 38 33 20 2a 2f 20 22 4d 75 6c 74   /*  83 */ "Mult
be720 69 70 6c 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20  iply",.     /*  
be730 38 34 20 2a 2f 20 22 44 69 76 69 64 65 22 2c 0a  84 */ "Divide",.
be740 20 20 20 20 20 2f 2a 20 20 38 35 20 2a 2f 20 22       /*  85 */ "
be750 52 65 6d 61 69 6e 64 65 72 22 2c 0a 20 20 20 20  Remainder",.    
be760 20 2f 2a 20 20 38 36 20 2a 2f 20 22 43 6f 6e 63   /*  86 */ "Conc
be770 61 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 37  at",.     /*  87
be780 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20   */ "IfNot",.   
be790 20 20 2f 2a 20 20 38 38 20 2a 2f 20 22 44 72 6f    /*  88 */ "Dro
be7a0 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a  pTable",.     /*
be7b0 20 20 38 39 20 2a 2f 20 22 53 65 65 6b 4c 74 22    89 */ "SeekLt"
be7c0 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30 20 2a 2f  ,.     /*  90 */
be7d0 20 22 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20   "BitNot",.     
be7e0 2f 2a 20 20 39 31 20 2a 2f 20 22 53 74 72 69 6e  /*  91 */ "Strin
be7f0 67 38 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32  g8",.     /*  92
be800 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22   */ "MakeRecord"
be810 2c 0a 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f  ,.     /*  93 */
be820 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20 20   "ResultRow",.  
be830 20 20 20 2f 2a 20 20 39 34 20 2a 2f 20 22 44 65     /*  94 */ "De
be840 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  lete",.     /*  
be850 39 35 20 2a 2f 20 22 41 67 67 46 69 6e 61 6c 22  95 */ "AggFinal"
be860 2c 0a 20 20 20 20 20 2f 2a 20 20 39 36 20 2a 2f  ,.     /*  96 */
be870 20 22 43 6f 6d 70 61 72 65 22 2c 0a 20 20 20 20   "Compare",.    
be880 20 2f 2a 20 20 39 37 20 2a 2f 20 22 47 6f 74 6f   /*  97 */ "Goto
be890 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38 20 2a  ",.     /*  98 *
be8a0 2f 20 22 54 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20  / "TableLock",. 
be8b0 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22 43      /*  99 */ "C
be8c0 6c 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31  lear",.     /* 1
be8d0 30 30 20 2a 2f 20 22 56 65 72 69 66 79 43 6f 6f  00 */ "VerifyCoo
be8e0 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  kie",.     /* 10
be8f0 31 20 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a  1 */ "AggStep",.
be900 20 20 20 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22       /* 102 */ "
be910 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 22 2c 0a  SetNumColumns",.
be920 20 20 20 20 20 2f 2a 20 31 30 33 20 2a 2f 20 22       /* 103 */ "
be930 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a 20 20  Transaction",.  
be940 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 56 46     /* 104 */ "VF
be950 69 6c 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  ilter",.     /* 
be960 31 30 35 20 2a 2f 20 22 56 44 65 73 74 72 6f 79  105 */ "VDestroy
be970 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 36 20 2a  ",.     /* 106 *
be980 2f 20 22 43 6f 6e 74 65 78 74 50 6f 70 22 2c 0a  / "ContextPop",.
be990 20 20 20 20 20 2f 2a 20 31 30 37 20 2a 2f 20 22       /* 107 */ "
be9a0 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Next",.     /* 1
be9b0 30 38 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74  08 */ "IdxInsert
be9c0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 2a  ",.     /* 109 *
be9d0 2f 20 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20  / "SeekGe",.    
be9e0 20 2f 2a 20 31 31 30 20 2a 2f 20 22 49 6e 73 65   /* 110 */ "Inse
be9f0 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31  rt",.     /* 111
bea00 20 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c 0a 20   */ "Destroy",. 
bea10 20 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 52      /* 112 */ "R
bea20 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20  eadCookie",.    
bea30 20 2f 2a 20 31 31 33 20 2a 2f 20 22 4c 6f 61 64   /* 113 */ "Load
bea40 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 20  Analysis",.     
bea50 2f 2a 20 31 31 34 20 2a 2f 20 22 45 78 70 6c 61  /* 114 */ "Expla
bea60 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35  in",.     /* 115
bea70 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64 6f 22   */ "OpenPseudo"
bea80 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f  ,.     /* 116 */
bea90 20 22 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 22   "OpenEphemeral"
beaa0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f  ,.     /* 117 */
beab0 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a   "Null",.     /*
beac0 20 31 31 38 20 2a 2f 20 22 4d 6f 76 65 22 2c 0a   118 */ "Move",.
bead0 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22       /* 119 */ "
beae0 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Blob",.     /* 1
beaf0 32 30 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a  20 */ "Rewind",.
beb00 20 20 20 20 20 2f 2a 20 31 32 31 20 2a 2f 20 22       /* 121 */ "
beb10 53 65 65 6b 47 74 22 2c 0a 20 20 20 20 20 2f 2a  SeekGt",.     /*
beb20 20 31 32 32 20 2a 2f 20 22 56 42 65 67 69 6e 22   122 */ "VBegin"
beb30 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20 2a 2f  ,.     /* 123 */
beb40 20 22 56 55 70 64 61 74 65 22 2c 0a 20 20 20 20   "VUpdate",.    
beb50 20 2f 2a 20 31 32 34 20 2a 2f 20 22 49 66 5a 65   /* 124 */ "IfZe
beb60 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 35  ro",.     /* 125
beb70 20 2a 2f 20 22 56 43 72 65 61 74 65 22 2c 0a 20   */ "VCreate",. 
beb80 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 46      /* 126 */ "F
beb90 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31  ound",.     /* 1
beba0 32 37 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20  27 */ "IfPos",. 
bebb0 20 20 20 20 2f 2a 20 31 32 38 20 2a 2f 20 22 4e      /* 128 */ "N
bebc0 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a  ullRow",.     /*
bebd0 20 31 32 39 20 2a 2f 20 22 52 65 61 6c 22 2c 0a   129 */ "Real",.
bebe0 20 20 20 20 20 2f 2a 20 31 33 30 20 2a 2f 20 22       /* 130 */ "
bebf0 4a 75 6d 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Jump",.     /* 1
bec00 33 31 20 2a 2f 20 22 50 65 72 6d 75 74 61 74 69  31 */ "Permutati
bec10 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32  on",.     /* 132
bec20 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 32   */ "NotUsed_132
bec30 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2a  ",.     /* 133 *
bec40 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 33 22 2c  / "NotUsed_133",
bec50 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20  .     /* 134 */ 
bec60 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a 20  "NotUsed_134",. 
bec70 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e      /* 135 */ "N
bec80 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20  otUsed_135",.   
bec90 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74    /* 136 */ "Not
beca0 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20  Used_136",.     
becb0 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73  /* 137 */ "NotUs
becc0 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a  ed_137",.     /*
becd0 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64   138 */ "NotUsed
bece0 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31  _138",.     /* 1
becf0 33 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31  39 */ "NotUsed_1
bed00 33 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30  39",.     /* 140
bed10 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 30   */ "NotUsed_140
bed20 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 2a  ",.     /* 141 *
bed30 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20  / "ToText",.    
bed40 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c   /* 142 */ "ToBl
bed50 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33  ob",.     /* 143
bed60 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c   */ "ToNumeric",
bed70 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20  .     /* 144 */ 
bed80 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a  "ToInt",.     /*
bed90 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22   145 */ "ToReal"
beda0 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20  ,.  };.  return 
bedb0 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e  azName[i];.}.#en
bedc0 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
bedd0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f  **** End of opco
bede0 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  des.c **********
bedf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bee00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bee10 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
bee20 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
bee30 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_os2.c *******
bee40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bee50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bee60 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
bee70 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  Feb 14.**.** The
bee80 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
bee90 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
beea0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
beeb0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
beec0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
beed0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
beee0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
beef0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
bef00 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
bef10 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
bef20 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
bef30 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
bef40 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
bef50 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
bef60 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
bef70 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
bef80 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
bef90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
befa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
befb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
befc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
befd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
befe0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
beff0 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73  ins code that is
bf000 20 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f   specific to OS/
bf010 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73  2..**.** $Id: os
bf020 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32 30  _os2.c,v 1.63 20
bf030 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32  08/12/10 19:26:2
bf040 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  4 drh Exp $.*/..
bf050 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  .#if SQLITE_OS_O
bf060 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65  S2../*.** A Note
bf070 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c   About Memory Al
bf080 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  location:.**.** 
bf090 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73  This driver uses
bf0a0 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29   malloc()/free()
bf0b0 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72   directly rather
bf0c0 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f   than going thro
bf0d0 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74  ugh.** the SQLit
bf0e0 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74  e-wrappers sqlit
bf0f0 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69  e3_malloc()/sqli
bf100 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f  te3_free().  Tho
bf110 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61  se wrappers.** a
bf120 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20  re designed for 
bf130 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20  use on embedded 
bf140 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65  systems where me
bf150 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61  mory is scarce a
bf160 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69  nd.** malloc fai
bf170 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65  lures happen fre
bf180 71 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64  quently.  OS/2 d
bf190 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c  oes not typicall
bf1a0 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65  y run on.** embe
bf1b0 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e  dded systems, an
bf1c0 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74  d when it does t
bf1d0 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f  he developers no
bf1e0 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67  rmally have bigg
bf1f0 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74  er.** problems t
bf200 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68  o worry about th
bf210 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f  an running out o
bf220 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68  f memory.  So th
bf230 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20  ere is not.** a 
bf240 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20  compelling need 
bf250 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70  to use the wrapp
bf260 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74  ers..**.** But t
bf270 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72  here is a good r
bf280 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65  eason to not use
bf290 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20   the wrappers.  
bf2a0 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a  If we use the.**
bf2b0 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77   wrappers then w
bf2c0 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c  e will get simul
bf2d0 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61  ated malloc() fa
bf2e0 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68  ilures within th
bf2f0 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41  is.** driver.  A
bf300 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61  nd that causes a
bf310 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62  ll kinds of prob
bf320 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73  lems for our tes
bf330 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64  ts.  We.** could
bf340 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20   enhance SQLite 
bf350 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d  to deal with sim
bf360 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61  ulated malloc fa
bf370 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a  ilures within.**
bf380 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20   the OS driver, 
bf390 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20  but the code to 
bf3a0 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20  deal with those 
bf3b0 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f  failure would no
bf3c0 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65  t.** be exercise
bf3d0 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63  d on Linux (whic
bf3e0 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  h does not need 
bf3f0 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74  to malloc() in t
bf400 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e  he driver).** an
bf410 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61  d so we would ha
bf420 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72  ve difficulty wr
bf430 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74  iting coverage t
bf440 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a  ests for that.**
bf450 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74   code.  Better t
bf460 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65  o leave the code
bf470 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a   out, we think..
bf480 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20  **.** The point 
bf490 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69  of this discussi
bf4a0 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  on is as follows
bf4b0 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67  :  When creating
bf4c0 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79   a new.** OS lay
bf4d0 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64  er for an embedd
bf4e0 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f  ed system, if yo
bf4f0 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20  u use this file 
bf500 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a  as an example,.*
bf510 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20  * avoid the use 
bf520 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65  of malloc()/free
bf530 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69  ().  Those routi
bf540 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f  nes work ok on O
bf550 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20  S/2.** desktops 
bf560 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20  but not so well 
bf570 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74  in embedded syst
bf580 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d  ems..*/../*.** M
bf590 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65  acros used to de
bf5a0 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20  termine whether 
bf5b0 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68  or not to use th
bf5c0 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65  reads..*/.#if de
bf5d0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52  fined(SQLITE_THR
bf5e0 45 41 44 53 41 46 45 29 20 26 26 20 53 51 4c 49  EADSAFE) && SQLI
bf5f0 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20  TE_THREADSAFE.# 
bf600 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
bf610 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64  2_THREADS 1.#end
bf620 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64  if../*.** Includ
bf630 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63  e code that is c
bf640 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f  ommon to all os_
bf650 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a  *.c files.*/./**
bf660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
bf670 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  lude os_common.h
bf680 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
bf690 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a  f os_os2.c *****
bf6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
bf6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
bf6c0 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f  in file os_commo
bf6d0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
bf6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
bf700 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a  ** 2004 May 22.*
bf710 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
bf720 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
bf730 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
bf740 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
bf750 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
bf760 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
bf770 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
bf780 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
bf790 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
bf7a0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
bf7b0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
bf7c0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
bf7d0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
bf7e0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
bf7f0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
bf800 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
bf810 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
bf820 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
bf830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf870 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
bf880 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72  le contains macr
bf890 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20  os and a little 
bf8a0 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74  bit of code that
bf8b0 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a   is common to.**
bf8c0 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74   all of the plat
bf8d0 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69  form-specific fi
bf8e0 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64  les (os_*.c) and
bf8f0 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e   is #included in
bf900 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65  to those.** file
bf910 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  s..**.** This fi
bf920 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e  le should be #in
bf930 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73  cluded by the os
bf940 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e  _*.c files only.
bf950 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a    It is not a.**
bf960 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65   general purpose
bf970 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a   header file..**
bf980 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d  .** $Id: os_comm
bf990 6f 6e 2e 68 2c 76 20 31 2e 33 37 20 32 30 30 38  on.h,v 1.37 2008
bf9a0 2f 30 35 2f 32 39 20 32 30 3a 32 32 3a 33 37 20  /05/29 20:22:37 
bf9b0 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23  shane Exp $.*/.#
bf9c0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f  ifndef _OS_COMMO
bf9d0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53  N_H_.#define _OS
bf9e0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a  _COMMON_H_../*.*
bf9f0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62  * At least two b
bfa00 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64  ugs have slipped
bfa10 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63   in because we c
bfa20 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52  hanged the MEMOR
bfa30 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f  Y_DEBUG.** macro
bfa40 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47   to SQLITE_DEBUG
bfa50 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20   and some older 
bfa60 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e  makefiles have n
bfa70 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a  ot yet made the.
bfa80 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20  ** switch.  The 
bfa90 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73  following code s
bfaa0 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73  hould catch this
bfab0 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70   problem at comp
bfac0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66  ile-time..*/.#if
bfad0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  def MEMORY_DEBUG
bfae0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45  .# error "The ME
bfaf0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f  MORY_DEBUG macro
bfb00 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55   is obsolete.  U
bfb10 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  se SQLITE_DEBUG 
bfb20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66  instead.".#endif
bfb30 0a 0a 0a 2f 2a 0a 20 2a 20 57 68 65 6e 20 74 65  .../*. * When te
bfb40 73 74 69 6e 67 2c 20 74 68 69 73 20 67 6c 6f 62  sting, this glob
bfb50 61 6c 20 76 61 72 69 61 62 6c 65 20 73 74 6f 72  al variable stor
bfb60 65 73 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20  es the location 
bfb70 6f 66 20 74 68 65 0a 20 2a 20 70 65 6e 64 69 6e  of the. * pendin
bfb80 67 2d 62 79 74 65 20 69 6e 20 74 68 65 20 64 61  g-byte in the da
bfb90 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 2a 2f  tabase file.. */
bfba0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
bfbb0 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 75  EST.SQLITE_API u
bfbc0 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 71 6c 69  nsigned int sqli
bfbd0 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65  te3_pending_byte
bfbe0 20 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a 23   = 0x40000000;.#
bfbf0 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
bfc00 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54  LITE_DEBUG.SQLIT
bfc10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
bfc20 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30  lite3OSTrace = 0
bfc30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ;.#define OSTRAC
bfc40 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66  E1(X)         if
bfc50 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
bfc60 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
bfc70 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65  rintf(X).#define
bfc80 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20   OSTRACE2(X,Y)  
bfc90 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
bfca0 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
bfcb0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
bfcc0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
bfcd0 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66  E3(X,Y,Z)     if
bfce0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
bfcf0 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
bfd00 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65  rintf(X,Y,Z).#de
bfd10 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c  fine OSTRACE4(X,
bfd20 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c  Y,Z,A)   if( sql
bfd30 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
bfd40 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
bfd50 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e  (X,Y,Z,A).#defin
bfd60 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a  e OSTRACE5(X,Y,Z
bfd70 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65  ,A,B) if( sqlite
bfd80 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
bfd90 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
bfda0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65  Y,Z,A,B).#define
bfdb0 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c   OSTRACE6(X,Y,Z,
bfdc0 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28  A,B,C) \.    if(
bfdd0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20  sqlite3OSTrace) 
bfde0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
bfdf0 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a  tf(X,Y,Z,A,B,C).
bfe00 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37  #define OSTRACE7
bfe10 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20  (X,Y,Z,A,B,C,D) 
bfe20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
bfe30 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  OSTrace) sqlite3
bfe40 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
bfe50 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65  Z,A,B,C,D).#else
bfe60 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
bfe70 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  1(X).#define OST
bfe80 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69  RACE2(X,Y).#defi
bfe90 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
bfea0 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Z).#define OSTRA
bfeb0 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65  CE4(X,Y,Z,A).#de
bfec0 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c  fine OSTRACE5(X,
bfed0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65  Y,Z,A,B).#define
bfee0 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c   OSTRACE6(X,Y,Z,
bfef0 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f  A,B,C).#define O
bff00 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c  STRACE7(X,Y,Z,A,
bff10 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f  B,C,D).#endif../
bff20 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20  *.** Macros for 
bff30 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63  performance trac
bff40 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74  ing.  Normally t
bff50 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79  urned off.  Only
bff60 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38   works.** on i48
bff70 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23  6 hardware..*/.#
bff80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52  ifdef SQLITE_PER
bff90 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a  FORMANCE_TRACE..
bffa0 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20  /* .** hwtime.h 
bffb0 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20  contains inline 
bffc0 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66  assembler code f
bffd0 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  or implementing 
bffe0 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d  .** high-perform
bfff0 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74  ance timing rout
c0000 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  ines..*/./******
c0010 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
c0020 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65   hwtime.h in the
c0030 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f   middle of os_co
c0040 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
c0050 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
c0060 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
c0070 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a  ile hwtime.h ***
c0080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c00a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
c00b0 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a  008 May 27.**.**
c00c0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
c00d0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
c00e0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
c00f0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
c0100 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
c0110 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
c0120 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
c0130 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
c0140 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
c0150 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
c0160 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
c0170 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
c0180 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
c0190 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
c01a0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
c01b0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
c01c0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
c01d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c01e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c01f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
c0220 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
c0230 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61  ontains inline a
c0240 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72  sm code for retr
c0250 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72  ieving "high-per
c0260 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75  formance".** cou
c0270 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c  nters for x86 cl
c0280 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20  ass CPUs..**.** 
c0290 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20  $Id: hwtime.h,v 
c02a0 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31  1.3 2008/08/01 1
c02b0 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78  4:33:15 shane Ex
c02c0 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  p $.*/.#ifndef _
c02d0 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e  HWTIME_H_.#defin
c02e0 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a  e _HWTIME_H_../*
c02f0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
c0300 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77  g routine only w
c0310 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d  orks on pentium-
c0320 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29  class (or newer)
c0330 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20   processors..** 
c0340 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53  It uses the RDTS
c0350 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64  C opcode to read
c0360 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74   the cycle count
c0370 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68   value out of th
c0380 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61  e.** processor a
c0390 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20  nd returns that 
c03a0 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e  value.  This can
c03b0 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67   be used for hig
c03c0 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69  h-res.** profili
c03d0 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69  ng..*/.#if (defi
c03e0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c  ned(__GNUC__) ||
c03f0 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
c0400 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28  R)) && \.      (
c0410 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c  defined(i386) ||
c0420 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f   defined(__i386_
c0430 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
c0440 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64  _IX86))..  #if d
c0450 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
c0460 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
c0470 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
c0480 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
c0490 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  {.     unsigned 
c04a0 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20  int lo, hi;.    
c04b0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
c04c0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
c04d0 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22   "=a" (lo), "=d"
c04e0 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74   (hi));.     ret
c04f0 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74  urn (sqlite_uint
c0500 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f  64)hi << 32 | lo
c0510 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64  ;.  }..  #elif d
c0520 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
c0530 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e  ..  __declspec(n
c0540 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73  aked) __inline s
c0550 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63  qlite_uint64 __c
c0560 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69  decl sqlite3Hwti
c0570 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f  me(void){.     _
c0580 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72  _asm {.        r
c0590 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74  dtsc.        ret
c05a0 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20         ; return 
c05b0 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58  value at EDX:EAX
c05c0 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23  .     }.  }..  #
c05d0 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65  endif..#elif (de
c05e0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
c05f0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36  && defined(__x86
c0600 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  _64__))..  __inl
c0610 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
c0620 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
c0630 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
c0640 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c  nsigned long val
c0650 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
c0660 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72  __volatile__ ("r
c0670 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61  dtsc" : "=A" (va
c0680 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  l));.      retur
c0690 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c  n val;.  }. .#el
c06a0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
c06b0 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64  UC__) && defined
c06c0 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f  (__ppc__))..  __
c06d0 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f  inline__ sqlite_
c06e0 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
c06f0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
c0700 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
c0710 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20  long retval;.   
c0720 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
c0730 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61   junk;.      __a
c0740 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
c0750 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20  _ ("\n\.        
c0760 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20    1:      mftbu 
c0770 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20    %1\n\.        
c0780 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20            mftb  
c0790 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20    %L0\n\.       
c07a0 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75             mftbu
c07b0 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20     %0\n\.       
c07c0 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20             cmpw 
c07d0 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20     %0,%1\n\.    
c07e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e                bn
c07f0 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20  e     1b".      
c0800 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d              : "=
c0810 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72  r" (retval), "=r
c0820 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20  " (junk));.     
c0830 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a   return retval;.
c0840 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65    }..#else..  #e
c0850 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d  rror Need implem
c0860 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69  entation of sqli
c0870 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
c0880 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a  your platform...
c0890 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d    /*.  ** To com
c08a0 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70  pile without imp
c08b0 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65  lementing sqlite
c08c0 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f  3Hwtime() for yo
c08d0 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a  ur platform,.  *
c08e0 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65  * you can remove
c08f0 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f   the above #erro
c0900 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f  r and use the fo
c0910 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75  llowing.  ** stu
c0920 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75  b function.  You
c0930 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e   will lose timin
c0940 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61  g support for ma
c0950 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64  ny.  ** of the d
c0960 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73  ebugging and tes
c0970 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20  ting utilities, 
c0980 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74  but it should at
c0990 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70  .  ** least comp
c09a0 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a  ile and run..  *
c09b0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
c09c0 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34     sqlite_uint64
c09d0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
c09e0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73  oid){ return ((s
c09f0 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b  qlite_uint64)0);
c0a00 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64   }..#endif..#end
c0a10 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
c0a20 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f  HWTIME_H_) */../
c0a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
c0a40 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a  nd of hwtime.h *
c0a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
c0a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
c0a90 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
c0aa0 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f  we left off in o
c0ab0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
c0ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
c0ad0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69  static sqlite_ui
c0ae0 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74  nt64 g_start;.st
c0af0 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74  atic sqlite_uint
c0b00 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64  64 g_elapsed;.#d
c0b10 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52  efine TIMER_STAR
c0b20 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d  T       g_start=
c0b30 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a  sqlite3Hwtime().
c0b40 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e  #define TIMER_EN
c0b50 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70  D         g_elap
c0b60 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d  sed=sqlite3Hwtim
c0b70 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66  e()-g_start.#def
c0b80 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
c0b90 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a  D     g_elapsed.
c0ba0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49  #else.#define TI
c0bb0 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e  MER_START.#defin
c0bc0 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66  e TIMER_END.#def
c0bd0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
c0be0 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75  D     ((sqlite_u
c0bf0 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a  int64)0).#endif.
c0c00 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d  ./*.** If we com
c0c10 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51  pile with the SQ
c0c20 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20  LITE_TEST macro 
c0c30 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f  set, then the fo
c0c40 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a  llowing block.**
c0c50 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69   of code will gi
c0c60 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74  ve us the abilit
c0c70 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20  y to simulate a 
c0c80 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20  disk I/O error. 
c0c90 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64   This.** is used
c0ca0 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65   for testing the
c0cb0 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f   I/O recovery lo
c0cc0 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  gic..*/.#ifdef S
c0cd0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
c0ce0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c0cf0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d  3_io_error_hit =
c0d00 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
c0d10 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
c0d20 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a  f I/O Errors */.
c0d30 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
c0d40 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c0d50 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20  hardhit = 0;    
c0d60 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
c0d70 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f   non-benign erro
c0d80 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  rs */.SQLITE_API
c0d90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
c0da0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20  error_pending = 
c0db0 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75  0;        /* Cou
c0dc0 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74  nt down to first
c0dd0 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51   I/O error */.SQ
c0de0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
c0df0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
c0e00 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20  rsist = 0;      
c0e10 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f    /* True if I/O
c0e20 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20   errors persist 
c0e30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
c0e40 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
c0e50 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20  or_benign = 0;  
c0e60 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
c0e70 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e  f errors are ben
c0e80 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ign */.SQLITE_AP
c0e90 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
c0ea0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d  skfull_pending =
c0eb0 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
c0ec0 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
c0ed0 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  ull = 0;.#define
c0ee0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
c0ef0 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65  Benign(X) sqlite
c0f00 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67  3_io_error_benig
c0f10 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69  n=(X).#define Si
c0f20 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f  mulateIOError(CO
c0f30 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71  DE)  \.  if( (sq
c0f40 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
c0f50 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65  ersist && sqlite
c0f60 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20  3_io_error_hit) 
c0f70 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69  \.       || sqli
c0f80 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
c0f90 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c  ding-- == 1 )  \
c0fa0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  .              {
c0fb0 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20   local_ioerr(); 
c0fc0 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76  CODE; }.static v
c0fd0 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28  oid local_ioerr(
c0fe0 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49  ){.  IOTRACE(("I
c0ff0 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c  OERR\n"));.  sql
c1000 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
c1010 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  t++;.  if( !sqli
c1020 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
c1030 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f  ign ) sqlite3_io
c1040 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b  _error_hardhit++
c1050 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  ;.}.#define Simu
c1060 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
c1070 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28  r(CODE) \.   if(
c1080 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
c1090 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20  l_pending ){ \. 
c10a0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
c10b0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
c10c0 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20   == 1 ){ \.     
c10d0 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b    local_ioerr();
c10e0 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65   \.       sqlite
c10f0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20  3_diskfull = 1; 
c1100 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
c1110 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20  _io_error_hit = 
c1120 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45  1; \.       CODE
c1130 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20  ; \.     }else{ 
c1140 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
c1150 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
c1160 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a  g--; \.     } \.
c1170 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69     }.#else.#defi
c1180 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
c1190 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66  orBenign(X).#def
c11a0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
c11b0 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53  ror(A).#define S
c11c0 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45  imulateDiskfullE
c11d0 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a  rror(A).#endif..
c11e0 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69  /*.** When testi
c11f0 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74  ng, keep a count
c1200 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
c1210 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f  f open files..*/
c1220 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
c1230 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
c1240 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  nt sqlite3_open_
c1250 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  file_count = 0;.
c1260 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e  #define OpenCoun
c1270 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f  ter(X)  sqlite3_
c1280 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b  open_file_count+
c1290 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  =(X).#else.#defi
c12a0 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
c12b0 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ).#endif..#endif
c12c0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53   /* !defined(_OS
c12d0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a  _COMMON_H_) */..
c12e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c12f0 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e  End of os_common
c1300 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
c1310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c1330 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c1340 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
c1350 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
c1360 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_os2.c *******
c1370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c1380 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46 69  ./*.** The os2Fi
c1390 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  le structure is 
c13a0 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69  subclass of sqli
c13b0 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69  te3_file specifi
c13c0 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a 2a  c for the OS/2.*
c13d0 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c 61  * protability la
c13e0 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  yer..*/.typedef 
c13f0 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 6f  struct os2File o
c1400 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 6f  s2File;.struct o
c1410 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74  s2File {.  const
c1420 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
c1430 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f  ods *pMethod;  /
c1440 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69 72  * Always the fir
c1450 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48 46  st entry */.  HF
c1460 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 20  ILE h;          
c1470 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
c1480 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  e for accessing 
c1490 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68  the file */.  ch
c14a0 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20 20  ar* pathToDel;  
c14b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
c14c0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74  of file to delet
c14d0 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c 4c  e on close, NULL
c14e0 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e 73   if not */.  uns
c14f0 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74  igned char lockt
c1500 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f  ype;   /* Type o
c1510 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79  f lock currently
c1520 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69   held on this fi
c1530 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e  le */.};..#defin
c1540 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 31  e LOCK_TIMEOUT 1
c1550 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 75 6c  0L /* the defaul
c1560 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f 75  t locking timeou
c1570 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  t */../*********
c1580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c15c0 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ****.** The next
c15d0 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e   group of routin
c15e0 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  es implement the
c15f0 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65   I/O methods spe
c1600 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65  cified.** by the
c1610 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
c1620 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a  ods object..****
c1630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
c1680 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e  ** Close a file.
c1690 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c16a0 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 65 33  s2Close( sqlite3
c16b0 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 41  _file *id ){.  A
c16c0 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52  PIRET rc = NO_ER
c16d0 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  ROR;.  os2File *
c16e0 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 64 20  pFile;.  if( id 
c16f0 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f 73 32  && (pFile = (os2
c1700 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 20 29  File*)id) != 0 )
c1710 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 20  {.    OSTRACE2( 
c1720 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46  "CLOSE %d\n", pF
c1730 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72 63  ile->h );.    rc
c1740 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 46 69   = DosClose( pFi
c1750 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46 69  le->h );.    pFi
c1760 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e  le->locktype = N
c1770 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28 20  O_LOCK;.    if( 
c1780 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
c1790 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20   != NULL ){.    
c17a0 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 65 44    rc = DosForceD
c17b0 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69 6c  elete( (PSZ)pFil
c17c0 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a  e->pathToDel );.
c17d0 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69 6c        free( pFil
c17e0 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a  e->pathToDel );.
c17f0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74        pFile->pat
c1800 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20  hToDel = NULL;. 
c1810 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 30 3b     }.    id = 0;
c1820 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72  .    OpenCounter
c1830 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 72  ( -1 );.  }..  r
c1840 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45  eturn rc == NO_E
c1850 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  RROR ? SQLITE_OK
c1860 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b   : SQLITE_IOERR;
c1870 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64  .}../*.** Read d
c1880 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20  ata from a file 
c1890 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20  into a buffer.  
c18a0 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
c18b0 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73   if all.** bytes
c18c0 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65   were read succe
c18d0 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49  ssfully and SQLI
c18e0 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74  TE_IOERR if anyt
c18f0 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f  hing goes.** wro
c1900 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
c1910 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 71 6c  t os2Read(.  sql
c1920 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20  ite3_file *id,  
c1930 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c1940 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f  File to read fro
c1950 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75  m */.  void *pBu
c1960 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f,              
c1970 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
c1980 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69  content into thi
c1990 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e  s buffer */.  in
c19a0 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20  t amt,          
c19b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c19c0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
c19d0 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71   to read */.  sq
c19e0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
c19f0 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  et            /*
c1a00 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
c1a10 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
c1a20 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65  .){.  ULONG file
c1a30 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20  Location = 0L;. 
c1a40 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f 73   ULONG got;.  os
c1a50 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
c1a60 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61  os2File*)id;.  a
c1a70 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
c1a80 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
c1a90 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
c1aa0 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a 20  _IOERR_READ );. 
c1ab0 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 41 44   OSTRACE3( "READ
c1ac0 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20   %d lock=%d\n", 
c1ad0 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d  pFile->h, pFile-
c1ae0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69  >locktype );.  i
c1af0 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72  f( DosSetFilePtr
c1b00 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65  (pFile->h, offse
c1b10 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26  t, FILE_BEGIN, &
c1b20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d  fileLocation) !=
c1b30 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
c1b40 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
c1b50 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OERR;.  }.  if( 
c1b60 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d 3e  DosRead( pFile->
c1b70 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67  h, pBuf, amt, &g
c1b80 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ot ) != NO_ERROR
c1b90 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
c1ba0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
c1bb0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 20  ;.  }.  if( got 
c1bc0 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29 0a  == (ULONG)amt ).
c1bd0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
c1be0 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20  E_OK;.  else {. 
c1bf0 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f 72     /* Unread por
c1c00 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e 70  tions of the inp
c1c10 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 20 62  ut buffer must b
c1c20 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f  e zero-filled */
c1c30 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63  .    memset(&((c
c1c40 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c  har*)pBuf)[got],
c1c50 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20   0, amt-got);.  
c1c60 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c1c70 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
c1c80 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  ;.  }.}../*.** W
c1c90 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61  rite data from a
c1ca0 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66   buffer into a f
c1cb0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  ile.  Return SQL
c1cc0 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
c1cd0 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68  s.** or some oth
c1ce0 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e  er error code on
c1cf0 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61   failure..*/.sta
c1d00 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 74 65  tic int os2Write
c1d10 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
c1d20 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20   *id,           
c1d30 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77      /* File to w
c1d40 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63  rite into */.  c
c1d50 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c  onst void *pBuf,
c1d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c1d70 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62  * The bytes to b
c1d80 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69  e written */.  i
c1d90 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
c1da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c1db0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
c1dc0 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20  s to write */.  
c1dd0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66  sqlite3_int64 of
c1de0 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20  fset            
c1df0 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74  /* Offset into t
c1e00 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e  he file to begin
c1e10 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29   writing at */.)
c1e20 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f  {.  ULONG fileLo
c1e30 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 41  cation = 0L;.  A
c1e40 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52  PIRET rc = NO_ER
c1e50 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 6f  ROR;.  ULONG wro
c1e60 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70  te;.  os2File *p
c1e70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
c1e80 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69  )id;.  assert( i
c1e90 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61  d!=0 );.  Simula
c1ea0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
c1eb0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  n SQLITE_IOERR_W
c1ec0 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c 61  RITE );.  Simula
c1ed0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
c1ee0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46   return SQLITE_F
c1ef0 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  ULL );.  OSTRACE
c1f00 33 28 20 22 57 52 49 54 45 20 25 64 20 6c 6f 63  3( "WRITE %d loc
c1f10 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  k=%d\n", pFile->
c1f20 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  h, pFile->lockty
c1f30 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53  pe );.  if( DosS
c1f40 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d  etFilePtr(pFile-
c1f50 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45  >h, offset, FILE
c1f60 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63  _BEGIN, &fileLoc
c1f70 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52  ation) != NO_ERR
c1f80 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
c1f90 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20   SQLITE_IOERR;. 
c1fa0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74   }.  assert( amt
c1fb0 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 61  >0 );.  while( a
c1fc0 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 20 20  mt > 0 &&.      
c1fd0 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 72 69     ( rc = DosWri
c1fe0 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28 50  te( pFile->h, (P
c1ff0 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 2c 20  VOID)pBuf, amt, 
c2000 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e 4f  &wrote ) ) == NO
c2010 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 20 20  _ERROR &&.      
c2020 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 20 29     wrote > 0.  )
c2030 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f  {.    amt -= wro
c2040 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26  te;.    pBuf = &
c2050 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72  ((char*)pBuf)[wr
c2060 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  ote];.  }..  ret
c2070 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f 45  urn ( rc != NO_E
c2080 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28 69  RROR || amt > (i
c2090 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 51 4c  nt)wrote ) ? SQL
c20a0 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 54  ITE_FULL : SQLIT
c20b0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
c20c0 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20  runcate an open 
c20d0 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66  file to a specif
c20e0 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74  ied size.*/.stat
c20f0 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e 63 61  ic int os2Trunca
c2100 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  te( sqlite3_file
c2110 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 20   *id, i64 nByte 
c2120 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  ){.  APIRET rc =
c2130 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32   NO_ERROR;.  os2
c2140 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f  File *pFile = (o
c2150 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53  s2File*)id;.  OS
c2160 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 41 54  TRACE3( "TRUNCAT
c2170 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46  E %d %lld\n", pF
c2180 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b  ile->h, nByte );
c2190 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
c21a0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
c21b0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45  E_IOERR_TRUNCATE
c21c0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 65   );.  rc = DosSe
c21d0 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 6c 65  tFileSize( pFile
c21e0 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20  ->h, nByte );.  
c21f0 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f  return rc == NO_
c2200 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f  ERROR ? SQLITE_O
c2210 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  K : SQLITE_IOERR
c2220 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69  _TRUNCATE;.}..#i
c2230 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
c2240 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65  ./*.** Count the
c2250 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73   number of fulls
c2260 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20  yncs and normal 
c2270 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20  syncs.  This is 
c2280 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20  used to test.** 
c2290 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66  that syncs and f
c22a0 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63  ullsyncs are occ
c22b0 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67  uring at the rig
c22c0 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c  ht times..*/.SQL
c22d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c22e0 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d  te3_sync_count =
c22f0 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
c2300 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73  nt sqlite3_fulls
c2310 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  ync_count = 0;.#
c2320 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  endif../*.** Mak
c2330 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65  e sure all write
c2340 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61  s to a particula
c2350 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69  r file are commi
c2360 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f  tted to disk..*/
c2370 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53  .static int os2S
c2380 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 69 6c  ync( sqlite3_fil
c2390 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73  e *id, int flags
c23a0 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70   ){.  os2File *p
c23b0 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
c23c0 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  )id;.  OSTRACE3(
c23d0 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25   "SYNC %d lock=%
c23e0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
c23f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
c2400 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
c2410 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67  _TEST.  if( flag
c2420 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  s & SQLITE_SYNC_
c2430 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69 74  FULL){.    sqlit
c2440 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e  e3_fullsync_coun
c2450 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  t++;.  }.  sqlit
c2460 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b  e3_sync_count++;
c2470 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20  .#endif.  /* If 
c2480 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  we compiled with
c2490 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53   the SQLITE_NO_S
c24a0 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73  YNC flag, then s
c24b0 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a  yncing is a.  **
c24c0 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64   no-op.  */.#ifd
c24d0 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  ef SQLITE_NO_SYN
c24e0 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  C.  UNUSED_PARAM
c24f0 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 20 72  ETER(pFile);.  r
c2500 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
c2510 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20  .#else.  return 
c2520 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28 20  DosResetBuffer( 
c2530 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f  pFile->h ) == NO
c2540 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
c2550 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
c2560 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  R;.#endif.}../*.
c2570 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  ** Determine the
c2580 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66   current size of
c2590 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73   a file in bytes
c25a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c25b0 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 6c 69  s2FileSize( sqli
c25c0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71  te3_file *id, sq
c25d0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69  lite3_int64 *pSi
c25e0 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72  ze ){.  APIRET r
c25f0 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
c2600 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 73  FILESTATUS3 fsts
c2610 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d  3FileInfo;.  mem
c2620 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 49 6e  set(&fsts3FileIn
c2630 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73  fo, 0, sizeof(fs
c2640 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20  ts3FileInfo));. 
c2650 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
c2660 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
c2670 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
c2680 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 29  TE_IOERR_FSTAT )
c2690 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72  ;.  rc = DosQuer
c26a0 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32  yFileInfo( ((os2
c26b0 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49  File*)id)->h, FI
c26c0 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74  L_STANDARD, &fst
c26d0 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65  s3FileInfo, size
c26e0 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20  of(FILESTATUS3) 
c26f0 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 4e  );.  if( rc == N
c2700 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a  O_ERROR ){.    *
c2710 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 69 6c  pSize = fsts3Fil
c2720 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20  eInfo.cbFile;.  
c2730 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c2740 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
c2750 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
c2760 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a  OERR_FSTAT;.  }.
c2770 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65  }../*.** Acquire
c2780 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a   a reader lock..
c2790 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
c27a0 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69  tReadLock( os2Fi
c27b0 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46  le *pFile ){.  F
c27c0 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65  ILELOCK  LockAre
c27d0 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55  a,.            U
c27e0 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49  nlockArea;.  API
c27f0 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65  RET res;.  memse
c2800 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  t(&LockArea, 0, 
c2810 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29  sizeof(LockArea)
c2820 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c  );.  memset(&Unl
c2830 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65  ockArea, 0, size
c2840 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b  of(UnlockArea));
c2850 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66  .  LockArea.lOff
c2860 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52  set = SHARED_FIR
c2870 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ST;.  LockArea.l
c2880 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53  Range = SHARED_S
c2890 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65  IZE;.  UnlockAre
c28a0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
c28b0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
c28c0 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20  nge = 0L;.  res 
c28d0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
c28e0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
c28f0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
c2900 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
c2910 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41  T, 1L );.  OSTRA
c2920 43 45 33 28 20 22 47 45 54 52 45 41 44 4c 4f 43  CE3( "GETREADLOC
c2930 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20  K %d res=%d\n", 
c2940 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
c2950 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d  .  return res;.}
c2960 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72  ../*.** Undo a r
c2970 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69  eadlock.*/.stati
c2980 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64  c int unlockRead
c2990 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69  Lock( os2File *i
c29a0 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20  d ){.  FILELOCK 
c29b0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20   LockArea,.     
c29c0 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
c29d0 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b  a;.  APIRET res;
c29e0 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41  .  memset(&LockA
c29f0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c  rea, 0, sizeof(L
c2a00 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d  ockArea));.  mem
c2a10 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  set(&UnlockArea,
c2a20 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63   0, sizeof(Unloc
c2a30 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41  kArea));.  LockA
c2a40 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
c2a50 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61  ;.  LockArea.lRa
c2a60 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f  nge = 0L;.  Unlo
c2a70 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c2a80 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20   SHARED_FIRST;. 
c2a90 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
c2aa0 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  ge = SHARED_SIZE
c2ab0 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74  ;.  res = DosSet
c2ac0 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68  FileLocks( id->h
c2ad0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
c2ae0 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
c2af0 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20  IMEOUT, 1L );.  
c2b00 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43  OSTRACE3( "UNLOC
c2b10 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20  K-READLOCK file 
c2b20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 64  handle=%d res=%d
c2b30 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73  ?\n", id->h, res
c2b40 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73   );.  return res
c2b50 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20  ;.}../*.** Lock 
c2b60 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68  the file with th
c2b70 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64  e lock specified
c2b80 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f   by parameter lo
c2b90 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20  cktype - one.** 
c2ba0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
c2bb0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20  :.**.**     (1) 
c2bc0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SHARED_LOCK.**  
c2bd0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f     (2) RESERVED_
c2be0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20  LOCK.**     (3) 
c2bf0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20  PENDING_LOCK.** 
c2c00 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56      (4) EXCLUSIV
c2c10 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d  E_LOCK.**.** Som
c2c20 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75  etimes when requ
c2c30 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20  esting one lock 
c2c40 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61  state, additiona
c2c50 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a  l lock states.**
c2c60 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e   are inserted in
c2c70 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c   between.  The l
c2c80 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69  ocking might fai
c2c90 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  l on one of the 
c2ca0 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74  later.** transit
c2cb0 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65  ions leaving the
c2cc0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66   lock state diff
c2cd0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20  erent from what 
c2ce0 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a  it started but.*
c2cf0 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66  * still short of
c2d00 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20   its goal.  The 
c2d10 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20  following chart 
c2d20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65  shows the allowe
c2d30 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73  d.** transitions
c2d40 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65   and the inserte
c2d50 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73  d intermediate s
c2d60 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  tates:.**.**    
c2d70 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52  UNLOCKED -> SHAR
c2d80 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
c2d90 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20  -> RESERVED.**  
c2da0 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e    SHARED -> (PEN
c2db0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
c2dc0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45  VE.**    RESERVE
c2dd0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
c2de0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  > EXCLUSIVE.**  
c2df0 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43    PENDING -> EXC
c2e00 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69  LUSIVE.**.** Thi
c2e10 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f  s routine will o
c2e20 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c  nly increase a l
c2e30 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c  ock.  The os2Unl
c2e40 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a  ock() routine.**
c2e50 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b   erases all lock
c2e60 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65  s at once and re
c2e70 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61  turns us immedia
c2e80 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20  tely to locking 
c2e90 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69  level 0..** It i
c2ea0 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  s not possible t
c2eb0 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b  o lower the lock
c2ec0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74  ing level one st
c2ed0 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59  ep at a time.  Y
c2ee0 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74  ou.** must go st
c2ef0 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e  raight to lockin
c2f00 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74  g level 0..*/.st
c2f10 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b  atic int os2Lock
c2f20 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ( sqlite3_file *
c2f30 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
c2f40 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   ){.  int rc = S
c2f50 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
c2f60 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66  /* Return code f
c2f70 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20  rom subroutines 
c2f80 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 73 20  */.  APIRET res 
c2f90 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f  = NO_ERROR;    /
c2fa0 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f  * Result of an O
c2fb0 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f  S/2 lock call */
c2fc0 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79  .  int newLockty
c2fd0 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74  pe;       /* Set
c2fe0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c2ff0 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62   to this value b
c3000 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f  efore exiting */
c3010 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e  .  int gotPendin
c3020 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75  gLock = 0;/* Tru
c3030 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65 64  e if we acquired
c3040 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   a PENDING lock 
c3050 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46  this time */.  F
c3060 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65  ILELOCK  LockAre
c3070 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55  a,.            U
c3080 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32  nlockArea;.  os2
c3090 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f  File *pFile = (o
c30a0 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65  s2File*)id;.  me
c30b0 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20  mset(&LockArea, 
c30c0 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72  0, sizeof(LockAr
c30d0 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26  ea));.  memset(&
c30e0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73  UnlockArea, 0, s
c30f0 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61  izeof(UnlockArea
c3100 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46  ));.  assert( pF
c3110 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52  ile!=0 );.  OSTR
c3120 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 25  ACE4( "LOCK %d %
c3130 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69  d was %d\n", pFi
c3140 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
c3150 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c3160 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
c3170 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
c3180 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70  lock of this typ
c3190 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69  e or more restri
c31a0 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a  ctive on the.  *
c31b0 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f  * os2File, do no
c31c0 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65  thing. Don't use
c31d0 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65   the end_lock: e
c31e0 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a  xit path, as.  *
c31f0 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  * sqlite3_mutex_
c3200 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 20 62  enter() hasn't b
c3210 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a  een called yet..
c3220 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65    */.  if( pFile
c3230 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b  ->locktype>=lock
c3240 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52  type ){.    OSTR
c3250 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 25  ACE3( "LOCK %d %
c3260 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65  d ok (already he
c3270 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  ld)\n", pFile->h
c3280 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  , locktype );.  
c3290 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c32a0 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  OK;.  }..  /* Ma
c32b0 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b  ke sure the lock
c32c0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20  ing sequence is 
c32d0 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61  correct.  */.  a
c32e0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
c32f0 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20  cktype!=NO_LOCK 
c3300 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  || locktype==SHA
c3310 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  RED_LOCK );.  as
c3320 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d  sert( locktype!=
c3330 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a  PENDING_LOCK );.
c3340 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
c3350 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe!=RESERVED_LOC
c3360 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  K || pFile->lock
c3370 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
c3380 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20  K );..  /* Lock 
c3390 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  the PENDING_LOCK
c33a0 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64   byte if we need
c33b0 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45   to acquire a PE
c33c0 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20  NDING lock or.  
c33d0 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  ** a SHARED lock
c33e0 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71  .  If we are acq
c33f0 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20  uiring a SHARED 
c3400 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73  lock, the acquis
c3410 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68  ition of.  ** th
c3420 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62  e PENDING_LOCK b
c3430 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79  yte is temporary
c3440 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b  ..  */.  newLock
c3450 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f  type = pFile->lo
c3460 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46  cktype;.  if( pF
c3470 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e  ile->locktype==N
c3480 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20  O_LOCK.      || 
c3490 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55  (locktype==EXCLU
c34a0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69  SIVE_LOCK && pFi
c34b0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45  le->locktype==RE
c34c0 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29  SERVED_LOCK).  )
c34d0 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  {.    LockArea.l
c34e0 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47  Offset = PENDING
c34f0 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41  _BYTE;.    LockA
c3500 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b  rea.lRange = 1L;
c3510 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  .    UnlockArea.
c3520 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20  lOffset = 0L;.  
c3530 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
c3540 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f  nge = 0L;..    /
c3550 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 68  * wait longer th
c3560 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20  an LOCK_TIMEOUT 
c3570 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 65  here not to have
c3580 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c 65   to try multiple
c3590 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65   times */.    re
c35a0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  s = DosSetFileLo
c35b0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
c35c0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
c35d0 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20  kArea, 100L, 0L 
c35e0 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d  );.    if( res =
c35f0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c3600 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f      gotPendingLo
c3610 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53  ck = 1;.      OS
c3620 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64  TRACE3( "LOCK %d
c3630 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f   pending lock bo
c3640 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 73 3d  olean set.  res=
c3650 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
c3660 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20   res );.    }.  
c3670 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
c3680 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20  a shared lock.  
c3690 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
c36a0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
c36b0 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f  & res == NO_ERRO
c36c0 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  R ){.    assert(
c36d0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c36e0 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20  ==NO_LOCK );.   
c36f0 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f   res = getReadLo
c3700 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69  ck(pFile);.    i
c3710 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52  f( res == NO_ERR
c3720 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c  OR ){.      newL
c3730 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44  ocktype = SHARED
c3740 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  _LOCK;.    }.   
c3750 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b   OSTRACE3( "LOCK
c3760 20 25 64 20 61 63 71 75 69 72 65 20 73 68 61 72   %d acquire shar
c3770 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c  ed lock. res=%d\
c3780 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
c3790 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  s );.  }..  /* A
c37a0 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45  cquire a RESERVE
c37b0 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  D lock.  */.  if
c37c0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45  ( locktype==RESE
c37d0 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73  RVED_LOCK && res
c37e0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
c37f0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
c3800 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  e->locktype==SHA
c3810 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
c3820 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
c3830 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45   = RESERVED_BYTE
c3840 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
c3850 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20  Range = 1L;.    
c3860 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
c3870 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c  et = 0L;.    Unl
c3880 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c3890 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44   0L;.    res = D
c38a0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
c38b0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
c38c0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
c38d0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
c38e0 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65  0L );.    if( re
c38f0 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
c3900 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
c3910 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f  pe = RESERVED_LO
c3920 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53  CK;.    }.    OS
c3930 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64  TRACE3( "LOCK %d
c3940 20 61 63 71 75 69 72 65 20 72 65 73 65 72 76 65   acquire reserve
c3950 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e  d lock. res=%d\n
c3960 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73  ", pFile->h, res
c3970 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63   );.  }..  /* Ac
c3980 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20  quire a PENDING 
c3990 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
c39a0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
c39b0 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  IVE_LOCK && res 
c39c0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
c39d0 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d     newLocktype =
c39e0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20   PENDING_LOCK;. 
c39f0 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63     gotPendingLoc
c3a00 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 41  k = 0;.    OSTRA
c3a10 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 63  CE2( "LOCK %d ac
c3a20 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f  quire pending lo
c3a30 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b  ck. pending lock
c3a40 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c   boolean unset.\
c3a50 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a  n", pFile->h );.
c3a60 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
c3a70 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  e an EXCLUSIVE l
c3a80 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ock.  */.  if( l
c3a90 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
c3aa0 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d  VE_LOCK && res =
c3ab0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c3ac0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
c3ad0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45  >locktype>=SHARE
c3ae0 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65  D_LOCK );.    re
c3af0 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  s = unlockReadLo
c3b00 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f  ck(pFile);.    O
c3b10 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 61 64  STRACE2( "unread
c3b20 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65  lock = %d\n", re
c3b30 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  s );.    LockAre
c3b40 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52  a.lOffset = SHAR
c3b50 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f  ED_FIRST;.    Lo
c3b60 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c3b70 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20  SHARED_SIZE;.   
c3b80 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66   UnlockArea.lOff
c3b90 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  set = 0L;.    Un
c3ba0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c3bb0 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20  = 0L;.    res = 
c3bc0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c3bd0 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c3be0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c3bf0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c3c00 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72   0L );.    if( r
c3c10 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
c3c20 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74  {.      newLockt
c3c30 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f  ype = EXCLUSIVE_
c3c40 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LOCK;.    }else{
c3c50 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28  .      OSTRACE2(
c3c60 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64   "OS/2 error-cod
c3c70 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29  e = %d\n", res )
c3c80 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 4c  ;.      getReadL
c3c90 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
c3ca0 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20  }.    OSTRACE3( 
c3cb0 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65  "LOCK %d acquire
c3cc0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e   exclusive lock.
c3cd0 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69    res=%d\n", pFi
c3ce0 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20  le->h, res );.  
c3cf0 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72  }..  /* If we ar
c3d00 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44  e holding a PEND
c3d10 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75  ING lock that ou
c3d20 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73  ght to be releas
c3d30 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65  ed, then.  ** re
c3d40 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20  lease it now..  
c3d50 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64  */.  if( gotPend
c3d60 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74  ingLock && lockt
c3d70 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
c3d80 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20   ){.    int r;. 
c3d90 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
c3da0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f  set = 0L;.    Lo
c3db0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c3dc0 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  0L;.    UnlockAr
c3dd0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e  ea.lOffset = PEN
c3de0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55  DING_BYTE;.    U
c3df0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c3e00 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44   = 1L;.    r = D
c3e10 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
c3e20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
c3e30 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
c3e40 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
c3e50 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  0L );.    OSTRAC
c3e60 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c  E3( "LOCK %d unl
c3e70 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69  ocking pending/i
c3e80 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e  s shared. r=%d\n
c3e90 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29  ", pFile->h, r )
c3ea0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61  ;.  }..  /* Upda
c3eb0 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  te the state of 
c3ec0 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c  the lock has hel
c3ed0 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65  d in the file de
c3ee0 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20  scriptor then.  
c3ef0 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70  ** return the ap
c3f00 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74  propriate result
c3f10 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   code..  */.  if
c3f20 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f  ( res == NO_ERRO
c3f30 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  R ){.    rc = SQ
c3f40 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
c3f50 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 20  {.    OSTRACE4( 
c3f60 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20  "LOCK FAILED %d 
c3f70 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75  trying for %d bu
c3f80 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69  t got %d\n", pFi
c3f90 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
c3fa0 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e       locktype, n
c3fb0 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ewLocktype );.  
c3fc0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
c3fd0 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d  SY;.  }.  pFile-
c3fe0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c  >locktype = newL
c3ff0 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41  ocktype;.  OSTRA
c4000 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f  CE3( "LOCK %d no
c4010 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  w %d\n", pFile->
c4020 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  h, pFile->lockty
c4030 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pe );.  return r
c4040 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
c4050 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
c4060 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45  if there is a RE
c4070 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64  SERVED lock held
c4080 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65   on the specifie
c4090 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69  d.** file by thi
c40a0 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  s or any other p
c40b0 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20  rocess. If such 
c40c0 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20  a lock is held, 
c40d0 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65  return.** non-ze
c40e0 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65  ro, otherwise ze
c40f0 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ro..*/.static in
c4100 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76  t os2CheckReserv
c4110 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f  edLock( sqlite3_
c4120 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
c4130 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d  Out ){.  int r =
c4140 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70   0;.  os2File *p
c4150 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
c4160 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 70  )id;.  assert( p
c4170 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28  File!=0 );.  if(
c4180 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c4190 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  >=RESERVED_LOCK 
c41a0 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 20  ){.    r = 1;.  
c41b0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53    OSTRACE3( "TES
c41c0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
c41d0 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c  (local)\n", pFil
c41e0 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c  e->h, r );.  }el
c41f0 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b  se{.    FILELOCK
c4200 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20    LockArea,.    
c4210 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b            Unlock
c4220 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 45 54  Area;.    APIRET
c4230 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
c4240 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b      memset(&Lock
c4250 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
c4260 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20  LockArea));.    
c4270 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72  memset(&UnlockAr
c4280 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e  ea, 0, sizeof(Un
c4290 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20  lockArea));.    
c42a0 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
c42b0 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45   = RESERVED_BYTE
c42c0 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
c42d0 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20  Range = 1L;.    
c42e0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
c42f0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c  et = 0L;.    Unl
c4300 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c4310 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f   0L;.    rc = Do
c4320 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
c4330 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
c4340 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
c4350 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
c4360 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  L );.    OSTRACE
c4370 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  3( "TEST WR-LOCK
c4380 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 65   %d lock reserve
c4390 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c  d byte rc=%d\n",
c43a0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b   pFile->h, rc );
c43b0 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e  .    if( rc == N
c43c0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
c43d0 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 4e 4f   APIRET rcu = NO
c43e0 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72  _ERROR; /* retur
c43f0 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63  n code for unloc
c4400 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f  king */.      Lo
c4410 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c4420 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41   0L;.      LockA
c4430 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b  rea.lRange = 0L;
c4440 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  .      UnlockAre
c4450 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45  a.lOffset = RESE
c4460 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20  RVED_BYTE;.     
c4470 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
c4480 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72  ge = 1L;.      r
c4490 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c  cu = DosSetFileL
c44a0 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
c44b0 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
c44c0 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d  ckArea, LOCK_TIM
c44d0 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20  EOUT, 0L );.    
c44e0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53    OSTRACE3( "TES
c44f0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c  T WR-LOCK %d unl
c4500 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79 74  ock reserved byt
c4510 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  e r=%d\n", pFile
c4520 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 20  ->h, rcu );.    
c4530 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 20 3d  }.    r = !(rc =
c4540 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20  = NO_ERROR);.   
c4550 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54   OSTRACE3( "TEST
c4560 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28   WR-LOCK %d %d (
c4570 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c  remote)\n", pFil
c4580 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20  e->h, r );.  }. 
c4590 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65   *pOut = r;.  re
c45a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c45b0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74  }../*.** Lower t
c45c0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
c45d0 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70   on file descrip
c45e0 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79  tor id to lockty
c45f0 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a  pe.  locktype.**
c4600 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
c4610 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45  NO_LOCK or SHARE
c4620 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  D_LOCK..**.** If
c4630 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
c4640 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
c4650 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
c4660 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
c4670 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65  .** the requeste
c4680 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c  d locking level,
c4690 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
c46a0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
c46b0 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  It is not possib
c46c0 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74  le for this rout
c46d0 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74  ine to fail if t
c46e0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
c46f0 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b  nt.** is NO_LOCK
c4700 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64  .  If the second
c4710 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41   argument is SHA
c4720 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68  RED_LOCK then th
c4730 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69  is routine.** mi
c4740 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ght return SQLIT
c4750 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74  E_IOERR;.*/.stat
c4760 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b  ic int os2Unlock
c4770 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ( sqlite3_file *
c4780 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
c4790 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a   ){.  int type;.
c47a0 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65    os2File *pFile
c47b0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b   = (os2File*)id;
c47c0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 53  .  APIRET rc = S
c47d0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52  QLITE_OK;.  APIR
c47e0 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f  ET res = NO_ERRO
c47f0 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c  R;.  FILELOCK  L
c4800 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20  ockArea,.       
c4810 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b       UnlockArea;
c4820 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41  .  memset(&LockA
c4830 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c  rea, 0, sizeof(L
c4840 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d  ockArea));.  mem
c4850 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  set(&UnlockArea,
c4860 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63   0, sizeof(Unloc
c4870 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72  kArea));.  asser
c4880 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20  t( pFile!=0 );. 
c4890 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
c48a0 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
c48b0 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 55  ;.  OSTRACE4( "U
c48c0 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77  NLOCK %d to %d w
c48d0 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  as %d\n", pFile-
c48e0 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46  >h, locktype, pF
c48f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b  ile->locktype );
c4900 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d  .  type = pFile-
c4910 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28  >locktype;.  if(
c4920 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45   type>=EXCLUSIVE
c4930 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63  _LOCK ){.    Loc
c4940 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
c4950 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  0L;.    LockArea
c4960 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
c4970 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
c4980 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49  fset = SHARED_FI
c4990 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41  RST;.    UnlockA
c49a0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41  rea.lRange = SHA
c49b0 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65  RED_SIZE;.    re
c49c0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  s = DosSetFileLo
c49d0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
c49e0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
c49f0 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45  kArea, LOCK_TIME
c4a00 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f  OUT, 0L );.    O
c4a10 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b  STRACE3( "UNLOCK
c4a20 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   %d exclusive lo
c4a30 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  ck res=%d\n", pF
c4a40 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20  ile->h, res );. 
c4a50 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d     if( locktype=
c4a60 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20  =SHARED_LOCK && 
c4a70 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
c4a80 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  e) != NO_ERROR )
c4a90 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  {.      /* This 
c4aa0 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70  should never hap
c4ab0 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20  pen.  We should 
c4ac0 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 74  always be able t
c4ad0 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71  o.      ** reacq
c4ae0 75 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f  uire the read lo
c4af0 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52  ck */.      OSTR
c4b00 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
c4b10 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 4c 6f   to %d getReadLo
c4b20 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20  ck() failed\n", 
c4b30 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
c4b40 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  pe );.      rc =
c4b50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
c4b60 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  LOCK;.    }.  }.
c4b70 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45    if( type>=RESE
c4b80 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  RVED_LOCK ){.   
c4b90 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
c4ba0 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b  t = 0L;.    Lock
c4bb0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
c4bc0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
c4bd0 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52  .lOffset = RESER
c4be0 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e  VED_BYTE;.    Un
c4bf0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c4c00 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20  = 1L;.    res = 
c4c10 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c4c20 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c4c30 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c4c40 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c4c50 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
c4c60 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20  CE3( "UNLOCK %d 
c4c70 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 64 5c  reserved res=%d\
c4c80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
c4c90 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c  s );.  }.  if( l
c4ca0 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
c4cb0 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44   && type>=SHARED
c4cc0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73  _LOCK ){.    res
c4cd0 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63   = unlockReadLoc
c4ce0 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53  k(pFile);.    OS
c4cf0 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20  TRACE5( "UNLOCK 
c4d00 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 25 64  %d is %d want %d
c4d10 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
c4d20 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b  e->h, type, lock
c4d30 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d  type, res );.  }
c4d40 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e  .  if( type>=PEN
c4d50 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  DING_LOCK ){.   
c4d60 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
c4d70 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b  t = 0L;.    Lock
c4d80 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
c4d90 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
c4da0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49  .lOffset = PENDI
c4db0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c  NG_BYTE;.    Unl
c4dc0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c4dd0 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44   1L;.    res = D
c4de0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
c4df0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
c4e00 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
c4e10 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
c4e20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  0L );.    OSTRAC
c4e30 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70  E3( "UNLOCK %d p
c4e40 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22  ending res=%d\n"
c4e50 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
c4e60 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e  );.  }.  pFile->
c4e70 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
c4e80 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  ype;.  OSTRACE3(
c4e90 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20   "UNLOCK %d now 
c4ea0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
c4eb0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c4ec0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
c4ed0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f  .}../*.** Contro
c4ee0 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74  l and query of t
c4ef0 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e  he open file han
c4f00 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  dle..*/.static i
c4f10 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f  nt os2FileContro
c4f20 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  l(sqlite3_file *
c4f30 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64  id, int op, void
c4f40 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63   *pArg){.  switc
c4f50 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  h( op ){.    cas
c4f60 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c  e SQLITE_FCNTL_L
c4f70 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20  OCKSTATE: {.    
c4f80 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20    *(int*)pArg = 
c4f90 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e  ((os2File*)id)->
c4fa0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20  locktype;.      
c4fb0 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e 54 4c  OSTRACE3( "FCNTL
c4fc0 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c 6f  _LOCKSTATE %d lo
c4fd0 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46  ck=%d\n", ((os2F
c4fe0 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f  ile*)id)->h, ((o
c4ff0 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63  s2File*)id)->loc
c5000 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72  ktype );.      r
c5010 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
c5020 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
c5030 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
c5040 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
c5050 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  n the sector siz
c5060 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68  e in bytes of th
c5070 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f  e underlying blo
c5080 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a  ck device for.**
c5090 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66   the specified f
c50a0 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d  ile. This is alm
c50b0 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62  ost always 512 b
c50c0 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65  ytes, but may be
c50d0 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73  .** larger for s
c50e0 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a  ome devices..**.
c50f0 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61  ** SQLite code a
c5100 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63  ssumes this func
c5110 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c  tion cannot fail
c5120 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65  . It also assume
c5130 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f  s that.** if two
c5140 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74   files are creat
c5150 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66  ed in the same f
c5160 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63  ile-system direc
c5170 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20  tory (i.e..** a 
c5180 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73  database and its
c5190 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74   journal file) t
c51a0 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73  hat the sector s
c51b0 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a  ize will be the.
c51c0 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68  ** same for both
c51d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c51e0 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 73 71  os2SectorSize(sq
c51f0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
c5200 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
c5210 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
c5220 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  SIZE;.}../*.** R
c5230 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f  eturn a vector o
c5240 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74  f device charact
c5250 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61  eristics..*/.sta
c5260 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 69 63  tic int os2Devic
c5270 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
c5280 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
c5290 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  d){.  return 0;.
c52a0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63  }.../*.** Charac
c52b0 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 73 69  ter set conversi
c52c0 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20  on objects used 
c52d0 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f  by conversion ro
c52e0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  utines..*/.stati
c52f0 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63  c UconvObject uc
c5300 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20  Utf8 = NULL; /* 
c5310 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20  convert between 
c5320 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 20  UTF-8 and UCS-2 
c5330 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f  */.static UconvO
c5340 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 4e 55  bject uclCp = NU
c5350 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20  LL;  /* convert 
c5360 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f  between local co
c5370 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 2d 32  depage and UCS-2
c5380 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65   */../*.** Helpe
c5390 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  r function to in
c53a0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e  itialize the con
c53b0 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20  version objects 
c53c0 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d  from and to UTF-
c53d0 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  8..*/.static voi
c53e0 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63  d initUconvObjec
c53f0 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66  ts( void ){.  if
c5400 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76  ( UniCreateUconv
c5410 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 26  Object( UTF_8, &
c5420 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c 53 5f  ucUtf8 ) != ULS_
c5430 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 63  SUCCESS ).    uc
c5440 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69  Utf8 = NULL;.  i
c5450 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f  f ( UniCreateUco
c5460 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 68  nvObject( (UniCh
c5470 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73  ar *)L"@path=yes
c5480 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d 20 55  ", &uclCp ) != U
c5490 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20  LS_SUCCESS ).   
c54a0 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d   uclCp = NULL;.}
c54b0 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66  ../*.** Helper f
c54c0 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 20  unction to free 
c54d0 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f  the conversion o
c54e0 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20  bjects from and 
c54f0 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61  to UTF-8..*/.sta
c5500 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 63 6f  tic void freeUco
c5510 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20  nvObjects( void 
c5520 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 66 38  ){.  if ( ucUtf8
c5530 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63   ).    UniFreeUc
c5540 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 74 66  onvObject( ucUtf
c5550 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 6c 43  8 );.  if ( uclC
c5560 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55  p ).    UniFreeU
c5570 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c 43  convObject( uclC
c5580 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 3d 20  p );.  ucUtf8 = 
c5590 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20  NULL;.  uclCp = 
c55a0 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48  NULL;.}../*.** H
c55b0 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74  elper function t
c55c0 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 20  o convert UTF-8 
c55d0 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63  filenames to loc
c55e0 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 65  al OS/2 codepage
c55f0 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65  ..** The two-ste
c5600 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74  p process: first
c5610 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63   convert the inc
c5620 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69  oming UTF-8 stri
c5630 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32  ng.** into UCS-2
c5640 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55   and then from U
c5650 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 72 72  CS-2 to the curr
c5660 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a  ent codepage..**
c5670 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63 68   The returned ch
c5680 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74  ar pointer has t
c5690 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73  o be freed..*/.s
c56a0 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f 6e 76  tatic char *conv
c56b0 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28  ertUtf8PathToCp(
c56c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20   const char *in 
c56d0 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d  ){.  UniChar tem
c56e0 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48  pPath[CCHMAXPATH
c56f0 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d  ];.  char *out =
c5700 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28   (char *)calloc(
c5710 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29   CCHMAXPATH, 1 )
c5720 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a  ;..  if( !out ).
c5730 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b      return NULL;
c5740 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 20  ..  if( !ucUtf8 
c5750 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20  || !uclCp ).    
c5760 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73  initUconvObjects
c5770 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d  ();..  /* determ
c5780 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74  ine string for t
c5790 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66  he conversion of
c57a0 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20   UTF-8 which is 
c57b0 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 20  CP1208 */.  if( 
c57c0 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63 55  UniStrToUcs( ucU
c57d0 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 28  tf8, tempPath, (
c57e0 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41  char *)in, CCHMA
c57f0 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53  XPATH ) != ULS_S
c5800 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65 74  UCCESS ).    ret
c5810 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63  urn out; /* if c
c5820 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c  onversion fails,
c5830 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74   return the empt
c5840 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f  y string */..  /
c5850 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72  * conversion for
c5860 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67   current codepag
c5870 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75  e which can be u
c5880 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f  sed for paths */
c5890 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73  .  UniStrFromUcs
c58a0 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65  ( uclCp, out, te
c58b0 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41  mpPath, CCHMAXPA
c58c0 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  TH );..  return 
c58d0 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65  out;.}../*.** He
c58e0 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f  lper function to
c58f0 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d   convert filenam
c5900 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f  es from local co
c5910 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d 38 2e  depage to UTF-8.
c5920 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70  .** The two-step
c5930 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20   process: first 
c5940 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f  convert the inco
c5950 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 70  ming codepage-sp
c5960 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67  ecific.** string
c5970 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20   into UCS-2 and 
c5980 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20  then from UCS-2 
c5990 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 65 20  to the codepage 
c59a0 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65  of UTF-8..** The
c59b0 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 70   returned char p
c59c0 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65  ointer has to be
c59d0 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68   freed..**.** Th
c59e0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e  is function is n
c59f0 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 65 20  on-static to be 
c5a00 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 69 73  able to use this
c5a10 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a   in shell.c and.
c5a20 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 6c 69  ** similar appli
c5a30 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b  cations that tak
c5a40 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61  e command line a
c5a50 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61  rguments..*/.cha
c5a60 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 68  r *convertCpPath
c5a70 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 63 68  ToUtf8( const ch
c5a80 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43  ar *in ){.  UniC
c5a90 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48  har tempPath[CCH
c5aa0 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72  MAXPATH];.  char
c5ab0 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29   *out = (char *)
c5ac0 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41  calloc( CCHMAXPA
c5ad0 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20  TH, 1 );..  if( 
c5ae0 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75 72  !out ).    retur
c5af0 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21  n NULL;..  if( !
c5b00 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70  ucUtf8 || !uclCp
c5b10 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76   ).    initUconv
c5b20 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a  Objects();..  /*
c5b30 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20   conversion for 
c5b40 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65  current codepage
c5b50 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73   which can be us
c5b60 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a  ed for paths */.
c5b70 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63    if( UniStrToUc
c5b80 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 61  s( uclCp, tempPa
c5b90 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20  th, (char *)in, 
c5ba0 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20  CCHMAXPATH ) != 
c5bb0 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20  ULS_SUCCESS ).  
c5bc0 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a    return out; /*
c5bd0 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66   if conversion f
c5be0 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65  ails, return the
c5bf0 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f   empty string */
c5c00 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65  ..  /* determine
c5c10 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20   string for the 
c5c20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54  conversion of UT
c5c30 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 31  F-8 which is CP1
c5c40 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46  208 */.  UniStrF
c5c50 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 2c 20  romUcs( ucUtf8, 
c5c60 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43  out, tempPath, C
c5c70 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20  CHMAXPATH );..  
c5c80 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f  return out;.}../
c5c90 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72  *.** This vector
c5ca0 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65   defines all the
c5cb0 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 61   methods that ca
c5cc0 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a  n operate on an.
c5cd0 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ** sqlite3_file 
c5ce0 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74  for os2..*/.stat
c5cf0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
c5d00 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49  _io_methods os2I
c5d10 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c  oMethod = {.  1,
c5d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5d30 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
c5d40 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73  ion */.  os2Clos
c5d50 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 20  e,.  os2Read,.  
c5d60 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 54  os2Write,.  os2T
c5d70 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 79  runcate,.  os2Sy
c5d80 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a  nc,.  os2FileSiz
c5d90 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20  e,.  os2Lock,.  
c5da0 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32  os2Unlock,.  os2
c5db0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
c5dc0 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74  k,.  os2FileCont
c5dd0 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72  rol,.  os2Sector
c5de0 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 63  Size,.  os2Devic
c5df0 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
c5e00 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .};../**********
c5e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e50 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74  *.** Here ends t
c5e60 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74  he I/O methods t
c5e70 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c  hat form the sql
c5e80 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
c5e90 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  object..**.** Th
c5ea0 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20  e next block of 
c5eb0 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  code implements 
c5ec0 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e  the VFS methods.
c5ed0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
c5ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
c5f20 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74  /*.** Create a t
c5f30 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61  emporary file na
c5f40 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75  me in zBuf.  zBu
c5f50 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e  f must be big en
c5f60 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20  ough to.** hold 
c5f70 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  at pVfs->mxPathn
c5f80 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a  ame characters..
c5f90 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
c5fa0 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42  tTempname(int nB
c5fb0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29  uf, char *zBuf )
c5fc0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
c5fd0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a   unsigned char z
c5fe0 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61  Chars[] =.    "a
c5ff0 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71  bcdefghijklmnopq
c6000 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22  rstuvwxyz".    "
c6010 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50  ABCDEFGHIJKLMNOP
c6020 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20  QRSTUVWXYZ".    
c6030 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20  "0123456789";.  
c6040 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72  int i, j;.  char
c6050 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b 33 5d   zTempPathBuf[3]
c6060 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 74  ;.  PSZ zTempPat
c6070 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 50  h = (PSZ)&zTempP
c6080 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 73 71  athBuf;.  if( sq
c6090 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
c60a0 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d  tory ){.    zTem
c60b0 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 5f  pPath = sqlite3_
c60c0 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a  temp_directory;.
c60d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
c60e0 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53   DosScanEnv( (PS
c60f0 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70  Z)"TEMP", &zTemp
c6100 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20  Path ) ){.      
c6110 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20  if( DosScanEnv( 
c6120 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65  (PSZ)"TMP", &zTe
c6130 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20  mpPath ) ){.    
c6140 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45      if( DosScanE
c6150 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 49 52  nv( (PSZ)"TMPDIR
c6160 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20  ", &zTempPath ) 
c6170 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 55 4c  ){.           UL
c6180 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d  ONG ulDriveNum =
c6190 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 3d   0, ulDriveMap =
c61a0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 44   0;.           D
c61b0 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44 69  osQueryCurrentDi
c61c0 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c  sk( &ulDriveNum,
c61d0 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a   &ulDriveMap );.
c61e0 20 20 20 20 20 20 20 20 20 20 20 73 70 72 69 6e             sprin
c61f0 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 6d 70  tf( (char*)zTemp
c6200 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 68  Path, "%c:", (ch
c6210 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 72 69  ar)( 'A' + ulDri
c6220 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20  veNum - 1 ) );. 
c6230 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
c6240 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20  .    }.  }.  /* 
c6250 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 61 69  Strip off a trai
c6260 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 20  ling slashes or 
c6270 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68  backslashes, oth
c6280 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64 20  erwise we would 
c6290 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69  get *.   * multi
c62a0 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68 65  ple (back)slashe
c62b0 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20 44  s which causes D
c62c0 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 6c  osOpen() to fail
c62d0 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  .              *
c62e0 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 73  .   * Trailing s
c62f0 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 61 6c  paces are not al
c6300 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e 20 20  lowed, either.  
c6310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6320 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a            */.  j
c6330 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
c6340 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20  30(zTempPath);. 
c6350 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 26 26   while( j > 0 &&
c6360 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31   ( zTempPath[j-1
c6370 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 65  ] == '\\' || zTe
c6380 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27  mpPath[j-1] == '
c6390 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  /'.             
c63a0 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70 50         || zTempP
c63b0 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 20  ath[j-1] == ' ' 
c63c0 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 20  ) ){.    j--;.  
c63d0 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d  }.  zTempPath[j]
c63e0 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 21   = '\0';.  if( !
c63f0 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
c6400 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 63 68  ectory ){.    ch
c6410 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 54 46  ar *zTempPathUTF
c6420 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74 68   = convertCpPath
c6430 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 61 74  ToUtf8( zTempPat
c6440 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  h );.    sqlite3
c6450 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d  _snprintf( nBuf-
c6460 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20  30, zBuf,.      
c6470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6480 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d  "%s\\"SQLITE_TEM
c6490 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a  P_FILE_PREFIX, z
c64a0 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20  TempPathUTF );. 
c64b0 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 50 61     free( zTempPa
c64c0 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73 65  thUTF );.  }else
c64d0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
c64e0 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c  printf( nBuf-30,
c64f0 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20   zBuf,.         
c6500 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73               "%s
c6510 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46  \\"SQLITE_TEMP_F
c6520 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d  ILE_PREFIX, zTem
c6530 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20 6a  pPath );.  }.  j
c6540 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
c6550 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 73 71  30( zBuf );.  sq
c6560 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
c6570 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 29  ( 20, &zBuf[j] )
c6580 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20  ;.  for( i = 0; 
c6590 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b  i < 20; i++, j++
c65a0 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20   ){.    zBuf[j] 
c65b0 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20  = (char)zChars[ 
c65c0 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  ((unsigned char)
c65d0 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66  zBuf[j])%(sizeof
c65e0 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20  (zChars)-1) ];. 
c65f0 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30   }.  zBuf[j] = 0
c6600 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 54  ;.  OSTRACE2( "T
c6610 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73  EMP FILENAME: %s
c6620 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20 72  \n", zBuf );.  r
c6630 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
c6640 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20  .}.../*.** Turn 
c6650 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e  a relative pathn
c6660 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20  ame into a full 
c6670 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65  pathname.  Write
c6680 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74   the full.** pat
c6690 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c 6c  hname into zFull
c66a0 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c  [].  zFull[] wil
c66b0 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 56  l be at least pV
c66c0 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a  fs->mxPathname.*
c66d0 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e  * bytes in size.
c66e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c66f0 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a  s2FullPathname(.
c6700 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
c6710 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Vfs,          /*
c6720 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20   Pointer to vfs 
c6730 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73  object */.  cons
c6740 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
c6750 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69  e,      /* Possi
c6760 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70  bly relative inp
c6770 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74  ut path */.  int
c6780 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20   nFull,         
c6790 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
c67a0 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 65   of output buffe
c67b0 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  r in bytes */.  
c67c0 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20  char *zFull     
c67d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
c67e0 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a  utput buffer */.
c67f0 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c 61  ){.  char *zRela
c6800 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 72 74  tiveCp = convert
c6810 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 52  Utf8PathToCp( zR
c6820 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 68 61  elative );.  cha
c6830 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41 58  r zFullCp[CCHMAX
c6840 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 20  PATH] = "\0";.  
c6850 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a  char *zFullUTF;.
c6860 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44 6f    APIRET rc = Do
c6870 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20  sQueryPathInfo( 
c6880 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 49 4c  zRelativeCp, FIL
c6890 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c 20  _QUERYFULLNAME, 
c68a0 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20 20  zFullCp,.       
c68b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c68c0 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 58 50           CCHMAXP
c68d0 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 20 7a  ATH );.  free( z
c68e0 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a 20 20  RelativeCp );.  
c68f0 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 65  zFullUTF = conve
c6900 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20  rtCpPathToUtf8( 
c6910 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71 6c  zFullCp );.  sql
c6920 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e  ite3_snprintf( n
c6930 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75  Full, zFull, zFu
c6940 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 65 28  llUTF );.  free(
c6950 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 72   zFullUTF );.  r
c6960 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45  eturn rc == NO_E
c6970 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  RROR ? SQLITE_OK
c6980 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b   : SQLITE_IOERR;
c6990 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  .}.../*.** Open 
c69a0 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
c69b0 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a 20  c int os2Open(. 
c69c0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
c69d0 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  fs,            /
c69e0 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20  * Not used */.  
c69f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
c6a00 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
c6a10 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
c6a20 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  e */.  sqlite3_f
c6a30 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20  ile *id,        
c6a40 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
c6a50 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61  e SQLite file ha
c6a60 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69  ndle here */.  i
c6a70 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
c6a80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c6a90 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20  Open mode flags 
c6aa0 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c  */.  int *pOutFl
c6ab0 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ags             
c6ac0 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74     /* Status ret
c6ad0 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a  urn flags */.){.
c6ae0 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c 4f    HFILE h;.  ULO
c6af0 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75  NG ulFileAttribu
c6b00 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c  te = FILE_NORMAL
c6b10 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e  ;.  ULONG ulOpen
c6b20 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c 4f  Flags = 0;.  ULO
c6b30 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20  NG ulOpenMode = 
c6b40 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46  0;.  os2File *pF
c6b50 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29  ile = (os2File*)
c6b60 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20  id;.  APIRET rc 
c6b70 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c  = NO_ERROR;.  UL
c6b80 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20  ONG ulAction;.  
c6b90 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20  char *zNameCp;. 
c6ba0 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 43   char zTmpname[C
c6bb0 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20 20  CHMAXPATH+1];   
c6bc0 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 68 6f   /* Buffer to ho
c6bd0 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70 20  ld name of temp 
c6be0 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  file */..  /* If
c6bf0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
c6c00 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
c6c10 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67  ction is NULL, g
c6c20 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20  enerate a .  ** 
c6c30 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e  temporary file n
c6c40 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f  ame to use .  */
c6c50 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b  .  if( !zName ){
c6c60 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65  .    int rc = ge
c6c70 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41 58  tTempname(CCHMAX
c6c80 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65  PATH+1, zTmpname
c6c90 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
c6ca0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
c6cb0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
c6cc0 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a   }.    zName = z
c6cd0 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20  Tmpname;.  }... 
c6ce0 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 2c 20   memset( pFile, 
c6cf0 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65  0, sizeof(*pFile
c6d00 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 32  ) );..  OSTRACE2
c6d10 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 64 5c  ( "OPEN want %d\
c6d20 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20 20  n", flags );..  
c6d30 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
c6d40 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
c6d50 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d  E ){.    ulOpenM
c6d60 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45  ode |= OPEN_ACCE
c6d70 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20  SS_READWRITE;.  
c6d80 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
c6d90 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 20  N read/write\n" 
c6da0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
c6db0 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50  ulOpenMode |= OP
c6dc0 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e  EN_ACCESS_READON
c6dd0 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31  LY;.    OSTRACE1
c6de0 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e 6c  ( "OPEN read onl
c6df0 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  y\n" );.  }..  i
c6e00 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
c6e10 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b  E_OPEN_CREATE ){
c6e20 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73  .    ulOpenFlags
c6e30 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f   |= OPEN_ACTION_
c6e40 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c  OPEN_IF_EXISTS |
c6e50 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45   OPEN_ACTION_CRE
c6e60 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20  ATE_IF_NEW;.    
c6e70 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20  OSTRACE1( "OPEN 
c6e80 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 65 5c  open new/create\
c6e90 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n" );.  }else{. 
c6ea0 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c     ulOpenFlags |
c6eb0 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50  = OPEN_ACTION_OP
c6ec0 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f  EN_IF_EXISTS | O
c6ed0 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f  PEN_ACTION_FAIL_
c6ee0 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52  IF_NEW;.    OSTR
c6ef0 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e  ACE1( "OPEN open
c6f00 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b 0a   existing\n" );.
c6f10 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73    }..  if( flags
c6f20 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d   & SQLITE_OPEN_M
c6f30 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75 6c  AIN_DB ){.    ul
c6f40 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e  OpenMode |= OPEN
c6f50 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b  _SHARE_DENYNONE;
c6f60 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22  .    OSTRACE1( "
c6f70 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64 2f  OPEN share read/
c6f80 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65  write\n" );.  }e
c6f90 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d  lse{.    ulOpenM
c6fa0 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52  ode |= OPEN_SHAR
c6fb0 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 20 20  E_DENYWRITE;.   
c6fc0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e   OSTRACE1( "OPEN
c6fd0 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e 6c 79   share read only
c6fe0 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  \n" );.  }..  if
c6ff0 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
c7000 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
c7010 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 72 20  OSE ){.    char 
c7020 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 58 50  pathUtf8[CCHMAXP
c7030 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44 45  ATH];.#ifdef NDE
c7040 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 62 75  BUG /* when debu
c7050 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 74 6f  gging we want to
c7060 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
c7070 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20 20   deleted */.    
c7080 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 20  ulFileAttribute 
c7090 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a 23  = FILE_HIDDEN;.#
c70a0 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 75 6c  endif.    os2Ful
c70b0 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 73 2c  lPathname( pVfs,
c70c0 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50 41   zName, CCHMAXPA
c70d0 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 3b 0a  TH, pathUtf8 );.
c70e0 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54      pFile->pathT
c70f0 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55 74  oDel = convertUt
c7100 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 74 68  f8PathToCp( path
c7110 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 54 52  Utf8 );.    OSTR
c7120 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 64 64  ACE1( "OPEN hidd
c7130 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f  en/delete on clo
c7140 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 75 74  se file attribut
c7150 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65  es\n" );.  }else
c7160 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74  {.    pFile->pat
c7170 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20  hToDel = NULL;. 
c7180 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50     OSTRACE1( "OP
c7190 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 61  EN normal file a
c71a0 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a 20  ttribute\n" );. 
c71b0 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73 20   }..  /* always 
c71c0 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 61  open in random a
c71d0 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20 70  ccess mode for p
c71e0 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 20 73  ossibly better s
c71f0 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e  peed */.  ulOpen
c7200 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41  Mode |= OPEN_FLA
c7210 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f  GS_RANDOM;.  ulO
c7220 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f  penMode |= OPEN_
c7230 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52  FLAGS_FAIL_ON_ER
c7240 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64  ROR;.  ulOpenMod
c7250 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f  e |= OPEN_FLAGS_
c7260 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a 4e  NOINHERIT;..  zN
c7270 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55  ameCp = convertU
c7280 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 4e 61  tf8PathToCp( zNa
c7290 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73  me );.  rc = Dos
c72a0 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d 65  Open( (PSZ)zName
c72b0 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Cp,.            
c72c0 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 20 20      &h,.        
c72d0 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 69 6f          &ulActio
c72e0 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n,.             
c72f0 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 20     0L,.         
c7300 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 74 74         ulFileAtt
c7310 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 20 20  ribute,.        
c7320 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 46 6c          ulOpenFl
c7330 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ags,.           
c7340 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c       ulOpenMode,
c7350 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c7360 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b   (PEAOP2)NULL );
c7370 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 43 70  .  free( zNameCp
c7380 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20   );.  if( rc != 
c7390 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
c73a0 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 4e 20  OSTRACE7( "OPEN 
c73b0 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 20 72  Invalid handle r
c73c0 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20  c=%d: zName=%s, 
c73d0 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 75  ulAction=%#lx, u
c73e0 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46 6c  lAttr=%#lx, ulFl
c73f0 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 65  ags=%#lx, ulMode
c7400 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 20  =%#lx\n",.      
c7410 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e 61 6d          rc, zNam
c7420 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c 46  e, ulAction, ulF
c7430 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 75 6c  ileAttribute, ul
c7440 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 65  OpenFlags, ulOpe
c7450 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 66 28  nMode );.    if(
c7460 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65   pFile->pathToDe
c7470 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 28 20  l ).      free( 
c7480 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
c7490 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70   );.    pFile->p
c74a0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b  athToDel = NULL;
c74b0 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26  .    if( flags &
c74c0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
c74d0 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20  DWRITE ){.      
c74e0 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e 20  OSTRACE2( "OPEN 
c74f0 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c  %d Invalid handl
c7500 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c 20  e\n", ((flags | 
c7510 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
c7520 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 5f  ONLY) & ~SQLITE_
c7530 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 20  OPEN_READWRITE) 
c7540 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
c7550 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c 20 7a  os2Open( pVfs, z
c7560 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20 20  Name, id,.      
c7570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7580 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45  ((flags | SQLITE
c7590 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20  _OPEN_READONLY) 
c75a0 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  & ~SQLITE_OPEN_R
c75b0 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 20 20  EADWRITE),.     
c75c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c75d0 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20   pOutFlags );.  
c75e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
c75f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e  eturn SQLITE_CAN
c7600 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d  TOPEN;.    }.  }
c7610 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67  ..  if( pOutFlag
c7620 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c  s ){.    *pOutFl
c7630 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 53 51  ags = flags & SQ
c7640 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
c7650 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50 45  ITE ? SQLITE_OPE
c7660 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 53 51  N_READWRITE : SQ
c7670 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
c7680 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c 65  LY;.  }..  pFile
c7690 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73 32  ->pMethod = &os2
c76a0 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c  IoMethod;.  pFil
c76b0 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65 6e  e->h = h;.  Open
c76c0 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 4f  Counter(+1);.  O
c76d0 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e 20 25  STRACE3( "OPEN %
c76e0 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c 6e  d pOutFlags=%d\n
c76f0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f 75  ", pFile->h, pOu
c7700 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 74 75  tFlags );.  retu
c7710 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
c7720 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68  ./*.** Delete th
c7730 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f  e named file..*/
c7740 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44  .static int os2D
c7750 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33  elete(.  sqlite3
c7760 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
c7770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7780 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f  /* Not used on o
c7790 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  s2 */.  const ch
c77a0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
c77b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c77c0 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74  * Name of file t
c77d0 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e  o delete */.  in
c77e0 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20 20  t syncDir       
c77f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7800 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64       /* Not used
c7810 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20   on os2 */.){.  
c7820 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
c7830 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46  RROR;.  char *zF
c7840 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76  ilenameCp = conv
c7850 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28  ertUtf8PathToCp(
c7860 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20   zFilename );.  
c7870 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
c7880 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
c7890 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a 20  OERR_DELETE );. 
c78a0 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 65 28   rc = DosDelete(
c78b0 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43   (PSZ)zFilenameC
c78c0 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69  p );.  free( zFi
c78d0 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53  lenameCp );.  OS
c78e0 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 45 20  TRACE2( "DELETE 
c78f0 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65  \"%s\"\n", zFile
c7900 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72 6e  name );.  return
c7910 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc == NO_ERROR 
c7920 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51  ? SQLITE_OK : SQ
c7930 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54  LITE_IOERR_DELET
c7940 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  E;.}../*.** Chec
c7950 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20  k the existance 
c7960 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 20  and status of a 
c7970 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
c7980 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 0a 20  int os2Access(. 
c7990 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
c79a0 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  fs,        /* No
c79b0 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f  t used on os2 */
c79c0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
c79d0 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20  Filename,    /* 
c79e0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
c79f0 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66  check */.  int f
c7a00 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
c7a10 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20       /* Type of 
c7a20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20  test to make on 
c7a30 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69  this file */.  i
c7a40 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 20 20  nt *pOut        
c7a50 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
c7a60 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a  e results here *
c7a70 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 55  /.){.  FILESTATU
c7a80 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e  S3 fsts3ConfigIn
c7a90 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20  fo;.  APIRET rc 
c7aa0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68  = NO_ERROR;.  ch
c7ab0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20  ar *zFilenameCp 
c7ac0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74  = convertUtf8Pat
c7ad0 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65  hToCp( zFilename
c7ae0 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20 26   );..  memset( &
c7af0 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c  fsts3ConfigInfo,
c7b00 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33   0, sizeof(fsts3
c7b10 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a 20  ConfigInfo) );. 
c7b20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61   rc = DosQueryPa
c7b30 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46 69  thInfo( (PSZ)zFi
c7b40 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53 54  lenameCp, FIL_ST
c7b50 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 20 20  ANDARD,.        
c7b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7b70 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66   &fsts3ConfigInf
c7b80 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 54  o, sizeof(FILEST
c7b90 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 65 65  ATUS3) );.  free
c7ba0 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b  ( zFilenameCp );
c7bb0 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 41 43  .  OSTRACE4( "AC
c7bc0 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 69 67  CESS fsts3Config
c7bd0 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25 64  Info.attrFile=%d
c7be0 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 64 5c   flags=%d rc=%d\
c7bf0 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
c7c00 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e  fsts3ConfigInfo.
c7c10 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 73 2c  attrFile, flags,
c7c20 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 68 28   rc );.  switch(
c7c30 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61   flags ){.    ca
c7c40 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
c7c50 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20  _READ:.    case 
c7c60 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
c7c70 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d  ISTS:.      rc =
c7c80 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   (rc == NO_ERROR
c7c90 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  );.      OSTRACE
c7ca0 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 63  3( "ACCESS %s ac
c7cb0 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 6e 64  cess of read and
c7cc0 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 5c 6e   exists  rc=%d\n
c7cd0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63  ", zFilename, rc
c7ce0 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
c7cf0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
c7d00 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
c7d10 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72  E:.      rc = (r
c7d20 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 26  c == NO_ERROR) &
c7d30 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 69 67  & ( (fsts3Config
c7d40 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 26 20  Info.attrFile & 
c7d50 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20 3d  FILE_READONLY) =
c7d60 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f 53 54  = 0 );.      OST
c7d70 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20 25  RACE3( "ACCESS %
c7d80 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61 64  s access of read
c7d90 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c 6e 22  /write  rc=%d\n"
c7da0 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20  , zFilename, rc 
c7db0 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
c7dc0 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
c7dd0 20 20 20 61 73 73 65 72 74 28 20 21 22 49 6e 76     assert( !"Inv
c7de0 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d  alid flags argum
c7df0 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a 70  ent" );.  }.  *p
c7e00 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75  Out = rc;.  retu
c7e10 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
c7e20 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
c7e30 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
c7e40 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  SION./*.** Inter
c7e50 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e  faces for openin
c7e60 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61  g a shared libra
c7e70 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72  ry, finding entr
c7e80 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68  y points.** with
c7e90 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  in the shared li
c7ea0 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69  brary, and closi
c7eb0 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  ng the shared li
c7ec0 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20  brary..*/./*.** 
c7ed0 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f  Interfaces for o
c7ee0 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20  pening a shared 
c7ef0 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67  library, finding
c7f00 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a   entry points.**
c7f10 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72   within the shar
c7f20 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20  ed library, and 
c7f30 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72  closing the shar
c7f40 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73  ed library..*/.s
c7f50 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44  tatic void *os2D
c7f60 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  lOpen(sqlite3_vf
c7f70 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63  s *pVfs, const c
c7f80 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
c7f90 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72 72  .  UCHAR loadErr
c7fa0 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45  [256];.  HMODULE
c7fb0 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 20   hmod;.  APIRET 
c7fc0 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  rc;.  char *zFil
c7fd0 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72  enameCp = conver
c7fe0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 7a 46  tUtf8PathToCp(zF
c7ff0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20 3d  ilename);.  rc =
c8000 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28   DosLoadModule((
c8010 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a  PSZ)loadErr, siz
c8020 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46  eof(loadErr), zF
c8030 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f 64  ilenameCp, &hmod
c8040 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c 65 6e  );.  free(zFilen
c8050 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 72 6e  ameCp);.  return
c8060 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc != NO_ERROR 
c8070 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d 6f  ? 0 : (void*)hmo
c8080 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d  d;.}./*.** A no-
c8090 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 72 72  op since the err
c80a0 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
c80b0 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f  ned on the DosLo
c80c0 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a  adModule call..*
c80d0 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 75  * os2Dlopen retu
c80e0 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c  rns zero if DosL
c80f0 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74  oadModule is not
c8100 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a   successful..*/.
c8110 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44  static void os2D
c8120 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76  lError(sqlite3_v
c8130 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42  fs *pVfs, int nB
c8140 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75  uf, char *zBufOu
c8150 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a  t){./* no-op */.
c8160 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f  }.static void *o
c8170 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f  s2DlSym(sqlite3_
c8180 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20  vfs *pVfs, void 
c8190 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20  *pHandle, const 
c81a0 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a  char *zSymbol){.
c81b0 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50 49    PFN pfn;.  API
c81c0 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d 20 44  RET rc;.  rc = D
c81d0 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72 28  osQueryProcAddr(
c81e0 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65  (HMODULE)pHandle
c81f0 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26  , 0L, zSymbol, &
c8200 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 20 21  pfn);.  if( rc !
c8210 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c8220 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 6d 62    /* if the symb
c8230 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 6e 6f  ol itself was no
c8240 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20  t found, search 
c8250 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 73 61  again for the sa
c8260 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f 6c  me.     * symbol
c8270 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 20 75   with an extra u
c8280 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 74 20  nderscore, that 
c8290 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 64 20  might be needed 
c82a0 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 20 2a  depending.     *
c82b0 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   on the calling 
c82c0 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20  convention */.  
c82d0 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b    char _zSymbol[
c82e0 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 20  256] = "_";.    
c82f0 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f 6c  strncat(_zSymbol
c8300 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29 3b  , zSymbol, 255);
c8310 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 75 65  .    rc = DosQue
c8320 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44  ryProcAddr((HMOD
c8330 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c  ULE)pHandle, 0L,
c8340 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29   _zSymbol, &pfn)
c8350 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
c8360 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20  c != NO_ERROR ? 
c8370 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0a  0 : (void*)pfn;.
c8380 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  }.static void os
c8390 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  2DlClose(sqlite3
c83a0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64  _vfs *pVfs, void
c83b0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44 6f   *pHandle){.  Do
c83c0 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f  sFreeModule((HMO
c83d0 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d  DULE)pHandle);.}
c83e0 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c  .#else /* if SQL
c83f0 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
c8400 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e  TENSION is defin
c8410 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65  ed: */.  #define
c8420 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20 23   os2DlOpen 0.  #
c8430 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 72 6f  define os2DlErro
c8440 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73  r 0.  #define os
c8450 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 66 69  2DlSym 0.  #defi
c8460 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30 0a  ne os2DlClose 0.
c8470 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57  #endif.../*.** W
c8480 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66 20  rite up to nBuf 
c8490 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e  bytes of randomn
c84a0 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a  ess into zBuf..*
c84b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
c84c0 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74  Randomness(sqlit
c84d0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
c84e0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
c84f0 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  uf ){.  int n = 
c8500 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  0;.#if defined(S
c8510 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20  QLITE_TEST).  n 
c8520 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74  = nBuf;.  memset
c8530 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b  (zBuf, 0, nBuf);
c8540 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 69 7a  .#else.  int siz
c8550 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f  eofULong = sizeo
c8560 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 20  f(ULONG);.  if( 
c8570 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 54 45  (int)sizeof(DATE
c8580 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d 20  TIME) <= nBuf - 
c8590 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 49 4d  n ){.    DATETIM
c85a0 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 74 44  E x;.    DosGetD
c85b0 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 20 20  ateTime(&x);.   
c85c0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
c85d0 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29  , &x, sizeof(x))
c85e0 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f  ;.    n += sizeo
c85f0 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  f(x);.  }..  if(
c8600 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20   sizeofULong <= 
c8610 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20  nBuf - n ){.    
c8620 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 44  PPIB ppib;.    D
c8630 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
c8640 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20 20  NULL, &ppib);.  
c8650 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
c8660 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f 75 6c  ], &ppib->pib_ul
c8670 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  pid, sizeofULong
c8680 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
c8690 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20  ofULong;.  }..  
c86a0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20  if( sizeofULong 
c86b0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20  <= nBuf - n ){. 
c86c0 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20     PTIB ptib;.  
c86d0 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63    DosGetInfoBloc
c86e0 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b  ks(&ptib, NULL);
c86f0 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  .    memcpy(&zBu
c8700 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69 62  f[n], &ptib->tib
c8710 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74  _ptib2->tib2_ult
c8720 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29  id, sizeofULong)
c8730 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f  ;.    n += sizeo
c8740 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 2f  fULong;.  }..  /
c8750 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 68 61  * if we still ha
c8760 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 68 65  ven't filled the
c8770 20 62 75 66 66 65 72 20 79 65 74 20 74 68 65 20   buffer yet the 
c8780 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 2a  following will *
c8790 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 65 72  /.  /* grab ever
c87a0 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73 74  ything once inst
c87b0 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73 65  ead of making se
c87c0 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72 20  veral calls for 
c87d0 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a 2f  a single item */
c87e0 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f  .  if( sizeofULo
c87f0 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29  ng <= nBuf - n )
c8800 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53 79  {.    ULONG ulSy
c8810 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b 0a  sInfo[QSV_MAX];.
c8820 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 73 49      DosQuerySysI
c8830 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58 2c  nfo(1L, QSV_MAX,
c8840 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a 65   ulSysInfo, size
c8850 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d 41  ofULong * QSV_MA
c8860 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28  X);..    memcpy(
c8870 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73  &zBuf[n], &ulSys
c8880 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 4e  Info[QSV_MS_COUN
c8890 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c  T - 1], sizeofUL
c88a0 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73  ong);.    n += s
c88b0 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 20  izeofULong;..   
c88c0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   if( sizeofULong
c88d0 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a   <= nBuf - n ){.
c88e0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42        memcpy(&zB
c88f0 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66  uf[n], &ulSysInf
c8900 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54 45  o[QSV_TIMER_INTE
c8910 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 6f  RVAL - 1], sizeo
c8920 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e  fULong);.      n
c8930 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b   += sizeofULong;
c8940 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
c8950 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42  izeofULong <= nB
c8960 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20  uf - n ){.      
c8970 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c  memcpy(&zBuf[n],
c8980 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f   &ulSysInfo[QSV_
c8990 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 73  TIME_LOW - 1], s
c89a0 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20  izeofULong);.   
c89b0 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c     n += sizeofUL
c89c0 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ong;.    }.    i
c89d0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c  f( sizeofULong <
c89e0 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20  = nBuf - n ){.  
c89f0 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66      memcpy(&zBuf
c8a00 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b  [n], &ulSysInfo[
c8a10 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d 20  QSV_TIME_HIGH - 
c8a20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29  1], sizeofULong)
c8a30 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ;.      n += siz
c8a40 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a  eofULong;.    }.
c8a50 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c      if( sizeofUL
c8a60 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20  ong <= nBuf - n 
c8a70 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
c8a80 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73  &zBuf[n], &ulSys
c8a90 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41 49  Info[QSV_TOTAVAI
c8aa0 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 6f  LMEM - 1], sizeo
c8ab0 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e  fULong);.      n
c8ac0 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b   += sizeofULong;
c8ad0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
c8ae0 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d  f..  return n;.}
c8af0 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f  ../*.** Sleep fo
c8b00 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65  r a little while
c8b10 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d  .  Return the am
c8b20 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65  ount of time sle
c8b30 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  pt..** The argum
c8b40 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ent is the numbe
c8b50 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64  r of microsecond
c8b60 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65  s we want to sle
c8b70 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  ep..** The retur
c8b80 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e  n value is the n
c8b90 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65  umber of microse
c8ba0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61  conds of sleep a
c8bb0 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65  ctually.** reque
c8bc0 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e  sted from the un
c8bd0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
c8be0 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d  ng system, a num
c8bf0 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67  ber which.** mig
c8c00 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68  ht be greater th
c8c10 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
c8c20 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74  he argument, but
c8c30 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61   not less.** tha
c8c40 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a  n the argument..
c8c50 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c8c60 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 33 5f  2Sleep( sqlite3_
c8c70 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d  vfs *pVfs, int m
c8c80 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73  icrosec ){.  Dos
c8c90 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 65 63  Sleep( (microsec
c8ca0 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 74 75  /1000) );.  retu
c8cb0 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a  rn microsec;.}..
c8cc0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
c8cd0 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66  ing variable, if
c8ce0 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65   set to a non-ze
c8cf0 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65  ro value, become
c8d00 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  s the result.** 
c8d10 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71  returned from sq
c8d20 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
c8d30 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 75  me().  This is u
c8d40 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e  sed for testing.
c8d50 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
c8d60 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
c8d70 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75  I int sqlite3_cu
c8d80 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a  rrent_time = 0;.
c8d90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69  #endif../*.** Fi
c8da0 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  nd the current t
c8db0 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61  ime (in Universa
c8dc0 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69  l Coordinated Ti
c8dd0 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a  me).  Write the.
c8de0 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  ** current time 
c8df0 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75  and date as a Ju
c8e00 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20  lian Day number 
c8e10 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a  into *prNow and.
c8e20 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65  ** return 0.  Re
c8e30 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69  turn 1 if the ti
c8e40 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e  me and date cann
c8e50 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  ot be found..*/.
c8e60 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 54 69  int os2CurrentTi
c8e70 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20  me( sqlite3_vfs 
c8e80 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70  *pVfs, double *p
c8e90 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65  rNow ){.  double
c8ea0 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69   now;.  SHORT mi
c8eb0 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74  nute; /* needs t
c8ec0 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 6f 70  o be able to cop
c8ed0 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 65 20  e with negative 
c8ee0 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20  timezone offset 
c8ef0 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 63 6f  */.  USHORT seco
c8f00 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 20  nd, hour,.      
c8f10 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79     day, month, y
c8f20 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 20  ear;.  DATETIME 
c8f30 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 65  dt;.  DosGetDate
c8f40 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 73  Time( &dt );.  s
c8f50 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 29  econd = (USHORT)
c8f60 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69  dt.seconds;.  mi
c8f70 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 64 74  nute = (SHORT)dt
c8f80 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 69  .minutes + dt.ti
c8f90 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d  mezone;.  hour =
c8fa0 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 72   (USHORT)dt.hour
c8fb0 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 48 4f  s;.  day = (USHO
c8fc0 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e  RT)dt.day;.  mon
c8fd0 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e  th = (USHORT)dt.
c8fe0 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d 20  month;.  year = 
c8ff0 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 72 3b  (USHORT)dt.year;
c9000 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69  ..  /* Calculati
c9010 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f  ons from http://
c9020 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e  www.astro.keele.
c9030 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f  ac.uk/~rno/Astro
c9040 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20  nomy/hjd.html.  
c9050 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73     http://www.as
c9060 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f  tro.keele.ac.uk/
c9070 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68  ~rno/Astronomy/h
c9080 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a  jd-0.1.c */.  /*
c9090 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 4a   Calculate the J
c90a0 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20  ulian days */.  
c90b0 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 30 37  now = day - 3207
c90c0 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 65  6 +.    1461*(ye
c90d0 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e  ar + 4800 + (mon
c90e0 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b  th - 14)/12)/4 +
c90f0 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20  .    367*(month 
c9100 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31  - 2 - (month - 1
c9110 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20  4)/12*12)/12 -. 
c9120 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 34 39     3*((year + 49
c9130 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34  00 + (month - 14
c9140 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20  )/12)/100)/4;.. 
c9150 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 61 63   /* Add the frac
c9160 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69  tional hours, mi
c9170 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a  ns and seconds *
c9180 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72  /.  now += (hour
c9190 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20   + 12.0)/24.0;. 
c91a0 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31   now += minute/1
c91b0 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20  440.0;.  now += 
c91c0 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a  second/86400.0;.
c91d0 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a    *prNow = now;.
c91e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
c91f0 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ST.  if( sqlite3
c9200 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b  _current_time ){
c9210 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71  .    *prNow = sq
c9220 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
c9230 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34  me/86400.0 + 244
c9240 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64  0587.5;.  }.#end
c9250 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  if.  return 0;.}
c9260 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  ..static int os2
c9270 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c  GetLastError(sql
c9280 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c9290 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
c92a0 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20  zBuf){.  return 
c92b0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  0;.}../*.** Init
c92c0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69  ialize and deini
c92d0 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72  tialize the oper
c92e0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74  ating system int
c92f0 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erface..*/.SQLIT
c9300 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c9310 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b  3_os_init(void){
c9320 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
c9330 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d 20 7b  3_vfs os2Vfs = {
c9340 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20  .    1,         
c9350 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
c9360 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f  ion */.    sizeo
c9370 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a  f(os2File),   /*
c9380 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20   szOsFile */.   
c9390 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 20 20   CCHMAXPATH,    
c93a0 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d      /* mxPathnam
c93b0 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  e */.    0,     
c93c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
c93d0 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32  Next */.    "os2
c93e0 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ",             /
c93f0 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30  * zName */.    0
c9400 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
c9410 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f    /* pAppData */
c9420 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20  ..    os2Open,  
c9430 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65           /* xOpe
c9440 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65  n */.    os2Dele
c9450 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78  te,         /* x
c9460 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73  Delete */.    os
c9470 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20  2Access,        
c9480 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20   /* xAccess */. 
c9490 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61     os2FullPathna
c94a0 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61  me,   /* xFullPa
c94b0 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73  thname */.    os
c94c0 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20  2DlOpen,        
c94d0 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20   /* xDlOpen */. 
c94e0 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20     os2DlError,  
c94f0 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f        /* xDlErro
c9500 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79  r */.    os2DlSy
c9510 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  m,          /* x
c9520 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32  DlSym */.    os2
c9530 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  DlClose,        
c9540 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20  /* xDlClose */. 
c9550 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73     os2Randomness
c9560 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d  ,     /* xRandom
c9570 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53  ness */.    os2S
c9580 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f  leep,          /
c9590 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20  * xSleep */.    
c95a0 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 2c 20  os2CurrentTime, 
c95b0 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69     /* xCurrentTi
c95c0 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74  me */.    os2Get
c95d0 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20  LastError    /* 
c95e0 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f  xGetLastError */
c95f0 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f  .  };.  sqlite3_
c9600 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 6f 73  vfs_register(&os
c9610 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74  2Vfs, 1);.  init
c9620 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a  UconvObjects();.
c9630 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c9640 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  OK;.}.SQLITE_API
c9650 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f   int sqlite3_os_
c9660 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 65  end(void){.  fre
c9670 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b  eUconvObjects();
c9680 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
c9690 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  _OK;.}..#endif /
c96a0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20  * SQLITE_OS_OS2 
c96b0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
c96c0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73  *** End of os_os
c96d0 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  2.c ************
c96e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c96f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9700 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
c9710 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
c9720 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  s_unix.c *******
c9730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9750 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d  **/./*.** 2004 M
c9760 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 22.**.** The 
c9770 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
c9780 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
c9790 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
c97a0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
c97b0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
c97c0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
c97d0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
c97e0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
c97f0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
c9800 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
c9810 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
c9820 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
c9830 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
c9840 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
c9850 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
c9860 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
c9870 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
c9880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c98a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c98b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c98c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
c98d0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
c98e0 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 6c 65  ns the VFS imple
c98f0 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75 6e  mentation for un
c9900 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e  ix-like operatin
c9910 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e 63  g systems.** inc
c9920 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63 4f  lude Linux, MacO
c9930 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20 56  SX, *BSD, QNX, V
c9940 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50 55  xWorks, AIX, HPU
c9950 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a 2a  X, and others..*
c9960 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61  *.** There are a
c9970 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 6c 20  ctually several 
c9980 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 69 6d  different VFS im
c9990 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 6e  plementations in
c99a0 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 54   this file..** T
c99b0 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 61  he differences a
c99c0 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 74 68  re in the way th
c99d0 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  at file locking 
c99e0 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 64 65  is done.  The de
c99f0 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65  fault.** impleme
c9a00 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 6f 73  ntation uses Pos
c9a10 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b  ix Advisory Lock
c9a20 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 65 20  s.  Alternative 
c9a30 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a  implementations.
c9a40 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c 20  ** use flock(), 
c9a50 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 69 6f  dot-files, vario
c9a60 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 20 6c  us proprietary l
c9a70 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c 20  ocking schemas, 
c9a80 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b 69  or simply.** ski
c9a90 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 6f  p locking all to
c9aa0 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  gether..**.** Th
c9ab0 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69  is source file i
c9ac0 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74 6f  s organized into
c9ad0 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 72 65   divisions where
c9ae0 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 20 76   the logic for v
c9af0 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75 6e  arious.** subfun
c9b00 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61 69  ctions is contai
c9b10 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 61  ned within the a
c9b20 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 69 73  ppropriate divis
c9b30 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a 20  ion.  PLEASE.** 
c9b40 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 54 55  KEEP THE STRUCTU
c9b50 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c 45 20  RE OF THIS FILE 
c9b60 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 6f 64  INTACT.  New cod
c9b70 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c 61 63  e should be plac
c9b80 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72  ed.** in the cor
c9b90 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 61 6e  rect division an
c9ba0 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 65 61  d should be clea
c9bb0 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a 0a  rly labeled..**.
c9bc0 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 6f 66  ** The layout of
c9bd0 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 61 73   divisions is as
c9be0 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
c9bf0 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75 72    *  General-pur
c9c00 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 6f 6e  pose declaration
c9c10 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 66 75  s and utility fu
c9c20 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20  nctions..**   * 
c9c30 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20   Unique file ID 
c9c40 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 56 78  logic used by Vx
c9c50 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 56  Works..**   *  V
c9c60 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 70  arious locking p
c9c70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d 65  rimitive impleme
c9c80 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65 78  ntations (all ex
c9c90 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b 69  cept proxy locki
c9ca0 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 66  ng):.**      + f
c9cb0 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72  or Posix Advisor
c9cc0 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20  y Locks.**      
c9cd0 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b  + for no-op lock
c9ce0 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20  s.**      + for 
c9cf0 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a  dot-file locks.*
c9d00 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 6c 6f  *      + for flo
c9d10 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20  ck() locking.** 
c9d20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65 64       + for named
c9d30 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73   semaphore locks
c9d40 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 0a   (VxWorks only).
c9d50 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 41 46  **      + for AF
c9d60 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f 63  P filesystem loc
c9d70 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29  ks (MacOSX only)
c9d80 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33  .**   *  sqlite3
c9d90 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e 6f  _file methods no
c9da0 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  t associated wit
c9db0 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20  h locking..**   
c9dc0 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f  *  Definitions o
c9dd0 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  f sqlite3_io_met
c9de0 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f 72  hods objects for
c9df0 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20   all locking.** 
c9e00 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 6c 75       methods plu
c9e10 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74  s "finder" funct
c9e20 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 6c 6f  ions for each lo
c9e30 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a 2a  cking method..**
c9e40 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 76 66     *  sqlite3_vf
c9e50 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d 65  s method impleme
c9e60 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a  ntations..**   *
c9e70 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74    Locking primit
c9e80 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 72 6f  ives for the pro
c9e90 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d  xy uber-locking-
c9ea0 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58 20  method. (MacOSX 
c9eb0 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44 65  only).**   *  De
c9ec0 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c  finitions of sql
c9ed0 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73  ite3_vfs objects
c9ee0 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67   for all locking
c9ef0 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20 20   methods.**     
c9f00 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74 61   plus implementa
c9f10 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33  tions of sqlite3
c9f20 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73  _os_init() and s
c9f30 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e  qlite3_os_end().
c9f40 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 75  .**.** $Id: os_u
c9f50 6e 69 78 2e 63 2c 76 20 31 2e 32 33 37 20 32 30  nix.c,v 1.237 20
c9f60 30 39 2f 30 31 2f 31 35 20 30 34 3a 33 30 3a 30  09/01/15 04:30:0
c9f70 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23  3 drh Exp $.*/.#
c9f80 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  if SQLITE_OS_UNI
c9f90 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  X              /
c9fa0 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 75  * This file is u
c9fb0 73 65 64 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79  sed on unix only
c9fc0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65   */../*.** There
c9fd0 20 61 72 65 20 76 61 72 69 6f 75 73 20 6d 65 74   are various met
c9fe0 68 6f 64 73 20 66 6f 72 20 66 69 6c 65 20 6c 6f  hods for file lo
c9ff0 63 6b 69 6e 67 20 75 73 65 64 20 66 6f 72 20 63  cking used for c
ca000 6f 6e 63 75 72 72 65 6e 63 79 0a 2a 2a 20 63 6f  oncurrency.** co
ca010 6e 74 72 6f 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ntrol:.**.**   1
ca020 2e 20 50 4f 53 49 58 20 6c 6f 63 6b 69 6e 67 20  . POSIX locking 
ca030 28 74 68 65 20 64 65 66 61 75 6c 74 29 2c 0a 2a  (the default),.*
ca040 2a 20 20 20 32 2e 20 4e 6f 20 6c 6f 63 6b 69 6e  *   2. No lockin
ca050 67 2c 0a 2a 2a 20 20 20 33 2e 20 44 6f 74 2d 66  g,.**   3. Dot-f
ca060 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20  ile locking,.** 
ca070 20 20 34 2e 20 66 6c 6f 63 6b 28 29 20 6c 6f 63    4. flock() loc
ca080 6b 69 6e 67 2c 0a 2a 2a 20 20 20 35 2e 20 41 46  king,.**   5. AF
ca090 50 20 6c 6f 63 6b 69 6e 67 20 28 4f 53 58 20 6f  P locking (OSX o
ca0a0 6e 6c 79 29 2c 0a 2a 2a 20 20 20 36 2e 20 4e 61  nly),.**   6. Na
ca0b0 6d 65 64 20 50 4f 53 49 58 20 73 65 6d 61 70 68  med POSIX semaph
ca0c0 6f 72 65 73 20 28 56 58 57 6f 72 6b 73 20 6f 6e  ores (VXWorks on
ca0d0 6c 79 29 2c 0a 2a 2a 20 20 20 37 2e 20 70 72 6f  ly),.**   7. pro
ca0e0 78 79 20 6c 6f 63 6b 69 6e 67 2e 20 28 4f 53 58  xy locking. (OSX
ca0f0 20 6f 6e 6c 79 29 0a 2a 2a 0a 2a 2a 20 53 74 79   only).**.** Sty
ca100 6c 65 73 20 34 2c 20 35 2c 20 61 6e 64 20 37 20  les 4, 5, and 7 
ca110 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62  are only availab
ca120 6c 65 20 6f 66 20 53 51 4c 49 54 45 5f 45 4e 41  le of SQLITE_ENA
ca130 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
ca140 45 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 20  E.** is defined 
ca150 74 6f 20 31 2e 20 20 54 68 65 20 53 51 4c 49 54  to 1.  The SQLIT
ca160 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
ca170 5f 53 54 59 4c 45 20 61 6c 73 6f 20 65 6e 61 62  _STYLE also enab
ca180 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a  les automatic.**
ca190 20 73 65 6c 65 63 74 69 6f 6e 20 6f 66 20 74 68   selection of th
ca1a0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f  e appropriate lo
ca1b0 63 6b 69 6e 67 20 73 74 79 6c 65 20 62 61 73 65  cking style base
ca1c0 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 73 79 73  d on the filesys
ca1d0 74 65 6d 0a 2a 2a 20 77 68 65 72 65 20 74 68 65  tem.** where the
ca1e0 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63   database is loc
ca1f0 61 74 65 64 2e 20 20 0a 2a 2f 0a 23 69 66 20 21  ated.  .*/.#if !
ca200 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
ca210 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
ca220 59 4c 45 29 0a 23 20 20 69 66 20 64 65 66 69 6e  YLE).#  if defin
ca230 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 20  ed(__APPLE__).# 
ca240 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
ca250 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
ca260 53 54 59 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a  STYLE 1.#  else.
ca270 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  #    define SQLI
ca280 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
ca290 47 5f 53 54 59 4c 45 20 30 0a 23 20 20 65 6e 64  G_STYLE 0.#  end
ca2a0 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  if.#endif../*.**
ca2b0 20 44 65 66 69 6e 65 20 74 68 65 20 4f 53 5f 56   Define the OS_V
ca2c0 58 57 4f 52 4b 53 20 70 72 65 2d 70 72 6f 63 65  XWORKS pre-proce
ca2d0 73 73 6f 72 20 6d 61 63 72 6f 20 74 6f 20 31 20  ssor macro to 1 
ca2e0 69 66 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 0a  if building on .
ca2f0 2a 2a 20 76 78 77 6f 72 6b 73 2c 20 6f 72 20 30  ** vxworks, or 0
ca300 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 23   otherwise..*/.#
ca310 69 66 6e 64 65 66 20 4f 53 5f 56 58 57 4f 52 4b  ifndef OS_VXWORK
ca320 53 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28  S.#  if defined(
ca330 5f 5f 52 54 50 5f 5f 29 20 7c 7c 20 64 65 66 69  __RTP__) || defi
ca340 6e 65 64 28 5f 57 52 53 5f 4b 45 52 4e 45 4c 29  ned(_WRS_KERNEL)
ca350 0a 23 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f  .#    define OS_
ca360 56 58 57 4f 52 4b 53 20 31 0a 23 20 20 65 6c 73  VXWORKS 1.#  els
ca370 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 4f 53  e.#    define OS
ca380 5f 56 58 57 4f 52 4b 53 20 30 0a 23 20 20 65 6e  _VXWORKS 0.#  en
ca390 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  dif.#endif../*.*
ca3a0 2a 20 54 68 65 73 65 20 23 64 65 66 69 6e 65 73  * These #defines
ca3b0 20 73 68 6f 75 6c 64 20 65 6e 61 62 6c 65 20 3e   should enable >
ca3c0 32 47 42 20 66 69 6c 65 20 73 75 70 70 6f 72 74  2GB file support
ca3d0 20 6f 6e 20 50 6f 73 69 78 20 69 66 20 74 68 65   on Posix if the
ca3e0 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f  .** underlying o
ca3f0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
ca400 73 75 70 70 6f 72 74 73 20 69 74 2e 20 20 49 66  supports it.  If
ca410 20 74 68 65 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a   the OS lacks.**
ca420 20 6c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70   large file supp
ca430 6f 72 74 2c 20 74 68 65 73 65 20 73 68 6f 75 6c  ort, these shoul
ca440 64 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a  d be no-ops..**.
ca450 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65 20 73 75  ** Large file su
ca460 70 70 6f 72 74 20 63 61 6e 20 62 65 20 64 69 73  pport can be dis
ca470 61 62 6c 65 64 20 75 73 69 6e 67 20 74 68 65 20  abled using the 
ca480 2d 44 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  -DSQLITE_DISABLE
ca490 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a 2a 20 6f  _LFS switch.** o
ca4a0 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63  n the compiler c
ca4b0 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20 20 54 68  ommand line.  Th
ca4c0 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  is is necessary 
ca4d0 69 66 20 79 6f 75 20 61 72 65 20 63 6f 6d 70 69  if you are compi
ca4e0 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20 72 65 63  ling.** on a rec
ca4f0 65 6e 74 20 6d 61 63 68 69 6e 65 20 28 65 78 3a  ent machine (ex:
ca500 20 52 65 64 48 61 74 20 37 2e 32 29 20 62 75 74   RedHat 7.2) but
ca510 20 79 6f 75 20 77 61 6e 74 20 79 6f 75 72 20 63   you want your c
ca520 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f  ode to work.** o
ca530 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61 63 68 69  n an older machi
ca540 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 36  ne (ex: RedHat 6
ca550 2e 30 29 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d  .0).  If you com
ca560 70 69 6c 65 20 6f 6e 20 52 65 64 48 61 74 20 37  pile on RedHat 7
ca570 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68  .2.** without th
ca580 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69  is option, LFS i
ca590 73 20 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c  s enable.  But L
ca5a0 46 53 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  FS does not exis
ca5b0 74 20 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a  t in the kernel.
ca5c0 2a 2a 20 69 6e 20 52 65 64 48 61 74 20 36 2e 30  ** in RedHat 6.0
ca5d0 2c 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f  , so the code wo
ca5e0 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65  n't work.  Hence
ca5f0 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69  , for maximum bi
ca600 6e 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c  nary.** portabil
ca610 69 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f  ity you should o
ca620 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 54  mit LFS..**.** T
ca630 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72 61  he previous para
ca640 67 72 61 70 68 20 77 61 73 20 77 72 69 74 74 65  graph was writte
ca650 6e 20 69 6e 20 32 30 30 35 2e 20 20 28 54 68 69  n in 2005.  (Thi
ca660 73 20 70 61 72 61 67 72 61 70 68 20 69 73 20 77  s paragraph is w
ca670 72 69 74 74 65 6e 0a 2a 2a 20 6f 6e 20 32 30 30  ritten.** on 200
ca680 38 2d 31 31 2d 32 38 2e 29 20 54 68 65 73 65 20  8-11-28.) These 
ca690 64 61 79 73 2c 20 61 6c 6c 20 4c 69 6e 75 78 20  days, all Linux 
ca6a0 6b 65 72 6e 65 6c 73 20 73 75 70 70 6f 72 74 20  kernels support 
ca6b0 6c 61 72 67 65 20 66 69 6c 65 73 2c 20 73 6f 0a  large files, so.
ca6c0 2a 2a 20 79 6f 75 20 73 68 6f 75 6c 64 20 70 72  ** you should pr
ca6d0 6f 62 61 62 6c 79 20 6c 65 61 76 65 20 4c 46 53  obably leave LFS
ca6e0 20 65 6e 61 62 6c 65 64 2e 20 20 42 75 74 20 73   enabled.  But s
ca6f0 6f 6d 65 20 65 6d 62 65 64 64 65 64 20 70 6c 61  ome embedded pla
ca700 74 66 6f 72 6d 73 20 6d 69 67 68 74 0a 2a 2a 20  tforms might.** 
ca710 6c 61 63 6b 20 4c 46 53 20 69 6e 20 77 68 69 63  lack LFS in whic
ca720 68 20 63 61 73 65 20 74 68 65 20 53 51 4c 49 54  h case the SQLIT
ca730 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 6d 61  E_DISABLE_LFS ma
ca740 63 72 6f 20 6d 69 67 68 74 20 73 74 69 6c 6c 20  cro might still 
ca750 62 65 20 75 73 65 66 75 6c 2e 0a 2a 2f 0a 23 69  be useful..*/.#i
ca760 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53  fndef SQLITE_DIS
ca770 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e  ABLE_LFS.# defin
ca780 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20  e _LARGE_FILE   
ca790 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f      1.# ifndef _
ca7a0 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53  FILE_OFFSET_BITS
ca7b0 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c  .#   define _FIL
ca7c0 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34  E_OFFSET_BITS 64
ca7d0 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e  .# endif.# defin
ca7e0 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55  e _LARGEFILE_SOU
ca7f0 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  RCE 1.#endif../*
ca800 0a 2a 2a 20 73 74 61 6e 64 61 72 64 20 69 6e 63  .** standard inc
ca810 6c 75 64 65 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23  lude files..*/.#
ca820 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 79 70  include <sys/typ
ca830 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  es.h>.#include <
ca840 73 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63  sys/stat.h>.#inc
ca850 6c 75 64 65 20 3c 66 63 6e 74 6c 2e 68 3e 0a 23  lude <fcntl.h>.#
ca860 69 6e 63 6c 75 64 65 20 3c 75 6e 69 73 74 64 2e  include <unistd.
ca870 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  h>.#include <sys
ca880 2f 74 69 6d 65 2e 68 3e 0a 23 69 6e 63 6c 75 64  /time.h>.#includ
ca890 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 0a 23 69 66  e <errno.h>..#if
ca8a0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
ca8b0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 23 20 69  OCKING_STYLE.# i
ca8c0 6e 63 6c 75 64 65 20 3c 73 79 73 2f 69 6f 63 74  nclude <sys/ioct
ca8d0 6c 2e 68 3e 0a 23 20 69 66 20 4f 53 5f 56 58 57  l.h>.# if OS_VXW
ca8e0 4f 52 4b 53 0a 23 20 20 69 6e 63 6c 75 64 65 20  ORKS.#  include 
ca8f0 3c 73 65 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20  <semaphore.h>.# 
ca900 20 69 6e 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73   include <limits
ca910 2e 68 3e 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e  .h>.# else.#  in
ca920 63 6c 75 64 65 20 3c 73 79 73 2f 66 69 6c 65 2e  clude <sys/file.
ca930 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73  h>.#  include <s
ca940 79 73 2f 70 61 72 61 6d 2e 68 3e 0a 23 20 20 69  ys/param.h>.#  i
ca950 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6f 75 6e  nclude <sys/moun
ca960 74 2e 68 3e 0a 23 20 65 6e 64 69 66 0a 23 65 6e  t.h>.# endif.#en
ca970 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
ca980 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
ca990 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  LE */../*.** If 
ca9a0 77 65 20 61 72 65 20 74 6f 20 62 65 20 74 68 72  we are to be thr
ca9b0 65 61 64 2d 73 61 66 65 2c 20 69 6e 63 6c 75 64  ead-safe, includ
ca9c0 65 20 74 68 65 20 70 74 68 72 65 61 64 73 20 68  e the pthreads h
ca9d0 65 61 64 65 72 20 61 6e 64 20 64 65 66 69 6e 65  eader and define
ca9e0 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 55  .** the SQLITE_U
ca9f0 4e 49 58 5f 54 48 52 45 41 44 53 20 6d 61 63 72  NIX_THREADS macr
caa00 6f 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  o..*/.#if SQLITE
caa10 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65  _THREADSAFE.# de
caa20 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 4e 49 58  fine SQLITE_UNIX
caa30 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69  _THREADS 1.#endi
caa40 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74  f../*.** Default
caa50 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65   permissions whe
caa60 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77  n creating a new
caa70 20 66 69 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66   file.*/.#ifndef
caa80 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
caa90 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53  FILE_PERMISSIONS
caaa0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
caab0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45  _DEFAULT_FILE_PE
caac0 52 4d 49 53 53 49 4f 4e 53 20 30 36 34 34 0a 23  RMISSIONS 0644.#
caad0 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 2a 20 44 65  endif../*. ** De
caae0 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e  fault permission
caaf0 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20  s when creating 
cab00 61 75 74 6f 20 70 72 6f 78 79 20 64 69 72 0a 20  auto proxy dir. 
cab10 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
cab20 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44  E_DEFAULT_PROXYD
cab30 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23  IR_PERMISSIONS.#
cab40 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
cab50 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f  EFAULT_PROXYDIR_
cab60 50 45 52 4d 49 53 53 49 4f 4e 53 20 30 37 35 35  PERMISSIONS 0755
cab70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
cab80 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64  aximum supported
cab90 20 70 61 74 68 2d 6c 65 6e 67 74 68 2e 0a 2a 2f   path-length..*/
caba0 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 50 41 54  .#define MAX_PAT
cabb0 48 4e 41 4d 45 20 35 31 32 0a 0a 2f 2a 0a 2a 2a  HNAME 512../*.**
cabc0 20 4f 6e 6c 79 20 73 65 74 20 74 68 65 20 6c 61   Only set the la
cabd0 73 74 45 72 72 6e 6f 20 69 66 20 74 68 65 20 65  stErrno if the e
cabe0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 61 20 72  rror code is a r
cabf0 65 61 6c 20 65 72 72 6f 72 20 61 6e 64 20 6e 6f  eal error and no
cac00 74 20 0a 2a 2a 20 61 20 6e 6f 72 6d 61 6c 20 65  t .** a normal e
cac10 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 20 63  xpected return c
cac20 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 42 55  ode of SQLITE_BU
cac30 53 59 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 0a  SY or SQLITE_OK.
cac40 2a 2f 0a 23 64 65 66 69 6e 65 20 49 53 5f 4c 4f  */.#define IS_LO
cac50 43 4b 5f 45 52 52 4f 52 28 78 29 20 20 28 28 78  CK_ERROR(x)  ((x
cac60 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 20 26   != SQLITE_OK) &
cac70 26 20 28 78 20 21 3d 20 53 51 4c 49 54 45 5f 42  & (x != SQLITE_B
cac80 55 53 59 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  USY)).../*.** Th
cac90 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63  e unixFile struc
caca0 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73  ture is subclass
cacb0 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
cacc0 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65   specific to the
cacd0 20 75 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70   unix.** VFS imp
cace0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f  lementations..*/
cacf0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
cad00 75 6e 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c  unixFile unixFil
cad10 65 3b 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69  e;.struct unixFi
cad20 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  le {.  sqlite3_i
cad30 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20  o_methods const 
cad40 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c  *pMethod;  /* Al
cad50 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20 65  ways the first e
cad60 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  ntry */.  struct
cad70 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f   unixOpenCnt *pO
cad80 70 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e  pen;       /* In
cad90 66 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65  fo about all ope
cada0 6e 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69  n fd's on this i
cadb0 6e 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  node */.  struct
cadc0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70   unixLockInfo *p
cadd0 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e  Lock;      /* In
cade0 66 6f 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f  fo about locks o
cadf0 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a  n this inode */.
cae00 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20    int h;        
cae10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cae20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64     /* The file d
cae30 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69  escriptor */.  i
cae40 6e 74 20 64 69 72 66 64 3b 20 20 20 20 20 20 20  nt dirfd;       
cae50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cae60 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74  /* File descript
cae70 6f 72 20 66 6f 72 20 74 68 65 20 64 69 72 65 63  or for the direc
cae80 74 6f 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  tory */.  unsign
cae90 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65  ed char locktype
caea0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
caeb0 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68  e type of lock h
caec0 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a  eld on this fd *
caed0 2f 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e  /.  int lastErrn
caee0 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o;              
caef0 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78       /* The unix
caf00 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20   errno from the 
caf10 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a  last I/O error *
caf20 2f 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e  /.  void *lockin
caf30 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20  gContext;       
caf40 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20       /* Locking 
caf50 73 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 73  style specific s
caf60 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  tate */.  int op
caf70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  enFlags;        
caf80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
caf90 65 20 66 6c 61 67 73 20 73 70 65 63 69 66 69 65  e flags specifie
cafa0 64 20 61 74 20 6f 70 65 6e 20 2a 2f 0a 23 69 66  d at open */.#if
cafb0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
cafc0 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  FE && defined(__
cafd0 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68 72 65  linux__).  pthre
cafe0 61 64 5f 74 20 74 69 64 3b 20 20 20 20 20 20 20  ad_t tid;       
caff0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
cb000 68 65 20 74 68 72 65 61 64 20 74 68 61 74 20 22  he thread that "
cb010 6f 77 6e 73 22 20 74 68 69 73 20 75 6e 69 78 46  owns" this unixF
cb020 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ile */.#endif.#i
cb030 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69  f OS_VXWORKS.  i
cb040 6e 74 20 69 73 44 65 6c 65 74 65 3b 20 20 20 20  nt isDelete;    
cb050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb060 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f  /* Delete on clo
cb070 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20  se if true */.  
cb080 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69  struct vxworksFi
cb090 6c 65 49 64 20 2a 70 49 64 3b 20 20 20 20 20 20  leId *pId;      
cb0a0 20 2f 2a 20 55 6e 69 71 75 65 20 66 69 6c 65 20   /* Unique file 
cb0b0 49 44 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  ID */.#endif.#if
cb0c0 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
cb0d0 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20   The next group 
cb0e0 6f 66 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  of variables are
cb0f0 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20 77   used to track w
cb100 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
cb110 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  e.  ** transacti
cb120 6f 6e 20 63 6f 75 6e 74 65 72 20 69 6e 20 62 79  on counter in by
cb130 74 65 73 20 32 34 2d 32 37 20 6f 66 20 64 61 74  tes 24-27 of dat
cb140 61 62 61 73 65 20 66 69 6c 65 73 20 61 72 65 20  abase files are 
cb150 75 70 64 61 74 65 64 0a 20 20 2a 2a 20 77 68 65  updated.  ** whe
cb160 6e 65 76 65 72 20 61 6e 79 20 70 61 72 74 20 6f  never any part o
cb170 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
cb180 68 61 6e 67 65 73 2e 20 20 41 6e 20 61 73 73 65  hanges.  An asse
cb190 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c  rtion fault will
cb1a0 0a 20 20 2a 2a 20 6f 63 63 75 72 20 69 66 20 61  .  ** occur if a
cb1b0 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64   file is updated
cb1c0 20 77 69 74 68 6f 75 74 20 61 6c 73 6f 20 75 70   without also up
cb1d0 64 61 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73  dating the trans
cb1e0 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f 75 6e  action.  ** coun
cb1f0 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74 20  ter.  This test 
cb200 69 73 20 6d 61 64 65 20 74 6f 20 61 76 6f 69 64  is made to avoid
cb210 20 6e 65 77 20 70 72 6f 62 6c 65 6d 73 20 73 69   new problems si
cb220 6d 69 6c 61 72 20 74 6f 20 74 68 65 0a 20 20 2a  milar to the.  *
cb230 2a 20 6f 6e 65 20 64 65 73 63 72 69 62 65 64 20  * one described 
cb240 62 79 20 74 69 63 6b 65 74 20 23 33 35 38 34 2e  by ticket #3584.
cb250 20 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65   .  */.  unsigne
cb260 64 20 63 68 61 72 20 74 72 61 6e 73 43 6e 74 72  d char transCntr
cb270 43 68 6e 67 3b 20 20 20 2f 2a 20 54 72 75 65 20  Chng;   /* True 
cb280 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  if the transacti
cb290 6f 6e 20 63 6f 75 6e 74 65 72 20 63 68 61 6e 67  on counter chang
cb2a0 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ed */.  unsigned
cb2b0 20 63 68 61 72 20 64 62 55 70 64 61 74 65 3b 20   char dbUpdate; 
cb2c0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
cb2d0 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 64 61  f any part of da
cb2e0 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e  tabase file chan
cb2f0 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ged */.  unsigne
cb300 64 20 63 68 61 72 20 69 6e 4e 6f 72 6d 61 6c 57  d char inNormalW
cb310 72 69 74 65 3b 20 20 20 2f 2a 20 54 72 75 65 20  rite;   /* True 
cb320 69 66 20 69 6e 20 61 20 6e 6f 72 6d 61 6c 20 77  if in a normal w
cb330 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a  rite operation *
cb340 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  /.#endif.#ifdef 
cb350 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a  SQLITE_TEST.  /*
cb360 20 49 6e 20 74 65 73 74 20 6d 6f 64 65 2c 20 69   In test mode, i
cb370 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65  ncrease the size
cb380 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
cb390 72 65 20 61 20 62 69 74 20 73 6f 20 74 68 61 74  re a bit so that
cb3a0 20 0a 20 20 2a 2a 20 69 74 20 69 73 20 6c 61 72   .  ** it is lar
cb3b0 67 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 72  ger than the str
cb3c0 75 63 74 20 43 72 61 73 68 46 69 6c 65 20 64 65  uct CrashFile de
cb3d0 66 69 6e 65 64 20 69 6e 20 74 65 73 74 36 2e 63  fined in test6.c
cb3e0 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 61 50  ..  */.  char aP
cb3f0 61 64 64 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e 64  adding[32];.#end
cb400 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  if.};../*.** Inc
cb410 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 69  lude code that i
cb420 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20  s common to all 
cb430 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a  os_*.c files.*/.
cb440 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
cb450 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f  Include os_commo
cb460 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  n.h in the middl
cb470 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 2a  e of os_unix.c *
cb480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
cb490 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
cb4a0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f  Begin file os_co
cb4b0 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
cb4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
cb4e0 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
cb4f0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
cb500 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
cb510 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
cb520 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
cb530 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
cb540 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
cb550 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
cb560 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
cb570 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
cb580 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
cb590 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
cb5a0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
cb5b0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
cb5c0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
cb5d0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
cb5e0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
cb5f0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
cb600 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
cb610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb650 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
cb660 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d   file contains m
cb670 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74  acros and a litt
cb680 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74  le bit of code t
cb690 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f  hat is common to
cb6a0 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70  .** all of the p
cb6b0 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63  latform-specific
cb6c0 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20   files (os_*.c) 
cb6d0 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64  and is #included
cb6e0 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66   into those.** f
cb6f0 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  iles..**.** This
cb700 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20   file should be 
cb710 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65  #included by the
cb720 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e   os_*.c files on
cb730 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61  ly.  It is not a
cb740 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70  .** general purp
cb750 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e  ose header file.
cb760 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63  .**.** $Id: os_c
cb770 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 37 20 32  ommon.h,v 1.37 2
cb780 30 30 38 2f 30 35 2f 32 39 20 32 30 3a 32 32 3a  008/05/29 20:22:
cb790 33 37 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  37 shane Exp $.*
cb7a0 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f  /.#ifndef _OS_CO
cb7b0 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20  MMON_H_.#define 
cb7c0 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f  _OS_COMMON_H_../
cb7d0 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77  *.** At least tw
cb7e0 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 70  o bugs have slip
cb7f0 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 77  ped in because w
cb800 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45  e changed the ME
cb810 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61  MORY_DEBUG.** ma
cb820 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45  cro to SQLITE_DE
cb830 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64  BUG and some old
cb840 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76  er makefiles hav
cb850 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74  e not yet made t
cb860 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54  he.** switch.  T
cb870 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64  he following cod
cb880 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74  e should catch t
cb890 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63  his problem at c
cb8a0 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a  ompile-time..*/.
cb8b0 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45  #ifdef MEMORY_DE
cb8c0 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65  BUG.# error "The
cb8d0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61   MEMORY_DEBUG ma
cb8e0 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e  cro is obsolete.
cb8f0 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42    Use SQLITE_DEB
cb900 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e  UG instead.".#en
cb910 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20 57 68 65 6e  dif.../*. * When
cb920 20 74 65 73 74 69 6e 67 2c 20 74 68 69 73 20 67   testing, this g
cb930 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 73  lobal variable s
cb940 74 6f 72 65 73 20 74 68 65 20 6c 6f 63 61 74 69  tores the locati
cb950 6f 6e 20 6f 66 20 74 68 65 0a 20 2a 20 70 65 6e  on of the. * pen
cb960 64 69 6e 67 2d 62 79 74 65 20 69 6e 20 74 68 65  ding-byte in the
cb970 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
cb980 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
cb990 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
cb9a0 49 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 73  I unsigned int s
cb9b0 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62  qlite3_pending_b
cb9c0 79 74 65 20 3d 20 30 78 34 30 30 30 30 30 30 30  yte = 0x40000000
cb9d0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
cb9e0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51   SQLITE_DEBUG.SQ
cb9f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
cba00 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20   sqlite3OSTrace 
cba10 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54  = 0;.#define OST
cba20 52 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20  RACE1(X)        
cba30 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72   if( sqlite3OSTr
cba40 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
cba50 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66  ugPrintf(X).#def
cba60 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59  ine OSTRACE2(X,Y
cba70 29 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  )       if( sqli
cba80 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
cba90 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
cbaa0 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  X,Y).#define OST
cbab0 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20  RACE3(X,Y,Z)    
cbac0 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72   if( sqlite3OSTr
cbad0 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
cbae0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a  ugPrintf(X,Y,Z).
cbaf0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34  #define OSTRACE4
cbb00 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20  (X,Y,Z,A)   if( 
cbb10 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
cbb20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
cbb30 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65  ntf(X,Y,Z,A).#de
cbb40 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c  fine OSTRACE5(X,
cbb50 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c  Y,Z,A,B) if( sql
cbb60 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
cbb70 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
cbb80 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66  (X,Y,Z,A,B).#def
cbb90 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59  ine OSTRACE6(X,Y
cbba0 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20  ,Z,A,B,C) \.    
cbbb0 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63  if(sqlite3OSTrac
cbbc0 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  e) sqlite3DebugP
cbbd0 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c  rintf(X,Y,Z,A,B,
cbbe0 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  C).#define OSTRA
cbbf0 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c  CE7(X,Y,Z,A,B,C,
cbc00 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69  D) \.    if(sqli
cbc10 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69  te3OSTrace) sqli
cbc20 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
cbc30 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65  ,Y,Z,A,B,C,D).#e
cbc40 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  lse.#define OSTR
cbc50 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20  ACE1(X).#define 
cbc60 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64  OSTRACE2(X,Y).#d
cbc70 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58  efine OSTRACE3(X
cbc80 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Y,Z).#define OS
cbc90 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a  TRACE4(X,Y,Z,A).
cbca0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35  #define OSTRACE5
cbcb0 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66  (X,Y,Z,A,B).#def
cbcc0 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59  ine OSTRACE6(X,Y
cbcd0 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e  ,Z,A,B,C).#defin
cbce0 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a  e OSTRACE7(X,Y,Z
cbcf0 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66  ,A,B,C,D).#endif
cbd00 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
cbd10 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74  or performance t
cbd20 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c  racing.  Normall
cbd30 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f  y turned off.  O
cbd40 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20  nly works.** on 
cbd50 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a  i486 hardware..*
cbd60 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
cbd70 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43  PERFORMANCE_TRAC
cbd80 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65  E../* .** hwtime
cbd90 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69  .h contains inli
cbda0 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64  ne assembler cod
cbdb0 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69  e for implementi
cbdc0 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66  ng .** high-perf
cbdd0 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72  ormance timing r
cbde0 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a  outines..*/./***
cbdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
cbe00 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20  ude hwtime.h in 
cbe10 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73  the middle of os
cbe20 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a  _common.h ******
cbe30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
cbe40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
cbe50 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20  n file hwtime.h 
cbe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
cbe90 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a  * 2008 May 27.**
cbea0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
cbeb0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
cbec0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
cbed0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
cbee0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
cbef0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
cbf00 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
cbf10 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
cbf20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
cbf30 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
cbf40 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
cbf50 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
cbf60 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
cbf70 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
cbf80 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
cbf90 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
cbfa0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
cbfb0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
cbfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc000 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
cc010 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e  e contains inlin
cc020 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72  e asm code for r
cc030 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d  etrieving "high-
cc040 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20  performance".** 
cc050 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36  counters for x86
cc060 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a   class CPUs..**.
cc070 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68  ** $Id: hwtime.h
cc080 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30  ,v 1.3 2008/08/0
cc090 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65  1 14:33:15 shane
cc0a0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
cc0b0 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65  f _HWTIME_H_.#de
cc0c0 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a  fine _HWTIME_H_.
cc0d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
cc0e0 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c  wing routine onl
cc0f0 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69  y works on penti
cc100 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77  um-class (or new
cc110 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a  er) processors..
cc120 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52  ** It uses the R
cc130 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72  DTSC opcode to r
cc140 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f  ead the cycle co
cc150 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66  unt value out of
cc160 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f   the.** processo
cc170 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68  r and returns th
cc180 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20  at value.  This 
cc190 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20  can be used for 
cc1a0 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66  high-res.** prof
cc1b0 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64  iling..*/.#if (d
cc1c0 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
cc1d0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43   || defined(_MSC
cc1e0 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20  _VER)) && \.    
cc1f0 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29    (defined(i386)
cc200 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33   || defined(__i3
cc210 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  86__) || defined
cc220 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69  (_M_IX86))..  #i
cc230 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43  f defined(__GNUC
cc240 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  __)..  __inline_
cc250 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
cc260 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
cc270 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e  id){.     unsign
cc280 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20  ed int lo, hi;. 
cc290 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f      __asm__ __vo
cc2a0 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63  latile__ ("rdtsc
cc2b0 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22  " : "=a" (lo), "
cc2c0 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20  =d" (hi));.     
cc2d0 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75  return (sqlite_u
cc2e0 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c  int64)hi << 32 |
cc2f0 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69   lo;.  }..  #eli
cc300 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56  f defined(_MSC_V
cc310 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65  ER)..  __declspe
cc320 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e  c(naked) __inlin
cc330 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  e sqlite_uint64 
cc340 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48  __cdecl sqlite3H
cc350 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
cc360 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20    __asm {.      
cc370 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20    rdtsc.        
cc380 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75  ret       ; retu
cc390 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a  rn value at EDX:
cc3a0 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a  EAX.     }.  }..
cc3b0 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20    #endif..#elif 
cc3c0 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
cc3d0 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  _) && defined(__
cc3e0 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f  x86_64__))..  __
cc3f0 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f  inline__ sqlite_
cc400 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
cc410 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
cc420 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
cc430 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d  val;.      __asm
cc440 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20  __ __volatile__ 
cc450 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20  ("rdtsc" : "=A" 
cc460 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65  (val));.      re
cc470 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a  turn val;.  }. .
cc480 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f  #elif (defined(_
cc490 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69  _GNUC__) && defi
cc4a0 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20  ned(__ppc__)).. 
cc4b0 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
cc4c0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
cc4d0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
cc4e0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
cc4f0 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a  ng long retval;.
cc500 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c        unsigned l
cc510 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20  ong junk;.      
cc520 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69  __asm__ __volati
cc530 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20  le__ ("\n\.     
cc540 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74       1:      mft
cc550 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20  bu   %1\n\.     
cc560 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74               mft
cc570 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20  b    %L0\n\.    
cc580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66                mf
cc590 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20  tbu   %0\n\.    
cc5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d                cm
cc5b0 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20  pw    %0,%1\n\. 
cc5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cc5d0 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20   bne     1b".   
cc5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a                 :
cc5f0 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20   "=r" (retval), 
cc600 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20  "=r" (junk));.  
cc610 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61      return retva
cc620 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20  l;.  }..#else.. 
cc630 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70   #error Need imp
cc640 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73  lementation of s
cc650 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66  qlite3Hwtime() f
cc660 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d  or your platform
cc670 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20  ...  /*.  ** To 
cc680 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20  compile without 
cc690 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c  implementing sql
cc6a0 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72  ite3Hwtime() for
cc6b0 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a   your platform,.
cc6c0 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d    ** you can rem
cc6d0 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65  ove the above #e
cc6e0 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65  rror and use the
cc6f0 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20   following.  ** 
cc700 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20  stub function.  
cc710 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69  You will lose ti
cc720 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72  ming support for
cc730 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68   many.  ** of th
cc740 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20  e debugging and 
cc750 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65  testing utilitie
cc760 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64  s, but it should
cc770 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63   at.  ** least c
cc780 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a  ompile and run..
cc790 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56    */.SQLITE_PRIV
cc7a0 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e  ATE   sqlite_uin
cc7b0 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
cc7c0 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  e(void){ return 
cc7d0 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29  ((sqlite_uint64)
cc7e0 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23  0); }..#endif..#
cc7f0 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
cc800 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f  d(_HWTIME_H_) */
cc810 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
cc820 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e  * End of hwtime.
cc830 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
cc840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc860 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
cc870 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
cc880 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
cc890 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  n os_common.h **
cc8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc8b0 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  /..static sqlite
cc8c0 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b  _uint64 g_start;
cc8d0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75  .static sqlite_u
cc8e0 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b  int64 g_elapsed;
cc8f0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53  .#define TIMER_S
cc900 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61  TART       g_sta
cc910 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65  rt=sqlite3Hwtime
cc920 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  ().#define TIMER
cc930 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65  _END         g_e
cc940 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77  lapsed=sqlite3Hw
cc950 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23  time()-g_start.#
cc960 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41  define TIMER_ELA
cc970 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73  PSED     g_elaps
cc980 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  ed.#else.#define
cc990 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65   TIMER_START.#de
cc9a0 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23  fine TIMER_END.#
cc9b0 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41  define TIMER_ELA
cc9c0 50 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74  PSED     ((sqlit
cc9d0 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64  e_uint64)0).#end
cc9e0 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  if../*.** If we 
cc9f0 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65  compile with the
cca00 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63   SQLITE_TEST mac
cca10 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  ro set, then the
cca20 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
cca30 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c  .** of code will
cca40 20 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69   give us the abi
cca50 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65  lity to simulate
cca60 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f   a disk I/O erro
cca70 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75  r.  This.** is u
cca80 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
cca90 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79  the I/O recovery
ccaa0 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65   logic..*/.#ifde
ccab0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
ccac0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
ccad0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
ccae0 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
ccaf0 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
ccb00 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20  r of I/O Errors 
ccb10 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
ccb20 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
ccb30 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20  or_hardhit = 0; 
ccb40 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
ccb50 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65   of non-benign e
ccb60 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f  rrors */.SQLITE_
ccb70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
ccb80 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67  io_error_pending
ccb90 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
ccba0 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69  Count down to fi
ccbb0 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f  rst I/O error */
ccbc0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
ccbd0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
ccbe0 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20  _persist = 0;   
ccbf0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
ccc00 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69  I/O errors persi
ccc10 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  st */.SQLITE_API
ccc20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
ccc30 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30  error_benign = 0
ccc40 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
ccc50 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20  e if errors are 
ccc60 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45  benign */.SQLITE
ccc70 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
ccc80 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
ccc90 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50  g = 0;.SQLITE_AP
ccca0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
cccb0 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66  skfull = 0;.#def
cccc0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
cccd0 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c  rorBenign(X) sql
ccce0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65  ite3_io_error_be
cccf0 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65  nign=(X).#define
ccd00 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
ccd10 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20  (CODE)  \.  if( 
ccd20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f  (sqlite3_io_erro
ccd30 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c  r_persist && sql
ccd40 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
ccd50 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73  t) \.       || s
ccd60 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
ccd70 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29  pending-- == 1 )
ccd80 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20    \.            
ccd90 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28    { local_ioerr(
ccda0 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69  ); CODE; }.stati
ccdb0 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65  c void local_ioe
ccdc0 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28  rr(){.  IOTRACE(
ccdd0 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20  ("IOERR\n"));.  
ccde0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
ccdf0 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73  _hit++;.  if( !s
cce00 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
cce10 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33  benign ) sqlite3
cce20 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69  _io_error_hardhi
cce30 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53  t++;.}.#define S
cce40 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45  imulateDiskfullE
cce50 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20  rror(CODE) \.   
cce60 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  if( sqlite3_disk
cce70 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20  full_pending ){ 
cce80 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  \.     if( sqlit
cce90 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
ccea0 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20  ing == 1 ){ \.  
cceb0 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72       local_ioerr
ccec0 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c  (); \.       sql
cced0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20  ite3_diskfull = 
ccee0 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69  1; \.       sqli
ccef0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
ccf00 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43   = 1; \.       C
ccf10 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73  ODE; \.     }els
ccf20 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69  e{ \.       sqli
ccf30 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
ccf40 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d  ding--; \.     }
ccf50 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64   \.   }.#else.#d
ccf60 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
ccf70 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23  ErrorBenign(X).#
ccf80 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
ccf90 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e  OError(A).#defin
ccfa0 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75  e SimulateDiskfu
ccfb0 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69  llError(A).#endi
ccfc0 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65  f../*.** When te
ccfd0 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f  sting, keep a co
ccfe0 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  unt of the numbe
ccff0 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e  r of open files.
cd000 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
cd010 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
cd020 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70  I int sqlite3_op
cd030 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20  en_file_count = 
cd040 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43  0;.#define OpenC
cd050 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74  ounter(X)  sqlit
cd060 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75  e3_open_file_cou
cd070 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64  nt+=(X).#else.#d
cd080 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65  efine OpenCounte
cd090 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  r(X).#endif..#en
cd0a0 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
cd0b0 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a  _OS_COMMON_H_) *
cd0c0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
cd0d0 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d  ** End of os_com
cd0e0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mon.h **********
cd0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd110 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
cd120 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
cd130 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
cd140 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a  in os_unix.c ***
cd150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd160 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65  */../*.** Define
cd170 20 76 61 72 69 6f 75 73 20 6d 61 63 72 6f 73 20   various macros 
cd180 74 68 61 74 20 61 72 65 20 6d 69 73 73 69 6e 67  that are missing
cd190 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73 74 65   from some syste
cd1a0 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f  ms..*/.#ifndef O
cd1b0 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66  _LARGEFILE.# def
cd1c0 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20  ine O_LARGEFILE 
cd1d0 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  0.#endif.#ifdef 
cd1e0 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c  SQLITE_DISABLE_L
cd1f0 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c 41 52  FS.# undef O_LAR
cd200 47 45 46 49 4c 45 0a 23 20 64 65 66 69 6e 65 20  GEFILE.# define 
cd210 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a 23 65  O_LARGEFILE 0.#e
cd220 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f 4e  ndif.#ifndef O_N
cd230 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69 6e 65  OFOLLOW.# define
cd240 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a 23 65   O_NOFOLLOW 0.#e
cd250 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f 42  ndif.#ifndef O_B
cd260 49 4e 41 52 59 0a 23 20 64 65 66 69 6e 65 20 4f  INARY.# define O
cd270 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e 64 69 66  _BINARY 0.#endif
cd280 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a 47 50  ../*.** The DJGP
cd290 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76 69 72  P compiler envir
cd2a0 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d 6f 73  onment looks mos
cd2b0 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c 20 62  tly like Unix, b
cd2c0 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73 20 74  ut it.** lacks t
cd2d0 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65  he fcntl() syste
cd2e0 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65 64 65  m call.  So rede
cd2f0 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 74 6f 20  fine fcntl() to 
cd300 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20  be something.** 
cd310 74 68 61 74 20 61 6c 77 61 79 73 20 73 75 63 63  that always succ
cd320 65 65 64 73 2e 20 20 54 68 69 73 20 6d 65 61 6e  eeds.  This mean
cd330 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67 20 64  s that locking d
cd340 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 75 6e  oes not occur un
cd350 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20 20 42  der.** DJGPP.  B
cd360 75 74 20 69 74 20 69 73 20 44 4f 53 20 2d 20 77  ut it is DOS - w
cd370 68 61 74 20 64 69 64 20 79 6f 75 20 65 78 70 65  hat did you expe
cd380 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f  ct?.*/.#ifdef __
cd390 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69 6e 65  DJGPP__.# define
cd3a0 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20 30 0a   fcntl(A,B,C) 0.
cd3b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
cd3c0 65 20 74 68 72 65 61 64 69 64 20 6d 61 63 72 6f  e threadid macro
cd3d0 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 68 65   resolves to the
cd3e0 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20 74 6f   thread-id or to
cd3f0 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a 2a   0.  Used for.**
cd400 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62   testing and deb
cd410 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ugging only..*/.
cd420 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
cd430 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20 74 68  DSAFE.#define th
cd440 72 65 61 64 69 64 20 70 74 68 72 65 61 64 5f 73  readid pthread_s
cd450 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65 66  elf().#else.#def
cd460 69 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a 23  ine threadid 0.#
cd470 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 48 65  endif.../*.** He
cd480 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74  lper functions t
cd490 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c  o obtain and rel
cd4a0 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c 6f 62  inquish the glob
cd4b0 61 6c 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61  al mutex..*/.sta
cd4c0 74 69 63 20 76 6f 69 64 20 75 6e 69 78 45 6e 74  tic void unixEnt
cd4d0 65 72 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20  erMutex(void){. 
cd4e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
cd4f0 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65  nter(sqlite3Mute
cd500 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
cd510 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
cd520 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  R));.}.static vo
cd530 69 64 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65  id unixLeaveMute
cd540 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74  x(void){.  sqlit
cd550 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
cd560 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
cd570 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
cd580 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d  ATIC_MASTER));.}
cd590 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
cd5a0 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c  _DEBUG./*.** Hel
cd5b0 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  per function for
cd5c0 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 74 72   printing out tr
cd5d0 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ace information 
cd5e0 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a  from debugging.*
cd5f0 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 69 73  * binaries. This
cd600 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 74 72   returns the str
cd610 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 69 6f  ing represetatio
cd620 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65  n of the supplie
cd630 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63  d.** integer loc
cd640 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69  k-type..*/.stati
cd650 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f  c const char *lo
cd660 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c  cktypeName(int l
cd670 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 69 74  ocktype){.  swit
cd680 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  ch( locktype ){.
cd690 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20    case NO_LOCK: 
cd6a0 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20  return "NONE";. 
cd6b0 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c 4f 43   case SHARED_LOC
cd6c0 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 52 45  K: return "SHARE
cd6d0 44 22 3b 0a 20 20 63 61 73 65 20 52 45 53 45 52  D";.  case RESER
cd6e0 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e  VED_LOCK: return
cd6f0 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20 20 63   "RESERVED";.  c
cd700 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  ase PENDING_LOCK
cd710 3a 20 72 65 74 75 72 6e 20 22 50 45 4e 44 49 4e  : return "PENDIN
cd720 47 22 3b 0a 20 20 63 61 73 65 20 45 58 43 4c 55  G";.  case EXCLU
cd730 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74 75 72  SIVE_LOCK: retur
cd740 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b 0a 20  n "EXCLUSIVE";. 
cd750 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45 52 52   }.  return "ERR
cd760 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  OR";.}.#endif..#
cd770 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c 4f 43  ifdef SQLITE_LOC
cd780 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20 50 72  K_TRACE./*.** Pr
cd790 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d 61 74  int out informat
cd7a0 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20 6c 6f  ion about all lo
cd7b0 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73  cking operations
cd7c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
cd7d0 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72  tine is used for
cd7e0 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67   troubleshooting
cd7f0 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74 69 74   locks on multit
cd800 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c 61 74 66  hreaded.** platf
cd810 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20 62 79  orms.  Enable by
cd820 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20   compiling with 
cd830 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c 4f 43  the -DSQLITE_LOC
cd840 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f 6d 6d 61  K_TRACE.** comma
cd850 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e 20 6f  nd-line option o
cd860 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20  n the compiler. 
cd870 20 54 68 69 73 20 63 6f 64 65 20 69 73 20 6e 6f   This code is no
cd880 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 6e 65 64  rmally.** turned
cd890 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   off..*/.static 
cd8a0 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 28 69 6e  int lockTrace(in
cd8b0 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c 20 73 74  t fd, int op, st
cd8c0 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 29 7b 0a  ruct flock *p){.
cd8d0 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 6d 65 2c    char *zOpName,
cd8e0 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e 74 20 73   *zType;.  int s
cd8f0 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 45 72 72  ;.  int savedErr
cd900 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 46 5f  no;.  if( op==F_
cd910 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f 70  GETLK ){.    zOp
cd920 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b 22 3b 0a  Name = "GETLK";.
cd930 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d    }else if( op==
cd940 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a  F_SETLK ){.    z
cd950 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 4c 4b 22  OpName = "SETLK"
cd960 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
cd970 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c   = fcntl(fd, op,
cd980 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   p);.    sqlite3
cd990 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e  DebugPrintf("fcn
cd9a0 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20 25 64  tl unknown %d %d
cd9b0 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70 2c 20   %d\n", fd, op, 
cd9c0 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73  s);.    return s
cd9d0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 6c  ;.  }.  if( p->l
cd9e0 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29  _type==F_RDLCK )
cd9f0 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 52  {.    zType = "R
cda00 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69  DLCK";.  }else i
cda10 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f  f( p->l_type==F_
cda20 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79  WRLCK ){.    zTy
cda30 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20  pe = "WRLCK";.  
cda40 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74  }else if( p->l_t
cda50 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a  ype==F_UNLCK ){.
cda60 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c      zType = "UNL
cda70 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  CK";.  }else{.  
cda80 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20    assert( 0 );. 
cda90 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e   }.  assert( p->
cdaa0 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 4b 5f 53  l_whence==SEEK_S
cdab0 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 63 6e 74  ET );.  s = fcnt
cdac0 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20  l(fd, op, p);.  
cdad0 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65 72 72  savedErrno = err
cdae0 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62  no;.  sqlite3Deb
cdaf0 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c 20  ugPrintf("fcntl 
cdb00 25 64 20 25 64 20 25 73 20 25 73 20 25 64 20 25  %d %d %s %s %d %
cdb10 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20  d %d %d\n",.    
cdb20 20 74 68 72 65 61 64 69 64 2c 20 66 64 2c 20 7a   threadid, fd, z
cdb30 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 2c 20 28  OpName, zType, (
cdb40 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 74 2c 20  int)p->l_start, 
cdb50 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e 2c 0a 20  (int)p->l_len,. 
cdb60 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c 5f 70 69      (int)p->l_pi
cdb70 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 73 3d 3d  d, s);.  if( s==
cdb80 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 5f 53 45  (-1) && op==F_SE
cdb90 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f 74 79 70  TLK && (p->l_typ
cdba0 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c 20 70 2d  e==F_RDLCK || p-
cdbb0 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b  >l_type==F_WRLCK
cdbc0 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  ) ){.    struct 
cdbd0 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 20 6c 32  flock l2;.    l2
cdbe0 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 6e 74 6c   = *p;.    fcntl
cdbf0 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c  (fd, F_GETLK, &l
cdc00 32 29 3b 0a 20 20 20 20 69 66 28 20 6c 32 2e 6c  2);.    if( l2.l
cdc10 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29  _type==F_RDLCK )
cdc20 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20  {.      zType = 
cdc30 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c  "RDLCK";.    }el
cdc40 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65  se if( l2.l_type
cdc50 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20  ==F_WRLCK ){.   
cdc60 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43     zType = "WRLC
cdc70 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  K";.    }else if
cdc80 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55  ( l2.l_type==F_U
cdc90 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54  NLCK ){.      zT
cdca0 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20  ype = "UNLCK";. 
cdcb0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
cdcc0 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20  assert( 0 );.   
cdcd0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65   }.    sqlite3De
cdce0 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c  bugPrintf("fcntl
cdcf0 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 6f 6e 3a  -failure-reason:
cdd00 20 25 73 20 25 64 20 25 64 20 25 64 5c 6e 22 2c   %s %d %d %d\n",
cdd10 0a 20 20 20 20 20 20 20 7a 54 79 70 65 2c 20 28  .       zType, (
cdd20 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 74 2c 20  int)l2.l_start, 
cdd30 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e 2c 20 28  (int)l2.l_len, (
cdd40 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 3b 0a 20  int)l2.l_pid);. 
cdd50 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 73 61 76   }.  errno = sav
cdd60 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 74 75 72  edErrno;.  retur
cdd70 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 66  n s;.}.#define f
cdd80 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 65 0a 23  cntl lockTrace.#
cdd90 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
cdda0 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f 0a 0a 0a  LOCK_TRACE */...
cddb0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
cddc0 69 6e 65 20 74 72 61 6e 73 6c 61 74 65 73 20 61  ine translates a
cddd0 20 73 74 61 6e 64 61 72 64 20 50 4f 53 49 58 20   standard POSIX 
cdde0 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e 74 6f 20  errno code into 
cddf0 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 75 73 65  something.** use
cde00 66 75 6c 20 74 6f 20 74 68 65 20 63 6c 69 65 6e  ful to the clien
cde10 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ts of the sqlite
cde20 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 53 70  3 functions.  Sp
cde30 65 63 69 66 69 63 61 6c 6c 79 2c 20 69 74 20 69  ecifically, it i
cde40 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 74 6f  s.** intended to
cde50 20 74 72 61 6e 73 6c 61 74 65 20 61 20 76 61 72   translate a var
cde60 69 65 74 79 20 6f 66 20 22 74 72 79 20 61 67 61  iety of "try aga
cde70 69 6e 22 20 65 72 72 6f 72 73 20 69 6e 74 6f 20  in" errors into 
cde80 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a 2a 20 61  SQLITE_BUSY.** a
cde90 6e 64 20 61 20 76 61 72 69 65 74 79 20 6f 66 20  nd a variety of 
cdea0 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 20 74 68  "please close th
cdeb0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
cdec0 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 20 69 6e  r NOW" errors in
cded0 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f  to .** SQLITE_IO
cdee0 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72 72 6f 72  ERR.** .** Error
cdef0 73 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c  s during initial
cdf00 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f 63 6b 73  ization of locks
cdf10 2c 20 6f 72 20 66 69 6c 65 20 73 79 73 74 65 6d  , or file system
cdf20 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6c 6f 63   support for loc
cdf30 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 68 61  ks,.** should ha
cdf40 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20 45 4e 4f  ndle ENOLCK, ENO
cdf50 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53 55 50 50  TSUP, EOPNOTSUPP
cdf60 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2f 0a   separately..*/.
cdf70 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
cdf80 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
cdf90 72 72 6f 72 28 69 6e 74 20 70 6f 73 69 78 45 72  rror(int posixEr
cdfa0 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69 74 65 49  ror, int sqliteI
cdfb0 4f 45 72 72 29 20 7b 0a 20 20 73 77 69 74 63 68  OErr) {.  switch
cdfc0 20 28 70 6f 73 69 78 45 72 72 6f 72 29 20 7b 0a   (posixError) {.
cdfd0 20 20 63 61 73 65 20 30 3a 20 0a 20 20 20 20 72    case 0: .    r
cdfe0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
cdff0 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 47  .    .  case EAG
ce000 41 49 4e 3a 0a 20 20 63 61 73 65 20 45 54 49 4d  AIN:.  case ETIM
ce010 45 44 4f 55 54 3a 0a 20 20 63 61 73 65 20 45 42  EDOUT:.  case EB
ce020 55 53 59 3a 0a 20 20 63 61 73 65 20 45 49 4e 54  USY:.  case EINT
ce030 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f 4c 43 4b  R:.  case ENOLCK
ce040 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61 6e 64 6f  :  .    /* rando
ce050 6d 20 4e 46 53 20 72 65 74 72 79 20 65 72 72 6f  m NFS retry erro
ce060 72 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67  r, unless during
ce070 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70   file system sup
ce080 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74  port .     * int
ce090 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77  rospection, in w
ce0a0 68 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79  hich it actually
ce0b0 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73   means what it s
ce0c0 61 79 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ays */.    retur
ce0d0 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  n SQLITE_BUSY;. 
ce0e0 20 20 20 0a 20 20 63 61 73 65 20 45 41 43 43 45     .  case EACCE
ce0f0 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41 43 43 45  S: .    /* EACCE
ce100 53 20 69 73 20 6c 69 6b 65 20 45 41 47 41 49 4e  S is like EAGAIN
ce110 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67 20   during locking 
ce120 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62 75 74 20  operations, but 
ce130 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72 20 74 69  not any other ti
ce140 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20 28 73 71  me*/.    if( (sq
ce150 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c  liteIOErr == SQL
ce160 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 20  ITE_IOERR_LOCK) 
ce170 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72  || ..(sqliteIOEr
ce180 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  r == SQLITE_IOER
ce190 52 5f 55 4e 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28  R_UNLOCK) || ..(
ce1a0 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53  sqliteIOErr == S
ce1b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f  QLITE_IOERR_RDLO
ce1c0 43 4b 29 20 7c 7c 0a 09 28 73 71 6c 69 74 65 49  CK) ||..(sqliteI
ce1d0 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49  OErr == SQLITE_I
ce1e0 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56  OERR_CHECKRESERV
ce1f0 45 44 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 20  EDLOCK) ){.     
ce200 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
ce210 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  USY;.    }.    /
ce220 2a 20 65 6c 73 65 20 66 61 6c 6c 20 74 68 72 6f  * else fall thro
ce230 75 67 68 20 2a 2f 0a 20 20 63 61 73 65 20 45 50  ugh */.  case EP
ce240 45 52 4d 3a 20 0a 20 20 20 20 72 65 74 75 72 6e  ERM: .    return
ce250 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20   SQLITE_PERM;.  
ce260 20 20 0a 20 20 63 61 73 65 20 45 44 45 41 44 4c    .  case EDEADL
ce270 4b 3a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  K:.    return SQ
ce280 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
ce290 45 44 3b 0a 20 20 20 20 0a 23 69 66 20 45 4f 50  ED;.    .#if EOP
ce2a0 4e 4f 54 53 55 50 50 21 3d 45 4e 4f 54 53 55 50  NOTSUPP!=ENOTSUP
ce2b0 0a 20 20 63 61 73 65 20 45 4f 50 4e 4f 54 53 55  .  case EOPNOTSU
ce2c0 50 50 3a 20 0a 20 20 20 20 2f 2a 20 73 6f 6d 65  PP: .    /* some
ce2d0 74 68 69 6e 67 20 77 65 6e 74 20 74 65 72 72 69  thing went terri
ce2e0 62 6c 79 20 61 77 72 79 2c 20 75 6e 6c 65 73 73  bly awry, unless
ce2f0 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73   during file sys
ce300 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20  tem support .   
ce310 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f    * introspectio
ce320 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 61  n, in which it a
ce330 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68  ctually means wh
ce340 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 65  at it says */.#e
ce350 6e 64 69 66 0a 23 69 66 64 65 66 20 45 4e 4f 54  ndif.#ifdef ENOT
ce360 53 55 50 0a 20 20 63 61 73 65 20 45 4e 4f 54 53  SUP.  case ENOTS
ce370 55 50 3a 20 0a 20 20 20 20 2f 2a 20 69 6e 76 61  UP: .    /* inva
ce380 6c 69 64 20 66 64 2c 20 75 6e 6c 65 73 73 20 64  lid fd, unless d
ce390 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65  uring file syste
ce3a0 6d 20 73 75 70 70 6f 72 74 20 69 6e 74 72 6f 73  m support intros
ce3b0 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63  pection, in whic
ce3c0 68 20 0a 20 20 20 20 20 2a 20 69 74 20 61 63 74  h .     * it act
ce3d0 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74  ually means what
ce3e0 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 65 6e 64   it says */.#end
ce3f0 69 66 0a 20 20 63 61 73 65 20 45 49 4f 3a 0a 20  if.  case EIO:. 
ce400 20 63 61 73 65 20 45 42 41 44 46 3a 0a 20 20 63   case EBADF:.  c
ce410 61 73 65 20 45 49 4e 56 41 4c 3a 0a 20 20 63 61  ase EINVAL:.  ca
ce420 73 65 20 45 4e 4f 54 43 4f 4e 4e 3a 0a 20 20 63  se ENOTCONN:.  c
ce430 61 73 65 20 45 4e 4f 44 45 56 3a 0a 20 20 63 61  ase ENODEV:.  ca
ce440 73 65 20 45 4e 58 49 4f 3a 0a 20 20 63 61 73 65  se ENXIO:.  case
ce450 20 45 4e 4f 45 4e 54 3a 0a 20 20 63 61 73 65 20   ENOENT:.  case 
ce460 45 53 54 41 4c 45 3a 0a 20 20 63 61 73 65 20 45  ESTALE:.  case E
ce470 4e 4f 53 59 53 3a 0a 20 20 20 20 2f 2a 20 74 68  NOSYS:.    /* th
ce480 65 73 65 20 73 68 6f 75 6c 64 20 66 6f 72 63 65  ese should force
ce490 20 74 68 65 20 63 6c 69 65 6e 74 20 74 6f 20 63   the client to c
ce4a0 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e  lose the file an
ce4b0 64 20 72 65 63 6f 6e 6e 65 63 74 20 2a 2f 0a 20  d reconnect */. 
ce4c0 20 20 20 0a 20 20 64 65 66 61 75 6c 74 3a 20 0a     .  default: .
ce4d0 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
ce4e0 65 49 4f 45 72 72 3b 0a 20 20 7d 0a 7d 0a 0a 0a  eIOErr;.  }.}...
ce4f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
ce500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ce510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ce520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ce530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ce540 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
ce550 2a 2a 2a 20 42 65 67 69 6e 20 55 6e 69 71 75 65  *** Begin Unique
ce560 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79   File ID Utility
ce570 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73   Used By VxWorks
ce580 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
ce590 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f 73 74 20 76  .**.** On most v
ce5a0 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e 69 78 2c  ersions of unix,
ce5b0 20 77 65 20 63 61 6e 20 67 65 74 20 61 20 75 6e   we can get a un
ce5c0 69 71 75 65 20 49 44 20 66 6f 72 20 61 20 66 69  ique ID for a fi
ce5d0 6c 65 20 62 79 20 63 6f 6e 63 61 74 65 6e 61 74  le by concatenat
ce5e0 69 6e 67 0a 2a 2a 20 74 68 65 20 64 65 76 69 63  ing.** the devic
ce5f0 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65  e number and the
ce600 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 20   inode number.  
ce610 42 75 74 20 74 68 69 73 20 64 6f 65 73 20 6e 6f  But this does no
ce620 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 6b  t work on VxWork
ce630 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 6f 72 6b 73  s..** On VxWorks
ce640 2c 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20  , a unique file 
ce650 69 64 20 6d 75 73 74 20 62 65 20 62 61 73 65 64  id must be based
ce660 20 6f 6e 20 74 68 65 20 63 61 6e 6f 6e 69 63 61   on the canonica
ce670 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 0a 2a  l filename..**.*
ce680 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * A pointer to a
ce690 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
ce6a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
ce6b0 63 74 75 72 65 20 63 61 6e 20 62 65 20 75 73 65  cture can be use
ce6c0 64 20 61 73 20 61 0a 2a 2a 20 75 6e 69 71 75 65  d as a.** unique
ce6d0 20 66 69 6c 65 20 49 44 20 69 6e 20 56 78 57 6f   file ID in VxWo
ce6e0 72 6b 73 2e 20 20 45 61 63 68 20 69 6e 73 74 61  rks.  Each insta
ce6f0 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
ce700 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a  cture contains.*
ce710 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  * a copy of the 
ce720 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61  canonical filena
ce730 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c  me.  There is al
ce740 73 6f 20 61 20 72 65 66 65 72 65 6e 63 65 20 63  so a reference c
ce750 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 68 65 20 73  ount.  .** The s
ce760 74 72 75 63 74 75 72 65 20 69 73 20 72 65 63 6c  tructure is recl
ce770 61 69 6d 65 64 20 77 68 65 6e 20 74 68 65 20 6e  aimed when the n
ce780 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72  umber of pointer
ce790 73 20 74 6f 20 69 74 20 64 72 6f 70 73 20 74 6f  s to it drops to
ce7a0 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20  .** zero..**.** 
ce7b0 54 68 65 72 65 20 61 72 65 20 6e 65 76 65 72 20  There are never 
ce7c0 76 65 72 79 20 6d 61 6e 79 20 66 69 6c 65 73 20  very many files 
ce7d0 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74 69 6d 65  open at one time
ce7e0 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20 61 72 65   and lookups are
ce7f0 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 72 66 6f 72   not.** a perfor
ce800 6d 61 6e 63 65 2d 63 72 69 74 69 63 61 6c 20 70  mance-critical p
ce810 61 74 68 2c 20 73 6f 20 69 74 20 69 73 20 73 75  ath, so it is su
ce820 66 66 69 63 69 65 6e 74 20 74 6f 20 70 75 74 20  fficient to put 
ce830 74 68 65 73 65 0a 2a 2a 20 73 74 72 75 63 74 75  these.** structu
ce840 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20  res on a linked 
ce850 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  list..*/.struct 
ce860 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 7b 0a  vxworksFileId {.
ce870 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73    struct vxworks
ce880 46 69 6c 65 49 64 20 2a 70 4e 65 78 74 3b 20 20  FileId *pNext;  
ce890 2f 2a 20 4e 65 78 74 20 69 6e 20 61 20 6c 69 73  /* Next in a lis
ce8a0 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f  t of them all */
ce8b0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
ce8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce8d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
ce8e0 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73  ferences to this
ce8f0 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e   one */.  int nN
ce900 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
ce910 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
ce920 68 20 6f 66 20 74 68 65 20 7a 43 61 6e 6f 6e 69  h of the zCanoni
ce930 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 72 69 6e 67  calName[] string
ce940 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 61 6e   */.  char *zCan
ce950 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 20 20 20 20  onicalName;     
ce960 20 20 20 20 2f 2a 20 43 61 6e 6f 6e 69 63 61 6c      /* Canonical
ce970 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 7d 3b 0a   filename */.};.
ce980 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
ce990 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 6e 69 71 75  /* .** All uniqu
ce9a0 65 20 66 69 6c 65 6e 61 6d 65 73 20 61 72 65 20  e filenames are 
ce9b0 68 65 6c 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64  held on a linked
ce9c0 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 20   list headed by 
ce9d0 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65  this.** variable
ce9e0 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  :.*/.static stru
ce9f0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  ct vxworksFileId
cea00 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73   *vxworksFileLis
cea10 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69  t = 0;../*.** Si
cea20 6d 70 6c 69 66 79 20 61 20 66 69 6c 65 6e 61 6d  mplify a filenam
cea30 65 20 69 6e 74 6f 20 69 74 73 20 63 61 6e 6f 6e  e into its canon
cea40 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 20 62 79 20  ical form.** by 
cea50 6d 61 6b 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f  making the follo
cea60 77 69 6e 67 20 63 68 61 6e 67 65 73 3a 0a 2a 2a  wing changes:.**
cea70 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 69 6e 67 20  .**  * removing 
cea80 61 6e 79 20 74 72 61 69 6c 69 6e 67 20 61 6e 64  any trailing and
cea90 20 64 75 70 6c 69 63 61 74 65 20 2f 0a 2a 2a 20   duplicate /.** 
ceaa0 20 2a 20 63 6f 6e 76 65 72 74 20 2f 2e 2f 20 69   * convert /./ i
ceab0 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 20 20 2a  nto just /.**  *
ceac0 20 63 6f 6e 76 65 72 74 20 2f 41 2f 2e 2e 2f 20   convert /A/../ 
cead0 77 68 65 72 65 20 41 20 69 73 20 61 6e 79 20 73  where A is any s
ceae0 69 6d 70 6c 65 20 6e 61 6d 65 20 69 6e 74 6f 20  imple name into 
ceaf0 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a 20 43 68 61  just /.**.** Cha
ceb00 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 6e  nges are made in
ceb10 2d 70 6c 61 63 65 2e 20 20 52 65 74 75 72 6e 20  -place.  Return 
ceb20 74 68 65 20 6e 65 77 20 6e 61 6d 65 20 6c 65 6e  the new name len
ceb30 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  gth..**.** The o
ceb40 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  riginal filename
ceb50 20 69 73 20 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d   is in z[0..n-1]
ceb60 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  .  Return the nu
ceb70 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61  mber of.** chara
ceb80 63 74 65 72 73 20 69 6e 20 74 68 65 20 73 69 6d  cters in the sim
ceb90 70 6c 69 66 69 65 64 20 6e 61 6d 65 2e 0a 2a 2f  plified name..*/
ceba0 0a 73 74 61 74 69 63 20 69 6e 74 20 76 78 77 6f  .static int vxwo
cebb0 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28  rksSimplifyName(
cebc0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b  char *z, int n){
cebd0 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 77  .  int i, j;.  w
cebe0 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a 5b 6e  hile( n>1 && z[n
cebf0 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d 2d 3b  -1]=='/' ){ n--;
cec00 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20   }.  for(i=j=0; 
cec10 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  i<n; i++){.    i
cec20 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a  f( z[i]=='/' ){.
cec30 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d        if( z[i+1]
cec40 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75 65  =='/' ) continue
cec50 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b  ;.      if( z[i+
cec60 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 32 3c 6e  1]=='.' && i+2<n
cec70 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2f 27 20   && z[i+2]=='/' 
cec80 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20  ){.        i += 
cec90 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69  1;.        conti
ceca0 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nue;.      }.   
cecb0 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27     if( z[i+1]=='
cecc0 2e 27 20 26 26 20 69 2b 33 3c 6e 20 26 26 20 7a  .' && i+3<n && z
cecd0 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 26 20 7a 5b  [i+2]=='.' && z[
cece0 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20  i+3]=='/' ){.   
cecf0 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3e 30 20       while( j>0 
ced00 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 2f 27 20 29  && z[j-1]!='/' )
ced10 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20  { j--; }.       
ced20 20 69 66 28 20 6a 3e 30 20 29 7b 20 6a 2d 2d 3b   if( j>0 ){ j--;
ced30 20 7d 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20   }.        i += 
ced40 32 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69  2;.        conti
ced50 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nue;.      }.   
ced60 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20   }.    z[j++] = 
ced70 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6a 5d  z[i];.  }.  z[j]
ced80 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6a   = 0;.  return j
ced90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  ;.}../*.** Find 
ceda0 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49 44  a unique file ID
cedb0 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 61   for the given a
cedc0 62 73 6f 6c 75 74 65 20 70 61 74 68 6e 61 6d 65  bsolute pathname
cedd0 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70  .  Return.** a p
cede0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 76 78  ointer to the vx
cedf0 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65  worksFileId obje
cee00 63 74 2e 20 20 54 68 69 73 20 70 6f 69 6e 74 65  ct.  This pointe
cee10 72 20 69 73 20 74 68 65 20 75 6e 69 71 75 65 0a  r is the unique.
cee20 2a 2a 20 66 69 6c 65 20 49 44 2e 0a 2a 2a 0a 2a  ** file ID..**.*
cee30 2a 20 54 68 65 20 6e 52 65 66 20 66 69 65 6c 64  * The nRef field
cee40 20 6f 66 20 74 68 65 20 76 78 77 6f 72 6b 73 46   of the vxworksF
cee50 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73 20  ileId object is 
cee60 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 65 66 6f  incremented befo
cee70 72 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74  re.** the object
cee80 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 41   is returned.  A
cee90 20 6e 65 77 20 76 78 77 6f 72 6b 73 46 69 6c 65   new vxworksFile
ceea0 49 64 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65  Id object is cre
ceeb0 61 74 65 64 0a 2a 2a 20 61 6e 64 20 61 64 64 65  ated.** and adde
ceec0 64 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20  d to the global 
ceed0 6c 69 73 74 20 69 66 20 6e 65 63 65 73 73 61 72  list if necessar
ceee0 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65  y..**.** If a me
ceef0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
cef00 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65  error occurs, re
cef10 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74  turn NULL..*/.st
cef20 61 74 69 63 20 73 74 72 75 63 74 20 76 78 77 6f  atic struct vxwo
cef30 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f 72  rksFileId *vxwor
cef40 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 63 6f 6e  ksFindFileId(con
cef50 73 74 20 63 68 61 72 20 2a 7a 41 62 73 6f 6c 75  st char *zAbsolu
cef60 74 65 4e 61 6d 65 29 7b 0a 20 20 73 74 72 75 63  teName){.  struc
cef70 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  t vxworksFileId 
cef80 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 2f  *pNew;         /
cef90 2a 20 73 65 61 72 63 68 20 6b 65 79 20 61 6e 64  * search key and
cefa0 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a 2f 0a   new file ID */.
cefb0 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73    struct vxworks
cefc0 46 69 6c 65 49 64 20 2a 70 43 61 6e 64 69 64 61  FileId *pCandida
cefd0 74 65 3b 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f  te;   /* For loo
cefe0 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69  ping over existi
ceff0 6e 67 20 66 69 6c 65 20 49 44 73 20 2a 2f 0a 20  ng file IDs */. 
cf000 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
cf010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cf020 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
cf030 66 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 20  f zAbsoluteName 
cf040 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 61 73 73  string */..  ass
cf050 65 72 74 28 20 7a 41 62 73 6f 6c 75 74 65 4e 61  ert( zAbsoluteNa
cf060 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b 0a 20 20  me[0]=='/' );.  
cf070 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  n = (int)strlen(
cf080 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 3b 0a  zAbsoluteName);.
cf090 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
cf0a0 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  _malloc( sizeof(
cf0b0 2a 70 4e 65 77 29 20 2b 20 28 6e 2b 31 29 20 29  *pNew) + (n+1) )
cf0c0 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
cf0d0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
cf0e0 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61  ew->zCanonicalNa
cf0f0 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70 4e 65  me = (char*)&pNe
cf100 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 28 70  w[1];.  memcpy(p
cf110 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e  New->zCanonicalN
cf120 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 74 65 4e 61  ame, zAbsoluteNa
cf130 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 6e 20 3d 20  me, n+1);.  n = 
cf140 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e  vxworksSimplifyN
cf150 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e  ame(pNew->zCanon
cf160 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3b 0a 0a 20  icalName, n);.. 
cf170 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61   /* Search for a
cf180 6e 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79  n existing entry
cf190 20 74 68 61 74 20 6d 61 74 63 68 69 6e 67 20 74   that matching t
cf1a0 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 6e 61 6d  he canonical nam
cf1b0 65 2e 0a 20 20 2a 2a 20 49 66 20 66 6f 75 6e 64  e..  ** If found
cf1c0 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  , increment the 
cf1d0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
cf1e0 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
cf1f0 6e 74 65 72 20 74 6f 0a 20 20 2a 2a 20 74 68 65  nter to.  ** the
cf200 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20 49   existing file I
cf210 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e  D..  */.  unixEn
cf220 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 66 6f  terMutex();.  fo
cf230 72 28 70 43 61 6e 64 69 64 61 74 65 3d 76 78 77  r(pCandidate=vxw
cf240 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20 70 43  orksFileList; pC
cf250 61 6e 64 69 64 61 74 65 3b 20 70 43 61 6e 64 69  andidate; pCandi
cf260 64 61 74 65 3d 70 43 61 6e 64 69 64 61 74 65 2d  date=pCandidate-
cf270 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
cf280 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 4e 61   pCandidate->nNa
cf290 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 26 26 20 6d  me==n .     && m
cf2a0 65 6d 63 6d 70 28 70 43 61 6e 64 69 64 61 74 65  emcmp(pCandidate
cf2b0 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65  ->zCanonicalName
cf2c0 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63  , pNew->zCanonic
cf2d0 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d 30 0a 20 20  alName, n)==0.  
cf2e0 20 20 29 7b 0a 20 20 20 20 20 20 20 73 71 6c 69    ){.       sqli
cf2f0 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a  te3_free(pNew);.
cf300 20 20 20 20 20 20 20 70 43 61 6e 64 69 64 61 74         pCandidat
cf310 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20  e->nRef++;.     
cf320 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
cf330 28 29 3b 0a 20 20 20 20 20 20 20 72 65 74 75 72  ();.       retur
cf340 6e 20 70 43 61 6e 64 69 64 61 74 65 3b 0a 20 20  n pCandidate;.  
cf350 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f    }.  }..  /* No
cf360 20 6d 61 74 63 68 20 77 61 73 20 66 6f 75 6e 64   match was found
cf370 2e 20 20 57 65 20 77 69 6c 6c 20 6d 61 6b 65 20  .  We will make 
cf380 61 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a 2f  a new file ID */
cf390 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20  .  pNew->nRef = 
cf3a0 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4e 61 6d 65  1;.  pNew->nName
cf3b0 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 2d 3e 70 4e   = n;.  pNew->pN
cf3c0 65 78 74 20 3d 20 76 78 77 6f 72 6b 73 46 69 6c  ext = vxworksFil
cf3d0 65 4c 69 73 74 3b 0a 20 20 76 78 77 6f 72 6b 73  eList;.  vxworks
cf3e0 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e 65 77 3b  FileList = pNew;
cf3f0 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65  .  unixLeaveMute
cf400 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e  x();.  return pN
cf410 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63  ew;.}../*.** Dec
cf420 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72  rement the refer
cf430 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e 20 61 20  ence count on a 
cf440 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62  vxworksFileId ob
cf450 6a 65 63 74 2e 20 20 46 72 65 65 0a 2a 2a 20 74  ject.  Free.** t
cf460 68 65 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 74  he object when t
cf470 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
cf480 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e  nt reaches zero.
cf490 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
cf4a0 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69  vxworksReleaseFi
cf4b0 6c 65 49 64 28 73 74 72 75 63 74 20 76 78 77 6f  leId(struct vxwo
cf4c0 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 29 7b  rksFileId *pId){
cf4d0 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65  .  unixEnterMute
cf4e0 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  x();.  assert( p
cf4f0 49 64 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20  Id->nRef>0 );.  
cf500 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69  pId->nRef--;.  i
cf510 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d 3d 30 20  f( pId->nRef==0 
cf520 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 76 78  ){.    struct vx
cf530 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 2a 70 70  worksFileId **pp
cf540 3b 0a 20 20 20 20 66 6f 72 28 70 70 3d 26 76 78  ;.    for(pp=&vx
cf550 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20 2a  worksFileList; *
cf560 70 70 20 26 26 20 2a 70 70 21 3d 70 49 64 3b 20  pp && *pp!=pId; 
cf570 70 70 20 3d 20 26 28 28 2a 70 70 29 2d 3e 70 4e  pp = &((*pp)->pN
cf580 65 78 74 29 29 7b 7d 0a 20 20 20 20 61 73 73 65  ext)){}.    asse
cf590 72 74 28 20 2a 70 70 3d 3d 70 49 64 20 29 3b 0a  rt( *pp==pId );.
cf5a0 20 20 20 20 2a 70 70 20 3d 20 70 49 64 2d 3e 70      *pp = pId->p
cf5b0 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Next;.    sqlite
cf5c0 33 5f 66 72 65 65 28 70 49 64 29 3b 0a 20 20 7d  3_free(pId);.  }
cf5d0 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65  .  unixLeaveMute
cf5e0 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  x();.}.#endif /*
cf5f0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f   OS_VXWORKS */./
cf600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
cf610 45 6e 64 20 6f 66 20 55 6e 69 71 75 65 20 46 69  End of Unique Fi
cf620 6c 65 20 49 44 20 55 74 69 6c 69 74 79 20 55 73  le ID Utility Us
cf630 65 64 20 42 79 20 56 78 57 6f 72 6b 73 20 2a 2a  ed By VxWorks **
cf640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
cf650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
cf6a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
cf6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf6f0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
cf700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 50 6f 73  ************ Pos
cf710 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b  ix Advisory Lock
cf720 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ing ************
cf730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf740 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 61 64 76  .**.** POSIX adv
cf750 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61 72 65 20  isory locks are 
cf760 62 72 6f 6b 65 6e 20 62 79 20 64 65 73 69 67 6e  broken by design
cf770 2e 20 20 41 4e 53 49 20 53 54 44 20 31 30 30 33  .  ANSI STD 1003
cf780 2e 31 20 28 31 39 39 36 29 0a 2a 2a 20 73 65 63  .1 (1996).** sec
cf790 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20 6c 69 6e  tion 6.5.2.2 lin
cf7a0 65 73 20 34 38 33 20 74 68 72 6f 75 67 68 20 34  es 483 through 4
cf7b0 39 30 20 73 70 65 63 69 66 79 20 74 68 61 74 20  90 specify that 
cf7c0 77 68 65 6e 20 61 20 70 72 6f 63 65 73 73 0a 2a  when a process.*
cf7d0 2a 20 73 65 74 73 20 6f 72 20 63 6c 65 61 72 73  * sets or clears
cf7e0 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74 20 6f 70   a lock, that op
cf7f0 65 72 61 74 69 6f 6e 20 6f 76 65 72 72 69 64 65  eration override
cf800 73 20 61 6e 79 20 70 72 69 6f 72 20 6c 6f 63 6b  s any prior lock
cf810 73 20 73 65 74 0a 2a 2a 20 62 79 20 74 68 65 20  s set.** by the 
cf820 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 49  same process.  I
cf830 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 70 6c 69  t does not expli
cf840 63 69 74 6c 79 20 73 61 79 20 73 6f 2c 20 62 75  citly say so, bu
cf850 74 20 74 68 69 73 20 69 6d 70 6c 69 65 73 0a 2a  t this implies.*
cf860 2a 20 74 68 61 74 20 69 74 20 6f 76 65 72 72 69  * that it overri
cf870 64 65 73 20 6c 6f 63 6b 73 20 73 65 74 20 62 79  des locks set by
cf880 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73   the same proces
cf890 73 20 75 73 69 6e 67 20 61 20 64 69 66 66 65 72  s using a differ
cf8a0 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63  ent.** file desc
cf8b0 72 69 70 74 6f 72 2e 20 20 43 6f 6e 73 69 64 65  riptor.  Conside
cf8c0 72 20 74 68 69 73 20 74 65 73 74 20 63 61 73 65  r this test case
cf8d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 69 6e  :.**.**       in
cf8e0 74 20 66 64 31 20 3d 20 6f 70 65 6e 28 22 2e 2f  t fd1 = open("./
cf8f0 66 69 6c 65 31 22 2c 20 4f 5f 52 44 57 52 7c 4f  file1", O_RDWR|O
cf900 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a  _CREAT, 0644);.*
cf910 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 32 20  *       int fd2 
cf920 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 32 22  = open("./file2"
cf930 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54  , O_RDWR|O_CREAT
cf940 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a 2a 20 53  , 0644);.**.** S
cf950 75 70 70 6f 73 65 20 2e 2f 66 69 6c 65 31 20 61  uppose ./file1 a
cf960 6e 64 20 2e 2f 66 69 6c 65 32 20 61 72 65 20 72  nd ./file2 are r
cf970 65 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 66  eally the same f
cf980 69 6c 65 20 28 62 65 63 61 75 73 65 0a 2a 2a 20  ile (because.** 
cf990 6f 6e 65 20 69 73 20 61 20 68 61 72 64 20 6f 72  one is a hard or
cf9a0 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b 20 74   symbolic link t
cf9b0 6f 20 74 68 65 20 6f 74 68 65 72 29 20 74 68 65  o the other) the
cf9c0 6e 20 69 66 20 79 6f 75 20 73 65 74 0a 2a 2a 20  n if you set.** 
cf9d0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
cf9e0 6b 20 6f 6e 20 66 64 31 2c 20 74 68 65 6e 20 74  k on fd1, then t
cf9f0 72 79 20 74 6f 20 67 65 74 20 61 6e 20 65 78 63  ry to get an exc
cfa00 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 6f  lusive lock.** o
cfa10 6e 20 66 64 32 2c 20 69 74 20 77 6f 72 6b 73 2e  n fd2, it works.
cfa20 20 20 49 20 77 6f 75 6c 64 20 68 61 76 65 20 65    I would have e
cfa30 78 70 65 63 74 65 64 20 74 68 65 20 73 65 63 6f  xpected the seco
cfa40 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a 20 66 61  nd lock to.** fa
cfa50 69 6c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77  il since there w
cfa60 61 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63  as already a loc
cfa70 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 64 75  k on the file du
cfa80 65 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 42 75 74  e to fd1..** But
cfa90 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63 65 20   not so.  Since 
cfaa0 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d 65 20  both locks came 
cfab0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 70 72  from the same pr
cfac0 6f 63 65 73 73 2c 20 74 68 65 0a 2a 2a 20 73 65  ocess, the.** se
cfad0 63 6f 6e 64 20 6f 76 65 72 72 69 64 65 73 20 74  cond overrides t
cfae0 68 65 20 66 69 72 73 74 2c 20 65 76 65 6e 20 74  he first, even t
cfaf0 68 6f 75 67 68 20 74 68 65 79 20 77 65 72 65 20  hough they were 
cfb00 6f 6e 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20  on different.** 
cfb10 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
cfb20 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66 66 65   opened on diffe
cfb30 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65 73 2e  rent file names.
cfb40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 61 6e  .**.** This mean
cfb50 73 20 74 68 61 74 20 77 65 20 63 61 6e 6e 6f 74  s that we cannot
cfb60 20 75 73 65 20 50 4f 53 49 58 20 6c 6f 63 6b 73   use POSIX locks
cfb70 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20   to synchronize 
cfb80 66 69 6c 65 20 61 63 63 65 73 73 0a 2a 2a 20 61  file access.** a
cfb90 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 6e 67 20 74  mong competing t
cfba0 68 72 65 61 64 73 20 6f 66 20 74 68 65 20 73 61  hreads of the sa
cfbb0 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 50 4f 53  me process.  POS
cfbc0 49 58 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 77 6f  IX locks will wo
cfbd0 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f 20 73 79  rk fine.** to sy
cfbe0 6e 63 68 72 6f 6e 69 7a 65 20 61 63 63 65 73 73  nchronize access
cfbf0 20 66 6f 72 20 74 68 72 65 61 64 73 20 69 6e 20   for threads in 
cfc00 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73  separate process
cfc10 65 73 2c 20 62 75 74 20 6e 6f 74 0a 2a 2a 20 74  es, but not.** t
cfc20 68 72 65 61 64 73 20 77 69 74 68 69 6e 20 74 68  hreads within th
cfc30 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 0a  e same process..
cfc40 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72  **.** To work ar
cfc50 6f 75 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d  ound the problem
cfc60 2c 20 53 51 4c 69 74 65 20 68 61 73 20 74 6f 20  , SQLite has to 
cfc70 6d 61 6e 61 67 65 20 66 69 6c 65 20 6c 6f 63 6b  manage file lock
cfc80 73 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20  s internally.** 
cfc90 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 20 57 68 65  on its own.  Whe
cfca0 6e 65 76 65 72 20 61 20 6e 65 77 20 64 61 74 61  never a new data
cfcb0 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2c 20  base is opened, 
cfcc0 77 65 20 68 61 76 65 20 74 6f 20 66 69 6e 64 20  we have to find 
cfcd0 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20  the.** specific 
cfce0 69 6e 6f 64 65 20 6f 66 20 74 68 65 20 64 61 74  inode of the dat
cfcf0 61 62 61 73 65 20 66 69 6c 65 20 28 74 68 65 20  abase file (the 
cfd00 69 6e 6f 64 65 20 69 73 20 64 65 74 65 72 6d 69  inode is determi
cfd10 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 74  ned by the.** st
cfd20 5f 64 65 76 20 61 6e 64 20 73 74 5f 69 6e 6f 20  _dev and st_ino 
cfd30 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 73 74  fields of the st
cfd40 61 74 20 73 74 72 75 63 74 75 72 65 20 74 68 61  at structure tha
cfd50 74 20 66 73 74 61 74 28 29 20 66 69 6c 6c 73 20  t fstat() fills 
cfd60 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 68 65 63 6b  in).** and check
cfd70 20 66 6f 72 20 6c 6f 63 6b 73 20 61 6c 72 65 61   for locks alrea
cfd80 64 79 20 65 78 69 73 74 69 6e 67 20 6f 6e 20 74  dy existing on t
cfd90 68 61 74 20 69 6e 6f 64 65 2e 20 20 57 68 65 6e  hat inode.  When
cfda0 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 63 72   locks are.** cr
cfdb0 65 61 74 65 64 20 6f 72 20 72 65 6d 6f 76 65 64  eated or removed
cfdc0 2c 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 6f  , we have to loo
cfdd0 6b 20 61 74 20 6f 75 72 20 6f 77 6e 20 69 6e 74  k at our own int
cfde0 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 20  ernal record of 
cfdf0 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20 74 6f 20  the.** locks to 
cfe00 73 65 65 20 69 66 20 61 6e 6f 74 68 65 72 20 74  see if another t
cfe10 68 72 65 61 64 20 68 61 73 20 70 72 65 76 69 6f  hread has previo
cfe20 75 73 6c 79 20 73 65 74 20 61 20 6c 6f 63 6b 20  usly set a lock 
cfe30 6f 6e 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20  on that same.** 
cfe40 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 28 41 73  inode..**.** (As
cfe50 69 64 65 3a 20 54 68 65 20 75 73 65 20 6f 66 20  ide: The use of 
cfe60 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 61 73  inode numbers as
cfe70 20 75 6e 69 71 75 65 20 49 44 73 20 64 6f 65 73   unique IDs does
cfe80 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57   not work on VxW
cfe90 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 20 56 78 57  orks..** For VxW
cfea0 6f 72 6b 73 2c 20 77 65 20 68 61 76 65 20 74 6f  orks, we have to
cfeb0 20 75 73 65 20 74 68 65 20 61 6c 74 65 72 6e 61   use the alterna
cfec0 74 69 76 65 20 75 6e 69 71 75 65 20 49 44 20 73  tive unique ID s
cfed0 79 73 74 65 6d 20 62 61 73 65 64 20 6f 6e 0a 2a  ystem based on.*
cfee0 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65  * canonical file
cfef0 6e 61 6d 65 20 61 6e 64 20 69 6d 70 6c 65 6d 65  name and impleme
cff00 6e 74 65 64 20 69 6e 20 74 68 65 20 70 72 65 76  nted in the prev
cff10 69 6f 75 73 20 64 69 76 69 73 69 6f 6e 2e 29 0a  ious division.).
cff20 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
cff30 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65  3_file structure
cff40 20 66 6f 72 20 50 4f 53 49 58 20 69 73 20 6e 6f   for POSIX is no
cff50 20 6c 6f 6e 67 65 72 20 6a 75 73 74 20 61 6e 20   longer just an 
cff60 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20  integer file.** 
cff70 64 65 73 63 72 69 70 74 6f 72 2e 20 20 49 74 20  descriptor.  It 
cff80 69 73 20 6e 6f 77 20 61 20 73 74 72 75 63 74 75  is now a structu
cff90 72 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  re that holds th
cffa0 65 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a  e integer file.*
cffb0 2a 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64  * descriptor and
cffc0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
cffd0 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64  structure that d
cffe0 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6e 74  escribes the int
cfff0 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73 20 6f  ernal.** locks o
d0000 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
d0010 69 6e 67 20 69 6e 6f 64 65 2e 20 20 54 68 65 72  ing inode.  Ther
d0020 65 20 69 73 20 6f 6e 65 20 6c 6f 63 6b 69 6e 67  e is one locking
d0030 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65   structure.** pe
d0040 72 20 69 6e 6f 64 65 2c 20 73 6f 20 69 66 20 74  r inode, so if t
d0050 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 69 73  he same inode is
d0060 20 6f 70 65 6e 65 64 20 74 77 69 63 65 2c 20 62   opened twice, b
d0070 6f 74 68 20 75 6e 69 78 46 69 6c 65 20 73 74 72  oth unixFile str
d0080 75 63 74 75 72 65 73 0a 2a 2a 20 70 6f 69 6e 74  uctures.** point
d0090 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6c 6f 63   to the same loc
d00a0 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20  king structure. 
d00b0 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72   The locking str
d00c0 75 63 74 75 72 65 20 6b 65 65 70 73 0a 2a 2a 20  ucture keeps.** 
d00d0 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  a reference coun
d00e0 74 20 28 73 6f 20 77 65 20 77 69 6c 6c 20 6b 6e  t (so we will kn
d00f0 6f 77 20 77 68 65 6e 20 74 6f 20 64 65 6c 65 74  ow when to delet
d0100 65 20 69 74 29 20 61 6e 64 20 61 20 22 63 6e 74  e it) and a "cnt
d0110 22 0a 2a 2a 20 66 69 65 6c 64 20 74 68 61 74 20  ".** field that 
d0120 74 65 6c 6c 73 20 75 73 20 69 74 73 20 69 6e 74  tells us its int
d0130 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 75  ernal lock statu
d0140 73 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73  s.  cnt==0 means
d0150 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20   the.** file is 
d0160 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e 74 3d 3d  unlocked.  cnt==
d0170 2d 31 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c  -1 means the fil
d0180 65 20 68 61 73 20 61 6e 20 65 78 63 6c 75 73 69  e has an exclusi
d0190 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 6e 74 3e  ve lock..** cnt>
d01a0 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 61 72  0 means there ar
d01b0 65 20 63 6e 74 20 73 68 61 72 65 64 20 6c 6f 63  e cnt shared loc
d01c0 6b 73 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a  ks on the file..
d01d0 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70  **.** Any attemp
d01e0 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c  t to lock or unl
d01f0 6f 63 6b 20 61 20 66 69 6c 65 20 66 69 72 73 74  ock a file first
d0200 20 63 68 65 63 6b 73 20 74 68 65 20 6c 6f 63 6b   checks the lock
d0210 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ing.** structure
d0220 2e 20 20 54 68 65 20 66 63 6e 74 6c 28 29 20 73  .  The fcntl() s
d0230 79 73 74 65 6d 20 63 61 6c 6c 20 69 73 20 6f 6e  ystem call is on
d0240 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 65  ly invoked to se
d0250 74 20 61 20 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f  t a .** POSIX lo
d0260 63 6b 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e  ck if the intern
d0270 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63 74 75 72  al lock structur
d0280 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62 65  e transitions be
d0290 74 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f 63 6b 65  tween.** a locke
d02a0 64 20 61 6e 64 20 61 6e 20 75 6e 6c 6f 63 6b 65  d and an unlocke
d02b0 64 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 42  d state..**.** B
d02c0 75 74 20 77 61 69 74 3a 20 20 74 68 65 72 65 20  ut wait:  there 
d02d0 61 72 65 20 79 65 74 20 6d 6f 72 65 20 70 72 6f  are yet more pro
d02e0 62 6c 65 6d 73 20 77 69 74 68 20 50 4f 53 49 58  blems with POSIX
d02f0 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 2e   advisory locks.
d0300 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 63 6c  .**.** If you cl
d0310 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 63 72  ose a file descr
d0320 69 70 74 6f 72 20 74 68 61 74 20 70 6f 69 6e 74  iptor that point
d0330 73 20 74 6f 20 61 20 66 69 6c 65 20 74 68 61 74  s to a file that
d0340 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 61   has locks,.** a
d0350 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74  ll locks on that
d0360 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 6f   file that are o
d0370 77 6e 65 64 20 62 79 20 74 68 65 20 63 75 72 72  wned by the curr
d0380 65 6e 74 20 70 72 6f 63 65 73 73 20 61 72 65 0a  ent process are.
d0390 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 20 54 6f  ** released.  To
d03a0 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69   work around thi
d03b0 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 63 68 20  s problem, each 
d03c0 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75  unixFile structu
d03d0 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61  re contains.** a
d03e0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 75   pointer to an u
d03f0 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63  nixOpenCnt struc
d0400 74 75 72 65 2e 20 20 54 68 65 72 65 20 69 73 20  ture.  There is 
d0410 6f 6e 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  one unixOpenCnt 
d0420 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65 72  structure.** per
d0430 20 6f 70 65 6e 20 69 6e 6f 64 65 2c 20 77 68 69   open inode, whi
d0440 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6d 75  ch means that mu
d0450 6c 74 69 70 6c 65 20 75 6e 69 78 46 69 6c 65 20  ltiple unixFile 
d0460 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20 61 20 73  can point to a s
d0470 69 6e 67 6c 65 0a 2a 2a 20 75 6e 69 78 4f 70 65  ingle.** unixOpe
d0480 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e 20 61  nCnt.  When an a
d0490 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74  ttempt is made t
d04a0 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69 78 46  o close an unixF
d04b0 69 6c 65 2c 20 69 66 20 74 68 65 72 65 20 61 72  ile, if there ar
d04c0 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69 78 46  e.** other unixF
d04d0 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20  ile open on the 
d04e0 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61 74 20  same inode that 
d04f0 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f 63 6b  are holding lock
d0500 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a 2a 20 74  s, the call.** t
d0510 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20 66 69  o close() the fi
d0520 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
d0530 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20   deferred until 
d0540 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73  all of the locks
d0550 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 65 20 75   clear..** The u
d0560 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63  nixOpenCnt struc
d0570 74 75 72 65 20 6b 65 65 70 73 20 61 20 6c 69 73  ture keeps a lis
d0580 74 20 6f 66 20 66 69 6c 65 20 64 65 73 63 72 69  t of file descri
d0590 70 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64 20  ptors that need 
d05a0 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73 65 64 20  to.** be closed 
d05b0 61 6e 64 20 74 68 61 74 20 6c 69 73 74 20 69 73  and that list is
d05c0 20 77 61 6c 6b 65 64 20 28 61 6e 64 20 63 6c 65   walked (and cle
d05d0 61 72 65 64 29 20 77 68 65 6e 20 74 68 65 20 6c  ared) when the l
d05e0 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63 6c 65 61  ast lock.** clea
d05f0 72 73 2e 0a 2a 2a 0a 2a 2a 20 59 65 74 20 61 6e  rs..**.** Yet an
d0600 6f 74 68 65 72 20 70 72 6f 62 6c 65 6d 3a 20 20  other problem:  
d0610 4c 69 6e 75 78 54 68 72 65 61 64 73 20 64 6f 20  LinuxThreads do 
d0620 6e 6f 74 20 70 6c 61 79 20 77 65 6c 6c 20 77 69  not play well wi
d0630 74 68 20 70 6f 73 69 78 20 6c 6f 63 6b 73 2e 0a  th posix locks..
d0640 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 6f 6c 64 65 72  **.** Many older
d0650 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 6c 69 6e   versions of lin
d0660 75 78 20 75 73 65 20 74 68 65 20 4c 69 6e 75 78  ux use the Linux
d0670 54 68 72 65 61 64 73 20 6c 69 62 72 61 72 79 20  Threads library 
d0680 77 68 69 63 68 20 69 73 0a 2a 2a 20 6e 6f 74 20  which is.** not 
d0690 70 6f 73 69 78 20 63 6f 6d 70 6c 69 61 6e 74 2e  posix compliant.
d06a0 20 20 55 6e 64 65 72 20 4c 69 6e 75 78 54 68 72    Under LinuxThr
d06b0 65 61 64 73 2c 20 61 20 6c 6f 63 6b 20 63 72 65  eads, a lock cre
d06c0 61 74 65 64 20 62 79 20 74 68 72 65 61 64 0a 2a  ated by thread.*
d06d0 2a 20 41 20 63 61 6e 6e 6f 74 20 62 65 20 6d 6f  * A cannot be mo
d06e0 64 69 66 69 65 64 20 6f 72 20 6f 76 65 72 72 69  dified or overri
d06f0 64 64 65 6e 20 62 79 20 61 20 64 69 66 66 65 72  dden by a differ
d0700 65 6e 74 20 74 68 72 65 61 64 20 42 2e 0a 2a 2a  ent thread B..**
d0710 20 4f 6e 6c 79 20 74 68 72 65 61 64 20 41 20 63   Only thread A c
d0720 61 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 6c 6f  an modify the lo
d0730 63 6b 2e 20 20 4c 6f 63 6b 69 6e 67 20 62 65 68  ck.  Locking beh
d0740 61 76 69 6f 72 20 69 73 20 63 6f 72 72 65 63 74  avior is correct
d0750 0a 2a 2a 20 69 66 20 74 68 65 20 61 70 70 6c 69  .** if the appli
d0760 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 6e  ation uses the n
d0770 65 77 65 72 20 4e 61 74 69 76 65 20 50 6f 73 69  ewer Native Posi
d0780 78 20 54 68 72 65 61 64 20 4c 69 62 72 61 72 79  x Thread Library
d0790 20 28 4e 50 54 4c 29 0a 2a 2a 20 6f 6e 20 6c 69   (NPTL).** on li
d07a0 6e 75 78 20 2d 20 77 69 74 68 20 4e 50 54 4c 20  nux - with NPTL 
d07b0 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62  a lock created b
d07c0 79 20 74 68 72 65 61 64 20 41 20 63 61 6e 20 6f  y thread A can o
d07d0 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 0a 2a 2a  verride locks.**
d07e0 20 69 6e 20 74 68 72 65 61 64 20 42 2e 20 20 42   in thread B.  B
d07f0 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77  ut there is no w
d0800 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 20 63 6f  ay to know at co
d0810 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 69 63 68  mpile-time which
d0820 0a 2a 2a 20 74 68 72 65 61 64 69 6e 67 20 6c 69  .** threading li
d0830 62 72 61 72 79 20 69 73 20 62 65 69 6e 67 20 75  brary is being u
d0840 73 65 64 2e 20 20 53 6f 20 74 68 65 72 65 20 69  sed.  So there i
d0850 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f 77  s no way to know
d0860 20 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74   at.** compile-t
d0870 69 6d 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ime whether or n
d0880 6f 74 20 74 68 72 65 61 64 20 41 20 63 61 6e 20  ot thread A can 
d0890 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 20 6f  override locks o
d08a0 6e 20 74 68 72 65 61 64 20 42 2e 0a 2a 2a 20 57  n thread B..** W
d08b0 65 20 68 61 76 65 20 74 6f 20 64 6f 20 61 20 72  e have to do a r
d08c0 75 6e 2d 74 69 6d 65 20 63 68 65 63 6b 20 74 6f  un-time check to
d08d0 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 62 65   discover the be
d08e0 68 61 76 69 6f 72 20 6f 66 20 74 68 65 0a 2a 2a  havior of the.**
d08f0 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73   current process
d0900 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 79 73 74 65  ..**.** On syste
d0910 6d 73 20 77 68 65 72 65 20 74 68 72 65 61 64 20  ms where thread 
d0920 41 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 6d  A is unable to m
d0930 6f 64 69 66 79 20 6c 6f 63 6b 73 20 63 72 65 61  odify locks crea
d0940 74 65 64 20 62 79 0a 2a 2a 20 74 68 72 65 61 64  ted by.** thread
d0950 20 42 2c 20 77 65 20 68 61 76 65 20 74 6f 20 6b   B, we have to k
d0960 65 65 70 20 74 72 61 63 6b 20 6f 66 20 77 68 69  eep track of whi
d0970 63 68 20 74 68 72 65 61 64 20 63 72 65 61 74 65  ch thread create
d0980 64 20 65 61 63 68 0a 2a 2a 20 6c 6f 63 6b 2e 20  d each.** lock. 
d0990 20 48 65 6e 63 65 20 74 68 65 72 65 20 69 73 20   Hence there is 
d09a0 61 6e 20 65 78 74 72 61 20 66 69 65 6c 64 20 69  an extra field i
d09b0 6e 20 74 68 65 20 6b 65 79 20 74 6f 20 74 68 65  n the key to the
d09c0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 0a 2a 2a   unixLockInfo.**
d09d0 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 72 65   structure to re
d09e0 63 6f 72 64 20 74 68 69 73 20 69 6e 66 6f 72 6d  cord this inform
d09f0 61 74 69 6f 6e 2e 20 20 41 6e 64 20 6f 6e 20 74  ation.  And on t
d0a00 68 6f 73 65 20 73 79 73 74 65 6d 73 20 69 74 0a  hose systems it.
d0a10 2a 2a 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f  ** is illegal to
d0a20 20 62 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63   begin a transac
d0a30 74 69 6f 6e 20 69 6e 20 6f 6e 65 20 74 68 72 65  tion in one thre
d0a40 61 64 20 61 6e 64 20 66 69 6e 69 73 68 20 69 74  ad and finish it
d0a50 0a 2a 2a 20 69 6e 20 61 6e 6f 74 68 65 72 2e 20  .** in another. 
d0a60 20 46 6f 72 20 74 68 69 73 20 6c 61 74 74 65 72   For this latter
d0a70 20 72 65 73 74 72 69 63 74 69 6f 6e 2c 20 74 68   restriction, th
d0a80 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 2d 61  ere is no work-a
d0a90 72 6f 75 6e 64 2e 0a 2a 2a 20 49 74 20 69 73 20  round..** It is 
d0aa0 61 20 6c 69 6d 69 74 61 74 69 6f 6e 20 6f 66 20  a limitation of 
d0ab0 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a 2a 2f  LinuxThreads..*/
d0ac0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 63  ../*.** Set or c
d0ad0 68 65 63 6b 20 74 68 65 20 75 6e 69 78 46 69 6c  heck the unixFil
d0ae0 65 2e 74 69 64 20 66 69 65 6c 64 2e 20 20 54 68  e.tid field.  Th
d0af0 69 73 20 66 69 65 6c 64 20 69 73 20 73 65 74 20  is field is set 
d0b00 77 68 65 6e 20 61 6e 20 75 6e 69 78 46 69 6c 65  when an unixFile
d0b10 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 6f 70 65  .** is first ope
d0b20 6e 65 64 2e 20 20 41 6c 6c 20 73 75 62 73 65 71  ned.  All subseq
d0b30 75 65 6e 74 20 75 73 65 73 20 6f 66 20 74 68 65  uent uses of the
d0b40 20 75 6e 69 78 46 69 6c 65 20 76 65 72 69 66 79   unixFile verify
d0b50 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d   that the.** sam
d0b60 65 20 74 68 72 65 61 64 20 69 73 20 6f 70 65 72  e thread is oper
d0b70 61 74 69 6e 67 20 6f 6e 20 74 68 65 20 75 6e 69  ating on the uni
d0b80 78 46 69 6c 65 2e 20 20 53 6f 6d 65 20 6f 70 65  xFile.  Some ope
d0b90 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 64  rating systems d
d0ba0 6f 0a 2a 2a 20 6e 6f 74 20 61 6c 6c 6f 77 20 6c  o.** not allow l
d0bb0 6f 63 6b 73 20 74 6f 20 62 65 20 6f 76 65 72 72  ocks to be overr
d0bc0 69 64 64 65 6e 20 62 79 20 6f 74 68 65 72 20 74  idden by other t
d0bd0 68 72 65 61 64 73 20 61 6e 64 20 74 68 61 74 20  hreads and that 
d0be0 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 6d  restriction.** m
d0bf0 65 61 6e 73 20 74 68 61 74 20 73 71 6c 69 74 65  eans that sqlite
d0c00 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  3* database hand
d0c10 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 6d 6f  les cannot be mo
d0c20 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72  ved from one thr
d0c30 65 61 64 0a 2a 2a 20 74 6f 20 61 6e 6f 74 68 65  ead.** to anothe
d0c40 72 20 77 68 69 6c 65 20 6c 6f 63 6b 73 20 61 72  r while locks ar
d0c50 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 56 65  e held..**.** Ve
d0c60 72 73 69 6f 6e 20 33 2e 33 2e 31 20 28 32 30 30  rsion 3.3.1 (200
d0c70 36 2d 30 31 2d 31 35 29 3a 20 20 75 6e 69 78 46  6-01-15):  unixF
d0c80 69 6c 65 20 63 61 6e 20 62 65 20 6d 6f 76 65 64  ile can be moved
d0c90 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64   from one thread
d0ca0 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 61   to.** another a
d0cb0 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 61 72 65  s long as we are
d0cc0 20 72 75 6e 6e 69 6e 67 20 6f 6e 20 61 20 73 79   running on a sy
d0cd0 73 74 65 6d 20 74 68 61 74 20 73 75 70 70 6f 72  stem that suppor
d0ce0 74 73 20 74 68 72 65 61 64 73 0a 2a 2a 20 6f 76  ts threads.** ov
d0cf0 65 72 72 69 64 69 6e 67 20 65 61 63 68 20 6f 74  erriding each ot
d0d00 68 65 72 73 20 6c 6f 63 6b 73 20 28 77 68 69 63  hers locks (whic
d0d10 68 20 69 73 20 6e 6f 77 20 74 68 65 20 6d 6f 73  h is now the mos
d0d20 74 20 63 6f 6d 6d 6f 6e 20 62 65 68 61 76 69 6f  t common behavio
d0d30 72 29 0a 2a 2a 20 6f 72 20 69 66 20 6e 6f 20 6c  r).** or if no l
d0d40 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 20 20  ocks are held.  
d0d50 42 75 74 20 74 68 65 20 75 6e 69 78 46 69 6c 65  But the unixFile
d0d60 2e 70 4c 6f 63 6b 20 66 69 65 6c 64 20 6e 65 65  .pLock field nee
d0d70 64 73 20 74 6f 20 62 65 0a 2a 2a 20 72 65 63 6f  ds to be.** reco
d0d80 6d 70 75 74 65 64 20 62 65 63 61 75 73 65 20 69  mputed because i
d0d90 74 73 20 6b 65 79 20 69 6e 63 6c 75 64 65 73 20  ts key includes 
d0da0 74 68 65 20 74 68 72 65 61 64 2d 69 64 2e 20 20  the thread-id.  
d0db0 53 65 65 20 74 68 65 20 0a 2a 2a 20 74 72 61 6e  See the .** tran
d0dc0 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 29 20  sferOwnership() 
d0dd0 66 75 6e 63 74 69 6f 6e 20 62 65 6c 6f 77 20 66  function below f
d0de0 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
d0df0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2f 0a 23 69 66  formation.*/.#if
d0e00 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
d0e10 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  FE && defined(__
d0e20 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e  linux__).# defin
d0e30 65 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 58  e SET_THREADID(X
d0e40 29 20 20 20 28 58 29 2d 3e 74 69 64 20 3d 20 70  )   (X)->tid = p
d0e50 74 68 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 20  thread_self().# 
d0e60 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52  define CHECK_THR
d0e70 45 41 44 49 44 28 58 29 20 28 74 68 72 65 61 64  EADID(X) (thread
d0e80 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
d0e90 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 26 20 5c  ersLocks==0 && \
d0ea0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
d0eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 21 70 74               !pt
d0ec0 68 72 65 61 64 5f 65 71 75 61 6c 28 28 58 29 2d  hread_equal((X)-
d0ed0 3e 74 69 64 2c 20 70 74 68 72 65 61 64 5f 73 65  >tid, pthread_se
d0ee0 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a 23 20 64  lf())).#else.# d
d0ef0 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44  efine SET_THREAD
d0f00 49 44 28 58 29 0a 23 20 64 65 66 69 6e 65 20 43  ID(X).# define C
d0f10 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29  HECK_THREADID(X)
d0f20 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
d0f30 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
d0f40 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
d0f50 72 75 63 74 75 72 65 20 73 65 72 76 65 73 20 61  ructure serves a
d0f60 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a  s the key used.*
d0f70 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61  * to locate a pa
d0f80 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4f 70 65  rticular unixOpe
d0f90 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 67  nCnt structure g
d0fa0 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 20  iven its inode. 
d0fb0 20 54 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20   This.** is the 
d0fc0 73 61 6d 65 20 61 73 20 74 68 65 20 75 6e 69 78  same as the unix
d0fd0 4c 6f 63 6b 4b 65 79 20 65 78 63 65 70 74 20 74  LockKey except t
d0fe0 68 61 74 20 74 68 65 20 74 68 72 65 61 64 20 49  hat the thread I
d0ff0 44 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2f  D is omitted..*/
d1000 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65  .struct unixFile
d1010 49 64 20 7b 0a 20 20 64 65 76 5f 74 20 64 65 76  Id {.  dev_t dev
d1020 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
d1030 20 20 20 2f 2a 20 44 65 76 69 63 65 20 6e 75 6d     /* Device num
d1040 62 65 72 20 2a 2f 0a 23 69 66 20 4f 53 5f 56 58  ber */.#if OS_VX
d1050 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 76  WORKS.  struct v
d1060 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49  xworksFileId *pI
d1070 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 66 69  d;  /* Unique fi
d1080 6c 65 20 49 44 20 66 6f 72 20 76 78 77 6f 72 6b  le ID for vxwork
d1090 73 2e 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 69 6e  s. */.#else.  in
d10a0 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 20 20 20  o_t ino;        
d10b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 6f            /* Ino
d10c0 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 65 6e  de number */.#en
d10d0 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e  dif.};../*.** An
d10e0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
d10f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
d1100 74 75 72 65 20 73 65 72 76 65 73 20 61 73 20 74  ture serves as t
d1110 68 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74  he key used.** t
d1120 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 72 74 69  o locate a parti
d1130 63 75 6c 61 72 20 75 6e 69 78 4c 6f 63 6b 49 6e  cular unixLockIn
d1140 66 6f 20 73 74 72 75 63 74 75 72 65 20 67 69 76  fo structure giv
d1150 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 0a 2a 2a  en its inode..**
d1160 0a 2a 2a 20 49 66 20 74 68 72 65 61 64 73 20 63  .** If threads c
d1170 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65  annot override e
d1180 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
d1190 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73 29 2c   (LinuxThreads),
d11a0 20 74 68 65 6e 20 77 65 0a 2a 2a 20 73 65 74 20   then we.** set 
d11b0 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 2e  the unixLockKey.
d11c0 74 69 64 20 66 69 65 6c 64 20 74 6f 20 74 68 65  tid field to the
d11d0 20 74 68 72 65 61 64 20 49 44 2e 20 20 49 66 20   thread ID.  If 
d11e0 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72  threads can over
d11f0 72 69 64 65 0a 2a 2a 20 65 61 63 68 20 6f 74 68  ride.** each oth
d1200 65 72 73 20 6c 6f 63 6b 73 20 28 50 6f 73 69 78  ers locks (Posix
d1210 20 61 6e 64 20 4e 50 54 4c 29 20 74 68 65 6e 20   and NPTL) then 
d1220 74 69 64 20 69 73 20 61 6c 77 61 79 73 20 73 65  tid is always se
d1230 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 20 74 69  t to zero..** ti
d1240 64 20 69 73 20 6f 6d 69 74 74 65 64 20 69 66 20  d is omitted if 
d1250 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f  we compile witho
d1260 75 74 20 74 68 72 65 61 64 69 6e 67 20 73 75 70  ut threading sup
d1270 70 6f 72 74 20 6f 72 20 6f 6e 20 61 6e 20 4f 53  port or on an OS
d1280 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 6c  .** other than l
d1290 69 6e 75 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  inux..*/.struct 
d12a0 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 7b 0a 20 20  unixLockKey {.  
d12b0 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49  struct unixFileI
d12c0 64 20 66 69 64 3b 20 20 2f 2a 20 55 6e 69 71 75  d fid;  /* Uniqu
d12d0 65 20 69 64 65 6e 74 69 66 69 65 72 20 66 6f 72  e identifier for
d12e0 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 23 69 66   the file */.#if
d12f0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
d1300 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  FE && defined(__
d1310 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68 72 65  linux__).  pthre
d1320 61 64 5f 74 20 74 69 64 3b 20 20 2f 2a 20 54 68  ad_t tid;  /* Th
d1330 72 65 61 64 20 49 44 20 6f 66 20 6c 6f 63 6b 20  read ID of lock 
d1340 6f 77 6e 65 72 2e 20 5a 65 72 6f 20 69 66 20 6e  owner. Zero if n
d1350 6f 74 20 75 73 69 6e 67 20 4c 69 6e 75 78 54 68  ot using LinuxTh
d1360 72 65 61 64 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  reads */.#endif.
d1370 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
d1380 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
d1390 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
d13a0 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f   is allocated fo
d13b0 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69  r each open.** i
d13c0 6e 6f 64 65 2e 20 20 4f 72 2c 20 6f 6e 20 4c 69  node.  Or, on Li
d13d0 6e 75 78 54 68 72 65 61 64 73 2c 20 74 68 65 72  nuxThreads, ther
d13e0 65 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73  e is one of thes
d13f0 65 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72  e structures for
d1400 0a 2a 2a 20 65 61 63 68 20 69 6e 6f 64 65 20 6f  .** each inode o
d1410 70 65 6e 65 64 20 62 79 20 65 61 63 68 20 74 68  pened by each th
d1420 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 69  read..**.** A si
d1430 6e 67 6c 65 20 69 6e 6f 64 65 20 63 61 6e 20 68  ngle inode can h
d1440 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 66 69 6c  ave multiple fil
d1450 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20 73  e descriptors, s
d1460 6f 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65 0a  o each unixFile.
d1470 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  ** structure con
d1480 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  tains a pointer 
d1490 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  to an instance o
d14a0 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 61 6e  f this object an
d14b0 64 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74  d this.** object
d14c0 20 6b 65 65 70 73 20 61 20 63 6f 75 6e 74 20 6f   keeps a count o
d14d0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
d14e0 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e 74 69 6e  unixFile pointin
d14f0 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73 74 72 75  g to it..*/.stru
d1500 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
d1510 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c  {.  struct unixL
d1520 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65 79 3b 20  ockKey lockKey; 
d1530 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75      /* The looku
d1540 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 63  p key */.  int c
d1550 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
d1560 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
d1570 6d 62 65 72 20 6f 66 20 53 48 41 52 45 44 20 6c  mber of SHARED l
d1580 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 20 69  ocks held */.  i
d1590 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20  nt locktype;    
d15a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d15b0 2a 20 4f 6e 65 20 6f 66 20 53 48 41 52 45 44 5f  * One of SHARED_
d15c0 4c 4f 43 4b 2c 20 52 45 53 45 52 56 45 44 5f 4c  LOCK, RESERVED_L
d15d0 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a 20 20 69 6e  OCK etc. */.  in
d15e0 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
d15f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d1600 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74   Number of point
d1610 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75  ers to this stru
d1620 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 63  cture */.  struc
d1630 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a  t unixLockInfo *
d1640 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a 20 4c 69  pNext;     /* Li
d1650 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f  st of all unixLo
d1660 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 73 20 2a  ckInfo objects *
d1670 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c  /.  struct unixL
d1680 6f 63 6b 49 6e 66 6f 20 2a 70 50 72 65 76 3b 20  ockInfo *pPrev; 
d1690 20 20 20 20 2f 2a 20 20 20 20 2e 2e 2e 2e 20 64      /*    .... d
d16a0 6f 75 62 6c 79 20 6c 69 6e 6b 65 64 20 2a 2f 0a  oubly linked */.
d16b0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
d16c0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
d16d0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
d16e0 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f   is allocated fo
d16f0 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69  r each open.** i
d1700 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74 72 75  node.  This stru
d1710 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63  cture keeps trac
d1720 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  k of the number 
d1730 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74  of locks on that
d1740 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66 20 61  .** inode.  If a
d1750 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65 6d 70   close is attemp
d1760 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e 20 69  ted against an i
d1770 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68 6f 6c  node that is hol
d1780 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c 20 74  ding.** locks, t
d1790 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65 66 65  he close is defe
d17a0 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6c  rred until all l
d17b0 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20 61 64  ocks clear by ad
d17c0 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65  ding the.** file
d17d0 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 62   descriptor to b
d17e0 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68 65 20  e closed to the 
d17f0 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a 2a 2a  pending list..**
d1800 0a 2a 2a 20 54 4f 44 4f 3a 20 20 43 6f 6e 73 69  .** TODO:  Consi
d1810 64 65 72 20 63 68 61 6e 67 69 6e 67 20 74 68 69  der changing thi
d1820 73 20 73 6f 20 74 68 61 74 20 74 68 65 72 65 20  s so that there 
d1830 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65  is only a single
d1840 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70   file.** descrip
d1850 74 6f 72 20 66 6f 72 20 65 61 63 68 20 6f 70 65  tor for each ope
d1860 6e 20 66 69 6c 65 2c 20 65 76 65 6e 20 77 68 65  n file, even whe
d1870 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64 20 6d  n it is opened m
d1880 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a 2a  ultiple times..*
d1890 2a 20 54 68 65 20 63 6c 6f 73 65 28 29 20 73 79  * The close() sy
d18a0 73 74 65 6d 20 63 61 6c 6c 20 77 6f 75 6c 64 20  stem call would 
d18b0 6f 6e 6c 79 20 6f 63 63 75 72 20 77 68 65 6e 20  only occur when 
d18c0 74 68 65 20 6c 61 73 74 20 64 61 74 61 62 61 73  the last databas
d18d0 65 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 66  e.** using the f
d18e0 69 6c 65 20 63 6c 6f 73 65 73 2e 0a 2a 2f 0a 73  ile closes..*/.s
d18f0 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
d1900 74 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69  t {.  struct uni
d1910 78 46 69 6c 65 49 64 20 66 69 6c 65 49 64 3b 20  xFileId fileId; 
d1920 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20    /* The lookup 
d1930 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  key */.  int nRe
d1940 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
d1950 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
d1960 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  f pointers to th
d1970 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  is structure */.
d1980 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 20 20    int nLock;    
d1990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d19a0 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74   Number of outst
d19b0 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a  anding locks */.
d19c0 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e 67 3b 20    int nPending; 
d19d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d19e0 20 4e 75 6d 62 65 72 20 6f 66 20 70 65 6e 64 69   Number of pendi
d19f0 6e 67 20 63 6c 6f 73 65 28 29 20 6f 70 65 72 61  ng close() opera
d1a00 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  tions */.  int *
d1a10 61 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 20  aPending;       
d1a20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 65 64       /* Malloced
d1a30 20 73 70 61 63 65 20 68 6f 6c 64 69 6e 67 20 66   space holding f
d1a40 64 27 73 20 61 77 61 69 74 69 6e 67 20 61 20 63  d's awaiting a c
d1a50 6c 6f 73 65 28 29 20 2a 2f 0a 23 69 66 20 4f 53  lose() */.#if OS
d1a60 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d 5f 74  _VXWORKS.  sem_t
d1a70 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20 20 20   *pSem;         
d1a80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
d1a90 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d 61 70  amed POSIX semap
d1aa0 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 61  hore */.  char a
d1ab0 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 41 54 48  SemName[MAX_PATH
d1ac0 4e 41 4d 45 2b 31 5d 3b 20 20 20 2f 2a 20 4e 61  NAME+1];   /* Na
d1ad0 6d 65 20 6f 66 20 74 68 61 74 20 73 65 6d 61 70  me of that semap
d1ae0 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  hore */.#endif. 
d1af0 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e   struct unixOpen
d1b00 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72  Cnt *pNext, *pPr
d1b10 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ev;   /* List of
d1b20 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 43 6e 74   all unixOpenCnt
d1b30 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a   objects */.};..
d1b40 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f 66 20 61  /*.** Lists of a
d1b50 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ll unixLockInfo 
d1b60 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  and unixOpenCnt 
d1b70 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 73 65 20  objects.  These 
d1b80 75 73 65 64 20 74 6f 20 62 65 20 68 61 73 68 0a  used to be hash.
d1b90 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 75 74 20  ** tables.  But 
d1ba0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 62  the number of ob
d1bb0 6a 65 63 74 73 20 69 73 20 72 61 72 65 6c 79 20  jects is rarely 
d1bc0 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65  more than a doze
d1bd0 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 72 20 65  n and.** never e
d1be0 78 63 65 65 64 73 20 61 20 66 65 77 20 74 68 6f  xceeds a few tho
d1bf0 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c 6f 6f 6b  usand.  And look
d1c00 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 63  up is not on a c
d1c10 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 74 68 20  ritical.** path 
d1c20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c 69 6e 6b  so a simple link
d1c30 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 73 75 66  ed list will suf
d1c40 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  fice..*/.static 
d1c50 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49  struct unixLockI
d1c60 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20 3d 20  nfo *lockList = 
d1c70 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  0;.static struct
d1c80 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 6f 70   unixOpenCnt *op
d1c90 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a  enList = 0;../*.
d1ca0 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65  ** This variable
d1cb0 20 72 65 6d 65 6d 62 65 72 73 20 77 68 65 74 68   remembers wheth
d1cc0 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64  er or not thread
d1cd0 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65  s can override e
d1ce0 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20 6c 6f  ach others.** lo
d1cf0 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a  cks..**.**    0:
d1d00 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 73 20 63    No.  Threads c
d1d10 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65  annot override e
d1d20 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
d1d30 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73  .  (LinuxThreads
d1d40 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65 73 2e  ).**    1:  Yes.
d1d50 20 20 54 68 72 65 61 64 73 20 63 61 6e 20 6f 76    Threads can ov
d1d60 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65  erride each othe
d1d70 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 6f 73 69  rs locks.  (Posi
d1d80 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 20 20 2d  x & NLPT).**   -
d1d90 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b 6e 6f  1:  We don't kno
d1da0 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20  w yet..**.** On 
d1db0 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 77 65  some systems, we
d1dc0 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65   know at compile
d1dd0 2d 74 69 6d 65 20 69 66 20 74 68 72 65 61 64 73  -time if threads
d1de0 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61   can override ea
d1df0 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c 6f 63  ch.** others loc
d1e00 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79  ks.  On those sy
d1e10 73 74 65 6d 73 2c 20 74 68 65 20 53 51 4c 49 54  stems, the SQLIT
d1e20 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44  E_THREAD_OVERRID
d1e30 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a 2a 20  E_LOCK macro.** 
d1e40 77 69 6c 6c 20 62 65 20 73 65 74 20 61 70 70 72  will be set appr
d1e50 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e 20 6f  opriately.  On o
d1e60 74 68 65 72 20 73 79 73 74 65 6d 73 2c 20 77 65  ther systems, we
d1e70 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b 20 61   have to check a
d1e80 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 20 4f  t.** runtime.  O
d1e90 6e 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 73  n these latter s
d1ea0 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 45 5f 54  ystems, SQLTIE_T
d1eb0 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c  HREAD_OVERRIDE_L
d1ec0 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69  OCK is.** undefi
d1ed0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ned..**.** This 
d1ee0 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61 6c 6c  variable normall
d1ef0 79 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65  y has file scope
d1f00 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75 72 69   only.  But duri
d1f10 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 65 20 6d  ng testing, we m
d1f20 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c 6f 62  ake.** it a glob
d1f30 61 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20 74  al so that the t
d1f40 65 73 74 20 63 6f 64 65 20 63 61 6e 20 63 68 61  est code can cha
d1f50 6e 67 65 20 69 74 73 20 76 61 6c 75 65 20 69 6e  nge its value in
d1f60 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66 79   order to verify
d1f70 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72 69 67  .** that the rig
d1f80 68 74 20 73 74 75 66 66 20 68 61 70 70 65 6e 73  ht stuff happens
d1f90 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e   in either case.
d1fa0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  .*/.#if SQLITE_T
d1fb0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
d1fc0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
d1fd0 23 20 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  #  ifndef SQLITE
d1fe0 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45  _THREAD_OVERRIDE
d1ff0 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 66 69 6e  _LOCK.#    defin
d2000 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f  e SQLITE_THREAD_
d2010 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 2d 31  OVERRIDE_LOCK -1
d2020 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 69 66 64  .#  endif.#  ifd
d2030 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69  ef SQLITE_TEST.i
d2040 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 72 69  nt threadsOverri
d2050 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b  deEachOthersLock
d2060 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 45 41  s = SQLITE_THREA
d2070 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 3b  D_OVERRIDE_LOCK;
d2080 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 69 63 20  .#  else.static 
d2090 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 72  int threadsOverr
d20a0 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63  ideEachOthersLoc
d20b0 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 45  ks = SQLITE_THRE
d20c0 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b  AD_OVERRIDE_LOCK
d20d0 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69  ;.#  endif.#endi
d20e0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74  f../*.** This st
d20f0 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e  ructure holds in
d2100 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64  formation passed
d2110 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c   into individual
d2120 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 61 64 73   test.** threads
d2130 20 62 79 20 74 68 65 20 74 65 73 74 54 68 72 65   by the testThre
d2140 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f  adLockingBehavio
d2150 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  r() routine..*/.
d2160 73 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73  struct threadTes
d2170 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 20 66 64  tData {.  int fd
d2180 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
d2190 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65 20 6c   /* File to be l
d21a0 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72 75 63  ocked */.  struc
d21b0 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20  t flock lock;   
d21c0 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67    /* The locking
d21d0 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
d21e0 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 20 20 20  int result;     
d21f0 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
d2200 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   of the locking 
d2210 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a  operation */.};.
d2220 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
d2230 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65  ADSAFE && define
d2240 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a 0a  d(__linux__)./*.
d2250 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
d2260 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20   is used as the 
d2270 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 66 6f 72  main routine for
d2280 20 61 20 74 68 72 65 61 64 20 6c 61 75 6e 63 68   a thread launch
d2290 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 54 68 72  ed by.** testThr
d22a0 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69  eadLockingBehavi
d22b0 6f 72 28 29 2e 20 49 74 20 74 65 73 74 73 20 77  or(). It tests w
d22c0 68 65 74 68 65 72 20 74 68 65 20 73 68 61 72 65  hether the share
d22d0 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a  d-lock obtained.
d22e0 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 6e 20 74  ** by the main t
d22f0 68 72 65 61 64 20 69 6e 20 74 65 73 74 54 68 72  hread in testThr
d2300 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69  eadLockingBehavi
d2310 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 73 20 77  or() conflicts w
d2320 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f 74 68 65  ith a.** hypothe
d2330 74 69 63 61 6c 20 77 72 69 74 65 2d 6c 6f 63 6b  tical write-lock
d2340 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 69   obtained by thi
d2350 73 20 74 68 72 65 61 64 20 6f 6e 20 74 68 65 20  s thread on the 
d2360 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  same file..**.**
d2370 20 54 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20   The write-lock 
d2380 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20  is not actually 
d2390 61 63 71 75 69 72 65 64 2c 20 61 73 20 74 68 69  acquired, as thi
d23a0 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  s is not possibl
d23b0 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 66 69 6c  e if .** the fil
d23c0 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 72 65 61  e is open in rea
d23d0 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 73 65 65  d-only mode (see
d23e0 20 74 69 63 6b 65 74 20 23 33 34 37 32 29 2e 0a   ticket #3472)..
d23f0 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */ .static void 
d2400 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65  *threadLockingTe
d2410 73 74 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  st(void *pArg){.
d2420 20 20 73 74 72 75 63 74 20 74 68 72 65 61 64 54    struct threadT
d2430 65 73 74 44 61 74 61 20 2a 70 44 61 74 61 20 3d  estData *pData =
d2440 20 28 73 74 72 75 63 74 20 74 68 72 65 61 64 54   (struct threadT
d2450 65 73 74 44 61 74 61 2a 29 70 41 72 67 3b 0a 20  estData*)pArg;. 
d2460 20 70 44 61 74 61 2d 3e 72 65 73 75 6c 74 20 3d   pData->result =
d2470 20 66 63 6e 74 6c 28 70 44 61 74 61 2d 3e 66 64   fcntl(pData->fd
d2480 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 44 61 74  , F_GETLK, &pDat
d2490 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 65 74 75  a->lock);.  retu
d24a0 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 6e 64 69  rn pArg;.}.#endi
d24b0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45  f /* SQLITE_THRE
d24c0 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65  ADSAFE && define
d24d0 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a  d(__linux__) */.
d24e0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ..#if SQLITE_THR
d24f0 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e  EADSAFE && defin
d2500 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a  ed(__linux__)./*
d2510 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 64 75  .** This procedu
d2520 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64  re attempts to d
d2530 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
d2540 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73 0a   or not threads.
d2550 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20  ** can override 
d2560 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b  each others lock
d2570 73 20 74 68 65 6e 20 73 65 74 73 20 74 68 65 20  s then sets the 
d2580 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65 72 72  .** threadsOverr
d2590 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63  ideEachOthersLoc
d25a0 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70 70 72  ks variable appr
d25b0 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74  opriately..*/.st
d25c0 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 54 68  atic void testTh
d25d0 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76  readLockingBehav
d25e0 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69 67 29  ior(int fd_orig)
d25f0 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 69 6e  {.  int fd;.  in
d2600 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 74  t rc;.  struct t
d2610 68 72 65 61 64 54 65 73 74 44 61 74 61 20 64 3b  hreadTestData d;
d2620 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20  .  struct flock 
d2630 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74  l;.  pthread_t t
d2640 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 28 66 64  ;..  fd = dup(fd
d2650 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 20 66 64  _orig);.  if( fd
d2660 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d  <0 ) return;.  m
d2670 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 73 69 7a  emset(&l, 0, siz
d2680 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e 6c 5f 74  eof(l));.  l.l_t
d2690 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20  ype = F_RDLCK;. 
d26a0 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20   l.l_len = 1;.  
d26b0 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20  l.l_start = 0;. 
d26c0 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45   l.l_whence = SE
d26d0 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 3d 20 66  EK_SET;.  rc = f
d26e0 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c 20 46 5f  cntl(fd_orig, F_
d26f0 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 20 69 66  SETLK, &l);.  if
d2700 28 20 72 63 21 3d 30 20 29 20 72 65 74 75 72 6e  ( rc!=0 ) return
d2710 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 2c 20 30  ;.  memset(&d, 0
d2720 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b 0a 20 20  , sizeof(d));.  
d2730 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 64 2e 6c  d.fd = fd;.  d.l
d2740 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e 6c 6f 63  ock = l;.  d.loc
d2750 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c  k.l_type = F_WRL
d2760 43 4b 3b 0a 20 20 70 74 68 72 65 61 64 5f 63 72  CK;.  pthread_cr
d2770 65 61 74 65 28 26 74 2c 20 30 2c 20 74 68 72 65  eate(&t, 0, thre
d2780 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 2c 20 26  adLockingTest, &
d2790 64 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6a 6f  d);.  pthread_jo
d27a0 69 6e 28 74 2c 20 30 29 3b 0a 20 20 63 6c 6f 73  in(t, 0);.  clos
d27b0 65 28 66 64 29 3b 0a 20 20 69 66 28 20 64 2e 72  e(fd);.  if( d.r
d27c0 65 73 75 6c 74 21 3d 30 20 29 20 72 65 74 75 72  esult!=0 ) retur
d27d0 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f 76 65 72  n;.  threadsOver
d27e0 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
d27f0 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e 6c 5f  cks = (d.lock.l_
d2800 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29 3b 0a  type==F_UNLCK);.
d2810 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
d2820 54 45 5f 54 48 45 52 41 44 53 41 46 45 20 26 26  TE_THERADSAFE &&
d2830 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
d2840 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  __) */../*.** Re
d2850 6c 65 61 73 65 20 61 20 75 6e 69 78 4c 6f 63 6b  lease a unixLock
d2860 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 70  Info structure p
d2870 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61  reviously alloca
d2880 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49  ted by findLockI
d2890 6e 66 6f 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  nfo()..*/.static
d28a0 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4c 6f 63   void releaseLoc
d28b0 6b 49 6e 66 6f 28 73 74 72 75 63 74 20 75 6e 69  kInfo(struct uni
d28c0 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b  xLockInfo *pLock
d28d0 29 7b 0a 20 20 69 66 28 20 70 4c 6f 63 6b 20 29  ){.  if( pLock )
d28e0 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65  {.    pLock->nRe
d28f0 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f  f--;.    if( pLo
d2900 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20  ck->nRef==0 ){. 
d2910 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e       if( pLock->
d2920 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20  pPrev ){.       
d2930 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
d2940 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 4c  pPrev->pNext==pL
d2950 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70  ock );.        p
d2960 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  Lock->pPrev->pNe
d2970 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78  xt = pLock->pNex
d2980 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  t;.      }else{.
d2990 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
d29a0 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f 63 6b 20  lockList==pLock 
d29b0 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 4c  );.        lockL
d29c0 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65  ist = pLock->pNe
d29d0 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  xt;.      }.    
d29e0 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65    if( pLock->pNe
d29f0 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  xt ){.        as
d2a00 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65  sert( pLock->pNe
d2a10 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c 6f 63 6b  xt->pPrev==pLock
d2a20 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63   );.        pLoc
d2a30 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20  k->pNext->pPrev 
d2a40 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 3b 0a  = pLock->pPrev;.
d2a50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
d2a60 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b  lite3_free(pLock
d2a70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
d2a80 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20  /*.** Release a 
d2a90 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75  unixOpenCnt stru
d2aa0 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79  cture previously
d2ab0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69   allocated by fi
d2ac0 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f  ndLockInfo()..*/
d2ad0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c  .static void rel
d2ae0 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 74 72 75  easeOpenCnt(stru
d2af0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
d2b00 70 4f 70 65 6e 29 7b 0a 20 20 69 66 28 20 70 4f  pOpen){.  if( pO
d2b10 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e  pen ){.    pOpen
d2b20 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66  ->nRef--;.    if
d2b30 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30  ( pOpen->nRef==0
d2b40 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   ){.      if( pO
d2b50 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20  pen->pPrev ){.  
d2b60 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
d2b70 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78  pen->pPrev->pNex
d2b80 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20  t==pOpen );.    
d2b90 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76      pOpen->pPrev
d2ba0 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d  ->pNext = pOpen-
d2bb0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 65  >pNext;.      }e
d2bc0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
d2bd0 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d 70  ert( openList==p
d2be0 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20  Open );.        
d2bf0 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e  openList = pOpen
d2c00 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
d2c10 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e  .      if( pOpen
d2c20 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20  ->pNext ){.     
d2c30 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e     assert( pOpen
d2c40 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d  ->pNext->pPrev==
d2c50 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20  pOpen );.       
d2c60 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70   pOpen->pNext->p
d2c70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 50  Prev = pOpen->pP
d2c80 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rev;.      }.   
d2c90 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
d2ca0 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 29  pOpen->aPending)
d2cb0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
d2cc0 66 72 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20 20  free(pOpen);.   
d2cd0 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
d2ce0 47 69 76 65 6e 20 61 20 66 69 6c 65 20 64 65 73  Given a file des
d2cf0 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20  criptor, locate 
d2d00 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64  unixLockInfo and
d2d10 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72   unixOpenCnt str
d2d20 75 63 74 75 72 65 73 20 74 68 61 74 0a 2a 2a 20  uctures that.** 
d2d30 64 65 73 63 72 69 62 65 73 20 74 68 61 74 20 66  describes that f
d2d40 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20  ile descriptor. 
d2d50 20 43 72 65 61 74 65 20 6e 65 77 20 6f 6e 65 73   Create new ones
d2d60 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20   if necessary.  
d2d70 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61  The.** return va
d2d80 6c 75 65 73 20 6d 69 67 68 74 20 62 65 20 75 6e  lues might be un
d2d90 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61  initialized if a
d2da0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  n error occurs..
d2db0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20  **.** Return an 
d2dc0 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f  appropriate erro
d2dd0 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  r code..*/.stati
d2de0 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b 49 6e  c int findLockIn
d2df0 66 6f 28 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  fo(.  unixFile *
d2e00 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20  pFile,          
d2e10 20 20 20 20 20 2f 2a 20 55 6e 69 78 20 66 69 6c       /* Unix fil
d2e20 65 20 77 69 74 68 20 66 69 6c 65 20 64 65 73 63  e with file desc
d2e30 20 75 73 65 64 20 69 6e 20 74 68 65 20 6b 65 79   used in the key
d2e40 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69   */.  struct uni
d2e50 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70 4c 6f  xLockInfo **ppLo
d2e60 63 6b 2c 20 20 2f 2a 20 52 65 74 75 72 6e 20 74  ck,  /* Return t
d2e70 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  he unixLockInfo 
d2e80 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a  structure here *
d2e90 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f  /.  struct unixO
d2ea0 70 65 6e 43 6e 74 20 2a 2a 70 70 4f 70 65 6e 20  penCnt **ppOpen 
d2eb0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65     /* Return the
d2ec0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72   unixOpenCnt str
d2ed0 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 29  ucture here */.)
d2ee0 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
d2ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d2f00 20 20 20 2f 2a 20 53 79 73 74 65 6d 20 63 61 6c     /* System cal
d2f10 6c 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  l return code */
d2f20 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20  .  int fd;      
d2f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d2f40 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
d2f50 73 63 72 69 70 74 6f 72 20 66 6f 72 20 70 46 69  scriptor for pFi
d2f60 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  le */.  struct u
d2f70 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b  nixLockKey lockK
d2f80 65 79 3b 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70  ey;    /* Lookup
d2f90 20 6b 65 79 20 66 6f 72 20 74 68 65 20 75 6e 69   key for the uni
d2fa0 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74  xLockInfo struct
d2fb0 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ure */.  struct 
d2fc0 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49  unixFileId fileI
d2fd0 64 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75  d;      /* Looku
d2fe0 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20 75 6e  p key for the un
d2ff0 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74  ixOpenCnt struct
d3000 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 74 61   */.  struct sta
d3010 74 20 73 74 61 74 62 75 66 3b 20 20 20 20 20 20  t statbuf;      
d3020 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65       /* Low-leve
d3030 6c 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69  l file informati
d3040 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  on */.  struct u
d3050 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f  nixLockInfo *pLo
d3060 63 6b 3b 20 20 20 20 2f 2a 20 43 61 6e 64 69 64  ck;    /* Candid
d3070 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  ate unixLockInfo
d3080 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 74 72   object */.  str
d3090 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  uct unixOpenCnt 
d30a0 2a 70 4f 70 65 6e 3b 20 20 20 20 20 2f 2a 20 43  *pOpen;     /* C
d30b0 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4f 70 65  andidate unixOpe
d30c0 6e 43 6e 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 0a  nCnt object */..
d30d0 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c 65 76    /* Get low-lev
d30e0 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  el information a
d30f0 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 74 68  bout the file th
d3100 61 74 20 77 65 20 63 61 6e 20 75 73 65 64 20 74  at we can used t
d3110 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 61 20  o.  ** create a 
d3120 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f 72 20  unique name for 
d3130 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20  the file..  */. 
d3140 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a   fd = pFile->h;.
d3150 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c    rc = fstat(fd,
d3160 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 69 66   &statbuf);.  if
d3170 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 70  ( rc!=0 ){.    p
d3180 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d3190 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 65 66 20  = errno;.#ifdef 
d31a0 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20 69 66  EOVERFLOW.    if
d31b0 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72  ( pFile->lastErr
d31c0 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 20 29 20  no==EOVERFLOW ) 
d31d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
d31e0 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  LFS;.#endif.    
d31f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
d3200 45 52 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  ERR;.  }..  /* O
d3210 6e 20 4f 53 20 58 20 6f 6e 20 61 6e 20 6d 73 64  n OS X on an msd
d3220 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2c 20 74  os filesystem, t
d3230 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 20  he inode number 
d3240 69 73 20 72 65 70 6f 72 74 65 64 0a 20 20 2a 2a  is reported.  **
d3250 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 66 6f 72   incorrectly for
d3260 20 7a 65 72 6f 2d 73 69 7a 65 20 66 69 6c 65 73   zero-size files
d3270 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 33  .  See ticket #3
d3280 32 36 30 2e 20 20 54 6f 20 77 6f 72 6b 0a 20 20  260.  To work.  
d3290 2a 2a 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70  ** around this p
d32a0 72 6f 62 6c 65 6d 20 28 77 65 20 63 6f 6e 73 69  roblem (we consi
d32b0 64 65 72 20 69 74 20 61 20 62 75 67 20 69 6e 20  der it a bug in 
d32c0 4f 53 20 58 2c 20 6e 6f 74 20 53 51 4c 69 74 65  OS X, not SQLite
d32d0 29 0a 20 20 2a 2a 20 77 65 20 61 6c 77 61 79 73  ).  ** we always
d32e0 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 66 69   increase the fi
d32f0 6c 65 20 73 69 7a 65 20 74 6f 20 31 20 62 79 20  le size to 1 by 
d3300 77 72 69 74 69 6e 67 20 61 20 73 69 6e 67 6c 65  writing a single
d3310 20 62 79 74 65 0a 20 20 2a 2a 20 70 72 69 6f 72   byte.  ** prior
d3320 20 74 6f 20 61 63 63 65 73 73 69 6e 67 20 74 68   to accessing th
d3330 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20  e inode number. 
d3340 20 54 68 65 20 6f 6e 65 20 62 79 74 65 20 77 72   The one byte wr
d3350 69 74 74 65 6e 20 69 73 0a 20 20 2a 2a 20 61 6e  itten is.  ** an
d3360 20 41 53 43 49 49 20 27 53 27 20 63 68 61 72 61   ASCII 'S' chara
d3370 63 74 65 72 20 77 68 69 63 68 20 61 6c 73 6f 20  cter which also 
d3380 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 74 68  happens to be th
d3390 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20 2a  e first byte.  *
d33a0 2a 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20  * in the header 
d33b0 6f 66 20 65 76 65 72 79 20 53 51 4c 69 74 65 20  of every SQLite 
d33c0 64 61 74 61 62 61 73 65 2e 20 20 49 6e 20 74 68  database.  In th
d33d0 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 72 65  is way, if there
d33e0 0a 20 20 2a 2a 20 69 73 20 61 20 72 61 63 65 20  .  ** is a race 
d33f0 63 6f 6e 64 69 74 69 6f 6e 20 73 75 63 68 20 74  condition such t
d3400 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65  hat another thre
d3410 61 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 70  ad has already p
d3420 6f 70 75 6c 61 74 65 64 0a 20 20 2a 2a 20 74 68  opulated.  ** th
d3430 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20  e first page of 
d3440 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 6e 6f  the database, no
d3450 20 64 61 6d 61 67 65 20 69 73 20 64 6f 6e 65 2e   damage is done.
d3460 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 74 61 74  .  */.  if( stat
d3470 62 75 66 2e 73 74 5f 73 69 7a 65 3d 3d 30 20 29  buf.st_size==0 )
d3480 7b 0a 20 20 20 20 77 72 69 74 65 28 66 64 2c 20  {.    write(fd, 
d3490 22 53 22 2c 20 31 29 3b 0a 20 20 20 20 72 63 20  "S", 1);.    rc 
d34a0 3d 20 66 73 74 61 74 28 66 64 2c 20 26 73 74 61  = fstat(fd, &sta
d34b0 74 62 75 66 29 3b 0a 20 20 20 20 69 66 28 20 72  tbuf);.    if( r
d34c0 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 46  c!=0 ){.      pF
d34d0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d34e0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65   errno;.      re
d34f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
d3500 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  R;.    }.  }..  
d3510 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65 79 2c  memset(&lockKey,
d3520 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b   0, sizeof(lockK
d3530 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65 79 2e  ey));.  lockKey.
d3540 66 69 64 2e 64 65 76 20 3d 20 73 74 61 74 62 75  fid.dev = statbu
d3550 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20 4f 53  f.st_dev;.#if OS
d3560 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63 6b 4b  _VXWORKS.  lockK
d3570 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 70 46 69  ey.fid.pId = pFi
d3580 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a 20  le->pId;.#else. 
d3590 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69 6e 6f   lockKey.fid.ino
d35a0 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 69 6e   = statbuf.st_in
d35b0 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  o;.#endif.#if SQ
d35c0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
d35d0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e  && defined(__lin
d35e0 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 68 72 65  ux__).  if( thre
d35f0 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f  adsOverrideEachO
d3600 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b 0a  thersLocks<0 ){.
d3610 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c 6f      testThreadLo
d3620 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66 64  ckingBehavior(fd
d3630 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b 65 79  );.  }.  lockKey
d3640 2e 74 69 64 20 3d 20 74 68 72 65 61 64 73 4f 76  .tid = threadsOv
d3650 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
d3660 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74 68 72  Locks ? 0 : pthr
d3670 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e 64  ead_self();.#end
d3680 69 66 0a 20 20 66 69 6c 65 49 64 20 3d 20 6c 6f  if.  fileId = lo
d3690 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69 66 28  ckKey.fid;.  if(
d36a0 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a 20 20   ppLock!=0 ){.  
d36b0 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c 69    pLock = lockLi
d36c0 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70  st;.    while( p
d36d0 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28 26  Lock && memcmp(&
d36e0 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63 6b 2d  lockKey, &pLock-
d36f0 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65 6f 66  >lockKey, sizeof
d3700 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a 20 20  (lockKey)) ){.  
d3710 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63      pLock = pLoc
d3720 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  k->pNext;.    }.
d3730 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d 30      if( pLock==0
d3740 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20   ){.      pLock 
d3750 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
d3760 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63 6b 29  ( sizeof(*pLock)
d3770 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c   );.      if( pL
d3780 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ock==0 ){.      
d3790 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
d37a0 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  MEM;.        got
d37b0 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69  o exit_findlocki
d37c0 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nfo;.      }.   
d37d0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65     pLock->lockKe
d37e0 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a 20 20 20  y = lockKey;.   
d37f0 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 20 3d     pLock->nRef =
d3800 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d   1;.      pLock-
d3810 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  >cnt = 0;.      
d3820 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20  pLock->locktype 
d3830 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b  = 0;.      pLock
d3840 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69  ->pNext = lockLi
d3850 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d  st;.      pLock-
d3860 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  >pPrev = 0;.    
d3870 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73 74 20 29    if( lockList )
d3880 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50 72 65 76   lockList->pPrev
d3890 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 20   = pLock;.      
d38a0 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b  lockList = pLock
d38b0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
d38c0 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2b 2b     pLock->nRef++
d38d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4c  ;.    }.    *ppL
d38e0 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d  ock = pLock;.  }
d38f0 0a 20 20 69 66 28 20 70 70 4f 70 65 6e 21 3d 30  .  if( ppOpen!=0
d3900 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20   ){.    pOpen = 
d3910 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68  openList;.    wh
d3920 69 6c 65 28 20 70 4f 70 65 6e 20 26 26 20 6d 65  ile( pOpen && me
d3930 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c 20 26 70  mcmp(&fileId, &p
d3940 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c 20 73 69  Open->fileId, si
d3950 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 20 29 7b  zeof(fileId)) ){
d3960 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 70  .      pOpen = p
d3970 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Open->pNext;.   
d3980 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e   }.    if( pOpen
d3990 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4f 70  ==0 ){.      pOp
d39a0 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  en = sqlite3_mal
d39b0 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4f 70  loc( sizeof(*pOp
d39c0 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  en) );.      if(
d39d0 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20   pOpen==0 ){.   
d39e0 20 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b       releaseLock
d39f0 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20 20  Info(pLock);.   
d3a00 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
d3a10 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
d3a20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f  goto exit_findlo
d3a30 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a  ckinfo;.      }.
d3a40 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66 69 6c        pOpen->fil
d3a50 65 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a 20 20  eId = fileId;.  
d3a60 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 20      pOpen->nRef 
d3a70 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e  = 1;.      pOpen
d3a80 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20  ->nLock = 0;.   
d3a90 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69     pOpen->nPendi
d3aa0 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f  ng = 0;.      pO
d3ab0 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20  pen->aPending = 
d3ac0 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  0;.      pOpen->
d3ad0 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 73 74  pNext = openList
d3ae0 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70  ;.      pOpen->p
d3af0 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20  Prev = 0;.      
d3b00 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 6f  if( openList ) o
d3b10 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d  penList->pPrev =
d3b20 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f 70   pOpen;.      op
d3b30 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b 0a  enList = pOpen;.
d3b40 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
d3b50 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 53 65 6d       pOpen->pSem
d3b60 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 70   = NULL;.      p
d3b70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b 30  Open->aSemName[0
d3b80 5d 20 3d 20 27 5c 30 27 3b 0a 23 65 6e 64 69 66  ] = '\0';.#endif
d3b90 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
d3ba0 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b 3b    pOpen->nRef++;
d3bb0 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f 70  .    }.    *ppOp
d3bc0 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d 0a  en = pOpen;.  }.
d3bd0 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e  .exit_findlockin
d3be0 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  fo:.  return rc;
d3bf0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  .}../*.** If we 
d3c00 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  are currently in
d3c10 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72   a different thr
d3c20 65 61 64 20 74 68 61 6e 20 74 68 65 20 74 68 72  ead than the thr
d3c30 65 61 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ead that the.** 
d3c40 75 6e 69 78 46 69 6c 65 20 61 72 67 75 6d 65 6e  unixFile argumen
d3c50 74 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68  t belongs to, th
d3c60 65 6e 20 74 72 61 6e 73 66 65 72 20 6f 77 6e 65  en transfer owne
d3c70 72 73 68 69 70 20 6f 66 20 74 68 65 20 75 6e 69  rship of the uni
d3c80 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f  xFile.** over to
d3c90 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72   the current thr
d3ca0 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69  ead..**.** A uni
d3cb0 78 46 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77  xFile is only ow
d3cc0 6e 65 64 20 62 79 20 61 20 74 68 72 65 61 64 20  ned by a thread 
d3cd0 6f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 20  on systems that 
d3ce0 75 73 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73  use LinuxThreads
d3cf0 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 69  ..**.** Ownershi
d3d00 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f 6e  p transfer is on
d3d10 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74 68  ly allowed if th
d3d20 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63 75  e unixFile is cu
d3d30 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64  rrently unlocked
d3d40 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69 78  ..** If the unix
d3d50 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 61  File is locked a
d3d60 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70 20  nd an ownership 
d3d70 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20 72  is wrong, then r
d3d80 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  eturn.** SQLITE_
d3d90 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45 5f  MISUSE.  SQLITE_
d3da0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  OK is returned i
d3db0 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72  f everything wor
d3dc0 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ks..*/.#if SQLIT
d3dd0 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
d3de0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
d3df0 5f 29 0a 73 74 61 74 69 63 20 69 6e 74 20 74 72  _).static int tr
d3e00 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28  ansferOwnership(
d3e10 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29  unixFile *pFile)
d3e20 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 74  {.  int rc;.  pt
d3e30 68 72 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a 20  hread_t hSelf;. 
d3e40 20 69 66 28 20 74 68 72 65 61 64 73 4f 76 65 72   if( threadsOver
d3e50 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
d3e60 63 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 77  cks ){.    /* Ow
d3e70 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72  nership transfer
d3e80 73 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20  s not needed on 
d3e90 74 68 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a 20  this system */. 
d3ea0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d3eb0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c 66  _OK;.  }.  hSelf
d3ec0 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28   = pthread_self(
d3ed0 29 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61 64  );.  if( pthread
d3ee0 5f 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74 69  _equal(pFile->ti
d3ef0 64 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20 20 20  d, hSelf) ){.   
d3f00 20 2f 2a 20 57 65 20 61 72 65 20 73 74 69 6c 6c   /* We are still
d3f10 20 69 6e 20 74 68 65 20 73 61 6d 65 20 74 68 72   in the same thr
d3f20 65 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41  ead */.    OSTRA
d3f30 43 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65 72  CE1("No-transfer
d3f40 2c 20 73 61 6d 65 20 74 68 72 65 61 64 5c 6e 22  , same thread\n"
d3f50 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
d3f60 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
d3f70 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d3f80 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20  pe!=NO_LOCK ){. 
d3f90 20 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20     /* We cannot 
d3fa0 63 68 61 6e 67 65 20 6f 77 6e 65 72 73 68 69 70  change ownership
d3fb0 20 77 68 69 6c 65 20 77 65 20 61 72 65 20 68 6f   while we are ho
d3fc0 6c 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a 2f  lding a lock! */
d3fd0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d3fe0 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
d3ff0 20 4f 53 54 52 41 43 45 34 28 22 54 72 61 6e 73   OSTRACE4("Trans
d4000 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66  fer ownership of
d4010 20 25 64 20 66 72 6f 6d 20 25 64 20 74 6f 20 25   %d from %d to %
d4020 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
d4030 20 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c    pFile->h, pFil
d4040 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b 0a  e->tid, hSelf);.
d4050 20 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20 68    pFile->tid = h
d4060 53 65 6c 66 3b 0a 20 20 69 66 20 28 70 46 69 6c  Self;.  if (pFil
d4070 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c 4c  e->pLock != NULL
d4080 29 20 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c  ) {.    releaseL
d4090 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70  ockInfo(pFile->p
d40a0 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20  Lock);.    rc = 
d40b0 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 69  findLockInfo(pFi
d40c0 6c 65 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63  le, &pFile->pLoc
d40d0 6b 2c 20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41  k, 0);.    OSTRA
d40e0 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20  CE5("LOCK    %d 
d40f0 69 73 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29  is now %s(%s,%d)
d4100 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20  \n", pFile->h,. 
d4110 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79            lockty
d4120 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f  peName(pFile->lo
d4130 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20  cktype),.       
d4140 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65      locktypeName
d4150 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c  (pFile->pLock->l
d4160 6f 63 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d  ocktype), pFile-
d4170 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20  >pLock->cnt);.  
d4180 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
d4190 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75   else {.    retu
d41a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
d41b0 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66  }.}.#else  /* if
d41c0 20 6e 6f 74 20 53 51 4c 49 54 45 5f 54 48 52 45   not SQLITE_THRE
d41d0 41 44 53 41 46 45 20 2a 2f 0a 20 20 2f 2a 20 4f  ADSAFE */.  /* O
d41e0 6e 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65  n single-threade
d41f0 64 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73  d builds, owners
d4200 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20  hip transfer is 
d4210 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66  a no-op */.# def
d4220 69 6e 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65  ine transferOwne
d4230 72 73 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f  rship(X) SQLITE_
d4240 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  OK.#endif /* SQL
d4250 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a  ITE_THREADSAFE *
d4260 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  /.../*.** This r
d4270 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
d4280 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45   there is a RESE
d4290 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f  RVED lock held o
d42a0 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  n the specified.
d42b0 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20  ** file by this 
d42c0 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
d42d0 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20  cess. If such a 
d42e0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65  lock is held, se
d42f0 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f  t *pResOut.** to
d4300 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75   a non-zero valu
d4310 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65  e otherwise *pRe
d4320 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a  sOut is set to z
d4330 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ero.  The return
d4340 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74   value.** is set
d4350 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e   to SQLITE_OK un
d4360 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f  less an I/O erro
d4370 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
d4380 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a  lock checking..*
d4390 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
d43a0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
d43b0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d43c0 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
d43d0 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
d43e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
d43f0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20  reserved = 0;.  
d4400 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d4410 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d4420 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ..  SimulateIOEr
d4430 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
d4440 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45  TE_IOERR_CHECKRE
d4450 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a  SERVEDLOCK; );..
d4460 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
d4470 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  );.  unixEnterMu
d4480 74 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 73  tex(); /* Becaus
d4490 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69  e pFile->pLock i
d44a0 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20  s shared across 
d44b0 74 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a  threads */..  /*
d44c0 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65   Check if a thre
d44d0 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65  ad in this proce
d44e0 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20  ss holds such a 
d44f0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46  lock */.  if( pF
d4500 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  ile->pLock->lock
d4510 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
d4520 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64   ){.    reserved
d4530 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 1;.  }..  /* 
d4540 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66  Otherwise see if
d4550 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
d4560 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20  ess holds it..  
d4570 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76  */.  if( !reserv
d4580 65 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  ed ){.    struct
d4590 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20   flock lock;.   
d45a0 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d   lock.l_whence =
d45b0 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c   SEEK_SET;.    l
d45c0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45  ock.l_start = RE
d45d0 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20  SERVED_BYTE;.   
d45e0 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b   lock.l_len = 1;
d45f0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65  .    lock.l_type
d4600 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20   = F_WRLCK;.    
d4610 69 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28  if (-1 == fcntl(
d4620 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c  pFile->h, F_GETL
d4630 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20  K, &lock)) {.   
d4640 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
d4650 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20  errno;.      rc 
d4660 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
d4670 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
d4680 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
d4690 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f  _CHECKRESERVEDLO
d46a0 43 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65  CK);.      pFile
d46b0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
d46c0 72 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65  rrno;.    } else
d46d0 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65   if( lock.l_type
d46e0 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20  !=F_UNLCK ){.   
d46f0 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b     reserved = 1;
d4700 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20  .    }.  }.  .  
d4710 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
d4720 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45  ;.  OSTRACE4("TE
d4730 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
d4740 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
d4750 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b  , rc, reserved);
d4760 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72  ..  *pResOut = r
d4770 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72  eserved;.  retur
d4780 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
d4790 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74  ock the file wit
d47a0 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69  h the lock speci
d47b0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
d47c0 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65  r locktype - one
d47d0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  .** of the follo
d47e0 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  wing:.**.**     
d47f0 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a  (1) SHARED_LOCK.
d4800 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52  **     (2) RESER
d4810 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  VED_LOCK.**     
d4820 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  (3) PENDING_LOCK
d4830 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c  .**     (4) EXCL
d4840 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a  USIVE_LOCK.**.**
d4850 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20   Sometimes when 
d4860 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c  requesting one l
d4870 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74  ock state, addit
d4880 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65  ional lock state
d4890 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65  s.** are inserte
d48a0 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54  d in between.  T
d48b0 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74  he locking might
d48c0 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20   fail on one of 
d48d0 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61  the later.** tra
d48e0 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67  nsitions leaving
d48f0 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20   the lock state 
d4900 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77  different from w
d4910 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62  hat it started b
d4920 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72  ut.** still shor
d4930 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20  t of its goal.  
d4940 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  The following ch
d4950 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c  art shows the al
d4960 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74  lowed.** transit
d4970 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73  ions and the ins
d4980 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61  erted intermedia
d4990 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a  te states:.**.**
d49a0 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20      UNLOCKED -> 
d49b0 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41  SHARED.**    SHA
d49c0 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a  RED -> RESERVED.
d49d0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
d49e0 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
d49f0 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53  LUSIVE.**    RES
d4a00 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  ERVED -> (PENDIN
d4a10 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
d4a20 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e  **    PENDING ->
d4a30 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a   EXCLUSIVE.**.**
d4a40 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
d4a50 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65  ll only increase
d4a60 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68   a lock.  Use th
d4a70 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63  e sqlite3OsUnloc
d4a80 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74  k().** routine t
d4a90 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e  o lower a lockin
d4aa0 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74  g level..*/.stat
d4ab0 69 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b 28  ic int unixLock(
d4ac0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d4ad0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b  , int locktype){
d4ae0 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
d4af0 69 6e 67 20 64 65 73 63 72 69 62 65 73 20 74 68  ing describes th
d4b00 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
d4b10 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20   of the various 
d4b20 6c 6f 63 6b 73 20 61 6e 64 0a 20 20 2a 2a 20 6c  locks and.  ** l
d4b30 6f 63 6b 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  ock transitions 
d4b40 69 6e 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20  in terms of the 
d4b50 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 73  POSIX advisory s
d4b60 68 61 72 65 64 20 61 6e 64 20 65 78 63 6c 75 73  hared and exclus
d4b70 69 76 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70 72  ive.  ** lock pr
d4b80 69 6d 69 74 69 76 65 73 20 28 63 61 6c 6c 65 64  imitives (called
d4b90 20 72 65 61 64 2d 6c 6f 63 6b 73 20 61 6e 64 20   read-locks and 
d4ba0 77 72 69 74 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f  write-locks belo
d4bb0 77 2c 20 74 6f 20 61 76 6f 69 64 0a 20 20 2a 2a  w, to avoid.  **
d4bc0 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20   confusion with 
d4bd0 53 51 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 6d 65  SQLite lock name
d4be0 73 29 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68  s). The algorith
d4bf0 6d 73 20 61 72 65 20 63 6f 6d 70 6c 69 63 61 74  ms are complicat
d4c00 65 64 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79  ed.  ** slightly
d4c10 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20   in order to be 
d4c20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20  compatible with 
d4c30 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 20  windows systems 
d4c40 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0a 20  simultaneously. 
d4c50 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68   ** accessing th
d4c60 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
d4c70 66 69 6c 65 2c 20 69 6e 20 63 61 73 65 20 74 68  file, in case th
d4c80 61 74 20 69 73 20 65 76 65 72 20 72 65 71 75 69  at is ever requi
d4c90 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53  red..  **.  ** S
d4ca0 79 6d 62 6f 6c 73 20 64 65 66 69 6e 65 64 20 69  ymbols defined i
d4cb0 6e 20 6f 73 2e 68 20 69 6e 64 65 6e 74 69 66 79  n os.h indentify
d4cc0 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79   the 'pending by
d4cd0 74 65 27 20 61 6e 64 20 74 68 65 20 27 72 65 73  te' and the 'res
d4ce0 65 72 76 65 64 0a 20 20 2a 2a 20 62 79 74 65 27  erved.  ** byte'
d4cf0 2c 20 65 61 63 68 20 73 69 6e 67 6c 65 20 62 79  , each single by
d4d00 74 65 73 20 61 74 20 77 65 6c 6c 20 6b 6e 6f 77  tes at well know
d4d10 6e 20 6f 66 66 73 65 74 73 2c 20 61 6e 64 20 74  n offsets, and t
d4d20 68 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a  he 'shared byte.
d4d30 20 20 2a 2a 20 72 61 6e 67 65 27 2c 20 61 20 72    ** range', a r
d4d40 61 6e 67 65 20 6f 66 20 35 31 30 20 62 79 74 65  ange of 510 byte
d4d50 73 20 61 74 20 61 20 77 65 6c 6c 20 6b 6e 6f 77  s at a well know
d4d60 6e 20 6f 66 66 73 65 74 2e 0a 20 20 2a 2a 0a 20  n offset..  **. 
d4d70 20 2a 2a 20 54 6f 20 6f 62 74 61 69 6e 20 61 20   ** To obtain a 
d4d80 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 61 20 72  SHARED lock, a r
d4d90 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61  ead-lock is obta
d4da0 69 6e 65 64 20 6f 6e 20 74 68 65 20 27 70 65 6e  ined on the 'pen
d4db0 64 69 6e 67 0a 20 20 2a 2a 20 62 79 74 65 27 2e  ding.  ** byte'.
d4dc0 20 20 49 66 20 74 68 69 73 20 69 73 20 73 75 63    If this is suc
d4dd0 63 65 73 73 66 75 6c 2c 20 61 20 72 61 6e 64 6f  cessful, a rando
d4de0 6d 20 62 79 74 65 20 66 72 6f 6d 20 74 68 65 20  m byte from the 
d4df0 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a  'shared byte.  *
d4e00 2a 20 72 61 6e 67 65 27 20 69 73 20 72 65 61 64  * range' is read
d4e10 2d 6c 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 20  -locked and the 
d4e20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 27 70 65 6e  lock on the 'pen
d4e30 64 69 6e 67 20 62 79 74 65 27 20 72 65 6c 65 61  ding byte' relea
d4e40 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  sed..  **.  ** A
d4e50 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c   process may onl
d4e60 79 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52  y obtain a RESER
d4e70 56 45 44 20 6c 6f 63 6b 20 61 66 74 65 72 20 69  VED lock after i
d4e80 74 20 68 61 73 20 61 20 53 48 41 52 45 44 20 6c  t has a SHARED l
d4e90 6f 63 6b 2e 0a 20 20 2a 2a 20 41 20 52 45 53 45  ock..  ** A RESE
d4ea0 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 69 6d 70  RVED lock is imp
d4eb0 6c 65 6d 65 6e 74 65 64 20 62 79 20 67 72 61 62  lemented by grab
d4ec0 62 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63  bing a write-loc
d4ed0 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 27 72  k on the.  ** 'r
d4ee0 65 73 65 72 76 65 64 20 62 79 74 65 27 2e 20 0a  eserved byte'. .
d4ef0 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63    **.  ** A proc
d4f00 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74  ess may only obt
d4f10 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  ain a PENDING lo
d4f20 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 20  ck after it has 
d4f30 6f 62 74 61 69 6e 65 64 20 61 0a 20 20 2a 2a 20  obtained a.  ** 
d4f40 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 41 20 50  SHARED lock. A P
d4f50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 69  ENDING lock is i
d4f60 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62  mplemented by ob
d4f70 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d  taining a write-
d4f80 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65  lock.  ** on the
d4f90 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 2e   'pending byte'.
d4fa0 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68   This ensures th
d4fb0 61 74 20 6e 6f 20 6e 65 77 20 53 48 41 52 45 44  at no new SHARED
d4fc0 20 6c 6f 63 6b 73 20 63 61 6e 20 62 65 0a 20 20   locks can be.  
d4fd0 2a 2a 20 6f 62 74 61 69 6e 65 64 2c 20 62 75 74  ** obtained, but
d4fe0 20 65 78 69 73 74 69 6e 67 20 53 48 41 52 45 44   existing SHARED
d4ff0 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c 6c 6f 77   locks are allow
d5000 65 64 20 74 6f 20 70 65 72 73 69 73 74 2e 20 41  ed to persist. A
d5010 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 64 6f   process.  ** do
d5020 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6f  es not have to o
d5030 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44  btain a RESERVED
d5040 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 77 61 79   lock on the way
d5050 20 74 6f 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f   to a PENDING lo
d5060 63 6b 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72  ck..  ** This pr
d5070 6f 70 65 72 74 79 20 69 73 20 75 73 65 64 20 62  operty is used b
d5080 79 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d 20  y the algorithm 
d5090 66 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b  for rolling back
d50a0 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a   a journal file.
d50b0 20 20 2a 2a 20 61 66 74 65 72 20 61 20 63 72 61    ** after a cra
d50c0 73 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e  sh..  **.  ** An
d50d0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c   EXCLUSIVE lock,
d50e0 20 6f 62 74 61 69 6e 65 64 20 61 66 74 65 72 20   obtained after 
d50f0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69  a PENDING lock i
d5100 73 20 68 65 6c 64 2c 20 69 73 0a 20 20 2a 2a 20  s held, is.  ** 
d5110 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f  implemented by o
d5120 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65  btaining a write
d5130 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 65 6e 74  -lock on the ent
d5140 69 72 65 20 27 73 68 61 72 65 64 20 62 79 74 65  ire 'shared byte
d5150 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2e 20 53 69  .  ** range'. Si
d5160 6e 63 65 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f  nce all other lo
d5170 63 6b 73 20 72 65 71 75 69 72 65 20 61 20 72 65  cks require a re
d5180 61 64 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f  ad-lock on one o
d5190 66 20 74 68 65 20 62 79 74 65 73 0a 20 20 2a 2a  f the bytes.  **
d51a0 20 77 69 74 68 69 6e 20 74 68 69 73 20 72 61 6e   within this ran
d51b0 67 65 2c 20 74 68 69 73 20 65 6e 73 75 72 65 73  ge, this ensures
d51c0 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 6c   that no other l
d51d0 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e  ocks are held on
d51e0 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
d51f0 73 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  se. .  **.  ** T
d5200 68 65 20 72 65 61 73 6f 6e 20 61 20 73 69 6e 67  he reason a sing
d5210 6c 65 20 62 79 74 65 20 63 61 6e 6e 6f 74 20 62  le byte cannot b
d5220 65 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f  e used instead o
d5230 66 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79  f the 'shared by
d5240 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69  te.  ** range' i
d5250 73 20 74 68 61 74 20 73 6f 6d 65 20 76 65 72 73  s that some vers
d5260 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20  ions of windows 
d5270 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72  do not support r
d5280 65 61 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a 20 20  ead-locks. By.  
d5290 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 20 72 61 6e  ** locking a ran
d52a0 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 61 20  dom byte from a 
d52b0 72 61 6e 67 65 2c 20 63 6f 6e 63 75 72 72 65 6e  range, concurren
d52c0 74 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d  t SHARED locks m
d52d0 61 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 65 76  ay exist.  ** ev
d52e0 65 6e 20 69 66 20 74 68 65 20 6c 6f 63 6b 69 6e  en if the lockin
d52f0 67 20 70 72 69 6d 69 74 69 76 65 20 75 73 65 64  g primitive used
d5300 20 69 73 20 61 6c 77 61 79 73 20 61 20 77 72 69   is always a wri
d5310 74 65 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  te-lock..  */.  
d5320 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
d5330 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  OK;.  unixFile *
d5340 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d5350 65 2a 29 69 64 3b 0a 20 20 73 74 72 75 63 74 20  e*)id;.  struct 
d5360 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c  unixLockInfo *pL
d5370 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f  ock = pFile->pLo
d5380 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f  ck;.  struct flo
d5390 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 73  ck lock;.  int s
d53a0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  ;..  assert( pFi
d53b0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37  le );.  OSTRACE7
d53c0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
d53d0 77 61 73 20 25 73 28 25 73 2c 25 64 29 20 70 69  was %s(%s,%d) pi
d53e0 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d=%d\n", pFile->
d53f0 68 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 70  h,.      locktyp
d5400 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c  eName(locktype),
d5410 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46   locktypeName(pF
d5420 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a  ile->locktype),.
d5430 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61        locktypeNa
d5440 6d 65 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79  me(pLock->lockty
d5450 70 65 29 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20  pe), pLock->cnt 
d5460 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20  , getpid());..  
d5470 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
d5480 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66  lready a lock of
d5490 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f   this type or mo
d54a0 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f  re restrictive o
d54b0 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46  n the.  ** unixF
d54c0 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e  ile, do nothing.
d54d0 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65   Don't use the e
d54e0 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61  nd_lock: exit pa
d54f0 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78  th, as.  ** unix
d5500 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73  EnterMutex() has
d5510 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  n't been called 
d5520 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  yet..  */.  if( 
d5530 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
d5540 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
d5550 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20   OSTRACE3("LOCK 
d5560 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72     %d %s ok (alr
d5570 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70  eady held)\n", p
d5580 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
d5590 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d       locktypeNam
d55a0 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20  e(locktype));.  
d55b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d55c0 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  OK;.  }..  /* Ma
d55d0 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b  ke sure the lock
d55e0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20  ing sequence is 
d55f0 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61  correct.  */.  a
d5600 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
d5610 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20  cktype!=NO_LOCK 
d5620 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  || locktype==SHA
d5630 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  RED_LOCK );.  as
d5640 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d  sert( locktype!=
d5650 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a  PENDING_LOCK );.
d5660 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
d5670 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe!=RESERVED_LOC
d5680 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  K || pFile->lock
d5690 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
d56a0 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20  K );..  /* This 
d56b0 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 20  mutex is needed 
d56c0 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70  because pFile->p
d56d0 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 61  Lock is shared a
d56e0 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20  cross threads.  
d56f0 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  */.  unixEnterMu
d5700 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  tex();..  /* Mak
d5710 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65  e sure the curre
d5720 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74  nt thread owns t
d5730 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20  he pFile..  */. 
d5740 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77   rc = transferOw
d5750 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a  nership(pFile);.
d5760 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
d5770 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c  _OK ){.    unixL
d5780 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20  eaveMutex();.   
d5790 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
d57a0 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d    pLock = pFile-
d57b0 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66  >pLock;..  /* If
d57c0 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 73 69   some thread usi
d57d0 6e 67 20 74 68 69 73 20 50 49 44 20 68 61 73 20  ng this PID has 
d57e0 61 20 6c 6f 63 6b 20 76 69 61 20 61 20 64 69 66  a lock via a dif
d57f0 66 65 72 65 6e 74 20 75 6e 69 78 46 69 6c 65 2a  ferent unixFile*
d5800 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68 61  .  ** handle tha
d5810 74 20 70 72 65 63 6c 75 64 65 73 20 74 68 65 20  t precludes the 
d5820 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2c 20  requested lock, 
d5830 72 65 74 75 72 6e 20 42 55 53 59 2e 0a 20 20 2a  return BUSY..  *
d5840 2f 0a 20 20 69 66 28 20 28 70 46 69 6c 65 2d 3e  /.  if( (pFile->
d5850 6c 6f 63 6b 74 79 70 65 21 3d 70 4c 6f 63 6b 2d  locktype!=pLock-
d5860 3e 6c 6f 63 6b 74 79 70 65 20 26 26 20 0a 20 20  >locktype && .  
d5870 20 20 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e          (pLock->
d5880 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e  locktype>=PENDIN
d5890 47 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79  G_LOCK || lockty
d58a0 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29  pe>SHARED_LOCK))
d58b0 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  .  ){.    rc = S
d58c0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
d58d0 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20  goto end_lock;. 
d58e0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 53 48   }..  /* If a SH
d58f0 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72 65 71  ARED lock is req
d5900 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f 6d 65  uested, and some
d5910 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68   thread using th
d5920 69 73 20 50 49 44 20 61 6c 72 65 61 64 79 0a 20  is PID already. 
d5930 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52 45 44   ** has a SHARED
d5940 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c 6f 63   or RESERVED loc
d5950 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e  k, then incremen
d5960 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  t reference coun
d5970 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65 74 75  ts and.  ** retu
d5980 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20  rn SQLITE_OK..  
d5990 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
d59a0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
d59b0 26 20 0a 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d  & .      (pLock-
d59c0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  >locktype==SHARE
d59d0 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d  D_LOCK || pLock-
d59e0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52  >locktype==RESER
d59f0 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20  VED_LOCK) ){.   
d5a00 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
d5a10 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
d5a20 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  ;.    assert( pF
d5a30 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30  ile->locktype==0
d5a40 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
d5a50 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a  pLock->cnt>0 );.
d5a60 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d5a70 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43  ype = SHARED_LOC
d5a80 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e  K;.    pLock->cn
d5a90 74 2b 2b 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  t++;.    pFile->
d5aa0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a  pOpen->nLock++;.
d5ab0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63      goto end_loc
d5ac0 6b 3b 0a 20 20 7d 0a 0a 20 20 6c 6f 63 6b 2e 6c  k;.  }..  lock.l
d5ad0 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 0a 20 20 6c 6f  _len = 1L;..  lo
d5ae0 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45  ck.l_whence = SE
d5af0 45 4b 5f 53 45 54 3b 0a 0a 20 20 2f 2a 20 41 20  EK_SET;..  /* A 
d5b00 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20  PENDING lock is 
d5b10 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20 61 63  needed before ac
d5b20 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44  quiring a SHARED
d5b30 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65   lock and before
d5b40 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20  .  ** acquiring 
d5b50 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
d5b60 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41 52  k.  For the SHAR
d5b70 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e  ED lock, the PEN
d5b80 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62  DING will.  ** b
d5b90 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f  e released..  */
d5ba0 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
d5bb0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20  =SHARED_LOCK .  
d5bc0 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65      || (locktype
d5bd0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
d5be0 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   && pFile->lockt
d5bf0 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  ype<PENDING_LOCK
d5c00 29 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e  ).  ){.    lock.
d5c10 6c 5f 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74 79  l_type = (lockty
d5c20 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 3f  pe==SHARED_LOCK?
d5c30 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b 29  F_RDLCK:F_WRLCK)
d5c40 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61  ;.    lock.l_sta
d5c50 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54  rt = PENDING_BYT
d5c60 45 3b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c  E;.    s = fcntl
d5c70 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54  (pFile->h, F_SET
d5c80 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20  LK, &lock);.    
d5c90 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20  if( s==(-1) ){. 
d5ca0 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20       int tErrno 
d5cb0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72  = errno;.      r
d5cc0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d5cd0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d5ce0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d5cf0 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  RR_LOCK);.      
d5d00 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
d5d10 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20  R(rc) ){.       
d5d20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d5d30 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d5d40 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65    }.      goto e
d5d50 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20  nd_lock;.    }. 
d5d60 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e   }...  /* If con
d5d70 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69  trol gets to thi
d5d80 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63  s point, then ac
d5d90 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20  tually go ahead 
d5da0 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70  and make.  ** op
d5db0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63  erating system c
d5dc0 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65  alls for the spe
d5dd0 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a  cified lock..  *
d5de0 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  /.  if( locktype
d5df0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  ==SHARED_LOCK ){
d5e00 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20  .    int tErrno 
d5e10 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = 0;.    assert(
d5e20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29   pLock->cnt==0 )
d5e30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c  ;.    assert( pL
d5e40 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30  ock->locktype==0
d5e50 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20   );..    /* Now 
d5e60 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63  get the read-loc
d5e70 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  k */.    lock.l_
d5e80 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46  start = SHARED_F
d5e90 49 52 53 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c  IRST;.    lock.l
d5ea0 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49  _len = SHARED_SI
d5eb0 5a 45 3b 0a 20 20 20 20 69 66 28 20 28 73 20 3d  ZE;.    if( (s =
d5ec0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
d5ed0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
d5ee0 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  )==(-1) ){.     
d5ef0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
d5f00 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72  .    }.    /* Dr
d5f10 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  op the temporary
d5f20 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f   PENDING lock */
d5f30 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  .    lock.l_star
d5f40 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  t = PENDING_BYTE
d5f50 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e  ;.    lock.l_len
d5f60 20 3d 20 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e   = 1L;.    lock.
d5f70 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b  l_type = F_UNLCK
d5f80 3b 0a 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28  ;.    if( fcntl(
d5f90 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c  pFile->h, F_SETL
d5fa0 4b 2c 20 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a  K, &lock)!=0 ){.
d5fb0 20 20 20 20 20 20 69 66 28 20 73 20 21 3d 20 2d        if( s != -
d5fc0 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  1 ){.        /* 
d5fd0 54 68 69 73 20 63 6f 75 6c 64 20 68 61 70 70 65  This could happe
d5fe0 6e 20 77 69 74 68 20 61 20 6e 65 74 77 6f 72 6b  n with a network
d5ff0 20 6d 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20   mount */.      
d6000 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f    tErrno = errno
d6010 3b 20 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ; .        rc = 
d6020 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d6030 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d6040 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  , SQLITE_IOERR_U
d6050 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20  NLOCK); .       
d6060 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d6070 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20  OR(rc) ){.      
d6080 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d6090 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
d60a0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
d60b0 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a   goto end_lock;.
d60c0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
d60d0 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b    if( s==(-1) ){
d60e0 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
d60f0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d6100 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d6110 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29  LITE_IOERR_LOCK)
d6120 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c  ;.      if( IS_L
d6130 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
d6140 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
d6150 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d6160 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  no;.      }.    
d6170 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69  }else{.      pFi
d6180 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53  le->locktype = S
d6190 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20  HARED_LOCK;.    
d61a0 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e    pFile->pOpen->
d61b0 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70  nLock++;.      p
d61c0 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20  Lock->cnt = 1;. 
d61d0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
d61e0 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
d61f0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f  SIVE_LOCK && pLo
d6200 63 6b 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20  ck->cnt>1 ){.   
d6210 20 2f 2a 20 57 65 20 61 72 65 20 74 72 79 69 6e   /* We are tryin
d6220 67 20 66 6f 72 20 61 6e 20 65 78 63 6c 75 73 69  g for an exclusi
d6230 76 65 20 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74  ve lock but anot
d6240 68 65 72 20 74 68 72 65 61 64 20 69 6e 20 74 68  her thread in th
d6250 69 73 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 70  is.    ** same p
d6260 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20  rocess is still 
d6270 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64  holding a shared
d6280 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63   lock. */.    rc
d6290 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
d62a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
d62b0 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73 20  The request was 
d62c0 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 6f  for a RESERVED o
d62d0 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  r EXCLUSIVE lock
d62e0 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20  .  It is.    ** 
d62f0 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65  assumed that the
d6300 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20 6f  re is a SHARED o
d6310 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f  r greater lock o
d6320 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a  n the file.    *
d6330 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a  * already..    *
d6340 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 21  /.    assert( 0!
d6350 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  =pFile->locktype
d6360 20 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74   );.    lock.l_t
d6370 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20  ype = F_WRLCK;. 
d6380 20 20 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74     switch( lockt
d6390 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73  ype ){.      cas
d63a0 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a  e RESERVED_LOCK:
d63b0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
d63c0 73 74 61 72 74 20 3d 20 52 45 53 45 52 56 45 44  start = RESERVED
d63d0 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 20 20 62  _BYTE;.        b
d63e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
d63f0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a   EXCLUSIVE_LOCK:
d6400 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
d6410 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46  start = SHARED_F
d6420 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f  IRST;.        lo
d6430 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45  ck.l_len = SHARE
d6440 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20  D_SIZE;.        
d6450 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66  break;.      def
d6460 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 73  ault:.        as
d6470 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20  sert(0);.    }. 
d6480 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69     s = fcntl(pFi
d6490 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20  le->h, F_SETLK, 
d64a0 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20  &lock);.    if( 
d64b0 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  s==(-1) ){.     
d64c0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72   int tErrno = er
d64d0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rno;.      rc = 
d64e0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d64f0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d6500 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c  , SQLITE_IOERR_L
d6510 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
d6520 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
d6530 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  ) ){.        pFi
d6540 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d6550 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a  tErrno;.      }.
d6560 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69      }.  }.  ..#i
d6570 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f  fndef NDEBUG.  /
d6580 2a 20 53 65 74 20 75 70 20 74 68 65 20 74 72 61  * Set up the tra
d6590 6e 73 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72  nsaction-counter
d65a0 20 63 68 61 6e 67 65 20 63 68 65 63 6b 69 6e 67   change checking
d65b0 20 66 6c 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a   flags when.  **
d65c0 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66   transitioning f
d65d0 72 6f 6d 20 61 20 53 48 41 52 45 44 20 74 6f 20  rom a SHARED to 
d65e0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e  a RESERVED lock.
d65f0 20 20 54 68 65 20 63 68 61 6e 67 65 0a 20 20 2a    The change.  *
d6600 2a 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f  * from SHARED to
d6610 20 52 45 53 45 52 56 45 44 20 6d 61 72 6b 73 20   RESERVED marks 
d6620 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
d6630 20 61 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77   a normal.  ** w
d6640 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 28  rite operation (
d6650 6e 6f 74 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61  not a hot journa
d6660 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a  l rollback)..  *
d6670 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
d6680 54 45 5f 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c  TE_OK.   && pFil
d6690 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  e->locktype<=SHA
d66a0 52 45 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c  RED_LOCK.   && l
d66b0 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45  ocktype==RESERVE
d66c0 44 5f 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20  D_LOCK.  ){.    
d66d0 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72  pFile->transCntr
d66e0 43 68 6e 67 20 3d 20 30 3b 0a 20 20 20 20 70 46  Chng = 0;.    pF
d66f0 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20  ile->dbUpdate = 
d6700 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e  0;.    pFile->in
d6710 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b  NormalWrite = 1;
d6720 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20  .  }.#endif...  
d6730 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
d6740 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
d6750 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
d6760 79 70 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  ype;.    pLock->
d6770 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
d6780 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ype;.  }else if(
d6790 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
d67a0 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  SIVE_LOCK ){.   
d67b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d67c0 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b   = PENDING_LOCK;
d67d0 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  .    pLock->lock
d67e0 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c  type = PENDING_L
d67f0 4f 43 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f  OCK;.  }..end_lo
d6800 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d  ck:.  unixLeaveM
d6810 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43  utex();.  OSTRAC
d6820 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25  E4("LOCK    %d %
d6830 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s %s\n", pFile->
d6840 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28  h, locktypeName(
d6850 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20  locktype), .    
d6860 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20    rc==SQLITE_OK 
d6870 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64  ? "ok" : "failed
d6880 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ");.  return rc;
d6890 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20  .}../*.** Lower 
d68a0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
d68b0 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
d68c0 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f  ptor pFile to lo
d68d0 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70  cktype.  locktyp
d68e0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  e.** must be eit
d68f0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
d6900 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
d6910 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
d6920 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
d6930 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
d6940 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
d6950 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
d6960 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
d6970 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
d6980 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  e is a no-op..*/
d6990 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
d69a0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
d69b0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
d69c0 6b 74 79 70 65 29 7b 0a 20 20 73 74 72 75 63 74  ktype){.  struct
d69d0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70   unixLockInfo *p
d69e0 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66  Lock;.  struct f
d69f0 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74  lock lock;.  int
d6a00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d6a10 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d6a20 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d6a30 69 64 3b 0a 20 20 69 6e 74 20 68 3b 0a 0a 20 20  id;.  int h;..  
d6a40 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d6a50 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c  .  OSTRACE7("UNL
d6a60 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25  OCK  %d %d was %
d6a70 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c  d(%d,%d) pid=%d\
d6a80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
d6a90 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46  cktype,.      pF
d6aa0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70  ile->locktype, p
d6ab0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63  File->pLock->loc
d6ac0 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c  ktype, pFile->pL
d6ad0 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64  ock->cnt, getpid
d6ae0 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ());..  assert( 
d6af0 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44  locktype<=SHARED
d6b00 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70  _LOCK );.  if( p
d6b10 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d  File->locktype<=
d6b20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20  locktype ){.    
d6b30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d6b40 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43  ;.  }.  if( CHEC
d6b50 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c 65  K_THREADID(pFile
d6b60 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
d6b70 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
d6b80 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75   }.  unixEnterMu
d6b90 74 65 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69  tex();.  h = pFi
d6ba0 6c 65 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d  le->h;.  pLock =
d6bb0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20   pFile->pLock;. 
d6bc0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
d6bd0 63 6e 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  cnt!=0 );.  if( 
d6be0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
d6bf0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
d6c00 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
d6c10 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c  ->locktype==pFil
d6c20 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
d6c30 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72     SimulateIOErr
d6c40 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20  orBenign(1);.   
d6c50 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d6c60 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53  ( h=(-1) ).    S
d6c70 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
d6c80 6e 69 67 6e 28 30 29 3b 0a 0a 23 69 66 6e 64 65  nign(0);..#ifnde
d6c90 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20  f NDEBUG.    /* 
d6ca0 57 68 65 6e 20 72 65 64 75 63 69 6e 67 20 61 20  When reducing a 
d6cb0 6c 6f 63 6b 20 73 75 63 68 20 74 68 61 74 20 6f  lock such that o
d6cc0 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 63  ther processes c
d6cd0 61 6e 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20  an start.    ** 
d6ce0 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61  reading the data
d6cf0 62 61 73 65 20 66 69 6c 65 20 61 67 61 69 6e 2c  base file again,
d6d00 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
d6d10 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  the.    ** trans
d6d20 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77  action counter w
d6d30 61 73 20 75 70 64 61 74 65 64 20 69 66 20 61 6e  as updated if an
d6d40 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  y part of the da
d6d50 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 69  tabase.    ** fi
d6d60 6c 65 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20  le changed.  If 
d6d70 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
d6d80 63 6f 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 75  counter is not u
d6d90 70 64 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 6f  pdated,.    ** o
d6da0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  ther connections
d6db0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c   to the same fil
d6dc0 65 20 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 6c  e might not real
d6dd0 69 7a 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  ize that.    ** 
d6de0 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61  the file has cha
d6df0 6e 67 65 64 20 61 6e 64 20 68 65 6e 63 65 20 6d  nged and hence m
d6e00 69 67 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 6f  ight not know to
d6e10 20 66 6c 75 73 68 20 74 68 65 69 72 0a 20 20 20   flush their.   
d6e20 20 2a 2a 20 63 61 63 68 65 2e 20 20 54 68 65 20   ** cache.  The 
d6e30 75 73 65 20 6f 66 20 61 20 73 74 61 6c 65 20 63  use of a stale c
d6e40 61 63 68 65 20 63 61 6e 20 6c 65 61 64 20 74 6f  ache can lead to
d6e50 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
d6e60 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
d6e70 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
d6e80 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3d 3d 30  inNormalWrite==0
d6e90 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69  .         || pFi
d6ea0 6c 65 2d 3e 64 62 55 70 64 61 74 65 3d 3d 30 0a  le->dbUpdate==0.
d6eb0 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c           || pFil
d6ec0 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67  e->transCntrChng
d6ed0 3d 3d 31 20 29 3b 0a 20 20 20 20 70 46 69 6c 65  ==1 );.    pFile
d6ee0 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20  ->inNormalWrite 
d6ef0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 20 20  = 0;.#endif...  
d6f00 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
d6f10 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
d6f20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65       lock.l_type
d6f30 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 20 20   = F_RDLCK;.    
d6f40 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20    lock.l_whence 
d6f50 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20  = SEEK_SET;.    
d6f60 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
d6f70 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20   SHARED_FIRST;. 
d6f80 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20       lock.l_len 
d6f90 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20  = SHARED_SIZE;. 
d6fa0 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68       if( fcntl(h
d6fb0 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
d6fc0 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  )==(-1) ){.     
d6fd0 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
d6fe0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72  errno;.        r
d6ff0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d7000 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d7010 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d7020 52 52 5f 52 44 4c 4f 43 4b 29 3b 0a 20 20 20 20  RR_RDLOCK);.    
d7030 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
d7040 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20  ERROR(rc) ){.   
d7050 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d7060 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d7070 3b 0a 20 20 20 20 20 20 20 20 7d 0a 09 09 09 09  ;.        }.....
d7080 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b  goto end_unlock;
d7090 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d70a0 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d     lock.l_type =
d70b0 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f   F_UNLCK;.    lo
d70c0 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45  ck.l_whence = SE
d70d0 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b  EK_SET;.    lock
d70e0 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49  .l_start = PENDI
d70f0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63  NG_BYTE;.    loc
d7100 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61  k.l_len = 2L;  a
d7110 73 73 65 72 74 28 20 50 45 4e 44 49 4e 47 5f 42  ssert( PENDING_B
d7120 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f  YTE+1==RESERVED_
d7130 42 59 54 45 20 29 3b 0a 20 20 20 20 69 66 28 20  BYTE );.    if( 
d7140 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b  fcntl(h, F_SETLK
d7150 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29  , &lock)!=(-1) )
d7160 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c  {.      pLock->l
d7170 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44  ocktype = SHARED
d7180 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  _LOCK;.    }else
d7190 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72  {.      int tErr
d71a0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d71b0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d71c0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d71d0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d71e0 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20  IOERR_UNLOCK);. 
d71f0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
d7200 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
d7210 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d7220 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d7230 0a 20 20 20 20 20 20 7d 0a 09 09 09 67 6f 74 6f  .      }....goto
d7240 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20   end_unlock;.   
d7250 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63   }.  }.  if( loc
d7260 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29  ktype==NO_LOCK )
d7270 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e 69  {.    struct uni
d7280 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b  xOpenCnt *pOpen;
d7290 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65  ..    /* Decreme
d72a0 6e 74 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f  nt the shared lo
d72b0 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 65 6c  ck counter.  Rel
d72c0 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 75 73  ease the lock us
d72d0 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f 53  ing an.    ** OS
d72e0 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e 20   call only when 
d72f0 61 6c 6c 20 74 68 72 65 61 64 73 20 69 6e 20 74  all threads in t
d7300 68 69 73 20 73 61 6d 65 20 70 72 6f 63 65 73 73  his same process
d7310 20 68 61 76 65 20 72 65 6c 65 61 73 65 64 0a 20   have released. 
d7320 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a     ** the lock..
d7330 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63 6b      */.    pLock
d7340 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28  ->cnt--;.    if(
d7350 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29   pLock->cnt==0 )
d7360 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74  {.      lock.l_t
d7370 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20  ype = F_UNLCK;. 
d7380 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e       lock.l_when
d7390 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20  ce = SEEK_SET;. 
d73a0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72       lock.l_star
d73b0 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d  t = lock.l_len =
d73c0 20 30 4c 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c   0L;.      Simul
d73d0 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
d73e0 28 31 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c  (1);.      Simul
d73f0 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d  ateIOError( h=(-
d7400 31 29 20 29 0a 20 20 20 20 20 20 53 69 6d 75 6c  1) ).      Simul
d7410 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
d7420 28 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66  (0);.      if( f
d7430 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c  cntl(h, F_SETLK,
d7440 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b   &lock)!=(-1) ){
d7450 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e  .        pLock->
d7460 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f  locktype = NO_LO
d7470 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  CK;.      }else{
d7480 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74 45 72  .        int tEr
d7490 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d74a0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d74b0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d74c0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d74d0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29  TE_IOERR_UNLOCK)
d74e0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53  ;.        if( IS
d74f0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
d7500 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ){.          pFi
d7510 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d7520 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20  tErrno;.        
d7530 7d 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d  }.        pLock-
d7540 3e 63 6e 74 20 3d 20 31 3b 0a 09 09 09 09 67 6f  >cnt = 1;.....go
d7550 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20  to end_unlock;. 
d7560 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
d7570 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74    /* Decrement t
d7580 68 65 20 63 6f 75 6e 74 20 6f 66 20 6c 6f 63 6b  he count of lock
d7590 73 20 61 67 61 69 6e 73 74 20 74 68 69 73 20 73  s against this s
d75a0 61 6d 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 20  ame file.  When 
d75b0 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74  the.    ** count
d75c0 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 63   reaches zero, c
d75d0 6c 6f 73 65 20 61 6e 79 20 6f 74 68 65 72 20 66  lose any other f
d75e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20  ile descriptors 
d75f0 77 68 6f 73 65 20 63 6c 6f 73 65 0a 20 20 20 20  whose close.    
d7600 2a 2a 20 77 61 73 20 64 65 66 65 72 72 65 64 20  ** was deferred 
d7610 62 65 63 61 75 73 65 20 6f 66 20 6f 75 74 73 74  because of outst
d7620 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e 0a 20 20  anding locks..  
d7630 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
d7640 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
d7650 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 46 69 6c      pOpen = pFil
d7660 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20  e->pOpen;.      
d7670 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a  pOpen->nLock--;.
d7680 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
d7690 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b  pen->nLock>=0 );
d76a0 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e  .      if( pOpen
d76b0 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 4f  ->nLock==0 && pO
d76c0 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3e 30 20  pen->nPending>0 
d76d0 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
d76e0 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
d76f0 30 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 65 6e  0; i<pOpen->nPen
d7700 64 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ding; i++){.    
d7710 20 20 20 20 20 20 2f 2a 20 63 6c 6f 73 65 20 70        /* close p
d7720 65 6e 64 69 6e 67 20 66 64 73 2c 20 62 75 74 20  ending fds, but 
d7730 69 66 20 63 6c 6f 73 69 6e 67 20 66 61 69 6c 73  if closing fails
d7740 20 64 6f 6e 27 74 20 66 72 65 65 20 74 68 65 20   don't free the 
d7750 61 72 72 61 79 0a 20 20 20 20 20 20 20 20 20 20  array.          
d7760 2a 2a 20 61 73 73 69 67 6e 20 2d 31 20 74 6f 20  ** assign -1 to 
d7770 74 68 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79  the successfully
d7780 20 63 6c 6f 73 65 64 20 64 65 73 63 72 69 70 74   closed descript
d7790 6f 72 73 20 61 6e 64 20 72 65 63 6f 72 64 20 74  ors and record t
d77a0 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  he.          ** 
d77b0 65 72 72 6f 72 2e 20 20 54 68 65 20 6e 65 78 74  error.  The next
d77c0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 6e 6c 6f   attempt to unlo
d77d0 63 6b 20 77 69 6c 6c 20 74 72 79 20 61 67 61 69  ck will try agai
d77e0 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  n. */.          
d77f0 69 66 28 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64  if( pOpen->aPend
d7800 69 6e 67 5b 69 5d 20 3c 20 30 20 29 20 63 6f 6e  ing[i] < 0 ) con
d7810 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20  tinue;.         
d7820 20 69 66 28 20 63 6c 6f 73 65 28 70 4f 70 65 6e   if( close(pOpen
d7830 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 29 20 29  ->aPending[i]) )
d7840 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 46  {.            pF
d7850 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d7860 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
d7870 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
d7880 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20  IOERR_CLOSE;.   
d7890 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
d78a0 20 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d            pOpen-
d78b0 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d  >aPending[i] = -
d78c0 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  1;.          }. 
d78d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
d78e0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
d78f0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
d7900 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70  sqlite3_free(pOp
d7910 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 29 3b 0a 20  en->aPending);. 
d7920 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e           pOpen->
d7930 6e 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20  nPending = 0;.  
d7940 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61          pOpen->a
d7950 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20  Pending = 0;.   
d7960 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
d7970 20 20 20 7d 0a 20 20 7d 0a 09 0a 65 6e 64 5f 75     }.  }...end_u
d7980 6e 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61  nlock:.  unixLea
d7990 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28  veMutex();.  if(
d79a0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
d79b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d79c0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72   = locktype;.  r
d79d0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d79e0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
d79f0 20 70 65 72 66 6f 72 6d 73 20 74 68 65 20 70 61   performs the pa
d7a00 72 74 73 20 6f 66 20 74 68 65 20 22 63 6c 6f 73  rts of the "clos
d7a10 65 20 66 69 6c 65 22 20 6f 70 65 72 61 74 69 6f  e file" operatio
d7a20 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20  n .** common to 
d7a30 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65  all locking sche
d7a40 6d 65 73 2e 20 49 74 20 63 6c 6f 73 65 73 20 74  mes. It closes t
d7a50 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 6e 64  he directory and
d7a60 20 66 69 6c 65 0a 2a 2a 20 68 61 6e 64 6c 65 73   file.** handles
d7a70 2c 20 69 66 20 74 68 65 79 20 61 72 65 20 76 61  , if they are va
d7a80 6c 69 64 2c 20 61 6e 64 20 73 65 74 73 20 61 6c  lid, and sets al
d7a90 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  l fields of the 
d7aa0 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75  unixFile.** stru
d7ab0 63 74 75 72 65 20 74 6f 20 30 2e 0a 2a 2a 0a 2a  cture to 0..**.*
d7ac0 2a 20 49 74 20 69 73 20 2a 6e 6f 74 2a 20 6e 65  * It is *not* ne
d7ad0 63 65 73 73 61 72 79 20 74 6f 20 68 6f 6c 64 20  cessary to hold 
d7ae0 74 68 65 20 6d 75 74 65 78 20 77 68 65 6e 20 74  the mutex when t
d7af0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
d7b00 61 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65 6e 20 6f  alled,.** even o
d7b10 6e 20 56 78 57 6f 72 6b 73 2e 20 20 41 20 6d 75  n VxWorks.  A mu
d7b20 74 65 78 20 77 69 6c 6c 20 62 65 20 61 63 71 75  tex will be acqu
d7b30 69 72 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 20  ired on VxWorks 
d7b40 62 79 20 74 68 65 0a 2a 2a 20 76 78 77 6f 72 6b  by the.** vxwork
d7b50 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 29  sReleaseFileId()
d7b60 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61   routine..*/.sta
d7b70 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 55 6e 69  tic int closeUni
d7b80 78 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69  xFile(sqlite3_fi
d7b90 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78 46  le *id){.  unixF
d7ba0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
d7bb0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 66  ixFile*)id;.  if
d7bc0 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69  ( pFile ){.    i
d7bd0 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e  f( pFile->dirfd>
d7be0 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  =0 ){.      int 
d7bf0 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c  err = close(pFil
d7c00 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20 20 20 20  e->dirfd);.     
d7c10 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20   if( err ){.    
d7c20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d7c30 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d7c40 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
d7c50 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c  ITE_IOERR_DIR_CL
d7c60 4f 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  OSE;.      }else
d7c70 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
d7c80 3e 64 69 72 66 64 3d 2d 31 3b 0a 20 20 20 20 20  >dirfd=-1;.     
d7c90 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
d7ca0 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a   pFile->h>=0 ){.
d7cb0 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20        int err = 
d7cc0 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b  close(pFile->h);
d7cd0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20 29  .      if( err )
d7ce0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
d7cf0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
d7d00 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  no;.        retu
d7d10 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
d7d20 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20  CLOSE;.      }. 
d7d30 20 20 20 7d 0a 23 69 66 20 4f 53 5f 56 58 57 4f     }.#if OS_VXWO
d7d40 52 4b 53 0a 20 20 20 20 69 66 28 20 70 46 69 6c  RKS.    if( pFil
d7d50 65 2d 3e 70 49 64 20 29 7b 0a 20 20 20 20 20 20  e->pId ){.      
d7d60 69 66 28 20 70 46 69 6c 65 2d 3e 69 73 44 65 6c  if( pFile->isDel
d7d70 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75  ete ){.        u
d7d80 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 70 49 64  nlink(pFile->pId
d7d90 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65  ->zCanonicalName
d7da0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
d7db0 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46   vxworksReleaseF
d7dc0 69 6c 65 49 64 28 70 46 69 6c 65 2d 3e 70 49 64  ileId(pFile->pId
d7dd0 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  );.      pFile->
d7de0 70 49 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 23  pId = 0;.    }.#
d7df0 65 6e 64 69 66 0a 20 20 20 20 4f 53 54 52 41 43  endif.    OSTRAC
d7e00 45 32 28 22 43 4c 4f 53 45 20 20 20 25 2d 33 64  E2("CLOSE   %-3d
d7e10 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a  \n", pFile->h);.
d7e20 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28      OpenCounter(
d7e30 2d 31 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  -1);.    memset(
d7e40 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66  pFile, 0, sizeof
d7e50 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 7d  (unixFile));.  }
d7e60 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
d7e70 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
d7e80 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73  ose a file..*/.s
d7e90 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 6c  tatic int unixCl
d7ea0 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
d7eb0 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20   *id){.  int rc 
d7ec0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
d7ed0 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69  f( id ){.    uni
d7ee0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
d7ef0 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20  unixFile *)id;. 
d7f00 20 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 69 64     unixUnlock(id
d7f10 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  , NO_LOCK);.    
d7f20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
d7f30 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d  ;.    if( pFile-
d7f40 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d  >pOpen && pFile-
d7f50 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b  >pOpen->nLock ){
d7f60 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
d7f70 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69  re are outstandi
d7f80 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74  ng locks, do not
d7f90 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20   actually close 
d7fa0 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20  the file just.  
d7fb0 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75      ** yet becau
d7fc0 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c  se that would cl
d7fd0 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e  ear those locks.
d7fe0 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74    Instead, add t
d7ff0 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a  he file.      **
d8000 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70   descriptor to p
d8010 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20  Open->aPending. 
d8020 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f   It will be auto
d8030 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64  matically closed
d8040 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74   when.      ** t
d8050 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20  he last lock is 
d8060 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a  cleared..      *
d8070 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 4e 65  /.      int *aNe
d8080 77 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  w;.      struct 
d8090 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70  unixOpenCnt *pOp
d80a0 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  en = pFile->pOpe
d80b0 6e 3b 0a 20 20 20 20 20 20 61 4e 65 77 20 3d 20  n;.      aNew = 
d80c0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
d80d0 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2c  pOpen->aPending,
d80e0 20 28 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e   (pOpen->nPendin
d80f0 67 2b 31 29 2a 73 69 7a 65 6f 66 28 69 6e 74 29  g+1)*sizeof(int)
d8100 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61 4e   );.      if( aN
d8110 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ew==0 ){.       
d8120 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20   /* If a malloc 
d8130 66 61 69 6c 73 2c 20 6a 75 73 74 20 6c 65 61 6b  fails, just leak
d8140 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
d8150 70 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 7d 65  ptor */.      }e
d8160 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4f 70  lse{.        pOp
d8170 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 61  en->aPending = a
d8180 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70 4f 70  New;.        pOp
d8190 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 70 4f 70  en->aPending[pOp
d81a0 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 5d 20 3d 20  en->nPending] = 
d81b0 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 20 20 20 20  pFile->h;.      
d81c0 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e    pOpen->nPendin
d81d0 67 2b 2b 3b 0a 20 20 20 20 20 20 20 20 70 46 69  g++;.        pFi
d81e0 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 20 20  le->h = -1;.    
d81f0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 65    }.    }.    re
d8200 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46  leaseLockInfo(pF
d8210 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20  ile->pLock);.   
d8220 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28   releaseOpenCnt(
d8230 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20  pFile->pOpen);. 
d8240 20 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69     rc = closeUni
d8250 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20 75  xFile(id);.    u
d8260 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
d8270 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
d8280 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
d8290 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  **** End of the 
d82a0 70 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c  posix advisory l
d82b0 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ock implementati
d82c0 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on *************
d82d0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
d82e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d82f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8320 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
d8330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8370 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
d8380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8390 2a 2a 2a 2a 20 4e 6f 2d 6f 70 20 4c 6f 63 6b 69  **** No-op Locki
d83a0 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
d83b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d83c0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 66 20 74  *****.**.** Of t
d83d0 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 69  he various locki
d83e0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
d83f0 6e 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68  ns available, th
d8400 69 73 20 69 73 20 62 79 20 66 61 72 20 74 68 65  is is by far the
d8410 0a 2a 2a 20 73 69 6d 70 6c 65 73 74 3a 20 20 6c  .** simplest:  l
d8420 6f 63 6b 69 6e 67 20 69 73 20 69 67 6e 6f 72 65  ocking is ignore
d8430 64 2e 20 20 4e 6f 20 61 74 74 65 6d 70 74 20 69  d.  No attempt i
d8440 73 20 6d 61 64 65 20 74 6f 20 6c 6f 63 6b 20 74  s made to lock t
d8450 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66  he database.** f
d8460 69 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20  ile for reading 
d8470 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a  or writing..**.*
d8480 2a 20 54 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d  * This locking m
d8490 6f 64 65 20 69 73 20 61 70 70 72 6f 70 72 69 61  ode is appropria
d84a0 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 72 65  te for use on re
d84b0 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65  ad-only database
d84c0 73 0a 2a 2a 20 28 65 78 3a 20 64 61 74 61 62 61  s.** (ex: databa
d84d0 73 65 73 20 74 68 61 74 20 61 72 65 20 62 75 72  ses that are bur
d84e0 6e 65 64 20 69 6e 74 6f 20 43 44 2d 52 4f 4d 2c  ned into CD-ROM,
d84f0 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 29 20 20   for example.)  
d8500 49 74 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62  It can.** also b
d8510 65 20 75 73 65 64 20 69 66 20 74 68 65 20 61 70  e used if the ap
d8520 70 6c 69 63 61 74 69 6f 6e 20 65 6d 70 6c 6f 79  plication employ
d8530 73 20 73 6f 6d 65 20 65 78 74 65 72 6e 61 6c 20  s some external 
d8540 6d 65 63 68 61 6e 69 73 6d 20 74 6f 0a 2a 2a 20  mechanism to.** 
d8550 70 72 65 76 65 6e 74 20 73 69 6d 75 6c 74 61 6e  prevent simultan
d8560 65 6f 75 73 20 61 63 63 65 73 73 20 6f 66 20 74  eous access of t
d8570 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
d8580 20 62 79 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a   by two or more.
d8590 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
d85a0 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 74 68  ections.  But th
d85b0 65 72 65 20 69 73 20 61 20 73 65 72 69 6f 75 73  ere is a serious
d85c0 20 72 69 73 6b 20 6f 66 20 64 61 74 61 62 61 73   risk of databas
d85d0 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20  e.** corruption 
d85e0 69 66 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20  if this locking 
d85f0 6d 6f 64 65 20 69 73 20 75 73 65 64 20 69 6e 20  mode is used in 
d8600 73 69 74 75 61 74 69 6f 6e 73 20 77 68 65 72 65  situations where
d8610 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 64 61 74   multiple.** dat
d8620 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
d8630 73 20 61 72 65 20 61 63 63 65 73 73 69 6e 67 20  s are accessing 
d8640 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
d8650 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 61  e file at the sa
d8660 6d 65 0a 2a 2a 20 74 69 6d 65 20 61 6e 64 20 6f  me.** time and o
d8670 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
d8680 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ose connections 
d8690 61 72 65 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a  are writing..*/.
d86a0 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f  .static int nolo
d86b0 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  ckCheckReservedL
d86c0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
d86d0 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 2a   *NotUsed, int *
d86e0 70 52 65 73 4f 75 74 29 7b 0a 20 20 55 4e 55 53  pResOut){.  UNUS
d86f0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
d8700 55 73 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f 75  Used);.  *pResOu
d8710 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  t = 0;.  return 
d8720 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61  SQLITE_OK;.}.sta
d8730 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f  tic int nolockLo
d8740 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d8750 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f  *NotUsed, int No
d8760 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45  tUsed2){.  UNUSE
d8770 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
d8780 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
d8790 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
d87a0 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  _OK;.}.static in
d87b0 74 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73  t nolockUnlock(s
d87c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74  qlite3_file *Not
d87d0 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65  Used, int NotUse
d87e0 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  d2){.  UNUSED_PA
d87f0 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
d8800 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72  , NotUsed2);.  r
d8810 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d8820 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
d8830 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
d8840 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 6c  tic int nolockCl
d8850 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
d8860 20 2a 69 64 29 20 7b 0a 20 20 72 65 74 75 72 6e   *id) {.  return
d8870 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69   closeUnixFile(i
d8880 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  d);.}../********
d8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
d88a0 6f 66 20 74 68 65 20 6e 6f 2d 6f 70 20 6c 6f 63  of the no-op loc
d88b0 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
d88c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
d88d0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
d88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8920 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  *****/../*******
d8930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8970 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
d8980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8990 2a 20 42 65 67 69 6e 20 64 6f 74 2d 66 69 6c 65  * Begin dot-file
d89a0 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a   Locking *******
d89b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d89c0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
d89d0 65 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e  e dotfile lockin
d89e0 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  g implementation
d89f0 20 75 73 65 73 20 74 68 65 20 65 78 69 73 74 69   uses the existi
d8a00 6e 67 20 6f 66 20 73 65 70 61 72 61 74 65 20 6c  ng of separate l
d8a10 6f 63 6b 0a 2a 2a 20 66 69 6c 65 73 20 69 6e 20  ock.** files in 
d8a20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 72 6f 6c  order to control
d8a30 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 64   access to the d
d8a40 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 77  atabase.  This w
d8a50 6f 72 6b 73 20 6f 6e 20 6a 75 73 74 0a 2a 2a 20  orks on just.** 
d8a60 61 62 6f 75 74 20 65 76 65 72 79 20 66 69 6c 65  about every file
d8a70 73 79 73 74 65 6d 20 69 6d 61 67 69 6e 61 62 6c  system imaginabl
d8a80 65 2e 20 20 42 75 74 20 74 68 65 72 65 20 61 72  e.  But there ar
d8a90 65 20 73 65 72 69 6f 75 73 20 64 6f 77 6e 73 69  e serious downsi
d8aa0 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31  des:.**.**    (1
d8ab0 29 20 20 54 68 65 72 65 20 69 73 20 7a 65 72 6f  )  There is zero
d8ac0 20 63 6f 6e 63 75 72 72 65 6e 63 79 2e 20 20 41   concurrency.  A
d8ad0 20 73 69 6e 67 6c 65 20 72 65 61 64 65 72 20 62   single reader b
d8ae0 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 68 65 72 0a  locks all other.
d8af0 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e 6e 65  **         conne
d8b00 63 74 69 6f 6e 73 20 66 72 6f 6d 20 72 65 61 64  ctions from read
d8b10 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74  ing or writing t
d8b20 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  he database..**.
d8b30 2a 2a 20 20 20 20 28 32 29 20 20 41 6e 20 61 70  **    (2)  An ap
d8b40 70 6c 69 63 61 74 69 6f 6e 20 63 72 61 73 68 20  plication crash 
d8b50 6f 72 20 70 6f 77 65 72 20 6c 6f 73 73 20 63 61  or power loss ca
d8b60 6e 20 6c 65 61 76 65 20 73 74 61 6c 65 20 6c 6f  n leave stale lo
d8b70 63 6b 20 66 69 6c 65 73 0a 2a 2a 20 20 20 20 20  ck files.**     
d8b80 20 20 20 20 73 69 74 74 69 6e 67 20 61 72 6f 75      sitting arou
d8b90 6e 64 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20  nd that need to 
d8ba0 62 65 20 63 6c 65 61 72 65 64 20 6d 61 6e 75 61  be cleared manua
d8bb0 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76 65 72  lly..**.** Never
d8bc0 74 68 65 6c 65 73 73 2c 20 61 20 64 6f 74 6c 6f  theless, a dotlo
d8bd0 63 6b 20 69 73 20 61 6e 20 61 70 70 72 6f 70 72  ck is an appropr
d8be0 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64  iate locking mod
d8bf0 65 20 66 6f 72 20 75 73 65 20 69 66 20 6e 6f 0a  e for use if no.
d8c00 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67  ** other locking
d8c10 20 73 74 72 61 74 65 67 79 20 69 73 20 61 76 61   strategy is ava
d8c20 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f  ilable..**.** Do
d8c30 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 77 6f  tfile locking wo
d8c40 72 6b 73 20 62 79 20 63 72 65 61 74 69 6e 67 20  rks by creating 
d8c50 61 20 66 69 6c 65 20 69 6e 20 74 68 65 20 73 61  a file in the sa
d8c60 6d 65 20 64 69 72 65 63 74 6f 72 79 20 61 73 20  me directory as 
d8c70 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
d8c80 61 6e 64 20 77 69 74 68 20 74 68 65 20 73 61 6d  and with the sam
d8c90 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74 68 20  e name but with 
d8ca0 61 20 22 2e 6c 6f 63 6b 22 20 65 78 74 65 6e 73  a ".lock" extens
d8cb0 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2a 20 54 68  ion added..** Th
d8cc0 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 61  e existance of a
d8cd0 20 6c 6f 63 6b 20 66 69 6c 65 20 69 6d 70 6c 69   lock file impli
d8ce0 65 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  es an EXCLUSIVE 
d8cf0 6c 6f 63 6b 2e 20 20 41 6c 6c 20 6f 74 68 65 72  lock.  All other
d8d00 20 6c 6f 63 6b 0a 2a 2a 20 74 79 70 65 73 20 28   lock.** types (
d8d10 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44  SHARED, RESERVED
d8d20 2c 20 50 45 4e 44 49 4e 47 29 20 61 72 65 20 6d  , PENDING) are m
d8d30 61 70 70 65 64 20 69 6e 74 6f 20 45 58 43 4c 55  apped into EXCLU
d8d40 53 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  SIVE..*/../*.** 
d8d50 54 68 65 20 66 69 6c 65 20 73 75 66 66 69 78 20  The file suffix 
d8d60 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 61 74  added to the dat
d8d70 61 20 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  a base filename 
d8d80 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61  in order to crea
d8d90 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 66  te the.** lock f
d8da0 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ile..*/.#define 
d8db0 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 20 22  DOTLOCK_SUFFIX "
d8dc0 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a 2a 20 54 68  .lock"../*.** Th
d8dd0 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
d8de0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
d8df0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
d8e00 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
d8e10 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ied.** file by t
d8e20 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
d8e30 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
d8e40 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
d8e50 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a  , set *pResOut.*
d8e60 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  * to a non-zero 
d8e70 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20  value otherwise 
d8e80 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20  *pResOut is set 
d8e90 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65  to zero.  The re
d8ea0 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73  turn value.** is
d8eb0 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f   set to SQLITE_O
d8ec0 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20  K unless an I/O 
d8ed0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
d8ee0 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e  ing lock checkin
d8ef0 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 64 6f 74 66  g..**.** In dotf
d8f00 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 65 69 74  ile locking, eit
d8f10 68 65 72 20 61 20 6c 6f 63 6b 20 65 78 69 73 74  her a lock exist
d8f20 73 20 6f 72 20 69 74 20 64 6f 65 73 20 6e 6f 74  s or it does not
d8f30 2e 20 20 53 6f 20 69 6e 20 74 68 69 73 0a 2a 2a  .  So in this.**
d8f40 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 43 68   variation of Ch
d8f50 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
d8f60 29 2c 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73  ), *pResOut is s
d8f70 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 6e  et to true if an
d8f80 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 68 65 6c  y lock.** is hel
d8f90 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 61 6e  d on the file an
d8fa0 64 20 66 61 6c 73 65 20 69 66 20 74 68 65 20 66  d false if the f
d8fb0 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e  ile is unlocked.
d8fc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
d8fd0 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72  otlockCheckReser
d8fe0 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
d8ff0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
d9000 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20  ResOut) {.  int 
d9010 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d9020 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d    int reserved =
d9030 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a   0;.  unixFile *
d9040 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d9050 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61  e*)id;..  Simula
d9060 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
d9070 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  n SQLITE_IOERR_C
d9080 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b  HECKRESERVEDLOCK
d9090 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  ; );.  .  assert
d90a0 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a  ( pFile );..  /*
d90b0 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65   Check if a thre
d90c0 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65  ad in this proce
d90d0 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20  ss holds such a 
d90e0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46  lock */.  if( pF
d90f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48  ile->locktype>SH
d9100 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d9110 20 2f 2a 20 45 69 74 68 65 72 20 74 68 69 73 20   /* Either this 
d9120 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 73 6f  connection or so
d9130 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74  me other connect
d9140 69 6f 6e 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ion in the same 
d9150 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 68  process.    ** h
d9160 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  olds a lock on t
d9170 68 65 20 66 69 6c 65 2e 20 20 4e 6f 20 6e 65 65  he file.  No nee
d9180 64 20 74 6f 20 63 68 65 63 6b 20 66 75 72 74 68  d to check furth
d9190 65 72 2e 20 2a 2f 0a 20 20 20 20 72 65 73 65 72  er. */.    reser
d91a0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  ved = 1;.  }else
d91b0 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63  {.    /* The loc
d91c0 6b 20 69 73 20 68 65 6c 64 20 69 66 20 61 6e 64  k is held if and
d91d0 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 6c 6f 63   only if the loc
d91e0 6b 66 69 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a  kfile exists */.
d91f0 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
d9200 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 6f 6e  zLockFile = (con
d9210 73 74 20 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e  st char*)pFile->
d9220 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
d9230 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 61      reserved = a
d9240 63 63 65 73 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c  ccess(zLockFile,
d9250 20 30 29 3d 3d 30 3b 0a 20 20 7d 0a 20 20 4f 53   0)==0;.  }.  OS
d9260 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d  TRACE4("TEST WR-
d9270 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22  LOCK %d %d %d\n"
d9280 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20  , pFile->h, rc, 
d9290 72 65 73 65 72 76 65 64 29 3b 0a 20 20 2a 70 52  reserved);.  *pR
d92a0 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64  esOut = reserved
d92b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
d92c0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65  ../*.** Lock the
d92d0 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c   file with the l
d92e0 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79  ock specified by
d92f0 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74   parameter lockt
d9300 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20  ype - one.** of 
d9310 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
d9320 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41  *.**     (1) SHA
d9330 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  RED_LOCK.**     
d9340 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  (2) RESERVED_LOC
d9350 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e  K.**     (3) PEN
d9360 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  DING_LOCK.**    
d9370 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c   (4) EXCLUSIVE_L
d9380 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69  OCK.**.** Someti
d9390 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74  mes when request
d93a0 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61  ing one lock sta
d93b0 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c  te, additional l
d93c0 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72  ock states.** ar
d93d0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65  e inserted in be
d93e0 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b  tween.  The lock
d93f0 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f  ing might fail o
d9400 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74  n one of the lat
d9410 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  er.** transition
d9420 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f  s leaving the lo
d9430 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65  ck state differe
d9440 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20  nt from what it 
d9450 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73  started but.** s
d9460 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74  till short of it
d9470 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c  s goal.  The fol
d9480 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f  lowing chart sho
d9490 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a  ws the allowed.*
d94a0 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e  * transitions an
d94b0 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69  d the inserted i
d94c0 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74  ntermediate stat
d94d0 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c  es:.**.**    UNL
d94e0 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a  OCKED -> SHARED.
d94f0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
d9500 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53  RESERVED.**    S
d9510 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  HARED -> (PENDIN
d9520 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
d9530 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d  **    RESERVED -
d9540 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
d9550 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50  XCLUSIVE.**    P
d9560 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53  ENDING -> EXCLUS
d9570 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  IVE.**.** This r
d9580 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79  outine will only
d9590 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b   increase a lock
d95a0 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74  .  Use the sqlit
d95b0 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20  e3OsUnlock().** 
d95c0 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72  routine to lower
d95d0 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c   a locking level
d95e0 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 64 6f 74  ..**.** With dot
d95f0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 77 65  file locking, we
d9600 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20 73 75 70   really only sup
d9610 70 6f 72 74 20 73 74 61 74 65 20 28 34 29 3a 20  port state (4): 
d9620 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a 20 42 75  EXCLUSIVE..** Bu
d9630 74 20 77 65 20 74 72 61 63 6b 20 74 68 65 20 6f  t we track the o
d9640 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  ther locking lev
d9650 65 6c 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a  els internally..
d9660 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f  */.static int do
d9670 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65  tlockLock(sqlite
d9680 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
d9690 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e  locktype) {.  un
d96a0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
d96b0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
d96c0 20 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20   int fd;.  char 
d96d0 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68  *zLockFile = (ch
d96e0 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ar *)pFile->lock
d96f0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e  ingContext;.  in
d9700 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
d9710 3b 0a 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68  ;...  /* If we h
d9720 61 76 65 20 61 6e 79 20 6c 6f 63 6b 2c 20 74 68  ave any lock, th
d9730 65 6e 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65  en the lock file
d9740 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e   already exists.
d9750 20 20 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20    All we have.  
d9760 2a 2a 20 74 6f 20 64 6f 20 69 73 20 61 64 6a 75  ** to do is adju
d9770 73 74 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20  st our internal 
d9780 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 6c 6f  record of the lo
d9790 63 6b 20 6c 65 76 65 6c 2e 0a 20 20 2a 2f 0a 20  ck level..  */. 
d97a0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d97b0 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 20 29  type > NO_LOCK )
d97c0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
d97d0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
d97e0 3b 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b  ;.#if !OS_VXWORK
d97f0 53 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20  S.    /* Always 
d9800 75 70 64 61 74 65 20 74 68 65 20 74 69 6d 65 73  update the times
d9810 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20  tamp on the old 
d9820 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74 69 6d  file */.    utim
d9830 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55  es(zLockFile, NU
d9840 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  LL);.#endif.    
d9850 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d9860 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72  ;.  }.  .  /* gr
d9870 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ab an exclusive 
d9880 6c 6f 63 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f  lock */.  fd = o
d9890 70 65 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f  pen(zLockFile,O_
d98a0 52 44 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f  RDONLY|O_CREAT|O
d98b0 5f 45 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69  _EXCL,0600);.  i
d98c0 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f  f( fd<0 ){.    /
d98d0 2a 20 66 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e  * failed to open
d98e0 2f 63 72 65 61 74 65 20 74 68 65 20 66 69 6c 65  /create the file
d98f0 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d  , someone else m
d9900 61 79 20 68 61 76 65 20 73 74 6f 6c 65 6e 20 74  ay have stolen t
d9910 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69  he lock */.    i
d9920 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
d9930 6f 3b 0a 20 20 20 20 69 66 28 20 45 45 58 49 53  o;.    if( EEXIS
d9940 54 20 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20  T == tErrno ){. 
d9950 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
d9960 5f 42 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73  _BUSY;.    } els
d9970 65 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  e {.      rc = s
d9980 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d9990 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d99a0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
d99b0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49  CK);.      if( I
d99c0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d99d0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
d99e0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d99f0 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  Errno;.      }. 
d9a00 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
d9a10 72 63 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 63  rc;.  } .  if( c
d9a20 6c 6f 73 65 28 66 64 29 20 29 7b 0a 20 20 20 20  lose(fd) ){.    
d9a30 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d9a40 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63   = errno;.    rc
d9a50 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
d9a60 43 4c 4f 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20  CLOSE;.  }.  .  
d9a70 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74  /* got it, set t
d9a80 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 75  he type and retu
d9a90 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65  rn ok */.  pFile
d9aa0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
d9ab0 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20  ktype;.  return 
d9ac0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77  rc;.}../*.** Low
d9ad0 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
d9ae0 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
d9af0 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f  criptor pFile to
d9b00 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b   locktype.  lock
d9b10 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20  type.** must be 
d9b20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f  either NO_LOCK o
d9b30 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a  r SHARED_LOCK..*
d9b40 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b  *.** If the lock
d9b50 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  ing level of the
d9b60 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d9b70 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f   is already at o
d9b80 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72  r below.** the r
d9b90 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67  equested locking
d9ba0 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75   level, this rou
d9bb0 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
d9bc0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
d9bd0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 72 65  locking level re
d9be0 61 63 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c 20 64  aches NO_LOCK, d
d9bf0 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66  elete the lock f
d9c00 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
d9c10 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b  nt dotlockUnlock
d9c20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d9c30 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
d9c40 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70   {.  unixFile *p
d9c50 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d9c60 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c  *)id;.  char *zL
d9c70 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20  ockFile = (char 
d9c80 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
d9c90 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65  Context;..  asse
d9ca0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f  rt( pFile );.  O
d9cb0 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20  STRACE5("UNLOCK 
d9cc0 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70 69   %d %d was %d pi
d9cd0 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d=%d\n", pFile->
d9ce0 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20  h, locktype,..  
d9cf0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d9d00 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61  , getpid());.  a
d9d10 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c  ssert( locktype<
d9d20 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
d9d30 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66    .  /* no-op if
d9d40 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69   possible */.  i
d9d50 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d9d60 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe==locktype ){.
d9d70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d9d80 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
d9d90 54 6f 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20  To downgrade to 
d9da0 73 68 61 72 65 64 2c 20 73 69 6d 70 6c 79 20 75  shared, simply u
d9db0 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e  pdate our intern
d9dc0 61 6c 20 6e 6f 74 69 6f 6e 20 6f 66 20 74 68 65  al notion of the
d9dd0 0a 20 20 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65  .  ** lock state
d9de0 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 6d 65  .  No need to me
d9df0 73 73 20 77 69 74 68 20 74 68 65 20 66 69 6c 65  ss with the file
d9e00 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20   on disk..  */. 
d9e10 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
d9e20 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
d9e30 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d9e40 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b  e = SHARED_LOCK;
d9e50 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d9e60 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
d9e70 2f 2a 20 54 6f 20 66 75 6c 6c 79 20 75 6e 6c 6f  /* To fully unlo
d9e80 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  ck the database,
d9e90 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b   delete the lock
d9ea0 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72   file */.  asser
d9eb0 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  t( locktype==NO_
d9ec0 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 75 6e  LOCK );.  if( un
d9ed0 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29 20  link(zLockFile) 
d9ee0 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 74  ){.    int rc, t
d9ef0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d9f00 20 20 20 69 66 28 20 45 4e 4f 45 4e 54 20 21 3d     if( ENOENT !=
d9f10 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20   tErrno ){.     
d9f20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
d9f30 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
d9f40 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
d9f50 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20  OERR_UNLOCK);.  
d9f60 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c    }.    if( IS_L
d9f70 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
d9f80 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
d9f90 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d9fa0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
d9fb0 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46  rn rc; .  }.  pF
d9fc0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
d9fd0 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72  NO_LOCK;.  retur
d9fe0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d9ff0 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
da000 6c 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74  le.  Make sure t
da010 68 65 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e  he lock has been
da020 20 72 65 6c 65 61 73 65 64 20 62 65 66 6f 72 65   released before
da030 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f 0a 73 74 61   closing..*/.sta
da040 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43  tic int dotlockC
da050 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
da060 65 20 2a 69 64 29 20 7b 0a 20 20 69 6e 74 20 72  e *id) {.  int r
da070 63 3b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20  c;.  if( id ){. 
da080 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69     unixFile *pFi
da090 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
da0a0 69 64 3b 0a 20 20 20 20 64 6f 74 6c 6f 63 6b 55  id;.    dotlockU
da0b0 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43  nlock(id, NO_LOC
da0c0 4b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  K);.    sqlite3_
da0d0 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  free(pFile->lock
da0e0 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d  ingContext);.  }
da0f0 0a 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69  .  rc = closeUni
da100 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 72 65 74  xFile(id);.  ret
da110 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a  urn rc;.}./*****
da120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
da130 64 20 6f 66 20 74 68 65 20 64 6f 74 2d 66 69 6c  d of the dot-fil
da140 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  e lock implement
da150 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ation **********
da160 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
da170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da1b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a  ********/../****
da1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
da210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da220 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 6c 6f 63  ***** Begin floc
da230 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a  k Locking ******
da240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
da260 20 55 73 65 20 74 68 65 20 66 6c 6f 63 6b 28 29   Use the flock()
da270 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74 6f 20   system call to 
da280 64 6f 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e  do file locking.
da290 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6c  .**.** flock() l
da2a0 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64  ocking is like d
da2b0 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  ot-file locking 
da2c0 69 6e 20 74 68 61 74 20 74 68 65 20 76 61 72 69  in that the vari
da2d0 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d 67 72 61 69  ous.** fine-grai
da2e0 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73  n locking levels
da2f0 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 53 51   supported by SQ
da300 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c 61 70 73  Lite are collaps
da310 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e  ed into.** a sin
da320 67 6c 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f  gle exclusive lo
da330 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  ck.  In other wo
da340 72 64 73 2c 20 53 48 41 52 45 44 2c 20 52 45 53  rds, SHARED, RES
da350 45 52 56 45 44 2c 20 61 6e 64 0a 2a 2a 20 50 45  ERVED, and.** PE
da360 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 61 72 65 20  NDING locks are 
da370 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61  the same thing a
da380 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  s an EXCLUSIVE l
da390 6f 63 6b 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20  ock.  SQLite.** 
da3a0 73 74 69 6c 6c 20 77 6f 72 6b 73 20 77 68 65 6e  still works when
da3b0 20 79 6f 75 20 64 6f 20 74 68 69 73 2c 20 62 75   you do this, bu
da3c0 74 20 63 6f 6e 63 75 72 72 65 6e 63 79 20 69 73  t concurrency is
da3d0 20 72 65 64 75 63 65 64 20 73 69 6e 63 65 0a 2a   reduced since.*
da3e0 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20  * only a single 
da3f0 70 72 6f 63 65 73 73 20 63 61 6e 20 62 65 20 72  process can be r
da400 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62  eading the datab
da410 61 73 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a  ase at a time..*
da420 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 69 73 20 73  *.** Omit this s
da430 65 63 74 69 6f 6e 20 69 66 20 53 51 4c 49 54 45  ection if SQLITE
da440 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
da450 53 54 59 4c 45 20 69 73 20 74 75 72 6e 65 64 20  STYLE is turned 
da460 6f 66 66 20 6f 72 20 69 66 0a 2a 2a 20 63 6f 6d  off or if.** com
da470 70 69 6c 69 6e 67 20 66 6f 72 20 56 58 57 4f 52  piling for VXWOR
da480 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  KS..*/.#if SQLIT
da490 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
da4a0 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58  _STYLE && !OS_VX
da4b0 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  WORKS../*.** Thi
da4c0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
da4d0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52   if there is a R
da4e0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c  ESERVED lock hel
da4f0 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  d on the specifi
da500 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68  ed.** file by th
da510 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  is or any other 
da520 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68  process. If such
da530 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c   a lock is held,
da540 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a   set *pResOut.**
da550 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76   to a non-zero v
da560 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a  alue otherwise *
da570 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74  pResOut is set t
da580 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74  o zero.  The ret
da590 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  urn value.** is 
da5a0 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b  set to SQLITE_OK
da5b0 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65   unless an I/O e
da5c0 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
da5d0 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67  ng lock checking
da5e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
da5f0 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76  flockCheckReserv
da600 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
da610 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
da620 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63  esOut){.  int rc
da630 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
da640 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30  int reserved = 0
da650 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
da660 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
da670 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61  )id;.  .  Simula
da680 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
da690 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  n SQLITE_IOERR_C
da6a0 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b  HECKRESERVEDLOCK
da6b0 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  ; );.  .  assert
da6c0 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 0a 20 20  ( pFile );.  .  
da6d0 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68  /* Check if a th
da6e0 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f  read in this pro
da6f0 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20  cess holds such 
da700 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20  a lock */.  if( 
da710 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
da720 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
da730 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b     reserved = 1;
da740 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68  .  }.  .  /* Oth
da750 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f  erwise see if so
da760 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
da770 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20   holds it. */.  
da780 69 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b  if( !reserved ){
da790 0a 20 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20  .    /* attempt 
da7a0 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 6b 20  to get the lock 
da7b0 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 3d  */.    int lrc =
da7c0 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c   flock(pFile->h,
da7d0 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f   LOCK_EX | LOCK_
da7e0 4e 42 29 3b 0a 20 20 20 20 69 66 28 20 21 6c 72  NB);.    if( !lr
da7f0 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 67 6f  c ){.      /* go
da800 74 20 74 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f  t the lock, unlo
da810 63 6b 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 6c  ck it */.      l
da820 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65  rc = flock(pFile
da830 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20  ->h, LOCK_UN);. 
da840 20 20 20 20 20 69 66 20 28 20 6c 72 63 20 29 20       if ( lrc ) 
da850 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74 45  {.        int tE
da860 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
da870 20 20 20 20 20 20 2f 2a 20 75 6e 6c 6f 63 6b 20        /* unlock 
da880 66 61 69 6c 65 64 20 77 69 74 68 20 61 6e 20 65  failed with an e
da890 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  rror */.        
da8a0 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f  lrc = sqliteErro
da8b0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
da8c0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
da8d0 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20  OERR_UNLOCK); . 
da8e0 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f         if( IS_LO
da8f0 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b  CK_ERROR(lrc) ){
da900 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65  .          pFile
da910 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
da920 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20  rrno;.          
da930 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20  rc = lrc;.      
da940 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
da950 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 69  } else {.      i
da960 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
da970 6f 3b 0a 20 20 20 20 20 20 72 65 73 65 72 76 65  o;.      reserve
da980 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a 20  d = 1;.      /* 
da990 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 69 67  someone else mig
da9a0 68 74 20 68 61 76 65 20 69 74 20 72 65 73 65 72  ht have it reser
da9b0 76 65 64 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63  ved */.      lrc
da9c0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72   = sqliteErrorFr
da9d0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72  omPosixError(tEr
da9e0 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52  rno, SQLITE_IOER
da9f0 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20  R_LOCK); .      
daa00 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
daa10 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20  R(lrc) ){.      
daa20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
daa30 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
daa40 20 20 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20       rc = lrc;. 
daa50 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
daa60 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53  .  OSTRACE4("TES
daa70 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
daa80 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
daa90 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a   rc, reserved);.
daaa0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49  .#ifdef SQLITE_I
daab0 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b  GNORE_FLOCK_LOCK
daac0 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 20 28 72  _ERRORS.  if( (r
daad0 63 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c & SQLITE_IOERR
daae0 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  ) == SQLITE_IOER
daaf0 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  R ){.    rc = SQ
dab00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 65 73  LITE_OK;.    res
dab10 65 72 76 65 64 3d 31 3b 0a 20 20 7d 0a 23 65 6e  erved=1;.  }.#en
dab20 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47  dif /* SQLITE_IG
dab30 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f  NORE_FLOCK_LOCK_
dab40 45 52 52 4f 52 53 20 2a 2f 0a 20 20 2a 70 52 65  ERRORS */.  *pRe
dab50 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b  sOut = reserved;
dab60 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
dab70 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20  ./*.** Lock the 
dab80 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f  file with the lo
dab90 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ck specified by 
daba0 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79  parameter lockty
dabb0 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74  pe - one.** of t
dabc0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
dabd0 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52  .**     (1) SHAR
dabe0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
dabf0 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  2) RESERVED_LOCK
dac00 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44  .**     (3) PEND
dac10 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  ING_LOCK.**     
dac20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  (4) EXCLUSIVE_LO
dac30 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d  CK.**.** Sometim
dac40 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69  es when requesti
dac50 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74  ng one lock stat
dac60 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f  e, additional lo
dac70 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65  ck states.** are
dac80 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74   inserted in bet
dac90 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69  ween.  The locki
daca0 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e  ng might fail on
dacb0 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65   one of the late
dacc0 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73  r.** transitions
dacd0 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63   leaving the loc
dace0 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e  k state differen
dacf0 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73  t from what it s
dad00 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74  tarted but.** st
dad10 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73  ill short of its
dad20 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c   goal.  The foll
dad30 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77  owing chart show
dad40 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a  s the allowed.**
dad50 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64   transitions and
dad60 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e   the inserted in
dad70 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65  termediate state
dad80 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f  s:.**.**    UNLO
dad90 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a  CKED -> SHARED.*
dada0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52  *    SHARED -> R
dadb0 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48  ESERVED.**    SH
dadc0 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  ARED -> (PENDING
dadd0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
dade0 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e  *    RESERVED ->
dadf0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
dae00 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45  CLUSIVE.**    PE
dae10 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49  NDING -> EXCLUSI
dae20 56 45 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29  VE.**.** flock()
dae30 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70   only really sup
dae40 70 6f 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c  port EXCLUSIVE l
dae50 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20  ocks.  We track 
dae60 69 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20  intermediate.** 
dae70 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74  lock states in t
dae80 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  he sqlite3_file 
dae90 73 74 72 75 63 74 75 72 65 2c 20 62 75 74 20 61  structure, but a
daea0 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20  ll locks SHARED 
daeb0 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20  or.** above are 
daec0 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45  really EXCLUSIVE
daed0 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75   locks and exclu
daee0 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f  de all other pro
daef0 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61  cesses from.** a
daf00 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a  ccess the file..
daf10 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
daf20 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63  ne will only inc
daf30 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55  rease a lock.  U
daf40 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73  se the sqlite3Os
daf50 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74  Unlock().** rout
daf60 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c  ine to lower a l
daf70 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f  ocking level..*/
daf80 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63  .static int floc
daf90 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  kLock(sqlite3_fi
dafa0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
dafb0 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20 72 63  type) {.  int rc
dafc0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
dafd0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
dafe0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
daff0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
db000 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65  e );..  /* if we
db010 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20   already have a 
db020 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 63 6c  lock, it is excl
db030 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75  usive.  .  ** Ju
db040 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 6c 20  st adjust level 
db050 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74  and punt on outt
db060 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20  a here. */.  if 
db070 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  (pFile->locktype
db080 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20   > NO_LOCK) {.  
db090 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
db0a0 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
db0b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
db0c0 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  OK;.  }.  .  /* 
db0d0 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76  grab an exclusiv
db0e0 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20 20 69  e lock */.  .  i
db0f0 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e  f (flock(pFile->
db100 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43  h, LOCK_EX | LOC
db110 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20 69 6e 74  K_NB)) {.    int
db120 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
db130 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27 74 20 67  .    /* didn't g
db140 65 74 2c 20 6d 75 73 74 20 62 65 20 62 75 73 79  et, must be busy
db150 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
db160 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
db170 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
db180 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b  QLITE_IOERR_LOCK
db190 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  );.    if( IS_LO
db1a0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
db1b0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
db1c0 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
db1d0 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20  .    }.  } else 
db1e0 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20 69 74 2c  {.    /* got it,
db1f0 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e   set the type an
db200 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20  d return ok */. 
db210 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
db220 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20  pe = locktype;. 
db230 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c   }.  OSTRACE4("L
db240 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c  OCK    %d %s %s\
db250 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
db260 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74  cktypeName(lockt
db270 79 70 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20  ype), .         
db280 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20    rc==SQLITE_OK 
db290 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64  ? "ok" : "failed
db2a0 22 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ");.#ifdef SQLIT
db2b0 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c  E_IGNORE_FLOCK_L
db2c0 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28  OCK_ERRORS.  if(
db2d0 20 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f   (rc & SQLITE_IO
db2e0 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  ERR) == SQLITE_I
db2f0 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d  OERR ){.    rc =
db300 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
db310 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
db320 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f  TE_IGNORE_FLOCK_
db330 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20  LOCK_ERRORS */. 
db340 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
db350 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20  /*.** Lower the 
db360 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e  locking level on
db370 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
db380 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79   pFile to lockty
db390 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a  pe.  locktype.**
db3a0 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
db3b0 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45  NO_LOCK or SHARE
db3c0 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  D_LOCK..**.** If
db3d0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
db3e0 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
db3f0 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
db400 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
db410 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65  .** the requeste
db420 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c  d locking level,
db430 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
db440 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
db450 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 6c  tic int flockUnl
db460 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
db470 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
db480 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  pe) {.  unixFile
db490 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
db4a0 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 61 73  ile*)id;.  .  as
db4b0 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
db4c0 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43   OSTRACE5("UNLOC
db4d0 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20  K  %d %d was %d 
db4e0 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  pid=%d\n", pFile
db4f0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20  ->h, locktype,. 
db500 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
db510 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69  >locktype, getpi
db520 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  d());.  assert( 
db530 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44  locktype<=SHARED
db540 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a  _LOCK );.  .  /*
db550 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62   no-op if possib
db560 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  le */.  if( pFil
db570 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63  e->locktype==loc
db580 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74  ktype ){.    ret
db590 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
db5a0 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65   }.  .  /* share
db5b0 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65  d can just be se
db5c0 74 20 62 65 63 61 75 73 65 20 77 65 20 61 6c 77  t because we alw
db5d0 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c  ays have an excl
db5e0 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c  usive */.  if (l
db5f0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
db600 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c  LOCK) {.    pFil
db610 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
db620 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75  cktype;.    retu
db630 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
db640 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65  }.  .  /* no, re
db650 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f  ally, unlock. */
db660 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 6c 6f 63  .  int rc = floc
db670 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b  k(pFile->h, LOCK
db680 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72 63 29 20  _UN);.  if (rc) 
db690 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20 74 45 72  {.    int r, tEr
db6a0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
db6b0 20 72 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72   r = sqliteError
db6c0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
db6d0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
db6e0 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20  ERR_UNLOCK);.   
db6f0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
db700 4f 52 28 72 29 20 29 7b 0a 20 20 20 20 20 20 70  OR(r) ){.      p
db710 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
db720 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a  = tErrno;.    }.
db730 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47  #ifdef SQLITE_IG
db740 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f  NORE_FLOCK_LOCK_
db750 45 52 52 4f 52 53 0a 20 20 20 20 69 66 28 20 28  ERRORS.    if( (
db760 72 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  r & SQLITE_IOERR
db770 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  ) == SQLITE_IOER
db780 52 20 29 7b 0a 20 20 20 20 20 20 72 20 3d 20 53  R ){.      r = S
db790 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
db7a0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
db7b0 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f  TE_IGNORE_FLOCK_
db7c0 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20  LOCK_ERRORS */. 
db7d0 20 20 20 0a 20 20 20 20 72 65 74 75 72 6e 20 72     .    return r
db7e0 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
db7f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
db800 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20   = NO_LOCK;.    
db810 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
db820 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
db830 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  lose a file..*/.
db840 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b  static int flock
db850 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69  Close(sqlite3_fi
db860 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20  le *id) {.  if( 
db870 69 64 20 29 7b 0a 20 20 20 20 66 6c 6f 63 6b 55  id ){.    flockU
db880 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43  nlock(id, NO_LOC
db890 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  K);.  }.  return
db8a0 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69   closeUnixFile(i
db8b0 64 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  d);.}..#endif /*
db8c0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
db8d0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20  OCKING_STYLE && 
db8e0 21 4f 53 5f 56 58 57 4f 52 4b 20 2a 2f 0a 0a 2f  !OS_VXWORK */../
db8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db900 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 66  *** End of the f
db910 6c 6f 63 6b 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d  lock lock implem
db920 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
db930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
db940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
db990 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
db9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
db9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db9f0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 4e  ******** Begin N
dba00 61 6d 65 64 20 53 65 6d 61 70 68 6f 72 65 20 4c  amed Semaphore L
dba10 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ocking *********
dba20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
dba30 2a 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61  **.** Named sema
dba40 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73  phore locking is
dba50 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 20   only supported 
dba60 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a  on VxWorks..**.*
dba70 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b  * Semaphore lock
dba80 69 6e 67 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d  ing is like dot-
dba90 6c 6f 63 6b 20 61 6e 64 20 66 6c 6f 63 6b 20 69  lock and flock i
dbaa0 6e 20 74 68 61 74 20 69 74 20 72 65 61 6c 6c 79  n that it really
dbab0 20 6f 6e 6c 79 0a 2a 2a 20 73 75 70 70 6f 72 74   only.** support
dbac0 73 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  s EXCLUSIVE lock
dbad0 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 20 73 69 6e  ing.  Only a sin
dbae0 67 6c 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20  gle process can 
dbaf0 72 65 61 64 20 6f 72 20 77 72 69 74 65 0a 2a 2a  read or write.**
dbb00 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
dbb10 6c 65 20 61 74 20 61 20 74 69 6d 65 2e 20 20 54  le at a time.  T
dbb20 68 69 73 20 72 65 64 75 63 65 73 20 70 6f 74 65  his reduces pote
dbb30 6e 74 69 61 6c 20 63 6f 6e 63 75 72 72 65 6e 63  ntial concurrenc
dbb40 79 2c 20 62 75 74 0a 2a 2a 20 6d 61 6b 65 73 20  y, but.** makes 
dbb50 74 68 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65  the lock impleme
dbb60 6e 74 61 74 69 6f 6e 20 6d 75 63 68 20 65 61 73  ntation much eas
dbb70 69 65 72 2e 0a 2a 2f 0a 23 69 66 20 4f 53 5f 56  ier..*/.#if OS_V
dbb80 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68  XWORKS../*.** Th
dbb90 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
dbba0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
dbbb0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
dbbc0 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
dbbd0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ied.** file by t
dbbe0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
dbbf0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
dbc00 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
dbc10 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a  , set *pResOut.*
dbc20 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  * to a non-zero 
dbc30 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20  value otherwise 
dbc40 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20  *pResOut is set 
dbc50 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65  to zero.  The re
dbc60 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73  turn value.** is
dbc70 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f   set to SQLITE_O
dbc80 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20  K unless an I/O 
dbc90 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
dbca0 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e  ing lock checkin
dbcb0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
dbcc0 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65   semCheckReserve
dbcd0 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  dLock(sqlite3_fi
dbce0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65  le *id, int *pRe
dbcf0 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 72 63  sOut) {.  int rc
dbd00 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
dbd10 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30  int reserved = 0
dbd20 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
dbd30 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
dbd40 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65  )id;..  Simulate
dbd50 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
dbd60 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
dbd70 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20  CKRESERVEDLOCK; 
dbd80 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20  );.  .  assert( 
dbd90 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43  pFile );..  /* C
dbda0 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64  heck if a thread
dbdb0 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73   in this process
dbdc0 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f   holds such a lo
dbdd0 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  ck */.  if( pFil
dbde0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52  e->locktype>SHAR
dbdf0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  ED_LOCK ){.    r
dbe00 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d  eserved = 1;.  }
dbe10 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69  .  .  /* Otherwi
dbe20 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f  se see if some o
dbe30 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c  ther process hol
dbe40 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20  ds it. */.  if( 
dbe50 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20  !reserved ){.   
dbe60 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70   sem_t *pSem = p
dbe70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65  File->pOpen->pSe
dbe80 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 73 74  m;.    struct st
dbe90 61 74 20 73 74 61 74 42 75 66 3b 0a 0a 20 20 20  at statBuf;..   
dbea0 20 69 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74   if( sem_trywait
dbeb0 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20  (pSem)==-1 ){.  
dbec0 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
dbed0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66   errno;.      if
dbee0 28 20 45 41 47 41 49 4e 20 21 3d 20 74 45 72 72  ( EAGAIN != tErr
dbef0 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  no ){.        rc
dbf00 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72   = sqliteErrorFr
dbf10 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72  omPosixError(tEr
dbf20 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52  rno, SQLITE_IOER
dbf30 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c  R_CHECKRESERVEDL
dbf40 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 70 46  OCK);.        pF
dbf50 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
dbf60 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d   tErrno;.      }
dbf70 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20   else {.        
dbf80 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20  /* someone else 
dbf90 68 61 73 20 74 68 65 20 6c 6f 63 6b 20 77 68 65  has the lock whe
dbfa0 6e 20 77 65 20 61 72 65 20 69 6e 20 4e 4f 5f 4c  n we are in NO_L
dbfb0 4f 43 4b 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  OCK */.        r
dbfc0 65 73 65 72 76 65 64 20 3d 20 28 70 46 69 6c 65  eserved = (pFile
dbfd0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 53 48 41  ->locktype < SHA
dbfe0 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  RED_LOCK);.     
dbff0 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
dc000 20 20 20 20 2f 2a 20 77 65 20 63 6f 75 6c 64 20      /* we could 
dc010 68 61 76 65 20 69 74 20 69 66 20 77 65 20 77 61  have it if we wa
dc020 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 73  nt it */.      s
dc030 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3b 0a 20  em_post(pSem);. 
dc040 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41     }.  }.  OSTRA
dc050 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43  CE4("TEST WR-LOC
dc060 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70  K %d %d %d\n", p
dc070 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73  File->h, rc, res
dc080 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73  erved);..  *pRes
dc090 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a  Out = reserved;.
dc0a0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
dc0b0 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66  /*.** Lock the f
dc0c0 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63  ile with the loc
dc0d0 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70  k specified by p
dc0e0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70  arameter locktyp
dc0f0 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68  e - one.** of th
dc100 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
dc110 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45  **     (1) SHARE
dc120 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32  D_LOCK.**     (2
dc130 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a  ) RESERVED_LOCK.
dc140 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49  **     (3) PENDI
dc150 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  NG_LOCK.**     (
dc160 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  4) EXCLUSIVE_LOC
dc170 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65  K.**.** Sometime
dc180 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e  s when requestin
dc190 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65  g one lock state
dc1a0 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63  , additional loc
dc1b0 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20  k states.** are 
dc1c0 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77  inserted in betw
dc1d0 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e  een.  The lockin
dc1e0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20  g might fail on 
dc1f0 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72  one of the later
dc200 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
dc210 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b  leaving the lock
dc220 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74   state different
dc230 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74   from what it st
dc240 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69  arted but.** sti
dc250 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20  ll short of its 
dc260 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  goal.  The follo
dc270 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73  wing chart shows
dc280 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20   the allowed.** 
dc290 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20  transitions and 
dc2a0 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74  the inserted int
dc2b0 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73  ermediate states
dc2c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43  :.**.**    UNLOC
dc2d0 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a  KED -> SHARED.**
dc2e0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45      SHARED -> RE
dc2f0 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41  SERVED.**    SHA
dc300 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  RED -> (PENDING)
dc310 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
dc320 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20      RESERVED -> 
dc330 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
dc340 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e  LUSIVE.**    PEN
dc350 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56  DING -> EXCLUSIV
dc360 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f 72  E.**.** Semaphor
dc370 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79 20 72 65 61  e locks only rea
dc380 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c  lly support EXCL
dc390 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65  USIVE locks.  We
dc3a0 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69   track intermedi
dc3b0 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74  ate.** lock stat
dc3c0 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  es in the sqlite
dc3d0 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65  3_file structure
dc3e0 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20  , but all locks 
dc3f0 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f  SHARED or.** abo
dc400 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58  ve are really EX
dc410 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e  CLUSIVE locks an
dc420 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74  d exclude all ot
dc430 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66 72  her processes fr
dc440 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65  om.** access the
dc450 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   file..**.** Thi
dc460 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f  s routine will o
dc470 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c  nly increase a l
dc480 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71  ock.  Use the sq
dc490 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a  lite3OsUnlock().
dc4a0 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f  ** routine to lo
dc4b0 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65  wer a locking le
dc4c0 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  vel..*/.static i
dc4d0 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 71 6c 69 74  nt semLock(sqlit
dc4e0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
dc4f0 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75   locktype) {.  u
dc500 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
dc510 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
dc520 20 20 69 6e 74 20 66 64 3b 0a 20 20 73 65 6d 5f    int fd;.  sem_
dc530 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d  t *pSem = pFile-
dc540 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20  >pOpen->pSem;.  
dc550 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
dc560 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20  OK;..  /* if we 
dc570 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 6c  already have a l
dc580 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 63 6c 75  ock, it is exclu
dc590 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73  sive.  .  ** Jus
dc5a0 74 20 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 61  t adjust level a
dc5b0 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61  nd punt on outta
dc5c0 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28   here. */.  if (
dc5d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
dc5e0 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20  > NO_LOCK) {.   
dc5f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dc600 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20   = locktype;.   
dc610 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
dc620 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d 5f 65 6e  .    goto sem_en
dc630 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20  d_lock;.  }.  . 
dc640 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61 70 68 6f   /* lock semapho
dc650 72 65 20 6e 6f 77 20 62 75 74 20 62 61 69 6c 20  re now but bail 
dc660 6f 75 74 20 77 68 65 6e 20 61 6c 72 65 61 64 79  out when already
dc670 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 69 66   locked. */.  if
dc680 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53  ( sem_trywait(pS
dc690 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 72  em)==-1 ){.    r
dc6a0 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
dc6b0 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d 5f 65 6e  .    goto sem_en
dc6c0 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f  d_lock;.  }..  /
dc6d0 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68  * got it, set th
dc6e0 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72  e type and retur
dc6f0 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d  n ok */.  pFile-
dc700 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
dc710 74 79 70 65 3b 0a 0a 20 73 65 6d 5f 65 6e 64 5f  type;.. sem_end_
dc720 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72 6e 20 72  lock:.  return r
dc730 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65  c;.}../*.** Lowe
dc740 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  r the locking le
dc750 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63  vel on file desc
dc760 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20  riptor pFile to 
dc770 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74  locktype.  lockt
dc780 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65  ype.** must be e
dc790 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72  ither NO_LOCK or
dc7a0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a   SHARED_LOCK..**
dc7b0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69  .** If the locki
dc7c0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  ng level of the 
dc7d0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
dc7e0 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72  is already at or
dc7f0 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65   below.** the re
dc800 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20  quested locking 
dc810 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74  level, this rout
dc820 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
dc830 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
dc840 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  mUnlock(sqlite3_
dc850 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
dc860 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78  cktype) {.  unix
dc870 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
dc880 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73  nixFile*)id;.  s
dc890 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69  em_t *pSem = pFi
dc8a0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b  le->pOpen->pSem;
dc8b0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
dc8c0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
dc8d0 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  Sem );.  OSTRACE
dc8e0 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64  5("UNLOCK  %d %d
dc8f0 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e   was %d pid=%d\n
dc900 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63  ", pFile->h, loc
dc910 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65  ktype,..   pFile
dc920 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70  ->locktype, getp
dc930 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28  id());.  assert(
dc940 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45   locktype<=SHARE
dc950 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f  D_LOCK );.  .  /
dc960 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69  * no-op if possi
dc970 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ble */.  if( pFi
dc980 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f  le->locktype==lo
dc990 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65  cktype ){.    re
dc9a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
dc9b0 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72    }.  .  /* shar
dc9c0 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73  ed can just be s
dc9d0 65 74 20 62 65 63 61 75 73 65 20 77 65 20 61 6c  et because we al
dc9e0 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 63  ways have an exc
dc9f0 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28  lusive */.  if (
dca00 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
dca10 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69  _LOCK) {.    pFi
dca20 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
dca30 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74  ocktype;.    ret
dca40 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
dca50 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72   }.  .  /* no, r
dca60 65 61 6c 6c 79 20 75 6e 6c 6f 63 6b 2e 20 2a 2f  eally unlock. */
dca70 0a 20 20 69 66 20 28 20 73 65 6d 5f 70 6f 73 74  .  if ( sem_post
dca80 28 70 53 65 6d 29 3d 3d 2d 31 20 29 20 7b 0a 20  (pSem)==-1 ) {. 
dca90 20 20 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e     int rc, tErrn
dcaa0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72  o = errno;.    r
dcab0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
dcac0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
dcad0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
dcae0 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20  RR_UNLOCK);.    
dcaf0 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
dcb00 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70  R(rc) ){.      p
dcb10 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
dcb20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a  = tErrno;.    }.
dcb30 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a      return rc; .
dcb40 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63    }.  pFile->loc
dcb50 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b  ktype = NO_LOCK;
dcb60 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
dcb70 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43  _OK;.}../*. ** C
dcb80 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 20 2a 2f  lose a file.. */
dcb90 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43  .static int semC
dcba0 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
dcbb0 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69  e *id) {.  if( i
dcbc0 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c  d ){.    unixFil
dcbd0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
dcbe0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 73 65  File*)id;.    se
dcbf0 6d 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c  mUnlock(id, NO_L
dcc00 4f 43 4b 29 3b 0a 20 20 20 20 61 73 73 65 72 74  OCK);.    assert
dcc10 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 75  ( pFile );.    u
dcc20 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
dcc30 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b  .    releaseLock
dcc40 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63  Info(pFile->pLoc
dcc50 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f  k);.    releaseO
dcc60 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f  penCnt(pFile->pO
dcc70 70 65 6e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55  pen);.    closeU
dcc80 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 20  nixFile(id);.   
dcc90 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
dcca0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
dccb0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65  SQLITE_OK;.}..#e
dccc0 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52  ndif /* OS_VXWOR
dccd0 4b 53 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65  KS */./*.** Name
dcce0 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b  d semaphore lock
dccf0 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69  ing is only avai
dcd00 6c 61 62 6c 65 20 6f 6e 20 56 78 57 6f 72 6b 73  lable on VxWorks
dcd10 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
dcd20 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  **** End of the 
dcd30 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20  named semaphore 
dcd40 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lock implementat
dcd50 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ion ************
dcd60 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
dcd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcdb0 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ***/.../********
dcdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dce00 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
dce10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dce20 2a 2a 20 42 65 67 69 6e 20 41 46 50 20 4c 6f 63  ** Begin AFP Loc
dce30 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  king ***********
dce40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dce50 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50  ******.**.** AFP
dce60 20 69 73 20 74 68 65 20 41 70 70 6c 65 20 46 69   is the Apple Fi
dce70 6c 69 6e 67 20 50 72 6f 74 6f 63 6f 6c 2e 20 20  ling Protocol.  
dce80 41 46 50 20 69 73 20 61 20 6e 65 74 77 6f 72 6b  AFP is a network
dce90 20 66 69 6c 65 73 79 73 74 65 6d 20 66 6f 75 6e   filesystem foun
dcea0 64 0a 2a 2a 20 6f 6e 20 41 70 70 6c 65 20 4d 61  d.** on Apple Ma
dceb0 63 69 6e 74 6f 73 68 20 63 6f 6d 70 75 74 65 72  cintosh computer
dcec0 73 20 2d 20 62 6f 74 68 20 4f 53 39 20 61 6e 64  s - both OS9 and
dced0 20 4f 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72   OSX..**.** Thir
dcee0 64 2d 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e  d-party implemen
dcef0 74 61 74 69 6f 6e 73 20 6f 66 20 41 46 50 20 61  tations of AFP a
dcf00 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 42  re available.  B
dcf10 75 74 20 74 68 69 73 20 63 6f 64 65 20 68 65 72  ut this code her
dcf20 65 0a 2a 2a 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  e.** only works 
dcf30 6f 6e 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20  on OSX..*/..#if 
dcf40 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
dcf50 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  _) && SQLITE_ENA
dcf60 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
dcf70 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 66 70 4c  E./*.** The afpL
dcf80 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74  ockingContext st
dcf90 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
dcfa0 20 61 6c 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70   all afp lock sp
dcfb0 65 63 69 66 69 63 20 73 74 61 74 65 0a 2a 2f 0a  ecific state.*/.
dcfc0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 61  typedef struct a
dcfd0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
dcfe0 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   afpLockingConte
dcff0 78 74 3b 0a 73 74 72 75 63 74 20 61 66 70 4c 6f  xt;.struct afpLo
dd000 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20  ckingContext {. 
dd010 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
dd020 6f 6e 67 20 73 68 61 72 65 64 42 79 74 65 3b 0a  ong sharedByte;.
dd030 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 62    const char *db
dd040 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  Path;           
dd050 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
dd060 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b   open file */.};
dd070 0a 0a 73 74 72 75 63 74 20 42 79 74 65 52 61 6e  ..struct ByteRan
dd080 67 65 4c 6f 63 6b 50 42 32 0a 7b 0a 20 20 75 6e  geLockPB2.{.  un
dd090 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
dd0a0 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20   offset;        
dd0b0 2f 2a 20 6f 66 66 73 65 74 20 74 6f 20 66 69 72  /* offset to fir
dd0c0 73 74 20 62 79 74 65 20 74 6f 20 6c 6f 63 6b 20  st byte to lock 
dd0d0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f  */.  unsigned lo
dd0e0 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 3b 20  ng long length; 
dd0f0 20 20 20 20 20 20 20 2f 2a 20 6e 62 72 20 6f 66         /* nbr of
dd100 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a   bytes to lock *
dd110 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  /.  unsigned lon
dd120 67 20 6c 6f 6e 67 20 72 65 74 52 61 6e 67 65 53  g long retRangeS
dd130 74 61 72 74 3b 20 2f 2a 20 6e 62 72 20 6f 66 20  tart; /* nbr of 
dd140 31 73 74 20 62 79 74 65 20 6c 6f 63 6b 65 64 20  1st byte locked 
dd150 69 66 20 73 75 63 63 65 73 73 66 75 6c 20 2a 2f  if successful */
dd160 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
dd170 20 75 6e 4c 6f 63 6b 46 6c 61 67 3b 20 20 20 20   unLockFlag;    
dd180 20 20 20 20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f       /* 1 = unlo
dd190 63 6b 2c 20 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a  ck, 0 = lock */.
dd1a0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
dd1b0 73 74 61 72 74 45 6e 64 46 6c 61 67 3b 20 20 20  startEndFlag;   
dd1c0 20 20 20 20 2f 2a 20 31 3d 72 65 6c 20 74 6f 20      /* 1=rel to 
dd1d0 65 6e 64 20 6f 66 20 66 6f 72 6b 2c 20 30 3d 72  end of fork, 0=r
dd1e0 65 6c 20 74 6f 20 73 74 61 72 74 20 2a 2f 0a 20  el to start */. 
dd1f0 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20   int fd;        
dd200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd210 20 20 20 2f 2a 20 66 69 6c 65 20 64 65 73 63 20     /* file desc 
dd220 74 6f 20 61 73 73 6f 63 20 74 68 69 73 20 6c 6f  to assoc this lo
dd230 63 6b 20 77 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23  ck with */.};..#
dd240 64 65 66 69 6e 65 20 61 66 70 66 73 42 79 74 65  define afpfsByte
dd250 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 20  RangeLock2FSCTL 
dd260 20 20 20 20 20 20 20 5f 49 4f 57 52 28 27 7a 27         _IOWR('z'
dd270 2c 20 32 33 2c 20 73 74 72 75 63 74 20 42 79 74  , 23, struct Byt
dd280 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 29 0a 0a  eRangeLockPB2)..
dd290 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20  /*.** This is a 
dd2a0 75 74 69 6c 69 74 79 20 66 6f 72 20 73 65 74 74  utility for sett
dd2b0 69 6e 67 20 6f 72 20 63 6c 65 61 72 69 6e 67 20  ing or clearing 
dd2c0 61 20 62 69 74 2d 72 61 6e 67 65 20 6c 6f 63 6b  a bit-range lock
dd2d0 20 6f 6e 20 61 6e 0a 2a 2a 20 41 46 50 20 66 69   on an.** AFP fi
dd2e0 6c 65 73 79 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a  lesystem..** .**
dd2f0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
dd300 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 53 51  K on success, SQ
dd310 4c 49 54 45 5f 42 55 53 59 20 6f 6e 20 66 61 69  LITE_BUSY on fai
dd320 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lure..*/.static 
dd330 69 6e 74 20 61 66 70 53 65 74 4c 6f 63 6b 28 0a  int afpSetLock(.
dd340 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61    const char *pa
dd350 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  th,             
dd360 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
dd370 66 69 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65  file to be locke
dd380 64 20 6f 72 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f  d or unlocked */
dd390 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
dd3a0 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  le,             
dd3b0 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64    /* Open file d
dd3c0 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 70 61 74  escriptor on pat
dd3d0 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  h */.  unsigned 
dd3e0 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74  long long offset
dd3f0 2c 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62  ,     /* First b
dd400 79 74 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64  yte to be locked
dd410 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c   */.  unsigned l
dd420 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c  ong long length,
dd430 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
dd440 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20  f bytes to lock 
dd450 2a 2f 0a 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b  */.  int setLock
dd460 46 6c 61 67 20 20 20 20 20 20 20 20 20 20 20 20  Flag            
dd470 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73      /* True to s
dd480 65 74 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20  et lock.  False 
dd490 74 6f 20 63 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f  to clear lock */
dd4a0 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 42 79 74  .){.  struct Byt
dd4b0 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 20 70 62  eRangeLockPB2 pb
dd4c0 3b 0a 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 0a  ;.  int err;.  .
dd4d0 20 20 70 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20    pb.unLockFlag 
dd4e0 3d 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20  = setLockFlag ? 
dd4f0 30 20 3a 20 31 3b 0a 20 20 70 62 2e 73 74 61 72  0 : 1;.  pb.star
dd500 74 45 6e 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20  tEndFlag = 0;.  
dd510 70 62 2e 6f 66 66 73 65 74 20 3d 20 6f 66 66 73  pb.offset = offs
dd520 65 74 3b 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20  et;.  pb.length 
dd530 3d 20 6c 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e  = length; .  pb.
dd540 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20  fd = pFile->h;. 
dd550 20 0a 20 20 4f 53 54 52 41 43 45 36 28 22 41 46   .  OSTRACE6("AF
dd560 50 53 45 54 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f  PSETLOCK [%s] fo
dd570 72 20 25 64 25 73 20 69 6e 20 72 61 6e 67 65 20  r %d%s in range 
dd580 25 6c 6c 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20  %llx:%llx\n", . 
dd590 20 20 20 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f     (setLockFlag?
dd5a0 22 4f 4e 22 3a 22 4f 46 46 22 29 2c 20 70 46 69  "ON":"OFF"), pFi
dd5b0 6c 65 2d 3e 68 2c 20 28 70 62 2e 66 64 3d 3d 2d  le->h, (pb.fd==-
dd5c0 31 3f 22 5b 74 65 73 74 76 61 6c 2d 31 5d 22 3a  1?"[testval-1]":
dd5d0 22 22 29 2c 0a 20 20 20 20 6f 66 66 73 65 74 2c  ""),.    offset,
dd5e0 20 6c 65 6e 67 74 68 29 3b 0a 20 20 65 72 72 20   length);.  err 
dd5f0 3d 20 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66  = fsctl(path, af
dd600 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b  pfsByteRangeLock
dd610 32 46 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b  2FSCTL, &pb, 0);
dd620 0a 20 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20  .  if ( err==-1 
dd630 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a  ) {.    int rc;.
dd640 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
dd650 20 65 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52   errno;.    OSTR
dd660 41 43 45 34 28 22 41 46 50 53 45 54 4c 4f 43 4b  ACE4("AFPSETLOCK
dd670 20 66 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c   failed to fsctl
dd680 28 29 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22  () '%s' %d %s\n"
dd690 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 70  ,.             p
dd6a0 61 74 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72  ath, tErrno, str
dd6b0 65 72 72 6f 72 28 74 45 72 72 6e 6f 29 29 3b 0a  error(tErrno));.
dd6c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47  #ifdef SQLITE_IG
dd6d0 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52  NORE_AFP_LOCK_ER
dd6e0 52 4f 52 53 0a 20 20 20 20 72 63 20 3d 20 53 51  RORS.    rc = SQ
dd6f0 4c 49 54 45 5f 42 55 53 59 3b 0a 23 65 6c 73 65  LITE_BUSY;.#else
dd700 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
dd710 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
dd720 72 6f 72 28 74 45 72 72 6e 6f 2c 0a 20 20 20 20  ror(tErrno,.    
dd730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd740 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51  setLockFlag ? SQ
dd750 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20  LITE_IOERR_LOCK 
dd760 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  : SQLITE_IOERR_U
dd770 4e 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 20 2f  NLOCK);.#endif /
dd780 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f  * SQLITE_IGNORE_
dd790 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20  AFP_LOCK_ERRORS 
dd7a0 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  */.    if( IS_LO
dd7b0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
dd7c0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
dd7d0 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
dd7e0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
dd7f0 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  n rc;.  } else {
dd800 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
dd810 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  TE_OK;.  }.}../*
dd820 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
dd830 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
dd840 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
dd850 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
dd860 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
dd870 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
dd880 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
dd890 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
dd8a0 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65  s held, set *pRe
dd8b0 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e  sOut.** to a non
dd8c0 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65  -zero value othe
dd8d0 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69  rwise *pResOut i
dd8e0 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20  s set to zero.  
dd8f0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
dd900 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51  .** is set to SQ
dd910 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61  LITE_OK unless a
dd920 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75  n I/O error occu
dd930 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63  rs during lock c
dd940 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  hecking..*/.stat
dd950 69 63 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52  ic int afpCheckR
dd960 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
dd970 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
dd980 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69  t *pResOut){.  i
dd990 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
dd9a0 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65  K;.  int reserve
dd9b0 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c  d = 0;.  unixFil
dd9c0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
dd9d0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53  File*)id;.  .  S
dd9e0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
dd9f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
dda00 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45  ERR_CHECKRESERVE
dda10 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61  DLOCK; );.  .  a
dda20 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
dda30 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74    afpLockingCont
dda40 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28  ext *context = (
dda50 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
dda60 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  t *) pFile->lock
dda70 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20  ingContext;.  . 
dda80 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74   /* Check if a t
dda90 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72  hread in this pr
ddaa0 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68  ocess holds such
ddab0 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28   a lock */.  if(
ddac0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
ddad0 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  >SHARED_LOCK ){.
ddae0 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31      reserved = 1
ddaf0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74  ;.  }.  .  /* Ot
ddb00 68 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73  herwise see if s
ddb10 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73  ome other proces
ddb20 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 20 2a  s holds it..   *
ddb30 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65  /.  if( !reserve
ddb40 64 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b  d ){.    /* lock
ddb50 20 74 68 65 20 52 45 53 45 52 56 45 44 20 62 79   the RESERVED by
ddb60 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72  te */.    int lr
ddb70 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  c = afpSetLock(c
ddb80 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
ddb90 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f  pFile, RESERVED_
ddba0 42 59 54 45 2c 20 31 2c 31 29 3b 20 20 0a 20 20  BYTE, 1,1);  .  
ddbb0 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
ddbc0 3d 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a  =lrc ){.      /*
ddbd0 20 69 66 20 77 65 20 73 75 63 63 65 65 64 65 64   if we succeeded
ddbe0 20 69 6e 20 74 61 6b 69 6e 67 20 74 68 65 20 72   in taking the r
ddbf0 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c 20 75 6e  eserved lock, un
ddc00 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65 73 74 6f  lock it to resto
ddc10 72 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  re.      ** the 
ddc20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 2a  original state *
ddc30 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 61 66  /.      lrc = af
ddc40 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  pSetLock(context
ddc50 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
ddc60 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
ddc70 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 65 6c 73  1, 0);.    } els
ddc80 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20  e {.      /* if 
ddc90 77 65 20 66 61 69 6c 65 64 20 74 6f 20 67 65 74  we failed to get
ddca0 20 74 68 65 20 6c 6f 63 6b 20 74 68 65 6e 20 73   the lock then s
ddcb0 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 75 73 74  omeone else must
ddcc0 20 68 61 76 65 20 69 74 20 2a 2f 0a 20 20 20 20   have it */.    
ddcd0 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
ddce0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53      }.    if( IS
ddcf0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29  _LOCK_ERROR(lrc)
ddd00 20 29 7b 0a 20 20 20 20 20 20 72 63 3d 6c 72 63   ){.      rc=lrc
ddd10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20  ;.    }.  }.  . 
ddd20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20   OSTRACE4("TEST 
ddd30 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64  WR-LOCK %d %d %d
ddd40 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
ddd50 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 20 20  c, reserved);.  
ddd60 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65  .  *pResOut = re
ddd70 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e  served;.  return
ddd80 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
ddd90 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68  ck the file with
ddda0 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66   the lock specif
dddb0 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
dddc0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a   locktype - one.
dddd0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** of the follow
ddde0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  ing:.**.**     (
dddf0 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a  1) SHARED_LOCK.*
dde00 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56  *     (2) RESERV
dde10 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
dde20 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a  3) PENDING_LOCK.
dde30 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55  **     (4) EXCLU
dde40 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20  SIVE_LOCK.**.** 
dde50 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72  Sometimes when r
dde60 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f  equesting one lo
dde70 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69  ck state, additi
dde80 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73  onal lock states
dde90 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64  .** are inserted
ddea0 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68   in between.  Th
ddeb0 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20  e locking might 
ddec0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74  fail on one of t
dded0 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e  he later.** tran
ddee0 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20  sitions leaving 
ddef0 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64  the lock state d
ddf00 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68  ifferent from wh
ddf10 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75  at it started bu
ddf20 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74  t.** still short
ddf30 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54   of its goal.  T
ddf40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
ddf50 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c  rt shows the all
ddf60 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69  owed.** transiti
ddf70 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65  ons and the inse
ddf80 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74  rted intermediat
ddf90 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
ddfa0 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53     UNLOCKED -> S
ddfb0 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  HARED.**    SHAR
ddfc0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a  ED -> RESERVED.*
ddfd0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28  *    SHARED -> (
ddfe0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
ddff0 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45  USIVE.**    RESE
de000 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  RVED -> (PENDING
de010 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
de020 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20  *    PENDING -> 
de030 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20  EXCLUSIVE.**.** 
de040 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
de050 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20  l only increase 
de060 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65  a lock.  Use the
de070 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b   sqlite3OsUnlock
de080 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f  ().** routine to
de090 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67   lower a locking
de0a0 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69   level..*/.stati
de0b0 63 20 69 6e 74 20 61 66 70 4c 6f 63 6b 28 73 71  c int afpLock(sq
de0c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
de0d0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20  int locktype){. 
de0e0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
de0f0 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  _OK;.  unixFile 
de100 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
de110 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63  le*)id;.  afpLoc
de120 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e  kingContext *con
de130 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69  text = (afpLocki
de140 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69  ngContext *) pFi
de150 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
de160 78 74 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28  xt;.  .  assert(
de170 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52   pFile );.  OSTR
de180 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE5("LOCK    %d
de190 20 25 73 20 77 61 73 20 25 73 20 70 69 64 3d 25   %s was %s pid=%
de1a0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  d\n", pFile->h,.
de1b0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70           locktyp
de1c0 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c  eName(locktype),
de1d0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46   locktypeName(pF
de1e0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20  ile->locktype), 
de1f0 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a  getpid());..  /*
de200 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72   If there is alr
de210 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74  eady a lock of t
de220 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65  his type or more
de230 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20   restrictive on 
de240 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c  the.  ** unixFil
de250 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44  e, do nothing. D
de260 6f 6e 27 74 20 75 73 65 20 74 68 65 20 61 66 70  on't use the afp
de270 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20  _end_lock: exit 
de280 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75 6e  path, as.  ** un
de290 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68  ixEnterMutex() h
de2a0 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65  asn't been calle
de2b0 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  d yet..  */.  if
de2c0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
de2d0 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  e>=locktype ){. 
de2e0 20 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43     OSTRACE3("LOC
de2f0 4b 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61  K    %d %s ok (a
de300 6c 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c  lready held)\n",
de310 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20   pFile->h,.     
de320 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61        locktypeNa
de330 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20  me(locktype));. 
de340 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
de350 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  _OK;.  }..  /* M
de360 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
de370 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
de380 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20   correct.  */.  
de390 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
de3a0 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
de3b0 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   || locktype==SH
de3c0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
de3d0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
de3e0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b  =PENDING_LOCK );
de3f0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
de400 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype!=RESERVED_LO
de410 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK || pFile->loc
de420 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
de430 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68  CK );.  .  /* Th
de440 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64  is mutex is need
de450 65 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65  ed because pFile
de460 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65  ->pLock is share
de470 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73  d across threads
de480 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65  .  */.  unixEnte
de490 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20  rMutex();..  /* 
de4a0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 75  Make sure the cu
de4b0 72 72 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e  rrent thread own
de4c0 73 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a  s the pFile..  *
de4d0 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65  /.  rc = transfe
de4e0 72 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65  rOwnership(pFile
de4f0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
de500 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e  ITE_OK ){.    un
de510 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
de520 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
de530 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20 41 20 50   }.    .  /* A P
de540 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e  ENDING lock is n
de550 65 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 71  eeded before acq
de560 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20  uiring a SHARED 
de570 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a  lock and before.
de580 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61    ** acquiring a
de590 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
de5a0 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 45  .  For the SHARE
de5b0 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44  D lock, the PEND
de5c0 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65  ING will.  ** be
de5d0 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a   released..  */.
de5e0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
de5f0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20  SHARED_LOCK .   
de600 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d     || (locktype=
de610 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
de620 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  && pFile->lockty
de630 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29  pe<PENDING_LOCK)
de640 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61  .  ){.    int fa
de650 69 6c 65 64 3b 0a 20 20 20 20 66 61 69 6c 65 64  iled;.    failed
de660 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f   = afpSetLock(co
de670 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70  ntext->dbPath, p
de680 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59  File, PENDING_BY
de690 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69  TE, 1, 1);.    i
de6a0 66 20 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20  f (failed) {.   
de6b0 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a     rc = failed;.
de6c0 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65        goto afp_e
de6d0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20  nd_lock;.    }. 
de6e0 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f   }.  .  /* If co
de6f0 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68  ntrol gets to th
de700 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61  is point, then a
de710 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64  ctually go ahead
de720 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f   and make.  ** o
de730 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
de740 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70  calls for the sp
de750 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20  ecified lock..  
de760 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
de770 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
de780 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c 20 6c 72  {.    int lk, lr
de790 63 31 2c 20 6c 72 63 32 2c 20 6c 72 63 31 45 72  c1, lrc2, lrc1Er
de7a0 72 6e 6f 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a  rno;.    .    /*
de7b0 20 4e 6f 77 20 67 65 74 20 74 68 65 20 72 65 61   Now get the rea
de7c0 64 2d 6c 6f 63 6b 20 53 48 41 52 45 44 5f 4c 4f  d-lock SHARED_LO
de7d0 43 4b 20 2a 2f 0a 20 20 20 20 2f 2a 20 6e 6f 74  CK */.    /* not
de7e0 65 20 74 68 61 74 20 74 68 65 20 71 75 61 6c 69  e that the quali
de7f0 74 79 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d  ty of the random
de800 6e 65 73 73 20 64 6f 65 73 6e 27 74 20 6d 61 74  ness doesn't mat
de810 74 65 72 20 74 68 61 74 20 6d 75 63 68 20 2a 2f  ter that much */
de820 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e 64 6f 6d  .    lk = random
de830 28 29 3b 20 0a 20 20 20 20 63 6f 6e 74 65 78 74  (); .    context
de840 2d 3e 73 68 61 72 65 64 42 79 74 65 20 3d 20 28  ->sharedByte = (
de850 6c 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 29  lk & 0x7fffffff)
de860 25 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d 20  %(SHARED_SIZE - 
de870 31 29 3b 0a 20 20 20 20 6c 72 63 31 20 3d 20 61  1);.    lrc1 = a
de880 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  fpSetLock(contex
de890 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  t->dbPath, pFile
de8a0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 48 41  , .          SHA
de8b0 52 45 44 5f 46 49 52 53 54 2b 63 6f 6e 74 65 78  RED_FIRST+contex
de8c0 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31  t->sharedByte, 1
de8d0 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 49 53  , 1);.    if( IS
de8e0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31  _LOCK_ERROR(lrc1
de8f0 29 20 29 7b 0a 20 20 20 20 20 20 6c 72 63 31 45  ) ){.      lrc1E
de900 72 72 6e 6f 20 3d 20 70 46 69 6c 65 2d 3e 6c 61  rrno = pFile->la
de910 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20  stErrno;.    }. 
de920 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74     /* Drop the t
de930 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e 47  emporary PENDING
de940 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 72 63   lock */.    lrc
de950 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  2 = afpSetLock(c
de960 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
de970 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42  pFile, PENDING_B
de980 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  YTE, 1, 0);.    
de990 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b  .    if( IS_LOCK
de9a0 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 29 20 7b  _ERROR(lrc1) ) {
de9b0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
de9c0 73 74 45 72 72 6e 6f 20 3d 20 6c 72 63 31 45 72  stErrno = lrc1Er
de9d0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rno;.      rc = 
de9e0 6c 72 63 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f  lrc1;.      goto
de9f0 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20   afp_end_lock;. 
dea00 20 20 20 7d 20 65 6c 73 65 20 69 66 28 20 49 53     } else if( IS
dea10 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 32  _LOCK_ERROR(lrc2
dea20 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
dea30 6c 72 63 32 3b 0a 20 20 20 20 20 20 67 6f 74 6f  lrc2;.      goto
dea40 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20   afp_end_lock;. 
dea50 20 20 20 7d 20 65 6c 73 65 20 69 66 28 20 6c 72     } else if( lr
dea60 63 31 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20  c1 != SQLITE_OK 
dea70 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c  ) {.      rc = l
dea80 72 63 31 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20  rc1;.    } else 
dea90 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
deaa0 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44  ocktype = SHARED
deab0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69  _LOCK;.      pFi
deac0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b  le->pOpen->nLock
dead0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ++;.    }.  }els
deae0 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65  e{.    /* The re
deaf0 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20  quest was for a 
deb00 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c  RESERVED or EXCL
deb10 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20  USIVE lock.  It 
deb20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65  is.    ** assume
deb30 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20  d that there is 
deb40 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61  a SHARED or grea
deb50 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ter lock on the 
deb60 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65  file.    ** alre
deb70 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ady..    */.    
deb80 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 30 3b 0a  int failed = 0;.
deb90 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70      assert( 0!=p
deba0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
debb0 3b 0a 20 20 20 20 69 66 20 28 6c 6f 63 6b 74 79  ;.    if (lockty
debc0 70 65 20 3e 3d 20 52 45 53 45 52 56 45 44 5f 4c  pe >= RESERVED_L
debd0 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f  OCK && pFile->lo
debe0 63 6b 74 79 70 65 20 3c 20 52 45 53 45 52 56 45  cktype < RESERVE
debf0 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20  D_LOCK) {.      
dec00 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52    /* Acquire a R
dec10 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a  ESERVED lock */.
dec20 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d          failed =
dec30 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
dec40 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
dec50 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  le, RESERVED_BYT
dec60 45 2c 20 31 2c 31 29 3b 0a 20 20 20 20 7d 0a 20  E, 1,1);.    }. 
dec70 20 20 20 69 66 20 28 21 66 61 69 6c 65 64 20 26     if (!failed &
dec80 26 20 6c 6f 63 6b 74 79 70 65 20 3d 3d 20 45 58  & locktype == EX
dec90 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a  CLUSIVE_LOCK) {.
deca0 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65        /* Acquire
decb0 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
decc0 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 0a 20  ck */.        . 
decd0 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74       /* Remove t
dece0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 62  he shared lock b
decf0 65 66 6f 72 65 20 74 72 79 69 6e 67 20 74 68 65  efore trying the
ded00 20 72 61 6e 67 65 2e 20 20 77 65 27 6c 6c 20 6e   range.  we'll n
ded10 65 65 64 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a  eed to .      **
ded20 20 72 65 65 73 74 61 62 6c 69 73 68 20 74 68 65   reestablish the
ded30 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 66 20   shared lock if 
ded40 77 65 20 63 61 6e 27 74 20 67 65 74 20 74 68 65  we can't get the
ded50 20 20 61 66 70 55 6e 6c 6f 63 6b 0a 20 20 20 20    afpUnlock.    
ded60 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21    */.      if( !
ded70 28 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74  (failed = afpSet
ded80 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
ded90 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41  Path, pFile, SHA
deda0 52 45 44 5f 46 49 52 53 54 20 2b 0a 20 20 20 20  RED_FIRST +.    
dedb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dedc0 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68       context->sh
dedd0 61 72 65 64 42 79 74 65 2c 20 31 2c 20 30 29 29  aredByte, 1, 0))
dede0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
dedf0 66 61 69 6c 65 64 32 20 3d 20 53 51 4c 49 54 45  failed2 = SQLITE
dee00 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  _OK;.        /* 
dee10 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20 74 6f 20  now attemmpt to 
dee20 67 65 74 20 74 68 65 20 65 78 63 6c 75 73 69 76  get the exclusiv
dee30 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20 2a 2f 0a  e lock range */.
dee40 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d          failed =
dee50 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
dee60 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
dee70 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54  le, SHARED_FIRST
dee80 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
dee90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
deea0 20 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 31    SHARED_SIZE, 1
deeb0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  );.        if( f
deec0 61 69 6c 65 64 20 26 26 20 28 66 61 69 6c 65 64  ailed && (failed
deed0 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  2 = afpSetLock(c
deee0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
deef0 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20  pFile, .        
def00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
def10 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 63 6f  HARED_FIRST + co
def20 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74  ntext->sharedByt
def30 65 2c 20 31 2c 20 31 29 29 20 29 7b 0a 20 20 20  e, 1, 1)) ){.   
def40 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 27 74 20         /* Can't 
def50 72 65 65 73 74 61 62 6c 69 73 68 20 74 68 65 20  reestablish the 
def60 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 53 71  shared lock.  Sq
def70 6c 69 74 65 20 63 61 6e 27 74 20 64 65 61 6c 2c  lite can't deal,
def80 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 20 20   this is.       
def90 20 20 20 2a 2a 20 61 20 63 72 69 74 69 63 61 6c     ** a critical
defa0 20 49 2f 4f 20 65 72 72 6f 72 0a 20 20 20 20 20   I/O error.     
defb0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
defc0 20 20 72 63 20 3d 20 28 28 66 61 69 6c 65 64 20    rc = ((failed 
defd0 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20  & SQLITE_IOERR) 
defe0 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29  == SQLITE_IOERR)
deff0 20 3f 20 66 61 69 6c 65 64 32 20 3a 20 0a 20 20   ? failed2 : .  
df000 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
df010 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 3b 0a  ITE_IOERR_LOCK;.
df020 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61            goto a
df030 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20  fp_end_lock;.   
df040 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 7d 65       } .      }e
df050 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20  lse{.        rc 
df060 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20 20 20 20  = failed; .     
df070 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
df080 20 66 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20   failed ){.     
df090 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20   rc = failed;.  
df0a0 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28    }.  }.  .  if(
df0b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
df0c0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
df0d0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
df0e0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f  ;.  }else if( lo
df0f0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
df100 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46  E_LOCK ){.    pF
df110 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
df120 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20  PENDING_LOCK;.  
df130 7d 0a 20 20 0a 61 66 70 5f 65 6e 64 5f 6c 6f 63  }.  .afp_end_loc
df140 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  k:.  unixLeaveMu
df150 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45  tex();.  OSTRACE
df160 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73  4("LOCK    %d %s
df170 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %s\n", pFile->h
df180 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c  , locktypeName(l
df190 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20  ocktype), .     
df1a0 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f      rc==SQLITE_O
df1b0 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c  K ? "ok" : "fail
df1c0 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ed");.  return r
df1d0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65  c;.}../*.** Lowe
df1e0 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  r the locking le
df1f0 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63  vel on file desc
df200 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20  riptor pFile to 
df210 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74  locktype.  lockt
df220 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65  ype.** must be e
df230 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72  ither NO_LOCK or
df240 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a   SHARED_LOCK..**
df250 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69  .** If the locki
df260 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  ng level of the 
df270 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
df280 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72  is already at or
df290 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65   below.** the re
df2a0 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20  quested locking 
df2b0 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74  level, this rout
df2c0 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
df2d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66  */.static int af
df2e0 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  pUnlock(sqlite3_
df2f0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
df300 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20  cktype) {.  int 
df310 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
df320 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
df330 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
df340 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43  d;.  afpLockingC
df350 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28  ontext *pCtx = (
df360 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
df370 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  t *) pFile->lock
df380 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61  ingContext;..  a
df390 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
df3a0 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f    OSTRACE5("UNLO
df3b0 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64  CK  %d %d was %d
df3c0 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   pid=%d\n", pFil
df3d0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a  e->h, locktype,.
df3e0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
df3f0 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64  locktype, getpid
df400 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ());..  assert( 
df410 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44  locktype<=SHARED
df420 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70  _LOCK );.  if( p
df430 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d  File->locktype<=
df440 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20  locktype ){.    
df450 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
df460 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43  ;.  }.  if( CHEC
df470 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c 65  K_THREADID(pFile
df480 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
df490 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
df4a0 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75   }.  unixEnterMu
df4b0 74 65 78 28 29 3b 0a 20 20 69 66 28 20 70 46 69  tex();.  if( pFi
df4c0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41  le->locktype>SHA
df4d0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
df4e0 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  .    if( pFile->
df4f0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
df500 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  IVE_LOCK ){.    
df510 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63    rc = afpSetLoc
df520 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20  k(pCtx->dbPath, 
df530 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  pFile, SHARED_FI
df540 52 53 54 2c 20 53 48 41 52 45 44 5f 53 49 5a 45  RST, SHARED_SIZE
df550 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
df560 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
df570 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
df580 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20  D_LOCK ){.      
df590 20 20 2f 2a 20 6f 6e 6c 79 20 72 65 2d 65 73 74    /* only re-est
df5a0 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 65  ablish the share
df5b0 64 20 6c 6f 63 6b 20 69 66 20 6e 65 63 65 73 73  d lock if necess
df5c0 61 72 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ary */.        i
df5d0 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  nt sharedLockByt
df5e0 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  e = SHARED_FIRST
df5f0 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42 79 74  +pCtx->sharedByt
df600 65 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e;.        rc = 
df610 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d  afpSetLock(pCtx-
df620 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20  >dbPath, pFile, 
df630 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20  sharedLockByte, 
df640 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  1, 1);.      }. 
df650 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
df660 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46  =SQLITE_OK && pF
df670 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50  ile->locktype>=P
df680 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20  ENDING_LOCK ){. 
df690 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74       rc = afpSet
df6a0 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74  Lock(pCtx->dbPat
df6b0 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e  h, pFile, PENDIN
df6c0 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20  G_BYTE, 1, 0);. 
df6d0 20 20 20 7d 20 0a 20 20 20 20 69 66 28 20 72 63     } .    if( rc
df6e0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
df6f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  File->locktype>=
df700 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b  RESERVED_LOCK ){
df710 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 53  .      rc = afpS
df720 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50  etLock(pCtx->dbP
df730 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45  ath, pFile, RESE
df740 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 29  RVED_BYTE, 1, 0)
df750 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
df760 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  if( locktype==NO
df770 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20  _LOCK ){.    /* 
df780 63 6c 65 61 72 20 74 68 65 20 73 68 61 72 65 64  clear the shared
df790 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74   lock */.    int
df7a0 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 20   sharedLockByte 
df7b0 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 70  = SHARED_FIRST+p
df7c0 43 74 78 2d 3e 73 68 61 72 65 64 42 79 74 65 3b  Ctx->sharedByte;
df7d0 0a 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74  .    rc = afpSet
df7e0 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74  Lock(pCtx->dbPat
df7f0 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64  h, pFile, shared
df800 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 30 29 3b  LockByte, 1, 0);
df810 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
df820 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
df830 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e   if( locktype==N
df840 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20  O_LOCK ){.      
df850 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
df860 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c  nt *pOpen = pFil
df870 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20  e->pOpen;.      
df880 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a  pOpen->nLock--;.
df890 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
df8a0 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b  pen->nLock>=0 );
df8b0 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e  .      if( pOpen
df8c0 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 4f  ->nLock==0 && pO
df8d0 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3e 30 20  pen->nPending>0 
df8e0 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
df8f0 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
df900 30 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 65 6e  0; i<pOpen->nPen
df910 64 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ding; i++){.    
df920 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d        if( pOpen-
df930 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3c 20 30  >aPending[i] < 0
df940 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
df950 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65         if( close
df960 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67  (pOpen->aPending
df970 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20  [i]) ){.        
df980 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
df990 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
df9a0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
df9b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53  QLITE_IOERR_CLOS
df9c0 45 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  E;.          }el
df9d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
df9e0 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b  pOpen->aPending[
df9f0 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20  i] = -1;.       
dfa00 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
dfa10 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
dfa20 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
dfa30 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
dfa40 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69  ee(pOpen->aPendi
dfa50 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  ng);.          p
dfa60 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d  Open->nPending =
dfa70 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f   0;.          pO
dfa80 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20  pen->aPending = 
dfa90 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
dfaa0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
dfab0 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
dfac0 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
dfad0 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e  ITE_OK ) pFile->
dfae0 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
dfaf0 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ype;.  return rc
dfb00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
dfb10 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61 6e 75   a file & cleanu
dfb20 70 20 41 46 50 20 73 70 65 63 69 66 69 63 20 6c  p AFP specific l
dfb30 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 0a  ocking context .
dfb40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66  */.static int af
dfb50 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  pClose(sqlite3_f
dfb60 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28  ile *id) {.  if(
dfb70 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46   id ){.    unixF
dfb80 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
dfb90 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20  ixFile*)id;.    
dfba0 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f  afpUnlock(id, NO
dfbb0 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69 78  _LOCK);.    unix
dfbc0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
dfbd0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70    if( pFile->pOp
dfbe0 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70  en && pFile->pOp
dfbf0 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20  en->nLock ){.   
dfc00 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61     /* If there a
dfc10 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c  re outstanding l
dfc20 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74  ocks, do not act
dfc30 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20  ually close the 
dfc40 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20  file just.      
dfc50 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74  ** yet because t
dfc60 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20  hat would clear 
dfc70 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e  those locks.  In
dfc80 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66  stead, add the f
dfc90 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73  ile.      ** des
dfca0 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e  criptor to pOpen
dfcb0 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 74 20  ->aPending.  It 
dfcc0 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
dfcd0 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68 65  cally closed whe
dfce0 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c  n.      ** the l
dfcf0 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61  ast lock is clea
dfd00 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  red..      */.  
dfd10 20 20 20 20 69 6e 74 20 2a 61 4e 65 77 3b 0a 20      int *aNew;. 
dfd20 20 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78       struct unix
dfd30 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d  OpenCnt *pOpen =
dfd40 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20   pFile->pOpen;. 
dfd50 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69       aNew = sqli
dfd60 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 4f 70 65  te3_realloc(pOpe
dfd70 6e 2d 3e 61 50 65 6e 64 69 6e 67 2c 20 28 70 4f  n->aPending, (pO
dfd80 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 31 29  pen->nPending+1)
dfd90 2a 73 69 7a 65 6f 66 28 69 6e 74 29 20 29 3b 0a  *sizeof(int) );.
dfda0 20 20 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d        if( aNew==
dfdb0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
dfdc0 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  If a malloc fail
dfdd0 73 2c 20 6a 75 73 74 20 6c 65 61 6b 20 74 68 65  s, just leak the
dfde0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
dfdf0 20 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   */.      }else{
dfe00 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  .        pOpen->
dfe10 61 50 65 6e 64 69 6e 67 20 3d 20 61 4e 65 77 3b  aPending = aNew;
dfe20 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  .        pOpen->
dfe30 61 50 65 6e 64 69 6e 67 5b 70 4f 70 65 6e 2d 3e  aPending[pOpen->
dfe40 6e 50 65 6e 64 69 6e 67 5d 20 3d 20 70 46 69 6c  nPending] = pFil
dfe50 65 2d 3e 68 3b 0a 20 20 20 20 20 20 20 20 70 4f  e->h;.        pO
dfe60 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 2b 3b  pen->nPending++;
dfe70 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
dfe80 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a  h = -1;.      }.
dfe90 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73      }.    releas
dfea0 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e  eOpenCnt(pFile->
dfeb0 70 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69  pOpen);.    sqli
dfec0 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e  te3_free(pFile->
dfed0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b  lockingContext);
dfee0 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69  .    closeUnixFi
dfef0 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78  le(id);.    unix
dff00 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
dff10 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
dff20 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  E_OK;.}..#endif 
dff30 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  /* defined(__APP
dff40 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f  LE__) && SQLITE_
dff50 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
dff60 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68  TYLE */./*.** Th
dff70 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20  e code above is 
dff80 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70  the AFP lock imp
dff90 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
dffa0 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 66  e code is specif
dffb0 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20  ic.** to MacOSX 
dffc0 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72  and does not wor
dffd0 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20  k on other unix 
dffe0 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61  platforms.  No a
dfff0 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73  lternative.** is
e0000 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20   available.  If 
e0010 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c  you don't compil
e0020 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65  e for a mac, the
e0030 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22  n the "unix-afp"
e0040 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 61  .** VFS is not a
e0050 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a  vailable..**.***
e0060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0070 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 46  ** End of the AF
e0080 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  P lock implement
e0090 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ation **********
e00a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
e00b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e00c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e00d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e00e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e00f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f  ***********/.../
e0100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
e0150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
e0160 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69  Non-locking sqli
e0170 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
e0180 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
e0190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
e01a0 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 64 69  *.** The next di
e01b0 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  vision contains 
e01c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
e01d0 66 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20  for all methods 
e01e0 6f 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  of the .** sqlit
e01f0 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f  e3_file object o
e0200 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6c 6f  ther than the lo
e0210 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20  cking methods.  
e0220 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d  The locking.** m
e0230 65 74 68 6f 64 73 20 77 65 72 65 20 64 65 66 69  ethods were defi
e0240 6e 65 64 20 69 6e 20 64 69 76 69 73 69 6f 6e 73  ned in divisions
e0250 20 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b   above (one lock
e0260 69 6e 67 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a  ing method per.*
e0270 2a 20 64 69 76 69 73 69 6f 6e 29 2e 20 20 54 68  * division).  Th
e0280 6f 73 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74  ose methods that
e0290 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61   are common to a
e02a0 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73  ll locking modes
e02b0 0a 2a 2a 20 61 72 65 20 67 61 74 68 65 72 20 74  .** are gather t
e02c0 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 74 68 69  ogether into thi
e02d0 73 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a  s division..*/..
e02e0 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68  /*.** Seek to th
e02f0 65 20 6f 66 66 73 65 74 20 70 61 73 73 65 64 20  e offset passed 
e0300 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
e0310 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 65 61  gument, then rea
e0320 64 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20  d cnt .** bytes 
e0330 69 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 75 72  into pBuf. Retur
e0340 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
e0350 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72  bytes actually r
e0360 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20  ead..**.** NB:  
e0370 49 66 20 79 6f 75 20 64 65 66 69 6e 65 20 55 53  If you define US
e0380 45 5f 50 52 45 41 44 20 6f 72 20 55 53 45 5f 50  E_PREAD or USE_P
e0390 52 45 41 44 36 34 2c 20 74 68 65 6e 20 69 74 20  READ64, then it 
e03a0 6d 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65  might also.** be
e03b0 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 64 65   necessary to de
e03c0 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52  fine _XOPEN_SOUR
e03d0 43 45 20 74 6f 20 62 65 20 35 30 30 2e 20 20 54  CE to be 500.  T
e03e0 68 69 73 20 76 61 72 69 65 73 20 66 72 6f 6d 0a  his varies from.
e03f0 2a 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f  ** one system to
e0400 20 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e 63 65   another.  Since
e0410 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74   SQLite does not
e0420 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41   define USE_PREA
e0430 44 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72  D.** any any for
e0440 6d 20 62 79 20 64 65 66 61 75 6c 74 2c 20 77 65  m by default, we
e0450 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70   will not attemp
e0460 74 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50  t to define _XOP
e0470 45 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65  EN_SOURCE..** Se
e0480 65 20 74 69 63 6b 65 74 73 20 23 32 37 34 31 20  e tickets #2741 
e0490 61 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a  and #2681..**.**
e04a0 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69   To avoid stompi
e04b0 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c  ng the errno val
e04c0 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 72  ue on a failed r
e04d0 65 61 64 20 74 68 65 20 6c 61 73 74 45 72 72 6e  ead the lastErrn
e04e0 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  o value.** is se
e04f0 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  t before returni
e0500 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
e0510 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e  t seekAndRead(un
e0520 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  ixFile *id, sqli
e0530 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74  te3_int64 offset
e0540 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e  , void *pBuf, in
e0550 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f  t cnt){.  int go
e0560 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73  t;.  i64 newOffs
e0570 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52  et;.  TIMER_STAR
e0580 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55  T;.#if defined(U
e0590 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20  SE_PREAD).  got 
e05a0 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70  = pread(id->h, p
e05b0 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74  Buf, cnt, offset
e05c0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
e05d0 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29  rror( got = -1 )
e05e0 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28  ;.#elif defined(
e05f0 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67  USE_PREAD64).  g
e0600 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d  ot = pread64(id-
e0610 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
e0620 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61  ffset);.  Simula
e0630 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d  teIOError( got =
e0640 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e   -1 );.#else.  n
e0650 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b  ewOffset = lseek
e0660 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20  (id->h, offset, 
e0670 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d  SEEK_SET);.  Sim
e0680 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65  ulateIOError( ne
e0690 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69  wOffset-- );.  i
e06a0 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66  f( newOffset!=of
e06b0 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20  fset ){.    if( 
e06c0 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20  newOffset == -1 
e06d0 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46  ){.      ((unixF
e06e0 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
e06f0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
e0700 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28   }else{.      ((
e0710 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
e0720 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09  astErrno = 0;...
e0730 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
e0740 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20  n -1;.  }.  got 
e0750 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42  = read(id->h, pB
e0760 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66  uf, cnt);.#endif
e0770 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20  .  TIMER_END;.  
e0780 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20  if( got<0 ){.   
e0790 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
e07a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
e07b0 72 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41  rno;.  }.  OSTRA
e07c0 43 45 35 28 22 52 45 41 44 20 20 20 20 25 2d 33  CE5("READ    %-3
e07d0 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75  d %5d %7lld %llu
e07e0 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c  \n", id->h, got,
e07f0 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45   offset, TIMER_E
e0800 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72  LAPSED);.  retur
e0810 6e 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n got;.}../*.** 
e0820 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  Read data from a
e0830 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66   file into a buf
e0840 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  fer.  Return SQL
e0850 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a  ITE_OK if all.**
e0860 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64   bytes were read
e0870 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e   successfully an
e0880 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69  d SQLITE_IOERR i
e0890 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a  f anything goes.
e08a0 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61  ** wrong..*/.sta
e08b0 74 69 63 20 69 6e 74 20 75 6e 69 78 52 65 61 64  tic int unixRead
e08c0 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
e08d0 20 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70   *id, .  void *p
e08e0 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c  Buf, .  int amt,
e08f0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
e0900 20 6f 66 66 73 65 74 0a 29 7b 0a 20 20 69 6e 74   offset.){.  int
e0910 20 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20   got;.  assert( 
e0920 69 64 20 29 3b 0a 20 20 67 6f 74 20 3d 20 73 65  id );.  got = se
e0930 65 6b 41 6e 64 52 65 61 64 28 28 75 6e 69 78 46  ekAndRead((unixF
e0940 69 6c 65 2a 29 69 64 2c 20 6f 66 66 73 65 74 2c  ile*)id, offset,
e0950 20 70 42 75 66 2c 20 61 6d 74 29 3b 0a 20 20 69   pBuf, amt);.  i
e0960 66 28 20 67 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20  f( got==amt ){. 
e0970 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e0980 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  _OK;.  }else if(
e0990 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20 2f 2a   got<0 ){.    /*
e09a0 20 6c 61 73 74 45 72 72 6e 6f 20 73 65 74 20 62   lastErrno set b
e09b0 79 20 73 65 65 6b 41 6e 64 52 65 61 64 20 2a 2f  y seekAndRead */
e09c0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
e09d0 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20  TE_IOERR_READ;. 
e09e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 28 28 75 6e   }else{.    ((un
e09f0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
e0a00 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e  tErrno = 0; /* n
e0a10 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 6f  ot a system erro
e0a20 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e 72 65  r */.    /* Unre
e0a30 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20  ad parts of the 
e0a40 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a  buffer must be z
e0a50 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20  ero-filled */.  
e0a60 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72    memset(&((char
e0a70 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c  *)pBuf)[got], 0,
e0a80 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72   amt-got);.    r
e0a90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
e0aa0 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20  RR_SHORT_READ;. 
e0ab0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b   }.}../*.** Seek
e0ac0 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69   to the offset i
e0ad0 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74 68 65  n id->offset the
e0ae0 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74 65 73  n read cnt bytes
e0af0 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a 20 52   into pBuf..** R
e0b00 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
e0b10 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c   of bytes actual
e0b20 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61 74 65  ly read.  Update
e0b30 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a   the offset..**.
e0b40 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d  ** To avoid stom
e0b50 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76  ping the errno v
e0b60 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64  alue on a failed
e0b70 20 77 72 69 74 65 20 74 68 65 20 6c 61 73 74 45   write the lastE
e0b80 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73  rrno value.** is
e0b90 20 73 65 74 20 62 65 66 6f 72 65 20 72 65 74 75   set before retu
e0ba0 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  rning..*/.static
e0bb0 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72 69 74   int seekAndWrit
e0bc0 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20  e(unixFile *id, 
e0bd0 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f 6e 73  i64 offset, cons
e0be0 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e  t void *pBuf, in
e0bf0 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f  t cnt){.  int go
e0c00 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73  t;.  i64 newOffs
e0c10 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52  et;.  TIMER_STAR
e0c20 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55  T;.#if defined(U
e0c30 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20  SE_PREAD).  got 
e0c40 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68 2c 20  = pwrite(id->h, 
e0c50 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65  pBuf, cnt, offse
e0c60 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65  t);.#elif define
e0c70 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20  d(USE_PREAD64). 
e0c80 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36 34 28   got = pwrite64(
e0c90 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74  id->h, pBuf, cnt
e0ca0 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 73 65  , offset);.#else
e0cb0 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c  .  newOffset = l
e0cc0 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73  seek(id->h, offs
e0cd0 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20  et, SEEK_SET);. 
e0ce0 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d   if( newOffset!=
e0cf0 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66  offset ){.    if
e0d00 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d  ( newOffset == -
e0d10 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69  1 ){.      ((uni
e0d20 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74  xFile*)id)->last
e0d30 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e0d40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
e0d50 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
e0d60 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09  >lastErrno = 0;.
e0d70 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  ...    }.    ret
e0d80 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f  urn -1;.  }.  go
e0d90 74 20 3d 20 77 72 69 74 65 28 69 64 2d 3e 68 2c  t = write(id->h,
e0da0 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e   pBuf, cnt);.#en
e0db0 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b  dif.  TIMER_END;
e0dc0 0a 20 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a  .  if( got<0 ){.
e0dd0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
e0de0 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  id)->lastErrno =
e0df0 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f   errno;.  }..  O
e0e00 53 54 52 41 43 45 35 28 22 57 52 49 54 45 20 20  STRACE5("WRITE  
e0e10 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20   %-3d %5d %7lld 
e0e20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20  %llu\n", id->h, 
e0e30 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d  got, offset, TIM
e0e40 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20 72  ER_ELAPSED);.  r
e0e50 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f  eturn got;.}.../
e0e60 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  *.** Write data 
e0e70 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e  from a buffer in
e0e80 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75  to a file.  Retu
e0e90 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
e0ea0 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f  success.** or so
e0eb0 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63  me other error c
e0ec0 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a  ode on failure..
e0ed0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
e0ee0 69 78 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  ixWrite(.  sqlit
e0ef0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20  e3_file *id, .  
e0f00 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66  const void *pBuf
e0f10 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20  , .  int amt,.  
e0f20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66  sqlite3_int64 of
e0f30 66 73 65 74 20 0a 29 7b 0a 20 20 69 6e 74 20 77  fset .){.  int w
e0f40 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65  rote = 0;.  asse
e0f50 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65  rt( id );.  asse
e0f60 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 23 69  rt( amt>0 );..#i
e0f70 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f  fndef NDEBUG.  /
e0f80 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e  * If we are doin
e0f90 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65  g a normal write
e0fa0 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66   to a database f
e0fb0 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20  ile (as opposed 
e0fc0 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20  to.  ** doing a 
e0fd0 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c  hot-journal roll
e0fe0 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20  back or a write 
e0ff0 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68  to some file oth
e1000 65 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e  er than a.  ** n
e1010 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66  ormal database f
e1020 69 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64  ile) then record
e1030 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74   the fact that t
e1040 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  he database.  **
e1050 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49   has changed.  I
e1060 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  f the transactio
e1070 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64  n counter is mod
e1080 69 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68  ified, record th
e1090 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f  at.  ** fact too
e10a0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 28 75  ..  */.  if( ((u
e10b0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 6e  nixFile*)id)->in
e10c0 4e 6f 72 6d 61 6c 57 72 69 74 65 20 29 7b 0a 20  NormalWrite ){. 
e10d0 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69     unixFile *pFi
e10e0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
e10f0 69 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64  id;.    pFile->d
e1100 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a  bUpdate = 1;  /*
e1110 20 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61   The database ha
e1120 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  s been modified 
e1130 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65  */.    if( offse
e1140 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b  t<=24 && offset+
e1150 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20  amt>=27 ){.     
e1160 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d   char oldCntr[4]
e1170 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65  ;.      Simulate
e1180 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29  IOErrorBenign(1)
e1190 3b 0a 20 20 20 20 20 20 73 65 65 6b 41 6e 64 52  ;.      seekAndR
e11a0 65 61 64 28 70 46 69 6c 65 2c 20 32 34 2c 20 6f  ead(pFile, 24, o
e11b0 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 20 20 20  ldCntr, 4);.    
e11c0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
e11d0 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20  rBenign(0);.    
e11e0 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 6f 6c 64    if( memcmp(old
e11f0 43 6e 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70  Cntr, &((char*)p
e1200 42 75 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c  Buf)[24-offset],
e1210 20 34 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20   4)!=0 ){.      
e1220 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e    pFile->transCn
e1230 74 72 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20  trChng = 1;  /* 
e1240 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  The transaction 
e1250 63 6f 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e  counter has chan
e1260 67 65 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ged */.      }. 
e1270 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
e1280 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20  .  while( amt>0 
e1290 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b  && (wrote = seek
e12a0 41 6e 64 57 72 69 74 65 28 28 75 6e 69 78 46 69  AndWrite((unixFi
e12b0 6c 65 2a 29 69 64 2c 20 6f 66 66 73 65 74 2c 20  le*)id, offset, 
e12c0 70 42 75 66 2c 20 61 6d 74 29 29 3e 30 20 29 7b  pBuf, amt))>0 ){
e12d0 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74  .    amt -= wrot
e12e0 65 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 2b 3d  e;.    offset +=
e12f0 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66   wrote;.    pBuf
e1300 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 66   = &((char*)pBuf
e1310 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20  )[wrote];.  }.  
e1320 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
e1330 28 20 77 72 6f 74 65 3d 28 2d 31 29 2c 20 61 6d  ( wrote=(-1), am
e1340 74 3d 31 20 29 29 3b 0a 20 20 53 69 6d 75 6c 61  t=1 ));.  Simula
e1350 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
e1360 28 20 77 72 6f 74 65 3d 30 2c 20 61 6d 74 3d 31  ( wrote=0, amt=1
e1370 20 29 29 3b 0a 20 20 69 66 28 20 61 6d 74 3e 30   ));.  if( amt>0
e1380 20 29 7b 0a 20 20 20 20 69 66 28 20 77 72 6f 74   ){.    if( wrot
e1390 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  e<0 ){.      /* 
e13a0 6c 61 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79  lastErrno set by
e13b0 20 73 65 65 6b 41 6e 64 57 72 69 74 65 20 2a 2f   seekAndWrite */
e13c0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
e13d0 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45  LITE_IOERR_WRITE
e13e0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
e13f0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
e1400 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  d)->lastErrno = 
e1410 30 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74  0; /* not a syst
e1420 65 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  em error */.    
e1430 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e1440 46 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  FULL;.    }.  }.
e1450 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e1460 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  OK;.}..#ifdef SQ
e1470 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20  LITE_TEST./*.** 
e1480 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72  Count the number
e1490 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e   of fullsyncs an
e14a0 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20  d normal syncs. 
e14b0 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f   This is used to
e14c0 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79   test.** that sy
e14d0 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63  ncs and fullsync
e14e0 73 20 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20  s are occurring 
e14f0 61 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d  at the right tim
e1500 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  es..*/.SQLITE_AP
e1510 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79  I int sqlite3_sy
e1520 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51  nc_count = 0;.SQ
e1530 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
e1540 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
e1550 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
e1560 0a 2f 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 66  ./*.** Use the f
e1570 64 61 74 61 73 79 6e 63 28 29 20 41 50 49 20 6f  datasync() API o
e1580 6e 6c 79 20 69 66 20 74 68 65 20 48 41 56 45 5f  nly if the HAVE_
e1590 46 44 41 54 41 53 59 4e 43 20 6d 61 63 72 6f 20  FDATASYNC macro 
e15a0 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 4f  is defined..** O
e15b0 74 68 65 72 77 69 73 65 20 75 73 65 20 66 73 79  therwise use fsy
e15c0 6e 63 28 29 20 69 6e 20 69 74 73 20 70 6c 61 63  nc() in its plac
e15d0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 48 41  e..*/.#ifndef HA
e15e0 56 45 5f 46 44 41 54 41 53 59 4e 43 0a 23 20 64  VE_FDATASYNC.# d
e15f0 65 66 69 6e 65 20 66 64 61 74 61 73 79 6e 63 20  efine fdatasync 
e1600 66 73 79 6e 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a  fsync.#endif../*
e1610 0a 2a 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f  .** Define HAVE_
e1620 46 55 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f  FULLFSYNC to 0 o
e1630 72 20 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  r 1 depending on
e1640 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a   whether or not.
e1650 2a 2a 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59  ** the F_FULLFSY
e1660 4e 43 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69  NC macro is defi
e1670 6e 65 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e  ned.  F_FULLFSYN
e1680 43 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a  C is currently.*
e1690 2a 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  * only available
e16a0 20 6f 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20 42   on Mac OS X.  B
e16b0 75 74 20 74 68 61 74 20 63 6f 75 6c 64 20 63 68  ut that could ch
e16c0 61 6e 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ange..*/.#ifdef 
e16d0 46 5f 46 55 4c 4c 46 53 59 4e 43 0a 23 20 64 65  F_FULLFSYNC.# de
e16e0 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53  fine HAVE_FULLFS
e16f0 59 4e 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65  YNC 1.#else.# de
e1700 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53  fine HAVE_FULLFS
e1710 59 4e 43 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f  YNC 0.#endif.../
e1720 2a 0a 2a 2a 20 54 68 65 20 66 73 79 6e 63 28 29  *.** The fsync()
e1730 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 64 6f 65   system call doe
e1740 73 20 6e 6f 74 20 77 6f 72 6b 20 61 73 20 61 64  s not work as ad
e1750 76 65 72 74 69 73 65 64 20 6f 6e 20 6d 61 6e 79  vertised on many
e1760 0a 2a 2a 20 75 6e 69 78 20 73 79 73 74 65 6d 73  .** unix systems
e1770 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
e1780 20 70 72 6f 63 65 64 75 72 65 20 69 73 20 61 6e   procedure is an
e1790 20 61 74 74 65 6d 70 74 20 74 6f 20 6d 61 6b 65   attempt to make
e17a0 0a 2a 2a 20 69 74 20 77 6f 72 6b 20 62 65 74 74  .** it work bett
e17b0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  er..**.** The SQ
e17c0 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63  LITE_NO_SYNC mac
e17d0 72 6f 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20  ro disables all 
e17e0 66 73 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20  fsync()s.  This 
e17f0 69 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72  is useful.** for
e1800 20 74 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65   testing when we
e1810 20 77 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72   want to run thr
e1820 6f 75 67 68 20 74 68 65 20 74 65 73 74 20 73 75  ough the test su
e1830 69 74 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20  ite quickly..** 
e1840 59 6f 75 20 61 72 65 20 73 74 72 6f 6e 67 6c 79  You are strongly
e1850 20 61 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20 74   advised *not* t
e1860 6f 20 64 65 70 6c 6f 79 20 77 69 74 68 20 53 51  o deploy with SQ
e1870 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20  LITE_NO_SYNC.** 
e1880 65 6e 61 62 6c 65 64 2c 20 68 6f 77 65 76 65 72  enabled, however
e1890 2c 20 73 69 6e 63 65 20 77 69 74 68 20 53 51 4c  , since with SQL
e18a0 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e 61 62  ITE_NO_SYNC enab
e18b0 6c 65 64 2c 20 61 6e 20 4f 53 20 63 72 61 73 68  led, an OS crash
e18c0 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69  .** or power fai
e18d0 6c 75 72 65 20 77 69 6c 6c 20 6c 69 6b 65 6c 79  lure will likely
e18e0 20 63 6f 72 72 75 70 74 20 74 68 65 20 64 61 74   corrupt the dat
e18f0 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  abase file..*/.s
e1900 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66  tatic int full_f
e1910 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74  sync(int fd, int
e1920 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64   fullSync, int d
e1930 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20  ataOnly){.  int 
e1940 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f  rc;..  /* The fo
e1950 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65  llowing "ifdef/e
e1960 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b  lif/else/" block
e1970 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74   has the same st
e1980 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20  ructure as.  ** 
e1990 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49  the one below. I
e19a0 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20  t is replicated 
e19b0 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61  here solely to a
e19c0 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20  void cluttering 
e19d0 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61  .  ** up the rea
e19e0 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20  l code with the 
e19f0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e1a00 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a  () macros..  */.
e1a10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f  #ifdef SQLITE_NO
e1a20 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50  _SYNC.  UNUSED_P
e1a30 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20  ARAMETER(fd);.  
e1a40 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e1a50 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e  (fullSync);.  UN
e1a60 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64  USED_PARAMETER(d
e1a70 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20  ataOnly);.#elif 
e1a80 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20  HAVE_FULLFSYNC. 
e1a90 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e1aa0 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c  R(dataOnly);.#el
e1ab0 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
e1ac0 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b  METER(fullSync);
e1ad0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65  .#endif..  /* Re
e1ae0 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20  cord the number 
e1af0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65  of times that we
e1b00 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79   do a normal fsy
e1b10 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46  nc() and .  ** F
e1b20 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69  ULLSYNC.  This i
e1b30 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65  s used during te
e1b40 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20  sting to verify 
e1b50 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64  that this proced
e1b60 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61  ure.  ** gets ca
e1b70 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f  lled with the co
e1b80 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e  rrect arguments.
e1b90 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
e1ba0 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66  ITE_TEST.  if( f
e1bb0 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65  ullSync ) sqlite
e1bc0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
e1bd0 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79  ++;.  sqlite3_sy
e1be0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64  nc_count++;.#end
e1bf0 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  if..  /* If we c
e1c00 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
e1c10 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20   SQLITE_NO_SYNC 
e1c20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69  flag, then synci
e1c30 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d  ng is a.  ** no-
e1c40 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  op.  */.#ifdef S
e1c50 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20  QLITE_NO_SYNC.  
e1c60 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
e1c70 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46  #elif HAVE_FULLF
e1c80 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53  SYNC.  if( fullS
e1c90 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ync ){.    rc = 
e1ca0 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c  fcntl(fd, F_FULL
e1cb0 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c  FSYNC, 0);.  }el
e1cc0 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a  se{.    rc = 1;.
e1cd0 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20    }.  /* If the 
e1ce0 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64  FULLFSYNC failed
e1cf0 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61  , fall back to a
e1d00 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79  ttempting an fsy
e1d10 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68  nc()..  ** It sh
e1d20 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69  ouldn't be possi
e1d30 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e  ble for fullfsyn
e1d40 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65  c to fail on the
e1d50 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c   local .  ** fil
e1d60 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58  e system (on OSX
e1d70 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e  ), so failure in
e1d80 64 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c  dicates that FUL
e1d90 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27  LFSYNC.  ** isn'
e1da0 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20  t supported for 
e1db0 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d  this file system
e1dc0 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e  . So, attempt an
e1dd0 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64   fsync .  ** and
e1de0 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72   (for now) ignor
e1df0 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f  e the overhead o
e1e00 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20  f a superfluous 
e1e10 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20  fcntl call.  .  
e1e20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65  ** It'd be bette
e1e30 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c  r to detect full
e1e40 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e  fsync support on
e1e50 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20  ce and avoid .  
e1e60 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c  ** the fcntl cal
e1e70 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e  l every time syn
e1e80 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a  c is called..  *
e1e90 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 63 20  /.  if( rc ) rc 
e1ea0 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65  = fsync(fd);..#e
e1eb0 6c 73 65 20 0a 20 20 69 66 28 20 64 61 74 61 4f  lse .  if( dataO
e1ec0 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  nly ){.    rc = 
e1ed0 66 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23  fdatasync(fd);.#
e1ee0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
e1ef0 20 20 69 66 28 20 72 63 3d 3d 2d 31 20 26 26 20    if( rc==-1 && 
e1f00 65 72 72 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29  errno==ENOTSUP )
e1f10 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 73 79  {.      rc = fsy
e1f20 6e 63 28 66 64 29 3b 0a 20 20 20 20 7d 0a 23 65  nc(fd);.    }.#e
e1f30 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ndif.  }else{.  
e1f40 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29    rc = fsync(fd)
e1f50 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
e1f60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f  ifdef SQLITE_NO_
e1f70 53 59 4e 43 20 65 6c 69 66 20 48 41 56 45 5f 46  SYNC elif HAVE_F
e1f80 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a 0a 20 20 69  ULLFSYNC */..  i
e1f90 66 28 20 4f 53 5f 56 58 57 4f 52 4b 53 20 26 26  f( OS_VXWORKS &&
e1fa0 20 72 63 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20   rc!= -1 ){.    
e1fb0 72 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  rc = 0;.  }.  re
e1fc0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
e1fd0 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  * Make sure all 
e1fe0 77 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74  writes to a part
e1ff0 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20  icular file are 
e2000 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73  committed to dis
e2010 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 61 74 61  k..**.** If data
e2020 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e 20 62 6f 74  Only==0 then bot
e2030 68 20 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c  h the file itsel
e2040 66 20 61 6e 64 20 69 74 73 20 6d 65 74 61 64 61  f and its metada
e2050 74 61 20 28 66 69 6c 65 0a 2a 2a 20 73 69 7a 65  ta (file.** size
e2060 2c 20 61 63 63 65 73 73 20 74 69 6d 65 2c 20 65  , access time, e
e2070 74 63 29 20 61 72 65 20 73 79 6e 63 65 64 2e 20  tc) are synced. 
e2080 20 49 66 20 64 61 74 61 4f 6e 6c 79 21 3d 30 20   If dataOnly!=0 
e2090 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a  then only the.**
e20a0 20 66 69 6c 65 20 64 61 74 61 20 69 73 20 73 79   file data is sy
e20b0 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 64 65  nced..**.** Unde
e20c0 72 20 55 6e 69 78 2c 20 61 6c 73 6f 20 6d 61 6b  r Unix, also mak
e20d0 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
e20e0 64 69 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20  directory entry 
e20f0 66 6f 72 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20  for the file.** 
e2100 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64  has been created
e2110 20 62 79 20 66 73 79 6e 63 2d 69 6e 67 20 74 68   by fsync-ing th
e2120 65 20 64 69 72 65 63 74 6f 72 79 20 74 68 61 74  e directory that
e2130 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 69   contains the fi
e2140 6c 65 2e 0a 2a 2a 20 49 66 20 77 65 20 64 6f 20  le..** If we do 
e2150 6e 6f 74 20 64 6f 20 74 68 69 73 20 61 6e 64 20  not do this and 
e2160 77 65 20 65 6e 63 6f 75 6e 74 65 72 20 61 20 70  we encounter a p
e2170 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68  ower failure, th
e2180 65 20 64 69 72 65 63 74 6f 72 79 0a 2a 2a 20 65  e directory.** e
e2190 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a 6f 75  ntry for the jou
e21a0 72 6e 61 6c 20 6d 69 67 68 74 20 6e 6f 74 20 65  rnal might not e
e21b0 78 69 73 74 20 61 66 74 65 72 20 77 65 20 72 65  xist after we re
e21c0 62 6f 6f 74 2e 20 20 54 68 65 20 6e 65 78 74 0a  boot.  The next.
e21d0 2a 2a 20 53 51 4c 69 74 65 20 74 6f 20 61 63 63  ** SQLite to acc
e21e0 65 73 73 20 74 68 65 20 66 69 6c 65 20 77 69 6c  ess the file wil
e21f0 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74 68 61 74 20  l not know that 
e2200 74 68 65 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73  the journal exis
e2210 74 73 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74  ts (because.** t
e2220 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74  he directory ent
e2230 72 79 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e  ry for the journ
e2240 61 6c 20 77 61 73 20 6e 65 76 65 72 20 63 72 65  al was never cre
e2250 61 74 65 64 29 20 61 6e 64 20 74 68 65 20 74 72  ated) and the tr
e2260 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c  ansaction.** wil
e2270 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62 61 63 6b 20  l not roll back 
e2280 2d 20 70 6f 73 73 69 62 6c 79 20 6c 65 61 64 69  - possibly leadi
e2290 6e 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 63  ng to database c
e22a0 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  orruption..*/.st
e22b0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 79 6e  atic int unixSyn
e22c0 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  c(sqlite3_file *
e22d0 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  id, int flags){.
e22e0 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 69 78    int rc;.  unix
e22f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
e2300 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20  nixFile*)id;..  
e2310 69 6e 74 20 69 73 44 61 74 61 4f 6e 6c 79 20 3d  int isDataOnly =
e2320 20 28 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 53   (flags&SQLITE_S
e2330 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 29 3b 0a 20  YNC_DATAONLY);. 
e2340 20 69 6e 74 20 69 73 46 75 6c 6c 73 79 6e 63 20   int isFullsync 
e2350 3d 20 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d  = (flags&0x0F)==
e2360 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c  SQLITE_SYNC_FULL
e2370 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  ;..  /* Check th
e2380 61 74 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45  at one of SQLITE
e2390 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20  _SYNC_NORMAL or 
e23a0 46 55 4c 4c 20 77 61 73 20 70 61 73 73 65 64 20  FULL was passed 
e23b0 2a 2f 0a 20 20 61 73 73 65 72 74 28 28 66 6c 61  */.  assert((fla
e23c0 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45  gs&0x0F)==SQLITE
e23d0 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 0a 20 20 20  _SYNC_NORMAL.   
e23e0 20 20 20 7c 7c 20 28 66 6c 61 67 73 26 30 78 30     || (flags&0x0
e23f0 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f  F)==SQLITE_SYNC_
e2400 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20  FULL.  );..  /* 
e2410 55 6e 69 78 20 63 61 6e 6e 6f 74 2c 20 62 75 74  Unix cannot, but
e2420 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 6d 61   some systems ma
e2430 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  y return SQLITE_
e2440 46 55 4c 4c 20 66 72 6f 6d 20 68 65 72 65 2e 20  FULL from here. 
e2450 54 68 69 73 0a 20 20 2a 2a 20 6c 69 6e 65 20 69  This.  ** line i
e2460 73 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 64  s to test that d
e2470 6f 69 6e 67 20 73 6f 20 64 6f 65 73 20 6e 6f 74  oing so does not
e2480 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f 62 6c   cause any probl
e2490 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75  ems..  */.  Simu
e24a0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
e24b0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
e24c0 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20 61 73 73 65  _FULL );..  asse
e24d0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f  rt( pFile );.  O
e24e0 53 54 52 41 43 45 32 28 22 53 59 4e 43 20 20 20  STRACE2("SYNC   
e24f0 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d   %-3d\n", pFile-
e2500 3e 68 29 3b 0a 20 20 72 63 20 3d 20 66 75 6c 6c  >h);.  rc = full
e2510 5f 66 73 79 6e 63 28 70 46 69 6c 65 2d 3e 68 2c  _fsync(pFile->h,
e2520 20 69 73 46 75 6c 6c 73 79 6e 63 2c 20 69 73 44   isFullsync, isD
e2530 61 74 61 4f 6e 6c 79 29 3b 0a 20 20 53 69 6d 75  ataOnly);.  Simu
e2540 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d  lateIOError( rc=
e2550 31 20 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  1 );.  if( rc ){
e2560 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
e2570 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e2580 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e2590 5f 49 4f 45 52 52 5f 46 53 59 4e 43 3b 0a 20 20  _IOERR_FSYNC;.  
e25a0 7d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 64  }.  if( pFile->d
e25b0 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 69  irfd>=0 ){.    i
e25c0 6e 74 20 65 72 72 3b 0a 20 20 20 20 4f 53 54 52  nt err;.    OSTR
e25d0 41 43 45 34 28 22 44 49 52 53 59 4e 43 20 25 2d  ACE4("DIRSYNC %-
e25e0 33 64 20 28 68 61 76 65 5f 66 75 6c 6c 66 73 79  3d (have_fullfsy
e25f0 6e 63 3d 25 64 20 66 75 6c 6c 73 79 6e 63 3d 25  nc=%d fullsync=%
e2600 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 64 69  d)\n", pFile->di
e2610 72 66 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rfd,.           
e2620 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 2c   HAVE_FULLFSYNC,
e2630 20 69 73 46 75 6c 6c 73 79 6e 63 29 3b 0a 23 69   isFullsync);.#i
e2640 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53  fndef SQLITE_DIS
e2650 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a 20 20 20  ABLE_DIRSYNC.   
e2660 20 2f 2a 20 54 68 65 20 64 69 72 65 63 74 6f 72   /* The director
e2670 79 20 73 79 6e 63 20 69 73 20 6f 6e 6c 79 20 61  y sync is only a
e2680 74 74 65 6d 70 74 65 64 20 69 66 20 66 75 6c 6c  ttempted if full
e2690 5f 66 73 79 6e 63 20 69 73 0a 20 20 20 20 2a 2a  _fsync is.    **
e26a0 20 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 75   turned off or u
e26b0 6e 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20  navailable.  If 
e26c0 61 20 66 75 6c 6c 5f 66 73 79 6e 63 20 6f 63 63  a full_fsync occ
e26d0 75 72 72 65 64 20 61 62 6f 76 65 2c 0a 20 20 20  urred above,.   
e26e0 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 64 69 72   ** then the dir
e26f0 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 73  ectory sync is s
e2700 75 70 65 72 66 6c 75 6f 75 73 2e 0a 20 20 20 20  uperfluous..    
e2710 2a 2f 0a 20 20 20 20 69 66 28 20 28 21 48 41 56  */.    if( (!HAV
e2720 45 5f 46 55 4c 4c 46 53 59 4e 43 20 7c 7c 20 21  E_FULLFSYNC || !
e2730 69 73 46 75 6c 6c 73 79 6e 63 29 20 26 26 20 66  isFullsync) && f
e2740 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65 2d  ull_fsync(pFile-
e2750 3e 64 69 72 66 64 2c 30 2c 30 29 20 29 7b 0a 20  >dirfd,0,0) ){. 
e2760 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20        /*.       
e2770 2a 2a 20 57 65 20 68 61 76 65 20 72 65 63 65 69  ** We have recei
e2780 76 65 64 20 6d 75 6c 74 69 70 6c 65 20 72 65 70  ved multiple rep
e2790 6f 72 74 73 20 6f 66 20 66 73 79 6e 63 28 29 20  orts of fsync() 
e27a0 72 65 74 75 72 6e 69 6e 67 0a 20 20 20 20 20 20  returning.      
e27b0 20 2a 2a 20 65 72 72 6f 72 73 20 77 68 65 6e 20   ** errors when 
e27c0 61 70 70 6c 69 65 64 20 74 6f 20 64 69 72 65 63  applied to direc
e27d0 74 6f 72 69 65 73 20 6f 6e 20 63 65 72 74 61 69  tories on certai
e27e0 6e 20 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 0a  n file systems..
e27f0 20 20 20 20 20 20 20 2a 2a 20 41 20 66 61 69 6c         ** A fail
e2800 65 64 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e  ed directory syn
e2810 63 20 69 73 20 6e 6f 74 20 61 20 62 69 67 20 64  c is not a big d
e2820 65 61 6c 2e 20 20 53 6f 20 69 74 20 73 65 65 6d  eal.  So it seem
e2830 73 0a 20 20 20 20 20 20 20 2a 2a 20 62 65 74 74  s.       ** bett
e2840 65 72 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65  er to ignore the
e2850 20 65 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20   error.  Ticket 
e2860 23 31 36 35 37 0a 20 20 20 20 20 20 20 2a 2f 0a  #1657.       */.
e2870 20 20 20 20 20 20 20 2f 2a 20 70 46 69 6c 65 2d         /* pFile-
e2880 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
e2890 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a  no; */.       /*
e28a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
e28b0 4f 45 52 52 3b 20 2a 2f 0a 20 20 20 20 7d 0a 23  OERR; */.    }.#
e28c0 65 6e 64 69 66 0a 20 20 20 20 65 72 72 20 3d 20  endif.    err = 
e28d0 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72  close(pFile->dir
e28e0 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79 20 6e 65 65  fd); /* Only nee
e28f0 64 20 74 6f 20 73 79 6e 63 20 6f 6e 63 65 2c 20  d to sync once, 
e2900 73 6f 20 63 6c 6f 73 65 20 74 68 65 20 2a 2f 0a  so close the */.
e2910 20 20 20 20 69 66 28 20 65 72 72 3d 3d 30 20 29      if( err==0 )
e2920 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  {              /
e2930 2a 20 64 69 72 65 63 74 6f 72 79 20 77 68 65 6e  * directory when
e2940 20 77 65 20 61 72 65 20 64 6f 6e 65 20 2a 2f 0a   we are done */.
e2950 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 69 72        pFile->dir
e2960 66 64 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c  fd = -1;.    }el
e2970 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  se{.      pFile-
e2980 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
e2990 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  no;.      rc = S
e29a0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f  QLITE_IOERR_DIR_
e29b0 43 4c 4f 53 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  CLOSE;.    }.  }
e29c0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
e29d0 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
e29e0 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20  an open file to 
e29f0 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65  a specified size
e2a00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
e2a10 6e 69 78 54 72 75 6e 63 61 74 65 28 73 71 6c 69  nixTruncate(sqli
e2a20 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36  te3_file *id, i6
e2a30 34 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20  4 nByte){.  int 
e2a40 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  rc;.  assert( id
e2a50 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   );.  SimulateIO
e2a60 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
e2a70 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
e2a80 41 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 66 74  ATE );.  rc = ft
e2a90 72 75 6e 63 61 74 65 28 28 28 75 6e 69 78 46 69  runcate(((unixFi
e2aa0 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 6f 66 66  le*)id)->h, (off
e2ab0 5f 74 29 6e 42 79 74 65 29 3b 0a 20 20 69 66 28  _t)nByte);.  if(
e2ac0 20 72 63 20 29 7b 0a 20 20 20 20 28 28 75 6e 69   rc ){.    ((uni
e2ad0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74  xFile*)id)->last
e2ae0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e2af0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e2b00 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b  _IOERR_TRUNCATE;
e2b10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
e2b20 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e2b30 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74    }.}../*.** Det
e2b40 65 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65  ermine the curre
e2b50 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c  nt size of a fil
e2b60 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74  e in bytes.*/.st
e2b70 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c  atic int unixFil
e2b80 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  eSize(sqlite3_fi
e2b90 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69  le *id, i64 *pSi
e2ba0 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ze){.  int rc;. 
e2bb0 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66   struct stat buf
e2bc0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29  ;.  assert( id )
e2bd0 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 28  ;.  rc = fstat((
e2be0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
e2bf0 68 2c 20 26 62 75 66 29 3b 0a 20 20 53 69 6d 75  h, &buf);.  Simu
e2c00 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d  lateIOError( rc=
e2c10 31 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30  1 );.  if( rc!=0
e2c20 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69   ){.    ((unixFi
e2c30 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72  le*)id)->lastErr
e2c40 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
e2c50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
e2c60 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20  ERR_FSTAT;.  }. 
e2c70 20 2a 70 53 69 7a 65 20 3d 20 62 75 66 2e 73 74   *pSize = buf.st
e2c80 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 57 68 65  _size;..  /* Whe
e2c90 6e 20 6f 70 65 6e 69 6e 67 20 61 20 7a 65 72 6f  n opening a zero
e2ca0 2d 73 69 7a 65 20 64 61 74 61 62 61 73 65 2c 20  -size database, 
e2cb0 74 68 65 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f  the findLockInfo
e2cc0 28 29 20 70 72 6f 63 65 64 75 72 65 0a 20 20 2a  () procedure.  *
e2cd0 2a 20 77 72 69 74 65 73 20 61 20 73 69 6e 67 6c  * writes a singl
e2ce0 65 20 62 79 74 65 20 69 6e 74 6f 20 74 68 61 74  e byte into that
e2cf0 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74   file in order t
e2d00 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 61 20  o work around a 
e2d10 62 75 67 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  bug.  ** in the 
e2d20 4f 53 2d 58 20 6d 73 64 6f 73 20 66 69 6c 65 73  OS-X msdos files
e2d30 79 73 74 65 6d 2e 20 20 49 6e 20 6f 72 64 65 72  ystem.  In order
e2d40 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65   to avoid proble
e2d50 6d 73 20 77 69 74 68 20 75 70 70 65 72 0a 20 20  ms with upper.  
e2d60 2a 2a 20 6c 61 79 65 72 73 2c 20 77 65 20 6e 65  ** layers, we ne
e2d70 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68 69  ed to report thi
e2d80 73 20 66 69 6c 65 20 73 69 7a 65 20 61 73 20 7a  s file size as z
e2d90 65 72 6f 20 65 76 65 6e 20 74 68 6f 75 67 68 20  ero even though 
e2da0 69 74 20 69 73 0a 20 20 2a 2a 20 72 65 61 6c 6c  it is.  ** reall
e2db0 79 20 31 2e 20 20 20 54 69 63 6b 65 74 20 23 33  y 1.   Ticket #3
e2dc0 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  260..  */.  if( 
e2dd0 2a 70 53 69 7a 65 3d 3d 31 20 29 20 2a 70 53 69  *pSize==1 ) *pSi
e2de0 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 72 65 74 75  ze = 0;...  retu
e2df0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
e2e00 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
e2e10 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e2e20 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   && defined(__AP
e2e30 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 48 61 6e  PLE__)./*.** Han
e2e40 64 6c 65 72 20 66 6f 72 20 70 72 6f 78 79 2d 6c  dler for proxy-l
e2e50 6f 63 6b 69 6e 67 20 66 69 6c 65 2d 63 6f 6e 74  ocking file-cont
e2e60 72 6f 6c 20 76 65 72 62 73 2e 20 20 44 65 66 69  rol verbs.  Defi
e2e70 6e 65 64 20 62 65 6c 6f 77 20 69 6e 20 74 68 65  ned below in the
e2e80 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63  .** proxying loc
e2e90 6b 69 6e 67 20 64 69 76 69 73 69 6f 6e 2e 0a 2a  king division..*
e2ea0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
e2eb0 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71  xyFileControl(sq
e2ec0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c  lite3_file*,int,
e2ed0 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  void*);.#endif..
e2ee0 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69  ./*.** Informati
e2ef0 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 6f  on and control o
e2f00 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 68  f an open file h
e2f10 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  andle..*/.static
e2f20 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 43 6f 6e   int unixFileCon
e2f30 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c  trol(sqlite3_fil
e2f40 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76  e *id, int op, v
e2f50 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77  oid *pArg){.  sw
e2f60 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20  itch( op ){.    
e2f70 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  case SQLITE_FCNT
e2f80 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20  L_LOCKSTATE: {. 
e2f90 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67       *(int*)pArg
e2fa0 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69   = ((unixFile*)i
e2fb0 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  d)->locktype;.  
e2fc0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e2fd0 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
e2fe0 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54  case SQLITE_LAST
e2ff0 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20  _ERRNO: {.      
e3000 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28  *(int*)pArg = ((
e3010 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
e3020 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  astErrno;.      
e3030 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e3040 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
e3050 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 54 68  NDEBUG.    /* Th
e3060 65 20 70 61 67 65 72 20 63 61 6c 6c 73 20 74 68  e pager calls th
e3070 69 73 20 6d 65 74 68 6f 64 20 74 6f 20 73 69 67  is method to sig
e3080 6e 61 6c 20 74 68 61 74 20 69 74 20 68 61 73 20  nal that it has 
e3090 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 20 72 6f  done.    ** a ro
e30a0 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68 61 74 20  llback and that 
e30b0 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
e30c0 74 68 65 72 65 66 6f 72 65 20 75 6e 63 68 61 6e  therefore unchan
e30d0 67 65 64 20 61 6e 64 0a 20 20 20 20 2a 2a 20 69  ged and.    ** i
e30e0 74 20 68 65 6e 63 65 20 69 74 20 69 73 20 4f 4b  t hence it is OK
e30f0 20 66 6f 72 20 74 68 65 20 74 72 61 6e 73 61 63   for the transac
e3100 74 69 6f 6e 20 63 68 61 6e 67 65 20 63 6f 75 6e  tion change coun
e3110 74 65 72 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a  ter to be.    **
e3120 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 20 20   unchanged..    
e3130 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
e3140 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48  TE_FCNTL_DB_UNCH
e3150 41 4e 47 45 44 3a 20 7b 0a 20 20 20 20 20 20 28  ANGED: {.      (
e3160 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
e3170 64 62 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20  dbUpdate = 0;.  
e3180 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e3190 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  E_OK;.    }.#end
e31a0 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  if.#if SQLITE_EN
e31b0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
e31c0 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  LE && defined(__
e31d0 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 63 61 73  APPLE__).    cas
e31e0 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43  e SQLITE_SET_LOC
e31f0 4b 50 52 4f 58 59 46 49 4c 45 3a 0a 20 20 20 20  KPROXYFILE:.    
e3200 63 61 73 65 20 53 51 4c 49 54 45 5f 47 45 54 5f  case SQLITE_GET_
e3210 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b  LOCKPROXYFILE: {
e3220 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 72  .      return pr
e3230 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69  oxyFileControl(i
e3240 64 2c 6f 70 2c 70 41 72 67 29 3b 0a 20 20 20 20  d,op,pArg);.    
e3250 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
e3260 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
e3270 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e  G_STYLE && defin
e3280 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 2a 2f  ed(__APPLE__) */
e3290 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
e32a0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f  LITE_ERROR;.}../
e32b0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
e32c0 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62  sector size in b
e32d0 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65  ytes of the unde
e32e0 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76  rlying block dev
e32f0 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73  ice for.** the s
e3300 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54  pecified file. T
e3310 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c  his is almost al
e3320 77 61 79 73 20 35 31 32 20 62 79 74 65 73 2c 20  ways 512 bytes, 
e3330 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61  but may be.** la
e3340 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65  rger for some de
e3350 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  vices..**.** SQL
e3360 69 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73  ite code assumes
e3370 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   this function c
e3380 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61  annot fail. It a
e3390 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74  lso assumes that
e33a0 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73  .** if two files
e33b0 20 61 72 65 20 63 72 65 61 74 65 64 20 69 6e 20   are created in 
e33c0 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79  the same file-sy
e33d0 73 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28  stem directory (
e33e0 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61  i.e..** a databa
e33f0 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e  se and its journ
e3400 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 68  al file) that th
e3410 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69  e sector size wi
e3420 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d  ll be the.** sam
e3430 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73  e for both..*/.s
e3440 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 65  tatic int unixSe
e3450 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33  ctorSize(sqlite3
e3460 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b  _file *NotUsed){
e3470 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
e3480 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
e3490 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45  return SQLITE_DE
e34a0 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a  FAULT_SECTOR_SIZ
e34b0 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  E;.}../*.** Retu
e34c0 72 6e 20 74 68 65 20 64 65 76 69 63 65 20 63 68  rn the device ch
e34d0 61 72 61 63 74 65 72 69 73 74 69 63 73 20 66 6f  aracteristics fo
e34e0 72 20 74 68 65 20 66 69 6c 65 2e 20 54 68 69 73  r the file. This
e34f0 20 69 73 20 61 6c 77 61 79 73 20 30 20 66 6f 72   is always 0 for
e3500 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 61 74 69 63   unix..*/.static
e3510 20 69 6e 74 20 75 6e 69 78 44 65 76 69 63 65 43   int unixDeviceC
e3520 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73  haracteristics(s
e3530 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74  qlite3_file *Not
e3540 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
e3550 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e3560 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  d);.  return 0;.
e3570 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 65 6e  }../*.** Here en
e3580 64 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ds the implement
e3590 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 71 6c  ation of all sql
e35a0 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64  ite3_file method
e35b0 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s..**.**********
e35c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
e35d0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 4d 65   sqlite3_file Me
e35e0 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  thods **********
e35f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3600 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
e3610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3650 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ****/../*.** Thi
e3660 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61  s division conta
e3670 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20  ins definitions 
e3680 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  of sqlite3_io_me
e3690 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 74 68  thods objects th
e36a0 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  at.** implement 
e36b0 76 61 72 69 6f 75 73 20 66 69 6c 65 20 6c 6f 63  various file loc
e36c0 6b 69 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e  king strategies.
e36d0 20 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69    It also contai
e36e0 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a  ns definitions.*
e36f0 2a 20 6f 66 20 22 66 69 6e 64 65 72 22 20 66 75  * of "finder" fu
e3700 6e 63 74 69 6f 6e 73 2e 20 20 41 20 66 69 6e 64  nctions.  A find
e3710 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  er-function is u
e3720 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  sed to locate th
e3730 65 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a  e appropriate.**
e3740 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e3750 6f 64 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 61  ods object for a
e3760 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
e3770 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
e3780 70 41 70 70 44 61 74 61 0a 2a 2a 20 66 69 65 6c  pAppData.** fiel
e3790 64 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  d of the sqlite3
e37a0 5f 76 66 73 20 56 46 53 20 6f 62 6a 65 63 74 73  _vfs VFS objects
e37b0 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64   are initialized
e37c0 20 74 6f 20 62 65 20 70 6f 69 6e 74 65 72 73 20   to be pointers 
e37d0 74 6f 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63  to.** the correc
e37e0 74 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  t finder-functio
e37f0 6e 20 66 6f 72 20 74 68 61 74 20 56 46 53 2e 0a  n for that VFS..
e3800 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65  **.** Most finde
e3810 72 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75  r functions retu
e3820 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
e3830 61 20 66 69 78 65 64 20 73 71 6c 69 74 65 33 5f  a fixed sqlite3_
e3840 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62  io_methods.** ob
e3850 6a 65 63 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20  ject.  The only 
e3860 69 6e 74 65 72 65 73 74 69 6e 67 20 66 69 6e 64  interesting find
e3870 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  er-function is a
e3880 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c  utolockIoFinder,
e3890 20 77 68 69 63 68 0a 2a 2a 20 6c 6f 6f 6b 73 20   which.** looks 
e38a0 61 74 20 74 68 65 20 66 69 6c 65 73 79 73 74 65  at the filesyste
e38b0 6d 20 74 79 70 65 20 61 6e 64 20 74 72 69 65 73  m type and tries
e38c0 20 74 6f 20 67 75 65 73 73 20 74 68 65 20 62 65   to guess the be
e38d0 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74  st locking.** st
e38e0 72 61 74 65 67 79 20 66 72 6f 6d 20 74 68 61 74  rategy from that
e38f0 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 69 6e 64  ..**.** For find
e3900 65 72 2d 66 75 6e 74 69 6f 6e 20 46 2c 20 74 77  er-funtion F, tw
e3910 6f 20 6f 62 6a 65 63 74 73 20 61 72 65 20 63 72  o objects are cr
e3920 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  eated:.**.**    
e3930 28 31 29 20 54 68 65 20 72 65 61 6c 20 66 69 6e  (1) The real fin
e3940 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  der-function nam
e3950 65 64 20 22 46 49 6d 70 74 28 29 22 2e 0a 2a 2a  ed "FImpt()"..**
e3960 0a 2a 2a 20 20 20 20 28 32 29 20 41 20 63 6f 6e  .**    (2) A con
e3970 73 74 61 6e 74 20 70 6f 69 6e 74 65 72 20 74 6f  stant pointer to
e3980 20 74 68 69 73 20 66 75 6e 63 74 69 6f 20 6e 61   this functio na
e3990 6d 65 64 20 6a 75 73 74 20 22 46 22 2e 0a 2a 2a  med just "F"..**
e39a0 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  .**.** A pointer
e39b0 20 74 6f 20 74 68 65 20 46 20 70 6f 69 6e 74 65   to the F pointe
e39c0 72 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65  r is used as the
e39d0 20 70 41 70 70 44 61 74 61 20 76 61 6c 75 65 20   pAppData value 
e39e0 66 6f 72 20 56 46 53 0a 2a 2a 20 6f 62 6a 65 63  for VFS.** objec
e39f0 74 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  ts.  We have to 
e3a00 64 6f 20 74 68 69 73 20 69 6e 73 74 65 61 64 20  do this instead 
e3a10 6f 66 20 6c 65 74 74 69 6e 67 20 70 41 70 70 44  of letting pAppD
e3a20 61 74 61 20 70 6f 69 6e 74 0a 2a 2a 20 64 69 72  ata point.** dir
e3a30 65 63 74 6c 79 20 61 74 20 74 68 65 20 66 69 6e  ectly at the fin
e3a40 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 73 69 6e  der-function sin
e3a50 63 65 20 43 39 30 20 72 75 6c 65 73 20 70 72 65  ce C90 rules pre
e3a60 76 65 6e 74 20 61 20 76 6f 69 64 2a 0a 2a 2a 20  vent a void*.** 
e3a70 66 72 6f 6d 20 62 65 20 63 61 73 74 20 69 6e 74  from be cast int
e3a80 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  o a function poi
e3a90 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45  nter..**.**.** E
e3aa0 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  ach instance of 
e3ab0 74 68 69 73 20 6d 61 63 72 6f 20 67 65 6e 65 72  this macro gener
e3ac0 61 74 65 73 20 74 77 6f 20 6f 62 6a 65 63 74 73  ates two objects
e3ad0 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 20 63  :.**.**   *  A c
e3ae0 6f 6e 73 74 61 6e 74 20 73 71 6c 69 74 65 33 5f  onstant sqlite3_
e3af0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63  io_methods objec
e3b00 74 20 63 61 6c 6c 20 4d 45 54 48 4f 44 20 74 68  t call METHOD th
e3b10 61 74 20 68 61 73 20 6c 6f 63 6b 69 6e 67 0a 2a  at has locking.*
e3b20 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 43  *      methods C
e3b30 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f  LOSE, LOCK, UNLO
e3b40 43 4b 2c 20 43 4b 52 45 53 4c 4f 43 4b 2e 0a 2a  CK, CKRESLOCK..*
e3b50 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 49 2f 4f  *.**   *  An I/O
e3b60 20 6d 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66   method finder f
e3b70 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 64 20 46  unction called F
e3b80 49 4e 44 45 52 20 74 68 61 74 20 72 65 74 75 72  INDER that retur
e3b90 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  ns a pointer.** 
e3ba0 20 20 20 20 20 74 6f 20 74 68 65 20 4d 45 54 48       to the METH
e3bb0 4f 44 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65  OD object in the
e3bc0 20 70 72 65 76 69 6f 75 73 20 62 75 6c 6c 65 74   previous bullet
e3bd0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4f 4d  ..*/.#define IOM
e3be0 45 54 48 4f 44 53 28 46 49 4e 44 45 52 2c 20 4d  ETHODS(FINDER, M
e3bf0 45 54 48 4f 44 2c 20 43 4c 4f 53 45 2c 20 4c 4f  ETHOD, CLOSE, LO
e3c00 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 4c 4f  CK, UNLOCK, CKLO
e3c10 43 4b 29 20 20 20 20 20 20 20 20 20 20 20 20 20  CK)             
e3c20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74    \.static const
e3c30 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e3c40 6f 64 73 20 4d 45 54 48 4f 44 20 3d 20 7b 20 20  ods METHOD = {  
e3c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3c70 20 5c 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20   \.   1,        
e3c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3c90 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f    /* iVersion */
e3ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3cc0 5c 0a 20 20 20 43 4c 4f 53 45 2c 20 20 20 20 20  \.   CLOSE,     
e3cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3ce0 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 20 20 20   /* xClose */   
e3cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e3d10 0a 20 20 20 75 6e 69 78 52 65 61 64 2c 20 20 20  .   unixRead,   
e3d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3d30 2f 2a 20 78 52 65 61 64 20 2a 2f 20 20 20 20 20  /* xRead */     
e3d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e3d60 20 20 20 75 6e 69 78 57 72 69 74 65 2c 20 20 20     unixWrite,   
e3d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e3d80 2a 20 78 57 72 69 74 65 20 2a 2f 20 20 20 20 20  * xWrite */     
e3d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3da0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e3db0 20 20 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 20    unixTruncate, 
e3dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e3dd0 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 20 20 20   xTruncate */   
e3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3df0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e3e00 20 75 6e 69 78 53 79 6e 63 2c 20 20 20 20 20 20   unixSync,      
e3e10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e3e20 78 53 79 6e 63 20 2a 2f 20 20 20 20 20 20 20 20  xSync */        
e3e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3e40 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e3e50 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20  unixFileSize,   
e3e60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e3e70 46 69 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20  FileSize */     
e3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3e90 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 4c            \.   L
e3ea0 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20  OCK,            
e3eb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c             /* xL
e3ec0 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ock */          
e3ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3ee0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 55 4e           \.   UN
e3ef0 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20  LOCK,           
e3f00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e            /* xUn
e3f10 6c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20  lock */         
e3f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3f30 20 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4b 4c          \.   CKL
e3f40 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20  OCK,            
e3f50 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 68 65           /* xChe
e3f60 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 2a  ckReservedLock *
e3f70 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e3f80 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78         \.   unix
e3f90 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20  FileControl,    
e3fa0 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65          /* xFile
e3fb0 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 20 20  Control */      
e3fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3fd0 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53        \.   unixS
e3fe0 65 63 74 6f 72 53 69 7a 65 2c 20 20 20 20 20 20  ectorSize,      
e3ff0 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f         /* xSecto
e4000 72 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20  rSize */        
e4010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4020 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 44 65       \.   unixDe
e4030 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
e4040 69 63 73 20 20 20 2f 2a 20 78 44 65 76 69 63 65  ics   /* xDevice
e4050 43 61 70 61 62 69 6c 69 74 69 65 73 20 2a 2f 20  Capabilities */ 
e4060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4070 20 20 20 20 5c 0a 7d 3b 20 20 20 20 20 20 20 20      \.};        
e4080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e40a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e40b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e40c0 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73     \.static cons
e40d0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e40e0 68 6f 64 73 20 2a 46 49 4e 44 45 52 23 23 49 6d  hods *FINDER##Im
e40f0 70 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  pl(const char *z
e4100 2c 20 69 6e 74 20 68 29 7b 20 20 20 20 20 20 20  , int h){       
e4110 20 20 5c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52    \.  UNUSED_PAR
e4120 41 4d 45 54 45 52 28 7a 29 3b 20 55 4e 55 53 45  AMETER(z); UNUSE
e4130 44 5f 50 41 52 41 4d 45 54 45 52 28 68 29 3b 20  D_PARAMETER(h); 
e4140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4160 20 5c 0a 20 20 72 65 74 75 72 6e 20 26 4d 45 54   \.  return &MET
e4170 48 4f 44 3b 20 20 20 20 20 20 20 20 20 20 20 20  HOD;            
e4180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e41a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e41b0 5c 0a 7d 20 20 20 20 20 20 20 20 20 20 20 20 20  \.}             
e41c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e41d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e41e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e41f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e4200 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
e4210 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
e4220 20 2a 28 2a 63 6f 6e 73 74 20 46 49 4e 44 45 52   *(*const FINDER
e4230 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  )(const char*,in
e4240 74 29 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a  t)            \.
e4250 20 20 20 20 3d 20 46 49 4e 44 45 52 23 23 49 6d      = FINDER##Im
e4260 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20  pl;../*.** Here 
e4270 61 72 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73  are all of the s
e4280 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e4290 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 65 61  s objects for ea
e42a0 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63  ch of the.** loc
e42b0 6b 69 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e  king strategies.
e42c0 20 20 46 75 6e 63 74 69 6f 6e 73 20 74 68 61 74    Functions that
e42d0 20 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 73   return pointers
e42e0 20 74 6f 20 74 68 65 73 65 20 6d 65 74 68 6f 64   to these method
e42f0 73 0a 2a 2a 20 61 72 65 20 61 6c 73 6f 20 63 72  s.** are also cr
e4300 65 61 74 65 64 2e 0a 2a 2f 0a 49 4f 4d 45 54 48  eated..*/.IOMETH
e4310 4f 44 53 28 0a 20 20 70 6f 73 69 78 49 6f 46 69  ODS(.  posixIoFi
e4320 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  nder,           
e4330 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74   /* Finder funct
e4340 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 6f  ion name */.  po
e4350 73 69 78 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20  sixIoMethods,   
e4360 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
e4370 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
e4380 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 75  ject name */.  u
e4390 6e 69 78 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  nixClose,       
e43a0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f           /* xClo
e43b0 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75  se method */.  u
e43c0 6e 69 78 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20  nixLock,        
e43d0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
e43e0 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e  k method */.  un
e43f0 69 78 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20  ixUnlock,       
e4400 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
e4410 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75  ck method */.  u
e4420 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  nixCheckReserved
e4430 4c 6f 63 6b 20 20 20 20 20 2f 2a 20 78 43 68 65  Lock     /* xChe
e4440 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d  ckReservedLock m
e4450 65 74 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54  ethod */.).IOMET
e4460 48 4f 44 53 28 0a 20 20 6e 6f 6c 6f 63 6b 49 6f  HODS(.  nolockIo
e4470 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20  Finder,         
e4480 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63    /* Finder func
e4490 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e  tion name */.  n
e44a0 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20  olockIoMethods, 
e44b0 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69           /* sqli
e44c0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  te3_io_methods o
e44d0 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20  bject name */.  
e44e0 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20  nolockClose,    
e44f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c            /* xCl
e4500 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ose method */.  
e4510 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20  nolockLock,     
e4520 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f            /* xLo
e4530 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e  ck method */.  n
e4540 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20  olockUnlock,    
e4550 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c           /* xUnl
e4560 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ock method */.  
e4570 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72  nolockCheckReser
e4580 76 65 64 4c 6f 63 6b 20 20 20 2f 2a 20 78 43 68  vedLock   /* xCh
e4590 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
e45a0 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45  method */.).IOME
e45b0 54 48 4f 44 53 28 0a 20 20 64 6f 74 6c 6f 63 6b  THODS(.  dotlock
e45c0 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
e45d0 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e     /* Finder fun
e45e0 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  ction name */.  
e45f0 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73  dotlockIoMethods
e4600 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c  ,         /* sql
e4610 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
e4620 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20  object name */. 
e4630 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20   dotlockClose,  
e4640 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
e4650 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lose method */. 
e4660 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20   dotlockLock,   
e4670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c             /* xL
e4680 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ock method */.  
e4690 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20  dotlockUnlock,  
e46a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e            /* xUn
e46b0 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lock method */. 
e46c0 20 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73   dotlockCheckRes
e46d0 65 72 76 65 64 4c 6f 63 6b 20 20 2f 2a 20 78 43  ervedLock  /* xC
e46e0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
e46f0 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 0a 23 69   method */.)..#i
e4700 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
e4710 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f  LOCKING_STYLE.IO
e4720 4d 45 54 48 4f 44 53 28 0a 20 20 66 6c 6f 63 6b  METHODS(.  flock
e4730 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
e4740 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66       /* Finder f
e4750 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
e4760 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73    flockIoMethods
e4770 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73  ,           /* s
e4780 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e4790 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f  s object name */
e47a0 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20  .  flockClose,  
e47b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e47c0 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f  xClose method */
e47d0 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20  .  flockLock,   
e47e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e47f0 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  xLock method */.
e4800 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20    flockUnlock,  
e4810 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e4820 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  Unlock method */
e4830 0a 20 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73  .  flockCheckRes
e4840 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a 20  ervedLock    /* 
e4850 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
e4860 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23  ck method */.).#
e4870 65 6e 64 69 66 0a 0a 23 69 66 20 4f 53 5f 56 58  endif..#if OS_VX
e4880 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28  WORKS.IOMETHODS(
e4890 0a 20 20 73 65 6d 49 6f 46 69 6e 64 65 72 2c 20  .  semIoFinder, 
e48a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e48b0 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20  Finder function 
e48c0 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d 49 6f 4d  name */.  semIoM
e48d0 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20  ethods,         
e48e0 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69      /* sqlite3_i
e48f0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
e4900 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d 43 6c   name */.  semCl
e4910 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ose,            
e4920 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d       /* xClose m
e4930 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d 4c 6f  ethod */.  semLo
e4940 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
e4950 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65       /* xLock me
e4960 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d 55 6e 6c  thod */.  semUnl
e4970 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
e4980 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d      /* xUnlock m
e4990 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d 43 68  ethod */.  semCh
e49a0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
e49b0 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
e49c0 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f  servedLock metho
e49d0 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23  d */.).#endif..#
e49e0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  if defined(__APP
e49f0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f  LE__) && SQLITE_
e4a00 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e4a10 54 59 4c 45 0a 49 4f 4d 45 54 48 4f 44 53 28 0a  TYLE.IOMETHODS(.
e4a20 20 20 61 66 70 49 6f 46 69 6e 64 65 72 2c 20 20    afpIoFinder,  
e4a30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
e4a40 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e  inder function n
e4a50 61 6d 65 20 2a 2f 0a 20 20 61 66 70 49 6f 4d 65  ame */.  afpIoMe
e4a60 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20  thods,          
e4a70 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f     /* sqlite3_io
e4a80 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20  _methods object 
e4a90 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 43 6c 6f  name */.  afpClo
e4aa0 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
e4ab0 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65      /* xClose me
e4ac0 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 4c 6f 63  thod */.  afpLoc
e4ad0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
e4ae0 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74      /* xLock met
e4af0 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55 6e 6c 6f  hod */.  afpUnlo
e4b00 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
e4b10 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65     /* xUnlock me
e4b20 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 43 68 65  thod */.  afpChe
e4b30 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20  ckReservedLock  
e4b40 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73      /* xCheckRes
e4b50 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64  ervedLock method
e4b60 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a   */.).#endif../*
e4b70 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f  .** The proxy lo
e4b80 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 69 73 20  cking method is 
e4b90 61 20 22 73 75 70 65 72 2d 6d 65 74 68 6f 64 22  a "super-method"
e4ba0 20 69 6e 20 74 68 65 20 73 65 6e 73 65 20 74 68   in the sense th
e4bb0 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73  at it.** opens s
e4bc0 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 20 64 65  econdary file de
e4bd0 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 74 68  scriptors for th
e4be0 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b  e conch and lock
e4bf0 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74   files and.** it
e4c00 20 75 73 65 73 20 70 72 6f 78 79 2c 20 64 6f 74   uses proxy, dot
e4c10 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20  -file, AFP, and 
e4c20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20  flock() locking 
e4c30 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65  methods on those
e4c40 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 20 66 69  .** secondary fi
e4c50 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 73 20 72  les.  For this r
e4c60 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73  eason, the divis
e4c70 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ion that impleme
e4c80 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63  nts.** proxy loc
e4c90 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 20  king is located 
e4ca0 6d 75 63 68 20 66 75 72 74 68 65 72 20 64 6f 77  much further dow
e4cb0 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20  n in the file.  
e4cc0 42 75 74 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74  But we need.** t
e4cd0 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 64  o go ahead and d
e4ce0 65 66 69 6e 65 20 74 68 65 20 73 71 6c 69 74 65  efine the sqlite
e4cf0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64  3_io_methods and
e4d00 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e   finder function
e4d10 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f  .** for proxy lo
e4d20 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53 6f 20  cking here.  So 
e4d30 77 65 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61  we forward decla
e4d40 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f  re the I/O metho
e4d50 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ds..*/.#if defin
e4d60 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
e4d70 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
e4d80 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61  OCKING_STYLE.sta
e4d90 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f  tic int proxyClo
e4da0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  se(sqlite3_file*
e4db0 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  );.static int pr
e4dc0 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  oxyLock(sqlite3_
e4dd0 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61  file*, int);.sta
e4de0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c  tic int proxyUnl
e4df0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
e4e00 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 20  *, int);.static 
e4e10 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65  int proxyCheckRe
e4e20 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
e4e30 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b  e3_file*, int*);
e4e40 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 72  .IOMETHODS(.  pr
e4e50 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  oxyIoFinder,    
e4e60 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
e4e70 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
e4e80 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d 65 74 68  */.  proxyIoMeth
e4e90 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ods,           /
e4ea0 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
e4eb0 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
e4ec0 20 2a 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65   */.  proxyClose
e4ed0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e4ee0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
e4ef0 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c   */.  proxyLock,
e4f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4f10 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
e4f20 2a 2f 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b  */.  proxyUnlock
e4f30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e4f40 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
e4f50 20 2a 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b   */.  proxyCheck
e4f60 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20  ReservedLock    
e4f70 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
e4f80 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
e4f90 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64  ).#endif...#if d
e4fa0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
e4fb0 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42  ) && SQLITE_ENAB
e4fc0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e4fd0 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69  ./* .** This "fi
e4fe0 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61  nder" function a
e4ff0 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72  ttempts to deter
e5000 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f  mine the best lo
e5010 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a  cking strategy .
e5020 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  ** for the datab
e5030 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61  ase file "filePa
e5040 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65  th".  It then re
e5050 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65  turns the sqlite
e5060 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20  3_io_methods.** 
e5070 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c  object that impl
e5080 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61  ements that stra
e5090 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tegy..**.** This
e50a0 20 69 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f   is for MacOSX o
e50b0 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  nly..*/.static c
e50c0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
e50d0 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63  methods *autoloc
e50e0 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20  kIoFinderImpl(. 
e50f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c   const char *fil
e5100 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d  ePath,    /* nam
e5110 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
e5120 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  e file */.  int 
e5130 66 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  fd              
e5140 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64 65 73       /* file des
e5150 63 72 69 70 74 6f 72 20 6f 70 65 6e 20 6f 6e 20  criptor open on 
e5160 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
e5170 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63  e */.){.  static
e5180 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 4d 61   const struct Ma
e5190 70 70 69 6e 67 20 7b 0a 20 20 20 20 63 6f 6e 73  pping {.    cons
e51a0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 73 79 73  t char *zFilesys
e51b0 74 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  tem;            
e51c0 20 20 2f 2a 20 46 69 6c 65 73 79 73 74 65 6d 20    /* Filesystem 
e51d0 74 79 70 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  type name */.   
e51e0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e51f0 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68  o_methods *pMeth
e5200 6f 64 73 3b 20 20 20 2f 2a 20 41 70 70 72 6f 70  ods;   /* Approp
e5210 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 65  riate locking me
e5220 74 68 6f 64 20 2a 2f 0a 20 20 7d 20 61 4d 61 70  thod */.  } aMap
e5230 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66  [] = {.    { "hf
e5240 73 22 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d  s",    &posixIoM
e5250 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20  ethods },.    { 
e5260 22 75 66 73 22 2c 20 20 20 20 26 70 6f 73 69 78  "ufs",    &posix
e5270 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20  IoMethods },.   
e5280 20 7b 20 22 61 66 70 66 73 22 2c 20 20 26 61 66   { "afpfs",  &af
e5290 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 69  pIoMethods },.#i
e52a0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
e52b0 4c 45 5f 41 46 50 5f 4c 4f 43 4b 49 4e 47 5f 53  LE_AFP_LOCKING_S
e52c0 4d 42 0a 20 20 20 20 7b 20 22 73 6d 62 66 73 22  MB.    { "smbfs"
e52d0 2c 20 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73  ,  &afpIoMethods
e52e0 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20   },.#else.    { 
e52f0 22 73 6d 62 66 73 22 2c 20 20 26 66 6c 6f 63 6b  "smbfs",  &flock
e5300 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65 6e  IoMethods },.#en
e5310 64 69 66 0a 20 20 20 20 7b 20 22 77 65 62 64 61  dif.    { "webda
e5320 76 22 2c 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74  v", &nolockIoMet
e5330 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 30 2c  hods },.    { 0,
e5340 20 30 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20   0 }.  };.  int 
e5350 69 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74  i;.  struct stat
e5360 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 73 74 72  fs fsInfo;.  str
e5370 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e  uct flock lockIn
e5380 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c 65  fo;..  if( !file
e5390 50 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Path ){.    /* I
e53a0 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c  f filePath==NULL
e53b0 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 20 61   that means we a
e53c0 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
e53d0 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65  a transient file
e53e0 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 64 6f 65  .    ** that doe
e53f0 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  s not need to be
e5400 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20   locked. */.    
e5410 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f  return &nolockIo
e5420 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 20 20 69  Methods;.  }.  i
e5430 66 28 20 73 74 61 74 66 73 28 66 69 6c 65 50 61  f( statfs(filePa
e5440 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 21 3d 20  th, &fsInfo) != 
e5450 2d 31 20 29 7b 0a 20 20 20 20 69 66 28 20 66 73  -1 ){.    if( fs
e5460 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 20 26 20 4d  Info.f_flags & M
e5470 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b 0a 20 20 20  NT_RDONLY ){.   
e5480 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63     return &noloc
e5490 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20  kIoMethods;.    
e54a0 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61  }.    for(i=0; a
e54b0 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74  Map[i].zFilesyst
e54c0 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  em; i++){.      
e54d0 69 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66  if( strcmp(fsInf
e54e0 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20  o.f_fstypename, 
e54f0 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73  aMap[i].zFilesys
e5500 74 65 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  tem)==0 ){.     
e5510 20 20 20 72 65 74 75 72 6e 20 61 4d 61 70 5b 69     return aMap[i
e5520 5d 2e 70 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20  ].pMethods;.    
e5530 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
e5540 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 61 73 65   /* Default case
e5550 2e 20 48 61 6e 64 6c 65 73 2c 20 61 6d 6f 6e 67  . Handles, among
e5560 73 74 20 6f 74 68 65 72 73 2c 20 22 6e 66 73 22  st others, "nfs"
e5570 2e 0a 20 20 2a 2a 20 54 65 73 74 20 62 79 74 65  ..  ** Test byte
e5580 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 75 73 69 6e  -range lock usin
e5590 67 20 66 63 6e 74 6c 28 29 2e 20 49 66 20 74 68  g fcntl(). If th
e55a0 65 20 63 61 6c 6c 20 73 75 63 63 65 65 64 73 2c  e call succeeds,
e55b0 20 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20 74 68   .  ** assume th
e55c0 61 74 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  at the file-syst
e55d0 65 6d 20 73 75 70 70 6f 72 74 73 20 50 4f 53 49  em supports POSI
e55e0 58 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e 20 0a  X style locks. .
e55f0 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e    */.  lockInfo.
e5600 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63  l_len = 1;.  loc
e5610 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20  kInfo.l_start = 
e5620 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f  0;.  lockInfo.l_
e5630 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45  whence = SEEK_SE
e5640 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f  T;.  lockInfo.l_
e5650 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a  type = F_RDLCK;.
e5660 20 20 69 66 28 20 66 63 6e 74 6c 28 66 64 2c 20    if( fcntl(fd, 
e5670 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e  F_GETLK, &lockIn
e5680 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20  fo)!=-1 ) {.    
e5690 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f 4d  return &posixIoM
e56a0 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b  ethods;.  }else{
e56b0 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f 74  .    return &dot
e56c0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  lockIoMethods;. 
e56d0 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73   }.}.static cons
e56e0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e56f0 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 61 75  hods *(*const au
e5700 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28  tolockIoFinder)(
e5710 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29  const char*,int)
e5720 0a 20 20 20 20 20 20 20 20 3d 20 61 75 74 6f 6c  .        = autol
e5730 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b  ockIoFinderImpl;
e5740 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  ..#endif /* defi
e5750 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26  ned(__APPLE__) &
e5760 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
e5770 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
e5780 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 61 62 73 74 72  ../*.** An abstr
e5790 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 70  act type for a p
e57a0 6f 69 6e 74 65 72 20 74 6f 20 61 20 49 4f 20 6d  ointer to a IO m
e57b0 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e  ethod finder fun
e57c0 63 74 69 6f 6e 3a 0a 2a 2f 0a 74 79 70 65 64 65  ction:.*/.typede
e57d0 66 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  f const sqlite3_
e57e0 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 66 69  io_methods *(*fi
e57f0 6e 64 65 72 5f 74 79 70 65 29 28 63 6f 6e 73 74  nder_type)(const
e5800 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 0a 0a 2f   char*,int);.../
e5810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e5820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e5830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e5840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e5850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
e5860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e5870 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65  ********* sqlite
e5880 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a  3_vfs methods **
e5890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e58a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
e58b0 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63   This division c
e58c0 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c  ontains the impl
e58d0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65  ementation of me
e58e0 74 68 6f 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20  thods on the.** 
e58f0 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65  sqlite3_vfs obje
e5900 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ct..*/../*.** In
e5910 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e  itialize the con
e5920 74 65 6e 74 73 20 6f 66 20 74 68 65 20 75 6e 69  tents of the uni
e5930 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20  xFile structure 
e5940 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 49  pointed to by pI
e5950 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
e5960 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28   fillInUnixFile(
e5970 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
e5980 70 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f  pVfs,      /* Po
e5990 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a  inter to vfs obj
e59a0 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20  ect */.  int h, 
e59b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e59c0 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65   /* Open file de
e59d0 73 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c 65  scriptor of file
e59e0 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f   being opened */
e59f0 0a 20 20 69 6e 74 20 64 69 72 66 64 2c 20 20 20  .  int dirfd,   
e5a00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69             /* Di
e5a10 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73  rectory file des
e5a20 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c  criptor */.  sql
e5a30 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20  ite3_file *pId, 
e5a40 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 6f       /* Write to
e5a50 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74   the unixFile st
e5a60 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a  ructure here */.
e5a70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
e5a80 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d  ilename,  /* Nam
e5a90 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 62 65  e of the file be
e5aa0 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20  ing opened */.  
e5ab0 69 6e 74 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20  int noLock,     
e5ac0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20          /* Omit 
e5ad0 6c 6f 63 6b 69 6e 67 20 69 66 20 74 72 75 65 20  locking if true 
e5ae0 2a 2f 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74  */.  int isDelet
e5af0 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e            /* 
e5b00 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20  Delete on close 
e5b10 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20  if true */.){.  
e5b20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
e5b30 5f 6d 65 74 68 6f 64 73 20 2a 70 4c 6f 63 6b 69  _methods *pLocki
e5b40 6e 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46  ngStyle;.  unixF
e5b50 69 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69  ile *pNew = (uni
e5b60 78 46 69 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69  xFile *)pId;.  i
e5b70 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
e5b80 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e  K;..  assert( pN
e5b90 65 77 2d 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20  ew->pLock==NULL 
e5ba0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65  );.  assert( pNe
e5bb0 77 2d 3e 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29  w->pOpen==NULL )
e5bc0 3b 0a 0a 20 20 2f 2a 20 50 61 72 61 6d 65 74 65  ;..  /* Paramete
e5bd0 72 20 69 73 44 65 6c 65 74 65 20 69 73 20 6f 6e  r isDelete is on
e5be0 6c 79 20 75 73 65 64 20 6f 6e 20 76 78 77 6f 72  ly used on vxwor
e5bf0 6b 73 2e 0a 20 20 2a 2a 20 45 78 70 72 65 73 73  ks..  ** Express
e5c00 20 74 68 69 73 20 65 78 70 6c 69 63 69 74 6c 79   this explicitly
e5c10 20 68 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74   here to prevent
e5c20 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e   compiler warnin
e5c30 67 73 0a 20 20 2a 2a 20 61 62 6f 75 74 20 75 6e  gs.  ** about un
e5c40 75 73 65 64 20 70 61 72 61 6d 65 74 65 72 73 2e  used parameters.
e5c50 0a 20 20 2a 2f 0a 23 69 66 20 21 4f 53 5f 56 58  .  */.#if !OS_VX
e5c60 57 4f 52 4b 53 0a 20 20 55 4e 55 53 45 44 5f 50  WORKS.  UNUSED_P
e5c70 41 52 41 4d 45 54 45 52 28 69 73 44 65 6c 65 74  ARAMETER(isDelet
e5c80 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 4f 53  e);.#endif..  OS
e5c90 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20 20 20  TRACE3("OPEN    
e5ca0 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c 20 7a  %-3d %s\n", h, z
e5cb0 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20 0a 20  Filename);    . 
e5cc0 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a 20 20   pNew->h = h;.  
e5cd0 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20 64 69  pNew->dirfd = di
e5ce0 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52 45 41  rfd;.  SET_THREA
e5cf0 44 49 44 28 70 4e 65 77 29 3b 0a 0a 23 69 66 20  DID(pNew);..#if 
e5d00 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 70 4e 65  OS_VXWORKS.  pNe
e5d10 77 2d 3e 70 49 64 20 3d 20 76 78 77 6f 72 6b 73  w->pId = vxworks
e5d20 46 69 6e 64 46 69 6c 65 49 64 28 7a 46 69 6c 65  FindFileId(zFile
e5d30 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e 65  name);.  if( pNe
e5d40 77 2d 3e 70 49 64 3d 3d 30 20 29 7b 0a 20 20 20  w->pId==0 ){.   
e5d50 20 6e 6f 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20   noLock = 1;.   
e5d60 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
e5d70 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  EM;.  }.#endif..
e5d80 20 20 69 66 28 20 6e 6f 4c 6f 63 6b 20 29 7b 0a    if( noLock ){.
e5d90 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c      pLockingStyl
e5da0 65 20 3d 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74  e = &nolockIoMet
e5db0 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  hods;.  }else{. 
e5dc0 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65     pLockingStyle
e5dd0 20 3d 20 28 2a 2a 28 66 69 6e 64 65 72 5f 74 79   = (**(finder_ty
e5de0 70 65 2a 29 70 56 66 73 2d 3e 70 41 70 70 44 61  pe*)pVfs->pAppDa
e5df0 74 61 29 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 68  ta)(zFilename, h
e5e00 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  );.#if SQLITE_EN
e5e10 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
e5e20 4c 45 0a 20 20 20 20 2f 2a 20 43 61 63 68 65 20  LE.    /* Cache 
e5e30 7a 46 69 6c 65 6e 61 6d 65 20 69 6e 20 74 68 65  zFilename in the
e5e40 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
e5e50 20 28 41 46 50 20 61 6e 64 20 64 6f 74 6c 6f 63   (AFP and dotloc
e5e60 6b 20 6f 76 65 72 72 69 64 65 29 20 66 6f 72 0a  k override) for.
e5e70 20 20 20 20 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b      ** proxyLock
e5e80 20 61 63 74 69 76 61 74 69 6f 6e 20 69 73 20 70   activation is p
e5e90 6f 73 73 69 62 6c 65 20 28 72 65 6d 6f 74 65 20  ossible (remote 
e5ea0 70 72 6f 78 79 20 69 73 20 62 61 73 65 64 20 6f  proxy is based o
e5eb0 6e 20 64 62 20 6e 61 6d 65 29 0a 20 20 20 20 2a  n db name).    *
e5ec0 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 72 65 6d 61  * zFilename rema
e5ed0 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20  ins valid until 
e5ee0 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 2c 20  file is closed, 
e5ef0 74 6f 20 73 75 70 70 6f 72 74 20 2a 2f 0a 20 20  to support */.  
e5f00 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43    pNew->lockingC
e5f10 6f 6e 74 65 78 74 20 3d 20 28 76 6f 69 64 2a 29  ontext = (void*)
e5f20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 23 65 6e 64 69  zFilename;.#endi
e5f30 66 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4c 6f  f.  }..  if( pLo
e5f40 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 70  ckingStyle == &p
e5f50 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 29 7b  osixIoMethods ){
e5f60 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  .    unixEnterMu
e5f70 74 65 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20  tex();.    rc = 
e5f80 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65  findLockInfo(pNe
e5f90 77 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c  w, &pNew->pLock,
e5fa0 20 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a   &pNew->pOpen);.
e5fb0 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74      unixLeaveMut
e5fc0 65 78 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 20 53  ex();.  }..#if S
e5fd0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
e5fe0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65  KING_STYLE && de
e5ff0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e6000 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63  .  else if( pLoc
e6010 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 61 66  kingStyle == &af
e6020 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  pIoMethods ){.  
e6030 20 20 2f 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67    /* AFP locking
e6040 20 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70   uses the file p
e6050 61 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20  ath so it needs 
e6060 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69  to be included i
e6070 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 61 66 70  n.    ** the afp
e6080 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2e 0a  LockingContext..
e6090 20 20 20 20 2a 2f 0a 20 20 20 20 61 66 70 4c 6f      */.    afpLo
e60a0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
e60b0 74 78 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f  tx;.    pNew->lo
e60c0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70  ckingContext = p
e60d0 43 74 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  Ctx = sqlite3_ma
e60e0 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43  lloc( sizeof(*pC
e60f0 74 78 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70  tx) );.    if( p
e6100 43 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Ctx==0 ){.      
e6110 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
e6120 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  M;.    }else{.  
e6130 20 20 20 20 2f 2a 20 4e 42 3a 20 7a 46 69 6c 65      /* NB: zFile
e6140 6e 61 6d 65 20 65 78 69 73 74 73 20 61 6e 64 20  name exists and 
e6150 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e  remains valid un
e6160 74 69 6c 20 74 68 65 20 66 69 6c 65 20 69 73 20  til the file is 
e6170 63 6c 6f 73 65 64 0a 20 20 20 20 20 20 2a 2a 20  closed.      ** 
e6180 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 72 65 71  according to req
e6190 75 69 72 65 6d 65 6e 74 20 46 31 31 31 34 31 2e  uirement F11141.
e61a0 20 20 53 6f 20 77 65 20 64 6f 20 6e 6f 74 20 6e    So we do not n
e61b0 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 0a 20 20  eed to make a.  
e61c0 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74      ** copy of t
e61d0 68 65 20 66 69 6c 65 6e 61 6d 65 2e 20 2a 2f 0a  he filename. */.
e61e0 20 20 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61        pCtx->dbPa
e61f0 74 68 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a  th = zFilename;.
e6200 20 20 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76        srandomdev
e6210 28 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 45 6e  ();.      unixEn
e6220 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  terMutex();.    
e6230 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49    rc = findLockI
e6240 6e 66 6f 28 70 4e 65 77 2c 20 4e 55 4c 4c 2c 20  nfo(pNew, NULL, 
e6250 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20  &pNew->pOpen);. 
e6260 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75       unixLeaveMu
e6270 74 65 78 28 29 3b 20 20 20 20 20 20 20 20 0a 20  tex();        . 
e6280 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
e6290 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63  .  else if( pLoc
e62a0 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 64 6f  kingStyle == &do
e62b0 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29  tlockIoMethods )
e62c0 7b 0a 20 20 20 20 2f 2a 20 44 6f 74 66 69 6c 65  {.    /* Dotfile
e62d0 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68   locking uses th
e62e0 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69  e file path so i
e62f0 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e  t needs to be in
e6300 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a  cluded in.    **
e6310 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b   the dotlockLock
e6320 69 6e 67 43 6f 6e 74 65 78 74 20 0a 20 20 20 20  ingContext .    
e6330 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4c 6f  */.    char *zLo
e6340 63 6b 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 20  ckFile;.    int 
e6350 6e 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 6e  nFilename;.    n
e6360 46 69 6c 65 6e 61 6d 65 20 3d 20 28 69 6e 74 29  Filename = (int)
e6370 73 74 72 6c 65 6e 28 7a 46 69 6c 65 6e 61 6d 65  strlen(zFilename
e6380 29 20 2b 20 36 3b 0a 20 20 20 20 7a 4c 6f 63 6b  ) + 6;.    zLock
e6390 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 73  File = (char *)s
e63a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 46  qlite3_malloc(nF
e63b0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66  ilename);.    if
e63c0 28 20 7a 4c 6f 63 6b 46 69 6c 65 3d 3d 30 20 29  ( zLockFile==0 )
e63d0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
e63e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
e63f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
e6400 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69  te3_snprintf(nFi
e6410 6c 65 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c  lename, zLockFil
e6420 65 2c 20 22 25 73 22 20 44 4f 54 4c 4f 43 4b 5f  e, "%s" DOTLOCK_
e6430 53 55 46 46 49 58 2c 20 7a 46 69 6c 65 6e 61 6d  SUFFIX, zFilenam
e6440 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e  e);.    }.    pN
e6450 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  ew->lockingConte
e6460 78 74 20 3d 20 7a 4c 6f 63 6b 46 69 6c 65 3b 0a  xt = zLockFile;.
e6470 20 20 7d 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f    }..#if OS_VXWO
e6480 52 4b 53 0a 20 20 65 6c 73 65 20 69 66 28 20 70  RKS.  else if( p
e6490 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20  LockingStyle == 
e64a0 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 20 29 7b  &semIoMethods ){
e64b0 0a 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 73 65  .    /* Named se
e64c0 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20  maphore locking 
e64d0 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61  uses the file pa
e64e0 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74  th so it needs t
e64f0 6f 20 62 65 0a 20 20 20 20 2a 2a 20 69 6e 63 6c  o be.    ** incl
e6500 75 64 65 64 20 69 6e 20 74 68 65 20 73 65 6d 4c  uded in the semL
e6510 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 20 20  ockingContext.  
e6520 20 20 2a 2f 0a 20 20 20 20 75 6e 69 78 45 6e 74    */.    unixEnt
e6530 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72  erMutex();.    r
e6540 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f  c = findLockInfo
e6550 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 4c  (pNew, &pNew->pL
e6560 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65  ock, &pNew->pOpe
e6570 6e 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 3d  n);.    if( (rc=
e6580 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28  =SQLITE_OK) && (
e6590 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65  pNew->pOpen->pSe
e65a0 6d 3d 3d 4e 55 4c 4c 29 20 29 7b 0a 20 20 20 20  m==NULL) ){.    
e65b0 20 20 63 68 61 72 20 2a 7a 53 65 6d 4e 61 6d 65    char *zSemName
e65c0 20 3d 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e   = pNew->pOpen->
e65d0 61 53 65 6d 4e 61 6d 65 3b 0a 20 20 20 20 20 20  aSemName;.      
e65e0 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 73 71 6c  int n;.      sql
e65f0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41  ite3_snprintf(MA
e6600 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 53 65 6d  X_PATHNAME, zSem
e6610 4e 61 6d 65 2c 20 22 25 73 2e 73 65 6d 22 2c 0a  Name, "%s.sem",.
e6620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6630 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 49 64         pNew->pId
e6640 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65  ->zCanonicalName
e6650 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 20 6e 3d  );.      for( n=
e6660 30 3b 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3b 20  0; zSemName[n]; 
e6670 6e 2b 2b 20 29 0a 20 20 20 20 20 20 20 20 69 66  n++ ).        if
e6680 28 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d 27  ( zSemName[n]=='
e6690 2f 27 20 29 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d  /' ) zSemName[n]
e66a0 20 3d 20 27 5f 27 3b 0a 20 20 20 20 20 20 70 4e   = '_';.      pN
e66b0 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20  ew->pOpen->pSem 
e66c0 3d 20 73 65 6d 5f 6f 70 65 6e 28 7a 53 65 6d 4e  = sem_open(zSemN
e66d0 61 6d 65 2c 20 4f 5f 43 52 45 41 54 2c 20 30 36  ame, O_CREAT, 06
e66e0 36 36 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66  66, 1);.      if
e66f0 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70  ( pNew->pOpen->p
e6700 53 65 6d 20 3d 3d 20 53 45 4d 5f 46 41 49 4c 45  Sem == SEM_FAILE
e6710 44 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  D ){.        rc 
e6720 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
e6730 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f          pNew->pO
e6740 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b 30 5d  pen->aSemName[0]
e6750 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 7d   = '\0';.      }
e6760 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c  .    }.    unixL
e6770 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
e6780 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 70 4e 65  .#endif.  .  pNe
e6790 77 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30  w->lastErrno = 0
e67a0 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  ;.#if OS_VXWORKS
e67b0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
e67c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 69  E_OK ){.    unli
e67d0 6e 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  nk(zFilename);. 
e67e0 20 20 20 69 73 44 65 6c 65 74 65 20 3d 20 30 3b     isDelete = 0;
e67f0 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 69 73 44  .  }.  pNew->isD
e6800 65 6c 65 74 65 20 3d 20 69 73 44 65 6c 65 74 65  elete = isDelete
e6810 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 72  ;.#endif.  if( r
e6820 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
e6830 20 20 20 20 69 66 28 20 64 69 72 66 64 3e 3d 30      if( dirfd>=0
e6840 20 29 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b   ) close(dirfd);
e6850 20 2f 2a 20 73 69 6c 65 6e 74 20 6c 65 61 6b 20   /* silent leak 
e6860 69 66 20 66 61 69 6c 2c 20 61 6c 72 65 61 64 79  if fail, already
e6870 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20   in error */.   
e6880 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 7d 65 6c   close(h);.  }el
e6890 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4d  se{.    pNew->pM
e68a0 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b 69 6e 67  ethod = pLocking
e68b0 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70 65 6e 43  Style;.    OpenC
e68c0 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 7d 0a  ounter(+1);.  }.
e68d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
e68e0 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c  /*.** Open a fil
e68f0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20  e descriptor to 
e6900 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 63 6f  the directory co
e6910 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 20 7a 46  ntaining file zF
e6920 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66 20 73  ilename..** If s
e6930 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46 64 20  uccessful, *pFd 
e6940 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f 70  is set to the op
e6950 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63 72 69  ened file descri
e6960 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 51 4c 49  ptor and.** SQLI
e6970 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
e6980 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  d. If an error o
e6990 63 63 75 72 73 2c 20 65 69 74 68 65 72 20 53 51  ccurs, either SQ
e69a0 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 6f 72  LITE_NOMEM.** or
e69b0 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
e69c0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
e69d0 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f 20   *pFd is set to 
e69e0 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20  an undefined.** 
e69f0 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  value..**.** If 
e6a00 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
e6a10 75 72 6e 65 64 2c 20 74 68 65 20 63 61 6c 6c 65  urned, the calle
e6a20 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  r is responsible
e6a30 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a 2a 2a 20   for closing.** 
e6a40 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
e6a50 74 6f 72 20 2a 70 46 64 20 75 73 69 6e 67 20 63  tor *pFd using c
e6a60 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  lose()..*/.stati
e6a70 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 65 63 74  c int openDirect
e6a80 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ory(const char *
e6a90 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e 74 20 2a  zFilename, int *
e6aa0 70 46 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  pFd){.  int ii;.
e6ab0 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b 0a 20    int fd = -1;. 
e6ac0 20 63 68 61 72 20 7a 44 69 72 6e 61 6d 65 5b 4d   char zDirname[M
e6ad0 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 0a  AX_PATHNAME+1];.
e6ae0 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
e6af0 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ntf(MAX_PATHNAME
e6b00 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 25 73 22  , zDirname, "%s"
e6b10 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  , zFilename);.  
e6b20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73 74 72 6c  for(ii=(int)strl
e6b30 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20 69 69  en(zDirname); ii
e6b40 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d 65 5b 69  >1 && zDirname[i
e6b50 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 3b 0a  i]!='/'; ii--);.
e6b60 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a 20 20    if( ii>0 ){.  
e6b70 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 20 3d    zDirname[ii] =
e6b80 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20 3d 20   '\0';.    fd = 
e6b90 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20 4f  open(zDirname, O
e6ba0 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59  _RDONLY|O_BINARY
e6bb0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 66 64  , 0);.    if( fd
e6bc0 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 46 44  >=0 ){.#ifdef FD
e6bd0 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20 20 66  _CLOEXEC.      f
e6be0 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44  cntl(fd, F_SETFD
e6bf0 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45  , fcntl(fd, F_GE
e6c00 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f  TFD, 0) | FD_CLO
e6c10 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a 20 20  EXEC);.#endif.  
e6c20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50      OSTRACE3("OP
e6c30 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c 6e 22  ENDIR %-3d %s\n"
e6c40 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65 29 3b  , fd, zDirname);
e6c50 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 46  .    }.  }.  *pF
e6c60 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75 72 6e  d = fd;.  return
e6c70 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45 5f 4f   (fd>=0?SQLITE_O
e6c80 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  K:SQLITE_CANTOPE
e6c90 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  N);.}../*.** Cre
e6ca0 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ate a temporary 
e6cb0 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75  file name in zBu
e6cc0 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65  f.  zBuf must be
e6cd0 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79   allocated.** by
e6ce0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f   the calling pro
e6cf0 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20 62 65  cess and must be
e6d00 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68   big enough to h
e6d10 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20  old at least.** 
e6d20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
e6d30 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   bytes..*/.stati
e6d40 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d  c int getTempnam
e6d50 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  e(int nBuf, char
e6d60 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69   *zBuf){.  stati
e6d70 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a  c const char *az
e6d80 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  Dirs[] = {.     
e6d90 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20 20 20 20  0,.     0,.     
e6da0 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20 20 20 20  "/var/tmp",.    
e6db0 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a 20 20 20   "/usr/tmp",.   
e6dc0 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22    "/tmp",.     "
e6dd0 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69  .",.  };.  stati
e6de0 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
e6df0 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d   char zChars[] =
e6e00 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a  .    "abcdefghij
e6e10 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a  klmnopqrstuvwxyz
e6e20 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49  ".    "ABCDEFGHI
e6e30 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59  JKLMNOPQRSTUVWXY
e6e40 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37  Z".    "01234567
e6e50 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  89";.  unsigned 
e6e60 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75  int i, j;.  stru
e6e70 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 63  ct stat buf;.  c
e6e80 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 20  onst char *zDir 
e6e90 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49 74 27  = ".";..  /* It'
e6ea0 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74  s odd to simulat
e6eb0 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65  e an io-error he
e6ec0 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74  re, but really t
e6ed0 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a  his is just.  **
e6ee0 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72   using the io-er
e6ef0 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75  ror infrastructu
e6f00 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20  re to test that 
e6f10 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74  SQLite handles t
e6f20 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  his.  ** functio
e6f30 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f  n failing. .  */
e6f40 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
e6f50 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
e6f60 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 61 7a  E_IOERR );..  az
e6f70 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69 74 65  Dirs[0] = sqlite
e6f80 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
e6f90 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20 3d 3d 20  ;.  if (NULL == 
e6fa0 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a 20 20 20  azDirs[1]) {.   
e6fb0 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20 67 65 74   azDirs[1] = get
e6fc0 65 6e 76 28 22 54 4d 50 44 49 52 22 29 3b 0a 20  env("TMPDIR");. 
e6fd0 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69 3d 30 3b   }.  .  for(i=0;
e6fe0 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 69 72 73   i<sizeof(azDirs
e6ff0 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 72 73 5b  )/sizeof(azDirs[
e7000 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  0]); i++){.    i
e7010 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d 3d 30 20  f( azDirs[i]==0 
e7020 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
e7030 69 66 28 20 73 74 61 74 28 61 7a 44 69 72 73 5b  if( stat(azDirs[
e7040 69 5d 2c 20 26 62 75 66 29 20 29 20 63 6f 6e 74  i], &buf) ) cont
e7050 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 21 53  inue;.    if( !S
e7060 5f 49 53 44 49 52 28 62 75 66 2e 73 74 5f 6d 6f  _ISDIR(buf.st_mo
e7070 64 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  de) ) continue;.
e7080 20 20 20 20 69 66 28 20 61 63 63 65 73 73 28 61      if( access(a
e7090 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29 20 29 20  zDirs[i], 07) ) 
e70a0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 44  continue;.    zD
e70b0 69 72 20 3d 20 61 7a 44 69 72 73 5b 69 5d 3b 0a  ir = azDirs[i];.
e70c0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
e70d0 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
e70e0 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
e70f0 72 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67  r is large enoug
e7100 68 20 66 6f 72 20 74 68 65 20 74 65 6d 70 6f 72  h for the tempor
e7110 61 72 79 20 66 69 6c 65 20 0a 20 20 2a 2a 20 6e  ary file .  ** n
e7120 61 6d 65 2e 20 49 66 20 69 74 20 69 73 20 6e 6f  ame. If it is no
e7130 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  t, return SQLITE
e7140 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69  _ERROR..  */.  i
e7150 66 28 20 28 73 74 72 6c 65 6e 28 7a 44 69 72 29  f( (strlen(zDir)
e7160 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c 49 54 45   + strlen(SQLITE
e7170 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49  _TEMP_FILE_PREFI
e7180 58 29 20 2b 20 31 37 29 20 3e 3d 20 28 73 69 7a  X) + 17) >= (siz
e7190 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20 20 20 20  e_t)nBuf ){.    
e71a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
e71b0 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a  ROR;.  }..  do{.
e71c0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
e71d0 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c 20 7a 42  intf(nBuf-17, zB
e71e0 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 54 45 5f  uf, "%s/"SQLITE_
e71f0 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58  TEMP_FILE_PREFIX
e7200 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 6a 20 3d  , zDir);.    j =
e7210 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 42 75   (int)strlen(zBu
e7220 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  f);.    sqlite3_
e7230 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35 2c 20 26  randomness(15, &
e7240 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20 20 66 6f  zBuf[j]);.    fo
e7250 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20 69 2b 2b  r(i=0; i<15; i++
e7260 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 42  , j++){.      zB
e7270 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43  uf[j] = (char)zC
e7280 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64  hars[ ((unsigned
e7290 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28   char)zBuf[j])%(
e72a0 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31  sizeof(zChars)-1
e72b0 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  ) ];.    }.    z
e72c0 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d 77  Buf[j] = 0;.  }w
e72d0 68 69 6c 65 28 20 61 63 63 65 73 73 28 7a 42 75  hile( access(zBu
e72e0 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 72 65 74  f,0)==0 );.  ret
e72f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
e7300 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
e7310 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e7320 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41  E && defined(__A
e7330 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 52 6f  PPLE__)./*.** Ro
e7340 75 74 69 6e 65 20 74 6f 20 74 72 61 6e 73 66 6f  utine to transfo
e7350 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 20 69 6e  rm a unixFile in
e7360 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f 63 6b 69  to a proxy-locki
e7370 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a 2a 2a 20  ng unixFile..** 
e7380 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  Implementation i
e7390 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c 6f 63 6b  n the proxy-lock
e73a0 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 74 20 75   division, but u
e73b0 73 65 64 20 62 79 20 75 6e 69 78 4f 70 65 6e 28  sed by unixOpen(
e73c0 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 50  ).** if SQLITE_P
e73d0 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b  REFER_PROXY_LOCK
e73e0 49 4e 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a  ING is defined..
e73f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
e7400 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78  oxyTransformUnix
e7410 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 2a 2c 20  File(unixFile*, 
e7420 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65  const char*);.#e
e7430 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  ndif.../*.** Ope
e7440 6e 20 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68  n the file zPath
e7450 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75  ..** .** Previou
e7460 73 6c 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20  sly, the SQLite 
e7470 4f 53 20 6c 61 79 65 72 20 75 73 65 64 20 74 68  OS layer used th
e7480 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e  ree functions in
e7490 20 70 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a   place of this.*
e74a0 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  * one:.**.**    
e74b0 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65   sqlite3OsOpenRe
e74c0 61 64 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20  adWrite();.**   
e74d0 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52    sqlite3OsOpenR
e74e0 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20  eadOnly();.**   
e74f0 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45    sqlite3OsOpenE
e7500 78 63 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a  xclusive();.**.*
e7510 2a 20 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f  * These calls co
e7520 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20  rrespond to the 
e7530 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e  following combin
e7540 61 74 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a  ations of flags:
e7550 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57  .**.**     ReadW
e7560 72 69 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52  rite() ->     (R
e7570 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 41 54  EADWRITE | CREAT
e7580 45 29 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e  E).**     ReadOn
e7590 6c 79 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45  ly()  ->     (RE
e75a0 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20  ADONLY) .**     
e75b0 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20  OpenExclusive() 
e75c0 2d 3e 20 28 52 45 41 44 57 52 49 54 45 20 7c 20  -> (READWRITE | 
e75d0 43 52 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49  CREATE | EXCLUSI
e75e0 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c  VE).**.** The ol
e75f0 64 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28  d OpenExclusive(
e7600 29 20 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f  ) accepted a boo
e7610 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20  lean argument - 
e7620 22 64 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a  "delFlag". If.**
e7630 20 74 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20   true, the file 
e7640 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74  was configured t
e7650 6f 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  o be automatical
e7660 6c 79 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20  ly deleted when 
e7670 74 68 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64  the.** file hand
e7680 6c 65 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63  le closed. To ac
e7690 68 69 65 76 65 20 74 68 65 20 73 61 6d 65 20 65  hieve the same e
e76a0 66 66 65 63 74 20 75 73 69 6e 67 20 74 68 69 73  ffect using this
e76b0 20 6e 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61   new .** interfa
e76c0 63 65 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45  ce, add the DELE
e76d0 54 45 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74  TEONCLOSE flag t
e76e0 6f 20 74 68 6f 73 65 20 73 70 65 63 69 66 69 65  o those specifie
e76f0 64 20 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20  d above for .** 
e7700 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e  OpenExclusive().
e7710 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
e7720 6e 69 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  nixOpen(.  sqlit
e7730 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
e7740 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
e7750 46 53 20 66 6f 72 20 77 68 69 63 68 20 74 68 69  FS for which thi
e7760 73 20 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d  s is the xOpen m
e7770 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  ethod */.  const
e7780 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20   char *zPath,   
e7790 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e          /* Pathn
e77a0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62  ame of file to b
e77b0 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71  e opened */.  sq
e77c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
e77d0 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
e77e0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
e77f0 72 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69  r to be filled i
e7800 6e 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  n */.  int flags
e7810 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e7820 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61      /* Input fla
e7830 67 73 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68  gs to control th
e7840 65 20 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69  e opening */.  i
e7850 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20  nt *pOutFlags   
e7860 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
e7870 75 74 70 75 74 20 66 6c 61 67 73 20 72 65 74 75  utput flags retu
e7880 72 6e 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63  rned to SQLite c
e7890 6f 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ore */.){.  int 
e78a0 66 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  fd = 0;         
e78b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
e78c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72 65  le descriptor re
e78d0 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28 29  turned by open()
e78e0 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 20   */.  int dirfd 
e78f0 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20  = -1;           
e7900 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f 72       /* Director
e7910 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  y file descripto
e7920 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46  r */.  int openF
e7930 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20  lags = 0;       
e7940 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74        /* Flags t
e7950 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 29  o pass to open()
e7960 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 20   */.  int eType 
e7970 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 46  = flags&0xFFFFFF
e7980 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 20  00;  /* Type of 
e7990 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  file to open */.
e79a0 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20    int noLock;   
e79b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e79c0 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74   /* True to omit
e79d0 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69   locking primiti
e79e0 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ves */.  int rc 
e79f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
e7a00 69 6e 74 20 69 73 45 78 63 6c 75 73 69 76 65 20  int isExclusive 
e7a10 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
e7a20 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
e7a30 45 29 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65  E);.  int isDele
e7a40 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20  te     = (flags 
e7a50 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45  & SQLITE_OPEN_DE
e7a60 4c 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20  LETEONCLOSE);.  
e7a70 69 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 20  int isCreate    
e7a80 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
e7a90 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b  TE_OPEN_CREATE);
e7aa0 0a 20 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c  .  int isReadonl
e7ab0 79 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53  y   = (flags & S
e7ac0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
e7ad0 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65  NLY);.  int isRe
e7ae0 61 64 57 72 69 74 65 20 20 3d 20 28 66 6c 61 67  adWrite  = (flag
e7af0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
e7b00 52 45 41 44 57 52 49 54 45 29 3b 0a 0a 20 20 2f  READWRITE);..  /
e7b10 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20  * If creating a 
e7b20 6d 61 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d 66  master or main-f
e7b30 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 69  ile journal, thi
e7b40 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20  s function will 
e7b50 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c 65  open.  ** a file
e7b60 2d 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74  -descriptor on t
e7b70 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f 6f  he directory too
e7b80 2e 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65  . The first time
e7b90 20 75 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a 2a   unixSync().  **
e7ba0 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 64   is called the d
e7bb0 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65  irectory file de
e7bc0 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65  scriptor will be
e7bd0 20 66 73 79 6e 63 28 29 65 64 20 61 6e 64 20 63   fsync()ed and c
e7be0 6c 6f 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20 20  lose()d..  */.  
e7bf0 69 6e 74 20 69 73 4f 70 65 6e 44 69 72 65 63 74  int isOpenDirect
e7c00 6f 72 79 20 3d 20 28 69 73 43 72 65 61 74 65 20  ory = (isCreate 
e7c10 26 26 20 0a 20 20 20 20 20 20 28 65 54 79 70 65  && .      (eType
e7c20 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
e7c30 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20  STER_JOURNAL || 
e7c40 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
e7c50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29  EN_MAIN_JOURNAL)
e7c60 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  .  );..  /* If a
e7c70 72 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73  rgument zPath is
e7c80 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
e7c90 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
e7ca0 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70  s required to op
e7cb0 65 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72  en.  ** a tempor
e7cc0 61 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68  ary file. Use th
e7cd0 69 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f  is buffer to sto
e7ce0 72 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65  re the file name
e7cf0 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72   in..  */.  char
e7d00 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41   zTmpname[MAX_PA
e7d10 54 48 4e 41 4d 45 2b 31 5d 3b 0a 20 20 63 6f 6e  THNAME+1];.  con
e7d20 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  st char *zName =
e7d30 20 7a 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43 68   zPath;..  /* Ch
e7d40 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  eck the followin
e7d50 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  g statements are
e7d60 20 74 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a   true: .  **.  *
e7d70 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c 79 20  *   (a) Exactly 
e7d80 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41 44 57  one of the READW
e7d90 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c  RITE and READONL
e7da0 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20  Y flags must be 
e7db0 73 65 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20  set, and .  **  
e7dc0 20 28 62 29 20 69 66 20 43 52 45 41 54 45 20 69   (b) if CREATE i
e7dd0 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44  s set, then READ
e7de0 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20  WRITE must also 
e7df0 62 65 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a  be set, and.  **
e7e00 20 20 20 28 63 29 20 69 66 20 45 58 43 4c 55 53     (c) if EXCLUS
e7e10 49 56 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e  IVE is set, then
e7e20 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73   CREATE must als
e7e30 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20  o be set..  **  
e7e40 20 28 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e   (d) if DELETEON
e7e50 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68  CLOSE is set, th
e7e60 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61  en CREATE must a
e7e70 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f  lso be set..  */
e7e80 0a 20 20 61 73 73 65 72 74 28 28 69 73 52 65 61  .  assert((isRea
e7e90 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65  donly==0 || isRe
e7ea0 61 64 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28  adWrite==0) && (
e7eb0 69 73 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69  isReadWrite || i
e7ec0 73 52 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61  sReadonly));.  a
e7ed0 73 73 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d  ssert(isCreate==
e7ee0 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65  0 || isReadWrite
e7ef0 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 45 78  );.  assert(isEx
e7f00 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73  clusive==0 || is
e7f10 43 72 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72  Create);.  asser
e7f20 74 28 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c  t(isDelete==0 ||
e7f30 20 69 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f   isCreate);..  /
e7f40 2a 20 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d  * The main DB, m
e7f50 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 61 6e 64  ain journal, and
e7f60 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
e7f70 61 72 65 20 6e 65 76 65 72 20 61 75 74 6f 6d 61  are never automa
e7f80 74 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 64 65 6c  tically.  ** del
e7f90 65 74 65 64 0a 20 20 2a 2f 0a 20 20 61 73 73 65  eted.  */.  asse
e7fa0 72 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49 54  rt( eType!=SQLIT
e7fb0 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c  E_OPEN_MAIN_DB |
e7fc0 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b 0a 20  | !isDelete );. 
e7fd0 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d   assert( eType!=
e7fe0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
e7ff0 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21 69 73 44  _JOURNAL || !isD
e8000 65 6c 65 74 65 20 29 3b 0a 20 20 61 73 73 65 72  elete );.  asser
e8010 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45  t( eType!=SQLITE
e8020 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55  _OPEN_MASTER_JOU
e8030 52 4e 41 4c 20 7c 7c 20 21 69 73 44 65 6c 65 74  RNAL || !isDelet
e8040 65 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72  e );..  /* Asser
e8050 74 20 74 68 61 74 20 74 68 65 20 75 70 70 65 72  t that the upper
e8060 20 6c 61 79 65 72 20 68 61 73 20 73 65 74 20 6f   layer has set o
e8070 6e 65 20 6f 66 20 74 68 65 20 22 66 69 6c 65 2d  ne of the "file-
e8080 74 79 70 65 22 20 66 6c 61 67 73 2e 20 2a 2f 0a  type" flags. */.
e8090 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d    assert( eType=
e80a0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
e80b0 4e 5f 44 42 20 20 20 20 20 20 7c 7c 20 65 54 79  N_DB      || eTy
e80c0 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
e80d0 54 45 4d 50 5f 44 42 20 0a 20 20 20 20 20 20 20  TEMP_DB .       
e80e0 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  || eType==SQLITE
e80f0 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e  _OPEN_MAIN_JOURN
e8100 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c  AL || eType==SQL
e8110 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f  ITE_OPEN_TEMP_JO
e8120 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c  URNAL .       ||
e8130 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
e8140 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20  PEN_SUBJOURNAL  
e8150 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
e8160 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
e8170 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c  URNAL .       ||
e8180 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
e8190 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42  PEN_TRANSIENT_DB
e81a0 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  .  );..  memset(
e81b0 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66  pFile, 0, sizeof
e81c0 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20  (unixFile));..  
e81d0 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20  if( !zName ){.  
e81e0 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74    assert(isDelet
e81f0 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69 72 65  e && !isOpenDire
e8200 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63 20 3d  ctory);.    rc =
e8210 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58   getTempname(MAX
e8220 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a 54 6d  _PATHNAME+1, zTm
e8230 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  pname);.    if( 
e8240 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
e8250 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
e8260 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d  ;.    }.    zNam
e8270 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20  e = zTmpname;.  
e8280 7d 0a 0a 20 20 69 66 28 20 69 73 52 65 61 64 6f  }..  if( isReado
e8290 6e 6c 79 20 29 20 20 6f 70 65 6e 46 6c 61 67 73  nly )  openFlags
e82a0 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20   |= O_RDONLY;.  
e82b0 69 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20  if( isReadWrite 
e82c0 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f  ) openFlags |= O
e82d0 5f 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73 43  _RDWR;.  if( isC
e82e0 72 65 61 74 65 20 29 20 20 20 20 6f 70 65 6e 46  reate )    openF
e82f0 6c 61 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54 3b  lags |= O_CREAT;
e8300 0a 20 20 69 66 28 20 69 73 45 78 63 6c 75 73 69  .  if( isExclusi
e8310 76 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c  ve ) openFlags |
e8320 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f  = (O_EXCL|O_NOFO
e8330 4c 4c 4f 57 29 3b 0a 20 20 6f 70 65 6e 46 6c 61  LLOW);.  openFla
e8340 67 73 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 49  gs |= (O_LARGEFI
e8350 4c 45 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a 20  LE|O_BINARY);.. 
e8360 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65   fd = open(zName
e8370 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 69 73 44  , openFlags, isD
e8380 65 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54  elete?0600:SQLIT
e8390 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50  E_DEFAULT_FILE_P
e83a0 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 4f  ERMISSIONS);.  O
e83b0 53 54 52 41 43 45 34 28 22 4f 50 45 4e 58 20 20  STRACE4("OPENX  
e83c0 20 25 2d 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c   %-3d %s 0%o\n",
e83d0 20 66 64 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e   fd, zName, open
e83e0 46 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 66 64  Flags);.  if( fd
e83f0 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45 49 53  <0 && errno!=EIS
e8400 44 49 52 20 26 26 20 69 73 52 65 61 64 57 72 69  DIR && isReadWri
e8410 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 73 69  te && !isExclusi
e8420 76 65 20 29 7b 0a 20 20 20 20 2f 2a 20 46 61 69  ve ){.    /* Fai
e8430 6c 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  led to open the 
e8440 66 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72  file for read/wr
e8450 69 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20  ite access. Try 
e8460 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20  read-only. */.  
e8470 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 51 4c    flags &= ~(SQL
e8480 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
e8490 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  TE|SQLITE_OPEN_C
e84a0 52 45 41 54 45 29 3b 0a 20 20 20 20 66 6c 61 67  REATE);.    flag
e84b0 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e  s |= SQLITE_OPEN
e84c0 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 72  _READONLY;.    r
e84d0 65 74 75 72 6e 20 75 6e 69 78 4f 70 65 6e 28 70  eturn unixOpen(p
e84e0 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c  Vfs, zPath, pFil
e84f0 65 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c  e, flags, pOutFl
e8500 61 67 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ags);.  }.  if( 
e8510 66 64 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75  fd<0 ){.    retu
e8520 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  rn SQLITE_CANTOP
e8530 45 4e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73  EN;.  }.  if( is
e8540 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f 53  Delete ){.#if OS
e8550 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50 61  _VXWORKS.    zPa
e8560 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c 73  th = zName;.#els
e8570 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61  e.    unlink(zNa
e8580 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  me);.#endif.  }.
e8590 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
e85a0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
e85b0 20 20 65 6c 73 65 7b 0a 20 20 20 20 28 28 75 6e    else{.    ((un
e85c0 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 29 2d 3e  ixFile*)pFile)->
e85d0 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e  openFlags = open
e85e0 46 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69  Flags;.  }.#endi
e85f0 66 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67  f.  if( pOutFlag
e8600 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c  s ){.    *pOutFl
e8610 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d  ags = flags;.  }
e8620 0a 0a 20 20 61 73 73 65 72 74 28 66 64 21 3d 30  ..  assert(fd!=0
e8630 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44  );.  if( isOpenD
e8640 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
e8650 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f  rc = openDirecto
e8660 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64  ry(zPath, &dirfd
e8670 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
e8680 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e8690 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20    close(fd); /* 
e86a0 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66  silently leak if
e86b0 20 66 61 69 6c 2c 20 61 6c 72 65 61 64 79 20 69   fail, already i
e86c0 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20  n error */.     
e86d0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
e86e0 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44  }.  }..#ifdef FD
e86f0 5f 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c  _CLOEXEC.  fcntl
e8700 28 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63  (fd, F_SETFD, fc
e8710 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c  ntl(fd, F_GETFD,
e8720 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43   0) | FD_CLOEXEC
e8730 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c  );.#endif..  noL
e8740 6f 63 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c  ock = eType!=SQL
e8750 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
e8760 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52  ;..#if SQLITE_PR
e8770 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49  EFER_PROXY_LOCKI
e8780 4e 47 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d  NG.  if( zPath!=
e8790 4e 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20  NULL && !noLock 
e87a0 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 65 6e 76  ){.    char *env
e87b0 66 6f 72 63 65 20 3d 20 67 65 74 65 6e 76 28 22  force = getenv("
e87c0 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f  SQLITE_FORCE_PRO
e87d0 58 59 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20  XY_LOCKING");.  
e87e0 20 20 69 6e 74 20 75 73 65 50 72 6f 78 79 20 3d    int useProxy =
e87f0 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49   0;..    /* SQLI
e8800 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c  TE_FORCE_PROXY_L
e8810 4f 43 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20  OCKING==1 means 
e8820 66 6f 72 63 65 20 61 6c 77 61 79 73 20 75 73 65  force always use
e8830 20 70 72 6f 78 79 2c 20 0a 20 20 20 20 2a 2a 20   proxy, .    ** 
e8840 30 20 6d 65 61 6e 73 20 6e 65 76 65 72 20 75 73  0 means never us
e8850 65 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65  e proxy, NULL me
e8860 61 6e 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f  ans use proxy fo
e8870 72 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65  r non-local file
e8880 73 20 6f 6e 6c 79 0a 20 20 20 20 2a 2f 0a 20 20  s only.    */.  
e8890 20 20 69 66 28 20 65 6e 76 66 6f 72 63 65 21 3d    if( envforce!=
e88a0 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 75 73  NULL ){.      us
e88b0 65 50 72 6f 78 79 20 3d 20 61 74 6f 69 28 65 6e  eProxy = atoi(en
e88c0 76 66 6f 72 63 65 29 3e 30 3b 0a 20 20 20 20 7d  vforce)>0;.    }
e88d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 72 75  else{.      stru
e88e0 63 74 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f  ct statfs fsInfo
e88f0 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 73 74 61  ;..      if( sta
e8900 74 66 73 28 7a 50 61 74 68 2c 20 26 66 73 49 6e  tfs(zPath, &fsIn
e8910 66 6f 29 20 3d 3d 20 2d 31 20 29 7b 0a 09 09 09  fo) == -1 ){....
e8920 09 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69  .((unixFile*)pFi
e8930 6c 65 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  le)->lastErrno =
e8940 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
e8950 69 66 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63  if( dirfd>=0 ) c
e8960 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a 20  lose(dirfd); /* 
e8970 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66  silently leak if
e8980 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72 20   fail, in error 
e8990 2a 2f 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65  */.        close
e89a0 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c  (fd); /* silentl
e89b0 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20  y leak if fail, 
e89c0 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  in error */.    
e89d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e89e0 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 3b 0a  E_IOERR_ACCESS;.
e89f0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 73        }.      us
e8a00 65 50 72 6f 78 79 20 3d 20 21 28 66 73 49 6e 66  eProxy = !(fsInf
e8a10 6f 2e 66 5f 66 6c 61 67 73 26 4d 4e 54 5f 4c 4f  o.f_flags&MNT_LO
e8a20 43 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  CAL);.    }.    
e8a30 69 66 28 20 75 73 65 50 72 6f 78 79 20 29 7b 0a  if( useProxy ){.
e8a40 20 20 20 20 20 20 72 63 20 3d 20 66 69 6c 6c 49        rc = fillI
e8a50 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20  nUnixFile(pVfs, 
e8a60 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65  fd, dirfd, pFile
e8a70 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c  , zPath, noLock,
e8a80 20 69 73 44 65 6c 65 74 65 29 3b 0a 20 20 20 20   isDelete);.    
e8a90 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
e8aa0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
e8ab0 63 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 66 6f  c = proxyTransfo
e8ac0 72 6d 55 6e 69 78 46 69 6c 65 28 28 75 6e 69 78  rmUnixFile((unix
e8ad0 46 69 6c 65 2a 29 70 46 69 6c 65 2c 20 22 3a 61  File*)pFile, ":a
e8ae0 75 74 6f 3a 22 29 3b 0a 20 20 20 20 20 20 7d 0a  uto:");.      }.
e8af0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
e8b00 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
e8b10 66 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 66 69  f.  .  return fi
e8b20 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 66  llInUnixFile(pVf
e8b30 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 70 46  s, fd, dirfd, pF
e8b40 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f  ile, zPath, noLo
e8b50 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a 7d  ck, isDelete);.}
e8b60 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74  ../*.** Delete t
e8b70 68 65 20 66 69 6c 65 20 61 74 20 7a 50 61 74 68  he file at zPath
e8b80 2e 20 49 66 20 74 68 65 20 64 69 72 53 79 6e 63  . If the dirSync
e8b90 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75   argument is tru
e8ba0 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a 20 74 68  e, fsync().** th
e8bb0 65 20 64 69 72 65 63 74 6f 72 79 20 61 66 74 65  e directory afte
e8bc0 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 66  r deleting the f
e8bd0 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
e8be0 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 28 0a 20  nt unixDelete(. 
e8bf0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f   sqlite3_vfs *No
e8c00 74 55 73 65 64 2c 20 20 20 20 20 2f 2a 20 56 46  tUsed,     /* VF
e8c10 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69  S containing thi
e8c20 73 20 61 73 20 74 68 65 20 78 44 65 6c 65 74 65  s as the xDelete
e8c30 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e   method */.  con
e8c40 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
e8c50 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
e8c60 66 20 66 69 6c 65 20 74 6f 20 62 65 20 64 65 6c  f file to be del
e8c70 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69  eted */.  int di
e8c80 72 53 79 6e 63 20 20 20 20 20 20 20 20 20 20 20  rSync           
e8c90 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20      /* If true, 
e8ca0 66 73 79 6e 63 28 29 20 64 69 72 65 63 74 6f 72  fsync() director
e8cb0 79 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67  y after deleting
e8cc0 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   file */.){.  in
e8cd0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
e8ce0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
e8cf0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
e8d00 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
e8d10 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49  (return SQLITE_I
e8d20 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20  OERR_DELETE);.  
e8d30 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b 0a 23  unlink(zPath);.#
e8d40 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49  ifndef SQLITE_DI
e8d50 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a 20 20  SABLE_DIRSYNC.  
e8d60 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b 0a 20  if( dirSync ){. 
e8d70 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20 20 72     int fd;.    r
e8d80 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f 72  c = openDirector
e8d90 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b 0a 20  y(zPath, &fd);. 
e8da0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
e8db0 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f 53 5f 56  E_OK ){.#if OS_V
e8dc0 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69 66 28  XWORKS.      if(
e8dd0 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31 20 29   fsync(fd)==-1 )
e8de0 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69 66 28  .#else.      if(
e8df0 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23 65 6e   fsync(fd) ).#en
e8e00 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20  dif.      {.    
e8e10 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e8e20 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 3b  IOERR_DIR_FSYNC;
e8e30 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
e8e40 66 28 20 63 6c 6f 73 65 28 66 64 29 26 26 21 72  f( close(fd)&&!r
e8e50 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  c ){.        rc 
e8e60 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  = SQLITE_IOERR_D
e8e70 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20  IR_CLOSE;.      
e8e80 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  }.    }.  }.#end
e8e90 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  if.  return rc;.
e8ea0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 68  }../*.** Test th
e8eb0 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 6f  e existance of o
e8ec0 72 20 61 63 63 65 73 73 20 70 65 72 6d 69 73 73  r access permiss
e8ed0 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a 50 61  ions of file zPa
e8ee0 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73 74 20  th. The.** test 
e8ef0 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65 6e 64  performed depend
e8f00 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  s on the value o
e8f10 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20  f flags:.**.**  
e8f20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53     SQLITE_ACCESS
e8f30 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72 6e 20  _EXISTS: Return 
e8f40 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 65 78  1 if the file ex
e8f50 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51 4c 49  ists.**     SQLI
e8f60 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
e8f70 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20 69 66  ITE: Return 1 if
e8f80 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 61   the file is rea
e8f90 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2e 0a  d and writable..
e8fa0 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43  **     SQLITE_AC
e8fb0 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a 20 52  CESS_READONLY: R
e8fc0 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 66  eturn 1 if the f
e8fd0 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e  ile is readable.
e8fe0 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
e8ff0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74   return 0..*/.st
e9000 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41 63 63  atic int unixAcc
e9010 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ess(.  sqlite3_v
e9020 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20 2f  fs *NotUsed,   /
e9030 2a 20 54 68 65 20 56 46 53 20 63 6f 6e 74 61 69  * The VFS contai
e9040 6e 69 6e 67 20 74 68 69 73 20 78 41 63 63 65 73  ning this xAcces
e9050 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f  s method */.  co
e9060 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c  nst char *zPath,
e9070 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20 6f 66        /* Path of
e9080 20 74 68 65 20 66 69 6c 65 20 74 6f 20 65 78 61   the file to exa
e9090 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  mine */.  int fl
e90a0 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
e90b0 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 77 65 20    /* What do we 
e90c0 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e 20 61 62  want to learn ab
e90d0 6f 75 74 20 74 68 65 20 7a 50 61 74 68 20 66 69  out the zPath fi
e90e0 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52  le? */.  int *pR
e90f0 65 73 4f 75 74 20 20 20 20 20 20 20 20 20 20 20  esOut           
e9100 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74   /* Write result
e9110 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 20 2a 2f   boolean here */
e9120 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64 65 20  .){.  int amode 
e9130 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  = 0;.  UNUSED_PA
e9140 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e9150 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
e9160 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
e9170 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 3b  TE_IOERR_ACCESS;
e9180 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c   );.  switch( fl
e9190 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ags ){.    case 
e91a0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
e91b0 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d 6f 64  ISTS:.      amod
e91c0 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20 20 20  e = F_OK;.      
e91d0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
e91e0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45  SQLITE_ACCESS_RE
e91f0 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 61  ADWRITE:.      a
e9200 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b  mode = W_OK|R_OK
e9210 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
e9220 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41     case SQLITE_A
e9230 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20  CCESS_READ:.    
e9240 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b 3b 0a    amode = R_OK;.
e9250 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20        break;..  
e9260 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
e9270 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 6c 69   assert(!"Invali
e9280 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74  d flags argument
e9290 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f  ");.  }.  *pResO
e92a0 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a 50 61  ut = (access(zPa
e92b0 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29 3b 0a  th, amode)==0);.
e92c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e92d0 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75  OK;.}.../*.** Tu
e92e0 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61  rn a relative pa
e92f0 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75  thname into a fu
e9300 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54 68 65  ll pathname. The
e9310 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 0a 2a   relative path.*
e9320 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 61  * is stored as a
e9330 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   nul-terminated 
e9340 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 62 75  string in the bu
e9350 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20  ffer pointed to 
e9360 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a 2a 2a  by.** zPath. .**
e9370 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74 73 20  .** zOut points 
e9380 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 61  to a buffer of a
e9390 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65 33 5f  t least sqlite3_
e93a0 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 20 62  vfs.mxPathname b
e93b0 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74 68 69  ytes .** (in thi
e93c0 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41 54 48  s case, MAX_PATH
e93d0 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54 68 65  NAME bytes). The
e93e0 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20 77 72   full-path is wr
e93f0 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68 69 73  itten to.** this
e9400 20 62 75 66 66 65 72 20 62 65 66 6f 72 65 20 72   buffer before r
e9410 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61  eturning..*/.sta
e9420 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75 6c 6c  tic int unixFull
e9430 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69  Pathname(.  sqli
e9440 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20  te3_vfs *pVfs,  
e9450 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
e9460 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65  nter to vfs obje
e9470 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ct */.  const ch
e9480 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20  ar *zPath,      
e9490 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c        /* Possibl
e94a0 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74  y relative input
e94b0 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e   path */.  int n
e94c0 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  Out,            
e94d0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
e94e0 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 65   of output buffe
e94f0 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  r in bytes */.  
e9500 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20 20 20  char *zOut      
e9510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e9520 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a   Output buffer *
e9530 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27 73 20  /.){..  /* It's 
e9540 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20  odd to simulate 
e9550 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 72 65  an io-error here
e9560 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 68 69  , but really thi
e9570 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a 20 75  s is just.  ** u
e9580 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 72 6f  sing the io-erro
e9590 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 72 65  r infrastructure
e95a0 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 53 51   to test that SQ
e95b0 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 68 69  Lite handles thi
e95c0 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  s.  ** function 
e95d0 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20 66 75  failing. This fu
e95e0 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66 61 69  nction could fai
e95f0 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d 70 6c  l if, for exampl
e9600 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72  e, the.  ** curr
e9610 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69 72 65  ent working dire
e9620 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e 20 75  ctory has been u
e9630 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a 20 20  nlinked..  */.  
e9640 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
e9650 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
e9660 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73 65 72  RROR );..  asser
e9670 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  t( pVfs->mxPathn
e9680 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e 41 4d  ame==MAX_PATHNAM
e9690 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  E );.  UNUSED_PA
e96a0 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 0a  RAMETER(pVfs);..
e96b0 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d 20 3d    zOut[nOut-1] =
e96c0 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 7a 50 61   '\0';.  if( zPa
e96d0 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20  th[0]=='/' ){.  
e96e0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
e96f0 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 2c 20 22  tf(nOut, zOut, "
e9700 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 20 7d  %s", zPath);.  }
e9710 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 43  else{.    int nC
e9720 77 64 3b 0a 20 20 20 20 69 66 28 20 67 65 74 63  wd;.    if( getc
e9730 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d 31 29  wd(zOut, nOut-1)
e9740 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
e9750 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  urn SQLITE_CANTO
e9760 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  PEN;.    }.    n
e9770 43 77 64 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  Cwd = (int)strle
e9780 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c  n(zOut);.    sql
e9790 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f  ite3_snprintf(nO
e97a0 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74 5b 6e  ut-nCwd, &zOut[n
e97b0 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a 50 61  Cwd], "/%s", zPa
e97c0 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  th);.  }.  retur
e97d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
e97e0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
e97f0 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
e9800 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66  ION./*.** Interf
e9810 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67  aces for opening
e9820 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72   a shared librar
e9830 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79  y, finding entry
e9840 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69   points.** withi
e9850 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62  n the shared lib
e9860 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e  rary, and closin
e9870 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62  g the shared lib
e9880 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64  rary..*/.#includ
e9890 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 74 61 74  e <dlfcn.h>.stat
e98a0 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 44 6c 4f  ic void *unixDlO
e98b0 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20  pen(sqlite3_vfs 
e98c0 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e 73 74 20  *NotUsed, const 
e98d0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
e98e0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
e98f0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
e9900 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 6e 28 7a   return dlopen(z
e9910 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c 44 5f 4e  Filename, RTLD_N
e9920 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f 42 41 4c  OW | RTLD_GLOBAL
e9930 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69  );.}../*.** SQLi
e9940 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75  te calls this fu
e9950 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 61 74 65  nction immediate
e9960 6c 79 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20  ly after a call 
e9970 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 29 20 6f  to unixDlSym() o
e9980 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 65 6e 28  r.** unixDlOpen(
e9990 29 20 66 61 69 6c 73 20 28 72 65 74 75 72 6e 73  ) fails (returns
e99a0 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 29   a null pointer)
e99b0 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 65 74 61  . If a more deta
e99c0 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a 20 6d 65  iled error.** me
e99d0 73 73 61 67 65 20 69 73 20 61 76 61 69 6c 61 62  ssage is availab
e99e0 6c 65 2c 20 69 74 20 69 73 20 77 72 69 74 74 65  le, it is writte
e99f0 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e 20 49 66  n to zBufOut. If
e9a00 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61 67   no error messag
e9a10 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61 62 6c  e.** is availabl
e9a20 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 20 6c 65  e, zBufOut is le
e9a30 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 20 61 6e  ft unmodified an
e9a40 64 20 53 51 4c 69 74 65 20 75 73 65 73 20 61 20  d SQLite uses a 
e9a50 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 72 6f 72  default.** error
e9a60 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73 74 61   message..*/.sta
e9a70 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 45  tic void unixDlE
e9a80 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73  rror(sqlite3_vfs
e9a90 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e   *NotUsed, int n
e9aa0 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f  Buf, char *zBufO
e9ab0 75 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 45 72  ut){.  char *zEr
e9ac0 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  r;.  UNUSED_PARA
e9ad0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
e9ae0 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
e9af0 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64 6c 65  ();.  zErr = dle
e9b00 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 7a 45  rror();.  if( zE
e9b10 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rr ){.    sqlite
e9b20 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c  3_snprintf(nBuf,
e9b30 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22 2c 20   zBufOut, "%s", 
e9b40 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75 6e 69  zErr);.  }.  uni
e9b50 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d  xLeaveMutex();.}
e9b60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28 2a 75  .static void (*u
e9b70 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 65 33  nixDlSym(sqlite3
e9b80 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76  _vfs *NotUsed, v
e9b90 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  oid *p, const ch
e9ba0 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b  ar*zSym))(void){
e9bb0 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43 43 20  .  /* .  ** GCC 
e9bc0 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65  with -pedantic-e
e9bd0 72 72 6f 72 73 20 73 61 79 73 20 74 68 61 74 20  rrors says that 
e9be0 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c  C90 does not all
e9bf0 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20 62 65  ow a void* to be
e9c00 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74 6f 20  .  ** cast into 
e9c10 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
e9c20 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20 79 65  unction.  And ye
e9c30 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 64 6c  t the library dl
e9c40 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a 20 20  sym() routine.  
e9c50 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 6f 69  ** returns a voi
e9c60 64 2a 20 77 68 69 63 68 20 69 73 20 72 65 61 6c  d* which is real
e9c70 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ly a pointer to 
e9c80 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f 20  a function.  So 
e9c90 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a 20 75  how do we.  ** u
e9ca0 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74 68 20  se dlsym() with 
e9cb0 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 73  -pedantic-errors
e9cc0 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69  ?.  **.  ** Vari
e9cd0 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69 73 20  able x below is 
e9ce0 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 61 20  defined to be a 
e9cf0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
e9d00 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20 20 2a  ction taking.  *
e9d10 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76 6f 69  * parameters voi
e9d20 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63 68 61  d* and const cha
e9d30 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67  r* and returning
e9d40 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
e9d50 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 57  function..  ** W
e9d60 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78 20 62  e initialize x b
e9d70 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74 20 61  y assigning it a
e9d80 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
e9d90 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69 6f 6e  dlsym() function
e9da0 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61 73 73  ..  ** (That ass
e9db0 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 73  ignment requires
e9dc0 20 61 20 63 61 73 74 2e 29 20 20 54 68 65 6e 20   a cast.)  Then 
e9dd0 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75 6e 63  we call the func
e9de0 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a 20 78  tion that.  ** x
e9df0 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a 20 20   points to.  .  
e9e00 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77 6f 72  **.  ** This wor
e9e10 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e 6c 69  k-around is unli
e9e20 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63 6f 72  kely to work cor
e9e30 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20 73 79  rectly on any sy
e9e40 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a 2a 20  stem where.  ** 
e9e50 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e 6e 6f  you really canno
e9e60 74 20 63 61 73 74 20 61 20 66 75 6e 63 74 69 6f  t cast a functio
e9e70 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 76  n pointer into v
e9e80 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65 6e 2c  oid*.  But then,
e9e90 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68   on the.  ** oth
e9ea0 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d 28 29  er hand, dlsym()
e9eb0 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 6f   will not work o
e9ec0 6e 20 73 75 63 68 20 61 20 73 79 73 74 65 6d 20  n such a system 
e9ed0 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20 68 61  either, so we ha
e9ee0 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 61 6c  ve.  ** not real
e9ef0 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69 6e 67  ly lost anything
e9f00 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ..  */.  void (*
e9f10 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  (*x)(void*,const
e9f20 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 3b 0a   char*))(void);.
e9f30 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
e9f40 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 78  ER(NotUsed);.  x
e9f50 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28 76 6f   = (void(*(*)(vo
e9f60 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  id*,const char*)
e9f70 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b 0a 20  )(void))dlsym;. 
e9f80 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70 2c 20   return (*x)(p, 
e9f90 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69 63 20  zSym);.}.static 
e9fa0 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f 73 65  void unixDlClose
e9fb0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f  (sqlite3_vfs *No
e9fc0 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 48 61  tUsed, void *pHa
e9fd0 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f  ndle){.  UNUSED_
e9fe0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e9ff0 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 70 48  d);.  dlclose(pH
ea000 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20  andle);.}.#else 
ea010 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49  /* if SQLITE_OMI
ea020 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
ea030 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a   is defined: */.
ea040 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c    #define unixDl
ea050 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e  Open  0.  #defin
ea060 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 30 0a  e unixDlError 0.
ea070 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c    #define unixDl
ea080 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 69 6e  Sym   0.  #defin
ea090 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 30 0a  e unixDlClose 0.
ea0a0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72  #endif../*.** Wr
ea0b0 69 74 65 20 6e 42 75 66 20 62 79 74 65 73 20 6f  ite nBuf bytes o
ea0c0 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 74 6f  f random data to
ea0d0 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 75   the supplied bu
ea0e0 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74  ffer zBuf..*/.st
ea0f0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 61 6e  atic int unixRan
ea100 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f  domness(sqlite3_
ea110 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  vfs *NotUsed, in
ea120 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
ea130 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  uf){.  UNUSED_PA
ea140 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
ea150 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69 7a 65  ;.  assert((size
ea160 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 6f 66  _t)nBuf>=(sizeof
ea170 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f 66 28  (time_t)+sizeof(
ea180 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 57 65  int)));..  /* We
ea190 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69 61 6c   have to initial
ea1a0 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 65 76  ize zBuf to prev
ea1b0 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 72 6f  ent valgrind fro
ea1c0 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 2a 2a  m reporting.  **
ea1d0 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 72 65   errors.  The re
ea1e0 70 6f 72 74 73 20 69 73 73 75 65 64 20 62 79 20  ports issued by 
ea1f0 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69 6e 63  valgrind are inc
ea200 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f 75 6c  orrect - we woul
ea210 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 74 68  d.  ** prefer th
ea220 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73  at the randomnes
ea230 73 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 62  s be increased b
ea240 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f 66 20  y making use of 
ea250 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 74 69  the.  ** uniniti
ea260 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69 6e 20  alized space in 
ea270 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c 67 72  zBuf - but valgr
ea280 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e 64 20  ind errors tend 
ea290 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 73 6f  to worry.  ** so
ea2a0 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74 68 65  me users.  Rathe
ea2b0 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20 69 74  r than argue, it
ea2c0 20 73 65 65 6d 73 20 65 61 73 69 65 72 20 6a 75   seems easier ju
ea2d0 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  st to initialize
ea2e0 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c 65 20  .  ** the whole 
ea2f0 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65 6e 63  array and silenc
ea300 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 65 6e  e valgrind, even
ea310 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 6c   if that means l
ea320 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 0a 20  ess randomness. 
ea330 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e 64 6f   ** in the rando
ea340 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a  m seed..  **.  *
ea350 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20  * When testing, 
ea360 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a 42 75  initializing zBu
ea370 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 20 61  f[] to zero is a
ea380 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 74 20  ll we do.  That 
ea390 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 74 20  means.  ** that 
ea3a0 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 68  we always use th
ea3b0 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 6e 75  e same random nu
ea3c0 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e 20 20  mber sequence.  
ea3d0 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 0a 20  This makes the. 
ea3e0 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65 61 74   ** tests repeat
ea3f0 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d  able..  */.  mem
ea400 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75  set(zBuf, 0, nBu
ea410 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64  f);.#if !defined
ea420 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20  (SQLITE_TEST).  
ea430 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c 20 66  {.    int pid, f
ea440 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e  d;.    fd = open
ea450 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c  ("/dev/urandom",
ea460 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 20 20   O_RDONLY);.    
ea470 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20  if( fd<0 ){.    
ea480 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20    time_t t;.    
ea490 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20    time(&t);.    
ea4a0 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26    memcpy(zBuf, &
ea4b0 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20  t, sizeof(t));. 
ea4c0 20 20 20 20 20 70 69 64 20 3d 20 67 65 74 70 69       pid = getpi
ea4d0 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  d();.      memcp
ea4e0 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 28 74  y(&zBuf[sizeof(t
ea4f0 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66  )], &pid, sizeof
ea500 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20 61 73  (pid));.      as
ea510 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74 29 2b  sert( sizeof(t)+
ea520 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28 73 69  sizeof(pid)<=(si
ea530 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20 20 20  ze_t)nBuf );.   
ea540 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65 6f 66     nBuf = sizeof
ea550 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70 69 64  (t) + sizeof(pid
ea560 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
ea570 20 20 20 20 6e 42 75 66 20 3d 20 72 65 61 64 28      nBuf = read(
ea580 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66 29 3b  fd, zBuf, nBuf);
ea590 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29  .      close(fd)
ea5a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
ea5b0 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 42 75 66  if.  return nBuf
ea5c0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65  ;.}.../*.** Slee
ea5d0 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77  p for a little w
ea5e0 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68  hile.  Return th
ea5f0 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65  e amount of time
ea600 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61   slept..** The a
ea610 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e  rgument is the n
ea620 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65  umber of microse
ea630 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f  conds we want to
ea640 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72   sleep..** The r
ea650 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74  eturn value is t
ea660 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63  he number of mic
ea670 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65  roseconds of sle
ea680 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72  ep actually.** r
ea690 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68  equested from th
ea6a0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65  e underlying ope
ea6b0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61  rating system, a
ea6c0 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a   number which.**
ea6d0 20 6d 69 67 68 74 20 62 65 20 67 72 65 61 74 65   might be greate
ea6e0 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
ea6f0 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c  to the argument,
ea700 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a   but not less.**
ea710 20 74 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65   than the argume
ea720 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nt..*/.static in
ea730 74 20 75 6e 69 78 53 6c 65 65 70 28 73 71 6c 69  t unixSleep(sqli
ea740 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
ea750 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e  , int microsecon
ea760 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f  ds){.#if OS_VXWO
ea770 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d  RKS.  struct tim
ea780 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 73 70 2e  espec sp;..  sp.
ea790 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f 73 65  tv_sec = microse
ea7a0 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 30 30 3b  conds / 1000000;
ea7b0 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 20 3d 20  .  sp.tv_nsec = 
ea7c0 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 25 20  (microseconds % 
ea7d0 31 30 30 30 30 30 30 29 20 2a 20 31 30 30 30 3b  1000000) * 1000;
ea7e0 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26 73 70  .  nanosleep(&sp
ea7f0 2c 20 4e 55 4c 4c 29 3b 0a 20 20 72 65 74 75 72  , NULL);.  retur
ea800 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a  n microseconds;.
ea810 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41  #elif defined(HA
ea820 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41  VE_USLEEP) && HA
ea830 56 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65  VE_USLEEP.  usle
ea840 65 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29  ep(microseconds)
ea850 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f  ;.  return micro
ea860 73 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20  seconds;.#else. 
ea870 20 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20 28   int seconds = (
ea880 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39  microseconds+999
ea890 39 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20 20  999)/1000000;.  
ea8a0 73 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a  sleep(seconds);.
ea8b0 20 20 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 73    return seconds
ea8c0 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66  *1000000;.#endif
ea8d0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ea8e0 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 7d 0a  TER(NotUsed);.}.
ea8f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
ea900 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69  wing variable, i
ea910 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a  f set to a non-z
ea920 65 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69 6e  ero value, is in
ea930 74 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a 20  terpreted as.** 
ea940 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65  the number of se
ea950 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30  conds since 1970
ea960 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20   and is used to 
ea970 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 6f  set the result o
ea980 66 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43 75  f.** sqlite3OsCu
ea990 72 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72 69  rrentTime() duri
ea9a0 6e 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23  ng testing..*/.#
ea9b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
ea9c0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
ea9d0 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
ea9e0 5f 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46  _time = 0;  /* F
ea9f0 61 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65 20  ake system time 
eaa00 69 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65  in seconds since
eaa10 20 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66   1970. */.#endif
eaa20 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
eaa30 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69   current time (i
eaa40 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72  n Universal Coor
eaa50 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20  dinated Time).  
eaa60 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72  Write the.** cur
eaa70 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61  rent time and da
eaa80 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44  te as a Julian D
eaa90 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a  ay number into *
eaaa0 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74  prNow and.** ret
eaab0 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31  urn 0.  Return 1
eaac0 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64   if the time and
eaad0 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20   date cannot be 
eaae0 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  found..*/.static
eaaf0 20 69 6e 74 20 75 6e 69 78 43 75 72 72 65 6e 74   int unixCurrent
eab00 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73  Time(sqlite3_vfs
eab10 20 2a 4e 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c   *NotUsed, doubl
eab20 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 20 64  e *prNow){.#if d
eab30 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54 4f 44  efined(NO_GETTOD
eab40 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20  ).  time_t t;.  
eab50 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e  time(&t);.  *prN
eab60 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e 30 20 2b  ow = t/86400.0 +
eab70 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 65 6c 69   2440587.5;.#eli
eab80 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73  f OS_VXWORKS.  s
eab90 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 20 73  truct timespec s
eaba0 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f 67 65 74  Now;.  clock_get
eabb0 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 41 4c 54  time(CLOCK_REALT
eabc0 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 2a  IME, &sNow);.  *
eabd0 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37 2e  prNow = 2440587.
eabe0 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63 2f  5 + sNow.tv_sec/
eabf0 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e 74  86400.0 + sNow.t
eac00 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 30 30 30  v_nsec/864000000
eac10 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 65 0a 20  00000.0;.#else. 
eac20 20 73 74 72 75 63 74 20 74 69 6d 65 76 61 6c 20   struct timeval 
eac30 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 6d 65 6f  sNow;.  gettimeo
eac40 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 29 3b 0a  fday(&sNow, 0);.
eac50 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35    *prNow = 24405
eac60 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73  87.5 + sNow.tv_s
eac70 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f  ec/86400.0 + sNo
eac80 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 30 30 30  w.tv_usec/864000
eac90 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 69 66 0a  00000.0;.#endif.
eaca0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
eacb0 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  EST.  if( sqlite
eacc0 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29  3_current_time )
eacd0 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73  {.    *prNow = s
eace0 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
eacf0 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34  ime/86400.0 + 24
ead00 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e  40587.5;.  }.#en
ead10 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  dif.  UNUSED_PAR
ead20 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
ead30 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
ead40 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 64 20 74  /*.** We added t
ead50 68 65 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72  he xGetLastError
ead60 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20 74  () method with t
ead70 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 6f 66 20  he intention of 
ead80 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 62 65 74  providing.** bet
ead90 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c 20 65 72  ter low-level er
eada0 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 68 65  ror messages whe
eadb0 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74  n operating-syst
eadc0 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 6f 6d 65  em problems come
eadd0 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 20 53 51   up.** during SQ
eade0 4c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20  Lite operation. 
eadf0 20 42 75 74 20 73 6f 20 66 61 72 2c 20 6e 6f 6e   But so far, non
eae00 65 20 6f 66 20 74 68 61 74 20 68 61 73 20 62 65  e of that has be
eae10 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a  en implemented.*
eae20 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 2e 20 20  * in the core.  
eae30 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  So this routine 
eae40 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e  is never called.
eae50 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 20 69 73    For now, it is
eae60 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 70 6c 61   merely.** a pla
eae70 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f 0a 73 74  ce-holder..*/.st
eae80 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47 65 74  atic int unixGet
eae90 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65  LastError(sqlite
eaea0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
eaeb0 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c 20 63 68  int NotUsed2, ch
eaec0 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 7b 0a 20  ar *NotUsed3){. 
eaed0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
eaee0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 55 4e  R(NotUsed);.  UN
eaef0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
eaf00 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 4e 55 53  otUsed2);.  UNUS
eaf10 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
eaf20 55 73 65 64 33 29 3b 0a 20 20 72 65 74 75 72 6e  Used3);.  return
eaf30 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a   0;.}../*.******
eaf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaf50 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65  ** End of sqlite
eaf60 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a  3_vfs methods **
eaf70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaf80 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
eaf90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eafa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eafb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eafc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eafd0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a  ********/../****
eafe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
eb030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb040 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 72 6f 78  ***** Begin Prox
eb050 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a  y Locking ******
eb060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
eb080 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69   Proxy locking i
eb090 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 6b 69 6e  s a "uber-lockin
eb0a0 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 74 68 69  g-method" in thi
eb0b0 73 20 73 65 6e 73 65 3a 20 20 49 74 20 75 73 65  s sense:  It use
eb0c0 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 72 20 6c  s the.** other l
eb0d0 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f  ocking methods o
eb0e0 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c 6f 63 6b  n secondary lock
eb0f0 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 79 20 6c   files.  Proxy l
eb100 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a 2a 20 6d  ocking is a.** m
eb110 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 72 20 74  eta-layer over t
eb120 6f 70 20 6f 66 20 74 68 65 20 70 72 69 6d 69 74  op of the primit
eb130 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c  ive locking impl
eb140 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 2e 20 20  emented above.  
eb150 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 65 61 73  For.** this reas
eb160 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f 6e  on, the division
eb170 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
eb180 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e   of proxy lockin
eb190 67 20 69 73 20 64 65 66 65 72 72 65 64 0a 2a 2a  g is deferred.**
eb1a0 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 6e 20 74   until late in t
eb1b0 68 65 20 66 69 6c 65 20 28 68 65 72 65 29 20 61  he file (here) a
eb1c0 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20  fter all of the 
eb1d0 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 68 6f 64  other I/O method
eb1e0 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 64  s have.** been d
eb1f0 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 68 61 74  efined - so that
eb200 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 6c   the primitive l
eb210 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 61  ocking methods a
eb220 72 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20  re available.** 
eb230 61 73 20 73 65 72 76 69 63 65 73 20 74 6f 20 68  as services to h
eb240 65 6c 70 20 77 69 74 68 20 74 68 65 20 69 6d 70  elp with the imp
eb250 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70  lementation of p
eb260 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a  roxy locking..**
eb270 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  .****.**.** The 
eb280 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20  default locking 
eb290 73 63 68 65 6d 65 73 20 69 6e 20 53 51 4c 69 74  schemes in SQLit
eb2a0 65 20 75 73 65 20 62 79 74 65 2d 72 61 6e 67 65  e use byte-range
eb2b0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 0a 2a 2a   locks on the.**
eb2c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
eb2d0 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 73 61 66  o coordinate saf
eb2e0 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 61 63  e, concurrent ac
eb2f0 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70 6c 65  cess by multiple
eb300 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 6e 64 20   readers.** and 
eb310 77 72 69 74 65 72 73 20 5b 68 74 74 70 3a 2f 2f  writers [http://
eb320 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f 63 6b 69  sqlite.org/locki
eb330 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 54 68 65  ngv3.html].  The
eb340 20 66 69 76 65 20 66 69 6c 65 20 6c 6f 63 6b 69   five file locki
eb350 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 28 55 4e  ng.** states (UN
eb360 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 4e 47 2c  LOCKED, PENDING,
eb370 20 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45   SHARED, RESERVE
eb380 44 2c 20 45 58 43 4c 55 53 49 56 45 29 20 61 72  D, EXCLUSIVE) ar
eb390 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a  e implemented.**
eb3a0 20 61 73 20 50 4f 53 49 58 20 72 65 61 64 20 26   as POSIX read &
eb3b0 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 6f 76 65   write locks ove
eb3c0 72 20 66 69 78 65 64 20 73 65 74 20 6f 66 20 6c  r fixed set of l
eb3d0 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 20 66 73  ocations (via fs
eb3e0 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 46 50 20  ctl),.** on AFP 
eb3f0 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 65 78 63  and SMB only exc
eb400 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61 6e 67  lusive byte-rang
eb410 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 76 61 69  e locks are avai
eb420 6c 61 62 6c 65 20 76 69 61 20 66 73 63 74 6c 0a  lable via fsctl.
eb430 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 28 27 7a  ** with _IOWR('z
eb440 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 42 79  ', 23, struct By
eb450 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 29 20  teRangeLockPB2) 
eb460 74 6f 20 74 72 61 63 6b 20 74 68 65 20 73 61 6d  to track the sam
eb470 65 20 35 20 73 74 61 74 65 73 2e 0a 2a 2a 20 54  e 5 states..** T
eb480 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 46 5f 52  o simulate a F_R
eb490 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 68 61 72  DLCK on the shar
eb4a0 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 41 46 50  ed range, on AFP
eb4b0 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 65 6c 65   a randomly sele
eb4c0 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 73 73 20  cted.** address 
eb4d0 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 72 61  in the shared ra
eb4e0 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66 6f 72  nge is taken for
eb4f0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20   a SHARED lock, 
eb500 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 73 68  the entire.** sh
eb510 61 72 65 64 20 72 61 6e 67 65 20 69 73 20 74 61  ared range is ta
eb520 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 43 4c 55  ken for an EXCLU
eb530 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a 2a 0a 2a  SIVE lock):.**.*
eb540 2a 20 20 20 20 20 20 50 45 4e 44 49 4e 47 5f 42  *      PENDING_B
eb550 59 54 45 20 20 20 20 20 20 20 20 30 78 34 30 30  YTE        0x400
eb560 30 30 30 30 30 09 09 20 20 20 09 0a 2a 2a 20 20  00000..   ..**  
eb570 20 20 20 20 52 45 53 45 52 56 45 44 5f 42 59 54      RESERVED_BYT
eb580 45 20 20 20 20 20 20 20 30 78 34 30 30 30 30 30  E       0x400000
eb590 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45  01.**      SHARE
eb5a0 44 5f 52 41 4e 47 45 20 20 20 20 20 20 20 20 30  D_RANGE        0
eb5b0 78 34 30 30 30 30 30 30 32 20 2d 3e 20 30 78 34  x40000002 -> 0x4
eb5c0 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20 54 68  0000200.**.** Th
eb5d0 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c 20 6f 6e  is works well on
eb5e0 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65 20   the local file 
eb5f0 73 79 73 74 65 6d 2c 20 62 75 74 20 73 68 6f 77  system, but show
eb600 73 20 61 20 6e 65 61 72 6c 79 20 31 30 30 78 0a  s a nearly 100x.
eb610 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 6e 20 72  ** slowdown in r
eb620 65 61 64 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ead performance 
eb630 6f 6e 20 41 46 50 20 62 65 63 61 75 73 65 20 74  on AFP because t
eb640 68 65 20 41 46 50 20 63 6c 69 65 6e 74 20 64 69  he AFP client di
eb650 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 20 72 65  sables.** the re
eb660 61 64 20 63 61 63 68 65 20 77 68 65 6e 20 62 79  ad cache when by
eb670 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61  te-range locks a
eb680 72 65 20 70 72 65 73 65 6e 74 2e 20 20 45 6e 61  re present.  Ena
eb690 62 6c 69 6e 67 20 74 68 65 20 72 65 61 64 0a 2a  bling the read.*
eb6a0 2a 20 63 61 63 68 65 20 65 78 70 6f 73 65 73 20  * cache exposes 
eb6b0 61 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63  a cache coherenc
eb6c0 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 74 20 69  y problem that i
eb6d0 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 61 6c 6c  s present on all
eb6e0 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 6f 72 74   OS X.** support
eb6f0 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 20  ed network file 
eb700 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 20 61 6e  systems.  NFS an
eb710 64 20 41 46 50 20 62 6f 74 68 20 6f 62 73 65 72  d AFP both obser
eb720 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f 73 65 2d  ve the.** close-
eb730 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e 74 69 63  to-open semantic
eb740 73 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 63  s for ensuring c
eb750 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 0a 2a  ache coherency.*
eb760 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e 73 6f  * [http://nfs.so
eb770 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 2f 23 66  urceforge.net/#f
eb780 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 20 64 6f  aq_a8], which do
eb790 65 73 20 6e 6f 74 20 65 66 66 65 63 74 69 76 65  es not effective
eb7a0 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 68  ly.** address th
eb7b0 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 20 66  e requirements f
eb7c0 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 20 64 61  or concurrent da
eb7d0 74 61 62 61 73 65 20 61 63 63 65 73 73 20 62 79  tabase access by
eb7e0 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 72 65 61   multiple.** rea
eb7f0 64 65 72 73 20 61 6e 64 20 77 72 69 74 65 72 73  ders and writers
eb800 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 77 77 2e  .** [http://www.
eb810 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c 69 74  nabble.com/SQLit
eb820 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 65 2d 63  e-on-NFS-cache-c
eb830 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 36 35 35  oherency-td15655
eb840 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a  701.html]..**.**
eb850 20 54 6f 20 61 64 64 72 65 73 73 20 74 68 65 20   To address the 
eb860 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 6e 64 20  performance and 
eb870 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20  cache coherency 
eb880 69 73 73 75 65 73 2c 20 70 72 6f 78 79 20 66 69  issues, proxy fi
eb890 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 63 68  le locking.** ch
eb8a0 61 6e 67 65 73 20 74 68 65 20 77 61 79 20 64 61  anges the way da
eb8b0 74 61 62 61 73 65 20 61 63 63 65 73 73 20 69 73  tabase access is
eb8c0 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 6c   controlled by l
eb8d0 69 6d 69 74 69 6e 67 20 61 63 63 65 73 73 20 74  imiting access t
eb8e0 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 68 6f  o a.** single ho
eb8f0 73 74 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64  st at a time and
eb900 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 6c 6f 63   moving file loc
eb910 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 20 64 61  ks off of the da
eb920 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61  tabase file.** a
eb930 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f 78 79 20  nd onto a proxy 
eb940 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61  file on the loca
eb950 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 20  l file system.  
eb960 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e 67 20  .**.**.** Using 
eb970 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a 20 2d  proxy locks.** -
eb980 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb990 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 0a 2a 2a  .**.** C APIs.**
eb9a0 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  .**  sqlite3_fil
eb9b0 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62  e_control(db, db
eb9c0 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 53 45 54  name, SQLITE_SET
eb9d0 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 0a  _LOCKPROXYFILE,.
eb9e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
eb9f0 20 20 20 20 20 20 20 20 20 3c 70 72 6f 78 79 5f           <proxy_
eba00 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 6f 3a 22  path> | ":auto:"
eba10 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66  );.**  sqlite3_f
eba20 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20  ile_control(db, 
eba30 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 47  dbname, SQLITE_G
eba40 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45  ET_LOCKPROXYFILE
eba50 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 68 3e 29  , &<proxy_path>)
eba60 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 70  ;.**.**.** SQL p
eba70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20 50 52  ragmas.**.**  PR
eba80 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
eba90 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 3d  lock_proxy_file=
ebaa0 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 3a  <proxy_path> | :
ebab0 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 47 4d 41  auto:.**  PRAGMA
ebac0 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b   [database.]lock
ebad0 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a 0a 2a  _proxy_file.**.*
ebae0 2a 20 53 70 65 63 69 66 79 69 6e 67 20 22 3a 61  * Specifying ":a
ebaf0 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 68 61 74  uto:" means that
ebb00 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 63   if there is a c
ebb10 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 68 20 61  onch file with a
ebb20 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 68 6f 73   matching.** hos
ebb30 74 20 49 44 20 69 6e 20 69 74 2c 20 74 68 65 20  t ID in it, the 
ebb40 70 72 6f 78 79 20 70 61 74 68 20 69 6e 20 74 68  proxy path in th
ebb50 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 6c  e conch file wil
ebb60 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 68 65 72  l be used, other
ebb70 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f 78 79 20  wise.** a proxy 
ebb80 70 61 74 68 20 62 61 73 65 64 20 6f 6e 20 74 68  path based on th
ebb90 65 20 75 73 65 72 27 73 20 74 65 6d 70 20 64 69  e user's temp di
ebba0 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e 66 73 74  r.** (via confst
ebbb0 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45  r(_CS_DARWIN_USE
ebbc0 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e 29 29  R_TEMP_DIR,...))
ebbd0 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 61 6e   will be used an
ebbe0 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 6c 20  d the.** actual 
ebbf0 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 6d 65 20  proxy file name 
ebc00 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f  is generated fro
ebc10 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 70  m the name and p
ebc20 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  ath of the.** da
ebc30 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 46 6f  tabase file.  Fo
ebc40 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  r example:.**.**
ebc50 20 20 20 20 20 20 20 46 6f 72 20 64 61 74 61 62         For datab
ebc60 61 73 65 20 70 61 74 68 20 22 2f 55 73 65 72 73  ase path "/Users
ebc70 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a 2a 20  /me/foo.db" .** 
ebc80 20 20 20 20 20 20 54 68 65 20 6c 6f 63 6b 20 70        The lock p
ebc90 61 74 68 20 77 69 6c 6c 20 62 65 20 22 3c 74 6d  ath will be "<tm
ebca0 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 6c 6f 63  pdir>/sqliteploc
ebcb0 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f 66 6f 6f  ks/_Users_me_foo
ebcc0 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a 0a 2a  .db:auto:").**.*
ebcd0 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b 20 70 72  * Once a lock pr
ebce0 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 75 72 65  oxy is configure
ebcf0 64 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65  d for a database
ebd00 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 74 20   connection, it 
ebd10 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20 72 65  can not.** be re
ebd20 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 72 20 69  moved, however i
ebd30 74 20 6d 61 79 20 62 65 20 73 77 69 74 63 68 65  t may be switche
ebd40 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74  d to a different
ebd50 20 70 72 6f 78 79 20 70 61 74 68 20 76 69 61 0a   proxy path via.
ebd60 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 41 50 49  ** the above API
ebd70 73 20 28 61 73 73 75 6d 69 6e 67 20 74 68 65 20  s (assuming the 
ebd80 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 20 6e 6f  conch file is no
ebd90 74 20 62 65 69 6e 67 20 68 65 6c 64 20 62 79 20  t being held by 
ebda0 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e 6e 65  another.** conne
ebdb0 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 65 73 73  ction or process
ebdc0 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f 77  ). .**.**.** How
ebdd0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 77   proxy locking w
ebde0 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d  orks.** --------
ebdf0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
ebe00 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 69 6c 65  **.** Proxy file
ebe10 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 65 73 20   locking relies 
ebe20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 74 77 6f  primarily on two
ebe30 20 6e 65 77 20 73 75 70 70 6f 72 74 69 6e 67 20   new supporting 
ebe40 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 20  files: .**.**   
ebe50 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 20 74 6f  *  conch file to
ebe60 20 6c 69 6d 69 74 20 61 63 63 65 73 73 20 74 6f   limit access to
ebe70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
ebe80 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 68  le to a single h
ebe90 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 74 20 61  ost.**      at a
ebea0 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20 2a 20   time.**.**   * 
ebeb0 20 70 72 6f 78 79 20 66 69 6c 65 20 74 6f 20 61   proxy file to a
ebec0 63 74 20 61 73 20 61 20 70 72 6f 78 79 20 66 6f  ct as a proxy fo
ebed0 72 20 74 68 65 20 61 64 76 69 73 6f 72 79 20 6c  r the advisory l
ebee0 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a  ocks normally.**
ebef0 20 20 20 20 20 20 74 61 6b 65 6e 20 6f 6e 20 74        taken on t
ebf00 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 0a 2a  he database.**.*
ebf10 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  * The conch file
ebf20 20 2d 20 74 6f 20 75 73 65 20 61 20 70 72 6f 78   - to use a prox
ebf30 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 65 20 6d  y file, sqlite m
ebf40 75 73 74 20 66 69 72 73 74 20 22 68 6f 6c 64 20  ust first "hold 
ebf50 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a 20 62 79  the conch".** by
ebf60 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 6c 69 74   taking an sqlit
ebf70 65 2d 73 74 79 6c 65 20 73 68 61 72 65 64 20 6c  e-style shared l
ebf80 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 68  ock on the conch
ebf90 20 66 69 6c 65 2c 20 72 65 61 64 69 6e 67 20 74   file, reading t
ebfa0 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 61  he.** contents a
ebfb0 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65  nd comparing the
ebfc0 20 68 6f 73 74 27 73 20 75 6e 69 71 75 65 20 68   host's unique h
ebfd0 6f 73 74 20 49 44 20 28 73 65 65 20 62 65 6c 6f  ost ID (see belo
ebfe0 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 70  w) and lock.** p
ebff0 72 6f 78 79 20 70 61 74 68 20 61 67 61 69 6e 73  roxy path agains
ec000 74 20 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f  t the values sto
ec010 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e 63 68  red in the conch
ec020 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 66 69 6c  .  The conch fil
ec030 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69  e is.** stored i
ec040 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63  n the same direc
ec050 74 6f 72 79 20 61 73 20 74 68 65 20 64 61 74 61  tory as the data
ec060 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68  base file and th
ec070 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 69  e file name.** i
ec080 73 20 70 61 74 74 65 72 6e 65 64 20 61 66 74 65  s patterned afte
ec090 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  r the database f
ec0a0 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 2e 3c 64  ile name as ".<d
ec0b0 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d 63 6f 6e  atabasename>-con
ec0c0 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63  ch"..** If the c
ec0d0 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 73 20 6e  onch file does n
ec0e0 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 69 74 27  ot exist, or it'
ec0f0 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f 20 6e 6f  s contents do no
ec100 74 20 6d 61 74 63 68 20 74 68 65 0a 2a 2a 20 68  t match the.** h
ec110 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 20 70 72  ost ID and/or pr
ec120 6f 78 79 20 70 61 74 68 2c 20 74 68 65 6e 20 74  oxy path, then t
ec130 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 63 61 6c  he lock is escal
ec140 61 74 65 64 20 74 6f 20 61 6e 20 65 78 63 6c 75  ated to an exclu
ec150 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61 6e 64  sive.** lock and
ec160 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
ec170 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 70 64 61  contents is upda
ec180 74 65 64 20 77 69 74 68 20 74 68 65 20 68 6f 73  ted with the hos
ec190 74 20 49 44 20 61 6e 64 20 70 72 6f 78 79 0a 2a  t ID and proxy.*
ec1a0 2a 20 70 61 74 68 20 61 6e 64 20 74 68 65 20 6c  * path and the l
ec1b0 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 61 64 65  ock is downgrade
ec1c0 64 20 74 6f 20 61 20 73 68 61 72 65 64 20 6c 6f  d to a shared lo
ec1d0 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 20 74 68  ck again.  If th
ec1e0 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 20 68 65  e conch.** is he
ec1f0 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 70 72  ld by another pr
ec200 6f 63 65 73 73 20 28 77 69 74 68 20 61 20 73 68  ocess (with a sh
ec210 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 68 65 20  ared lock), the 
ec220 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a  exclusive lock.*
ec230 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20  * will fail and 
ec240 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
ec250 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
ec260 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 2d 20  he proxy file - 
ec270 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 66 69  a single-byte fi
ec280 6c 65 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20  le used for all 
ec290 61 64 76 69 73 6f 72 79 20 66 69 6c 65 20 6c 6f  advisory file lo
ec2a0 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20  cks.** normally 
ec2b0 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 61 74  taken on the dat
ec2c0 61 62 61 73 65 20 66 69 6c 65 2e 20 20 20 54 68  abase file.   Th
ec2d0 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 20 73 61  is allows for sa
ec2e0 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 6f 66  fe sharing.** of
ec2f0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
ec300 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 20  le for multiple 
ec310 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74  readers and writ
ec320 65 72 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a  ers on the same.
ec330 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 63 6f 6e  ** host (the con
ec340 63 68 20 65 6e 73 75 72 65 73 20 74 68 61 74 20  ch ensures that 
ec350 74 68 65 79 20 61 6c 6c 20 75 73 65 20 74 68 65  they all use the
ec360 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b   same local lock
ec370 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20 54 68   file)..**.** Th
ec380 65 72 65 20 69 73 20 61 20 74 68 69 72 64 20 66  ere is a third f
ec390 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 74 20 49  ile - the host I
ec3a0 44 20 66 69 6c 65 20 2d 20 75 73 65 64 20 61 73  D file - used as
ec3b0 20 61 20 70 65 72 73 69 73 74 65 6e 74 20 72 65   a persistent re
ec3c0 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 75 6e 69  cord.** of a uni
ec3d0 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20 66  que identifier f
ec3e0 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 61 20 31  or the host, a 1
ec3f0 32 38 2d 62 79 74 65 20 75 6e 69 71 75 65 20 68  28-byte unique h
ec400 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a 20 69  ost id file.** i
ec410 6e 20 74 68 65 20 70 61 74 68 20 64 65 66 69 6e  n the path defin
ec420 65 64 20 62 79 20 74 68 65 20 48 4f 53 54 49 44  ed by the HOSTID
ec430 50 41 54 48 20 6d 61 63 72 6f 20 28 64 65 66 61  PATH macro (defa
ec440 75 6c 74 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20  ult value is.** 
ec450 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65 73 2f  /Library/Caches/
ec460 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69 74  .com.apple.sqlit
ec470 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 2e 0a 2a  eConchHostId)..*
ec480 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 6e 67 20  *.** Requesting 
ec490 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 79 20 64  the lock proxy d
ec4a0 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 69 61 74  oes not immediat
ec4b0 65 6c 79 20 74 61 6b 65 20 74 68 65 20 63 6f 6e  ely take the con
ec4c0 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 6f 6e 6c  ch, it is.** onl
ec4d0 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 68 65  y taken when the
ec4e0 20 66 69 72 73 74 20 72 65 71 75 65 73 74 20 74   first request t
ec4f0 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 73 65 20  o lock database 
ec500 66 69 6c 65 20 69 73 20 6d 61 64 65 2e 20 20 0a  file is made.  .
ec510 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 65 73 20  ** This matches 
ec520 74 68 65 20 73 65 6d 61 6e 74 69 63 73 20 6f 66  the semantics of
ec530 20 74 68 65 20 74 72 61 64 69 74 69 6f 6e 61 6c   the traditional
ec540 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 76 69 6f   locking behavio
ec550 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f 70 65 6e  r, where.** open
ec560 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e  ing a connection
ec570 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66   to a database f
ec580 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 74 61 6b  ile does not tak
ec590 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 74 2e 0a  e a lock on it..
ec5a0 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 6c 6f  ** The shared lo
ec5b0 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 6e 20 66  ck and an open f
ec5c0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 61  ile descriptor a
ec5d0 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 20 75 6e  re maintained un
ec5e0 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e  til .** the conn
ec5f0 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 61  ection to the da
ec600 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64  tabase is closed
ec610 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f  . .**.** The pro
ec620 78 79 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20  xy file and the 
ec630 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 20 6e 65  lock file are ne
ec640 76 65 72 20 64 65 6c 65 74 65 64 20 73 6f 20 74  ver deleted so t
ec650 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 0a 2a 2a  hey only need.**
ec660 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 74   to be created t
ec670 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
ec680 65 79 20 61 72 65 20 75 73 65 64 2e 0a 2a 2a 0a  ey are used..**.
ec690 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ** Configuration
ec6a0 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d 2d   options.** ----
ec6b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ec6c0 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 45 5f  -.**.**  SQLITE_
ec6d0 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43  PREFER_PROXY_LOC
ec6e0 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  KING.**.**      
ec6f0 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   Database files 
ec700 61 63 63 65 73 73 65 64 20 6f 6e 20 6e 6f 6e 2d  accessed on non-
ec710 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65  local file syste
ec720 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20  ms are.**       
ec730 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6f  automatically co
ec740 6e 66 69 67 75 72 65 64 20 66 6f 72 20 70 72 6f  nfigured for pro
ec750 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c 6f 63 6b  xy locking, lock
ec760 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a 20 20 20   files are.**   
ec770 20 20 20 20 6e 61 6d 65 64 20 61 75 74 6f 6d 61      named automa
ec780 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 20 74 68  tically using th
ec790 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 61 73 0a  e same logic as.
ec7a0 2a 2a 20 20 20 20 20 20 20 50 52 41 47 4d 41 20  **       PRAGMA 
ec7b0 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 3d  lock_proxy_file=
ec7c0 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 20 20 0a  ":auto:".**    .
ec7d0 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 4f 58 59  **  SQLITE_PROXY
ec7e0 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 20 20 20  _DEBUG.**.**    
ec7f0 20 20 20 45 6e 61 62 6c 65 73 20 74 68 65 20 6c     Enables the l
ec800 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 6f 72 20  ogging of error 
ec810 6d 65 73 73 61 67 65 73 20 64 75 72 69 6e 67 20  messages during 
ec820 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a 20  host id file.** 
ec830 20 20 20 20 20 20 72 65 74 72 69 65 76 61 6c 20        retrieval 
ec840 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a 2a 2a 0a  and creation.**.
ec850 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 48 0a 2a  **  HOSTIDPATH.*
ec860 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65 72 72  *.**       Overr
ec870 69 64 65 73 20 74 68 65 20 64 65 66 61 75 6c 74  ides the default
ec880 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 70 61   host ID file pa
ec890 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 0a 2a  th location.**.*
ec8a0 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 0a  *  LOCKPROXYDIR.
ec8b0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65 72  **.**       Over
ec8c0 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75 6c  rides the defaul
ec8d0 74 20 64 69 72 65 63 74 6f 72 79 20 75 73 65 64  t directory used
ec8e0 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f 78 79 20   for lock proxy 
ec8f0 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a 20 20 20  files that.**   
ec900 20 20 20 20 61 72 65 20 6e 61 6d 65 64 20 61 75      are named au
ec910 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 69 61 20  tomatically via 
ec920 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 73 65 74  the ":auto:" set
ec930 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49  ting.**.**  SQLI
ec940 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59  TE_DEFAULT_PROXY
ec950 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a  DIR_PERMISSIONS.
ec960 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 65 72 6d  **.**       Perm
ec970 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 65 20 77  issions to use w
ec980 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 64  hen creating a d
ec990 69 72 65 63 74 6f 72 79 20 66 6f 72 20 73 74 6f  irectory for sto
ec9a0 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20 20 20  ring the.**     
ec9b0 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c    lock proxy fil
ec9c0 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 20 77 68  es, only used wh
ec9d0 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 20  en LOCKPROXYDIR 
ec9e0 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a 2a 20 20  is not set..**  
ec9f0 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 41 73 20    .**    .** As 
eca00 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f 76 65 2c  mentioned above,
eca10 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20 77   when compiled w
eca20 69 74 68 20 53 51 4c 49 54 45 5f 50 52 45 46 45  ith SQLITE_PREFE
eca30 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 2c  R_PROXY_LOCKING,
eca40 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 68 65 20  .** setting the 
eca50 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 72 69  environment vari
eca60 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 4f 52 43  able SQLITE_FORC
eca70 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20  E_PROXY_LOCKING 
eca80 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 66 6f 72  to 1 will.** for
eca90 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  ce proxy locking
ecaa0 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20   to be used for 
ecab0 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20 66  every database f
ecac0 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 6e 64 20  ile opened, and 
ecad0 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 63 65 20  0.** will force 
ecae0 61 75 74 6f 6d 61 74 69 63 20 70 72 6f 78 79 20  automatic proxy 
ecaf0 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 64 69  locking to be di
ecb00 73 61 62 6c 65 64 20 66 6f 72 20 61 6c 6c 20 64  sabled for all d
ecb10 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 73  atabase.** files
ecb20 20 28 65 78 70 6c 69 63 69 74 79 20 63 61 6c 6c   (explicity call
ecb30 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 53  ing the SQLITE_S
ecb40 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45  ET_LOCKPROXYFILE
ecb50 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a 20 73 71   pragma or.** sq
ecb60 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  lite_file_contro
ecb70 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 61 66 66  l API is not aff
ecb80 65 63 74 65 64 20 62 79 20 53 51 4c 49 54 45 5f  ected by SQLITE_
ecb90 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b  FORCE_PROXY_LOCK
ecba0 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ING)..*/../*.** 
ecbb0 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73  Proxy locking is
ecbc0 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
ecbd0 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f 0a 23 69  on MacOSX .*/.#i
ecbe0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  f defined(__APPL
ecbf0 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45  E__) && SQLITE_E
ecc00 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
ecc10 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  YLE..#ifdef SQLI
ecc20 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 6d 75 6c  TE_TEST./* simul
ecc30 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 68 6f 73  ate multiple hos
ecc40 74 73 20 62 79 20 63 72 65 61 74 69 6e 67 20 75  ts by creating u
ecc50 6e 69 71 75 65 20 68 6f 73 74 69 64 20 66 69 6c  nique hostid fil
ecc60 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 4c 49 54  e paths */.SQLIT
ecc70 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
ecc80 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d 20 30  3_hostid_num = 0
ecc90 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
ecca0 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67  The proxyLocking
eccb0 43 6f 6e 74 65 78 74 20 68 61 73 20 74 68 65 20  Context has the 
eccc0 70 61 74 68 20 61 6e 64 20 66 69 6c 65 20 73 74  path and file st
eccd0 72 75 63 74 75 72 65 73 20 66 6f 72 20 74 68 65  ructures for the
ecce0 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 6e 64 20   remote .** and 
eccf0 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 69 6c 65  local proxy file
ecd00 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 70 65 64  s in it.*/.typed
ecd10 65 66 20 73 74 72 75 63 74 20 70 72 6f 78 79 4c  ef struct proxyL
ecd20 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 70 72  ockingContext pr
ecd30 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
ecd40 74 3b 0a 73 74 72 75 63 74 20 70 72 6f 78 79 4c  t;.struct proxyL
ecd50 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a  ockingContext {.
ecd60 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63    unixFile *conc
ecd70 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 2f  hFile;         /
ecd80 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 66 69 6c  * Open conch fil
ecd90 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63 6f 6e  e */.  char *con
ecda0 63 68 46 69 6c 65 50 61 74 68 3b 20 20 20 20 20  chFilePath;     
ecdb0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
ecdc0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f  he conch file */
ecdd0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63  .  unixFile *loc
ecde0 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 20 20 20  kProxy;         
ecdf0 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 20 6c 6f  /* Open proxy lo
ece00 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61  ck file */.  cha
ece10 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  r *lockProxyPath
ece20 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ;         /* Nam
ece30 65 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20 6c  e of the proxy l
ece40 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68  ock file */.  ch
ece50 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 20 20  ar *dbPath;     
ece60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
ece70 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66  me of the open f
ece80 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e  ile */.  int con
ece90 63 68 48 65 6c 64 3b 20 20 20 20 20 20 20 20 20  chHeld;         
ecea0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
eceb0 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 63 75   the conch is cu
ecec0 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 2a 2f 0a  rrently held */.
eced0 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f 63 6b 69    void *oldLocki
ecee0 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 2f  ngContext;     /
ecef0 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b 69  * Original locki
ecf00 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73  ngcontext to res
ecf10 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f  tore on close */
ecf20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  .  sqlite3_io_me
ecf30 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4f 6c  thods const *pOl
ecf40 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 2f 2a 20  dMethod;     /* 
ecf50 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 6d 65 74  Original I/O met
ecf60 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 65 20 2a  hods for close *
ecf70 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 49 44 4c  /.};../* HOSTIDL
ecf80 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c 45 4e 20  EN and CONCHLEN 
ecf90 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 73 70 61  both include spa
ecfa0 63 65 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e  ce for the strin
ecfb0 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 74 69 6e  g .** terminatin
ecfc0 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 66 69 6e  g nul .*/.#defin
ecfd0 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 20 20 20  e HOSTIDLEN     
ecfe0 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20      128.#define 
ecff0 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 20 20 20  CONCHLEN        
ed000 20 20 28 4d 41 58 50 41 54 48 4c 45 4e 2b 48 4f    (MAXPATHLEN+HO
ed010 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 66 6e 64  STIDLEN+1).#ifnd
ed020 65 66 20 48 4f 53 54 49 44 50 41 54 48 0a 23 20  ef HOSTIDPATH.# 
ed030 64 65 66 69 6e 65 20 48 4f 53 54 49 44 50 41 54  define HOSTIDPAT
ed040 48 20 20 20 20 20 20 20 22 2f 4c 69 62 72 61 72  H       "/Librar
ed050 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e 61 70  y/Caches/.com.ap
ed060 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63 68 48  ple.sqliteConchH
ed070 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 0a 0a 2f  ostId".#endif../
ed080 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 20 63 6f  * basically a co
ed090 70 79 20 6f 66 20 75 6e 69 78 52 61 6e 64 6f 6d  py of unixRandom
ed0a0 6e 65 73 73 20 77 69 74 68 20 64 69 66 66 65 72  ness with differ
ed0b0 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 65 68 61  ent.** test beha
ed0c0 76 69 6f 72 20 62 75 69 6c 74 20 69 6e 20 2a 2f  vior built in */
ed0d0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
ed0e0 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28  yGenerateHostID(
ed0f0 63 68 61 72 20 2a 70 48 6f 73 74 49 44 29 7b 0a  char *pHostID){.
ed100 20 20 69 6e 74 20 70 69 64 2c 20 66 64 2c 20 6c    int pid, fd, l
ed110 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  en;.  unsigned c
ed120 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 6e 73 69  har *key = (unsi
ed130 67 6e 65 64 20 63 68 61 72 20 2a 29 70 48 6f 73  gned char *)pHos
ed140 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d 73 65 74  tID;.  .  memset
ed150 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 49 44 4c  (key, 0, HOSTIDL
ed160 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 3b 0a  EN);.  len = 0;.
ed170 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65    fd = open("/de
ed180 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44  v/urandom", O_RD
ed190 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 66 64 3e  ONLY);.  if( fd>
ed1a0 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20  =0 ){.    len = 
ed1b0 72 65 61 64 28 66 64 2c 20 6b 65 79 2c 20 48 4f  read(fd, key, HO
ed1c0 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20 63 6c  STIDLEN);.    cl
ed1d0 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65  ose(fd); /* sile
ed1e0 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 66 64  ntly leak the fd
ed1f0 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a   if it fails */.
ed200 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e 20 3c 20    }.  if( len < 
ed210 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20  HOSTIDLEN ){.   
ed220 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20 74   time_t t;.    t
ed230 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 6d 65 6d  ime(&t);.    mem
ed240 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 73 69 7a  cpy(key, &t, siz
ed250 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 70 69 64  eof(t));.    pid
ed260 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 20 20   = getpid();.   
ed270 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b 73 69 7a   memcpy(&key[siz
ed280 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 73  eof(t)], &pid, s
ed290 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 7d  izeof(pid));.  }
ed2a0 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 4b 45 5f  .  .#ifdef MAKE_
ed2b0 50 52 45 54 54 59 5f 48 4f 53 54 49 44 0a 20 20  PRETTY_HOSTID.  
ed2c0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
ed2d0 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 65 20 62   /* filter the b
ed2e0 79 74 65 73 20 69 6e 74 6f 20 70 72 69 6e 74 61  ytes into printa
ed2f0 62 6c 65 20 61 73 63 69 69 20 63 68 61 72 61 63  ble ascii charac
ed300 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 74 65 72  ters and NUL ter
ed310 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 20 6b 65  minate */.    ke
ed320 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d 31 29 5d  y[(HOSTIDLEN-1)]
ed330 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 66 6f 72   = 0x00;.    for
ed340 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 54 49 44  ( i=0; i<(HOSTID
ed350 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 7b 0a 20  LEN-1); i++ ){. 
ed360 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68       unsigned ch
ed370 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 5d 26 30  ar pa = key[i]&0
ed380 78 37 46 3b 0a 20 20 20 20 20 20 69 66 28 20 70  x7F;.      if( p
ed390 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 20 20 20  a<0x20 ){.      
ed3a0 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65 79 5b    key[i] = (key[
ed3b0 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38 30 29  i]&0x80 == 0x80)
ed3c0 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 70 61 2b   ? pa+0x40 : pa+
ed3d0 30 78 32 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73  0x20;.      }els
ed3e0 65 20 69 66 28 20 70 61 3d 3d 30 78 37 46 20 29  e if( pa==0x7F )
ed3f0 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b 69 5d  {.        key[i]
ed400 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38 30 20   = (key[i]&0x80 
ed410 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 3d 30 78  == 0x80) ? pa=0x
ed420 32 30 20 3a 20 70 61 2b 30 78 37 45 3b 0a 20 20  20 : pa+0x7E;.  
ed430 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
ed440 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
ed450 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
ed460 20 77 72 69 74 65 73 20 74 68 65 20 68 6f 73 74   writes the host
ed470 20 69 64 20 70 61 74 68 20 74 6f 20 70 61 74 68   id path to path
ed480 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 20 62 65  , path should be
ed490 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 61 74 65   an pre-allocate
ed4a0 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 69 74 68  d buffer.** with
ed4b0 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 66 6f   enough space fo
ed4c0 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a 73 74 61  r a path .*/.sta
ed4d0 74 69 63 20 76 6f 69 64 20 70 72 6f 78 79 47 65  tic void proxyGe
ed4e0 74 48 6f 73 74 49 44 50 61 74 68 28 63 68 61 72  tHostIDPath(char
ed4f0 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f 74 20 6c   *path, size_t l
ed500 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 79 28 70  en){.  strlcpy(p
ed510 61 74 68 2c 20 48 4f 53 54 49 44 50 41 54 48 2c  ath, HOSTIDPATH,
ed520 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 20 53 51   len);.#ifdef SQ
ed530 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20  LITE_TEST.  if( 
ed540 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e  sqlite3_hostid_n
ed550 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 68 61 72  um>0 ){.    char
ed560 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 22 31 22   suffix[2] = "1"
ed570 3b 0a 20 20 20 20 73 75 66 66 69 78 5b 30 5d 20  ;.    suffix[0] 
ed580 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 20 73 71  = suffix[0] + sq
ed590 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d  lite3_hostid_num
ed5a0 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 28 70 61  ;.    strlcat(pa
ed5b0 74 68 2c 20 73 75 66 66 69 78 2c 20 6c 65 6e 29  th, suffix, len)
ed5c0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 4f  ;.  }.#endif.  O
ed5d0 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53 54  STRACE3("GETHOST
ed5e0 49 44 50 41 54 48 20 20 25 73 20 70 69 64 3d 25  IDPATH  %s pid=%
ed5f0 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 65 74 70  d\n", path, getp
ed600 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 67 65 74  id());.}../* get
ed610 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 72 6f   the host ID fro
ed620 6d 20 61 20 73 71 6c 69 74 65 20 68 6f 73 74 69  m a sqlite hosti
ed630 64 20 66 69 6c 65 20 73 74 6f 72 65 64 20 69 6e  d file stored in
ed640 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 2d 73 70   the .** user-sp
ed650 65 63 69 66 69 63 20 74 6d 70 20 64 69 72 65 63  ecific tmp direc
ed660 74 6f 72 79 2c 20 63 72 65 61 74 65 20 74 68 65  tory, create the
ed670 20 49 44 20 69 66 20 69 74 27 73 20 6e 6f 74 20   ID if it's not 
ed680 74 68 65 72 65 20 61 6c 72 65 61 64 79 20 0a 2a  there already .*
ed690 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
ed6a0 78 79 47 65 74 48 6f 73 74 49 44 28 63 68 61 72  xyGetHostID(char
ed6b0 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74 20 2a   *pHostID, int *
ed6c0 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e 74 20 66  pError){.  int f
ed6d0 64 3b 0a 20 20 63 68 61 72 20 70 61 74 68 5b 4d  d;.  char path[M
ed6e0 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a 20 20 73  AXPATHLEN]; .  s
ed6f0 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 69 6e 74  ize_t len;.  int
ed700 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a   rc=SQLITE_OK;..
ed710 20 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44    proxyGetHostID
ed720 50 61 74 68 28 70 61 74 68 2c 20 4d 41 58 50 41  Path(path, MAXPA
ed730 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 74 72 79  THLEN);.  /* try
ed740 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 68   to create the h
ed750 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 69 66 20  ost ID file, if 
ed760 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  it already exist
ed770 73 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65  s read the conte
ed780 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d 20 6f 70  nts */.  fd = op
ed790 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 45 41 54  en(path, O_CREAT
ed7a0 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 58 43 4c  |O_WRONLY|O_EXCL
ed7b0 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 28 20 66  , 0644);.  if( f
ed7c0 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 65  d<0 ){.    int e
ed7d0 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a 20 20 20  rr=errno;....   
ed7e0 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 53 54   if( err!=EEXIST
ed7f0 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   ){.#ifdef SQLIT
ed800 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f 2a  E_PROXY_DEBUG /*
ed810 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 20   set the sqlite 
ed820 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
ed830 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20 66  stead */.      f
ed840 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
ed850 73 71 6c 69 74 65 20 65 72 72 6f 72 20 63 72 65  sqlite error cre
ed860 61 74 69 6e 67 20 68 6f 73 74 20 49 44 20 66 69  ating host ID fi
ed870 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20 20  le %s: %s\n",.  
ed880 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74 68              path
ed890 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29  , strerror(err))
ed8a0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72  ;.#endif.      r
ed8b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52  eturn SQLITE_PER
ed8c0 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  M;.    }.    /* 
ed8d0 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 74 65 20  couldn't create 
ed8e0 74 68 65 20 66 69 6c 65 2c 20 72 65 61 64 20 69  the file, read i
ed8f0 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20  t instead */.   
ed900 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c   fd = open(path,
ed910 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 58 43 4c   O_RDONLY|O_EXCL
ed920 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20  );.    if( fd<0 
ed930 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
ed940 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f 2a 20  _PROXY_DEBUG /* 
ed950 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 20 65  set the sqlite e
ed960 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 73  rror message ins
ed970 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e  tead */.      in
ed980 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20  t err = errno;. 
ed990 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
ed9a0 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72 72  err, "sqlite err
ed9b0 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f 73 74 20  or opening host 
ed9c0 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e  ID file %s: %s\n
ed9d0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
ed9e0 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28   path, strerror(
ed9f0 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20  err));.#endif.  
eda00 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
eda10 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20  E_PERM;.    }.  
eda20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 28 66 64    len = pread(fd
eda30 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53 54 49  , pHostID, HOSTI
eda40 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66  DLEN, 0);.    if
eda50 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20  ( len<0 ){.     
eda60 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72 6e 6f   *pError = errno
eda70 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
eda80 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a  ITE_IOERR_READ;.
eda90 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65      }else if( le
edaa0 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20  n<HOSTIDLEN ){. 
edab0 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 30       *pError = 0
edac0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
edad0 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
edae0 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20  READ;.    }.    
edaf0 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69  close(fd); /* si
edb00 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20  lently leak the 
edb10 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a  fd if it fails *
edb20 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22  /.    OSTRACE3("
edb30 47 45 54 48 4f 53 54 49 44 20 20 72 65 61 64 20  GETHOSTID  read 
edb40 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 48  %s pid=%d\n", pH
edb50 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 29 29  ostID, getpid())
edb60 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
edb70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
edb80 20 77 65 27 72 65 20 63 72 65 61 74 69 6e 67 20   we're creating 
edb90 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65  the host ID file
edba0 20 28 75 73 65 20 61 20 72 61 6e 64 6f 6d 20 73   (use a random s
edbb0 74 72 69 6e 67 20 6f 66 20 62 79 74 65 73 29 20  tring of bytes) 
edbc0 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 65 6e 65  */.    proxyGene
edbd0 72 61 74 65 48 6f 73 74 49 44 28 70 48 6f 73 74  rateHostID(pHost
edbe0 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70  ID);.    len = p
edbf0 77 72 69 74 65 28 66 64 2c 20 70 48 6f 73 74 49  write(fd, pHostI
edc00 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20 30 29  D, HOSTIDLEN, 0)
edc10 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c 30 20  ;.    if( len<0 
edc20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72  ){.      *pError
edc30 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
edc40 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
edc50 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c  R_WRITE;.    }el
edc60 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53 54 49  se if( len<HOSTI
edc70 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20 2a 70  DLEN ){.      *p
edc80 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20  Error = 0;.     
edc90 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
edca0 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 0a  RR_WRITE;.    }.
edcb0 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f      close(fd); /
edcc0 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20  * silently leak 
edcd0 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69  the fd if it fai
edce0 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43  ls */.    OSTRAC
edcf0 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 20 77  E3("GETHOSTID  w
edd00 72 6f 74 65 20 25 73 20 70 69 64 3d 25 64 5c 6e  rote %s pid=%d\n
edd10 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70  ", pHostID, getp
edd20 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72  id());.    retur
edd30 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61  n rc;.  }.}..sta
edd40 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74  tic int proxyGet
edd50 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 74 20 63  LockPath(const c
edd60 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61  har *dbPath, cha
edd70 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a 65 5f 74  r *lPath, size_t
edd80 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 6e 74 20   maxLen){.  int 
edd90 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 4c 65 6e  len;.  int dbLen
edda0 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 69 66 64  ;.  int i;..#ifd
eddb0 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 0a  ef LOCKPROXYDIR.
eddc0 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28    len = strlcpy(
eddd0 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 4f 58 59  lPath, LOCKPROXY
edde0 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 65  DIR, maxLen);.#e
eddf0 6c 73 65 0a 23 20 69 66 64 65 66 20 5f 43 53 5f  lse.# ifdef _CS_
ede00 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50  DARWIN_USER_TEMP
ede10 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 63 6f 6e  _DIR.  {.    con
ede20 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f  fstr(_CS_DARWIN_
ede30 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 20 6c  USER_TEMP_DIR, l
ede40 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20  Path, maxLen);. 
ede50 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74     len = strlcat
ede60 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 74 65 70  (lPath, "sqlitep
ede70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 6e 29 3b  locks", maxLen);
ede80 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 72 28 6c  .    if( mkdir(l
ede90 50 61 74 68 2c 20 53 51 4c 49 54 45 5f 44 45 46  Path, SQLITE_DEF
edea0 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45  AULT_PROXYDIR_PE
edeb0 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b 0a 20 20  RMISSIONS) ){.  
edec0 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 69 72 20      /* if mkdir 
eded0 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 20 61 73  fails, handle as
edee0 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 65 61 74   lock file creat
edef0 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20  ion failure */. 
edf00 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65       int err = e
edf10 72 72 6e 6f 3b 0a 23 20 20 69 66 64 65 66 20 53  rrno;.#  ifdef S
edf20 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
edf30 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 53    if( err!=EEXIS
edf40 54 20 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72  T ){.        fpr
edf50 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 70 72  intf(stderr, "pr
edf60 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 3a 20  oxyGetLockPath: 
edf70 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 20 65 72  mkdir(%s,0%o) er
edf80 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c 20 6c 50  ror %d %s\n", lP
edf90 61 74 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ath,.           
edfa0 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41       SQLITE_DEFA
edfb0 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52  ULT_PROXYDIR_PER
edfc0 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 2c 20 73  MISSIONS, err, s
edfd0 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a 20  trerror(err));. 
edfe0 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 69 66 0a       }.#  endif.
edff0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
ee000 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 4c 4f   OSTRACE3("GETLO
ee010 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 20 25 73  CKPATH  mkdir %s
ee020 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50 61 74   pid=%d\n", lPat
ee030 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20  h, getpid());.  
ee040 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a 23 20 65    }.    .  }.# e
ee050 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c  lse.  len = strl
ee060 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74 6d 70  cpy(lPath, "/tmp
ee070 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 20 65  /", maxLen);.# e
ee080 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 69  ndif.#endif..  i
ee090 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31 5d 21  f( lPath[len-1]!
ee0a0 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65 6e 20  ='/' ){.    len 
ee0b0 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c  = strlcat(lPath,
ee0c0 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20   "/", maxLen);. 
ee0d0 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61 6e 73   }.  .  /* trans
ee0e0 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61 74 68  form the db path
ee0f0 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63 61 63   to a unique cac
ee100 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 62 4c  he name */.  dbL
ee110 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e  en = (int)strlen
ee120 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f 72 28  (dbPath);.  for(
ee130 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20 26 26   i=0; i<dbLen &&
ee140 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 78 4c 65   (i+len+7)<maxLe
ee150 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61  n; i++){.    cha
ee160 72 20 63 20 3d 20 64 62 50 61 74 68 5b 69 5d 3b  r c = dbPath[i];
ee170 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b 6c 65 6e  .    lPath[i+len
ee180 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f 27 5f 27  ] = (c=='/')?'_'
ee190 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 74 68 5b  :c;.  }.  lPath[
ee1a0 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a 20 20 73  i+len]='\0';.  s
ee1b0 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 3a  trlcat(lPath, ":
ee1c0 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e 29 3b  auto:", maxLen);
ee1d0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
ee1e0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  _OK;.}../*.** Cr
ee1f0 65 61 74 65 20 61 20 6e 65 77 20 56 46 53 20 66  eate a new VFS f
ee200 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 28  ile descriptor (
ee210 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
ee220 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
ee230 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  * sqlite3_malloc
ee240 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 65 20 66  ) and open the f
ee250 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 74 68 22  ile named "path"
ee260 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73   in the file des
ee270 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  criptor..**.** T
ee280 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
ee290 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 6f 6e 6c  ponsible not onl
ee2a0 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 20 74 68  y for closing th
ee2b0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
ee2c0 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f 20 66 6f  r.** but also fo
ee2d0 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 6d 65  r freeing the me
ee2e0 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  mory associated 
ee2f0 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 64 65  with the file de
ee300 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a 73 74 61  scriptor..*/.sta
ee310 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 72 65  tic int proxyCre
ee320 61 74 65 55 6e 69 78 46 69 6c 65 28 63 6f 6e 73  ateUnixFile(cons
ee330 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 75 6e  t char *path, un
ee340 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 6c 65 29  ixFile **ppFile)
ee350 20 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 69   {.  int fd;.  i
ee360 6e 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20  nt dirfd = -1;. 
ee370 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 3b   unixFile *pNew;
ee380 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
ee390 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33  TE_OK;.  sqlite3
ee3a0 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b 0a 0a  _vfs dummyVfs;..
ee3b0 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 68    fd = open(path
ee3c0 2c 20 4f 5f 52 44 57 52 20 7c 20 4f 5f 43 52 45  , O_RDWR | O_CRE
ee3d0 41 54 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55  AT, SQLITE_DEFAU
ee3e0 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49  LT_FILE_PERMISSI
ee3f0 4f 4e 53 29 3b 0a 20 20 69 66 28 20 66 64 3c 30  ONS);.  if( fd<0
ee400 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
ee410 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
ee420 20 20 7d 0a 20 20 0a 20 20 70 4e 65 77 20 3d 20    }.  .  pNew = 
ee430 28 75 6e 69 78 46 69 6c 65 20 2a 29 73 71 6c 69  (unixFile *)sqli
ee440 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
ee450 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20  f(unixFile));.  
ee460 69 66 28 20 70 4e 65 77 3d 3d 4e 55 4c 4c 20 29  if( pNew==NULL )
ee470 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
ee480 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74  E_NOMEM;.    got
ee490 6f 20 65 6e 64 5f 63 72 65 61 74 65 5f 70 72 6f  o end_create_pro
ee4a0 78 79 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  xy;.  }.  memset
ee4b0 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66  (pNew, 0, sizeof
ee4c0 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20  (unixFile));..  
ee4d0 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 44 61 74  dummyVfs.pAppDat
ee4e0 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 75 74 6f  a = (void*)&auto
ee4f0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b 0a 20 20  lockIoFinder;.  
ee500 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46  rc = fillInUnixF
ee510 69 6c 65 28 26 64 75 6d 6d 79 56 66 73 2c 20 66  ile(&dummyVfs, f
ee520 64 2c 20 64 69 72 66 64 2c 20 28 73 71 6c 69 74  d, dirfd, (sqlit
ee530 65 33 5f 66 69 6c 65 2a 29 70 4e 65 77 2c 20 70  e3_file*)pNew, p
ee540 61 74 68 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66  ath, 0, 0);.  if
ee550 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
ee560 29 7b 0a 20 20 20 20 2a 70 70 46 69 6c 65 20 3d  ){.    *ppFile =
ee570 20 70 4e 65 77 3b 0a 20 20 20 20 72 65 74 75 72   pNew;.    retur
ee580 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
ee590 0a 65 6e 64 5f 63 72 65 61 74 65 5f 70 72 6f 78  .end_create_prox
ee5a0 79 3a 20 20 20 20 0a 20 20 63 6c 6f 73 65 28 66  y:    .  close(f
ee5b0 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20  d); /* silently 
ee5c0 6c 65 61 6b 20 66 64 20 69 66 20 65 72 72 6f 72  leak fd if error
ee5d0 2c 20 77 65 27 72 65 20 61 6c 72 65 61 64 79 20  , we're already 
ee5e0 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71  in error */.  sq
ee5f0 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29  lite3_free(pNew)
ee600 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
ee610 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63  ../* takes the c
ee620 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61  onch by taking a
ee630 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64   shared lock and
ee640 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e   read the conten
ee650 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a  ts conch, if .**
ee660 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e   lockPath is non
ee670 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20  -NULL, the host 
ee680 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65  ID and lock file
ee690 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68   path must match
ee6a0 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f  .  A NULL .** lo
ee6b0 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61  ckPath means tha
ee6c0 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69  t the lockPath i
ee6d0 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  n the conch file
ee6e0 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66   will be used if
ee6f0 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44   the .** host ID
ee700 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65  s match, or a ne
ee710 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c  w lock path will
ee720 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75   be generated au
ee730 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20  tomatically .** 
ee740 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74  and written to t
ee750 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a  he conch file..*
ee760 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
ee770 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78  xyTakeConch(unix
ee780 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20  File *pFile){.  
ee790 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
ee7a0 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
ee7b0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
ee7c0 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
ee7d0 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20  gContext; .  .  
ee7e0 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  if( pCtx->conchH
ee7f0 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74  eld>0 ){.    ret
ee800 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
ee810 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78   }else{.    unix
ee820 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20  File *conchFile 
ee830 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  = pCtx->conchFil
ee840 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74  e;.    char test
ee850 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b  Value[CONCHLEN];
ee860 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56  .    char conchV
ee870 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a  alue[CONCHLEN];.
ee880 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74      char lockPat
ee890 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20  h[MAXPATHLEN];. 
ee8a0 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61     char *tLockPa
ee8b0 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69  th = NULL;.    i
ee8c0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
ee8d0 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52  K;.    int readR
ee8e0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
ee8f0 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73     int syncPerms
ee900 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41   = 0;..    OSTRA
ee910 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20  CE4("TAKECONCH  
ee920 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64  %d for %s pid=%d
ee930 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  \n", conchFile->
ee940 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  h,.             
ee950 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  (pCtx->lockProxy
ee960 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63  Path ? pCtx->loc
ee970 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61  kProxyPath : ":a
ee980 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29  uto:"), getpid()
ee990 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e  );..    rc = con
ee9a0 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d  chFile->pMethod-
ee9b0 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f  >xLock((sqlite3_
ee9c0 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c  file*)conchFile,
ee9d0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20   SHARED_LOCK);. 
ee9e0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
ee9f0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e  E_OK ){.      in
eea00 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20  t pError = 0;.  
eea10 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56      memset(testV
eea20 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45  alue, 0, CONCHLE
eea30 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20  N); /* conch is 
eea40 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20  fixed size */.  
eea50 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65      rc = proxyGe
eea60 74 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75  tHostID(testValu
eea70 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20  e, &pError);.   
eea80 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29     if( (rc&0xff)
eea90 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  ==SQLITE_IOERR )
eeaa0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
eeab0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72  >lastErrno = pEr
eeac0 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
eead0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63     if( pCtx->loc
eeae0 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
eeaf0 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74        strlcpy(&t
eeb00 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c  estValue[HOSTIDL
eeb10 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  EN], pCtx->lockP
eeb20 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54  roxyPath, MAXPAT
eeb30 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  HLEN);.      }. 
eeb40 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
eeb50 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
eeb60 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b      goto end_tak
eeb70 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20  econch;.    }.  
eeb80 20 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20    .    readRc = 
eeb90 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65  unixRead((sqlite
eeba0 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69  3_file *)conchFi
eebb0 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20  le, conchValue, 
eebc0 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20  CONCHLEN, 0);.  
eebd0 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51    if( readRc!=SQ
eebe0 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
eebf0 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69  _READ ){.      i
eec00 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54  f( readRc!=SQLIT
eec10 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
eec20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53  if( (rc&0xff)==S
eec30 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20  QLITE_IOERR ){. 
eec40 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
eec50 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63  lastErrno = conc
eec60 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  hFile->lastErrno
eec70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
eec80 20 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b      rc = readRc;
eec90 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  .        goto en
eeca0 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20  d_takeconch;.   
eecb0 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66     }.      /* if
eecc0 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64   the conch has d
eecd0 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20  ata compare the 
eece0 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20  contents */.    
eecf0 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63    if( !pCtx->loc
eed00 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
eed10 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74        /* for aut
eed20 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f  o-named local lo
eed30 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68  ck file, just ch
eed40 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20  eck the host ID 
eed50 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20  and we'll.      
eed60 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f     ** use the lo
eed70 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61  cal lock file pa
eed80 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64  th that's alread
eed90 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20  y in there */.  
eeda0 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d        if( !memcm
eedb0 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e  p(testValue, con
eedc0 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c  chValue, HOSTIDL
eedd0 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EN) ){.         
eede0 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68   tLockPath = (ch
eedf0 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65  ar *)&conchValue
eee00 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20  [HOSTIDLEN];.   
eee10 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
eee20 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20  takeconch;.     
eee30 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
eee40 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27  {.        /* we'
eee50 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68  ve got the conch
eee60 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d   if conchValue m
eee70 61 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20  atches our path 
eee80 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20  and host ID */. 
eee90 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63         if( !memc
eeea0 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f  mp(testValue, co
eeeb0 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c  nchValue, CONCHL
eeec0 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EN) ){.         
eeed0 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f   goto end_takeco
eeee0 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nch;.        }. 
eeef0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
eef00 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f  {.      /* a sho
eef10 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65  rt read means we
eef20 27 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74  're "creating" t
eef30 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74  he conch (even t
eef40 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a  hough it could .
eef50 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65        ** have be
eef60 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e  en user-interven
eef70 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71  tion), if we acq
eef80 75 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69  uire the exclusi
eef90 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a  ve lock,.      *
eefa0 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d  * we'll try to m
eefb0 61 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74  atch the current
eefc0 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73   on-disk permiss
eefd0 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61  ions of the data
eefe0 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20  base.      */.  
eeff0 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20      syncPerms = 
ef000 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20  1;.    }.    .  
ef010 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63    /* either conc
ef020 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64  h was emtpy or d
ef030 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20  idn't match */. 
ef040 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f     if( !pCtx->lo
ef050 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20  ckProxyPath ){. 
ef060 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63       proxyGetLoc
ef070 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61  kPath(pCtx->dbPa
ef080 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41  th, lockPath, MA
ef090 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20  XPATHLEN);.     
ef0a0 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63   tLockPath = loc
ef0b0 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72  kPath;.      str
ef0c0 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b  lcpy(&testValue[
ef0d0 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b  HOSTIDLEN], lock
ef0e0 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e  Path, MAXPATHLEN
ef0f0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20  );.    }.    .  
ef100 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63    /* update conc
ef110 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20  h with host and 
ef120 70 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20  path (this will 
ef130 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72  fail if other pr
ef140 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61  ocess.     ** ha
ef150 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  s a shared lock 
ef160 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20  already) */.    
ef170 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  rc = conchFile->
ef180 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28  pMethod->xLock((
ef190 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f  sqlite3_file*)co
ef1a0 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49  nchFile, EXCLUSI
ef1b0 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  VE_LOCK);.    if
ef1c0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
ef1d0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e  ){.      rc = un
ef1e0 69 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33  ixWrite((sqlite3
ef1f0 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c  _file *)conchFil
ef200 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f  e, testValue, CO
ef210 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20  NCHLEN, 0);.    
ef220 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
ef230 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73  _OK && syncPerms
ef240 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75   ){.        stru
ef250 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20  ct stat buf;.   
ef260 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66       int err = f
ef270 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26  stat(pFile->h, &
ef280 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66  buf);.        if
ef290 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20  ( err==0 ){.    
ef2a0 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20        /* try to 
ef2b0 6d 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61  match the databa
ef2c0 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69  se file permissi
ef2d0 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c  ons, ignore fail
ef2e0 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ure */.#ifndef S
ef2f0 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55  QLITE_PROXY_DEBU
ef300 47 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d  G.          fchm
ef310 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  od(conchFile->h,
ef320 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23   buf.st_mode);.#
ef330 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69  else.          i
ef340 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46  f( fchmod(conchF
ef350 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d  ile->h, buf.st_m
ef360 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ode)!=0 ){.     
ef370 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20         int code 
ef380 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
ef390 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
ef3a0 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20  err, "fchmod %o 
ef3b0 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25  FAILED with %d %
ef3c0 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
ef3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef3e0 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20     buf.st_mode, 
ef3f0 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63  code, strerror(c
ef400 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20  ode));.         
ef410 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
ef420 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
ef430 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f  derr, "fchmod %o
ef440 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66   SUCCEDED\n",buf
ef450 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20  .st_mode);.     
ef460 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
ef470 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
ef480 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f  int code = errno
ef490 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69  ;.          fpri
ef4a0 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41  ntf(stderr, "STA
ef4b0 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74  T FAILED[%d] wit
ef4c0 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20  h %d %s\n", .   
ef4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef4e0 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65         err, code
ef4f0 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29  , strerror(code)
ef500 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
ef510 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
ef520 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d  }.    conchFile-
ef530 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
ef540 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
ef550 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52  )conchFile, SHAR
ef560 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64  ED_LOCK);.  .end
ef570 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20  _takeconch:.    
ef580 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50  OSTRACE2("TRANSP
ef590 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c  ROXY: CLOSE  %d\
ef5a0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20  n", pFile->h);. 
ef5b0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
ef5c0 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f  E_OK && pFile->o
ef5d0 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20  penFlags ){.    
ef5e0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d    if( pFile->h>=
ef5f0 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49  0 ){.#ifdef STRI
ef600 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20  CT_CLOSE_ERROR. 
ef610 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65         if( close
ef620 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20  (pFile->h) ){.  
ef630 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
ef640 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
ef650 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
ef660 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
ef670 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d  CLOSE;.        }
ef680 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63  .#else.        c
ef690 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20  lose(pFile->h); 
ef6a0 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b  /* silently leak
ef6b0 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23   fd if fail */.#
ef6c0 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20  endif.      }.  
ef6d0 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d      pFile->h = -
ef6e0 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20  1;.      int fd 
ef6f0 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50  = open(pCtx->dbP
ef700 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e  ath, pFile->open
ef710 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20  Flags,.         
ef720 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
ef730 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50  E_DEFAULT_FILE_P
ef740 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20  ERMISSIONS);.   
ef750 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41     OSTRACE2("TRA
ef760 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25  NSPROXY: OPEN  %
ef770 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20  d\n", fd);.     
ef780 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20   if( fd>=0 ){.  
ef790 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d        pFile->h =
ef7a0 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   fd;.      }else
ef7b0 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c  {.        rc=SQL
ef7c0 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a  ITE_CANTOPEN; /*
ef7d0 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72   SQLITE_BUSY? pr
ef7e0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c  oxyTakeConch cal
ef7f0 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20  led.            
ef800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef810 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e     during lockin
ef820 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  g */.      }.   
ef830 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
ef840 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74  QLITE_OK && !pCt
ef850 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a  x->lockProxy ){.
ef860 20 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68        char *path
ef870 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74   = tLockPath ? t
ef880 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d  LockPath : pCtx-
ef890 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a  >lockProxyPath;.
ef8a0 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65        /* ACS: Ne
ef8b0 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70  ed to make a cop
ef8c0 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69  y of path someti
ef8d0 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  mes */.      rc 
ef8e0 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69  = proxyCreateUni
ef8f0 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74  xFile(path, &pCt
ef900 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20  x->lockProxy);. 
ef910 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
ef920 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
ef930 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48      pCtx->conchH
ef940 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20  eld = 1;..      
ef950 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b  if( tLockPath ){
ef960 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c  .        pCtx->l
ef970 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73  ockProxyPath = s
ef980 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30  qlite3DbStrDup(0
ef990 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20  , tLockPath);.  
ef9a0 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e        if( pCtx->
ef9b0 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68  lockProxy->pMeth
ef9c0 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68  od == &afpIoMeth
ef9d0 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ods ){.         
ef9e0 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e   ((afpLockingCon
ef9f0 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63  text *)pCtx->loc
efa00 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43  kProxy->lockingC
efa10 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20  ontext)->dbPath 
efa20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
efa30 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63         pCtx->loc
efa40 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20  kProxyPath;.    
efa50 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
efa60 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
efa70 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74   conchFile->pMet
efa80 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71  hod->xUnlock((sq
efa90 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63  lite3_file*)conc
efaa0 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  hFile, NO_LOCK);
efab0 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41  .    }.    OSTRA
efac0 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20  CE3("TAKECONCH  
efad0 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46  %d %s\n", conchF
efae0 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49  ile->h, rc==SQLI
efaf0 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c  TE_OK?"ok":"fail
efb00 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ed");.    return
efb10 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   rc;.  }.}../*.*
efb20 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73  * If pFile holds
efb30 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e   a lock on a con
efb40 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65  ch file, then re
efb50 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e  lease that lock.
efb60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
efb70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68  roxyReleaseConch
efb80 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
efb90 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
efba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
efbb0 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72   /* Subroutine r
efbc0 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
efbd0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
efbe0 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54  ext *pCtx;  /* T
efbf0 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65  he locking conte
efc00 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79  xt for the proxy
efc10 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46   lock */.  unixF
efc20 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20  ile *conchFile; 
efc30 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
efc40 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  f the conch file
efc50 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70   */..  pCtx = (p
efc60 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
efc70 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt *)pFile->lock
efc80 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f  ingContext;.  co
efc90 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e  nchFile = pCtx->
efca0 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54  conchFile;.  OST
efcb0 52 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f  RACE4("RELEASECO
efcc0 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70  NCH  %d for %s p
efcd0 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46  id=%d\n", conchF
efce0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
efcf0 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72     (pCtx->lockPr
efd00 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e  oxyPath ? pCtx->
efd10 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20  lockProxyPath : 
efd20 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20  ":auto:"), .    
efd30 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29         getpid())
efd40 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  ;.  pCtx->conchH
efd50 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20  eld = 0;.  rc = 
efd60 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68  conchFile->pMeth
efd70 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c  od->xUnlock((sql
efd80 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68  ite3_file*)conch
efd90 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  File, NO_LOCK);.
efda0 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45    OSTRACE3("RELE
efdb0 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c  ASECONCH  %d %s\
efdc0 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68  n", conchFile->h
efdd0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63  ,.           (rc
efde0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f  ==SQLITE_OK ? "o
efdf0 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b  k" : "failed"));
efe00 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
efe10 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65  ./*.** Given the
efe20 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62   name of a datab
efe30 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74  ase file, comput
efe40 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74  e the name of it
efe50 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a  s conch file..**
efe60 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68   Store the conch
efe70 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d   filename in mem
efe80 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
efe90 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
efea0 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f  ()..** Make *pCo
efeb0 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f  nchPath point to
efec0 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20   the new name.  
efed0 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
efee0 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f   on success.** o
efef0 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  r SQLITE_NOMEM i
eff00 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61  f unable to obta
eff10 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a  in memory..**.**
eff20 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72   The caller is r
eff30 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65  esponsible for e
eff40 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65  nsuring that the
eff50 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
eff60 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76  y.** space is ev
eff70 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a  entually freed..
eff80 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74  **.** *pConchPat
eff90 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c  h is set to NULL
effa0 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   if a memory all
effb0 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63  ocation error oc
effc0 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  curs..*/.static 
effd0 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43  int proxyCreateC
effe0 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61  onchPathname(cha
efff0 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20  r *dbPath, char 
f0000 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20  **pConchPath){. 
f0010 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
f0020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f0030 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
f0040 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69  /.  int len = (i
f0050 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68  nt)strlen(dbPath
f0060 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20  ); /* Length of 
f0070 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  database filenam
f0080 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20  e - dbPath */.  
f0090 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b  char *conchPath;
f00a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f00b0 20 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68   buffer in which
f00c0 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f   to construct co
f00d0 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f  nch name */..  /
f00e0 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65  * Allocate space
f00f0 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66   for the conch f
f0100 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74  ilename and init
f0110 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20  ialize the name 
f0120 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65  to.  ** the name
f0130 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
f0140 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
f0150 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61  */  .  *pConchPa
f0160 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d  th = conchPath =
f0170 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
f0180 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29  _malloc(len + 8)
f0190 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74  ;.  if( conchPat
f01a0 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  h==0 ){.    retu
f01b0 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
f01c0 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f  .  }.  memcpy(co
f01d0 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c  nchPath, dbPath,
f01e0 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a   len+1);.  .  /*
f01f0 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e   now insert a ".
f0200 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73  " before the las
f0210 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f  t / character */
f0220 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31  .  for( i=(len-1
f0230 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a  ); i>=0; i-- ){.
f0240 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74      if( conchPat
f0250 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20  h[i]=='/' ){.   
f0260 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72     i++;.      br
f0270 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
f0280 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e   conchPath[i]='.
f0290 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c  ';.  while ( i<l
f02a0 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50  en ){.    conchP
f02b0 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b  ath[i+1]=dbPath[
f02c0 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d  i];.    i++;.  }
f02d0 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68  ..  /* append th
f02e0 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69  e "-conch" suffi
f02f0 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f  x to the file */
f0300 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68  .  memcpy(&conch
f0310 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e  Path[i+1], "-con
f0320 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72  ch", 7);.  asser
f0330 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63  t( (int)strlen(c
f0340 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e  onchPath) == len
f0350 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  +7 );..  return 
f0360 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
f0370 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20  * Takes a fully 
f0380 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79  configured proxy
f0390 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75   locking-style u
f03a0 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69  nix file and swi
f03b0 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63  tches.** the loc
f03c0 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74  al lock file pat
f03d0 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h .*/.static int
f03e0 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79   switchLockProxy
f03f0 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70  Path(unixFile *p
f0400 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  File, const char
f0410 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78   *path) {.  prox
f0420 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f0430 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
f0440 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46  ckingContext*)pF
f0450 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
f0460 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64  ext;.  char *old
f0470 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63  Path = pCtx->loc
f0480 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e  kProxyPath;.  in
f0490 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
f04a0 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  ;..  if( pFile->
f04b0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
f04c0 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
f04d0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
f04e0 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67    ..  /* nothing
f04f0 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61   to do if the pa
f0500 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74  th is NULL, :aut
f0510 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68  o: or matches th
f0520 65 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20  e existing path 
f0530 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c  */.  if( !path |
f0540 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20  | path[0]=='\0' 
f0550 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c  || !strcmp(path,
f0560 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20   ":auto:") ||.  
f0570 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73    (oldPath && !s
f0580 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20  trncmp(oldPath, 
f0590 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e  path, MAXPATHLEN
f05a0 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  )) ){.    return
f05b0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
f05c0 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c  lse{.    unixFil
f05d0 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70  e *lockProxy = p
f05e0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a  Ctx->lockProxy;.
f05f0 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72      pCtx->lockPr
f0600 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43  oxy=NULL;.    pC
f0610 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20  tx->conchHeld = 
f0620 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50  0;.    if( lockP
f0630 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20  roxy!=NULL ){.  
f0640 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79      rc=lockProxy
f0650 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73  ->pMethod->xClos
f0660 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  e((sqlite3_file 
f0670 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20  *)lockProxy);.  
f0680 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
f0690 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71  urn rc;.      sq
f06a0 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50  lite3_free(lockP
f06b0 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20  roxy);.    }.   
f06c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c   sqlite3_free(ol
f06d0 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78  dPath);.    pCtx
f06e0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
f06f0 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
f0700 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a  p(0, path);.  }.
f0710 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a    .  return rc;.
f0720 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69  }../*.** pFile i
f0730 73 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61  s a file that ha
f0740 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79  s been opened by
f0750 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63   a prior xOpen c
f0760 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20  all.  dbPath.** 
f0770 69 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66  is a string buff
f0780 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50  er at least MAXP
f0790 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74  ATHLEN+1 charact
f07a0 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a  ers in size..**.
f07b0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
f07c0 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d  find the filenam
f07d0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
f07e0 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74  h pFile and writ
f07f0 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50  es it.** int dbP
f0800 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ath..*/.static i
f0810 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74  nt proxyGetDbPat
f0820 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69  hForUnixFile(uni
f0830 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68  xFile *pFile, ch
f0840 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66  ar *dbPath){.#if
f0850 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
f0860 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d  __).  if( pFile-
f0870 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70  >pMethod == &afp
f0880 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  IoMethods ){.   
f0890 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65   /* afp style ke
f08a0 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20  eps a reference 
f08b0 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69  to the db path i
f08c0 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66  n the filePath f
f08d0 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20  ield .    ** of 
f08e0 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20  the struct */.  
f08f0 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73    assert( (int)s
f0900 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69  trlen((char*)pFi
f0910 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f0920 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20  xt)<=MAXPATHLEN 
f0930 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62  );.    strcpy(db
f0940 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69  Path, ((afpLocki
f0950 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
f0960 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f0970 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d  t)->dbPath);.  }
f0980 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66  else.#endif.  if
f0990 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  ( pFile->pMethod
f09a0 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65   == &dotlockIoMe
f09b0 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
f09c0 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75  dot lock style u
f09d0 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ses the locking 
f09e0 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65  context to store
f09f0 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20   the dot lock.  
f0a00 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a    ** file path *
f0a10 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20  /.    int len = 
f0a20 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70  strlen((char *)p
f0a30 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
f0a40 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44  text) - strlen(D
f0a50 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a  OTLOCK_SUFFIX);.
f0a60 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74      memcpy(dbPat
f0a70 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65  h, (char *)pFile
f0a80 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f0a90 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65  , len + 1);.  }e
f0aa0 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20  lse{.    /* all 
f0ab0 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65  other styles use
f0ac0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e   the locking con
f0ad0 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68  text to store th
f0ae0 65 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a  e db file path *
f0af0 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74  /.    assert( st
f0b00 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c  rlen((char*)pFil
f0b10 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f0b20 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29  t)<=MAXPATHLEN )
f0b30 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50  ;.    strcpy(dbP
f0b40 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69  ath, (char *)pFi
f0b50 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f0b60 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  xt);.  }.  retur
f0b70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
f0b80 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61  /*.** Takes an a
f0b90 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e  lready filled in
f0ba0 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61   unix file and a
f0bb0 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20  lters it so all 
f0bc0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a  file locking .**
f0bd0 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d   will be perform
f0be0 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ed on the local 
f0bf0 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e  proxy lock file.
f0c00 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
f0c10 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72  fields.** are pr
f0c20 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c  eserved in the l
f0c30 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73  ocking context s
f0c40 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20  o that they can 
f0c50 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20  be restored and 
f0c60 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72  .** the unix str
f0c70 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20  ucture properly 
f0c80 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c  cleaned up at cl
f0c90 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e  ose time:.**  ->
f0ca0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a  lockingContext.*
f0cb0 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a  *  ->pMethod.*/.
f0cc0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
f0cd0 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c  TransformUnixFil
f0ce0 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c  e(unixFile *pFil
f0cf0 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  e, const char *p
f0d00 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f  ath) {.  proxyLo
f0d10 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
f0d20 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74  tx;.  char dbPat
f0d30 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b  h[MAXPATHLEN+1];
f0d40 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
f0d50 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
f0d60 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c  ile */.  char *l
f0d70 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20  ockPath=NULL;.  
f0d80 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
f0d90 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69  OK;.  .  if( pFi
f0da0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f  le->locktype!=NO
f0db0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74  _LOCK ){.    ret
f0dc0 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
f0dd0 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44  .  }.  proxyGetD
f0de0 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65  bPathForUnixFile
f0df0 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b  (pFile, dbPath);
f0e00 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20  .  if( !path || 
f0e10 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c  path[0]=='\0' ||
f0e20 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22   !strcmp(path, "
f0e30 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20  :auto:") ){.    
f0e40 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20  lockPath=NULL;. 
f0e50 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b   }else{.    lock
f0e60 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74  Path=(char *)pat
f0e70 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52  h;.  }.  .  OSTR
f0e80 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59  ACE4("TRANSPROXY
f0e90 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d    %d for %s pid=
f0ea0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
f0eb0 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63  .           (loc
f0ec0 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68  kPath ? lockPath
f0ed0 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65   : ":auto:"), ge
f0ee0 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78  tpid());..  pCtx
f0ef0 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
f0f00 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29  c( sizeof(*pCtx)
f0f10 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d   );.  if( pCtx==
f0f20 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
f0f30 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
f0f40 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c  }.  memset(pCtx,
f0f50 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78   0, sizeof(*pCtx
f0f60 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78  ));..  rc = prox
f0f70 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68  yCreateConchPath
f0f80 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43  name(dbPath, &pC
f0f90 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74  tx->conchFilePat
f0fa0 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  h);.  if( rc==SQ
f0fb0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
f0fc0 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55  c = proxyCreateU
f0fd0 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f  nixFile(pCtx->co
f0fe0 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43  nchFilePath, &pC
f0ff0 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a  tx->conchFile);.
f1000 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d    }  .  if( rc==
f1010 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63  SQLITE_OK && loc
f1020 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74  kPath ){.    pCt
f1030 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
f1040 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
f1050 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b  up(0, lockPath);
f1060 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
f1070 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
f1080 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69   /* all memory i
f1090 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f  s allocated, pro
f10a0 78 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20  xys are created 
f10b0 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20  and assigned, . 
f10c0 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65     ** switch the
f10d0 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
f10e0 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65   and pMethod the
f10f0 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f  n return..    */
f1100 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74  .    pCtx->dbPat
f1110 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  h = sqlite3DbStr
f1120 44 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a  Dup(0, dbPath);.
f1130 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63      pCtx->oldLoc
f1140 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46  kingContext = pF
f1150 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
f1160 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ext;.    pFile->
f1170 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d  lockingContext =
f1180 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d   pCtx;.    pCtx-
f1190 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46  >pOldMethod = pF
f11a0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20  ile->pMethod;.  
f11b0 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64    pFile->pMethod
f11c0 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f   = &proxyIoMetho
f11d0 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ds;.  }else{.   
f11e0 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68   if( pCtx->conch
f11f0 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72  File ){ .      r
f1200 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46  c = pCtx->conchF
f1210 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43  ile->pMethod->xC
f1220 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69  lose((sqlite3_fi
f1230 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68  le *)pCtx->conch
f1240 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28  File);.      if(
f1250 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
f1260 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
f1270 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46  ree(pCtx->conchF
f1280 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ile);.    }.    
f1290 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74  sqlite3_free(pCt
f12a0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68  x->conchFilePath
f12b0 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ); .    sqlite3_
f12c0 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a  free(pCtx);.  }.
f12d0 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e    OSTRACE3("TRAN
f12e0 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22  SPROXY  %d %s\n"
f12f0 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
f1300 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49         (rc==SQLI
f1310 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22  TE_OK ? "ok" : "
f1320 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74  failed"));.  ret
f1330 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
f1340 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68  * This routine h
f1350 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66  andles sqlite3_f
f1360 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61  ile_control() ca
f1370 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65  lls that are spe
f1380 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78  cific.** to prox
f1390 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74  y locking..*/.st
f13a0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69  atic int proxyFi
f13b0 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
f13c0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
f13d0 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
f13e0 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
f13f0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
f1400 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _GET_LOCKPROXYFI
f1410 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78  LE: {.      unix
f1420 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
f1430 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20  nixFile*)id;.   
f1440 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d     if( pFile->pM
f1450 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49  ethod == &proxyI
f1460 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
f1470 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67      proxyLocking
f1480 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20  Context *pCtx = 
f1490 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e  (proxyLockingCon
f14a0 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  text*)pFile->loc
f14b0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20  kingContext;.   
f14c0 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f       proxyTakeCo
f14d0 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  nch(pFile);.    
f14e0 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f      if( pCtx->lo
f14f0 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20  ckProxyPath ){. 
f1500 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74           *(const
f1510 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20   char **)pArg = 
f1520 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
f1530 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ath;.        }el
f1540 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28  se{.          *(
f1550 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41  const char **)pA
f1560 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f  rg = ":auto: (no
f1570 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20  t held)";.      
f1580 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65    }.      } else
f1590 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e   {.        *(con
f15a0 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20  st char **)pArg 
f15b0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a  = NULL;.      }.
f15c0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
f15d0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
f15e0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45    case SQLITE_SE
f15f0 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a  T_LOCKPROXYFILE:
f1600 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c   {.      unixFil
f1610 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
f1620 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20  File*)id;.      
f1630 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
f1640 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73  OK;.      int is
f1650 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46  ProxyStyle = (pF
f1660 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20  ile->pMethod == 
f1670 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29  &proxyIoMethods)
f1680 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67  ;.      if( pArg
f1690 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74  ==NULL || (const
f16a0 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20   char *)pArg==0 
f16b0 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  ){.        if( i
f16c0 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20  sProxyStyle ){. 
f16d0 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e           /* turn
f16e0 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69   off proxy locki
f16f0 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74  ng - not support
f1700 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ed */.          
f1710 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
f1720 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f  R /*SQLITE_PROTO
f1730 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55  COL? SQLITE_MISU
f1740 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d  SE?*/;.        }
f1750 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
f1760 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78  /* turn off prox
f1770 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65  y locking - alre
f1780 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a  ady off - NOOP *
f1790 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  /.          rc =
f17a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
f17b0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
f17c0 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  e{.        const
f17d0 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68   char *proxyPath
f17e0 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
f17f0 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69  )pArg;.        i
f1800 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20  f( isProxyStyle 
f1810 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f  ){.          pro
f1820 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f1830 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20   *pCtx = .      
f1840 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b        (proxyLock
f1850 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c  ingContext*)pFil
f1860 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f1870 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  t;.          if(
f1880 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22   !strcmp(pArg, "
f1890 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20  :auto:") .      
f18a0 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c       || (pCtx->l
f18b0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a  ockProxyPath &&.
f18c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21                 !
f18d0 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f  strncmp(pCtx->lo
f18e0 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f  ckProxyPath, pro
f18f0 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c  xyPath, MAXPATHL
f1900 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29  EN)).          )
f1910 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
f1920 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
f1930 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
f1940 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
f1950 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50  switchLockProxyP
f1960 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79  ath(pFile, proxy
f1970 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20  Path);.         
f1980 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
f1990 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74  {.          /* t
f19a0 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c  urn on proxy fil
f19b0 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20  e locking */.   
f19c0 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78         rc = prox
f19d0 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69  yTransformUnixFi
f19e0 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50  le(pFile, proxyP
f19f0 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ath);.        }.
f1a00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
f1a10 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
f1a20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
f1a30 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b      assert( 0 );
f1a40 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73    /* The call as
f1a50 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20  sures that only 
f1a60 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72  valid opcodes ar
f1a70 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a  e sent */.    }.
f1a80 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48    }.  /*NOTREACH
f1a90 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51  ED*/.  return SQ
f1aa0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f  LITE_ERROR;.}../
f1ab0 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73  *.** Within this
f1ac0 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70   division (the p
f1ad0 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20  roxying locking 
f1ae0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20  implementation) 
f1af0 74 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a  the procedures.*
f1b00 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69  * above this poi
f1b10 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69  nt are all utili
f1b20 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d  ties.  The lock-
f1b30 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20  related methods 
f1b40 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d  of the.** proxy-
f1b50 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f  locking sqlite3_
f1b60 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74  io_method object
f1b70 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a   follow..*/.../*
f1b80 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
f1b90 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
f1ba0 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
f1bb0 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
f1bc0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
f1bd0 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
f1be0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
f1bf0 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
f1c00 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65  s held, set *pRe
f1c10 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e  sOut.** to a non
f1c20 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65  -zero value othe
f1c30 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69  rwise *pResOut i
f1c40 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20  s set to zero.  
f1c50 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
f1c60 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51  .** is set to SQ
f1c70 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61  LITE_OK unless a
f1c80 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75  n I/O error occu
f1c90 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63  rs during lock c
f1ca0 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  hecking..*/.stat
f1cb0 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63  ic int proxyChec
f1cc0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
f1cd0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
f1ce0 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a  int *pResOut) {.
f1cf0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
f1d00 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
f1d10 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72  d;.  int rc = pr
f1d20 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69  oxyTakeConch(pFi
f1d30 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  le);.  if( rc==S
f1d40 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
f1d50 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f1d60 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
f1d70 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f1d80 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
f1d90 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e  gContext;.    un
f1da0 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20  ixFile *proxy = 
f1db0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b  pCtx->lockProxy;
f1dc0 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78  .    return prox
f1dd0 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65  y->pMethod->xChe
f1de0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28  ckReservedLock((
f1df0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72  sqlite3_file*)pr
f1e00 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20  oxy, pResOut);. 
f1e10 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
f1e20 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
f1e30 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
f1e40 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
f1e50 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
f1e60 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
f1e70 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
f1e80 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
f1e90 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
f1ea0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
f1eb0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
f1ec0 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
f1ed0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
f1ee0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
f1ef0 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
f1f00 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
f1f10 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
f1f20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
f1f30 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
f1f40 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
f1f50 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
f1f60 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
f1f70 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
f1f80 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
f1f90 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
f1fa0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
f1fb0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
f1fc0 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
f1fd0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
f1fe0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
f1ff0 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
f2000 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
f2010 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
f2020 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
f2030 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
f2040 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
f2050 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
f2060 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
f2070 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
f2080 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
f2090 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
f20a0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
f20b0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
f20c0 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
f20d0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
f20e0 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
f20f0 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
f2100 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69  k.  Use the sqli
f2110 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a  te3OsUnlock().**
f2120 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65   routine to lowe
f2130 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  r a locking leve
f2140 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
f2150 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74   proxyLock(sqlit
f2160 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
f2170 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75   locktype) {.  u
f2180 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
f2190 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
f21a0 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79    int rc = proxy
f21b0 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29  TakeConch(pFile)
f21c0 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
f21d0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f  TE_OK ){.    pro
f21e0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f21f0 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c   *pCtx = (proxyL
f2200 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
f2210 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f2220 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46  ntext;.    unixF
f2230 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74  ile *proxy = pCt
f2240 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20  x->lockProxy;.  
f2250 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d    rc = proxy->pM
f2260 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71  ethod->xLock((sq
f2270 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78  lite3_file*)prox
f2280 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20  y, locktype);.  
f2290 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
f22a0 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74  e = proxy->lockt
f22b0 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ype;.  }.  retur
f22c0 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
f22d0 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  Lower the lockin
f22e0 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20  g level on file 
f22f0 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65  descriptor pFile
f2300 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c   to locktype.  l
f2310 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20  ocktype.** must 
f2320 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
f2330 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
f2340 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
f2350 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20  ocking level of 
f2360 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
f2370 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
f2380 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68  t or below.** th
f2390 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
f23a0 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  ing level, this 
f23b0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
f23c0 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  op..*/.static in
f23d0 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71  t proxyUnlock(sq
f23e0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
f23f0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a  int locktype) {.
f2400 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
f2410 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
f2420 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72  d;.  int rc = pr
f2430 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69  oxyTakeConch(pFi
f2440 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  le);.  if( rc==S
f2450 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
f2460 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f2470 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
f2480 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f2490 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
f24a0 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e  gContext;.    un
f24b0 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20  ixFile *proxy = 
f24c0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b  pCtx->lockProxy;
f24d0 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d  .    rc = proxy-
f24e0 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
f24f0 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
f2500 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65  )proxy, locktype
f2510 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  );.    pFile->lo
f2520 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e  cktype = proxy->
f2530 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20  locktype;.  }.  
f2540 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
f2550 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65  .** Close a file
f2560 20 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79   that uses proxy
f2570 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69   locks..*/.stati
f2580 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65  c int proxyClose
f2590 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
f25a0 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b  d) {.  if( id ){
f25b0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
f25c0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
f25d0 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c  *)id;.    proxyL
f25e0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70  ockingContext *p
f25f0 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b  Ctx = (proxyLock
f2600 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69  ingContext *)pFi
f2610 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f2620 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65  xt;.    unixFile
f2630 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43   *lockProxy = pC
f2640 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20  tx->lockProxy;. 
f2650 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e     unixFile *con
f2660 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63  chFile = pCtx->c
f2670 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e  onchFile;.    in
f2680 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
f2690 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c  ;.    .    if( l
f26a0 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20  ockProxy ){.    
f26b0 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79    rc = lockProxy
f26c0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f  ->pMethod->xUnlo
f26d0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck((sqlite3_file
f26e0 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f  *)lockProxy, NO_
f26f0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28  LOCK);.      if(
f2700 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
f2710 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b  .      rc = lock
f2720 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e  Proxy->pMethod->
f2730 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f  xClose((sqlite3_
f2740 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29  file*)lockProxy)
f2750 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
f2760 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
f2770 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c    sqlite3_free(l
f2780 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20  ockProxy);.     
f2790 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
f27a0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
f27b0 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b  if( conchFile ){
f27c0 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d  .      if( pCtx-
f27d0 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20  >conchHeld ){.  
f27e0 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
f27f0 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69  ReleaseConch(pFi
f2800 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  le);.        if(
f2810 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
f2820 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
f2830 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70  c = conchFile->p
f2840 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28  Method->xClose((
f2850 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f  sqlite3_file*)co
f2860 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20  nchFile);.      
f2870 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
f2880 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  rc;.      sqlite
f2890 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65  3_free(conchFile
f28a0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
f28b0 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e  ite3_free(pCtx->
f28c0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a  lockProxyPath);.
f28d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f28e0 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65  (pCtx->conchFile
f28f0 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74  Path);.    sqlit
f2900 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62  e3_free(pCtx->db
f2910 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65  Path);.    /* re
f2920 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e  store the origin
f2930 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65  al locking conte
f2940 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74  xt and pMethod t
f2950 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a  hen close it */.
f2960 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69      pFile->locki
f2970 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78  ngContext = pCtx
f2980 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74  ->oldLockingCont
f2990 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ext;.    pFile->
f29a0 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e  pMethod = pCtx->
f29b0 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20  pOldMethod;.    
f29c0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74  sqlite3_free(pCt
f29d0 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70  x);.    return p
f29e0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  File->pMethod->x
f29f0 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20  Close(id);.  }. 
f2a00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f2a10 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f  K;.}....#endif /
f2a20 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  * defined(__APPL
f2a30 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45  E__) && SQLITE_E
f2a40 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
f2a50 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65  YLE */./*.** The
f2a60 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73   proxy locking s
f2a70 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64  tyle is intended
f2a80 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46   for use with AF
f2a90 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a  P filesystems..*
f2aa0 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20  * And since AFP 
f2ab0 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65  is only supporte
f2ac0 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65  d on MacOSX, the
f2ad0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69   proxy locking i
f2ae0 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69  s also.** restri
f2af0 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a  cted to MacOSX..
f2b00 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ** .**.*********
f2b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f2b20 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b  f the proxy lock
f2b30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
f2b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2b50 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
f2b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2ba0 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e  *****/../*.** In
f2bb0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65  itialize the ope
f2bc0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e  rating system in
f2bd0 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54  terface..**.** T
f2be0 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69  his routine regi
f2bf0 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d  sters all VFS im
f2c00 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f  plementations fo
f2c10 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72  r unix-like oper
f2c20 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73  ating.** systems
f2c30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c  .  This routine,
f2c40 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33   and the sqlite3
f2c50 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e  _os_end() routin
f2c60 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a  e that follows,.
f2c70 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  ** should be the
f2c80 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69   only routines i
f2c90 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74  n this file that
f2ca0 20 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f   are visible fro
f2cb0 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73  m other.** files
f2cc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
f2cd0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f  tine is called o
f2ce0 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74  nce during SQLit
f2cf0 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
f2d00 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e   and by a.** sin
f2d10 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  gle thread.  The
f2d20 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
f2d30 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62  on and mutex sub
f2d40 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74  systems have not
f2d50 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20  .** necessarily 
f2d60 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
f2d70 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
f2d80 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e  ne is called, an
f2d90 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f  d so they.** sho
f2da0 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e  uld not be used.
f2db0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
f2dc0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  nt sqlite3_os_in
f2dd0 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20  it(void){ .  /* 
f2de0 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  ** The follow
f2df0 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65  ing macro define
f2e00 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72  s an initializer
f2e10 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f   for an sqlite3_
f2e20 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a  vfs object..  **
f2e30 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
f2e40 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54   VFS is NAME.  T
f2e50 68 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61  he pAppData is a
f2e60 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f   pointer to a po
f2e70 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68  inter.  ** to th
f2e80 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74  e "finder" funct
f2e90 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20  ion.  (pAppData 
f2ea0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
f2eb0 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73  a pointer becaus
f2ec0 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30  e.  ** silly C90
f2ed0 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20   rules prohibit 
f2ee0 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69  a void* from bei
f2ef0 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e  ng cast to a fun
f2f00 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20  ction pointer.  
f2f10 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76  ** and so we hav
f2f20 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20  e to go through 
f2f30 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65  the intermediate
f2f40 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69   pointer to avoi
f2f50 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20  d problems.  ** 
f2f60 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77  when compiling w
f2f70 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72  ith -pedantic-er
f2f80 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20  rors on GCC.).  
f2f90 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44  **.  ** The FIND
f2fa0 45 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  ER parameter to 
f2fb0 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68  this macro is th
f2fc0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f  e name of the po
f2fd0 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a  inter to the.  *
f2fe0 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  * finder-functio
f2ff0 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66  n.  The finder-f
f3000 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
f3010 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
f3020 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f  .  ** sqlite_io_
f3030 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74  methods object t
f3040 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
f3050 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69  he desired locki
f3060 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72  ng.  ** behavior
f3070 73 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69  s.  See the divi
f3080 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20  sion above that 
f3090 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d  contains the IOM
f30a0 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72  ETHODS.  ** macr
f30b0 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69  o for addition i
f30c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69  nformation on fi
f30d0 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a  nder-functions..
f30e0 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66    **.  ** Most f
f30f0 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65  inders simply re
f3100 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
f3110 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65  o a fixed sqlite
f3120 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a  3_io_methods.  *
f3130 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74  * object.  But t
f3140 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69  he "autolockIoFi
f3150 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20  nder" available 
f3160 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61  on MacOSX does a
f3170 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72   little.  ** mor
f3180 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20  e than that; it 
f3190 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c  looks at the fil
f31a0 65 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61  esystem type tha
f31b0 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a  t hosts the .  *
f31c0 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
f31d0 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f  and tries to cho
f31e0 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d  ose an locking m
f31f0 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74  ethod appropriat
f3200 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20  e for.  ** that 
f3210 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e  filesystem time.
f3220 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20  .  */.  #define 
f3230 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c  UNIXVFS(VFSNAME,
f3240 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20   FINDER) {      
f3250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3260 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20    \.    1,      
f3270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f3280 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20   iVersion */    
f3290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f32a0 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69  \.    sizeof(uni
f32b0 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73  xFile),     /* s
f32c0 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20  zOsFile */      
f32d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f32e0 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45      MAX_PATHNAME
f32f0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50  ,         /* mxP
f3300 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20  athname */      
f3310 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f3320 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
f3330 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
f3340 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f3350 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f3360 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20  VFSNAME,        
f3370 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a        /* zName *
f3380 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f3390 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76          \.    (v
f33a0 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20  oid*)&FINDER,   
f33b0 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20      /* pAppData 
f33c0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f33d0 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f33e0 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
f33f0 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20    /* xOpen */   
f3400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3410 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65      \.    unixDe
f3420 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  lete,           
f3430 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20  /* xDelete */   
f3440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3450 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65    \.    unixAcce
f3460 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ss,           /*
f3470 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20   xAccess */     
f3480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3490 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61  \.    unixFullPa
f34a0 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78  thname,     /* x
f34b0 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20  FullPathname */ 
f34c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f34d0 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20      unixDlOpen, 
f34e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
f34f0 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20  Open */         
f3500 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f3510 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20    unixDlError,  
f3520 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72          /* xDlEr
f3530 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ror */          
f3540 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f3550 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20  unixDlSym,      
f3560 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20        /* xDlSym 
f3570 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f3580 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
f3590 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20  ixDlClose,      
f35a0 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20      /* xDlClose 
f35b0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f35c0 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f35d0 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20  Randomness,     
f35e0 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73    /* xRandomness
f35f0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f3600 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c      \.    unixSl
f3610 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  eep,            
f3620 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20  /* xSleep */    
f3630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3640 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72    \.    unixCurr
f3650 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a  entTime,      /*
f3660 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f   xCurrentTime */
f3670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3680 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73  \.    unixGetLas
f3690 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78  tError      /* x
f36a0 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20  GetLastError */ 
f36b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f36c0 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41    }..  /*.  ** A
f36d0 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73  ll default VFSes
f36e0 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f   for unix are co
f36f0 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66  ntained in the f
f3700 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a  ollowing array..
f3710 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74    **.  ** Note t
f3720 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f  hat the sqlite3_
f3730 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20  vfs.pNext field 
f3740 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63  of the VFS objec
f3750 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20  t is modified.  
f3760 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65  ** by the SQLite
f3770 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56   core when the V
f3780 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64  FS is registered
f3790 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  .  So the follow
f37a0 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63  ing.  ** array c
f37b0 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a  annot be const..
f37c0 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71    */.  static sq
f37d0 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d  lite3_vfs aVfs[]
f37e0 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f   = {.#if SQLITE_
f37f0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
f3800 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28  TYLE && defined(
f3810 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55  __APPLE__).    U
f3820 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20  NIXVFS("unix",  
f3830 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b          autolock
f3840 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73  IoFinder ),.#els
f3850 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75  e.    UNIXVFS("u
f3860 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70  nix",          p
f3870 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a  osixIoFinder ),.
f3880 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56  #endif.    UNIXV
f3890 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20  FS("unix-none", 
f38a0 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64      nolockIoFind
f38b0 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46  er ),.    UNIXVF
f38c0 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22  S("unix-dotfile"
f38d0 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64  ,  dotlockIoFind
f38e0 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58 57  er ),.#if OS_VXW
f38f0 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 53  ORKS.    UNIXVFS
f3900 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22  ("unix-namedsem"
f3910 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29 2c  , semIoFinder ),
f3920 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
f3930 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
f3940 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49 58  G_STYLE.    UNIX
f3950 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 22  VFS("unix-posix"
f3960 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64  ,    posixIoFind
f3970 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46  er ),.    UNIXVF
f3980 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20  S("unix-flock", 
f3990 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72     flockIoFinder
f39a0 20 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53   ),.#endif.#if S
f39b0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
f39c0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65  KING_STYLE && de
f39d0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
f39e0 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e  .    UNIXVFS("un
f39f0 69 78 2d 61 66 70 22 2c 20 20 20 20 20 20 61 66  ix-afp",      af
f3a00 70 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20  pIoFinder ),.   
f3a10 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70   UNIXVFS("unix-p
f3a20 72 6f 78 79 22 2c 20 20 20 20 70 72 6f 78 79 49  roxy",    proxyI
f3a30 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69  oFinder ),.#endi
f3a40 66 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e 65  f.  };.  unsigne
f3a50 64 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20  d int i;        
f3a60 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
f3a70 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73  r */..  /* Regis
f3a80 74 65 72 20 61 6c 6c 20 56 46 53 65 73 20 64 65  ter all VFSes de
f3a90 66 69 6e 65 64 20 69 6e 20 74 68 65 20 61 56 66  fined in the aVf
f3aa0 73 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 20 66  s[] array */.  f
f3ab0 6f 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65 6f  or(i=0; i<(sizeo
f3ac0 66 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28 73  f(aVfs)/sizeof(s
f3ad0 71 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69 2b  qlite3_vfs)); i+
f3ae0 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  +){.    sqlite3_
f3af0 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 61 56  vfs_register(&aV
f3b00 66 73 5b 69 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20  fs[i], i==0);.  
f3b10 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
f3b20 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  E_OK; .}../*.** 
f3b30 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6f 70 65  Shutdown the ope
f3b40 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e  rating system in
f3b50 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53  terface..**.** S
f3b60 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  ome operating sy
f3b70 73 74 65 6d 73 20 6d 69 67 68 74 20 6e 65 65 64  stems might need
f3b80 20 74 6f 20 64 6f 20 73 6f 6d 65 20 63 6c 65 61   to do some clea
f3b90 6e 75 70 20 69 6e 20 74 68 69 73 20 72 6f 75 74  nup in this rout
f3ba0 69 6e 65 2c 0a 2a 2a 20 74 6f 20 72 65 6c 65 61  ine,.** to relea
f3bb0 73 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61  se dynamically a
f3bc0 6c 6c 6f 63 61 74 65 64 20 6f 62 6a 65 63 74 73  llocated objects
f3bd0 2e 20 20 42 75 74 20 6e 6f 74 20 6f 6e 20 75 6e  .  But not on un
f3be0 69 78 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ix..** This rout
f3bf0 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66  ine is a no-op f
f3c00 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49  or unix..*/.SQLI
f3c10 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
f3c20 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b  e3_os_end(void){
f3c30 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54   .  return SQLIT
f3c40 45 5f 4f 4b 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69  E_OK; .}. .#endi
f3c50 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 55  f /* SQLITE_OS_U
f3c60 4e 49 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  NIX */../*******
f3c70 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
f3c80 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  s_unix.c *******
f3c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3cb0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
f3cc0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
f3cd0 6c 65 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a  le os_win.c ****
f3ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3d00 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
f3d10 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20  04 May 22.**.** 
f3d20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
f3d30 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
f3d40 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
f3d50 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
f3d60 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
f3d70 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
f3d80 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
f3d90 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
f3da0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
f3db0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
f3dc0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
f3dd0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
f3de0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
f3df0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
f3e00 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
f3e10 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
f3e20 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
f3e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
f3e80 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
f3e90 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74  ntains code that
f3ea0 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f 20   is specific to 
f3eb0 77 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 24  windows..**.** $
f3ec0 49 64 3a 20 6f 73 5f 77 69 6e 2e 63 2c 76 20 31  Id: os_win.c,v 1
f3ed0 2e 31 34 35 20 32 30 30 38 2f 31 32 2f 31 31 20  .145 2008/12/11 
f3ee0 30 32 3a 35 38 3a 32 37 20 73 68 61 6e 65 20 45  02:58:27 shane E
f3ef0 78 70 20 24 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  xp $.*/.#if SQLI
f3f00 54 45 5f 4f 53 5f 57 49 4e 20 20 20 20 20 20 20  TE_OS_WIN       
f3f10 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
f3f20 66 69 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72  file is used for
f3f30 20 77 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f   windows only */
f3f40 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20  .../*.** A Note 
f3f50 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c  About Memory All
f3f60 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54  ocation:.**.** T
f3f70 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20  his driver uses 
f3f80 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20  malloc()/free() 
f3f90 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20  directly rather 
f3fa0 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75  than going throu
f3fb0 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65  gh.** the SQLite
f3fc0 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65  -wrappers sqlite
f3fd0 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74  3_malloc()/sqlit
f3fe0 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73  e3_free().  Thos
f3ff0 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72  e wrappers.** ar
f4000 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75  e designed for u
f4010 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73  se on embedded s
f4020 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d  ystems where mem
f4030 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e  ory is scarce an
f4040 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  d.** malloc fail
f4050 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71  ures happen freq
f4060 75 65 6e 74 6c 79 2e 20 20 57 69 6e 33 32 20 64  uently.  Win32 d
f4070 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c  oes not typicall
f4080 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65  y run on.** embe
f4090 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e  dded systems, an
f40a0 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74  d when it does t
f40b0 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f  he developers no
f40c0 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67  rmally have bigg
f40d0 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74  er.** problems t
f40e0 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68  o worry about th
f40f0 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f  an running out o
f4100 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68  f memory.  So th
f4110 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20  ere is not.** a 
f4120 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20  compelling need 
f4130 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70  to use the wrapp
f4140 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74  ers..**.** But t
f4150 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72  here is a good r
f4160 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65  eason to not use
f4170 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20   the wrappers.  
f4180 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a  If we use the.**
f4190 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77   wrappers then w
f41a0 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c  e will get simul
f41b0 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61  ated malloc() fa
f41c0 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68  ilures within th
f41d0 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41  is.** driver.  A
f41e0 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61  nd that causes a
f41f0 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62  ll kinds of prob
f4200 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73  lems for our tes
f4210 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64  ts.  We.** could
f4220 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20   enhance SQLite 
f4230 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d  to deal with sim
f4240 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61  ulated malloc fa
f4250 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a  ilures within.**
f4260 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20   the OS driver, 
f4270 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20  but the code to 
f4280 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20  deal with those 
f4290 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f  failure would no
f42a0 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65  t.** be exercise
f42b0 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63  d on Linux (whic
f42c0 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  h does not need 
f42d0 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74  to malloc() in t
f42e0 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e  he driver).** an
f42f0 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61  d so we would ha
f4300 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72  ve difficulty wr
f4310 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74  iting coverage t
f4320 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a  ests for that.**
f4330 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74   code.  Better t
f4340 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65  o leave the code
f4350 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a   out, we think..
f4360 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20  **.** The point 
f4370 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69  of this discussi
f4380 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  on is as follows
f4390 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67  :  When creating
f43a0 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79   a new.** OS lay
f43b0 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64  er for an embedd
f43c0 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f  ed system, if yo
f43d0 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20  u use this file 
f43e0 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a  as an example,.*
f43f0 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20  * avoid the use 
f4400 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65  of malloc()/free
f4410 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69  ().  Those routi
f4420 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77  nes work ok on w
f4430 69 6e 64 6f 77 73 0a 2a 2a 20 64 65 73 6b 74 6f  indows.** deskto
f4440 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65  ps but not so we
f4450 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73  ll in embedded s
f4460 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63  ystems..*/..#inc
f4470 6c 75 64 65 20 3c 77 69 6e 62 61 73 65 2e 68 3e  lude <winbase.h>
f4480 0a 0a 23 69 66 64 65 66 20 5f 5f 43 59 47 57 49  ..#ifdef __CYGWI
f4490 4e 5f 5f 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73  N__.# include <s
f44a0 79 73 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 65 6e  ys/cygwin.h>.#en
f44b0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  dif../*.** Macro
f44c0 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
f44d0 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
f44e0 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 61 64  ot to use thread
f44f0 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  s..*/.#if define
f4500 64 28 54 48 52 45 41 44 53 41 46 45 29 20 26 26  d(THREADSAFE) &&
f4510 20 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65   THREADSAFE.# de
f4520 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 33 32 5f  fine SQLITE_W32_
f4530 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66  THREADS 1.#endif
f4540 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20  ../*.** Include 
f4550 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d  code that is com
f4560 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e  mon to all os_*.
f4570 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a  c files.*/./****
f4580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
f4590 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69  de os_common.h i
f45a0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
f45b0 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_win.c *******
f45c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f45d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
f45e0 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e   file os_common.
f45f0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
f4600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
f4620 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a   2004 May 22.**.
f4630 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
f4640 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
f4650 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
f4660 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
f4670 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
f4680 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
f4690 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
f46a0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
f46b0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
f46c0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
f46d0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
f46e0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
f46f0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
f4700 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
f4710 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
f4720 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
f4730 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
f4740 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
f4750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4790 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
f47a0 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73   contains macros
f47b0 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69   and a little bi
f47c0 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69  t of code that i
f47d0 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61  s common to.** a
f47e0 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f  ll of the platfo
f47f0 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65  rm-specific file
f4800 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69  s (os_*.c) and i
f4810 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f  s #included into
f4820 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e   those.** files.
f4830 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
f4840 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c   should be #incl
f4850 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a  uded by the os_*
f4860 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20  .c files only.  
f4870 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67  It is not a.** g
f4880 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68  eneral purpose h
f4890 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  eader file..**.*
f48a0 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e  * $Id: os_common
f48b0 2e 68 2c 76 20 31 2e 33 37 20 32 30 30 38 2f 30  .h,v 1.37 2008/0
f48c0 35 2f 32 39 20 32 30 3a 32 32 3a 33 37 20 73 68  5/29 20:22:37 sh
f48d0 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  ane Exp $.*/.#if
f48e0 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f  ndef _OS_COMMON_
f48f0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43  H_.#define _OS_C
f4900 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  OMMON_H_../*.** 
f4910 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67  At least two bug
f4920 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69  s have slipped i
f4930 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61  n because we cha
f4940 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f  nged the MEMORY_
f4950 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74  DEBUG.** macro t
f4960 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61  o SQLITE_DEBUG a
f4970 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61  nd some older ma
f4980 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74  kefiles have not
f4990 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a   yet made the.**
f49a0 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f   switch.  The fo
f49b0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f  llowing code sho
f49c0 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70  uld catch this p
f49d0 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c  roblem at compil
f49e0 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65  e-time..*/.#ifde
f49f0 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23  f MEMORY_DEBUG.#
f4a00 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f   error "The MEMO
f4a10 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69  RY_DEBUG macro i
f4a20 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65  s obsolete.  Use
f4a30 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e   SQLITE_DEBUG in
f4a40 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a  stead.".#endif..
f4a50 0a 2f 2a 0a 20 2a 20 57 68 65 6e 20 74 65 73 74  ./*. * When test
f4a60 69 6e 67 2c 20 74 68 69 73 20 67 6c 6f 62 61 6c  ing, this global
f4a70 20 76 61 72 69 61 62 6c 65 20 73 74 6f 72 65 73   variable stores
f4a80 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66   the location of
f4a90 20 74 68 65 0a 20 2a 20 70 65 6e 64 69 6e 67 2d   the. * pending-
f4aa0 62 79 74 65 20 69 6e 20 74 68 65 20 64 61 74 61  byte in the data
f4ab0 62 61 73 65 20 66 69 6c 65 2e 0a 20 2a 2f 0a 23  base file.. */.#
f4ac0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
f4ad0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 75 6e 73  T.SQLITE_API uns
f4ae0 69 67 6e 65 64 20 69 6e 74 20 73 71 6c 69 74 65  igned int sqlite
f4af0 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65 20 3d  3_pending_byte =
f4b00 20 30 78 34 30 30 30 30 30 30 30 3b 0a 23 65 6e   0x40000000;.#en
f4b10 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
f4b20 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
f4b30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
f4b40 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a  te3OSTrace = 0;.
f4b50 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31  #define OSTRACE1
f4b60 28 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20  (X)         if( 
f4b70 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
f4b80 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
f4b90 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f  ntf(X).#define O
f4ba0 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20  STRACE2(X,Y)    
f4bb0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
f4bc0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
f4bd0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a  ebugPrintf(X,Y).
f4be0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33  #define OSTRACE3
f4bf0 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20  (X,Y,Z)     if( 
f4c00 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
f4c10 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
f4c20 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69  ntf(X,Y,Z).#defi
f4c30 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c  ne OSTRACE4(X,Y,
f4c40 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74  Z,A)   if( sqlit
f4c50 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
f4c60 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
f4c70 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20  ,Y,Z,A).#define 
f4c80 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41  OSTRACE5(X,Y,Z,A
f4c90 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f  ,B) if( sqlite3O
f4ca0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
f4cb0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
f4cc0 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f  Z,A,B).#define O
f4cd0 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c  STRACE6(X,Y,Z,A,
f4ce0 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71  B,C) \.    if(sq
f4cf0 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71  lite3OSTrace) sq
f4d00 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
f4d10 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64  (X,Y,Z,A,B,C).#d
f4d20 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58  efine OSTRACE7(X
f4d30 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a  ,Y,Z,A,B,C,D) \.
f4d40 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53      if(sqlite3OS
f4d50 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65  Trace) sqlite3De
f4d60 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
f4d70 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23  A,B,C,D).#else.#
f4d80 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28  define OSTRACE1(
f4d90 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  X).#define OSTRA
f4da0 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65  CE2(X,Y).#define
f4db0 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29   OSTRACE3(X,Y,Z)
f4dc0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
f4dd0 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69  4(X,Y,Z,A).#defi
f4de0 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c  ne OSTRACE5(X,Y,
f4df0 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f  Z,A,B).#define O
f4e00 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c  STRACE6(X,Y,Z,A,
f4e10 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  B,C).#define OST
f4e20 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c  RACE7(X,Y,Z,A,B,
f4e30 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  C,D).#endif../*.
f4e40 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65  ** Macros for pe
f4e50 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e  rformance tracin
f4e60 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72  g.  Normally tur
f4e70 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77  ned off.  Only w
f4e80 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20  orks.** on i486 
f4e90 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66  hardware..*/.#if
f4ea0 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f  def SQLITE_PERFO
f4eb0 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a  RMANCE_TRACE../*
f4ec0 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f   .** hwtime.h co
f4ed0 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73  ntains inline as
f4ee0 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72  sembler code for
f4ef0 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a   implementing .*
f4f00 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e  * high-performan
f4f10 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e  ce timing routin
f4f20 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  es..*/./********
f4f30 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68  ****** Include h
f4f40 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d  wtime.h in the m
f4f50 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d  iddle of os_comm
f4f60 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
f4f70 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
f4f80 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
f4f90 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a  e hwtime.h *****
f4fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4fc0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
f4fd0 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54  8 May 27.**.** T
f4fe0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
f4ff0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
f5000 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
f5010 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
f5020 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
f5030 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
f5040 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
f5050 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
f5060 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
f5070 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
f5080 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
f5090 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
f50a0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
f50b0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
f50c0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
f50d0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
f50e0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
f50f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
f5140 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
f5150 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d  tains inline asm
f5160 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65   code for retrie
f5170 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f  ving "high-perfo
f5180 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74  rmance".** count
f5190 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73  ers for x86 clas
f51a0 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49  s CPUs..**.** $I
f51b0 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e  d: hwtime.h,v 1.
f51c0 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a  3 2008/08/01 14:
f51d0 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20  33:15 shane Exp 
f51e0 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57  $.*/.#ifndef _HW
f51f0 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20  TIME_H_.#define 
f5200 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a  _HWTIME_H_../*.*
f5210 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
f5220 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72  routine only wor
f5230 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c  ks on pentium-cl
f5240 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70  ass (or newer) p
f5250 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74  rocessors..** It
f5260 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20   uses the RDTSC 
f5270 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74  opcode to read t
f5280 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76  he cycle count v
f5290 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a  alue out of the.
f52a0 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64  ** processor and
f52b0 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61   returns that va
f52c0 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62  lue.  This can b
f52d0 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d  e used for high-
f52e0 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67  res.** profiling
f52f0 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65  ..*/.#if (define
f5300 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64  d(__GNUC__) || d
f5310 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
f5320 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65  ) && \.      (de
f5330 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64  fined(i386) || d
f5340 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29  efined(__i386__)
f5350 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49   || defined(_M_I
f5360 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66  X86))..  #if def
f5370 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a  ined(__GNUC__)..
f5380 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
f5390 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
f53a0 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
f53b0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e       unsigned in
f53c0 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f  t lo, hi;.     _
f53d0 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
f53e0 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22  e__ ("rdtsc" : "
f53f0 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28  =a" (lo), "=d" (
f5400 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72  hi));.     retur
f5410 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  n (sqlite_uint64
f5420 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a  )hi << 32 | lo;.
f5430 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66    }..  #elif def
f5440 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a  ined(_MSC_VER)..
f5450 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b    __declspec(nak
f5460 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c  ed) __inline sql
f5470 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65  ite_uint64 __cde
f5480 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  cl sqlite3Hwtime
f5490 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61  (void){.     __a
f54a0 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74  sm {.        rdt
f54b0 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20  sc.        ret  
f54c0 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61       ; return va
f54d0 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20  lue at EDX:EAX. 
f54e0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e      }.  }..  #en
f54f0 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69  dif..#elif (defi
f5500 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26  ned(__GNUC__) &&
f5510 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36   defined(__x86_6
f5520 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  4__))..  __inlin
f5530 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
f5540 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
f5550 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
f5560 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a  igned long val;.
f5570 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
f5580 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74  volatile__ ("rdt
f5590 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29  sc" : "=A" (val)
f55a0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
f55b0 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66  val;.  }. .#elif
f55c0 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
f55d0 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f  __) && defined(_
f55e0 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e  _ppc__))..  __in
f55f0 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69  line__ sqlite_ui
f5600 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
f5610 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20  me(void){.      
f5620 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
f5630 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20  ng retval;.     
f5640 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a   unsigned long j
f5650 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d  unk;.      __asm
f5660 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20  __ __volatile__ 
f5670 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  ("\n\.          
f5680 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20  1:      mftbu   
f5690 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %1\n\.          
f56a0 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20          mftb    
f56b0 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  %L0\n\.         
f56c0 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20           mftbu  
f56d0 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20   %0\n\.         
f56e0 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20           cmpw   
f56f0 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20   %0,%1\n\.      
f5700 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20              bne 
f5710 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20      1b".        
f5720 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22            : "=r"
f5730 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20   (retval), "=r" 
f5740 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72  (junk));.      r
f5750 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20  eturn retval;.  
f5760 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72  }..#else..  #err
f5770 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e  or Need implemen
f5780 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
f5790 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f  3Hwtime() for yo
f57a0 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20  ur platform...  
f57b0 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69  /*.  ** To compi
f57c0 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65  le without imple
f57d0 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48  menting sqlite3H
f57e0 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72  wtime() for your
f57f0 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20   platform,.  ** 
f5800 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74  you can remove t
f5810 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20  he above #error 
f5820 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c  and use the foll
f5830 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20  owing.  ** stub 
f5840 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77  function.  You w
f5850 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20  ill lose timing 
f5860 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79  support for many
f5870 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62  .  ** of the deb
f5880 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69  ugging and testi
f5890 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75  ng utilities, bu
f58a0 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20  t it should at. 
f58b0 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c   ** least compil
f58c0 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a  e and run..  */.
f58d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
f58e0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
f58f0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
f5900 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c  d){ return ((sql
f5910 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d  ite_uint64)0); }
f5920 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ..#endif..#endif
f5930 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57   /* !defined(_HW
f5940 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a  TIME_H_) */../**
f5950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
f5960 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a   of hwtime.h ***
f5970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
f59a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
f59b0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
f59c0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f   left off in os_
f59d0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
f59e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74  ***********/..st
f59f0 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74  atic sqlite_uint
f5a00 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74  64 g_start;.stat
f5a10 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ic sqlite_uint64
f5a20 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66   g_elapsed;.#def
f5a30 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20  ine TIMER_START 
f5a40 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71        g_start=sq
f5a50 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64  lite3Hwtime().#d
f5a60 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20  efine TIMER_END 
f5a70 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65          g_elapse
f5a80 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  d=sqlite3Hwtime(
f5a90 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e  )-g_start.#defin
f5aa0 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20  e TIMER_ELAPSED 
f5ab0 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65      g_elapsed.#e
f5ac0 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  lse.#define TIME
f5ad0 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20  R_START.#define 
f5ae0 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e  TIMER_END.#defin
f5af0 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20  e TIMER_ELAPSED 
f5b00 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e      ((sqlite_uin
f5b10 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f  t64)0).#endif../
f5b20 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69  *.** If we compi
f5b30 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  le with the SQLI
f5b40 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65  TE_TEST macro se
f5b50 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c  t, then the foll
f5b60 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f  owing block.** o
f5b70 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65  f code will give
f5b80 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20   us the ability 
f5b90 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69  to simulate a di
f5ba0 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54  sk I/O error.  T
f5bb0 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66  his.** is used f
f5bc0 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49  or testing the I
f5bd0 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69  /O recovery logi
f5be0 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  c..*/.#ifdef SQL
f5bf0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
f5c00 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
f5c10 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30  io_error_hit = 0
f5c20 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
f5c30 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
f5c40 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51  I/O Errors */.SQ
f5c50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
f5c60 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61  ite3_io_error_ha
f5c70 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20  rdhit = 0;      
f5c80 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e    /* Number of n
f5c90 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73  on-benign errors
f5ca0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
f5cb0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
f5cc0 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b  ror_pending = 0;
f5cd0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74          /* Count
f5ce0 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49   down to first I
f5cf0 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49  /O error */.SQLI
f5d00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
f5d10 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73  e3_io_error_pers
f5d20 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ist = 0;        
f5d30 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65  /* True if I/O e
f5d40 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f  rrors persist */
f5d50 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f5d60 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
f5d70 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20  _benign = 0;    
f5d80 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
f5d90 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67  errors are benig
f5da0 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  n */.SQLITE_API 
f5db0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  int sqlite3_disk
f5dc0 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30  full_pending = 0
f5dd0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
f5de0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
f5df0 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53  l = 0;.#define S
f5e00 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
f5e10 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f  nign(X) sqlite3_
f5e20 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d  io_error_benign=
f5e30 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  (X).#define Simu
f5e40 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45  lateIOError(CODE
f5e50 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69  )  \.  if( (sqli
f5e60 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72  te3_io_error_per
f5e70 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f  sist && sqlite3_
f5e80 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a  io_error_hit) \.
f5e90 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
f5ea0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
f5eb0 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20  ng-- == 1 )  \. 
f5ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c               { l
f5ed0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f  ocal_ioerr(); CO
f5ee0 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69  DE; }.static voi
f5ef0 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b  d local_ioerr(){
f5f00 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45  .  IOTRACE(("IOE
f5f10 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74  RR\n"));.  sqlit
f5f20 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b  e3_io_error_hit+
f5f30 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  +;.  if( !sqlite
f5f40 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67  3_io_error_benig
f5f50 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  n ) sqlite3_io_e
f5f60 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a  rror_hardhit++;.
f5f70 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  }.#define Simula
f5f80 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
f5f90 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73  CODE) \.   if( s
f5fa0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
f5fb0 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20  pending ){ \.   
f5fc0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69    if( sqlite3_di
f5fd0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d  skfull_pending =
f5fe0 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20  = 1 ){ \.       
f5ff0 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c  local_ioerr(); \
f6000 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
f6010 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a  diskfull = 1; \.
f6020 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
f6030 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b  o_error_hit = 1;
f6040 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20   \.       CODE; 
f6050 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a  \.     }else{ \.
f6060 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64         sqlite3_d
f6070 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d  iskfull_pending-
f6080 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20  -; \.     } \.  
f6090 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   }.#else.#define
f60a0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
f60b0 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e  Benign(X).#defin
f60c0 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
f60d0 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  r(A).#define Sim
f60e0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72  ulateDiskfullErr
f60f0 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  or(A).#endif../*
f6100 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67  .** When testing
f6110 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f  , keep a count o
f6120 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
f6130 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23  open files..*/.#
f6140 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
f6150 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
f6160 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69   sqlite3_open_fi
f6170 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64  le_count = 0;.#d
f6180 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65  efine OpenCounte
f6190 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70  r(X)  sqlite3_op
f61a0 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28  en_file_count+=(
f61b0 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  X).#else.#define
f61c0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a   OpenCounter(X).
f61d0 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
f61e0 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43  * !defined(_OS_C
f61f0 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a  OMMON_H_) */../*
f6200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
f6210 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  d of os_common.h
f6220 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
f6230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
f6250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
f6260 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
f6270 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73  e left off in os
f6280 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _win.c *********
f6290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
f62a0 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73  *.** Some micros
f62b0 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61  oft compilers la
f62c0 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74 69  ck this definiti
f62d0 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49  on..*/.#ifndef I
f62e0 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52  NVALID_FILE_ATTR
f62f0 49 42 55 54 45 53 0a 23 20 64 65 66 69 6e 65 20  IBUTES.# define 
f6300 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
f6310 52 49 42 55 54 45 53 20 28 28 44 57 4f 52 44 29  RIBUTES ((DWORD)
f6320 2d 31 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  -1) .#endif../*.
f6330 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20  ** Determine if 
f6340 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77  we are dealing w
f6350 69 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20  ith WindowsCE - 
f6360 77 68 69 63 68 20 68 61 73 20 61 20 6d 75 63 68  which has a much
f6370 0a 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e  .** reduced API.
f6380 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
f6390 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65  S_WINCE.# define
f63a0 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49   AreFileApisANSI
f63b0 28 29 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  () 1.#endif../*.
f63c0 2a 2a 20 57 69 6e 43 45 20 6c 61 63 6b 73 20 6e  ** WinCE lacks n
f63d0 61 74 69 76 65 20 73 75 70 70 6f 72 74 20 66 6f  ative support fo
f63e0 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73  r file locking s
f63f0 6f 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6b  o we have to fak
f6400 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 73 6f 6d  e it.** with som
f6410 65 20 63 6f 64 65 20 6f 66 20 6f 75 72 20 6f 77  e code of our ow
f6420 6e 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  n..*/.#if SQLITE
f6430 5f 4f 53 5f 57 49 4e 43 45 0a 74 79 70 65 64 65  _OS_WINCE.typede
f6440 66 20 73 74 72 75 63 74 20 77 69 6e 63 65 4c 6f  f struct winceLo
f6450 63 6b 20 7b 0a 20 20 69 6e 74 20 6e 52 65 61 64  ck {.  int nRead
f6460 65 72 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  ers;       /* Nu
f6470 6d 62 65 72 20 6f 66 20 72 65 61 64 65 72 20 6c  mber of reader l
f6480 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 2a 2f  ocks obtained */
f6490 0a 20 20 42 4f 4f 4c 20 62 50 65 6e 64 69 6e 67  .  BOOL bPending
f64a0 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61  ;      /* Indica
f64b0 74 65 73 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f  tes a pending lo
f64c0 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61  ck has been obta
f64d0 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62  ined */.  BOOL b
f64e0 52 65 73 65 72 76 65 64 3b 20 20 20 20 20 2f 2a  Reserved;     /*
f64f0 20 49 6e 64 69 63 61 74 65 73 20 61 20 72 65 73   Indicates a res
f6500 65 72 76 65 64 20 6c 6f 63 6b 20 68 61 73 20 62  erved lock has b
f6510 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a  een obtained */.
f6520 20 20 42 4f 4f 4c 20 62 45 78 63 6c 75 73 69 76    BOOL bExclusiv
f6530 65 3b 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74  e;    /* Indicat
f6540 65 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  es an exclusive 
f6550 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62  lock has been ob
f6560 74 61 69 6e 65 64 20 2a 2f 0a 7d 20 77 69 6e 63  tained */.} winc
f6570 65 4c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f  eLock;.#endif../
f6580 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 46 69 6c 65  *.** The winFile
f6590 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 20   structure is a 
f65a0 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69  subclass of sqli
f65b0 74 65 33 5f 66 69 6c 65 2a 20 73 70 65 63 69 66  te3_file* specif
f65c0 69 63 20 74 6f 20 74 68 65 20 77 69 6e 33 32 0a  ic to the win32.
f65d0 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 6c  ** portability l
f65e0 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ayer..*/.typedef
f65f0 20 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20   struct winFile 
f6600 77 69 6e 46 69 6c 65 3b 0a 73 74 72 75 63 74 20  winFile;.struct 
f6610 77 69 6e 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73  winFile {.  cons
f6620 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
f6630 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a  hods *pMethod;/*
f6640 20 4d 75 73 74 20 62 65 20 66 69 72 73 74 20 2a   Must be first *
f6650 2f 0a 20 20 48 41 4e 44 4c 45 20 68 3b 20 20 20  /.  HANDLE h;   
f6660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
f6670 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73  andle for access
f6680 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  ing the file */.
f6690 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
f66a0 6c 6f 63 6b 74 79 70 65 3b 20 2f 2a 20 54 79 70  locktype; /* Typ
f66b0 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e  e of lock curren
f66c0 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73  tly held on this
f66d0 20 66 69 6c 65 20 2a 2f 0a 20 20 73 68 6f 72 74   file */.  short
f66e0 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b   sharedLockByte;
f66f0 20 20 20 2f 2a 20 52 61 6e 64 6f 6d 6c 79 20 63     /* Randomly c
f6700 68 6f 73 65 6e 20 62 79 74 65 20 75 73 65 64 20  hosen byte used 
f6710 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  as a shared lock
f6720 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f   */.#if SQLITE_O
f6730 53 5f 57 49 4e 43 45 0a 20 20 57 43 48 41 52 20  S_WINCE.  WCHAR 
f6740 2a 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b  *zDeleteOnClose;
f6750 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
f6760 65 20 74 6f 20 64 65 6c 65 74 65 20 77 68 65 6e  e to delete when
f6770 20 63 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 48 41   closing */.  HA
f6780 4e 44 4c 45 20 68 4d 75 74 65 78 3b 20 20 20 20  NDLE hMutex;    
f6790 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75        /* Mutex u
f67a0 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61  sed to control a
f67b0 63 63 65 73 73 20 74 6f 20 73 68 61 72 65 64 20  ccess to shared 
f67c0 6c 6f 63 6b 20 2a 2f 20 20 0a 20 20 48 41 4e 44  lock */  .  HAND
f67d0 4c 45 20 68 53 68 61 72 65 64 3b 20 20 20 20 20  LE hShared;     
f67e0 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65      /* Shared me
f67f0 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 75 73 65  mory segment use
f6800 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f  d for locking */
f6810 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63  .  winceLock loc
f6820 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  al;        /* Lo
f6830 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20  cks obtained by 
f6840 74 68 69 73 20 69 6e 73 74 61 6e 63 65 20 6f 66  this instance of
f6850 20 77 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 77 69   winFile */.  wi
f6860 6e 63 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64 3b  nceLock *shared;
f6870 20 20 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c 20        /* Global 
f6880 73 68 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f  shared lock memo
f6890 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20  ry for the file 
f68a0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 0a   */.#endif.};...
f68b0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
f68c0 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20  ing variable is 
f68d0 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f  (normally) set o
f68e0 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68  nce and never ch
f68f0 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66  anges.** thereaf
f6900 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73  ter.  It records
f6910 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65   whether the ope
f6920 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73  rating system is
f6930 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e   Win95.** or Win
f6940 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f  NT..**.** 0:   O
f6950 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
f6960 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20  unknown..** 1:  
f6970 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   Operating syste
f6980 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32  m is Win95..** 2
f6990 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79  :   Operating sy
f69a0 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a  stem is WinNT..*
f69b0 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f  *.** In order to
f69c0 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74   facilitate test
f69d0 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73  ing on a WinNT s
f69e0 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20  ystem, the test 
f69f0 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d  fixture.** can m
f6a00 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73  anually set this
f6a10 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65   value to 1 to e
f6a20 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68  mulate Win98 beh
f6a30 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66  avior..*/.#ifdef
f6a40 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
f6a50 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f6a60 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b  te3_os_type = 0;
f6a70 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e  .#else.static in
f6a80 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70  t sqlite3_os_typ
f6a90 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  e = 0;.#endif../
f6aa0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
f6ab0 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77   (non-zero) if w
f6ac0 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e  e are running un
f6ad0 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b  der WinNT, Win2K
f6ae0 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57  , WinXP,.** or W
f6af0 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61  inCE.  Return fa
f6b00 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57  lse (zero) for W
f6b10 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20  in95, Win98, or 
f6b20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72  WinME..**.** Her
f6b30 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74  e is an interest
f6b40 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a  ing observation:
f6b50 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20    Win95, Win98, 
f6b60 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a  and WinME lack.*
f6b70 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78  * the LockFileEx
f6b80 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20  () API.  But we 
f6b90 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63  can still static
f6ba0 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73  ally link agains
f6bb0 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73  t that.** API as
f6bc0 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27   long as we don'
f6bd0 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75  t call it win ru
f6be0 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d  nning Win95/98/M
f6bf0 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a  E.  A call to.**
f6c00 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
f6c10 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
f6c20 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69  ne if the host i
f6c30 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72  s Win95/98/ME or
f6c40 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20  .** WinNT/2K/XP 
f6c50 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20  so that we will 
f6c60 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20  know whether or 
f6c70 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c  not we can safel
f6c80 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f  y call.** the Lo
f6c90 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a  ckFileEx() API..
f6ca0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f6cb0 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20  _WINCE.# define 
f6cc0 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73  isNT()  (1).#els
f6cd0 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 69  e.  static int i
f6ce0 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 69  sNT(void){.    i
f6cf0 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79  f( sqlite3_os_ty
f6d00 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f  pe==0 ){.      O
f6d10 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e  SVERSIONINFO sIn
f6d20 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e  fo;.      sInfo.
f6d30 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53  dwOSVersionInfoS
f6d40 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e  ize = sizeof(sIn
f6d50 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56 65  fo);.      GetVe
f6d60 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b  rsionEx(&sInfo);
f6d70 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6f  .      sqlite3_o
f6d80 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64  s_type = sInfo.d
f6d90 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52  wPlatformId==VER
f6da0 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f  _PLATFORM_WIN32_
f6db0 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20  NT ? 2 : 1;.    
f6dc0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  }.    return sql
f6dd0 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b  ite3_os_type==2;
f6de0 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
f6df0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a  QLITE_OS_WINCE *
f6e00 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  /../*.** Convert
f6e10 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20   a UTF-8 string 
f6e20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69  to microsoft uni
f6e30 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e 20  code (UTF-16?). 
f6e40 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20  .**.** Space to 
f6e50 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
f6e60 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61  d string is obta
f6e70 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
f6e80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41  ..*/.static WCHA
f6e90 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64 65  R *utf8ToUnicode
f6ea0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
f6eb0 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e  lename){.  int n
f6ec0 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a 7a  Char;.  WCHAR *z
f6ed0 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20  WideFilename;.. 
f6ee0 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 79   nChar = MultiBy
f6ef0 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 5f  teToWideChar(CP_
f6f00 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61  UTF8, 0, zFilena
f6f10 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29  me, -1, NULL, 0)
f6f20 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  ;.  zWideFilenam
f6f30 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 61  e = malloc( nCha
f6f40 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46 69  r*sizeof(zWideFi
f6f50 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20  lename[0]) );.  
f6f60 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  if( zWideFilenam
f6f70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
f6f80 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 61  rn 0;.  }.  nCha
f6f90 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57  r = MultiByteToW
f6fa0 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 2c  ideChar(CP_UTF8,
f6fb0 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d   0, zFilename, -
f6fc0 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65  1, zWideFilename
f6fd0 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28 20  , nChar);.  if( 
f6fe0 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20  nChar==0 ){.    
f6ff0 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e 61  free(zWideFilena
f7000 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46 69  me);.    zWideFi
f7010 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a  lename = 0;.  }.
f7020 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46 69    return zWideFi
f7030 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  lename;.}../*.**
f7040 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f   Convert microso
f7050 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55 54  ft unicode to UT
f7060 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68  F-8.  Space to h
f7070 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64  old the returned
f7080 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f 62   string is.** ob
f7090 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
f70a0 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  oc()..*/.static 
f70b0 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 55  char *unicodeToU
f70c0 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52 20  tf8(const WCHAR 
f70d0 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b  *zWideFilename){
f70e0 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20  .  int nByte;.  
f70f0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b  char *zFilename;
f7100 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65  ..  nByte = Wide
f7110 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28  CharToMultiByte(
f7120 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 64  CP_UTF8, 0, zWid
f7130 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30  eFilename, -1, 0
f7140 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46  , 0, 0, 0);.  zF
f7150 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63  ilename = malloc
f7160 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28  ( nByte );.  if(
f7170 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b   zFilename==0 ){
f7180 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
f7190 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64   }.  nByte = Wid
f71a0 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
f71b0 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69  (CP_UTF8, 0, zWi
f71c0 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20  deFilename, -1, 
f71d0 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65  zFilename, nByte
f71e0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
f71f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7200 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79  0, 0);.  if( nBy
f7210 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66  te == 0 ){.    f
f7220 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ree(zFilename);.
f7230 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20      zFilename = 
f7240 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
f7250 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a  zFilename;.}../*
f7260 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 61  .** Convert an a
f7270 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d 69  nsi string to mi
f7280 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 2c  crosoft unicode,
f7290 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
f72a0 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67   current codepag
f72b0 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 66  e settings for f
f72c0 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a  ile apis..** .**
f72d0 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
f72e0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
f72f0 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a  ng is obtained.*
f7300 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a  * from malloc..*
f7310 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a  /.static WCHAR *
f7320 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63 6f  mbcsToUnicode(co
f7330 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
f7340 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ame){.  int nByt
f7350 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62 63  e;.  WCHAR *zMbc
f7360 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74  sFilename;.  int
f7370 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 46   codepage = AreF
f7380 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20  ileApisANSI() ? 
f7390 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43  CP_ACP : CP_OEMC
f73a0 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75  P;..  nByte = Mu
f73b0 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61  ltiByteToWideCha
f73c0 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a  r(codepage, 0, z
f73d0 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55  Filename, -1, NU
f73e0 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43 48  LL,0)*sizeof(WCH
f73f0 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c 65  AR);.  zMbcsFile
f7400 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  name = malloc( n
f7410 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 63  Byte*sizeof(zMbc
f7420 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b  sFilename[0]) );
f7430 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c 65  .  if( zMbcsFile
f7440 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  name==0 ){.    r
f7450 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
f7460 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74 65  Byte = MultiByte
f7470 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65 70  ToWideChar(codep
f7480 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d  age, 0, zFilenam
f7490 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c 65  e, -1, zMbcsFile
f74a0 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20 20  name, nByte);.  
f74b0 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a  if( nByte==0 ){.
f74c0 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46 69      free(zMbcsFi
f74d0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d 62  lename);.    zMb
f74e0 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a  csFilename = 0;.
f74f0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d 62    }.  return zMb
f7500 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f  csFilename;.}../
f7510 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63  *.** Convert mic
f7520 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 74  rosoft unicode t
f7530 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72  o multibyte char
f7540 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62 61  acter string, ba
f7550 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 73  sed on the.** us
f7560 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70 61  er's Ansi codepa
f7570 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20  ge..**.** Space 
f7580 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
f7590 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f  rned string is o
f75a0 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
f75b0 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61  malloc()..*/.sta
f75c0 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f 64  tic char *unicod
f75d0 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57 43  eToMbcs(const WC
f75e0 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61  HAR *zWideFilena
f75f0 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65  me){.  int nByte
f7600 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  ;.  char *zFilen
f7610 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65 70  ame;.  int codep
f7620 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70 69  age = AreFileApi
f7630 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43 50  sANSI() ? CP_ACP
f7640 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20   : CP_OEMCP;..  
f7650 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72  nByte = WideChar
f7660 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65  ToMultiByte(code
f7670 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69  page, 0, zWideFi
f7680 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30  lename, -1, 0, 0
f7690 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65  , 0, 0);.  zFile
f76a0 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  name = malloc( n
f76b0 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46  Byte );.  if( zF
f76c0 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ilename==0 ){.  
f76d0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
f76e0 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68    nByte = WideCh
f76f0 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f  arToMultiByte(co
f7700 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65  depage, 0, zWide
f7710 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46  Filename, -1, zF
f7720 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a  ilename, nByte,.
f7730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
f7750 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65   0);.  if( nByte
f7760 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65   == 0 ){.    fre
f7770 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
f7780 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b    zFilename = 0;
f7790 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46  .  }.  return zF
f77a0 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ilename;.}../*.*
f77b0 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69 62  * Convert multib
f77c0 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 74  yte character st
f77d0 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20 20  ring to UTF-8.  
f77e0 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
f77f0 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74  e.** returned st
f7800 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64  ring is obtained
f7810 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a   from malloc()..
f7820 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  */.SQLITE_API ch
f7830 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e 33  ar *sqlite3_win3
f7840 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 63  2_mbcs_to_utf8(c
f7850 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
f7860 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a  name){.  char *z
f7870 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20 20  FilenameUtf8;.  
f7880 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b  WCHAR *zTmpWide;
f7890 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 6d  ..  zTmpWide = m
f78a0 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69  bcsToUnicode(zFi
f78b0 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a  lename);.  if( z
f78c0 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20  TmpWide==0 ){.  
f78d0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
f78e0 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 20    zFilenameUtf8 
f78f0 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28  = unicodeToUtf8(
f7900 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 65  zTmpWide);.  fre
f7910 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72  e(zTmpWide);.  r
f7920 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 55  eturn zFilenameU
f7930 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  tf8;.}../*.** Co
f7940 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20 6d  nvert UTF-8 to m
f7950 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74  ultibyte charact
f7960 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61 63  er string.  Spac
f7970 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a 2a  e to hold the .*
f7980 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  * returned strin
f7990 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  g is obtained fr
f79a0 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  om malloc()..*/.
f79b0 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74 66  static char *utf
f79c0 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63 68  8ToMbcs(const ch
f79d0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
f79e0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d    char *zFilenam
f79f0 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20 2a  eMbcs;.  WCHAR *
f7a00 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d  zTmpWide;..  zTm
f7a10 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55 6e  pWide = utf8ToUn
f7a20 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29  icode(zFilename)
f7a30 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 65  ;.  if( zTmpWide
f7a40 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f7a50 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65  n 0;.  }.  zFile
f7a60 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f  nameMbcs = unico
f7a70 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64  deToMbcs(zTmpWid
f7a80 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70 57  e);.  free(zTmpW
f7a90 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  ide);.  return z
f7aa0 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a  FilenameMbcs;.}.
f7ab0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
f7ac0 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  INCE./**********
f7ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
f7b10 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20  ** This section 
f7b20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f  contains code fo
f7b30 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f  r WinCE only..*/
f7b40 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 45  ./*.** WindowsCE
f7b50 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61   does not have a
f7b60 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 6e   localtime() fun
f7b70 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74  ction.  So creat
f7b80 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 74  e a.** substitut
f7b90 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d 20  e..*/.struct tm 
f7ba0 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74 69  *__cdecl localti
f7bb0 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20  me(const time_t 
f7bc0 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20 73  *t).{.  static s
f7bd0 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46 49  truct tm y;.  FI
f7be0 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b  LETIME uTm, lTm;
f7bf0 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70 54  .  SYSTEMTIME pT
f7c00 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  m;.  sqlite3_int
f7c10 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d 20  64 t64;.  t64 = 
f7c20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36 34  *t;.  t64 = (t64
f7c30 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29 2a   + 11644473600)*
f7c40 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d 2e  10000000;.  uTm.
f7c50 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d 20  dwLowDateTime = 
f7c60 74 36 34 20 26 20 30 78 46 46 46 46 46 46 46 46  t64 & 0xFFFFFFFF
f7c70 3b 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44 61  ;.  uTm.dwHighDa
f7c80 74 65 54 69 6d 65 3d 20 74 36 34 20 3e 3e 20 33  teTime= t64 >> 3
f7c90 32 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c  2;.  FileTimeToL
f7ca0 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54  ocalFileTime(&uT
f7cb0 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54  m,&lTm);.  FileT
f7cc0 69 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28  imeToSystemTime(
f7cd0 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e  &lTm,&pTm);.  y.
f7ce0 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59  tm_year = pTm.wY
f7cf0 65 61 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e  ear - 1900;.  y.
f7d00 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f  tm_mon = pTm.wMo
f7d10 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f  nth - 1;.  y.tm_
f7d20 77 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f  wday = pTm.wDayO
f7d30 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64  fWeek;.  y.tm_md
f7d40 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20  ay = pTm.wDay;. 
f7d50 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d   y.tm_hour = pTm
f7d60 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d  .wHour;.  y.tm_m
f7d70 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65  in = pTm.wMinute
f7d80 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70  ;.  y.tm_sec = p
f7d90 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65  Tm.wSecond;.  re
f7da0 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54  turn &y;.}../* T
f7db0 68 69 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  his will never b
f7dc0 65 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65  e called, but de
f7dd0 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68  fined to make th
f7de0 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a  e code compile *
f7df0 2f 0a 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d  /.#define GetTem
f7e00 70 50 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65  pPathA(a,b)..#de
f7e10 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c  fine LockFile(a,
f7e20 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77  b,c,d,e)       w
f7e30 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c  inceLockFile(&a,
f7e40 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65   b, c, d, e).#de
f7e50 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  fine UnlockFile(
f7e60 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77  a,b,c,d,e)     w
f7e70 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26  inceUnlockFile(&
f7e80 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23  a, b, c, d, e).#
f7e90 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45  define LockFileE
f7ea0 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20  x(a,b,c,d,e,f)  
f7eb0 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78   winceLockFileEx
f7ec0 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c  (&a, b, c, d, e,
f7ed0 20 66 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e   f)..#define HAN
f7ee0 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61  DLE_TO_WINFILE(a
f7ef0 29 20 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63  ) (winFile*)&((c
f7f00 68 61 72 2a 29 61 29 5b 2d 6f 66 66 73 65 74 6f  har*)a)[-offseto
f7f10 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f  f(winFile,h)]../
f7f20 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 6c  *.** Acquire a l
f7f30 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61 6e 64 6c  ock on the handl
f7f40 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e h.*/.static vo
f7f50 69 64 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71  id winceMutexAcq
f7f60 75 69 72 65 28 48 41 4e 44 4c 45 20 68 29 7b 0a  uire(HANDLE h){.
f7f70 20 20 20 44 57 4f 52 44 20 64 77 45 72 72 3b 0a     DWORD dwErr;.
f7f80 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 64 77 45     do {.     dwE
f7f90 72 72 20 3d 20 57 61 69 74 46 6f 72 53 69 6e 67  rr = WaitForSing
f7fa0 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49 4e 46 49  leObject(h, INFI
f7fb0 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77 68 69 6c  NITE);.   } whil
f7fc0 65 20 28 64 77 45 72 72 20 21 3d 20 57 41 49 54  e (dwErr != WAIT
f7fd0 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20 64 77 45  _OBJECT_0 && dwE
f7fe0 72 72 20 21 3d 20 57 41 49 54 5f 41 42 41 4e 44  rr != WAIT_ABAND
f7ff0 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52  ONED);.}./*.** R
f8000 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b 20 61 63  elease a lock ac
f8010 71 75 69 72 65 64 20 62 79 20 77 69 6e 63 65 4d  quired by winceM
f8020 75 74 65 78 41 63 71 75 69 72 65 28 29 0a 2a 2f  utexAcquire().*/
f8030 0a 23 64 65 66 69 6e 65 20 77 69 6e 63 65 4d 75  .#define winceMu
f8040 74 65 78 52 65 6c 65 61 73 65 28 68 29 20 52 65  texRelease(h) Re
f8050 6c 65 61 73 65 4d 75 74 65 78 28 68 29 0a 0a 2f  leaseMutex(h)../
f8060 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20  *.** Create the 
f8070 6d 75 74 65 78 20 61 6e 64 20 73 68 61 72 65 64  mutex and shared
f8080 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f 72   memory used for
f8090 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 65 20   locking in the 
f80a0 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74  file.** descript
f80b0 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74  or pFile.*/.stat
f80c0 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 43 72 65  ic BOOL winceCre
f80d0 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 68  ateLock(const ch
f80e0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 77  ar *zFilename, w
f80f0 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  inFile *pFile){.
f8100 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20    WCHAR *zTok;. 
f8110 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65 20 3d 20   WCHAR *zName = 
f8120 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46  utf8ToUnicode(zF
f8130 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c  ilename);.  BOOL
f8140 20 62 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a 0a   bInit = TRUE;..
f8150 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
f8160 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61  the local lockda
f8170 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f  ta */.  ZeroMemo
f8180 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  ry(&pFile->local
f8190 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c 65 2d 3e  , sizeof(pFile->
f81a0 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52  local));..  /* R
f81b0 65 70 6c 61 63 65 20 74 68 65 20 62 61 63 6b 73  eplace the backs
f81c0 6c 61 73 68 65 73 20 66 72 6f 6d 20 74 68 65 20  lashes from the 
f81d0 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 6c 6f 77  filename and low
f81e0 65 72 63 61 73 65 20 69 74 0a 20 20 2a 2a 20 74  ercase it.  ** t
f81f0 6f 20 64 65 72 69 76 65 20 61 20 6d 75 74 65 78  o derive a mutex
f8200 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b   name. */.  zTok
f8210 20 3d 20 43 68 61 72 4c 6f 77 65 72 57 28 7a 4e   = CharLowerW(zN
f8220 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a  ame);.  for (;*z
f8230 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20  Tok;zTok++){.   
f8240 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c   if (*zTok == '\
f8250 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b  \') *zTok = '_';
f8260 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74  .  }..  /* Creat
f8270 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61 6d 65 64  e/open the named
f8280 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70 46 69 6c   mutex */.  pFil
f8290 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43 72 65 61  e->hMutex = Crea
f82a0 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c 2c 20 46  teMutexW(NULL, F
f82b0 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  ALSE, zName);.  
f82c0 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74  if (!pFile->hMut
f82d0 65 78 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 4e  ex){.    free(zN
f82e0 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
f82f0 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f   FALSE;.  }..  /
f8300 2a 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75  * Acquire the mu
f8310 74 65 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69  tex before conti
f8320 6e 75 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65  nuing */.  wince
f8330 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69  MutexAcquire(pFi
f8340 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a  le->hMutex);.  .
f8350 20 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e    /* Since the n
f8360 61 6d 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75  ames of named mu
f8370 74 65 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65  texes, semaphore
f8380 73 2c 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73  s, file mappings
f8390 20 65 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63   etc are .  ** c
f83a0 61 73 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74  ase-sensitive, t
f83b0 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66  ake advantage of
f83c0 20 74 68 61 74 20 62 79 20 75 70 70 65 72 63 61   that by upperca
f83d0 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e  sing the mutex n
f83e0 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69  ame.  ** and usi
f83f0 6e 67 20 74 68 61 74 20 61 73 20 74 68 65 20 73  ng that as the s
f8400 68 61 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e  hared filemappin
f8410 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43  g name..  */.  C
f8420 68 61 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29  harUpperW(zName)
f8430 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72  ;.  pFile->hShar
f8440 65 64 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d  ed = CreateFileM
f8450 61 70 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f  appingW(INVALID_
f8460 48 41 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55  HANDLE_VALUE, NU
f8470 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LL,.            
f8480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8490 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f             PAGE_
f84a0 52 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69  READWRITE, 0, si
f84b0 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c  zeof(winceLock),
f84c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
f84d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f84e0 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20          zName); 
f84f0 20 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c   ..  /* Set a fl
f8500 61 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65  ag that indicate
f8510 73 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73  s we're the firs
f8520 74 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  t to create the 
f8530 6d 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20  memory so it .  
f8540 2a 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d  ** must be zero-
f8550 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
f8560 20 69 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f   if (GetLastErro
f8570 72 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52  r() == ERROR_ALR
f8580 45 41 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20  EADY_EXISTS){.  
f8590 20 20 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b    bInit = FALSE;
f85a0 0a 20 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61  .  }..  free(zNa
f85b0 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65  me);..  /* If we
f85c0 20 73 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61   succeeded in ma
f85d0 6b 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  king the shared 
f85e0 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d  memory handle, m
f85f0 61 70 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28  ap it. */.  if (
f8600 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b  pFile->hShared){
f8610 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72  .    pFile->shar
f8620 65 64 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a  ed = (winceLock*
f8630 29 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70  )MapViewOfFile(p
f8640 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a  File->hShared, .
f8650 20 20 20 20 20 20 20 20 20 20 20 20 20 46 49 4c               FIL
f8660 45 5f 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f  E_MAP_READ|FILE_
f8670 4d 41 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c  MAP_WRITE, 0, 0,
f8680 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63   sizeof(winceLoc
f8690 6b 29 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d  k));.    /* If m
f86a0 61 70 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63  apping failed, c
f86b0 6c 6f 73 65 20 74 68 65 20 73 68 61 72 65 64 20  lose the shared 
f86c0 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e  memory handle an
f86d0 64 20 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20  d erase it */.  
f86e0 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68    if (!pFile->sh
f86f0 61 72 65 64 29 7b 0a 20 20 20 20 20 20 43 6c 6f  ared){.      Clo
f8700 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e  seHandle(pFile->
f8710 68 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20  hShared);.      
f8720 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d  pFile->hShared =
f8730 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d   NULL;.    }.  }
f8740 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61 72 65 64  ..  /* If shared
f8750 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 20 6e 6f   memory could no
f8760 74 20 62 65 20 63 72 65 61 74 65 64 2c 20 74 68  t be created, th
f8770 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 6d 75 74  en close the mut
f8780 65 78 20 61 6e 64 20 66 61 69 6c 20 2a 2f 0a 20  ex and fail */. 
f8790 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61   if (pFile->hSha
f87a0 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20  red == NULL){.  
f87b0 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65    winceMutexRele
f87c0 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  ase(pFile->hMute
f87d0 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e  x);.    CloseHan
f87e0 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  dle(pFile->hMute
f87f0 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68  x);.    pFile->h
f8800 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20  Mutex = NULL;.  
f8810 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a    return FALSE;.
f8820 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 74    }.  .  /* Init
f8830 69 61 6c 69 7a 65 20 74 68 65 20 73 68 61 72 65  ialize the share
f8840 64 20 6d 65 6d 6f 72 79 20 69 66 20 77 65 27 72  d memory if we'r
f8850 65 20 73 75 70 70 6f 73 65 64 20 74 6f 20 2a 2f  e supposed to */
f8860 0a 20 20 69 66 20 28 62 49 6e 69 74 29 20 7b 0a  .  if (bInit) {.
f8870 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 70      ZeroMemory(p
f8880 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c 20 73 69  File->shared, si
f8890 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29  zeof(winceLock))
f88a0 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75  ;.  }..  winceMu
f88b0 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65  texRelease(pFile
f88c0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74  ->hMutex);.  ret
f88d0 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a  urn TRUE;.}../*.
f88e0 2a 2a 20 44 65 73 74 72 6f 79 20 74 68 65 20 70  ** Destroy the p
f88f0 61 72 74 20 6f 66 20 77 69 6e 46 69 6c 65 20 74  art of winFile t
f8900 68 61 74 20 64 65 61 6c 73 20 77 69 74 68 20 77  hat deals with w
f8910 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74  ince locks.*/.st
f8920 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65 44  atic void winceD
f8930 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 69  estroyLock(winFi
f8940 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 66  le *pFile){.  if
f8950 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29   (pFile->hMutex)
f8960 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65  {.    /* Acquire
f8970 20 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20   the mutex */.  
f8980 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75    winceMutexAcqu
f8990 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  ire(pFile->hMute
f89a0 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20  x);..    /* The 
f89b0 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 73  following blocks
f89c0 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79   should probably
f89d0 20 61 73 73 65 72 74 20 69 6e 20 64 65 62 75 67   assert in debug
f89e0 20 6d 6f 64 65 2c 20 62 75 74 20 74 68 65 79 0a   mode, but they.
f89f0 20 20 20 20 20 20 20 61 72 65 20 74 6f 20 63 6c         are to cl
f8a00 65 61 6e 75 70 20 69 6e 20 63 61 73 65 20 61 6e  eanup in case an
f8a10 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69 6e 65 64  y locks remained
f8a20 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69 66 20   open */.    if 
f8a30 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52  (pFile->local.nR
f8a40 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 70  eaders){.      p
f8a50 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52  File->shared->nR
f8a60 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 7d  eaders --;.    }
f8a70 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f8a80 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29  local.bReserved)
f8a90 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  {.      pFile->s
f8aa0 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64  hared->bReserved
f8ab0 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a   = FALSE;.    }.
f8ac0 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c      if (pFile->l
f8ad0 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0a  ocal.bPending){.
f8ae0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
f8af0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20  red->bPending = 
f8b00 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20  FALSE;.    }.   
f8b10 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61   if (pFile->loca
f8b20 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20  l.bExclusive){. 
f8b30 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
f8b40 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d  ed->bExclusive =
f8b50 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a 20   FALSE;.    }.. 
f8b60 20 20 20 2f 2a 20 44 65 2d 72 65 66 65 72 65 6e     /* De-referen
f8b70 63 65 20 61 6e 64 20 63 6c 6f 73 65 20 6f 75 72  ce and close our
f8b80 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 68 61   copy of the sha
f8b90 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c  red memory handl
f8ba0 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61 70 56 69  e */.    UnmapVi
f8bb0 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e  ewOfFile(pFile->
f8bc0 73 68 61 72 65 64 29 3b 0a 20 20 20 20 43 6c 6f  shared);.    Clo
f8bd0 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e  seHandle(pFile->
f8be0 68 53 68 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f  hShared);..    /
f8bf0 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 68 65 20  * Done with the 
f8c00 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e  mutex */.    win
f8c10 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70  ceMutexRelease(p
f8c20 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20  File->hMutex);  
f8c30 20 20 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64    .    CloseHand
f8c40 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  le(pFile->hMutex
f8c50 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d  );.    pFile->hM
f8c60 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d  utex = NULL;.  }
f8c70 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d  .}../* .** An im
f8c80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
f8c90 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20 41  the LockFile() A
f8ca0 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f  PI of windows fo
f8cb0 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69  r wince.*/.stati
f8cc0 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b  c BOOL winceLock
f8cd0 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a  File(.  HANDLE *
f8ce0 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20  phFile,.  DWORD 
f8cf0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c  dwFileOffsetLow,
f8d00 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f  .  DWORD dwFileO
f8d10 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f  ffsetHigh,.  DWO
f8d20 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  RD nNumberOfByte
f8d30 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57  sToLockLow,.  DW
f8d40 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
f8d50 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a  esToLockHigh.){.
f8d60 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
f8d70 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e   = HANDLE_TO_WIN
f8d80 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20  FILE(phFile);.  
f8d90 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46  BOOL bReturn = F
f8da0 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46  ALSE;..  if (!pF
f8db0 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74  ile->hMutex) ret
f8dc0 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63  urn TRUE;.  winc
f8dd0 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46  eMutexAcquire(pF
f8de0 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20  ile->hMutex);.. 
f8df0 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 65   /* Wanting an e
f8e00 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a  xclusive lock? *
f8e10 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66  /.  if (dwFileOf
f8e20 66 73 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52 45  fsetLow == SHARE
f8e30 44 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 26  D_FIRST.       &
f8e40 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  & nNumberOfBytes
f8e50 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41  ToLockLow == SHA
f8e60 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69  RED_SIZE){.    i
f8e70 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  f (pFile->shared
f8e80 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 20  ->nReaders == 0 
f8e90 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  && pFile->shared
f8ea0 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20  ->bExclusive == 
f8eb0 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 65  0){.       pFile
f8ec0 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75  ->shared->bExclu
f8ed0 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20  sive = TRUE;.   
f8ee0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
f8ef0 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52  .bExclusive = TR
f8f00 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74 75  UE;.       bRetu
f8f10 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d  rn = TRUE;.    }
f8f20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20  .  }..  /* Want 
f8f30 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b  a read-only lock
f8f40 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  ? */.  else if (
f8f50 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77  (dwFileOffsetLow
f8f60 20 3e 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54   >= SHARED_FIRST
f8f70 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20   &&.            
f8f80 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
f8f90 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b  < SHARED_FIRST +
f8fa0 20 53 48 41 52 45 44 5f 53 49 5a 45 29 20 26 26   SHARED_SIZE) &&
f8fb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75  .            nNu
f8fc0 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63  mberOfBytesToLoc
f8fd0 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20  kLow == 1){.    
f8fe0 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65  if (pFile->share
f8ff0 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d  d->bExclusive ==
f9000 20 30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65   0){.      pFile
f9010 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73  ->local.nReaders
f9020 20 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70   ++;.      if (p
f9030 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
f9040 64 65 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20  ders == 1){.    
f9050 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
f9060 64 2d 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a  d->nReaders ++;.
f9070 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52        }.      bR
f9080 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20  eturn = TRUE;.  
f9090 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61    }.  }..  /* Wa
f90a0 6e 74 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63  nt a pending loc
f90b0 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20  k? */.  else if 
f90c0 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77  (dwFileOffsetLow
f90d0 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45   == PENDING_BYTE
f90e0 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74   && nNumberOfByt
f90f0 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31  esToLockLow == 1
f9100 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20  ){.    /* If no 
f9110 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73  pending lock has
f9120 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20   been acquired, 
f9130 74 68 65 6e 20 61 63 71 75 69 72 65 20 69 74 20  then acquire it 
f9140 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65  */.    if (pFile
f9150 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69  ->shared->bPendi
f9160 6e 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20  ng == 0) {.     
f9170 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
f9180 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b  bPending = TRUE;
f9190 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
f91a0 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54  cal.bPending = T
f91b0 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75  RUE;.      bRetu
f91c0 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d  rn = TRUE;.    }
f91d0 0a 20 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20 61  .  }.  /* Want a
f91e0 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20   reserved lock? 
f91f0 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77  */.  else if (dw
f9200 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d  FileOffsetLow ==
f9210 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 26   RESERVED_BYTE &
f9220 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  & nNumberOfBytes
f9230 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b  ToLockLow == 1){
f9240 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f9250 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65  shared->bReserve
f9260 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20  d == 0) {.      
f9270 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
f9280 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b  Reserved = TRUE;
f9290 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
f92a0 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20  cal.bReserved = 
f92b0 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74  TRUE;.      bRet
f92c0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
f92d0 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75  }.  }..  winceMu
f92e0 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65  texRelease(pFile
f92f0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74  ->hMutex);.  ret
f9300 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a  urn bReturn;.}..
f9310 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65  /*.** An impleme
f9320 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 55  ntation of the U
f9330 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f 66  nlockFile API of
f9340 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e   windows for win
f9350 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f  ce.*/.static BOO
f9360 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c  L winceUnlockFil
f9370 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46  e(.  HANDLE *phF
f9380 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46  ile,.  DWORD dwF
f9390 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20  ileOffsetLow,.  
f93a0 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73  DWORD dwFileOffs
f93b0 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20  etHigh,.  DWORD 
f93c0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f93d0 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f  UnlockLow,.  DWO
f93e0 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  RD nNumberOfByte
f93f0 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b  sToUnlockHigh.){
f9400 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
f9410 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49  e = HANDLE_TO_WI
f9420 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20  NFILE(phFile);. 
f9430 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20   BOOL bReturn = 
f9440 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70  FALSE;..  if (!p
f9450 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65  File->hMutex) re
f9460 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e  turn TRUE;.  win
f9470 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70  ceMutexAcquire(p
f9480 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a  File->hMutex);..
f9490 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61    /* Releasing a
f94a0 20 72 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20   reader lock or 
f94b0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
f94c0 6b 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c  k */.  if (dwFil
f94d0 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53 48  eOffsetLow >= SH
f94e0 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20  ARED_FIRST &&.  
f94f0 20 20 20 20 20 64 77 46 69 6c 65 4f 66 66 73 65       dwFileOffse
f9500 74 4c 6f 77 20 3c 20 53 48 41 52 45 44 5f 46 49  tLow < SHARED_FI
f9510 52 53 54 20 2b 20 53 48 41 52 45 44 5f 53 49 5a  RST + SHARED_SIZ
f9520 45 29 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77  E){.    /* Did w
f9530 65 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73  e have an exclus
f9540 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20  ive lock? */.   
f9550 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61   if (pFile->loca
f9560 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20  l.bExclusive){. 
f9570 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
f9580 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46  l.bExclusive = F
f9590 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c  ALSE;.      pFil
f95a0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c  e->shared->bExcl
f95b0 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20  usive = FALSE;. 
f95c0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54       bReturn = T
f95d0 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  RUE;.    }..    
f95e0 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 74 20 68  /* Did we just h
f95f0 61 76 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63  ave a reader loc
f9600 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69  k? */.    else i
f9610 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
f9620 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20  nReaders){.     
f9630 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52   pFile->local.nR
f9640 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20  eaders --;.     
f9650 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61   if (pFile->loca
f9660 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 29  l.nReaders == 0)
f9670 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
f9680 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
f9690 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20  nReaders --;.   
f96a0 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75     }.      bRetu
f96b0 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d  rn = TRUE;.    }
f96c0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61  .  }..  /* Relea
f96d0 73 69 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c  sing a pending l
f96e0 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ock */.  else if
f96f0 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f   (dwFileOffsetLo
f9700 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54  w == PENDING_BYT
f9710 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79  E && nNumberOfBy
f9720 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d  tesToUnlockLow =
f9730 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46  = 1){.    if (pF
f9740 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64  ile->local.bPend
f9750 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  ing){.      pFil
f9760 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e  e->local.bPendin
f9770 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20  g = FALSE;.     
f9780 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
f9790 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45  bPending = FALSE
f97a0 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  ;.      bReturn 
f97b0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
f97c0 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67  }.  /* Releasing
f97d0 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
f97e0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
f97f0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
f9800 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20  = RESERVED_BYTE 
f9810 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
f9820 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20  sToUnlockLow == 
f9830 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c  1){.    if (pFil
f9840 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76  e->local.bReserv
f9850 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c  ed) {.      pFil
f9860 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76  e->local.bReserv
f9870 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ed = FALSE;.    
f9880 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f9890 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c  >bReserved = FAL
f98a0 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72  SE;.      bRetur
f98b0 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a  n = TRUE;.    }.
f98c0 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65    }..  winceMute
f98d0 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e  xRelease(pFile->
f98e0 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  hMutex);.  retur
f98f0 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  n bReturn;.}../*
f9900 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74  .** An implement
f9910 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63  ation of the Loc
f9920 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20 6f 66  kFileEx() API of
f9930 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e   windows for win
f9940 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f  ce.*/.static BOO
f9950 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45  L winceLockFileE
f9960 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46  x(.  HANDLE *phF
f9970 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46  ile,.  DWORD dwF
f9980 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 64 77  lags,.  DWORD dw
f9990 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57 4f 52  Reserved,.  DWOR
f99a0 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  D nNumberOfBytes
f99b0 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f  ToLockLow,.  DWO
f99c0 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  RD nNumberOfByte
f99d0 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20 4c  sToLockHigh,.  L
f99e0 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70 4f 76  POVERLAPPED lpOv
f99f0 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f 2a  erlapped.){.  /*
f9a00 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 77   If the caller w
f9a10 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72 65  ants a shared re
f9a20 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 64  ad lock, forward
f9a30 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a 20   this call.  ** 
f9a40 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65  to winceLockFile
f9a50 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65 72   */.  if (lpOver
f9a60 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 3d  lapped->Offset =
f9a70 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 26  = SHARED_FIRST &
f9a80 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 73 20  &.      dwFlags 
f9a90 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 6e 4e  == 1 &&.      nN
f9aa0 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f  umberOfBytesToLo
f9ab0 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f  ckLow == SHARED_
f9ac0 53 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72  SIZE){.    retur
f9ad0 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28  n winceLockFile(
f9ae0 70 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46  phFile, SHARED_F
f9af0 49 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a  IRST, 0, 1, 0);.
f9b00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c    }.  return FAL
f9b10 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20  SE;.}./*.** End 
f9b20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 63  of the special c
f9b30 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a  ode for wince.**
f9b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e  ***********/.#en
f9b90 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53  dif /* SQLITE_OS
f9ba0 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _WINCE */../****
f9bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  *********.** The
f9c00 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72   next group of r
f9c10 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e  outines implemen
f9c20 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64  t the I/O method
f9c30 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62  s specified.** b
f9c40 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f  y the sqlite3_io
f9c50 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e  _methods object.
f9c60 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
f9c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
f9cb0 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
f9cc0 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  file..**.** It i
f9cd0 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20  s reported that 
f9ce0 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c  an attempt to cl
f9cf0 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67  ose a handle mig
f9d00 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20  ht sometimes.** 
f9d10 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61  fail.  This is a
f9d20 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62   very unreasonab
f9d30 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77  le result, but w
f9d40 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69  indows is notori
f9d50 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67  ous.** for being
f9d60 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f   unreasonable so
f9d70 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20   I do not doubt 
f9d80 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61  that it might ha
f9d90 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65  ppen.  If.** the
f9da0 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65   close fails, we
f9db0 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d   pause for 100 m
f9dc0 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20  illiseconds and 
f9dd0 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a  try again.  As.*
f9de0 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f  * many as MX_CLO
f9df0 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d  SE_ATTEMPT attem
f9e00 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65  pts to close the
f9e10 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65   handle are made
f9e20 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e   before.** givin
f9e30 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69  g up and returni
f9e40 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  ng an error..*/.
f9e50 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45  #define MX_CLOSE
f9e60 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69  _ATTEMPT 3.stati
f9e70 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73  c int winClose(s
f9e80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
f9e90 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20  {.  int rc, cnt 
f9ea0 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a  = 0;.  winFile *
f9eb0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
f9ec0 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 32  *)id;.  OSTRACE2
f9ed0 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70  ("CLOSE %d\n", p
f9ee0 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 64 6f 7b 0a  File->h);.  do{.
f9ef0 20 20 20 20 72 63 20 3d 20 43 6c 6f 73 65 48 61      rc = CloseHa
f9f00 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a  ndle(pFile->h);.
f9f10 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 30 20    }while( rc==0 
f9f20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c  && ++cnt < MX_CL
f9f30 4f 53 45 5f 41 54 54 45 4d 50 54 20 26 26 20 28  OSE_ATTEMPT && (
f9f40 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 29  Sleep(100), 1) )
f9f50 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ;.#if SQLITE_OS_
f9f60 57 49 4e 43 45 0a 23 64 65 66 69 6e 65 20 57 49  WINCE.#define WI
f9f70 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54  NCE_DELETION_ATT
f9f80 45 4d 50 54 53 20 33 0a 20 20 77 69 6e 63 65 44  EMPTS 3.  winceD
f9f90 65 73 74 72 6f 79 4c 6f 63 6b 28 70 46 69 6c 65  estroyLock(pFile
f9fa0 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  );.  if( pFile->
f9fb0 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 29  zDeleteOnClose )
f9fc0 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20  {.    int cnt = 
f9fd0 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 0a 20 20  0;.    while(.  
f9fe0 20 20 20 20 20 20 20 20 20 44 65 6c 65 74 65 46           DeleteF
f9ff0 69 6c 65 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c  ileW(pFile->zDel
fa000 65 74 65 4f 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20  eteOnClose)==0. 
fa010 20 20 20 20 20 20 20 26 26 20 47 65 74 46 69 6c         && GetFil
fa020 65 41 74 74 72 69 62 75 74 65 73 57 28 70 46 69  eAttributesW(pFi
fa030 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f  le->zDeleteOnClo
fa040 73 65 29 21 3d 30 78 66 66 66 66 66 66 66 66 20  se)!=0xffffffff 
fa050 0a 20 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b  .        && cnt+
fa060 2b 20 3c 20 57 49 4e 43 45 5f 44 45 4c 45 54 49  + < WINCE_DELETI
fa070 4f 4e 5f 41 54 54 45 4d 50 54 53 0a 20 20 20 20  ON_ATTEMPTS.    
fa080 29 7b 0a 20 20 20 20 20 20 20 53 6c 65 65 70 28  ){.       Sleep(
fa090 31 30 30 29 3b 20 20 2f 2a 20 57 61 69 74 20 61  100);  /* Wait a
fa0a0 20 6c 69 74 74 6c 65 20 62 65 66 6f 72 65 20 74   little before t
fa0b0 72 79 69 6e 67 20 61 67 61 69 6e 20 2a 2f 0a 20  rying again */. 
fa0c0 20 20 20 7d 0a 20 20 20 20 66 72 65 65 28 70 46     }.    free(pF
fa0d0 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c  ile->zDeleteOnCl
fa0e0 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ose);.  }.#endif
fa0f0 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d  .  OpenCounter(-
fa100 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20  1);.  return rc 
fa110 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51  ? SQLITE_OK : SQ
fa120 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f  LITE_IOERR;.}../
fa130 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73  *.** Some micros
fa140 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61  oft compilers la
fa150 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74 69  ck this definiti
fa160 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49  on..*/.#ifndef I
fa170 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f  NVALID_SET_FILE_
fa180 50 4f 49 4e 54 45 52 0a 23 20 64 65 66 69 6e 65  POINTER.# define
fa190 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c   INVALID_SET_FIL
fa1a0 45 5f 50 4f 49 4e 54 45 52 20 28 28 44 57 4f 52  E_POINTER ((DWOR
fa1b0 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  D)-1).#endif../*
fa1c0 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72  .** Read data fr
fa1d0 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61  om a file into a
fa1e0 20 62 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e   buffer.  Return
fa1f0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c   SQLITE_OK if al
fa200 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20  l.** bytes were 
fa210 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c  read successfull
fa220 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45  y and SQLITE_IOE
fa230 52 52 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67  RR if anything g
fa240 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f  oes.** wrong..*/
fa250 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 52  .static int winR
fa260 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66  ead(.  sqlite3_f
fa270 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20  ile *id,        
fa280 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65 61    /* File to rea
fa290 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64  d from */.  void
fa2a0 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20   *pBuf,         
fa2b0 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
fa2c0 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69  content into thi
fa2d0 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e  s buffer */.  in
fa2e0 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20  t amt,          
fa2f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
fa300 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72  er of bytes to r
fa310 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ead */.  sqlite3
fa320 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20  _int64 offset   
fa330 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61      /* Begin rea
fa340 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66  ding at this off
fa350 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47  set */.){.  LONG
fa360 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f   upperBits = (LO
fa370 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 29  NG)((offset>>32)
fa380 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a   & 0x7fffffff);.
fa390 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73    LONG lowerBits
fa3a0 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74   = (LONG)(offset
fa3b0 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a   & 0xffffffff);.
fa3c0 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57    DWORD rc;.  DW
fa3d0 4f 52 44 20 67 6f 74 3b 0a 20 20 77 69 6e 46 69  ORD got;.  winFi
fa3e0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
fa3f0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65  File*)id;.  asse
fa400 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53  rt( id!=0 );.  S
fa410 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72  imulateIOError(r
fa420 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
fa430 52 52 5f 52 45 41 44 29 3b 0a 20 20 4f 53 54 52  RR_READ);.  OSTR
fa440 41 43 45 33 28 22 52 45 41 44 20 25 64 20 6c 6f  ACE3("READ %d lo
fa450 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ck=%d\n", pFile-
fa460 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
fa470 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74  ype);.  rc = Set
fa480 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c  FilePointer(pFil
fa490 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c  e->h, lowerBits,
fa4a0 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c   &upperBits, FIL
fa4b0 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20  E_BEGIN);.  if( 
fa4c0 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f  rc==INVALID_SET_
fa4d0 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20  FILE_POINTER && 
fa4e0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 21 3d  GetLastError()!=
fa4f0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
fa500 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55  return SQLITE_FU
fa510 4c 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52  LL;.  }.  if( !R
fa520 65 61 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  eadFile(pFile->h
fa530 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f  , pBuf, amt, &go
fa540 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74  t, 0) ){.    ret
fa550 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
fa560 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28  _READ;.  }.  if(
fa570 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74   got==(DWORD)amt
fa580 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
fa590 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
fa5a0 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64  e{.    /* Unread
fa5b0 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75   parts of the bu
fa5c0 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72  ffer must be zer
fa5d0 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20  o-filled */.    
fa5e0 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29  memset(&((char*)
fa5f0 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61  pBuf)[got], 0, a
fa600 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74  mt-got);.    ret
fa610 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
fa620 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d  _SHORT_READ;.  }
fa630 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
fa640 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66  data from a buff
fa650 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20  er into a file. 
fa660 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
fa670 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20  K on success.** 
fa680 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72  or some other er
fa690 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c  ror code on fail
fa6a0 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
fa6b0 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73  nt winWrite(.  s
fa6c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fa6d0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
fa6e0 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a   to write into *
fa6f0 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
fa700 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a  pBuf,         /*
fa710 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 65   The bytes to be
fa720 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e   written */.  in
fa730 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20  t amt,          
fa740 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
fa750 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72  r of bytes to wr
fa760 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ite */.  sqlite3
fa770 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20  _int64 offset   
fa780 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
fa790 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65  o the file to be
fa7a0 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a  gin writing at *
fa7b0 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65  /.){.  LONG uppe
fa7c0 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28  rBits = (LONG)((
fa7d0 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78  offset>>32) & 0x
fa7e0 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e  7fffffff);.  LON
fa7f0 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c  G lowerBits = (L
fa800 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 78  ONG)(offset & 0x
fa810 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f  ffffffff);.  DWO
fa820 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 77  RD rc;.  DWORD w
fa830 72 6f 74 65 20 3d 20 30 3b 0a 20 20 77 69 6e 46  rote = 0;.  winF
fa840 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
fa850 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73  nFile*)id;.  ass
fa860 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
fa870 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
fa880 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
fa890 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69  ERR_WRITE);.  Si
fa8a0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
fa8b0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
fa8c0 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41  E_FULL);.  OSTRA
fa8d0 43 45 33 28 22 57 52 49 54 45 20 25 64 20 6c 6f  CE3("WRITE %d lo
fa8e0 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ck=%d\n", pFile-
fa8f0 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
fa900 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74  ype);.  rc = Set
fa910 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c  FilePointer(pFil
fa920 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c  e->h, lowerBits,
fa930 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c   &upperBits, FIL
fa940 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20  E_BEGIN);.  if( 
fa950 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f  rc==INVALID_SET_
fa960 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20  FILE_POINTER && 
fa970 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 21 3d  GetLastError()!=
fa980 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
fa990 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55  return SQLITE_FU
fa9a0 4c 4c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  LL;.  }.  assert
fa9b0 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69  ( amt>0 );.  whi
fa9c0 6c 65 28 0a 20 20 20 20 20 61 6d 74 3e 30 0a 20  le(.     amt>0. 
fa9d0 20 20 20 20 26 26 20 28 72 63 20 3d 20 57 72 69      && (rc = Wri
fa9e0 74 65 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  teFile(pFile->h,
fa9f0 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f   pBuf, amt, &wro
faa00 74 65 2c 20 30 29 29 21 3d 30 0a 20 20 20 20 20  te, 0))!=0.     
faa10 26 26 20 77 72 6f 74 65 3e 30 0a 20 20 29 7b 0a  && wrote>0.  ){.
faa20 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65      amt -= wrote
faa30 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28  ;.    pBuf = &((
faa40 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74  char*)pBuf)[wrot
faa50 65 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 72  e];.  }.  if( !r
faa60 63 20 7c 7c 20 61 6d 74 3e 28 69 6e 74 29 77 72  c || amt>(int)wr
faa70 6f 74 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ote ){.    retur
faa80 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
faa90 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
faaa0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
faab0 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e  Truncate an open
faac0 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69   file to a speci
faad0 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61  fied size.*/.sta
faae0 74 69 63 20 69 6e 74 20 77 69 6e 54 72 75 6e 63  tic int winTrunc
faaf0 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ate(sqlite3_file
fab00 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e   *id, sqlite3_in
fab10 74 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 44 57  t64 nByte){.  DW
fab20 4f 52 44 20 72 63 3b 0a 20 20 4c 4f 4e 47 20 75  ORD rc;.  LONG u
fab30 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47  pperBits = (LONG
fab40 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 20 26 20  )((nByte>>32) & 
fab50 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c  0x7fffffff);.  L
fab60 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20  ONG lowerBits = 
fab70 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26 20 30  (LONG)(nByte & 0
fab80 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 77 69  xffffffff);.  wi
fab90 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
faba0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f  winFile*)id;.  O
fabb0 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 41 54  STRACE3("TRUNCAT
fabc0 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46  E %d %lld\n", pF
fabd0 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b 0a  ile->h, nByte);.
fabe0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
fabf0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
fac00 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29 3b  IOERR_TRUNCATE);
fac10 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50  .  rc = SetFileP
fac20 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c  ointer(pFile->h,
fac30 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70   lowerBits, &upp
fac40 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47  erBits, FILE_BEG
fac50 49 4e 29 3b 0a 20 20 69 66 28 20 49 4e 56 41 4c  IN);.  if( INVAL
fac60 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e  ID_SET_FILE_POIN
fac70 54 45 52 20 21 3d 20 72 63 20 29 7b 0a 20 20 20  TER != rc ){.   
fac80 20 2f 2a 20 53 65 74 45 6e 64 4f 66 46 69 6c 65   /* SetEndOfFile
fac90 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 6e 42   will fail if nB
faca0 79 74 65 20 69 73 20 6e 65 67 61 74 69 76 65 20  yte is negative 
facb0 2a 2f 0a 20 20 20 20 69 66 28 20 53 65 74 45 6e  */.    if( SetEn
facc0 64 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  dOfFile(pFile->h
facd0 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
face0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
facf0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
fad00 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55  SQLITE_IOERR_TRU
fad10 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69 66 64 65 66  NCATE;.}..#ifdef
fad20 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a   SQLITE_TEST./*.
fad30 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d  ** Count the num
fad40 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73  ber of fullsyncs
fad50 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63   and normal sync
fad60 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  s.  This is used
fad70 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 74   to test.** that
fad80 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73   syncs and fulls
fad90 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72 69 6e  yncs are occurin
fada0 67 20 61 74 20 74 68 65 20 72 69 67 68 74 20 74  g at the right t
fadb0 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  imes..*/.SQLITE_
fadc0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
fadd0 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  sync_count = 0;.
fade0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
fadf0 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f  qlite3_fullsync_
fae00 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69  count = 0;.#endi
fae10 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  f../*.** Make su
fae20 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f  re all writes to
fae30 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69   a particular fi
fae40 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64  le are committed
fae50 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61   to disk..*/.sta
fae60 74 69 63 20 69 6e 74 20 77 69 6e 53 79 6e 63 28  tic int winSync(
fae70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fae80 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 23 69  , int flags){.#i
fae90 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f  fndef SQLITE_NO_
faea0 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c 65 20 2a  SYNC.  winFile *
faeb0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
faec0 2a 29 69 64 3b 0a 23 65 6c 73 65 0a 20 20 55 4e  *)id;.#else.  UN
faed0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69  USED_PARAMETER(i
faee0 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 4f 53 54  d);.#endif.  OST
faef0 52 41 43 45 33 28 22 53 59 4e 43 20 25 64 20 6c  RACE3("SYNC %d l
faf00 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ock=%d\n", pFile
faf10 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
faf20 74 79 70 65 29 3b 0a 23 69 66 6e 64 65 66 20 53  type);.#ifndef S
faf30 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 55 4e 55  QLITE_TEST.  UNU
faf40 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 6c  SED_PARAMETER(fl
faf50 61 67 73 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66  ags);.#else.  if
faf60 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
faf70 5f 53 59 4e 43 5f 46 55 4c 4c 20 29 7b 0a 20 20  _SYNC_FULL ){.  
faf80 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79    sqlite3_fullsy
faf90 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a  nc_count++;.  }.
fafa0 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63    sqlite3_sync_c
fafb0 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
fafc0 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c   /* If we compil
fafd0 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ed with the SQLI
fafe0 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c  TE_NO_SYNC flag,
faff0 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73   then syncing is
fb000 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20   a.  ** no-op.  
fb010 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
fb020 5f 4e 4f 5f 53 59 4e 43 0a 20 20 20 20 72 65 74  _NO_SYNC.    ret
fb030 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23  urn SQLITE_OK;.#
fb040 65 6c 73 65 0a 20 20 69 66 28 20 46 6c 75 73 68  else.  if( Flush
fb050 46 69 6c 65 42 75 66 66 65 72 73 28 70 46 69 6c  FileBuffers(pFil
fb060 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 72 65 74  e->h) ){.    ret
fb070 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
fb080 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
fb090 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
fb0a0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
fb0b0 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74  *.** Determine t
fb0c0 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
fb0d0 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74  of a file in byt
fb0e0 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  es.*/.static int
fb0f0 20 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c   winFileSize(sql
fb100 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73  ite3_file *id, s
fb110 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53  qlite3_int64 *pS
fb120 69 7a 65 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20  ize){.  winFile 
fb130 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
fb140 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 75  e*)id;.  DWORD u
fb150 70 70 65 72 42 69 74 73 2c 20 6c 6f 77 65 72 42  pperBits, lowerB
fb160 69 74 73 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  its;.  SimulateI
fb170 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51  OError(return SQ
fb180 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
fb190 29 3b 0a 20 20 6c 6f 77 65 72 42 69 74 73 20 3d  );.  lowerBits =
fb1a0 20 47 65 74 46 69 6c 65 53 69 7a 65 28 70 46 69   GetFileSize(pFi
fb1b0 6c 65 2d 3e 68 2c 20 26 75 70 70 65 72 42 69 74  le->h, &upperBit
fb1c0 73 29 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28  s);.  *pSize = (
fb1d0 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  ((sqlite3_int64)
fb1e0 75 70 70 65 72 42 69 74 73 29 3c 3c 33 32 29 20  upperBits)<<32) 
fb1f0 2b 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 72  + lowerBits;.  r
fb200 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
fb210 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49  .}../*.** LOCKFI
fb220 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54  LE_FAIL_IMMEDIAT
fb230 45 4c 59 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ELY is undefined
fb240 20 6f 6e 20 73 6f 6d 65 20 57 69 6e 64 6f 77 73   on some Windows
fb250 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66   systems..*/.#if
fb260 6e 64 65 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 41  ndef LOCKFILE_FA
fb270 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 0a 23  IL_IMMEDIATELY.#
fb280 20 64 65 66 69 6e 65 20 4c 4f 43 4b 46 49 4c 45   define LOCKFILE
fb290 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c  _FAIL_IMMEDIATEL
fb2a0 59 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  Y 1.#endif../*.*
fb2b0 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64  * Acquire a read
fb2c0 65 72 20 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66  er lock..** Diff
fb2d0 65 72 65 6e 74 20 41 50 49 20 72 6f 75 74 69 6e  erent API routin
fb2e0 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 64 65  es are called de
fb2f0 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68  pending on wheth
fb300 65 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 0a 2a  er or not this.*
fb310 2a 20 69 73 20 57 69 6e 39 35 20 6f 72 20 57 69  * is Win95 or Wi
fb320 6e 4e 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  nNT..*/.static i
fb330 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 77  nt getReadLock(w
fb340 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  inFile *pFile){.
fb350 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28    int res;.  if(
fb360 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 4f   isNT() ){.    O
fb370 56 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b 0a  VERLAPPED ovlp;.
fb380 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20      ovlp.Offset 
fb390 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a  = SHARED_FIRST;.
fb3a0 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48      ovlp.OffsetH
fb3b0 69 67 68 20 3d 20 30 3b 0a 20 20 20 20 6f 76 6c  igh = 0;.    ovl
fb3c0 70 2e 68 45 76 65 6e 74 20 3d 20 30 3b 0a 20 20  p.hEvent = 0;.  
fb3d0 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65    res = LockFile
fb3e0 45 78 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43  Ex(pFile->h, LOC
fb3f0 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44  KFILE_FAIL_IMMED
fb400 49 41 54 45 4c 59 2c 0a 20 20 20 20 20 20 20 20  IATELY,.        
fb410 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
fb420 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20  SHARED_SIZE, 0, 
fb430 26 6f 76 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54 28  &ovlp);./* isNT(
fb440 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
fb450 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
fb460 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
fb470 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
fb480 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
fb490 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65  WINCE==0.  }else
fb4a0 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 3b 0a 20 20  {.    int lk;.  
fb4b0 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
fb4c0 6e 65 73 73 28 73 69 7a 65 6f 66 28 6c 6b 29 2c  ness(sizeof(lk),
fb4d0 20 26 6c 6b 29 3b 0a 20 20 20 20 70 46 69 6c 65   &lk);.    pFile
fb4e0 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  ->sharedLockByte
fb4f0 20 3d 20 28 73 68 6f 72 74 29 28 28 6c 6b 20 26   = (short)((lk &
fb500 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53 48   0x7fffffff)%(SH
fb510 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 29 3b  ARED_SIZE - 1));
fb520 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46  .    res = LockF
fb530 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48  ile(pFile->h, SH
fb540 41 52 45 44 5f 46 49 52 53 54 2b 70 46 69 6c 65  ARED_FIRST+pFile
fb550 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  ->sharedLockByte
fb560 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64  , 0, 1, 0);.#end
fb570 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  if.  }.  return 
fb580 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e  res;.}../*.** Un
fb590 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f  do a readlock.*/
fb5a0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f  .static int unlo
fb5b0 63 6b 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69  ckReadLock(winFi
fb5c0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e  le *pFile){.  in
fb5d0 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e  t res;.  if( isN
fb5e0 54 28 29 20 29 7b 0a 20 20 20 20 72 65 73 20 3d  T() ){.    res =
fb5f0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   UnlockFile(pFil
fb600 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52  e->h, SHARED_FIR
fb610 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49  ST, 0, SHARED_SI
fb620 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69 73 4e 54 28  ZE, 0);./* isNT(
fb630 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
fb640 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
fb650 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
fb660 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
fb670 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
fb680 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65  WINCE==0.  }else
fb690 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f  {.    res = Unlo
fb6a0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
fb6b0 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20   SHARED_FIRST + 
fb6c0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63  pFile->sharedLoc
fb6d0 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b  kByte, 0, 1, 0);
fb6e0 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65  .#endif.  }.  re
fb6f0 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a  turn res;.}../*.
fb700 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65  ** Lock the file
fb710 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73   with the lock s
fb720 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
fb730 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d  meter locktype -
fb740 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66   one.** of the f
fb750 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
fb760 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c      (1) SHARED_L
fb770 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52  OCK.**     (2) R
fb780 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20  ESERVED_LOCK.** 
fb790 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f      (3) PENDING_
fb7a0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20  LOCK.**     (4) 
fb7b0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a  EXCLUSIVE_LOCK.*
fb7c0 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
fb7d0 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
fb7e0 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
fb7f0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
fb800 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73  tates.** are ins
fb810 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e  erted in between
fb820 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d  .  The locking m
fb830 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65  ight fail on one
fb840 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a   of the later.**
fb850 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61   transitions lea
fb860 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74  ving the lock st
fb870 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  ate different fr
fb880 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74  om what it start
fb890 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20  ed but.** still 
fb8a0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
fb8b0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
fb8c0 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
fb8d0 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61  e allowed.** tra
fb8e0 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
fb8f0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d   inserted interm
fb900 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a  ediate states:.*
fb910 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44  *.**    UNLOCKED
fb920 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20   -> SHARED.**   
fb930 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
fb940 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  VED.**    SHARED
fb950 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
fb960 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
fb970 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45   RESERVED -> (PE
fb980 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
fb990 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  IVE.**    PENDIN
fb9a0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  G -> EXCLUSIVE.*
fb9b0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
fb9c0 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72  e will only incr
fb9d0 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68  ease a lock.  Th
fb9e0 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72 6f  e winUnlock() ro
fb9f0 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20  utine.** erases 
fba00 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63  all locks at onc
fba10 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73  e and returns us
fba20 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20   immediately to 
fba30 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e  locking level 0.
fba40 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f  .** It is not po
fba50 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20  ssible to lower 
fba60 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
fba70 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20  l one step at a 
fba80 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75  time.  You.** mu
fba90 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74  st go straight t
fbaa0 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  o locking level 
fbab0 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  0..*/.static int
fbac0 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65 33   winLock(sqlite3
fbad0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
fbae0 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20  ocktype){.  int 
fbaf0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
fbb00 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
fbb10 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e  e from subroutin
fbb20 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20  es */.  int res 
fbb30 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 2f  = 1;           /
fbb40 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77 69  * Result of a wi
fbb50 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c 20  ndows lock call 
fbb60 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b  */.  int newLock
fbb70 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53  type;       /* S
fbb80 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  et pFile->lockty
fbb90 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65  pe to this value
fbba0 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
fbbb0 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64  */.  int gotPend
fbbc0 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54  ingLock = 0;/* T
fbbd0 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72  rue if we acquir
fbbe0 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  ed a PENDING loc
fbbf0 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20  k this time */. 
fbc00 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
fbc10 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
fbc20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
fbc30 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  !=0 );.  OSTRACE
fbc40 35 28 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 61  5("LOCK %d %d wa
fbc50 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20  s %d(%d)\n",.   
fbc60 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c         pFile->h,
fbc70 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65   locktype, pFile
fbc80 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c  ->locktype, pFil
fbc90 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  e->sharedLockByt
fbca0 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  e);..  /* If the
fbcb0 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
fbcc0 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70  lock of this typ
fbcd0 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69  e or more restri
fbce0 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a  ctive on the.  *
fbcf0 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74  * OsFile, do not
fbd00 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20  hing. Don't use 
fbd10 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  the end_lock: ex
fbd20 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
fbd30 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d   sqlite3OsEnterM
fbd40 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65  utex() hasn't be
fbd50 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20  en called yet.. 
fbd60 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
fbd70 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74  >locktype>=lockt
fbd80 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ype ){.    retur
fbd90 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
fbda0 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
fbdb0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71   the locking seq
fbdc0 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74  uence is correct
fbdd0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
fbde0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21  pFile->locktype!
fbdf0 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b  =NO_LOCK || lock
fbe00 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
fbe10 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  K );.  assert( l
fbe20 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47  ocktype!=PENDING
fbe30 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
fbe40 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53  t( locktype!=RES
fbe50 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46  ERVED_LOCK || pF
fbe60 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53  ile->locktype==S
fbe70 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20  HARED_LOCK );.. 
fbe80 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e   /* Lock the PEN
fbe90 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69  DING_LOCK byte i
fbea0 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71  f we need to acq
fbeb0 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c  uire a PENDING l
fbec0 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48  ock or.  ** a SH
fbed0 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77  ARED lock.  If w
fbee0 65 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20  e are acquiring 
fbef0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74  a SHARED lock, t
fbf00 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f  he acquisition o
fbf10 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49  f.  ** the PENDI
fbf20 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20  NG_LOCK byte is 
fbf30 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a  temporary..  */.
fbf40 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
fbf50 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b  pFile->locktype;
fbf60 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
fbf70 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a  cktype==NO_LOCK.
fbf80 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d     || (locktype=
fbf90 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
fbfa0 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  && pFile->lockty
fbfb0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe==RESERVED_LOC
fbfc0 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20  K).  ){.    int 
fbfd0 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69  cnt = 3;.    whi
fbfe0 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28  le( cnt-->0 && (
fbff0 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70  res = LockFile(p
fc000 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
fc010 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29  _BYTE, 0, 1, 0))
fc020 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
fc030 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67  Try 3 times to g
fc040 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c  et the pending l
fc050 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e  ock.  The pendin
fc060 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a  g lock might be.
fc070 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79        ** held by
fc080 20 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20   another reader 
fc090 70 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c  process who will
fc0a0 20 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65   release it mome
fc0b0 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a  ntarily..      *
fc0c0 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  /.      OSTRACE2
fc0d0 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20  ("could not get 
fc0e0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20  a PENDING lock. 
fc0f0 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b  cnt=%d\n", cnt);
fc100 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b  .      Sleep(1);
fc110 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65  .    }.    gotPe
fc120 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b  ndingLock = res;
fc130 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
fc140 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  re a shared lock
fc150 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
fc160 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
fc170 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20  K && res ){.    
fc180 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
fc190 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
fc1a0 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65   );.    res = ge
fc1b0 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  tReadLock(pFile)
fc1c0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
fc1d0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
fc1e0 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b  pe = SHARED_LOCK
fc1f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
fc200 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45  * Acquire a RESE
fc210 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20  RVED lock.  */. 
fc220 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52   if( locktype==R
fc230 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20  ESERVED_LOCK && 
fc240 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72  res ){.    asser
fc250 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
fc260 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
fc270 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63  );.    res = Loc
fc280 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fc290 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30  RESERVED_BYTE, 0
fc2a0 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 1, 0);.    if(
fc2b0 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65   res ){.      ne
fc2c0 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45  wLocktype = RESE
fc2d0 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  RVED_LOCK;.    }
fc2e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
fc2f0 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  re a PENDING loc
fc300 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
fc310 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
fc320 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
fc330 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
fc340 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a  = PENDING_LOCK;.
fc350 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f      gotPendingLo
fc360 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  ck = 0;.  }..  /
fc370 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43  * Acquire an EXC
fc380 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f  LUSIVE lock.  */
fc390 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
fc3a0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
fc3b0 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73  && res ){.    as
fc3c0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
fc3d0 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f  ktype>=SHARED_LO
fc3e0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20  CK );.    res = 
fc3f0 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70  unlockReadLock(p
fc400 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41  File);.    OSTRA
fc410 43 45 32 28 22 75 6e 72 65 61 64 6c 6f 63 6b 20  CE2("unreadlock 
fc420 3d 20 25 64 5c 6e 22 2c 20 72 65 73 29 3b 0a 20  = %d\n", res);. 
fc430 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c     res = LockFil
fc440 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  e(pFile->h, SHAR
fc450 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41  ED_FIRST, 0, SHA
fc460 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20  RED_SIZE, 0);.  
fc470 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
fc480 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d     newLocktype =
fc490 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b   EXCLUSIVE_LOCK;
fc4a0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
fc4b0 20 20 4f 53 54 52 41 43 45 32 28 22 65 72 72 6f    OSTRACE2("erro
fc4c0 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20  r-code = %d\n", 
fc4d0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 3b  GetLastError());
fc4e0 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f  .      getReadLo
fc4f0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d  ck(pFile);.    }
fc500 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65  .  }..  /* If we
fc510 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50   are holding a P
fc520 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74  ENDING lock that
fc530 20 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c   ought to be rel
fc540 65 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a  eased, then.  **
fc550 20 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e   release it now.
fc560 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50  .  */.  if( gotP
fc570 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f  endingLock && lo
fc580 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
fc590 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63  OCK ){.    Unloc
fc5a0 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fc5b0 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c  PENDING_BYTE, 0,
fc5c0 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f   1, 0);.  }..  /
fc5d0 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 74 61  * Update the sta
fc5e0 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68  te of the lock h
fc5f0 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66  as held in the f
fc600 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
fc610 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20  hen.  ** return 
fc620 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
fc630 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a  result code..  *
fc640 2f 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20  /.  if( res ){. 
fc650 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
fc660 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
fc670 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46  OSTRACE4("LOCK F
fc680 41 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20  AILED %d trying 
fc690 66 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25  for %d but got %
fc6a0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  d\n", pFile->h,.
fc6b0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74             lockt
fc6c0 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65  ype, newLocktype
fc6d0 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
fc6e0 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70  TE_BUSY;.  }.  p
fc6f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
fc700 20 28 75 38 29 6e 65 77 4c 6f 63 6b 74 79 70 65   (u8)newLocktype
fc710 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
fc720 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
fc730 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
fc740 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
fc750 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
fc760 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
fc770 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72   file by this or
fc780 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
fc790 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ss. If such a lo
fc7a0 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 75  ck is held, retu
fc7b0 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  rn.** non-zero, 
fc7c0 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a  otherwise zero..
fc7d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
fc7e0 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  nCheckReservedLo
fc7f0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
fc800 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
fc810 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
fc820 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
fc830 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20   (winFile*)id;. 
fc840 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d   assert( pFile!=
fc850 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  0 );.  if( pFile
fc860 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45  ->locktype>=RESE
fc870 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  RVED_LOCK ){.   
fc880 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54   rc = 1;.    OST
fc890 52 41 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c  RACE3("TEST WR-L
fc8a0 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c  OCK %d %d (local
fc8b0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
fc8c0 72 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  rc);.  }else{.  
fc8d0 20 20 72 63 20 3d 20 4c 6f 63 6b 46 69 6c 65 28    rc = LockFile(
fc8e0 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56  pFile->h, RESERV
fc8f0 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30  ED_BYTE, 0, 1, 0
fc900 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
fc910 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c  .      UnlockFil
fc920 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45  e(pFile->h, RESE
fc930 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c  RVED_BYTE, 0, 1,
fc940 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72   0);.    }.    r
fc950 63 20 3d 20 21 72 63 3b 0a 20 20 20 20 4f 53 54  c = !rc;.    OST
fc960 52 41 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c  RACE3("TEST WR-L
fc970 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d 6f 74  OCK %d %d (remot
fc980 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  e)\n", pFile->h,
fc990 20 72 63 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65   rc);.  }.  *pRe
fc9a0 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74  sOut = rc;.  ret
fc9b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
fc9c0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68  ../*.** Lower th
fc9d0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
fc9e0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  on file descript
fc9f0 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70  or id to locktyp
fca00 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20  e.  locktype.** 
fca10 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e  must be either N
fca20 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44  O_LOCK or SHARED
fca30 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  _LOCK..**.** If 
fca40 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
fca50 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65  l of the file de
fca60 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65  scriptor is alre
fca70 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a  ady at or below.
fca80 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64  ** the requested
fca90 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20   locking level, 
fcaa0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
fcab0 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49  a no-op..**.** I
fcac0 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
fcad0 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69  e for this routi
fcae0 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68  ne to fail if th
fcaf0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
fcb00 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e  t.** is NO_LOCK.
fcb10 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20    If the second 
fcb20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52  argument is SHAR
fcb30 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69  ED_LOCK then thi
fcb40 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67  s routine.** mig
fcb50 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ht return SQLITE
fcb60 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69  _IOERR;.*/.stati
fcb70 63 20 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 6b 28  c int winUnlock(
fcb80 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fcb90 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b  , int locktype){
fcba0 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 77  .  int type;.  w
fcbb0 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  inFile *pFile = 
fcbc0 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (winFile*)id;.  
fcbd0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
fcbe0 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46  OK;.  assert( pF
fcbf0 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ile!=0 );.  asse
fcc00 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48  rt( locktype<=SH
fcc10 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f  ARED_LOCK );.  O
fcc20 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20  STRACE5("UNLOCK 
fcc30 25 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 28  %d to %d was %d(
fcc40 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  %d)\n", pFile->h
fcc50 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20  , locktype,.    
fcc60 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
fcc70 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68  ktype, pFile->sh
fcc80 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 20  aredLockByte);. 
fcc90 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c   type = pFile->l
fcca0 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74  ocktype;.  if( t
fccb0 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype>=EXCLUSIVE_L
fccc0 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63  OCK ){.    Unloc
fccd0 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fcce0 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c  SHARED_FIRST, 0,
fccf0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29   SHARED_SIZE, 0)
fcd00 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79  ;.    if( lockty
fcd10 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
fcd20 26 26 20 21 67 65 74 52 65 61 64 4c 6f 63 6b 28  && !getReadLock(
fcd30 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 20 20  pFile) ){.      
fcd40 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e  /* This should n
fcd50 65 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65  ever happen.  We
fcd60 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62   should always b
fcd70 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20  e able to.      
fcd80 2a 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65  ** reacquire the
fcd90 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20   read lock */.  
fcda0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
fcdb0 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20  IOERR_UNLOCK;.  
fcdc0 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79    }.  }.  if( ty
fcdd0 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe>=RESERVED_LOC
fcde0 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46  K ){.    UnlockF
fcdf0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45  ile(pFile->h, RE
fce00 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20  SERVED_BYTE, 0, 
fce10 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  1, 0);.  }.  if(
fce20 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f   locktype==NO_LO
fce30 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52  CK && type>=SHAR
fce40 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75  ED_LOCK ){.    u
fce50 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46  nlockReadLock(pF
fce60 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ile);.  }.  if( 
fce70 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type>=PENDING_LO
fce80 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b  CK ){.    Unlock
fce90 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50  File(pFile->h, P
fcea0 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20  ENDING_BYTE, 0, 
fceb0 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 46 69  1, 0);.  }.  pFi
fcec0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28  le->locktype = (
fced0 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72  u8)locktype;.  r
fcee0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
fcef0 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71  ** Control and q
fcf00 75 65 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e  uery of the open
fcf10 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f   file handle..*/
fcf20 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46  .static int winF
fcf30 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74  ileControl(sqlit
fcf40 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
fcf50 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29   op, void *pArg)
fcf60 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  {.  switch( op )
fcf70 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
fcf80 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
fcf90 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74  E: {.      *(int
fcfa0 2a 29 70 41 72 67 20 3d 20 28 28 77 69 6e 46 69  *)pArg = ((winFi
fcfb0 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70  le*)id)->locktyp
fcfc0 65 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e;.      return 
fcfd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
fcfe0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
fcff0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f  LITE_ERROR;.}../
fd000 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
fd010 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62  sector size in b
fd020 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65  ytes of the unde
fd030 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76  rlying block dev
fd040 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73  ice for.** the s
fd050 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54  pecified file. T
fd060 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c  his is almost al
fd070 77 61 79 73 20 35 31 32 20 62 79 74 65 73 2c 20  ways 512 bytes, 
fd080 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61  but may be.** la
fd090 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65  rger for some de
fd0a0 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  vices..**.** SQL
fd0b0 69 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73  ite code assumes
fd0c0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   this function c
fd0d0 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61  annot fail. It a
fd0e0 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74  lso assumes that
fd0f0 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73  .** if two files
fd100 20 61 72 65 20 63 72 65 61 74 65 64 20 69 6e 20   are created in 
fd110 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79  the same file-sy
fd120 73 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28  stem directory (
fd130 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61  i.e..** a databa
fd140 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e  se and its journ
fd150 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 68  al file) that th
fd160 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69  e sector size wi
fd170 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d  ll be the.** sam
fd180 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73  e for both..*/.s
fd190 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63  tatic int winSec
fd1a0 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  torSize(sqlite3_
fd1b0 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 55 4e 55  file *id){.  UNU
fd1c0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 64  SED_PARAMETER(id
fd1d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
fd1e0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
fd1f0 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  R_SIZE;.}../*.**
fd200 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72   Return a vector
fd210 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61   of device chara
fd220 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73  cteristics..*/.s
fd230 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 76  tatic int winDev
fd240 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
fd250 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
fd260 2a 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  *id){.  UNUSED_P
fd270 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a 20 20  ARAMETER(id);.  
fd280 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
fd290 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64  ** This vector d
fd2a0 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d  efines all the m
fd2b0 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20  ethods that can 
fd2c0 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a  operate on an.**
fd2d0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f   sqlite3_file fo
fd2e0 72 20 77 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74  r win32..*/.stat
fd2f0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
fd300 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 77 69 6e 49  _io_methods winI
fd310 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c  oMethod = {.  1,
fd320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd330 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
fd340 69 6f 6e 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73  ion */.  winClos
fd350 65 2c 0a 20 20 77 69 6e 52 65 61 64 2c 0a 20 20  e,.  winRead,.  
fd360 77 69 6e 57 72 69 74 65 2c 0a 20 20 77 69 6e 54  winWrite,.  winT
fd370 72 75 6e 63 61 74 65 2c 0a 20 20 77 69 6e 53 79  runcate,.  winSy
fd380 6e 63 2c 0a 20 20 77 69 6e 46 69 6c 65 53 69 7a  nc,.  winFileSiz
fd390 65 2c 0a 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20  e,.  winLock,.  
fd3a0 77 69 6e 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e  winUnlock,.  win
fd3b0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
fd3c0 6b 2c 0a 20 20 77 69 6e 46 69 6c 65 43 6f 6e 74  k,.  winFileCont
fd3d0 72 6f 6c 2c 0a 20 20 77 69 6e 53 65 63 74 6f 72  rol,.  winSector
fd3e0 53 69 7a 65 2c 0a 20 20 77 69 6e 44 65 76 69 63  Size,.  winDevic
fd3f0 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
fd400 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .};../**********
fd410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd450 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74  *.** Here ends t
fd460 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74  he I/O methods t
fd470 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c  hat form the sql
fd480 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
fd490 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  object..**.** Th
fd4a0 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20  e next block of 
fd4b0 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  code implements 
fd4c0 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e  the VFS methods.
fd4d0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
fd4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
fd520 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
fd530 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65 20 69  UTF-8 filename i
fd540 6e 74 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72  nto whatever for
fd550 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  m the underlying
fd560 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79  .** operating sy
fd570 73 74 65 6d 20 77 61 6e 74 73 20 66 69 6c 65 6e  stem wants filen
fd580 61 6d 65 73 20 69 6e 2e 20 20 53 70 61 63 65 20  ames in.  Space 
fd590 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75  to hold the resu
fd5a0 6c 74 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65  lt.** is obtaine
fd5b0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e  d from malloc an
fd5c0 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20  d must be freed 
fd5d0 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a  by the calling.*
fd5e0 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  * function..*/.s
fd5f0 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 76  tatic void *conv
fd600 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28  ertUtf8Filename(
fd610 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
fd620 65 6e 61 6d 65 29 7b 0a 20 20 76 6f 69 64 20 2a  ename){.  void *
fd630 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 30 3b 0a  zConverted = 0;.
fd640 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
fd650 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d      zConverted =
fd660 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a   utf8ToUnicode(z
fd670 46 69 6c 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73  Filename);./* is
fd680 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
fd690 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
fd6a0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
fd6b0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
fd6c0 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f   .*/.#if SQLITE_
fd6d0 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65  OS_WINCE==0.  }e
fd6e0 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72  lse{.    zConver
fd6f0 74 65 64 20 3d 20 75 74 66 38 54 6f 4d 62 63 73  ted = utf8ToMbcs
fd700 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e  (zFilename);.#en
fd710 64 69 66 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c  dif.  }.  /* cal
fd720 6c 65 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20  ler will handle 
fd730 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f  out of memory */
fd740 0a 20 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65  .  return zConve
fd750 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rted;.}../*.** C
fd760 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72  reate a temporar
fd770 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a  y file name in z
fd780 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20  Buf.  zBuf must 
fd790 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f  be big enough to
fd7a0 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73  .** hold at pVfs
fd7b0 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61  ->mxPathname cha
fd7c0 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74  racters..*/.stat
fd7d0 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61  ic int getTempna
fd7e0 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  me(int nBuf, cha
fd7f0 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74  r *zBuf){.  stat
fd800 69 63 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d  ic char zChars[]
fd810 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68   =.    "abcdefgh
fd820 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78  ijklmnopqrstuvwx
fd830 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47  yz".    "ABCDEFG
fd840 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57  HIJKLMNOPQRSTUVW
fd850 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35  XYZ".    "012345
fd860 36 37 38 39 22 3b 0a 20 20 73 69 7a 65 5f 74 20  6789";.  size_t 
fd870 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65  i, j;.  char zTe
fd880 6d 70 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b  mpPath[MAX_PATH+
fd890 31 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  1];.  if( sqlite
fd8a0 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
fd8b0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
fd8c0 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
fd8d0 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c  H-30, zTempPath,
fd8e0 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 74   "%s", sqlite3_t
fd8f0 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a  emp_directory);.
fd900 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4e 54    }else if( isNT
fd910 28 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  () ){.    char *
fd920 7a 4d 75 6c 74 69 3b 0a 20 20 20 20 57 43 48 41  zMulti;.    WCHA
fd930 52 20 7a 57 69 64 65 50 61 74 68 5b 4d 41 58 5f  R zWidePath[MAX_
fd940 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 74 54 65  PATH];.    GetTe
fd950 6d 70 50 61 74 68 57 28 4d 41 58 5f 50 41 54 48  mpPathW(MAX_PATH
fd960 2d 33 30 2c 20 7a 57 69 64 65 50 61 74 68 29 3b  -30, zWidePath);
fd970 0a 20 20 20 20 7a 4d 75 6c 74 69 20 3d 20 75 6e  .    zMulti = un
fd980 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 57 69 64  icodeToUtf8(zWid
fd990 65 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20  ePath);.    if( 
fd9a0 7a 4d 75 6c 74 69 20 29 7b 0a 20 20 20 20 20 20  zMulti ){.      
fd9b0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
fd9c0 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54  (MAX_PATH-30, zT
fd9d0 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a  empPath, "%s", z
fd9e0 4d 75 6c 74 69 29 3b 0a 20 20 20 20 20 20 66 72  Multi);.      fr
fd9f0 65 65 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20  ee(zMulti);.    
fda00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74  }else{.      ret
fda10 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
fda20 3b 0a 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28  ;.    }./* isNT(
fda30 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
fda40 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
fda50 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
fda60 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
fda70 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49  * Since the ASCI
fda80 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  I version of the
fda90 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64  se Windows API d
fdaa0 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20  o not exist for 
fdab0 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69  WINCE,.** it's i
fdac0 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20  mportant to not 
fdad0 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66  reference them f
fdae0 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e  or WINCE builds.
fdaf0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
fdb00 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c  S_WINCE==0.  }el
fdb10 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 55  se{.    char *zU
fdb20 74 66 38 3b 0a 20 20 20 20 63 68 61 72 20 7a 4d  tf8;.    char zM
fdb30 62 63 73 50 61 74 68 5b 4d 41 58 5f 50 41 54 48  bcsPath[MAX_PATH
fdb40 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61  ];.    GetTempPa
fdb50 74 68 41 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c  thA(MAX_PATH-30,
fdb60 20 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20   zMbcsPath);.   
fdb70 20 7a 55 74 66 38 20 3d 20 73 71 6c 69 74 65 33   zUtf8 = sqlite3
fdb80 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75  _win32_mbcs_to_u
fdb90 74 66 38 28 7a 4d 62 63 73 50 61 74 68 29 3b 0a  tf8(zMbcsPath);.
fdba0 20 20 20 20 69 66 28 20 7a 55 74 66 38 20 29 7b      if( zUtf8 ){
fdbb0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
fdbc0 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48  nprintf(MAX_PATH
fdbd0 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20  -30, zTempPath, 
fdbe0 22 25 73 22 2c 20 7a 55 74 66 38 29 3b 0a 20 20  "%s", zUtf8);.  
fdbf0 20 20 20 20 66 72 65 65 28 7a 55 74 66 38 29 3b      free(zUtf8);
fdc00 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
fdc10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fdc20 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e  NOMEM;.    }.#en
fdc30 64 69 66 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  dif.  }.  for(i=
fdc40 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
fdc50 7a 54 65 6d 70 50 61 74 68 29 3b 20 69 3e 30 20  zTempPath); i>0 
fdc60 26 26 20 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31  && zTempPath[i-1
fdc70 5d 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a  ]=='\\'; i--){}.
fdc80 20 20 7a 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d    zTempPath[i] =
fdc90 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e   0;.  sqlite3_sn
fdca0 70 72 69 6e 74 66 28 6e 42 75 66 2d 33 30 2c 20  printf(nBuf-30, 
fdcb0 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20  zBuf,.          
fdcc0 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53           "%s\\"S
fdcd0 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f  QLITE_TEMP_FILE_
fdce0 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74  PREFIX, zTempPat
fdcf0 68 29 3b 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65  h);.  j = sqlite
fdd00 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 66 29 3b  3Strlen30(zBuf);
fdd10 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  .  sqlite3_rando
fdd20 6d 6e 65 73 73 28 32 30 2c 20 26 7a 42 75 66 5b  mness(20, &zBuf[
fdd30 6a 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  j]);.  for(i=0; 
fdd40 69 3c 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b  i<20; i++, j++){
fdd50 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28  .    zBuf[j] = (
fdd60 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75  char)zChars[ ((u
fdd70 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75  nsigned char)zBu
fdd80 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43  f[j])%(sizeof(zC
fdd90 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a  hars)-1) ];.  }.
fdda0 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20    zBuf[j] = 0;. 
fddb0 20 4f 53 54 52 41 43 45 32 28 22 54 45 4d 50 20   OSTRACE2("TEMP 
fddc0 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c  FILENAME: %s\n",
fddd0 20 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e   zBuf);.  return
fdde0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a   SQLITE_OK; .}..
fddf0 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  /*.** The return
fde00 20 76 61 6c 75 65 20 6f 66 20 67 65 74 4c 61 73   value of getLas
fde10 74 45 72 72 6f 72 4d 73 67 0a 2a 2a 20 69 73 20  tErrorMsg.** is 
fde20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f  zero if the erro
fde30 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20 69  r message fits i
fde40 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72  n the buffer, or
fde50 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68   non-zero.** oth
fde60 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 6d  erwise (if the m
fde70 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e 63  essage was trunc
fde80 61 74 65 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ated)..*/.static
fde90 20 69 6e 74 20 67 65 74 4c 61 73 74 45 72 72 6f   int getLastErro
fdea0 72 4d 73 67 28 69 6e 74 20 6e 42 75 66 2c 20 63  rMsg(int nBuf, c
fdeb0 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 44 57  har *zBuf){.  DW
fdec0 4f 52 44 20 65 72 72 6f 72 20 3d 20 47 65 74 4c  ORD error = GetL
fded0 61 73 74 45 72 72 6f 72 28 29 3b 0a 0a 23 69 66  astError();..#if
fdee0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
fdef0 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
fdf00 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20  ntf(nBuf, zBuf, 
fdf10 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20 28 25  "OsError 0x%x (%
fdf20 75 29 22 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f  u)", error, erro
fdf30 72 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 46  r);.#else.  /* F
fdf40 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 72 65 74  ormatMessage ret
fdf50 75 72 6e 73 20 30 20 6f 6e 20 66 61 69 6c 75 72  urns 0 on failur
fdf60 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74  e.  Otherwise it
fdf70 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 74 68  .  ** returns th
fdf80 65 20 6e 75 6d 62 65 72 20 6f 66 20 54 43 48 41  e number of TCHA
fdf90 52 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  Rs written to th
fdfa0 65 20 6f 75 74 70 75 74 0a 20 20 2a 2a 20 62 75  e output.  ** bu
fdfb0 66 66 65 72 2c 20 65 78 63 6c 75 64 69 6e 67 20  ffer, excluding 
fdfc0 74 68 65 20 74 65 72 6d 69 6e 61 74 69 6e 67 20  the terminating 
fdfd0 6e 75 6c 6c 20 63 68 61 72 2e 0a 20 20 2a 2f 0a  null char..  */.
fdfe0 20 20 69 66 20 28 21 46 6f 72 6d 61 74 4d 65 73    if (!FormatMes
fdff0 73 61 67 65 41 28 46 4f 52 4d 41 54 5f 4d 45 53  sageA(FORMAT_MES
fe000 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 45 4d  SAGE_FROM_SYSTEM
fe010 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
fe020 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20          NULL,.  
fe030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe040 20 20 20 20 65 72 72 6f 72 2c 0a 20 20 20 20 20      error,.     
fe050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe060 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
fe070 20 20 20 20 20 20 20 20 20 20 7a 42 75 66 2c 0a            zBuf,.
fe080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe090 20 20 20 20 20 20 6e 42 75 66 2d 31 2c 0a 20 20        nBuf-1,.  
fe0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe0b0 20 20 20 20 30 29 29 0a 20 20 7b 0a 20 20 20 20      0)).  {.    
fe0c0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
fe0d0 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73  (nBuf, zBuf, "Os
fe0e0 45 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22  Error 0x%x (%u)"
fe0f0 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b  , error, error);
fe100 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72  .  }.#endif..  r
fe110 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn 0;.}.../*.
fe120 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a  ** Open a file..
fe130 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
fe140 6e 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  nOpen(.  sqlite3
fe150 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
fe160 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a     /* Not used *
fe170 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
fe180 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a  zName,        /*
fe190 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
fe1a0 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73  e (UTF-8) */.  s
fe1b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fe1c0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
fe1d0 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c  e the SQLite fil
fe1e0 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f  e handle here */
fe1f0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
fe200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fe210 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20  Open mode flags 
fe220 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c  */.  int *pOutFl
fe230 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f  ags            /
fe240 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20  * Status return 
fe250 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41  flags */.){.  HA
fe260 4e 44 4c 45 20 68 3b 0a 20 20 44 57 4f 52 44 20  NDLE h;.  DWORD 
fe270 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b  dwDesiredAccess;
fe280 0a 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65  .  DWORD dwShare
fe290 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77  Mode;.  DWORD dw
fe2a0 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
fe2b0 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46  ion;.  DWORD dwF
fe2c0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
fe2d0 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54  s = 0;.#if SQLIT
fe2e0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74  E_OS_WINCE.  int
fe2f0 20 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e   isTemp = 0;.#en
fe300 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  dif.  winFile *p
fe310 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
fe320 29 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  )id;.  void *zCo
fe330 6e 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20  nverted;        
fe340 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
fe350 6e 61 6d 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64  name in OS encod
fe360 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ing */.  const c
fe370 68 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d  har *zUtf8Name =
fe380 20 7a 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69   zName;    /* Fi
fe390 6c 65 6e 61 6d 65 20 69 6e 20 55 54 46 2d 38 20  lename in UTF-8 
fe3a0 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68  encoding */.  ch
fe3b0 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f  ar zTmpname[MAX_
fe3c0 50 41 54 48 2b 31 5d 3b 20 20 20 20 20 20 20 20  PATH+1];        
fe3d0 2f 2a 20 42 75 66 66 65 72 20 75 73 65 64 20 74  /* Buffer used t
fe3e0 6f 20 63 72 65 61 74 65 20 74 65 6d 70 20 66 69  o create temp fi
fe3f0 6c 65 6e 61 6d 65 20 2a 2f 0a 0a 20 20 55 4e 55  lename */..  UNU
fe400 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56  SED_PARAMETER(pV
fe410 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  fs);..  /* If th
fe420 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
fe430 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
fe440 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65  on is NULL, gene
fe450 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d  rate a .  ** tem
fe460 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65  porary file name
fe470 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20   to use .  */.  
fe480 69 66 28 20 21 7a 55 74 66 38 4e 61 6d 65 20 29  if( !zUtf8Name )
fe490 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67  {.    int rc = g
fe4a0 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50  etTempname(MAX_P
fe4b0 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29  ATH+1, zTmpname)
fe4c0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
fe4d0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
fe4e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
fe4f0 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20  }.    zUtf8Name 
fe500 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a  = zTmpname;.  }.
fe510 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68  .  /* Convert th
fe520 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68  e filename to th
fe530 65 20 73 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e  e system encodin
fe540 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74  g. */.  zConvert
fe550 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  ed = convertUtf8
fe560 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61  Filename(zUtf8Na
fe570 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76  me);.  if( zConv
fe580 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  erted==0 ){.    
fe590 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
fe5a0 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  MEM;.  }..  if( 
fe5b0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
fe5c0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b  PEN_READWRITE ){
fe5d0 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63  .    dwDesiredAc
fe5e0 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52  cess = GENERIC_R
fe5f0 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52  EAD | GENERIC_WR
fe600 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ITE;.  }else{.  
fe610 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73    dwDesiredAcces
fe620 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44  s = GENERIC_READ
fe630 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67  ;.  }.  if( flag
fe640 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
fe650 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 64 77  CREATE ){.    dw
fe660 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
fe670 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57 41 59  ion = OPEN_ALWAY
fe680 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  S;.  }else{.    
fe690 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73  dwCreationDispos
fe6a0 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49  ition = OPEN_EXI
fe6b0 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 69 66 28  STING;.  }.  if(
fe6c0 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
fe6d0 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a  OPEN_MAIN_DB ){.
fe6e0 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 20      dwShareMode 
fe6f0 3d 20 46 49 4c 45 5f 53 48 41 52 45 5f 52 45 41  = FILE_SHARE_REA
fe700 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45 5f 57  D | FILE_SHARE_W
fe710 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  RITE;.  }else{. 
fe720 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 20 3d     dwShareMode =
fe730 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c   0;.  }.  if( fl
fe740 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
fe750 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20  N_DELETEONCLOSE 
fe760 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  ){.#if SQLITE_OS
fe770 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46 6c 61  _WINCE.    dwFla
fe780 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20  gsAndAttributes 
fe790 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  = FILE_ATTRIBUTE
fe7a0 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69 73 54  _HIDDEN;.    isT
fe7b0 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a 20  emp = 1;.#else. 
fe7c0 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74     dwFlagsAndAtt
fe7d0 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41  ributes = FILE_A
fe7e0 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 41  TTRIBUTE_TEMPORA
fe7f0 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  RY.             
fe800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe810 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 42 55    | FILE_ATTRIBU
fe820 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20 20 20  TE_HIDDEN.      
fe830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe840 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f           | FILE_
fe850 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f 43  FLAG_DELETE_ON_C
fe860 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  LOSE;.#endif.  }
fe870 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c 61 67  else{.    dwFlag
fe880 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d  sAndAttributes =
fe890 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f   FILE_ATTRIBUTE_
fe8a0 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f 2a  NORMAL;.  }.  /*
fe8b0 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20 74 68   Reports from th
fe8c0 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65 20 74  e internet are t
fe8d0 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  hat performance 
fe8e0 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 62  is always.  ** b
fe8f0 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f 46 4c  etter if FILE_FL
fe900 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 53  AG_RANDOM_ACCESS
fe910 20 69 73 20 75 73 65 64 2e 20 20 54 69 63 6b 65   is used.  Ticke
fe920 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66 20  t #2699. */.#if 
fe930 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
fe940 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72    dwFlagsAndAttr
fe950 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f 46  ibutes |= FILE_F
fe960 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53  LAG_RANDOM_ACCES
fe970 53 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20  S;.#endif.  if( 
fe980 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20  isNT() ){.    h 
fe990 3d 20 43 72 65 61 74 65 46 69 6c 65 57 28 28 57  = CreateFileW((W
fe9a0 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
fe9b0 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 72  ,.       dwDesir
fe9c0 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20  edAccess,.      
fe9d0 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20   dwShareMode,.  
fe9e0 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20       NULL,.     
fe9f0 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70    dwCreationDisp
fea00 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20  osition,.       
fea10 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62  dwFlagsAndAttrib
fea20 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c  utes,.       NUL
fea30 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20 69 73 4e 54  L.    );./* isNT
fea40 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54  () is 1 if SQLIT
fea50 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73  E_OS_WINCE==1, s
fea60 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e  o this else is n
fea70 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a  ever executed. .
fea80 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43  ** Since the ASC
fea90 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  II version of th
feaa0 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20  ese Windows API 
feab0 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72  do not exist for
feac0 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20   WINCE,.** it's 
fead0 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74  important to not
feae0 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20   reference them 
feaf0 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73  for WINCE builds
feb00 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
feb10 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65  OS_WINCE==0.  }e
feb20 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 43 72 65  lse{.    h = Cre
feb30 61 74 65 46 69 6c 65 41 28 28 63 68 61 72 2a 29  ateFileA((char*)
feb40 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20  zConverted,.    
feb50 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65     dwDesiredAcce
feb60 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61  ss,.       dwSha
feb70 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e  reMode,.       N
feb80 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72  ULL,.       dwCr
feb90 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
feba0 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67  n,.       dwFlag
febb0 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a  sAndAttributes,.
febc0 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20         NULL.    
febd0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  );.#endif.  }.  
febe0 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49 44 5f 48  if( h==INVALID_H
febf0 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 7b 0a 20  ANDLE_VALUE ){. 
fec00 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74     free(zConvert
fec10 65 64 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61  ed);.    if( fla
fec20 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
fec30 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20  _READWRITE ){.  
fec40 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 4f 70      return winOp
fec50 65 6e 28 30 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c  en(0, zName, id,
fec60 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28   .             (
fec70 28 66 6c 61 67 73 7c 53 51 4c 49 54 45 5f 4f 50  (flags|SQLITE_OP
fec80 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26 7e 53 51  EN_READONLY)&~SQ
fec90 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
feca0 49 54 45 29 2c 20 70 4f 75 74 46 6c 61 67 73 29  ITE), pOutFlags)
fecb0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
fecc0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fecd0 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d  _CANTOPEN;.    }
fece0 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 46  .  }.  if( pOutF
fecf0 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 66 28 20  lags ){.    if( 
fed00 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
fed10 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b  PEN_READWRITE ){
fed20 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 67  .      *pOutFlag
fed30 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s = SQLITE_OPEN_
fed40 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20 7d  READWRITE;.    }
fed50 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 4f 75  else{.      *pOu
fed60 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f  tFlags = SQLITE_
fed70 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20  OPEN_READONLY;. 
fed80 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65     }.  }.  memse
fed90 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65  t(pFile, 0, size
feda0 6f 66 28 2a 70 46 69 6c 65 29 29 3b 0a 20 20 70  of(*pFile));.  p
fedb0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20  File->pMethod = 
fedc0 26 77 69 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 20  &winIoMethod;.  
fedd0 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 23 69  pFile->h = h;.#i
fede0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
fedf0 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20 26  E.  if( (flags &
fee00 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   (SQLITE_OPEN_RE
fee10 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f  ADWRITE|SQLITE_O
fee20 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 3d 3d  PEN_MAIN_DB)) ==
fee30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
fee40 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  (SQLITE_OPEN_REA
fee50 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
fee60 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 20 20  EN_MAIN_DB).    
fee70 20 20 20 26 26 20 21 77 69 6e 63 65 43 72 65 61     && !winceCrea
fee80 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46  teLock(zName, pF
fee90 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 43 6c  ile).  ){.    Cl
feea0 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20  oseHandle(h);.  
feeb0 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65    free(zConverte
feec0 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  d);.    return S
feed0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
feee0 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65 6d 70    }.  if( isTemp
feef0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 7a   ){.    pFile->z
fef00 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20  DeleteOnClose = 
fef10 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 7d 65  zConverted;.  }e
fef20 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20  lse.#endif.  {. 
fef30 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74     free(zConvert
fef40 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43  ed);.  }.  OpenC
fef50 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 72 65  ounter(+1);.  re
fef60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
fef70 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
fef80 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a  the named file..
fef90 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
fefa0 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74  windows does not
fefb0 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f   allow a file to
fefc0 20 62 65 20 64 65 6c 65 74 65 64 20 69 66 20 73   be deleted if s
fefd0 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f  ome other.** pro
fefe0 63 65 73 73 20 68 61 73 20 69 74 20 6f 70 65 6e  cess has it open
feff0 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 20 76  .  Sometimes a v
ff000 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f 72 20  irus scanner or 
ff010 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 61 6d  indexing program
ff020 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20  .** will open a 
ff030 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f  journal file sho
ff040 72 74 6c 79 20 61 66 74 65 72 20 69 74 20 69 73  rtly after it is
ff050 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72 64 65   created in orde
ff060 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65  r to do.** whate
ff070 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20 57 68  ver it does.  Wh
ff080 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72 20 70  ile this other p
ff090 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e  rocess is holdin
ff0a0 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70  g the.** file op
ff0b0 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 20 75  en, we will be u
ff0c0 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20  nable to delete 
ff0d0 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f  it.  To work aro
ff0e0 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62  und this.** prob
ff0f0 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 31 30  lem, we delay 10
ff100 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61  0 milliseconds a
ff110 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65 74 65  nd try to delete
ff120 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74   again.  Up.** t
ff130 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54  o MX_DELETION_AT
ff140 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e 20  TEMPTs deletion 
ff150 61 74 74 65 6d 70 74 73 20 61 72 65 20 72 75 6e  attempts are run
ff160 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a   before giving.*
ff170 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69  * up and returni
ff180 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  ng an error..*/.
ff190 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c 45 54  #define MX_DELET
ff1a0 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 0a 73  ION_ATTEMPTS 5.s
ff1b0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c  tatic int winDel
ff1c0 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ete(.  sqlite3_v
ff1d0 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20  fs *pVfs,       
ff1e0 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f     /* Not used o
ff1f0 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e  n win32 */.  con
ff200 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
ff210 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  me,      /* Name
ff220 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65   of file to dele
ff230 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63  te */.  int sync
ff240 44 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20  Dir             
ff250 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20      /* Not used 
ff260 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20  on win32 */.){. 
ff270 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
ff280 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f 52  DWORD rc;.  DWOR
ff290 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20 20 76  D error = 0;.  v
ff2a0 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20  oid *zConverted 
ff2b0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
ff2c0 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29  ename(zFilename)
ff2d0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
ff2e0 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 55 4e  ETER(pVfs);.  UN
ff2f0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73  USED_PARAMETER(s
ff300 79 6e 63 44 69 72 29 3b 0a 20 20 69 66 28 20 7a  yncDir);.  if( z
ff310 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a  Converted==0 ){.
ff320 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ff330 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53  E_NOMEM;.  }.  S
ff340 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72  imulateIOError(r
ff350 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
ff360 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 69 66  RR_DELETE);.  if
ff370 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
ff380 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 65  do{.      Delete
ff390 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 65 64  FileW(zConverted
ff3a0 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 20  );.    }while(  
ff3b0 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74 46   (   ((rc = GetF
ff3c0 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 7a  ileAttributesW(z
ff3d0 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20 49  Converted)) != I
ff3e0 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52  NVALID_FILE_ATTR
ff3f0 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20 20  IBUTES).        
ff400 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72 6f         || ((erro
ff410 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  r = GetLastError
ff420 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43  ()) == ERROR_ACC
ff430 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 20  ESS_DENIED)).   
ff440 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63 6e          && (++cn
ff450 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f  t < MX_DELETION_
ff460 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20 20  ATTEMPTS).      
ff470 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28 31       && (Sleep(1
ff480 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73  00), 1) );./* is
ff490 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
ff4a0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
ff4b0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
ff4c0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
ff4d0 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41   .** Since the A
ff4e0 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20  SCII version of 
ff4f0 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50  these Windows AP
ff500 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66  I do not exist f
ff510 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27  or WINCE,.** it'
ff520 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e  s important to n
ff530 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  ot reference the
ff540 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c  m for WINCE buil
ff550 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ds..*/.#if SQLIT
ff560 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20  E_OS_WINCE==0.  
ff570 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20  }else{.    do{. 
ff580 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 41       DeleteFileA
ff590 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
ff5a0 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 20    }while(   (   
ff5b0 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 74  ((rc = GetFileAt
ff5c0 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e 76 65  tributesA(zConve
ff5d0 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c 49  rted)) != INVALI
ff5e0 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  D_FILE_ATTRIBUTE
ff5f0 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  S).             
ff600 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 47    || ((error = G
ff610 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 3d  etLastError()) =
ff620 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f 44  = ERROR_ACCESS_D
ff630 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 20  ENIED)).        
ff640 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d     && (++cnt < M
ff650 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d  X_DELETION_ATTEM
ff660 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 20  PTS).           
ff670 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20  && (Sleep(100), 
ff680 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  1) );.#endif.  }
ff690 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74  .  free(zConvert
ff6a0 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28  ed);.  OSTRACE2(
ff6b0 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e  "DELETE \"%s\"\n
ff6c0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  ", zFilename);. 
ff6d0 20 72 65 74 75 72 6e 20 28 20 20 20 28 72 63 20   return (   (rc 
ff6e0 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f  == INVALID_FILE_
ff6f0 41 54 54 52 49 42 55 54 45 53 29 20 0a 20 20 20  ATTRIBUTES) .   
ff700 20 20 20 20 20 20 20 26 26 20 28 65 72 72 6f 72         && (error
ff710 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e   == ERROR_FILE_N
ff720 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c  OT_FOUND)) ? SQL
ff730 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f  ITE_OK : SQLITE_
ff740 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a  IOERR_DELETE;.}.
ff750 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65  ./*.** Check the
ff760 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73   existance and s
ff770 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e  tatus of a file.
ff780 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
ff790 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69  inAccess(.  sqli
ff7a0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20  te3_vfs *pVfs,  
ff7b0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73         /* Not us
ff7c0 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20  ed on win32 */. 
ff7d0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
ff7e0 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e  lename,     /* N
ff7f0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63  ame of file to c
ff800 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  heck */.  int fl
ff810 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
ff820 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20       /* Type of 
ff830 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20  test to make on 
ff840 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69  this file */.  i
ff850 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20  nt *pResOut     
ff860 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
ff870 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20  : Result */.){. 
ff880 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20 69   DWORD attr;.  i
ff890 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 76 6f 69  nt rc = 0;.  voi
ff8a0 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  d *zConverted = 
ff8b0 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
ff8c0 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ame(zFilename);.
ff8d0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
ff8e0 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20  ER(pVfs);.  if( 
ff8f0 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b  zConverted==0 ){
ff900 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
ff910 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
ff920 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
ff930 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65    attr = GetFile
ff940 41 74 74 72 69 62 75 74 65 73 57 28 28 57 43 48  AttributesW((WCH
ff950 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b  AR*)zConverted);
ff960 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
ff970 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ff980 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
ff990 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
ff9a0 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65  cuted. .** Since
ff9b0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69   the ASCII versi
ff9c0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64  on of these Wind
ff9d0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65  ows API do not e
ff9e0 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a  xist for WINCE,.
ff9f0 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e  ** it's importan
ffa00 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e  t to not referen
ffa10 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43  ce them for WINC
ffa20 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66  E builds..*/.#if
ffa30 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ffa40 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
ffa50 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41   attr = GetFileA
ffa60 74 74 72 69 62 75 74 65 73 41 28 28 63 68 61 72  ttributesA((char
ffa70 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23  *)zConverted);.#
ffa80 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65  endif.  }.  free
ffa90 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
ffaa0 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b  switch( flags ){
ffab0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
ffac0 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20  _ACCESS_READ:.  
ffad0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
ffae0 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20  CESS_EXISTS:.   
ffaf0 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d 49 4e     rc = attr!=IN
ffb00 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49  VALID_FILE_ATTRI
ffb10 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62 72 65  BUTES;.      bre
ffb20 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
ffb30 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57  ITE_ACCESS_READW
ffb40 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d  RITE:.      rc =
ffb50 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f 41 54   (attr & FILE_AT
ffb60 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59  TRIBUTE_READONLY
ffb70 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72 65 61  )==0;.      brea
ffb80 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a  k;.    default:.
ffb90 20 20 20 20 20 20 61 73 73 65 72 74 28 21 22 49        assert(!"I
ffba0 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67  nvalid flags arg
ffbb0 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a  ument");.  }.  *
ffbc0 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20  pResOut = rc;.  
ffbd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
ffbe0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e  ;.}.../*.** Turn
ffbf0 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68   a relative path
ffc00 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c  name into a full
ffc10 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74   pathname.  Writ
ffc20 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61  e the full.** pa
ffc30 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 74  thname into zOut
ffc40 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c  [].  zOut[] will
ffc50 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 56 66   be at least pVf
ffc60 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a  s->mxPathname.**
ffc70 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a   bytes in size..
ffc80 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
ffc90 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20  nFullPathname(. 
ffca0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
ffcb0 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  fs,            /
ffcc0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73  * Pointer to vfs
ffcd0 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e   object */.  con
ffce0 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69  st char *zRelati
ffcf0 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  ve,        /* Po
ffd00 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20  ssibly relative 
ffd10 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20  input path */.  
ffd20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20  int nFull,      
ffd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ffd40 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20   Size of output 
ffd50 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20  buffer in bytes 
ffd60 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c  */.  char *zFull
ffd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffd80 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66     /* Output buf
ffd90 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66  fer */.){.  .#if
ffda0 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49   defined(__CYGWI
ffdb0 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f 50 41  N__).  UNUSED_PA
ffdc0 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a  RAMETER(nFull);.
ffdd0 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f    cygwin_conv_to
ffde0 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68  _full_win32_path
ffdf0 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c  (zRelative, zFul
ffe00 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  l);.  return SQL
ffe10 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a  ITE_OK;.#endif..
ffe20 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
ffe30 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  NCE.  UNUSED_PAR
ffe40 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20  AMETER(nFull);. 
ffe50 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e 6f   /* WinCE has no
ffe60 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72 65   concept of a re
ffe70 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2c  lative pathname,
ffe80 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64   or so I am told
ffe90 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
ffea0 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78  nprintf(pVfs->mx
ffeb0 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c  Pathname, zFull,
ffec0 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 76 65   "%s", zRelative
ffed0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
ffee0 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23  TE_OK;.#endif..#
ffef0 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49  if !SQLITE_OS_WI
fff00 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65 64 28  NCE && !defined(
fff10 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e  __CYGWIN__).  in
fff20 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64 20  t nByte;.  void 
fff30 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63  *zConverted;.  c
fff40 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55  har *zOut;.  UNU
fff50 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 46  SED_PARAMETER(nF
fff60 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74  ull);.  zConvert
fff70 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  ed = convertUtf8
fff80 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69  Filename(zRelati
fff90 76 65 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 28  ve);.  if( isNT(
fffa0 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20 2a  ) ){.    WCHAR *
fffb0 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65  zTemp;.    nByte
fffc0 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61   = GetFullPathNa
fffd0 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e  meW((WCHAR*)zCon
fffe0 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29  verted, 0, 0, 0)
ffff0 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20   + 3;.    zTemp 
10000 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a  = malloc( nByte*
10001 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29  sizeof(zTemp[0])
10002 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d   );.    if( zTem
10003 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72  p==0 ){.      fr
10004 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
10005 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
10006 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
10007 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68  .    GetFullPath
10008 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43  NameW((WCHAR*)zC
10009 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c  onverted, nByte,
1000a 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20   zTemp, 0);.    
1000b 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
1000c 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69  ;.    zOut = uni
1000d 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d 70  codeToUtf8(zTemp
1000e 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 6d  );.    free(zTem
1000f 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73  p);./* isNT() is
10010 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
10011 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
10012 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
10013 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69  executed. .** Si
10014 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65  nce the ASCII ve
10015 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57  rsion of these W
10016 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f  indows API do no
10017 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43  t exist for WINC
10018 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72  E,.** it's impor
10019 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65  tant to not refe
1001a 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57  rence them for W
1001b 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a  INCE builds..*/.
1001c 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
1001d 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a  NCE==0.  }else{.
1001e 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b      char *zTemp;
1001f 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74  .    nByte = Get
10020 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63  FullPathNameA((c
10021 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c  har*)zConverted,
10022 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20   0, 0, 0) + 3;. 
10023 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f     zTemp = mallo
10024 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28  c( nByte*sizeof(
10025 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20  zTemp[0]) );.   
10026 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b   if( zTemp==0 ){
10027 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e  .      free(zCon
10028 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72  verted);.      r
10029 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1002a 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 65  EM;.    }.    Ge
1002b 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28  tFullPathNameA((
1002c 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64  char*)zConverted
1002d 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20  , nByte, zTemp, 
1002e 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f  0);.    free(zCo
1002f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f  nverted);.    zO
10030 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e  ut = sqlite3_win
10031 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28  32_mbcs_to_utf8(
10032 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65  zTemp);.    free
10033 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a  (zTemp);.#endif.
10034 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 20 29    }.  if( zOut )
10035 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
10036 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50  printf(pVfs->mxP
10037 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20  athname, zFull, 
10038 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20  "%s", zOut);.   
10039 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20   free(zOut);.   
1003a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1003b 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
1003c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1003d 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  MEM;.  }.#endif.
1003e 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1003f 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
10040 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  NSION./*.** Inte
10041 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69  rfaces for openi
10042 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72  ng a shared libr
10043 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74  ary, finding ent
10044 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74  ry points.** wit
10045 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c  hin the shared l
10046 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73  ibrary, and clos
10047 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
10048 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a  ibrary..*/./*.**
10049 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20   Interfaces for 
1004a 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64  opening a shared
1004b 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e   library, findin
1004c 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a  g entry points.*
1004d 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61  * within the sha
1004e 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64  red library, and
1004f 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61   closing the sha
10050 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  red library..*/.
10051 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69 6e  static void *win
10052 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  DlOpen(sqlite3_v
10053 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20  fs *pVfs, const 
10054 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
10055 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20  {.  HANDLE h;.  
10056 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64  void *zConverted
10057 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69   = convertUtf8Fi
10058 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65  lename(zFilename
10059 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
1005a 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69  METER(pVfs);.  i
1005b 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30  f( zConverted==0
1005c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
1005d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 54  ;.  }.  if( isNT
1005e 28 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 4c 6f  () ){.    h = Lo
1005f 61 64 4c 69 62 72 61 72 79 57 28 28 57 43 48 41  adLibraryW((WCHA
10060 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  R*)zConverted);.
10061 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
10062 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
10063 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
10064 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
10065 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20  uted. .** Since 
10066 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f  the ASCII versio
10067 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f  n of these Windo
10068 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78  ws API do not ex
10069 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a  ist for WINCE,.*
1006a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74  * it's important
1006b 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63   to not referenc
1006c 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45  e them for WINCE
1006d 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20   builds..*/.#if 
1006e 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
1006f 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
10070 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79 41  h = LoadLibraryA
10071 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74  ((char*)zConvert
10072 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  ed);.#endif.  }.
10073 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65    free(zConverte
10074 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f  d);.  return (vo
10075 69 64 2a 29 68 3b 0a 7d 0a 73 74 61 74 69 63 20  id*)h;.}.static 
10076 76 6f 69 64 20 77 69 6e 44 6c 45 72 72 6f 72 28  void winDlError(
10077 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
10078 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
10079 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 55  r *zBufOut){.  U
1007a 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
1007b 70 56 66 73 29 3b 0a 20 20 67 65 74 4c 61 73 74  pVfs);.  getLast
1007c 45 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a  ErrorMsg(nBuf, z
1007d 42 75 66 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 20  BufOut);.}.void 
1007e 28 2a 77 69 6e 44 6c 53 79 6d 28 73 71 6c 69 74  (*winDlSym(sqlit
1007f 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f  e3_vfs *pVfs, vo
10080 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e  id *pHandle, con
10081 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c  st char *zSymbol
10082 29 29 28 76 6f 69 64 29 7b 0a 20 20 55 4e 55 53  ))(void){.  UNUS
10083 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
10084 73 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  s);.#if SQLITE_O
10085 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 54 68 65  S_WINCE.  /* The
10086 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 41   GetProcAddressA
10087 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e  () routine is on
10088 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  ly available on 
10089 77 69 6e 63 65 2e 20 2a 2f 0a 20 20 72 65 74 75  wince. */.  retu
1008a 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64  rn (void(*)(void
1008b 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 73  ))GetProcAddress
1008c 41 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c  A((HANDLE)pHandl
1008d 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c  e, zSymbol);.#el
1008e 73 65 0a 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 65  se.  /* All othe
1008f 72 20 77 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f  r windows platfo
10090 72 6d 73 20 65 78 70 65 63 74 20 47 65 74 50 72  rms expect GetPr
10091 6f 63 41 64 64 72 65 73 73 28 29 20 74 6f 20 74  ocAddress() to t
10092 61 6b 65 0a 20 20 2a 2a 20 61 6e 20 41 6e 73 69  ake.  ** an Ansi
10093 20 73 74 72 69 6e 67 20 72 65 67 61 72 64 6c 65   string regardle
10094 73 73 20 6f 66 20 74 68 65 20 5f 55 4e 49 43 4f  ss of the _UNICO
10095 44 45 20 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20  DE setting */.  
10096 72 65 74 75 72 6e 20 28 76 6f 69 64 28 2a 29 28  return (void(*)(
10097 76 6f 69 64 29 29 47 65 74 50 72 6f 63 41 64 64  void))GetProcAdd
10098 72 65 73 73 28 28 48 41 4e 44 4c 45 29 70 48 61  ress((HANDLE)pHa
10099 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a  ndle, zSymbol);.
1009a 23 65 6e 64 69 66 0a 7d 0a 76 6f 69 64 20 77 69  #endif.}.void wi
1009b 6e 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  nDlClose(sqlite3
1009c 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64  _vfs *pVfs, void
1009d 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e   *pHandle){.  UN
1009e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
1009f 56 66 73 29 3b 0a 20 20 46 72 65 65 4c 69 62 72  Vfs);.  FreeLibr
100a0 61 72 79 28 28 48 41 4e 44 4c 45 29 70 48 61 6e  ary((HANDLE)pHan
100a1 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a  dle);.}.#else /*
100a2 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   if SQLITE_OMIT_
100a3 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69  LOAD_EXTENSION i
100a4 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20  s defined: */.  
100a5 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 4f 70 65  #define winDlOpe
100a6 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77  n  0.  #define w
100a7 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64  inDlError 0.  #d
100a8 65 66 69 6e 65 20 77 69 6e 44 6c 53 79 6d 20 20  efine winDlSym  
100a9 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e   0.  #define win
100aa 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66  DlClose 0.#endif
100ab 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75  .../*.** Write u
100ac 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20  p to nBuf bytes 
100ad 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e  of randomness in
100ae 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74  to zBuf..*/.stat
100af 69 63 20 69 6e 74 20 77 69 6e 52 61 6e 64 6f 6d  ic int winRandom
100b0 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73  ness(sqlite3_vfs
100b1 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66   *pVfs, int nBuf
100b2 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
100b3 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 55 4e   int n = 0;.  UN
100b4 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
100b5 56 66 73 29 3b 0a 23 69 66 20 64 65 66 69 6e 65  Vfs);.#if define
100b6 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20  d(SQLITE_TEST). 
100b7 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d   n = nBuf;.  mem
100b8 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75  set(zBuf, 0, nBu
100b9 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20  f);.#else.  if( 
100ba 73 69 7a 65 6f 66 28 53 59 53 54 45 4d 54 49 4d  sizeof(SYSTEMTIM
100bb 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20  E)<=nBuf-n ){.  
100bc 20 20 53 59 53 54 45 4d 54 49 4d 45 20 78 3b 0a    SYSTEMTIME x;.
100bd 20 20 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d      GetSystemTim
100be 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70  e(&x);.    memcp
100bf 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20  y(&zBuf[n], &x, 
100c0 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20  sizeof(x));.    
100c1 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a  n += sizeof(x);.
100c2 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66    }.  if( sizeof
100c3 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20  (DWORD)<=nBuf-n 
100c4 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 70 69 64  ){.    DWORD pid
100c5 20 3d 20 47 65 74 43 75 72 72 65 6e 74 50 72 6f   = GetCurrentPro
100c6 63 65 73 73 49 64 28 29 3b 0a 20 20 20 20 6d 65  cessId();.    me
100c7 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
100c8 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29  pid, sizeof(pid)
100c9 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
100ca 6f 66 28 70 69 64 29 3b 0a 20 20 7d 0a 20 20 69  of(pid);.  }.  i
100cb 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29  f( sizeof(DWORD)
100cc 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20  <=nBuf-n ){.    
100cd 44 57 4f 52 44 20 63 6e 74 20 3d 20 47 65 74 54  DWORD cnt = GetT
100ce 69 63 6b 43 6f 75 6e 74 28 29 3b 0a 20 20 20 20  ickCount();.    
100cf 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c  memcpy(&zBuf[n],
100d0 20 26 63 6e 74 2c 20 73 69 7a 65 6f 66 28 63 6e   &cnt, sizeof(cn
100d1 74 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69  t));.    n += si
100d2 7a 65 6f 66 28 63 6e 74 29 3b 0a 20 20 7d 0a 20  zeof(cnt);.  }. 
100d3 20 69 66 28 20 73 69 7a 65 6f 66 28 4c 41 52 47   if( sizeof(LARG
100d4 45 5f 49 4e 54 45 47 45 52 29 3c 3d 6e 42 75 66  E_INTEGER)<=nBuf
100d5 2d 6e 20 29 7b 0a 20 20 20 20 4c 41 52 47 45 5f  -n ){.    LARGE_
100d6 49 4e 54 45 47 45 52 20 69 3b 0a 20 20 20 20 51  INTEGER i;.    Q
100d7 75 65 72 79 50 65 72 66 6f 72 6d 61 6e 63 65 43  ueryPerformanceC
100d8 6f 75 6e 74 65 72 28 26 69 29 3b 0a 20 20 20 20  ounter(&i);.    
100d9 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c  memcpy(&zBuf[n],
100da 20 26 69 2c 20 73 69 7a 65 6f 66 28 69 29 29 3b   &i, sizeof(i));
100db 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66  .    n += sizeof
100dc 28 69 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  (i);.  }.#endif.
100dd 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a    return n;.}...
100de 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20  /*.** Sleep for 
100df 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20  a little while. 
100e0 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
100e1 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74  nt of time slept
100e2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
100e3 77 69 6e 53 6c 65 65 70 28 73 71 6c 69 74 65 33  winSleep(sqlite3
100e4 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
100e5 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20 53 6c 65  microsec){.  Sle
100e6 65 70 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 39  ep((microsec+999
100e7 29 2f 31 30 30 30 29 3b 0a 20 20 55 4e 55 53 45  )/1000);.  UNUSE
100e8 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
100e9 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6d 69  );.  return ((mi
100ea 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 30  crosec+999)/1000
100eb 29 2a 31 30 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  )*1000;.}../*.**
100ec 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
100ed 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74 20  ariable, if set 
100ee 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61  to a non-zero va
100ef 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74 68 65  lue, becomes the
100f0 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72   result.** retur
100f1 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
100f2 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29 2e  OsCurrentTime().
100f3 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66    This is used f
100f4 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23  or testing..*/.#
100f5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
100f6 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
100f7 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
100f8 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69  _time = 0;.#endi
100f9 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  f../*.** Find th
100fa 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28  e current time (
100fb 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f  in Universal Coo
100fc 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20  rdinated Time). 
100fd 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75   Write the.** cu
100fe 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64  rrent time and d
100ff 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20  ate as a Julian 
10100 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20  Day number into 
10101 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65  *prNow and.** re
10102 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20  turn 0.  Return 
10103 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e  1 if the time an
10104 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65  d date cannot be
10105 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 77   found..*/.int w
10106 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71  inCurrentTime(sq
10107 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
10108 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b   double *prNow){
10109 0a 20 20 46 49 4c 45 54 49 4d 45 20 66 74 3b 0a  .  FILETIME ft;.
1010a 20 20 2f 2a 20 46 49 4c 45 54 49 4d 45 20 73 74    /* FILETIME st
1010b 72 75 63 74 75 72 65 20 69 73 20 61 20 36 34 2d  ructure is a 64-
1010c 62 69 74 20 76 61 6c 75 65 20 72 65 70 72 65 73  bit value repres
1010d 65 6e 74 69 6e 67 20 74 68 65 20 6e 75 6d 62 65  enting the numbe
1010e 72 20 6f 66 20 0a 20 20 20 20 20 31 30 30 2d 6e  r of .     100-n
1010f 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72 76  anosecond interv
10110 61 6c 73 20 73 69 6e 63 65 20 4a 61 6e 75 61 72  als since Januar
10111 79 20 31 2c 20 31 36 30 31 20 28 3d 20 4a 44 20  y 1, 1601 (= JD 
10112 32 33 30 35 38 31 33 2e 35 29 2e 20 0a 20 20 2a  2305813.5). .  *
10113 2f 0a 20 20 64 6f 75 62 6c 65 20 6e 6f 77 3b 0a  /.  double now;.
10114 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
10115 4e 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d 45  NCE.  SYSTEMTIME
10116 20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 74   time;.  GetSyst
10117 65 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a 20  emTime(&time);. 
10118 20 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69 6d   /* if SystemTim
10119 65 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 61  eToFileTime() fa
1011a 69 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20  ils, it returns 
1011b 7a 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 21  zero. */.  if (!
1011c 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65  SystemTimeToFile
1011d 54 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 29  Time(&time,&ft))
1011e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
1011f 20 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 53    }.#else.  GetS
10120 79 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54  ystemTimeAsFileT
10121 69 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e 64  ime( &ft );.#end
10122 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  if.  UNUSED_PARA
10123 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 6e  METER(pVfs);.  n
10124 6f 77 20 3d 20 28 28 64 6f 75 62 6c 65 29 66 74  ow = ((double)ft
10125 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65 29  .dwHighDateTime)
10126 20 2a 20 34 32 39 34 39 36 37 32 39 36 2e 30 3b   * 4294967296.0;
10127 20 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 6e 6f   .  *prNow = (no
10128 77 20 2b 20 66 74 2e 64 77 4c 6f 77 44 61 74 65  w + ft.dwLowDate
10129 54 69 6d 65 29 2f 38 36 34 30 30 30 30 30 30 30  Time)/8640000000
1012a 30 30 2e 30 20 2b 20 32 33 30 35 38 31 33 2e 35  00.0 + 2305813.5
1012b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
1012c 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74  TEST.  if( sqlit
1012d 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20  e3_current_time 
1012e 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20  ){.    *prNow = 
1012f 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f  sqlite3_current_
10130 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32  time/86400.0 + 2
10131 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65  440587.5;.  }.#e
10132 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b  ndif.  return 0;
10133 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 64  .}../*.** The id
10134 65 61 20 69 73 20 74 68 61 74 20 74 68 69 73 20  ea is that this 
10135 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 6c  function works l
10136 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f  ike a combinatio
10137 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 74 45  n of.** GetLastE
10138 72 72 6f 72 28 29 20 61 6e 64 20 46 6f 72 6d 61  rror() and Forma
10139 74 4d 65 73 73 61 67 65 28 29 20 6f 6e 20 77 69  tMessage() on wi
1013a 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 6e 6f 20  ndows (or errno 
1013b 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 6f 72 5f  and.** strerror_
1013c 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e 20 41 66  r() on unix). Af
1013d 74 65 72 20 61 6e 20 65 72 72 6f 72 20 69 73 20  ter an error is 
1013e 72 65 74 75 72 6e 65 64 20 62 79 20 61 6e 20 4f  returned by an O
1013f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 53  S.** function, S
10140 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73  QLite calls this
10141 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 7a   function with z
10142 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a  Buf pointing to.
10143 2a 2a 20 61 20 62 75 66 66 65 72 20 6f 66 20 6e  ** a buffer of n
10144 42 75 66 20 62 79 74 65 73 2e 20 54 68 65 20 4f  Buf bytes. The O
10145 53 20 6c 61 79 65 72 20 73 68 6f 75 6c 64 20 70  S layer should p
10146 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 62  opulate the.** b
10147 75 66 66 65 72 20 77 69 74 68 20 61 20 6e 75 6c  uffer with a nul
10148 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
10149 38 20 65 6e 63 6f 64 65 64 20 65 72 72 6f 72 20  8 encoded error 
1014a 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 73 63 72  message.** descr
1014b 69 62 69 6e 67 20 74 68 65 20 6c 61 73 74 20 49  ibing the last I
1014c 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 76 65 20  O error to have 
1014d 6f 63 63 75 72 65 64 20 77 69 74 68 69 6e 20 74  occured within t
1014e 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68  he calling.** th
1014f 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  read..**.** If t
10150 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
10151 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f   is too large fo
10152 72 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 62  r the supplied b
10153 75 66 66 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f  uffer,.** it sho
10154 75 6c 64 20 62 65 20 74 72 75 6e 63 61 74 65 64  uld be truncated
10155 2e 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  . The return val
10156 75 65 20 6f 66 20 78 47 65 74 4c 61 73 74 45 72  ue of xGetLastEr
10157 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69  ror.** is zero i
10158 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  f the error mess
10159 61 67 65 20 66 69 74 73 20 69 6e 20 74 68 65 20  age fits in the 
1015a 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a  buffer, or non-z
1015b 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65  ero.** otherwise
1015c 20 28 69 66 20 74 68 65 20 6d 65 73 73 61 67 65   (if the message
1015d 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e   was truncated).
1015e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20   If non-zero is 
1015f 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65  returned,.** the
10160 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65  n it is not nece
10161 73 73 61 72 79 20 74 6f 20 69 6e 63 6c 75 64 65  ssary to include
10162 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61   the nul-termina
10163 74 6f 72 20 63 68 61 72 61 63 74 65 72 0a 2a 2a  tor character.**
10164 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62   in the output b
10165 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  uffer..**.** Not
10166 20 73 75 70 70 6c 79 69 6e 67 20 61 6e 20 65 72   supplying an er
10167 72 6f 72 20 6d 65 73 73 61 67 65 20 77 69 6c 6c  ror message will
10168 20 68 61 76 65 20 6e 6f 20 61 64 76 65 72 73 65   have no adverse
10169 20 65 66 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51   effect.** on SQ
1016a 4c 69 74 65 2e 20 49 74 20 69 73 20 66 69 6e 65  Lite. It is fine
1016b 20 74 6f 20 68 61 76 65 20 61 6e 20 69 6d 70 6c   to have an impl
1016c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20  ementation that 
1016d 6e 65 76 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73  never.** returns
1016e 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1016f 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78  e:.**.**   int x
10170 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c  GetLastError(sql
10171 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
10172 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
10173 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73  zBuf){.**     as
10174 73 65 72 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c  sert(zBuf[0]=='\
10175 30 27 29 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75  0');.**     retu
10176 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a  rn 0;.**   }.**.
10177 2a 2a 20 48 6f 77 65 76 65 72 20 69 66 20 61 6e  ** However if an
10178 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
10179 73 20 73 75 70 70 6c 69 65 64 2c 20 69 74 20 77  s supplied, it w
1017a 69 6c 6c 20 62 65 20 69 6e 63 6f 72 70 6f 72 61  ill be incorpora
1017b 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65  ted.** by sqlite
1017c 20 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20   into the error 
1017d 6d 65 73 73 61 67 65 20 61 76 61 69 6c 61 62 6c  message availabl
1017e 65 20 74 6f 20 74 68 65 20 75 73 65 72 20 75 73  e to the user us
1017f 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65  ing.** sqlite3_e
10180 72 72 6d 73 67 28 29 2c 20 70 6f 73 73 69 62 6c  rrmsg(), possibl
10181 79 20 6d 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f  y making IO erro
10182 72 73 20 65 61 73 69 65 72 20 74 6f 20 64 65 62  rs easier to deb
10183 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ug..*/.static in
10184 74 20 77 69 6e 47 65 74 4c 61 73 74 45 72 72 6f  t winGetLastErro
10185 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  r(sqlite3_vfs *p
10186 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63  Vfs, int nBuf, c
10187 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e  har *zBuf){.  UN
10188 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
10189 56 66 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 67  Vfs);.  return g
1018a 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 6e  etLastErrorMsg(n
1018b 42 75 66 2c 20 7a 42 75 66 29 3b 0a 7d 0a 0a 2f  Buf, zBuf);.}../
1018c 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
1018d 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65  and deinitialize
1018e 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
1018f 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e  ystem interface.
10190 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
10191 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  nt sqlite3_os_in
10192 69 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  it(void){.  stat
10193 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 77  ic sqlite3_vfs w
10194 69 6e 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c  inVfs = {.    1,
10195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10196 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
10197 20 20 20 20 73 69 7a 65 6f 66 28 77 69 6e 46 69      sizeof(winFi
10198 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69  le),   /* szOsFi
10199 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58 5f 50 41  le */.    MAX_PA
1019a 54 48 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  TH,          /* 
1019b 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20  mxPathname */.  
1019c 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
1019d 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f       /* pNext */
1019e 0a 20 20 20 20 22 77 69 6e 33 32 22 2c 20 20 20  .    "win32",   
1019f 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65          /* zName
101a0 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
101a1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41             /* pA
101a2 70 70 44 61 74 61 20 2a 2f 0a 20 0a 20 20 20 20  ppData */. .    
101a3 77 69 6e 4f 70 65 6e 2c 20 20 20 20 20 20 20 20  winOpen,        
101a4 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20     /* xOpen */. 
101a5 20 20 20 77 69 6e 44 65 6c 65 74 65 2c 20 20 20     winDelete,   
101a6 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65        /* xDelete
101a7 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63 63 65 73   */.    winAcces
101a8 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41  s,         /* xA
101a9 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e  ccess */.    win
101aa 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20  FullPathname,   
101ab 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65  /* xFullPathname
101ac 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 4f 70 65   */.    winDlOpe
101ad 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44  n,         /* xD
101ae 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e  lOpen */.    win
101af 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20  DlError,        
101b0 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20  /* xDlError */. 
101b1 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20 20 20 20     winDlSym,    
101b2 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20        /* xDlSym 
101b3 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 43 6c 6f 73  */.    winDlClos
101b4 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c  e,        /* xDl
101b5 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 77 69 6e  Close */.    win
101b6 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20  Randomness,     
101b7 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a  /* xRandomness *
101b8 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65 70 2c 20  /.    winSleep, 
101b9 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65           /* xSle
101ba 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e 43 75 72  ep */.    winCur
101bb 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20  rentTime,    /* 
101bc 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a  xCurrentTime */.
101bd 20 20 20 20 77 69 6e 47 65 74 4c 61 73 74 45 72      winGetLastEr
101be 72 6f 72 20 20 20 20 2f 2a 20 78 47 65 74 4c 61  ror    /* xGetLa
101bf 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a  stError */.  };.
101c0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65    sqlite3_vfs_re
101c1 67 69 73 74 65 72 28 26 77 69 6e 56 66 73 2c 20  gister(&winVfs, 
101c2 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  1);.  return SQL
101c3 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 51 4c 49 54  ITE_OK; .}.SQLIT
101c4 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
101c5 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20  3_os_end(void){ 
101c6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
101c7 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  _OK;.}..#endif /
101c8 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20  * SQLITE_OS_WIN 
101c9 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
101ca 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 77 69  *** End of os_wi
101cb 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
101cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101ce 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
101cf 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
101d0 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  itvec.c ********
101d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101d3 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 46  **/./*.** 2008 F
101d4 65 62 72 75 61 72 79 20 31 36 0a 2a 2a 0a 2a 2a  ebruary 16.**.**
101d5 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
101d6 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
101d7 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
101d8 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
101d9 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
101da 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
101db 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
101dc 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
101dd 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
101de 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
101df 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
101e0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
101e1 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
101e2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
101e3 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
101e4 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
101e5 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
101e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
101eb 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
101ec 73 20 61 6e 20 6f 62 6a 65 63 74 20 74 68 61 74  s an object that
101ed 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 66 69   represents a fi
101ee 78 65 64 2d 6c 65 6e 67 74 68 0a 2a 2a 20 62 69  xed-length.** bi
101ef 74 6d 61 70 2e 20 20 42 69 74 73 20 61 72 65 20  tmap.  Bits are 
101f0 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e  numbered startin
101f1 67 20 77 69 74 68 20 31 2e 0a 2a 2a 0a 2a 2a 20  g with 1..**.** 
101f2 41 20 62 69 74 6d 61 70 20 69 73 20 75 73 65 64  A bitmap is used
101f3 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 63 68   to record which
101f4 20 70 61 67 65 73 20 6f 66 20 61 20 64 61 74 61   pages of a data
101f5 62 61 73 65 20 66 69 6c 65 20 68 61 76 65 20 62  base file have b
101f6 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65  een.** journalle
101f7 64 20 64 75 72 69 6e 67 20 61 20 74 72 61 6e 73  d during a trans
101f8 61 63 74 69 6f 6e 2c 20 6f 72 20 77 68 69 63 68  action, or which
101f9 20 70 61 67 65 73 20 68 61 76 65 20 74 68 65 20   pages have the 
101fa 22 64 6f 6e 74 2d 77 72 69 74 65 22 0a 2a 2a 20  "dont-write".** 
101fb 70 72 6f 70 65 72 74 79 2e 20 20 55 73 75 61 6c  property.  Usual
101fc 6c 79 20 6f 6e 6c 79 20 61 20 66 65 77 20 70 61  ly only a few pa
101fd 67 65 73 20 61 72 65 20 6d 65 65 74 20 65 69 74  ges are meet eit
101fe 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a  her condition..*
101ff 2a 20 53 6f 20 74 68 65 20 62 69 74 6d 61 70 20  * So the bitmap 
10200 69 73 20 75 73 75 61 6c 6c 79 20 73 70 61 72 73  is usually spars
10201 65 20 61 6e 64 20 68 61 73 20 6c 6f 77 20 63 61  e and has low ca
10202 72 64 69 6e 61 6c 69 74 79 2e 0a 2a 2a 20 42 75  rdinality..** Bu
10203 74 20 73 6f 6d 65 74 69 6d 65 73 20 28 66 6f 72  t sometimes (for
10204 20 65 78 61 6d 70 6c 65 20 77 68 65 6e 20 64 75   example when du
10205 72 69 6e 67 20 61 20 44 52 4f 50 20 6f 66 20 61  ring a DROP of a
10206 20 6c 61 72 67 65 20 74 61 62 6c 65 29 20 6d 6f   large table) mo
10207 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66 20  st.** or all of 
10208 74 68 65 20 70 61 67 65 73 20 69 6e 20 61 20 64  the pages in a d
10209 61 74 61 62 61 73 65 20 63 61 6e 20 67 65 74 20  atabase can get 
1020a 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 20 49 6e 20  journalled.  In 
1020b 74 68 6f 73 65 20 63 61 73 65 73 2c 20 0a 2a 2a  those cases, .**
1020c 20 74 68 65 20 62 69 74 6d 61 70 20 62 65 63 6f   the bitmap beco
1020d 6d 65 73 20 64 65 6e 73 65 20 77 69 74 68 20 68  mes dense with h
1020e 69 67 68 20 63 61 72 64 69 6e 61 6c 69 74 79 2e  igh cardinality.
1020f 20 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20    The algorithm 
10210 6e 65 65 64 73 20 0a 2a 2a 20 74 6f 20 68 61 6e  needs .** to han
10211 64 6c 65 20 62 6f 74 68 20 63 61 73 65 73 20 77  dle both cases w
10212 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ell..**.** The s
10213 69 7a 65 20 6f 66 20 74 68 65 20 62 69 74 6d 61  ize of the bitma
10214 70 20 69 73 20 66 69 78 65 64 20 77 68 65 6e 20  p is fixed when 
10215 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 63 72  the object is cr
10216 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c  eated..**.** All
10217 20 62 69 74 73 20 61 72 65 20 63 6c 65 61 72 20   bits are clear 
10218 77 68 65 6e 20 74 68 65 20 62 69 74 6d 61 70 20  when the bitmap 
10219 69 73 20 63 72 65 61 74 65 64 2e 20 20 49 6e 64  is created.  Ind
1021a 69 76 69 64 75 61 6c 20 62 69 74 73 0a 2a 2a 20  ividual bits.** 
1021b 6d 61 79 20 62 65 20 73 65 74 20 6f 72 20 63 6c  may be set or cl
1021c 65 61 72 65 64 20 6f 6e 65 20 61 74 20 61 20 74  eared one at a t
1021d 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 20  ime..**.** Test 
1021e 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 61  operations are a
1021f 62 6f 75 74 20 31 30 30 20 74 69 6d 65 73 20 6d  bout 100 times m
10220 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20  ore common that 
10221 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a  set operations..
10222 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72 61 74 69  ** Clear operati
10223 6f 6e 73 20 61 72 65 20 65 78 63 65 65 64 69 6e  ons are exceedin
10224 67 6c 79 20 72 61 72 65 2e 20 20 54 68 65 72 65  gly rare.  There
10225 20 61 72 65 20 75 73 75 61 6c 6c 79 20 62 65 74   are usually bet
10226 77 65 65 6e 0a 2a 2a 20 35 20 61 6e 64 20 35 30  ween.** 5 and 50
10227 30 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73  0 set operations
10228 20 70 65 72 20 42 69 74 76 65 63 20 6f 62 6a 65   per Bitvec obje
10229 63 74 2c 20 74 68 6f 75 67 68 20 74 68 65 20 6e  ct, though the n
1022a 75 6d 62 65 72 20 6f 66 20 73 65 74 73 20 63 61  umber of sets ca
1022b 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 67  n.** sometimes g
1022c 72 6f 77 20 69 6e 74 6f 20 74 65 6e 73 20 6f 66  row into tens of
1022d 20 74 68 6f 75 73 61 6e 64 73 20 6f 72 20 6c 61   thousands or la
1022e 72 67 65 72 2e 20 20 54 68 65 20 73 69 7a 65 20  rger.  The size 
1022f 6f 66 20 74 68 65 0a 2a 2a 20 42 69 74 76 65 63  of the.** Bitvec
10230 20 6f 62 6a 65 63 74 20 69 73 20 74 68 65 20 6e   object is the n
10231 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
10232 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
10233 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 73 74  ile at the.** st
10234 61 72 74 20 6f 66 20 61 20 74 72 61 6e 73 61 63  art of a transac
10235 74 69 6f 6e 2c 20 61 6e 64 20 69 73 20 74 68 75  tion, and is thu
10236 73 20 75 73 75 61 6c 6c 79 20 6c 65 73 73 20 74  s usually less t
10237 68 61 6e 20 61 20 66 65 77 20 74 68 6f 75 73 61  han a few thousa
10238 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 61 6e 20 62  nd,.** but can b
10239 65 20 61 73 20 6c 61 72 67 65 20 61 73 20 32 20  e as large as 2 
1023a 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 61 20 72 65  billion for a re
1023b 61 6c 6c 79 20 62 69 67 20 64 61 74 61 62 61 73  ally big databas
1023c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  e..**.** @(#) $I
1023d 64 3a 20 62 69 74 76 65 63 2e 63 2c 76 20 31 2e  d: bitvec.c,v 1.
1023e 31 30 20 32 30 30 39 2f 30 31 2f 30 32 20 32 31  10 2009/01/02 21
1023f 3a 33 39 3a 33 39 20 64 72 68 20 45 78 70 20 24  :39:39 drh Exp $
10240 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20  .*/../* Size of 
10241 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75 63  the Bitvec struc
10242 74 75 72 65 20 69 6e 20 62 79 74 65 73 2e 20 2a  ture in bytes. *
10243 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43  /.#define BITVEC
10244 5f 53 5a 20 20 20 20 20 20 20 20 35 31 32 0a 0a  _SZ        512..
10245 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20 75 6e 69  /* Round the uni
10246 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20  on size down to 
10247 74 68 65 20 6e 65 61 72 65 73 74 20 70 6f 69 6e  the nearest poin
10248 74 65 72 20 62 6f 75 6e 64 61 72 79 2c 20 73 69  ter boundary, si
10249 6e 63 65 20 74 68 61 74 27 73 20 68 6f 77 20 0a  nce that's how .
1024a 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65 20 61 6c  ** it will be al
1024b 69 67 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65  igned within the
1024c 20 42 69 74 76 65 63 20 73 74 72 75 63 74 2e 20   Bitvec struct. 
1024d 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  */.#define BITVE
1024e 43 5f 55 53 49 5a 45 20 20 20 20 20 28 28 28 42  C_USIZE     (((B
1024f 49 54 56 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65  ITVEC_SZ-(3*size
10250 6f 66 28 75 33 32 29 29 29 2f 73 69 7a 65 6f 66  of(u32)))/sizeof
10251 28 42 69 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f  (Bitvec*))*sizeo
10252 66 28 42 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20  f(Bitvec*))../* 
10253 54 79 70 65 20 6f 66 20 74 68 65 20 61 72 72 61  Type of the arra
10254 79 20 22 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20  y "element" for 
10255 74 68 65 20 62 69 74 6d 61 70 20 72 65 70 72 65  the bitmap repre
10256 73 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53  sentation. .** S
10257 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 77 65 72  hould be a power
10258 20 6f 66 20 32 2c 20 61 6e 64 20 69 64 65 61 6c   of 2, and ideal
10259 6c 79 2c 20 65 76 65 6e 6c 79 20 64 69 76 69 64  ly, evenly divid
1025a 65 20 69 6e 74 6f 20 42 49 54 56 45 43 5f 55 53  e into BITVEC_US
1025b 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67  IZE. .** Setting
1025c 20 74 68 69 73 20 74 6f 20 74 68 65 20 22 6e 61   this to the "na
1025d 74 75 72 61 6c 20 77 6f 72 64 22 20 73 69 7a 65  tural word" size
1025e 20 6f 66 20 79 6f 75 72 20 43 50 55 20 6d 61 79   of your CPU may
1025f 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66   improve.** perf
10260 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66  ormance. */.#def
10261 69 6e 65 20 42 49 54 56 45 43 5f 54 45 4c 45 4d  ine BITVEC_TELEM
10262 20 20 20 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c       u8./* Size,
10263 20 69 6e 20 62 69 74 73 2c 20 6f 66 20 74 68 65   in bits, of the
10264 20 62 69 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e   bitmap element.
10265 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56   */.#define BITV
10266 45 43 5f 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f  EC_SZELEM    8./
10267 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
10268 65 6e 74 73 20 69 6e 20 61 20 62 69 74 6d 61 70  ents in a bitmap
10269 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69   array. */.#defi
1026a 6e 65 20 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20  ne BITVEC_NELEM 
1026b 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a      (BITVEC_USIZ
1026c 45 2f 73 69 7a 65 6f 66 28 42 49 54 56 45 43 5f  E/sizeof(BITVEC_
1026d 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65  TELEM))./* Numbe
1026e 72 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65  r of bits in the
1026f 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a   bitmap array. *
10270 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43  /.#define BITVEC
10271 5f 4e 42 49 54 20 20 20 20 20 20 28 42 49 54 56  _NBIT      (BITV
10272 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f  EC_NELEM*BITVEC_
10273 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62  SZELEM)../* Numb
10274 65 72 20 6f 66 20 75 33 32 20 76 61 6c 75 65 73  er of u32 values
10275 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20   in hash table. 
10276 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  */.#define BITVE
10277 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49 54  C_NINT      (BIT
10278 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66  VEC_USIZE/sizeof
10279 28 75 33 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75  (u32))./* Maximu
1027a 6d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  m number of entr
1027b 69 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c  ies in hash tabl
1027c 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62  e before .** sub
1027d 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65  -dividing and re
1027e 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65  -hashing. */.#de
1027f 66 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 41  fine BITVEC_MXHA
10280 53 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e 49  SH    (BITVEC_NI
10281 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67  NT/2)./* Hashing
10282 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68   function for th
10283 65 20 61 48 61 73 68 20 72 65 70 72 65 73 65 6e  e aHash represen
10284 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72  tation..** Empir
10285 69 63 61 6c 20 74 65 73 74 69 6e 67 20 73 68 6f  ical testing sho
10286 77 65 64 20 74 68 61 74 20 74 68 65 20 2a 33 37  wed that the *37
10287 20 6d 75 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20   multiplier .** 
10288 28 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 72  (an arbitrary pr
10289 69 6d 65 29 69 6e 20 74 68 65 20 68 61 73 68 20  ime)in the hash 
1028a 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69 64 65  function provide
1028b 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63  d .** no fewer c
1028c 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 61 6e 20 74  ollisions than t
1028d 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a  he no-op *1. */.
1028e 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 48  #define BITVEC_H
1028f 41 53 48 28 58 29 20 20 20 28 28 28 58 29 2a 31  ASH(X)   (((X)*1
10290 29 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a  )%BITVEC_NINT)..
10291 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e  #define BITVEC_N
10292 50 54 52 20 20 20 20 20 20 28 42 49 54 56 45 43  PTR      (BITVEC
10293 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69  _USIZE/sizeof(Bi
10294 74 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a  tvec *)).../*.**
10295 20 41 20 62 69 74 6d 61 70 20 69 73 20 61 6e 20   A bitmap is an 
10296 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
10297 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
10298 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ure..**.** This 
10299 62 69 74 6d 61 70 20 72 65 63 6f 72 64 73 20 74  bitmap records t
1029a 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20  he existance of 
1029b 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74  zero or more bit
1029c 73 0a 2a 2a 20 77 69 74 68 20 76 61 6c 75 65 73  s.** with values
1029d 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69   between 1 and i
1029e 53 69 7a 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e  Size, inclusive.
1029f 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
102a0 20 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65 20   three possible 
102a1 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20  representations 
102a2 6f 66 20 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a  of the bitmap..*
102a3 2a 20 49 66 20 69 53 69 7a 65 3c 3d 42 49 54 56  * If iSize<=BITV
102a4 45 43 5f 4e 42 49 54 2c 20 74 68 65 6e 20 42 69  EC_NBIT, then Bi
102a5 74 76 65 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d  tvec.u.aBitmap[]
102a6 20 69 73 20 61 20 73 74 72 61 69 67 68 74 0a 2a   is a straight.*
102a7 2a 20 62 69 74 6d 61 70 2e 20 20 54 68 65 20 6c  * bitmap.  The l
102a8 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74  east significant
102a9 20 62 69 74 20 69 73 20 62 69 74 20 31 2e 0a 2a   bit is bit 1..*
102aa 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3e 42 49  *.** If iSize>BI
102ab 54 56 45 43 5f 4e 42 49 54 20 61 6e 64 20 69 44  TVEC_NBIT and iD
102ac 69 76 69 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42  ivisor==0 then B
102ad 69 74 76 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20  itvec.u.aHash[] 
102ae 69 73 0a 2a 2a 20 61 20 68 61 73 68 20 74 61 62  is.** a hash tab
102af 6c 65 20 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c  le that will hol
102b0 64 20 75 70 20 74 6f 20 42 49 54 56 45 43 5f 4d  d up to BITVEC_M
102b1 58 48 41 53 48 20 64 69 73 74 69 6e 63 74 20 76  XHASH distinct v
102b2 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  alues..**.** Oth
102b3 65 72 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75  erwise, the valu
102b4 65 20 69 20 69 73 20 72 65 64 69 72 65 63 74 65  e i is redirecte
102b5 64 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49  d into one of BI
102b6 54 56 45 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62  TVEC_NPTR.** sub
102b7 2d 62 69 74 6d 61 70 73 20 70 6f 69 6e 74 65 64  -bitmaps pointed
102b8 20 74 6f 20 62 79 20 42 69 74 76 65 63 2e 75 2e   to by Bitvec.u.
102b9 61 70 53 75 62 5b 5d 2e 20 20 45 61 63 68 20 73  apSub[].  Each s
102ba 75 62 62 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64  ubbitmap.** hand
102bb 6c 65 73 20 75 70 20 74 6f 20 69 44 69 76 69 73  les up to iDivis
102bc 6f 72 20 73 65 70 61 72 61 74 65 20 76 61 6c 75  or separate valu
102bd 65 73 20 6f 66 20 69 2e 20 20 61 70 53 75 62 5b  es of i.  apSub[
102be 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75  0] holds.** valu
102bf 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  es between 1 and
102c0 20 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75   iDivisor.  apSu
102c1 62 5b 31 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65  b[1] holds value
102c2 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69  s between.** iDi
102c3 76 69 73 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44  visor+1 and 2*iD
102c4 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e  ivisor.  apSub[N
102c5 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62  ] holds values b
102c6 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76  etween.** N*iDiv
102c7 69 73 6f 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29  isor+1 and (N+1)
102c8 2a 69 44 69 76 69 73 6f 72 2e 20 20 45 61 63 68  *iDivisor.  Each
102c9 20 73 75 62 62 69 74 6d 61 70 20 69 73 20 6e 6f   subbitmap is no
102ca 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68  rmalized.** to h
102cb 6f 6c 64 20 64 65 61 6c 20 77 69 74 68 20 76 61  old deal with va
102cc 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61  lues between 1 a
102cd 6e 64 20 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a  nd iDivisor..*/.
102ce 73 74 72 75 63 74 20 42 69 74 76 65 63 20 7b 0a  struct Bitvec {.
102cf 20 20 75 33 32 20 69 53 69 7a 65 3b 20 20 20 20    u32 iSize;    
102d0 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74    /* Maximum bit
102d1 20 69 6e 64 65 78 2e 20 20 4d 61 78 20 69 53 69   index.  Max iSi
102d2 7a 65 20 69 73 20 34 2c 32 39 34 2c 39 36 37 2c  ze is 4,294,967,
102d3 32 39 36 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53  296. */.  u32 nS
102d4 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  et;       /* Num
102d5 62 65 72 20 6f 66 20 62 69 74 73 20 74 68 61 74  ber of bits that
102d6 20 61 72 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20   are set - only 
102d7 76 61 6c 69 64 20 66 6f 72 20 61 48 61 73 68 20  valid for aHash 
102d8 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20  element */.     
102d9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
102da 4d 61 78 20 6e 53 65 74 20 69 73 20 42 49 54 56  Max nSet is BITV
102db 45 43 5f 4e 49 4e 54 2e 20 20 46 6f 72 20 42 49  EC_NINT.  For BI
102dc 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 20  TVEC_SZ of 512, 
102dd 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 31 32  this would be 12
102de 35 2e 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76  5. */.  u32 iDiv
102df 69 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65  isor;   /* Numbe
102e0 72 20 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65  r of bits handle
102e1 64 20 62 79 20 65 61 63 68 20 61 70 53 75 62 5b  d by each apSub[
102e2 5d 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20  ] entry. */.    
102e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
102e4 20 53 68 6f 75 6c 64 20 3e 3d 30 20 66 6f 72 20   Should >=0 for 
102e5 61 70 53 75 62 20 65 6c 65 6d 65 6e 74 2e 20 2a  apSub element. *
102e6 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
102e7 20 20 20 20 2f 2a 20 4d 61 78 20 69 44 69 76 69      /* Max iDivi
102e8 73 6f 72 20 69 73 20 6d 61 78 28 75 33 32 29 20  sor is max(u32) 
102e9 2f 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2b 20  / BITVEC_NPTR + 
102ea 31 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  1.  */.         
102eb 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
102ec 61 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35  a BITVEC_SZ of 5
102ed 31 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62  12, this would b
102ee 65 20 33 34 2c 33 35 39 2c 37 33 39 2e 20 2a 2f  e 34,359,739. */
102ef 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 42  .  union {.    B
102f0 49 54 56 45 43 5f 54 45 4c 45 4d 20 61 42 69 74  ITVEC_TELEM aBit
102f1 6d 61 70 5b 42 49 54 56 45 43 5f 4e 45 4c 45 4d  map[BITVEC_NELEM
102f2 5d 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20  ];    /* Bitmap 
102f3 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a  representation *
102f4 2f 0a 20 20 20 20 75 33 32 20 61 48 61 73 68 5b  /.    u32 aHash[
102f5 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20  BITVEC_NINT];   
102f6 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65     /* Hash table
102f7 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
102f8 2a 2f 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61  */.    Bitvec *a
102f9 70 53 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52  pSub[BITVEC_NPTR
102fa 5d 3b 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65  ];  /* Recursive
102fb 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
102fc 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a  */.  } u;.};../*
102fd 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
102fe 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61   bitmap object a
102ff 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69  ble to handle bi
10300 74 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64  ts between 0 and
10301 20 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75   iSize,.** inclu
10302 73 69 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20  sive.  Return a 
10303 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
10304 65 77 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75  ew object.  Retu
10305 72 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d  rn NULL if .** m
10306 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a  alloc fails..*/.
10307 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42  SQLITE_PRIVATE B
10308 69 74 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69  itvec *sqlite3Bi
10309 74 76 65 63 43 72 65 61 74 65 28 75 33 32 20 69  tvecCreate(u32 i
1030a 53 69 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20  Size){.  Bitvec 
1030b 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69  *p;.  assert( si
1030c 7a 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43  zeof(*p)==BITVEC
1030d 5f 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c  _SZ );.  p = sql
1030e 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20  ite3MallocZero( 
1030f 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20  sizeof(*p) );.  
10310 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e  if( p ){.    p->
10311 69 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20  iSize = iSize;. 
10312 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
10313 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
10314 20 73 65 65 20 69 66 20 74 68 65 20 69 2d 74 68   see if the i-th
10315 20 62 69 74 20 69 73 20 73 65 74 2e 20 20 52 65   bit is set.  Re
10316 74 75 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c  turn true or fal
10317 73 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e  se..** If p is N
10318 55 4c 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d  ULL (if the bitm
10319 61 70 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  ap has not been 
1031a 63 72 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a  created) or if.*
1031b 2a 20 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61  * i is out of ra
1031c 6e 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  nge, then return
1031d 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54   false..*/.SQLIT
1031e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1031f 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
10320 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69  Bitvec *p, u32 i
10321 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  ){.  if( p==0 ) 
10322 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
10323 69 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d  i>p->iSize || i=
10324 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
10325 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70   i--;.  while( p
10326 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20  ->iDivisor ){.  
10327 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d    u32 bin = i/p-
10328 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69  >iDivisor;.    i
10329 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72   = i%p->iDivisor
1032a 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61  ;.    p = p->u.a
1032b 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69  pSub[bin];.    i
1032c 66 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72  f (!p) {.      r
1032d 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
1032e 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a   }.  if( p->iSiz
1032f 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29  e<=BITVEC_NBIT )
10330 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 2d  {.    return (p-
10331 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54  >u.aBitmap[i/BIT
10332 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 20 28 31  VEC_SZELEM] & (1
10333 3c 3c 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45  <<(i&(BITVEC_SZE
10334 4c 45 4d 2d 31 29 29 29 29 21 3d 30 3b 0a 20 20  LEM-1))))!=0;.  
10335 7d 20 65 6c 73 65 7b 0a 20 20 20 20 75 33 32 20  } else{.    u32 
10336 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28  h = BITVEC_HASH(
10337 69 2b 2b 29 3b 0a 20 20 20 20 77 68 69 6c 65 28  i++);.    while(
10338 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29   p->u.aHash[h] )
10339 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75  {.      if( p->u
1033a 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72  .aHash[h]==i ) r
1033b 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 68  eturn 1;.      h
1033c 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 68 3e  ++;.      if( h>
1033d 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68  =BITVEC_NINT ) h
1033e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
1033f 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a  return 0;.  }.}.
10340 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 69  ./*.** Set the i
10341 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72 6e  -th bit.  Return
10342 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e   0 on success an
10343 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  d an error code 
10344 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 67  if.** anything g
10345 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a  oes wrong..**.**
10346 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69   This routine mi
10347 67 68 74 20 63 61 75 73 65 20 73 75 62 2d 62 69  ght cause sub-bi
10348 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c 6c 6f  tmaps to be allo
10349 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e 67 0a  cated.  Failing.
1034a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 6d 65  ** to get the me
1034b 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f 20 68  mory needed to h
1034c 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 74 6d  old the sub-bitm
1034d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a 2a  ap is the only.*
1034e 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 77 72  * that can go wr
1034f 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 73 65  ong with an inse
10350 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 20 61  rt, assuming p a
10351 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 2e 0a  nd i are valid..
10352 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e  **.** The callin
10353 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  g function must 
10354 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 69 73  ensure that p is
10355 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 63 20   a valid Bitvec 
10356 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74 68  object.** and th
10357 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72  at the value for
10358 20 22 69 22 20 69 73 20 77 69 74 68 69 6e 20 72   "i" is within r
10359 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 74 76  ange of the Bitv
1035a 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 74  ec object..** Ot
1035b 68 65 72 77 69 73 65 20 74 68 65 20 62 65 68 61  herwise the beha
1035c 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
1035d 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1035e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1035f 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65 63  BitvecSet(Bitvec
10360 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 75   *p, u32 i){.  u
10361 33 32 20 68 3b 0a 20 20 61 73 73 65 72 74 28 20  32 h;.  assert( 
10362 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  p!=0 );.  assert
10363 28 20 69 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ( i>0 );.  asser
10364 74 28 20 69 3c 3d 70 2d 3e 69 53 69 7a 65 20 29  t( i<=p->iSize )
10365 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65  ;.  i--;.  while
10366 28 28 70 2d 3e 69 53 69 7a 65 20 3e 20 42 49 54  ((p->iSize > BIT
10367 56 45 43 5f 4e 42 49 54 29 20 26 26 20 70 2d 3e  VEC_NBIT) && p->
10368 69 44 69 76 69 73 6f 72 29 20 7b 0a 20 20 20 20  iDivisor) {.    
10369 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69  u32 bin = i/p->i
1036a 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d  Divisor;.    i =
1036b 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a   i%p->iDivisor;.
1036c 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53      if( p->u.apS
1036d 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20  ub[bin]==0 ){.  
1036e 20 20 20 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62      p->u.apSub[b
1036f 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 74  in] = sqlite3Bit
10370 76 65 63 43 72 65 61 74 65 28 20 70 2d 3e 69 44  vecCreate( p->iD
10371 69 76 69 73 6f 72 20 29 3b 0a 20 20 20 20 20 20  ivisor );.      
10372 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62  if( p->u.apSub[b
10373 69 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  in]==0 ) return 
10374 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
10375 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75    }.    p = p->u
10376 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 7d  .apSub[bin];.  }
10377 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c  .  if( p->iSize<
10378 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a  =BITVEC_NBIT ){.
10379 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70      p->u.aBitmap
1037a 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d  [i/BITVEC_SZELEM
1037b 5d 20 7c 3d 20 31 20 3c 3c 20 28 69 26 28 42 49  ] |= 1 << (i&(BI
1037c 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 3b  TVEC_SZELEM-1));
1037d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1037e 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 20 3d  TE_OK;.  }.  h =
1037f 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 2b 2b   BITVEC_HASH(i++
10380 29 3b 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 65  );.  /* if there
10381 20 77 61 73 6e 27 74 20 61 20 68 61 73 68 20 63   wasn't a hash c
10382 6f 6c 6c 69 73 69 6f 6e 2c 20 61 6e 64 20 74 68  ollision, and th
10383 69 73 20 64 6f 65 73 6e 27 74 20 2a 2f 0a 20 20  is doesn't */.  
10384 2f 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20 66 69  /* completely fi
10385 6c 6c 20 74 68 65 20 68 61 73 68 2c 20 74 68 65  ll the hash, the
10386 6e 20 6a 75 73 74 20 61 64 64 20 69 74 20 77 69  n just add it wi
10387 74 68 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20 77 6f  thout */.  /* wo
10388 72 72 69 6e 67 20 61 62 6f 75 74 20 73 75 62 2d  rring about sub-
10389 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d  dividing and re-
1038a 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66  hashing. */.  if
1038b 28 20 21 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  ( !p->u.aHash[h]
1038c 20 29 7b 0a 20 20 20 20 69 66 20 28 70 2d 3e 6e   ){.    if (p->n
1038d 53 65 74 3c 28 42 49 54 56 45 43 5f 4e 49 4e 54  Set<(BITVEC_NINT
1038e 2d 31 29 29 20 7b 0a 20 20 20 20 20 20 67 6f 74  -1)) {.      got
1038f 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64  o bitvec_set_end
10390 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20  ;.    } else {. 
10391 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63       goto bitvec
10392 5f 73 65 74 5f 72 65 68 61 73 68 3b 0a 20 20 20  _set_rehash;.   
10393 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 74 68 65 72   }.  }.  /* ther
10394 65 20 77 61 73 20 61 20 63 6f 6c 6c 69 73 69 6f  e was a collisio
10395 6e 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20  n, check to see 
10396 69 66 20 69 74 27 73 20 61 6c 72 65 61 64 79 20  if it's already 
10397 2a 2f 0a 20 20 2f 2a 20 69 6e 20 68 61 73 68 2c  */.  /* in hash,
10398 20 69 66 20 6e 6f 74 2c 20 74 72 79 20 74 6f 20   if not, try to 
10399 66 69 6e 64 20 61 20 73 70 6f 74 20 66 6f 72 20  find a spot for 
1039a 69 74 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20 20 20  it */.  do {.   
1039b 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b   if( p->u.aHash[
1039c 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 53  h]==i ) return S
1039d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b  QLITE_OK;.    h+
1039e 2b 3b 0a 20 20 20 20 69 66 28 20 68 3e 3d 42 49  +;.    if( h>=BI
1039f 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20  TVEC_NINT ) h = 
103a0 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 70 2d  0;.  } while( p-
103a1 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 3b 0a 20  >u.aHash[h] );. 
103a2 20 2f 2a 20 77 65 20 64 69 64 6e 27 74 20 66 69   /* we didn't fi
103a3 6e 64 20 69 74 20 69 6e 20 74 68 65 20 68 61 73  nd it in the has
103a4 68 2e 20 20 68 20 70 6f 69 6e 74 73 20 74 6f 20  h.  h points to 
103a5 74 68 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 2f  the first */.  /
103a6 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 72 65 65  * available free
103a7 20 73 70 6f 74 2e 20 63 68 65 63 6b 20 74 6f 20   spot. check to 
103a8 73 65 65 20 69 66 20 74 68 69 73 20 69 73 20 67  see if this is g
103a9 6f 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f 2a 20  oing to */.  /* 
103aa 6d 61 6b 65 20 6f 75 72 20 68 61 73 68 20 74 6f  make our hash to
103ab 6f 20 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a 62 69  o "full".  */.bi
103ac 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3a  tvec_set_rehash:
103ad 0a 20 20 69 66 28 20 70 2d 3e 6e 53 65 74 3e 3d  .  if( p->nSet>=
103ae 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 29 7b  BITVEC_MXHASH ){
103af 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  .    unsigned in
103b0 74 20 6a 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b  t j;.    int rc;
103b1 0a 20 20 20 20 75 33 32 20 61 69 56 61 6c 75 65  .    u32 aiValue
103b2 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a  s[BITVEC_NINT];.
103b3 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c      memcpy(aiVal
103b4 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c  ues, p->u.aHash,
103b5 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73   sizeof(aiValues
103b6 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70  ));.    memset(p
103b7 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20 73 69  ->u.apSub, 0, si
103b8 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29 29 3b  zeof(aiValues));
103b9 0a 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f 72  .    p->iDivisor
103ba 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20 42   = (p->iSize + B
103bb 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29 2f  ITVEC_NPTR - 1)/
103bc 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20 20  BITVEC_NPTR;.   
103bd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74   rc = sqlite3Bit
103be 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a 20 20  vecSet(p, i);.  
103bf 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54    for(j=0; j<BIT
103c0 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a  VEC_NINT; j++){.
103c1 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c 75        if( aiValu
103c2 65 73 5b 6a 5d 20 29 20 72 63 20 7c 3d 20 73 71  es[j] ) rc |= sq
103c3 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70  lite3BitvecSet(p
103c4 2c 20 61 69 56 61 6c 75 65 73 5b 6a 5d 29 3b 0a  , aiValues[j]);.
103c5 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
103c6 20 72 63 3b 0a 20 20 7d 0a 62 69 74 76 65 63 5f   rc;.  }.bitvec_
103c7 73 65 74 5f 65 6e 64 3a 0a 20 20 70 2d 3e 6e 53  set_end:.  p->nS
103c8 65 74 2b 2b 3b 0a 20 20 70 2d 3e 75 2e 61 48 61  et++;.  p->u.aHa
103c9 73 68 5b 68 5d 20 3d 20 69 3b 0a 20 20 72 65 74  sh[h] = i;.  ret
103ca 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
103cb 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
103cc 65 20 69 2d 74 68 20 62 69 74 2e 0a 2a 2f 0a 53  e i-th bit..*/.S
103cd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
103ce 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  id sqlite3Bitvec
103cf 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a 70 2c  Clear(Bitvec *p,
103d0 20 75 33 32 20 69 29 7b 0a 20 20 61 73 73 65 72   u32 i){.  asser
103d1 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73  t( p!=0 );.  ass
103d2 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d  ert( i>0 );.  i-
103d3 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69  -;.  while( p->i
103d4 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75  Divisor ){.    u
103d5 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44  32 bin = i/p->iD
103d6 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20  ivisor;.    i = 
103d7 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20  i%p->iDivisor;. 
103d8 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75     p = p->u.apSu
103d9 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28  b[bin];.    if (
103da 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75  !p) {.      retu
103db 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
103dc 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49  if( p->iSize<=BI
103dd 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20  TVEC_NBIT ){.   
103de 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f   p->u.aBitmap[i/
103df 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26  BITVEC_SZELEM] &
103e0 3d 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 49 54  = ~(1 << (i&(BIT
103e1 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b  VEC_SZELEM-1)));
103e2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e  .  }else{.    un
103e3 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20  signed int j;.  
103e4 20 20 75 33 32 20 61 69 56 61 6c 75 65 73 5b 42    u32 aiValues[B
103e5 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a 20 20 20  ITVEC_NINT];.   
103e6 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 65 73   memcpy(aiValues
103e7 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69  , p->u.aHash, si
103e8 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29 29 3b  zeof(aiValues));
103e9 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75  .    memset(p->u
103ea 2e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f  .aHash, 0, sizeo
103eb 66 28 61 69 56 61 6c 75 65 73 29 29 3b 0a 20 20  f(aiValues));.  
103ec 20 20 70 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20    p->nSet = 0;. 
103ed 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49     for(j=0; j<BI
103ee 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b  TVEC_NINT; j++){
103ef 0a 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c  .      if( aiVal
103f0 75 65 73 5b 6a 5d 20 26 26 20 61 69 56 61 6c 75  ues[j] && aiValu
103f1 65 73 5b 6a 5d 21 3d 28 69 2b 31 29 20 29 7b 0a  es[j]!=(i+1) ){.
103f2 20 20 20 20 20 20 20 20 75 33 32 20 68 20 3d 20          u32 h = 
103f3 42 49 54 56 45 43 5f 48 41 53 48 28 61 69 56 61  BITVEC_HASH(aiVa
103f4 6c 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20  lues[j]-1);.    
103f5 20 20 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20      p->nSet++;. 
103f6 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d         while( p-
103f7 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20  >u.aHash[h] ){. 
103f8 20 20 20 20 20 20 20 20 20 68 2b 2b 3b 0a 20 20           h++;.  
103f9 20 20 20 20 20 20 20 20 69 66 28 20 68 3e 3d 42          if( h>=B
103fa 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d  ITVEC_NINT ) h =
103fb 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
103fc 20 20 20 20 20 20 70 2d 3e 75 2e 61 48 61 73 68        p->u.aHash
103fd 5b 68 5d 20 3d 20 61 69 56 61 6c 75 65 73 5b 6a  [h] = aiValues[j
103fe 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ];.      }.    }
103ff 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  .  }.}../*.** De
10400 73 74 72 6f 79 20 61 20 62 69 74 6d 61 70 20 6f  stroy a bitmap o
10401 62 6a 65 63 74 2e 20 20 52 65 63 6c 61 69 6d 20  bject.  Reclaim 
10402 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 2e  all memory used.
10403 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10404 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
10405 69 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 74  itvecDestroy(Bit
10406 76 65 63 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  vec *p){.  if( p
10407 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
10408 69 66 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20  if( p->iDivisor 
10409 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  ){.    unsigned 
1040a 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
1040b 3d 30 3b 20 69 3c 42 49 54 56 45 43 5f 4e 50 54  =0; i<BITVEC_NPT
1040c 52 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  R; i++){.      s
1040d 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
1040e 72 6f 79 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69  roy(p->u.apSub[i
1040f 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ]);.    }.  }.  
10410 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
10411 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
10412 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
10413 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56  TEST./*.** Let V
10414 5b 5d 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f  [] be an array o
10415 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61  f unsigned chara
10416 63 74 65 72 73 20 73 75 66 66 69 63 69 65 6e 74  cters sufficient
10417 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74   to hold.** up t
10418 6f 20 4e 20 62 69 74 73 2e 20 20 4c 65 74 20 49  o N bits.  Let I
10419 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62   be an integer b
1041a 65 74 77 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20  etween 0 and N. 
1041b 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e   0<=I<N..** Then
1041c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
1041d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65  acros can be use
1041e 64 20 74 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c  d to set, clear,
1041f 20 6f 72 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69   or test.** indi
10420 76 69 64 75 61 6c 20 62 69 74 73 20 77 69 74 68  vidual bits with
10421 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  in V..*/.#define
10422 20 53 45 54 42 49 54 28 56 2c 49 29 20 20 20 20   SETBIT(V,I)    
10423 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c    V[I>>3] |= (1<
10424 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20  <(I&7)).#define 
10425 43 4c 45 41 52 42 49 54 28 56 2c 49 29 20 20 20  CLEARBIT(V,I)   
10426 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c   V[I>>3] &= ~(1<
10427 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20  <(I&7)).#define 
10428 54 45 53 54 42 49 54 28 56 2c 49 29 20 20 20 20  TESTBIT(V,I)    
10429 20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49   (V[I>>3]&(1<<(I
1042a 26 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20  &7)))!=0../*.** 
1042b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e  This routine run
1042c 73 20 61 6e 20 65 78 74 65 6e 73 69 76 65 20 74  s an extensive t
1042d 65 73 74 20 6f 66 20 74 68 65 20 42 69 74 76 65  est of the Bitve
1042e 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  c code..**.** Th
1042f 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 61 72  e input is an ar
10430 72 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20  ray of integers 
10431 74 68 61 74 20 61 63 74 73 20 61 73 20 61 20 70  that acts as a p
10432 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73  rogram.** to tes
10433 74 20 74 68 65 20 42 69 74 76 65 63 2e 20 20 54  t the Bitvec.  T
10434 68 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20  he integers are 
10435 6f 70 63 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64  opcodes followed
10436 0a 2a 2a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20  .** by 0, 1, or 
10437 33 20 6f 70 65 72 61 6e 64 73 2c 20 64 65 70 65  3 operands, depe
10438 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63  nding on the opc
10439 6f 64 65 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a  ode.  Another.**
1043a 20 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20   opcode follows 
1043b 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
1043c 72 20 74 68 65 20 6c 61 73 74 20 6f 70 65 72 61  r the last opera
1043d 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  nd..**.** There 
1043e 61 72 65 20 36 20 6f 70 63 6f 64 65 73 20 6e 75  are 6 opcodes nu
1043f 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 20 74 68  mbered from 0 th
10440 72 6f 75 67 68 20 35 2e 20 20 30 20 69 73 20 74  rough 5.  0 is t
10441 68 65 0a 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63  he.** "halt" opc
10442 6f 64 65 20 61 6e 64 20 63 61 75 73 65 73 20 74  ode and causes t
10443 68 65 20 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a  he test to end..
10444 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20  **.**    0      
10445 20 20 20 20 48 61 6c 74 20 61 6e 64 20 72 65 74      Halt and ret
10446 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
10447 66 20 65 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31  f errors.**    1
10448 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20   N S X    Set N 
10449 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77  bits beginning w
1044a 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d  ith S and increm
1044b 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20  enting by X.**  
1044c 20 20 32 20 4e 20 53 20 58 20 20 20 20 43 6c 65    2 N S X    Cle
1044d 61 72 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e  ar N bits beginn
1044e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69  ing with S and i
1044f 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58  ncrementing by X
10450 0a 2a 2a 20 20 20 20 33 20 4e 20 20 20 20 20 20  .**    3 N      
10451 20 20 53 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79    Set N randomly
10452 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20   chosen bits.** 
10453 20 20 20 34 20 4e 20 20 20 20 20 20 20 20 43 6c     4 N        Cl
10454 65 61 72 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63  ear N randomly c
10455 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20  hosen bits.**   
10456 20 35 20 4e 20 53 20 58 20 20 20 20 53 65 74 20   5 N S X    Set 
10457 4e 20 62 69 74 73 20 66 72 6f 6d 20 53 20 69 6e  N bits from S in
10458 63 72 65 6d 65 6e 74 20 58 20 69 6e 20 61 72 72  crement X in arr
10459 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20  ay only, not in 
1045a 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65  bitvec.**.** The
1045b 20 6f 70 63 6f 64 65 73 20 31 20 74 68 72 6f 75   opcodes 1 throu
1045c 67 68 20 34 20 70 65 72 66 6f 72 6d 20 73 65 74  gh 4 perform set
1045d 20 61 6e 64 20 63 6c 65 61 72 20 6f 70 65 72 61   and clear opera
1045e 74 69 6f 6e 73 20 61 72 65 20 70 65 72 66 6f 72  tions are perfor
1045f 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61  med.** on both a
10460 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 61   Bitvec object a
10461 6e 64 20 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61  nd on a linear a
10462 72 72 61 79 20 6f 66 20 62 69 74 73 20 6f 62 74  rray of bits obt
10463 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
10464 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77  c..** Opcode 5 w
10465 6f 72 6b 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65  orks on the line
10466 61 72 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e  ar array only, n
10467 6f 74 20 6f 6e 20 74 68 65 20 42 69 74 76 65 63  ot on the Bitvec
10468 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 69 73  ..** Opcode 5 is
10469 20 75 73 65 64 20 74 6f 20 64 65 6c 69 62 65 72   used to deliber
1046a 61 74 65 6c 79 20 69 6e 64 75 63 65 20 61 20 66  ately induce a f
1046b 61 75 6c 74 20 69 6e 20 6f 72 64 65 72 20 74 6f  ault in order to
1046c 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20 74 68 61 74  .** confirm that
1046d 20 65 72 72 6f 72 20 64 65 74 65 63 74 69 6f 6e   error detection
1046e 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74   works..**.** At
1046f 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20   the conclusion 
10470 6f 66 20 74 68 65 20 74 65 73 74 20 74 68 65 20  of the test the 
10471 6c 69 6e 65 61 72 20 61 72 72 61 79 20 69 73 20  linear array is 
10472 63 6f 6d 70 61 72 65 64 0a 2a 2a 20 61 67 61 69  compared.** agai
10473 6e 73 74 20 74 68 65 20 42 69 74 76 65 63 20 6f  nst the Bitvec o
10474 62 6a 65 63 74 2e 20 20 49 66 20 74 68 65 72 65  bject.  If there
10475 20 61 72 65 20 61 6e 79 20 64 69 66 66 65 72 65   are any differe
10476 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f  nces,.** an erro
10477 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  r is returned.  
10478 49 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20  If they are the 
10479 73 61 6d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65  same, zero is re
1047a 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
1047b 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
1047c 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
1047d 73 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f  s, return -1..*/
1047e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1047f 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65  int sqlite3Bitve
10480 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74  cBuiltinTest(int
10481 20 73 7a 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a   sz, int *aOp){.
10482 20 20 42 69 74 76 65 63 20 2a 70 42 69 74 76 65    Bitvec *pBitve
10483 63 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65  c = 0;.  unsigne
10484 64 20 63 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a  d char *pV = 0;.
10485 20 20 69 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20    int rc = -1;. 
10486 20 69 6e 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20   int i, nx, pc, 
10487 6f 70 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  op;..  /* Alloca
10488 74 65 20 74 68 65 20 42 69 74 76 65 63 20 74 6f  te the Bitvec to
10489 20 62 65 20 74 65 73 74 65 64 20 61 6e 64 20 61   be tested and a
1048a 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66   linear array of
1048b 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f 20 61 63  .  ** bits to ac
1048c 74 20 61 73 20 74 68 65 20 72 65 66 65 72 65 6e  t as the referen
1048d 63 65 20 2a 2f 0a 20 20 70 42 69 74 76 65 63 20  ce */.  pBitvec 
1048e 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43  = sqlite3BitvecC
1048f 72 65 61 74 65 28 20 73 7a 20 29 3b 0a 20 20 70  reate( sz );.  p
10490 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  V = sqlite3_mall
10491 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31  oc( (sz+7)/8 + 1
10492 20 29 3b 0a 20 20 69 66 28 20 70 42 69 74 76 65   );.  if( pBitve
10493 63 3d 3d 30 20 7c 7c 20 70 56 3d 3d 30 20 29 20  c==0 || pV==0 ) 
10494 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b  goto bitvec_end;
10495 0a 20 20 6d 65 6d 73 65 74 28 70 56 2c 20 30 2c  .  memset(pV, 0,
10496 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 29 3b 0a   (sz+7)/8 + 1);.
10497 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 70 72  .  /* Run the pr
10498 6f 67 72 61 6d 20 2a 2f 0a 20 20 70 63 20 3d 20  ogram */.  pc = 
10499 30 3b 0a 20 20 77 68 69 6c 65 28 20 28 6f 70 20  0;.  while( (op 
1049a 3d 20 61 4f 70 5b 70 63 5d 29 21 3d 30 20 29 7b  = aOp[pc])!=0 ){
1049b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 70 20  .    switch( op 
1049c 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 31 3a  ){.      case 1:
1049d 0a 20 20 20 20 20 20 63 61 73 65 20 32 3a 0a 20  .      case 2:. 
1049e 20 20 20 20 20 63 61 73 65 20 35 3a 20 7b 0a 20       case 5: {. 
1049f 20 20 20 20 20 20 20 6e 78 20 3d 20 34 3b 0a 20         nx = 4;. 
104a0 20 20 20 20 20 20 20 69 20 3d 20 61 4f 70 5b 70         i = aOp[p
104a1 63 2b 32 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20  c+2] - 1;.      
104a2 20 20 61 4f 70 5b 70 63 2b 32 5d 20 2b 3d 20 61    aOp[pc+2] += a
104a3 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20 20 20 20 20  Op[pc+3];.      
104a4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
104a5 0a 20 20 20 20 20 20 63 61 73 65 20 33 3a 0a 20  .      case 3:. 
104a6 20 20 20 20 20 63 61 73 65 20 34 3a 20 0a 20 20       case 4: .  
104a7 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
104a8 20 20 20 20 20 20 20 6e 78 20 3d 20 32 3b 0a 20         nx = 2;. 
104a9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
104aa 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
104ab 28 69 29 2c 20 26 69 29 3b 0a 20 20 20 20 20 20  (i), &i);.      
104ac 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
104ad 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28  .    }.    if( (
104ae 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29 20 3e 20 30  --aOp[pc+1]) > 0
104af 20 29 20 6e 78 20 3d 20 30 3b 0a 20 20 20 20 70   ) nx = 0;.    p
104b0 63 20 2b 3d 20 6e 78 3b 0a 20 20 20 20 69 20 3d  c += nx;.    i =
104b1 20 28 69 20 26 20 30 78 37 66 66 66 66 66 66 66   (i & 0x7fffffff
104b2 29 25 73 7a 3b 0a 20 20 20 20 69 66 28 20 28 6f  )%sz;.    if( (o
104b3 70 20 26 20 31 29 21 3d 30 20 29 7b 0a 20 20 20  p & 1)!=0 ){.   
104b4 20 20 20 53 45 54 42 49 54 28 70 56 2c 20 28 69     SETBIT(pV, (i
104b5 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +1));.      if( 
104b6 6f 70 21 3d 35 20 29 7b 0a 20 20 20 20 20 20 20  op!=5 ){.       
104b7 20 69 66 28 20 73 71 6c 69 74 65 33 42 69 74 76   if( sqlite3Bitv
104b8 65 63 53 65 74 28 70 42 69 74 76 65 63 2c 20 69  ecSet(pBitvec, i
104b9 2b 31 29 20 29 20 67 6f 74 6f 20 62 69 74 76 65  +1) ) goto bitve
104ba 63 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20  c_end;.      }. 
104bb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
104bc 43 4c 45 41 52 42 49 54 28 70 56 2c 20 28 69 2b  CLEARBIT(pV, (i+
104bd 31 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  1));.      sqlit
104be 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 42  e3BitvecClear(pB
104bf 69 74 76 65 63 2c 20 69 2b 31 29 3b 0a 20 20 20  itvec, i+1);.   
104c0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73   }.  }..  /* Tes
104c1 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  t to make sure t
104c2 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20  he linear array 
104c3 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20  exactly matches 
104c4 74 68 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20  the.  ** Bitvec 
104c5 6f 62 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77  object.  Start w
104c6 69 74 68 20 74 68 65 20 61 73 73 75 6d 70 74 69  ith the assumpti
104c7 6f 6e 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a  on that they do.
104c8 20 20 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d    ** match (rc==
104c9 30 29 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74  0).  Change rc t
104ca 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20  o non-zero if a 
104cb 64 69 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a  discrepancy.  **
104cc 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a   is found..  */.
104cd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69    rc = sqlite3Bi
104ce 74 76 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20  tvecTest(0,0) + 
104cf 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
104d0 74 28 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29  t(pBitvec, sz+1)
104d1 0a 20 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c  .          + sql
104d2 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
104d3 42 69 74 76 65 63 2c 20 30 29 3b 0a 20 20 66 6f  Bitvec, 0);.  fo
104d4 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b  r(i=1; i<=sz; i+
104d5 2b 29 7b 0a 20 20 20 20 69 66 28 20 20 28 54 45  +){.    if(  (TE
104d6 53 54 42 49 54 28 70 56 2c 69 29 29 21 3d 73 71  STBIT(pV,i))!=sq
104d7 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
104d8 70 42 69 74 76 65 63 2c 69 29 20 29 7b 0a 20 20  pBitvec,i) ){.  
104d9 20 20 20 20 72 63 20 3d 20 69 3b 0a 20 20 20 20      rc = i;.    
104da 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
104db 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c   }..  /* Free al
104dc 6c 6f 63 61 74 65 64 20 73 74 72 75 63 74 75 72  located structur
104dd 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a  e */.bitvec_end:
104de 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
104df 70 56 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69  pV);.  sqlite3Bi
104e0 74 76 65 63 44 65 73 74 72 6f 79 28 70 42 69 74  tvecDestroy(pBit
104e1 76 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  vec);.  return r
104e2 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  c;.}.#endif /* S
104e3 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
104e4 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a  IN_TEST */../***
104e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
104e6 6f 66 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a  of bitvec.c ****
104e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
104e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
104e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
104ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
104eb 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 63 20  n file pcache.c 
104ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
104ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
104ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
104ef 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35  * 2008 August 05
104f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
104f1 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
104f2 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
104f3 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
104f4 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
104f5 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
104f6 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
104f7 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
104f8 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
104f9 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
104fa 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
104fb 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
104fc 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
104fd 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
104fe 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
104ff 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
10500 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
10501 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
10502 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10503 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10504 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10505 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
10506 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70  ** This file imp
10507 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 70 61 67  lements that pag
10508 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 40  e cache..**.** @
10509 28 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 2e  (#) $Id: pcache.
1050a 63 2c 76 20 31 2e 33 39 20 32 30 30 38 2f 31 32  c,v 1.39 2008/12
1050b 2f 30 34 20 32 30 3a 34 30 3a 31 30 20 64 72 68  /04 20:40:10 drh
1050c 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
1050d 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65   A complete page
1050e 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73   cache is an ins
1050f 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
10510 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
10511 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67  ct PCache {.  Pg
10512 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44  Hdr *pDirty, *pD
10513 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20  irtyTail;       
10514 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72    /* List of dir
10515 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20  ty pages in LRU 
10516 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72  order */.  PgHdr
10517 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20   *pSynced;      
10518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10519 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61  * Last synced pa
1051a 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65  ge in dirty page
1051b 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e   list */.  int n
1051c 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
1051d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1051e 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  * Number of refe
1051f 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a  renced pages */.
10520 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20    int nMax;     
10521 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10522 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75        /* Configu
10523 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a  red cache size *
10524 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e 3b 20 20 20  /.  int nMin;   
10525 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10526 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69          /* Confi
10527 67 75 72 65 64 20 6d 69 6e 69 6d 75 6d 20 63 61  gured minimum ca
10528 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e  che size */.  in
10529 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20  t szPage;       
1052a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1052b 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65    /* Size of eve
1052c 72 79 20 70 61 67 65 20 69 6e 20 74 68 69 73 20  ry page in this 
1052d 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73  cache */.  int s
1052e 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20  zExtra;         
1052f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10530 2a 20 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20  * Size of extra 
10531 73 70 61 63 65 20 66 6f 72 20 65 61 63 68 20 70  space for each p
10532 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75  age */.  int bPu
10533 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20  rgeable;        
10534 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10535 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
10536 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
10537 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  re */.  int (*xS
10538 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48  tress)(void*,PgH
10539 64 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43  dr*);       /* C
1053a 61 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20  all to try make 
1053b 61 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a  a page clean */.
1053c 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b    void *pStress;
1053d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1053e 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
1053f 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a  t to xStress */.
10540 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65    sqlite3_pcache
10541 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20   *pCache;       
10542 20 20 20 20 20 20 2f 2a 20 50 6c 75 67 67 61 62        /* Pluggab
10543 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 20  le cache module 
10544 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67  */.  PgHdr *pPag
10545 65 31 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f  e1;.};../*.** So
10546 6d 65 20 6f 66 20 74 68 65 20 61 73 73 65 72 74  me of the assert
10547 28 29 20 6d 61 63 72 6f 73 20 69 6e 20 74 68 69  () macros in thi
10548 73 20 63 6f 64 65 20 61 72 65 20 74 6f 6f 20 65  s code are too e
10549 78 70 65 6e 73 69 76 65 20 74 6f 20 72 75 6e 0a  xpensive to run.
1054a 2a 2a 20 65 76 65 6e 20 64 75 72 69 6e 67 20 6e  ** even during n
1054b 6f 72 6d 61 6c 20 64 65 62 75 67 67 69 6e 67 2e  ormal debugging.
1054c 20 20 55 73 65 20 74 68 65 6d 20 6f 6e 6c 79 20    Use them only 
1054d 72 61 72 65 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72  rarely on long-r
1054e 75 6e 6e 69 6e 67 0a 2a 2a 20 74 65 73 74 73 2e  unning.** tests.
1054f 20 20 45 6e 61 62 6c 65 20 74 68 65 20 65 78 70    Enable the exp
10550 65 6e 73 69 76 65 20 61 73 73 65 72 74 73 20 75  ensive asserts u
10551 73 69 6e 67 20 74 68 65 0a 2a 2a 20 2d 44 53 51  sing the.** -DSQ
10552 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45  LITE_ENABLE_EXPE
10553 4e 53 49 56 45 5f 41 53 53 45 52 54 3d 31 20 63  NSIVE_ASSERT=1 c
10554 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
10555 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  on..*/.#ifdef SQ
10556 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45  LITE_ENABLE_EXPE
10557 4e 53 49 56 45 5f 41 53 53 45 52 54 0a 23 20 64  NSIVE_ASSERT.# d
10558 65 66 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f  efine expensive_
10559 61 73 73 65 72 74 28 58 29 20 20 61 73 73 65 72  assert(X)  asser
1055a 74 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  t(X).#else.# def
1055b 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73  ine expensive_as
1055c 73 65 72 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a  sert(X).#endif..
1055d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
1055e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1055f 2a 2a 2a 20 4c 69 6e 6b 65 64 20 4c 69 73 74 20  *** Linked List 
10560 4d 61 6e 61 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a  Management *****
10561 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
10562 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  ..#if !defined(N
10563 44 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65  DEBUG) && define
10564 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
10565 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54  EXPENSIVE_ASSERT
10566 29 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  )./*.** Check th
10567 61 74 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70  at the pCache->p
10568 53 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20  Synced variable 
10569 69 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79  is set correctly
1056a 2e 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f  . If it.** is no
1056b 74 2c 20 65 69 74 68 65 72 20 66 61 69 6c 20 61  t, either fail a
1056c 6e 20 61 73 73 65 72 74 20 6f 72 20 72 65 74 75  n assert or retu
1056d 72 6e 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69  rn zero. Otherwi
1056e 73 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f  se, return.** no
1056f 6e 2d 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20  n-zero. This is 
10570 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62  only used in deb
10571 75 67 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61  ugging builds, a
10572 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
10573 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73     expensive_ass
10574 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b  ert( pcacheCheck
10575 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29  Synced(pCache) )
10576 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.*/.static int 
10577 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65  pcacheCheckSynce
10578 64 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  d(PCache *pCache
10579 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20  ){.  PgHdr *p;. 
1057a 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70   for(p=pCache->p
1057b 44 69 72 74 79 54 61 69 6c 3b 20 70 21 3d 70 43  DirtyTail; p!=pC
1057c 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 70  ache->pSynced; p
1057d 3d 70 2d 3e 70 44 69 72 74 79 50 72 65 76 29 7b  =p->pDirtyPrev){
1057e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1057f 6e 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67  nRef || (p->flag
10580 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
10581 43 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  C) );.  }.  retu
10582 72 6e 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e  rn (p==0 || p->n
10583 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73  Ref || (p->flags
10584 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
10585 29 3d 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  )==0);.}.#endif 
10586 2f 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51  /* !NDEBUG && SQ
10587 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45  LITE_ENABLE_EXPE
10588 4e 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a  NSIVE_ASSERT */.
10589 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 61  ./*.** Remove pa
1058a 67 65 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68  ge pPage from th
1058b 65 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20  e list of dirty 
1058c 70 61 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  pages..*/.static
1058d 20 76 6f 69 64 20 70 63 61 63 68 65 52 65 6d 6f   void pcacheRemo
1058e 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28  veFromDirtyList(
1058f 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20  PgHdr *pPage){. 
10590 20 50 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61   PCache *p = pPa
10591 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61  ge->pCache;..  a
10592 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44  ssert( pPage->pD
10593 69 72 74 79 4e 65 78 74 20 7c 7c 20 70 50 61 67  irtyNext || pPag
10594 65 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c  e==p->pDirtyTail
10595 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
10596 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20  age->pDirtyPrev 
10597 7c 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69  || pPage==p->pDi
10598 72 74 79 20 29 3b 0a 0a 20 20 2f 2a 20 55 70 64  rty );..  /* Upd
10599 61 74 65 20 74 68 65 20 50 43 61 63 68 65 31 2e  ate the PCache1.
1059a 70 53 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65  pSynced variable
1059b 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 2a   if necessary. *
1059c 2f 0a 20 20 69 66 28 20 70 2d 3e 70 53 79 6e 63  /.  if( p->pSync
1059d 65 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20  ed==pPage ){.   
1059e 20 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 20   PgHdr *pSynced 
1059f 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50  = pPage->pDirtyP
105a0 72 65 76 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  rev;.    while( 
105a1 70 53 79 6e 63 65 64 20 26 26 20 28 70 53 79 6e  pSynced && (pSyn
105a2 63 65 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  ced->flags&PGHDR
105a3 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20  _NEED_SYNC) ){. 
105a4 20 20 20 20 20 70 53 79 6e 63 65 64 20 3d 20 70       pSynced = p
105a5 53 79 6e 63 65 64 2d 3e 70 44 69 72 74 79 50 72  Synced->pDirtyPr
105a6 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  ev;.    }.    p-
105a7 3e 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63  >pSynced = pSync
105a8 65 64 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  ed;.  }..  if( p
105a9 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
105aa 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70   ){.    pPage->p
105ab 44 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74  DirtyNext->pDirt
105ac 79 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70  yPrev = pPage->p
105ad 44 69 72 74 79 50 72 65 76 3b 0a 20 20 7d 65 6c  DirtyPrev;.  }el
105ae 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
105af 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79  pPage==p->pDirty
105b0 54 61 69 6c 20 29 3b 0a 20 20 20 20 70 2d 3e 70  Tail );.    p->p
105b1 44 69 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67  DirtyTail = pPag
105b2 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20  e->pDirtyPrev;. 
105b3 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e   }.  if( pPage->
105b4 70 44 69 72 74 79 50 72 65 76 20 29 7b 0a 20 20  pDirtyPrev ){.  
105b5 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50    pPage->pDirtyP
105b6 72 65 76 2d 3e 70 44 69 72 74 79 4e 65 78 74 20  rev->pDirtyNext 
105b7 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e  = pPage->pDirtyN
105b8 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ext;.  }else{.  
105b9 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d    assert( pPage=
105ba 3d 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 20 20  =p->pDirty );.  
105bb 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50    p->pDirty = pP
105bc 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b  age->pDirtyNext;
105bd 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 70 44  .  }.  pPage->pD
105be 69 72 74 79 4e 65 78 74 20 3d 20 30 3b 0a 20 20  irtyNext = 0;.  
105bf 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
105c0 76 20 3d 20 30 3b 0a 0a 20 20 65 78 70 65 6e 73  v = 0;..  expens
105c1 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63  ive_assert( pcac
105c2 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29  heCheckSynced(p)
105c3 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64   );.}../*.** Add
105c4 20 70 61 67 65 20 70 50 61 67 65 20 74 6f 20 74   page pPage to t
105c5 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64  he head of the d
105c6 69 72 74 79 20 6c 69 73 74 20 28 50 43 61 63 68  irty list (PCach
105c7 65 31 2e 70 44 69 72 74 79 20 69 73 20 73 65 74  e1.pDirty is set
105c8 20 74 6f 0a 2a 2a 20 70 50 61 67 65 29 2e 0a 2a   to.** pPage)..*
105c9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
105ca 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69  acheAddToDirtyLi
105cb 73 74 28 50 67 48 64 72 20 2a 70 50 61 67 65 29  st(PgHdr *pPage)
105cc 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d 20  {.  PCache *p = 
105cd 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a  pPage->pCache;..
105ce 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
105cf 3e 70 44 69 72 74 79 4e 65 78 74 3d 3d 30 20 26  >pDirtyNext==0 &
105d0 26 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50  & pPage->pDirtyP
105d1 72 65 76 3d 3d 30 20 26 26 20 70 2d 3e 70 44 69  rev==0 && p->pDi
105d2 72 74 79 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20  rty!=pPage );.. 
105d3 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65   pPage->pDirtyNe
105d4 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a  xt = p->pDirty;.
105d5 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69    if( pPage->pDi
105d6 72 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 61  rtyNext ){.    a
105d7 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44  ssert( pPage->pD
105d8 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79  irtyNext->pDirty
105d9 50 72 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 70  Prev==0 );.    p
105da 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
105db 2d 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70  ->pDirtyPrev = p
105dc 50 61 67 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  Page;.  }.  p->p
105dd 44 69 72 74 79 20 3d 20 70 50 61 67 65 3b 0a 20  Dirty = pPage;. 
105de 20 69 66 28 20 21 70 2d 3e 70 44 69 72 74 79 54   if( !p->pDirtyT
105df 61 69 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 70 44  ail ){.    p->pD
105e0 69 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65  irtyTail = pPage
105e1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 2d 3e  ;.  }.  if( !p->
105e2 70 53 79 6e 63 65 64 20 26 26 20 30 3d 3d 28 70  pSynced && 0==(p
105e3 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44  Page->flags&PGHD
105e4 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a  R_NEED_SYNC) ){.
105e5 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d      p->pSynced =
105e6 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 65 78   pPage;.  }.  ex
105e7 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20  pensive_assert( 
105e8 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65  pcacheCheckSynce
105e9 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  d(p) );.}../*.**
105ea 20 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20   Wrapper around 
105eb 74 68 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61  the pluggable ca
105ec 63 68 65 73 20 78 55 6e 70 69 6e 20 6d 65 74 68  ches xUnpin meth
105ed 6f 64 2e 20 49 66 20 74 68 65 20 63 61 63 68 65  od. If the cache
105ee 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 75 73 65   is.** being use
105ef 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  d for an in-memo
105f0 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 69  ry database, thi
105f1 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
105f2 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
105f3 20 76 6f 69 64 20 70 63 61 63 68 65 55 6e 70 69   void pcacheUnpi
105f4 6e 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50  n(PgHdr *p){.  P
105f5 43 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20  Cache *pCache = 
105f6 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 69 66 28  p->pCache;.  if(
105f7 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61   pCache->bPurgea
105f8 62 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ble ){.    if( p
105f9 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20  ->pgno==1 ){.   
105fa 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65     pCache->pPage
105fb 31 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  1 = 0;.    }.   
105fc 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
105fd 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70  nfig.pcache.xUnp
105fe 69 6e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  in(pCache->pCach
105ff 65 2c 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a  e, p, 0);.  }.}.
10600 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
10601 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10602 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10603 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e  ***** General In
10604 74 65 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a  terfaces ******.
10605 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  **.** Initialize
10606 20 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68   and shutdown th
10607 65 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62  e page cache sub
10608 73 79 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20  system. Neither 
10609 6f 66 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e  of these .** fun
1060a 63 74 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61  ctions are threa
1060b 64 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  dsafe..*/.SQLITE
1060c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1060d 69 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61  ite3PcacheInitia
1060e 6c 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66  lize(void){.  if
1060f 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
10610 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e  onfig.pcache.xIn
10611 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  it==0 ){.    sql
10612 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66  ite3PCacheSetDef
10613 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 72 65  ault();.  }.  re
10614 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62  turn sqlite3Glob
10615 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10616 78 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f  xInit(sqlite3Glo
10617 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
10618 2e 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45  .pArg);.}.SQLITE
10619 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1061a 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64  lite3PcacheShutd
1061b 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28  own(void){.  if(
1061c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
1061d 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75  nfig.pcache.xShu
1061e 74 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c  tdown ){.    sql
1061f 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
10620 2e 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77  .pcache.xShutdow
10621 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  n(sqlite3GlobalC
10622 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72  onfig.pcache.pAr
10623 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  g);.  }.}../*.**
10624 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65   Return the size
10625 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50   in bytes of a P
10626 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  Cache object..*/
10627 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10628 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
10629 65 53 69 7a 65 28 76 6f 69 64 29 7b 20 72 65 74  eSize(void){ ret
1062a 75 72 6e 20 73 69 7a 65 6f 66 28 50 43 61 63 68  urn sizeof(PCach
1062b 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  e); }../*.** Cre
1062c 61 74 65 20 61 20 6e 65 77 20 50 43 61 63 68 65  ate a new PCache
1062d 20 6f 62 6a 65 63 74 2e 20 53 74 6f 72 61 67 65   object. Storage
1062e 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   space to hold t
1062f 68 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 68 61 73  he object.** has
10630 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c   already been al
10631 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 73 20 70  located and is p
10632 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20  assed in as the 
10633 70 20 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 54  p pointer. .** T
10634 68 65 20 63 61 6c 6c 65 72 20 64 69 73 63 6f 76  he caller discov
10635 65 72 73 20 68 6f 77 20 6d 75 63 68 20 73 70 61  ers how much spa
10636 63 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61  ce needs to be a
10637 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20  llocated by .** 
10638 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50  calling sqlite3P
10639 63 61 63 68 65 53 69 7a 65 28 29 2e 0a 2a 2f 0a  cacheSize()..*/.
1063a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1063b 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
1063c 65 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50  eOpen(.  int szP
1063d 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  age,            
1063e 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1063f 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20   every page */. 
10640 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20   int szExtra,   
10641 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10642 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73   Extra space ass
10643 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63  ociated with eac
10644 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  h page */.  int 
10645 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20  bPurgeable,     
10646 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
10647 20 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e   if pages are on
10648 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a   backing store *
10649 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73  /.  int (*xStres
1064a 73 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29  s)(void*,PgHdr*)
1064b 2c 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20  ,/* Call to try 
1064c 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c  to make pages cl
1064d 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ean */.  void *p
1064e 53 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20  Stress,         
1064f 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
10650 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a  t to xStress */.
10651 20 20 50 43 61 63 68 65 20 2a 70 20 20 20 20 20    PCache *p     
10652 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10653 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73  * Preallocated s
10654 70 61 63 65 20 66 6f 72 20 74 68 65 20 50 43 61  pace for the PCa
10655 63 68 65 20 2a 2f 0a 29 7b 0a 20 20 6d 65 6d 73  che */.){.  mems
10656 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28  et(p, 0, sizeof(
10657 50 43 61 63 68 65 29 29 3b 0a 20 20 70 2d 3e 73  PCache));.  p->s
10658 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a  zPage = szPage;.
10659 20 20 70 2d 3e 73 7a 45 78 74 72 61 20 3d 20 73    p->szExtra = s
1065a 7a 45 78 74 72 61 3b 0a 20 20 70 2d 3e 62 50 75  zExtra;.  p->bPu
1065b 72 67 65 61 62 6c 65 20 3d 20 62 50 75 72 67 65  rgeable = bPurge
1065c 61 62 6c 65 3b 0a 20 20 70 2d 3e 78 53 74 72 65  able;.  p->xStre
1065d 73 73 20 3d 20 78 53 74 72 65 73 73 3b 0a 20 20  ss = xStress;.  
1065e 70 2d 3e 70 53 74 72 65 73 73 20 3d 20 70 53 74  p->pStress = pSt
1065f 72 65 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61 78 20  ress;.  p->nMax 
10660 3d 20 31 30 30 3b 0a 20 20 70 2d 3e 6e 4d 69 6e  = 100;.  p->nMin
10661 20 3d 20 31 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   = 10;.}../*.** 
10662 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20  Change the page 
10663 73 69 7a 65 20 66 6f 72 20 50 43 61 63 68 65 20  size for PCache 
10664 6f 62 6a 65 63 74 2e 20 54 68 65 20 63 61 6c 6c  object. The call
10665 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74  er must ensure t
10666 68 61 74 20 74 68 65 72 65 0a 2a 2a 20 61 72 65  hat there.** are
10667 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   no outstanding 
10668 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20  page references 
10669 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
1066a 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  on is called..*/
1066b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1066c 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
1066d 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43  heSetPageSize(PC
1066e 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e  ache *pCache, in
1066f 74 20 73 7a 50 61 67 65 29 7b 0a 20 20 61 73 73  t szPage){.  ass
10670 65 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 52 65  ert( pCache->nRe
10671 66 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e  f==0 && pCache->
10672 70 44 69 72 74 79 3d 3d 30 20 29 3b 0a 20 20 69  pDirty==0 );.  i
10673 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68  f( pCache->pCach
10674 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
10675 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
10676 63 68 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61  che.xDestroy(pCa
10677 63 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20  che->pCache);.  
10678 20 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65    pCache->pCache
10679 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 43 61 63   = 0;.  }.  pCac
1067a 68 65 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50  he->szPage = szP
1067b 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72  age;.}../*.** Tr
1067c 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 61  y to obtain a pa
1067d 67 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68  ge from the cach
1067e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1067f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
10680 50 63 61 63 68 65 46 65 74 63 68 28 0a 20 20 50  PcacheFetch(.  P
10681 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 20  Cache *pCache,  
10682 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74       /* Obtain t
10683 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 69  he page from thi
10684 73 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e  s cache */.  Pgn
10685 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20  o pgno,         
10686 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
10687 72 20 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0a 20  r to obtain */. 
10688 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c   int createFlag,
10689 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75         /* If tru
1068a 65 2c 20 63 72 65 61 74 65 20 70 61 67 65 20 69  e, create page i
1068b 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78  f it does not ex
1068c 69 73 74 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20  ist already */. 
1068d 20 50 67 48 64 72 20 2a 2a 70 70 50 61 67 65 20   PgHdr **ppPage 
1068e 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
1068f 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f  the page here */
10690 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 61  .){.  PgHdr *pPa
10691 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 65 43  ge = 0;.  int eC
10692 72 65 61 74 65 3b 0a 0a 20 20 61 73 73 65 72 74  reate;..  assert
10693 28 20 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20  ( pCache!=0 );. 
10694 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 20   assert( pgno>0 
10695 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
10696 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20  pluggable cache 
10697 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
10698 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61  ) has not been a
10699 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 61  llocated,.  ** a
1069a 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 0a  llocate it now..
1069b 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 63    */.  if( !pCac
1069c 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63 72  he->pCache && cr
1069d 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 20  eateFlag ){.    
1069e 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
1069f 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  p;.    int nByte
106a0 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 43  ;.    nByte = pC
106a1 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70  ache->szPage + p
106a2 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b  Cache->szExtra +
106a3 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b 0a   sizeof(PgHdr);.
106a4 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47      p = sqlite3G
106a5 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
106a6 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74 65  he.xCreate(nByte
106a7 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  , pCache->bPurge
106a8 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 21  able);.    if( !
106a9 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  p ){.      retur
106aa 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
106ab 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
106ac 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
106ad 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28  ache.xCachesize(
106ae 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29  p, pCache->nMax)
106af 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43  ;.    pCache->pC
106b0 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20  ache = p;.  }.. 
106b1 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61 74   eCreate = creat
106b2 65 46 6c 61 67 20 3f 20 31 20 3a 20 30 3b 0a 20  eFlag ? 1 : 0;. 
106b3 20 69 66 28 20 65 43 72 65 61 74 65 20 26 26 20   if( eCreate && 
106b4 28 21 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  (!pCache->bPurge
106b5 61 62 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d  able || !pCache-
106b6 3e 70 44 69 72 74 79 29 20 29 7b 0a 20 20 20 20  >pDirty) ){.    
106b7 65 43 72 65 61 74 65 20 3d 20 32 3b 0a 20 20 7d  eCreate = 2;.  }
106b8 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70  .  if( pCache->p
106b9 43 61 63 68 65 20 29 7b 0a 20 20 20 20 70 50 61  Cache ){.    pPa
106ba 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  ge = sqlite3Glob
106bb 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
106bc 78 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e 70  xFetch(pCache->p
106bd 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 65 43 72  Cache, pgno, eCr
106be 65 61 74 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  eate);.  }..  if
106bf 28 20 21 70 50 61 67 65 20 26 26 20 65 43 72 65  ( !pPage && eCre
106c0 61 74 65 3d 3d 31 20 29 7b 0a 20 20 20 20 50 67  ate==1 ){.    Pg
106c1 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 20 20 2f  Hdr *pPg;..    /
106c2 2a 20 46 69 6e 64 20 61 20 64 69 72 74 79 20 70  * Find a dirty p
106c3 61 67 65 20 74 6f 20 77 72 69 74 65 2d 6f 75 74  age to write-out
106c4 20 61 6e 64 20 72 65 63 79 63 6c 65 2e 20 46 69   and recycle. Fi
106c5 72 73 74 20 74 72 79 20 74 6f 20 66 69 6e 64 20  rst try to find 
106c6 61 20 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 74  a .    ** page t
106c7 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 71  hat does not req
106c8 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 2d 73  uire a journal-s
106c9 79 6e 63 20 28 6f 6e 65 20 77 69 74 68 20 50 47  ync (one with PG
106ca 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 0a 20 20  HDR_NEED_SYNC.  
106cb 20 20 2a 2a 20 63 6c 65 61 72 65 64 29 2c 20 62    ** cleared), b
106cc 75 74 20 69 66 20 74 68 61 74 20 69 73 20 6e 6f  ut if that is no
106cd 74 20 70 6f 73 73 69 62 6c 65 20 73 65 74 74 6c  t possible settl
106ce 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  e for any other 
106cf 0a 20 20 20 20 2a 2a 20 75 6e 72 65 66 65 72 65  .    ** unrefere
106d0 6e 63 65 64 20 64 69 72 74 79 20 70 61 67 65 2e  nced dirty page.
106d1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 78 70 65  .    */.    expe
106d2 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63  nsive_assert( pc
106d3 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28  acheCheckSynced(
106d4 70 43 61 63 68 65 29 20 29 3b 0a 20 20 20 20 66  pCache) );.    f
106d5 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d 3e 70  or(pPg=pCache->p
106d6 53 79 6e 63 65 64 3b 20 0a 20 20 20 20 20 20 20  Synced; .       
106d7 20 70 50 67 20 26 26 20 28 70 50 67 2d 3e 6e 52   pPg && (pPg->nR
106d8 65 66 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67  ef || (pPg->flag
106d9 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
106da 43 29 29 3b 20 0a 20 20 20 20 20 20 20 20 70 50  C)); .        pP
106db 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 72 65  g=pPg->pDirtyPre
106dc 76 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28  v.    );.    if(
106dd 20 21 70 50 67 20 29 7b 0a 20 20 20 20 20 20 66   !pPg ){.      f
106de 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d 3e 70  or(pPg=pCache->p
106df 44 69 72 74 79 54 61 69 6c 3b 20 70 50 67 20 26  DirtyTail; pPg &
106e0 26 20 70 50 67 2d 3e 6e 52 65 66 3b 20 70 50 67  & pPg->nRef; pPg
106e1 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 72 65 76  =pPg->pDirtyPrev
106e2 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
106e3 20 70 50 67 20 29 7b 0a 20 20 20 20 20 20 69 6e   pPg ){.      in
106e4 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d  t rc;.      rc =
106e5 20 70 43 61 63 68 65 2d 3e 78 53 74 72 65 73 73   pCache->xStress
106e6 28 70 43 61 63 68 65 2d 3e 70 53 74 72 65 73 73  (pCache->pStress
106e7 2c 20 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66  , pPg);.      if
106e8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
106e9 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55  && rc!=SQLITE_BU
106ea 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  SY ){.        re
106eb 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
106ec 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67  .    }..    pPag
106ed 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  e = sqlite3Globa
106ee 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78  lConfig.pcache.x
106ef 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e 70 43  Fetch(pCache->pC
106f0 61 63 68 65 2c 20 70 67 6e 6f 2c 20 32 29 3b 0a  ache, pgno, 2);.
106f1 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65    }..  if( pPage
106f2 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 70   ){.    if( 0==p
106f3 50 61 67 65 2d 3e 6e 52 65 66 20 29 7b 0a 20 20  Page->nRef ){.  
106f4 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66      pCache->nRef
106f5 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  ++;.    }.    pP
106f6 61 67 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20  age->nRef++;.   
106f7 20 70 50 61 67 65 2d 3e 70 44 61 74 61 20 3d 20   pPage->pData = 
106f8 28 76 6f 69 64 2a 29 26 70 50 61 67 65 5b 31 5d  (void*)&pPage[1]
106f9 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78  ;.    pPage->pEx
106fa 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 28 28  tra = (void*)&((
106fb 63 68 61 72 2a 29 70 50 61 67 65 2d 3e 70 44 61  char*)pPage->pDa
106fc 74 61 29 5b 70 43 61 63 68 65 2d 3e 73 7a 50 61  ta)[pCache->szPa
106fd 67 65 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  ge];.    pPage->
106fe 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65 3b  pCache = pCache;
106ff 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f  .    pPage->pgno
10700 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 69 66 28   = pgno;.    if(
10701 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20   pgno==1 ){.    
10702 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31    pCache->pPage1
10703 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a   = pPage;.    }.
10704 20 20 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20    }.  *ppPage = 
10705 70 50 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20  pPage;.  return 
10706 28 70 50 61 67 65 3d 3d 30 20 26 26 20 65 43 72  (pPage==0 && eCr
10707 65 61 74 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e  eate) ? SQLITE_N
10708 4f 4d 45 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b  OMEM : SQLITE_OK
10709 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65  ;.}../*.** Decre
1070a 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e  ment the referen
1070b 63 65 20 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61  ce count on a pa
1070c 67 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 20  ge. If the page 
1070d 69 73 20 63 6c 65 61 6e 20 61 6e 64 20 74 68 65  is clean and the
1070e 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 63 6f  .** reference co
1070f 75 6e 74 20 64 72 6f 70 73 20 74 6f 20 30 2c 20  unt drops to 0, 
10710 74 68 65 6e 20 69 74 20 69 73 20 6d 61 64 65 20  then it is made 
10711 65 6c 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63  elible for recyc
10712 6c 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ling..*/.SQLITE_
10713 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
10714 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
10715 65 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61  e(PgHdr *p){.  a
10716 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30  ssert( p->nRef>0
10717 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b   );.  p->nRef--;
10718 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d  .  if( p->nRef==
10719 30 20 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20  0 ){.    PCache 
1071a 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61  *pCache = p->pCa
1071b 63 68 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d  che;.    pCache-
1071c 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28  >nRef--;.    if(
1071d 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52   (p->flags&PGHDR
1071e 5f 44 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20  _DIRTY)==0 ){.  
1071f 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28      pcacheUnpin(
10720 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  p);.    }else{. 
10721 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65       /* Move the
10722 20 70 61 67 65 20 74 6f 20 74 68 65 20 68 65 61   page to the hea
10723 64 20 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c  d of the dirty l
10724 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63  ist. */.      pc
10725 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69  acheRemoveFromDi
10726 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20  rtyList(p);.    
10727 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72    pcacheAddToDir
10728 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 7d  tyList(p);.    }
10729 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  .  }.}../*.** In
1072a 63 72 65 61 73 65 20 74 68 65 20 72 65 66 65 72  crease the refer
1072b 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20  ence count of a 
1072c 73 75 70 70 6c 69 65 64 20 70 61 67 65 20 62 79  supplied page by
1072d 20 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   1..*/.SQLITE_PR
1072e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1072f 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48 64  e3PcacheRef(PgHd
10730 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  r *p){.  assert(
10731 70 2d 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d  p->nRef>0);.  p-
10732 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a  >nRef++;.}../*.*
10733 2a 20 44 72 6f 70 20 61 20 70 61 67 65 20 66 72  * Drop a page fr
10734 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68  om the cache. Th
10735 65 72 65 20 6d 75 73 74 20 62 65 20 65 78 61 63  ere must be exac
10736 74 6c 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63  tly one referenc
10737 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65  e to the.** page
10738 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
10739 64 65 6c 65 74 65 73 20 74 68 61 74 20 72 65 66  deletes that ref
1073a 65 72 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72  erence, so after
1073b 20 69 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a   it returns the.
1073c 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20  ** page pointed 
1073d 74 6f 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c  to by p is inval
1073e 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  id..*/.SQLITE_PR
1073f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
10740 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48  e3PcacheDrop(PgH
10741 64 72 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65  dr *p){.  PCache
10742 20 2a 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65   *pCache;.  asse
10743 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29  rt( p->nRef==1 )
10744 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  ;.  if( p->flags
10745 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 7b 0a  &PGHDR_DIRTY ){.
10746 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65      pcacheRemove
10747 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29  FromDirtyList(p)
10748 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 20 3d  ;.  }.  pCache =
10749 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70 43   p->pCache;.  pC
1074a 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  ache->nRef--;.  
1074b 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29  if( p->pgno==1 )
1074c 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 50  {.    pCache->pP
1074d 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  age1 = 0;.  }.  
1074e 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
1074f 66 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69  fig.pcache.xUnpi
10750 6e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  n(pCache->pCache
10751 2c 20 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , p, 1);.}../*.*
10752 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
10753 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61  page is marked a
10754 73 20 64 69 72 74 79 2e 20 49 66 20 69 74 20 69  s dirty. If it i
10755 73 6e 27 74 20 64 69 72 74 79 20 61 6c 72 65 61  sn't dirty alrea
10756 64 79 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73  dy,.** make it s
10757 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  o..*/.SQLITE_PRI
10758 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
10759 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
1075a 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43  (PgHdr *p){.  PC
1075b 61 63 68 65 20 2a 70 43 61 63 68 65 3b 0a 20 20  ache *pCache;.  
1075c 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48  p->flags &= ~PGH
1075d 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 20  DR_DONT_WRITE;. 
1075e 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
1075f 3e 30 20 29 3b 0a 20 20 69 66 28 20 30 3d 3d 28  >0 );.  if( 0==(
10760 70 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52  p->flags & PGHDR
10761 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70  _DIRTY) ){.    p
10762 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68  Cache = p->pCach
10763 65 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  e;.    p->flags 
10764 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a  |= PGHDR_DIRTY;.
10765 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 44      pcacheAddToD
10766 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20  irtyList( p);.  
10767 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  }.}../*.** Make 
10768 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73  sure the page is
10769 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e   marked as clean
1076a 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c  . If it isn't cl
1076b 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20  ean already,.** 
1076c 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53  make it so..*/.S
1076d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1076e 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
1076f 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20  MakeClean(PgHdr 
10770 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66  *p){.  if( (p->f
10771 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52  lags & PGHDR_DIR
10772 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68  TY) ){.    pcach
10773 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79  eRemoveFromDirty
10774 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e  List(p);.    p->
10775 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52  flags &= ~(PGHDR
10776 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45  _DIRTY|PGHDR_NEE
10777 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28  D_SYNC);.    if(
10778 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20   p->nRef==0 ){. 
10779 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e       pcacheUnpin
1077a 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
1077b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65  ../*.** Make eve
1077c 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  ry page in the c
1077d 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53  ache clean..*/.S
1077e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1077f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
10780 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20  CleanAll(PCache 
10781 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64  *pCache){.  PgHd
10782 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28  r *p;.  while( (
10783 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72  p = pCache->pDir
10784 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ty)!=0 ){.    sq
10785 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
10786 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  lean(p);.  }.}..
10787 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20  /*.** Clear the 
10788 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
10789 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69  flag from all di
1078a 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51  rty pages..*/.SQ
1078b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1078c 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43  d sqlite3PcacheC
1078d 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43  learSyncFlags(PC
1078e 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
1078f 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72   PgHdr *p;.  for
10790 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74  (p=pCache->pDirt
10791 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74  y; p; p=p->pDirt
10792 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66  yNext){.    p->f
10793 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e  lags &= ~PGHDR_N
10794 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20  EED_SYNC;.  }.  
10795 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20  pCache->pSynced 
10796 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  = pCache->pDirty
10797 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Tail;.}../*.** C
10798 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e  hange the page n
10799 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 20  umber of page p 
1079a 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a  to newPgno. .*/.
1079b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1079c 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
1079d 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20  eMove(PgHdr *p, 
1079e 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20  Pgno newPgno){. 
1079f 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 20   PCache *pCache 
107a0 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61  = p->pCache;.  a
107a1 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30  ssert( p->nRef>0
107a2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65   );.  assert( ne
107a3 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c  wPgno>0 );.  sql
107a4 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
107a5 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70  .pcache.xRekey(p
107a6 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70  Cache->pCache, p
107a7 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67  , p->pgno, newPg
107a8 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d  no);.  p->pgno =
107a9 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20   newPgno;.  if( 
107aa 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  (p->flags&PGHDR_
107ab 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c  DIRTY) && (p->fl
107ac 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
107ad 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63  YNC) ){.    pcac
107ae 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
107af 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63  yList(p);.    pc
107b0 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69  acheAddToDirtyLi
107b1 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  st(p);.  }.}../*
107b2 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63  .** Drop every c
107b3 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65  ache entry whose
107b4 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
107b5 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67  greater than "pg
107b6 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c  no". The.** call
107b7 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74  er must ensure t
107b8 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hat there are no
107b9 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66   outstanding ref
107ba 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70  erences to any p
107bb 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68  ages.** other th
107bc 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 61  an page 1 with a
107bd 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65   page number gre
107be 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a  ater than pgno..
107bf 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  **.** If there i
107c0 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
107c1 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 20   page 1 and the 
107c2 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70  pgno parameter p
107c3 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a  assed to this.**
107c4 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20   function is 0, 
107c5 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 72  then the data ar
107c6 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ea associated wi
107c7 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 72  th page 1 is zer
107c8 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20  oed, but.** the 
107c9 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e  page object is n
107ca 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53  ot dropped..*/.S
107cb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
107cc 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
107cd 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 20  Truncate(PCache 
107ce 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67  *pCache, Pgno pg
107cf 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68  no){.  if( pCach
107d0 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20  e->pCache ){.   
107d1 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50   PgHdr *p;.    P
107d2 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20  gHdr *pNext;.   
107d3 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70   for(p=pCache->p
107d4 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78  Dirty; p; p=pNex
107d5 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20  t){.      pNext 
107d6 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b  = p->pDirtyNext;
107d7 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67  .      if( p->pg
107d8 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20  no>pgno ){.     
107d9 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c     assert( p->fl
107da 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20  ags&PGHDR_DIRTY 
107db 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
107dc 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61  e3PcacheMakeClea
107dd 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n(p);.      }.  
107de 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f    }.    if( pgno
107df 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70  ==0 && pCache->p
107e0 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d  Page1 ){.      m
107e1 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50  emset(pCache->pP
107e2 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20  age1->pData, 0, 
107e3 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b  pCache->szPage);
107e4 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b  .      pgno = 1;
107e5 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
107e6 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
107e7 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28  cache.xTruncate(
107e8 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
107e9 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  pgno+1);.  }.}..
107ea 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61  /*.** Close a ca
107eb 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  che..*/.SQLITE_P
107ec 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
107ed 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50  te3PcacheClose(P
107ee 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
107ef 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43    if( pCache->pC
107f0 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  ache ){.    sqli
107f1 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
107f2 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28  pcache.xDestroy(
107f3 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b  pCache->pCache);
107f4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44  .  }.}../* .** D
107f5 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65  iscard the conte
107f6 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65  nts of the cache
107f7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
107f8 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
107f9 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68  cacheClear(PCach
107fa 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 73 71  e *pCache){.  sq
107fb 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63  lite3PcacheTrunc
107fc 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a  ate(pCache, 0);.
107fd 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
107fe 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72  OK;.}../*.** Mer
107ff 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20  ge two lists of 
10800 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20  pages connected 
10801 62 79 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e  by pDirty and in
10802 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20   pgno order..** 
10803 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69  Do not both fixi
10804 6e 67 20 74 68 65 20 70 44 69 72 74 79 50 72 65  ng the pDirtyPre
10805 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73  v pointers..*/.s
10806 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61  tatic PgHdr *pca
10807 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73  cheMergeDirtyLis
10808 74 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48  t(PgHdr *pA, PgH
10809 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72  dr *pB){.  PgHdr
1080a 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b   result, *pTail;
1080b 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75  .  pTail = &resu
1080c 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20  lt;.  while( pA 
1080d 26 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28  && pB ){.    if(
1080e 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67   pA->pgno<pB->pg
1080f 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69  no ){.      pTai
10810 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a  l->pDirty = pA;.
10811 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41        pTail = pA
10812 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d  ;.      pA = pA-
10813 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c  >pDirty;.    }el
10814 73 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d  se{.      pTail-
10815 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20  >pDirty = pB;.  
10816 20 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a      pTail = pB;.
10817 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70        pB = pB->p
10818 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d  Dirty;.    }.  }
10819 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20  .  if( pA ){.   
1081a 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d   pTail->pDirty =
1081b 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   pA;.  }else if(
1081c 20 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c   pB ){.    pTail
1081d 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20  ->pDirty = pB;. 
1081e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69   }else{.    pTai
1081f 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20  l->pDirty = 0;. 
10820 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75   }.  return resu
10821 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a  lt.pDirty;.}../*
10822 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73  .** Sort the lis
10823 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63  t of pages in ac
10824 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79  cending order by
10825 20 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72   pgno.  Pages ar
10826 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62  e.** connected b
10827 79 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72  y pDirty pointer
10828 73 2e 20 20 54 68 65 20 70 44 69 72 74 79 50 72  s.  The pDirtyPr
10829 65 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a  ev pointers are.
1082a 2a 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79 20  ** corrupted by 
1082b 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64  this sort..*/.#d
1082c 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43  efine N_SORT_BUC
1082d 4b 45 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 65  KET_ALLOC 25.#de
1082e 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b  fine N_SORT_BUCK
1082f 45 54 20 20 20 20 20 20 20 32 35 0a 23 69 66 64  ET       25.#ifd
10830 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
10831 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67   int sqlite3_pag
10832 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74  er_n_sort_bucket
10833 20 3d 20 30 3b 0a 20 20 23 75 6e 64 65 66 20 4e   = 0;.  #undef N
10834 5f 53 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20 23  _SORT_BUCKET.  #
10835 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55  define N_SORT_BU
10836 43 4b 45 54 20 5c 0a 20 20 20 28 73 71 6c 69 74  CKET \.   (sqlit
10837 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f  e3_pager_n_sort_
10838 62 75 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f 70  bucket?sqlite3_p
10839 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b  ager_n_sort_buck
1083a 65 74 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  et:N_SORT_BUCKET
1083b 5f 41 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a 73  _ALLOC).#endif.s
1083c 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61  tatic PgHdr *pca
1083d 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74  cheSortDirtyList
1083e 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20  (PgHdr *pIn){.  
1083f 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f  PgHdr *a[N_SORT_
10840 42 55 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a  BUCKET_ALLOC], *
10841 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65  p;.  int i;.  me
10842 6d 73 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f  mset(a, 0, sizeo
10843 66 28 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20  f(a));.  while( 
10844 70 49 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70  pIn ){.    p = p
10845 49 6e 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d  In;.    pIn = p-
10846 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e  >pDirty;.    p->
10847 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20  pDirty = 0;.    
10848 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52  for(i=0; i<N_SOR
10849 54 5f 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b 29  T_BUCKET-1; i++)
1084a 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69 5d  {.      if( a[i]
1084b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==0 ){.        a
1084c 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 20  [i] = p;.       
1084d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65   break;.      }e
1084e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20 3d  lse{.        p =
1084f 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74   pcacheMergeDirt
10850 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a  yList(a[i], p);.
10851 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 30          a[i] = 0
10852 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
10853 20 20 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f 52      if( i==N_SOR
10854 54 5f 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 20  T_BUCKET-1 ){.  
10855 20 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a      /* Coverage:
10856 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68   To get here, th
10857 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32  ere need to be 2
10858 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29  ^(N_SORT_BUCKET)
10859 20 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65   .      ** eleme
1085a 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74  nts in the input
1085b 20 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 70   list. This is p
1085c 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 69 6d 70  ossible, but imp
1085d 72 61 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 20  ractical..      
1085e 2a 2a 20 54 65 73 74 69 6e 67 20 74 68 69 73 20  ** Testing this 
1085f 6c 69 6e 65 20 69 73 20 74 68 65 20 70 6f 69 6e  line is the poin
10860 74 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69  t of global vari
10861 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 71  able.      ** sq
10862 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f  lite3_pager_n_so
10863 72 74 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 20  rt_bucket..     
10864 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d   */.      a[i] =
10865 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74   pcacheMergeDirt
10866 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a  yList(a[i], p);.
10867 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20      }.  }.  p = 
10868 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b  a[0];.  for(i=1;
10869 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54   i<N_SORT_BUCKET
1086a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20  ; i++){.    p = 
1086b 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79  pcacheMergeDirty
1086c 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20  List(p, a[i]);. 
1086d 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
1086e 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
1086f 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72   list of all dir
10870 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ty pages in the 
10871 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79  cache, sorted by
10872 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f   page number..*/
10873 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10874 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63  PgHdr *sqlite3Pc
10875 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43  acheDirtyList(PC
10876 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
10877 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72   PgHdr *p;.  for
10878 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74  (p=pCache->pDirt
10879 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74  y; p; p=p->pDirt
1087a 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70  yNext){.    p->p
1087b 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69 72 74  Dirty = p->pDirt
1087c 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74  yNext;.  }.  ret
1087d 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74 44 69  urn pcacheSortDi
1087e 72 74 79 4c 69 73 74 28 70 43 61 63 68 65 2d 3e  rtyList(pCache->
1087f 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a  pDirty);.}../* .
10880 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f  ** Return the to
10881 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  tal number of re
10882 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 68  ferenced pages h
10883 65 6c 64 20 62 79 20 74 68 65 20 63 61 63 68 65  eld by the cache
10884 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10885 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
10886 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43  cacheRefCount(PC
10887 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
10888 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e   return pCache->
10889 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  nRef;.}../*.** R
1088a 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
1088b 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
1088c 6f 20 74 68 65 20 70 61 67 65 20 73 75 70 70 6c  o the page suppl
1088d 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65  ied as an argume
1088e 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
1088f 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
10890 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f  3PcachePageRefco
10891 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b 0a 20  unt(PgHdr *p){. 
10892 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b   return p->nRef;
10893 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72  .}../* .** Retur
10894 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  n the total numb
10895 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
10896 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c  he cache..*/.SQL
10897 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
10898 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67  sqlite3PcachePag
10899 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70  ecount(PCache *p
1089a 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50  Cache){.  int nP
1089b 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  age = 0;.  if( p
1089c 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b  Cache->pCache ){
1089d 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73 71 6c  .    nPage = sql
1089e 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
1089f 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63 6f 75  .pcache.xPagecou
108a0 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  nt(pCache->pCach
108a1 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
108a2 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65   nPage;.}..#ifde
108a3 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
108a4 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75 67 67  .** Get the sugg
108a5 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65  ested cache-size
108a6 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54   value..*/.SQLIT
108a7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
108a8 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61  lite3PcacheGetCa
108a9 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a  chesize(PCache *
108aa 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 75 72  pCache){.  retur
108ab 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a  n pCache->nMax;.
108ac 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
108ad 53 65 74 20 74 68 65 20 73 75 67 67 65 73 74 65  Set the suggeste
108ae 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c  d cache-size val
108af 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ue..*/.SQLITE_PR
108b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
108b1 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65  e3PcacheSetCache
108b2 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61  size(PCache *pCa
108b3 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67 65 29  che, int mxPage)
108b4 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78  {.  pCache->nMax
108b5 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69 66 28   = mxPage;.  if(
108b6 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
108b7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
108b8 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
108b9 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 43 61  e.xCachesize(pCa
108ba 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50  che->pCache, mxP
108bb 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66  age);.  }.}..#if
108bc 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  def SQLITE_CHECK
108bd 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 46 6f 72  _PAGES./*.** For
108be 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
108bf 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68   currently in th
108c0 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65 20  e cache, invoke 
108c1 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
108c2 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 20   callback. This 
108c3 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20  is only used if 
108c4 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  the SQLITE_CHECK
108c5 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73 0a  _PAGES macro is.
108c6 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53  ** defined..*/.S
108c7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
108c8 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
108c9 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 61  IterateDirty(PCa
108ca 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69  che *pCache, voi
108cb 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 72  d (*xIter)(PgHdr
108cc 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a 70   *)){.  PgHdr *p
108cd 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44 69  Dirty;.  for(pDi
108ce 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 72  rty=pCache->pDir
108cf 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69 72  ty; pDirty; pDir
108d0 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 74  ty=pDirty->pDirt
108d1 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74 65  yNext){.    xIte
108d2 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a 7d  r(pDirty);.  }.}
108d3 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
108d4 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
108d5 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  pcache.c *******
108d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
108d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
108d8 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
108d9 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
108da 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a 2a  ile pcache1.c **
108db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
108dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
108dd 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
108de 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35 0a  008 November 05.
108df 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
108e0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
108e1 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
108e2 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
108e3 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
108e4 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
108e5 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
108e6 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
108e7 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
108e8 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
108e9 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
108ea 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
108eb 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
108ec 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
108ed 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
108ee 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
108ef 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
108f0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
108f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
108f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
108f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
108f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
108f5 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69  *.** This file i
108f6 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 65  mplements the de
108f7 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
108f8 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
108f9 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  (the.** sqlite3_
108fa 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65  pcache interface
108fb 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61  ). It also conta
108fc 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65 20  ins part of the 
108fd 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
108fe 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f  * of the SQLITE_
108ff 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10900 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 6c   and sqlite3_rel
10901 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66 65  ease_memory() fe
10902 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74 68  atures..** If th
10903 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63  e default page c
10904 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ache implementat
10905 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65 6e  ion is overriden
10906 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 6f  , then neither o
10907 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20 66  f.** these two f
10908 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61 69  eatures are avai
10909 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  lable..**.** @(#
1090a 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e 63  ) $Id: pcache1.c
1090b 2c 76 20 31 2e 37 20 32 30 30 39 2f 30 31 2f 30  ,v 1.7 2009/01/0
1090c 37 20 31 35 3a 31 38 3a 32 31 20 64 61 6e 69 65  7 15:18:21 danie
1090d 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
1090e 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
1090f 20 50 43 61 63 68 65 31 20 50 43 61 63 68 65 31   PCache1 PCache1
10910 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
10911 20 50 67 48 64 72 31 20 50 67 48 64 72 31 3b 0a   PgHdr1 PgHdr1;.
10912 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50  typedef struct P
10913 67 46 72 65 65 73 6c 6f 74 20 50 67 46 72 65 65  gFreeslot PgFree
10914 73 6c 6f 74 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65  slot;../* Pointe
10915 72 73 20 74 6f 20 73 74 72 75 63 74 75 72 65 73  rs to structures
10916 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 61 72   of this type ar
10917 65 20 63 61 73 74 20 61 6e 64 20 72 65 74 75 72  e cast and retur
10918 6e 65 64 20 61 73 20 0a 2a 2a 20 6f 70 61 71 75  ned as .** opaqu
10919 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
1091a 2a 20 68 61 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72  * handles.*/.str
1091b 75 63 74 20 50 43 61 63 68 65 31 20 7b 0a 20 20  uct PCache1 {.  
1091c 2f 2a 20 43 61 63 68 65 20 63 6f 6e 66 69 67 75  /* Cache configu
1091d 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72  ration parameter
1091e 73 2e 20 50 61 67 65 20 73 69 7a 65 20 28 73 7a  s. Page size (sz
1091f 50 61 67 65 29 20 61 6e 64 20 74 68 65 20 70 75  Page) and the pu
10920 72 67 65 61 62 6c 65 0a 20 20 2a 2a 20 66 6c 61  rgeable.  ** fla
10921 67 20 28 62 50 75 72 67 65 61 62 6c 65 29 20 61  g (bPurgeable) a
10922 72 65 20 73 65 74 20 77 68 65 6e 20 74 68 65 20  re set when the 
10923 63 61 63 68 65 20 69 73 20 63 72 65 61 74 65 64  cache is created
10924 2e 20 6e 4d 61 78 20 6d 61 79 20 62 65 20 0a 20  . nMax may be . 
10925 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 74 20   ** modified at 
10926 61 6e 79 20 74 69 6d 65 20 62 79 20 61 20 63 61  any time by a ca
10927 6c 6c 20 74 6f 20 74 68 65 20 70 63 61 63 68 65  ll to the pcache
10928 31 43 61 63 68 65 53 69 7a 65 28 29 20 6d 65 74  1CacheSize() met
10929 68 6f 64 2e 0a 20 20 2a 2a 20 54 68 65 20 67 6c  hod..  ** The gl
1092a 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20  obal mutex must 
1092b 62 65 20 68 65 6c 64 20 77 68 65 6e 20 61 63 63  be held when acc
1092c 65 73 73 69 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a  essing nMax..  *
1092d 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20  /.  int szPage; 
1092e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1092f 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
10930 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67  of allocated pag
10931 65 73 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  es in bytes */. 
10932 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b   int bPurgeable;
10933 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10934 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
10935 63 61 63 68 65 20 69 73 20 70 75 72 67 65 61 62  cache is purgeab
10936 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  le */.  unsigned
10937 20 69 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20   int nMin;      
10938 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
10939 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  inimum number of
1093a 20 70 61 67 65 73 20 72 65 73 65 72 76 65 64 20   pages reserved 
1093b 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
1093c 74 20 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20  t nMax;         
1093d 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66           /* Conf
1093e 69 67 75 72 65 64 20 22 63 61 63 68 65 5f 73 69  igured "cache_si
1093f 7a 65 22 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20  ze" value */..  
10940 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66  /* Hash table of
10941 20 61 6c 6c 20 70 61 67 65 73 2e 20 54 68 65 20   all pages. The 
10942 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62  following variab
10943 6c 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  les may only be 
10944 61 63 63 65 73 73 65 64 0a 20 20 2a 2a 20 77 68  accessed.  ** wh
10945 65 6e 20 74 68 65 20 61 63 63 65 73 73 6f 72 20  en the accessor 
10946 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 67  is holding the g
10947 6c 6f 62 61 6c 20 6d 75 74 65 78 20 28 73 65 65  lobal mutex (see
10948 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
10949 65 78 28 29 20 0a 20 20 2a 2a 20 61 6e 64 20 70  ex() .  ** and p
1094a 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
1094b 28 29 29 2e 0a 20 20 2a 2f 0a 20 20 75 6e 73 69  ())..  */.  unsi
1094c 67 6e 65 64 20 69 6e 74 20 6e 52 65 63 79 63 6c  gned int nRecycl
1094d 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  able;           
1094e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
1094f 65 73 20 69 6e 20 74 68 65 20 4c 52 55 20 6c 69  es in the LRU li
10950 73 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  st */.  unsigned
10951 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20   int nPage;     
10952 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
10953 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
10954 61 67 65 73 20 69 6e 20 61 70 48 61 73 68 20 2a  ages in apHash *
10955 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
10956 20 6e 48 61 73 68 3b 20 20 20 20 20 20 20 20 20   nHash;         
10957 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
10958 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70  r of slots in ap
10959 48 61 73 68 5b 5d 20 2a 2f 0a 20 20 50 67 48 64  Hash[] */.  PgHd
1095a 72 31 20 2a 2a 61 70 48 61 73 68 3b 20 20 20 20  r1 **apHash;    
1095b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1095c 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f  /* Hash table fo
1095d 72 20 66 61 73 74 20 6c 6f 6f 6b 75 70 20 62 79  r fast lookup by
1095e 20 6b 65 79 20 2a 2f 0a 0a 20 20 75 6e 73 69 67   key */..  unsig
1095f 6e 65 64 20 69 6e 74 20 69 4d 61 78 4b 65 79 3b  ned int iMaxKey;
10960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10961 2a 20 4c 61 72 67 65 73 74 20 6b 65 79 20 73 65  * Largest key se
10962 65 6e 20 73 69 6e 63 65 20 78 54 72 75 6e 63 61  en since xTrunca
10963 74 65 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  te() */.};../*.*
10964 2a 20 45 61 63 68 20 63 61 63 68 65 20 65 6e 74  * Each cache ent
10965 72 79 20 69 73 20 72 65 70 72 65 73 65 6e 74 65  ry is represente
10966 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  d by an instance
10967 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
10968 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  g .** structure.
10969 20 41 20 62 75 66 66 65 72 20 6f 66 20 50 67 48   A buffer of PgH
1096a 64 72 31 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61  dr1.pCache->szPa
1096b 67 65 20 62 79 74 65 73 20 69 73 20 61 6c 6c 6f  ge bytes is allo
1096c 63 61 74 65 64 20 0a 2a 2a 20 64 69 72 65 63 74  cated .** direct
1096d 6c 79 20 61 66 74 65 72 20 74 68 65 20 73 74 72  ly after the str
1096e 75 63 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79  ucture in memory
1096f 20 28 73 65 65 20 74 68 65 20 50 47 48 44 52 31   (see the PGHDR1
10970 5f 54 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d  _TO_PAGE() .** m
10971 61 63 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a  acro below)..*/.
10972 73 74 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a  struct PgHdr1 {.
10973 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
10974 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
10975 20 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70   /* Key value (p
10976 61 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20  age number) */. 
10977 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20   PgHdr1 *pNext; 
10978 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10979 2f 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20  /* Next in hash 
1097a 74 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20  table chain */. 
1097b 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
1097c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1097d 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 63 75  /* Cache that cu
1097e 72 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69  rrently owns thi
1097f 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64  s page */.  PgHd
10980 72 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20  r1 *pLruNext;   
10981 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
10982 78 74 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f  xt in LRU list o
10983 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73  f unpinned pages
10984 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c   */.  PgHdr1 *pL
10985 72 75 50 72 65 76 3b 20 20 20 20 20 20 20 20 20  ruPrev;         
10986 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73       /* Previous
10987 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20   in LRU list of 
10988 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a  unpinned pages *
10989 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  /.};../*.** Free
1098a 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c   slots in the al
1098b 6c 6f 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20  locator used to 
1098c 64 69 76 69 64 65 20 75 70 20 74 68 65 20 62 75  divide up the bu
1098d 66 66 65 72 20 70 72 6f 76 69 64 65 64 20 75 73  ffer provided us
1098e 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54  ing.** the SQLIT
1098f 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
10990 48 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f  HE mechanism..*/
10991 0a 73 74 72 75 63 74 20 50 67 46 72 65 65 73 6c  .struct PgFreesl
10992 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f  ot {.  PgFreeslo
10993 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65  t *pNext;  /* Ne
10994 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a  xt free slot */.
10995 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c  };../*.** Global
10996 20 64 61 74 61 20 75 73 65 64 20 62 79 20 74 68   data used by th
10997 69 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61  is cache..*/.sta
10998 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
10999 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62  truct PCacheGlob
1099a 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  al {.  sqlite3_m
1099b 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20  utex *mutex;    
1099c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74             /* st
1099d 61 74 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58  atic mutex MUTEX
1099e 5f 53 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a  _STATIC_LRU */..
1099f 20 20 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20    int nMaxPage; 
109a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
109a1 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20        /* Sum of 
109a2 6e 4d 61 78 50 61 67 65 20 66 6f 72 20 70 75 72  nMaxPage for pur
109a3 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f  geable caches */
109a4 0a 20 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b  .  int nMinPage;
109a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
109a6 20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66         /* Sum of
109a7 20 6e 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75   nMinPage for pu
109a8 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a  rgeable caches *
109a9 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74  /.  int nCurrent
109aa 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
109ab 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
109ac 72 20 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70  r of purgeable p
109ad 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a  ages allocated *
109ae 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75  /.  PgHdr1 *pLru
109af 48 65 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b  Head, *pLruTail;
109b0 20 20 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c          /* LRU l
109b1 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20  ist of unpinned 
109b2 70 61 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56  pages */..  /* V
109b3 61 72 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64  ariables related
109b4 20 74 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49   to SQLITE_CONFI
109b5 47 5f 50 41 47 45 43 41 43 48 45 20 73 65 74 74  G_PAGECACHE sett
109b6 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73  ings. */.  int s
109b7 7a 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20  zSlot;          
109b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
109b9 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66  * Size of each f
109ba 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f  ree slot */.  vo
109bb 69 64 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e  id *pStart, *pEn
109bc 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
109bd 20 20 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70    /* Bounds of p
109be 61 67 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20  agecache malloc 
109bf 72 61 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65  range */.  PgFre
109c0 65 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20  eslot *pFree;   
109c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
109c2 2a 20 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63  * Free page bloc
109c3 6b 73 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f  ks */.} pcache1_
109c4 67 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f  g;../*.** All co
109c5 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
109c6 73 68 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68  should access th
109c7 65 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75  e global structu
109c8 72 65 20 61 62 6f 76 65 20 76 69 61 20 74 68 65  re above via the
109c9 0a 2a 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68  .** alias "pcach
109ca 65 31 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65  e1". This ensure
109cb 73 20 74 68 61 74 20 74 68 65 20 57 53 44 20 65  s that the WSD e
109cc 6d 75 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64  mulation is used
109cd 20 77 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69   when.** compili
109ce 6e 67 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74  ng for systems t
109cf 68 61 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f  hat do not suppo
109d0 72 74 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a  rt real WSD..*/.
109d1 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 20  #define pcache1 
109d2 28 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50  (GLOBAL(struct P
109d3 43 61 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61  CacheGlobal, pca
109d4 63 68 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20  che1_g))../*.** 
109d5 57 68 65 6e 20 61 20 50 67 48 64 72 31 20 73 74  When a PgHdr1 st
109d6 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63  ructure is alloc
109d7 61 74 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69  ated, the associ
109d8 61 74 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50  ated PCache1.szP
109d9 61 67 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20  age.** bytes of 
109da 64 61 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64  data are located
109db 20 64 69 72 65 63 74 6c 79 20 61 66 74 65 72 20   directly after 
109dc 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e  it in memory (i.
109dd 65 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20  e. the total.** 
109de 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f  size of the allo
109df 63 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66  cation is sizeof
109e0 28 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31  (PgHdr1)+PCache1
109e1 2e 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54  .szPage byte). T
109e2 68 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f  he.** PGHDR1_TO_
109e3 50 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b  PAGE() macro tak
109e4 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  es a pointer to 
109e5 61 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75  a PgHdr1 structu
109e6 72 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75  re as.** an argu
109e7 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73  ment and returns
109e8 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
109e9 65 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f  e associated blo
109ea 63 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20  ck of szPage.** 
109eb 62 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f  bytes. The PAGE_
109ec 54 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72  TO_PGHDR1() macr
109ed 6f 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73  o does the oppos
109ee 69 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e  ite: its argumen
109ef 74 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  t is.** a pointe
109f0 72 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20  r to a block of 
109f1 73 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20  szPage bytes of 
109f2 64 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74  data and the ret
109f3 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20  urn value is.** 
109f4 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
109f5 20 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64   associated PgHd
109f6 72 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  r1 structure..**
109f7 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47  .**   assert( PG
109f8 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47  HDR1_TO_PAGE(PAG
109f9 45 5f 54 4f 5f 50 47 48 44 52 31 28 58 29 29 3d  E_TO_PGHDR1(X))=
109fa 3d 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e 65  =X );.*/.#define
109fb 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28   PGHDR1_TO_PAGE(
109fc 70 29 20 28 76 6f 69 64 20 2a 29 28 26 28 28 75  p) (void *)(&((u
109fd 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
109fe 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29  )[sizeof(PgHdr1)
109ff 5d 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 5f  ]).#define PAGE_
10a00 54 4f 5f 50 47 48 44 52 31 28 70 29 20 28 50 67  TO_PGHDR1(p) (Pg
10a01 48 64 72 31 20 2a 29 28 26 28 28 75 6e 73 69 67  Hdr1 *)(&((unsig
10a02 6e 65 64 20 63 68 61 72 20 2a 29 70 29 5b 2d 31  ned char *)p)[-1
10a03 2a 28 69 6e 74 29 73 69 7a 65 6f 66 28 50 67 48  *(int)sizeof(PgH
10a04 64 72 31 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61  dr1)])../*.** Ma
10a05 63 72 6f 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  cros to enter an
10a06 64 20 6c 65 61 76 65 20 74 68 65 20 67 6c 6f 62  d leave the glob
10a07 61 6c 20 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f  al LRU mutex..*/
10a08 0a 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31  .#define pcache1
10a09 45 6e 74 65 72 4d 75 74 65 78 28 29 20 73 71 6c  EnterMutex() sql
10a0a 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
10a0b 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 0a  (pcache1.mutex).
10a0c 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 4c  #define pcache1L
10a0d 65 61 76 65 4d 75 74 65 78 28 29 20 73 71 6c 69  eaveMutex() sqli
10a0e 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
10a0f 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a  pcache1.mutex)..
10a10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
10a11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
10a15 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20  ./******** Page 
10a16 41 6c 6c 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54  Allocation/SQLIT
10a17 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20  E_CONFIG_PCACHE 
10a18 52 65 6c 61 74 65 64 20 46 75 6e 63 74 69 6f 6e  Related Function
10a19 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s **************
10a1a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  /../*.** This fu
10a1b 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
10a1c 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69   during initiali
10a1d 7a 61 74 69 6f 6e 20 69 66 20 61 20 73 74 61 74  zation if a stat
10a1e 69 63 20 62 75 66 66 65 72 20 69 73 20 0a 2a 2a  ic buffer is .**
10a1f 20 73 75 70 70 6c 69 65 64 20 74 6f 20 75 73 65   supplied to use
10a20 20 66 6f 72 20 74 68 65 20 70 61 67 65 2d 63 61   for the page-ca
10a21 63 68 65 20 62 79 20 70 61 73 73 69 6e 67 20 74  che by passing t
10a22 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  he SQLITE_CONFIG
10a23 5f 50 41 47 45 43 41 43 48 45 0a 2a 2a 20 76 65  _PAGECACHE.** ve
10a24 72 62 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  rb to sqlite3_co
10a25 6e 66 69 67 28 29 2e 20 50 61 72 61 6d 65 74 65  nfig(). Paramete
10a26 72 20 70 42 75 66 20 70 6f 69 6e 74 73 20 74 6f  r pBuf points to
10a27 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c   an allocation l
10a28 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74  arge.** enough t
10a29 6f 20 63 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75  o contain 'n' bu
10a2a 66 66 65 72 73 20 6f 66 20 27 73 7a 27 20 62 79  ffers of 'sz' by
10a2b 74 65 73 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c  tes each..*/.SQL
10a2c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
10a2d 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 42 75   sqlite3PCacheBu
10a2e 66 66 65 72 53 65 74 75 70 28 76 6f 69 64 20 2a  fferSetup(void *
10a2f 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e  pBuf, int sz, in
10a30 74 20 6e 29 7b 0a 20 20 50 67 46 72 65 65 73 6c  t n){.  PgFreesl
10a31 6f 74 20 2a 70 3b 0a 20 20 73 7a 20 26 3d 20 7e  ot *p;.  sz &= ~
10a32 37 3b 0a 20 20 70 63 61 63 68 65 31 2e 73 7a 53  7;.  pcache1.szS
10a33 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20 70 63 61 63  lot = sz;.  pcac
10a34 68 65 31 2e 70 53 74 61 72 74 20 3d 20 70 42 75  he1.pStart = pBu
10a35 66 3b 0a 20 20 70 63 61 63 68 65 31 2e 70 46 72  f;.  pcache1.pFr
10a36 65 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  ee = 0;.  while(
10a37 20 6e 2d 2d 20 29 7b 0a 20 20 20 20 70 20 3d 20   n-- ){.    p = 
10a38 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75  (PgFreeslot*)pBu
10a39 66 3b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 20  f;.    p->pNext 
10a3a 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b  = pcache1.pFree;
10a3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72  .    pcache1.pFr
10a3c 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 42 75 66  ee = p;.    pBuf
10a3d 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61   = (void*)&((cha
10a3e 72 2a 29 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20  r*)pBuf)[sz];.  
10a3f 7d 0a 20 20 70 63 61 63 68 65 31 2e 70 45 6e 64  }.  pcache1.pEnd
10a40 20 3d 20 70 42 75 66 3b 0a 7d 0a 0a 2f 2a 0a 2a   = pBuf;.}../*.*
10a41 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f  * Malloc functio
10a42 6e 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 68  n used within th
10a43 69 73 20 66 69 6c 65 20 74 6f 20 61 6c 6c 6f 63  is file to alloc
10a44 61 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74  ate space from t
10a45 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 63 6f 6e  he buffer.** con
10a46 66 69 67 75 72 65 64 20 75 73 69 6e 67 20 73 71  figured using sq
10a47 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c  lite3_config(SQL
10a48 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
10a49 41 43 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66  ACHE) option. If
10a4a 20 6e 6f 20 0a 2a 2a 20 73 75 63 68 20 62 75 66   no .** such buf
10a4b 66 65 72 20 65 78 69 73 74 73 20 6f 72 20 74 68  fer exists or th
10a4c 65 72 65 20 69 73 20 6e 6f 20 73 70 61 63 65 20  ere is no space 
10a4d 6c 65 66 74 20 69 6e 20 69 74 2c 20 74 68 69 73  left in it, this
10a4e 20 66 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20   function falls 
10a4f 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 73 71 6c 69  .** back to sqli
10a50 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  te3Malloc()..*/.
10a51 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61  static void *pca
10a52 63 68 65 31 41 6c 6c 6f 63 28 69 6e 74 20 6e 42  che1Alloc(int nB
10a53 79 74 65 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b  yte){.  void *p;
10a54 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
10a55 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
10a56 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10a57 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 70 63 61    if( nByte<=pca
10a58 63 68 65 31 2e 73 7a 53 6c 6f 74 20 26 26 20 70  che1.szSlot && p
10a59 63 61 63 68 65 31 2e 70 46 72 65 65 20 29 7b 0a  cache1.pFree ){.
10a5a 20 20 20 20 70 20 3d 20 28 50 67 48 64 72 31 20      p = (PgHdr1 
10a5b 2a 29 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b  *)pcache1.pFree;
10a5c 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72  .    pcache1.pFr
10a5d 65 65 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72  ee = pcache1.pFr
10a5e 65 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73  ee->pNext;.    s
10a5f 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28  qlite3StatusSet(
10a60 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
10a61 47 45 43 41 43 48 45 5f 53 49 5a 45 2c 20 6e 42  GECACHE_SIZE, nB
10a62 79 74 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  yte);.    sqlite
10a63 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
10a64 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43  E_STATUS_PAGECAC
10a65 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 7d  HE_USED, 1);.  }
10a66 65 6c 73 65 7b 0a 0a 20 20 20 20 2f 2a 20 41 6c  else{..    /* Al
10a67 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 62 75 66  locate a new buf
10a68 66 65 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65  fer using sqlite
10a69 33 4d 61 6c 6c 6f 63 2e 20 42 65 66 6f 72 65 20  3Malloc. Before 
10a6a 64 6f 69 6e 67 20 73 6f 2c 20 65 78 69 74 20 74  doing so, exit t
10a6b 68 65 0a 20 20 20 20 2a 2a 20 67 6c 6f 62 61 6c  he.    ** global
10a6c 20 70 63 61 63 68 65 20 6d 75 74 65 78 20 61 6e   pcache mutex an
10a6d 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67  d unlock the pag
10a6e 65 72 2d 63 61 63 68 65 20 6f 62 6a 65 63 74 20  er-cache object 
10a6f 70 43 61 63 68 65 2e 20 54 68 69 73 20 69 73 20  pCache. This is 
10a70 0a 20 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20  .    ** so that 
10a71 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74  if the attempt t
10a72 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  o allocate a new
10a73 20 62 75 66 66 65 72 20 63 61 75 73 65 73 20 74   buffer causes t
10a74 68 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  he the .    ** c
10a75 6f 6e 66 69 67 75 72 65 64 20 73 6f 66 74 2d 68  onfigured soft-h
10a76 65 61 70 2d 6c 69 6d 69 74 20 74 6f 20 62 65 20  eap-limit to be 
10a77 62 72 65 61 63 68 65 64 2c 20 69 74 20 77 69 6c  breached, it wil
10a78 6c 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f  l be possible to
10a79 0a 20 20 20 20 2a 2a 20 72 65 63 6c 61 69 6d 20  .    ** reclaim 
10a7a 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 69 73  memory from this
10a7b 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 20 20   pager-cache..  
10a7c 20 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31    */.    pcache1
10a7d 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
10a7e 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
10a7f 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 20 20  loc(nByte);.    
10a80 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
10a81 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29  x();.    if( p )
10a82 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 20 3d  {.      int sz =
10a83 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69   sqlite3MallocSi
10a84 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c  ze(p);.      sql
10a85 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
10a86 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
10a87 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20  CACHE_OVERFLOW, 
10a88 73 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  sz);.    }.  }. 
10a89 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
10a8a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61 6c 6c 6f  .** Free an allo
10a8b 63 61 74 65 64 20 62 75 66 66 65 72 20 6f 62 74  cated buffer obt
10a8c 61 69 6e 65 64 20 66 72 6f 6d 20 70 63 61 63 68  ained from pcach
10a8d 65 31 41 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74  e1Alloc()..*/.st
10a8e 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
10a8f 31 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a  1Free(void *p){.
10a90 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
10a91 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61  3_mutex_held(pca
10a92 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20  che1.mutex) );. 
10a93 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
10a94 72 6e 3b 0a 20 20 69 66 28 20 70 3e 3d 70 63 61  rn;.  if( p>=pca
10a95 63 68 65 31 2e 70 53 74 61 72 74 20 26 26 20 70  che1.pStart && p
10a96 3c 70 63 61 63 68 65 31 2e 70 45 6e 64 20 29 7b  <pcache1.pEnd ){
10a97 0a 20 20 20 20 50 67 46 72 65 65 73 6c 6f 74 20  .    PgFreeslot 
10a98 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73 71 6c 69  *pSlot;.    sqli
10a99 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
10a9a 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
10a9b 41 43 48 45 5f 55 53 45 44 2c 20 2d 31 29 3b 0a  ACHE_USED, -1);.
10a9c 20 20 20 20 70 53 6c 6f 74 20 3d 20 28 50 67 46      pSlot = (PgF
10a9d 72 65 65 73 6c 6f 74 2a 29 70 3b 0a 20 20 20 20  reeslot*)p;.    
10a9e 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 3d 20 70  pSlot->pNext = p
10a9f 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 20  cache1.pFree;.  
10aa0 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20    pcache1.pFree 
10aa1 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d 65 6c 73 65  = pSlot;.  }else
10aa2 7b 0a 20 20 20 20 69 6e 74 20 69 53 69 7a 65 20  {.    int iSize 
10aa3 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
10aa4 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69  ize(p);.    sqli
10aa5 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
10aa6 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
10aa7 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d  ACHE_OVERFLOW, -
10aa8 69 53 69 7a 65 29 3b 0a 20 20 20 20 73 71 6c 69  iSize);.    sqli
10aa9 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d  te3_free(p);.  }
10aaa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
10aab 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62  te a new page ob
10aac 6a 65 63 74 20 69 6e 69 74 69 61 6c 6c 79 20 61  ject initially a
10aad 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63  ssociated with c
10aae 61 63 68 65 20 70 43 61 63 68 65 2e 0a 2a 2f 0a  ache pCache..*/.
10aaf 73 74 61 74 69 63 20 50 67 48 64 72 31 20 2a 70  static PgHdr1 *p
10ab0 63 61 63 68 65 31 41 6c 6c 6f 63 50 61 67 65 28  cache1AllocPage(
10ab1 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 29  PCache1 *pCache)
10ab2 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20  {.  int nByte = 
10ab3 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 20 2b  sizeof(PgHdr1) +
10ab4 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 3b   pCache->szPage;
10ab5 0a 20 20 50 67 48 64 72 31 20 2a 70 20 3d 20 28  .  PgHdr1 *p = (
10ab6 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65 31  PgHdr1 *)pcache1
10ab7 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  Alloc(nByte);.  
10ab8 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d  if( p ){.    mem
10ab9 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74 65 29  set(p, 0, nByte)
10aba 3b 0a 20 20 20 20 69 66 28 20 70 43 61 63 68 65  ;.    if( pCache
10abb 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a  ->bPurgeable ){.
10abc 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43        pcache1.nC
10abd 75 72 72 65 6e 74 50 61 67 65 2b 2b 3b 0a 20 20  urrentPage++;.  
10abe 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
10abf 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   p;.}../*.** Fre
10ac0 65 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20  e a page object 
10ac1 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61  allocated by pca
10ac2 63 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e  che1AllocPage().
10ac3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10ac4 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
10ac5 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69 66  PgHdr1 *p){.  if
10ac6 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ( p ){.    if( p
10ac7 2d 3e 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  ->pCache->bPurge
10ac8 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 63  able ){.      pc
10ac9 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61  ache1.nCurrentPa
10aca 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ge--;.    }.    
10acb 70 63 61 63 68 65 31 46 72 65 65 28 70 29 3b 0a  pcache1Free(p);.
10acc 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c    }.}../*.** Mal
10acd 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  loc function use
10ace 64 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 6f  d by SQLite to o
10acf 62 74 61 69 6e 20 73 70 61 63 65 20 66 72 6f 6d  btain space from
10ad0 20 74 68 65 20 62 75 66 66 65 72 20 63 6f 6e 66   the buffer conf
10ad1 69 67 75 72 65 64 0a 2a 2a 20 75 73 69 6e 67 20  igured.** using 
10ad2 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53  sqlite3_config(S
10ad3 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
10ad4 45 43 41 43 48 45 29 20 6f 70 74 69 6f 6e 2e 20  ECACHE) option. 
10ad5 49 66 20 6e 6f 20 73 75 63 68 20 62 75 66 66 65  If no such buffe
10ad6 72 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 69  r.** exists, thi
10ad7 73 20 66 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73  s function falls
10ad8 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33   back to sqlite3
10ad9 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c  Malloc()..*/.SQL
10ada 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
10adb 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c   *sqlite3PageMal
10adc 6c 6f 63 28 69 6e 74 20 73 7a 29 7b 0a 20 20 76  loc(int sz){.  v
10add 6f 69 64 20 2a 70 3b 0a 20 20 70 63 61 63 68 65  oid *p;.  pcache
10ade 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10adf 20 70 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f   p = pcache1Allo
10ae0 63 28 73 7a 29 3b 0a 20 20 70 63 61 63 68 65 31  c(sz);.  pcache1
10ae1 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
10ae2 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
10ae3 2a 2a 20 46 72 65 65 20 61 6e 20 61 6c 6c 6f 63  ** Free an alloc
10ae4 61 74 65 64 20 62 75 66 66 65 72 20 6f 62 74 61  ated buffer obta
10ae5 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
10ae6 33 50 61 67 65 4d 61 6c 6c 6f 63 28 29 2e 0a 2a  3PageMalloc()..*
10ae7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10ae8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
10ae9 65 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a  eFree(void *p){.
10aea 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10aeb 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31  tex();.  pcache1
10aec 46 72 65 65 28 70 29 3b 0a 20 20 70 63 61 63 68  Free(p);.  pcach
10aed 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  e1LeaveMutex();.
10aee 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
10aef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10af1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10af2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10af3 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65  **/./******** Ge
10af4 6e 65 72 61 6c 20 49 6d 70 6c 65 6d 65 6e 74 61  neral Implementa
10af5 74 69 6f 6e 20 46 75 6e 63 74 69 6f 6e 73 20 2a  tion Functions *
10af6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10af7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10af8 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ***/../*.** This
10af9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
10afa 64 20 74 6f 20 72 65 73 69 7a 65 20 74 68 65 20  d to resize the 
10afb 68 61 73 68 20 74 61 62 6c 65 20 75 73 65 64 20  hash table used 
10afc 62 79 20 74 68 65 20 63 61 63 68 65 20 70 61 73  by the cache pas
10afd 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69  sed.** as the fi
10afe 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  rst argument..**
10aff 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d  .** The global m
10b00 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c  utex must be hel
10b01 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  d when this func
10b02 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tion is called..
10b03 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63  */.static int pc
10b04 61 63 68 65 31 52 65 73 69 7a 65 48 61 73 68 28  ache1ResizeHash(
10b05 50 43 61 63 68 65 31 20 2a 70 29 7b 0a 20 20 50  PCache1 *p){.  P
10b06 67 48 64 72 31 20 2a 2a 61 70 4e 65 77 3b 0a 20  gHdr1 **apNew;. 
10b07 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4e   unsigned int nN
10b08 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  ew;.  unsigned i
10b09 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28  nt i;..  assert(
10b0a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
10b0b 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65  eld(pcache1.mute
10b0c 78 29 20 29 3b 0a 0a 20 20 6e 4e 65 77 20 3d 20  x) );..  nNew = 
10b0d 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a 20 20 69 66  p->nHash*2;.  if
10b0e 28 20 6e 4e 65 77 3c 32 35 36 20 29 7b 0a 20 20  ( nNew<256 ){.  
10b0f 20 20 6e 4e 65 77 20 3d 20 32 35 36 3b 0a 20 20    nNew = 256;.  
10b10 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  }..  pcache1Leav
10b11 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20  eMutex();.  if( 
10b12 70 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69  p->nHash ){ sqli
10b13 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
10b14 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 61 70 4e 65  lloc(); }.  apNe
10b15 77 20 3d 20 28 50 67 48 64 72 31 20 2a 2a 29 73  w = (PgHdr1 **)s
10b16 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
10b17 7a 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a 6e  zeof(PgHdr1 *)*n
10b18 4e 65 77 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  New);.  if( p->n
10b19 48 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 45  Hash ){ sqlite3E
10b1a 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
10b1b 3b 20 7d 0a 20 20 70 63 61 63 68 65 31 45 6e 74  ; }.  pcache1Ent
10b1c 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28  erMutex();.  if(
10b1d 20 61 70 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65   apNew ){.    me
10b1e 6d 73 65 74 28 61 70 4e 65 77 2c 20 30 2c 20 73  mset(apNew, 0, s
10b1f 69 7a 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a  izeof(PgHdr1 *)*
10b20 6e 4e 65 77 29 3b 0a 20 20 20 20 66 6f 72 28 69  nNew);.    for(i
10b21 3d 30 3b 20 69 3c 70 2d 3e 6e 48 61 73 68 3b 20  =0; i<p->nHash; 
10b22 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 48 64  i++){.      PgHd
10b23 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20  r1 *pPage;.     
10b24 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 20 3d   PgHdr1 *pNext =
10b25 20 70 2d 3e 61 70 48 61 73 68 5b 69 5d 3b 0a 20   p->apHash[i];. 
10b26 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 50 61       while( (pPa
10b27 67 65 20 3d 20 70 4e 65 78 74 29 21 3d 30 20 29  ge = pNext)!=0 )
10b28 7b 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e  {.        unsign
10b29 65 64 20 69 6e 74 20 68 20 3d 20 70 50 61 67 65  ed int h = pPage
10b2a 2d 3e 69 4b 65 79 20 25 20 6e 4e 65 77 3b 0a 20  ->iKey % nNew;. 
10b2b 20 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70         pNext = p
10b2c 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Page->pNext;.   
10b2d 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78       pPage->pNex
10b2e 74 20 3d 20 61 70 4e 65 77 5b 68 5d 3b 0a 20 20  t = apNew[h];.  
10b2f 20 20 20 20 20 20 61 70 4e 65 77 5b 68 5d 20 3d        apNew[h] =
10b30 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a   pPage;.      }.
10b31 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
10b32 33 5f 66 72 65 65 28 70 2d 3e 61 70 48 61 73 68  3_free(p->apHash
10b33 29 3b 0a 20 20 20 20 70 2d 3e 61 70 48 61 73 68  );.    p->apHash
10b34 20 3d 20 61 70 4e 65 77 3b 0a 20 20 20 20 70 2d   = apNew;.    p-
10b35 3e 6e 48 61 73 68 20 3d 20 6e 4e 65 77 3b 0a 20  >nHash = nNew;. 
10b36 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 70 2d   }..  return (p-
10b37 3e 61 70 48 61 73 68 20 3f 20 53 51 4c 49 54 45  >apHash ? SQLITE
10b38 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d  _OK : SQLITE_NOM
10b39 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  EM);.}../*.** Th
10b3a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
10b3b 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74  sed internally t
10b3c 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 70 61 67  o remove the pag
10b3d 65 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65  e pPage from the
10b3e 20 0a 2a 2a 20 67 6c 6f 62 61 6c 20 4c 52 55 20   .** global LRU 
10b3f 6c 69 73 74 2c 20 69 66 20 69 73 20 70 61 72 74  list, if is part
10b40 20 6f 66 20 69 74 2e 20 49 66 20 70 50 61 67 65   of it. If pPage
10b41 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20   is not part of 
10b42 74 68 65 20 67 6c 6f 62 61 6c 0a 2a 2a 20 4c 52  the global.** LR
10b43 55 20 6c 69 73 74 2c 20 74 68 65 6e 20 74 68 69  U list, then thi
10b44 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
10b45 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  no-op..**.** The
10b46 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75   global mutex mu
10b47 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20  st be held when 
10b48 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
10b49 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74   called..*/.stat
10b4a 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 50  ic void pcache1P
10b4b 69 6e 50 61 67 65 28 50 67 48 64 72 31 20 2a 70  inPage(PgHdr1 *p
10b4c 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Page){.  assert(
10b4d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
10b4e 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65  eld(pcache1.mute
10b4f 78 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  x) );.  if( pPag
10b50 65 20 26 26 20 28 70 50 61 67 65 2d 3e 70 4c 72  e && (pPage->pLr
10b51 75 4e 65 78 74 20 7c 7c 20 70 50 61 67 65 3d 3d  uNext || pPage==
10b52 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
10b53 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  ) ){.    if( pPa
10b54 67 65 2d 3e 70 4c 72 75 50 72 65 76 20 29 7b 0a  ge->pLruPrev ){.
10b55 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72        pPage->pLr
10b56 75 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 20  uPrev->pLruNext 
10b57 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78  = pPage->pLruNex
10b58 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
10b59 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10b5a 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d   ){.      pPage-
10b5b 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50  >pLruNext->pLruP
10b5c 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72  rev = pPage->pLr
10b5d 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20  uPrev;.    }.   
10b5e 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c 72   if( pcache1.pLr
10b5f 75 48 65 61 64 3d 3d 70 50 61 67 65 20 29 7b 0a  uHead==pPage ){.
10b60 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c        pcache1.pL
10b61 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 2d 3e  ruHead = pPage->
10b62 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  pLruNext;.    }.
10b63 20 20 20 20 69 66 28 20 70 63 61 63 68 65 31 2e      if( pcache1.
10b64 70 4c 72 75 54 61 69 6c 3d 3d 70 50 61 67 65 20  pLruTail==pPage 
10b65 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  ){.      pcache1
10b66 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 61 67  .pLruTail = pPag
10b67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20  e->pLruPrev;.   
10b68 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c   }.    pPage->pL
10b69 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20  ruNext = 0;.    
10b6a 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20  pPage->pLruPrev 
10b6b 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  = 0;.    pPage->
10b6c 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61  pCache->nRecycla
10b6d 62 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f  ble--;.  }.}.../
10b6e 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20  *.** Remove the 
10b6f 70 61 67 65 20 73 75 70 70 6c 69 65 64 20 61 73  page supplied as
10b70 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 66 72 6f   an argument fro
10b71 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
10b72 20 0a 2a 2a 20 28 50 43 61 63 68 65 31 2e 61 70   .** (PCache1.ap
10b73 48 61 73 68 20 73 74 72 75 63 74 75 72 65 29 20  Hash structure) 
10b74 74 68 61 74 20 69 74 20 69 73 20 63 75 72 72 65  that it is curre
10b75 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 2e 0a  ntly stored in..
10b76 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c  **.** The global
10b77 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68   mutex must be h
10b78 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75  eld when this fu
10b79 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
10b7a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
10b7b 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72   pcache1RemoveFr
10b7c 6f 6d 48 61 73 68 28 50 67 48 64 72 31 20 2a 70  omHash(PgHdr1 *p
10b7d 50 61 67 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65  Page){.  unsigne
10b7e 64 20 69 6e 74 20 68 3b 0a 20 20 50 43 61 63 68  d int h;.  PCach
10b7f 65 31 20 2a 70 43 61 63 68 65 20 3d 20 70 50 61  e1 *pCache = pPa
10b80 67 65 2d 3e 70 43 61 63 68 65 3b 0a 20 20 50 67  ge->pCache;.  Pg
10b81 48 64 72 31 20 2a 2a 70 70 3b 0a 0a 20 20 68 20  Hdr1 **pp;..  h 
10b82 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25 20  = pPage->iKey % 
10b83 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20  pCache->nHash;. 
10b84 20 66 6f 72 28 70 70 3d 26 70 43 61 63 68 65 2d   for(pp=&pCache-
10b85 3e 61 70 48 61 73 68 5b 68 5d 3b 20 28 2a 70 70  >apHash[h]; (*pp
10b86 29 21 3d 70 50 61 67 65 3b 20 70 70 3d 26 28 2a  )!=pPage; pp=&(*
10b87 70 70 29 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 2a  pp)->pNext);.  *
10b88 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78  pp = (*pp)->pNex
10b89 74 3b 0a 0a 20 20 70 43 61 63 68 65 2d 3e 6e 50  t;..  pCache->nP
10b8a 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  age--;.}../*.** 
10b8b 49 66 20 74 68 65 72 65 20 61 72 65 20 63 75 72  If there are cur
10b8c 72 65 6e 74 6c 79 20 6d 6f 72 65 20 74 68 61 6e  rently more than
10b8d 20 70 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65   pcache.nMaxPage
10b8e 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64   pages allocated
10b8f 2c 20 74 72 79 0a 2a 2a 20 74 6f 20 72 65 63 79  , try.** to recy
10b90 63 6c 65 20 70 61 67 65 73 20 74 6f 20 72 65 64  cle pages to red
10b91 75 63 65 20 74 68 65 20 6e 75 6d 62 65 72 20 61  uce the number a
10b92 6c 6c 6f 63 61 74 65 64 20 74 6f 20 70 63 61 63  llocated to pcac
10b93 68 65 2e 6e 4d 61 78 50 61 67 65 2e 0a 2a 2f 0a  he.nMaxPage..*/.
10b94 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10b95 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67  he1EnforceMaxPag
10b96 65 28 76 6f 69 64 29 7b 0a 20 20 61 73 73 65 72  e(void){.  asser
10b97 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
10b98 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75  _held(pcache1.mu
10b99 74 65 78 29 20 29 3b 0a 20 20 77 68 69 6c 65 28  tex) );.  while(
10b9a 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10b9b 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d  tPage>pcache1.nM
10b9c 61 78 50 61 67 65 20 26 26 20 70 63 61 63 68 65  axPage && pcache
10b9d 31 2e 70 4c 72 75 54 61 69 6c 20 29 7b 0a 20 20  1.pLruTail ){.  
10b9e 20 20 50 67 48 64 72 31 20 2a 70 20 3d 20 70 63    PgHdr1 *p = pc
10b9f 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a  ache1.pLruTail;.
10ba0 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61      pcache1PinPa
10ba1 67 65 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68  ge(p);.    pcach
10ba2 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68  e1RemoveFromHash
10ba3 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31  (p);.    pcache1
10ba4 46 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 7d  FreePage(p);.  }
10ba5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 63 61 72  .}../*.** Discar
10ba6 64 20 61 6c 6c 20 70 61 67 65 73 20 66 72 6f 6d  d all pages from
10ba7 20 63 61 63 68 65 20 70 43 61 63 68 65 20 77 69   cache pCache wi
10ba8 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  th a page number
10ba9 20 28 6b 65 79 20 76 61 6c 75 65 29 20 0a 2a 2a   (key value) .**
10baa 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
10bab 20 65 71 75 61 6c 20 74 6f 20 69 4c 69 6d 69 74   equal to iLimit
10bac 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67  . Any pinned pag
10bad 65 73 20 74 68 61 74 20 6d 65 65 74 20 74 68 69  es that meet thi
10bae 73 20 0a 2a 2a 20 63 72 69 74 65 72 69 61 20 61  s .** criteria a
10baf 72 65 20 75 6e 70 69 6e 6e 65 64 20 62 65 66 6f  re unpinned befo
10bb0 72 65 20 74 68 65 79 20 61 72 65 20 64 69 73 63  re they are disc
10bb1 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  arded..**.** The
10bb2 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75   global mutex mu
10bb3 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20  st be held when 
10bb4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
10bb5 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74   called..*/.stat
10bb6 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 54  ic void pcache1T
10bb7 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 0a 20  runcateUnsafe(. 
10bb8 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
10bb9 2c 20 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  , .  unsigned in
10bba 74 20 69 4c 69 6d 69 74 20 0a 29 7b 0a 20 20 75  t iLimit .){.  u
10bbb 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20  nsigned int h;. 
10bbc 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
10bbd 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63  _mutex_held(pcac
10bbe 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  he1.mutex) );.  
10bbf 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43 61 63 68  for(h=0; h<pCach
10bc0 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b 29 7b 0a  e->nHash; h++){.
10bc1 20 20 20 20 50 67 48 64 72 31 20 2a 2a 70 70 20      PgHdr1 **pp 
10bc2 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73  = &pCache->apHas
10bc3 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67 48 64 72  h[h]; .    PgHdr
10bc4 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 77 68  1 *pPage;.    wh
10bc5 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 2a 70  ile( (pPage = *p
10bc6 70 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  p)!=0 ){.      i
10bc7 66 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3e 3d  f( pPage->iKey>=
10bc8 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20  iLimit ){.      
10bc9 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65    pcache1PinPage
10bca 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
10bcb 20 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e   *pp = pPage->pN
10bcc 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 63 61  ext;.        pca
10bcd 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50 61  che1FreePage(pPa
10bce 67 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ge);.      }else
10bcf 7b 0a 20 20 20 20 20 20 20 20 70 70 20 3d 20 26  {.        pp = &
10bd0 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20  pPage->pNext;.  
10bd1 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
10bd2 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
10bd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bd7 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71  **/./******** sq
10bd8 6c 69 74 65 33 5f 70 63 61 63 68 65 20 4d 65 74  lite3_pcache Met
10bd9 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  hods ***********
10bda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bdc 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  ***/../*.** Impl
10bdd 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
10bde 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
10bdf 2e 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e 0a 2a  .xInit method..*
10be0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 61  /.static int pca
10be1 63 68 65 31 49 6e 69 74 28 76 6f 69 64 20 2a 4e  che1Init(void *N
10be2 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45  otUsed){.  UNUSE
10be3 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
10be4 73 65 64 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26  sed);.  memset(&
10be5 70 63 61 63 68 65 31 2c 20 30 2c 20 73 69 7a 65  pcache1, 0, size
10be6 6f 66 28 70 63 61 63 68 65 31 29 29 3b 0a 20 20  of(pcache1));.  
10be7 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
10be8 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
10be9 65 78 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65  ex ){.    pcache
10bea 31 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  1.mutex = sqlite
10beb 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51  3_mutex_alloc(SQ
10bec 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
10bed 43 5f 4c 52 55 29 3b 0a 20 20 7d 0a 20 20 72 65  C_LRU);.  }.  re
10bee 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
10bef 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
10bf0 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
10bf1 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53  qlite3_pcache.xS
10bf2 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a  hutdown method..
10bf3 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10bf4 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76  cache1Shutdown(v
10bf5 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  oid *NotUsed){. 
10bf6 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
10bf7 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2f 2a  R(NotUsed);.  /*
10bf8 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 0a 2f 2a 0a   no-op */.}../*.
10bf9 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
10bfa 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
10bfb 5f 70 63 61 63 68 65 2e 78 43 72 65 61 74 65 20  _pcache.xCreate 
10bfc 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c  method..**.** Al
10bfd 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 63 61 63  locate a new cac
10bfe 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71  he..*/.static sq
10bff 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 63  lite3_pcache *pc
10c00 61 63 68 65 31 43 72 65 61 74 65 28 69 6e 74 20  ache1Create(int 
10c01 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 50 75 72  szPage, int bPur
10c02 67 65 61 62 6c 65 29 7b 0a 20 20 50 43 61 63 68  geable){.  PCach
10c03 65 31 20 2a 70 43 61 63 68 65 3b 0a 0a 20 20 70  e1 *pCache;..  p
10c04 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31  Cache = (PCache1
10c05 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
10c06 63 28 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31  c(sizeof(PCache1
10c07 29 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65  ));.  if( pCache
10c08 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70   ){.    memset(p
10c09 43 61 63 68 65 2c 20 30 2c 20 73 69 7a 65 6f 66  Cache, 0, sizeof
10c0a 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 20 20  (PCache1));.    
10c0b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 3d  pCache->szPage =
10c0c 20 73 7a 50 61 67 65 3b 0a 20 20 20 20 70 43 61   szPage;.    pCa
10c0d 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20  che->bPurgeable 
10c0e 3d 20 28 62 50 75 72 67 65 61 62 6c 65 20 3f 20  = (bPurgeable ? 
10c0f 31 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20  1 : 0);.    if( 
10c10 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20  bPurgeable ){.  
10c11 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e      pCache->nMin
10c12 20 3d 20 31 30 3b 0a 20 20 20 20 20 20 70 63 61   = 10;.      pca
10c13 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10c14 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  ;.      pcache1.
10c15 6e 4d 69 6e 50 61 67 65 20 2b 3d 20 70 43 61 63  nMinPage += pCac
10c16 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 20 20 20 20  he->nMin;.      
10c17 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65  pcache1LeaveMute
10c18 78 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  x();.    }.  }. 
10c19 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33   return (sqlite3
10c1a 5f 70 63 61 63 68 65 20 2a 29 70 43 61 63 68 65  _pcache *)pCache
10c1b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
10c1c 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
10c1d 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e   sqlite3_pcache.
10c1e 78 43 61 63 68 65 73 69 7a 65 20 6d 65 74 68 6f  xCachesize metho
10c1f 64 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67  d. .**.** Config
10c20 75 72 65 20 74 68 65 20 63 61 63 68 65 5f 73 69  ure the cache_si
10c21 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 61 20 63  ze limit for a c
10c22 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ache..*/.static 
10c23 76 6f 69 64 20 70 63 61 63 68 65 31 43 61 63 68  void pcache1Cach
10c24 65 73 69 7a 65 28 73 71 6c 69 74 65 33 5f 70 63  esize(sqlite3_pc
10c25 61 63 68 65 20 2a 70 2c 20 69 6e 74 20 6e 4d 61  ache *p, int nMa
10c26 78 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70  x){.  PCache1 *p
10c27 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31  Cache = (PCache1
10c28 20 2a 29 70 3b 0a 20 20 69 66 28 20 70 43 61 63   *)p;.  if( pCac
10c29 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29  he->bPurgeable )
10c2a 7b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e 74  {.    pcache1Ent
10c2b 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 70  erMutex();.    p
10c2c 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20  cache1.nMaxPage 
10c2d 2b 3d 20 28 6e 4d 61 78 20 2d 20 70 43 61 63 68  += (nMax - pCach
10c2e 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 20 70 43  e->nMax);.    pC
10c2f 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20 6e 4d 61  ache->nMax = nMa
10c30 78 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e  x;.    pcache1En
10c31 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b 0a  forceMaxPage();.
10c32 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65      pcache1Leave
10c33 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 7d 0a 0a  Mutex();.  }.}..
10c34 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
10c35 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
10c36 74 65 33 5f 70 63 61 63 68 65 2e 78 50 61 67 65  te3_pcache.xPage
10c37 63 6f 75 6e 74 20 6d 65 74 68 6f 64 2e 20 0a 2a  count method. .*
10c38 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 61  /.static int pca
10c39 63 68 65 31 50 61 67 65 63 6f 75 6e 74 28 73 71  che1Pagecount(sq
10c3a 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29  lite3_pcache *p)
10c3b 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 70 63 61  {.  int n;.  pca
10c3c 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10c3d 3b 0a 20 20 6e 20 3d 20 28 28 50 43 61 63 68 65  ;.  n = ((PCache
10c3e 31 20 2a 29 70 29 2d 3e 6e 50 61 67 65 3b 0a 20  1 *)p)->nPage;. 
10c3f 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10c40 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e  ex();.  return n
10c41 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
10c42 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
10c43 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e   sqlite3_pcache.
10c44 78 46 65 74 63 68 20 6d 65 74 68 6f 64 2e 20 0a  xFetch method. .
10c45 2a 2a 0a 2a 2a 20 46 65 74 63 68 20 61 20 70 61  **.** Fetch a pa
10c46 67 65 20 62 79 20 6b 65 79 20 76 61 6c 75 65 2e  ge by key value.
10c47 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f  .**.** Whether o
10c48 72 20 6e 6f 74 20 61 20 6e 65 77 20 70 61 67 65  r not a new page
10c49 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65   may be allocate
10c4a 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
10c4b 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 0a 2a 2a  on depends on.**
10c4c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
10c4d 65 20 63 72 65 61 74 65 46 6c 61 67 20 61 72 67  e createFlag arg
10c4e 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ument..**.** The
10c4f 72 65 20 61 72 65 20 74 68 72 65 65 20 64 69 66  re are three dif
10c50 66 65 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65  ferent approache
10c51 73 20 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73  s to obtaining s
10c52 70 61 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c  pace for a page,
10c53 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  .** depending on
10c54 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61   the value of pa
10c55 72 61 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c  rameter createFl
10c56 61 67 20 28 77 68 69 63 68 20 6d 61 79 20 62 65  ag (which may be
10c57 20 30 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a   0, 1 or 2)..**.
10c58 2a 2a 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65  **   1. Regardle
10c59 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20  ss of the value 
10c5a 6f 66 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74  of createFlag, t
10c5b 68 65 20 63 61 63 68 65 20 69 73 20 73 65 61 72  he cache is sear
10c5c 63 68 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20  ched for a .**  
10c5d 20 20 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20      copy of the 
10c5e 72 65 71 75 65 73 74 65 64 20 70 61 67 65 2e 20  requested page. 
10c5f 49 66 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c  If one is found,
10c60 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e   it is returned.
10c61 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63  .**.**   2. If c
10c62 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64  reateFlag==0 and
10c63 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
10c64 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
10c65 63 61 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a  cache, NULL is.*
10c66 2a 20 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e  *      returned.
10c67 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63  .**.**   3. If c
10c68 72 65 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20  reateFlag is 1, 
10c69 74 68 65 20 63 61 63 68 65 20 69 73 20 6d 61 72  the cache is mar
10c6a 6b 65 64 20 61 73 20 70 75 72 67 65 61 62 6c 65  ked as purgeable
10c6b 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73   and the page is
10c6c 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 74 20 61 6c   .**      not al
10c6d 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63  ready in the cac
10c6e 68 65 2c 20 61 6e 64 20 69 66 20 65 69 74 68 65  he, and if eithe
10c6f 72 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  r of the followi
10c70 6e 67 20 61 72 65 20 74 72 75 65 2c 20 0a 2a 2a  ng are true, .**
10c71 20 20 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c        return NUL
10c72 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28  L:.**.**       (
10c73 61 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  a) the number of
10c74 20 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62 79   pages pinned by
10c75 20 74 68 65 20 63 61 63 68 65 20 69 73 20 67 72   the cache is gr
10c76 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20  eater than.**   
10c77 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31 2e          PCache1.
10c78 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20 20 20 20  nMax, or.**     
10c79 20 20 28 62 29 20 74 68 65 20 6e 75 6d 62 65 72    (b) the number
10c7a 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64   of pages pinned
10c7b 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 73   by the cache is
10c7c 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a   greater than.**
10c7d 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73             the s
10c7e 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 20 61  um of nMax for a
10c7f 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63  ll purgeable cac
10c80 68 65 73 2c 20 6c 65 73 73 20 74 68 65 20 73 75  hes, less the su
10c81 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20  m of .**        
10c82 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61 6c 6c 20     nMin for all 
10c83 6f 74 68 65 72 20 70 75 72 67 65 61 62 6c 65 20  other purgeable 
10c84 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 20  caches. .**.**  
10c85 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74   4. If none of t
10c86 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 63  he first three c
10c87 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 6c 79 20  onditions apply 
10c88 61 6e 64 20 74 68 65 20 63 61 63 68 65 20 69 73  and the cache is
10c89 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20 20 20 20   marked.**      
10c8a 61 73 20 70 75 72 67 65 61 62 6c 65 2c 20 61 6e  as purgeable, an
10c8b 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20  d if one of the 
10c8c 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75  following is tru
10c8d 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28  e:.**.**       (
10c8e 61 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  a) The number of
10c8f 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64   pages allocated
10c90 20 66 6f 72 20 74 68 65 20 63 61 63 68 65 20 69   for the cache i
10c91 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 20 20  s already .**   
10c92 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31 2e          PCache1.
10c93 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20  nMax, or.**.**  
10c94 20 20 20 20 20 28 62 29 20 54 68 65 20 6e 75 6d       (b) The num
10c95 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c  ber of pages all
10c96 6f 63 61 74 65 64 20 66 6f 72 20 61 6c 6c 20 70  ocated for all p
10c97 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20  urgeable caches 
10c98 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  is.**           
10c99 61 6c 72 65 61 64 79 20 65 71 75 61 6c 20 74 6f  already equal to
10c9a 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e   or greater than
10c9b 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78   the sum of nMax
10c9c 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20 20 20 20   for all.**     
10c9d 20 20 20 20 20 20 70 75 72 67 65 61 62 6c 65 20        purgeable 
10c9e 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 20  caches,.**.**   
10c9f 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20     then attempt 
10ca0 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67  to recycle a pag
10ca1 65 20 66 72 6f 6d 20 74 68 65 20 4c 52 55 20 6c  e from the LRU l
10ca2 69 73 74 2e 20 49 66 20 69 74 20 69 73 20 74 68  ist. If it is th
10ca3 65 20 72 69 67 68 74 0a 2a 2a 20 20 20 20 20 20  e right.**      
10ca4 73 69 7a 65 2c 20 72 65 74 75 72 6e 20 74 68 65  size, return the
10ca5 20 72 65 63 79 63 6c 65 64 20 62 75 66 66 65 72   recycled buffer
10ca6 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 72 65  . Otherwise, fre
10ca7 65 20 74 68 65 20 62 75 66 66 65 72 20 61 6e 64  e the buffer and
10ca8 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63 65 65 64  .**      proceed
10ca9 20 74 6f 20 73 74 65 70 20 35 2e 20 0a 2a 2a 0a   to step 5. .**.
10caa 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72 77 69 73  **   5. Otherwis
10cab 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20  e, allocate and 
10cac 72 65 74 75 72 6e 20 61 20 6e 65 77 20 70 61 67  return a new pag
10cad 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61  e buffer..*/.sta
10cae 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65  tic void *pcache
10caf 31 46 65 74 63 68 28 73 71 6c 69 74 65 33 5f 70  1Fetch(sqlite3_p
10cb0 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e  cache *p, unsign
10cb1 65 64 20 69 6e 74 20 69 4b 65 79 2c 20 69 6e 74  ed int iKey, int
10cb2 20 63 72 65 61 74 65 46 6c 61 67 29 7b 0a 20 20   createFlag){.  
10cb3 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 69  unsigned int nPi
10cb4 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 65 31 20  nned;.  PCache1 
10cb5 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68  *pCache = (PCach
10cb6 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31  e1 *)p;.  PgHdr1
10cb7 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20   *pPage = 0;..  
10cb8 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
10cb9 78 28 29 3b 0a 20 20 69 66 28 20 63 72 65 61 74  x();.  if( creat
10cba 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 74  eFlag==1 ) sqlit
10cbb 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
10cbc 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a 20 53 65 61  loc();..  /* Sea
10cbd 72 63 68 20 74 68 65 20 68 61 73 68 20 74 61 62  rch the hash tab
10cbe 6c 65 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69  le for an existi
10cbf 6e 67 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 69  ng entry. */.  i
10cc0 66 28 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68  f( pCache->nHash
10cc1 3e 30 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e  >0 ){.    unsign
10cc2 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 20  ed int h = iKey 
10cc3 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b  % pCache->nHash;
10cc4 0a 20 20 20 20 66 6f 72 28 70 50 61 67 65 3d 70  .    for(pPage=p
10cc5 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d  Cache->apHash[h]
10cc6 3b 20 70 50 61 67 65 26 26 70 50 61 67 65 2d 3e  ; pPage&&pPage->
10cc7 69 4b 65 79 21 3d 69 4b 65 79 3b 20 70 50 61 67  iKey!=iKey; pPag
10cc8 65 3d 70 50 61 67 65 2d 3e 70 4e 65 78 74 29 3b  e=pPage->pNext);
10cc9 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67  .  }..  if( pPag
10cca 65 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 3d  e || createFlag=
10ccb 3d 30 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65  =0 ){.    pcache
10ccc 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b  1PinPage(pPage);
10ccd 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f  .    goto fetch_
10cce 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  out;.  }..  /* S
10ccf 74 65 70 20 33 20 6f 66 20 68 65 61 64 65 72 20  tep 3 of header 
10cd0 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a 20 20 6e 50  comment. */.  nP
10cd1 69 6e 6e 65 64 20 3d 20 70 43 61 63 68 65 2d 3e  inned = pCache->
10cd2 6e 50 61 67 65 20 2d 20 70 43 61 63 68 65 2d 3e  nPage - pCache->
10cd3 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a 20 20 69  nRecyclable;.  i
10cd4 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31  f( createFlag==1
10cd5 20 26 26 20 70 43 61 63 68 65 2d 3e 62 50 75 72   && pCache->bPur
10cd6 67 65 61 62 6c 65 20 26 26 20 28 0a 20 20 20 20  geable && (.    
10cd7 20 20 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63      nPinned>=(pc
10cd8 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70  ache1.nMaxPage+p
10cd9 43 61 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63  Cache->nMin-pcac
10cda 68 65 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20  he1.nMinPage).  
10cdb 20 20 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28     || nPinned>=(
10cdc 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 0a 20 20  pCache->nMax).  
10cdd 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74  )){.    goto fet
10cde 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69  ch_out;.  }..  i
10cdf 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65  f( pCache->nPage
10ce0 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 20  >=pCache->nHash 
10ce1 26 26 20 70 63 61 63 68 65 31 52 65 73 69 7a 65  && pcache1Resize
10ce2 48 61 73 68 28 70 43 61 63 68 65 29 20 29 7b 0a  Hash(pCache) ){.
10ce3 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f      goto fetch_o
10ce4 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  ut;.  }..  /* St
10ce5 65 70 20 34 2e 20 54 72 79 20 74 6f 20 72 65 63  ep 4. Try to rec
10ce6 79 63 6c 65 20 61 20 70 61 67 65 20 62 75 66 66  ycle a page buff
10ce7 65 72 20 69 66 20 61 70 70 72 6f 70 72 69 61 74  er if appropriat
10ce8 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63  e. */.  if( pCac
10ce9 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26  he->bPurgeable &
10cea 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  & pcache1.pLruTa
10ceb 69 6c 20 26 26 20 28 0a 20 20 20 20 20 20 70 43  il && (.      pC
10cec 61 63 68 65 2d 3e 6e 50 61 67 65 3e 3d 70 43 61  ache->nPage>=pCa
10ced 63 68 65 2d 3e 6e 4d 61 78 2d 31 20 7c 7c 20 70  che->nMax-1 || p
10cee 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50  cache1.nCurrentP
10cef 61 67 65 3e 3d 70 63 61 63 68 65 31 2e 6e 4d 61  age>=pcache1.nMa
10cf0 78 50 61 67 65 0a 20 20 29 29 7b 0a 20 20 20 20  xPage.  )){.    
10cf1 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31 2e  pPage = pcache1.
10cf2 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63  pLruTail;.    pc
10cf3 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
10cf4 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ash(pPage);.    
10cf5 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70  pcache1PinPage(p
10cf6 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Page);.    if( p
10cf7 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 73 7a  Page->pCache->sz
10cf8 50 61 67 65 21 3d 70 43 61 63 68 65 2d 3e 73 7a  Page!=pCache->sz
10cf9 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63  Page ){.      pc
10cfa 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50  ache1FreePage(pP
10cfb 61 67 65 29 3b 0a 20 20 20 20 20 20 70 50 61 67  age);.      pPag
10cfc 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  e = 0;.    }else
10cfd 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  {.      pcache1.
10cfe 6e 43 75 72 72 65 6e 74 50 61 67 65 20 2d 3d 20  nCurrentPage -= 
10cff 28 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e  (pPage->pCache->
10d00 62 50 75 72 67 65 61 62 6c 65 20 2d 20 70 43 61  bPurgeable - pCa
10d01 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29  che->bPurgeable)
10d02 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
10d03 2a 20 53 74 65 70 20 35 2e 20 49 66 20 61 20 75  * Step 5. If a u
10d04 73 61 62 6c 65 20 70 61 67 65 20 62 75 66 66 65  sable page buffe
10d05 72 20 68 61 73 20 73 74 69 6c 6c 20 6e 6f 74 20  r has still not 
10d06 62 65 65 6e 20 66 6f 75 6e 64 2c 20 0a 20 20 2a  been found, .  *
10d07 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c  * attempt to all
10d08 6f 63 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 2e  ocate a new one.
10d09 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50   .  */.  if( !pP
10d0a 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65  age ){.    pPage
10d0b 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 50   = pcache1AllocP
10d0c 61 67 65 28 70 43 61 63 68 65 29 3b 0a 20 20 7d  age(pCache);.  }
10d0d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b  ..  if( pPage ){
10d0e 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  .    unsigned in
10d0f 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70 43 61  t h = iKey % pCa
10d10 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 20 20  che->nHash;.    
10d11 6d 65 6d 73 65 74 28 70 50 61 67 65 2c 20 30 2c  memset(pPage, 0,
10d12 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20   pCache->szPage 
10d13 2b 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29  + sizeof(PgHdr1)
10d14 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e  );.    pCache->n
10d15 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 67  Page++;.    pPag
10d16 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a  e->iKey = iKey;.
10d17 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74      pPage->pNext
10d18 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73   = pCache->apHas
10d19 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d  h[h];.    pPage-
10d1a 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65  >pCache = pCache
10d1b 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70  ;.    pCache->ap
10d1c 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b  Hash[h] = pPage;
10d1d 0a 20 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a  .  }..fetch_out:
10d1e 0a 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20  .  if( pPage && 
10d1f 69 4b 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61  iKey>pCache->iMa
10d20 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63  xKey ){.    pCac
10d21 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b  he->iMaxKey = iK
10d22 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72  ey;.  }.  if( cr
10d23 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71  eateFlag==1 ) sq
10d24 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
10d25 6c 6c 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65  lloc();.  pcache
10d26 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
10d27 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f   return (pPage ?
10d28 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28   PGHDR1_TO_PAGE(
10d29 70 50 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a  pPage) : 0);.}..
10d2a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
10d2b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
10d2c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70  ite3_pcache.xUnp
10d2d 69 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a  in method..**.**
10d2e 20 4d 61 72 6b 20 61 20 70 61 67 65 20 61 73 20   Mark a page as 
10d2f 75 6e 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62  unpinned (eligib
10d30 6c 65 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e  le for asynchron
10d31 6f 75 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a  ous recycling)..
10d32 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10d33 63 61 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69  cache1Unpin(sqli
10d34 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76  te3_pcache *p, v
10d35 6f 69 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65  oid *pPg, int re
10d36 75 73 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20  useUnlikely){.  
10d37 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20  PCache1 *pCache 
10d38 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a  = (PCache1 *)p;.
10d39 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20    PgHdr1 *pPage 
10d3a 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31  = PAGE_TO_PGHDR1
10d3b 28 70 50 67 29 3b 0a 0a 20 20 70 63 61 63 68 65  (pPg);..  pcache
10d3c 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a  1EnterMutex();..
10d3d 20 20 2f 2a 20 49 74 20 69 73 20 61 6e 20 65 72    /* It is an er
10d3e 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  ror to call this
10d3f 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65   function if the
10d40 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79   page is already
10d41 20 0a 20 20 2a 2a 20 70 61 72 74 20 6f 66 20 74   .  ** part of t
10d42 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69  he global LRU li
10d43 73 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  st..  */.  asser
10d44 74 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72  t( pPage->pLruPr
10d45 65 76 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e  ev==0 && pPage->
10d46 70 4c 72 75 4e 65 78 74 3d 3d 30 20 29 3b 0a 20  pLruNext==0 );. 
10d47 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31   assert( pcache1
10d48 2e 70 4c 72 75 48 65 61 64 21 3d 70 50 61 67 65  .pLruHead!=pPage
10d49 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75   && pcache1.pLru
10d4a 54 61 69 6c 21 3d 70 50 61 67 65 20 29 3b 0a 0a  Tail!=pPage );..
10d4b 20 20 69 66 28 20 72 65 75 73 65 55 6e 6c 69 6b    if( reuseUnlik
10d4c 65 6c 79 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e  ely || pcache1.n
10d4d 43 75 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63  CurrentPage>pcac
10d4e 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 29 7b 0a  he1.nMaxPage ){.
10d4f 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76      pcache1Remov
10d50 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29  eFromHash(pPage)
10d51 3b 0a 20 20 20 20 70 63 61 63 68 65 31 46 72 65  ;.    pcache1Fre
10d52 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
10d53 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41 64  }else{.    /* Ad
10d54 64 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68  d the page to th
10d55 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73  e global LRU lis
10d56 74 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 65  t. Normally, the
10d57 20 70 61 67 65 20 69 73 20 61 64 64 65 64 20 74   page is added t
10d58 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 68 65 61  o.    ** the hea
10d59 64 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28 6c  d of the list (l
10d5a 61 73 74 20 70 61 67 65 20 74 6f 20 62 65 20 72  ast page to be r
10d5b 65 63 79 63 6c 65 64 29 2e 20 48 6f 77 65 76 65  ecycled). Howeve
10d5c 72 2c 20 69 66 20 74 68 65 20 0a 20 20 20 20 2a  r, if the .    *
10d5d 2a 20 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20  * reuseUnlikely 
10d5e 66 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 74  flag passed to t
10d5f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
10d60 74 72 75 65 2c 20 74 68 65 20 70 61 67 65 20 69  true, the page i
10d61 73 20 61 64 64 65 64 0a 20 20 20 20 2a 2a 20 74  s added.    ** t
10d62 6f 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68  o the tail of th
10d63 65 20 6c 69 73 74 20 28 66 69 72 73 74 20 70 61  e list (first pa
10d64 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65  ge to be recycle
10d65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  d)..    */.    i
10d66 66 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48  f( pcache1.pLruH
10d67 65 61 64 20 29 7b 0a 20 20 20 20 20 20 70 63 61  ead ){.      pca
10d68 63 68 65 31 2e 70 4c 72 75 48 65 61 64 2d 3e 70  che1.pLruHead->p
10d69 4c 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 3b  LruPrev = pPage;
10d6a 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c  .      pPage->pL
10d6b 72 75 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31  ruNext = pcache1
10d6c 2e 70 4c 72 75 48 65 61 64 3b 0a 20 20 20 20 20  .pLruHead;.     
10d6d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61   pcache1.pLruHea
10d6e 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d  d = pPage;.    }
10d6f 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 61 63  else{.      pcac
10d70 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70  he1.pLruTail = p
10d71 50 61 67 65 3b 0a 20 20 20 20 20 20 70 63 61 63  Page;.      pcac
10d72 68 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70  he1.pLruHead = p
10d73 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Page;.    }.    
10d74 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61  pCache->nRecycla
10d75 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 70 63  ble++;.  }..  pc
10d76 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28  ache1LeaveMutex(
10d77 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
10d78 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
10d79 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
10d7a 2e 78 52 65 6b 65 79 20 6d 65 74 68 6f 64 2e 20  .xRekey method. 
10d7b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10d7c 70 63 61 63 68 65 31 52 65 6b 65 79 28 0a 20 20  pcache1Rekey(.  
10d7d 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
10d7e 70 2c 0a 20 20 76 6f 69 64 20 2a 70 50 67 2c 0a  p,.  void *pPg,.
10d7f 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
10d80 4f 6c 64 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20  Old,.  unsigned 
10d81 69 6e 74 20 69 4e 65 77 0a 29 7b 0a 20 20 50 43  int iNew.){.  PC
10d82 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20  ache1 *pCache = 
10d83 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20  (PCache1 *)p;.  
10d84 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20  PgHdr1 *pPage = 
10d85 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70  PAGE_TO_PGHDR1(p
10d86 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a  Pg);.  PgHdr1 **
10d87 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  pp;.  unsigned i
10d88 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72 74 28  nt h; .  assert(
10d89 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f   pPage->iKey==iO
10d8a 6c 64 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31  ld );..  pcache1
10d8b 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20  EnterMutex();.. 
10d8c 20 68 20 3d 20 69 4f 6c 64 25 70 43 61 63 68 65   h = iOld%pCache
10d8d 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 70 20 3d 20  ->nHash;.  pp = 
10d8e 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b  &pCache->apHash[
10d8f 68 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 2a 70  h];.  while( (*p
10d90 70 29 21 3d 70 50 61 67 65 20 29 7b 0a 20 20 20  p)!=pPage ){.   
10d91 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e   pp = &(*pp)->pN
10d92 65 78 74 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d  ext;.  }.  *pp =
10d93 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a   pPage->pNext;..
10d94 20 20 68 20 3d 20 69 4e 65 77 25 70 43 61 63 68    h = iNew%pCach
10d95 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 50 61 67  e->nHash;.  pPag
10d96 65 2d 3e 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a  e->iKey = iNew;.
10d97 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d    pPage->pNext =
10d98 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b   pCache->apHash[
10d99 68 5d 3b 0a 20 20 70 43 61 63 68 65 2d 3e 61 70  h];.  pCache->ap
10d9a 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b  Hash[h] = pPage;
10d9b 0a 0a 20 20 69 66 28 20 69 4e 65 77 3e 70 43 61  ..  if( iNew>pCa
10d9c 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a  che->iMaxKey ){.
10d9d 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78      pCache->iMax
10d9e 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 20 7d 0a  Key = iNew;.  }.
10d9f 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
10da0 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  utex();.}../*.**
10da1 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
10da2 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
10da3 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 20  cache.xTruncate 
10da4 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44  method. .**.** D
10da5 69 73 63 61 72 64 20 61 6c 6c 20 75 6e 70 69 6e  iscard all unpin
10da6 6e 65 64 20 70 61 67 65 73 20 69 6e 20 74 68 65  ned pages in the
10da7 20 63 61 63 68 65 20 77 69 74 68 20 61 20 70 61   cache with a pa
10da8 67 65 20 6e 75 6d 62 65 72 20 65 71 75 61 6c 20  ge number equal 
10da9 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72  to.** or greater
10daa 20 74 68 61 6e 20 70 61 72 61 6d 65 74 65 72 20   than parameter 
10dab 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e  iLimit. Any pinn
10dac 65 64 20 70 61 67 65 73 20 77 69 74 68 20 61 20  ed pages with a 
10dad 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 65  page number.** e
10dae 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74  qual to or great
10daf 65 72 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 61  er than iLimit a
10db0 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e  re implicitly un
10db1 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  pinned..*/.stati
10db2 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 54 72  c void pcache1Tr
10db3 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 70  uncate(sqlite3_p
10db4 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e  cache *p, unsign
10db5 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 29 7b 0a  ed int iLimit){.
10db6 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
10db7 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70  e = (PCache1 *)p
10db8 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10db9 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 69  Mutex();.  if( i
10dba 4c 69 6d 69 74 3c 3d 70 43 61 63 68 65 2d 3e 69  Limit<=pCache->i
10dbb 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 63  MaxKey ){.    pc
10dbc 61 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73  ache1TruncateUns
10dbd 61 66 65 28 70 43 61 63 68 65 2c 20 69 4c 69 6d  afe(pCache, iLim
10dbe 69 74 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d  it);.    pCache-
10dbf 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4c 69 6d 69  >iMaxKey = iLimi
10dc0 74 2d 31 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68  t-1;.  }.  pcach
10dc1 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  e1LeaveMutex();.
10dc2 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
10dc3 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
10dc4 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 44  qlite3_pcache.xD
10dc5 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 2e 20 0a  estroy method. .
10dc6 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20  **.** Destroy a 
10dc7 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  cache allocated 
10dc8 75 73 69 6e 67 20 70 63 61 63 68 65 31 43 72 65  using pcache1Cre
10dc9 61 74 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ate()..*/.static
10dca 20 76 6f 69 64 20 70 63 61 63 68 65 31 44 65 73   void pcache1Des
10dcb 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 70 63 61  troy(sqlite3_pca
10dcc 63 68 65 20 2a 70 29 7b 0a 20 20 50 43 61 63 68  che *p){.  PCach
10dcd 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43  e1 *pCache = (PC
10dce 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61  ache1 *)p;.  pca
10dcf 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10dd0 3b 0a 20 20 70 63 61 63 68 65 31 54 72 75 6e 63  ;.  pcache1Trunc
10dd1 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 68 65  ateUnsafe(pCache
10dd2 2c 20 30 29 3b 0a 20 20 70 63 61 63 68 65 31 2e  , 0);.  pcache1.
10dd3 6e 4d 61 78 50 61 67 65 20 2d 3d 20 70 43 61 63  nMaxPage -= pCac
10dd4 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20 70 63 61 63  he->nMax;.  pcac
10dd5 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2d 3d 20  he1.nMinPage -= 
10dd6 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20  pCache->nMin;.  
10dd7 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61  pcache1EnforceMa
10dd8 78 50 61 67 65 28 29 3b 0a 20 20 70 63 61 63 68  xPage();.  pcach
10dd9 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  e1LeaveMutex();.
10dda 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
10ddb 43 61 63 68 65 2d 3e 61 70 48 61 73 68 29 3b 0a  Cache->apHash);.
10ddc 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
10ddd 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Cache);.}../*.**
10dde 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
10ddf 73 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20  s called during 
10de0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 28  initialization (
10de1 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
10de2 7a 65 28 29 29 20 74 6f 0a 2a 2a 20 69 6e 73 74  ze()) to.** inst
10de3 61 6c 6c 20 74 68 65 20 64 65 66 61 75 6c 74 20  all the default 
10de4 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20  pluggable cache 
10de5 6d 6f 64 75 6c 65 2c 20 61 73 73 75 6d 69 6e 67  module, assuming
10de6 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f   the user has no
10de7 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 70 72 6f  t.** already pro
10de8 76 69 64 65 64 20 61 6e 20 61 6c 74 65 72 6e 61  vided an alterna
10de9 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tive..*/.SQLITE_
10dea 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
10deb 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66  ite3PCacheSetDef
10dec 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74  ault(void){.  st
10ded 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61  atic sqlite3_pca
10dee 63 68 65 5f 6d 65 74 68 6f 64 73 20 64 65 66 61  che_methods defa
10def 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20  ultMethods = {. 
10df0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
10df1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
10df2 41 72 67 20 2a 2f 0a 20 20 20 20 70 63 61 63 68  Arg */.    pcach
10df3 65 31 49 6e 69 74 2c 20 20 20 20 20 20 20 20 20  e1Init,         
10df4 20 20 20 20 2f 2a 20 78 49 6e 69 74 20 2a 2f 0a      /* xInit */.
10df5 20 20 20 20 70 63 61 63 68 65 31 53 68 75 74 64      pcache1Shutd
10df6 6f 77 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  own,         /* 
10df7 78 53 68 75 74 64 6f 77 6e 20 2a 2f 0a 20 20 20  xShutdown */.   
10df8 20 70 63 61 63 68 65 31 43 72 65 61 74 65 2c 20   pcache1Create, 
10df9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72            /* xCr
10dfa 65 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63  eate */.    pcac
10dfb 68 65 31 43 61 63 68 65 73 69 7a 65 2c 20 20 20  he1Cachesize,   
10dfc 20 20 20 20 20 2f 2a 20 78 43 61 63 68 65 73 69       /* xCachesi
10dfd 7a 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  ze */.    pcache
10dfe 31 50 61 67 65 63 6f 75 6e 74 2c 20 20 20 20 20  1Pagecount,     
10dff 20 20 20 2f 2a 20 78 50 61 67 65 63 6f 75 6e 74     /* xPagecount
10e00 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 46   */.    pcache1F
10e01 65 74 63 68 2c 20 20 20 20 20 20 20 20 20 20 20  etch,           
10e02 20 2f 2a 20 78 46 65 74 63 68 20 2a 2f 0a 20 20   /* xFetch */.  
10e03 20 20 70 63 61 63 68 65 31 55 6e 70 69 6e 2c 20    pcache1Unpin, 
10e04 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55             /* xU
10e05 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63  npin */.    pcac
10e06 68 65 31 52 65 6b 65 79 2c 20 20 20 20 20 20 20  he1Rekey,       
10e07 20 20 20 20 20 2f 2a 20 78 52 65 6b 65 79 20 2a       /* xRekey *
10e08 2f 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75  /.    pcache1Tru
10e09 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 2f  ncate,         /
10e0a 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a 20  * xTruncate */. 
10e0b 20 20 20 70 63 61 63 68 65 31 44 65 73 74 72 6f     pcache1Destro
10e0c 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  y           /* x
10e0d 44 65 73 74 72 6f 79 20 2a 2f 0a 20 20 7d 3b 0a  Destroy */.  };.
10e0e 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67    sqlite3_config
10e0f 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  (SQLITE_CONFIG_P
10e10 43 41 43 48 45 2c 20 26 64 65 66 61 75 6c 74 4d  CACHE, &defaultM
10e11 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 69 66 64  ethods);.}..#ifd
10e12 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
10e13 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
10e14 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  NT./*.** This fu
10e15 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
10e16 20 74 6f 20 66 72 65 65 20 73 75 70 65 72 66 6c   to free superfl
10e17 75 6f 75 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79  uous dynamically
10e18 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
10e19 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 65  y.** held by the
10e1a 20 70 61 67 65 72 20 73 79 73 74 65 6d 2e 20 4d   pager system. M
10e1b 65 6d 6f 72 79 20 69 6e 20 75 73 65 20 62 79 20  emory in use by 
10e1c 61 6e 79 20 53 51 4c 69 74 65 20 70 61 67 65 72  any SQLite pager
10e1d 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79   allocated.** by
10e1e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72   the current thr
10e1f 65 61 64 20 6d 61 79 20 62 65 20 73 71 6c 69 74  ead may be sqlit
10e20 65 33 5f 66 72 65 65 28 29 65 64 2e 0a 2a 2a 0a  e3_free()ed..**.
10e21 2a 2a 20 6e 52 65 71 20 69 73 20 74 68 65 20 6e  ** nReq is the n
10e22 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
10e23 66 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65  f memory require
10e24 64 2e 20 4f 6e 63 65 20 74 68 69 73 20 6d 75 63  d. Once this muc
10e25 68 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 72 65  h has.** been re
10e26 6c 65 61 73 65 64 2c 20 74 68 65 20 66 75 6e 63  leased, the func
10e27 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20 54 68  tion returns. Th
10e28 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
10e29 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  s the total numb
10e2a 65 72 20 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20  er .** of bytes 
10e2b 6f 66 20 6d 65 6d 6f 72 79 20 72 65 6c 65 61 73  of memory releas
10e2c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
10e2d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
10e2e 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65  3PcacheReleaseMe
10e2f 6d 6f 72 79 28 69 6e 74 20 6e 52 65 71 29 7b 0a  mory(int nReq){.
10e30 20 20 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b    int nFree = 0;
10e31 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70  .  if( pcache1.p
10e32 53 74 61 72 74 3d 3d 30 20 29 7b 0a 20 20 20 20  Start==0 ){.    
10e33 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 20 20 70  PgHdr1 *p;.    p
10e34 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10e35 28 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28  ();.    while( (
10e36 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 72 65 65 3c  nReq<0 || nFree<
10e37 6e 52 65 71 29 20 26 26 20 28 70 3d 70 63 61 63  nReq) && (p=pcac
10e38 68 65 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b  he1.pLruTail) ){
10e39 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20  .      nFree += 
10e3a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
10e3b 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63  e(p);.      pcac
10e3c 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20  he1PinPage(p);. 
10e3d 20 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f       pcache1Remo
10e3e 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20  veFromHash(p);. 
10e3f 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65       pcache1Free
10e40 50 61 67 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20  Page(p);.    }. 
10e41 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d     pcache1LeaveM
10e42 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65  utex();.  }.  re
10e43 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65  turn nFree;.}.#e
10e44 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45  ndif /* SQLITE_E
10e45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
10e46 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 23 69 66 64  AGEMENT */..#ifd
10e47 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f  ef SQLITE_TEST./
10e48 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
10e49 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74 65  on is used by te
10e4a 73 74 20 70 72 6f 63 65 64 75 72 65 73 20 74 6f  st procedures to
10e4b 20 69 6e 73 70 65 63 74 20 74 68 65 20 69 6e 74   inspect the int
10e4c 65 72 6e 61 6c 20 73 74 61 74 65 0a 2a 2a 20 6f  ernal state.** o
10e4d 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 61 63  f the global cac
10e4e 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  he..*/.SQLITE_PR
10e4f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
10e50 65 33 50 63 61 63 68 65 53 74 61 74 73 28 0a 20  e3PcacheStats(. 
10e51 20 69 6e 74 20 2a 70 6e 43 75 72 72 65 6e 74 2c   int *pnCurrent,
10e52 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f        /* OUT: To
10e53 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
10e54 67 65 73 20 63 61 63 68 65 64 20 2a 2f 0a 20 20  ges cached */.  
10e55 69 6e 74 20 2a 70 6e 4d 61 78 2c 20 20 20 20 20  int *pnMax,     
10e56 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 47 6c 6f       /* OUT: Glo
10e57 62 61 6c 20 6d 61 78 69 6d 75 6d 20 63 61 63 68  bal maximum cach
10e58 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20  e size */.  int 
10e59 2a 70 6e 4d 69 6e 2c 20 20 20 20 20 20 20 20 20  *pnMin,         
10e5a 20 2f 2a 20 4f 55 54 3a 20 53 75 6d 20 6f 66 20   /* OUT: Sum of 
10e5b 50 43 61 63 68 65 31 2e 6e 4d 69 6e 20 66 6f 72  PCache1.nMin for
10e5c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65   purgeable cache
10e5d 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 65  s */.  int *pnRe
10e5e 63 79 63 6c 61 62 6c 65 20 20 20 20 2f 2a 20 4f  cyclable    /* O
10e5f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72  UT: Total number
10e60 20 6f 66 20 70 61 67 65 73 20 61 76 61 69 6c 61   of pages availa
10e61 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e  ble for recyclin
10e62 67 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 31  g */.){.  PgHdr1
10e63 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 52 65 63 79   *p;.  int nRecy
10e64 63 6c 61 62 6c 65 20 3d 20 30 3b 0a 20 20 66 6f  clable = 0;.  fo
10e65 72 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75  r(p=pcache1.pLru
10e66 48 65 61 64 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c  Head; p; p=p->pL
10e67 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 6e 52 65  ruNext){.    nRe
10e68 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a  cyclable++;.  }.
10e69 20 20 2a 70 6e 43 75 72 72 65 6e 74 20 3d 20 70    *pnCurrent = p
10e6a 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50  cache1.nCurrentP
10e6b 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 78 20 3d 20  age;.  *pnMax = 
10e6c 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65  pcache1.nMaxPage
10e6d 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d 20 70 63 61  ;.  *pnMin = pca
10e6e 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 3b 0a 20  che1.nMinPage;. 
10e6f 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d   *pnRecyclable =
10e70 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a 7d 0a   nRecyclable;.}.
10e71 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
10e72 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
10e73 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a  cache1.c *******
10e74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10e75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10e76 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
10e77 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
10e78 6c 65 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a 2a  le rowset.c ****
10e79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10e7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10e7b 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
10e7c 30 38 20 44 65 63 65 6d 62 65 72 20 33 0a 2a 2a  08 December 3.**
10e7d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
10e7e 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
10e7f 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
10e80 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
10e81 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
10e82 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
10e83 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
10e84 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
10e85 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
10e86 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
10e87 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
10e88 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
10e89 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
10e8a 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
10e8b 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
10e8c 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
10e8d 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
10e8e 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
10e8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10e91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10e92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
10e93 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69  ** This module i
10e94 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a  mplements an obj
10e95 65 63 74 20 77 65 20 63 61 6c 6c 20 61 20 22 52  ect we call a "R
10e96 6f 77 20 53 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 54  ow Set"..**.** T
10e97 68 65 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74  he RowSet object
10e98 20 69 73 20 61 20 62 61 67 20 6f 66 20 72 6f 77   is a bag of row
10e99 69 64 73 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20  ids.  Rowids.** 
10e9a 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74  are inserted int
10e9b 6f 20 74 68 65 20 62 61 67 20 69 6e 20 61 6e 20  o the bag in an 
10e9c 61 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e  arbitrary order.
10e9d 20 20 54 68 65 6e 20 74 68 65 79 20 61 72 65 0a    Then they are.
10e9e 2a 2a 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 74  ** pulled from t
10e9f 68 65 20 62 61 67 20 69 6e 20 73 6f 72 74 65 64  he bag in sorted
10ea0 20 6f 72 64 65 72 2e 20 20 52 6f 77 69 64 73 20   order.  Rowids 
10ea1 6f 6e 6c 79 20 61 70 70 65 61 72 20 69 6e 20 74  only appear in t
10ea2 68 65 0a 2a 2a 20 62 61 67 20 6f 6e 63 65 2e 20  he.** bag once. 
10ea3 20 49 66 20 74 68 65 20 73 61 6d 65 20 72 6f 77   If the same row
10ea4 69 64 20 69 73 20 69 6e 73 65 72 74 65 64 20 6d  id is inserted m
10ea5 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2c 20 74  ultiple times, t
10ea6 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 6e 64  he.** second and
10ea7 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65   subsequent inse
10ea8 72 74 73 20 6d 61 6b 65 20 6e 6f 20 64 69 66 66  rts make no diff
10ea9 65 72 65 6e 63 65 20 6f 6e 20 74 68 65 20 6f 75  erence on the ou
10eaa 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tput..**.** This
10eab 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
10eac 61 63 63 75 6d 75 6c 61 74 65 73 20 72 6f 77 69  accumulates rowi
10ead 64 73 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c  ds in a linked l
10eae 69 73 74 2e 20 20 46 6f 72 0a 2a 2a 20 6f 75 74  ist.  For.** out
10eaf 70 75 74 2c 20 69 74 20 66 69 72 73 74 20 73 6f  put, it first so
10eb0 72 74 73 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c  rts the linked l
10eb1 69 73 74 20 28 72 65 6d 6f 76 69 6e 67 20 64 75  ist (removing du
10eb2 70 6c 69 63 61 74 65 73 20 64 75 72 69 6e 67 0a  plicates during.
10eb3 2a 2a 20 74 68 65 20 73 6f 72 74 29 20 74 68 65  ** the sort) the
10eb4 6e 20 72 65 74 75 72 6e 73 20 65 6c 65 6d 65 6e  n returns elemen
10eb5 74 73 20 6f 6e 65 20 62 79 20 6f 6e 65 20 62 79  ts one by one by
10eb6 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 6c 69 73   walking the lis
10eb7 74 2e 0a 2a 2a 0a 2a 2a 20 42 69 67 20 63 68 75  t..**.** Big chu
10eb8 6e 6b 73 20 6f 66 20 72 6f 77 69 64 2f 6e 65 78  nks of rowid/nex
10eb9 74 2d 70 74 72 20 70 61 69 72 73 20 61 72 65 20  t-ptr pairs are 
10eba 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61 20 74  allocated at a t
10ebb 69 6d 65 2c 20 74 6f 0a 2a 2a 20 72 65 64 75 63  ime, to.** reduc
10ebc 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 6f 76 65  e the malloc ove
10ebd 72 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  rhead..**.** $Id
10ebe 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31 2e 33  : rowset.c,v 1.3
10ebf 20 32 30 30 39 2f 30 31 2f 31 33 20 32 30 3a 31   2009/01/13 20:1
10ec0 34 3a 31 36 20 64 72 68 20 45 78 70 20 24 0a 2a  4:16 drh Exp $.*
10ec1 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d  /../*.** The num
10ec2 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20 65 6e  ber of rowset en
10ec3 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f 63 61  tries per alloca
10ec4 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23  tion chunk..*/.#
10ec5 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 45 4e  define ROWSET_EN
10ec6 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20 20 36  TRY_PER_CHUNK  6
10ec7 33 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e  3../*.** Each en
10ec8 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20  try in a RowSet 
10ec9 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
10eca 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  f the following.
10ecb 2a 2a 20 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f  ** structure:.*/
10ecc 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e  .struct RowSetEn
10ecd 74 72 79 20 7b 20 20 20 20 20 20 20 20 20 20 20  try {           
10ece 20 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 20   .  i64 v;      
10ecf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ed0 20 20 2f 2a 20 52 4f 57 49 44 20 76 61 6c 75 65    /* ROWID value
10ed1 20 66 6f 72 20 74 68 69 73 20 65 6e 74 72 79 20   for this entry 
10ed2 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
10ed3 65 74 45 6e 74 72 79 20 2a 70 4e 65 78 74 3b 20  etEntry *pNext; 
10ed4 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79     /* Next entry
10ed5 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c   on a list of al
10ed6 6c 20 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a  l entries */.};.
10ed7 0a 2f 2a 0a 2a 2a 20 49 6e 64 65 78 20 65 6e 74  ./*.** Index ent
10ed8 72 69 65 73 20 61 72 65 20 61 6c 6c 6f 63 61 74  ries are allocat
10ed9 65 64 20 69 6e 20 6c 61 72 67 65 20 63 68 75 6e  ed in large chun
10eda 6b 73 20 28 69 6e 73 74 61 6e 63 65 73 20 6f 66  ks (instances of
10edb 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e   the.** followin
10edc 67 20 73 74 72 75 63 74 75 72 65 29 20 74 6f 20  g structure) to 
10edd 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20 61 6c  reduce memory al
10ede 6c 6f 63 61 74 69 6f 6e 20 6f 76 65 72 68 65 61  location overhea
10edf 64 2e 20 20 54 68 65 0a 2a 2a 20 63 68 75 6e 6b  d.  The.** chunk
10ee0 73 20 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 20  s are kept on a 
10ee1 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 6f 20 74  linked list so t
10ee2 68 61 74 20 74 68 65 79 20 63 61 6e 20 62 65 20  hat they can be 
10ee3 64 65 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 77  deallocated.** w
10ee4 68 65 6e 20 74 68 65 20 52 6f 77 53 65 74 20 69  hen the RowSet i
10ee5 73 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a  s destroyed..*/.
10ee6 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75  struct RowSetChu
10ee7 6e 6b 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f  nk {.  struct Ro
10ee8 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65 78 74  wSetChunk *pNext
10ee9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
10eea 20 4e 65 78 74 20 63 68 75 6e 6b 20 6f 6e 20 6c   Next chunk on l
10eeb 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20  ist of them all 
10eec 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
10eed 65 74 45 6e 74 72 79 20 61 45 6e 74 72 79 5b 52  etEntry aEntry[R
10eee 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f  OWSET_ENTRY_PER_
10eef 43 48 55 4e 4b 5d 3b 20 2f 2a 20 41 6c 6c 6f 63  CHUNK]; /* Alloc
10ef0 61 74 65 64 20 65 6e 74 72 69 65 73 20 2a 2f 0a  ated entries */.
10ef1 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 52 6f 77 53  };../*.** A RowS
10ef2 65 74 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63  et in an instanc
10ef3 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
10ef4 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
10ef5 0a 2a 2a 20 41 20 74 79 70 65 64 65 66 20 6f 66  .** A typedef of
10ef6 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
10ef7 69 66 20 66 6f 75 6e 64 20 69 6e 20 73 71 6c 69  if found in sqli
10ef8 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 73 74 72 75  teInt.h..*/.stru
10ef9 63 74 20 52 6f 77 53 65 74 20 7b 0a 20 20 73 74  ct RowSet {.  st
10efa 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b  ruct RowSetChunk
10efb 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 2f 2a 20   *pChunk;    /* 
10efc 4c 69 73 74 20 6f 66 20 61 6c 6c 20 63 68 75 6e  List of all chun
10efd 6b 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  k allocations */
10efe 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
10eff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10f00 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
10f01 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
10f02 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
10f03 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b 20 20 20  ntry *pEntry;   
10f04 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65 6e 74 72   /* List of entr
10f05 69 65 73 20 69 6e 20 74 68 65 20 72 6f 77 73 65  ies in the rowse
10f06 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f  t */.  struct Ro
10f07 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61 73 74  wSetEntry *pLast
10f08 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 65 6e  ;     /* Last en
10f09 74 72 79 20 6f 6e 20 74 68 65 20 70 45 6e 74 72  try on the pEntr
10f0a 79 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75  y list */.  stru
10f0b 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
10f0c 70 46 72 65 73 68 3b 20 20 20 20 2f 2a 20 53 6f  pFresh;    /* So
10f0d 75 72 63 65 20 6f 66 20 6e 65 77 20 65 6e 74 72  urce of new entr
10f0e 79 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 75  y objects */.  u
10f0f 31 36 20 6e 46 72 65 73 68 3b 20 20 20 20 20 20  16 nFresh;      
10f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10f11 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63   Number of objec
10f12 74 73 20 6f 6e 20 70 46 72 65 73 68 20 2a 2f 0a  ts on pFresh */.
10f13 20 20 75 38 20 69 73 53 6f 72 74 65 64 3b 20 20    u8 isSorted;  
10f14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10f15 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 6e 74   /* True if cont
10f16 65 6e 74 20 69 73 20 73 6f 72 74 65 64 20 2a 2f  ent is sorted */
10f17 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20  .};../*.** Turn 
10f18 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f  bulk memory into
10f19 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74   a RowSet object
10f1a 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65  .  N bytes of me
10f1b 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69  mory.** are avai
10f1c 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e  lable at pSpace.
10f1d 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72    The db pointer
10f1e 20 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65   is used as a me
10f1f 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20  mory context.** 
10f20 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65  for any subseque
10f21 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74  nt allocations t
10f22 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75  hat need to occu
10f23 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  r..** Return a p
10f24 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
10f25 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e  w RowSet object.
10f26 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62  .**.** It must b
10f27 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20  e the case that 
10f28 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20  N is sufficient 
10f29 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74  to make a Rowset
10f2a 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20  .  If not.** an 
10f2b 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
10f2c 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49  occurs..** .** I
10f2d 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68  f N is larger th
10f2e 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20  an the minimum, 
10f2f 75 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20  use the surplus 
10f30 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a  as an initial.**
10f31 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65   allocation of e
10f32 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65  ntries available
10f33 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a   to be filled..*
10f34 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10f35 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33   RowSet *sqlite3
10f36 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74  RowSetInit(sqlit
10f37 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53  e3 *db, void *pS
10f38 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69  pace, unsigned i
10f39 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20  nt N){.  RowSet 
10f3a 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20  *p;.  assert( N 
10f3b 3e 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b  >= sizeof(*p) );
10f3c 0a 20 20 70 20 3d 20 70 53 70 61 63 65 3b 0a 20  .  p = pSpace;. 
10f3d 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a   p->pChunk = 0;.
10f3e 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20    p->db = db;.  
10f3f 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20  p->pEntry = 0;. 
10f40 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20   p->pLast = 0;. 
10f41 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 28 73 74   p->pFresh = (st
10f42 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
10f43 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d 3e 6e 46  *)&p[1];.  p->nF
10f44 72 65 73 68 20 3d 20 28 75 31 36 29 28 28 4e 20  resh = (u16)((N 
10f45 2d 20 73 69 7a 65 6f 66 28 2a 70 29 29 2f 73 69  - sizeof(*p))/si
10f46 7a 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53  zeof(struct RowS
10f47 65 74 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e  etEntry));.  p->
10f48 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20  isSorted = 1;.  
10f49 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
10f4a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c  ** Deallocate al
10f4b 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20  l chunks from a 
10f4c 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54  RowSet..*/.SQLIT
10f4d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
10f4e 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61  qlite3RowSetClea
10f4f 72 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20  r(RowSet *p){.  
10f50 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75  struct RowSetChu
10f51 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65  nk *pChunk, *pNe
10f52 78 74 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70  xtChunk;.  for(p
10f53 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b  Chunk=p->pChunk;
10f54 20 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20   pChunk; pChunk 
10f55 3d 20 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20  = pNextChunk){. 
10f56 20 20 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20     pNextChunk = 
10f57 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20  pChunk->pNext;. 
10f58 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
10f59 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b  (p->db, pChunk);
10f5a 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b  .  }.  p->pChunk
10f5b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73   = 0;.  p->nFres
10f5c 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74  h = 0;.  p->pEnt
10f5d 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61  ry = 0;.  p->pLa
10f5e 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53  st = 0;.  p->isS
10f5f 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  orted = 1;.}../*
10f60 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77  .** Insert a new
10f61 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f   value into a Ro
10f62 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  wSet..**.** The 
10f63 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
10f64 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  g of the databas
10f65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
10f66 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f  set if a.** memo
10f67 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
10f68 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ils..*/.SQLITE_P
10f69 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
10f6a 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28  te3RowSetInsert(
10f6b 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 72  RowSet *p, i64 r
10f6c 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20  owid){.  struct 
10f6d 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e  RowSetEntry *pEn
10f6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52 6f  try;.  struct Ro
10f6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61 73 74  wSetEntry *pLast
10f70 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
10f71 65 74 75 72 6e 3b 20 20 2f 2a 20 4d 75 73 74 20  eturn;  /* Must 
10f72 68 61 76 65 20 62 65 65 6e 20 61 20 6d 61 6c 6c  have been a mall
10f73 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20  oc failure */.  
10f74 69 66 28 20 70 2d 3e 6e 46 72 65 73 68 3d 3d 30  if( p->nFresh==0
10f75 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52   ){.    struct R
10f76 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65 77  owSetChunk *pNew
10f77 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c  ;.    pNew = sql
10f78 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
10f79 70 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  p->db, sizeof(*p
10f7a 4e 65 77 29 29 3b 0a 20 20 20 20 69 66 28 20 70  New));.    if( p
10f7b 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  New==0 ){.      
10f7c 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
10f7d 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20    pNew->pNext = 
10f7e 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70  p->pChunk;.    p
10f7f 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b  ->pChunk = pNew;
10f80 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20 3d  .    p->pFresh =
10f81 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20   pNew->aEntry;. 
10f82 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52     p->nFresh = R
10f83 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f  OWSET_ENTRY_PER_
10f84 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e  CHUNK;.  }.  pEn
10f85 74 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b  try = p->pFresh+
10f86 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d  +;.  p->nFresh--
10f87 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20  ;.  pEntry->v = 
10f88 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d  rowid;.  pEntry-
10f89 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 4c  >pNext = 0;.  pL
10f8a 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a  ast = p->pLast;.
10f8b 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b 0a 20    if( pLast ){. 
10f8c 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f 72 74     if( p->isSort
10f8d 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c 61  ed && rowid<=pLa
10f8e 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70  st->v ){.      p
10f8f 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30 3b 0a  ->isSorted = 0;.
10f90 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 2d      }.    pLast-
10f91 3e 70 4e 65 78 74 20 3d 20 70 45 6e 74 72 79 3b  >pNext = pEntry;
10f92 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
10f93 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 72 79 3d  sert( p->pEntry=
10f94 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 70 45 6e  =0 );.    p->pEn
10f95 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20  try = pEntry;.  
10f96 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 70  }.  p->pLast = p
10f97 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Entry;.}../*.** 
10f98 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20  Merge two lists 
10f99 6f 66 20 52 6f 77 53 65 74 20 65 6e 74 72 69 65  of RowSet entrie
10f9a 73 2e 20 20 52 65 6d 6f 76 65 20 64 75 70 6c 69  s.  Remove dupli
10f9b 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cates..**.** The
10f9c 20 69 6e 70 75 74 20 6c 69 73 74 73 20 61 72 65   input lists are
10f9d 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 69   assumed to be i
10f9e 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a  n sorted order..
10f9f 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
10fa0 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 62 6f   RowSetEntry *bo
10fa1 6f 6c 69 64 78 4d 65 72 67 65 28 0a 20 20 73 74  olidxMerge(.  st
10fa2 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
10fa3 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69 72 73   *pA,    /* Firs
10fa4 74 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f  t sorted list to
10fa5 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20   be merged */.  
10fa6 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
10fa7 72 79 20 2a 70 42 20 20 20 20 20 2f 2a 20 53 65  ry *pB     /* Se
10fa8 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69 73 74  cond sorted list
10fa9 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f   to be merged */
10faa 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77  .){.  struct Row
10fab 53 65 74 45 6e 74 72 79 20 68 65 61 64 3b 0a 20  SetEntry head;. 
10fac 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
10fad 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20 20 70  try *pTail;..  p
10fae 54 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a 20 20  Tail = &head;.  
10faf 77 68 69 6c 65 28 20 70 41 20 26 26 20 70 42 20  while( pA && pB 
10fb0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
10fb1 41 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70  A->pNext==0 || p
10fb2 41 2d 3e 76 3c 3d 70 41 2d 3e 70 4e 65 78 74 2d  A->v<=pA->pNext-
10fb3 3e 76 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >v );.    assert
10fb4 28 20 70 42 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c  ( pB->pNext==0 |
10fb5 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 4e 65  | pB->v<=pB->pNe
10fb6 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20 69 66 28  xt->v );.    if(
10fb7 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 29 7b 0a   pA->v<pB->v ){.
10fb8 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 4e 65        pTail->pNe
10fb9 78 74 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 70  xt = pA;.      p
10fba 41 20 3d 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20  A = pA->pNext;. 
10fbb 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61       pTail = pTa
10fbc 69 6c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  il->pNext;.    }
10fbd 65 6c 73 65 20 69 66 28 20 70 42 2d 3e 76 3c 70  else if( pB->v<p
10fbe 41 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54  A->v ){.      pT
10fbf 61 69 6c 2d 3e 70 4e 65 78 74 20 3d 20 70 42 3b  ail->pNext = pB;
10fc0 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e  .      pB = pB->
10fc1 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 54 61  pNext;.      pTa
10fc2 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 4e 65 78  il = pTail->pNex
10fc3 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  t;.    }else{.  
10fc4 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 4e 65      pA = pA->pNe
10fc5 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  xt;.    }.  }.  
10fc6 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 61 73  if( pA ){.    as
10fc7 73 65 72 74 28 20 70 41 2d 3e 70 4e 65 78 74 3d  sert( pA->pNext=
10fc8 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d  =0 || pA->v<=pA-
10fc9 3e 70 4e 65 78 74 2d 3e 76 20 29 3b 0a 20 20 20  >pNext->v );.   
10fca 20 70 54 61 69 6c 2d 3e 70 4e 65 78 74 20 3d 20   pTail->pNext = 
10fcb 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  pA;.  }else{.   
10fcc 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20 7c   assert( pB==0 |
10fcd 7c 20 70 42 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c  | pB->pNext==0 |
10fce 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 4e 65  | pB->v<=pB->pNe
10fcf 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61  xt->v );.    pTa
10fd0 69 6c 2d 3e 70 4e 65 78 74 20 3d 20 70 42 3b 0a  il->pNext = pB;.
10fd1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 65 61    }.  return hea
10fd2 64 2e 70 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  d.pNext;.}../*.*
10fd3 2a 20 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d 65  * Sort all eleme
10fd4 6e 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65  nts of the RowSe
10fd5 74 20 69 6e 74 6f 20 61 73 63 65 6e 64 69 6e 67  t into ascending
10fd6 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73 74 61 74   order..*/ .stat
10fd7 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  ic void sqlite3R
10fd8 6f 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 74  owSetSort(RowSet
10fd9 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64   *p){.  unsigned
10fda 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
10fdb 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45   RowSetEntry *pE
10fdc 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52  ntry;.  struct R
10fdd 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75 63  owSetEntry *aBuc
10fde 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65  ket[40];..  asse
10fdf 72 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d  rt( p->isSorted=
10fe0 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61  =0 );.  memset(a
10fe1 42 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f  Bucket, 0, sizeo
10fe2 66 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20 77  f(aBucket));.  w
10fe3 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79 20  hile( p->pEntry 
10fe4 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20  ){.    pEntry = 
10fe5 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20 70  p->pEntry;.    p
10fe6 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72  ->pEntry = pEntr
10fe7 79 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70 45  y->pNext;.    pE
10fe8 6e 74 72 79 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  ntry->pNext = 0;
10fe9 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 42  .    for(i=0; aB
10fea 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  ucket[i]; i++){.
10feb 20 20 20 20 20 20 70 45 6e 74 72 79 20 3d 20 62        pEntry = b
10fec 6f 6f 6c 69 64 78 4d 65 72 67 65 28 61 42 75 63  oolidxMerge(aBuc
10fed 6b 65 74 5b 69 5d 2c 70 45 6e 74 72 79 29 3b 0a  ket[i],pEntry);.
10fee 20 20 20 20 20 20 61 42 75 63 6b 65 74 5b 69 5d        aBucket[i]
10fef 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
10ff0 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70 45 6e  aBucket[i] = pEn
10ff1 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72  try;.  }.  pEntr
10ff2 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  y = 0;.  for(i=0
10ff3 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 63 6b  ; i<sizeof(aBuck
10ff4 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 63 6b  et)/sizeof(aBuck
10ff5 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20  et[0]); i++){.  
10ff6 20 20 70 45 6e 74 72 79 20 3d 20 62 6f 6f 6c 69    pEntry = booli
10ff7 64 78 4d 65 72 67 65 28 70 45 6e 74 72 79 2c 61  dxMerge(pEntry,a
10ff8 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a  Bucket[i]);.  }.
10ff9 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45    p->pEntry = pE
10ffa 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61 73 74  ntry;.  p->pLast
10ffb 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72   = 0;.  p->isSor
10ffc 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  ted = 1;.}../*.*
10ffd 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e 65  * Extract the ne
10ffe 78 74 20 28 73 6d 61 6c 6c 65 73 74 29 20 65 6c  xt (smallest) el
10fff 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52  ement from the R
11000 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20  owSet..** Write 
11001 74 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f  the element into
11002 20 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72   *pRowid.  Retur
11003 6e 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  n 1 on success. 
11004 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20   Return.** 0 if 
11005 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c  the RowSet is al
11006 72 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2f 0a  ready empty..*/.
11007 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
11008 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  nt sqlite3RowSet
11009 4e 65 78 74 28 52 6f 77 53 65 74 20 2a 70 2c 20  Next(RowSet *p, 
1100a 69 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20  i64 *pRowid){.  
1100b 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 74 65 64  if( !p->isSorted
1100c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52   ){.    sqlite3R
1100d 6f 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20 20  owSetSort(p);.  
1100e 7d 0a 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 72  }.  if( p->pEntr
1100f 79 20 29 7b 0a 20 20 20 20 2a 70 52 6f 77 69 64  y ){.    *pRowid
11010 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 76 3b   = p->pEntry->v;
11011 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d  .    p->pEntry =
11012 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 70 4e 65 78   p->pEntry->pNex
11013 74 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 45  t;.    if( p->pE
11014 6e 74 72 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ntry==0 ){.     
11015 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c   sqlite3RowSetCl
11016 65 61 72 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20  ear(p);.    }.  
11017 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65    return 1;.  }e
11018 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
11019 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  0;.  }.}../*****
1101a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1101b 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a   rowset.c ******
1101c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1101d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1101e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1101f 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
11020 66 69 6c 65 20 70 61 67 65 72 2e 63 20 2a 2a 2a  file pager.c ***
11021 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11022 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11023 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
11024 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
11025 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
11026 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
11027 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
11028 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
11029 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1102a 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1102b 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1102c 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1102d 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1102e 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1102f 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
11030 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
11031 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
11032 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
11033 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
11034 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
11035 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
11036 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
11037 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11038 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11039 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1103a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1103b 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
1103c 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1103d 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  f the page cache
1103e 20 73 75 62 73 79 73 74 65 6d 20 6f 72 20 22 70   subsystem or "p
1103f 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  ager"..** .** Th
11040 65 20 70 61 67 65 72 20 69 73 20 75 73 65 64 20  e pager is used 
11041 74 6f 20 61 63 63 65 73 73 20 61 20 64 61 74 61  to access a data
11042 62 61 73 65 20 64 69 73 6b 20 66 69 6c 65 2e 20  base disk file. 
11043 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a   It implements.*
11044 2a 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20  * atomic commit 
11045 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 72  and rollback thr
11046 6f 75 67 68 20 74 68 65 20 75 73 65 20 6f 66 20  ough the use of 
11047 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  a journal file t
11048 68 61 74 0a 2a 2a 20 69 73 20 73 65 70 61 72 61  hat.** is separa
11049 74 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  te from the data
1104a 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
1104b 70 61 67 65 72 20 61 6c 73 6f 20 69 6d 70 6c 65  pager also imple
1104c 6d 65 6e 74 73 20 66 69 6c 65 0a 2a 2a 20 6c 6f  ments file.** lo
1104d 63 6b 69 6e 67 20 74 6f 20 70 72 65 76 65 6e 74  cking to prevent
1104e 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 66   two processes f
1104f 72 6f 6d 20 77 72 69 74 69 6e 67 20 74 68 65 20  rom writing the 
11050 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  same database.**
11051 20 66 69 6c 65 20 73 69 6d 75 6c 74 61 6e 65 6f   file simultaneo
11052 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 70 72 6f  usly, or one pro
11053 63 65 73 73 20 66 72 6f 6d 20 72 65 61 64 69 6e  cess from readin
11054 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  g the database w
11055 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68 65 72 20  hile.** another 
11056 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a  is writing..**.*
11057 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65  * @(#) $Id: page
11058 72 2e 63 2c 76 20 31 2e 35 35 31 20 32 30 30 39  r.c,v 1.551 2009
11059 2f 30 31 2f 31 34 20 32 33 3a 30 33 3a 34 31 20  /01/14 23:03:41 
1105a 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  drh Exp $.*/.#if
1105b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1105c 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d  _DISKIO../*.** M
1105d 61 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c  acros for troubl
1105e 65 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d  eshooting.  Norm
1105f 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a  ally turned off.
11060 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c  */.#if 0.int sql
11061 69 74 65 33 50 61 67 65 72 54 72 61 63 65 3d 31  ite3PagerTrace=1
11062 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e  ;  /* True to en
11063 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a  able tracing */.
11064 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44  #define sqlite3D
11065 65 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74  ebugPrintf print
11066 66 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54  f.#define PAGERT
11067 52 41 43 45 28 58 29 20 20 20 20 20 69 66 28 20  RACE(X)     if( 
11068 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63  sqlite3PagerTrac
11069 65 20 29 7b 20 73 71 6c 69 74 65 33 44 65 62 75  e ){ sqlite3Debu
1106a 67 50 72 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c  gPrintf X; }.#el
1106b 73 65 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  se.#define PAGER
1106c 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a  TRACE(X).#endif.
1106d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
1106e 77 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73 20  wing two macros 
1106f 61 72 65 20 75 73 65 64 20 77 69 74 68 69 6e 20  are used within 
11070 74 68 65 20 50 41 47 45 52 54 52 41 43 45 28 29  the PAGERTRACE()
11071 20 6d 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a   macros above.**
11072 20 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69   to print out fi
11073 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20  le-descriptors. 
11074 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29  .**.** PAGERID()
11075 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72   takes a pointer
11076 20 74 6f 20 61 20 50 61 67 65 72 20 73 74 72 75   to a Pager stru
11077 63 74 20 61 73 20 69 74 73 20 61 72 67 75 6d 65  ct as its argume
11078 6e 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63  nt. The.** assoc
11079 69 61 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72  iated file-descr
1107a 69 70 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65  iptor is returne
1107b 64 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28  d. FILEHANDLEID(
1107c 29 20 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74  ) takes an sqlit
1107d 65 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63  e3_file.** struc
1107e 74 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e  t as its argumen
1107f 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  t..*/.#define PA
11080 47 45 52 49 44 28 70 29 20 28 28 69 6e 74 29 28  GERID(p) ((int)(
11081 70 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20  p->fd)).#define 
11082 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29  FILEHANDLEID(fd)
11083 20 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a   ((int)fd)../*.*
11084 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65  * The page cache
11085 20 61 73 20 61 20 77 68 6f 6c 65 20 69 73 20 61   as a whole is a
11086 6c 77 61 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20  lways in one of 
11087 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
11088 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
11089 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20   PAGER_UNLOCK   
1108a 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61       The page ca
1108b 63 68 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65  che is not curre
1108c 6e 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20  ntly reading or 
1108d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1108e 20 20 20 20 20 20 20 20 20 20 77 72 69 74 69 6e            writin
1108f 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  g the database f
11090 69 6c 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e  ile.  There is n
11091 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  o.**            
11092 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 20             data 
11093 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20  held in memory. 
11094 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6e 69   This is the ini
11095 74 69 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  tial.**         
11096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
11097 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47  ate..**.**   PAG
11098 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 20  ER_SHARED       
11099 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
1109a 69 73 20 72 65 61 64 69 6e 67 20 74 68 65 20 64  is reading the d
1109b 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20  atabase..**     
1109c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1109d 20 20 57 72 69 74 69 6e 67 20 69 73 20 6e 6f 74    Writing is not
1109e 20 70 65 72 6d 69 74 74 65 64 2e 20 20 54 68 65   permitted.  The
1109f 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20  re can be.**    
110a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110a1 20 20 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64     multiple read
110a2 65 72 73 20 61 63 63 65 73 73 69 6e 67 20 74 68  ers accessing th
110a3 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a  e same database.
110a4 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
110a5 20 20 20 20 20 20 20 20 20 66 69 6c 65 20 61 74           file at
110a6 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a   the same time..
110a7 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 45  **.**   PAGER_RE
110a8 53 45 52 56 45 44 20 20 20 20 20 20 54 68 69 73  SERVED      This
110a9 20 70 72 6f 63 65 73 73 20 68 61 73 20 72 65 73   process has res
110aa 65 72 76 65 64 20 74 68 65 20 64 61 74 61 62 61  erved the databa
110ab 73 65 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a  se for writing.*
110ac 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
110ad 20 20 20 20 20 20 20 20 62 75 74 20 68 61 73 20          but has 
110ae 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 61 6e 79  not yet made any
110af 20 63 68 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20   changes.  Only 
110b0 6f 6e 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20  one process.**  
110b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110b2 20 20 20 20 20 61 74 20 61 20 74 69 6d 65 20 63       at a time c
110b3 61 6e 20 72 65 73 65 72 76 65 20 74 68 65 20 64  an reserve the d
110b4 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 6f 72  atabase.  The or
110b5 69 67 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20  iginal.**       
110b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110b7 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
110b8 73 20 6e 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66  s not been modif
110b9 69 65 64 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20  ied so other.** 
110ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110bb 20 20 20 20 20 20 70 72 6f 63 65 73 73 65 73 20        processes 
110bc 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61  may still be rea
110bd 64 69 6e 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b  ding the on-disk
110be 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
110bf 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61            databa
110c0 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20  se file..**.**  
110c1 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
110c2 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61       The page ca
110c3 63 68 65 20 69 73 20 77 72 69 74 69 6e 67 20 74  che is writing t
110c4 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20  he database..** 
110c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110c6 20 20 20 20 20 20 41 63 63 65 73 73 20 69 73 20        Access is 
110c7 65 78 63 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f  exclusive.  No o
110c8 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6f  ther processes o
110c9 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
110ca 20 20 20 20 20 20 20 20 20 20 20 74 68 72 65 61             threa
110cb 64 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e  ds can be readin
110cc 67 20 6f 72 20 77 72 69 74 69 6e 67 20 77 68 69  g or writing whi
110cd 6c 65 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20  le one.**       
110ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110cf 70 72 6f 63 65 73 73 20 69 73 20 77 72 69 74 69  process is writi
110d0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  ng..**.**   PAGE
110d1 52 5f 53 59 4e 43 45 44 20 20 20 20 20 20 20 20  R_SYNCED        
110d2 54 68 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20  The pager moves 
110d3 74 6f 20 74 68 69 73 20 73 74 61 74 65 20 66 72  to this state fr
110d4 6f 6d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49  om PAGER_EXCLUSI
110d5 56 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  VE.**           
110d6 20 20 20 20 20 20 20 20 20 20 20 20 61 66 74 65              afte
110d7 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  r all dirty page
110d8 73 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74  s have been writ
110d9 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20  ten to the.**   
110da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110db 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c      database fil
110dc 65 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68  e and the file h
110dd 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74  as been synced t
110de 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  o.**            
110df 20 20 20 20 20 20 20 20 20 20 20 64 69 73 6b 2e             disk.
110e0 20 41 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e   All that remain
110e1 73 20 74 6f 20 64 6f 20 69 73 20 74 6f 20 72 65  s to do is to re
110e2 6d 6f 76 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20  move or.**      
110e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110e4 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f   truncate the jo
110e5 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 74  urnal file and t
110e6 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a  he transaction .
110e7 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
110e8 20 20 20 20 20 20 20 20 20 77 69 6c 6c 20 62 65           will be
110e9 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a   committed..**.*
110ea 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65  * The page cache
110eb 20 63 6f 6d 65 73 20 75 70 20 69 6e 20 50 41 47   comes up in PAG
110ec 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20  ER_UNLOCK.  The 
110ed 66 69 72 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20  first time a.** 
110ee 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
110ef 29 20 6f 63 63 75 72 73 2c 20 74 68 65 20 73 74  ) occurs, the st
110f0 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  ate transitions 
110f1 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 2e  to PAGER_SHARED.
110f2 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 70 61  .** After all pa
110f3 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65  ges have been re
110f4 6c 65 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c  leased using sql
110f5 69 74 65 5f 70 61 67 65 5f 75 6e 72 65 66 28 29  ite_page_unref()
110f6 2c 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 74  ,.** the state t
110f7 72 61 6e 73 69 74 69 6f 6e 73 20 62 61 63 6b 20  ransitions back 
110f8 74 6f 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e  to PAGER_UNLOCK.
110f9 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65    The first time
110fa 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33  .** that sqlite3
110fb 50 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20  PagerWrite() is 
110fc 63 61 6c 6c 65 64 2c 20 74 68 65 20 73 74 61 74  called, the stat
110fd 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f  e transitions to
110fe 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56  .** PAGER_RESERV
110ff 45 44 2e 20 20 28 4e 6f 74 65 20 74 68 61 74 20  ED.  (Note that 
11100 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
11101 65 28 29 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a  e() can only be.
11102 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20  ** called on an 
11103 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
11104 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61   which means tha
11105 74 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74  t the pager must
11106 0a 2a 2a 20 62 65 20 69 6e 20 50 41 47 45 52 5f  .** be in PAGER_
11107 53 48 41 52 45 44 20 62 65 66 6f 72 65 20 69 74  SHARED before it
11108 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20   transitions to 
11109 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 29  PAGER_RESERVED.)
1110a 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56  .** PAGER_RESERV
1110b 45 44 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ED means that th
1110c 65 72 65 20 69 73 20 61 6e 20 6f 70 65 6e 20 72  ere is an open r
1110d 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
1110e 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 69 74 69  .** The transiti
1110f 6f 6e 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c  on to PAGER_EXCL
11110 55 53 49 56 45 20 6f 63 63 75 72 73 20 62 65 66  USIVE occurs bef
11111 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a  ore any changes.
11112 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74  ** are made to t
11113 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
11114 2c 20 74 68 6f 75 67 68 20 77 72 69 74 65 73 20  , though writes 
11115 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a  to the rollback.
11116 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72  ** journal occur
11117 73 20 77 69 74 68 20 6a 75 73 74 20 50 41 47 45  s with just PAGE
11118 52 5f 52 45 53 45 52 56 45 44 2e 20 20 41 66 74  R_RESERVED.  Aft
11119 65 72 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67  er an sqlite3Pag
1111a 65 72 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20  erRollback().** 
1111b 6f 72 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  or sqlite3PagerC
1111c 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2c  ommitPhaseTwo(),
1111d 20 74 68 65 20 73 74 61 74 65 20 63 61 6e 20 67   the state can g
1111e 6f 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f  o back to PAGER_
1111f 53 48 41 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74  SHARED,.** or it
11120 20 63 61 6e 20 73 74 61 79 20 61 74 20 50 41 47   can stay at PAG
11121 45 52 5f 45 58 43 4c 55 53 49 56 45 20 69 66 20  ER_EXCLUSIVE if 
11122 77 65 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73  we are in exclus
11123 69 76 65 20 61 63 63 65 73 73 20 6d 6f 64 65 2e  ive access mode.
11124 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45  .*/.#define PAGE
11125 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a  R_UNLOCK      0.
11126 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 48  #define PAGER_SH
11127 41 52 45 44 20 20 20 20 20 20 31 20 20 20 2f 2a  ARED      1   /*
11128 20 73 61 6d 65 20 61 73 20 53 48 41 52 45 44 5f   same as SHARED_
11129 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20  LOCK */.#define 
1112a 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20  PAGER_RESERVED  
1112b 20 20 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73    2   /* same as
1112c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a   RESERVED_LOCK *
1112d 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
1112e 45 58 43 4c 55 53 49 56 45 20 20 20 34 20 20 20  EXCLUSIVE   4   
1112f 2f 2a 20 73 61 6d 65 20 61 73 20 45 58 43 4c 55  /* same as EXCLU
11130 53 49 56 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65  SIVE_LOCK */.#de
11131 66 69 6e 65 20 50 41 47 45 52 5f 53 59 4e 43 45  fine PAGER_SYNCE
11132 44 20 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20  D      5../*.** 
11133 54 68 69 73 20 6d 61 63 72 6f 20 72 6f 75 6e 64  This macro round
11134 73 20 76 61 6c 75 65 73 20 75 70 20 73 6f 20 74  s values up so t
11135 68 61 74 20 69 66 20 74 68 65 20 76 61 6c 75 65  hat if the value
11136 20 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 69   is an address i
11137 74 0a 2a 2a 20 69 73 20 67 75 61 72 61 6e 74 65  t.** is guarante
11138 65 64 20 74 6f 20 62 65 20 61 6e 20 61 64 64 72  ed to be an addr
11139 65 73 73 20 74 68 61 74 20 69 73 20 61 6c 69 67  ess that is alig
1113a 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65  ned to an 8-byte
1113b 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 23 64   boundary..*/.#d
1113c 65 66 69 6e 65 20 46 4f 52 43 45 5f 41 4c 49 47  efine FORCE_ALIG
1113d 4e 4d 45 4e 54 28 58 29 20 20 20 28 28 28 58 29  NMENT(X)   (((X)
1113e 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 41  +7)&~7)../*.** A
1113f 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20   macro used for 
11140 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64  invoking the cod
11141 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ec if there is o
11142 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ne.*/.#ifdef SQL
11143 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20  ITE_HAS_CODEC.# 
11144 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c  define CODEC1(P,
11145 44 2c 4e 2c 58 29 20 69 66 28 20 50 2d 3e 78 43  D,N,X) if( P->xC
11146 6f 64 65 63 21 3d 30 20 29 7b 20 50 2d 3e 78 43  odec!=0 ){ P->xC
11147 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72  odec(P->pCodecAr
11148 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23 20 64 65  g,D,N,X); }.# de
11149 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c  fine CODEC2(P,D,
1114a 4e 2c 58 29 20 28 28 63 68 61 72 2a 29 28 50 2d  N,X) ((char*)(P-
1114b 3e 78 43 6f 64 65 63 21 3d 30 3f 50 2d 3e 78 43  >xCodec!=0?P->xC
1114c 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72  odec(P->pCodecAr
1114d 67 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a 23 65 6c  g,D,N,X):D)).#el
1114e 73 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45  se.# define CODE
1114f 43 31 28 50 2c 44 2c 4e 2c 58 29 20 2f 2a 20 4e  C1(P,D,N,X) /* N
11150 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65  O-OP */.# define
11151 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 29   CODEC2(P,D,N,X)
11152 20 28 28 63 68 61 72 2a 29 44 29 0a 23 65 6e 64   ((char*)D).#end
11153 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
11154 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65  ximum allowed se
11155 63 74 6f 72 20 73 69 7a 65 2e 20 31 36 4d 42 2e  ctor size. 16MB.
11156 20 49 66 20 74 68 65 20 78 53 65 63 74 6f 72 73   If the xSectors
11157 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a  ize() method .**
11158 20 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65   returns a value
11159 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69   larger than thi
1115a 73 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54  s, then MAX_SECT
1115b 4f 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20  OR_SIZE is used 
1115c 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73  instead..** This
1115d 20 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62   could conceivab
1115e 6c 79 20 63 61 75 73 65 20 63 6f 72 72 75 70 74  ly cause corrupt
1115f 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  ion following a 
11160 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e  power failure on
11161 0a 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74 65  .** such a syste
11162 6d 2e 20 54 68 69 73 20 69 73 20 63 75 72 72 65  m. This is curre
11163 6e 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65  ntly an undocume
11164 6e 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23  nted limit..*/.#
11165 64 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f  define MAX_SECTO
11166 52 5f 53 49 5a 45 20 30 78 30 31 30 30 30 30 30  R_SIZE 0x0100000
11167 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
11168 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
11169 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69  wing structure i
1116a 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
1116b 65 61 63 68 20 61 63 74 69 76 65 0a 2a 2a 20 73  each active.** s
1116c 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 74 61  avepoint and sta
1116d 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1116e 6f 6e 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d  on in the system
1116f 2e 20 41 6c 6c 20 73 75 63 68 20 73 74 72 75 63  . All such struc
11170 74 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f  tures.** are sto
11171 72 65 64 20 69 6e 20 74 68 65 20 50 61 67 65 72  red in the Pager
11172 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72  .aSavepoint[] ar
11173 72 61 79 2c 20 77 68 69 63 68 20 69 73 20 61 6c  ray, which is al
11174 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72  located and.** r
11175 65 73 69 7a 65 64 20 75 73 69 6e 67 20 73 71 6c  esized using sql
11176 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a  ite3Realloc()..*
11177 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 76 65  *.** When a save
11178 70 6f 69 6e 74 20 69 73 20 63 72 65 61 74 65 64  point is created
11179 2c 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70  , the PagerSavep
1117a 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20  oint.iHdrOffset 
1117b 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20  field is.** set 
1117c 74 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e  to 0. If a journ
1117d 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69  al-header is wri
1117e 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61  tten into the ma
1117f 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65  in journal while
11180 0a 2a 2a 20 74 68 65 20 73 61 76 65 70 6f 69 6e  .** the savepoin
11181 74 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 65  t is active, the
11182 6e 20 69 48 64 72 4f 66 66 73 65 74 20 69 73 20  n iHdrOffset is 
11183 73 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20  set to the byte 
11184 6f 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64  offset .** immed
11185 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67  iately following
11186 20 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61   the last journa
11187 6c 20 72 65 63 6f 72 64 20 77 72 69 74 74 65 6e  l record written
11188 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a   into the main.*
11189 2a 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65  * journal before
1118a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61   the journal-hea
1118b 64 65 72 2e 20 54 68 69 73 20 69 73 20 72 65 71  der. This is req
1118c 75 69 72 65 64 20 64 75 72 69 6e 67 20 73 61 76  uired during sav
1118d 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61  epoint.** rollba
1118e 63 6b 20 28 73 65 65 20 70 61 67 65 72 50 6c 61  ck (see pagerPla
1118f 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 29  ybackSavepoint()
11190 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  )..*/.typedef st
11191 72 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f  ruct PagerSavepo
11192 69 6e 74 20 50 61 67 65 72 53 61 76 65 70 6f 69  int PagerSavepoi
11193 6e 74 3b 0a 73 74 72 75 63 74 20 50 61 67 65 72  nt;.struct Pager
11194 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36  Savepoint {.  i6
11195 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20  4 iOffset;      
11196 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
11197 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 69 6e  arting offset in
11198 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f   main journal */
11199 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 73 65  .  i64 iHdrOffse
1119a 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
1119b 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a  /* See above */.
1119c 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53 61 76    Bitvec *pInSav
1119d 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f  epoint;        /
1119e 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 69  * Set of pages i
1119f 6e 20 74 68 69 73 20 73 61 76 65 70 6f 69 6e 74  n this savepoint
111a0 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67   */.  Pgno nOrig
111a1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
111a2 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e     /* Original n
111a3 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
111a4 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  n file */.  Pgno
111a5 20 69 53 75 62 52 65 63 3b 20 20 20 20 20 20 20   iSubRec;       
111a6 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
111a7 78 20 6f 66 20 66 69 72 73 74 20 72 65 63 6f 72  x of first recor
111a8 64 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c  d in sub-journal
111a9 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
111aa 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20  open page cache 
111ab 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
111ac 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
111ad 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
111ae 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 6d   Pager.errCode m
111af 61 79 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c  ay be set to SQL
111b0 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54  ITE_IOERR, SQLIT
111b1 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a  E_CORRUPT, or.**
111b2 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e   or SQLITE_FULL.
111b3 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68 65   Once one of the
111b4 20 66 69 72 73 74 20 74 68 72 65 65 20 65 72 72   first three err
111b5 6f 72 73 20 6f 63 63 75 72 73 2c 20 69 74 20 70  ors occurs, it p
111b6 65 72 73 69 73 74 73 0a 2a 2a 20 61 6e 64 20 69  ersists.** and i
111b7 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 68  s returned as th
111b8 65 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65 72  e result of ever
111b9 79 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41 50  y major pager AP
111ba 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20  I call.  The.** 
111bb 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72 65 74 75  SQLITE_FULL retu
111bc 72 6e 20 63 6f 64 65 20 69 73 20 73 6c 69 67 68  rn code is sligh
111bd 74 6c 79 20 64 69 66 66 65 72 65 6e 74 2e 20 49  tly different. I
111be 74 20 70 65 72 73 69 73 74 73 20 6f 6e 6c 79 20  t persists only 
111bf 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 6e 65 78  until the.** nex
111c0 74 20 73 75 63 63 65 73 73 66 75 6c 20 72 6f 6c  t successful rol
111c1 6c 62 61 63 6b 20 69 73 20 70 65 72 66 6f 72 6d  lback is perform
111c2 65 64 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20  ed on the pager 
111c3 63 61 63 68 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20  cache. Also,.** 
111c4 53 51 4c 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73  SQLITE_FULL does
111c5 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 20   not affect the 
111c6 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
111c7 29 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67  ) and sqlite3Pag
111c8 65 72 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 41 50  erLookup().** AP
111c9 49 73 2c 20 74 68 65 79 20 6d 61 79 20 73 74 69  Is, they may sti
111ca 6c 6c 20 62 65 20 75 73 65 64 20 73 75 63 63 65  ll be used succe
111cb 73 73 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4d  ssfully..**.** M
111cc 61 6e 61 67 69 6e 67 20 74 68 65 20 73 69 7a 65  anaging the size
111cd 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
111ce 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 69   file in pages i
111cf 73 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c  s a little compl
111d0 69 63 61 74 65 64 2e 0a 2a 2a 20 54 68 65 20 76  icated..** The v
111d1 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62  ariable Pager.db
111d2 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  Size contains th
111d3 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
111d4 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  s that the datab
111d5 61 73 65 0a 2a 2a 20 69 6d 61 67 65 20 63 75 72  ase.** image cur
111d6 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 2e  rently contains.
111d7 20 41 73 20 74 68 65 20 64 61 74 61 62 61 73 65   As the database
111d8 20 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72 20   image grows or 
111d9 73 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a 20  shrinks this.** 
111da 76 61 72 69 61 62 6c 65 20 69 73 20 75 70 64 61  variable is upda
111db 74 65 64 2e 20 54 68 65 20 76 61 72 69 61 62 6c  ted. The variabl
111dc 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69  e Pager.dbFileSi
111dd 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  ze contains the 
111de 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 70 61 67  number.** of pag
111df 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
111e0 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6d 61  se file. This ma
111e1 79 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 66  y be different f
111e2 72 6f 6d 20 50 61 67 65 72 2e 64 62 53 69 7a 65  rom Pager.dbSize
111e3 0a 2a 2a 20 69 66 20 73 6f 6d 65 20 70 61 67 65  .** if some page
111e4 73 20 68 61 76 65 20 62 65 65 6e 20 61 70 70 65  s have been appe
111e5 6e 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 61  nded to the data
111e6 62 61 73 65 20 69 6d 61 67 65 20 62 75 74 20 6e  base image but n
111e7 6f 74 20 79 65 74 20 77 72 69 74 74 65 6e 0a 2a  ot yet written.*
111e8 2a 20 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 63  * out from the c
111e9 61 63 68 65 20 74 6f 20 74 68 65 20 61 63 74 75  ache to the actu
111ea 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e  al file on disk.
111eb 20 4f 72 20 69 66 20 74 68 65 20 69 6d 61 67 65   Or if the image
111ec 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 74 72 75   has been.** tru
111ed 6e 63 61 74 65 64 20 62 79 20 61 6e 20 69 6e 63  ncated by an inc
111ee 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20  remental-vacuum 
111ef 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 50  operation. The P
111f0 61 67 65 72 2e 64 62 4f 72 69 67 53 69 7a 65 20  ager.dbOrigSize 
111f1 76 61 72 69 61 62 6c 65 0a 2a 2a 20 63 6f 6e 74  variable.** cont
111f2 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  ains the number 
111f3 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
111f4 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77  database image w
111f5 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a  hen the current.
111f6 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  ** transaction w
111f7 61 73 20 6f 70 65 6e 65 64 2e 20 54 68 65 20 63  as opened. The c
111f8 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74  ontents of all t
111f9 68 72 65 65 20 6f 66 20 74 68 65 73 65 20 76 61  hree of these va
111fa 72 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20 6f 6e  riables is.** on
111fb 6c 79 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  ly guaranteed to
111fc 20 62 65 20 63 6f 72 72 65 63 74 20 69 66 20 74   be correct if t
111fd 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65 72  he boolean Pager
111fe 2e 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73 20  .dbSizeValid is 
111ff 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  true..*/.struct 
11200 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 65  Pager {.  sqlite
11201 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20  3_vfs *pVfs;    
11202 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e 63        /* OS func
11203 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f 72  tions to use for
11204 20 49 4f 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72   IO */.  u8 jour
11205 6e 61 6c 4f 70 65 6e 3b 20 20 20 20 20 20 20 20  nalOpen;        
11206 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
11207 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73  journal file des
11208 63 72 69 70 74 6f 72 73 20 69 73 20 76 61 6c 69  criptors is vali
11209 64 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61  d */.  u8 journa
1120a 6c 53 74 61 72 74 65 64 3b 20 20 20 20 20 20 20  lStarted;       
1120b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65     /* True if he
1120c 61 64 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20  ader of journal 
1120d 69 73 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75  is synced */.  u
1120e 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20  8 useJournal;   
1120f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
11210 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  e a rollback jou
11211 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c  rnal on this fil
11212 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 64  e */.  u8 noRead
11213 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  lock;           
11214 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74     /* Do not bot
11215 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65  her to obtain re
11216 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 20  adlocks */.  u8 
11217 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20  noSync;         
11218 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e           /* Do n
11219 6f 74 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72  ot sync the jour
1121a 6e 61 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a 20  nal if true */. 
1121b 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20   u8 fullSync;   
1121c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1121d 44 6f 20 65 78 74 72 61 20 73 79 6e 63 73 20 6f  Do extra syncs o
1121e 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f  f the journal fo
1121f 72 20 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f 0a  r robustness */.
11220 20 20 75 38 20 73 79 6e 63 5f 66 6c 61 67 73 3b    u8 sync_flags;
11221 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11222 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f 4e 4f 52   One of SYNC_NOR
11223 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c 4c  MAL or SYNC_FULL
11224 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20   */.  u8 state; 
11225 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11226 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43    /* PAGER_UNLOC
11227 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53  K, _SHARED, _RES
11228 45 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20  ERVED, etc. */. 
11229 20 75 38 20 74 65 6d 70 46 69 6c 65 3b 20 20 20   u8 tempFile;   
1122a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1122b 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 74  zFilename is a t
1122c 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f  emporary file */
1122d 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20  .  u8 readOnly; 
1122e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1122f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 72 65 61  * True for a rea
11230 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20  d-only database 
11231 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 79 6e 63  */.  u8 needSync
11232 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11233 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 66   /* True if an f
11234 73 79 6e 63 28 29 20 69 73 20 6e 65 65 64 65 64  sync() is needed
11235 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   on the journal 
11236 2a 2f 0a 20 20 75 38 20 64 69 72 74 79 43 61 63  */.  u8 dirtyCac
11237 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  he;             
11238 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 68   /* True if cach
11239 65 64 20 70 61 67 65 73 20 68 61 76 65 20 63 68  ed pages have ch
1123a 61 6e 67 65 64 20 2a 2f 0a 20 20 75 38 20 6d 65  anged */.  u8 me
1123b 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  mDb;            
1123c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
1123d 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66 69  o inhibit all fi
1123e 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 75 38 20 73  le I/O */.  u8 s
1123f 65 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20  etMaster;       
11240 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
11241 69 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68 61  if a m-j name ha
11242 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  s been written t
11243 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20 64  o jrnl */.  u8 d
11244 6f 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20 20  oNotSync;       
11245 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65          /* Boole
11246 61 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c 20  an. While true, 
11247 64 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68 65  do not spill the
11248 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 65   cache */.  u8 e
11249 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20  xclusiveMode;   
1124a 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65          /* Boole
1124b 61 6e 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b  an. True if lock
1124c 69 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53  ing_mode==EXCLUS
1124d 49 56 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72  IVE */.  u8 jour
1124e 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20  nalMode;        
1124f 20 20 20 20 20 2f 2a 20 4f 6e 20 6f 66 20 74 68       /* On of th
11250 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  e PAGER_JOURNALM
11251 4f 44 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a  ODE_* values */.
11252 20 20 75 38 20 64 62 4d 6f 64 69 66 69 65 64 3b    u8 dbModified;
11253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11254 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 61   True if there a
11255 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74  re any changes t
11256 6f 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 75 38  o the Db */.  u8
11257 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65   changeCountDone
11258 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74  ;         /* Set
11259 20 61 66 74 65 72 20 69 6e 63 72 65 6d 65 6e 74   after increment
1125a 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63  ing the change-c
1125b 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 64  ounter */.  u8 d
1125c 62 53 69 7a 65 56 61 6c 69 64 3b 20 20 20 20 20  bSizeValid;     
1125d 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 77          /* Set w
1125e 68 65 6e 20 64 62 53 69 7a 65 20 69 73 20 63 6f  hen dbSize is co
1125f 72 72 65 63 74 20 2a 2f 0a 20 20 50 67 6e 6f 20  rrect */.  Pgno 
11260 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  dbSize;         
11261 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
11262 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
11263 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50   database */.  P
11264 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20  gno dbOrigSize; 
11265 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62             /* db
11266 53 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20  Size before the 
11267 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
11268 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62  ion */.  Pgno db
11269 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20  FileSize;       
1126a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1126b 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
1126c 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
1126d 20 20 75 33 32 20 76 66 73 46 6c 61 67 73 3b 20    u32 vfsFlags; 
1126e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1126f 20 46 6c 61 67 73 20 66 6f 72 20 73 71 6c 69 74   Flags for sqlit
11270 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a  e3_vfs.xOpen() *
11271 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b  /.  int errCode;
11272 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11273 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61  /* One of severa
11274 6c 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72  l kinds of error
11275 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b  s */.  int nRec;
11276 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11277 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
11278 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f  pages written to
11279 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   the journal */.
1127a 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 74 3b    u32 cksumInit;
1127b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1127c 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 76 61   Quasi-random va
1127d 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 76 65  lue added to eve
1127e 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20  ry checksum */. 
1127f 20 69 6e 74 20 73 74 6d 74 4e 52 65 63 3b 20 20   int stmtNRec;  
11280 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11281 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64  Number of record
11282 73 20 69 6e 20 73 74 6d 74 20 73 75 62 6a 6f 75  s in stmt subjou
11283 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  rnal */.  int nE
11284 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20  xtra;           
11285 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 69        /* Add thi
11286 73 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f 20  s many bytes to 
11287 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70  each in-memory p
11288 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67  age */.  int pag
11289 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  eSize;          
1128a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1128b 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 61 67  f bytes in a pag
1128c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65  e */.  int nPage
1128d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1128e 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62     /* Total numb
1128f 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20  er of in-memory 
11290 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6d  pages */.  int m
11291 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  xPage;          
11292 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
11293 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  m number of page
11294 73 20 74 6f 20 68 6f 6c 64 20 69 6e 20 63 61 63  s to hold in cac
11295 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50  he */.  Pgno mxP
11296 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  gno;            
11297 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61      /* Maximum a
11298 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 74  llowed size of t
11299 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
1129a 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72   Bitvec *pInJour
1129b 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  nal;         /* 
1129c 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68  One bit for each
1129d 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
1129e 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
1129f 42 69 74 76 65 63 20 2a 70 41 6c 77 61 79 73 52  Bitvec *pAlwaysR
112a0 6f 6c 6c 62 61 63 6b 3b 20 20 20 20 2f 2a 20 4f  ollback;    /* O
112a1 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 20  ne bit for each 
112a2 70 61 67 65 20 6d 61 72 6b 65 64 20 61 6c 77 61  page marked alwa
112a3 79 73 2d 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20  ys-rollback */. 
112a4 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
112a5 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
112a6 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
112a7 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63  base file */.  c
112a8 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20  har *zJournal;  
112a9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
112aa 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  me of the journa
112ab 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72  l file */.  char
112ac 20 2a 7a 44 69 72 65 63 74 6f 72 79 3b 20 20 20   *zDirectory;   
112ad 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63          /* Direc
112ae 74 6f 72 79 20 68 6f 6c 64 20 64 61 74 61 62 61  tory hold databa
112af 73 65 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 20 66  se and journal f
112b0 69 6c 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  iles */.  sqlite
112b1 33 5f 66 69 6c 65 20 2a 66 64 2c 20 2a 6a 66 64  3_file *fd, *jfd
112b2 3b 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65  ;     /* File de
112b3 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 64 61  scriptors for da
112b4 74 61 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e  tabase and journ
112b5 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  al */.  sqlite3_
112b6 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20  file *sjfd;     
112b7 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63      /* File desc
112b8 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20 73  riptor for the s
112b9 75 62 2d 6a 6f 75 72 6e 61 6c 2a 2f 0a 20 20 69  ub-journal*/.  i
112ba 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65  nt (*xBusyHandle
112bb 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75  r)(void*); /* Fu
112bc 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77  nction to call w
112bd 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f  hen busy */.  vo
112be 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72  id *pBusyHandler
112bf 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e  Arg;      /* Con
112c0 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f  text argument fo
112c1 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a  r xBusyHandler *
112c2 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 4f  /.  i64 journalO
112c3 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ff;             
112c4 2f 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65 20  /* Current byte 
112c5 6f 66 66 73 65 74 20 69 6e 20 74 68 65 20 6a 6f  offset in the jo
112c6 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20  urnal file */.  
112c7 69 36 34 20 6a 6f 75 72 6e 61 6c 48 64 72 3b 20  i64 journalHdr; 
112c8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
112c9 79 74 65 20 6f 66 66 73 65 74 20 74 6f 20 70 72  yte offset to pr
112ca 65 76 69 6f 75 73 20 6a 6f 75 72 6e 61 6c 20 68  evious journal h
112cb 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 73  eader */.  u32 s
112cc 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20  ectorSize;      
112cd 20 20 20 20 20 20 20 2f 2a 20 41 73 73 75 6d 65         /* Assume
112ce 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 64 75  d sector size du
112cf 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f  ring rollback */
112d0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
112d1 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20  EST.  int nHit, 
112d2 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20 20  nMiss;          
112d3 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73 20    /* Cache hits 
112d4 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20  and missing */. 
112d5 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 69   int nRead, nWri
112d6 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  te;          /* 
112d7 44 61 74 61 62 61 73 65 20 70 61 67 65 73 20 72  Database pages r
112d8 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23  ead/written */.#
112d9 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a 78  endif.  void (*x
112da 52 65 69 6e 69 74 65 72 29 28 44 62 50 61 67 65  Reiniter)(DbPage
112db 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73  *); /* Call this
112dc 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 65   routine when re
112dd 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a 2f  loading pages */
112de 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48  .#ifdef SQLITE_H
112df 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 20  AS_CODEC.  void 
112e0 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a  *(*xCodec)(void*
112e1 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29  ,void*,Pgno,int)
112e2 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72  ; /* Routine for
112e3 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 74   en/decoding dat
112e4 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f  a */.  void *pCo
112e5 64 65 63 41 72 67 3b 20 20 20 20 20 20 20 20 20  decArg;         
112e6 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
112e7 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 28 29  ment to xCodec()
112e8 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61   */.#endif.  cha
112e9 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20  r *pTmpSpace;   
112ea 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
112eb 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  r.pageSize bytes
112ec 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d   of space for tm
112ed 70 20 75 73 65 20 2a 2f 0a 20 20 63 68 61 72 20  p use */.  char 
112ee 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20  dbFileVers[16]; 
112ef 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65         /* Change
112f0 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61 62  s whenever datab
112f1 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73  ase file changes
112f2 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61   */.  i64 journa
112f3 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20  lSizeLimit;     
112f4 20 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20    /* Size limit 
112f5 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a  for persistent j
112f6 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a  ournal files */.
112f7 20 20 50 43 61 63 68 65 20 2a 70 50 43 61 63 68    PCache *pPCach
112f8 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
112f9 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65   Pointer to page
112fa 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f   cache object */
112fb 0a 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  .  PagerSavepoin
112fc 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f  t *aSavepoint; /
112fd 2a 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 76  * Array of activ
112fe 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a  e savepoints */.
112ff 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74    int nSavepoint
11300 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
11301 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
11302 6e 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e  nts in aSavepoin
11303 74 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  t[] */.};../*.**
11304 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67   The following g
11305 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20  lobal variables 
11306 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 73  hold counters us
11307 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e  ed for.** testin
11308 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e  g purposes only.
11309 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c 65    These variable
1130a 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 69  s do not exist i
1130b 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69  n.** a non-testi
1130c 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 65  ng build.  These
1130d 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6e   variables are n
1130e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a  ot thread-safe..
1130f 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
11310 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
11311 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67   int sqlite3_pag
11312 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 20  er_readdb_count 
11313 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65  = 0;    /* Numbe
11314 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20  r of full pages 
11315 72 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a  read from DB */.
11316 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
11317 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69  qlite3_pager_wri
11318 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20  tedb_count = 0; 
11319 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
1131a 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65  ull pages writte
1131b 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54  n to DB */.SQLIT
1131c 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1131d 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63  3_pager_writej_c
1131e 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20  ount = 0;    /* 
1131f 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
11320 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e  written to journ
11321 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50  al */.# define P
11322 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 2b  AGER_INCR(v)  v+
11323 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  +.#else.# define
11324 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a 23   PAGER_INCR(v).#
11325 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a  endif..../*.** J
11326 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 67  ournal files beg
11327 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c  in with the foll
11328 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 69  owing magic stri
11329 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a 2a  ng.  The data.**
1132a 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72   was obtained fr
1132b 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20  om /dev/random. 
1132c 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79   It is used only
1132d 20 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 65   as a sanity che
1132e 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20  ck..**.** Since 
1132f 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74  version 2.8.0, t
11330 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61  he journal forma
11331 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 74  t contains addit
11332 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20  ional sanity.** 
11333 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61  checking informa
11334 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f  tion.  If the po
11335 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20  wer fails while 
11336 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62  the journal is b
11337 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c  eing.** written,
11338 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72   semi-random gar
11339 62 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20  bage data might 
1133a 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f  appear in the jo
1133b 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66  urnal.** file af
1133c 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73  ter power is res
1133d 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74  tored.  If an at
1133e 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61  tempt is then ma
1133f 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68  de.** to roll th
11340 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20  e journal back, 
11341 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75  the database cou
11342 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e  ld be corrupted.
11343 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 6c    The additional
11344 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 6b  .** sanity check
11345 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 61  ing data is an a
11346 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f 76  ttempt to discov
11347 65 72 20 74 68 65 20 67 61 72 62 61 67 65 20 69  er the garbage i
11348 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  n the.** journal
11349 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a   and ignore it..
1134a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 79  **.** The sanity
1134b 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d   checking inform
1134c 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65  ation for the ne
1134d 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74  w journal format
1134e 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20   consists.** of 
1134f 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 75  a 32-bit checksu
11350 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f  m on each page o
11351 66 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 65  f data.  The che
11352 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f 74  cksum covers bot
11353 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75  h.** the page nu
11354 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 61  mber and the pPa
11355 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79  ger->pageSize by
11356 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 20  tes of data for 
11357 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69  the page..** Thi
11358 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 69  s cksum is initi
11359 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d 62  alized to a 32-b
1135a 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20  it random value 
1135b 74 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 20  that appears in 
1135c 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  the.** journal f
1135d 69 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 20  ile right after 
1135e 74 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 65  the header.  The
1135f 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69   random initiali
11360 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  zer is important
11361 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 72  ,.** because gar
11362 62 61 67 65 20 64 61 74 61 20 74 68 61 74 20 61  bage data that a
11363 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e  ppears at the en
11364 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69  d of a journal i
11365 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61  s likely.** data
11366 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 69   that was once i
11367 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68  n other files th
11368 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e  at have now been
11369 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 68   deleted.  If th
1136a 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 74  e.** garbage dat
1136b 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f  a came from an o
1136c 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20  bsolete journal 
1136d 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b 73  file, the checks
1136e 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20  ums might.** be 
1136f 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 79  correct.  But by
11370 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68   initializing th
11371 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 61  e checksum to ra
11372 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 68  ndom value which
11373 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74  .** is different
11374 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e   for every journ
11375 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20  al, we minimize 
11376 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74  that risk..*/.st
11377 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
11378 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61  ned char aJourna
11379 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30  lMagic[] = {.  0
1137a 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 2c  xd9, 0xd5, 0x05,
1137b 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 61   0xf9, 0x20, 0xa
1137c 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d  1, 0x63, 0xd7,.}
1137d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a  ;../*.** The siz
1137e 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20  e of the header 
1137f 61 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65  and of each page
11380 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
11381 69 73 20 64 65 74 65 72 6d 69 6e 65 64 0a 2a 2a  is determined.**
11382 20 62 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   by the followin
11383 67 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65  g macros..*/.#de
11384 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f  fine JOURNAL_PG_
11385 53 5a 28 70 50 61 67 65 72 29 20 20 28 28 70 50  SZ(pPager)  ((pP
11386 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20  ager->pageSize) 
11387 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  + 8)../*.** The 
11388 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73  journal header s
11389 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67  ize for this pag
1138a 65 72 2e 20 49 6e 20 74 68 65 20 66 75 74 75 72  er. In the futur
1138b 65 2c 20 74 68 69 73 20 63 6f 75 6c 64 20 62 65  e, this could be
1138c 0a 2a 2a 20 73 65 74 20 74 6f 20 73 6f 6d 65 20  .** set to some 
1138d 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20  value read from 
1138e 74 68 65 20 64 69 73 6b 20 63 6f 6e 74 72 6f 6c  the disk control
1138f 6c 65 72 2e 20 54 68 65 20 69 6d 70 6f 72 74 61  ler. The importa
11390 6e 74 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 69  nt.** characteri
11391 73 74 69 63 20 69 73 20 74 68 61 74 20 69 74 20  stic is that it 
11392 69 73 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65  is the same size
11393 20 61 73 20 61 20 64 69 73 6b 20 73 65 63 74 6f   as a disk secto
11394 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f  r..*/.#define JO
11395 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
11396 67 65 72 29 20 28 70 50 61 67 65 72 2d 3e 73 65  ger) (pPager->se
11397 63 74 6f 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a  ctorSize)../*.**
11398 20 54 68 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42   The macro MEMDB
11399 20 69 73 20 74 72 75 65 20 69 66 20 77 65 20 61   is true if we a
1139a 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
1139b 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
1139c 61 62 61 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20  abase..** We do 
1139d 74 68 69 73 20 61 73 20 61 20 6d 61 63 72 6f 20  this as a macro 
1139e 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 53  so that if the S
1139f 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
113a0 59 44 42 20 6d 61 63 72 6f 20 69 73 20 73 65 74  YDB macro is set
113a1 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f  ,.** the value o
113a2 66 20 4d 45 4d 44 42 20 77 69 6c 6c 20 62 65 20  f MEMDB will be 
113a3 61 20 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74  a constant and t
113a4 68 65 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c  he compiler will
113a5 20 6f 70 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74   optimize.** out
113a6 20 63 6f 64 65 20 74 68 61 74 20 77 6f 75 6c 64   code that would
113a7 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 2e 0a   never execute..
113a8 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
113a9 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23  _OMIT_MEMORYDB.#
113aa 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a   define MEMDB 0.
113ab 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 4d  #else.# define M
113ac 45 4d 44 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d  EMDB pPager->mem
113ad 44 62 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  Db.#endif../*.**
113ae 20 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47   Page number PAG
113af 45 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65  ER_MJ_PGNO is ne
113b0 76 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53  ver used in an S
113b1 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28  QLite database (
113b2 69 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65  it is.** reserve
113b3 64 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72  d for working ar
113b4 6f 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70  ound a windows/p
113b5 6f 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69  osix incompatibi
113b6 6c 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20  lity). It is.** 
113b7 75 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72  used in the jour
113b8 6e 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74  nal to signify t
113b9 68 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65  hat the remainde
113ba 72 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  r of the journal
113bb 20 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76   file .** is dev
113bc 6f 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20  oted to storing 
113bd 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
113be 20 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72   name - there ar
113bf 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20  e no more pages 
113c0 74 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e  to.** roll back.
113c1 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f   See comments fo
113c2 72 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65  r function write
113c3 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20  MasterJournal() 
113c4 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a  for details..*/.
113c5 2f 2a 20 23 64 65 66 69 6e 65 20 50 41 47 45 52  /* #define PAGER
113c6 5f 4d 4a 5f 50 47 4e 4f 28 78 29 20 28 50 45 4e  _MJ_PGNO(x) (PEN
113c7 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e  DING_BYTE/((x)->
113c8 70 61 67 65 53 69 7a 65 29 29 20 2a 2f 0a 23 64  pageSize)) */.#d
113c9 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50  efine PAGER_MJ_P
113ca 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28  GNO(x) ((Pgno)((
113cb 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78  PENDING_BYTE/((x
113cc 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29  )->pageSize))+1)
113cd 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  )../*.** The max
113ce 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 67 65 20  imum legal page 
113cf 6e 75 6d 62 65 72 20 69 73 20 28 32 5e 33 31 20  number is (2^31 
113d0 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  - 1)..*/.#define
113d1 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20   PAGER_MAX_PGNO 
113d2 32 31 34 37 34 38 33 36 34 37 0a 0a 2f 2a 0a 2a  2147483647../*.*
113d3 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
113d4 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79   it is necessary
113d5 20 74 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a   to write page *
113d6 70 50 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62  pPg into the sub
113d7 2d 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70  -journal..** A p
113d8 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20  age needs to be 
113d9 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
113da 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20   sub-journal if 
113db 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65  there exists one
113dc 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e  .** or more open
113dd 20 73 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20   savepoints for 
113de 77 68 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  which:.**.**   *
113df 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72   The page-number
113e0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   is less than or
113e1 20 65 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53   equal to PagerS
113e2 61 76 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20  avepoint.nOrig, 
113e3 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62  and.**   * The b
113e4 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  it corresponding
113e5 20 74 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d   to the page-num
113e6 62 65 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69  ber is not set i
113e7 6e 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61  n.**     PagerSa
113e8 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70  vepoint.pInSavep
113e9 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  oint..*/.static 
113ea 69 6e 74 20 73 75 62 6a 52 65 71 75 69 72 65 73  int subjRequires
113eb 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29  Page(PgHdr *pPg)
113ec 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20  {.  Pgno pgno = 
113ed 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67  pPg->pgno;.  Pag
113ee 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
113ef 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20  ->pPager;.  int 
113f0 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
113f1 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69  pPager->nSavepoi
113f2 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61  nt; i++){.    Pa
113f3 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20  gerSavepoint *p 
113f4 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65  = &pPager->aSave
113f5 70 6f 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66  point[i];.    if
113f6 28 20 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f  ( p->nOrig>=pgno
113f7 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69   && 0==sqlite3Bi
113f8 74 76 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53  tvecTest(p->pInS
113f9 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20  avepoint, pgno) 
113fa 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
113fb 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
113fc 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
113fd 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
113fe 20 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72   the page is alr
113ff 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72  eady in the jour
11400 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  nal file..*/.sta
11401 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f  tic int pageInJo
11402 75 72 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67  urnal(PgHdr *pPg
11403 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
11404 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50  te3BitvecTest(pP
11405 67 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  g->pPager->pInJo
11406 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f  urnal, pPg->pgno
11407 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  );.}../*.** Read
11408 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65   a 32-bit intege
11409 72 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e  r from the given
1140a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
1140b 2e 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74  .  Store the int
1140c 65 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20  eger.** that is 
1140d 72 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20  read in *pRes.  
1140e 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
1140f 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   if everything w
11410 6f 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20  orked, or an.** 
11411 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f  error code is so
11412 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
11413 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61  ng..**.** All va
11414 6c 75 65 73 20 61 72 65 20 73 74 6f 72 65 64 20  lues are stored 
11415 6f 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65  on disk as big-e
11416 6e 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ndian..*/.static
11417 20 69 6e 74 20 72 65 61 64 33 32 62 69 74 73 28   int read32bits(
11418 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64  sqlite3_file *fd
11419 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33  , i64 offset, u3
1141a 32 20 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69  2 *pRes){.  unsi
1141b 67 6e 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b  gned char ac[4];
1141c 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69  .  int rc = sqli
1141d 74 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63  te3OsRead(fd, ac
1141e 2c 20 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66  , sizeof(ac), of
1141f 66 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d  fset);.  if( rc=
11420 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11421 20 20 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65    *pRes = sqlite
11422 33 47 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20  3Get4byte(ac);. 
11423 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
11424 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61  }../*.** Write a
11425 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
11426 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75  into a string bu
11427 66 66 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69  ffer in big-endi
11428 61 6e 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a  an byte order..*
11429 2f 0a 23 64 65 66 69 6e 65 20 70 75 74 33 32 62  /.#define put32b
1142a 69 74 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65  its(A,B)  sqlite
1142b 33 50 75 74 34 62 79 74 65 28 28 75 38 2a 29 41  3Put4byte((u8*)A
1142c 2c 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ,B)../*.** Write
1142d 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65   a 32-bit intege
1142e 72 20 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e  r into the given
1142f 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
11430 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
11431 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73  _OK.** on succes
11432 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  s or an error co
11433 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20  de is something 
11434 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73  goes wrong..*/.s
11435 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 33  tatic int write3
11436 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69  2bits(sqlite3_fi
11437 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73  le *fd, i64 offs
11438 65 74 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20  et, u32 val){.  
11439 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75  char ac[4];.  pu
1143a 74 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29  t32bits(ac, val)
1143b 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
1143c 65 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63  e3OsWrite(fd, ac
1143d 2c 20 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a  , 4, offset);.}.
1143e 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69 6c 65 20 70  ./*.** If file p
1143f 46 64 20 69 73 20 6f 70 65 6e 2c 20 63 61 6c 6c  Fd is open, call
11440 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b   sqlite3OsUnlock
11441 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61  () on it..*/.sta
11442 74 69 63 20 69 6e 74 20 6f 73 55 6e 6c 6f 63 6b  tic int osUnlock
11443 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
11444 46 64 2c 20 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a  Fd, int eLock){.
11445 20 20 69 66 28 20 21 70 46 64 2d 3e 70 4d 65 74    if( !pFd->pMet
11446 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75  hods ){.    retu
11447 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
11448 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
11449 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 64 2c 20  e3OsUnlock(pFd, 
1144a 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  eLock);.}../*.**
1144b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64   This function d
1144c 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
1144d 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 6f  r or not the ato
1144e 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69  mic-write optimi
1144f 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62 65  zation.** can be
11450 20 75 73 65 64 20 77 69 74 68 20 74 68 69 73 20   used with this 
11451 70 61 67 65 72 2e 20 54 68 65 20 6f 70 74 69 6d  pager. The optim
11452 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75  ization can be u
11453 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 28  sed if:.**.**  (
11454 61 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  a) the value ret
11455 75 72 6e 65 64 20 62 79 20 4f 73 44 65 76 69 63  urned by OsDevic
11456 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
11457 28 29 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  () indicates tha
11458 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 61 74 61  t.**      a data
11459 62 61 73 65 20 70 61 67 65 20 6d 61 79 20 62 65  base page may be
1145a 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61   written atomica
1145b 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 28 62 29  lly, and.**  (b)
1145c 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
1145d 6e 65 64 20 62 79 20 4f 73 53 65 63 74 6f 72 53  ned by OsSectorS
1145e 69 7a 65 28 29 20 69 73 20 6c 65 73 73 20 74 68  ize() is less th
1145f 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 20  an or equal.**  
11460 20 20 20 20 74 6f 20 74 68 65 20 70 61 67 65 20      to the page 
11461 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  size..**.** If t
11462 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
11463 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c 20  cannot be used, 
11464 30 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  0 is returned. I
11465 66 20 69 74 20 63 61 6e 20 62 65 20 75 73 65 64  f it can be used
11466 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61  ,.** then the va
11467 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
11468 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
11469 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65  journal file whe
1146a 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73  n it.** contains
1146b 20 72 6f 6c 6c 62 61 63 6b 20 64 61 74 61 20 66   rollback data f
1146c 6f 72 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 70  or exactly one p
1146d 61 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  age..*/.#ifdef S
1146e 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
1146f 4d 49 43 5f 57 52 49 54 45 0a 73 74 61 74 69 63  MIC_WRITE.static
11470 20 69 6e 74 20 6a 72 6e 6c 42 75 66 66 65 72 53   int jrnlBufferS
11471 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ize(Pager *pPage
11472 72 29 7b 0a 20 20 69 6e 74 20 64 63 3b 20 20 20  r){.  int dc;   
11473 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63          /* Devic
11474 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
11475 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 63 74  s */.  int nSect
11476 6f 72 3b 20 20 20 20 20 20 2f 2a 20 53 65 63 74  or;      /* Sect
11477 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74  or size */.  int
11478 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20   szPage;        
11479 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 2a 2f 0a  /* Page size */.
1147a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1147b 66 64 20 3d 20 70 50 61 67 65 72 2d 3e 66 64 3b  fd = pPager->fd;
1147c 0a 0a 20 20 69 66 28 20 66 64 2d 3e 70 4d 65 74  ..  if( fd->pMet
1147d 68 6f 64 73 20 29 7b 0a 20 20 20 20 64 63 20 3d  hods ){.    dc =
1147e 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65   sqlite3OsDevice
1147f 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
11480 66 64 29 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72  fd);.    nSector
11481 20 3d 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f   = pPager->secto
11482 72 53 69 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67  rSize;.    szPag
11483 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65  e = pPager->page
11484 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  Size;.  }..  ass
11485 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50  ert(SQLITE_IOCAP
11486 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32  _ATOMIC512==(512
11487 3e 3e 38 29 29 3b 0a 20 20 61 73 73 65 72 74 28  >>8));.  assert(
11488 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
11489 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e  MIC64K==(65536>>
1148a 38 29 29 3b 0a 0a 20 20 69 66 28 20 21 66 64 2d  8));..  if( !fd-
1148b 3e 70 4d 65 74 68 6f 64 73 20 7c 7c 20 0a 20 20  >pMethods || .  
1148c 20 20 20 20 20 28 64 63 20 26 20 28 53 51 4c 49       (dc & (SQLI
1148d 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c  TE_IOCAP_ATOMIC|
1148e 28 73 7a 50 61 67 65 3e 3e 38 29 29 20 26 26 20  (szPage>>8)) && 
1148f 6e 53 65 63 74 6f 72 3c 3d 73 7a 50 61 67 65 29  nSector<=szPage)
11490 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 4a   ){.    return J
11491 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
11492 61 67 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f  ager) + JOURNAL_
11493 50 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  PG_SZ(pPager);. 
11494 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
11495 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
11496 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  his function sho
11497 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68  uld be called wh
11498 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  en an error occu
11499 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 70 61  rs within the pa
1149a 67 65 72 0a 2a 2a 20 63 6f 64 65 2e 20 54 68 65  ger.** code. The
1149b 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
1149c 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1149d 74 68 65 20 70 61 67 65 72 20 73 74 72 75 63 74  the pager struct
1149e 75 72 65 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f  ure, the.** seco
1149f 6e 64 20 74 68 65 20 65 72 72 6f 72 2d 63 6f 64  nd the error-cod
114a0 65 20 61 62 6f 75 74 20 74 6f 20 62 65 20 72 65  e about to be re
114a1 74 75 72 6e 65 64 20 62 79 20 61 20 70 61 67 65  turned by a page
114a2 72 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20  r API function. 
114a3 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65  .** The value re
114a4 74 75 72 6e 65 64 20 69 73 20 61 20 63 6f 70 79  turned is a copy
114a5 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   of the second a
114a6 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
114a7 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a  function. .**.**
114a8 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   If the second a
114a9 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54  rgument is SQLIT
114aa 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f  E_IOERR, SQLITE_
114ab 43 4f 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49  CORRUPT, or SQLI
114ac 54 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65  TE_FULL.** the e
114ad 72 72 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72  rror becomes per
114ae 73 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74  sistent. Until t
114af 68 65 20 70 65 72 73 69 73 74 65 6e 20 65 72 72  he persisten err
114b0 6f 72 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a  or is cleared,.*
114b1 2a 20 73 75 62 73 65 71 75 65 6e 74 20 41 50 49  * subsequent API
114b2 20 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50   calls on this P
114b3 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69  ager will immedi
114b4 61 74 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65  ately return the
114b5 20 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20   same .** error 
114b6 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65  code..**.** A pe
114b7 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69  rsistent error i
114b8 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68  ndicates that th
114b9 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
114ba 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a  e pager-cache .*
114bb 2a 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73  * cannot be trus
114bc 74 65 64 2e 20 54 68 69 73 20 73 74 61 74 65 20  ted. This state 
114bd 63 61 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62  can be cleared b
114be 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73  y completely dis
114bf 63 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20  carding .** the 
114c0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
114c1 70 61 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20  pager-cache. If 
114c2 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61  a transaction wa
114c3 73 20 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a  s active when.**
114c4 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20   the persistent 
114c5 65 72 72 6f 72 20 6f 63 63 75 72 65 64 2c 20 74  error occured, t
114c6 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  hen the rollback
114c7 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65   journal may nee
114c8 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c 61  d.** to be repla
114c9 79 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  yed..*/.static v
114ca 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b  oid pager_unlock
114cb 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b  (Pager *pPager);
114cc 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
114cd 72 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70  r_error(Pager *p
114ce 50 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a  Pager, int rc){.
114cf 20 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26    int rc2 = rc &
114d0 20 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28   0xff;.  assert(
114d1 0a 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  .       pPager->
114d2 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f  errCode==SQLITE_
114d3 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70  FULL ||.       p
114d4 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d  Pager->errCode==
114d5 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20  SQLITE_OK ||.   
114d6 20 20 20 20 28 70 50 61 67 65 72 2d 3e 65 72 72      (pPager->err
114d7 43 6f 64 65 20 26 20 30 78 66 66 29 3d 3d 53 51  Code & 0xff)==SQ
114d8 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a  LITE_IOERR.  );.
114d9 20 20 69 66 28 0a 20 20 20 20 72 63 32 3d 3d 53    if(.    rc2==S
114da 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20  QLITE_FULL ||.  
114db 20 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 4f    rc2==SQLITE_IO
114dc 45 52 52 20 7c 7c 0a 20 20 20 20 72 63 32 3d 3d  ERR ||.    rc2==
114dd 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 20  SQLITE_CORRUPT. 
114de 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
114df 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20  errCode = rc;.  
114e0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
114e1 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43  ate==PAGER_UNLOC
114e2 4b 20 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74  K .     && sqlit
114e3 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74  e3PcacheRefCount
114e4 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
114e5 29 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20  )==0 .    ){.   
114e6 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67     /* If the pag
114e7 65 72 20 69 73 20 61 6c 72 65 61 64 79 20 75 6e  er is already un
114e8 6c 6f 63 6b 65 64 2c 20 63 61 6c 6c 20 70 61 67  locked, call pag
114e9 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 6e 6f 77 20  er_unlock() now 
114ea 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61  to.      ** clea
114eb 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  r the error stat
114ec 65 20 61 6e 64 20 65 6e 73 75 72 65 20 74 68 61  e and ensure tha
114ed 74 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68  t the pager-cach
114ee 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 63  e is .      ** c
114ef 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e  ompletely empty.
114f0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
114f1 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61  pager_unlock(pPa
114f2 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ger);.    }.  }.
114f3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
114f4 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  /*.** If SQLITE_
114f5 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64  CHECK_PAGES is d
114f6 65 66 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64  efined then we d
114f7 6f 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68  o some sanity ch
114f8 65 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65  ecking.** on the
114f9 20 63 61 63 68 65 20 75 73 69 6e 67 20 61 20 68   cache using a h
114fa 61 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  ash function.  T
114fb 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
114fc 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64  testing.** and d
114fd 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a  ebugging only..*
114fe 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
114ff 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a  CHECK_PAGES./*.*
11500 2a 20 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69  * Return a 32-bi
11501 74 20 68 61 73 68 20 6f 66 20 74 68 65 20 70 61  t hash of the pa
11502 67 65 20 64 61 74 61 20 66 6f 72 20 70 50 61 67  ge data for pPag
11503 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32  e..*/.static u32
11504 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28   pager_datahash(
11505 69 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67  int nByte, unsig
11506 6e 65 64 20 63 68 61 72 20 2a 70 44 61 74 61 29  ned char *pData)
11507 7b 0a 20 20 75 33 32 20 68 61 73 68 20 3d 20 30  {.  u32 hash = 0
11508 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  ;.  int i;.  for
11509 28 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69  (i=0; i<nByte; i
1150a 2b 2b 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20  ++){.    hash = 
1150b 28 68 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44  (hash*1039) + pD
1150c 61 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65  ata[i];.  }.  re
1150d 74 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61  turn hash;.}.sta
1150e 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 70 61  tic u32 pager_pa
1150f 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50  gehash(PgHdr *pP
11510 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  age){.  return p
11511 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50  ager_datahash(pP
11512 61 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67  age->pPager->pag
11513 65 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64  eSize, (unsigned
11514 20 63 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70   char *)pPage->p
11515 44 61 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20  Data);.}.static 
11516 76 6f 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70  void pager_set_p
11517 61 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70  agehash(PgHdr *p
11518 50 61 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e  Page){.  pPage->
11519 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72  pageHash = pager
1151a 5f 70 61 67 65 68 61 73 68 28 70 50 61 67 65 29  _pagehash(pPage)
1151b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43  ;.}../*.** The C
1151c 48 45 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20  HECK_PAGE macro 
1151d 74 61 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61  takes a PgHdr* a
1151e 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49  s an argument. I
1151f 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
11520 41 47 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e  AGES.** is defin
11521 65 64 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69  ed, and NDEBUG i
11522 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61  s not defined, a
11523 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  n assert() state
11524 6d 65 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74  ment checks.** t
11525 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20  hat the page is 
11526 65 69 74 68 65 72 20 64 69 72 74 79 20 6f 72 20  either dirty or 
11527 73 74 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68  still matches th
11528 65 20 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67  e calculated pag
11529 65 2d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69  e-hash..*/.#defi
1152a 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29  ne CHECK_PAGE(x)
1152b 20 63 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74   checkPage(x).st
1152c 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50  atic void checkP
1152d 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b  age(PgHdr *pPg){
1152e 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
1152f 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
11530 20 20 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e    assert( !pPg->
11531 70 61 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67  pageHash || pPag
11532 65 72 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20  er->errCode.    
11533 20 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73    || (pPg->flags
11534 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c  &PGHDR_DIRTY) ||
11535 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d   pPg->pageHash==
11536 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
11537 50 67 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a  Pg) );.}..#else.
11538 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61  #define pager_da
11539 74 61 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23  tahash(X,Y)  0.#
1153a 64 65 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67  define pager_pag
1153b 65 68 61 73 68 28 58 29 20 20 30 0a 23 64 65 66  ehash(X)  0.#def
1153c 69 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78  ine CHECK_PAGE(x
1153d 29 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c  ).#endif  /* SQL
1153e 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20  ITE_CHECK_PAGES 
1153f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74  */../*.** When t
11540 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68  his is called th
11541 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
11542 6f 72 20 70 61 67 65 72 20 70 50 61 67 65 72 20  or pager pPager 
11543 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a  must be open..**
11544 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   The master jour
11545 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73  nal file name is
11546 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 65   read from the e
11547 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61  nd of the file a
11548 6e 64 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69  nd .** written i
11549 6e 74 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c  nto memory suppl
1154a 69 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65  ied by the calle
1154b 72 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65  r. .**.** zMaste
1154c 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20  r must point to 
1154d 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c  a buffer of at l
1154e 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 74  east nMaster byt
1154f 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a  es allocated by.
11550 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54  ** the caller. T
11551 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 71  his should be sq
11552 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68  lite3_vfs.mxPath
11553 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 72  name+1 (to ensur
11554 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e  e there is.** en
11555 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 72  ough space to wr
11556 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ite the master j
11557 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66  ournal name). If
11558 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11559 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74  nal.** name in t
1155a 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f  he journal is lo
1155b 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 65  nger than nMaste
1155c 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 69  r bytes (includi
1155d 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d  ng a.** nul-term
1155e 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 68  inator), then th
1155f 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 73  is is handled as
11560 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f   if no master jo
11561 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65  urnal name.** we
11562 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68  re present in th
11563 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a  e journal..**.**
11564 20 49 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f   If no master jo
11565 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
11566 69 73 20 70 72 65 73 65 6e 74 20 7a 4d 61 73 74  is present zMast
11567 65 72 5b 30 5d 20 69 73 20 73 65 74 20 74 6f 20  er[0] is set to 
11568 30 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f  0 and.** SQLITE_
11569 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  OK returned..*/.
1156a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4d  static int readM
1156b 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c  asterJournal(sql
1156c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c  ite3_file *pJrnl
1156d 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c  , char *zMaster,
1156e 20 75 33 32 20 6e 4d 61 73 74 65 72 29 7b 0a 20   u32 nMaster){. 
1156f 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 20 6c   int rc;.  u32 l
11570 65 6e 3b 0a 20 20 69 36 34 20 73 7a 4a 3b 0a 20  en;.  i64 szJ;. 
11571 20 75 33 32 20 63 6b 73 75 6d 3b 0a 20 20 75 33   u32 cksum;.  u3
11572 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20  2 u;            
11573 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e         /* Unsign
11574 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  ed loop counter 
11575 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
11576 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 2f 2a  ar aMagic[8]; /*
11577 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c   A buffer to hol
11578 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64  d the magic head
11579 65 72 20 2a 2f 0a 0a 20 20 7a 4d 61 73 74 65 72  er */..  zMaster
1157a 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 72  [0] = '\0';..  r
1157b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
1157c 65 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a  eSize(pJrnl, &sz
1157d 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  J);.  if( rc!=SQ
1157e 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3c 31  LITE_OK || szJ<1
1157f 36 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  6 ) return rc;..
11580 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74    rc = read32bit
11581 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c  s(pJrnl, szJ-16,
11582 20 26 6c 65 6e 29 3b 0a 20 20 69 66 28 20 72 63   &len);.  if( rc
11583 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
11584 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 66 28 20  turn rc;..  if( 
11585 6c 65 6e 3e 3d 6e 4d 61 73 74 65 72 20 29 7b 0a  len>=nMaster ){.
11586 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
11587 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  E_OK;.  }..  rc 
11588 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a 72  = read32bits(pJr
11589 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 6b 73  nl, szJ-12, &cks
1158a 75 6d 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  um);.  if( rc!=S
1158b 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
1158c 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
1158d 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e  lite3OsRead(pJrn
1158e 6c 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a  l, aMagic, 8, sz
1158f 4a 2d 38 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  J-8);.  if( rc!=
11590 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6d 65 6d  SQLITE_OK || mem
11591 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75  cmp(aMagic, aJou
11592 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 20 29 20  rnalMagic, 8) ) 
11593 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
11594 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
11595 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 2c  (pJrnl, zMaster,
11596 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 6e   len, szJ-16-len
11597 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
11598 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
11599 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 7a  turn rc;.  }.  z
1159a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c  Master[len] = '\
1159b 30 27 3b 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66  0';..  /* See if
1159c 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61   the checksum ma
1159d 74 63 68 65 73 20 74 68 65 20 6d 61 73 74 65 72  tches the master
1159e 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f   journal name */
1159f 0a 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65  .  for(u=0; u<le
115a0 6e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73  n; u++){.    cks
115a1 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d  um -= zMaster[u]
115a2 3b 0a 20 20 20 7d 0a 20 20 69 66 28 20 63 6b 73  ;.   }.  if( cks
115a3 75 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  um ){.    /* If 
115a4 74 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65  the checksum doe
115a5 73 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68 65  sn't add up, the
115a6 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66  n one or more of
115a7 20 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72   the disk sector
115a8 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e  s.    ** contain
115a9 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ing the master j
115aa 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20  ournal filename 
115ab 69 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68  is corrupted. Th
115ac 69 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20  is means.    ** 
115ad 64 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20  definitely roll 
115ae 62 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65  back, so just re
115af 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61  turn SQLITE_OK a
115b0 6e 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c  nd report a (nul
115b1 29 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d  ).    ** master-
115b2 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
115b3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61  ..    */.    zMa
115b4 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a  ster[0] = '\0';.
115b5 20 20 7d 0a 20 20 20 0a 20 20 72 65 74 75 72 6e    }.   .  return
115b6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
115b7 2a 0a 2a 2a 20 53 65 65 6b 20 74 68 65 20 6a 6f  *.** Seek the jo
115b8 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72  urnal file descr
115b9 69 70 74 6f 72 20 74 6f 20 74 68 65 20 6e 65 78  iptor to the nex
115ba 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72  t sector boundar
115bb 79 20 77 68 65 72 65 20 61 0a 2a 2a 20 6a 6f 75  y where a.** jou
115bc 72 6e 61 6c 20 68 65 61 64 65 72 20 6d 61 79 20  rnal header may 
115bd 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74  be read or writt
115be 65 6e 2e 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61  en. Pager.journa
115bf 6c 4f 66 66 20 69 73 20 75 70 64 61 74 65 64 20  lOff is updated 
115c0 77 69 74 68 0a 2a 2a 20 74 68 65 20 6e 65 77 20  with.** the new 
115c1 73 65 65 6b 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a  seek offset..**.
115c2 2a 2a 20 69 2e 65 20 66 6f 72 20 61 20 73 65 63  ** i.e for a sec
115c3 74 6f 72 20 73 69 7a 65 20 6f 66 20 35 31 32 3a  tor size of 512:
115c4 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 20 4f 66 66  .**.** Input Off
115c5 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 20  set             
115c6 20 4f 75 74 70 75 74 20 4f 66 66 73 65 74 0a 2a   Output Offset.*
115c7 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
115c8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
115c9 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 30 20 20  ---------.** 0  
115ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
115cb 20 20 20 20 20 20 20 30 0a 2a 2a 20 35 31 32 20         0.** 512 
115cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
115cd 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 31 30 30        512.** 100
115ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
115cf 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 32 30         512.** 20
115d0 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  00              
115d1 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20          2048.** 
115d2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a  .*/.static i64 j
115d3 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28  ournalHdrOffset(
115d4 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
115d5 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30    i64 offset = 0
115d6 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67  ;.  i64 c = pPag
115d7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a  er->journalOff;.
115d8 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f    if( c ){.    o
115d9 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a  ffset = ((c-1)/J
115da 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
115db 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55  ager) + 1) * JOU
115dc 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
115dd 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  er);.  }.  asser
115de 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41  t( offset%JOURNA
115df 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
115e0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
115e1 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20   offset>=c );.  
115e2 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d  assert( (offset-
115e3 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  c)<JOURNAL_HDR_S
115e4 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72  Z(pPager) );.  r
115e5 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a  eturn offset;.}.
115e6 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 65 6b  static void seek
115e7 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72  JournalHdr(Pager
115e8 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 70 50 61   *pPager){.  pPa
115e9 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
115ea 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73  = journalHdrOffs
115eb 65 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f  et(pPager);.}../
115ec 2a 0a 2a 2a 20 57 72 69 74 65 20 7a 65 72 6f 73  *.** Write zeros
115ed 20 6f 76 65 72 20 74 68 65 20 68 65 61 64 65 72   over the header
115ee 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
115ef 66 69 6c 65 2e 20 20 54 68 69 73 20 68 61 73 20  file.  This has 
115f0 74 68 65 0a 2a 2a 20 65 66 66 65 63 74 20 6f 66  the.** effect of
115f1 20 69 6e 76 61 6c 69 64 61 74 69 6e 67 20 74 68   invalidating th
115f2 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
115f3 6e 64 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68  nd committing th
115f4 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  e.** transaction
115f5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
115f6 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50  zeroJournalHdr(P
115f7 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
115f8 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20  t doTruncate){. 
115f9 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
115fa 5f 4f 4b 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  _OK;.  static co
115fb 6e 73 74 20 63 68 61 72 20 7a 65 72 6f 48 64 72  nst char zeroHdr
115fc 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 0a 20 20 69  [28] = {0};..  i
115fd 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
115fe 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 69 36 34  alOff ){.    i64
115ff 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72   iLimit = pPager
11600 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  ->journalSizeLim
11601 69 74 3b 0a 0a 20 20 20 20 49 4f 54 52 41 43 45  it;..    IOTRACE
11602 28 28 22 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e  (("JZEROHDR %p\n
11603 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20  ", pPager)).    
11604 69 66 28 20 64 6f 54 72 75 6e 63 61 74 65 20 7c  if( doTruncate |
11605 7c 20 69 4c 69 6d 69 74 3d 3d 30 20 29 7b 0a 20  | iLimit==0 ){. 
11606 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
11607 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67  3OsTruncate(pPag
11608 65 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20  er->jfd, 0);.   
11609 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
1160a 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
1160b 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a  e(pPager->jfd, z
1160c 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28 7a  eroHdr, sizeof(z
1160d 65 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20 20  eroHdr), 0);.   
1160e 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
1160f 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61  QLITE_OK && !pPa
11610 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20  ger->noSync ){. 
11611 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
11612 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e  3OsSync(pPager->
11613 6a 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43  jfd, SQLITE_SYNC
11614 5f 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72  _DATAONLY|pPager
11615 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20  ->sync_flags);. 
11616 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20     }..    /* At 
11617 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 74  this point the t
11618 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
11619 6d 6d 69 74 74 65 64 20 62 75 74 20 74 68 65 20  mmitted but the 
1161a 77 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20 20  write lock .    
1161b 2a 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c 64  ** is still held
1161c 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 66   on the file. If
1161d 20 74 68 65 72 65 20 69 73 20 61 20 73 69 7a 65   there is a size
1161e 20 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72 65   limit configure
1161f 64 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68  d for .    ** th
11620 65 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75  e persistent jou
11621 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f 75  rnal and the jou
11622 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e  rnal file curren
11623 74 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f 72  tly consumes mor
11624 65 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 74  e.    ** space t
11625 68 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20 61  han that limit a
11626 6c 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e 63  llows for, trunc
11627 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 72  ate it now. Ther
11628 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20  e is no need.   
11629 20 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65 20   ** to sync the 
1162a 66 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  file following t
1162b 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20  his operation.. 
1162c 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63     */.    if( rc
1162d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
1162e 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 20  Limit>0 ){.     
1162f 20 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20 72   i64 sz;.      r
11630 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
11631 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66  eSize(pPager->jf
11632 64 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 69  d, &sz);.      i
11633 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
11634 20 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 7b   && sz>iLimit ){
11635 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
11636 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28  lite3OsTruncate(
11637 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c 69  pPager->jfd, iLi
11638 6d 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  mit);.      }.  
11639 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1163a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
1163b 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d  e journal file m
1163c 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e  ust be open when
1163d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1163e 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e   called. A journ
1163f 61 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a 4f  al.** header (JO
11640 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74  URNAL_HDR_SZ byt
11641 65 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 69  es) is written i
11642 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
11643 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 63  file at the.** c
11644 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e  urrent location.
11645 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61  .**.** The forma
11646 74 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61  t for the journa
11647 6c 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66  l header is as f
11648 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 62  ollows:.** - 8 b
11649 79 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e  ytes: Magic iden
1164a 74 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20  tifying journal 
1164b 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 62  format..** - 4 b
1164c 79 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20  ytes: Number of 
1164d 72 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e  records in journ
1164e 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e  al, or -1 no-syn
1164f 63 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a  c mode is on..**
11650 20 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64   - 4 bytes: Rand
11651 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66  om number used f
11652 6f 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a 2a  or page hash..**
11653 20 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 74   - 4 bytes: Init
11654 69 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67  ial database pag
11655 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20  e count..** - 4 
11656 62 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 69  bytes: Sector si
11657 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 70  ze used by the p
11658 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74  rocess that wrot
11659 65 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a  e this journal..
1165a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 61  ** - 4 bytes: Da
1165b 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65  tabase page size
1165c 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65  ..** .** Followe
1165d 64 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44  d by (JOURNAL_HD
1165e 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73  R_SZ - 28) bytes
1165f 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65   of unused space
11660 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11661 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
11662 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
11663 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
11664 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 48  E_OK;.  char *zH
11665 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e  eader = pPager->
11666 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 75 33 32  pTmpSpace;.  u32
11667 20 6e 48 65 61 64 65 72 20 3d 20 70 50 61 67 65   nHeader = pPage
11668 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 75  r->pageSize;.  u
11669 33 32 20 6e 57 72 69 74 65 3b 0a 20 20 69 6e 74  32 nWrite;.  int
1166a 20 69 69 3b 0a 0a 20 20 69 66 28 20 6e 48 65 61   ii;..  if( nHea
1166b 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  der>JOURNAL_HDR_
1166c 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  SZ(pPager) ){.  
1166d 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f 55 52    nHeader = JOUR
1166e 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
1166f 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  r);.  }..  /* If
11670 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
11671 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64  e savepoints and
11672 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77 65 72   any of them wer
11673 65 20 63 72 65 61 74 65 64 20 73 69 6e 63 65 20  e created since 
11674 74 68 65 0a 20 20 2a 2a 20 6d 6f 73 74 20 72 65  the.  ** most re
11675 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61  cent journal hea
11676 64 65 72 20 77 61 73 20 77 72 69 74 74 65 6e 2c  der was written,
11677 20 75 70 64 61 74 65 20 74 68 65 20 50 61 67 65   update the Page
11678 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f  rSavepoint.iHdrO
11679 66 66 0a 20 20 2a 2a 20 66 69 65 6c 64 73 20 6e  ff.  ** fields n
1167a 6f 77 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ow..  */.  for(i
1167b 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e  i=0; ii<pPager->
1167c 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b  nSavepoint; ii++
1167d 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  ){.    if( pPage
1167e 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69  r->aSavepoint[ii
1167f 5d 2e 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20  ].iHdrOffset==0 
11680 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
11681 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e  >aSavepoint[ii].
11682 69 48 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61  iHdrOffset = pPa
11683 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
11684 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 65  .    }.  }..  se
11685 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  ekJournalHdr(pPa
11686 67 65 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ger);.  pPager->
11687 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 50 61  journalHdr = pPa
11688 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
11689 0a 0a 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64  ..  memcpy(zHead
1168a 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  er, aJournalMagi
1168b 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  c, sizeof(aJourn
1168c 61 6c 4d 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a  alMagic));..  /*
1168d 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65   .  ** Write the
1168e 20 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68   nRec Field - th
1168f 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
11690 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f   records that fo
11691 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a  llow this.  ** j
11692 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e  ournal header. N
11693 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73  ormally, zero is
11694 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73   written to this
11695 20 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 74   value at this t
11696 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20  ime..  ** After 
11697 74 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 20  the records are 
11698 61 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75  added to the jou
11699 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f  rnal (and the jo
1169a 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20  urnal synced, . 
1169b 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73   ** if in full-s
1169c 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a  ync mode), the z
1169d 65 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74  ero is overwritt
1169e 65 6e 20 77 69 74 68 20 74 68 65 20 74 72 75 65  en with the true
1169f 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20   number.  ** of 
116a0 72 65 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e  records (see syn
116a1 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a  cJournal())..  *
116a2 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20  *.  ** A faster 
116a3 61 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20 74  alternative is t
116a4 6f 20 77 72 69 74 65 20 30 78 46 46 46 46 46 46  o write 0xFFFFFF
116a5 46 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66  FF to the nRec f
116a6 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20  ield. When.  ** 
116a7 72 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72  reading the jour
116a8 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74  nal this value t
116a9 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61  ells SQLite to a
116aa 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20  ssume that the. 
116ab 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20   ** rest of the 
116ac 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e  journal file con
116ad 74 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65  tains valid page
116ae 20 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61   records. This a
116af 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69  ssumption.  ** i
116b0 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20  s dangerous, as 
116b1 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63  if a failure occ
116b2 75 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74  ured whilst writ
116b3 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ing to the journ
116b4 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20  al.  ** file it 
116b5 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65  may contain some
116b6 20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54   garbage data. T
116b7 68 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65  here are two sce
116b8 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72  narios.  ** wher
116b9 65 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20  e this risk can 
116ba 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a  be ignored:.  **
116bb 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74  .  **   * When t
116bc 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e  he pager is in n
116bd 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72  o-sync mode. Cor
116be 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c  ruption can foll
116bf 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f  ow a.  **     po
116c0 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74  wer failure in t
116c1 68 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e  his case anyway.
116c2 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57  .  **.  **   * W
116c3 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49  hen the SQLITE_I
116c4 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
116c5 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68   flag is set. Th
116c6 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20  is guarantees.  
116c7 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62  **     that garb
116c8 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65  age data is neve
116c9 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  r appended to th
116ca 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
116cb 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 70 50    */.  assert(pP
116cc 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f  ager->fd->pMetho
116cd 64 73 7c 7c 70 50 61 67 65 72 2d 3e 6e 6f 53 79  ds||pPager->noSy
116ce 6e 63 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67  nc);.  if( (pPag
116cf 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28  er->noSync) || (
116d0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
116d1 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
116d2 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20  ALMODE_MEMORY). 
116d3 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44    || (sqlite3OsD
116d4 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
116d5 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29  tics(pPager->fd)
116d6 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41  &SQLITE_IOCAP_SA
116d7 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b  FE_APPEND) .  ){
116d8 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26  .    put32bits(&
116d9 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
116da 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20  JournalMagic)], 
116db 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 7d  0xffffffff);.  }
116dc 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 32 62  else{.    put32b
116dd 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a  its(&zHeader[siz
116de 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  eof(aJournalMagi
116df 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  c)], 0);.  }..  
116e0 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68  /* The random ch
116e1 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c  eck-hash initial
116e2 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74  iser */ .  sqlit
116e3 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69  e3_randomness(si
116e4 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73  zeof(pPager->cks
116e5 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72  umInit), &pPager
116e6 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20  ->cksumInit);.  
116e7 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64  put32bits(&zHead
116e8 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
116e9 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61  alMagic)+4], pPa
116ea 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b  ger->cksumInit);
116eb 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61  .  /* The initia
116ec 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  l database size 
116ed 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26  */.  put32bits(&
116ee 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
116ef 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d  JournalMagic)+8]
116f0 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  , pPager->dbOrig
116f1 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20  Size);.  /* The 
116f2 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73  assumed sector s
116f3 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f  ize for this pro
116f4 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 62  cess */.  put32b
116f5 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a  its(&zHeader[siz
116f6 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  eof(aJournalMagi
116f7 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e  c)+12], pPager->
116f8 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20  sectorSize);..  
116f9 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 69 6e 67 20  /* Initializing 
116fa 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20  the tail of the 
116fb 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 6e 65  buffer is not ne
116fc 63 65 73 73 61 72 79 2e 20 20 45 76 65 72 79 74  cessary.  Everyt
116fd 68 69 6e 67 0a 20 20 2a 2a 20 77 6f 72 6b 73 20  hing.  ** works 
116fe 66 69 6e 64 20 69 66 20 74 68 65 20 66 6f 6c 6c  find if the foll
116ff 6f 77 69 6e 67 20 6d 65 6d 73 65 74 28 29 20 69  owing memset() i
11700 73 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20  s omitted.  But 
11701 69 6e 69 74 69 61 6c 69 7a 69 6e 67 0a 20 20 2a  initializing.  *
11702 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 72 65  * the memory pre
11703 76 65 6e 74 73 20 76 61 6c 67 72 69 6e 64 20 66  vents valgrind f
11704 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e 69 6e 67 2c  rom complaining,
11705 20 73 6f 20 77 65 20 61 72 65 20 77 69 6c 6c 69   so we are willi
11706 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 61 6b 65 20  ng to.  ** take 
11707 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  the performance 
11708 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73  hit..  */.  mems
11709 65 74 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65  et(&zHeader[size
1170a 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
1170b 29 2b 31 36 5d 2c 20 30 2c 0a 20 20 20 20 20 20  )+16], 0,.      
1170c 20 20 20 6e 48 65 61 64 65 72 2d 28 73 69 7a 65     nHeader-(size
1170d 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
1170e 29 2b 31 36 29 29 3b 0a 0a 20 20 69 66 28 20 70  )+16));..  if( p
1170f 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
11710 72 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54  r==0 ){.    /* T
11711 68 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a  he page size */.
11712 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26 7a      put32bits(&z
11713 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a  Header[sizeof(aJ
11714 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d  ournalMagic)+16]
11715 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
11716 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28  ze);.  }..  for(
11717 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51  nWrite=0; rc==SQ
11718 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c  LITE_OK&&nWrite<
11719 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
1171a 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d  Pager); nWrite+=
1171b 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f  nHeader){.    IO
1171c 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20  TRACE(("JHDR %p 
1171d 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67  %lld %d\n", pPag
1171e 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  er, pPager->jour
1171f 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29  nalHdr, nHeader)
11720 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ).    rc = sqlit
11721 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
11722 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20  ->jfd, zHeader, 
11723 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d  nHeader, pPager-
11724 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20  >journalOff);.  
11725 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11726 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b  lOff += nHeader;
11727 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
11728 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
11729 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73  journal file mus
1172a 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74  t be open when t
1172b 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41  his is called. A
1172c 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
1172d 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c  file.** (JOURNAL
1172e 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69  _HDR_SZ bytes) i
1172f 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  s read from the 
11730 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e  current location
11731 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a   in the journal.
11732 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 72  ** file. The cur
11733 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e  rent location in
11734 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11735 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a  e is given by.**
11736 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11737 4f 66 66 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e  Off.  See commen
11738 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f  ts above functio
11739 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64  n writeJournalHd
1173a 72 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73  r() for.** a des
1173b 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20  cription of the 
1173c 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
1173d 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ormat..**.** If 
1173e 74 68 65 20 68 65 61 64 65 72 20 69 73 20 72 65  the header is re
1173f 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c  ad successfully,
11740 20 2a 6e 52 65 63 20 69 73 20 73 65 74 20 74 6f   *nRec is set to
11741 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
11742 2a 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66  * page records f
11743 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65  ollowing this he
11744 61 64 65 72 20 61 6e 64 20 2a 64 62 53 69 7a 65  ader and *dbSize
11745 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 73   is set to the s
11746 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  ize of the.** da
11747 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74 68  tabase before th
11748 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65  e transaction be
11749 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41  gan, in pages. A
1174a 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73  lso, pPager->cks
1174b 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74  umInit.** is set
1174c 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65   to the value re
1174d 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ad from the jour
1174e 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49  nal header. SQLI
1174f 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
11750 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73  d.** in this cas
11751 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
11752 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
11753 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62  ile appears to b
11754 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c  e corrupted, SQL
11755 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72  ITE_DONE is.** r
11756 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 6e 52 65  eturned and *nRe
11757 63 20 61 6e 64 20 2a 64 62 53 69 7a 65 20 61 72  c and *dbSize ar
11758 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 66  e undefined.  If
11759 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20   JOURNAL_HDR_SZ 
1175a 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20  bytes.** cannot 
1175b 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  be read from the
1175c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e   journal file an
1175d 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
1175e 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
1175f 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75 72 6e  ic int readJourn
11760 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72 20 2a  alHdr(.  Pager *
11761 70 50 61 67 65 72 2c 20 0a 20 20 69 36 34 20 6a  pPager, .  i64 j
11762 6f 75 72 6e 61 6c 53 69 7a 65 2c 0a 20 20 75 33  ournalSize,.  u3
11763 32 20 2a 70 4e 52 65 63 2c 20 0a 20 20 75 33 32  2 *pNRec, .  u32
11764 20 2a 70 44 62 53 69 7a 65 0a 29 7b 0a 20 20 69   *pDbSize.){.  i
11765 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65  nt rc;.  unsigne
11766 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38 5d  d char aMagic[8]
11767 3b 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f  ; /* A buffer to
11768 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20   hold the magic 
11769 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20  header */.  i64 
1176a 6a 72 6e 6c 4f 66 66 3b 0a 20 20 75 33 32 20 69  jrnlOff;.  u32 i
1176b 50 61 67 65 53 69 7a 65 3b 0a 20 20 75 33 32 20  PageSize;.  u32 
1176c 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 0a 20 20  iSectorSize;..  
1176d 73 65 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70  seekJournalHdr(p
1176e 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50  Pager);.  if( pP
1176f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11770 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  +JOURNAL_HDR_SZ(
11771 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61  pPager) > journa
11772 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74  lSize ){.    ret
11773 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
11774 0a 20 20 7d 0a 20 20 6a 72 6e 6c 4f 66 66 20 3d  .  }.  jrnlOff =
11775 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11776 4f 66 66 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c  Off;..  rc = sql
11777 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65  ite3OsRead(pPage
11778 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20  r->jfd, aMagic, 
11779 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c 20  sizeof(aMagic), 
1177a 6a 72 6e 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20  jrnlOff);.  if( 
1177b 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1177c 20 20 6a 72 6e 6c 4f 66 66 20 2b 3d 20 73 69 7a    jrnlOff += siz
1177d 65 6f 66 28 61 4d 61 67 69 63 29 3b 0a 0a 20 20  eof(aMagic);..  
1177e 69 66 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69  if( memcmp(aMagi
1177f 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  c, aJournalMagic
11780 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29  , sizeof(aMagic)
11781 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )!=0 ){.    retu
11782 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
11783 20 20 7d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64    }..  rc = read
11784 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
11785 66 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 70 4e 52  fd, jrnlOff, pNR
11786 65 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  ec);.  if( rc ) 
11787 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
11788 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50   = read32bits(pP
11789 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f  ager->jfd, jrnlO
1178a 66 66 2b 34 2c 20 26 70 50 61 67 65 72 2d 3e 63  ff+4, &pPager->c
1178b 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 69 66 28  ksumInit);.  if(
1178c 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
1178d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62  ..  rc = read32b
1178e 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
1178f 20 6a 72 6e 6c 4f 66 66 2b 38 2c 20 70 44 62 53   jrnlOff+8, pDbS
11790 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ize);.  if( rc )
11791 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69   return rc;..  i
11792 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
11793 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20  alOff==0 ){.    
11794 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
11795 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e  pPager->jfd, jrn
11796 6c 4f 66 66 2b 31 36 2c 20 26 69 50 61 67 65 53  lOff+16, &iPageS
11797 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ize);.    if( rc
11798 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
11799 20 20 20 69 66 28 20 69 50 61 67 65 53 69 7a 65     if( iPageSize
1179a 3c 35 31 32 20 0a 20 20 20 20 20 7c 7c 20 69 50  <512 .     || iP
1179b 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d  ageSize>SQLITE_M
1179c 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 0a 20 20  AX_PAGE_SIZE .  
1179d 20 20 20 7c 7c 20 28 28 69 50 61 67 65 53 69 7a     || ((iPageSiz
1179e 65 2d 31 29 26 69 50 61 67 65 53 69 7a 65 29 21  e-1)&iPageSize)!
1179f 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  =0 .    ){.     
117a0 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 2d   /* If the page-
117a1 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f 75 72  size in the jour
117a2 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 69 6e  nal-header is in
117a3 76 61 6c 69 64 2c 20 74 68 65 6e 20 74 68 65 20  valid, then the 
117a4 70 72 6f 63 65 73 73 0a 20 20 20 20 20 20 2a 2a  process.      **
117a5 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 65 20   that wrote the 
117a6 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d  journal-header m
117a7 75 73 74 20 68 61 76 65 20 63 72 61 73 68 65 64  ust have crashed
117a8 20 62 65 66 6f 72 65 20 74 68 65 20 68 65 61 64   before the head
117a9 65 72 0a 20 20 20 20 20 20 2a 2a 20 77 61 73 20  er.      ** was 
117aa 73 79 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20  synced. In this 
117ab 63 61 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e  case stop readin
117ac 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  g the journal fi
117ad 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a  le here..      *
117ae 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  /.      rc = SQL
117af 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65  ITE_DONE;.    }e
117b0 6c 73 65 7b 0a 20 20 20 20 20 20 75 31 36 20 70  lse{.      u16 p
117b1 61 67 65 73 69 7a 65 20 3d 20 28 75 31 36 29 69  agesize = (u16)i
117b2 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20  PageSize;.      
117b3 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
117b4 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61  rSetPagesize(pPa
117b5 67 65 72 2c 20 26 70 61 67 65 73 69 7a 65 29 3b  ger, &pagesize);
117b6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
117b7 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
117b8 70 61 67 65 73 69 7a 65 3d 3d 28 75 31 36 29 69  pagesize==(u16)i
117b9 50 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20  PageSize );.    
117ba 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  }.    if( rc ) r
117bb 65 74 75 72 6e 20 72 63 3b 0a 20 20 0a 20 20 20  eturn rc;.  .   
117bc 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61   /* Update the a
117bd 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69  ssumed sector-si
117be 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20  ze to match the 
117bf 76 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20  value used by . 
117c0 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73     ** the proces
117c1 73 20 74 68 61 74 20 63 72 65 61 74 65 64 20 74  s that created t
117c2 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20  his journal. If 
117c3 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73  this journal was
117c4 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20  .    ** created 
117c5 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74 68  by a process oth
117c6 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e 65  er than this one
117c7 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
117c8 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 62 65  ine.    ** is be
117c9 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20  ing called from 
117ca 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70 6c 61  within pager_pla
117cb 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63  yback(). The loc
117cc 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20  al value.    ** 
117cd 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53  of Pager.sectorS
117ce 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65 64 20  ize is restored 
117cf 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
117d0 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20 20  at routine..    
117d1 2a 2f 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64  */.    rc = read
117d2 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
117d3 66 64 2c 20 6a 72 6e 6c 4f 66 66 2b 31 32 2c 20  fd, jrnlOff+12, 
117d4 26 69 53 65 63 74 6f 72 53 69 7a 65 29 3b 0a 20  &iSectorSize);. 
117d5 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
117d6 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 28  rn rc;.    if( (
117d7 69 53 65 63 74 6f 72 53 69 7a 65 26 28 69 53 65  iSectorSize&(iSe
117d8 63 74 6f 72 53 69 7a 65 2d 31 29 29 0a 20 20 20  ctorSize-1)).   
117d9 20 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a     || iSectorSiz
117da 65 3c 35 31 32 0a 20 20 20 20 20 20 7c 7c 20 69  e<512.      || i
117db 53 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53  SectorSize>MAX_S
117dc 45 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 29  ECTOR_SIZE.    )
117dd 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
117de 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
117df 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65  }.    pPager->se
117e0 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 65 63 74  ctorSize = iSect
117e1 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70  orSize;.  }..  p
117e2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
117e3 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  f += JOURNAL_HDR
117e4 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 72  _SZ(pPager);.  r
117e5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
117e6 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  .}.../*.** Write
117e7 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6d 61   the supplied ma
117e8 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
117e9 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  e into the journ
117ea 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65  al file for page
117eb 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 20 74  r.** pPager at t
117ec 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  he current locat
117ed 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 72 20  ion. The master 
117ee 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d 75 73  journal name mus
117ef 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a 2a 2a  t be the last.**
117f0 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e 20 74   thing written t
117f1 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  o a journal file
117f2 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  . If the pager i
117f3 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d  s in full-sync m
117f4 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f 75 72  ode, the.** jour
117f5 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
117f6 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 64 20  tor is advanced 
117f7 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 74  to the next sect
117f8 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 66 6f  or boundary befo
117f9 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 69  re.** anything i
117fa 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 20 66  s written. The f
117fb 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20  ormat is:.**.** 
117fc 2b 20 34 20 62 79 74 65 73 3a 20 50 41 47 45 52  + 4 bytes: PAGER
117fd 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 2b 20 4e  _MJ_PGNO..** + N
117fe 20 62 79 74 65 73 3a 20 6c 65 6e 67 74 68 20 6f   bytes: length o
117ff 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  f master journal
11800 20 6e 61 6d 65 2e 0a 2a 2a 20 2b 20 34 20 62 79   name..** + 4 by
11801 74 65 73 3a 20 4e 0a 2a 2a 20 2b 20 34 20 62 79  tes: N.** + 4 by
11802 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72  tes: Master jour
11803 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75  nal name checksu
11804 6d 2e 0a 2a 2a 20 2b 20 38 20 62 79 74 65 73 3a  m..** + 8 bytes:
11805 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d   aJournalMagic[]
11806 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74  ..**.** The mast
11807 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20  er journal page 
11808 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68 65 20  checksum is the 
11809 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74 65 73  sum of the bytes
1180a 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 0a 2a   in the master.*
1180b 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 0a  * journal name..
1180c 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72  **.** If zMaster
1180d 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
1180e 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61  er (occurs for a
1180f 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
11810 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a   transaction), .
11811 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20  ** this call is 
11812 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74  a no-op..*/.stat
11813 69 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74  ic int writeMast
11814 65 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20  erJournal(Pager 
11815 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63  *pPager, const c
11816 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20  har *zMaster){. 
11817 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6c   int rc;.  int l
11818 65 6e 3b 20 0a 20 20 69 6e 74 20 69 3b 20 0a 20  en; .  int i; . 
11819 20 69 36 34 20 6a 72 6e 6c 4f 66 66 3b 0a 20 20   i64 jrnlOff;.  
1181a 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 0a 20 20  i64 jrnlSize;.  
1181b 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b 0a 20  u32 cksum = 0;. 
1181c 20 63 68 61 72 20 7a 42 75 66 5b 73 69 7a 65 6f   char zBuf[sizeo
1181d 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
1181e 2b 32 2a 34 5d 3b 0a 0a 20 20 69 66 28 20 21 7a  +2*4];..  if( !z
1181f 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72  Master || pPager
11820 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 20 72 65  ->setMaster ) re
11821 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
11822 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f    if( pPager->jo
11823 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
11824 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
11825 4f 52 59 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ORY ) return SQL
11826 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72  ITE_OK;.  pPager
11827 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 31 3b  ->setMaster = 1;
11828 0a 0a 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65  ..  len = sqlite
11829 33 53 74 72 6c 65 6e 33 30 28 7a 4d 61 73 74 65  3Strlen30(zMaste
1182a 72 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  r);.  for(i=0; i
1182b 3c 6c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  <len; i++){.    
1182c 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72  cksum += zMaster
1182d 5b 69 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  [i];.  }..  /* I
1182e 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d  f in full-sync m
1182f 6f 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20  ode, advance to 
11830 74 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65  the next disk se
11831 63 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74  ctor before writ
11832 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 73  ing.  ** the mas
11833 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
11834 2e 20 54 68 69 73 20 69 73 20 69 6e 20 63 61 73  . This is in cas
11835 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70  e the previous p
11836 61 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 20  age written to. 
11837 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   ** the journal 
11838 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
11839 20 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 20   synced..  */.  
1183a 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c  if( pPager->full
1183b 53 79 6e 63 20 29 7b 0a 20 20 20 20 73 65 65 6b  Sync ){.    seek
1183c 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65  JournalHdr(pPage
1183d 72 29 3b 0a 20 20 7d 0a 20 20 6a 72 6e 6c 4f 66  r);.  }.  jrnlOf
1183e 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  f = pPager->jour
1183f 6e 61 6c 4f 66 66 3b 0a 20 20 70 50 61 67 65 72  nalOff;.  pPager
11840 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20  ->journalOff += 
11841 28 6c 65 6e 2b 32 30 29 3b 0a 0a 20 20 72 63 20  (len+20);..  rc 
11842 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50  = write32bits(pP
11843 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f  ager->jfd, jrnlO
11844 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  ff, PAGER_MJ_PGN
11845 4f 28 70 50 61 67 65 72 29 29 3b 0a 20 20 69 66  O(pPager));.  if
11846 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
11847 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6a  ) return rc;.  j
11848 72 6e 6c 4f 66 66 20 2b 3d 20 34 3b 0a 0a 20 20  rnlOff += 4;..  
11849 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
1184a 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ite(pPager->jfd,
1184b 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 6a   zMaster, len, j
1184c 72 6e 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20 72  rnlOff);.  if( r
1184d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
1184e 65 74 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c  eturn rc;.  jrnl
1184f 4f 66 66 20 2b 3d 20 6c 65 6e 3b 0a 0a 20 20 70  Off += len;..  p
11850 75 74 33 32 62 69 74 73 28 7a 42 75 66 2c 20 6c  ut32bits(zBuf, l
11851 65 6e 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73  en);.  put32bits
11852 28 26 7a 42 75 66 5b 34 5d 2c 20 63 6b 73 75 6d  (&zBuf[4], cksum
11853 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  );.  memcpy(&zBu
11854 66 5b 38 5d 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61  f[8], aJournalMa
11855 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75  gic, sizeof(aJou
11856 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 72  rnalMagic));.  r
11857 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
11858 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
11859 7a 42 75 66 2c 20 38 2b 73 69 7a 65 6f 66 28 61  zBuf, 8+sizeof(a
1185a 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2c 20 6a  JournalMagic), j
1185b 72 6e 6c 4f 66 66 29 3b 0a 20 20 6a 72 6e 6c 4f  rnlOff);.  jrnlO
1185c 66 66 20 2b 3d 20 38 2b 73 69 7a 65 6f 66 28 61  ff += 8+sizeof(a
1185d 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 20  JournalMagic);. 
1185e 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
1185f 63 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53  c = !pPager->noS
11860 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ync;..  /* If th
11861 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 70 65  e pager is in pe
11862 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20  ristent-journal 
11863 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70  mode, then the p
11864 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f  hysical .  ** jo
11865 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65  urnal-file may e
11866 78 74 65 6e 64 20 70 61 73 74 20 74 68 65 20 65  xtend past the e
11867 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  nd of the master
11868 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20  -journal name.  
11869 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f  ** and 8 bytes o
1186a 66 20 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73  f magic data jus
1186b 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  t written to the
1186c 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a   file. This is .
1186d 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62    ** dangerous b
1186e 65 63 61 75 73 65 20 74 68 65 20 63 6f 64 65 20  ecause the code 
1186f 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f  to rollback a ho
11870 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20  t-journal file. 
11871 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20   ** will not be 
11872 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65  able to find the
11873 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20   master-journal 
11874 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e  name to determin
11875 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20  e .  ** whether 
11876 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e  or not the journ
11877 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a  al is hot. .  **
11878 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68  .  ** Easiest th
11879 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69  ing to do in thi
1187a 73 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f  s scenario is to
1187b 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f   truncate the jo
1187c 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65  urnal .  ** file
1187d 20 74 6f 20 74 68 65 20 72 65 71 75 69 72 65 64   to the required
1187e 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69   size..  */ .  i
1187f 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  f( (rc==SQLITE_O
11880 4b 29 0a 20 20 20 26 26 20 28 72 63 20 3d 20 73  K).   && (rc = s
11881 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
11882 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a  (pPager->jfd, &j
11883 72 6e 6c 53 69 7a 65 29 29 3d 3d 53 51 4c 49 54  rnlSize))==SQLIT
11884 45 5f 4f 4b 0a 20 20 20 26 26 20 6a 72 6e 6c 53  E_OK.   && jrnlS
11885 69 7a 65 3e 6a 72 6e 6c 4f 66 66 0a 20 20 29 7b  ize>jrnlOff.  ){
11886 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11887 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67  3OsTruncate(pPag
11888 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66  er->jfd, jrnlOff
11889 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1188a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  rc;.}../*.** Fin
1188b 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20  d a page in the 
1188c 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e  hash table given
1188d 20 69 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72   its page number
1188e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70  .  Return.** a p
1188f 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61  ointer to the pa
11890 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6e 6f  ge or NULL if no
11891 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74  t found..*/.stat
11892 69 63 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f  ic PgHdr *pager_
11893 6c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50  lookup(Pager *pP
11894 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  ager, Pgno pgno)
11895 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20  {.  PgHdr *p;.  
11896 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
11897 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ch(pPager->pPCac
11898 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29  he, pgno, 0, &p)
11899 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
1189a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
1189b 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65   in-memory cache
1189c 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a  .  This routine.
1189d 2a 2a 20 73 65 74 73 20 74 68 65 20 73 74 61 74  ** sets the stat
1189e 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 62  e of the pager b
1189f 61 63 6b 20 74 6f 20 77 68 61 74 20 69 74 20 77  ack to what it w
118a0 61 73 20 77 68 65 6e 20 69 74 20 77 61 73 20 66  as when it was f
118a1 69 72 73 74 0a 2a 2a 20 6f 70 65 6e 65 64 2e 20  irst.** opened. 
118a2 20 41 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67   Any outstanding
118a3 20 70 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c   pages are inval
118a4 69 64 61 74 65 64 20 61 6e 64 20 73 75 62 73 65  idated and subse
118a5 71 75 65 6e 74 20 61 74 74 65 6d 70 74 73 0a 2a  quent attempts.*
118a6 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 6f 73  * to access thos
118a7 65 20 70 61 67 65 73 20 77 69 6c 6c 20 6c 69 6b  e pages will lik
118a8 65 6c 79 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ely result in a 
118a9 63 6f 72 65 64 75 6d 70 2e 0a 2a 2f 0a 73 74 61  coredump..*/.sta
118aa 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 72  tic void pager_r
118ab 65 73 65 74 28 50 61 67 65 72 20 2a 70 50 61 67  eset(Pager *pPag
118ac 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65  er){.  if( pPage
118ad 72 2d 3e 65 72 72 43 6f 64 65 20 29 20 72 65 74  r->errCode ) ret
118ae 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 50 63  urn;.  sqlite3Pc
118af 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 65 72  acheClear(pPager
118b0 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f  ->pPCache);.}../
118b1 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 73 74  *.** Free all st
118b2 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 65 20  ructures in the 
118b3 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74  Pager.aSavepoint
118b4 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 73 65 74  [] array and set
118b5 20 62 6f 74 68 0a 2a 2a 20 50 61 67 65 72 2e 61   both.** Pager.a
118b6 53 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 50 61  Savepoint and Pa
118b7 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 20 74  ger.nSavepoint t
118b8 6f 20 7a 65 72 6f 2e 20 43 6c 6f 73 65 20 74 68  o zero. Close th
118b9 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a  e sub-journal.**
118ba 20 69 66 20 69 74 20 69 73 20 6f 70 65 6e 20 61   if it is open a
118bb 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73 20  nd the pager is 
118bc 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65  not in exclusive
118bd 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   mode..*/.static
118be 20 76 6f 69 64 20 72 65 6c 65 61 73 65 41 6c 6c   void releaseAll
118bf 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20  Savepoint(Pager 
118c0 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20  *pPager){.  int 
118c1 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ii;.  for(ii=0; 
118c2 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65  ii<pPager->nSave
118c3 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20  point; ii++){.  
118c4 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
118c5 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61  estroy(pPager->a
118c6 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49  Savepoint[ii].pI
118c7 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d  nSavepoint);.  }
118c8 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
118c9 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b  exclusiveMode ){
118ca 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
118cb 6f 73 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  ose(pPager->sjfd
118cc 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
118cd 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61 53  _free(pPager->aS
118ce 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 70 50 61  avepoint);.  pPa
118cf 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20  ger->aSavepoint 
118d0 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  = 0;.  pPager->n
118d1 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20  Savepoint = 0;. 
118d2 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65   pPager->stmtNRe
118d3 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  c = 0;.}../*.** 
118d4 53 65 74 20 74 68 65 20 62 69 74 20 6e 75 6d 62  Set the bit numb
118d5 65 72 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 50  er pgno in the P
118d6 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49  agerSavepoint.pI
118d7 6e 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65  nSavepoint bitve
118d8 63 73 20 6f 66 0a 2a 2a 20 61 6c 6c 20 6f 70 65  cs of.** all ope
118d9 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 0a 2a 2f  n savepoints..*/
118da 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54  .static int addT
118db 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63  oSavepointBitvec
118dc 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  s(Pager *pPager,
118dd 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69   Pgno pgno){.  i
118de 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
118df 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
118e0 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
118e1 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
118e2 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c  ;       /* Resul
118e3 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 66 6f 72  t code */..  for
118e4 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72  (ii=0; ii<pPager
118e5 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69  ->nSavepoint; ii
118e6 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 53 61  ++){.    PagerSa
118e7 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50  vepoint *p = &pP
118e8 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
118e9 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 67  [ii];.    if( pg
118ea 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a  no<=p->nOrig ){.
118eb 20 20 20 20 20 20 72 63 20 7c 3d 20 73 71 6c 69        rc |= sqli
118ec 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2d 3e  te3BitvecSet(p->
118ed 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70 67  pInSavepoint, pg
118ee 6e 6f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  no);.      asser
118ef 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
118f0 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   || rc==SQLITE_N
118f1 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20  OMEM );.    }.  
118f2 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
118f3 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74  ../*.** Unlock t
118f4 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
118f5 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  . .**.** If the 
118f6 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74  pager is current
118f7 6c 79 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74  ly in error stat
118f8 65 2c 20 64 69 73 63 61 72 64 20 74 68 65 20 63  e, discard the c
118f9 6f 6e 74 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 74  ontents of .** t
118fa 68 65 20 63 61 63 68 65 20 61 6e 64 20 72 65 73  he cache and res
118fb 65 74 20 74 68 65 20 50 61 67 65 72 20 73 74 72  et the Pager str
118fc 75 63 74 75 72 65 20 69 6e 74 65 72 6e 61 6c 20  ucture internal 
118fd 73 74 61 74 65 2e 20 49 66 20 74 68 65 72 65 20  state. If there 
118fe 69 73 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a 6f  is.** an open jo
118ff 75 72 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 65 6e  urnal-file, then
11900 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 61   the next time a
11901 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20   shared-lock is 
11902 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74  obtained.** on t
11903 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 28 62  he pager file (b
11904 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74  y this or any ot
11905 68 65 72 20 70 72 6f 63 65 73 73 29 2c 20 69 74  her process), it
11906 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 61   will be.** trea
11907 74 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75  ted as a hot-jou
11908 72 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20  rnal and rolled 
11909 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  back..*/.static 
1190a 76 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63  void pager_unloc
1190b 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  k(Pager *pPager)
1190c 7b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d  {.  if( !pPager-
1190d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29  >exclusiveMode )
1190e 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 0a 20  {.    int rc;.. 
1190f 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 63 6c 6f     /* Always clo
11910 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  se the journal f
11911 69 6c 65 20 77 68 65 6e 20 64 72 6f 70 70 69 6e  ile when droppin
11912 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  g the database l
11913 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65  ock..    ** Othe
11914 72 77 69 73 65 2c 20 61 6e 6f 74 68 65 72 20 63  rwise, another c
11915 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 6a  onnection with j
11916 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65  ournal_mode=dele
11917 74 65 20 6d 69 67 68 74 0a 20 20 20 20 2a 2a 20  te might.    ** 
11918 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20  delete the file 
11919 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75  out from under u
1191a 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
1191b 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
1191c 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 73  lOpen ){.      s
1191d 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
1191e 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20  ager->jfd);.    
1191f 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11920 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  lOpen = 0;.     
11921 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
11922 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49  stroy(pPager->pI
11923 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20  nJournal);.     
11924 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
11925 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 73  nal = 0;.      s
11926 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
11927 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 41 6c 77  roy(pPager->pAlw
11928 61 79 73 52 6f 6c 6c 62 61 63 6b 29 3b 0a 20 20  aysRollback);.  
11929 20 20 20 20 70 50 61 67 65 72 2d 3e 70 41 6c 77      pPager->pAlw
1192a 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 30 3b  aysRollback = 0;
1192b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d  .    }..    rc =
1192c 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72   osUnlock(pPager
1192d 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  ->fd, NO_LOCK);.
1192e 20 20 20 20 69 66 28 20 72 63 20 29 20 70 50 61      if( rc ) pPa
1192f 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72  ger->errCode = r
11930 63 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64  c;.    pPager->d
11931 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a  bSizeValid = 0;.
11932 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 55 4e      IOTRACE(("UN
11933 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61 67  LOCK %p\n", pPag
11934 65 72 29 29 0a 0a 20 20 20 20 2f 2a 20 49 66 20  er))..    /* If 
11935 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 73  Pager.errCode is
11936 20 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e   set, the conten
11937 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 20  ts of the pager 
11938 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a  cache cannot be.
11939 20 20 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20      ** trusted. 
1193a 4e 6f 77 20 74 68 61 74 20 74 68 65 20 70 61 67  Now that the pag
1193b 65 72 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63  er file is unloc
1193c 6b 65 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74  ked, the content
1193d 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  s of the.    ** 
1193e 63 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 73  cache can be dis
1193f 63 61 72 64 65 64 20 61 6e 64 20 74 68 65 20 65  carded and the e
11940 72 72 6f 72 20 63 6f 64 65 20 73 61 66 65 6c 79  rror code safely
11941 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f   cleared..    */
11942 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
11943 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20  >errCode ){.    
11944 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
11945 5f 4f 4b 20 29 20 70 50 61 67 65 72 2d 3e 65 72  _OK ) pPager->er
11946 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f  rCode = SQLITE_O
11947 4b 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72  K;.      pager_r
11948 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20  eset(pPager);.  
11949 20 20 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61      releaseAllSa
1194a 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 29 3b  vepoint(pPager);
1194b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  .      pPager->j
1194c 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20  ournalOff = 0;. 
1194d 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
1194e 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b  rnalStarted = 0;
1194f 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64  .      pPager->d
11950 62 4f 72 69 67 53 69 7a 65 20 3d 20 30 3b 0a 20  bOrigSize = 0;. 
11951 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 72     }..    pPager
11952 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
11953 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 70 50 61 67  UNLOCK;.    pPag
11954 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44  er->changeCountD
11955 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  one = 0;.  }.}..
11956 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 20  /*.** Execute a 
11957 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 72  rollback if a tr
11958 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
11959 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74  ive and unlock t
1195a 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  he .** database 
1195b 66 69 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67  file. If the pag
1195c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 65  er has already e
1195d 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72  ntered the error
1195e 20 73 74 61 74 65 2c 20 0a 2a 2a 20 64 6f 20 6e   state, .** do n
1195f 6f 74 20 61 74 74 65 6d 70 74 20 74 68 65 20 72  ot attempt the r
11960 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74  ollback..*/.stat
11961 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c  ic void pagerUnl
11962 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 50  ockAndRollback(P
11963 61 67 65 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  ager *p){.  if( 
11964 70 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49  p->errCode==SQLI
11965 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 73 74 61 74  TE_OK && p->stat
11966 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45  e>=PAGER_RESERVE
11967 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  D ){.    sqlite3
11968 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
11969 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  c();.    sqlite3
1196a 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 29  PagerRollback(p)
1196b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64  ;.    sqlite3End
1196c 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
1196d 20 20 7d 0a 20 20 70 61 67 65 72 5f 75 6e 6c 6f    }.  pager_unlo
1196e 63 6b 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ck(p);.}../*.** 
1196f 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 64  This routine end
11970 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  s a transaction.
11971 20 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20    A transaction 
11972 69 73 20 65 6e 64 65 64 20 62 79 20 65 69 74 68  is ended by eith
11973 65 72 0a 2a 2a 20 61 20 43 4f 4d 4d 49 54 20 6f  er.** a COMMIT o
11974 72 20 61 20 52 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a  r a ROLLBACK..**
11975 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f  .** When this ro
11976 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
11977 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 74   the pager has t
11978 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11979 6f 70 65 6e 20 61 6e 64 0a 2a 2a 20 61 20 52 45  open and.** a RE
1197a 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53  SERVED or EXCLUS
1197b 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  IVE lock on the 
1197c 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20  database.  This 
1197d 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 6c  routine will rel
1197e 65 61 73 65 0a 2a 2a 20 74 68 65 20 64 61 74 61  ease.** the data
1197f 62 61 73 65 20 6c 6f 63 6b 20 61 6e 64 20 61 63  base lock and ac
11980 71 75 69 72 65 73 20 61 20 53 48 41 52 45 44 20  quires a SHARED 
11981 6c 6f 63 6b 20 69 6e 20 69 74 73 20 70 6c 61 63  lock in its plac
11982 65 20 69 66 20 74 68 61 74 20 69 73 0a 2a 2a 20  e if that is.** 
11983 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
11984 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 52 65  thing to do.  Re
11985 6c 65 61 73 65 20 6c 6f 63 6b 73 20 75 73 75 61  lease locks usua
11986 6c 6c 79 20 69 73 20 61 70 70 72 6f 70 72 69 61  lly is appropria
11987 74 65 2c 0a 2a 2a 20 75 6e 6c 65 73 73 20 77 65  te,.** unless we
11988 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69 76   are in exclusiv
11989 65 20 61 63 63 65 73 73 20 6d 6f 64 65 20 6f 72  e access mode or
1198a 20 75 6e 6c 65 73 73 20 74 68 69 73 20 69 73 20   unless this is 
1198b 61 20 0a 2a 2a 20 43 4f 4d 4d 49 54 20 41 4e 44  a .** COMMIT AND
1198c 20 42 45 47 49 4e 20 6f 72 20 52 4f 4c 4c 42 41   BEGIN or ROLLBA
1198d 43 4b 20 41 4e 44 20 42 45 47 49 4e 20 6f 70 65  CK AND BEGIN ope
1198e 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ration..**.** Th
1198f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
11990 73 20 65 69 74 68 65 72 20 64 65 6c 65 74 65 64  s either deleted
11991 20 6f 72 20 74 72 75 6e 63 61 74 65 64 2e 0a 2a   or truncated..*
11992 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 43 6f 6e 73 69  *.** TODO: Consi
11993 64 65 72 20 6b 65 65 70 69 6e 67 20 74 68 65 20  der keeping the 
11994 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 70 65  journal file ope
11995 6e 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20  n for temporary 
11996 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 20 54 68  databases..** Th
11997 69 73 20 6d 69 67 68 74 20 67 69 76 65 20 61 20  is might give a 
11998 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70 72  performance impr
11999 6f 76 65 6d 65 6e 74 20 6f 6e 20 77 69 6e 64 6f  ovement on windo
1199a 77 73 20 77 68 65 72 65 20 6f 70 65 6e 69 6e 67  ws where opening
1199b 0a 2a 2a 20 61 20 66 69 6c 65 20 69 73 20 61 6e  .** a file is an
1199c 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65 72 61   expensive opera
1199d 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
1199e 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 5f 74 72  int pager_end_tr
1199f 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 65 72 20  ansaction(Pager 
119a0 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 68 61 73  *pPager, int has
119a1 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72  Master){.  int r
119a2 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
119a3 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54   int rc2 = SQLIT
119a4 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67  E_OK;.  if( pPag
119a5 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f  er->state<PAGER_
119a6 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20  RESERVED ){.    
119a7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
119a8 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 41  ;.  }.  releaseA
119a9 6c 6c 53 61 76 65 70 6f 69 6e 74 28 70 50 61 67  llSavepoint(pPag
119aa 65 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  er);.  if( pPage
119ab 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29  r->journalOpen )
119ac 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  {.    if( pPager
119ad 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
119ae 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
119af 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20  _MEMORY ){.     
119b0 20 69 6e 74 20 69 73 4d 65 6d 6f 72 79 4a 6f 75   int isMemoryJou
119b1 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 49 73  rnal = sqlite3Is
119b2 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  MemJournal(pPage
119b3 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 73  r->jfd);.      s
119b4 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
119b5 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20  ager->jfd);.    
119b6 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
119b7 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  lOpen = 0;.     
119b8 20 69 66 28 20 21 69 73 4d 65 6d 6f 72 79 4a 6f   if( !isMemoryJo
119b9 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 20  urnal ){.       
119ba 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44   rc = sqlite3OsD
119bb 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56  elete(pPager->pV
119bc 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  fs, pPager->zJou
119bd 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20  rnal, 0);.      
119be 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  }.    }else if( 
119bf 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
119c0 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
119c1 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a  ALMODE_TRUNCATE.
119c2 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63 20           && (rc 
119c3 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
119c4 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ate(pPager->jfd,
119c5 20 30 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20   0))==SQLITE_OK 
119c6 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
119c7 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b  >journalOff = 0;
119c8 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  .      pPager->j
119c9 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20  ournalStarted = 
119ca 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  0;.    }else if(
119cb 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
119cc 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c 20  veMode .     || 
119cd 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
119ce 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
119cf 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20  ALMODE_PERSIST. 
119d0 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d     ){.      rc =
119d1 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28   zeroJournalHdr(
119d2 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74 65  pPager, hasMaste
119d3 72 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f  r);.      pager_
119d4 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63  error(pPager, rc
119d5 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  );.      pPager-
119d6 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b  >journalOff = 0;
119d7 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  .      pPager->j
119d8 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20  ournalStarted = 
119d9 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
119da 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
119db 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
119dc 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
119dd 44 45 5f 44 45 4c 45 54 45 20 7c 7c 20 72 63 20  DE_DELETE || rc 
119de 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
119df 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
119e0 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 50 61 67  jfd);.      pPag
119e1 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
119e2 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72  = 0;.      if( r
119e3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
119e4 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
119e5 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  e ){.        rc 
119e6 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74  = sqlite3OsDelet
119e7 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20  e(pPager->pVfs, 
119e8 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
119e9 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
119ea 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42    }.    sqlite3B
119eb 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
119ec 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29  ger->pInJournal)
119ed 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49  ;.    pPager->pI
119ee 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20  nJournal = 0;.  
119ef 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
119f0 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70  estroy(pPager->p
119f1 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 29 3b  AlwaysRollback);
119f2 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 41 6c  .    pPager->pAl
119f3 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 30  waysRollback = 0
119f4 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
119f5 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20  CHECK_PAGES.    
119f6 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65  sqlite3PcacheIte
119f7 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72  rateDirty(pPager
119f8 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72  ->pPCache, pager
119f9 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a  _set_pagehash);.
119fa 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
119fb 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c  e3PcacheCleanAll
119fc 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
119fd 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64  );.    pPager->d
119fe 69 72 74 79 43 61 63 68 65 20 3d 20 30 3b 0a 20  irtyCache = 0;. 
119ff 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20     pPager->nRec 
11a00 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
11a01 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
11a02 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->pInJournal==0 
11a03 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70  );.  }..  if( !p
11a04 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
11a05 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32 20  Mode ){.    rc2 
11a06 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65  = osUnlock(pPage
11a07 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f  r->fd, SHARED_LO
11a08 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  CK);.    pPager-
11a09 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53  >state = PAGER_S
11a0a 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67 65  HARED;.    pPage
11a0b 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
11a0c 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  ne = 0;.  }else 
11a0d 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
11a0e 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20  e==PAGER_SYNCED 
11a0f 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  ){.    pPager->s
11a10 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43  tate = PAGER_EXC
11a11 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70 50  LUSIVE;.  }.  pP
11a12 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
11a13 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e   = 0;.  pPager->
11a14 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20  setMaster = 0;. 
11a15 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
11a16 63 20 3d 20 30 3b 0a 20 20 2f 2a 20 6c 72 75 4c  c = 0;.  /* lruL
11a17 69 73 74 53 65 74 46 69 72 73 74 53 79 6e 63 65  istSetFirstSynce
11a18 64 28 70 50 61 67 65 72 29 3b 20 2a 2f 0a 20 20  d(pPager); */.  
11a19 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75  sqlite3PcacheTru
11a1a 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50  ncate(pPager->pP
11a1b 43 61 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64  Cache, pPager->d
11a1c 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 4d  bSize);.  if( !M
11a1d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61 67  EMDB ){.    pPag
11a1e 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20  er->dbSizeValid 
11a1f 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  = 0;.  }.  pPage
11a20 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20  r->dbModified = 
11a21 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63  0;..  return (rc
11a22 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a  ==SQLITE_OK?rc2:
11a23 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  rc);.}../*.** Co
11a24 6d 70 75 74 65 20 61 6e 64 20 72 65 74 75 72 6e  mpute and return
11a25 20 61 20 63 68 65 63 6b 73 75 6d 20 66 6f 72 20   a checksum for 
11a26 74 68 65 20 70 61 67 65 20 6f 66 20 64 61 74 61  the page of data
11a27 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
11a28 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b  not a real check
11a29 73 75 6d 2e 20 20 49 74 20 69 73 20 72 65 61 6c  sum.  It is real
11a2a 6c 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20  ly just the sum 
11a2b 6f 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f  of the .** rando
11a2c 6d 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20  m initial value 
11a2d 61 6e 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d  and the page num
11a2e 62 65 72 2e 20 20 57 65 20 65 78 70 65 72 69 6d  ber.  We experim
11a2f 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20  ented with.** a 
11a30 63 68 65 63 6b 73 75 6d 20 6f 66 20 74 68 65 20  checksum of the 
11a31 65 6e 74 69 72 65 20 64 61 74 61 2c 20 62 75 74  entire data, but
11a32 20 74 68 61 74 20 77 61 73 20 66 6f 75 6e 64 20   that was found 
11a33 74 6f 20 62 65 20 74 6f 6f 20 73 6c 6f 77 2e 0a  to be too slow..
11a34 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
11a35 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
11a36 69 73 20 73 74 6f 72 65 64 20 61 74 20 74 68 65  is stored at the
11a37 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 64 61   beginning of da
11a38 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 68  ta and.** the ch
11a39 65 63 6b 73 75 6d 20 69 73 20 73 74 6f 72 65 64  ecksum is stored
11a3a 20 61 74 20 74 68 65 20 65 6e 64 2e 20 20 54 68   at the end.  Th
11a3b 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e  is is important.
11a3c 20 20 49 66 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20    If journal.** 
11a3d 63 6f 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72  corruption occur
11a3e 73 20 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72  s due to a power
11a3f 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f   failure, the mo
11a40 73 74 20 6c 69 6b 65 6c 79 20 73 63 65 6e 61 72  st likely scenar
11a41 69 6f 0a 2a 2a 20 69 73 20 74 68 61 74 20 6f 6e  io.** is that on
11a42 65 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68  e end or the oth
11a43 65 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  er of the record
11a44 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64   will be changed
11a45 2e 20 20 49 74 20 69 73 0a 2a 2a 20 6d 75 63 68  .  It is.** much
11a46 20 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61   less likely tha
11a47 74 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f  t the two ends o
11a48 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65  f the journal re
11a49 63 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20  cord will be.** 
11a4a 63 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 20  correct and the 
11a4b 6d 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70  middle be corrup
11a4c 74 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 22  t.  Thus, this "
11a4d 63 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65  checksum" scheme
11a4e 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74  ,.** though fast
11a4f 20 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74   and simple, cat
11a50 63 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20  ches the mostly 
11a51 6c 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63  likely kind of c
11a52 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  orruption..**.**
11a53 20 46 49 58 20 4d 45 3a 20 20 43 6f 6e 73 69 64   FIX ME:  Consid
11a54 65 72 20 61 64 64 69 6e 67 20 65 76 65 72 79 20  er adding every 
11a55 32 30 30 74 68 20 28 6f 72 20 73 6f 29 20 62 79  200th (or so) by
11a56 74 65 20 6f 66 20 74 68 65 20 64 61 74 61 20 74  te of the data t
11a57 6f 20 74 68 65 0a 2a 2a 20 63 68 65 63 6b 73 75  o the.** checksu
11a58 6d 2e 20 20 54 68 61 74 20 77 61 79 20 69 66 20  m.  That way if 
11a59 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 73 70  a single page sp
11a5a 61 6e 73 20 33 20 6f 72 20 6d 6f 72 65 20 64 69  ans 3 or more di
11a5b 73 6b 20 73 65 63 74 6f 72 73 20 61 6e 64 0a 2a  sk sectors and.*
11a5c 2a 20 6f 6e 6c 79 20 74 68 65 20 6d 69 64 64 6c  * only the middl
11a5d 65 20 73 65 63 74 6f 72 20 69 73 20 63 6f 72 72  e sector is corr
11a5e 75 70 74 2c 20 77 65 20 77 69 6c 6c 20 73 74 69  upt, we will sti
11a5f 6c 6c 20 68 61 76 65 20 61 20 72 65 61 73 6f 6e  ll have a reason
11a60 61 62 6c 65 0a 2a 2a 20 63 68 61 6e 63 65 20 6f  able.** chance o
11a61 66 20 66 61 69 6c 69 6e 67 20 74 68 65 20 63 68  f failing the ch
11a62 65 63 6b 73 75 6d 20 61 6e 64 20 74 68 75 73 20  ecksum and thus 
11a63 64 65 74 65 63 74 69 6e 67 20 74 68 65 20 70 72  detecting the pr
11a64 6f 62 6c 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63  oblem..*/.static
11a65 20 75 33 32 20 70 61 67 65 72 5f 63 6b 73 75 6d   u32 pager_cksum
11a66 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
11a67 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61 29  const u8 *aData)
11a68 7b 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20  {.  u32 cksum = 
11a69 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
11a6a 74 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61  t;.  int i = pPa
11a6b 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30  ger->pageSize-20
11a6c 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20  0;.  while( i>0 
11a6d 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20  ){.    cksum += 
11a6e 61 44 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20  aData[i];.    i 
11a6f 2d 3d 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65  -= 200;.  }.  re
11a70 74 75 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f  turn cksum;.}../
11a71 2a 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67  *.** Read a sing
11a72 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74  le page from eit
11a73 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  her the journal 
11a74 66 69 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a  file (if isMainJ
11a75 72 6e 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72  rnl==1) or.** fr
11a76 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  om the sub-journ
11a77 61 6c 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e  al (if isMainJrn
11a78 6c 3d 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61  l==0) and playba
11a79 63 6b 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a  ck that page..**
11a7a 20 54 68 65 20 70 61 67 65 20 62 65 67 69 6e 73   The page begins
11a7b 20 61 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66   at offset *pOff
11a7c 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c  set into the fil
11a7d 65 2e 20 20 54 68 65 20 20 2a 70 4f 66 66 73 65  e.  The  *pOffse
11a7e 74 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 69 6e  t.** value is in
11a7f 63 72 65 61 73 65 64 20 74 6f 20 74 68 65 20 73  creased to the s
11a80 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74  tart of the next
11a81 20 70 61 67 65 20 69 6e 20 74 68 65 20 6a 6f 75   page in the jou
11a82 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rnal..**.** The 
11a83 69 73 4d 61 69 6e 4a 72 6e 6c 20 66 6c 61 67 20  isMainJrnl flag 
11a84 69 73 20 74 72 75 65 20 69 66 20 74 68 69 73 20  is true if this 
11a85 69 73 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c  is the main roll
11a86 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e 64  back journal and
11a87 0a 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 74 68  .** false for th
11a88 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  e statement jour
11a89 6e 61 6c 2e 20 20 54 68 65 20 6d 61 69 6e 20 72  nal.  The main r
11a8a 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
11a8b 75 73 65 73 0a 2a 2a 20 63 68 65 63 6b 73 75 6d  uses.** checksum
11a8c 73 20 2d 20 74 68 65 20 73 74 61 74 65 6d 65 6e  s - the statemen
11a8d 74 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e  t journal does n
11a8e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f  ot..**.** If pDo
11a8f 6e 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  ne is not NULL, 
11a90 74 68 65 6e 20 69 74 20 69 73 20 61 20 72 65 63  then it is a rec
11a91 6f 72 64 20 6f 66 20 70 61 67 65 73 20 74 68 61  ord of pages tha
11a92 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a  t have already.*
11a93 2a 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61  * been played ba
11a94 63 6b 2e 20 20 49 66 20 74 68 65 20 70 61 67 65  ck.  If the page
11a95 20 61 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73   at *pOffset has
11a96 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c   already been pl
11a97 61 79 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66  ayed back.** (if
11a98 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
11a99 6e 67 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20  ng pDone bit is 
11a9a 73 65 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74  set) then skip t
11a9b 68 65 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20  he playback..** 
11a9c 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 44  Make sure the pD
11a9d 6f 6e 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f  one bit correspo
11a9e 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f  nding to the *pO
11a9f 66 66 73 65 74 20 70 61 67 65 20 69 73 20 73 65  ffset page is se
11aa0 74 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65  t.** prior to re
11aa1 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  turning..*/.stat
11aa2 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61  ic int pager_pla
11aa3 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a  yback_one_page(.
11aa4 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c    Pager *pPager,
11aa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11aa6 2f 2a 20 54 68 65 20 70 61 67 65 72 20 62 65 69  /* The pager bei
11aa7 6e 67 20 70 6c 61 79 65 64 20 62 61 63 6b 20 2a  ng played back *
11aa8 2f 0a 20 20 69 6e 74 20 69 73 4d 61 69 6e 4a 72  /.  int isMainJr
11aa9 6e 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nl,             
11aaa 20 20 2f 2a 20 31 20 2d 3e 20 6d 61 69 6e 20 6a    /* 1 -> main j
11aab 6f 75 72 6e 61 6c 2e 20 30 20 2d 3e 20 73 75 62  ournal. 0 -> sub
11aac 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 69  -journal. */.  i
11aad 36 34 20 2a 70 4f 66 66 73 65 74 2c 20 20 20 20  64 *pOffset,    
11aae 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11aaf 4f 66 66 73 65 74 20 6f 66 20 72 65 63 6f 72 64  Offset of record
11ab0 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 2a 2f 0a   to playback */.
11ab1 20 20 69 6e 74 20 69 73 53 61 76 65 70 6e 74 2c    int isSavepnt,
11ab2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ab3 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 73 61  /* True for a sa
11ab4 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b  vepoint rollback
11ab5 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 44   */.  Bitvec *pD
11ab6 6f 6e 65 20 20 20 20 20 20 20 20 20 20 20 20 20  one             
11ab7 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20 6f 66      /* Bitvec of
11ab8 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 70   pages already p
11ab9 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 29 7b  layed back */.){
11aba 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48  .  int rc;.  PgH
11abb 64 72 20 2a 70 50 67 3b 20 20 20 20 20 20 20 20  dr *pPg;        
11abc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
11abd 20 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 69   existing page i
11abe 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20  n the cache */. 
11abf 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20   Pgno pgno;     
11ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11ac1 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  * The page numbe
11ac2 72 20 6f 66 20 61 20 70 61 67 65 20 69 6e 20 6a  r of a page in j
11ac3 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20  ournal */.  u32 
11ac4 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20  cksum;          
11ac5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65            /* Che
11ac6 63 6b 73 75 6d 20 75 73 65 64 20 66 6f 72 20 73  cksum used for s
11ac7 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a  anity checking *
11ac8 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20  /.  u8 *aData;  
11ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11aca 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73    /* Temporary s
11acb 74 6f 72 61 67 65 20 66 6f 72 20 74 68 65 20 70  torage for the p
11acc 61 67 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  age */.  sqlite3
11acd 5f 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20  _file *jfd;     
11ace 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69         /* The fi
11acf 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f  le descriptor fo
11ad0 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  r the journal fi
11ad1 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  le */..  assert(
11ad2 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29   (isMainJrnl&~1)
11ad3 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a 20 69  ==0 );      /* i
11ad4 73 4d 61 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f  sMainJrnl is 0 o
11ad5 72 20 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  r 1 */.  assert(
11ad6 20 28 69 73 53 61 76 65 70 6e 74 26 7e 31 29 3d   (isSavepnt&~1)=
11ad7 3d 30 20 29 3b 20 20 20 20 20 20 20 2f 2a 20 69  =0 );       /* i
11ad8 73 53 61 76 65 70 6e 74 20 69 73 20 30 20 6f 72  sSavepnt is 0 or
11ad9 20 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20   1 */.  assert( 
11ada 69 73 4d 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44  isMainJrnl || pD
11adb 6f 6e 65 20 29 3b 20 20 20 20 20 2f 2a 20 70 44  one );     /* pD
11adc 6f 6e 65 20 61 6c 77 61 79 73 20 75 73 65 64 20  one always used 
11add 6f 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20  on sub-journals 
11ade 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 53  */.  assert( isS
11adf 61 76 65 70 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d  avepnt || pDone=
11ae0 3d 30 20 29 3b 20 20 20 2f 2a 20 70 44 6f 6e 65  =0 );   /* pDone
11ae1 20 6e 65 76 65 72 20 75 73 65 64 20 6f 6e 20 6e   never used on n
11ae2 6f 6e 2d 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a  on-savepoint */.
11ae3 0a 20 20 61 44 61 74 61 20 3d 20 28 75 38 2a 29  .  aData = (u8*)
11ae4 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
11ae5 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61 44 61  e;.  assert( aDa
11ae6 74 61 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a  ta );         /*
11ae7 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 6d 75   Temp storage mu
11ae8 73 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  st have already 
11ae9 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a  been allocated *
11aea 2f 0a 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 69  /..  jfd = isMai
11aeb 6e 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e  nJrnl ? pPager->
11aec 6a 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a  jfd : pPager->sj
11aed 66 64 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61 64  fd;..  rc = read
11aee 33 32 62 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66  32bits(jfd, *pOf
11aef 66 73 65 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20  fset, &pgno);.  
11af0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
11af1 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
11af2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
11af3 65 61 64 28 6a 66 64 2c 20 61 44 61 74 61 2c 20  ead(jfd, aData, 
11af4 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
11af5 2c 20 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b  , (*pOffset)+4);
11af6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
11af7 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
11af8 3b 0a 20 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20  ;.  *pOffset += 
11af9 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
11afa 20 2b 20 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e   + 4 + isMainJrn
11afb 6c 2a 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74  l*4;..  /* Sanit
11afc 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68  y checking on th
11afd 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 69 73  e page.  This is
11afe 20 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20   more important 
11aff 74 68 61 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c  that I originall
11b00 79 0a 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20  y.  ** thought. 
11b01 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c   If a power fail
11b02 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ure occurs while
11b03 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
11b04 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20  being written,. 
11b05 20 2a 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 75   ** it could cau
11b06 73 65 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20  se invalid data 
11b07 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  to be written in
11b08 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  to the journal. 
11b09 20 57 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a   We need to.  **
11b0a 20 64 65 74 65 63 74 20 74 68 69 73 20 69 6e 76   detect this inv
11b0b 61 6c 69 64 20 64 61 74 61 20 28 77 69 74 68 20  alid data (with 
11b0c 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79  high probability
11b0d 29 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e  ) and ignore it.
11b0e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f  .  */.  if( pgno
11b0f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47  ==0 || pgno==PAG
11b10 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
11b11 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  r) ){.    return
11b12 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
11b13 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 50 67  }.  if( pgno>(Pg
11b14 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  no)pPager->dbSiz
11b15 65 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76  e || sqlite3Bitv
11b16 65 63 54 65 73 74 28 70 44 6f 6e 65 2c 20 70 67  ecTest(pDone, pg
11b17 6e 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  no) ){.    retur
11b18 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
11b19 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e  .  if( isMainJrn
11b1a 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 65  l ){.    rc = re
11b1b 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20 28 2a  ad32bits(jfd, (*
11b1c 70 4f 66 66 73 65 74 29 2d 34 2c 20 26 63 6b 73  pOffset)-4, &cks
11b1d 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  um);.    if( rc 
11b1e 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
11b1f 20 69 66 28 20 21 69 73 53 61 76 65 70 6e 74 20   if( !isSavepnt 
11b20 26 26 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70  && pager_cksum(p
11b21 50 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d 63  Pager, aData)!=c
11b22 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 65  ksum ){.      re
11b23 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
11b24 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
11b25 28 20 70 44 6f 6e 65 20 26 26 20 28 72 63 20 3d  ( pDone && (rc =
11b26 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65   sqlite3BitvecSe
11b27 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 29 20  t(pDone, pgno)) 
11b28 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
11b29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
11b2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
11b2b 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 7c  PAGER_RESERVED |
11b2c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  | pPager->state>
11b2d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45  =PAGER_EXCLUSIVE
11b2e 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
11b2f 20 70 61 67 65 72 20 69 73 20 69 6e 20 52 45 53   pager is in RES
11b30 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65  ERVED state, the
11b31 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20  n there must be 
11b32 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73 0a 20  a copy of this. 
11b33 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20   ** page in the 
11b34 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 6e 20  pager cache. In 
11b35 74 68 69 73 20 63 61 73 65 20 6a 75 73 74 20 75  this case just u
11b36 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20  pdate the pager 
11b37 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20  cache,.  ** not 
11b38 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11b39 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 6c  e. The page is l
11b3a 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72 74 79  eft marked dirty
11b3b 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20   in this case.. 
11b3c 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 63 65   **.  ** An exce
11b3d 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 62 6f  ption to the abo
11b3e 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68 65 20  ve rule: If the 
11b3f 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 6e  database is in n
11b40 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a  o-sync mode.  **
11b41 20 61 6e 64 20 61 20 70 61 67 65 20 69 73 20 6d   and a page is m
11b42 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20 69  oved during an i
11b43 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75  ncremental vacuu
11b44 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20  m then the page 
11b45 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20  may.  ** not be 
11b46 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63  in the pager cac
11b47 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61 20  he. Later: if a 
11b48 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65  malloc() or IO e
11b49 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 2a 2a  rror occurs.  **
11b4a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70 61   during a Movepa
11b4b 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e 20  ge() call, then 
11b4c 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f 74  the page may not
11b4d 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65   be in the cache
11b4e 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 53 6f  .  ** either. So
11b4f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64   the condition d
11b50 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20  escribed in the 
11b51 61 62 6f 76 65 20 70 61 72 61 67 72 61 70 68 20  above paragraph 
11b52 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 65  is not.  ** asse
11b53 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20  rt()able..  **. 
11b54 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c 55 53   ** If in EXCLUS
11b55 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e 20  IVE state, then 
11b56 77 65 20 75 70 64 61 74 65 20 74 68 65 20 70 61  we update the pa
11b57 67 65 72 20 63 61 63 68 65 20 69 66 20 69 74 20  ger cache if it 
11b58 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e 64 20  exists.  ** and 
11b59 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e 20 54  the main file. T
11b5a 68 65 20 70 61 67 65 20 69 73 20 74 68 65 6e 20  he page is then 
11b5b 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 74 79  marked not dirty
11b5c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b  ..  **.  ** Tick
11b5d 65 74 20 23 31 31 37 31 3a 20 20 54 68 65 20 73  et #1171:  The s
11b5e 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
11b5f 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 70   might contain p
11b60 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61 74  age content that
11b61 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72 65   is.  ** differe
11b62 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  nt from the page
11b63 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65 20   content at the 
11b64 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61  start of the tra
11b65 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54  nsaction..  ** T
11b66 68 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20  his occurs when 
11b67 61 20 70 61 67 65 20 69 73 20 63 68 61 6e 67 65  a page is change
11b68 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73  d prior to the s
11b69 74 61 72 74 20 6f 66 20 61 20 73 74 61 74 65 6d  tart of a statem
11b6a 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68  ent.  ** then ch
11b6b 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74 68  anged again with
11b6c 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  in the statement
11b6d 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20  .  When rolling 
11b6e 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a  back such a.  **
11b6f 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d 75   statement we mu
11b70 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20  st not write to 
11b71 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74  the original dat
11b72 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65 20  abase unless we 
11b73 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65  know.  ** for ce
11b74 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67 69  rtain that origi
11b75 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74  nal page content
11b76 73 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e 74  s are synced int
11b77 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62  o the main rollb
11b78 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  ack.  ** journal
11b79 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20  .  Otherwise, a 
11b7a 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68 74  power loss might
11b7b 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64 20   leave modified 
11b7c 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a  data in the.  **
11b7d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
11b7e 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79 20  ithout an entry 
11b7f 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  in the rollback 
11b80 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61 6e  journal that can
11b81 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74 68  .  ** restore th
11b82 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74  e database to it
11b83 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e  s original form.
11b84 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73    Two conditions
11b85 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65   must be.  ** me
11b86 74 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67  t before writing
11b87 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
11b88 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65 20   files. (1) the 
11b89 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65  database must be
11b8a 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28  .  ** locked.  (
11b8b 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20  2) we know that 
11b8c 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  the original pag
11b8d 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c  e content is ful
11b8e 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69  ly synced.  ** i
11b8f 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  n the main journ
11b90 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 73  al either becaus
11b91 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  e the page is no
11b92 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 6c  t in cache or el
11b93 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65  se.  ** the page
11b94 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e 65   is marked as ne
11b95 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a  edSync==0..  **.
11b96 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a    ** 2008-04-14:
11b97 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69 6e    When attemptin
11b98 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63 6f  g to vacuum a co
11b99 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66  rrupt database f
11b9a 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20  ile, it.  ** is 
11b9b 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69 6c  possible to fail
11b9c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20   a statement on 
11b9d 61 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  a database that 
11b9e 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78 69  does not yet exi
11b9f 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20  st..  ** Do not 
11ba0 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
11ba1 20 69 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   if database fil
11ba2 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e  e has never been
11ba3 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20   opened..  */.  
11ba4 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b  pPg = pager_look
11ba5 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  up(pPager, pgno)
11ba6 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28  ;.  PAGERTRACE((
11ba7 22 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67  "PLAYBACK %d pag
11ba8 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 20  e %d hash(%08x) 
11ba9 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  %s\n",.         
11baa 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50        PAGERID(pP
11bab 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67  ager), pgno, pag
11bac 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67  er_datahash(pPag
11bad 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 61 44  er->pageSize, aD
11bae 61 74 61 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ata),.          
11baf 20 20 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c       (isMainJrnl
11bb0 3f 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a  ?"main-journal":
11bb1 22 73 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20  "sub-journal"). 
11bb2 20 29 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67   ));.  if( (pPag
11bb3 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
11bb4 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20 26  _EXCLUSIVE).   &
11bb5 26 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d  & (pPg==0 || 0==
11bb6 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44  (pPg->flags&PGHD
11bb7 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20 20  R_NEED_SYNC)).  
11bb8 20 26 26 20 28 70 50 61 67 65 72 2d 3e 66 64 2d   && (pPager->fd-
11bb9 3e 70 4d 65 74 68 6f 64 73 29 0a 20 20 29 7b 0a  >pMethods).  ){.
11bba 20 20 20 20 69 36 34 20 6f 66 73 74 20 3d 20 28      i64 ofst = (
11bbb 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61  pgno-1)*(i64)pPa
11bbc 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20  ger->pageSize;. 
11bbd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
11bbe 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66  sWrite(pPager->f
11bbf 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72  d, aData, pPager
11bc0 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 73 74  ->pageSize, ofst
11bc1 29 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e  );.    if( pgno>
11bc2 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69  pPager->dbFileSi
11bc3 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67  ze ){.      pPag
11bc4 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d  er->dbFileSize =
11bc5 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d   pgno;.    }.  }
11bc6 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 6e  else if( !isMain
11bc7 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 29  Jrnl && pPg==0 )
11bc8 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
11bc9 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f   is a rollback o
11bca 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e  f a savepoint an
11bcb 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77  d data was not w
11bcc 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a  ritten to.    **
11bcd 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
11bce 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  d the page is no
11bcf 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65  t in-memory, the
11bd0 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61  re is a potentia
11bd1 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d  l.    ** problem
11bd2 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 20  . When the page 
11bd3 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 20  is next fetched 
11bd4 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  by the b-tree la
11bd5 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20  yer, it .    ** 
11bd6 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 6f  will be read fro
11bd7 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
11bd8 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6f  ile, which may o
11bd9 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 20  r may not be .  
11bda 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a 20    ** current. . 
11bdb 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
11bdc 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 20  re are a couple 
11bdd 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 79  of different way
11bde 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65  s this can happe
11bdf 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 65  n. All are quite
11be0 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 2e  .    ** obscure.
11be1 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e   When running in
11be2 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64   synchronous mod
11be3 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79  e, this can only
11be4 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a 20   happen .    ** 
11be5 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f  if the page is o
11be6 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  n the free-list 
11be7 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
11be8 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  the transaction,
11be9 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f 70   then.    ** pop
11bea 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f 76  ulated, then mov
11beb 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
11bec 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 2e  PagerMovepage().
11bed 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
11bee 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74  he solution is t
11bef 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f  o add an in-memo
11bf0 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 63  ry page to the c
11bf1 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 0a  ache containing.
11bf2 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20      ** the data 
11bf3 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 74  just read from t
11bf4 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20  he sub-journal. 
11bf5 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73  Mark the page as
11bf6 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 61   dirty .    ** a
11bf7 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20  nd if the pager 
11bf8 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 6e  requires a journ
11bf9 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d 61  al-sync, then ma
11bfa 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 0a  rk the page as .
11bfb 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67      ** requiring
11bfc 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20   a journal-sync 
11bfd 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 69  before it is wri
11bfe 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tten..    */.   
11bff 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70   assert( isSavep
11c00 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 72  nt );.    if( (r
11c01 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
11c02 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20  Acquire(pPager, 
11c03 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 20  pgno, &pPg, 1)) 
11c04 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
11c05 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  rc;.    }.    pP
11c06 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48  g->flags &= ~PGH
11c07 44 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20  DR_NEED_READ;.  
11c08 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
11c09 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20  akeDirty(pPg);. 
11c0a 20 7d 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a   }.  if( pPg ){.
11c0b 20 20 20 20 2f 2a 20 4e 6f 20 70 61 67 65 20 73      /* No page s
11c0c 68 6f 75 6c 64 20 65 76 65 72 20 62 65 20 65 78  hould ever be ex
11c0d 70 6c 69 63 69 74 6c 79 20 72 6f 6c 6c 65 64 20  plicitly rolled 
11c0e 62 61 63 6b 20 74 68 61 74 20 69 73 20 69 6e 20  back that is in 
11c0f 75 73 65 2c 20 65 78 63 65 70 74 0a 20 20 20 20  use, except.    
11c10 2a 2a 20 66 6f 72 20 70 61 67 65 20 31 20 77 68  ** for page 1 wh
11c11 69 63 68 20 69 73 20 68 65 6c 64 20 69 6e 20 75  ich is held in u
11c12 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  se in order to k
11c13 65 65 70 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20  eep the lock on 
11c14 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  the.    ** datab
11c15 61 73 65 20 61 63 74 69 76 65 2e 20 48 6f 77 65  ase active. Howe
11c16 76 65 72 20 73 75 63 68 20 61 20 70 61 67 65 20  ver such a page 
11c17 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  may be rolled ba
11c18 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 0a 20  ck as a result. 
11c19 20 20 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 74 65     ** of an inte
11c1a 72 6e 61 6c 20 65 72 72 6f 72 20 72 65 73 75 6c  rnal error resul
11c1b 74 69 6e 67 20 69 6e 20 61 6e 20 61 75 74 6f 6d  ting in an autom
11c1c 61 74 69 63 20 63 61 6c 6c 20 74 6f 0a 20 20 20  atic call to.   
11c1d 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72   ** sqlite3Pager
11c1e 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 20 20 20 20  Rollback()..    
11c1f 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61  */.    void *pDa
11c20 74 61 3b 0a 20 20 20 20 70 44 61 74 61 20 3d 20  ta;.    pData = 
11c21 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20  pPg->pData;.    
11c22 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20 61 44  memcpy(pData, aD
11c23 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  ata, pPager->pag
11c24 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20  eSize);.    if( 
11c25 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65  pPager->xReinite
11c26 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  r ){.      pPage
11c27 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67  r->xReiniter(pPg
11c28 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
11c29 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 28   isMainJrnl && (
11c2a 21 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 70 50  !isSavepnt || pP
11c2b 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11c2c 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  <=pPager->journa
11c2d 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f  lHdr) ){.      /
11c2e 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74  * If the content
11c2f 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 77  s of this page w
11c30 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65  ere just restore
11c31 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20  d from the main 
11c32 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61  .      ** journa
11c33 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73  l file, then its
11c34 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65   content must be
11c35 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77 68   as they were wh
11c36 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  en the .      **
11c37 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
11c38 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49   first opened. I
11c39 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 63  n this case we c
11c3a 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65  an mark the page
11c3b 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65  .      ** as cle
11c3c 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20  an, since there 
11c3d 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20  will be no need 
11c3e 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 20  to write it out 
11c3f 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a  to the..      **
11c40 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20  .      ** There 
11c41 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e  is one exception
11c42 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 49   to this rule. I
11c43 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 65  f the page is be
11c44 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20  ing rolled.     
11c45 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 74   ** back as part
11c46 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20   of a savepoint 
11c47 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 72  (or statement) r
11c48 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20  ollback from an 
11c49 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63  .      ** unsync
11c4a 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68  ed portion of th
11c4b 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66  e main journal f
11c4c 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ile, then it is 
11c4d 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 2a  not safe.      *
11c4e 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 61  * to mark the pa
11c4f 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 69  ge as clean. Thi
11c50 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 72  s is because mar
11c51 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 73  king the page as
11c52 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20  .      ** clean 
11c53 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 50  will clear the P
11c54 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66  GHDR_NEED_SYNC f
11c55 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 70  lag. Since the p
11c56 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20  age is.      ** 
11c57 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a  already in the j
11c58 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 63  ournal file (rec
11c59 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e 70  orded in Pager.p
11c5a 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20  InJournal) and. 
11c5b 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44       ** the PGHD
11c5c 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
11c5d 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 20   is cleared, if 
11c5e 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74  the page is writ
11c5f 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ten to.      ** 
11c60 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69  again within thi
11c61 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69  s transaction, i
11c62 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64  t will be marked
11c63 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 20   as dirty but.  
11c64 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52      ** the PGHDR
11c65 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20  _NEED_SYNC flag 
11c66 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e  will not be set.
11c67 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 70   It could then p
11c68 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20  otentially.     
11c69 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 6f   ** be written o
11c6a 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ut into the data
11c6b 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65  base file before
11c6c 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   its journal fil
11c6d 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65  e.      ** segme
11c6e 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66  nt is synced. If
11c6f 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 20   a crash occurs 
11c70 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77  during or follow
11c71 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 20  ing this,.      
11c72 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  ** database corr
11c73 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65  uption may ensue
11c74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
11c75 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61   sqlite3PcacheMa
11c76 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20  keClean(pPg);.  
11c77 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
11c78 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
11c79 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20    pPg->pageHash 
11c7a 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  = pager_pagehash
11c7b 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20  (pPg);.#endif.  
11c7c 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73    /* If this was
11c7d 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 65   page 1, then re
11c7e 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20  store the value 
11c7f 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56  of Pager.dbFileV
11c80 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74  ers..    ** Do t
11c81 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20 64  his before any d
11c82 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20  ecoding. */.    
11c83 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20  if( pgno==1 ){. 
11c84 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61       memcpy(&pPa
11c85 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c  ger->dbFileVers,
11c86 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b 32   &((u8*)pData)[2
11c87 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 72  4],sizeof(pPager
11c88 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a  ->dbFileVers));.
11c89 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
11c8a 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a 75  code the page ju
11c8b 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73  st read from dis
11c8c 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 28  k */.    CODEC1(
11c8d 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70  pPager, pData, p
11c8e 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20 20  Pg->pgno, 3);.  
11c8f 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52    sqlite3PcacheR
11c90 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d  elease(pPg);.  }
11c91 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
11c92 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
11c93 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64  EBUG) || defined
11c94 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45  (SQLITE_COVERAGE
11c95 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69  _TEST)./*.** Thi
11c96 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20  s routine looks 
11c97 61 68 65 61 64 20 69 6e 74 6f 20 74 68 65 20 6d  ahead into the m
11c98 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
11c99 20 61 6e 64 20 64 65 74 65 72 6d 69 6e 65 73 0a   and determines.
11c9a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ** whether or no
11c9b 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72  t the next recor
11c9c 64 20 28 74 68 65 20 72 65 63 6f 72 64 20 74 68  d (the record th
11c9d 61 74 20 62 65 67 69 6e 73 20 61 74 20 66 69 6c  at begins at fil
11c9e 65 0a 2a 2a 20 6f 66 66 73 65 74 20 70 50 61 67  e.** offset pPag
11c9f 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20  er->journalOff) 
11ca0 69 73 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  is a well-formed
11ca1 20 70 61 67 65 20 72 65 63 6f 72 64 20 63 6f 6e   page record con
11ca2 73 69 73 74 69 6e 67 0a 2a 2a 20 6f 66 20 61 20  sisting.** of a 
11ca3 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62 65  valid page numbe
11ca4 72 2c 20 70 50 61 67 65 2d 3e 70 61 67 65 53 69  r, pPage->pageSi
11ca5 7a 65 20 62 79 74 65 73 20 6f 66 20 63 6f 6e 74  ze bytes of cont
11ca6 65 6e 74 2c 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a  ent, followed.**
11ca7 20 62 79 20 61 20 76 61 6c 69 64 20 63 68 65 63   by a valid chec
11ca8 6b 73 75 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ksum..**.** The 
11ca9 70 61 67 65 72 20 6e 65 76 65 72 20 6e 65 65 64  pager never need
11caa 73 20 74 6f 20 6b 6e 6f 77 20 74 68 69 73 20 69  s to know this i
11cab 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 20 69 74  n order to do it
11cac 73 20 6a 6f 62 2e 20 20 20 54 68 69 73 0a 2a 2a  s job.   This.**
11cad 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79   routine is only
11cae 20 75 73 65 64 20 66 72 6f 6d 20 77 69 74 68 20   used from with 
11caf 61 73 73 65 72 74 28 29 20 61 6e 64 20 74 65 73  assert() and tes
11cb0 74 63 61 73 65 28 29 20 6d 61 63 72 6f 73 2e 0a  tcase() macros..
11cb1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
11cb2 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50 61  gerNextJournalPa
11cb3 67 65 49 73 56 61 6c 69 64 28 50 61 67 65 72 20  geIsValid(Pager 
11cb4 2a 70 50 61 67 65 72 29 7b 0a 20 20 50 67 6e 6f  *pPager){.  Pgno
11cb5 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
11cb6 20 2f 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d   /* The page num
11cb7 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20  ber of the page 
11cb8 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20  */.  u32 cksum; 
11cb9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
11cba 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 2a   page checksum *
11cbb 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
11cbc 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
11cbd 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 72 65 61  rn code from rea
11cbe 64 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a  d operations */.
11cbf 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
11cc0 66 64 3b 20 20 20 20 2f 2a 20 54 68 65 20 66 69  fd;    /* The fi
11cc1 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 72  le descriptor fr
11cc2 6f 6d 20 77 68 69 63 68 20 77 65 20 61 72 65 20  om which we are 
11cc3 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 75 38 20  reading */.  u8 
11cc4 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  *aData;         
11cc5 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20    /* Content of 
11cc6 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20 2f  the page */..  /
11cc7 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 20  * Read the page 
11cc8 6e 75 6d 62 65 72 20 68 65 61 64 65 72 20 2a 2f  number header */
11cc9 0a 20 20 66 64 20 3d 20 70 50 61 67 65 72 2d 3e  .  fd = pPager->
11cca 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64  jfd;.  rc = read
11ccb 33 32 62 69 74 73 28 66 64 2c 20 70 50 61 67 65  32bits(fd, pPage
11ccc 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 26  r->journalOff, &
11ccd 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21  pgno);.  if( rc!
11cce 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72 65  =SQLITE_OK ){ re
11ccf 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20  turn 0; }       
11cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11cd1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
11cd2 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 6e  TEST*/.  if( pgn
11cd3 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41  o==0 || pgno==PA
11cd4 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
11cd5 65 72 29 20 29 7b 20 72 65 74 75 72 6e 20 30 3b  er) ){ return 0;
11cd6 20 7d 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f   }         /*NO_
11cd7 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 6e  TEST*/.  if( pgn
11cd8 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d 3e  o>(Pgno)pPager->
11cd9 64 62 53 69 7a 65 20 29 7b 20 72 65 74 75 72 6e  dbSize ){ return
11cda 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20   0; }           
11cdb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
11cdc 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61  TEST*/..  /* Rea
11cdd 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 2a  d the checksum *
11cde 2f 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62  /.  rc = read32b
11cdf 69 74 73 28 66 64 2c 20 70 50 61 67 65 72 2d 3e  its(fd, pPager->
11ce0 6a 6f 75 72 6e 61 6c 4f 66 66 2b 70 50 61 67 65  journalOff+pPage
11ce1 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 2c 20 26  r->pageSize+4, &
11ce2 63 6b 73 75 6d 29 3b 0a 20 20 69 66 28 20 72 63  cksum);.  if( rc
11ce3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72  !=SQLITE_OK ){ r
11ce4 65 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20  eturn 0; }      
11ce5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ce6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f              /*NO
11ce7 5f 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65  _TEST*/..  /* Re
11ce8 61 64 20 74 68 65 20 64 61 74 61 20 61 6e 64 20  ad the data and 
11ce9 76 65 72 69 66 79 20 74 68 65 20 63 68 65 63 6b  verify the check
11cea 73 75 6d 20 2a 2f 0a 20 20 61 44 61 74 61 20 3d  sum */.  aData =
11ceb 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 54   (u8*)pPager->pT
11cec 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20  mpSpace;.  rc = 
11ced 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66 64  sqlite3OsRead(fd
11cee 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d  , aData, pPager-
11cef 3e 70 61 67 65 53 69 7a 65 2c 20 70 50 61 67 65  >pageSize, pPage
11cf0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 34 29  r->journalOff+4)
11cf1 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
11cf2 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e 20  TE_OK ){ return 
11cf3 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  0; }            
11cf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11cf5 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
11cf6 2f 0a 20 20 69 66 28 20 70 61 67 65 72 5f 63 6b  /.  if( pager_ck
11cf7 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 74  sum(pPager, aDat
11cf8 61 29 21 3d 63 6b 73 75 6d 20 29 7b 20 72 65 74  a)!=cksum ){ ret
11cf9 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20 20  urn 0; }        
11cfa 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
11cfb 2f 0a 0a 20 20 2f 2a 20 52 65 61 63 68 20 74 68  /..  /* Reach th
11cfc 69 73 20 70 6f 69 6e 74 20 6f 6e 6c 79 20 69 66  is point only if
11cfd 20 74 68 65 20 70 61 67 65 20 69 73 20 76 61 6c   the page is val
11cfe 69 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31  id */.  return 1
11cff 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  ;.}.#endif /* !d
11d00 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c  efined(NDEBUG) |
11d01 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
11d02 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 20  _COVERAGE_TEST) 
11d03 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65  */../*.** Parame
11d04 74 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74  ter zMaster is t
11d05 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73  he name of a mas
11d06 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
11d07 2e 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e  . A single journ
11d08 61 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20  al.** file that 
11d09 72 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20  referred to the 
11d0a 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
11d0b 69 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65  ile has just bee
11d0c 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  n rolled back..*
11d0d 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
11d0e 68 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70  hecks if it is p
11d0f 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74  ossible to delet
11d10 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
11d11 72 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e  rnal file,.** an
11d12 64 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20  d does so if it 
11d13 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65  is..**.** Argume
11d14 6e 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70  nt zMaster may p
11d15 6f 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54  oint to Pager.pT
11d16 6d 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74  mpSpace. So that
11d17 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a   buffer is not .
11d18 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ** available for
11d19 20 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73   use within this
11d1a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
11d1b 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a  .** The master j
11d1c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74  ournal file cont
11d1d 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 73 20 6f  ains the names o
11d1e 66 20 61 6c 6c 20 63 68 69 6c 64 20 6a 6f 75 72  f all child jour
11d1f 6e 61 6c 73 2e 0a 2a 2a 20 54 6f 20 74 65 6c 6c  nals..** To tell
11d20 20 69 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   if a master jou
11d21 72 6e 61 6c 20 63 61 6e 20 62 65 20 64 65 6c 65  rnal can be dele
11d22 74 65 64 2c 20 63 68 65 63 6b 20 74 6f 20 65 61  ted, check to ea
11d23 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 63 68 69  ch of the.** chi
11d24 6c 64 72 65 6e 2e 20 20 49 66 20 61 6c 6c 20 63  ldren.  If all c
11d25 68 69 6c 64 72 65 6e 20 61 72 65 20 65 69 74 68  hildren are eith
11d26 65 72 20 6d 69 73 73 69 6e 67 20 6f 72 20 64 6f  er missing or do
11d27 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 0a 2a 2a   not refer to.**
11d28 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 61 73   a different mas
11d29 74 65 72 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65  ter journal, the
11d2a 6e 20 74 68 69 73 20 6d 61 73 74 65 72 20 6a 6f  n this master jo
11d2b 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 64 65 6c  urnal can be del
11d2c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eted..*/.static 
11d2d 69 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73  int pager_delmas
11d2e 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65  ter(Pager *pPage
11d2f 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
11d30 4d 61 73 74 65 72 29 7b 0a 20 20 73 71 6c 69 74  Master){.  sqlit
11d31 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70  e3_vfs *pVfs = p
11d32 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69  Pager->pVfs;.  i
11d33 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6d 61 73  nt rc;.  int mas
11d34 74 65 72 5f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20  ter_open = 0;.  
11d35 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d  sqlite3_file *pM
11d36 61 73 74 65 72 3b 0a 20 20 73 71 6c 69 74 65 33  aster;.  sqlite3
11d37 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b  _file *pJournal;
11d38 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  .  char *zMaster
11d39 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20  Journal = 0; /* 
11d3a 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 73 74  Contents of mast
11d3b 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
11d3c 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 65 72  */.  i64 nMaster
11d3d 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 2f  Journal;       /
11d3e 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 65 72  * Size of master
11d3f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
11d40 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
11d41 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
11d42 69 6c 65 20 65 78 63 6c 75 73 69 76 65 6c 79 20  ile exclusively 
11d43 69 6e 20 63 61 73 65 20 73 6f 6d 65 20 6f 74 68  in case some oth
11d44 65 72 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20  er process.  ** 
11d45 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 69 73 20  is running this 
11d46 72 6f 75 74 69 6e 65 20 61 6c 73 6f 2e 20 4e 6f  routine also. No
11d47 74 20 74 68 61 74 20 69 74 20 6d 61 6b 65 73 20  t that it makes 
11d48 74 6f 6f 20 6d 75 63 68 20 64 69 66 66 65 72 65  too much differe
11d49 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73  nce..  */.  pMas
11d4a 74 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 66  ter = (sqlite3_f
11d4b 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c  ile *)sqlite3Mal
11d4c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69  loc(pVfs->szOsFi
11d4d 6c 65 20 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72  le * 2);.  pJour
11d4e 6e 61 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66  nal = (sqlite3_f
11d4f 69 6c 65 20 2a 29 28 28 28 75 38 20 2a 29 70 4d  ile *)(((u8 *)pM
11d50 61 73 74 65 72 29 20 2b 20 70 56 66 73 2d 3e 73  aster) + pVfs->s
11d51 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  zOsFile);.  if( 
11d52 21 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  !pMaster ){.    
11d53 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
11d54 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  M;.  }else{.    
11d55 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c  int flags = (SQL
11d56 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
11d57 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  Y|SQLITE_OPEN_MA
11d58 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20  STER_JOURNAL);. 
11d59 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
11d5a 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73  sOpen(pVfs, zMas
11d5b 74 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c  ter, pMaster, fl
11d5c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  ags, 0);.  }.  i
11d5d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
11d5e 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65   ) goto delmaste
11d5f 72 5f 6f 75 74 3b 0a 20 20 6d 61 73 74 65 72 5f  r_out;.  master_
11d60 6f 70 65 6e 20 3d 20 31 3b 0a 0a 20 20 72 63 20  open = 1;..  rc 
11d61 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
11d62 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d  ize(pMaster, &nM
11d63 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20  asterJournal);. 
11d64 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
11d65 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73  OK ) goto delmas
11d66 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20  ter_out;..  if( 
11d67 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30  nMasterJournal>0
11d68 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a   ){.    char *zJ
11d69 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72  ournal;.    char
11d6a 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30   *zMasterPtr = 0
11d6b 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65  ;.    int nMaste
11d6c 72 50 74 72 20 3d 20 70 50 61 67 65 72 2d 3e 70  rPtr = pPager->p
11d6d 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
11d6e 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20  1;..    /* Load 
11d6f 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65  the entire maste
11d70 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  r journal file i
11d71 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e  nto space obtain
11d72 65 64 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 73  ed from.    ** s
11d73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
11d74 61 6e 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  and pointed to b
11d75 79 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  y zMasterJournal
11d76 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d  . .    */.    zM
11d77 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 28  asterJournal = (
11d78 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61  char *)sqlite3Ma
11d79 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 65  lloc((int)nMaste
11d7a 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74  rJournal + nMast
11d7b 65 72 50 74 72 29 3b 0a 20 20 20 20 69 66 28 20  erPtr);.    if( 
11d7c 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20  !zMasterJournal 
11d7d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
11d7e 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
11d7f 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
11d80 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
11d81 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a 4d  zMasterPtr = &zM
11d82 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61  asterJournal[nMa
11d83 73 74 65 72 4a 6f 75 72 6e 61 6c 5d 3b 0a 20 20  sterJournal];.  
11d84 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11d85 52 65 61 64 28 70 4d 61 73 74 65 72 2c 20 7a 4d  Read(pMaster, zM
11d86 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 28 69  asterJournal, (i
11d87 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61  nt)nMasterJourna
11d88 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  l, 0);.    if( r
11d89 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
11d8a 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75  oto delmaster_ou
11d8b 74 3b 0a 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c  t;..    zJournal
11d8c 20 3d 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61   = zMasterJourna
11d8d 6c 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a  l;.    while( (z
11d8e 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a  Journal-zMasterJ
11d8f 6f 75 72 6e 61 6c 29 3c 6e 4d 61 73 74 65 72 4a  ournal)<nMasterJ
11d90 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20  ournal ){.      
11d91 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20  int exists;.    
11d92 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11d93 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4a 6f  Access(pVfs, zJo
11d94 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43  urnal, SQLITE_AC
11d95 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78  CESS_EXISTS, &ex
11d96 69 73 74 73 29 3b 0a 20 20 20 20 20 20 69 66 28  ists);.      if(
11d97 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11d98 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64  {.        goto d
11d99 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20  elmaster_out;.  
11d9a 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
11d9b 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20  exists ){.      
11d9c 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20    /* One of the 
11d9d 6a 6f 75 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64  journals pointed
11d9e 20 74 6f 20 62 79 20 74 68 65 20 6d 61 73 74 65   to by the maste
11d9f 72 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73  r journal exists
11da0 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 70 65  ..        ** Ope
11da1 6e 20 69 74 20 61 6e 64 20 63 68 65 63 6b 20 69  n it and check i
11da2 66 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 74  f it points at t
11da3 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
11da4 6c 2e 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a  l. If.        **
11da5 20 73 6f 2c 20 72 65 74 75 72 6e 20 77 69 74 68   so, return with
11da6 6f 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65  out deleting the
11da7 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11da8 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  file..        */
11da9 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a  .        int c;.
11daa 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67          int flag
11dab 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e  s = (SQLITE_OPEN
11dac 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45  _READONLY|SQLITE
11dad 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e  _OPEN_MAIN_JOURN
11dae 41 4c 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  AL);.        rc 
11daf 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  = sqlite3OsOpen(
11db0 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20  pVfs, zJournal, 
11db1 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73 2c  pJournal, flags,
11db2 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
11db3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11db4 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  {.          goto
11db5 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
11db6 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
11db7 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74     rc = readMast
11db8 65 72 4a 6f 75 72 6e 61 6c 28 70 4a 6f 75 72 6e  erJournal(pJourn
11db9 61 6c 2c 20 7a 4d 61 73 74 65 72 50 74 72 2c 20  al, zMasterPtr, 
11dba 6e 4d 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20  nMasterPtr);.   
11dbb 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c       sqlite3OsCl
11dbc 6f 73 65 28 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20  ose(pJournal);. 
11dbd 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
11dbe 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
11dbf 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
11dc0 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20  ster_out;.      
11dc1 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 63 20 3d    }..        c =
11dc2 20 7a 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d   zMasterPtr[0]!=
11dc3 30 20 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73  0 && strcmp(zMas
11dc4 74 65 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29  terPtr, zMaster)
11dc5 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ==0;.        if(
11dc6 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20   c ){.          
11dc7 2f 2a 20 57 65 20 68 61 76 65 20 61 20 6d 61 74  /* We have a mat
11dc8 63 68 2e 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74  ch. Do not delet
11dc9 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
11dca 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20  rnal file. */.  
11dcb 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c          goto del
11dcc 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20  master_out;.    
11dcd 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
11dce 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20      zJournal += 
11dcf 28 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30  (sqlite3Strlen30
11dd0 28 7a 4a 6f 75 72 6e 61 6c 29 2b 31 29 3b 0a 20  (zJournal)+1);. 
11dd1 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 72 63     }.  }.  .  rc
11dd2 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65   = sqlite3OsDele
11dd3 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  te(pVfs, zMaster
11dd4 2c 20 30 29 3b 0a 0a 64 65 6c 6d 61 73 74 65 72  , 0);..delmaster
11dd5 5f 6f 75 74 3a 0a 20 20 69 66 28 20 7a 4d 61 73  _out:.  if( zMas
11dd6 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  terJournal ){.  
11dd7 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
11dd8 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a  MasterJournal);.
11dd9 20 20 7d 20 20 0a 20 20 69 66 28 20 6d 61 73 74    }  .  if( mast
11dda 65 72 5f 6f 70 65 6e 20 29 7b 0a 20 20 20 20 73  er_open ){.    s
11ddb 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4d  qlite3OsClose(pM
11ddc 61 73 74 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71  aster);.  }.  sq
11ddd 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61 73 74  lite3_free(pMast
11dde 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  er);.  return rc
11ddf 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ;.}.../*.** If t
11de0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
11de1 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 61 6e   file is open an
11de2 64 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  d an exclusive l
11de3 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 0a 2a 2a  ock is held, .**
11de4 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6d 61   truncate the ma
11de5 69 6e 20 66 69 6c 65 20 6f 66 20 74 68 65 20 67  in file of the g
11de6 69 76 65 6e 20 70 61 67 65 72 20 74 6f 20 74 68  iven pager to th
11de7 65 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62  e specified numb
11de8 65 72 20 0a 2a 2a 20 6f 66 20 70 61 67 65 73 2e  er .** of pages.
11de9 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20  .**.** It might 
11dea 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73  might be the cas
11deb 65 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20  e that the file 
11dec 6f 6e 20 64 69 73 6b 20 69 73 20 73 6d 61 6c 6c  on disk is small
11ded 65 72 20 74 68 61 6e 20 6e 50 61 67 65 2e 0a 2a  er than nPage..*
11dee 2a 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65  * This can happe
11def 6e 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20  n, for example, 
11df0 69 66 20 77 65 20 61 72 65 20 69 6e 20 74 68 65  if we are in the
11df1 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 74 72 61   middle of a tra
11df2 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 63  nsaction.** whic
11df3 68 20 68 61 73 20 65 78 74 65 6e 64 65 64 20 74  h has extended t
11df4 68 65 20 66 69 6c 65 20 73 69 7a 65 20 61 6e 64  he file size and
11df5 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 61   the new pages a
11df6 72 65 20 73 74 69 6c 6c 20 61 6c 6c 20 68 65 6c  re still all hel
11df7 64 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2c 20 74  d.** in cache, t
11df8 68 65 6e 20 61 6e 20 49 4e 53 45 52 54 20 6f 72  hen an INSERT or
11df9 20 55 50 44 41 54 45 20 64 6f 65 73 20 61 20 73   UPDATE does a s
11dfa 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61 63  tatement rollbac
11dfb 6b 2e 20 20 53 6f 6d 65 0a 2a 2a 20 6f 70 65 72  k.  Some.** oper
11dfc 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70  ating system imp
11dfd 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e  lementations can
11dfe 20 67 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66   get confused if
11dff 20 79 6f 75 20 74 72 79 20 74 6f 0a 2a 2a 20 74   you try to.** t
11e00 72 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20 74  runcate a file t
11e01 6f 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61 74  o some size that
11e02 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
11e03 69 74 20 63 75 72 72 65 6e 74 6c 79 20 69 73 2c  it currently is,
11e04 0a 2a 2a 20 73 6f 20 64 65 74 65 63 74 20 74 68  .** so detect th
11e05 69 73 20 63 61 73 65 20 61 6e 64 20 77 72 69 74  is case and writ
11e06 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20  e a single zero 
11e07 62 79 74 65 20 74 6f 20 74 68 65 20 65 6e 64 20  byte to the end 
11e08 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a 20 66 69  of the new.** fi
11e09 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 73  le instead..*/.s
11e0a 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f  tatic int pager_
11e0b 74 72 75 6e 63 61 74 65 28 50 61 67 65 72 20 2a  truncate(Pager *
11e0c 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 61  pPager, Pgno nPa
11e0d 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ge){.  int rc = 
11e0e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
11e0f 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
11e10 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20  PAGER_EXCLUSIVE 
11e11 26 26 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70  && pPager->fd->p
11e12 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 69  Methods ){.    i
11e13 36 34 20 63 75 72 72 65 6e 74 53 69 7a 65 2c 20  64 currentSize, 
11e14 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 72 63 20  newSize;.    rc 
11e15 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
11e16 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ize(pPager->fd, 
11e17 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20  &currentSize);. 
11e18 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61     newSize = pPa
11e19 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69  ger->pageSize*(i
11e1a 36 34 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66  64)nPage;.    if
11e1b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
11e1c 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d  && currentSize!=
11e1d 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  newSize ){.     
11e1e 20 69 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65   if( currentSize
11e1f 3e 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20  >newSize ){.    
11e20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
11e21 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65  OsTruncate(pPage
11e22 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b  r->fd, newSize);
11e23 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
11e24 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
11e25 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
11e26 2d 3e 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77  ->fd, "", 1, new
11e27 53 69 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d  Size-1);.      }
11e28 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
11e29 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
11e2a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69      pPager->dbFi
11e2b 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a  leSize = nPage;.
11e2c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
11e2d 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
11e2e 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
11e2f 73 65 63 74 6f 72 53 69 7a 65 20 66 6f 72 20 74  sectorSize for t
11e30 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 2e 0a  he given pager..
11e31 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 74 6f 72  **.** The sector
11e32 20 73 69 7a 65 20 69 73 20 61 74 20 6c 65 61 73   size is at leas
11e33 74 20 61 73 20 62 69 67 20 61 73 20 74 68 65 20  t as big as the 
11e34 73 65 63 74 6f 72 20 73 69 7a 65 20 72 65 70 6f  sector size repo
11e35 72 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74  rted.** by sqlit
11e36 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29  e3OsSectorSize()
11e37 2e 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 65  . The minimum se
11e38 63 74 6f 72 20 73 69 7a 65 20 69 73 20 35 31 32  ctor size is 512
11e39 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
11e3a 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 50   setSectorSize(P
11e3b 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
11e3c 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e   assert(pPager->
11e3d 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50  fd->pMethods||pP
11e3e 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b  ager->tempFile);
11e3f 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
11e40 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  tempFile ){.    
11e41 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 64  /* Sector size d
11e42 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 66 6f  oesn't matter fo
11e43 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  r temporary file
11e44 73 2e 20 41 6c 73 6f 2c 20 74 68 65 20 66 69 6c  s. Also, the fil
11e45 65 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f 74  e.    ** may not
11e46 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65   have been opene
11e47 64 20 79 65 74 2c 20 69 6e 20 77 68 63 69 68 20  d yet, in whcih 
11e48 63 61 73 65 20 74 68 65 20 4f 73 53 65 63 74 6f  case the OsSecto
11e49 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 63  rSize().    ** c
11e4a 61 6c 6c 20 77 69 6c 6c 20 73 65 67 66 61 75 6c  all will segfaul
11e4b 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50  t..    */.    pP
11e4c 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
11e4d 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74   = sqlite3OsSect
11e4e 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66  orSize(pPager->f
11e4f 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  d);.  }.  if( pP
11e50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
11e51 3c 35 31 32 20 29 7b 0a 20 20 20 20 70 50 61 67  <512 ){.    pPag
11e52 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d  er->sectorSize =
11e53 20 35 31 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20   512;.  }.  if( 
11e54 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
11e55 7a 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49  ze>MAX_SECTOR_SI
11e56 5a 45 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  ZE ){.    pPager
11e57 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 4d  ->sectorSize = M
11e58 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a  AX_SECTOR_SIZE;.
11e59 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61    }.}../*.** Pla
11e5a 79 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61  yback the journa
11e5b 6c 20 61 6e 64 20 74 68 75 73 20 72 65 73 74 6f  l and thus resto
11e5c 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
11e5d 66 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73  file to.** the s
11e5e 74 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 62  tate it was in b
11e5f 65 66 6f 72 65 20 77 65 20 73 74 61 72 74 65 64  efore we started
11e60 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e   making changes.
11e61 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75    .**.** The jou
11e62 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74  rnal file format
11e63 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20   is as follows: 
11e64 0a 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 20 62  .**.**  (1)  8 b
11e65 79 74 65 20 70 72 65 66 69 78 2e 20 20 41 20 63  yte prefix.  A c
11e66 6f 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d  opy of aJournalM
11e67 61 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20  agic[]..**  (2) 
11e68 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69   4 byte big-endi
11e69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68  an integer which
11e6a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
11e6b 66 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63  f valid page rec
11e6c 6f 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e  ords.**       in
11e6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49   the journal.  I
11e6e 66 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20  f this value is 
11e6f 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e  0xffffffff, then
11e70 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20   compute the.** 
11e71 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20        number of 
11e72 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 72 6f  page records fro
11e73 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69  m the journal si
11e74 7a 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 20 62  ze..**  (3)  4 b
11e75 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69  yte big-endian i
11e76 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20  nteger which is 
11e77 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75  the initial valu
11e78 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20  e for the .**   
11e79 20 20 20 20 73 61 6e 69 74 79 20 63 68 65 63 6b      sanity check
11e7a 73 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20  sum..**  (4)  4 
11e7b 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 69  byte integer whi
11e7c 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ch is the number
11e7d 20 6f 66 20 70 61 67 65 73 20 74 6f 20 74 72 75   of pages to tru
11e7e 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 20 20  ncate the.**    
11e7f 20 20 20 64 61 74 61 62 61 73 65 20 74 6f 20 64     database to d
11e80 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b  uring a rollback
11e81 2e 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 79 74  ..**  (5)  4 byt
11e82 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74  e big-endian int
11e83 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
11e84 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 20  e sector size.  
11e85 54 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 20 20  The header.**   
11e86 20 20 20 20 69 73 20 74 68 69 73 20 6d 61 6e 79      is this many
11e87 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a   bytes in size..
11e88 2a 2a 20 20 28 36 29 20 20 34 20 62 79 74 65 20  **  (6)  4 byte 
11e89 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67  big-endian integ
11e8a 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20  er which is the 
11e8b 70 61 67 65 20 63 61 73 65 2e 0a 2a 2a 20 20 28  page case..**  (
11e8c 37 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67  7)  4 byte integ
11e8d 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20  er which is the 
11e8e 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
11e8f 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  in the master jo
11e90 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e  urnal.**       n
11e91 61 6d 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20  ame.  The value 
11e92 6d 61 79 20 62 65 20 7a 65 72 6f 20 28 69 6e 64  may be zero (ind
11e93 69 63 61 74 65 20 74 68 61 74 20 74 68 65 72 65  icate that there
11e94 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a   is no master.**
11e95 20 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29         journal.)
11e96 0a 2a 2a 20 20 28 38 29 20 20 4e 20 62 79 74 65  .**  (8)  N byte
11e97 73 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  s of the master 
11e98 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54  journal name.  T
11e99 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20  he name will be 
11e9a 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a  nul-terminated.*
11e9b 2a 20 20 20 20 20 20 20 61 6e 64 20 6d 69 67 68  *       and migh
11e9c 74 20 62 65 20 73 68 6f 72 74 65 72 20 74 68 61  t be shorter tha
11e9d 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64  n the value read
11e9e 20 66 72 6f 6d 20 28 35 29 2e 20 20 49 66 20 74   from (5).  If t
11e9f 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 2a 2a  he first byte.**
11ea0 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 61         of the na
11ea1 6d 65 20 69 73 20 5c 30 30 30 20 74 68 65 6e 20  me is \000 then 
11ea2 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 73 74  there is no mast
11ea3 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65  er journal.  The
11ea4 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 20   master.**      
11ea5 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73   journal name is
11ea6 20 73 74 6f 72 65 64 20 69 6e 20 55 54 46 2d 38   stored in UTF-8
11ea7 2e 0a 2a 2a 20 20 28 39 29 20 20 5a 65 72 6f 20  ..**  (9)  Zero 
11ea8 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 69 6e  or more pages in
11ea9 73 74 61 6e 63 65 73 2c 20 65 61 63 68 20 61 73  stances, each as
11eaa 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20   follows:.**    
11eab 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 70 61      +  4 byte pa
11eac 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20  ge number..**   
11ead 20 20 20 20 20 2b 20 20 70 50 61 67 65 72 2d 3e       +  pPager->
11eae 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f  pageSize bytes o
11eaf 66 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20  f data..**      
11eb0 20 20 2b 20 20 34 20 62 79 74 65 20 63 68 65 63    +  4 byte chec
11eb1 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ksum.**.** When 
11eb2 77 65 20 73 70 65 61 6b 20 6f 66 20 74 68 65 20  we speak of the 
11eb3 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20  journal header, 
11eb4 77 65 20 6d 65 61 6e 20 74 68 65 20 66 69 72 73  we mean the firs
11eb5 74 20 38 20 69 74 65 6d 73 20 61 62 6f 76 65 2e  t 8 items above.
11eb6 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69  .** Each entry i
11eb7 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  n the journal is
11eb8 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
11eb9 74 68 65 20 39 74 68 20 69 74 65 6d 2e 0a 2a 2a  the 9th item..**
11eba 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76 61 6c  .** Call the val
11ebb 75 65 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f  ue from the seco
11ebc 6e 64 20 62 75 6c 6c 65 74 20 22 6e 52 65 63 22  nd bullet "nRec"
11ebd 2e 20 20 6e 52 65 63 20 69 73 20 74 68 65 20 6e  .  nRec is the n
11ebe 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69  umber of.** vali
11ebf 64 20 70 61 67 65 20 65 6e 74 72 69 65 73 20 69  d page entries i
11ec0 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  n the journal.  
11ec1 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 79  In most cases, y
11ec2 6f 75 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74  ou can compute t
11ec3 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e  he.** value of n
11ec4 52 65 63 20 66 72 6f 6d 20 74 68 65 20 73 69 7a  Rec from the siz
11ec5 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
11ec6 20 66 69 6c 65 2e 20 20 42 75 74 20 69 66 20 61   file.  But if a
11ec7 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72   power.** failur
11ec8 65 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65  e occurred while
11ec9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73   the journal was
11eca 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20   being written, 
11ecb 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 65 0a  it could be the.
11ecc 2a 2a 20 63 61 73 65 20 74 68 61 74 20 74 68 65  ** case that the
11ecd 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75   size of the jou
11ece 72 6e 61 6c 20 66 69 6c 65 20 68 61 64 20 61 6c  rnal file had al
11ecf 72 65 61 64 79 20 62 65 65 6e 20 69 6e 63 72 65  ready been incre
11ed0 61 73 65 64 20 62 75 74 0a 2a 2a 20 74 68 65 20  ased but.** the 
11ed1 65 78 74 72 61 20 65 6e 74 72 69 65 73 20 68 61  extra entries ha
11ed2 64 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 69  d not yet made i
11ed3 74 20 73 61 66 65 6c 79 20 74 6f 20 64 69 73 6b  t safely to disk
11ed4 2e 20 20 49 6e 20 73 75 63 68 20 61 20 63 61 73  .  In such a cas
11ed5 65 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  e,.** the value 
11ed6 6f 66 20 6e 52 65 63 20 63 6f 6d 70 75 74 65 64  of nRec computed
11ed7 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73   from the file s
11ed8 69 7a 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f  ize would be too
11ed9 20 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20   large.  For.** 
11eda 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20  that reason, we 
11edb 61 6c 77 61 79 73 20 75 73 65 20 74 68 65 20 6e  always use the n
11edc 52 65 63 20 76 61 6c 75 65 20 69 6e 20 74 68 65  Rec value in the
11edd 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49   header..**.** I
11ede 66 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65  f the nRec value
11edf 20 69 73 20 30 78 66 66 66 66 66 66 66 66 20 69   is 0xffffffff i
11ee0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 52 65  t means that nRe
11ee1 63 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70  c should be comp
11ee2 75 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  uted.** from the
11ee3 20 66 69 6c 65 20 73 69 7a 65 2e 20 20 54 68 69   file size.  Thi
11ee4 73 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20  s value is used 
11ee5 77 68 65 6e 20 74 68 65 20 75 73 65 72 20 73 65  when the user se
11ee6 6c 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d  lects the.** no-
11ee7 73 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20  sync option for 
11ee8 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20  the journal.  A 
11ee9 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f  power failure co
11eea 75 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f 72 72  uld lead to corr
11eeb 75 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69  uption.** in thi
11eec 73 20 63 61 73 65 2e 20 20 42 75 74 20 66 6f 72  s case.  But for
11eed 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d   things like tem
11eee 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28 77 68  porary table (wh
11eef 69 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64  ich will be.** d
11ef0 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20  eleted when the 
11ef1 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65  power is restore
11ef2 64 29 20 77 65 20 64 6f 6e 27 74 20 63 61 72 65  d) we don't care
11ef3 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  .  .**.** If the
11ef4 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 73 20   file opened as 
11ef5 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11ef6 20 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66   is not a well-f
11ef7 6f 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  ormed.** journal
11ef8 20 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70   file then all p
11ef9 61 67 65 73 20 75 70 20 74 6f 20 74 68 65 20 66  ages up to the f
11efa 69 72 73 74 20 63 6f 72 72 75 70 74 65 64 20 70  irst corrupted p
11efb 61 67 65 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a  age are rolled.*
11efc 2a 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61  * back (or no pa
11efd 67 65 73 20 69 66 20 74 68 65 20 6a 6f 75 72 6e  ges if the journ
11efe 61 6c 20 68 65 61 64 65 72 20 69 73 20 63 6f 72  al header is cor
11eff 72 75 70 74 65 64 29 2e 20 54 68 65 20 6a 6f 75  rupted). The jou
11f00 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20  rnal file.** is 
11f01 74 68 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64  then deleted and
11f02 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
11f03 6e 65 64 2c 20 6a 75 73 74 20 61 73 20 69 66 20  ned, just as if 
11f04 6e 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20 68 61  no corruption ha
11f05 64 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e  d.** been encoun
11f06 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  tered..**.** If 
11f07 61 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63  an I/O or malloc
11f08 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  () error occurs,
11f09 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c   the journal-fil
11f0a 65 20 69 73 20 6e 6f 74 20 64 65 6c 65 74 65 64  e is not deleted
11f0b 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  .** and an error
11f0c 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
11f0d 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
11f0e 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
11f0f 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
11f10 6e 74 20 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c  nt isHot){.  sql
11f11 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
11f12 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20   pPager->pVfs;. 
11f13 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20   i64 szJ;       
11f14 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
11f15 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
11f16 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a   file in bytes *
11f17 2f 0a 20 20 75 33 32 20 6e 52 65 63 3b 20 20 20  /.  u32 nRec;   
11f18 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11f19 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 6f 72 64  Number of Record
11f1a 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  s in the journal
11f1b 20 2a 2f 0a 20 20 75 33 32 20 75 3b 20 20 20 20   */.  u32 u;    
11f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11f1d 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20  * Unsigned loop 
11f1e 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 50 67 6e  counter */.  Pgn
11f1f 6f 20 6d 78 50 67 20 3d 20 30 3b 20 20 20 20 20  o mxPg = 0;     
11f20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
11f21 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69   the original fi
11f22 6c 65 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20  le in pages */. 
11f23 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
11f24 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
11f25 75 6c 74 20 63 6f 64 65 20 6f 66 20 61 20 73 75  ult code of a su
11f26 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
11f27 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20  t res = 1;      
11f28 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
11f29 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
11f2a 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 2a 2f  te3OsAccess() */
11f2b 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  .  char *zMaster
11f2c 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e   = 0;       /* N
11f2d 61 6d 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f  ame of master jo
11f2e 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 61 6e  urnal file if an
11f2f 79 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72  y */..  /* Figur
11f30 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72  e out how many r
11f31 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20 74 68  ecords are in th
11f32 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72  e journal.  Abor
11f33 74 20 65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20  t early if.  ** 
11f34 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65  the journal is e
11f35 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  mpty..  */.  ass
11f36 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ert( pPager->jou
11f37 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 72 63  rnalOpen );.  rc
11f38 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
11f39 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  Size(pPager->jfd
11f3a 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72  , &szJ);.  if( r
11f3b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
11f3c 73 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f  szJ==0 ){.    go
11f3d 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b  to end_playback;
11f3e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20  .  }..  /* Read 
11f3f 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
11f40 61 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65  al name from the
11f41 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20   journal, if it 
11f42 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a  is present..  **
11f43 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   If a master jou
11f44 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69  rnal file name i
11f45 73 20 73 70 65 63 69 66 69 65 64 2c 20 62 75 74  s specified, but
11f46 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74   the file is not
11f47 0a 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e  .  ** present on
11f48 20 64 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20   disk, then the 
11f49 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68  journal is not h
11f4a 6f 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20  ot and does not 
11f4b 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20  need to be.  ** 
11f4c 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a  played back..  *
11f4d 2f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50  /.  zMaster = pP
11f4e 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b  ager->pTmpSpace;
11f4f 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74  .  rc = readMast
11f50 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  erJournal(pPager
11f51 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20  ->jfd, zMaster, 
11f52 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78  pPager->pVfs->mx
11f53 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69  Pathname+1);.  i
11f54 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
11f55 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29   && zMaster[0] )
11f56 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
11f57 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c  e3OsAccess(pVfs,
11f58 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45   zMaster, SQLITE
11f59 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20  _ACCESS_EXISTS, 
11f5a 26 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61  &res);.  }.  zMa
11f5b 73 74 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20  ster = 0;.  if( 
11f5c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
11f5d 20 21 72 65 73 20 29 7b 0a 20 20 20 20 67 6f 74   !res ){.    got
11f5e 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
11f5f 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f    }.  pPager->jo
11f60 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 0a 20  urnalOff = 0;.. 
11f61 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74 65   /* This loop te
11f62 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72 20  rminates either 
11f63 77 68 65 6e 20 74 68 65 20 72 65 61 64 4a 6f 75  when the readJou
11f64 72 6e 61 6c 48 64 72 28 29 20 63 61 6c 6c 20 72  rnalHdr() call r
11f65 65 74 75 72 6e 73 0a 20 20 2a 2a 20 53 51 4c 49  eturns.  ** SQLI
11f66 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f  TE_DONE or an IO
11f67 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a   error occurs. *
11f68 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a  /.  while( 1 ){.
11f69 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65  .    /* Read the
11f6a 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65   next journal he
11f6b 61 64 65 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f  ader from the jo
11f6c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20  urnal file.  If 
11f6d 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 2a 2a  there are.    **
11f6e 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74 65   not enough byte
11f6f 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a 6f  s left in the jo
11f70 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 61  urnal file for a
11f71 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72   complete header
11f72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20 69  , or.    ** it i
11f73 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65  s corrupted, the
11f74 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73 74  n a process must
11f75 20 6f 66 20 66 61 69 6c 65 64 20 77 68 69 6c 65   of failed while
11f76 20 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20   writing it..   
11f77 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74   ** This indicat
11f78 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20  es nothing more 
11f79 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c  needs to be roll
11f7a 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a  ed back..    */.
11f7b 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75      rc = readJou
11f7c 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20  rnalHdr(pPager, 
11f7d 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d 78 50  szJ, &nRec, &mxP
11f7e 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  g);.    if( rc!=
11f7f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a 20 20  SQLITE_OK ){ .  
11f80 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
11f81 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  TE_DONE ){.     
11f82 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
11f83 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  K;.      }.     
11f84 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61   goto end_playba
11f85 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ck;.    }..    /
11f86 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 78 66  * If nRec is 0xf
11f87 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 74 68  fffffff, then th
11f88 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 63  is journal was c
11f89 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63  reated by a proc
11f8a 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 69  ess.    ** worki
11f8b 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f  ng in no-sync mo
11f8c 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74  de. This means t
11f8d 68 61 74 20 74 68 65 20 72 65 73 74 20 6f 66 20  hat the rest of 
11f8e 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  the journal.    
11f8f 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 74 73  ** file consists
11f90 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65 72 65   of pages, there
11f91 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75   are no more jou
11f92 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 43 6f  rnal headers. Co
11f93 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 68 65  mpute.    ** the
11f94 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 62   value of nRec b
11f95 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61 73 73  ased on this ass
11f96 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  umption..    */.
11f97 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 78      if( nRec==0x
11f98 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20  ffffffff ){.    
11f99 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
11f9a 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f  ->journalOff==JO
11f9b 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
11f9c 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 6e 52  ger) );.      nR
11f9d 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20  ec = (int)((szJ 
11f9e 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  - JOURNAL_HDR_SZ
11f9f 28 70 50 61 67 65 72 29 29 2f 4a 4f 55 52 4e 41  (pPager))/JOURNA
11fa0 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29  L_PG_SZ(pPager))
11fa1 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
11fa2 49 66 20 6e 52 65 63 20 69 73 20 30 20 61 6e 64  If nRec is 0 and
11fa3 20 74 68 69 73 20 72 6f 6c 6c 62 61 63 6b 20 69   this rollback i
11fa4 73 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69  s of a transacti
11fa5 6f 6e 20 63 72 65 61 74 65 64 20 62 79 20 74 68  on created by th
11fa6 69 73 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73  is.    ** proces
11fa7 73 20 61 6e 64 20 69 66 20 74 68 69 73 20 69 73  s and if this is
11fa8 20 74 68 65 20 66 69 6e 61 6c 20 68 65 61 64 65   the final heade
11fa9 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  r in the journal
11faa 2c 20 74 68 65 6e 20 69 74 20 6d 65 61 6e 73 0a  , then it means.
11fab 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 69 73      ** that this
11fac 20 70 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75   part of the jou
11fad 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 20 66  rnal was being f
11fae 69 6c 6c 65 64 20 62 75 74 20 68 61 73 20 6e 6f  illed but has no
11faf 74 20 79 65 74 20 62 65 65 6e 0a 20 20 20 20 2a  t yet been.    *
11fb0 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b  * synced to disk
11fb1 2e 20 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6e  .  Compute the n
11fb2 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 62  umber of pages b
11fb3 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6d 61  ased on the rema
11fb4 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 73 69 7a  ining.    ** siz
11fb5 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 20  e of the file.. 
11fb6 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
11fb7 20 74 68 69 72 64 20 74 65 72 6d 20 6f 66 20 74   third term of t
11fb8 68 65 20 74 65 73 74 20 77 61 73 20 61 64 64 65  he test was adde
11fb9 64 20 74 6f 20 66 69 78 20 74 69 63 6b 65 74 20  d to fix ticket 
11fba 23 32 35 36 35 2e 0a 20 20 20 20 2a 2a 20 57 68  #2565..    ** Wh
11fbb 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  en rolling back 
11fbc 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2c 20 6e  a hot journal, n
11fbd 52 65 63 3d 3d 30 20 61 6c 77 61 79 73 20 6d 65  Rec==0 always me
11fbe 61 6e 73 20 74 68 61 74 20 74 68 65 20 6e 65 78  ans that the nex
11fbf 74 0a 20 20 20 20 2a 2a 20 63 68 75 6e 6b 20 6f  t.    ** chunk o
11fc0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 63 6f  f the journal co
11fc1 6e 74 61 69 6e 73 20 7a 65 72 6f 20 70 61 67 65  ntains zero page
11fc2 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  s to be rolled b
11fc3 61 63 6b 2e 20 20 42 75 74 0a 20 20 20 20 2a 2a  ack.  But.    **
11fc4 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 52 4f   when doing a RO
11fc5 4c 4c 42 41 43 4b 20 61 6e 64 20 74 68 65 20 6e  LLBACK and the n
11fc6 52 65 63 3d 3d 30 20 63 68 75 6e 6b 20 69 73 20  Rec==0 chunk is 
11fc7 74 68 65 20 6c 61 73 74 20 63 68 75 6e 6b 20 69  the last chunk i
11fc8 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75  n.    ** the jou
11fc9 72 6e 61 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74  rnal, it means t
11fca 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
11fcb 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 64  might contain ad
11fcc 64 69 74 69 6f 6e 61 6c 0a 20 20 20 20 2a 2a 20  ditional.    ** 
11fcd 70 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 20  pages that need 
11fce 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  to be rolled bac
11fcf 6b 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 6e  k and that the n
11fd0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 0a  umber of pages .
11fd1 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65      ** should be
11fd2 20 63 6f 6d 70 75 74 65 64 20 62 61 73 65 64 20   computed based 
11fd3 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  on the journal f
11fd4 69 6c 65 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f  ile size..    */
11fd5 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e  .    testcase( n
11fd6 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74  Rec==0 && !isHot
11fd7 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61  .         && pPa
11fd8 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b  ger->journalHdr+
11fd9 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
11fda 50 61 67 65 72 29 21 3d 70 50 61 67 65 72 2d 3e  Pager)!=pPager->
11fdb 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 20  journalOff.     
11fdc 20 20 20 20 26 26 20 28 28 73 7a 4a 20 2d 20 70      && ((szJ - p
11fdd 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11fde 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f  f) / JOURNAL_PG_
11fdf 53 5a 28 70 50 61 67 65 72 29 29 3e 30 0a 20 20  SZ(pPager))>0.  
11fe0 20 20 20 20 20 20 20 26 26 20 70 61 67 65 72 4e         && pagerN
11fe1 65 78 74 4a 6f 75 72 6e 61 6c 50 61 67 65 49 73  extJournalPageIs
11fe2 56 61 6c 69 64 28 70 50 61 67 65 72 29 0a 20 20  Valid(pPager).  
11fe3 20 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 52 65    );.    if( nRe
11fe4 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26  c==0 && !isHot &
11fe5 26 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  &.        pPager
11fe6 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55  ->journalHdr+JOU
11fe7 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
11fe8 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75  er)==pPager->jou
11fe9 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20  rnalOff ){.     
11fea 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73   nRec = (int)((s
11feb 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75  zJ - pPager->jou
11fec 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e  rnalOff) / JOURN
11fed 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29  AL_PG_SZ(pPager)
11fee 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
11fef 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
11ff0 66 69 72 73 74 20 68 65 61 64 65 72 20 72 65 61  first header rea
11ff1 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e  d from the journ
11ff2 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65  al, truncate the
11ff3 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
11ff4 20 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74   file back to it
11ff5 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e  s original size.
11ff6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
11ff7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11ff8 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ff==JOURNAL_HDR_
11ff9 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  SZ(pPager) ){.  
11ffa 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74      rc = pager_t
11ffb 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20  runcate(pPager, 
11ffc 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28  mxPg);.      if(
11ffd 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11ffe 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  {.        goto e
11fff 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20  nd_playback;.   
12000 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65     }.      pPage
12001 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78 50 67  r->dbSize = mxPg
12002 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
12003 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61  Copy original pa
12004 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a  ges out of the j
12005 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20  ournal and back 
12006 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
12007 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20  e file..    */. 
12008 20 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52     for(u=0; u<nR
12009 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20  ec; u++){.      
1200a 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62  rc = pager_playb
1200b 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61  ack_one_page(pPa
1200c 67 65 72 2c 20 31 2c 20 26 70 50 61 67 65 72 2d  ger, 1, &pPager-
1200d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 30 2c 20  >journalOff, 0, 
1200e 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
1200f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
12010 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
12011 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
12012 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
12013 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20  ITE_OK;.        
12014 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
12015 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 20  lOff = szJ;.    
12016 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
12017 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
12018 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61        /* If we a
12019 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c  re unable to rol
1201a 6c 62 61 63 6b 2c 20 74 68 65 6e 20 74 68 65 20  lback, then the 
1201b 64 61 74 61 62 61 73 65 20 69 73 20 70 72 6f 62  database is prob
1201c 61 62 6c 79 0a 20 20 20 20 20 20 20 20 20 20 2a  ably.          *
1201d 2a 20 67 6f 69 6e 67 20 74 6f 20 65 6e 64 20 75  * going to end u
1201e 70 20 62 65 69 6e 67 20 63 6f 72 72 75 70 74 2e  p being corrupt.
1201f 20 20 49 74 20 69 73 20 63 6f 72 72 75 70 74 20    It is corrupt 
12020 74 6f 20 75 73 2c 20 61 6e 79 68 6f 77 2e 0a 20  to us, anyhow.. 
12021 20 20 20 20 20 20 20 20 20 2a 2a 20 50 65 72 68           ** Perh
12022 61 70 73 20 74 68 65 20 6e 65 78 74 20 70 72 6f  aps the next pro
12023 63 65 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f  cess to come alo
12024 6e 67 20 63 61 6e 20 66 69 78 20 69 74 2e 2e 2e  ng can fix it...
12025 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
12026 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
12027 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
12028 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  T;.          got
12029 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
1202a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1202b 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  }.    }.  }.  /*
1202c 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61  NOTREACHED*/.  a
1202d 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64  ssert( 0 );..end
1202e 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20  _playback:.  /* 
1202f 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c  Following a roll
12030 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61  back, the databa
12031 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  se file should b
12032 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20 6f 72  e back in its or
12033 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74  iginal.  ** stat
12034 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73  e prior to the s
12035 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e  tart of the tran
12036 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f  saction, so invo
12037 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49  ke the.  ** SQLI
12038 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48  TE_FCNTL_DB_UNCH
12039 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72  ANGED file-contr
1203a 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 73  ol method to dis
1203b 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 73  able the.  ** as
1203c 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74 68 65  sertion that the
1203d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75   transaction cou
1203e 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66 69 65  nter was modifie
1203f 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  d..  */.  assert
12040 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 66 64  (.    pPager->fd
12041 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c  ->pMethods==0 ||
12042 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69  .    sqlite3OsFi
12043 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72  leControl(pPager
12044 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54  ->fd,SQLITE_FCNT
12045 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30  L_DB_UNCHANGED,0
12046 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29  )>=SQLITE_OK.  )
12047 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
12048 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d  ITE_OK ){.    zM
12049 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e  aster = pPager->
1204a 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72  pTmpSpace;.    r
1204b 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f  c = readMasterJo
1204c 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66  urnal(pPager->jf
1204d 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67  d, zMaster, pPag
1204e 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68  er->pVfs->mxPath
1204f 6e 61 6d 65 2b 31 29 3b 0a 20 20 7d 0a 20 20 69  name+1);.  }.  i
12050 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12051 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   ){.    rc = pag
12052 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  er_end_transacti
12053 6f 6e 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74  on(pPager, zMast
12054 65 72 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20  er[0]!='\0');.  
12055 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
12056 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72  TE_OK && zMaster
12057 5b 30 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20  [0] && res ){.  
12058 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61    /* If there wa
12059 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  s a master journ
1205a 61 6c 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  al and this rout
1205b 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ine will return 
1205c 73 75 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20  success,.    ** 
1205d 73 65 65 20 69 66 20 69 74 20 69 73 20 70 6f 73  see if it is pos
1205e 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20  sible to delete 
1205f 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
12060 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  al..    */.    r
12061 63 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73  c = pager_delmas
12062 74 65 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73  ter(pPager, zMas
12063 74 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ter);.  }..  /* 
12064 54 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72  The Pager.sector
12065 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61  Size variable ma
12066 79 20 68 61 76 65 20 62 65 65 6e 20 75 70 64 61  y have been upda
12067 74 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e  ted while rollin
12068 67 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f  g.  ** back a jo
12069 75 72 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79  urnal created by
1206a 20 61 20 70 72 6f 63 65 73 73 20 77 69 74 68 20   a process with 
1206b 61 20 64 69 66 66 65 72 65 6e 74 20 73 65 63 74  a different sect
1206c 6f 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c  or size.  ** val
1206d 75 65 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20  ue. Reset it to 
1206e 74 68 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75  the correct valu
1206f 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65  e for this proce
12070 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65  ss..  */.  setSe
12071 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29  ctorSize(pPager)
12072 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
12073 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b  ../*.** Playback
12074 20 73 61 76 65 70 6f 69 6e 74 20 70 53 61 76 65   savepoint pSave
12075 70 6f 69 6e 74 2e 20 20 4f 72 2c 20 69 66 20 70  point.  Or, if p
12076 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c  Savepoint==NULL,
12077 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 0a 2a   then playback.*
12078 2a 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73  * the entire mas
12079 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1207a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 73 65  ..**.** The case
1207b 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c   pSavepoint==NUL
1207c 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 61 20  L occurs when a 
1207d 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 63 6f 6d 6d  ROLLBACK TO comm
1207e 61 6e 64 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a  and is invoked.*
1207f 2a 20 6f 6e 20 61 20 53 41 56 45 50 4f 49 4e 54  * on a SAVEPOINT
12080 20 74 68 61 74 20 69 73 20 61 20 74 72 61 6e 73   that is a trans
12081 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74  action savepoint
12082 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
12083 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76  pagerPlaybackSav
12084 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50  epoint(Pager *pP
12085 61 67 65 72 2c 20 50 61 67 65 72 53 61 76 65 70  ager, PagerSavep
12086 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74  oint *pSavepoint
12087 29 7b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20  ){.  i64 szJ;   
12088 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12089 20 45 66 66 65 63 74 69 76 65 20 73 69 7a 65 20   Effective size 
1208a 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  of the main jour
1208b 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64  nal */.  i64 iHd
1208c 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  rOff;           
1208d 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 72 73    /* End of firs
1208e 74 20 73 65 67 6d 65 6e 74 20 6f 66 20 6d 61 69  t segment of mai
1208f 6e 2d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64  n-journal record
12090 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 69 3b 20  s */.  Pgno ii; 
12091 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12092 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
12093 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
12094 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a  LITE_OK;      /*
12095 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
12096 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20    Bitvec *pDone 
12097 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 69  = 0;       /* Bi
12098 74 76 65 63 20 74 6f 20 65 6e 73 75 72 65 20 70  tvec to ensure p
12099 61 67 65 73 20 70 6c 61 79 65 64 20 62 61 63 6b  ages played back
1209a 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20   only once */.. 
1209b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62   /* Allocate a b
1209c 69 74 76 65 63 20 74 6f 20 75 73 65 20 74 6f 20  itvec to use to 
1209d 73 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66  store the set of
1209e 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61   pages rolled ba
1209f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76  ck */.  if( pSav
120a0 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 70 44  epoint ){.    pD
120a1 6f 6e 65 20 3d 20 73 71 6c 69 74 65 33 42 69 74  one = sqlite3Bit
120a2 76 65 63 43 72 65 61 74 65 28 70 53 61 76 65 70  vecCreate(pSavep
120a3 6f 69 6e 74 2d 3e 6e 4f 72 69 67 29 3b 0a 20 20  oint->nOrig);.  
120a4 20 20 69 66 28 20 21 70 44 6f 6e 65 20 29 7b 0a    if( !pDone ){.
120a5 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
120a6 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
120a7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 75 6e 63  .  }..  /* Trunc
120a8 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ate the database
120a9 20 62 61 63 6b 20 74 6f 20 74 68 65 20 73 69 7a   back to the siz
120aa 65 20 69 74 20 77 61 73 20 62 65 66 6f 72 65 20  e it was before 
120ab 74 68 65 20 0a 20 20 2a 2a 20 73 61 76 65 70 6f  the .  ** savepo
120ac 69 6e 74 20 62 65 69 6e 67 20 72 65 76 65 72 74  int being revert
120ad 65 64 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 20  ed was opened.. 
120ae 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62   */.  pPager->db
120af 53 69 7a 65 20 3d 20 70 53 61 76 65 70 6f 69 6e  Size = pSavepoin
120b0 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e  t ? pSavepoint->
120b1 6e 4f 72 69 67 20 3a 20 70 50 61 67 65 72 2d 3e  nOrig : pPager->
120b2 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 61 73  dbOrigSize;.  as
120b3 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
120b4 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45  ate>=PAGER_SHARE
120b5 44 20 29 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70  D );..  /* Use p
120b6 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
120b7 66 20 61 73 20 74 68 65 20 65 66 66 65 63 74 69  f as the effecti
120b8 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d  ve size of the m
120b9 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a  ain rollback.  *
120ba 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20  * journal.  The 
120bb 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69 67 68  actual file migh
120bc 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e  t be larger than
120bd 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41   this in.  ** PA
120be 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
120bf 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41 47 45  TRUNCATE or PAGE
120c0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  R_JOURNALMODE_PE
120c1 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e 79 74  RSIST.  But anyt
120c2 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74 20 70  hing.  ** past p
120c3 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
120c4 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20  f is off-limits 
120c5 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a  to us..  */.  sz
120c6 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  J = pPager->jour
120c7 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65  nalOff;..  /* Be
120c8 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62  gin by rolling b
120c9 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 6f 6d  ack records from
120ca 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
120cb 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20  l starting at.  
120cc 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  ** PagerSavepoin
120cd 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f  t.iOffset and co
120ce 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20  ntinuing to the 
120cf 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61  next journal hea
120d0 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20  der..  ** There 
120d1 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 73  might be records
120d2 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   in the main jou
120d3 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 61  rnal that have a
120d4 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a   page number.  *
120d5 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  * greater than t
120d6 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62  he current datab
120d7 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65 72  ase size (pPager
120d8 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74 68  ->dbSize) but th
120d9 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65  ose.  ** will be
120da 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d 61 74   skipped automat
120db 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 20 61  ically.  Pages a
120dc 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 6f 6e  re added to pDon
120dd 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a 20 61  e as they.  ** a
120de 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a  re played back..
120df 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65    */.  if( pSave
120e0 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 48 64  point ){.    iHd
120e1 72 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e  rOff = pSavepoin
120e2 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3f 20  t->iHdrOffset ? 
120e3 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72  pSavepoint->iHdr
120e4 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20  Offset : szJ;.  
120e5 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
120e6 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e  lOff = pSavepoin
120e7 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20  t->iOffset;.    
120e8 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
120e9 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e  E_OK && pPager->
120ea 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f  journalOff<iHdrO
120eb 66 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ff ){.      rc =
120ec 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
120ed 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c  one_page(pPager,
120ee 20 31 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75   1, &pPager->jou
120ef 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e  rnalOff, 1, pDon
120f0 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  e);.      assert
120f1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
120f2 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  E );.    }.  }el
120f3 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  se{.    pPager->
120f4 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
120f5 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 6e    }..  /* Contin
120f6 75 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  ue rolling back 
120f7 72 65 63 6f 72 64 73 20 6f 75 74 20 6f 66 20 74  records out of t
120f8 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
120f9 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a  starting at.  **
120fa 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e   the first journ
120fb 61 6c 20 68 65 61 64 65 72 20 73 65 65 6e 20 61  al header seen a
120fc 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75 6e  nd continuing un
120fd 74 69 6c 20 74 68 65 20 65 66 66 65 63 74 69 76  til the effectiv
120fe 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68  e end.  ** of th
120ff 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66  e main journal f
12100 69 6c 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20 74  ile.  Continue t
12101 6f 20 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 61  o skip out-of-ra
12102 6e 67 65 20 70 61 67 65 73 20 61 6e 64 0a 20 20  nge pages and.  
12103 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 61 64 64 69  ** continue addi
12104 6e 67 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20  ng pages rolled 
12105 62 61 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a 20  back to pDone.. 
12106 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 3d   */.  while( rc=
12107 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50  =SQLITE_OK && pP
12108 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
12109 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32 20  <szJ ){.    u32 
1210a 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 20 2f  nJRec = 0;     /
1210b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75 72  * Number of Jour
1210c 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20  nal Records */. 
1210d 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20     u32 dummy;.  
1210e 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e    rc = readJourn
1210f 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 73 7a  alHdr(pPager, sz
12110 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d 6d  J, &nJRec, &dumm
12111 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  y);.    assert( 
12112 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
12113 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a  );..    /*.    *
12114 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a  * The "pPager->j
12115 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41  ournalHdr+JOURNA
12116 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
12117 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ==pPager->journa
12118 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 73  lOff".    ** tes
12119 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f 20  t is related to 
1211a 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20 53  ticket #2565.  S
1211b 65 65 20 74 68 65 20 64 69 73 63 75 73 73 69 6f  ee the discussio
1211c 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  n in the.    ** 
1211d 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29  pager_playback()
1211e 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 64   function for ad
1211f 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
12120 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
12121 20 61 73 73 65 72 74 28 20 21 28 6e 4a 52 65 63   assert( !(nJRec
12122 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20  ==0.         && 
12123 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
12124 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  dr+JOURNAL_HDR_S
12125 5a 28 70 50 61 67 65 72 29 21 3d 70 50 61 67 65  Z(pPager)!=pPage
12126 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20  r->journalOff.  
12127 20 20 20 20 20 20 20 26 26 20 28 28 73 7a 4a 20         && ((szJ 
12128 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  - pPager->journa
12129 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f  lOff) / JOURNAL_
1212a 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3e 30  PG_SZ(pPager))>0
1212b 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 61 67  .         && pag
1212c 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50 61 67  erNextJournalPag
1212d 65 49 73 56 61 6c 69 64 28 70 50 61 67 65 72 29  eIsValid(pPager)
1212e 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28  ).    );.    if(
1212f 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20 20 20   nJRec==0 .     
12130 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
12131 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44  alHdr+JOURNAL_HD
12132 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 50  R_SZ(pPager)==pP
12133 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
12134 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 4a  .    ){.      nJ
12135 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 70 50 61  Rec = (szJ - pPa
12136 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29  ger->journalOff)
12137 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70  /JOURNAL_PG_SZ(p
12138 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  Pager);.    }.  
12139 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d    for(ii=0; rc==
1213a 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c  SQLITE_OK && ii<
1213b 6e 4a 52 65 63 20 26 26 20 70 50 61 67 65 72 2d  nJRec && pPager-
1213c 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b  >journalOff<szJ;
1213d 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63   ii++){.      rc
1213e 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63   = pager_playbac
1213f 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65  k_one_page(pPage
12140 72 2c 20 31 2c 20 26 70 50 61 67 65 72 2d 3e 6a  r, 1, &pPager->j
12141 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44  ournalOff, 1, pD
12142 6f 6e 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65  one);.      asse
12143 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
12144 4f 4e 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ONE );.    }.  }
12145 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53  .  assert( rc!=S
12146 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67  QLITE_OK || pPag
12147 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d  er->journalOff==
12148 73 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e  szJ );..  /* Fin
12149 61 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20  ally,  rollback 
1214a 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73  pages from the s
1214b 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67  ub-journal.  Pag
1214c 65 20 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a  e that were.  **
1214d 20 70 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c   previously roll
1214e 65 64 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74  ed back out of t
1214f 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
12150 28 61 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69  (and are hence i
12151 6e 20 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69  n pDone).  ** wi
12152 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20  ll be skipped.  
12153 4f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67  Out-of-range pag
12154 65 73 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70  es are also skip
12155 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ped..  */.  if( 
12156 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20  pSavepoint ){.  
12157 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 70    i64 offset = p
12158 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52  Savepoint->iSubR
12159 65 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61  ec*(4+pPager->pa
1215a 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 66 6f 72  geSize);.    for
1215b 28 69 69 3d 70 53 61 76 65 70 6f 69 6e 74 2d 3e  (ii=pSavepoint->
1215c 69 53 75 62 52 65 63 3b 20 72 63 3d 3d 53 51 4c  iSubRec; rc==SQL
1215d 49 54 45 5f 4f 4b 26 26 69 69 3c 28 75 33 32 29  ITE_OK&&ii<(u32)
1215e 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63  pPager->stmtNRec
1215f 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61  ; ii++){.      a
12160 73 73 65 72 74 28 20 6f 66 66 73 65 74 20 3d 3d  ssert( offset ==
12161 20 69 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70   ii*(4+pPager->p
12162 61 67 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20  ageSize) );.    
12163 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
12164 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70  yback_one_page(p
12165 50 61 67 65 72 2c 20 30 2c 20 26 6f 66 66 73 65  Pager, 0, &offse
12166 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20  t, 1, pDone);.  
12167 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d      assert( rc!=
12168 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20  SQLITE_DONE );. 
12169 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69     }.  }..  sqli
1216a 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
1216b 28 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 72  (pDone);.  if( r
1216c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1216d 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
1216e 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20  nalOff = szJ;.  
1216f 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
12170 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
12171 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
12172 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70  r of in-memory p
12173 61 67 65 73 20 74 68 61 74 20 61 72 65 20 61 6c  ages that are al
12174 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  lowed..*/.SQLITE
12175 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
12176 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63  lite3PagerSetCac
12177 68 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50  hesize(Pager *pP
12178 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65  ager, int mxPage
12179 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  ){.  sqlite3Pcac
1217a 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 70  heSetCachesize(p
1217b 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20  Pager->pPCache, 
1217c 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  mxPage);.}../*.*
1217d 2a 20 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62  * Adjust the rob
1217e 75 73 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64  ustness of the d
1217f 61 74 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67  atabase to damag
12180 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73  e due to OS cras
12181 68 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20  hes.** or power 
12182 66 61 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e  failures by chan
12183 67 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20  ging the number 
12184 6f 66 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e  of syncs()s when
12185 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20   writing.** the 
12186 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
12187 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 68 72  .  There are thr
12188 65 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a  ee levels:.**.**
12189 20 20 20 20 4f 46 46 20 20 20 20 20 20 20 73 71      OFF       sq
1218a 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73  lite3OsSync() is
1218b 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20   never called.  
1218c 54 68 69 73 20 69 73 20 74 68 65 20 64 65 66 61  This is the defa
1218d 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ult.**          
1218e 20 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72      for temporar
1218f 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20  y and transient 
12190 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  files..**.**    
12191 4e 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f  NORMAL    The jo
12192 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20  urnal is synced 
12193 6f 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69 74  once before writ
12194 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a  es begin on the.
12195 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
12196 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20  database.  This 
12197 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71  is normally adeq
12198 75 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c  uate protection,
12199 20 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   but.**         
1219a 20 20 20 20 20 69 74 20 69 73 20 74 68 65 6f 72       it is theor
1219b 65 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c  etically possibl
1219c 65 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20 75  e, though very u
1219d 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20  nlikely,.**     
1219e 20 20 20 20 20 20 20 20 20 74 68 61 74 20 61 6e           that an
1219f 20 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65   inopertune powe
121a0 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20  r failure could 
121a1 6c 65 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61  leave the journa
121a2 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  l.**            
121a3 20 20 69 6e 20 61 20 73 74 61 74 65 20 77 68 69    in a state whi
121a4 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64  ch would cause d
121a5 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74  amage to the dat
121a6 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20  abase.**        
121a7 20 20 20 20 20 20 77 68 65 6e 20 69 74 20 69 73        when it is
121a8 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a   rolled back..**
121a9 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20  .**    FULL     
121aa 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   The journal is 
121ab 73 79 6e 63 65 64 20 74 77 69 63 65 20 62 65 66  synced twice bef
121ac 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e  ore writes begin
121ad 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   on the.**      
121ae 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
121af 20 28 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69   (with some addi
121b0 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
121b1 6f 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69  on - the nRec fi
121b2 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  eld.**          
121b3 20 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e      of the journ
121b4 61 6c 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e  al header - bein
121b5 67 20 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74  g written in bet
121b6 77 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20  ween the two.** 
121b7 20 20 20 20 20 20 20 20 20 20 20 20 20 73 79 6e               syn
121b8 63 73 29 2e 20 20 49 66 20 77 65 20 61 73 73 75  cs).  If we assu
121b9 6d 65 20 74 68 61 74 20 77 72 69 74 69 6e 67 20  me that writing 
121ba 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  a.**            
121bb 20 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65    single disk se
121bc 63 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20  ctor is atomic, 
121bd 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70  then this mode p
121be 72 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20  rovides.**      
121bf 20 20 20 20 20 20 20 20 61 73 73 75 72 61 6e 63          assuranc
121c0 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e  e that the journ
121c1 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63  al will not be c
121c2 6f 72 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a  orrupted to the.
121c3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
121c4 70 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67  point of causing
121c5 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64   damage to the d
121c6 61 74 61 62 61 73 65 20 64 75 72 69 6e 67 20 72  atabase during r
121c7 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e  ollback..**.** N
121c8 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 73  umeric values as
121c9 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
121ca 65 73 65 20 73 74 61 74 65 73 20 61 72 65 20 4f  ese states are O
121cb 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c  FF==1, NORMAL=2,
121cc 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a  .** and FULL=3..
121cd 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
121ce 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
121cf 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56  GMAS.SQLITE_PRIV
121d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
121d1 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65  PagerSetSafetyLe
121d2 76 65 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65  vel(Pager *pPage
121d3 72 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e  r, int level, in
121d4 74 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20  t bFullFsync){. 
121d5 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20   pPager->noSync 
121d6 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20  =  (level==1 || 
121d7 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
121d8 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72  ) ?1:0;.  pPager
121d9 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c 65  ->fullSync = (le
121da 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65  vel==3 && !pPage
121db 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a  r->tempFile) ?1:
121dc 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e  0;.  pPager->syn
121dd 63 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c  c_flags = (bFull
121de 46 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e  Fsync?SQLITE_SYN
121df 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59  C_FULL:SQLITE_SY
121e0 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66  NC_NORMAL);.  if
121e1 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63  ( pPager->noSync
121e2 20 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53   ) pPager->needS
121e3 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69  ync = 0;.}.#endi
121e4 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  f../*.** The fol
121e5 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61  lowing global va
121e6 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d  riable is increm
121e7 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74  ented whenever t
121e8 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74  he library.** at
121e9 74 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61  tempts to open a
121ea 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e   temporary file.
121eb 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69    This informati
121ec 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a  on is used for.*
121ed 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e  * testing and an
121ee 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a  alysis only.  .*
121ef 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
121f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
121f1 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
121f2 74 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  temp_count = 0;.
121f3 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70  #endif../*.** Op
121f4 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  en a temporary f
121f5 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 72 69 74  ile. .**.** Writ
121f6 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  e the file descr
121f7 69 70 74 6f 72 20 69 6e 74 6f 20 2a 66 64 2e 20  iptor into *fd. 
121f8 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
121f9 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20  K on success or 
121fa 73 6f 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 65 72  some.** other er
121fb 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 66  ror code if we f
121fc 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c  ail. The OS will
121fd 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64   automatically d
121fe 65 6c 65 74 65 20 74 68 65 20 74 65 6d 70 6f 72  elete the tempor
121ff 61 72 79 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e  ary.** file when
12200 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a   it is closed..*
12201 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
12202 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d  ite3PagerOpentem
12203 70 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  p(.  Pager *pPag
12204 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  er,        /* Th
12205 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a  e pager object *
12206 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
12207 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69   *pFile,  /* Wri
12208 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  te the file desc
12209 72 69 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20  riptor here */. 
1220a 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20   int vfsFlags   
1220b 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
1220c 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74  passed through t
1220d 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a  o the VFS */.){.
1220e 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 64 65    int rc;..#ifde
1220f 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
12210 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70  sqlite3_opentemp
12211 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73  _count++;  /* Us
12212 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61  ed for testing a
12213 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79  nd analysis only
12214 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66   */.#endif..  vf
12215 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54  sFlags |=  SQLIT
12216 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
12217 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   | SQLITE_OPEN_C
12218 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20  REATE |.        
12219 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
1221a 45 58 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49  EXCLUSIVE | SQLI
1221b 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
1221c 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71  CLOSE;.  rc = sq
1221d 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67  lite3OsOpen(pPag
1221e 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69  er->pVfs, 0, pFi
1221f 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29  le, vfsFlags, 0)
12220 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d  ;.  assert( rc!=
12221 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 46 69  SQLITE_OK || pFi
12222 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b 0a  le->pMethods );.
12223 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
12224 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
12225 53 74 72 65 73 73 28 76 6f 69 64 20 2a 2c 50 67  Stress(void *,Pg
12226 48 64 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  Hdr *);../*.** C
12227 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  reate a new page
12228 20 63 61 63 68 65 20 61 6e 64 20 70 75 74 20 61   cache and put a
12229 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1222a 70 61 67 65 20 63 61 63 68 65 20 69 6e 20 2a 70  page cache in *p
1222b 70 50 61 67 65 72 2e 0a 2a 2a 20 54 68 65 20 66  pPager..** The f
1222c 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64  ile to be cached
1222d 20 6e 65 65 64 20 6e 6f 74 20 65 78 69 73 74 2e   need not exist.
1222e 20 20 54 68 65 20 66 69 6c 65 20 69 73 20 6e 6f    The file is no
1222f 74 20 6c 6f 63 6b 65 64 20 75 6e 74 69 6c 0a 2a  t locked until.*
12230 2a 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  * the first call
12231 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72   to sqlite3Pager
12232 47 65 74 28 29 20 61 6e 64 20 69 73 20 6f 6e 6c  Get() and is onl
12233 79 20 68 65 6c 64 20 6f 70 65 6e 20 75 6e 74 69  y held open unti
12234 6c 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61  l the.** last pa
12235 67 65 20 69 73 20 72 65 6c 65 61 73 65 64 20 75  ge is released u
12236 73 69 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65  sing sqlite3Page
12237 72 55 6e 72 65 66 28 29 2e 0a 2a 2a 0a 2a 2a 20  rUnref()..**.** 
12238 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  If zFilename is 
12239 4e 55 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64  NULL then a rand
1223a 6f 6d 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f  omly-named tempo
1223b 72 61 72 79 20 66 69 6c 65 20 69 73 20 63 72 65  rary file is cre
1223c 61 74 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64  ated.** and used
1223d 20 61 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20   as the file to 
1223e 62 65 20 63 61 63 68 65 64 2e 20 20 54 68 65 20  be cached.  The 
1223f 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c  file will be del
12240 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69  eted.** automati
12241 63 61 6c 6c 79 20 77 68 65 6e 20 69 74 20 69 73  cally when it is
12242 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49   closed..**.** I
12243 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22  f zFilename is "
12244 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61  :memory:" then a
12245 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ll information i
12246 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 2e  s held in cache.
12247 0a 2a 2a 20 49 74 20 69 73 20 6e 65 76 65 72 20  .** It is never 
12248 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e  written to disk.
12249 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73    This can be us
1224a 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
1224b 61 6e 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20  an.** in-memory 
1224c 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c  database..*/.SQL
1224d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1224e 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
1224f 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
12250 2a 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20  *pVfs,       /* 
12251 54 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65  The virtual file
12252 20 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a   system to use *
12253 2f 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61  /.  Pager **ppPa
12254 67 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ger,         /* 
12255 52 65 74 75 72 6e 20 74 68 65 20 50 61 67 65 72  Return the Pager
12256 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20   structure here 
12257 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
12258 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a  *zFilename,   /*
12259 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
1225a 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70  abase file to op
1225b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74  en */.  int nExt
1225c 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ra,             
1225d 20 2f 2a 20 45 78 74 72 61 20 62 79 74 65 73 20   /* Extra bytes 
1225e 61 70 70 65 6e 64 20 74 6f 20 65 61 63 68 20 69  append to each i
1225f 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f  n-memory page */
12260 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
12261 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
12262 6c 61 67 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67  lags controlling
12263 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
12264 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20  int vfsFlags    
12265 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67           /* flag
12266 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68  s passed through
12267 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e   to sqlite3_vfs.
12268 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20  xOpen() */.){.  
12269 75 38 20 2a 70 50 74 72 3b 0a 20 20 50 61 67 65  u8 *pPtr;.  Page
1226a 72 20 2a 70 50 61 67 65 72 20 3d 20 30 3b 0a 20  r *pPager = 0;. 
1226b 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1226c 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  _OK;.  int i;.  
1226d 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 30  int tempFile = 0
1226e 3b 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20  ;.  int memDb = 
1226f 30 3b 0a 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c  0;.  int readOnl
12270 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 75 73 65  y = 0;.  int use
12271 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73  Journal = (flags
12272 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f   & PAGER_OMIT_JO
12273 55 52 4e 41 4c 29 3d 3d 30 3b 0a 20 20 69 6e 74  URNAL)==0;.  int
12274 20 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66   noReadlock = (f
12275 6c 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f  lags & PAGER_NO_
12276 52 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 0a 20 20  READLOCK)!=0;.  
12277 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53  int journalFileS
12278 69 7a 65 3b 0a 20 20 69 6e 74 20 70 63 61 63 68  ize;.  int pcach
12279 65 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50  eSize = sqlite3P
1227a 63 61 63 68 65 53 69 7a 65 28 29 3b 0a 20 20 69  cacheSize();.  i
1227b 6e 74 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20  nt szPageDflt = 
1227c 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
1227d 41 47 45 5f 53 49 5a 45 3b 0a 20 20 63 68 61 72  AGE_SIZE;.  char
1227e 20 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b   *zPathname = 0;
1227f 0a 20 20 69 6e 74 20 6e 50 61 74 68 6e 61 6d 65  .  int nPathname
12280 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 73 71 6c   = 0;..  if( sql
12281 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  ite3JournalSize(
12282 70 56 66 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d  pVfs)>sqlite3Mem
12283 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b  JournalSize() ){
12284 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65  .    journalFile
12285 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4a 6f  Size = sqlite3Jo
12286 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3b  urnalSize(pVfs);
12287 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6a 6f  .  }else{.    jo
12288 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20  urnalFileSize = 
12289 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
1228a 6c 53 69 7a 65 28 29 3b 0a 20 20 7d 0a 0a 20 20  lSize();.  }..  
1228b 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 72  /* The default r
1228c 65 74 75 72 6e 20 69 73 20 61 20 4e 55 4c 4c 20  eturn is a NULL 
1228d 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 2a 70 70  pointer */.  *pp
1228e 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a  Pager = 0;..  /*
1228f 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f   Compute and sto
12290 72 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  re the full path
12291 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63  name in an alloc
12292 61 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e  ated buffer poin
12293 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a  ted.  ** to by z
12294 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68  Pathname, length
12295 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20   nPathname. Or, 
12296 69 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d  if this is a tem
12297 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a  porary file,.  *
12298 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61  * leave both nPa
12299 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68  thname and zPath
1229a 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20  name set to 0.. 
1229b 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e   */.  if( zFilen
1229c 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65  ame && zFilename
1229d 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68  [0] ){.    nPath
1229e 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50  name = pVfs->mxP
1229f 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a  athname+1;.    z
122a0 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74  Pathname = sqlit
122a1 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61  e3Malloc(nPathna
122a2 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a  me*2);.    if( z
122a3 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20  Pathname==0 ){. 
122a4 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
122a5 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
122a6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
122a7 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20  MIT_MEMORYDB.   
122a8 20 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c   if( strcmp(zFil
122a9 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22  ename,":memory:"
122aa 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65  )==0 ){.      me
122ab 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a  mDb = 1;.      z
122ac 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b  Pathname[0] = 0;
122ad 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  .    }else.#endi
122ae 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 72 63  f.    {.      rc
122af 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c   = sqlite3OsFull
122b0 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a  Pathname(pVfs, z
122b1 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 6e  Filename, nPathn
122b2 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b  ame, zPathname);
122b3 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
122b4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
122b5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
122b6 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20  ee(zPathname);. 
122b7 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
122b8 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 74 68 6e      }.    nPathn
122b9 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
122ba 6c 65 6e 33 30 28 7a 50 61 74 68 6e 61 6d 65 29  len30(zPathname)
122bb 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  ;.  }..  /* Allo
122bc 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20  cate memory for 
122bd 74 68 65 20 70 61 67 65 72 20 73 74 72 75 63 74  the pager struct
122be 75 72 65 20 2a 2f 0a 20 20 70 50 61 67 65 72 20  ure */.  pPager 
122bf 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
122c0 65 72 6f 28 0a 20 20 20 20 73 69 7a 65 6f 66 28  ero(.    sizeof(
122c1 2a 70 50 61 67 65 72 29 20 2b 20 20 20 20 20 20  *pPager) +      
122c2 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 73 74       /* Pager st
122c3 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 70  ructure */.    p
122c4 63 61 63 68 65 53 69 7a 65 20 20 20 20 20 20 2b  cacheSize      +
122c5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43             /* PC
122c6 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  ache object */. 
122c7 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69     journalFileSi
122c8 7a 65 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f  ze +           /
122c9 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
122ca 6c 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 20  le structure */ 
122cb 0a 20 20 20 20 70 56 66 73 2d 3e 73 7a 4f 73 46  .    pVfs->szOsF
122cc 69 6c 65 20 20 2b 20 20 20 20 20 20 20 20 20 20  ile  +          
122cd 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20   /* The main db 
122ce 66 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72  file */.    jour
122cf 6e 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20  nalFileSize * 2 
122d0 2b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74  +       /* The t
122d1 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  wo journal files
122d2 20 2a 2f 20 0a 20 20 20 20 33 2a 6e 50 61 74 68   */ .    3*nPath
122d3 6e 61 6d 65 20 2b 20 34 30 20 20 20 20 20 20 20  name + 40       
122d4 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d       /* zFilenam
122d5 65 2c 20 7a 44 69 72 65 63 74 6f 72 79 2c 20 7a  e, zDirectory, z
122d6 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a  Journal */.  );.
122d7 20 20 69 66 28 20 21 70 50 61 67 65 72 20 29 7b    if( !pPager ){
122d8 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
122d9 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20  e(zPathname);.  
122da 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
122db 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61  NOMEM;.  }.  pPa
122dc 67 65 72 2d 3e 70 50 43 61 63 68 65 20 3d 20 28  ger->pPCache = (
122dd 50 43 61 63 68 65 20 2a 29 26 70 50 61 67 65 72  PCache *)&pPager
122de 5b 31 5d 3b 0a 20 20 70 50 74 72 20 3d 20 28 28  [1];.  pPtr = ((
122df 75 38 20 2a 29 26 70 50 61 67 65 72 5b 31 5d 29  u8 *)&pPager[1])
122e0 20 2b 20 70 63 61 63 68 65 53 69 7a 65 3b 0a 20   + pcacheSize;. 
122e1 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67   pPager->vfsFlag
122e2 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a 20 20  s = vfsFlags;.  
122e3 70 50 61 67 65 72 2d 3e 66 64 20 3d 20 28 73 71  pPager->fd = (sq
122e4 6c 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50 74  lite3_file*)&pPt
122e5 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  r[pVfs->szOsFile
122e6 2a 30 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73  *0];.  pPager->s
122e7 6a 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f 66  jfd = (sqlite3_f
122e8 69 6c 65 2a 29 26 70 50 74 72 5b 70 56 66 73 2d  ile*)&pPtr[pVfs-
122e9 3e 73 7a 4f 73 46 69 6c 65 5d 3b 0a 20 20 70 50  >szOsFile];.  pP
122ea 61 67 65 72 2d 3e 6a 66 64 20 3d 20 28 73 71 6c  ager->jfd = (sql
122eb 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50 74 72  ite3_file*)&pPtr
122ec 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 2b  [pVfs->szOsFile+
122ed 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 5d  journalFileSize]
122ee 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c  ;.  pPager->zFil
122ef 65 6e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26  ename = (char*)&
122f0 70 50 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46  pPtr[pVfs->szOsF
122f1 69 6c 65 2b 32 2a 6a 6f 75 72 6e 61 6c 46 69 6c  ile+2*journalFil
122f2 65 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 65 72  eSize];.  pPager
122f3 2d 3e 7a 44 69 72 65 63 74 6f 72 79 20 3d 20 26  ->zDirectory = &
122f4 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
122f5 65 5b 6e 50 61 74 68 6e 61 6d 65 2b 31 5d 3b 0a  e[nPathname+1];.
122f6 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e    pPager->zJourn
122f7 61 6c 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a 44  al = &pPager->zD
122f8 69 72 65 63 74 6f 72 79 5b 6e 50 61 74 68 6e 61  irectory[nPathna
122f9 6d 65 2b 31 5d 3b 0a 20 20 70 50 61 67 65 72 2d  me+1];.  pPager-
122fa 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20  >pVfs = pVfs;.  
122fb 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b  if( zPathname ){
122fc 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67  .    memcpy(pPag
122fd 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a  er->zFilename, z
122fe 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e  Pathname, nPathn
122ff 61 6d 65 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  ame+1);.    sqli
12300 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61  te3_free(zPathna
12301 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  me);.  }..  /* O
12302 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 69  pen the pager fi
12303 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a  le..  */.  if( z
12304 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c  Filename && zFil
12305 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 6d  ename[0] && !mem
12306 44 62 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 50  Db ){.    if( nP
12307 61 74 68 6e 61 6d 65 3e 28 70 56 66 73 2d 3e 6d  athname>(pVfs->m
12308 78 50 61 74 68 6e 61 6d 65 20 2d 20 28 69 6e 74  xPathname - (int
12309 29 73 69 7a 65 6f 66 28 22 2d 6a 6f 75 72 6e 61  )sizeof("-journa
1230a 6c 22 29 29 20 29 7b 0a 20 20 20 20 20 20 72 63  l")) ){.      rc
1230b 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50   = SQLITE_CANTOP
1230c 45 4e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  EN;.    }else{. 
1230d 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20       int fout = 
1230e 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  0;.      rc = sq
1230f 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73  lite3OsOpen(pVfs
12310 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e  , pPager->zFilen
12311 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c  ame, pPager->fd,
12312 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
12313 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
12314 2d 3e 76 66 73 46 6c 61 67 73 2c 20 26 66 6f 75  ->vfsFlags, &fou
12315 74 29 3b 0a 20 20 20 20 20 20 72 65 61 64 4f 6e  t);.      readOn
12316 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 54  ly = (fout&SQLIT
12317 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29  E_OPEN_READONLY)
12318 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ;..      /* If t
12319 68 65 20 66 69 6c 65 20 77 61 73 20 73 75 63 63  he file was succ
1231a 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20  essfully opened 
1231b 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 61  for read/write a
1231c 63 63 65 73 73 2c 0a 20 20 20 20 20 20 2a 2a 20  ccess,.      ** 
1231d 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c 74  choose a default
1231e 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63 61   page size in ca
1231f 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63 72  se we have to cr
12320 65 61 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a  eate the.      *
12321 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  * database file.
12322 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 61 67   The default pag
12323 65 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d 61  e size is the ma
12324 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 20 20  ximum of:.      
12325 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 2b  **.      **    +
12326 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
12327 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20 20  PAGE_SIZE,.     
12328 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61 6c   **    + The val
12329 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
1232a 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69  qlite3OsSectorSi
1232b 7a 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 20 20  ze().      **   
1232c 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20 70   + The largest p
1232d 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63 61  age size that ca
1232e 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f  n be written ato
1232f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 20 20 2a  mically..      *
12330 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  /.      if( rc==
12331 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 65  SQLITE_OK && !re
12332 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20  adOnly ){.      
12333 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28    setSectorSize(
12334 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20  pPager);.       
12335 20 69 66 28 20 73 7a 50 61 67 65 44 66 6c 74 3c   if( szPageDflt<
12336 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
12337 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ze ){.          
12338 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 70 50 61  szPageDflt = pPa
12339 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b  ger->sectorSize;
1233a 0a 20 20 20 20 20 20 20 20 7d 0a 23 69 66 64 65  .        }.#ifde
1233b 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
1233c 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20  ATOMIC_WRITE.   
1233d 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20       {.         
1233e 20 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74   int iDc = sqlit
1233f 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63  e3OsDeviceCharac
12340 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72  teristics(pPager
12341 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20  ->fd);.         
12342 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20   int ii;.       
12343 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45     assert(SQLITE
12344 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32  _IOCAP_ATOMIC512
12345 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20  ==(512>>8));.   
12346 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51         assert(SQ
12347 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
12348 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29  C64K==(65536>>8)
12349 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
1234a 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44  ert(SQLITE_MAX_D
1234b 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1234c 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20  <=65536);.      
1234d 20 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67      for(ii=szPag
1234e 65 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54  eDflt; ii<=SQLIT
1234f 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
12350 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32  GE_SIZE; ii=ii*2
12351 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
12352 66 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49  f( iDc&(SQLITE_I
12353 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e  OCAP_ATOMIC|(ii>
12354 3e 38 29 29 20 29 20 73 7a 50 61 67 65 44 66 6c  >8)) ) szPageDfl
12355 74 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20  t = ii;.        
12356 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 23 65    }.        }.#e
12357 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 28  ndif.        if(
12358 20 73 7a 50 61 67 65 44 66 6c 74 3e 53 51 4c 49   szPageDflt>SQLI
12359 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50  TE_MAX_DEFAULT_P
1235a 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20  AGE_SIZE ){.    
1235b 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74        szPageDflt
1235c 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45   = SQLITE_MAX_DE
1235d 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b  FAULT_PAGE_SIZE;
1235e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1235f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
12360 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65  {.    /* If a te
12361 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20  mporary file is 
12362 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73  requested, it is
12363 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65   not opened imme
12364 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20  diately..    ** 
12365 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20  In this case we 
12366 61 63 63 65 70 74 20 74 68 65 20 64 65 66 61 75  accept the defau
12367 6c 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64  lt page size and
12368 20 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a   delay actually.
12369 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74      ** opening t
1236a 68 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68  he file until th
1236b 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20  e first call to 
1236c 4f 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a  OsWrite()..    *
1236d 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72  *.    ** This br
1236e 61 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e  anch is also run
1236f 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72   for an in-memor
12370 79 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69  y database. An i
12371 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20  n-memory.    ** 
12372 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20  database is the 
12373 73 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66  same as a temp-f
12374 69 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 65  ile that is neve
12375 72 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f  r written out to
12376 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64  .    ** disk and
12377 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f   uses an in-memo
12378 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  ry rollback jour
12379 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20  nal..    */ .   
1237a 20 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20   tempFile = 1;. 
1237b 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65     pPager->state
1237c 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49   = PAGER_EXCLUSI
1237d 56 45 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  VE;.  }..  if( p
1237e 50 61 67 65 72 20 26 26 20 72 63 3d 3d 53 51 4c  Pager && rc==SQL
1237f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50  ITE_OK ){.    pP
12380 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20  ager->pTmpSpace 
12381 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c  = sqlite3PageMal
12382 6c 6f 63 28 73 7a 50 61 67 65 44 66 6c 74 29 3b  loc(szPageDflt);
12383 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e  .  }..  /* If an
12384 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 69   error occured i
12385 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20  n either of the 
12386 62 6c 6f 63 6b 73 20 61 62 6f 76 65 2e 0a 20 20  blocks above..  
12387 2a 2a 20 46 72 65 65 20 74 68 65 20 50 61 67 65  ** Free the Page
12388 72 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  r structure and 
12389 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a  close the file..
1238a 20 20 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 70    ** Since the p
1238b 61 67 65 72 20 69 73 20 6e 6f 74 20 61 6c 6c 6f  ager is not allo
1238c 63 61 74 65 64 20 74 68 65 72 65 20 69 73 20 6e  cated there is n
1238d 6f 20 6e 65 65 64 20 74 6f 20 73 65 74 20 0a 20  o need to set . 
1238e 20 2a 2a 20 61 6e 79 20 50 61 67 65 72 2e 65 72   ** any Pager.er
1238f 72 4d 61 73 6b 20 76 61 72 69 61 62 6c 65 73 2e  rMask variables.
12390 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61  .  */.  if( !pPa
12391 67 65 72 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e  ger || !pPager->
12392 70 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20  pTmpSpace ){.   
12393 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
12394 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20  pPager->fd);.   
12395 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
12396 61 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72  ager);.    retur
12397 6e 20 28 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  n ((rc==SQLITE_O
12398 4b 29 3f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3a  K)?SQLITE_NOMEM:
12399 72 63 29 3b 0a 20 20 7d 0a 20 20 6e 45 78 74 72  rc);.  }.  nExtr
1239a 61 20 3d 20 46 4f 52 43 45 5f 41 4c 49 47 4e 4d  a = FORCE_ALIGNM
1239b 45 4e 54 28 6e 45 78 74 72 61 29 3b 0a 20 20 73  ENT(nExtra);.  s
1239c 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e  qlite3PcacheOpen
1239d 28 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78  (szPageDflt, nEx
1239e 74 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20  tra, !memDb,.   
1239f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
123a0 20 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72   !memDb?pagerStr
123a1 65 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70  ess:0, (void *)p
123a2 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70  Pager, pPager->p
123a3 50 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45  PCache);..  PAGE
123a4 52 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 64  RTRACE(("OPEN %d
123a5 20 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44   %s\n", FILEHAND
123a6 4c 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29  LEID(pPager->fd)
123a7 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e  , pPager->zFilen
123a8 61 6d 65 29 29 3b 0a 20 20 49 4f 54 52 41 43 45  ame));.  IOTRACE
123a9 28 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22  (("OPEN %p %s\n"
123aa 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  , pPager, pPager
123ab 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20  ->zFilename)).. 
123ac 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 50 61 67 65   /* Fill in Page
123ad 72 2e 7a 44 69 72 65 63 74 6f 72 79 5b 5d 20 2a  r.zDirectory[] *
123ae 2f 0a 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65  /.  memcpy(pPage
123af 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 2c 20 70  r->zDirectory, p
123b0 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65  Pager->zFilename
123b1 2c 20 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a  , nPathname+1);.
123b2 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 53    for(i=sqlite3S
123b3 74 72 6c 65 6e 33 30 28 70 50 61 67 65 72 2d 3e  trlen30(pPager->
123b4 7a 44 69 72 65 63 74 6f 72 79 29 3b 20 0a 20 20  zDirectory); .  
123b5 20 20 20 20 69 3e 30 20 26 26 20 70 50 61 67 65      i>0 && pPage
123b6 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d  r->zDirectory[i-
123b7 31 5d 21 3d 27 2f 27 3b 20 69 2d 2d 29 7b 7d 0a  1]!='/'; i--){}.
123b8 20 20 69 66 28 20 69 3e 30 20 29 20 70 50 61 67    if( i>0 ) pPag
123b9 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69  er->zDirectory[i
123ba 2d 31 5d 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 46  -1] = 0;..  /* F
123bb 69 6c 6c 20 69 6e 20 50 61 67 65 72 2e 7a 4a 6f  ill in Pager.zJo
123bc 75 72 6e 61 6c 5b 5d 20 2a 2f 0a 20 20 69 66 28  urnal[] */.  if(
123bd 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20   zPathname ){.  
123be 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d    memcpy(pPager-
123bf 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65  >zJournal, pPage
123c0 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50  r->zFilename, nP
123c1 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65  athname);.    me
123c2 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a  mcpy(&pPager->zJ
123c3 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65  ournal[nPathname
123c4 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 39  ], "-journal", 9
123c5 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
123c6 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
123c7 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
123c8 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
123c9 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50  pen = 0; */.  pP
123ca 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c  ager->useJournal
123cb 20 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61   = (u8)useJourna
123cc 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 52  l;.  pPager->noR
123cd 65 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 65 61  eadlock = (noRea
123ce 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c  dlock && readOnl
123cf 79 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 70 50  y) ?1:0;.  /* pP
123d0 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d  ager->stmtOpen =
123d1 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
123d2 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20  er->stmtInUse = 
123d3 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65  0; */.  /* pPage
123d4 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a 2f 0a  r->nRef = 0; */.
123d5 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
123d6 56 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 6d 44  Valid = (u8)memD
123d7 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 61 67  b;.  pPager->pag
123d8 65 53 69 7a 65 20 3d 20 73 7a 50 61 67 65 44 66  eSize = szPageDf
123d9 6c 74 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  lt;.  /* pPager-
123da 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a  >stmtSize = 0; *
123db 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73  /.  /* pPager->s
123dc 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f  tmtJSize = 0; */
123dd 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50  .  /* pPager->nP
123de 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50  age = 0; */.  pP
123df 61 67 65 72 2d 3e 6d 78 50 61 67 65 20 3d 20 31  ager->mxPage = 1
123e0 30 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78  00;.  pPager->mx
123e1 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41  Pgno = SQLITE_MA
123e2 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20  X_PAGE_COUNT;.  
123e3 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  /* pPager->state
123e4 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b   = PAGER_UNLOCK;
123e5 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
123e6 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28  ager->state == (
123e7 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52  tempFile ? PAGER
123e8 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47  _EXCLUSIVE : PAG
123e9 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20  ER_UNLOCK) );.  
123ea 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d 61  /* pPager->errMa
123eb 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  sk = 0; */.  pPa
123ec 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20  ger->tempFile = 
123ed 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a 20 20  (u8)tempFile;.  
123ee 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c 65  assert( tempFile
123ef 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
123f0 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20 20  ODE_NORMAL .    
123f1 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69 6c        || tempFil
123f2 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
123f3 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29  MODE_EXCLUSIVE )
123f4 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
123f5 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
123f6 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20 20  CLUSIVE==1 );.  
123f7 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
123f8 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65 6d 70  eMode = (u8)temp
123f9 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65 72 2d  File; .  pPager-
123fa 3e 6d 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d  >memDb = (u8)mem
123fb 44 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65  Db;.  pPager->re
123fc 61 64 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61  adOnly = (u8)rea
123fd 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67  dOnly;.  /* pPag
123fe 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30  er->needSync = 0
123ff 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e  ; */.  pPager->n
12400 6f 53 79 6e 63 20 3d 20 28 70 50 61 67 65 72 2d  oSync = (pPager-
12401 3e 74 65 6d 70 46 69 6c 65 20 7c 7c 20 21 75 73  >tempFile || !us
12402 65 4a 6f 75 72 6e 61 6c 29 20 3f 31 3a 30 3b 0a  eJournal) ?1:0;.
12403 20 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79    pPager->fullSy
12404 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53  nc = pPager->noS
12405 79 6e 63 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67  ync ?0:1;.  pPag
12406 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d  er->sync_flags =
12407 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52   SQLITE_SYNC_NOR
12408 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72  MAL;.  /* pPager
12409 2d 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f  ->pFirst = 0; */
1240a 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46  .  /* pPager->pF
1240b 69 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20  irstSynced = 0; 
1240c 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  */.  /* pPager->
1240d 70 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20  pLast = 0; */.  
1240e 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d  pPager->nExtra =
1240f 20 6e 45 78 74 72 61 3b 0a 20 20 70 50 61 67 65   nExtra;.  pPage
12410 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  r->journalSizeLi
12411 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46  mit = SQLITE_DEF
12412 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a  AULT_JOURNAL_SIZ
12413 45 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73 65 72  E_LIMIT;.  asser
12414 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d  t(pPager->fd->pM
12415 65 74 68 6f 64 73 7c 7c 74 65 6d 70 46 69 6c 65  ethods||tempFile
12416 29 3b 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69  );.  setSectorSi
12417 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66  ze(pPager);.  if
12418 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 70  ( memDb ){.    p
12419 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
1241a 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e  de = PAGER_JOURN
1241b 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a 20  ALMODE_MEMORY;. 
1241c 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e   }.  /* pPager->
1241d 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 30  xBusyHandler = 0
1241e 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
1241f 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72  ->pBusyHandlerAr
12420 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 6d  g = 0; */.  /* m
12421 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61 48  emset(pPager->aH
12422 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70  ash, 0, sizeof(p
12423 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b 20  Pager->aHash)); 
12424 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20  */.  *ppPager = 
12425 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 6e  pPager;.  return
12426 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
12427 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 73  *.** Set the bus
12428 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69  y handler functi
12429 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
1242a 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1242b 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61  e3PagerSetBusyha
1242c 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 72 20 2a  ndler(.  Pager *
1242d 70 50 61 67 65 72 2c 20 0a 20 20 69 6e 74 20 28  pPager, .  int (
1242e 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76  *xBusyHandler)(v
1242f 6f 69 64 20 2a 29 2c 0a 20 20 76 6f 69 64 20 2a  oid *),.  void *
12430 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 0a  pBusyHandlerArg.
12431 29 7b 20 20 0a 20 20 70 50 61 67 65 72 2d 3e 78  ){  .  pPager->x
12432 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42  BusyHandler = xB
12433 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50  usyHandler;.  pP
12434 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c  ager->pBusyHandl
12435 65 72 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e  erArg = pBusyHan
12436 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a  dlerArg;.}../*.*
12437 2a 20 53 65 74 20 74 68 65 20 72 65 69 6e 69 74  * Set the reinit
12438 69 61 6c 69 7a 65 72 20 66 6f 72 20 74 68 69 73  ializer for this
12439 20 70 61 67 65 72 2e 20 20 49 66 20 6e 6f 74 20   pager.  If not 
1243a 4e 55 4c 4c 2c 20 74 68 65 20 72 65 69 6e 69 74  NULL, the reinit
1243b 69 61 6c 69 7a 65 72 0a 2a 2a 20 69 73 20 63 61  ializer.** is ca
1243c 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 63 6f  lled when the co
1243d 6e 74 65 6e 74 20 6f 66 20 61 20 70 61 67 65 20  ntent of a page 
1243e 69 6e 20 63 61 63 68 65 20 69 73 20 72 65 73 74  in cache is rest
1243f 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67  ored to its orig
12440 69 6e 61 6c 0a 2a 2a 20 76 61 6c 75 65 20 61 73  inal.** value as
12441 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 20 72   a result of a r
12442 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 63 61  ollback.  The ca
12443 6c 6c 62 61 63 6b 20 67 69 76 65 73 20 68 69 67  llback gives hig
12444 68 65 72 2d 6c 65 76 65 6c 20 63 6f 64 65 0a 2a  her-level code.*
12445 2a 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79  * an opportunity
12446 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20   to restore the 
12447 45 58 54 52 41 20 73 65 63 74 69 6f 6e 20 74 6f  EXTRA section to
12448 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65 20   agree with the 
12449 72 65 73 74 6f 72 65 64 0a 2a 2a 20 70 61 67 65  restored.** page
1244a 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45   data..*/.SQLITE
1244b 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1244c 6c 69 74 65 33 50 61 67 65 72 53 65 74 52 65 69  lite3PagerSetRei
1244d 6e 69 74 65 72 28 50 61 67 65 72 20 2a 70 50 61  niter(Pager *pPa
1244e 67 65 72 2c 20 76 6f 69 64 20 28 2a 78 52 65 69  ger, void (*xRei
1244f 6e 69 74 29 28 44 62 50 61 67 65 2a 29 29 7b 0a  nit)(DbPage*)){.
12450 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69    pPager->xReini
12451 74 65 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a 7d  ter = xReinit;.}
12452 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
12453 70 61 67 65 20 73 69 7a 65 20 74 6f 20 2a 70 50  page size to *pP
12454 61 67 65 53 69 7a 65 2e 20 49 66 20 74 68 65 20  ageSize. If the 
12455 73 75 67 67 65 73 74 20 6e 65 77 20 70 61 67 65  suggest new page
12456 20 73 69 7a 65 20 69 73 0a 2a 2a 20 69 6e 61 70   size is.** inap
12457 70 72 6f 70 72 69 61 74 65 2c 20 74 68 65 6e 20  propriate, then 
12458 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70  an alternative p
12459 61 67 65 20 73 69 7a 65 20 69 73 20 73 65 74 20  age size is set 
1245a 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65  to that.** value
1245b 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
1245c 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
1245d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1245e 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
1245f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
12460 75 31 36 20 2a 70 50 61 67 65 53 69 7a 65 29 7b  u16 *pPageSize){
12461 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67  .  int rc = pPag
12462 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 69  er->errCode;.  i
12463 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12464 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 65   ){.    u16 page
12465 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 69 7a  Size = *pPageSiz
12466 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e;.    assert( p
12467 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70  ageSize==0 || (p
12468 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20  ageSize>=512 && 
12469 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45  pageSize<=SQLITE
1246a 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 20  _MAX_PAGE_SIZE) 
1246b 29 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 53  );.    if( pageS
1246c 69 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21  ize && pageSize!
1246d 3d 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  =pPager->pageSiz
1246e 65 20 0a 20 20 20 20 20 26 26 20 28 70 50 61 67  e .     && (pPag
1246f 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20  er->memDb==0 || 
12470 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d  pPager->dbSize==
12471 30 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74  0).     && sqlit
12472 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74  e3PcacheRefCount
12473 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
12474 29 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20  )==0 .    ){.   
12475 20 20 20 63 68 61 72 20 2a 70 4e 65 77 20 3d 20     char *pNew = 
12476 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50  (char *)sqlite3P
12477 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69  ageMalloc(pageSi
12478 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ze);.      if( !
12479 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
1247a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1247b 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  M;.      }else{.
1247c 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65          pager_re
1247d 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20  set(pPager);.   
1247e 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67       pPager->pag
1247f 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65  eSize = pageSize
12480 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  ;.        if( !p
12481 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 20 73  Pager->memDb ) s
12482 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61  etSectorSize(pPa
12483 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ger);.        sq
12484 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 50  lite3PageFree(pP
12485 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29  ager->pTmpSpace)
12486 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  ;.        pPager
12487 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 70 4e  ->pTmpSpace = pN
12488 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ew;.        sqli
12489 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65  te3PcacheSetPage
1248a 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43  Size(pPager->pPC
1248b 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65 29 3b  ache, pageSize);
1248c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1248d 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20 3d 20     *pPageSize = 
1248e 28 75 31 36 29 70 50 61 67 65 72 2d 3e 70 61 67  (u16)pPager->pag
1248f 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74  eSize;.  }.  ret
12490 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
12491 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
12492 72 20 74 6f 20 74 68 65 20 22 74 65 6d 70 6f 72  r to the "tempor
12493 61 72 79 20 70 61 67 65 22 20 62 75 66 66 65 72  ary page" buffer
12494 20 68 65 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79   held internally
12495 0a 2a 2a 20 62 79 20 74 68 65 20 70 61 67 65 72  .** by the pager
12496 2e 20 20 54 68 69 73 20 69 73 20 61 20 62 75 66  .  This is a buf
12497 66 65 72 20 74 68 61 74 20 69 73 20 62 69 67 20  fer that is big 
12498 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74  enough to hold t
12499 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e  he.** entire con
1249a 74 65 6e 74 20 6f 66 20 61 20 64 61 74 61 62 61  tent of a databa
1249b 73 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 62  se page.  This b
1249c 75 66 66 65 72 20 69 73 20 75 73 65 64 20 69 6e  uffer is used in
1249d 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69  ternally.** duri
1249e 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20  ng rollback and 
1249f 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74  will be overwrit
124a0 74 65 6e 20 77 68 65 6e 65 76 65 72 20 61 20 72  ten whenever a r
124a1 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72  ollback.** occur
124a2 73 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d 6f  s.  But other mo
124a3 64 75 6c 65 73 20 61 72 65 20 66 72 65 65 20 74  dules are free t
124a4 6f 20 75 73 65 20 69 74 20 74 6f 6f 2c 20 61 73  o use it too, as
124a5 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72   long as.** no r
124a6 6f 6c 6c 62 61 63 6b 73 20 61 72 65 20 68 61 70  ollbacks are hap
124a7 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  pening..*/.SQLIT
124a8 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
124a9 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70  sqlite3PagerTemp
124aa 53 70 61 63 65 28 50 61 67 65 72 20 2a 70 50 61  Space(Pager *pPa
124ab 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ger){.  return p
124ac 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
124ad 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  ;.}../*.** Attem
124ae 70 74 20 74 6f 20 73 65 74 20 74 68 65 20 6d 61  pt to set the ma
124af 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 70  ximum database p
124b0 61 67 65 20 63 6f 75 6e 74 20 69 66 20 6d 78 50  age count if mxP
124b1 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e  age is positive.
124b2 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61   .** Make no cha
124b3 6e 67 65 73 20 69 66 20 6d 78 50 61 67 65 20 69  nges if mxPage i
124b4 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69  s zero or negati
124b5 76 65 2e 20 20 41 6e 64 20 6e 65 76 65 72 20 72  ve.  And never r
124b6 65 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78  educe the.** max
124b7 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20  imum page count 
124b8 62 65 6c 6f 77 20 74 68 65 20 63 75 72 72 65 6e  below the curren
124b9 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  t size of the da
124ba 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65  tabase..**.** Re
124bb 67 61 72 64 6c 65 73 73 20 6f 66 20 6d 78 50 61  gardless of mxPa
124bc 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 63  ge, return the c
124bd 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70  urrent maximum p
124be 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51  age count..*/.SQ
124bf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
124c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 78   sqlite3PagerMax
124c1 50 61 67 65 43 6f 75 6e 74 28 50 61 67 65 72 20  PageCount(Pager 
124c2 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50  *pPager, int mxP
124c3 61 67 65 29 7b 0a 20 20 69 66 28 20 6d 78 50 61  age){.  if( mxPa
124c4 67 65 3e 30 20 29 7b 0a 20 20 20 20 70 50 61 67  ge>0 ){.    pPag
124c5 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 50  er->mxPgno = mxP
124c6 61 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  age;.  }.  sqlit
124c7 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
124c8 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 72  (pPager, 0);.  r
124c9 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6d 78  eturn pPager->mx
124ca 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  Pgno;.}../*.** T
124cb 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 74  he following set
124cc 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 65   of routines are
124cd 20 75 73 65 64 20 74 6f 20 64 69 73 61 62 6c 65   used to disable
124ce 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 0a 2a   the simulated.*
124cf 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 63 68  * I/O error mech
124d0 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 72 6f  anism.  These ro
124d1 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20  utines are used 
124d2 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c 61 74  to avoid simulat
124d3 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 6e 20  ed.** errors in 
124d4 70 6c 61 63 65 73 20 77 68 65 72 65 20 77 65 20  places where we 
124d5 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75  do not care abou
124d6 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  t errors..**.** 
124d7 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 45 5f  Unless -DSQLITE_
124d8 54 45 53 54 3d 31 20 69 73 20 75 73 65 64 2c 20  TEST=1 is used, 
124d9 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  these routines a
124da 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a  re all no-ops.**
124db 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 6e 6f   and generate no
124dc 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66   code..*/.#ifdef
124dd 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
124de 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 69  ITE_API extern i
124df 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
124e0 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 53 51 4c  ror_pending;.SQL
124e1 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 69  ITE_API extern i
124e2 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
124e3 72 6f 72 5f 68 69 74 3b 0a 73 74 61 74 69 63 20  ror_hit;.static 
124e4 69 6e 74 20 73 61 76 65 64 5f 63 6e 74 3b 0a 76  int saved_cnt;.v
124e5 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d 75  oid disable_simu
124e6 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
124e7 76 6f 69 64 29 7b 0a 20 20 73 61 76 65 64 5f 63  void){.  saved_c
124e8 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 69 6f 5f  nt = sqlite3_io_
124e9 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20  error_pending;. 
124ea 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
124eb 72 5f 70 65 6e 64 69 6e 67 20 3d 20 2d 31 3b 0a  r_pending = -1;.
124ec 7d 0a 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69  }.void enable_si
124ed 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
124ee 73 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74  s(void){.  sqlit
124ef 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
124f0 69 6e 67 20 3d 20 73 61 76 65 64 5f 63 6e 74 3b  ing = saved_cnt;
124f1 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  .}.#else.# defin
124f2 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61  e disable_simula
124f3 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a  ted_io_errors().
124f4 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f  # define enable_
124f5 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
124f6 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ors().#endif../*
124f7 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 66 69 72  .** Read the fir
124f8 73 74 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20  st N bytes from 
124f9 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
124fa 20 74 68 65 20 66 69 6c 65 20 69 6e 74 6f 20 6d   the file into m
124fb 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 20 70 44  emory.** that pD
124fc 65 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 0a  est points to. .
124fd 2a 2a 0a 2a 2a 20 4e 6f 20 65 72 72 6f 72 20 63  **.** No error c
124fe 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 2e  hecking is done.
124ff 20 54 68 65 20 72 61 74 69 6f 6e 61 6c 20 66 6f   The rational fo
12500 72 20 74 68 69 73 20 69 73 20 74 68 61 74 20 74  r this is that t
12501 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a  his function .**
12502 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 65   may be called e
12503 76 65 6e 20 69 66 20 74 68 65 20 66 69 6c 65 20  ven if the file 
12504 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 6f  does not exist o
12505 72 20 63 6f 6e 74 61 69 6e 20 61 20 68 65 61 64  r contain a head
12506 65 72 2e 20 49 6e 20 0a 2a 2a 20 74 68 65 73 65  er. In .** these
12507 20 63 61 73 65 73 20 73 71 6c 69 74 65 33 4f 73   cases sqlite3Os
12508 52 65 61 64 28 29 20 77 69 6c 6c 20 72 65 74 75  Read() will retu
12509 72 6e 20 61 6e 20 65 72 72 6f 72 2c 20 74 6f 20  rn an error, to 
1250a 77 68 69 63 68 20 74 68 65 20 63 6f 72 72 65 63  which the correc
1250b 74 20 0a 2a 2a 20 72 65 73 70 6f 6e 73 65 20 69  t .** response i
1250c 73 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 6d 65  s to zero the me
1250d 6d 6f 72 79 20 61 74 20 70 44 65 73 74 20 61 6e  mory at pDest an
1250e 64 20 63 6f 6e 74 69 6e 75 65 2e 20 20 41 20 72  d continue.  A r
1250f 65 61 6c 20 49 4f 20 65 72 72 6f 72 20 0a 2a 2a  eal IO error .**
12510 20 77 69 6c 6c 20 70 72 65 73 75 6d 61 62 6c 79   will presumably
12511 20 72 65 63 75 72 20 61 6e 64 20 62 65 20 70 69   recur and be pi
12512 63 6b 65 64 20 75 70 20 6c 61 74 65 72 20 28 54  cked up later (T
12513 6f 64 6f 3a 20 54 68 69 6e 6b 20 61 62 6f 75 74  odo: Think about
12514 20 74 68 69 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54   this)..*/.SQLIT
12515 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
12516 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69  lite3PagerReadFi
12517 6c 65 68 65 61 64 65 72 28 50 61 67 65 72 20 2a  leheader(Pager *
12518 70 50 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20 75  pPager, int N, u
12519 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44  nsigned char *pD
1251a 65 73 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  est){.  int rc =
1251b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65   SQLITE_OK;.  me
1251c 6d 73 65 74 28 70 44 65 73 74 2c 20 30 2c 20 4e  mset(pDest, 0, N
1251d 29 3b 0a 20 20 61 73 73 65 72 74 28 70 50 61 67  );.  assert(pPag
1251e 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73  er->fd->pMethods
1251f 7c 7c 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  ||pPager->tempFi
12520 6c 65 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  le);.  if( pPage
12521 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20  r->fd->pMethods 
12522 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28  ){.    IOTRACE((
12523 22 44 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e  "DBHDR %p 0 %d\n
12524 22 2c 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20  ", pPager, N)). 
12525 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
12526 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64  sRead(pPager->fd
12527 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a  , pDest, N, 0);.
12528 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
12529 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52  TE_IOERR_SHORT_R
1252a 45 41 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20  EAD ){.      rc 
1252b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
1252c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1252d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
1252e 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  urn the total nu
1252f 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
12530 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61   the disk file a
12531 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a  ssociated with.*
12532 2a 20 70 50 61 67 65 72 2e 20 0a 2a 2a 0a 2a 2a  * pPager. .**.**
12533 20 49 66 20 74 68 65 20 50 45 4e 44 49 4e 47 5f   If the PENDING_
12534 42 59 54 45 20 6c 69 65 73 20 6f 6e 20 74 68 65  BYTE lies on the
12535 20 70 61 67 65 20 64 69 72 65 63 74 6c 79 20 61   page directly a
12536 66 74 65 72 20 74 68 65 20 65 6e 64 20 6f 66 20  fter the end of 
12537 74 68 65 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65  the.** file, the
12538 6e 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73 20  n consider this 
12539 70 61 67 65 20 70 61 72 74 20 6f 66 20 74 68 65  page part of the
1253a 20 66 69 6c 65 20 74 6f 6f 2e 20 46 6f 72 20 65   file too. For e
1253b 78 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 50 45  xample, if.** PE
1253c 4e 44 49 4e 47 5f 42 59 54 45 20 69 73 20 62 79  NDING_BYTE is by
1253d 74 65 20 34 30 39 36 20 28 74 68 65 20 66 69 72  te 4096 (the fir
1253e 73 74 20 62 79 74 65 20 6f 66 20 70 61 67 65 20  st byte of page 
1253f 35 29 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20  5) and the size 
12540 6f 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69  of the.** file i
12541 73 20 34 30 39 36 20 62 79 74 65 73 2c 20 35 20  s 4096 bytes, 5 
12542 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74  is returned inst
12543 65 61 64 20 6f 66 20 34 2e 0a 2a 2f 0a 53 51 4c  ead of 4..*/.SQL
12544 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
12545 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
12546 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61  count(Pager *pPa
12547 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65  ger, int *pnPage
12548 29 7b 0a 20 20 69 36 34 20 6e 20 3d 20 30 3b 0a  ){.  i64 n = 0;.
12549 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
1254a 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b  rt( pPager!=0 );
1254b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65  .  if( pPager->e
1254c 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 63  rrCode ){.    rc
1254d 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f   = pPager->errCo
1254e 64 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  de;.    return r
1254f 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  c;.  }.  if( pPa
12550 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
12551 20 29 7b 0a 20 20 20 20 6e 20 3d 20 70 50 61 67   ){.    n = pPag
12552 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 20  er->dbSize;.  } 
12553 65 6c 73 65 20 7b 0a 20 20 20 20 61 73 73 65 72  else {.    asser
12554 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d  t(pPager->fd->pM
12555 65 74 68 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e  ethods||pPager->
12556 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 20 20 69  tempFile);.    i
12557 66 28 20 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e  f( (pPager->fd->
12558 70 4d 65 74 68 6f 64 73 29 0a 20 20 20 20 20 26  pMethods).     &
12559 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  & (rc = sqlite3O
1255a 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72  sFileSize(pPager
1255b 2d 3e 66 64 2c 20 26 6e 29 29 21 3d 53 51 4c 49  ->fd, &n))!=SQLI
1255c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
1255d 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
1255e 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 72 65  r, rc);.      re
1255f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
12560 20 20 20 69 66 28 20 6e 3e 30 20 26 26 20 6e 3c     if( n>0 && n<
12561 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
12562 20 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 31 3b   ){.      n = 1;
12563 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
12564 20 20 6e 20 2f 3d 20 70 50 61 67 65 72 2d 3e 70    n /= pPager->p
12565 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 20  ageSize;.    }. 
12566 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73     if( pPager->s
12567 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate!=PAGER_UNLO
12568 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67  CK ){.      pPag
12569 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 28 50 67  er->dbSize = (Pg
1256a 6e 6f 29 6e 3b 0a 20 20 20 20 20 20 70 50 61 67  no)n;.      pPag
1256b 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d  er->dbFileSize =
1256c 20 28 50 67 6e 6f 29 6e 3b 0a 20 20 20 20 20 20   (Pgno)n;.      
1256d 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
1256e 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  lid = 1;.    }. 
1256f 20 7d 0a 20 20 69 66 28 20 6e 3d 3d 28 50 45 4e   }.  if( n==(PEN
12570 44 49 4e 47 5f 42 59 54 45 2f 70 50 61 67 65 72  DING_BYTE/pPager
12571 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 7b 0a 20  ->pageSize) ){. 
12572 20 20 20 6e 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66     n++;.  }.  if
12573 28 20 6e 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67  ( n>pPager->mxPg
12574 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  no ){.    pPager
12575 2d 3e 6d 78 50 67 6e 6f 20 3d 20 28 50 67 6e 6f  ->mxPgno = (Pgno
12576 29 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 6e  )n;.  }.  if( pn
12577 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 6e 50  Page ){.    *pnP
12578 61 67 65 20 3d 20 28 69 6e 74 29 6e 3b 0a 20 20  age = (int)n;.  
12579 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
1257a 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  E_OK;.}../*.** F
1257b 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
1257c 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  on.*/.static int
1257d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67   syncJournal(Pag
1257e 65 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 72 79  er*);../*.** Try
1257f 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63   to obtain a loc
12580 6b 20 6f 6e 20 61 20 66 69 6c 65 2e 20 20 49 6e  k on a file.  In
12581 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63 61  voke the busy ca
12582 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 20 6c 6f  llback if the lo
12583 63 6b 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74  ck.** is current
12584 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  ly not available
12585 2e 20 20 52 65 70 65 61 74 20 75 6e 74 69 6c 20  .  Repeat until 
12586 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
12587 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 66 61 6c  k returns.** fal
12588 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20  se or until the 
12589 6c 6f 63 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a  lock succeeds..*
1258a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
1258b 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
1258c 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f   and an error co
1258d 64 65 20 69 66 20 77 65 20 63 61 6e 6e 6f 74 20  de if we cannot 
1258e 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f  obtain.** the lo
1258f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ck..*/.static in
12590 74 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f  t pager_wait_on_
12591 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67  lock(Pager *pPag
12592 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  er, int locktype
12593 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
12594 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 76  /* The OS lock v
12595 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 74 68  alues must be th
12596 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 50 61  e same as the Pa
12597 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20  ger lock values 
12598 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  */.  assert( PAG
12599 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 45  ER_SHARED==SHARE
1259a 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  D_LOCK );.  asse
1259b 72 74 28 20 50 41 47 45 52 5f 52 45 53 45 52 56  rt( PAGER_RESERV
1259c 45 44 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  ED==RESERVED_LOC
1259d 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50  K );.  assert( P
1259e 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3d 3d  AGER_EXCLUSIVE==
1259f 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
125a0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66  ;..  /* If the f
125a1 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ile is currently
125a2 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 74   unlocked then t
125a3 68 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20  he size must be 
125a4 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61 73 73  unknown */.  ass
125a5 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
125a6 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44  te>=PAGER_SHARED
125a7 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69   || pPager->dbSi
125a8 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 0a 20  zeValid==0 );.. 
125a9 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
125aa 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  te>=locktype ){.
125ab 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
125ac 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
125ad 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 3d   do {.      rc =
125ae 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70   sqlite3OsLock(p
125af 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74  Pager->fd, lockt
125b0 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65  ype);.    }while
125b1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc==SQLITE_BUS
125b2 59 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42 75  Y && pPager->xBu
125b3 73 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65 72  syHandler(pPager
125b4 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72  ->pBusyHandlerAr
125b5 67 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  g) );.    if( rc
125b6 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
125b7 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61       pPager->sta
125b8 74 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70  te = (u8)locktyp
125b9 65 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45  e;.      IOTRACE
125ba 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22  (("LOCK %p %d\n"
125bb 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79  , pPager, lockty
125bc 70 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  pe)).    }.  }. 
125bd 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
125be 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
125bf 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a  IT_AUTOVACUUM./*
125c0 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65  .** Truncate the
125c1 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
125c2 61 73 65 20 66 69 6c 65 20 69 6d 61 67 65 20 74  ase file image t
125c3 6f 20 6e 50 61 67 65 20 70 61 67 65 73 2e 20 54  o nPage pages. T
125c4 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  his .** function
125c5 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c   does not actual
125c6 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61  ly modify the da
125c7 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 64  tabase file on d
125c8 69 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75 73 74  isk. It .** just
125c9 20 73 65 74 73 20 74 68 65 20 69 6e 74 65 72 6e   sets the intern
125ca 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  al state of the 
125cb 70 61 67 65 72 20 6f 62 6a 65 63 74 20 73 6f 20  pager object so 
125cc 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 74 72 75  that the .** tru
125cd 6e 63 61 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  ncation will be 
125ce 64 6f 6e 65 20 77 68 65 6e 20 74 68 65 20 63 75  done when the cu
125cf 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
125d0 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a  n is committed..
125d1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
125d2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
125d3 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65  gerTruncateImage
125d4 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
125d5 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 61  Pgno nPage){.  a
125d6 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64  ssert( pPager->d
125d7 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20  bSizeValid );.  
125d8 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
125d9 64 62 53 69 7a 65 3e 3d 6e 50 61 67 65 20 29 3b  dbSize>=nPage );
125da 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  .  pPager->dbSiz
125db 65 20 3d 20 6e 50 61 67 65 3b 0a 7d 0a 0a 2f 2a  e = nPage;.}../*
125dc 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63  .** Return the c
125dd 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74  urrent size of t
125de 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
125df 20 69 6d 61 67 65 20 69 6e 20 70 61 67 65 73 2e   image in pages.
125e0 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   This.** functio
125e1 6e 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 73  n differs from s
125e2 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
125e3 6f 75 6e 74 28 29 20 69 6e 20 74 77 6f 20 77 61  ount() in two wa
125e4 79 73 3a 0a 2a 2a 0a 2a 2a 20 20 61 29 20 49 74  ys:.**.**  a) It
125e5 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c   may only be cal
125e6 6c 65 64 20 77 68 65 6e 20 61 74 20 6c 65 61 73  led when at leas
125e7 74 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20  t one reference 
125e8 74 6f 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a  to a database.**
125e9 20 20 20 20 20 70 61 67 65 20 69 73 20 68 65 6c       page is hel
125ea 64 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65  d. This guarante
125eb 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61  es that the data
125ec 62 61 73 65 20 73 69 7a 65 20 69 73 20 61 6c 72  base size is alr
125ed 65 61 64 79 0a 2a 2a 20 20 20 20 20 6b 6e 6f 77  eady.**     know
125ee 6e 20 61 6e 64 20 61 20 63 61 6c 6c 20 74 6f 20  n and a call to 
125ef 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
125f0 65 28 29 20 69 73 20 6e 6f 74 20 72 65 71 75 69  e() is not requi
125f1 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 62 29 20 54  red..**.**  b) T
125f2 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
125f3 69 73 20 6e 6f 74 20 61 64 6a 75 73 74 65 64 20  is not adjusted 
125f4 66 6f 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  for the locking 
125f5 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  page..*/.SQLITE_
125f6 50 52 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c  PRIVATE Pgno sql
125f7 69 74 65 33 50 61 67 65 72 49 6d 61 67 65 53 69  ite3PagerImageSi
125f8 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ze(Pager *pPager
125f9 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ){.  assert( pPa
125fa 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
125fb 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61   );.  return pPa
125fc 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 7d 0a 23  ger->dbSize;.}.#
125fd 65 6e 64 69 66 20 20 2f 2a 20 69 66 6e 64 65 66  endif  /* ifndef
125fe 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
125ff 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a 0a 2a  OVACUUM */../*.*
12600 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70  * Shutdown the p
12601 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 65 65  age cache.  Free
12602 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20   all memory and 
12603 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e  close all files.
12604 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e  .**.** If a tran
12605 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70  saction was in p
12606 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68 69  rogress when thi
12607 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
12608 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61  led, that.** tra
12609 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
1260a 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75  ed back.  All ou
1260b 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20  tstanding pages 
1260c 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a  are invalidated.
1260d 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d  ** and their mem
1260e 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 20 41  ory is freed.  A
1260f 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ny attempt to us
12610 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 69 61  e a page associa
12611 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73  ted.** with this
12612 20 70 61 67 65 20 63 61 63 68 65 20 61 66 74 65   page cache afte
12613 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  r this function 
12614 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b  returns will lik
12615 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e  ely.** result in
12616 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a   a coredump..**.
12617 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
12618 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73   always succeeds
12619 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  . If a transacti
1261a 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e 20  on is active an 
1261b 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61  attempt.** is ma
1261c 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61  de to roll it ba
1261d 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ck. If an error 
1261e 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68  occurs during th
1261f 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61  e rollback .** a
12620 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79   hot journal may
12621 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20   be left in the 
12622 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e  filesystem but n
12623 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  o error is retur
12624 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61  ned.** to the ca
12625 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ller..*/.SQLITE_
12626 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
12627 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61  te3PagerClose(Pa
12628 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20  ger *pPager){.. 
12629 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74   disable_simulat
1262a 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a  ed_io_errors();.
1262b 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
1262c 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
1262d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
1262e 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65  = 0;.  pPager->e
1262f 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 30  xclusiveMode = 0
12630 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65 74 28  ;.  pager_reset(
12631 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21  pPager);.  if( !
12632 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 2f 2a 20  MEMDB ){.    /* 
12633 53 65 74 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61  Set Pager.journa
12634 6c 48 64 72 20 74 6f 20 2d 31 20 66 6f 72 20 74  lHdr to -1 for t
12635 68 65 20 62 65 6e 65 66 69 74 20 6f 66 20 74 68  he benefit of th
12636 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  e pager_playback
12637 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20  () .    ** call 
12638 77 68 69 63 68 20 6d 61 79 20 62 65 20 6d 61 64  which may be mad
12639 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61  e from within pa
1263a 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c  gerUnlockAndRoll
1263b 62 61 63 6b 28 29 2e 20 49 66 20 69 74 0a 20 20  back(). If it.  
1263c 20 20 2a 2a 20 69 73 20 6e 6f 74 20 2d 31 2c 20    ** is not -1, 
1263d 74 68 65 6e 20 74 68 65 20 75 6e 73 79 6e 63 65  then the unsynce
1263e 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 61 6e 20  d portion of an 
1263f 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  open journal fil
12640 65 20 6d 61 79 0a 20 20 20 20 2a 2a 20 62 65 20  e may.    ** be 
12641 70 6c 61 79 65 64 20 62 61 63 6b 20 69 6e 74 6f  played back into
12642 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49   the database. I
12643 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  f a power failur
12644 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 0a 20  e occurs while. 
12645 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61     ** this is ha
12646 70 70 65 6e 69 6e 67 2c 20 74 68 65 20 64 61 74  ppening, the dat
12647 61 62 61 73 65 20 6d 61 79 20 62 65 63 6f 6d 65  abase may become
12648 20 63 6f 72 72 75 70 74 2e 0a 20 20 20 20 2a 2f   corrupt..    */
12649 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
1264a 72 6e 61 6c 48 64 72 20 3d 20 2d 31 3b 0a 20 20  rnalHdr = -1;.  
1264b 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64    pagerUnlockAnd
1264c 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29  Rollback(pPager)
1264d 3b 0a 20 20 7d 0a 20 20 65 6e 61 62 6c 65 5f 73  ;.  }.  enable_s
1264e 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
1264f 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  rs();.  sqlite3E
12650 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
12651 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28  ;.  PAGERTRACE((
12652 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 41  "CLOSE %d\n", PA
12653 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b  GERID(pPager)));
12654 0a 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 4f  .  IOTRACE(("CLO
12655 53 45 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72  SE %p\n", pPager
12656 29 29 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  )).  if( pPager-
12657 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a  >journalOpen ){.
12658 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
12659 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  se(pPager->jfd);
1265a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 69  .  }.  sqlite3Bi
1265b 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67  tvecDestroy(pPag
1265c 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b  er->pInJournal);
1265d 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  .  sqlite3Bitvec
1265e 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e  Destroy(pPager->
1265f 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 29  pAlwaysRollback)
12660 3b 0a 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61  ;.  releaseAllSa
12661 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 29 3b  vepoint(pPager);
12662 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73  .  sqlite3OsClos
12663 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20  e(pPager->fd);. 
12664 20 2f 2a 20 54 65 6d 70 20 66 69 6c 65 73 20 61   /* Temp files a
12665 72 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  re automatically
12666 20 64 65 6c 65 74 65 64 20 62 79 20 74 68 65 20   deleted by the 
12667 4f 53 0a 20 20 2a 2a 20 69 66 28 20 70 50 61 67  OS.  ** if( pPag
12668 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a  er->tempFile ){.
12669 20 20 2a 2a 20 20 20 73 71 6c 69 74 65 33 4f 73    **   sqlite3Os
1266a 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 7a  Delete(pPager->z
1266b 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 2a 2a 20  Filename);.  ** 
1266c 7d 0a 20 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65  }.  */..  sqlite
1266d 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72  3PageFree(pPager
1266e 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20  ->pTmpSpace);.  
1266f 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f  sqlite3PcacheClo
12670 73 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  se(pPager->pPCac
12671 68 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  he);.  sqlite3_f
12672 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72  ree(pPager);.  r
12673 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
12674 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .}..#if !defined
12675 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  (NDEBUG) || defi
12676 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
12677 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
12678 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  e page number fo
12679 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65  r the given page
1267a 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45   data..*/.SQLITE
1267b 5f 50 52 49 56 41 54 45 20 50 67 6e 6f 20 73 71  _PRIVATE Pgno sq
1267c 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75  lite3PagerPagenu
1267d 6d 62 65 72 28 44 62 50 61 67 65 20 2a 70 29 7b  mber(DbPage *p){
1267e 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 67 6e  .  return p->pgn
1267f 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  o;.}.#endif../*.
12680 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  ** Increment the
12681 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
12682 20 66 6f 72 20 61 20 70 61 67 65 2e 20 20 54 68   for a page.  Th
12683 65 20 69 6e 70 75 74 20 70 6f 69 6e 74 65 72 20  e input pointer 
12684 69 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63  is.** a referenc
12685 65 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 61  e to the page da
12686 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ta..*/.SQLITE_PR
12687 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
12688 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 65  3PagerRef(DbPage
12689 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74 65   *pPg){.  sqlite
1268a 33 50 63 61 63 68 65 52 65 66 28 70 50 67 29 3b  3PcacheRef(pPg);
1268b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1268c 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79  _OK;.}../*.** Sy
1268d 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  nc the journal. 
1268e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
1268f 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74   make sure all t
12690 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68 61  he pages that ha
12691 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74 74  ve.** been writt
12692 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  en to the journa
12693 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79 20  l have actually 
12694 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72 66  reached the surf
12695 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 69  ace of the.** di
12696 73 6b 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 73  sk.  It is not s
12697 61 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  afe to modify th
12698 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62  e original datab
12699 61 73 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 61  ase file until a
1269a 66 74 65 72 0a 2a 2a 20 74 68 65 20 6a 6f 75 72  fter.** the jour
1269b 6e 61 6c 20 68 61 73 20 62 65 65 6e 20 73 79 6e  nal has been syn
1269c 63 65 64 2e 20 20 49 66 20 74 68 65 20 6f 72 69  ced.  If the ori
1269d 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 69  ginal database i
1269e 73 20 6d 6f 64 69 66 69 65 64 20 62 65 66 6f 72  s modified befor
1269f 65 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e.** the journal
126a0 20 69 73 20 73 79 6e 63 65 64 20 61 6e 64 20 61   is synced and a
126a1 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f   power failure o
126a2 63 63 75 72 73 2c 20 74 68 65 20 75 6e 73 79 6e  ccurs, the unsyn
126a3 63 65 64 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 64  ced journal.** d
126a4 61 74 61 20 77 6f 75 6c 64 20 62 65 20 6c 6f 73  ata would be los
126a5 74 20 61 6e 64 20 77 65 20 77 6f 75 6c 64 20 62  t and we would b
126a6 65 20 75 6e 61 62 6c 65 20 74 6f 20 63 6f 6d 70  e unable to comp
126a7 6c 65 74 65 6c 79 20 72 6f 6c 6c 62 61 63 6b 20  letely rollback 
126a8 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
126a9 63 68 61 6e 67 65 73 2e 20 20 44 61 74 61 62 61  changes.  Databa
126aa 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 6f  se corruption wo
126ab 75 6c 64 20 6f 63 63 75 72 2e 0a 2a 2a 20 0a 2a  uld occur..** .*
126ac 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
126ad 6c 73 6f 20 75 70 64 61 74 65 73 20 74 68 65 20  lso updates the 
126ae 6e 52 65 63 20 66 69 65 6c 64 20 69 6e 20 74 68  nRec field in th
126af 65 20 68 65 61 64 65 72 20 6f 66 20 74 68 65 20  e header of the 
126b0 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 28 53 65 65  journal..** (See
126b1 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65   comments on the
126b2 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
126b3 29 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 64  ) routine for ad
126b4 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
126b5 74 69 6f 6e 2e 29 0a 2a 2a 20 49 66 20 74 68 65  tion.).** If the
126b6 20 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 46 55   sync mode is FU
126b7 4c 4c 2c 20 74 77 6f 20 73 79 6e 63 73 20 77 69  LL, two syncs wi
126b8 6c 6c 20 6f 63 63 75 72 2e 20 20 46 69 72 73 74  ll occur.  First
126b9 20 74 68 65 20 77 68 6f 6c 65 20 6a 6f 75 72 6e   the whole journ
126ba 61 6c 0a 2a 2a 20 69 73 20 73 79 6e 63 65 64 2c  al.** is synced,
126bb 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66   then the nRec f
126bc 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2c  ield is updated,
126bd 20 74 68 65 6e 20 61 20 73 65 63 6f 6e 64 20 73   then a second s
126be 79 6e 63 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a  ync occurs..**.*
126bf 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20  * For temporary 
126c0 64 61 74 61 62 61 73 65 73 2c 20 77 65 20 64 6f  databases, we do
126c1 20 6e 6f 74 20 63 61 72 65 20 69 66 20 77 65 20   not care if we 
126c2 61 72 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c  are able to roll
126c3 62 61 63 6b 0a 2a 2a 20 61 66 74 65 72 20 61 20  back.** after a 
126c4 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 73  power failure, s
126c5 6f 20 6e 6f 20 73 79 6e 63 20 6f 63 63 75 72 73  o no sync occurs
126c6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 49  ..**.** If the I
126c7 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20  OCAP_SEQUENTIAL 
126c8 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 20  flag is set for 
126c9 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d  the persistent m
126ca 65 64 69 61 20 6f 6e 20 77 68 69 63 68 0a 2a 2a  edia on which.**
126cb 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
126cc 20 73 74 6f 72 65 64 2c 20 74 68 65 6e 20 4f 73   stored, then Os
126cd 53 79 6e 63 28 29 20 69 73 20 6e 65 76 65 72 20  Sync() is never 
126ce 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6a 6f  called on the jo
126cf 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 49  urnal.** file. I
126d0 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20  n this case all 
126d1 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64  that is required
126d2 20 69 73 20 74 6f 20 75 70 64 61 74 65 20 74 68   is to update th
126d3 65 20 6e 52 65 63 20 66 69 65 6c 64 20 69 6e 0a  e nRec field in.
126d4 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  ** the journal h
126d5 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  eader..**.** Thi
126d6 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73  s routine clears
126d7 20 74 68 65 20 6e 65 65 64 53 79 6e 63 20 66 69   the needSync fi
126d8 65 6c 64 20 6f 66 20 65 76 65 72 79 20 70 61 67  eld of every pag
126d9 65 20 63 75 72 72 65 6e 74 20 68 65 6c 64 20 69  e current held i
126da 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  n.** memory..*/.
126db 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63 4a  static int syncJ
126dc 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50  ournal(Pager *pP
126dd 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  ager){.  int rc 
126de 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
126df 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72  /* Sync the jour
126e0 6e 61 6c 20 62 65 66 6f 72 65 20 6d 6f 64 69 66  nal before modif
126e1 79 69 6e 67 20 74 68 65 20 6d 61 69 6e 20 64 61  ying the main da
126e2 74 61 62 61 73 65 0a 20 20 2a 2a 20 28 61 73 73  tabase.  ** (ass
126e3 75 6d 69 6e 67 20 74 68 65 72 65 20 69 73 20 61  uming there is a
126e4 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 74 20   journal and it 
126e5 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63  needs to be sync
126e6 65 64 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ed.).  */.  if( 
126e7 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
126e8 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
126e9 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
126ea 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61  e );.    if( pPa
126eb 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
126ec 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  !=PAGER_JOURNALM
126ed 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20  ODE_MEMORY ){.  
126ee 20 20 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71      int iDc = sq
126ef 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61  lite3OsDeviceCha
126f0 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61  racteristics(pPa
126f1 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20  ger->fd);.      
126f2 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
126f3 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 0a  journalOpen );..
126f4 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44        if( 0==(iD
126f5 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  c&SQLITE_IOCAP_S
126f6 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20  AFE_APPEND) ){. 
126f7 20 20 20 20 20 20 20 69 36 34 20 6a 72 6e 6c 4f         i64 jrnlO
126f8 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f  ff = journalHdrO
126f9 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  ffset(pPager);. 
126fa 20 20 20 20 20 20 20 75 38 20 7a 4d 61 67 69 63         u8 zMagic
126fb 5b 38 5d 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  [8];..        /*
126fc 20 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c   This block deal
126fd 73 20 77 69 74 68 20 61 6e 20 6f 62 73 63 75 72  s with an obscur
126fe 65 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68  e problem. If th
126ff 65 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f  e last connectio
12700 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61  n.        ** tha
12701 74 20 77 72 6f 74 65 20 74 6f 20 74 68 69 73 20  t wrote to this 
12702 64 61 74 61 62 61 73 65 20 77 61 73 20 6f 70 65  database was ope
12703 72 61 74 69 6e 67 20 69 6e 20 70 65 72 73 69 73  rating in persis
12704 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20  tent-journal.   
12705 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68       ** mode, th
12706 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  en the journal f
12707 69 6c 65 20 6d 61 79 20 61 74 20 74 68 69 73 20  ile may at this 
12708 70 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 62  point actually b
12709 65 20 6c 61 72 67 65 72 0a 20 20 20 20 20 20 20  e larger.       
1270a 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 6a   ** than Pager.j
1270b 6f 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e  ournalOff bytes.
1270c 20 49 66 20 74 68 65 20 6e 65 78 74 20 74 68 69   If the next thi
1270d 6e 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ng in the journa
1270e 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c  l.        ** fil
1270f 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20  e happens to be 
12710 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72  a journal-header
12711 20 28 77 72 69 74 74 65 6e 20 61 73 20 70 61 72   (written as par
12712 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20  t of the.       
12713 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 6f 6e   ** previous con
12714 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63  nections transac
12715 74 69 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 61  tion), and a cra
12716 73 68 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c  sh or power-fail
12717 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ure .        ** 
12718 6f 63 63 75 72 73 20 61 66 74 65 72 20 6e 52 65  occurs after nRe
12719 63 20 69 73 20 75 70 64 61 74 65 64 20 62 75 74  c is updated but
1271a 20 62 65 66 6f 72 65 20 74 68 69 73 20 63 6f 6e   before this con
1271b 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 0a  nection writes .
1271c 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 68          ** anyth
1271d 69 6e 67 20 65 6c 73 65 20 74 6f 20 74 68 65 20  ing else to the 
1271e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72  journal file (or
1271f 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62   commits/rolls b
12720 61 63 6b 20 69 74 73 20 0a 20 20 20 20 20 20 20  ack its .       
12721 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 29   ** transaction)
12722 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61  , then SQLite ma
12723 79 20 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65  y become confuse
12724 64 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 65  d when doing the
12725 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f 74   .        ** hot
12726 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63  -journal rollbac
12727 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f  k following reco
12728 76 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c  very. It may rol
12729 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20  l back all.     
1272a 20 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f     ** of this co
1272b 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c 20  nnections data, 
1272c 74 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f 20  then proceed to 
1272d 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 65  rolling back the
1272e 20 6f 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a   old,.        **
1272f 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 74   out-of-date dat
12730 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69  a that follows i
12731 74 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72 72  t. Database corr
12732 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20  uption..        
12733 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 6f  **.        ** To
12734 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69   work around thi
12735 73 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61  s, if the journa
12736 6c 20 66 69 6c 65 20 64 6f 65 73 20 61 70 70 65  l file does appe
12737 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20  ar to contain.  
12738 20 20 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 64        ** a valid
12739 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e   header followin
1273a 67 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f  g Pager.journalO
1273b 66 66 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61  ff, then write a
1273c 20 30 78 30 30 0a 20 20 20 20 20 20 20 20 2a 2a   0x00.        **
1273d 20 62 79 74 65 20 74 6f 20 74 68 65 20 73 74 61   byte to the sta
1273e 72 74 20 6f 66 20 69 74 20 74 6f 20 70 72 65 76  rt of it to prev
1273f 65 6e 74 20 69 74 20 66 72 6f 6d 20 62 65 69 6e  ent it from bein
12740 67 20 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20  g recognized..  
12741 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
12742 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
12743 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ead(pPager->jfd,
12744 20 7a 4d 61 67 69 63 2c 20 38 2c 20 6a 72 6e 6c   zMagic, 8, jrnl
12745 4f 66 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Off);.        if
12746 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
12747 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 4d 61  && 0==memcmp(zMa
12748 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67  gic, aJournalMag
12749 69 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20  ic, 8) ){.      
1274a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
1274b 20 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30   u8 zerobyte = 0
1274c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ;.          rc =
1274d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
1274e 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65  pPager->jfd, &ze
1274f 72 6f 62 79 74 65 2c 20 31 2c 20 6a 72 6e 6c 4f  robyte, 1, jrnlO
12750 66 66 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ff);.        }. 
12751 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
12752 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d  QLITE_OK && rc!=
12753 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
12754 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20  RT_READ ){.     
12755 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
12756 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
12757 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
12758 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 74 6f 20  nRec value into 
12759 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1275a 20 68 65 61 64 65 72 2e 20 49 66 20 69 6e 0a 20   header. If in. 
1275b 20 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c 2d 73         ** full-s
1275c 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c  ynchronous mode,
1275d 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61   sync the journa
1275e 6c 20 66 69 72 73 74 2e 20 54 68 69 73 20 65 6e  l first. This en
1275f 73 75 72 65 73 20 74 68 61 74 0a 20 20 20 20 20  sures that.     
12760 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68     ** all data h
12761 61 73 20 72 65 61 6c 6c 79 20 68 69 74 20 74 68  as really hit th
12762 65 20 64 69 73 6b 20 62 65 66 6f 72 65 20 6e 52  e disk before nR
12763 65 63 20 69 73 20 75 70 64 61 74 65 64 20 74 6f  ec is updated to
12764 20 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 2a 2a   mark.        **
12765 20 69 74 20 61 73 20 61 20 63 61 6e 64 69 64 61   it as a candida
12766 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e  te for rollback.
12767 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
12768 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 6e      ** This is n
12769 6f 74 20 72 65 71 75 69 72 65 64 20 69 66 20 74  ot required if t
1276a 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d 65  he persistent me
1276b 64 69 61 20 73 75 70 70 6f 72 74 73 20 74 68 65  dia supports the
1276c 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46 45  .        ** SAFE
1276d 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79  _APPEND property
1276e 2e 20 42 65 63 61 75 73 65 20 69 6e 20 74 68 69  . Because in thi
1276f 73 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74  s case it is not
12770 20 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 20 20   possible .     
12771 20 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 61 67     ** for garbag
12772 65 20 64 61 74 61 20 74 6f 20 62 65 20 61 70 70  e data to be app
12773 65 6e 64 65 64 20 74 6f 20 74 68 65 20 66 69 6c  ended to the fil
12774 65 2c 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c  e, the nRec fiel
12775 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20  d.        ** is 
12776 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 30  populated with 0
12777 78 46 46 46 46 46 46 46 46 20 77 68 65 6e 20 74  xFFFFFFFF when t
12778 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
12779 72 20 69 73 20 77 72 69 74 74 65 6e 0a 20 20 20  r is written.   
1277a 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 76 65       ** and neve
1277b 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70  r needs to be up
1277c 64 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a  dated..        *
1277d 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  /.        if( pP
1277e 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26  ager->fullSync &
1277f 26 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45  & 0==(iDc&SQLITE
12780 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41  _IOCAP_SEQUENTIA
12781 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  L) ){.          
12782 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59 4e  PAGERTRACE(("SYN
12783 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c  C journal of %d\
12784 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
12785 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20 20  er)));.         
12786 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43   IOTRACE(("JSYNC
12787 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29   %p\n", pPager))
12788 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
12789 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50  sqlite3OsSync(pP
1278a 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65  ager->jfd, pPage
1278b 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a  r->sync_flags);.
1278c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
1278d 21 3d 30 20 29 20 72 65 74 75 72 6e 20 72 63 3b  !=0 ) return rc;
1278e 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
1278f 20 20 20 20 6a 72 6e 6c 4f 66 66 20 3d 20 70 50      jrnlOff = pP
12790 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
12791 20 2b 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e   + sizeof(aJourn
12792 61 6c 4d 61 67 69 63 29 3b 0a 20 20 20 20 20 20  alMagic);.      
12793 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52    IOTRACE(("JHDR
12794 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20   %p %lld %d\n", 
12795 70 50 61 67 65 72 2c 20 6a 72 6e 6c 4f 66 66 2c  pPager, jrnlOff,
12796 20 34 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63   4));.        rc
12797 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
12798 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c  Pager->jfd, jrnl
12799 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65  Off, pPager->nRe
1279a 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  c);.        if( 
1279b 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1279c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1279d 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45  ( 0==(iDc&SQLITE
1279e 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41  _IOCAP_SEQUENTIA
1279f 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 50 41  L) ){.        PA
127a0 47 45 52 54 52 41 43 45 28 28 22 53 59 4e 43 20  GERTRACE(("SYNC 
127a1 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22  journal of %d\n"
127a2 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72  , PAGERID(pPager
127a3 29 29 29 3b 0a 20 20 20 20 20 20 20 20 49 4f 54  )));.        IOT
127a4 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70 5c  RACE(("JSYNC %p\
127a5 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20  n", pPager)).   
127a6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
127a7 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e  3OsSync(pPager->
127a8 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e  jfd, pPager->syn
127a9 63 5f 66 6c 61 67 73 7c 20 0a 20 20 20 20 20 20  c_flags| .      
127aa 20 20 20 20 28 70 50 61 67 65 72 2d 3e 73 79 6e      (pPager->syn
127ab 63 5f 66 6c 61 67 73 3d 3d 53 51 4c 49 54 45 5f  c_flags==SQLITE_
127ac 53 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c 49 54 45  SYNC_FULL?SQLITE
127ad 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 3a 30  _SYNC_DATAONLY:0
127ae 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20  ).        );.   
127af 20 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29       if( rc!=0 )
127b0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
127b1 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72    }.      pPager
127b2 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
127b3 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
127b4 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
127b5 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 45 72   = 0;..    /* Er
127b6 61 73 65 20 74 68 65 20 6e 65 65 64 53 79 6e 63  ase the needSync
127b7 20 66 6c 61 67 20 66 72 6f 6d 20 65 76 65 72 79   flag from every
127b8 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   page..    */.  
127b9 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43    sqlite3PcacheC
127ba 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 70 50  learSyncFlags(pP
127bb 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
127bc 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
127bd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  ;.}../*.** Given
127be 20 61 20 6c 69 73 74 20 6f 66 20 70 61 67 65 73   a list of pages
127bf 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 74   (connected by t
127c0 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 79 20  he PgHdr.pDirty 
127c1 70 6f 69 6e 74 65 72 29 20 77 72 69 74 65 0a 2a  pointer) write.*
127c2 2a 20 65 76 65 72 79 20 6f 6e 65 20 6f 66 20 74  * every one of t
127c3 68 6f 73 65 20 70 61 67 65 73 20 6f 75 74 20 74  hose pages out t
127c4 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
127c5 69 6c 65 2e 20 4e 6f 20 63 61 6c 6c 73 20 61 72  ile. No calls ar
127c6 65 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65  e made.** to the
127c7 20 70 61 67 65 2d 63 61 63 68 65 20 74 6f 20 6d   page-cache to m
127c8 61 72 6b 20 74 68 65 20 70 61 67 65 73 20 61 73  ark the pages as
127c9 20 63 6c 65 61 6e 2e 20 49 74 20 69 73 20 74 68   clean. It is th
127ca 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  e responsibility
127cb 0a 2a 2a 20 6f 66 20 74 68 65 20 63 61 6c 6c 65  .** of the calle
127cc 72 20 74 6f 20 75 73 65 20 50 63 61 63 68 65 43  r to use PcacheC
127cd 6c 65 61 6e 41 6c 6c 28 29 20 6f 72 20 50 63 61  leanAll() or Pca
127ce 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 74  cheMakeClean() t
127cf 6f 20 6d 61 72 6b 0a 2a 2a 20 74 68 65 20 70 61  o mark.** the pa
127d0 67 65 73 20 61 73 20 63 6c 65 61 6e 2e 0a 2a 2f  ges as clean..*/
127d1 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
127d2 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74  r_write_pagelist
127d3 28 50 67 48 64 72 20 2a 70 4c 69 73 74 29 7b 0a  (PgHdr *pList){.
127d4 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
127d5 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66  .  int rc;..  if
127d6 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74  ( pList==0 ) ret
127d7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
127d8 20 70 50 61 67 65 72 20 3d 20 70 4c 69 73 74 2d   pPager = pList-
127d9 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a 20 41  >pPager;..  /* A
127da 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
127db 72 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72  re may be either
127dc 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45   a RESERVED or E
127dd 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e  XCLUSIVE lock on
127de 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
127df 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72  se file. If ther
127e0 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e 20  e is already an 
127e1 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20  EXCLUSIVE lock, 
127e2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
127e3 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ** calls to sqli
127e4 74 65 33 4f 73 4c 6f 63 6b 28 29 20 61 72 65 20  te3OsLock() are 
127e5 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2a 0a 20 20 2a  no-ops..  **.  *
127e6 2a 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63  * Moving the loc
127e7 6b 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44 20  k from RESERVED 
127e8 74 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63 74  to EXCLUSIVE act
127e9 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67  ually involves g
127ea 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67  oing.  ** throug
127eb 68 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 74  h an intermediat
127ec 65 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47 2e  e state PENDING.
127ed 20 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63     A PENDING loc
127ee 6b 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a 20  k prevents new. 
127ef 20 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f 6d   ** readers from
127f0 20 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74 68   attaching to th
127f1 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 69  e database but i
127f2 73 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20 66  s unsufficient f
127f3 6f 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72  or us to.  ** wr
127f4 69 74 65 2e 20 20 54 68 65 20 69 64 65 61 20 6f  ite.  The idea o
127f5 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  f a PENDING lock
127f6 20 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20 6e   is to prevent n
127f7 65 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d 0a  ew readers from.
127f8 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77    ** coming in w
127f9 68 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f 72  hile we wait for
127fa 20 65 78 69 73 74 69 6e 67 20 72 65 61 64 65 72   existing reader
127fb 73 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a  s to clear..  **
127fc 0a 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 20  .  ** While the 
127fd 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 20  pager is in the 
127fe 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20  RESERVED state, 
127ff 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74  the original dat
12800 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20  abase file.  ** 
12801 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64  is unchanged and
12802 20 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b   we can rollback
12803 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
12804 74 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65 0a  to playback the.
12805 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74    ** journal int
12806 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  o the original d
12807 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4f  atabase file.  O
12808 6e 63 65 20 77 65 20 74 72 61 6e 73 69 74 69 6f  nce we transitio
12809 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53  n to.  ** EXCLUS
1280a 49 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  IVE, it means th
1280b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1280c 68 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65 64  has been changed
1280d 20 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63   and any rollbac
1280e 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75  k.  ** will requ
1280f 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c  ire a journal pl
12810 61 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 72  ayback..  */.  r
12811 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f  c = pager_wait_o
12812 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45  n_lock(pPager, E
12813 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a  XCLUSIVE_LOCK);.
12814 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12815 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
12816 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 77 68 69  n rc;.  }..  whi
12817 6c 65 28 20 70 4c 69 73 74 20 29 7b 0a 0a 20 20  le( pList ){..  
12818 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65    /* If the file
12819 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65   has not yet bee
1281a 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69  n opened, open i
1281b 74 20 6e 6f 77 2e 20 2a 2f 0a 20 20 20 20 69 66  t now. */.    if
1281c 28 20 21 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70  ( !pPager->fd->p
1281d 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20  Methods ){.     
1281e 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e   assert(pPager->
1281f 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 20 20 20  tempFile);.     
12820 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
12821 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65  erOpentemp(pPage
12822 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 70  r, pPager->fd, p
12823 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 29  Pager->vfsFlags)
12824 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
12825 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
12826 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
12827 72 65 20 61 72 65 20 64 69 72 74 79 20 70 61 67  re are dirty pag
12828 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63  es in the page c
12829 61 63 68 65 20 77 69 74 68 20 70 61 67 65 20 6e  ache with page n
1282a 75 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a 20  umbers greater. 
1282b 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72     ** than Pager
1282c 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65  .dbSize, this me
1282d 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65 72  ans sqlite3Pager
1282e 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20  TruncateImage() 
1282f 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20  was called to.  
12830 20 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69    ** make the fi
12831 6c 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73  le smaller (pres
12832 75 6d 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76  umably by auto-v
12833 61 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20  acuum code). Do 
12834 6e 6f 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a  not write.    **
12835 20 61 6e 79 20 73 75 63 68 20 70 61 67 65 73 20   any such pages 
12836 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20  to the file..   
12837 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c 69 73   */.    if( pLis
12838 74 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d  t->pgno<=pPager-
12839 3e 64 62 53 69 7a 65 20 26 26 20 30 3d 3d 28 70  >dbSize && 0==(p
1283a 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 48 44  List->flags&PGHD
1283b 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20 29 7b  R_DONT_WRITE) ){
1283c 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66 73 65  .      i64 offse
1283d 74 20 3d 20 28 70 4c 69 73 74 2d 3e 70 67 6e 6f  t = (pList->pgno
1283e 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d  -1)*(i64)pPager-
1283f 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20  >pageSize;.     
12840 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 43   char *pData = C
12841 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 4c  ODEC2(pPager, pL
12842 69 73 74 2d 3e 70 44 61 74 61 2c 20 70 4c 69 73  ist->pData, pLis
12843 74 2d 3e 70 67 6e 6f 2c 20 36 29 3b 0a 0a 20 20  t->pgno, 6);..  
12844 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28      PAGERTRACE((
12845 22 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25  "STORE %d page %
12846 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c  d hash(%08x)\n",
12847 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
12848 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67      PAGERID(pPag
12849 65 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f  er), pList->pgno
1284a 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  , pager_pagehash
1284b 28 70 4c 69 73 74 29 29 29 3b 0a 20 20 20 20 20  (pList)));.     
1284c 20 49 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54   IOTRACE(("PGOUT
1284d 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   %p %d\n", pPage
1284e 72 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 29 29  r, pList->pgno))
1284f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
12850 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
12851 65 72 2d 3e 66 64 2c 20 70 44 61 74 61 2c 20 70  er->fd, pData, p
12852 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
12853 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20   offset);.      
12854 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74  PAGER_INCR(sqlit
12855 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62  e3_pager_writedb
12856 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50  _count);.      P
12857 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72  AGER_INCR(pPager
12858 2d 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 20  ->nWrite);.     
12859 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67 6e 6f   if( pList->pgno
1285a 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  ==1 ){.        m
1285b 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64  emcpy(&pPager->d
1285c 62 46 69 6c 65 56 65 72 73 2c 20 26 70 44 61 74  bFileVers, &pDat
1285d 61 5b 32 34 5d 2c 20 73 69 7a 65 6f 66 28 70 50  a[24], sizeof(pP
1285e 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73  ager->dbFileVers
1285f 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
12860 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67 6e    if( pList->pgn
12861 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  o>pPager->dbFile
12862 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Size ){.        
12863 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69  pPager->dbFileSi
12864 7a 65 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f  ze = pList->pgno
12865 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
12866 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
12867 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 50     else{.      P
12868 41 47 45 52 54 52 41 43 45 28 28 22 4e 4f 53 54  AGERTRACE(("NOST
12869 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 5c 6e  ORE %d page %d\n
1286a 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
1286b 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 29  r), pList->pgno)
1286c 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
1286d 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1286e 75 72 6e 20 72 63 3b 0a 23 69 66 64 65 66 20 53  urn rc;.#ifdef S
1286f 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
12870 53 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61 67  S.    pList->pag
12871 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61  eHash = pager_pa
12872 67 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a 23  gehash(pList);.#
12873 65 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74 20  endif.    pList 
12874 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 3b  = pList->pDirty;
12875 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
12876 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
12877 2a 2a 20 41 64 64 20 74 68 65 20 70 61 67 65 20  ** Add the page 
12878 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  to the sub-journ
12879 61 6c 2e 20 49 74 20 69 73 20 74 68 65 20 63 61  al. It is the ca
1287a 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69  llers responsibi
1287b 6c 69 74 79 20 74 6f 0a 2a 2a 20 75 73 65 20 73  lity to.** use s
1287c 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28  ubjRequiresPage(
1287d 29 20 74 6f 20 63 68 65 63 6b 20 74 68 61 74 20  ) to check that 
1287e 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72 65 71  it is really req
1287f 75 69 72 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a  uired before .**
12880 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75   calling this fu
12881 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
12882 63 20 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c  c int subjournal
12883 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29  Page(PgHdr *pPg)
12884 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 76 6f  {.  int rc;.  vo
12885 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d  id *pData = pPg-
12886 3e 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20  >pData;.  Pager 
12887 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
12888 50 61 67 65 72 3b 0a 20 20 69 36 34 20 6f 66 66  Pager;.  i64 off
12889 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 73 74  set = pPager->st
1288a 6d 74 4e 52 65 63 2a 28 34 2b 70 50 61 67 65 72  mtNRec*(4+pPager
1288b 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 63  ->pageSize);.  c
1288c 68 61 72 20 2a 70 44 61 74 61 32 20 3d 20 43 4f  har *pData2 = CO
1288d 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 61  DEC2(pPager, pDa
1288e 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37  ta, pPg->pgno, 7
1288f 29 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45  );..  PAGERTRACE
12890 28 28 22 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20  (("STMT-JOURNAL 
12891 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50  %d page %d\n", P
12892 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
12893 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20  pPg->pgno));..  
12894 61 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f  assert( pageInJo
12895 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50  urnal(pPg) || pP
12896 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e  g->pgno>pPager->
12897 64 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20  dbOrigSize );.  
12898 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
12899 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f  (pPager->sjfd, o
1289a 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f  ffset, pPg->pgno
1289b 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1289c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
1289d 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
1289e 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20  e(pPager->sjfd, 
1289f 70 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e  pData2, pPager->
128a0 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74  pageSize, offset
128a1 2b 34 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  +4);.  }.  if( r
128a2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
128a3 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74      pPager->stmt
128a4 4e 52 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73 65  NRec++;.    asse
128a5 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76  rt( pPager->nSav
128a6 65 70 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20 20  epoint>0 );.    
128a7 72 63 20 3d 20 61 64 64 54 6f 53 61 76 65 70 6f  rc = addToSavepo
128a8 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65  intBitvecs(pPage
128a9 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20  r, pPg->pgno);. 
128aa 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
128ab 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  }.../*.** This f
128ac 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
128ad 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20  d by the pcache 
128ae 6c 61 79 65 72 20 77 68 65 6e 20 69 74 20 68 61  layer when it ha
128af 73 20 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a  s reached some.*
128b0 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69  * soft memory li
128b1 6d 69 74 2e 20 54 68 65 20 61 72 67 75 6d 65 6e  mit. The argumen
128b2 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
128b3 6f 20 61 20 70 75 72 67 65 61 62 6c 65 20 50 61  o a purgeable Pa
128b4 67 65 72 20 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20  ger .** object. 
128b5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74  This function at
128b6 74 65 6d 70 74 73 20 74 6f 20 6d 61 6b 65 20 61  tempts to make a
128b7 20 73 69 6e 67 6c 65 20 64 69 72 74 79 20 70 61   single dirty pa
128b8 67 65 20 74 68 61 74 20 68 61 73 20 6e 6f 0a 2a  ge that has no.*
128b9 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65  * outstanding re
128ba 66 65 72 65 6e 63 65 73 20 28 69 66 20 6f 6e 65  ferences (if one
128bb 20 65 78 69 73 74 73 29 20 63 6c 65 61 6e 20 73   exists) clean s
128bc 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65  o that it can be
128bd 20 72 65 63 79 63 6c 65 64 20 0a 2a 2a 20 62 79   recycled .** by
128be 20 74 68 65 20 70 63 61 63 68 65 20 6c 61 79 65   the pcache laye
128bf 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
128c0 20 70 61 67 65 72 53 74 72 65 73 73 28 76 6f 69   pagerStress(voi
128c1 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70 50 67  d *p, PgHdr *pPg
128c2 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
128c3 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29 70 3b  er = (Pager *)p;
128c4 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
128c5 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 50  TE_OK;..  if( pP
128c6 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20  ager->doNotSync 
128c7 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
128c8 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
128c9 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61  assert( pPg->fla
128ca 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29  gs&PGHDR_DIRTY )
128cb 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
128cc 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f  errCode==SQLITE_
128cd 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  OK ){.    if( pP
128ce 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
128cf 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20  EED_SYNC ){.    
128d0 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e    rc = syncJourn
128d1 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  al(pPager);.    
128d2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
128d3 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 66  _OK && pPager->f
128d4 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 20  ullSync && .    
128d5 20 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f      !(pPager->jo
128d6 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
128d7 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
128d8 4f 52 59 29 20 26 26 0a 20 20 20 20 20 20 20 20  ORY) &&.        
128d9 21 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63  !(sqlite3OsDevic
128da 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
128db 28 70 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c  (pPager->fd)&SQL
128dc 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
128dd 50 50 45 4e 44 29 0a 20 20 20 20 20 20 29 7b 0a  PPEND).      ){.
128de 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
128df 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 20 20  nRec = 0;.      
128e0 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72    rc = writeJour
128e1 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a  nalHdr(pPager);.
128e2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
128e3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
128e4 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 67  _OK ){.      pPg
128e5 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  ->pDirty = 0;.  
128e6 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e      if( pPg->pgn
128e7 6f 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  o>pPager->dbSize
128e8 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 65 73   && subjRequires
128e9 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20  Page(pPg) ){.   
128ea 20 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75       rc = subjou
128eb 72 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20  rnalPage(pPg);. 
128ec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
128ed 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
128ee 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  {.        rc = p
128ef 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c  ager_write_pagel
128f0 69 73 74 28 70 50 67 29 3b 0a 20 20 20 20 20 20  ist(pPg);.      
128f1 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
128f2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
128f3 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 72  .      pager_err
128f4 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a  or(pPager, rc);.
128f5 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
128f6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
128f7 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45  {.    PAGERTRACE
128f8 28 28 22 53 54 52 45 53 53 20 25 64 20 70 61 67  (("STRESS %d pag
128f9 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  e %d\n", PAGERID
128fa 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70  (pPager), pPg->p
128fb 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  gno));.    sqlit
128fc 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61  e3PcacheMakeClea
128fd 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65  n(pPg);.  }.  re
128fe 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
128ff 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74  ** Return 1 if t
12900 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f  here is a hot jo
12901 75 72 6e 61 6c 20 6f 6e 20 74 68 65 20 67 69 76  urnal on the giv
12902 65 6e 20 70 61 67 65 72 2e 0a 2a 2a 20 41 20 68  en pager..** A h
12903 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e  ot journal is on
12904 65 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20  e that needs to 
12905 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a  be played back..
12906 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72  **.** If the cur
12907 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65  rent size of the
12908 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
12909 73 20 30 20 62 75 74 20 61 20 6a 6f 75 72 6e 61  s 0 but a journa
1290a 6c 20 66 69 6c 65 0a 2a 2a 20 65 78 69 73 74 73  l file.** exists
1290b 2c 20 74 68 61 74 20 69 73 20 70 72 6f 62 61 62  , that is probab
1290c 6c 79 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61  ly an old journa
1290d 6c 20 6c 65 66 74 20 6f 76 65 72 20 66 72 6f 6d  l left over from
1290e 20 61 20 70 72 69 6f 72 0a 2a 2a 20 64 61 74 61   a prior.** data
1290f 62 61 73 65 20 77 69 74 68 20 74 68 65 20 73 61  base with the sa
12910 6d 65 20 6e 61 6d 65 2e 20 20 4a 75 73 74 20 64  me name.  Just d
12911 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  elete the journa
12912 6c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  l..**.** Return 
12913 6e 65 67 61 74 69 76 65 20 69 66 20 75 6e 61 62  negative if unab
12914 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  le to determine 
12915 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 74 68  the status of th
12916 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a  e journal..**.**
12917 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   This routine do
12918 65 73 20 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20  es not open the 
12919 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20  journal file to 
1291a 65 78 61 6d 69 6e 65 20 69 74 73 0a 2a 2a 20 63  examine its.** c
1291b 6f 6e 74 65 6e 74 2e 20 20 48 65 6e 63 65 2c 20  ontent.  Hence, 
1291c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68  the journal migh
1291d 74 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61  t contain the na
1291e 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 0a 2a  me of a master.*
1291f 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  * journal file t
12920 68 61 74 20 68 61 73 20 62 65 65 6e 20 64 65 6c  hat has been del
12921 65 74 65 64 2c 20 61 6e 64 20 68 65 6e 63 65 20  eted, and hence 
12922 6e 6f 74 20 62 65 20 68 6f 74 2e 20 20 4f 72 0a  not be hot.  Or.
12923 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 6f 66  ** the header of
12924 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67   the journal mig
12925 68 74 20 62 65 20 7a 65 72 6f 65 64 20 6f 75 74  ht be zeroed out
12926 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a  .  This routine.
12927 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 64 69 73 63  ** does not disc
12928 6f 76 65 72 20 74 68 65 73 65 20 63 61 73 65 73  over these cases
12929 20 6f 66 20 61 20 6e 6f 6e 2d 68 6f 74 20 6a 6f   of a non-hot jo
1292a 75 72 6e 61 6c 20 2d 20 69 66 20 74 68 65 0a 2a  urnal - if the.*
1292b 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65  * journal file e
1292c 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74  xists and is not
1292d 20 65 6d 70 74 79 20 74 68 69 73 20 72 6f 75 74   empty this rout
1292e 69 6e 65 20 61 73 73 75 6d 65 73 20 69 74 0a 2a  ine assumes it.*
1292f 2a 20 69 73 20 68 6f 74 2e 20 20 54 68 65 20 70  * is hot.  The p
12930 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20  ager_playback() 
12931 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73  routine will dis
12932 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 0a 2a  cover that the.*
12933 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  * journal file i
12934 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74  s not really hot
12935 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 2d 6f 70 2e   and will no-op.
12936 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
12937 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 67  asHotJournal(Pag
12938 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
12939 2a 70 45 78 69 73 74 73 29 7b 0a 20 20 73 71 6c  *pExists){.  sql
1293a 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
1293b 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20   pPager->pVfs;. 
1293c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1293d 5f 4f 4b 3b 0a 20 20 69 6e 74 20 65 78 69 73 74  _OK;.  int exist
1293e 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6c 6f 63  s = 0;.  int loc
1293f 6b 65 64 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  ked = 0;.  asser
12940 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a  t( pPager!=0 );.
12941 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12942 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a  ->useJournal );.
12943 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12944 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29  ->fd->pMethods )
12945 3b 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30  ;.  *pExists = 0
12946 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
12947 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70  OsAccess(pVfs, p
12948 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
12949 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
1294a 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b  XISTS, &exists);
1294b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1294c 45 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29  E_OK && exists )
1294d 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1294e 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
1294f 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64  dLock(pPager->fd
12950 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 7d 0a  , &locked);.  }.
12951 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
12952 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 26 26  _OK && exists &&
12953 20 21 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20   !locked ){.    
12954 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 72  int nPage;.    r
12955 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
12956 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
12957 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 69  , &nPage);.    i
12958 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12959 20 29 7b 0a 20 20 20 20 20 69 66 28 20 6e 50 61   ){.     if( nPa
1295a 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ge==0 ){.       
1295b 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
1295c 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  (pVfs, pPager->z
1295d 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20  Journal, 0);.   
1295e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1295f 20 20 2a 70 45 78 69 73 74 73 20 3d 20 31 3b 0a    *pExists = 1;.
12960 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
12961 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
12962 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65  ../*.** Read the
12963 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65   content of page
12964 20 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20   pPg out of the 
12965 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
12966 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61  /.static int rea
12967 64 44 62 50 61 67 65 28 50 61 67 65 72 20 2a 70  dDbPage(Pager *p
12968 50 61 67 65 72 2c 20 50 67 48 64 72 20 2a 70 50  Pager, PgHdr *pP
12969 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20  g, Pgno pgno){. 
1296a 20 69 6e 74 20 72 63 3b 0a 20 20 69 36 34 20 6f   int rc;.  i64 o
1296b 66 66 73 65 74 3b 0a 20 20 61 73 73 65 72 74 28  ffset;.  assert(
1296c 20 4d 45 4d 44 42 3d 3d 30 20 29 3b 0a 20 20 61   MEMDB==0 );.  a
1296d 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66 64  ssert(pPager->fd
1296e 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67  ->pMethods||pPag
1296f 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20  er->tempFile);. 
12970 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 66 64   if( !pPager->fd
12971 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ->pMethods ){.  
12972 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12973 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
12974 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d  ;.  }.  offset =
12975 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70   (pgno-1)*(i64)p
12976 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b  Pager->pageSize;
12977 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
12978 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64  sRead(pPager->fd
12979 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 50  , pPg->pData, pP
1297a 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
1297b 6f 66 66 73 65 74 29 3b 0a 20 20 50 41 47 45 52  offset);.  PAGER
1297c 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61  _INCR(sqlite3_pa
1297d 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74  ger_readdb_count
1297e 29 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28  );.  PAGER_INCR(
1297f 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a  pPager->nRead);.
12980 20 20 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e    IOTRACE(("PGIN
12981 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   %p %d\n", pPage
12982 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 69 66 28  r, pgno));.  if(
12983 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20   pgno==1 ){.    
12984 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e  memcpy(&pPager->
12985 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75  dbFileVers, &((u
12986 38 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 5b 32  8*)pPg->pData)[2
12987 34 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  4],.            
12988 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12989 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1298a 20 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d    sizeof(pPager-
1298b 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20  >dbFileVers));. 
1298c 20 7d 0a 20 20 43 4f 44 45 43 31 28 70 50 61 67   }.  CODEC1(pPag
1298d 65 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20  er, pPg->pData, 
1298e 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20  pPg->pgno, 3);. 
1298f 20 50 41 47 45 52 54 52 41 43 45 28 28 22 46 45   PAGERTRACE(("FE
12990 54 43 48 20 25 64 20 70 61 67 65 20 25 64 20 68  TCH %d page %d h
12991 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20  ash(%08x)\n",.  
12992 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47               PAG
12993 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50  ERID(pPager), pP
12994 67 2d 3e 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70  g->pgno, pager_p
12995 61 67 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a  agehash(pPg)));.
12996 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
12997 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
12998 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
12999 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 73 68 61  o obtain the sha
1299a 72 65 64 20 6c 6f 63 6b 20 72 65 71 75 69 72 65  red lock require
1299b 64 20 62 65 66 6f 72 65 0a 2a 2a 20 64 61 74 61  d before.** data
1299c 20 6d 61 79 20 62 65 20 72 65 61 64 20 66 72 6f   may be read fro
1299d 6d 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68  m the pager cach
1299e 65 2e 20 49 66 20 74 68 65 20 73 68 61 72 65 64  e. If the shared
1299f 20 6c 6f 63 6b 20 68 61 73 20 61 6c 72 65 61 64   lock has alread
129a0 79 0a 2a 2a 20 62 65 65 6e 20 6f 62 74 61 69 6e  y.** been obtain
129a1 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ed, this functio
129a2 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  n is a no-op..**
129a3 0a 2a 2a 20 49 6d 6d 65 64 69 61 74 65 6c 79 20  .** Immediately 
129a4 61 66 74 65 72 20 6f 62 74 61 69 6e 69 6e 67 20  after obtaining 
129a5 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  the shared lock 
129a6 28 69 66 20 72 65 71 75 69 72 65 64 29 2c 20 74  (if required), t
129a7 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
129a8 63 68 65 63 6b 73 20 66 6f 72 20 61 20 68 6f 74  checks for a hot
129a9 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49  -journal file. I
129aa 66 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20  f one is found, 
129ab 61 6e 20 65 6d 65 72 67 65 6e 63 79 20 72 6f 6c  an emergency rol
129ac 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 70 65 72 66  lback.** is perf
129ad 6f 72 6d 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  ormed immediatel
129ae 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
129af 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b   pagerSharedLock
129b0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
129b1 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
129b2 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 73 45  TE_OK;.  int isE
129b3 72 72 6f 72 52 65 73 65 74 20 3d 20 30 3b 0a 0a  rrorReset = 0;..
129b4 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74    /* If this dat
129b5 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20  abase is opened 
129b6 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20 61 63  for exclusive ac
129b7 63 65 73 73 2c 20 68 61 73 20 6e 6f 20 6f 75 74  cess, has no out
129b8 73 74 61 6e 64 69 6e 67 20 0a 20 20 2a 2a 20 70  standing .  ** p
129b9 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 61  age references a
129ba 6e 64 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f  nd is in an erro
129bb 72 2d 73 74 61 74 65 2c 20 6e 6f 77 20 69 73 20  r-state, now is 
129bc 74 68 65 20 63 68 61 6e 63 65 20 74 6f 20 63 6c  the chance to cl
129bd 65 61 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 72  ear.  ** the err
129be 6f 72 2e 20 44 69 73 63 61 72 64 20 74 68 65 20  or. Discard the 
129bf 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
129c0 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20  pager-cache and 
129c1 74 72 65 61 74 20 61 6e 79 0a 20 20 2a 2a 20 6f  treat any.  ** o
129c2 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  pen journal file
129c3 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61   as a hot-journa
129c4 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 4d  l..  */.  if( !M
129c5 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e  EMDB && pPager->
129c6 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20  exclusiveMode . 
129c7 20 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 63    && sqlite3Pcac
129c8 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65  heRefCount(pPage
129c9 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 26  r->pPCache)==0 &
129ca 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  & pPager->errCod
129cb 65 20 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20  e .  ){.    if( 
129cc 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
129cd 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 69 73 45  pen ){.      isE
129ce 72 72 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a 20  rrorReset = 1;. 
129cf 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d     }.    pPager-
129d0 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54  >errCode = SQLIT
129d1 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f  E_OK;.    pager_
129d2 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  reset(pPager);. 
129d3 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
129d4 70 61 67 65 72 20 69 73 20 73 74 69 6c 6c 20 69  pager is still i
129d5 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65  n an error state
129d6 2c 20 64 6f 20 6e 6f 74 20 70 72 6f 63 65 65 64  , do not proceed
129d7 2e 20 54 68 65 20 65 72 72 6f 72 20 0a 20 20 2a  . The error .  *
129d8 2a 20 73 74 61 74 65 20 77 69 6c 6c 20 62 65 20  * state will be 
129d9 63 6c 65 61 72 65 64 20 61 74 20 73 6f 6d 65 20  cleared at some 
129da 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74  point in the fut
129db 75 72 65 20 77 68 65 6e 20 61 6c 6c 20 70 61 67  ure when all pag
129dc 65 20 0a 20 20 2a 2a 20 72 65 66 65 72 65 6e 63  e .  ** referenc
129dd 65 73 20 61 72 65 20 64 72 6f 70 70 65 64 20 61  es are dropped a
129de 6e 64 20 74 68 65 20 63 61 63 68 65 20 63 61 6e  nd the cache can
129df 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 0a 20   be discarded.. 
129e0 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72   */.  if( pPager
129e1 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 70 50 61  ->errCode && pPa
129e2 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51  ger->errCode!=SQ
129e3 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20  LITE_FULL ){.   
129e4 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
129e5 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20  errCode;.  }..  
129e6 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
129e7 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20  e==PAGER_UNLOCK 
129e8 7c 7c 20 69 73 45 72 72 6f 72 52 65 73 65 74 20  || isErrorReset 
129e9 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
129ea 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65  fs *pVfs = pPage
129eb 72 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74  r->pVfs;.    int
129ec 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d 20   isHotJournal = 
129ed 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  0;.    assert( !
129ee 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73  MEMDB );.    ass
129ef 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 63  ert( sqlite3Pcac
129f0 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65  heRefCount(pPage
129f1 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29  r->pPCache)==0 )
129f2 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  ;.    if( !pPage
129f3 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b  r->noReadlock ){
129f4 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
129f5 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70  r_wait_on_lock(p
129f6 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c 4f  Pager, SHARED_LO
129f7 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  CK);.      if( r
129f8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
129f9 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
129fa 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
129fb 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20  AGER_UNLOCK );. 
129fc 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 61         return pa
129fd 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
129fe 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20  , rc);.      }. 
129ff 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61     }else if( pPa
12a00 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45  ger->state==PAGE
12a01 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  R_UNLOCK ){.    
12a02 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
12a03 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a  = PAGER_SHARED;.
12a04 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
12a05 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
12a06 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
12a07 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 6a 6f 75  .    /* If a jou
12a08 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73  rnal file exists
12a09 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e  , and there is n
12a0a 6f 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  o RESERVED lock 
12a0b 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61  on the.    ** da
12a0c 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
12a0d 6e 20 69 74 20 65 69 74 68 65 72 20 6e 65 65 64  n it either need
12a0e 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62  s to be played b
12a0f 61 63 6b 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a  ack or deleted..
12a10 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
12a11 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b 0a  isErrorReset ){.
12a12 20 20 20 20 20 20 72 63 20 3d 20 68 61 73 48 6f        rc = hasHo
12a13 74 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c  tJournal(pPager,
12a14 20 26 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b   &isHotJournal);
12a15 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
12a16 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12a17 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b      goto failed;
12a18 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
12a19 20 20 20 69 66 28 20 69 73 45 72 72 6f 72 52 65     if( isErrorRe
12a1a 73 65 74 20 7c 7c 20 69 73 48 6f 74 4a 6f 75 72  set || isHotJour
12a1b 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  nal ){.      /* 
12a1c 47 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45  Get an EXCLUSIVE
12a1d 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
12a1e 61 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20 74  abase file. At t
12a1f 68 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73 0a  his point it is.
12a20 20 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 61        ** importa
12a21 6e 74 20 74 68 61 74 20 61 20 52 45 53 45 52 56  nt that a RESERV
12a22 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 6f  ED lock is not o
12a23 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 77  btained on the w
12a24 61 79 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20  ay to the.      
12a25 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  ** EXCLUSIVE loc
12a26 6b 2e 20 49 66 20 69 74 20 77 65 72 65 2c 20 61  k. If it were, a
12a27 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d  nother process m
12a28 69 67 68 74 20 6f 70 65 6e 20 74 68 65 0a 20 20  ight open the.  
12a29 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
12a2a 66 69 6c 65 2c 20 64 65 74 65 63 74 20 74 68 65  file, detect the
12a2b 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20   RESERVED lock, 
12a2c 61 6e 64 20 63 6f 6e 63 6c 75 64 65 20 74 68 61  and conclude tha
12a2d 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64  t the.      ** d
12a2e 61 74 61 62 61 73 65 20 69 73 20 73 61 66 65 20  atabase is safe 
12a2f 74 6f 20 72 65 61 64 20 77 68 69 6c 65 20 74 68  to read while th
12a30 69 73 20 70 72 6f 63 65 73 73 20 69 73 20 73 74  is process is st
12a31 69 6c 6c 20 72 6f 6c 6c 69 6e 67 20 69 74 20 0a  ill rolling it .
12a32 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 2e 0a 20        ** back.. 
12a33 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 2a       ** .      *
12a34 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 69 6e  * Because the in
12a35 74 65 72 6d 65 64 69 61 74 65 20 52 45 53 45 52  termediate RESER
12a36 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20  VED lock is not 
12a37 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 0a 20  requested, the. 
12a38 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 20 70       ** second p
12a39 72 6f 63 65 73 73 20 77 69 6c 6c 20 67 65 74 20  rocess will get 
12a3a 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e  to this point in
12a3b 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20 66 61   the code and fa
12a3c 69 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 6f  il to.      ** o
12a3d 62 74 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58  btain its own EX
12a3e 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
12a3f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12a40 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
12a41 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
12a42 61 74 65 3c 45 58 43 4c 55 53 49 56 45 5f 4c 4f  ate<EXCLUSIVE_LO
12a43 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  CK ){.        rc
12a44 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b   = sqlite3OsLock
12a45 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 45 58 43  (pPager->fd, EXC
12a46 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20  LUSIVE_LOCK);.  
12a47 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
12a48 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
12a49 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
12a4a 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63  error(pPager, rc
12a4b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
12a4c 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20  o failed;.      
12a4d 20 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61 67    }.        pPag
12a4e 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
12a4f 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20  R_EXCLUSIVE;.   
12a50 20 20 20 7d 0a 20 0a 20 20 20 20 20 20 2f 2a 20     }. .      /* 
12a51 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Open the journal
12a52 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20   for read/write 
12a53 61 63 63 65 73 73 2e 20 54 68 69 73 20 69 73 20  access. This is 
12a54 62 65 63 61 75 73 65 20 69 6e 20 0a 20 20 20 20  because in .    
12a55 20 20 2a 2a 20 65 78 63 6c 75 73 69 76 65 2d 61    ** exclusive-a
12a56 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 66  ccess mode the f
12a57 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77  ile descriptor w
12a58 69 6c 6c 20 62 65 20 6b 65 70 74 20 6f 70 65 6e  ill be kept open
12a59 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 70 6f   and.      ** po
12a5a 73 73 69 62 6c 79 20 75 73 65 64 20 66 6f 72 20  ssibly used for 
12a5b 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6c 61  a transaction la
12a5c 74 65 72 20 6f 6e 2e 20 4f 6e 20 73 6f 6d 65 20  ter on. On some 
12a5d 73 79 73 74 65 6d 73 2c 20 74 68 65 0a 20 20 20  systems, the.   
12a5e 20 20 20 2a 2a 20 4f 73 54 72 75 6e 63 61 74 65     ** OsTruncate
12a5f 28 29 20 63 61 6c 6c 20 75 73 65 64 20 69 6e 20  () call used in 
12a60 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73  exclusive-access
12a61 20 6d 6f 64 65 20 61 6c 73 6f 20 72 65 71 75 69   mode also requi
12a62 72 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 72  res.      ** a r
12a63 65 61 64 2f 77 72 69 74 65 20 66 69 6c 65 20 68  ead/write file h
12a64 61 6e 64 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  andle..      */.
12a65 20 20 20 20 20 20 69 66 28 20 21 69 73 45 72 72        if( !isErr
12a66 6f 72 52 65 73 65 74 20 26 26 20 70 50 61 67 65  orReset && pPage
12a67 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d  r->journalOpen==
12a68 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  0 ){.        int
12a69 20 72 65 73 3b 0a 20 20 20 20 20 20 20 20 72 63   res;.        rc
12a6a 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65   = sqlite3OsAcce
12a6b 73 73 28 70 56 66 73 2c 70 50 61 67 65 72 2d 3e  ss(pVfs,pPager->
12a6c 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 54 45 5f  zJournal,SQLITE_
12a6d 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 26 72  ACCESS_EXISTS,&r
12a6e 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  es);.        if(
12a6f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
12a70 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
12a71 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  res ){.         
12a72 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b     int fout = 0;
12a73 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
12a74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e   f = SQLITE_OPEN
12a75 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
12a76 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
12a77 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 20  NAL;.           
12a78 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72   assert( !pPager
12a79 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20  ->tempFile );.  
12a7a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
12a7b 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
12a7c 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
12a7d 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  nal, pPager->jfd
12a7e 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20  , f, &fout);.   
12a7f 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
12a80 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
12a81 7c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d 3e 70  | pPager->jfd->p
12a82 4d 65 74 68 6f 64 73 20 29 3b 0a 20 20 20 20 20  Methods );.     
12a83 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
12a84 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 74  QLITE_OK && fout
12a85 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  &SQLITE_OPEN_REA
12a86 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 20  DONLY ){.       
12a87 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
12a88 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20  TE_CANTOPEN;.   
12a89 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
12a8a 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72  e3OsClose(pPager
12a8b 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20  ->jfd);.        
12a8c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
12a8d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
12a8e 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75     /* If the jou
12a8f 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65 78  rnal does not ex
12a90 69 73 74 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  ist, that means 
12a91 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65  some other proce
12a92 73 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  ss.            *
12a93 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 72 6f  * has already ro
12a94 6c 6c 65 64 20 69 74 20 62 61 63 6b 20 2a 2f 0a  lled it back */.
12a95 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
12a96 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
12a97 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
12a98 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
12a99 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12a9a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
12a9b 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  oto failed;.    
12a9c 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72    }.      pPager
12a9d 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 3d 20  ->journalOpen = 
12a9e 31 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  1;.      pPager-
12a9f 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20  >journalStarted 
12aa0 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65  = 0;.      pPage
12aa1 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
12aa2 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  0;.      pPager-
12aa3 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a  >setMaster = 0;.
12aa4 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
12aa5 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 0a  urnalHdr = 0;. .
12aa6 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 63        /* Playbac
12aa7 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65  k and delete the
12aa8 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 20   journal.  Drop 
12aa9 74 68 65 20 64 61 74 61 62 61 73 65 20 77 72 69  the database wri
12aaa 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b  te.      ** lock
12aab 20 61 6e 64 20 72 65 61 63 71 75 69 72 65 20 74   and reacquire t
12aac 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 75  he read lock. Pu
12aad 72 67 65 20 74 68 65 20 63 61 63 68 65 20 62 65  rge the cache be
12aae 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 6c  fore.      ** pl
12aaf 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20 68  aying back the h
12ab0 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 68  ot-journal so th
12ab1 61 74 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 20  at we don't end 
12ab2 75 70 20 77 69 74 68 0a 20 20 20 20 20 20 2a 2a  up with.      **
12ab3 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74   an inconsistent
12ab4 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2f   cache..      */
12ab5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63  .      sqlite3Pc
12ab6 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 65 72  acheClear(pPager
12ab7 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20  ->pPCache);.    
12ab8 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
12ab9 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29  yback(pPager, 1)
12aba 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
12abb 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
12abc 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
12abd 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63  error(pPager, rc
12abe 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  );.        goto 
12abf 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a  failed;.      }.
12ac0 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61        assert(pPa
12ac1 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45  ger->state==PAGE
12ac2 52 5f 53 48 41 52 45 44 20 7c 7c 20 0a 20 20 20  R_SHARED || .   
12ac3 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e         (pPager->
12ac4 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26  exclusiveMode &&
12ac5 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50   pPager->state>P
12ac6 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20  AGER_SHARED).   
12ac7 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20     );.    }..   
12ac8 20 69 66 28 20 73 71 6c 69 74 65 33 50 63 61 63   if( sqlite3Pcac
12ac9 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  hePagecount(pPag
12aca 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29  er->pPCache)>0 )
12acb 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73  {.      /* The s
12acc 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a  hared-lock has j
12acd 75 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65  ust been acquire
12ace 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  d on the databas
12acf 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20  e file.      ** 
12ad0 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c  and there are al
12ad1 72 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74  ready pages in t
12ad2 68 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61  he cache (from a
12ad3 20 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20   previous.      
12ad4 2a 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  ** read or write
12ad5 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20   transaction).  
12ad6 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
12ad7 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20  the database.   
12ad8 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d     ** has been m
12ad9 6f 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65  odified.  If the
12ada 20 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68   database has ch
12adb 61 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65  anged, flush the
12adc 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e  .      ** cache.
12add 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
12ade 2a 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e  ** Database chan
12adf 67 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20  ges is detected 
12ae0 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35  by looking at 15
12ae1 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67   bytes beginning
12ae2 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66  .      ** at off
12ae3 73 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20  set 24 into the 
12ae4 66 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74  file.  The first
12ae5 20 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62   4 of these 16 b
12ae6 79 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a  ytes are.      *
12ae7 2a 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74  * a 32-bit count
12ae8 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65  er that is incre
12ae9 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68  mented with each
12aea 20 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20   change.  The.  
12aeb 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74      ** other byt
12aec 65 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d  es change random
12aed 6c 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c  ly with each fil
12aee 65 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20  e change when.  
12aef 20 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69      ** a codec i
12af0 73 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20  s in use..      
12af1 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65  ** .      ** The
12af2 72 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e  re is a vanishin
12af3 67 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65  gly small chance
12af4 20 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77   that a change w
12af5 69 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20  ill not be .    
12af6 20 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20    ** detected.  
12af7 54 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e  The chance of an
12af8 20 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e   undetected chan
12af9 67 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74  ge is so small t
12afa 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20  hat.      ** it 
12afb 63 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64  can be neglected
12afc 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
12afd 20 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73   char dbFileVers
12afe 5b 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e  [sizeof(pPager->
12aff 64 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20  dbFileVers)];.  
12b00 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
12b01 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
12b02 2c 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28  , 0);..      if(
12b03 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
12b04 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
12b05 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
12b06 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66  ;.        goto f
12b07 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a  ailed;.      }..
12b08 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
12b09 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69  ager->dbSizeVali
12b0a 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  d );.      if( p
12b0b 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20  Pager->dbSize>0 
12b0c 29 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41  ){.        IOTRA
12b0d 43 45 28 28 22 43 4b 56 45 52 53 20 25 70 20 25  CE(("CKVERS %p %
12b0e 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 69  d\n", pPager, si
12b0f 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29  zeof(dbFileVers)
12b10 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ));.        rc =
12b11 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70   sqlite3OsRead(p
12b12 50 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 69  Pager->fd, &dbFi
12b13 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64  leVers, sizeof(d
12b14 62 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29 3b  bFileVers), 24);
12b15 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
12b16 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12b17 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69          goto fai
12b18 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  led;.        }. 
12b19 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
12b1a 20 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c      memset(dbFil
12b1b 65 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66  eVers, 0, sizeof
12b1c 28 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20  (dbFileVers));. 
12b1d 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66       }..      if
12b1e 28 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d  ( memcmp(pPager-
12b1f 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46  >dbFileVers, dbF
12b20 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28  ileVers, sizeof(
12b21 64 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20  dbFileVers))!=0 
12b22 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72  ){.        pager
12b23 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a  _reset(pPager);.
12b24 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
12b25 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12b26 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
12b27 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  || pPager->state
12b28 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29  ==PAGER_SHARED )
12b29 3b 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a  ;.  }.. failed:.
12b2a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12b2b 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61  _OK ){.    /* pa
12b2c 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20  ger_unlock() is 
12b2d 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c  a no-op for excl
12b2e 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69  usive mode and i
12b2f 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
12b30 65 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 72  es. */.    pager
12b31 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b  _unlock(pPager);
12b32 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
12b33 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  ;.}../*.** Make 
12b34 73 75 72 65 20 77 65 20 68 61 76 65 20 74 68 65  sure we have the
12b35 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 61 20 70   content for a p
12b36 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61 67  age.  If the pag
12b37 65 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75  e was.** previou
12b38 73 6c 79 20 61 63 71 75 69 72 65 64 20 77 69 74  sly acquired wit
12b39 68 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20  h noContent==1, 
12b3a 74 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74  then the content
12b3b 20 77 61 73 0a 2a 2a 20 6a 75 73 74 20 69 6e 69   was.** just ini
12b3c 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f  tialized to zero
12b3d 73 20 69 6e 73 74 65 61 64 20 6f 66 20 62 65 69  s instead of bei
12b3e 6e 67 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73  ng read from dis
12b3f 6b 2e 0a 2a 2a 20 42 75 74 20 6e 6f 77 20 77 65  k..** But now we
12b40 20 6e 65 65 64 20 74 68 65 20 72 65 61 6c 20 64   need the real d
12b41 61 74 61 20 6f 66 66 20 6f 66 20 64 69 73 6b 2e  ata off of disk.
12b42 20 20 53 6f 20 6d 61 6b 65 20 73 75 72 65 20 77    So make sure w
12b43 65 0a 2a 2a 20 68 61 76 65 20 69 74 2e 20 20 52  e.** have it.  R
12b44 65 61 64 20 69 74 20 69 6e 20 69 66 20 77 65 20  ead it in if we 
12b45 64 6f 20 6e 6f 74 20 68 61 76 65 20 69 74 20 61  do not have it a
12b46 6c 72 65 61 64 79 2e 0a 2a 2f 0a 73 74 61 74 69  lready..*/.stati
12b47 63 20 69 6e 74 20 70 61 67 65 72 5f 67 65 74 5f  c int pager_get_
12b48 63 6f 6e 74 65 6e 74 28 50 67 48 64 72 20 2a 70  content(PgHdr *p
12b49 50 67 29 7b 0a 20 20 69 66 28 20 70 50 67 2d 3e  Pg){.  if( pPg->
12b4a 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
12b4b 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 69 6e 74  _READ ){.    int
12b4c 20 72 63 20 3d 20 72 65 61 64 44 62 50 61 67 65   rc = readDbPage
12b4d 28 70 50 67 2d 3e 70 50 61 67 65 72 2c 20 70 50  (pPg->pPager, pP
12b4e 67 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20  g, pPg->pgno);. 
12b4f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
12b50 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50  E_OK ){.      pP
12b51 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48  g->flags &= ~PGH
12b52 44 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20  DR_NEED_READ;.  
12b53 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
12b54 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
12b55 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
12b56 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
12b57 20 49 66 20 74 68 65 20 72 65 66 65 72 65 6e 63   If the referenc
12b58 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65 61 63  e count has reac
12b59 68 65 64 20 7a 65 72 6f 2c 20 61 6e 64 20 74 68  hed zero, and th
12b5a 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69  e pager is not i
12b5b 6e 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20  n the.** middle 
12b5c 6f 66 20 61 20 77 72 69 74 65 20 74 72 61 6e 73  of a write trans
12b5d 61 63 74 69 6f 6e 20 6f 72 20 6f 70 65 6e 65 64  action or opened
12b5e 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
12b5f 64 65 2c 20 75 6e 6c 6f 63 6b 20 69 74 2e 0a 2a  de, unlock it..*
12b60 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  / .static void p
12b61 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73  agerUnlockIfUnus
12b62 65 64 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ed(Pager *pPager
12b63 29 7b 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65  ){.  if( (sqlite
12b64 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
12b65 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
12b66 3d 3d 30 29 0a 20 20 20 20 26 26 20 28 21 70 50  ==0).    && (!pP
12b67 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
12b68 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a  ode || pPager->j
12b69 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 0a 20 20  ournalOff>0) .  
12b6a 29 7b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f  ){.    pagerUnlo
12b6b 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50  ckAndRollback(pP
12b6c 61 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ager);.  }.}../*
12b6d 0a 2a 2a 20 44 72 6f 70 20 61 20 70 61 67 65 20  .** Drop a page 
12b6e 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75  from the cache u
12b6f 73 69 6e 67 20 73 71 6c 69 74 65 33 50 63 61 63  sing sqlite3Pcac
12b70 68 65 44 72 6f 70 28 29 2e 0a 2a 2a 0a 2a 2a 20  heDrop()..**.** 
12b71 49 66 20 74 68 69 73 20 6d 65 61 6e 73 20 74 68  If this means th
12b72 65 72 65 20 61 72 65 20 6e 6f 77 20 6e 6f 20 70  ere are now no p
12b73 61 67 65 73 20 77 69 74 68 20 72 65 66 65 72 65  ages with refere
12b74 6e 63 65 73 20 74 6f 20 74 68 65 6d 2c 20 61 20  nces to them, a 
12b75 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75  rollback.** occu
12b76 72 73 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  rs and the lock 
12b77 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
12b78 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73  is removed..*/.s
12b79 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72  tatic void pager
12b7a 44 72 6f 70 50 61 67 65 28 44 62 50 61 67 65 20  DropPage(DbPage 
12b7b 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a  *pPg){.  Pager *
12b7c 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50  pPager = pPg->pP
12b7d 61 67 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 50  ager;.  sqlite3P
12b7e 63 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b 0a  cacheDrop(pPg);.
12b7f 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55    pagerUnlockIfU
12b80 6e 75 73 65 64 28 70 50 61 67 65 72 29 3b 0a 7d  nused(pPager);.}
12b81 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20  ../*.** Acquire 
12b82 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 20  a page..**.** A 
12b83 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  read lock on the
12b84 20 64 69 73 6b 20 66 69 6c 65 20 69 73 20 6f 62   disk file is ob
12b85 74 61 69 6e 65 64 20 77 68 65 6e 20 74 68 65 20  tained when the 
12b86 66 69 72 73 74 20 70 61 67 65 20 69 73 20 61 63  first page is ac
12b87 71 75 69 72 65 64 2e 20 0a 2a 2a 20 54 68 69 73  quired. .** This
12b88 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 64 72   read lock is dr
12b89 6f 70 70 65 64 20 77 68 65 6e 20 74 68 65 20 6c  opped when the l
12b8a 61 73 74 20 70 61 67 65 20 69 73 20 72 65 6c 65  ast page is rele
12b8b 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ased..**.** This
12b8c 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 66   routine works f
12b8d 6f 72 20 61 6e 79 20 70 61 67 65 20 6e 75 6d 62  or any page numb
12b8e 65 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  er greater than 
12b8f 30 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62  0.  If the datab
12b90 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 73  ase.** file is s
12b91 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20  maller than the 
12b92 72 65 71 75 65 73 74 65 64 20 70 61 67 65 2c 20  requested page, 
12b93 74 68 65 6e 20 6e 6f 20 61 63 74 75 61 6c 20 64  then no actual d
12b94 69 73 6b 0a 2a 2a 20 72 65 61 64 20 6f 63 63 75  isk.** read occu
12b95 72 73 20 61 6e 64 20 74 68 65 20 6d 65 6d 6f 72  rs and the memor
12b96 79 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 70  y image of the p
12b97 61 67 65 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  age is initializ
12b98 65 64 20 74 6f 0a 2a 2a 20 61 6c 6c 20 7a 65 72  ed to.** all zer
12b99 6f 73 2e 20 20 54 68 65 20 65 78 74 72 61 20 64  os.  The extra d
12b9a 61 74 61 20 61 70 70 65 6e 64 65 64 20 74 6f 20  ata appended to 
12b9b 61 20 70 61 67 65 20 69 73 20 61 6c 77 61 79 73  a page is always
12b9c 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20   initialized.** 
12b9d 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 66 69 72  to zeros the fir
12b9e 73 74 20 74 69 6d 65 20 61 20 70 61 67 65 20 69  st time a page i
12b9f 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65  s loaded into me
12ba0 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  mory..**.** The 
12ba1 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68  acquisition migh
12ba2 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72  t fail for sever
12ba3 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20  al reasons.  In 
12ba4 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e  all cases,.** an
12ba5 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72   appropriate err
12ba6 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
12ba7 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20  ned and *ppPage 
12ba8 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a  is set to NULL..
12ba9 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73  **.** See also s
12baa 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
12bab 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20  p().  Both this 
12bac 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b  routine and Look
12bad 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20  up() attempt.** 
12bae 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20 69  to find a page i
12baf 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  n the in-memory 
12bb0 63 61 63 68 65 20 66 69 72 73 74 2e 20 20 49 66  cache first.  If
12bb1 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
12bb2 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d   already.** in m
12bb3 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74  emory, this rout
12bb4 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b  ine goes to disk
12bb5 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77   to read it in w
12bb6 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a  hereas Lookup().
12bb7 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20  ** just returns 
12bb8 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  0.  This routine
12bb9 20 61 63 71 75 69 72 65 73 20 61 20 72 65 61 64   acquires a read
12bba 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20  -lock the first 
12bbb 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74  time it.** has t
12bbc 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e  o go to disk, an
12bbd 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61  d could also pla
12bbe 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75  yback an old jou
12bbf 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72  rnal if necessar
12bc0 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b  y..** Since Look
12bc1 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20  up() never goes 
12bc2 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65  to disk, it neve
12bc3 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69  r has to deal wi
12bc4 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a  th locks.** or j
12bc5 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a  ournal files..**
12bc6 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74  .** If noContent
12bc7 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 20 70   is false, the p
12bc8 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 65  age contents are
12bc9 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 20 66   actually read f
12bca 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 20 49 66 20  rom disk..** If 
12bcb 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75  noContent is tru
12bcc 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  e, it means that
12bcd 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20   we do not care 
12bce 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e  about the conten
12bcf 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67  ts.** of the pag
12bd0 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2c 20  e at this time, 
12bd1 73 6f 20 64 6f 20 6e 6f 74 20 64 6f 20 61 20 64  so do not do a d
12bd2 69 73 6b 20 72 65 61 64 2e 20 20 4a 75 73 74 20  isk read.  Just 
12bd3 66 69 6c 6c 20 69 6e 20 74 68 65 0a 2a 2a 20 70  fill in the.** p
12bd4 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68  age content with
12bd5 20 7a 65 72 6f 73 2e 20 20 42 75 74 20 6d 61 72   zeros.  But mar
12bd6 6b 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20  k the fact that 
12bd7 77 65 20 68 61 76 65 20 6e 6f 74 20 72 65 61 64  we have not read
12bd8 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20   the.** content 
12bd9 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50  by setting the P
12bda 67 48 64 72 2e 6e 65 65 64 52 65 61 64 20 66 6c  gHdr.needRead fl
12bdb 61 67 2e 20 20 4c 61 74 65 72 20 6f 6e 2c 20 69  ag.  Later on, i
12bdc 66 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67  f .** sqlite3Pag
12bdd 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c  erWrite() is cal
12bde 6c 65 64 20 6f 6e 20 74 68 69 73 20 70 61 67 65  led on this page
12bdf 20 6f 72 20 69 66 20 74 68 69 73 20 72 6f 75 74   or if this rout
12be0 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64  ine is.** called
12be1 20 61 67 61 69 6e 20 77 69 74 68 20 6e 6f 43 6f   again with noCo
12be2 6e 74 65 6e 74 3d 3d 30 2c 20 74 68 61 74 20 6d  ntent==0, that m
12be3 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f  eans that the co
12be4 6e 74 65 6e 74 20 69 73 20 6e 65 65 64 65 64 0a  ntent is needed.
12be5 2a 2a 20 61 6e 64 20 74 68 65 20 64 69 73 6b 20  ** and the disk 
12be6 72 65 61 64 20 73 68 6f 75 6c 64 20 6f 63 63 75  read should occu
12be7 72 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e  r at that point.
12be8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
12be9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
12bea 67 65 72 41 63 71 75 69 72 65 28 0a 20 20 50 61  gerAcquire(.  Pa
12beb 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20  ger *pPager,    
12bec 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f    /* The pager o
12bed 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62  pen on the datab
12bee 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67  ase file */.  Pg
12bef 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20  no pgno,        
12bf0 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
12bf1 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44   to fetch */.  D
12bf2 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20  bPage **ppPage, 
12bf3 20 20 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f     /* Write a po
12bf4 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67  inter to the pag
12bf5 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  e here */.  int 
12bf6 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20  noContent       
12bf7 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72  /* Do not bother
12bf8 20 72 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74   reading content
12bf9 20 66 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72   from disk if tr
12bfa 75 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72  ue */.){.  PgHdr
12bfb 20 2a 70 50 67 20 3d 20 30 3b 0a 20 20 69 6e 74   *pPg = 0;.  int
12bfc 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
12bfd 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
12bfe 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 0a 20 20 20  AGER_UNLOCK .   
12bff 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 63      || sqlite3Pc
12c00 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61  acheRefCount(pPa
12c01 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20  ger->pPCache)>0 
12c02 0a 20 20 20 20 20 20 20 7c 7c 20 70 67 6e 6f 3d  .       || pgno=
12c03 3d 31 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  =1.  );..  /* Th
12c04 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 6e  e maximum page n
12c05 75 6d 62 65 72 20 69 73 20 32 5e 33 31 2e 20 52  umber is 2^31. R
12c06 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
12c07 52 55 50 54 20 69 66 20 61 20 70 61 67 65 0a 20  RUPT if a page. 
12c08 20 2a 2a 20 6e 75 6d 62 65 72 20 67 72 65 61 74   ** number great
12c09 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20 6f 72  er than this, or
12c0a 20 7a 65 72 6f 2c 20 69 73 20 72 65 71 75 65 73   zero, is reques
12c0b 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ted..  */.  if( 
12c0c 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50  pgno>PAGER_MAX_P
12c0d 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 30 20 7c  GNO || pgno==0 |
12c0e 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a  | pgno==PAGER_MJ
12c0f 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b  _PGNO(pPager) ){
12c10 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
12c11 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
12c12 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
12c13 73 75 72 65 20 77 65 20 68 61 76 65 20 6e 6f 74  sure we have not
12c14 20 68 69 74 20 61 6e 79 20 63 72 69 74 69 63 61   hit any critica
12c15 6c 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 20 0a  l errors..  */ .
12c16 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12c17 21 3d 30 20 29 3b 0a 20 20 2a 70 70 50 61 67 65  !=0 );.  *ppPage
12c18 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 74   = 0;..  /* If t
12c19 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74  his is the first
12c1a 20 70 61 67 65 20 61 63 63 65 73 73 65 64 2c 20   page accessed, 
12c1b 74 68 65 6e 20 67 65 74 20 61 20 53 48 41 52 45  then get a SHARE
12c1c 44 20 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74  D lock.  ** on t
12c1d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12c1e 2e 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f 63  . pagerSharedLoc
12c1f 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  k() is a no-op i
12c20 66 20 0a 20 20 2a 2a 20 61 20 64 61 74 61 62 61  f .  ** a databa
12c21 73 65 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61  se lock is alrea
12c22 64 79 20 68 65 6c 64 2e 0a 20 20 2a 2f 0a 20 20  dy held..  */.  
12c23 72 63 20 3d 20 70 61 67 65 72 53 68 61 72 65 64  rc = pagerShared
12c24 4c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  Lock(pPager);.  
12c25 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12c26 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
12c27 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rc;.  }.  assert
12c28 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21  ( pPager->state!
12c29 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b  =PAGER_UNLOCK );
12c2a 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
12c2b 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67  PcacheFetch(pPag
12c2c 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e  er->pPCache, pgn
12c2d 6f 2c 20 31 2c 20 26 70 50 67 29 3b 0a 20 20 69  o, 1, &pPg);.  i
12c2e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
12c2f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
12c30 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 67  c;.  }.  if( pPg
12c31 2d 3e 70 50 61 67 65 72 3d 3d 30 20 29 7b 0a 20  ->pPager==0 ){. 
12c32 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20     /* The pager 
12c33 63 61 63 68 65 20 68 61 73 20 63 72 65 61 74 65  cache has create
12c34 64 20 61 20 6e 65 77 20 70 61 67 65 2e 20 49 74  d a new page. It
12c35 73 20 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 20  s content needs 
12c36 74 6f 20 0a 20 20 20 20 2a 2a 20 62 65 20 69 6e  to .    ** be in
12c37 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 20 20 2a  itialized..    *
12c38 2f 0a 20 20 20 20 69 6e 74 20 6e 4d 61 78 3b 0a  /.    int nMax;.
12c39 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70      PAGER_INCR(p
12c3a 50 61 67 65 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20  Pager->nMiss);. 
12c3b 20 20 20 70 50 67 2d 3e 70 50 61 67 65 72 20 3d     pPg->pPager =
12c3c 20 70 50 61 67 65 72 3b 0a 20 20 20 20 6d 65 6d   pPager;.    mem
12c3d 73 65 74 28 70 50 67 2d 3e 70 45 78 74 72 61 2c  set(pPg->pExtra,
12c3e 20 30 2c 20 70 50 61 67 65 72 2d 3e 6e 45 78 74   0, pPager->nExt
12c3f 72 61 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73  ra);..    rc = s
12c40 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
12c41 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 4d  ount(pPager, &nM
12c42 61 78 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ax);.    if( rc!
12c43 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12c44 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
12c45 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20  Unref(pPg);.    
12c46 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
12c47 20 7d 0a 0a 20 20 20 20 69 66 28 20 6e 4d 61 78   }..    if( nMax
12c48 3c 28 69 6e 74 29 70 67 6e 6f 20 7c 7c 20 4d 45  <(int)pgno || ME
12c49 4d 44 42 20 7c 7c 20 6e 6f 43 6f 6e 74 65 6e 74  MDB || noContent
12c4a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67   ){.      if( pg
12c4b 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e  no>pPager->mxPgn
12c4c 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  o ){.        sql
12c4d 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
12c4e 50 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  Pg);.        ret
12c4f 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  urn SQLITE_FULL;
12c50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
12c51 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61  emset(pPg->pData
12c52 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  , 0, pPager->pag
12c53 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66  eSize);.      if
12c54 28 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20  ( noContent ){. 
12c55 20 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67         pPg->flag
12c56 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f  s |= PGHDR_NEED_
12c57 52 45 41 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20  READ;.      }.  
12c58 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 5a 45      IOTRACE(("ZE
12c59 52 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61  RO %p %d\n", pPa
12c5a 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20  ger, pgno));.   
12c5b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
12c5c 20 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 50   = readDbPage(pP
12c5d 61 67 65 72 2c 20 70 50 67 2c 20 70 67 6e 6f 29  ager, pPg, pgno)
12c5e 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
12c5f 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
12c60 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  =SQLITE_IOERR_SH
12c61 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20  ORT_READ ){.    
12c62 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 50 61      /* sqlite3Pa
12c63 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b 20 2a  gerUnref(pPg); *
12c64 2f 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 44  /.        pagerD
12c65 72 6f 70 50 61 67 65 28 70 50 67 29 3b 0a 20 20  ropPage(pPg);.  
12c66 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
12c67 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
12c68 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45  ifdef SQLITE_CHE
12c69 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67  CK_PAGES.    pPg
12c6a 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67  ->pageHash = pag
12c6b 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29  er_pagehash(pPg)
12c6c 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65  ;.#endif.  }else
12c6d 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71  {.    /* The req
12c6e 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 69  uested page is i
12c6f 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
12c70 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
12c71 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
12c72 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50  Count(pPager->pP
12c73 43 61 63 68 65 29 3e 30 20 7c 7c 20 70 67 6e 6f  Cache)>0 || pgno
12c74 3d 3d 31 29 3b 0a 20 20 20 20 50 41 47 45 52 5f  ==1);.    PAGER_
12c75 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 48 69  INCR(pPager->nHi
12c76 74 29 3b 0a 20 20 20 20 69 66 28 20 21 6e 6f 43  t);.    if( !noC
12c77 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  ontent ){.      
12c78 72 63 20 3d 20 70 61 67 65 72 5f 67 65 74 5f 63  rc = pager_get_c
12c79 6f 6e 74 65 6e 74 28 70 50 67 29 3b 0a 20 20 20  ontent(pPg);.   
12c7a 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
12c7b 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
12c7c 72 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20  rUnref(pPg);.   
12c7d 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
12c7e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
12c7f 7d 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70  }..  *ppPage = p
12c80 50 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  Pg;.  return SQL
12c81 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
12c82 20 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20   Acquire a page 
12c83 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79  if it is already
12c84 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   in the in-memor
12c85 79 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20  y cache.  Do.** 
12c86 6e 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67  not read the pag
12c87 65 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65  e from disk.  Re
12c88 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
12c89 6f 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f  o the page,.** o
12c8a 72 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20  r 0 if the page 
12c8b 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e  is not in cache.
12c8c 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
12c8d 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
12c8e 29 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e  ).  The differen
12c8f 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20  ce between this 
12c90 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73  routine.** and s
12c91 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
12c92 20 69 73 20 74 68 61 74 20 5f 67 65 74 28 29 20   is that _get() 
12c93 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64  will go to the d
12c94 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20  isk and read.** 
12c95 69 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74  in the page if t
12c96 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
12c97 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e  lready in cache.
12c98 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a    This routine.*
12c99 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69  * returns NULL i
12c9a 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  f the page is no
12c9b 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66  t in cache or if
12c9c 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f   a disk I/O erro
12c9d 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68  r .** has ever h
12c9e 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  appened..*/.SQLI
12c9f 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67  TE_PRIVATE DbPag
12ca0 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c  e *sqlite3PagerL
12ca1 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61  ookup(Pager *pPa
12ca2 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b  ger, Pgno pgno){
12ca3 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20  .  PgHdr *pPg = 
12ca4 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  0;.  assert( pPa
12ca5 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ger!=0 );.  asse
12ca6 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 0a  rt( pgno!=0 );..
12ca7 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 73    if( (pPager->s
12ca8 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate!=PAGER_UNLO
12ca9 43 4b 29 0a 20 20 20 26 26 20 28 70 50 61 67 65  CK).   && (pPage
12caa 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49  r->errCode==SQLI
12cab 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d  TE_OK || pPager-
12cac 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45  >errCode==SQLITE
12cad 5f 46 55 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20  _FULL).  ){.    
12cae 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
12caf 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ch(pPager->pPCac
12cb0 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50  he, pgno, 0, &pP
12cb1 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  g);.  }..  retur
12cb2 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n pPg;.}../*.** 
12cb3 52 65 6c 65 61 73 65 20 61 20 70 61 67 65 2e 0a  Release a page..
12cb4 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d  **.** If the num
12cb5 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
12cb6 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72  s to the page dr
12cb7 6f 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e  op to zero, then
12cb8 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20   the.** page is 
12cb9 61 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55  added to the LRU
12cba 20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c   list.  When all
12cbb 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61   references to a
12cbc 6c 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20  ll pages.** are 
12cbd 72 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c  released, a roll
12cbe 62 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20  back occurs and 
12cbf 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  the lock on the 
12cc0 64 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72  database is.** r
12cc1 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  emoved..*/.SQLIT
12cc2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
12cc3 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
12cc4 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
12cc5 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50  if( pPg ){.    P
12cc6 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
12cc7 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20  Pg->pPager;.    
12cc8 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c  sqlite3PcacheRel
12cc9 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70  ease(pPg);.    p
12cca 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73  agerUnlockIfUnus
12ccb 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  ed(pPager);.  }.
12ccc 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12ccd 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  OK;.}../*.** If 
12cce 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
12ccf 20 66 69 6c 65 20 68 61 73 20 61 6c 72 65 61 64   file has alread
12cd0 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 65  y been opened, e
12cd1 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a  nsure that the.*
12cd2 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69  * sub-journal fi
12cd3 6c 65 20 69 73 20 6f 70 65 6e 20 74 6f 6f 2e 20  le is open too. 
12cd4 49 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  If the main jour
12cd5 6e 61 6c 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c  nal is not open,
12cd6 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
12cd7 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  n is a no-op..**
12cd8 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
12cd9 20 72 65 74 75 72 6e 65 64 20 69 66 20 65 76 65   returned if eve
12cda 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61 63 63  rything goes acc
12cdb 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20  ording to plan. 
12cdc 41 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f  An .** SQLITE_IO
12cdd 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f  ERR_XXX error co
12cde 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  de is returned i
12cdf 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 0a 2a  f the call to .*
12ce0 2a 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  * sqlite3OsOpen(
12ce1 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74  ) fails..*/.stat
12ce2 69 63 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f  ic int openSubJo
12ce3 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61  urnal(Pager *pPa
12ce4 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ger){.  int rc =
12ce5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
12ce6 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
12ce7 6c 4f 70 65 6e 20 26 26 20 21 70 50 61 67 65 72  lOpen && !pPager
12ce8 2d 3e 73 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 73  ->sjfd->pMethods
12ce9 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67   ){.    if( pPag
12cea 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
12ceb 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
12cec 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20  DE_MEMORY ){.   
12ced 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75     sqlite3MemJou
12cee 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d  rnalOpen(pPager-
12cef 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73  >sjfd);.    }els
12cf0 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
12cf1 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65  lite3PagerOpente
12cf2 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65  mp(pPager, pPage
12cf3 72 2d 3e 73 6a 66 64 2c 20 53 51 4c 49 54 45 5f  r->sjfd, SQLITE_
12cf4 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 29  OPEN_SUBJOURNAL)
12cf5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
12cf6 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
12cf7 2a 20 43 72 65 61 74 65 20 61 20 6a 6f 75 72 6e  * Create a journ
12cf8 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 50 61 67  al file for pPag
12cf9 65 72 2e 20 20 54 68 65 72 65 20 73 68 6f 75 6c  er.  There shoul
12cfa 64 20 61 6c 72 65 61 64 79 20 62 65 20 61 20 52  d already be a R
12cfb 45 53 45 52 56 45 44 0a 2a 2a 20 6f 72 20 45 58  ESERVED.** or EX
12cfc 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
12cfd 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12cfe 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  e when this rout
12cff 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ine is called..*
12d00 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
12d01 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68  TE_OK if everyth
12d02 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 61 6e 20  ing.  Return an 
12d03 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 72  error code and r
12d04 65 6c 65 61 73 65 20 74 68 65 0a 2a 2a 20 77 72  elease the.** wr
12d05 69 74 65 20 6c 6f 63 6b 20 69 66 20 61 6e 79 74  ite lock if anyt
12d06 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
12d07 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
12d08 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61  ager_open_journa
12d09 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  l(Pager *pPager)
12d0a 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
12d0b 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e  *pVfs = pPager->
12d0c 70 56 66 73 3b 0a 20 20 69 6e 74 20 66 6c 61 67  pVfs;.  int flag
12d0d 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e  s = (SQLITE_OPEN
12d0e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
12d0f 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
12d10 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45  |SQLITE_OPEN_CRE
12d11 41 54 45 29 3b 0a 0a 20 20 69 6e 74 20 72 63 3b  ATE);..  int rc;
12d12 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
12d13 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
12d14 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 61 73  RESERVED );.  as
12d15 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73  sert( pPager->us
12d16 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73  eJournal );.  as
12d17 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49  sert( pPager->pI
12d18 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20  nJournal==0 );. 
12d19 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
12d1a 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30  ecount(pPager, 0
12d1b 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 6e  );.  pPager->pIn
12d1c 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  Journal = sqlite
12d1d 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 50  3BitvecCreate(pP
12d1e 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20  ager->dbSize);. 
12d1f 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e   if( pPager->pIn
12d20 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20  Journal==0 ){.  
12d21 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
12d22 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 66 61  MEM;.    goto fa
12d23 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75  iled_to_open_jou
12d24 72 6e 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  rnal;.  }..  if(
12d25 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12d26 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Open==0 ){.    i
12d27 66 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  f( pPager->tempF
12d28 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 66 6c 61  ile ){.      fla
12d29 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f 4f 50  gs |= (SQLITE_OP
12d2a 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
12d2b 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d  |SQLITE_OPEN_TEM
12d2c 50 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20  P_JOURNAL);.    
12d2d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c 61  }else{.      fla
12d2e 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f 4f 50  gs |= (SQLITE_OP
12d2f 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29  EN_MAIN_JOURNAL)
12d30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
12d31 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
12d32 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
12d33 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b  ALMODE_MEMORY ){
12d34 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  .      sqlite3Me
12d35 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61  mJournalOpen(pPa
12d36 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20  ger->jfd);.     
12d37 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
12d38 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69 66 64  .    }else{.#ifd
12d39 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
12d3a 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20  _ATOMIC_WRITE.  
12d3b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12d3c 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 20  JournalOpen(.   
12d3d 20 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 61         pVfs, pPa
12d3e 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70  ger->zJournal, p
12d3f 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67  Pager->jfd, flag
12d40 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a  s, jrnlBufferSiz
12d41 65 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 20  e(pPager).      
12d42 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 72  );.#else.      r
12d43 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
12d44 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  n(pVfs, pPager->
12d45 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72  zJournal, pPager
12d46 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29  ->jfd, flags, 0)
12d47 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20  ;.#endif.    }. 
12d48 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
12d49 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67  QLITE_OK || pPag
12d4a 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f 64  er->jfd->pMethod
12d4b 73 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  s );.    pPager-
12d4c 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b  >journalOff = 0;
12d4d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74  .    pPager->set
12d4e 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
12d4f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
12d50 64 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  dr = 0;.    if( 
12d51 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
12d52 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
12d53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
12d54 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73         sqlite3Os
12d55 44 65 6c 65 74 65 28 70 56 66 73 2c 20 70 50 61  Delete(pVfs, pPa
12d56 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30  ger->zJournal, 0
12d57 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
12d58 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f 74 6f 5f   goto failed_to_
12d59 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a 20 20  open_journal;.  
12d5a 20 20 7d 0a 20 20 7d 0a 20 20 70 50 61 67 65 72    }.  }.  pPager
12d5b 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 3d 20  ->journalOpen = 
12d5c 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  1;.  pPager->jou
12d5d 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b  rnalStarted = 0;
12d5e 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  .  pPager->needS
12d5f 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 67 65  ync = 0;.  pPage
12d60 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 69  r->nRec = 0;.  i
12d61 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
12d62 64 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  de ){.    rc = p
12d63 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
12d64 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f      goto failed_
12d65 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b  to_open_journal;
12d66 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 64  .  }.  pPager->d
12d67 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 50 61 67  bOrigSize = pPag
12d68 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 0a 20 20 72  er->dbSize;..  r
12d69 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c  c = writeJournal
12d6a 48 64 72 28 70 50 61 67 65 72 29 3b 0a 0a 20 20  Hdr(pPager);..  
12d6b 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76  if( pPager->nSav
12d6c 65 70 6f 69 6e 74 20 26 26 20 72 63 3d 3d 53 51  epoint && rc==SQ
12d6d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
12d6e 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e  c = openSubJourn
12d6f 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  al(pPager);.  }.
12d70 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12d71 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54  _OK && rc!=SQLIT
12d72 45 5f 4e 4f 4d 45 4d 20 26 26 20 72 63 21 3d 53  E_NOMEM && rc!=S
12d73 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
12d74 4d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61  M ){.    rc = pa
12d75 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74  ger_end_transact
12d76 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  ion(pPager, 0);.
12d77 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
12d78 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
12d79 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  c = SQLITE_FULL;
12d7a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
12d7b 75 72 6e 20 72 63 3b 0a 0a 66 61 69 6c 65 64 5f  urn rc;..failed_
12d7c 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3a  to_open_journal:
12d7d 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  .  sqlite3Bitvec
12d7e 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e  Destroy(pPager->
12d7f 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 70  pInJournal);.  p
12d80 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
12d81 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  l = 0;.  return 
12d82 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71  rc;.}../*.** Acq
12d83 75 69 72 65 20 61 20 77 72 69 74 65 2d 6c 6f 63  uire a write-loc
12d84 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
12d85 65 2e 20 20 54 68 65 20 6c 6f 63 6b 20 69 73 20  e.  The lock is 
12d86 72 65 6d 6f 76 65 64 20 77 68 65 6e 0a 2a 2a 20  removed when.** 
12d87 74 68 65 20 61 6e 79 20 6f 66 20 74 68 65 20 66  the any of the f
12d88 6f 6c 6c 6f 77 69 6e 67 20 68 61 70 70 65 6e 3a  ollowing happen:
12d89 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69  .**.**   *  sqli
12d8a 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68  te3PagerCommitPh
12d8b 61 73 65 54 77 6f 28 29 20 69 73 20 63 61 6c 6c  aseTwo() is call
12d8c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69  ed..**   *  sqli
12d8d 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
12d8e 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  () is called..**
12d8f 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50 61 67     *  sqlite3Pag
12d90 65 72 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c  erClose() is cal
12d91 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71 6c  led..**   *  sql
12d92 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29  ite3PagerUnref()
12d93 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 6f 6e   is called to on
12d94 20 65 76 65 72 79 20 6f 75 74 73 74 61 6e 64 69   every outstandi
12d95 6e 67 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54  ng page..**.** T
12d96 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
12d97 65 72 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  er to this routi
12d98 6e 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ne is a pointer 
12d99 74 6f 20 61 6e 79 20 6f 70 65 6e 20 70 61 67 65  to any open page
12d9a 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   of the.** datab
12d9b 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f 74 68 69  ase file.  Nothi
12d9c 6e 67 20 63 68 61 6e 67 65 73 20 61 62 6f 75 74  ng changes about
12d9d 20 74 68 65 20 70 61 67 65 20 2d 20 69 74 20 69   the page - it i
12d9e 73 20 75 73 65 64 20 6d 65 72 65 6c 79 20 74 6f  s used merely to
12d9f 0a 2a 2a 20 61 63 71 75 69 72 65 20 61 20 70 6f  .** acquire a po
12da0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61 67  inter to the Pag
12da1 65 72 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  er structure and
12da2 20 61 73 20 70 72 6f 6f 66 20 74 68 61 74 20 74   as proof that t
12da3 68 65 72 65 20 69 73 0a 2a 2a 20 61 6c 72 65 61  here is.** alrea
12da4 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  dy a read-lock o
12da5 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  n the database..
12da6 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  **.** The second
12da7 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63   parameter indic
12da8 61 74 65 73 20 68 6f 77 20 6d 75 63 68 20 73 70  ates how much sp
12da9 61 63 65 20 69 6e 20 62 79 74 65 73 20 74 6f 20  ace in bytes to 
12daa 72 65 73 65 72 76 65 20 66 6f 72 20 61 0a 2a 2a  reserve for a.**
12dab 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
12dac 66 69 6c 65 2d 6e 61 6d 65 20 61 74 20 74 68 65  file-name at the
12dad 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f   start of the jo
12dae 75 72 6e 61 6c 20 77 68 65 6e 20 69 74 20 69 73  urnal when it is
12daf 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   created..**.** 
12db0 41 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  A journal file i
12db1 73 20 6f 70 65 6e 65 64 20 69 66 20 74 68 69 73  s opened if this
12db2 20 69 73 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72   is not a tempor
12db3 61 72 79 20 66 69 6c 65 2e 20 20 46 6f 72 20 74  ary file.  For t
12db4 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 66 69 6c 65  emporary.** file
12db5 73 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 6f  s, the opening o
12db6 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
12db7 6c 65 20 69 73 20 64 65 66 65 72 72 65 64 20 75  le is deferred u
12db8 6e 74 69 6c 20 74 68 65 72 65 20 69 73 20 61 6e  ntil there is an
12db9 0a 2a 2a 20 61 63 74 75 61 6c 20 6e 65 65 64 20  .** actual need 
12dba 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20  to write to the 
12dbb 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49  journal..**.** I
12dbc 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
12dbd 73 20 61 6c 72 65 61 64 79 20 72 65 73 65 72 76  s already reserv
12dbe 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 20  ed for writing, 
12dbf 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
12dc0 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49  a no-op..**.** I
12dc1 66 20 65 78 46 6c 61 67 20 69 73 20 74 72 75 65  f exFlag is true
12dc2 2c 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 67  , go ahead and g
12dc3 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  et an EXCLUSIVE 
12dc4 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65  lock on the file
12dc5 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  .** immediately 
12dc6 69 6e 73 74 65 61 64 20 6f 66 20 77 61 69 74 69  instead of waiti
12dc7 6e 67 20 75 6e 74 69 6c 20 77 65 20 74 72 79 20  ng until we try 
12dc8 74 6f 20 66 6c 75 73 68 20 74 68 65 20 63 61 63  to flush the cac
12dc9 68 65 2e 20 20 54 68 65 0a 2a 2a 20 65 78 46 6c  he.  The.** exFl
12dca 61 67 20 69 73 20 69 67 6e 6f 72 65 64 20 69 66  ag is ignored if
12dcb 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
12dcc 73 20 61 6c 72 65 61 64 79 20 61 63 74 69 76 65  s already active
12dcd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
12dce 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
12dcf 61 67 65 72 42 65 67 69 6e 28 44 62 50 61 67 65  agerBegin(DbPage
12dd0 20 2a 70 50 67 2c 20 69 6e 74 20 65 78 46 6c 61   *pPg, int exFla
12dd1 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  g){.  Pager *pPa
12dd2 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65  ger = pPg->pPage
12dd3 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  r;.  int rc = SQ
12dd4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72  LITE_OK;.  asser
12dd5 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29  t( pPg->nRef>0 )
12dd6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
12dd7 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52  er->state!=PAGER
12dd8 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28  _UNLOCK );.  if(
12dd9 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
12dda 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 7b 0a  PAGER_SHARED ){.
12ddb 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
12ddc 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d  er->pInJournal==
12ddd 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
12dde 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 72   !MEMDB );.    r
12ddf 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  c = sqlite3OsLoc
12de0 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45  k(pPager->fd, RE
12de1 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SERVED_LOCK);.  
12de2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
12de3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61  _OK ){.      pPa
12de4 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47  ger->state = PAG
12de5 45 52 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20  ER_RESERVED;.   
12de6 20 20 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b     if( exFlag ){
12de7 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61  .        rc = pa
12de8 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b  ger_wait_on_lock
12de9 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49  (pPager, EXCLUSI
12dea 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  VE_LOCK);.      
12deb 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
12dec 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
12ded 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
12dee 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67  ;.    }.    pPag
12def 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 3d  er->dirtyCache =
12df0 20 30 3b 0a 20 20 20 20 50 41 47 45 52 54 52 41   0;.    PAGERTRA
12df1 43 45 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e  CE(("TRANSACTION
12df2 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28   %d\n", PAGERID(
12df3 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 69  pPager)));.    i
12df4 66 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f  f( pPager->useJo
12df5 75 72 6e 61 6c 20 26 26 20 21 70 50 61 67 65 72  urnal && !pPager
12df6 2d 3e 74 65 6d 70 46 69 6c 65 0a 20 20 20 20 20  ->tempFile.     
12df7 20 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d        && pPager-
12df8 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41  >journalMode!=PA
12df9 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
12dfa 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 72 63 20  OFF ){.      rc 
12dfb 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75  = pager_open_jou
12dfc 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20  rnal(pPager);.  
12dfd 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
12dfe 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12dff 70 65 6e 20 26 26 20 70 50 61 67 65 72 2d 3e 6a  pen && pPager->j
12e00 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a  ournalOff==0 ){.
12e01 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70      /* This happ
12e02 65 6e 73 20 77 68 65 6e 20 74 68 65 20 70 61 67  ens when the pag
12e03 65 72 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73  er was in exclus
12e04 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20  ive-access mode 
12e05 74 68 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20  the last.    ** 
12e06 74 69 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20  time a (read or 
12e07 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69  write) transacti
12e08 6f 6e 20 77 61 73 20 73 75 63 63 65 73 73 66 75  on was successfu
12e09 6c 6c 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20  lly concluded.  
12e0a 20 20 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e    ** by this con
12e0b 6e 65 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64  nection. Instead
12e0c 20 6f 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65   of deleting the
12e0d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74   journal file it
12e0e 20 77 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70   was .    ** kep
12e0f 74 20 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65  t open and eithe
12e10 72 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 20  r was truncated 
12e11 74 6f 20 30 20 62 79 74 65 73 20 6f 72 20 69 74  to 0 bytes or it
12e12 73 20 68 65 61 64 65 72 20 77 61 73 0a 20 20 20  s header was.   
12e13 20 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20   ** overwritten 
12e14 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20  with zeros..    
12e15 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
12e16 50 61 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29  Pager->nRec==0 )
12e17 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
12e18 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
12e19 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
12e1a 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  t( pPager->pInJo
12e1b 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  urnal==0 );.    
12e1c 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
12e1d 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29  count(pPager, 0)
12e1e 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49  ;.    pPager->pI
12e1f 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74  nJournal = sqlit
12e20 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 20  e3BitvecCreate( 
12e21 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29  pPager->dbSize )
12e22 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  ;.    if( !pPage
12e23 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 7b  r->pInJournal ){
12e24 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
12e25 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
12e26 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 67 65  lse{.      pPage
12e27 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20  r->dbOrigSize = 
12e28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a  pPager->dbSize;.
12e29 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
12e2a 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65  JournalHdr(pPage
12e2b 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  r);.    }.  }.  
12e2c 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d  assert( !pPager-
12e2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 7c 7c 20  >journalOpen || 
12e2e 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12e2f 66 66 3e 30 20 7c 7c 20 72 63 21 3d 53 51 4c 49  ff>0 || rc!=SQLI
12e30 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 65 74 75 72  TE_OK );.  retur
12e31 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  n rc;.}../*.** M
12e32 61 72 6b 20 61 20 64 61 74 61 20 70 61 67 65 20  ark a data page 
12e33 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 20 54  as writeable.  T
12e34 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74  he page is writt
12e35 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  en into the jour
12e36 6e 61 6c 20 0a 2a 2a 20 69 66 20 69 74 20 69 73  nal .** if it is
12e37 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61   not there alrea
12e38 64 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  dy.  This routin
12e39 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
12e3a 20 62 65 66 6f 72 65 20 6d 61 6b 69 6e 67 0a 2a   before making.*
12e3b 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 70  * changes to a p
12e3c 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  age..**.** The f
12e3d 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72  irst time this r
12e3e 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
12e3f 2c 20 74 68 65 20 70 61 67 65 72 20 63 72 65 61  , the pager crea
12e40 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6a 6f 75  tes a new.** jou
12e41 72 6e 61 6c 20 61 6e 64 20 61 63 71 75 69 72 65  rnal and acquire
12e42 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  s a RESERVED loc
12e43 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
12e44 65 2e 20 20 49 66 20 74 68 65 20 52 45 53 45 52  e.  If the RESER
12e45 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 75 6c  VED.** lock coul
12e46 64 20 6e 6f 74 20 62 65 20 61 63 71 75 69 72 65  d not be acquire
12e47 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  d, this routine 
12e48 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 42  returns SQLITE_B
12e49 55 53 59 2e 20 20 54 68 65 0a 2a 2a 20 63 61 6c  USY.  The.** cal
12e4a 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73  ling routine mus
12e4b 74 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 74  t check for that
12e4c 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 6e   return value an
12e4d 64 20 62 65 20 63 61 72 65 66 75 6c 20 6e 6f 74  d be careful not
12e4e 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 61 6e   to.** change an
12e4f 79 20 70 61 67 65 20 64 61 74 61 20 75 6e 74 69  y page data unti
12e50 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  l this routine r
12e51 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
12e52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a  ..**.** If the j
12e53 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 75 6c  ournal file coul
12e54 64 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e  d not be written
12e55 20 62 65 63 61 75 73 65 20 74 68 65 20 64 69 73   because the dis
12e56 6b 20 69 73 20 66 75 6c 6c 2c 0a 2a 2a 20 74 68  k is full,.** th
12e57 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
12e58 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 46  returns SQLITE_F
12e59 55 4c 4c 20 61 6e 64 20 64 6f 65 73 20 61 6e 20  ULL and does an 
12e5a 69 6d 6d 65 64 69 61 74 65 20 72 6f 6c 6c 62 61  immediate rollba
12e5b 63 6b 2e 0a 2a 2a 20 41 6c 6c 20 73 75 62 73 65  ck..** All subse
12e5c 71 75 65 6e 74 20 77 72 69 74 65 20 61 74 74 65  quent write atte
12e5d 6d 70 74 73 20 61 6c 73 6f 20 72 65 74 75 72 6e  mpts also return
12e5e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 75 6e 74   SQLITE_FULL unt
12e5f 69 6c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61  il there.** is a
12e60 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
12e61 50 61 67 65 72 43 6f 6d 6d 69 74 28 29 20 6f 72  PagerCommit() or
12e62 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c   sqlite3PagerRol
12e63 6c 62 61 63 6b 28 29 20 74 6f 0a 2a 2a 20 72 65  lback() to.** re
12e64 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  set..*/.static i
12e65 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 28 50  nt pager_write(P
12e66 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f  gHdr *pPg){.  vo
12e67 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d  id *pData = pPg-
12e68 3e 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20  >pData;.  Pager 
12e69 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
12e6a 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20  Pager;.  int rc 
12e6b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
12e6c 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 65 72 72  /* Check for err
12e6d 6f 72 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ors.  */.  if( p
12e6e 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29  Pager->errCode )
12e6f 7b 20 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50  { .    return pP
12e70 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
12e71 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d   }.  if( pPager-
12e72 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20  >readOnly ){.   
12e73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50   return SQLITE_P
12e74 45 52 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  ERM;.  }..  asse
12e75 72 74 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74  rt( !pPager->set
12e76 4d 61 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45  Master );..  CHE
12e77 43 4b 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20  CK_PAGE(pPg);.. 
12e78 20 2f 2a 20 49 66 20 74 68 69 73 20 70 61 67 65   /* If this page
12e79 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20   was previously 
12e7a 61 63 71 75 69 72 65 64 20 77 69 74 68 20 6e 6f  acquired with no
12e7b 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 74 68 61 74  Content==1, that
12e7c 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 77 65 20 64   means.  ** we d
12e7d 69 64 6e 27 74 20 72 65 61 6c 6c 79 20 72 65 61  idn't really rea
12e7e 64 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74  d in the content
12e7f 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20 54   of the page.  T
12e80 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 20  his can happen. 
12e81 20 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c 65   ** (for example
12e82 29 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 20  ) when the page 
12e83 69 73 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 74  is being moved t
12e84 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20  o the freelist. 
12e85 20 42 75 74 0a 20 20 2a 2a 20 6e 6f 77 20 77 65   But.  ** now we
12e86 20 61 72 65 20 28 70 65 72 68 61 70 73 29 20 6d   are (perhaps) m
12e87 6f 76 69 6e 67 20 74 68 65 20 70 61 67 65 20 6f  oving the page o
12e88 66 66 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69  ff of the freeli
12e89 73 74 20 66 6f 72 0a 20 20 2a 2a 20 72 65 75 73  st for.  ** reus
12e8a 65 20 61 6e 64 20 77 65 20 6e 65 65 64 20 74 6f  e and we need to
12e8b 20 6b 6e 6f 77 20 69 74 73 20 6f 72 69 67 69 6e   know its origin
12e8c 61 6c 20 63 6f 6e 74 65 6e 74 20 73 6f 20 74 68  al content so th
12e8d 61 74 20 63 6f 6e 74 65 6e 74 0a 20 20 2a 2a 20  at content.  ** 
12e8e 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e  can be stored in
12e8f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
12e90 75 72 6e 61 6c 2e 20 20 53 6f 20 64 6f 20 74 68  urnal.  So do th
12e91 65 20 72 65 61 64 20 61 74 20 74 68 69 73 0a 20  e read at this. 
12e92 20 2a 2a 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20   ** time..  */. 
12e93 20 72 63 20 3d 20 70 61 67 65 72 5f 67 65 74 5f   rc = pager_get_
12e94 63 6f 6e 74 65 6e 74 28 70 50 67 29 3b 0a 20 20  content(pPg);.  
12e95 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
12e96 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
12e97 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65  /* Mark the page
12e98 20 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74   as dirty.  If t
12e99 68 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65  he page has alre
12e9a 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ady been written
12e9b 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75  .  ** to the jou
12e9c 72 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e  rnal then we can
12e9d 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77   return right aw
12e9e 61 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ay..  */.  sqlit
12e9f 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74  e3PcacheMakeDirt
12ea0 79 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61  y(pPg);.  if( pa
12ea1 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29  geInJournal(pPg)
12ea2 20 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65   && !subjRequire
12ea3 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20  sPage(pPg) ){.  
12ea4 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43    pPager->dirtyC
12ea5 61 63 68 65 20 3d 20 31 3b 0a 20 20 20 20 70 50  ache = 1;.    pP
12ea6 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
12ea7 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a   = 1;.  }else{..
12ea8 20 20 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74      /* If we get
12ea9 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65   this far, it me
12eaa 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67  ans that the pag
12eab 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20  e needs to be.  
12eac 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20    ** written to 
12ead 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
12eae 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63  journal or the c
12eaf 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61  keckpoint journa
12eb0 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68  l.    ** or both
12eb1 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
12eb2 46 69 72 73 74 20 63 68 65 63 6b 20 74 6f 20 73  First check to s
12eb3 65 65 20 74 68 61 74 20 74 68 65 20 74 72 61 6e  ee that the tran
12eb4 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20  saction journal 
12eb5 65 78 69 73 74 73 20 61 6e 64 0a 20 20 20 20 2a  exists and.    *
12eb6 2a 20 63 72 65 61 74 65 20 69 74 20 69 66 20 69  * create it if i
12eb7 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20 20 20  t does not..    
12eb8 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
12eb9 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41  Pager->state!=PA
12eba 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20  GER_UNLOCK );.  
12ebb 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
12ebc 67 65 72 42 65 67 69 6e 28 70 50 67 2c 20 30 29  gerBegin(pPg, 0)
12ebd 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
12ebe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
12ebf 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
12ec0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  }.    assert( pP
12ec1 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
12ec2 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20  ER_RESERVED );. 
12ec3 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e     if( !pPager->
12ec4 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 26 26 20 70  journalOpen && p
12ec5 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61  Pager->useJourna
12ec6 6c 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 70  l.          && p
12ec7 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
12ec8 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de!=PAGER_JOURNA
12ec9 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20  LMODE_OFF ){.   
12eca 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70     rc = pager_op
12ecb 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65  en_journal(pPage
12ecc 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  r);.      if( rc
12ecd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
12ece 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
12ecf 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79     pPager->dirty
12ed0 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 20 20 70  Cache = 1;.    p
12ed1 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65  Pager->dbModifie
12ed2 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a  d = 1;.  .    /*
12ed3 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   The transaction
12ed4 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69   journal now exi
12ed5 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20  sts and we have 
12ed6 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e  a RESERVED or an
12ed7 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56  .    ** EXCLUSIV
12ed8 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61  E lock on the ma
12ed9 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
12eda 2e 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72  .  Write the cur
12edb 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20  rent page to.   
12edc 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74   ** the transact
12edd 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69  ion journal if i
12ede 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61  t is not there a
12edf 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20  lready..    */. 
12ee0 20 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a 6f     if( !pageInJo
12ee1 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 70 50  urnal(pPg) && pP
12ee2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
12ee3 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  n ){.      if( p
12ee4 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72  Pg->pgno<=pPager
12ee5 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a  ->dbOrigSize ){.
12ee6 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75          u32 cksu
12ee7 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  m;.        char 
12ee8 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 20  *pData2;..      
12ee9 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 6e    /* We should n
12eea 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 68  ever write to th
12eeb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  e journal file t
12eec 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 20  he page that.   
12eed 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73       ** contains
12eee 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f   the database lo
12eef 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  cks.  The follow
12ef0 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 66  ing assert verif
12ef1 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ies.        ** t
12ef2 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 2a  hat we do not. *
12ef3 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
12ef4 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 47  ( pPg->pgno!=PAG
12ef5 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
12ef6 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 44  r) );.        pD
12ef7 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70 50  ata2 = CODEC2(pP
12ef8 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67  ager, pData, pPg
12ef9 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20 20  ->pgno, 7);.    
12efa 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 65      cksum = page
12efb 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20  r_cksum(pPager, 
12efc 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 20  (u8*)pData2);.  
12efd 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
12efe 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
12eff 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  fd, pPager->jour
12f00 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 6e  nalOff, pPg->pgn
12f01 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  o);.        if( 
12f02 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
12f03 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
12f04 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
12f05 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 74  Pager->jfd, pDat
12f06 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  a2, pPager->page
12f07 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Size,.          
12f08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f09 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
12f0a 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20 20  nalOff + 4);.   
12f0b 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
12f0c 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 61  ournalOff += pPa
12f0d 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 3b  ger->pageSize+4;
12f0e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
12f0f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
12f10 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
12f11 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69    rc = write32bi
12f12 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ts(pPager->jfd, 
12f13 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12f14 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20  ff, cksum);.    
12f15 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
12f16 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20  urnalOff += 4;. 
12f17 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12f18 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54 20   IOTRACE(("JOUT 
12f19 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e 22  %p %d %lld %d\n"
12f1a 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70  , pPager, pPg->p
12f1b 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20  gno, .          
12f1c 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
12f1d 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67 65  ournalOff, pPage
12f1e 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a 20  r->pageSize));. 
12f1f 20 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43         PAGER_INC
12f20 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f  R(sqlite3_pager_
12f21 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a 20  writej_count);. 
12f22 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
12f23 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20 70  E(("JOURNAL %d p
12f24 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d  age %d needSync=
12f25 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22  %d hash(%08x)\n"
12f26 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 50  ,.             P
12f27 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
12f28 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20  pPg->pgno, .    
12f29 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e           ((pPg->
12f2a 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
12f2b 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61 67  _SYNC)?1:0), pag
12f2c 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29  er_pagehash(pPg)
12f2d 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  ));..        /* 
12f2e 45 76 65 6e 20 69 66 20 61 6e 20 49 4f 20 6f 72  Even if an IO or
12f2f 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 6f 72 20   diskfull error 
12f30 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 6a  occurred while j
12f31 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a 20  ournalling the. 
12f32 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69         ** page i
12f33 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f 76  n the block abov
12f34 65 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64 2d  e, set the need-
12f35 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74 68  sync flag for th
12f36 65 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20  e page..        
12f37 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 68  ** Otherwise, wh
12f38 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  en the transacti
12f39 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63  on is rolled bac
12f3a 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e 0a  k, the logic in.
12f3b 20 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 62          ** playb
12f3c 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 77  ack_one_page() w
12f3d 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20 74  ill think that t
12f3e 68 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f  he page needs to
12f3f 20 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20 20   be restored.   
12f40 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 64       ** in the d
12f41 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 6e  atabase file. An
12f42 64 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  d if an IO error
12f43 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f   occurs while do
12f44 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20 20  ing so,.        
12f45 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74 69  ** then corrupti
12f46 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a 20  on may follow.. 
12f47 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
12f48 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e    if( !pPager->n
12f49 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  oSync ){.       
12f4a 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d     pPg->flags |=
12f4b 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
12f4c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67  ;.          pPag
12f4d 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31  er->needSync = 1
12f4e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20  ;.        }..   
12f4f 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72       /* An error
12f50 20 68 61 73 20 6f 63 63 75 72 65 64 20 77 72 69   has occured wri
12f51 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72  ting to the jour
12f52 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 0a 20  nal file. The . 
12f53 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61         ** transa
12f54 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72 6f  ction will be ro
12f55 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65  lled back by the
12f56 20 6c 61 79 65 72 20 61 62 6f 76 65 2e 0a 20 20   layer above..  
12f57 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
12f58 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12f59 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
12f5a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
12f5b 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 70 50     }..        pP
12f5c 61 67 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20  ager->nRec++;.  
12f5d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
12f5e 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
12f5f 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72  !=0 );.        r
12f60 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  c = sqlite3Bitve
12f61 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e  cSet(pPager->pIn
12f62 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67  Journal, pPg->pg
12f63 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  no);.        tes
12f64 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54  tcase( rc==SQLIT
12f65 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20  E_NOMEM );.     
12f66 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
12f67 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d  QLITE_OK || rc==
12f68 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
12f69 20 20 20 20 20 20 20 20 72 63 20 7c 3d 20 61 64          rc |= ad
12f6a 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76  dToSavepointBitv
12f6b 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d  ecs(pPager, pPg-
12f6c 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  >pgno);.        
12f6d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12f6e 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  K ){.          a
12f6f 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
12f70 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20  E_NOMEM );.     
12f71 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
12f72 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
12f73 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
12f74 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72  f( !pPager->jour
12f75 6e 61 6c 53 74 61 72 74 65 64 20 26 26 20 21 70  nalStarted && !p
12f76 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b  Pager->noSync ){
12f77 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 2d 3e  .          pPg->
12f78 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e  flags |= PGHDR_N
12f79 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20  EED_SYNC;.      
12f7a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64      pPager->need
12f7b 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20  Sync = 1;.      
12f7c 20 20 7d 0a 20 20 20 20 20 20 20 20 50 41 47 45    }.        PAGE
12f7d 52 54 52 41 43 45 28 28 22 41 50 50 45 4e 44 20  RTRACE(("APPEND 
12f7e 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53  %d page %d needS
12f7f 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20  ync=%d\n",.     
12f80 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52             PAGER
12f81 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
12f82 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20  >pgno,.         
12f83 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66 6c 61        ((pPg->fla
12f84 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
12f85 4e 43 29 3f 31 3a 30 29 29 29 3b 0a 20 20 20 20  NC)?1:0)));.    
12f86 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
12f87 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 65   /* If the state
12f88 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20  ment journal is 
12f89 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 67  open and the pag
12f8a 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74 2c 0a  e is not in it,.
12f8b 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72 69 74      ** then writ
12f8c 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61  e the current pa
12f8d 67 65 20 74 6f 20 74 68 65 20 73 74 61 74 65 6d  ge to the statem
12f8e 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f  ent journal.  No
12f8f 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74  te that.    ** t
12f90 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  he statement jou
12f91 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69 66 66  rnal format diff
12f92 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ers from the sta
12f93 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20 66 6f  ndard journal fo
12f94 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e 20 74  rmat.    ** in t
12f95 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74 68 65  hat it omits the
12f96 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64 20 74   checksums and t
12f97 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20 2a  he header..    *
12f98 2f 0a 20 20 20 20 69 66 28 20 73 75 62 6a 52 65  /.    if( subjRe
12f99 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20  quiresPage(pPg) 
12f9a 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 75  ){.      rc = su
12f9b 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67  bjournalPage(pPg
12f9c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
12f9d 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 61  /* Update the da
12f9e 74 61 62 61 73 65 20 73 69 7a 65 20 61 6e 64 20  tabase size and 
12f9f 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 61  return..  */.  a
12fa0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
12fa1 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
12fa2 45 44 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  ED );.  if( pPag
12fa3 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e  er->dbSize<pPg->
12fa4 70 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67  pgno ){.    pPag
12fa5 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 67  er->dbSize = pPg
12fa6 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 69 66 28 20  ->pgno;.    if( 
12fa7 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d  pPager->dbSize==
12fa8 28 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70  (PAGER_MJ_PGNO(p
12fa9 50 61 67 65 72 29 2d 31 29 20 29 7b 0a 20 20 20  Pager)-1) ){.   
12faa 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a     pPager->dbSiz
12fab 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  e++;.    }.  }. 
12fac 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
12fad 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
12fae 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61  on is used to ma
12faf 72 6b 20 61 20 64 61 74 61 2d 70 61 67 65 20 61  rk a data-page a
12fb0 73 20 77 72 69 74 61 62 6c 65 2e 20 49 74 20 75  s writable. It u
12fb1 73 65 73 20 0a 2a 2a 20 70 61 67 65 72 5f 77 72  ses .** pager_wr
12fb2 69 74 65 28 29 20 74 6f 20 6f 70 65 6e 20 61 20  ite() to open a 
12fb3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 69 66  journal file (if
12fb4 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61   it is not alrea
12fb5 64 79 20 6f 70 65 6e 29 0a 2a 2a 20 61 6e 64 20  dy open).** and 
12fb6 77 72 69 74 65 20 74 68 65 20 70 61 67 65 20 2a  write the page *
12fb7 70 44 61 74 61 20 74 6f 20 74 68 65 20 6a 6f 75  pData to the jou
12fb8 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rnal..**.** The 
12fb9 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
12fba 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
12fbb 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69 74 65   and pager_write
12fbc 28 29 20 69 73 20 74 68 61 74 20 74 68 69 73 0a  () is that this.
12fbd 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f  ** function also
12fbe 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 20   deals with the 
12fbf 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65  special case whe
12fc0 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 61 67  re 2 or more pag
12fc1 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73  es.** fit on a s
12fc2 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f  ingle disk secto
12fc3 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  r. In this case 
12fc4 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e 74 20  all co-resident 
12fc5 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61  pages.** must ha
12fc6 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  ve been written 
12fc7 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
12fc8 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74 75 72  ile before retur
12fc9 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ning..*/.SQLITE_
12fca 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
12fcb 74 65 33 50 61 67 65 72 57 72 69 74 65 28 44 62  te3PagerWrite(Db
12fcc 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a  Page *pDbPage){.
12fcd 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
12fce 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a  E_OK;..  PgHdr *
12fcf 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b 0a 20  pPg = pDbPage;. 
12fd0 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
12fd1 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
12fd2 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53 65 63  Pgno nPagePerSec
12fd3 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d 3e 73  tor = (pPager->s
12fd4 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67 65 72  ectorSize/pPager
12fd5 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20  ->pageSize);..  
12fd6 69 66 28 20 6e 50 61 67 65 50 65 72 53 65 63 74  if( nPagePerSect
12fd7 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f  or>1 ){.    Pgno
12fd8 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20   nPageCount;    
12fd9 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
12fda 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
12fdb 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
12fdc 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b  */.    Pgno pg1;
12fdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12fde 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f   /* First page o
12fdf 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67  f the sector pPg
12fe0 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20   is located on. 
12fe1 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65  */.    int nPage
12fe2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
12fe3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
12fe4 67 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 20  ges starting at 
12fe5 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a  pg1 to journal *
12fe6 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20  /.    int ii;.  
12fe7 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d    int needSync =
12fe8 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20   0;..    /* Set 
12fe9 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c  the doNotSync fl
12fea 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20 69 73  ag to 1. This is
12feb 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e 6e   because we cann
12fec 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e  ot allow a journ
12fed 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64 65 72  al.    ** header
12fee 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 62   to be written b
12fef 65 74 77 65 65 6e 20 74 68 65 20 70 61 67 65 73  etween the pages
12ff0 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20 74 68   journaled by th
12ff1 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20  is function..   
12ff2 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
12ff3 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73  !MEMDB );.    as
12ff4 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f  sert( pPager->do
12ff5 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20  NotSync==0 );.  
12ff6 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53    pPager->doNotS
12ff7 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a  ync = 1;..    /*
12ff8 20 54 68 69 73 20 74 72 69 63 6b 20 61 73 73 75   This trick assu
12ff9 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20 74 68  mes that both th
12ffa 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20  e page-size and 
12ffb 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72 65 0a  sector-size are.
12ffc 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65      ** an intege
12ffd 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 49 74  r power of 2. It
12ffe 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20 70   sets variable p
12fff 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69  g1 to the identi
13000 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74  fier.    ** of t
13001 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66  he first page of
13002 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 20   the sector pPg 
13003 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a 20  is located on.. 
13004 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d 20     */.    pg1 = 
13005 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 26  ((pPg->pgno-1) &
13006 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63 74 6f   ~(nPagePerSecto
13007 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20 20  r-1)) + 1;..    
13008 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
13009 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 28 69  count(pPager, (i
1300a 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e 74  nt *)&nPageCount
1300b 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e  );.    if( pPg->
1300c 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 20  pgno>nPageCount 
1300d 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d  ){.      nPage =
1300e 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 67   (pPg->pgno - pg
1300f 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20  1)+1;.    }else 
13010 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 50 65  if( (pg1+nPagePe
13011 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67 65  rSector-1)>nPage
13012 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e  Count ){.      n
13013 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f 75 6e  Page = nPageCoun
13014 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65 6c  t+1-pg1;.    }el
13015 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20  se{.      nPage 
13016 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72  = nPagePerSector
13017 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
13018 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20 20  rt(nPage>0);.   
13019 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 50 67   assert(pg1<=pPg
1301a 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73 73  ->pgno);.    ass
1301b 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 29 3e  ert((pg1+nPage)>
1301c 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20  pPg->pgno);..   
1301d 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50   for(ii=0; ii<nP
1301e 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  age && rc==SQLIT
1301f 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  E_OK; ii++){.   
13020 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 31     Pgno pg = pg1
13021 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 64 72  +ii;.      PgHdr
13022 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20 69   *pPage;.      i
13023 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f  f( pg==pPg->pgno
13024 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74 76   || !sqlite3Bitv
13025 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70  ecTest(pPager->p
13026 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20 29  InJournal, pg) )
13027 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 67  {.        if( pg
13028 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  !=PAGER_MJ_PGNO(
13029 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20  pPager) ){.     
1302a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1302b 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72  3PagerGet(pPager
1302c 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b 0a 20  , pg, &pPage);. 
1302d 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d           if( rc=
1302e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1302f 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70            rc = p
13030 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 67 65  ager_write(pPage
13031 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
13032 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 26  f( pPage->flags&
13033 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
13034 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
13035 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20   needSync = 1;. 
13036 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73               ass
13037 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64  ert(pPager->need
13038 53 79 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 20  Sync);.         
13039 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
1303a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
1303b 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ef(pPage);.     
1303c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1303d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1303e 20 28 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f   (pPage = pager_
1303f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70  lookup(pPager, p
13040 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  g))!=0 ){.      
13041 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61    if( pPage->fla
13042 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
13043 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  NC ){.          
13044 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20  needSync = 1;.  
13045 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13046 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
13047 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  f(pPage);.      
13048 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
13049 49 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45  If the PGHDR_NEE
1304a 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 73  D_SYNC flag is s
1304b 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 20 74 68  et for any of th
1304c 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 0a 20  e nPage pages . 
1304d 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 61     ** starting a
1304e 74 20 70 67 31 2c 20 74 68 65 6e 20 69 74 20 6e  t pg1, then it n
1304f 65 65 64 73 20 74 6f 20 62 65 20 73 65 74 20 66  eeds to be set f
13050 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e 20  or all of them. 
13051 42 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 77  Because.    ** w
13052 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f 66  riting to any of
13053 20 74 68 65 73 65 20 6e 50 61 67 65 20 70 61 67   these nPage pag
13054 65 73 20 6d 61 79 20 64 61 6d 61 67 65 20 74 68  es may damage th
13055 65 20 6f 74 68 65 72 73 2c 20 74 68 65 0a 20 20  e others, the.  
13056 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c    ** journal fil
13057 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 73  e must contain s
13058 79 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20 6f  ync()ed copies o
13059 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20 20  f all of them.  
1305a 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 20    ** before any 
1305b 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20 77  of them can be w
1305c 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68  ritten out to th
1305d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1305e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1305f 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20  needSync ){.    
13060 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42    assert( !MEMDB
13061 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79   && pPager->noSy
13062 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 66  nc==0 );.      f
13063 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67  or(ii=0; ii<nPag
13064 65 20 26 26 20 6e 65 65 64 53 79 6e 63 3b 20 69  e && needSync; i
13065 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 67  i++){.        Pg
13066 48 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61 67  Hdr *pPage = pag
13067 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72  er_lookup(pPager
13068 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 20  , pg1+ii);.     
13069 20 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a     if( pPage ){.
1306a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 2d            pPage-
1306b 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f  >flags |= PGHDR_
1306c 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20  NEED_SYNC;.     
1306d 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
1306e 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20  rUnref(pPage);. 
1306f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
13070 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50  .      assert(pP
13071 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b  ager->needSync);
13072 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65  .    }..    asse
13073 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f  rt( pPager->doNo
13074 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20  tSync==1 );.    
13075 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e  pPager->doNotSyn
13076 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  c = 0;.  }else{.
13077 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77      rc = pager_w
13078 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20  rite(pDbPage);. 
13079 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1307a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1307b 54 52 55 45 20 69 66 20 74 68 65 20 70 61 67 65  TRUE if the page
1307c 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72   given in the ar
1307d 67 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76 69  gument was previ
1307e 6f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20  ously passed.** 
1307f 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  to sqlite3PagerW
13080 72 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 65  rite().  In othe
13081 72 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20  r words, return 
13082 54 52 55 45 20 69 66 20 69 74 20 69 73 20 6f 6b  TRUE if it is ok
13083 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68  .** to change th
13084 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
13085 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65   page..*/.#ifnde
13086 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  f NDEBUG.SQLITE_
13087 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
13088 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
13089 62 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67 29  ble(DbPage *pPg)
1308a 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e  {.  return pPg->
1308b 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
1308c 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  Y;.}.#endif../*.
1308d 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69  ** A call to thi
1308e 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20  s routine tells 
1308f 74 68 65 20 70 61 67 65 72 20 74 68 61 74 20 69  the pager that i
13090 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  t is not necessa
13091 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 74  ry to.** write t
13092 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  he information o
13093 6e 20 70 61 67 65 20 70 50 67 20 62 61 63 6b 20  n page pPg back 
13094 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76 65  to the disk, eve
13095 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61 74  n though.** that
13096 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 6d   page might be m
13097 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20  arked as dirty. 
13098 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66   This happens, f
13099 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e  or example, when
1309a 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61 73  .** the page has
1309b 20 62 65 65 6e 20 61 64 64 65 64 20 61 73 20 61   been added as a
1309c 20 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72 65   leaf of the fre
1309d 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74 73  elist and so its
1309e 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c  .** content no l
1309f 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a 2a  onger matters..*
130a0 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 69  *.** The overlyi
130a1 6e 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79 65  ng software laye
130a2 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
130a3 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66  tine when all of
130a4 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e 20   the data.** on 
130a5 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 20 69  the given page i
130a6 73 20 75 6e 75 73 65 64 2e 20 20 54 68 65 20 70  s unused.  The p
130a7 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70  ager marks the p
130a8 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a  age as clean so.
130a9 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 73 20  ** that it does 
130aa 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 6e 20  not get written 
130ab 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54  to disk..**.** T
130ac 65 73 74 73 20 73 68 6f 77 20 74 68 61 74 20 74  ests show that t
130ad 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
130ae 2c 20 74 6f 67 65 74 68 65 72 20 77 69 74 68 20  , together with 
130af 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61  the.** sqlite3Pa
130b0 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28  gerDontRollback(
130b1 29 20 62 65 6c 6f 77 2c 20 6d 6f 72 65 20 74 68  ) below, more th
130b2 61 6e 20 64 6f 75 62 6c 65 20 74 68 65 20 73 70  an double the sp
130b3 65 65 64 0a 2a 2a 20 6f 66 20 6c 61 72 67 65 20  eed.** of large 
130b4 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e  INSERT operation
130b5 73 20 61 6e 64 20 71 75 61 64 72 75 70 6c 65 20  s and quadruple 
130b6 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61 72  the speed of lar
130b7 67 65 20 44 45 4c 45 54 45 73 2e 0a 2a 2a 0a 2a  ge DELETEs..**.*
130b8 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f 75 74  * When this rout
130b9 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 73  ine is called, s
130ba 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 72 65  et the bit corre
130bb 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 44 62 50  sponding to pDbP
130bc 61 67 65 20 69 6e 0a 2a 2a 20 74 68 65 20 50 61  age in.** the Pa
130bd 67 65 72 2e 70 41 6c 77 61 79 73 52 6f 6c 6c 62  ger.pAlwaysRollb
130be 61 63 6b 20 62 69 74 76 65 63 2e 20 20 53 75 62  ack bitvec.  Sub
130bf 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
130c0 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72  .** sqlite3Pager
130c1 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 66  DontRollback() f
130c2 6f 72 20 74 68 65 20 73 61 6d 65 20 70 61 67 65  or the same page
130c3 20 77 69 6c 6c 20 74 68 65 72 65 61 66 74 65 72   will thereafter
130c4 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20   be ignored..** 
130c5 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72  This is necessar
130c6 79 20 74 6f 20 61 76 6f 69 64 20 61 20 70 72 6f  y to avoid a pro
130c7 62 6c 65 6d 20 77 68 65 72 65 20 61 20 70 61 67  blem where a pag
130c8 65 20 77 69 74 68 20 64 61 74 61 20 69 73 20 61  e with data is a
130c9 64 64 65 64 20 74 6f 0a 2a 2a 20 74 68 65 20 66  dded to.** the f
130ca 72 65 65 6c 69 73 74 20 64 75 72 69 6e 67 20 6f  reelist during o
130cb 6e 65 20 70 61 72 74 20 6f 66 20 61 20 74 72 61  ne part of a tra
130cc 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 72 65  nsaction then re
130cd 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a  moved from the.*
130ce 2a 20 66 72 65 65 6c 69 73 74 20 64 75 72 69 6e  * freelist durin
130cf 67 20 61 20 6c 61 74 65 72 20 70 61 72 74 20 6f  g a later part o
130d0 66 20 74 68 65 20 73 61 6d 65 20 74 72 61 6e 73  f the same trans
130d1 61 63 74 69 6f 6e 20 61 6e 64 20 72 65 75 73 65  action and reuse
130d2 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 6f 74  d for some.** ot
130d3 68 65 72 20 70 75 72 70 6f 73 65 2e 20 20 57 68  her purpose.  Wh
130d4 65 6e 20 69 74 20 69 73 20 66 69 72 73 74 20 61  en it is first a
130d5 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65  dded to the free
130d6 6c 69 73 74 2c 20 74 68 69 73 20 72 6f 75 74 69  list, this routi
130d7 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e  ne is.** called.
130d8 20 20 57 68 65 6e 20 72 65 75 73 65 64 2c 20 74    When reused, t
130d9 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 44  he sqlite3PagerD
130da 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 72 6f  ontRollback() ro
130db 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
130dc 0a 2a 2a 20 42 75 74 20 62 65 63 61 75 73 65 20  .** But because 
130dd 74 68 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e  the page contain
130de 73 20 63 72 69 74 69 63 61 6c 20 64 61 74 61 2c  s critical data,
130df 20 77 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74   we still need t
130e0 6f 20 62 65 20 73 75 72 65 20 69 74 0a 2a 2a 20  o be sure it.** 
130e1 67 65 74 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  gets rolled back
130e2 20 69 6e 20 73 70 69 74 65 20 6f 66 20 74 68 65   in spite of the
130e3 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
130e4 74 52 6f 6c 6c 62 61 63 6b 28 29 20 63 61 6c 6c  tRollback() call
130e5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
130e6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
130e7 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 44 62  agerDontWrite(Db
130e8 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a  Page *pDbPage){.
130e9 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70    PgHdr *pPg = p
130ea 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20  DbPage;.  Pager 
130eb 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
130ec 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b  Pager;.  int rc;
130ed 0a 0a 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e  ..  if( pPg->pgn
130ee 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  o>pPager->dbOrig
130ef 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 75  Size ){.    retu
130f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
130f1 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  }.  if( pPager->
130f2 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 3d  pAlwaysRollback=
130f3 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
130f4 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  ( pPager->pInJou
130f5 72 6e 61 6c 20 29 3b 0a 20 20 20 20 70 50 61 67  rnal );.    pPag
130f6 65 72 2d 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62  er->pAlwaysRollb
130f7 61 63 6b 20 3d 20 73 71 6c 69 74 65 33 42 69 74  ack = sqlite3Bit
130f8 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65 72  vecCreate(pPager
130f9 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b 0a 20  ->dbOrigSize);. 
130fa 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e     if( !pPager->
130fb 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20  pAlwaysRollback 
130fc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
130fd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
130fe 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73    }.  }.  rc = s
130ff 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
13100 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52  pPager->pAlwaysR
13101 6f 6c 6c 62 61 63 6b 2c 20 70 50 67 2d 3e 70 67  ollback, pPg->pg
13102 6e 6f 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  no);..  if( rc==
13103 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 70 50  SQLITE_OK && (pP
13104 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
13105 49 52 54 59 29 20 26 26 20 70 50 61 67 65 72 2d  IRTY) && pPager-
13106 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29  >nSavepoint==0 )
13107 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
13108 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
13109 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 20  ER_SHARED );.   
1310a 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53   if( pPager->dbS
1310b 69 7a 65 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 26  ize==pPg->pgno &
1310c 26 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  & pPager->dbOrig
1310d 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 53  Size<pPager->dbS
1310e 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ize ){.      /* 
1310f 49 66 20 74 68 69 73 20 70 61 67 65 73 20 69 73  If this pages is
13110 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69   the last page i
13111 6e 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 74  n the file and t
13112 68 65 20 66 69 6c 65 20 68 61 73 20 67 72 6f 77  he file has grow
13113 6e 0a 20 20 20 20 20 20 2a 2a 20 64 75 72 69 6e  n.      ** durin
13114 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  g the current tr
13115 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
13116 64 6f 20 4e 4f 54 20 6d 61 72 6b 20 74 68 65 20  do NOT mark the 
13117 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 0a 20  page as clean.. 
13118 20 20 20 20 20 2a 2a 20 57 68 65 6e 20 74 68 65       ** When the
13119 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 67   database file g
1311a 72 6f 77 73 2c 20 77 65 20 6d 75 73 74 20 6d 61  rows, we must ma
1311b 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
1311c 20 6c 61 73 74 20 70 61 67 65 0a 20 20 20 20 20   last page.     
1311d 20 2a 2a 20 67 65 74 73 20 77 72 69 74 74 65 6e   ** gets written
1311e 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 20 73   at least once s
1311f 6f 20 74 68 61 74 20 74 68 65 20 64 69 73 6b 20  o that the disk 
13120 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 74 68 65  file will be the
13121 20 63 6f 72 72 65 63 74 0a 20 20 20 20 20 20 2a   correct.      *
13122 2a 20 73 69 7a 65 2e 20 49 66 20 79 6f 75 20 64  * size. If you d
13123 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 68 69 73  o not write this
13124 20 70 61 67 65 20 61 6e 64 20 74 68 65 20 73 69   page and the si
13125 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20  ze of the file. 
13126 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64       ** on the d
13127 69 73 6b 20 65 6e 64 73 20 75 70 20 62 65 69 6e  isk ends up bein
13128 67 20 74 6f 6f 20 73 6d 61 6c 6c 2c 20 74 68 61  g too small, tha
13129 74 20 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 61  t can lead to da
1312a 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20  tabase.      ** 
1312b 63 6f 72 72 75 70 74 69 6f 6e 20 64 75 72 69 6e  corruption durin
1312c 67 20 74 68 65 20 6e 65 78 74 20 74 72 61 6e 73  g the next trans
1312d 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f  action..      */
1312e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1312f 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 44    PAGERTRACE(("D
13130 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 25  ONT_WRITE page %
13131 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d  d of %d\n", pPg-
13132 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70  >pgno, PAGERID(p
13133 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20  Pager)));.      
13134 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20  IOTRACE(("CLEAN 
13135 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72  %p %d\n", pPager
13136 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20  , pPg->pgno)).  
13137 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c      pPg->flags |
13138 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49  = PGHDR_DONT_WRI
13139 54 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  TE;.#ifdef SQLIT
1313a 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
1313b 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73      pPg->pageHas
1313c 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61  h = pager_pageha
1313d 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a  sh(pPg);.#endif.
1313e 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1313f 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
13140 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72  A call to this r
13141 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 74 68 65  outine tells the
13142 20 70 61 67 65 72 20 74 68 61 74 20 69 66 20 61   pager that if a
13143 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73   rollback occurs
13144 2c 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 6e  ,.** it is not n
13145 65 63 65 73 73 61 72 79 20 74 6f 20 72 65 73 74  ecessary to rest
13146 6f 72 65 20 74 68 65 20 64 61 74 61 20 6f 6e 20  ore the data on 
13147 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 2e 20  the given page. 
13148 20 54 68 69 73 0a 2a 2a 20 6d 65 61 6e 73 20 74   This.** means t
13149 68 61 74 20 74 68 65 20 70 61 67 65 72 20 64 6f  hat the pager do
1314a 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 72  es not have to r
1314b 65 63 6f 72 64 20 74 68 65 20 67 69 76 65 6e 20  ecord the given 
1314c 70 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a 20 72  page in the.** r
1314d 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
1314e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 65 20 68 61 76  .**.** If we hav
1314f 65 20 6e 6f 74 20 79 65 74 20 61 63 74 75 61 6c  e not yet actual
13150 6c 79 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74  ly read the cont
13151 65 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67 65  ent of this page
13152 20 28 69 66 0a 2a 2a 20 74 68 65 20 50 67 48 64   (if.** the PgHd
13153 72 2e 6e 65 65 64 52 65 61 64 20 66 6c 61 67 20  r.needRead flag 
13154 69 73 20 73 65 74 29 20 74 68 65 6e 20 74 68 69  is set) then thi
13155 73 20 72 6f 75 74 69 6e 65 20 61 63 74 73 20 61  s routine acts a
13156 73 20 61 20 70 72 6f 6d 69 73 65 0a 2a 2a 20 74  s a promise.** t
13157 68 61 74 20 77 65 20 77 69 6c 6c 20 6e 65 76 65  hat we will neve
13158 72 20 6e 65 65 64 20 74 6f 20 72 65 61 64 20 74  r need to read t
13159 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20  he page content 
1315a 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 2a  in the future..*
1315b 2a 20 73 6f 20 74 68 65 20 6e 65 65 64 52 65 61  * so the needRea
1315c 64 20 66 6c 61 67 20 63 61 6e 20 62 65 20 63 6c  d flag can be cl
1315d 65 61 72 65 64 20 61 74 20 74 68 69 73 20 70 6f  eared at this po
1315e 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  int..*/.SQLITE_P
1315f 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
13160 74 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c  te3PagerDontRoll
13161 62 61 63 6b 28 44 62 50 61 67 65 20 2a 70 50 67  back(DbPage *pPg
13162 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
13163 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
13164 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e  ;.  TESTONLY( in
13165 74 20 72 63 3b 20 29 20 20 2f 2a 20 52 65 74 75  t rc; )  /* Retu
13166 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71  rn value from sq
13167 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 29  lite3BitvecSet()
13168 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
13169 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
1316a 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a  GER_RESERVED );.
1316b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75  .  /* If the jou
1316c 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74  rnal file is not
1316d 20 6f 70 65 6e 2c 20 6f 72 20 44 6f 6e 74 57 72   open, or DontWr
1316e 69 74 65 28 29 20 68 61 73 20 62 65 65 6e 20 63  ite() has been c
1316f 61 6c 6c 65 64 20 6f 6e 0a 20 20 2a 2a 20 74 68  alled on.  ** th
13170 69 73 20 70 61 67 65 20 28 44 6f 6e 74 57 72 69  is page (DontWri
13171 74 65 28 29 20 73 65 74 73 20 74 68 65 20 50 61  te() sets the Pa
13172 67 65 72 2e 70 41 6c 77 61 79 73 52 6f 6c 6c 62  ger.pAlwaysRollb
13173 61 63 6b 20 62 69 74 29 2c 20 74 68 65 6e 20 74  ack bit), then t
13174 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  his.  ** functio
13175 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20  n is a no-op..  
13176 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  */.  if( pPager-
13177 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20  >journalOpen==0 
13178 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 42 69  .   || sqlite3Bi
13179 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d  tvecTest(pPager-
1317a 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b  >pAlwaysRollback
1317b 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 0a 20 20 20  , pPg->pgno).   
1317c 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61  || pPg->pgno>pPa
1317d 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 0a  ger->dbOrigSize.
1317e 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b    ){.    return;
1317f 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  .  }..#ifdef SQL
13180 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54  ITE_SECURE_DELET
13181 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 42  E.  if( sqlite3B
13182 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72  itvecTest(pPager
13183 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50  ->pInJournal, pP
13184 67 2d 3e 70 67 6e 6f 29 21 3d 30 0a 20 20 20 7c  g->pgno)!=0.   |
13185 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67  | pPg->pgno>pPag
13186 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29  er->dbOrigSize )
13187 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
13188 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
13189 66 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20  f SECURE_DELETE 
1318a 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65  is disabled, the
1318b 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61  n there is no wa
1318c 79 20 74 68 61 74 20 74 68 69 73 0a 20 20 2a 2a  y that this.  **
1318d 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20   routine can be 
1318e 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65  called on a page
1318f 20 66 6f 72 20 77 68 69 63 68 20 73 71 6c 69 74   for which sqlit
13190 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65  e3PagerDontWrite
13191 28 29 0a 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20  ().  ** has not 
13192 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20  been previously 
13193 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74 68  called during th
13194 65 20 73 61 6d 65 20 74 72 61 6e 73 61 63 74 69  e same transacti
13195 6f 6e 2e 0a 20 20 2a 2a 20 41 6e 64 20 69 66 20  on..  ** And if 
13196 44 6f 6e 74 57 72 69 74 65 28 29 20 68 61 73 20  DontWrite() has 
13197 70 72 65 76 69 6f 75 73 6c 79 20 62 65 65 6e 20  previously been 
13198 63 61 6c 6c 65 64 2c 20 74 68 65 20 66 6f 6c 6c  called, the foll
13199 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 6f 6e 64 69  owing.  ** condi
1319a 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 6d 65  tions must be me
1319b 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 28 4c 61  t..  **.  ** (La
1319c 74 65 72 3a 29 20 20 4e 6f 74 20 74 72 75 65 2e  ter:)  Not true.
1319d 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73    If the databas
1319e 65 20 69 73 20 63 6f 72 72 75 70 74 65 64 20 62  e is corrupted b
1319f 79 20 68 61 76 69 6e 67 20 64 75 70 6c 69 63 61  y having duplica
131a0 74 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6f 6e  te.  ** pages on
131a1 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 28 65   the freelist (e
131a2 78 3a 20 63 6f 72 72 75 70 74 39 2e 74 65 73 74  x: corrupt9.test
131a3 29 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  ) then the follo
131a4 77 69 6e 67 20 69 73 20 6e 6f 74 0a 20 20 2a 2a  wing is not.  **
131a5 20 6e 65 63 65 73 73 61 72 69 6c 79 20 74 72 75   necessarily tru
131a6 65 3a 0a 20 20 2a 2f 0a 20 20 2f 2a 20 61 73 73  e:.  */.  /* ass
131a7 65 72 74 28 20 21 70 50 67 2d 3e 69 6e 4a 6f 75  ert( !pPg->inJou
131a8 72 6e 61 6c 20 26 26 20 28 69 6e 74 29 70 50 67  rnal && (int)pPg
131a9 2d 3e 70 67 6e 6f 20 3c 3d 20 70 50 61 67 65 72  ->pgno <= pPager
131aa 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 3b 20  ->dbOrigSize ); 
131ab 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
131ac 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
131ad 21 3d 30 20 29 3b 0a 20 20 70 50 67 2d 3e 66 6c  !=0 );.  pPg->fl
131ae 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45  ags &= ~PGHDR_NE
131af 45 44 5f 52 45 41 44 3b 0a 0a 20 20 2f 2a 20 46  ED_READ;..  /* F
131b0 61 69 6c 75 72 65 20 74 6f 20 73 65 74 20 74 68  ailure to set th
131b1 65 20 62 69 74 73 20 69 6e 20 74 68 65 20 49 6e  e bits in the In
131b2 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 65 63 74  Journal bit-vect
131b3 6f 72 73 20 69 73 20 62 65 6e 69 67 6e 2e 0a 20  ors is benign.. 
131b4 20 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 6d 65   ** It merely me
131b5 61 6e 73 20 74 68 61 74 20 77 65 20 6d 69 67 68  ans that we migh
131b6 74 20 64 6f 20 73 6f 6d 65 20 65 78 74 72 61 20  t do some extra 
131b7 77 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20  work to journal 
131b8 61 20 70 61 67 65 0a 20 20 2a 2a 20 74 68 61 74  a page.  ** that
131b9 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
131ba 6f 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20  o be journaled. 
131bb 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 62   Nevertheless, b
131bc 65 20 73 75 72 65 20 74 6f 20 74 65 73 74 20 74  e sure to test t
131bd 68 65 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65  he.  ** case whe
131be 72 65 20 61 20 6d 61 6c 6c 6f 63 20 65 72 72 6f  re a malloc erro
131bf 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74  r occurs while t
131c0 72 79 69 6e 67 20 74 6f 20 73 65 74 20 61 20 62  rying to set a b
131c1 69 74 20 69 6e 20 61 20 0a 20 20 2a 2a 20 62 69  it in a .  ** bi
131c2 74 20 76 65 63 74 6f 72 2e 0a 20 20 2a 2f 0a 20  t vector..  */. 
131c3 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
131c4 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 54  ignMalloc();.  T
131c5 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20  ESTONLY( rc = ) 
131c6 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74  sqlite3BitvecSet
131c7 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  (pPager->pInJour
131c8 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b  nal, pPg->pgno);
131c9 0a 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d  .  testcase( rc=
131ca 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
131cb 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20  .  TESTONLY( rc 
131cc 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69  = ) addToSavepoi
131cd 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72  ntBitvecs(pPager
131ce 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20  , pPg->pgno);.  
131cf 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51  testcase( rc==SQ
131d0 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20  LITE_NOMEM );.  
131d1 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
131d2 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 0a 20 20 50 41  Malloc();...  PA
131d3 47 45 52 54 52 41 43 45 28 28 22 44 4f 4e 54 5f  GERTRACE(("DONT_
131d4 52 4f 4c 4c 42 41 43 4b 20 70 61 67 65 20 25 64  ROLLBACK page %d
131d5 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e   of %d\n", pPg->
131d6 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50  pgno, PAGERID(pP
131d7 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 52 41  ager)));.  IOTRA
131d8 43 45 28 28 22 47 41 52 42 41 47 45 20 25 70 20  CE(("GARBAGE %p 
131d9 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
131da 50 67 2d 3e 70 67 6e 6f 29 29 0a 7d 0a 0a 0a 2f  Pg->pgno)).}.../
131db 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
131dc 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69  e is called to i
131dd 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 64 61 74  ncrement the dat
131de 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67  abase file chang
131df 65 2d 63 6f 75 6e 74 65 72 2c 0a 2a 2a 20 73 74  e-counter,.** st
131e0 6f 72 65 64 20 61 74 20 62 79 74 65 20 32 34 20  ored at byte 24 
131e1 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c  of the pager fil
131e2 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
131e3 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
131e4 67 65 63 6f 75 6e 74 65 72 28 50 61 67 65 72 20  gecounter(Pager 
131e5 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 44  *pPager, int isD
131e6 69 72 65 63 74 29 7b 0a 20 20 50 67 48 64 72 20  irect){.  PgHdr 
131e7 2a 70 50 67 48 64 72 3b 0a 20 20 75 33 32 20 63  *pPgHdr;.  u32 c
131e8 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 3b 0a 20  hange_counter;. 
131e9 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
131ea 5f 4f 4b 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  _OK;..#ifndef SQ
131eb 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d  LITE_ENABLE_ATOM
131ec 49 43 5f 57 52 49 54 45 0a 20 20 61 73 73 65 72  IC_WRITE.  asser
131ed 74 28 20 69 73 44 69 72 65 63 74 3d 3d 30 20 29  t( isDirect==0 )
131ee 3b 20 20 2f 2a 20 69 73 44 69 72 65 63 74 20 69  ;  /* isDirect i
131ef 73 20 6f 6e 6c 79 20 74 72 75 65 20 66 6f 72 20  s only true for 
131f0 61 74 6f 6d 69 63 20 77 72 69 74 65 73 20 2a 2f  atomic writes */
131f1 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 70  .#endif.  if( !p
131f2 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
131f3 6e 74 44 6f 6e 65 20 26 26 20 70 50 61 67 65 72  ntDone && pPager
131f4 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20 20  ->dbSize>0 ){.  
131f5 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 31    /* Open page 1
131f6 20 6f 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72   of the file for
131f7 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20   writing. */.   
131f8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
131f9 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 31 2c  erGet(pPager, 1,
131fa 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20 20 69   &pPgHdr);.    i
131fb 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
131fc 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
131fd 20 20 20 69 66 28 20 21 69 73 44 69 72 65 63 74     if( !isDirect
131fe 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
131ff 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
13200 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 20 20  (pPgHdr);.      
13201 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
13202 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  K ){.        sql
13203 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
13204 50 67 48 64 72 29 3b 0a 20 20 20 20 20 20 20 20  PgHdr);.        
13205 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
13206 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
13207 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76   Increment the v
13208 61 6c 75 65 20 6a 75 73 74 20 72 65 61 64 20 61  alue just read a
13209 6e 64 20 77 72 69 74 65 20 69 74 20 62 61 63 6b  nd write it back
1320a 20 74 6f 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a   to byte 24. */.
1320b 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74      change_count
1320c 65 72 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34  er = sqlite3Get4
1320d 62 79 74 65 28 28 75 38 2a 29 70 50 61 67 65 72  byte((u8*)pPager
1320e 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20  ->dbFileVers);. 
1320f 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65     change_counte
13210 72 2b 2b 3b 0a 20 20 20 20 70 75 74 33 32 62 69  r++;.    put32bi
13211 74 73 28 28 28 63 68 61 72 2a 29 70 50 67 48 64  ts(((char*)pPgHd
13212 72 2d 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68  r->pData)+24, ch
13213 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a  ange_counter);..
13214 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
13215 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
13216 45 0a 20 20 20 20 69 66 28 20 69 73 44 69 72 65  E.    if( isDire
13217 63 74 20 26 26 20 70 50 61 67 65 72 2d 3e 66 64  ct && pPager->fd
13218 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ->pMethods ){.  
13219 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a      const void *
1321a 7a 42 75 66 20 3d 20 70 50 67 48 64 72 2d 3e 70  zBuf = pPgHdr->p
1321b 44 61 74 61 3b 0a 20 20 20 20 20 20 61 73 73 65  Data;.      asse
1321c 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 46 69  rt( pPager->dbFi
1321d 6c 65 53 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20  leSize>0 );.    
1321e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1321f 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64  Write(pPager->fd
13220 2c 20 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e  , zBuf, pPager->
13221 70 61 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20  pageSize, 0);.  
13222 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
13223 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 70  /* Release the p
13224 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 2a  age reference. *
13225 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  /.    sqlite3Pag
13226 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b  erUnref(pPgHdr);
13227 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61  .    pPager->cha
13228 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31  ngeCountDone = 1
13229 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1322a 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63  c;.}../*.** Sync
1322b 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20   the pager file 
1322c 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 53 51 4c 49  to disk..*/.SQLI
1322d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1322e 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28  qlite3PagerSync(
1322f 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
13230 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20    int rc;.  if( 
13231 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 72 63 20  MEMDB ){.    rc 
13232 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
13233 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73  else{.    rc = s
13234 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61  qlite3OsSync(pPa
13235 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d  ger->fd, pPager-
13236 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20  >sync_flags);.  
13237 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
13238 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65  ../*.** Sync the
13239 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
1323a 6f 72 20 74 68 65 20 70 61 67 65 72 20 70 50 61  or the pager pPa
1323b 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69  ger. zMaster poi
1323c 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d 65 0a  nts to the name.
1323d 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a  ** of a master j
1323e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74  ournal file that
1323f 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74   should be writt
13240 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 69  en into the indi
13241 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e 61  vidual.** journa
13242 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72 20  l file. zMaster 
13243 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68 69  may be NULL, whi
13244 63 68 20 69 73 20 69 6e 74 65 72 70 72 65 74 65  ch is interprete
13245 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a  d as no master.*
13246 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69 6e  * journal (a sin
13247 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61  gle database tra
13248 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a  nsaction)..**.**
13249 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e   This routine en
1324a 73 75 72 65 73 20 74 68 61 74 20 74 68 65 20 6a  sures that the j
1324b 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64  ournal is synced
1324c 2c 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  , all dirty page
1324d 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20  s written.** to 
1324e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1324f 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  e and the databa
13250 73 65 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20  se file synced. 
13251 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74  The only thing t
13252 68 61 74 0a 2a 2a 20 72 65 6d 61 69 6e 73 20 74  hat.** remains t
13253 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61  o commit the tra
13254 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f 20 64  nsaction is to d
13255 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  elete the journa
13256 6c 20 66 69 6c 65 20 28 6f 72 0a 2a 2a 20 6d 61  l file (or.** ma
13257 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
13258 65 20 69 66 20 73 70 65 63 69 66 69 65 64 29 2e  e if specified).
13259 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
1325a 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 4c   if zMaster==NUL
1325b 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74  L, this does not
1325c 20 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 65   overwrite a pre
1325d 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 70  vious value.** p
1325e 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c 69  assed to an sqli
1325f 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68  te3PagerCommitPh
13260 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a 2a  aseOne() call..*
13261 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e 61  *.** If the fina
13262 6c 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f  l parameter - no
13263 53 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c 20  Sync - is true, 
13264 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  then the databas
13265 65 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a  e file itself.**
13266 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20   is not synced. 
13267 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  The caller must 
13268 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65  call sqlite3Page
13269 72 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c 79  rSync() directly
1326a 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20   to.** sync the 
1326b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65  database file be
1326c 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d  fore calling Com
1326d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 74 6f  mitPhaseTwo() to
1326e 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a   delete the.** j
1326f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74  ournal file in t
13270 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c  his case..*/.SQL
13271 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
13272 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
13273 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50 61  itPhaseOne(.  Pa
13274 67 65 72 20 2a 70 50 61 67 65 72 2c 20 0a 20 20  ger *pPager, .  
13275 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
13276 74 65 72 2c 20 0a 20 20 69 6e 74 20 6e 6f 53 79  ter, .  int noSy
13277 6e 63 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  nc.){.  int rc =
13278 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69   SQLITE_OK;..  i
13279 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
1327a 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  de ){.    return
1327b 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
1327c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e  ;.  }..  /* If n
1327d 6f 20 63 68 61 6e 67 65 73 20 68 61 76 65 20 62  o changes have b
1327e 65 65 6e 20 6d 61 64 65 2c 20 77 65 20 63 61 6e  een made, we can
1327f 20 6c 65 61 76 65 20 74 68 65 20 74 72 61 6e 73   leave the trans
13280 61 63 74 69 6f 6e 20 65 61 72 6c 79 2e 0a 20 20  action early..  
13281 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  */.  if( pPager-
13282 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26  >dbModified==0 &
13283 26 0a 20 20 20 20 20 20 20 20 28 70 50 61 67 65  &.        (pPage
13284 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d  r->journalMode!=
13285 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
13286 45 5f 44 45 4c 45 54 45 20 7c 7c 0a 20 20 20 20  E_DELETE ||.    
13287 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78        pPager->ex
13288 63 6c 75 73 69 76 65 4d 6f 64 65 21 3d 30 29 20  clusiveMode!=0) 
13289 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
1328a 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68  Pager->dirtyCach
1328b 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e  e==0 || pPager->
1328c 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29  journalOpen==0 )
1328d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
1328e 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50  ITE_OK;.  }..  P
1328f 41 47 45 52 54 52 41 43 45 28 28 22 44 41 54 41  AGERTRACE(("DATA
13290 42 41 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d  BASE SYNC: File=
13291 25 73 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 53  %s zMaster=%s nS
13292 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20  ize=%d\n", .    
13293 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e    pPager->zFilen
13294 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50  ame, zMaster, pP
13295 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 29 3b 0a  ager->dbSize));.
13296 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
13297 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62   an in-memory db
13298 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61  , or no pages ha
13299 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  ve been written 
1329a 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20 2a 2a  to, or this.  **
1329b 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c   function has al
1329c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65  ready been calle
1329d 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70  d, it is a no-op
1329e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
1329f 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45  ger->state!=PAGE
132a0 52 5f 53 59 4e 43 45 44 20 26 26 20 21 4d 45 4d  R_SYNCED && !MEM
132a1 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64 69  DB && pPager->di
132a2 72 74 79 43 61 63 68 65 20 29 7b 0a 20 20 20 20  rtyCache ){.    
132a3 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 23 69 66  PgHdr *pPg;..#if
132a4 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
132a5 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20  E_ATOMIC_WRITE. 
132a6 20 20 20 2f 2a 20 54 68 65 20 61 74 6f 6d 69 63     /* The atomic
132a7 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74  -write optimizat
132a8 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20  ion can be used 
132a9 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20  if all of the.  
132aa 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61    ** following a
132ab 72 65 20 74 72 75 65 3a 0a 20 20 20 20 2a 2a 0a  re true:.    **.
132ac 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20      **    + The 
132ad 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70  file-system supp
132ae 6f 72 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d  orts the atomic-
132af 77 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 66  write property f
132b0 6f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62  or.    **      b
132b1 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61  locks of size pa
132b2 67 65 2d 73 69 7a 65 2c 20 61 6e 64 0a 20 20 20  ge-size, and.   
132b3 20 2a 2a 20 20 20 20 2b 20 54 68 69 73 20 63 6f   **    + This co
132b4 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74  mmit is not part
132b5 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65   of a multi-file
132b6 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e   transaction, an
132b7 64 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 45 78  d.    **    + Ex
132b8 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68  actly one page h
132b9 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  as been modified
132ba 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68   and store in th
132bb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
132bc 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66      **.    ** If
132bd 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f   the optimizatio
132be 6e 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 74  n can be used, t
132bf 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hen the journal 
132c0 66 69 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72 0a  file will never.
132c1 20 20 20 20 2a 2a 20 62 65 20 63 72 65 61 74 65      ** be create
132c2 64 20 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73  d for this trans
132c3 61 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20  action..    */. 
132c4 20 20 20 69 6e 74 20 75 73 65 41 74 6f 6d 69 63     int useAtomic
132c5 57 72 69 74 65 3b 0a 20 20 20 20 70 50 67 20 3d  Write;.    pPg =
132c6 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69   sqlite3PcacheDi
132c7 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e  rtyList(pPager->
132c8 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20 75 73  pPCache);.    us
132c9 65 41 74 6f 6d 69 63 57 72 69 74 65 20 3d 20 28  eAtomicWrite = (
132ca 0a 20 20 20 20 20 20 20 20 21 7a 4d 61 73 74 65  .        !zMaste
132cb 72 20 26 26 20 0a 20 20 20 20 20 20 20 20 70 50  r && .        pP
132cc 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
132cd 6e 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61  n &&.        pPa
132ce 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
132cf 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28  =jrnlBufferSize(
132d0 70 50 61 67 65 72 29 20 26 26 20 0a 20 20 20 20  pPager) && .    
132d1 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
132d2 7a 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46 69  ze>=pPager->dbFi
132d3 6c 65 53 69 7a 65 20 26 26 20 0a 20 20 20 20 20  leSize && .     
132d4 20 20 20 28 70 50 67 3d 3d 30 20 7c 7c 20 70 50     (pPg==0 || pP
132d5 67 2d 3e 70 44 69 72 74 79 3d 3d 30 29 0a 20 20  g->pDirty==0).  
132d6 20 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28    );.    assert(
132d7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
132d8 4f 70 65 6e 20 7c 7c 20 70 50 61 67 65 72 2d 3e  Open || pPager->
132d9 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
132da 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
132db 46 46 20 29 3b 0a 20 20 20 20 69 66 28 20 75 73  FF );.    if( us
132dc 65 41 74 6f 6d 69 63 57 72 69 74 65 20 29 7b 0a  eAtomicWrite ){.
132dd 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20        /* Update 
132de 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 69  the nRec field i
132df 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
132e0 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  le. */.      int
132e1 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72   offset = pPager
132e2 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 2b 20 73  ->journalHdr + s
132e3 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
132e4 67 69 63 29 3b 0a 20 20 20 20 20 20 61 73 73 65  gic);.      asse
132e5 72 74 28 70 50 61 67 65 72 2d 3e 6e 52 65 63 3d  rt(pPager->nRec=
132e6 3d 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  =1);.      rc = 
132e7 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
132e8 65 72 2d 3e 6a 66 64 2c 20 6f 66 66 73 65 74 2c  er->jfd, offset,
132e9 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a   pPager->nRec);.
132ea 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65  .      /* Update
132eb 20 74 68 65 20 64 62 20 66 69 6c 65 20 63 68 61   the db file cha
132ec 6e 67 65 20 63 6f 75 6e 74 65 72 2e 20 54 68 65  nge counter. The
132ed 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20   following call 
132ee 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 20 20 20 20  will modify.    
132ef 20 20 2a 2a 20 74 68 65 20 69 6e 2d 6d 65 6d 6f    ** the in-memo
132f0 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ry representatio
132f1 6e 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20 69  n of page 1 to i
132f2 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 61 74  nclude the updat
132f3 65 64 0a 20 20 20 20 20 20 2a 2a 20 63 68 61 6e  ed.      ** chan
132f4 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20 74  ge counter and t
132f5 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20 31  hen write page 1
132f6 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65   directly to the
132f7 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20   database.      
132f8 2a 2a 20 66 69 6c 65 2e 20 42 65 63 61 75 73 65  ** file. Because
132f9 20 6f 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77   of the atomic-w
132fa 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 6f 66  rite property of
132fb 20 74 68 65 20 68 6f 73 74 20 66 69 6c 65 2d 73   the host file-s
132fc 79 73 74 65 6d 2c 20 0a 20 20 20 20 20 20 2a 2a  ystem, .      **
132fd 20 74 68 69 73 20 69 73 20 73 61 66 65 2e 0a 20   this is safe.. 
132fe 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
132ff 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
13300 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
13301 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67  pager_incr_chang
13302 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 2c  ecounter(pPager,
13303 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   1);.      }.   
13304 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
13305 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61   = sqlite3Journa
13306 6c 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e  lCreate(pPager->
13307 6a 66 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  jfd);.    }..   
13308 20 69 66 28 20 21 75 73 65 41 74 6f 6d 69 63 57   if( !useAtomicW
13309 72 69 74 65 20 26 26 20 72 63 3d 3d 53 51 4c 49  rite && rc==SQLI
1330a 54 45 5f 4f 4b 20 29 0a 23 65 6e 64 69 66 0a 0a  TE_OK ).#endif..
1330b 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 73 74      /* If a mast
1330c 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
1330d 6e 61 6d 65 20 68 61 73 20 61 6c 72 65 61 64 79  name has already
1330e 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
1330f 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 72   the.    ** jour
13310 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 6e  nal file, then n
13311 6f 20 73 79 6e 63 20 69 73 20 72 65 71 75 69 72  o sync is requir
13312 65 64 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73  ed. This happens
13313 20 77 68 65 6e 20 69 74 20 69 73 0a 20 20 20 20   when it is.    
13314 2a 2a 20 77 72 69 74 74 65 6e 2c 20 74 68 65 6e  ** written, then
13315 20 74 68 65 20 70 72 6f 63 65 73 73 20 66 61 69   the process fai
13316 6c 73 20 74 6f 20 75 70 67 72 61 64 65 20 66 72  ls to upgrade fr
13317 6f 6d 20 61 20 52 45 53 45 52 56 45 44 20 74 6f  om a RESERVED to
13318 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55   an.    ** EXCLU
13319 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65 20 6e  SIVE lock. The n
1331a 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 72 6f  ext time the pro
1331b 63 65 73 73 20 74 72 69 65 73 20 74 6f 20 63 6f  cess tries to co
1331c 6d 6d 69 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  mmit the.    ** 
1331d 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68 65 20  transaction the 
1331e 6d 2d 6a 20 6e 61 6d 65 20 77 69 6c 6c 20 68 61  m-j name will ha
1331f 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
13320 77 72 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a  written..    */.
13321 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d      if( !pPager-
13322 3e 73 65 74 4d 61 73 74 65 72 20 29 7b 0a 20 20  >setMaster ){.  
13323 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69      rc = pager_i
13324 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65  ncr_changecounte
13325 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20  r(pPager, 0);.  
13326 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
13327 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e  TE_OK ) goto syn
13328 63 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 69 66  c_exit;.      if
13329 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
1332a 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55  lMode!=PAGER_JOU
1332b 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a  RNALMODE_OFF ){.
1332c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1332d 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1332e 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
1332f 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 72  r->dbSize<pPager
13330 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a  ->dbOrigSize ){.
13331 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
13332 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
13333 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61   has made the da
13334 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20  tabase smaller, 
13335 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20  then all pages. 
13336 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 69 6e           ** bein
13337 67 20 64 69 73 63 61 72 64 65 64 20 62 79 20 74  g discarded by t
13338 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d 75  he truncation mu
13339 73 74 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f  st be written to
1333a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20   the journal.   
1333b 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 0a         ** file..
1333c 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
1333d 20 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 0a 20         Pgno i;. 
1333e 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 53           Pgno iS
1333f 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f 50  kip = PAGER_MJ_P
13340 47 4e 4f 28 70 50 61 67 65 72 29 3b 0a 20 20 20  GNO(pPager);.   
13341 20 20 20 20 20 20 20 50 67 6e 6f 20 64 62 53 69         Pgno dbSi
13342 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  ze = pPager->dbS
13343 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70  ize;.          p
13344 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
13345 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69  pPager->dbOrigSi
13346 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  ze;.          fo
13347 72 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69  r( i=dbSize+1; i
13348 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  <=pPager->dbOrig
13349 53 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  Size; i++ ){.   
1334a 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71           if( !sq
1334b 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
1334c 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
1334d 61 6c 2c 20 69 29 20 26 26 20 69 21 3d 69 53 6b  al, i) && i!=iSk
1334e 69 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ip ){.          
1334f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13350 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c  PagerGet(pPager,
13351 20 69 2c 20 26 70 50 67 29 3b 0a 20 20 20 20 20   i, &pPg);.     
13352 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
13353 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
13354 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20  o sync_exit;.   
13355 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
13356 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
13357 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20  e(pPg);.        
13358 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
13359 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20  erUnref(pPg);.  
1335a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1335b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
1335c 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a  goto sync_exit;.
1335d 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1335e 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20          } .     
1335f 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53       pPager->dbS
13360 69 7a 65 20 3d 20 64 62 53 69 7a 65 3b 0a 20 20  ize = dbSize;.  
13361 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
13362 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74         rc = writ
13363 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70  eMasterJournal(p
13364 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b  Pager, zMaster);
13365 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
13366 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
13367 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20  o sync_exit;.   
13368 20 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f       rc = syncJo
13369 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
1336a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1336b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1336c 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65  OK ) goto sync_e
1336d 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 57 72 69  xit;..    /* Wri
1336e 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67  te all dirty pag
1336f 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  es to the databa
13370 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 70  se file */.    p
13371 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63  Pg = sqlite3Pcac
13372 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67  heDirtyList(pPag
13373 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20  er->pPCache);.  
13374 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69    rc = pager_wri
13375 74 65 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29  te_pagelist(pPg)
13376 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
13377 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13378 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
13379 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
1337a 44 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68  D );.      /* Th
1337b 65 20 65 72 72 6f 72 20 6d 69 67 68 74 20 68 61  e error might ha
1337c 76 65 20 6c 65 66 74 20 74 68 65 20 64 69 72 74  ve left the dirt
1337d 79 20 6c 69 73 74 20 61 6c 6c 20 66 6f 75 6c 65  y list all foule
1337e 64 20 75 70 20 68 65 72 65 2c 0a 20 20 20 20 20  d up here,.     
1337f 20 2a 2a 20 62 75 74 20 74 68 61 74 20 64 6f 65   ** but that doe
13380 73 20 6e 6f 74 20 6d 61 74 74 65 72 20 62 65 63  s not matter bec
13381 61 75 73 65 20 69 66 20 74 68 65 20 69 66 20 74  ause if the if t
13382 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 64 69  he dirty list di
13383 64 0a 20 20 20 20 20 20 2a 2a 20 67 65 74 20 63  d.      ** get c
13384 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 74  orrupted, then t
13385 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  he transaction w
13386 69 6c 6c 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6e  ill roll back an
13387 64 0a 20 20 20 20 20 20 2a 2a 20 64 69 73 63 61  d.      ** disca
13388 72 64 20 74 68 65 20 64 69 72 74 79 20 6c 69 73  rd the dirty lis
13389 74 2e 20 20 54 68 65 72 65 20 69 73 20 61 6e 20  t.  There is an 
1338a 61 73 73 65 72 74 20 69 6e 0a 20 20 20 20 20 20  assert in.      
1338b 2a 2a 20 70 61 67 65 72 5f 67 65 74 5f 61 6c 6c  ** pager_get_all
1338c 5f 64 69 72 74 79 5f 70 61 67 65 73 28 29 20 74  _dirty_pages() t
1338d 68 61 74 20 76 65 72 69 66 69 65 73 20 74 68 61  hat verifies tha
1338e 74 20 6e 6f 20 61 74 74 65 6d 70 74 0a 20 20 20  t no attempt.   
1338f 20 20 20 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f     ** is made to
13390 20 75 73 65 20 61 6e 20 69 6e 76 61 6c 69 64 20   use an invalid 
13391 64 69 72 74 79 20 6c 69 73 74 2e 0a 20 20 20 20  dirty list..    
13392 20 20 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20    */.      goto 
13393 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 7d  sync_exit;.    }
13394 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
13395 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65  heCleanAll(pPage
13396 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20  r->pPCache);..  
13397 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62    if( pPager->db
13398 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 46  Size<pPager->dbF
13399 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  ileSize ){.     
1339a 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1339b 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58  >state>=PAGER_EX
1339c 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 20 20 20  CLUSIVE );.     
1339d 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e   rc = pager_trun
1339e 63 61 74 65 28 70 50 61 67 65 72 2c 20 70 50 61  cate(pPager, pPa
1339f 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20  ger->dbSize);.  
133a0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
133a1 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e  TE_OK ) goto syn
133a2 63 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20  c_exit;.    }.. 
133a3 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 64     /* Sync the d
133a4 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f  atabase file. */
133a5 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72  .    if( !pPager
133a6 2d 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53  ->noSync && !noS
133a7 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ync ){.      rc 
133a8 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  = sqlite3OsSync(
133a9 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67  pPager->fd, pPag
133aa 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b  er->sync_flags);
133ab 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41  .    }.    IOTRA
133ac 43 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e  CE(("DBSYNC %p\n
133ad 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20  ", pPager))..   
133ae 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d   pPager->state =
133af 20 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20   PAGER_SYNCED;. 
133b0 20 7d 0a 0a 73 79 6e 63 5f 65 78 69 74 3a 0a 20   }..sync_exit:. 
133b1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
133b2 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 7b  IOERR_BLOCKED ){
133b3 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f 69 6e  .    /* pager_in
133b4 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72  cr_changecounter
133b5 28 29 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74  () may attempt t
133b6 6f 20 6f 62 74 61 69 6e 20 61 6e 20 65 78 63 6c  o obtain an excl
133b7 75 73 69 76 65 0a 20 20 20 20 20 2a 20 6c 6f 63  usive.     * loc
133b8 6b 20 74 6f 20 73 70 69 6c 6c 20 74 68 65 20 63  k to spill the c
133b9 61 63 68 65 20 61 6e 64 20 72 65 74 75 72 6e 20  ache and return 
133ba 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 2e 20 42  IOERR_BLOCKED. B
133bb 75 74 20 73 69 6e 63 65 20 0a 20 20 20 20 20 2a  ut since .     *
133bc 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61   there is no cha
133bd 6e 63 65 20 74 68 65 20 63 61 63 68 65 20 69 73  nce the cache is
133be 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2c 20 69   inconsistent, i
133bf 74 20 69 73 0a 20 20 20 20 20 2a 20 62 65 74 74  t is.     * bett
133c0 65 72 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c  er to return SQL
133c1 49 54 45 5f 42 55 53 59 2e 0a 20 20 20 20 20 2a  ITE_BUSY..     *
133c2 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  /.    rc = SQLIT
133c3 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 72 65  E_BUSY;.  }.  re
133c4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
133c5 2a 2a 20 43 6f 6d 6d 69 74 20 61 6c 6c 20 63 68  ** Commit all ch
133c6 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74  anges to the dat
133c7 61 62 61 73 65 20 61 6e 64 20 72 65 6c 65 61 73  abase and releas
133c8 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b  e the write lock
133c9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  ..**.** If the c
133ca 6f 6d 6d 69 74 20 66 61 69 6c 73 20 66 6f 72 20  ommit fails for 
133cb 61 6e 79 20 72 65 61 73 6f 6e 2c 20 61 20 72 6f  any reason, a ro
133cc 6c 6c 62 61 63 6b 20 61 74 74 65 6d 70 74 20 69  llback attempt i
133cd 73 20 6d 61 64 65 0a 2a 2a 20 61 6e 64 20 61 6e  s made.** and an
133ce 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
133cf 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65  eturned.  If the
133d0 20 63 6f 6d 6d 69 74 20 77 6f 72 6b 65 64 2c 20   commit worked, 
133d1 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20  SQLITE_OK.** is 
133d2 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
133d3 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
133d4 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
133d5 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72  itPhaseTwo(Pager
133d6 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74   *pPager){.  int
133d7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
133d8 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
133d9 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72  errCode ){.    r
133da 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
133db 72 43 6f 64 65 3b 0a 20 20 7d 0a 20 20 69 66 28  rCode;.  }.  if(
133dc 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50   pPager->state<P
133dd 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b  AGER_RESERVED ){
133de 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
133df 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
133e0 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f  if( pPager->dbMo
133e1 64 69 66 69 65 64 3d 3d 30 20 26 26 0a 20 20 20  dified==0 &&.   
133e2 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 6a 6f       (pPager->jo
133e3 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52  urnalMode!=PAGER
133e4 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c  _JOURNALMODE_DEL
133e5 45 54 45 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  ETE ||.         
133e6 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
133e7 76 65 4d 6f 64 65 21 3d 30 29 20 29 7b 0a 20 20  veMode!=0) ){.  
133e8 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
133e9 2d 3e 64 69 72 74 79 43 61 63 68 65 3d 3d 30 20  ->dirtyCache==0 
133ea 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  || pPager->journ
133eb 61 6c 4f 70 65 6e 3d 3d 30 20 29 3b 0a 20 20 20  alOpen==0 );.   
133ec 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
133ed 4b 3b 0a 20 20 7d 0a 20 20 50 41 47 45 52 54 52  K;.  }.  PAGERTR
133ee 41 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c  ACE(("COMMIT %d\
133ef 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
133f0 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28  er)));.  assert(
133f1 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
133f2 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20  PAGER_SYNCED || 
133f3 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65 72  MEMDB || !pPager
133f4 2d 3e 64 69 72 74 79 43 61 63 68 65 20 29 3b 0a  ->dirtyCache );.
133f5 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64    rc = pager_end
133f6 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61  _transaction(pPa
133f7 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74  ger, pPager->set
133f8 4d 61 73 74 65 72 29 3b 0a 20 20 72 63 20 3d 20  Master);.  rc = 
133f9 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
133fa 65 72 2c 20 72 63 29 3b 0a 20 20 72 65 74 75 72  er, rc);.  retur
133fb 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
133fc 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e  ollback all chan
133fd 67 65 73 2e 20 20 54 68 65 20 64 61 74 61 62 61  ges.  The databa
133fe 73 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f  se falls back to
133ff 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d 6f   PAGER_SHARED mo
13400 64 65 2e 0a 2a 2a 20 41 6c 6c 20 69 6e 2d 6d 65  de..** All in-me
13401 6d 6f 72 79 20 63 61 63 68 65 20 70 61 67 65 73  mory cache pages
13402 20 72 65 76 65 72 74 20 74 6f 20 74 68 65 69 72   revert to their
13403 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 20 63   original data c
13404 6f 6e 74 65 6e 74 73 2e 0a 2a 2a 20 54 68 65 20  ontents..** The 
13405 6a 6f 75 72 6e 61 6c 20 69 73 20 64 65 6c 65 74  journal is delet
13406 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
13407 6f 75 74 69 6e 65 20 63 61 6e 6e 6f 74 20 66 61  outine cannot fa
13408 69 6c 20 75 6e 6c 65 73 73 20 73 6f 6d 65 20 6f  il unless some o
13409 74 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 20  ther process is 
1340a 6e 6f 74 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  not following.**
1340b 20 74 68 65 20 63 6f 72 72 65 63 74 20 6c 6f 63   the correct loc
1340c 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c 20 6f 72  king protocol or
1340d 20 75 6e 6c 65 73 73 20 73 6f 6d 65 20 6f 74 68   unless some oth
1340e 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 73  er.** process is
1340f 20 77 72 69 74 69 6e 67 20 74 72 61 73 68 20 69   writing trash i
13410 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
13411 66 69 6c 65 20 28 53 51 4c 49 54 45 5f 43 4f 52  file (SQLITE_COR
13412 52 55 50 54 29 20 6f 72 0a 2a 2a 20 75 6e 6c 65  RUPT) or.** unle
13413 73 73 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f  ss a prior mallo
13414 63 28 29 20 66 61 69 6c 65 64 20 28 53 51 4c 49  c() failed (SQLI
13415 54 45 5f 4e 4f 4d 45 4d 29 2e 20 20 41 70 70 72  TE_NOMEM).  Appr
13416 6f 70 72 69 61 74 65 20 65 72 72 6f 72 0a 2a 2a  opriate error.**
13417 20 63 6f 64 65 73 20 61 72 65 20 72 65 74 75 72   codes are retur
13418 6e 65 64 20 66 6f 72 20 61 6c 6c 20 74 68 65 73  ned for all thes
13419 65 20 6f 63 63 61 73 69 6f 6e 73 2e 20 20 4f 74  e occasions.  Ot
1341a 68 65 72 77 69 73 65 2c 0a 2a 2a 20 53 51 4c 49  herwise,.** SQLI
1341b 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
1341c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1341d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1341e 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61  PagerRollback(Pa
1341f 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
13420 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
13421 4f 4b 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45  OK;.  PAGERTRACE
13422 28 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e  (("ROLLBACK %d\n
13423 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
13424 72 29 29 29 3b 0a 20 20 69 66 28 20 21 70 50 61  r)));.  if( !pPa
13425 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20  ger->dirtyCache 
13426 7c 7c 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72  || !pPager->jour
13427 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 72  nalOpen ){.    r
13428 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72  c = pager_end_tr
13429 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72  ansaction(pPager
1342a 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73  , pPager->setMas
1342b 74 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ter);.  }else if
1342c 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
1342d 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72  e && pPager->err
1342e 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c  Code!=SQLITE_FUL
1342f 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  L ){.    if( pPa
13430 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
13431 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20  R_EXCLUSIVE ){. 
13432 20 20 20 20 20 70 61 67 65 72 5f 70 6c 61 79 62       pager_playb
13433 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  ack(pPager, 0);.
13434 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70      }.    rc = p
13435 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
13436 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
13437 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
13438 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29  PAGER_RESERVED )
13439 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b  {.      int rc2;
1343a 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
1343b 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65  r_playback(pPage
1343c 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32  r, 0);.      rc2
1343d 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
1343e 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
1343f 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
13440 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  er);.      if( r
13441 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13442 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32          rc = rc2
13443 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
13444 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
13445 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70  pager_playback(p
13446 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d  Pager, 0);.    }
13447 0a 0a 20 20 20 20 69 66 28 20 21 4d 45 4d 44 42  ..    if( !MEMDB
13448 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
13449 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20  ->dbSizeValid = 
1344a 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  0;.    }..    /*
1344b 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
1344c 75 72 73 20 64 75 72 69 6e 67 20 61 20 52 4f 4c  urs during a ROL
1344d 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f  LBACK, we can no
1344e 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 74 68   longer trust th
1344f 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 63  e pager.    ** c
13450 61 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61  ache. So call pa
13451 67 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74  ger_error() on t
13452 68 65 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61  he way out to ma
13453 6b 65 20 61 6e 79 20 65 72 72 6f 72 20 0a 20 20  ke any error .  
13454 20 20 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 2e    ** persistent.
13455 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
13456 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61   pager_error(pPa
13457 67 65 72 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20  ger, rc);.  }.  
13458 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
13459 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
1345a 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  if the database 
1345b 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 72  file is opened r
1345c 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72  ead-only.  Retur
1345d 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68  n FALSE.** if th
1345e 65 20 64 61 74 61 62 61 73 65 20 69 73 20 28 69  e database is (i
1345f 6e 20 74 68 65 6f 72 79 29 20 77 72 69 74 61 62  n theory) writab
13460 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
13461 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33  IVATE u8 sqlite3
13462 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28  PagerIsreadonly(
13463 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
13464 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
13465 3e 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a  >readOnly;.}../*
13466 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
13467 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e  umber of referen
13468 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 72  ces to the pager
13469 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1346a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
1346b 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67  agerRefcount(Pag
1346c 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
1346d 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61  eturn sqlite3Pca
1346e 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
1346f 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a  er->pPCache);.}.
13470 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
13471 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  e number of refe
13472 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 70  rences to the sp
13473 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f  ecified page..*/
13474 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13475 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
13476 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50  PageRefcount(DbP
13477 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 72  age *pPage){.  r
13478 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61  eturn sqlite3Pca
13479 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28  chePageRefcount(
1347a 70 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65  pPage);.}..#ifde
1347b 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
1347c 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1347d 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
1347e 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69  ting and analysi
1347f 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  s only..*/.SQLIT
13480 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 2a 73  E_PRIVATE int *s
13481 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74 73  qlite3PagerStats
13482 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
13483 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 61 5b  .  static int a[
13484 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71  11];.  a[0] = sq
13485 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
13486 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
13487 63 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73  che);.  a[1] = s
13488 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65  qlite3PcachePage
13489 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50  count(pPager->pP
1348a 43 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d  Cache);.  a[2] =
1348b 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65   sqlite3PcacheGe
1348c 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65  tCachesize(pPage
1348d 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61  r->pPCache);.  a
1348e 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 64 62  [3] = pPager->db
1348f 53 69 7a 65 56 61 6c 69 64 20 3f 20 28 69 6e 74  SizeValid ? (int
13490 29 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ) pPager->dbSize
13491 20 3a 20 2d 31 3b 0a 20 20 61 5b 34 5d 20 3d 20   : -1;.  a[4] = 
13492 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20  pPager->state;. 
13493 20 61 5b 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[5] = pPager->
13494 65 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20  errCode;.  a[6] 
13495 3d 20 70 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a  = pPager->nHit;.
13496 20 20 61 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d    a[7] = pPager-
13497 3e 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d  >nMiss;.  a[8] =
13498 20 30 3b 20 20 2f 2a 20 55 73 65 64 20 74 6f 20   0;  /* Used to 
13499 62 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c  be pPager->nOvfl
1349a 20 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61   */.  a[9] = pPa
1349b 67 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b  ger->nRead;.  a[
1349c 31 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57  10] = pPager->nW
1349d 72 69 74 65 3b 0a 20 20 72 65 74 75 72 6e 20 61  rite;.  return a
1349e 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
1349f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
134a0 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72  gerIsMemdb(Pager
134a1 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
134a2 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 23 65 6e  urn MEMDB;.}.#en
134a3 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72  dif../*.** Ensur
134a4 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65  e that there are
134a5 20 61 74 20 6c 65 61 73 74 20 6e 53 61 76 65 70   at least nSavep
134a6 6f 69 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20  oint savepoints 
134a7 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  open..*/.SQLITE_
134a8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
134a9 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65  te3PagerOpenSave
134aa 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61  point(Pager *pPa
134ab 67 65 72 2c 20 69 6e 74 20 6e 53 61 76 65 70 6f  ger, int nSavepo
134ac 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  int){.  int rc =
134ad 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69   SQLITE_OK;..  i
134ae 66 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e 70 50  f( nSavepoint>pP
134af 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
134b0 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 4a   && pPager->useJ
134b1 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69 6e  ournal ){.    in
134b2 74 20 69 69 3b 0a 20 20 20 20 50 61 67 65 72 53  t ii;.    PagerS
134b3 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 0a  avepoint *aNew;.
134b4 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74  .    /* Either t
134b5 68 65 72 65 20 69 73 20 6e 6f 20 61 63 74 69 76  here is no activ
134b6 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65  e journal or the
134b7 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20   sub-journal is 
134b8 6f 70 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a 20  open or .    ** 
134b9 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61  the journal is a
134ba 6c 77 61 79 73 20 73 74 6f 72 65 64 20 69 6e 20  lways stored in 
134bb 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61 73  memory */.    as
134bc 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53  sert( pPager->nS
134bd 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 70  avepoint==0 || p
134be 50 61 67 65 72 2d 3e 73 6a 66 64 2d 3e 70 4d 65  Pager->sjfd->pMe
134bf 74 68 6f 64 73 20 7c 7c 0a 20 20 20 20 20 20 20  thods ||.       
134c0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
134c1 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
134c2 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
134c3 52 59 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 72  RY );..    /* Gr
134c4 6f 77 20 74 68 65 20 50 61 67 65 72 2e 61 53 61  ow the Pager.aSa
134c5 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 75 73  vepoint array us
134c6 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52  ing realloc(). R
134c7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
134c8 45 4d 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65  EM.    ** if the
134c9 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
134ca 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 7a 65  s. Otherwise, ze
134cb 72 6f 20 74 68 65 20 6e 65 77 20 70 6f 72 74 69  ro the new porti
134cc 6f 6e 20 69 6e 20 63 61 73 65 20 61 20 0a 20 20  on in case a .  
134cd 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c    ** malloc fail
134ce 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ure occurs while
134cf 20 70 6f 70 75 6c 61 74 69 6e 67 20 69 74 20 69   populating it i
134d0 6e 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c  n the for(...) l
134d1 6f 6f 70 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a  oop below..    *
134d2 2f 0a 20 20 20 20 61 4e 65 77 20 3d 20 28 50 61  /.    aNew = (Pa
134d3 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 29 73  gerSavepoint *)s
134d4 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20  qlite3Realloc(. 
134d5 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61         pPager->a
134d6 53 61 76 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f  Savepoint, sizeo
134d7 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  f(PagerSavepoint
134d8 29 2a 6e 53 61 76 65 70 6f 69 6e 74 0a 20 20 20  )*nSavepoint.   
134d9 20 29 3b 0a 20 20 20 20 69 66 28 20 21 61 4e 65   );.    if( !aNe
134da 77 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  w ){.      retur
134db 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
134dc 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74      }.    memset
134dd 28 26 61 4e 65 77 5b 70 50 61 67 65 72 2d 3e 6e  (&aNew[pPager->n
134de 53 61 76 65 70 6f 69 6e 74 5d 2c 20 30 2c 0a 20  Savepoint], 0,. 
134df 20 20 20 20 20 20 20 28 6e 53 61 76 65 70 6f 69         (nSavepoi
134e0 6e 74 20 2d 20 70 50 61 67 65 72 2d 3e 6e 53 61  nt - pPager->nSa
134e1 76 65 70 6f 69 6e 74 29 20 2a 20 73 69 7a 65 6f  vepoint) * sizeo
134e2 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  f(PagerSavepoint
134e3 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 50 61  ).    );.    pPa
134e4 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20  ger->aSavepoint 
134e5 3d 20 61 4e 65 77 3b 0a 20 20 20 20 69 69 20 3d  = aNew;.    ii =
134e6 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f   pPager->nSavepo
134e7 69 6e 74 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  int;.    pPager-
134e8 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e 53  >nSavepoint = nS
134e9 61 76 65 70 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f  avepoint;..    /
134ea 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 50  * Populate the P
134eb 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 73 74  agerSavepoint st
134ec 72 75 63 74 75 72 65 73 20 6a 75 73 74 20 61 6c  ructures just al
134ed 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 20 20 20 20  located. */.    
134ee 66 6f 72 28 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 3b  for(/* no-op */;
134ef 20 69 69 3c 6e 53 61 76 65 70 6f 69 6e 74 3b 20   ii<nSavepoint; 
134f0 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73  ii++){.      ass
134f1 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53  ert( pPager->dbS
134f2 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20  izeValid );.    
134f3 20 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69 67    aNew[ii].nOrig
134f4 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a   = pPager->dbSiz
134f5 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  e;.      if( pPa
134f6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e  ger->journalOpen
134f7 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   && pPager->jour
134f8 6e 61 6c 4f 66 66 3e 30 20 29 7b 0a 20 20 20 20  nalOff>0 ){.    
134f9 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66      aNew[ii].iOf
134fa 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a  fset = pPager->j
134fb 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 20  ournalOff;.     
134fc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
134fd 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74  aNew[ii].iOffset
134fe 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53   = JOURNAL_HDR_S
134ff 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  Z(pPager);.     
13500 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69   }.      aNew[ii
13501 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 50 61 67  ].iSubRec = pPag
13502 65 72 2d 3e 73 74 6d 74 4e 52 65 63 3b 0a 20 20  er->stmtNRec;.  
13503 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 6e      aNew[ii].pIn
13504 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c 69  Savepoint = sqli
13505 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28  te3BitvecCreate(
13506 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b  pPager->dbSize);
13507 0a 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 77  .      if( !aNew
13508 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e  [ii].pInSavepoin
13509 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  t ){.        ret
1350a 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1350b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1350c 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65  .    /* Open the
1350d 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 66   sub-journal, if
1350e 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61   it is not alrea
1350f 64 79 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20  dy opened. */.  
13510 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f    rc = openSubJo
13511 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
13512 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
13513 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65  .}../*.** Parame
13514 74 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 73  ter op is always
13515 20 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e   either SAVEPOIN
13516 54 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41  T_ROLLBACK or SA
13517 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e  VEPOINT_RELEASE.
13518 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 56  .** If it is SAV
13519 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20  EPOINT_RELEASE, 
1351a 74 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e 64  then release and
1351b 20 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76   destroy the sav
1351c 65 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69  epoint with.** i
1351d 6e 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 2e  ndex iSavepoint.
1351e 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f   If it is SAVEPO
1351f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68  INT_ROLLBACK, th
13520 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20  en rollback all 
13521 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20  changes.** that 
13522 68 61 76 65 20 6f 63 63 75 72 65 64 20 73 69 6e  have occured sin
13523 63 65 20 73 61 76 65 70 6f 69 6e 74 20 69 53 61  ce savepoint iSa
13524 76 65 70 6f 69 6e 74 20 77 61 73 20 63 72 65 61  vepoint was crea
13525 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69  ted..**.** In ei
13526 74 68 65 72 20 63 61 73 65 2c 20 61 6c 6c 20 73  ther case, all s
13527 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68 20 61  avepoints with a
13528 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72 20  n index greater 
13529 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e 74 20  than iSavepoint 
1352a 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f 79 65  .** are destroye
1352b 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  d..**.** If ther
1352c 65 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20  e are less than 
1352d 28 69 53 61 76 65 70 6f 69 6e 74 2b 31 29 20 61  (iSavepoint+1) a
1352e 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73  ctive savepoints
1352f 20 77 68 65 6e 20 74 68 69 73 20 0a 2a 2a 20 66   when this .** f
13530 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
13531 64 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  d it is a no-op.
13532 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/ .SQLITE_PRIV
13533 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
13534 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 50 61  agerSavepoint(Pa
13535 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
13536 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f   op, int iSavepo
13537 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  int){.  int rc =
13538 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61   SQLITE_OK;..  a
13539 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45 50  ssert( op==SAVEP
1353a 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20  OINT_RELEASE || 
1353b 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  op==SAVEPOINT_RO
1353c 4c 4c 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28  LLBACK );..  if(
1353d 20 69 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67   iSavepoint<pPag
1353e 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29  er->nSavepoint )
1353f 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20  {.    int ii;.  
13540 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 69 53 61    int nNew = iSa
13541 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70 3d 3d 53  vepoint + (op==S
13542 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
13543 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e  K);.    for(ii=n
13544 4e 65 77 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e  New; ii<pPager->
13545 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b  nSavepoint; ii++
13546 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
13547 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50  BitvecDestroy(pP
13548 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
13549 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e  [ii].pInSavepoin
1354a 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  t);.    }.    pP
1354b 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
1354c 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 69 66   = nNew;..    if
1354d 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  ( op==SAVEPOINT_
1354e 52 4f 4c 4c 42 41 43 4b 20 26 26 20 70 50 61 67  ROLLBACK && pPag
1354f 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f 64  er->jfd->pMethod
13550 73 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72  s ){.      Pager
13551 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65  Savepoint *pSave
13552 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30  point = (nNew==0
13553 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61  )?0:&pPager->aSa
13554 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b  vepoint[nNew-1];
13555 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
13556 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69  rPlaybackSavepoi
13557 6e 74 28 70 50 61 67 65 72 2c 20 70 53 61 76 65  nt(pPager, pSave
13558 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73  point);.      as
13559 73 65 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f  sert(rc!=SQLITE_
1355a 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a  DONE);.    }.  .
1355b 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
1355c 73 20 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74  s a release of t
1355d 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76  he outermost sav
1355e 65 70 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65  epoint, truncate
1355f 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62   .    ** the sub
13560 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20  -journal. */.   
13561 20 69 66 28 20 6e 4e 65 77 3d 3d 30 20 26 26 20   if( nNew==0 && 
13562 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  op==SAVEPOINT_RE
13563 4c 45 41 53 45 20 26 26 20 70 50 61 67 65 72 2d  LEASE && pPager-
13564 3e 73 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20  >sjfd->pMethods 
13565 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
13566 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13567 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
13568 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
13569 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30 29 3b  Pager->sjfd, 0);
1356a 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73  .      pPager->s
1356b 74 6d 74 4e 52 65 63 20 3d 20 30 3b 0a 20 20 20  tmtNRec = 0;.   
1356c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1356d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
1356e 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74  urn the full pat
1356f 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74  hname of the dat
13570 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 53  abase file..*/.S
13571 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
13572 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
13573 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50  3PagerFilename(P
13574 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
13575 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
13576 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a  zFilename;.}../*
13577 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 56  .** Return the V
13578 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  FS structure for
13579 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53   the pager..*/.S
1357a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
1357b 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 20  nst sqlite3_vfs 
1357c 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 73  *sqlite3PagerVfs
1357d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
1357e 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72  .  return pPager
1357f 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ->pVfs;.}../*.**
13580 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c 65   Return the file
13581 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20   handle for the 
13582 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 73  database file as
13583 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
13584 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 69   the pager.  Thi
13585 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 4e  s might return N
13586 55 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65 20  ULL if the file 
13587 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 62  has.** not yet b
13588 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 53  een opened..*/.S
13589 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
1358a 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69  lite3_file *sqli
1358b 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67  te3PagerFile(Pag
1358c 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
1358d 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66 64  eturn pPager->fd
1358e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1358f 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  n the directory 
13590 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
13591 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  file..*/.SQLITE_
13592 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
13593 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  ar *sqlite3Pager
13594 44 69 72 6e 61 6d 65 28 50 61 67 65 72 20 2a 70  Dirname(Pager *p
13595 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
13596 20 70 50 61 67 65 72 2d 3e 7a 44 69 72 65 63 74   pPager->zDirect
13597 6f 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ory;.}../*.** Re
13598 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61  turn the full pa
13599 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  thname of the jo
1359a 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53  urnal file..*/.S
1359b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
1359c 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1359d 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d  3PagerJournalnam
1359e 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  e(Pager *pPager)
1359f 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
135a0 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a  r->zJournal;.}..
135a1 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
135a2 65 20 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c  e if fsync() cal
135a3 6c 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20  ls are disabled 
135a4 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20  for this pager. 
135a5 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a   Return FALSE.**
135a6 20 69 66 20 66 73 79 6e 63 28 29 73 20 61 72 65   if fsync()s are
135a7 20 65 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c   executed normal
135a8 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
135a9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
135aa 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67  3PagerNosync(Pag
135ab 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
135ac 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f  eturn pPager->no
135ad 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  Sync;.}..#ifdef 
135ae 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
135af 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63  ./*.** Set the c
135b0 6f 64 65 63 20 66 6f 72 20 74 68 69 73 20 70 61  odec for this pa
135b1 67 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ger.*/.SQLITE_PR
135b2 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
135b3 65 33 50 61 67 65 72 53 65 74 43 6f 64 65 63 28  e3PagerSetCodec(
135b4 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
135b5 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64  ,.  void *(*xCod
135b6 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c  ec)(void*,void*,
135b7 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 6f 69  Pgno,int),.  voi
135b8 64 20 2a 70 43 6f 64 65 63 41 72 67 0a 29 7b 0a  d *pCodecArg.){.
135b9 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63    pPager->xCodec
135ba 20 3d 20 78 43 6f 64 65 63 3b 0a 20 20 70 50 61   = xCodec;.  pPa
135bb 67 65 72 2d 3e 70 43 6f 64 65 63 41 72 67 20 3d  ger->pCodecArg =
135bc 20 70 43 6f 64 65 63 41 72 67 3b 0a 7d 0a 23 65   pCodecArg;.}.#e
135bd 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
135be 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
135bf 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  CUUM./*.** Move 
135c0 74 68 65 20 70 61 67 65 20 70 50 67 20 74 6f 20  the page pPg to 
135c1 6c 6f 63 61 74 69 6f 6e 20 70 67 6e 6f 20 69 6e  location pgno in
135c2 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
135c3 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e   There must be n
135c4 6f 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  o references to 
135c5 74 68 65 20 70 61 67 65 20 70 72 65 76 69 6f 75  the page previou
135c6 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a  sly located at.*
135c7 2a 20 70 67 6e 6f 20 28 77 68 69 63 68 20 77 65  * pgno (which we
135c8 20 63 61 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68   call pPgOld) th
135c9 6f 75 67 68 20 74 68 61 74 20 70 61 67 65 20 69  ough that page i
135ca 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0a  s allowed to be.
135cb 2a 2a 20 69 6e 20 63 61 63 68 65 2e 20 20 49 66  ** in cache.  If
135cc 20 74 68 65 20 70 61 67 65 20 70 72 65 76 69 6f   the page previo
135cd 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 20  usly located at 
135ce 70 67 6e 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65  pgno is not alre
135cf 61 64 79 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f  ady.** in the ro
135d0 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20  llback journal, 
135d1 69 74 20 69 73 20 6e 6f 74 20 70 75 74 20 74 68  it is not put th
135d2 65 72 65 20 62 79 20 62 79 20 74 68 69 73 20 72  ere by by this r
135d3 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65  outine..**.** Re
135d4 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
135d5 70 61 67 65 20 70 50 67 20 72 65 6d 61 69 6e 20  page pPg remain 
135d6 76 61 6c 69 64 2e 20 55 70 64 61 74 69 6e 67 20  valid. Updating 
135d7 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61  any.** meta-data
135d8 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
135d9 20 70 50 67 20 28 69 2e 65 2e 20 64 61 74 61 20   pPg (i.e. data 
135da 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 45  stored in the nE
135db 78 74 72 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c  xtra bytes.** al
135dc 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69  located along wi
135dd 74 68 20 74 68 65 20 70 61 67 65 29 20 69 73 20  th the page) is 
135de 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  the responsibili
135df 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72  ty of the caller
135e0 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61  ..**.** A transa
135e1 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 63  ction must be ac
135e2 74 69 76 65 20 77 68 65 6e 20 74 68 69 73 20 72  tive when this r
135e3 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
135e4 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 0a  . It used to be.
135e5 2a 2a 20 72 65 71 75 69 72 65 64 20 74 68 61 74  ** required that
135e6 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61   a statement tra
135e7 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74  nsaction was not
135e8 20 61 63 74 69 76 65 2c 20 62 75 74 20 74 68 69   active, but thi
135e9 73 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a  s restriction.**
135ea 20 68 61 73 20 62 65 65 6e 20 72 65 6d 6f 76 65   has been remove
135eb 64 20 28 43 52 45 41 54 45 20 49 4e 44 45 58 20  d (CREATE INDEX 
135ec 6e 65 65 64 73 20 74 6f 20 6d 6f 76 65 20 61 20  needs to move a 
135ed 70 61 67 65 20 77 68 65 6e 20 61 20 73 74 61 74  page when a stat
135ee 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63  ement.** transac
135ef 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 29 2e  tion is active).
135f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f  .**.** If the fo
135f1 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69  urth argument, i
135f2 73 43 6f 6d 6d 69 74 2c 20 69 73 20 6e 6f 6e 2d  sCommit, is non-
135f3 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 69 73 20  zero, then this 
135f4 70 61 67 65 20 69 73 20 62 65 69 6e 67 0a 2a 2a  page is being.**
135f5 20 6d 6f 76 65 64 20 61 73 20 70 61 72 74 20 6f   moved as part o
135f6 66 20 61 20 64 61 74 61 62 61 73 65 20 72 65 6f  f a database reo
135f7 72 67 61 6e 69 7a 61 74 69 6f 6e 20 6a 75 73 74  rganization just
135f8 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e   before the tran
135f9 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 62  saction .** is b
135fa 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65 64 2e 20  eing committed. 
135fb 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 69 74  In this case, it
135fc 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
135fd 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
135fe 20 70 61 67 65 20 0a 2a 2a 20 70 50 67 20 72 65   page .** pPg re
135ff 66 65 72 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74  fers to will not
13600 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 61   be written to a
13601 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73  gain within this
13602 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f   transaction..*/
13603 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13604 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
13605 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 20 2a  Movepage(Pager *
13606 70 50 61 67 65 72 2c 20 44 62 50 61 67 65 20 2a  pPager, DbPage *
13607 70 50 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  pPg, Pgno pgno, 
13608 69 6e 74 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20  int isCommit){. 
13609 20 50 67 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20   PgHdr *pPgOld; 
1360a 20 2f 2a 20 54 68 65 20 70 61 67 65 20 62 65 69   /* The page bei
1360b 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20  ng overwritten. 
1360c 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65 64 53 79  */.  Pgno needSy
1360d 6e 63 50 67 6e 6f 20 3d 20 30 3b 0a 20 20 69 6e  ncPgno = 0;.  in
1360e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
1360f 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a   pPg->nRef>0 );.
13610 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67  .  /* If the pag
13611 65 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 69 73  e being moved is
13612 20 64 69 72 74 79 20 61 6e 64 20 68 61 73 20 6e   dirty and has n
13613 6f 74 20 62 65 65 6e 20 73 61 76 65 64 20 62 79  ot been saved by
13614 20 74 68 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a   the latest.  **
13615 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e   savepoint, then
13616 20 73 61 76 65 20 74 68 65 20 63 75 72 72 65 6e   save the curren
13617 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  t contents of th
13618 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  e page into the 
13619 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61  .  ** sub-journa
1361a 6c 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20 72  l now. This is r
1361b 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c  equired to handl
1361c 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
1361d 73 63 65 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20  scenario:.  **. 
1361e 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a   **   BEGIN;.  *
1361f 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70  *     <journal p
13620 61 67 65 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69  age X, then modi
13621 66 79 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e  fy it in memory>
13622 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f  .  **     SAVEPO
13623 49 4e 54 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20  INT one;.  **   
13624 20 20 20 20 3c 4d 6f 76 65 20 70 61 67 65 20 58      <Move page X
13625 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a   to location Y>.
13626 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43    **     ROLLBAC
13627 4b 20 54 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20  K TO one;.  **. 
13628 20 2a 2a 20 49 66 20 70 61 67 65 20 58 20 77 65   ** If page X we
13629 72 65 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74  re not written t
1362a 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  o the sub-journa
1362b 6c 20 68 65 72 65 2c 20 69 74 20 77 6f 75 6c 64  l here, it would
1362c 20 6e 6f 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73   not.  ** be pos
1362d 73 69 62 6c 65 20 74 6f 20 72 65 73 74 6f 72 65  sible to restore
1362e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 77 68   its contents wh
1362f 65 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b  en the "ROLLBACK
13630 20 54 4f 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74   TO one".  ** st
13631 61 74 65 6d 65 6e 74 20 77 65 72 65 20 70 72 6f  atement were pro
13632 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  cessed..  */.  i
13633 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  f( pPg->flags&PG
13634 48 44 52 5f 44 49 52 54 59 20 0a 20 20 20 26 26  HDR_DIRTY .   &&
13635 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67   subjRequiresPag
13636 65 28 70 50 67 29 0a 20 20 20 26 26 20 53 51 4c  e(pPg).   && SQL
13637 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 75  ITE_OK!=(rc = su
13638 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67  bjournalPage(pPg
13639 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  )).  ){.    retu
1363a 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 50 41  rn rc;.  }..  PA
1363b 47 45 52 54 52 41 43 45 28 28 22 4d 4f 56 45 20  GERTRACE(("MOVE 
1363c 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 65 64  %d page %d (need
1363d 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 74  Sync=%d) moves t
1363e 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20  o %d\n", .      
1363f 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
13640 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50 67   pPg->pgno, (pPg
13641 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
13642 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70 67  ED_SYNC)?1:0, pg
13643 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28  no));.  IOTRACE(
13644 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25 64 5c  ("MOVE %p %d %d\
13645 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d  n", pPager, pPg-
13646 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20  >pgno, pgno)).. 
13647 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e 74 65   pager_get_conte
13648 6e 74 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 49  nt(pPg);..  /* I
13649 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65  f the journal ne
1364a 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29  eds to be sync()
1364b 65 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70  ed before page p
1364c 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a  Pg->pgno can.  *
1364d 2a 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c  * be written to,
1364e 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f   store pPg->pgno
1364f 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   in local variab
13650 6c 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e  le needSyncPgno.
13651 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
13652 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20  e isCommit flag 
13653 69 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73  is set, there is
13654 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65   no need to reme
13655 6d 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74  mber that.  ** t
13656 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73  he journal needs
13657 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20   to be sync()ed 
13658 62 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20  before database 
13659 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a  page pPg->pgno .
1365a 20 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74    ** can be writ
1365b 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c  ten to. The call
1365c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70  er has already p
1365d 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77  romised not to w
1365e 72 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f  rite to it..  */
1365f 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61  .  if( (pPg->fla
13660 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
13661 4e 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74  NC) && !isCommit
13662 20 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63   ){.    needSync
13663 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f  Pgno = pPg->pgno
13664 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61  ;.    assert( pa
13665 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29  geInJournal(pPg)
13666 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50   || pPg->pgno>pP
13667 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
13668 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
13669 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
1366a 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73  _DIRTY );.    as
1366b 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65  sert( pPager->ne
1366c 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20  edSync );.  }.. 
1366d 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65   /* If the cache
1366e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65   contains a page
1366f 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65   with page-numbe
13670 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69  r pgno, remove i
13671 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20  t.  ** from its 
13672 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f  hash chain. Also
13673 2c 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e  , if the PgHdr.n
13674 65 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20  eedSync was set 
13675 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70  for .  ** page p
13676 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27  gno before the '
13677 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c  move' operation,
13678 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20   it needs to be 
13679 72 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66  retained .  ** f
1367a 6f 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65  or the page move
1367b 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20  d there..  */.  
1367c 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50  pPg->flags &= ~P
1367d 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a  GHDR_NEED_SYNC;.
1367e 20 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72    pPgOld = pager
1367f 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20  _lookup(pPager, 
13680 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28  pgno);.  assert(
13681 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f   !pPgOld || pPgO
13682 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20  ld->nRef==1 );. 
13683 20 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20   if( pPgOld ){. 
13684 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d     pPg->flags |=
13685 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26   (pPgOld->flags&
13686 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
13687 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
13688 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 2c 20  PcacheMove(pPg, 
13689 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 50 67  pgno);.  if( pPg
1368a 4f 6c 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Old ){.    sqlit
1368b 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 67  e3PcacheDrop(pPg
1368c 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  Old);.  }..  sql
1368d 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69  ite3PcacheMakeDi
1368e 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50 61 67  rty(pPg);.  pPag
1368f 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 3d  er->dirtyCache =
13690 20 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62   1;.  pPager->db
13691 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 0a 20  Modified = 1;.. 
13692 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e   if( needSyncPgn
13693 6f 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e  o ){.    /* If n
13694 65 65 64 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e  eedSyncPgno is n
13695 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  on-zero, then th
13696 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e  e journal file n
13697 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20 20 20  eeds to be .    
13698 2a 2a 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f  ** sync()ed befo
13699 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 77  re any data is w
1369a 72 69 74 74 65 6e 20 74 6f 20 64 61 74 61 62 61  ritten to databa
1369b 73 65 20 66 69 6c 65 20 70 61 67 65 20 6e 65 65  se file page nee
1369c 64 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a  dSyncPgno..    *
1369d 2a 20 43 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20  * Currently, no 
1369e 73 75 63 68 20 70 61 67 65 20 65 78 69 73 74 73  such page exists
1369f 20 69 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63   in the page-cac
136a0 68 65 20 61 6e 64 20 74 68 65 20 0a 20 20 20 20  he and the .    
136a1 2a 2a 20 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64  ** "is journaled
136a2 22 20 62 69 74 76 65 63 20 66 6c 61 67 20 68 61  " bitvec flag ha
136a3 73 20 62 65 65 6e 20 73 65 74 2e 20 54 68 69 73  s been set. This
136a4 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d   needs to be rem
136a5 65 64 69 65 64 20 62 79 0a 20 20 20 20 2a 2a 20  edied by.    ** 
136a6 6c 6f 61 64 69 6e 67 20 74 68 65 20 70 61 67 65  loading the page
136a7 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65 72 2d   into the pager-
136a8 63 61 63 68 65 20 61 6e 64 20 73 65 74 74 69 6e  cache and settin
136a9 67 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64  g the PgHdr.need
136aa 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 66 6c 61  Sync .    ** fla
136ab 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  g..    **.    **
136ac 20 49 66 20 74 68 65 20 61 74 74 65 6d 70 74 20   If the attempt 
136ad 74 6f 20 6c 6f 61 64 20 74 68 65 20 70 61 67 65  to load the page
136ae 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65 2d 63   into the page-c
136af 61 63 68 65 20 66 61 69 6c 73 2c 20 28 64 75 65  ache fails, (due
136b0 0a 20 20 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c  .    ** to a mal
136b1 6c 6f 63 28 29 20 6f 72 20 49 4f 20 66 61 69 6c  loc() or IO fail
136b2 75 72 65 29 2c 20 63 6c 65 61 72 20 74 68 65 20  ure), clear the 
136b3 62 69 74 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f  bit in the pInJo
136b4 75 72 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61  urnal[].    ** a
136b5 72 72 61 79 2e 20 4f 74 68 65 72 77 69 73 65 2c  rray. Otherwise,
136b6 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
136b7 6c 6f 61 64 65 64 20 61 6e 64 20 77 72 69 74 74  loaded and writt
136b8 65 6e 20 61 67 61 69 6e 20 69 6e 0a 20 20 20 20  en again in.    
136b9 2a 2a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  ** this transact
136ba 69 6f 6e 2c 20 69 74 20 6d 61 79 20 62 65 20 77  ion, it may be w
136bb 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
136bc 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f  tabase file befo
136bd 72 65 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20  re.    ** it is 
136be 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20  synced into the 
136bf 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68  journal file. Th
136c0 69 73 20 77 61 79 2c 20 69 74 20 6d 61 79 20 65  is way, it may e
136c1 6e 64 20 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20  nd up in.    ** 
136c2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
136c3 20 74 77 69 63 65 2c 20 62 75 74 20 74 68 61 74   twice, but that
136c4 20 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65   is not a proble
136c5 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  m..    **.    **
136c6 20 54 68 65 20 73 71 6c 69 74 65 33 50 61 67 65   The sqlite3Page
136c7 72 47 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20  rGet() call may 
136c8 63 61 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61  cause the journa
136c9 6c 20 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61  l to sync. So ma
136ca 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74  ke.    ** sure t
136cb 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e  he Pager.needSyn
136cc 63 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f  c flag is set to
136cd 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 50 67  o..    */.    Pg
136ce 48 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 20  Hdr *pPgHdr;.   
136cf 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
136d0 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 20  >needSync );.   
136d1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
136d2 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 6e 65  erGet(pPager, ne
136d3 65 64 53 79 6e 63 50 67 6e 6f 2c 20 26 70 50 67  edSyncPgno, &pPg
136d4 48 64 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  Hdr);.    if( rc
136d5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
136d6 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d       if( pPager-
136d7 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 26 26 20 6e  >pInJournal && n
136d8 65 65 64 53 79 6e 63 50 67 6e 6f 3c 3d 70 50 61  eedSyncPgno<=pPa
136d9 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20  ger->dbOrigSize 
136da 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
136db 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 50  e3BitvecClear(pP
136dc 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
136dd 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 29 3b  , needSyncPgno);
136de 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
136df 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
136e0 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64      pPager->need
136e1 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 61 73  Sync = 1;.    as
136e2 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 6f  sert( pPager->no
136e3 53 79 6e 63 3d 3d 30 20 26 26 20 21 4d 45 4d 44  Sync==0 && !MEMD
136e4 42 20 29 3b 0a 20 20 20 20 70 50 67 48 64 72 2d  B );.    pPgHdr-
136e5 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f  >flags |= PGHDR_
136e6 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73  NEED_SYNC;.    s
136e7 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
136e8 44 69 72 74 79 28 70 50 67 48 64 72 29 3b 0a 20  Dirty(pPgHdr);. 
136e9 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
136ea 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 20  nref(pPgHdr);.  
136eb 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
136ec 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
136ed 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
136ee 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 64  pointer to the d
136ef 61 74 61 20 66 6f 72 20 74 68 65 20 73 70 65 63  ata for the spec
136f0 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53  ified page..*/.S
136f1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
136f2 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
136f3 47 65 74 44 61 74 61 28 44 62 50 61 67 65 20 2a  GetData(DbPage *
136f4 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20  pPg){.  assert( 
136f5 70 50 67 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20 70  pPg->nRef>0 || p
136f6 50 67 2d 3e 70 50 61 67 65 72 2d 3e 6d 65 6d 44  Pg->pPager->memD
136f7 62 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50  b );.  return pP
136f8 67 2d 3e 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a  g->pData;.}../*.
136f9 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
136fa 74 65 72 20 74 6f 20 74 68 65 20 50 61 67 65 72  ter to the Pager
136fb 2e 6e 45 78 74 72 61 20 62 79 74 65 73 20 6f 66  .nExtra bytes of
136fc 20 22 65 78 74 72 61 22 20 73 70 61 63 65 20 0a   "extra" space .
136fd 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f  ** allocated alo
136fe 6e 67 20 77 69 74 68 20 74 68 65 20 73 70 65 63  ng with the spec
136ff 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53  ified page..*/.S
13700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
13701 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
13702 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20  GetExtra(DbPage 
13703 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a  *pPg){.  Pager *
13704 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50  pPager = pPg->pP
13705 61 67 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 28  ager;.  return (
13706 70 50 61 67 65 72 3f 70 50 67 2d 3e 70 45 78 74  pPager?pPg->pExt
13707 72 61 3a 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ra:0);.}../*.** 
13708 47 65 74 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b  Get/set the lock
13709 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69  ing-mode for thi
1370a 73 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65 74  s pager. Paramet
1370b 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 65  er eMode must be
1370c 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52   one.** of PAGER
1370d 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45  _LOCKINGMODE_QUE
1370e 52 59 2c 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  RY, PAGER_LOCKIN
1370f 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20  GMODE_NORMAL or 
13710 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  .** PAGER_LOCKIN
13711 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2e  GMODE_EXCLUSIVE.
13712 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   If the paramete
13713 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c  r is not _QUERY,
13714 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63   then.** the loc
13715 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 20 73 65 74  king-mode is set
13716 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 70   to the value sp
13717 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  ecified..**.** T
13718 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
13719 65 20 69 73 20 65 69 74 68 65 72 20 50 41 47 45  e is either PAGE
1371a 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f  R_LOCKINGMODE_NO
1371b 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52  RMAL or.** PAGER
1371c 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
1371d 4c 55 53 49 56 45 2c 20 69 6e 64 69 63 61 74 69  LUSIVE, indicati
1371e 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 28  ng the current (
1371f 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64  possibly updated
13720 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64  ).** locking-mod
13721 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
13722 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
13723 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65  PagerLockingMode
13724 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
13725 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73  int eMode){.  as
13726 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47  sert( eMode==PAG
13727 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51  ER_LOCKINGMODE_Q
13728 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20  UERY.           
13729 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   || eMode==PAGER
1372a 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52  _LOCKINGMODE_NOR
1372b 4d 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20  MAL.            
1372c 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
1372d 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
1372e 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72  USIVE );.  asser
1372f 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  t( PAGER_LOCKING
13730 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a  MODE_QUERY<0 );.
13731 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
13732 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
13733 41 4c 3e 3d 30 20 26 26 20 50 41 47 45 52 5f 4c  AL>=0 && PAGER_L
13734 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
13735 53 49 56 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28  SIVE>=0 );.  if(
13736 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 70 50   eMode>=0 && !pP
13737 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
13738 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78  {.    pPager->ex
13739 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75  clusiveMode = (u
1373a 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72  8)eMode;.  }.  r
1373b 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65  eturn (int)pPage
1373c 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
1373d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73  ;.}../*.** Get/s
1373e 65 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d  et the journal-m
1373f 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67  ode for this pag
13740 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d  er. Parameter eM
13741 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20  ode must be one 
13742 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47  of:.**.**    PAG
13743 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51  ER_JOURNALMODE_Q
13744 55 45 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52  UERY.**    PAGER
13745 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c  _JOURNALMODE_DEL
13746 45 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  ETE.**    PAGER_
13747 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e  JOURNALMODE_TRUN
13748 43 41 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52  CATE.**    PAGER
13749 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52  _JOURNALMODE_PER
1374a 53 49 53 54 0a 2a 2a 20 20 20 20 50 41 47 45 52  SIST.**    PAGER
1374b 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
1374c 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
1374d 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 5f  rameter is not _
1374e 51 55 45 52 59 2c 20 74 68 65 6e 20 74 68 65 20  QUERY, then the 
1374f 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 69 73 20  journal-mode is 
13750 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61  set to the.** va
13751 6c 75 65 20 73 70 65 63 69 66 69 65 64 2e 0a 2a  lue specified..*
13752 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  *.** The returne
13753 64 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 63  d indicate the c
13754 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c 79  urrent (possibly
13755 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6a 6f 75   updated).** jou
13756 72 6e 61 6c 2d 6d 6f 64 65 2e 0a 2a 2f 0a 53 51  rnal-mode..*/.SQ
13757 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13758 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
13759 72 6e 61 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a  rnalMode(Pager *
1375a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f 64  pPager, int eMod
1375b 65 29 7b 0a 20 20 69 66 28 20 21 4d 45 4d 44 42  e){.  if( !MEMDB
1375c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1375d 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
1375e 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 20  RNALMODE_QUERY. 
1375f 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
13760 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
13761 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a  RNALMODE_DELETE.
13762 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
13763 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
13764 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41  URNALMODE_TRUNCA
13765 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  TE.             
13766 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   || eMode==PAGER
13767 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52  _JOURNALMODE_PER
13768 53 49 53 54 0a 20 20 20 20 20 20 20 20 20 20 20  SIST.           
13769 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
1376a 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
1376b 46 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  FF .            
1376c 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45    || eMode==PAGE
1376d 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
1376e 4d 4f 52 59 20 29 3b 0a 20 20 20 20 61 73 73 65  MORY );.    asse
1376f 72 74 28 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  rt( PAGER_JOURNA
13770 4c 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b  LMODE_QUERY<0 );
13771 0a 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3e 3d  .    if( eMode>=
13772 30 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  0 ){.      pPage
13773 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d  r->journalMode =
13774 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 20 20   (u8)eMode;.    
13775 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
13776 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45  ert( eMode==PAGE
13777 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
13778 45 52 59 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ERY );.    }.  }
13779 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70  .  return (int)p
1377a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
1377b 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  de;.}../*.** Get
1377c 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69  /set the size-li
1377d 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70 65 72  mit used for per
1377e 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  sistent journal 
1377f 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  files..*/.SQLITE
13780 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71 6c  _PRIVATE i64 sql
13781 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
13782 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 72 20  SizeLimit(Pager 
13783 2a 70 50 61 67 65 72 2c 20 69 36 34 20 69 4c 69  *pPager, i64 iLi
13784 6d 69 74 29 7b 0a 20 20 69 66 28 20 69 4c 69 6d  mit){.  if( iLim
13785 69 74 3e 3d 2d 31 20 29 7b 0a 20 20 20 20 70 50  it>=-1 ){.    pP
13786 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a  ager->journalSiz
13787 65 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74 3b  eLimit = iLimit;
13788 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 50  .  }.  return pP
13789 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a  ager->journalSiz
1378a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a 23 65 6e 64 69  eLimit;.}..#endi
1378b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1378c 5f 44 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a  _DISKIO */../***
1378d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1378e 6f 66 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a  of pager.c *****
1378f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13791 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
13792 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
13793 6e 20 66 69 6c 65 20 62 74 6d 75 74 65 78 2e 63  n file btmutex.c
13794 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
13795 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13796 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
13797 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 37  * 2007 August 27
13798 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
13799 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1379a 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1379b 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1379c 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1379d 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1379e 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1379f 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
137a0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
137a1 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
137a2 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
137a3 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
137a4 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
137a5 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
137a6 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
137a7 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
137a8 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
137a9 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
137aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
137ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
137ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
137ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
137ae 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 6d 75 74  **.** $Id: btmut
137af 65 78 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 38  ex.c,v 1.12 2008
137b0 2f 31 31 2f 31 37 20 31 39 3a 31 38 3a 35 35 20  /11/17 19:18:55 
137b1 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
137b2 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  $.**.** This fil
137b3 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
137b4 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
137b5 74 20 6d 75 74 65 78 65 73 20 6f 6e 20 42 74 72  t mutexes on Btr
137b6 65 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54  ee objects..** T
137b7 68 69 73 20 63 6f 64 65 20 72 65 61 6c 6c 79 20  his code really 
137b8 62 65 6c 6f 6e 67 73 20 69 6e 20 62 74 72 65 65  belongs in btree
137b9 2e 63 2e 20 20 42 75 74 20 62 74 72 65 65 2e 63  .c.  But btree.c
137ba 20 69 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 0a   is getting too.
137bb 2a 2a 20 62 69 67 20 61 6e 64 20 77 65 20 77 61  ** big and we wa
137bc 6e 74 20 74 6f 20 62 72 65 61 6b 20 69 74 20 64  nt to break it d
137bd 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68 69 73 20  own some.  This 
137be 70 61 63 6b 61 67 65 64 20 73 65 65 6d 65 64 20  packaged seemed 
137bf 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f 6f 64 20 62  like.** a good b
137c0 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f 2a 2a 2a  reakout..*/./***
137c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
137c2 75 64 65 20 62 74 72 65 65 49 6e 74 2e 68 20 69  ude btreeInt.h i
137c3 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
137c4 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a  btmutex.c ******
137c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
137c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
137c7 6e 20 66 69 6c 65 20 62 74 72 65 65 49 6e 74 2e  n file btreeInt.
137c8 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
137c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
137ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
137cb 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a  * 2004 April 6.*
137cc 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
137cd 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
137ce 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
137cf 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
137d0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
137d1 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
137d2 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
137d3 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
137d4 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
137d5 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
137d6 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
137d7 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
137d8 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
137d9 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
137da 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
137db 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
137dc 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
137dd 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
137de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
137df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
137e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
137e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
137e2 20 24 49 64 3a 20 62 74 72 65 65 49 6e 74 2e 68   $Id: btreeInt.h
137e3 2c 76 20 31 2e 33 38 20 32 30 30 38 2f 31 32 2f  ,v 1.38 2008/12/
137e4 32 37 20 31 35 3a 32 33 3a 31 33 20 64 61 6e 69  27 15:23:13 dani
137e5 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a  elk1977 Exp $.**
137e6 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
137e7 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65 72  plements a exter
137e8 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 29  nal (disk-based)
137e9 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20   database using 
137ea 42 54 72 65 65 73 2e 0a 2a 2a 20 46 6f 72 20 61  BTrees..** For a
137eb 20 64 65 74 61 69 6c 65 64 20 64 69 73 63 75 73   detailed discus
137ec 73 69 6f 6e 20 6f 66 20 42 54 72 65 65 73 2c 20  sion of BTrees, 
137ed 72 65 66 65 72 20 74 6f 0a 2a 2a 0a 2a 2a 20 20  refer to.**.**  
137ee 20 20 20 44 6f 6e 61 6c 64 20 45 2e 20 4b 6e 75     Donald E. Knu
137ef 74 68 2c 20 54 48 45 20 41 52 54 20 4f 46 20 43  th, THE ART OF C
137f0 4f 4d 50 55 54 45 52 20 50 52 4f 47 52 41 4d 4d  OMPUTER PROGRAMM
137f1 49 4e 47 2c 20 56 6f 6c 75 6d 65 20 33 3a 0a 2a  ING, Volume 3:.*
137f2 2a 20 20 20 20 20 22 53 6f 72 74 69 6e 67 20 41  *     "Sorting A
137f3 6e 64 20 53 65 61 72 63 68 69 6e 67 22 2c 20 70  nd Searching", p
137f4 61 67 65 73 20 34 37 33 2d 34 38 30 2e 20 41 64  ages 473-480. Ad
137f5 64 69 73 6f 6e 2d 57 65 73 6c 65 79 0a 2a 2a 20  dison-Wesley.** 
137f6 20 20 20 20 50 75 62 6c 69 73 68 69 6e 67 20 43      Publishing C
137f7 6f 6d 70 61 6e 79 2c 20 52 65 61 64 69 6e 67 2c  ompany, Reading,
137f8 20 4d 61 73 73 61 63 68 75 73 65 74 74 73 2e 0a   Massachusetts..
137f9 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20  **.** The basic 
137fa 69 64 65 61 20 69 73 20 74 68 61 74 20 65 61 63  idea is that eac
137fb 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69  h page of the fi
137fc 6c 65 20 63 6f 6e 74 61 69 6e 73 20 4e 20 64 61  le contains N da
137fd 74 61 62 61 73 65 0a 2a 2a 20 65 6e 74 72 69 65  tabase.** entrie
137fe 73 20 61 6e 64 20 4e 2b 31 20 70 6f 69 6e 74 65  s and N+1 pointe
137ff 72 73 20 74 6f 20 73 75 62 70 61 67 65 73 2e 0a  rs to subpages..
13800 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  **.**   --------
13801 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13802 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13803 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13804 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 7c 20  --------.**   | 
13805 20 50 74 72 28 30 29 20 7c 20 4b 65 79 28 30 29   Ptr(0) | Key(0)
13806 20 7c 20 50 74 72 28 31 29 20 7c 20 4b 65 79 28   | Ptr(1) | Key(
13807 31 29 20 7c 20 2e 2e 2e 20 7c 20 4b 65 79 28 4e  1) | ... | Key(N
13808 2d 31 29 20 7c 20 50 74 72 28 4e 29 20 7c 0a 2a  -1) | Ptr(N) |.*
13809 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *   ------------
1380a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1380b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1380c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1380d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f  ----.**.** All o
1380e 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 74 68  f the keys on th
1380f 65 20 70 61 67 65 20 74 68 61 74 20 50 74 72 28  e page that Ptr(
13810 30 29 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 76  0) points to hav
13811 65 20 76 61 6c 75 65 73 20 6c 65 73 73 0a 2a 2a  e values less.**
13812 20 74 68 61 6e 20 4b 65 79 28 30 29 2e 20 20 41   than Key(0).  A
13813 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 6f  ll of the keys o
13814 6e 20 70 61 67 65 20 50 74 72 28 31 29 20 61 6e  n page Ptr(1) an
13815 64 20 69 74 73 20 73 75 62 70 61 67 65 73 20 68  d its subpages h
13816 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 20 67 72  ave.** values gr
13817 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 30  eater than Key(0
13818 29 20 61 6e 64 20 6c 65 73 73 20 74 68 61 6e 20  ) and less than 
13819 4b 65 79 28 31 29 2e 20 20 41 6c 6c 20 6f 66 20  Key(1).  All of 
1381a 74 68 65 20 6b 65 79 73 0a 2a 2a 20 6f 6e 20 50  the keys.** on P
1381b 74 72 28 4e 29 20 61 6e 64 20 69 74 73 20 73 75  tr(N) and its su
1381c 62 70 61 67 65 73 20 68 61 76 65 20 76 61 6c 75  bpages have valu
1381d 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  es greater than 
1381e 4b 65 79 28 4e 2d 31 29 2e 20 20 41 6e 64 0a 2a  Key(N-1).  And.*
1381f 2a 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a 2a  * so forth..**.*
13820 2a 20 46 69 6e 64 69 6e 67 20 61 20 70 61 72 74  * Finding a part
13821 69 63 75 6c 61 72 20 6b 65 79 20 72 65 71 75 69  icular key requi
13822 72 65 73 20 72 65 61 64 69 6e 67 20 4f 28 6c 6f  res reading O(lo
13823 67 28 4d 29 29 20 70 61 67 65 73 20 66 72 6f 6d  g(M)) pages from
13824 20 74 68 65 20 0a 2a 2a 20 64 69 73 6b 20 77 68   the .** disk wh
13825 65 72 65 20 4d 20 69 73 20 74 68 65 20 6e 75 6d  ere M is the num
13826 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
13827 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a  n the tree..**.*
13828 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d  * In this implem
13829 65 6e 74 61 74 69 6f 6e 2c 20 61 20 73 69 6e 67  entation, a sing
1382a 6c 65 20 66 69 6c 65 20 63 61 6e 20 68 6f 6c 64  le file can hold
1382b 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 65 70   one or more sep
1382c 61 72 61 74 65 20 0a 2a 2a 20 42 54 72 65 65 73  arate .** BTrees
1382d 2e 20 20 45 61 63 68 20 42 54 72 65 65 20 69 73  .  Each BTree is
1382e 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
1382f 68 65 20 69 6e 64 65 78 20 6f 66 20 69 74 73 20  he index of its 
13830 72 6f 6f 74 20 70 61 67 65 2e 20 20 54 68 65 0a  root page.  The.
13831 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20  ** key and data 
13832 66 6f 72 20 61 6e 79 20 65 6e 74 72 79 20 61 72  for any entry ar
13833 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f  e combined to fo
13834 72 6d 20 74 68 65 20 22 70 61 79 6c 6f 61 64 22  rm the "payload"
13835 2e 20 20 41 0a 2a 2a 20 66 69 78 65 64 20 61 6d  .  A.** fixed am
13836 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
13837 63 61 6e 20 62 65 20 63 61 72 72 69 65 64 20 64  can be carried d
13838 69 72 65 63 74 6c 79 20 6f 6e 20 74 68 65 20 64  irectly on the d
13839 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 2e  atabase.** page.
1383a 20 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64    If the payload
1383b 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
1383c 74 68 65 20 70 72 65 73 65 74 20 61 6d 6f 75 6e  the preset amoun
1383d 74 20 74 68 65 6e 20 73 75 72 70 6c 75 73 0a 2a  t then surplus.*
1383e 2a 20 62 79 74 65 73 20 61 72 65 20 73 74 6f 72  * bytes are stor
1383f 65 64 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70  ed on overflow p
13840 61 67 65 73 2e 20 20 54 68 65 20 70 61 79 6c 6f  ages.  The paylo
13841 61 64 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 0a  ad for an entry.
13842 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 65 63 65  ** and the prece
13843 64 69 6e 67 20 70 6f 69 6e 74 65 72 20 61 72 65  ding pointer are
13844 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f 72   combined to for
13845 6d 20 61 20 22 43 65 6c 6c 22 2e 20 20 45 61 63  m a "Cell".  Eac
13846 68 20 0a 2a 2a 20 70 61 67 65 20 68 61 73 20 61  h .** page has a
13847 20 73 6d 61 6c 6c 20 68 65 61 64 65 72 20 77 68   small header wh
13848 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ich contains the
13849 20 50 74 72 28 4e 29 20 70 6f 69 6e 74 65 72 20   Ptr(N) pointer 
1384a 61 6e 64 20 6f 74 68 65 72 0a 2a 2a 20 69 6e 66  and other.** inf
1384b 6f 72 6d 61 74 69 6f 6e 20 73 75 63 68 20 61 73  ormation such as
1384c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 6b 65 79   the size of key
1384d 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a   and data..**.**
1384e 20 46 4f 52 4d 41 54 20 44 45 54 41 49 4c 53 0a   FORMAT DETAILS.
1384f 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 69  **.** The file i
13850 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 70  s divided into p
13851 61 67 65 73 2e 20 20 54 68 65 20 66 69 72 73 74  ages.  The first
13852 20 70 61 67 65 20 69 73 20 63 61 6c 6c 65 64 20   page is called 
13853 70 61 67 65 20 31 2c 0a 2a 2a 20 74 68 65 20 73  page 1,.** the s
13854 65 63 6f 6e 64 20 69 73 20 70 61 67 65 20 32 2c  econd is page 2,
13855 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20   and so forth.  
13856 41 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  A page number of
13857 20 7a 65 72 6f 20 69 6e 64 69 63 61 74 65 73 0a   zero indicates.
13858 2a 2a 20 22 6e 6f 20 73 75 63 68 20 70 61 67 65  ** "no such page
13859 22 2e 20 20 54 68 65 20 70 61 67 65 20 73 69 7a  ".  The page siz
1385a 65 20 63 61 6e 20 62 65 20 61 6e 79 74 68 69 6e  e can be anythin
1385b 67 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e  g between 512 an
1385c 64 20 36 35 35 33 36 2e 0a 2a 2a 20 45 61 63 68  d 65536..** Each
1385d 20 70 61 67 65 20 63 61 6e 20 62 65 20 65 69 74   page can be eit
1385e 68 65 72 20 61 20 62 74 72 65 65 20 70 61 67 65  her a btree page
1385f 2c 20 61 20 66 72 65 65 6c 69 73 74 20 70 61 67  , a freelist pag
13860 65 20 6f 72 20 61 6e 20 6f 76 65 72 66 6c 6f 77  e or an overflow
13861 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  .** page..**.** 
13862 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69  The first page i
13863 73 20 61 6c 77 61 79 73 20 61 20 62 74 72 65 65  s always a btree
13864 20 70 61 67 65 2e 20 20 54 68 65 20 66 69 72 73   page.  The firs
13865 74 20 31 30 30 20 62 79 74 65 73 20 6f 66 20 74  t 100 bytes of t
13866 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65  he first.** page
13867 20 63 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69   contain a speci
13868 61 6c 20 68 65 61 64 65 72 20 28 74 68 65 20 22  al header (the "
13869 66 69 6c 65 20 68 65 61 64 65 72 22 29 20 74 68  file header") th
1386a 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65  at describes the
1386b 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f   file..** The fo
1386c 72 6d 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65  rmat of the file
1386d 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f   header is as fo
1386e 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f  llows:.**.**   O
1386f 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 20 20  FFSET   SIZE    
13870 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20  DESCRIPTION.**  
13871 20 20 20 20 30 20 20 20 20 20 20 31 36 20 20 20      0      16   
13872 20 20 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a    Header string:
13873 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20   "SQLite format 
13874 33 5c 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36  3\000".**     16
13875 20 20 20 20 20 20 20 32 20 20 20 20 20 50 61 67         2     Pag
13876 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e  e size in bytes.
13877 20 20 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20    .**     18    
13878 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f     1     File fo
13879 72 6d 61 74 20 77 72 69 74 65 20 76 65 72 73 69  rmat write versi
1387a 6f 6e 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20  on.**     19    
1387b 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f     1     File fo
1387c 72 6d 61 74 20 72 65 61 64 20 76 65 72 73 69 6f  rmat read versio
1387d 6e 0a 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20  n.**     20     
1387e 20 20 31 20 20 20 20 20 42 79 74 65 73 20 6f 66    1     Bytes of
1387f 20 75 6e 75 73 65 64 20 73 70 61 63 65 20 61 74   unused space at
13880 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68   the end of each
13881 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20   page.**     21 
13882 20 20 20 20 20 20 31 20 20 20 20 20 4d 61 78 20        1     Max 
13883 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
13884 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20   fraction.**    
13885 20 32 32 20 20 20 20 20 20 20 31 20 20 20 20 20   22       1     
13886 4d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79  Min embedded pay
13887 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a  load fraction.**
13888 20 20 20 20 20 32 33 20 20 20 20 20 20 20 31 20       23       1 
13889 20 20 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79      Min leaf pay
1388a 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a  load fraction.**
1388b 20 20 20 20 20 32 34 20 20 20 20 20 20 20 34 20       24       4 
1388c 20 20 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20      File change 
1388d 63 6f 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32  counter.**     2
1388e 38 20 20 20 20 20 20 20 34 20 20 20 20 20 52 65  8       4     Re
1388f 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72  served for futur
13890 65 20 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20  e use.**     32 
13891 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73        4     Firs
13892 74 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a  t freelist page.
13893 2a 2a 20 20 20 20 20 33 36 20 20 20 20 20 20 20  **     36       
13894 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20  4     Number of 
13895 66 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 69  freelist pages i
13896 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20  n the file.**   
13897 20 20 34 30 20 20 20 20 20 20 36 30 20 20 20 20    40      60    
13898 20 31 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20   15 4-byte meta 
13899 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f  values passed to
1389a 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a   higher layers.*
1389b 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20  *.** All of the 
1389c 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 61  integer values a
1389d 72 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 28 6d  re big-endian (m
1389e 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20  ost significant 
1389f 62 79 74 65 20 66 69 72 73 74 29 2e 0a 2a 2a 0a  byte first)..**.
138a0 2a 2a 20 54 68 65 20 66 69 6c 65 20 63 68 61 6e  ** The file chan
138a1 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 69 6e  ge counter is in
138a2 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 20 74  cremented when t
138a3 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63  he database is c
138a4 68 61 6e 67 65 64 0a 2a 2a 20 54 68 69 73 20 63  hanged.** This c
138a5 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 73 20 6f 74  ounter allows ot
138a6 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f  her processes to
138a7 20 6b 6e 6f 77 20 77 68 65 6e 20 74 68 65 20 66   know when the f
138a8 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a  ile has changed.
138a9 2a 2a 20 61 6e 64 20 74 68 75 73 20 77 68 65 6e  ** and thus when
138aa 20 74 68 65 79 20 6e 65 65 64 20 74 6f 20 66 6c   they need to fl
138ab 75 73 68 20 74 68 65 69 72 20 63 61 63 68 65 2e  ush their cache.
138ac 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 20 65  .**.** The max e
138ad 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20  mbedded payload 
138ae 66 72 61 63 74 69 6f 6e 20 69 73 20 74 68 65 20  fraction is the 
138af 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74 6f  amount of the to
138b0 74 61 6c 20 75 73 61 62 6c 65 0a 2a 2a 20 73 70  tal usable.** sp
138b1 61 63 65 20 69 6e 20 61 20 70 61 67 65 20 74 68  ace in a page th
138b2 61 74 20 63 61 6e 20 62 65 20 63 6f 6e 73 75 6d  at can be consum
138b3 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 63  ed by a single c
138b4 65 6c 6c 20 66 6f 72 20 73 74 61 6e 64 61 72 64  ell for standard
138b5 0a 2a 2a 20 42 2d 74 72 65 65 20 28 6e 6f 6e 2d  .** B-tree (non-
138b6 4c 45 41 46 44 41 54 41 29 20 74 61 62 6c 65 73  LEAFDATA) tables
138b7 2e 20 20 41 20 76 61 6c 75 65 20 6f 66 20 32 35  .  A value of 25
138b8 35 20 6d 65 61 6e 73 20 31 30 30 25 2e 20 20 54  5 means 100%.  T
138b9 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 73  he default.** is
138ba 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6d 61   to limit the ma
138bb 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20  ximum cell size 
138bc 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74  so that at least
138bd 20 34 20 63 65 6c 6c 73 20 77 69 6c 6c 20 66 69   4 cells will fi
138be 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67 65  t.** on one page
138bf 2e 20 20 54 68 75 73 20 74 68 65 20 64 65 66 61  .  Thus the defa
138c0 75 6c 74 20 6d 61 78 20 65 6d 62 65 64 64 65 64  ult max embedded
138c1 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
138c2 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a 2a 2a 20 49  n is 64..**.** I
138c3 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 66 6f  f the payload fo
138c4 72 20 61 20 63 65 6c 6c 20 69 73 20 6c 61 72 67  r a cell is larg
138c5 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 20  er than the max 
138c6 70 61 79 6c 6f 61 64 2c 20 74 68 65 6e 20 65 78  payload, then ex
138c7 74 72 61 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 69  tra.** payload i
138c8 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 6f 76 65  s spilled to ove
138c9 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 4f 6e  rflow pages.  On
138ca 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  ce an overflow p
138cb 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  age is allocated
138cc 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 20 62 79 74  ,.** as many byt
138cd 65 73 20 61 73 20 70 6f 73 73 69 62 6c 65 20 61  es as possible a
138ce 72 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68  re moved into th
138cf 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  e overflow pages
138d0 20 77 69 74 68 6f 75 74 20 6c 65 74 74 69 6e 67   without letting
138d1 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 73 69 7a  .** the cell siz
138d2 65 20 64 72 6f 70 20 62 65 6c 6f 77 20 74 68 65  e drop below the
138d3 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61   min embedded pa
138d4 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 2e 0a  yload fraction..
138d5 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 20 6c 65  **.** The min le
138d6 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  af payload fract
138d7 69 6f 6e 20 69 73 20 6c 69 6b 65 20 74 68 65 20  ion is like the 
138d8 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79  min embedded pay
138d9 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a  load fraction.**
138da 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
138db 61 70 70 6c 69 65 73 20 74 6f 20 6c 65 61 66 20  applies to leaf 
138dc 6e 6f 64 65 73 20 69 6e 20 61 20 4c 45 41 46 44  nodes in a LEAFD
138dd 41 54 41 20 74 72 65 65 2e 20 20 54 68 65 20 6d  ATA tree.  The m
138de 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 79 6c 6f 61  aximum.** payloa
138df 64 20 66 72 61 63 74 69 6f 6e 20 66 6f 72 20 61  d fraction for a
138e0 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20 69   LEAFDATA tree i
138e1 73 20 61 6c 77 61 79 73 20 31 30 30 25 20 28 6f  s always 100% (o
138e2 72 20 32 35 35 29 20 61 6e 64 20 69 74 0a 2a 2a  r 255) and it.**
138e3 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 69   not specified i
138e4 6e 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a  n the header..**
138e5 0a 2a 2a 20 45 61 63 68 20 62 74 72 65 65 20 70  .** Each btree p
138e6 61 67 65 73 20 69 73 20 64 69 76 69 64 65 64 20  ages is divided 
138e7 69 6e 74 6f 20 74 68 72 65 65 20 73 65 63 74 69  into three secti
138e8 6f 6e 73 3a 20 20 54 68 65 20 68 65 61 64 65 72  ons:  The header
138e9 2c 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 20 70 6f  , the.** cell po
138ea 69 6e 74 65 72 20 61 72 72 61 79 2c 20 61 6e 64  inter array, and
138eb 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
138ec 74 20 61 72 65 61 2e 20 20 50 61 67 65 20 31 20  t area.  Page 1 
138ed 61 6c 73 6f 20 68 61 73 20 61 20 31 30 30 2d 62  also has a 100-b
138ee 79 74 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64  yte.** file head
138ef 65 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 62  er that occurs b
138f0 65 66 6f 72 65 20 74 68 65 20 70 61 67 65 20 68  efore the page h
138f1 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  eader..**.**    
138f2 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
138f3 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 66  ---|.**      | f
138f4 69 6c 65 20 68 65 61 64 65 72 20 20 20 20 7c 20  ile header    | 
138f5 20 20 31 30 30 20 62 79 74 65 73 2e 20 20 50 61    100 bytes.  Pa
138f6 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a 2a 20 20 20  ge 1 only..**   
138f7 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     |------------
138f8 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20  ----|.**      | 
138f9 70 61 67 65 20 68 65 61 64 65 72 20 20 20 20 7c  page header    |
138fa 20 20 20 38 20 62 79 74 65 73 20 66 6f 72 20 6c     8 bytes for l
138fb 65 61 76 65 73 2e 20 20 31 32 20 62 79 74 65 73  eaves.  12 bytes
138fc 20 66 6f 72 20 69 6e 74 65 72 69 6f 72 20 6e 6f   for interior no
138fd 64 65 73 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d  des.**      |---
138fe 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a  -------------|.*
138ff 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 70 6f  *      | cell po
13900 69 6e 74 65 72 20 20 20 7c 20 20 20 7c 20 20 32  inter   |   |  2
13901 20 62 79 74 65 73 20 70 65 72 20 63 65 6c 6c 2e   bytes per cell.
13902 20 20 53 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a    Sorted order..
13903 2a 2a 20 20 20 20 20 20 7c 20 61 72 72 61 79 20  **      | array 
13904 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20           |   |  
13905 47 72 6f 77 73 20 64 6f 77 6e 77 61 72 64 0a 2a  Grows downward.*
13906 2a 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20  *      |        
13907 20 20 20 20 20 20 20 20 7c 20 20 20 76 0a 2a 2a          |   v.**
13908 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d        |---------
13909 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20  -------|.**     
1390a 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20 20   | unallocated  
1390b 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 73 70    |.**      | sp
1390c 61 63 65 20 20 20 20 20 20 20 20 20 20 7c 0a 2a  ace          |.*
1390d 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
1390e 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e 20 20 47  --------|   ^  G
1390f 72 6f 77 73 20 75 70 77 61 72 64 73 0a 2a 2a 20  rows upwards.** 
13910 20 20 20 20 20 7c 20 63 65 6c 6c 20 63 6f 6e 74       | cell cont
13911 65 6e 74 20 20 20 7c 20 20 20 7c 20 20 41 72 62  ent   |   |  Arb
13912 69 74 72 61 72 79 20 6f 72 64 65 72 20 69 6e 74  itrary order int
13913 65 72 73 70 65 72 73 65 64 20 77 69 74 68 20 66  erspersed with f
13914 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20  reeblocks..**   
13915 20 20 20 7c 20 61 72 65 61 20 20 20 20 20 20 20     | area       
13916 20 20 20 20 7c 20 20 20 7c 20 20 61 6e 64 20 66      |   |  and f
13917 72 65 65 20 73 70 61 63 65 20 66 72 61 67 6d 65  ree space fragme
13918 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d  nts..**      |--
13919 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a  --------------|.
1391a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 68  **.** The page h
1391b 65 61 64 65 72 73 20 6c 6f 6f 6b 73 20 6c 69 6b  eaders looks lik
1391c 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
1391d 4f 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 20  OFFSET   SIZE   
1391e 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a    DESCRIPTION.**
1391f 20 20 20 20 20 20 30 20 20 20 20 20 20 20 31 20        0       1 
13920 20 20 20 20 20 46 6c 61 67 73 2e 20 31 3a 20 69       Flags. 1: i
13921 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 72 6f 64 61  ntkey, 2: zeroda
13922 74 61 2c 20 34 3a 20 6c 65 61 66 64 61 74 61 2c  ta, 4: leafdata,
13923 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 20 20 20 20   8: leaf.**     
13924 20 31 20 20 20 20 20 20 20 32 20 20 20 20 20 20   1       2      
13925 62 79 74 65 20 6f 66 66 73 65 74 20 74 6f 20 74  byte offset to t
13926 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f  he first freeblo
13927 63 6b 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20  ck.**      3    
13928 20 20 20 32 20 20 20 20 20 20 6e 75 6d 62 65 72     2      number
13929 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69   of cells on thi
1392a 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 35  s page.**      5
1392b 20 20 20 20 20 20 20 32 20 20 20 20 20 20 66 69         2      fi
1392c 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20  rst byte of the 
1392d 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
1392e 61 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20 20  a.**      7     
1392f 20 20 31 20 20 20 20 20 20 6e 75 6d 62 65 72 20    1      number 
13930 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 66 72  of fragmented fr
13931 65 65 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20  ee bytes.**     
13932 20 38 20 20 20 20 20 20 20 34 20 20 20 20 20 20   8       4      
13933 52 69 67 68 74 20 63 68 69 6c 64 20 28 74 68 65  Right child (the
13934 20 50 74 72 28 4e 29 20 76 61 6c 75 65 29 2e 20   Ptr(N) value). 
13935 20 4f 6d 69 74 74 65 64 20 6f 6e 20 6c 65 61 76   Omitted on leav
13936 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c  es..**.** The fl
13937 61 67 73 20 64 65 66 69 6e 65 20 74 68 65 20 66  ags define the f
13938 6f 72 6d 61 74 20 6f 66 20 74 68 69 73 20 62 74  ormat of this bt
13939 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20 6c  ree page.  The l
1393a 65 61 66 20 66 6c 61 67 20 6d 65 61 6e 73 20 74  eaf flag means t
1393b 68 61 74 0a 2a 2a 20 74 68 69 73 20 70 61 67 65  hat.** this page
1393c 20 68 61 73 20 6e 6f 20 63 68 69 6c 64 72 65 6e   has no children
1393d 2e 20 20 54 68 65 20 7a 65 72 6f 64 61 74 61 20  .  The zerodata 
1393e 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20  flag means that 
1393f 74 68 69 73 20 70 61 67 65 20 63 61 72 72 69 65  this page carrie
13940 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 79 73 20 61  s.** only keys a
13941 6e 64 20 6e 6f 20 64 61 74 61 2e 20 20 54 68 65  nd no data.  The
13942 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 6d 65 61   intkey flag mea
13943 6e 73 20 74 68 61 74 20 74 68 65 20 6b 65 79 20  ns that the key 
13944 69 73 20 61 20 69 6e 74 65 67 65 72 0a 2a 2a 20  is a integer.** 
13945 77 68 69 63 68 20 69 73 20 73 74 6f 72 65 64 20  which is stored 
13946 69 6e 20 74 68 65 20 6b 65 79 20 73 69 7a 65 20  in the key size 
13947 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 65 6c  entry of the cel
13948 6c 20 68 65 61 64 65 72 20 72 61 74 68 65 72 20  l header rather 
13949 74 68 61 6e 20 69 6e 0a 2a 2a 20 74 68 65 20 70  than in.** the p
1394a 61 79 6c 6f 61 64 20 61 72 65 61 2e 0a 2a 2a 0a  ayload area..**.
1394b 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e  ** The cell poin
1394c 74 65 72 20 61 72 72 61 79 20 62 65 67 69 6e 73  ter array begins
1394d 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 62 79   on the first by
1394e 74 65 20 61 66 74 65 72 20 74 68 65 20 70 61 67  te after the pag
1394f 65 20 68 65 61 64 65 72 2e 0a 2a 2a 20 54 68 65  e header..** The
13950 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72   cell pointer ar
13951 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72  ray contains zer
13952 6f 20 6f 72 20 6d 6f 72 65 20 32 2d 62 79 74 65  o or more 2-byte
13953 20 6e 75 6d 62 65 72 73 20 77 68 69 63 68 20 61   numbers which a
13954 72 65 0a 2a 2a 20 6f 66 66 73 65 74 73 20 66 72  re.** offsets fr
13955 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  om the beginning
13956 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f 20   of the page to 
13957 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
13958 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20   in the cell.** 
13959 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 54  content area.  T
1395a 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73  he cell pointers
1395b 20 6f 63 63 75 72 20 69 6e 20 73 6f 72 74 65 64   occur in sorted
1395c 20 6f 72 64 65 72 2e 20 20 54 68 65 20 73 79 73   order.  The sys
1395d 74 65 6d 20 73 74 72 69 76 65 73 0a 2a 2a 20 74  tem strives.** t
1395e 6f 20 6b 65 65 70 20 66 72 65 65 20 73 70 61 63  o keep free spac
1395f 65 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74  e after the last
13960 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 73 6f   cell pointer so
13961 20 74 68 61 74 20 6e 65 77 20 63 65 6c 6c 73 20   that new cells 
13962 63 61 6e 0a 2a 2a 20 62 65 20 65 61 73 69 6c 79  can.** be easily
13963 20 61 64 64 65 64 20 77 69 74 68 6f 75 74 20 68   added without h
13964 61 76 69 6e 67 20 74 6f 20 64 65 66 72 61 67 6d  aving to defragm
13965 65 6e 74 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a  ent the page..**
13966 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74  .** Cell content
13967 20 69 73 20 73 74 6f 72 65 64 20 61 74 20 74 68   is stored at th
13968 65 20 76 65 72 79 20 65 6e 64 20 6f 66 20 74 68  e very end of th
13969 65 20 70 61 67 65 20 61 6e 64 20 67 72 6f 77 73  e page and grows
1396a 20 74 6f 77 61 72 64 20 74 68 65 0a 2a 2a 20 62   toward the.** b
1396b 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
1396c 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 55 6e 75 73  page..**.** Unus
1396d 65 64 20 73 70 61 63 65 20 77 69 74 68 69 6e 20  ed space within 
1396e 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
1396f 20 61 72 65 61 20 69 73 20 63 6f 6c 6c 65 63 74   area is collect
13970 65 64 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64  ed into a linked
13971 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 66 72 65 65   list of.** free
13972 62 6c 6f 63 6b 73 2e 20 20 45 61 63 68 20 66 72  blocks.  Each fr
13973 65 65 62 6c 6f 63 6b 20 69 73 20 61 74 20 6c 65  eeblock is at le
13974 61 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 73  ast 4 bytes in s
13975 69 7a 65 2e 20 20 54 68 65 20 62 79 74 65 20 6f  ize.  The byte o
13976 66 66 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65 20  ffset.** to the 
13977 66 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 20  first freeblock 
13978 69 73 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  is given in the 
13979 68 65 61 64 65 72 2e 20 20 46 72 65 65 62 6c 6f  header.  Freeblo
1397a 63 6b 73 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20  cks occur in.** 
1397b 69 6e 63 72 65 61 73 69 6e 67 20 6f 72 64 65 72  increasing order
1397c 2e 20 20 42 65 63 61 75 73 65 20 61 20 66 72 65  .  Because a fre
1397d 65 62 6c 6f 63 6b 20 6d 75 73 74 20 62 65 20 61  eblock must be a
1397e 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 20  t least 4 bytes 
1397f 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20 61 6e 79 20  in size,.** any 
13980 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65  group of 3 or fe
13981 77 65 72 20 75 6e 75 73 65 64 20 62 79 74 65 73  wer unused bytes
13982 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   in the cell con
13983 74 65 6e 74 20 61 72 65 61 20 63 61 6e 6e 6f 74  tent area cannot
13984 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 65  .** exist on the
13985 20 66 72 65 65 62 6c 6f 63 6b 20 63 68 61 69 6e   freeblock chain
13986 2e 20 20 41 20 67 72 6f 75 70 20 6f 66 20 33 20  .  A group of 3 
13987 6f 72 20 66 65 77 65 72 20 66 72 65 65 20 62 79  or fewer free by
13988 74 65 73 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a  tes is called.**
13989 20 61 20 66 72 61 67 6d 65 6e 74 2e 20 20 54 68   a fragment.  Th
1398a 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
1398b 66 20 62 79 74 65 73 20 69 6e 20 61 6c 6c 20 66  f bytes in all f
1398c 72 61 67 6d 65 6e 74 73 20 69 73 20 72 65 63 6f  ragments is reco
1398d 72 64 65 64 2e 0a 2a 2a 20 69 6e 20 74 68 65 20  rded..** in the 
1398e 70 61 67 65 20 68 65 61 64 65 72 20 61 74 20 6f  page header at o
1398f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a 2a 2a 20 20  ffset 7..**.**  
13990 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49    SIZE    DESCRI
13991 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 32 20  PTION.**      2 
13992 20 20 20 20 42 79 74 65 20 6f 66 66 73 65 74 20      Byte offset 
13993 6f 66 20 74 68 65 20 6e 65 78 74 20 66 72 65 65  of the next free
13994 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 32 20  block.**      2 
13995 20 20 20 20 42 79 74 65 73 20 69 6e 20 74 68 69      Bytes in thi
13996 73 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a  s freeblock.**.*
13997 2a 20 43 65 6c 6c 73 20 61 72 65 20 6f 66 20 76  * Cells are of v
13998 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 2e 20  ariable length. 
13999 20 43 65 6c 6c 73 20 61 72 65 20 73 74 6f 72 65   Cells are store
1399a 64 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f  d in the cell co
1399b 6e 74 65 6e 74 20 61 72 65 61 20 61 74 0a 2a 2a  ntent area at.**
1399c 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1399d 70 61 67 65 2e 20 20 50 6f 69 6e 74 65 72 73 20  page.  Pointers 
1399e 74 6f 20 74 68 65 20 63 65 6c 6c 73 20 61 72 65  to the cells are
1399f 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f 69   in the cell poi
139a0 6e 74 65 72 20 61 72 72 61 79 0a 2a 2a 20 74 68  nter array.** th
139a1 61 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66  at immediately f
139a2 6f 6c 6c 6f 77 73 20 74 68 65 20 70 61 67 65 20  ollows the page 
139a3 68 65 61 64 65 72 2e 20 20 43 65 6c 6c 73 20 69  header.  Cells i
139a4 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  s not necessaril
139a5 79 0a 2a 2a 20 63 6f 6e 74 69 67 75 6f 75 73 20  y.** contiguous 
139a6 6f 72 20 69 6e 20 6f 72 64 65 72 2c 20 62 75 74  or in order, but
139a7 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61   cell pointers a
139a8 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 6e  re contiguous an
139a9 64 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a  d in order..**.*
139aa 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6d  * Cell content m
139ab 61 6b 65 73 20 75 73 65 20 6f 66 20 76 61 72 69  akes use of vari
139ac 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65  able length inte
139ad 67 65 72 73 2e 20 20 41 20 76 61 72 69 61 62 6c  gers.  A variabl
139ae 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 69 6e 74 65  e.** length inte
139af 67 65 72 20 69 73 20 31 20 74 6f 20 39 20 62 79  ger is 1 to 9 by
139b0 74 65 73 20 77 68 65 72 65 20 74 68 65 20 6c 6f  tes where the lo
139b1 77 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 61  wer 7 bits of ea
139b2 63 68 20 0a 2a 2a 20 62 79 74 65 20 61 72 65 20  ch .** byte are 
139b3 75 73 65 64 2e 20 20 54 68 65 20 69 6e 74 65 67  used.  The integ
139b4 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61  er consists of a
139b5 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 61  ll bytes that ha
139b6 76 65 20 62 69 74 20 38 20 73 65 74 20 61 6e 64  ve bit 8 set and
139b7 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79  .** the first by
139b8 74 65 20 77 69 74 68 20 62 69 74 20 38 20 63 6c  te with bit 8 cl
139b9 65 61 72 2e 20 20 54 68 65 20 6d 6f 73 74 20 73  ear.  The most s
139ba 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20  ignificant byte 
139bb 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  of the integer.*
139bc 2a 20 61 70 70 65 61 72 73 20 66 69 72 73 74 2e  * appears first.
139bd 20 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e    A variable-len
139be 67 74 68 20 69 6e 74 65 67 65 72 20 6d 61 79 20  gth integer may 
139bf 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e  not be more than
139c0 20 39 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a   9 bytes long..*
139c1 2a 20 41 73 20 61 20 73 70 65 63 69 61 6c 20 63  * As a special c
139c2 61 73 65 2c 20 61 6c 6c 20 38 20 62 79 74 65 73  ase, all 8 bytes
139c3 20 6f 66 20 74 68 65 20 39 74 68 20 62 79 74 65   of the 9th byte
139c4 20 61 72 65 20 75 73 65 64 20 61 73 20 64 61 74   are used as dat
139c5 61 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f  a.  This.** allo
139c6 77 73 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65  ws a 64-bit inte
139c7 67 65 72 20 74 6f 20 62 65 20 65 6e 63 6f 64 65  ger to be encode
139c8 64 20 69 6e 20 39 20 62 79 74 65 73 2e 0a 2a 2a  d in 9 bytes..**
139c9 0a 2a 2a 20 20 20 20 30 78 30 30 20 20 20 20 20  .**    0x00     
139ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139cb 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30   becomes  0x0000
139cc 30 30 30 30 0a 2a 2a 20 20 20 20 30 78 37 66 20  0000.**    0x7f 
139cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139ce 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78       becomes  0x
139cf 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 30  0000007f.**    0
139d0 78 38 31 20 30 78 30 30 20 20 20 20 20 20 20 20  x81 0x00        
139d1 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73           becomes
139d2 20 20 30 78 30 30 30 30 30 30 38 30 0a 2a 2a 20    0x00000080.** 
139d3 20 20 20 30 78 38 32 20 30 78 30 30 20 20 20 20     0x82 0x00    
139d4 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63               bec
139d5 6f 6d 65 73 20 20 30 78 30 30 30 30 30 31 30 30  omes  0x00000100
139d6 0a 2a 2a 20 20 20 20 30 78 38 30 20 30 78 37 66  .**    0x80 0x7f
139d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139d8 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30   becomes  0x0000
139d9 30 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 61 20  007f.**    0x8a 
139da 30 78 39 31 20 30 78 64 31 20 30 78 61 63 20 30  0x91 0xd1 0xac 0
139db 78 37 38 20 20 62 65 63 6f 6d 65 73 20 20 30 78  x78  becomes  0x
139dc 31 32 33 34 35 36 37 38 0a 2a 2a 20 20 20 20 30  12345678.**    0
139dd 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78  x81 0x81 0x81 0x
139de 38 31 20 30 78 30 31 20 20 62 65 63 6f 6d 65 73  81 0x01  becomes
139df 20 20 30 78 31 30 32 30 34 30 38 31 0a 2a 2a 0a    0x10204081.**.
139e0 2a 2a 20 56 61 72 69 61 62 6c 65 20 6c 65 6e 67  ** Variable leng
139e1 74 68 20 69 6e 74 65 67 65 72 73 20 61 72 65 20  th integers are 
139e2 75 73 65 64 20 66 6f 72 20 72 6f 77 69 64 73 20  used for rowids 
139e3 61 6e 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  and to hold the 
139e4 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74  number of.** byt
139e5 65 73 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 61  es of key and da
139e6 74 61 20 69 6e 20 61 20 62 74 72 65 65 20 63 65  ta in a btree ce
139e7 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ll..**.** The co
139e8 6e 74 65 6e 74 20 6f 66 20 61 20 63 65 6c 6c 20  ntent of a cell 
139e9 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a  looks like this:
139ea 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20  .**.**    SIZE  
139eb 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a    DESCRIPTION.**
139ec 20 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65        4     Page
139ed 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c   number of the l
139ee 65 66 74 20 63 68 69 6c 64 2e 20 4f 6d 69 74 74  eft child. Omitt
139ef 65 64 20 69 66 20 6c 65 61 66 20 66 6c 61 67 20  ed if leaf flag 
139f0 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76  is set..**     v
139f1 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20  ar    Number of 
139f2 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e 20 4f  bytes of data. O
139f3 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 7a 65  mitted if the ze
139f4 72 6f 64 61 74 61 20 66 6c 61 67 20 69 73 20 73  rodata flag is s
139f5 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20  et..**     var  
139f6 20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65    Number of byte
139f7 73 20 6f 66 20 6b 65 79 2e 20 4f 72 20 74 68 65  s of key. Or the
139f8 20 6b 65 79 20 69 74 73 65 6c 66 20 69 66 20 69   key itself if i
139f9 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73 65  ntkey flag is se
139fa 74 2e 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20  t..**      *    
139fb 20 50 61 79 6c 6f 61 64 0a 2a 2a 20 20 20 20 20   Payload.**     
139fc 20 34 20 20 20 20 20 46 69 72 73 74 20 70 61 67   4     First pag
139fd 65 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f  e of the overflo
139fe 77 20 63 68 61 69 6e 2e 20 20 4f 6d 69 74 74 65  w chain.  Omitte
139ff 64 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77  d if no overflow
13a00 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66 6c 6f 77 20  .**.** Overflow 
13a01 70 61 67 65 73 20 66 6f 72 6d 20 61 20 6c 69 6e  pages form a lin
13a02 6b 65 64 20 6c 69 73 74 2e 20 20 45 61 63 68 20  ked list.  Each 
13a03 70 61 67 65 20 65 78 63 65 70 74 20 74 68 65 20  page except the 
13a04 6c 61 73 74 20 69 73 20 63 6f 6d 70 6c 65 74 65  last is complete
13a05 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74  ly.** filled wit
13a06 68 20 64 61 74 61 20 28 70 61 67 65 73 69 7a 65  h data (pagesize
13a07 20 2d 20 34 20 62 79 74 65 73 29 2e 20 20 54 68   - 4 bytes).  Th
13a08 65 20 6c 61 73 74 20 70 61 67 65 20 63 61 6e 20  e last page can 
13a09 68 61 76 65 20 61 73 20 6c 69 74 74 6c 65 0a 2a  have as little.*
13a0a 2a 20 61 73 20 31 20 62 79 74 65 20 6f 66 20 64  * as 1 byte of d
13a0b 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49  ata..**.**    SI
13a0c 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f  ZE    DESCRIPTIO
13a0d 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20  N.**      4     
13a0e 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e  Page number of n
13a0f 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ext overflow pag
13a10 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20  e.**      *     
13a11 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46 72 65 65 6c  Data.**.** Freel
13a12 69 73 74 20 70 61 67 65 73 20 63 6f 6d 65 20 69  ist pages come i
13a13 6e 20 74 77 6f 20 73 75 62 74 79 70 65 73 3a 20  n two subtypes: 
13a14 74 72 75 6e 6b 20 70 61 67 65 73 20 61 6e 64 20  trunk pages and 
13a15 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65  leaf pages.  The
13a16 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20  .** file header 
13a17 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69  points to the fi
13a18 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20  rst in a linked 
13a19 6c 69 73 74 20 6f 66 20 74 72 75 6e 6b 20 70 61  list of trunk pa
13a1a 67 65 2e 20 20 45 61 63 68 20 74 72 75 6e 6b 0a  ge.  Each trunk.
13a1b 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 73 20 74  ** page points t
13a1c 6f 20 6d 75 6c 74 69 70 6c 65 20 6c 65 61 66 20  o multiple leaf 
13a1d 70 61 67 65 73 2e 20 20 54 68 65 20 63 6f 6e 74  pages.  The cont
13a1e 65 6e 74 20 6f 66 20 61 20 6c 65 61 66 20 70 61  ent of a leaf pa
13a1f 67 65 20 69 73 0a 2a 2a 20 75 6e 73 70 65 63 69  ge is.** unspeci
13a20 66 69 65 64 2e 20 20 41 20 74 72 75 6e 6b 20 70  fied.  A trunk p
13a21 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74  age looks like t
13a22 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49  his:.**.**    SI
13a23 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f  ZE    DESCRIPTIO
13a24 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20  N.**      4     
13a25 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e  Page number of n
13a26 65 78 74 20 74 72 75 6e 6b 20 70 61 67 65 0a 2a  ext trunk page.*
13a27 2a 20 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d  *      4     Num
13a28 62 65 72 20 6f 66 20 6c 65 61 66 20 70 6f 69 6e  ber of leaf poin
13a29 74 65 72 73 20 6f 6e 20 74 68 69 73 20 70 61 67  ters on this pag
13a2a 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20  e.**      *     
13a2b 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67  zero or more pag
13a2c 65 73 20 6e 75 6d 62 65 72 73 20 6f 66 20 6c 65  es numbers of le
13a2d 61 76 65 73 0a 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e  aves.*/../* Roun
13a2e 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74 6f  d up a number to
13a2f 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72   the next larger
13a30 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20   multiple of 8. 
13a31 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a   This is used.**
13a32 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74 65   to force 8-byte
13a33 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36 34   alignment on 64
13a34 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75 72  -bit architectur
13a35 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  es..*/.#define R
13a36 4f 55 4e 44 38 28 78 29 20 20 20 28 28 78 2b 37  OUND8(x)   ((x+7
13a37 29 26 7e 37 29 0a 0a 0a 2f 2a 20 54 68 65 20 66  )&~7).../* The f
13a38 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 69  ollowing value i
13a39 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65  s the maximum ce
13a3a 6c 6c 20 73 69 7a 65 20 61 73 73 75 6d 69 6e 67  ll size assuming
13a3b 20 61 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 0a   a maximum page.
13a3c 2a 2a 20 73 69 7a 65 20 67 69 76 65 20 61 62 6f  ** size give abo
13a3d 76 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ve..*/.#define M
13a3e 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29  X_CELL_SIZE(pBt)
13a3f 20 20 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65    (pBt->pageSize
13a40 2d 38 29 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69  -8)../* The maxi
13a41 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65  mum number of ce
13a42 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  lls on a single 
13a43 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
13a44 62 61 73 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61  base.  This.** a
13a45 73 73 75 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d  ssumes a minimum
13a46 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20   cell size of 6 
13a47 62 79 74 65 73 20 20 28 34 20 62 79 74 65 73 20  bytes  (4 bytes 
13a48 66 6f 72 20 74 68 65 20 63 65 6c 6c 20 69 74 73  for the cell its
13a49 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79  elf.** plus 2 by
13a4a 74 65 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65  tes for the inde
13a4b 78 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e  x to the cell in
13a4c 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
13a4d 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c  ).  Such.** smal
13a4e 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20  l cells will be 
13a4f 72 61 72 65 2c 20 62 75 74 20 74 68 65 79 20 61  rare, but they a
13a50 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a  re possible..*/.
13a51 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28  #define MX_CELL(
13a52 70 42 74 29 20 28 28 70 42 74 2d 3e 70 61 67 65  pBt) ((pBt->page
13a53 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46  Size-8)/6)../* F
13a54 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
13a55 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73  ons */.typedef s
13a56 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 4d 65  truct MemPage Me
13a57 6d 50 61 67 65 3b 0a 74 79 70 65 64 65 66 20 73  mPage;.typedef s
13a58 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c  truct BtLock BtL
13a59 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ock;../*.** This
13a5a 20 69 73 20 61 20 6d 61 67 69 63 20 73 74 72 69   is a magic stri
13a5b 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ng that appears 
13a5c 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
13a5d 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c   of every.** SQL
13a5e 69 74 65 20 64 61 74 61 62 61 73 65 20 69 6e 20  ite database in 
13a5f 6f 72 64 65 72 20 74 6f 20 69 64 65 6e 74 69 66  order to identif
13a60 79 20 74 68 65 20 66 69 6c 65 20 61 73 20 61 20  y the file as a 
13a61 72 65 61 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a  real database..*
13a62 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20 63 68 61  *.** You can cha
13a63 6e 67 65 20 74 68 69 73 20 76 61 6c 75 65 20 61  nge this value a
13a64 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62  t compile-time b
13a65 79 20 73 70 65 63 69 66 79 69 6e 67 20 61 0a 2a  y specifying a.*
13a66 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f  * -DSQLITE_FILE_
13a67 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20  HEADER="..." on 
13a68 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d  the compiler com
13a69 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a  mand-line.  The.
13a6a 2a 2a 20 68 65 61 64 65 72 20 6d 75 73 74 20 62  ** header must b
13a6b 65 20 65 78 61 63 74 6c 79 20 31 36 20 62 79 74  e exactly 16 byt
13a6c 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  es including the
13a6d 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72   zero-terminator
13a6e 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e   so.** the strin
13a6f 67 20 69 74 73 65 6c 66 20 73 68 6f 75 6c 64 20  g itself should 
13a70 62 65 20 31 35 20 63 68 61 72 61 63 74 65 72 73  be 15 characters
13a71 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f 75 20 63   long.  If you c
13a72 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 68 65 61  hange.** the hea
13a73 64 65 72 2c 20 74 68 65 6e 20 79 6f 75 72 20 63  der, then your c
13a74 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 20 77 69  ustom library wi
13a75 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74  ll not be able t
13a76 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61 74 61 62  o read .** datab
13a77 61 73 65 73 20 67 65 6e 65 72 61 74 65 64 20 62  ases generated b
13a78 79 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74  y the standard t
13a79 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20 73 74 61  ools and the sta
13a7a 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77  ndard tools.** w
13a7b 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20  ill not be able 
13a7c 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65  to read database
13a7d 73 20 63 72 65 61 74 65 64 20 62 79 20 79 6f 75  s created by you
13a7e 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79  r custom library
13a7f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
13a80 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20  ITE_FILE_HEADER 
13a81 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33  /* 123456789 123
13a82 34 35 36 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65  456 */.#  define
13a83 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41   SQLITE_FILE_HEA
13a84 44 45 52 20 22 53 51 4c 69 74 65 20 66 6f 72 6d  DER "SQLite form
13a85 61 74 20 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a  at 3".#endif../*
13a86 0a 2a 2a 20 50 61 67 65 20 74 79 70 65 20 66 6c  .** Page type fl
13a87 61 67 73 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f  ags.  An ORed co
13a88 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65  mbination of the
13a89 73 65 20 66 6c 61 67 73 20 61 70 70 65 61 72 20  se flags appear 
13a8a 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  as the.** first 
13a8b 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20  byte of on-disk 
13a8c 69 6d 61 67 65 20 6f 66 20 65 76 65 72 79 20 42  image of every B
13a8d 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64  Tree page..*/.#d
13a8e 65 66 69 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59  efine PTF_INTKEY
13a8f 20 20 20 20 30 78 30 31 0a 23 64 65 66 69 6e 65      0x01.#define
13a90 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 20 30   PTF_ZERODATA  0
13a91 78 30 32 0a 23 64 65 66 69 6e 65 20 50 54 46 5f  x02.#define PTF_
13a92 4c 45 41 46 44 41 54 41 20 20 30 78 30 34 0a 23  LEAFDATA  0x04.#
13a93 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 20  define PTF_LEAF 
13a94 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a       0x08../*.**
13a95 20 41 73 20 65 61 63 68 20 70 61 67 65 20 6f 66   As each page of
13a96 20 74 68 65 20 66 69 6c 65 20 69 73 20 6c 6f 61   the file is loa
13a97 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c  ded into memory,
13a98 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
13a99 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
13a9a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70   structure is ap
13a9b 70 65 6e 64 65 64 20 61 6e 64 20 69 6e 69 74 69  pended and initi
13a9c 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20  alized to zero. 
13a9d 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
13a9e 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d  stores.** inform
13a9f 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
13aa0 70 61 67 65 20 74 68 61 74 20 69 73 20 64 65 63  page that is dec
13aa1 6f 64 65 64 20 66 72 6f 6d 20 74 68 65 20 72 61  oded from the ra
13aa2 77 20 66 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a  w file page..**.
13aa3 2a 2a 20 54 68 65 20 70 50 61 72 65 6e 74 20 66  ** The pParent f
13aa4 69 65 6c 64 20 70 6f 69 6e 74 73 20 62 61 63 6b  ield points back
13aa5 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70   to the parent p
13aa6 61 67 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77  age.  This allow
13aa7 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20  s us to.** walk 
13aa8 75 70 20 74 68 65 20 42 54 72 65 65 20 66 72 6f  up the BTree fro
13aa9 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f 20 74 68  m any leaf to th
13aaa 65 20 72 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75  e root.  Care mu
13aab 73 74 20 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a  st be taken to.*
13aac 2a 20 75 6e 72 65 66 28 29 20 74 68 65 20 70 61  * unref() the pa
13aad 72 65 6e 74 20 70 61 67 65 20 70 6f 69 6e 74 65  rent page pointe
13aae 72 20 77 68 65 6e 20 74 68 69 73 20 70 61 67 65  r when this page
13aaf 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65   is no longer re
13ab0 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65  ferenced..** The
13ab1 20 70 61 67 65 44 65 73 74 72 75 63 74 6f 72 28   pageDestructor(
13ab2 29 20 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65  ) routine handle
13ab3 73 20 74 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a  s that chore..**
13ab4 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f 20 61 6c  .** Access to al
13ab5 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73  l fields of this
13ab6 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 6f   structure is co
13ab7 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74 68 65 20  ntrolled by the 
13ab8 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20  mutex.** stored 
13ab9 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e  in MemPage.pBt->
13aba 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74  mutex..*/.struct
13abb 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20 75 38 20   MemPage {.  u8 
13abc 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  isInit;         
13abd 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 65    /* True if pre
13abe 76 69 6f 75 73 6c 79 20 69 6e 69 74 69 61 6c 69  viously initiali
13abf 7a 65 64 2e 20 4d 55 53 54 20 42 45 20 46 49 52  zed. MUST BE FIR
13ac0 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65  ST! */.  u8 nOve
13ac1 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a  rflow;        /*
13ac2 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76 65 72 66   Number of overf
13ac3 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69 65 73 20  low cell bodies 
13ac4 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20  in aCell[] */.  
13ac5 75 38 20 69 6e 74 4b 65 79 3b 20 20 20 20 20 20  u8 intKey;      
13ac6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
13ac7 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73  intkey flag is s
13ac8 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b  et */.  u8 leaf;
13ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13aca 54 72 75 65 20 69 66 20 6c 65 61 66 20 66 6c 61  True if leaf fla
13acb 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38  g is set */.  u8
13acc 20 68 61 73 44 61 74 61 3b 20 20 20 20 20 20 20   hasData;       
13acd 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
13ace 69 73 20 70 61 67 65 20 73 74 6f 72 65 73 20 64  is page stores d
13acf 61 74 61 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f  ata */.  u8 hdrO
13ad0 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  ffset;        /*
13ad1 20 31 30 30 20 66 6f 72 20 70 61 67 65 20 31 2e   100 for page 1.
13ad2 20 20 30 20 6f 74 68 65 72 77 69 73 65 20 2a 2f    0 otherwise */
13ad3 0a 20 20 75 38 20 63 68 69 6c 64 50 74 72 53 69  .  u8 childPtrSi
13ad4 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 69 66 20  ze;     /* 0 if 
13ad5 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c  leaf==1.  4 if l
13ad6 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75 31 36 20  eaf==0 */.  u16 
13ad7 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20  maxLocal;       
13ad8 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68   /* Copy of BtSh
13ad9 61 72 65 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72  ared.maxLocal or
13ada 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 65 61   BtShared.maxLea
13adb 66 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f  f */.  u16 minLo
13adc 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  cal;        /* C
13add 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e  opy of BtShared.
13ade 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68  minLocal or BtSh
13adf 61 72 65 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a  ared.minLeaf */.
13ae0 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74    u16 cellOffset
13ae1 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20  ;      /* Index 
13ae2 69 6e 20 61 44 61 74 61 20 6f 66 20 66 69 72 73  in aData of firs
13ae3 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a  t cell pointer *
13ae4 2f 0a 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20  /.  u16 nFree;  
13ae5 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
13ae6 65 72 20 6f 66 20 66 72 65 65 20 62 79 74 65 73  er of free bytes
13ae7 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a   on the page */.
13ae8 20 20 75 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20    u16 nCell;    
13ae9 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
13aea 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69   of cells on thi
13aeb 73 20 70 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e  s page, local an
13aec 64 20 6f 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20  d ovfl */.  u16 
13aed 6d 61 73 6b 50 61 67 65 3b 20 20 20 20 20 20 20  maskPage;       
13aee 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 70 61 67   /* Mask for pag
13aef 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 73 74  e offset */.  st
13af0 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 7b  ruct _OvflCell {
13af1 20 20 20 2f 2a 20 43 65 6c 6c 73 20 74 68 61 74     /* Cells that
13af2 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e   will not fit on
13af3 20 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20   aData[] */.    
13af4 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20  u8 *pCell;      
13af5 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20      /* Pointers 
13af6 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74  to the body of t
13af7 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  he overflow cell
13af8 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 64 78 3b   */.    u16 idx;
13af9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
13afa 6e 73 65 72 74 20 74 68 69 73 20 63 65 6c 6c 20  nsert this cell 
13afb 62 65 66 6f 72 65 20 69 64 78 2d 74 68 20 6e 6f  before idx-th no
13afc 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20  n-overflow cell 
13afd 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b  */.  } aOvfl[5];
13afe 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
13aff 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  ;       /* Point
13b00 65 72 20 74 6f 20 42 74 53 68 61 72 65 64 20 74  er to BtShared t
13b01 68 61 74 20 74 68 69 73 20 70 61 67 65 20 69 73  hat this page is
13b02 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 75 38   part of */.  u8
13b03 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20   *aData;        
13b04 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
13b05 20 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 74   disk image of t
13b06 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a  he page data */.
13b07 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
13b08 65 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20  e;     /* Pager 
13b09 70 61 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  page handle */. 
13b0a 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20   Pgno pgno;     
13b0b 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
13b0c 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61  mber for this pa
13b0d 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ge */.};../*.** 
13b0e 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d  The in-memory im
13b0f 61 67 65 20 6f 66 20 61 20 64 69 73 6b 20 70 61  age of a disk pa
13b10 67 65 20 68 61 73 20 74 68 65 20 61 75 78 69 6c  ge has the auxil
13b11 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  iary information
13b12 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20   appended.** to 
13b13 74 68 65 20 65 6e 64 2e 20 20 45 58 54 52 41 5f  the end.  EXTRA_
13b14 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62  SIZE is the numb
13b15 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
13b16 70 61 63 65 20 6e 65 65 64 65 64 20 74 6f 20 68  pace needed to h
13b17 6f 6c 64 0a 2a 2a 20 74 68 61 74 20 65 78 74 72  old.** that extr
13b18 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  a information..*
13b19 2f 0a 23 64 65 66 69 6e 65 20 45 58 54 52 41 5f  /.#define EXTRA_
13b1a 53 49 5a 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50  SIZE sizeof(MemP
13b1b 61 67 65 29 0a 0a 2f 2a 20 41 20 42 74 72 65 65  age)../* A Btree
13b1c 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20   handle.**.** A 
13b1d 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
13b1e 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  ion contains a p
13b1f 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
13b20 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73  tance of.** this
13b21 20 6f 62 6a 65 63 74 20 66 6f 72 20 65 76 65 72   object for ever
13b22 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  y database file 
13b23 74 68 61 74 20 69 74 20 68 61 73 20 6f 70 65 6e  that it has open
13b24 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
13b25 65 0a 2a 2a 20 69 73 20 6f 70 61 71 75 65 20 74  e.** is opaque t
13b26 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  o the database c
13b27 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20  onnection.  The 
13b28 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
13b29 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65  ion cannot.** se
13b2a 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 73 20  e the internals 
13b2b 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
13b2c 65 20 61 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73  e and only deals
13b2d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
13b2e 6f 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74  o.** this struct
13b2f 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73  ure..**.** For s
13b30 6f 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ome database fil
13b31 65 73 2c 20 74 68 65 20 73 61 6d 65 20 75 6e 64  es, the same und
13b32 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65  erlying database
13b33 20 63 61 63 68 65 20 6d 69 67 68 74 20 62 65 20   cache might be 
13b34 0a 2a 2a 20 73 68 61 72 65 64 20 62 65 74 77 65  .** shared betwe
13b35 65 6e 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e  en multiple conn
13b36 65 63 74 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61  ections.  In tha
13b37 74 20 63 61 73 65 2c 20 65 61 63 68 20 63 6f 6e  t case, each con
13b38 74 65 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69  tection.** has i
13b39 74 20 6f 77 6e 20 70 6f 69 6e 74 65 72 20 74 6f  t own pointer to
13b3a 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 20 20 42   this object.  B
13b3b 75 74 20 65 61 63 68 20 69 6e 73 74 61 6e 63 65  ut each instance
13b3c 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a   of this object.
13b3d 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  ** points to the
13b3e 20 73 61 6d 65 20 42 74 53 68 61 72 65 64 20 6f   same BtShared o
13b3f 62 6a 65 63 74 2e 20 20 54 68 65 20 64 61 74 61  bject.  The data
13b40 62 61 73 65 20 63 61 63 68 65 20 61 6e 64 20 74  base cache and t
13b41 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 61 73 73  he.** schema ass
13b42 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
13b43 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
13b44 72 65 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 64  re all contained
13b45 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 42   within.** the B
13b46 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a  tShared object..
13b47 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c 64 73  **.** All fields
13b48 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75   in this structu
13b49 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64 20  re are accessed 
13b4a 75 6e 64 65 72 20 73 71 6c 69 74 65 33 2e 6d 75  under sqlite3.mu
13b4b 74 65 78 2e 0a 2a 2a 20 54 68 65 20 70 42 74 20  tex..** The pBt 
13b4c 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 20 6d  pointer itself m
13b4d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65  ay not be change
13b4e 64 20 77 68 69 6c 65 20 74 68 65 72 65 20 65 78  d while there ex
13b4f 69 73 74 73 20 63 75 72 73 6f 72 73 20 0a 2a 2a  ists cursors .**
13b50 20 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63   in the referenc
13b51 65 64 20 42 74 53 68 61 72 65 64 20 74 68 61 74  ed BtShared that
13b52 20 70 6f 69 6e 74 20 62 61 63 6b 20 74 6f 20 74   point back to t
13b53 68 69 73 20 42 74 72 65 65 20 73 69 6e 63 65 20  his Btree since 
13b54 74 68 6f 73 65 0a 2a 2a 20 63 75 72 73 6f 72 73  those.** cursors
13b55 20 68 61 76 65 20 74 6f 20 64 6f 20 67 6f 20 74   have to do go t
13b56 68 72 6f 75 67 68 20 74 68 69 73 20 42 74 72 65  hrough this Btre
13b57 65 20 74 6f 20 66 69 6e 64 20 74 68 65 69 72 20  e to find their 
13b58 42 74 53 68 61 72 65 64 20 61 6e 64 0a 2a 2a 20  BtShared and.** 
13b59 74 68 65 79 20 6f 66 74 65 6e 20 64 6f 20 73 6f  they often do so
13b5a 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67   without holding
13b5b 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a   sqlite3.mutex..
13b5c 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 65 65 20  */.struct Btree 
13b5d 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
13b5e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
13b5f 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
13b60 6e 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 62  n holding this b
13b61 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72  tree */.  BtShar
13b62 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20  ed *pBt;     /* 
13b63 53 68 61 72 61 62 6c 65 20 63 6f 6e 74 65 6e 74  Sharable content
13b64 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 2a   of this btree *
13b65 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 3b 20  /.  u8 inTrans; 
13b66 20 20 20 20 20 20 20 2f 2a 20 54 52 41 4e 53 5f         /* TRANS_
13b67 4e 4f 4e 45 2c 20 54 52 41 4e 53 5f 52 45 41 44  NONE, TRANS_READ
13b68 20 6f 72 20 54 52 41 4e 53 5f 57 52 49 54 45 20   or TRANS_WRITE 
13b69 2a 2f 0a 20 20 75 38 20 73 68 61 72 61 62 6c 65  */.  u8 sharable
13b6a 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
13b6b 69 66 20 77 65 20 63 61 6e 20 73 68 61 72 65 20  if we can share 
13b6c 70 42 74 20 77 69 74 68 20 61 6e 6f 74 68 65 72  pBt with another
13b6d 20 64 62 20 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b   db */.  u8 lock
13b6e 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ed;         /* T
13b6f 72 75 65 20 69 66 20 64 62 20 63 75 72 72 65 6e  rue if db curren
13b70 74 6c 79 20 68 61 73 20 70 42 74 20 6c 6f 63 6b  tly has pBt lock
13b71 65 64 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6e 74  ed */.  int want
13b72 54 6f 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e 75  ToLock;    /* Nu
13b73 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63  mber of nested c
13b74 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42  alls to sqlite3B
13b75 74 72 65 65 45 6e 74 65 72 28 29 20 2a 2f 0a 20  treeEnter() */. 
13b76 20 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20 20   Btree *pNext;  
13b77 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f      /* List of o
13b78 74 68 65 72 20 73 68 61 72 61 62 6c 65 20 42 74  ther sharable Bt
13b79 72 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61  rees from the sa
13b7a 6d 65 20 64 62 20 2a 2f 0a 20 20 42 74 72 65 65  me db */.  Btree
13b7b 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20 2f 2a   *pPrev;      /*
13b7c 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66   Back pointer of
13b7d 20 74 68 65 20 73 61 6d 65 20 6c 69 73 74 20 2a   the same list *
13b7e 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65  /.};../*.** Btre
13b7f 65 2e 69 6e 54 72 61 6e 73 20 6d 61 79 20 74 61  e.inTrans may ta
13b80 6b 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  ke one of the fo
13b81 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a  llowing values..
13b82 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61  **.** If the sha
13b83 72 65 64 2d 64 61 74 61 20 65 78 74 65 6e 73 69  red-data extensi
13b84 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74  on is enabled, t
13b85 68 65 72 65 20 6d 61 79 20 62 65 20 6d 75 6c 74  here may be mult
13b86 69 70 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66  iple users.** of
13b87 20 74 68 65 20 42 74 72 65 65 20 73 74 72 75 63   the Btree struc
13b88 74 75 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e  ture. At most on
13b89 65 20 6f 66 20 74 68 65 73 65 20 6d 61 79 20 6f  e of these may o
13b8a 70 65 6e 20 61 20 77 72 69 74 65 20 74 72 61 6e  pen a write tran
13b8b 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20  saction,.** but 
13b8c 61 6e 79 20 6e 75 6d 62 65 72 20 6d 61 79 20 68  any number may h
13b8d 61 76 65 20 61 63 74 69 76 65 20 72 65 61 64 20  ave active read 
13b8e 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2f  transactions..*/
13b8f 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e  .#define TRANS_N
13b90 4f 4e 45 20 20 30 0a 23 64 65 66 69 6e 65 20 54  ONE  0.#define T
13b91 52 41 4e 53 5f 52 45 41 44 20 20 31 0a 23 64 65  RANS_READ  1.#de
13b92 66 69 6e 65 20 54 52 41 4e 53 5f 57 52 49 54 45  fine TRANS_WRITE
13b93 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73   2../*.** An ins
13b94 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
13b95 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20  ject represents 
13b96 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  a single databas
13b97 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 41  e file..** .** A
13b98 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
13b99 20 66 69 6c 65 20 63 61 6e 20 62 65 20 69 6e 20   file can be in 
13b9a 75 73 65 20 61 73 20 74 68 65 20 73 61 6d 65 20  use as the same 
13b9b 74 69 6d 65 20 62 79 20 74 77 6f 0a 2a 2a 20 6f  time by two.** o
13b9c 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20  r more database 
13b9d 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 57 68  connections.  Wh
13b9e 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63  en two or more c
13b9f 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a  onnections are.*
13ba0 2a 20 73 68 61 72 69 6e 67 20 74 68 65 20 73 61  * sharing the sa
13ba1 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  me database file
13ba2 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f  , each connectio
13ba3 6e 20 68 61 73 20 69 74 20 6f 77 6e 0a 2a 2a 20  n has it own.** 
13ba4 70 72 69 76 61 74 65 20 42 74 72 65 65 20 6f 62  private Btree ob
13ba5 6a 65 63 74 20 66 6f 72 20 74 68 65 20 66 69 6c  ject for the fil
13ba6 65 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68  e and each of th
13ba7 6f 73 65 20 42 74 72 65 65 73 20 70 6f 69 6e 74  ose Btrees point
13ba8 73 0a 2a 2a 20 74 6f 20 74 68 69 73 20 6f 6e 65  s.** to this one
13ba9 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
13baa 2e 20 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66  .  BtShared.nRef
13bab 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
13bac 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  f.** connections
13bad 20 63 75 72 72 65 6e 74 6c 79 20 73 68 61 72 69   currently shari
13bae 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73 65  ng this database
13baf 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65   file..**.** Fie
13bb0 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75  lds in this stru
13bb1 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73  cture are access
13bb2 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 53  ed under the BtS
13bb3 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 6d  hared.mutex.** m
13bb4 75 74 65 78 2c 20 65 78 63 65 70 74 20 66 6f 72  utex, except for
13bb5 20 6e 52 65 66 20 61 6e 64 20 70 4e 65 78 74 20   nRef and pNext 
13bb6 77 68 69 63 68 20 61 72 65 20 61 63 63 65 73 73  which are access
13bb7 65 64 20 75 6e 64 65 72 20 74 68 65 0a 2a 2a 20  ed under the.** 
13bb8 67 6c 6f 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55  global SQLITE_MU
13bb9 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
13bba 52 20 6d 75 74 65 78 2e 20 20 54 68 65 20 70 50  R mutex.  The pP
13bbb 61 67 65 72 20 66 69 65 6c 64 0a 2a 2a 20 6d 61  ager field.** ma
13bbc 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65  y not be modifie
13bbd 64 20 6f 6e 63 65 20 69 74 20 69 73 20 69 6e 69  d once it is ini
13bbe 74 69 61 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f  tially set as lo
13bbf 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a  ng as nRef>0..**
13bc0 20 54 68 65 20 70 53 63 68 65 6d 61 20 66 69 65   The pSchema fie
13bc1 6c 64 20 6d 61 79 20 62 65 20 73 65 74 20 6f 6e  ld may be set on
13bc2 63 65 20 75 6e 64 65 72 20 42 74 53 68 61 72 65  ce under BtShare
13bc3 64 2e 6d 75 74 65 78 20 61 6e 64 0a 2a 2a 20 74  d.mutex and.** t
13bc4 68 65 72 65 61 66 74 65 72 20 69 73 20 75 6e 63  hereafter is unc
13bc5 68 61 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20 61  hanged as long a
13bc6 73 20 6e 52 65 66 3e 30 2e 0a 2a 2f 0a 73 74 72  s nRef>0..*/.str
13bc7 75 63 74 20 42 74 53 68 61 72 65 64 20 7b 0a 20  uct BtShared {. 
13bc8 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20   Pager *pPager; 
13bc9 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
13bca 67 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 73 71  ge cache */.  sq
13bcb 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
13bcc 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
13bcd 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65  connection curre
13bce 6e 74 6c 79 20 75 73 69 6e 67 20 74 68 69 73 20  ntly using this 
13bcf 42 74 72 65 65 20 2a 2f 0a 20 20 42 74 43 75 72  Btree */.  BtCur
13bd0 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20  sor *pCursor;   
13bd1 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 61 6c   /* A list of al
13bd2 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 2a  l open cursors *
13bd3 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  /.  MemPage *pPa
13bd4 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 46 69 72  ge1;      /* Fir
13bd5 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64  st page of the d
13bd6 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20  atabase */.  u8 
13bd7 69 6e 53 74 6d 74 3b 20 20 20 20 20 20 20 20 20  inStmt;         
13bd8 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65     /* True if we
13bd9 20 61 72 65 20 69 6e 20 61 20 73 74 61 74 65 6d   are in a statem
13bda 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69  ent subtransacti
13bdb 6f 6e 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f  on */.  u8 readO
13bdc 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  nly;          /*
13bdd 20 54 72 75 65 20 69 66 20 74 68 65 20 75 6e 64   True if the und
13bde 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 69 73 20  erlying file is 
13bdf 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38  readonly */.  u8
13be0 20 70 61 67 65 53 69 7a 65 46 69 78 65 64 3b 20   pageSizeFixed; 
13be1 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
13be2 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e  he page size can
13be3 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68   no longer be ch
13be4 61 6e 67 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66  anged */.#ifndef
13be5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
13be6 4f 56 41 43 55 55 4d 0a 20 20 75 38 20 61 75 74  OVACUUM.  u8 aut
13be7 6f 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20  oVacuum;        
13be8 2f 2a 20 54 72 75 65 20 69 66 20 61 75 74 6f 2d  /* True if auto-
13be9 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65  vacuum is enable
13bea 64 20 2a 2f 0a 20 20 75 38 20 69 6e 63 72 56 61  d */.  u8 incrVa
13beb 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20  cuum;        /* 
13bec 54 72 75 65 20 69 66 20 69 6e 63 72 2d 76 61 63  True if incr-vac
13bed 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a  uum is enabled *
13bee 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20 70  /.#endif.  u16 p
13bef 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  ageSize;        
13bf0 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
13bf1 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 61 20 70   of bytes on a p
13bf2 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 75 73 61  age */.  u16 usa
13bf3 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 2f  bleSize;       /
13bf4 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62  * Number of usab
13bf5 6c 65 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68  le bytes on each
13bf6 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6d   page */.  u16 m
13bf7 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20  axLocal;        
13bf8 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61   /* Maximum loca
13bf9 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e  l payload in non
13bfa 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 73  -LEAFDATA tables
13bfb 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63   */.  u16 minLoc
13bfc 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  al;         /* M
13bfd 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79  inimum local pay
13bfe 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46  load in non-LEAF
13bff 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20  DATA tables */. 
13c00 20 75 31 36 20 6d 61 78 4c 65 61 66 3b 20 20 20   u16 maxLeaf;   
13c01 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
13c02 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20  m local payload 
13c03 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61  in a LEAFDATA ta
13c04 62 6c 65 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e  ble */.  u16 min
13c05 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f  Leaf;          /
13c06 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20  * Minimum local 
13c07 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41  payload in a LEA
13c08 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20  FDATA table */. 
13c09 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f   u8 inTransactio
13c0a 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 61  n;     /* Transa
13c0b 63 74 69 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20  ction state */. 
13c0c 20 69 6e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f   int nTransactio
13c0d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n;     /* Number
13c0e 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e 73 61 63   of open transac
13c0f 74 69 6f 6e 73 20 28 72 65 61 64 20 2b 20 77 72  tions (read + wr
13c10 69 74 65 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ite) */.  void *
13c11 70 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20  pSchema;        
13c12 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 70  /* Pointer to sp
13c13 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  ace allocated by
13c14 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
13c15 65 6d 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20  ema() */.  void 
13c16 28 2a 78 46 72 65 65 53 63 68 65 6d 61 29 28 76  (*xFreeSchema)(v
13c17 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72  oid*);  /* Destr
13c18 75 63 74 6f 72 20 66 6f 72 20 42 74 53 68 61 72  uctor for BtShar
13c19 65 64 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20  ed.pSchema */.  
13c1a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
13c1b 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63  utex; /* Non-rec
13c1c 75 72 73 69 76 65 20 6d 75 74 65 78 20 72 65 71  ursive mutex req
13c1d 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73 20  uired to access 
13c1e 74 68 69 73 20 73 74 72 75 63 74 20 2a 2f 0a 23  this struct */.#
13c1f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
13c20 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
13c21 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
13c22 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
13c23 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
13c24 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
13c25 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  e */.  BtShared 
13c26 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20  *pNext;      /* 
13c27 4e 65 78 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f  Next on a list o
13c28 66 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61  f sharable BtSha
13c29 72 65 64 20 73 74 72 75 63 74 73 20 2a 2f 0a 20  red structs */. 
13c2a 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20   BtLock *pLock; 
13c2b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
13c2c 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20  f locks held on 
13c2d 74 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65  this shared-btre
13c2e 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 74  e struct */.  Bt
13c2f 72 65 65 20 2a 70 45 78 63 6c 75 73 69 76 65 3b  ree *pExclusive;
13c30 20 20 20 20 2f 2a 20 42 74 72 65 65 20 77 69 74      /* Btree wit
13c31 68 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  h an EXCLUSIVE l
13c32 6f 63 6b 20 6f 6e 20 74 68 65 20 77 68 6f 6c 65  ock on the whole
13c33 20 64 62 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20   db */.#endif.  
13c34 75 38 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20  u8 *pTmpSpace;  
13c35 20 20 20 20 20 20 2f 2a 20 42 74 53 68 61 72 65        /* BtShare
13c36 64 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  d.pageSize bytes
13c37 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d   of space for tm
13c38 70 20 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  p use */.};../*.
13c39 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
13c3a 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
13c3b 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
13c3c 64 20 74 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d  d to hold inform
13c3d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61  ation.** about a
13c3e 20 63 65 6c 6c 2e 20 20 54 68 65 20 70 61 72 73   cell.  The pars
13c3f 65 43 65 6c 6c 50 74 72 28 29 20 66 75 6e 63 74  eCellPtr() funct
13c40 69 6f 6e 20 66 69 6c 6c 73 20 69 6e 20 74 68 69  ion fills in thi
13c41 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 62  s structure.** b
13c42 61 73 65 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74  ased on informat
13c43 69 6f 6e 20 65 78 74 72 61 63 74 20 66 72 6f 6d  ion extract from
13c44 20 74 68 65 20 72 61 77 20 64 69 73 6b 20 70 61   the raw disk pa
13c45 67 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ge..*/.typedef s
13c46 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 43  truct CellInfo C
13c47 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20  ellInfo;.struct 
13c48 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20 20 75 38 20  CellInfo {.  u8 
13c49 2a 70 43 65 6c 6c 3b 20 20 20 20 20 2f 2a 20 50  *pCell;     /* P
13c4a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 74  ointer to the st
13c4b 61 72 74 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74  art of cell cont
13c4c 65 6e 74 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65  ent */.  i64 nKe
13c4d 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6b  y;      /* The k
13c4e 65 79 20 66 6f 72 20 49 4e 54 4b 45 59 20 74 61  ey for INTKEY ta
13c4f 62 6c 65 73 2c 20 6f 72 20 6e 75 6d 62 65 72 20  bles, or number 
13c50 6f 66 20 62 79 74 65 73 20 69 6e 20 6b 65 79 20  of bytes in key 
13c51 2a 2f 0a 20 20 75 33 32 20 6e 44 61 74 61 3b 20  */.  u32 nData; 
13c52 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
13c53 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 2a   bytes of data *
13c54 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64  /.  u32 nPayload
13c55 3b 20 20 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75  ;  /* Total amou
13c56 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f  nt of payload */
13c57 0a 20 20 75 31 36 20 6e 48 65 61 64 65 72 3b 20  .  u16 nHeader; 
13c58 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
13c59 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
13c5a 61 64 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f  ader in bytes */
13c5b 0a 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20  .  u16 nLocal;  
13c5c 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70    /* Amount of p
13c5d 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61  ayload held loca
13c5e 6c 6c 79 20 2a 2f 0a 20 20 75 31 36 20 69 4f 76  lly */.  u16 iOv
13c5f 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65  erflow; /* Offse
13c60 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61  t to overflow pa
13c61 67 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f  ge number.  Zero
13c62 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20   if no overflow 
13c63 2a 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b 20  */.  u16 nSize; 
13c64 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
13c65 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
13c66 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 72  on the main b-tr
13c67 65 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f  ee page */.};../
13c68 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70  *.** Maximum dep
13c69 74 68 20 6f 66 20 61 6e 20 53 51 4c 69 74 65 20  th of an SQLite 
13c6a 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 65  B-Tree structure
13c6b 2e 20 41 6e 79 20 42 2d 54 72 65 65 20 64 65 65  . Any B-Tree dee
13c6c 70 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 69 73  per than.** this
13c6d 20 77 69 6c 6c 20 62 65 20 64 65 63 6c 61 72 65   will be declare
13c6e 64 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 20  d corrupt. This 
13c6f 76 61 6c 75 65 20 69 73 20 63 61 6c 63 75 6c 61  value is calcula
13c70 74 65 64 20 62 61 73 65 64 20 6f 6e 20 61 0a 2a  ted based on a.*
13c71 2a 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61  * maximum databa
13c72 73 65 20 73 69 7a 65 20 6f 66 20 32 5e 33 31 20  se size of 2^31 
13c73 70 61 67 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20  pages a minimum 
13c74 66 61 6e 6f 75 74 20 6f 66 20 32 20 66 6f 72 20  fanout of 2 for 
13c75 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 61  a.** root-node a
13c76 6e 64 20 33 20 66 6f 72 20 61 6c 6c 20 6f 74 68  nd 3 for all oth
13c77 65 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  er internal node
13c78 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72  s..**.** If a tr
13c79 65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ee that appears 
13c7a 74 6f 20 62 65 20 74 61 6c 6c 65 72 20 74 68 61  to be taller tha
13c7b 6e 20 74 68 69 73 20 69 73 20 65 6e 63 6f 75 6e  n this is encoun
13c7c 74 65 72 65 64 2c 20 69 74 20 69 73 0a 2a 2a 20  tered, it is.** 
13c7d 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65  assumed that the
13c7e 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72   database is cor
13c7f 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rupt..*/.#define
13c80 20 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45   BTCURSOR_MAX_DE
13c81 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 20  PTH 20../*.** A 
13c82 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e  cursor is a poin
13c83 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63 75  ter to a particu
13c84 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69 6e  lar entry within
13c85 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
13c86 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20 61   b-tree within a
13c87 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
13c88 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 20  **.** The entry 
13c89 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  is identified by
13c8a 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e 64   its MemPage and
13c8b 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a   the index in.**
13c8c 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d   MemPage.aCell[]
13c8d 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a   of the entry..*
13c8e 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 69 6e 67  *.** When a sing
13c8f 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
13c90 20 63 61 6e 20 73 68 61 72 65 64 20 62 79 20 74   can shared by t
13c91 77 6f 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65  wo more database
13c92 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a   connections,.**
13c93 20 62 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e   but cursors can
13c94 6e 6f 74 20 62 65 20 73 68 61 72 65 64 2e 20 20  not be shared.  
13c95 45 61 63 68 20 63 75 72 73 6f 72 20 69 73 20 61  Each cursor is a
13c96 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
13c97 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64  .** particular d
13c98 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
13c99 6f 6e 20 69 64 65 6e 74 69 66 69 65 64 20 42 74  on identified Bt
13c9a 43 75 72 73 6f 72 2e 70 42 74 72 65 65 2e 64 62  Cursor.pBtree.db
13c9b 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69  ..**.** Fields i
13c9c 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
13c9d 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e   are accessed un
13c9e 64 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64  der the BtShared
13c9f 2e 6d 75 74 65 78 0a 2a 2a 20 66 6f 75 6e 64 20  .mutex.** found 
13ca0 61 74 20 73 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75  at self->pBt->mu
13ca1 74 65 78 2e 20 0a 2a 2f 0a 73 74 72 75 63 74 20  tex. .*/.struct 
13ca2 42 74 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 72  BtCursor {.  Btr
13ca3 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20  ee *pBtree;     
13ca4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74         /* The Bt
13ca5 72 65 65 20 74 6f 20 77 68 69 63 68 20 74 68 69  ree to which thi
13ca6 73 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73  s cursor belongs
13ca7 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a   */.  BtShared *
13ca8 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  pBt;            
13ca9 2f 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 20  /* The BtShared 
13caa 74 68 69 73 20 63 75 72 73 6f 72 20 70 6f 69 6e  this cursor poin
13cab 74 73 20 74 6f 20 2a 2f 0a 20 20 42 74 43 75 72  ts to */.  BtCur
13cac 73 6f 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72  sor *pNext, *pPr
13cad 65 76 3b 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20  ev;  /* Forms a 
13cae 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61  linked list of a
13caf 6c 6c 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20  ll cursors */.  
13cb0 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a  struct KeyInfo *
13cb1 70 4b 65 79 49 6e 66 6f 3b 20 2f 2a 20 41 72 67  pKeyInfo; /* Arg
13cb2 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20  ument passed to 
13cb3 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
13cb4 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ion */.  Pgno pg
13cb5 6e 6f 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20  noRoot;         
13cb6 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70     /* The root p
13cb7 61 67 65 20 6f 66 20 74 68 69 73 20 74 72 65 65  age of this tree
13cb8 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69   */.  CellInfo i
13cb9 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  nfo;            
13cba 2f 2a 20 41 20 70 61 72 73 65 20 6f 66 20 74 68  /* A parse of th
13cbb 65 20 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f  e cell we are po
13cbc 69 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75  inting at */.  u
13cbd 38 20 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20  8 wrFlag;       
13cbe 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
13cbf 20 69 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a   if writable */.
13cc0 20 20 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20    u8 atLast;    
13cc1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
13cc2 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74  ursor pointing t
13cc3 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  o the last entry
13cc4 20 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b   */.  u8 validNK
13cc5 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ey;             
13cc6 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e  /* True if info.
13cc7 6e 4b 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f  nKey is valid */
13cc8 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20 20 20  .  u8 eState;   
13cc9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13cca 4f 6e 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f  One of the CURSO
13ccb 52 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20  R_XXX constants 
13ccc 28 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20  (see below) */. 
13ccd 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20   void *pKey;    
13cce 20 20 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74    /* Saved key t
13ccf 68 61 74 20 77 61 73 20 63 75 72 73 6f 72 27 73  hat was cursor's
13cd0 20 6c 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69   last known posi
13cd1 74 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b  tion */.  i64 nK
13cd2 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69  ey;        /* Si
13cd3 7a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c  ze of pKey, or l
13cd4 61 73 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20  ast integer key 
13cd5 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 3b 20 20  */.  int skip;  
13cd6 20 20 20 20 20 20 2f 2a 20 28 73 6b 69 70 3c 30        /* (skip<0
13cd7 29 20 2d 3e 20 50 72 65 76 28 29 20 69 73 20 61  ) -> Prev() is a
13cd8 20 6e 6f 2d 6f 70 2e 20 28 73 6b 69 70 3e 30 29   no-op. (skip>0)
13cd9 20 2d 3e 20 4e 65 78 74 28 29 20 69 73 20 2a 2f   -> Next() is */
13cda 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
13cdb 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20  OMIT_INCRBLOB.  
13cdc 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  u8 isIncrblobHan
13cdd 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  dle;      /* Tru
13cde 65 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72  e if this cursor
13cdf 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20   is an incr. io 
13ce0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  handle */.  Pgno
13ce1 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20   *aOverflow;    
13ce2 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f        /* Cache o
13ce3 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  f overflow page 
13ce4 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e  locations */.#en
13ce5 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  dif.#ifndef NDEB
13ce6 55 47 0a 20 20 75 38 20 70 61 67 65 73 53 68 75  UG.  u8 pagesShu
13ce7 66 66 6c 65 64 3b 20 20 20 20 20 20 20 20 20 2f  ffled;         /
13ce8 2a 20 54 72 75 65 20 69 66 20 42 74 72 65 65 20  * True if Btree 
13ce9 70 61 67 65 73 20 61 72 65 20 72 65 61 72 72 61  pages are rearra
13cea 6e 67 65 64 20 62 79 20 62 61 6c 61 6e 63 65 28  nged by balance(
13ceb 29 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 31 36  )*/.#endif.  i16
13cec 20 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20   iPage;         
13ced 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13cee 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
13cef 75 72 72 65 6e 74 20 70 61 67 65 20 69 6e 20 61  urrent page in a
13cf0 70 50 61 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61  pPage */.  MemPa
13cf1 67 65 20 2a 61 70 50 61 67 65 5b 42 54 43 55 52  ge *apPage[BTCUR
13cf2 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20  SOR_MAX_DEPTH]; 
13cf3 20 2f 2a 20 50 61 67 65 73 20 66 72 6f 6d 20 72   /* Pages from r
13cf4 6f 6f 74 20 74 6f 20 63 75 72 72 65 6e 74 20 70  oot to current p
13cf5 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 61 69 49  age */.  u16 aiI
13cf6 64 78 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f  dx[BTCURSOR_MAX_
13cf7 44 45 50 54 48 5d 3b 20 20 20 20 20 20 20 20 2f  DEPTH];        /
13cf8 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20  * Current index 
13cf9 69 6e 20 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a  in apPage[i] */.
13cfa 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74  };../*.** Potent
13cfb 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 42  ial values for B
13cfc 74 43 75 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a  tCursor.eState..
13cfd 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c  **.** CURSOR_VAL
13cfe 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20  ID:.**   Cursor 
13cff 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c 69  points to a vali
13d00 64 20 65 6e 74 72 79 2e 20 67 65 74 50 61 79 6c  d entry. getPayl
13d01 6f 61 64 28 29 20 65 74 63 2e 20 6d 61 79 20 62  oad() etc. may b
13d02 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  e called..**.** 
13d03 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a  CURSOR_INVALID:.
13d04 2a 2a 20 20 20 43 75 72 73 6f 72 20 64 6f 65 73  **   Cursor does
13d05 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 20   not point to a 
13d06 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 54 68 69  valid entry. Thi
13d07 73 20 63 61 6e 20 68 61 70 70 65 6e 20 28 66 6f  s can happen (fo
13d08 72 20 65 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20  r example) .**  
13d09 20 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62   because the tab
13d0a 6c 65 20 69 73 20 65 6d 70 74 79 20 6f 72 20 62  le is empty or b
13d0b 65 63 61 75 73 65 20 42 74 72 65 65 43 75 72 73  ecause BtreeCurs
13d0c 6f 72 46 69 72 73 74 28 29 20 68 61 73 20 6e 6f  orFirst() has no
13d0d 74 20 62 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c  t been.**   call
13d0e 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52  ed..**.** CURSOR
13d0f 5f 52 45 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a  _REQUIRESEEK:.**
13d10 20 20 20 54 68 65 20 74 61 62 6c 65 20 74 68 61     The table tha
13d11 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 77 61  t this cursor wa
13d12 73 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c  s opened on stil
13d13 6c 20 65 78 69 73 74 73 2c 20 62 75 74 20 68 61  l exists, but ha
13d14 73 20 62 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64  s been .**   mod
13d15 69 66 69 65 64 20 73 69 6e 63 65 20 74 68 65 20  ified since the 
13d16 63 75 72 73 6f 72 20 77 61 73 20 6c 61 73 74 20  cursor was last 
13d17 75 73 65 64 2e 20 54 68 65 20 63 75 72 73 6f 72  used. The cursor
13d18 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 73 61 76   position is sav
13d19 65 64 0a 2a 2a 20 20 20 69 6e 20 76 61 72 69 61  ed.**   in varia
13d1a 62 6c 65 73 20 42 74 43 75 72 73 6f 72 2e 70 4b  bles BtCursor.pK
13d1b 65 79 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e  ey and BtCursor.
13d1c 6e 4b 65 79 2e 20 57 68 65 6e 20 61 20 63 75 72  nKey. When a cur
13d1d 73 6f 72 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20  sor is in .**   
13d1e 74 68 69 73 20 73 74 61 74 65 2c 20 72 65 73 74  this state, rest
13d1f 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
13d20 6e 28 29 20 63 61 6e 20 62 65 20 63 61 6c 6c 65  n() can be calle
13d21 64 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a  d to attempt to.
13d22 2a 2a 20 20 20 73 65 65 6b 20 74 68 65 20 63 75  **   seek the cu
13d23 72 73 6f 72 20 74 6f 20 74 68 65 20 73 61 76 65  rsor to the save
13d24 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a  d position..**.*
13d25 2a 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a  * CURSOR_FAULT:.
13d26 2a 2a 20 20 20 41 20 75 6e 72 65 63 6f 76 65 72  **   A unrecover
13d27 61 62 6c 65 20 65 72 72 6f 72 20 28 61 6e 20 49  able error (an I
13d28 2f 4f 20 65 72 72 6f 72 20 6f 72 20 61 20 6d 61  /O error or a ma
13d29 6c 6c 6f 63 20 66 61 69 6c 75 72 65 29 20 68 61  lloc failure) ha
13d2a 73 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 20 20  s occurred.**   
13d2b 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 63  on a different c
13d2c 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 73  onnection that s
13d2d 68 61 72 65 73 20 74 68 65 20 42 74 53 68 61 72  hares the BtShar
13d2e 65 64 20 63 61 63 68 65 20 77 69 74 68 20 74 68  ed cache with th
13d2f 69 73 0a 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20  is.**   cursor. 
13d30 20 54 68 65 20 65 72 72 6f 72 20 68 61 73 20 6c   The error has l
13d31 65 66 74 20 74 68 65 20 63 61 63 68 65 20 69 6e  eft the cache in
13d32 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74   an inconsistent
13d33 20 73 74 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20   state..**   Do 
13d34 6e 6f 74 68 69 6e 67 20 65 6c 73 65 20 77 69 74  nothing else wit
13d35 68 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 20  h this cursor.  
13d36 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75  Any attempt to u
13d37 73 65 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  se the cursor.**
13d38 20 20 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e     should return
13d39 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
13d3a 73 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73  stored in BtCurs
13d3b 6f 72 2e 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69  or.skip.*/.#defi
13d3c 6e 65 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  ne CURSOR_INVALI
13d3d 44 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 64  D           0.#d
13d3e 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c  efine CURSOR_VAL
13d3f 49 44 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ID             1
13d40 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f  .#define CURSOR_
13d41 52 45 51 55 49 52 45 53 45 45 4b 20 20 20 20 20  REQUIRESEEK     
13d42 20 20 32 0a 23 64 65 66 69 6e 65 20 43 55 52 53    2.#define CURS
13d43 4f 52 5f 46 41 55 4c 54 20 20 20 20 20 20 20 20  OR_FAULT        
13d44 20 20 20 20 20 33 0a 0a 2f 2a 20 54 68 65 20 64       3../* The d
13d45 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68 65  atabase page the
13d46 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63   PENDING_BYTE oc
13d47 63 75 70 69 65 73 2e 20 54 68 69 73 20 70 61 67  cupies. This pag
13d48 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e  e is never used.
13d49 0a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 6d  .** TODO: This m
13d4a 61 63 72 6f 20 69 73 20 76 65 72 79 20 73 69 6d  acro is very sim
13d4b 69 6c 61 72 79 20 74 6f 20 50 41 47 45 52 5f 4d  ilary to PAGER_M
13d4c 4a 5f 50 47 4e 4f 28 29 20 69 6e 20 70 61 67 65  J_PGNO() in page
13d4d 72 2e 63 2e 20 54 68 65 79 0a 2a 2a 20 73 68 6f  r.c. They.** sho
13d4e 75 6c 64 20 70 6f 73 73 69 62 6c 79 20 62 65 20  uld possibly be 
13d4f 63 6f 6e 73 6f 6c 69 64 61 74 65 64 20 28 70 72  consolidated (pr
13d50 65 73 75 6d 61 62 6c 79 20 69 6e 20 70 61 67 65  esumably in page
13d51 72 2e 68 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64  r.h)..**.** If d
13d52 69 73 6b 20 49 2f 4f 20 69 73 20 6f 6d 69 74 74  isk I/O is omitt
13d53 65 64 20 28 6d 65 61 6e 69 6e 67 20 74 68 61 74  ed (meaning that
13d54 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
13d55 20 73 74 6f 72 65 64 20 70 75 72 65 6c 79 0a 2a   stored purely.*
13d56 2a 20 69 6e 20 6d 65 6d 6f 72 79 29 20 74 68 65  * in memory) the
13d57 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 65  n there is no pe
13d58 6e 64 69 6e 67 20 62 79 74 65 2e 0a 2a 2f 0a 23  nding byte..*/.#
13d59 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
13d5a 54 5f 44 49 53 4b 49 4f 0a 23 20 64 65 66 69 6e  T_DISKIO.# defin
13d5b 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  e PENDING_BYTE_P
13d5c 41 47 45 28 70 42 74 29 20 20 30 78 37 66 66 66  AGE(pBt)  0x7fff
13d5d 66 66 66 66 0a 23 65 6c 73 65 0a 23 20 64 65 66  ffff.#else.# def
13d5e 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  ine PENDING_BYTE
13d5f 5f 50 41 47 45 28 70 42 74 29 20 28 28 50 67 6e  _PAGE(pBt) ((Pgn
13d60 6f 29 28 28 50 45 4e 44 49 4e 47 5f 42 59 54 45  o)((PENDING_BYTE
13d61 2f 28 70 42 74 29 2d 3e 70 61 67 65 53 69 7a 65  /(pBt)->pageSize
13d62 29 2b 31 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  )+1)).#endif../*
13d63 0a 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73  .** A linked lis
13d64 74 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  t of the followi
13d65 6e 67 20 73 74 72 75 63 74 75 72 65 73 20 69 73  ng structures is
13d66 20 73 74 6f 72 65 64 20 61 74 20 42 74 53 68 61   stored at BtSha
13d67 72 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f  red.pLock..** Lo
13d68 63 6b 73 20 61 72 65 20 61 64 64 65 64 20 28 6f  cks are added (o
13d69 72 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d 20  r upgraded from 
13d6a 52 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49  READ_LOCK to WRI
13d6b 54 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20  TE_LOCK) when a 
13d6c 63 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70  cursor .** is op
13d6d 65 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62 6c  ened on the tabl
13d6e 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65  e with root page
13d6f 20 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c 65   BtShared.iTable
13d70 2e 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f  . Locks are remo
13d71 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73  ved.** from this
13d72 20 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72 61   list when a tra
13d73 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d  nsaction is comm
13d74 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20  itted or rolled 
13d75 62 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a  back, or when.**
13d76 20 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20   a btree handle 
13d77 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74  is closed..*/.st
13d78 72 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20  ruct BtLock {.  
13d79 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 20  Btree *pBtree;  
13d7a 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 68        /* Btree h
13d7b 61 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68  andle holding th
13d7c 69 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e  is lock */.  Pgn
13d7d 6f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20  o iTable;       
13d7e 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20     /* Root page 
13d7f 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38  of table */.  u8
13d80 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20   eLock;         
13d81 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b      /* READ_LOCK
13d82 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a   or WRITE_LOCK *
13d83 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78  /.  BtLock *pNex
13d84 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  t;        /* Nex
13d85 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 4c  t in BtShared.pL
13d86 6f 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a  ock list */.};..
13d87 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c  /* Candidate val
13d88 75 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65  ues for BtLock.e
13d89 4c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20  Lock */.#define 
13d8a 52 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a  READ_LOCK     1.
13d8b 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f  #define WRITE_LO
13d8c 43 4b 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 54  CK    2../*.** T
13d8d 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 66 69  hese macros defi
13d8e 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20  ne the location 
13d8f 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  of the pointer-m
13d90 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61 20 0a  ap entry for a .
13d91 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ** database page
13d92 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  . The first argu
13d93 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69 73 20  ment to each is 
13d94 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73  the number of us
13d95 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20 6f 6e  able.** bytes on
13d96 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68   each page of th
13d97 65 20 64 61 74 61 62 61 73 65 20 28 6f 66 74 65  e database (ofte
13d98 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73 65 63  n 1024). The sec
13d99 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20 70 61  ond is the.** pa
13d9a 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 6f  ge number to loo
13d9b 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f 69 6e  k up in the poin
13d9c 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50  ter map..**.** P
13d9d 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72 65 74  TRMAP_PAGENO ret
13d9e 75 72 6e 73 20 74 68 65 20 64 61 74 61 62 61 73  urns the databas
13d9f 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  e page number of
13da0 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
13da1 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20 73 74  .** page that st
13da2 6f 72 65 73 20 74 68 65 20 72 65 71 75 69 72 65  ores the require
13da3 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52 4d 41  d pointer. PTRMA
13da4 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65 74 75  P_PTROFFSET retu
13da5 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65  rns.** the offse
13da6 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  t of the request
13da7 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a  ed map entry..**
13da8 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e 6f 20  .** If the pgno 
13da9 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
13daa 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  to PTRMAP_PAGENO
13dab 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61   is a pointer-ma
13dac 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 6e 20  p page,.** then 
13dad 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e 65 64  pgno is returned
13dae 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 52 4d  . So (pgno==PTRM
13daf 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a 2c 20  AP_PAGENO(pgsz, 
13db0 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a 2a 2a  pgno)) can be.**
13db1 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 69 66   used to test if
13db2 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69 6e 74   pgno is a point
13db3 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50 54 52  er-map page. PTR
13db4 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70 6c 65  MAP_ISPAGE imple
13db5 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20 74 65  ments.** this te
13db6 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  st..*/.#define P
13db7 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
13db8 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70 50 61  , pgno) ptrmapPa
13db9 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f 29 0a  geno(pBt, pgno).
13dba 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 50  #define PTRMAP_P
13dbb 54 52 4f 46 46 53 45 54 28 70 67 70 74 72 6d 61  TROFFSET(pgptrma
13dbc 70 2c 20 70 67 6e 6f 29 20 28 35 2a 28 70 67 6e  p, pgno) (5*(pgn
13dbd 6f 2d 70 67 70 74 72 6d 61 70 2d 31 29 29 0a 23  o-pgptrmap-1)).#
13dbe 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 49 53  define PTRMAP_IS
13dbf 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f 29 20  PAGE(pBt, pgno) 
13dc0 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28  (PTRMAP_PAGENO((
13dc1 70 42 74 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70  pBt),(pgno))==(p
13dc2 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  gno))../*.** The
13dc3 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20   pointer map is 
13dc4 61 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74  a lookup table t
13dc5 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 74  hat identifies t
13dc6 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 66  he parent page f
13dc7 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64  or.** each child
13dc8 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
13dc9 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65  abase file.  The
13dca 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20   parent page is 
13dcb 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a  the page that.**
13dcc 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e   contains a poin
13dcd 74 65 72 20 74 6f 20 74 68 65 20 63 68 69 6c 64  ter to the child
13dce 2e 20 20 45 76 65 72 79 20 70 61 67 65 20 69 6e  .  Every page in
13dcf 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
13dd0 6e 74 61 69 6e 73 0a 2a 2a 20 30 20 6f 72 20 31  ntains.** 0 or 1
13dd1 20 70 61 72 65 6e 74 20 70 61 67 65 73 2e 20 20   parent pages.  
13dd2 28 49 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74  (In this context
13dd3 20 27 64 61 74 61 62 61 73 65 20 70 61 67 65 27   'database page'
13dd4 20 72 65 66 65 72 73 0a 2a 2a 20 74 6f 20 61 6e   refers.** to an
13dd5 79 20 70 61 67 65 20 74 68 61 74 20 69 73 20 6e  y page that is n
13dd6 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 70  ot part of the p
13dd7 6f 69 6e 74 65 72 20 6d 61 70 20 69 74 73 65 6c  ointer map itsel
13dd8 66 2e 29 20 20 45 61 63 68 20 70 6f 69 6e 74 65  f.)  Each pointe
13dd9 72 20 6d 61 70 0a 2a 2a 20 65 6e 74 72 79 20 63  r map.** entry c
13dda 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 69 6e  onsists of a sin
13ddb 67 6c 65 20 62 79 74 65 20 27 74 79 70 65 27 20  gle byte 'type' 
13ddc 61 6e 64 20 61 20 34 20 62 79 74 65 20 70 61 72  and a 4 byte par
13ddd 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e  ent page number.
13dde 0a 2a 2a 20 54 68 65 20 50 54 52 4d 41 50 5f 58  .** The PTRMAP_X
13ddf 58 58 20 69 64 65 6e 74 69 66 69 65 72 73 20 62  XX identifiers b
13de0 65 6c 6f 77 20 61 72 65 20 74 68 65 20 76 61 6c  elow are the val
13de1 69 64 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20  id types..**.** 
13de2 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74  The purpose of t
13de3 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
13de4 73 20 74 6f 20 66 61 63 69 6c 69 74 79 20 6d 6f  s to facility mo
13de5 76 69 6e 67 20 70 61 67 65 73 20 66 72 6f 6d 20  ving pages from 
13de6 6f 6e 65 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20  one.** position 
13de7 69 6e 20 74 68 65 20 66 69 6c 65 20 74 6f 20 61  in the file to a
13de8 6e 6f 74 68 65 72 20 61 73 20 70 61 72 74 20 6f  nother as part o
13de9 66 20 61 75 74 6f 76 61 63 75 75 6d 2e 20 20 57  f autovacuum.  W
13dea 68 65 6e 20 61 20 70 61 67 65 0a 2a 2a 20 69 73  hen a page.** is
13deb 20 6d 6f 76 65 64 2c 20 74 68 65 20 70 6f 69 6e   moved, the poin
13dec 74 65 72 20 69 6e 20 69 74 73 20 70 61 72 65 6e  ter in its paren
13ded 74 20 6d 75 73 74 20 62 65 20 75 70 64 61 74 65  t must be update
13dee 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  d to point to th
13def 65 0a 2a 2a 20 6e 65 77 20 6c 6f 63 61 74 69 6f  e.** new locatio
13df0 6e 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20  n.  The pointer 
13df1 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f 20 6c  map is used to l
13df2 6f 63 61 74 65 20 74 68 65 20 70 61 72 65 6e 74  ocate the parent
13df3 20 70 61 67 65 20 71 75 69 63 6b 6c 79 2e 0a 2a   page quickly..*
13df4 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54  *.** PTRMAP_ROOT
13df5 50 41 47 45 3a 20 54 68 65 20 64 61 74 61 62 61  PAGE: The databa
13df6 73 65 20 70 61 67 65 20 69 73 20 61 20 72 6f 6f  se page is a roo
13df7 74 2d 70 61 67 65 2e 20 54 68 65 20 70 61 67 65  t-page. The page
13df8 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 0a 2a  -number is not.*
13df9 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
13dfa 20 20 20 75 73 65 64 20 69 6e 20 74 68 69 73 20     used in this 
13dfb 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d  case..**.** PTRM
13dfc 41 50 5f 46 52 45 45 50 41 47 45 3a 20 54 68 65  AP_FREEPAGE: The
13dfd 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69   database page i
13dfe 73 20 61 6e 20 75 6e 75 73 65 64 20 28 66 72 65  s an unused (fre
13dff 65 29 20 70 61 67 65 2e 20 54 68 65 20 70 61 67  e) page. The pag
13e00 65 2d 6e 75 6d 62 65 72 20 0a 2a 2a 20 20 20 20  e-number .**    
13e01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73                is
13e02 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 74 68 69   not used in thi
13e03 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54  s case..**.** PT
13e04 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20  RMAP_OVERFLOW1: 
13e05 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  The database pag
13e06 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70  e is the first p
13e07 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66  age in a list of
13e08 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
13e09 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20         overflow 
13e0a 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 65 20  pages. The page 
13e0b 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65  number identifie
13e0c 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a  s the page that.
13e0d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
13e0e 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 74 68       contains th
13e0f 65 20 63 65 6c 6c 20 77 69 74 68 20 61 20 70 6f  e cell with a po
13e10 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 6f 76  inter to this ov
13e11 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a  erflow page..**.
13e12 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  ** PTRMAP_OVERFL
13e13 4f 57 32 3a 20 54 68 65 20 64 61 74 61 62 61 73  OW2: The databas
13e14 65 20 70 61 67 65 20 69 73 20 74 68 65 20 73 65  e page is the se
13e15 63 6f 6e 64 20 6f 72 20 6c 61 74 65 72 20 70 61  cond or later pa
13e16 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 0a  ge in a list of.
13e17 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
13e18 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61       overflow pa
13e19 67 65 73 2e 20 54 68 65 20 70 61 67 65 2d 6e 75  ges. The page-nu
13e1a 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 73 20  mber identifies 
13e1b 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20  the previous.** 
13e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13e1d 20 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76    page in the ov
13e1e 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74  erflow page list
13e1f 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42  ..**.** PTRMAP_B
13e20 54 52 45 45 3a 20 54 68 65 20 64 61 74 61 62 61  TREE: The databa
13e21 73 65 20 70 61 67 65 20 69 73 20 61 20 6e 6f 6e  se page is a non
13e22 2d 72 6f 6f 74 20 62 74 72 65 65 20 70 61 67 65  -root btree page
13e23 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  . The page numbe
13e24 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
13e25 20 20 20 69 64 65 6e 74 69 66 69 65 73 20 74 68     identifies th
13e26 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 6e  e parent page in
13e27 20 74 68 65 20 62 74 72 65 65 2e 0a 2a 2f 0a 23   the btree..*/.#
13e28 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f  define PTRMAP_RO
13e29 4f 54 50 41 47 45 20 31 0a 23 64 65 66 69 6e 65  OTPAGE 1.#define
13e2a 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45   PTRMAP_FREEPAGE
13e2b 20 32 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41   2.#define PTRMA
13e2c 50 5f 4f 56 45 52 46 4c 4f 57 31 20 33 0a 23 64  P_OVERFLOW1 3.#d
13e2d 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45  efine PTRMAP_OVE
13e2e 52 46 4c 4f 57 32 20 34 0a 23 64 65 66 69 6e 65  RFLOW2 4.#define
13e2f 20 50 54 52 4d 41 50 5f 42 54 52 45 45 20 35 0a   PTRMAP_BTREE 5.
13e30 0a 2f 2a 20 41 20 62 75 6e 63 68 20 6f 66 20 61  ./* A bunch of a
13e31 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
13e32 74 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20  ts to check the 
13e33 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74  transaction stat
13e34 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f  e variables.** o
13e35 66 20 68 61 6e 64 6c 65 20 70 20 28 74 79 70 65  f handle p (type
13e36 20 42 74 72 65 65 2a 29 20 61 72 65 20 69 6e 74   Btree*) are int
13e37 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65  ernally consiste
13e38 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62  nt..*/.#define b
13e39 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29  treeIntegrity(p)
13e3a 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e   \.  assert( p->
13e3b 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
13e3c 6f 6e 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c  on!=TRANS_NONE |
13e3d 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73  | p->pBt->nTrans
13e3e 61 63 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20  action==0 ); \. 
13e3f 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d   assert( p->pBt-
13e40 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 3d  >inTransaction>=
13e41 70 2d 3e 69 6e 54 72 61 6e 73 20 29 3b 20 0a 0a  p->inTrans ); ..
13e42 0a 2f 2a 0a 2a 2a 20 54 68 65 20 49 53 41 55 54  ./*.** The ISAUT
13e43 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 20 69 73  OVACUUM macro is
13e44 20 75 73 65 64 20 77 69 74 68 69 6e 20 62 61 6c   used within bal
13e45 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74  ance_nonroot() t
13e46 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 69  o determine.** i
13e47 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  f the database s
13e48 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63  upports auto-vac
13e49 75 75 6d 20 6f 72 20 6e 6f 74 2e 20 42 65 63 61  uum or not. Beca
13e4a 75 73 65 20 69 74 20 69 73 20 75 73 65 64 0a 2a  use it is used.*
13e4b 2a 20 77 69 74 68 69 6e 20 61 6e 20 65 78 70 72  * within an expr
13e4c 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61  ession that is a
13e4d 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 6e  n argument to an
13e4e 6f 74 68 65 72 20 6d 61 63 72 6f 20 0a 2a 2a 20  other macro .** 
13e4f 28 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77  (sqliteMallocRaw
13e50 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  ), it is not pos
13e51 73 69 62 6c 65 20 74 6f 20 75 73 65 20 63 6f 6e  sible to use con
13e52 64 69 74 69 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61  ditional compila
13e53 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69  tion..** So, thi
13e54 73 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e  s macro is defin
13e55 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23  ed instead..*/.#
13e56 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
13e57 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 23 64  IT_AUTOVACUUM.#d
13e58 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55  efine ISAUTOVACU
13e59 55 4d 20 28 70 42 74 2d 3e 61 75 74 6f 56 61 63  UM (pBt->autoVac
13e5a 75 75 6d 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  uum).#else.#defi
13e5b 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  ne ISAUTOVACUUM 
13e5c 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
13e5d 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
13e5e 69 73 20 70 61 73 73 65 64 20 61 72 6f 75 6e 64  is passed around
13e5f 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65   through all the
13e60 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   sanity checking
13e61 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20   routines.** in 
13e62 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 72  order to keep tr
13e63 61 63 6b 20 6f 66 20 73 6f 6d 65 20 67 6c 6f 62  ack of some glob
13e64 61 6c 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61  al state informa
13e65 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tion..*/.typedef
13e66 20 73 74 72 75 63 74 20 49 6e 74 65 67 72 69 74   struct Integrit
13e67 79 43 6b 20 49 6e 74 65 67 72 69 74 79 43 6b 3b  yCk IntegrityCk;
13e68 0a 73 74 72 75 63 74 20 49 6e 74 65 67 72 69 74  .struct Integrit
13e69 79 43 6b 20 7b 0a 20 20 42 74 53 68 61 72 65 64  yCk {.  BtShared
13e6a 20 2a 70 42 74 3b 20 20 20 20 2f 2a 20 54 68 65   *pBt;    /* The
13e6b 20 74 72 65 65 20 62 65 69 6e 67 20 63 68 65 63   tree being chec
13e6c 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 50 61 67  ked out */.  Pag
13e6d 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 2f  er *pPager;    /
13e6e 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64  * The associated
13e6f 20 70 61 67 65 72 2e 20 20 41 6c 73 6f 20 61 63   pager.  Also ac
13e70 63 65 73 73 69 62 6c 65 20 62 79 20 70 42 74 2d  cessible by pBt-
13e71 3e 70 50 61 67 65 72 20 2a 2f 0a 20 20 50 67 6e  >pPager */.  Pgn
13e72 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 2f  o nPage;       /
13e73 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
13e74 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
13e75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e 52 65  e */.  int *anRe
13e76 66 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  f;       /* Numb
13e77 65 72 20 6f 66 20 74 69 6d 65 73 20 65 61 63 68  er of times each
13e78 20 70 61 67 65 20 69 73 20 72 65 66 65 72 65 6e   page is referen
13e79 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45  ced */.  int mxE
13e7a 72 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 74  rr;        /* St
13e7b 6f 70 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20  op accumulating 
13e7c 65 72 72 6f 72 73 20 77 68 65 6e 20 74 68 69 73  errors when this
13e7d 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 2a 2f   reaches zero */
13e7e 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20  .  int nErr;    
13e7f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
13e80 66 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 74  f messages writt
13e81 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f  en to zErrMsg so
13e82 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61   far */.  int ma
13e83 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 2f 2a 20 41  llocFailed; /* A
13e84 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
13e85 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  on error has occ
13e86 75 72 72 65 64 20 2a 2f 0a 20 20 53 74 72 41 63  urred */.  StrAc
13e87 63 75 6d 20 65 72 72 4d 73 67 3b 20 20 2f 2a 20  cum errMsg;  /* 
13e88 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 65  Accumulate the e
13e89 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78  rror message tex
13e8a 74 20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  t here */.};../*
13e8b 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74  .** Read or writ
13e8c 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75  e a two- and fou
13e8d 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61  r-byte big-endia
13e8e 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  n integer values
13e8f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74  ..*/.#define get
13e90 32 62 79 74 65 28 78 29 20 20 20 28 28 78 29 5b  2byte(x)   ((x)[
13e91 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a  0]<<8 | (x)[1]).
13e92 23 64 65 66 69 6e 65 20 70 75 74 32 62 79 74 65  #define put2byte
13e93 28 70 2c 76 29 20 28 28 70 29 5b 30 5d 20 3d 20  (p,v) ((p)[0] = 
13e94 28 75 38 29 28 28 76 29 3e 3e 38 29 2c 20 28 70  (u8)((v)>>8), (p
13e95 29 5b 31 5d 20 3d 20 28 75 38 29 28 76 29 29 0a  )[1] = (u8)(v)).
13e96 23 64 65 66 69 6e 65 20 67 65 74 34 62 79 74 65  #define get4byte
13e97 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
13e98 0a 23 64 65 66 69 6e 65 20 70 75 74 34 62 79 74  .#define put4byt
13e99 65 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74  e sqlite3Put4byt
13e9a 65 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61  e../*.** Interna
13e9b 6c 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  l routines that 
13e9c 73 68 6f 75 6c 64 20 62 65 20 61 63 63 65 73 73  should be access
13e9d 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20  ed by the btree 
13e9e 6c 61 79 65 72 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53  layer only..*/.S
13e9f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
13ea0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
13ea1 74 50 61 67 65 28 42 74 53 68 61 72 65 64 2a 2c  tPage(BtShared*,
13ea2 20 50 67 6e 6f 2c 20 4d 65 6d 50 61 67 65 2a 2a   Pgno, MemPage**
13ea3 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
13ea4 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13ea5 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28  e3BtreeInitPage(
13ea6 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b  MemPage *pPage);
13ea7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13ea8 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
13ea9 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 4d 65  eParseCellPtr(Me
13eaa 6d 50 61 67 65 2a 2c 20 75 38 2a 2c 20 43 65 6c  mPage*, u8*, Cel
13eab 6c 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f  lInfo*);.SQLITE_
13eac 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
13ead 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
13eae 6c 6c 28 4d 65 6d 50 61 67 65 2a 2c 20 69 6e 74  ll(MemPage*, int
13eaf 2c 20 43 65 6c 6c 49 6e 66 6f 2a 29 3b 0a 53 51  , CellInfo*);.SQ
13eb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13eb1 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 73   sqlite3BtreeRes
13eb2 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
13eb3 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  on(BtCursor *pCu
13eb4 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
13eb5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
13eb6 74 72 65 65 47 65 74 54 65 6d 70 43 75 72 73 6f  treeGetTempCurso
13eb7 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  r(BtCursor *pCur
13eb8 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 54 65 6d  , BtCursor *pTem
13eb9 70 43 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pCur);.SQLITE_PR
13eba 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
13ebb 65 33 42 74 72 65 65 52 65 6c 65 61 73 65 54 65  e3BtreeReleaseTe
13ebc 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f  mpCursor(BtCurso
13ebd 72 20 2a 70 43 75 72 29 3b 0a 53 51 4c 49 54 45  r *pCur);.SQLITE
13ebe 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
13ebf 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f  lite3BtreeMoveTo
13ec0 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f 72 20  Parent(BtCursor 
13ec1 2a 70 43 75 72 29 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a  *pCur);../******
13ec2 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
13ec3 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  btreeInt.h *****
13ec4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13ec5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13ec6 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
13ec7 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
13ec8 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
13ec9 74 20 6f 66 66 20 69 6e 20 62 74 6d 75 74 65 78  t off in btmutex
13eca 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
13ecb 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 20 53 51 4c  *******/.#if SQL
13ecc 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26  ITE_THREADSAFE &
13ecd 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
13ece 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
13ecf 43 48 45 29 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74  CHE).../*.** Ent
13ed0 65 72 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68  er a mutex on th
13ed1 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62  e given BTree ob
13ed2 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ject..**.** If t
13ed3 68 65 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74  he object is not
13ed4 20 73 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20   sharable, then 
13ed5 6e 6f 20 6d 75 74 65 78 20 69 73 20 65 76 65 72  no mutex is ever
13ed6 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64   required.** and
13ed7 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
13ed8 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75   a no-op.  The u
13ed9 6e 64 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20  nderlying mutex 
13eda 69 73 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65  is non-recursive
13edb 2e 0a 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70  ..** But we keep
13edc 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75   a reference cou
13edd 6e 74 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74  nt in Btree.want
13ede 54 6f 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65  ToLock so the be
13edf 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69  havior.** of thi
13ee0 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72  s interface is r
13ee1 65 63 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  ecursive..**.** 
13ee2 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63  To avoid deadloc
13ee3 6b 73 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72  ks, multiple Btr
13ee4 65 65 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69  ees are locked i
13ee5 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72  n the same order
13ee6 0a 2a 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62  .** by all datab
13ee7 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ase connections.
13ee8 20 20 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69    The p->pNext i
13ee9 73 20 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65  s a list of othe
13eea 72 0a 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f  r.** Btrees belo
13eeb 6e 67 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d  nging to the sam
13eec 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
13eed 63 74 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42  ction as the p B
13eee 74 72 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65  tree.** which ne
13eef 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20  ed to be locked 
13ef0 61 66 74 65 72 20 70 2e 20 20 49 66 20 77 65 20  after p.  If we 
13ef1 63 61 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63  cannot get a loc
13ef2 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20  k on.** p, then 
13ef3 66 69 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c  first unlock all
13ef4 20 6f 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f   of the others o
13ef5 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e  n p->pNext, then
13ef6 20 77 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65   wait.** for the
13ef7 20 6c 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20   lock to become 
13ef8 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20  available on p, 
13ef9 74 68 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20  then relock all 
13efa 6f 66 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71  of the.** subseq
13efb 75 65 6e 74 20 42 74 72 65 65 73 20 74 68 61 74  uent Btrees that
13efc 20 64 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a   desire a lock..
13efd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
13efe 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
13eff 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a  reeEnter(Btree *
13f00 70 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61  p){.  Btree *pLa
13f01 74 65 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20  ter;..  /* Some 
13f02 62 61 73 69 63 20 73 61 6e 69 74 79 20 63 68 65  basic sanity che
13f03 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72  cking on the Btr
13f04 65 65 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66  ee.  The list of
13f05 20 42 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e   Btrees.  ** con
13f06 6e 65 63 74 65 64 20 62 79 20 70 4e 65 78 74 20  nected by pNext 
13f07 61 6e 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64  and pPrev should
13f08 20 62 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72   be in sorted or
13f09 64 65 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65  der by.  ** Btre
13f0a 65 2e 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c  e.pBt value. All
13f0b 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
13f0c 20 6c 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c   list should bel
13f0d 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20  ong to.  ** the 
13f0e 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  same connection.
13f0f 20 4f 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72   Only shared Btr
13f10 65 65 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c  ees are on the l
13f11 69 73 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ist. */.  assert
13f12 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c  ( p->pNext==0 ||
13f13 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70   p->pNext->pBt>p
13f14 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72  ->pBt );.  asser
13f15 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c  t( p->pPrev==0 |
13f16 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c  | p->pPrev->pBt<
13f17 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65  p->pBt );.  asse
13f18 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20  rt( p->pNext==0 
13f19 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d  || p->pNext->db=
13f1a 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65  =p->db );.  asse
13f1b 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20  rt( p->pPrev==0 
13f1c 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d  || p->pPrev->db=
13f1d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65  =p->db );.  asse
13f1e 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  rt( p->sharable 
13f1f 7c 7c 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20  || (p->pNext==0 
13f20 26 26 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20  && p->pPrev==0) 
13f21 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66  );..  /* Check f
13f22 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69  or locking consi
13f23 73 74 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65  stency */.  asse
13f24 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c  rt( !p->locked |
13f25 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e  | p->wantToLock>
13f26 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
13f27 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d  ->sharable || p-
13f28 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29  >wantToLock==0 )
13f29 3b 0a 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c  ;..  /* We shoul
13f2a 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61  d already hold a
13f2b 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
13f2c 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
13f2d 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71   */.  assert( sq
13f2e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
13f2f 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
13f30 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61  ;..  if( !p->sha
13f31 72 61 62 6c 65 20 29 20 72 65 74 75 72 6e 3b 0a  rable ) return;.
13f32 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b    p->wantToLock+
13f33 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63 6b  +;.  if( p->lock
13f34 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  ed ) return;..  
13f35 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73  /* In most cases
13f36 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 65 20 61  , we should be a
13f37 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74  ble to acquire t
13f38 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a 20  he lock we.  ** 
13f39 77 61 6e 74 20 77 69 74 68 6f 75 74 20 68 61 76  want without hav
13f3a 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 67  ing to go throug
13f3b 68 74 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67  ht the ascending
13f3c 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 65   lock.  ** proce
13f3d 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77  dure that follow
13f3e 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75 72 65  s.  Just be sure
13f3f 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20   not to block.. 
13f40 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
13f41 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 70  3_mutex_try(p->p
13f42 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c 49  Bt->mutex)==SQLI
13f43 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d 3e  TE_OK ){.    p->
13f44 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20  locked = 1;.    
13f45 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  return;.  }..  /
13f46 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c  * To avoid deadl
13f47 6f 63 6b 2c 20 66 69 72 73 74 20 72 65 6c 65 61  ock, first relea
13f48 73 65 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 74  se all locks wit
13f49 68 20 61 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20  h a larger.  ** 
13f4a 42 74 53 68 61 72 65 64 20 61 64 64 72 65 73 73  BtShared address
13f4b 2e 20 20 54 68 65 6e 20 61 63 71 75 69 72 65 20  .  Then acquire 
13f4c 6f 75 72 20 6c 6f 63 6b 2e 20 20 54 68 65 6e 20  our lock.  Then 
13f4d 72 65 61 63 71 75 69 72 65 0a 20 20 2a 2a 20 74  reacquire.  ** t
13f4e 68 65 20 6f 74 68 65 72 20 42 74 53 68 61 72 65  he other BtShare
13f4f 64 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 20  d locks that we 
13f50 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e 20  used to hold in 
13f51 61 73 63 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f  ascending.  ** o
13f52 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  rder..  */.  for
13f53 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74  (pLater=p->pNext
13f54 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72  ; pLater; pLater
13f55 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b  =pLater->pNext){
13f56 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 61  .    assert( pLa
13f57 74 65 72 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b  ter->sharable );
13f58 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 61  .    assert( pLa
13f59 74 65 72 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c  ter->pNext==0 ||
13f5a 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 2d 3e   pLater->pNext->
13f5b 70 42 74 3e 70 4c 61 74 65 72 2d 3e 70 42 74 20  pBt>pLater->pBt 
13f5c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
13f5d 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c  pLater->locked |
13f5e 7c 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 54 6f  | pLater->wantTo
13f5f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 69 66  Lock>0 );.    if
13f60 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64  ( pLater->locked
13f61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
13f62 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4c  3_mutex_leave(pL
13f63 61 74 65 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  ater->pBt->mutex
13f64 29 3b 0a 20 20 20 20 20 20 70 4c 61 74 65 72 2d  );.      pLater-
13f65 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20  >locked = 0;.   
13f66 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
13f67 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e  _mutex_enter(p->
13f68 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70  pBt->mutex);.  p
13f69 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20  ->locked = 1;.  
13f6a 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e  for(pLater=p->pN
13f6b 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61  ext; pLater; pLa
13f6c 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78  ter=pLater->pNex
13f6d 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74  t){.    if( pLat
13f6e 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29  er->wantToLock )
13f6f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
13f70 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4c 61 74  mutex_enter(pLat
13f71 65 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  er->pBt->mutex);
13f72 0a 20 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c  .      pLater->l
13f73 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d  ocked = 1;.    }
13f74 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78  .  }.}../*.** Ex
13f75 69 74 20 74 68 65 20 72 65 63 75 72 73 69 76 65  it the recursive
13f76 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65   mutex on a Btre
13f77 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
13f78 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
13f79 33 42 74 72 65 65 4c 65 61 76 65 28 42 74 72 65  3BtreeLeave(Btre
13f7a 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  e *p){.  if( p->
13f7b 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
13f7c 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54  assert( p->wantT
13f7d 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70  oLock>0 );.    p
13f7e 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a  ->wantToLock--;.
13f7f 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54      if( p->wantT
13f80 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20  oLock==0 ){.    
13f81 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63    assert( p->loc
13f82 6b 65 64 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ked );.      sql
13f83 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
13f84 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
13f85 0a 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64  .      p->locked
13f86 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
13f87 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
13f88 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  G./*.** Return t
13f89 72 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61  rue if the BtSha
13f8a 72 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c  red mutex is hel
13f8b 64 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2e 20  d on the btree. 
13f8c 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75   .**.** This rou
13f8d 74 69 6e 65 20 6d 61 6b 65 73 20 6e 6f 20 64 65  tine makes no de
13f8e 74 65 72 6d 69 6e 61 74 69 6f 6e 20 6f 6e 65 20  termination one 
13f8f 77 68 79 20 6f 72 20 61 6e 6f 74 68 65 72 20 69  why or another i
13f90 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  f the.** databas
13f91 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74  e connection mut
13f92 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2a 0a 2a  ex is held..**.*
13f93 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
13f94 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72 6f 6d  s used only from
13f95 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29   within assert()
13f96 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
13f97 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
13f98 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  nt sqlite3BtreeH
13f99 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 20  oldsMutex(Btree 
13f9a 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70  *p){.  return (p
13f9b 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c  ->sharable==0 ||
13f9c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 70  .             (p
13f9d 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 77  ->locked && p->w
13f9e 61 6e 74 54 6f 4c 6f 63 6b 20 26 26 20 73 71 6c  antToLock && sql
13f9f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
13fa0 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 29  p->pBt->mutex)))
13fa1 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66  ;.}.#endif...#if
13fa2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
13fa3 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20  _INCRBLOB./*.** 
13fa4 45 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20  Enter and leave 
13fa5 61 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72  a mutex on a Btr
13fa6 65 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f  ee given a curso
13fa7 72 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a  r owned by that.
13fa8 2a 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65  ** Btree.  These
13fa9 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72   entry points ar
13faa 65 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d  e used by increm
13fab 65 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61  ental I/O and ca
13fac 6e 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20  n be.** omitted 
13fad 69 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69  if that module i
13fae 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53  s not used..*/.S
13faf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
13fb0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
13fb1 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72  nterCursor(BtCur
13fb2 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71  sor *pCur){.  sq
13fb3 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
13fb4 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d  pCur->pBtree);.}
13fb5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13fb6 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
13fb7 65 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43  eLeaveCursor(BtC
13fb8 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
13fb9 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
13fba 65 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b  e(pCur->pBtree);
13fbb 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
13fbc 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
13fbd 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74  B */.../*.** Ent
13fbe 65 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20  er the mutex on 
13fbf 65 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f  every Btree asso
13fc0 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61  ciated with a da
13fc1 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
13fc2 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e  tion.  This is n
13fc3 65 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70  eeded (for examp
13fc4 6c 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72  le) prior to par
13fc5 73 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d  sing.** a statem
13fc6 65 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c  ent since we wil
13fc7 6c 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74  l be comparing t
13fc8 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20  able and column 
13fc9 6e 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74  names.** against
13fca 20 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64   all schemas and
13fcb 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   we do not want 
13fcc 74 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65  those schemas be
13fcd 69 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74  ing.** reset out
13fce 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a   from under us..
13fcf 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61  **.** There is a
13fd0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c   corresponding l
13fd1 65 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75  eave-all procedu
13fd2 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72  res..**.** Enter
13fd3 20 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20   the mutexes in 
13fd4 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20  accending order 
13fd5 62 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e  by BtShared poin
13fd6 74 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74  ter address.** t
13fd7 6f 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73  o avoid the poss
13fd8 69 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c  ibility of deadl
13fd9 6f 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72  ock when two thr
13fda 65 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f  eads with.** two
13fdb 20 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20   or more btrees 
13fdc 69 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74  in common both t
13fdd 72 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74  ry to lock all t
13fde 68 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61  heir btrees.** a
13fdf 74 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61  t the same insta
13fe0 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
13fe1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
13fe2 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
13fe3 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
13fe4 69 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a  int i;.  Btree *
13fe5 70 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73  p, *pLater;.  as
13fe6 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
13fe7 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
13fe8 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  ex) );.  for(i=0
13fe9 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
13fea 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61  ){.    p = db->a
13feb 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69  Db[i].pBt;.    i
13fec 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61  f( p && p->shara
13fed 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ble ){.      p->
13fee 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20  wantToLock++;.  
13fef 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b      if( !p->lock
13ff0 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  ed ){.        as
13ff1 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c  sert( p->wantToL
13ff2 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  ock==1 );.      
13ff3 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 72 65    while( p->pPre
13ff4 76 20 29 20 70 20 3d 20 70 2d 3e 70 50 72 65 76  v ) p = p->pPrev
13ff5 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
13ff6 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d   p->locked && p-
13ff7 3e 70 4e 65 78 74 20 29 20 70 20 3d 20 70 2d 3e  >pNext ) p = p->
13ff8 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66  pNext;.        f
13ff9 6f 72 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70  or(pLater = p->p
13ffa 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c  Next; pLater; pL
13ffb 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65  ater=pLater->pNe
13ffc 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  xt){.          i
13ffd 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65  f( pLater->locke
13ffe 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  d ){.           
13fff 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
14000 65 61 76 65 28 70 4c 61 74 65 72 2d 3e 70 42 74  eave(pLater->pBt
14001 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
14002 20 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c 6f        pLater->lo
14003 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20  cked = 0;.      
14004 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
14005 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
14006 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
14007 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
14008 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  r(p->pBt->mutex)
14009 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6c  ;.          p->l
1400a 6f 63 6b 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20  ocked++;.       
1400b 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b     p = p->pNext;
1400c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1400d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53   }.    }.  }.}.S
1400e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1400f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  id sqlite3BtreeL
14010 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 20  eaveAll(sqlite3 
14011 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
14012 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 61 73 73   Btree *p;.  ass
14013 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14014 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
14015 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  x) );.  for(i=0;
14016 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
14017 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44  {.    p = db->aD
14018 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[i].pBt;.    if
14019 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62  ( p && p->sharab
1401a 6c 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  le ){.      asse
1401b 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  rt( p->wantToLoc
1401c 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e  k>0 );.      p->
1401d 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20  wantToLock--;.  
1401e 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54      if( p->wantT
1401f 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20  oLock==0 ){.    
14020 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c      assert( p->l
14021 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20 20 20 20  ocked );.       
14022 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
14023 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74  eave(p->pBt->mut
14024 65 78 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ex);.        p->
14025 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20  locked = 0;.    
14026 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
14027 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
14028 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
14029 65 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74  e if the current
1402a 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68   thread holds th
1402b 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1402c 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61  ction.** mutex a
1402d 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20  nd all required 
1402e 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73  BtShared mutexes
1402f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
14030 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73  tine is used ins
14031 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
14032 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f  tements only..*/
14033 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14034 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
14035 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
14036 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
14037 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71  int i;.  if( !sq
14038 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14039 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20  (db->mutex) ){. 
1403a 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1403b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
1403c 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
1403d 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70   Btree *p;.    p
1403e 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
1403f 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20  t;.    if( p && 
14040 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20  p->sharable &&. 
14041 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74          (p->want
14042 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71  ToLock==0 || !sq
14043 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14044 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29  (p->pBt->mutex))
14045 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
14046 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
14047 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
14048 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  if /* NDEBUG */.
14049 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
1404a 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74   Btree pointer t
1404b 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 41 72  o a BtreeMutexAr
1404c 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20  ray. .** if the 
1404d 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73  pointer can poss
1404e 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 20 77  ibly be shared w
1404f 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64  ith.** another d
14050 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14051 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  on..**.** The po
14052 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 74 20  inters are kept 
14053 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
14054 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20  by pBtree->pBt. 
14055 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65   That.** way whe
14056 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72  n we go to enter
14057 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73   all the mutexes
14058 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74  , we can enter t
14059 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  hem.** in order 
1405a 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61  without every ha
1405b 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61  ving to backup a
1405c 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 69 74  nd retry and wit
1405d 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67  hout.** worrying
1405e 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e   about deadlock.
1405f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  .**.** The numbe
14060 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 72 65  r of shared btre
14061 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62  es will always b
14062 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79  e small (usually
14063 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61   0 or 1).** so a
14064 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74  n insertion sort
14065 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 65 20   is an adequate 
14066 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a  algorithm here..
14067 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14068 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14069 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73  reeMutexArrayIns
1406a 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72  ert(BtreeMutexAr
1406b 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72  ray *pArray, Btr
1406c 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69  ee *pBtree){.  i
1406d 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61  nt i, j;.  BtSha
1406e 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20  red *pBt;.  if( 
1406f 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74  pBtree==0 || pBt
14070 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30  ree->sharable==0
14071 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64   ) return;.#ifnd
14072 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20  ef NDEBUG.  {.  
14073 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72    for(i=0; i<pAr
14074 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b  ray->nMutex; i++
14075 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
14076 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14077 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20  i]!=pBtree );.  
14078 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
14079 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
1407a 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20  >nMutex>=0 );.  
1407b 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e  assert( pArray->
1407c 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65  nMutex<ArraySize
1407d 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29  (pArray->aBtree)
1407e 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42  -1 );.  pBt = pB
1407f 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72  tree->pBt;.  for
14080 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e  (i=0; i<pArray->
14081 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20  nMutex; i++){.  
14082 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79    assert( pArray
14083 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74  ->aBtree[i]!=pBt
14084 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ree );.    if( p
14085 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d  Array->aBtree[i]
14086 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20  ->pBt>pBt ){.   
14087 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d     for(j=pArray-
14088 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d  >nMutex; j>i; j-
14089 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72  -){.        pArr
1408a 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20  ay->aBtree[j] = 
1408b 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a  pArray->aBtree[j
1408c 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  -1];.      }.   
1408d 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65     pArray->aBtre
1408e 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20  e[i] = pBtree;. 
1408f 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75       pArray->nMu
14090 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  tex++;.      ret
14091 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
14092 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14093 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b  pArray->nMutex++
14094 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f  ] = pBtree;.}../
14095 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d  *.** Enter the m
14096 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74  utex of every bt
14097 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79  ree in the array
14098 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
14099 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20  is.** called at 
1409a 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
1409b 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
1409c 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73  ().  The mutexes
1409d 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61   are.** exited a
1409e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
1409f 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a   same function..
140a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
140a1 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
140a2 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  reeMutexArrayEnt
140a3 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72  er(BtreeMutexArr
140a4 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69  ay *pArray){.  i
140a5 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
140a6 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65   i<pArray->nMute
140a7 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  x; i++){.    Btr
140a8 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e  ee *p = pArray->
140a9 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f  aBtree[i];.    /
140aa 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e  * Some basic san
140ab 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a  ity checking */.
140ac 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30      assert( i==0
140ad 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72   || pArray->aBtr
140ae 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e  ee[i-1]->pBt<p->
140af 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
140b0 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c  t( !p->locked ||
140b1 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30   p->wantToLock>0
140b2 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73   );..    /* We s
140b3 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f  hould already ho
140b4 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ld a lock on the
140b5 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
140b6 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65  tion */.    asse
140b7 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
140b8 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
140b9 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e  tex) );..    p->
140ba 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20  wantToLock++;.  
140bb 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64    if( !p->locked
140bc 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20   && p->sharable 
140bd 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
140be 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e  _mutex_enter(p->
140bf 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  pBt->mutex);.   
140c0 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31     p->locked = 1
140c1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
140c2 2a 0a 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d  *.** Leave the m
140c3 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74  utex of every bt
140c4 72 65 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70  ree in the group
140c5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
140c6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
140c7 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c  BtreeMutexArrayL
140c8 65 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41  eave(BtreeMutexA
140c9 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20  rray *pArray){. 
140ca 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
140cb 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75  0; i<pArray->nMu
140cc 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42  tex; i++){.    B
140cd 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79  tree *p = pArray
140ce 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20  ->aBtree[i];.   
140cf 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73   /* Some basic s
140d0 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a  anity checking *
140d1 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d  /.    assert( i=
140d2 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42  =0 || pArray->aB
140d3 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70  tree[i-1]->pBt<p
140d4 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73  ->pBt );.    ass
140d5 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c  ert( p->locked |
140d6 7c 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  | !p->sharable )
140d7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
140d8 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b  >wantToLock>0 );
140d9 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75  ..    /* We shou
140da 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20  ld already hold 
140db 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  a lock on the da
140dc 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
140dd 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  n */.    assert(
140de 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
140df 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
140e0 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e  ) );..    p->wan
140e1 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69  tToLock--;.    i
140e2 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  f( p->wantToLock
140e3 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64  ==0 && p->locked
140e4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
140e5 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
140e6 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  >pBt->mutex);.  
140e7 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20      p->locked = 
140e8 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
140e9 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49  .#endif  /* SQLI
140ea 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
140eb 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48   !SQLITE_OMIT_SH
140ec 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f  ARED_CACHE */../
140ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
140ee 6e 64 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20  nd of btmutex.c 
140ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
140f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
140f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
140f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
140f3 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e  egin file btree.
140f4 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
140f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
140f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
140f7 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20  *.** 2004 April 
140f8 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
140f9 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
140fa 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
140fb 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
140fc 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
140fd 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
140fe 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
140ff 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
14100 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
14101 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
14102 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
14103 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
14104 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
14105 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
14106 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
14107 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
14108 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
14109 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1410a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1410b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1410c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1410d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1410e 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 2e 63  .** $Id: btree.c
1410f 2c 76 20 31 2e 35 35 38 20 32 30 30 39 2f 30 31  ,v 1.558 2009/01
14110 2f 31 30 20 31 36 3a 31 35 3a 32 31 20 64 72 68  /10 16:15:21 drh
14111 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69   Exp $.**.** Thi
14112 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
14113 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69  s a external (di
14114 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61  sk-based) databa
14115 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e  se using BTrees.
14116 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 61 64  .** See the head
14117 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62  er comment on "b
14118 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61  treeInt.h" for a
14119 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1411a 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64  ation..** Includ
1411b 69 6e 67 20 61 20 64 65 73 63 72 69 70 74 69 6f  ing a descriptio
1411c 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74  n of file format
1411d 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77   and an overview
1411e 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a   of operation..*
1411f 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61  /../*.** The hea
14120 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20  der string that 
14121 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62  appears at the b
14122 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72  eginning of ever
14123 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61  y.** SQLite data
14124 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
14125 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69  const char zMagi
14126 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49  cHeader[] = SQLI
14127 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a  TE_FILE_HEADER;.
14128 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20  ./*.** Set this 
14129 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1412a 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74  to 1 to enable t
1412b 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65  racing using the
1412c 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e   TRACE.** macro.
1412d 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71  .*/.#if 0.int sq
1412e 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65 3d  lite3BtreeTrace=
1412f 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65  0;  /* True to e
14130 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f  nable tracing */
14131 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28  .# define TRACE(
14132 58 29 20 20 69 66 28 73 71 6c 69 74 65 33 42 74  X)  if(sqlite3Bt
14133 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66  reeTrace){printf
14134 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74   X;fflush(stdout
14135 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  );}.#else.# defi
14136 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64  ne TRACE(X).#end
14137 69 66 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  if....#ifndef SQ
14138 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
14139 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c  _CACHE./*.** A l
1413a 69 73 74 20 6f 66 20 42 74 53 68 61 72 65 64 20  ist of BtShared 
1413b 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65  objects that are
1413c 20 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61   eligible for pa
1413d 72 74 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69  rticipation.** i
1413e 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 2e 20  n shared cache. 
1413f 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 68   This variable h
14140 61 73 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75  as file scope du
14141 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c  ring normal buil
14142 64 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74  ds,.** but the t
14143 65 73 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64  est harness need
14144 73 20 74 6f 20 61 63 63 65 73 73 20 69 74 20 73  s to access it s
14145 6f 20 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f  o we make it glo
14146 62 61 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74  bal for .** test
14147 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 64   builds..*/.#ifd
14148 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
14149 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 74  QLITE_PRIVATE Bt
1414a 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57  Shared *SQLITE_W
1414b 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 64  SD sqlite3Shared
1414c 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23  CacheList = 0;.#
1414d 65 6c 73 65 0a 73 74 61 74 69 63 20 42 74 53 68  else.static BtSh
1414e 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44  ared *SQLITE_WSD
1414f 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61   sqlite3SharedCa
14150 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6e  cheList = 0;.#en
14151 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  dif.#endif /* SQ
14152 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
14153 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64  _CACHE */..#ifnd
14154 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
14155 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a  HARED_CACHE./*.*
14156 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61  * Enable or disa
14157 62 6c 65 20 74 68 65 20 73 68 61 72 65 64 20 70  ble the shared p
14158 61 67 65 72 20 61 6e 64 20 73 63 68 65 6d 61 20  ager and schema 
14159 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20  features..**.** 
1415a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73  This routine has
1415b 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 65 78   no effect on ex
1415c 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20  isting database 
1415d 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20  connections..** 
1415e 54 68 65 20 73 68 61 72 65 64 20 63 61 63 68 65  The shared cache
1415f 20 73 65 74 74 69 6e 67 20 65 66 66 65 63 74 73   setting effects
14160 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 63 61 6c   only future cal
14161 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ls to.** sqlite3
14162 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 74 65 33  _open(), sqlite3
14163 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 20 73 71  _open16(), or sq
14164 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e  lite3_open_v2().
14165 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
14166 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  nt sqlite3_enabl
14167 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 69  e_shared_cache(i
14168 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71  nt enable){.  sq
14169 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
1416a 67 2e 73 68 61 72 65 64 43 61 63 68 65 45 6e 61  g.sharedCacheEna
1416b 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20  bled = enable;. 
1416c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1416d 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  K;.}.#endif.../*
1416e 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  .** Forward decl
1416f 61 72 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69  aration.*/.stati
14170 63 20 69 6e 74 20 63 68 65 63 6b 52 65 61 64 4c  c int checkReadL
14171 6f 63 6b 73 28 42 74 72 65 65 2a 2c 20 50 67 6e  ocks(Btree*, Pgn
14172 6f 2c 20 42 74 43 75 72 73 6f 72 2a 2c 20 69 36  o, BtCursor*, i6
14173 34 29 3b 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c  4);...#ifdef SQL
14174 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
14175 43 41 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20  CACHE.  /*.  ** 
14176 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 75  The functions qu
14177 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20  eryTableLock(), 
14178 6c 6f 63 6b 54 61 62 6c 65 28 29 20 61 6e 64 20  lockTable() and 
14179 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28  unlockAllTables(
1417a 29 0a 20 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74  ).  ** manipulat
1417b 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  e entries in the
1417c 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20   BtShared.pLock 
1417d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 75 73 65 64  linked list used
1417e 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73   to store.  ** s
1417f 68 61 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c  hared-cache tabl
14180 65 20 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49  e level locks. I
14181 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73  f the library is
14182 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
14183 68 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63  he.  ** shared-c
14184 61 63 68 65 20 66 65 61 74 75 72 65 20 64 69 73  ache feature dis
14185 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72  abled, then ther
14186 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f  e is only ever o
14187 6e 65 20 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20  ne user.  ** of 
14188 65 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74  each BtShared st
14189 72 75 63 74 75 72 65 20 61 6e 64 20 73 6f 20 74  ructure and so t
1418a 68 69 73 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e  his locking is n
1418b 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 20  ot necessary. . 
1418c 20 2a 2a 20 53 6f 20 64 65 66 69 6e 65 20 74 68   ** So define th
1418d 65 20 6c 6f 63 6b 20 72 65 6c 61 74 65 64 20 66  e lock related f
1418e 75 6e 63 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f  unctions as no-o
1418f 70 73 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69  ps..  */.  #defi
14190 6e 65 20 71 75 65 72 79 54 61 62 6c 65 4c 6f 63  ne queryTableLoc
14191 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f  k(a,b,c) SQLITE_
14192 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 6c 6f 63  OK.  #define loc
14193 6b 54 61 62 6c 65 28 61 2c 62 2c 63 29 20 53 51  kTable(a,b,c) SQ
14194 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e  LITE_OK.  #defin
14195 65 20 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65  e unlockAllTable
14196 73 28 61 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66  s(a).#endif..#if
14197 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14198 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a  _SHARED_CACHE./*
14199 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65  .** Query to see
1419a 20 69 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65   if btree handle
1419b 20 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20   p may obtain a 
1419c 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f  lock of type eLo
1419d 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43  ck .** (READ_LOC
1419e 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29  K or WRITE_LOCK)
1419f 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69   on the table wi
141a0 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  th root-page iTa
141a1 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  b. Return.** SQL
141a2 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f  ITE_OK if the lo
141a3 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e  ck may be obtain
141a4 65 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 20 6c  ed (by calling l
141a5 6f 63 6b 54 61 62 6c 65 28 29 29 2c 20 6f 72 0a  ockTable()), or.
141a6 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  ** SQLITE_LOCKED
141a7 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74   if not..*/.stat
141a8 69 63 20 69 6e 74 20 71 75 65 72 79 54 61 62 6c  ic int queryTabl
141a9 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20  eLock(Btree *p, 
141aa 50 67 6e 6f 20 69 54 61 62 2c 20 75 38 20 65 4c  Pgno iTab, u8 eL
141ab 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64  ock){.  BtShared
141ac 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
141ad 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b    BtLock *pIter;
141ae 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
141af 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
141b0 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72  ex(p) );.  asser
141b1 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c  t( eLock==READ_L
141b2 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52  OCK || eLock==WR
141b3 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  ITE_LOCK );.  as
141b4 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29  sert( p->db!=0 )
141b5 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 69  ;.  .  /* This i
141b6 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65  s a no-op if the
141b7 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73   shared-cache is
141b8 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a   not enabled */.
141b9 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62    if( !p->sharab
141ba 6c 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  le ){.    return
141bb 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
141bc 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74  .  /* If some ot
141bd 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  her connection i
141be 73 20 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63  s holding an exc
141bf 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65  lusive lock, the
141c0 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 20  .  ** requested 
141c1 6c 6f 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20  lock may not be 
141c2 6f 62 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20  obtained..  */. 
141c3 20 69 66 28 20 70 42 74 2d 3e 70 45 78 63 6c 75   if( pBt->pExclu
141c4 73 69 76 65 20 26 26 20 70 42 74 2d 3e 70 45 78  sive && pBt->pEx
141c5 63 6c 75 73 69 76 65 21 3d 70 20 29 7b 0a 20 20  clusive!=p ){.  
141c6 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
141c7 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 0a 20 20 2f  LOCKED;.  }..  /
141c8 2a 20 54 68 69 73 20 28 61 6c 6f 6e 67 20 77 69  * This (along wi
141c9 74 68 20 6c 6f 63 6b 54 61 62 6c 65 28 29 29 20  th lockTable()) 
141ca 69 73 20 77 68 65 72 65 20 74 68 65 20 52 65 61  is where the Rea
141cb 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61  dUncommitted fla
141cc 67 20 69 73 0a 20 20 2a 2a 20 64 65 61 6c 74 20  g is.  ** dealt 
141cd 77 69 74 68 2e 20 49 66 20 74 68 65 20 63 61 6c  with. If the cal
141ce 6c 65 72 20 69 73 20 71 75 65 72 79 69 6e 67 20  ler is querying 
141cf 66 6f 72 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  for a read-lock 
141d0 61 6e 64 20 74 68 65 20 66 6c 61 67 20 69 73 0a  and the flag is.
141d1 20 20 2a 2a 20 73 65 74 2c 20 69 74 20 69 73 20    ** set, it is 
141d2 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20  unconditionally 
141d3 67 72 61 6e 74 65 64 20 2d 20 65 76 65 6e 20 69  granted - even i
141d4 66 20 74 68 65 72 65 20 61 72 65 20 77 72 69 74  f there are writ
141d5 65 2d 6c 6f 63 6b 73 0a 20 20 2a 2a 20 6f 6e 20  e-locks.  ** on 
141d6 74 68 65 20 74 61 62 6c 65 2e 20 49 66 20 61 20  the table. If a 
141d7 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20 72 65  write-lock is re
141d8 71 75 65 73 74 65 64 2c 20 74 68 65 20 52 65 61  quested, the Rea
141d9 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61  dUncommitted fla
141da 67 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 63 6f  g.  ** is not co
141db 6e 73 69 64 65 72 65 64 2e 0a 20 20 2a 2a 0a 20  nsidered..  **. 
141dc 20 2a 2a 20 49 6e 20 66 75 6e 63 74 69 6f 6e 20   ** In function 
141dd 6c 6f 63 6b 54 61 62 6c 65 28 29 2c 20 69 66 20  lockTable(), if 
141de 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 64  a read-lock is d
141df 65 6d 61 6e 64 65 64 20 61 6e 64 20 74 68 65 20  emanded and the 
141e0 0a 20 20 2a 2a 20 52 65 61 64 55 6e 63 6f 6d 6d  .  ** ReadUncomm
141e1 69 74 74 65 64 20 66 6c 61 67 20 69 73 20 73 65  itted flag is se
141e2 74 2c 20 6e 6f 20 65 6e 74 72 79 20 69 73 20 61  t, no entry is a
141e3 64 64 65 64 20 74 6f 20 74 68 65 20 6c 6f 63 6b  dded to the lock
141e4 73 20 6c 69 73 74 20 0a 20 20 2a 2a 20 28 42 74  s list .  ** (Bt
141e5 53 68 61 72 65 64 2e 70 4c 6f 63 6b 29 2e 0a 20  Shared.pLock).. 
141e6 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 73 75 6d 6d   **.  ** To summ
141e7 61 72 69 7a 65 3a 20 49 66 20 74 68 65 20 52 65  arize: If the Re
141e8 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c  adUncommitted fl
141e9 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  ag is set, then 
141ea 72 65 61 64 20 63 75 72 73 6f 72 73 20 64 6f 0a  read cursors do.
141eb 20 20 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20    ** not create 
141ec 6f 72 20 72 65 73 70 65 63 74 20 74 61 62 6c 65  or respect table
141ed 20 6c 6f 63 6b 73 2e 20 54 68 65 20 6c 6f 63 6b   locks. The lock
141ee 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 66 6f  ing procedure fo
141ef 72 20 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 2d  r a .  ** write-
141f0 63 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20  cursor does not 
141f1 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69  change..  */.  i
141f2 66 28 20 0a 20 20 20 20 30 3d 3d 28 70 2d 3e 64  f( .    0==(p->d
141f3 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
141f4 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29  ReadUncommitted)
141f5 20 7c 7c 20 0a 20 20 20 20 65 4c 6f 63 6b 3d 3d   || .    eLock==
141f6 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c 0a 20 20  WRITE_LOCK ||.  
141f7 20 20 69 54 61 62 3d 3d 4d 41 53 54 45 52 5f 52    iTab==MASTER_R
141f8 4f 4f 54 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72  OOT.  ){.    for
141f9 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63  (pIter=pBt->pLoc
141fa 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d  k; pIter; pIter=
141fb 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  pIter->pNext){. 
141fc 20 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e       if( pIter->
141fd 70 42 74 72 65 65 21 3d 70 20 26 26 20 70 49 74  pBtree!=p && pIt
141fe 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62  er->iTable==iTab
141ff 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 28   && .          (
14200 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c  pIter->eLock!=eL
14201 6f 63 6b 20 7c 7c 20 65 4c 6f 63 6b 21 3d 52 45  ock || eLock!=RE
14202 41 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20  AD_LOCK) ){.    
14203 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
14204 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20  E_LOCKED;.      
14205 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
14206 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
14207 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
14208 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
14209 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  CACHE */..#ifnde
1420a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
1420b 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
1420c 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74   Add a lock on t
1420d 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  he table with ro
1420e 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74  ot-page iTable t
1420f 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  o the shared-btr
14210 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74  ee used.** by Bt
14211 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61  ree handle p. Pa
14212 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75  rameter eLock mu
14213 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41  st be either REA
14214 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52  D_LOCK or .** WR
14215 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  ITE_LOCK..**.** 
14216 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
14217 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63  urned if the loc
14218 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65  k is added succe
14219 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f  ssfully. SQLITE_
1421a 42 55 53 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49  BUSY and.** SQLI
1421b 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73  TE_NOMEM may als
1421c 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a  o be returned..*
1421d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63  /.static int loc
1421e 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c  kTable(Btree *p,
1421f 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38   Pgno iTable, u8
14220 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61   eLock){.  BtSha
14221 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
14222 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f  t;.  BtLock *pLo
14223 63 6b 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b  ck = 0;.  BtLock
14224 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65   *pIter;..  asse
14225 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
14226 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b  HoldsMutex(p) );
14227 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b  .  assert( eLock
14228 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65  ==READ_LOCK || e
14229 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
1422a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1422b 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  >db!=0 );..  /* 
1422c 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20  This is a no-op 
1422d 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 61  if the shared-ca
1422e 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c  che is not enabl
1422f 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e  ed */.  if( !p->
14230 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
14231 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
14232 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
14233 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72   SQLITE_OK==quer
14234 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54  yTableLock(p, iT
14235 61 62 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a  able, eLock) );.
14236 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 61  .  /* If the rea
14237 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c  d-uncommitted fl
14238 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 61 20  ag is set and a 
14239 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 72 65 71  read-lock is req
1423a 75 65 73 74 65 64 2c 0a 20 20 2a 2a 20 72 65 74  uested,.  ** ret
1423b 75 72 6e 20 65 61 72 6c 79 20 77 69 74 68 6f 75  urn early withou
1423c 74 20 61 64 64 69 6e 67 20 61 6e 20 65 6e 74 72  t adding an entr
1423d 79 20 74 6f 20 74 68 65 20 42 74 53 68 61 72 65  y to the BtShare
1423e 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 2e 20 53 65  d.pLock list. Se
1423f 65 0a 20 20 2a 2a 20 63 6f 6d 6d 65 6e 74 20 69  e.  ** comment i
14240 6e 20 66 75 6e 63 74 69 6f 6e 20 71 75 65 72 79  n function query
14241 54 61 62 6c 65 4c 6f 63 6b 28 29 20 66 6f 72 20  TableLock() for 
14242 6d 6f 72 65 20 69 6e 66 6f 20 6f 6e 20 68 61 6e  more info on han
14243 64 6c 69 6e 67 20 0a 20 20 2a 2a 20 74 68 65 20  dling .  ** the 
14244 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
14245 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  flag..  */.  if(
14246 20 0a 20 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c   .    (p->db->fl
14247 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55  ags&SQLITE_ReadU
14248 6e 63 6f 6d 6d 69 74 74 65 64 29 20 26 26 20 0a  ncommitted) && .
14249 20 20 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44      (eLock==READ
1424a 5f 4c 4f 43 4b 29 20 26 26 0a 20 20 20 20 69 54  _LOCK) &&.    iT
1424b 61 62 6c 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f  able!=MASTER_ROO
1424c 54 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  T.  ){.    retur
1424d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1424e 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61  ..  /* First sea
1424f 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72  rch the list for
14250 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63   an existing loc
14251 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e  k on this table.
14252 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d   */.  for(pIter=
14253 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65  pBt->pLock; pIte
14254 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e  r; pIter=pIter->
14255 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
14256 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69  pIter->iTable==i
14257 54 61 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e  Table && pIter->
14258 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20  pBtree==p ){.   
14259 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72     pLock = pIter
1425a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1425b 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
1425c 66 20 74 68 65 20 61 62 6f 76 65 20 73 65 61 72  f the above sear
1425d 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20  ch did not find 
1425e 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20  a BtLock struct 
1425f 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65  associating Btre
14260 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61  e p.  ** with ta
14261 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f  ble iTable, allo
14262 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e  cate one and lin
14263 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69  k it into the li
14264 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  st..  */.  if( !
14265 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f  pLock ){.    pLo
14266 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73  ck = (BtLock *)s
14267 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
14268 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29  (sizeof(BtLock))
14269 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b  ;.    if( !pLock
1426a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1426b 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1426c 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e     }.    pLock->
1426d 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b  iTable = iTable;
1426e 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72  .    pLock->pBtr
1426f 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63  ee = p;.    pLoc
14270 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e  k->pNext = pBt->
14271 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e  pLock;.    pBt->
14272 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20  pLock = pLock;. 
14273 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
14274 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61   BtLock.eLock va
14275 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61  riable to the ma
14276 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72  ximum of the cur
14277 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61  rent lock.  ** a
14278 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 65 64  nd the requested
14279 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e   lock. This mean
1427a 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63  s if a write-loc
1427b 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20 68 65  k was already he
1427c 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65  ld.  ** and a re
1427d 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65  ad-lock requeste
1427e 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f  d, we don't inco
1427f 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64  rrectly downgrad
14280 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f  e the lock..  */
14281 0a 20 20 61 73 73 65 72 74 28 20 57 52 49 54 45  .  assert( WRITE
14282 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20  _LOCK>READ_LOCK 
14283 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70  );.  if( eLock>p
14284 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20  Lock->eLock ){. 
14285 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20     pLock->eLock 
14286 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20  = eLock;.  }..  
14287 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
14288 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
14289 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1428a 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e  D_CACHE */..#ifn
1428b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1428c 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
1428d 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74  ** Release all t
1428e 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28  he table locks (
1428f 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76  locks obtained v
14290 69 61 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20  ia calls to the 
14291 6c 6f 63 6b 54 61 62 6c 65 28 29 0a 2a 2a 20 70  lockTable().** p
14292 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20 62  rocedure) held b
14293 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70  y Btree handle p
14294 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
14295 20 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73   unlockAllTables
14296 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74  (Btree *p){.  Bt
14297 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
14298 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a  >pBt;.  BtLock *
14299 2a 70 70 49 74 65 72 20 3d 20 26 70 42 74 2d 3e  *ppIter = &pBt->
1429a 70 4c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74  pLock;..  assert
1429b 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
1429c 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20  ldsMutex(p) );. 
1429d 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72   assert( p->shar
1429e 61 62 6c 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74  able || 0==*ppIt
1429f 65 72 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  er );..  while( 
142a0 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 42  *ppIter ){.    B
142a1 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a  tLock *pLock = *
142a2 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 65  ppIter;.    asse
142a3 72 74 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 73  rt( pBt->pExclus
142a4 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70  ive==0 || pBt->p
142a5 45 78 63 6c 75 73 69 76 65 3d 3d 70 4c 6f 63 6b  Exclusive==pLock
142a6 2d 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20  ->pBtree );.    
142a7 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65  if( pLock->pBtre
142a8 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a 70  e==p ){.      *p
142a9 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70  pIter = pLock->p
142aa 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69  Next;.      sqli
142ab 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b  te3_free(pLock);
142ac 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
142ad 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63    ppIter = &pLoc
142ae 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  k->pNext;.    }.
142af 20 20 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e    }..  if( pBt->
142b0 70 45 78 63 6c 75 73 69 76 65 3d 3d 70 20 29 7b  pExclusive==p ){
142b1 0a 20 20 20 20 70 42 74 2d 3e 70 45 78 63 6c 75  .    pBt->pExclu
142b2 73 69 76 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  sive = 0;.  }.}.
142b3 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
142b4 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
142b5 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f  HE */..static vo
142b6 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d  id releasePage(M
142b7 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20  emPage *pPage); 
142b8 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65   /* Forward refe
142b9 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  rence */../*.** 
142ba 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
142bb 63 75 72 73 6f 72 20 68 6f 6c 64 73 20 61 20 6d  cursor holds a m
142bc 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68  utex on the BtSh
142bd 61 72 65 64 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ared.*/.#ifndef 
142be 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e  NDEBUG.static in
142bf 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  t cursorHoldsMut
142c0 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b  ex(BtCursor *p){
142c1 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
142c2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
142c3 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23  pBt->mutex);.}.#
142c4 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20  endif...#ifndef 
142c5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
142c6 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c  BLOB./*.** Inval
142c7 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c  idate the overfl
142c8 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63  ow page-list cac
142c9 68 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 43  he for cursor pC
142ca 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73  ur, if any..*/.s
142cb 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c  tatic void inval
142cc 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63  idateOverflowCac
142cd 68 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  he(BtCursor *pCu
142ce 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75  r){.  assert( cu
142cf 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
142d0 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  Cur) );.  sqlite
142d1 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f 76  3_free(pCur->aOv
142d2 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d  erflow);.  pCur-
142d3 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a  >aOverflow = 0;.
142d4 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64  }../*.** Invalid
142d5 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  ate the overflow
142d6 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65   page-list cache
142d7 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72 73   for all cursors
142d8 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68   opened.** on th
142d9 65 20 73 68 61 72 65 64 20 62 74 72 65 65 20 73  e shared btree s
142da 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a 2f  tructure pBt..*/
142db 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76  .static void inv
142dc 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c  alidateAllOverfl
142dd 6f 77 43 61 63 68 65 28 42 74 53 68 61 72 65 64  owCache(BtShared
142de 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73   *pBt){.  BtCurs
142df 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  or *p;.  assert(
142e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
142e1 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
142e2 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e  );.  for(p=pBt->
142e3 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d  pCursor; p; p=p-
142e4 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 76  >pNext){.    inv
142e5 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43  alidateOverflowC
142e6 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 23  ache(p);.  }.}.#
142e7 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 69  else.  #define i
142e8 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f  nvalidateOverflo
142e9 77 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66  wCache(x).  #def
142ea 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c  ine invalidateAl
142eb 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78  lOverflowCache(x
142ec 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
142ed 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74  Save the current
142ee 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e   cursor position
142ef 20 69 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65   in the variable
142f0 73 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20  s BtCursor.nKey 
142f1 0a 2a 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72  .** and BtCursor
142f2 2e 70 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f  .pKey. The curso
142f3 72 27 73 20 73 74 61 74 65 20 69 73 20 73 65 74  r's state is set
142f4 20 74 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49   to CURSOR_REQUI
142f5 52 45 53 45 45 4b 2e 0a 2a 2f 0a 73 74 61 74 69  RESEEK..*/.stati
142f6 63 20 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72  c int saveCursor
142f7 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f  Position(BtCurso
142f8 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20  r *pCur){.  int 
142f9 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43  rc;..  assert( C
142fa 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75  URSOR_VALID==pCu
142fb 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61  r->eState );.  a
142fc 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e  ssert( 0==pCur->
142fd 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74  pKey );.  assert
142fe 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
142ff 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72  ex(pCur) );..  r
14300 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
14301 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70  KeySize(pCur, &p
14302 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 0a 20 20 2f  Cur->nKey);..  /
14303 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
14304 69 6e 74 4b 65 79 20 74 61 62 6c 65 2c 20 74 68  intKey table, th
14305 65 6e 20 74 68 65 20 61 62 6f 76 65 20 63 61 6c  en the above cal
14306 6c 20 74 6f 20 42 74 72 65 65 4b 65 79 53 69 7a  l to BtreeKeySiz
14307 65 28 29 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20  e().  ** stores 
14308 74 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20  the integer key 
14309 69 6e 20 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49  in pCur->nKey. I
1430a 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 69 73  n this case this
1430b 20 76 61 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61   value is.  ** a
1430c 6c 6c 20 74 68 61 74 20 69 73 20 72 65 71 75 69  ll that is requi
1430d 72 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  red. Otherwise, 
1430e 69 66 20 70 43 75 72 20 69 73 20 6e 6f 74 20 6f  if pCur is not o
1430f 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79  pen on an intKey
14310 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65  .  ** table, the
14311 6e 20 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 66  n malloc space f
14312 6f 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  or and store the
14313 20 70 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65   pCur->nKey byte
14314 73 20 6f 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64  s of key .  ** d
14315 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ata..  */.  if( 
14316 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
14317 20 30 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65   0==pCur->apPage
14318 5b 30 5d 2d 3e 69 6e 74 4b 65 79 29 7b 0a 20 20  [0]->intKey){.  
14319 20 20 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73    void *pKey = s
1431a 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 69  qlite3Malloc( (i
1431b 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 20 29 3b  nt)pCur->nKey );
1431c 0a 20 20 20 20 69 66 28 20 70 4b 65 79 20 29 7b  .    if( pKey ){
1431d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1431e 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72  te3BtreeKey(pCur
1431f 2c 20 30 2c 20 28 69 6e 74 29 70 43 75 72 2d 3e  , 0, (int)pCur->
14320 6e 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20 20  nKey, pKey);.   
14321 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
14322 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
14323 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b 65  pCur->pKey = pKe
14324 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  y;.      }else{.
14325 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
14326 66 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 20 20  free(pKey);.    
14327 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
14328 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
14329 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
1432a 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 43 75  }.  assert( !pCu
1432b 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e  r->apPage[0]->in
1432c 74 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d 3e 70  tKey || !pCur->p
1432d 4b 65 79 20 29 3b 0a 0a 20 20 69 66 28 20 72 63  Key );..  if( rc
1432e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1432f 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
14330 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e  r(i=0; i<=pCur->
14331 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  iPage; i++){.   
14332 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
14333 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b  Cur->apPage[i]);
14334 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 70 50  .      pCur->apP
14335 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  age[i] = 0;.    
14336 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67  }.    pCur->iPag
14337 65 20 3d 20 2d 31 3b 0a 20 20 20 20 70 43 75 72  e = -1;.    pCur
14338 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
14339 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a 20  R_REQUIRESEEK;. 
1433a 20 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65   }..  invalidate
1433b 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43  OverflowCache(pC
1433c 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ur);.  return rc
1433d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20  ;.}../*.** Save 
1433e 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66  the positions of
1433f 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 63   all cursors exc
14340 65 70 74 20 70 45 78 63 65 70 74 20 6f 70 65 6e  ept pExcept open
14341 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a   on the table .*
14342 2a 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65  * with root-page
14343 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79 2c   iRoot. Usually,
14344 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   this is called 
14345 6a 75 73 74 20 62 65 66 6f 72 65 20 63 75 72 73  just before curs
14346 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 20 69 73  or.** pExcept is
14347 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 20   used to modify 
14348 74 68 65 20 74 61 62 6c 65 20 28 42 74 72 65 65  the table (Btree
14349 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65  Delete() or Btre
1434a 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a 73  eInsert())..*/.s
1434b 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 41 6c  tatic int saveAl
1434c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 65  lCursors(BtShare
1434d 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 6f  d *pBt, Pgno iRo
1434e 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 45  ot, BtCursor *pE
1434f 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72 73  xcept){.  BtCurs
14350 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  or *p;.  assert(
14351 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14352 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
14353 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
14354 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 65  cept==0 || pExce
14355 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a  pt->pBt==pBt );.
14356 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75    for(p=pBt->pCu
14357 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  rsor; p; p=p->pN
14358 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 21  ext){.    if( p!
14359 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d 3d  =pExcept && (0==
1435a 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f  iRoot || p->pgno
1435b 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26 20  Root==iRoot) && 
1435c 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61  .        p->eSta
1435d 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
1435e 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63   ){.      int rc
1435f 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73   = saveCursorPos
14360 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20  ition(p);.      
14361 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72  if( SQLITE_OK!=r
14362 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  c ){.        ret
14363 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
14364 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
14365 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
14366 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
14367 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20   current cursor 
14368 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  position..*/.SQL
14369 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1436a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
1436b 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f  arCursor(BtCurso
1436c 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65  r *pCur){.  asse
1436d 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
1436e 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
1436f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75  sqlite3_free(pCu
14370 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75 72  r->pKey);.  pCur
14371 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70 43  ->pKey = 0;.  pC
14372 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52  ur->eState = CUR
14373 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a 0a  SOR_INVALID;.}..
14374 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68  /*.** Restore th
14375 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
14376 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20  position it was 
14377 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20  in (or as close 
14378 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a  to as possible).
14379 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73  ** when saveCurs
1437a 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73  orPosition() was
1437b 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68   called. Note th
1437c 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c  at this call del
1437d 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76  etes the .** sav
1437e 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f  ed position info
1437f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43   stored by saveC
14380 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c  ursorPosition(),
14381 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65   so there can be
14382 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20  .** at most one 
14383 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72  effective restor
14384 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
14385 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63  ) call after eac
14386 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72  h .** saveCursor
14387 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 53  Position()..*/.S
14388 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14389 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65  t sqlite3BtreeRe
1438a 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
1438b 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ion(BtCursor *pC
1438c 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ur){.  int rc;. 
1438d 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
1438e 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
1438f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
14390 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f  r->eState>=CURSO
14391 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b  R_REQUIRESEEK );
14392 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
14393 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c  ate==CURSOR_FAUL
14394 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  T ){.    return 
14395 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a  pCur->skip;.  }.
14396 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
14397 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b   CURSOR_INVALID;
14398 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
14399 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c  treeMoveto(pCur,
1439a 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75   pCur->pKey, pCu
1439b 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75  r->nKey, 0, &pCu
1439c 72 2d 3e 73 6b 69 70 29 3b 0a 20 20 69 66 28 20  r->skip);.  if( 
1439d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1439e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
1439f 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20  e(pCur->pKey);. 
143a0 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20     pCur->pKey = 
143a1 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  0;.    assert( p
143a2 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
143a3 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75  SOR_VALID || pCu
143a4 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
143a5 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d  R_INVALID );.  }
143a6 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
143a7 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f 72 65  .#define restore
143a8 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
143a9 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 65  ) \.  (p->eState
143aa 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  >=CURSOR_REQUIRE
143ab 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 20  SEEK ? \.       
143ac 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65    sqlite3BtreeRe
143ad 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
143ae 69 6f 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 20  ion(p) : \.     
143af 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a      SQLITE_OK)..
143b0 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20  /*.** Determine 
143b1 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  whether or not a
143b2 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65   cursor has move
143b3 64 20 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 74  d from the posit
143b4 69 6f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c 61  ion it.** was la
143b5 73 74 20 70 6c 61 63 65 64 20 61 74 2e 20 20 43  st placed at.  C
143b6 75 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20  ursors can move 
143b7 77 68 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65  when the row the
143b8 79 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a 2a  y are pointing.*
143b9 2a 20 61 74 20 69 73 20 64 65 6c 65 74 65 64 20  * at is deleted 
143ba 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74  out from under t
143bb 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  hem..**.** This 
143bc 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
143bd 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  an error code if
143be 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20   something goes 
143bf 77 72 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 69  wrong.  The.** i
143c0 6e 74 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 65  nteger *pHasMove
143c1 64 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20  d is set to one 
143c2 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 68 61  if the cursor ha
143c3 73 20 6d 6f 76 65 64 20 61 6e 64 20 30 20 69 66  s moved and 0 if
143c4 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   not..*/.SQLITE_
143c5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
143c6 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61  te3BtreeCursorHa
143c7 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 20  sMoved(BtCursor 
143c8 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 73  *pCur, int *pHas
143c9 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63  Moved){.  int rc
143ca 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72  ;..  rc = restor
143cb 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
143cc 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20  pCur);.  if( rc 
143cd 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65  ){.    *pHasMove
143ce 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  d = 1;.    retur
143cf 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20  n rc;.  }.  if( 
143d0 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55  pCur->eState!=CU
143d1 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43  RSOR_VALID || pC
143d2 75 72 2d 3e 73 6b 69 70 21 3d 30 20 29 7b 0a 20  ur->skip!=0 ){. 
143d3 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20     *pHasMoved = 
143d4 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
143d5 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a  *pHasMoved = 0;.
143d6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
143d7 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
143d8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
143d9 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
143da 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d  Given a page num
143db 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72  ber of a regular
143dc 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20   database page, 
143dd 72 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a  return the page.
143de 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ** number for th
143df 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  e pointer-map pa
143e0 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ge that contains
143e1 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74   the entry for t
143e2 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65  he.** input page
143e3 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
143e4 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61  ic Pgno ptrmapPa
143e5 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70  geno(BtShared *p
143e6 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  Bt, Pgno pgno){.
143e7 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d    int nPagesPerM
143e8 61 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69  apPage;.  Pgno i
143e9 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61  PtrMap, ret;.  a
143ea 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
143eb 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
143ec 75 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65  utex) );.  nPage
143ed 73 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70  sPerMapPage = (p
143ee 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35  Bt->usableSize/5
143ef 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d  )+1;.  iPtrMap =
143f0 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73   (pgno-2)/nPages
143f1 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65  PerMapPage;.  re
143f2 74 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61  t = (iPtrMap*nPa
143f3 67 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b  gesPerMapPage) +
143f4 20 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d   2; .  if( ret==
143f5 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
143f6 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65  E(pBt) ){.    re
143f7 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  t++;.  }.  retur
143f8 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n ret;.}../*.** 
143f9 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69  Write an entry i
143fa 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20  nto the pointer 
143fb 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  map..**.** This 
143fc 72 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20  routine updates 
143fd 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  the pointer map 
143fe 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e  entry for page n
143ff 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73  umber 'key'.** s
14400 6f 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74  o that it maps t
14401 6f 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61  o type 'eType' a
14402 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e  nd parent page n
14403 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a  umber 'pgno'..**
14404 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   An error code i
14405 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f  s returned if so
14406 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
14407 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51  ng, otherwise SQ
14408 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74  LITE_OK..*/.stat
14409 69 63 20 69 6e 74 20 70 74 72 6d 61 70 50 75 74  ic int ptrmapPut
1440a 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
1440b 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54 79  Pgno key, u8 eTy
1440c 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74 29  pe, Pgno parent)
1440d 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50  {.  DbPage *pDbP
1440e 61 67 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69  age;  /* The poi
1440f 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f  nter map page */
14410 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20  .  u8 *pPtrmap; 
14411 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e       /* The poin
14412 74 65 72 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a  ter map data */.
14413 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20    Pgno iPtrmap; 
14414 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74      /* The point
14415 65 72 20 6d 61 70 20 70 61 67 65 20 6e 75 6d 62  er map page numb
14416 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73  er */.  int offs
14417 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66  et;       /* Off
14418 73 65 74 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d  set in pointer m
14419 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ap page */.  int
1441a 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
1441b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1441c 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
1441d 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 65  ;.  /* The maste
1441e 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e  r-journal page n
1441f 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65 72  umber must never
14420 20 62 65 20 75 73 65 64 20 61 73 20 61 20 70 6f   be used as a po
14421 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a  inter map page *
14422 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 50  /.  assert( 0==P
14423 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
14424 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  , PENDING_BYTE_P
14425 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 20  AGE(pBt)) );..  
14426 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74  assert( pBt->aut
14427 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 28  oVacuum );.  if(
14428 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 72   key==0 ){.    r
14429 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
1442a 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
1442b 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41   iPtrmap = PTRMA
1442c 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65  P_PAGENO(pBt, ke
1442d 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  y);.  rc = sqlit
1442e 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e  e3PagerGet(pBt->
1442f 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c  pPager, iPtrmap,
14430 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66   &pDbPage);.  if
14431 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
14432 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
14433 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d  ;.  }.  offset =
14434 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45   PTRMAP_PTROFFSE
14435 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b  T(iPtrmap, key);
14436 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38  .  pPtrmap = (u8
14437 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
14438 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b  etData(pDbPage);
14439 0a 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 70  ..  if( eType!=p
1443a 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 7c  Ptrmap[offset] |
1443b 7c 20 67 65 74 34 62 79 74 65 28 26 70 50 74 72  | get4byte(&pPtr
1443c 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 21 3d  map[offset+1])!=
1443d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 54 52  parent ){.    TR
1443e 41 43 45 28 28 22 50 54 52 4d 41 50 5f 55 50 44  ACE(("PTRMAP_UPD
1443f 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25 64 29  ATE: %d->(%d,%d)
14440 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70 65 2c  \n", key, eType,
14441 20 70 61 72 65 6e 74 29 29 3b 0a 20 20 20 20 72   parent));.    r
14442 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
14443 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a  Write(pDbPage);.
14444 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
14445 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
14446 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 3d  Ptrmap[offset] =
14447 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 70 75   eType;.      pu
14448 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b  t4byte(&pPtrmap[
14449 6f 66 66 73 65 74 2b 31 5d 2c 20 70 61 72 65 6e  offset+1], paren
1444a 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
1444b 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
1444c 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 72  ef(pDbPage);.  r
1444d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1444e 2a 2a 20 52 65 61 64 20 61 6e 20 65 6e 74 72 79  ** Read an entry
1444f 20 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 65   from the pointe
14450 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  r map..**.** Thi
14451 73 20 72 6f 75 74 69 6e 65 20 72 65 74 72 69 65  s routine retrie
14452 76 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20  ves the pointer 
14453 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61  map entry for pa
14454 67 65 20 27 6b 65 79 27 2c 20 77 72 69 74 69 6e  ge 'key', writin
14455 67 0a 2a 2a 20 74 68 65 20 74 79 70 65 20 61 6e  g.** the type an
14456 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75  d parent page nu
14457 6d 62 65 72 20 74 6f 20 2a 70 45 54 79 70 65 20  mber to *pEType 
14458 61 6e 64 20 2a 70 50 67 6e 6f 20 72 65 73 70 65  and *pPgno respe
14459 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e 20 65  ctively..** An e
1445a 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
1445b 75 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69  urned if somethi
1445c 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f  ng goes wrong, o
1445d 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f  therwise SQLITE_
1445e 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  OK..*/.static in
1445f 74 20 70 74 72 6d 61 70 47 65 74 28 42 74 53 68  t ptrmapGet(BtSh
14460 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20  ared *pBt, Pgno 
14461 6b 65 79 2c 20 75 38 20 2a 70 45 54 79 70 65 2c  key, u8 *pEType,
14462 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b 0a 20   Pgno *pPgno){. 
14463 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
14464 3b 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74  ;   /* The point
14465 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20  er map page */. 
14466 20 69 6e 74 20 69 50 74 72 6d 61 70 3b 20 20 20   int iPtrmap;   
14467 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d      /* Pointer m
14468 61 70 20 70 61 67 65 20 69 6e 64 65 78 20 2a 2f  ap page index */
14469 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20  .  u8 *pPtrmap; 
1446a 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
1446b 20 6d 61 70 20 70 61 67 65 20 64 61 74 61 20 2a   map page data *
1446c 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20  /.  int offset; 
1446d 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
1446e 20 6f 66 20 65 6e 74 72 79 20 69 6e 20 70 6f 69   of entry in poi
1446f 6e 74 65 72 20 6d 61 70 20 2a 2f 0a 20 20 69 6e  nter map */.  in
14470 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
14471 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14472 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
14473 29 3b 0a 0a 20 20 69 50 74 72 6d 61 70 20 3d 20  );..  iPtrmap = 
14474 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42  PTRMAP_PAGENO(pB
14475 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20  t, key);.  rc = 
14476 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
14477 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74  pBt->pPager, iPt
14478 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b  rmap, &pDbPage);
14479 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a  .  if( rc!=0 ){.
1447a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1447b 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28   }.  pPtrmap = (
1447c 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  u8 *)sqlite3Page
1447d 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65  rGetData(pDbPage
1447e 29 3b 0a 0a 20 20 6f 66 66 73 65 74 20 3d 20 50  );..  offset = P
1447f 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28  TRMAP_PTROFFSET(
14480 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20  iPtrmap, key);. 
14481 20 61 73 73 65 72 74 28 20 70 45 54 79 70 65 21   assert( pEType!
14482 3d 30 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20  =0 );.  *pEType 
14483 3d 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  = pPtrmap[offset
14484 5d 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29  ];.  if( pPgno )
14485 20 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79   *pPgno = get4by
14486 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73  te(&pPtrmap[offs
14487 65 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74  et+1]);..  sqlit
14488 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62  e3PagerUnref(pDb
14489 50 61 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45  Page);.  if( *pE
1448a 54 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70  Type<1 || *pETyp
1448b 65 3e 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c  e>5 ) return SQL
1448c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1448d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1448e 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f  E_OK;.}..#else /
1448f 2a 20 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c  * if defined SQL
14490 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
14491 55 55 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65  UUM */.  #define
14492 20 70 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79   ptrmapPut(w,x,y
14493 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20  ,z) SQLITE_OK.  
14494 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 47 65  #define ptrmapGe
14495 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54  t(w,x,y,z) SQLIT
14496 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70  E_OK.  #define p
14497 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 79 2c 7a  trmapPutOvfl(y,z
14498 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64  ) SQLITE_OK.#end
14499 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  if../*.** Given 
1449a 61 20 62 74 72 65 65 20 70 61 67 65 20 61 6e 64  a btree page and
1449b 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20 28 30   a cell index (0
1449c 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72 73 74   means the first
1449d 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20   cell on.** the 
1449e 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20 74 68  page, 1 means th
1449f 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c 20 61  e second cell, a
144a0 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72 65 74  nd so forth) ret
144a1 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a  urn a pointer.**
144a2 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   to the cell con
144a3 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tent..**.** This
144a4 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f   routine works o
144a5 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 74 68  nly for pages th
144a6 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69  at do not contai
144a7 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73  n overflow cells
144a8 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e  ..*/.#define fin
144a9 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28  dCell(P,I) \.  (
144aa 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 28 50  (P)->aData + ((P
144ab 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65  )->maskPage & ge
144ac 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61 44 61  t2byte(&(P)->aDa
144ad 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 66 73  ta[(P)->cellOffs
144ae 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a  et+2*(I)])))../*
144af 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20  .** This a more 
144b0 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20  complex version 
144b1 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68  of findCell() th
144b2 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20  at works for.** 
144b3 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f  pages that do co
144b4 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63  ntain overflow c
144b5 65 6c 6c 73 2e 20 20 53 65 65 20 69 6e 73 65 72  ells.  See inser
144b6 74 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 2a  t.*/.static u8 *
144b7 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c  findOverflowCell
144b8 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
144b9 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 69   int iCell){.  i
144ba 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
144bb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
144bc 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
144bd 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69  utex) );.  for(i
144be 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f  =pPage->nOverflo
144bf 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b  w-1; i>=0; i--){
144c0 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20  .    int k;.    
144c1 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c  struct _OvflCell
144c2 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 70 4f 76   *pOvfl;.    pOv
144c3 66 6c 20 3d 20 26 70 50 61 67 65 2d 3e 61 4f 76  fl = &pPage->aOv
144c4 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d 20 70  fl[i];.    k = p
144c5 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 20 20 20 69  Ovfl->idx;.    i
144c6 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b 0a 20  f( k<=iCell ){. 
144c7 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43 65 6c       if( k==iCel
144c8 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  l ){.        ret
144c9 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65 6c 6c  urn pOvfl->pCell
144ca 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
144cb 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d 0a 20  iCell--;.    }. 
144cc 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69 6e 64   }.  return find
144cd 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c  Cell(pPage, iCel
144ce 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72  l);.}../*.** Par
144cf 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74 65 6e  se a cell conten
144d0 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69 6c 6c  t block and fill
144d1 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e 66 6f   in the CellInfo
144d2 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
144d3 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20 76 65  re.** are two ve
144d4 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 66  rsions of this f
144d5 75 6e 63 74 69 6f 6e 2e 20 20 73 71 6c 69 74 65  unction.  sqlite
144d6 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28  3BtreeParseCell(
144d7 29 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65  ) takes a .** ce
144d8 6c 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20  ll index as the 
144d9 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
144da 61 6e 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  and sqlite3Btree
144db 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 0a  ParseCellPtr() .
144dc 2a 2a 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74  ** takes a point
144dd 65 72 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f  er to the body o
144de 66 20 74 68 65 20 63 65 6c 6c 20 61 73 20 69 74  f the cell as it
144df 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  s second argumen
144e0 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20  t..**.** Within 
144e1 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 20 70  this file, the p
144e2 61 72 73 65 43 65 6c 6c 28 29 20 6d 61 63 72 6f  arseCell() macro
144e3 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 69   can be called i
144e4 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 73 71 6c  nstead of.** sql
144e5 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
144e6 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20 73  llPtr(). Using s
144e7 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 74  ome compilers, t
144e8 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73 74  his will be fast
144e9 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
144ea 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
144eb 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
144ec 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a  Ptr(.  MemPage *
144ed 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f  pPage,         /
144ee 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
144ef 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20  g the cell */.  
144f0 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20  u8 *pCell,      
144f1 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
144f2 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74  er to the cell t
144f3 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e  ext. */.  CellIn
144f4 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20  fo *pInfo       
144f5 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69    /* Fill in thi
144f6 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29  s structure */.)
144f7 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20 20 20  {.  u16 n;      
144f8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
144f9 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e 20 63  umber bytes in c
144fa 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64  ell content head
144fb 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79  er */.  u32 nPay
144fc 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 20 20  load;           
144fd 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
144fe 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f  es of cell paylo
144ff 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ad */..  assert(
14500 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14501 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
14502 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 49 6e  mutex) );..  pIn
14503 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 65 6c  fo->pCell = pCel
14504 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  l;.  assert( pPa
14505 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 70  ge->leaf==0 || p
14506 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b  Page->leaf==1 );
14507 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e 63 68  .  n = pPage->ch
14508 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 61 73  ildPtrSize;.  as
14509 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 50 61  sert( n==4-4*pPa
1450a 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 69 66  ge->leaf );.  if
1450b 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
1450c 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  ){.    if( pPage
1450d 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20  ->hasData ){.   
1450e 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e     n += getVarin
1450f 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e  t32(&pCell[n], n
14510 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 7d 65  Payload);.    }e
14511 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 79 6c  lse{.      nPayl
14512 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  oad = 0;.    }. 
14513 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e     n += getVarin
14514 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36  t(&pCell[n], (u6
14515 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29  4*)&pInfo->nKey)
14516 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61  ;.    pInfo->nDa
14517 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20  ta = nPayload;. 
14518 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66   }else{.    pInf
14519 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20  o->nData = 0;.  
1451a 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74    n += getVarint
1451b 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50  32(&pCell[n], nP
1451c 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 49 6e  ayload);.    pIn
1451d 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 79 6c  fo->nKey = nPayl
1451e 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e 66 6f  oad;.  }.  pInfo
1451f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61  ->nPayload = nPa
14520 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e  yload;.  pInfo->
14521 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20 20 69  nHeader = n;.  i
14522 66 28 20 6c 69 6b 65 6c 79 28 6e 50 61 79 6c 6f  f( likely(nPaylo
14523 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  ad<=pPage->maxLo
14524 63 61 6c 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54  cal) ){.    /* T
14525 68 69 73 20 69 73 20 74 68 65 20 28 65 61 73 79  his is the (easy
14526 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68  ) common case wh
14527 65 72 65 20 74 68 65 20 65 6e 74 69 72 65 20 70  ere the entire p
14528 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 20 20 20  ayload fits.    
14529 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ** on the local 
1452a 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72 66 6c  page.  No overfl
1452b 6f 77 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a  ow is required..
1452c 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e      */.    int n
1452d 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Size;          /
1452e 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20  * Total size of 
1452f 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20  cell content in 
14530 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 6e 53 69  bytes */.    nSi
14531 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64 20 2b 20  ze = nPayload + 
14532 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c  n;.    pInfo->nL
14533 6f 63 61 6c 20 3d 20 28 75 31 36 29 6e 50 61 79  ocal = (u16)nPay
14534 6c 6f 61 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d  load;.    pInfo-
14535 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a  >iOverflow = 0;.
14536 20 20 20 20 69 66 28 20 28 6e 53 69 7a 65 20 26      if( (nSize &
14537 20 7e 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20   ~3)==0 ){.     
14538 20 6e 53 69 7a 65 20 3d 20 34 3b 20 20 20 20 20   nSize = 4;     
14539 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65     /* Minimum ce
1453a 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a  ll size is 4 */.
1453b 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d      }.    pInfo-
1453c 3e 6e 53 69 7a 65 20 3d 20 28 75 31 36 29 6e 53  >nSize = (u16)nS
1453d 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ize;.  }else{.  
1453e 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 79 6c    /* If the payl
1453f 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74  oad will not fit
14540 20 63 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74   completely on t
14541 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77  he local page, w
14542 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f  e have.    ** to
14543 20 64 65 63 69 64 65 20 68 6f 77 20 6d 75 63 68   decide how much
14544 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c   to store locall
14545 79 20 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 74  y and how much t
14546 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 20  o spill onto.   
14547 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   ** overflow pag
14548 65 73 2e 20 20 54 68 65 20 73 74 72 61 74 65 67  es.  The strateg
14549 79 20 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a 65  y is to minimize
1454a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 75   the amount of u
1454b 6e 75 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 61  nused.    ** spa
1454c 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70  ce on overflow p
1454d 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65 70 69  ages while keepi
1454e 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  ng the amount of
1454f 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a 20   local storage. 
14550 20 20 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e     ** in between
14551 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61   minLocal and ma
14552 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a 20  xLocal..    **. 
14553 20 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20     ** Warning:  
14554 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79  changing the way
14555 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61   overflow payloa
14556 64 20 69 73 20 64 69 73 74 72 69 62 75 74 65 64  d is distributed
14557 20 69 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 77   in any.    ** w
14558 61 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69  ay will result i
14559 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  n an incompatibl
1455a 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20  e file format.. 
1455b 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 69     */.    int mi
1455c 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e 69  nLocal;  /* Mini
1455d 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61  mum amount of pa
1455e 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c  yload held local
1455f 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61  ly */.    int ma
14560 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 69  xLocal;  /* Maxi
14561 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61  mum amount of pa
14562 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c  yload held local
14563 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 75  ly */.    int su
14564 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 72  rplus;   /* Over
14565 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 61  flow payload ava
14566 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 6c  ilable for local
14567 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 20   storage */..   
14568 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67   minLocal = pPag
14569 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20  e->minLocal;.   
1456a 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67   maxLocal = pPag
1456b 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20  e->maxLocal;.   
1456c 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f   surplus = minLo
1456d 63 61 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20  cal + (nPayload 
1456e 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 61  - minLocal)%(pPa
1456f 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
14570 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 69 66  ize - 4);.    if
14571 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78  ( surplus <= max
14572 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70  Local ){.      p
14573 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28  Info->nLocal = (
14574 75 31 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20  u16)surplus;.   
14575 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49   }else{.      pI
14576 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75  nfo->nLocal = (u
14577 31 36 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20  16)minLocal;.   
14578 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f   }.    pInfo->iO
14579 76 65 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28  verflow = (u16)(
1457a 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20  pInfo->nLocal + 
1457b 6e 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  n);.    pInfo->n
1457c 53 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f  Size = pInfo->iO
1457d 76 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d  verflow + 4;.  }
1457e 0a 7d 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65  .}.#define parse
1457f 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c  Cell(pPage, iCel
14580 6c 2c 20 70 49 6e 66 6f 29 20 5c 0a 20 20 73 71  l, pInfo) \.  sq
14581 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
14582 65 6c 6c 50 74 72 28 28 70 50 61 67 65 29 2c 20  ellPtr((pPage), 
14583 66 69 6e 64 43 65 6c 6c 28 28 70 50 61 67 65 29  findCell((pPage)
14584 2c 20 28 69 43 65 6c 6c 29 29 2c 20 28 70 49 6e  , (iCell)), (pIn
14585 66 6f 29 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  fo)).SQLITE_PRIV
14586 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14587 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 0a  BtreeParseCell(.
14588 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
14589 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67  ,         /* Pag
1458a 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
1458b 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69   cell */.  int i
1458c 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Cell,           
1458d 20 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69     /* The cell i
1458e 6e 64 65 78 2e 20 20 46 69 72 73 74 20 63 65 6c  ndex.  First cel
1458f 6c 20 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c 6c  l is 0 */.  Cell
14590 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20  Info *pInfo     
14591 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74      /* Fill in t
14592 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
14593 0a 29 7b 0a 20 20 70 61 72 73 65 43 65 6c 6c 28  .){.  parseCell(
14594 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49  pPage, iCell, pI
14595 6e 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  nfo);.}../*.** C
14596 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61 6c  ompute the total
14597 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
14598 20 74 68 61 74 20 61 20 43 65 6c 6c 20 6e 65 65   that a Cell nee
14599 64 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a  ds in the cell.*
1459a 2a 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 74  * data area of t
1459b 68 65 20 62 74 72 65 65 2d 70 61 67 65 2e 20 20  he btree-page.  
1459c 54 68 65 20 72 65 74 75 72 6e 20 6e 75 6d 62 65  The return numbe
1459d 72 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 63  r includes the c
1459e 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 68 65 61 64  ell.** data head
1459f 65 72 20 61 6e 64 20 74 68 65 20 6c 6f 63 61 6c  er and the local
145a0 20 70 61 79 6c 6f 61 64 2c 20 62 75 74 20 6e 6f   payload, but no
145a1 74 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70  t any overflow p
145a2 61 67 65 20 6f 72 0a 2a 2a 20 74 68 65 20 73 70  age or.** the sp
145a3 61 63 65 20 75 73 65 64 20 62 79 20 74 68 65 20  ace used by the 
145a4 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  cell pointer..*/
145a5 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
145a6 73 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53  static u16 cellS
145a7 69 7a 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ize(MemPage *pPa
145a8 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a  ge, int iCell){.
145a9 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b    CellInfo info;
145aa 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  .  sqlite3BtreeP
145ab 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20  arseCell(pPage, 
145ac 69 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20  iCell, &info);. 
145ad 20 72 65 74 75 72 6e 20 69 6e 66 6f 2e 6e 53 69   return info.nSi
145ae 7a 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 73 74 61  ze;.}.#endif.sta
145af 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65  tic u16 cellSize
145b0 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  Ptr(MemPage *pPa
145b1 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a  ge, u8 *pCell){.
145b2 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b    CellInfo info;
145b3 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  .  sqlite3BtreeP
145b4 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67  arseCellPtr(pPag
145b5 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29  e, pCell, &info)
145b6 3b 0a 20 20 72 65 74 75 72 6e 20 69 6e 66 6f 2e  ;.  return info.
145b7 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65  nSize;.}..#ifnde
145b8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
145b9 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49  TOVACUUM./*.** I
145ba 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c  f the cell pCell
145bb 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65 20 70  , part of page p
145bc 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 61 20  Page contains a 
145bd 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e  pointer.** to an
145be 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20   overflow page, 
145bf 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 79 20  insert an entry 
145c0 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72  into the pointer
145c1 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20  -map.** for the 
145c2 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a  overflow page..*
145c3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72  /.static int ptr
145c4 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65  mapPutOvflPtr(Me
145c5 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38  mPage *pPage, u8
145c6 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c   *pCell){.  Cell
145c7 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 61 73 73  Info info;.  ass
145c8 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b  ert( pCell!=0 );
145c9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  .  sqlite3BtreeP
145ca 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67  arseCellPtr(pPag
145cb 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29  e, pCell, &info)
145cc 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 66  ;.  assert( (inf
145cd 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e  o.nData+(pPage->
145ce 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b  intKey?0:info.nK
145cf 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c  ey))==info.nPayl
145d0 6f 61 64 20 29 3b 0a 20 20 69 66 28 20 28 69 6e  oad );.  if( (in
145d1 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d  fo.nData+(pPage-
145d2 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e  >intKey?0:info.n
145d3 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  Key))>info.nLoca
145d4 6c 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76  l ){.    Pgno ov
145d5 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  fl = get4byte(&p
145d6 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66  Cell[info.iOverf
145d7 6c 6f 77 5d 29 3b 0a 20 20 20 20 72 65 74 75 72  low]);.    retur
145d8 6e 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 67  n ptrmapPut(pPag
145d9 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54  e->pBt, ovfl, PT
145da 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20  RMAP_OVERFLOW1, 
145db 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20  pPage->pgno);.  
145dc 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
145dd 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 49 66  E_OK;.}./*.** If
145de 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69   the cell with i
145df 6e 64 65 78 20 69 43 65 6c 6c 20 6f 6e 20 70 61  ndex iCell on pa
145e0 67 65 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e  ge pPage contain
145e1 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  s a pointer.** t
145e2 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61  o an overflow pa
145e3 67 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e  ge, insert an en
145e4 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69  try into the poi
145e5 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20  nter-map.** for 
145e6 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
145e7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
145e8 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 4d   ptrmapPutOvfl(M
145e9 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
145ea 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38 20  nt iCell){.  u8 
145eb 2a 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74  *pCell;.  assert
145ec 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
145ed 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
145ee 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 65  >mutex) );.  pCe
145ef 6c 6c 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f  ll = findOverflo
145f0 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65  wCell(pPage, iCe
145f1 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 74  ll);.  return pt
145f2 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70  rmapPutOvflPtr(p
145f3 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 7d 0a  Page, pCell);.}.
145f4 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44  #endif.../*.** D
145f5 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61  efragment the pa
145f6 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43  ge given.  All C
145f7 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74  ells are moved t
145f8 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20  o the.** end of 
145f9 74 68 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c  the page and all
145fa 20 66 72 65 65 20 73 70 61 63 65 20 69 73 20 63   free space is c
145fb 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e  ollected into on
145fc 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b  e.** big FreeBlk
145fd 20 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20   that occurs in 
145fe 62 65 74 77 65 65 6e 20 74 68 65 20 68 65 61 64  between the head
145ff 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70  er and cell.** p
14600 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64  ointer array and
14601 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
14602 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69  t area..*/.stati
14603 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65 6e 74  c int defragment
14604 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
14605 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  age){.  int i;  
14606 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14607 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
14608 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20  er */.  int pc; 
14609 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1460a 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
1460b 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a   a i-th cell */.
1460c 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20    int addr;     
1460d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1460e 4f 66 66 73 65 74 20 6f 66 20 66 69 72 73 74 20  Offset of first 
1460f 62 79 74 65 20 61 66 74 65 72 20 63 65 6c 6c 20  byte after cell 
14610 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f  pointer array */
14611 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20  .  int hdr;     
14612 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14613 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 70   Offset to the p
14614 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  age header */.  
14615 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20  int size;       
14616 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
14617 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a  ze of a cell */.
14618 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65    int usableSize
14619 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1461a 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65  Number of usable
1461b 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65   bytes on a page
1461c 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66   */.  int cellOf
1461d 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
1461e 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68   /* Offset to th
1461f 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
14620 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62  rray */.  int cb
14621 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rk;             
14622 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
14623 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  o the cell conte
14624 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74  nt area */.  int
14625 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20   nCell;         
14626 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
14627 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68  r of cells on th
14628 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69  e page */.  unsi
14629 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b  gned char *data;
1462a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
1462b 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73  ge data */.  uns
1462c 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70  igned char *temp
1462d 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20  ;       /* Temp 
1462e 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f  area for cell co
1462f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65  ntent */..  asse
14630 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
14631 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
14632 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
14633 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
14634 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  pBt!=0 );.  asse
14635 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e  rt( pPage->pBt->
14636 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 51  usableSize <= SQ
14637 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
14638 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ZE );.  assert( 
14639 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
1463a 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
1463b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1463c 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
1463d 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d 70  mutex) );.  temp
1463e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 54   = sqlite3PagerT
1463f 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d 3e  empSpace(pPage->
14640 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
14641 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
14642 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61  ata;.  hdr = pPa
14643 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
14644 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50   cellOffset = pP
14645 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b  age->cellOffset;
14646 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65  .  nCell = pPage
14647 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72  ->nCell;.  asser
14648 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79  t( nCell==get2by
14649 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29  te(&data[hdr+3])
1464a 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65   );.  usableSize
1464b 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75   = pPage->pBt->u
1464c 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 72  sableSize;.  cbr
1464d 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  k = get2byte(&da
1464e 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d 65  ta[hdr+5]);.  me
1464f 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b 5d  mcpy(&temp[cbrk]
14650 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75  , &data[cbrk], u
14651 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 6b  sableSize - cbrk
14652 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 62  );.  cbrk = usab
14653 6c 65 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69 3d  leSize;.  for(i=
14654 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
14655 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b  {.    u8 *pAddr;
14656 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68       /* The i-th
14657 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f   cell pointer */
14658 0a 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61  .    pAddr = &da
14659 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20  ta[cellOffset + 
1465a 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67  i*2];.    pc = g
1465b 65 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a  et2byte(pAddr);.
1465c 20 20 20 20 69 66 28 20 70 63 3e 3d 75 73 61 62      if( pc>=usab
1465d 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  leSize ){.      
1465e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1465f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
14660 7d 0a 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c  }.    size = cel
14661 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20  lSizePtr(pPage, 
14662 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 20  &temp[pc]);.    
14663 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 20 20  cbrk -= size;.  
14664 20 20 69 66 28 20 63 62 72 6b 3c 63 65 6c 6c 4f    if( cbrk<cellO
14665 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 7c 7c  ffset+2*nCell ||
14666 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53   pc+size>usableS
14667 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ize ){.      ret
14668 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
14669 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20  PT_BKPT;.    }. 
1466a 20 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 2b     assert( cbrk+
1466b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65  size<=usableSize
1466c 20 26 26 20 63 62 72 6b 3e 3d 30 20 29 3b 0a 20   && cbrk>=0 );. 
1466d 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b     memcpy(&data[
1466e 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d  cbrk], &temp[pc]
1466f 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 74  , size);.    put
14670 32 62 79 74 65 28 70 41 64 64 72 2c 20 63 62 72  2byte(pAddr, cbr
14671 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  k);.  }.  assert
14672 28 20 63 62 72 6b 3e 3d 63 65 6c 6c 4f 66 66 73  ( cbrk>=cellOffs
14673 65 74 2b 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 20  et+2*nCell );.  
14674 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  put2byte(&data[h
14675 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20 20  dr+5], cbrk);.  
14676 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 3b  data[hdr+1] = 0;
14677 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 3d  .  data[hdr+2] =
14678 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37   0;.  data[hdr+7
14679 5d 20 3d 20 30 3b 0a 20 20 61 64 64 72 20 3d 20  ] = 0;.  addr = 
1467a 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65  cellOffset+2*nCe
1467b 6c 6c 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61  ll;.  memset(&da
1467c 74 61 5b 61 64 64 72 5d 2c 20 30 2c 20 63 62 72  ta[addr], 0, cbr
1467d 6b 2d 61 64 64 72 29 3b 0a 20 20 61 73 73 65 72  k-addr);.  asser
1467e 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
1467f 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
14680 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
14681 69 66 28 20 63 62 72 6b 2d 61 64 64 72 21 3d 70  if( cbrk-addr!=p
14682 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20  Page->nFree ){. 
14683 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
14684 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
14685 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
14686 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
14687 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62  Allocate nByte b
14688 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6f 6e  ytes of space on
14689 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 52   a page..**.** R
1468a 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
1468b 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74  into pPage->aDat
1468c 61 5b 5d 20 6f 66 20 74 68 65 20 66 69 72 73 74  a[] of the first
1468d 20 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20   byte of.** the 
1468e 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  new allocation. 
1468f 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72   The caller guar
14690 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 72  antees that ther
14691 65 20 69 73 20 65 6e 6f 75 67 68 0a 2a 2a 20 73  e is enough.** s
14692 70 61 63 65 2e 20 20 54 68 69 73 20 72 6f 75 74  pace.  This rout
14693 69 6e 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 66  ine will never f
14694 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ail..**.** If th
14695 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20  e page contains 
14696 6e 42 79 74 65 73 20 6f 66 20 66 72 65 65 20 73  nBytes of free s
14697 70 61 63 65 20 62 75 74 20 64 6f 65 73 20 6e 6f  pace but does no
14698 74 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 42 79  t contain.** nBy
14699 74 65 73 20 6f 66 20 63 6f 6e 74 69 67 75 6f 75  tes of contiguou
1469a 73 20 66 72 65 65 20 73 70 61 63 65 2c 20 74 68  s free space, th
1469b 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
1469c 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a  automatically.**
1469d 20 63 61 6c 6c 73 20 64 65 66 72 61 67 65 6d 65   calls defrageme
1469e 6e 74 50 61 67 65 28 29 20 74 6f 20 63 6f 6e 73  ntPage() to cons
1469f 6f 6c 69 64 61 74 65 20 61 6c 6c 20 66 72 65 65  olidate all free
146a0 20 73 70 61 63 65 20 62 65 66 6f 72 65 20 0a 2a   space before .*
146a1 2a 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65  * allocating the
146a2 20 6e 65 77 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 73   new chunk..*/.s
146a3 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61  tatic int alloca
146a4 74 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20  teSpace(MemPage 
146a5 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 42 79 74  *pPage, int nByt
146a6 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c 20  e){.  int addr, 
146a7 70 63 2c 20 68 64 72 3b 0a 20 20 69 6e 74 20 73  pc, hdr;.  int s
146a8 69 7a 65 3b 0a 20 20 69 6e 74 20 6e 46 72 61 67  ize;.  int nFrag
146a9 3b 0a 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 69  ;.  int top;.  i
146aa 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 69 6e 74 20  nt nCell;.  int 
146ab 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 75 6e  cellOffset;.  un
146ac 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74  signed char *dat
146ad 61 3b 0a 20 20 0a 20 20 64 61 74 61 20 3d 20 70  a;.  .  data = p
146ae 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 61  Page->aData;.  a
146af 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
146b0 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
146b1 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
146b2 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
146b3 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65  e->pBt );.  asse
146b4 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
146b5 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
146b6 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
146b7 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30 20  ssert( nByte>=0 
146b8 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63  );  /* Minimum c
146b9 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f  ell size is 4 */
146ba 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
146bb 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20 29  ->nFree>=nByte )
146bc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
146bd 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20  e->nOverflow==0 
146be 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  );.  pPage->nFre
146bf 65 20 2d 3d 20 28 75 31 36 29 6e 42 79 74 65 3b  e -= (u16)nByte;
146c0 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  .  hdr = pPage->
146c1 68 64 72 4f 66 66 73 65 74 3b 0a 0a 20 20 6e 46  hdrOffset;..  nF
146c2 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37  rag = data[hdr+7
146c3 5d 3b 0a 20 20 69 66 28 20 6e 46 72 61 67 3c 36  ];.  if( nFrag<6
146c4 30 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 61 72  0 ){.    /* Sear
146c5 63 68 20 74 68 65 20 66 72 65 65 6c 69 73 74 20  ch the freelist 
146c6 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 73 6c  looking for a sl
146c7 6f 74 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f  ot big enough to
146c8 20 73 61 74 69 73 66 79 20 74 68 65 0a 20 20 20   satisfy the.   
146c9 20 2a 2a 20 73 70 61 63 65 20 72 65 71 75 65 73   ** space reques
146ca 74 2e 20 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d  t. */.    addr =
146cb 20 68 64 72 2b 31 3b 0a 20 20 20 20 77 68 69 6c   hdr+1;.    whil
146cc 65 28 20 28 70 63 20 3d 20 67 65 74 32 62 79 74  e( (pc = get2byt
146cd 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e  e(&data[addr]))>
146ce 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a 65 20  0 ){.      size 
146cf 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
146d0 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69  [pc+2]);.      i
146d1 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29  f( size>=nByte )
146d2 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 20  {.        int x 
146d3 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0a  = size - nByte;.
146d4 20 20 20 20 20 20 20 20 69 66 28 20 73 69 7a 65          if( size
146d5 3c 6e 42 79 74 65 2b 34 20 29 7b 0a 20 20 20 20  <nByte+4 ){.    
146d6 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61        memcpy(&da
146d7 74 61 5b 61 64 64 72 5d 2c 20 26 64 61 74 61 5b  ta[addr], &data[
146d8 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20  pc], 2);.       
146d9 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d     data[hdr+7] =
146da 20 28 75 38 29 28 6e 46 72 61 67 20 2b 20 78 29   (u8)(nFrag + x)
146db 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
146dc 72 6e 20 70 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn pc;.        }
146dd 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
146de 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
146df 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20  c+2], x);.      
146e0 20 20 20 20 72 65 74 75 72 6e 20 70 63 20 2b 20      return pc + 
146e1 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  x;.        }.   
146e2 20 20 20 7d 0a 20 20 20 20 20 20 61 64 64 72 20     }.      addr 
146e3 3d 20 70 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  = pc;.    }.  }.
146e4 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d  .  /* Allocate m
146e5 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 67  emory from the g
146e6 61 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68  ap in between th
146e7 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
146e8 72 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68  rray.  ** and th
146e9 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  e cell content a
146ea 72 65 61 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20  rea..  */.  top 
146eb 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
146ec 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6e 43 65 6c  [hdr+5]);.  nCel
146ed 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  l = get2byte(&da
146ee 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 63 65  ta[hdr+3]);.  ce
146ef 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65  llOffset = pPage
146f0 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20  ->cellOffset;.  
146f1 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 20 7c 7c  if( nFrag>=60 ||
146f2 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a   cellOffset + 2*
146f3 6e 43 65 6c 6c 20 3e 20 74 6f 70 20 2d 20 6e 42  nCell > top - nB
146f4 79 74 65 20 29 7b 0a 20 20 20 20 64 65 66 72 61  yte ){.    defra
146f5 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29  gmentPage(pPage)
146f6 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32  ;.    top = get2
146f7 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
146f8 5d 29 3b 0a 20 20 7d 0a 20 20 74 6f 70 20 2d 3d  ]);.  }.  top -=
146f9 20 6e 42 79 74 65 3b 0a 20 20 61 73 73 65 72 74   nByte;.  assert
146fa 28 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32  ( cellOffset + 2
146fb 2a 6e 43 65 6c 6c 20 3c 3d 20 74 6f 70 20 29 3b  *nCell <= top );
146fc 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
146fd 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a  a[hdr+5], top);.
146fe 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
146ff 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
14700 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
14701 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 74 6f  ) );.  return to
14702 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  p;.}../*.** Retu
14703 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f 66 20  rn a section of 
14704 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61  the pPage->aData
14705 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74   to the freelist
14706 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 62  ..** The first b
14707 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77 20 66  yte of the new f
14708 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70 50 61  ree block is pPa
14709 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d  ge->aDisk[start]
1470a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69 7a 65  .** and the size
1470b 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 69 73   of the block is
1470c 20 22 73 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a   "size" bytes..*
1470d 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65  *.** Most of the
1470e 20 65 66 66 6f 72 74 20 68 65 72 65 20 69 73 20   effort here is 
1470f 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c  involved in coal
14710 65 73 69 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a  esing adjacent.*
14711 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e  * free blocks in
14712 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 69 67 20  to a single big 
14713 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73  free block..*/.s
14714 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 53 70  tatic int freeSp
14715 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ace(MemPage *pPa
14716 67 65 2c 20 69 6e 74 20 73 74 61 72 74 2c 20 69  ge, int start, i
14717 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20  nt size){.  int 
14718 61 64 64 72 2c 20 70 62 65 67 69 6e 2c 20 68 64  addr, pbegin, hd
14719 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  r;.  unsigned ch
1471a 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65  ar *data = pPage
1471b 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61 73 73 65  ->aData;..  asse
1471c 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d  rt( pPage->pBt!=
1471d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
1471e 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1471f 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
14720 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
14721 72 74 28 20 73 74 61 72 74 3e 3d 70 50 61 67 65  rt( start>=pPage
14722 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b 28 70  ->hdrOffset+6+(p
14723 50 61 67 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 20  Page->leaf?0:4) 
14724 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 74  );.  assert( (st
14725 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61  art + size)<=pPa
14726 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
14727 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ize );.  assert(
14728 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14729 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
1472a 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
1472b 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20  rt( size>=0 );  
1472c 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c   /* Minimum cell
1472d 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23   size is 4 */..#
1472e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43  ifdef SQLITE_SEC
1472f 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20  URE_DELETE.  /* 
14730 4f 76 65 72 77 72 69 74 65 20 64 65 6c 65 74 65  Overwrite delete
14731 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69  d information wi
14732 74 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 74 68  th zeros when th
14733 65 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20  e SECURE_DELETE 
14734 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20  .  ** option is 
14735 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69  enabled at compi
14736 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d  le-time */.  mem
14737 73 65 74 28 26 64 61 74 61 5b 73 74 61 72 74 5d  set(&data[start]
14738 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64  , 0, size);.#end
14739 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65  if..  /* Add the
1473a 20 73 70 61 63 65 20 62 61 63 6b 20 69 6e 74 6f   space back into
1473b 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
1473c 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 20 2a   of freeblocks *
1473d 2f 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d  /.  hdr = pPage-
1473e 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 61 64  >hdrOffset;.  ad
1473f 64 72 20 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20  dr = hdr + 1;.  
14740 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d  while( (pbegin =
14741 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
14742 61 64 64 72 5d 29 29 3c 73 74 61 72 74 20 26 26  addr]))<start &&
14743 20 70 62 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20   pbegin>0 ){.   
14744 20 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 3c   assert( pbegin<
14745 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61  =pPage->pBt->usa
14746 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20  bleSize-4 );.   
14747 20 69 66 28 20 70 62 65 67 69 6e 3c 3d 61 64 64   if( pbegin<=add
14748 72 20 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75  r ) {.      retu
14749 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1474a 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20  T_BKPT;.    }.  
1474b 20 20 61 64 64 72 20 3d 20 70 62 65 67 69 6e 3b    addr = pbegin;
1474c 0a 20 20 7d 0a 20 20 69 66 20 28 20 70 62 65 67  .  }.  if ( pbeg
1474d 69 6e 3e 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  in>pPage->pBt->u
1474e 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 20 7b 0a  sableSize-4 ) {.
1474f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
14750 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
14751 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62    }.  assert( pb
14752 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65  egin>addr || pbe
14753 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32  gin==0 );.  put2
14754 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d  byte(&data[addr]
14755 2c 20 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32  , start);.  put2
14756 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74  byte(&data[start
14757 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75  ], pbegin);.  pu
14758 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74 61  t2byte(&data[sta
14759 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20  rt+2], size);.  
1475a 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20  pPage->nFree += 
1475b 28 75 31 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a  (u16)size;..  /*
1475c 20 43 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65   Coalesce adjace
1475d 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a  nt free blocks *
1475e 2f 0a 20 20 61 64 64 72 20 3d 20 70 50 61 67 65  /.  addr = pPage
1475f 2d 3e 68 64 72 4f 66 66 73 65 74 20 2b 20 31 3b  ->hdrOffset + 1;
14760 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69  .  while( (pbegi
14761 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  n = get2byte(&da
14762 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a  ta[addr]))>0 ){.
14763 20 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70      int pnext, p
14764 73 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73 73  size, x;.    ass
14765 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72  ert( pbegin>addr
14766 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
14767 70 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70  pbegin<=pPage->p
14768 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34  Bt->usableSize-4
14769 20 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d 20   );.    pnext = 
1476a 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
1476b 62 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69  begin]);.    psi
1476c 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  ze = get2byte(&d
1476d 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a  ata[pbegin+2]);.
1476e 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 20 2b      if( pbegin +
1476f 20 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e   psize + 3 >= pn
14770 65 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20 29  ext && pnext>0 )
14771 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61 67  {.      int frag
14772 20 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65 67   = pnext - (pbeg
14773 69 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20 20  in+psize);.     
14774 20 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c 7c   if( (frag<0) ||
14775 20 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74 61   (frag>(int)data
14776 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
14777 74 2b 37 5d 29 20 29 7b 0a 20 20 20 20 20 20 20  t+7]) ){.       
14778 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
14779 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1477a 20 20 20 7d 0a 20 20 20 20 20 20 64 61 74 61 5b     }.      data[
1477b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
1477c 2b 37 5d 20 2d 3d 20 28 75 38 29 66 72 61 67 3b  +7] -= (u8)frag;
1477d 0a 20 20 20 20 20 20 78 20 3d 20 67 65 74 32 62  .      x = get2b
1477e 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74 5d  yte(&data[pnext]
1477f 29 3b 0a 20 20 20 20 20 20 70 75 74 32 62 79 74  );.      put2byt
14780 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c  e(&data[pbegin],
14781 20 78 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 70   x);.      x = p
14782 6e 65 78 74 20 2b 20 67 65 74 32 62 79 74 65 28  next + get2byte(
14783 26 64 61 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20  &data[pnext+2]) 
14784 2d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 20 20  - pbegin;.      
14785 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
14786 62 65 67 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20  begin+2], x);.  
14787 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
14788 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20  ddr = pbegin;.  
14789 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
1478a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
1478b 74 20 61 72 65 61 20 62 65 67 69 6e 73 20 77 69  t area begins wi
1478c 74 68 20 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20  th a freeblock, 
1478d 72 65 6d 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20  remove it. */.  
1478e 69 66 28 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d  if( data[hdr+1]=
1478f 3d 64 61 74 61 5b 68 64 72 2b 35 5d 20 26 26 20  =data[hdr+5] && 
14790 64 61 74 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74  data[hdr+2]==dat
14791 61 5b 68 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20  a[hdr+6] ){.    
14792 69 6e 74 20 74 6f 70 3b 0a 20 20 20 20 70 62 65  int top;.    pbe
14793 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26  gin = get2byte(&
14794 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20  data[hdr+1]);.  
14795 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 68    memcpy(&data[h
14796 64 72 2b 31 5d 2c 20 26 64 61 74 61 5b 70 62 65  dr+1], &data[pbe
14797 67 69 6e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f  gin], 2);.    to
14798 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  p = get2byte(&da
14799 74 61 5b 68 64 72 2b 35 5d 29 20 2b 20 67 65 74  ta[hdr+5]) + get
1479a 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67  2byte(&data[pbeg
1479b 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 70 75 74 32  in+2]);.    put2
1479c 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
1479d 5d 2c 20 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61  ], top);.  }.  a
1479e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
1479f 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
147a0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
147a1 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
147a2 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  E_OK;.}../*.** D
147a3 65 63 6f 64 65 20 74 68 65 20 66 6c 61 67 73 20  ecode the flags 
147a4 62 79 74 65 20 28 74 68 65 20 66 69 72 73 74 20  byte (the first 
147a5 62 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 64  byte of the head
147a6 65 72 29 20 66 6f 72 20 61 20 70 61 67 65 0a 2a  er) for a page.*
147a7 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  * and initialize
147a8 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 4d   fields of the M
147a9 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65  emPage structure
147aa 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a   accordingly..**
147ab 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c  .** Only the fol
147ac 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69  lowing combinati
147ad 6f 6e 73 20 61 72 65 20 73 75 70 70 6f 72 74 65  ons are supporte
147ae 64 2e 20 20 41 6e 79 74 68 69 6e 67 20 64 69 66  d.  Anything dif
147af 66 65 72 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61  ferent.** indica
147b0 74 65 73 20 61 20 63 6f 72 72 75 70 74 20 64 61  tes a corrupt da
147b1 74 61 62 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a  tabase files:.**
147b2 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f  .**         PTF_
147b3 5a 45 52 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20  ZERODATA.**     
147b4 20 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41      PTF_ZERODATA
147b5 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20   | PTF_LEAF.**  
147b6 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44         PTF_LEAFD
147b7 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59  ATA | PTF_INTKEY
147b8 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f  .**         PTF_
147b9 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49  LEAFDATA | PTF_I
147ba 4e 54 4b 45 59 20 7c 20 50 54 46 5f 4c 45 41 46  NTKEY | PTF_LEAF
147bb 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
147bc 65 63 6f 64 65 46 6c 61 67 73 28 4d 65 6d 50 61  ecodeFlags(MemPa
147bd 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66  ge *pPage, int f
147be 6c 61 67 42 79 74 65 29 7b 0a 20 20 42 74 53 68  lagByte){.  BtSh
147bf 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f  ared *pBt;     /
147c0 2a 20 41 20 63 6f 70 79 20 6f 66 20 70 50 61 67  * A copy of pPag
147c1 65 2d 3e 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73  e->pBt */..  ass
147c2 65 72 74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f  ert( pPage->hdrO
147c3 66 66 73 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70  ffset==(pPage->p
147c4 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30  gno==1 ? 100 : 0
147c5 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
147c6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
147c7 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
147c8 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 2d  tex) );.  pPage-
147c9 3e 6c 65 61 66 20 3d 20 28 75 38 29 28 66 6c 61  >leaf = (u8)(fla
147ca 67 42 79 74 65 3e 3e 33 29 3b 20 20 61 73 73 65  gByte>>3);  asse
147cb 72 74 28 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20  rt( PTF_LEAF == 
147cc 31 3c 3c 33 20 29 3b 0a 20 20 66 6c 61 67 42 79  1<<3 );.  flagBy
147cd 74 65 20 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b  te &= ~PTF_LEAF;
147ce 0a 20 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50  .  pPage->childP
147cf 74 72 53 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61  trSize = 4-4*pPa
147d0 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20  ge->leaf;.  pBt 
147d1 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
147d2 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 28 50  if( flagByte==(P
147d3 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54  TF_LEAFDATA | PT
147d4 46 5f 49 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20  F_INTKEY) ){.   
147d5 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d   pPage->intKey =
147d6 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68   1;.    pPage->h
147d7 61 73 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e  asData = pPage->
147d8 6c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d  leaf;.    pPage-
147d9 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d  >maxLocal = pBt-
147da 3e 6d 61 78 4c 65 61 66 3b 0a 20 20 20 20 70 50  >maxLeaf;.    pP
147db 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20  age->minLocal = 
147dc 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20  pBt->minLeaf;.  
147dd 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 42 79  }else if( flagBy
147de 74 65 3d 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41  te==PTF_ZERODATA
147df 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69   ){.    pPage->i
147e0 6e 74 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 70  ntKey = 0;.    p
147e1 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20  Page->hasData = 
147e2 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61  0;.    pPage->ma
147e3 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61  xLocal = pBt->ma
147e4 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 70 50 61 67  xLocal;.    pPag
147e5 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42  e->minLocal = pB
147e6 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d  t->minLocal;.  }
147e7 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
147e8 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
147e9 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75  BKPT;.  }.  retu
147ea 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
147eb 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
147ec 65 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  e the auxiliary 
147ed 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
147ee 61 20 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a  a disk block..**
147ef 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
147f0 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e  E_OK on success.
147f1 20 20 49 66 20 77 65 20 73 65 65 20 74 68 61 74    If we see that
147f2 20 74 68 65 20 70 61 67 65 20 64 6f 65 73 0a 2a   the page does.*
147f3 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  * not contain a 
147f4 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61  well-formed data
147f5 62 61 73 65 20 70 61 67 65 2c 20 74 68 65 6e 20  base page, then 
147f6 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54  return .** SQLIT
147f7 45 5f 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65  E_CORRUPT.  Note
147f8 20 74 68 61 74 20 61 20 72 65 74 75 72 6e 20 6f   that a return o
147f9 66 20 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73  f SQLITE_OK does
147fa 20 6e 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65   not.** guarante
147fb 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
147fc 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20  is well-formed. 
147fd 20 49 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74   It only shows t
147fe 68 61 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64  hat.** we failed
147ff 20 74 6f 20 64 65 74 65 63 74 20 61 6e 79 20 63   to detect any c
14800 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  orruption..*/.SQ
14801 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
14802 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69   sqlite3BtreeIni
14803 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  tPage(MemPage *p
14804 50 61 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 74  Page){..  assert
14805 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20  ( pPage->pBt!=0 
14806 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
14807 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14808 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
14809 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
1480a 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c  pPage->pgno==sql
1480b 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d  ite3PagerPagenum
1480c 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ber(pPage->pDbPa
1480d 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
1480e 20 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65   pPage == sqlite
1480f 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70  3PagerGetExtra(p
14810 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
14811 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
14812 65 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69  e->aData == sqli
14813 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
14814 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
14815 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65  );..  if( !pPage
14816 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20  ->isInit ){.    
14817 75 31 36 20 70 63 3b 20 20 20 20 20 20 20 20 20  u16 pc;         
14818 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
14819 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74   a freeblock wit
1481a 68 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61  hin pPage->aData
1481b 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 68 64 72  [] */.    u8 hdr
1481c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1481d 4f 66 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e  Offset to beginn
1481e 69 6e 67 20 6f 66 20 70 61 67 65 20 68 65 61 64  ing of page head
1481f 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61  er */.    u8 *da
14820 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ta;          /* 
14821 45 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e  Equal to pPage->
14822 61 44 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 53  aData */.    BtS
14823 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20  hared *pBt;     
14824 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62     /* The main b
14825 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
14826 2f 0a 20 20 20 20 75 31 36 20 75 73 61 62 6c 65  /.    u16 usable
14827 53 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75  Size;    /* Amou
14828 6e 74 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61  nt of usable spa
14829 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20  ce on each page 
1482a 2a 2f 0a 20 20 20 20 75 31 36 20 63 65 6c 6c 4f  */.    u16 cellO
1482b 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66  ffset;    /* Off
1482c 73 65 74 20 66 72 6f 6d 20 73 74 61 72 74 20 6f  set from start o
1482d 66 20 70 61 67 65 20 74 6f 20 66 69 72 73 74 20  f page to first 
1482e 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  cell pointer */.
1482f 20 20 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20      u16 nFree;  
14830 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
14831 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 73   of unused bytes
14832 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a   on the page */.
14833 20 20 20 20 75 31 36 20 74 6f 70 3b 20 20 20 20      u16 top;    
14834 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
14835 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  byte of the cell
14836 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f   content area */
14837 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50 61 67  ..    pBt = pPag
14838 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68 64 72  e->pBt;..    hdr
14839 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
1483a 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20 3d 20  set;.    data = 
1483b 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20  pPage->aData;.  
1483c 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c 61 67    if( decodeFlag
1483d 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b 68 64  s(pPage, data[hd
1483e 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c  r]) ) return SQL
1483f 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
14840 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
14841 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32  t->pageSize>=512
14842 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a   && pBt->pageSiz
14843 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 20 20  e<=32768 );.    
14844 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20  pPage->maskPage 
14845 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  = pBt->pageSize 
14846 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  - 1;.    pPage->
14847 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20  nOverflow = 0;. 
14848 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20     usableSize = 
14849 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b  pBt->usableSize;
1484a 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c  .    pPage->cell
1484b 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66  Offset = cellOff
1484c 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d  set = hdr + 12 -
1484d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a   4*pPage->leaf;.
1484e 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79      top = get2by
1484f 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
14850 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65  ;.    pPage->nCe
14851 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  ll = get2byte(&d
14852 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20  ata[hdr+3]);.   
14853 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c   if( pPage->nCel
14854 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 29  l>MX_CELL(pBt) )
14855 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61  {.      /* To ma
14856 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 20 73  ny cells for a s
14857 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54 68 65  ingle page.  The
14858 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f   page must be co
14859 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20 20 72  rrupt */.      r
1485a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
1485b 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
1485c 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75  .  .    /* Compu
1485d 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 65  te the total fre
1485e 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70  e space on the p
1485f 61 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20  age */.    pc = 
14860 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
14861 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65  dr+1]);.    nFre
14862 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 20  e = data[hdr+7] 
14863 2b 20 74 6f 70 20 2d 20 28 63 65 6c 6c 4f 66 66  + top - (cellOff
14864 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e  set + 2*pPage->n
14865 43 65 6c 6c 29 3b 0a 20 20 20 20 77 68 69 6c 65  Cell);.    while
14866 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20 20  ( pc>0 ){.      
14867 75 31 36 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a  u16 next, size;.
14868 20 20 20 20 20 20 69 66 28 20 70 63 3e 75 73 61        if( pc>usa
14869 62 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20  bleSize-4 ){.   
1486a 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f       /* Free blo
1486b 63 6b 20 69 73 20 6f 66 66 20 74 68 65 20 70 61  ck is off the pa
1486c 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65  ge */.        re
1486d 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1486e 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20  UPT_BKPT; .     
1486f 20 7d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20   }.      next = 
14870 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
14871 63 5d 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20  c]);.      size 
14872 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
14873 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69  [pc+2]);.      i
14874 66 28 20 6e 65 78 74 3e 30 20 26 26 20 6e 65 78  f( next>0 && nex
14875 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 20 29 7b 0a  t<=pc+size+3 ){.
14876 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20          /* Free 
14877 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69  blocks must be i
14878 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65  n accending orde
14879 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74  r */.        ret
1487a 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1487b 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20  PT_BKPT; .      
1487c 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d  }.      nFree +=
1487d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20   size;.      pc 
1487e 3d 20 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  = next;.    }.  
1487f 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d    pPage->nFree =
14880 20 28 75 31 36 29 6e 46 72 65 65 3b 0a 20 20 20   (u16)nFree;.   
14881 20 69 66 28 20 6e 46 72 65 65 3e 3d 75 73 61 62   if( nFree>=usab
14882 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  leSize ){.      
14883 2f 2a 20 46 72 65 65 20 73 70 61 63 65 20 63 61  /* Free space ca
14884 6e 6e 6f 74 20 65 78 63 65 65 64 20 74 6f 74 61  nnot exceed tota
14885 6c 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20  l page size */. 
14886 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
14887 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
14888 20 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20   .    }..#if 0. 
14889 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61   /* Check that a
1488a 6c 6c 20 74 68 65 20 6f 66 66 73 65 74 73 20 69  ll the offsets i
1488b 6e 20 74 68 65 20 63 65 6c 6c 20 6f 66 66 73 65  n the cell offse
1488c 74 20 61 72 72 61 79 20 61 72 65 20 77 69 74 68  t array are with
1488d 69 6e 20 72 61 6e 67 65 2e 20 0a 20 20 2a 2a 20  in range. .  ** 
1488e 0a 20 20 2a 2a 20 4f 6d 69 74 74 69 6e 67 20 74  .  ** Omitting t
1488f 68 69 73 20 63 6f 6e 73 69 73 74 65 6e 63 79 20  his consistency 
14890 63 68 65 63 6b 20 61 6e 64 20 75 73 69 6e 67 20  check and using 
14891 74 68 65 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50  the pPage->maskP
14892 61 67 65 20 6d 61 73 6b 0a 20 20 2a 2a 20 74 6f  age mask.  ** to
14893 20 70 72 65 76 65 6e 74 20 6f 76 65 72 72 75 6e   prevent overrun
14894 6e 69 6e 67 20 74 68 65 20 70 61 67 65 20 62 75  ning the page bu
14895 66 66 65 72 20 69 6e 20 66 69 6e 64 43 65 6c 6c  ffer in findCell
14896 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20 61 0a  () results in a.
14897 20 20 2a 2a 20 32 2e 35 25 20 70 65 72 66 6f 72    ** 2.5% perfor
14898 6d 61 6e 63 65 20 67 61 69 6e 2e 0a 20 20 2a 2f  mance gain..  */
14899 0a 20 20 7b 0a 20 20 20 20 75 38 20 2a 70 4f 66  .  {.    u8 *pOf
1489a 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65  f;        /* Ite
1489b 72 61 74 6f 72 20 75 73 65 64 20 74 6f 20 63 68  rator used to ch
1489c 65 63 6b 20 61 6c 6c 20 63 65 6c 6c 20 6f 66 66  eck all cell off
1489d 73 65 74 73 20 61 72 65 20 69 6e 20 72 61 6e 67  sets are in rang
1489e 65 20 2a 2f 0a 20 20 20 20 75 38 20 2a 70 45 6e  e */.    u8 *pEn
1489f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  d;        /* Poi
148a0 6e 74 65 72 20 74 6f 20 65 6e 64 20 6f 66 20 63  nter to end of c
148a1 65 6c 6c 20 6f 66 66 73 65 74 20 61 72 72 61 79  ell offset array
148a2 20 2a 2f 0a 20 20 20 20 75 38 20 6d 61 73 6b 3b   */.    u8 mask;
148a3 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
148a4 20 6f 66 20 62 69 74 73 20 74 68 61 74 20 6d 75   of bits that mu
148a5 73 74 20 62 65 20 7a 65 72 6f 20 69 6e 20 4d 53  st be zero in MS
148a6 42 20 6f 66 20 63 65 6c 6c 20 6f 66 66 73 65 74  B of cell offset
148a7 73 20 2a 2f 0a 20 20 20 20 6d 61 73 6b 20 3d 20  s */.    mask = 
148a8 7e 28 28 28 75 38 29 28 70 42 74 2d 3e 70 61 67  ~(((u8)(pBt->pag
148a9 65 53 69 7a 65 3e 3e 38 29 29 2d 31 29 3b 0a 20  eSize>>8))-1);. 
148aa 20 20 20 70 45 6e 64 20 3d 20 26 64 61 74 61 5b     pEnd = &data[
148ab 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 70 50 61  cellOffset + pPa
148ac 67 65 2d 3e 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20  ge->nCell*2];.  
148ad 20 20 66 6f 72 28 70 4f 66 66 3d 26 64 61 74 61    for(pOff=&data
148ae 5b 63 65 6c 6c 4f 66 66 73 65 74 5d 3b 20 70 4f  [cellOffset]; pO
148af 66 66 21 3d 70 45 6e 64 20 26 26 20 21 28 28 2a  ff!=pEnd && !((*
148b0 70 4f 66 66 29 26 6d 61 73 6b 29 3b 20 70 4f 66  pOff)&mask); pOf
148b1 66 2b 3d 32 29 3b 0a 20 20 20 20 69 66 28 20 70  f+=2);.    if( p
148b2 4f 66 66 21 3d 70 45 6e 64 20 29 7b 0a 20 20 20  Off!=pEnd ){.   
148b3 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
148b4 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
148b5 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
148b6 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e  .    pPage->isIn
148b7 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65  it = 1;.  }.  re
148b8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
148b9 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20  }../*.** Set up 
148ba 61 20 72 61 77 20 70 61 67 65 20 73 6f 20 74 68  a raw page so th
148bb 61 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65  at it looks like
148bc 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65   a database page
148bd 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65   holding.** no e
148be 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  ntries..*/.stati
148bf 63 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 28  c void zeroPage(
148c0 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
148c1 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e  int flags){.  un
148c2 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74  signed char *dat
148c3 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61  a = pPage->aData
148c4 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
148c5 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
148c6 20 20 75 38 20 68 64 72 20 3d 20 70 50 61 67 65    u8 hdr = pPage
148c7 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 75  ->hdrOffset;.  u
148c8 31 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73  16 first;..  ass
148c9 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
148ca 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67  rPagenumber(pPag
148cb 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61  e->pDbPage)==pPa
148cc 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73  ge->pgno );.  as
148cd 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
148ce 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65  erGetExtra(pPage
148cf 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76  ->pDbPage) == (v
148d0 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20  oid*)pPage );.  
148d1 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
148d2 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67  agerGetData(pPag
148d3 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64  e->pDbPage) == d
148d4 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ata );.  assert(
148d5 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
148d6 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
148d7 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73  pDbPage) );.  as
148d8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
148d9 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
148da 74 65 78 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73  tex) );.  /*mems
148db 65 74 28 26 64 61 74 61 5b 68 64 72 5d 2c 20 30  et(&data[hdr], 0
148dc 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  , pBt->usableSiz
148dd 65 20 2d 20 68 64 72 29 3b 2a 2f 0a 20 20 64 61  e - hdr);*/.  da
148de 74 61 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 29  ta[hdr] = (char)
148df 66 6c 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d  flags;.  first =
148e0 20 68 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66   hdr + 8 + 4*((f
148e1 6c 61 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d  lags&PTF_LEAF)==
148e2 30 20 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65  0 ?1:0);.  memse
148e3 74 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20  t(&data[hdr+1], 
148e4 30 2c 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64  0, 4);.  data[hd
148e5 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32  r+7] = 0;.  put2
148e6 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
148e7 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  ], pBt->usableSi
148e8 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46  ze);.  pPage->nF
148e9 72 65 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c  ree = pBt->usabl
148ea 65 53 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20  eSize - first;. 
148eb 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61   decodeFlags(pPa
148ec 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50  ge, flags);.  pP
148ed 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d  age->hdrOffset =
148ee 20 68 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63   hdr;.  pPage->c
148ef 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73  ellOffset = firs
148f0 74 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65  t;.  pPage->nOve
148f1 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73  rflow = 0;.  ass
148f2 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69  ert( pBt->pageSi
148f3 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e  ze>=512 && pBt->
148f4 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20  pageSize<=32768 
148f5 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b  );.  pPage->mask
148f6 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 65  Page = pBt->page
148f7 53 69 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 67  Size - 1;.  pPag
148f8 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20  e->nCell = 0;.  
148f9 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
148fa 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  1;.}.../*.** Con
148fb 76 65 72 74 20 61 20 44 62 50 61 67 65 20 6f 62  vert a DbPage ob
148fc 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20  tained from the 
148fd 70 61 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d  pager into a Mem
148fe 50 61 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20  Page used by.** 
148ff 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e  the btree layer.
14900 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61  .*/.static MemPa
14901 67 65 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f  ge *btreePageFro
14902 6d 44 62 50 61 67 65 28 44 62 50 61 67 65 20 2a  mDbPage(DbPage *
14903 70 44 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67  pDbPage, Pgno pg
14904 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42  no, BtShared *pB
14905 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  t){.  MemPage *p
14906 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a  Page = (MemPage*
14907 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  )sqlite3PagerGet
14908 45 78 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a  Extra(pDbPage);.
14909 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d    pPage->aData =
1490a 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
1490b 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20  Data(pDbPage);. 
1490c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20   pPage->pDbPage 
1490d 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61  = pDbPage;.  pPa
1490e 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20  ge->pBt = pBt;. 
1490f 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70   pPage->pgno = p
14910 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64  gno;.  pPage->hd
14911 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d  rOffset = pPage-
14912 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a  >pgno==1 ? 100 :
14913 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61   0;.  return pPa
14914 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ge; .}../*.** Ge
14915 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  t a page from th
14916 65 20 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61  e pager.  Initia
14917 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65  lize the MemPage
14918 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50  .pBt and.** MemP
14919 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e  age.aData elemen
1491a 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a  ts if needed..**
1491b 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e  .** If the noCon
1491c 74 65 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74  tent flag is set
1491d 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
1491e 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61  we do not care a
1491f 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  bout.** the cont
14920 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 20  ent of the page 
14921 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53  at this time.  S
14922 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74  o do not go to t
14923 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65  he disk.** to fe
14924 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e  tch the content.
14925 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74    Just fill in t
14926 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20  he content with 
14927 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a  zeros for now..*
14928 2a 20 49 66 20 69 6e 20 74 68 65 20 66 75 74 75  * If in the futu
14929 72 65 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74  re we call sqlit
1492a 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 6f  e3PagerWrite() o
1492b 6e 20 74 68 69 73 20 70 61 67 65 2c 20 74 68 61  n this page, tha
1492c 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61  t.** means we ha
1492d 76 65 20 73 74 61 72 74 65 64 20 74 6f 20 62 65  ve started to be
1492e 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74   concerned about
1492f 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65   content and the
14930 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68   disk.** read sh
14931 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68  ould occur at th
14932 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c  at point..*/.SQL
14933 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
14934 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
14935 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20  age(.  BtShared 
14936 2a 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54  *pBt,       /* T
14937 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67  he btree */.  Pg
14938 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20  no pgno,        
14939 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1493a 74 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63  the page to fetc
1493b 68 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  h */.  MemPage *
1493c 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52  *ppPage,    /* R
1493d 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69  eturn the page i
1493e 6e 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72  n this parameter
1493f 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74   */.  int noCont
14940 65 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  ent        /* Do
14941 20 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 63   not load page c
14942 6f 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 2a  ontent if true *
14943 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
14944 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
14945 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
14946 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14947 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
14948 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
14949 65 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70  erAcquire(pBt->p
1494a 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62  Pager, pgno, (Db
1494b 50 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c  Page**)&pDbPage,
1494c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69   noContent);.  i
1494d 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1494e 63 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62  c;.  *ppPage = b
1494f 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61  treePageFromDbPa
14950 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f  ge(pDbPage, pgno
14951 2c 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 6e  , pBt);.  return
14952 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
14953 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
14954 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
14955 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67  base file in pag
14956 65 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  es. If there is 
14957 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65  any kind of.** e
14958 72 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75  rror, return ((u
14959 6e 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e  nsigned int)-1).
1495a 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20  .*/.static Pgno 
1495b 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 42  pagerPagecount(B
1495c 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
1495d 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b   int nPage = -1;
1495e 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73  .  int rc;.  ass
1495f 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ert( pBt->pPage1
14960 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
14961 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
14962 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e  (pBt->pPager, &n
14963 50 61 67 65 29 3b 0a 20 20 61 73 73 65 72 74 28  Page);.  assert(
14964 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
14965 7c 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20  | nPage==-1 );. 
14966 20 72 65 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50   return (Pgno)nP
14967 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  age;.}../*.** Ge
14968 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  t a page from th
14969 65 20 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74  e pager and init
1496a 69 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73  ialize it.  This
1496b 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6a   routine.** is j
1496c 75 73 74 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63  ust a convenienc
1496d 65 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64  e wrapper around
1496e 20 73 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20   separate calls 
1496f 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72  to.** sqlite3Btr
14970 65 65 47 65 74 50 61 67 65 28 29 20 61 6e 64 20  eeGetPage() and 
14971 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
14972 50 61 67 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Page()..*/.stati
14973 63 20 69 6e 74 20 67 65 74 41 6e 64 49 6e 69 74  c int getAndInit
14974 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64  Page(.  BtShared
14975 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20   *pBt,          
14976 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
14977 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  file */.  Pgno p
14978 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f  gno,           /
14979 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  * Number of the 
1497a 70 61 67 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20  page to get */. 
1497b 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67   MemPage **ppPag
1497c 65 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74  e     /* Write t
1497d 68 65 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20  he page pointer 
1497e 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
1497f 20 72 63 3b 0a 20 20 44 62 50 61 67 65 20 2a 70   rc;.  DbPage *p
14980 44 62 50 61 67 65 3b 0a 20 20 4d 65 6d 50 61 67  DbPage;.  MemPag
14981 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73  e *pPage;..  ass
14982 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14983 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
14984 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67 6e  ex) );.  if( pgn
14985 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  o==0 ){.    retu
14986 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
14987 54 5f 42 4b 50 54 3b 20 0a 20 20 7d 0a 0a 20 20  T_BKPT; .  }..  
14988 2f 2a 20 49 74 20 69 73 20 6f 66 74 65 6e 20 74  /* It is often t
14989 68 65 20 63 61 73 65 20 74 68 61 74 20 74 68 65  he case that the
1498a 20 70 61 67 65 20 77 65 20 77 61 6e 74 20 69 73   page we want is
1498b 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68   already in cach
1498c 65 2e 0a 20 20 2a 2a 20 49 66 20 73 6f 2c 20 67  e..  ** If so, g
1498d 65 74 20 69 74 20 64 69 72 65 63 74 6c 79 2e 20  et it directly. 
1498e 20 54 68 69 73 20 73 61 76 65 73 20 75 73 20 66   This saves us f
1498f 72 6f 6d 20 68 61 76 69 6e 67 20 74 6f 20 63 61  rom having to ca
14990 6c 6c 0a 20 20 2a 2a 20 70 61 67 65 72 50 61 67  ll.  ** pagerPag
14991 65 63 6f 75 6e 74 28 29 20 74 6f 20 6d 61 6b 65  ecount() to make
14992 20 73 75 72 65 20 70 67 6e 6f 20 69 73 20 77 69   sure pgno is wi
14993 74 68 69 6e 20 6c 69 6d 69 74 73 2c 20 77 68 69  thin limits, whi
14994 63 68 20 72 65 73 75 6c 74 73 0a 20 20 2a 2a 20  ch results.  ** 
14995 69 6e 20 61 20 6d 65 61 73 75 72 65 61 62 6c 65  in a measureable
14996 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70   performance imp
14997 72 6f 76 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a  rovements..  */.
14998 20 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69    pDbPage = sqli
14999 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70  te3PagerLookup(p
1499a 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f  Bt->pPager, pgno
1499b 29 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65  );.  if( pDbPage
1499c 20 29 7b 0a 20 20 20 20 2f 2a 20 50 61 67 65 20   ){.    /* Page 
1499d 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61  is already in ca
1499e 63 68 65 20 2a 2f 0a 20 20 20 20 2a 70 70 50 61  che */.    *ppPa
1499f 67 65 20 3d 20 70 50 61 67 65 20 3d 20 62 74 72  ge = pPage = btr
149a0 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65  eePageFromDbPage
149a1 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20  (pDbPage, pgno, 
149a2 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  pBt);.    rc = S
149a3 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
149a4 65 7b 0a 20 20 20 20 2f 2a 20 50 61 67 65 20 6e  e{.    /* Page n
149a5 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 20 41 63  ot in cache.  Ac
149a6 71 75 69 72 65 20 69 74 2e 20 2a 2f 0a 20 20 20  quire it. */.   
149a7 20 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50   if( pgno>pagerP
149a8 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b  agecount(pBt) ){
149a9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
149aa 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
149ab 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  T; .    }.    rc
149ac 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
149ad 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f  etPage(pBt, pgno
149ae 2c 20 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20  , ppPage, 0);.  
149af 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
149b0 6e 20 72 63 3b 0a 20 20 20 20 70 50 61 67 65 20  n rc;.    pPage 
149b1 3d 20 2a 70 70 50 61 67 65 3b 0a 20 20 7d 0a 20  = *ppPage;.  }. 
149b2 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 73 49   if( !pPage->isI
149b3 6e 69 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  nit ){.    rc = 
149b4 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
149b5 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d  Page(pPage);.  }
149b6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
149b7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 6c 65  E_OK ){.    rele
149b8 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
149b9 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b      *ppPage = 0;
149ba 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
149bb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
149bc 73 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 20 54  se a MemPage.  T
149bd 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 63 61  his should be ca
149be 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  lled once for ea
149bf 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c  ch prior.** call
149c0 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
149c1 47 65 74 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74  GetPage..*/.stat
149c2 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50  ic void releaseP
149c3 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  age(MemPage *pPa
149c4 67 65 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65  ge){.  if( pPage
149c5 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
149c6 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
149c7 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61  ==0 || sqlite3Pa
149c8 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28  gerPageRefcount(
149c9 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3e  pPage->pDbPage)>
149ca 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  1 );.    assert(
149cb 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b   pPage->aData );
149cc 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
149cd 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61  ge->pBt );.    a
149ce 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
149cf 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67  gerGetExtra(pPag
149d0 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28  e->pDbPage) == (
149d1 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20  void*)pPage );. 
149d2 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
149d3 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
149d4 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d  Page->pDbPage)==
149d5 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a  pPage->aData );.
149d6 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
149d7 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
149d8 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
149d9 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) );.    sqlite3
149da 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65  PagerUnref(pPage
149db 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a  ->pDbPage);.  }.
149dc 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 20  }../*.** During 
149dd 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 77 68 65 6e  a rollback, when
149de 20 74 68 65 20 70 61 67 65 72 20 72 65 6c 6f 61   the pager reloa
149df 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ds information i
149e0 6e 74 6f 20 74 68 65 20 63 61 63 68 65 0a 2a 2a  nto the cache.**
149e1 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 61 63   so that the cac
149e2 68 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 74  he is restored t
149e3 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73  o its original s
149e4 74 61 74 65 20 61 74 20 74 68 65 20 73 74 61 72  tate at the star
149e5 74 20 6f 66 0a 2a 2a 20 74 68 65 20 74 72 61 6e  t of.** the tran
149e6 73 61 63 74 69 6f 6e 2c 20 66 6f 72 20 65 61 63  saction, for eac
149e7 68 20 70 61 67 65 20 72 65 73 74 6f 72 65 64 20  h page restored 
149e8 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
149e9 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  called..**.** Th
149ea 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 73  is routine needs
149eb 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 65 78   to reset the ex
149ec 74 72 61 20 64 61 74 61 20 73 65 63 74 69 6f 6e  tra data section
149ed 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
149ee 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 61 67  he.** page to ag
149ef 72 65 65 20 77 69 74 68 20 74 68 65 20 72 65 73  ree with the res
149f0 74 6f 72 65 64 20 64 61 74 61 2e 0a 2a 2f 0a 73  tored data..*/.s
149f1 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 52  tatic void pageR
149f2 65 69 6e 69 74 28 44 62 50 61 67 65 20 2a 70 44  einit(DbPage *pD
149f3 61 74 61 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  ata){.  MemPage 
149f4 2a 70 50 61 67 65 3b 0a 20 20 70 50 61 67 65 20  *pPage;.  pPage 
149f5 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c  = (MemPage *)sql
149f6 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72  ite3PagerGetExtr
149f7 61 28 70 44 61 74 61 29 3b 0a 20 20 69 66 28 20  a(pData);.  if( 
149f8 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b  pPage->isInit ){
149f9 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
149fa 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
149fb 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
149fc 78 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d  x) );.    pPage-
149fd 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20  >isInit = 0;.   
149fe 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65   if( sqlite3Page
149ff 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 44  rPageRefcount(pD
14a00 61 74 61 29 3e 30 20 29 7b 0a 20 20 20 20 20 20  ata)>0 ){.      
14a01 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
14a02 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
14a03 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
14a04 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20  Invoke the busy 
14a05 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74  handler for a bt
14a06 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ree..*/.static i
14a07 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75  nt btreeInvokeBu
14a08 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a  syHandler(void *
14a09 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65  pArg){.  BtShare
14a0a 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72  d *pBt = (BtShar
14a0b 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65  ed*)pArg;.  asse
14a0c 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20  rt( pBt->db );. 
14a0d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
14a0e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
14a0f 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
14a10 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49   return sqlite3I
14a11 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72  nvokeBusyHandler
14a12 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48  (&pBt->db->busyH
14a13 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  andler);.}../*.*
14a14 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73  * Open a databas
14a15 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a  e file..** .** z
14a16 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20  Filename is the 
14a17 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
14a18 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a  base file.  If z
14a19 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c  Filename is NULL
14a1a 0a 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62 61  .** a new databa
14a1b 73 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f 6d  se with a random
14a1c 20 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65 64   name is created
14a1d 2e 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79  .  This randomly
14a1e 20 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61   named.** databa
14a1f 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20  se file will be 
14a20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71 6c  deleted when sql
14a21 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 29  ite3BtreeClose()
14a22 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49   is called..** I
14a23 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22  f zFilename is "
14a24 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61  :memory:" then a
14a25 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
14a26 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 0a  base is created.
14a27 2a 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d  ** that is autom
14a28 61 74 69 63 61 6c 6c 79 20 64 65 73 74 72 6f 79  atically destroy
14a29 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c  ed when it is cl
14a2a 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  osed..*/.SQLITE_
14a2b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14a2c 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20  te3BtreeOpen(.  
14a2d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
14a2e 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20  ename,  /* Name 
14a2f 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74  of the file cont
14a30 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65  aining the BTree
14a31 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73   database */.  s
14a32 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
14a33 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69         /* Associ
14a34 61 74 65 64 20 64 61 74 61 62 61 73 65 20 68 61  ated database ha
14a35 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20  ndle */.  Btree 
14a36 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 20  **ppBtree,      
14a37 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
14a38 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74  new Btree object
14a39 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
14a3a 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
14a3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
14a3c 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76  tions */.  int v
14a3d 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20  fsFlags         
14a3e 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73     /* Flags pass
14a3f 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71  ed through to sq
14a40 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28  lite3_vfs.xOpen(
14a41 29 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  ) */.){.  sqlite
14a42 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20  3_vfs *pVfs;    
14a43 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20    /* The VFS to 
14a44 75 73 65 20 66 6f 72 20 74 68 69 73 20 62 74 72  use for this btr
14a45 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64  ee */.  BtShared
14a46 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 20 20   *pBt = 0;      
14a47 2f 2a 20 53 68 61 72 65 64 20 70 61 72 74 20 6f  /* Shared part o
14a48 66 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72  f btree structur
14a49 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b  e */.  Btree *p;
14a4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14a4b 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 75  * Handle to retu
14a4c 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  rn */.  int rc =
14a4d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38   SQLITE_OK;.  u8
14a4e 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 75 6e 73   nReserve;.  uns
14a4f 69 67 6e 65 64 20 63 68 61 72 20 7a 44 62 48 65  igned char zDbHe
14a50 61 64 65 72 5b 31 30 30 5d 3b 0a 0a 20 20 2f 2a  ader[100];..  /*
14a51 20 53 65 74 20 74 68 65 20 76 61 72 69 61 62 6c   Set the variabl
14a52 65 20 69 73 4d 65 6d 64 62 20 74 6f 20 74 72 75  e isMemdb to tru
14a53 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  e for an in-memo
14a54 72 79 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20  ry database, or 
14a55 0a 20 20 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20  .  ** false for 
14a56 61 20 66 69 6c 65 2d 62 61 73 65 64 20 64 61 74  a file-based dat
14a57 61 62 61 73 65 2e 20 54 68 69 73 20 73 79 6d 62  abase. This symb
14a58 6f 6c 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69  ol is only requi
14a59 72 65 64 20 69 66 0a 20 20 2a 2a 20 65 69 74 68  red if.  ** eith
14a5a 65 72 20 6f 66 20 74 68 65 20 73 68 61 72 65 64  er of the shared
14a5b 2d 64 61 74 61 20 6f 72 20 61 75 74 6f 76 61 63  -data or autovac
14a5c 75 75 6d 20 66 65 61 74 75 72 65 73 20 61 72 65  uum features are
14a5d 20 63 6f 6d 70 69 6c 65 64 20 0a 20 20 2a 2a 20   compiled .  ** 
14a5e 69 6e 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79  into the library
14a5f 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69  ..  */.#if !defi
14a60 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
14a61 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 7c 7c  SHARED_CACHE) ||
14a62 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
14a63 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
14a64 29 0a 20 20 23 69 66 64 65 66 20 53 51 4c 49 54  ).  #ifdef SQLIT
14a65 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a  E_OMIT_MEMORYDB.
14a66 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73      const int is
14a67 4d 65 6d 64 62 20 3d 20 30 3b 0a 20 20 23 65 6c  Memdb = 0;.  #el
14a68 73 65 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  se.    const int
14a69 20 69 73 4d 65 6d 64 62 20 3d 20 7a 46 69 6c 65   isMemdb = zFile
14a6a 6e 61 6d 65 20 26 26 20 21 73 74 72 63 6d 70 28  name && !strcmp(
14a6b 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d  zFilename, ":mem
14a6c 6f 72 79 3a 22 29 3b 0a 20 20 23 65 6e 64 69 66  ory:");.  #endif
14a6d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72  .#endif..  asser
14a6e 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73  t( db!=0 );.  as
14a6f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14a70 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
14a71 65 78 29 20 29 3b 0a 0a 20 20 70 56 66 73 20 3d  ex) );..  pVfs =
14a72 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 70 20 3d   db->pVfs;.  p =
14a73 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65   sqlite3MallocZe
14a74 72 6f 28 73 69 7a 65 6f 66 28 42 74 72 65 65 29  ro(sizeof(Btree)
14a75 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  );.  if( !p ){. 
14a76 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
14a77 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d  _NOMEM;.  }.  p-
14a78 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53  >inTrans = TRANS
14a79 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 64 62 20 3d  _NONE;.  p->db =
14a7a 20 64 62 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e   db;..#if !defin
14a7b 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
14a7c 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20  HARED_CACHE) && 
14a7d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
14a7e 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f  OMIT_DISKIO).  /
14a7f 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 42  *.  ** If this B
14a80 74 72 65 65 20 69 73 20 61 20 63 61 6e 64 69 64  tree is a candid
14a81 61 74 65 20 66 6f 72 20 73 68 61 72 65 64 20 63  ate for shared c
14a82 61 63 68 65 2c 20 74 72 79 20 74 6f 20 66 69 6e  ache, try to fin
14a83 64 20 61 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69  d an.  ** existi
14a84 6e 67 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  ng BtShared obje
14a85 63 74 20 74 68 61 74 20 77 65 20 63 61 6e 20 73  ct that we can s
14a86 68 61 72 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20  hare with.  */. 
14a87 20 69 66 28 20 69 73 4d 65 6d 64 62 3d 3d 30 0a   if( isMemdb==0.
14a88 20 20 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73     && (db->flags
14a89 20 26 20 53 51 4c 49 54 45 5f 56 74 61 62 29 3d   & SQLITE_Vtab)=
14a8a 3d 30 0a 20 20 20 26 26 20 7a 46 69 6c 65 6e 61  =0.   && zFilena
14a8b 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b  me && zFilename[
14a8c 30 5d 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20  0].  ){.    if( 
14a8d 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
14a8e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 45  fig.sharedCacheE
14a8f 6e 61 62 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  nabled ){.      
14a90 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d  int nFullPathnam
14a91 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68  e = pVfs->mxPath
14a92 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63 68  name+1;.      ch
14a93 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d  ar *zFullPathnam
14a94 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  e = sqlite3Mallo
14a95 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  c(nFullPathname)
14a96 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
14a97 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72  mutex *mutexShar
14a98 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 61  ed;.      p->sha
14a99 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20  rable = 1;.     
14a9a 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51   db->flags |= SQ
14a9b 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 65  LITE_SharedCache
14a9c 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 46 75  ;.      if( !zFu
14a9d 6c 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20  llPathname ){.  
14a9e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
14a9f 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72  ee(p);.        r
14aa0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
14aa1 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EM;.      }.    
14aa2 20 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50    sqlite3OsFullP
14aa3 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46  athname(pVfs, zF
14aa4 69 6c 65 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50 61  ilename, nFullPa
14aa5 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74  thname, zFullPat
14aa6 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 6d 75  hname);.      mu
14aa7 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69  texShared = sqli
14aa8 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
14aa9 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
14aaa 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20  C_MASTER);.     
14aab 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
14aac 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 65 64  nter(mutexShared
14aad 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74  );.      for(pBt
14aae 3d 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64  =GLOBAL(BtShared
14aaf 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43  *,sqlite3SharedC
14ab0 61 63 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20  acheList); pBt; 
14ab1 70 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b  pBt=pBt->pNext){
14ab2 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
14ab3 20 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a   pBt->nRef>0 );.
14ab4 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73          if( 0==s
14ab5 74 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e  trcmp(zFullPathn
14ab6 61 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65  ame, sqlite3Page
14ab7 72 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70  rFilename(pBt->p
14ab8 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20  Pager)).        
14ab9 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69           && sqli
14aba 74 65 33 50 61 67 65 72 56 66 73 28 70 42 74 2d  te3PagerVfs(pBt-
14abb 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73 20 29  >pPager)==pVfs )
14abc 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  {.          p->p
14abd 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20  Bt = pBt;.      
14abe 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b      pBt->nRef++;
14abf 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
14ac0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
14ac1 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
14ac2 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
14ac3 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20  texShared);.    
14ac4 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
14ac5 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20  FullPathname);. 
14ac6 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
14ac7 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65 6c 73  TE_DEBUG.    els
14ac8 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 64  e{.      /* In d
14ac9 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61  ebug mode, we ma
14aca 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74 65 6e  rk all persisten
14acb 74 20 64 61 74 61 62 61 73 65 73 20 61 73 20 73  t databases as s
14acc 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  harable.      **
14acd 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 79 20   even when they 
14ace 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 65  are not.  This e
14acf 78 65 72 63 69 73 65 73 20 74 68 65 20 6c 6f 63  xercises the loc
14ad0 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 20  king code and.  
14ad1 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72      ** gives mor
14ad2 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f  e opportunity fo
14ad3 72 20 61 73 73 65 72 74 73 28 73 71 6c 69 74 65  r asserts(sqlite
14ad4 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 0a  3_mutex_held()).
14ad5 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65        ** stateme
14ad6 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b  nts to find lock
14ad7 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20  ing problems..  
14ad8 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e      */.      p->
14ad9 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20  sharable = 1;.  
14ada 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23    }.#endif.  }.#
14adb 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42 74 3d  endif.  if( pBt=
14adc 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20  =0 ){.    /*.   
14add 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   ** The followin
14ade 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65 20 73  g asserts make s
14adf 75 72 65 20 74 68 61 74 20 73 74 72 75 63 74 75  ure that structu
14ae0 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  res used by the 
14ae1 62 74 72 65 65 20 61 72 65 0a 20 20 20 20 2a 2a  btree are.    **
14ae2 20 74 68 65 20 72 69 67 68 74 20 73 69 7a 65 2e   the right size.
14ae3 20 20 54 68 69 73 20 69 73 20 74 6f 20 67 75 61    This is to gua
14ae4 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a 65 20  rd against size 
14ae5 63 68 61 6e 67 65 73 20 74 68 61 74 20 72 65 73  changes that res
14ae6 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20  ult.    ** when 
14ae7 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64  compiling on a d
14ae8 69 66 66 65 72 65 6e 74 20 61 72 63 68 69 74 65  ifferent archite
14ae9 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  cture..    */.  
14aea 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
14aeb 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65  (i64)==8 || size
14aec 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20  of(i64)==4 );.  
14aed 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
14aee 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65  (u64)==8 || size
14aef 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20  of(u64)==4 );.  
14af0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
14af1 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20  (u32)==4 );.    
14af2 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75  assert( sizeof(u
14af3 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73  16)==2 );.    as
14af4 73 65 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e  sert( sizeof(Pgn
14af5 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20  o)==4 );.  .    
14af6 70 42 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  pBt = sqlite3Mal
14af7 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
14af8 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28  *pBt) );.    if(
14af9 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20   pBt==0 ){.     
14afa 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
14afb 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  EM;.      goto b
14afc 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  tree_open_out;. 
14afd 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
14afe 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70  lite3PagerOpen(p
14aff 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65  Vfs, &pBt->pPage
14b00 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20  r, zFilename,.  
14b01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b02 20 20 20 20 20 20 20 20 45 58 54 52 41 5f 53 49          EXTRA_SI
14b03 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c  ZE, flags, vfsFl
14b04 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ags);.    if( rc
14b05 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
14b06 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
14b07 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65  3PagerReadFilehe
14b08 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72  ader(pBt->pPager
14b09 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 64 65  ,sizeof(zDbHeade
14b0a 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b 0a 20  r),zDbHeader);. 
14b0b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
14b0c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14b0d 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f      goto btree_o
14b0e 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
14b0f 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53     sqlite3PagerS
14b10 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 70 42  etBusyhandler(pB
14b11 74 2d 3e 70 50 61 67 65 72 2c 20 62 74 72 65 65  t->pPager, btree
14b12 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65  InvokeBusyHandle
14b13 72 2c 20 70 42 74 29 3b 0a 20 20 20 20 70 2d 3e  r, pBt);.    p->
14b14 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a 20 20  pBt = pBt;.  .  
14b15 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65    sqlite3PagerSe
14b16 74 52 65 69 6e 69 74 65 72 28 70 42 74 2d 3e 70  tReiniter(pBt->p
14b17 50 61 67 65 72 2c 20 70 61 67 65 52 65 69 6e 69  Pager, pageReini
14b18 74 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 43 75  t);.    pBt->pCu
14b19 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 70 42  rsor = 0;.    pB
14b1a 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20  t->pPage1 = 0;. 
14b1b 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79     pBt->readOnly
14b1c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 49   = sqlite3PagerI
14b1d 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d 3e 70  sreadonly(pBt->p
14b1e 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 74 2d  Pager);.    pBt-
14b1f 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32  >pageSize = get2
14b20 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b  byte(&zDbHeader[
14b21 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 42  16]);.    if( pB
14b22 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31 32 20  t->pageSize<512 
14b23 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  || pBt->pageSize
14b24 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  >SQLITE_MAX_PAGE
14b25 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20 20 7c  _SIZE.         |
14b26 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a  | ((pBt->pageSiz
14b27 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 53 69  e-1)&pBt->pageSi
14b28 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ze)!=0 ){.      
14b29 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20  pBt->pageSize = 
14b2a 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
14b2b 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
14b2c 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70  (pBt->pPager, &p
14b2d 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23  Bt->pageSize);.#
14b2e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14b2f 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
14b30 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6d 61      /* If the ma
14b31 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72  gic name ":memor
14b32 79 3a 22 20 77 69 6c 6c 20 63 72 65 61 74 65 20  y:" will create 
14b33 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
14b34 61 62 61 73 65 2c 20 74 68 65 6e 0a 20 20 20 20  abase, then.    
14b35 20 20 2a 2a 20 6c 65 61 76 65 20 74 68 65 20 61    ** leave the a
14b36 75 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65 20 61  utoVacuum mode a
14b37 74 20 30 20 28 64 6f 20 6e 6f 74 20 61 75 74 6f  t 0 (do not auto
14b38 2d 76 61 63 75 75 6d 29 2c 20 65 76 65 6e 20 69  -vacuum), even i
14b39 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54  f.      ** SQLIT
14b3a 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41  E_DEFAULT_AUTOVA
14b3b 43 55 55 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e  CUUM is true. On
14b3c 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
14b3d 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c   if.      ** SQL
14b3e 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44  ITE_OMIT_MEMORYD
14b3f 42 20 68 61 73 20 62 65 65 6e 20 64 65 66 69 6e  B has been defin
14b40 65 64 2c 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72  ed, then ":memor
14b41 79 3a 22 20 69 73 20 6a 75 73 74 20 61 0a 20 20  y:" is just a.  
14b42 20 20 20 20 2a 2a 20 72 65 67 75 6c 61 72 20 66      ** regular f
14b43 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 69  ile-name. In thi
14b44 73 20 63 61 73 65 20 74 68 65 20 61 75 74 6f 2d  s case the auto-
14b45 76 61 63 75 75 6d 20 61 70 70 6c 69 65 73 20 61  vacuum applies a
14b46 73 20 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20  s per normal..  
14b47 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
14b48 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 69   zFilename && !i
14b49 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 20 20  sMemdb ){.      
14b4a 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75    pBt->autoVacuu
14b4b 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41  m = (SQLITE_DEFA
14b4c 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f  ULT_AUTOVACUUM ?
14b4d 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20   1 : 0);.       
14b4e 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d   pBt->incrVacuum
14b4f 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55   = (SQLITE_DEFAU
14b50 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32  LT_AUTOVACUUM==2
14b51 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20   ? 1 : 0);.     
14b52 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
14b53 6e 52 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20  nReserve = 0;.  
14b54 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
14b55 52 65 73 65 72 76 65 20 3d 20 7a 44 62 48 65 61  Reserve = zDbHea
14b56 64 65 72 5b 32 30 5d 3b 0a 20 20 20 20 20 20 70  der[20];.      p
14b57 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65  Bt->pageSizeFixe
14b58 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53  d = 1;.#ifndef S
14b59 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
14b5a 41 43 55 55 4d 0a 20 20 20 20 20 20 70 42 74 2d  ACUUM.      pBt-
14b5b 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67  >autoVacuum = (g
14b5c 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 61 64  et4byte(&zDbHead
14b5d 65 72 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a  er[36 + 4*4])?1:
14b5e 30 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69  0);.      pBt->i
14b5f 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74  ncrVacuum = (get
14b60 34 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72  4byte(&zDbHeader
14b61 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29  [36 + 7*4])?1:0)
14b62 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20  ;.#endif.    }. 
14b63 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69     pBt->usableSi
14b64 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69  ze = pBt->pageSi
14b65 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a 20  ze - nReserve;. 
14b66 20 20 20 61 73 73 65 72 74 28 20 28 70 42 74 2d     assert( (pBt-
14b67 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d  >pageSize & 7)==
14b68 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 20  0 );  /* 8-byte 
14b69 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 67  alignment of pag
14b6a 65 53 69 7a 65 20 2a 2f 0a 20 20 20 20 73 71 6c  eSize */.    sql
14b6b 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65  ite3PagerSetPage
14b6c 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72  size(pBt->pPager
14b6d 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  , &pBt->pageSize
14b6e 29 3b 0a 20 20 20 0a 23 69 66 20 21 64 65 66 69  );.   .#if !defi
14b6f 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
14b70 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26  SHARED_CACHE) &&
14b71 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
14b72 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20  _OMIT_DISKIO).  
14b73 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77    /* Add the new
14b74 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
14b75 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c   to the linked l
14b76 69 73 74 20 73 68 61 72 61 62 6c 65 20 42 74 53  ist sharable BtS
14b77 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f 0a 20  hareds..    */. 
14b78 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62     if( p->sharab
14b79 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  le ){.      sqli
14b7a 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
14b7b 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 70 42  Shared;.      pB
14b7c 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  t->nRef = 1;.   
14b7d 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d     mutexShared =
14b7e 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
14b7f 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
14b80 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
14b81 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45        if( SQLITE
14b82 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 73  _THREADSAFE && s
14b83 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
14b84 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b  ig.bCoreMutex ){
14b85 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6d 75  .        pBt->mu
14b86 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
14b87 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
14b88 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 20  UTEX_FAST);.    
14b89 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d 75 74      if( pBt->mut
14b8a 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ex==0 ){.       
14b8b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
14b8c 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20  OMEM;.          
14b8d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
14b8e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
14b8f 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f  goto btree_open_
14b90 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  out;.        }. 
14b91 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
14b92 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
14b93 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20  (mutexShared);. 
14b94 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78 74 20       pBt->pNext 
14b95 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65  = GLOBAL(BtShare
14b96 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
14b97 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20  CacheList);.    
14b98 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65    GLOBAL(BtShare
14b99 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
14b9a 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74  CacheList) = pBt
14b9b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
14b9c 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
14b9d 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 7d 0a  xShared);.    }.
14b9e 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 66 20  #endif.  }..#if 
14b9f 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
14ba0 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
14ba1 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  E) && !defined(S
14ba2 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
14ba3 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e  O).  /* If the n
14ba4 65 77 20 42 74 72 65 65 20 75 73 65 73 20 61 20  ew Btree uses a 
14ba5 73 68 61 72 61 62 6c 65 20 70 42 74 53 68 61 72  sharable pBtShar
14ba6 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 74 68  ed, then link th
14ba7 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72 65 65  e new.  ** Btree
14ba8 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 20 6f   into the list o
14ba9 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65 20 42  f all sharable B
14baa 74 72 65 65 73 20 66 6f 72 20 74 68 65 20 73 61  trees for the sa
14bab 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 20  me connection.. 
14bac 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20   ** The list is 
14bad 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64 69 6e  kept in ascendin
14bae 67 20 6f 72 64 65 72 20 62 79 20 70 42 74 20 61  g order by pBt a
14baf 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 69  ddress..  */.  i
14bb0 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  f( p->sharable )
14bb1 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
14bb2 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a 20 20   Btree *pSib;.  
14bb3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
14bb4 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
14bb5 20 20 69 66 28 20 28 70 53 69 62 20 3d 20 64 62    if( (pSib = db
14bb6 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21 3d 30  ->aDb[i].pBt)!=0
14bb7 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72 61 62   && pSib->sharab
14bb8 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68  le ){.        wh
14bb9 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72 65 76  ile( pSib->pPrev
14bba 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69 62 2d   ){ pSib = pSib-
14bbb 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20 20 20  >pPrev; }.      
14bbc 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70 53 69    if( p->pBt<pSi
14bbd 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20  b->pBt ){.      
14bbe 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70      p->pNext = p
14bbf 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70  Sib;.          p
14bc0 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20  ->pPrev = 0;.   
14bc1 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 50 72         pSib->pPr
14bc2 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  ev = p;.        
14bc3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
14bc4 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 4e   while( pSib->pN
14bc5 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70 4e 65  ext && pSib->pNe
14bc6 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29  xt->pBt<p->pBt )
14bc7 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53  {.            pS
14bc8 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74  ib = pSib->pNext
14bc9 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
14bca 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74          p->pNext
14bcb 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a   = pSib->pNext;.
14bcc 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72            p->pPr
14bcd 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20  ev = pSib;.     
14bce 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78       if( p->pNex
14bcf 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
14bd0 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76   p->pNext->pPrev
14bd1 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20   = p;.          
14bd2 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 62  }.          pSib
14bd3 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 20 20  ->pNext = p;.   
14bd4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
14bd5 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
14bd6 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
14bd7 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b 0a 0a   *ppBtree = p;..
14bd8 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3a 0a  btree_open_out:.
14bd9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
14bda 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70  _OK ){.    if( p
14bdb 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65  Bt && pBt->pPage
14bdc 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
14bdd 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74  e3PagerClose(pBt
14bde 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d  ->pPager);.    }
14bdf 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
14be0 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69  e(pBt);.    sqli
14be1 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20  te3_free(p);.   
14be2 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b 0a 20   *ppBtree = 0;. 
14be3 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
14be4 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65  }../*.** Decreme
14be5 6e 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  nt the BtShared.
14be6 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 20 57  nRef counter.  W
14be7 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 7a  hen it reaches z
14be8 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 74  ero,.** remove t
14be9 68 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75  he BtShared stru
14bea 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 73  cture from the s
14beb 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 52 65  haring list.  Re
14bec 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20  turn.** true if 
14bed 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65  the BtShared.nRe
14bee 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63 68 65  f counter reache
14bef 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72  s zero and retur
14bf0 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74  n.** false if it
14bf1 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 74 69   is still positi
14bf2 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ve..*/.static in
14bf3 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72  t removeFromShar
14bf4 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72 65 64  ingList(BtShared
14bf5 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 66 20   *pBt){.#ifndef 
14bf6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
14bf7 45 44 5f 43 41 43 48 45 0a 20 20 73 71 6c 69 74  ED_CACHE.  sqlit
14bf8 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 65  e3_mutex *pMaste
14bf9 72 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  r;.  BtShared *p
14bfa 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f  List;.  int remo
14bfb 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65  ved = 0;..  asse
14bfc 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
14bfd 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d  x_notheld(pBt->m
14bfe 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 61 73 74  utex) );.  pMast
14bff 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  er = sqlite3Mute
14c00 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
14c01 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
14c02 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  R);.  sqlite3_mu
14c03 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65  tex_enter(pMaste
14c04 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d  r);.  pBt->nRef-
14c05 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52  -;.  if( pBt->nR
14c06 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ef<=0 ){.    if(
14c07 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64   GLOBAL(BtShared
14c08 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43  *,sqlite3SharedC
14c09 61 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29  acheList)==pBt )
14c0a 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42  {.      GLOBAL(B
14c0b 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33  tShared*,sqlite3
14c0c 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29  SharedCacheList)
14c0d 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20   = pBt->pNext;. 
14c0e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
14c0f 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42  pList = GLOBAL(B
14c10 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33  tShared*,sqlite3
14c11 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29  SharedCacheList)
14c12 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41  ;.      while( A
14c13 4c 57 41 59 53 28 70 4c 69 73 74 29 20 26 26 20  LWAYS(pList) && 
14c14 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42  pList->pNext!=pB
14c15 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69  t ){.        pLi
14c16 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b  st=pList->pNext;
14c17 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
14c18 66 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29  f( ALWAYS(pList)
14c19 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73   ){.        pLis
14c1a 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e  t->pNext = pBt->
14c1b 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
14c1c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c     }.    if( SQL
14c1d 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 29  ITE_THREADSAFE )
14c1e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
14c1f 6d 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e  mutex_free(pBt->
14c20 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20  mutex);.    }.  
14c21 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20    removed = 1;. 
14c22 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
14c23 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72  ex_leave(pMaster
14c24 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f  );.  return remo
14c25 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74  ved;.#else.  ret
14c26 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a  urn 1;.#endif.}.
14c27 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
14c28 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20   pBt->pTmpSpace 
14c29 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c  points to an all
14c2a 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d  ocation of .** M
14c2b 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29  X_CELL_SIZE(pBt)
14c2c 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   bytes..*/.stati
14c2d 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54  c void allocateT
14c2e 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65  empSpace(BtShare
14c2f 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 21  d *pBt){.  if( !
14c30 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29  pBt->pTmpSpace )
14c31 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53  {.    pBt->pTmpS
14c32 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61  pace = sqlite3Pa
14c33 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70  geMalloc( pBt->p
14c34 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d  ageSize );.  }.}
14c35 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65  ../*.** Free the
14c36 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20   pBt->pTmpSpace 
14c37 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74  allocation.*/.st
14c38 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65  atic void freeTe
14c39 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64  mpSpace(BtShared
14c3a 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65   *pBt){.  sqlite
14c3b 33 50 61 67 65 46 72 65 65 28 20 70 42 74 2d 3e  3PageFree( pBt->
14c3c 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 70 42  pTmpSpace);.  pB
14c3d 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30  t->pTmpSpace = 0
14c3e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
14c3f 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73   an open databas
14c40 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65  e and invalidate
14c41 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f   all cursors..*/
14c42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14c43 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
14c44 43 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b  Close(Btree *p){
14c45 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
14c46 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43   = p->pBt;.  BtC
14c47 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20  ursor *pCur;..  
14c48 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72  /* Close all cur
14c49 73 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20  sors opened via 
14c4a 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f  this handle.  */
14c4b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
14c4c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
14c4d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
14c4e 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
14c4f 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62  er(p);.  pBt->db
14c50 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 70 43 75 72   = p->db;.  pCur
14c51 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b   = pBt->pCursor;
14c52 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 29  .  while( pCur )
14c53 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a  {.    BtCursor *
14c54 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20  pTmp = pCur;.   
14c55 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e   pCur = pCur->pN
14c56 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d  ext;.    if( pTm
14c57 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a  p->pBtree==p ){.
14c58 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
14c59 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54  eeCloseCursor(pT
14c5a 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  mp);.    }.  }..
14c5b 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e    /* Rollback an
14c5c 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63  y active transac
14c5d 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68  tion and free th
14c5e 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75  e handle structu
14c5f 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c  re..  ** The cal
14c60 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
14c61 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70  eRollback() drop
14c62 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b  s any table-lock
14c63 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74  s held by.  ** t
14c64 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f  his handle..  */
14c65 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  .  sqlite3BtreeR
14c66 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71  ollback(p);.  sq
14c67 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
14c68 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  p);..  /* If the
14c69 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68  re are still oth
14c6a 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72  er outstanding r
14c6b 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
14c6c 20 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20   shared-btree.  
14c6d 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65  ** structure, re
14c6e 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65  turn now. The re
14c6f 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20  mainder of this 
14c70 70 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73  procedure cleans
14c71 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68   .  ** up the sh
14c72 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f  ared-btree..  */
14c73 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61  .  assert( p->wa
14c74 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70  ntToLock==0 && p
14c75 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20  ->locked==0 );. 
14c76 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
14c77 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53  e || removeFromS
14c78 68 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20  haringList(pBt) 
14c79 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42  ){.    /* The pB
14c7a 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f  t is no longer o
14c7b 6e 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69  n the sharing li
14c7c 73 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63  st, so we can ac
14c7d 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77  cess.    ** it w
14c7e 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f  ithout having to
14c7f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e   hold the mutex.
14c80 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
14c81 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c  lean out and del
14c82 65 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64  ete the BtShared
14c83 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a   object..    */.
14c84 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74      assert( !pBt
14c85 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20  ->pCursor );.   
14c86 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
14c87 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  se(pBt->pPager);
14c88 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46  .    if( pBt->xF
14c89 72 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74  reeSchema && pBt
14c8a 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ->pSchema ){.   
14c8b 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68     pBt->xFreeSch
14c8c 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61  ema(pBt->pSchema
14c8d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
14c8e 69 74 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70  ite3_free(pBt->p
14c8f 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 66 72 65  Schema);.    fre
14c90 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
14c91 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
14c92 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66  e(pBt);.  }..#if
14c93 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14c94 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
14c95 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54  assert( p->wantT
14c96 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73  oLock==0 );.  as
14c97 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d  sert( p->locked=
14c98 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  =0 );.  if( p->p
14c99 50 72 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d  Prev ) p->pPrev-
14c9a 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78  >pNext = p->pNex
14c9b 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78  t;.  if( p->pNex
14c9c 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50  t ) p->pNext->pP
14c9d 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a  rev = p->pPrev;.
14c9e 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
14c9f 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74  3_free(p);.  ret
14ca0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
14ca1 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
14ca2 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20  he limit on the 
14ca3 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
14ca4 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65 20 63  allowed in the c
14ca5 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ache..**.** The 
14ca6 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
14ca7 66 20 63 61 63 68 65 20 70 61 67 65 73 20 69 73  f cache pages is
14ca8 20 73 65 74 20 74 6f 20 74 68 65 20 61 62 73 6f   set to the abso
14ca9 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66  lute.** value of
14caa 20 6d 78 50 61 67 65 2e 20 20 49 66 20 6d 78 50   mxPage.  If mxP
14cab 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c  age is negative,
14cac 20 74 68 65 20 70 61 67 65 72 20 77 69 6c 6c 0a   the pager will.
14cad 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 79 6e 63  ** operate async
14cae 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74 20 77  hronously - it w
14caf 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74 6f 20  ill not stop to 
14cb0 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a 20 74  do fsync()s.** t
14cb1 6f 20 69 6e 73 75 72 65 20 64 61 74 61 20 69 73  o insure data is
14cb2 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
14cb3 64 69 73 6b 20 73 75 72 66 61 63 65 20 62 65 66  disk surface bef
14cb4 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e  ore.** continuin
14cb5 67 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73  g.  Transactions
14cb6 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66 20 73   still work if s
14cb7 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66  ynchronous is of
14cb8 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  f,.** and the da
14cb9 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65  tabase cannot be
14cba 20 63 6f 72 72 75 70 74 65 64 20 69 66 20 74 68   corrupted if th
14cbb 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 63 72  is program.** cr
14cbc 61 73 68 65 73 2e 20 20 42 75 74 20 69 66 20 74  ashes.  But if t
14cbd 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
14cbe 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72 20 74  tem crashes or t
14cbf 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 61 62  here is.** an ab
14cc0 72 75 70 74 20 70 6f 77 65 72 20 66 61 69 6c 75  rupt power failu
14cc1 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72 6f 6e  re when synchron
14cc2 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68 65 20  ous is off, the 
14cc3 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 75 6c  database.** coul
14cc4 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20  d be left in an 
14cc5 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61 6e 64  inconsistent and
14cc6 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 73   unrecoverable s
14cc7 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f  tate..** Synchro
14cc8 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20 64 65  nous is on by de
14cc9 66 61 75 6c 74 20 73 6f 20 64 61 74 61 62 61 73  fault so databas
14cca 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20  e corruption is 
14ccb 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20  not.** normally 
14ccc 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49  a worry..*/.SQLI
14ccd 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
14cce 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
14ccf 63 68 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70  cheSize(Btree *p
14cd0 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
14cd1 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
14cd2 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72   p->pBt;.  asser
14cd3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14cd4 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
14cd5 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
14cd6 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
14cd7 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
14cd8 43 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e 70  Cachesize(pBt->p
14cd9 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a  Pager, mxPage);.
14cda 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
14cdb 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
14cdc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
14cdd 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
14cde 77 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63  way data is sync
14cdf 65 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72  ed to disk in or
14ce0 64 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20  der to increase 
14ce1 6f 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68  or decrease.** h
14ce2 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61  ow well the data
14ce3 62 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d  base resists dam
14ce4 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72  age due to OS cr
14ce5 61 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a  ashes and power.
14ce6 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65  ** failures.  Le
14ce7 76 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d  vel 1 is the sam
14ce8 65 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75  e as asynchronou
14ce9 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63  s (no syncs() oc
14cea 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65  cur and.** there
14ceb 20 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61   is a high proba
14cec 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65  bility of damage
14ced 29 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68  )  Level 2 is th
14cee 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72  e default.  Ther
14cef 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c  e.** is a very l
14cf0 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20  ow but non-zero 
14cf1 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64  probability of d
14cf2 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20  amage.  Level 3 
14cf3 72 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70  reduces the.** p
14cf4 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61  robability of da
14cf5 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72  mage to near zer
14cf6 6f 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69  o but with a wri
14cf7 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72  te performance r
14cf8 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66  eduction..*/.#if
14cf9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14cfa 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53  _PAGER_PRAGMAS.S
14cfb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14cfc 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
14cfd 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72  tSafetyLevel(Btr
14cfe 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 65 6c  ee *p, int level
14cff 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 29 7b  , int fullSync){
14d00 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
14d01 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73   = p->pBt;.  ass
14d02 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14d03 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
14d04 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
14d05 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
14d06 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  .  sqlite3PagerS
14d07 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42  etSafetyLevel(pB
14d08 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c  t->pPager, level
14d09 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 73  , fullSync);.  s
14d0a 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
14d0b 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (p);.  return SQ
14d0c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
14d0d 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
14d0e 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65  TRUE if the give
14d0f 6e 20 62 74 72 65 65 20 69 73 20 73 65 74 20 74  n btree is set t
14d10 6f 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31  o safety level 1
14d11 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77  .  In other.** w
14d12 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55  ords, return TRU
14d13 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f  E if no sync() o
14d14 63 63 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73  ccurs on the dis
14d15 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49  k files..*/.SQLI
14d16 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
14d17 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44  qlite3BtreeSyncD
14d18 69 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a 70  isabled(Btree *p
14d19 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
14d1a 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69  Bt = p->pBt;.  i
14d1b 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
14d1c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14d1d 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
14d1e 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33  ) );  .  sqlite3
14d1f 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
14d20 20 61 73 73 65 72 74 28 20 70 42 74 20 26 26 20   assert( pBt && 
14d21 70 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a 20  pBt->pPager );. 
14d22 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
14d23 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50  erNosync(pBt->pP
14d24 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  ager);.  sqlite3
14d25 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
14d26 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
14d27 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
14d28 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
14d29 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e  AGMAS) || !defin
14d2a 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
14d2b 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61  ACUUM)./*.** Cha
14d2c 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  nge the default 
14d2d 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74  pages size and t
14d2e 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73  he number of res
14d2f 65 72 76 65 64 20 62 79 74 65 73 20 70 65 72 20  erved bytes per 
14d30 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  page..**.** The 
14d31 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20 62  page size must b
14d32 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 62  e a power of 2 b
14d33 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36  etween 512 and 6
14d34 35 35 33 36 2e 20 20 49 66 20 74 68 65 20 70 61  5536.  If the pa
14d35 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c  ge.** size suppl
14d36 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65  ied does not mee
14d37 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  t this constrain
14d38 74 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20  t then the page 
14d39 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63  size is not.** c
14d3a 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61  hanged..**.** Pa
14d3b 67 65 20 73 69 7a 65 73 20 61 72 65 20 63 6f 6e  ge sizes are con
14d3c 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65 20 61  strained to be a
14d3d 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 73 6f   power of two so
14d3e 20 74 68 61 74 20 74 68 65 20 72 65 67 69 6f 6e   that the region
14d3f 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62  .** of the datab
14d40 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f  ase file used fo
14d41 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e  r locking (begin
14d42 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e 47 5f  ning at PENDING_
14d43 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72  BYTE,.** the fir
14d44 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65  st byte past the
14d45 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c 20 30   1GB boundary, 0
14d46 78 34 30 30 30 30 30 30 30 29 20 6e 65 65 64 73  x40000000) needs
14d47 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20   to occur.** at 
14d48 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
14d49 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49   a page..**.** I
14d4a 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52 65 73  f parameter nRes
14d4b 65 72 76 65 20 69 73 20 6c 65 73 73 20 74 68 61  erve is less tha
14d4c 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n zero, then the
14d4d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72   number of reser
14d4e 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 65 72  ved.** bytes per
14d4f 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e   page is left un
14d50 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49  changed..*/.SQLI
14d51 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
14d52 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61  qlite3BtreeSetPa
14d53 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c  geSize(Btree *p,
14d54 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20 69   int pageSize, i
14d55 6e 74 20 6e 52 65 73 65 72 76 65 29 7b 0a 20 20  nt nReserve){.  
14d56 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
14d57 4f 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  OK;.  BtShared *
14d58 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
14d59 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65  assert( nReserve
14d5a 3e 3d 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65  >=-1 && nReserve
14d5b 3c 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74  <=255 );.  sqlit
14d5c 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
14d5d 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65  .  if( pBt->page
14d5e 53 69 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20  SizeFixed ){.   
14d5f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
14d60 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72  ve(p);.    retur
14d61 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  n SQLITE_READONL
14d62 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65  Y;.  }.  if( nRe
14d63 73 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e  serve<0 ){.    n
14d64 52 65 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70  Reserve = pBt->p
14d65 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75  ageSize - pBt->u
14d66 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20  sableSize;.  }. 
14d67 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 76   assert( nReserv
14d68 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65  e>=0 && nReserve
14d69 3c 3d 32 35 35 20 29 3b 0a 20 20 69 66 28 20 70  <=255 );.  if( p
14d6a 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20  ageSize>=512 && 
14d6b 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45  pageSize<=SQLITE
14d6c 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26  _MAX_PAGE_SIZE &
14d6d 26 0a 20 20 20 20 20 20 20 20 28 28 70 61 67 65  &.        ((page
14d6e 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65  Size-1)&pageSize
14d6f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  )==0 ){.    asse
14d70 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20  rt( (pageSize & 
14d71 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  7)==0 );.    ass
14d72 65 72 74 28 20 21 70 42 74 2d 3e 70 50 61 67 65  ert( !pBt->pPage
14d73 31 20 26 26 20 21 70 42 74 2d 3e 70 43 75 72 73  1 && !pBt->pCurs
14d74 6f 72 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70  or );.    pBt->p
14d75 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70  ageSize = (u16)p
14d76 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65  ageSize;.    fre
14d77 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
14d78 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
14d79 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a  3PagerSetPagesiz
14d7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26  e(pBt->pPager, &
14d7b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  pBt->pageSize);.
14d7c 20 20 7d 0a 20 20 70 42 74 2d 3e 75 73 61 62 6c    }.  pBt->usabl
14d7d 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67  eSize = pBt->pag
14d7e 65 53 69 7a 65 20 2d 20 28 75 31 36 29 6e 52 65  eSize - (u16)nRe
14d7f 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 74 65 33  serve;.  sqlite3
14d80 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
14d81 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
14d82 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
14d83 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65  currently define
14d84 64 20 70 61 67 65 20 73 69 7a 65 0a 2a 2f 0a 53  d page size.*/.S
14d85 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14d86 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
14d87 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20  tPageSize(Btree 
14d88 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
14d89 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a  >pBt->pageSize;.
14d8a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
14d8b 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
14d8c 65 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65  eGetReserve(Btre
14d8d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a  e *p){.  int n;.
14d8e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
14d8f 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d  ter(p);.  n = p-
14d90 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d  >pBt->pageSize -
14d91 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53   p->pBt->usableS
14d92 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ize;.  sqlite3Bt
14d93 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
14d94 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
14d95 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75  * Set the maximu
14d96 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72  m page count for
14d97 20 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d   a database if m
14d98 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76  xPage is positiv
14d99 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73  e..** No changes
14d9a 20 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50   are made if mxP
14d9b 61 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61  age is 0 or nega
14d9c 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c  tive..** Regardl
14d9d 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ess of the value
14d9e 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75   of mxPage, retu
14d9f 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70  rn the maximum p
14da0 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51  age count..*/.SQ
14da1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
14da2 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78   sqlite3BtreeMax
14da3 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20  PageCount(Btree 
14da4 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b  *p, int mxPage){
14da5 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69  .  int n;.  sqli
14da6 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
14da7 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50  ;.  n = sqlite3P
14da8 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74  agerMaxPageCount
14da9 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c  (p->pBt->pPager,
14daa 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69   mxPage);.  sqli
14dab 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
14dac 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a  ;.  return n;.}.
14dad 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
14dae 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ed(SQLITE_OMIT_P
14daf 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c  AGER_PRAGMAS) ||
14db0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
14db1 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f  _OMIT_VACUUM) */
14db2 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
14db3 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27  he 'auto-vacuum'
14db4 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65   property of the
14db5 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68   database. If th
14db6 65 20 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a  e 'autoVacuum'.*
14db7 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  * parameter is n
14db8 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75  on-zero, then au
14db9 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69  to-vacuum mode i
14dba 73 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65  s enabled. If ze
14dbb 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73  ro, it.** is dis
14dbc 61 62 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75  abled. The defau
14dbd 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
14dbe 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f   auto-vacuum pro
14dbf 70 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74  perty is .** det
14dc0 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53  ermined by the S
14dc1 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
14dc2 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a  TOVACUUM macro..
14dc3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14dc4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
14dc5 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeSetAutoVacuum(
14dc6 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75  Btree *p, int au
14dc7 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65  toVacuum){.#ifde
14dc8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
14dc9 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72  TOVACUUM.  retur
14dca 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  n SQLITE_READONL
14dcb 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61  Y;.#else.  BtSha
14dcc 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
14dcd 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
14dce 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76  LITE_OK;.  u8 av
14dcf 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 20 3f 31   = autoVacuum ?1
14dd0 3a 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74  :0;..  sqlite3Bt
14dd1 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69  reeEnter(p);.  i
14dd2 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  f( pBt->pageSize
14dd3 46 69 78 65 64 20 26 26 20 61 76 21 3d 70 42 74  Fixed && av!=pBt
14dd4 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
14dd5 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
14dd6 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c 73  READONLY;.  }els
14dd7 65 7b 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f  e{.    pBt->auto
14dd8 56 61 63 75 75 6d 20 3d 20 61 76 3b 0a 20 20 7d  Vacuum = av;.  }
14dd9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
14dda 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
14ddb 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  n rc;.#endif.}..
14ddc 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
14ddd 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 27 61   value of the 'a
14dde 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70  uto-vacuum' prop
14ddf 65 72 74 79 2e 20 49 66 20 61 75 74 6f 2d 76 61  erty. If auto-va
14de0 63 75 75 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62  cuum is .** enab
14de1 6c 65 64 20 31 20 69 73 20 72 65 74 75 72 6e 65  led 1 is returne
14de2 64 2e 20 4f 74 68 65 72 77 69 73 65 20 30 2e 0a  d. Otherwise 0..
14de3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14de4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
14de5 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeGetAutoVacuum(
14de6 42 74 72 65 65 20 2a 70 29 7b 0a 23 69 66 64 65  Btree *p){.#ifde
14de7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
14de8 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72  TOVACUUM.  retur
14de9 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  n BTREE_AUTOVACU
14dea 55 4d 5f 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20  UM_NONE;.#else. 
14deb 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
14dec 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
14ded 0a 20 20 72 63 20 3d 20 28 0a 20 20 20 20 28 21  .  rc = (.    (!
14dee 70 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  p->pBt->autoVacu
14def 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41  um)?BTREE_AUTOVA
14df0 43 55 55 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28  CUUM_NONE:.    (
14df1 21 70 2d 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63  !p->pBt->incrVac
14df2 75 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56  uum)?BTREE_AUTOV
14df3 41 43 55 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20  ACUUM_FULL:.    
14df4 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
14df5 5f 49 4e 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c  _INCR.  );.  sql
14df6 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
14df7 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
14df8 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  #endif.}.../*.**
14df9 20 47 65 74 20 61 20 72 65 66 65 72 65 6e 63 65   Get a reference
14dfa 20 74 6f 20 70 50 61 67 65 31 20 6f 66 20 74 68   to pPage1 of th
14dfb 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
14dfc 20 20 54 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61    This will.** a
14dfd 6c 73 6f 20 61 63 71 75 69 72 65 20 61 20 72 65  lso acquire a re
14dfe 61 64 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66  adlock on that f
14dff 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  ile..**.** SQLIT
14e00 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
14e01 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66   on success.  If
14e02 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74   the file is not
14e03 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65   a.** well-forme
14e04 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  d database file,
14e05 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52   then SQLITE_COR
14e06 52 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64  RUPT is returned
14e07 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59  ..** SQLITE_BUSY
14e08 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
14e09 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
14e0a 6c 6f 63 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f  locked.  SQLITE_
14e0b 4e 4f 4d 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75  NOMEM.** is retu
14e0c 72 6e 65 64 20 69 66 20 77 65 20 72 75 6e 20 6f  rned if we run o
14e0d 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a  ut of memory. .*
14e0e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63  /.static int loc
14e0f 6b 42 74 72 65 65 28 42 74 53 68 61 72 65 64 20  kBtree(BtShared 
14e10 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b  *pBt){.  int rc;
14e11 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
14e12 65 31 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b  e1;.  int nPage;
14e13 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
14e14 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14e15 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
14e16 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20  if( pBt->pPage1 
14e17 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
14e18 4f 4b 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  OK;.  rc = sqlit
14e19 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
14e1a 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c 20  Bt, 1, &pPage1, 
14e1b 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  0);.  if( rc!=SQ
14e1c 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
14e1d 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f   rc;..  /* Do so
14e1e 6d 65 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 68  me checking to h
14e1f 65 6c 70 20 69 6e 73 75 72 65 20 74 68 65 20 66  elp insure the f
14e20 69 6c 65 20 77 65 20 6f 70 65 6e 65 64 20 72 65  ile we opened re
14e21 61 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61 20 76  ally is.  ** a v
14e22 61 6c 69 64 20 64 61 74 61 62 61 73 65 20 66 69  alid database fi
14e23 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  le. .  */.  rc =
14e24 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
14e25 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  ecount(pBt->pPag
14e26 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69  er, &nPage);.  i
14e27 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
14e28 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 61 67   ){.    goto pag
14e29 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a  e1_init_failed;.
14e2a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 50 61 67    }else if( nPag
14e2b 65 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 70  e>0 ){.    int p
14e2c 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 69 6e 74  ageSize;.    int
14e2d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20   usableSize;.   
14e2e 20 75 38 20 2a 70 61 67 65 31 20 3d 20 70 50 61   u8 *page1 = pPa
14e2f 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20  ge1->aData;.    
14e30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 41  rc = SQLITE_NOTA
14e31 44 42 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 63  DB;.    if( memc
14e32 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61 67 69 63  mp(page1, zMagic
14e33 48 65 61 64 65 72 2c 20 31 36 29 21 3d 30 20 29  Header, 16)!=0 )
14e34 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67  {.      goto pag
14e35 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a  e1_init_failed;.
14e36 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 61      }.    if( pa
14e37 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20  ge1[18]>1 ){.   
14e38 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79     pBt->readOnly
14e39 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
14e3a 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e 31 20  if( page1[19]>1 
14e3b 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61  ){.      goto pa
14e3c 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b  ge1_init_failed;
14e3d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
14e3e 68 65 20 6d 61 78 69 6d 75 6d 20 65 6d 62 65 64  he maximum embed
14e3f 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 73  ded fraction mus
14e40 74 20 62 65 20 65 78 61 63 74 6c 79 20 32 35 25  t be exactly 25%
14e41 2e 20 20 41 6e 64 20 74 68 65 20 6d 69 6e 69 6d  .  And the minim
14e42 75 6d 0a 20 20 20 20 2a 2a 20 65 6d 62 65 64 64  um.    ** embedd
14e43 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74  ed fraction must
14e44 20 62 65 20 31 32 2e 35 25 20 66 6f 72 20 62 6f   be 12.5% for bo
14e45 74 68 20 6c 65 61 66 2d 64 61 74 61 20 61 6e 64  th leaf-data and
14e46 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 2e 0a   non-leaf-data..
14e47 20 20 20 20 2a 2a 20 54 68 65 20 6f 72 69 67 69      ** The origi
14e48 6e 61 6c 20 64 65 73 69 67 6e 20 61 6c 6c 6f 77  nal design allow
14e49 65 64 20 74 68 65 73 65 20 61 6d 6f 75 6e 74 73  ed these amounts
14e4a 20 74 6f 20 76 61 72 79 2c 20 62 75 74 20 61 73   to vary, but as
14e4b 20 6f 66 0a 20 20 20 20 2a 2a 20 76 65 72 73 69   of.    ** versi
14e4c 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 20 72 65 71  on 3.6.0, we req
14e4d 75 69 72 65 20 74 68 65 6d 20 74 6f 20 62 65 20  uire them to be 
14e4e 66 69 78 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  fixed..    */.  
14e4f 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26 70 61    if( memcmp(&pa
14e50 67 65 31 5b 32 31 5d 2c 20 22 5c 31 30 30 5c 30  ge1[21], "\100\0
14e51 34 30 5c 30 34 30 22 2c 33 29 21 3d 30 20 29 7b  40\040",3)!=0 ){
14e52 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65  .      goto page
14e53 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20  1_init_failed;. 
14e54 20 20 20 7d 0a 20 20 20 20 70 61 67 65 53 69 7a     }.    pageSiz
14e55 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 70 61  e = get2byte(&pa
14e56 67 65 31 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66  ge1[16]);.    if
14e57 28 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29 26  ( ((pageSize-1)&
14e58 70 61 67 65 53 69 7a 65 29 21 3d 30 20 7c 7c 20  pageSize)!=0 || 
14e59 70 61 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c 0a  pageSize<512 ||.
14e5a 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
14e5b 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 3c 33 32  MAX_PAGE_SIZE<32
14e5c 37 36 38 20 26 26 20 70 61 67 65 53 69 7a 65 3e  768 && pageSize>
14e5d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
14e5e 53 49 5a 45 29 0a 20 20 20 20 29 7b 0a 20 20 20  SIZE).    ){.   
14e5f 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e     goto page1_in
14e60 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d  it_failed;.    }
14e61 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 61  .    assert( (pa
14e62 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29  geSize & 7)==0 )
14e63 3b 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65  ;.    usableSize
14e64 20 3d 20 70 61 67 65 53 69 7a 65 20 2d 20 70 61   = pageSize - pa
14e65 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69 66 28  ge1[20];.    if(
14e66 20 70 61 67 65 53 69 7a 65 21 3d 70 42 74 2d 3e   pageSize!=pBt->
14e67 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  pageSize ){.    
14e68 20 20 2f 2a 20 41 66 74 65 72 20 72 65 61 64 69    /* After readi
14e69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67  ng the first pag
14e6a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
14e6b 65 20 61 73 73 75 6d 69 6e 67 20 61 20 70 61 67  e assuming a pag
14e6c 65 20 73 69 7a 65 0a 20 20 20 20 20 20 2a 2a 20  e size.      ** 
14e6d 6f 66 20 42 74 53 68 61 72 65 64 2e 70 61 67 65  of BtShared.page
14e6e 53 69 7a 65 2c 20 77 65 20 68 61 76 65 20 64 69  Size, we have di
14e6f 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 74 68  scovered that th
14e70 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 0a 20  e page-size is. 
14e71 20 20 20 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79       ** actually
14e72 20 70 61 67 65 53 69 7a 65 2e 20 55 6e 6c 6f 63   pageSize. Unloc
14e73 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  k the database, 
14e74 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 67 65  leave pBt->pPage
14e75 31 20 61 74 0a 20 20 20 20 20 20 2a 2a 20 7a 65  1 at.      ** ze
14e76 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51  ro and return SQ
14e77 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 63 61 6c  LITE_OK. The cal
14e78 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20 74 68  ler will call th
14e79 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20  is function.    
14e7a 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74 68 20    ** again with 
14e7b 74 68 65 20 63 6f 72 72 65 63 74 20 70 61 67 65  the correct page
14e7c 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  -size..      */.
14e7d 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
14e7e 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20 20  e(pPage1);.     
14e7f 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
14e80 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 69   = (u16)usableSi
14e81 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70  ze;.      pBt->p
14e82 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70  ageSize = (u16)p
14e83 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 66  ageSize;.      f
14e84 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 74  reeTempSpace(pBt
14e85 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
14e86 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
14e87 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70  (pBt->pPager, &p
14e88 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  Bt->pageSize);. 
14e89 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
14e8a 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  TE_OK;.    }.   
14e8b 20 69 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c   if( usableSize<
14e8c 35 30 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  500 ){.      got
14e8d 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69  o page1_init_fai
14e8e 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  led;.    }.    p
14e8f 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28  Bt->pageSize = (
14e90 75 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20  u16)pageSize;.  
14e91 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a    pBt->usableSiz
14e92 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53  e = (u16)usableS
14e93 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ize;.#ifndef SQL
14e94 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
14e95 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74  UUM.    pBt->aut
14e96 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62  oVacuum = (get4b
14e97 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20  yte(&page1[36 + 
14e98 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20  4*4])?1:0);.    
14e99 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20  pBt->incrVacuum 
14e9a 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61 67  = (get4byte(&pag
14e9b 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a  e1[36 + 7*4])?1:
14e9c 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a  0);.#endif.  }..
14e9d 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73    /* maxLocal is
14e9e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f   the maximum amo
14e9f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74  unt of payload t
14ea0 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20  o store locally 
14ea1 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e  for.  ** a cell.
14ea2 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69    Make sure it i
14ea3 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73  s small enough s
14ea4 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 20  o that at least 
14ea5 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63  minFanout.  ** c
14ea6 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69  ells can will fi
14ea7 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20  t on one page.  
14ea8 57 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62  We assume a 10-b
14ea9 79 74 65 20 70 61 67 65 20 68 65 61 64 65 72 2e  yte page header.
14eaa 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68  .  ** Besides th
14eab 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63  e payload, the c
14eac 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a  ell must store:.
14ead 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20    **     2-byte 
14eae 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  pointer to the c
14eaf 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62  ell.  **     4-b
14eb0 79 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65  yte child pointe
14eb1 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74  r.  **     9-byt
14eb2 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a  e nKey value.  *
14eb3 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61  *     4-byte nDa
14eb4 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20  ta value.  **   
14eb5 20 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f    4-byte overflo
14eb6 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20  w page pointer. 
14eb7 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f   ** So a cell co
14eb8 6e 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79  nsists of a 2-by
14eb9 74 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 61  te poiner, a hea
14eba 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73 20  der which is as 
14ebb 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20  much as.  ** 17 
14ebc 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f  bytes long, 0 to
14ebd 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c   N bytes of payl
14ebe 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69  oad, and an opti
14ebf 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72  onal 4 byte over
14ec0 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70  flow.  ** page p
14ec1 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70  ointer..  */.  p
14ec2 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28  Bt->maxLocal = (
14ec3 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d  pBt->usableSize-
14ec4 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b  12)*64/255 - 23;
14ec5 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c  .  pBt->minLocal
14ec6 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53   = (pBt->usableS
14ec7 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d  ize-12)*32/255 -
14ec8 20 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c   23;.  pBt->maxL
14ec9 65 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c  eaf = pBt->usabl
14eca 65 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42  eSize - 35;.  pB
14ecb 74 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42  t->minLeaf = (pB
14ecc 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32  t->usableSize-12
14ecd 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20  )*32/255 - 23;. 
14ece 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61   assert( pBt->ma
14ecf 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58  xLeaf + 23 <= MX
14ed0 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20  _CELL_SIZE(pBt) 
14ed1 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31  );.  pBt->pPage1
14ed2 20 3d 20 70 50 61 67 65 31 3b 0a 20 20 72 65 74   = pPage1;.  ret
14ed3 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
14ed4 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
14ed5 64 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  d:.  releasePage
14ed6 28 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d  (pPage1);.  pBt-
14ed7 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72  >pPage1 = 0;.  r
14ed8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
14ed9 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
14eda 77 6f 72 6b 73 20 6c 69 6b 65 20 6c 6f 63 6b 42  works like lockB
14edb 74 72 65 65 28 29 20 65 78 63 65 70 74 20 74 68  tree() except th
14edc 61 74 20 69 74 20 61 6c 73 6f 20 69 6e 76 6f 6b  at it also invok
14edd 65 73 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63  es the.** busy c
14ede 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72 65  allback if there
14edf 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74   is lock content
14ee0 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
14ee1 6e 74 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68  nt lockBtreeWith
14ee2 52 65 74 72 79 28 42 74 72 65 65 20 2a 70 52 65  Retry(Btree *pRe
14ee3 66 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  f){.  int rc = S
14ee4 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73  QLITE_OK;..  ass
14ee5 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
14ee6 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 52 65 66  eHoldsMutex(pRef
14ee7 29 20 29 3b 0a 20 20 69 66 28 20 70 52 65 66 2d  ) );.  if( pRef-
14ee8 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
14ee9 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 75 38 20 69  NONE ){.    u8 i
14eea 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70  nTransaction = p
14eeb 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e  Ref->pBt->inTran
14eec 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20 62 74 72  saction;.    btr
14eed 65 65 49 6e 74 65 67 72 69 74 79 28 70 52 65 66  eeIntegrity(pRef
14eee 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
14eef 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61  te3BtreeBeginTra
14ef0 6e 73 28 70 52 65 66 2c 20 30 29 3b 0a 20 20 20  ns(pRef, 0);.   
14ef1 20 70 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72   pRef->pBt->inTr
14ef2 61 6e 73 61 63 74 69 6f 6e 20 3d 20 69 6e 54 72  ansaction = inTr
14ef3 61 6e 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20 70  ansaction;.    p
14ef4 52 65 66 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54  Ref->inTrans = T
14ef5 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 69  RANS_NONE;.    i
14ef6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
14ef7 20 29 7b 0a 20 20 20 20 20 20 70 52 65 66 2d 3e   ){.      pRef->
14ef8 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f  pBt->nTransactio
14ef9 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62  n--;.    }.    b
14efa 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 52  treeIntegrity(pR
14efb 65 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ef);.  }.  retur
14efc 6e 20 72 63 3b 0a 7d 0a 20 20 20 20 20 20 20 0a  n rc;.}.       .
14efd 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  ./*.** If there 
14efe 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69  are no outstandi
14eff 6e 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 77  ng cursors and w
14f00 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65  e are not in the
14f01 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20   middle.** of a 
14f02 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 20  transaction but 
14f03 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64 20  there is a read 
14f04 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
14f05 62 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  base, then.** th
14f06 69 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 66  is routine unref
14f07 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  s the first page
14f08 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
14f09 20 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20   file which .** 
14f0a 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f  has the effect o
14f0b 66 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20  f releasing the 
14f0c 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a  read lock..**.**
14f0d 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   If there are an
14f0e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75  y outstanding cu
14f0f 72 73 6f 72 73 2c 20 74 68 69 73 20 72 6f 75 74  rsors, this rout
14f10 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
14f11 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  **.** If there i
14f12 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  s a transaction 
14f13 69 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 69  in progress, thi
14f14 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
14f15 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
14f16 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65  void unlockBtree
14f17 49 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 65  IfUnused(BtShare
14f18 64 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 72  d *pBt){.  asser
14f19 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14f1a 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
14f1b 29 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e  ) );.  if( pBt->
14f1c 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
14f1d 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74  RANS_NONE && pBt
14f1e 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 26 26 20  ->pCursor==0 && 
14f1f 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29  pBt->pPage1!=0 )
14f20 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
14f21 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
14f22 42 74 2d 3e 70 50 61 67 65 72 29 3e 3d 31 20 29  Bt->pPager)>=1 )
14f23 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
14f24 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
14f25 74 61 20 29 3b 0a 23 69 66 20 30 0a 20 20 20 20  ta );.#if 0.    
14f26 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65    if( pBt->pPage
14f27 31 2d 3e 61 44 61 74 61 3d 3d 30 20 29 7b 0a 20  1->aData==0 ){. 
14f28 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a         MemPage *
14f29 70 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 50 61  pPage = pBt->pPa
14f2a 67 65 31 3b 0a 20 20 20 20 20 20 20 20 70 50 61  ge1;.        pPa
14f2b 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69  ge->aData = sqli
14f2c 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
14f2d 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
14f2e 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e  .        pPage->
14f2f 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 20  pBt = pBt;.     
14f30 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d     pPage->pgno =
14f31 20 31 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64   1;.      }.#end
14f32 69 66 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  if.      release
14f33 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 31  Page(pBt->pPage1
14f34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74  );.    }.    pBt
14f35 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20  ->pPage1 = 0;.  
14f36 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20    pBt->inStmt = 
14f37 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
14f38 43 72 65 61 74 65 20 61 20 6e 65 77 20 64 61 74  Create a new dat
14f39 61 62 61 73 65 20 62 79 20 69 6e 69 74 69 61 6c  abase by initial
14f3a 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73 74 20  izing the first 
14f3b 70 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66  page of the.** f
14f3c 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
14f3d 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28 42  nt newDatabase(B
14f3e 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
14f3f 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20   MemPage *pP1;. 
14f40 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
14f41 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  data;.  int rc;.
14f42 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20    int nPage;..  
14f43 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14f44 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
14f45 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
14f46 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
14f47 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  ecount(pBt->pPag
14f48 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69  er, &nPage);.  i
14f49 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
14f4a 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20   || nPage>0 ){. 
14f4b 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
14f4c 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70  }.  pP1 = pBt->p
14f4d 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74 28  Page1;.  assert(
14f4e 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 74   pP1!=0 );.  dat
14f4f 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a  a = pP1->aData;.
14f50 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
14f51 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44  gerWrite(pP1->pD
14f52 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  bPage);.  if( rc
14f53 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
14f54 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61  memcpy(data, zMa
14f55 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f  gicHeader, sizeo
14f56 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29  f(zMagicHeader))
14f57 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
14f58 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29  of(zMagicHeader)
14f59 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62 79  ==16 );.  put2by
14f5a 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 42  te(&data[16], pB
14f5b 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  t->pageSize);.  
14f5c 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20  data[18] = 1;.  
14f5d 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20  data[19] = 1;.  
14f5e 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61  assert( pBt->usa
14f5f 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 61  bleSize<=pBt->pa
14f60 67 65 53 69 7a 65 20 26 26 20 70 42 74 2d 3e 75  geSize && pBt->u
14f61 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 3e 3d 70  sableSize+255>=p
14f62 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  Bt->pageSize);. 
14f63 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 75 38 29   data[20] = (u8)
14f64 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d  (pBt->pageSize -
14f65 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
14f66 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 3d 20  );.  data[21] = 
14f67 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d 20 3d  64;.  data[22] =
14f68 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 5d 20   32;.  data[23] 
14f69 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 28 26  = 32;.  memset(&
14f6a 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30  data[24], 0, 100
14f6b 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65  -24);.  zeroPage
14f6c 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59  (pP1, PTF_INTKEY
14f6d 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45  |PTF_LEAF|PTF_LE
14f6e 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 74 2d  AFDATA );.  pBt-
14f6f 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d  >pageSizeFixed =
14f70 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   1;.#ifndef SQLI
14f71 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
14f72 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  UM.  assert( pBt
14f73 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20  ->autoVacuum==1 
14f74 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  || pBt->autoVacu
14f75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  um==0 );.  asser
14f76 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  t( pBt->incrVacu
14f77 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e  um==1 || pBt->in
14f78 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20  crVacuum==0 );. 
14f79 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b   put4byte(&data[
14f7a 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 2d 3e  36 + 4*4], pBt->
14f7b 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 70  autoVacuum);.  p
14f7c 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 36  ut4byte(&data[36
14f7d 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e   + 7*4], pBt->in
14f7e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e 64 69  crVacuum);.#endi
14f7f 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  f.  return SQLIT
14f80 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
14f81 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 20  ttempt to start 
14f82 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f  a new transactio
14f83 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73  n. A write-trans
14f84 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61  action.** is sta
14f85 72 74 65 64 20 69 66 20 74 68 65 20 73 65 63 6f  rted if the seco
14f86 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  nd argument is n
14f87 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73  onzero, otherwis
14f88 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61  e a read-.** tra
14f89 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68  nsaction.  If th
14f8a 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
14f8b 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61  t is 2 or more a
14f8c 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20  nd exclusive.** 
14f8d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
14f8e 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20  tarted, meaning 
14f8f 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72  that no other pr
14f90 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64  ocess is allowed
14f91 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68  .** to access th
14f92 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 70  e database.  A p
14f93 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73  reexisting trans
14f94 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62  action may not b
14f95 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 6f  e.** upgraded to
14f96 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 61   exclusive by ca
14f97 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  lling this routi
14f98 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65  ne a second time
14f99 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73   - the.** exclus
14f9a 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20  ivity flag only 
14f9b 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 20  works for a new 
14f9c 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
14f9d 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73  ** A write-trans
14f9e 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73  action must be s
14f9f 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 74  tarted before at
14fa0 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a  tempting any .**
14fa1 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20   changes to the 
14fa2 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20  database.  None 
14fa3 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
14fa4 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69   routines .** wi
14fa5 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61  ll work unless a
14fa6 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
14fa7 73 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a 2a  started first:.*
14fa8 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  *.**      sqlite
14fa9 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c  3BtreeCreateTabl
14faa 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
14fab 74 65 33 42 74 72 65 65 43 72 65 61 74 65 49 6e  te3BtreeCreateIn
14fac 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 71  dex().**      sq
14fad 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54  lite3BtreeClearT
14fae 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73  able().**      s
14faf 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54  qlite3BtreeDropT
14fb0 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73  able().**      s
14fb1 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72  qlite3BtreeInser
14fb2 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  t().**      sqli
14fb3 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 29  te3BtreeDelete()
14fb4 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
14fb5 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28  BtreeUpdateMeta(
14fb6 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e  ).**.** If an in
14fb7 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f  itial attempt to
14fb8 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63   acquire the loc
14fb9 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 20  k fails because 
14fba 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69  of lock contenti
14fbb 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  on.** and the da
14fbc 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76 69  tabase was previ
14fbd 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20  ously unlocked, 
14fbe 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20  then invoke the 
14fbf 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20  busy handler.** 
14fc0 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e  if there is one.
14fc1 20 20 42 75 74 20 69 66 20 74 68 65 72 65 20 77    But if there w
14fc2 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 20  as previously a 
14fc3 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f  read-lock, do no
14fc4 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20  t.** invoke the 
14fc5 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a  busy handler - j
14fc6 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ust return SQLIT
14fc7 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 5f  E_BUSY.  SQLITE_
14fc8 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 75  BUSY is .** retu
14fc9 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 20  rned when there 
14fca 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 61  is already a rea
14fcb 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20  d-lock in order 
14fcc 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 6c  to avoid a deadl
14fcd 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f  ock..**.** Suppo
14fce 73 65 20 74 68 65 72 65 20 61 72 65 20 74 77 6f  se there are two
14fcf 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e 64   processes A and
14fd0 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 61   B.  A has a rea
14fd1 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 73  d lock and B has
14fd2 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 6c  .** a reserved l
14fd3 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 6f  ock.  B tries to
14fd4 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c   promote to excl
14fd5 75 73 69 76 65 20 62 75 74 20 69 73 20 62 6c 6f  usive but is blo
14fd6 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20  cked because.** 
14fd7 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 6b  of A's read lock
14fd8 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 72  .  A tries to pr
14fd9 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 65  omote to reserve
14fda 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64  d but is blocked
14fdb 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72   by B..** One or
14fdc 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68   the other of th
14fdd 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20  e two processes 
14fde 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 6f 72  must give way or
14fdf 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a   there can be.**
14fe0 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 42   no progress.  B
14fe1 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49  y returning SQLI
14fe2 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20  TE_BUSY and not 
14fe3 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73  invoking the bus
14fe4 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68  y callback.** wh
14fe5 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 73  en A already has
14fe6 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65   a read lock, we
14fe7 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f 20   encourage A to 
14fe8 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 20  give up and let 
14fe9 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f  B.** proceed..*/
14fea 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14feb 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
14fec 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65  BeginTrans(Btree
14fed 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 29   *p, int wrflag)
14fee 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
14fef 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e  t = p->pBt;.  in
14ff0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
14ff1 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
14ff2 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74  eEnter(p);.  pBt
14ff3 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
14ff4 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
14ff5 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
14ff6 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 79  btree is already
14ff7 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e   in a write-tran
14ff8 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20  saction, or it. 
14ff9 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69   ** is already i
14ffa 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63  n a read-transac
14ffb 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d  tion and a read-
14ffc 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a  transaction.  **
14ffd 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74   is requested, t
14ffe 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  his is a no-op..
14fff 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e    */.  if( p->in
15000 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
15001 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e  TE || (p->inTran
15002 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26  s==TRANS_READ &&
15003 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20   !wrflag) ){.   
15004 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75   goto trans_begu
15005 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69  n;.  }..  /* Wri
15006 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  te transactions 
15007 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65  are not possible
15008 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20   on a read-only 
15009 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66  database */.  if
1500a 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  ( pBt->readOnly 
1500b 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20  && wrflag ){.   
1500c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   rc = SQLITE_REA
1500d 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20  DONLY;.    goto 
1500e 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d  trans_begun;.  }
1500f 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65  ..  /* If anothe
15010 72 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  r database handl
15011 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f 70  e has already op
15012 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 61  ened a write tra
15013 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f  nsaction .  ** o
15014 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74  n this shared-bt
15015 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e  ree structure an
15016 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 65  d a second write
15017 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a   transaction is.
15018 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c 20    ** requested, 
15019 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
1501a 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  SY..  */.  if( p
1501b 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
1501c 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26  n==TRANS_WRITE &
1501d 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20  & wrflag ){.    
1501e 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
1501f 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73  ;.    goto trans
15020 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 69 66  _begun;.  }..#if
15021 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15022 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
15023 69 66 28 20 77 72 66 6c 61 67 3e 31 20 29 7b 0a  if( wrflag>1 ){.
15024 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65      BtLock *pIte
15025 72 3b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72  r;.    for(pIter
15026 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74  =pBt->pLock; pIt
15027 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d  er; pIter=pIter-
15028 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69  >pNext){.      i
15029 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65  f( pIter->pBtree
1502a 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 72  !=p ){.        r
1502b 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
1502c 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 74 72  .        goto tr
1502d 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 20 20 20  ans_begun;.     
1502e 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e   }.    }.  }.#en
1502f 64 69 66 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20  dif..  do {.    
15030 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d  if( pBt->pPage1=
15031 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 7b 0a  =0 ){.      do{.
15032 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63          rc = loc
15033 6b 42 74 72 65 65 28 70 42 74 29 3b 0a 20 20 20  kBtree(pBt);.   
15034 20 20 20 7d 77 68 69 6c 65 28 20 70 42 74 2d 3e     }while( pBt->
15035 70 50 61 67 65 31 3d 3d 30 20 26 26 20 72 63 3d  pPage1==0 && rc=
15036 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
15037 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d    }..    if( rc=
15038 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72  =SQLITE_OK && wr
15039 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 69 66  flag ){.      if
1503a 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  ( pBt->readOnly 
1503b 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
1503c 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
1503d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1503e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1503f 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 42 74  e3PagerBegin(pBt
15040 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67  ->pPage1->pDbPag
15041 65 2c 20 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20  e, wrflag>1);.  
15042 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
15043 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
15044 20 20 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74       rc = newDat
15045 61 62 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20  abase(pBt);.    
15046 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
15047 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72    }.  .    if( r
15048 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
15049 20 20 20 20 20 20 69 66 28 20 77 72 66 6c 61 67        if( wrflag
1504a 20 29 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d   ) pBt->inStmt =
1504b 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
1504c 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65       unlockBtree
1504d 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20  IfUnused(pBt);. 
1504e 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 72     }.  }while( r
1504f 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26  c==SQLITE_BUSY &
15050 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  & pBt->inTransac
15051 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45  tion==TRANS_NONE
15052 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 62 74   &&.          bt
15053 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e  reeInvokeBusyHan
15054 64 6c 65 72 28 70 42 74 29 20 29 3b 0a 0a 20 20  dler(pBt) );..  
15055 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
15056 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  K ){.    if( p->
15057 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e  inTrans==TRANS_N
15058 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 70 42 74  ONE ){.      pBt
15059 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2b 2b  ->nTransaction++
1505a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69  ;.    }.    p->i
1505b 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 67  nTrans = (wrflag
1505c 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 41  ?TRANS_WRITE:TRA
1505d 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 66  NS_READ);.    if
1505e 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74  ( p->inTrans>pBt
1505f 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20  ->inTransaction 
15060 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e  ){.      pBt->in
15061 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d  Transaction = p-
15062 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a  >inTrans;.    }.
15063 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
15064 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
15065 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61 67 3e  .    if( wrflag>
15066 31 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  1 ){.      asser
15067 74 28 20 21 70 42 74 2d 3e 70 45 78 63 6c 75 73  t( !pBt->pExclus
15068 69 76 65 20 29 3b 0a 20 20 20 20 20 20 70 42 74  ive );.      pBt
15069 2d 3e 70 45 78 63 6c 75 73 69 76 65 20 3d 20 70  ->pExclusive = p
1506a 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
1506b 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e   }...trans_begun
1506c 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  :.  if( rc==SQLI
1506d 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20  TE_OK && wrflag 
1506e 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63  ){.    /* This c
1506f 61 6c 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74  all makes sure t
15070 68 61 74 20 74 68 65 20 70 61 67 65 72 20 68 61  hat the pager ha
15071 73 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75  s the correct nu
15072 6d 62 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f  mber of.    ** o
15073 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20  pen savepoints. 
15074 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  If the second pa
15075 72 61 6d 65 74 65 72 20 69 73 20 67 72 65 61 74  rameter is great
15076 65 72 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 20  er than 0 and.  
15077 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75    ** the sub-jou
15078 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65  rnal is not alre
15079 61 64 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69  ady open, then i
1507a 74 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64  t will be opened
1507b 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   here..    */.  
1507c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1507d 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74  gerOpenSavepoint
1507e 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d  (pBt->pPager, p-
1507f 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29  >db->nSavepoint)
15080 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e  ;.  }..  btreeIn
15081 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71  tegrity(p);.  sq
15082 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
15083 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
15084 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
15085 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
15086 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  UM../*.** Set th
15087 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e  e pointer-map en
15088 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68  tries for all ch
15089 69 6c 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70  ildren of page p
1508a 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a  Page. Also, if.*
1508b 2a 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73  * pPage contains
1508c 20 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e   cells that poin
1508d 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61  t to overflow pa
1508e 67 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69  ges, set the poi
1508f 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72  nter.** map entr
15090 69 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72  ies for the over
15091 66 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77 65  flow pages as we
15092 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ll..*/.static in
15093 74 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70  t setChildPtrmap
15094 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  s(MemPage *pPage
15095 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
15096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15097 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74          /* Count
15098 65 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  er variable */. 
15099 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20   int nCell;     
1509a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1509b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1509c 20 63 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70   cells in page p
1509d 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  Page */.  int rc
1509e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1509f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
150a0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
150a1 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
150a2 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75   pPage->pBt;.  u
150a3 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70  8 isInitOrig = p
150a4 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20  Page->isInit;.  
150a5 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67  Pgno pgno = pPag
150a6 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65  e->pgno;..  asse
150a7 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
150a8 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
150a9 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72  t->mutex) );.  r
150aa 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
150ab 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b  InitPage(pPage);
150ac 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
150ad 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
150ae 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61   set_child_ptrma
150af 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e 43  ps_out;.  }.  nC
150b0 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65  ell = pPage->nCe
150b1 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ll;..  for(i=0; 
150b2 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20  i<nCell; i++){. 
150b3 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66     u8 *pCell = f
150b4 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  indCell(pPage, i
150b5 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72  );..    rc = ptr
150b6 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50  mapPutOvflPtr(pP
150b7 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  age, pCell);.   
150b8 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
150b9 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  OK ){.      goto
150ba 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61   set_child_ptrma
150bb 70 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20  ps_out;.    }.. 
150bc 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
150bd 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e  eaf ){.      Pgn
150be 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65  o childPgno = ge
150bf 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20  t4byte(pCell);. 
150c0 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70       rc = ptrmap
150c1 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67  Put(pBt, childPg
150c2 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45  no, PTRMAP_BTREE
150c3 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69  , pgno);.      i
150c4 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
150c5 20 29 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c   ) goto set_chil
150c6 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20  d_ptrmaps_out;. 
150c7 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
150c8 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
150c9 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67      Pgno childPg
150ca 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
150cb 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
150cc 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
150cd 3b 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61  ;.    rc = ptrma
150ce 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50  pPut(pBt, childP
150cf 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
150d0 45 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a 73  E, pgno);.  }..s
150d1 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73  et_child_ptrmaps
150d2 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69  _out:.  pPage->i
150d3 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72  sInit = isInitOr
150d4 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ig;.  return rc;
150d5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68  .}../*.** Somewh
150d6 65 72 65 20 6f 6e 20 70 50 61 67 65 2c 20 77 68  ere on pPage, wh
150d7 69 63 68 20 69 73 20 67 75 61 72 65 6e 74 65 65  ich is guarentee
150d8 64 20 74 6f 20 62 65 20 61 20 62 74 72 65 65 20  d to be a btree 
150d9 70 61 67 65 2c 20 6e 6f 74 20 61 6e 20 6f 76 65  page, not an ove
150da 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 69  rflow.** page, i
150db 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70  s a pointer to p
150dc 61 67 65 20 69 46 72 6f 6d 2e 20 4d 6f 64 69 66  age iFrom. Modif
150dd 79 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73  y this pointer s
150de 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73  o that it points
150df 20 74 6f 0a 2a 2a 20 69 54 6f 2e 20 50 61 72 61   to.** iTo. Para
150e0 6d 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63  meter eType desc
150e1 72 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f  ribes the type o
150e2 66 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20  f pointer to be 
150e3 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 0a 2a 2a  modified, as .**
150e4 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
150e5 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20  PTRMAP_BTREE:   
150e6 20 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72    pPage is a btr
150e7 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69  ee-page. The poi
150e8 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61  nter points at a
150e9 20 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20   child .**      
150ea 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
150eb 65 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a  e of pPage..**.*
150ec 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  * PTRMAP_OVERFLO
150ed 57 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62  W1: pPage is a b
150ee 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70  tree-page. The p
150ef 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74  ointer points at
150f0 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20   an overflow.** 
150f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
150f2 20 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74    page pointed t
150f3 6f 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20  o by one of the 
150f4 63 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a  cells on pPage..
150f5 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45  **.** PTRMAP_OVE
150f6 52 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73  RFLOW2: pPage is
150f7 20 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67   an overflow-pag
150f8 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70  e. The pointer p
150f9 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78  oints at the nex
150fa 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
150fb 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20         overflow 
150fc 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74  page in the list
150fd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
150fe 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65  modifyPagePointe
150ff 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  r(MemPage *pPage
15100 2c 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67  , Pgno iFrom, Pg
15101 6e 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65  no iTo, u8 eType
15102 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
15103 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15104 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
15105 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
15106 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
15107 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
15108 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28  DbPage) );.  if(
15109 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f   eType==PTRMAP_O
1510a 56 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20  VERFLOW2 ){.    
1510b 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69  /* The pointer i
1510c 73 20 61 6c 77 61 79 73 20 74 68 65 20 66 69 72  s always the fir
1510d 73 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68  st 4 bytes of th
1510e 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63  e page in this c
1510f 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28  ase.  */.    if(
15110 20 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d   get4byte(pPage-
15111 3e 61 44 61 74 61 29 21 3d 69 46 72 6f 6d 20 29  >aData)!=iFrom )
15112 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
15113 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
15114 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75  PT;.    }.    pu
15115 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44  t4byte(pPage->aD
15116 61 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c  ata, iTo);.  }el
15117 73 65 7b 0a 20 20 20 20 75 38 20 69 73 49 6e 69  se{.    u8 isIni
15118 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69  tOrig = pPage->i
15119 73 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 20 69  sInit;.    int i
1511a 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b  ;.    int nCell;
1511b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ..    sqlite3Btr
1511c 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65  eeInitPage(pPage
1511d 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70  );.    nCell = p
1511e 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20  Page->nCell;..  
1511f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65    for(i=0; i<nCe
15120 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ll; i++){.      
15121 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64  u8 *pCell = find
15122 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a  Cell(pPage, i);.
15123 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d        if( eType=
15124 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  =PTRMAP_OVERFLOW
15125 31 20 29 7b 0a 20 20 20 20 20 20 20 20 43 65 6c  1 ){.        Cel
15126 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20  lInfo info;.    
15127 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
15128 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61  ParseCellPtr(pPa
15129 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f  ge, pCell, &info
1512a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
1512b 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b  nfo.iOverflow ){
1512c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69  .          if( i
1512d 46 72 6f 6d 3d 3d 67 65 74 34 62 79 74 65 28 26  From==get4byte(&
1512e 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72  pCell[info.iOver
1512f 66 6c 6f 77 5d 29 20 29 7b 0a 20 20 20 20 20 20  flow]) ){.      
15130 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
15131 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72  pCell[info.iOver
15132 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20  flow], iTo);.   
15133 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
15134 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
15135 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
15136 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 67  e{.        if( g
15137 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d  et4byte(pCell)==
15138 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20  iFrom ){.       
15139 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c     put4byte(pCel
1513a 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20  l, iTo);.       
1513b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1513c 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1513d 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69 3d 3d  }.  .    if( i==
1513e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69  nCell ){.      i
1513f 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50  f( eType!=PTRMAP
15140 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 20 20  _BTREE || .     
15141 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26 70       get4byte(&p
15142 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
15143 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
15144 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  !=iFrom ){.     
15145 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15146 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
15147 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 75 74       }.      put
15148 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  4byte(&pPage->aD
15149 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
1514a 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20  fset+8], iTo);. 
1514b 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d     }..    pPage-
1514c 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74  >isInit = isInit
1514d 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Orig;.  }.  retu
1514e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1514f 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  ../*.** Move the
15150 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 70   open database p
15151 61 67 65 20 70 44 62 50 61 67 65 20 74 6f 20 6c  age pDbPage to l
15152 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 50 61 67  ocation iFreePag
15153 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74  e in the .** dat
15154 61 62 61 73 65 2e 20 54 68 65 20 70 44 62 50 61  abase. The pDbPa
15155 67 65 20 72 65 66 65 72 65 6e 63 65 20 72 65 6d  ge reference rem
15156 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2f 0a 73  ains valid..*/.s
15157 74 61 74 69 63 20 69 6e 74 20 72 65 6c 6f 63 61  tatic int reloca
15158 74 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72  tePage(.  BtShar
15159 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20  ed *pBt,        
1515a 20 20 20 2f 2a 20 42 74 72 65 65 20 2a 2f 0a 20     /* Btree */. 
1515b 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 50 61 67   MemPage *pDbPag
1515c 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65  e,        /* Ope
1515d 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 2a  n page to move *
1515e 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20  /.  u8 eType,   
1515f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15160 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 74 79 70  Pointer map 'typ
15161 65 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62  e' entry for pDb
15162 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  Page */.  Pgno i
15163 50 74 72 50 61 67 65 2c 20 20 20 20 20 20 20 20  PtrPage,        
15164 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61     /* Pointer ma
15165 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e 74 72  p 'page-no' entr
15166 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f  y for pDbPage */
15167 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67  .  Pgno iFreePag
15168 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e,          /* T
15169 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 6d  he location to m
1516a 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f 20 2a  ove pDbPage to *
1516b 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74  /.  int isCommit
1516c 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  .){.  MemPage *p
1516d 50 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 54 68  PtrPage;   /* Th
1516e 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74  e page that cont
1516f 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ains a pointer t
15170 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50  o pDbPage */.  P
15171 67 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 70 44  gno iDbPage = pD
15172 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 50  bPage->pgno;.  P
15173 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
15174 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e  Bt->pPager;.  in
15175 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
15176 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f   eType==PTRMAP_O
15177 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 79 70  VERFLOW2 || eTyp
15178 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  e==PTRMAP_OVERFL
15179 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 65 54  OW1 || .      eT
1517a 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45  ype==PTRMAP_BTRE
1517b 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d  E || eType==PTRM
1517c 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20  AP_ROOTPAGE );. 
1517d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1517e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
1517f 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
15180 65 72 74 28 20 70 44 62 50 61 67 65 2d 3e 70 42  ert( pDbPage->pB
15181 74 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 2f 2a 20  t==pBt );..  /* 
15182 4d 6f 76 65 20 70 61 67 65 20 69 44 62 50 61 67  Move page iDbPag
15183 65 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72 65  e from its curre
15184 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70  nt location to p
15185 61 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65 65  age number iFree
15186 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45 28  Page */.  TRACE(
15187 28 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f  ("AUTOVACUUM: Mo
15188 76 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65 20  ving %d to free 
15189 70 61 67 65 20 25 64 20 28 70 74 72 20 70 61 67  page %d (ptr pag
1518a 65 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e 22  e %d type %d)\n"
1518b 2c 20 0a 20 20 20 20 20 20 69 44 62 50 61 67 65  , .      iDbPage
1518c 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 50 74  , iFreePage, iPt
1518d 72 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b 0a  rPage, eType));.
1518e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1518f 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61 67  gerMovepage(pPag
15190 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44 62  er, pDbPage->pDb
15191 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c  Page, iFreePage,
15192 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 66   isCommit);.  if
15193 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
15194 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
15195 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 2d  ;.  }.  pDbPage-
15196 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 67  >pgno = iFreePag
15197 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 50  e;..  /* If pDbP
15198 61 67 65 20 77 61 73 20 61 20 62 74 72 65 65 2d  age was a btree-
15199 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d 61  page, then it ma
1519a 79 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 67  y have child pag
1519b 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a  es and/or cells.
1519c 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 20    ** that point 
1519d 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  to overflow page
1519e 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d  s. The pointer m
1519f 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61  ap entries for a
151a0 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 61  ll these.  ** pa
151a1 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 63  ges need to be c
151a2 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a  hanged..  **.  *
151a3 2a 20 49 66 20 70 44 62 50 61 67 65 20 69 73 20  * If pDbPage is 
151a4 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
151a5 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74  , then the first
151a6 20 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 6f   4 bytes may sto
151a7 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65  re a.  ** pointe
151a8 72 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 6e  r to a subsequen
151a9 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e  t overflow page.
151aa 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
151ab 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  case, then.  ** 
151ac 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  the pointer map 
151ad 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61  needs to be upda
151ae 74 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 73  ted for the subs
151af 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20  equent overflow 
151b0 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  page..  */.  if(
151b1 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42   eType==PTRMAP_B
151b2 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50  TREE || eType==P
151b3 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29  TRMAP_ROOTPAGE )
151b4 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 68  {.    rc = setCh
151b5 69 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 61  ildPtrmaps(pDbPa
151b6 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
151b7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
151b8 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
151b9 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
151ba 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20    Pgno nextOvfl 
151bb 3d 20 67 65 74 34 62 79 74 65 28 70 44 62 50 61  = get4byte(pDbPa
151bc 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20  ge->aData);.    
151bd 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20  if( nextOvfl!=0 
151be 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74  ){.      rc = pt
151bf 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78  rmapPut(pBt, nex
151c0 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56  tOvfl, PTRMAP_OV
151c1 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61  ERFLOW2, iFreePa
151c2 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
151c3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
151c4 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
151c5 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
151c6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74  .  }..  /* Fix t
151c7 68 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e  he database poin
151c8 74 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 72  ter on page iPtr
151c9 50 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 65  Page that pointe
151ca 64 20 61 74 20 69 44 62 50 61 67 65 20 73 6f 0a  d at iDbPage so.
151cb 20 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69    ** that it poi
151cc 6e 74 73 20 61 74 20 69 46 72 65 65 50 61 67 65  nts at iFreePage
151cd 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 70  . Also fix the p
151ce 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79  ointer map entry
151cf 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 61   for.  ** iPtrPa
151d0 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65  ge..  */.  if( e
151d1 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f  Type!=PTRMAP_ROO
151d2 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20  TPAGE ){.    rc 
151d3 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
151d4 74 50 61 67 65 28 70 42 74 2c 20 69 50 74 72 50  tPage(pBt, iPtrP
151d5 61 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20  age, &pPtrPage, 
151d6 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  0);.    if( rc!=
151d7 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
151d8 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
151d9 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
151da 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
151db 50 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65  PtrPage->pDbPage
151dc 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
151dd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
151de 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
151df 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72  trPage);.      r
151e0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
151e1 20 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50      rc = modifyP
151e2 61 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50  agePointer(pPtrP
151e3 61 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46  age, iDbPage, iF
151e4 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b  reePage, eType);
151e5 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
151e6 28 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20  (pPtrPage);.    
151e7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
151e8 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
151e9 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 69  ptrmapPut(pBt, i
151ea 46 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 2c  FreePage, eType,
151eb 20 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20   iPtrPage);.    
151ec 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
151ed 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  c;.}../* Forward
151ee 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71   declaration req
151ef 75 69 72 65 64 20 62 79 20 69 6e 63 72 56 61 63  uired by incrVac
151f0 75 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74  uumStep(). */.st
151f1 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74  atic int allocat
151f2 65 42 74 72 65 65 50 61 67 65 28 42 74 53 68 61  eBtreePage(BtSha
151f3 72 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a  red *, MemPage *
151f4 2a 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c  *, Pgno *, Pgno,
151f5 20 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72   u8);../*.** Per
151f6 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74  form a single st
151f7 65 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65  ep of an increme
151f8 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20  ntal-vacuum. If 
151f9 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72  successful,.** r
151fa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
151fb 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
151fc 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20  work to do (and 
151fd 74 68 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20  therefore no.** 
151fe 70 6f 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67  point in calling
151ff 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
15200 67 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51  gain), return SQ
15201 4c 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a  LITE_DONE..**.**
15202 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79   More specificly
15203 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
15204 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f  attempts to re-o
15205 72 67 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20  rganize the .** 
15206 64 61 74 61 62 61 73 65 20 73 6f 20 74 68 61 74  database so that
15207 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f   the last page o
15208 66 20 74 68 65 20 66 69 6c 65 20 63 75 72 72 65  f the file curre
15209 6e 74 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69  ntly in use.** i
1520a 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75  s no longer in u
1520b 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
1520c 20 6e 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20   nFin parameter 
1520d 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1520e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
1520f 61 73 73 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20  assumes.** that 
15210 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20  the caller will 
15211 6b 65 65 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63  keep calling inc
15212 72 56 61 63 75 75 6d 53 74 65 70 28 29 20 75 6e  rVacuumStep() un
15213 74 69 6c 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e  til.** it return
15214 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72  s SQLITE_DONE or
15215 20 61 6e 20 65 72 72 6f 72 2c 20 61 6e 64 20 74   an error, and t
15216 68 61 74 20 6e 46 69 6e 20 69 73 20 74 68 65 0a  hat nFin is the.
15217 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ** number of pag
15218 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  es the database 
15219 66 69 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69  file will contai
1521a 6e 20 61 66 74 65 72 20 74 68 69 73 20 0a 2a 2a  n after this .**
1521b 20 70 72 6f 63 65 73 73 20 69 73 20 63 6f 6d 70   process is comp
1521c 6c 65 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lete..*/.static 
1521d 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74  int incrVacuumSt
1521e 65 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ep(BtShared *pBt
1521f 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e  , Pgno nFin, Pgn
15220 6f 20 69 4c 61 73 74 50 67 29 7b 0a 20 20 50 67  o iLastPg){.  Pg
15221 6e 6f 20 6e 46 72 65 65 4c 69 73 74 3b 20 20 20  no nFreeList;   
15222 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
15223 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c  r of pages still
15224 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   on the free-lis
15225 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
15226 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
15227 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
15228 3b 0a 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50  ;..  if( !PTRMAP
15229 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61  _ISPAGE(pBt, iLa
1522a 73 74 50 67 29 20 26 26 20 69 4c 61 73 74 50 67  stPg) && iLastPg
1522b 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
1522c 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
1522d 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 38 20 65  int rc;.    u8 e
1522e 54 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69  Type;.    Pgno i
1522f 50 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46  PtrPage;..    nF
15230 72 65 65 4c 69 73 74 20 3d 20 67 65 74 34 62 79  reeList = get4by
15231 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d  te(&pBt->pPage1-
15232 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20  >aData[36]);.   
15233 20 69 66 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d   if( nFreeList==
15234 30 20 7c 7c 20 6e 46 69 6e 3d 3d 69 4c 61 73 74  0 || nFin==iLast
15235 50 67 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  Pg ){.      retu
15236 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
15237 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20      }..    rc = 
15238 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69  ptrmapGet(pBt, i
15239 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65 2c 20  LastPg, &eType, 
1523a 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20  &iPtrPage);.    
1523b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1523c 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
1523d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
1523e 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  if( eType==PTRMA
1523f 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20  P_ROOTPAGE ){.  
15240 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
15241 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
15242 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 65      }..    if( e
15243 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45  Type==PTRMAP_FRE
15244 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 69  EPAGE ){.      i
15245 66 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20  f( nFin==0 ){.  
15246 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20        /* Remove 
15247 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68  the page from th
15248 65 20 66 69 6c 65 73 20 66 72 65 65 2d 6c 69 73  e files free-lis
15249 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72  t. This is not r
1524a 65 71 75 69 72 65 64 0a 20 20 20 20 20 20 20 20  equired.        
1524b 2a 2a 20 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f  ** if nFin is no
1524c 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61 74 20  n-zero. In that 
1524d 63 61 73 65 2c 20 74 68 65 20 66 72 65 65 2d 6c  case, the free-l
1524e 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20 20 20  ist will be.    
1524f 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 64      ** truncated
15250 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 20 74   to zero after t
15251 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
15252 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f 65 73  urns, so it does
15253 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  n't .        ** 
15254 6d 61 74 74 65 72 20 69 66 20 69 74 20 73 74 69  matter if it sti
15255 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65  ll contains some
15256 20 67 61 72 62 61 67 65 20 65 6e 74 72 69 65 73   garbage entries
15257 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
15258 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50       Pgno iFreeP
15259 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61  g;.        MemPa
1525a 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20  ge *pFreePg;.   
1525b 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
1525c 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
1525d 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65   &pFreePg, &iFre
1525e 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 31 29  ePg, iLastPg, 1)
1525f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
15260 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
15261 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
15262 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
15263 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46        assert( iF
15264 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 67 20 29  reePg==iLastPg )
15265 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
15266 65 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a  ePage(pFreePg);.
15267 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c        }.    } el
15268 73 65 20 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20  se {.      Pgno 
15269 69 46 72 65 65 50 67 3b 20 20 20 20 20 20 20 20  iFreePg;        
1526a 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1526b 20 66 72 65 65 20 70 61 67 65 20 74 6f 20 6d 6f   free page to mo
1526c 76 65 20 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f  ve pLastPg to */
1526d 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  .      MemPage *
1526e 70 4c 61 73 74 50 67 3b 0a 0a 20 20 20 20 20 20  pLastPg;..      
1526f 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
15270 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c  eGetPage(pBt, iL
15271 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 67 2c  astPg, &pLastPg,
15272 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
15273 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
15274 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
15275 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  c;.      }..    
15276 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69 73 20    /* If nFin is 
15277 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f 70 20  zero, this loop 
15278 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f 6e 63  runs exactly onc
15279 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61 73 74  e and page pLast
1527a 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73  Pg.      ** is s
1527b 77 61 70 70 65 64 20 77 69 74 68 20 74 68 65 20  wapped with the 
1527c 66 69 72 73 74 20 66 72 65 65 20 70 61 67 65 20  first free page 
1527d 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65 20 66  pulled off the f
1527e 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20 20 20  ree list..      
1527f 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e 20 74  **.      ** On t
15280 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69  he other hand, i
15281 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61 74 65  f nFin is greate
15282 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  r than zero, the
15283 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a 2a 20  n keep.      ** 
15284 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 20  looping until a 
15285 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 74 65  free-page locate
15286 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 69 72  d within the fir
15287 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a 20 20  st nFin pages.  
15288 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69      ** of the fi
15289 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 20  le is found..   
1528a 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b     */.      do {
1528b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65  .        MemPage
1528c 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20   *pFreePg;.     
1528d 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65     rc = allocate
1528e 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26  BtreePage(pBt, &
1528f 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50  pFreePg, &iFreeP
15290 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  g, 0, 0);.      
15291 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
15292 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
15293 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61   releasePage(pLa
15294 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20  stPg);.         
15295 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
15296 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
15297 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 65 50  leasePage(pFreeP
15298 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65  g);.      }while
15299 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69 46 72  ( nFin!=0 && iFr
1529a 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 20 20  eePg>nFin );.   
1529b 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65     assert( iFree
1529c 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a 20 20  Pg<iLastPg );.  
1529d 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20      .      rc = 
1529e 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1529f 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62 50 61  e(pLastPg->pDbPa
152a0 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
152a1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
152a2 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c          rc = rel
152a3 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70  ocatePage(pBt, p
152a4 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c 20 69  LastPg, eType, i
152a5 50 74 72 50 61 67 65 2c 20 69 46 72 65 65 50 67  PtrPage, iFreePg
152a6 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20 20 20  , nFin!=0);.    
152a7 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73    }.      releas
152a8 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a  ePage(pLastPg);.
152a9 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
152aa 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
152ab 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
152ac 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
152ad 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 20 29  .  if( nFin==0 )
152ae 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b  {.    iLastPg--;
152af 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c 61 73  .    while( iLas
152b0 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  tPg==PENDING_BYT
152b1 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50 54 52  E_PAGE(pBt)||PTR
152b2 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
152b3 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20  iLastPg) ){.    
152b4 20 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20    iLastPg--;.   
152b5 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61   }.    sqlite3Pa
152b6 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65  gerTruncateImage
152b7 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c  (pBt->pPager, iL
152b8 61 73 74 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65  astPg);.  }.  re
152b9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
152ba 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65  }../*.** A write
152bb 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73  -transaction mus
152bc 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 6f  t be opened befo
152bd 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  re calling this 
152be 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20  function..** It 
152bf 70 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c  performs a singl
152c0 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74  e unit of work t
152c1 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65 6d  owards an increm
152c2 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a  ental vacuum..**
152c3 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72 65  .** If the incre
152c4 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69 73  mental vacuum is
152c5 20 66 69 6e 69 73 68 65 64 20 61 66 74 65 72 20   finished after 
152c6 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61  this function ha
152c7 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45  s run,.** SQLITE
152c8 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65  _DONE is returne
152c9 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  d. If it is not 
152ca 66 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e 6f  finished, but no
152cb 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 2c 0a   error occured,.
152cc 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
152cd 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
152ce 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72  ise an SQLite er
152cf 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51  ror code. .*/.SQ
152d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
152d1 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63   sqlite3BtreeInc
152d2 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70  rVacuum(Btree *p
152d3 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
152d4 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
152d5 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65  ->pBt;..  sqlite
152d6 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
152d7 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64    pBt->db = p->d
152d8 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  b;.  assert( pBt
152d9 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
152da 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26 20  =TRANS_WRITE && 
152db 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
152dc 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 28  S_WRITE );.  if(
152dd 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75   !pBt->autoVacuu
152de 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  m ){.    rc = SQ
152df 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c  LITE_DONE;.  }el
152e0 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61  se{.    invalida
152e1 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63  teAllOverflowCac
152e2 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20  he(pBt);.    rc 
152e3 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70  = incrVacuumStep
152e4 28 70 42 74 2c 20 30 2c 20 73 71 6c 69 74 65 33  (pBt, 0, sqlite3
152e5 50 61 67 65 72 49 6d 61 67 65 53 69 7a 65 28 70  PagerImageSize(p
152e6 42 74 2d 3e 70 50 61 67 65 72 29 29 3b 0a 20 20  Bt->pPager));.  
152e7 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
152e8 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
152e9 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
152ea 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
152eb 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20  called prior to 
152ec 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
152ed 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61  it when a transa
152ee 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d  ction.** is comm
152ef 69 74 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f  ited for an auto
152f0 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
152f1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ..**.** If SQLIT
152f2 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
152f3 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20  , then *pnTrunc 
152f4 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
152f5 6d 62 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a  mber of pages.**
152f6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
152f7 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75  le should be tru
152f8 6e 63 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67  ncated to during
152f9 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63   the commit proc
152fa 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68  ess. .** i.e. th
152fb 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62  e database has b
152fc 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20  een reorganized 
152fd 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65  so that only the
152fe 20 66 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a   first *pnTrunc.
152ff 2a 2a 20 70 61 67 65 73 20 61 72 65 20 69 6e 20  ** pages are in 
15300 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  use..*/.static i
15301 6e 74 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d  nt autoVacuumCom
15302 6d 69 74 28 42 74 53 68 61 72 65 64 20 2a 70 42  mit(BtShared *pB
15303 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
15304 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65  QLITE_OK;.  Page
15305 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d  r *pPager = pBt-
15306 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f  >pPager;.  VVA_O
15307 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20  NLY( int nRef = 
15308 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
15309 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a  ount(pPager) );.
1530a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1530b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
1530c 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  t->mutex) );.  i
1530d 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72  nvalidateAllOver
1530e 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a  flowCache(pBt);.
1530f 20 20 61 73 73 65 72 74 28 70 42 74 2d 3e 61 75    assert(pBt->au
15310 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 28  toVacuum);.  if(
15311 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75   !pBt->incrVacuu
15312 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46  m ){.    Pgno nF
15313 69 6e 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 72  in;.    Pgno nFr
15314 65 65 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 50 74  ee;.    Pgno nPt
15315 72 6d 61 70 3b 0a 20 20 20 20 50 67 6e 6f 20 69  rmap;.    Pgno i
15316 46 72 65 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Free;.    const 
15317 69 6e 74 20 70 67 73 7a 20 3d 20 70 42 74 2d 3e  int pgsz = pBt->
15318 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 50 67  pageSize;.    Pg
15319 6e 6f 20 6e 4f 72 69 67 20 3d 20 70 61 67 65 72  no nOrig = pager
1531a 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a  Pagecount(pBt);.
1531b 0a 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f  .    if( PTRMAP_
1531c 49 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69  ISPAGE(pBt, nOri
1531d 67 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  g) ){.      retu
1531e 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1531f 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20  T_BKPT;.    }.  
15320 20 20 69 66 28 20 6e 4f 72 69 67 3d 3d 50 45 4e    if( nOrig==PEN
15321 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
15322 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 4f 72  Bt) ){.      nOr
15323 69 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ig--;.    }.    
15324 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65  nFree = get4byte
15325 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61  (&pBt->pPage1->a
15326 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e  Data[36]);.    n
15327 50 74 72 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d  Ptrmap = (nFree-
15328 6e 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50 41 47  nOrig+PTRMAP_PAG
15329 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b  ENO(pBt, nOrig)+
1532a 70 67 73 7a 2f 35 29 2f 28 70 67 73 7a 2f 35 29  pgsz/5)/(pgsz/5)
1532b 3b 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72  ;.    nFin = nOr
1532c 69 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74  ig - nFree - nPt
1532d 72 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f  rmap;.    if( nO
1532e 72 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45  rig>PENDING_BYTE
1532f 5f 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46  _PAGE(pBt) && nF
15330 69 6e 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  in<=PENDING_BYTE
15331 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20  _PAGE(pBt) ){.  
15332 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20      nFin--;.    
15333 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52  }.    while( PTR
15334 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
15335 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50  nFin) || nFin==P
15336 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
15337 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e  (pBt) ){.      n
15338 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 0a 20 20  Fin--;.    }..  
15339 20 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69    for(iFree=nOri
1533a 67 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26  g; iFree>nFin &&
1533b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20   rc==SQLITE_OK; 
1533c 69 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20  iFree--){.      
1533d 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53  rc = incrVacuumS
1533e 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69  tep(pBt, nFin, i
1533f 46 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Free);.    }.   
15340 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45   if( (rc==SQLITE
15341 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c  _DONE || rc==SQL
15342 49 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65  ITE_OK) && nFree
15343 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  >0 ){.      rc =
15344 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
15345 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
15346 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50  gerWrite(pBt->pP
15347 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a  age1->pDbPage);.
15348 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
15349 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
1534a 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20  ta[32], 0);.    
1534b 20 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d    put4byte(&pBt-
1534c 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
1534d 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  6], 0);.      sq
1534e 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
1534f 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61  teImage(pBt->pPa
15350 67 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20  ger, nFin);.    
15351 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  }.    if( rc!=SQ
15352 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
15353 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c   sqlite3PagerRol
15354 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20  lback(pPager);. 
15355 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65     }.  }..  asse
15356 72 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65  rt( nRef==sqlite
15357 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
15358 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75  Pager) );.  retu
15359 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66  rn rc;.}..#endif
1535a 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54   /* ifndef SQLIT
1535b 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1535c 4d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  M */../*.** This
1535d 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68   routine does th
1535e 65 20 66 69 72 73 74 20 70 68 61 73 65 20 6f 66  e first phase of
1535f 20 61 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d   a two-phase com
15360 6d 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69  mit.  This routi
15361 6e 65 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72  ne.** causes a r
15362 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
15363 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 28 69  to be created (i
15364 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c  f it does not al
15365 72 65 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20  ready exist).** 
15366 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69  and populated wi
15367 74 68 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d  th enough inform
15368 61 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66  ation so that if
15369 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63   a power loss oc
1536a 63 75 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61  curs.** the data
1536b 62 61 73 65 20 63 61 6e 20 62 65 20 72 65 73 74  base can be rest
1536c 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67  ored to its orig
1536d 69 6e 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c  inal state by pl
1536e 61 79 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68  aying back.** th
1536f 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e  e journal.  Then
15370 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
15371 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65   the journal are
15372 20 66 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a   flushed out to.
15373 2a 2a 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66  ** the disk.  Af
15374 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ter the journal 
15375 69 73 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69  is safely on oxi
15376 64 65 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20  de, the changes 
15377 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  to the.** databa
15378 73 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 69  se are written i
15379 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
1537a 20 66 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65   file and flushe
1537b 64 20 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41  d to oxide..** A
1537c 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69  t the end of thi
1537d 73 20 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c  s call, the roll
1537e 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69  back journal sti
1537f 6c 6c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65  ll exists on the
15380 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20  .** disk and we 
15381 61 72 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e  are still holdin
15382 67 20 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20  g all locks, so 
15383 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
15384 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69  has not.** commi
15385 74 74 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74  tted.  See sqlit
15386 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 29 20  e3BtreeCommit() 
15387 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70  for the second p
15388 68 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63  hase of the.** c
15389 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a  ommit process..*
1538a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69  *.** This call i
1538b 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20  s a no-op if no 
1538c 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1538d 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  n is currently a
1538e 63 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a  ctive on pBt..**
1538f 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73  .** Otherwise, s
15390 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65  ync the database
15391 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74   file for the bt
15392 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72  ree pBt. zMaster
15393 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68   points to.** th
15394 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74  e name of a mast
15395 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
15396 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77  that should be w
15397 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a  ritten into the.
15398 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f  ** individual jo
15399 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69  urnal file, or i
1539a 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69  s NULL, indicati
1539b 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  ng no master jou
1539c 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73  rnal file .** (s
1539d 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74  ingle database t
1539e 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a  ransaction)..**.
1539f 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20  ** When this is 
153a0 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74  called, the mast
153a1 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c  er journal shoul
153a2 64 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62  d already have b
153a3 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20  een.** created, 
153a4 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74  populated with t
153a5 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e  his journal poin
153a6 74 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74  ter and synced t
153a7 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  o disk..**.** On
153a8 63 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69  ce this is routi
153a9 6e 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c  ne has returned,
153aa 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   the only thing 
153ab 72 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d  required to comm
153ac 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d  it.** the write-
153ad 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20  transaction for 
153ae 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69  this database fi
153af 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20  le is to delete 
153b0 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a  the journal..*/.
153b1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
153b2 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
153b3 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74  ommitPhaseOne(Bt
153b4 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  ree *p, const ch
153b5 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
153b6 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
153b7 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54  OK;.  if( p->inT
153b8 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
153b9 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65  E ){.    BtShare
153ba 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
153bb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
153bc 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 70  eEnter(p);.    p
153bd 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
153be 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
153bf 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
153c0 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
153c1 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20  Vacuum ){.      
153c2 72 63 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43  rc = autoVacuumC
153c3 6f 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20  ommit(pBt);.    
153c4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
153c5 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73  _OK ){.        s
153c6 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
153c7 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  (p);.        ret
153c8 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
153c9 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
153ca 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
153cb 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  erCommitPhaseOne
153cc 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d  (pBt->pPager, zM
153cd 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73  aster, 0);.    s
153ce 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
153cf 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  (p);.  }.  retur
153d0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
153d1 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61  ommit the transa
153d2 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20  ction currently 
153d3 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a  in progress..**.
153d4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
153d5 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73  implements the s
153d6 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61  econd phase of a
153d7 20 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e   2-phase commit.
153d8 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33    The.** sqlite3
153d9 42 74 72 65 65 53 79 6e 63 28 29 20 72 6f 75 74  BtreeSync() rout
153da 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72  ine does the fir
153db 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f  st phase and sho
153dc 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a  uld be invoked.*
153dd 2a 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69  * prior to calli
153de 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ng this routine.
153df 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72    The sqlite3Btr
153e0 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65  eeSync() routine
153e1 20 64 69 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20   did.** all the 
153e2 77 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20  work of writing 
153e3 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20  information out 
153e4 74 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73  to disk and flus
153e5 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74  hing the.** cont
153e6 65 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65  ents so that the
153e7 79 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e  y are written on
153e8 74 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74  to the disk plat
153e9 74 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a  ter.  All this.*
153ea 2a 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f  * routine has to
153eb 20 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72   do is delete or
153ec 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 72 6f   truncate the ro
153ed 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a  llback journal.*
153ee 2a 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20  * (which causes 
153ef 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
153f0 74 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64  to commit) and d
153f1 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a  rop locks..**.**
153f2 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61   This will relea
153f3 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63  se the write loc
153f4 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
153f5 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72  e file.  If ther
153f6 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69  e.** are no acti
153f7 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61  ve cursors, it a
153f8 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65  lso releases the
153f9 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53   read lock..*/.S
153fa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
153fb 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
153fc 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72  mmitPhaseTwo(Btr
153fd 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72  ee *p){.  BtShar
153fe 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
153ff 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
15400 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74  eEnter(p);.  pBt
15401 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
15402 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
15403 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
15404 68 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69  handle has a wri
15405 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te-transaction o
15406 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20  pen, commit the 
15407 73 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20  shared-btrees . 
15408 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
15409 61 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72  and set the shar
1540a 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e  ed state to TRAN
1540b 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69  S_READ..  */.  i
1540c 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  f( p->inTrans==T
1540d 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
1540e 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73    int rc;.    as
1540f 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61  sert( pBt->inTra
15410 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
15411 57 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73  WRITE );.    ass
15412 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73  ert( pBt->nTrans
15413 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20  action>0 );.    
15414 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15415 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
15416 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
15417 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
15418 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
15419 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1541a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1541b 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  rc;.    }.    pB
1541c 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
1541d 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20   = TRANS_READ;. 
1541e 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d     pBt->inStmt =
1541f 20 30 3b 0a 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b   0;.  }.  unlock
15420 41 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20  AllTables(p);.. 
15421 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c   /* If the handl
15422 65 20 68 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f  e has any kind o
15423 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  f transaction op
15424 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68  en, decrement th
15425 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  e transaction.  
15426 2a 2a 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20  ** count of the 
15427 73 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66  shared btree. If
15428 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
15429 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 30   count reaches 0
1542a 2c 20 73 65 74 0a 20 20 2a 2a 20 74 68 65 20 73  , set.  ** the s
1542b 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54  hared state to T
1542c 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75  RANS_NONE. The u
1542d 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
1542e 65 64 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a  ed() call below.
1542f 20 20 2a 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b    ** will unlock
15430 20 74 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f   the pager..  */
15431 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
15432 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b  s!=TRANS_NONE ){
15433 0a 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73  .    pBt->nTrans
15434 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66  action--;.    if
15435 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73  ( 0==pBt->nTrans
15436 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  action ){.      
15437 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
15438 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b  on = TRANS_NONE;
15439 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1543a 20 53 65 74 20 74 68 65 20 68 61 6e 64 6c 65 73   Set the handles
1543b 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
1543c 74 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52  tion state to TR
1543d 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c  ANS_NONE and unl
1543e 6f 63 6b 0a 20 20 2a 2a 20 74 68 65 20 70 61 67  ock.  ** the pag
1543f 65 72 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20  er if this call 
15440 63 6c 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 20  closed the only 
15441 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72  read or write tr
15442 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  ansaction..  */.
15443 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54    p->inTrans = T
15444 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c  RANS_NONE;.  unl
15445 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64  ockBtreeIfUnused
15446 28 70 42 74 29 3b 0a 0a 20 20 62 74 72 65 65 49  (pBt);..  btreeI
15447 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73  ntegrity(p);.  s
15448 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
15449 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (p);.  return SQ
1544a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1544b 2a 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65 73  * Do both phases
1544c 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f   of a commit..*/
1544d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1544e 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
1544f 43 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 29  Commit(Btree *p)
15450 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
15451 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
15452 70 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  p);.  rc = sqlit
15453 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
15454 73 65 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69  seOne(p, 0);.  i
15455 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15456 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
15457 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
15458 68 61 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d 0a  haseTwo(p);.  }.
15459 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1545a 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
1545b 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20   rc;.}..#ifndef 
1545c 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74  NDEBUG./*.** Ret
1545d 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1545e 66 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73 20  f write-cursors 
1545f 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e  open on this han
15460 64 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f 72  dle. This is for
15461 20 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72   use.** in asser
15462 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 73 2c  t() expressions,
15463 20 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 63   so it is only c
15464 6f 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 42 55  ompiled if NDEBU
15465 47 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69  G is not.** defi
15466 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  ned..**.** For t
15467 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74  he purposes of t
15468 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 77  his routine, a w
15469 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 61  rite-cursor is a
1546a 6e 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a  ny cursor that.*
1546b 2a 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20  * is capable of 
1546c 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64  writing to the d
1546d 61 74 61 62 73 65 2e 20 20 54 68 61 74 20 6d 65  atabse.  That me
1546e 61 6e 73 20 74 68 65 20 63 75 72 73 6f 72 20 77  ans the cursor w
1546f 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79  as.** originally
15470 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74   opened for writ
15471 69 6e 67 20 61 6e 64 20 74 68 65 20 63 75 72 73  ing and the curs
15472 6f 72 20 68 61 73 20 6e 6f 74 20 62 65 20 64 69  or has not be di
15473 73 61 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61 76  sabled.** by hav
15474 69 6e 67 20 69 74 73 20 73 74 61 74 65 20 63 68  ing its state ch
15475 61 6e 67 65 64 20 74 6f 20 43 55 52 53 4f 52 5f  anged to CURSOR_
15476 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 63  FAULT..*/.static
15477 20 69 6e 74 20 63 6f 75 6e 74 57 72 69 74 65 43   int countWriteC
15478 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 20  ursors(BtShared 
15479 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f  *pBt){.  BtCurso
1547a 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72  r *pCur;.  int r
1547b 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 72   = 0;.  for(pCur
1547c 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70  =pBt->pCursor; p
1547d 43 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e  Cur; pCur=pCur->
1547e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
1547f 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20  pCur->wrFlag && 
15480 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55  pCur->eState!=CU
15481 52 53 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b  RSOR_FAULT ) r++
15482 3b 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  ; .  }.  return 
15483 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  r;.}.#endif../*.
15484 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
15485 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20 74  sets the state t
15486 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 61  o CURSOR_FAULT a
15487 6e 64 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20  nd the error.** 
15488 63 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 65 20  code to errCode 
15489 66 6f 72 20 65 76 65 72 79 20 63 75 72 73 6f 72  for every cursor
1548a 20 6f 6e 20 42 74 53 68 61 72 65 64 20 74 68 61   on BtShared tha
1548b 74 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 65  t pBtree.** refe
1548c 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76  rences..**.** Ev
1548d 65 72 79 20 63 75 72 73 6f 72 20 69 73 20 74 72  ery cursor is tr
1548e 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e 67  ipped, including
1548f 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 62 65   cursors that be
15490 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 72  long.** to other
15491 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
15492 74 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70 65  tions that happe
15493 6e 20 74 6f 20 62 65 20 73 68 61 72 69 6e 67 0a  n to be sharing.
15494 2a 2a 20 74 68 65 20 63 61 63 68 65 20 77 69 74  ** the cache wit
15495 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  h pBtree..**.** 
15496 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 74  This routine get
15497 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20  s called when a 
15498 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e  rollback occurs.
15499 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 20  .** All cursors 
1549a 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63  using the same c
1549b 61 63 68 65 20 6d 75 73 74 20 62 65 20 74 72 69  ache must be tri
1549c 70 70 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76 65  pped.** to preve
1549d 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 79  nt them from try
1549e 69 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20 62  ing to use the b
1549f 74 72 65 65 20 61 66 74 65 72 0a 2a 2a 20 74 68  tree after.** th
154a0 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65  e rollback.  The
154a1 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61   rollback may ha
154a2 76 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c 65  ve deleted table
154a3 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72 6f  s.** or moved ro
154a4 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74 20  ot pages, so it 
154a5 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e  is not sufficien
154a6 74 20 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68 65  t to.** save the
154a7 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 75   state of the cu
154a8 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f  rsor.  The curso
154a9 72 20 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e 76  r must be.** inv
154aa 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c  alidated..*/.SQL
154ab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
154ac 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69   sqlite3BtreeTri
154ad 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65  pAllCursors(Btre
154ae 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 65  e *pBtree, int e
154af 72 72 43 6f 64 65 29 7b 0a 20 20 42 74 43 75 72  rrCode){.  BtCur
154b0 73 6f 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65  sor *p;.  sqlite
154b1 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72  3BtreeEnter(pBtr
154b2 65 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74  ee);.  for(p=pBt
154b3 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f  ree->pBt->pCurso
154b4 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  r; p; p=p->pNext
154b5 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
154b6 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
154b7 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20  earCursor(p);.  
154b8 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55    p->eState = CU
154b9 52 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 20  RSOR_FAULT;.    
154ba 70 2d 3e 73 6b 69 70 20 3d 20 65 72 72 43 6f 64  p->skip = errCod
154bb 65 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  e;.    for(i=0; 
154bc 69 3c 3d 70 2d 3e 69 50 61 67 65 3b 20 69 2b 2b  i<=p->iPage; i++
154bd 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
154be 50 61 67 65 28 70 2d 3e 61 70 50 61 67 65 5b 69  Page(p->apPage[i
154bf 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 50  ]);.      p->apP
154c0 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  age[i] = 0;.    
154c1 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42  }.  }.  sqlite3B
154c2 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65  treeLeave(pBtree
154c3 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c  );.}../*.** Roll
154c4 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63  back the transac
154c5 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73  tion in progress
154c6 2e 20 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 77  .  All cursors w
154c7 69 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69  ill be.** invali
154c8 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72  ded by this oper
154c9 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65  ation.  Any atte
154ca 6d 70 74 20 74 6f 20 75 73 65 20 61 20 63 75 72  mpt to use a cur
154cb 73 6f 72 0a 2a 2a 20 74 68 61 74 20 77 61 73 20  sor.** that was 
154cc 6f 70 65 6e 20 61 74 20 74 68 65 20 62 65 67 69  open at the begi
154cd 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 6f 70  nning of this op
154ce 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73  eration will res
154cf 75 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 65 72 72  ult.** in an err
154d0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77  or..**.** This w
154d1 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20  ill release the 
154d2 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  write lock on th
154d3 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
154d4 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72    If there.** ar
154d5 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73  e no active curs
154d6 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c  ors, it also rel
154d7 65 61 73 65 73 20 74 68 65 20 72 65 61 64 20 6c  eases the read l
154d8 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ock..*/.SQLITE_P
154d9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
154da 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28  e3BtreeRollback(
154db 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74  Btree *p){.  int
154dc 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20   rc;.  BtShared 
154dd 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
154de 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31   MemPage *pPage1
154df 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
154e0 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74  eEnter(p);.  pBt
154e1 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
154e2 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73  rc = saveAllCurs
154e3 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a  ors(pBt, 0, 0);.
154e4 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
154e5 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
154e6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
154e7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 54  E_OK ){.    /* T
154e8 68 69 73 20 69 73 20 61 20 68 6f 72 72 69 62 6c  his is a horribl
154e9 65 20 73 69 74 75 61 74 69 6f 6e 2e 20 41 6e 20  e situation. An 
154ea 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65  IO or malloc() e
154eb 72 72 6f 72 20 6f 63 63 75 72 65 64 20 77 68 69  rror occured whi
154ec 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72 79 69 6e  lst.    ** tryin
154ed 67 20 74 6f 20 73 61 76 65 20 63 75 72 73 6f 72  g to save cursor
154ee 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49 66 20 74   positions. If t
154ef 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 6d 61  his is an automa
154f0 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 28 61 73  tic rollback (as
154f1 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 65 73 75  .    ** the resu
154f2 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74 72 61 69  lt of a constrai
154f3 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  nt, malloc() fai
154f4 6c 75 72 65 20 6f 72 20 49 4f 20 65 72 72 6f 72  lure or IO error
154f5 29 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74  ) then .    ** t
154f6 68 65 20 63 61 63 68 65 20 6d 61 79 20 62 65 20  he cache may be 
154f7 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e 63 6f 6e  internally incon
154f8 73 69 73 74 65 6e 74 20 28 6e 6f 74 20 63 6f 6e  sistent (not con
154f9 74 61 69 6e 20 76 61 6c 69 64 20 74 72 65 65 73  tain valid trees
154fa 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77 65 20 63  ) so.    ** we c
154fb 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 72 65 74  annot simply ret
154fc 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 74 6f  urn the error to
154fd 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 73   the caller. Ins
154fe 74 65 61 64 2c 20 61 62 6f 72 74 20 0a 20 20 20  tead, abort .   
154ff 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69 65 73 20   ** all queries 
15500 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 69 6e  that may be usin
15501 67 20 61 6e 79 20 6f 66 20 74 68 65 20 63 75 72  g any of the cur
15502 73 6f 72 73 20 74 68 61 74 20 66 61 69 6c 65 64  sors that failed
15503 20 74 6f 20 73 61 76 65 2e 0a 20 20 20 20 2a 2f   to save..    */
15504 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
15505 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28  eTripAllCursors(
15506 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 65 6e 64  p, rc);.  }.#end
15507 69 66 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72  if.  btreeIntegr
15508 69 74 79 28 70 29 3b 0a 20 20 75 6e 6c 6f 63 6b  ity(p);.  unlock
15509 41 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20  AllTables(p);.. 
1550a 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d   if( p->inTrans=
1550b 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a  =TRANS_WRITE ){.
1550c 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20      int rc2;..  
1550d 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53 5f    assert( TRANS_
1550e 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72  WRITE==pBt->inTr
1550f 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20  ansaction );.   
15510 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50 61   rc2 = sqlite3Pa
15511 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d  gerRollback(pBt-
15512 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66  >pPager);.    if
15513 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b  ( rc2!=SQLITE_OK
15514 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   ){.      rc = r
15515 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  c2;.    }..    /
15516 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d  * The rollback m
15517 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79 65  ay have destroye
15518 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61 44  d the pPage1->aD
15519 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20  ata value.  So. 
1551a 20 20 20 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74     ** call sqlit
1551b 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 29  e3BtreeGetPage()
1551c 20 6f 6e 20 70 61 67 65 20 31 20 61 67 61 69 6e   on page 1 again
1551d 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20   to make.    ** 
1551e 73 75 72 65 20 70 50 61 67 65 31 2d 3e 61 44 61  sure pPage1->aDa
1551f 74 61 20 69 73 20 73 65 74 20 63 6f 72 72 65 63  ta is set correc
15520 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  tly. */.    if( 
15521 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
15522 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61  age(pBt, 1, &pPa
15523 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f  ge1, 0)==SQLITE_
15524 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  OK ){.      rele
15525 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b  asePage(pPage1);
15526 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
15527 74 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72  t( countWriteCur
15528 73 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a  sors(pBt)==0 );.
15529 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73      pBt->inTrans
1552a 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52  action = TRANS_R
1552b 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  EAD;.  }..  if( 
1552c 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e  p->inTrans!=TRAN
1552d 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73  S_NONE ){.    as
1552e 73 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e  sert( pBt->nTran
1552f 73 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20  saction>0 );.   
15530 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69   pBt->nTransacti
15531 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66 28 20 30 3d  on--;.    if( 0=
15532 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69  =pBt->nTransacti
15533 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d  on ){.      pBt-
15534 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d  >inTransaction =
15535 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20   TRANS_NONE;.   
15536 20 7d 0a 20 20 7d 0a 0a 20 20 70 2d 3e 69 6e 54   }.  }..  p->inT
15537 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e  rans = TRANS_NON
15538 45 3b 0a 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74  E;.  pBt->inStmt
15539 20 3d 20 30 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74   = 0;.  unlockBt
1553a 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
1553b 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72  ;..  btreeIntegr
1553c 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65  ity(p);.  sqlite
1553d 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1553e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1553f 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74  /*.** Start a st
15540 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73  atement subtrans
15541 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 73 75 62  action.  The sub
15542 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 0a  transaction can.
15543 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64  ** can be rolled
15544 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e   back independen
15545 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  tly of the main 
15546 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20  transaction..** 
15547 59 6f 75 20 6d 75 73 74 20 73 74 61 72 74 20 61  You must start a
15548 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66   transaction bef
15549 6f 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73  ore starting a s
1554a 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  ubtransaction..*
1554b 2a 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63  * The subtransac
1554c 74 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75  tion is ended au
1554d 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 66 20 74  tomatically if t
1554e 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74  he main transact
1554f 69 6f 6e 0a 2a 2a 20 63 6f 6d 6d 69 74 73 20 6f  ion.** commits o
15550 72 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a  r rolls back..**
15551 0a 2a 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 75 62  .** Only one sub
15552 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20  transaction may 
15553 62 65 20 61 63 74 69 76 65 20 61 74 20 61 20 74  be active at a t
15554 69 6d 65 2e 20 20 49 74 20 69 73 20 61 6e 20 65  ime.  It is an e
15555 72 72 6f 72 20 74 6f 20 74 72 79 0a 2a 2a 20 74  rror to try.** t
15556 6f 20 73 74 61 72 74 20 61 20 6e 65 77 20 73 75  o start a new su
15557 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20  btransaction if 
15558 61 6e 6f 74 68 65 72 20 73 75 62 74 72 61 6e 73  another subtrans
15559 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64  action is alread
1555a 79 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  y active..**.** 
1555b 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61  Statement subtra
1555c 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73  nsactions are us
1555d 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69  ed around indivi
1555e 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65  dual SQL stateme
1555f 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  nts.** that are 
15560 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
15561 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49   a BEGIN...COMMI
15562 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63  T block.  If a c
15563 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72  onstraint.** err
15564 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  or occurs within
15565 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20   the statement, 
15566 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68  the effect of th
15567 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74  at one statement
15568 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65  .** can be rolle
15569 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68  d back without h
1556a 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63  aving to rollbac
1556b 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61  k the entire tra
1556c 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nsaction..*/.SQL
1556d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1556e 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
1556f 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70 29 7b  nStmt(Btree *p){
15570 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53  .  int rc;.  BtS
15571 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
15572 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  pBt;.  sqlite3Bt
15573 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70  reeEnter(p);.  p
15574 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
15575 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54    assert( p->inT
15576 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
15577 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  E );.  assert( !
15578 70 42 74 2d 3e 69 6e 53 74 6d 74 20 29 3b 0a 20  pBt->inStmt );. 
15579 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65   assert( pBt->re
1557a 61 64 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 69  adOnly==0 );.  i
1557b 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e 54 72  f( NEVER(p->inTr
1557c 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans!=TRANS_WRITE
1557d 20 7c 7c 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20   || pBt->inStmt 
1557e 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79  || pBt->readOnly
1557f 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  ) ){.    rc = SQ
15580 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a 20  LITE_INTERNAL;. 
15581 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
15582 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73  rt( pBt->inTrans
15583 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
15584 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a 20 41 74  ITE );.    /* At
15585 20 74 68 65 20 70 61 67 65 72 20 6c 65 76 65 6c   the pager level
15586 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72  , a statement tr
15587 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 20 73  ansaction is a s
15588 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 20 20  avepoint with.  
15589 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67 72    ** an index gr
1558a 65 61 74 65 72 20 74 68 61 6e 20 61 6c 6c 20 73  eater than all s
1558b 61 76 65 70 6f 69 6e 74 73 20 63 72 65 61 74 65  avepoints create
1558c 64 20 65 78 70 6c 69 63 69 74 6c 79 20 75 73 69  d explicitly usi
1558d 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c 20 73 74  ng.    ** SQL st
1558e 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20  atements. It is 
1558f 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e 2c  illegal to open,
15590 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c   release or roll
15591 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 2a 2a 20  back any.    ** 
15592 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73 20  such savepoints 
15593 77 68 69 6c 65 20 74 68 65 20 73 74 61 74 65 6d  while the statem
15594 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
15595 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63 74  savepoint is act
15596 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ive..    */.    
15597 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15598 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70  rOpenSavepoint(p
15599 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64  Bt->pPager, p->d
1559a 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 31 29  b->nSavepoint+1)
1559b 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d  ;.    pBt->inStm
1559c 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c  t = 1;.  }.  sql
1559d 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1559e 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1559f 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20  }../*.** Commit 
155a0 74 68 65 20 73 74 61 74 6d 65 6e 74 20 73 75 62  the statment sub
155a1 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72  transaction curr
155a2 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73  ently in progres
155a3 73 2e 20 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 62  s.  If no.** sub
155a4 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
155a5 63 74 69 76 65 2c 20 74 68 69 73 20 69 73 20 61  ctive, this is a
155a6 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
155a7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
155a8 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
155a9 53 74 6d 74 28 42 74 72 65 65 20 2a 70 29 7b 0a  Stmt(Btree *p){.
155aa 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68    int rc;.  BtSh
155ab 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
155ac 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  Bt;.  sqlite3Btr
155ad 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42  eeEnter(p);.  pB
155ae 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  t->db = p->db;. 
155af 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65   assert( pBt->re
155b0 61 64 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 69  adOnly==0 );.  i
155b1 66 28 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 29  f( pBt->inStmt )
155b2 7b 0a 20 20 20 20 69 6e 74 20 69 53 74 6d 74 70  {.    int iStmtp
155b3 6f 69 6e 74 20 3d 20 70 2d 3e 64 62 2d 3e 6e 53  oint = p->db->nS
155b4 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 72 63  avepoint;.    rc
155b5 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53   = sqlite3PagerS
155b6 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50  avepoint(pBt->pP
155b7 61 67 65 72 2c 20 53 41 56 45 50 4f 49 4e 54 5f  ager, SAVEPOINT_
155b8 52 45 4c 45 41 53 45 2c 20 69 53 74 6d 74 70 6f  RELEASE, iStmtpo
155b9 69 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  int);.  }else{. 
155ba 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
155bb 4b 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 69 6e  K;.  }.  pBt->in
155bc 53 74 6d 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69  Stmt = 0;.  sqli
155bd 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
155be 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
155bf 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b  ../*.** Rollback
155c0 20 74 68 65 20 61 63 74 69 76 65 20 73 74 61 74   the active stat
155c1 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63  ement subtransac
155c2 74 69 6f 6e 2e 20 20 49 66 20 6e 6f 20 73 75 62  tion.  If no sub
155c3 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69  transaction.** i
155c4 73 20 61 63 74 69 76 65 20 74 68 69 73 20 72 6f  s active this ro
155c5 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
155c6 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 63 75 72 73  ..**.** All curs
155c7 6f 72 73 20 77 69 6c 6c 20 62 65 20 69 6e 76 61  ors will be inva
155c8 6c 69 64 61 74 65 64 20 62 79 20 74 68 69 73 20  lidated by this 
155c9 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20  operation.  Any 
155ca 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 75 73  attempt.** to us
155cb 65 20 61 20 63 75 72 73 6f 72 20 74 68 61 74 20  e a cursor that 
155cc 77 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65 20  was open at the 
155cd 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69  beginning of thi
155ce 73 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 77  s operation.** w
155cf 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 6e  ill result in an
155d0 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54   error..*/.SQLIT
155d1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
155d2 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
155d3 63 6b 53 74 6d 74 28 42 74 72 65 65 20 2a 70 29  ckStmt(Btree *p)
155d4 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
155d5 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72  ITE_OK;.  BtShar
155d6 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
155d7 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
155d8 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d  Enter(p);.  pBt-
155d9 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61  >db = p->db;.  a
155da 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64  ssert( pBt->read
155db 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28  Only==0 );.  if(
155dc 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 29 7b 0a   pBt->inStmt ){.
155dd 20 20 20 20 69 6e 74 20 69 53 74 6d 74 70 6f 69      int iStmtpoi
155de 6e 74 20 3d 20 70 2d 3e 64 62 2d 3e 6e 53 61 76  nt = p->db->nSav
155df 65 70 6f 69 6e 74 3b 0a 20 20 20 20 72 63 20 3d  epoint;.    rc =
155e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76   sqlite3PagerSav
155e1 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67  epoint(pBt->pPag
155e2 65 72 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  er, SAVEPOINT_RO
155e3 4c 4c 42 41 43 4b 2c 20 69 53 74 6d 74 70 6f 69  LLBACK, iStmtpoi
155e4 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  nt);.    if( rc=
155e5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
155e6 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
155e7 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70  PagerSavepoint(p
155e8 42 74 2d 3e 70 50 61 67 65 72 2c 20 53 41 56 45  Bt->pPager, SAVE
155e9 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69  POINT_RELEASE, i
155ea 53 74 6d 74 70 6f 69 6e 74 29 3b 0a 20 20 20 20  Stmtpoint);.    
155eb 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d  }.    pBt->inStm
155ec 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  t = 0;.  }.  sql
155ed 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
155ee 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
155ef 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63  }../*.** The sec
155f0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
155f1 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6f  this function, o
155f2 70 2c 20 69 73 20 61 6c 77 61 79 73 20 53 41 56  p, is always SAV
155f3 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a  EPOINT_ROLLBACK.
155f4 2a 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f  ** or SAVEPOINT_
155f5 52 45 4c 45 41 53 45 2e 20 54 68 69 73 20 66 75  RELEASE. This fu
155f6 6e 63 74 69 6f 6e 20 65 69 74 68 65 72 20 72 65  nction either re
155f7 6c 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c 73 20  leases or rolls 
155f8 62 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61 76 65  back the.** save
155f9 70 6f 69 6e 74 20 69 64 65 6e 74 69 66 69 65 64  point identified
155fa 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 69 53   by parameter iS
155fb 61 76 65 70 6f 69 6e 74 2c 20 64 65 70 65 6e 64  avepoint, depend
155fc 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  ing on the value
155fd 20 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a   .** of op..**.*
155fe 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53 61 76  * Normally, iSav
155ff 65 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65  epoint is greate
15600 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
15601 74 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76 65 72  to zero. However
15602 2c 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20 53 41  , if op is.** SA
15603 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
15604 2c 20 74 68 65 6e 20 69 53 61 76 65 70 6f 69 6e  , then iSavepoin
15605 74 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 2d 31  t may also be -1
15606 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
15607 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20  he .** contents 
15608 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 72  of the entire tr
15609 61 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 72 6f  ansaction are ro
1560a 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20  lled back. This 
1560b 69 73 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20  is different.** 
1560c 66 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 74 72  from a normal tr
1560d 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 62 61  ansaction rollba
1560e 63 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b 73 20  ck, as no locks 
1560f 61 72 65 20 72 65 6c 65 61 73 65 64 20 61 6e 64  are released and
15610 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74   the.** transact
15611 69 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e  ion remains open
15612 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
15613 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
15614 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74  treeSavepoint(Bt
15615 72 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20  ree *p, int op, 
15616 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b  int iSavepoint){
15617 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
15618 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 26  TE_OK;.  if( p &
15619 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  & p->inTrans==TR
1561a 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20  ANS_WRITE ){.   
1561b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1561c 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73   p->pBt;.    ass
1561d 65 72 74 28 20 70 42 74 2d 3e 69 6e 53 74 6d 74  ert( pBt->inStmt
1561e 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
1561f 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54  t( op==SAVEPOINT
15620 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d  _RELEASE || op==
15621 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
15622 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  CK );.    assert
15623 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20  ( iSavepoint>=0 
15624 7c 7c 20 28 69 53 61 76 65 70 6f 69 6e 74 3d 3d  || (iSavepoint==
15625 2d 31 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f  -1 && op==SAVEPO
15626 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29 3b  INT_ROLLBACK) );
15627 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
15628 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 70  eEnter(p);.    p
15629 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
1562a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1562b 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70  PagerSavepoint(p
1562c 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 2c 20  Bt->pPager, op, 
1562d 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20  iSavepoint);.   
1562e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1562f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
15630 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74   newDatabase(pBt
15631 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
15632 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
15633 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
15634 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  rc;.}../*.** Cre
15635 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72  ate a new cursor
15636 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 20 77   for the BTree w
15637 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20  hose root is on 
15638 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62  the page.** iTab
15639 6c 65 2e 20 20 54 68 65 20 61 63 74 20 6f 66 20  le.  The act of 
1563a 61 63 71 75 69 72 69 6e 67 20 61 20 63 75 72 73  acquiring a curs
1563b 6f 72 20 67 65 74 73 20 61 20 72 65 61 64 20 6c  or gets a read l
1563c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 74 68 65 20 64  ock on .** the d
1563d 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
1563e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 30  .** If wrFlag==0
1563f 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f  , then the curso
15640 72 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73  r can only be us
15641 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a  ed for reading..
15642 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 31 2c  ** If wrFlag==1,
15643 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   then the cursor
15644 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72   can be used for
15645 20 72 65 61 64 69 6e 67 20 6f 72 20 66 6f 72 0a   reading or for.
15646 2a 2a 20 77 72 69 74 69 6e 67 20 69 66 20 6f 74  ** writing if ot
15647 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 66  her conditions f
15648 6f 72 20 77 72 69 74 69 6e 67 20 61 72 65 20 61  or writing are a
15649 6c 73 6f 20 6d 65 74 2e 20 20 54 68 65 73 65 0a  lso met.  These.
1564a 2a 2a 20 61 72 65 20 74 68 65 20 63 6f 6e 64 69  ** are the condi
1564b 74 69 6f 6e 73 20 74 68 61 74 20 6d 75 73 74 20  tions that must 
1564c 62 65 20 6d 65 74 20 69 6e 20 6f 72 64 65 72 20  be met in order 
1564d 66 6f 72 20 77 72 69 74 69 6e 67 20 74 6f 0a 2a  for writing to.*
1564e 2a 20 62 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a  * be allowed:.**
1564f 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63 75 72 73  .** 1:  The curs
15650 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65  or must have bee
15651 6e 20 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72  n opened with wr
15652 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a  Flag==1.**.** 2:
15653 20 20 4f 74 68 65 72 20 64 61 74 61 62 61 73 65    Other database
15654 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61   connections tha
15655 74 20 73 68 61 72 65 20 74 68 65 20 73 61 6d 65  t share the same
15656 20 70 61 67 65 72 20 63 61 63 68 65 0a 2a 2a 20   pager cache.** 
15657 20 20 20 20 62 75 74 20 77 68 69 63 68 20 61 72      but which ar
15658 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 45 41  e not in the REA
15659 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 73 74  D_UNCOMMITTED st
1565a 61 74 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65  ate may not have
1565b 0a 2a 2a 20 20 20 20 20 63 75 72 73 6f 72 73 20  .**     cursors 
1565c 6f 70 65 6e 20 77 69 74 68 20 77 72 46 6c 61 67  open with wrFlag
1565d 3d 3d 30 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  ==0 on the same 
1565e 74 61 62 6c 65 2e 20 20 4f 74 68 65 72 77 69 73  table.  Otherwis
1565f 65 0a 2a 2a 20 20 20 20 20 74 68 65 20 63 68 61  e.**     the cha
15660 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 69  nges made by thi
15661 73 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 77  s write cursor w
15662 6f 75 6c 64 20 62 65 20 76 69 73 69 62 6c 65 20  ould be visible 
15663 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20 72 65  to.**     the re
15664 61 64 20 63 75 72 73 6f 72 73 20 69 6e 20 74 68  ad cursors in th
15665 65 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  e other database
15666 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a   connection..**.
15667 2a 2a 20 33 3a 20 20 54 68 65 20 64 61 74 61 62  ** 3:  The datab
15668 61 73 65 20 6d 75 73 74 20 62 65 20 77 72 69 74  ase must be writ
15669 61 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 65 61  able (not on rea
1566a 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 0a 2a 2a  d-only media).**
1566b 0a 2a 2a 20 34 3a 20 20 54 68 65 72 65 20 6d 75  .** 4:  There mu
1566c 73 74 20 62 65 20 61 6e 20 61 63 74 69 76 65 20  st be an active 
1566d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
1566e 2a 2a 20 4e 6f 20 63 68 65 63 6b 69 6e 67 20 69  ** No checking i
1566f 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 73  s done to make s
15670 75 72 65 20 74 68 61 74 20 70 61 67 65 20 69 54  ure that page iT
15671 61 62 6c 65 20 72 65 61 6c 6c 79 20 69 73 20 74  able really is t
15672 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20  he.** root page 
15673 6f 66 20 61 20 62 2d 74 72 65 65 2e 20 20 49 66  of a b-tree.  If
15674 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 6e   it is not, then
15675 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 71 75   the cursor acqu
15676 69 72 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74  ired.** will not
15677 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 2e   work correctly.
15678 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73  .**.** It is ass
15679 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 73 71  umed that the sq
1567a 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
1567b 53 69 7a 65 28 29 20 62 79 74 65 73 20 6f 66 20  Size() bytes of 
1567c 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70 6f 69 6e 74  memory .** point
1567d 65 64 20 74 6f 20 62 79 20 70 43 75 72 20 68 61  ed to by pCur ha
1567e 76 65 20 62 65 65 6e 20 7a 65 72 6f 65 64 20 62  ve been zeroed b
1567f 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f  y the caller..*/
15680 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
15681 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65  eCursor(.  Btree
15682 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
15683 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15684 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a    /* The btree *
15685 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
15686 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15687 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
15688 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ot page of table
15689 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e   to open */.  in
1568a 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20  t wrFlag,       
1568b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1568c 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69       /* 1 to wri
1568d 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20  te. 0 read-only 
1568e 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49  */.  struct KeyI
1568f 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20  nfo *pKeyInfo,  
15690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
15691 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f 6d 70  irst arg to comp
15692 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  arison function 
15693 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  */.  BtCursor *p
15694 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20 20  Cur             
15695 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
15696 70 61 63 65 20 66 6f 72 20 6e 65 77 20 63 75 72  pace for new cur
15697 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  sor */.){.  int 
15698 72 63 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65  rc;.  Pgno nPage
15699 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
1569a 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61  t = p->pBt;..  a
1569b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1569c 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29  reeHoldsMutex(p)
1569d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72   );.  assert( wr
1569e 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61  Flag==0 || wrFla
1569f 67 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 77 72  g==1 );.  if( wr
156a0 46 6c 61 67 20 29 7b 0a 20 20 20 20 61 73 73 65  Flag ){.    asse
156a1 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e  rt( !pBt->readOn
156a2 6c 79 20 29 3b 0a 20 20 20 20 69 66 28 20 4e 45  ly );.    if( NE
156a3 56 45 52 28 70 42 74 2d 3e 72 65 61 64 4f 6e 6c  VER(pBt->readOnl
156a4 79 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  y) ){.      retu
156a5 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e  rn SQLITE_READON
156a6 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  LY;.    }.    if
156a7 28 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73  ( checkReadLocks
156a8 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 2c 20 30  (p, iTable, 0, 0
156a9 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
156aa 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b  n SQLITE_LOCKED;
156ab 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
156ac 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30  ( pBt->pPage1==0
156ad 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6c 6f 63   ){.    rc = loc
156ae 6b 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28  kBtreeWithRetry(
156af 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  p);.    if( rc!=
156b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
156b1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
156b2 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e    }.  }.  pCur->
156b3 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f  pgnoRoot = (Pgno
156b4 29 69 54 61 62 6c 65 3b 0a 20 20 72 63 20 3d 20  )iTable;.  rc = 
156b5 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
156b6 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65  count(pBt->pPage
156b7 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50 61 67 65  r, (int *)&nPage
156b8 29 3b 20 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ); .  if( rc!=SQ
156b9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
156ba 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
156bb 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 26  if( iTable==1 &&
156bc 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20   nPage==0 ){.   
156bd 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 4d 50   rc = SQLITE_EMP
156be 54 59 3b 0a 20 20 20 20 67 6f 74 6f 20 63 72 65  TY;.    goto cre
156bf 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70  ate_cursor_excep
156c0 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  tion;.  }.  rc =
156c1 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28   getAndInitPage(
156c2 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52  pBt, pCur->pgnoR
156c3 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 50 61  oot, &pCur->apPa
156c4 67 65 5b 30 5d 29 3b 0a 20 20 69 66 28 20 72 63  ge[0]);.  if( rc
156c5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
156c6 20 20 20 67 6f 74 6f 20 63 72 65 61 74 65 5f 63     goto create_c
156c7 75 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3b  ursor_exception;
156c8 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74  .  }..  /* Now t
156c9 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72  hat no other err
156ca 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66  ors can occur, f
156cb 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e  inish filling in
156cc 20 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20   the BtCursor.  
156cd 2a 2a 20 76 61 72 69 61 62 6c 65 73 2c 20 6c 69  ** variables, li
156ce 6e 6b 20 74 68 65 20 63 75 72 73 6f 72 20 69 6e  nk the cursor in
156cf 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 20  to the BtShared 
156d0 6c 69 73 74 20 61 6e 64 20 73 65 74 20 2a 70 70  list and set *pp
156d1 43 75 72 20 28 74 68 65 0a 20 20 2a 2a 20 6f 75  Cur (the.  ** ou
156d2 74 70 75 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  tput argument to
156d3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 29 2e   this function).
156d4 0a 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 4b  .  */.  pCur->pK
156d5 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66  eyInfo = pKeyInf
156d6 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 65  o;.  pCur->pBtre
156d7 65 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70  e = p;.  pCur->p
156d8 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 43 75 72  Bt = pBt;.  pCur
156d9 2d 3e 77 72 46 6c 61 67 20 3d 20 28 75 38 29 77  ->wrFlag = (u8)w
156da 72 46 6c 61 67 3b 0a 20 20 70 43 75 72 2d 3e 70  rFlag;.  pCur->p
156db 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 43 75 72  Next = pBt->pCur
156dc 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  sor;.  if( pCur-
156dd 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 43  >pNext ){.    pC
156de 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76  ur->pNext->pPrev
156df 20 3d 20 70 43 75 72 3b 0a 20 20 7d 0a 20 20 70   = pCur;.  }.  p
156e0 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43  Bt->pCursor = pC
156e1 75 72 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61  ur;.  pCur->eSta
156e2 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41  te = CURSOR_INVA
156e3 4c 49 44 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  LID;..  return S
156e4 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 63 72 65 61 74  QLITE_OK;..creat
156e5 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70 74 69  e_cursor_excepti
156e6 6f 6e 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67  on:.  releasePag
156e7 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  e(pCur->apPage[0
156e8 5d 29 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65  ]);.  unlockBtre
156e9 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
156ea 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
156eb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
156ec 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
156ed 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70  rsor(.  Btree *p
156ee 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
156ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156f0 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65      /* The btree
156f1 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
156f2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
156f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156f4 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
156f5 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  f table to open 
156f6 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c  */.  int wrFlag,
156f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156f9 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20   /* 1 to write. 
156fa 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20  0 read-only */. 
156fb 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20   struct KeyInfo 
156fc 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20  *pKeyInfo,      
156fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
156fe 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f  First arg to xCo
156ff 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43  mpare() */.  BtC
15700 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20  ursor *pCur     
15701 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15702 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
15703 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72  e new cursor her
15704 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
15705 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
15706 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 2d 3e 70  Enter(p);.  p->p
15707 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
15708 20 20 72 63 20 3d 20 62 74 72 65 65 43 75 72 73    rc = btreeCurs
15709 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c 20 77 72  or(p, iTable, wr
1570a 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66 6f 2c 20  Flag, pKeyInfo, 
1570b 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  pCur);.  sqlite3
1570c 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
1570d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51   return rc;.}.SQ
1570e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1570f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
15710 73 6f 72 53 69 7a 65 28 29 7b 0a 20 20 72 65 74  sorSize(){.  ret
15711 75 72 6e 20 73 69 7a 65 6f 66 28 42 74 43 75 72  urn sizeof(BtCur
15712 73 6f 72 29 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a  sor);.}..../*.**
15713 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e   Close a cursor.
15714 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20    The read lock 
15715 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
15716 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 64  file is released
15717 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73  .** when the las
15718 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73  t cursor is clos
15719 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1571a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1571b 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f  3BtreeCloseCurso
1571c 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  r(BtCursor *pCur
1571d 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72  ){.  Btree *pBtr
1571e 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65  ee = pCur->pBtre
1571f 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 20  e;.  if( pBtree 
15720 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
15721 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
15722 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 20  = pCur->pBt;.   
15723 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
15724 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20  er(pBtree);.    
15725 70 42 74 2d 3e 64 62 20 3d 20 70 42 74 72 65 65  pBt->db = pBtree
15726 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65  ->db;.    sqlite
15727 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f  3BtreeClearCurso
15728 72 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28  r(pCur);.    if(
15729 20 70 43 75 72 2d 3e 70 50 72 65 76 20 29 7b 0a   pCur->pPrev ){.
1572a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 50 72 65        pCur->pPre
1572b 76 2d 3e 70 4e 65 78 74 20 3d 20 70 43 75 72 2d  v->pNext = pCur-
1572c 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73  >pNext;.    }els
1572d 65 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 43  e{.      pBt->pC
1572e 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e  ursor = pCur->pN
1572f 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ext;.    }.    i
15730 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29  f( pCur->pNext )
15731 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4e  {.      pCur->pN
15732 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75  ext->pPrev = pCu
15733 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d 0a  r->pPrev;.    }.
15734 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d      for(i=0; i<=
15735 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b  pCur->iPage; i++
15736 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
15737 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67  Page(pCur->apPag
15738 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  e[i]);.    }.   
15739 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
1573a 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 69  used(pBt);.    i
1573b 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f  nvalidateOverflo
1573c 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20  wCache(pCur);.  
1573d 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72 65    /* sqlite3_fre
1573e 65 28 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20 20  e(pCur); */.    
1573f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
15740 65 28 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20  e(pBtree);.  }. 
15741 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
15742 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  K;.}../*.** Make
15743 20 61 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72   a temporary cur
15744 73 6f 72 20 62 79 20 66 69 6c 6c 69 6e 67 20 69  sor by filling i
15745 6e 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20  n the fields of 
15746 70 54 65 6d 70 43 75 72 2e 0a 2a 2a 20 54 68 65  pTempCur..** The
15747 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f   temporary curso
15748 72 20 69 73 20 6e 6f 74 20 6f 6e 20 74 68 65 20  r is not on the 
15749 63 75 72 73 6f 72 20 6c 69 73 74 20 66 6f 72 20  cursor list for 
1574a 74 68 65 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51  the Btree..*/.SQ
1574b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1574c 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  d sqlite3BtreeGe
1574d 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75  tTempCursor(BtCu
1574e 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74 43 75  rsor *pCur, BtCu
1574f 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72 29 7b  rsor *pTempCur){
15750 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
15751 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
15752 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
15753 6d 65 6d 63 70 79 28 70 54 65 6d 70 43 75 72 2c  memcpy(pTempCur,
15754 20 70 43 75 72 2c 20 73 69 7a 65 6f 66 28 42 74   pCur, sizeof(Bt
15755 43 75 72 73 6f 72 29 29 3b 0a 20 20 70 54 65 6d  Cursor));.  pTem
15756 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  pCur->pNext = 0;
15757 0a 20 20 70 54 65 6d 70 43 75 72 2d 3e 70 50 72  .  pTempCur->pPr
15758 65 76 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  ev = 0;.  for(i=
15759 30 3b 20 69 3c 3d 70 54 65 6d 70 43 75 72 2d 3e  0; i<=pTempCur->
1575a 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  iPage; i++){.   
1575b 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
1575c 28 70 54 65 6d 70 43 75 72 2d 3e 61 70 50 61 67  (pTempCur->apPag
1575d 65 5b 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b 0a  e[i]->pDbPage);.
1575e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54    }.  assert( pT
1575f 65 6d 70 43 75 72 2d 3e 70 4b 65 79 3d 3d 30 20  empCur->pKey==0 
15760 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  );.}../*.** Dele
15761 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 63  te a temporary c
15762 75 72 73 6f 72 20 73 75 63 68 20 61 73 20 77 61  ursor such as wa
15763 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 43 72  s made by the Cr
15764 65 61 74 65 54 65 6d 70 6f 72 61 72 79 43 75 72  eateTemporaryCur
15765 73 6f 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f  sor().** functio
15766 6e 20 61 62 6f 76 65 2e 0a 2a 2f 0a 53 51 4c 49  n above..*/.SQLI
15767 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
15768 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 6c 65  sqlite3BtreeRele
15769 61 73 65 54 65 6d 70 43 75 72 73 6f 72 28 42 74  aseTempCursor(Bt
1576a 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
1576b 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
1576c 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
1576d 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 66 6f  ex(pCur) );.  fo
1576e 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e  r(i=0; i<=pCur->
1576f 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  iPage; i++){.   
15770 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
15771 65 66 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  ef(pCur->apPage[
15772 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  i]->pDbPage);.  
15773 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
15774 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 7d 0a  (pCur->pKey);.}.
15775 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
15776 20 74 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67   the BtCursor* g
15777 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75  iven in the argu
15778 6d 65 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64  ment has a valid
15779 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66  .** BtCursor.inf
1577a 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66  o structure.  If
1577b 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61   it is not alrea
1577c 64 79 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a  dy valid, call.*
1577d 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  * sqlite3BtreePa
1577e 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c  rseCell() to fil
1577f 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42  l it in..**.** B
15780 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20  tCursor.info is 
15781 61 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 69  a cache of the i
15782 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68  nformation in th
15783 65 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a  e current cell..
15784 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 20 63 61  ** Using this ca
15785 63 68 65 20 72 65 64 75 63 65 73 20 74 68 65 20  che reduces the 
15786 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20  number of calls 
15787 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  to sqlite3BtreeP
15788 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a  arseCell()..**.*
15789 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54  * 2007-06-25:  T
1578a 68 65 72 65 20 69 73 20 61 20 62 75 67 20 69 6e  here is a bug in
1578b 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f   some versions o
1578c 66 20 4d 53 56 43 20 74 68 61 74 20 63 61 75 73  f MSVC that caus
1578d 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65  e the.** compile
1578e 72 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20  r to crash when 
1578f 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73  getCellInfo() is
15790 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
15791 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20  a macro..** But 
15792 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 73 75  there is a measu
15793 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 64 76  reable speed adv
15794 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20  antage to using 
15795 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63  the macro on gcc
15796 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63  .** (when less c
15797 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61  ompiler optimiza
15798 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f  tions like -Os o
15799 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61  r -O0 are used a
1579a 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c  nd the.** compil
1579b 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20  er is not doing 
1579c 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69  agressive inlini
1579d 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20  ng.)  So we use 
1579e 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a  a real function.
1579f 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20  ** for MSVC and 
157a0 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72  a macro for ever
157a1 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69  ything else.  Ti
157a2 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23  cket #2457..*/.#
157a3 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
157a4 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65  static void asse
157a5 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72  rtCellInfo(BtCur
157a6 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20  sor *pCur){.    
157a7 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
157a8 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70     int iPage = p
157a9 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20  Cur->iPage;.    
157aa 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c  memset(&info, 0,
157ab 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a   sizeof(info));.
157ac 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
157ad 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e  ParseCell(pCur->
157ae 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70  apPage[iPage], p
157af 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65  Cur->aiIdx[iPage
157b0 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61  ], &info);.    a
157b1 73 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69  ssert( memcmp(&i
157b2 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f  nfo, &pCur->info
157b3 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d  , sizeof(info))=
157b4 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  =0 );.  }.#else.
157b5 20 20 23 64 65 66 69 6e 65 20 61 73 73 65 72 74    #define assert
157b6 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64  CellInfo(x).#end
157b7 69 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56  if.#ifdef _MSC_V
157b8 45 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65  ER.  /* Use a re
157b9 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d  al function in M
157ba 53 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75  SVC to work arou
157bb 6e 64 20 62 75 67 73 20 69 6e 20 74 68 61 74 20  nd bugs in that 
157bc 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73  compiler. */.  s
157bd 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 65  tatic void getCe
157be 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20  llInfo(BtCursor 
157bf 2a 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20  *pCur){.    if( 
157c0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
157c1 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ==0 ){.      int
157c2 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69   iPage = pCur->i
157c3 50 61 67 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  Page;.      sqli
157c4 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
157c5 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  l(pCur->apPage[i
157c6 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64  Page],pCur->aiId
157c7 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e  x[iPage],&pCur->
157c8 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75  info);.      pCu
157c9 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31  r->validNKey = 1
157ca 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
157cb 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66     assertCellInf
157cc 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20  o(pCur);.    }. 
157cd 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e   }.#else /* if n
157ce 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20  ot _MSC_VER */. 
157cf 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20   /* Use a macro 
157d0 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d  in all other com
157d1 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74  pilers so that t
157d2 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  he function is i
157d3 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e  nlined */.#defin
157d4 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43  e getCellInfo(pC
157d5 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20  ur)             
157d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157d8 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28           \.  if(
157d9 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
157da 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20  e==0 ){         
157db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157dd 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
157de 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72  int iPage = pCur
157df 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20  ->iPage;        
157e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157e2 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
157e3 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
157e4 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50  seCell(pCur->apP
157e5 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d  age[iPage],pCur-
157e6 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70  >aiIdx[iPage],&p
157e7 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 20  Cur->info); \.  
157e8 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
157e9 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20  y = 1;          
157ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157ec 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
157ed 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20   }else{         
157ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
157f2 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e      assertCellIn
157f3 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20 20  fo(pCur);       
157f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
157f7 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f  .  }.#endif /* _
157f8 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a  MSC_VER */../*.*
157f9 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20  * Set *pSize to 
157fa 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
157fb 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 74 6f  buffer needed to
157fc 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 20   hold the value 
157fd 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f  of.** the key fo
157fe 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 65 6e  r the current en
157ff 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 75 72  try.  If the cur
15800 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74  sor is not point
15801 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69  ing.** to a vali
15802 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20  d entry, *pSize 
15803 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a  is set to 0. .**
15804 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 20  .** For a table 
15805 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 59 20  with the INTKEY 
15806 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 20 72  flag set, this r
15807 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
15808 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66  he key.** itself
15809 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72  , not the number
1580a 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
1580b 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   key..*/.SQLITE_
1580c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1580d 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
1580e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
1580f 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69  i64 *pSize){.  i
15810 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
15811 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
15812 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
15813 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
15814 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
15815 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
15816 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
15817 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
15818 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
15819 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  || pCur->eState=
1581a 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
1581b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65  .    if( pCur->e
1581c 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
1581d 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2a  VALID ){.      *
1581e 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d  pSize = 0;.    }
1581f 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 74 43  else{.      getC
15820 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20  ellInfo(pCur);. 
15821 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 43       *pSize = pC
15822 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20  ur->info.nKey;. 
15823 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
15824 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
15825 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65  et *pSize to the
15826 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
15827 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68 65 20   of data in the 
15828 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 72  entry the.** cur
15829 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  sor currently po
1582a 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77 61 79 73  ints to.  Always
1582b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1582c 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73  K..** Failure is
1582d 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20   not possible.  
1582e 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  If the cursor is
1582f 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 0a 2a   not currently.*
15830 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e  * pointing to an
15831 20 65 6e 74 72 79 20 28 77 68 69 63 68 20 63 61   entry (which ca
15832 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 20 65 78  n happen, for ex
15833 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 74 68 65  ample, if.** the
15834 20 64 61 74 61 62 61 73 65 20 69 73 20 65 6d 70   database is emp
15835 74 79 29 20 74 68 65 6e 20 2a 70 53 69 7a 65 20  ty) then *pSize 
15836 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a  is set to 0..*/.
15837 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15838 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
15839 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72  ataSize(BtCursor
1583a 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69   *pCur, u32 *pSi
1583b 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  ze){.  int rc;..
1583c 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
1583d 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
1583e 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f   );.  rc = resto
1583f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
15840 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
15841 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
15842 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
15843 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
15844 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d  INVALID || pCur-
15845 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
15846 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 28  VALID );.    if(
15847 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
15848 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
15849 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 70 6f  .      /* Not po
1584a 69 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69  inting at a vali
1584b 64 20 65 6e 74 72 79 20 2d 20 73 65 74 20 2a 70  d entry - set *p
1584c 53 69 7a 65 20 74 6f 20 30 2e 20 2a 2f 0a 20 20  Size to 0. */.  
1584d 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a      *pSize = 0;.
1584e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1584f 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75   getCellInfo(pCu
15850 72 29 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65  r);.      *pSize
15851 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44   = pCur->info.nD
15852 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ata;.    }.  }. 
15853 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
15854 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70  *.** Given the p
15855 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e  age number of an
15856 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
15857 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28  n the database (
15858 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66  parameter.** ovf
15859 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  l), this functio
1585a 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67 65  n finds the page
1585b 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e   number of the n
1585c 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ext page in the 
1585d 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  .** linked list 
1585e 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  of overflow page
1585f 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20  s. If possible, 
15860 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74 6f  it uses the auto
15861 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74  -vacuum.** point
15862 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 74  er-map data inst
15863 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20 74  ead of reading t
15864 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61  he content of pa
15865 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f  ge ovfl to do so
15866 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  . .**.** If an e
15867 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 53  rror occurs an S
15868 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
15869 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
1586a 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 55  herwise:.**.** U
1586b 6e 6c 65 73 73 20 70 50 67 6e 6f 4e 65 78 74 20  nless pPgnoNext 
1586c 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 70 61 67  is NULL, the pag
1586d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
1586e 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 0a 2a  next overflow .*
1586f 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69  * page in the li
15870 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 77 72 69  nked list is wri
15871 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e 65  tten to *pPgnoNe
15872 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66 6c  xt. If page ovfl
15873 0a 2a 2a 20 69 73 20 74 68 65 20 6c 61 73 74 20  .** is the last 
15874 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b  page in its link
15875 65 64 20 6c 69 73 74 2c 20 2a 70 50 67 6e 6f 4e  ed list, *pPgnoN
15876 65 78 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65  ext is set to ze
15877 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70 70  ro. .**.** If pp
15878 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  Page is not NULL
15879 2c 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74  , *ppPage is set
1587a 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 65 2a   to the MemPage*
1587b 20 68 61 6e 64 6c 65 0a 2a 2a 20 66 6f 72 20 70   handle.** for p
1587c 61 67 65 20 6f 76 66 6c 2e 20 54 68 65 20 75 6e  age ovfl. The un
1587d 64 65 72 6c 79 69 6e 67 20 70 61 67 65 72 20 70  derlying pager p
1587e 61 67 65 20 6d 61 79 20 68 61 76 65 20 62 65 65  age may have bee
1587f 6e 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 77  n requested.** w
15880 69 74 68 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e  ith the noConten
15881 74 20 66 6c 61 67 20 73 65 74 2c 20 73 6f 20 74  t flag set, so t
15882 68 65 20 70 61 67 65 20 64 61 74 61 20 61 63 63  he page data acc
15883 65 73 73 61 62 6c 65 20 76 69 61 0a 2a 2a 20 74  essable via.** t
15884 68 69 73 20 68 61 6e 64 6c 65 20 6d 61 79 20 6e  his handle may n
15885 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 0a 2a  ot be trusted..*
15886 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
15887 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 20  OverflowPage(.  
15888 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a  BtShared *pBt, .
15889 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20    Pgno ovfl,    
1588a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1588b 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  * Overflow page 
1588c 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70  */.  MemPage **p
1588d 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  pPage,          
1588e 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67    /* OUT: MemPag
1588f 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67  e handle */.  Pg
15890 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20  no *pPgnoNext   
15891 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
15892 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77  T: Next overflow
15893 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
15894 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d  ){.  Pgno next =
15895 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   0;.  int rc = S
15896 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73  QLITE_OK;..  ass
15897 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15898 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
15899 65 78 29 20 29 3b 0a 20 20 2f 2a 20 4f 6e 65 20  ex) );.  /* One 
1589a 6f 66 20 74 68 65 73 65 20 6d 75 73 74 20 6e 6f  of these must no
1589b 74 20 62 65 20 4e 55 4c 4c 2e 20 4f 74 68 65 72  t be NULL. Other
1589c 77 69 73 65 2c 20 77 68 79 20 63 61 6c 6c 20 74  wise, why call t
1589d 68 69 73 20 66 75 6e 63 74 69 6f 6e 3f 20 2a 2f  his function? */
1589e 0a 20 20 61 73 73 65 72 74 28 70 70 50 61 67 65  .  assert(ppPage
1589f 20 7c 7c 20 70 50 67 6e 6f 4e 65 78 74 29 3b 0a   || pPgnoNext);.
158a0 0a 20 20 2f 2a 20 49 66 20 70 50 67 6e 6f 4e 65  .  /* If pPgnoNe
158a1 78 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  xt is NULL, then
158a2 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
158a3 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 74  s being called t
158a4 6f 20 6f 62 74 61 69 6e 0a 20 20 2a 2a 20 61 20  o obtain.  ** a 
158a5 4d 65 6d 50 61 67 65 2a 20 72 65 66 65 72 65 6e  MemPage* referen
158a6 63 65 20 6f 6e 6c 79 2e 20 4e 6f 20 70 61 67 65  ce only. No page
158a7 2d 64 61 74 61 20 69 73 20 72 65 71 75 69 72 65  -data is require
158a8 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  d in this case..
158a9 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 67 6e    */.  if( !pPgn
158aa 6f 4e 65 78 74 20 29 7b 0a 20 20 20 20 72 65 74  oNext ){.    ret
158ab 75 72 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65  urn sqlite3Btree
158ac 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66  GetPage(pBt, ovf
158ad 6c 2c 20 70 70 50 61 67 65 2c 20 31 29 3b 0a 20  l, ppPage, 1);. 
158ae 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
158af 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
158b0 55 4d 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66  UM.  /* Try to f
158b1 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 61 67  ind the next pag
158b2 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f  e in the overflo
158b3 77 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65  w list using the
158b4 0a 20 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d  .  ** autovacuum
158b5 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
158b6 65 73 2e 20 47 75 65 73 73 20 74 68 61 74 20 74  es. Guess that t
158b7 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
158b8 0a 20 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c  .  ** the overfl
158b9 6f 77 20 6c 69 73 74 20 69 73 20 70 61 67 65 20  ow list is page 
158ba 6e 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e  number (ovfl+1).
158bb 20 49 66 20 74 68 61 74 20 67 75 65 73 73 20 74   If that guess t
158bc 75 72 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74  urns .  ** out t
158bd 6f 20 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c  o be wrong, fall
158be 20 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67   back to loading
158bf 20 74 68 65 20 64 61 74 61 20 6f 66 20 70 61 67   the data of pag
158c0 65 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f  e .  ** number o
158c1 76 66 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  vfl to determine
158c2 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e   the next page n
158c3 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  umber..  */.  if
158c4 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
158c5 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67  m ){.    Pgno pg
158c6 6e 6f 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75  no;.    Pgno iGu
158c7 65 73 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20  ess = ovfl+1;.  
158c8 20 20 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20    u8 eType;..   
158c9 20 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49   while( PTRMAP_I
158ca 53 50 41 47 45 28 70 42 74 2c 20 69 47 75 65 73  SPAGE(pBt, iGues
158cb 73 29 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45  s) || iGuess==PE
158cc 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
158cd 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47  pBt) ){.      iG
158ce 75 65 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20  uess++;.    }.. 
158cf 20 20 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70     if( iGuess<=p
158d0 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
158d1 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  t) ){.      rc =
158d2 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20   ptrmapGet(pBt, 
158d3 69 47 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20  iGuess, &eType, 
158d4 26 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66  &pgno);.      if
158d5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
158d6 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
158d7 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
158d8 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50      if( eType==P
158d9 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20  TRMAP_OVERFLOW2 
158da 26 26 20 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29 7b  && pgno==ovfl ){
158db 0a 20 20 20 20 20 20 20 20 6e 65 78 74 20 3d 20  .        next = 
158dc 69 47 75 65 73 73 3b 0a 20 20 20 20 20 20 7d 0a  iGuess;.      }.
158dd 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
158de 0a 0a 20 20 69 66 28 20 6e 65 78 74 3d 3d 30 20  ..  if( next==0 
158df 7c 7c 20 70 70 50 61 67 65 20 29 7b 0a 20 20 20  || ppPage ){.   
158e0 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
158e1 3d 20 30 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73  = 0;..    rc = s
158e2 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
158e3 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 70  ge(pBt, ovfl, &p
158e4 50 61 67 65 2c 20 6e 65 78 74 21 3d 30 29 3b 0a  Page, next!=0);.
158e5 20 20 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53      assert(rc==S
158e6 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67  QLITE_OK || pPag
158e7 65 3d 3d 30 29 3b 0a 20 20 20 20 69 66 28 20 6e  e==0);.    if( n
158e8 65 78 74 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51  ext==0 && rc==SQ
158e9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
158ea 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74 65   next = get4byte
158eb 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a  (pPage->aData);.
158ec 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70      }..    if( p
158ed 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 2a  pPage ){.      *
158ee 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a  ppPage = pPage;.
158ef 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
158f0 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
158f1 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ge);.    }.  }. 
158f2 20 2a 70 50 67 6e 6f 4e 65 78 74 20 3d 20 6e 65   *pPgnoNext = ne
158f3 78 74 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63  xt;..  return rc
158f4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20  ;.}../*.** Copy 
158f5 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66  data from a buff
158f6 65 72 20 74 6f 20 61 20 70 61 67 65 2c 20 6f 72  er to a page, or
158f7 20 66 72 6f 6d 20 61 20 70 61 67 65 20 74 6f 20   from a page to 
158f8 61 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20  a buffer..**.** 
158f9 70 50 61 79 6c 6f 61 64 20 69 73 20 61 20 70 6f  pPayload is a po
158fa 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 73 74  inter to data st
158fb 6f 72 65 64 20 6f 6e 20 64 61 74 61 62 61 73 65  ored on database
158fc 20 70 61 67 65 20 70 44 62 50 61 67 65 2e 0a 2a   page pDbPage..*
158fd 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 65 4f  * If argument eO
158fe 70 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e  p is false, then
158ff 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20   nByte bytes of 
15900 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 0a  data are copied.
15901 2a 2a 20 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64  ** from pPayload
15902 20 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 70   to the buffer p
15903 6f 69 6e 74 65 64 20 61 74 20 62 79 20 70 42 75  ointed at by pBu
15904 66 2e 20 49 66 20 65 4f 70 20 69 73 20 74 72 75  f. If eOp is tru
15905 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74  e,.** then sqlit
15906 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 69  e3PagerWrite() i
15907 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50  s called on pDbP
15908 61 67 65 20 61 6e 64 20 6e 42 79 74 65 20 62 79  age and nByte by
15909 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61  tes.** of data a
1590a 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74  re copied from t
1590b 68 65 20 62 75 66 66 65 72 20 70 42 75 66 20 74  he buffer pBuf t
1590c 6f 20 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a  o pPayload..**.*
1590d 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
1590e 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65  eturned on succe
1590f 73 73 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e  ss, otherwise an
15910 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
15911 73 74 61 74 69 63 20 69 6e 74 20 63 6f 70 79 50  static int copyP
15912 61 79 6c 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a  ayload(.  void *
15913 70 50 61 79 6c 6f 61 64 2c 20 20 20 20 20 20 20  pPayload,       
15914 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
15915 6f 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20  o page data */. 
15916 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20   void *pBuf,    
15917 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
15918 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20  inter to buffer 
15919 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20  */.  int nByte, 
1591a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1591b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1591c 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69  s to copy */.  i
1591d 6e 74 20 65 4f 70 2c 20 20 20 20 20 20 20 20 20  nt eOp,         
1591e 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e           /* 0 ->
1591f 20 63 6f 70 79 20 66 72 6f 6d 20 70 61 67 65 2c   copy from page,
15920 20 31 20 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61   1 -> copy to pa
15921 67 65 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a  ge */.  DbPage *
15922 70 44 62 50 61 67 65 20 20 20 20 20 20 20 20 20  pDbPage         
15923 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69    /* Page contai
15924 6e 69 6e 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f  ning pPayload */
15925 0a 29 7b 0a 20 20 69 66 28 20 65 4f 70 20 29 7b  .){.  if( eOp ){
15926 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74  .    /* Copy dat
15927 61 20 66 72 6f 6d 20 62 75 66 66 65 72 20 74 6f  a from buffer to
15928 20 70 61 67 65 20 28 61 20 77 72 69 74 65 20 6f   page (a write o
15929 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20  peration) */.   
1592a 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
1592b 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50  3PagerWrite(pDbP
1592c 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
1592d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1592e 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1592f 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79      }.    memcpy
15930 28 70 50 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c  (pPayload, pBuf,
15931 20 6e 42 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65   nByte);.  }else
15932 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61  {.    /* Copy da
15933 74 61 20 66 72 6f 6d 20 70 61 67 65 20 74 6f 20  ta from page to 
15934 62 75 66 66 65 72 20 28 61 20 72 65 61 64 20 6f  buffer (a read o
15935 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20  peration) */.   
15936 20 6d 65 6d 63 70 79 28 70 42 75 66 2c 20 70 50   memcpy(pBuf, pP
15937 61 79 6c 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a  ayload, nByte);.
15938 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
15939 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
1593a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1593b 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f  s used to read o
1593c 72 20 6f 76 65 72 77 72 69 74 65 20 70 61 79 6c  r overwrite payl
1593d 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  oad information.
1593e 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79  ** for the entry
1593f 20 74 68 61 74 20 74 68 65 20 70 43 75 72 20 63   that the pCur c
15940 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e  ursor is pointin
15941 67 20 74 6f 2e 20 49 66 20 74 68 65 20 65 4f 70  g to. If the eOp
15942 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73  .** parameter is
15943 20 30 2c 20 74 68 69 73 20 69 73 20 61 20 72 65   0, this is a re
15944 61 64 20 6f 70 65 72 61 74 69 6f 6e 20 28 64 61  ad operation (da
15945 74 61 20 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a  ta copied into.*
15946 2a 20 62 75 66 66 65 72 20 70 42 75 66 29 2e 20  * buffer pBuf). 
15947 49 66 20 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72  If it is non-zer
15948 6f 2c 20 61 20 77 72 69 74 65 20 28 64 61 74 61  o, a write (data
15949 20 63 6f 70 69 65 64 20 66 72 6f 6d 0a 2a 2a 20   copied from.** 
1594a 62 75 66 66 65 72 20 70 42 75 66 29 2e 0a 2a 2a  buffer pBuf)..**
1594b 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 6f 66 20 22  .** A total of "
1594c 61 6d 74 22 20 62 79 74 65 73 20 61 72 65 20 72  amt" bytes are r
1594d 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 20 62  ead or written b
1594e 65 67 69 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66  eginning at "off
1594f 73 65 74 22 2e 0a 2a 2a 20 44 61 74 61 20 69 73  set"..** Data is
15950 20 72 65 61 64 20 74 6f 20 6f 72 20 66 72 6f 6d   read to or from
15951 20 74 68 65 20 62 75 66 66 65 72 20 70 42 75 66   the buffer pBuf
15952 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
15953 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61  tine does not ma
15954 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e  ke a distinction
15955 20 62 65 74 77 65 65 6e 20 6b 65 79 20 61 6e 64   between key and
15956 20 64 61 74 61 2e 0a 2a 2a 20 49 74 20 6a 75 73   data..** It jus
15957 74 20 72 65 61 64 73 20 6f 72 20 77 72 69 74 65  t reads or write
15958 73 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65  s bytes from the
15959 20 70 61 79 6c 6f 61 64 20 61 72 65 61 2e 20 20   payload area.  
1595a 44 61 74 61 20 6d 69 67 68 74 20 0a 2a 2a 20 61  Data might .** a
1595b 70 70 65 61 72 20 6f 6e 20 74 68 65 20 6d 61 69  ppear on the mai
1595c 6e 20 70 61 67 65 20 6f 72 20 62 65 20 73 63 61  n page or be sca
1595d 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75  ttered out on mu
1595e 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20  ltiple overflow 
1595f 0a 2a 2a 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a  .** pages..**.**
15960 20 49 66 20 74 68 65 20 42 74 43 75 72 73 6f 72   If the BtCursor
15961 2e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c  .isIncrblobHandl
15962 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 61  e flag is set, a
15963 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  nd the current.*
15964 2a 20 63 75 72 73 6f 72 20 65 6e 74 72 79 20 75  * cursor entry u
15965 73 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ses one or more 
15966 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20  overflow pages, 
15967 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  this function.**
15968 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 65   allocates space
15969 20 66 6f 72 20 61 6e 64 20 6c 61 7a 69 6c 79 20   for and lazily 
1596a 70 6f 70 6c 75 61 74 65 73 20 74 68 65 20 6f 76  popluates the ov
1596b 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74  erflow page-list
1596c 20 0a 2a 2a 20 63 61 63 68 65 20 61 72 72 61 79   .** cache array
1596d 20 28 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 72   (BtCursor.aOver
1596e 66 6c 6f 77 29 2e 20 53 75 62 73 65 71 75 65 6e  flow). Subsequen
1596f 74 20 63 61 6c 6c 73 20 75 73 65 20 74 68 69 73  t calls use this
15970 0a 2a 2a 20 63 61 63 68 65 20 74 6f 20 6d 61 6b  .** cache to mak
15971 65 20 73 65 65 6b 69 6e 67 20 74 6f 20 74 68 65  e seeking to the
15972 20 73 75 70 70 6c 69 65 64 20 6f 66 66 73 65 74   supplied offset
15973 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 2e   more efficient.
15974 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f  .**.** Once an o
15975 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
15976 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e  t cache has been
15977 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d   allocated, it m
15978 61 79 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64  ay be.** invalid
15979 61 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 68  ated if some oth
1597a 65 72 20 63 75 72 73 6f 72 20 77 72 69 74 65 73  er cursor writes
1597b 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 61 62   to the same tab
1597c 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65  le, or if.** the
1597d 20 63 75 72 73 6f 72 20 69 73 20 6d 6f 76 65 64   cursor is moved
1597e 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20   to a different 
1597f 72 6f 77 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c  row. Additionall
15980 79 2c 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75  y, in auto-vacuu
15981 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66  m.** mode, the f
15982 6f 6c 6c 6f 77 69 6e 67 20 65 76 65 6e 74 73 20  ollowing events 
15983 6d 61 79 20 69 6e 76 61 6c 69 64 61 74 65 20 61  may invalidate a
15984 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  n overflow page-
15985 6c 69 73 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a  list cache..**.*
15986 2a 20 20 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65  *   * An increme
15987 6e 74 61 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20  ntal vacuum,.** 
15988 20 20 2a 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20    * A commit in 
15989 61 75 74 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c  auto_vacuum="ful
1598a 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20  l" mode,.**   * 
1598b 43 72 65 61 74 69 6e 67 20 61 20 74 61 62 6c 65  Creating a table
1598c 20 28 6d 61 79 20 72 65 71 75 69 72 65 20 6d 6f   (may require mo
1598d 76 69 6e 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77  ving an overflow
1598e 20 70 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69   page)..*/.stati
1598f 63 20 69 6e 74 20 61 63 63 65 73 73 50 61 79 6c  c int accessPayl
15990 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20  oad(.  BtCursor 
15991 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43  *pCur,      /* C
15992 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74  ursor pointing t
15993 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20  o entry to read 
15994 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66  from */.  u32 of
15995 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 2f  fset,          /
15996 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20  * Begin reading 
15997 74 68 69 73 20 66 61 72 20 69 6e 74 6f 20 70 61  this far into pa
15998 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 33 32 20 61  yload */.  u32 a
15999 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
1599a 2f 2a 20 52 65 61 64 20 74 68 69 73 20 6d 61 6e  /* Read this man
1599b 79 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73  y bytes */.  uns
1599c 69 67 6e 65 64 20 63 68 61 72 20 2a 70 42 75 66  igned char *pBuf
1599d 2c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 62  , /* Write the b
1599e 79 74 65 73 20 69 6e 74 6f 20 74 68 69 73 20 62  ytes into this b
1599f 75 66 66 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20  uffer */ .  int 
159a0 73 6b 69 70 4b 65 79 2c 20 20 20 20 20 20 20 20  skipKey,        
159a1 20 2f 2a 20 6f 66 66 73 65 74 20 62 65 67 69 6e   /* offset begin
159a2 73 20 61 74 20 64 61 74 61 20 69 66 20 74 68 69  s at data if thi
159a3 73 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 69  s is true */.  i
159a4 6e 74 20 65 4f 70 20 20 20 20 20 20 20 20 20 20  nt eOp          
159a5 20 20 20 20 2f 2a 20 7a 65 72 6f 20 74 6f 20 72      /* zero to r
159a6 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 6f 20 74 6f  ead. non-zero to
159a7 20 77 72 69 74 65 2e 20 2a 2f 0a 29 7b 0a 20 20   write. */.){.  
159a8 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61  unsigned char *a
159a9 50 61 79 6c 6f 61 64 3b 0a 20 20 69 6e 74 20 72  Payload;.  int r
159aa 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
159ab 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 69 6e 74   u32 nKey;.  int
159ac 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 4d 65 6d   iIdx = 0;.  Mem
159ad 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43  Page *pPage = pC
159ae 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
159af 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 42 74 72 65  >iPage]; /* Btre
159b0 65 20 70 61 67 65 20 6f 66 20 63 75 72 72 65 6e  e page of curren
159b1 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 74 53  t entry */.  BtS
159b2 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75  hared *pBt = pCu
159b3 72 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20  r->pBt;         
159b4 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65           /* Btre
159b5 65 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 65  e this cursor be
159b6 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20 20 61  longs to */..  a
159b7 73 73 65 72 74 28 20 70 50 61 67 65 20 29 3b 0a  ssert( pPage );.
159b8 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
159b9 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
159ba 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74  ALID );.  assert
159bb 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  ( pCur->aiIdx[pC
159bc 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65  ur->iPage]<pPage
159bd 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73  ->nCell );.  ass
159be 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
159bf 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a  Mutex(pCur) );..
159c0 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43    getCellInfo(pC
159c1 75 72 29 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20  ur);.  aPayload 
159c2 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65  = pCur->info.pCe
159c3 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  ll + pCur->info.
159c4 6e 48 65 61 64 65 72 3b 0a 20 20 6e 4b 65 79 20  nHeader;.  nKey 
159c5 3d 20 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  = (pPage->intKey
159c6 20 3f 20 30 20 3a 20 28 69 6e 74 29 70 43 75 72   ? 0 : (int)pCur
159c7 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 0a 20  ->info.nKey);.. 
159c8 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a   if( skipKey ){.
159c9 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 6e 4b      offset += nK
159ca 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f 66  ey;.  }.  if( of
159cb 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79 2b  fset+amt > nKey+
159cc 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61  pCur->info.nData
159cd 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c 6f 61   .   || &aPayloa
159ce 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  d[pCur->info.nLo
159cf 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d 3e 61  cal] > &pPage->a
159d0 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65  Data[pBt->usable
159d1 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20 20 2f  Size].  ){.    /
159d2 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61 64  * Trying to read
159d3 20 6f 72 20 77 72 69 74 65 20 70 61 73 74 20 74   or write past t
159d4 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61  he end of the da
159d5 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20 2a  ta is an error *
159d6 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  /.    return SQL
159d7 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
159d8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
159d9 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20 62  k if data must b
159da 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 74  e read/written t
159db 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65  o/from the btree
159dc 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a 2f   page itself. */
159dd 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 43  .  if( offset<pC
159de 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20  ur->info.nLocal 
159df 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 61  ){.    int a = a
159e0 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f 66  mt;.    if( a+of
159e1 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e  fset>pCur->info.
159e2 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20  nLocal ){.      
159e3 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  a = pCur->info.n
159e4 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b 0a  Local - offset;.
159e5 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63      }.    rc = c
159e6 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79  opyPayload(&aPay
159e7 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70 42  load[offset], pB
159e8 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61 67  uf, a, eOp, pPag
159e9 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
159ea 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20   offset = 0;.   
159eb 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20   pBuf += a;.    
159ec 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73  amt -= a;.  }els
159ed 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d 3d  e{.    offset -=
159ee 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63   pCur->info.nLoc
159ef 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  al;.  }..  if( r
159f0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
159f1 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 6f 6e  amt>0 ){.    con
159f2 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a 65 20  st u32 ovflSize 
159f3 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
159f4 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 65 73  e - 4;  /* Bytes
159f5 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f 76 66   content per ovf
159f6 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 67  l page */.    Pg
159f7 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a 20 20  no nextPage;..  
159f8 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74    nextPage = get
159f9 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 64 5b  4byte(&aPayload[
159fa 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
159fb 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  l]);..#ifndef SQ
159fc 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
159fd 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  OB.    /* If the
159fe 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c   isIncrblobHandl
159ff 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e  e flag is set an
15a00 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 61  d the BtCursor.a
15a01 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 20 2a  Overflow[].    *
15a02 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61  * has not been a
15a03 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63 61  llocated, alloca
15a04 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 61  te it now. The a
15a05 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 61 74  rray is sized at
15a06 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e 74 72  .    ** one entr
15a07 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72 66  y for each overf
15a08 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20  low page in the 
15a09 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20  overflow chain. 
15a0a 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 20  The.    ** page 
15a0b 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 69  number of the fi
15a0c 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  rst overflow pag
15a0d 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  e is stored in a
15a0e 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 20 20  Overflow[0],.   
15a0f 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c 75 65   ** etc. A value
15a10 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f 76   of 0 in the aOv
15a11 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 6d  erflow[] array m
15a12 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 6b 6e  eans "not yet kn
15a13 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 68 65  own".    ** (the
15a14 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c 79   cache is lazily
15a15 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20 20   populated)..   
15a16 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72   */.    if( pCur
15a17 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  ->isIncrblobHand
15a18 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76  le && !pCur->aOv
15a19 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
15a1a 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 43 75  int nOvfl = (pCu
15a1b 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64  r->info.nPayload
15a1c 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63  -pCur->info.nLoc
15a1d 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 2f 6f  al+ovflSize-1)/o
15a1e 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 70  vflSize;.      p
15a1f 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d  Cur->aOverflow =
15a20 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 65 33   (Pgno *)sqlite3
15a21 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f  MallocZero(sizeo
15a22 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0a  f(Pgno)*nOvfl);.
15a23 20 20 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20        if( nOvfl 
15a24 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 72 66  && !pCur->aOverf
15a25 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72  low ){.        r
15a26 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
15a27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
15a28 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f  .    /* If the o
15a29 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
15a2a 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e  t cache has been
15a2b 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74   allocated and t
15a2c 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20  he.    ** entry 
15a2d 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 72 65  for the first re
15a2e 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f 77 20  quired overflow 
15a2f 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c 20 73  page is valid, s
15a30 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72 65 63  kip.    ** direc
15a31 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a  tly to it..    *
15a32 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  /.    if( pCur->
15a33 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75  aOverflow && pCu
15a34 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66  r->aOverflow[off
15a35 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b  set/ovflSize] ){
15a36 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20 28 6f  .      iIdx = (o
15a37 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b  ffset/ovflSize);
15a38 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20  .      nextPage 
15a39 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  = pCur->aOverflo
15a3a 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f  w[iIdx];.      o
15a3b 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65 74 25  ffset = (offset%
15a3c 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d  ovflSize);.    }
15a3d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66 6f 72  .#endif..    for
15a3e 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ( ; rc==SQLITE_O
15a3f 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20 6e 65  K && amt>0 && ne
15a40 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b 29 7b  xtPage; iIdx++){
15a41 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
15a42 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
15a43 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71 75 69       /* If requi
15a44 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68  red, populate th
15a45 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  e overflow page-
15a46 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f 0a 20  list cache. */. 
15a47 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61       if( pCur->a
15a48 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
15a49 20 20 20 20 61 73 73 65 72 74 28 21 70 43 75 72      assert(!pCur
15a4a 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
15a4b 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72  ] || pCur->aOver
15a4c 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74  flow[iIdx]==next
15a4d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70  Page);.        p
15a4e 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
15a4f 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b  Idx] = nextPage;
15a50 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
15a51 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66 73 65  .      if( offse
15a52 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20  t>=ovflSize ){. 
15a53 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e         /* The on
15a54 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72 65 61  ly reason to rea
15a55 64 20 74 68 69 73 20 70 61 67 65 20 69 73 20 74  d this page is t
15a56 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70 61 67  o obtain the pag
15a57 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d  e.        ** num
15a58 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 78 74  ber for the next
15a59 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65   page in the ove
15a5a 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65  rflow chain. The
15a5b 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a   page.        **
15a5c 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 65 71   data is not req
15a5d 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 74 20  uired. So first 
15a5e 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68  try to lookup th
15a5f 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20  e overflow.     
15a60 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 74 20     ** page-list 
15a61 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c 20 74  cache, if any, t
15a62 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f  hen fall back to
15a63 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c 6f 77   the getOverflow
15a64 50 61 67 65 28 29 0a 20 20 20 20 20 20 20 20 2a  Page().        *
15a65 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20  * function..    
15a66 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
15a67 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
15a68 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66 28 20  LOB.        if( 
15a69 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
15a6a 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  && pCur->aOverfl
15a6b 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a 20 20  ow[iIdx+1] ){.  
15a6c 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65          nextPage
15a6d 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c   = pCur->aOverfl
15a6e 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 20 20  ow[iIdx+1];.    
15a6f 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65 6e 64      } else .#end
15a70 69 66 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  if.          rc 
15a71 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67  = getOverflowPag
15a72 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67 65 2c  e(pBt, nextPage,
15a73 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29 3b 0a   0, &nextPage);.
15a74 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2d          offset -
15a75 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20  = ovflSize;.    
15a76 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
15a77 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65 61 64   /* Need to read
15a78 20 74 68 69 73 20 70 61 67 65 20 70 72 6f 70 65   this page prope
15a79 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69 6e 73  rly. It contains
15a7a 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20 20 20   some of the.   
15a7b 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66       ** range of
15a7c 20 64 61 74 61 20 74 68 61 74 20 69 73 20 62 65   data that is be
15a7d 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d 3d 30  ing read (eOp==0
15a7e 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28 65 4f  ) or written (eO
15a7f 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20 20 2a  p!=0)..        *
15a80 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65  /.        DbPage
15a81 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20   *pDbPage;.     
15a82 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a     int a = amt;.
15a83 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
15a84 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74  ite3PagerGet(pBt
15a85 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74 50 61  ->pPager, nextPa
15a86 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20  ge, &pDbPage);. 
15a87 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
15a88 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
15a89 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d        aPayload =
15a8a 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
15a8b 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20  Data(pDbPage);. 
15a8c 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67           nextPag
15a8d 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 61  e = get4byte(aPa
15a8e 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 20  yload);.        
15a8f 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73 65 74    if( a + offset
15a90 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20   > ovflSize ){. 
15a91 20 20 20 20 20 20 20 20 20 20 20 61 20 3d 20 6f             a = o
15a92 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 65 74  vflSize - offset
15a93 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
15a94 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f 70          rc = cop
15a95 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f  yPayload(&aPaylo
15a96 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42  ad[offset+4], pB
15a97 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 62 50  uf, a, eOp, pDbP
15a98 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
15a99 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
15a9a 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  f(pDbPage);.    
15a9b 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30        offset = 0
15a9c 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d 74 20  ;.          amt 
15a9d 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 20 20  -= a;.          
15a9e 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 20  pBuf += a;.     
15a9f 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
15aa0 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63   }.  }..  if( rc
15aa1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61  ==SQLITE_OK && a
15aa2 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75  mt>0 ){.    retu
15aa3 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
15aa4 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65  T_BKPT;.  }.  re
15aa5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
15aa6 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74  * Read part of t
15aa7 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 65  he key associate
15aa8 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43  d with cursor pC
15aa9 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20  ur.  Exactly.** 
15aaa 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c  "amt" bytes will
15aab 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69   be transfered i
15aac 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65  nto pBuf[].  The
15aad 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67   transfer.** beg
15aae 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e  ins at "offset".
15aaf 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
15ab0 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
15ab1 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63  ss or an error c
15ab2 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20  ode if anything 
15ab3 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20  goes.** wrong.  
15ab4 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  An error is retu
15ab5 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b  rned if "offset+
15ab6 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74  amt" is larger t
15ab7 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c  han.** the avail
15ab8 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f  able payload..*/
15ab9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15aba 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
15abb 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43  Key(BtCursor *pC
15abc 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20  ur, u32 offset, 
15abd 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70  u32 amt, void *p
15abe 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Buf){.  int rc;.
15abf 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
15ac0 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
15ac1 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74  ) );.  rc = rest
15ac2 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
15ac3 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  n(pCur);.  if( r
15ac4 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
15ac5 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
15ac6 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
15ac7 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 61 73  _VALID );.    as
15ac8 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67  sert( pCur->iPag
15ac9 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70  e>=0 && pCur->ap
15aca 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
15acb 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75  ] );.    if( pCu
15acc 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e  r->apPage[0]->in
15acd 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 65  tKey ){.      re
15ace 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
15acf 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
15ad0 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
15ad1 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
15ad2 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67  age]<pCur->apPag
15ad3 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
15ad4 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20  nCell );.    rc 
15ad5 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28  = accessPayload(
15ad6 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d  pCur, offset, am
15ad7 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61  t, (unsigned cha
15ad8 72 2a 29 70 42 75 66 2c 20 30 2c 20 30 29 3b 0a  r*)pBuf, 0, 0);.
15ad9 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
15ada 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 70  .}../*.** Read p
15adb 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20  art of the data 
15adc 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
15add 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78  cursor pCur.  Ex
15ade 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62  actly.** "amt" b
15adf 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61  ytes will be tra
15ae0 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75  nsfered into pBu
15ae1 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66  f[].  The transf
15ae2 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20  er.** begins at 
15ae3 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20  "offset"..**.** 
15ae4 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
15ae5 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61   on success or a
15ae6 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  n error code if 
15ae7 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a  anything goes.**
15ae8 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f   wrong.  An erro
15ae9 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  r is returned if
15aea 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73   "offset+amt" is
15aeb 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20   larger than.** 
15aec 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 70 61  the available pa
15aed 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  yload..*/.SQLITE
15aee 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15aef 69 74 65 33 42 74 72 65 65 44 61 74 61 28 42 74  ite3BtreeData(Bt
15af0 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33  Cursor *pCur, u3
15af1 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d  2 offset, u32 am
15af2 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a  t, void *pBuf){.
15af3 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 6e 64    int rc;..#ifnd
15af4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
15af5 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 20 70  NCRBLOB.  if ( p
15af6 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
15af7 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20  SOR_INVALID ){. 
15af8 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15af9 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 6e 64  _ABORT;.  }.#end
15afa 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  if..  assert( cu
15afb 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
15afc 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72  Cur) );.  rc = r
15afd 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
15afe 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66  tion(pCur);.  if
15aff 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15b00 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
15b01 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
15b02 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20  SOR_VALID );.   
15b03 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
15b04 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d  Page>=0 && pCur-
15b05 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
15b06 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 73 65  age] );.    asse
15b07 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
15b08 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75  pCur->iPage]<pCu
15b09 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
15b0a 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b  iPage]->nCell );
15b0b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73  .    rc = access
15b0c 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66  Payload(pCur, of
15b0d 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 66 2c  fset, amt, pBuf,
15b0e 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65   1, 0);.  }.  re
15b0f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
15b10 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
15b11 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e  er to payload in
15b12 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
15b13 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68  he entry that th
15b14 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f  e .** pCur curso
15b15 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
15b16 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69  .  The pointer i
15b17 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  s to the beginni
15b18 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79  ng of.** the key
15b19 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61   if skipKey==0 a
15b1a 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  nd it points to 
15b1b 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
15b1c 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70   data if.** skip
15b1d 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d  Key==1.  The num
15b1e 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
15b1f 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61  available key/da
15b20 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  ta is written.**
15b21 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66   into *pAmt.  If
15b22 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20   *pAmt==0, then 
15b23 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
15b24 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a  ed will not be.*
15b25 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65  * a valid pointe
15b26 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  r..**.** This ro
15b27 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69  utine is an opti
15b28 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73  mization.  It is
15b29 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20   common for the 
15b2a 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e  entire key.** an
15b2b 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e  d data to fit on
15b2c 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20   the local page 
15b2d 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f  and for there to
15b2e 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a   be no overflow.
15b2f 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20  ** pages.  When 
15b30 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73  that is so, this
15b31 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20   routine can be 
15b32 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74  used to access t
15b33 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61  he.** key and da
15b34 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e  ta without makin
15b35 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68  g a copy.  If th
15b36 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74  e key and/or dat
15b37 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f  a spills.** onto
15b38 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c   overflow pages,
15b39 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c   then accessPayl
15b3a 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 73  oad() must be us
15b3b 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 79  ed to reassembly
15b3c 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61  .** the key/data
15b3d 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74   and copy it int
15b3e 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64  o a preallocated
15b3f 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54   buffer..**.** T
15b40 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72  he pointer retur
15b41 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ned by this rout
15b42 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74  ine looks direct
15b43 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68  ly into the cach
15b44 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68  ed.** page of th
15b45 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
15b46 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e   data might chan
15b47 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e  ge or move the n
15b48 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20  ext time.** any 
15b49 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73  btree routine is
15b4a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74   called..*/.stat
15b4b 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
15b4c 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 79  d char *fetchPay
15b4d 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72  load(.  BtCursor
15b4e 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20   *pCur,      /* 
15b4f 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  Cursor pointing 
15b50 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64  to entry to read
15b51 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a   from */.  int *
15b52 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20  pAmt,           
15b53 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d  /* Write the num
15b54 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65  ber of available
15b55 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20   bytes here */. 
15b56 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20   int skipKey    
15b57 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65        /* read be
15b58 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20  ginning at data 
15b59 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20  if this is true 
15b5a 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  */.){.  unsigned
15b5b 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b   char *aPayload;
15b5c 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
15b5d 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20  e;.  u32 nKey;. 
15b5e 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20   u32 nLocal;..  
15b5f 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 20  assert( pCur!=0 
15b60 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d  && pCur->iPage>=
15b61 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67  0 && pCur->apPag
15b62 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b  e[pCur->iPage]);
15b63 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
15b64 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
15b65 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
15b66 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
15b67 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70  tex(pCur) );.  p
15b68 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
15b69 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
15b6a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
15b6b 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
15b6c 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  age]<pPage->nCel
15b6d 6c 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e  l );.  getCellIn
15b6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79  fo(pCur);.  aPay
15b6f 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66  load = pCur->inf
15b70 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c  o.pCell;.  aPayl
15b71 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66  oad += pCur->inf
15b72 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 66 28  o.nHeader;.  if(
15b73 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29   pPage->intKey )
15b74 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a  {.    nKey = 0;.
15b75 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65    }else{.    nKe
15b76 79 20 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69  y = (int)pCur->i
15b77 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20  nfo.nKey;.  }.  
15b78 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20  if( skipKey ){. 
15b79 20 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e     aPayload += n
15b7a 4b 65 79 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20  Key;.    nLocal 
15b7b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  = pCur->info.nLo
15b7c 63 61 6c 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65  cal - nKey;.  }e
15b7d 6c 73 65 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20  lse{.    nLocal 
15b7e 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  = pCur->info.nLo
15b7f 63 61 6c 3b 0a 20 20 20 20 69 66 28 20 6e 4c 6f  cal;.    if( nLo
15b80 63 61 6c 3e 6e 4b 65 79 20 29 7b 0a 20 20 20 20  cal>nKey ){.    
15b81 20 20 6e 4c 6f 63 61 6c 20 3d 20 6e 4b 65 79 3b    nLocal = nKey;
15b82 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 41  .    }.  }.  *pA
15b83 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b 0a 20 20 72  mt = nLocal;.  r
15b84 65 74 75 72 6e 20 61 50 61 79 6c 6f 61 64 3b 0a  eturn aPayload;.
15b85 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68  }.../*.** For th
15b86 65 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72  e entry that cur
15b87 73 6f 72 20 70 43 75 72 20 69 73 20 70 6f 69 6e  sor pCur is poin
15b88 74 20 74 6f 2c 20 72 65 74 75 72 6e 20 61 73 0a  t to, return as.
15b89 2a 2a 20 6d 61 6e 79 20 62 79 74 65 73 20 6f 66  ** many bytes of
15b8a 20 74 68 65 20 6b 65 79 20 6f 72 20 64 61 74 61   the key or data
15b8b 20 61 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c   as are availabl
15b8c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 0a 2a  e on the local.*
15b8d 2a 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20  * b-tree page.  
15b8e 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72  Write the number
15b8f 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 79   of available by
15b90 74 65 73 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 0a  tes into *pAmt..
15b91 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65  **.** The pointe
15b92 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 65 70  r returned is ep
15b93 68 65 6d 65 72 61 6c 2e 20 20 54 68 65 20 6b 65  hemeral.  The ke
15b94 79 2f 64 61 74 61 20 6d 61 79 20 6d 6f 76 65 0a  y/data may move.
15b95 2a 2a 20 6f 72 20 62 65 20 64 65 73 74 72 6f 79  ** or be destroy
15b96 65 64 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 63  ed on the next c
15b97 61 6c 6c 20 74 6f 20 61 6e 79 20 42 74 72 65 65  all to any Btree
15b98 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 69 6e 63   routine,.** inc
15b99 6c 75 64 69 6e 67 20 63 61 6c 6c 73 20 66 72 6f  luding calls fro
15b9a 6d 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20  m other threads 
15b9b 61 67 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65  against the same
15b9c 20 63 61 63 68 65 2e 0a 2a 2a 20 48 65 6e 63 65   cache..** Hence
15b9d 2c 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65  , a mutex on the
15b9e 20 42 74 53 68 61 72 65 64 20 73 68 6f 75 6c 64   BtShared should
15b9f 20 62 65 20 68 65 6c 64 20 70 72 69 6f 72 20 74   be held prior t
15ba0 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69  o calling.** thi
15ba1 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a  s routine..**.**
15ba2 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
15ba3 69 73 20 75 73 65 64 20 74 6f 20 67 65 74 20 71  is used to get q
15ba4 75 69 63 6b 20 61 63 63 65 73 73 20 74 6f 20 6b  uick access to k
15ba5 65 79 20 61 6e 64 20 64 61 74 61 0a 2a 2a 20 69  ey and data.** i
15ba6 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73  n the common cas
15ba7 65 20 77 68 65 72 65 20 6e 6f 20 6f 76 65 72 66  e where no overf
15ba8 6c 6f 77 20 70 61 67 65 73 20 61 72 65 20 75 73  low pages are us
15ba9 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
15baa 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64  IVATE const void
15bab 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65   *sqlite3BtreeKe
15bac 79 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 20  yFetch(BtCursor 
15bad 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74  *pCur, int *pAmt
15bae 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ){.  assert( cur
15baf 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
15bb0 75 72 29 20 29 3b 0a 20 20 69 66 28 20 70 43 75  ur) );.  if( pCu
15bb1 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
15bb2 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72  R_VALID ){.    r
15bb3 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 76 6f 69  eturn (const voi
15bb4 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28  d*)fetchPayload(
15bb5 70 43 75 72 2c 20 70 41 6d 74 2c 20 30 29 3b 0a  pCur, pAmt, 0);.
15bb6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
15bb7 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
15bb8 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
15bb9 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74  ite3BtreeDataFet
15bba 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ch(BtCursor *pCu
15bbb 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20  r, int *pAmt){. 
15bbc 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
15bbd 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
15bbe 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65  );.  if( pCur->e
15bbf 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
15bc0 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72  LID ){.    retur
15bc1 6e 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66  n (const void*)f
15bc2 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72  etchPayload(pCur
15bc3 2c 20 70 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a  , pAmt, 1);.  }.
15bc4 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a    return 0;.}...
15bc5 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63  /*.** Move the c
15bc6 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20  ursor down to a 
15bc7 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 2e 20  new child page. 
15bc8 20 54 68 65 20 6e 65 77 50 67 6e 6f 20 61 72 67   The newPgno arg
15bc9 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20  ument is the.** 
15bca 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
15bcb 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 74 6f  he child page to
15bcc 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2f 0a 73 74 61   move to..*/.sta
15bcd 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 43 68  tic int moveToCh
15bce 69 6c 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ild(BtCursor *pC
15bcf 75 72 2c 20 75 33 32 20 6e 65 77 50 67 6e 6f 29  ur, u32 newPgno)
15bd0 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  {.  int rc;.  in
15bd1 74 20 69 20 3d 20 70 43 75 72 2d 3e 69 50 61 67  t i = pCur->iPag
15bd2 65 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e  e;.  MemPage *pN
15bd3 65 77 50 61 67 65 3b 0a 20 20 42 74 53 68 61 72  ewPage;.  BtShar
15bd4 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e  ed *pBt = pCur->
15bd5 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  pBt;..  assert( 
15bd6 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
15bd7 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
15bd8 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
15bd9 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
15bda 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
15bdb 2d 3e 69 50 61 67 65 3c 42 54 43 55 52 53 4f 52  ->iPage<BTCURSOR
15bdc 5f 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a 20 20  _MAX_DEPTH );.  
15bdd 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e  if( pCur->iPage>
15bde 3d 28 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44  =(BTCURSOR_MAX_D
15bdf 45 50 54 48 2d 31 29 20 29 7b 0a 20 20 20 20 72  EPTH-1) ){.    r
15be0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15be1 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
15be2 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74   rc = getAndInit
15be3 50 61 67 65 28 70 42 74 2c 20 6e 65 77 50 67 6e  Page(pBt, newPgn
15be4 6f 2c 20 26 70 4e 65 77 50 61 67 65 29 3b 0a 20  o, &pNewPage);. 
15be5 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
15be6 20 72 63 3b 0a 20 20 70 43 75 72 2d 3e 61 70 50   rc;.  pCur->apP
15be7 61 67 65 5b 69 2b 31 5d 20 3d 20 70 4e 65 77 50  age[i+1] = pNewP
15be8 61 67 65 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49  age;.  pCur->aiI
15be9 64 78 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 70  dx[i+1] = 0;.  p
15bea 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a 0a 20  Cur->iPage++;.. 
15beb 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
15bec 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76  e = 0;.  pCur->v
15bed 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20  alidNKey = 0;.  
15bee 69 66 28 20 70 4e 65 77 50 61 67 65 2d 3e 6e 43  if( pNewPage->nC
15bef 65 6c 6c 3c 31 20 29 7b 0a 20 20 20 20 72 65 74  ell<1 ){.    ret
15bf0 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
15bf1 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72  PT_BKPT;.  }.  r
15bf2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
15bf3 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  .}..#ifndef NDEB
15bf4 55 47 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 70 50  UG./*.** Page pP
15bf5 61 72 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65  arent is an inte
15bf6 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 61 66 29 20  rnal (non-leaf) 
15bf7 74 72 65 65 20 70 61 67 65 2e 20 54 68 69 73 20  tree page. This 
15bf8 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 61 73 73  function .** ass
15bf9 65 72 74 73 20 74 68 61 74 20 70 61 67 65 20 6e  erts that page n
15bfa 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69 73 20  umber iChild is 
15bfb 74 68 65 20 6c 65 66 74 2d 63 68 69 6c 64 20 69  the left-child i
15bfc 66 20 74 68 65 20 69 49 64 78 27 74 68 0a 2a 2a  f the iIdx'th.**
15bfd 20 63 65 6c 6c 20 69 6e 20 70 61 67 65 20 70 50   cell in page pP
15bfe 61 72 65 6e 74 2e 20 4f 72 2c 20 69 66 20 69 49  arent. Or, if iI
15bff 64 78 20 69 73 20 65 71 75 61 6c 20 74 6f 20 74  dx is equal to t
15c00 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
15c01 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 69 6e 20 70  of.** cells in p
15c02 50 61 72 65 6e 74 2c 20 74 68 61 74 20 70 61 67  Parent, that pag
15c03 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 20  e number iChild 
15c04 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69  is the right-chi
15c05 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 20 70 61 67  ld of.** the pag
15c06 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
15c07 64 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e  d assertParentIn
15c08 64 65 78 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  dex(MemPage *pPa
15c09 72 65 6e 74 2c 20 69 6e 74 20 69 49 64 78 2c 20  rent, int iIdx, 
15c0a 50 67 6e 6f 20 69 43 68 69 6c 64 29 7b 0a 20 20  Pgno iChild){.  
15c0b 61 73 73 65 72 74 28 20 69 49 64 78 3c 3d 70 50  assert( iIdx<=pP
15c0c 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  arent->nCell );.
15c0d 20 20 69 66 28 20 69 49 64 78 3d 3d 70 50 61 72    if( iIdx==pPar
15c0e 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20  ent->nCell ){.  
15c0f 20 20 61 73 73 65 72 74 28 20 67 65 74 34 62 79    assert( get4by
15c10 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61  te(&pParent->aDa
15c11 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f  ta[pParent->hdrO
15c12 66 66 73 65 74 2b 38 5d 29 3d 3d 69 43 68 69 6c  ffset+8])==iChil
15c13 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  d );.  }else{.  
15c14 20 20 61 73 73 65 72 74 28 20 67 65 74 34 62 79    assert( get4by
15c15 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 72  te(findCell(pPar
15c16 65 6e 74 2c 20 69 49 64 78 29 29 3d 3d 69 43 68  ent, iIdx))==iCh
15c17 69 6c 64 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c  ild );.  }.}.#el
15c18 73 65 0a 23 20 20 64 65 66 69 6e 65 20 61 73 73  se.#  define ass
15c19 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 78  ertParentIndex(x
15c1a 2c 79 2c 7a 29 20 0a 23 65 6e 64 69 66 0a 0a 2f  ,y,z) .#endif../
15c1b 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  *.** Move the cu
15c1c 72 73 6f 72 20 75 70 20 74 6f 20 74 68 65 20 70  rsor up to the p
15c1d 61 72 65 6e 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a  arent page..**.*
15c1e 2a 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73  * pCur->idx is s
15c1f 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69  et to the cell i
15c20 6e 64 65 78 20 74 68 61 74 20 63 6f 6e 74 61 69  ndex that contai
15c21 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a  ns the pointer.*
15c22 2a 20 74 6f 20 74 68 65 20 70 61 67 65 20 77 65  * to the page we
15c23 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d   are coming from
15c24 2e 20 20 49 66 20 77 65 20 61 72 65 20 63 6f 6d  .  If we are com
15c25 69 6e 67 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ing from the.** 
15c26 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64  right-most child
15c27 20 70 61 67 65 20 74 68 65 6e 20 70 43 75 72 2d   page then pCur-
15c28 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f 20 6f  >idx is set to o
15c29 6e 65 20 6d 6f 72 65 20 74 68 61 6e 0a 2a 2a 20  ne more than.** 
15c2a 74 68 65 20 6c 61 72 67 65 73 74 20 63 65 6c 6c  the largest cell
15c2b 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54   index..*/.SQLIT
15c2c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
15c2d 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 54  qlite3BtreeMoveT
15c2e 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f 72  oParent(BtCursor
15c2f 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72   *pCur){.  asser
15c30 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
15c31 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61  tex(pCur) );.  a
15c32 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
15c33 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
15c34 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  D );.  assert( p
15c35 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a  Cur->iPage>0 );.
15c36 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
15c37 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
15c38 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 50  ge] );.  assertP
15c39 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20 20 20  arentIndex(.    
15c3a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
15c3b 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20  r->iPage-1], .  
15c3c 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43    pCur->aiIdx[pC
15c3d 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20  ur->iPage-1], . 
15c3e 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b     pCur->apPage[
15c3f 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67  pCur->iPage]->pg
15c40 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65 61 73  no.  );.  releas
15c41 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61  ePage(pCur->apPa
15c42 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
15c43 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d  ;.  pCur->iPage-
15c44 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  -;.  pCur->info.
15c45 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75  nSize = 0;.  pCu
15c46 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30  r->validNKey = 0
15c47 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
15c48 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
15c49 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2f 0a 73  e root page.*/.s
15c4a 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f  tatic int moveTo
15c4b 52 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70  Root(BtCursor *p
15c4c 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  Cur){.  MemPage 
15c4d 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63  *pRoot;.  int rc
15c4e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
15c4f 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d  Btree *p = pCur-
15c50 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61  >pBtree;.  BtSha
15c51 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
15c52 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  t;..  assert( cu
15c53 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
15c54 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
15c55 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ( CURSOR_INVALID
15c56 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52   < CURSOR_REQUIR
15c57 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72  ESEEK );.  asser
15c58 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  t( CURSOR_VALID 
15c59 20 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49    < CURSOR_REQUI
15c5a 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65  RESEEK );.  asse
15c5b 72 74 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54  rt( CURSOR_FAULT
15c5c 20 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55     > CURSOR_REQU
15c5d 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28  IRESEEK );.  if(
15c5e 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43   pCur->eState>=C
15c5f 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
15c60 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75  K ){.    if( pCu
15c61 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
15c62 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20  R_FAULT ){.     
15c63 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b   return pCur->sk
15c64 69 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ip;.    }.    sq
15c65 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43  lite3BtreeClearC
15c66 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d  ursor(pCur);.  }
15c67 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50  ..  if( pCur->iP
15c68 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e  age>=0 ){.    in
15c69 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31  t i;.    for(i=1
15c6a 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65  ; i<=pCur->iPage
15c6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65  ; i++){.      re
15c6c 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e  leasePage(pCur->
15c6d 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20  apPage[i]);.    
15c6e 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  }.  }else{.    i
15c6f 66 28 20 0a 20 20 20 20 20 20 53 51 4c 49 54 45  f( .      SQLITE
15c70 5f 4f 4b 21 3d 28 72 63 20 3d 20 67 65 74 41 6e  _OK!=(rc = getAn
15c71 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70  dInitPage(pBt, p
15c72 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26  Cur->pgnoRoot, &
15c73 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29  pCur->apPage[0])
15c74 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70  ).    ){.      p
15c75 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55  Cur->eState = CU
15c76 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20  RSOR_INVALID;.  
15c77 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
15c78 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 52 6f 6f     }.  }..  pRoo
15c79 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  t = pCur->apPage
15c7a 5b 30 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  [0];.  assert( p
15c7b 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72  Root->pgno==pCur
15c7c 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b 0a 20 20  ->pgnoRoot );.  
15c7d 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 30 3b  pCur->iPage = 0;
15c7e 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30  .  pCur->aiIdx[0
15c7f 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69  ] = 0;.  pCur->i
15c80 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20  nfo.nSize = 0;. 
15c81 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20   pCur->atLast = 
15c82 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64  0;.  pCur->valid
15c83 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 69 66 28  NKey = 0;..  if(
15c84 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d 30   pRoot->nCell==0
15c85 20 26 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 66   && !pRoot->leaf
15c86 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73 75 62   ){.    Pgno sub
15c87 70 61 67 65 3b 0a 20 20 20 20 61 73 73 65 72 74  page;.    assert
15c88 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 31  ( pRoot->pgno==1
15c89 20 29 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20   );.    subpage 
15c8a 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f  = get4byte(&pRoo
15c8b 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e  t->aData[pRoot->
15c8c 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20  hdrOffset+8]);. 
15c8d 20 20 20 61 73 73 65 72 74 28 20 73 75 62 70 61     assert( subpa
15c8e 67 65 3e 30 20 29 3b 0a 20 20 20 20 70 43 75 72  ge>0 );.    pCur
15c8f 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
15c90 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 20  R_VALID;.    rc 
15c91 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
15c92 75 72 2c 20 73 75 62 70 61 67 65 29 3b 0a 20 20  ur, subpage);.  
15c93 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 72 2d  }else{.    pCur-
15c94 3e 65 53 74 61 74 65 20 3d 20 28 28 70 52 6f 6f  >eState = ((pRoo
15c95 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f 43 55 52 53  t->nCell>0)?CURS
15c96 4f 52 5f 56 41 4c 49 44 3a 43 55 52 53 4f 52 5f  OR_VALID:CURSOR_
15c97 49 4e 56 41 4c 49 44 29 3b 0a 20 20 7d 0a 20 20  INVALID);.  }.  
15c98 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
15c99 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72  .** Move the cur
15c9a 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20  sor down to the 
15c9b 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65  left-most leaf e
15c9c 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
15c9d 0a 2a 2a 20 65 6e 74 72 79 20 74 6f 20 77 68 69  .** entry to whi
15c9e 63 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74  ch it is current
15c9f 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a  ly pointing..**.
15ca0 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74  ** The left-most
15ca1 20 6c 65 61 66 20 69 73 20 74 68 65 20 6f 6e 65   leaf is the one
15ca2 20 77 69 74 68 20 74 68 65 20 73 6d 61 6c 6c 65   with the smalle
15ca3 73 74 20 6b 65 79 20 2d 20 74 68 65 20 66 69 72  st key - the fir
15ca4 73 74 0a 2a 2a 20 69 6e 20 61 73 63 65 6e 64 69  st.** in ascendi
15ca5 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61  ng order..*/.sta
15ca6 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 4c 65  tic int moveToLe
15ca7 66 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72 20  ftmost(BtCursor 
15ca8 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70  *pCur){.  Pgno p
15ca9 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  gno;.  int rc = 
15caa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d  SQLITE_OK;.  Mem
15cab 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20  Page *pPage;..  
15cac 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
15cad 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
15cae 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
15caf 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
15cb0 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c  _VALID );.  whil
15cb1 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
15cb2 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70 43   && !(pPage = pC
15cb3 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
15cb4 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29  >iPage])->leaf )
15cb5 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  {.    assert( pC
15cb6 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
15cb7 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43  iPage]<pPage->nC
15cb8 65 6c 6c 20 29 3b 0a 20 20 20 20 70 67 6e 6f 20  ell );.    pgno 
15cb9 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43  = get4byte(findC
15cba 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72 2d  ell(pPage, pCur-
15cbb 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
15cbc 67 65 5d 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  ge]));.    rc = 
15cbd 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
15cbe 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72  , pgno);.  }.  r
15cbf 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
15cc0 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  ** Move the curs
15cc1 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 72  or down to the r
15cc2 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65  ight-most leaf e
15cc3 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
15cc4 0a 2a 2a 20 70 61 67 65 20 74 6f 20 77 68 69 63  .** page to whic
15cc5 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  h it is currentl
15cc6 79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74  y pointing.  Not
15cc7 69 63 65 20 74 68 65 20 64 69 66 66 65 72 65 6e  ice the differen
15cc8 63 65 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f  ce.** between mo
15cc9 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61  veToLeftmost() a
15cca 6e 64 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f  nd moveToRightmo
15ccb 73 74 28 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66  st().  moveToLef
15ccc 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73  tmost().** finds
15ccd 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65   the left-most e
15cce 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
15ccf 20 2a 65 6e 74 72 79 2a 20 77 68 65 72 65 61 73   *entry* whereas
15cd0 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
15cd1 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20  ().** finds the 
15cd2 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79  right-most entry
15cd3 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 70 61   beneath the *pa
15cd4 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ge*..**.** The r
15cd5 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20  ight-most entry 
15cd6 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20  is the one with 
15cd7 74 68 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20  the largest key 
15cd8 2d 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65  - the last.** ke
15cd9 79 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f  y in ascending o
15cda 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rder..*/.static 
15cdb 69 6e 74 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d  int moveToRightm
15cdc 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ost(BtCursor *pC
15cdd 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  ur){.  Pgno pgno
15cde 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
15cdf 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67  ITE_OK;.  MemPag
15ce0 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20  e *pPage = 0;.. 
15ce1 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
15ce2 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
15ce3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
15ce4 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
15ce5 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69  R_VALID );.  whi
15ce6 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
15ce7 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70  K && !(pPage = p
15ce8 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
15ce9 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20  ->iPage])->leaf 
15cea 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65  ){.    pgno = ge
15ceb 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
15cec 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
15ced 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70  ffset+8]);.    p
15cee 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
15cef 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d  >iPage] = pPage-
15cf0 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 72 63 20 3d  >nCell;.    rc =
15cf1 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
15cf2 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20  r, pgno);.  }.  
15cf3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
15cf4 4b 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 61  K ){.    pCur->a
15cf5 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
15cf6 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ] = pPage->nCell
15cf7 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e  -1;.    pCur->in
15cf8 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
15cf9 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
15cfa 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  y = 0;.  }.  ret
15cfb 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f  urn rc;.}../* Mo
15cfc 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ve the cursor to
15cfd 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
15cfe 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20   in the table.  
15cff 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
15d00 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  .** on success. 
15d01 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20   Set *pRes to 0 
15d02 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63  if the cursor ac
15d03 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f  tually points to
15d04 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72   something.** or
15d05 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20   set *pRes to 1 
15d06 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
15d07 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  empty..*/.SQLITE
15d08 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15d09 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 42  ite3BtreeFirst(B
15d0a 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69  tCursor *pCur, i
15d0b 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74  nt *pRes){.  int
15d0c 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
15d0d 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
15d0e 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
15d0f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15d10 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74  x_held(pCur->pBt
15d11 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  ree->db->mutex) 
15d12 29 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  );.  rc = moveTo
15d13 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66  Root(pCur);.  if
15d14 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15d15 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  ){.    if( pCur-
15d16 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
15d17 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20  INVALID ){.     
15d18 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
15d19 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
15d1a 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a  e]->nCell==0 );.
15d1b 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b        *pRes = 1;
15d1c 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
15d1d 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  TE_OK;.    }else
15d1e 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
15d1f 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
15d20 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
15d21 3e 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65  >0 );.      *pRe
15d22 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  s = 0;.      rc 
15d23 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74  = moveToLeftmost
15d24 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20  (pCur);.    }.  
15d25 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
15d26 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  ../* Move the cu
15d27 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 73 74  rsor to the last
15d28 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61   entry in the ta
15d29 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  ble.  Return SQL
15d2a 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63  ITE_OK.** on suc
15d2b 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73  cess.  Set *pRes
15d2c 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 72   to 0 if the cur
15d2d 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69  sor actually poi
15d2e 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67  nts to something
15d2f 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73  .** or set *pRes
15d30 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 62   to 1 if the tab
15d31 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a  le is empty..*/.
15d32 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15d33 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  nt sqlite3BtreeL
15d34 61 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ast(BtCursor *pC
15d35 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a  ur, int *pRes){.
15d36 20 20 69 6e 74 20 72 63 3b 0a 20 0a 20 20 61 73    int rc;. .  as
15d37 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
15d38 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
15d39 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15d3a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75  3_mutex_held(pCu
15d3b 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d  r->pBtree->db->m
15d3c 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20  utex) );.  rc = 
15d3d 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29  moveToRoot(pCur)
15d3e 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
15d3f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
15d40 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d   CURSOR_INVALID=
15d41 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b  =pCur->eState ){
15d42 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
15d43 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
15d44 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d  ->iPage]->nCell=
15d45 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65  =0 );.      *pRe
15d46 73 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  s = 1;.    }else
15d47 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
15d48 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
15d49 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
15d4a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20      *pRes = 0;. 
15d4b 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f       rc = moveTo
15d4c 52 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b  Rightmost(pCur);
15d4d 0a 20 20 20 20 20 20 67 65 74 43 65 6c 6c 49 6e  .      getCellIn
15d4e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20  fo(pCur);.      
15d4f 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 72  pCur->atLast = r
15d50 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a  c==SQLITE_OK ?1:
15d51 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
15d52 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
15d53 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
15d54 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74  so that it point
15d55 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65  s to an entry ne
15d56 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73  ar the key .** s
15d57 70 65 63 69 66 69 65 64 20 62 79 20 70 49 64 78  pecified by pIdx
15d58 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20  Key or intKey.  
15d59 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73   Return a succes
15d5a 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f  s code..**.** Fo
15d5b 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c  r INTKEY tables,
15d5c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 72 61   the intKey para
15d5d 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e 20 20  meter is used.  
15d5e 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74  pIdxKey .** must
15d5f 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69   be NULL.  For i
15d60 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 49 64  ndex tables, pId
15d61 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 6e 64  xKey is used and
15d62 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67   intKey.** is ig
15d63 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  nored..**.** If 
15d64 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20 69  an exact match i
15d65 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65  s not found, the
15d66 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  n the cursor is 
15d67 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70  always.** left p
15d68 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c 65 61  ointing at a lea
15d69 66 20 70 61 67 65 20 77 68 69 63 68 20 77 6f 75  f page which wou
15d6a 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 72  ld hold the entr
15d6b 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 65 20  y if it.** were 
15d6c 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 63 75  present.  The cu
15d6d 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e 74  rsor might point
15d6e 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 61   to an entry tha
15d6f 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72  t comes.** befor
15d70 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 20 6b  e or after the k
15d71 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74  ey..**.** An int
15d72 65 67 65 72 20 69 73 20 77 72 69 74 74 65 6e 20  eger is written 
15d73 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 63 68  into *pRes which
15d74 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f   is the result o
15d75 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74  f.** comparing t
15d76 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 65 20  he key with the 
15d77 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74  entry to which t
15d78 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a  he cursor is .**
15d79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65 20   pointing.  The 
15d7a 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 69  meaning of the i
15d7b 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e 20 69  nteger written i
15d7c 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20  nto.** *pRes is 
15d7d 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
15d7e 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 20 20  *     *pRes<0   
15d7f 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73     The cursor is
15d80 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
15d81 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a  t an entry that.
15d82 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
15d83 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74      is smaller t
15d84 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b  han intKey/pIdxK
15d85 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 61 62  ey or if the tab
15d86 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20  le is empty.**  
15d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d88 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 69  and the cursor i
15d89 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74  s therefore left
15d8a 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e   point to nothin
15d8b 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52  g..**.**     *pR
15d8c 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75  es==0     The cu
15d8d 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69  rsor is left poi
15d8e 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72  nting at an entr
15d8f 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20  y that.**       
15d90 20 20 20 20 20 20 20 20 20 20 20 65 78 61 63 74             exact
15d91 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 4b 65  ly matches intKe
15d92 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a  y/pIdxKey..**.**
15d93 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 20 20       *pRes>0    
15d94 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20    The cursor is 
15d95 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
15d96 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a   an entry that.*
15d97 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
15d98 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 68 61     is larger tha
15d99 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79  n intKey/pIdxKey
15d9a 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ..**.*/.SQLITE_P
15d9b 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15d9c 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
15d9d 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f  acked(.  BtCurso
15d9e 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20  r *pCur,        
15d9f 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
15da0 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20  to be moved */. 
15da1 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
15da2 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70  *pIdxKey, /* Unp
15da3 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20  acked index key 
15da4 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c  */.  i64 intKey,
15da5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15da6 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a   The table key *
15da7 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 67 68  /.  int biasRigh
15da8 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t,           /* 
15da9 49 66 20 74 72 75 65 2c 20 62 69 61 73 20 74 68  If true, bias th
15daa 65 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 20  e search to the 
15dab 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e  high end */.  in
15dac 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 20  t *pRes         
15dad 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
15dae 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 68  search results h
15daf 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
15db0 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63  rc;..  assert( c
15db1 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
15db2 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
15db3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
15db4 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72  _held(pCur->pBtr
15db5 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ee->db->mutex) )
15db6 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  ;..  /* If the c
15db7 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79  ursor is already
15db8 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 74 20 74   positioned at t
15db9 68 65 20 70 6f 69 6e 74 20 77 65 20 61 72 65 20  he point we are 
15dba 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d  trying.  ** to m
15dbb 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75 73  ove to, then jus
15dbc 74 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74  t return without
15dbd 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 20   doing any work 
15dbe 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65  */.  if( pCur->e
15dbf 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
15dc0 4c 49 44 20 26 26 20 70 43 75 72 2d 3e 76 61 6c  LID && pCur->val
15dc1 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 20 70 43  idNKey .   && pC
15dc2 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69  ur->apPage[0]->i
15dc3 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 20 20 20  ntKey .  ){.    
15dc4 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  if( pCur->info.n
15dc5 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20  Key==intKey ){. 
15dc6 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a       *pRes = 0;.
15dc7 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15dc8 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
15dc9 20 20 69 66 28 20 70 43 75 72 2d 3e 61 74 4c 61    if( pCur->atLa
15dca 73 74 20 26 26 20 70 43 75 72 2d 3e 69 6e 66 6f  st && pCur->info
15dcb 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a  .nKey<intKey ){.
15dcc 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31        *pRes = -1
15dcd 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
15dce 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
15dcf 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65    }..  rc = move
15dd0 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20  ToRoot(pCur);.  
15dd1 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
15dd2 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61  turn rc;.  }.  a
15dd3 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
15dd4 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
15dd5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
15dd6 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
15dd7 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e 69 74 20  >iPage]->isInit 
15dd8 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65  );.  if( pCur->e
15dd9 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
15dda 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 52  VALID ){.    *pR
15ddb 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73  es = -1;.    ass
15ddc 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
15ddd 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
15dde 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  nCell==0 );.    
15ddf 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
15de0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
15de1 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d  pCur->apPage[0]-
15de2 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78 4b  >intKey || pIdxK
15de3 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b  ey );.  for(;;){
15de4 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 75 70  .    int lwr, up
15de5 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c 64  r;.    Pgno chld
15de6 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20  Pg;.    MemPage 
15de7 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61  *pPage = pCur->a
15de8 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
15de9 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 20 3d 20  e];.    int c = 
15dea 2d 31 3b 20 20 2f 2a 20 70 52 65 73 20 72 65 74  -1;  /* pRes ret
15deb 75 72 6e 20 69 66 20 74 61 62 6c 65 20 69 73 20  urn if table is 
15dec 65 6d 70 74 79 20 6d 75 73 74 20 62 65 20 2d 31  empty must be -1
15ded 20 2a 2f 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b   */.    lwr = 0;
15dee 0a 20 20 20 20 75 70 72 20 3d 20 70 50 61 67 65  .    upr = pPage
15def 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69  ->nCell-1;.    i
15df0 66 28 20 28 21 70 50 61 67 65 2d 3e 69 6e 74 4b  f( (!pPage->intK
15df1 65 79 20 26 26 20 70 49 64 78 4b 65 79 3d 3d 30  ey && pIdxKey==0
15df2 29 20 7c 7c 20 75 70 72 3c 30 20 29 7b 0a 20 20  ) || upr<0 ){.  
15df3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
15df4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
15df5 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f      goto moveto_
15df6 66 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20  finish;.    }.  
15df7 20 20 69 66 28 20 62 69 61 73 52 69 67 68 74 20    if( biasRight 
15df8 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61  ){.      pCur->a
15df9 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
15dfa 5d 20 3d 20 28 75 31 36 29 75 70 72 3b 0a 20 20  ] = (u16)upr;.  
15dfb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
15dfc 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
15dfd 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28  >iPage] = (u16)(
15dfe 28 75 70 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20  (upr+lwr)/2);.  
15dff 20 20 7d 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b    }.    for(;;){
15e00 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65  .      void *pCe
15e01 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 69 36 34  llKey;.      i64
15e02 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20   nCellKey;.     
15e03 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d   int idx = pCur-
15e04 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
15e05 67 65 5d 3b 0a 20 20 20 20 20 20 70 43 75 72 2d  ge];.      pCur-
15e06 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b  >info.nSize = 0;
15e07 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c  .      pCur->val
15e08 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20  idNKey = 1;.    
15e09 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74    if( pPage->int
15e0a 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 75  Key ){.        u
15e0b 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20  8 *pCell;.      
15e0c 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65    pCell = findCe
15e0d 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b  ll(pPage, idx) +
15e0e 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72   pPage->childPtr
15e0f 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 69 66  Size;.        if
15e10 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61  ( pPage->hasData
15e11 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33   ){.          u3
15e12 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20  2 dummy;.       
15e13 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56     pCell += getV
15e14 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64  arint32(pCell, d
15e15 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d  ummy);.        }
15e16 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69  .        getVari
15e17 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29  nt(pCell, (u64*)
15e18 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20  &nCellKey);.    
15e19 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79      if( nCellKey
15e1a 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20  ==intKey ){.    
15e1b 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20        c = 0;.   
15e1c 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
15e1d 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29  CellKey<intKey )
15e1e 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20  {.          c = 
15e1f 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  -1;.        }els
15e20 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  e{.          ass
15e21 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e  ert( nCellKey>in
15e22 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20  tKey );.        
15e23 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20    c = +1;.      
15e24 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
15e25 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 76 61  .        int ava
15e26 69 6c 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20  ilable;.        
15e27 70 43 65 6c 6c 4b 65 79 20 3d 20 28 76 6f 69 64  pCellKey = (void
15e28 20 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28   *)fetchPayload(
15e29 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65  pCur, &available
15e2a 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 6e 43  , 0);.        nC
15e2b 65 6c 6c 4b 65 79 20 3d 20 70 43 75 72 2d 3e 69  ellKey = pCur->i
15e2c 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20  nfo.nKey;.      
15e2d 20 20 69 66 28 20 61 76 61 69 6c 61 62 6c 65 3e    if( available>
15e2e 3d 6e 43 65 6c 6c 4b 65 79 20 29 7b 0a 20 20 20  =nCellKey ){.   
15e2f 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74         c = sqlit
15e30 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70  e3VdbeRecordComp
15e31 61 72 65 28 28 69 6e 74 29 6e 43 65 6c 6c 4b 65  are((int)nCellKe
15e32 79 2c 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64  y, pCellKey, pId
15e33 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d  xKey);.        }
15e34 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
15e35 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74  pCellKey = sqlit
15e36 65 33 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 6e  e3Malloc( (int)n
15e37 43 65 6c 6c 4b 65 79 20 29 3b 0a 20 20 20 20 20  CellKey );.     
15e38 20 20 20 20 20 69 66 28 20 70 43 65 6c 6c 4b 65       if( pCellKe
15e39 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  y==0 ){.        
15e3a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
15e3b 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
15e3c 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66     goto moveto_f
15e3d 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 20  inish;.         
15e3e 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20   }.          rc 
15e3f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  = sqlite3BtreeKe
15e40 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29  y(pCur, 0, (int)
15e41 6e 43 65 6c 6c 4b 65 79 2c 20 28 76 6f 69 64 2a  nCellKey, (void*
15e42 29 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20  )pCellKey);.    
15e43 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65        c = sqlite
15e44 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61  3VdbeRecordCompa
15e45 72 65 28 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79  re((int)nCellKey
15e46 2c 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78  , pCellKey, pIdx
15e47 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Key);.          
15e48 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65  sqlite3_free(pCe
15e49 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20  llKey);.        
15e4a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
15e4b 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
15e4c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
15e4d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20  .      if( c==0 
15e4e 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d  ){.        pCur-
15e4f 3e 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65  >info.nKey = nCe
15e50 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69  llKey;.        i
15e51 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  f( pPage->intKey
15e52 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66   && !pPage->leaf
15e53 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 77   ){.          lw
15e54 72 20 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 20  r = idx;.       
15e55 20 20 20 75 70 72 20 3d 20 6c 77 72 20 2d 20 31     upr = lwr - 1
15e56 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
15e57 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  k;.        }else
15e58 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 52 65  {.          *pRe
15e59 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  s = 0;.         
15e5a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
15e5b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
15e5c 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
15e5d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
15e5e 0a 20 20 20 20 20 20 69 66 28 20 63 3c 30 20 29  .      if( c<0 )
15e5f 7b 0a 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20  {.        lwr = 
15e60 69 64 78 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c  idx+1;.      }el
15e61 73 65 7b 0a 20 20 20 20 20 20 20 20 75 70 72 20  se{.        upr 
15e62 3d 20 69 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d  = idx-1;.      }
15e63 0a 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75  .      if( lwr>u
15e64 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  pr ){.        pC
15e65 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20  ur->info.nKey = 
15e66 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20  nCellKey;.      
15e67 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
15e68 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49  .      pCur->aiI
15e69 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  dx[pCur->iPage] 
15e6a 3d 20 28 75 31 36 29 28 28 6c 77 72 2b 75 70 72  = (u16)((lwr+upr
15e6b 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  )/2);.    }.    
15e6c 61 73 73 65 72 74 28 20 6c 77 72 3d 3d 75 70 72  assert( lwr==upr
15e6d 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  +1 );.    assert
15e6e 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20  ( pPage->isInit 
15e6f 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  );.    if( pPage
15e70 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
15e71 63 68 6c 64 50 67 20 3d 20 30 3b 0a 20 20 20 20  chldPg = 0;.    
15e72 7d 65 6c 73 65 20 69 66 28 20 6c 77 72 3e 3d 70  }else if( lwr>=p
15e73 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  Page->nCell ){. 
15e74 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65       chldPg = ge
15e75 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
15e76 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
15e77 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 7d  ffset+8]);.    }
15e78 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 6c 64  else{.      chld
15e79 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 66 69  Pg = get4byte(fi
15e7a 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6c 77  ndCell(pPage, lw
15e7b 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  r));.    }.    i
15e7c 66 28 20 63 68 6c 64 50 67 3d 3d 30 20 29 7b 0a  f( chldPg==0 ){.
15e7d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43        assert( pC
15e7e 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
15e7f 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50  iPage]<pCur->apP
15e80 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
15e81 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 20  ->nCell );.     
15e82 20 69 66 28 20 70 52 65 73 20 29 20 2a 70 52 65   if( pRes ) *pRe
15e83 73 20 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20  s = c;.      rc 
15e84 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
15e85 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66     goto moveto_f
15e86 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20  inish;.    }.   
15e87 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
15e88 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36  r->iPage] = (u16
15e89 29 6c 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e  )lwr;.    pCur->
15e8a 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a  info.nSize = 0;.
15e8b 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e      pCur->validN
15e8c 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20  Key = 0;.    rc 
15e8d 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
15e8e 75 72 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20  ur, chldPg);.   
15e8f 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d   if( rc ) goto m
15e90 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20  oveto_finish;.  
15e91 7d 0a 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a  }.moveto_finish:
15e92 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
15e93 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 76  ./*.** In this v
15e94 65 72 73 69 6f 6e 20 6f 66 20 42 74 72 65 65 4d  ersion of BtreeM
15e95 6f 76 65 74 6f 2c 20 70 4b 65 79 20 69 73 20 61  oveto, pKey is a
15e96 20 70 61 63 6b 65 64 20 69 6e 64 65 78 20 72 65   packed index re
15e97 63 6f 72 64 0a 2a 2a 20 73 75 63 68 20 61 73 20  cord.** such as 
15e98 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  is generated by 
15e99 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  the OP_MakeRecor
15e9a 64 20 6f 70 63 6f 64 65 2e 20 20 55 6e 70 61 63  d opcode.  Unpac
15e9b 6b 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20  k the.** record 
15e9c 61 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20 42 74  and then call Bt
15e9d 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
15e9e 64 28 29 20 74 6f 20 64 6f 20 74 68 65 20 77 6f  d() to do the wo
15e9f 72 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rk..*/.SQLITE_PR
15ea0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15ea1 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 20  3BtreeMoveto(.  
15ea2 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
15ea3 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70      /* Cursor op
15ea4 65 6e 20 6f 6e 20 74 68 65 20 62 74 72 65 65 20  en on the btree 
15ea5 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 2a  to be searched *
15ea6 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
15ea7 70 4b 65 79 2c 20 20 20 2f 2a 20 50 61 63 6b 65  pKey,   /* Packe
15ea8 64 20 6b 65 79 20 69 66 20 74 68 65 20 62 74 72  d key if the btr
15ea9 65 65 20 69 73 20 61 6e 20 69 6e 64 65 78 20 2a  ee is an index *
15eaa 2f 0a 20 20 69 36 34 20 6e 4b 65 79 2c 20 20 20  /.  i64 nKey,   
15eab 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67          /* Integ
15eac 65 72 20 6b 65 79 20 66 6f 72 20 74 61 62 6c 65  er key for table
15ead 73 2e 20 20 53 69 7a 65 20 6f 66 20 70 4b 65 79  s.  Size of pKey
15eae 20 66 6f 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a   for indices */.
15eaf 20 20 69 6e 74 20 62 69 61 73 2c 20 20 20 20 20    int bias,     
15eb0 20 20 20 20 20 20 2f 2a 20 42 69 61 73 20 73 65        /* Bias se
15eb1 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 67 68  arch to the high
15eb2 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70   end */.  int *p
15eb3 52 65 73 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Res           /*
15eb4 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 65   Write search re
15eb5 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b  sults here */.){
15eb6 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
15eb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15eb8 20 53 74 61 74 75 73 20 63 6f 64 65 20 2a 2f 0a   Status code */.
15eb9 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
15eba 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20   *pIdxKey;   /* 
15ebb 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b  Unpacked index k
15ebc 65 79 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64  ey */.  Unpacked
15ebd 52 65 63 6f 72 64 20 61 53 70 61 63 65 5b 31 36  Record aSpace[16
15ebe 5d 3b 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 65  ]; /* Temp space
15ebf 20 66 6f 72 20 70 49 64 78 4b 65 79 20 2d 20 74   for pIdxKey - t
15ec0 6f 20 61 76 6f 69 64 20 61 20 6d 61 6c 6c 6f 63  o avoid a malloc
15ec1 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4b 65 79 20   */..  if( pKey 
15ec2 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  ){.    assert( n
15ec3 4b 65 79 3d 3d 28 69 36 34 29 28 69 6e 74 29 6e  Key==(i64)(int)n
15ec4 4b 65 79 20 29 3b 0a 20 20 20 20 70 49 64 78 4b  Key );.    pIdxK
15ec5 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ey = sqlite3Vdbe
15ec6 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 75  RecordUnpack(pCu
15ec7 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 28 69 6e  r->pKeyInfo, (in
15ec8 74 29 6e 4b 65 79 2c 20 70 4b 65 79 2c 0a 20 20  t)nKey, pKey,.  
15ec9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15eca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15ecb 20 20 20 20 61 53 70 61 63 65 2c 20 73 69 7a 65      aSpace, size
15ecc 6f 66 28 61 53 70 61 63 65 29 29 3b 0a 20 20 20  of(aSpace));.   
15ecd 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 20   if( pIdxKey==0 
15ece 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
15ecf 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NOMEM;.  }else{.
15ed0 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 30 3b      pIdxKey = 0;
15ed1 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
15ed2 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
15ed3 70 61 63 6b 65 64 28 70 43 75 72 2c 20 70 49 64  packed(pCur, pId
15ed4 78 4b 65 79 2c 20 6e 4b 65 79 2c 20 62 69 61 73  xKey, nKey, bias
15ed5 2c 20 70 52 65 73 29 3b 0a 20 20 69 66 28 20 70  , pRes);.  if( p
15ed6 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Key ){.    sqlit
15ed7 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61  e3VdbeDeleteUnpa
15ed8 63 6b 65 64 52 65 63 6f 72 64 28 70 49 64 78 4b  ckedRecord(pIdxK
15ed9 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ey);.  }.  retur
15eda 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
15edb 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
15edc 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74  he cursor is not
15edd 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20   pointing at an 
15ede 65 6e 74 72 79 20 6f 66 20 74 68 65 20 74 61 62  entry of the tab
15edf 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 55 45 20 77  le..**.** TRUE w
15ee0 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20  ill be returned 
15ee1 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20  after a call to 
15ee2 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
15ee3 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 70 61 73 74  () moves.** past
15ee4 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
15ee5 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  in the table or 
15ee6 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
15ee7 28 29 20 6d 6f 76 65 73 20 70 61 73 74 0a 2a 2a  () moves past.**
15ee8 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
15ee9 2e 20 20 54 52 55 45 20 69 73 20 61 6c 73 6f 20  .  TRUE is also 
15eea 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20  returned if the 
15eeb 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a  table is empty..
15eec 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
15eed 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
15eee 65 65 45 6f 66 28 42 74 43 75 72 73 6f 72 20 2a  eeEof(BtCursor *
15eef 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f  pCur){.  /* TODO
15ef0 3a 20 57 68 61 74 20 69 66 20 74 68 65 20 63 75  : What if the cu
15ef1 72 73 6f 72 20 69 73 20 69 6e 20 43 55 52 53 4f  rsor is in CURSO
15ef2 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 62 75  R_REQUIRESEEK bu
15ef3 74 20 61 6c 6c 20 74 61 62 6c 65 20 65 6e 74 72  t all table entr
15ef4 69 65 73 0a 20 20 2a 2a 20 68 61 76 65 20 62 65  ies.  ** have be
15ef5 65 6e 20 64 65 6c 65 74 65 64 3f 20 54 68 69 73  en deleted? This
15ef6 20 41 50 49 20 77 69 6c 6c 20 6e 65 65 64 20 74   API will need t
15ef7 6f 20 63 68 61 6e 67 65 20 74 6f 20 72 65 74 75  o change to retu
15ef8 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  rn an error code
15ef9 0a 20 20 2a 2a 20 61 73 20 77 65 6c 6c 20 61 73  .  ** as well as
15efa 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 72 65 73   the boolean res
15efb 75 6c 74 20 76 61 6c 75 65 2e 0a 20 20 2a 2f 0a  ult value..  */.
15efc 20 20 72 65 74 75 72 6e 20 28 43 55 52 53 4f 52    return (CURSOR
15efd 5f 56 41 4c 49 44 21 3d 70 43 75 72 2d 3e 65 53  _VALID!=pCur->eS
15efe 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tate);.}../*.** 
15eff 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 62  Return the datab
15f00 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  ase connection h
15f01 61 6e 64 6c 65 20 66 6f 72 20 61 20 63 75 72 73  andle for a curs
15f02 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
15f03 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73  IVATE sqlite3 *s
15f04 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
15f05 72 44 62 28 63 6f 6e 73 74 20 42 74 43 75 72 73  rDb(const BtCurs
15f06 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
15f07 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15f08 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
15f09 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
15f0a 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 75   );.  return pCu
15f0b 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3b 0a 7d  r->pBtree->db;.}
15f0c 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20  ../*.** Advance 
15f0d 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
15f0e 65 20 6e 65 78 74 20 65 6e 74 72 79 20 69 6e 20  e next entry in 
15f0f 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49  the database.  I
15f10 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20  f.** successful 
15f11 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30  then set *pRes=0
15f12 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  .  If the cursor
15f13 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20  .** was already 
15f14 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
15f15 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  last entry in th
15f16 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72  e database befor
15f17 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  e.** this routin
15f18 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68  e was called, th
15f19 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a  en set *pRes=1..
15f1a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
15f1b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
15f1c 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72 20  eeNext(BtCursor 
15f1d 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73  *pCur, int *pRes
15f1e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
15f1f 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67  nt idx;.  MemPag
15f20 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73  e *pPage;..  ass
15f21 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
15f22 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
15f23 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72   rc = restoreCur
15f24 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72  sorPosition(pCur
15f25 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
15f26 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
15f27 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61  turn rc;.  }.  a
15f28 73 73 65 72 74 28 20 70 52 65 73 21 3d 30 20 29  ssert( pRes!=0 )
15f29 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49  ;.  if( CURSOR_I
15f2a 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53  NVALID==pCur->eS
15f2b 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65  tate ){.    *pRe
15f2c 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  s = 1;.    retur
15f2d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
15f2e 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69  .  if( pCur->ski
15f2f 70 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d  p>0 ){.    pCur-
15f30 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 2a  >skip = 0;.    *
15f31 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65  pRes = 0;.    re
15f32 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
15f33 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70    }.  pCur->skip
15f34 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d   = 0;..  pPage =
15f35 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
15f36 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 64  ur->iPage];.  id
15f37 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64  x = ++pCur->aiId
15f38 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  x[pCur->iPage];.
15f39 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15f3a 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73  >isInit );.  ass
15f3b 65 72 74 28 20 69 64 78 3c 3d 70 50 61 67 65 2d  ert( idx<=pPage-
15f3c 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 70 43 75  >nCell );..  pCu
15f3d 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20  r->info.nSize = 
15f3e 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64  0;.  pCur->valid
15f3f 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20  NKey = 0;.  if( 
15f40 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  idx>=pPage->nCel
15f41 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 50  l ){.    if( !pP
15f42 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
15f43 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
15f44 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79  ild(pCur, get4by
15f45 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
15f46 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
15f47 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20 69 66  t+8]));.      if
15f48 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
15f49 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76  ;.      rc = mov
15f4a 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72  eToLeftmost(pCur
15f4b 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d  );.      *pRes =
15f4c 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   0;.      return
15f4d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64   rc;.    }.    d
15f4e 6f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 75  o{.      if( pCu
15f4f 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20  r->iPage==0 ){. 
15f50 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31         *pRes = 1
15f51 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  ;.        pCur->
15f52 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
15f53 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20  INVALID;.       
15f54 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
15f55 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  K;.      }.     
15f56 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
15f57 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b  eToParent(pCur);
15f58 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70  .      pPage = p
15f59 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
15f5a 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77  ->iPage];.    }w
15f5b 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64  hile( pCur->aiId
15f5c 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d  x[pCur->iPage]>=
15f5d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  pPage->nCell );.
15f5e 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20      *pRes = 0;. 
15f5f 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e     if( pPage->in
15f60 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63  tKey ){.      rc
15f61 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e   = sqlite3BtreeN
15f62 65 78 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b  ext(pCur, pRes);
15f63 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
15f64 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
15f65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
15f66 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52  rn rc;.  }.  *pR
15f67 65 73 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50  es = 0;.  if( pP
15f68 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
15f69 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
15f6a 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f  K;.  }.  rc = mo
15f6b 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75  veToLeftmost(pCu
15f6c 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  r);.  return rc;
15f6d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20  .}.../*.** Step 
15f6e 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
15f6f 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72  e back to the pr
15f70 65 76 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20  evious entry in 
15f71 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49  the database.  I
15f72 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20  f.** successful 
15f73 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30  then set *pRes=0
15f74 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  .  If the cursor
15f75 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20  .** was already 
15f76 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
15f77 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74  first entry in t
15f78 68 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f  he database befo
15f79 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  re.** this routi
15f7a 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74  ne was called, t
15f7b 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e  hen set *pRes=1.
15f7c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15f7d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
15f7e 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75  reePrevious(BtCu
15f7f 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
15f80 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63  *pRes){.  int rc
15f81 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
15f82 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63  ge;..  assert( c
15f83 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
15f84 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20  pCur) );.  rc = 
15f85 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  restoreCursorPos
15f86 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69  ition(pCur);.  i
15f87 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
15f88 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
15f89 63 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61  c;.  }.  pCur->a
15f8a 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 69 66 28  tLast = 0;.  if(
15f8b 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d   CURSOR_INVALID=
15f8c 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b  =pCur->eState ){
15f8d 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a  .    *pRes = 1;.
15f8e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
15f8f 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  E_OK;.  }.  if( 
15f90 70 43 75 72 2d 3e 73 6b 69 70 3c 30 20 29 7b 0a  pCur->skip<0 ){.
15f91 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d      pCur->skip =
15f92 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   0;.    *pRes = 
15f93 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
15f94 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70  LITE_OK;.  }.  p
15f95 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a  Cur->skip = 0;..
15f96 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
15f97 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
15f98 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ge];.  assert( p
15f99 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a  Page->isInit );.
15f9a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
15f9b 61 66 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 64  af ){.    int id
15f9c 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  x = pCur->aiIdx[
15f9d 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
15f9e 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69    rc = moveToChi
15f9f 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74  ld(pCur, get4byt
15fa0 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  e(findCell(pPage
15fa1 2c 20 69 64 78 29 29 29 3b 0a 20 20 20 20 69 66  , idx)));.    if
15fa2 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
15fa3 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
15fa4 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69     rc = moveToRi
15fa5 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20  ghtmost(pCur);. 
15fa6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c   }else{.    whil
15fa7 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  e( pCur->aiIdx[p
15fa8 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 20 29  Cur->iPage]==0 )
15fa9 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72  {.      if( pCur
15faa 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20  ->iPage==0 ){.  
15fab 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61        pCur->eSta
15fac 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41  te = CURSOR_INVA
15fad 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 2a 70 52  LID;.        *pR
15fae 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  es = 1;.        
15faf 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
15fb0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
15fb1 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
15fb2 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a  ToParent(pCur);.
15fb3 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e      }.    pCur->
15fb4 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a  info.nSize = 0;.
15fb5 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e      pCur->validN
15fb6 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43  Key = 0;..    pC
15fb7 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
15fb8 69 50 61 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50  iPage]--;.    pP
15fb9 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
15fba 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
15fbb 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
15fbc 69 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65  intKey && !pPage
15fbd 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
15fbe 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
15fbf 65 50 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20  ePrevious(pCur, 
15fc0 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65  pRes);.    }else
15fc1 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
15fc2 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
15fc3 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20  }.  *pRes = 0;. 
15fc4 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
15fc5 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
15fc6 6e 65 77 20 70 61 67 65 20 66 72 6f 6d 20 74 68  new page from th
15fc7 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
15fc8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70  .**.** The new p
15fc9 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73  age is marked as
15fca 20 64 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 68   dirty.  (In oth
15fcb 65 72 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 65  er words, sqlite
15fcc 33 50 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a  3PagerWrite().**
15fcd 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
15fce 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20  n called on the 
15fcf 6e 65 77 20 70 61 67 65 2e 29 20 20 54 68 65 20  new page.)  The 
15fd0 6e 65 77 20 70 61 67 65 20 68 61 73 20 61 6c 73  new page has als
15fd1 6f 0a 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 65  o.** been refere
15fd2 6e 63 65 64 20 61 6e 64 20 74 68 65 20 63 61 6c  nced and the cal
15fd3 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20  ling routine is 
15fd4 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
15fd5 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  calling.** sqlit
15fd6 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f  e3PagerUnref() o
15fd7 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 77  n the new page w
15fd8 68 65 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a  hen it is done..
15fd9 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  **.** SQLITE_OK 
15fda 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73  is returned on s
15fdb 75 63 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68  uccess.  Any oth
15fdc 65 72 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  er return value 
15fdd 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20  indicates.** an 
15fde 65 72 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20  error.  *ppPage 
15fdf 61 6e 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 75  and *pPgno are u
15fe0 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20  ndefined in the 
15fe1 65 76 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f  event of an erro
15fe2 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76  r..** Do not inv
15fe3 6f 6b 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  oke sqlite3Pager
15fe4 55 6e 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 61  Unref() on *ppPa
15fe5 67 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69  ge if an error i
15fe6 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
15fe7 2a 20 49 66 20 74 68 65 20 22 6e 65 61 72 62 79  * If the "nearby
15fe8 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  " parameter is n
15fe9 6f 74 20 30 2c 20 74 68 65 6e 20 61 20 28 66 65  ot 0, then a (fe
15fea 65 62 6c 65 29 20 65 66 66 6f 72 74 20 69 73 20  eble) effort is 
15feb 6d 61 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61  made to .** loca
15fec 74 65 20 61 20 70 61 67 65 20 63 6c 6f 73 65 20  te a page close 
15fed 74 6f 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  to the page numb
15fee 65 72 20 22 6e 65 61 72 62 79 22 2e 20 20 54 68  er "nearby".  Th
15fef 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 69  is can be used i
15ff0 6e 20 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20  n an.** attempt 
15ff1 74 6f 20 6b 65 65 70 20 72 65 6c 61 74 65 64 20  to keep related 
15ff2 70 61 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 65  pages close to e
15ff3 61 63 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65  ach other in the
15ff4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a   database file,.
15ff5 2a 2a 20 77 68 69 63 68 20 69 6e 20 74 75 72 6e  ** which in turn
15ff6 20 63 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 61   can make databa
15ff7 73 65 20 61 63 63 65 73 73 20 66 61 73 74 65 72  se access faster
15ff8 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22  ..**.** If the "
15ff9 65 78 61 63 74 22 20 70 61 72 61 6d 65 74 65 72  exact" parameter
15ffa 20 69 73 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74   is not 0, and t
15ffb 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e  he page-number n
15ffc 65 61 72 62 79 20 65 78 69 73 74 73 20 0a 2a 2a  earby exists .**
15ffd 20 61 6e 79 77 68 65 72 65 20 6f 6e 20 74 68 65   anywhere on the
15ffe 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e   free-list, then
15fff 20 69 74 20 69 73 20 67 75 61 72 65 6e 74 65 65   it is guarentee
16000 64 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  d to be returned
16001 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c  . This.** is onl
16002 79 20 75 73 65 64 20 62 79 20 61 75 74 6f 2d 76  y used by auto-v
16003 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 73 20  acuum databases 
16004 77 68 65 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20  when allocating 
16005 61 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a  a new table..*/.
16006 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63  static int alloc
16007 61 74 65 42 74 72 65 65 50 61 67 65 28 0a 20 20  ateBtreePage(.  
16008 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a  BtShared *pBt, .
16009 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61    MemPage **ppPa
1600a 67 65 2c 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67  ge, .  Pgno *pPg
1600b 6e 6f 2c 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72  no, .  Pgno near
1600c 62 79 2c 0a 20 20 75 38 20 65 78 61 63 74 0a 29  by,.  u8 exact.)
1600d 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
1600e 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ge1;.  int rc;. 
1600f 20 69 6e 74 20 6e 3b 20 20 20 20 20 2f 2a 20 4e   int n;     /* N
16010 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f  umber of pages o
16011 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a  n the freelist *
16012 2f 0a 20 20 69 6e 74 20 6b 3b 20 20 20 20 20 2f  /.  int k;     /
16013 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 76  * Number of leav
16014 65 73 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20  es on the trunk 
16015 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20  of the freelist 
16016 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54  */.  MemPage *pT
16017 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50  runk = 0;.  MemP
16018 61 67 65 20 2a 70 50 72 65 76 54 72 75 6e 6b 20  age *pPrevTrunk 
16019 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
1601a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1601b 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
1601c 3b 0a 20 20 70 50 61 67 65 31 20 3d 20 70 42 74  ;.  pPage1 = pBt
1601d 2d 3e 70 50 61 67 65 31 3b 0a 20 20 6e 20 3d 20  ->pPage1;.  n = 
1601e 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31  get4byte(&pPage1
1601f 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20  ->aData[36]);.  
16020 69 66 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 2f  if( n>0 ){.    /
16021 2a 20 54 68 65 72 65 20 61 72 65 20 70 61 67 65  * There are page
16022 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73  s on the freelis
16023 74 2e 20 20 52 65 75 73 65 20 6f 6e 65 20 6f 66  t.  Reuse one of
16024 20 74 68 6f 73 65 20 70 61 67 65 73 2e 20 2a 2f   those pages. */
16025 0a 20 20 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b  .    Pgno iTrunk
16026 3b 0a 20 20 20 20 75 38 20 73 65 61 72 63 68 4c  ;.    u8 searchL
16027 69 73 74 20 3d 20 30 3b 20 2f 2a 20 49 66 20 74  ist = 0; /* If t
16028 68 65 20 66 72 65 65 2d 6c 69 73 74 20 6d 75 73  he free-list mus
16029 74 20 62 65 20 73 65 61 72 63 68 65 64 20 66 6f  t be searched fo
1602a 72 20 27 6e 65 61 72 62 79 27 20 2a 2f 0a 20 20  r 'nearby' */.  
1602b 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65    .    /* If the
1602c 20 27 65 78 61 63 74 27 20 70 61 72 61 6d 65 74   'exact' paramet
1602d 65 72 20 77 61 73 20 74 72 75 65 20 61 6e 64 20  er was true and 
1602e 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 70  a query of the p
1602f 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a  ointer-map.    *
16030 2a 20 73 68 6f 77 73 20 74 68 61 74 20 74 68 65  * shows that the
16031 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69   page 'nearby' i
16032 73 20 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20 74  s somewhere on t
16033 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68  he free-list, th
16034 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 6e  en.    ** the en
16035 74 69 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62  tire-list will b
16036 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 74  e searched for t
16037 68 61 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  hat page..    */
16038 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
16039 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1603a 20 20 20 20 69 66 28 20 65 78 61 63 74 20 26 26      if( exact &&
1603b 20 6e 65 61 72 62 79 3c 3d 70 61 67 65 72 50 61   nearby<=pagerPa
1603c 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a  gecount(pBt) ){.
1603d 20 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a        u8 eType;.
1603e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 65        assert( ne
1603f 61 72 62 79 3e 30 20 29 3b 0a 20 20 20 20 20 20  arby>0 );.      
16040 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74  assert( pBt->aut
16041 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 20 20 20  oVacuum );.     
16042 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28   rc = ptrmapGet(
16043 70 42 74 2c 20 6e 65 61 72 62 79 2c 20 26 65 54  pBt, nearby, &eT
16044 79 70 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  ype, 0);.      i
16045 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
16046 63 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79  c;.      if( eTy
16047 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50  pe==PTRMAP_FREEP
16048 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 73  AGE ){.        s
16049 65 61 72 63 68 4c 69 73 74 20 3d 20 31 3b 0a 20  earchList = 1;. 
1604a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 50       }.      *pP
1604b 67 6e 6f 20 3d 20 6e 65 61 72 62 79 3b 0a 20 20  gno = nearby;.  
1604c 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
1604d 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  /* Decrement the
1604e 20 66 72 65 65 2d 6c 69 73 74 20 63 6f 75 6e 74   free-list count
1604f 20 62 79 20 31 2e 20 53 65 74 20 69 54 72 75 6e   by 1. Set iTrun
16050 6b 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f  k to the index o
16051 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72  f the.    ** fir
16052 73 74 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75  st free-list tru
16053 6e 6b 20 70 61 67 65 2e 20 69 50 72 65 76 54 72  nk page. iPrevTr
16054 75 6e 6b 20 69 73 20 69 6e 69 74 69 61 6c 6c 79  unk is initially
16055 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72   1..    */.    r
16056 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
16057 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44  Write(pPage1->pD
16058 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
16059 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1605a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50      put4byte(&pP
1605b 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c  age1->aData[36],
1605c 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54   n-1);..    /* T
1605d 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 20 74  he code within t
1605e 68 69 73 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20  his loop is run 
1605f 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20 74 68 65  only once if the
16060 20 27 73 65 61 72 63 68 4c 69 73 74 27 20 76 61   'searchList' va
16061 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73  riable.    ** is
16062 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74 68 65 72   not true. Other
16063 77 69 73 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e  wise, it runs on
16064 63 65 20 66 6f 72 20 65 61 63 68 20 74 72 75 6e  ce for each trun
16065 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65 0a 20 20  k-page on the.  
16066 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 75    ** free-list u
16067 6e 74 69 6c 20 74 68 65 20 70 61 67 65 20 27 6e  ntil the page 'n
16068 65 61 72 62 79 27 20 69 73 20 6c 6f 63 61 74 65  earby' is locate
16069 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 6f  d..    */.    do
1606a 20 7b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72   {.      pPrevTr
1606b 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20  unk = pTrunk;.  
1606c 20 20 20 20 69 66 28 20 70 50 72 65 76 54 72 75      if( pPrevTru
1606d 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 54  nk ){.        iT
1606e 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28  runk = get4byte(
1606f 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61  &pPrevTrunk->aDa
16070 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65  ta[0]);.      }e
16071 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 54 72  lse{.        iTr
16072 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26  unk = get4byte(&
16073 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
16074 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
16075 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
16076 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
16077 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c  iTrunk, &pTrunk,
16078 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
16079 63 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 72  c ){.        pTr
1607a 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  unk = 0;.       
1607b 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
1607c 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 7d  te_page;.      }
1607d 0a 0a 20 20 20 20 20 20 6b 20 3d 20 67 65 74 34  ..      k = get4
1607e 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44  byte(&pTrunk->aD
1607f 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20 20 20 69  ata[4]);.      i
16080 66 28 20 6b 3d 3d 30 20 26 26 20 21 73 65 61 72  f( k==0 && !sear
16081 63 68 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20  chList ){.      
16082 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 68    /* The trunk h
16083 61 73 20 6e 6f 20 6c 65 61 76 65 73 20 61 6e 64  as no leaves and
16084 20 74 68 65 20 6c 69 73 74 20 69 73 20 6e 6f 74   the list is not
16085 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 2e   being searched.
16086 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 6f 20   .        ** So 
16087 65 78 74 72 61 63 74 20 74 68 65 20 74 72 75 6e  extract the trun
16088 6b 20 70 61 67 65 20 69 74 73 65 6c 66 20 61 6e  k page itself an
16089 64 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20  d use it as the 
1608a 6e 65 77 6c 79 20 0a 20 20 20 20 20 20 20 20 2a  newly .        *
1608b 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65  * allocated page
1608c 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
1608d 72 74 28 20 70 50 72 65 76 54 72 75 6e 6b 3d 3d  rt( pPrevTrunk==
1608e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  0 );.        rc 
1608f 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
16090 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50  ite(pTrunk->pDbP
16091 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66  age);.        if
16092 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
16093 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63    goto end_alloc
16094 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20  ate_page;.      
16095 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 50 67    }.        *pPg
16096 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 20  no = iTrunk;.   
16097 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61       memcpy(&pPa
16098 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20  ge1->aData[32], 
16099 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  &pTrunk->aData[0
1609a 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 2a  ], 4);.        *
1609b 70 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b  ppPage = pTrunk;
1609c 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20  .        pTrunk 
1609d 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41  = 0;.        TRA
1609e 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25  CE(("ALLOCATE: %
1609f 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65  d trunk - %d fre
160a0 65 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c  e pages left\n",
160a1 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a   *pPgno, n-1));.
160a2 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
160a3 6b 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  k>pBt->usableSiz
160a4 65 2f 34 20 2d 20 32 20 29 7b 0a 20 20 20 20 20  e/4 - 2 ){.     
160a5 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b     /* Value of k
160a6 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
160a7 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f 72 72  .  Database corr
160a8 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  uption */.      
160a9 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
160aa 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
160ab 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
160ac 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e  ocate_page;.#ifn
160ad 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
160ae 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
160af 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61 72 63   }else if( searc
160b0 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62 79 3d  hList && nearby=
160b1 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20  =iTrunk ){.     
160b2 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 69     /* The list i
160b3 73 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64  s being searched
160b4 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e 6b 20   and this trunk 
160b5 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67 65  page is the page
160b6 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61  .        ** to a
160b7 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 64 6c  llocate, regardl
160b8 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 69  ess of whether i
160b9 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a 20 20  t has leaves..  
160ba 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
160bb 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 3d   assert( *pPgno=
160bc 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 20 20  =iTrunk );.     
160bd 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72     *ppPage = pTr
160be 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 65 61  unk;.        sea
160bf 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20  rchList = 0;.   
160c0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
160c1 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75  3PagerWrite(pTru
160c2 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  nk->pDbPage);.  
160c3 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
160c4 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
160c5 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
160c6 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
160c7 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0a      if( k==0 ){.
160c8 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70            if( !p
160c9 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20  PrevTrunk ){.   
160ca 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
160cb 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
160cc 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61  2], &pTrunk->aDa
160cd 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20  ta[0], 4);.     
160ce 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
160cf 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
160d0 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74  pPrevTrunk->aDat
160d1 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61  a[0], &pTrunk->a
160d2 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20  Data[0], 4);.   
160d3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
160d4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
160d5 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 70    /* The trunk p
160d6 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 20  age is required 
160d7 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62 75  by the caller bu
160d8 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a 20  t it contains . 
160d9 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e           ** poin
160da 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69 73  ters to free-lis
160db 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66 69  t leaves. The fi
160dc 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65 73  rst leaf becomes
160dd 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20 20   a trunk.       
160de 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68     ** page in th
160df 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20  is case..       
160e0 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
160e1 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72 75  MemPage *pNewTru
160e2 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 67  nk;.          Pg
160e3 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20 67  no iNewTrunk = g
160e4 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d  et4byte(&pTrunk-
160e5 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20  >aData[8]);.    
160e6 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
160e7 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
160e8 42 74 2c 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26  Bt, iNewTrunk, &
160e9 70 4e 65 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20  pNewTrunk, 0);. 
160ea 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
160eb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
160ec 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
160ed 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
160ee 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
160ef 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
160f0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
160f1 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  NewTrunk->pDbPag
160f2 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  e);.          if
160f3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
160f4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ){.            r
160f5 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 54  eleasePage(pNewT
160f6 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20  runk);.         
160f7 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
160f8 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
160f9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
160fa 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75   memcpy(&pNewTru
160fb 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70  nk->aData[0], &p
160fc 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c  Trunk->aData[0],
160fd 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70   4);.          p
160fe 75 74 34 62 79 74 65 28 26 70 4e 65 77 54 72 75  ut4byte(&pNewTru
160ff 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d  nk->aData[4], k-
16100 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65  1);.          me
16101 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d  mcpy(&pNewTrunk-
16102 3e 61 44 61 74 61 5b 38 5d 2c 20 26 70 54 72 75  >aData[8], &pTru
16103 6e 6b 2d 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28  nk->aData[12], (
16104 6b 2d 31 29 2a 34 29 3b 0a 20 20 20 20 20 20 20  k-1)*4);.       
16105 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
16106 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20  NewTrunk);.     
16107 20 20 20 20 20 69 66 28 20 21 70 50 72 65 76 54       if( !pPrevT
16108 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  runk ){.        
16109 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
1610a 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
1610b 62 6c 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50  ble(pPage1->pDbP
1610c 61 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20  age) );.        
1610d 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50      put4byte(&pP
1610e 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c  age1->aData[32],
1610f 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20   iNewTrunk);.   
16110 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
16111 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
16112 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
16113 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62  (pPrevTrunk->pDb
16114 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
16115 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
16116 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
16117 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
16118 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  e;.            }
16119 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 74  .            put
1611a 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75 6e  4byte(&pPrevTrun
1611b 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65  k->aData[0], iNe
1611c 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20  wTrunk);.       
1611d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1611e 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20         pTrunk = 
1611f 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 45  0;.        TRACE
16120 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20  (("ALLOCATE: %d 
16121 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20  trunk - %d free 
16122 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a  pages left\n", *
16123 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65  pPgno, n-1));.#e
16124 6e 64 69 66 0a 20 20 20 20 20 20 7d 65 6c 73 65  ndif.      }else
16125 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74  {.        /* Ext
16126 72 61 63 74 20 61 20 6c 65 61 66 20 66 72 6f 6d  ract a leaf from
16127 20 74 68 65 20 74 72 75 6e 6b 20 2a 2f 0a 20 20   the trunk */.  
16128 20 20 20 20 20 20 69 6e 74 20 63 6c 6f 73 65 73        int closes
16129 74 3b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20  t;.        Pgno 
1612a 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 75  iPage;.        u
1612b 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 44  nsigned char *aD
1612c 61 74 61 20 3d 20 70 54 72 75 6e 6b 2d 3e 61 44  ata = pTrunk->aD
1612d 61 74 61 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ata;.        rc 
1612e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1612f 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50  ite(pTrunk->pDbP
16130 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66  age);.        if
16131 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
16132 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63    goto end_alloc
16133 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20  ate_page;.      
16134 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
16135 6e 65 61 72 62 79 3e 30 20 29 7b 0a 20 20 20 20  nearby>0 ){.    
16136 20 20 20 20 20 20 69 6e 74 20 69 2c 20 64 69 73        int i, dis
16137 74 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f  t;.          clo
16138 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  sest = 0;.      
16139 20 20 20 20 64 69 73 74 20 3d 20 67 65 74 34 62      dist = get4b
1613a 79 74 65 28 26 61 44 61 74 61 5b 38 5d 29 20 2d  yte(&aData[8]) -
1613b 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20   nearby;.       
1613c 20 20 20 69 66 28 20 64 69 73 74 3c 30 20 29 20     if( dist<0 ) 
1613d 64 69 73 74 20 3d 20 2d 64 69 73 74 3b 0a 20 20  dist = -dist;.  
1613e 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b          for(i=1;
1613f 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<k; i++){.    
16140 20 20 20 20 20 20 20 20 69 6e 74 20 64 32 20 3d          int d2 =
16141 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61   get4byte(&aData
16142 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e 65 61 72 62  [8+i*4]) - nearb
16143 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  y;.            i
16144 66 28 20 64 32 3c 30 20 29 20 64 32 20 3d 20 2d  f( d2<0 ) d2 = -
16145 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  d2;.            
16146 69 66 28 20 64 32 3c 64 69 73 74 20 29 7b 0a 20  if( d2<dist ){. 
16147 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6c 6f               clo
16148 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 20 20  sest = i;.      
16149 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 64          dist = d
1614a 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  2;.            }
1614b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1614c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1614d 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20        closest = 
1614e 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  0;.        }..  
1614f 20 20 20 20 20 20 69 50 61 67 65 20 3d 20 67 65        iPage = ge
16150 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 2b  t4byte(&aData[8+
16151 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0a 20 20 20  closest*4]);.   
16152 20 20 20 20 20 69 66 28 20 21 73 65 61 72 63 68       if( !search
16153 4c 69 73 74 20 7c 7c 20 69 50 61 67 65 3d 3d 6e  List || iPage==n
16154 65 61 72 62 79 20 29 7b 0a 20 20 20 20 20 20 20  earby ){.       
16155 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 0a 20     Pgno nPage;. 
16156 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20           *pPgno 
16157 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20  = iPage;.       
16158 20 20 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72     nPage = pager
16159 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a  Pagecount(pBt);.
1615a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70            if( *p
1615b 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b 0a 20 20  Pgno>nPage ){.  
1615c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65            /* Fre
1615d 65 20 70 61 67 65 20 6f 66 66 20 74 68 65 20 65  e page off the e
1615e 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a  nd of the file *
1615f 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  /.            rc
16160 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
16161 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20  T_BKPT;.        
16162 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
16163 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
16164 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
16165 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41    TRACE(("ALLOCA
16166 54 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20  TE: %d was leaf 
16167 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e  %d of %d on trun
16168 6b 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20  k %d".          
16169 20 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72         ": %d mor
1616a 65 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c  e free pages\n",
1616b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1616c 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73    *pPgno, closes
1616d 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e  t+1, k, pTrunk->
1616e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20  pgno, n-1));.   
1616f 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65         if( close
16170 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20  st<k-1 ){.      
16171 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 44        memcpy(&aD
16172 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d  ata[8+closest*4]
16173 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c  , &aData[4+k*4],
16174 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   4);.          }
16175 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62  .          put4b
16176 79 74 65 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b  yte(&aData[4], k
16177 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  -1);.          a
16178 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
16179 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
1617a 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 20  Trunk->pDbPage) 
1617b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
1617c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
1617d 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e  tPage(pBt, *pPgn
1617e 6f 2c 20 70 70 50 61 67 65 2c 20 31 29 3b 0a 20  o, ppPage, 1);. 
1617f 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d           if( rc=
16180 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16181 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
16182 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61  3PagerDontRollba
16183 63 6b 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44  ck((*ppPage)->pD
16184 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
16185 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16186 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 50  PagerWrite((*ppP
16187 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a  age)->pDbPage);.
16188 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
16189 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1618a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
1618b 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61  eleasePage(*ppPa
1618c 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ge);.           
1618d 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
1618e 20 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c           searchL
1618f 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ist = 0;.       
16190 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
16191 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 72   releasePage(pPr
16192 65 76 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20  evTrunk);.      
16193 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a  pPrevTrunk = 0;.
16194 20 20 20 20 7d 77 68 69 6c 65 28 20 73 65 61 72      }while( sear
16195 63 68 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73  chList );.  }els
16196 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20  e{.    /* There 
16197 61 72 65 20 6e 6f 20 70 61 67 65 73 20 6f 6e 20  are no pages on 
16198 74 68 65 20 66 72 65 65 6c 69 73 74 2c 20 73 6f  the freelist, so
16199 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 70 61   create a new pa
1619a 67 65 20 61 74 20 74 68 65 0a 20 20 20 20 2a 2a  ge at the.    **
1619b 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65   end of the file
1619c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67   */.    int nPag
1619d 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75  e = pagerPagecou
1619e 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 2a 70 50  nt(pBt);.    *pP
1619f 67 6e 6f 20 3d 20 6e 50 61 67 65 20 2b 20 31 3b  gno = nPage + 1;
161a0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
161a1 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
161a2 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75  .    if( pBt->au
161a3 74 6f 56 61 63 75 75 6d 20 26 26 20 50 54 52 4d  toVacuum && PTRM
161a4 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 2a  AP_ISPAGE(pBt, *
161a5 70 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20  pPgno) ){.      
161a6 2f 2a 20 49 66 20 2a 70 50 67 6e 6f 20 72 65 66  /* If *pPgno ref
161a7 65 72 73 20 74 6f 20 61 20 70 6f 69 6e 74 65 72  ers to a pointer
161a8 2d 6d 61 70 20 70 61 67 65 2c 20 61 6c 6c 6f 63  -map page, alloc
161a9 61 74 65 20 74 77 6f 20 6e 65 77 20 70 61 67 65  ate two new page
161aa 73 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 74 68  s.      ** at th
161ab 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c  e end of the fil
161ac 65 20 69 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65  e instead of one
161ad 2e 20 54 68 65 20 66 69 72 73 74 20 61 6c 6c 6f  . The first allo
161ae 63 61 74 65 64 20 70 61 67 65 0a 20 20 20 20 20  cated page.     
161af 20 2a 2a 20 62 65 63 6f 6d 65 73 20 61 20 6e 65   ** becomes a ne
161b0 77 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  w pointer-map pa
161b1 67 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 69  ge, the second i
161b2 73 20 75 73 65 64 20 62 79 20 74 68 65 20 63 61  s used by the ca
161b3 6c 6c 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ller..      */. 
161b4 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c       TRACE(("ALL
161b5 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65  OCATE: %d from e
161b6 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f 69 6e  nd of file (poin
161b7 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c 6e 22  ter-map page)\n"
161b8 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 20 20  , *pPgno));.    
161b9 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f    assert( *pPgno
161ba 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
161bb 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 20  AGE(pBt) );.    
161bc 20 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20    (*pPgno)++;.  
161bd 20 20 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d      if( *pPgno==
161be 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
161bf 45 28 70 42 74 29 20 29 7b 20 28 2a 70 50 67 6e  E(pBt) ){ (*pPgn
161c0 6f 29 2b 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23 65  o)++; }.    }.#e
161c1 6e 64 69 66 0a 0a 20 20 20 20 61 73 73 65 72 74  ndif..    assert
161c2 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e  ( *pPgno!=PENDIN
161c3 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
161c4 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
161c5 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
161c6 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70  (pBt, *pPgno, pp
161c7 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66  Page, 0);.    if
161c8 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
161c9 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
161ca 65 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70  e3PagerWrite((*p
161cb 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29  pPage)->pDbPage)
161cc 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
161cd 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
161ce 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70   releasePage(*pp
161cf 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Page);.    }.   
161d0 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54   TRACE(("ALLOCAT
161d1 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f  E: %d from end o
161d2 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e  f file\n", *pPgn
161d3 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  o));.  }..  asse
161d4 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44  rt( *pPgno!=PEND
161d5 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
161d6 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63  t) );..end_alloc
161d7 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65  ate_page:.  rele
161d8 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b  asePage(pTrunk);
161d9 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70  .  releasePage(p
161da 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66  PrevTrunk);.  if
161db 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
161dc 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
161dd 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f  e3PagerPageRefco
161de 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70  unt((*ppPage)->p
161df 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20  DbPage)>1 ){.   
161e0 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a     releasePage(*
161e1 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72  ppPage);.      r
161e2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
161e3 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
161e4 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 2d 3e  .    (*ppPage)->
161e5 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a  isInit = 0;.  }.
161e6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
161e7 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 70 61 67 65  /*.** Add a page
161e8 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
161e9 20 66 69 6c 65 20 74 6f 20 74 68 65 20 66 72 65   file to the fre
161ea 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c  elist..**.** sql
161eb 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29  ite3PagerUnref()
161ec 20 69 73 20 4e 4f 54 20 63 61 6c 6c 65 64 20 66   is NOT called f
161ed 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61  or pPage..*/.sta
161ee 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65  tic int freePage
161ef 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29  (MemPage *pPage)
161f0 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
161f1 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
161f2 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
161f3 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b  1 = pBt->pPage1;
161f4 0a 20 20 69 6e 74 20 72 63 2c 20 6e 2c 20 6b 3b  .  int rc, n, k;
161f5 0a 0a 20 20 2f 2a 20 50 72 65 70 61 72 65 20 74  ..  /* Prepare t
161f6 68 65 20 70 61 67 65 20 66 6f 72 20 66 72 65 65  he page for free
161f7 69 6e 67 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ing */.  assert(
161f8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
161f9 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
161fa 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
161fb 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3e  rt( pPage->pgno>
161fc 31 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 69 73  1 );.  pPage->is
161fd 49 6e 69 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  Init = 0;..  /* 
161fe 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72  Increment the fr
161ff 65 65 20 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e  ee page count on
16200 20 70 50 61 67 65 31 20 2a 2f 0a 20 20 72 63 20   pPage1 */.  rc 
16201 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
16202 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50  ite(pPage1->pDbP
16203 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
16204 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6e 20   return rc;.  n 
16205 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
16206 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a  e1->aData[36]);.
16207 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67    put4byte(&pPag
16208 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e  e1->aData[36], n
16209 2b 31 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  +1);..#ifdef SQL
1620a 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54  ITE_SECURE_DELET
1620b 45 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 53 51  E.  /* If the SQ
1620c 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45  LITE_SECURE_DELE
1620d 54 45 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  TE compile-time 
1620e 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65  option is enable
1620f 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6c 77  d, then.  ** alw
16210 61 79 73 20 66 75 6c 6c 79 20 6f 76 65 72 77 72  ays fully overwr
16211 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f  ite deleted info
16212 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72  rmation with zer
16213 6f 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  os..  */.  rc = 
16214 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
16215 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
16216 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
16217 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 73 65  turn rc;.  memse
16218 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20  t(pPage->aData, 
16219 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70  0, pPage->pBt->p
1621a 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 66  ageSize);.#endif
1621b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61  ..  /* If the da
1621c 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20  tabase supports 
1621d 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 69  auto-vacuum, wri
1621e 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74  te an entry in t
1621f 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20  he pointer-map. 
16220 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20   ** to indicate 
16221 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73  that the page is
16222 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   free..  */.  if
16223 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29  ( ISAUTOVACUUM )
16224 7b 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61  {.    rc = ptrma
16225 70 50 75 74 28 70 42 74 2c 20 70 50 61 67 65 2d  pPut(pBt, pPage-
16226 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 46 52  >pgno, PTRMAP_FR
16227 45 45 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20  EEPAGE, 0);.    
16228 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
16229 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e  rc;.  }..  if( n
1622a 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==0 ){.    /* Th
1622b 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20  is is the first 
1622c 66 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 20  free page */.   
1622d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1622e 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70  erWrite(pPage->p
1622f 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  DbPage);.    if(
16230 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
16231 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67  .    memset(pPag
16232 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 38 29 3b  e->aData, 0, 8);
16233 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70  .    put4byte(&p
16234 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
16235 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a  , pPage->pgno);.
16236 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45      TRACE(("FREE
16237 2d 50 41 47 45 3a 20 25 64 20 66 69 72 73 74 5c  -PAGE: %d first\
16238 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29  n", pPage->pgno)
16239 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1623a 2f 2a 20 4f 74 68 65 72 20 66 72 65 65 20 70 61  /* Other free pa
1623b 67 65 73 20 61 6c 72 65 61 64 79 20 65 78 69 73  ges already exis
1623c 74 2e 20 20 52 65 74 72 69 76 65 20 74 68 65 20  t.  Retrive the 
1623d 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65  first trunk page
1623e 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66  .    ** of the f
1623f 72 65 65 6c 69 73 74 20 61 6e 64 20 66 69 6e 64  reelist and find
16240 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 6c 65   out how many le
16241 61 76 65 73 20 69 74 20 68 61 73 2e 20 2a 2f 0a  aves it has. */.
16242 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72      MemPage *pTr
16243 75 6e 6b 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  unk;.    rc = sq
16244 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
16245 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28  e(pBt, get4byte(
16246 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
16247 32 5d 29 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29  2]), &pTrunk, 0)
16248 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
16249 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6b 20  eturn rc;.    k 
1624a 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75  = get4byte(&pTru
1624b 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20  nk->aData[4]);. 
1624c 20 20 20 69 66 28 20 6b 3e 3d 70 42 74 2d 3e 75     if( k>=pBt->u
1624d 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20  sableSize/4 - 8 
1624e 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ){.      /* The 
1624f 74 72 75 6e 6b 20 69 73 20 66 75 6c 6c 2e 20 20  trunk is full.  
16250 54 75 72 6e 20 74 68 65 20 70 61 67 65 20 62 65  Turn the page be
16251 69 6e 67 20 66 72 65 65 64 20 69 6e 74 6f 20 61  ing freed into a
16252 20 6e 65 77 0a 20 20 20 20 20 20 2a 2a 20 74 72   new.      ** tr
16253 75 6e 6b 20 70 61 67 65 20 77 69 74 68 20 6e 6f  unk page with no
16254 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 2a   leaves..      *
16255 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20  *.      ** Note 
16256 74 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70  that the trunk p
16257 61 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c  age is not reall
16258 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20  y full until it 
16259 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a  contains.      *
1625a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d  * usableSize/4 -
1625b 20 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20   2 entries, not 
1625c 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38  usableSize/4 - 8
1625d 20 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68   entries as we h
1625e 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64  ave.      ** cod
1625f 65 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20  ed.  But due to 
16260 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69  a coding error i
16261 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  n versions of SQ
16262 4c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 20  Lite prior to.  
16263 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61      ** 3.6.0, da
16264 74 61 62 61 73 65 73 20 77 69 74 68 20 66 72 65  tabases with fre
16265 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  elist trunk page
16266 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74  s holding more t
16267 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 61  han.      ** usa
16268 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e  bleSize/4 - 8 en
16269 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 65  tries will be re
1626a 70 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 70  ported as corrup
1626b 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 20  t.  In order.   
1626c 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69     ** to maintai
1626d 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  n backwards comp
1626e 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f  atibility with o
1626f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  lder versions of
16270 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a   SQLite,.      *
16271 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69  * we will contai
16272 6e 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 68  n to restrict th
16273 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
16274 69 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a  ies to usableSiz
16275 65 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a  e/4 - 8.      **
16276 20 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f   for now.  At so
16277 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20  me point in the 
16278 66 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65  future (once eve
16279 72 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64  ryone has upgrad
1627a 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33  ed.      ** to 3
1627b 2e 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77  .6.0 or later) w
1627c 65 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65  e should conside
1627d 72 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e  r fixing the con
1627e 64 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20  ditional above. 
1627f 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20       ** to read 
16280 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22  "usableSize/4-2"
16281 20 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61   instead of "usa
16282 62 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20  bleSize/4-8"..  
16283 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20      */.      rc 
16284 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
16285 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
16286 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
16287 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16288 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
16289 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 70  (pPage->aData, p
1628a 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 3b 0a 20 20  Trunk->pgno);.  
1628b 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
1628c 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c  pPage->aData[4],
1628d 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74   0);.        put
1628e 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
1628f 44 61 74 61 5b 33 32 5d 2c 20 70 50 61 67 65 2d  Data[32], pPage-
16290 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  >pgno);.        
16291 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47  TRACE(("FREE-PAG
16292 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20  E: %d new trunk 
16293 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25  page replacing %
16294 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
16295 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e        pPage->pgn
16296 6f 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29  o, pTrunk->pgno)
16297 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
16298 65 6c 73 65 20 69 66 28 20 6b 3c 30 20 29 7b 0a  else if( k<0 ){.
16299 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1629a 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 7d  E_CORRUPT;.    }
1629b 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41  else{.      /* A
1629c 64 64 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65  dd the newly fre
1629d 65 64 20 70 61 67 65 20 61 73 20 61 20 6c 65 61  ed page as a lea
1629e 66 20 6f 6e 20 74 68 65 20 63 75 72 72 65 6e 74  f on the current
1629f 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20   trunk */.      
162a0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
162a1 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70  rWrite(pTrunk->p
162a2 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  DbPage);.      i
162a3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
162a4 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34   ){.        put4
162a5 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44  byte(&pTrunk->aD
162a6 61 74 61 5b 34 5d 2c 20 6b 2b 31 29 3b 0a 20 20  ata[4], k+1);.  
162a7 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
162a8 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b  pTrunk->aData[8+
162a9 6b 2a 34 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e  k*4], pPage->pgn
162aa 6f 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  o);.#ifndef SQLI
162ab 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45  TE_SECURE_DELETE
162ac 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
162ad 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
162ae 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
162af 67 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ge);.#endif.    
162b0 20 20 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28    }.      TRACE(
162b1 28 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20  ("FREE-PAGE: %d 
162b2 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61  leaf on trunk pa
162b3 67 65 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e  ge %d\n",pPage->
162b4 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e  pgno,pTrunk->pgn
162b5 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  o));.    }.    r
162b6 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e  eleasePage(pTrun
162b7 6b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  k);.  }.  return
162b8 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72   rc;.}../*.** Fr
162b9 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20  ee any overflow 
162ba 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64  pages associated
162bb 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20   with the given 
162bc 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Cell..*/.static 
162bd 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d 65  int clearCell(Me
162be 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 6e  mPage *pPage, un
162bf 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65  signed char *pCe
162c0 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ll){.  BtShared 
162c1 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42  *pBt = pPage->pB
162c2 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e  t;.  CellInfo in
162c3 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 50  fo;.  Pgno ovflP
162c4 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  gno;.  int rc;. 
162c5 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 69 6e   int nOvfl;.  in
162c6 74 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a  t ovflPageSize;.
162c7 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
162c8 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
162c9 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
162ca 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   );.  sqlite3Btr
162cb 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
162cc 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e  Page, pCell, &in
162cd 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e  fo);.  if( info.
162ce 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a  iOverflow==0 ){.
162cf 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
162d0 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65  E_OK;  /* No ove
162d1 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 52 65 74  rflow pages. Ret
162d2 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e  urn without doin
162d3 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20  g anything */.  
162d4 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 67  }.  ovflPgno = g
162d5 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  et4byte(&pCell[i
162d6 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b  nfo.iOverflow]);
162d7 0a 20 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 20  .  ovflPageSize 
162d8 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
162d9 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d  e - 4;.  nOvfl =
162da 20 28 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20   (info.nPayload 
162db 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20  - info.nLocal + 
162dc 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 2d 20 31  ovflPageSize - 1
162dd 29 2f 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a  )/ovflPageSize;.
162de 20 20 61 73 73 65 72 74 28 20 6f 76 66 6c 50 67    assert( ovflPg
162df 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30  no==0 || nOvfl>0
162e0 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76   );.  while( nOv
162e1 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 4d 65 6d 50  fl-- ){.    MemP
162e2 61 67 65 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20  age *pOvfl;.    
162e3 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20  if( ovflPgno==0 
162e4 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65  || ovflPgno>page
162e5 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20  rPagecount(pBt) 
162e6 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
162e7 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
162e8 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  KPT;.    }..    
162e9 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77  rc = getOverflow
162ea 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 50 67  Page(pBt, ovflPg
162eb 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20 28 6e 4f 76  no, &pOvfl, (nOv
162ec 66 6c 3d 3d 30 29 3f 30 3a 26 6f 76 66 6c 50 67  fl==0)?0:&ovflPg
162ed 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  no);.    if( rc 
162ee 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
162ef 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70   rc = freePage(p
162f0 4f 76 66 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  Ovfl);.    sqlit
162f1 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76  e3PagerUnref(pOv
162f2 66 6c 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  fl->pDbPage);.  
162f3 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
162f4 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75  n rc;.  }.  retu
162f5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
162f6 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68  ./*.** Create th
162f7 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 20  e byte sequence 
162f8 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e  used to represen
162f9 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65  t a cell on page
162fa 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72   pPage.** and wr
162fb 69 74 65 20 74 68 61 74 20 62 79 74 65 20 73 65  ite that byte se
162fc 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c  quence into pCel
162fd 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70  l[].  Overflow p
162fe 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f  ages are.** allo
162ff 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 64  cated and filled
16300 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72 79   in as necessary
16301 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70  .  The calling p
16302 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 72  rocedure.** is r
16303 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d  esponsible for m
16304 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66 69  aking sure suffi
16305 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 73 20  cient space has 
16306 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a  been allocated.*
16307 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a  * for pCell[]..*
16308 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 70  *.** Note that p
16309 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65  Cell does not ne
1630a 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f 20  cessary need to 
1630b 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50 61  point to the pPa
1630c 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65  ge->aData.** are
1630d 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74 20  a.  pCell might 
1630e 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65  point to some te
1630f 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 2e  mporary storage.
16310 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a    The cell will.
16311 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65  ** be constructe
16312 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f 72  d in this tempor
16313 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 63 6f  ary area then co
16314 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65 2d  pied into pPage-
16315 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e  >aData.** later.
16316 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
16317 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d  illInCell(.  Mem
16318 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20  Page *pPage,    
16319 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1631a 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e  he page that con
1631b 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a  tains the cell *
1631c 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
1631d 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20  r *pCell,       
1631e 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74     /* Complete t
1631f 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  ext of the cell 
16320 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
16321 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c  *pKey, i64 nKey,
16322 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a      /* The key *
16323 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
16324 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61 2c  pData,int nData,
16325 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a     /* The data *
16326 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20  /.  int nZero,  
16327 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16328 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f     /* Extra zero
16329 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64   bytes to append
1632a 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20 69   to pData */.  i
1632b 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20  nt *pnSize      
1632c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1632d 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a 65   Write cell size
1632e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
1632f 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63 6f  t nPayload;.  co
16330 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20 20  nst u8 *pSrc;.  
16331 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63 3b  int nSrc, n, rc;
16332 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66 74  .  int spaceLeft
16333 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76  ;.  MemPage *pOv
16334 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67  fl = 0;.  MemPag
16335 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d 20  e *pToRelease = 
16336 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  0;.  unsigned ch
16337 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e  ar *pPrior;.  un
16338 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 61  signed char *pPa
16339 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72 65  yload;.  BtShare
1633a 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
1633b 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  pBt;.  Pgno pgno
1633c 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Ovfl = 0;.  int 
1633d 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c 49  nHeader;.  CellI
1633e 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 73  nfo info;..  ass
1633f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
16340 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
16341 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  Bt->mutex) );.. 
16342 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f 74   /* pPage is not
16343 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72 69   necessarily wri
16344 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43 65  teable since pCe
16345 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78 69  ll might be auxi
16346 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66 65  liary.  ** buffe
16347 72 20 73 70 61 63 65 20 74 68 61 74 20 69 73 20  r space that is 
16348 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68  separate from th
16349 65 20 70 50 61 67 65 20 62 75 66 66 65 72 20 61  e pPage buffer a
1634a 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  rea */.  assert(
1634b 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 44   pCell<pPage->aD
1634c 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70  ata || pCell>=&p
1634d 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d  Page->aData[pBt-
1634e 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20  >pageSize].     
1634f 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
16350 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
16351 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
16352 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20  ) );..  /* Fill 
16353 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 2a  in the header. *
16354 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 3b  /.  nHeader = 0;
16355 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c  .  if( !pPage->l
16356 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 64  eaf ){.    nHead
16357 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69  er += 4;.  }.  i
16358 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74  f( pPage->hasDat
16359 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72  a ){.    nHeader
1635a 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70   += putVarint(&p
1635b 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e  Cell[nHeader], n
1635c 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d  Data+nZero);.  }
1635d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 20  else{.    nData 
1635e 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d  = nZero = 0;.  }
1635f 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75  .  nHeader += pu
16360 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e  tVarint(&pCell[n
16361 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29  Header], *(u64*)
16362 26 6e 4b 65 79 29 3b 0a 20 20 73 71 6c 69 74 65  &nKey);.  sqlite
16363 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
16364 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
16365 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72   &info);.  asser
16366 74 28 20 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3d  t( info.nHeader=
16367 3d 6e 48 65 61 64 65 72 20 29 3b 0a 20 20 61 73  =nHeader );.  as
16368 73 65 72 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d  sert( info.nKey=
16369 3d 6e 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72  =nKey );.  asser
1636a 74 28 20 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28  t( info.nData==(
1636b 75 33 32 29 28 6e 44 61 74 61 2b 6e 5a 65 72 6f  u32)(nData+nZero
1636c 29 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 46 69 6c  ) );.  .  /* Fil
1636d 6c 20 69 6e 20 74 68 65 20 70 61 79 6c 6f 61 64  l in the payload
1636e 20 2a 2f 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d   */.  nPayload =
1636f 20 6e 44 61 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a   nData + nZero;.
16370 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74    if( pPage->int
16371 4b 65 79 20 29 7b 0a 20 20 20 20 70 53 72 63 20  Key ){.    pSrc 
16372 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 6e 53 72  = pData;.    nSr
16373 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 6e  c = nData;.    n
16374 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  Data = 0;.  }els
16375 65 7b 20 0a 20 20 20 20 2f 2a 20 54 42 44 3a 20  e{ .    /* TBD: 
16376 20 50 65 72 68 61 70 73 20 72 61 69 73 65 20 53   Perhaps raise S
16377 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66  QLITE_CORRUPT if
16378 20 6e 4b 65 79 20 69 73 20 6c 61 72 67 65 72 20   nKey is larger 
16379 74 68 61 6e 20 33 31 20 62 69 74 73 3f 20 2a 2f  than 31 bits? */
1637a 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b 3d  .    nPayload +=
1637b 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 20 20   (int)nKey;.    
1637c 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 20 20  pSrc = pKey;.   
1637d 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e 4b 65   nSrc = (int)nKe
1637e 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 7a 65  y;.  }.  *pnSize
1637f 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 20   = info.nSize;. 
16380 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 6e 66   spaceLeft = inf
16381 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 61 79  o.nLocal;.  pPay
16382 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e 48  load = &pCell[nH
16383 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 6f 72  eader];.  pPrior
16384 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69   = &pCell[info.i
16385 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 77 68  Overflow];..  wh
16386 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e 30 20  ile( nPayload>0 
16387 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 63 65  ){.    if( space
16388 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 6e 64  Left==0 ){.#ifnd
16389 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1638a 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
1638b 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 70 20  Pgno pgnoPtrmap 
1638c 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a 20 4f  = pgnoOvfl; /* O
1638d 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69  verflow page poi
1638e 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 70  nter-map entry p
1638f 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  age */.      if(
16390 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
16391 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 7b 0a   ){.        do{.
16392 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f 4f 76            pgnoOv
16393 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 20  fl++;.        } 
16394 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 20 20  while( .        
16395 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28    PTRMAP_ISPAGE(
16396 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 20 7c  pBt, pgnoOvfl) |
16397 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 4e 44  | pgnoOvfl==PEND
16398 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
16399 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  t) .        );. 
1639a 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
1639b 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74      rc = allocat
1639c 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
1639d 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f 76 66  &pOvfl, &pgnoOvf
1639e 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 29 3b  l, pgnoOvfl, 0);
1639f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
163a0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
163a1 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
163a2 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74  database support
163a3 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 61  s auto-vacuum, a
163a4 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72  nd the second or
163a5 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20 20   subsequent.    
163a6 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61    ** overflow pa
163a7 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c 6f  ge is being allo
163a8 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65 6e  cated, add an en
163a9 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e 74  try to the point
163aa 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a 20  er-map.      ** 
163ab 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e 6f  for that page no
163ac 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  w. .      **.   
163ad 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73     ** If this is
163ae 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66   the first overf
163af 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 77  low page, then w
163b0 72 69 74 65 20 61 20 70 61 72 74 69 61 6c 20 65  rite a partial e
163b1 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 74  ntry .      ** t
163b2 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  o the pointer-ma
163b3 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 20 6e  p. If we write n
163b4 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20 70  othing to this p
163b5 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74 2c  ointer-map slot,
163b6 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  .      ** then t
163b7 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f 76  he optimistic ov
163b8 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72 6f  erflow chain pro
163b9 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61 72  cessing in clear
163ba 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a 20  Cell().      ** 
163bb 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65 74  may misinterpret
163bc 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69 73   the uninitialis
163bd 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64 65  ed values and de
163be 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a  lete the.      *
163bf 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66 72  * wrong pages fr
163c0 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  om the database.
163c1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
163c2 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
163c3 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  uum && rc==SQLIT
163c4 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
163c5 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e 6f  u8 eType = (pgno
163c6 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f 56  Ptrmap?PTRMAP_OV
163c7 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f 4f  ERFLOW2:PTRMAP_O
163c8 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20 20  VERFLOW1);.     
163c9 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
163ca 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c  t(pBt, pgnoOvfl,
163cb 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d   eType, pgnoPtrm
163cc 61 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ap);.        if(
163cd 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20   rc ){.         
163ce 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f 76   releasePage(pOv
163cf 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  fl);.        }. 
163d0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
163d1 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
163d2 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
163d3 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20  e(pToRelease);. 
163d4 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
163d5 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
163d6 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 73   /* If pToReleas
163d7 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68  e is not zero th
163d8 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e 74 73  an pPrior points
163d9 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 61   into the data a
163da 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  rea.      ** of 
163db 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b  pToRelease.  Mak
163dc 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73  e sure pToReleas
163dd 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65  e is still write
163de 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61  able. */.      a
163df 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61 73  ssert( pToReleas
163e0 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50  e==0 || sqlite3P
163e1 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
163e2 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 50  pToRelease->pDbP
163e3 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 2f  age) );..      /
163e4 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 20 70  * If pPrior is p
163e5 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20  art of the data 
163e6 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20 74  area of pPage, t
163e7 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70 50  hen make sure pP
163e8 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20  age.      ** is 
163e9 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 20  still writeable 
163ea 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
163eb 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d 3e 61   pPrior<pPage->a
163ec 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 3e 3d  Data || pPrior>=
163ed 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42  &pPage->aData[pB
163ee 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20  t->pageSize].   
163ef 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69           || sqli
163f0 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
163f1 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
163f2 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 70 75  ge) );..      pu
163f3 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 70  t4byte(pPrior, p
163f4 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20  gnoOvfl);.      
163f5 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52  releasePage(pToR
163f6 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 70  elease);.      p
163f7 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f 76 66  ToRelease = pOvf
163f8 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 20  l;.      pPrior 
163f9 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 3b 0a  = pOvfl->aData;.
163fa 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70        put4byte(p
163fb 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 20 20  Prior, 0);.     
163fc 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 4f 76   pPayload = &pOv
163fd 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a 20 20  fl->aData[4];.  
163fe 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20      spaceLeft = 
163ff 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
16400 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  - 4;.    }.    n
16401 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20   = nPayload;.   
16402 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 66 74   if( n>spaceLeft
16403 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 66 74   ) n = spaceLeft
16404 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 54 6f  ;..    /* If pTo
16405 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 7a  Release is not z
16406 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c 6f 61  ero than pPayloa
16407 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68  d points into th
16408 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20  e data area.    
16409 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 65  ** of pToRelease
1640a 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54 6f  .  Make sure pTo
1640b 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c 6c  Release is still
1640c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20   writeable. */. 
1640d 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65     assert( pToRe
1640e 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69  lease==0 || sqli
1640f 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
16410 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e  ble(pToRelease->
16411 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20  pDbPage) );..   
16412 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 64 20   /* If pPayload 
16413 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64  is part of the d
16414 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67  ata area of pPag
16415 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72  e, then make sur
16416 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a 20 69  e pPage.    ** i
16417 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c  s still writeabl
16418 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  e */.    assert(
16419 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 65 2d   pPayload<pPage-
1641a 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 6c 6f  >aData || pPaylo
1641b 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74  ad>=&pPage->aDat
1641c 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d  a[pBt->pageSize]
1641d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
1641e 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
1641f 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
16420 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20  DbPage) );..    
16421 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a 20 20  if( nSrc>0 ){.  
16422 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 20 29      if( n>nSrc )
16423 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 20 20   n = nSrc;.     
16424 20 61 73 73 65 72 74 28 20 70 53 72 63 20 29 3b   assert( pSrc );
16425 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 50  .      memcpy(pP
16426 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 6e 29  ayload, pSrc, n)
16427 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
16428 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 6c 6f     memset(pPaylo
16429 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 20 7d  ad, 0, n);.    }
1642a 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2d 3d  .    nPayload -=
1642b 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f 61 64   n;.    pPayload
1642c 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 63 20   += n;.    pSrc 
1642d 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 20 2d  += n;.    nSrc -
1642e 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 4c 65  = n;.    spaceLe
1642f 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 66 28  ft -= n;.    if(
16430 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20   nSrc==0 ){.    
16431 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a    nSrc = nData;.
16432 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 44 61        pSrc = pDa
16433 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ta;.    }.  }.  
16434 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52  releasePage(pToR
16435 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 75 72  elease);.  retur
16436 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
16437 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  /*.** Remove the
16438 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d 20   i-th cell from 
16439 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f 75  pPage.  This rou
1643a 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 50 61  tine effects pPa
1643b 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 65 20  ge only..** The 
1643c 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20  cell content is 
1643d 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65 61  not freed or dea
1643e 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69 73  llocated.  It is
1643f 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a   assumed that.**
16440 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
16441 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 69 65  t has been copie
16442 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c 73 65  d someplace else
16443 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
16444 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 73 20  just.** removes 
16445 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f  the reference to
16446 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 70   the cell from p
16447 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 7a 22  Page..**.** "sz"
16448 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e 75 6d   must be the num
16449 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1644a 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61  the cell..*/.sta
1644b 74 69 63 20 69 6e 74 20 64 72 6f 70 43 65 6c 6c  tic int dropCell
1644c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1644d 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20 73 7a   int idx, int sz
1644e 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
1644f 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
16450 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63  nter */.  int pc
16451 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66  ;         /* Off
16452 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74  set to cell cont
16453 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e  ent of cell bein
16454 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75  g deleted */.  u
16455 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f  8 *data;       /
16456 2a 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a  * pPage->aData *
16457 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20  /.  u8 *ptr;    
16458 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d      /* Used to m
16459 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64  ove bytes around
1645a 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a   within data[] *
1645b 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
1645c 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72      /* The retur
1645d 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 61 73 73  n code */..  ass
1645e 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69  ert( idx>=0 && i
1645f 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  dx<pPage->nCell 
16460 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d  );.  assert( sz=
16461 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c  =cellSize(pPage,
16462 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72   idx) );.  asser
16463 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
16464 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
16465 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
16466 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
16467 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
16468 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
16469 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  .  data = pPage-
1646a 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20  >aData;.  ptr = 
1646b 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c  &data[pPage->cel
1646c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d  lOffset + 2*idx]
1646d 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 74  ;.  pc = get2byt
1646e 65 28 70 74 72 29 3b 0a 20 20 69 66 28 20 28 70  e(ptr);.  if( (p
1646f 63 3c 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  c<pPage->hdrOffs
16470 65 74 2b 36 2b 28 70 50 61 67 65 2d 3e 6c 65 61  et+6+(pPage->lea
16471 66 3f 30 3a 34 29 29 0a 20 20 20 20 20 7c 7c 20  f?0:4)).     || 
16472 28 70 63 2b 73 7a 3e 70 50 61 67 65 2d 3e 70 42  (pc+sz>pPage->pB
16473 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 20 29  t->usableSize) )
16474 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
16475 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
16476 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66 72 65  ;.  }.  rc = fre
16477 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 70 63  eSpace(pPage, pc
16478 2c 20 73 7a 29 3b 0a 20 20 69 66 28 20 72 63 21  , sz);.  if( rc!
16479 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1647a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
1647b 0a 20 20 66 6f 72 28 69 3d 69 64 78 2b 31 3b 20  .  for(i=idx+1; 
1647c 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20  i<pPage->nCell; 
1647d 69 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b 0a 20 20  i++, ptr+=2){.  
1647e 20 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 32    ptr[0] = ptr[2
1647f 5d 3b 0a 20 20 20 20 70 74 72 5b 31 5d 20 3d 20  ];.    ptr[1] = 
16480 70 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 20 70 50  ptr[3];.  }.  pP
16481 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20  age->nCell--;.  
16482 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
16483 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
16484 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  3], pPage->nCell
16485 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  );.  pPage->nFre
16486 65 20 2b 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e  e += 2;.  return
16487 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
16488 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65  *.** Insert a ne
16489 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20  w cell on pPage 
1648a 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20 22 69  at cell index "i
1648b 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e 74 73  ".  pCell points
1648c 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65   to the.** conte
1648d 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2e 0a  nt of the cell..
1648e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c  **.** If the cel
1648f 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 66  l content will f
16490 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2c 20  it on the page, 
16491 74 68 65 6e 20 70 75 74 20 69 74 20 74 68 65 72  then put it ther
16492 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77 69 6c  e.  If it.** wil
16493 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65 6e 20  l not fit, then 
16494 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
16495 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
16496 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a 2a 2a  into pTemp if.**
16497 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20 6e 75   pTemp is not nu
16498 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73 73 20  ll.  Regardless 
16499 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f 63 61  of pTemp, alloca
1649a 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 0a 2a  te a new entry.*
1649b 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 4f 76 66  * in pPage->aOvf
1649c 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 69 74 20  l[] and make it 
1649d 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 63 65 6c  point to the cel
1649e 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 74 68 65  l content (eithe
1649f 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 20 6f 72  r.** in pTemp or
164a0 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 43   the original pC
164a1 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f 20 72 65  ell) and also re
164a2 63 6f 72 64 20 69 74 73 20 69 6e 64 65 78 2e 20  cord its index. 
164a3 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67 20 61  .** Allocating a
164a4 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 70 50   new entry in pP
164a5 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69 6d 70  age->aCell[] imp
164a6 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a 20 70 50  lies that .** pP
164a7 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 69  age->nOverflow i
164a8 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a  s incremented..*
164a9 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 70 20 69 73  *.** If nSkip is
164aa 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
164ab 64 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68 65 20  do not copy the 
164ac 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79 74 65  first nSkip byte
164ad 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 65 6c 6c  s of the.** cell
164ae 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c  . The caller wil
164af 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 6d  l overwrite them
164b0 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63   after this func
164b1 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20 49 66  tion returns. If
164b2 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e  .** nSkip is non
164b3 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 70 43 65 6c  -zero, then pCel
164b4 6c 20 6d 61 79 20 6e 6f 74 20 70 6f 69 6e 74 20  l may not point 
164b5 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 6d 65  to an invalid me
164b6 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 0a 2a  mory location .*
164b7 2a 20 28 62 75 74 20 70 43 65 6c 6c 2b 6e 53 6b  * (but pCell+nSk
164b8 69 70 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c  ip is always val
164b9 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  id)..*/.static i
164ba 6e 74 20 69 6e 73 65 72 74 43 65 6c 6c 28 0a 20  nt insertCell(. 
164bb 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c   MemPage *pPage,
164bc 20 20 20 2f 2a 20 50 61 67 65 20 69 6e 74 6f 20     /* Page into 
164bd 77 68 69 63 68 20 77 65 20 61 72 65 20 63 6f 70  which we are cop
164be 79 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 2c  ying */.  int i,
164bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
164c0 65 77 20 63 65 6c 6c 20 62 65 63 6f 6d 65 73 20  ew cell becomes 
164c1 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 6f 66  the i-th cell of
164c2 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75   the page */.  u
164c3 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20  8 *pCell,       
164c4 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74   /* Content of t
164c5 68 65 20 6e 65 77 20 63 65 6c 6c 20 2a 2f 0a 20  he new cell */. 
164c6 20 69 6e 74 20 73 7a 2c 20 20 20 20 20 20 20 20   int sz,        
164c7 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 63     /* Bytes of c
164c8 6f 6e 74 65 6e 74 20 69 6e 20 70 43 65 6c 6c 20  ontent in pCell 
164c9 2a 2f 0a 20 20 75 38 20 2a 70 54 65 6d 70 2c 20  */.  u8 *pTemp, 
164ca 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73         /* Temp s
164cb 74 6f 72 61 67 65 20 73 70 61 63 65 20 66 6f 72  torage space for
164cc 20 70 43 65 6c 6c 2c 20 69 66 20 6e 65 65 64 65   pCell, if neede
164cd 64 20 2a 2f 0a 20 20 75 38 20 6e 53 6b 69 70 20  d */.  u8 nSkip 
164ce 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e           /* Do n
164cf 6f 74 20 77 72 69 74 65 20 74 68 65 20 66 69 72  ot write the fir
164d0 73 74 20 6e 53 6b 69 70 20 62 79 74 65 73 20 6f  st nSkip bytes o
164d1 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 29 7b  f the cell */.){
164d2 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20  .  int idx;     
164d3 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f       /* Where to
164d4 20 77 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20   write new cell 
164d5 63 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b  content in data[
164d6 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20  ] */.  int j;   
164d7 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
164d8 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
164d9 74 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20  t top;          
164da 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66  /* First byte of
164db 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 61 6e 79   content for any
164dc 20 63 65 6c 6c 20 69 6e 20 64 61 74 61 5b 5d 20   cell in data[] 
164dd 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20 20 20  */.  int end;   
164de 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
164df 62 79 74 65 20 70 61 73 74 20 74 68 65 20 6c 61  byte past the la
164e0 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  st cell pointer 
164e1 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69  in data[] */.  i
164e2 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 20 20 20  nt ins;         
164e3 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64 61 74   /* Index in dat
164e4 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 20 63 65  a[] where new ce
164e5 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 69 6e  ll pointer is in
164e6 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  serted */.  int 
164e7 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  hdr;          /*
164e8 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 64 61 74   Offset into dat
164e9 61 5b 5d 20 6f 66 20 74 68 65 20 70 61 67 65 20  a[] of the page 
164ea 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20  header */.  int 
164eb 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 2f 2a  cellOffset;   /*
164ec 20 41 64 64 72 65 73 73 20 6f 66 20 66 69 72 73   Address of firs
164ed 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69  t cell pointer i
164ee 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38  n data[] */.  u8
164ef 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20   *data;         
164f0 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f  /* The content o
164f1 66 20 74 68 65 20 77 68 6f 6c 65 20 70 61 67 65  f the whole page
164f2 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20   */.  u8 *ptr;  
164f3 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
164f4 66 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66 6f 72  for moving infor
164f5 6d 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20 69 6e  mation around in
164f6 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20 61 73   data[] */..  as
164f7 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
164f8 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50  =pPage->nCell+pP
164f9 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29  age->nOverflow )
164fa 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
164fb 65 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c  e->nCell<=MX_CEL
164fc 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26  L(pPage->pBt) &&
164fd 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e   MX_CELL(pPage->
164fe 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20  pBt)<=5460 );.  
164ff 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
16500 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53  Overflow<=ArrayS
16501 69 7a 65 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c  ize(pPage->aOvfl
16502 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
16503 7a 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70  z==cellSizePtr(p
16504 50 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a  Page, pCell) );.
16505 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16506 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
16507 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
16508 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  );.  if( pPage->
16509 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b  nOverflow || sz+
1650a 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29  2>pPage->nFree )
1650b 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70 20  {.    if( pTemp 
1650c 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
1650d 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65  pTemp+nSkip, pCe
1650e 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b  ll+nSkip, sz-nSk
1650f 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c  ip);.      pCell
16510 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a   = pTemp;.    }.
16511 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d 3e 6e      j = pPage->n
16512 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20  Overflow++;.    
16513 61 73 73 65 72 74 28 20 6a 3c 28 69 6e 74 29 28  assert( j<(int)(
16514 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f  sizeof(pPage->aO
16515 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50 61 67  vfl)/sizeof(pPag
16516 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 20 29 3b  e->aOvfl[0])) );
16517 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f 76 66  .    pPage->aOvf
16518 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 70 43 65  l[j].pCell = pCe
16519 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61  ll;.    pPage->a
1651a 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d 20 28 75  Ovfl[j].idx = (u
1651b 31 36 29 69 3b 0a 20 20 20 20 70 50 61 67 65 2d  16)i;.    pPage-
1651c 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 7d 65  >nFree = 0;.  }e
1651d 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  lse{.    int rc 
1651e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1651f 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
16520 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
16521 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16522 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
16523 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
16524 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
16525 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
16526 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20  pDbPage) );.    
16527 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
16528 61 74 61 3b 0a 20 20 20 20 68 64 72 20 3d 20 70  ata;.    hdr = p
16529 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
1652a 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62  .    top = get2b
1652b 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
1652c 29 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65  );.    cellOffse
1652d 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f  t = pPage->cellO
1652e 66 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d  ffset;.    end =
1652f 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a   cellOffset + 2*
16530 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 2b 20 32  pPage->nCell + 2
16531 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63 65 6c 6c  ;.    ins = cell
16532 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b 0a 20 20  Offset + 2*i;.  
16533 20 20 69 66 28 20 65 6e 64 20 3e 20 74 6f 70 20    if( end > top 
16534 2d 20 73 7a 20 29 7b 0a 20 20 20 20 20 20 72 63  - sz ){.      rc
16535 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67   = defragmentPag
16536 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
16537 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
16538 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  K ){.        ret
16539 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
1653a 20 20 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32        top = get2
1653b 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
1653c 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ]);.      assert
1653d 28 20 65 6e 64 20 2b 20 73 7a 20 3c 3d 20 74 6f  ( end + sz <= to
1653e 70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  p );.    }.    i
1653f 64 78 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61  dx = allocateSpa
16540 63 65 28 70 50 61 67 65 2c 20 73 7a 29 3b 0a 20  ce(pPage, sz);. 
16541 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 30     assert( idx>0
16542 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
16543 65 6e 64 20 3c 3d 20 67 65 74 32 62 79 74 65 28  end <= get2byte(
16544 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 29 3b  &data[hdr+5]) );
16545 0a 20 20 20 20 69 66 20 28 69 64 78 2b 73 7a 20  .    if (idx+sz 
16546 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  > pPage->pBt->us
16547 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20 20 20 20  ableSize) {.    
16548 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
16549 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1654a 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e    }.    pPage->n
1654b 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 61 67  Cell++;.    pPag
1654c 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 3b 0a 20  e->nFree -= 2;. 
1654d 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b     memcpy(&data[
1654e 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65 6c  idx+nSkip], pCel
1654f 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69  l+nSkip, sz-nSki
16550 70 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e  p);.    for(j=en
16551 64 2d 32 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a  d-2, ptr=&data[j
16552 5d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20  ]; j>ins; j-=2, 
16553 70 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70  ptr-=2){.      p
16554 74 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b  tr[0] = ptr[-2];
16555 0a 20 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20  .      ptr[1] = 
16556 70 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20  ptr[-1];.    }. 
16557 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74     put2byte(&dat
16558 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20  a[ins], idx);.  
16559 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
1655a 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d 3e  [hdr+3], pPage->
1655b 6e 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20  nCell);.#ifndef 
1655c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
1655d 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70  VACUUM.    if( p
1655e 50 61 67 65 2d 3e 70 42 74 2d 3e 61 75 74 6f 56  Page->pBt->autoV
1655f 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 2f  acuum ){.      /
16560 2a 20 54 68 65 20 63 65 6c 6c 20 6d 61 79 20 63  * The cell may c
16561 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72  ontain a pointer
16562 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20   to an overflow 
16563 70 61 67 65 2e 20 49 66 20 73 6f 2c 20 77 72 69  page. If so, wri
16564 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  te.      ** the 
16565 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76  entry for the ov
16566 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 74 6f  erflow page into
16567 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
16568 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
16569 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
1656a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1656b 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
1656c 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e  Page, pCell, &in
1656d 66 6f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  fo);.      asser
1656e 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28  t( (info.nData+(
1656f 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a  pPage->intKey?0:
16570 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66  info.nKey))==inf
16571 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20  o.nPayload );.  
16572 20 20 20 20 69 66 28 20 28 69 6e 66 6f 2e 6e 44      if( (info.nD
16573 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b  ata+(pPage->intK
16574 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29  ey?0:info.nKey))
16575 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a  >info.nLocal ){.
16576 20 20 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e          Pgno pgn
16577 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65  oOvfl = get4byte
16578 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
16579 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20  erflow]);.      
1657a 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
1657b 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 67 6e  (pPage->pBt, pgn
1657c 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56  oOvfl, PTRMAP_OV
1657d 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e  ERFLOW1, pPage->
1657e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69  pgno);.        i
1657f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
16580 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
16581 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64      }.    }.#end
16582 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  if.  }..  return
16583 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
16584 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 73 74 20  *.** Add a list 
16585 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 20 70 61  of cells to a pa
16586 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 73 68  ge.  The page sh
16587 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 6c  ould be initiall
16588 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 65 20  y empty..** The 
16589 63 65 6c 6c 73 20 61 72 65 20 67 75 61 72 61 6e  cells are guaran
1658a 74 65 65 64 20 74 6f 20 66 69 74 20 6f 6e 20 74  teed to fit on t
1658b 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74  he page..*/.stat
1658c 69 63 20 76 6f 69 64 20 61 73 73 65 6d 62 6c 65  ic void assemble
1658d 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 65 20  Page(.  MemPage 
1658e 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 54 68 65  *pPage,   /* The
1658f 20 70 61 67 65 20 74 6f 20 62 65 20 61 73 73 65   page to be asse
16590 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 6e 74 20  mblied */.  int 
16591 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a  nCell,        /*
16592 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   The number of c
16593 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 6f 20 74  ells to add to t
16594 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 75 38  his page */.  u8
16595 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20   **apCell,      
16596 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 63  /* Pointers to c
16597 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f 0a 20 20  ell bodies */.  
16598 75 31 36 20 2a 61 53 69 7a 65 20 20 20 20 20 20  u16 *aSize      
16599 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 74 68    /* Sizes of th
1659a 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a 20 20  e cells */.){.  
1659b 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
1659c 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1659d 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 74 61 6c  r */.  int total
1659e 53 69 7a 65 3b 20 20 20 20 2f 2a 20 54 6f 74 61  Size;    /* Tota
1659f 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65  l size of all ce
165a0 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72  lls */.  int hdr
165a1 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ;          /* In
165a2 64 65 78 20 6f 66 20 70 61 67 65 20 68 65 61 64  dex of page head
165a3 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c  er */.  int cell
165a4 70 74 72 3b 20 20 20 20 20 20 2f 2a 20 41 64 64  ptr;      /* Add
165a5 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c  ress of next cel
165a6 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69  l pointer */.  i
165a7 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 20 20  nt cellbody;    
165a8 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e   /* Address of n
165a9 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 2a 2f  ext cell body */
165aa 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20  .  u8 *data;    
165ab 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72       /* Data for
165ac 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20   the page */..  
165ad 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
165ae 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20  Overflow==0 );. 
165af 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
165b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
165b1 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
165b2 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c  ;.  assert( nCel
165b3 6c 3e 3d 30 20 26 26 20 6e 43 65 6c 6c 3c 3d 4d  l>=0 && nCell<=M
165b4 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42  X_CELL(pPage->pB
165b5 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50  t) && MX_CELL(pP
165b6 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20  age->pBt)<=5460 
165b7 29 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 3d  );.  totalSize =
165b8 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
165b9 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
165ba 20 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d 20 61    totalSize += a
165bb 53 69 7a 65 5b 69 5d 3b 0a 20 20 7d 0a 20 20 61  Size[i];.  }.  a
165bc 73 73 65 72 74 28 20 74 6f 74 61 6c 53 69 7a 65  ssert( totalSize
165bd 2b 32 2a 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d  +2*nCell<=pPage-
165be 3e 6e 46 72 65 65 20 29 3b 0a 20 20 61 73 73 65  >nFree );.  asse
165bf 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  rt( pPage->nCell
165c0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
165c1 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
165c2 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
165c3 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 63 65  pDbPage) );.  ce
165c4 6c 6c 70 74 72 20 3d 20 70 50 61 67 65 2d 3e 63  llptr = pPage->c
165c5 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 64 61 74  ellOffset;.  dat
165c6 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61  a = pPage->aData
165c7 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d  ;.  hdr = pPage-
165c8 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 70 75  >hdrOffset;.  pu
165c9 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
165ca 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 69  +3], nCell);.  i
165cb 66 28 20 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20  f( nCell ){.    
165cc 63 65 6c 6c 62 6f 64 79 20 3d 20 61 6c 6c 6f 63  cellbody = alloc
165cd 61 74 65 53 70 61 63 65 28 70 50 61 67 65 2c 20  ateSpace(pPage, 
165ce 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 20 20  totalSize);.    
165cf 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79  assert( cellbody
165d0 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
165d1 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e  ( pPage->nFree >
165d2 3d 20 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  = 2*nCell );.   
165d3 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d   pPage->nFree -=
165d4 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 20 20 66 6f   2*nCell;.    fo
165d5 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
165d6 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 75 74 32  i++){.      put2
165d7 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 70  byte(&data[cellp
165d8 74 72 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a  tr], cellbody);.
165d9 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61        memcpy(&da
165da 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70  ta[cellbody], ap
165db 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69  Cell[i], aSize[i
165dc 5d 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 70 74  ]);.      cellpt
165dd 72 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 63 65  r += 2;.      ce
165de 6c 6c 62 6f 64 79 20 2b 3d 20 61 53 69 7a 65 5b  llbody += aSize[
165df 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  i];.    }.    as
165e0 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79 3d 3d  sert( cellbody==
165e1 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
165e2 6c 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 20 20  leSize );.  }.  
165e3 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28  pPage->nCell = (
165e4 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a  u16)nCell;.}../*
165e5 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
165e6 67 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74  g parameters det
165e7 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20  ermine how many 
165e8 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67  adjacent pages g
165e9 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69  et involved.** i
165ea 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70  n a balancing op
165eb 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20  eration.  NN is 
165ec 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65  the number of ne
165ed 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65  ighbors on eithe
165ee 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65  r side.** of the
165ef 20 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69   page that parti
165f0 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61  cipate in the ba
165f1 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f  lancing operatio
165f2 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a  n.  NB is the.**
165f3 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
165f4 20 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74   pages that part
165f5 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69  icipate, includi
165f6 6e 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61  ng the target pa
165f7 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69  ge and.** NN nei
165f8 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72  ghbors on either
165f9 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   side..**.** The
165fa 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f   minimum value o
165fb 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f  f NN is 1 (of co
165fc 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69  urse).  Increasi
165fd 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a  ng NN above 1.**
165fe 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76   (to 2 or 3) giv
165ff 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72  es a modest impr
16600 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43  ovement in SELEC
16601 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72  T and DELETE per
16602 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65  formance.** in e
16603 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61  xchange for a la
16604 72 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e  rger degradation
16605 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55   in INSERT and U
16606 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63  PDATE performanc
16607 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  e..** The value 
16608 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f  of NN appears to
16609 20 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72   give the best r
1660a 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a  esults overall..
1660b 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20  */.#define NN 1 
1660c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1660d 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f  umber of neighbo
1660e 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64  rs on either sid
1660f 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64  e of pPage */.#d
16610 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31  efine NB (NN*2+1
16611 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20  )      /* Total 
16612 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69  pages involved i
16613 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f  n the balance */
16614 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66  ../* Forward ref
16615 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63  erence */.static
16616 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 74 43   int balance(BtC
16617 75 72 73 6f 72 2a 2c 20 69 6e 74 29 3b 0a 0a 23  ursor*, int);..#
16618 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16619 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a  IT_QUICKBALANCE.
1661a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
1661b 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65 28 29 20  on of balance() 
1661c 68 61 6e 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d  handles the comm
1661d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61 73 65 20  on special case 
1661e 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65  where.** a new e
1661f 6e 74 72 79 20 69 73 20 62 65 69 6e 67 20 69 6e  ntry is being in
16620 73 65 72 74 65 64 20 6f 6e 20 74 68 65 20 65 78  serted on the ex
16621 74 72 65 6d 65 20 72 69 67 68 74 2d 65 6e 64 20  treme right-end 
16622 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20  of the.** tree, 
16623 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  in other words, 
16624 77 68 65 6e 20 74 68 65 20 6e 65 77 20 65 6e 74  when the new ent
16625 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74  ry will become t
16626 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e  he largest.** en
16627 74 72 79 20 69 6e 20 74 68 65 20 74 72 65 65 2e  try in the tree.
16628 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f  .**.** Instead o
16629 66 20 74 72 79 69 6e 67 20 62 61 6c 61 6e 63 65  f trying balance
1662a 20 74 68 65 20 33 20 72 69 67 68 74 2d 6d 6f 73   the 3 right-mos
1662b 74 20 6c 65 61 66 20 70 61 67 65 73 2c 20 6a 75  t leaf pages, ju
1662c 73 74 20 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20  st add.** a new 
1662d 70 61 67 65 20 74 6f 20 74 68 65 20 72 69 67 68  page to the righ
1662e 74 2d 68 61 6e 64 20 73 69 64 65 20 61 6e 64 20  t-hand side and 
1662f 70 75 74 20 74 68 65 20 6f 6e 65 20 6e 65 77 20  put the one new 
16630 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74  entry in.** that
16631 20 70 61 67 65 2e 20 20 54 68 69 73 20 6c 65 61   page.  This lea
16632 76 65 73 20 74 68 65 20 72 69 67 68 74 20 73 69  ves the right si
16633 64 65 20 6f 66 20 74 68 65 20 74 72 65 65 20 73  de of the tree s
16634 6f 6d 65 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c  omewhat.** unbal
16635 61 6e 63 65 64 2e 20 20 42 75 74 20 6f 64 64 73  anced.  But odds
16636 20 61 72 65 20 74 68 61 74 20 77 65 20 77 69 6c   are that we wil
16637 6c 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20 6e  l be inserting n
16638 65 77 20 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74  ew entries.** at
16639 20 74 68 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66   the end soon af
1663a 74 65 72 77 61 72 64 73 20 73 6f 20 74 68 65 20  terwards so the 
1663b 6e 65 61 72 6c 79 20 65 6d 70 74 79 20 70 61 67  nearly empty pag
1663c 65 20 77 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a  e will quickly.*
1663d 2a 20 66 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61  * fill up.  On a
1663e 76 65 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50  verage..**.** pP
1663f 61 67 65 20 69 73 20 74 68 65 20 6c 65 61 66 20  age is the leaf 
16640 70 61 67 65 20 77 68 69 63 68 20 69 73 20 74 68  page which is th
16641 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 70 61 67  e right-most pag
16642 65 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  e in the tree..*
16643 2a 20 70 50 61 72 65 6e 74 20 69 73 20 69 74 73  * pParent is its
16644 20 70 61 72 65 6e 74 2e 20 20 70 50 61 67 65 20   parent.  pPage 
16645 6d 75 73 74 20 68 61 76 65 20 61 20 73 69 6e 67  must have a sing
16646 6c 65 20 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72  le overflow entr
16647 79 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61 6c  y.** which is al
16648 73 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  so the right-mos
16649 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70  t entry on the p
1664a 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  age..*/.static i
1664b 6e 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  nt balance_quick
1664c 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
1664d 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65  {.  int rc;.  Me
1664e 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d 20 30 3b  mPage *pNew = 0;
1664f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b  .  Pgno pgnoNew;
16650 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20  .  u8 *pCell;.  
16651 75 31 36 20 73 7a 43 65 6c 6c 3b 0a 20 20 43 65  u16 szCell;.  Ce
16652 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 4d  llInfo info;.  M
16653 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
16654 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
16655 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 4d 65 6d  r->iPage];.  Mem
16656 50 61 67 65 20 2a 70 50 61 72 65 6e 74 20 3d 20  Page *pParent = 
16657 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
16658 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 42  r->iPage-1];.  B
16659 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1665a 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74  Page->pBt;.  int
1665b 20 70 61 72 65 6e 74 49 64 78 20 3d 20 70 50 61   parentIdx = pPa
1665c 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 20 20 20 2f  rent->nCell;   /
1665d 2a 20 70 50 61 72 65 6e 74 20 6e 65 77 20 64 69  * pParent new di
1665e 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 64 65 78  vider cell index
1665f 20 2a 2f 0a 20 20 69 6e 74 20 70 61 72 65 6e 74   */.  int parent
16660 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
16661 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
16662 6f 66 20 6e 65 77 20 64 69 76 69 64 65 72 20 63  of new divider c
16663 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 70 61 72 65  ell */.  u8 pare
16664 6e 74 43 65 6c 6c 5b 36 34 5d 3b 20 20 20 20 20  ntCell[64];     
16665 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70             /* Sp
16666 61 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ace for the new 
16667 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a  divider cell */.
16668 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16669 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
1666a 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
1666b 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61   );..  /* Alloca
1666c 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 49  te a new page. I
1666d 6e 73 65 72 74 20 74 68 65 20 6f 76 65 72 66 6c  nsert the overfl
1666e 6f 77 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61  ow cell from pPa
1666f 67 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 69 74 2e  ge.  ** into it.
16670 20 54 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65   Then remove the
16671 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 66   overflow cell f
16672 72 6f 6d 20 70 50 61 67 65 2e 0a 20 20 2a 2f 0a  rom pPage..  */.
16673 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
16674 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70  treePage(pBt, &p
16675 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30  New, &pgnoNew, 0
16676 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , 0);.  if( rc==
16677 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16678 20 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e   pCell = pPage->
16679 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a  aOvfl[0].pCell;.
1667a 20 20 20 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c      szCell = cel
1667b 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20  lSizePtr(pPage, 
1667c 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73 73 65  pCell);.    asse
1667d 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
1667e 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77  Iswriteable(pNew
1667f 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
16680 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c    zeroPage(pNew,
16681 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d   pPage->aData[0]
16682 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50  );.    assembleP
16683 61 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43  age(pNew, 1, &pC
16684 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20  ell, &szCell);. 
16685 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66     pPage->nOverf
16686 6c 6f 77 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20  low = 0;.  .    
16687 2f 2a 20 70 50 61 67 65 20 69 73 20 63 75 72 72  /* pPage is curr
16688 65 6e 74 6c 79 20 74 68 65 20 72 69 67 68 74 2d  ently the right-
16689 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74  child of pParent
1668a 2e 20 43 68 61 6e 67 65 20 74 68 69 73 0a 20 20  . Change this.  
1668b 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65    ** so that the
1668c 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69 73 20   right-child is 
1668d 74 68 65 20 6e 65 77 20 70 61 67 65 20 61 6c 6c  the new page all
1668e 6f 63 61 74 65 64 20 61 62 6f 76 65 20 61 6e 64  ocated above and
1668f 0a 20 20 20 20 2a 2a 20 70 50 61 67 65 20 69 73  .    ** pPage is
16690 20 74 68 65 20 6e 65 78 74 2d 74 6f 2d 72 69 67   the next-to-rig
16691 68 74 20 63 68 69 6c 64 2e 20 0a 20 20 20 20 2a  ht child. .    *
16692 2a 0a 20 20 20 20 2a 2a 20 49 67 6e 6f 72 65 20  *.    ** Ignore 
16693 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
16694 20 6f 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20   of the call to 
16695 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 2e 20 66 69  fillInCell(). fi
16696 6c 6c 49 6e 43 65 6c 6c 28 29 0a 20 20 20 20 2a  llInCell().    *
16697 2a 20 6d 61 79 20 6f 6e 6c 79 20 72 65 74 75 72  * may only retur
16698 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c  n other than SQL
16699 49 54 45 5f 4f 4b 20 69 66 20 69 74 20 69 73 20  ITE_OK if it is 
1669a 72 65 71 75 69 72 65 64 20 74 6f 20 61 6c 6c 6f  required to allo
1669b 63 61 74 65 0a 20 20 20 20 2a 2a 20 6f 6e 65 20  cate.    ** one 
1669c 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77  or more overflow
1669d 20 70 61 67 65 73 2e 20 53 69 6e 63 65 20 61 6e   pages. Since an
1669e 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20   internal table 
1669f 42 2d 54 72 65 65 20 63 65 6c 6c 20 0a 20 20 20  B-Tree cell .   
166a0 20 2a 2a 20 6d 61 79 20 6e 65 76 65 72 20 73 70   ** may never sp
166a1 69 6c 6c 20 6f 76 65 72 20 6f 6e 74 6f 20 61 6e  ill over onto an
166a2 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 28   overflow page (
166a3 69 74 20 69 73 20 61 20 6d 61 78 69 6d 75 6d 20  it is a maximum 
166a4 6f 66 20 0a 20 20 20 20 2a 2a 20 31 33 20 62 79  of .    ** 13 by
166a5 74 65 73 20 69 6e 20 73 69 7a 65 29 2c 20 69 74  tes in size), it
166a6 20 69 73 20 6e 6f 74 20 6e 65 63 63 65 73 73 61   is not neccessa
166a7 72 79 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20  ry to check the 
166a8 72 65 74 75 72 6e 20 63 6f 64 65 2e 0a 20 20 20  return code..   
166a9 20 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c   **.    ** Simil
166aa 61 72 6c 79 2c 20 74 68 65 20 69 6e 73 65 72 74  arly, the insert
166ab 43 65 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20  Cell() function 
166ac 63 61 6e 6e 6f 74 20 66 61 69 6c 20 69 66 20 74  cannot fail if t
166ad 68 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 62  he page.    ** b
166ae 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e  eing inserted in
166af 74 6f 20 69 73 20 61 6c 72 65 61 64 79 20 77 72  to is already wr
166b0 69 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 63  itable and the c
166b1 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 0a 20 20  ell does not .  
166b2 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 6e 20    ** contain an 
166b3 6f 76 65 72 66 6c 6f 77 20 70 6f 69 6e 74 65 72  overflow pointer
166b4 2e 20 53 6f 20 69 67 6e 6f 72 65 20 74 68 69 73  . So ignore this
166b5 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f 6f   return code too
166b6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
166b7 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c  ert( pPage->nCel
166b8 6c 3e 30 20 29 3b 0a 20 20 20 20 70 43 65 6c 6c  l>0 );.    pCell
166b9 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67   = findCell(pPag
166ba 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d  e, pPage->nCell-
166bb 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  1);.    sqlite3B
166bc 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
166bd 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26  (pPage, pCell, &
166be 69 6e 66 6f 29 3b 0a 20 20 20 20 66 69 6c 6c 49  info);.    fillI
166bf 6e 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70  nCell(pParent, p
166c0 61 72 65 6e 74 43 65 6c 6c 2c 20 30 2c 20 69 6e  arentCell, 0, in
166c1 66 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c 20 30  fo.nKey, 0, 0, 0
166c2 2c 20 26 70 61 72 65 6e 74 53 69 7a 65 29 3b 0a  , &parentSize);.
166c3 20 20 20 20 61 73 73 65 72 74 28 20 70 61 72 65      assert( pare
166c4 6e 74 53 69 7a 65 3c 36 34 20 29 3b 0a 20 20 20  ntSize<64 );.   
166c5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
166c6 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
166c7 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67  (pParent->pDbPag
166c8 65 29 20 29 3b 0a 20 20 20 20 69 6e 73 65 72 74  e) );.    insert
166c9 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61  Cell(pParent, pa
166ca 72 65 6e 74 49 64 78 2c 20 70 61 72 65 6e 74 43  rentIdx, parentC
166cb 65 6c 6c 2c 20 70 61 72 65 6e 74 53 69 7a 65 2c  ell, parentSize,
166cc 20 30 2c 20 34 29 3b 0a 20 20 20 20 70 75 74 34   0, 4);.    put4
166cd 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f  byte(findOverflo
166ce 77 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 70 61  wCell(pParent,pa
166cf 72 65 6e 74 49 64 78 29 2c 20 70 50 61 67 65 2d  rentIdx), pPage-
166d0 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 70 75 74 34  >pgno);.    put4
166d1 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61  byte(&pParent->a
166d2 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64  Data[pParent->hd
166d3 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f  rOffset+8], pgno
166d4 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20  New);.  .    /* 
166d5 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75  If this is an au
166d6 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61  to-vacuum databa
166d7 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70  se, update the p
166d8 6f 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a  ointer map.    *
166d9 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73 20 66  * with entries f
166da 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c  or the new page,
166db 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72   and any pointer
166dc 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a   from the .    *
166dd 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61  * cell on the pa
166de 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f  ge to an overflo
166df 77 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20  w page..    */. 
166e0 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
166e1 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20  UUM ){.      rc 
166e2 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c  = ptrmapPut(pBt,
166e3 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50   pgnoNew, PTRMAP
166e4 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d  _BTREE, pParent-
166e5 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66  >pgno);.      if
166e6 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
166e7 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
166e8 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e  ptrmapPutOvfl(pN
166e9 65 77 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  ew, 0);.      }.
166ea 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
166eb 6c 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65  lease the refere
166ec 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 77 20 70  nce to the new p
166ed 61 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65  age. */.    rele
166ee 61 73 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20  asePage(pNew);. 
166ef 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73   }..  /* At this
166f0 20 70 6f 69 6e 74 20 74 68 65 20 70 50 61 67 65   point the pPage
166f1 2d 3e 6e 46 72 65 65 20 76 61 72 69 61 62 6c 65  ->nFree variable
166f2 20 69 73 20 6e 6f 74 20 73 65 74 20 63 6f 72 72   is not set corr
166f3 65 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20  ectly with.  ** 
166f4 72 65 73 70 65 63 74 20 74 6f 20 74 68 65 20 63  respect to the c
166f5 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61  ontent of the pa
166f6 67 65 20 28 62 65 63 61 75 73 65 20 69 74 20 77  ge (because it w
166f7 61 73 20 73 65 74 20 74 6f 20 30 20 62 79 20 0a  as set to 0 by .
166f8 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 29    ** insertCell)
166f9 2e 20 53 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65  . So call sqlite
166fa 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29  3BtreeInitPage()
166fb 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74   to make sure it
166fc 20 69 73 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74   is.  ** correct
166fd 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
166fe 20 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 20   has to be done 
166ff 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72  even if an error
16700 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65   will be returne
16701 64 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 0a  d. Normally, if.
16702 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63    ** an error oc
16703 63 75 72 73 20 64 75 72 69 6e 67 20 74 72 65 65  curs during tree
16704 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 20   balancing, the 
16705 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65 6d 50  contents of MemP
16706 61 67 65 20 61 72 65 0a 20 20 2a 2a 20 6e 6f 74  age are.  ** not
16707 20 69 6d 70 6f 72 74 61 6e 74 2c 20 61 73 20 74   important, as t
16708 68 65 79 20 77 69 6c 6c 20 62 65 20 72 65 63 61  hey will be reca
16709 6c 63 75 6c 61 74 65 64 20 77 68 65 6e 20 74 68  lculated when th
1670a 65 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64  e page is rolled
1670b 0a 20 20 2a 2a 20 62 61 63 6b 2e 20 42 75 74 20  .  ** back. But 
1670c 68 65 72 65 2c 20 69 6e 20 62 61 6c 61 6e 63 65  here, in balance
1670d 5f 71 75 69 63 6b 28 29 2c 20 69 74 20 69 73 20  _quick(), it is 
1670e 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 70 50  possible that pP
1670f 61 67 65 20 68 61 73 20 0a 20 20 2a 2a 20 6e 6f  age has .  ** no
16710 74 20 79 65 74 20 62 65 65 6e 20 6d 61 72 6b 65  t yet been marke
16711 64 20 64 69 72 74 79 20 6f 72 20 77 72 69 74 74  d dirty or writt
16712 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  en into the jour
16713 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 66  nal file. Theref
16714 6f 72 65 0a 20 20 2a 2a 20 69 74 20 77 69 6c 6c  ore.  ** it will
16715 20 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 62   not be rolled b
16716 61 63 6b 20 61 6e 64 20 73 6f 20 69 74 20 69 73  ack and so it is
16717 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6d 61   important to ma
16718 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a  ke sure that.  *
16719 2a 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20  * the page data 
1671a 61 6e 64 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  and contents of 
1671b 4d 65 6d 50 61 67 65 20 61 72 65 20 63 6f 6e 73  MemPage are cons
1671c 69 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70  istent..  */.  p
1671d 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30  Page->isInit = 0
1671e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1671f 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b  InitPage(pPage);
16720 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
16721 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29  ->nOverflow==0 )
16722 3b 0a 0a 20 20 2f 2a 20 49 66 20 65 76 65 72 79  ;..  /* If every
16723 74 68 69 6e 67 20 65 6c 73 65 20 73 75 63 63 65  thing else succe
16724 65 64 65 64 2c 20 62 61 6c 61 6e 63 65 20 74 68  eded, balance th
16725 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 69  e parent page, i
16726 6e 20 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65  n .  ** case the
16727 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e   divider cell in
16728 73 65 72 74 65 64 20 63 61 75 73 65 64 20 69 74  serted caused it
16729 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66   to become overf
1672a 75 6c 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ull..  */.  if( 
1672b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1672c 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
1672d 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 43 75  (pPage);.    pCu
1672e 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20  r->iPage--;.    
1672f 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75  rc = balance(pCu
16730 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  r, 0);.  }.  ret
16731 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
16732 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
16733 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a  QUICKBALANCE */.
16734 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
16735 69 6e 65 20 72 65 64 69 73 74 72 69 62 75 74 65  ine redistribute
16736 73 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65  s Cells on pPage
16737 20 61 6e 64 20 75 70 20 74 6f 20 4e 4e 2a 32 20   and up to NN*2 
16738 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20 70  siblings.** of p
16739 50 61 67 65 20 73 6f 20 74 68 61 74 20 61 6c 6c  Page so that all
1673a 20 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75   pages have abou
1673b 74 20 74 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e  t the same amoun
1673c 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e  t of free space.
1673d 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 4e 4e 20 73  .** Usually NN s
1673e 69 62 6c 69 6e 67 73 20 6f 6e 20 65 69 74 68 65  iblings on eithe
1673f 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 20  r side of pPage 
16740 69 73 20 75 73 65 64 20 69 6e 20 74 68 65 20 62  is used in the b
16741 61 6c 61 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68 6f  alancing,.** tho
16742 75 67 68 20 6d 6f 72 65 20 73 69 62 6c 69 6e 67  ugh more sibling
16743 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 72 6f  s might come fro
16744 6d 20 6f 6e 65 20 73 69 64 65 20 69 66 20 70 50  m one side if pP
16745 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74  age is the first
16746 0a 2a 2a 20 6f 72 20 6c 61 73 74 20 63 68 69 6c  .** or last chil
16747 64 20 6f 66 20 69 74 73 20 70 61 72 65 6e 74 2e  d of its parent.
16748 20 20 49 66 20 70 50 61 67 65 20 68 61 73 20 66    If pPage has f
16749 65 77 65 72 20 74 68 61 6e 20 32 2a 4e 4e 20 73  ewer than 2*NN s
1674a 69 62 6c 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d 65  iblings.** (some
1674b 74 68 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20  thing which can 
1674c 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 70  only happen if p
1674d 50 61 67 65 20 69 73 20 74 68 65 20 72 6f 6f 74  Page is the root
1674e 20 70 61 67 65 20 6f 72 20 61 20 0a 2a 2a 20 63   page or a .** c
1674f 68 69 6c 64 20 6f 66 20 72 6f 6f 74 29 20 74 68  hild of root) th
16750 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65  en all available
16751 20 73 69 62 6c 69 6e 67 73 20 70 61 72 74 69 63   siblings partic
16752 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c  ipate in the bal
16753 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  ancing..**.** Th
16754 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c  e number of sibl
16755 69 6e 67 73 20 6f 66 20 70 50 61 67 65 20 6d 69  ings of pPage mi
16756 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65 64  ght be increased
16757 20 6f 72 20 64 65 63 72 65 61 73 65 64 20 62 79   or decreased by
16758 20 6f 6e 65 20 6f 72 0a 2a 2a 20 74 77 6f 20 69   one or.** two i
16759 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b  n an effort to k
1675a 65 65 70 20 70 61 67 65 73 20 6e 65 61 72 6c 79  eep pages nearly
1675b 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76   full but not ov
1675c 65 72 20 66 75 6c 6c 2e 20 54 68 65 20 72 6f 6f  er full. The roo
1675d 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 73 70 65  t page.** is spe
1675e 63 69 61 6c 20 61 6e 64 20 69 73 20 61 6c 6c 6f  cial and is allo
1675f 77 65 64 20 74 6f 20 62 65 20 6e 65 61 72 6c 79  wed to be nearly
16760 20 65 6d 70 74 79 2e 20 49 66 20 70 50 61 67 65   empty. If pPage
16761 20 69 73 20 0a 2a 2a 20 74 68 65 20 72 6f 6f 74   is .** the root
16762 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20   page, then the 
16763 64 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65  depth of the tre
16764 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65  e might be incre
16765 61 73 65 64 0a 2a 2a 20 6f 72 20 64 65 63 72 65  ased.** or decre
16766 61 73 65 64 20 62 79 20 6f 6e 65 2c 20 61 73 20  ased by one, as 
16767 6e 65 63 65 73 73 61 72 79 2c 20 74 6f 20 6b 65  necessary, to ke
16768 65 70 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  ep the root page
16769 20 66 72 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20 6f   from being.** o
1676a 76 65 72 66 75 6c 6c 20 6f 72 20 63 6f 6d 70 6c  verfull or compl
1676b 65 74 65 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a  etely empty..**.
1676c 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65  ** Note that whe
1676d 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
1676e 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f  s called, some o
1676f 66 20 74 68 65 20 43 65 6c 6c 73 20 6f 6e 20 70  f the Cells on p
16770 50 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f  Page.** might no
16771 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 73 74  t actually be st
16772 6f 72 65 64 20 69 6e 20 70 50 61 67 65 2d 3e 61  ored in pPage->a
16773 44 61 74 61 5b 5d 2e 20 20 54 68 69 73 20 63 61  Data[].  This ca
16774 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74  n happen.** if t
16775 68 65 20 70 61 67 65 20 69 73 20 6f 76 65 72 66  he page is overf
16776 75 6c 6c 2e 20 20 50 61 72 74 20 6f 66 20 74 68  ull.  Part of th
16777 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f  e job of this ro
16778 75 74 69 6e 65 20 69 73 20 74 6f 0a 2a 2a 20 6d  utine is to.** m
16779 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 43 65 6c  ake sure all Cel
1677a 6c 73 20 66 6f 72 20 70 50 61 67 65 20 6f 6e 63  ls for pPage onc
1677b 65 20 61 67 61 69 6e 20 66 69 74 20 69 6e 20 70  e again fit in p
1677c 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a 2a  Page->aData[]..*
1677d 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75 72  *.** In the cour
1677e 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20  se of balancing 
1677f 74 68 65 20 73 69 62 6c 69 6e 67 73 20 6f 66 20  the siblings of 
16780 70 50 61 67 65 2c 20 74 68 65 20 70 61 72 65 6e  pPage, the paren
16781 74 20 6f 66 20 70 50 61 67 65 0a 2a 2a 20 6d 69  t of pPage.** mi
16782 67 68 74 20 62 65 63 6f 6d 65 20 6f 76 65 72 66  ght become overf
16783 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c  ull or underfull
16784 2e 20 20 49 66 20 74 68 61 74 20 68 61 70 70 65  .  If that happe
16785 6e 73 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  ns, then this ro
16786 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c  utine.** is call
16787 65 64 20 72 65 63 75 72 73 69 76 65 6c 79 20 6f  ed recursively o
16788 6e 20 74 68 65 20 70 61 72 65 6e 74 2e 0a 2a 2a  n the parent..**
16789 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74  .** If this rout
1678a 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e  ine fails for an
1678b 79 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67  y reason, it mig
1678c 68 74 20 6c 65 61 76 65 20 74 68 65 20 64 61 74  ht leave the dat
1678d 61 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f  abase.** in a co
1678e 72 72 75 70 74 65 64 20 73 74 61 74 65 2e 20 20  rrupted state.  
1678f 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69  So if this routi
16790 6e 65 20 66 61 69 6c 73 2c 20 74 68 65 20 64 61  ne fails, the da
16791 74 61 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a  tabase should.**
16792 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   be rolled back.
16793 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
16794 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 42  alance_nonroot(B
16795 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
16796 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
16797 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
16798 2a 20 54 68 65 20 6f 76 65 72 20 6f 72 20 75 6e  * The over or un
16799 64 65 72 66 75 6c 6c 20 70 61 67 65 20 74 6f 20  derfull page to 
1679a 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 4d 65 6d  balance */.  Mem
1679b 50 61 67 65 20 2a 70 50 61 72 65 6e 74 3b 20 20  Page *pParent;  
1679c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1679d 20 70 61 72 65 6e 74 20 6f 66 20 70 50 61 67 65   parent of pPage
1679e 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a   */.  BtShared *
1679f 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  pBt;            
167a0 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20     /* The whole 
167a1 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e  database */.  in
167a2 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20  t nCell = 0;    
167a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
167a4 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e  mber of cells in
167a5 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69   apCell[] */.  i
167a6 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30  nt nMaxCells = 0
167a7 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  ;           /* A
167a8 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66  llocated size of
167a9 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c   apCell, szCell,
167aa 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74   aFrom. */.  int
167ab 20 6e 4f 6c 64 20 3d 20 30 3b 20 20 20 20 20 20   nOld = 0;      
167ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
167ad 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
167ae 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74  apOld[] */.  int
167af 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20   nNew = 0;      
167b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
167b1 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
167b2 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74  apNew[] */.  int
167b3 20 6e 44 69 76 3b 20 20 20 20 20 20 20 20 20 20   nDiv;          
167b4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
167b5 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20  ber of cells in 
167b6 61 70 44 69 76 5b 5d 20 2a 2f 0a 20 20 69 6e 74  apDiv[] */.  int
167b7 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20   i, j, k;       
167b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
167b9 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
167ba 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20  int idx;        
167bb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
167bc 49 6e 64 65 78 20 6f 66 20 70 50 61 67 65 20 69  Index of pPage i
167bd 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c  n pParent->aCell
167be 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69  [] */.  int nxDi
167bf 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
167c0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 64 69 76       /* Next div
167c1 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 70 50 61  ider slot in pPa
167c2 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f  rent->aCell[] */
167c3 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
167c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
167c5 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f  /* The return co
167c6 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66  de */.  int leaf
167c7 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20 20 20  Correction;     
167c8 20 20 20 20 20 2f 2a 20 34 20 69 66 20 70 50 61       /* 4 if pPa
167c9 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30  ge is a leaf.  0
167ca 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74   if not */.  int
167cb 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20 20 20   leafData;      
167cc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
167cd 65 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20  e if pPage is a 
167ce 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 46 44 41  leaf of a LEAFDA
167cf 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74  TA tree */.  int
167d0 20 75 73 61 62 6c 65 53 70 61 63 65 3b 20 20 20   usableSpace;   
167d1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
167d2 65 73 20 69 6e 20 70 50 61 67 65 20 62 65 79 6f  es in pPage beyo
167d3 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f  nd the header */
167d4 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61 67 73  .  int pageFlags
167d5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
167d6 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50 61 67  /* Value of pPag
167d7 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20  e->aData[0] */. 
167d8 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b 20 20   int subtotal;  
167d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
167da 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62 79 74   Subtotal of byt
167db 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f  es in cells on o
167dc 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ne page */.  int
167dd 20 69 53 70 61 63 65 31 20 3d 20 30 3b 20 20 20   iSpace1 = 0;   
167de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
167df 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f  st unused byte o
167e0 66 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20  f aSpace1[] */. 
167e1 20 69 6e 74 20 69 53 70 61 63 65 32 20 3d 20 30   int iSpace2 = 0
167e2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
167e3 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 62 79   First unused by
167e4 74 65 20 6f 66 20 61 53 70 61 63 65 32 5b 5d 20  te of aSpace2[] 
167e5 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74  */.  int szScrat
167e6 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ch;             
167e7 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 72    /* Size of scr
167e8 61 74 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 75  atch memory requ
167e9 65 73 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61  ested */.  MemPa
167ea 67 65 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20  ge *apOld[NB];  
167eb 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65          /* pPage
167ec 20 61 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 73   and up to two s
167ed 69 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 50 67 6e  iblings */.  Pgn
167ee 6f 20 70 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b 20 20  o pgnoOld[NB];  
167ef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
167f0 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61  e numbers for ea
167f1 63 68 20 70 61 67 65 20 69 6e 20 61 70 4f 6c 64  ch page in apOld
167f2 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  [] */.  MemPage 
167f3 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20  *apCopy[NB];    
167f4 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20       /* Private 
167f5 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b  copies of apOld[
167f6 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d  ] pages */.  Mem
167f7 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32  Page *apNew[NB+2
167f8 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61  ];        /* pPa
167f9 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20  ge and up to NB 
167fa 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62  siblings after b
167fb 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 50 67  alancing */.  Pg
167fc 6e 6f 20 70 67 6e 6f 4e 65 77 5b 4e 42 2b 32 5d  no pgnoNew[NB+2]
167fd 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ;          /* Pa
167fe 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65  ge numbers for e
167ff 61 63 68 20 70 61 67 65 20 69 6e 20 61 70 4e 65  ach page in apNe
16800 77 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44  w[] */.  u8 *apD
16801 69 76 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20  iv[NB];         
16802 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72        /* Divider
16803 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e   cells in pParen
16804 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65  t */.  int cntNe
16805 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20  w[NB+2];        
16806 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
16807 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20  aCell[] of cell 
16808 61 66 74 65 72 20 69 2d 74 68 20 70 61 67 65 20  after i-th page 
16809 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e  */.  int szNew[N
1680a 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20  B+2];           
1680b 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69    /* Combined si
1680c 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63  ze of cells plac
1680d 65 20 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a  e on i-th page *
1680e 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20  /.  u8 **apCell 
1680f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
16810 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65   /* All cells be
16811 67 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a  gin balanced */.
16812 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20    u16 *szCell;  
16813 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16814 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20  * Local size of 
16815 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43  all cells in apC
16816 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61  ell[] */.  u8 *a
16817 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20  Copy[NB];       
16818 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 68    /* Space for h
16819 6f 6c 64 69 6e 67 20 64 61 74 61 20 6f 66 20 61  olding data of a
1681a 70 43 6f 70 79 5b 5d 20 2a 2f 0a 20 20 75 38 20  pCopy[] */.  u8 
1681b 2a 61 53 70 61 63 65 31 3b 20 20 20 20 20 20 20  *aSpace1;       
1681c 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72      /* Space for
1681d 20 63 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64   copies of divid
1681e 65 72 73 20 63 65 6c 6c 73 20 62 65 66 6f 72 65  ers cells before
1681f 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38   balance */.  u8
16820 20 2a 61 53 70 61 63 65 32 20 3d 20 30 3b 20 20   *aSpace2 = 0;  
16821 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f       /* Space fo
16822 72 20 6f 76 65 72 66 6c 6f 77 20 64 69 76 69 64  r overflow divid
16823 65 72 73 20 63 65 6c 6c 73 20 61 66 74 65 72 20  ers cells after 
16824 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20  balance */.  u8 
16825 2a 61 46 72 6f 6d 20 3d 20 30 3b 0a 0a 20 20 70  *aFrom = 0;..  p
16826 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
16827 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
16828 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
16829 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1682a 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
1682b 29 20 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28  ) );.  VVA_ONLY(
1682c 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68 75 66   pCur->pagesShuf
1682d 66 6c 65 64 20 3d 20 31 20 29 3b 0a 0a 20 20 2f  fled = 1 );..  /
1682e 2a 20 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65  * .  ** Find the
1682f 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20   parent page..  
16830 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  */.  assert( pCu
16831 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a 20 20  r->iPage>0 );.  
16832 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
16833 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72  sInit );.  asser
16834 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
16835 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
16836 2d 3e 70 44 62 50 61 67 65 29 20 7c 7c 20 70 50  ->pDbPage) || pP
16837 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d  age->nOverflow==
16838 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61  1 );.  pBt = pPa
16839 67 65 2d 3e 70 42 74 3b 0a 20 20 70 50 61 72 65  ge->pBt;.  pPare
1683a 6e 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  nt = pCur->apPag
1683b 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d  e[pCur->iPage-1]
1683c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
1683d 65 6e 74 20 29 3b 0a 20 20 69 66 28 20 53 51 4c  ent );.  if( SQL
1683e 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
1683f 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
16840 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65  pParent->pDbPage
16841 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62  )) ){.    goto b
16842 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
16843 20 20 7d 0a 0a 20 20 54 52 41 43 45 28 28 22 42    }..  TRACE(("B
16844 41 4c 41 4e 43 45 3a 20 62 65 67 69 6e 20 70 61  ALANCE: begin pa
16845 67 65 20 25 64 20 63 68 69 6c 64 20 6f 66 20 25  ge %d child of %
16846 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e  d\n", pPage->pgn
16847 6f 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f  o, pParent->pgno
16848 29 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ));..#ifndef SQL
16849 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41  ITE_OMIT_QUICKBA
1684a 4c 41 4e 43 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20  LANCE.  /*.  ** 
1684b 41 20 73 70 65 63 69 61 6c 20 63 61 73 65 3a 20  A special case: 
1684c 20 49 66 20 61 20 6e 65 77 20 65 6e 74 72 79 20   If a new entry 
1684d 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 69 6e  has just been in
1684e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 0a 20 20  serted into a.  
1684f 2a 2a 20 74 61 62 6c 65 20 28 74 68 61 74 20 69  ** table (that i
16850 73 2c 20 61 20 62 74 72 65 65 20 77 69 74 68 20  s, a btree with 
16851 69 6e 74 65 67 65 72 20 6b 65 79 73 20 61 6e 64  integer keys and
16852 20 61 6c 6c 20 64 61 74 61 20 61 74 20 74 68 65   all data at the
16853 20 6c 65 61 76 65 73 29 0a 20 20 2a 2a 20 61 6e   leaves).  ** an
16854 64 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20  d the new entry 
16855 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  is the right-mos
16856 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74  t entry in the t
16857 72 65 65 20 28 69 74 20 68 61 73 20 74 68 65 0a  ree (it has the.
16858 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b 65 79    ** largest key
16859 29 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 73  ) then use the s
1685a 70 65 63 69 61 6c 20 62 61 6c 61 6e 63 65 5f 71  pecial balance_q
1685b 75 69 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 66  uick() routine f
1685c 6f 72 0a 20 20 2a 2a 20 62 61 6c 61 6e 63 69 6e  or.  ** balancin
1685d 67 2e 20 20 62 61 6c 61 6e 63 65 5f 71 75 69 63  g.  balance_quic
1685e 6b 28 29 20 69 73 20 6d 75 63 68 20 66 61 73 74  k() is much fast
1685f 65 72 20 61 6e 64 20 72 65 73 75 6c 74 73 20 69  er and results i
16860 6e 20 61 20 74 69 67 68 74 65 72 0a 20 20 2a 2a  n a tighter.  **
16861 20 70 61 63 6b 69 6e 67 20 6f 66 20 64 61 74 61   packing of data
16862 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63   in the common c
16863 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ase..  */.  if( 
16864 70 50 61 67 65 2d 3e 6c 65 61 66 20 26 26 0a 20  pPage->leaf &&. 
16865 20 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b       pPage->intK
16866 65 79 20 26 26 0a 20 20 20 20 20 20 70 50 61 67  ey &&.      pPag
16867 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20  e->nOverflow==1 
16868 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e  &&.      pPage->
16869 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50  aOvfl[0].idx==pP
1686a 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 0a 20 20  age->nCell &&.  
1686b 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e      pParent->pgn
1686c 6f 21 3d 31 20 26 26 0a 20 20 20 20 20 20 67 65  o!=1 &&.      ge
1686d 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d  t4byte(&pParent-
1686e 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e  >aData[pParent->
1686f 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 70  hdrOffset+8])==p
16870 50 61 67 65 2d 3e 70 67 6e 6f 0a 20 20 29 7b 0a  Page->pgno.  ){.
16871 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
16872 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20  e->intKey );.   
16873 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 4f 44 4f 3a   /*.    ** TODO:
16874 20 43 68 65 63 6b 20 74 68 65 20 73 69 62 6c 69   Check the sibli
16875 6e 67 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20  ngs to the left 
16876 6f 66 20 70 50 61 67 65 2e 20 49 74 20 6d 61 79  of pPage. It may
16877 20 62 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20   be that.    ** 
16878 74 68 65 79 20 61 72 65 20 6e 6f 74 20 66 75 6c  they are not ful
16879 6c 20 61 6e 64 20 6e 6f 20 6e 65 77 20 70 61 67  l and no new pag
1687a 65 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20  e is required.. 
1687b 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e     */.    return
1687c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70   balance_quick(p
1687d 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Cur);.  }.#endif
1687e 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
1687f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K!=(rc = sqlite3
16880 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
16881 2d 3e 70 44 62 50 61 67 65 29 29 20 29 7b 0a 20  ->pDbPage)) ){. 
16882 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f     goto balance_
16883 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
16884 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65  /*.  ** Find the
16885 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72   cell in the par
16886 65 6e 74 20 70 61 67 65 20 77 68 6f 73 65 20 6c  ent page whose l
16887 65 66 74 20 63 68 69 6c 64 20 70 6f 69 6e 74 73  eft child points
16888 20 62 61 63 6b 0a 20 20 2a 2a 20 74 6f 20 70 50   back.  ** to pP
16889 61 67 65 2e 20 20 54 68 65 20 22 69 64 78 22 20  age.  The "idx" 
1688a 76 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20  variable is the 
1688b 69 6e 64 65 78 20 6f 66 20 74 68 61 74 20 63 65  index of that ce
1688c 6c 6c 2e 20 20 49 66 20 70 50 61 67 65 0a 20 20  ll.  If pPage.  
1688d 2a 2a 20 69 73 20 74 68 65 20 72 69 67 68 74 6d  ** is the rightm
1688e 6f 73 74 20 63 68 69 6c 64 20 6f 66 20 70 50 61  ost child of pPa
1688f 72 65 6e 74 20 74 68 65 6e 20 73 65 74 20 69 64  rent then set id
16890 78 20 74 6f 20 70 50 61 72 65 6e 74 2d 3e 6e 43  x to pParent->nC
16891 65 6c 6c 20 0a 20 20 2a 2f 0a 20 20 69 64 78 20  ell .  */.  idx 
16892 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  = pCur->aiIdx[pC
16893 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20  ur->iPage-1];.  
16894 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65  assertParentInde
16895 78 28 70 50 61 72 65 6e 74 2c 20 69 64 78 2c 20  x(pParent, idx, 
16896 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 0a 20  pPage->pgno);.. 
16897 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 73 69   /*.  ** Find si
16898 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 70  bling pages to p
16899 50 61 67 65 20 61 6e 64 20 74 68 65 20 63 65 6c  Page and the cel
1689a 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 74 68  ls in pParent th
1689b 61 74 20 64 69 76 69 64 65 0a 20 20 2a 2a 20 74  at divide.  ** t
1689c 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 20 41 6e  he siblings.  An
1689d 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
1689e 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c   to find NN sibl
1689f 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 0a 20  ings on either. 
168a0 20 2a 2a 20 73 69 64 65 20 6f 66 20 70 50 61 67   ** side of pPag
168a1 65 2e 20 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67  e.  More sibling
168a2 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d  s are taken from
168a3 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76   one side, howev
168a4 65 72 2c 20 69 66 0a 20 20 2a 2a 20 70 50 61 67  er, if.  ** pPag
168a5 65 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65  e there are fewe
168a6 72 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e  r than NN siblin
168a7 67 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20  gs on the other 
168a8 73 69 64 65 2e 20 20 49 66 20 70 50 61 72 65 6e  side.  If pParen
168a9 74 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72  t.  ** has NB or
168aa 20 66 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20   fewer children 
168ab 74 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65  then all childre
168ac 6e 20 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65  n of pParent are
168ad 20 74 61 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 6e   taken..  */.  n
168ae 78 44 69 76 20 3d 20 69 64 78 20 2d 20 4e 4e 3b  xDiv = idx - NN;
168af 0a 20 20 69 66 28 20 6e 78 44 69 76 20 2b 20 4e  .  if( nxDiv + N
168b0 42 20 3e 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65  B > pParent->nCe
168b1 6c 6c 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 20  ll ){.    nxDiv 
168b2 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  = pParent->nCell
168b3 20 2d 20 4e 42 20 2b 20 31 3b 0a 20 20 7d 0a 20   - NB + 1;.  }. 
168b4 20 69 66 28 20 6e 78 44 69 76 3c 30 20 29 7b 0a   if( nxDiv<0 ){.
168b5 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20      nxDiv = 0;. 
168b6 20 7d 0a 20 20 6e 44 69 76 20 3d 20 30 3b 0a 20   }.  nDiv = 0;. 
168b7 20 66 6f 72 28 69 3d 30 2c 20 6b 3d 6e 78 44 69   for(i=0, k=nxDi
168b8 76 3b 20 69 3c 4e 42 3b 20 69 2b 2b 2c 20 6b 2b  v; i<NB; i++, k+
168b9 2b 29 7b 0a 20 20 20 20 69 66 28 20 6b 3c 70 50  +){.    if( k<pP
168ba 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a  arent->nCell ){.
168bb 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d        apDiv[i] =
168bc 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e   findCell(pParen
168bd 74 2c 20 6b 29 3b 0a 20 20 20 20 20 20 6e 44 69  t, k);.      nDi
168be 76 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72  v++;.      asser
168bf 74 28 20 21 70 50 61 72 65 6e 74 2d 3e 6c 65 61  t( !pParent->lea
168c0 66 20 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4f  f );.      pgnoO
168c1 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62 79 74 65  ld[i] = get4byte
168c2 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20  (apDiv[i]);.    
168c3 7d 65 6c 73 65 20 69 66 28 20 6b 3d 3d 70 50 61  }else if( k==pPa
168c4 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  rent->nCell ){. 
168c5 20 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20       pgnoOld[i] 
168c6 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72  = get4byte(&pPar
168c7 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65  ent->aData[pPare
168c8 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  nt->hdrOffset+8]
168c9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
168ca 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
168cb 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64  .    rc = getAnd
168cc 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 67  InitPage(pBt, pg
168cd 6e 6f 4f 6c 64 5b 69 5d 2c 20 26 61 70 4f 6c 64  noOld[i], &apOld
168ce 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63  [i]);.    if( rc
168cf 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f   ) goto balance_
168d0 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 2f 2a 20  cleanup;.    /* 
168d1 61 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72  apOld[i]->idxPar
168d2 65 6e 74 20 3d 20 6b 3b 20 2a 2f 0a 20 20 20 20  ent = k; */.    
168d3 61 70 43 6f 70 79 5b 69 5d 20 3d 20 30 3b 0a 20  apCopy[i] = 0;. 
168d4 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 4f     assert( i==nO
168d5 6c 64 20 29 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b  ld );.    nOld++
168d6 3b 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20  ;.    nMaxCells 
168d7 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e  += 1+apOld[i]->n
168d8 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e  Cell+apOld[i]->n
168d9 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20  Overflow;.  }.. 
168da 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c   /* Make nMaxCel
168db 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66  ls a multiple of
168dc 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70   4 in order to p
168dd 72 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20  reserve 8-byte. 
168de 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f   ** alignment */
168df 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28  .  nMaxCells = (
168e0 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e  nMaxCells + 3)&~
168e1 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c  3;..  /*.  ** Al
168e2 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
168e3 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72   memory structur
168e4 65 73 0a 20 20 2a 2f 0a 20 20 73 7a 53 63 72 61  es.  */.  szScra
168e5 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61  tch =.       nMa
168e6 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38  xCells*sizeof(u8
168e7 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *)              
168e8 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65           /* apCe
168e9 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61  ll */.     + nMa
168ea 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31  xCells*sizeof(u1
168eb 36 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  6)              
168ec 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65           /* szCe
168ed 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 28 52 4f  ll */.     + (RO
168ee 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50  UND8(sizeof(MemP
168ef 61 67 65 29 29 2b 70 42 74 2d 3e 70 61 67 65 53  age))+pBt->pageS
168f0 69 7a 65 29 2a 4e 42 20 20 2f 2a 20 61 43 6f 70  ize)*NB  /* aCop
168f1 79 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d  y */.     + pBt-
168f2 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20  >pageSize       
168f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
168f4 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63          /* aSpac
168f5 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 28 49 53  e1 */.     + (IS
168f6 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 6e 4d 61  AUTOVACUUM ? nMa
168f7 78 43 65 6c 6c 73 20 3a 20 30 29 3b 20 20 20 20  xCells : 0);    
168f8 20 20 20 20 20 20 20 20 20 2f 2a 20 61 46 72 6f           /* aFro
168f9 6d 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20  m */.  apCell = 
168fa 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61  sqlite3ScratchMa
168fb 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20  lloc( szScratch 
168fc 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c  ); .  if( apCell
168fd 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
168fe 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
168ff 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63    goto balance_c
16900 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a  leanup;.  }.  sz
16901 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70  Cell = (u16*)&ap
16902 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b  Cell[nMaxCells];
16903 0a 20 20 61 43 6f 70 79 5b 30 5d 20 3d 20 28 75  .  aCopy[0] = (u
16904 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43  8*)&szCell[nMaxC
16905 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28  ells];.  assert(
16906 20 28 28 61 43 6f 70 79 5b 30 5d 20 2d 20 28 75   ((aCopy[0] - (u
16907 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29 3b  8*)0) & 7)==0 );
16908 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 6e   /* 8-byte align
16909 6d 65 6e 74 20 72 65 71 75 69 72 65 64 20 2a 2f  ment required */
1690a 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e 42  .  for(i=1; i<NB
1690b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 43 6f 70  ; i++){.    aCop
1690c 79 5b 69 5d 20 3d 20 26 61 43 6f 70 79 5b 69 2d  y[i] = &aCopy[i-
1690d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  1][pBt->pageSize
1690e 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d  +ROUND8(sizeof(M
1690f 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 20 20 61  emPage))];.    a
16910 73 73 65 72 74 28 20 28 28 61 43 6f 70 79 5b 69  ssert( ((aCopy[i
16911 5d 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29  ] - (u8*)0) & 7)
16912 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65  ==0 ); /* 8-byte
16913 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69   alignment requi
16914 72 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 53 70  red */.  }.  aSp
16915 61 63 65 31 20 3d 20 26 61 43 6f 70 79 5b 4e 42  ace1 = &aCopy[NB
16916 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a  -1][pBt->pageSiz
16917 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  e+ROUND8(sizeof(
16918 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 61 73  MemPage))];.  as
16919 73 65 72 74 28 20 28 28 61 53 70 61 63 65 31 20  sert( ((aSpace1 
1691a 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d 3d  - (u8*)0) & 7)==
1691b 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61  0 ); /* 8-byte a
1691c 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65  lignment require
1691d 64 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55 54  d */.  if( ISAUT
1691e 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 61  OVACUUM ){.    a
1691f 46 72 6f 6d 20 3d 20 26 61 53 70 61 63 65 31 5b  From = &aSpace1[
16920 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 3b 0a  pBt->pageSize];.
16921 20 20 7d 0a 20 20 61 53 70 61 63 65 32 20 3d 20    }.  aSpace2 = 
16922 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f  sqlite3PageMallo
16923 63 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29  c(pBt->pageSize)
16924 3b 0a 20 20 69 66 28 20 61 53 70 61 63 65 32 3d  ;.  if( aSpace2=
16925 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
16926 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
16927 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
16928 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 0a 20 20  eanup;.  }.  .  
16929 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 63 6f 70  /*.  ** Make cop
1692a 69 65 73 20 6f 66 20 74 68 65 20 63 6f 6e 74 65  ies of the conte
1692b 6e 74 20 6f 66 20 70 50 61 67 65 20 61 6e 64 20  nt of pPage and 
1692c 69 74 73 20 73 69 62 6c 69 6e 67 73 20 69 6e 74  its siblings int
1692d 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a 2a 20 54  o aOld[]..  ** T
1692e 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73 20  he rest of this 
1692f 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75 73  function will us
16930 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20  e data from the 
16931 63 6f 70 69 65 73 20 72 61 74 68 65 72 0a 20 20  copies rather.  
16932 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 72 69 67  ** that the orig
16933 69 6e 61 6c 20 70 61 67 65 73 20 73 69 6e 63 65  inal pages since
16934 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61   the original pa
16935 67 65 73 20 77 69 6c 6c 20 62 65 20 69 6e 20 74  ges will be in t
16936 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 20  he.  ** process 
16937 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69  of being overwri
16938 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  tten..  */.  for
16939 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b  (i=0; i<nOld; i+
1693a 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20  +){.    MemPage 
1693b 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20 3d  *p = apCopy[i] =
1693c 20 28 4d 65 6d 50 61 67 65 2a 29 61 43 6f 70 79   (MemPage*)aCopy
1693d 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  [i];.    memcpy(
1693e 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a  p, apOld[i], siz
1693f 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20  eof(MemPage));. 
16940 20 20 20 70 2d 3e 61 44 61 74 61 20 3d 20 28 76     p->aData = (v
16941 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20  oid*)&p[1];.    
16942 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61 74 61 2c  memcpy(p->aData,
16943 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74 61   apOld[i]->aData
16944 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29  , pBt->pageSize)
16945 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ;.  }..  /*.  **
16946 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20 74   Load pointers t
16947 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 73  o all cells on s
16948 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e 64  ibling pages and
16949 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
1694a 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65  ls.  ** into the
1694b 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d 20   local apCell[] 
1694c 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f 70  array.  Make cop
1694d 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 64  ies of the divid
1694e 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e  er cells.  ** in
1694f 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  to space obtaine
16950 64 20 66 6f 72 6d 20 61 53 70 61 63 65 31 5b 5d  d form aSpace1[]
16951 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20   and remove the 
16952 74 68 65 20 64 69 76 69 64 65 72 20 43 65 6c 6c  the divider Cell
16953 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72  s.  ** from pPar
16954 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ent..  **.  ** I
16955 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61  f the siblings a
16956 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 73  re on leaf pages
16957 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64  , then the child
16958 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 65   pointers of the
16959 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 65  .  ** divider ce
1695a 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 64  lls are stripped
1695b 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20   from the cells 
1695c 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20  before they are 
1695d 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f  copied.  ** into
1695e 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e 20   aSpace1[].  In 
1695f 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 65  this way, all ce
16960 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20  lls in apCell[] 
16961 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a  are without.  **
16962 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e   child pointers.
16963 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 72    If siblings ar
16964 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68  e not leaves, th
16965 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20  en all cell in. 
16966 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63   ** apCell[] inc
16967 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e 74  lude child point
16968 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 79  ers.  Either way
16969 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61  , all cells in a
1696a 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65  pCell[].  ** are
1696b 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a   alike..  **.  *
1696c 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  * leafCorrection
1696d 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 69 73  :  4 if pPage is
1696e 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 70   a leaf.  0 if p
1696f 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c 65  Page is not a le
16970 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c  af..  **       l
16971 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 70  eafData:  1 if p
16972 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64  Page holds key+d
16973 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 20  ata and pParent 
16974 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e  holds only keys.
16975 0a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 3d 20  .  */.  nCell = 
16976 30 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74  0;.  leafCorrect
16977 69 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61  ion = pPage->lea
16978 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20  f*4;.  leafData 
16979 3d 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61  = pPage->hasData
1697a 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
1697b 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d  Old; i++){.    M
1697c 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61  emPage *pOld = a
1697d 70 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 69 6e  pCopy[i];.    in
1697e 74 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e  t limit = pOld->
1697f 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65  nCell+pOld->nOve
16980 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28 6a  rflow;.    for(j
16981 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b  =0; j<limit; j++
16982 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
16983 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73   nCell<nMaxCells
16984 20 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c 6c   );.      apCell
16985 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76  [nCell] = findOv
16986 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c  erflowCell(pOld,
16987 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c   j);.      szCel
16988 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53  l[nCell] = cellS
16989 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 43  izePtr(pOld, apC
1698a 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 20 20  ell[nCell]);.   
1698b 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
1698c 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 69  UUM ){.        i
1698d 6e 74 20 61 3b 0a 20 20 20 20 20 20 20 20 61 46  nt a;.        aF
1698e 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 28 75 38  rom[nCell] = (u8
1698f 29 69 3b 20 20 20 61 73 73 65 72 74 28 20 69 3e  )i;   assert( i>
16990 3d 30 20 26 26 20 69 3c 36 20 29 3b 0a 20 20 20  =0 && i<6 );.   
16991 20 20 20 20 20 66 6f 72 28 61 3d 30 3b 20 61 3c       for(a=0; a<
16992 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b  pOld->nOverflow;
16993 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   a++){.         
16994 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c   if( pOld->aOvfl
16995 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70 43 65 6c  [a].pCell==apCel
16996 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20 20 20 20  l[nCell] ){.    
16997 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43          aFrom[nC
16998 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20  ell] = 0xFF;.   
16999 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
1699a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1699b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1699c 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20      nCell++;.   
1699d 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c   }.    if( i<nOl
1699e 64 2d 31 20 29 7b 0a 20 20 20 20 20 20 75 31 36  d-1 ){.      u16
1699f 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74   sz = cellSizePt
169a0 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76  r(pParent, apDiv
169a1 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [i]);.      if( 
169a2 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20  leafData ){.    
169a3 20 20 20 20 2f 2a 20 57 69 74 68 20 74 68 65 20      /* With the 
169a4 4c 45 41 46 44 41 54 41 20 66 6c 61 67 2c 20 70  LEAFDATA flag, p
169a5 50 61 72 65 6e 74 20 63 65 6c 6c 73 20 68 6f 6c  Parent cells hol
169a6 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59 73 20 74  d only INTKEYs t
169a7 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  hat.        ** a
169a8 72 65 20 64 75 70 6c 69 63 61 74 65 73 20 6f 66  re duplicates of
169a9 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 63 68 69   keys on the chi
169aa 6c 64 20 70 61 67 65 73 2e 20 20 57 65 20 6e 65  ld pages.  We ne
169ab 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a 20 20 20  ed to remove.   
169ac 20 20 20 20 20 2a 2a 20 74 68 65 20 64 69 76 69       ** the divi
169ad 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70  der cells from p
169ae 50 61 72 65 6e 74 2c 20 62 75 74 20 74 68 65 20  Parent, but the 
169af 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 61  dividers cells a
169b0 72 65 20 6e 6f 74 0a 20 20 20 20 20 20 20 20 2a  re not.        *
169b1 2a 20 61 64 64 65 64 20 74 6f 20 61 70 43 65 6c  * added to apCel
169b2 6c 5b 5d 20 62 65 63 61 75 73 65 20 74 68 65 79  l[] because they
169b3 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 20   are duplicates 
169b4 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c 73 2e 0a  of child cells..
169b5 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
169b6 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 72     dropCell(pPar
169b7 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a 29 3b  ent, nxDiv, sz);
169b8 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
169b9 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d 70 3b        u8 *pTemp;
169ba 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
169bb 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73   nCell<nMaxCells
169bc 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65   );.        szCe
169bd 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 3b 0a  ll[nCell] = sz;.
169be 20 20 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20          pTemp = 
169bf 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 65 31  &aSpace1[iSpace1
169c0 5d 3b 0a 20 20 20 20 20 20 20 20 69 53 70 61 63  ];.        iSpac
169c1 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20  e1 += sz;.      
169c2 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42    assert( sz<=pB
169c3 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b  t->pageSize/4 );
169c4 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
169c5 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70   iSpace1<=pBt->p
169c6 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20  ageSize );.     
169c7 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c     memcpy(pTemp,
169c8 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a   apDiv[i], sz);.
169c9 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e          apCell[n
169ca 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b 6c 65  Cell] = pTemp+le
169cb 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20  afCorrection;.  
169cc 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f        if( ISAUTO
169cd 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20  VACUUM ){.      
169ce 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d      aFrom[nCell]
169cf 20 3d 20 30 78 46 46 3b 0a 20 20 20 20 20 20 20   = 0xFF;.       
169d0 20 7d 0a 20 20 20 20 20 20 20 20 64 72 6f 70 43   }.        dropC
169d1 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44  ell(pParent, nxD
169d2 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 20  iv, sz);.       
169d3 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72   assert( leafCor
169d4 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65  rection==0 || le
169d5 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20  afCorrection==4 
169d6 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65 6c  );.        szCel
169d7 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 28 75 31 36  l[nCell] -= (u16
169d8 29 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b  )leafCorrection;
169d9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
169da 20 67 65 74 34 62 79 74 65 28 70 54 65 6d 70 29   get4byte(pTemp)
169db 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20 29 3b 0a  ==pgnoOld[i] );.
169dc 20 20 20 20 20 20 20 20 69 66 28 20 21 70 4f 6c          if( !pOl
169dd 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  d->leaf ){.     
169de 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61       assert( lea
169df 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 29  fCorrection==0 )
169e0 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;.          /* T
169e1 68 65 20 72 69 67 68 74 20 70 6f 69 6e 74 65 72  he right pointer
169e2 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 61   of the child pa
169e3 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65 73 20  ge pOld becomes 
169e4 74 68 65 20 6c 65 66 74 0a 20 20 20 20 20 20 20  the left.       
169e5 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66     ** pointer of
169e6 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
169e7 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d  l */.          m
169e8 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43 65  emcpy(apCell[nCe
169e9 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61 74  ll], &pOld->aDat
169ea 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73 65  a[pOld->hdrOffse
169eb 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20  t+8], 4);.      
169ec 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
169ed 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43     assert( leafC
169ee 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a  orrection==4 );.
169ef 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 7a            if( sz
169f0 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b  Cell[nCell]<4 ){
169f1 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
169f2 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e 79  Do not allow any
169f3 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 20 74   cells smaller t
169f4 68 61 6e 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a  han 4 bytes. */.
169f5 20 20 20 20 20 20 20 20 20 20 20 20 73 7a 43 65              szCe
169f6 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b 0a 20  ll[nCell] = 4;. 
169f7 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
169f8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 43 65     }.        nCe
169f9 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ll++;.      }.  
169fa 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20    }.  }..  /*.  
169fb 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68  ** Figure out th
169fc 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
169fd 73 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64  s needed to hold
169fe 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c 6c 73   all nCell cells
169ff 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20 74 68 69  ..  ** Store thi
16a00 73 20 6e 75 6d 62 65 72 20 69 6e 20 22 6b 22 2e  s number in "k".
16a01 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74 65 20 73    Also compute s
16a02 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20  zNew[] which is 
16a03 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a 2a 20 73  the total.  ** s
16a04 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73  ize of all cells
16a05 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 70 61 67   on the i-th pag
16a06 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20 77  e and cntNew[] w
16a07 68 69 63 68 20 69 73 20 74 68 65 20 69 6e 64 65  hich is the inde
16a08 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c 6c  x.  ** in apCell
16a09 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74  [] of the cell t
16a0a 68 61 74 20 64 69 76 69 64 65 73 20 70 61 67 65  hat divides page
16a0b 20 69 20 66 72 6f 6d 20 70 61 67 65 20 69 2b 31   i from page i+1
16a0c 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b  .  .  ** cntNew[
16a0d 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 61 6c 20  k] should equal 
16a0e 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  nCell..  **.  **
16a0f 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 74 65 64   Values computed
16a10 20 62 79 20 74 68 69 73 20 62 6c 6f 63 6b 3a 0a   by this block:.
16a11 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
16a12 20 20 20 20 6b 3a 20 54 68 65 20 74 6f 74 61 6c      k: The total
16a13 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69   number of sibli
16a14 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a 20 20 20  ng pages.  **   
16a15 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 63 65   szNew[i]: Space
16a16 64 20 75 73 65 64 20 6f 6e 20 74 68 65 20 69 2d  d used on the i-
16a17 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e  th sibling page.
16a18 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65 77 5b 69  .  **   cntNew[i
16a19 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61 70 43 65  ]: Index in apCe
16a1a 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 6c 6c 5b  ll[] and szCell[
16a1b 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  ] for the first 
16a1c 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 20 20  cell to.  **    
16a1d 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72 69            the ri
16a1e 67 68 74 20 6f 66 20 74 68 65 20 69 2d 74 68 20  ght of the i-th 
16a1f 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20  sibling page..  
16a20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63 65 3a 20  ** usableSpace: 
16a21 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
16a22 6f 66 20 73 70 61 63 65 20 61 76 61 69 6c 61 62  of space availab
16a23 6c 65 20 6f 6e 20 65 61 63 68 20 73 69 62 6c 69  le on each sibli
16a24 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20  ng..  ** .  */. 
16a25 20 75 73 61 62 6c 65 53 70 61 63 65 20 3d 20 70   usableSpace = p
16a26 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
16a27 20 31 32 20 2b 20 6c 65 61 66 43 6f 72 72 65 63   12 + leafCorrec
16a28 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 62 74  tion;.  for(subt
16a29 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43  otal=k=i=0; i<nC
16a2a 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61  ell; i++){.    a
16a2b 73 73 65 72 74 28 20 69 3c 6e 4d 61 78 43 65 6c  ssert( i<nMaxCel
16a2c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62 74 6f 74  ls );.    subtot
16a2d 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20  al += szCell[i] 
16a2e 2b 20 32 3b 0a 20 20 20 20 69 66 28 20 73 75 62  + 2;.    if( sub
16a2f 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c 65 53 70  total > usableSp
16a30 61 63 65 20 29 7b 0a 20 20 20 20 20 20 73 7a 4e  ace ){.      szN
16a31 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c  ew[k] = subtotal
16a32 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20   - szCell[i];.  
16a33 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20      cntNew[k] = 
16a34 69 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61  i;.      if( lea
16a35 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a  fData ){ i--; }.
16a36 20 20 20 20 20 20 73 75 62 74 6f 74 61 6c 20 3d        subtotal =
16a37 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b 0a 20   0;.      k++;. 
16a38 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65 77     }.  }.  szNew
16a39 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 3b 0a  [k] = subtotal;.
16a3a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43    cntNew[k] = nC
16a3b 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f  ell;.  k++;..  /
16a3c 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 63 6b 69  *.  ** The packi
16a3d 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74  ng computed by t
16a3e 68 65 20 70 72 65 76 69 6f 75 73 20 62 6c 6f 63  he previous bloc
16a3f 6b 20 69 73 20 62 69 61 73 65 64 20 74 6f 77 61  k is biased towa
16a40 72 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73 0a  rd the siblings.
16a41 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 66 74    ** on the left
16a42 20 73 69 64 65 2e 20 20 54 68 65 20 6c 65 66 74   side.  The left
16a43 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 61 6c   siblings are al
16a44 77 61 79 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c  ways nearly full
16a45 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 20 2a 2a  , while the.  **
16a46 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c   right-most sibl
16a47 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6e 65 61  ing might be nea
16a48 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73  rly empty.  This
16a49 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 61   block of code a
16a4a 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 74 6f 20  ttempts.  ** to 
16a4b 61 64 6a 75 73 74 20 74 68 65 20 70 61 63 6b 69  adjust the packi
16a4c 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 74  ng of siblings t
16a4d 6f 20 67 65 74 20 61 20 62 65 74 74 65 72 20 62  o get a better b
16a4e 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a  alance..  **.  *
16a4f 2a 20 54 68 69 73 20 61 64 6a 75 73 74 6d 65 6e  * This adjustmen
16a50 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 61  t is more than a
16a51 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  n optimization. 
16a52 20 54 68 65 20 70 61 63 6b 69 6e 67 20 61 62 6f   The packing abo
16a53 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65  ve might.  ** be
16a54 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 6c 61 6e   so out of balan
16a55 63 65 20 61 73 20 74 6f 20 62 65 20 69 6c 6c 65  ce as to be ille
16a56 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  gal.  For exampl
16a57 65 2c 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  e, the right-mos
16a58 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 6d  t.  ** sibling m
16a59 69 67 68 74 20 62 65 20 63 6f 6d 70 6c 65 74 65  ight be complete
16a5a 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73 20  ly empty.  This 
16a5b 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6e 6f  adjustment is no
16a5c 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f  t optional..  */
16a5d 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e  .  for(i=k-1; i>
16a5e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74  0; i--){.    int
16a5f 20 73 7a 52 69 67 68 74 20 3d 20 73 7a 4e 65 77   szRight = szNew
16a60 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 20 6f 66  [i];  /* Size of
16a61 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20   sibling on the 
16a62 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 69 6e 74  right */.    int
16a63 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 77 5b   szLeft = szNew[
16a64 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66  i-1]; /* Size of
16a65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20   sibling on the 
16a66 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  left */.    int 
16a67 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
16a68 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 69 67 68  /* Index of righ
16a69 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e 20 6c  t-most cell in l
16a6a 65 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 20  eft sibling */. 
16a6b 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20 20 20     int d;       
16a6c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
16a6d 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f  of first cell to
16a6e 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72 69 67   the left of rig
16a6f 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20  ht sibling */.. 
16a70 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d     r = cntNew[i-
16a71 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 20 3d 20  1] - 1;.    d = 
16a72 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 61  r + 1 - leafData
16a73 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 3c  ;.    assert( d<
16a74 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20  nMaxCells );.   
16a75 20 61 73 73 65 72 74 28 20 72 3c 6e 4d 61 78 43   assert( r<nMaxC
16a76 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 68 69 6c  ells );.    whil
16a77 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 20 7c 7c  e( szRight==0 ||
16a78 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 6c 6c 5b   szRight+szCell[
16a79 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a  d]+2<=szLeft-(sz
16a7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b 0a 20 20  Cell[r]+2) ){.  
16a7b 20 20 20 20 73 7a 52 69 67 68 74 20 2b 3d 20 73      szRight += s
16a7c 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20  zCell[d] + 2;.  
16a7d 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d 20 73 7a      szLeft -= sz
16a7e 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 20  Cell[r] + 2;.   
16a7f 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d     cntNew[i-1]--
16a80 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 6e 74 4e  ;.      r = cntN
16a81 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20  ew[i-1] - 1;.   
16a82 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c     d = r + 1 - l
16a83 65 61 66 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20  eafData;.    }. 
16a84 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a     szNew[i] = sz
16a85 52 69 67 68 74 3b 0a 20 20 20 20 73 7a 4e 65 77  Right;.    szNew
16a86 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a  [i-1] = szLeft;.
16a87 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72    }..  /* Either
16a88 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 20 6f 72   we found one or
16a89 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 63 6e 74   more cells (cnt
16a8a 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 20 77 65  new[0])>0) or we
16a8b 20 61 72 65 20 74 68 65 0a 20 20 2a 2a 20 61 20   are the.  ** a 
16a8c 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67  virtual root pag
16a8d 65 2e 20 20 41 20 76 69 72 74 75 61 6c 20 72 6f  e.  A virtual ro
16a8e 6f 74 20 70 61 67 65 20 69 73 20 77 68 65 6e 20  ot page is when 
16a8f 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 0a 20 20  the real root.  
16a90 2a 2a 20 70 61 67 65 20 69 73 20 70 61 67 65 20  ** page is page 
16a91 31 20 61 6e 64 20 77 65 20 61 72 65 20 74 68 65  1 and we are the
16a92 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f 66 20 74   only child of t
16a93 68 61 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20  hat page..  */. 
16a94 20 61 73 73 65 72 74 28 20 63 6e 74 4e 65 77 5b   assert( cntNew[
16a95 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 65 6e 74  0]>0 || (pParent
16a96 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 70 50 61  ->pgno==1 && pPa
16a97 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 29 20  rent->nCell==0) 
16a98 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c  );..  /*.  ** Al
16a99 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67  locate k new pag
16a9a 65 73 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70  es.  Reuse old p
16a9b 61 67 65 73 20 77 68 65 72 65 20 70 6f 73 73 69  ages where possi
16a9c 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ble..  */.  asse
16a9d 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3e  rt( pPage->pgno>
16a9e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c 61 67 73  1 );.  pageFlags
16a9f 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b   = pPage->aData[
16aa0 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  0];.  for(i=0; i
16aa1 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65  <k; i++){.    Me
16aa2 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 20  mPage *pNew;.   
16aa3 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20   if( i<nOld ){. 
16aa4 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e 65       pNew = apNe
16aa5 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b  w[i] = apOld[i];
16aa6 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69  .      pgnoNew[i
16aa7 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69 5d 3b 0a  ] = pgnoOld[i];.
16aa8 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d        apOld[i] =
16aa9 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   0;.      rc = s
16aaa 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
16aab 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 3b  (pNew->pDbPage);
16aac 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 20  .      nNew++;. 
16aad 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f       if( rc ) go
16aae 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
16aaf 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  up;.    }else{. 
16ab0 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 30       assert( i>0
16ab1 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   );.      rc = a
16ab2 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65  llocateBtreePage
16ab3 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 67  (pBt, &pNew, &pg
16ab4 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e 6f 4e 65  noNew[i], pgnoNe
16ab5 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20 20 20 20  w[i-1], 0);.    
16ab6 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
16ab7 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b  balance_cleanup;
16ab8 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20  .      apNew[i] 
16ab9 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 6e 4e  = pNew;.      nN
16aba 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ew++;.    }.  }.
16abb 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 6f  .  /* Free any o
16abc 6c 64 20 70 61 67 65 73 20 74 68 61 74 20 77 65  ld pages that we
16abd 72 65 20 6e 6f 74 20 72 65 75 73 65 64 20 61 73  re not reused as
16abe 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2f   new pages..  */
16abf 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4f 6c 64  .  while( i<nOld
16ac0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 72 65   ){.    rc = fre
16ac1 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b  ePage(apOld[i]);
16ac2 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
16ac3 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
16ac4 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50  up;.    releaseP
16ac5 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20  age(apOld[i]);. 
16ac6 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b     apOld[i] = 0;
16ac7 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20  .    i++;.  }.. 
16ac8 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 74 68 65   /*.  ** Put the
16ac9 20 6e 65 77 20 70 61 67 65 73 20 69 6e 20 61 63   new pages in ac
16aca 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 20 20  cending order.  
16acb 54 68 69 73 20 68 65 6c 70 73 20 74 6f 0a 20 20  This helps to.  
16acc 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 65 73 20  ** keep entries 
16acd 69 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65  in the disk file
16ace 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 74 68 61   in order so tha
16acf 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a 20 6f 66  t a scan.  ** of
16ad0 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20   the table is a 
16ad1 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f  linear scan thro
16ad2 75 67 68 20 74 68 65 20 66 69 6c 65 2e 20 20 54  ugh the file.  T
16ad3 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 75 72 6e  hat.  ** in turn
16ad4 20 68 65 6c 70 73 20 74 68 65 20 6f 70 65 72 61   helps the opera
16ad5 74 69 6e 67 20 73 79 73 74 65 6d 20 74 6f 20 64  ting system to d
16ad6 65 6c 69 76 65 72 20 70 61 67 65 73 0a 20 20 2a  eliver pages.  *
16ad7 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 20  * from the disk 
16ad8 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e 0a 20 20  more rapidly..  
16ad9 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 6e 5e 32  **.  ** An O(n^2
16ada 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74  ) insertion sort
16adb 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 75 73   algorithm is us
16adc 65 64 2c 20 62 75 74 20 73 69 6e 63 65 0a 20 20  ed, but since.  
16add 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 20 6d 6f  ** n is never mo
16ade 72 65 20 74 68 61 6e 20 4e 42 20 28 61 20 73 6d  re than NB (a sm
16adf 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 2c 20 74  all constant), t
16ae0 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 2a 2a 20  hat should.  ** 
16ae1 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d  not be a problem
16ae2 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e  ..  **.  ** When
16ae3 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f 6e 65   NB==3, this one
16ae4 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 61   optimization ma
16ae5 6b 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65  kes the database
16ae6 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 35 25 20  .  ** about 25% 
16ae7 66 61 73 74 65 72 20 66 6f 72 20 6c 61 72 67 65  faster for large
16ae8 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e 64 20   insertions and 
16ae9 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a  deletions..  */.
16aea 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 2d 31    for(i=0; i<k-1
16aeb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  ; i++){.    int 
16aec 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 5b 69  minV = pgnoNew[i
16aed 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49 20  ];.    int minI 
16aee 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69  = i;.    for(j=i
16aef 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20  +1; j<k; j++){. 
16af0 20 20 20 20 20 69 66 28 20 70 67 6e 6f 4e 65 77       if( pgnoNew
16af1 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64 29 6d 69  [j]<(unsigned)mi
16af2 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 69  nV ){.        mi
16af3 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20  nI = j;.        
16af4 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 5b 6a  minV = pgnoNew[j
16af5 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ];.      }.    }
16af6 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 3e 69 20  .    if( minI>i 
16af7 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 3b 0a  ){.      int t;.
16af8 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
16af9 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 70 67 6e  T;.      t = pgn
16afa 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 70  oNew[i];.      p
16afb 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20  T = apNew[i];.  
16afc 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 5d 20 3d      pgnoNew[i] =
16afd 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 3b 0a   pgnoNew[minI];.
16afe 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d        apNew[i] =
16aff 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20   apNew[minI];.  
16b00 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49      pgnoNew[minI
16b01 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20 61 70 4e  ] = t;.      apN
16b02 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20  ew[minI] = pT;. 
16b03 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45     }.  }.  TRACE
16b04 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c 64 3a  (("BALANCE: old:
16b05 20 25 64 20 25 64 20 25 64 20 20 6e 65 77 3a 20   %d %d %d  new: 
16b06 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64  %d(%d) %d(%d) %d
16b07 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28 25  (%d) %d(%d) %d(%
16b08 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67 6e 6f 4f  d)\n",.    pgnoO
16b09 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e 4f 6c 64  ld[0], .    nOld
16b0a 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64 5b 31 5d  >=2 ? pgnoOld[1]
16b0b 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 3e 3d   : 0,.    nOld>=
16b0c 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32 5d 20 3a  3 ? pgnoOld[2] :
16b0d 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e 65 77 5b   0,.    pgnoNew[
16b0e 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20  0], szNew[0],.  
16b0f 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70 67 6e 6f    nNew>=2 ? pgno
16b10 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e 4e 65 77  New[1] : 0, nNew
16b11 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 3a  >=2 ? szNew[1] :
16b12 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 20   0,.    nNew>=3 
16b13 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20 3a 20 30  ? pgnoNew[2] : 0
16b14 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a 4e 65  , nNew>=3 ? szNe
16b15 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e  w[2] : 0,.    nN
16b16 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e 65 77 5b  ew>=4 ? pgnoNew[
16b17 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 34 20  3] : 0, nNew>=4 
16b18 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 30 2c 0a  ? szNew[3] : 0,.
16b19 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 20 70 67      nNew>=5 ? pg
16b1a 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c 20 6e 4e  noNew[4] : 0, nN
16b1b 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d  ew>=5 ? szNew[4]
16b1c 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20   : 0));..  /*.  
16b1d 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72 69  ** Evenly distri
16b1e 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69 6e  bute the data in
16b1f 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73   apCell[] across
16b20 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e 0a   the new pages..
16b21 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69    ** Insert divi
16b22 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70  der cells into p
16b23 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73  Parent as necess
16b24 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20  ary..  */.  j = 
16b25 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
16b26 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nNew; i++){.    
16b27 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65 20  /* Assemble the 
16b28 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65  new sibling page
16b29 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65  . */.    MemPage
16b2a 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69   *pNew = apNew[i
16b2b 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a  ];.    assert( j
16b2c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20  <nMaxCells );.  
16b2d 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e    assert( pNew->
16b2e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77 5b 69 5d  pgno==pgnoNew[i]
16b2f 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65   );.    zeroPage
16b30 28 70 4e 65 77 2c 20 70 61 67 65 46 6c 61 67 73  (pNew, pageFlags
16b31 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50  );.    assembleP
16b32 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 4e 65 77  age(pNew, cntNew
16b33 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c 5b 6a  [i]-j, &apCell[j
16b34 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a  ], &szCell[j]);.
16b35 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77      assert( pNew
16b36 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 6e 4e  ->nCell>0 || (nN
16b37 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e 65 77 5b  ew==1 && cntNew[
16b38 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 20 61 73  0]==0) );.    as
16b39 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4f 76 65  sert( pNew->nOve
16b3a 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20 20 20  rflow==0 );..   
16b3b 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
16b3c 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61  n auto-vacuum da
16b3d 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 20 74  tabase, update t
16b3e 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65  he pointer map e
16b3f 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 74 68  ntries.    ** th
16b40 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  at point to the 
16b41 73 69 62 6c 69 6e 67 73 20 74 68 61 74 20 77 65  siblings that we
16b42 72 65 20 72 65 61 72 72 61 6e 67 65 64 2e 20 54  re rearranged. T
16b43 68 65 73 65 20 63 61 6e 20 62 65 3a 20 6c 65 66  hese can be: lef
16b44 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 72 65  t.    ** childre
16b45 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74 68 65 20  n of cells, the 
16b46 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 74  right-child of t
16b47 68 65 20 70 61 67 65 2c 20 6f 72 20 6f 76 65 72  he page, or over
16b48 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 20 20 2a  flow pages.    *
16b49 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  * pointed to by 
16b4a 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  cells..    */.  
16b4b 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
16b4c 55 4d 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  UM ){.      for(
16b4d 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77 5b 69 5d  k=j; k<cntNew[i]
16b4e 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; k++){.        
16b4f 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61 78 43 65  assert( k<nMaxCe
16b50 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 69  lls );.        i
16b51 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d 30 78 46  f( aFrom[k]==0xF
16b52 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 46 72 6f  F || apCopy[aFro
16b53 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d 70 4e 65  m[k]]->pgno!=pNe
16b54 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20  w->pgno ){.     
16b55 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70       rc = ptrmap
16b56 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c 20 6b 2d  PutOvfl(pNew, k-
16b57 6a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  j);.          if
16b58 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16b59 26 26 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  && leafCorrectio
16b5a 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  n==0 ){.        
16b5b 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50      rc = ptrmapP
16b5c 75 74 28 70 42 74 2c 20 67 65 74 34 62 79 74 65  ut(pBt, get4byte
16b5d 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20 50 54 52  (apCell[k]), PTR
16b5e 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 2d  MAP_BTREE, pNew-
16b5f 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  >pgno);.        
16b60 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
16b61 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
16b62 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  ){.            g
16b63 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61  oto balance_clea
16b64 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  nup;.          }
16b65 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
16b66 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6a 20   }.    }..    j 
16b67 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20  = cntNew[i];..  
16b68 20 20 2f 2a 20 49 66 20 74 68 65 20 73 69 62 6c    /* If the sibl
16b69 69 6e 67 20 70 61 67 65 20 61 73 73 65 6d 62 6c  ing page assembl
16b6a 65 64 20 61 62 6f 76 65 20 77 61 73 20 6e 6f 74  ed above was not
16b6b 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
16b6c 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20  sibling,.    ** 
16b6d 69 6e 73 65 72 74 20 61 20 64 69 76 69 64 65 72  insert a divider
16b6e 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70   cell into the p
16b6f 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20  arent page..    
16b70 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c 6e 4e 65  */.    if( i<nNe
16b71 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c 6c 20 29  w-1 && j<nCell )
16b72 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c  {.      u8 *pCel
16b73 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 54 65  l;.      u8 *pTe
16b74 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a  mp;.      int sz
16b75 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
16b76 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a   j<nMaxCells );.
16b77 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 61 70        pCell = ap
16b78 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 73  Cell[j];.      s
16b79 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20 2b 20  z = szCell[j] + 
16b7a 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a  leafCorrection;.
16b7b 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26 61        pTemp = &a
16b7c 53 70 61 63 65 32 5b 69 53 70 61 63 65 32 5d 3b  Space2[iSpace2];
16b7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77  .      if( !pNew
16b7e 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
16b7f 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 2d 3e    memcpy(&pNew->
16b80 61 44 61 74 61 5b 38 5d 2c 20 70 43 65 6c 6c 2c  aData[8], pCell,
16b81 20 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   4);.        if(
16b82 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 0a 20   ISAUTOVACUUM . 
16b83 20 20 20 20 20 20 20 20 26 26 20 28 61 46 72 6f          && (aFro
16b84 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c 20 61 70  m[j]==0xFF || ap
16b85 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d 5d 2d 3e  Copy[aFrom[j]]->
16b86 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e 6f  pgno!=pNew->pgno
16b87 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20  ).        ){.   
16b88 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d         rc = ptrm
16b89 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62  apPut(pBt, get4b
16b8a 79 74 65 28 70 43 65 6c 6c 29 2c 20 50 54 52 4d  yte(pCell), PTRM
16b8b 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 2d 3e  AP_BTREE, pNew->
16b8c 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20  pgno);.         
16b8d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16b8e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
16b8f 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63    goto balance_c
16b90 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20  leanup;.        
16b91 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
16b92 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65      }else if( le
16b93 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20  afData ){.      
16b94 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65    /* If the tree
16b95 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20   is a leaf-data 
16b96 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69  tree, and the si
16b97 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65  blings are leave
16b98 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  s, .        ** t
16b99 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20  hen there is no 
16b9a 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20  divider cell in 
16b9b 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61  apCell[]. Instea
16b9c 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 0a  d, the divider .
16b9d 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20          ** cell 
16b9e 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20  consists of the 
16b9f 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20  integer key for 
16ba0 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63  the right-most c
16ba1 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 20  ell of .        
16ba2 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d 70  ** the sibling-p
16ba3 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62  age assembled ab
16ba4 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20  ove only..      
16ba5 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 6c    */.        Cel
16ba6 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20  lInfo info;.    
16ba7 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20      j--;.       
16ba8 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
16ba9 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 77 2c 20  seCellPtr(pNew, 
16baa 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f  apCell[j], &info
16bab 29 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c  );.        pCell
16bac 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 20 20   = pTemp;.      
16bad 20 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 61    fillInCell(pPa
16bae 72 65 6e 74 2c 20 70 43 65 6c 6c 2c 20 30 2c 20  rent, pCell, 0, 
16baf 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c  info.nKey, 0, 0,
16bb0 20 30 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20   0, &sz);.      
16bb1 20 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20    pTemp = 0;.   
16bb2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
16bb3 20 20 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20    pCell -= 4;.  
16bb4 20 20 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65        /* Obscure
16bb5 20 63 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65   case for non-le
16bb6 61 66 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49  af-data trees: I
16bb7 66 20 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43  f the cell at pC
16bb8 65 6c 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20  ell was.        
16bb9 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74  ** previously st
16bba 6f 72 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e  ored on a leaf n
16bbb 6f 64 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70  ode, and its rep
16bbc 6f 72 74 65 64 20 73 69 7a 65 20 77 61 73 20 34  orted size was 4
16bbd 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65  .        ** byte
16bbe 73 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61  s, then it may a
16bbf 63 74 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c  ctually be small
16bc0 65 72 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20  er than this .  
16bc1 20 20 20 20 20 20 2a 2a 20 28 73 65 65 20 73 71        ** (see sq
16bc2 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
16bc3 65 6c 6c 50 74 72 28 29 2c 20 34 20 62 79 74 65  ellPtr(), 4 byte
16bc4 73 20 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d  s is the minimum
16bc5 20 73 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 20   size of.       
16bc6 20 2a 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42   ** any cell). B
16bc7 75 74 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61  ut it is importa
16bc8 6e 74 20 74 6f 20 70 61 73 73 20 74 68 65 20 63  nt to pass the c
16bc9 6f 72 72 65 63 74 20 73 69 7a 65 20 74 6f 20 0a  orrect size to .
16bca 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 65 72          ** inser
16bcb 74 43 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61  tCell(), so repa
16bcc 72 73 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77  rse the cell now
16bcd 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
16bce 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61       ** Note tha
16bcf 74 20 74 68 69 73 20 63 61 6e 20 6e 65 76 65 72  t this can never
16bd0 20 68 61 70 70 65 6e 20 69 6e 20 61 6e 20 53 51   happen in an SQ
16bd1 4c 69 74 65 20 64 61 74 61 20 66 69 6c 65 2c 20  Lite data file, 
16bd2 61 73 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a  as all.        *
16bd3 2a 20 63 65 6c 6c 73 20 61 72 65 20 61 74 20 6c  * cells are at l
16bd4 65 61 73 74 20 34 20 62 79 74 65 73 2e 20 49 74  east 4 bytes. It
16bd5 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 6e   only happens in
16bd6 20 62 2d 74 72 65 65 73 20 75 73 65 64 0a 20 20   b-trees used.  
16bd7 20 20 20 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c        ** to eval
16bd8 75 61 74 65 20 22 49 4e 20 28 53 45 4c 45 43 54  uate "IN (SELECT
16bd9 20 2e 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c   ...)" and simil
16bda 61 72 20 63 6c 61 75 73 65 73 2e 0a 20 20 20 20  ar clauses..    
16bdb 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
16bdc 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20  f( szCell[j]==4 
16bdd 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  ){.          ass
16bde 65 72 74 28 6c 65 61 66 43 6f 72 72 65 63 74 69  ert(leafCorrecti
16bdf 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20 20  on==4);.        
16be0 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50    sz = cellSizeP
16be1 74 72 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c  tr(pParent, pCel
16be2 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  l);.        }.  
16be3 20 20 20 20 7d 0a 20 20 20 20 20 20 69 53 70 61      }.      iSpa
16be4 63 65 32 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20  ce2 += sz;.     
16be5 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74   assert( sz<=pBt
16be6 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a  ->pageSize/4 );.
16be7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53        assert( iS
16be8 70 61 63 65 32 3c 3d 70 42 74 2d 3e 70 61 67 65  pace2<=pBt->page
16be9 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 72 63  Size );.      rc
16bea 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50   = insertCell(pP
16beb 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43  arent, nxDiv, pC
16bec 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20  ell, sz, pTemp, 
16bed 34 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  4);.      if( rc
16bee 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
16bef 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
16bf0 75 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  up;.      assert
16bf1 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
16bf2 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e  writeable(pParen
16bf3 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  t->pDbPage) );. 
16bf4 20 20 20 20 20 70 75 74 34 62 79 74 65 28 66 69       put4byte(fi
16bf5 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70  ndOverflowCell(p
16bf6 50 61 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70  Parent,nxDiv), p
16bf7 4e 65 77 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20  New->pgno);..   
16bf8 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
16bf9 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   an auto-vacuum 
16bfa 64 61 74 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f  database, and no
16bfb 74 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72  t a leaf-data tr
16bfc 65 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  ee,.      ** the
16bfd 6e 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69  n update the poi
16bfe 6e 74 65 72 20 6d 61 70 20 77 69 74 68 20 61 6e  nter map with an
16bff 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f   entry for the o
16c00 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20  verflow page.   
16c01 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63     ** that the c
16c02 65 6c 6c 20 6a 75 73 74 20 69 6e 73 65 72 74 65  ell just inserte
16c03 64 20 70 6f 69 6e 74 73 20 74 6f 20 28 69 66 20  d points to (if 
16c04 61 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  any)..      */. 
16c05 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56       if( ISAUTOV
16c06 41 43 55 55 4d 20 26 26 20 21 6c 65 61 66 44 61  ACUUM && !leafDa
16c07 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  ta ){.        rc
16c08 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c   = ptrmapPutOvfl
16c09 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29  (pParent, nxDiv)
16c0a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
16c0b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
16c0c 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61           goto ba
16c0d 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
16c0e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
16c0f 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20  .      j++;.    
16c10 20 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d    nxDiv++;.    }
16c11 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65  ..    /* Set the
16c12 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74   pointer-map ent
16c13 72 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73  ry for the new s
16c14 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a  ibling page. */.
16c15 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
16c16 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63  CUUM ){.      rc
16c17 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74   = ptrmapPut(pBt
16c18 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54  , pNew->pgno, PT
16c19 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72  RMAP_BTREE, pPar
16c1a 65 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20  ent->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 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
16c1d 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61  oto balance_clea
16c1e 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nup;.      }.   
16c1f 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
16c20 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61   j==nCell );.  a
16c21 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b  ssert( nOld>0 );
16c22 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e  .  assert( nNew>
16c23 30 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65  0 );.  if( (page
16c24 46 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46  Flags & PTF_LEAF
16c25 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a  )==0 ){.    u8 *
16c26 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79  zChild = &apCopy
16c27 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b  [nOld-1]->aData[
16c28 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  8];.    memcpy(&
16c29 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61  apNew[nNew-1]->a
16c2a 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c  Data[8], zChild,
16c2b 20 34 29 3b 0a 20 20 20 20 69 66 28 20 49 53 41   4);.    if( ISA
16c2c 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  UTOVACUUM ){.   
16c2d 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
16c2e 74 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28  t(pBt, get4byte(
16c2f 7a 43 68 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f  zChild), PTRMAP_
16c30 42 54 52 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65  BTREE, apNew[nNe
16c31 77 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20  w-1]->pgno);.   
16c32 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
16c33 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
16c34 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
16c35 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20  anup;.      }.  
16c36 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74    }.  }.  assert
16c37 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
16c38 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e  writeable(pParen
16c39 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  t->pDbPage) );. 
16c3a 20 69 66 28 20 6e 78 44 69 76 3d 3d 70 50 61 72   if( nxDiv==pPar
16c3b 65 6e 74 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 65  ent->nCell+pPare
16c3c 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b  nt->nOverflow ){
16c3d 0a 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f  .    /* Right-mo
16c3e 73 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68  st sibling is th
16c3f 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69  e right-most chi
16c40 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 20 2a 2f  ld of pParent */
16c41 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70  .    put4byte(&p
16c42 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50  Parent->aData[pP
16c43 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74  arent->hdrOffset
16c44 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65  +8], pgnoNew[nNe
16c45 77 2d 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  w-1]);.  }else{.
16c46 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73      /* Right-mos
16c47 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65  t sibling is the
16c48 20 6c 65 66 74 20 63 68 69 6c 64 20 6f 66 20 74   left child of t
16c49 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69  he first entry i
16c4a 6e 20 70 50 61 72 65 6e 74 0a 20 20 20 20 2a 2a  n pParent.    **
16c4b 20 70 61 73 74 20 74 68 65 20 72 69 67 68 74 2d   past the right-
16c4c 6d 6f 73 74 20 64 69 76 69 64 65 72 20 65 6e 74  most divider ent
16c4d 72 79 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79  ry */.    put4by
16c4e 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43  te(findOverflowC
16c4f 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44  ell(pParent, nxD
16c50 69 76 29 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65  iv), pgnoNew[nNe
16c51 77 2d 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  w-1]);.  }..  /*
16c52 0a 20 20 2a 2a 20 42 61 6c 61 6e 63 65 20 74 68  .  ** Balance th
16c53 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20  e parent page.  
16c54 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 75  Note that the cu
16c55 72 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 67  rrent page (pPag
16c56 65 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 68 61  e) might.  ** ha
16c57 76 65 20 62 65 65 6e 20 61 64 64 65 64 20 74 6f  ve been added to
16c58 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 73 6f   the freelist so
16c59 20 69 74 20 6d 69 67 68 74 20 6e 6f 20 6c 6f 6e   it might no lon
16c5a 67 65 72 20 62 65 20 69 6e 69 74 69 61 6c 69 7a  ger be initializ
16c5b 65 64 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68 65  ed..  ** But the
16c5c 20 70 61 72 65 6e 74 20 70 61 67 65 20 77 69 6c   parent page wil
16c5d 6c 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74  l always be init
16c5e 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20  ialized..  */.  
16c5f 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d  assert( pParent-
16c60 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 73 71 6c  >isInit );.  sql
16c61 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28  ite3ScratchFree(
16c62 61 70 43 65 6c 6c 29 3b 0a 20 20 61 70 43 65 6c  apCell);.  apCel
16c63 6c 20 3d 20 30 3b 0a 20 20 54 52 41 43 45 28 28  l = 0;.  TRACE((
16c64 22 42 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68  "BALANCE: finish
16c65 65 64 20 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d  ed with %d: old=
16c66 25 64 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d  %d new=%d cells=
16c67 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  %d\n",.         
16c68 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f   pPage->pgno, nO
16c69 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29  ld, nNew, nCell)
16c6a 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65  );.  pPage->nOve
16c6b 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 72 65 6c  rflow = 0;.  rel
16c6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b  easePage(pPage);
16c6d 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d  .  pCur->iPage--
16c6e 3b 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65  ;.  rc = balance
16c6f 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20  (pCur, 0);.  .  
16c70 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20  /*.  ** Cleanup 
16c71 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
16c72 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63  ..  */.balance_c
16c73 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65  leanup:.  sqlite
16c74 33 50 61 67 65 46 72 65 65 28 61 53 70 61 63 65  3PageFree(aSpace
16c75 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 63 72  2);.  sqlite3Scr
16c76 61 74 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29  atchFree(apCell)
16c77 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
16c78 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72  Old; i++){.    r
16c79 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64  eleasePage(apOld
16c7a 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  [i]);.  }.  for(
16c7b 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b  i=0; i<nNew; i++
16c7c 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  ){.    releasePa
16c7d 67 65 28 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20  ge(apNew[i]);.  
16c7e 7d 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65  }.  pCur->apPage
16c7f 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
16c80 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20  Overflow = 0;.. 
16c81 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
16c82 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
16c83 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20  e is called for 
16c84 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  the root page of
16c85 20 61 20 62 74 72 65 65 20 77 68 65 6e 20 74 68   a btree when th
16c86 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 63  e root.** page c
16c87 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73  ontains no cells
16c88 2e 20 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70  .  This is an op
16c89 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 6d 61 6b  portunity to mak
16c8a 65 20 74 68 65 20 74 72 65 65 0a 2a 2a 20 73 68  e the tree.** sh
16c8b 61 6c 6c 6f 77 65 72 20 62 79 20 6f 6e 65 20 6c  allower by one l
16c8c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  evel..*/.static 
16c8d 69 6e 74 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c  int balance_shal
16c8e 6c 6f 77 65 72 28 42 74 43 75 72 73 6f 72 20 2a  lower(BtCursor *
16c8f 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65  pCur){.  MemPage
16c90 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20   *pPage;        
16c91 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61        /* Root pa
16c92 67 65 20 6f 66 20 42 2d 54 72 65 65 20 2a 2f 0a  ge of B-Tree */.
16c93 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c    MemPage *pChil
16c94 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  d;             /
16c95 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64  * The only child
16c96 20 70 61 67 65 20 6f 66 20 70 50 61 67 65 20 2a   page of pPage *
16c97 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69  /.  Pgno pgnoChi
16c98 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ld;             
16c99 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
16c9a 66 6f 72 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20  for pChild */.  
16c9b 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
16c9c 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  OK;          /* 
16c9d 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  Return code from
16c9e 20 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a   subprocedures *
16c9f 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  /.  BtShared *pB
16ca0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
16ca1 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20      /* The main 
16ca2 42 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20  BTree structure 
16ca3 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 65 6c 6c 50  */.  int mxCellP
16ca4 65 72 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  erPage;         
16ca5 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d    /* Maximum num
16ca6 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 70 65 72  ber of cells per
16ca7 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a   page */.  u8 **
16ca8 61 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20  apCell;         
16ca9 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63          /* All c
16caa 65 6c 6c 73 20 66 72 6f 6d 20 70 61 67 65 73 20  ells from pages 
16cab 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a  being balanced *
16cac 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b  /.  u16 *szCell;
16cad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16cae 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f   /* Local size o
16caf 66 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a  f all cells */..
16cb0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16cb1 69 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 70 50  iPage==0 );.  pP
16cb2 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
16cb3 67 65 5b 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74  ge[0];..  assert
16cb4 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d  ( pPage->nCell==
16cb5 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
16cb6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
16cb7 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
16cb8 74 65 78 29 20 29 3b 0a 20 20 70 42 74 20 3d 20  tex) );.  pBt = 
16cb9 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 6d 78  pPage->pBt;.  mx
16cba 43 65 6c 6c 50 65 72 50 61 67 65 20 3d 20 4d 58  CellPerPage = MX
16cbb 5f 43 45 4c 4c 28 70 42 74 29 3b 0a 20 20 61 70  _CELL(pBt);.  ap
16cbc 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61  Cell = sqlite3Ma
16cbd 6c 6c 6f 63 28 20 6d 78 43 65 6c 6c 50 65 72 50  lloc( mxCellPerP
16cbe 61 67 65 2a 28 73 69 7a 65 6f 66 28 75 38 2a 29  age*(sizeof(u8*)
16cbf 2b 73 69 7a 65 6f 66 28 75 31 36 29 29 20 29 3b  +sizeof(u16)) );
16cc0 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30  .  if( apCell==0
16cc1 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
16cc2 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c  _NOMEM;.  szCell
16cc3 20 3d 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c   = (u16*)&apCell
16cc4 5b 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 5d 3b  [mxCellPerPage];
16cc5 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65  .  if( pPage->le
16cc6 61 66 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  af ){.    /* The
16cc7 20 74 61 62 6c 65 20 69 73 20 63 6f 6d 70 6c 65   table is comple
16cc8 74 65 6c 79 20 65 6d 70 74 79 20 2a 2f 0a 20 20  tely empty */.  
16cc9 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43    TRACE(("BALANC
16cca 45 3a 20 65 6d 70 74 79 20 74 61 62 6c 65 20 25  E: empty table %
16ccb 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e  d\n", pPage->pgn
16ccc 6f 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  o));.  }else{.  
16ccd 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61    /* The root pa
16cce 67 65 20 69 73 20 65 6d 70 74 79 20 62 75 74 20  ge is empty but 
16ccf 68 61 73 20 6f 6e 65 20 63 68 69 6c 64 2e 20 20  has one child.  
16cd0 54 72 61 6e 73 66 65 72 20 74 68 65 0a 20 20 20  Transfer the.   
16cd1 20 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20   ** information 
16cd2 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 63 68  from that one ch
16cd3 69 6c 64 20 69 6e 74 6f 20 74 68 65 20 72 6f 6f  ild into the roo
16cd4 74 20 70 61 67 65 20 69 66 20 69 74 20 0a 20 20  t page if it .  
16cd5 20 20 2a 2a 20 77 69 6c 6c 20 66 69 74 2e 20 20    ** will fit.  
16cd6 54 68 69 73 20 72 65 64 75 63 65 73 20 74 68 65  This reduces the
16cd7 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 74 72   depth of the tr
16cd8 65 65 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a  ee by one..    *
16cd9 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  *.    ** If the 
16cda 72 6f 6f 74 20 70 61 67 65 20 69 73 20 70 61 67  root page is pag
16cdb 65 20 31 2c 20 69 74 20 68 61 73 20 6c 65 73 73  e 1, it has less
16cdc 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65   space available
16cdd 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 69 74 73   than.    ** its
16cde 20 63 68 69 6c 64 20 28 64 75 65 20 74 6f 20 74   child (due to t
16cdf 68 65 20 31 30 30 20 62 79 74 65 20 68 65 61 64  he 100 byte head
16ce0 65 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 61  er that occurs a
16ce1 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  t the beginning.
16ce2 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 61      ** of the da
16ce3 74 61 62 61 73 65 20 66 6c 65 29 2c 20 73 6f 20  tabase fle), so 
16ce4 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20  it might not be 
16ce5 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c  able to hold all
16ce6 20 6f 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20   of the .    ** 
16ce7 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 75 72 72  information curr
16ce8 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20  ently contained 
16ce9 69 6e 20 74 68 65 20 63 68 69 6c 64 2e 20 20 49  in the child.  I
16cea 66 20 74 68 69 73 20 69 73 20 74 68 65 20 0a 20  f this is the . 
16ceb 20 20 20 2a 2a 20 63 61 73 65 2c 20 74 68 65 6e     ** case, then
16cec 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 65 20 74   do not do the t
16ced 72 61 6e 73 66 65 72 2e 20 20 4c 65 61 76 65 20  ransfer.  Leave 
16cee 70 61 67 65 20 31 20 65 6d 70 74 79 20 65 78 63  page 1 empty exc
16cef 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74  ept.    ** for t
16cf0 68 65 20 72 69 67 68 74 2d 70 6f 69 6e 74 65 72  he right-pointer
16cf1 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20 70 61   to the child pa
16cf2 67 65 2e 20 20 54 68 65 20 63 68 69 6c 64 20 70  ge.  The child p
16cf3 61 67 65 20 62 65 63 6f 6d 65 73 0a 20 20 20 20  age becomes.    
16cf4 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 72  ** the virtual r
16cf5 6f 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e  oot of the tree.
16cf6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 56 56 41 5f  .    */.    VVA_
16cf7 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65  ONLY( pCur->page
16cf8 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b  sShuffled = 1 );
16cf9 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d  .    pgnoChild =
16cfa 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
16cfb 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
16cfc 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
16cfd 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68    assert( pgnoCh
16cfe 69 6c 64 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  ild>0 );.    ass
16cff 65 72 74 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d  ert( pgnoChild<=
16d00 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
16d01 50 61 67 65 2d 3e 70 42 74 29 20 29 3b 0a 20 20  Page->pBt) );.  
16d02 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
16d03 72 65 65 47 65 74 50 61 67 65 28 70 50 61 67 65  reeGetPage(pPage
16d04 2d 3e 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64  ->pBt, pgnoChild
16d05 2c 20 26 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20  , &pChild, 0);. 
16d06 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
16d07 20 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c   end_shallow_bal
16d08 61 6e 63 65 3b 0a 20 20 20 20 69 66 28 20 70 50  ance;.    if( pP
16d09 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a  age->pgno==1 ){.
16d0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
16d0b 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28  e3BtreeInitPage(
16d0c 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 69  pChild);.      i
16d0d 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64  f( rc ) goto end
16d0e 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65  _shallow_balance
16d0f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
16d10 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  pChild->nOverflo
16d11 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  w==0 );.      if
16d12 28 20 70 43 68 69 6c 64 2d 3e 6e 46 72 65 65 3e  ( pChild->nFree>
16d13 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20  =100 ){.        
16d14 2f 2a 20 54 68 65 20 63 68 69 6c 64 20 69 6e 66  /* The child inf
16d15 6f 72 6d 61 74 69 6f 6e 20 77 69 6c 6c 20 66 69  ormation will fi
16d16 74 20 6f 6e 20 74 68 65 20 72 6f 6f 74 20 70 61  t on the root pa
16d17 67 65 2c 20 73 6f 20 64 6f 20 74 68 65 0a 20 20  ge, so do the.  
16d18 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 2a 2f        ** copy */
16d19 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  .        int i;.
16d1a 20 20 20 20 20 20 20 20 7a 65 72 6f 50 61 67 65          zeroPage
16d1b 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e  (pPage, pChild->
16d1c 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20  aData[0]);.     
16d1d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43     for(i=0; i<pC
16d1e 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b  hild->nCell; i++
16d1f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 43  ){.          apC
16d20 65 6c 6c 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c  ell[i] = findCel
16d21 6c 28 70 43 68 69 6c 64 2c 69 29 3b 0a 20 20 20  l(pChild,i);.   
16d22 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 69 5d         szCell[i]
16d23 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
16d24 43 68 69 6c 64 2c 20 61 70 43 65 6c 6c 5b 69 5d  Child, apCell[i]
16d25 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
16d26 20 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67       assemblePag
16d27 65 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d  e(pPage, pChild-
16d28 3e 6e 43 65 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20  >nCell, apCell, 
16d29 73 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20  szCell);.       
16d2a 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 69 67   /* Copy the rig
16d2b 68 74 2d 70 6f 69 6e 74 65 72 20 6f 66 20 74 68  ht-pointer of th
16d2c 65 20 63 68 69 6c 64 20 74 6f 20 74 68 65 20 70  e child to the p
16d2d 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20  arent. */.      
16d2e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16d2f 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
16d30 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
16d31 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74  ) );.        put
16d32 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  4byte(&pPage->aD
16d33 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
16d34 66 73 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20  fset+8], .      
16d35 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26        get4byte(&
16d36 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43  pChild->aData[pC
16d37 68 69 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b  hild->hdrOffset+
16d38 38 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63  8]));.        rc
16d39 20 3d 20 66 72 65 65 50 61 67 65 28 70 43 68 69   = freePage(pChi
16d3a 6c 64 29 3b 0a 20 20 20 20 20 20 20 20 54 52 41  ld);.        TRA
16d3b 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68  CE(("BALANCE: ch
16d3c 69 6c 64 20 25 64 20 74 72 61 6e 73 66 65 72 20  ild %d transfer 
16d3d 74 6f 20 70 61 67 65 20 31 5c 6e 22 2c 20 70 43  to page 1\n", pC
16d3e 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20  hild->pgno));.  
16d3f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
16d40 20 20 20 2f 2a 20 54 68 65 20 63 68 69 6c 64 20     /* The child 
16d41 68 61 73 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61  has more informa
16d42 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 66  tion that will f
16d43 69 74 20 6f 6e 20 74 68 65 20 72 6f 6f 74 2e 0a  it on the root..
16d44 20 20 20 20 20 20 20 20 2a 2a 20 54 68 65 20 74          ** The t
16d45 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 62  ree is already b
16d46 61 6c 61 6e 63 65 64 2e 20 20 44 6f 20 6e 6f 74  alanced.  Do not
16d47 68 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 20  hing. */.       
16d48 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45   TRACE(("BALANCE
16d49 3a 20 63 68 69 6c 64 20 25 64 20 77 69 6c 6c 20  : child %d will 
16d4a 6e 6f 74 20 66 69 74 20 6f 6e 20 70 61 67 65 20  not fit on page 
16d4b 31 5c 6e 22 2c 20 70 43 68 69 6c 64 2d 3e 70 67  1\n", pChild->pg
16d4c 6e 6f 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  no));.      }.  
16d4d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d    }else{.      m
16d4e 65 6d 63 70 79 28 70 50 61 67 65 2d 3e 61 44 61  emcpy(pPage->aDa
16d4f 74 61 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74  ta, pChild->aDat
16d50 61 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  a, pPage->pBt->u
16d51 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20  sableSize);.    
16d52 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
16d53 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
16d54 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
16d55 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
16d56 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
16d57 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
16d58 20 20 66 72 65 65 50 61 67 65 28 70 43 68 69 6c    freePage(pChil
16d59 64 29 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28  d);.      TRACE(
16d5a 28 22 42 41 4c 41 4e 43 45 3a 20 74 72 61 6e 73  ("BALANCE: trans
16d5b 66 65 72 20 63 68 69 6c 64 20 25 64 20 69 6e 74  fer child %d int
16d5c 6f 20 72 6f 6f 74 20 25 64 5c 6e 22 2c 0a 20 20  o root %d\n",.  
16d5d 20 20 20 20 20 20 20 20 20 20 20 20 70 43 68 69              pChi
16d5e 6c 64 2d 3e 70 67 6e 6f 2c 20 70 50 61 67 65 2d  ld->pgno, pPage-
16d5f 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20  >pgno));.    }. 
16d60 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
16d61 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29  ->nOverflow==0 )
16d62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
16d63 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
16d64 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56  .    if( ISAUTOV
16d65 41 43 55 55 4d 20 26 26 20 72 63 3d 3d 53 51 4c  ACUUM && rc==SQL
16d66 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
16d67 72 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72  rc = setChildPtr
16d68 6d 61 70 73 28 70 50 61 67 65 29 3b 0a 20 20 20  maps(pPage);.   
16d69 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65   }.#endif.    re
16d6a 6c 65 61 73 65 50 61 67 65 28 70 43 68 69 6c 64  leasePage(pChild
16d6b 29 3b 0a 20 20 7d 0a 65 6e 64 5f 73 68 61 6c 6c  );.  }.end_shall
16d6c 6f 77 5f 62 61 6c 61 6e 63 65 3a 0a 20 20 73 71  ow_balance:.  sq
16d6d 6c 69 74 65 33 5f 66 72 65 65 28 61 70 43 65 6c  lite3_free(apCel
16d6e 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  l);.  return rc;
16d6f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72  .}.../*.** The r
16d70 6f 6f 74 20 70 61 67 65 20 69 73 20 6f 76 65 72  oot page is over
16d71 66 75 6c 6c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  full.**.** When 
16d72 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 43 72  this happens, Cr
16d73 65 61 74 65 20 61 20 6e 65 77 20 63 68 69 6c 64  eate a new child
16d74 20 70 61 67 65 20 61 6e 64 20 63 6f 70 79 20 74   page and copy t
16d75 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f  he.** contents o
16d76 66 20 74 68 65 20 72 6f 6f 74 20 69 6e 74 6f 20  f the root into 
16d77 74 68 65 20 63 68 69 6c 64 2e 20 20 54 68 65 6e  the child.  Then
16d78 20 6d 61 6b 65 20 74 68 65 20 72 6f 6f 74 0a 2a   make the root.*
16d79 2a 20 70 61 67 65 20 61 6e 20 65 6d 70 74 79 20  * page an empty 
16d7a 70 61 67 65 20 77 69 74 68 20 72 69 67 68 74 43  page with rightC
16d7b 68 69 6c 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f  hild pointing to
16d7c 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 68 69 6c   the new.** chil
16d7d 64 2e 20 20 20 46 69 6e 61 6c 6c 79 2c 20 63 61  d.   Finally, ca
16d7e 6c 6c 20 62 61 6c 61 6e 63 65 5f 69 6e 74 65 72  ll balance_inter
16d7f 6e 61 6c 28 29 20 6f 6e 20 74 68 65 20 6e 65 77  nal() on the new
16d80 20 63 68 69 6c 64 0a 2a 2a 20 74 6f 20 63 61 75   child.** to cau
16d81 73 65 20 69 74 20 74 6f 20 73 70 6c 69 74 2e 0a  se it to split..
16d82 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61  */.static int ba
16d83 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 42 74 43  lance_deeper(BtC
16d84 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
16d85 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
16d86 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61      /* Return va
16d87 6c 75 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63  lue from subproc
16d88 65 64 75 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50  edures */.  MemP
16d89 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20  age *pPage;     
16d8a 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
16d8b 65 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20  e root page */. 
16d8c 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64   MemPage *pChild
16d8d 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20  ;    /* Pointer 
16d8e 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70  to a new child p
16d8f 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  age */.  Pgno pg
16d90 6e 6f 43 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20  noChild;     /* 
16d91 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  Page number of t
16d92 68 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67  he new child pag
16d93 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  e */.  BtShared 
16d94 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  *pBt;         /*
16d95 20 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 20 20   The BTree */.  
16d96 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20  int usableSize; 
16d97 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61      /* Total usa
16d98 62 6c 65 20 73 69 7a 65 20 6f 66 20 61 20 70 61  ble size of a pa
16d99 67 65 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61  ge */.  u8 *data
16d9a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  ;           /* C
16d9b 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61  ontent of the pa
16d9c 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 75  rent page */.  u
16d9d 38 20 2a 63 64 61 74 61 3b 20 20 20 20 20 20 20  8 *cdata;       
16d9e 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
16d9f 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20   the child page 
16da0 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20  */.  int hdr;   
16da1 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
16da2 65 74 20 74 6f 20 70 61 67 65 20 68 65 61 64 65  et to page heade
16da3 72 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20  r in parent */. 
16da4 20 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20   int cbrk;      
16da5 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
16da6 6f 20 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72  o content of fir
16da7 73 74 20 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e  st cell in paren
16da8 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
16da9 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29  pCur->iPage==0 )
16daa 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16dab 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 6e 4f 76  ->apPage[0]->nOv
16dac 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 56  erflow>0 );..  V
16dad 56 41 5f 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70  VA_ONLY( pCur->p
16dae 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20 31  agesShuffled = 1
16daf 20 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43   );.  pPage = pC
16db0 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20  ur->apPage[0];. 
16db1 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42   pBt = pPage->pB
16db2 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  t;.  assert( sql
16db3 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
16db4 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
16db5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16db6 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
16db7 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
16db8 20 29 3b 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63   );.  rc = alloc
16db9 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
16dba 2c 20 26 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f  , &pChild, &pgno
16dbb 43 68 69 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67  Child, pPage->pg
16dbc 6e 6f 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  no, 0);.  if( rc
16dbd 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
16dbe 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
16dbf 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
16dc0 70 43 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29  pChild->pDbPage)
16dc1 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65   );.  usableSize
16dc2 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
16dc3 7a 65 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61  ze;.  data = pPa
16dc4 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72  ge->aData;.  hdr
16dc5 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
16dc6 73 65 74 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65  set;.  cbrk = ge
16dc7 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
16dc8 2b 35 5d 29 3b 0a 20 20 63 64 61 74 61 20 3d 20  +5]);.  cdata = 
16dc9 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20  pChild->aData;. 
16dca 20 6d 65 6d 63 70 79 28 63 64 61 74 61 2c 20 26   memcpy(cdata, &
16dcb 64 61 74 61 5b 68 64 72 5d 2c 20 70 50 61 67 65  data[hdr], pPage
16dcc 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70  ->cellOffset+2*p
16dcd 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29  Page->nCell-hdr)
16dce 3b 0a 20 20 6d 65 6d 63 70 79 28 26 63 64 61 74  ;.  memcpy(&cdat
16dcf 61 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63  a[cbrk], &data[c
16dd0 62 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65  brk], usableSize
16dd1 2d 63 62 72 6b 29 3b 0a 0a 20 20 61 73 73 65 72  -cbrk);..  asser
16dd2 74 28 20 70 43 68 69 6c 64 2d 3e 69 73 49 6e 69  t( pChild->isIni
16dd3 74 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73  t==0 );.  rc = s
16dd4 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
16dd5 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 69  age(pChild);.  i
16dd6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
16dd7 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70   ){.    int nCop
16dd8 79 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  y = pPage->nOver
16dd9 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 50 61 67  flow*sizeof(pPag
16dda 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 3b 0a 20 20  e->aOvfl[0]);.  
16ddb 20 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d    memcpy(pChild-
16ddc 3e 61 4f 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61  >aOvfl, pPage->a
16ddd 4f 76 66 6c 2c 20 6e 43 6f 70 79 29 3b 0a 20 20  Ovfl, nCopy);.  
16dde 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66    pChild->nOverf
16ddf 6c 6f 77 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76  low = pPage->nOv
16de0 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20  erflow;.    if( 
16de1 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  pChild->nOverflo
16de2 77 20 29 7b 0a 20 20 20 20 20 20 70 43 68 69 6c  w ){.      pChil
16de3 64 2d 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20  d->nFree = 0;.  
16de4 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
16de5 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70  pChild->nCell==p
16de6 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
16de7 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
16de8 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
16de9 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
16dea 65 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61  e) );.    zeroPa
16deb 67 65 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64  ge(pPage, pChild
16dec 2d 3e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54  ->aData[0] & ~PT
16ded 46 5f 4c 45 41 46 29 3b 0a 20 20 20 20 70 75 74  F_LEAF);.    put
16dee 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  4byte(&pPage->aD
16def 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
16df0 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69  fset+8], pgnoChi
16df1 6c 64 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28  ld);.    TRACE((
16df2 22 42 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72  "BALANCE: copy r
16df3 6f 6f 74 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e  oot %d into %d\n
16df4 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20  ", pPage->pgno, 
16df5 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a  pChild->pgno));.
16df6 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
16df7 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63  CUUM ){.      rc
16df8 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74   = ptrmapPut(pBt
16df9 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20  , pChild->pgno, 
16dfa 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50  PTRMAP_BTREE, pP
16dfb 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e  age->pgno);.#ifn
16dfc 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
16dfd 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
16dfe 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16dff 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
16e00 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61   = setChildPtrma
16e01 70 73 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20  ps(pChild);.    
16e02 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63    }.      if( rc
16e03 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 68 69   ){.        pChi
16e04 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20  ld->nOverflow = 
16e05 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  0;.      }.#endi
16e06 66 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  f.    }.  }..  i
16e07 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
16e08 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 50   ){.    pCur->iP
16e09 61 67 65 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d  age++;.    pCur-
16e0a 3e 61 70 50 61 67 65 5b 31 5d 20 3d 20 70 43 68  >apPage[1] = pCh
16e0b 69 6c 64 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61  ild;.    pCur->a
16e0c 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20  iIdx[0] = 0;.   
16e0d 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f   rc = balance_no
16e0e 6e 72 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d  nroot(pCur);.  }
16e0f 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73  else{.    releas
16e10 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20  ePage(pChild);. 
16e11 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
16e12 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  .}../*.** The pa
16e13 67 65 20 74 68 61 74 20 70 43 75 72 20 63 75 72  ge that pCur cur
16e14 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f  rently points to
16e15 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d   has just been m
16e16 6f 64 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f  odified in.** so
16e17 6d 65 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e  me way. This fun
16e18 63 74 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75  ction figures ou
16e19 74 20 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69  t if this modifi
16e1a 63 61 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65  cation means the
16e1b 0a 2a 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74  .** tree needs t
16e1c 6f 20 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61  o be balanced, a
16e1d 6e 64 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74  nd if so calls t
16e1e 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 62  he appropriate b
16e1f 61 6c 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75  alancing .** rou
16e20 74 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72  tine..** .** Par
16e21 61 6d 65 74 65 72 20 69 73 49 6e 73 65 72 74 20  ameter isInsert 
16e22 69 73 20 74 72 75 65 20 69 66 20 61 20 6e 65 77  is true if a new
16e23 20 63 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69   cell was just i
16e24 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65  nserted into the
16e25 0a 2a 2a 20 70 61 67 65 2c 20 6f 72 20 66 61 6c  .** page, or fal
16e26 73 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f  se otherwise..*/
16e27 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61  .static int bala
16e28 6e 63 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43  nce(BtCursor *pC
16e29 75 72 2c 20 69 6e 74 20 69 73 49 6e 73 65 72 74  ur, int isInsert
16e2a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
16e2b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61  LITE_OK;.  MemPa
16e2c 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72  ge *pPage = pCur
16e2d 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
16e2e 50 61 67 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74  Page];..  assert
16e2f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
16e30 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
16e31 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
16e32 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20   pCur->iPage==0 
16e33 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
16e34 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
16e35 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
16e36 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
16e37 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e  E_OK && pPage->n
16e38 4f 76 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20  Overflow>0 ){.  
16e39 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
16e3a 5f 64 65 65 70 65 72 28 70 43 75 72 29 3b 0a 20  _deeper(pCur);. 
16e3b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75       assert( pCu
16e3c 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3d 3d 70 50  r->apPage[0]==pP
16e3d 61 67 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73  age );.      ass
16e3e 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ert( pPage->nOve
16e3f 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63 21 3d  rflow==0 || rc!=
16e40 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
16e41 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
16e42 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67  QLITE_OK && pPag
16e43 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20  e->nCell==0 ){. 
16e44 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63       rc = balanc
16e45 65 5f 73 68 61 6c 6c 6f 77 65 72 28 70 43 75 72  e_shallower(pCur
16e46 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
16e47 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
16e48 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20  ==pPage );.     
16e49 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
16e4a 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20  nOverflow==0 || 
16e4b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
16e4c 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
16e4d 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e      if( pPage->n
16e4e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20  Overflow>0 || . 
16e4f 20 20 20 20 20 20 20 28 21 69 73 49 6e 73 65 72         (!isInser
16e50 74 20 26 26 20 70 50 61 67 65 2d 3e 6e 46 72 65  t && pPage->nFre
16e51 65 3e 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  e>pPage->pBt->us
16e52 61 62 6c 65 53 69 7a 65 2a 32 2f 33 29 20 29 7b  ableSize*2/3) ){
16e53 0a 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61  .      rc = bala
16e54 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 75 72  nce_nonroot(pCur
16e55 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
16e56 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
16e57 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
16e58 63 68 65 63 6b 73 20 61 6c 6c 20 63 75 72 73 6f  checks all curso
16e59 72 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f  rs that point to
16e5a 20 74 61 62 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e   table pgnoRoot.
16e5b 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68  .** If any of th
16e5c 6f 73 65 20 63 75 72 73 6f 72 73 20 77 65 72 65  ose cursors were
16e5d 20 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46   opened with wrF
16e5e 6c 61 67 3d 3d 30 20 69 6e 20 61 20 64 69 66 66  lag==0 in a diff
16e5f 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73  erent.** databas
16e60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 61 20  e connection (a 
16e61 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
16e62 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20  ion that shares 
16e63 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 63 61 63  the pager.** cac
16e64 68 65 20 77 69 74 68 20 74 68 65 20 63 75 72 72  he with the curr
16e65 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20  ent connection) 
16e66 61 6e 64 20 74 68 61 74 20 6f 74 68 65 72 20 63  and that other c
16e67 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 69 73  onnection .** is
16e68 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 65 61 64   not in the Read
16e69 55 6e 63 6f 6d 6d 6d 69 74 74 65 64 20 73 74 61  Uncommmitted sta
16e6a 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  te, then this ro
16e6b 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 0a 2a  utine returns .*
16e6c 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e  * SQLITE_LOCKED.
16e6d 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61  .**.** As well a
16e6e 73 20 63 75 72 73 6f 72 73 20 77 69 74 68 20 77  s cursors with w
16e6f 72 46 6c 61 67 3d 3d 30 2c 20 63 75 72 73 6f 72  rFlag==0, cursor
16e70 73 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 31  s with wrFlag==1
16e71 20 61 6e 64 20 0a 2a 2a 20 69 73 49 6e 63 72 62   and .** isIncrb
16e72 6c 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61 72 65  lobHandle==1 are
16e73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64   also considered
16e74 20 27 72 65 61 64 27 20 63 75 72 73 6f 72 73 2e   'read' cursors.
16e75 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 0a 2a 2a   Incremental .**
16e76 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 61 72   blob cursors ar
16e77 65 20 75 73 65 64 20 66 6f 72 20 62 6f 74 68 20  e used for both 
16e78 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74  reading and writ
16e79 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ing..**.** When 
16e7a 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 68 65 20  pgnoRoot is the 
16e7b 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 6e 20  root page of an 
16e7c 69 6e 74 6b 65 79 20 74 61 62 6c 65 2c 20 74 68  intkey table, th
16e7d 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
16e7e 6c 73 6f 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62  lso.** responsib
16e7f 6c 65 20 66 6f 72 20 69 6e 76 61 6c 69 64 61 74  le for invalidat
16e80 69 6e 67 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20  ing incremental 
16e81 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 77 68 65  blob cursors whe
16e82 6e 20 74 68 65 20 74 61 62 6c 65 20 72 6f 77 0a  n the table row.
16e83 2a 2a 20 6f 6e 20 77 68 69 63 68 20 74 68 65 79  ** on which they
16e84 20 61 72 65 20 6f 70 65 6e 65 64 20 69 73 20 64   are opened is d
16e85 65 6c 65 74 65 64 20 6f 72 20 6d 6f 64 69 66 69  eleted or modifi
16e86 65 64 2e 20 43 75 72 73 6f 72 73 20 61 72 65 20  ed. Cursors are 
16e87 69 6e 76 61 6c 69 64 61 74 65 64 0a 2a 2a 20 61  invalidated.** a
16e88 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
16e89 66 6f 6c 6c 6f 77 69 6e 67 20 72 75 6c 65 73 3a  following rules:
16e8a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 6e  .**.**   1) When
16e8b 20 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65   BtreeClearTable
16e8c 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  () is called to 
16e8d 63 6f 6d 70 6c 65 74 65 6c 79 20 64 65 6c 65 74  completely delet
16e8e 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a  e the contents.*
16e8f 2a 20 20 20 20 20 20 6f 66 20 61 20 42 2d 54 72  *      of a B-Tr
16e90 65 65 20 74 61 62 6c 65 2c 20 70 45 78 63 6c 75  ee table, pExclu
16e91 64 65 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72  de is set to zer
16e92 6f 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20  o and parameter 
16e93 69 52 6f 77 20 69 73 20 0a 2a 2a 20 20 20 20 20  iRow is .**     
16e94 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f   set to non-zero
16e95 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61  . In this case a
16e96 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62  ll incremental b
16e97 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e  lob cursors open
16e98 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 65 20  .**      on the 
16e99 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 61 74 20  table rooted at 
16e9a 70 67 6e 6f 52 6f 6f 74 20 61 72 65 20 69 6e 76  pgnoRoot are inv
16e9b 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  alidated..**.** 
16e9c 20 20 32 29 20 57 68 65 6e 20 42 74 72 65 65 49    2) When BtreeI
16e9d 6e 73 65 72 74 28 29 2c 20 42 74 72 65 65 44 65  nsert(), BtreeDe
16e9e 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65 65 50  lete() or BtreeP
16e9f 75 74 44 61 74 61 28 29 20 69 73 20 63 61 6c 6c  utData() is call
16ea0 65 64 20 74 6f 20 0a 2a 2a 20 20 20 20 20 20 6d  ed to .**      m
16ea1 6f 64 69 66 79 20 61 20 74 61 62 6c 65 20 72 6f  odify a table ro
16ea2 77 20 76 69 61 20 61 6e 20 53 51 4c 20 73 74 61  w via an SQL sta
16ea3 74 65 6d 65 6e 74 2c 20 70 45 78 63 6c 75 64 65  tement, pExclude
16ea4 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a   is set to the .
16ea5 2a 2a 20 20 20 20 20 20 77 72 69 74 65 20 63 75  **      write cu
16ea6 72 73 6f 72 20 75 73 65 64 20 74 6f 20 64 6f 20  rsor used to do 
16ea7 74 68 65 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e  the modification
16ea8 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69   and parameter i
16ea9 52 6f 77 20 69 73 20 73 65 74 0a 2a 2a 20 20 20  Row is set.**   
16eaa 20 20 20 74 6f 20 74 68 65 20 69 6e 74 65 67 65     to the intege
16eab 72 20 72 6f 77 20 69 64 20 6f 66 20 74 68 65 20  r row id of the 
16eac 42 2d 54 72 65 65 20 65 6e 74 72 79 20 62 65 69  B-Tree entry bei
16ead 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 55 6e 6c  ng modified. Unl
16eae 65 73 73 0a 2a 2a 20 20 20 20 20 20 70 45 78 63  ess.**      pExc
16eaf 6c 75 64 65 20 69 73 20 69 74 73 65 6c 66 20 61  lude is itself a
16eb0 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c  n incremental bl
16eb1 6f 62 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20  ob cursor, then 
16eb2 61 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 0a  all incremental.
16eb3 2a 2a 20 20 20 20 20 20 62 6c 6f 62 20 63 75 72  **      blob cur
16eb4 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 72 6f 77  sors open on row
16eb5 20 69 52 6f 77 20 6f 66 20 74 68 65 20 42 2d 54   iRow of the B-T
16eb6 72 65 65 20 61 72 65 20 69 6e 76 61 6c 69 64 61  ree are invalida
16eb7 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20  ted..**.**   3) 
16eb8 49 66 20 62 6f 74 68 20 70 45 78 63 6c 75 64 65  If both pExclude
16eb9 20 61 6e 64 20 69 52 6f 77 20 61 72 65 20 73 65   and iRow are se
16eba 74 20 74 6f 20 7a 65 72 6f 2c 20 6e 6f 20 69 6e  t to zero, no in
16ebb 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 0a  cremental blob .
16ebc 2a 2a 20 20 20 20 20 20 63 75 72 73 6f 72 73 20  **      cursors 
16ebd 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e  are invalidated.
16ebe 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
16ebf 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 0a 20  heckReadLocks(. 
16ec0 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20   Btree *pBtree, 
16ec1 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74  .  Pgno pgnoRoot
16ec2 2c 20 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  , .  BtCursor *p
16ec3 45 78 63 6c 75 64 65 2c 0a 20 20 69 36 34 20 69  Exclude,.  i64 i
16ec4 52 6f 77 0a 29 7b 0a 20 20 42 74 43 75 72 73 6f  Row.){.  BtCurso
16ec5 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 65 64  r *p;.  BtShared
16ec6 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e   *pBt = pBtree->
16ec7 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  pBt;.  sqlite3 *
16ec8 64 62 20 3d 20 70 42 74 72 65 65 2d 3e 64 62 3b  db = pBtree->db;
16ec9 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16eca 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
16ecb 78 28 70 42 74 72 65 65 29 20 29 3b 0a 20 20 66  x(pBtree) );.  f
16ecc 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f  or(p=pBt->pCurso
16ecd 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  r; p; p=p->pNext
16ece 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d 70 45  ){.    if( p==pE
16ecf 78 63 6c 75 64 65 20 29 20 63 6f 6e 74 69 6e 75  xclude ) continu
16ed0 65 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67  e;.    if( p->pg
16ed1 6e 6f 52 6f 6f 74 21 3d 70 67 6e 6f 52 6f 6f 74  noRoot!=pgnoRoot
16ed2 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66   ) continue;.#if
16ed3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16ed4 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 69 66  _INCRBLOB.    if
16ed5 28 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48  ( p->isIncrblobH
16ed6 61 6e 64 6c 65 20 26 26 20 28 20 0a 20 20 20 20  andle && ( .    
16ed7 20 20 20 20 20 28 21 70 45 78 63 6c 75 64 65 20       (!pExclude 
16ed8 26 26 20 69 52 6f 77 29 0a 20 20 20 20 20 20 7c  && iRow).      |
16ed9 7c 20 28 70 45 78 63 6c 75 64 65 20 26 26 20 21  | (pExclude && !
16eda 70 45 78 63 6c 75 64 65 2d 3e 69 73 49 6e 63 72  pExclude->isIncr
16edb 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 70 2d  blobHandle && p-
16edc 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77  >info.nKey==iRow
16edd 29 0a 20 20 20 20 29 29 7b 0a 20 20 20 20 20 20  ).    )){.      
16ede 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  p->eState = CURS
16edf 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20  OR_INVALID;.    
16ee0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  }.#endif.    if(
16ee1 20 70 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53   p->eState!=CURS
16ee2 4f 52 5f 56 41 4c 49 44 20 29 20 63 6f 6e 74 69  OR_VALID ) conti
16ee3 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  nue;.    if( p->
16ee4 77 72 46 6c 61 67 3d 3d 30 20 0a 23 69 66 6e 64  wrFlag==0 .#ifnd
16ee5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
16ee6 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 7c 7c 20  NCRBLOB.     || 
16ee7 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  p->isIncrblobHan
16ee8 64 6c 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 29  dle.#endif.    )
16ee9 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20  {.      sqlite3 
16eea 2a 64 62 4f 74 68 65 72 20 3d 20 70 2d 3e 70 42  *dbOther = p->pB
16eeb 74 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20  tree->db;.      
16eec 69 66 28 20 64 62 4f 74 68 65 72 3d 3d 30 20 7c  if( dbOther==0 |
16eed 7c 0a 20 20 20 20 20 20 20 20 20 28 64 62 4f 74  |.         (dbOt
16eee 68 65 72 21 3d 64 62 20 26 26 20 28 64 62 4f 74  her!=db && (dbOt
16eef 68 65 72 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  her->flags & SQL
16ef0 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74  ITE_ReadUncommit
16ef1 74 65 64 29 3d 3d 30 29 20 29 7b 0a 20 20 20 20  ted)==0) ){.    
16ef2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16ef3 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20  E_LOCKED;.      
16ef4 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
16ef5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
16ef6 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
16ef7 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e 74  a new record int
16ef8 6f 20 74 68 65 20 42 54 72 65 65 2e 20 20 54 68  o the BTree.  Th
16ef9 65 20 6b 65 79 20 69 73 20 67 69 76 65 6e 20 62  e key is given b
16efa 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a 2a 2a  y (pKey,nKey).**
16efb 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69 73   and the data is
16efc 20 67 69 76 65 6e 20 62 79 20 28 70 44 61 74 61   given by (pData
16efd 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 20 63 75  ,nData).  The cu
16efe 72 73 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c  rsor is used onl
16eff 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65 20 77  y to.** define w
16f00 68 61 74 20 74 61 62 6c 65 20 74 68 65 20 72 65  hat table the re
16f01 63 6f 72 64 20 73 68 6f 75 6c 64 20 62 65 20 69  cord should be i
16f02 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 20 20 54  nserted into.  T
16f03 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20  he cursor.** is 
16f04 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
16f05 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 74 69   a random locati
16f06 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e  on..**.** For an
16f07 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c 20 6f   INTKEY table, o
16f08 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 76 61 6c  nly the nKey val
16f09 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73  ue of the key is
16f0a 20 75 73 65 64 2e 20 20 70 4b 65 79 20 69 73 0a   used.  pKey is.
16f0b 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46 6f 72  ** ignored.  For
16f0c 20 61 20 5a 45 52 4f 44 41 54 41 20 74 61 62 6c   a ZERODATA tabl
16f0d 65 2c 20 74 68 65 20 70 44 61 74 61 20 61 6e 64  e, the pData and
16f0e 20 6e 44 61 74 61 20 61 72 65 20 62 6f 74 68 20   nData are both 
16f0f 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ignored..*/.SQLI
16f10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16f11 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72  qlite3BtreeInser
16f12 74 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  t(.  BtCursor *p
16f13 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  Cur,            
16f14 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 64 61      /* Insert da
16f15 74 61 20 69 6e 74 6f 20 74 68 65 20 74 61 62 6c  ta into the tabl
16f16 65 20 6f 66 20 74 68 69 73 20 63 75 72 73 6f 72  e of this cursor
16f17 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
16f18 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79   *pKey, i64 nKey
16f19 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20  ,    /* The key 
16f1a 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  of the new recor
16f1b 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  d */.  const voi
16f1c 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  d *pData, int nD
16f1d 61 74 61 2c 20 20 2f 2a 20 54 68 65 20 64 61 74  ata,  /* The dat
16f1e 61 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63  a of the new rec
16f1f 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65  ord */.  int nZe
16f20 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ro,             
16f21 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
16f22 72 20 6f 66 20 65 78 74 72 61 20 30 20 62 79 74  r of extra 0 byt
16f23 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20  es to append to 
16f24 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 61 70  data */.  int ap
16f25 70 65 6e 64 42 69 61 73 20 20 20 20 20 20 20 20  pendBias        
16f26 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
16f27 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65   if this is like
16f28 6c 79 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a  ly an append */.
16f29 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
16f2a 6e 74 20 6c 6f 63 3b 0a 20 20 69 6e 74 20 73 7a  nt loc;.  int sz
16f2b 4e 65 77 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a  New;.  int idx;.
16f2c 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
16f2d 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70  ;.  Btree *p = p
16f2e 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42  Cur->pBtree;.  B
16f2f 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
16f30 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65  ->pBt;.  unsigne
16f31 64 20 63 68 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b  d char *oldCell;
16f32 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
16f33 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 30 3b 0a 0a   *newCell = 0;..
16f34 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
16f35 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
16f36 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   );.  assert( pB
16f37 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
16f38 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
16f39 0a 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d  .  assert( !pBt-
16f3a 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61  >readOnly );.  a
16f3b 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46  ssert( pCur->wrF
16f3c 6c 61 67 20 29 3b 0a 20 20 69 66 28 20 63 68 65  lag );.  if( che
16f3d 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43 75 72  ckReadLocks(pCur
16f3e 2d 3e 70 42 74 72 65 65 2c 20 70 43 75 72 2d 3e  ->pBtree, pCur->
16f3f 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2c 20  pgnoRoot, pCur, 
16f40 6e 4b 65 79 29 20 29 7b 0a 20 20 20 20 72 65 74  nKey) ){.    ret
16f41 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
16f42 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20  D; /* The table 
16f43 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 68  pCur points to h
16f44 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a  as a read lock *
16f45 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72  /.  }.  if( pCur
16f46 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
16f47 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65  _FAULT ){.    re
16f48 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b  turn pCur->skip;
16f49 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20  .  }..  /* Save 
16f4a 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66  the positions of
16f4b 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73 6f   any other curso
16f4c 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20  rs open on this 
16f4d 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74  table */.  sqlit
16f4e 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73  e3BtreeClearCurs
16f4f 6f 72 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20  or(pCur);.  if( 
16f50 0a 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d  .    SQLITE_OK!=
16f51 28 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72  (rc = saveAllCur
16f52 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e  sors(pBt, pCur->
16f53 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 29  pgnoRoot, pCur))
16f54 20 7c 7c 0a 20 20 20 20 53 51 4c 49 54 45 5f 4f   ||.    SQLITE_O
16f55 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K!=(rc = sqlite3
16f56 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72  BtreeMoveto(pCur
16f57 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70  , pKey, nKey, ap
16f58 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 29  pendBias, &loc))
16f59 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
16f5a 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 67   rc;.  }..  pPag
16f5b 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
16f5c 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
16f5d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
16f5e 69 6e 74 4b 65 79 20 7c 7c 20 6e 4b 65 79 3e 3d  intKey || nKey>=
16f5f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
16f60 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 70  Page->leaf || !p
16f61 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a  Page->intKey );.
16f62 20 20 54 52 41 43 45 28 28 22 49 4e 53 45 52 54    TRACE(("INSERT
16f63 3a 20 74 61 62 6c 65 3d 25 64 20 6e 6b 65 79 3d  : table=%d nkey=
16f64 25 6c 6c 64 20 6e 64 61 74 61 3d 25 64 20 70 61  %lld ndata=%d pa
16f65 67 65 3d 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20  ge=%d %s\n",.   
16f66 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e         pCur->pgn
16f67 6f 52 6f 6f 74 2c 20 6e 4b 65 79 2c 20 6e 44 61  oRoot, nKey, nDa
16f68 74 61 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  ta, pPage->pgno,
16f69 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 3d 3d  .          loc==
16f6a 30 20 3f 20 22 6f 76 65 72 77 72 69 74 65 22 20  0 ? "overwrite" 
16f6b 3a 20 22 6e 65 77 20 65 6e 74 72 79 22 29 29 3b  : "new entry"));
16f6c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
16f6d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 6c  ->isInit );.  al
16f6e 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 28  locateTempSpace(
16f6f 70 42 74 29 3b 0a 20 20 6e 65 77 43 65 6c 6c 20  pBt);.  newCell 
16f70 3d 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65  = pBt->pTmpSpace
16f71 3b 0a 20 20 69 66 28 20 6e 65 77 43 65 6c 6c 3d  ;.  if( newCell=
16f72 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
16f73 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 63 20 3d  TE_NOMEM;.  rc =
16f74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 61 67   fillInCell(pPag
16f75 65 2c 20 6e 65 77 43 65 6c 6c 2c 20 70 4b 65 79  e, newCell, pKey
16f76 2c 20 6e 4b 65 79 2c 20 70 44 61 74 61 2c 20 6e  , nKey, pData, n
16f77 44 61 74 61 2c 20 6e 5a 65 72 6f 2c 20 26 73 7a  Data, nZero, &sz
16f78 4e 65 77 29 3b 0a 20 20 69 66 28 20 72 63 20 29  New);.  if( rc )
16f79 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74   goto end_insert
16f7a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 4e 65  ;.  assert( szNe
16f7b 77 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70  w==cellSizePtr(p
16f7c 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c 29 20 29  Page, newCell) )
16f7d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 4e 65  ;.  assert( szNe
16f7e 77 3c 3d 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28  w<=MX_CELL_SIZE(
16f7f 70 42 74 29 20 29 3b 0a 20 20 69 64 78 20 3d 20  pBt) );.  idx = 
16f80 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
16f81 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 66 28 20  ->iPage];.  if( 
16f82 6c 6f 63 3d 3d 30 20 26 26 20 43 55 52 53 4f 52  loc==0 && CURSOR
16f83 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53  _VALID==pCur->eS
16f84 74 61 74 65 20 29 7b 0a 20 20 20 20 75 31 36 20  tate ){.    u16 
16f85 73 7a 4f 6c 64 3b 0a 20 20 20 20 61 73 73 65 72  szOld;.    asser
16f86 74 28 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43  t( idx<pPage->nC
16f87 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ell );.    rc = 
16f88 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
16f89 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
16f8a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
16f8b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
16f8c 69 6e 73 65 72 74 3b 0a 20 20 20 20 7d 0a 20 20  insert;.    }.  
16f8d 20 20 6f 6c 64 43 65 6c 6c 20 3d 20 66 69 6e 64    oldCell = find
16f8e 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29  Cell(pPage, idx)
16f8f 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  ;.    if( !pPage
16f90 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
16f91 6d 65 6d 63 70 79 28 6e 65 77 43 65 6c 6c 2c 20  memcpy(newCell, 
16f92 6f 6c 64 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 20  oldCell, 4);.   
16f93 20 7d 0a 20 20 20 20 73 7a 4f 6c 64 20 3d 20 63   }.    szOld = c
16f94 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
16f95 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20  , oldCell);.    
16f96 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70  rc = clearCell(p
16f97 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a  Page, oldCell);.
16f98 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
16f99 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20  o end_insert;.  
16f9a 20 20 72 63 20 3d 20 64 72 6f 70 43 65 6c 6c 28    rc = dropCell(
16f9b 70 50 61 67 65 2c 20 69 64 78 2c 20 73 7a 4f 6c  pPage, idx, szOl
16f9c 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  d);.    if( rc!=
16f9d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20  SQLITE_OK ) {.  
16f9e 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73      goto end_ins
16f9f 65 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ert;.    }.  }el
16fa0 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 26 20  se if( loc<0 && 
16fa1 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29  pPage->nCell>0 )
16fa2 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
16fa3 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 20  age->leaf );.   
16fa4 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61   idx = ++pCur->a
16fa5 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
16fa6 5d 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66  ];.    pCur->inf
16fa7 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20  o.nSize = 0;.   
16fa8 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
16fa9 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
16faa 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
16fab 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20  ->leaf );.  }.  
16fac 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28  rc = insertCell(
16fad 70 50 61 67 65 2c 20 69 64 78 2c 20 6e 65 77 43  pPage, idx, newC
16fae 65 6c 6c 2c 20 73 7a 4e 65 77 2c 20 30 2c 20 30  ell, szNew, 0, 0
16faf 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
16fb0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
16fb1 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c   = balance(pCur,
16fb2 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d   1);.  }..  /* M
16fb3 75 73 74 20 6d 61 6b 65 20 73 75 72 65 20 6e 4f  ust make sure nO
16fb4 76 65 72 66 6c 6f 77 20 69 73 20 72 65 73 65 74  verflow is reset
16fb5 20 74 6f 20 7a 65 72 6f 20 65 76 65 6e 20 69 66   to zero even if
16fb6 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29 0a 20   the balance(). 
16fb7 20 2a 2a 20 66 61 69 6c 73 2e 20 20 49 6e 74 65   ** fails.  Inte
16fb8 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74  rnal data struct
16fb9 75 72 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77  ure corruption w
16fba 69 6c 6c 20 72 65 73 75 6c 74 20 6f 74 68 65 72  ill result other
16fbb 77 69 73 65 2e 20 2a 2f 0a 20 20 70 43 75 72 2d  wise. */.  pCur-
16fbc 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
16fbd 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  age]->nOverflow 
16fbe 3d 20 30 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  = 0;..  if( rc==
16fbf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16fc0 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72   moveToRoot(pCur
16fc1 29 3b 0a 20 20 7d 0a 65 6e 64 5f 69 6e 73 65 72  );.  }.end_inser
16fc2 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  t:.  return rc;.
16fc3 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
16fc4 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74  the entry that t
16fc5 68 65 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69  he cursor is poi
16fc6 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 63  nting to.  The c
16fc7 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74  ursor.** is left
16fc8 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 61   pointing at a a
16fc9 72 62 69 74 72 61 72 79 20 6c 6f 63 61 74 69 6f  rbitrary locatio
16fca 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
16fcb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16fcc 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43 75  BtreeDelete(BtCu
16fcd 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d  rsor *pCur){.  M
16fce 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
16fcf 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
16fd0 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 6e 74  r->iPage];.  int
16fd1 20 69 64 78 3b 0a 20 20 75 6e 73 69 67 6e 65 64   idx;.  unsigned
16fd2 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20   char *pCell;.  
16fd3 69 6e 74 20 72 63 3b 0a 20 20 50 67 6e 6f 20 70  int rc;.  Pgno p
16fd4 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 0a 20 20  gnoChild = 0;.  
16fd5 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d  Btree *p = pCur-
16fd6 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61  >pBtree;.  BtSha
16fd7 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
16fd8 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  t;..  assert( cu
16fd9 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
16fda 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
16fdb 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20  ( pPage->isInit 
16fdc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  );.  assert( pBt
16fdd 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
16fde 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a  =TRANS_WRITE );.
16fdf 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e    assert( !pBt->
16fe0 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 69 66  readOnly );.  if
16fe1 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
16fe2 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a  CURSOR_FAULT ){.
16fe3 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d      return pCur-
16fe4 3e 73 6b 69 70 3b 0a 20 20 7d 0a 20 20 69 66 28  >skip;.  }.  if(
16fe5 20 4e 45 56 45 52 28 70 43 75 72 2d 3e 61 69 49   NEVER(pCur->aiI
16fe6 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e  dx[pCur->iPage]>
16fe7 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 20 29  =pPage->nCell) )
16fe8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
16fe9 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 54  ITE_ERROR;  /* T
16fea 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74  he cursor is not
16feb 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e 79   pointing to any
16fec 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 61  thing */.  }.  a
16fed 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46  ssert( pCur->wrF
16fee 6c 61 67 20 29 3b 0a 20 20 69 66 28 20 63 68 65  lag );.  if( che
16fef 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43 75 72  ckReadLocks(pCur
16ff0 2d 3e 70 42 74 72 65 65 2c 20 70 43 75 72 2d 3e  ->pBtree, pCur->
16ff1 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2c 20  pgnoRoot, pCur, 
16ff2 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29  pCur->info.nKey)
16ff3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16ff4 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a  QLITE_LOCKED; /*
16ff5 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20   The table pCur 
16ff6 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20  points to has a 
16ff7 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d  read lock */.  }
16ff8 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20 74  ..  /* Restore t
16ff9 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f  he current curso
16ffa 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20 6e 6f  r position (a no
16ffb 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72 73 6f  -op if the curso
16ffc 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20 20 2a  r is not in .  *
16ffd 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  * CURSOR_REQUIRE
16ffe 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e 64 20  SEEK state) and 
16fff 73 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f  save the positio
17000 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20  ns of any other 
17001 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20 6f 70  cursors .  ** op
17002 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74  en on the same t
17003 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c 6c 20  able. Then call 
17004 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
17005 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67 65 0a  e() on the page.
17006 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 65 6e    ** that the en
17007 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65 6c 65  try will be dele
17008 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f 0a 20  ted from..  */. 
17009 20 69 66 28 20 0a 20 20 20 20 28 72 63 20 3d 20   if( .    (rc = 
1700a 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  restoreCursorPos
1700b 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d 30 20  ition(pCur))!=0 
1700c 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73 61 76  ||.    (rc = sav
1700d 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c  eAllCursors(pBt,
1700e 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
1700f 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a 20 20   pCur))!=0 ||.  
17010 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50    (rc = sqlite3P
17011 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
17012 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a 20 20  >pDbPage))!=0.  
17013 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
17014 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61  ;.  }..  /* Loca
17015 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68  te the cell with
17016 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e 64 20  in its page and 
17017 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f 69 6e  leave pCell poin
17018 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20 2a 2a  ting to the.  **
17019 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65 61 72   data. The clear
1701a 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72 65 65  Cell() call free
1701b 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70  s any overflow p
1701c 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ages associated 
1701d 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 63 65  with the.  ** ce
1701e 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69 74 73  ll. The cell its
1701f 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69 6e 74  elf is still int
17020 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64 78 20  act..  */.  idx 
17021 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  = pCur->aiIdx[pC
17022 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 70 43  ur->iPage];.  pC
17023 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
17024 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 69 66  Page, idx);.  if
17025 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
17026 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20  {.    pgnoChild 
17027 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c  = get4byte(pCell
17028 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c  );.  }.  rc = cl
17029 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70  earCell(pPage, p
1702a 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72 63 20  Cell);.  if( rc 
1702b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
1702c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50  ;.  }..  if( !pP
1702d 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
1702e 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 65   /*.    ** The e
1702f 6e 74 72 79 20 77 65 20 61 72 65 20 61 62 6f 75  ntry we are abou
17030 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e  t to delete is n
17031 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69 66 20  ot a leaf so if 
17032 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 2a 2a  we do not.    **
17033 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 65   do something we
17034 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 68 6f   will leave a ho
17035 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61  le on an interna
17036 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a 20 57  l page..    ** W
17037 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c 20 74  e have to fill t
17038 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 69 6e  he hole by movin
17039 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 6f 6d  g in a cell from
1703a 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a 20 20   a leaf.  The.  
1703b 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c 20 61    ** next Cell a
1703c 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74 6f 20  fter the one to 
1703d 62 65 20 64 65 6c 65 74 65 64 20 69 73 20 67 75  be deleted is gu
1703e 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73  aranteed to exis
1703f 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 6f 20  t and.    ** to 
17040 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77 65 20  be a leaf so we 
17041 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 20 20  can use it..    
17042 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20  */.    BtCursor 
17043 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d 65 6d  leafCur;.    Mem
17044 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67 65 20  Page *pLeafPage 
17045 3d 20 30 3b 0a 0a 20 20 20 20 75 6e 73 69 67 6e  = 0;..    unsign
17046 65 64 20 63 68 61 72 20 2a 70 4e 65 78 74 3b 0a  ed char *pNext;.
17047 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b      int notUsed;
17048 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
17049 61 72 20 2a 74 65 6d 70 43 65 6c 6c 20 3d 20 30  ar *tempCell = 0
1704a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  ;.    assert( !p
1704b 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a  Page->intKey );.
1704c 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1704d 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 70 43  GetTempCursor(pC
1704e 75 72 2c 20 26 6c 65 61 66 43 75 72 29 3b 0a 20  ur, &leafCur);. 
1704f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
17050 74 72 65 65 4e 65 78 74 28 26 6c 65 61 66 43 75  treeNext(&leafCu
17051 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20  r, &notUsed);.  
17052 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
17053 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73  _OK ){.      ass
17054 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61 69 49  ert( leafCur.aiI
17055 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65  dx[leafCur.iPage
17056 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4c  ]==0 );.      pL
17057 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66 43 75  eafPage = leafCu
17058 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43 75 72  r.apPage[leafCur
17059 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 72  .iPage];.      r
1705a 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1705b 57 72 69 74 65 28 70 4c 65 61 66 50 61 67 65 2d  Write(pLeafPage-
1705c 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 7d  >pDbPage);.    }
1705d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1705e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1705f 69 6e 74 20 6c 65 61 66 43 75 72 73 6f 72 49 6e  int leafCursorIn
17060 76 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20  valid = 0;.     
17061 20 75 31 36 20 73 7a 4e 65 78 74 3b 0a 20 20 20   u16 szNext;.   
17062 20 20 20 54 52 41 43 45 28 28 22 44 45 4c 45 54     TRACE(("DELET
17063 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 6c 65  E: table=%d dele
17064 74 65 20 69 6e 74 65 72 6e 61 6c 20 66 72 6f 6d  te internal from
17065 20 25 64 20 72 65 70 6c 61 63 65 20 66 72 6f 6d   %d replace from
17066 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20   leaf %d\n",.   
17067 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f        pCur->pgno
17068 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e  Root, pPage->pgn
17069 6f 2c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67  o, pLeafPage->pg
1706a 6e 6f 29 29 3b 0a 20 20 20 20 20 20 64 72 6f 70  no));.      drop
1706b 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c  Cell(pPage, idx,
1706c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
1706d 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20  ge, pCell));.   
1706e 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e 64 43     pNext = findC
1706f 65 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c 20 30  ell(pLeafPage, 0
17070 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 78 74 20  );.      szNext 
17071 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c  = cellSizePtr(pL
17072 65 61 66 50 61 67 65 2c 20 70 4e 65 78 74 29 3b  eafPage, pNext);
17073 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 4d  .      assert( M
17074 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29  X_CELL_SIZE(pBt)
17075 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 3b 0a 20 20  >=szNext+4 );.  
17076 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70      allocateTemp
17077 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20  Space(pBt);.    
17078 20 20 74 65 6d 70 43 65 6c 6c 20 3d 20 70 42 74    tempCell = pBt
17079 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20  ->pTmpSpace;.   
1707a 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c 6c 3d     if( tempCell=
1707b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  =0 ){.        rc
1707c 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1707d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1707e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1707f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
17080 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67   insertCell(pPag
17081 65 2c 20 69 64 78 2c 20 70 4e 65 78 74 2d 34 2c  e, idx, pNext-4,
17082 20 73 7a 4e 65 78 74 2b 34 2c 20 74 65 6d 70 43   szNext+4, tempC
17083 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  ell, 0);.      }
17084 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ...      /* The 
17085 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69  "if" statement i
17086 6e 20 74 68 65 20 6e 65 78 74 20 63 6f 64 65 20  n the next code 
17087 62 6c 6f 63 6b 20 69 73 20 63 72 69 74 69 63 61  block is critica
17088 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a  l.  The.      **
17089 20 73 6c 69 67 68 74 65 73 74 20 65 72 72 6f 72   slightest error
1708a 20 69 6e 20 74 68 61 74 20 73 74 61 74 65 6d 65   in that stateme
1708b 6e 74 20 77 6f 75 6c 64 20 61 6c 6c 6f 77 20 53  nt would allow S
1708c 51 4c 69 74 65 20 74 6f 20 6f 70 65 72 61 74 65  QLite to operate
1708d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 63  .      ** correc
1708e 74 6c 79 20 6d 6f 73 74 20 6f 66 20 74 68 65 20  tly most of the 
1708f 74 69 6d 65 20 62 75 74 20 70 72 6f 64 75 63 65  time but produce
17090 20 76 65 72 79 20 72 61 72 65 20 66 61 69 6c 75   very rare failu
17091 72 65 73 2e 20 20 54 6f 0a 20 20 20 20 20 20 2a  res.  To.      *
17092 2a 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20  * guard against 
17093 74 68 69 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  this, the follow
17094 69 6e 67 20 6d 61 63 72 6f 73 20 68 65 6c 70 20  ing macros help 
17095 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 0a 20  to verify that. 
17096 20 20 20 20 20 2a 2a 20 74 68 65 20 22 69 66 22       ** the "if"
17097 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 77 65   statement is we
17098 6c 6c 20 74 65 73 74 65 64 2e 0a 20 20 20 20 20  ll tested..     
17099 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
1709a 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  se( pPage->nOver
1709b 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65  flow==0 && pPage
1709c 2d 3e 6e 46 72 65 65 3c 70 42 74 2d 3e 75 73 61  ->nFree<pBt->usa
1709d 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 20  bleSize*2/3 .   
1709e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26                &&
1709f 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65   pLeafPage->nFre
170a0 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74  e+2+szNext > pBt
170a1 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33  ->usableSize*2/3
170a2 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
170a3 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  se( pPage->nOver
170a4 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65  flow==0 && pPage
170a5 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 73  ->nFree==pBt->us
170a6 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20  ableSize*2/3 .  
170a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
170a8 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72  & pLeafPage->nFr
170a9 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42  ee+2+szNext > pB
170aa 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f  t->usableSize*2/
170ab 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  3 );.      testc
170ac 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ase( pPage->nOve
170ad 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67  rflow==0 && pPag
170ae 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75  e->nFree==pBt->u
170af 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20  sableSize*2/3+1 
170b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
170b1 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e    && pLeafPage->
170b2 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e  nFree+2+szNext >
170b3 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
170b4 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65  *2/3 );.      te
170b5 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e  stcase( pPage->n
170b6 4f 76 65 72 66 6c 6f 77 3e 30 20 26 26 20 70 50  Overflow>0 && pP
170b7 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 70 42 74 2d  age->nFree<=pBt-
170b8 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 0a  >usableSize*2/3.
170b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
170ba 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e   && pLeafPage->n
170bb 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20  Free+2+szNext > 
170bc 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a  pBt->usableSize*
170bd 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73  2/3 );.      tes
170be 74 63 61 73 65 28 20 28 70 50 61 67 65 2d 3e 6e  tcase( (pPage->n
170bf 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 70  Overflow>0 || (p
170c0 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 42  Page->nFree > pB
170c1 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f  t->usableSize*2/
170c2 33 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  3)).            
170c3 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 67       && pLeafPag
170c4 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78  e->nFree+2+szNex
170c5 74 20 3d 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  t == pBt->usable
170c6 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a 20 20  Size*2/3 );...  
170c7 20 20 20 20 69 66 28 20 28 70 50 61 67 65 2d 3e      if( (pPage->
170c8 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28  nOverflow>0 || (
170c9 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70  pPage->nFree > p
170ca 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32  Bt->usableSize*2
170cb 2f 33 29 29 20 26 26 0a 20 20 20 20 20 20 20 20  /3)) &&.        
170cc 20 20 28 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46    (pLeafPage->nF
170cd 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70  ree+2+szNext > p
170ce 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32  Bt->usableSize*2
170cf 2f 33 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  /3).      ){.   
170d0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
170d1 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 69 66 20  nch is taken if 
170d2 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64  the internal nod
170d3 65 20 69 73 20 6e 6f 77 20 65 69 74 68 65 72 20  e is now either 
170d4 6f 76 65 72 66 6c 6f 77 69 6e 67 0a 20 20 20 20  overflowing.    
170d5 20 20 20 20 2a 2a 20 6f 72 20 75 6e 64 65 72 66      ** or underf
170d6 75 6c 6c 20 61 6e 64 20 74 68 65 20 6c 65 61 66  ull and the leaf
170d7 20 6e 6f 64 65 20 77 69 6c 6c 20 62 65 20 75 6e   node will be un
170d8 64 65 72 66 75 6c 6c 20 61 66 74 65 72 20 74 68  derfull after th
170d9 65 20 6a 75 73 74 20 63 65 6c 6c 20 0a 20 20 20  e just cell .   
170da 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 74       ** copied t
170db 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e  o the internal n
170dc 6f 64 65 20 69 73 20 64 65 6c 65 74 65 64 20 66  ode is deleted f
170dd 72 6f 6d 20 69 74 2e 20 54 68 69 73 20 69 73 20  rom it. This is 
170de 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20  a special.      
170df 20 20 2a 2a 20 63 61 73 65 20 62 65 63 61 75 73    ** case becaus
170e0 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 62 61  e the call to ba
170e1 6c 61 6e 63 65 28 29 20 74 6f 20 63 6f 72 72 65  lance() to corre
170e2 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ct the internal 
170e3 6e 6f 64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  node.        ** 
170e4 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 74  may change the t
170e5 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e  ree structure an
170e6 64 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65  d invalidate the
170e7 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 20 20 20   contents of.   
170e8 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 61 66       ** the leaf
170e9 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64  Cur.apPage[] and
170ea 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 5d   leafCur.aiIdx[]
170eb 20 61 72 72 61 79 73 2c 20 77 68 69 63 68 20 77   arrays, which w
170ec 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a  ill be.        *
170ed 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 62 61  * used by the ba
170ee 6c 61 6e 63 65 28 29 20 72 65 71 75 69 72 65 64  lance() required
170ef 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 65 20   to correct the 
170f0 75 6e 64 65 72 66 75 6c 6c 20 6c 65 61 66 0a 20  underfull leaf. 
170f1 20 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65 2e 0a         ** node..
170f2 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
170f3 20 20 20 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c     ** The formul
170f4 61 20 75 73 65 64 20 69 6e 20 74 68 65 20 65 78  a used in the ex
170f5 70 72 65 73 73 69 6f 6e 20 61 62 6f 76 65 20 61  pression above a
170f6 72 65 20 62 61 73 65 64 20 6f 6e 20 66 61 63 65  re based on face
170f7 74 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a  ts of.        **
170f8 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65   the SQLite file
170f9 2d 66 6f 72 6d 61 74 20 74 68 61 74 20 64 6f 20  -format that do 
170fa 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 72 20  not change over 
170fb 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  time..        */
170fc 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
170fd 65 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3d  e( pPage->nFree=
170fe 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  =pBt->usableSize
170ff 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 20 20 20 20  *2/3+1 );.      
17100 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 61    testcase( pLea
17101 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73  fPage->nFree+2+s
17102 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e 75 73 61 62  zNext==pBt->usab
17103 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a  leSize*2/3+1 );.
17104 20 20 20 20 20 20 20 20 6c 65 61 66 43 75 72 73          leafCurs
17105 6f 72 49 6e 76 61 6c 69 64 20 3d 20 31 3b 0a 20  orInvalid = 1;. 
17106 20 20 20 20 20 7d 20 20 20 20 20 20 20 20 0a 0a       }        ..
17107 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
17108 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17109 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
1710a 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
1710b 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
1710c 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 75  e) );.        pu
1710d 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66  t4byte(findOverf
1710e 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  lowCell(pPage, i
1710f 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b  dx), pgnoChild);
17110 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c  .        VVA_ONL
17111 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68  Y( pCur->pagesSh
17112 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a 20 20  uffled = 0 );.  
17113 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e        rc = balan
17114 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 20  ce(pCur, 0);.   
17115 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
17116 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
17117 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61 6c   leafCursorInval
17118 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  id ){.        /*
17119 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64 65 20 69   The leaf-node i
1711a 73 20 6e 6f 77 20 75 6e 64 65 72 66 75 6c 6c 20  s now underfull 
1711b 61 6e 64 20 73 6f 20 74 68 65 20 74 72 65 65 20  and so the tree 
1711c 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20 20  needs to be .   
1711d 20 20 20 20 20 2a 2a 20 72 65 62 61 6c 61 6e 63       ** rebalanc
1711e 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 68 65  ed. However, the
1711f 20 62 61 6c 61 6e 63 65 28 29 20 6f 70 65 72 61   balance() opera
17120 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e 74 65  tion on the inte
17121 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20  rnal.        ** 
17122 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61 79 20 68  node above may h
17123 61 76 65 20 6d 6f 64 69 66 69 65 64 20 74 68 65  ave modified the
17124 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 68   structure of th
17125 65 20 42 2d 54 72 65 65 20 61 6e 64 0a 20 20 20  e B-Tree and.   
17126 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 65 20 63       ** so the c
17127 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20  urrent contents 
17128 6f 66 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67  of leafCur.apPag
17129 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43 75 72 2e  e[] and leafCur.
1712a 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20  aiIdx[].        
1712b 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 72  ** may not be tr
1712c 75 73 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a  usted..        *
1712d 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 20  *.        ** It 
1712e 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
1712f 74 6f 20 63 6f 70 79 20 74 68 65 20 61 6e 63 65  to copy the ance
17130 73 74 72 79 20 66 72 6f 6d 20 70 43 75 72 2c 20  stry from pCur, 
17131 61 73 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20  as the same.    
17132 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 28 29      ** balance()
17133 20 63 61 6c 6c 20 68 61 73 20 69 6e 76 61 6c 69   call has invali
17134 64 61 74 65 64 20 74 68 65 20 70 43 75 72 2d 3e  dated the pCur->
17135 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61 69 49  apPage[] and aiI
17136 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 2a 2a 20  dx[].        ** 
17137 61 72 72 61 79 73 2e 20 0a 20 20 20 20 20 20 20  arrays. .       
17138 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54   **.        ** T
17139 68 65 20 63 61 6c 6c 20 74 6f 20 73 61 76 65 43  he call to saveC
1713a 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20  ursorPosition() 
1713b 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61 6c 6c 79  below internally
1713c 20 73 61 76 65 73 20 74 68 65 20 0a 20 20 20 20   saves the .    
1713d 20 20 20 20 2a 2a 20 6b 65 79 20 74 68 61 74 20      ** key that 
1713e 6c 65 61 66 43 75 72 20 69 73 20 63 75 72 72 65  leafCur is curre
1713f 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ntly pointing to
17140 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65  . Currently, the
17141 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 72  re.        ** ar
17142 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f 66 20  e two copies of 
17143 74 68 61 74 20 6b 65 79 20 69 6e 20 74 68 65 20  that key in the 
17144 74 72 65 65 20 2d 20 6f 6e 65 20 68 65 72 65 20  tree - one here 
17145 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20 20 20 20  on the leaf.    
17146 20 20 20 20 2a 2a 20 70 61 67 65 20 61 6e 64 20      ** page and 
17147 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69 6e 74 65  one on some inte
17148 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20 74 68 65  rnal node in the
17149 20 74 72 65 65 2e 20 54 68 65 20 63 6f 70 79 20   tree. The copy 
1714a 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  on.        ** th
1714b 65 20 6c 65 61 66 20 6e 6f 64 65 20 69 73 20 61  e leaf node is a
1714c 6c 77 61 79 73 20 74 68 65 20 6e 65 78 74 20 6b  lways the next k
1714d 65 79 20 69 6e 20 74 72 65 65 2d 6f 72 64 65 72  ey in tree-order
1714e 20 61 66 74 65 72 20 74 68 65 20 0a 20 20 20 20   after the .    
1714f 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 6e 20 74      ** copy on t
17150 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  he internal node
17151 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c 6c 20 74  . So, the call t
17152 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  o sqlite3BtreeNe
17153 78 74 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20  xt().        ** 
17154 63 61 6c 6c 73 20 72 65 73 74 6f 72 65 43 75 72  calls restoreCur
17155 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 74 6f  sorPosition() to
17156 20 70 6f 69 6e 74 20 74 68 65 20 63 75 72 73 6f   point the curso
17157 72 20 74 6f 20 74 68 65 20 63 6f 70 79 0a 20 20  r to the copy.  
17158 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20        ** stored 
17159 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  on the internal 
1715a 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64 76 61 6e  node, then advan
1715b 63 65 73 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ces to the next 
1715c 65 6e 74 72 79 2c 0a 20 20 20 20 20 20 20 20 2a  entry,.        *
1715d 2a 20 77 68 69 63 68 20 68 61 70 70 65 6e 73 20  * which happens 
1715e 74 6f 20 62 65 20 74 68 65 20 63 6f 70 79 20 6f  to be the copy o
1715f 66 20 74 68 65 20 6b 65 79 20 6f 6e 20 74 68 65  f the key on the
17160 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 0a   internal node..
17161 20 20 20 20 20 20 20 20 2a 2a 20 4e 65 74 20 65          ** Net e
17162 66 66 65 63 74 3a 20 6c 65 61 66 43 75 72 20 69  ffect: leafCur i
17163 73 20 70 6f 69 6e 74 69 6e 67 20 62 61 63 6b 20  s pointing back 
17164 74 6f 20 74 68 65 20 64 75 70 6c 69 63 61 74 65  to the duplicate
17165 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a   cell.        **
17166 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62   that needs to b
17167 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e 64 20 74  e removed, and t
17168 68 65 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67  he leafCur.apPag
17169 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20 20 20 20  e[] and.        
1716a 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78  ** leafCur.aiIdx
1716b 5b 5d 20 61 72 72 61 79 73 20 61 72 65 20 63 6f  [] arrays are co
1716c 72 72 65 63 74 2e 0a 20 20 20 20 20 20 20 20 2a  rrect..        *
1716d 2f 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e  /.        VVA_ON
1716e 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66 50 67 6e  LY( Pgno leafPgn
1716f 6f 20 3d 20 70 4c 65 61 66 50 61 67 65 2d 3e 70  o = pLeafPage->p
17170 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20 20 72  gno );.        r
17171 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f  c = saveCursorPo
17172 73 69 74 69 6f 6e 28 26 6c 65 61 66 43 75 72 29  sition(&leafCur)
17173 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
17174 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
17175 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
17176 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 26  lite3BtreeNext(&
17177 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74 55 73 65  leafCur, &notUse
17178 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  d);.        }.  
17179 20 20 20 20 20 20 70 4c 65 61 66 50 61 67 65 20        pLeafPage 
1717a 3d 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67 65  = leafCur.apPage
1717b 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3b  [leafCur.iPage];
1717c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1717d 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 6e 6f   pLeafPage->pgno
1717e 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b 0a 20 20  ==leafPgno );.  
1717f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65        assert( le
17180 61 66 43 75 72 2e 61 69 49 64 78 5b 6c 65 61 66  afCur.aiIdx[leaf
17181 43 75 72 2e 69 50 61 67 65 5d 3d 3d 30 20 29 3b  Cur.iPage]==0 );
17182 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
17183 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72  if( SQLITE_OK==r
17184 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c 49  c.       && SQLI
17185 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
17186 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
17187 4c 65 61 66 50 61 67 65 2d 3e 70 44 62 50 61 67  LeafPage->pDbPag
17188 65 29 29 20 0a 20 20 20 20 20 20 29 7b 0a 20 20  e)) .      ){.  
17189 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70        dropCell(p
1718a 4c 65 61 66 50 61 67 65 2c 20 30 2c 20 73 7a 4e  LeafPage, 0, szN
1718b 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 56 56  ext);.        VV
1718c 41 5f 4f 4e 4c 59 28 20 6c 65 61 66 43 75 72 2e  A_ONLY( leafCur.
1718d 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20  pagesShuffled = 
1718e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  0 );.        rc 
1718f 3d 20 62 61 6c 61 6e 63 65 28 26 6c 65 61 66 43  = balance(&leafC
17190 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ur, 0);.        
17191 61 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 73  assert( leafCurs
17192 6f 72 49 6e 76 61 6c 69 64 20 7c 7c 20 21 6c 65  orInvalid || !le
17193 61 66 43 75 72 2e 70 61 67 65 73 53 68 75 66 66  afCur.pagesShuff
17194 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20  led.            
17195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17196 20 20 20 20 20 20 20 7c 7c 20 21 70 43 75 72 2d         || !pCur-
17197 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 29  >pagesShuffled )
17198 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
17199 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1719a 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73 6f  ReleaseTempCurso
1719b 72 28 26 6c 65 61 66 43 75 72 29 3b 0a 20 20 7d  r(&leafCur);.  }
1719c 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43 45 28  else{.    TRACE(
1719d 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c 65 3d  ("DELETE: table=
1719e 25 64 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 6c  %d delete from l
1719f 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20  eaf %d\n",.     
171a0 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74    pCur->pgnoRoot
171a1 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b  , pPage->pgno));
171a2 0a 20 20 20 20 72 63 20 3d 20 64 72 6f 70 43 65  .    rc = dropCe
171a3 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 63  ll(pPage, idx, c
171a4 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
171a5 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 20 69  , pCell));.    i
171a6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
171a7 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62   ){.      rc = b
171a8 61 6c 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b  alance(pCur, 0);
171a9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
171aa 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
171ab 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74  {.    moveToRoot
171ac 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72 65  (pCur);.  }.  re
171ad 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
171ae 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 42  * Create a new B
171af 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 72 69  Tree table.  Wri
171b0 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c 65  te into *piTable
171b1 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d   the page.** num
171b2 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74  ber for the root
171b3 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77   page of the new
171b4 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
171b5 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 20 69  e type of type i
171b6 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20  s determined by 
171b7 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
171b8 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a 2a  ter.  Only the.*
171b9 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75  * following valu
171ba 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 65 20  es of flags are 
171bb 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
171bc 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20  .  Other values 
171bd 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69 67  for.** flags mig
171be 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a 0a  ht not work:.**.
171bf 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54  **     BTREE_INT
171c0 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 44 41  KEY|BTREE_LEAFDA
171c1 54 41 20 20 20 20 20 55 73 65 64 20 66 6f 72 20  TA     Used for 
171c2 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 68 20  SQL tables with 
171c3 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20 20  rowid keys.**   
171c4 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41    BTREE_ZERODATA
171c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
171c6 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c 20 69    Used for SQL i
171c7 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69 63  ndices.*/.static
171c8 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 74 65   int btreeCreate
171c9 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20  Table(Btree *p, 
171ca 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69 6e  int *piTable, in
171cb 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 53 68  t flags){.  BtSh
171cc 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
171cd 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  Bt;.  MemPage *p
171ce 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e  Root;.  Pgno pgn
171cf 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 3b  oRoot;.  int rc;
171d0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
171d1 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
171d2 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72  ex(p) );.  asser
171d3 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  t( pBt->inTransa
171d4 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
171d5 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TE );.  assert( 
171d6 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29  !pBt->readOnly )
171d7 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
171d8 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
171d9 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65  .  rc = allocate
171da 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26  BtreePage(pBt, &
171db 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74  pRoot, &pgnoRoot
171dc 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  , 1, 0);.  if( r
171dd 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
171de 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20  rc;.  }.#else.  
171df 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
171e0 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20  uum ){.    Pgno 
171e1 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20 2f  pgnoMove;      /
171e2 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 68 65  * Move a page he
171e3 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20  re to make room 
171e4 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67  for the root-pag
171e5 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65  e */.    MemPage
171e6 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a 20   *pPageMove; /* 
171e7 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65  The page to move
171e8 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20   to. */..    /* 
171e9 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 74  Creating a new t
171ea 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 62 6c  able may probabl
171eb 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67  y require moving
171ec 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 61 74   an existing dat
171ed 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f 20  abase.    ** to 
171ee 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68  make room for th
171ef 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 6f 6f  e new tables roo
171f0 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 65 20  t page. In case 
171f1 74 68 69 73 20 70 61 67 65 20 74 75 72 6e 73 0a  this page turns.
171f2 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65      ** out to be
171f3 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
171f4 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f 76  e, delete all ov
171f5 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70 20  erflow page-map 
171f6 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 68 65  caches.    ** he
171f7 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 73 6f  ld by open curso
171f8 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  rs..    */.    i
171f9 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72  nvalidateAllOver
171fa 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a  flowCache(pBt);.
171fb 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65  .    /* Read the
171fc 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b 33   value of meta[3
171fd 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  ] from the datab
171fe 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ase to determine
171ff 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 20 2a   where the.    *
17200 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  * root page of t
17201 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 68 6f  he new table sho
17202 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d 20  uld go. meta[3] 
17203 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 72  is the largest r
17204 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20  oot-page.    ** 
17205 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 2c 20  created so far, 
17206 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d  so the new root-
17207 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b 33 5d  page is (meta[3]
17208 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  +1)..    */.    
17209 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1720a 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c 20 26  eGetMeta(p, 4, &
1720b 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69  pgnoRoot);.    i
1720c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1720d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1720e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70   rc;.    }.    p
1720f 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20 20  gnoRoot++;..    
17210 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f 74 2d  /* The new root-
17211 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20  page may not be 
17212 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 70  allocated on a p
17213 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c  ointer-map page,
17214 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 50   or the.    ** P
17215 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 67 65  ENDING_BYTE page
17216 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69  ..    */.    whi
17217 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 54  le( pgnoRoot==PT
17218 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c  RMAP_PAGENO(pBt,
17219 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20 20   pgnoRoot) ||.  
1721a 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d 3d        pgnoRoot==
1721b 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1721c 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  E(pBt) ){.      
1721d 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20 20  pgnoRoot++;.    
1721e 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67  }.    assert( pg
1721f 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20 20  noRoot>=3 );..  
17220 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20    /* Allocate a 
17221 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 20 74  page. The page t
17222 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 72 65  hat currently re
17223 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52 6f 6f  sides at pgnoRoo
17224 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65  t will.    ** be
17225 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 61 6c   moved to the al
17226 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28 75 6e  located page (un
17227 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 61 74  less the allocat
17228 65 64 20 70 61 67 65 20 68 61 70 70 65 6e 73 0a  ed page happens.
17229 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 64 65      ** to reside
1722a 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a 20   at pgnoRoot).. 
1722b 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 61     */.    rc = a
1722c 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65  llocateBtreePage
1722d 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f 76 65  (pBt, &pPageMove
1722e 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67 6e  , &pgnoMove, pgn
1722f 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20 69  oRoot, 1);.    i
17230 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
17231 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
17232 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   rc;.    }..    
17233 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70 67  if( pgnoMove!=pg
17234 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20  noRoot ){.      
17235 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74  /* pgnoRoot is t
17236 68 65 20 70 61 67 65 20 74 68 61 74 20 77 69 6c  he page that wil
17237 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68  l be used for th
17238 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a 20  e root-page of. 
17239 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20       ** the new 
1723a 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 20  table (assuming 
1723b 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e 6f 74  an error did not
1723c 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77 65 20   occur). But we 
1723d 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 6c  were.      ** al
1723e 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76 65  located pgnoMove
1723f 2e 20 49 66 20 72 65 71 75 69 72 65 64 20 28 69  . If required (i
17240 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20 6e 6f  .e. if it was no
17241 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20  t allocated.    
17242 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 69 6e    ** by extendin
17243 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74 68 65  g the file), the
17244 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 61 74   current page at
17245 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d 6f   position pgnoMo
17246 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61  ve.      ** is a
17247 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65 64  lready journaled
17248 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
17249 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20   u8 eType;.     
1724a 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b 0a   Pgno iPtrPage;.
1724b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
1724c 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a 0a  ge(pPageMove);..
1724d 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68        /* Move th
1724e 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c 79  e page currently
1724f 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f 20   at pgnoRoot to 
17250 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20  pgnoMove. */.   
17251 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
17252 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
17253 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f   pgnoRoot, &pRoo
17254 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  t, 0);.      if(
17255 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
17256 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
17257 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
17258 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65     rc = ptrmapGe
17259 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c  t(pBt, pgnoRoot,
1725a 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61   &eType, &iPtrPa
1725b 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
1725c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
1725d 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f  eType==PTRMAP_RO
1725e 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70 65 3d  OTPAGE || eType=
1725f 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45  =PTRMAP_FREEPAGE
17260 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65   ){.        rele
17261 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a  asePage(pRoot);.
17262 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
17263 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
17264 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d   assert( eType!=
17265 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20  PTRMAP_ROOTPAGE 
17266 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
17267 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 46   eType!=PTRMAP_F
17268 52 45 45 50 41 47 45 20 29 3b 0a 20 20 20 20 20  REEPAGE );.     
17269 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1726a 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70  erWrite(pRoot->p
1726b 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  DbPage);.      i
1726c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1726d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65   ){.        rele
1726e 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a  asePage(pRoot);.
1726f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
17270 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
17271 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61   rc = relocatePa
17272 67 65 28 70 42 74 2c 20 70 52 6f 6f 74 2c 20 65  ge(pBt, pRoot, e
17273 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20  Type, iPtrPage, 
17274 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b 0a 20 20  pgnoMove, 0);.  
17275 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
17276 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 20 20 2f  pRoot);..      /
17277 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67  * Obtain the pag
17278 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 2a 2f  e at pgnoRoot */
17279 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1727a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1727b 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1727c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
1727d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
1727e 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 52  tPage(pBt, pgnoR
1727f 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b  oot, &pRoot, 0);
17280 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
17281 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17282 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
17283 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
17284 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
17285 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61  ite(pRoot->pDbPa
17286 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
17287 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
17288 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
17289 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20  age(pRoot);.    
1728a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1728b 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
1728c 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d 20  {.      pRoot = 
1728d 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20 7d  pPageMove;.    }
1728e 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65   ..    /* Update
1728f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
17290 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20 77   and meta-data w
17291 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f 74  ith the new root
17292 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a 2f  -page number. */
17293 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70  .    rc = ptrmap
17294 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f  Put(pBt, pgnoRoo
17295 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  t, PTRMAP_ROOTPA
17296 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  GE, 0);.    if( 
17297 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  rc ){.      rele
17298 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a  asePage(pRoot);.
17299 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1729a 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1729b 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61  sqlite3BtreeUpda
1729c 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67 6e  teMeta(p, 4, pgn
1729d 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28 20  oRoot);.    if( 
1729e 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  rc ){.      rele
1729f 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a  asePage(pRoot);.
172a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
172a1 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 7b  .    }..  }else{
172a2 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  .    rc = alloca
172a3 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
172a4 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f   &pRoot, &pgnoRo
172a5 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69  ot, 1, 0);.    i
172a6 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
172a7 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  c;.  }.#endif.  
172a8 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
172a9 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
172aa 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20  pRoot->pDbPage) 
172ab 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 52  );.  zeroPage(pR
172ac 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54 46  oot, flags | PTF
172ad 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74 65  _LEAF);.  sqlite
172ae 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f 6f  3PagerUnref(pRoo
172af 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 2a  t->pDbPage);.  *
172b0 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29 70  piTable = (int)p
172b1 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 72  gnoRoot;.  retur
172b2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53  n SQLITE_OK;.}.S
172b3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
172b4 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72  t sqlite3BtreeCr
172b5 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20  eateTable(Btree 
172b6 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65  *p, int *piTable
172b7 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
172b8 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
172b9 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
172ba 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70    p->pBt->db = p
172bb 2d 3e 64 62 3b 0a 20 20 72 63 20 3d 20 62 74 72  ->db;.  rc = btr
172bc 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c  eeCreateTable(p,
172bd 20 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29   piTable, flags)
172be 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
172bf 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
172c0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
172c1 45 72 61 73 65 20 74 68 65 20 67 69 76 65 6e 20  Erase the given 
172c2 64 61 74 61 62 61 73 65 20 70 61 67 65 20 61 6e  database page an
172c3 64 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 72  d all its childr
172c4 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74  en.  Return.** t
172c5 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 66  he page to the f
172c6 72 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74  reelist..*/.stat
172c7 69 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74 61  ic int clearData
172c8 62 61 73 65 50 61 67 65 28 0a 20 20 42 74 53 68  basePage(.  BtSh
172c9 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20  ared *pBt,      
172ca 20 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65       /* The BTre
172cb 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
172cc 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50  the table */.  P
172cd 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20  gno pgno,       
172ce 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
172cf 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a  ber to clear */.
172d0 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c    int freePageFl
172d1 61 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c  ag,     /* Deall
172d2 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74 72  ocate page if tr
172d3 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  ue */.  int *pnC
172d4 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 61  hange.){.  MemPa
172d5 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20  ge *pPage = 0;. 
172d6 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67   int rc;.  unsig
172d7 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b  ned char *pCell;
172d8 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73  .  int i;..  ass
172d9 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
172da 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
172db 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67 6e  ex) );.  if( pgn
172dc 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  o>pagerPagecount
172dd 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74  (pBt) ){.    ret
172de 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
172df 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20  PT_BKPT;.  }..  
172e0 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50  rc = getAndInitP
172e1 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 26  age(pBt, pgno, &
172e2 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  pPage);.  if( rc
172e3 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74   ) goto cleardat
172e4 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20  abasepage_out;. 
172e5 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67   for(i=0; i<pPag
172e6 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a  e->nCell; i++){.
172e7 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64      pCell = find
172e8 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a  Cell(pPage, i);.
172e9 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e      if( !pPage->
172ea 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63  leaf ){.      rc
172eb 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65   = clearDatabase
172ec 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 79  Page(pBt, get4by
172ed 74 65 28 70 43 65 6c 6c 29 2c 20 31 2c 20 70 6e  te(pCell), 1, pn
172ee 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 69  Change);.      i
172ef 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65  f( rc ) goto cle
172f0 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f  ardatabasepage_o
172f1 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  ut;.    }.    rc
172f2 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61   = clearCell(pPa
172f3 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20  ge, pCell);.    
172f4 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c  if( rc ) goto cl
172f5 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f  eardatabasepage_
172f6 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  out;.  }.  if( !
172f7 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
172f8 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61 74     rc = clearDat
172f9 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20 67  abasePage(pBt, g
172fa 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
172fb 61 44 61 74 61 5b 38 5d 29 2c 20 31 2c 20 70 6e  aData[8]), 1, pn
172fc 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 69 66 28  Change);.    if(
172fd 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72   rc ) goto clear
172fe 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74  databasepage_out
172ff 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 6e  ;.  }else if( pn
17300 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20 61 73  Change ){.    as
17301 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74  sert( pPage->int
17302 4b 65 79 20 29 3b 0a 20 20 20 20 2a 70 6e 43 68  Key );.    *pnCh
17303 61 6e 67 65 20 2b 3d 20 70 50 61 67 65 2d 3e 6e  ange += pPage->n
17304 43 65 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Cell;.  }.  if( 
17305 66 72 65 65 50 61 67 65 46 6c 61 67 20 29 7b 0a  freePageFlag ){.
17306 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67      rc = freePag
17307 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73  e(pPage);.  }els
17308 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69  e if( (rc = sqli
17309 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
1730a 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 3d 3d  age->pDbPage))==
1730b 30 20 29 7b 0a 20 20 20 20 7a 65 72 6f 50 61 67  0 ){.    zeroPag
1730c 65 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e  e(pPage, pPage->
1730d 61 44 61 74 61 5b 30 5d 20 7c 20 50 54 46 5f 4c  aData[0] | PTF_L
1730e 45 41 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61 72  EAF);.  }..clear
1730f 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74  databasepage_out
17310 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28  :.  releasePage(
17311 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e  pPage);.  return
17312 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65   rc;.}../*.** De
17313 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61  lete all informa
17314 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73 69 6e 67  tion from a sing
17315 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  le table in the 
17316 64 61 74 61 62 61 73 65 2e 20 20 69 54 61 62 6c  database.  iTabl
17317 65 20 69 73 0a 2a 2a 20 74 68 65 20 70 61 67 65  e is.** the page
17318 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72   number of the r
17319 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65  oot of the table
1731a 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  .  After this ro
1731b 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2c 0a 2a  utine returns,.*
1731c 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  * the root page 
1731d 69 73 20 65 6d 70 74 79 2c 20 62 75 74 20 73 74  is empty, but st
1731e 69 6c 6c 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a  ill exists..**.*
1731f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
17320 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 51  ill fail with SQ
17321 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74  LITE_LOCKED if t
17322 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 65  here are any ope
17323 6e 0a 2a 2a 20 72 65 61 64 20 63 75 72 73 6f 72  n.** read cursor
17324 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 20  s on the table. 
17325 20 4f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73   Open write curs
17326 6f 72 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f  ors are moved to
17327 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66 20   the.** root of 
17328 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  the table..**.**
17329 20 49 66 20 70 6e 43 68 61 6e 67 65 20 69 73 20   If pnChange is 
1732a 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  not NULL, then t
1732b 61 62 6c 65 20 69 54 61 62 6c 65 20 6d 75 73 74  able iTable must
1732c 20 62 65 20 61 6e 20 69 6e 74 6b 65 79 20 74 61   be an intkey ta
1732d 62 6c 65 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65  ble. The.** inte
1732e 67 65 72 20 76 61 6c 75 65 20 70 6f 69 6e 74 65  ger value pointe
1732f 64 20 74 6f 20 62 79 20 70 6e 43 68 61 6e 67 65  d to by pnChange
17330 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
17331 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  by the number of
17332 0a 2a 2a 20 65 6e 74 72 69 65 73 20 69 6e 20 74  .** entries in t
17333 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  he table..*/.SQL
17334 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
17335 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61  sqlite3BtreeClea
17336 72 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c  rTable(Btree *p,
17337 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74   int iTable, int
17338 20 2a 70 6e 43 68 61 6e 67 65 29 7b 0a 20 20 69   *pnChange){.  i
17339 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65  nt rc;.  BtShare
1733a 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
1733b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1733c 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e  nter(p);.  pBt->
1733d 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73  db = p->db;.  as
1733e 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73  sert( p->inTrans
1733f 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
17340 0a 20 20 69 66 28 20 28 72 63 20 3d 20 63 68 65  .  if( (rc = che
17341 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 2c 20 69  ckReadLocks(p, i
17342 54 61 62 6c 65 2c 20 30 2c 20 31 29 29 21 3d 53  Table, 0, 1))!=S
17343 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17344 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f  /* nothing to do
17345 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   */.  }else if( 
17346 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
17347 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28   saveAllCursors(
17348 70 42 74 2c 20 69 54 61 62 6c 65 2c 20 30 29 29  pBt, iTable, 0))
17349 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 6f 74 68 69   ){.    /* nothi
1734a 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65  ng to do */.  }e
1734b 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c  lse{.    rc = cl
1734c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28  earDatabasePage(
1734d 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c  pBt, (Pgno)iTabl
1734e 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65 29 3b  e, 0, pnChange);
1734f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
17350 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
17351 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
17352 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69 6e 66  ** Erase all inf
17353 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 74 61  ormation in a ta
17354 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68 65 20  ble and add the 
17355 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c  root of the tabl
17356 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72 65 65  e to.** the free
17357 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c 20 74  list.  Except, t
17358 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 70  he root of the p
17359 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65 20 28  rinciple table (
1735a 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61  the one on.** pa
1735b 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 20 61  ge 1) is never a
1735c 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65  dded to the free
1735d 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  list..**.** This
1735e 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61   routine will fa
1735f 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c  il with SQLITE_L
17360 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61  OCKED if there a
17361 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63  re any open.** c
17362 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74 61  ursors on the ta
17363 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55  ble..**.** If AU
17364 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62  TOVACUUM is enab
17365 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65  led and the page
17366 20 61 74 20 69 54 61 62 6c 65 20 69 73 20 6e 6f   at iTable is no
17367 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 72 6f  t the last.** ro
17368 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64  ot page in the d
17369 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
1736a 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74  en the last root
1736b 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74 68 65   page .** in the
1736c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1736d 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65  s moved into the
1736e 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f   slot formerly o
1736f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20 69 54  ccupied by.** iT
17370 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20 6c 61  able and that la
17371 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79  st slot formerly
17372 20 6f 63 63 75 70 69 65 64 20 62 79 20 74 68 65   occupied by the
17373 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 0a   last root page.
17374 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74  ** is added to t
17375 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e 73 74  he freelist inst
17376 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e 20 20  ead of iTable.  
17377 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61 6c 6c  In this say, all
17378 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 61  .** root pages a
17379 72 65 20 6b 65 70 74 20 61 74 20 74 68 65 20 62  re kept at the b
1737a 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
1737b 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77  database file, w
1737c 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63 65 73  hich.** is neces
1737d 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56 41 43  sary for AUTOVAC
1737e 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69 67 68  UUM to work righ
1737f 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69 73 20  t.  *piMoved is 
17380 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70  set to the .** p
17381 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61 74 20  age number that 
17382 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c  used to be the l
17383 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 69 6e  ast root page in
17384 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f 72 65   the file before
17385 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20 20 49  .** the move.  I
17386 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73 20 6d  f no page gets m
17387 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64 20 69  oved, *piMoved i
17388 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 54  s set to 0..** T
17389 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67  he last root pag
1738a 65 20 69 73 20 72 65 63 6f 72 64 65 64 20 69 6e  e is recorded in
1738b 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74 68 65   meta[3] and the
1738c 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d 65 74   value of.** met
1738d 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65 64 20  a[3] is updated 
1738e 62 79 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  by this procedur
1738f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
17390 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28   btreeDropTable(
17391 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69  Btree *p, Pgno i
17392 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f  Table, int *piMo
17393 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ved){.  int rc;.
17394 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
17395 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64   = 0;.  BtShared
17396 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
17397 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17398 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
17399 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74  x(p) );.  assert
1739a 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
1739b 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a 20 20  ANS_WRITE );..  
1739c 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c  /* It is illegal
1739d 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 6c 65   to drop a table
1739e 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 73 20   if any cursors 
1739f 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 0a  are open on the.
173a0 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54    ** database. T
173a1 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69  his is because i
173a2 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f  n auto-vacuum mo
173a3 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 20 6d  de the backend m
173a4 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20  ay.  ** need to 
173a5 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 6f 6f  move another roo
173a6 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c 20 61  t-page to fill a
173a7 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65   gap left by the
173a8 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 72 6f   deleted.  ** ro
173a9 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e 20 6f  ot page. If an o
173aa 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 20 75  pen cursor was u
173ab 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 20 61  sing this page a
173ac 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 20 0a   problem would .
173ad 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 2a 2f    ** occur..  */
173ae 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 43 75 72  .  if( pBt->pCur
173af 73 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72  sor ){.    retur
173b0 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b  n SQLITE_LOCKED;
173b1 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c  .  }..  rc = sql
173b2 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
173b3 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62  (pBt, (Pgno)iTab
173b4 6c 65 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a  le, &pPage, 0);.
173b5 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
173b6 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  n rc;.  rc = sql
173b7 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61  ite3BtreeClearTa
173b8 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 30  ble(p, iTable, 0
173b9 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
173ba 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
173bb 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Page);.    retur
173bc 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69  n rc;.  }..  *pi
173bd 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 69 66  Moved = 0;..  if
173be 28 20 69 54 61 62 6c 65 3e 31 20 29 7b 0a 23 69  ( iTable>1 ){.#i
173bf 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
173c0 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
173c1 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 50  rc = freePage(pP
173c2 61 67 65 29 3b 0a 20 20 20 20 72 65 6c 65 61 73  age);.    releas
173c3 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 23 65  ePage(pPage);.#e
173c4 6c 73 65 0a 20 20 20 20 69 66 28 20 70 42 74 2d  lse.    if( pBt-
173c5 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
173c6 20 20 20 20 20 50 67 6e 6f 20 6d 61 78 52 6f 6f       Pgno maxRoo
173c7 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20  tPgno;.      rc 
173c8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
173c9 74 4d 65 74 61 28 70 2c 20 34 2c 20 26 6d 61 78  tMeta(p, 4, &max
173ca 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20 20  RootPgno);.     
173cb 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
173cc 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
173cd 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
173ce 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
173cf 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   rc;.      }..  
173d0 20 20 20 20 69 66 28 20 69 54 61 62 6c 65 3d 3d      if( iTable==
173d1 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a 20  maxRootPgno ){. 
173d2 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
173d3 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f   table being dro
173d4 70 70 65 64 20 69 73 20 74 68 65 20 74 61 62 6c  pped is the tabl
173d5 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65  e with the large
173d6 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20  st root-page.   
173d7 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69       ** number i
173d8 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
173d9 70 75 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67  put the root pag
173da 65 20 6f 6e 20 74 68 65 20 66 72 65 65 20 6c 69  e on the free li
173db 73 74 2e 20 0a 20 20 20 20 20 20 20 20 2a 2f 0a  st. .        */.
173dc 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 72 65          rc = fre
173dd 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
173de 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
173df 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
173e0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
173e1 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
173e2 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
173e3 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
173e4 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e{.        /* Th
173e5 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72  e table being dr
173e6 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f 74 20 68  opped does not h
173e7 61 76 65 20 74 68 65 20 6c 61 72 67 65 73 74 20  ave the largest 
173e8 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20  root-page.      
173e9 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74    ** number in t
173ea 68 65 20 64 61 74 61 62 61 73 65 2e 20 53 6f 20  he database. So 
173eb 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 74 68  move the page th
173ec 61 74 20 64 6f 65 73 20 69 6e 74 6f 20 74 68 65  at does into the
173ed 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 61 70   .        ** gap
173ee 20 6c 65 66 74 20 62 79 20 74 68 65 20 64 65 6c   left by the del
173ef 65 74 65 64 20 72 6f 6f 74 2d 70 61 67 65 2e 0a  eted root-page..
173f0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
173f1 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4d 6f 76     MemPage *pMov
173f2 65 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  e;.        relea
173f3 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
173f4 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
173f5 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
173f6 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  pBt, maxRootPgno
173f7 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20  , &pMove, 0);.  
173f8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
173f9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
173fa 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
173fb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
173fc 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50    rc = relocateP
173fd 61 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20  age(pBt, pMove, 
173fe 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c  PTRMAP_ROOTPAGE,
173ff 20 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a   0, iTable, 0);.
17400 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
17401 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20  age(pMove);.    
17402 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
17403 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
17404 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
17405 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
17406 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
17407 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6d 61  eGetPage(pBt, ma
17408 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76  xRootPgno, &pMov
17409 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  e, 0);.        i
1740a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1740b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
1740c 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
1740d 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20   }.        rc = 
1740e 66 72 65 65 50 61 67 65 28 70 4d 6f 76 65 29 3b  freePage(pMove);
1740f 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
17410 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20  Page(pMove);.   
17411 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
17412 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
17413 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
17414 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
17415 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52   *piMoved = maxR
17416 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d  ootPgno;.      }
17417 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74  ..      /* Set t
17418 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74  he new 'max-root
17419 2d 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e 20  -page' value in 
1741a 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61  the database hea
1741b 64 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20 20  der. This.      
1741c 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76 61  ** is the old va
1741d 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65  lue less one, le
1741e 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74  ss one more if t
1741f 68 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a 20  hat happens to. 
17420 20 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f       ** be a roo
17421 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c  t-page number, l
17422 65 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69 66  ess one again if
17423 20 74 68 61 74 20 69 73 20 74 68 65 0a 20 20 20   that is the.   
17424 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59     ** PENDING_BY
17425 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20 2a  TE_PAGE..      *
17426 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50  /.      maxRootP
17427 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28  gno--;.      if(
17428 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45   maxRootPgno==PE
17429 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
1742a 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pBt) ){.        
1742b 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20  maxRootPgno--;. 
1742c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1742d 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 54   maxRootPgno==PT
1742e 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c  RMAP_PAGENO(pBt,
1742f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 20 29 7b   maxRootPgno) ){
17430 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f 6f 74  .        maxRoot
17431 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a  Pgno--;.      }.
17432 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 61        assert( ma
17433 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 4e 44 49  xRootPgno!=PENDI
17434 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
17435 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d  ) );..      rc =
17436 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
17437 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 6d 61  ateMeta(p, 4, ma
17438 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20  xRootPgno);.    
17439 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
1743a 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67 65  = freePage(pPage
1743b 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  );.      release
1743c 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
1743d 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73   }.#endif.  }els
1743e 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 71 6c  e{.    /* If sql
1743f 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62  ite3BtreeDropTab
17440 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e  le was called on
17441 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20 20 20 20   page 1. */.    
17442 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20  zeroPage(pPage, 
17443 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c  PTF_INTKEY|PTF_L
17444 45 41 46 20 29 3b 0a 20 20 20 20 72 65 6c 65 61  EAF );.    relea
17445 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
17446 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20   }.  return rc; 
17447 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   .}.SQLITE_PRIVA
17448 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
17449 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72  reeDropTable(Btr
1744a 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c  ee *p, int iTabl
1744b 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29  e, int *piMoved)
1744c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
1744d 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
1744e 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62  p);.  p->pBt->db
1744f 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 63 20 3d   = p->db;.  rc =
17450 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28   btreeDropTable(
17451 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f 76  p, iTable, piMov
17452 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ed);.  sqlite3Bt
17453 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
17454 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
17455 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d 65 74  .** Read the met
17456 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75  a-information ou
17457 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  t of a database 
17458 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a  file.  Meta[0].*
17459 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  * is the number 
1745a 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 63 75  of free pages cu
1745b 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 64  rrently in the d
1745c 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 31  atabase.  Meta[1
1745d 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d 65 74  ].** through met
1745e 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69 6c 61  a[15] are availa
1745f 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 20 68  ble for use by h
17460 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20 20 4d  igher layers.  M
17461 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72 65 61  eta[0].** is rea
17462 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74 68 65  d-only, the othe
17463 72 73 20 61 72 65 20 72 65 61 64 2f 77 72 69 74  rs are read/writ
17464 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 73 63  e..** .** The sc
17465 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d 62 65  hema layer numbe
17466 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73 20 64  rs meta values d
17467 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41 74 20  ifferently.  At 
17468 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20 6c 61  the schema.** la
17469 79 65 72 20 28 61 6e 64 20 74 68 65 20 53 65 74  yer (and the Set
1746a 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61 64 43  Cookie and ReadC
1746b 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29 20 74  ookie opcodes) t
1746c 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
1746d 66 72 65 65 20 70 61 67 65 73 20 69 73 20 6e 6f  free pages is no
1746e 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f 20 43  t visible.  So C
1746f 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68 65 20  ookie[0] is the 
17470 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31 5d 2e  same as Meta[1].
17471 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
17472 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
17473 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65  reeGetMeta(Btree
17474 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33   *p, int idx, u3
17475 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20 44 62 50  2 *pMeta){.  DbP
17476 61 67 65 20 2a 70 44 62 50 61 67 65 20 3d 20 30  age *pDbPage = 0
17477 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e  ;.  int rc;.  un
17478 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 31  signed char *pP1
17479 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
1747a 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73  t = p->pBt;..  s
1747b 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1747c 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d  (p);.  pBt->db =
1747d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 52 65   p->db;..  /* Re
1747e 61 64 69 6e 67 20 61 20 6d 65 74 61 2d 64 61 74  ading a meta-dat
1747f 61 20 76 61 6c 75 65 20 72 65 71 75 69 72 65 73  a value requires
17480 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20   a read-lock on 
17481 70 61 67 65 20 31 20 28 61 6e 64 20 68 65 6e 63  page 1 (and henc
17482 65 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74  e.  ** the sqlit
17483 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20  e_master table. 
17484 57 65 20 67 72 61 62 20 74 68 69 73 20 6c 6f 63  We grab this loc
17485 6b 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  k regardless of 
17486 77 68 65 74 68 65 72 20 6f 72 0a 20 20 2a 2a 20  whether or.  ** 
17487 6e 6f 74 20 74 68 65 20 53 51 4c 49 54 45 5f 52  not the SQLITE_R
17488 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66  eadUncommitted f
17489 6c 61 67 20 69 73 20 73 65 74 20 28 74 68 65 20  lag is set (the 
1748a 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 61 74 20  table rooted at 
1748b 70 61 67 65 0a 20 20 2a 2a 20 31 20 69 73 20 74  page.  ** 1 is t
1748c 72 65 61 74 65 64 20 61 73 20 61 20 73 70 65 63  reated as a spec
1748d 69 61 6c 20 63 61 73 65 20 62 79 20 71 75 65 72  ial case by quer
1748e 79 54 61 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64  yTableLock() and
1748f 20 6c 6f 63 6b 54 61 62 6c 65 28 29 29 2e 0a 20   lockTable()).. 
17490 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79   */.  rc = query
17491 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 31 2c 20  TableLock(p, 1, 
17492 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66  READ_LOCK);.  if
17493 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
17494 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
17495 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20  reeLeave(p);.   
17496 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
17497 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d  .  assert( idx>=
17498 30 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a  0 && idx<=15 );.
17499 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65    if( pBt->pPage
1749a 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  1 ){.    /* The 
1749b 62 2d 74 72 65 65 20 69 73 20 61 6c 72 65 61 64  b-tree is alread
1749c 79 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 66 65  y holding a refe
1749d 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20  rence to page 1 
1749e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  of the database.
1749f 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 49 6e 20      ** file. In 
174a0 74 68 69 73 20 63 61 73 65 20 74 68 65 20 72 65  this case the re
174a1 71 75 69 72 65 64 20 6d 65 74 61 2d 64 61 74 61  quired meta-data
174a2 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 72 65   value can be re
174a3 61 64 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20  ad directly.    
174a4 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ** from the page
174a5 20 64 61 74 61 20 6f 66 20 74 68 69 73 20 72 65   data of this re
174a6 66 65 72 65 6e 63 65 2e 20 54 68 69 73 20 69 73  ference. This is
174a7 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72   slightly faster
174a8 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 72 65 71   than.    ** req
174a9 75 65 73 74 69 6e 67 20 61 20 6e 65 77 20 72 65  uesting a new re
174aa 66 65 72 65 6e 63 65 20 66 72 6f 6d 20 74 68 65  ference from the
174ab 20 70 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20   pager layer..  
174ac 20 20 2a 2f 0a 20 20 20 20 70 50 31 20 3d 20 28    */.    pP1 = (
174ad 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
174ae 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
174af 74 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ta;.  }else{.   
174b0 20 2f 2a 20 54 68 65 20 62 2d 74 72 65 65 20 64   /* The b-tree d
174b1 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 20 72  oes not have a r
174b2 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65  eference to page
174b3 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61   1 of the databa
174b4 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 20  se file..    ** 
174b5 4f 62 74 61 69 6e 20 6f 6e 65 20 66 72 6f 6d 20  Obtain one from 
174b6 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 2e  the pager layer.
174b7 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
174b8 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
174b9 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 31 2c  (pBt->pPager, 1,
174ba 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20   &pDbPage);.    
174bb 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
174bc 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
174bd 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74 75  e(p);.      retu
174be 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
174bf 20 70 50 31 20 3d 20 28 75 6e 73 69 67 6e 65 64   pP1 = (unsigned
174c0 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50   char *)sqlite3P
174c1 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
174c2 61 67 65 29 3b 0a 20 20 7d 0a 20 20 2a 70 4d 65  age);.  }.  *pMe
174c3 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  ta = get4byte(&p
174c4 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b  P1[36 + idx*4]);
174c5 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 62 2d  ..  /* If the b-
174c6 74 72 65 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64  tree is not hold
174c7 69 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 20  ing a reference 
174c8 74 6f 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20  to page 1, then 
174c9 6f 6e 65 20 77 61 73 20 0a 20 20 2a 2a 20 72 65  one was .  ** re
174ca 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65  quested from the
174cb 20 70 61 67 65 72 20 6c 61 79 65 72 20 69 6e 20   pager layer in 
174cc 74 68 65 20 61 62 6f 76 65 20 62 6c 6f 63 6b 2e  the above block.
174cd 20 52 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e   Release it now.
174ce 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 42 74  .  */.  if( !pBt
174cf 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20  ->pPage1 ){.    
174d0 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
174d1 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a  f(pDbPage);.  }.
174d2 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 76 61 63  .  /* If autovac
174d3 75 75 6d 65 64 20 69 73 20 64 69 73 61 62 6c 65  uumed is disable
174d4 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20  d in this build 
174d5 62 75 74 20 77 65 20 61 72 65 20 74 72 79 69 6e  but we are tryin
174d6 67 20 74 6f 20 0a 20 20 2a 2a 20 61 63 63 65 73  g to .  ** acces
174d7 73 20 61 6e 20 61 75 74 6f 76 61 63 75 75 6d 65  s an autovacuume
174d8 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e  d database, then
174d9 20 6d 61 6b 65 20 74 68 65 20 64 61 74 61 62 61   make the databa
174da 73 65 20 72 65 61 64 6f 6e 6c 79 2e 20 0a 20 20  se readonly. .  
174db 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
174dc 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
174dd 0a 20 20 69 66 28 20 69 64 78 3d 3d 34 20 26 26  .  if( idx==4 &&
174de 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74 2d   *pMeta>0 ) pBt-
174df 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23  >readOnly = 1;.#
174e0 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 72 61 62  endif..  /* Grab
174e1 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 6f   the read-lock o
174e2 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20 20 72  n page 1. */.  r
174e3 63 20 3d 20 6c 6f 63 6b 54 61 62 6c 65 28 70 2c  c = lockTable(p,
174e4 20 31 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a   1, READ_LOCK);.
174e5 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
174e6 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
174e7 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72   rc;.}../*.** Wr
174e8 69 74 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61  ite meta-informa
174e9 74 69 6f 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74  tion back into t
174ea 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65  he database.  Me
174eb 74 61 5b 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64  ta[0] is.** read
174ec 2d 6f 6e 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f  -only and may no
174ed 74 20 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f  t be written..*/
174ee 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
174ef 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
174f0 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65  UpdateMeta(Btree
174f1 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33   *p, int idx, u3
174f2 32 20 69 4d 65 74 61 29 7b 0a 20 20 42 74 53 68  2 iMeta){.  BtSh
174f3 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
174f4 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  Bt;.  unsigned c
174f5 68 61 72 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20  har *pP1;.  int 
174f6 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  rc;.  assert( id
174f7 78 3e 3d 31 20 26 26 20 69 64 78 3c 3d 31 35 20  x>=1 && idx<=15 
174f8 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
174f9 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74  eEnter(p);.  pBt
174fa 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
174fb 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61  assert( p->inTra
174fc 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns==TRANS_WRITE 
174fd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  );.  assert( pBt
174fe 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 3b 0a 20  ->pPage1!=0 );. 
174ff 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67   pP1 = pBt->pPag
17500 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20  e1->aData;.  rc 
17501 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
17502 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d  ite(pBt->pPage1-
17503 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28  >pDbPage);.  if(
17504 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
17505 7b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26  {.    put4byte(&
17506 70 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d 2c  pP1[36 + idx*4],
17507 20 69 4d 65 74 61 29 3b 0a 23 69 66 6e 64 65 66   iMeta);.#ifndef
17508 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
17509 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20  OVACUUM.    if( 
1750a 69 64 78 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20  idx==7 ){.      
1750b 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74  assert( pBt->aut
1750c 6f 56 61 63 75 75 6d 20 7c 7c 20 69 4d 65 74 61  oVacuum || iMeta
1750d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  ==0 );.      ass
1750e 65 72 74 28 20 69 4d 65 74 61 3d 3d 30 20 7c 7c  ert( iMeta==0 ||
1750f 20 69 4d 65 74 61 3d 3d 31 20 29 3b 0a 20 20 20   iMeta==1 );.   
17510 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75     pBt->incrVacu
17511 75 6d 20 3d 20 28 75 38 29 69 4d 65 74 61 3b 0a  um = (u8)iMeta;.
17512 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
17513 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
17514 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
17515 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
17516 65 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20 62  eturn the flag b
17517 79 74 65 20 61 74 20 74 68 65 20 62 65 67 69 6e  yte at the begin
17518 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65  ning of the page
17519 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
1751a 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79  .** is currently
1751b 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2f   pointing to..*/
1751c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1751d 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
1751e 46 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20 2a  Flags(BtCursor *
1751f 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f  pCur){.  /* TODO
17520 3a 20 57 68 61 74 20 61 62 6f 75 74 20 43 55 52  : What about CUR
17521 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20  SOR_REQUIRESEEK 
17522 73 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79 20  state? Probably 
17523 6e 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20 2a  need to call.  *
17524 2a 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50  * restoreCursorP
17525 6f 73 69 74 69 6f 6e 28 29 20 68 65 72 65 2e 0a  osition() here..
17526 20 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a    */.  MemPage *
17527 70 50 61 67 65 3b 0a 20 20 72 65 73 74 6f 72 65  pPage;.  restore
17528 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
17529 43 75 72 29 3b 0a 20 20 70 50 61 67 65 20 3d 20  Cur);.  pPage = 
1752a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
1752b 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73  r->iPage];.  ass
1752c 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
1752d 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
1752e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 21 3d   assert( pPage!=
1752f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
17530 50 61 67 65 2d 3e 70 42 74 3d 3d 70 43 75 72 2d  Page->pBt==pCur-
17531 3e 70 42 74 20 29 3b 0a 20 20 72 65 74 75 72 6e  >pBt );.  return
17532 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50   pPage->aData[pP
17533 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 5d 3b  age->hdrOffset];
17534 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .}.../*.** Retur
17535 6e 20 74 68 65 20 70 61 67 65 72 20 61 73 73 6f  n the pager asso
17536 63 69 61 74 65 64 20 77 69 74 68 20 61 20 42 54  ciated with a BT
17537 72 65 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ree.  This routi
17538 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a  ne is used for.*
17539 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  * testing and de
1753a 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f  bugging only..*/
1753b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1753c 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33 42 74  Pager *sqlite3Bt
1753d 72 65 65 50 61 67 65 72 28 42 74 72 65 65 20 2a  reePager(Btree *
1753e 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p){.  return p->
1753f 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a  pBt->pPager;.}..
17540 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
17541 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
17542 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64  ECK./*.** Append
17543 20 61 20 6d 65 73 73 61 67 65 20 74 6f 20 74 68   a message to th
17544 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
17545 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  string..*/.stati
17546 63 20 76 6f 69 64 20 63 68 65 63 6b 41 70 70 65  c void checkAppe
17547 6e 64 4d 73 67 28 0a 20 20 49 6e 74 65 67 72 69  ndMsg(.  Integri
17548 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 0a 20 20  tyCk *pCheck,.  
17549 63 68 61 72 20 2a 7a 4d 73 67 31 2c 0a 20 20 63  char *zMsg1,.  c
1754a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
1754b 61 74 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76  at,.  ....){.  v
1754c 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28  a_list ap;.  if(
1754d 20 21 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20   !pCheck->mxErr 
1754e 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 68 65  ) return;.  pChe
1754f 63 6b 2d 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 70  ck->mxErr--;.  p
17550 43 68 65 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Check->nErr++;. 
17551 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
17552 6f 72 6d 61 74 29 3b 0a 20 20 69 66 28 20 70 43  ormat);.  if( pC
17553 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43 68  heck->errMsg.nCh
17554 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ar ){.    sqlite
17555 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
17556 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c  &pCheck->errMsg,
17557 20 22 5c 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a 20   "\n", 1);.  }. 
17558 20 69 66 28 20 7a 4d 73 67 31 20 29 7b 0a 20 20   if( zMsg1 ){.  
17559 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
1755a 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b 2d  mAppend(&pCheck-
1755b 3e 65 72 72 4d 73 67 2c 20 7a 4d 73 67 31 2c 20  >errMsg, zMsg1, 
1755c 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  -1);.  }.  sqlit
1755d 65 33 56 58 50 72 69 6e 74 66 28 26 70 43 68 65  e3VXPrintf(&pChe
1755e 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 31 2c 20 7a  ck->errMsg, 1, z
1755f 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76  Format, ap);.  v
17560 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28  a_end(ap);.  if(
17561 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e   pCheck->errMsg.
17562 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
17563 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c      pCheck->mall
17564 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
17565 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  }.}.#endif /* SQ
17566 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
17567 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69  ITY_CHECK */..#i
17568 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17569 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
1756a 4b 0a 2f 2a 0a 2a 2a 20 41 64 64 20 31 20 74 6f  K./*.** Add 1 to
1756b 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   the reference c
1756c 6f 75 6e 74 20 66 6f 72 20 70 61 67 65 20 69 50  ount for page iP
1756d 61 67 65 2e 20 20 49 66 20 74 68 69 73 20 69 73  age.  If this is
1756e 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 72   the second.** r
1756f 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
17570 70 61 67 65 2c 20 61 64 64 20 61 6e 20 65 72 72  page, add an err
17571 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 43  or message to pC
17572 68 65 63 6b 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a  heck->zErrMsg..*
17573 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68  * Return 1 if th
17574 65 72 65 20 61 72 65 20 32 20 6f 72 65 20 6d 6f  ere are 2 ore mo
17575 72 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  re references to
17576 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 30 20   the page and 0 
17577 69 66 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73  if.** if this is
17578 20 74 68 65 20 66 69 72 73 74 20 72 65 66 65 72   the first refer
17579 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65  ence to the page
1757a 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 63 68 65  ..**.** Also che
1757b 63 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ck that the page
1757c 20 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 62 6f   number is in bo
1757d 75 6e 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  unds..*/.static 
1757e 69 6e 74 20 63 68 65 63 6b 52 65 66 28 49 6e 74  int checkRef(Int
1757f 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b  egrityCk *pCheck
17580 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c 20 63 68  , Pgno iPage, ch
17581 61 72 20 2a 7a 43 6f 6e 74 65 78 74 29 7b 0a 20  ar *zContext){. 
17582 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20   if( iPage==0 ) 
17583 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20  return 1;.  if( 
17584 69 50 61 67 65 3e 70 43 68 65 63 6b 2d 3e 6e 50  iPage>pCheck->nP
17585 61 67 65 20 29 7b 0a 20 20 20 20 63 68 65 63 6b  age ){.    check
17586 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
17587 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 69 6e 76  , zContext, "inv
17588 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62 65 72  alid page number
17589 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20   %d", iPage);.  
1758a 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
1758b 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 61 6e    if( pCheck->an
1758c 52 65 66 5b 69 50 61 67 65 5d 3d 3d 31 20 29 7b  Ref[iPage]==1 ){
1758d 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64  .    checkAppend
1758e 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
1758f 74 65 78 74 2c 20 22 32 6e 64 20 72 65 66 65 72  text, "2nd refer
17590 65 6e 63 65 20 74 6f 20 70 61 67 65 20 25 64 22  ence to page %d"
17591 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65  , iPage);.    re
17592 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65  turn 1;.  }.  re
17593 74 75 72 6e 20 20 28 70 43 68 65 63 6b 2d 3e 61  turn  (pCheck->a
17594 6e 52 65 66 5b 69 50 61 67 65 5d 2b 2b 29 3e 31  nRef[iPage]++)>1
17595 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
17596 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
17597 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  UUM./*.** Check 
17598 74 68 61 74 20 74 68 65 20 65 6e 74 72 79 20 69  that the entry i
17599 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  n the pointer-ma
1759a 70 20 66 6f 72 20 70 61 67 65 20 69 43 68 69 6c  p for page iChil
1759b 64 20 6d 61 70 73 20 74 6f 20 0a 2a 2a 20 70 61  d maps to .** pa
1759c 67 65 20 69 50 61 72 65 6e 74 2c 20 70 6f 69 6e  ge iParent, poin
1759d 74 65 72 20 74 79 70 65 20 70 74 72 54 79 70 65  ter type ptrType
1759e 2e 20 49 66 20 6e 6f 74 2c 20 61 70 70 65 6e 64  . If not, append
1759f 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
175a0 65 0a 2a 2a 20 74 6f 20 70 43 68 65 63 6b 2e 0a  e.** to pCheck..
175a1 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
175a2 68 65 63 6b 50 74 72 6d 61 70 28 0a 20 20 49 6e  heckPtrmap(.  In
175a3 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63  tegrityCk *pChec
175a4 6b 2c 20 20 20 2f 2a 20 49 6e 74 65 67 72 69 74  k,   /* Integrit
175a5 79 20 63 68 65 63 6b 20 63 6f 6e 74 65 78 74 20  y check context 
175a6 2a 2f 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c 64  */.  Pgno iChild
175a7 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  ,           /* C
175a8 68 69 6c 64 20 70 61 67 65 20 6e 75 6d 62 65 72  hild page number
175a9 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20   */.  u8 eType, 
175aa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
175ab 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74 65 72  Expected pointer
175ac 20 6d 61 70 20 74 79 70 65 20 2a 2f 0a 20 20 50   map type */.  P
175ad 67 6e 6f 20 69 50 61 72 65 6e 74 2c 20 20 20 20  gno iParent,    
175ae 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65        /* Expecte
175af 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  d pointer map pa
175b0 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72  rent page number
175b1 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e   */.  char *zCon
175b2 74 65 78 74 20 20 20 20 20 20 20 20 20 2f 2a 20  text         /* 
175b3 43 6f 6e 74 65 78 74 20 64 65 73 63 72 69 70 74  Context descript
175b4 69 6f 6e 20 28 75 73 65 64 20 66 6f 72 20 65 72  ion (used for er
175b5 72 6f 72 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20  ror msg) */.){. 
175b6 20 69 6e 74 20 72 63 3b 0a 20 20 75 38 20 65 50   int rc;.  u8 eP
175b7 74 72 6d 61 70 54 79 70 65 3b 0a 20 20 50 67 6e  trmapType;.  Pgn
175b8 6f 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 3b  o iPtrmapParent;
175b9 0a 0a 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47  ..  rc = ptrmapG
175ba 65 74 28 70 43 68 65 63 6b 2d 3e 70 42 74 2c 20  et(pCheck->pBt, 
175bb 69 43 68 69 6c 64 2c 20 26 65 50 74 72 6d 61 70  iChild, &ePtrmap
175bc 54 79 70 65 2c 20 26 69 50 74 72 6d 61 70 50 61  Type, &iPtrmapPa
175bd 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 21  rent);.  if( rc!
175be 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
175bf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
175c0 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65 63 6b 2d  _NOMEM ) pCheck-
175c1 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
175c2 31 3b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65  1;.    checkAppe
175c3 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
175c4 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c 65 64 20  ontext, "Failed 
175c5 74 6f 20 72 65 61 64 20 70 74 72 6d 61 70 20 6b  to read ptrmap k
175c6 65 79 3d 25 64 22 2c 20 69 43 68 69 6c 64 29 3b  ey=%d", iChild);
175c7 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
175c8 0a 0a 20 20 69 66 28 20 65 50 74 72 6d 61 70 54  ..  if( ePtrmapT
175c9 79 70 65 21 3d 65 54 79 70 65 20 7c 7c 20 69 50  ype!=eType || iP
175ca 74 72 6d 61 70 50 61 72 65 6e 74 21 3d 69 50 61  trmapParent!=iPa
175cb 72 65 6e 74 20 29 7b 0a 20 20 20 20 63 68 65 63  rent ){.    chec
175cc 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
175cd 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20  k, zContext, .  
175ce 20 20 20 20 22 42 61 64 20 70 74 72 20 6d 61 70      "Bad ptr map
175cf 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 20 65 78   entry key=%d ex
175d0 70 65 63 74 65 64 3d 28 25 64 2c 25 64 29 20 67  pected=(%d,%d) g
175d1 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 0a 20 20  ot=(%d,%d)", .  
175d2 20 20 20 20 69 43 68 69 6c 64 2c 20 65 54 79 70      iChild, eTyp
175d3 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 50 74 72  e, iParent, ePtr
175d4 6d 61 70 54 79 70 65 2c 20 69 50 74 72 6d 61 70  mapType, iPtrmap
175d5 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 23  Parent);.  }.}.#
175d6 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  endif../*.** Che
175d7 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79  ck the integrity
175d8 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74   of the freelist
175d9 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c   or of an overfl
175da 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a  ow page list..**
175db 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
175dc 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
175dd 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20   on the list is 
175de 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  N..*/.static voi
175df 64 20 63 68 65 63 6b 4c 69 73 74 28 0a 20 20 49  d checkList(.  I
175e0 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65  ntegrityCk *pChe
175e1 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 72 69 74  ck,  /* Integrit
175e2 79 20 63 68 65 63 6b 69 6e 67 20 63 6f 6e 74 65  y checking conte
175e3 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 46 72  xt */.  int isFr
175e4 65 65 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  eeList,       /*
175e5 20 54 72 75 65 20 66 6f 72 20 61 20 66 72 65 65   True for a free
175e6 6c 69 73 74 2e 20 20 46 61 6c 73 65 20 66 6f 72  list.  False for
175e7 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c   overflow page l
175e8 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61  ist */.  int iPa
175e9 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ge,            /
175ea 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  * Page number fo
175eb 72 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20  r first page in 
175ec 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  the list */.  in
175ed 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20 20  t N,            
175ee 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20      /* Expected 
175ef 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
175f0 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  in the list */. 
175f1 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20   char *zContext 
175f2 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
175f3 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73  t for error mess
175f4 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ages */.){.  int
175f5 20 69 3b 0a 20 20 69 6e 74 20 65 78 70 65 63 74   i;.  int expect
175f6 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 20 69 46  ed = N;.  int iF
175f7 69 72 73 74 20 3d 20 69 50 61 67 65 3b 0a 20 20  irst = iPage;.  
175f8 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26  while( N-- > 0 &
175f9 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20  & pCheck->mxErr 
175fa 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70  ){.    DbPage *p
175fb 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20 20 75 6e  OvflPage;.    un
175fc 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 4f 76  signed char *pOv
175fd 66 6c 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20  flData;.    if( 
175fe 69 50 61 67 65 3c 31 20 29 7b 0a 20 20 20 20 20  iPage<1 ){.     
175ff 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
17600 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74  pCheck, zContext
17601 2c 0a 20 20 20 20 20 20 20 20 20 22 25 64 20 6f  ,.         "%d o
17602 66 20 25 64 20 70 61 67 65 73 20 6d 69 73 73 69  f %d pages missi
17603 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66 6c 6f 77  ng from overflow
17604 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61   list starting a
17605 74 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20  t %d",.         
17606 20 4e 2b 31 2c 20 65 78 70 65 63 74 65 64 2c 20   N+1, expected, 
17607 69 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20 62  iFirst);.      b
17608 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
17609 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 43 68  if( checkRef(pCh
1760a 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e  eck, iPage, zCon
1760b 74 65 78 74 29 20 29 20 62 72 65 61 6b 3b 0a 20  text) ) break;. 
1760c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61     if( sqlite3Pa
1760d 67 65 72 47 65 74 28 70 43 68 65 63 6b 2d 3e 70  gerGet(pCheck->p
1760e 50 61 67 65 72 2c 20 28 50 67 6e 6f 29 69 50 61  Pager, (Pgno)iPa
1760f 67 65 2c 20 26 70 4f 76 66 6c 50 61 67 65 29 20  ge, &pOvflPage) 
17610 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
17611 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
17612 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61 69 6c 65  zContext, "faile
17613 64 20 74 6f 20 67 65 74 20 70 61 67 65 20 25 64  d to get page %d
17614 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20  ", iPage);.     
17615 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
17616 20 20 70 4f 76 66 6c 44 61 74 61 20 3d 20 28 75    pOvflData = (u
17617 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73  nsigned char *)s
17618 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
17619 74 61 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20  ta(pOvflPage);. 
1761a 20 20 20 69 66 28 20 69 73 46 72 65 65 4c 69 73     if( isFreeLis
1761b 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  t ){.      int n
1761c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76   = get4byte(&pOv
1761d 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 69 66 6e  flData[4]);.#ifn
1761e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1761f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
17620 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74   if( pCheck->pBt
17621 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
17622 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72          checkPtr
17623 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 50 61 67  map(pCheck, iPag
17624 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41  e, PTRMAP_FREEPA
17625 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29  GE, 0, zContext)
17626 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
17627 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 70 43 68  .      if( n>pCh
17628 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  eck->pBt->usable
17629 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 20 20 20  Size/4-2 ){.    
1762a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
1762b 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
1762c 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ext,.           
1762d 22 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20 63  "freelist leaf c
1762e 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 6f 6e 20  ount too big on 
1762f 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29  page %d", iPage)
17630 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d 3b 0a 20  ;.        N--;. 
17631 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
17632 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
17633 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
17634 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67 65    Pgno iFreePage
17635 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76   = get4byte(&pOv
17636 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d 29 3b 0a  flData[8+i*4]);.
17637 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
17638 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
17639 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 68           if( pCh
1763a 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61  eck->pBt->autoVa
1763b 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  cuum ){.        
1763c 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28      checkPtrmap(
1763d 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67  pCheck, iFreePag
1763e 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41  e, PTRMAP_FREEPA
1763f 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29  GE, 0, zContext)
17640 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65  ;.          }.#e
17641 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 63  ndif.          c
17642 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c 20  heckRef(pCheck, 
17643 69 46 72 65 65 50 61 67 65 2c 20 7a 43 6f 6e 74  iFreePage, zCont
17644 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ext);.        }.
17645 20 20 20 20 20 20 20 20 4e 20 2d 3d 20 6e 3b 0a          N -= n;.
17646 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69        }.    }.#i
17647 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17648 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
17649 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20   else{.      /* 
1764a 49 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65  If this database
1764b 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76   supports auto-v
1764c 61 63 75 75 6d 20 61 6e 64 20 69 50 61 67 65 20  acuum and iPage 
1764d 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a  is not the last.
1764e 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e        ** page in
1764f 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 6c   this overflow l
17650 69 73 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20  ist, check that 
17651 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
17652 65 6e 74 72 79 20 66 6f 72 0a 20 20 20 20 20 20  entry for.      
17653 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** the following
17654 20 70 61 67 65 20 6d 61 74 63 68 65 73 20 69 50   page matches iP
17655 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  age..      */.  
17656 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e      if( pCheck->
17657 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
17658 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20 20 20 20  && N>0 ){.      
17659 20 20 69 20 3d 20 67 65 74 34 62 79 74 65 28 70    i = get4byte(p
1765a 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 20  OvflData);.     
1765b 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70     checkPtrmap(p
1765c 43 68 65 63 6b 2c 20 69 2c 20 50 54 52 4d 41 50  Check, i, PTRMAP
1765d 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 50 61 67  _OVERFLOW2, iPag
1765e 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  e, zContext);.  
1765f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64      }.    }.#end
17660 69 66 0a 20 20 20 20 69 50 61 67 65 20 3d 20 67  if.    iPage = g
17661 65 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74  et4byte(pOvflDat
17662 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  a);.    sqlite3P
17663 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c 50  agerUnref(pOvflP
17664 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  age);.  }.}.#end
17665 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
17666 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
17667 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  K */..#ifndef SQ
17668 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
17669 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20  ITY_CHECK./*.** 
1766a 44 6f 20 76 61 72 69 6f 75 73 20 73 61 6e 69 74  Do various sanit
1766b 79 20 63 68 65 63 6b 73 20 6f 6e 20 61 20 73 69  y checks on a si
1766c 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 61 20 74  ngle page of a t
1766d 72 65 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  ree.  Return.** 
1766e 74 68 65 20 74 72 65 65 20 64 65 70 74 68 2e 20  the tree depth. 
1766f 20 52 6f 6f 74 20 70 61 67 65 73 20 72 65 74 75   Root pages retu
17670 72 6e 20 30 2e 20 20 50 61 72 65 6e 74 73 20 6f  rn 0.  Parents o
17671 66 20 72 6f 6f 74 20 70 61 67 65 73 0a 2a 2a 20  f root pages.** 
17672 72 65 74 75 72 6e 20 31 2c 20 61 6e 64 20 73 6f  return 1, and so
17673 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 54   forth..** .** T
17674 68 65 73 65 20 63 68 65 63 6b 73 20 61 72 65 20  hese checks are 
17675 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  done:.**.**     
17676 20 31 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74   1.  Make sure t
17677 68 61 74 20 63 65 6c 6c 73 20 61 6e 64 20 66 72  hat cells and fr
17678 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e 6f 74 20  eeblocks do not 
17679 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20 20 20 20  overlap.**      
1767a 20 20 20 20 62 75 74 20 63 6f 6d 62 69 6e 65 20      but combine 
1767b 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f  to completely co
1767c 76 65 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a  ver the page..**
1767d 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b 65 20 73    NO  2.  Make s
1767e 75 72 65 20 63 65 6c 6c 20 6b 65 79 73 20 61 72  ure cell keys ar
1767f 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 20 20  e in order..**  
17680 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 73 75 72  NO  3.  Make sur
17681 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c 65 73 73  e no key is less
17682 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
17683 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 2e 0a 2a  o zLowerBound..*
17684 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61 6b 65 20  *  NO  4.  Make 
17685 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 67  sure no key is g
17686 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
17687 71 75 61 6c 20 74 6f 20 7a 55 70 70 65 72 42 6f  qual to zUpperBo
17688 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 35 2e 20  und..**      5. 
17689 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67   Check the integ
1768a 72 69 74 79 20 6f 66 20 6f 76 65 72 66 6c 6f 77  rity of overflow
1768b 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20 20 20 20   pages..**      
1768c 36 2e 20 20 52 65 63 75 72 73 69 76 65 6c 79 20  6.  Recursively 
1768d 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 65 50 61  call checkTreePa
1768e 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 6c 64 72  ge on all childr
1768f 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 2e 20 20  en..**      7.  
17690 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
17691 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 63 68 69  depth of all chi
17692 6c 64 72 65 6e 20 69 73 20 74 68 65 20 73 61 6d  ldren is the sam
17693 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e 20 20 4d  e..**      8.  M
17694 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 70 61  ake sure this pa
17695 67 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 33  ge is at least 3
17696 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c 73 65 20  3% full or else 
17697 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  it is.**        
17698 20 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68    the root of th
17699 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69  e tree..*/.stati
1769a 63 20 69 6e 74 20 63 68 65 63 6b 54 72 65 65 50  c int checkTreeP
1769b 61 67 65 28 0a 20 20 49 6e 74 65 67 72 69 74 79  age(.  Integrity
1769c 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20  Ck *pCheck,  /* 
1769d 43 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20  Context for the 
1769e 73 61 6e 69 74 79 20 63 68 65 63 6b 20 2a 2f 0a  sanity check */.
1769f 20 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20    int iPage,    
176a0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
176a1 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61  number of the pa
176a2 67 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20  ge to check */. 
176a3 20 63 68 61 72 20 2a 7a 50 61 72 65 6e 74 43 6f   char *zParentCo
176a4 6e 74 65 78 74 20 20 2f 2a 20 50 61 72 65 6e 74  ntext  /* Parent
176a5 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 7b 0a 20   context */.){. 
176a6 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
176a7 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c 20 64 65  .  int i, rc, de
176a8 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f 2c 20 63  pth, d2, pgno, c
176a9 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72 2c 20 63  nt;.  int hdr, c
176aa 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69 6e 74 20  ellStart;.  int 
176ab 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a 64 61 74  nCell;.  u8 *dat
176ac 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  a;.  BtShared *p
176ad 42 74 3b 0a 20 20 69 6e 74 20 75 73 61 62 6c 65  Bt;.  int usable
176ae 53 69 7a 65 3b 0a 20 20 63 68 61 72 20 7a 43 6f  Size;.  char zCo
176af 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20 20 63 68  ntext[100];.  ch
176b0 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a 0a 20 20  ar *hit = 0;..  
176b1 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
176b2 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74  (sizeof(zContext
176b3 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 50 61  ), zContext, "Pa
176b4 67 65 20 25 64 3a 20 22 2c 20 69 50 61 67 65 29  ge %d: ", iPage)
176b5 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  ;..  /* Check th
176b6 61 74 20 74 68 65 20 70 61 67 65 20 65 78 69 73  at the page exis
176b7 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 20 3d 20  ts.  */.  pBt = 
176b8 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a 20 20 75  pCheck->pBt;.  u
176b9 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d  sableSize = pBt-
176ba 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69  >usableSize;.  i
176bb 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65  f( iPage==0 ) re
176bc 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 63 68  turn 0;.  if( ch
176bd 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69  eckRef(pCheck, i
176be 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 43 6f 6e  Page, zParentCon
176bf 74 65 78 74 29 20 29 20 72 65 74 75 72 6e 20 30  text) ) return 0
176c0 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 73 71  ;.  if( (rc = sq
176c1 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
176c2 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 50 61  e(pBt, (Pgno)iPa
176c3 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 21  ge, &pPage, 0))!
176c4 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63  =0 ){.    if( rc
176c5 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
176c6 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46   pCheck->mallocF
176c7 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63  ailed = 1;.    c
176c8 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
176c9 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a  heck, zContext,.
176ca 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74         "unable t
176cb 6f 20 67 65 74 20 74 68 65 20 70 61 67 65 2e 20  o get the page. 
176cc 65 72 72 6f 72 20 63 6f 64 65 3d 25 64 22 2c 20  error code=%d", 
176cd 72 63 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  rc);.    return 
176ce 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 72 63  0;.  }.  if( (rc
176cf 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49   = sqlite3BtreeI
176d0 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 29 21  nitPage(pPage))!
176d1 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
176d2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52  ( rc==SQLITE_COR
176d3 52 55 50 54 20 29 3b 20 20 2f 2a 20 54 68 65 20  RUPT );  /* The 
176d4 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 20 65 72  only possible er
176d5 72 6f 72 20 66 72 6f 6d 20 49 6e 69 74 50 61 67  ror from InitPag
176d6 65 20 2a 2f 0a 20 20 20 20 63 68 65 63 6b 41 70  e */.    checkAp
176d7 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
176d8 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20  zContext, .     
176d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 73                "s
176da 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
176db 61 67 65 28 29 20 72 65 74 75 72 6e 73 20 65 72  age() returns er
176dc 72 6f 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63  ror code %d", rc
176dd 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  );.    releasePa
176de 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72  ge(pPage);.    r
176df 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
176e0 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c  /* Check out all
176e1 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f   the cells..  */
176e2 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20  .  depth = 0;.  
176e3 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65  for(i=0; i<pPage
176e4 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63  ->nCell && pChec
176e5 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a  k->mxErr; i++){.
176e6 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20      u8 *pCell;. 
176e7 20 20 20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43     u32 sz;.    C
176e8 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20  ellInfo info;.. 
176e9 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c     /* Check payl
176ea 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  oad overflow pag
176eb 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  es.    */.    sq
176ec 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
176ed 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c  izeof(zContext),
176ee 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20   zContext,.     
176ef 20 20 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65          "On tree
176f0 20 70 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64   page %d cell %d
176f1 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a  : ", iPage, i);.
176f2 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64      pCell = find
176f3 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20  Cell(pPage,i);. 
176f4 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50     sqlite3BtreeP
176f5 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67  arseCellPtr(pPag
176f6 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29  e, pCell, &info)
176f7 3b 0a 20 20 20 20 73 7a 20 3d 20 69 6e 66 6f 2e  ;.    sz = info.
176f8 6e 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 21  nData;.    if( !
176f9 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 20  pPage->intKey ) 
176fa 73 7a 20 2b 3d 20 28 69 6e 74 29 69 6e 66 6f 2e  sz += (int)info.
176fb 6e 4b 65 79 3b 0a 20 20 20 20 61 73 73 65 72 74  nKey;.    assert
176fc 28 20 73 7a 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c  ( sz==info.nPayl
176fd 6f 61 64 20 29 3b 0a 20 20 20 20 69 66 28 20 73  oad );.    if( s
176fe 7a 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b  z>info.nLocal ){
176ff 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65  .      int nPage
17700 20 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c   = (sz - info.nL
17701 6f 63 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a  ocal + usableSiz
17702 65 20 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69  e - 5)/(usableSi
17703 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50  ze - 4);.      P
17704 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67  gno pgnoOvfl = g
17705 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  et4byte(&pCell[i
17706 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b  nfo.iOverflow]);
17707 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
17708 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
17709 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61        if( pBt->a
1770a 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
1770b 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70       checkPtrmap
1770c 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66  (pCheck, pgnoOvf
1770d 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  l, PTRMAP_OVERFL
1770e 4f 57 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e  OW1, iPage, zCon
1770f 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23  text);.      }.#
17710 65 6e 64 69 66 0a 20 20 20 20 20 20 63 68 65 63  endif.      chec
17711 6b 4c 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c  kList(pCheck, 0,
17712 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65   pgnoOvfl, nPage
17713 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20  , zContext);.   
17714 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b   }..    /* Check
17715 20 73 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20   sanity of left 
17716 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20  child page..    
17717 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  */.    if( !pPag
17718 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  e->leaf ){.     
17719 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65   pgno = get4byte
1771a 28 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66  (pCell);.#ifndef
1771b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1771c 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66  OVACUUM.      if
1771d 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
1771e 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65  m ){.        che
1771f 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c  ckPtrmap(pCheck,
17720 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54   pgno, PTRMAP_BT
17721 52 45 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e  REE, iPage, zCon
17722 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23  text);.      }.#
17723 65 6e 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d  endif.      d2 =
17724 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70   checkTreePage(p
17725 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f  Check, pgno, zCo
17726 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66  ntext);.      if
17727 28 20 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70  ( i>0 && d2!=dep
17728 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  th ){.        ch
17729 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
1772a 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22  eck, zContext, "
1772b 43 68 69 6c 64 20 70 61 67 65 20 64 65 70 74 68  Child page depth
1772c 20 64 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20   differs");.    
1772d 20 20 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20    }.      depth 
1772e 3d 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  = d2;.    }.  }.
1772f 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
17730 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d  af ){.    pgno =
17731 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
17732 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
17733 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
17734 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
17735 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65  tf(sizeof(zConte
17736 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a  xt), zContext, .
17737 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17738 20 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64       "On page %d
17739 20 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a   at right child:
1773a 20 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e   ", iPage);.#ifn
1773b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1773c 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69  AUTOVACUUM.    i
1773d 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
1773e 75 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  um ){.      chec
1773f 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
17740 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52  pgno, PTRMAP_BTR
17741 45 45 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20  EE, iPage, 0);. 
17742 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
17743 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43  checkTreePage(pC
17744 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e  heck, pgno, zCon
17745 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f  text);.  }. .  /
17746 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70  * Check for comp
17747 6c 65 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66  lete coverage of
17748 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20   the page.  */. 
17749 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
1774a 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50  Data;.  hdr = pP
1774b 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
1774c 20 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50    hit = sqlite3P
1774d 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e  ageMalloc( pBt->
1774e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66  pageSize );.  if
1774f 28 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ( hit==0 ){.    
17750 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61  pCheck->mallocFa
17751 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  iled = 1;.  }els
17752 65 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65  e{.    u16 conte
17753 6e 74 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62  ntOffset = get2b
17754 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
17755 29 3b 0a 20 20 20 20 69 66 20 28 63 6f 6e 74 65  );.    if (conte
17756 6e 74 4f 66 66 73 65 74 20 3e 20 75 73 61 62 6c  ntOffset > usabl
17757 65 53 69 7a 65 29 20 7b 0a 20 20 20 20 20 20 63  eSize) {.      c
17758 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
17759 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20  heck, 0, .      
1775a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
1775b 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63  Corruption detec
1775c 74 65 64 20 69 6e 20 68 65 61 64 65 72 20 6f 6e  ted in header on
1775d 20 70 61 67 65 20 25 64 22 2c 69 50 61 67 65 2c   page %d",iPage,
1775e 30 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63  0);.      goto c
1775f 68 65 63 6b 5f 70 61 67 65 5f 61 62 6f 72 74 3b  heck_page_abort;
17760 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65  .    }.    memse
17761 74 28 68 69 74 2b 63 6f 6e 74 65 6e 74 4f 66 66  t(hit+contentOff
17762 73 65 74 2c 20 30 2c 20 75 73 61 62 6c 65 53 69  set, 0, usableSi
17763 7a 65 2d 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74  ze-contentOffset
17764 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69  );.    memset(hi
17765 74 2c 20 31 2c 20 63 6f 6e 74 65 6e 74 4f 66 66  t, 1, contentOff
17766 73 65 74 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20  set);.    nCell 
17767 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
17768 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 63 65  [hdr+3]);.    ce
17769 6c 6c 53 74 61 72 74 20 3d 20 68 64 72 20 2b 20  llStart = hdr + 
1776a 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65  12 - 4*pPage->le
1776b 61 66 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  af;.    for(i=0;
1776c 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a   i<nCell; i++){.
1776d 20 20 20 20 20 20 69 6e 74 20 70 63 20 3d 20 67        int pc = g
1776e 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65  et2byte(&data[ce
1776f 6c 6c 53 74 61 72 74 2b 69 2a 32 5d 29 3b 0a 20  llStart+i*2]);. 
17770 20 20 20 20 20 75 31 36 20 73 69 7a 65 20 3d 20       u16 size = 
17771 31 30 32 34 3b 0a 20 20 20 20 20 20 69 6e 74 20  1024;.      int 
17772 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c  j;.      if( pc<
17773 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20  =usableSize ){. 
17774 20 20 20 20 20 20 20 73 69 7a 65 20 3d 20 63 65         size = ce
17775 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
17776 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20   &data[pc]);.   
17777 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28     }.      if( (
17778 70 63 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62  pc+size-1)>=usab
17779 6c 65 53 69 7a 65 20 7c 7c 20 70 63 3c 30 20 29  leSize || pc<0 )
1777a 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41  {.        checkA
1777b 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
1777c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
1777d 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74   "Corruption det
1777e 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64  ected in cell %d
1777f 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69   on page %d",i,i
17780 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d  Page,0);.      }
17781 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f  else{.        fo
17782 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a  r(j=pc+size-1; j
17783 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a  >=pc; j--) hit[j
17784 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ]++;.      }.   
17785 20 7d 0a 20 20 20 20 66 6f 72 28 63 6e 74 3d 30   }.    for(cnt=0
17786 2c 20 69 3d 67 65 74 32 62 79 74 65 28 26 64 61  , i=get2byte(&da
17787 74 61 5b 68 64 72 2b 31 5d 29 3b 20 69 3e 30 20  ta[hdr+1]); i>0 
17788 26 26 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 20  && i<usableSize 
17789 26 26 20 63 6e 74 3c 31 30 30 30 30 3b 20 0a 20  && cnt<10000; . 
1778a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 29            cnt++)
1778b 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65  {.      int size
1778c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
1778d 61 5b 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69  a[i+2]);.      i
1778e 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20  nt j;.      if( 
1778f 28 69 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62  (i+size-1)>=usab
17790 6c 65 53 69 7a 65 20 7c 7c 20 69 3c 30 20 29 7b  leSize || i<0 ){
17791 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70  .        checkAp
17792 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
17793 30 2c 20 20 0a 20 20 20 20 20 20 20 20 20 20 20  0,  .           
17794 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74   "Corruption det
17795 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64  ected in cell %d
17796 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69   on page %d",i,i
17797 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d  Page,0);.      }
17798 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f  else{.        fo
17799 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a 3e  r(j=i+size-1; j>
1779a 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b  =i; j--) hit[j]+
1779b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  +;.      }.     
1779c 20 69 20 3d 20 67 65 74 32 62 79 74 65 28 26 64   i = get2byte(&d
1779d 61 74 61 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  ata[i]);.    }. 
1779e 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20     for(i=cnt=0; 
1779f 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b  i<usableSize; i+
177a0 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69  +){.      if( hi
177a1 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  t[i]==0 ){.     
177a2 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20     cnt++;.      
177a3 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d  }else if( hit[i]
177a4 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  >1 ){.        ch
177a5 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
177a6 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  eck, 0,.        
177a7 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73    "Multiple uses
177a8 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20   for byte %d of 
177a9 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61  page %d", i, iPa
177aa 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ge);.        bre
177ab 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
177ac 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64  }.    if( cnt!=d
177ad 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20  ata[hdr+7] ){.  
177ae 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
177af 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20  sg(pCheck, 0, . 
177b0 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65           "Fragme
177b1 6e 74 65 64 20 73 70 61 63 65 20 69 73 20 25 64  nted space is %d
177b2 20 62 79 74 65 20 72 65 70 6f 72 74 65 64 20 61   byte reported a
177b3 73 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22  s %d on page %d"
177b4 2c 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c  ,.          cnt,
177b5 20 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50   data[hdr+7], iP
177b6 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  age);.    }.  }.
177b7 63 68 65 63 6b 5f 70 61 67 65 5f 61 62 6f 72 74  check_page_abort
177b8 3a 0a 20 20 69 66 20 28 68 69 74 29 20 73 71 6c  :.  if (hit) sql
177b9 69 74 65 33 50 61 67 65 46 72 65 65 28 68 69 74  ite3PageFree(hit
177ba 29 3b 0a 0a 20 20 72 65 6c 65 61 73 65 50 61 67  );..  releasePag
177bb 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75  e(pPage);.  retu
177bc 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a 23 65  rn depth+1;.}.#e
177bd 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
177be 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
177bf 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ECK */..#ifndef 
177c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
177c1 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a  GRITY_CHECK./*.*
177c2 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
177c3 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 65 20 63  oes a complete c
177c4 68 65 63 6b 20 6f 66 20 74 68 65 20 67 69 76 65  heck of the give
177c5 6e 20 42 54 72 65 65 20 66 69 6c 65 2e 20 20 61  n BTree file.  a
177c6 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61 6e 20  Root[] is.** an 
177c7 61 72 72 61 79 20 6f 66 20 70 61 67 65 73 20 6e  array of pages n
177c8 75 6d 62 65 72 73 20 77 65 72 65 20 65 61 63 68  umbers were each
177c9 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
177ca 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  the root page of
177cb 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20 6e 52  .** a table.  nR
177cc 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65  oot is the numbe
177cd 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
177ce 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 57 72 69  aRoot..**.** Wri
177cf 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  te the number of
177d0 20 65 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a   error seen in *
177d1 70 6e 45 72 72 2e 20 20 45 78 63 65 70 74 20 66  pnErr.  Except f
177d2 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a  or some memory.*
177d3 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  * allocation err
177d4 6f 72 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d  ors,  an error m
177d5 65 73 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d  essage held in m
177d6 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
177d7 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73  rom.** malloc is
177d8 20 72 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e   returned if *pn
177d9 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e  Err is non-zero.
177da 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74    If *pnErr==0 t
177db 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72  hen NULL is.** r
177dc 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d  eturned.  If a m
177dd 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
177de 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e   error occurs, N
177df 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
177e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
177e1 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
177e2 42 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68  BtreeIntegrityCh
177e3 65 63 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c  eck(.  Btree *p,
177e4 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65       /* The btre
177e5 65 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20  e to be checked 
177e6 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c  */.  int *aRoot,
177e7 20 20 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f     /* An array o
177e8 66 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d  f root pages num
177e9 62 65 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64  bers for individ
177ea 75 61 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69  ual trees */.  i
177eb 6e 74 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20  nt nRoot,    /* 
177ec 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
177ed 73 20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a  s in aRoot[] */.
177ee 20 20 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20    int mxErr,    
177ef 2f 2a 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e  /* Stop reportin
177f0 67 20 65 72 72 6f 72 73 20 61 66 74 65 72 20 74  g errors after t
177f1 68 69 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e  his many */.  in
177f2 74 20 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57  t *pnErr    /* W
177f3 72 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65  rite number of e
177f4 72 72 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68  rrors seen to th
177f5 69 73 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29  is variable */.)
177f6 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e  {.  Pgno i;.  in
177f7 74 20 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72  t nRef;.  Integr
177f8 69 74 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20  ityCk sCheck;.  
177f9 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
177fa 70 2d 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a  p->pBt;.  char z
177fb 45 72 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c  Err[100];..  sql
177fc 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
177fd 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70  );.  pBt->db = p
177fe 2d 3e 64 62 3b 0a 20 20 6e 52 65 66 20 3d 20 73  ->db;.  nRef = s
177ff 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
17800 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
17801 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 42 74 72 65  ;.  if( lockBtre
17802 65 57 69 74 68 52 65 74 72 79 28 70 29 21 3d 53  eWithRetry(p)!=S
17803 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17804 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 20  *pnErr = 1;.    
17805 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
17806 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  e(p);.    return
17807 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
17808 28 30 2c 20 22 63 61 6e 6e 6f 74 20 61 63 71 75  (0, "cannot acqu
17809 69 72 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  ire a read lock 
1780a 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 22  on the database"
1780b 29 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e  );.  }.  sCheck.
1780c 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 73 43 68  pBt = pBt;.  sCh
1780d 65 63 6b 2e 70 50 61 67 65 72 20 3d 20 70 42 74  eck.pPager = pBt
1780e 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 43 68 65  ->pPager;.  sChe
1780f 63 6b 2e 6e 50 61 67 65 20 3d 20 70 61 67 65 72  ck.nPage = pager
17810 50 61 67 65 63 6f 75 6e 74 28 73 43 68 65 63 6b  Pagecount(sCheck
17811 2e 70 42 74 29 3b 0a 20 20 73 43 68 65 63 6b 2e  .pBt);.  sCheck.
17812 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20  mxErr = mxErr;. 
17813 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30   sCheck.nErr = 0
17814 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f  ;.  sCheck.mallo
17815 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a  cFailed = 0;.  *
17816 70 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28  pnErr = 0;.  if(
17817 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d 3d 30   sCheck.nPage==0
17818 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74   ){.    unlockBt
17819 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
1781a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1781b 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20  eeLeave(p);.    
1781c 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1781d 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20 73  sCheck.anRef = s
1781e 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 73  qlite3Malloc( (s
1781f 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a 73  Check.nPage+1)*s
17820 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e 52  izeof(sCheck.anR
17821 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  ef[0]) );.  if( 
17822 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29 7b  !sCheck.anRef ){
17823 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65  .    unlockBtree
17824 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20  IfUnused(pBt);. 
17825 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20     *pnErr = 1;. 
17826 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
17827 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74  eave(p);.    ret
17828 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72  urn 0;.  }.  for
17829 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e  (i=0; i<=sCheck.
1782a 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68  nPage; i++){ sCh
1782b 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30  eck.anRef[i] = 0
1782c 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e  ; }.  i = PENDIN
1782d 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
1782e 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63  ;.  if( i<=sChec
1782f 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73  k.nPage ){.    s
17830 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d  Check.anRef[i] =
17831 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   1;.  }.  sqlite
17832 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73  3StrAccumInit(&s
17833 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45  Check.errMsg, zE
17834 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29  rr, sizeof(zErr)
17835 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20  , 20000);..  /* 
17836 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72  Check the integr
17837 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c  ity of the freel
17838 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b  ist.  */.  check
17839 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c  List(&sCheck, 1,
1783a 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e   get4byte(&pBt->
1783b 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
1783c 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ]),.            
1783d 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70  get4byte(&pBt->p
1783e 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d  Page1->aData[36]
1783f 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73  ), "Main freelis
17840 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65  t: ");..  /* Che
17841 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65  ck all the table
17842 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  s..  */.  for(i=
17843 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20  0; (int)i<nRoot 
17844 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b  && sCheck.mxErr;
17845 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61   i++){.    if( a
17846 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e  Root[i]==0 ) con
17847 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53  tinue;.#ifndef S
17848 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
17849 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42  ACUUM.    if( pB
1784a 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26  t->autoVacuum &&
1784b 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20   aRoot[i]>1 ){. 
1784c 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70       checkPtrmap
1784d 28 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b  (&sCheck, aRoot[
1784e 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50  i], PTRMAP_ROOTP
1784f 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  AGE, 0, 0);.    
17850 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65  }.#endif.    che
17851 63 6b 54 72 65 65 50 61 67 65 28 26 73 43 68 65  ckTreePage(&sChe
17852 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c  ck, aRoot[i], "L
17853 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74  ist of tree root
17854 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  s: ");.  }..  /*
17855 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79   Make sure every
17856 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c   page in the fil
17857 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a  e is referenced.
17858 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20    */.  for(i=1; 
17859 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20  i<=sCheck.nPage 
1785a 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b  && sCheck.mxErr;
1785b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51   i++){.#ifdef SQ
1785c 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1785d 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68  CUUM.    if( sCh
1785e 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20  eck.anRef[i]==0 
1785f 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
17860 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c  pendMsg(&sCheck,
17861 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 20   0, "Page %d is 
17862 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b  never used", i);
17863 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20  .    }.#else.   
17864 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62   /* If the datab
17865 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
17866 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73  o-vacuum, make s
17867 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f  ure no tables co
17868 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66  ntain.    ** ref
17869 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74  erences to point
1786a 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20  er-map pages..  
1786b 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68    */.    if( sCh
1786c 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20  eck.anRef[i]==0 
1786d 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d  && .       (PTRM
1786e 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69  AP_PAGENO(pBt, i
1786f 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75  )!=i || !pBt->au
17870 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20  toVacuum) ){.   
17871 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
17872 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50  g(&sCheck, 0, "P
17873 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20  age %d is never 
17874 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d  used", i);.    }
17875 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e  .    if( sCheck.
17876 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a  anRef[i]!=0 && .
17877 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50         (PTRMAP_P
17878 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69  AGENO(pBt, i)==i
17879 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63   && pBt->autoVac
1787a 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68  uum) ){.      ch
1787b 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43  eckAppendMsg(&sC
1787c 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65  heck, 0, "Pointe
1787d 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 73  r map page %d is
1787e 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29   referenced", i)
1787f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
17880 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
17881 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73  re this analysis
17882 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61   did not leave a
17883 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 73  ny unref() pages
17884 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61  ..  ** This is a
17885 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69  n internal consi
17886 73 74 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e  stency check; an
17887 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b   integrity check
17888 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74  .  ** of the int
17889 65 67 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20  egrity check..  
1788a 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65  */.  unlockBtree
1788b 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20  IfUnused(pBt);. 
1788c 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65 66 20   if( NEVER(nRef 
1788d 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  != sqlite3PagerR
1788e 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61  efcount(pBt->pPa
1788f 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63 68 65  ger)) ){.    che
17890 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68  ckAppendMsg(&sCh
17891 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 22  eck, 0, .      "
17892 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  Outstanding page
17893 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72 6f 6d   count goes from
17894 20 25 64 20 74 6f 20 25 64 20 64 75 72 69 6e 67   %d to %d during
17895 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 22 2c   this analysis",
17896 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73 71 6c  .      nRef, sql
17897 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e  ite3PagerRefcoun
17898 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 0a 20  t(pBt->pPager). 
17899 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20     );.  }..  /* 
1789a 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20 72 65  Clean  up and re
1789b 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20 20 2a  port errors..  *
1789c 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  /.  sqlite3Btree
1789d 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71 6c 69  Leave(p);.  sqli
1789e 74 65 33 5f 66 72 65 65 28 73 43 68 65 63 6b 2e  te3_free(sCheck.
1789f 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20 73 43  anRef);.  if( sC
178a0 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65  heck.mallocFaile
178a1 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
178a2 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 26 73  StrAccumReset(&s
178a3 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20  Check.errMsg);. 
178a4 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65     *pnErr = sChe
178a5 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20 20 72  ck.nErr+1;.    r
178a6 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a  eturn 0;.  }.  *
178a7 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e  pnErr = sCheck.n
178a8 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68 65 63  Err;.  if( sChec
178a9 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71 6c 69  k.nErr==0 ) sqli
178aa 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74  te3StrAccumReset
178ab 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29  (&sCheck.errMsg)
178ac 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
178ad 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68  e3StrAccumFinish
178ae 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29  (&sCheck.errMsg)
178af 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
178b0 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
178b1 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a  ITY_CHECK */../*
178b2 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66  .** Return the f
178b3 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20  ull pathname of 
178b4 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64  the underlying d
178b5 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
178b6 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 66 69  .** The pager fi
178b7 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69  lename is invari
178b8 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74  ant as long as t
178b9 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f  he pager is.** o
178ba 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66  pen so it is saf
178bb 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74 68  e to access with
178bc 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65 64  out the BtShared
178bd 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54   mutex..*/.SQLIT
178be 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
178bf 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72  char *sqlite3Btr
178c0 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 42 74  eeGetFilename(Bt
178c1 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
178c2 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  t( p->pBt->pPage
178c3 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  r!=0 );.  return
178c4 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c   sqlite3PagerFil
178c5 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50  ename(p->pBt->pP
178c6 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ager);.}../*.** 
178c7 52 65 74 75 72 6e 20 74 68 65 20 70 61 74 68 6e  Return the pathn
178c8 61 6d 65 20 6f 66 20 74 68 65 20 64 69 72 65 63  ame of the direc
178c9 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69  tory that contai
178ca 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ns the database 
178cb 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  file..**.** The 
178cc 70 61 67 65 72 20 64 69 72 65 63 74 6f 72 79 20  pager directory 
178cd 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e  name is invarian
178ce 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65  t as long as the
178cf 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65   pager is.** ope
178d0 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 65 20  n so it is safe 
178d1 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f 75  to access withou
178d2 74 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d  t the BtShared m
178d3 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  utex..*/.SQLITE_
178d4 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
178d5 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
178d6 47 65 74 44 69 72 6e 61 6d 65 28 42 74 72 65 65  GetDirname(Btree
178d7 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
178d8 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d  p->pBt->pPager!=
178d9 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  0 );.  return sq
178da 6c 69 74 65 33 50 61 67 65 72 44 69 72 6e 61 6d  lite3PagerDirnam
178db 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72  e(p->pBt->pPager
178dc 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
178dd 72 6e 20 74 68 65 20 70 61 74 68 6e 61 6d 65 20  rn the pathname 
178de 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
178df 69 6c 65 20 66 6f 72 20 74 68 69 73 20 64 61 74  ile for this dat
178e0 61 62 61 73 65 2e 20 54 68 65 20 72 65 74 75 72  abase. The retur
178e1 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68  n.** value of th
178e2 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
178e3 65 20 73 61 6d 65 20 72 65 67 61 72 64 6c 65 73  e same regardles
178e4 73 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 65  s of whether the
178e5 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a   journal file.**
178e6 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65   has been create
178e7 64 20 6f 72 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20  d or not..**.** 
178e8 54 68 65 20 70 61 67 65 72 20 6a 6f 75 72 6e 61  The pager journa
178e9 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e  l filename is in
178ea 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20  variant as long 
178eb 61 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a  as the pager is.
178ec 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73  ** open so it is
178ed 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20   safe to access 
178ee 77 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68  without the BtSh
178ef 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53  ared mutex..*/.S
178f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
178f1 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
178f2 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c  3BtreeGetJournal
178f3 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  name(Btree *p){.
178f4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74    assert( p->pBt
178f5 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20  ->pPager!=0 );. 
178f6 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50   return sqlite3P
178f7 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28  agerJournalname(
178f8 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  p->pBt->pPager);
178f9 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
178fa 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f  TE_OMIT_VACUUM./
178fb 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 6f  *.** Copy the co
178fc 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f  mplete content o
178fd 66 20 70 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70  f pBtFrom into p
178fe 42 74 54 6f 2e 20 20 41 20 74 72 61 6e 73 61 63  BtTo.  A transac
178ff 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20  tion.** must be 
17900 61 63 74 69 76 65 20 66 6f 72 20 62 6f 74 68 20  active for both 
17901 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  files..**.** The
17902 20 73 69 7a 65 20 6f 66 20 66 69 6c 65 20 70 54   size of file pT
17903 6f 20 6d 61 79 20 62 65 20 72 65 64 75 63 65 64  o may be reduced
17904 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69   by this operati
17905 6f 6e 2e 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69  on..** If anythi
17906 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 74  ng goes wrong, t
17907 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
17908 6e 20 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64 20  n pTo is rolled 
17909 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  back. .**.** If 
1790a 73 75 63 63 65 73 73 66 75 6c 2c 20 43 6f 6d 6d  successful, Comm
1790b 69 74 50 68 61 73 65 4f 6e 65 28 29 20 6d 61 79  itPhaseOne() may
1790c 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 70 54   be called on pT
1790d 6f 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  o before returni
1790e 6e 67 2e 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ng. .** The call
1790f 65 72 20 73 68 6f 75 6c 64 20 66 69 6e 69 73 68  er should finish
17910 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 20   committing the 
17911 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 70  transaction on p
17912 54 6f 20 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a  To by calling.**
17913 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
17914 6d 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  mit()..*/.static
17915 20 69 6e 74 20 62 74 72 65 65 43 6f 70 79 46 69   int btreeCopyFi
17916 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42  le(Btree *pTo, B
17917 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20  tree *pFrom){.  
17918 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
17919 4f 4b 3b 0a 20 20 50 67 6e 6f 20 69 3b 0a 0a 20  OK;.  Pgno i;.. 
1791a 20 50 67 6e 6f 20 6e 46 72 6f 6d 50 61 67 65 3b   Pgno nFromPage;
1791b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1791c 66 20 70 61 67 65 73 20 69 6e 20 70 46 72 6f 6d  f pages in pFrom
1791d 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 54 6f 50 61   */.  Pgno nToPa
1791e 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ge;       /* Num
1791f 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
17920 70 54 6f 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4e  pTo */.  Pgno nN
17921 65 77 50 61 67 65 3b 20 20 20 20 20 20 2f 2a 20  ewPage;      /* 
17922 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
17923 69 6e 20 70 54 6f 20 61 66 74 65 72 20 74 68 65  in pTo after the
17924 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 50 67 6e 6f   copy */..  Pgno
17925 20 69 53 6b 69 70 3b 20 20 20 20 20 20 20 20 20   iSkip;         
17926 2f 2a 20 50 65 6e 64 69 6e 67 20 62 79 74 65 20  /* Pending byte 
17927 70 61 67 65 20 69 6e 20 70 54 6f 20 2a 2f 0a 20  page in pTo */. 
17928 20 69 6e 74 20 6e 54 6f 50 61 67 65 53 69 7a 65   int nToPageSize
17929 3b 20 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a  ;    /* Page siz
1792a 65 20 6f 66 20 70 54 6f 20 69 6e 20 62 79 74 65  e of pTo in byte
1792b 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 6f 6d  s */.  int nFrom
1792c 50 61 67 65 53 69 7a 65 3b 20 20 2f 2a 20 50 61  PageSize;  /* Pa
1792d 67 65 20 73 69 7a 65 20 6f 66 20 70 46 72 6f 6d  ge size of pFrom
1792e 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 0a 20 20   in bytes */..  
1792f 42 74 53 68 61 72 65 64 20 2a 70 42 74 54 6f 20  BtShared *pBtTo 
17930 3d 20 70 54 6f 2d 3e 70 42 74 3b 0a 20 20 42 74  = pTo->pBt;.  Bt
17931 53 68 61 72 65 64 20 2a 70 42 74 46 72 6f 6d 20  Shared *pBtFrom 
17932 3d 20 70 46 72 6f 6d 2d 3e 70 42 74 3b 0a 20 20  = pFrom->pBt;.  
17933 70 42 74 54 6f 2d 3e 64 62 20 3d 20 70 54 6f 2d  pBtTo->db = pTo-
17934 3e 64 62 3b 0a 20 20 70 42 74 46 72 6f 6d 2d 3e  >db;.  pBtFrom->
17935 64 62 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a  db = pFrom->db;.
17936 0a 20 20 6e 54 6f 50 61 67 65 53 69 7a 65 20 3d  .  nToPageSize =
17937 20 70 42 74 54 6f 2d 3e 70 61 67 65 53 69 7a 65   pBtTo->pageSize
17938 3b 0a 20 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a  ;.  nFromPageSiz
17939 65 20 3d 20 70 42 74 46 72 6f 6d 2d 3e 70 61 67  e = pBtFrom->pag
1793a 65 53 69 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74  eSize;..  assert
1793b 28 20 70 54 6f 2d 3e 69 6e 54 72 61 6e 73 3d 3d  ( pTo->inTrans==
1793c 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
1793d 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e   assert( pFrom->
1793e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
1793f 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 4e 45  RITE );.  if( NE
17940 56 45 52 28 70 42 74 54 6f 2d 3e 70 43 75 72 73  VER(pBtTo->pCurs
17941 6f 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  or) ){.    retur
17942 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  n SQLITE_BUSY;. 
17943 20 7d 0a 0a 20 20 6e 54 6f 50 61 67 65 20 3d 20   }..  nToPage = 
17944 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
17945 42 74 54 6f 29 3b 0a 20 20 6e 46 72 6f 6d 50 61  BtTo);.  nFromPa
17946 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f  ge = pagerPageco
17947 75 6e 74 28 70 42 74 46 72 6f 6d 29 3b 0a 20 20  unt(pBtFrom);.  
17948 69 53 6b 69 70 20 3d 20 50 45 4e 44 49 4e 47 5f  iSkip = PENDING_
17949 42 59 54 45 5f 50 41 47 45 28 70 42 74 54 6f 29  BYTE_PAGE(pBtTo)
1794a 3b 0a 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65  ;..  /* Variable
1794b 20 6e 4e 65 77 50 61 67 65 20 69 73 20 74 68 65   nNewPage is the
1794c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
1794d 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
1794e 72 65 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6e 74  re the.  ** cont
1794f 65 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20 75 73  ents of pFrom us
17950 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20  ing the current 
17951 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 70 54 6f  page-size of pTo
17952 2e 0a 20 20 2a 2f 0a 20 20 6e 4e 65 77 50 61 67  ..  */.  nNewPag
17953 65 20 3d 20 28 50 67 6e 6f 29 0a 20 20 20 20 20  e = (Pgno).     
17954 28 28 28 69 36 34 29 6e 46 72 6f 6d 50 61 67 65  (((i64)nFromPage
17955 2a 28 69 36 34 29 6e 46 72 6f 6d 50 61 67 65 53  *(i64)nFromPageS
17956 69 7a 65 2b 28 69 36 34 29 6e 54 6f 50 61 67 65  ize+(i64)nToPage
17957 53 69 7a 65 2d 31 29 2f 28 69 36 34 29 6e 54 6f  Size-1)/(i64)nTo
17958 50 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 66 6f  PageSize);..  fo
17959 72 28 69 3d 31 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=1; rc==SQLIT
1795a 45 5f 4f 4b 20 26 26 20 28 69 3c 3d 6e 54 6f 50  E_OK && (i<=nToP
1795b 61 67 65 20 7c 7c 20 69 3c 3d 6e 4e 65 77 50 61  age || i<=nNewPa
1795c 67 65 29 3b 20 69 2b 2b 29 7b 0a 0a 20 20 20 20  ge); i++){..    
1795d 2f 2a 20 4a 6f 75 72 6e 61 6c 20 74 68 65 20 6f  /* Journal the o
1795e 72 69 67 69 6e 61 6c 20 70 61 67 65 2e 0a 20 20  riginal page..  
1795f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 69 53 6b 69    **.    ** iSki
17960 70 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75  p is the page nu
17961 6d 62 65 72 20 6f 66 20 74 68 65 20 6c 6f 63 6b  mber of the lock
17962 69 6e 67 20 70 61 67 65 20 28 50 45 4e 44 49 4e  ing page (PENDIN
17963 47 5f 42 59 54 45 5f 50 41 47 45 29 0a 20 20 20  G_BYTE_PAGE).   
17964 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20   ** in database 
17965 2a 70 54 6f 20 28 62 65 66 6f 72 65 20 74 68 65  *pTo (before the
17966 20 63 6f 70 79 29 2e 20 54 68 69 73 20 70 61 67   copy). This pag
17967 65 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74  e is never writt
17968 65 6e 20 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20  en .    ** into 
17969 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1796a 2e 20 55 6e 6c 65 73 73 20 69 3d 3d 69 53 6b 69  . Unless i==iSki
1796b 70 20 6f 72 20 74 68 65 20 70 61 67 65 20 77 61  p or the page wa
1796c 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 70 72 65  s not.    ** pre
1796d 73 65 6e 74 20 69 6e 20 70 54 6f 20 62 65 66 6f  sent in pTo befo
1796e 72 65 20 74 68 65 20 63 6f 70 79 20 6f 70 65 72  re the copy oper
1796f 61 74 69 6f 6e 2c 20 6a 6f 75 72 6e 61 6c 20 70  ation, journal p
17970 61 67 65 20 69 20 66 72 6f 6d 20 70 54 6f 2e 0a  age i from pTo..
17971 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69      */.    if( i
17972 21 3d 69 53 6b 69 70 20 26 26 20 69 3c 3d 6e 54  !=iSkip && i<=nT
17973 6f 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 44  oPage ){.      D
17974 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 3d  bPage *pDbPage =
17975 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   0;.      rc = s
17976 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
17977 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20 69 2c  BtTo->pPager, i,
17978 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20   &pDbPage);.    
17979 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1797a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
1797b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1797c 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a  Write(pDbPage);.
1797d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
1797e 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3e 6e  SQLITE_OK && i>n
1797f 46 72 6f 6d 50 61 67 65 20 29 7b 0a 20 20 20 20  FromPage ){.    
17980 20 20 20 20 20 20 2f 2a 20 59 65 61 68 2e 20 20        /* Yeah.  
17981 49 74 20 73 65 65 6d 73 20 77 69 65 72 64 20 74  It seems wierd t
17982 6f 20 63 61 6c 6c 20 44 6f 6e 74 57 72 69 74 65  o call DontWrite
17983 28 29 20 72 69 67 68 74 20 61 66 74 65 72 20 57  () right after W
17984 72 69 74 65 28 29 2e 20 42 75 74 0a 20 20 20 20  rite(). But.    
17985 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 69 73        ** that is
17986 20 62 65 63 61 75 73 65 20 74 68 65 20 6e 61 6d   because the nam
17987 65 73 20 6f 66 20 74 68 6f 73 65 20 70 72 6f 63  es of those proc
17988 65 64 75 72 65 73 20 64 6f 20 6e 6f 74 20 65 78  edures do not ex
17989 61 63 74 6c 79 20 0a 20 20 20 20 20 20 20 20 20  actly .         
1798a 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 20 77 68   ** represent wh
1798b 61 74 20 74 68 65 79 20 64 6f 2e 20 20 57 72 69  at they do.  Wri
1798c 74 65 28 29 20 72 65 61 6c 6c 79 20 6d 65 61 6e  te() really mean
1798d 73 20 22 70 75 74 20 74 68 69 73 20 70 61 67 65  s "put this page
1798e 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20   in the.        
1798f 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f    ** rollback jo
17990 75 72 6e 61 6c 20 61 6e 64 20 6d 61 72 6b 20 69  urnal and mark i
17991 74 20 61 73 20 64 69 72 74 79 20 73 6f 20 74 68  t as dirty so th
17992 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 77 72  at it will be wr
17993 69 74 74 65 6e 0a 20 20 20 20 20 20 20 20 20 20  itten.          
17994 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ** to the databa
17995 73 65 20 66 69 6c 65 20 6c 61 74 65 72 2e 22 20  se file later." 
17996 20 44 6f 6e 74 57 72 69 74 65 28 29 20 75 6e 64   DontWrite() und
17997 6f 65 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70  oes the second p
17998 61 72 74 20 6f 66 0a 20 20 20 20 20 20 20 20 20  art of.         
17999 20 2a 2a 20 74 68 61 74 20 61 6e 64 20 70 72 65   ** that and pre
1799a 76 65 6e 74 73 20 74 68 65 20 70 61 67 65 20 66  vents the page f
1799b 72 6f 6d 20 62 65 69 6e 67 20 77 72 69 74 74 65  rom being writte
1799c 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  n to the databas
1799d 65 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20 20  e. The.         
1799e 20 2a 2a 20 70 61 67 65 20 69 73 20 73 74 69 6c   ** page is stil
1799f 6c 20 6f 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  l on the rollbac
179a0 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 6f 75 67  k journal, thoug
179a1 68 2e 20 20 41 6e 64 20 74 68 61 74 20 69 73 20  h.  And that is 
179a2 74 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 2a  the .          *
179a3 2a 20 77 68 6f 6c 65 20 70 6f 69 6e 74 20 6f 66  * whole point of
179a4 20 74 68 69 73 20 62 6c 6f 63 6b 3a 20 74 6f 20   this block: to 
179a5 70 75 74 20 70 61 67 65 73 20 6f 6e 20 74 68 65  put pages on the
179a6 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
179a7 6c 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  l. .          */
179a8 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
179a9 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
179aa 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a  Write(pDbPage);.
179ab 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
179ac 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
179ad 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ref(pDbPage);.  
179ae 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
179af 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74 68   /* Overwrite th
179b0 65 20 64 61 74 61 20 69 6e 20 70 61 67 65 20 69  e data in page i
179b1 20 6f 66 20 74 68 65 20 74 61 72 67 65 74 20 64   of the target d
179b2 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 69  atabase */.    i
179b3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
179b4 20 26 26 20 69 21 3d 69 53 6b 69 70 20 26 26 20   && i!=iSkip && 
179b5 69 3c 3d 6e 4e 65 77 50 61 67 65 20 29 7b 0a 0a  i<=nNewPage ){..
179b6 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 54        DbPage *pT
179b7 6f 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20  oPage = 0;.     
179b8 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
179b9 4f 66 66 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d  Off;..      rc =
179ba 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
179bb 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20  (pBtTo->pPager, 
179bc 69 2c 20 26 70 54 6f 50 61 67 65 29 3b 0a 20 20  i, &pToPage);.  
179bd 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
179be 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
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 54 6f 50 61 67 65 29  erWrite(pToPage)
179c1 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
179c2 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 69 4f   for(.        iO
179c3 66 66 3d 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65  ff=(i-1)*nToPage
179c4 53 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 72  Size; .        r
179c5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
179c6 69 4f 66 66 3c 69 2a 6e 54 6f 50 61 67 65 53 69  iOff<i*nToPageSi
179c7 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 69 4f 66  ze; .        iOf
179c8 66 20 2b 3d 20 6e 46 72 6f 6d 50 61 67 65 53 69  f += nFromPageSi
179c9 7a 65 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  ze.      ){.    
179ca 20 20 20 20 44 62 50 61 67 65 20 2a 70 46 72 6f      DbPage *pFro
179cb 6d 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20  mPage = 0;.     
179cc 20 20 20 50 67 6e 6f 20 69 46 72 6f 6d 20 3d 20     Pgno iFrom = 
179cd 28 50 67 6e 6f 29 28 69 4f 66 66 2f 6e 46 72 6f  (Pgno)(iOff/nFro
179ce 6d 50 61 67 65 53 69 7a 65 29 2b 31 3b 0a 0a 20  mPageSize)+1;.. 
179cf 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d         if( iFrom
179d0 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  ==PENDING_BYTE_P
179d1 41 47 45 28 70 42 74 46 72 6f 6d 29 20 29 7b 0a  AGE(pBtFrom) ){.
179d2 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
179d3 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  ue;.        }.. 
179d4 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
179d5 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 46  te3PagerGet(pBtF
179d6 72 6f 6d 2d 3e 70 50 61 67 65 72 2c 20 69 46 72  rom->pPager, iFr
179d7 6f 6d 2c 20 26 70 46 72 6f 6d 50 61 67 65 29 3b  om, &pFromPage);
179d8 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
179d9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
179da 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54          char *zT
179db 6f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  o = sqlite3Pager
179dc 47 65 74 44 61 74 61 28 70 54 6f 50 61 67 65 29  GetData(pToPage)
179dd 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72  ;.          char
179de 20 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65   *zFrom = sqlite
179df 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 46  3PagerGetData(pF
179e0 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20 20 20  romPage);.      
179e1 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 3b 0a 0a      int nCopy;..
179e2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 46            if( nF
179e3 72 6f 6d 50 61 67 65 53 69 7a 65 3e 3d 6e 54 6f  romPageSize>=nTo
179e4 50 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  PageSize ){.    
179e5 20 20 20 20 20 20 20 20 7a 46 72 6f 6d 20 2b 3d          zFrom +=
179e6 20 28 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65 53   ((i-1)*nToPageS
179e7 69 7a 65 20 2d 20 28 28 69 46 72 6f 6d 2d 31 29  ize - ((iFrom-1)
179e8 2a 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 29 29  *nFromPageSize))
179e9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 43  ;.            nC
179ea 6f 70 79 20 3d 20 6e 54 6f 50 61 67 65 53 69 7a  opy = nToPageSiz
179eb 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  e;.          }el
179ec 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
179ed 7a 54 6f 20 2b 3d 20 28 28 28 69 46 72 6f 6d 2d  zTo += (((iFrom-
179ee 31 29 2a 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65  1)*nFromPageSize
179ef 29 20 2d 20 28 69 2d 31 29 2a 6e 54 6f 50 61 67  ) - (i-1)*nToPag
179f0 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20  eSize);.        
179f1 20 20 20 20 6e 43 6f 70 79 20 3d 20 6e 46 72 6f      nCopy = nFro
179f2 6d 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20  mPageSize;.     
179f3 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
179f4 20 20 6d 65 6d 63 70 79 28 7a 54 6f 2c 20 7a 46    memcpy(zTo, zF
179f5 72 6f 6d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20  rom, nCopy);.   
179f6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
179f7 67 65 72 55 6e 72 65 66 28 70 46 72 6f 6d 50 61  gerUnref(pFromPa
179f8 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ge);.        }. 
179f9 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66       }..      if
179fa 28 20 70 54 6f 50 61 67 65 20 29 7b 0a 20 20 20  ( pToPage ){.   
179fb 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 20       MemPage *p 
179fc 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c  = (MemPage *)sql
179fd 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72  ite3PagerGetExtr
179fe 61 28 70 54 6f 50 61 67 65 29 3b 0a 20 20 20 20  a(pToPage);.    
179ff 20 20 20 20 70 2d 3e 69 73 49 6e 69 74 20 3d 20      p->isInit = 
17a00 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  0;.        sqlit
17a01 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 54 6f  e3PagerUnref(pTo
17a02 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Page);.      }. 
17a03 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
17a04 66 20 74 68 69 6e 67 73 20 68 61 76 65 20 77 6f  f things have wo
17a05 72 6b 65 64 20 73 6f 20 66 61 72 2c 20 74 68 65  rked so far, the
17a06 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d   database file m
17a07 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 0a 20  ay need to be . 
17a08 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 54   ** truncated. T
17a09 68 65 20 63 6f 6d 70 6c 65 78 20 70 61 72 74 20  he complex part 
17a0a 69 73 20 74 68 61 74 20 69 74 20 6d 61 79 20 6e  is that it may n
17a0b 65 65 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61  eed to be trunca
17a0c 74 65 64 20 74 6f 0a 20 20 2a 2a 20 61 20 73 69  ted to.  ** a si
17a0d 7a 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61  ze that is not a
17a0e 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70  n integer multip
17a0f 6c 65 20 6f 66 20 6e 54 6f 50 61 67 65 53 69 7a  le of nToPageSiz
17a10 65 20 2d 20 74 68 65 20 63 75 72 72 65 6e 74 0a  e - the current.
17a11 20 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 20 75    ** page size u
17a12 73 65 64 20 62 79 20 74 68 65 20 70 61 67 65 72  sed by the pager
17a13 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
17a14 20 42 2d 54 72 65 65 20 70 54 6f 2e 0a 20 20 2a   B-Tree pTo..  *
17a15 2a 0a 20 20 2a 2a 20 46 6f 72 20 65 78 61 6d 70  *.  ** For examp
17a16 6c 65 2c 20 73 61 79 20 74 68 65 20 70 61 67 65  le, say the page
17a17 2d 73 69 7a 65 20 6f 66 20 70 54 6f 20 69 73 20  -size of pTo is 
17a18 32 30 34 38 20 62 79 74 65 73 20 61 6e 64 20 74  2048 bytes and t
17a19 68 65 20 6f 72 69 67 69 6e 61 6c 20 0a 20 20 2a  he original .  *
17a1a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * number of page
17a1b 73 20 69 73 20 35 20 28 31 30 20 4b 42 20 66 69  s is 5 (10 KB fi
17a1c 6c 65 29 2e 20 49 66 20 70 46 72 6f 6d 20 68 61  le). If pFrom ha
17a1d 73 20 61 20 70 61 67 65 20 73 69 7a 65 20 6f 66  s a page size of
17a1e 20 31 30 32 34 20 0a 20 20 2a 2a 20 62 79 74 65   1024 .  ** byte
17a1f 73 20 61 6e 64 20 39 20 70 61 67 65 73 2c 20 74  s and 9 pages, t
17a20 68 65 6e 20 74 68 65 20 66 69 6c 65 20 6e 65 65  hen the file nee
17a21 64 73 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74  ds to be truncat
17a22 65 64 20 74 6f 20 39 4b 42 2e 0a 20 20 2a 2f 0a  ed to 9KB..  */.
17a23 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
17a24 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
17a25 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20 3d  e3_file *pFile =
17a26 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c   sqlite3PagerFil
17a27 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 29  e(pBtTo->pPager)
17a28 3b 0a 20 20 20 20 69 36 34 20 69 53 69 7a 65 20  ;.    i64 iSize 
17a29 3d 20 28 69 36 34 29 6e 46 72 6f 6d 50 61 67 65  = (i64)nFromPage
17a2a 53 69 7a 65 20 2a 20 28 69 36 34 29 6e 46 72 6f  Size * (i64)nFro
17a2b 6d 50 61 67 65 3b 0a 20 20 20 20 69 36 34 20 69  mPage;.    i64 i
17a2c 4e 6f 77 20 3d 20 28 69 36 34 29 28 28 6e 54 6f  Now = (i64)((nTo
17a2d 50 61 67 65 3e 6e 4e 65 77 50 61 67 65 29 3f 6e  Page>nNewPage)?n
17a2e 54 6f 50 61 67 65 3a 6e 4e 65 77 50 61 67 65 29  ToPage:nNewPage)
17a2f 20 2a 20 28 69 36 34 29 6e 54 6f 50 61 67 65 53   * (i64)nToPageS
17a30 69 7a 65 3b 20 0a 20 20 20 20 69 36 34 20 69 50  ize; .    i64 iP
17a31 65 6e 64 69 6e 67 20 3d 20 28 28 69 36 34 29 50  ending = ((i64)P
17a32 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
17a33 28 70 42 74 54 6f 29 2d 31 29 20 2a 28 69 36 34  (pBtTo)-1) *(i64
17a34 29 6e 54 6f 50 61 67 65 53 69 7a 65 3b 0a 0a 20  )nToPageSize;.. 
17a35 20 20 20 61 73 73 65 72 74 28 20 69 53 69 7a 65     assert( iSize
17a36 3c 3d 69 4e 6f 77 20 29 3b 0a 0a 20 20 20 20 2f  <=iNow );..    /
17a37 2a 20 43 6f 6d 6d 69 74 20 70 68 61 73 65 20 6f  * Commit phase o
17a38 6e 65 20 73 79 6e 63 73 20 74 68 65 20 6a 6f 75  ne syncs the jou
17a39 72 6e 61 6c 20 66 69 6c 65 20 61 73 73 6f 63 69  rnal file associ
17a3a 61 74 65 64 20 77 69 74 68 20 70 54 6f 20 0a 20  ated with pTo . 
17a3b 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67     ** containing
17a3c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
17a3d 74 61 2e 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  ta. It does not 
17a3e 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73  sync the databas
17a3f 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 69 74  e file.    ** it
17a40 73 65 6c 66 2e 20 41 66 74 65 72 20 64 6f 69 6e  self. After doin
17a41 67 20 74 68 69 73 20 69 74 20 69 73 20 73 61 66  g this it is saf
17a42 65 20 74 6f 20 75 73 65 20 4f 73 54 72 75 6e 63  e to use OsTrunc
17a43 61 74 65 28 29 20 61 6e 64 20 6f 74 68 65 72 0a  ate() and other.
17a44 20 20 20 20 2a 2a 20 66 69 6c 65 20 41 50 49 73      ** file APIs
17a45 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
17a46 20 66 69 6c 65 20 64 69 72 65 63 74 6c 79 2e 0a   file directly..
17a47 20 20 20 20 2a 2f 0a 20 20 20 20 70 42 74 54 6f      */.    pBtTo
17a48 2d 3e 64 62 20 3d 20 70 54 6f 2d 3e 64 62 3b 0a  ->db = pTo->db;.
17a49 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17a4a 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
17a4b 4f 6e 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 65  One(pBtTo->pPage
17a4c 72 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20 69 66  r, 0, 1);.    if
17a4d 28 20 69 53 69 7a 65 3c 69 4e 6f 77 20 26 26 20  ( iSize<iNow && 
17a4e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
17a4f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
17a50 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 46  te3OsTruncate(pF
17a51 69 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 20  ile, iSize);.   
17a52 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6c   }..    /* The l
17a53 6f 6f 70 20 74 68 61 74 20 63 6f 70 69 65 64 20  oop that copied 
17a54 64 61 74 61 20 66 72 6f 6d 20 64 61 74 61 62 61  data from databa
17a55 73 65 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 20  se pFrom to pTo 
17a56 64 69 64 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 70  did not.    ** p
17a57 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 63 6b  opulate the lock
17a58 69 6e 67 20 70 61 67 65 20 6f 66 20 64 61 74 61  ing page of data
17a59 62 61 73 65 20 70 54 6f 2e 20 49 66 20 74 68 65  base pTo. If the
17a5a 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 0a 20 20   page-size of.  
17a5b 20 20 2a 2a 20 70 46 72 6f 6d 20 69 73 20 73 6d    ** pFrom is sm
17a5c 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 61 74 20  aller than that 
17a5d 6f 66 20 70 54 6f 2c 20 74 68 69 73 20 6d 65 61  of pTo, this mea
17a5e 6e 73 20 73 6f 6d 65 20 64 61 74 61 20 77 69 6c  ns some data wil
17a5f 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61 76  l.    ** not hav
17a60 65 20 62 65 65 6e 20 63 6f 70 69 65 64 2e 20 0a  e been copied. .
17a61 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
17a62 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20  is block copies 
17a63 74 68 65 20 6d 69 73 73 69 6e 67 20 64 61 74 61  the missing data
17a64 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 70   from database p
17a65 46 72 6f 6d 20 74 6f 20 70 54 6f 20 0a 20 20 20  From to pTo .   
17a66 20 2a 2a 20 75 73 69 6e 67 20 66 69 6c 65 20 41   ** using file A
17a67 50 49 73 2e 20 54 68 69 73 20 69 73 20 73 61 66  PIs. This is saf
17a68 65 20 62 65 63 61 75 73 65 20 61 74 20 74 68 69  e because at thi
17a69 73 20 70 6f 69 6e 74 20 77 65 20 6b 6e 6f 77 20  s point we know 
17a6a 74 68 61 74 0a 20 20 20 20 2a 2a 20 61 6c 6c 20  that.    ** all 
17a6b 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  of the original 
17a6c 64 61 74 61 20 66 72 6f 6d 20 70 54 6f 20 68 61  data from pTo ha
17a6d 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 69 6e  s been synced in
17a6e 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 6a  to the .    ** j
17a6f 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 74 20  ournal file. At 
17a70 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 77 6f  this point it wo
17a71 75 6c 64 20 62 65 20 73 61 66 65 20 74 6f 20 64  uld be safe to d
17a72 6f 20 61 6e 79 74 68 69 6e 67 20 61 74 0a 20 20  o anything at.  
17a73 20 20 2a 2a 20 61 6c 6c 20 74 6f 20 74 68 65 20    ** all to the 
17a74 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 65 78  database file ex
17a75 63 65 70 74 20 74 72 75 6e 63 61 74 65 20 69 74  cept truncate it
17a76 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 2e 0a   to zero bytes..
17a77 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
17a78 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
17a79 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 3c 6e 54  nFromPageSize<nT
17a7a 6f 50 61 67 65 53 69 7a 65 20 26 26 20 69 53 69  oPageSize && iSi
17a7b 7a 65 3e 69 50 65 6e 64 69 6e 67 29 7b 0a 20 20  ze>iPending){.  
17a7c 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20      i64 iOff;.  
17a7d 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20 20      for(.       
17a7e 20 69 4f 66 66 3d 69 50 65 6e 64 69 6e 67 3b 20   iOff=iPending; 
17a7f 0a 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c  .        rc==SQL
17a80 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 28  ITE_OK && iOff<(
17a81 69 50 65 6e 64 69 6e 67 2b 6e 54 6f 50 61 67 65  iPending+nToPage
17a82 53 69 7a 65 29 3b 20 0a 20 20 20 20 20 20 20 20  Size); .        
17a83 69 4f 66 66 20 2b 3d 20 6e 46 72 6f 6d 50 61 67  iOff += nFromPag
17a84 65 53 69 7a 65 0a 20 20 20 20 20 20 29 7b 0a 20  eSize.      ){. 
17a85 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70         DbPage *p
17a86 46 72 6f 6d 50 61 67 65 20 3d 20 30 3b 0a 20 20  FromPage = 0;.  
17a87 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 6f 6d        Pgno iFrom
17a88 20 3d 20 28 50 67 6e 6f 29 28 69 4f 66 66 2f 6e   = (Pgno)(iOff/n
17a89 46 72 6f 6d 50 61 67 65 53 69 7a 65 29 2b 31 3b  FromPageSize)+1;
17a8a 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 46  ..        if( iF
17a8b 72 6f 6d 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  rom==PENDING_BYT
17a8c 45 5f 50 41 47 45 28 70 42 74 46 72 6f 6d 29 20  E_PAGE(pBtFrom) 
17a8d 7c 7c 20 69 46 72 6f 6d 3e 6e 46 72 6f 6d 50 61  || iFrom>nFromPa
17a8e 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ge ){.          
17a8f 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
17a90 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20    }..        rc 
17a91 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
17a92 74 28 70 42 74 46 72 6f 6d 2d 3e 70 50 61 67 65  t(pBtFrom->pPage
17a93 72 2c 20 69 46 72 6f 6d 2c 20 26 70 46 72 6f 6d  r, iFrom, &pFrom
17a94 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69  Page);.        i
17a95 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
17a96 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68   ){.          ch
17a97 61 72 20 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69  ar *zFrom = sqli
17a98 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
17a99 70 46 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20  pFromPage);.    
17a9a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
17a9b 65 33 4f 73 57 72 69 74 65 28 70 46 69 6c 65 2c  e3OsWrite(pFile,
17a9c 20 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d 50 61 67   zFrom, nFromPag
17a9d 65 53 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20 20  eSize, iOff);.  
17a9e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
17a9f 61 67 65 72 55 6e 72 65 66 28 70 46 72 6f 6d 50  agerUnref(pFromP
17aa0 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  age);.        }.
17aa1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
17aa2 7d 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65  }..  /* Sync the
17aa3 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
17aa4 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
17aa5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
17aa6 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  = sqlite3PagerSy
17aa7 6e 63 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72  nc(pBtTo->pPager
17aa8 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
17aa9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17aaa 20 20 70 42 74 54 6f 2d 3e 70 61 67 65 53 69 7a    pBtTo->pageSiz
17aab 65 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 7d 65  eFixed = 0;.  }e
17aac 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
17aad 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 54  BtreeRollback(pT
17aae 6f 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  o);.  }..  retur
17aaf 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  n rc;.}.SQLITE_P
17ab0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
17ab1 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28  e3BtreeCopyFile(
17ab2 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 74 72 65  Btree *pTo, Btre
17ab3 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74  e *pFrom){.  int
17ab4 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
17ab5 72 65 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a 20  reeEnter(pTo);. 
17ab6 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
17ab7 65 72 28 70 46 72 6f 6d 29 3b 0a 20 20 72 63 20  er(pFrom);.  rc 
17ab8 3d 20 62 74 72 65 65 43 6f 70 79 46 69 6c 65 28  = btreeCopyFile(
17ab9 70 54 6f 2c 20 70 46 72 6f 6d 29 3b 0a 20 20 73  pTo, pFrom);.  s
17aba 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
17abb 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74  (pFrom);.  sqlit
17abc 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f  e3BtreeLeave(pTo
17abd 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
17abe 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
17abf 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20  ITE_OMIT_VACUUM 
17ac0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
17ac1 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 74   non-zero if a t
17ac2 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
17ac3 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tive..*/.SQLITE_
17ac4 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
17ac5 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
17ac6 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61  s(Btree *p){.  a
17ac7 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 73  ssert( p==0 || s
17ac8 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
17ac9 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
17aca 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26  );.  return (p &
17acb 26 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  & (p->inTrans==T
17acc 52 41 4e 53 5f 57 52 49 54 45 29 29 3b 0a 7d 0a  RANS_WRITE));.}.
17acd 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f  ./*.** Return no
17ace 6e 2d 7a 65 72 6f 20 69 66 20 61 20 73 74 61 74  n-zero if a stat
17acf 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
17ad0 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a  n is active..*/.
17ad1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
17ad2 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
17ad3 73 49 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70  sInStmt(Btree *p
17ad4 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
17ad5 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
17ad6 74 65 78 28 70 29 20 29 3b 0a 20 20 72 65 74 75  tex(p) );.  retu
17ad7 72 6e 20 41 4c 57 41 59 53 28 70 2d 3e 70 42 74  rn ALWAYS(p->pBt
17ad8 29 20 26 26 20 70 2d 3e 70 42 74 2d 3e 69 6e 53  ) && p->pBt->inS
17ad9 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  tmt;.}../*.** Re
17ada 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66  turn non-zero if
17adb 20 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74   a read (or writ
17adc 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  e) transaction i
17add 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c  s active..*/.SQL
17ade 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
17adf 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
17ae0 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20  ReadTrans(Btree 
17ae1 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
17ae2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
17ae3 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
17ae4 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
17ae5 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e  ;.  return p->in
17ae6 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e  Trans!=TRANS_NON
17ae7 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  E;.}../*.** This
17ae8 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
17ae9 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
17aea 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20   blob of memory 
17aeb 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
17aec 2a 2a 20 61 20 73 69 6e 67 6c 65 20 73 68 61 72  ** a single shar
17aed 65 64 2d 62 74 72 65 65 2e 20 54 68 65 20 6d 65  ed-btree. The me
17aee 6d 6f 72 79 20 69 73 20 75 73 65 64 20 62 79 20  mory is used by 
17aef 63 6c 69 65 6e 74 20 63 6f 64 65 20 66 6f 72 20  client code for 
17af0 69 74 73 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f  its own.** purpo
17af1 73 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  ses (for example
17af2 2c 20 74 6f 20 73 74 6f 72 65 20 61 20 68 69 67  , to store a hig
17af3 68 2d 6c 65 76 65 6c 20 73 63 68 65 6d 61 20 61  h-level schema a
17af4 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a  ssociated with .
17af5 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d 62 74  ** the shared-bt
17af6 72 65 65 29 2e 20 54 68 65 20 62 74 72 65 65 20  ree). The btree 
17af7 6c 61 79 65 72 20 6d 61 6e 61 67 65 73 20 72 65  layer manages re
17af8 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67  ference counting
17af9 20 69 73 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54   issues..**.** T
17afa 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
17afb 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20  is is called on 
17afc 61 20 73 68 61 72 65 64 2d 62 74 72 65 65 2c 20  a shared-btree, 
17afd 6e 42 79 74 65 73 20 62 79 74 65 73 20 6f 66 20  nBytes bytes of 
17afe 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c  memory.** are al
17aff 6c 6f 63 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c  located, zeroed,
17b00 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 74 6f   and returned to
17b01 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72   the caller. For
17b02 20 65 61 63 68 20 73 75 62 73 65 71 75 65 6e 74   each subsequent
17b03 20 0a 2a 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42   .** call the nB
17b04 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69  ytes parameter i
17b05 73 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 61 20  s ignored and a 
17b06 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73  pointer to the s
17b07 61 6d 65 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d  ame blob.** of m
17b08 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e 20  emory returned. 
17b09 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42  .**.** If the nB
17b0a 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69  ytes parameter i
17b0b 73 20 30 20 61 6e 64 20 74 68 65 20 62 6c 6f 62  s 0 and the blob
17b0c 20 6f 66 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e   of memory has n
17b0d 6f 74 20 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61  ot yet been.** a
17b0e 6c 6c 6f 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c  llocated, a null
17b0f 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75   pointer is retu
17b10 72 6e 65 64 2e 20 49 66 20 74 68 65 20 62 6c 6f  rned. If the blo
17b11 62 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  b has already be
17b12 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c  en.** allocated,
17b13 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 20   it is returned 
17b14 61 73 20 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a  as normal..**.**
17b15 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65   Just before the
17b16 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 69 73   shared-btree is
17b17 20 63 6c 6f 73 65 64 2c 20 74 68 65 20 66 75 6e   closed, the fun
17b18 63 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20  ction passed as 
17b19 74 68 65 20 0a 2a 2a 20 78 46 72 65 65 20 61 72  the .** xFree ar
17b1a 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20  gument when the 
17b1b 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
17b1c 6e 20 77 61 73 20 6d 61 64 65 20 69 73 20 69 6e  n was made is in
17b1d 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a  voked on the .**
17b1e 20 62 6c 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74   blob of allocat
17b1f 65 64 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20  ed memory. This 
17b20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
17b21 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  not call sqlite3
17b22 5f 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68  _free().** on th
17b23 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62 74  e memory, the bt
17b24 72 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20 74  ree layer does t
17b25 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  hat..*/.SQLITE_P
17b26 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
17b27 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
17b28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42  Btree *p, int nB
17b29 79 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 65  ytes, void(*xFre
17b2a 65 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42  e)(void *)){.  B
17b2b 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
17b2c 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33  ->pBt;.  sqlite3
17b2d 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
17b2e 20 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68 65   if( !pBt->pSche
17b2f 6d 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b 0a  ma && nBytes ){.
17b30 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d 61      pBt->pSchema
17b31 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
17b32 5a 65 72 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20  Zero(nBytes);.  
17b33 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65    pBt->xFreeSche
17b34 6d 61 20 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a  ma = xFree;.  }.
17b35 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
17b36 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
17b37 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d   pBt->pSchema;.}
17b38 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
17b39 72 75 65 20 69 66 20 61 6e 6f 74 68 65 72 20 75  rue if another u
17b3a 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ser of the same 
17b3b 73 68 61 72 65 64 20 62 74 72 65 65 20 61 73 20  shared btree as 
17b3c 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  the argument.** 
17b3d 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61 6e 20  handle holds an 
17b3e 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f  exclusive lock o
17b3f 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
17b40 74 65 72 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ter table..*/.SQ
17b41 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
17b42 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
17b43 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20  emaLocked(Btree 
17b44 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  *p){.  int rc;. 
17b45 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17b46 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
17b47 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
17b48 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
17b49 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 71 75 65  (p);.  rc = (que
17b4a 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d  ryTableLock(p, M
17b4b 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44  ASTER_ROOT, READ
17b4c 5f 4c 4f 43 4b 29 21 3d 53 51 4c 49 54 45 5f 4f  _LOCK)!=SQLITE_O
17b4d 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  K);.  sqlite3Btr
17b4e 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
17b4f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66  turn rc;.}...#if
17b50 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17b51 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a  _SHARED_CACHE./*
17b52 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63  .** Obtain a loc
17b53 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77  k on the table w
17b54 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69  hose root page i
17b55 73 20 69 54 61 62 2e 20 20 54 68 65 0a 2a 2a 20  s iTab.  The.** 
17b56 6c 6f 63 6b 20 69 73 20 61 20 77 72 69 74 65 20  lock is a write 
17b57 6c 6f 63 6b 20 69 66 20 69 73 57 72 69 74 65 6c  lock if isWritel
17b58 6f 63 6b 20 69 73 20 74 72 75 65 20 6f 72 20 61  ock is true or a
17b59 20 72 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20 69 66   read lock.** if
17b5a 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2f   it is false..*/
17b5b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
17b5c 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
17b5d 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20  LockTable(Btree 
17b5e 2a 70 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 38  *p, int iTab, u8
17b5f 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 7b 0a 20   isWriteLock){. 
17b60 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
17b61 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 73 68  _OK;.  if( p->sh
17b62 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 75 38  arable ){.    u8
17b63 20 6c 6f 63 6b 54 79 70 65 20 3d 20 52 45 41 44   lockType = READ
17b64 5f 4c 4f 43 4b 20 2b 20 69 73 57 72 69 74 65 4c  _LOCK + isWriteL
17b65 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ock;.    assert(
17b66 20 52 45 41 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52   READ_LOCK+1==WR
17b67 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  ITE_LOCK );.    
17b68 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65 4c  assert( isWriteL
17b69 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74  ock==0 || isWrit
17b6a 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20  eLock==1 );.    
17b6b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
17b6c 72 28 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 71  r(p);.    rc = q
17b6d 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  ueryTableLock(p,
17b6e 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29   iTab, lockType)
17b6f 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
17b70 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17b71 20 72 63 20 3d 20 6c 6f 63 6b 54 61 62 6c 65 28   rc = lockTable(
17b72 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70  p, iTab, lockTyp
17b73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  e);.    }.    sq
17b74 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
17b75 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p);.  }.  return
17b76 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23   rc;.}.#endif..#
17b77 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
17b78 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a  IT_INCRBLOB./*.*
17b79 2a 20 41 72 67 75 6d 65 6e 74 20 70 43 73 72 20  * Argument pCsr 
17b7a 6d 75 73 74 20 62 65 20 61 20 63 75 72 73 6f 72  must be a cursor
17b7b 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74   opened for writ
17b7c 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e  ing on an .** IN
17b7d 54 4b 45 59 20 74 61 62 6c 65 20 63 75 72 72 65  TKEY table curre
17b7e 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61 74  ntly pointing at
17b7f 20 61 20 76 61 6c 69 64 20 74 61 62 6c 65 20 65   a valid table e
17b80 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66  ntry. .** This f
17b81 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73  unction modifies
17b82 20 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64   the data stored
17b83 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 61 74   as part of that
17b84 20 65 6e 74 72 79 2e 0a 2a 2a 20 4f 6e 6c 79 20   entry..** Only 
17b85 74 68 65 20 64 61 74 61 20 63 6f 6e 74 65 6e 74  the data content
17b86 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64   may only be mod
17b87 69 66 69 65 64 2c 20 69 74 20 69 73 20 6e 6f 74  ified, it is not
17b88 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20   possible.** to 
17b89 63 68 61 6e 67 65 20 74 68 65 20 6c 65 6e 67 74  change the lengt
17b8a 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 73 74  h of the data st
17b8b 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ored..*/.SQLITE_
17b8c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
17b8d 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28  te3BtreePutData(
17b8e 42 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20  BtCursor *pCsr, 
17b8f 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
17b90 61 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20  amt, void *z){. 
17b91 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
17b92 6f 6c 64 73 4d 75 74 65 78 28 70 43 73 72 29 20  oldsMutex(pCsr) 
17b93 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
17b94 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
17b95 70 43 73 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCsr->pBtree->db
17b96 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
17b97 73 65 72 74 28 70 43 73 72 2d 3e 69 73 49 6e 63  sert(pCsr->isInc
17b98 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 0a 20  rblobHandle);.. 
17b99 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
17b9a 73 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a 20 20  sition(pCsr);.  
17b9b 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 65 53  assert( pCsr->eS
17b9c 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 52 45 51  tate!=CURSOR_REQ
17b9d 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66  UIRESEEK );.  if
17b9e 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d  ( pCsr->eState!=
17b9f 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a  CURSOR_VALID ){.
17ba0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
17ba1 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 0a 20 20  E_ABORT;.  }..  
17ba2 2f 2a 20 43 68 65 63 6b 20 73 6f 6d 65 20 70 72  /* Check some pr
17ba3 65 63 6f 6e 64 69 74 69 6f 6e 73 3a 20 0a 20 20  econditions: .  
17ba4 2a 2a 20 20 20 28 61 29 20 74 68 65 20 63 75 72  **   (a) the cur
17ba5 73 6f 72 20 69 73 20 6f 70 65 6e 20 66 6f 72 20  sor is open for 
17ba6 77 72 69 74 69 6e 67 2c 0a 20 20 2a 2a 20 20 20  writing,.  **   
17ba7 28 62 29 20 74 68 65 72 65 20 69 73 20 6e 6f 20  (b) there is no 
17ba8 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  read-lock on the
17ba9 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6d 6f 64   table being mod
17baa 69 66 69 65 64 20 61 6e 64 0a 20 20 2a 2a 20 20  ified and.  **  
17bab 20 28 63 29 20 74 68 65 20 63 75 72 73 6f 72 20   (c) the cursor 
17bac 70 6f 69 6e 74 73 20 61 74 20 61 20 76 61 6c 69  points at a vali
17bad 64 20 72 6f 77 20 6f 66 20 61 6e 20 69 6e 74 4b  d row of an intK
17bae 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20  ey table..  */. 
17baf 20 69 66 28 20 21 70 43 73 72 2d 3e 77 72 46 6c   if( !pCsr->wrFl
17bb0 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ag ){.    return
17bb1 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
17bb2 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
17bb3 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72 65 61 64  !pCsr->pBt->read
17bb4 4f 6e 6c 79 20 0a 20 20 20 20 20 20 20 20 20 20  Only .          
17bb5 26 26 20 70 43 73 72 2d 3e 70 42 74 2d 3e 69 6e  && pCsr->pBt->in
17bb6 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
17bb7 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66  NS_WRITE );.  if
17bb8 28 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73  ( checkReadLocks
17bb9 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c 20 70  (pCsr->pBtree, p
17bba 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70  Csr->pgnoRoot, p
17bbb 43 73 72 2c 20 30 29 20 29 7b 0a 20 20 20 20 72  Csr, 0) ){.    r
17bbc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
17bbd 4b 45 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 6c  KED; /* The tabl
17bbe 65 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f  e pCur points to
17bbf 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b   has a read lock
17bc0 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43   */.  }.  if( pC
17bc1 73 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  sr->eState==CURS
17bc2 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 21 70  OR_INVALID || !p
17bc3 43 73 72 2d 3e 61 70 50 61 67 65 5b 70 43 73 72  Csr->apPage[pCsr
17bc4 2d 3e 69 50 61 67 65 5d 2d 3e 69 6e 74 4b 65 79  ->iPage]->intKey
17bc5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
17bc6 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
17bc7 0a 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73  ..  return acces
17bc8 73 50 61 79 6c 6f 61 64 28 70 43 73 72 2c 20 6f  sPayload(pCsr, o
17bc9 66 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73  ffset, amt, (uns
17bca 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 2c 20  igned char *)z, 
17bcb 30 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  0, 1);.}../* .**
17bcc 20 53 65 74 20 61 20 66 6c 61 67 20 6f 6e 20 74   Set a flag on t
17bcd 68 69 73 20 63 75 72 73 6f 72 20 74 6f 20 63 61  his cursor to ca
17bce 63 68 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e  che the location
17bcf 73 20 6f 66 20 70 61 67 65 73 20 66 72 6f 6d 20  s of pages from 
17bd0 74 68 65 20 0a 2a 2a 20 6f 76 65 72 66 6c 6f 77  the .** overflow
17bd1 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 63 75   list for the cu
17bd2 72 72 65 6e 74 20 72 6f 77 2e 20 54 68 69 73 20  rrent row. This 
17bd3 69 73 20 75 73 65 64 20 62 79 20 63 75 72 73 6f  is used by curso
17bd4 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72  rs opened.** for
17bd5 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f   incremental blo
17bd6 62 20 49 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a  b IO only..**.**
17bd7 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
17bd8 65 74 73 20 61 20 66 6c 61 67 20 6f 6e 6c 79 2e  ets a flag only.
17bd9 20 54 68 65 20 61 63 74 75 61 6c 20 70 61 67 65   The actual page
17bda 20 6c 6f 63 61 74 69 6f 6e 20 63 61 63 68 65 0a   location cache.
17bdb 2a 2a 20 28 73 74 6f 72 65 64 20 69 6e 20 42 74  ** (stored in Bt
17bdc 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77  Cursor.aOverflow
17bdd 5b 5d 29 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  []) is allocated
17bde 20 61 6e 64 20 75 73 65 64 20 62 79 20 66 75 6e   and used by fun
17bdf 63 74 69 6f 6e 0a 2a 2a 20 61 63 63 65 73 73 50  ction.** accessP
17be0 61 79 6c 6f 61 64 28 29 20 28 74 68 65 20 77 6f  ayload() (the wo
17be1 72 6b 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f  rker function fo
17be2 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  r sqlite3BtreeDa
17be3 74 61 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69  ta() and.** sqli
17be4 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28  te3BtreePutData(
17be5 29 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ))..*/.SQLITE_PR
17be6 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
17be7 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72  e3BtreeCacheOver
17be8 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 70  flow(BtCursor *p
17be9 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Cur){.  assert( 
17bea 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
17beb 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
17bec 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
17bed 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74  x_held(pCur->pBt
17bee 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  ree->db->mutex) 
17bef 29 3b 0a 20 20 61 73 73 65 72 74 28 21 70 43 75  );.  assert(!pCu
17bf0 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  r->isIncrblobHan
17bf1 64 6c 65 29 3b 0a 20 20 61 73 73 65 72 74 28 21  dle);.  assert(!
17bf2 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29  pCur->aOverflow)
17bf3 3b 0a 20 20 70 43 75 72 2d 3e 69 73 49 6e 63 72  ;.  pCur->isIncr
17bf4 62 6c 6f 62 48 61 6e 64 6c 65 20 3d 20 31 3b 0a  blobHandle = 1;.
17bf5 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
17bf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
17bf7 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   btree.c *******
17bf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17bf9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17bfa 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
17bfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
17bfc 66 69 6c 65 20 76 64 62 65 6d 65 6d 2e 63 20 2a  file vdbemem.c *
17bfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17bfe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17bff 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
17c00 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a  2004 May 26.**.*
17c01 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
17c02 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
17c03 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
17c04 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
17c05 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
17c06 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
17c07 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
17c08 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
17c09 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
17c0a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
17c0b 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
17c0c 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
17c0d 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
17c0e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
17c0f 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
17c10 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
17c11 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
17c12 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
17c13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
17c17 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
17c18 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20  ins code use to 
17c19 6d 61 6e 69 70 75 6c 61 74 65 20 22 4d 65 6d 22  manipulate "Mem"
17c1a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 20 22   structure.  A "
17c1b 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72 65 73 20 61  Mem".** stores a
17c1c 20 73 69 6e 67 6c 65 20 76 61 6c 75 65 20 69 6e   single value in
17c1d 20 74 68 65 20 56 44 42 45 2e 20 20 4d 65 6d 20   the VDBE.  Mem 
17c1e 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72  is an opaque str
17c1f 75 63 74 75 72 65 20 76 69 73 69 62 6c 65 0a 2a  ucture visible.*
17c20 2a 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 74 68  * only within th
17c21 65 20 56 44 42 45 2e 20 20 49 6e 74 65 72 66 61  e VDBE.  Interfa
17c22 63 65 20 72 6f 75 74 69 6e 65 73 20 72 65 66 65  ce routines refe
17c23 72 20 74 6f 20 61 20 4d 65 6d 20 75 73 69 6e 67  r to a Mem using
17c24 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 73 71 6c   the.** name sql
17c25 69 74 65 5f 76 61 6c 75 65 0a 2a 2a 0a 2a 2a 20  ite_value.**.** 
17c26 24 49 64 3a 20 76 64 62 65 6d 65 6d 2e 63 2c 76  $Id: vdbemem.c,v
17c27 20 31 2e 31 33 34 20 32 30 30 39 2f 30 31 2f 30   1.134 2009/01/0
17c28 35 20 32 32 3a 33 30 3a 33 39 20 64 72 68 20 45  5 22:30:39 drh E
17c29 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  xp $.*/../*.** C
17c2a 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 4d  all sqlite3VdbeM
17c2b 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 20 6f  emExpandBlob() o
17c2c 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 76  n the supplied v
17c2d 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d 2a 29  alue (type Mem*)
17c2e 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69 72 65  .** P if require
17c2f 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 65 78  d..*/.#define ex
17c30 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 28 50  pandBlob(P) (((P
17c31 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 72  )->flags&MEM_Zer
17c32 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65 4d 65  o)?sqlite3VdbeMe
17c33 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 3a 30  mExpandBlob(P):0
17c34 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d  )../*.** If pMem
17c35 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 20 77 69   is an object wi
17c36 74 68 20 61 20 76 61 6c 69 64 20 73 74 72 69 6e  th a valid strin
17c37 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  g representation
17c38 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  , this routine.*
17c39 2a 20 65 6e 73 75 72 65 73 20 74 68 65 20 69 6e  * ensures the in
17c3a 74 65 72 6e 61 6c 20 65 6e 63 6f 64 69 6e 67 20  ternal encoding 
17c3b 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 72  for the string r
17c3c 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73  epresentation is
17c3d 0a 2a 2a 20 27 64 65 73 69 72 65 64 45 6e 63 27  .** 'desiredEnc'
17c3e 2c 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f  , one of SQLITE_
17c3f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 55 54 46  UTF8, SQLITE_UTF
17c40 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55  16LE or SQLITE_U
17c41 54 46 31 36 42 45 2e 0a 2a 2a 0a 2a 2a 20 49 66  TF16BE..**.** If
17c42 20 70 4d 65 6d 20 69 73 20 6e 6f 74 20 61 20 73   pMem is not a s
17c43 74 72 69 6e 67 20 6f 62 6a 65 63 74 2c 20 6f 72  tring object, or
17c44 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66   the encoding of
17c45 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 72   the string.** r
17c46 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73  epresentation is
17c47 20 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20   already stored 
17c48 75 73 69 6e 67 20 74 68 65 20 72 65 71 75 65 73  using the reques
17c49 74 65 64 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68  ted encoding, th
17c4a 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  en this.** routi
17c4b 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
17c4c 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  *.** SQLITE_OK i
17c4d 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  s returned if th
17c4e 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  e conversion is 
17c4f 73 75 63 63 65 73 73 66 75 6c 20 28 6f 72 20 6e  successful (or n
17c50 6f 74 20 72 65 71 75 69 72 65 64 29 2e 0a 2a 2a  ot required)..**
17c51 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61   SQLITE_NOMEM ma
17c52 79 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66  y be returned if
17c53 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
17c54 73 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73  s during convers
17c55 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 66  ion.** between f
17c56 6f 72 6d 61 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ormats..*/.SQLIT
17c57 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
17c58 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
17c59 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a 70 4d 65  ncoding(Mem *pMe
17c5a 6d 2c 20 69 6e 74 20 64 65 73 69 72 65 64 45 6e  m, int desiredEn
17c5b 63 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  c){.  int rc;.  
17c5c 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
17c5d 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29  lags&MEM_RowSet)
17c5e 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
17c5f 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
17c60 49 54 45 5f 55 54 46 38 20 7c 7c 20 64 65 73 69  ITE_UTF8 || desi
17c61 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  redEnc==SQLITE_U
17c62 54 46 31 36 4c 45 0a 20 20 20 20 20 20 20 20 20  TF16LE.         
17c63 20 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d    || desiredEnc=
17c64 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
17c65 29 3b 0a 20 20 69 66 28 20 21 28 70 4d 65 6d 2d  );.  if( !(pMem-
17c66 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20  >flags&MEM_Str) 
17c67 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65  || pMem->enc==de
17c68 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20 20 20  siredEnc ){.    
17c69 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
17c6a 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
17c6b 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
17c6c 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
17c6d 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
17c6e 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  x) );.#ifdef SQL
17c6f 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20  ITE_OMIT_UTF16. 
17c70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
17c71 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 20 20 2f  RROR;.#else..  /
17c72 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29  * MemTranslate()
17c73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49   may return SQLI
17c74 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 45 5f  TE_OK or SQLITE_
17c75 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45 4d 20  NOMEM. If NOMEM 
17c76 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20 20 2a  is returned,.  *
17c77 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63 6f 64  * then the encod
17c78 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ing of the value
17c79 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 63 68   may not have ch
17c7a 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63  anged..  */.  rc
17c7b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
17c7c 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65 6d 2c  mTranslate(pMem,
17c7d 20 28 75 38 29 64 65 73 69 72 65 64 45 6e 63 29   (u8)desiredEnc)
17c7e 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53  ;.  assert(rc==S
17c7f 51 4c 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 72  QLITE_OK    || r
17c80 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29  c==SQLITE_NOMEM)
17c81 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53  ;.  assert(rc==S
17c82 51 4c 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 70  QLITE_OK    || p
17c83 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65  Mem->enc!=desire
17c84 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28  dEnc);.  assert(
17c85 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
17c86 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64   || pMem->enc==d
17c87 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 72 65  esiredEnc);.  re
17c88 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a  turn rc;.#endif.
17c89 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
17c8a 72 65 20 70 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74  re pMem->z point
17c8b 73 20 74 6f 20 61 20 77 72 69 74 61 62 6c 65 20  s to a writable 
17c8c 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 74  allocation of at
17c8d 20 6c 65 61 73 74 20 0a 2a 2a 20 6e 20 62 79 74   least .** n byt
17c8e 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  es..**.** If the
17c8f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 75 72   memory cell cur
17c90 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20  rently contains 
17c91 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 64  string or blob d
17c92 61 74 61 0a 2a 2a 20 61 6e 64 20 74 68 65 20 74  ata.** and the t
17c93 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 70 61  hird argument pa
17c94 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e  ssed to this fun
17c95 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74  ction is true, t
17c96 68 65 20 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63  he .** current c
17c97 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65  ontent of the ce
17c98 6c 6c 20 69 73 20 70 72 65 73 65 72 76 65 64 2e  ll is preserved.
17c99 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d   Otherwise, it m
17c9a 61 79 0a 2a 2a 20 62 65 20 64 69 73 63 61 72 64  ay.** be discard
17c9b 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ed.  .**.** This
17c9c 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74   function sets t
17c9d 68 65 20 4d 45 4d 5f 44 79 6e 20 66 6c 61 67 20  he MEM_Dyn flag 
17c9e 61 6e 64 20 63 6c 65 61 72 73 20 61 6e 79 20 78  and clears any x
17c9f 44 65 6c 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  Del callback..**
17ca0 20 49 74 20 61 6c 73 6f 20 63 6c 65 61 72 73 20   It also clears 
17ca1 4d 45 4d 5f 45 70 68 65 6d 20 61 6e 64 20 4d 45  MEM_Ephem and ME
17ca2 4d 5f 53 74 61 74 69 63 2e 20 49 66 20 74 68 65  M_Static. If the
17ca3 20 70 72 65 73 65 72 76 65 20 66 6c 61 67 20 69   preserve flag i
17ca4 73 20 0a 2a 2a 20 6e 6f 74 20 73 65 74 2c 20 4d  s .** not set, M
17ca5 65 6d 2e 6e 20 69 73 20 7a 65 72 6f 65 64 2e 0a  em.n is zeroed..
17ca6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17ca7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
17ca8 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d  eMemGrow(Mem *pM
17ca9 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70  em, int n, int p
17caa 72 65 73 65 72 76 65 29 7b 0a 20 20 61 73 73 65  reserve){.  asse
17cab 72 74 28 20 31 20 3e 3d 0a 20 20 20 20 28 28 70  rt( 1 >=.    ((p
17cac 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20  Mem->zMalloc && 
17cad 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70  pMem->zMalloc==p
17cae 4d 65 6d 2d 3e 7a 29 20 3f 20 31 20 3a 20 30 29  Mem->z) ? 1 : 0)
17caf 20 2b 0a 20 20 20 20 28 28 28 70 4d 65 6d 2d 3e   +.    (((pMem->
17cb0 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 26 26  flags&MEM_Dyn)&&
17cb1 70 4d 65 6d 2d 3e 78 44 65 6c 29 20 3f 20 31 20  pMem->xDel) ? 1 
17cb2 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 70 4d  : 0) + .    ((pM
17cb3 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70  em->flags&MEM_Ep
17cb4 68 65 6d 29 20 3f 20 31 20 3a 20 30 29 20 2b 20  hem) ? 1 : 0) + 
17cb5 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61  .    ((pMem->fla
17cb6 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 29 20 3f  gs&MEM_Static) ?
17cb7 20 31 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20 61   1 : 0).  );.  a
17cb8 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
17cb9 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  ags&MEM_RowSet)=
17cba 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 6e 3c 33  =0 );..  if( n<3
17cbb 32 20 29 20 6e 20 3d 20 33 32 3b 0a 20 20 69 66  2 ) n = 32;.  if
17cbc 28 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  ( sqlite3DbMallo
17cbd 63 53 69 7a 65 28 70 4d 65 6d 2d 3e 64 62 2c 20  cSize(pMem->db, 
17cbe 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3c 6e  pMem->zMalloc)<n
17cbf 20 29 7b 0a 20 20 20 20 69 66 28 20 70 72 65 73   ){.    if( pres
17cc0 65 72 76 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 3d  erve && pMem->z=
17cc1 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29  =pMem->zMalloc )
17cc2 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20  {.      pMem->z 
17cc3 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  = pMem->zMalloc 
17cc4 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
17cc5 6f 63 4f 72 46 72 65 65 28 70 4d 65 6d 2d 3e 64  ocOrFree(pMem->d
17cc6 62 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6e 29 3b 0a  b, pMem->z, n);.
17cc7 20 20 20 20 20 20 70 72 65 73 65 72 76 65 20 3d        preserve =
17cc8 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
17cc9 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
17cca 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65  ee(pMem->db, pMe
17ccb 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20  m->zMalloc);.   
17ccc 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63     pMem->zMalloc
17ccd 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
17cce 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64 62 2c 20  ocRaw(pMem->db, 
17ccf 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  n);.    }.  }.. 
17cd0 20 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26   if( preserve &&
17cd1 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d   pMem->z && pMem
17cd2 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65  ->zMalloc && pMe
17cd3 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c  m->z!=pMem->zMal
17cd4 6c 6f 63 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  loc ){.    memcp
17cd5 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c  y(pMem->zMalloc,
17cd6 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e   pMem->z, pMem->
17cd7 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d  n);.  }.  if( pM
17cd8 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  em->flags&MEM_Dy
17cd9 6e 20 26 26 20 70 4d 65 6d 2d 3e 78 44 65 6c 20  n && pMem->xDel 
17cda 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65  ){.    pMem->xDe
17cdb 6c 28 28 76 6f 69 64 20 2a 29 28 70 4d 65 6d 2d  l((void *)(pMem-
17cdc 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65  >z));.  }..  pMe
17cdd 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61  m->z = pMem->zMa
17cde 6c 6c 6f 63 3b 0a 20 20 69 66 28 20 70 4d 65 6d  lloc;.  if( pMem
17cdf 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4d  ->z==0 ){.    pM
17ce0 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
17ce1 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Null;.  }else{. 
17ce2 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26     pMem->flags &
17ce3 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45  = ~(MEM_Ephem|ME
17ce4 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 7d 0a 20  M_Static);.  }. 
17ce5 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b   pMem->xDel = 0;
17ce6 0a 20 20 72 65 74 75 72 6e 20 28 70 4d 65 6d 2d  .  return (pMem-
17ce7 3e 7a 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  >z ? SQLITE_OK :
17ce8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a   SQLITE_NOMEM);.
17ce9 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68  }../*.** Make th
17cea 65 20 67 69 76 65 6e 20 4d 65 6d 20 6f 62 6a 65  e given Mem obje
17ceb 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 20 49 6e 20  ct MEM_Dyn.  In 
17cec 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 61 6b  other words, mak
17ced 65 20 69 74 20 73 6f 0a 2a 2a 20 74 68 61 74 20  e it so.** that 
17cee 61 6e 79 20 54 45 58 54 20 6f 72 20 42 4c 4f 42  any TEXT or BLOB
17cef 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72   content is stor
17cf0 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74  ed in memory obt
17cf1 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61  ained from.** ma
17cf2 6c 6c 6f 63 28 29 2e 20 20 49 6e 20 74 68 69 73  lloc().  In this
17cf3 20 77 61 79 2c 20 77 65 20 6b 6e 6f 77 20 74 68   way, we know th
17cf4 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 73  at the memory is
17cf5 20 73 61 66 65 20 74 6f 20 62 65 0a 2a 2a 20 6f   safe to be.** o
17cf6 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 61 6c  verwritten or al
17cf7 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  tered..**.** Ret
17cf8 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  urn SQLITE_OK on
17cf9 20 73 75 63 63 65 73 73 20 6f 72 20 53 51 4c 49   success or SQLI
17cfa 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 6d 61 6c 6c  TE_NOMEM if mall
17cfb 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c  oc fails..*/.SQL
17cfc 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
17cfd 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61  sqlite3VdbeMemMa
17cfe 6b 65 57 72 69 74 65 61 62 6c 65 28 4d 65 6d 20  keWriteable(Mem 
17cff 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 3b  *pMem){.  int f;
17d00 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
17d01 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
17d02 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65  3_mutex_held(pMe
17d03 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  m->db->mutex) );
17d04 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
17d05 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53  ->flags&MEM_RowS
17d06 65 74 29 3d 3d 30 20 29 3b 0a 20 20 65 78 70 61  et)==0 );.  expa
17d07 6e 64 42 6c 6f 62 28 70 4d 65 6d 29 3b 0a 20 20  ndBlob(pMem);.  
17d08 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b  f = pMem->flags;
17d09 0a 20 20 69 66 28 20 28 66 26 28 4d 45 4d 5f 53  .  if( (f&(MEM_S
17d0a 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 26 26  tr|MEM_Blob)) &&
17d0b 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e   pMem->z!=pMem->
17d0c 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69  zMalloc ){.    i
17d0d 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
17d0e 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 70 4d 65 6d  mGrow(pMem, pMem
17d0f 2d 3e 6e 20 2b 20 32 2c 20 31 29 20 29 7b 0a 20  ->n + 2, 1) ){. 
17d10 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
17d11 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
17d12 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d      pMem->z[pMem
17d13 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d  ->n] = 0;.    pM
17d14 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d  em->z[pMem->n+1]
17d15 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e   = 0;.    pMem->
17d16 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72  flags |= MEM_Ter
17d17 6d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  m;.  }..  return
17d18 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
17d19 2a 0a 2a 2a 20 49 66 20 74 68 65 20 67 69 76 65  *.** If the give
17d1a 6e 20 4d 65 6d 2a 20 68 61 73 20 61 20 7a 65 72  n Mem* has a zer
17d1b 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2c 20 74  o-filled tail, t
17d1c 75 72 6e 20 69 74 20 69 6e 74 6f 20 61 6e 20 6f  urn it into an o
17d1d 72 64 69 6e 61 72 79 0a 2a 2a 20 62 6c 6f 62 20  rdinary.** blob 
17d1e 73 74 6f 72 65 64 20 69 6e 20 64 79 6e 61 6d 69  stored in dynami
17d1f 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  cally allocated 
17d20 73 70 61 63 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65  space..*/.#ifnde
17d21 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
17d22 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52  CRBLOB.SQLITE_PR
17d23 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
17d24 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
17d25 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  ob(Mem *pMem){. 
17d26 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
17d27 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20   & MEM_Zero ){. 
17d28 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20     int nByte;.  
17d29 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
17d2a 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29  flags&MEM_Blob )
17d2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
17d2c 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52  Mem->flags&MEM_R
17d2d 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20  owSet)==0 );.   
17d2e 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
17d2f 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
17d30 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
17d31 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  >db->mutex) );..
17d32 20 20 20 20 2f 2a 20 53 65 74 20 6e 42 79 74 65      /* Set nByte
17d33 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
17d34 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
17d35 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 65 78   to store the ex
17d36 70 61 6e 64 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a  panded blob. */.
17d37 20 20 20 20 6e 42 79 74 65 20 3d 20 70 4d 65 6d      nByte = pMem
17d38 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e 75 2e 6e 5a  ->n + pMem->u.nZ
17d39 65 72 6f 3b 0a 20 20 20 20 69 66 28 20 6e 42 79  ero;.    if( nBy
17d3a 74 65 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e  te<=0 ){.      n
17d3b 42 79 74 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  Byte = 1;.    }.
17d3c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
17d3d 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c  dbeMemGrow(pMem,
17d3e 20 6e 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 20   nByte, 1) ){.  
17d3f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
17d40 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 0a  E_NOMEM;.    }..
17d41 20 20 20 20 6d 65 6d 73 65 74 28 26 70 4d 65 6d      memset(&pMem
17d42 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c  ->z[pMem->n], 0,
17d43 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b   pMem->u.nZero);
17d44 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2b 3d 20  .    pMem->n += 
17d45 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20  pMem->u.nZero;. 
17d46 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26     pMem->flags &
17d47 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f 7c 4d 45 4d  = ~(MEM_Zero|MEM
17d48 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a 20 20 72 65  _Term);.  }.  re
17d49 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
17d4a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
17d4b 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 67   Make sure the g
17d4c 69 76 65 6e 20 4d 65 6d 20 69 73 20 5c 75 30 30  iven Mem is \u00
17d4d 30 30 20 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a  00 terminated..*
17d4e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17d4f 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
17d50 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28  MemNulTerminate(
17d51 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73  Mem *pMem){.  as
17d52 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
17d53 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
17d54 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
17d55 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
17d56 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
17d57 20 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 7c 7c   MEM_Term)!=0 ||
17d58 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
17d59 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20  MEM_Str)==0 ){. 
17d5a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17d5b 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f 74 68 69 6e  _OK;   /* Nothin
17d5c 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 7d 0a 20  g to do */.  }. 
17d5d 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
17d5e 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 70 4d  MemGrow(pMem, pM
17d5f 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20 29 7b 0a 20  em->n+2, 1) ){. 
17d60 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17d61 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 4d  _NOMEM;.  }.  pM
17d62 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d  em->z[pMem->n] =
17d63 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d   0;.  pMem->z[pM
17d64 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20  em->n+1] = 0;.  
17d65 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
17d66 45 4d 5f 54 65 72 6d 3b 0a 20 20 72 65 74 75 72  EM_Term;.  retur
17d67 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
17d68 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45 4d 5f 53 74  /*.** Add MEM_St
17d69 72 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66 20  r to the set of 
17d6a 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20  representations 
17d6b 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 4d 65  for the given Me
17d6c 6d 2e 20 20 4e 75 6d 62 65 72 73 0a 2a 2a 20 61  m.  Numbers.** a
17d6d 72 65 20 63 6f 6e 76 65 72 74 65 64 20 75 73 69  re converted usi
17d6e 6e 67 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  ng sqlite3_snpri
17d6f 6e 74 66 28 29 2e 20 20 43 6f 6e 76 65 72 74 69  ntf().  Converti
17d70 6e 67 20 61 20 42 4c 4f 42 20 74 6f 20 61 20 73  ng a BLOB to a s
17d71 74 72 69 6e 67 0a 2a 2a 20 69 73 20 61 20 6e 6f  tring.** is a no
17d72 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 69 73 74  -op..**.** Exist
17d73 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
17d74 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20 61 6e 64 20  ons MEM_Int and 
17d75 4d 45 4d 5f 52 65 61 6c 20 61 72 65 20 2a 6e 6f  MEM_Real are *no
17d76 74 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a  t* invalidated..
17d77 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f 4e 75 6c 6c  **.** A MEM_Null
17d78 20 76 61 6c 75 65 20 77 69 6c 6c 20 6e 65 76 65   value will neve
17d79 72 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 74  r be passed to t
17d7a 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68  his function. Th
17d7b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a  is function is.*
17d7c 2a 20 75 73 65 64 20 66 6f 72 20 63 6f 6e 76 65  * used for conve
17d7d 72 74 69 6e 67 20 76 61 6c 75 65 73 20 74 6f 20  rting values to 
17d7e 74 65 78 74 20 66 6f 72 20 72 65 74 75 72 6e 69  text for returni
17d7f 6e 67 20 74 6f 20 74 68 65 20 75 73 65 72 20 28  ng to the user (
17d80 69 2e 65 2e 20 76 69 61 0a 2a 2a 20 73 71 6c 69  i.e. via.** sqli
17d81 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29  te3_value_text()
17d82 29 2c 20 6f 72 20 66 6f 72 20 65 6e 73 75 72 69  ), or for ensuri
17d83 6e 67 20 74 68 61 74 20 76 61 6c 75 65 73 20 74  ng that values t
17d84 6f 20 62 65 20 75 73 65 64 20 61 73 20 62 74 72  o be used as btr
17d85 65 65 0a 2a 2a 20 6b 65 79 73 20 61 72 65 20 73  ee.** keys are s
17d86 74 72 69 6e 67 73 2e 20 49 6e 20 74 68 65 20 66  trings. In the f
17d87 6f 72 6d 65 72 20 63 61 73 65 20 61 20 4e 55 4c  ormer case a NUL
17d88 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74  L pointer is ret
17d89 75 72 6e 65 64 20 74 68 65 0a 2a 2a 20 75 73 65  urned the.** use
17d8a 72 20 61 6e 64 20 74 68 65 20 6c 61 74 65 72 20  r and the later 
17d8b 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 70  is an internal p
17d8c 72 6f 67 72 61 6d 6d 69 6e 67 20 65 72 72 6f 72  rogramming error
17d8d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17d8e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
17d8f 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28  dbeMemStringify(
17d90 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 65  Mem *pMem, int e
17d91 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  nc){.  int rc = 
17d92 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
17d93 20 66 67 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67   fg = pMem->flag
17d94 73 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e  s;.  const int n
17d95 42 79 74 65 20 3d 20 33 32 3b 0a 0a 20 20 61 73  Byte = 32;..  as
17d96 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
17d97 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
17d98 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
17d99 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
17d9a 73 65 72 74 28 20 21 28 66 67 26 4d 45 4d 5f 5a  sert( !(fg&MEM_Z
17d9b 65 72 6f 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ero) );.  assert
17d9c 28 20 21 28 66 67 26 28 4d 45 4d 5f 53 74 72 7c  ( !(fg&(MEM_Str|
17d9d 4d 45 4d 5f 42 6c 6f 62 29 29 20 29 3b 0a 20 20  MEM_Blob)) );.  
17d9e 61 73 73 65 72 74 28 20 66 67 26 28 4d 45 4d 5f  assert( fg&(MEM_
17d9f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 3b  Int|MEM_Real) );
17da0 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
17da1 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53  ->flags&MEM_RowS
17da2 65 74 29 3d 3d 30 20 29 3b 0a 0a 0a 20 20 69 66  et)==0 );...  if
17da3 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
17da4 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65  Grow(pMem, nByte
17da5 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 0) ){.    retu
17da6 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
17da7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61  .  }..  /* For a
17da8 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72   Real or Integer
17da9 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70  , use sqlite3_mp
17daa 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75  rintf() to produ
17dab 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a  ce the UTF-8.  *
17dac 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  * string represe
17dad 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76  ntation of the v
17dae 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74  alue. Then, if t
17daf 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f  he required enco
17db0 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46  ding.  ** is UTF
17db1 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62  -16le or UTF-16b
17db2 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69  e do a translati
17db3 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46  on..  ** .  ** F
17db4 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20  IX ME: It would 
17db5 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c  be better if sql
17db6 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20  ite3_snprintf() 
17db7 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e  could do UTF-16.
17db8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26  .  */.  if( fg &
17db9 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
17dba 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
17dbb 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c  (nByte, pMem->z,
17dbc 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75   "%lld", pMem->u
17dbd 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  .i);.  }else{.  
17dbe 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d    assert( fg & M
17dbf 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73  EM_Real );.    s
17dc0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
17dc1 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20  nByte, pMem->z, 
17dc2 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e  "%!.15g", pMem->
17dc3 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e  r);.  }.  pMem->
17dc4 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
17dc5 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  n30(pMem->z);.  
17dc6 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
17dc7 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d  TE_UTF8;.  pMem-
17dc8 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
17dc9 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71  r|MEM_Term;.  sq
17dca 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
17dcb 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e  ncoding(pMem, en
17dcc 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
17dcd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79  .}../*.** Memory
17dce 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61   cell pMem conta
17dcf 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20  ins the context 
17dd0 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  of an aggregate 
17dd1 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  function..** Thi
17dd2 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20  s routine calls 
17dd3 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74  the finalize met
17dd4 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e  hod for that fun
17dd5 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72  ction.  The.** r
17dd6 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67  esult of the agg
17dd7 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64  regate is stored
17dd8 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e   back into pMem.
17dd9 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
17dda 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68  LITE_ERROR if th
17ddb 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f  e finalizer repo
17ddc 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53  rts an error.  S
17ddd 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65  QLITE_OK.** othe
17dde 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rwise..*/.SQLITE
17ddf 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
17de0 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c  ite3VdbeMemFinal
17de1 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46  ize(Mem *pMem, F
17de2 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a  uncDef *pFunc){.
17de3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
17de4 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 46 75 6e  E_OK;.  if( pFun
17de5 63 20 26 26 20 70 46 75 6e 63 2d 3e 78 46 69 6e  c && pFunc->xFin
17de6 61 6c 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c  alize ){.    sql
17de7 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78  ite3_context ctx
17de8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
17de9 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
17dea 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20 70 46 75  _Null)!=0 || pFu
17deb 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70 44 65 66  nc==pMem->u.pDef
17dec 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
17ded 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
17dee 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
17def 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
17df0 78 29 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74  x) );.    memset
17df1 28 26 63 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66  (&ctx, 0, sizeof
17df2 28 63 74 78 29 29 3b 0a 20 20 20 20 63 74 78 2e  (ctx));.    ctx.
17df3 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  s.flags = MEM_Nu
17df4 6c 6c 3b 0a 20 20 20 20 63 74 78 2e 73 2e 64 62  ll;.    ctx.s.db
17df5 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 20   = pMem->db;.   
17df6 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d 65 6d   ctx.pMem = pMem
17df7 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20  ;.    ctx.pFunc 
17df8 3d 20 70 46 75 6e 63 3b 0a 20 20 20 20 70 46 75  = pFunc;.    pFu
17df9 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 28 26 63  nc->xFinalize(&c
17dfa 74 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  tx);.    assert(
17dfb 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 73   0==(pMem->flags
17dfc 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70 4d  &MEM_Dyn) && !pM
17dfd 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20  em->xDel );.    
17dfe 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d  sqlite3DbFree(pM
17dff 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d  em->db, pMem->zM
17e00 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 6d 65 6d 63  alloc);.    memc
17e01 70 79 28 70 4d 65 6d 2c 20 26 63 74 78 2e 73 2c  py(pMem, &ctx.s,
17e02 20 73 69 7a 65 6f 66 28 63 74 78 2e 73 29 29 3b   sizeof(ctx.s));
17e03 0a 20 20 20 20 72 63 20 3d 20 28 63 74 78 2e 69  .    rc = (ctx.i
17e04 73 45 72 72 6f 72 3f 53 51 4c 49 54 45 5f 45 52  sError?SQLITE_ER
17e05 52 4f 52 3a 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a  ROR:SQLITE_OK);.
17e06 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
17e07 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
17e08 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e   memory cell con
17e09 74 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76  tains a string v
17e0a 61 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62  alue that must b
17e0b 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e  e freed by.** in
17e0c 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e  voking an extern
17e0d 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65  al callback, fre
17e0e 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e  e it now. Callin
17e0f 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  g this function.
17e10 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65  ** does not free
17e11 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63   any Mem.zMalloc
17e12 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49   buffer..*/.SQLI
17e13 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
17e14 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
17e15 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65  leaseExternal(Me
17e16 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  m *p){.  assert(
17e17 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c   p->db==0 || sql
17e18 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
17e19 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
17e1a 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26  .  if( p->flags&
17e1b 4d 45 4d 5f 41 67 67 20 29 7b 0a 20 20 20 20 73  MEM_Agg ){.    s
17e1c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e  qlite3VdbeMemFin
17e1d 61 6c 69 7a 65 28 70 2c 20 70 2d 3e 75 2e 70 44  alize(p, p->u.pD
17e1e 65 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ef);.    assert(
17e1f 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d   (p->flags & MEM
17e20 5f 41 67 67 29 3d 3d 30 20 29 3b 0a 20 20 20 20  _Agg)==0 );.    
17e21 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
17e22 6c 65 61 73 65 28 70 29 3b 0a 20 20 7d 65 6c 73  lease(p);.  }els
17e23 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d  e if( p->flags&M
17e24 45 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65  EM_Dyn && p->xDe
17e25 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  l ){.    assert(
17e26 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52   (p->flags&MEM_R
17e27 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20  owSet)==0 );.   
17e28 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a   p->xDel((void *
17e29 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 2d 3e 78  )p->z);.    p->x
17e2a 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Del = 0;.  }else
17e2b 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45   if( p->flags&ME
17e2c 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20  M_RowSet ){.    
17e2d 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65  sqlite3RowSetCle
17e2e 61 72 28 70 2d 3e 75 2e 70 52 6f 77 53 65 74 29  ar(p->u.pRowSet)
17e2f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
17e30 65 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72  elease any memor
17e31 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 4d 65  y held by the Me
17e32 6d 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 76  m. This may leav
17e33 65 20 74 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a  e the Mem in an.
17e34 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20  ** inconsistent 
17e35 73 74 61 74 65 2c 20 66 6f 72 20 65 78 61 6d 70  state, for examp
17e36 6c 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d  le with (Mem.z==
17e37 30 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74  0) and.** (Mem.t
17e38 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54  ype==SQLITE_TEXT
17e39 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
17e3a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
17e3b 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
17e3c 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  Mem *p){.  sqlit
17e3d 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
17e3e 45 78 74 65 72 6e 61 6c 28 70 29 3b 0a 20 20 73  External(p);.  s
17e3f 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e  qlite3DbFree(p->
17e40 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b  db, p->zMalloc);
17e41 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70  .  p->z = 0;.  p
17e42 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  ->zMalloc = 0;. 
17e43 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a   p->xDel = 0;.}.
17e44 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
17e45 20 36 34 2d 62 69 74 20 49 45 45 45 20 64 6f 75   64-bit IEEE dou
17e46 62 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d 62 69  ble into a 64-bi
17e47 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
17e48 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 75 62  ..** If the doub
17e49 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c  le is too large,
17e4a 20 72 65 74 75 72 6e 20 30 78 38 30 30 30 30 30   return 0x800000
17e4b 30 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a  0000000000..**.*
17e4c 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d 73 20 61  * Most systems a
17e4d 70 70 65 61 72 20 74 6f 20 64 6f 20 74 68 69 73  ppear to do this
17e4e 20 73 69 6d 70 6c 79 20 62 79 20 61 73 73 69 67   simply by assig
17e4f 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65  ning.** variable
17e50 73 20 61 6e 64 20 77 69 74 68 6f 75 74 20 74 68  s and without th
17e51 65 20 65 78 74 72 61 20 72 61 6e 67 65 20 74 65  e extra range te
17e52 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65  sts.  But.** the
17e53 72 65 20 61 72 65 20 72 65 70 6f 72 74 73 20 74  re are reports t
17e54 68 61 74 20 77 69 6e 64 6f 77 73 20 74 68 72 6f  hat windows thro
17e55 77 73 20 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a  ws an expection.
17e56 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69  ** if the floati
17e57 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69  ng point value i
17e58 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20  s out of range. 
17e59 28 53 65 65 20 74 69 63 6b 65 74 20 23 32 38 38  (See ticket #288
17e5a 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65 20 77  0.).** Because w
17e5b 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74  e do not complet
17e5c 65 6c 79 20 75 6e 64 65 72 73 74 61 6e 64 20 74  ely understand t
17e5d 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77  he problem, we w
17e5e 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20  ill.** take the 
17e5f 63 6f 6e 73 65 72 76 61 74 69 76 65 20 61 70 70  conservative app
17e60 72 6f 61 63 68 20 61 6e 64 20 61 6c 77 61 79 73  roach and always
17e61 20 64 6f 20 72 61 6e 67 65 20 74 65 73 74 73 0a   do range tests.
17e62 2a 2a 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70  ** before attemp
17e63 74 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 73  ting the convers
17e64 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
17e65 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34  64 doubleToInt64
17e66 28 64 6f 75 62 6c 65 20 72 29 7b 0a 20 20 2f 2a  (double r){.  /*
17e67 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69  .  ** Many compi
17e68 6c 65 72 73 20 77 65 20 65 6e 63 6f 75 6e 74 65  lers we encounte
17e69 72 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20  r do not define 
17e6a 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68  constants for th
17e6b 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61  e.  ** minimum a
17e6c 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69  nd maximum 64-bi
17e6d 74 20 69 6e 74 65 67 65 72 73 2c 20 6f 72 20 74  t integers, or t
17e6e 68 65 79 20 64 65 66 69 6e 65 20 74 68 65 6d 0a  hey define them.
17e6f 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e    ** inconsisten
17e70 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64  tly.  And many d
17e71 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64  o not understand
17e72 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69   the "LL" notati
17e73 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64  on..  ** So we d
17e74 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74  efine our own st
17e75 61 74 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 68  atic constants h
17e76 65 72 65 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e  ere using nothin
17e77 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68  g.  ** larger th
17e78 61 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65  an a 32-bit inte
17e79 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20  ger constant..  
17e7a 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
17e7b 74 20 69 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c  t i64 maxInt = L
17e7c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20  ARGEST_INT64;.  
17e7d 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34  static const i64
17e7e 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45   minInt = SMALLE
17e7f 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28  ST_INT64;..  if(
17e80 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e   r<(double)minIn
17e81 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
17e82 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20  minInt;.  }else 
17e83 69 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61  if( r>(double)ma
17e84 78 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75  xInt ){.    retu
17e85 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c  rn minInt;.  }el
17e86 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28  se{.    return (
17e87 69 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  i64)r;.  }.}../*
17e88 0a 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d 65 20  .** Return some 
17e89 6b 69 6e 64 20 6f 66 20 69 6e 74 65 67 65 72 20  kind of integer 
17e8a 76 61 6c 75 65 20 77 68 69 63 68 20 69 73 20 74  value which is t
17e8b 68 65 20 62 65 73 74 20 77 65 20 63 61 6e 20 64  he best we can d
17e8c 6f 0a 2a 2a 20 61 74 20 72 65 70 72 65 73 65 6e  o.** at represen
17e8d 74 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 74  ting the value t
17e8e 68 61 74 20 2a 70 4d 65 6d 20 64 65 73 63 72 69  hat *pMem descri
17e8f 62 65 73 20 61 73 20 61 6e 20 69 6e 74 65 67 65  bes as an intege
17e90 72 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73  r..** If pMem is
17e91 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 68 65   an integer, the
17e92 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 65  n the value is e
17e93 78 61 63 74 2e 20 20 49 66 20 70 4d 65 6d 20 69  xact.  If pMem i
17e94 73 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e 67 2d  s.** a floating-
17e95 70 6f 69 6e 74 20 74 68 65 6e 20 74 68 65 20 76  point then the v
17e96 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
17e97 20 74 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   the integer par
17e98 74 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73  t..** If pMem is
17e99 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
17e9a 62 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b 65 20  b, then we make 
17e9b 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f  an attempt to co
17e9c 6e 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e 74 6f  nvert.** it into
17e9d 20 61 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72   a integer and r
17e9e 65 74 75 72 6e 20 74 68 61 74 2e 20 20 49 66 20  eturn that.  If 
17e9f 70 4d 65 6d 20 69 73 20 4e 55 4c 4c 2c 20 72 65  pMem is NULL, re
17ea0 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66  turn 0..**.** If
17ea1 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e   pMem is a strin
17ea2 67 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20  g, its encoding 
17ea3 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64  might be changed
17ea4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17ea5 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56  ATE i64 sqlite3V
17ea6 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20  dbeIntValue(Mem 
17ea7 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c  *pMem){.  int fl
17ea8 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ags;.  assert( p
17ea9 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
17eaa 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
17eab 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
17eac 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20 70  ) );.  flags = p
17ead 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66  Mem->flags;.  if
17eae 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ( flags & MEM_In
17eaf 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
17eb0 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c  pMem->u.i;.  }el
17eb1 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  se if( flags & M
17eb2 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  EM_Real ){.    r
17eb3 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 6f 49 6e  eturn doubleToIn
17eb4 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20  t64(pMem->r);.  
17eb5 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20  }else if( flags 
17eb6 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  & (MEM_Str|MEM_B
17eb7 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 36 34 20  lob) ){.    i64 
17eb8 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 65 6d 2d  value;.    pMem-
17eb9 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
17eba 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  r;.    if( sqlit
17ebb 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
17ebc 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54  ding(pMem, SQLIT
17ebd 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c  E_UTF8).       |
17ebe 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  | sqlite3VdbeMem
17ebf 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65  NulTerminate(pMe
17ec0 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  m) ){.      retu
17ec1 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 0;.    }.    
17ec2 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20  assert( pMem->z 
17ec3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74  );.    sqlite3At
17ec4 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 20 26 76  oi64(pMem->z, &v
17ec5 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 74 75 72  alue);.    retur
17ec6 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 6c 73 65  n value;.  }else
17ec7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
17ec8 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
17ec9 75 72 6e 20 74 68 65 20 62 65 73 74 20 72 65 70  urn the best rep
17eca 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70  resentation of p
17ecb 4d 65 6d 20 74 68 61 74 20 77 65 20 63 61 6e 20  Mem that we can 
17ecc 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a 20 64 6f  get into a.** do
17ecd 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 6d 20 69  uble.  If pMem i
17ece 73 20 61 6c 72 65 61 64 79 20 61 20 64 6f 75 62  s already a doub
17ecf 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 67 65 72  le or an integer
17ed0 2c 20 72 65 74 75 72 6e 20 69 74 73 0a 2a 2a 20  , return its.** 
17ed1 76 61 6c 75 65 2e 20 20 49 66 20 69 74 20 69 73  value.  If it is
17ed2 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
17ed3 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72  b, try to conver
17ed4 74 20 69 74 20 74 6f 20 61 20 64 6f 75 62 6c 65  t it to a double
17ed5 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 61 20  ..** If it is a 
17ed6 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 30  NULL, return 0.0
17ed7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17ed8 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  ATE double sqlit
17ed9 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
17eda 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73  Mem *pMem){.  as
17edb 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
17edc 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
17edd 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
17ede 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
17edf 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
17ee0 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
17ee1 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a  return pMem->r;.
17ee2 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d    }else if( pMem
17ee3 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
17ee4 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
17ee5 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e  (double)pMem->u.
17ee6 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  i;.  }else if( p
17ee7 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Mem->flags & (ME
17ee8 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
17ee9 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61  ){.    double va
17eea 6c 20 3d 20 30 2e 30 3b 0a 20 20 20 20 70 4d 65  l = 0.0;.    pMe
17eeb 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  m->flags |= MEM_
17eec 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Str;.    if( sql
17eed 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
17eee 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c  coding(pMem, SQL
17eef 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20  ITE_UTF8).      
17ef0 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d   || sqlite3VdbeM
17ef1 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70  emNulTerminate(p
17ef2 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65  Mem) ){.      re
17ef3 74 75 72 6e 20 30 2e 30 3b 0a 20 20 20 20 7d 0a  turn 0.0;.    }.
17ef4 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
17ef5 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ->z );.    sqlit
17ef6 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e 7a 2c 20  e3AtoF(pMem->z, 
17ef7 26 76 61 6c 29 3b 0a 20 20 20 20 72 65 74 75 72  &val);.    retur
17ef8 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a  n val;.  }else{.
17ef9 20 20 20 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a      return 0.0;.
17efa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
17efb 20 4d 45 4d 20 73 74 72 75 63 74 75 72 65 20 69   MEM structure i
17efc 73 20 61 6c 72 65 61 64 79 20 61 20 4d 45 4d 5f  s already a MEM_
17efd 52 65 61 6c 2e 20 20 54 72 79 20 74 6f 20 61 6c  Real.  Try to al
17efe 73 6f 20 6d 61 6b 65 20 69 74 20 61 0a 2a 2a 20  so make it a.** 
17eff 4d 45 4d 5f 49 6e 74 20 69 66 20 77 65 20 63 61  MEM_Int if we ca
17f00 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
17f01 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
17f02 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69  3VdbeIntegerAffi
17f03 6e 69 74 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  nity(Mem *pMem){
17f04 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
17f05 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
17f06 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  l );.  assert( (
17f07 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
17f08 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a  M_RowSet)==0 );.
17f09 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
17f0a 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
17f0b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
17f0c 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
17f0d 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64  .  pMem->u.i = d
17f0e 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65  oubleToInt64(pMe
17f0f 6d 2d 3e 72 29 3b 0a 20 20 69 66 28 20 70 4d 65  m->r);.  if( pMe
17f10 6d 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65 29 70 4d  m->r==(double)pM
17f11 65 6d 2d 3e 75 2e 69 20 29 7b 0a 20 20 20 20 70  em->u.i ){.    p
17f12 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
17f13 4d 5f 49 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  M_Int;.  }.}../*
17f14 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d  .** Convert pMem
17f15 20 74 6f 20 74 79 70 65 20 69 6e 74 65 67 65 72   to type integer
17f16 2e 20 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6e  .  Invalidate an
17f17 79 20 70 72 69 6f 72 20 72 65 70 72 65 73 65 6e  y prior represen
17f18 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  tations..*/.SQLI
17f19 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
17f1a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
17f1b 65 67 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65  egerify(Mem *pMe
17f1c 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
17f1d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
17f1e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
17f1f 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
17f20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
17f21 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
17f22 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
17f23 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71 6c   pMem->u.i = sql
17f24 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
17f25 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74  (pMem);.  MemSet
17f26 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d  TypeFlag(pMem, M
17f27 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 65 74 75 72  EM_Int);.  retur
17f28 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
17f29 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d  /*.** Convert pM
17f2a 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  em so that it is
17f2b 20 6f 66 20 74 79 70 65 20 4d 45 4d 5f 52 65 61   of type MEM_Rea
17f2c 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65  l..** Invalidate
17f2d 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72 65   any prior repre
17f2e 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53  sentations..*/.S
17f2f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
17f30 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
17f31 52 65 61 6c 69 66 79 28 4d 65 6d 20 2a 70 4d 65  Realify(Mem *pMe
17f32 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
17f33 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
17f34 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
17f35 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
17f36 20 29 3b 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20   );.  pMem->r = 
17f37 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56  sqlite3VdbeRealV
17f38 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65  alue(pMem);.  Me
17f39 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65  mSetTypeFlag(pMe
17f3a 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20  m, MEM_Real);.  
17f3b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
17f3c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
17f3d 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20  rt pMem so that 
17f3e 69 74 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d  it has types MEM
17f3f 5f 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74  _Real or MEM_Int
17f40 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76   or both..** Inv
17f41 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f  alidate any prio
17f42 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  r representation
17f43 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
17f44 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
17f45 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28  VdbeMemNumerify(
17f46 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f  Mem *pMem){.  do
17f47 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69  uble r1, r2;.  i
17f48 36 34 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  64 i;.  assert( 
17f49 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28  (pMem->flags & (
17f4a 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c  MEM_Int|MEM_Real
17f4b 7c 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29  |MEM_Null))==0 )
17f4c 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  ;.  assert( (pMe
17f4d 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  m->flags & (MEM_
17f4e 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d  Blob|MEM_Str))!=
17f4f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
17f50 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
17f51 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
17f52 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
17f53 29 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69  ) );.  r1 = sqli
17f54 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65  te3VdbeRealValue
17f55 28 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f  (pMem);.  i = do
17f56 75 62 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b  ubleToInt64(r1);
17f57 0a 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29  .  r2 = (double)
17f58 69 3b 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20  i;.  if( r1==r2 
17f59 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
17f5a 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
17f5b 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pMem);.  }else{.
17f5c 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31      pMem->r = r1
17f5d 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
17f5e 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52  Flag(pMem, MEM_R
17f5f 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  eal);.  }.  retu
17f60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
17f61 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
17f62 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  y previous value
17f63 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c   and set the val
17f64 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d  ue stored in *pM
17f65 65 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53  em to NULL..*/.S
17f66 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
17f67 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
17f68 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d  mSetNull(Mem *pM
17f69 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d  em){.  if( pMem-
17f6a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
17f6b 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Set ){.    sqlit
17f6c 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d  e3RowSetClear(pM
17f6d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a  em->u.pRowSet);.
17f6e 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65    }.  MemSetType
17f6f 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e  Flag(pMem, MEM_N
17f70 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79  ull);.  pMem->ty
17f71 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  pe = SQLITE_NULL
17f72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
17f73 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76  e any previous v
17f74 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65  alue and set the
17f75 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42   value to be a B
17f76 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a  LOB of length.**
17f77 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c   n containing al
17f78 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49  l zeros..*/.SQLI
17f79 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
17f7a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
17f7b 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70  tZeroBlob(Mem *p
17f7c 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73  Mem, int n){.  s
17f7d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
17f7e 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d  ease(pMem);.  pM
17f7f 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
17f80 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20  Blob|MEM_Zero;. 
17f81 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
17f82 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65  LITE_BLOB;.  pMe
17f83 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20  m->n = 0;.  if( 
17f84 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70  n<0 ) n = 0;.  p
17f85 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e  Mem->u.nZero = n
17f86 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20  ;.  pMem->enc = 
17f87 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a  SQLITE_UTF8;.}..
17f88 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79  /*.** Delete any
17f89 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
17f8a 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75  and set the valu
17f8b 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65  e stored in *pMe
17f8c 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e  m to val,.** man
17f8d 69 66 65 73 74 20 74 79 70 65 20 49 4e 54 45 47  ifest type INTEG
17f8e 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ER..*/.SQLITE_PR
17f8f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
17f90 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
17f91 34 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 36 34  4(Mem *pMem, i64
17f92 20 76 61 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33   val){.  sqlite3
17f93 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
17f94 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e  Mem);.  pMem->u.
17f95 69 20 3d 20 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d  i = val;.  pMem-
17f96 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
17f97 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d  ;.  pMem->type =
17f98 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b   SQLITE_INTEGER;
17f99 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
17f9a 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61   any previous va
17f9b 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20  lue and set the 
17f9c 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20  value stored in 
17f9d 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a  *pMem to val,.**
17f9e 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 52   manifest type R
17f9f 45 41 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  EAL..*/.SQLITE_P
17fa0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
17fa1 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75  te3VdbeMemSetDou
17fa2 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64  ble(Mem *pMem, d
17fa3 6f 75 62 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66  ouble val){.  if
17fa4 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 76  ( sqlite3IsNaN(v
17fa5 61 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  al) ){.    sqlit
17fa6 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
17fa7 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pMem);.  }else{
17fa8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
17fa9 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
17faa 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20  ;.    pMem->r = 
17fab 76 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66  val;.    pMem->f
17fac 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b  lags = MEM_Real;
17fad 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  .    pMem->type 
17fae 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a  = SQLITE_FLOAT;.
17faf 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c    }.}../*.** Del
17fb0 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ete any previous
17fb1 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74   value and set t
17fb2 68 65 20 76 61 6c 75 65 20 6f 66 20 70 4d 65 6d  he value of pMem
17fb3 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 65 6d 70   to be an.** emp
17fb4 74 79 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78  ty boolean index
17fb5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17fb6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
17fb7 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74  VdbeMemSetRowSet
17fb8 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 73  (Mem *pMem){.  s
17fb9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 4d 65  qlite3 *db = pMe
17fba 6d 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  m->db;.  assert(
17fbb 20 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20   db!=0 );.  if( 
17fbc 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
17fbd 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20  M_RowSet ){.    
17fbe 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65  sqlite3RowSetCle
17fbf 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53  ar(pMem->u.pRowS
17fc0 65 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  et);.  }else{.  
17fc1 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
17fc2 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
17fc3 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63     pMem->zMalloc
17fc4 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
17fc5 6f 63 52 61 77 28 64 62 2c 20 36 34 29 3b 0a 20  ocRaw(db, 64);. 
17fc6 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c   }.  if( db->mal
17fc7 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
17fc8 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
17fc9 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65  EM_Null;.  }else
17fca 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d  {.    assert( pM
17fcb 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20  em->zMalloc );. 
17fcc 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53     pMem->u.pRowS
17fcd 65 74 20 3d 20 73 71 6c 69 74 65 33 52 6f 77 53  et = sqlite3RowS
17fce 65 74 49 6e 69 74 28 64 62 2c 20 70 4d 65 6d 2d  etInit(db, pMem-
17fcf 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a 20 20 20 20 20  >zMalloc, .     
17fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17fd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17fd2 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f    sqlite3DbMallo
17fd3 63 53 69 7a 65 28 64 62 2c 20 70 4d 65 6d 2d 3e  cSize(db, pMem->
17fd4 7a 4d 61 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 61  zMalloc));.    a
17fd5 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 75 2e 70  ssert( pMem->u.p
17fd6 52 6f 77 53 65 74 21 3d 30 20 29 3b 0a 20 20 20  RowSet!=0 );.   
17fd7 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
17fd8 45 4d 5f 52 6f 77 53 65 74 3b 0a 20 20 7d 0a 7d  EM_RowSet;.  }.}
17fd9 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
17fda 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d 20 6f  rue if the Mem o
17fdb 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20 61  bject contains a
17fdc 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 74 68   TEXT or BLOB th
17fdd 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72  at is.** too lar
17fde 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a 65 20  ge - whose size 
17fdf 65 78 63 65 65 64 73 20 53 51 4c 49 54 45 5f 4d  exceeds SQLITE_M
17fe0 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51  AX_LENGTH..*/.SQ
17fe1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
17fe2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
17fe3 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a 20  ooBig(Mem *p){. 
17fe4 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d   assert( p->db!=
17fe5 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c  0 );.  if( p->fl
17fe6 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d  ags & (MEM_Str|M
17fe7 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20  EM_Blob) ){.    
17fe8 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20  int n = p->n;.  
17fe9 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26    if( p->flags &
17fea 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
17feb 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a 65     n += p->u.nZe
17fec 72 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  ro;.    }.    re
17fed 74 75 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c  turn n>p->db->aL
17fee 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
17fef 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20  T_LENGTH];.  }. 
17ff0 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f   return 0; .}../
17ff1 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74 72  *.** Size of str
17ff2 75 63 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c  uct Mem not incl
17ff3 75 64 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d  uding the Mem.zM
17ff4 61 6c 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f  alloc member..*/
17ff5 0a 23 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c  .#define MEMCELL
17ff6 53 49 5a 45 20 28 73 69 7a 65 5f 74 29 28 26 28  SIZE (size_t)(&(
17ff7 28 28 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c  ((Mem *)0)->zMal
17ff8 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  loc))../*.** Mak
17ff9 65 20 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70  e an shallow cop
17ffa 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20  y of pFrom into 
17ffb 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74  pTo.  Prior cont
17ffc 65 6e 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61  ents of.** pTo a
17ffd 72 65 20 66 72 65 65 64 2e 20 20 54 68 65 20 70  re freed.  The p
17ffe 46 72 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73  From->z field is
17fff 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e   not duplicated.
18000 20 20 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a    If.** pFrom->z
18001 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 70   is used, then p
18002 54 6f 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20  To->z points to 
18003 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61  the same thing a
18004 73 20 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e  s pFrom->z.** an
18005 64 20 66 6c 61 67 73 20 67 65 74 73 20 73 72 63  d flags gets src
18006 54 79 70 65 20 28 65 69 74 68 65 72 20 4d 45 4d  Type (either MEM
18007 5f 45 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74  _Ephem or MEM_St
18008 61 74 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  atic)..*/.SQLITE
18009 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1800a 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
1800b 6c 6f 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f  lowCopy(Mem *pTo
1800c 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72  , const Mem *pFr
1800d 6f 6d 2c 20 69 6e 74 20 73 72 63 54 79 70 65 29  om, int srcType)
1800e 7b 0a 20 20 61 73 73 65 72 74 28 20 28 70 46 72  {.  assert( (pFr
1800f 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  om->flags & MEM_
18010 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
18011 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
18012 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 54  leaseExternal(pT
18013 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f  o);.  memcpy(pTo
18014 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c  , pFrom, MEMCELL
18015 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78 44  SIZE);.  pTo->xD
18016 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 70  el = 0;.  if( (p
18017 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  From->flags&MEM_
18018 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f 6d  Dyn)!=0 || pFrom
18019 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 6c  ->z==pFrom->zMal
1801a 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e  loc ){.    pTo->
1801b 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 44  flags &= ~(MEM_D
1801c 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45  yn|MEM_Static|ME
1801d 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 61 73  M_Ephem);.    as
1801e 73 65 72 74 28 20 73 72 63 54 79 70 65 3d 3d 4d  sert( srcType==M
1801f 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63 54  EM_Ephem || srcT
18020 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63 20  ype==MEM_Static 
18021 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67  );.    pTo->flag
18022 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20 20  s |= srcType;.  
18023 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  }.}../*.** Make 
18024 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20 70  a full copy of p
18025 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20  From into pTo.  
18026 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f  Prior contents o
18027 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72 65  f pTo are.** fre
18028 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f  ed before the co
18029 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a 53  py is made..*/.S
1802a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1802b 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
1802c 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63  Copy(Mem *pTo, c
1802d 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 29  onst Mem *pFrom)
1802e 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
1802f 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72  ITE_OK;..  asser
18030 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73  t( (pFrom->flags
18031 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
18032 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  0 );.  sqlite3Vd
18033 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65  beMemReleaseExte
18034 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d  rnal(pTo);.  mem
18035 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20  cpy(pTo, pFrom, 
18036 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20  MEMCELLSIZE);.  
18037 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d  pTo->flags &= ~M
18038 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 66 28 20 70  EM_Dyn;..  if( p
18039 54 6f 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53  To->flags&(MEM_S
1803a 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a  tr|MEM_Blob) ){.
1803b 20 20 20 20 69 66 28 20 30 3d 3d 28 70 46 72 6f      if( 0==(pFro
1803c 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61  m->flags&MEM_Sta
1803d 74 69 63 29 20 29 7b 0a 20 20 20 20 20 20 70 54  tic) ){.      pT
1803e 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  o->flags |= MEM_
1803f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 72 63 20  Ephem;.      rc 
18040 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
18041 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 54  MakeWriteable(pT
18042 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  o);.    }.  }.. 
18043 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
18044 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68  *.** Transfer th
18045 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 46  e contents of pF
18046 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 41 6e 79 20  rom to pTo. Any 
18047 65 78 69 73 74 69 6e 67 20 76 61 6c 75 65 20 69  existing value i
18048 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 66 72 65 65  n pTo is.** free
18049 64 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e 74  d. If pFrom cont
1804a 61 69 6e 73 20 65 70 68 65 6d 65 72 61 6c 20 64  ains ephemeral d
1804b 61 74 61 2c 20 61 20 63 6f 70 79 20 69 73 20 6d  ata, a copy is m
1804c 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f 6d  ade..**.** pFrom
1804d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c   contains an SQL
1804e 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 20   NULL when this 
1804f 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e  routine returns.
18050 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18051 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
18052 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a  dbeMemMove(Mem *
18053 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d 29  pTo, Mem *pFrom)
18054 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 6f  {.  assert( pFro
18055 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
18056 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
18057 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  From->db->mutex)
18058 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
18059 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  o->db==0 || sqli
1805a 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1805b 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  To->db->mutex) )
1805c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 6f  ;.  assert( pFro
1805d 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f 2d  m->db==0 || pTo-
1805e 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d  >db==0 || pFrom-
1805f 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b 0a  >db==pTo->db );.
18060 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
18061 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20  mRelease(pTo);. 
18062 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72   memcpy(pTo, pFr
18063 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 29  om, sizeof(Mem))
18064 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 73  ;.  pFrom->flags
18065 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70   = MEM_Null;.  p
18066 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a  From->xDel = 0;.
18067 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63    pFrom->zMalloc
18068 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   = 0;.}../*.** C
18069 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
1806a 6f 66 20 61 20 4d 65 6d 20 74 6f 20 62 65 20 61  of a Mem to be a
1806b 20 73 74 72 69 6e 67 20 6f 72 20 61 20 42 4c 4f   string or a BLO
1806c 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d  B..**.** The mem
1806d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73  ory management s
1806e 74 72 61 74 65 67 79 20 64 65 70 65 6e 64 73 20  trategy depends 
1806f 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  on the value of 
18070 74 68 65 20 78 44 65 6c 0a 2a 2a 20 70 61 72 61  the xDel.** para
18071 6d 65 74 65 72 2e 20 49 66 20 74 68 65 20 76 61  meter. If the va
18072 6c 75 65 20 70 61 73 73 65 64 20 69 73 20 53 51  lue passed is SQ
18073 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 20  LITE_TRANSIENT, 
18074 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74 72  then the .** str
18075 69 6e 67 20 69 73 20 63 6f 70 69 65 64 20 69 6e  ing is copied in
18076 74 6f 20 61 20 28 70 6f 73 73 69 62 6c 79 20 65  to a (possibly e
18077 78 69 73 74 69 6e 67 29 20 62 75 66 66 65 72 20  xisting) buffer 
18078 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 0a  managed by the .
18079 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65  ** Mem structure
1807a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79  . Otherwise, any
1807b 20 65 78 69 73 74 69 6e 67 20 62 75 66 66 65 72   existing buffer
1807c 20 69 73 20 66 72 65 65 64 20 61 6e 64 20 74 68   is freed and th
1807d 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f 70  e.** pointer cop
1807e 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ied..*/.SQLITE_P
1807f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18080 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
18081 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20  .  Mem *pMem,   
18082 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
18083 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20   cell to set to 
18084 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a  string value */.
18085 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c    const char *z,
18086 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
18087 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  pointer */.  int
18088 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
18089 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72   /* Bytes in str
1808a 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65  ing, or negative
1808b 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20   */.  u8 enc,   
1808c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63            /* Enc
1808d 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66  oding of z.  0 f
1808e 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f  or BLOBs */.  vo
1808f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
18090 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20  ) /* Destructor 
18091 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  function */.){. 
18092 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20   int nByte = n; 
18093 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75       /* New valu
18094 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f  e for pMem->n */
18095 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20  .  int iLimit;  
18096 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
18097 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67  m allowed string
18098 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f   or blob size */
18099 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20 30  .  u16 flags = 0
1809a 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61  ;      /* New va
1809b 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c  lue for pMem->fl
1809c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ags */..  assert
1809d 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
1809e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1809f 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
180a0 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
180a1 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
180a2 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
180a3 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73  );..  /* If z is
180a4 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
180a5 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e   set pMem to con
180a6 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c  tain an SQL NULL
180a7 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b  . */.  if( !z ){
180a8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
180a9 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29  MemSetNull(pMem)
180aa 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
180ab 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69  ITE_OK;.  }..  i
180ac 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20  f( pMem->db ){. 
180ad 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d     iLimit = pMem
180ae 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ->db->aLimit[SQL
180af 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
180b0 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
180b1 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f  iLimit = SQLITE_
180b2 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a  MAX_LENGTH;.  }.
180b3 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d    flags = (enc==
180b4 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53  0?MEM_Blob:MEM_S
180b5 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65  tr);.  if( nByte
180b6 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  <0 ){.    assert
180b7 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20  ( enc!=0 );.    
180b8 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  if( enc==SQLITE_
180b9 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66 6f  UTF8 ){.      fo
180ba 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65  r(nByte=0; nByte
180bb 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42  <=iLimit && z[nB
180bc 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d  yte]; nByte++){}
180bd 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
180be 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e    for(nByte=0; n
180bf 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20  Byte<=iLimit && 
180c0 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42  (z[nByte] | z[nB
180c1 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d  yte+1]); nByte+=
180c2 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66  2){}.    }.    f
180c3 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d  lags |= MEM_Term
180c4 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
180c5 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20  following block 
180c6 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 61 6c  sets the new val
180c7 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64  ues of Mem.z and
180c8 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20   Mem.xDel. It.  
180c9 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20 66  ** also sets a f
180ca 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72  lag in local var
180cb 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74 6f  iable "flags" to
180cc 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d 65   indicate the me
180cd 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65  mory.  ** manage
180ce 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d  ment (one of MEM
180cf 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74  _Dyn or MEM_Stat
180d0 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ic)..  */.  if( 
180d1 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41  xDel==SQLITE_TRA
180d2 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e  NSIENT ){.    in
180d3 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65  t nAlloc = nByte
180d4 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 26  ;.    if( flags&
180d5 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20  MEM_Term ){.    
180d6 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63    nAlloc += (enc
180d7 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a  ==SQLITE_UTF8?1:
180d8 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  2);.    }.    if
180d9 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29  ( nByte>iLimit )
180da 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
180db 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20  QLITE_TOOBIG;.  
180dc 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
180dd 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
180de 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20  Mem, nAlloc, 0) 
180df 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
180e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
180e1 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70    }.    memcpy(p
180e2 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f  Mem->z, z, nAllo
180e3 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  c);.  }else if( 
180e4 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e  xDel==SQLITE_DYN
180e5 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c 69  AMIC ){.    sqli
180e6 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
180e7 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65  e(pMem);.    pMe
180e8 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65  m->zMalloc = pMe
180e9 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a  m->z = (char *)z
180ea 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c  ;.    pMem->xDel
180eb 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
180ec 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
180ed 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
180ee 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63      pMem->z = (c
180ef 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65  har *)z;.    pMe
180f0 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a  m->xDel = xDel;.
180f1 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 78      flags |= ((x
180f2 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54  Del==SQLITE_STAT
180f3 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d  IC)?MEM_Static:M
180f4 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69  EM_Dyn);.  }.  i
180f5 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20  f( nByte>iLimit 
180f6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
180f7 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d  LITE_TOOBIG;.  }
180f8 0a 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42  ..  pMem->n = nB
180f9 79 74 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  yte;.  pMem->fla
180fa 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d  gs = flags;.  pM
180fb 65 6d 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d  em->enc = (enc==
180fc 30 20 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 20  0 ? SQLITE_UTF8 
180fd 3a 20 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e  : enc);.  pMem->
180fe 74 79 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f  type = (enc==0 ?
180ff 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53   SQLITE_BLOB : S
18100 51 4c 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69  QLITE_TEXT);..#i
18101 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18102 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 70 4d  T_UTF16.  if( pM
18103 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f  em->enc!=SQLITE_
18104 55 54 46 38 20 26 26 20 73 71 6c 69 74 65 33 56  UTF8 && sqlite3V
18105 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28  dbeMemHandleBom(
18106 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 74  pMem) ){.    ret
18107 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
18108 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
18109 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1810a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61  ;.}../*.** Compa
1810b 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f  re the values co
1810c 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 74  ntained by the t
1810d 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c  wo memory cells,
1810e 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65   returning.** ne
1810f 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20  gative, zero or 
18110 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d  positive if pMem
18111 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20  1 is less than, 
18112 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65  equal to, or gre
18113 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65  ater.** than pMe
18114 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65  m2. Sorting orde
18115 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73  r is NULL's firs
18116 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e  t, followed by n
18117 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65 72 73  umbers (integers
18118 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73  .** and reals) s
18119 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c  orted numericall
1811a 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74  y, followed by t
1811b 65 78 74 20 6f 72 64 65 72 65 64 20 62 79 20 74  ext ordered by t
1811c 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20  he collating.** 
1811d 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61  sequence pColl a
1811e 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27  nd finally blob'
1811f 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d  s ordered by mem
18120 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f  cmp()..**.** Two
18121 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65   NULL values are
18122 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61   considered equa
18123 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  l by this functi
18124 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
18125 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18126 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73  3MemCompare(cons
18127 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f  t Mem *pMem1, co
18128 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20  nst Mem *pMem2, 
18129 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70  const CollSeq *p
1812a 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Coll){.  int rc;
1812b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20  .  int f1, f2;. 
1812c 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c   int combined_fl
1812d 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72  ags;..  /* Inter
1812e 63 68 61 6e 67 65 20 70 4d 65 6d 31 20 61 6e 64  change pMem1 and
1812f 20 70 4d 65 6d 32 20 69 66 20 74 68 65 20 63 6f   pMem2 if the co
18130 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
18131 20 73 70 65 63 69 66 69 65 73 0a 20 20 2a 2a 20   specifies.  ** 
18132 44 45 53 43 20 6f 72 64 65 72 2e 0a 20 20 2a 2f  DESC order..  */
18133 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 2d 3e 66  .  f1 = pMem1->f
18134 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 70 4d 65  lags;.  f2 = pMe
18135 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6d  m2->flags;.  com
18136 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d 20 66 31  bined_flags = f1
18137 7c 66 32 3b 0a 20 20 61 73 73 65 72 74 28 20 28  |f2;.  assert( (
18138 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 26  combined_flags &
18139 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
1813a 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65  );. .  /* If one
1813b 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20   value is NULL, 
1813c 69 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  it is less than 
1813d 74 68 65 20 6f 74 68 65 72 2e 20 49 66 20 62 6f  the other. If bo
1813e 74 68 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61  th values.  ** a
1813f 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20  re NULL, return 
18140 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f  0..  */.  if( co
18141 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d  mbined_flags&MEM
18142 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74  _Null ){.    ret
18143 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c  urn (f2&MEM_Null
18144 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c  ) - (f1&MEM_Null
18145 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
18146 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 6e  one value is a n
18147 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 6f 74  umber and the ot
18148 68 65 72 20 69 73 20 6e 6f 74 2c 20 74 68 65 20  her is not, the 
18149 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 2e 0a  number is less..
1814a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65    ** If both are
1814b 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72   numbers, compar
1814c 65 20 61 73 20 72 65 61 6c 73 20 69 66 20 6f 6e  e as reals if on
1814d 65 20 69 73 20 61 20 72 65 61 6c 2c 20 6f 72 20  e is a real, or 
1814e 61 73 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a  as integers.  **
1814f 20 69 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20   if both values 
18150 61 72 65 20 69 6e 74 65 67 65 72 73 2e 0a 20 20  are integers..  
18151 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65  */.  if( combine
18152 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74  d_flags&(MEM_Int
18153 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20  |MEM_Real) ){.  
18154 20 20 69 66 28 20 21 28 66 31 26 28 4d 45 4d 5f    if( !(f1&(MEM_
18155 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29  Int|MEM_Real)) )
18156 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
18157 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
18158 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45  !(f2&(MEM_Int|ME
18159 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20 20 20  M_Real)) ){.    
1815a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20    return -1;.   
1815b 20 7d 0a 20 20 20 20 69 66 28 20 28 66 31 20 26   }.    if( (f1 &
1815c 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d   f2 & MEM_Int)==
1815d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c  0 ){.      doubl
1815e 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 20 20 20  e r1, r2;.      
1815f 69 66 28 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c  if( (f1&MEM_Real
18160 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
18161 72 31 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65  r1 = (double)pMe
18162 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d  m1->u.i;.      }
18163 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31  else{.        r1
18164 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20   = pMem1->r;.   
18165 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28     }.      if( (
18166 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20  f2&MEM_Real)==0 
18167 29 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20  ){.        r2 = 
18168 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32 2d 3e 75  (double)pMem2->u
18169 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  .i;.      }else{
1816a 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 70 4d  .        r2 = pM
1816b 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a  em2->r;.      }.
1816c 20 20 20 20 20 20 69 66 28 20 72 31 3c 72 32 20        if( r1<r2 
1816d 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20  ) return -1;.   
1816e 20 20 20 69 66 28 20 72 31 3e 72 32 20 29 20 72     if( r1>r2 ) r
1816f 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72  eturn 1;.      r
18170 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c  eturn 0;.    }el
18171 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
18172 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a  ( f1&MEM_Int );.
18173 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 32        assert( f2
18174 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20  &MEM_Int );.    
18175 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69    if( pMem1->u.i
18176 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20   < pMem2->u.i ) 
18177 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20  return -1;.     
18178 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20   if( pMem1->u.i 
18179 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72  > pMem2->u.i ) r
1817a 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72  eturn 1;.      r
1817b 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
1817c 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20   }..  /* If one 
1817d 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  value is a strin
1817e 67 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20  g and the other 
1817f 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65 20 73  is a blob, the s
18180 74 72 69 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20  tring is less.. 
18181 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20   ** If both are 
18182 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65  strings, compare
18183 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61   using the colla
18184 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  ting functions..
18185 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69    */.  if( combi
18186 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74  ned_flags&MEM_St
18187 72 20 29 7b 0a 20 20 20 20 69 66 28 20 28 66 31  r ){.    if( (f1
18188 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29   & MEM_Str)==0 )
18189 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
1818a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1818b 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d  (f2 & MEM_Str)==
1818c 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
1818d 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n -1;.    }..   
1818e 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e   assert( pMem1->
1818f 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20  enc==pMem2->enc 
18190 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
18191 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  Mem1->enc==SQLIT
18192 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20  E_UTF8 || .     
18193 20 20 20 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e         pMem1->en
18194 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  c==SQLITE_UTF16L
18195 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d  E || pMem1->enc=
18196 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
18197 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63  );..    /* The c
18198 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
18199 65 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65  e must be define
1819a 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c  d at this point,
1819b 20 65 76 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20   even if.    ** 
1819c 74 68 65 20 75 73 65 72 20 64 65 6c 65 74 65 73  the user deletes
1819d 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
1819e 65 71 75 65 6e 63 65 20 61 66 74 65 72 20 74 68  equence after th
1819f 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69  e vdbe program i
181a0 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65  s.    ** compile
181a1 64 20 28 74 68 69 73 20 77 61 73 20 6e 6f 74 20  d (this was not 
181a2 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 29  always the case)
181a3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
181a4 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70  ert( !pColl || p
181a5 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20  Coll->xCmp );.. 
181a6 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
181a7 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d        if( pMem1-
181a8 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63  >enc==pColl->enc
181a9 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
181aa 68 65 20 73 74 72 69 6e 67 73 20 61 72 65 20 61  he strings are a
181ab 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 6f  lready in the co
181ac 72 72 65 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20  rrect encoding. 
181ad 20 43 61 6c 6c 20 74 68 65 0a 20 20 20 20 20 20   Call the.      
181ae 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20    ** comparison 
181af 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 63 74 6c  function directl
181b0 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74  y */.        ret
181b1 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28  urn pColl->xCmp(
181b2 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65  pColl->pUser,pMe
181b3 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70  m1->n,pMem1->z,p
181b4 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a  Mem2->n,pMem2->z
181b5 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
181b6 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f          const vo
181b7 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20  id *v1, *v2;.   
181b8 20 20 20 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b       int n1, n2;
181b9 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63 31 3b  .        Mem c1;
181ba 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63 32 3b  .        Mem c2;
181bb 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
181bc 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63  &c1, 0, sizeof(c
181bd 31 29 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  1));.        mem
181be 73 65 74 28 26 63 32 2c 20 30 2c 20 73 69 7a 65  set(&c2, 0, size
181bf 6f 66 28 63 32 29 29 3b 0a 20 20 20 20 20 20 20  of(c2));.       
181c0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
181c1 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20  hallowCopy(&c1, 
181c2 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d  pMem1, MEM_Ephem
181c3 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
181c4 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
181c5 43 6f 70 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c  Copy(&c2, pMem2,
181c6 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20   MEM_Ephem);.   
181c7 20 20 20 20 20 76 31 20 3d 20 73 71 6c 69 74 65       v1 = sqlite
181c8 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69  3ValueText((sqli
181c9 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20  te3_value*)&c1, 
181ca 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20  pColl->enc);.   
181cb 20 20 20 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20       n1 = v1==0 
181cc 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20  ? 0 : c1.n;.    
181cd 20 20 20 20 76 32 20 3d 20 73 71 6c 69 74 65 33      v2 = sqlite3
181ce 56 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74  ValueText((sqlit
181cf 65 33 5f 76 61 6c 75 65 2a 29 26 63 32 2c 20 70  e3_value*)&c2, p
181d0 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20  Coll->enc);.    
181d1 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f      n2 = v2==0 ?
181d2 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20   0 : c2.n;.     
181d3 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78     rc = pColl->x
181d4 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72  Cmp(pColl->pUser
181d5 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32  , n1, v1, n2, v2
181d6 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
181d7 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
181d8 28 26 63 31 29 3b 0a 20 20 20 20 20 20 20 20 73  (&c1);.        s
181d9 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
181da 65 61 73 65 28 26 63 32 29 3b 0a 20 20 20 20 20  ease(&c2);.     
181db 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
181dc 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
181dd 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69  /* If a NULL poi
181de 6e 74 65 72 20 77 61 73 20 70 61 73 73 65 64 20  nter was passed 
181df 61 73 20 74 68 65 20 63 6f 6c 6c 61 74 65 20 66  as the collate f
181e0 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68  unction, fall th
181e1 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20  rough.    ** to 
181e2 74 68 65 20 62 6c 6f 62 20 63 61 73 65 20 61 6e  the blob case an
181e3 64 20 75 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20  d use memcmp(). 
181e4 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42   */.  }. .  /* B
181e5 6f 74 68 20 76 61 6c 75 65 73 20 6d 75 73 74 20  oth values must 
181e6 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61  be blobs.  Compa
181e7 72 65 20 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28  re using memcmp(
181e8 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65  ).  */.  rc = me
181e9 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70  mcmp(pMem1->z, p
181ea 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d  Mem2->z, (pMem1-
181eb 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65  >n>pMem2->n)?pMe
181ec 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b  m2->n:pMem1->n);
181ed 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a  .  if( rc==0 ){.
181ee 20 20 20 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e      rc = pMem1->
181ef 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20  n - pMem2->n;.  
181f0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
181f1 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74  ../*.** Move dat
181f2 61 20 6f 75 74 20 6f 66 20 61 20 62 74 72 65 65  a out of a btree
181f3 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 69 65   key or data fie
181f4 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65  ld and into a Me
181f5 6d 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  m structure..** 
181f6 54 68 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20  The data or key 
181f7 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68  is taken from th
181f8 65 20 65 6e 74 72 79 20 74 68 61 74 20 70 43 75  e entry that pCu
181f9 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  r is currently p
181fa 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20  ointing.** to.  
181fb 6f 66 66 73 65 74 20 61 6e 64 20 61 6d 74 20 64  offset and amt d
181fc 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 70 6f  etermine what po
181fd 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74  rtion of the dat
181fe 61 20 6f 72 20 6b 65 79 20 74 6f 20 72 65 74 72  a or key to retr
181ff 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20  ieve..** key is 
18200 74 72 75 65 20 74 6f 20 67 65 74 20 74 68 65 20  true to get the 
18201 6b 65 79 20 6f 72 20 66 61 6c 73 65 20 74 6f 20  key or false to 
18202 67 65 74 20 64 61 74 61 2e 20 20 54 68 65 20 72  get data.  The r
18203 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e  esult is written
18204 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 70 4d 65  .** into the pMe
18205 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  m element..**.**
18206 20 54 68 65 20 70 4d 65 6d 20 73 74 72 75 63 74   The pMem struct
18207 75 72 65 20 69 73 20 61 73 73 75 6d 65 64 20 74  ure is assumed t
18208 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a  o be uninitializ
18209 65 64 2e 20 20 41 6e 79 20 70 72 69 6f 72 20 63  ed.  Any prior c
1820a 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65  ontent.** is ove
1820b 72 77 72 69 74 74 65 6e 20 77 69 74 68 6f 75 74  rwritten without
1820c 20 62 65 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a   being freed..**
1820d 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74  .** If this rout
1820e 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e  ine fails for an
1820f 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63  y reason (malloc
18210 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72   returns NULL or
18211 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65   unable.** to re
18212 61 64 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b  ad from the disk
18213 29 20 74 68 65 6e 20 74 68 65 20 70 4d 65 6d 20  ) then the pMem 
18214 69 73 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e  is left in an in
18215 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65  consistent state
18216 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18217 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
18218 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
18219 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
1821a 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70  r,   /* Cursor p
1821b 6f 69 6e 74 69 6e 67 20 61 74 20 72 65 63 6f 72  ointing at recor
1821c 64 20 74 6f 20 72 65 74 72 69 65 76 65 2e 20 2a  d to retrieve. *
1821d 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 2c 20  /.  int offset, 
1821e 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1821f 66 72 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f  from the start o
18220 66 20 64 61 74 61 20 74 6f 20 72 65 74 75 72 6e  f data to return
18221 20 62 79 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a   bytes from. */.
18222 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20    int amt,      
18223 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
18224 20 62 79 74 65 73 20 74 6f 20 72 65 74 75 72 6e   bytes to return
18225 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20  . */.  int key, 
18226 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74           /* If t
18227 72 75 65 2c 20 72 65 74 72 69 65 76 65 20 66 72  rue, retrieve fr
18228 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6b 65 79  om the btree key
18229 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20  , not data. */. 
1822a 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20   Mem *pMem      
1822b 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72     /* OUT: Retur
1822c 6e 20 64 61 74 61 20 69 6e 20 74 68 69 73 20 4d  n data in this M
1822d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f  em structure. */
1822e 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 61 74  .){.  char *zDat
1822f 61 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  a;       /* Data
18230 20 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20   from the btree 
18231 6c 61 79 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61  layer */.  int a
18232 76 61 69 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a  vailable = 0; /*
18233 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
18234 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68   available on th
18235 65 20 6c 6f 63 61 6c 20 62 74 72 65 65 20 70 61  e local btree pa
18236 67 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ge */.  sqlite3 
18237 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61  *db;       /* Da
18238 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
18239 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  n */.  int rc = 
1823a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62  SQLITE_OK;..  db
1823b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
1823c 75 72 73 6f 72 44 62 28 70 43 75 72 29 3b 0a 20  ursorDb(pCur);. 
1823d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1823e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
1823f 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
18240 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
18241 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
18242 30 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 20 29  0 );.  if( key )
18243 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63  {.    zData = (c
18244 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74 72  har *)sqlite3Btr
18245 65 65 4b 65 79 46 65 74 63 68 28 70 43 75 72 2c  eeKeyFetch(pCur,
18246 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20   &available);.  
18247 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61  }else{.    zData
18248 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
18249 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
1824a 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c  (pCur, &availabl
1824b 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  e);.  }.  assert
1824c 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20  ( zData!=0 );.. 
1824d 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c   if( offset+amt<
1824e 3d 61 76 61 69 6c 61 62 6c 65 20 26 26 20 28 28  =available && ((
1824f 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
18250 44 79 6e 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d  Dyn)==0 || pMem-
18251 3e 78 44 65 6c 29 20 29 7b 0a 20 20 20 20 73 71  >xDel) ){.    sq
18252 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
18253 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70  ase(pMem);.    p
18254 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 5b  Mem->z = &zData[
18255 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70 4d 65  offset];.    pMe
18256 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42  m->flags = MEM_B
18257 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20  lob|MEM_Ephem;. 
18258 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54   }else if( SQLIT
18259 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
1825a 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
1825b 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 29 20  Mem, amt+2, 0)) 
1825c 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
1825d 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45  gs = MEM_Blob|ME
1825e 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Dyn|MEM_Term;.
1825f 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
18260 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  0;.    pMem->typ
18261 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b  e = SQLITE_BLOB;
18262 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29 7b 0a  .    if( key ){.
18263 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
18264 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c  e3BtreeKey(pCur,
18265 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d   offset, amt, pM
18266 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73  em->z);.    }els
18267 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
18268 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 70  lite3BtreeData(p
18269 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74  Cur, offset, amt
1826a 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20  , pMem->z);.    
1826b 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d  }.    pMem->z[am
1826c 74 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d  t] = 0;.    pMem
1826d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a  ->z[amt+1] = 0;.
1826e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1826f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
18270 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
18271 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  ease(pMem);.    
18272 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20  }.  }.  pMem->n 
18273 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 74 75 72 6e  = amt;..  return
18274 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 30 0a 2f 2a   rc;.}..#if 0./*
18275 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 76 61 72 69  .** Perform vari
18276 6f 75 73 20 63 68 65 63 6b 73 20 6f 6e 20 74 68  ous checks on th
18277 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70 4d  e memory cell pM
18278 65 6d 2e 20 41 6e 20 61 73 73 65 72 74 28 29 20  em. An assert() 
18279 77 69 6c 6c 0a 2a 2a 20 66 61 69 6c 20 69 66 20  will.** fail if 
1827a 70 4d 65 6d 20 69 73 20 69 6e 74 65 72 6e 61 6c  pMem is internal
1827b 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2e  ly inconsistent.
1827c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1827d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1827e 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65 6d  dbeMemSanity(Mem
1827f 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66   *pMem){.  int f
18280 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61  lags = pMem->fla
18281 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 66 6c  gs;.  assert( fl
18282 61 67 73 21 3d 30 20 29 3b 20 20 2f 2a 20 4d 75  ags!=0 );  /* Mu
18283 73 74 20 64 65 66 69 6e 65 20 73 6f 6d 65 20 74  st define some t
18284 79 70 65 20 2a 2f 0a 20 20 69 66 28 20 66 6c 61  ype */.  if( fla
18285 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45  gs & (MEM_Str|ME
18286 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69  M_Blob) ){.    i
18287 6e 74 20 78 20 3d 20 66 6c 61 67 73 20 26 20 28  nt x = flags & (
18288 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44  MEM_Static|MEM_D
18289 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d  yn|MEM_Ephem|MEM
1828a 5f 53 68 6f 72 74 29 3b 0a 20 20 20 20 61 73 73  _Short);.    ass
1828b 65 72 74 28 20 78 21 3d 30 20 29 3b 20 20 20 20  ert( x!=0 );    
1828c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e          /* Strin
1828d 67 73 20 6d 75 73 74 20 64 65 66 69 6e 65 20 61  gs must define a
1828e 20 73 74 72 69 6e 67 20 73 75 62 74 79 70 65 20   string subtype 
1828f 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  */.    assert( (
18290 78 20 26 20 28 78 2d 31 29 29 3d 3d 30 20 29 3b  x & (x-1))==0 );
18291 20 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 74    /* Only one st
18292 72 69 6e 67 20 73 75 62 74 79 70 65 20 63 61 6e  ring subtype can
18293 20 62 65 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20   be defined */. 
18294 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
18295 3e 7a 21 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a  >z!=0 );      /*
18296 20 53 74 72 69 6e 67 73 20 6d 75 73 74 20 68 61   Strings must ha
18297 76 65 20 61 20 76 61 6c 75 65 20 2a 2f 0a 20 20  ve a value */.  
18298 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74    /* Mem.z point
18299 73 20 74 6f 20 4d 65 6d 2e 7a 53 68 6f 72 74 20  s to Mem.zShort 
1829a 69 66 66 20 74 68 65 20 73 75 62 74 79 70 65 20  iff the subtype 
1829b 69 73 20 4d 45 4d 5f 53 68 6f 72 74 20 2a 2f 0a  is MEM_Short */.
1829c 20 20 20 20 61 73 73 65 72 74 28 20 28 78 20 26      assert( (x &
1829d 20 4d 45 4d 5f 53 68 6f 72 74 29 3d 3d 30 20 7c   MEM_Short)==0 |
1829e 7c 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d  | pMem->z==pMem-
1829f 3e 7a 53 68 6f 72 74 20 29 3b 0a 20 20 20 20 61  >zShort );.    a
182a0 73 73 65 72 74 28 20 28 78 20 26 20 4d 45 4d 5f  ssert( (x & MEM_
182a1 53 68 6f 72 74 29 21 3d 30 20 7c 7c 20 70 4d 65  Short)!=0 || pMe
182a2 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 53 68 6f  m->z!=pMem->zSho
182a3 72 74 20 29 3b 0a 20 20 20 20 2f 2a 20 4e 6f 20  rt );.    /* No 
182a4 64 65 73 74 72 75 63 74 6f 72 20 75 6e 6c 65 73  destructor unles
182a5 73 20 74 68 65 72 65 20 69 73 20 4d 45 4d 5f 44  s there is MEM_D
182a6 79 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  yn */.    assert
182a7 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d 30 20  ( pMem->xDel==0 
182a8 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  || (pMem->flags 
182a9 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 29 3b  & MEM_Dyn)!=0 );
182aa 0a 0a 20 20 20 20 69 66 28 20 28 66 6c 61 67 73  ..    if( (flags
182ab 20 26 20 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20   & MEM_Str) ){. 
182ac 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65       assert( pMe
182ad 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  m->enc==SQLITE_U
182ae 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  TF8 || .        
182af 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 3d        pMem->enc=
182b0 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
182b1 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ||.             
182b2 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49   pMem->enc==SQLI
182b3 54 45 5f 55 54 46 31 36 4c 45 20 0a 20 20 20 20  TE_UTF16LE .    
182b4 20 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 49 66    );.      /* If
182b5 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 55   the string is U
182b6 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 61 6e 64  TF-8 encoded and
182b7 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 2c   nul terminated,
182b8 20 74 68 65 6e 20 70 4d 65 6d 2d 3e 6e 0a 20 20   then pMem->n.  
182b9 20 20 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 74      ** must be t
182ba 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
182bb 20 73 74 72 69 6e 67 2e 20 20 28 4c 61 74 65 72   string.  (Later
182bc 3a 29 20 20 49 66 20 74 68 65 20 64 61 74 61 62  :)  If the datab
182bd 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a  ase file.      *
182be 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72 75  * has been corru
182bf 70 74 65 64 2c 20 27 5c 30 30 30 27 20 63 68 61  pted, '\000' cha
182c0 72 61 63 74 65 72 73 20 6d 69 67 68 74 20 68 61  racters might ha
182c1 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64  ve been inserted
182c2 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74  .      ** into t
182c3 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 65  he middle of the
182c4 20 73 74 72 69 6e 67 2e 20 20 49 6e 20 74 68 61   string.  In tha
182c5 74 20 63 61 73 65 2c 20 74 68 65 20 73 71 6c 69  t case, the sqli
182c6 74 65 33 53 74 72 6c 65 6e 33 30 28 29 0a 20 20  te3Strlen30().  
182c7 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20      ** might be 
182c8 6c 65 73 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  less..      */. 
182c9 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65       if( pMem->e
182ca 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
182cb 26 26 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f  && (flags & MEM_
182cc 54 65 72 6d 29 20 29 7b 20 0a 20 20 20 20 20 20  Term) ){ .      
182cd 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
182ce 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e  3Strlen30(pMem->
182cf 7a 29 3c 3d 70 4d 65 6d 2d 3e 6e 20 29 3b 0a 20  z)<=pMem->n );. 
182d0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
182d1 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 3d  Mem->z[pMem->n]=
182d2 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  =0 );.      }.  
182d3 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
182d4 20 2f 2a 20 43 61 6e 6e 6f 74 20 64 65 66 69 6e   /* Cannot defin
182d5 65 20 61 20 73 74 72 69 6e 67 20 73 75 62 74 79  e a string subty
182d6 70 65 20 66 6f 72 20 6e 6f 6e 2d 73 74 72 69 6e  pe for non-strin
182d7 67 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 20  g objects */.   
182d8 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
182d9 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 61  flags & (MEM_Sta
182da 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f  tic|MEM_Dyn|MEM_
182db 45 70 68 65 6d 7c 4d 45 4d 5f 53 68 6f 72 74 29  Ephem|MEM_Short)
182dc 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  )==0 );.    asse
182dd 72 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d  rt( pMem->xDel==
182de 30 20 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 4d 45  0 );.  }.  /* ME
182df 4d 5f 4e 75 6c 6c 20 65 78 63 6c 75 64 65 73 20  M_Null excludes 
182e0 61 6c 6c 20 6f 74 68 65 72 20 74 79 70 65 73 20  all other types 
182e1 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  */.  assert( (pM
182e2 65 6d 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53  em->flags&(MEM_S
182e3 74 72 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52  tr|MEM_Int|MEM_R
182e4 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d  eal|MEM_Blob))==
182e5 30 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28  0.          || (
182e6 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
182e7 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 2f 2a  Null)==0 );.  /*
182e8 20 49 66 20 74 68 65 20 4d 45 4d 20 69 73 20 62   If the MEM is b
182e9 6f 74 68 20 72 65 61 6c 20 61 6e 64 20 69 6e 74  oth real and int
182ea 65 67 65 72 2c 20 74 68 65 20 76 61 6c 75 65 73  eger, the values
182eb 20 61 72 65 20 65 71 75 61 6c 20 2a 2f 0a 20 20   are equal */.  
182ec 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
182ed 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c  lags & (MEM_Int|
182ee 4d 45 4d 5f 52 65 61 6c 29 29 21 3d 28 4d 45 4d  MEM_Real))!=(MEM
182ef 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 0a  _Int|MEM_Real) .
182f0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 4d 65            || pMe
182f1 6d 2d 3e 72 3d 3d 70 4d 65 6d 2d 3e 75 2e 69 20  m->r==pMem->u.i 
182f2 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  );.}.#endif../* 
182f3 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
182f4 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
182f5 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69  internally, it i
182f6 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68  s not part of th
182f7 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50  e.** external AP
182f8 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61  I. It works in a
182f9 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20   similar way to 
182fa 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
182fb 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20  xt(),.** except 
182fc 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65  the data returne
182fd 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f  d is in the enco
182fe 64 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62  ding specified b
182ff 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  y the second.** 
18300 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68  parameter, which
18301 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20   must be one of 
18302 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20  SQLITE_UTF16BE, 
18303 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f  SQLITE_UTF16LE o
18304 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38  r.** SQLITE_UTF8
18305 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32  ..**.** (2006-02
18306 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76  -16:)  The enc v
18307 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65  alue can be or-e
18308 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54  d with SQLITE_UT
18309 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20  F16_ALIGNED..** 
1830a 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63  If that is the c
1830b 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ase, then the re
1830c 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69  sult must be ali
1830d 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20  gned on an even 
1830e 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79  byte.** boundary
1830f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18310 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ATE const void *
18311 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
18312 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20  (sqlite3_value* 
18313 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20  pVal, u8 enc){. 
18314 20 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74   if( !pVal ) ret
18315 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  urn 0;..  assert
18316 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pVal->db==0 ||
18317 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
18318 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75  eld(pVal->db->mu
18319 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
1831a 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26  ( (enc&3)==(enc&
1831b 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
1831c 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65  IGNED) );.  asse
1831d 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73  rt( (pVal->flags
1831e 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
1831f 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c  0 );..  if( pVal
18320 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c  ->flags&MEM_Null
18321 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
18322 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
18323 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d  (MEM_Blob>>3) ==
18324 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56   MEM_Str );.  pV
18325 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56  al->flags |= (pV
18326 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  al->flags & MEM_
18327 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61  Blob)>>3;.  expa
18328 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20  ndBlob(pVal);.  
18329 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26  if( pVal->flags&
1832a 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73  MEM_Str ){.    s
1832b 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1832c 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65  Encoding(pVal, e
1832d 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46  nc & ~SQLITE_UTF
1832e 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20  16_ALIGNED);.   
1832f 20 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49   if( (enc & SQLI
18330 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
18331 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51  )!=0 && 1==(1&SQ
18332 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
18333 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20  pVal->z)) ){.   
18334 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c     assert( (pVal
18335 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45  ->flags & (MEM_E
18336 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29  phem|MEM_Static)
18337 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  )!=0 );.      if
18338 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
18339 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56  MakeWriteable(pV
1833a 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  al)!=SQLITE_OK )
1833b 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
1833c 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1833d 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
1833e 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65  eMemNulTerminate
1833f 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pVal);.  }else{
18340 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56  .    assert( (pV
18341 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c  al->flags&MEM_Bl
18342 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71  ob)==0 );.    sq
18343 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69  lite3VdbeMemStri
18344 6e 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29  ngify(pVal, enc)
18345 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d  ;.    assert( 0=
18346 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54  =(1&SQLITE_PTR_T
18347 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20  O_INT(pVal->z)) 
18348 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  );.  }.  assert(
18349 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20  pVal->enc==(enc 
1834a 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  & ~SQLITE_UTF16_
1834b 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c  ALIGNED) || pVal
1834c 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20  ->db==0.        
1834d 20 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64        || pVal->d
1834e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1834f 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65  );.  if( pVal->e
18350 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49  nc==(enc & ~SQLI
18351 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
18352 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
18353 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65  pVal->z;.  }else
18354 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
18355 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65    }.}../*.** Cre
18356 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65  ate a new sqlite
18357 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a  3_value object..
18358 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18359 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  E sqlite3_value 
1835a 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77  *sqlite3ValueNew
1835b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
1835c 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65   Mem *p = sqlite
1835d 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
1835e 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20  , sizeof(*p));. 
1835f 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d   if( p ){.    p-
18360 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
18361 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d  l;.    p->type =
18362 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20   SQLITE_NULL;.  
18363 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20    p->db = db;.  
18364 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
18365 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
18366 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  new sqlite3_valu
18367 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69  e object, contai
18368 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f  ning the value o
18369 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54  f pExpr..**.** T
1836a 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66  his only works f
1836b 6f 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65  or very simple e
1836c 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20  xpressions that 
1836d 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63  consist of one c
1836e 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e  onstant.** token
1836f 20 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31   (i.e. "5", "5.1
18370 22 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29  ", "'a string'")
18371 2e 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  . If the express
18372 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f  ion can.** be co
18373 6e 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79  nverted directly
18374 20 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74   into a value, t
18375 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73  hen the value is
18376 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a   allocated and.*
18377 2a 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74  * a pointer writ
18378 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54  ten to *ppVal. T
18379 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
1837a 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61  ponsible for dea
1837b 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65  llocating.** the
1837c 20 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e   value by passin
1837d 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56  g it to sqlite3V
1837e 61 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72  alueFree() later
1837f 20 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72   on. If the expr
18380 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74  ession.** cannot
18381 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f   be converted to
18382 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a   a value, then *
18383 70 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20  ppVal is set to 
18384 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
18385 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18386 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
18387 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
18388 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18389 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
1838a 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  nnection */.  Ex
1838b 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20  pr *pExpr,      
1838c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
1838d 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61  xpression to eva
1838e 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e  luate */.  u8 en
1838f 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
18390 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67       /* Encoding
18391 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20   to use */.  u8 
18392 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20  affinity,       
18393 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69         /* Affini
18394 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73  ty to use */.  s
18395 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
18396 70 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74  pVal     /* Writ
18397 65 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20  e the new value 
18398 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
18399 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61   op;.  char *zVa
1839a 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  l = 0;.  sqlite3
1839b 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30  _value *pVal = 0
1839c 3b 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20  ;..  if( !pExpr 
1839d 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20  ){.    *ppVal = 
1839e 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
1839f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f  LITE_OK;.  }.  o
183a0 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 0a  p = pExpr->op;..
183a1 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52    if( op==TK_STR
183a2 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c  ING || op==TK_FL
183a3 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e  OAT || op==TK_IN
183a4 54 45 47 45 52 20 29 7b 0a 20 20 20 20 7a 56 61  TEGER ){.    zVa
183a5 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  l = sqlite3DbStr
183a6 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29  NDup(db, (char*)
183a7 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20  pExpr->token.z, 
183a8 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b  pExpr->token.n);
183a9 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69  .    pVal = sqli
183aa 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b  te3ValueNew(db);
183ab 0a 20 20 20 20 69 66 28 20 21 7a 56 61 6c 20 7c  .    if( !zVal |
183ac 7c 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e  | !pVal ) goto n
183ad 6f 5f 6d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  o_mem;.    sqlit
183ae 65 33 44 65 71 75 6f 74 65 28 7a 56 61 6c 29 3b  e3Dequote(zVal);
183af 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
183b0 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31  eSetStr(pVal, -1
183b1 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55  , zVal, SQLITE_U
183b2 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41  TF8, SQLITE_DYNA
183b3 4d 49 43 29 3b 0a 20 20 20 20 69 66 28 20 28 6f  MIC);.    if( (o
183b4 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c  p==TK_INTEGER ||
183b5 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20   op==TK_FLOAT ) 
183b6 26 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  && affinity==SQL
183b7 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a  ITE_AFF_NONE ){.
183b8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
183b9 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28  ueApplyAffinity(
183ba 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46  pVal, SQLITE_AFF
183bb 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63 29 3b 0a  _NUMERIC, enc);.
183bc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
183bd 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70   sqlite3ValueApp
183be 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61 6c 2c  lyAffinity(pVal,
183bf 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 29 3b   affinity, enc);
183c0 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
183c1 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53  f( op==TK_UMINUS
183c2 20 29 20 7b 0a 20 20 20 20 69 66 28 20 53 51 4c   ) {.    if( SQL
183c3 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56  ITE_OK==sqlite3V
183c4 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64 62 2c  alueFromExpr(db,
183c5 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63  pExpr->pLeft,enc
183c6 2c 61 66 66 69 6e 69 74 79 2c 26 70 56 61 6c 29  ,affinity,&pVal)
183c7 20 29 7b 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e   ){.      pVal->
183c8 75 2e 69 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d  u.i = -1 * pVal-
183c9 3e 75 2e 69 3b 0a 20 20 20 20 20 20 70 56 61 6c  >u.i;.      pVal
183ca 2d 3e 72 20 3d 20 2d 31 2e 30 20 2a 20 70 56 61  ->r = -1.0 * pVa
183cb 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  l->r;.    }.  }.
183cc 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
183cd 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c  MIT_BLOB_LITERAL
183ce 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d  .  else if( op==
183cf 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 69  TK_BLOB ){.    i
183d0 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 73 73  nt nVal;.    ass
183d1 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65  ert( pExpr->toke
183d2 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 20 61 73  n.n>=3 );.    as
183d3 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b  sert( pExpr->tok
183d4 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20  en.z[0]=='x' || 
183d5 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30  pExpr->token.z[0
183d6 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 61 73  ]=='X' );.    as
183d7 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b  sert( pExpr->tok
183d8 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b  en.z[1]=='\'' );
183d9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
183da 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 78 70  pr->token.z[pExp
183db 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d 27  r->token.n-1]=='
183dc 5c 27 27 20 29 3b 0a 20 20 20 20 70 56 61 6c 20  \'' );.    pVal 
183dd 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65  = sqlite3ValueNe
183de 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 21  w(db);.    if( !
183df 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  pVal ) goto no_m
183e0 65 6d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20 70  em;.    nVal = p
183e1 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 2d 20  Expr->token.n - 
183e2 33 3b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 28 63  3;.    zVal = (c
183e3 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
183e4 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 73 71 6c  n.z + 2;.    sql
183e5 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
183e6 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 65 33 48  r(pVal, sqlite3H
183e7 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 7a 56 61  exToBlob(db, zVa
183e8 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f 32  l, nVal), nVal/2
183e9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
183ea 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 53 51             0, SQ
183eb 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
183ec 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2a 70 70   }.#endif..  *pp
183ed 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20 72 65  Val = pVal;.  re
183ee 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
183ef 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d  .no_mem:.  db->m
183f0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
183f1 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
183f2 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 20 73 71  (db, zVal);.  sq
183f3 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70  lite3ValueFree(p
183f4 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61 6c 20 3d  Val);.  *ppVal =
183f5 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   0;.  return SQL
183f6 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a  ITE_NOMEM;.}../*
183f7 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73  .** Change the s
183f8 74 72 69 6e 67 20 76 61 6c 75 65 20 6f 66 20 61  tring value of a
183f9 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  n sqlite3_value 
183fa 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45  object.*/.SQLITE
183fb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
183fc 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
183fd 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  (.  sqlite3_valu
183fe 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20 56 61 6c  e *v,     /* Val
183ff 75 65 20 74 6f 20 62 65 20 73 65 74 20 2a 2f 0a  ue to be set */.
18400 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20    int n,        
18401 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
18402 68 20 6f 66 20 73 74 72 69 6e 67 20 7a 20 2a 2f  h of string z */
18403 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
18404 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74  ,        /* Text
18405 20 6f 66 20 74 68 65 20 6e 65 77 20 73 74 72 69   of the new stri
18406 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20  ng */.  u8 enc, 
18407 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18408 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65   Encoding to use
18409 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65   */.  void (*xDe
1840a 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f 2a 20 44  l)(void*)   /* D
1840b 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68  estructor for th
1840c 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20  e string */.){. 
1840d 20 69 66 28 20 76 20 29 20 73 71 6c 69 74 65 33   if( v ) sqlite3
1840e 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 28 4d  VdbeMemSetStr((M
1840f 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c 20 65 6e  em *)v, z, n, en
18410 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a  c, xDel);.}../*.
18411 2a 2a 20 46 72 65 65 20 61 6e 20 73 71 6c 69 74  ** Free an sqlit
18412 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a  e3_value object.
18413 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18414 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
18415 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f  lueFree(sqlite3_
18416 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 69 66 28  value *v){.  if(
18417 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20 20   !v ) return;.  
18418 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
18419 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29 76 29 3b  lease((Mem *)v);
1841a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1841b 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 62 2c 20  (((Mem*)v)->db, 
1841c 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  v);.}../*.** Ret
1841d 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1841e 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 73  f bytes in the s
1841f 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
18420 65 63 74 20 61 73 73 75 6d 69 6e 67 0a 2a 2a 20  ect assuming.** 
18421 74 68 61 74 20 69 74 20 75 73 65 73 20 74 68 65  that it uses the
18422 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e 63 22 0a   encoding "enc".
18423 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18424 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c  E int sqlite3Val
18425 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f  ueBytes(sqlite3_
18426 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 75 38 20  value *pVal, u8 
18427 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d  enc){.  Mem *p =
18428 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69   (Mem*)pVal;.  i
18429 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d  f( (p->flags & M
1842a 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c 7c 20 73  EM_Blob)!=0 || s
1842b 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
1842c 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b 0a 20 20  pVal, enc) ){.  
1842d 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26    if( p->flags &
1842e 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
1842f 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 20 2b     return p->n +
18430 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20   p->u.nZero;.   
18431 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
18432 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d  turn p->n;.    }
18433 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
18434 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
18435 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 6d  *** End of vdbem
18436 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  em.c ***********
18437 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18438 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18439 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1843a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76  *** Begin file v
1843b 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  dbeaux.c *******
1843c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1843d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1843e 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53  **/./*.** 2003 S
1843f 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a  eptember 6.**.**
18440 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
18441 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
18442 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
18443 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
18444 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
18445 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
18446 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
18447 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
18448 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
18449 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1844a 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1844b 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1844c 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1844d 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1844e 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1844f 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
18450 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
18451 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18452 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18453 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18454 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18455 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
18456 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
18457 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63 72  code used for cr
18458 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79 69  eating, destroyi
18459 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74 69  ng, and populati
1845a 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f 72  ng.** a VDBE (or
1845b 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d   an "sqlite3_stm
1845c 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f 77  t" as it is know
1845d 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64 65  n to the outside
1845e 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72 0a   world.)  Prior.
1845f 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32 2e  ** to version 2.
18460 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63 6f  8.7, all this co
18461 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64 20  de was combined 
18462 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63 20  into the vdbe.c 
18463 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 20  source file..** 
18464 42 75 74 20 74 68 61 74 20 66 69 6c 65 20 77 61  But that file wa
18465 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62 69  s getting too bi
18466 67 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f 75  g so this subrou
18467 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69 74  tines were split
18468 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a   out..**.** $Id:
18469 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e 34   vdbeaux.c,v 1.4
1846a 33 30 20 32 30 30 39 2f 30 31 2f 30 37 20 30 38  30 2009/01/07 08
1846b 3a 31 32 3a 31 36 20 64 61 6e 69 65 6c 6b 31 39  :12:16 danielk19
1846c 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f  77 Exp $.*/..../
1846d 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 75 67 67  *.** When debugg
1846e 69 6e 67 20 74 68 65 20 63 6f 64 65 20 67 65 6e  ing the code gen
1846f 65 72 61 74 6f 72 20 69 6e 20 61 20 73 79 6d 62  erator in a symb
18470 6f 6c 69 63 20 64 65 62 75 67 67 65 72 2c 20 6f  olic debugger, o
18471 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68  ne can.** set th
18472 65 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  e sqlite3VdbeAdd
18473 6f 70 54 72 61 63 65 20 74 6f 20 31 20 61 6e 64  opTrace to 1 and
18474 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c   all opcodes wil
18475 6c 20 62 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20  l be printed.** 
18476 61 73 20 74 68 65 79 20 61 72 65 20 61 64 64 65  as they are adde
18477 64 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63  d to the instruc
18478 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a  tion stream..*/.
18479 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
1847a 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
1847b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1847c 62 65 41 64 64 6f 70 54 72 61 63 65 20 3d 20 30  beAddopTrace = 0
1847d 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  ;.#endif.../*.**
1847e 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 76 69   Create a new vi
1847f 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
18480 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ngine..*/.SQLITE
18481 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a 73  _PRIVATE Vdbe *s
18482 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65  qlite3VdbeCreate
18483 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
18484 20 56 64 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20   Vdbe *p;.  p = 
18485 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
18486 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 56  ero(db, sizeof(V
18487 64 62 65 29 20 29 3b 0a 20 20 69 66 28 20 70 3d  dbe) );.  if( p=
18488 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
18489 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69   p->db = db;.  i
1848a 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a  f( db->pVdbe ){.
1848b 20 20 20 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70      db->pVdbe->p
1848c 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20  Prev = p;.  }.  
1848d 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70  p->pNext = db->p
1848e 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76  Vdbe;.  p->pPrev
1848f 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62   = 0;.  db->pVdb
18490 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69  e = p;.  p->magi
18491 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  c = VDBE_MAGIC_I
18492 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  NIT;.  return p;
18493 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62  .}../*.** Rememb
18494 65 72 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e  er the SQL strin
18495 67 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 64  g for a prepared
18496 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
18497 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
18498 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  id sqlite3VdbeSe
18499 74 53 71 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f  tSql(Vdbe *p, co
1849a 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
1849b 20 6e 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20   n){.  if( p==0 
1849c 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
1849d 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29  rt( p->zSql==0 )
1849e 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 71  ;.  p->zSql = sq
1849f 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70  lite3DbStrNDup(p
184a0 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 7d 0a 0a  ->db, z, n);.}..
184a1 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
184a2 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65 64 20   SQL associated 
184a3 77 69 74 68 20 61 20 70 72 65 70 61 72 65 64 20  with a prepared 
184a4 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c  statement.*/.SQL
184a5 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
184a6 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28  ar *sqlite3_sql(
184a7 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
184a8 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  tmt){.  return (
184a9 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 2d 3e  (Vdbe *)pStmt)->
184aa 7a 53 71 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  zSql;.}../*.** S
184ab 77 61 70 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20  wap all content 
184ac 62 65 74 77 65 65 6e 20 74 77 6f 20 56 44 42 45  between two VDBE
184ad 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a   structures..*/.
184ae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
184af 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53  oid sqlite3VdbeS
184b0 77 61 70 28 56 64 62 65 20 2a 70 41 2c 20 56 64  wap(Vdbe *pA, Vd
184b1 62 65 20 2a 70 42 29 7b 0a 20 20 56 64 62 65 20  be *pB){.  Vdbe 
184b2 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68  tmp, *pTmp;.  ch
184b3 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 69 6e 74 20  ar *zTmp;.  int 
184b4 6e 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a 70  nTmp;.  tmp = *p
184b5 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b 0a  A;.  *pA = *pB;.
184b6 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20 70    *pB = tmp;.  p
184b7 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74 3b  Tmp = pA->pNext;
184b8 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20 70  .  pA->pNext = p
184b9 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d 3e  B->pNext;.  pB->
184ba 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20 20  pNext = pTmp;.  
184bb 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65 76  pTmp = pA->pPrev
184bc 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d 20  ;.  pA->pPrev = 
184bd 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42 2d  pB->pPrev;.  pB-
184be 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a 20  >pPrev = pTmp;. 
184bf 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 6c   zTmp = pA->zSql
184c0 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 70  ;.  pA->zSql = p
184c1 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e 7a  B->zSql;.  pB->z
184c2 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 6e 54  Sql = zTmp;.  nT
184c3 6d 70 20 3d 20 70 41 2d 3e 6e 53 71 6c 3b 0a 20  mp = pA->nSql;. 
184c4 20 70 41 2d 3e 6e 53 71 6c 20 3d 20 70 42 2d 3e   pA->nSql = pB->
184c5 6e 53 71 6c 3b 0a 20 20 70 42 2d 3e 6e 53 71 6c  nSql;.  pB->nSql
184c6 20 3d 20 6e 54 6d 70 3b 0a 7d 0a 0a 23 69 66 64   = nTmp;.}..#ifd
184c7 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
184c8 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 63 69  /*.** Turn traci
184c9 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a  ng on or off.*/.
184ca 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
184cb 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54  oid sqlite3VdbeT
184cc 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 46 49  race(Vdbe *p, FI
184cd 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d  LE *trace){.  p-
184ce 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 3b 0a  >trace = trace;.
184cf 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
184d0 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e  Resize the Vdbe.
184d1 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 68 61  aOp array so tha
184d2 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 73 74  t it is at least
184d3 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 20 74   one op larger t
184d4 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a  han .** it was..
184d5 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d  **.** If an out-
184d6 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20  of-memory error 
184d7 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73  occurs while res
184d8 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c  izing the array,
184d9 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54   return.** SQLIT
184da 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73  E_NOMEM. In this
184db 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 20 61   case Vdbe.aOp a
184dc 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63  nd Vdbe.nOpAlloc
184dd 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68   remain .** unch
184de 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 20 73  anged (this is s
184df 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 6f 64  o that any opcod
184e0 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  es already alloc
184e1 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20  ated can be .** 
184e2 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f  correctly deallo
184e3 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68  cated along with
184e4 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65   the rest of the
184e5 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69   Vdbe)..*/.stati
184e6 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 72 61  c int growOpArra
184e7 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 56 64  y(Vdbe *p){.  Vd
184e8 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  beOp *pNew;.  in
184e9 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70  t nNew = (p->nOp
184ea 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c  Alloc ? p->nOpAl
184eb 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 31 30  loc*2 : (int)(10
184ec 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b  24/sizeof(Op)));
184ed 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
184ee 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62  3DbRealloc(p->db
184ef 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73  , p->aOp, nNew*s
184f0 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66  izeof(Op));.  if
184f1 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d  ( pNew ){.    p-
184f2 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 6e 4e 65 77  >nOpAlloc = nNew
184f3 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70  ;.    p->aOp = p
184f4 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  New;.  }.  retur
184f5 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45  n (pNew ? SQLITE
184f6 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d  _OK : SQLITE_NOM
184f7 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  EM);.}../*.** Ad
184f8 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74  d a new instruct
184f9 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20  ion to the list 
184fa 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  of instructions 
184fb 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a  current in the.*
184fc 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20  * VDBE.  Return 
184fd 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
184fe 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69  he new instructi
184ff 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65  on..**.** Parame
18500 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70  ters:.**.**    p
18501 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
18502 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44  ointer to the VD
18503 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20  BE.**.**    op  
18504 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
18505 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20  opcode for this 
18506 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a  instruction.**.*
18507 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20  *    p1, p2, p3 
18508 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a       Operands.**
18509 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69  .** Use the sqli
1850a 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
1850b 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  bel() function t
1850c 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73  o fix an address
1850d 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69   and.** the sqli
1850e 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
1850f 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68  ) function to ch
18510 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
18511 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72  f the P4.** oper
18512 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  and..*/.SQLITE_P
18513 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18514 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62  e3VdbeAddOp3(Vdb
18515 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  e *p, int op, in
18516 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e  t p1, int p2, in
18517 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  t p3){.  int i;.
18518 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a    VdbeOp *pOp;..
18519 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20    i = p->nOp;.  
1851a 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
1851b 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
1851c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
1851d 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29  p>0 && op<0xff )
1851e 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 6c  ;.  if( p->nOpAl
1851f 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 69 66  loc<=i ){.    if
18520 28 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29  ( growOpArray(p)
18521 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
18522 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
18523 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20  p->nOp++;.  pOp 
18524 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20  = &p->aOp[i];.  
18525 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 28 75  pOp->opcode = (u
18526 38 29 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20  8)op;.  pOp->p5 
18527 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d  = 0;.  pOp->p1 =
18528 20 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d   p1;.  pOp->p2 =
18529 20 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d   p2;.  pOp->p3 =
1852a 20 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70   p3;.  pOp->p4.p
1852b 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74   = 0;.  pOp->p4t
1852c 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44  ype = P4_NOTUSED
1852d 3b 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d  ;.  p->expired =
1852e 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   0;.#ifdef SQLIT
1852f 45 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a  E_DEBUG.  pOp->z
18530 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69  Comment = 0;.  i
18531 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  f( sqlite3VdbeAd
18532 64 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74  dopTrace ) sqlit
18533 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c  e3VdbePrintOp(0,
18534 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b   i, &p->aOp[i]);
18535 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56  .#endif.#ifdef V
18536 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f  DBE_PROFILE.  pO
18537 70 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20  p->cycles = 0;. 
18538 20 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23   pOp->cnt = 0;.#
18539 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69  endif.  return i
1853a 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
1853b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1853c 62 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70  beAddOp0(Vdbe *p
1853d 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74  , int op){.  ret
1853e 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41  urn sqlite3VdbeA
1853f 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20  ddOp3(p, op, 0, 
18540 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  0, 0);.}.SQLITE_
18541 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18542 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64  te3VdbeAddOp1(Vd
18543 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69  be *p, int op, i
18544 6e 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e  nt p1){.  return
18545 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
18546 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c  p3(p, op, p1, 0,
18547 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   0);.}.SQLITE_PR
18548 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18549 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65  3VdbeAddOp2(Vdbe
1854a 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74   *p, int op, int
1854b 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20   p1, int p2){.  
1854c 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64  return sqlite3Vd
1854d 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20  beAddOp3(p, op, 
1854e 70 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a  p1, p2, 0);.}...
1854f 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63  /*.** Add an opc
18550 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65  ode that include
18551 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61  s the p4 value a
18552 73 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  s a pointer..*/.
18553 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
18554 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
18555 64 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c  dOp4(.  Vdbe *p,
18556 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
18557 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f  dd the opcode to
18558 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e   this VM */.  in
18559 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
1855a 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63    /* The new opc
1855b 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c  ode */.  int p1,
1855c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1855d 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a  The P1 operand *
1855e 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20  /.  int p2,     
1855f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50          /* The P
18560 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  2 operand */.  i
18561 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20  nt p3,          
18562 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65     /* The P3 ope
18563 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rand */.  const 
18564 63 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a  char *zP4,    /*
18565 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20   The P4 operand 
18566 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20  */.  int p4type 
18567 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f           /* P4 o
18568 70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29  perand type */.)
18569 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73  {.  int addr = s
1856a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1856b 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20  (p, op, p1, p2, 
1856c 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  p3);.  sqlite3Vd
1856d 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64  beChangeP4(p, ad
1856e 64 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29  dr, zP4, p4type)
1856f 3b 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b  ;.  return addr;
18570 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
18571 20 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20   a new symbolic 
18572 6c 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73  label for an ins
18573 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61  truction that ha
18574 73 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63  s yet to be.** c
18575 6f 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f  oded.  The symbo
18576 6c 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61  lic label is rea
18577 6c 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74  lly just a negat
18578 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65  ive number.  The
18579 0a 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65  .** label can be
1857a 20 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20   used as the P2 
1857b 76 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72  value of an oper
1857c 61 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77  ation.  Later, w
1857d 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c  hen.** the label
1857e 20 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20   is resolved to 
1857f 61 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65  a specific addre
18580 73 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c  ss, the VDBE wil
18581 6c 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67  l scan.** throug
18582 68 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20  h its operation 
18583 6c 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20  list and change 
18584 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32  all values of P2
18585 20 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20   which match.** 
18586 74 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74  the label into t
18587 68 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72  he resolved addr
18588 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56  ess..**.** The V
18589 44 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61  DBE knows that a
1858a 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c   P2 value is a l
1858b 61 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62  abel because lab
1858c 65 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79  els are.** alway
1858d 73 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50  s negative and P
1858e 32 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70  2 values are sup
1858f 70 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e  pose to be non-n
18590 65 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63  egative..** Henc
18591 65 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32  e, a negative P2
18592 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65   value is a labe
18593 6c 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74  l that has yet t
18594 6f 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a  o be resolved..*
18595 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74  *.** Zero is ret
18596 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f  urned if a mallo
18597 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51  c() fails..*/.SQ
18598 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
18599 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1859a 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a  Label(Vdbe *p){.
1859b 20 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70    int i;.  i = p
1859c 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73  ->nLabel++;.  as
1859d 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
1859e 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
1859f 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e  );.  if( i>=p->n
185a0 4c 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20  LabelAlloc ){.  
185a1 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 4c 61    int n = p->nLa
185a2 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 35 3b 0a  belAlloc*2 + 5;.
185a3 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20      p->aLabel = 
185a4 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
185a5 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d  OrFree(p->db, p-
185a6 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 20  >aLabel,.       
185a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185a9 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62  n*sizeof(p->aLab
185aa 65 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 70 2d 3e  el[0]));.    p->
185ab 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 73 71  nLabelAlloc = sq
185ac 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
185ad 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62  e(p->db, p->aLab
185ae 65 6c 29 2f 73 69 7a 65 6f 66 28 70 2d 3e 61 4c  el)/sizeof(p->aL
185af 61 62 65 6c 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20  abel[0]);.  }.  
185b0 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b  if( p->aLabel ){
185b1 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69  .    p->aLabel[i
185b2 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65  ] = -1;.  }.  re
185b3 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a  turn -1-i;.}../*
185b4 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65  .** Resolve labe
185b5 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68 65 20  l "x" to be the 
185b6 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e  address of the n
185b7 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
185b8 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65  to.** be inserte
185b9 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65  d.  The paramete
185ba 72 20 22 78 22 20 6d 75 73 74 20 68 61 76 65 20  r "x" must have 
185bb 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72  been obtained fr
185bc 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61  om.** a prior ca
185bd 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62  ll to sqlite3Vdb
185be 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f  eMakeLabel()..*/
185bf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
185c0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
185c1 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62  ResolveLabel(Vdb
185c2 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20  e *p, int x){.  
185c3 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20  int j = -1-x;.  
185c4 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
185c5 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
185c6 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a  T );.  assert( j
185c7 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62  >=0 && j<p->nLab
185c8 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  el );.  if( p->a
185c9 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e  Label ){.    p->
185ca 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e  aLabel[j] = p->n
185cb 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  Op;.  }.}../*.**
185cc 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68   Loop through th
185cd 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b 69 6e  e program lookin
185ce 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65 73 20  g for P2 values 
185cf 74 68 61 74 20 61 72 65 20 6e 65 67 61 74 69 76  that are negativ
185d0 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69 6e 73  e.** on jump ins
185d1 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61 63 68  tructions.  Each
185d2 20 73 75 63 68 20 76 61 6c 75 65 20 69 73 20 61   such value is a
185d3 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c 76 65   label.  Resolve
185d4 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 62 79   the.** label by
185d5 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 32 20   setting the P2 
185d6 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63 6f 72  value to its cor
185d7 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61  rect non-zero va
185d8 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  lue..**.** This 
185d9 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
185da 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61 6c 6c  d once after all
185db 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 62 65   opcodes have be
185dc 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a  en inserted..**.
185dd 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70 4d 61  ** Variable *pMa
185de 78 46 75 6e 63 41 72 67 73 20 69 73 20 73 65 74  xFuncArgs is set
185df 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   to the maximum 
185e0 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50 32 20  value of any P2 
185e1 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20  argument .** to 
185e2 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20  an OP_Function, 
185e3 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20 4f 50  OP_AggStep or OP
185e4 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64 65 2e  _VFilter opcode.
185e5 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
185e6 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65   .** sqlite3Vdbe
185e7 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f 20 73  MakeReady() to s
185e8 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 70 41  ize the Vdbe.apA
185e9 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a  rg[] array..**.*
185ea 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
185eb 6c 73 6f 20 64 6f 65 73 20 74 68 65 20 66 6f 6c  lso does the fol
185ec 6c 6f 77 69 6e 67 20 6f 70 74 69 6d 69 7a 61 74  lowing optimizat
185ed 69 6f 6e 3a 20 20 49 74 20 73 63 61 6e 73 20 66  ion:  It scans f
185ee 6f 72 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f  or.** instructio
185ef 6e 73 20 74 68 61 74 20 6d 69 67 68 74 20 63 61  ns that might ca
185f0 75 73 65 20 61 20 73 74 61 74 65 6d 65 6e 74 20  use a statement 
185f1 72 6f 6c 6c 62 61 63 6b 2e 20 20 53 75 63 68 20  rollback.  Such 
185f2 69 6e 73 74 72 75 63 74 69 6f 6e 73 0a 2a 2a 20  instructions.** 
185f3 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20  are:.**.**   *  
185f4 4f 50 5f 48 61 6c 74 20 77 69 74 68 20 50 31 3d  OP_Halt with P1=
185f5 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
185f6 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62 6f 72  T and P2=OE_Abor
185f7 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 44 65  t..**   *  OP_De
185f8 73 74 72 6f 79 0a 2a 2a 20 20 20 2a 20 20 4f 50  stroy.**   *  OP
185f9 5f 56 55 70 64 61 74 65 0a 2a 2a 20 20 20 2a 20  _VUpdate.**   * 
185fa 20 4f 50 5f 56 52 65 6e 61 6d 65 0a 2a 2a 0a 2a   OP_VRename.**.*
185fb 2a 20 49 66 20 6e 6f 20 73 75 63 68 20 69 6e 73  * If no such ins
185fc 74 72 75 63 74 69 6f 6e 20 69 73 20 66 6f 75 6e  truction is foun
185fd 64 2c 20 74 68 65 6e 20 65 76 65 72 79 20 53 74  d, then every St
185fe 61 74 65 6d 65 6e 74 20 69 6e 73 74 72 75 63 74  atement instruct
185ff 69 6f 6e 20 0a 2a 2a 20 69 73 20 63 68 61 6e 67  ion .** is chang
18600 65 64 20 74 6f 20 61 20 4e 6f 6f 70 2e 20 20 49  ed to a Noop.  I
18601 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65 20 61  n this way, we a
18602 76 6f 69 64 20 63 72 65 61 74 69 6e 67 20 74 68  void creating th
18603 65 20 73 74 61 74 65 6d 65 6e 74 20 0a 2a 2a 20  e statement .** 
18604 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75 6e 6e  journal file unn
18605 65 63 65 73 73 61 72 69 6c 79 2e 0a 2a 2f 0a 73  ecessarily..*/.s
18606 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c  tatic void resol
18607 76 65 50 32 56 61 6c 75 65 73 28 56 64 62 65 20  veP2Values(Vdbe 
18608 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 78 46 75 6e  *p, int *pMaxFun
18609 63 41 72 67 73 29 7b 0a 20 20 69 6e 74 20 69 3b  cArgs){.  int i;
1860a 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 73 20  .  int nMaxArgs 
1860b 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a  = 0;.  Op *pOp;.
1860c 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20    int *aLabel = 
1860d 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20 69 6e 74  p->aLabel;.  int
1860e 20 64 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f   doesStatementRo
1860f 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 69 6e  llback = 0;.  in
18610 74 20 68 61 73 53 74 61 74 65 6d 65 6e 74 42 65  t hasStatementBe
18611 67 69 6e 20 3d 20 30 3b 0a 20 20 70 2d 3e 72 65  gin = 0;.  p->re
18612 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 70 2d  adOnly = 1;.  p-
18613 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c  >usesStmtJournal
18614 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 4f 70 3d   = 0;.  for(pOp=
18615 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70  p->aOp, i=p->nOp
18616 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70  -1; i>=0; i--, p
18617 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20 6f 70  Op++){.    u8 op
18618 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f  code = pOp->opco
18619 64 65 3b 0a 0a 20 20 20 20 69 66 28 20 6f 70 63  de;..    if( opc
1861a 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f 6e  ode==OP_Function
1861b 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41   || opcode==OP_A
1861c 67 67 53 74 65 70 20 29 7b 0a 20 20 20 20 20 20  ggStep ){.      
1861d 69 66 28 20 70 4f 70 2d 3e 70 35 3e 6e 4d 61 78  if( pOp->p5>nMax
1861e 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20  Args ) nMaxArgs 
1861f 3d 20 70 4f 70 2d 3e 70 35 3b 0a 23 69 66 6e 64  = pOp->p5;.#ifnd
18620 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
18621 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
18622 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65  }else if( opcode
18623 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 29 7b 0a  ==OP_VUpdate ){.
18624 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70        if( pOp->p
18625 32 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61  2>nMaxArgs ) nMa
18626 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 32 3b  xArgs = pOp->p2;
18627 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20  .#endif.    }.  
18628 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50    if( opcode==OP
18629 5f 48 61 6c 74 20 29 7b 0a 20 20 20 20 20 20 69  _Halt ){.      i
1862a 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c 49  f( pOp->p1==SQLI
1862b 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 26 26  TE_CONSTRAINT &&
1862c 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 62 6f   pOp->p2==OE_Abo
1862d 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f  rt ){.        do
1862e 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62  esStatementRollb
1862f 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  ack = 1;.      }
18630 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  .    }else if( o
18631 70 63 6f 64 65 3d 3d 4f 50 5f 53 74 61 74 65 6d  pcode==OP_Statem
18632 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 68 61 73  ent ){.      has
18633 53 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d  StatementBegin =
18634 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 75 73 65   1;.      p->use
18635 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 31  sStmtJournal = 1
18636 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
18637 6f 70 63 6f 64 65 3d 3d 4f 50 5f 44 65 73 74 72  opcode==OP_Destr
18638 6f 79 20 29 7b 0a 20 20 20 20 20 20 64 6f 65 73  oy ){.      does
18639 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63  StatementRollbac
1863a 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  k = 1;.    }else
1863b 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f   if( opcode==OP_
1863c 54 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 70  Transaction && p
1863d 4f 70 2d 3e 70 32 21 3d 30 20 29 7b 0a 20 20 20  Op->p2!=0 ){.   
1863e 20 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d     p->readOnly =
1863f 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   0;.#ifndef SQLI
18640 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
18641 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 69  ABLE.    }else i
18642 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55  f( opcode==OP_VU
18643 70 64 61 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d  pdate || opcode=
18644 3d 4f 50 5f 56 52 65 6e 61 6d 65 20 29 7b 0a 20  =OP_VRename ){. 
18645 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d 65       doesStateme
18646 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b 0a  ntRollback = 1;.
18647 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70      }else if( op
18648 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72  code==OP_VFilter
18649 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b   ){.      int n;
1864a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1864b 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29  ->nOp - i >= 3 )
1864c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1864d 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d  pOp[-1].opcode==
1864e 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 20  OP_Integer );.  
1864f 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e      n = pOp[-1].
18650 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e  p1;.      if( n>
18651 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41  nMaxArgs ) nMaxA
18652 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a  rgs = n;.#endif.
18653 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 73      }..    if( s
18654 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65  qlite3VdbeOpcode
18655 48 61 73 50 72 6f 70 65 72 74 79 28 6f 70 63 6f  HasProperty(opco
18656 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 20  de, OPFLG_JUMP) 
18657 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a  && pOp->p2<0 ){.
18658 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2d 31        assert( -1
18659 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62  -pOp->p2<p->nLab
1865a 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d  el );.      pOp-
1865b 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d  >p2 = aLabel[-1-
1865c 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a  pOp->p2];.    }.
1865d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
1865e 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c  ree(p->db, p->aL
1865f 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 62  abel);.  p->aLab
18660 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 78  el = 0;..  *pMax
18661 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 41  FuncArgs = nMaxA
18662 72 67 73 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65  rgs;..  /* If we
18663 20 6e 65 76 65 72 20 72 6f 6c 6c 62 61 63 6b 20   never rollback 
18664 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  a statement tran
18665 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 73 74  saction, then st
18666 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 72 61  atement.  ** tra
18667 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f  nsactions are no
18668 74 20 6e 65 65 64 65 64 2e 20 20 53 6f 20 63 68  t needed.  So ch
18669 61 6e 67 65 20 65 76 65 72 79 20 4f 50 5f 53 74  ange every OP_St
1866a 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 6f 70 63  atement.  ** opc
1866b 6f 64 65 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 4e  ode into an OP_N
1866c 6f 6f 70 2e 20 20 54 68 69 73 20 61 76 6f 69 64  oop.  This avoid
1866d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
1866e 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69 76  e3OsOpenExclusiv
1866f 65 28 29 0a 20 20 2a 2a 20 77 68 69 63 68 20 63  e().  ** which c
18670 61 6e 20 62 65 20 65 78 70 65 6e 73 69 76 65 20  an be expensive 
18671 6f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d  on some platform
18672 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 68 61  s..  */.  if( ha
18673 73 53 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20  sStatementBegin 
18674 26 26 20 21 64 6f 65 73 53 74 61 74 65 6d 65 6e  && !doesStatemen
18675 74 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  tRollback ){.   
18676 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72   p->usesStmtJour
18677 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  nal = 0;.    for
18678 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70  (pOp=p->aOp, i=p
18679 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 69  ->nOp-1; i>=0; i
1867a 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20  --, pOp++){.    
1867b 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
1867c 65 3d 3d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20  e==OP_Statement 
1867d 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  ){.        pOp->
1867e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70  opcode = OP_Noop
1867f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
18680 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
18681 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 73 20  urn the address 
18682 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74  of the next inst
18683 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e  ruction to be in
18684 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  serted..*/.SQLIT
18685 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
18686 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
18687 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b 0a 20  Addr(Vdbe *p){. 
18688 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
18689 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
1868a 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  IT );.  return p
1868b 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ->nOp;.}../*.** 
1868c 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69 73 74  Add a whole list
1868d 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 20 74   of operations t
1868e 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  o the operation 
1868f 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e 20 74  stack.  Return t
18690 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20 6f 66  he.** address of
18691 20 74 68 65 20 66 69 72 73 74 20 6f 70 65 72 61   the first opera
18692 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f 0a 53  tion added..*/.S
18693 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18694 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
18695 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70 2c 20  OpList(Vdbe *p, 
18696 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c  int nOp, VdbeOpL
18697 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 29 7b  ist const *aOp){
18698 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 61  .  int addr;.  a
18699 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d  ssert( p->magic=
1869a 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54  =VDBE_MAGIC_INIT
1869b 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70   );.  if( p->nOp
1869c 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f 70 41   + nOp > p->nOpA
1869d 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70 41 72  lloc && growOpAr
1869e 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 72 65  ray(p) ){.    re
1869f 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 64  turn 0;.  }.  ad
186a0 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 69  dr = p->nOp;.  i
186a1 66 28 20 6e 4f 70 3e 30 20 29 7b 0a 20 20 20 20  f( nOp>0 ){.    
186a2 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65 4f  int i;.    VdbeO
186a3 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70 49 6e  pList const *pIn
186a4 20 3d 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28   = aOp;.    for(
186a5 69 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c  i=0; i<nOp; i++,
186a6 20 70 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 69   pIn++){.      i
186a7 6e 74 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32 3b  nt p2 = pIn->p2;
186a8 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70  .      VdbeOp *p
186a9 4f 75 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b  Out = &p->aOp[i+
186aa 61 64 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f 75  addr];.      pOu
186ab 74 2d 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e 2d  t->opcode = pIn-
186ac 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 20 20 70  >opcode;.      p
186ad 4f 75 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e 70  Out->p1 = pIn->p
186ae 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c  1;.      if( p2<
186af 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65  0 && sqlite3Vdbe
186b0 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74  OpcodeHasPropert
186b1 79 28 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 2c 20  y(pOut->opcode, 
186b2 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 29 7b 0a 20  OPFLG_JUMP) ){. 
186b3 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20         pOut->p2 
186b4 3d 20 61 64 64 72 20 2b 20 41 44 44 52 28 70 32  = addr + ADDR(p2
186b5 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
186b6 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32          pOut->p2
186b7 20 3d 20 70 32 3b 0a 20 20 20 20 20 20 7d 0a 20   = p2;.      }. 
186b8 20 20 20 20 20 70 4f 75 74 2d 3e 70 33 20 3d 20       pOut->p3 = 
186b9 70 49 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 20 70  pIn->p3;.      p
186ba 4f 75 74 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  Out->p4type = P4
186bb 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 20 20  _NOTUSED;.      
186bc 70 4f 75 74 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a  pOut->p4.p = 0;.
186bd 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 35 20 3d        pOut->p5 =
186be 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   0;.#ifdef SQLIT
186bf 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f  E_DEBUG.      pO
186c0 75 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30  ut->zComment = 0
186c1 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
186c2 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63  te3VdbeAddopTrac
186c3 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
186c4 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
186c5 30 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e 61  0, i+addr, &p->a
186c6 4f 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20 20  Op[i+addr]);.   
186c7 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
186c8 7d 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20  }.    p->nOp += 
186c9 6e 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  nOp;.  }.  retur
186ca 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n addr;.}../*.**
186cb 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   Change the valu
186cc 65 20 6f 66 20 74 68 65 20 50 31 20 6f 70 65 72  e of the P1 oper
186cd 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66  and for a specif
186ce 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ic instruction..
186cf 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
186d0 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61  is useful when a
186d1 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69   large program i
186d2 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a  s loaded from a.
186d3 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20  ** static array 
186d4 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62  using sqlite3Vdb
186d5 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77  eAddOpList but w
186d6 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61  e want to make a
186d7 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68  .** few minor ch
186d8 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f  anges to the pro
186d9 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gram..*/.SQLITE_
186da 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
186db 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
186dc 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64  (Vdbe *p, int ad
186dd 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20  dr, int val){.  
186de 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20  assert( p==0 || 
186df 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
186e0 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
186e1 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20  f( p && addr>=0 
186e2 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26  && p->nOp>addr &
186e3 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20  & p->aOp ){.    
186e4 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 31 20  p->aOp[addr].p1 
186e5 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  = val;.  }.}../*
186e6 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76  .** Change the v
186e7 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 20 6f  alue of the P2 o
186e8 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65  perand for a spe
186e9 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f  cific instructio
186ea 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  n..** This routi
186eb 6e 65 20 69 73 20 75 73 65 66 75 6c 20 66 6f 72  ne is useful for
186ec 20 73 65 74 74 69 6e 67 20 61 20 6a 75 6d 70 20   setting a jump 
186ed 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a  destination..*/.
186ee 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
186ef 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
186f0 68 61 6e 67 65 50 32 28 56 64 62 65 20 2a 70 2c  hangeP2(Vdbe *p,
186f1 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 76   int addr, int v
186f2 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  al){.  assert( p
186f3 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d  ==0 || p->magic=
186f4 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54  =VDBE_MAGIC_INIT
186f5 20 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 61   );.  if( p && a
186f6 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e 6e 4f 70  ddr>=0 && p->nOp
186f7 3e 61 64 64 72 20 26 26 20 70 2d 3e 61 4f 70 20  >addr && p->aOp 
186f8 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64  ){.    p->aOp[ad
186f9 64 72 5d 2e 70 32 20 3d 20 76 61 6c 3b 0a 20 20  dr].p2 = val;.  
186fa 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  }.}../*.** Chang
186fb 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
186fc 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 66 6f  he P3 operand fo
186fd 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73  r a specific ins
186fe 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  truction..*/.SQL
186ff 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
18700 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
18701 67 65 50 33 28 56 64 62 65 20 2a 70 2c 20 69 6e  geP3(Vdbe *p, in
18702 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29  t addr, int val)
18703 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30  {.  assert( p==0
18704 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44   || p->magic==VD
18705 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b  BE_MAGIC_INIT );
18706 0a 20 20 69 66 28 20 70 20 26 26 20 61 64 64 72  .  if( p && addr
18707 3e 3d 30 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64  >=0 && p->nOp>ad
18708 64 72 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a  dr && p->aOp ){.
18709 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d      p->aOp[addr]
1870a 2e 70 33 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d  .p3 = val;.  }.}
1870b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
1870c 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1870d 50 35 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 74  P5 operand for t
1870e 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
1870f 0a 2a 2a 20 61 64 64 65 64 20 6f 70 65 72 61 74  .** added operat
18710 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
18711 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
18712 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
18713 56 64 62 65 20 2a 70 2c 20 75 38 20 76 61 6c 29  Vdbe *p, u8 val)
18714 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30  {.  assert( p==0
18715 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44   || p->magic==VD
18716 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b  BE_MAGIC_INIT );
18717 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 61  .  if( p && p->a
18718 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  Op ){.    assert
18719 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20  ( p->nOp>0 );.  
1871a 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d    p->aOp[p->nOp-
1871b 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20 20 7d  1].p5 = val;.  }
1871c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
1871d 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20   the P2 operand 
1871e 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61  of instruction a
1871f 64 64 72 20 73 6f 20 74 68 61 74 20 69 74 20 70  ddr so that it p
18720 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20  oints to.** the 
18721 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e  address of the n
18722 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
18723 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a 2f 0a  to be coded..*/.
18724 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
18725 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4a  oid sqlite3VdbeJ
18726 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a 70 2c  umpHere(Vdbe *p,
18727 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73 71   int addr){.  sq
18728 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
18729 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e 6e 4f  2(p, addr, p->nO
1872a 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66  p);.}.../*.** If
1872b 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e 63 44   the input FuncD
1872c 65 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ef structure is 
1872d 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65 6e 20  ephemeral, then 
1872e 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a 2a 20  free it.  If.** 
1872f 74 68 65 20 46 75 6e 63 44 65 66 20 69 73 20 6e  the FuncDef is n
18730 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74 68 65  ot ephermal, the
18731 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2f  n do nothing..*/
18732 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
18733 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69  eEphemeralFuncti
18734 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  on(sqlite3 *db, 
18735 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29 7b 0a  FuncDef *pDef){.
18736 20 20 69 66 28 20 70 44 65 66 20 26 26 20 28 70    if( pDef && (p
18737 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  Def->flags & SQL
18738 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 29 21  ITE_FUNC_EPHEM)!
18739 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1873a 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 66  3DbFree(db, pDef
1873b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1873c 44 65 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75  Delete a P4 valu
1873d 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a  e if necessary..
1873e 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
1873f 72 65 65 50 34 28 73 71 6c 69 74 65 33 20 2a 64  reeP4(sqlite3 *d
18740 62 2c 20 69 6e 74 20 70 34 74 79 70 65 2c 20 76  b, int p4type, v
18741 6f 69 64 20 2a 70 34 29 7b 0a 20 20 69 66 28 20  oid *p4){.  if( 
18742 70 34 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68  p4 ){.    switch
18743 28 20 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20  ( p4type ){.    
18744 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 0a    case P4_REAL:.
18745 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e        case P4_IN
18746 54 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20  T64:.      case 
18747 50 34 5f 4d 50 52 49 4e 54 46 3a 0a 20 20 20 20  P4_MPRINTF:.    
18748 20 20 63 61 73 65 20 50 34 5f 44 59 4e 41 4d 49    case P4_DYNAMI
18749 43 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34  C:.      case P4
1874a 5f 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20 20 20  _KEYINFO:.      
1874b 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59  case P4_INTARRAY
1874c 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  :.      case P4_
1874d 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a  KEYINFO_HANDOFF:
1874e 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
1874f 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 34 29  e3DbFree(db, p4)
18750 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
18751 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
18752 61 73 65 20 50 34 5f 56 44 42 45 46 55 4e 43 3a  ase P4_VDBEFUNC:
18753 20 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65 46   {.        VdbeF
18754 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d  unc *pVdbeFunc =
18755 20 28 56 64 62 65 46 75 6e 63 20 2a 29 70 34 3b   (VdbeFunc *)p4;
18756 0a 20 20 20 20 20 20 20 20 66 72 65 65 45 70 68  .        freeEph
18757 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64  emeralFunction(d
18758 62 2c 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46  b, pVdbeFunc->pF
18759 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 73 71  unc);.        sq
1875a 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41  lite3VdbeDeleteA
1875b 75 78 44 61 74 61 28 70 56 64 62 65 46 75 6e 63  uxData(pVdbeFunc
1875c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
1875d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1875e 70 56 64 62 65 46 75 6e 63 29 3b 0a 20 20 20 20  pVdbeFunc);.    
1875f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
18760 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34   }.      case P4
18761 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20  _FUNCDEF: {.    
18762 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61      freeEphemera
18763 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 46  lFunction(db, (F
18764 75 6e 63 44 65 66 2a 29 70 34 29 3b 0a 20 20 20  uncDef*)p4);.   
18765 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
18766 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50    }.      case P
18767 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20  4_MEM: {.       
18768 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
18769 65 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e((sqlite3_value
1876a 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62  *)p4);.        b
1876b 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1876c 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a    }.  }.}.../*.*
1876d 2a 20 43 68 61 6e 67 65 20 4e 20 6f 70 63 6f 64  * Change N opcod
1876e 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 61  es starting at a
1876f 64 64 72 20 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a  ddr to No-ops..*
18770 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18771 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
18772 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64  eChangeToNoop(Vd
18773 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c  be *p, int addr,
18774 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70   int N){.  if( p
18775 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20   && p->aOp ){.  
18776 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20    VdbeOp *pOp = 
18777 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20  &p->aOp[addr];. 
18778 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
18779 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68 69 6c   p->db;.    whil
1877a 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 20  e( N-- ){.      
1877b 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e  freeP4(db, pOp->
1877c 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e  p4type, pOp->p4.
1877d 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  p);.      memset
1877e 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28  (pOp, 0, sizeof(
1877f 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20  pOp[0]));.      
18780 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50  pOp->opcode = OP
18781 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70 4f 70  _Noop;.      pOp
18782 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ++;.    }.  }.}.
18783 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
18784 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
18785 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20  4 operand for a 
18786 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63  specific instruc
18787 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  tion..** This ro
18788 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c 20  utine is useful 
18789 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 72 6f  when a large pro
1878a 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 20 66  gram is loaded f
1878b 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 63 20  rom a.** static 
1878c 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 6c 69  array using sqli
1878d 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
1878e 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20   but we want to 
1878f 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 6d 69  make a.** few mi
18790 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74  nor changes to t
18791 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a  he program..**.*
18792 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e 20 74  * If n>=0 then t
18793 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 69 73  he P4 operand is
18794 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e 69 6e   dynamic, meanin
18795 67 20 74 68 61 74 20 61 20 63 6f 70 79 20 6f 66  g that a copy of
18796 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69  .** the string i
18797 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65 6d 6f  s made into memo
18798 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
18799 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
1879a 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66  )..** A value of
1879b 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f 70 79   n==0 means copy
1879c 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20 75 70   bytes of zP4 up
1879d 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 69 6e   to and includin
1879e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6e  g the.** first n
1879f 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20 6e 3e  ull byte.  If n>
187a0 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b 31 20  0 then copy n+1 
187a1 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a 2a 2a  bytes of zP4..**
187a2 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b 45 59  .** If n==P4_KEY
187a3 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20 74 68  INFO it means th
187a4 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f 69 6e  at zP4 is a poin
187a5 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f  ter to a KeyInfo
187a6 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 41   structure..** A
187a7 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 6f 66   copy is made of
187a8 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72   the KeyInfo str
187a9 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65 6d 6f  ucture into memo
187aa 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
187ab 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  .** sqlite3_mall
187ac 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65 64 20  oc, to be freed 
187ad 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73  when the Vdbe is
187ae 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6e   finalized..** n
187af 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  ==P4_KEYINFO_HAN
187b0 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73 20 74  DOFF indicates t
187b1 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 20 74  hat zP4 points t
187b2 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  o a KeyInfo stru
187b3 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65 64 20  cture.** stored 
187b4 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 74  in memory that t
187b5 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62  he caller has ob
187b6 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
187b7 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68 65 20  te3_malloc. The 
187b8 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c  .** caller shoul
187b9 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65 20 61  d not free the a
187ba 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 77 69  llocation, it wi
187bb 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68 65 6e  ll be freed when
187bc 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a 2a 20   the Vdbe is.** 
187bd 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 0a 2a  finalized..** .*
187be 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20 6f  * Other values o
187bf 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43 2c 20  f n (P4_STATIC, 
187c0 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63 2e 29  P4_COLLSEQ etc.)
187c1 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 7a   indicate that z
187c2 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20  P4 points.** to 
187c3 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74 72 75  a string or stru
187c4 63 74 75 72 65 20 74 68 61 74 20 69 73 20 67 75  cture that is gu
187c5 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73  aranteed to exis
187c6 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74 69  t for the lifeti
187c7 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56 64 62  me of.** the Vdb
187c8 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61 73 65  e. In these case
187c9 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20 63 6f  s we can just co
187ca 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72 2e 0a  py the pointer..
187cb 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c 30 20  **.** If addr<0 
187cc 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34 20 6f  then change P4 o
187cd 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  n the most recen
187ce 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69 6e 73  tly inserted ins
187cf 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  truction..*/.SQL
187d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
187d1 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
187d2 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20 69 6e  geP4(Vdbe *p, in
187d3 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 68  t addr, const ch
187d4 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e 29 7b  ar *zP4, int n){
187d5 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 73 71  .  Op *pOp;.  sq
187d6 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61 73 73  lite3 *db;.  ass
187d7 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 64  ert( p!=0 );.  d
187d8 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73  b = p->db;.  ass
187d9 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
187da 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
187db 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 3d 3d  ;.  if( p->aOp==
187dc 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
187dd 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 66 20  ailed ){.    if 
187de 28 6e 20 21 3d 20 50 34 5f 4b 45 59 49 4e 46 4f  (n != P4_KEYINFO
187df 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34  ) {.      freeP4
187e0 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 2a  (db, n, (void*)*
187e1 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a 20  (char**)&zP4);. 
187e2 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 3b     }.    return;
187e3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61  .  }.  assert( a
187e4 64 64 72 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20  ddr<p->nOp );.  
187e5 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 20 20  if( addr<0 ){.  
187e6 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20    addr = p->nOp 
187e7 2d 20 31 3b 0a 20 20 20 20 69 66 28 20 61 64 64  - 1;.    if( add
187e8 72 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  r<0 ) return;.  
187e9 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f  }.  pOp = &p->aO
187ea 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65 65 50  p[addr];.  freeP
187eb 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70  4(db, pOp->p4typ
187ec 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20  e, pOp->p4.p);. 
187ed 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a   pOp->p4.p = 0;.
187ee 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33    if( n==P4_INT3
187ef 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65  2 ){.    /* Note
187f0 3a 20 74 68 69 73 20 63 61 73 74 20 69 73 20 73  : this cast is s
187f1 61 66 65 2c 20 62 65 63 61 75 73 65 20 74 68 65  afe, because the
187f2 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70 6f 69   origin data poi
187f3 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a 20 20  nt was an int.  
187f4 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20 63 61    ** that was ca
187f5 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74 20 63  st to a (const c
187f6 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70  har *). */.    p
187f7 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54  Op->p4.i = SQLIT
187f8 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34  E_PTR_TO_INT(zP4
187f9 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  );.    pOp->p4ty
187fa 70 65 20 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20  pe = P4_INT32;. 
187fb 20 7d 65 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d   }else if( zP4==
187fc 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  0 ){.    pOp->p4
187fd 2e 70 20 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d  .p = 0;.    pOp-
187fe 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54  >p4type = P4_NOT
187ff 55 53 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66  USED;.  }else if
18800 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20  ( n==P4_KEYINFO 
18801 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  ){.    KeyInfo *
18802 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e  pKeyInfo;.    in
18803 74 20 6e 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b  t nField, nByte;
18804 0a 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 28  ..    nField = (
18805 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e  (KeyInfo*)zP4)->
18806 6e 46 69 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74  nField;.    nByt
18807 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79  e = sizeof(*pKey
18808 49 6e 66 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d  Info) + (nField-
18809 31 29 2a 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e  1)*sizeof(pKeyIn
1880a 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20  fo->aColl[0]) + 
1880b 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79  nField;.    pKey
1880c 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61  Info = sqlite3Ma
1880d 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20  lloc( nByte );. 
1880e 20 20 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49     pOp->p4.pKeyI
1880f 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a  nfo = pKeyInfo;.
18810 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f      if( pKeyInfo
18811 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 61 53   ){.      u8 *aS
18812 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20  ortOrder;.      
18813 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c  memcpy(pKeyInfo,
18814 20 7a 50 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20   zP4, nByte);.  
18815 20 20 20 20 61 53 6f 72 74 4f 72 64 65 72 20 3d      aSortOrder =
18816 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
18817 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 69 66 28  Order;.      if(
18818 20 61 53 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20   aSortOrder ){. 
18819 20 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d         pKeyInfo-
1881a 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  >aSortOrder = (u
1881b 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 70  nsigned char*)&p
1881c 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e  KeyInfo->aColl[n
1881d 46 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20  Field];.        
1881e 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d  memcpy(pKeyInfo-
1881f 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f  >aSortOrder, aSo
18820 72 74 4f 72 64 65 72 2c 20 6e 46 69 65 6c 64 29  rtOrder, nField)
18821 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
18822 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
18823 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65  _KEYINFO;.    }e
18824 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62  lse{.      p->db
18825 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
18826 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70   1;.      pOp->p
18827 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53  4type = P4_NOTUS
18828 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ED;.    }.  }els
18829 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49  e if( n==P4_KEYI
1882a 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20  NFO_HANDOFF ){. 
1882b 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28     pOp->p4.p = (
1882c 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70  void*)zP4;.    p
1882d 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f  Op->p4type = P4_
1882e 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65  KEYINFO;.  }else
1882f 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20   if( n<0 ){.    
18830 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69  pOp->p4.p = (voi
18831 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d  d*)zP4;.    pOp-
18832 3e 70 34 74 79 70 65 20 3d 20 28 73 69 67 6e 65  >p4type = (signe
18833 64 20 63 68 61 72 29 6e 3b 0a 20 20 7d 65 6c 73  d char)n;.  }els
18834 65 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20  e{.    if( n==0 
18835 29 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ) n = sqlite3Str
18836 6c 65 6e 33 30 28 7a 50 34 29 3b 0a 20 20 20 20  len30(zP4);.    
18837 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69  pOp->p4.z = sqli
18838 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e  te3DbStrNDup(p->
18839 64 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20  db, zP4, n);.   
1883a 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
1883b 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d  4_DYNAMIC;.  }.}
1883c 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
1883d 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
1883e 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65  e comment on the
1883f 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
18840 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63  ly coded instruc
18841 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73  tion.  Or.** ins
18842 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20  ert a No-op and 
18843 61 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20  add the comment 
18844 74 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74  to that new inst
18845 72 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a  ruction.  This.*
18846 2a 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65  * makes the code
18847 20 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20   easier to read 
18848 64 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67  during debugging
18849 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20  .  None of this 
1884a 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20  happens.** in a 
1884b 70 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64  production build
1884c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1884d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1884e 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65  VdbeComment(Vdbe
1884f 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
18850 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
18851 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
18852 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30  assert( p->nOp>0
18853 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b   || p->aOp==0 );
18854 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f  .  assert( p->aO
18855 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70  p==0 || p->aOp[p
18856 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e  ->nOp-1].zCommen
18857 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d  t==0 || p->db->m
18858 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
18859 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20   if( p->nOp ){. 
1885a 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26     char **pz = &
1885b 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d  p->aOp[p->nOp-1]
1885c 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76  .zComment;.    v
1885d 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
1885e 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  mat);.    sqlite
1885f 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a  3DbFree(p->db, *
18860 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73  pz);.    *pz = s
18861 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70  qlite3VMPrintf(p
18862 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  ->db, zFormat, a
18863 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61  p);.    va_end(a
18864 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45  p);.  }.}.SQLITE
18865 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
18866 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d  lite3VdbeNoopCom
18867 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f  ment(Vdbe *p, co
18868 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
18869 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  t, ...){.  va_li
1886a 73 74 20 61 70 3b 0a 20 20 73 71 6c 69 74 65 33  st ap;.  sqlite3
1886b 56 64 62 65 41 64 64 4f 70 30 28 70 2c 20 4f 50  VdbeAddOp0(p, OP
1886c 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65 72 74  _Noop);.  assert
1886d 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d  ( p->nOp>0 || p-
1886e 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73  >aOp==0 );.  ass
1886f 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c  ert( p->aOp==0 |
18870 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d  | p->aOp[p->nOp-
18871 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c  1].zComment==0 |
18872 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  | p->db->mallocF
18873 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70  ailed );.  if( p
18874 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61  ->nOp ){.    cha
18875 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70  r **pz = &p->aOp
18876 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d  [p->nOp-1].zComm
18877 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72  ent;.    va_star
18878 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
18879 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1887a 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20  e(p->db, *pz);. 
1887b 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33     *pz = sqlite3
1887c 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20  VMPrintf(p->db, 
1887d 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
1887e 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
1887f 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e  }.}.#endif  /* N
18880 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  DEBUG */../*.** 
18881 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63 6f 64  Return the opcod
18882 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20 61 64  e for a given ad
18883 64 72 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  dress..*/.SQLITE
18884 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20  _PRIVATE VdbeOp 
18885 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f  *sqlite3VdbeGetO
18886 70 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  p(Vdbe *p, int a
18887 64 64 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ddr){.  assert( 
18888 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
18889 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61  AGIC_INIT );.  a
1888a 73 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20  ssert( (addr>=0 
1888b 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20  && addr<p->nOp) 
1888c 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  || p->db->malloc
1888d 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75  Failed );.  retu
1888e 72 6e 20 28 28 61 64 64 72 3e 3d 30 20 26 26 20  rn ((addr>=0 && 
1888f 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 3f 28 26 70  addr<p->nOp)?(&p
18890 2d 3e 61 4f 70 5b 61 64 64 72 5d 29 3a 30 29 3b  ->aOp[addr]):0);
18891 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .}..#if !defined
18892 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50  (SQLITE_OMIT_EXP
18893 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65  LAIN) || !define
18894 64 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20 20  d(NDEBUG) \.    
18895 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45   || defined(VDBE
18896 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66  _PROFILE) || def
18897 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
18898 47 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  G)./*.** Compute
18899 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 64   a string that d
1889a 65 73 63 72 69 62 65 73 20 74 68 65 20 50 34 20  escribes the P4 
1889b 70 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6e  parameter for an
1889c 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20   opcode..** Use 
1889d 7a 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 65  zTemp for any re
1889e 71 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72 79  quired temporary
1889f 20 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a 2a   buffer space..*
188a0 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64  /.static char *d
188a1 69 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70  isplayP4(Op *pOp
188a2 2c 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69  , char *zTemp, i
188a3 6e 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61  nt nTemp){.  cha
188a4 72 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a  r *zP4 = zTemp;.
188a5 20 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 3e    assert( nTemp>
188a6 3d 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68 28  =20 );.  switch(
188a7 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a   pOp->p4type ){.
188a8 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49      case P4_KEYI
188a9 4e 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20 20  NFO_STATIC:.    
188aa 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a  case P4_KEYINFO:
188ab 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20   {.      int i, 
188ac 6a 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f  j;.      KeyInfo
188ad 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70   *pKeyInfo = pOp
188ae 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20  ->p4.pKeyInfo;. 
188af 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
188b0 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
188b1 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 22  mp, "keyinfo(%d"
188b2 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  , pKeyInfo->nFie
188b3 6c 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 73  ld);.      i = s
188b4 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
188b5 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f 72  Temp);.      for
188b6 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f  (j=0; j<pKeyInfo
188b7 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a  ->nField; j++){.
188b8 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20          CollSeq 
188b9 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66  *pColl = pKeyInf
188ba 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20  o->aColl[j];.   
188bb 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29       if( pColl )
188bc 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
188bd 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
188be 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  n30(pColl->zName
188bf 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
188c0 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a   i+n>nTemp-6 ){.
188c1 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63              memc
188c2 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e  py(&zTemp[i],",.
188c3 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20  ..",4);.        
188c4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
188c5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
188c6 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c   zTemp[i++] = ',
188c7 27 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ';.          if(
188c8 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
188c9 4f 72 64 65 72 20 26 26 20 70 4b 65 79 49 6e 66  Order && pKeyInf
188ca 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d  o->aSortOrder[j]
188cb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
188cc 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27  zTemp[i++] = '-'
188cd 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
188ce 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
188cf 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d  zTemp[i], pColl-
188d0 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20  >zName,n+1);.   
188d1 20 20 20 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20         i += n;. 
188d2 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
188d3 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a   i+4<nTemp-6 ){.
188d4 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
188d5 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c  (&zTemp[i],",nil
188d6 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ",4);.          
188d7 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 20  i += 4;.        
188d8 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
188d9 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27  zTemp[i++] = ')'
188da 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d  ;.      zTemp[i]
188db 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65   = 0;.      asse
188dc 72 74 28 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20  rt( i<nTemp );. 
188dd 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
188de 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 43 4f  }.    case P4_CO
188df 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20 20 20 43  LLSEQ: {.      C
188e0 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
188e1 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20  pOp->p4.pColl;. 
188e2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
188e3 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
188e4 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32  mp, "collseq(%.2
188e5 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61  0s)", pColl->zNa
188e6 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  me);.      break
188e7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
188e8 20 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20   P4_FUNCDEF: {. 
188e9 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44       FuncDef *pD
188ea 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75  ef = pOp->p4.pFu
188eb 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  nc;.      sqlite
188ec 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
188ed 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28 25 64 29  , zTemp, "%s(%d)
188ee 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20  ", pDef->zName, 
188ef 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20  pDef->nArg);.   
188f0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
188f1 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36      case P4_INT6
188f2 34 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  4: {.      sqlit
188f3 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
188f4 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22  p, zTemp, "%lld"
188f5 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29  , *pOp->p4.pI64)
188f6 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
188f7 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34     }.    case P4
188f8 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20 20 20 20  _INT32: {.      
188f9 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
188fa 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
188fb 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b  %d", pOp->p4.i);
188fc 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
188fd 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f    }.    case P4_
188fe 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71  REAL: {.      sq
188ff 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
18900 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e  Temp, zTemp, "%.
18901 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70  16g", *pOp->p4.p
18902 52 65 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Real);.      bre
18903 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
18904 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20  se P4_MEM: {.   
18905 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70     Mem *pMem = p
18906 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20  Op->p4.pMem;.   
18907 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d     assert( (pMem
18908 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
18909 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ll)==0 );.      
1890a 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
1890b 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20  & MEM_Str ){.   
1890c 20 20 20 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d       zP4 = pMem-
1890d 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  >z;.      }else 
1890e 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
1890f 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
18910 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
18911 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
18912 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d  mp, "%lld", pMem
18913 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65  ->u.i);.      }e
18914 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c  lse if( pMem->fl
18915 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29  ags & MEM_Real )
18916 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
18917 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
18918 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22  , zTemp, "%.16g"
18919 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20  , pMem->r);.    
1891a 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1891b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
1891c 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1891d 41 4c 54 41 42 4c 45 0a 20 20 20 20 63 61 73 65  ALTABLE.    case
1891e 20 50 34 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20   P4_VTAB: {.    
1891f 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
18920 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e  pVtab = pOp->p4.
18921 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c  pVtab;.      sql
18922 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
18923 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 76 74 61  emp, zTemp, "vta
18924 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 61 62 2c  b:%p:%p", pVtab,
18925 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 29   pVtab->pModule)
18926 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
18927 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
18928 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59  case P4_INTARRAY
18929 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
1892a 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
1892b 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 61 72 72  , zTemp, "intarr
1892c 61 79 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ay");.      brea
1892d 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
1892e 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50  ault: {.      zP
1892f 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  4 = pOp->p4.z;. 
18930 20 20 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 20       if( zP4==0 
18931 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d  ){.        zP4 =
18932 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20   zTemp;.        
18933 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20  zTemp[0] = 0;.  
18934 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
18935 20 20 61 73 73 65 72 74 28 20 7a 50 34 21 3d 30    assert( zP4!=0
18936 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 34   );.  return zP4
18937 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
18938 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65  * Declare to the
18939 20 56 64 62 65 20 74 68 61 74 20 74 68 65 20 42   Vdbe that the B
1893a 54 72 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64  Tree object at d
1893b 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65  b->aDb[i] is use
1893c 64 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  d..**.*/.SQLITE_
1893d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1893e 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
1893f 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69  e(Vdbe *p, int i
18940 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20  ){.  int mask;. 
18941 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
18942 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b   i<p->db->nDb );
18943 0a 20 20 61 73 73 65 72 74 28 20 69 3c 28 69 6e  .  assert( i<(in
18944 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65  t)sizeof(p->btre
18945 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61  eMask)*8 );.  ma
18946 73 6b 20 3d 20 31 3c 3c 69 3b 0a 20 20 69 66 28  sk = 1<<i;.  if(
18947 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
18948 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20   mask)==0 ){.   
18949 20 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 7c 3d   p->btreeMask |=
1894a 20 6d 61 73 6b 3b 0a 20 20 20 20 73 71 6c 69 74   mask;.    sqlit
1894b 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
1894c 79 49 6e 73 65 72 74 28 26 70 2d 3e 61 4d 75 74  yInsert(&p->aMut
1894d 65 78 2c 20 70 2d 3e 64 62 2d 3e 61 44 62 5b 69  ex, p->db->aDb[i
1894e 5d 2e 70 42 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a  ].pBt);.  }.}...
1894f 23 69 66 20 64 65 66 69 6e 65 64 28 56 44 42 45  #if defined(VDBE
18950 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66  _PROFILE) || def
18951 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
18952 47 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61  G)./*.** Print a
18953 20 73 69 6e 67 6c 65 20 6f 70 63 6f 64 65 2e 20   single opcode. 
18954 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
18955 20 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67   used for debugg
18956 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c  ing only..*/.SQL
18957 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
18958 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
18959 74 4f 70 28 46 49 4c 45 20 2a 70 4f 75 74 2c 20  tOp(FILE *pOut, 
1895a 69 6e 74 20 70 63 2c 20 4f 70 20 2a 70 4f 70 29  int pc, Op *pOp)
1895b 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 3b 0a 20  {.  char *zP4;. 
1895c 20 63 68 61 72 20 7a 50 74 72 5b 35 30 5d 3b 0a   char zPtr[50];.
1895d 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
1895e 68 61 72 20 2a 7a 46 6f 72 6d 61 74 31 20 3d 20  har *zFormat1 = 
1895f 22 25 34 64 20 25 2d 31 33 73 20 25 34 64 20 25  "%4d %-13s %4d %
18960 34 64 20 25 34 64 20 25 2d 34 73 20 25 2e 32 58  4d %4d %-4s %.2X
18961 20 25 73 5c 6e 22 3b 0a 20 20 69 66 28 20 70 4f   %s\n";.  if( pO
18962 75 74 3d 3d 30 20 29 20 70 4f 75 74 20 3d 20 73  ut==0 ) pOut = s
18963 74 64 6f 75 74 3b 0a 20 20 7a 50 34 20 3d 20 64  tdout;.  zP4 = d
18964 69 73 70 6c 61 79 50 34 28 70 4f 70 2c 20 7a 50  isplayP4(pOp, zP
18965 74 72 2c 20 73 69 7a 65 6f 66 28 7a 50 74 72 29  tr, sizeof(zPtr)
18966 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 70 4f 75  );.  fprintf(pOu
18967 74 2c 20 7a 46 6f 72 6d 61 74 31 2c 20 70 63 2c  t, zFormat1, pc,
18968 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f   .      sqlite3O
18969 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f  pcodeName(pOp->o
1896a 70 63 6f 64 65 29 2c 20 70 4f 70 2d 3e 70 31 2c  pcode), pOp->p1,
1896b 20 70 4f 70 2d 3e 70 32 2c 20 70 4f 70 2d 3e 70   pOp->p2, pOp->p
1896c 33 2c 20 7a 50 34 2c 20 70 4f 70 2d 3e 70 35 2c  3, zP4, pOp->p5,
1896d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1896e 45 42 55 47 0a 20 20 20 20 20 20 70 4f 70 2d 3e  EBUG.      pOp->
1896f 7a 43 6f 6d 6d 65 6e 74 20 3f 20 70 4f 70 2d 3e  zComment ? pOp->
18970 7a 43 6f 6d 6d 65 6e 74 20 3a 20 22 22 0a 23 65  zComment : "".#e
18971 6c 73 65 0a 20 20 20 20 20 20 22 22 0a 23 65 6e  lse.      "".#en
18972 64 69 66 0a 20 20 29 3b 0a 20 20 66 66 6c 75 73  dif.  );.  fflus
18973 68 28 70 4f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69  h(pOut);.}.#endi
18974 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  f../*.** Release
18975 20 61 6e 20 61 72 72 61 79 20 6f 66 20 4e 20 4d   an array of N M
18976 65 6d 20 65 6c 65 6d 65 6e 74 73 0a 2a 2f 0a 73  em elements.*/.s
18977 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61  tatic void relea
18978 73 65 4d 65 6d 41 72 72 61 79 28 4d 65 6d 20 2a  seMemArray(Mem *
18979 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28  p, int N){.  if(
1897a 20 70 20 26 26 20 4e 20 29 7b 0a 20 20 20 20 4d   p && N ){.    M
1897b 65 6d 20 2a 70 45 6e 64 3b 0a 20 20 20 20 73 71  em *pEnd;.    sq
1897c 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
1897d 62 3b 0a 20 20 20 20 75 38 20 6d 61 6c 6c 6f 63  b;.    u8 malloc
1897e 5f 66 61 69 6c 65 64 20 3d 20 64 62 2d 3e 6d 61  _failed = db->ma
1897f 6c 6c 6f 63 46 61 69 6c 65 64 3b 0a 20 20 20 20  llocFailed;.    
18980 66 6f 72 28 70 45 6e 64 3d 26 70 5b 4e 5d 3b 20  for(pEnd=&p[N]; 
18981 70 3c 70 45 6e 64 3b 20 70 2b 2b 29 7b 0a 20 20  p<pEnd; p++){.  
18982 20 20 20 20 61 73 73 65 72 74 28 20 28 26 70 5b      assert( (&p[
18983 31 5d 29 3d 3d 70 45 6e 64 20 7c 7c 20 70 5b 30  1])==pEnd || p[0
18984 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64 62 20 29 3b  ].db==p[1].db );
18985 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  ..      /* This 
18986 62 6c 6f 63 6b 20 69 73 20 72 65 61 6c 6c 79 20  block is really 
18987 61 6e 20 69 6e 6c 69 6e 65 64 20 76 65 72 73 69  an inlined versi
18988 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62  on of sqlite3Vdb
18989 65 4d 65 6d 52 65 6c 65 61 73 65 28 29 0a 20 20  eMemRelease().  
1898a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 61 6b 65      ** that take
1898b 73 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74  s advantage of t
1898c 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65  he fact that the
1898d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 76 61 6c   memory cell val
1898e 75 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20  ue is .      ** 
1898f 62 65 69 6e 67 20 73 65 74 20 74 6f 20 4e 55 4c  being set to NUL
18990 4c 20 61 66 74 65 72 20 72 65 6c 65 61 73 69 6e  L after releasin
18991 67 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 72 65  g any dynamic re
18992 73 6f 75 72 63 65 73 2e 0a 20 20 20 20 20 20 2a  sources..      *
18993 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 6a  *.      ** The j
18994 75 73 74 69 66 69 63 61 74 69 6f 6e 20 66 6f 72  ustification for
18995 20 64 75 70 6c 69 63 61 74 69 6e 67 20 63 6f 64   duplicating cod
18996 65 20 69 73 20 74 68 61 74 20 61 63 63 6f 72 64  e is that accord
18997 69 6e 67 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a  ing to .      **
18998 20 63 61 6c 6c 67 72 69 6e 64 2c 20 74 68 69 73   callgrind, this
18999 20 63 61 75 73 65 73 20 61 20 63 65 72 74 61 69   causes a certai
1899a 6e 20 74 65 73 74 20 63 61 73 65 20 74 6f 20 68  n test case to h
1899b 69 74 20 74 68 65 20 43 50 55 20 34 2e 37 20 0a  it the CPU 4.7 .
1899c 20 20 20 20 20 20 2a 2a 20 70 65 72 63 65 6e 74        ** percent
1899d 20 6c 65 73 73 20 28 78 38 36 20 6c 69 6e 75 78   less (x86 linux
1899e 2c 20 67 63 63 20 76 65 72 73 69 6f 6e 20 34 2e  , gcc version 4.
1899f 31 2e 32 2c 20 2d 4f 36 29 20 74 68 61 6e 20 69  1.2, -O6) than i
189a0 66 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  f .      ** sqli
189a1 74 65 33 4d 65 6d 52 65 6c 65 61 73 65 28 29 20  te3MemRelease() 
189a2 77 65 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  were called from
189a3 20 68 65 72 65 2e 20 57 69 74 68 20 2d 4f 32 2c   here. With -O2,
189a4 20 74 68 69 73 20 6a 75 6d 70 73 0a 20 20 20 20   this jumps.    
189a5 20 20 2a 2a 20 74 6f 20 36 2e 36 20 70 65 72 63    ** to 6.6 perc
189a6 65 6e 74 2e 20 54 68 65 20 74 65 73 74 20 63 61  ent. The test ca
189a7 73 65 20 69 73 20 69 6e 73 65 72 74 69 6e 67 20  se is inserting 
189a8 31 30 30 30 20 72 6f 77 73 20 69 6e 74 6f 20 61  1000 rows into a
189a9 20 74 61 62 6c 65 20 0a 20 20 20 20 20 20 2a 2a   table .      **
189aa 20 77 69 74 68 20 6e 6f 20 69 6e 64 65 78 65 73   with no indexes
189ab 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20   using a single 
189ac 70 72 65 70 61 72 65 64 20 49 4e 53 45 52 54 20  prepared INSERT 
189ad 73 74 61 74 65 6d 65 6e 74 2c 20 62 69 6e 64 28  statement, bind(
189ae 29 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  ) .      ** and 
189af 72 65 73 65 74 28 29 2e 20 49 6e 73 65 72 74 73  reset(). Inserts
189b0 20 61 72 65 20 67 72 6f 75 70 65 64 20 69 6e 74   are grouped int
189b1 6f 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  o a transaction.
189b2 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
189b3 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45  if( p->flags&(ME
189b4 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 29 20 29  M_Agg|MEM_Dyn) )
189b5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
189b6 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
189b7 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  p);.      }else 
189b8 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29  if( p->zMalloc )
189b9 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
189ba 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
189bb 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20  Malloc);.       
189bc 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b   p->zMalloc = 0;
189bd 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
189be 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  p->flags = MEM_N
189bf 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  ull;.    }.    d
189c0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
189c1 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b  = malloc_failed;
189c2 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 53  .  }.}..#ifdef S
189c3 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
189c4 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53  ORY_MANAGEMENT.S
189c5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
189c6 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c  t sqlite3VdbeRel
189c7 65 61 73 65 42 75 66 66 65 72 73 28 56 64 62 65  easeBuffers(Vdbe
189c8 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a   *p){.  int ii;.
189c9 20 20 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b    int nFree = 0;
189ca 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
189cb 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
189cc 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
189cd 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 3d 70   for(ii=1; ii<=p
189ce 2d 3e 6e 4d 65 6d 3b 20 69 69 2b 2b 29 7b 0a 20  ->nMem; ii++){. 
189cf 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26     Mem *pMem = &
189d0 70 2d 3e 61 4d 65 6d 5b 69 69 5d 3b 0a 20 20 20  p->aMem[ii];.   
189d1 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
189d2 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b   & MEM_RowSet ){
189d3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f  .      sqlite3Ro
189d4 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e  wSetClear(pMem->
189d5 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20  u.pRowSet);.    
189d6 7d 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e  }.    if( pMem->
189d7 7a 20 26 26 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  z && pMem->flags
189d8 26 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20  &MEM_Dyn ){.    
189d9 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 2d    assert( !pMem-
189da 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 6e  >xDel );.      n
189db 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 44  Free += sqlite3D
189dc 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d  bMallocSize(pMem
189dd 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a  ->db, pMem->z);.
189de 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
189df 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
189e0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
189e1 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23  eturn nFree;.}.#
189e2 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
189e3 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
189e4 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20  IN./*.** Give a 
189e5 6c 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70  listing of the p
189e6 72 6f 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69  rogram in the vi
189e7 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a  rtual machine..*
189e8 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61  *.** The interfa
189e9 63 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ce is the same a
189ea 73 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  s sqlite3VdbeExe
189eb 63 28 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61  c().  But instea
189ec 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20  d of.** running 
189ed 74 68 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76  the code, it inv
189ee 6f 6b 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63  okes the callbac
189ef 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  k once for each 
189f0 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20  instruction..** 
189f1 54 68 69 73 20 66 65 61 74 75 72 65 20 69 73 20  This feature is 
189f2 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
189f3 74 20 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a  t "EXPLAIN"..**.
189f4 2a 2a 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61  ** When p->expla
189f5 69 6e 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74  in==1, each inst
189f6 72 75 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65  ruction is liste
189f7 64 2e 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65  d.  When.** p->e
189f8 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20  xplain==2, only 
189f9 4f 50 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72  OP_Explain instr
189fa 75 63 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74  uctions are list
189fb 65 64 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20  ed and these.** 
189fc 61 72 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64  are shown in a d
189fd 69 66 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e  ifferent format.
189fe 20 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20    p->explain==2 
189ff 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  is used to imple
18a00 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20  ment.** EXPLAIN 
18a01 51 55 45 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53  QUERY PLAN..*/.S
18a02 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18a03 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73  t sqlite3VdbeLis
18a04 74 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20  t(.  Vdbe *p    
18a05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18a06 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b  * The VDBE */.){
18a07 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
18a08 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b   p->db;.  int i;
18a09 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
18a0a 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 2a 70 4d  TE_OK;.  Mem *pM
18a0b 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53  em = p->pResultS
18a0c 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d  et = &p->aMem[1]
18a0d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ;..  assert( p->
18a0e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28  explain );.  if(
18a0f 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f   p->magic!=VDBE_
18a10 4d 41 47 49 43 5f 52 55 4e 20 29 20 72 65 74 75  MAGIC_RUN ) retu
18a11 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
18a12 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
18a13 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41  magic==SQLITE_MA
18a14 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 61 73  GIC_BUSY );.  as
18a15 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c  sert( p->rc==SQL
18a16 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d  ITE_OK || p->rc=
18a17 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20  =SQLITE_BUSY || 
18a18 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  p->rc==SQLITE_NO
18a19 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 45 76 65  MEM );..  /* Eve
18a1a 6e 20 74 68 6f 75 67 68 20 74 68 69 73 20 6f 70  n though this op
18a1b 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20 75 73  code does not us
18a1c 65 20 64 79 6e 61 6d 69 63 20 73 74 72 69 6e 67  e dynamic string
18a1d 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72  s for.  ** the r
18a1e 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 20 63 6f  esult, result co
18a1f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 6f 6d 65  lumns may become
18a20 20 64 79 6e 61 6d 69 63 20 69 66 20 74 68 65 20   dynamic if the 
18a21 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 2a 2a 20  user calls.  ** 
18a22 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
18a23 65 78 74 31 36 28 29 2c 20 63 61 75 73 69 6e 67  ext16(), causing
18a24 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74   a translation t
18a25 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e  o UTF-16 encodin
18a26 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c 65 61 73  g..  */.  releas
18a27 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 6d 2c 20  eMemArray(pMem, 
18a28 70 2d 3e 6e 4d 65 6d 29 3b 0a 0a 20 20 69 66 28  p->nMem);..  if(
18a29 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   p->rc==SQLITE_N
18a2a 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54  OMEM ){.    /* T
18a2b 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61  his happens if a
18a2c 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65   malloc() inside
18a2d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
18a2e 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
18a2f 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   or.    ** sqlit
18a30 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
18a31 28 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20  () failed.  */. 
18a32 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
18a33 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74  led = 1;.    ret
18a34 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
18a35 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20  ;.  }..  do{.   
18a36 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20   i = p->pc++;.  
18a37 7d 77 68 69 6c 65 28 20 69 3c 70 2d 3e 6e 4f 70  }while( i<p->nOp
18a38 20 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d   && p->explain==
18a39 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f  2 && p->aOp[i].o
18a3a 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69  pcode!=OP_Explai
18a3b 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d  n );.  if( i>=p-
18a3c 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72  >nOp ){.    p->r
18a3d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
18a3e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44     rc = SQLITE_D
18a3f 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ONE;.  }else if(
18a40 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
18a41 75 70 74 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e  upted ){.    p->
18a42 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  rc = SQLITE_INTE
18a43 52 52 55 50 54 3b 0a 20 20 20 20 72 63 20 3d 20  RRUPT;.    rc = 
18a44 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
18a45 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
18a46 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
18a47 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
18a48 33 45 72 72 53 74 72 28 70 2d 3e 72 63 29 29 3b  3ErrStr(p->rc));
18a49 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68  .  }else{.    ch
18a4a 61 72 20 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a 70  ar *z;.    Op *p
18a4b 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b  Op = &p->aOp[i];
18a4c 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c  .    if( p->expl
18a4d 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ain==1 ){.      
18a4e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
18a4f 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d 65  M_Int;.      pMe
18a50 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
18a51 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20  _INTEGER;.      
18a52 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 3b 20 20  pMem->u.i = i;  
18a53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18a55 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72   Program counter
18a56 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b   */.      pMem++
18a57 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d 65 6d 2d  ;.  .      pMem-
18a58 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 61  >flags = MEM_Sta
18a59 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  tic|MEM_Str|MEM_
18a5a 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d  Term;.      pMem
18a5b 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  ->z = (char*)sql
18a5c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70  ite3OpcodeName(p
18a5d 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 20 2f 2a  Op->opcode);  /*
18a5e 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20   Opcode */.     
18a5f 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a   assert( pMem->z
18a60 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d 65  !=0 );.      pMe
18a61 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  m->n = sqlite3St
18a62 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b  rlen30(pMem->z);
18a63 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  .      pMem->typ
18a64 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b  e = SQLITE_TEXT;
18a65 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63  .      pMem->enc
18a66 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
18a67 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20        pMem++;.  
18a68 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66    }..    pMem->f
18a69 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
18a6a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
18a6b 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20 20 20 20  pOp->p1;        
18a6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a6d 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70    /* P1 */.    p
18a6e 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
18a6f 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  TE_INTEGER;.    
18a70 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65  pMem++;..    pMe
18a71 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
18a72 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  nt;.    pMem->u.
18a73 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20  i = pOp->p2;    
18a74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a75 20 20 20 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20        /* P2 */. 
18a76 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
18a77 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a  SQLITE_INTEGER;.
18a78 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20      pMem++;..   
18a79 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d   if( p->explain=
18a7a 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d  =1 ){.      pMem
18a7b 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
18a7c 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  t;.      pMem->u
18a7d 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b 20 20 20  .i = pOp->p3;   
18a7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a7f 20 20 20 20 20 20 20 2f 2a 20 50 33 20 2a 2f 0a         /* P3 */.
18a80 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65        pMem->type
18a81 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45   = SQLITE_INTEGE
18a82 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  R;.      pMem++;
18a83 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
18a84 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
18a85 6f 77 28 70 4d 65 6d 2c 20 33 32 2c 20 30 29 20  ow(pMem, 32, 0) 
18a86 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ){            /*
18a87 20 50 34 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e   P4 */.      p->
18a88 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
18a89 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75   = 1;.      retu
18a8a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
18a8b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
18a8c 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e  >flags = MEM_Dyn
18a8d 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72  |MEM_Str|MEM_Ter
18a8e 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c  m;.    z = displ
18a8f 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e  ayP4(pOp, pMem->
18a90 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20  z, 32);.    if( 
18a91 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20  z!=pMem->z ){.  
18a92 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
18a93 65 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a  emSetStr(pMem, z
18a94 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
18a95 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  8, 0);.    }else
18a96 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
18a97 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20  pMem->z!=0 );.  
18a98 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71      pMem->n = sq
18a99 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d  lite3Strlen30(pM
18a9a 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d  em->z);.      pM
18a9b 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45  em->enc = SQLITE
18a9c 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20  _UTF8;.    }.   
18a9d 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
18a9e 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70  LITE_TEXT;.    p
18a9f 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20  Mem++;..    if( 
18aa0 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b  p->explain==1 ){
18aa1 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
18aa2 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
18aa3 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20  em, 4, 0) ){.   
18aa4 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c       p->db->mall
18aa5 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
18aa6 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
18aa7 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
18aa8 20 7d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66   }.      pMem->f
18aa9 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d  lags = MEM_Dyn|M
18aaa 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b  EM_Str|MEM_Term;
18aab 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
18aac 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   2;.      sqlite
18aad 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 70 4d  3_snprintf(3, pM
18aae 65 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 2c 20 70  em->z, "%.2x", p
18aaf 4f 70 2d 3e 70 35 29 3b 20 20 20 2f 2a 20 50 35  Op->p5);   /* P5
18ab0 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
18ab1 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45  type = SQLITE_TE
18ab2 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  XT;.      pMem->
18ab3 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
18ab4 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  8;.      pMem++;
18ab5 0a 20 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .  .#ifdef SQLIT
18ab6 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66  E_DEBUG.      if
18ab7 28 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20  ( pOp->zComment 
18ab8 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  ){.        pMem-
18ab9 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72  >flags = MEM_Str
18aba 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20  |MEM_Term;.     
18abb 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70     pMem->z = pOp
18abc 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20  ->zComment;.    
18abd 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71      pMem->n = sq
18abe 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d  lite3Strlen30(pM
18abf 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 20 20  em->z);.        
18ac0 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
18ac1 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 20  TE_UTF8;.       
18ac2 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
18ac3 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20  LITE_TEXT;.     
18ac4 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
18ac5 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 4d      {.        pM
18ac6 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
18ac7 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Null;           
18ac8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
18ac9 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20  omment */.      
18aca 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
18acb 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20  QLITE_NULL;.    
18acc 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70    }.    }..    p
18acd 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 38  ->nResColumn = 8
18ace 20 2d 20 35 2a 28 70 2d 3e 65 78 70 6c 61 69 6e   - 5*(p->explain
18acf 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  -1);.    p->rc =
18ad0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
18ad1 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b  rc = SQLITE_ROW;
18ad2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
18ad3 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
18ad4 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
18ad5 4e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  N */..#ifdef SQL
18ad6 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
18ad7 50 72 69 6e 74 20 74 68 65 20 53 51 4c 20 74 68  Print the SQL th
18ad8 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 67  at was used to g
18ad9 65 6e 65 72 61 74 65 20 61 20 56 44 42 45 20 70  enerate a VDBE p
18ada 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  rogram..*/.SQLIT
18adb 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
18adc 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53  qlite3VdbePrintS
18add 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69  ql(Vdbe *p){.  i
18ade 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b  nt nOp = p->nOp;
18adf 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a  .  VdbeOp *pOp;.
18ae0 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65    if( nOp<1 ) re
18ae1 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70  turn;.  pOp = &p
18ae2 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20  ->aOp[0];.  if( 
18ae3 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
18ae4 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34  Trace && pOp->p4
18ae5 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e  .z!=0 ){.    con
18ae6 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70  st char *z = pOp
18ae7 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 68 69 6c  ->p4.z;.    whil
18ae8 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 38 2a  e( isspace(*(u8*
18ae9 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 70  )z) ) z++;.    p
18aea 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25 73 5d  rintf("SQL: [%s]
18aeb 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23  \n", z);.  }.}.#
18aec 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69  endif..#if !defi
18aed 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
18aee 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65  TRACE) && define
18aef 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
18af0 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 50  IOTRACE)./*.** P
18af1 72 69 6e 74 20 61 6e 20 49 4f 54 52 41 43 45 20  rint an IOTRACE 
18af2 6d 65 73 73 61 67 65 20 73 68 6f 77 69 6e 67 20  message showing 
18af3 53 51 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a  SQL content..*/.
18af4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
18af5 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49  oid sqlite3VdbeI
18af6 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65 20 2a  OTraceSql(Vdbe *
18af7 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20  p){.  int nOp = 
18af8 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70  p->nOp;.  VdbeOp
18af9 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 73 71 6c   *pOp;.  if( sql
18afa 69 74 65 33 49 6f 54 72 61 63 65 3d 3d 30 20 29  ite3IoTrace==0 )
18afb 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6e   return;.  if( n
18afc 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20  Op<1 ) return;. 
18afd 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30   pOp = &p->aOp[0
18afe 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  ];.  if( pOp->op
18aff 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26  code==OP_Trace &
18b00 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29  & pOp->p4.z!=0 )
18b01 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  {.    int i, j;.
18b02 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30 30 5d      char z[1000]
18b03 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ;.    sqlite3_sn
18b04 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 29  printf(sizeof(z)
18b05 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70 2d 3e  , z, "%s", pOp->
18b06 70 34 2e 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69  p4.z);.    for(i
18b07 3d 30 3b 20 69 73 73 70 61 63 65 28 28 75 6e 73  =0; isspace((uns
18b08 69 67 6e 65 64 20 63 68 61 72 29 7a 5b 69 5d 29  igned char)z[i])
18b09 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72  ; i++){}.    for
18b0a 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29  (j=0; z[i]; i++)
18b0b 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 73 70  {.      if( issp
18b0c 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68  ace((unsigned ch
18b0d 61 72 29 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20  ar)z[i]) ){.    
18b0e 20 20 20 20 69 66 28 20 7a 5b 69 2d 31 5d 21 3d      if( z[i-1]!=
18b0f 27 20 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ' ' ){.         
18b10 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20   z[j++] = ' ';. 
18b11 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
18b12 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b  else{.        z[
18b13 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20  j++] = z[i];.   
18b14 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a     }.    }.    z
18b15 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  [j] = 0;.    sql
18b16 69 74 65 33 49 6f 54 72 61 63 65 28 22 53 51 4c  ite3IoTrace("SQL
18b17 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a   %s\n", z);.  }.
18b18 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
18b19 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 26  ITE_OMIT_TRACE &
18b1a 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
18b1b 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 0a 2f 2a 0a  IOTRACE */.../*.
18b1c 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72  ** Prepare a vir
18b1d 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72  tual machine for
18b1e 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69   execution.  Thi
18b1f 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67  s involves thing
18b20 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c  s such.** as all
18b21 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70  ocating stack sp
18b22 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ace and initiali
18b23 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d  zing the program
18b24 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74   counter..** Aft
18b25 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20  er the VDBE has 
18b26 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63  be prepped, it c
18b27 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62  an be executed b
18b28 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  y one or more.**
18b29 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
18b2a 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a  3VdbeExec().  .*
18b2b 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
18b2c 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76   only way to mov
18b2d 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44  e a VDBE from VD
18b2e 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f  BE_MAGIC_INIT to
18b2f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52  .** VDBE_MAGIC_R
18b30 55 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  UN..*/.SQLITE_PR
18b31 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
18b32 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28  e3VdbeMakeReady(
18b33 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20  .  Vdbe *p,     
18b34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b35 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f    /* The VDBE */
18b36 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20  .  int nVar,    
18b37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b38 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27    /* Number of '
18b39 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20 53 51  ?' see in the SQ
18b3a 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  L statement */. 
18b3b 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20   int nMem,      
18b3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b3d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d  /* Number of mem
18b3e 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c  ory cells to all
18b3f 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  ocate */.  int n
18b40 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20  Cursor,         
18b41 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
18b42 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74  ber of cursors t
18b43 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20  o allocate */.  
18b44 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 20 20 20  int isExplain   
18b45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18b46 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58  * True if the EX
18b47 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69  PLAIN keywords i
18b48 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 29 7b 0a  s present */.){.
18b49 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74    int n;.  sqlit
18b4a 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  e3 *db = p->db;.
18b4b 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
18b4c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
18b4d 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
18b4e 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f 2a 20  C_INIT );..  /* 
18b4f 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20  There should be 
18b50 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 63  at least one opc
18b51 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ode..  */.  asse
18b52 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a  rt( p->nOp>0 );.
18b53 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d 61  .  /* Set the ma
18b54 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41 47 49  gic to VDBE_MAGI
18b55 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72 61 74  C_RUN sooner rat
18b56 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 2e 20  her than later. 
18b57 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20  */.  p->magic = 
18b58 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a  VDBE_MAGIC_RUN;.
18b59 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 63  .  /* For each c
18b5a 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 2c 20  ursor required, 
18b5b 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  also allocate a 
18b5c 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d 65 6d  memory cell. Mem
18b5d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 28  ory.  ** cells (
18b5e 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 29 2e  nMem+1-nCursor).
18b5f 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 76 65  .nMem, inclusive
18b60 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  , will never be 
18b61 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68 65  used by.  ** the
18b62 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 49   vdbe program. I
18b63 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 65 20  nstead they are 
18b64 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  used to allocate
18b65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20   space for.  ** 
18b66 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 75 72  VdbeCursor/BtCur
18b67 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 2e 20  sor structures. 
18b68 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f  The blob of memo
18b69 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ry associated wi
18b6a 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20  th .  ** cursor 
18b6b 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d  0 is stored in m
18b6c 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 6d 2e  emory cell nMem.
18b6d 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 6e 4d   Memory cell (nM
18b6e 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f 72 65  em-1).  ** store
18b6f 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65  s the blob of me
18b70 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  mory associated 
18b71 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c 20 65  with cursor 1, e
18b72 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 65  tc..  **.  ** Se
18b73 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 74 65  e also: allocate
18b74 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20  Cursor()..  */. 
18b75 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 6f 72   nMem += nCursor
18b76 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c  ;..  /*.  ** All
18b77 6f 63 61 74 69 6f 6e 20 73 70 61 63 65 20 66 6f  ocation space fo
18b78 72 20 72 65 67 69 73 74 65 72 73 2e 0a 20 20 2a  r registers..  *
18b79 2f 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 3d  /.  if( p->aMem=
18b7a 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41  =0 ){.    int nA
18b7b 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78  rg;       /* Max
18b7c 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61  imum number of a
18b7d 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 61 20  rgs passed to a 
18b7e 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 2a  user function. *
18b7f 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 56  /.    resolveP2V
18b80 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 3b  alues(p, &nArg);
18b81 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 56 61  .    assert( nVa
18b82 72 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  r>=0 );.    if( 
18b83 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e 4d 65  isExplain && nMe
18b84 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20 6e 4d  m<10 ){.      nM
18b85 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d 0a 20  em = 10;.    }. 
18b86 20 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 73 71 6c     p->aMem = sql
18b87 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
18b88 28 64 62 2c 0a 20 20 20 20 20 20 20 20 6e 4d 65  (db,.        nMe
18b89 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 20 20 20  m*sizeof(Mem)   
18b8a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61              /* a
18b8b 4d 65 6d 20 2a 2f 0a 20 20 20 20 20 20 2b 20 6e  Mem */.      + n
18b8c 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 20  Var*sizeof(Mem) 
18b8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18b8e 20 61 56 61 72 20 2a 2f 0a 20 20 20 20 20 20 2b   aVar */.      +
18b8f 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d   nArg*sizeof(Mem
18b90 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *)              
18b91 2f 2a 20 61 70 41 72 67 20 2a 2f 0a 20 20 20 20  /* apArg */.    
18b92 20 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28    + nVar*sizeof(
18b93 63 68 61 72 2a 29 20 20 20 20 20 20 20 20 20 20  char*)          
18b94 20 20 20 2f 2a 20 61 7a 56 61 72 20 2a 2f 0a 20     /* azVar */. 
18b95 20 20 20 20 20 2b 20 6e 43 75 72 73 6f 72 2a 73       + nCursor*s
18b96 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72  izeof(VdbeCursor
18b97 2a 29 2b 31 20 20 2f 2a 20 61 70 43 73 72 20 2a  *)+1  /* apCsr *
18b98 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28  /.    );.    if(
18b99 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
18b9a 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61  ed ){.      p->a
18b9b 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20 20 20  Mem--;          
18b9c 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65     /* aMem[] goe
18b9d 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a  s from 1..nMem *
18b9e 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20  /.      p->nMem 
18b9f 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 2f  = nMem;        /
18ba0 2a 20 20 20 20 20 20 20 6e 6f 74 20 66 72 6f 6d  *       not from
18ba1 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a 2f 0a 20 20   0..nMem-1 */.  
18ba2 20 20 20 20 70 2d 3e 61 56 61 72 20 3d 20 26 70      p->aVar = &p
18ba3 2d 3e 61 4d 65 6d 5b 6e 4d 65 6d 2b 31 5d 3b 0a  ->aMem[nMem+1];.
18ba4 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20        p->nVar = 
18ba5 6e 56 61 72 3b 0a 20 20 20 20 20 20 70 2d 3e 6f  nVar;.      p->o
18ba6 6b 56 61 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  kVar = 0;.      
18ba7 70 2d 3e 61 70 41 72 67 20 3d 20 28 4d 65 6d 2a  p->apArg = (Mem*
18ba8 2a 29 26 70 2d 3e 61 56 61 72 5b 6e 56 61 72 5d  *)&p->aVar[nVar]
18ba9 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 72  ;.      p->azVar
18baa 20 3d 20 28 63 68 61 72 2a 2a 29 26 70 2d 3e 61   = (char**)&p->a
18bab 70 41 72 67 5b 6e 41 72 67 5d 3b 0a 20 20 20 20  pArg[nArg];.    
18bac 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28 56 64    p->apCsr = (Vd
18bad 62 65 43 75 72 73 6f 72 2a 2a 29 26 70 2d 3e 61  beCursor**)&p->a
18bae 7a 56 61 72 5b 6e 56 61 72 5d 3b 0a 20 20 20 20  zVar[nVar];.    
18baf 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 6e    p->nCursor = n
18bb0 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 66 6f  Cursor;.      fo
18bb1 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20 6e  r(n=0; n<nVar; n
18bb2 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ++){.        p->
18bb3 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20  aVar[n].flags = 
18bb4 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20  MEM_Null;.      
18bb5 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 20    p->aVar[n].db 
18bb6 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = db;.      }.  
18bb7 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c 3d      for(n=1; n<=
18bb8 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20  nMem; n++){.    
18bb9 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 66      p->aMem[n].f
18bba 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
18bbb 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d  .        p->aMem
18bbc 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20  [n].db = db;.   
18bbd 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
18bbe 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
18bbf 55 47 0a 20 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c  UG.  for(n=1; n<
18bc0 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20  p->nMem; n++){. 
18bc1 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d     assert( p->aM
18bc2 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 20 29 3b 0a  em[n].db==db );.
18bc3 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70 2d    }.#endif..  p-
18bc4 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d 3e 72  >pc = -1;.  p->r
18bc5 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
18bc6 20 70 2d 3e 75 6e 69 71 75 65 43 6e 74 20 3d 20   p->uniqueCnt = 
18bc7 30 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74  0;.  p->errorAct
18bc8 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ion = OE_Abort;.
18bc9 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d 20    p->explain |= 
18bca 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d 3e  isExplain;.  p->
18bcb 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47  magic = VDBE_MAG
18bcc 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 68  IC_RUN;.  p->nCh
18bcd 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 63  ange = 0;.  p->c
18bce 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 70  acheCtr = 1;.  p
18bcf 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f  ->minWriteFileFo
18bd0 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 2d  rmat = 255;.  p-
18bd1 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e 74  >openedStatement
18bd2 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44 42   = 0;.#ifdef VDB
18bd3 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20  E_PROFILE.  {.  
18bd4 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
18bd5 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20  (i=0; i<p->nOp; 
18bd6 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61  i++){.      p->a
18bd7 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20  Op[i].cnt = 0;. 
18bd8 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63       p->aOp[i].c
18bd9 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d  ycles = 0;.    }
18bda 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
18bdb 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 42  *.** Close a VDB
18bdc 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 6c  E cursor and rel
18bdd 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 73  ease all the res
18bde 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72 73  ources that curs
18bdf 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74  or .** happens t
18be0 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54  o hold..*/.SQLIT
18be1 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
18be2 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75  qlite3VdbeFreeCu
18be3 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 64  rsor(Vdbe *p, Vd
18be4 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a  beCursor *pCx){.
18be5 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a    if( pCx==0 ){.
18be6 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
18be7 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20 29    if( pCx->pBt )
18be8 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
18be9 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74  eeClose(pCx->pBt
18bea 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 43  );.    /* The pC
18beb 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c 20  x->pCursor will 
18bec 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74  be close automat
18bed 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 78  ically, if it ex
18bee 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20  ists, by.    ** 
18bef 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20  the call above. 
18bf0 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  */.  }else if( p
18bf1 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20  Cx->pCursor ){. 
18bf2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
18bf3 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d 3e  loseCursor(pCx->
18bf4 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69  pCursor);.  }.#i
18bf5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18bf6 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
18bf7 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43   if( pCx->pVtabC
18bf8 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c  ursor ){.    sql
18bf9 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
18bfa 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20   *pVtabCursor = 
18bfb 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  pCx->pVtabCursor
18bfc 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69  ;.    const sqli
18bfd 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
18bfe 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75  ule = pCx->pModu
18bff 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61  le;.    p->inVta
18c00 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20  bMethod = 1;.   
18c01 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
18c02 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a  fetyOff(p->db);.
18c03 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c      pModule->xCl
18c04 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 29  ose(pVtabCursor)
18c05 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
18c06 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64  te3SafetyOn(p->d
18c07 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61  b);.    p->inVta
18c08 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d  bMethod = 0;.  }
18c09 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 70  .#endif.  if( !p
18c0a 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54  Cx->ephemPseudoT
18c0b 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  able ){.    sqli
18c0c 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c  te3DbFree(p->db,
18c0d 20 70 43 78 2d 3e 70 44 61 74 61 29 3b 0a 20 20   pCx->pData);.  
18c0e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  }.}../*.** Close
18c0f 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 63   all cursors exc
18c10 65 70 74 20 66 6f 72 20 56 54 61 62 20 63 75 72  ept for VTab cur
18c11 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 63 75  sors that are cu
18c12 72 72 65 6e 74 6c 79 0a 2a 2a 20 69 6e 20 75 73  rrently.** in us
18c13 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
18c14 64 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72  d closeAllCursor
18c15 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74 61  sExceptActiveVta
18c16 62 73 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69  bs(Vdbe *p){.  i
18c17 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 2d 3e 61  nt i;.  if( p->a
18c18 70 43 73 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  pCsr==0 ) return
18c19 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
18c1a 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b 29 7b  ->nCursor; i++){
18c1b 0a 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20  .    VdbeCursor 
18c1c 2a 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69  *pC = p->apCsr[i
18c1d 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 20 26 26  ];.    if( pC &&
18c1e 20 28 21 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68   (!p->inVtabMeth
18c1f 6f 64 20 7c 7c 20 21 70 43 2d 3e 70 56 74 61 62  od || !pC->pVtab
18c20 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 20  Cursor) ){.     
18c21 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65   sqlite3VdbeFree
18c22 43 75 72 73 6f 72 28 70 2c 20 70 43 29 3b 0a 20  Cursor(p, pC);. 
18c23 20 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d       p->apCsr[i]
18c24 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
18c25 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75  }../*.** Clean u
18c26 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 20 65  p the VM after e
18c27 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  xecution..**.** 
18c28 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
18c29 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  l automatically 
18c2a 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72  close any cursor
18c2b 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72  s, lists, and/or
18c2c 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74  .** sorters that
18c2d 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e   were left open.
18c2e 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65    It also delete
18c2f 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a  s the values of.
18c30 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20  ** variables in 
18c31 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79  the aVar[] array
18c32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
18c33 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70   Cleanup(Vdbe *p
18c34 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71  ){.  int i;.  sq
18c35 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
18c36 62 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a  b;.  Mem *pMem;.
18c37 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72    closeAllCursor
18c38 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74 61  sExceptActiveVta
18c39 62 73 28 70 29 3b 0a 20 20 66 6f 72 28 70 4d 65  bs(p);.  for(pMe
18c3a 6d 3d 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 69  m=&p->aMem[1], i
18c3b 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20  =1; i<=p->nMem; 
18c3c 69 2b 2b 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20  i++, pMem++){.  
18c3d 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67    if( pMem->flag
18c3e 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29  s & MEM_RowSet )
18c3f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
18c40 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d  owSetClear(pMem-
18c41 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20  >u.pRowSet);.   
18c42 20 7d 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70   }.    MemSetTyp
18c43 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f  eFlag(pMem, MEM_
18c44 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 6c  Null);.  }.  rel
18c45 65 61 73 65 4d 65 6d 41 72 72 61 79 28 26 70 2d  easeMemArray(&p-
18c46 3e 61 4d 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65  >aMem[1], p->nMe
18c47 6d 29 3b 0a 20 20 69 66 28 20 70 2d 3e 63 6f 6e  m);.  if( p->con
18c48 74 65 78 74 53 74 61 63 6b 20 29 7b 0a 20 20 20  textStack ){.   
18c49 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
18c4a 62 2c 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61  b, p->contextSta
18c4b 63 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 63 6f  ck);.  }.  p->co
18c4c 6e 74 65 78 74 53 74 61 63 6b 20 3d 20 30 3b 0a  ntextStack = 0;.
18c4d 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63    p->contextStac
18c4e 6b 44 65 70 74 68 20 3d 20 30 3b 0a 20 20 70 2d  kDepth = 0;.  p-
18c4f 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70  >contextStackTop
18c50 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44   = 0;.  sqlite3D
18c51 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
18c52 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72  rMsg);.  p->zErr
18c53 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52  Msg = 0;.  p->pR
18c54 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a  esultSet = 0;.}.
18c55 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e  ./*.** Set the n
18c56 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
18c57 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c  columns that wil
18c58 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79  l be returned by
18c59 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61   this SQL.** sta
18c5a 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20  tement. This is 
18c5b 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69  now set at compi
18c5c 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20  le time, rather 
18c5d 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65  than during.** e
18c5e 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20  xecution of the 
18c5f 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20  vdbe program so 
18c60 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c  that sqlite3_col
18c61 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a  umn_count() can.
18c62 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20  ** be called on 
18c63 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
18c64 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f   before sqlite3_
18c65 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  step()..*/.SQLIT
18c66 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
18c67 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
18c68 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e  Cols(Vdbe *p, in
18c69 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20  t nResColumn){. 
18c6a 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a   Mem *pColName;.
18c6b 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74    int n;.  sqlit
18c6c 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  e3 *db = p->db;.
18c6d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  .  releaseMemArr
18c6e 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20  ay(p->aColName, 
18c6f 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f  p->nResColumn*CO
18c70 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69  LNAME_N);.  sqli
18c71 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
18c72 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20  >aColName);.  n 
18c73 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c  = nResColumn*COL
18c74 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65  NAME_N;.  p->nRe
18c75 73 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 43 6f  sColumn = nResCo
18c76 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c 4e  lumn;.  p->aColN
18c77 61 6d 65 20 3d 20 70 43 6f 6c 4e 61 6d 65 20 3d  ame = pColName =
18c78 20 28 4d 65 6d 2a 29 73 71 6c 69 74 65 33 44 62   (Mem*)sqlite3Db
18c79 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
18c7a 69 7a 65 6f 66 28 4d 65 6d 29 2a 6e 20 29 3b 0a  izeof(Mem)*n );.
18c7b 20 20 69 66 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d    if( p->aColNam
18c7c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  e==0 ) return;. 
18c7d 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 20   while( n-- > 0 
18c7e 29 7b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d  ){.    pColName-
18c7f 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
18c80 6c 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d  l;.    pColName-
18c81 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20  >db = p->db;.   
18c82 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b 0a 20 20 7d   pColName++;.  }
18c83 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
18c84 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 64  e name of the id
18c85 78 27 74 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62  x'th column to b
18c86 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  e returned by th
18c87 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  e SQL statement.
18c88 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75 73 74 20 62  .** zName must b
18c89 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  e a pointer to a
18c8a 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20   nul terminated 
18c8b 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  string..**.** Th
18c8c 69 73 20 63 61 6c 6c 20 6d 75 73 74 20 62 65 20  is call must be 
18c8d 6d 61 64 65 20 61 66 74 65 72 20 61 20 63 61 6c  made after a cal
18c8e 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65  l to sqlite3Vdbe
18c8f 53 65 74 4e 75 6d 43 6f 6c 73 28 29 2e 0a 2a 2a  SetNumCols()..**
18c90 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20 70 61  .** The final pa
18c91 72 61 6d 65 74 65 72 2c 20 78 44 65 6c 2c 20 6d  rameter, xDel, m
18c92 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 51  ust be one of SQ
18c93 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20 53 51  LITE_DYNAMIC, SQ
18c94 4c 49 54 45 5f 53 54 41 54 49 43 0a 2a 2a 20 6f  LITE_STATIC.** o
18c95 72 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  r SQLITE_TRANSIE
18c96 4e 54 2e 20 49 66 20 69 74 20 69 73 20 53 51 4c  NT. If it is SQL
18c97 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65  ITE_DYNAMIC, the
18c98 6e 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69  n the buffer poi
18c99 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 7a 4e  nted.** to by zN
18c9a 61 6d 65 20 77 69 6c 6c 20 62 65 20 66 72 65 65  ame will be free
18c9b 64 20 62 79 20 73 71 6c 69 74 65 33 44 62 46 72  d by sqlite3DbFr
18c9c 65 65 28 29 20 77 68 65 6e 20 74 68 65 20 76 64  ee() when the vd
18c9d 62 65 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e  be is destroyed.
18c9e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18c9f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
18ca0 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 0a 20 20  beSetColName(.  
18ca1 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20  Vdbe *p,        
18ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18ca3 20 2f 2a 20 56 64 62 65 20 62 65 69 6e 67 20 63   /* Vdbe being c
18ca4 6f 6e 66 69 67 75 72 65 64 20 2a 2f 0a 20 20 69  onfigured */.  i
18ca5 6e 74 20 69 64 78 2c 20 20 20 20 20 20 20 20 20  nt idx,         
18ca6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18ca7 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
18ca8 6d 6e 20 7a 4e 61 6d 65 20 61 70 70 6c 69 65 73  mn zName applies
18ca9 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 76 61 72   to */.  int var
18caa 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
18cab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
18cac 20 6f 66 20 74 68 65 20 43 4f 4c 4e 41 4d 45 5f   of the COLNAME_
18cad 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20  * constants */. 
18cae 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
18caf 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  me,             
18cb0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
18cb1 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
18cb2 67 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64  g name */.  void
18cb3 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20   (*xDel)(void*) 
18cb4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18cb5 4d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e  Memory managemen
18cb6 74 20 73 74 72 61 74 65 67 79 20 66 6f 72 20 7a  t strategy for z
18cb7 4e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  Name */.){.  int
18cb8 20 72 63 3b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c   rc;.  Mem *pCol
18cb9 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28 20  Name;.  assert( 
18cba 69 64 78 3c 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d  idx<p->nResColum
18cbb 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  n );.  assert( v
18cbc 61 72 3c 43 4f 4c 4e 41 4d 45 5f 4e 20 29 3b 0a  ar<COLNAME_N );.
18cbd 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c    if( p->db->mal
18cbe 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
18cbf 20 61 73 73 65 72 74 28 20 21 7a 4e 61 6d 65 20   assert( !zName 
18cc0 7c 7c 20 78 44 65 6c 21 3d 53 51 4c 49 54 45 5f  || xDel!=SQLITE_
18cc1 44 59 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20 72  DYNAMIC );.    r
18cc2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
18cc3 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  EM;.  }.  assert
18cc4 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30  ( p->aColName!=0
18cc5 20 29 3b 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 3d   );.  pColName =
18cc6 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69   &(p->aColName[i
18cc7 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 65 73 43 6f  dx+var*p->nResCo
18cc8 6c 75 6d 6e 5d 29 3b 0a 20 20 72 63 20 3d 20 73  lumn]);.  rc = s
18cc9 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
18cca 53 74 72 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e  Str(pColName, zN
18ccb 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ame, -1, SQLITE_
18ccc 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 20 20 61  UTF8, xDel);.  a
18ccd 73 73 65 72 74 28 20 72 63 21 3d 30 20 7c 7c 20  ssert( rc!=0 || 
18cce 21 7a 4e 61 6d 65 20 7c 7c 20 28 70 43 6f 6c 4e  !zName || (pColN
18ccf 61 6d 65 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 54  ame->flags&MEM_T
18cd0 65 72 6d 29 21 3d 30 20 29 3b 0a 20 20 72 65 74  erm)!=0 );.  ret
18cd1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
18cd2 20 41 20 72 65 61 64 20 6f 72 20 77 72 69 74 65   A read or write
18cd3 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79   transaction may
18cd4 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 61   or may not be a
18cd5 63 74 69 76 65 20 6f 6e 20 64 61 74 61 62 61 73  ctive on databas
18cd6 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20  e handle.** db. 
18cd7 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  If a transaction
18cd8 20 69 73 20 61 63 74 69 76 65 2c 20 63 6f 6d 6d   is active, comm
18cd9 69 74 20 69 74 2e 20 49 66 20 74 68 65 72 65 20  it it. If there 
18cda 69 73 20 61 0a 2a 2a 20 77 72 69 74 65 2d 74 72  is a.** write-tr
18cdb 61 6e 73 61 63 74 69 6f 6e 20 73 70 61 6e 6e 69  ansaction spanni
18cdc 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  ng more than one
18cdd 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
18cde 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
18cdf 74 61 6b 65 73 20 63 61 72 65 20 6f 66 20 74 68  takes care of th
18ce0 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
18ce1 20 74 72 69 63 6b 65 72 79 2e 0a 2a 2f 0a 73 74   trickery..*/.st
18ce2 61 74 69 63 20 69 6e 74 20 76 64 62 65 43 6f 6d  atic int vdbeCom
18ce3 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  mit(sqlite3 *db,
18ce4 20 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74   Vdbe *p){.  int
18ce5 20 69 3b 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73   i;.  int nTrans
18ce6 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72   = 0;  /* Number
18ce7 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 77 69   of databases wi
18ce8 74 68 20 61 6e 20 61 63 74 69 76 65 20 77 72 69  th an active wri
18ce9 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  te-transaction *
18cea 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
18ceb 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65  ITE_OK;.  int ne
18cec 65 64 58 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a  edXcommit = 0;..
18ced 20 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e    /* Before doin
18cee 67 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c  g anything else,
18cef 20 63 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28   call the xSync(
18cf0 29 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  ) callback for a
18cf1 6e 79 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20  ny.  ** virtual 
18cf2 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72  module tables wr
18cf3 69 74 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72  itten in this tr
18cf4 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20  ansaction. This 
18cf5 68 61 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64  has to.  ** be d
18cf6 6f 6e 65 20 62 65 66 6f 72 65 20 64 65 74 65 72  one before deter
18cf7 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61  mining whether a
18cf8 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
18cf9 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65  file is .  ** re
18cfa 71 75 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53  quired, as an xS
18cfb 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d  ync() callback m
18cfc 61 79 20 61 64 64 20 61 6e 20 61 74 74 61 63 68  ay add an attach
18cfd 65 64 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  ed database.  **
18cfe 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74   to the transact
18cff 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ion..  */.  rc =
18d00 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63   sqlite3VtabSync
18d01 28 64 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67  (db, &p->zErrMsg
18d02 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
18d03 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
18d04 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
18d05 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74  /* This loop det
18d06 65 72 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74  ermines (a) if t
18d07 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73  he commit hook s
18d08 68 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64  hould be invoked
18d09 20 61 6e 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f   and.  ** (b) ho
18d0a 77 20 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20  w many database 
18d0b 66 69 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20  files have open 
18d0c 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
18d0d 6e 73 2c 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e  ns, not .  ** in
18d0e 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 6d 70  cluding the temp
18d0f 20 64 61 74 61 62 61 73 65 2e 20 28 62 29 20 69   database. (b) i
18d10 73 20 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61  s important beca
18d11 75 73 65 20 69 66 20 6d 6f 72 65 20 74 68 61 6e  use if more than
18d12 20 0a 20 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62   .  ** one datab
18d13 61 73 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20  ase file has an 
18d14 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73  open write trans
18d15 61 63 74 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72  action, a master
18d16 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69   journal.  ** fi
18d17 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66  le is required f
18d18 6f 72 20 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d  or an atomic com
18d19 6d 69 74 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72  mit..  */ .  for
18d1a 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
18d1b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 42 74 72 65   i++){ .    Btre
18d1c 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  e *pBt = db->aDb
18d1d 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
18d1e 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
18d1f 6e 54 72 61 6e 73 28 70 42 74 29 20 29 7b 0a 20  nTrans(pBt) ){. 
18d20 20 20 20 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74       needXcommit
18d21 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20   = 1;.      if( 
18d22 69 21 3d 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b  i!=1 ) nTrans++;
18d23 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
18d24 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   If there are an
18d25 79 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  y write-transact
18d26 69 6f 6e 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76  ions at all, inv
18d27 6f 6b 65 20 74 68 65 20 63 6f 6d 6d 69 74 20 68  oke the commit h
18d28 6f 6f 6b 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65  ook */.  if( nee
18d29 64 58 63 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e  dXcommit && db->
18d2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20  xCommitCallback 
18d2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ){.    assert( (
18d2c 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
18d2d 54 45 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d  TE_CommitBusy)==
18d2e 30 20 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61  0 );.    db->fla
18d2f 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 43 6f 6d  gs |= SQLITE_Com
18d30 6d 69 74 42 75 73 79 3b 0a 20 20 20 20 28 76 6f  mitBusy;.    (vo
18d31 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
18d32 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20  Off(db);.    rc 
18d33 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c  = db->xCommitCal
18d34 6c 62 61 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69  lback(db->pCommi
18d35 74 41 72 67 29 3b 0a 20 20 20 20 28 76 6f 69 64  tArg);.    (void
18d36 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
18d37 28 64 62 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c  (db);.    db->fl
18d38 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 43  ags &= ~SQLITE_C
18d39 6f 6d 6d 69 74 42 75 73 79 3b 0a 20 20 20 20 69  ommitBusy;.    i
18d3a 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
18d3b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e  eturn SQLITE_CON
18d3c 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20  STRAINT;.    }. 
18d3d 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d   }..  /* The sim
18d3e 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f  ple case - no mo
18d3f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61  re than one data
18d40 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63  base file (not c
18d41 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a  ounting the.  **
18d42 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 29 20   TEMP database) 
18d43 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f  has a transactio
18d44 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 65 72  n active.   Ther
18d45 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72  e is no need for
18d46 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72   the.  ** master
18d47 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20  -journal..  **. 
18d48 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75 72   ** If the retur
18d49 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74  n value of sqlit
18d4a 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
18d4b 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c  me() is a zero l
18d4c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e  ength.  ** strin
18d4d 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20  g, it means the 
18d4e 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73  main database is
18d4f 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74   :memory: or a t
18d50 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20  emp file.  In . 
18d51 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20 77 65   ** that case we
18d52 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   do not support 
18d53 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c  atomic multi-fil
18d54 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73  e commits, so us
18d55 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70  e the .  ** simp
18d56 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74 6f 6f  le case then too
18d57 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d  ..  */.  if( 0==
18d58 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
18d59 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46  sqlite3BtreeGetF
18d5a 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b  ilename(db->aDb[
18d5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e  0].pBt)).   || n
18d5c 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20  Trans<=1.  ){.  
18d5d 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
18d5e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
18d5f 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
18d60 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
18d61 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
18d62 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29  .      if( pBt )
18d63 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
18d64 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
18d65 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 30  tPhaseOne(pBt, 0
18d66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
18d67 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20  ..    /* Do the 
18d68 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61  commit only if a
18d69 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 63  ll databases suc
18d6a 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65  cessfully comple
18d6b 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 20  te phase 1. .   
18d6c 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 68   ** If one of th
18d6d 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e BtreeCommitPha
18d6e 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 61  seOne() calls fa
18d6f 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 61  ils, this indica
18d70 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f  tes an.    ** IO
18d71 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 6c   error while del
18d72 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74  eting or truncat
18d73 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  ing a journal fi
18d74 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b 65  le. It is unlike
18d75 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 63  ly,.    ** but c
18d76 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20  ould happen. In 
18d77 74 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 6f  this case abando
18d78 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64  n processing and
18d79 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   return the erro
18d7a 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  r..    */.    fo
18d7b 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
18d7c 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44  E_OK && i<db->nD
18d7d 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42  b; i++){.      B
18d7e 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
18d7f 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
18d80 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
18d81 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
18d82 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
18d83 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20  eTwo(pBt);.     
18d84 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
18d85 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
18d86 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
18d87 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20  tabCommit(db);. 
18d88 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
18d89 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65 20  he complex case 
18d8a 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d 75 6c  - There is a mul
18d8b 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74 72  ti-file write-tr
18d8c 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76 65  ansaction active
18d8d 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71 75  ..  ** This requ
18d8e 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a 6f  ires a master jo
18d8f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e  urnal file to en
18d90 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 61 63  sure the transac
18d91 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d  tion is.  ** com
18d92 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e  mitted atomicly.
18d93 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
18d94 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
18d95 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c  .  else{.    sql
18d96 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
18d97 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69   db->pVfs;.    i
18d98 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  nt needSync = 0;
18d99 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  .    char *zMast
18d9a 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c  er = 0;   /* Fil
18d9b 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 6d  e-name for the m
18d9c 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f  aster journal */
18d9d 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20  .    char const 
18d9e 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c  *zMainFile = sql
18d9f 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65  ite3BtreeGetFile
18da0 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e  name(db->aDb[0].
18da1 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pBt);.    sqlite
18da2 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 20  3_file *pMaster 
18da3 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f 66 66  = 0;.    i64 off
18da4 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  set = 0;.    int
18da5 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65   res;..    /* Se
18da6 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20 6a 6f  lect a master jo
18da7 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
18da8 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  */.    do {.    
18da9 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b 0a 20    u32 iRandom;. 
18daa 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
18dab 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
18dac 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
18dad 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
18dae 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 61 6e  (iRandom), &iRan
18daf 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d 61 73  dom);.      zMas
18db0 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ter = sqlite3MPr
18db1 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d 6a 25  intf(db, "%s-mj%
18db2 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c  08X", zMainFile,
18db3 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 66 66   iRandom&0x7ffff
18db4 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20  fff);.      if( 
18db5 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  !zMaster ){.    
18db6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
18db7 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
18db8 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
18db9 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73  te3OsAccess(pVfs
18dba 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54  , zMaster, SQLIT
18dbb 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c  E_ACCESS_EXISTS,
18dbc 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 68 69   &res);.    }whi
18dbd 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
18dbe 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20 20 20  K && res );.    
18dbf 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
18dc0 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70  K ){.      /* Op
18dc1 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  en the master jo
18dc2 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20  urnal. */.      
18dc3 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
18dc4 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c 20 7a  enMalloc(pVfs, z
18dc5 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74 65 72  Master, &pMaster
18dc6 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c  , .          SQL
18dc7 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
18dc8 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  TE|SQLITE_OPEN_C
18dc9 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 20  REATE|.         
18dca 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
18dcb 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50  LUSIVE|SQLITE_OP
18dcc 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41  EN_MASTER_JOURNA
18dcd 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 20  L, 0.      );.  
18dce 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
18dcf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
18dd0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
18dd1 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  (db, zMaster);. 
18dd2 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
18dd3 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 57      }. .    /* W
18dd4 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66  rite the name of
18dd5 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66   each database f
18dd6 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61 6e 73  ile in the trans
18dd7 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20  action into the 
18dd8 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65  new.    ** maste
18dd9 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  r journal file. 
18dda 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
18ddb 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74  rs at this point
18ddc 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 61 6e   close.    ** an
18ddd 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73  d delete the mas
18dde 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
18ddf 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 76 69  . All the indivi
18de0 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  dual journal fil
18de1 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20  es.    ** still 
18de2 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 20 74  have 'null' as t
18de3 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
18de4 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68  l pointer, so th
18de5 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20  ey will roll.   
18de6 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e   ** back indepen
18de7 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61 69 6c  dently if a fail
18de8 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 20 20  ure occurs..    
18de9 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
18dea 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
18deb 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
18dec 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
18ded 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d  Bt;.      if( i=
18dee 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20  =1 ) continue;  
18def 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 54   /* Ignore the T
18df0 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  EMP database */.
18df1 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
18df2 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
18df3 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pBt) ){.        
18df4 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c  char const *zFil
18df5 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  e = sqlite3Btree
18df6 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70  GetJournalname(p
18df7 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  Bt);.        if(
18df8 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 63   zFile[0]==0 ) c
18df9 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67 6e  ontinue;  /* Ign
18dfa 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74  ore :memory: dat
18dfb 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 20  abases */.      
18dfc 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e 63 20    if( !needSync 
18dfd 26 26 20 21 73 71 6c 69 74 65 33 42 74 72 65 65  && !sqlite3Btree
18dfe 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70 42 74  SyncDisabled(pBt
18dff 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) ){.          n
18e00 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20  eedSync = 1;.   
18e01 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72       }.        r
18e02 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
18e03 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 69 6c  te(pMaster, zFil
18e04 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
18e05 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66  30(zFile)+1, off
18e06 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66  set);.        of
18e07 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 53  fset += sqlite3S
18e08 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31  trlen30(zFile)+1
18e09 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
18e0a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
18e0b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
18e0c 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73  OsCloseFree(pMas
18e0d 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ter);.          
18e0e 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
18e0f 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30  pVfs, zMaster, 0
18e10 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
18e11 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
18e12 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20  Master);.       
18e13 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
18e14 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
18e15 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79      }..    /* Sy
18e16 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  nc the master jo
18e17 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74  urnal file. If t
18e18 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  he IOCAP_SEQUENT
18e19 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20 20 2a  IAL device.    *
18e1a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68  * flag is set th
18e1b 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  is is not requir
18e1c 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a  ed..    */.    z
18e1d 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74  MainFile = sqlit
18e1e 65 33 42 74 72 65 65 47 65 74 44 69 72 6e 61 6d  e3BtreeGetDirnam
18e1f 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  e(db->aDb[0].pBt
18e20 29 3b 0a 20 20 20 20 69 66 28 20 28 6e 65 65 64  );.    if( (need
18e21 53 79 6e 63 20 0a 20 20 20 20 20 26 26 20 28 30  Sync .     && (0
18e22 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69  ==(sqlite3OsDevi
18e23 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
18e24 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54  s(pMaster)&SQLIT
18e25 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  E_IOCAP_SEQUENTI
18e26 41 4c 29 29 0a 20 20 20 20 20 26 26 20 28 72 63  AL)).     && (rc
18e27 3d 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70  =sqlite3OsSync(p
18e28 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 53  Master, SQLITE_S
18e29 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 21 3d 53 51  YNC_NORMAL))!=SQ
18e2a 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20  LITE_OK) ){.    
18e2b 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
18e2c 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20  Free(pMaster);. 
18e2d 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65       sqlite3OsDe
18e2e 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74  lete(pVfs, zMast
18e2f 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  er, 0);.      sq
18e30 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
18e31 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  zMaster);.      
18e32 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
18e33 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 61 6c  ..    /* Sync al
18e34 6c 20 74 68 65 20 64 62 20 66 69 6c 65 73 20 69  l the db files i
18e35 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 20 74  nvolved in the t
18e36 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20  ransaction. The 
18e37 73 61 6d 65 20 63 61 6c 6c 0a 20 20 20 20 2a 2a  same call.    **
18e38 20 73 65 74 73 20 74 68 65 20 6d 61 73 74 65 72   sets the master
18e39 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72   journal pointer
18e3a 20 69 6e 20 65 61 63 68 20 69 6e 64 69 76 69 64   in each individ
18e3b 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a  ual journal. If.
18e3c 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20      ** an error 
18e3d 6f 63 63 75 72 73 20 68 65 72 65 2c 20 64 6f 20  occurs here, do 
18e3e 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 6d  not delete the m
18e3f 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
18e40 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  le..    **.    *
18e41 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72 20 6f  * If the error o
18e42 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65  ccurs during the
18e43 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 0a 20   first call to. 
18e44 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72     ** sqlite3Btr
18e45 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
18e46 28 29 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69  (), then there i
18e47 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20  s a chance that 
18e48 74 68 65 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65  the.    ** maste
18e49 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77  r journal file w
18e4a 69 6c 6c 20 62 65 20 6f 72 70 68 61 6e 65 64 2e  ill be orphaned.
18e4b 20 42 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 64   But we cannot d
18e4c 65 6c 65 74 65 20 69 74 2c 0a 20 20 20 20 2a 2a  elete it,.    **
18e4d 20 69 6e 20 63 61 73 65 20 74 68 65 20 6d 61 73   in case the mas
18e4e 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
18e4f 20 6e 61 6d 65 20 77 61 73 20 77 72 69 74 74 65   name was writte
18e50 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  n into the journ
18e51 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 62  al.    ** file b
18e52 65 66 6f 72 65 20 74 68 65 20 66 61 69 6c 75 72  efore the failur
18e53 65 20 6f 63 63 75 72 65 64 2e 0a 20 20 20 20 2a  e occured..    *
18e54 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72  /.    for(i=0; r
18e55 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
18e56 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
18e57 20 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70   .      Btree *p
18e58 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
18e59 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  pBt;.      if( p
18e5a 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  Bt ){.        rc
18e5b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
18e5c 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42  ommitPhaseOne(pB
18e5d 74 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  t, zMaster);.   
18e5e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
18e5f 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65  qlite3OsCloseFre
18e60 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  e(pMaster);.    
18e61 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
18e62 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
18e63 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61  e3DbFree(db, zMa
18e64 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74  ster);.      ret
18e65 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20  urn rc;.    }.. 
18e66 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65     /* Delete the
18e67 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
18e68 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 6d 6d 69  file. This commi
18e69 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ts the transacti
18e6a 6f 6e 2e 20 41 66 74 65 72 0a 20 20 20 20 2a 2a  on. After.    **
18e6b 20 64 6f 69 6e 67 20 74 68 69 73 20 74 68 65 20   doing this the 
18e6c 64 69 72 65 63 74 6f 72 79 20 69 73 20 73 79 6e  directory is syn
18e6d 63 65 64 20 61 67 61 69 6e 20 62 65 66 6f 72 65  ced again before
18e6e 20 61 6e 79 20 69 6e 64 69 76 69 64 75 61 6c 0a   any individual.
18e6f 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69      ** transacti
18e70 6f 6e 20 66 69 6c 65 73 20 61 72 65 20 64 65 6c  on files are del
18e71 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eted..    */.   
18e72 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44   rc = sqlite3OsD
18e73 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73  elete(pVfs, zMas
18e74 74 65 72 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  ter, 1);.    sql
18e75 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
18e76 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 7a 4d 61  Master);.    zMa
18e77 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 66  ster = 0;.    if
18e78 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
18e79 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a  turn rc;.    }..
18e7a 20 20 20 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73      /* All files
18e7b 20 61 6e 64 20 64 69 72 65 63 74 6f 72 69 65 73   and directories
18e7c 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
18e7d 65 6e 20 73 79 6e 63 65 64 2c 20 73 6f 20 74 68  en synced, so th
18e7e 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20  e following.    
18e7f 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ** calls to sqli
18e80 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
18e81 61 73 65 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c  aseTwo() are onl
18e82 79 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20  y closing files 
18e83 61 6e 64 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74  and.    ** delet
18e84 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e  ing or truncatin
18e85 67 20 6a 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73  g journals. If s
18e86 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72  omething goes wr
18e87 6f 6e 67 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a  ong while.    **
18e88 20 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69   this is happeni
18e89 6e 67 20 77 65 20 64 6f 6e 27 74 20 72 65 61 6c  ng we don't real
18e8a 6c 79 20 63 61 72 65 2e 20 54 68 65 20 69 6e 74  ly care. The int
18e8b 65 67 72 69 74 79 20 6f 66 20 74 68 65 0a 20 20  egrity of the.  
18e8c 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
18e8d 20 69 73 20 61 6c 72 65 61 64 79 20 67 75 61 72   is already guar
18e8e 61 6e 74 65 65 64 2c 20 62 75 74 20 73 6f 6d 65  anteed, but some
18e8f 20 73 74 72 61 79 20 27 63 6f 6c 64 27 20 6a 6f   stray 'cold' jo
18e90 75 72 6e 61 6c 73 0a 20 20 20 20 2a 2a 20 6d 61  urnals.    ** ma
18e91 79 20 62 65 20 6c 79 69 6e 67 20 61 72 6f 75 6e  y be lying aroun
18e92 64 2e 20 52 65 74 75 72 6e 69 6e 67 20 61 6e 20  d. Returning an 
18e93 65 72 72 6f 72 20 63 6f 64 65 20 77 6f 6e 27 74  error code won't
18e94 20 68 65 6c 70 20 6d 61 74 74 65 72 73 2e 0a 20   help matters.. 
18e95 20 20 20 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c     */.    disabl
18e96 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
18e97 72 72 6f 72 73 28 29 3b 0a 20 20 20 20 73 71 6c  rrors();.    sql
18e98 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
18e99 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 66 6f 72  alloc();.    for
18e9a 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
18e9b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74   i++){ .      Bt
18e9c 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
18e9d 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20  Db[i].pBt;.     
18e9e 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20   if( pBt ){.    
18e9f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
18ea0 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70  CommitPhaseTwo(p
18ea1 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Bt);.      }.   
18ea2 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e   }.    sqlite3En
18ea3 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
18ea4 0a 20 20 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75  .    enable_simu
18ea5 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
18ea6 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
18ea7 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20  tabCommit(db);. 
18ea8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74   }.#endif..  ret
18ea9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  urn rc;.}../* .*
18eaa 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
18eab 68 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 73  hecks that the s
18eac 71 6c 69 74 65 33 2e 61 63 74 69 76 65 56 64 62  qlite3.activeVdb
18ead 65 43 6e 74 20 63 6f 75 6e 74 20 76 61 72 69 61  eCnt count varia
18eae 62 6c 65 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74  ble.** matches t
18eaf 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 64 62  he number of vdb
18eb0 65 27 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20  e's in the list 
18eb1 73 71 6c 69 74 65 33 2e 70 56 64 62 65 20 74 68  sqlite3.pVdbe th
18eb2 61 74 20 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e  at are.** curren
18eb3 74 6c 79 20 61 63 74 69 76 65 2e 20 41 6e 20 61  tly active. An a
18eb4 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 69  ssertion fails i
18eb5 66 20 74 68 65 20 74 77 6f 20 63 6f 75 6e 74 73  f the two counts
18eb6 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a   do not match..*
18eb7 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74  * This is an int
18eb8 65 72 6e 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b  ernal self-check
18eb9 20 6f 6e 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f   only - it is no
18eba 74 20 61 6e 20 65 73 73 65 6e 74 69 61 6c 20 70  t an essential p
18ebb 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 73 74 65  rocessing.** ste
18ebc 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  p..**.** This is
18ebd 20 61 20 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42   a no-op if NDEB
18ebe 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  UG is defined..*
18ebf 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
18ec0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65  .static void che
18ec1 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 28  ckActiveVdbeCnt(
18ec2 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
18ec3 56 64 62 65 20 2a 70 3b 0a 20 20 69 6e 74 20 63  Vdbe *p;.  int c
18ec4 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 57  nt = 0;.  int nW
18ec5 72 69 74 65 20 3d 20 30 3b 0a 20 20 70 20 3d 20  rite = 0;.  p = 
18ec6 64 62 2d 3e 70 56 64 62 65 3b 0a 20 20 77 68 69  db->pVdbe;.  whi
18ec7 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28  le( p ){.    if(
18ec8 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
18ec9 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 70 2d 3e  MAGIC_RUN && p->
18eca 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 63  pc>=0 ){.      c
18ecb 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  nt++;.      if( 
18ecc 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29  p->readOnly==0 )
18ecd 20 6e 57 72 69 74 65 2b 2b 3b 0a 20 20 20 20 7d   nWrite++;.    }
18ece 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78  .    p = p->pNex
18ecf 74 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  t;.  }.  assert(
18ed0 20 63 6e 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65   cnt==db->active
18ed1 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 61 73 73  VdbeCnt );.  ass
18ed2 65 72 74 28 20 6e 57 72 69 74 65 3d 3d 64 62 2d  ert( nWrite==db-
18ed3 3e 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b  >writeVdbeCnt );
18ed4 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  .}.#else.#define
18ed5 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65   checkActiveVdbe
18ed6 43 6e 74 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f  Cnt(x).#endif../
18ed7 2a 0a 2a 2a 20 46 6f 72 20 65 76 65 72 79 20 42  *.** For every B
18ed8 74 72 65 65 20 74 68 61 74 20 69 6e 20 64 61 74  tree that in dat
18ed9 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
18eda 20 64 62 20 77 68 69 63 68 20 0a 2a 2a 20 68 61   db which .** ha
18edb 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2c  s been modified,
18edc 20 22 74 72 69 70 22 20 6f 72 20 69 6e 76 61 6c   "trip" or inval
18edd 69 64 61 74 65 20 65 61 63 68 20 63 75 72 73 6f  idate each curso
18ede 72 20 69 6e 0a 2a 2a 20 74 68 61 74 20 42 74 72  r in.** that Btr
18edf 65 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65  ee might have be
18ee0 65 6e 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 74  en modified so t
18ee1 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a  hat the cursor.*
18ee2 2a 20 63 61 6e 20 6e 65 76 65 72 20 62 65 20 75  * can never be u
18ee3 73 65 64 20 61 67 61 69 6e 2e 20 20 54 68 69 73  sed again.  This
18ee4 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 20   happens when a 
18ee5 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63  rollback.*** occ
18ee6 75 72 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f  urs.  We have to
18ee7 20 74 72 69 70 20 61 6c 6c 20 74 68 65 20 6f 74   trip all the ot
18ee8 68 65 72 20 63 75 72 73 6f 72 73 2c 20 65 76 65  her cursors, eve
18ee9 6e 0a 2a 2a 20 63 75 72 73 6f 72 20 66 72 6f 6d  n.** cursor from
18eea 20 6f 74 68 65 72 20 56 4d 73 20 69 6e 20 64 69   other VMs in di
18eeb 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65  fferent database
18eec 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a   connections,.**
18eed 20 73 6f 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66   so that none of
18eee 20 74 68 65 6d 20 74 72 79 20 74 6f 20 75 73 65   them try to use
18eef 20 74 68 65 20 64 61 74 61 20 61 74 20 77 68 69   the data at whi
18ef0 63 68 20 74 68 65 79 0a 2a 2a 20 77 65 72 65 20  ch they.** were 
18ef1 70 6f 69 6e 74 69 6e 67 20 61 6e 64 20 77 68 69  pointing and whi
18ef2 63 68 20 6e 6f 77 20 6d 61 79 20 68 61 76 65 20  ch now may have 
18ef3 62 65 65 6e 20 63 68 61 6e 67 65 64 20 64 75 65  been changed due
18ef4 0a 2a 2a 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62  .** to the rollb
18ef5 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d  ack..**.** Remem
18ef6 62 65 72 20 74 68 61 74 20 61 20 72 6f 6c 6c 62  ber that a rollb
18ef7 61 63 6b 20 63 61 6e 20 64 65 6c 65 74 65 20 74  ack can delete t
18ef8 61 62 6c 65 73 20 63 6f 6d 70 6c 65 74 65 20 61  ables complete a
18ef9 6e 64 0a 2a 2a 20 72 65 6f 72 64 65 72 20 72 6f  nd.** reorder ro
18efa 6f 74 70 61 67 65 73 2e 20 20 53 6f 20 69 74 20  otpages.  So it 
18efb 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e  is not sufficien
18efc 74 20 6a 75 73 74 20 74 6f 20 73 61 76 65 0a 2a  t just to save.*
18efd 2a 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  * the state of t
18efe 68 65 20 63 75 72 73 6f 72 2e 20 20 57 65 20 68  he cursor.  We h
18eff 61 76 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74  ave to invalidat
18f00 65 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  e the cursor.** 
18f01 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 65  so that it is ne
18f02 76 65 72 20 75 73 65 64 20 61 67 61 69 6e 2e 0a  ver used again..
18f03 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
18f04 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73  nvalidateCursors
18f05 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73  OnModifiedBtrees
18f06 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
18f07 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
18f08 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
18f09 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70  +){.    Btree *p
18f0a 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
18f0b 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20  t;.    if( p && 
18f0c 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
18f0d 54 72 61 6e 73 28 70 29 20 29 7b 0a 20 20 20 20  Trans(p) ){.    
18f0e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72    sqlite3BtreeTr
18f0f 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20  ipAllCursors(p, 
18f10 53 51 4c 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20  SQLITE_ABORT);. 
18f11 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
18f12 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
18f13 73 20 63 61 6c 6c 65 64 20 74 68 65 20 77 68 65  s called the whe
18f14 6e 20 61 20 56 44 42 45 20 74 72 69 65 73 20 74  n a VDBE tries t
18f15 6f 20 68 61 6c 74 2e 20 20 49 66 20 74 68 65 20  o halt.  If the 
18f16 56 44 42 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65  VDBE.** has made
18f17 20 63 68 61 6e 67 65 73 20 61 6e 64 20 69 73 20   changes and is 
18f18 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  in autocommit mo
18f19 64 65 2c 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20  de, then commit 
18f1a 74 68 6f 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73  those.** changes
18f1b 2e 20 20 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b  .  If a rollback
18f1c 20 69 73 20 6e 65 65 64 65 64 2c 20 74 68 65 6e   is needed, then
18f1d 20 64 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   do the rollback
18f1e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
18f1f 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79  tine is the only
18f20 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65   way to move the
18f21 20 73 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66   state of a VM f
18f22 72 6f 6d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41  rom.** SQLITE_MA
18f23 47 49 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54  GIC_RUN to SQLIT
18f24 45 5f 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49  E_MAGIC_HALT.  I
18f25 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f  t is harmless to
18f26 0a 2a 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e  .** call this on
18f27 20 61 20 56 4d 20 74 68 61 74 20 69 73 20 69 6e   a VM that is in
18f28 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49   the SQLITE_MAGI
18f29 43 5f 48 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a  C_HALT state..**
18f2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72  .** Return an er
18f2b 72 6f 72 20 63 6f 64 65 2e 20 20 49 66 20 74 68  ror code.  If th
18f2c 65 20 63 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e  e commit could n
18f2d 6f 74 20 63 6f 6d 70 6c 65 74 65 20 62 65 63 61  ot complete beca
18f2e 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63  use of.** lock c
18f2f 6f 6e 74 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72  ontention, retur
18f30 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  n SQLITE_BUSY.  
18f31 49 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69  If SQLITE_BUSY i
18f32 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a  s returned, it.*
18f33 2a 20 6d 65 61 6e 73 20 74 68 65 20 63 6c 6f 73  * means the clos
18f34 65 20 64 69 64 20 6e 6f 74 20 68 61 70 70 65 6e  e did not happen
18f35 20 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65   and needs to be
18f36 20 72 65 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51   repeated..*/.SQ
18f37 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
18f38 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74   sqlite3VdbeHalt
18f39 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c  (Vdbe *p){.  sql
18f3a 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
18f3b 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
18f3c 20 28 2a 78 46 75 6e 63 29 28 42 74 72 65 65 20   (*xFunc)(Btree 
18f3d 2a 70 42 74 29 20 3d 20 30 3b 20 20 2f 2a 20 46  *pBt) = 0;  /* F
18f3e 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20  unction to call 
18f3f 6f 6e 20 65 61 63 68 20 62 74 72 65 65 20 62 61  on each btree ba
18f40 63 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69  ckend */.  int i
18f41 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b 20 20  sSpecialError;  
18f42 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74            /* Set
18f43 20 74 6f 20 74 72 75 65 20 69 66 20 53 51 4c 49   to true if SQLI
18f44 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 49 4f 45 52  TE_NOMEM or IOER
18f45 52 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20  R */..  /* This 
18f46 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e  function contain
18f47 73 20 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74  s the logic that
18f48 20 64 65 74 65 72 6d 69 6e 65 73 20 69 66 20 61   determines if a
18f49 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20   statement or.  
18f4a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  ** transaction w
18f4b 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64  ill be committed
18f4c 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   or rolled back 
18f4d 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74  as a result of t
18f4e 68 65 0a 20 20 2a 2a 20 65 78 65 63 75 74 69 6f  he.  ** executio
18f4f 6e 20 6f 66 20 74 68 69 73 20 76 69 72 74 75 61  n of this virtua
18f50 6c 20 6d 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a  l machine. .  **
18f51 0a 20 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20  .  ** If any of 
18f52 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72  the following er
18f53 72 6f 72 73 20 6f 63 63 75 72 3a 0a 20 20 2a 2a  rors occur:.  **
18f54 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  .  **     SQLITE
18f55 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a 20 20 20 20 20  _NOMEM.  **     
18f56 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 2a  SQLITE_IOERR.  *
18f57 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 46 55 4c  *     SQLITE_FUL
18f58 4c 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54  L.  **     SQLIT
18f59 45 5f 49 4e 54 45 52 52 55 50 54 0a 20 20 2a 2a  E_INTERRUPT.  **
18f5a 0a 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69  .  ** Then the i
18f5b 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69  nternal cache mi
18f5c 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65  ght have been le
18f5d 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69  ft in an inconsi
18f5e 73 74 65 6e 74 0a 20 20 2a 2a 20 73 74 61 74 65  stent.  ** state
18f5f 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72 6f  .  We need to ro
18f60 6c 6c 62 61 63 6b 20 74 68 65 20 73 74 61 74 65  llback the state
18f61 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
18f62 2c 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20  , if there is.  
18f63 2a 2a 20 6f 6e 65 2c 20 6f 72 20 74 68 65 20 63  ** one, or the c
18f64 6f 6d 70 6c 65 74 65 20 74 72 61 6e 73 61 63 74  omplete transact
18f65 69 6f 6e 20 69 66 20 74 68 65 72 65 20 69 73 20  ion if there is 
18f66 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  no statement tra
18f67 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a  nsaction..  */..
18f68 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c    if( p->db->mal
18f69 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
18f6a 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
18f6b 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 63 6c 6f  NOMEM;.  }.  clo
18f6c 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65  seAllCursorsExce
18f6d 70 74 41 63 74 69 76 65 56 74 61 62 73 28 70 29  ptActiveVtabs(p)
18f6e 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63  ;.  if( p->magic
18f6f 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  !=VDBE_MAGIC_RUN
18f70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
18f71 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
18f72 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43  checkActiveVdbeC
18f73 6e 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f  nt(db);..  /* No
18f74 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62   commit or rollb
18f75 61 63 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68  ack needed if th
18f76 65 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20  e program never 
18f77 73 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28  started */.  if(
18f78 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20   p->pc>=0 ){.   
18f79 20 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50   int mrc;   /* P
18f7a 72 69 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64  rimary error cod
18f7b 65 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a  e from p->rc */.
18f7c 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 61 6c 6c  .    /* Lock all
18f7d 20 62 74 72 65 65 73 20 75 73 65 64 20 62 79 20   btrees used by 
18f7e 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  the statement */
18f7f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
18f80 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72  eMutexArrayEnter
18f81 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 0a 20  (&p->aMutex);.. 
18f82 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20     /* Check for 
18f83 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69  one of the speci
18f84 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20  al errors */.   
18f85 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30   mrc = p->rc & 0
18f86 78 66 66 3b 0a 20 20 20 20 69 73 53 70 65 63 69  xff;.    isSpeci
18f87 61 6c 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53  alError = mrc==S
18f88 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d  QLITE_NOMEM || m
18f89 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
18f8a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
18f8b 20 20 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51        || mrc==SQ
18f8c 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c  LITE_INTERRUPT |
18f8d 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55  | mrc==SQLITE_FU
18f8e 4c 4c 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70  LL;.    if( isSp
18f8f 65 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20  ecialError ){.  
18f90 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75      /* If the qu
18f91 65 72 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c  ery was read-onl
18f92 79 2c 20 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f  y, we need do no
18f93 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c   rollback at all
18f94 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 20  . Otherwise,.   
18f95 20 20 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 69     ** proceed wi
18f96 74 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 68  th the special h
18f97 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a  andling..      *
18f98 2f 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e  /.      if( !p->
18f99 72 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21  readOnly || mrc!
18f9a 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50  =SQLITE_INTERRUP
18f9b 54 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  T ){.        if(
18f9c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 49   p->rc==SQLITE_I
18f9d 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 26 26 20  OERR_BLOCKED && 
18f9e 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e  p->usesStmtJourn
18f9f 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  al ){.          
18fa0 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 42  xFunc = sqlite3B
18fa1 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74  treeRollbackStmt
18fa2 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72  ;.          p->r
18fa3 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
18fa4 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
18fa5 66 28 20 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f  f( (mrc==SQLITE_
18fa6 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51  NOMEM || mrc==SQ
18fa7 4c 49 54 45 5f 46 55 4c 4c 29 0a 20 20 20 20 20  LITE_FULL).     
18fa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26                &&
18fa9 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72   p->usesStmtJour
18faa 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  nal ){.         
18fab 20 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33   xFunc = sqlite3
18fac 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d  BtreeRollbackStm
18fad 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  t;.        }else
18fae 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  {.          /* W
18faf 65 20 61 72 65 20 66 6f 72 63 65 64 20 74 6f 20  e are forced to 
18fb0 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 20 61 63  roll back the ac
18fb1 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  tive transaction
18fb2 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 0a 20  . Before doing. 
18fb3 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20           ** so, 
18fb4 61 62 6f 72 74 20 61 6e 79 20 6f 74 68 65 72 20  abort any other 
18fb5 73 74 61 74 65 6d 65 6e 74 73 20 74 68 69 73 20  statements this 
18fb6 68 61 6e 64 6c 65 20 63 75 72 72 65 6e 74 6c 79  handle currently
18fb7 20 68 61 73 20 61 63 74 69 76 65 2e 0a 20 20 20   has active..   
18fb8 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
18fb9 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75      invalidateCu
18fba 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42  rsorsOnModifiedB
18fbb 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20  trees(db);.     
18fbc 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c       sqlite3Roll
18fbd 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20  backAll(db);.   
18fbe 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c         sqlite3Cl
18fbf 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62  oseSavepoints(db
18fc0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  );.          db-
18fc1 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
18fc2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
18fc3 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
18fc4 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d 63  /* If the auto-c
18fc5 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65  ommit flag is se
18fc6 74 20 61 6e 64 20 74 68 69 73 20 69 73 20 74 68  t and this is th
18fc7 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 76 64  e only active vd
18fc8 62 65 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20  be, then.    ** 
18fc9 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 63  we do either a c
18fca 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63  ommit or rollbac
18fcb 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  k of the current
18fcc 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 20   transaction. . 
18fcd 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74     **.    ** Not
18fce 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 6c  e: This block al
18fcf 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 6f  so runs if one o
18fd0 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 72  f the special er
18fd1 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 20  rors handled .  
18fd2 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 6f    ** above has o
18fd3 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a 2f  ccurred. .    */
18fd4 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
18fd5 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20  3VtabInSync(db) 
18fd6 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 74  .     && db->aut
18fd7 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 26  oCommit .     &&
18fd8 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e   db->writeVdbeCn
18fd9 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d  t==(p->readOnly=
18fda 3d 30 29 20 0a 20 20 20 20 20 26 26 20 28 64 62  =0) .     && (db
18fdb 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
18fdc 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 0a  _CommitBusy)==0.
18fdd 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28      ){.      if(
18fde 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
18fdf 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63  K || (p->errorAc
18fe0 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26  tion==OE_Fail &&
18fe1 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72   !isSpecialError
18fe2 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
18fe3 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  The auto-commit 
18fe4 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61 6e  flag is true, an
18fe5 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72  d the vdbe progr
18fe6 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20 20  am was .        
18fe7 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f 72  ** successful or
18fe8 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c   hit an 'OR FAIL
18fe9 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54 68  ' constraint. Th
18fea 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d 69  is means a commi
18feb 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73  t .        ** is
18fec 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 20   required..     
18fed 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
18fee 74 20 72 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69  t rc = vdbeCommi
18fef 74 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20  t(db, p);.      
18ff0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
18ff1 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20  _BUSY ){.       
18ff2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d     sqlite3BtreeM
18ff3 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26  utexArrayLeave(&
18ff4 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 20  p->aMutex);.    
18ff5 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
18ff6 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
18ff7 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d    }else if( rc!=
18ff8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
18ff9 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72         p->rc = r
18ffa 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  c;.          sql
18ffb 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28  ite3RollbackAll(
18ffc 64 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  db);.        }el
18ffd 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  se{.          sq
18ffe 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72  lite3CommitInter
18fff 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a  nalChanges(db);.
19000 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
19001 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
19002 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
19003 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20  l(db);.      }. 
19004 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 78 46     }else if( !xF
19005 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 69 66 28  unc ){.      if(
19006 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
19007 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74  K || p->errorAct
19008 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a  ion==OE_Fail ){.
19009 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f          if( p->o
1900a 70 65 6e 65 64 53 74 61 74 65 6d 65 6e 74 20 29  penedStatement )
1900b 7b 0a 20 20 20 20 20 20 20 20 20 20 78 46 75 6e  {.          xFun
1900c 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1900d 43 6f 6d 6d 69 74 53 74 6d 74 3b 0a 20 20 20 20  CommitStmt;.    
1900e 20 20 20 20 7d 20 0a 20 20 20 20 20 20 7d 65 6c      } .      }el
1900f 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 41  se if( p->errorA
19010 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 20  ction==OE_Abort 
19011 29 7b 0a 20 20 20 20 20 20 20 20 78 46 75 6e 63  ){.        xFunc
19012 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 52   = sqlite3BtreeR
19013 6f 6c 6c 62 61 63 6b 53 74 6d 74 3b 0a 20 20 20  ollbackStmt;.   
19014 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
19015 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72 73    invalidateCurs
19016 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72  orsOnModifiedBtr
19017 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20  ees(db);.       
19018 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
19019 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20  All(db);.       
1901a 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76   sqlite3CloseSav
1901b 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20  epoints(db);.   
1901c 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d       db->autoCom
1901d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  mit = 1;.      }
1901e 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
1901f 20 49 66 20 78 46 75 6e 63 20 69 73 20 6e 6f 74   If xFunc is not
19020 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69   NULL, then it i
19021 73 20 6f 6e 65 20 6f 66 20 73 71 6c 69 74 65 33  s one of sqlite3
19022 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d  BtreeRollbackStm
19023 74 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69  t or.    ** sqli
19024 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 74  te3BtreeCommitSt
19025 6d 74 2e 20 43 61 6c 6c 20 69 74 20 6f 6e 63 65  mt. Call it once
19026 20 6f 6e 20 65 61 63 68 20 62 61 63 6b 65 6e 64   on each backend
19027 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
19028 63 75 72 73 0a 20 20 20 20 2a 2a 20 61 6e 64 20  curs.    ** and 
19029 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
1902a 69 73 20 73 74 69 6c 6c 20 53 51 4c 49 54 45 5f  is still SQLITE_
1902b 4f 4b 2c 20 73 65 74 20 74 68 65 20 72 65 74 75  OK, set the retu
1902c 72 6e 20 63 6f 64 65 20 74 6f 20 74 68 65 20 6e  rn code to the n
1902d 65 77 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20  ew.    ** error 
1902e 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  value..    */.  
1902f 20 20 61 73 73 65 72 74 28 21 78 46 75 6e 63 20    assert(!xFunc 
19030 7c 7c 0a 20 20 20 20 20 20 78 46 75 6e 63 3d 3d  ||.      xFunc==
19031 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
19032 69 74 53 74 6d 74 20 7c 7c 0a 20 20 20 20 20 20  itStmt ||.      
19033 78 46 75 6e 63 3d 3d 73 71 6c 69 74 65 33 42 74  xFunc==sqlite3Bt
19034 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 0a  reeRollbackStmt.
19035 20 20 20 20 29 3b 0a 20 20 20 20 66 6f 72 28 69      );.    for(i
19036 3d 30 3b 20 78 46 75 6e 63 20 26 26 20 69 3c 64  =0; xFunc && i<d
19037 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20  b->nDb; i++){ . 
19038 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
19039 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
1903a 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1903b 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b        if( pBt ){
1903c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 78 46  .        rc = xF
1903d 75 6e 63 28 70 42 74 29 3b 0a 20 20 20 20 20 20  unc(pBt);.      
1903e 20 20 69 66 28 20 72 63 20 26 26 20 28 70 2d 3e    if( rc && (p->
1903f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
19040 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43   p->rc==SQLITE_C
19041 4f 4e 53 54 52 41 49 4e 54 29 20 29 7b 0a 20 20  ONSTRAINT) ){.  
19042 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20          p->rc = 
19043 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  rc;.          sq
19044 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
19045 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  p->zErrMsg);.   
19046 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73         p->zErrMs
19047 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  g = 0;.        }
19048 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
19049 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73   .    /* If this
1904a 20 77 61 73 20 61 6e 20 49 4e 53 45 52 54 2c 20   was an INSERT, 
1904b 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
1904c 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65   and the stateme
1904d 6e 74 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64  nt was committed
1904e 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68  , .    ** set th
1904f 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  e change counter
19050 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  . .    */.    if
19051 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e  ( p->changeCntOn
19052 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a   && p->pc>=0 ){.
19053 20 20 20 20 20 20 69 66 28 20 21 78 46 75 6e 63        if( !xFunc
19054 20 7c 7c 20 78 46 75 6e 63 3d 3d 73 71 6c 69 74   || xFunc==sqlit
19055 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d  e3BtreeCommitStm
19056 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
19057 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67  ite3VdbeSetChang
19058 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67  es(db, p->nChang
19059 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  e);.      }else{
1905a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1905b 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64  VdbeSetChanges(d
1905c 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  b, 0);.      }. 
1905d 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20       p->nChange 
1905e 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  = 0;.    }.  .  
1905f 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72    /* Rollback or
19060 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65   commit any sche
19061 6d 61 20 63 68 61 6e 67 65 73 20 74 68 61 74 20  ma changes that 
19062 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20  occurred. */.   
19063 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49   if( p->rc!=SQLI
19064 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61  TE_OK && db->fla
19065 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  gs&SQLITE_Intern
19066 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20  Changes ){.     
19067 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
19068 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
19069 30 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c  0);.      db->fl
1906a 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73  ags = (db->flags
1906b 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e   | SQLITE_Intern
1906c 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a  Changes);.    }.
1906d 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20  .    /* Release 
1906e 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20  the locks */.   
1906f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
19070 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d  exArrayLeave(&p-
19071 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20  >aMutex);.  }.. 
19072 20 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63 63   /* We have succ
19073 65 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20  essfully halted 
19074 61 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56  and closed the V
19075 4d 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 20  M.  Record this 
19076 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70  fact. */.  if( p
19077 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64  ->pc>=0 ){.    d
19078 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
19079 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e  --;.    if( !p->
1907a 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20  readOnly ){.    
1907b 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43    db->writeVdbeC
1907c 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nt--;.    }.    
1907d 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69  assert( db->acti
1907e 76 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77  veVdbeCnt>=db->w
1907f 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20  riteVdbeCnt );. 
19080 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20   }.  p->magic = 
19081 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b  VDBE_MAGIC_HALT;
19082 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 64  .  checkActiveVd
19083 62 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28  beCnt(db);.  if(
19084 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
19085 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72  iled ){.    p->r
19086 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
19087 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
19088 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
19089 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68  *.** Each VDBE h
1908a 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 20  olds the result 
1908b 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
1908c 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  nt sqlite3_step(
1908d 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e  ) call.** in p->
1908e 72 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  rc.  This routin
1908f 65 20 73 65 74 73 20 74 68 61 74 20 72 65 73 75  e sets that resu
19090 6c 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54  lt back to SQLIT
19091 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  E_OK..*/.SQLITE_
19092 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19093 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65  ite3VdbeResetSte
19094 70 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29  pResult(Vdbe *p)
19095 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49  {.  p->rc = SQLI
19096 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
19097 43 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45 20  Clean up a VDBE 
19098 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20  after execution 
19099 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74  but do not delet
1909a 65 20 74 68 65 20 56 44 42 45 20 6a 75 73 74 20  e the VDBE just 
1909b 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e  yet..** Write an
1909c 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  y error messages
1909d 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e   into *pzErrMsg.
1909e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73    Return the res
1909f 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ult code..**.** 
190a0 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69  After this routi
190a1 6e 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56  ne is run, the V
190a2 44 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65  DBE should be re
190a3 61 64 79 20 74 6f 20 62 65 20 65 78 65 63 75 74  ady to be execut
190a4 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a  ed.** again..**.
190a5 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74  ** To look at it
190a6 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68   another way, th
190a7 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74  is routine reset
190a8 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  s the state of t
190a9 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61  he.** virtual ma
190aa 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f  chine from VDBE_
190ab 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42  MAGIC_RUN or VDB
190ac 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63  E_MAGIC_HALT bac
190ad 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47  k to.** VDBE_MAG
190ae 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49  IC_INIT..*/.SQLI
190af 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
190b0 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28  qlite3VdbeReset(
190b1 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69  Vdbe *p){.  sqli
190b2 74 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20  te3 *db;.  db = 
190b3 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20  p->db;..  /* If 
190b4 74 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72  the VM did not r
190b5 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e  un to completion
190b6 20 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e   or if it encoun
190b7 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72  tered an.  ** er
190b8 72 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67  ror, then it mig
190b9 68 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e  ht not have been
190ba 20 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79   halted properly
190bb 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20  .  So halt.  ** 
190bc 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28  it now..  */.  (
190bd 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
190be 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69  tyOn(db);.  sqli
190bf 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a  te3VdbeHalt(p);.
190c0 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
190c1 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20  afetyOff(db);.. 
190c2 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45 20   /* If the VDBE 
190c3 68 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20  has be run even 
190c4 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20  partially, then 
190c5 74 72 61 6e 73 66 65 72 20 74 68 65 20 65 72 72  transfer the err
190c6 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64  or code.  ** and
190c7 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66   error message f
190c8 72 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74  rom the VDBE int
190c9 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  o the main datab
190ca 61 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20  ase structure.  
190cb 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20  But.  ** if the 
190cc 56 44 42 45 20 68 61 73 20 6a 75 73 74 20 62 65  VDBE has just be
190cd 65 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75  en set to run bu
190ce 74 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c  t has not actual
190cf 6c 79 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a  ly executed any.
190d0 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e    ** instruction
190d1 73 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65  s yet, leave the
190d2 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 65   main database e
190d3 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  rror information
190d4 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f   unchanged..  */
190d5 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20  .  if( p->pc>=0 
190d6 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45  ){.    if( p->zE
190d7 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73  rrMsg ){.      s
190d8 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
190d9 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  nMalloc();.     
190da 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
190db 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c  Str(db->pErr,-1,
190dc 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54  p->zErrMsg,SQLIT
190dd 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52  E_UTF8,SQLITE_TR
190de 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20  ANSIENT);.      
190df 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
190e0 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20  Malloc();.      
190e1 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d  db->errCode = p-
190e2 3e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  >rc;.      sqlit
190e3 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
190e4 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
190e5 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  p->zErrMsg = 0;.
190e6 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d      }else if( p-
190e7 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  >rc ){.      sql
190e8 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d  ite3Error(db, p-
190e9 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c  >rc, 0);.    }el
190ea 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
190eb 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
190ec 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  E_OK, 0);.    }.
190ed 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72    }else if( p->r
190ee 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20  c && p->expired 
190ef 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78  ){.    /* The ex
190f0 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20 73  pired flag was s
190f1 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62  et on the VDBE b
190f2 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20  efore the first 
190f3 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73  call.    ** to s
190f4 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46  qlite3_step(). F
190f5 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28  or consistency (
190f6 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74  since sqlite3_st
190f7 65 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20  ep() was.    ** 
190f8 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65  called), set the
190f9 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 20   database error 
190fa 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 73 20  in this case as 
190fb 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  well..    */.   
190fc 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
190fd 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20  , p->rc, 0);.   
190fe 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
190ff 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31  Str(db->pErr, -1
19100 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51  , p->zErrMsg, SQ
19101 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
19102 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
19103 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
19104 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, p->zErrMsg);
19105 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  .    p->zErrMsg 
19106 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  = 0;.  }..  /* R
19107 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72  eclaim all memor
19108 79 20 75 73 65 64 20 62 79 20 74 68 65 20 56 44  y used by the VD
19109 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75  BE.  */.  Cleanu
1910a 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65  p(p);..  /* Save
1910b 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72   profiling infor
1910c 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73  mation from this
1910d 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a   VDBE run..  */.
1910e 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1910f 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45  ILE.  {.    FILE
19110 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76   *out = fopen("v
19111 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22  dbe_profile.out"
19112 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20  , "a");.    if( 
19113 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  out ){.      int
19114 20 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   i;.      fprint
19115 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b  f(out, "---- ");
19116 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
19117 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a  i<p->nOp; i++){.
19118 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
19119 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e  out, "%02x", p->
1911a 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a  aOp[i].opcode);.
1911b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70        }.      fp
1911c 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29  rintf(out, "\n")
1911d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
1911e 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
1911f 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66  .        fprintf
19120 28 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c  (out, "%6d %10ll
19121 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20  d %8lld ",.     
19122 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e        p->aOp[i].
19123 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cnt,.           
19124 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73  p->aOp[i].cycles
19125 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ,.           p->
19126 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70  aOp[i].cnt>0 ? p
19127 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f  ->aOp[i].cycles/
19128 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20  p->aOp[i].cnt : 
19129 30 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20  0.        );.   
1912a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1912b 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20  PrintOp(out, i, 
1912c 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20  &p->aOp[i]);.   
1912d 20 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73     }.      fclos
1912e 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20  e(out);.    }.  
1912f 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61  }.#endif.  p->ma
19130 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43  gic = VDBE_MAGIC
19131 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20  _INIT;.  return 
19132 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d  p->rc & db->errM
19133 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43  ask;.}. ./*.** C
19134 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65  lean up and dele
19135 74 65 20 61 20 56 44 42 45 20 61 66 74 65 72 20  te a VDBE after 
19136 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75  execution.  Retu
19137 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68  rn an integer wh
19138 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65  ich is.** the re
19139 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74  sult code.  Writ
1913a 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73  e any error mess
1913b 61 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70  age text into *p
1913c 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49  zErrMsg..*/.SQLI
1913d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1913e 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69  qlite3VdbeFinali
1913f 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69  ze(Vdbe *p){.  i
19140 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
19141 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69  K;.  if( p->magi
19142 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
19143 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  N || p->magic==V
19144 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29  DBE_MAGIC_HALT )
19145 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
19146 65 33 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a  e3VdbeReset(p);.
19147 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63 20      assert( (rc 
19148 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b  & p->db->errMask
19149 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 65 6c 73 65  )==rc );.  }else
1914a 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56   if( p->magic!=V
1914b 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
1914c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1914d 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
1914e 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
1914f 65 74 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ete(p);.  return
19150 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61   rc;.}../*.** Ca
19151 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  ll the destructo
19152 72 20 66 6f 72 20 65 61 63 68 20 61 75 78 64 61  r for each auxda
19153 74 61 20 65 6e 74 72 79 20 69 6e 20 70 56 64 62  ta entry in pVdb
19154 65 46 75 6e 63 20 66 6f 72 20 77 68 69 63 68 0a  eFunc for which.
19155 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ** the correspon
19156 64 69 6e 67 20 62 69 74 20 69 6e 20 6d 61 73 6b  ding bit in mask
19157 20 69 73 20 63 6c 65 61 72 2e 20 20 41 75 78 64   is clear.  Auxd
19158 61 74 61 20 65 6e 74 72 69 65 73 20 62 65 79 6f  ata entries beyo
19159 6e 64 20 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77  nd 31.** are alw
1915a 61 79 73 20 64 65 73 74 72 6f 79 65 64 2e 20 20  ays destroyed.  
1915b 54 6f 20 64 65 73 74 72 6f 79 20 61 6c 6c 20 61  To destroy all a
1915c 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 2c 20  uxdata entries, 
1915d 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75  call this.** rou
1915e 74 69 6e 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d  tine with mask==
1915f 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  0..*/.SQLITE_PRI
19160 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
19161 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61  3VdbeDeleteAuxDa
19162 74 61 28 56 64 62 65 46 75 6e 63 20 2a 70 56 64  ta(VdbeFunc *pVd
19163 62 65 46 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b  beFunc, int mask
19164 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
19165 72 28 69 3d 30 3b 20 69 3c 70 56 64 62 65 46 75  r(i=0; i<pVdbeFu
19166 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a  nc->nAux; i++){.
19167 20 20 20 20 73 74 72 75 63 74 20 41 75 78 44 61      struct AuxDa
19168 74 61 20 2a 70 41 75 78 20 3d 20 26 70 56 64 62  ta *pAux = &pVdb
19169 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b  eFunc->apAux[i];
1916a 0a 20 20 20 20 69 66 28 20 28 69 3e 33 31 20 7c  .    if( (i>31 |
1916b 7c 20 21 28 6d 61 73 6b 26 28 31 3c 3c 69 29 29  | !(mask&(1<<i))
1916c 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75 78 20  ) && pAux->pAux 
1916d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 41 75  ){.      if( pAu
1916e 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20  x->xDelete ){.  
1916f 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44 65 6c        pAux->xDel
19170 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78 29 3b  ete(pAux->pAux);
19171 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
19172 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b 0a 20  Aux->pAux = 0;. 
19173 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
19174 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69  * Delete an enti
19175 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49  re VDBE..*/.SQLI
19176 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19177 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
19178 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  e(Vdbe *p){.  in
19179 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  t i;.  sqlite3 *
1917a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20  db;..  if( p==0 
1917b 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d  ) return;.  db =
1917c 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d   p->db;.  if( p-
1917d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 2d  >pPrev ){.    p-
1917e 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
1917f 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73  p->pNext;.  }els
19180 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  e{.    assert( d
19181 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20  b->pVdbe==p );. 
19182 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70     db->pVdbe = p
19183 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  ->pNext;.  }.  i
19184 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  f( p->pNext ){. 
19185 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72     p->pNext->pPr
19186 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20  ev = p->pPrev;. 
19187 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 20   }.  if( p->aOp 
19188 29 7b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d  ){.    Op *pOp =
19189 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 66 6f 72   p->aOp;.    for
1918a 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20  (i=0; i<p->nOp; 
1918b 69 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20  i++, pOp++){.   
1918c 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f     freeP4(db, pO
1918d 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e  p->p4type, pOp->
1918e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66 20 53 51  p4.p);.#ifdef SQ
1918f 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20  LITE_DEBUG.     
19190 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
19191 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74  b, pOp->zComment
19192 29 3b 0a 23 65 6e 64 69 66 20 20 20 20 20 0a 20  );.#endif     . 
19193 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
19194 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 4f  DbFree(db, p->aO
19195 70 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73  p);.  }.  releas
19196 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 61  eMemArray(p->aVa
19197 72 2c 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 73  r, p->nVar);.  s
19198 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
19199 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 69   p->aLabel);.  i
1919a 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20  f( p->aMem ){.  
1919b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1919c 64 62 2c 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d 29  db, &p->aMem[1])
1919d 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d  ;.  }.  releaseM
1919e 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e  emArray(p->aColN
1919f 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75  ame, p->nResColu
191a0 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20  mn*COLNAME_N);. 
191a1 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
191a2 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b  b, p->aColName);
191a3 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
191a4 28 64 62 2c 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20  (db, p->zSql);. 
191a5 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45   p->magic = VDBE
191a6 5f 4d 41 47 49 43 5f 44 45 41 44 3b 0a 20 20 73  _MAGIC_DEAD;.  s
191a7 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
191a8 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66   p);.}../*.** If
191a9 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61 74   a MoveTo operat
191aa 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20 6f  ion is pending o
191ab 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 73  n the given curs
191ac 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61 74  or, then do that
191ad 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e 20  .** MoveTo now. 
191ae 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
191af 20 63 6f 64 65 2e 20 20 49 66 20 6e 6f 20 4d 6f   code.  If no Mo
191b0 76 65 54 6f 20 69 73 20 70 65 6e 64 69 6e 67 2c  veTo is pending,
191b1 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
191b2 20 64 6f 65 73 20 6e 6f 74 68 69 6e 67 20 61 6e   does nothing an
191b3 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  d returns SQLITE
191b4 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  _OK..*/.SQLITE_P
191b5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
191b6 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
191b7 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 2a 70  to(VdbeCursor *p
191b8 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 66 65  ){.  if( p->defe
191b9 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20  rredMoveto ){.  
191ba 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b 0a 23    int res, rc;.#
191bb 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
191bc 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74  T.    extern int
191bd 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f   sqlite3_search_
191be 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 20  count;.#endif.  
191bf 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73 54    assert( p->isT
191c0 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 20 3d  able );.    rc =
191c1 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
191c2 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d 3e 70  etoUnpacked(p->p
191c3 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d 6f  Cursor, 0, p->mo
191c4 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c 20 26  vetoTarget, 0, &
191c5 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  res);.    if( rc
191c6 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
191c7 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d    p->lastRowid =
191c8 20 6b 65 79 54 6f 49 6e 74 28 70 2d 3e 6d 6f 76   keyToInt(p->mov
191c9 65 74 6f 54 61 72 67 65 74 29 3b 0a 20 20 20 20  etoTarget);.    
191ca 70 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  p->rowidIsValid 
191cb 3d 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20  = res==0 ?1:0;. 
191cc 20 20 20 69 66 28 20 72 65 73 3c 30 20 29 7b 0a     if( res<0 ){.
191cd 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
191ce 65 33 42 74 72 65 65 4e 65 78 74 28 70 2d 3e 70  e3BtreeNext(p->p
191cf 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20  Cursor, &res);. 
191d0 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65       if( rc ) re
191d1 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 23  turn rc;.    }.#
191d2 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
191d3 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65  T.    sqlite3_se
191d4 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  arch_count++;.#e
191d5 6e 64 69 66 0a 20 20 20 20 70 2d 3e 64 65 66 65  ndif.    p->defe
191d6 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a  rredMoveto = 0;.
191d7 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74      p->cacheStat
191d8 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
191d9 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
191da 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20  >pCursor ){.    
191db 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20  int hasMoved;.  
191dc 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
191dd 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73  e3BtreeCursorHas
191de 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f 72  Moved(p->pCursor
191df 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20  , &hasMoved);.  
191e0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
191e1 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 61  n rc;.    if( ha
191e2 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 20  sMoved ){.      
191e3 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  p->cacheStatus =
191e4 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
191e5 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d      p->nullRow =
191e6 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
191e7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
191e8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
191e9 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f  ollowing functio
191ea 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65  ns:.**.** sqlite
191eb 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28  3VdbeSerialType(
191ec 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  ).** sqlite3Vdbe
191ed 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a  SerialTypeLen().
191ee 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  ** sqlite3VdbeSe
191ef 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c  rialLen().** sql
191f0 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75  ite3VdbeSerialPu
191f1 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  t().** sqlite3Vd
191f2 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a  beSerialGet().**
191f3 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20  .** encapsulate 
191f4 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65  the code that se
191f5 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20  rializes values 
191f6 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53  for storage in S
191f7 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e  QLite.** data an
191f8 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e  d index records.
191f9 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64   Each serialized
191fa 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20   value consists 
191fb 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d  of a.** 'serial-
191fc 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62  type' and a blob
191fd 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65   of data. The se
191fe 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20  rial type is an 
191ff 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a  8-byte unsigned.
19200 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72  ** integer, stor
19201 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a  ed as a varint..
19202 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69  **.** In an SQLi
19203 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c  te index record,
19204 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65   the serial type
19205 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63   is stored direc
19206 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68  tly before.** th
19207 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74  e blob of data t
19208 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e  hat it correspon
19209 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c  ds to. In a tabl
1920a 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65  e record, all se
1920b 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72  rial.** types ar
1920c 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20  e stored at the 
1920d 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63  start of the rec
1920e 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f  ord, and the blo
1920f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a  bs of data at.**
19210 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20   the end. Hence 
19211 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  these functions 
19212 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72  allow the caller
19213 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a   to handle the.*
19214 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e  * serial-type an
19215 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65  d data blob sepe
19216 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rately..**.** Th
19217 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
19218 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  e describes the 
19219 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20  various storage 
1921a 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61  classes for data
1921b 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c  :.**.**   serial
1921c 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74   type        byt
1921d 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20  es of data      
1921e 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  type.**   ------
1921f 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d  --------     ---
19220 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20  ------------    
19221 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
19222 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20  **      0       
19223 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
19224 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a             NULL.
19225 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20  **      1       
19226 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
19227 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65             signe
19228 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  d integer.**    
19229 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
1922a 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
1922b 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
1922c 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20  ger.**      3   
1922d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1922e 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73    3            s
1922f 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
19230 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20        4         
19231 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20              4   
19232 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20           signed 
19233 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
19234 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  5               
19235 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20        6         
19236 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65     signed intege
19237 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20  r.**      6     
19238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19239 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67  8            sig
1923a 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  ned integer.**  
1923b 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20      7           
1923c 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20            8     
1923d 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61         IEEE floa
1923e 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20  t.**      8     
1923f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19240 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74  0            Int
19241 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a  eger constant 0.
19242 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20  **      9       
19243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
19244 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67             Integ
19245 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a  er constant 1.**
19246 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20       10,11      
19247 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19248 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65           reserve
19249 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a  d for expansion.
1924a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20  **    N>=12 and 
1924b 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32  even       (N-12
1924c 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a  )/2        BLOB.
1924d 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20  **    N>=13 and 
1924e 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33  odd        (N-13
1924f 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a  )/2        text.
19250 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20  **.** The 8 and 
19251 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64  9 types were add
19252 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c  ed in 3.3.0, fil
19253 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69  e format 4.  Pri
19254 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f  or versions.** o
19255 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f  f SQLite will no
19256 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f  t understand tho
19257 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e  se serial types.
19258 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .*/../*.** Retur
19259 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70  n the serial-typ
1925a 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20  e for the value 
1925b 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a  stored in pMem..
1925c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1925d 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62  E u32 sqlite3Vdb
1925e 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20  eSerialType(Mem 
1925f 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f  *pMem, int file_
19260 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66  format){.  int f
19261 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61  lags = pMem->fla
19262 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20  gs;.  int n;..  
19263 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  if( flags&MEM_Nu
19264 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
19265 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c   0;.  }.  if( fl
19266 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  ags&MEM_Int ){. 
19267 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
19268 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20   whether to use 
19269 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20  1, 2, 4, 6 or 8 
1926a 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65  bytes. */.#   de
1926b 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28  fine MAX_6BYTE (
1926c 28 28 28 69 36 34 29 30 78 30 30 30 30 38 30 30  (((i64)0x0000800
1926d 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69  0)<<32)-1).    i
1926e 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69  64 i = pMem->u.i
1926f 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20  ;.    u64 u;.   
19270 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74   if( file_format
19271 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20  >=4 && (i&1)==i 
19272 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
19273 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a  8+(u32)i;.    }.
19274 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69      u = i<0 ? -i
19275 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 3c   : i;.    if( u<
19276 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 3b  =127 ) return 1;
19277 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 36  .    if( u<=3276
19278 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 20  7 ) return 2;.  
19279 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 37    if( u<=8388607
1927a 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20   ) return 3;.   
1927b 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 36   if( u<=21474836
1927c 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 20  47 ) return 4;. 
1927d 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42     if( u<=MAX_6B
1927e 59 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b 0a  YTE ) return 5;.
1927f 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20      return 6;.  
19280 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45  }.  if( flags&ME
19281 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65  M_Real ){.    re
19282 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73  turn 7;.  }.  as
19283 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e  sert( pMem->db->
19284 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20  mallocFailed || 
19285 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d  flags&(MEM_Str|M
19286 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20  EM_Blob) );.  n 
19287 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28  = pMem->n;.  if(
19288 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72   flags & MEM_Zer
19289 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d  o ){.    n += pM
1928a 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d  em->u.nZero;.  }
1928b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20  .  assert( n>=0 
1928c 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e 2a  );.  return ((n*
1928d 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 67  2) + 12 + ((flag
1928e 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b  s&MEM_Str)!=0));
1928f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
19290 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
19291 68 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f  he data correspo
19292 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70  nding to the sup
19293 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 70  plied serial-typ
19294 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
19295 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
19296 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
19297 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70  n(u32 serial_typ
19298 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 6c  e){.  if( serial
19299 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20  _type>=12 ){.   
1929a 20 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c 5f   return (serial_
1929b 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65  type-12)/2;.  }e
1929c 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20  lse{.    static 
1929d 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b 5d  const u8 aSize[]
1929e 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c   = { 0, 1, 2, 3,
1929f 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20   4, 6, 8, 8, 0, 
192a0 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72  0, 0, 0 };.    r
192a1 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 69  eturn aSize[seri
192a2 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a  al_type];.  }.}.
192a3 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65  ./*.** If we are
192a4 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 74   on an architect
192a5 75 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d 65  ure with mixed-e
192a6 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a  ndian floating .
192a7 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 41  ** points (ex: A
192a8 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 74  RM7) then swap t
192a9 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 73  he lower 4 bytes
192aa 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 70   with the .** up
192ab 70 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 65  per 4 bytes.  Re
192ac 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 2e  turn the result.
192ad 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20  .**.** For most 
192ae 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74  architectures, t
192af 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  his is a no-op..
192b0 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20  **.** (later):  
192b1 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74  It is reported t
192b2 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d 69  o me that the mi
192b3 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c  xed-endian probl
192b4 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73  em.** on ARM7 is
192b5 20 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 47   an issue with G
192b6 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65  CC, not with the
192b7 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 20   ARM7 chip.  It 
192b8 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61  seems.** that ea
192b9 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  rly versions of 
192ba 47 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 74  GCC stored the t
192bb 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 34  wo words of a 64
192bc 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e  -bit.** float in
192bd 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 72   the wrong order
192be 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 6f  .  And that erro
192bf 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 61  r has been propa
192c0 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 69  gated.** ever si
192c1 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 20  nce.  The blame 
192c2 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  is not necessari
192c3 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 6f  ly with GCC, tho
192c4 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68  ugh..** GCC migh
192c5 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 79  t have just copy
192c6 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d 20  ing the problem 
192c7 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d  from a prior com
192c8 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61  piler..** I am a
192c9 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e 65  lso told that ne
192ca 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  wer versions of 
192cb 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20  GCC that follow 
192cc 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41  a different.** A
192cd 42 49 20 67 65 74 20 74 68 65 20 62 79 74 65 20  BI get the byte 
192ce 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a  order right..**.
192cf 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 73  ** Developers us
192d0 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e  ing SQLite on an
192d1 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d   ARM7 should com
192d2 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 65  pile and run the
192d3 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  ir.** applicatio
192d4 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45  n using -DSQLITE
192d5 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 73  _DEBUG=1 at leas
192d6 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 45  t once.  With DE
192d7 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20  BUG.** enabled, 
192d8 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 6c  some asserts bel
192d9 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 74  ow will ensure t
192da 68 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 64  hat the byte ord
192db 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e  er of.** floatin
192dc 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 69  g point values i
192dd 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a  s correct..**.**
192de 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 46   (2007-08-30)  F
192df 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 61  rank van Vugt ha
192e0 73 20 73 74 75 64 69 65 64 20 74 68 69 73 20 70  s studied this p
192e1 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a  roblem closely.*
192e2 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 68  * and has send h
192e3 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 74  is findings to t
192e4 68 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c 6f  he SQLite develo
192e5 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20  pers.  Frank.** 
192e6 77 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d 65  writes that some
192e7 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f   Linux kernels o
192e8 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f  ffer floating po
192e9 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a 20  int hardware.** 
192ea 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 75  emulation that u
192eb 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20  ses only 32-bit 
192ec 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 61  mantissas instea
192ed 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20  d of a full .** 
192ee 34 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 69  48-bits as requi
192ef 72 65 64 20 62 79 20 74 68 65 20 49 45 45 45 20  red by the IEEE 
192f0 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 73  standard.  (This
192f1 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49   is the.** CONFI
192f2 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f 70  G_FPE_FASTFPE op
192f3 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20  tion.)  On such 
192f4 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e  systems, floatin
192f5 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20  g point.** byte 
192f6 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 73  swapping becomes
192f7 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 65   very complicate
192f8 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 6f  d.  To avoid pro
192f9 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65  blems,.** the ne
192fa 63 65 73 73 61 72 79 20 62 79 74 65 20 73 77 61  cessary byte swa
192fb 70 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 64  pping is carried
192fc 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 2d   out using a 64-
192fd 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72  bit integer.** r
192fe 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 2d  ather than a 64-
192ff 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e  bit float.  Fran
19300 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 68 61  k assures us tha
19301 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 0a  t the code here.
19302 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d  ** works for him
19303 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 6c  .  We, the devel
19304 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 77  opers, have no w
19305 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e  ay to independen
19306 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 68  tly.** verify th
19307 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 65  is, but Frank se
19308 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74  ems to know what
19309 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 61   he is talking a
1930a 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72  bout.** so we tr
1930b 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64  ust him..*/.#ifd
1930c 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f  ef SQLITE_MIXED_
1930d 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f  ENDIAN_64BIT_FLO
1930e 41 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 6c  AT.static u64 fl
1930f 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 7b  oatSwap(u64 in){
19310 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75  .  union {.    u
19311 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 5b  64 r;.    u32 i[
19312 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32  2];.  } u;.  u32
19313 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b   t;..  u.r = in;
19314 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20  .  t = u.i[0];. 
19315 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d   u.i[0] = u.i[1]
19316 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a  ;.  u.i[1] = t;.
19317 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a    return u.r;.}.
19318 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78  # define swapMix
19319 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29  edEndianFloat(X)
1931a 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 28    X = floatSwap(
1931b 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  X).#else.# defin
1931c 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61  e swapMixedEndia
1931d 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 66  nFloat(X).#endif
1931e 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  ../*.** Write th
1931f 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74  e serialized dat
19320 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 76  a blob for the v
19321 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70  alue stored in p
19322 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66  Mem into .** buf
19323 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20  . It is assumed 
19324 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20  that the caller 
19325 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 75  has allocated su
19326 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e 0a  fficient space..
19327 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
19328 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72  mber of bytes wr
19329 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75  itten..**.** nBu
1932a 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20  f is the amount 
1932b 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e  of space left in
1932c 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75   buf[].  nBuf mu
1932d 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20  st always be.** 
1932e 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20  large enough to 
1932f 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 20  hold the entire 
19330 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c 20  field.  Except, 
19331 69 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 0a  if the field is.
19332 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 61  ** a blob with a
19333 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69   zero-filled tai
19334 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69  l, then buf[] mi
19335 67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 20  ght be just the 
19336 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f  right.** size to
19337 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67   hold everything
19338 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 20   except for the 
19339 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c  zero-filled tail
1933a 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69  .  If buf[].** i
1933b 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67  s only big enoug
1933c 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f  h to hold the no
1933d 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 74  n-zero prefix, t
1933e 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 74  hen only write t
1933f 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e  hat.** prefix in
19340 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 69  to buf[].  But i
19341 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 65  f buf[] is large
19342 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20   enough to hold 
19343 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 66  both the.** pref
19344 69 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c 20  ix and the tail 
19345 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 70  then write the p
19346 72 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 68  refix and set th
19347 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a  e tail to all.**
19348 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65   zeros..**.** Re
19349 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
1934a 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c  of bytes actuall
1934b 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 62  y written into b
1934c 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65  uf[].  The numbe
1934d 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 6e  r.** of bytes in
1934e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64   the zero-filled
1934f 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 65   tail is include
19350 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 20  d in the return 
19351 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66  value only.** if
19352 20 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 72   those bytes wer
19353 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 5b  e zeroed in buf[
19354 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  ]..*/ .SQLITE_PR
19355 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19356 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75  3VdbeSerialPut(u
19357 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 66  8 *buf, int nBuf
19358 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74  , Mem *pMem, int
19359 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20   file_format){. 
1935a 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
1935b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1935c 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66  rialType(pMem, f
1935d 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 69  ile_format);.  i
1935e 6e 74 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e  nt len;..  /* In
1935f 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 2a  teger and Real *
19360 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74  /.  if( serial_t
19361 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 6c  ype<=7 && serial
19362 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 75  _type>0 ){.    u
19363 36 34 20 76 3b 0a 20 20 20 20 69 6e 74 20 69 3b  64 v;.    int i;
19364 0a 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f  .    if( serial_
19365 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20  type==7 ){.     
19366 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
19367 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d  v)==sizeof(pMem-
19368 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d  >r) );.      mem
19369 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72  cpy(&v, &pMem->r
1936a 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20  , sizeof(v));.  
1936b 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64      swapMixedEnd
1936c 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20  ianFloat(v);.   
1936d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 20   }else{.      v 
1936e 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20  = pMem->u.i;.   
1936f 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d   }.    len = i =
19370 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
19371 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c  alTypeLen(serial
19372 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65  _type);.    asse
19373 72 74 28 20 6c 65 6e 3c 3d 6e 42 75 66 20 29 3b  rt( len<=nBuf );
19374 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20  .    while( i-- 
19375 29 7b 0a 20 20 20 20 20 20 62 75 66 5b 69 5d 20  ){.      buf[i] 
19376 3d 20 28 75 38 29 28 76 26 30 78 46 46 29 3b 0a  = (u8)(v&0xFF);.
19377 20 20 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20        v >>= 8;. 
19378 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
19379 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  len;.  }..  /* S
1937a 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 2a 2f  tring or blob */
1937b 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79  .  if( serial_ty
1937c 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 61 73  pe>=12 ){.    as
1937d 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 20 2b 20  sert( pMem->n + 
1937e 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ((pMem->flags & 
1937f 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d 65 6d 2d 3e  MEM_Zero)?pMem->
19380 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20 20 20 20 20  u.nZero:0).     
19381 20 20 20 20 20 20 20 20 3d 3d 20 73 71 6c 69 74          == sqlit
19382 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
19383 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29  Len(serial_type)
19384 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
19385 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29 3b  pMem->n<=nBuf );
19386 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d  .    len = pMem-
19387 3e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 62  >n;.    memcpy(b
19388 75 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e  uf, pMem->z, len
19389 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d  );.    if( pMem-
1938a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72  >flags & MEM_Zer
1938b 6f 20 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20 2b  o ){.      len +
1938c 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b  = pMem->u.nZero;
1938d 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3e 6e  .      if( len>n
1938e 42 75 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6c  Buf ){.        l
1938f 65 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 20 20 20  en = nBuf;.     
19390 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28   }.      memset(
19391 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30  &buf[pMem->n], 0
19392 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a  , len-pMem->n);.
19393 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
19394 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   len;.  }..  /* 
19395 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74  NULL or constant
19396 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65  s 0 or 1 */.  re
19397 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
19398 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65   Deserialize the
19399 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74   data blob point
1939a 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73 20  ed to by buf as 
1939b 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72 69  serial type seri
1939c 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73  al_type.** and s
1939d 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1939e 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e  in pMem.  Return
1939f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
193a0 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53  ytes read..*/ .S
193a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
193a2 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  t sqlite3VdbeSer
193a3 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20  ialGet(.  const 
193a4 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62  unsigned char *b
193a5 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65  uf,     /* Buffe
193a6 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65  r to deserialize
193a7 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73   from */.  u32 s
193a8 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20  erial_type,     
193a9 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69           /* Seri
193aa 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65 72  al type to deser
193ab 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20  ialize */.  Mem 
193ac 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20  *pMem           
193ad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
193ae 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74  ory cell to writ
193af 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a  e value into */.
193b0 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65 72  ){.  switch( ser
193b1 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20  ial_type ){.    
193b2 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65  case 10:   /* Re
193b3 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72  served for futur
193b4 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73  e use */.    cas
193b5 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72  e 11:   /* Reser
193b6 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75  ved for future u
193b7 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30  se */.    case 0
193b8 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a  : {  /* NULL */.
193b9 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
193ba 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
193bb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
193bc 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f  .    case 1: { /
193bd 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 1-byte signed 
193be 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20  integer */.     
193bf 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69   pMem->u.i = (si
193c0 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d  gned char)buf[0]
193c1 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
193c2 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
193c3 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
193c4 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a     }.    case 2:
193c5 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67   { /* 2-byte sig
193c6 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20  ned integer */. 
193c7 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d       pMem->u.i =
193c8 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 29   (((signed char)
193c9 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75  buf[0])<<8) | bu
193ca 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d  f[1];.      pMem
193cb 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
193cc 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  t;.      return 
193cd 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  2;.    }.    cas
193ce 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65  e 3: { /* 3-byte
193cf 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
193d0 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  */.      pMem->u
193d1 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63  .i = (((signed c
193d2 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29  har)buf[0])<<16)
193d3 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c   | (buf[1]<<8) |
193d4 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70   buf[2];.      p
193d5 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
193d6 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75  _Int;.      retu
193d7 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 3;.    }.    
193d8 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62  case 4: { /* 4-b
193d9 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
193da 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d  er */.      pMem
193db 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c  ->u.i = (buf[0]<
193dc 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c  <24) | (buf[1]<<
193dd 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38  16) | (buf[2]<<8
193de 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20  ) | buf[3];.    
193df 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
193e0 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72  MEM_Int;.      r
193e1 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20  eturn 4;.    }. 
193e2 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20     case 5: { /* 
193e3 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  6-byte signed in
193e4 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75  teger */.      u
193e5 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64  64 x = (((signed
193e6 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38   char)buf[0])<<8
193e7 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20  ) | buf[1];.    
193e8 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32    u32 y = (buf[2
193e9 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d  ]<<24) | (buf[3]
193ea 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c  <<16) | (buf[4]<
193eb 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20  <8) | buf[5];.  
193ec 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20      x = (x<<32) 
193ed 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  | y;.      pMem-
193ee 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78  >u.i = *(i64*)&x
193ef 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
193f0 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
193f1 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20       return 6;. 
193f2 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a     }.    case 6:
193f3 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67     /* 8-byte sig
193f4 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20  ned integer */. 
193f5 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20     case 7: { /* 
193f6 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f  IEEE floating po
193f7 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34  int */.      u64
193f8 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b   x;.      u32 y;
193f9 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
193fa 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65  EBUG) && !define
193fb 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  d(SQLITE_OMIT_FL
193fc 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20  OATING_POINT).  
193fd 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68      /* Verify th
193fe 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20  at integers and 
193ff 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
19400 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73 61  alues use the sa
19401 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65  me.      ** byte
19402 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61   order.  Or, tha
19403 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45  t if SQLITE_MIXE
19404 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46  D_ENDIAN_64BIT_F
19405 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a  LOAT is.      **
19406 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36 34   defined that 64
19407 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f  -bit floating po
19408 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c  int values reall
19409 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20  y are mixed.    
1940a 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20    ** endian..   
1940b 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74     */.      stat
1940c 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20  ic const u64 t1 
1940d 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30 30  = ((u64)0x3ff000
1940e 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73  00)<<32;.      s
1940f 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62  tatic const doub
19410 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20  le r1 = 1.0;.   
19411 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a     u64 t2 = t1;.
19412 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45        swapMixedE
19413 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a  ndianFloat(t2);.
19414 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69        assert( si
19415 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66  zeof(r1)==sizeof
19416 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26  (t2) && memcmp(&
19417 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28  r1, &t2, sizeof(
19418 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69  r1))==0 );.#endi
19419 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75  f..      x = (bu
1941a 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66  f[0]<<24) | (buf
1941b 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b  [1]<<16) | (buf[
1941c 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b  2]<<8) | buf[3];
1941d 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b  .      y = (buf[
1941e 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35  4]<<24) | (buf[5
1941f 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d  ]<<16) | (buf[6]
19420 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20  <<8) | buf[7];. 
19421 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29       x = (x<<32)
19422 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20   | y;.      if( 
19423 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29  serial_type==6 )
19424 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  {.        pMem->
19425 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b  u.i = *(i64*)&x;
19426 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  .        pMem->f
19427 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
19428 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
19429 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a       assert( siz
1942a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a  eof(x)==8 && siz
1942b 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20  eof(pMem->r)==8 
1942c 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d  );.        swapM
1942d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28  ixedEndianFloat(
1942e 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  x);.        memc
1942f 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c  py(&pMem->r, &x,
19430 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20   sizeof(x));.   
19431 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
19432 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28   = sqlite3IsNaN(
19433 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e  pMem->r) ? MEM_N
19434 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a  ull : MEM_Real;.
19435 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
19436 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20  turn 8;.    }.  
19437 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20    case 8:    /* 
19438 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20  Integer 0 */.   
19439 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49   case 9: {  /* I
1943a 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20  nteger 1 */.    
1943b 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65    pMem->u.i = se
1943c 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20  rial_type-8;.   
1943d 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1943e 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1943f 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
19440 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
19441 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28       int len = (
19442 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f  serial_type-12)/
19443 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a  2;.      pMem->z
19444 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a   = (char *)buf;.
19445 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20        pMem->n = 
19446 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  len;.      pMem-
19447 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20  >xDel = 0;.     
19448 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65   if( serial_type
19449 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20  &0x01 ){.       
1944a 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1944b 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68  EM_Str | MEM_Eph
1944c 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  em;.      }else{
1944d 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  .        pMem->f
1944e 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20  lags = MEM_Blob 
1944f 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20  | MEM_Ephem;.   
19450 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
19451 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  n len;.    }.  }
19452 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
19453 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65  ./*.** Given the
19454 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64   nKey-byte encod
19455 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20  ing of a record 
19456 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65  in pKey[], parse
19457 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69   the.** record i
19458 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65  nto a UnpackedRe
19459 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e 20  cord structure. 
1945a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
1945b 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72  r to.** that str
1945c 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
1945d 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
1945e 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65  on might provide
1945f 20 73 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f   szSpace bytes o
19460 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63  f memory.** spac
19461 65 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68  e at pSpace.  Th
19462 69 73 20 73 70 61 63 65 20 63 61 6e 20 62 65 20  is space can be 
19463 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65  used to hold the
19464 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62   returned.** VDb
19465 65 50 61 72 73 65 64 52 65 63 6f 72 64 20 73 74  eParsedRecord st
19466 72 75 63 74 75 72 65 20 69 66 20 69 74 20 69 73  ructure if it is
19467 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20   large enough.  
19468 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20  If it is.** not 
19469 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63  big enough, spac
1946a 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  e is obtained fr
1946b 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
1946c 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  c()..**.** The r
1946d 65 74 75 72 6e 65 64 20 73 74 72 75 63 74 75 72  eturned structur
1946e 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73  e should be clos
1946f 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ed by a call to.
19470 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  ** sqlite3VdbeDe
19471 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f  leteUnpackedReco
19472 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45  rd()..*/ .SQLITE
19473 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65  _PRIVATE Unpacke
19474 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33  dRecord *sqlite3
19475 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b  VdbeRecordUnpack
19476 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  (.  KeyInfo *pKe
19477 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e  yInfo,     /* In
19478 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
19479 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61  the record forma
1947a 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c  t */.  int nKey,
1947b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1947c 20 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e   Size of the bin
1947d 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  ary record */.  
1947e 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
1947f 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69  ,      /* The bi
19480 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20  nary record */. 
19481 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
19482 2a 70 53 70 61 63 65 2c 2f 2a 20 53 70 61 63 65  *pSpace,/* Space
19483 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f   available to ho
19484 6c 64 20 72 65 73 75 6c 74 69 6e 67 20 6f 62 6a  ld resulting obj
19485 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53  ect */.  int szS
19486 70 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20  pace            
19487 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 61 63  /* Size of pSpac
19488 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  e[] in bytes */.
19489 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
1948a 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 20 3d  ned char *aKey =
1948b 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
1948c 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20   char *)pKey;.  
1948d 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
1948e 70 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20  p;.  int nByte, 
1948f 64 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20  d;.  u32 idx;.  
19490 75 31 36 20 75 3b 20 20 20 20 20 20 20 20 20 20  u16 u;          
19491 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e         /* Unsign
19492 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  ed loop counter 
19493 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 0a  */.  u32 szHdr;.
19494 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 0a    Mem *pMem;.  .
19495 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
19496 28 4d 65 6d 29 3e 73 69 7a 65 6f 66 28 2a 70 29  (Mem)>sizeof(*p)
19497 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69   );.  nByte = si
19498 7a 65 6f 66 28 4d 65 6d 29 2a 28 70 4b 65 79 49  zeof(Mem)*(pKeyI
19499 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 32 29 3b 0a  nfo->nField+2);.
1949a 20 20 69 66 28 20 6e 42 79 74 65 3e 73 7a 53 70    if( nByte>szSp
1949b 61 63 65 20 29 7b 0a 20 20 20 20 70 20 3d 20 73  ace ){.    p = s
1949c 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
1949d 77 28 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20  w(pKeyInfo->db, 
1949e 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20  nByte);.    if( 
1949f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
194a0 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20  .    p->flags = 
194a1 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 46 52  UNPACKED_NEED_FR
194a2 45 45 20 7c 20 55 4e 50 41 43 4b 45 44 5f 4e 45  EE | UNPACKED_NE
194a3 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 7d 65  ED_DESTROY;.  }e
194a4 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 70 53 70  lse{.    p = pSp
194a5 61 63 65 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67  ace;.    p->flag
194a6 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45  s = UNPACKED_NEE
194a7 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20  D_DESTROY;.  }. 
194a8 20 70 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70   p->pKeyInfo = p
194a9 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46  KeyInfo;.  p->nF
194aa 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d  ield = pKeyInfo-
194ab 3e 6e 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70  >nField + 1;.  p
194ac 2d 3e 61 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20  ->aMem = pMem = 
194ad 26 28 28 4d 65 6d 2a 29 70 29 5b 31 5d 3b 0a 20  &((Mem*)p)[1];. 
194ae 20 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e 74   idx = getVarint
194af 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29 3b  32(aKey, szHdr);
194b0 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20 20  .  d = szHdr;.  
194b1 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  u = 0;.  while( 
194b2 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c 70  idx<szHdr && u<p
194b3 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20  ->nField ){.    
194b4 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b  u32 serial_type;
194b5 0a 0a 20 20 20 20 69 64 78 20 2b 3d 20 67 65 74  ..    idx += get
194b6 56 61 72 69 6e 74 33 32 28 26 61 4b 65 79 5b 69  Varint32(&aKey[i
194b7 64 78 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65  dx], serial_type
194b8 29 3b 0a 20 20 20 20 69 66 28 20 64 3e 3d 6e 4b  );.    if( d>=nK
194b9 65 79 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  ey && sqlite3Vdb
194ba 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73  eSerialTypeLen(s
194bb 65 72 69 61 6c 5f 74 79 70 65 29 3e 30 20 29 20  erial_type)>0 ) 
194bc 62 72 65 61 6b 3b 0a 20 20 20 20 70 4d 65 6d 2d  break;.    pMem-
194bd 3e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f 2d  >enc = pKeyInfo-
194be 3e 65 6e 63 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  >enc;.    pMem->
194bf 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64  db = pKeyInfo->d
194c0 62 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  b;.    pMem->fla
194c1 67 73 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d  gs = 0;.    pMem
194c2 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  ->zMalloc = 0;. 
194c3 20 20 20 64 20 2b 3d 20 73 71 6c 69 74 65 33 56     d += sqlite3V
194c4 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 4b  dbeSerialGet(&aK
194c5 65 79 5b 64 5d 2c 20 73 65 72 69 61 6c 5f 74 79  ey[d], serial_ty
194c6 70 65 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 70  pe, pMem);.    p
194c7 4d 65 6d 2b 2b 3b 0a 20 20 20 20 75 2b 2b 3b 0a  Mem++;.    u++;.
194c8 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 75 3c    }.  assert( u<
194c9 3d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c  =pKeyInfo->nFiel
194ca 64 20 2b 20 31 20 29 3b 0a 20 20 70 2d 3e 6e 46  d + 1 );.  p->nF
194cb 69 65 6c 64 20 3d 20 75 3b 0a 20 20 72 65 74 75  ield = u;.  retu
194cc 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a  rn (void*)p;.}..
194cd 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
194ce 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 55 6e  ne destroys a Un
194cf 70 61 63 6b 65 64 52 65 63 6f 72 64 20 6f 62 6a  packedRecord obj
194d0 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ect.*/.SQLITE_PR
194d1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
194d2 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61  e3VdbeDeleteUnpa
194d3 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70 61 63  ckedRecord(Unpac
194d4 6b 65 64 52 65 63 6f 72 64 20 2a 70 29 7b 0a 20  kedRecord *p){. 
194d5 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66   if( p ){.    if
194d6 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50  ( p->flags & UNP
194d7 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52  ACKED_NEED_DESTR
194d8 4f 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OY ){.      int 
194d9 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d  i;.      Mem *pM
194da 65 6d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  em;.      for(i=
194db 30 2c 20 70 4d 65 6d 3d 70 2d 3e 61 4d 65 6d 3b  0, pMem=p->aMem;
194dc 20 69 3c 70 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b   i<p->nField; i+
194dd 2b 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20  +, pMem++){.    
194de 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 4d      if( pMem->zM
194df 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20  alloc ){.       
194e0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
194e1 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
194e2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
194e3 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
194e4 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43  p->flags & UNPAC
194e5 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 29 7b  KED_NEED_FREE ){
194e6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
194e7 46 72 65 65 28 70 2d 3e 70 4b 65 79 49 6e 66 6f  Free(p->pKeyInfo
194e8 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 0a  ->db, p);.    }.
194e9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
194ea 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61  s function compa
194eb 72 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c  res the two tabl
194ec 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20  e rows or index 
194ed 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69  records.** speci
194ee 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20  fied by {nKey1, 
194ef 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 79  pKey1} and pPKey
194f0 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 61  2.  It returns a
194f1 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a   negative, zero.
194f2 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69  ** or positive i
194f3 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 69  nteger if key1 i
194f4 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75  s less than, equ
194f5 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65  al to or .** gre
194f6 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e 20  ater than key2. 
194f7 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65   The {nKey1, pKe
194f8 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 20  y1} key must be 
194f9 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65  a blob.** create
194fa 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52  d by th OP_MakeR
194fb 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20  ecord opcode of 
194fc 74 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 70  the VDBE.  The p
194fd 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73  PKey2.** key mus
194fe 74 20 62 65 20 61 20 70 61 72 73 65 64 20 6b 65  t be a parsed ke
194ff 79 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 6e  y such as obtain
19500 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74  ed from.** sqlit
19501 65 33 56 64 62 65 50 61 72 73 65 52 65 63 6f 72  e3VdbeParseRecor
19502 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e  d..**.** Key1 an
19503 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 61  d Key2 do not ha
19504 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  ve to contain th
19505 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66  e same number of
19506 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20   fields..** The 
19507 6b 65 79 20 77 69 74 68 20 66 65 77 65 72 20 66  key with fewer f
19508 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c 79  ields is usually
19509 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 74   compares less t
1950a 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67  han the .** long
1950b 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 72  er key.  However
1950c 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44   if the UNPACKED
1950d 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 69  _INCRKEY flags i
1950e 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 0a  n pPKey2 is set.
1950f 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f  ** and the commo
19510 6e 20 70 72 65 66 69 78 65 73 20 61 72 65 20 65  n prefixes are e
19511 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20  qual, then key1 
19512 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 79  is less than key
19513 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 20  2..** Or if the 
19514 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50  UNPACKED_MATCH_P
19515 52 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 65  REFIX flag is se
19516 74 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 78  t and the prefix
19517 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c  es are.** equal,
19518 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 61   then the keys a
19519 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  re considered to
1951a 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a   be equal and.**
1951b 20 74 68 65 20 70 61 72 74 73 20 62 65 79 6f 6e   the parts beyon
1951c 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65  d the common pre
1951d 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 2e  fix are ignored.
1951e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e  .**.** If the UN
1951f 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f  PACKED_IGNORE_RO
19520 57 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 2c  WID flag is set,
19521 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 62   then the last b
19522 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65  yte of.** the he
19523 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 73  ader of pKey1 is
19524 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 73   ignored.  It is
19525 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 4b   assumed that pK
19526 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64  ey1 is.** an ind
19527 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 73  ex key, and thus
19528 20 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f 77   ends with a row
19529 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 6c  id value.  The l
1952a 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74  ast byte.** of t
1952b 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 74  he header will t
1952c 68 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 20  herefore be the 
1952d 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74  serial type of t
1952e 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65  he rowid:.** one
1952f 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20   of 1, 2, 3, 4, 
19530 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20  5, 6, 8, or 9 - 
19531 74 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 69  the integer seri
19532 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65  al types..** The
19533 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20   serial type of 
19534 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 20  the final rowid 
19535 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 61  will always be a
19536 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a   single byte..**
19537 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 69   By ignoring thi
19538 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 74  s last byte of t
19539 68 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 6f  he header, we fo
1953a 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  rce the comparis
1953b 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20  on.** to ignore 
1953c 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65  the rowid at the
1953d 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f   end of key1..*/
1953e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1953f 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52  int sqlite3VdbeR
19540 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 20  ecordCompare(.  
19541 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74  int nKey1, const
19542 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a   void *pKey1, /*
19543 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55   Left key */.  U
19544 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70  npackedRecord *p
19545 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a 20  PKey2        /* 
19546 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a  Right key */.){.
19547 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 20    int d1;       
19548 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
19549 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65  nto aKey[] of ne
1954a 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20  xt data element 
1954b 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 20  */.  u32 idx1;  
1954c 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
1954d 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66  t into aKey[] of
1954e 20 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c 65   next header ele
1954f 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a  ment */.  u32 sz
19550 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a 20  Hdr1;        /* 
19551 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
19552 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69  in header */.  i
19553 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20  nt i = 0;.  int 
19554 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63  nField;.  int rc
19555 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e   = 0;.  const un
19556 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65  signed char *aKe
19557 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69  y1 = (const unsi
19558 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79  gned char *)pKey
19559 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  1;.  KeyInfo *pK
1955a 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65  eyInfo;.  Mem me
1955b 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20  m1;..  pKeyInfo 
1955c 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e  = pPKey2->pKeyIn
1955d 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d  fo;.  mem1.enc =
1955e 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a   pKeyInfo->enc;.
1955f 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79    mem1.db = pKey
19560 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d 31  Info->db;.  mem1
19561 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 65  .flags = 0;.  me
19562 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  m1.zMalloc = 0;.
19563 20 20 0a 20 20 69 64 78 31 20 3d 20 67 65 74 56    .  idx1 = getV
19564 61 72 69 6e 74 33 32 28 61 4b 65 79 31 2c 20 73  arint32(aKey1, s
19565 7a 48 64 72 31 29 3b 0a 20 20 64 31 20 3d 20 73  zHdr1);.  d1 = s
19566 7a 48 64 72 31 3b 0a 20 20 69 66 28 20 70 50 4b  zHdr1;.  if( pPK
19567 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50  ey2->flags & UNP
19568 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57  ACKED_IGNORE_ROW
19569 49 44 20 29 7b 0a 20 20 20 20 73 7a 48 64 72 31  ID ){.    szHdr1
1956a 2d 2d 3b 0a 20 20 7d 0a 20 20 6e 46 69 65 6c 64  --;.  }.  nField
1956b 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69   = pKeyInfo->nFi
1956c 65 6c 64 3b 0a 20 20 77 68 69 6c 65 28 20 69 64  eld;.  while( id
1956d 78 31 3c 73 7a 48 64 72 31 20 26 26 20 69 3c 70  x1<szHdr1 && i<p
1956e 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 20 29 7b  PKey2->nField ){
1956f 0a 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f  .    u32 serial_
19570 74 79 70 65 31 3b 0a 0a 20 20 20 20 2f 2a 20 52  type1;..    /* R
19571 65 61 64 20 74 68 65 20 73 65 72 69 61 6c 20 74  ead the serial t
19572 79 70 65 73 20 66 6f 72 20 74 68 65 20 6e 65 78  ypes for the nex
19573 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63  t element in eac
19574 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 69 64  h key. */.    id
19575 78 31 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33  x1 += getVarint3
19576 32 28 20 61 4b 65 79 31 2b 69 64 78 31 2c 20 73  2( aKey1+idx1, s
19577 65 72 69 61 6c 5f 74 79 70 65 31 20 29 3b 0a 20  erial_type1 );. 
19578 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b 65 79 31     if( d1>=nKey1
19579 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 53   && sqlite3VdbeS
1957a 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72  erialTypeLen(ser
1957b 69 61 6c 5f 74 79 70 65 31 29 3e 30 20 29 20 62  ial_type1)>0 ) b
1957c 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78  reak;..    /* Ex
1957d 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 73  tract the values
1957e 20 74 6f 20 62 65 20 63 6f 6d 70 61 72 65 64 2e   to be compared.
1957f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 31 20 2b  .    */.    d1 +
19580 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
19581 69 61 6c 47 65 74 28 26 61 4b 65 79 31 5b 64 31  ialGet(&aKey1[d1
19582 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31 2c  ], serial_type1,
19583 20 26 6d 65 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a   &mem1);..    /*
19584 20 44 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73   Do the comparis
19585 6f 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  on.    */.    rc
19586 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d   = sqlite3MemCom
19587 70 61 72 65 28 26 6d 65 6d 31 2c 20 26 70 50 4b  pare(&mem1, &pPK
19588 65 79 32 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20  ey2->aMem[i],.  
19589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1958a 20 20 20 20 20 20 20 20 20 69 3c 6e 46 69 65 6c           i<nFiel
1958b 64 20 3f 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  d ? pKeyInfo->aC
1958c 6f 6c 6c 5b 69 5d 20 3a 20 30 29 3b 0a 20 20 20  oll[i] : 0);.   
1958d 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20   if( rc!=0 ){.  
1958e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1958f 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20  .    i++;.  }.  
19590 69 66 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63  if( mem1.zMalloc
19591 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   ) sqlite3VdbeMe
19592 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b  mRelease(&mem1);
19593 0a 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b  ..  if( rc==0 ){
19594 0a 20 20 20 20 2f 2a 20 72 63 3d 3d 30 20 68 65  .    /* rc==0 he
19595 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e  re means that on
19596 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20 72 61  e of the keys ra
19597 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64 73 20  n out of fields 
19598 61 6e 64 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 74  and.    ** all t
19599 68 65 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20  he fields up to 
1959a 74 68 61 74 20 70 6f 69 6e 74 20 77 65 72 65 20  that point were 
1959b 65 71 75 61 6c 2e 20 49 66 20 74 68 65 20 55 4e  equal. If the UN
1959c 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a 20  PACKED_INCRKEY. 
1959d 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65     ** flag is se
1959e 74 2c 20 74 68 65 6e 20 62 72 65 61 6b 20 74 68  t, then break th
1959f 65 20 74 69 65 20 62 79 20 74 72 65 61 74 69 6e  e tie by treatin
195a0 67 20 6b 65 79 32 20 61 73 20 6c 61 72 67 65 72  g key2 as larger
195a1 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  ..    ** If the 
195a2 55 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d  UPACKED_PREFIX_M
195a3 41 54 43 48 20 66 6c 61 67 20 69 73 20 73 65 74  ATCH flag is set
195a4 2c 20 74 68 65 6e 20 6b 65 79 73 20 77 69 74 68  , then keys with
195a5 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65 73   common prefixes
195a6 0a 20 20 20 20 2a 2a 20 61 72 65 20 63 6f 6e 73  .    ** are cons
195a7 69 64 65 72 65 64 20 74 6f 20 62 65 20 65 71 75  idered to be equ
195a8 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20  al.  Otherwise, 
195a9 74 68 65 20 6c 6f 6e 67 65 72 20 6b 65 79 20 69  the longer key i
195aa 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 6c 61  s the .    ** la
195ab 72 67 65 72 2e 20 20 41 73 20 69 74 20 68 61 70  rger.  As it hap
195ac 70 65 6e 73 2c 20 74 68 65 20 70 50 4b 65 79 32  pens, the pPKey2
195ad 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20   will always be 
195ae 74 68 65 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a  the longer.    *
195af 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  * if there is a 
195b0 64 69 66 66 65 72 65 6e 63 65 2e 0a 20 20 20 20  difference..    
195b1 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 4b 65 79  */.    if( pPKey
195b2 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43  2->flags & UNPAC
195b3 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 7b 0a 20  KED_INCRKEY ){. 
195b4 20 20 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20 20       rc = -1;.  
195b5 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 4b 65    }else if( pPKe
195b6 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41  y2->flags & UNPA
195b7 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43  CKED_PREFIX_MATC
195b8 48 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65  H ){.      /* Le
195b9 61 76 65 20 72 63 3d 3d 30 20 2a 2f 0a 20 20 20  ave rc==0 */.   
195ba 20 7d 65 6c 73 65 20 69 66 28 20 69 64 78 31 3c   }else if( idx1<
195bb 73 7a 48 64 72 31 20 29 7b 0a 20 20 20 20 20 20  szHdr1 ){.      
195bc 72 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  rc = 1;.    }.  
195bd 7d 65 6c 73 65 20 69 66 28 20 70 4b 65 79 49 6e  }else if( pKeyIn
195be 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26  fo->aSortOrder &
195bf 26 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  & i<pKeyInfo->nF
195c0 69 65 6c 64 0a 20 20 20 20 20 20 20 20 20 20 20  ield.           
195c1 20 20 20 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d      && pKeyInfo-
195c2 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29  >aSortOrder[i] )
195c3 7b 0a 20 20 20 20 72 63 20 3d 20 2d 72 63 3b 0a  {.    rc = -rc;.
195c4 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
195c5 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75  ;.}. ../*.** pCu
195c6 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 69  r points at an i
195c7 6e 64 65 78 20 65 6e 74 72 79 20 63 72 65 61 74  ndex entry creat
195c8 65 64 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f  ed using the OP_
195c9 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64  MakeRecord opcod
195ca 65 2e 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 72  e..** Read the r
195cb 6f 77 69 64 20 28 74 68 65 20 6c 61 73 74 20 66  owid (the last f
195cc 69 65 6c 64 20 69 6e 20 74 68 65 20 72 65 63 6f  ield in the reco
195cd 72 64 29 20 61 6e 64 20 73 74 6f 72 65 20 69 74  rd) and store it
195ce 20 69 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52   in *rowid..** R
195cf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
195d0 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f  if everything wo
195d1 72 6b 73 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72  rks, or an error
195d2 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e   code otherwise.
195d3 0a 2a 2a 0a 2a 2a 20 70 43 75 72 20 6d 69 67 68  .**.** pCur migh
195d4 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f  t be pointing to
195d5 20 74 65 78 74 20 6f 62 74 61 69 6e 65 64 20 66   text obtained f
195d6 72 6f 6d 20 61 20 63 6f 72 72 75 70 74 20 64 61  rom a corrupt da
195d7 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20  tabase file..** 
195d8 53 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63  So the content c
195d9 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64  annot be trusted
195da 2e 20 20 44 6f 20 61 70 70 72 6f 70 72 69 61 74  .  Do appropriat
195db 65 20 63 68 65 63 6b 73 20 6f 6e 20 74 68 65 20  e checks on the 
195dc 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  content..*/.SQLI
195dd 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
195de 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77  qlite3VdbeIdxRow
195df 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  id(BtCursor *pCu
195e0 72 2c 20 69 36 34 20 2a 72 6f 77 69 64 29 7b 0a  r, i64 *rowid){.
195e1 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d    i64 nCellKey =
195e2 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20   0;.  int rc;.  
195e3 75 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20 20  u32 szHdr;      
195e4 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
195e5 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32   header */.  u32
195e6 20 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20 2f   typeRowid;    /
195e7 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f 66  * Serial type of
195e8 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20   the rowid */.  
195e9 75 33 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20 20  u32 lenRowid;   
195ea 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
195eb 20 72 6f 77 69 64 20 2a 2f 0a 20 20 4d 65 6d 20   rowid */.  Mem 
195ec 6d 2c 20 76 3b 0a 0a 20 20 2f 2a 20 47 65 74 20  m, v;..  /* Get 
195ed 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
195ee 69 6e 64 65 78 20 65 6e 74 72 79 2e 20 20 4f 6e  index entry.  On
195ef 6c 79 20 69 6e 64 69 63 65 73 20 65 6e 74 72 69  ly indices entri
195f0 65 73 20 6f 66 20 6c 65 73 73 0a 20 20 2a 2a 20  es of less.  ** 
195f1 74 68 61 6e 20 32 47 69 42 20 61 72 65 20 73 75  than 2GiB are su
195f2 70 70 6f 72 74 20 2d 20 61 6e 79 74 68 69 6e 67  pport - anything
195f3 20 6c 61 72 67 65 20 6d 75 73 74 20 62 65 20 64   large must be d
195f4 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
195f5 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42  on */.  sqlite3B
195f6 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72  treeKeySize(pCur
195f7 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20  , &nCellKey);.  
195f8 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 6e 43 65  if( unlikely(nCe
195f9 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65 6c  llKey<=0 || nCel
195fa 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66 29  lKey>0x7fffffff)
195fb 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
195fc 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
195fd 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  PT;.  }..  /* Re
195fe 61 64 20 69 6e 20 74 68 65 20 63 6f 6d 70 6c 65  ad in the comple
195ff 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  te content of th
19600 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f  e index entry */
19601 0a 20 20 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a  .  m.flags = 0;.
19602 20 20 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 6d 2e    m.db = 0;.  m.
19603 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 72  zMalloc = 0;.  r
19604 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
19605 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 75 72  emFromBtree(pCur
19606 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b  , 0, (int)nCellK
19607 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66  ey, 1, &m);.  if
19608 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75  ( rc ){.    retu
19609 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
1960a 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79   The index entry
1960b 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69 74 68   must begin with
1960c 20 61 20 68 65 61 64 65 72 20 73 69 7a 65 20 2a   a header size *
1960d 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72  /.  (void)getVar
1960e 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20  int32((u8*)m.z, 
1960f 73 7a 48 64 72 29 3b 0a 20 20 74 65 73 74 63 61  szHdr);.  testca
19610 73 65 28 20 73 7a 48 64 72 3d 3d 32 20 29 3b 0a  se( szHdr==2 );.
19611 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 48 64    testcase( szHd
19612 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66 28 20  r==m.n );.  if( 
19613 75 6e 6c 69 6b 65 6c 79 28 73 7a 48 64 72 3c 32  unlikely(szHdr<2
19614 20 7c 7c 20 28 69 6e 74 29 73 7a 48 64 72 3e 6d   || (int)szHdr>m
19615 2e 6e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  .n) ){.    goto 
19616 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70  idx_rowid_corrup
19617 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tion;.  }..  /* 
19618 54 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 6f  The last field o
19619 66 20 74 68 65 20 69 6e 64 65 78 20 73 68 6f 75  f the index shou
1961a 6c 64 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72  ld be an integer
1961b 20 2d 20 74 68 65 20 52 4f 57 49 44 2e 0a 20 20   - the ROWID..  
1961c 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  ** Verify that t
1961d 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 72 65  he last entry re
1961e 61 6c 6c 79 20 69 73 20 61 6e 20 69 6e 74 65 67  ally is an integ
1961f 65 72 2e 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67  er. */.  (void)g
19620 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29  etVarint32((u8*)
19621 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74  &m.z[szHdr-1], t
19622 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73  ypeRowid);.  tes
19623 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64  tcase( typeRowid
19624 3d 3d 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==1 );.  testcas
19625 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 32 20  e( typeRowid==2 
19626 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74  );.  testcase( t
19627 79 70 65 52 6f 77 69 64 3d 3d 33 20 29 3b 0a 20  ypeRowid==3 );. 
19628 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52   testcase( typeR
19629 6f 77 69 64 3d 3d 34 20 29 3b 0a 20 20 74 65 73  owid==4 );.  tes
1962a 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64  tcase( typeRowid
1962b 3d 3d 35 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==5 );.  testcas
1962c 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 36 20  e( typeRowid==6 
1962d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74  );.  testcase( t
1962e 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b 0a 20  ypeRowid==8 );. 
1962f 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52   testcase( typeR
19630 6f 77 69 64 3d 3d 39 20 29 3b 0a 20 20 69 66 28  owid==9 );.  if(
19631 20 75 6e 6c 69 6b 65 6c 79 28 74 79 70 65 52 6f   unlikely(typeRo
19632 77 69 64 3c 31 20 7c 7c 20 74 79 70 65 52 6f 77  wid<1 || typeRow
19633 69 64 3e 39 20 7c 7c 20 74 79 70 65 52 6f 77 69  id>9 || typeRowi
19634 64 3d 3d 37 29 20 29 7b 0a 20 20 20 20 67 6f 74  d==7) ){.    got
19635 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72  o idx_rowid_corr
19636 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c 65  uption;.  }.  le
19637 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  nRowid = sqlite3
19638 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
19639 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20  n(typeRowid);.  
1963a 74 65 73 74 63 61 73 65 28 20 6d 2e 6e 2d 6c 65  testcase( m.n-le
1963b 6e 52 6f 77 69 64 3d 3d 73 7a 48 64 72 20 29 3b  nRowid==szHdr );
1963c 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28  .  if( unlikely(
1963d 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 3c 73 7a 48  m.n-lenRowid<szH
1963e 64 72 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  dr) ){.    goto 
1963f 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70  idx_rowid_corrup
19640 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tion;.  }..  /* 
19641 46 65 74 63 68 20 74 68 65 20 69 6e 74 65 67 65  Fetch the intege
19642 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66  r off the end of
19643 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f 72   the index recor
19644 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64  d */.  sqlite3Vd
19645 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a  beSerialGet((u8*
19646 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77  )&m.z[m.n-lenRow
19647 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 2c 20  id], typeRowid, 
19648 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 3d 20  &v);.  *rowid = 
19649 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 65 33  v.u.i;.  sqlite3
1964a 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
1964b 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  m);.  return SQL
1964c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75  ITE_OK;..  /* Ju
1964d 6d 70 20 68 65 72 65 20 69 66 20 64 61 74 61 62  mp here if datab
1964e 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69  ase corruption i
1964f 73 20 64 65 74 65 63 74 65 64 20 61 66 74 65 72  s detected after
19650 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20 2a 2a   m has been.  **
19651 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 72 65   allocated.  Fre
19652 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74 20 61  e the m object a
19653 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
19654 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69 64 78  _CORRUPT. */.idx
19655 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f  _rowid_corruptio
19656 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28 20 6d  n:.  testcase( m
19657 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b 0a 20  .zMalloc!=0 );. 
19658 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19659 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65  elease(&m);.  re
1965a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1965b 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a 0a  UPT_BKPT;.}../*.
1965c 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 6b  ** Compare the k
1965d 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ey of the index 
1965e 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f  entry that curso
1965f 72 20 70 43 20 69 73 20 70 6f 69 6e 74 20 74 6f  r pC is point to
19660 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20   against.** the 
19661 6b 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70 4b  key string in pK
19662 65 79 20 28 6f 66 20 6c 65 6e 67 74 68 20 6e 4b  ey (of length nK
19663 65 79 29 2e 20 20 57 72 69 74 65 20 69 6e 74 6f  ey).  Write into
19664 20 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72 0a   *pRes a number.
19665 2a 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61 74  ** that is negat
19666 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f  ive, zero, or po
19667 73 69 74 69 76 65 20 69 66 20 70 43 20 69 73 20  sitive if pC is 
19668 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c  less than, equal
19669 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 74   to,.** or great
1966a 65 72 20 74 68 61 6e 20 70 4b 65 79 2e 20 20 52  er than pKey.  R
1966b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
1966c 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a  on success..**.*
1966d 2a 20 70 4b 65 79 20 69 73 20 65 69 74 68 65 72  * pKey is either
1966e 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75 74   created without
1966f 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20 74   a rowid or is t
19670 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61 74  runcated so that
19671 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 65   it.** omits the
19672 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e   rowid at the en
19673 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61 74  d.  The rowid at
19674 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
19675 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20 69  index entry.** i
19676 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65 6c  s ignored as wel
19677 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73 20  l.  Hence, this 
19678 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6d  routine only com
19679 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69 78  pares the prefix
1967a 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65  es .** of the ke
1967b 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  ys prior to the 
1967c 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f 74  final rowid, not
1967d 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 2e   the entire key.
1967e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65 64  .**.** pUnpacked
1967f 20 6d 61 79 20 62 65 20 61 6e 20 75 6e 70 61 63   may be an unpac
19680 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70  ked version of p
19681 4b 65 79 2c 6e 4b 65 79 2e 20 20 49 66 20 70 55  Key,nKey.  If pU
19682 6e 70 61 63 6b 65 64 20 69 73 0a 2a 2a 20 73 75  npacked is.** su
19683 70 70 6c 69 65 64 20 69 74 20 69 73 20 75 73 65  pplied it is use
19684 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 70 4b  d in place of pK
19685 65 79 2c 6e 4b 65 79 2e 0a 2a 2f 0a 53 51 4c 49  ey,nKey..*/.SQLI
19686 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19687 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79  qlite3VdbeIdxKey
19688 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65 43  Compare(.  VdbeC
19689 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 20  ursor *pC,      
1968a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75         /* The cu
1968b 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65 20  rsor to compare 
1968c 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e 70  against */.  Unp
1968d 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e  ackedRecord *pUn
1968e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70 61  packed,  /* Unpa
1968f 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  cked version of 
19690 70 4b 65 79 20 61 6e 64 20 6e 4b 65 79 20 2a 2f  pKey and nKey */
19691 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20 20 20  .  int *res     
19692 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19693 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d 70  * Write the comp
19694 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20 68 65  arison result he
19695 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20 6e  re */.){.  i64 n
19696 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69  CellKey = 0;.  i
19697 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 73 6f  nt rc;.  BtCurso
19698 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70 43  r *pCur = pC->pC
19699 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d 3b 0a  ursor;.  Mem m;.
1969a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b  .  sqlite3BtreeK
1969b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e 43  eySize(pCur, &nC
1969c 65 6c 6c 4b 65 79 29 3b 0a 20 20 69 66 28 20 6e  ellKey);.  if( n
1969d 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43  CellKey<=0 || nC
1969e 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66  ellKey>0x7ffffff
1969f 66 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20  f ){.    *res = 
196a0 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
196a1 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6d  LITE_OK;.  }.  m
196a2 2e 64 62 20 3d 20 30 3b 0a 20 20 6d 2e 66 6c 61  .db = 0;.  m.fla
196a3 67 73 20 3d 20 30 3b 0a 20 20 6d 2e 7a 4d 61 6c  gs = 0;.  m.zMal
196a4 6c 6f 63 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20  loc = 0;.  rc = 
196a5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
196a6 6f 6d 42 74 72 65 65 28 70 43 2d 3e 70 43 75 72  omBtree(pC->pCur
196a7 73 6f 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65  sor, 0, (int)nCe
196a8 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20  llKey, 1, &m);. 
196a9 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72   if( rc ){.    r
196aa 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
196ab 61 73 73 65 72 74 28 20 70 55 6e 70 61 63 6b 65  assert( pUnpacke
196ac 64 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43  d->flags & UNPAC
196ad 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44  KED_IGNORE_ROWID
196ae 20 29 3b 0a 20 20 2a 72 65 73 20 3d 20 73 71 6c   );.  *res = sql
196af 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f  ite3VdbeRecordCo
196b0 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c 20  mpare(m.n, m.z, 
196b1 70 55 6e 70 61 63 6b 65 64 29 3b 0a 20 20 73 71  pUnpacked);.  sq
196b2 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
196b3 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72  ase(&m);.  retur
196b4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
196b5 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
196b6 6e 65 20 73 65 74 73 20 74 68 65 20 76 61 6c 75  ne sets the valu
196b7 65 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  e to be returned
196b8 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63   by subsequent c
196b9 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74  alls to.** sqlit
196ba 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 6e 20  e3_changes() on 
196bb 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
196bc 64 6c 65 20 27 64 62 27 2e 20 0a 2a 2f 0a 53 51  dle 'db'. .*/.SQ
196bd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
196be 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
196bf 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20  Changes(sqlite3 
196c0 2a 64 62 2c 20 69 6e 74 20 6e 43 68 61 6e 67 65  *db, int nChange
196c1 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
196c2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
196c3 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
196c4 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 6e 43  db->nChange = nC
196c5 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f  hange;.  db->nTo
196c6 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20 6e 43 68  talChange += nCh
196c7 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  ange;.}../*.** S
196c8 65 74 20 61 20 66 6c 61 67 20 69 6e 20 74 68 65  et a flag in the
196c9 20 76 64 62 65 20 74 6f 20 75 70 64 61 74 65 20   vdbe to update 
196ca 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  the change count
196cb 65 72 20 77 68 65 6e 20 69 74 20 69 73 20 66 69  er when it is fi
196cc 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f 72 20 72 65  nalised.** or re
196cd 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  set..*/.SQLITE_P
196ce 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
196cf 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e  te3VdbeCountChan
196d0 67 65 73 28 56 64 62 65 20 2a 76 29 7b 0a 20 20  ges(Vdbe *v){.  
196d1 76 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20 3d  v->changeCntOn =
196d2 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72   1;.}../*.** Mar
196d3 6b 20 65 76 65 72 79 20 70 72 65 70 61 72 65 64  k every prepared
196d4 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 6f 63   statement assoc
196d5 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74  iated with a dat
196d6 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
196d7 0a 2a 2a 20 61 73 20 65 78 70 69 72 65 64 2e 0a  .** as expired..
196d8 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 69 72 65 64  **.** An expired
196d9 20 73 74 61 74 65 6d 65 6e 74 20 6d 65 61 6e 73   statement means
196da 20 74 68 61 74 20 72 65 63 6f 6d 70 69 6c 61 74   that recompilat
196db 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65  ion of the state
196dc 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 65 63 6f 6d  ment is.** recom
196dd 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d 65 6e 74  mend.  Statement
196de 73 20 65 78 70 69 72 65 20 77 68 65 6e 20 74 68  s expire when th
196df 69 6e 67 73 20 68 61 70 70 65 6e 20 74 68 61 74  ings happen that
196e0 20 6d 61 6b 65 20 74 68 65 69 72 0a 2a 2a 20 70   make their.** p
196e1 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c 65 74 65  rograms obsolete
196e2 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75 73 65 72  .  Removing user
196e3 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
196e4 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6e 67 0a  ns or collating.
196e5 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c 20 6f 72  ** sequences, or
196e6 20 63 68 61 6e 67 69 6e 67 20 61 6e 20 61 75 74   changing an aut
196e7 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74  horization funct
196e8 69 6f 6e 20 61 72 65 20 74 68 65 20 74 79 70 65  ion are the type
196e9 73 20 6f 66 0a 2a 2a 20 74 68 69 6e 67 73 20 74  s of.** things t
196ea 68 61 74 20 6d 61 6b 65 20 70 72 65 70 61 72 65  hat make prepare
196eb 64 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 62 73  d statements obs
196ec 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  olete..*/.SQLITE
196ed 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
196ee 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
196ef 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71  redStatements(sq
196f0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64  lite3 *db){.  Vd
196f1 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28 70 20 3d  be *p;.  for(p =
196f2 20 64 62 2d 3e 70 56 64 62 65 3b 20 70 3b 20 70   db->pVdbe; p; p
196f3 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
196f4 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a  p->expired = 1;.
196f5 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
196f6 75 72 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  urn the database
196f7 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
196f8 20 74 68 65 20 56 64 62 65 2e 0a 2a 2f 0a 53 51   the Vdbe..*/.SQ
196f9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
196fa 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64 62  ite3 *sqlite3Vdb
196fb 65 44 62 28 56 64 62 65 20 2a 76 29 7b 0a 20 20  eDb(Vdbe *v){.  
196fc 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b 0a 7d 0a  return v->db;.}.
196fd 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
196fe 20 45 6e 64 20 6f 66 20 76 64 62 65 61 75 78 2e   End of vdbeaux.
196ff 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
19700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19701 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
19702 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
19703 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
19704 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  api.c **********
19705 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19706 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
19707 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
19708 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  26.**.** The aut
19709 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1970a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1970b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1970c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1970d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1970e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1970f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
19710 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
19711 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
19712 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
19713 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
19714 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
19715 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
19716 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
19717 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
19718 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
19719 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1971a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1971b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1971c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1971d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1971e 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
1971f 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
19720 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  use to implement
19721 20 41 50 49 73 20 74 68 61 74 20 61 72 65 20 70   APIs that are p
19722 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44  art of the.** VD
19723 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76  BE..**.** $Id: v
19724 64 62 65 61 70 69 2e 63 2c 76 20 31 2e 31 35 30  dbeapi.c,v 1.150
19725 20 32 30 30 38 2f 31 32 2f 31 30 20 31 38 3a 30   2008/12/10 18:0
19726 33 3a 34 37 20 64 72 68 20 45 78 70 20 24 0a 2a  3:47 drh Exp $.*
19727 2f 0a 0a 23 69 66 20 30 20 26 26 20 64 65 66 69  /..#if 0 && defi
19728 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
19729 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
1972a 45 4e 54 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ENT)./*.** The f
1972b 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
1972c 72 65 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e  re contains poin
1972d 74 65 72 73 20 74 6f 20 74 68 65 20 65 6e 64 20  ters to the end 
1972e 70 6f 69 6e 74 73 20 6f 66 20 61 0a 2a 2a 20 64  points of a.** d
1972f 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73  oubly-linked lis
19730 74 20 6f 66 20 61 6c 6c 20 63 6f 6d 70 69 6c 65  t of all compile
19731 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  d SQL statements
19732 20 74 68 61 74 20 6d 61 79 20 62 65 20 68 6f 6c   that may be hol
19733 64 69 6e 67 0a 2a 2a 20 62 75 66 66 65 72 73 20  ding.** buffers 
19734 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 72 65 6c  eligible for rel
19735 65 61 73 65 20 77 68 65 6e 20 74 68 65 20 73 71  ease when the sq
19736 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
19737 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61 63 65  mory() interface
19738 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 20   is.** invoked. 
19739 41 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 6c  Access to this l
1973a 69 73 74 20 69 73 20 70 72 6f 74 65 63 74 65 64  ist is protected
1973b 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 4d   by the SQLITE_M
1973c 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32  UTEX_STATIC_LRU2
1973d 0a 2a 2a 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a  .** mutex..**.**
1973e 20 53 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   Statements are 
1973f 61 64 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64  added to the end
19740 20 6f 66 20 74 68 69 73 20 6c 69 73 74 20 77 68   of this list wh
19741 65 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  en sqlite3_reset
19742 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e  () is.** called.
19743 20 54 68 65 79 20 61 72 65 20 72 65 6d 6f 76 65   They are remove
19744 64 20 65 69 74 68 65 72 20 77 68 65 6e 20 73 71  d either when sq
19745 6c 69 74 65 33 5f 73 74 65 70 28 29 20 6f 72 20  lite3_step() or 
19746 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
19747 28 29 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e  ().** is called.
19748 20 57 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 73   When statements
19749 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68   are added to th
1974a 69 73 20 6c 69 73 74 2c 20 74 68 65 20 61 73 73  is list, the ass
1974b 6f 63 69 61 74 65 64 20 0a 2a 2a 20 72 65 67 69  ociated .** regi
1974c 73 74 65 72 20 61 72 72 61 79 20 28 70 2d 3e 61  ster array (p->a
1974d 4d 65 6d 5b 31 2e 2e 70 2d 3e 6e 4d 65 6d 5d 29  Mem[1..p->nMem])
1974e 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 64 79 6e   may contain dyn
1974f 61 6d 69 63 20 62 75 66 66 65 72 73 20 74 68 61  amic buffers tha
19750 74 0a 2a 2a 20 63 61 6e 20 62 65 20 66 72 65 65  t.** can be free
19751 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56  d using sqlite3V
19752 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79  dbeReleaseMemory
19753 28 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 73  ()..**.** When s
19754 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 61 64  tatements are ad
19755 64 65 64 20 6f 72 20 72 65 6d 6f 76 65 64 20 66  ded or removed f
19756 72 6f 6d 20 74 68 69 73 20 6c 69 73 74 2c 20 74  rom this list, t
19757 68 65 20 6d 75 74 65 78 0a 2a 2a 20 61 73 73 6f  he mutex.** asso
19758 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
19759 56 64 62 65 20 62 65 69 6e 67 20 61 64 64 65 64  Vdbe being added
1975a 20 6f 72 20 72 65 6d 6f 76 65 64 20 28 56 64 62   or removed (Vdb
1975b 65 2e 64 62 2d 3e 6d 75 74 65 78 29 20 69 73 0a  e.db->mutex) is.
1975c 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e  ** already held.
1975d 20 54 68 65 20 4c 52 55 32 20 6d 75 74 65 78 20   The LRU2 mutex 
1975e 69 73 20 74 68 65 6e 20 6f 62 74 61 69 6e 65 64  is then obtained
1975f 2c 20 62 6c 6f 63 6b 69 6e 67 20 69 66 20 6e 65  , blocking if ne
19760 63 65 73 73 61 72 79 2c 0a 2a 2a 20 74 68 65 20  cessary,.** the 
19761 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 70 6f 69 6e  linked-list poin
19762 74 65 72 73 20 6d 61 6e 69 70 75 6c 61 74 65 64  ters manipulated
19763 20 61 6e 64 20 74 68 65 20 4c 52 55 32 20 6d 75   and the LRU2 mu
19764 74 65 78 20 72 65 6c 69 6e 71 75 69 73 68 65 64  tex relinquished
19765 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 74 61 74  ..*/.struct Stat
19766 65 6d 65 6e 74 4c 72 75 4c 69 73 74 20 7b 0a 20  ementLruList {. 
19767 20 56 64 62 65 20 2a 70 46 69 72 73 74 3b 0a 20   Vdbe *pFirst;. 
19768 20 56 64 62 65 20 2a 70 4c 61 73 74 3b 0a 7d 3b   Vdbe *pLast;.};
19769 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 53  .static struct S
1976a 74 61 74 65 6d 65 6e 74 4c 72 75 4c 69 73 74 20  tatementLruList 
1976b 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d  sqlite3LruStatem
1976c 65 6e 74 73 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  ents;../*.** Che
1976d 63 6b 20 74 68 61 74 20 74 68 65 20 6c 69 73 74  ck that the list
1976e 20 6c 6f 6f 6b 73 20 74 6f 20 62 65 20 69 6e 74   looks to be int
1976f 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65  ernally consiste
19770 6e 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  nt. This is used
19771 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 61  .** as part of a
19772 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  n assert() state
19773 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ment as follows:
19774 0a 2a 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28  .**.**   assert(
19775 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 20   stmtLruCheck() 
19776 29 3b 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44  );.*/.#ifndef ND
19777 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20  EBUG.static int 
19778 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 7b 0a  stmtLruCheck(){.
19779 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72    Vdbe *p;.  for
1977a 28 70 3d 73 71 6c 69 74 65 33 4c 72 75 53 74 61  (p=sqlite3LruSta
1977b 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 3b 20  tements.pFirst; 
1977c 70 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74  p; p=p->pLruNext
1977d 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70 2d  ){.    assert(p-
1977e 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 3d 3d  >pLruNext || p==
1977f 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d  sqlite3LruStatem
19780 65 6e 74 73 2e 70 4c 61 73 74 29 3b 0a 20 20 20  ents.pLast);.   
19781 20 61 73 73 65 72 74 28 21 70 2d 3e 70 4c 72 75   assert(!p->pLru
19782 4e 65 78 74 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e  Next || p->pLruN
19783 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 70  ext->pLruPrev==p
19784 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 2d  );.    assert(p-
19785 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 3d 3d  >pLruPrev || p==
19786 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d  sqlite3LruStatem
19787 65 6e 74 73 2e 70 46 69 72 73 74 29 3b 0a 20 20  ents.pFirst);.  
19788 20 20 61 73 73 65 72 74 28 21 70 2d 3e 70 4c 72    assert(!p->pLr
19789 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75  uPrev || p->pLru
1978a 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d  Prev->pLruNext==
1978b 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p);.  }.  return
1978c 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a   1;.}.#endif../*
1978d 0a 2a 2a 20 41 64 64 20 76 64 62 65 20 70 20 74  .** Add vdbe p t
1978e 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
1978f 20 73 74 61 74 65 6d 65 6e 74 20 6c 72 75 20 6c   statement lru l
19790 69 73 74 2e 20 49 74 20 69 73 20 61 73 73 75 6d  ist. It is assum
19791 65 64 20 74 68 61 74 0a 2a 2a 20 70 20 69 73 20  ed that.** p is 
19792 6e 6f 74 20 61 6c 72 65 61 64 79 20 70 61 72 74  not already part
19793 20 6f 66 20 74 68 65 20 6c 69 73 74 20 77 68 65   of the list whe
19794 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  n this is called
19795 2e 20 54 68 65 20 6c 72 75 20 6c 69 73 74 0a 2a  . The lru list.*
19796 2a 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62  * is protected b
19797 79 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54  y the SQLITE_MUT
19798 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 6d 75  EX_STATIC_LRU mu
19799 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tex..*/.static v
1979a 6f 69 64 20 73 74 6d 74 4c 72 75 41 64 64 28 56  oid stmtLruAdd(V
1979b 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  dbe *p){.  sqlit
1979c 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73  e3_mutex_enter(s
1979d 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
1979e 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
1979f 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 0a 20 20  ATIC_LRU2));..  
197a0 69 66 28 20 70 2d 3e 70 4c 72 75 50 72 65 76 20  if( p->pLruPrev 
197a1 7c 7c 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c  || p->pLruNext |
197a2 7c 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74  | sqlite3LruStat
197a3 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 3d 3d 70  ements.pFirst==p
197a4 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
197a5 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69  mutex_leave(sqli
197a6 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
197a7 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
197a8 43 5f 4c 52 55 32 29 29 3b 0a 20 20 20 20 72 65  C_LRU2));.    re
197a9 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  turn;.  }..  ass
197aa 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63  ert( stmtLruChec
197ab 6b 28 29 20 29 3b 0a 0a 20 20 69 66 28 20 21 73  k() );..  if( !s
197ac 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65  qlite3LruStateme
197ad 6e 74 73 2e 70 46 69 72 73 74 20 29 7b 0a 20 20  nts.pFirst ){.  
197ae 20 20 61 73 73 65 72 74 28 20 21 73 71 6c 69 74    assert( !sqlit
197af 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e  e3LruStatements.
197b0 70 4c 61 73 74 20 29 3b 0a 20 20 20 20 73 71 6c  pLast );.    sql
197b1 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74  ite3LruStatement
197b2 73 2e 70 46 69 72 73 74 20 3d 20 70 3b 0a 20 20  s.pFirst = p;.  
197b3 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74    sqlite3LruStat
197b4 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20 3d 20 70  ements.pLast = p
197b5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
197b6 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 4c  ssert( !sqlite3L
197b7 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61  ruStatements.pLa
197b8 73 74 2d 3e 70 4c 72 75 4e 65 78 74 20 29 3b 0a  st->pLruNext );.
197b9 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65 76 20      p->pLruPrev 
197ba 3d 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74  = sqlite3LruStat
197bb 65 6d 65 6e 74 73 2e 70 4c 61 73 74 3b 0a 20 20  ements.pLast;.  
197bc 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74    sqlite3LruStat
197bd 65 6d 65 6e 74 73 2e 70 4c 61 73 74 2d 3e 70 4c  ements.pLast->pL
197be 72 75 4e 65 78 74 20 3d 20 70 3b 0a 20 20 20 20  ruNext = p;.    
197bf 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d  sqlite3LruStatem
197c0 65 6e 74 73 2e 70 4c 61 73 74 20 3d 20 70 3b 0a  ents.pLast = p;.
197c1 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 73    }..  assert( s
197c2 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b  tmtLruCheck() );
197c3 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
197c4 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d  x_leave(sqlite3M
197c5 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
197c6 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
197c7 55 32 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  U2));.}../*.** A
197c8 73 73 75 6d 69 6e 67 20 74 68 65 20 53 51 4c 49  ssuming the SQLI
197c9 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
197ca 4c 52 55 32 20 6d 75 74 65 78 74 20 69 73 20 61  LRU2 mutext is a
197cb 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 72 65 6d  lready held, rem
197cc 6f 76 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ove.** statement
197cd 20 70 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 73   p from the leas
197ce 74 2d 72 65 63 65 6e 74 6c 79 2d 75 73 65 64 20  t-recently-used 
197cf 73 74 61 74 65 6d 65 6e 74 20 6c 69 73 74 2e 20  statement list. 
197d0 49 66 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65  If the .** state
197d1 6d 65 6e 74 20 69 73 20 6e 6f 74 20 63 75 72 72  ment is not curr
197d2 65 6e 74 6c 79 20 70 61 72 74 20 6f 66 20 74 68  ently part of th
197d3 65 20 6c 69 73 74 2c 20 74 68 69 73 20 63 61 6c  e list, this cal
197d4 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  l is a no-op..*/
197d5 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 6d  .static void stm
197d6 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65  tLruRemoveNomute
197d7 78 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66  x(Vdbe *p){.  if
197d8 28 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c  ( p->pLruPrev ||
197d9 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20   p->pLruNext || 
197da 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75 53 74 61  p==sqlite3LruSta
197db 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 29  tements.pFirst )
197dc 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74  {.    assert( st
197dd 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a  mtLruCheck() );.
197de 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 4e      if( p->pLruN
197df 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ext ){.      p->
197e0 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72  pLruNext->pLruPr
197e1 65 76 20 3d 20 70 2d 3e 70 4c 72 75 50 72 65 76  ev = p->pLruPrev
197e2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
197e3 20 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61     sqlite3LruSta
197e4 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20 3d 20  tements.pLast = 
197e5 70 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20  p->pLruPrev;.   
197e6 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c   }.    if( p->pL
197e7 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20  ruPrev ){.      
197e8 70 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72  p->pLruPrev->pLr
197e9 75 4e 65 78 74 20 3d 20 70 2d 3e 70 4c 72 75 4e  uNext = p->pLruN
197ea 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ext;.    }else{.
197eb 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c 72 75        sqlite3Lru
197ec 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73  Statements.pFirs
197ed 74 20 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b  t = p->pLruNext;
197ee 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 4c  .    }.    p->pL
197ef 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20  ruNext = 0;.    
197f0 70 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b  p->pLruPrev = 0;
197f1 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 6d  .    assert( stm
197f2 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 20  tLruCheck() );. 
197f3 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75   }.}../*.** Assu
197f4 6d 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f  ming the SQLITE_
197f5 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
197f6 32 20 6d 75 74 65 78 74 20 69 73 20 6e 6f 74 20  2 mutext is not 
197f7 68 65 6c 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20  held, remove.** 
197f8 73 74 61 74 65 6d 65 6e 74 20 70 20 66 72 6f 6d  statement p from
197f9 20 74 68 65 20 6c 65 61 73 74 2d 72 65 63 65 6e   the least-recen
197fa 74 6c 79 2d 75 73 65 64 20 73 74 61 74 65 6d 65  tly-used stateme
197fb 6e 74 20 6c 69 73 74 2e 20 49 66 20 74 68 65 20  nt list. If the 
197fc 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73  .** statement is
197fd 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70   not currently p
197fe 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 74 2c  art of the list,
197ff 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20   this call is a 
19800 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
19801 20 76 6f 69 64 20 73 74 6d 74 4c 72 75 52 65 6d   void stmtLruRem
19802 6f 76 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ove(Vdbe *p){.  
19803 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
19804 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ter(sqlite3Mutex
19805 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
19806 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29  EX_STATIC_LRU2))
19807 3b 0a 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76  ;.  stmtLruRemov
19808 65 4e 6f 6d 75 74 65 78 28 70 29 3b 0a 20 20 73  eNomutex(p);.  s
19809 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1980a 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ve(sqlite3MutexA
1980b 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
1980c 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b  X_STATIC_LRU2));
1980d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f  .}../*.** Try to
1980e 20 72 65 6c 65 61 73 65 20 6e 20 62 79 74 65 73   release n bytes
1980f 20 6f 66 20 6d 65 6d 6f 72 79 20 62 79 20 66 72   of memory by fr
19810 65 65 69 6e 67 20 62 75 66 66 65 72 73 20 61 73  eeing buffers as
19811 73 6f 63 69 61 74 65 64 20 0a 2a 2a 20 77 69 74  sociated .** wit
19812 68 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 67  h the memory reg
19813 69 73 74 65 72 73 20 6f 66 20 63 75 72 72 65 6e  isters of curren
19814 74 6c 79 20 75 6e 75 73 65 64 20 76 64 62 65 73  tly unused vdbes
19815 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19816 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
19817 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79  dbeReleaseMemory
19818 28 69 6e 74 20 6e 29 7b 0a 20 20 56 64 62 65 20  (int n){.  Vdbe 
19819 2a 70 3b 0a 20 20 56 64 62 65 20 2a 70 4e 65 78  *p;.  Vdbe *pNex
1981a 74 3b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d  t;.  int nFree =
1981b 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d   0;..  sqlite3_m
1981c 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
1981d 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
1981e 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
1981f 5f 4c 52 55 32 29 29 3b 0a 20 20 66 6f 72 28 70  _LRU2));.  for(p
19820 3d 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65  =sqlite3LruState
19821 6d 65 6e 74 73 2e 70 46 69 72 73 74 3b 20 70 20  ments.pFirst; p 
19822 26 26 20 6e 46 72 65 65 3c 6e 3b 20 70 3d 70 4e  && nFree<n; p=pN
19823 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 20  ext){.    pNext 
19824 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 0a  = p->pLruNext;..
19825 20 20 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20      /* For each 
19826 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
19827 20 69 6e 20 74 68 65 20 6c 72 75 20 6c 69 73 74   in the lru list
19828 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 74  , attempt to obt
19829 61 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 61  ain the.    ** a
1982a 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61  ssociated databa
1982b 73 65 20 6d 75 74 65 78 2e 20 49 66 20 69 74 20  se mutex. If it 
1982c 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e  cannot be obtain
1982d 65 64 2c 20 63 6f 6e 74 69 6e 75 65 0a 20 20 20  ed, continue.   
1982e 20 2a 2a 20 74 6f 20 74 68 65 20 6e 65 78 74 20   ** to the next 
1982f 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
19830 2e 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  . It is not poss
19831 69 62 6c 65 20 74 6f 20 62 6c 6f 63 6b 20 6f 6e  ible to block on
19832 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61  .    ** the data
19833 62 61 73 65 20 6d 75 74 65 78 20 2d 20 74 68 61  base mutex - tha
19834 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 64 65  t could cause de
19835 61 64 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20  adlock..    */. 
19836 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
19837 3d 3d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ==sqlite3_mutex_
19838 74 72 79 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  try(p->db->mutex
19839 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65  ) ){.      nFree
1983a 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52   += sqlite3VdbeR
1983b 65 6c 65 61 73 65 42 75 66 66 65 72 73 28 70 29  eleaseBuffers(p)
1983c 3b 0a 20 20 20 20 20 20 73 74 6d 74 4c 72 75 52  ;.      stmtLruR
1983d 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 70 29 3b  emoveNomutex(p);
1983e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
1983f 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62  utex_leave(p->db
19840 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a  ->mutex);.    }.
19841 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
19842 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
19843 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
19844 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
19845 4c 52 55 32 29 29 3b 0a 0a 20 20 72 65 74 75 72  LRU2));..  retur
19846 6e 20 6e 46 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  n nFree;.}../*.*
19847 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 52 65  * Call sqlite3Re
19848 70 72 65 70 61 72 65 28 29 20 6f 6e 20 74 68 65  prepare() on the
19849 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 6d 6f   statement. Remo
1984a 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65 0a 2a  ve it from the.*
1984b 2a 20 6c 72 75 20 6c 69 73 74 20 62 65 66 6f 72  * lru list befor
1984c 65 20 64 6f 69 6e 67 20 73 6f 2c 20 61 73 20 52  e doing so, as R
1984d 65 70 72 65 70 61 72 65 28 29 20 77 69 6c 6c 20  eprepare() will 
1984e 66 72 65 65 20 61 6c 6c 20 74 68 65 0a 2a 2a 20  free all the.** 
1984f 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20  memory register 
19850 62 75 66 66 65 72 73 20 61 6e 79 77 61 79 2e 0a  buffers anyway..
19851 2a 2f 0a 69 6e 74 20 76 64 62 65 52 65 70 72 65  */.int vdbeRepre
19852 70 61 72 65 28 56 64 62 65 20 2a 70 29 7b 0a 20  pare(Vdbe *p){. 
19853 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 70   stmtLruRemove(p
19854 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
19855 74 65 33 52 65 70 72 65 70 61 72 65 28 70 29 3b  te3Reprepare(p);
19856 0a 7d 0a 0a 23 65 6c 73 65 20 20 20 20 20 20 20  .}..#else       
19857 2f 2a 20 21 53 51 4c 49 54 45 5f 45 4e 41 42 4c  /* !SQLITE_ENABL
19858 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
19859 45 4e 54 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65  ENT */.  #define
1985a 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 78   stmtLruRemove(x
1985b 29 0a 20 20 23 64 65 66 69 6e 65 20 73 74 6d 74  ).  #define stmt
1985c 4c 72 75 41 64 64 28 78 29 0a 20 20 23 64 65 66  LruAdd(x).  #def
1985d 69 6e 65 20 76 64 62 65 52 65 70 72 65 70 61 72  ine vdbeReprepar
1985e 65 28 78 29 20 73 71 6c 69 74 65 33 52 65 70 72  e(x) sqlite3Repr
1985f 65 70 61 72 65 28 78 29 0a 23 65 6e 64 69 66 0a  epare(x).#endif.
19860 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
19861 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
19862 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
19863 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66  UE (non-zero) of
19864 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73   the statement s
19865 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72  upplied as an ar
19866 67 75 6d 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20  gument needs.** 
19867 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64  to be recompiled
19868 2e 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 6e  .  A statement n
19869 65 65 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d  eeds to be recom
1986a 70 69 6c 65 64 20 77 68 65 6e 65 76 65 72 20 74  piled whenever t
1986b 68 65 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20  he.** execution 
1986c 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e  environment chan
1986d 67 65 73 20 69 6e 20 61 20 77 61 79 20 74 68 61  ges in a way tha
1986e 74 20 77 6f 75 6c 64 20 61 6c 74 65 72 20 74 68  t would alter th
1986f 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61  e program.** tha
19870 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
19871 65 28 29 20 67 65 6e 65 72 61 74 65 73 2e 20 20  e() generates.  
19872 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
19873 6e 65 77 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72  new functions or
19874 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  .** collating se
19875 71 75 65 6e 63 65 73 20 61 72 65 20 72 65 67 69  quences are regi
19876 73 74 65 72 65 64 20 6f 72 20 69 66 20 61 6e 20  stered or if an 
19877 61 75 74 68 6f 72 69 7a 65 72 20 66 75 6e 63 74  authorizer funct
19878 69 6f 6e 20 69 73 0a 2a 2a 20 61 64 64 65 64 20  ion is.** added 
19879 6f 72 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53  or changed..*/.S
1987a 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1987b 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28 73 71  lite3_expired(sq
1987c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1987d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  t){.  Vdbe *p = 
1987e 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
1987f 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70  return p==0 || p
19880 2d 3e 65 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e  ->expired;.}.#en
19881 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
19882 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
19883 20 64 65 73 74 72 6f 79 73 20 61 20 76 69 72 74   destroys a virt
19884 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74  ual machine that
19885 20 69 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a   is created by.*
19886 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  * the sqlite3_co
19887 6d 70 69 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e  mpile() routine.
19888 20 54 68 65 20 69 6e 74 65 67 65 72 20 72 65 74   The integer ret
19889 75 72 6e 65 64 20 69 73 20 61 6e 20 53 51 4c 49  urned is an SQLI
1988a 54 45 5f 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66  TE_.** success/f
1988b 61 69 6c 75 72 65 20 63 6f 64 65 20 74 68 61 74  ailure code that
1988c 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 72   describes the r
1988d 65 73 75 6c 74 20 6f 66 20 65 78 65 63 75 74 69  esult of executi
1988e 6e 67 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  ng the virtual.*
1988f 2a 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a  * machine..**.**
19890 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
19891 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ts the error cod
19892 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74  e and string ret
19893 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69  urned by.** sqli
19894 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73  te3_errcode(), s
19895 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20  qlite3_errmsg() 
19896 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  and sqlite3_errm
19897 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  sg16()..*/.SQLIT
19898 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
19899 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  3_finalize(sqlit
1989a 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1989b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
1989c 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20   pStmt==0 ){.   
1989d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1989e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64  .  }else{.    Vd
1989f 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70  be *v = (Vdbe*)p
198a0 53 74 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54 45  Stmt;.#if SQLITE
198a1 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20  _THREADSAFE.    
198a2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
198a3 75 74 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75  utex = v->db->mu
198a4 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  tex;.#endif.    
198a5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
198a6 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20  ter(mutex);.    
198a7 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 76 29  stmtLruRemove(v)
198a8 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
198a9 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76  e3VdbeFinalize(v
198aa 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
198ab 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
198ac 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
198ad 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72  rc;.}../*.** Ter
198ae 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 65  minate the curre
198af 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  nt execution of 
198b0 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
198b1 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a   and reset it.**
198b2 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61   back to its sta
198b3 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74  rting state so t
198b4 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65  hat it can be re
198b5 75 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 20  used. A success 
198b6 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  code from.** the
198b7 20 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e   prior execution
198b8 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
198b9 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
198ba 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20   sets the error 
198bb 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20  code and string 
198bc 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
198bd 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
198be 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
198bf 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65  () and sqlite3_e
198c0 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51  rrmsg16()..*/.SQ
198c1 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
198c2 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74  ite3_reset(sqlit
198c3 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
198c4 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
198c5 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20   pStmt==0 ){.   
198c6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
198c7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64  .  }else{.    Vd
198c8 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70  be *v = (Vdbe*)p
198c9 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Stmt;.    sqlite
198ca 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 2d  3_mutex_enter(v-
198cb 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
198cc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
198cd 65 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20 73  eReset(v);.    s
198ce 74 6d 74 4c 72 75 41 64 64 28 76 29 3b 0a 20 20  tmtLruAdd(v);.  
198cf 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b    sqlite3VdbeMak
198d0 65 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c  eReady(v, -1, 0,
198d1 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65   0, 0);.    asse
198d2 72 74 28 20 28 72 63 20 26 20 28 76 2d 3e 64 62  rt( (rc & (v->db
198d3 2d 3e 65 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20  ->errMask))==rc 
198d4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
198d5 75 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62  utex_leave(v->db
198d6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
198d7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
198d8 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20  .** Set all the 
198d9 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68  parameters in th
198da 65 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73  e compiled SQL s
198db 74 61 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c  tatement to NULL
198dc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
198dd 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61  int sqlite3_clea
198de 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  r_bindings(sqlit
198df 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
198e0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
198e1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
198e2 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
198e3 65 2a 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51  e*)pStmt;.#if SQ
198e4 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
198e5 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
198e6 2a 6d 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a  *mutex = ((Vdbe*
198e7 29 70 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74  )pStmt)->db->mut
198e8 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c  ex;.#endif.  sql
198e9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
198ea 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69  (mutex);.  for(i
198eb 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69  =0; i<p->nVar; i
198ec 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
198ed 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
198ee 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20  p->aVar[i]);.   
198ef 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67   p->aVar[i].flag
198f0 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
198f1 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
198f2 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
198f3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
198f4 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
198f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73  ************** s
198f6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a  qlite3_value_  *
198f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
198f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
198f9 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
198fa 72 6f 75 74 69 6e 65 73 20 65 78 74 72 61 63 74  routines extract
198fb 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
198fc 6d 20 61 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74  m a Mem or sqlit
198fd 65 33 5f 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75  e3_value.** stru
198fe 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
198ff 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
19900 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  *sqlite3_value_b
19901 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  lob(sqlite3_valu
19902 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20  e *pVal){.  Mem 
19903 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b  *p = (Mem*)pVal;
19904 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  .  if( p->flags 
19905 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f  & (MEM_Blob|MEM_
19906 53 74 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  Str) ){.    sqli
19907 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
19908 42 6c 6f 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e  Blob(p);.    p->
19909 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74  flags &= ~MEM_St
1990a 72 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  r;.    p->flags 
1990b 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20  |= MEM_Blob;.   
1990c 20 72 65 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20   return p->z;.  
1990d 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
1990e 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  n sqlite3_value_
1990f 74 65 78 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a  text(pVal);.  }.
19910 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
19911 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
19912 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c  ytes(sqlite3_val
19913 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue *pVal){.  ret
19914 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65  urn sqlite3Value
19915 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49  Bytes(pVal, SQLI
19916 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49  TE_UTF8);.}.SQLI
19917 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
19918 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36  e3_value_bytes16
19919 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1991a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
1991b 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65  sqlite3ValueByte
1991c 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55  s(pVal, SQLITE_U
1991d 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53  TF16NATIVE);.}.S
1991e 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
1991f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
19920 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61  ouble(sqlite3_va
19921 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65  lue *pVal){.  re
19922 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65  turn sqlite3Vdbe
19923 52 65 61 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29  RealValue((Mem*)
19924 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pVal);.}.SQLITE_
19925 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
19926 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65  value_int(sqlite
19927 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
19928 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 73 71    return (int)sq
19929 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1992a 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d  e((Mem*)pVal);.}
1992b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1992c 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  te_int64 sqlite3
1992d 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c  _value_int64(sql
1992e 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
1992f 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
19930 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
19931 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53  (Mem*)pVal);.}.S
19932 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
19933 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73  unsigned char *s
19934 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
19935 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t(sqlite3_value 
19936 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
19937 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
19938 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 56   char *)sqlite3V
19939 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53  alueText(pVal, S
1993a 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23  QLITE_UTF8);.}.#
1993b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1993c 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1993d 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1993e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1993f 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c  xt16(sqlite3_val
19940 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue* pVal){.  ret
19941 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65  urn sqlite3Value
19942 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
19943 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a  E_UTF16NATIVE);.
19944 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  }.SQLITE_API con
19945 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
19946 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28  _value_text16be(
19947 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
19948 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
19949 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
1994a 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
1994b 31 36 42 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  16BE);.}.SQLITE_
1994c 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1994d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1994e 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76  xt16le(sqlite3_v
1994f 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
19950 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c  eturn sqlite3Val
19951 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c  ueText(pVal, SQL
19952 49 54 45 5f 55 54 46 31 36 4c 45 29 3b 0a 7d 0a  ITE_UTF16LE);.}.
19953 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
19954 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53  _OMIT_UTF16 */.S
19955 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
19956 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
19957 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20  (sqlite3_value* 
19958 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
19959 70 56 61 6c 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f  pVal->type;.}../
1995a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1995b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c  ************ sql
1995c 69 74 65 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a  ite3_result_  **
1995d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1995e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1995f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
19960 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
19961 20 62 79 20 75 73 65 72 2d 64 65 66 69 6e 65 64   by user-defined
19962 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70   functions to sp
19963 65 63 69 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e  ecify.** the fun
19964 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2f  ction result..*/
19965 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
19966 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
19967 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f  blob(.  sqlite3_
19968 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a  context *pCtx, .
19969 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c    const void *z,
1996a 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f   .  int n, .  vo
1996b 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20  id (*xDel)(void 
1996c 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20  *).){.  assert( 
1996d 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  n>=0 );.  assert
1996e 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1996f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
19970 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
19971 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
19972 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e  r(&pCtx->s, z, n
19973 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51  , 0, xDel);.}.SQ
19974 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
19975 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
19976 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ble(sqlite3_cont
19977 65 78 74 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c  ext *pCtx, doubl
19978 65 20 72 56 61 6c 29 7b 0a 20 20 61 73 73 65 72  e rVal){.  asser
19979 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1997a 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1997b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
1997c 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44  lite3VdbeMemSetD
1997d 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20  ouble(&pCtx->s, 
1997e 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rVal);.}.SQLITE_
1997f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
19980 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71  _result_error(sq
19981 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
19982 43 74 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Ctx, const char 
19983 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73  *z, int n){.  as
19984 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
19985 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
19986 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
19987 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d   pCtx->isError =
19988 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
19989 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1998a 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
1998b 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
1998c 38 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  8, SQLITE_TRANSI
1998d 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  ENT);.}.#ifndef 
1998e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1998f 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  6.SQLITE_API voi
19990 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
19991 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33  _error16(sqlite3
19992 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
19993 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69  const void *z, i
19994 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28  nt n){.  assert(
19995 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
19996 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
19997 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78  mutex) );.  pCtx
19998 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49  ->isError = SQLI
19999 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69  TE_ERROR;.  sqli
1999a 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
1999b 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c  (&pCtx->s, z, n,
1999c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
1999d 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  IVE, SQLITE_TRAN
1999e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66  SIENT);.}.#endif
1999f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
199a0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
199a1 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  int(sqlite3_cont
199a2 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69  ext *pCtx, int i
199a3 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Val){.  assert( 
199a4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
199a5 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
199a6 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
199a7 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
199a8 34 28 26 70 43 74 78 2d 3e 73 2c 20 28 69 36 34  4(&pCtx->s, (i64
199a9 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45  )iVal);.}.SQLITE
199aa 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
199ab 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73  3_result_int64(s
199ac 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
199ad 70 43 74 78 2c 20 69 36 34 20 69 56 61 6c 29 7b  pCtx, i64 iVal){
199ae 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
199af 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
199b0 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
199b1 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
199b2 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 43  eMemSetInt64(&pC
199b3 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a  tx->s, iVal);.}.
199b4 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
199b5 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
199b6 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ull(sqlite3_cont
199b7 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73  ext *pCtx){.  as
199b8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
199b9 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
199ba 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
199bb 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
199bc 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29  etNull(&pCtx->s)
199bd 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
199be 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
199bf 6c 74 5f 74 65 78 74 28 0a 20 20 73 71 6c 69 74  lt_text(.  sqlit
199c0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
199c1 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
199c2 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 20  *z, .  int n,.  
199c3 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
199c4 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74  d *).){.  assert
199c5 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
199c6 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
199c7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
199c8 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
199c9 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e  r(&pCtx->s, z, n
199ca 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78  , SQLITE_UTF8, x
199cb 44 65 6c 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  Del);.}.#ifndef 
199cc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
199cd 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  6.SQLITE_API voi
199ce 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
199cf 5f 74 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74  _text16(.  sqlit
199d0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
199d1 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
199d2 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
199d3 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
199d4 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72  id *).){.  asser
199d5 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
199d6 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
199d7 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
199d8 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
199d9 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20  tr(&pCtx->s, z, 
199da 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  n, SQLITE_UTF16N
199db 41 54 49 56 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a  ATIVE, xDel);.}.
199dc 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
199dd 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
199de 65 78 74 31 36 62 65 28 0a 20 20 73 71 6c 69 74  ext16be(.  sqlit
199df 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
199e0 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
199e1 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
199e2 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
199e3 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72  id *).){.  asser
199e4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
199e5 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
199e6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
199e7 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
199e8 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20  tr(&pCtx->s, z, 
199e9 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  n, SQLITE_UTF16B
199ea 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49  E, xDel);.}.SQLI
199eb 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
199ec 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
199ed 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  6le(.  sqlite3_c
199ee 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20  ontext *pCtx, . 
199ef 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20   const void *z, 
199f0 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69  .  int n, .  voi
199f1 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a  d (*xDel)(void *
199f2 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  ).){.  assert( s
199f3 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
199f4 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
199f5 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
199f6 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26  3VdbeMemSetStr(&
199f7 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53  pCtx->s, z, n, S
199f8 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 78  QLITE_UTF16LE, x
199f9 44 65 6c 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Del);.}.#endif /
199fa 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
199fb 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  F16 */.SQLITE_AP
199fc 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
199fd 65 73 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69  esult_value(sqli
199fe 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
199ff 78 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  x, sqlite3_value
19a00 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 61 73 73   *pValue){.  ass
19a01 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
19a02 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
19a03 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
19a04 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f  sqlite3VdbeMemCo
19a05 70 79 28 26 70 43 74 78 2d 3e 73 2c 20 70 56 61  py(&pCtx->s, pVa
19a06 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  lue);.}.SQLITE_A
19a07 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
19a08 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28  result_zeroblob(
19a09 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
19a0a 2a 70 43 74 78 2c 20 69 6e 74 20 6e 29 7b 0a 20  *pCtx, int n){. 
19a0b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
19a0c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
19a0d 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
19a0e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
19a0f 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70  emSetZeroBlob(&p
19a10 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a 7d 0a 53 51  Ctx->s, n);.}.SQ
19a11 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
19a12 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
19a13 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f  or_code(sqlite3_
19a14 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69  context *pCtx, i
19a15 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 70  nt errCode){.  p
19a16 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 65  Ctx->isError = e
19a17 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a 20 46 6f  rrCode;.}../* Fo
19a18 72 63 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f  rce an SQLITE_TO
19a19 4f 42 49 47 20 65 72 72 6f 72 2e 20 2a 2f 0a 53  OBIG error. */.S
19a1a 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
19a1b 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
19a1c 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74  ror_toobig(sqlit
19a1d 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
19a1e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
19a1f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19a20 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
19a21 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73  x) );.  pCtx->is
19a22 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 54  Error = SQLITE_T
19a23 4f 4f 42 49 47 3b 0a 20 20 73 71 6c 69 74 65 33  OOBIG;.  sqlite3
19a24 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70  VdbeMemSetStr(&p
19a25 43 74 78 2d 3e 73 2c 20 22 73 74 72 69 6e 67 20  Ctx->s, "string 
19a26 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22  or blob too big"
19a27 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20  , -1, .         
19a28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
19a29 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
19a2a 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a  E_STATIC);.}../*
19a2b 20 41 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   An SQLITE_NOMEM
19a2c 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54   error. */.SQLIT
19a2d 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
19a2e 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
19a2f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f  nomem(sqlite3_co
19a30 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20  ntext *pCtx){.  
19a31 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
19a32 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
19a33 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
19a34 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
19a35 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e  mSetNull(&pCtx->
19a36 73 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72  s);.  pCtx->isEr
19a37 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  ror = SQLITE_NOM
19a38 45 4d 3b 0a 20 20 70 43 74 78 2d 3e 73 2e 64 62  EM;.  pCtx->s.db
19a39 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
19a3a 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65   1;.}../*.** Exe
19a3b 63 75 74 65 20 74 68 65 20 73 74 61 74 65 6d 65  cute the stateme
19a3c 6e 74 20 70 53 74 6d 74 2c 20 65 69 74 68 65 72  nt pStmt, either
19a3d 20 75 6e 74 69 6c 20 61 20 72 6f 77 20 6f 66 20   until a row of 
19a3e 64 61 74 61 20 69 73 20 72 65 61 64 79 2c 20 74  data is ready, t
19a3f 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  he.** statement 
19a40 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78  is completely ex
19a41 65 63 75 74 65 64 20 6f 72 20 61 6e 20 65 72 72  ecuted or an err
19a42 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a  or occurs..**.**
19a43 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d   This routine im
19a44 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 62 75 6c  plements the bul
19a45 6b 20 6f 66 20 74 68 65 20 6c 6f 67 69 63 20 62  k of the logic b
19a46 65 68 69 6e 64 20 74 68 65 20 73 71 6c 69 74 65  ehind the sqlite
19a47 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 2e 20  _step().** API. 
19a48 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   The only thing 
19a49 6f 6d 69 74 74 65 64 20 69 73 20 74 68 65 20 61  omitted is the a
19a4a 75 74 6f 6d 61 74 69 63 20 72 65 63 6f 6d 70 69  utomatic recompi
19a4b 6c 65 20 69 66 20 61 20 0a 2a 2a 20 73 63 68 65  le if a .** sche
19a4c 6d 61 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63  ma change has oc
19a4d 63 75 72 72 65 64 2e 20 20 54 68 61 74 20 64 65  curred.  That de
19a4e 74 61 69 6c 20 69 73 20 68 61 6e 64 6c 65 64 20  tail is handled 
19a4f 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 20  by the.** outer 
19a50 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 77  sqlite3_step() w
19a51 72 61 70 70 65 72 20 70 72 6f 63 65 64 75 72 65  rapper procedure
19a52 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
19a53 73 71 6c 69 74 65 33 53 74 65 70 28 56 64 62 65  sqlite3Step(Vdbe
19a54 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20   *p){.  sqlite3 
19a55 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  *db;.  int rc;..
19a56 20 20 61 73 73 65 72 74 28 70 29 3b 0a 20 20 69    assert(p);.  i
19a57 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42  f( p->magic!=VDB
19a58 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20  E_MAGIC_RUN ){. 
19a59 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
19a5a 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20  _MISUSE;.  }..  
19a5b 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 6d  /* Assert that m
19a5c 61 6c 6c 6f 63 28 29 20 68 61 73 20 6e 6f 74 20  alloc() has not 
19a5d 66 61 69 6c 65 64 20 2a 2f 0a 20 20 64 62 20 3d  failed */.  db =
19a5e 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62   p->db;.  if( db
19a5f 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
19a60 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
19a61 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a  ITE_NOMEM;.  }..
19a62 20 20 69 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26    if( p->pc<=0 &
19a63 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a  & p->expired ){.
19a64 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53      if( p->rc==S
19a65 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
19a66 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45    p->rc = SQLITE
19a67 5f 53 43 48 45 4d 41 3b 0a 20 20 20 20 7d 0a 20  _SCHEMA;.    }. 
19a68 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
19a69 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65  RROR;.    goto e
19a6a 6e 64 5f 6f 66 5f 73 74 65 70 3b 0a 20 20 7d 0a  nd_of_step;.  }.
19a6b 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
19a6c 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20  etyOn(db) ){.   
19a6d 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
19a6e 4d 49 53 55 53 45 3b 0a 20 20 20 20 72 65 74 75  MISUSE;.    retu
19a6f 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
19a70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
19a71 63 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  c<0 ){.    /* If
19a72 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 74   there are no ot
19a73 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 20 63  her statements c
19a74 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67  urrently running
19a75 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65  , then.    ** re
19a76 73 65 74 20 74 68 65 20 69 6e 74 65 72 72 75 70  set the interrup
19a77 74 20 66 6c 61 67 2e 20 20 54 68 69 73 20 70 72  t flag.  This pr
19a78 65 76 65 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f  events a call to
19a79 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
19a7a 70 74 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69  pt.    ** from i
19a7b 6e 74 65 72 72 75 70 74 69 6e 67 20 61 20 73 74  nterrupting a st
19a7c 61 74 65 6d 65 6e 74 20 74 68 61 74 20 68 61 73  atement that has
19a7d 20 6e 6f 74 20 79 65 74 20 73 74 61 72 74 65 64   not yet started
19a7e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
19a7f 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
19a80 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64  nt==0 ){.      d
19a81 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
19a82 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a  ted = 0;.    }..
19a83 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
19a84 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66  MIT_TRACE.    if
19a85 28 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26  ( db->xProfile &
19a86 26 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  & !db->init.busy
19a87 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65   ){.      double
19a88 20 72 4e 6f 77 3b 0a 20 20 20 20 20 20 73 71 6c   rNow;.      sql
19a89 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d  ite3OsCurrentTim
19a8a 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f  e(db->pVfs, &rNo
19a8b 77 29 3b 0a 20 20 20 20 20 20 70 2d 3e 73 74 61  w);.      p->sta
19a8c 72 74 54 69 6d 65 20 3d 20 28 75 36 34 29 28 28  rtTime = (u64)((
19a8d 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77  rNow - (int)rNow
19a8e 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30  )*3600.0*24.0*10
19a8f 30 30 30 30 30 30 30 30 2e 30 29 3b 0a 20 20 20  00000000.0);.   
19a90 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 64   }.#endif..    d
19a91 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
19a92 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72  ++;.    if( p->r
19a93 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 2d  eadOnly==0 ) db-
19a94 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2b 2b 3b  >writeVdbeCnt++;
19a95 0a 20 20 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a  .    p->pc = 0;.
19a96 20 20 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76      stmtLruRemov
19a97 65 28 70 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  e(p);.  }.#ifnde
19a98 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  f SQLITE_OMIT_EX
19a99 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e 65  PLAIN.  if( p->e
19a9a 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 63  xplain ){.    rc
19a9b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69   = sqlite3VdbeLi
19a9c 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23  st(p);.  }else.#
19a9d 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
19a9e 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a  OMIT_EXPLAIN */.
19a9f 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    {.    rc = sql
19aa0 69 74 65 33 56 64 62 65 45 78 65 63 28 70 29 3b  ite3VdbeExec(p);
19aa1 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69  .  }..  if( sqli
19aa2 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
19aa3 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
19aa4 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
19aa5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
19aa6 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a 20  OMIT_TRACE.  /* 
19aa7 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f 66 69  Invoke the profi
19aa8 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74  le callback if t
19aa9 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a 2f  here is one.  */
19aaa 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
19aab 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e 78 50 72  E_ROW && db->xPr
19aac 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e  ofile && !db->in
19aad 69 74 2e 62 75 73 79 20 26 26 20 70 2d 3e 6e 4f  it.busy && p->nO
19aae 70 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 26  p>0.           &
19aaf 26 20 70 2d 3e 61 4f 70 5b 30 5d 2e 6f 70 63 6f  & p->aOp[0].opco
19ab0 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20  de==OP_Trace && 
19ab1 70 2d 3e 61 4f 70 5b 30 5d 2e 70 34 2e 7a 21 3d  p->aOp[0].p4.z!=
19ab2 30 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20  0 ){.    double 
19ab3 72 4e 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 6c  rNow;.    u64 el
19ab4 61 70 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 73  apseTime;..    s
19ab5 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54  qlite3OsCurrentT
19ab6 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72  ime(db->pVfs, &r
19ab7 4e 6f 77 29 3b 0a 20 20 20 20 65 6c 61 70 73 65  Now);.    elapse
19ab8 54 69 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e  Time = (u64)((rN
19ab9 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a  ow - (int)rNow)*
19aba 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30  3600.0*24.0*1000
19abb 30 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 65  000000.0);.    e
19abc 6c 61 70 73 65 54 69 6d 65 20 2d 3d 20 70 2d 3e  lapseTime -= p->
19abd 73 74 61 72 74 54 69 6d 65 3b 0a 20 20 20 20 64  startTime;.    d
19abe 62 2d 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d 3e  b->xProfile(db->
19abf 70 50 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d 3e  pProfileArg, p->
19ac0 61 4f 70 5b 30 5d 2e 70 34 2e 7a 2c 20 65 6c 61  aOp[0].p4.z, ela
19ac1 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 65  pseTime);.  }.#e
19ac2 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72 43  ndif..  db->errC
19ac3 6f 64 65 20 3d 20 72 63 3b 0a 20 20 2f 2a 73 71  ode = rc;.  /*sq
19ac4 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62  lite3Error(p->db
19ac5 2c 20 72 63 2c 20 30 29 3b 2a 2f 0a 20 20 70 2d  , rc, 0);*/.  p-
19ac6 3e 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69  >rc = sqlite3Api
19ac7 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72  Exit(p->db, p->r
19ac8 63 29 3b 0a 65 6e 64 5f 6f 66 5f 73 74 65 70 3a  c);.end_of_step:
19ac9 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26 30  .  assert( (rc&0
19aca 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 69 66  xff)==rc );.  if
19acb 28 20 70 2d 3e 7a 53 71 6c 20 26 26 20 28 72 63  ( p->zSql && (rc
19acc 26 30 78 66 66 29 3c 53 51 4c 49 54 45 5f 52 4f  &0xff)<SQLITE_RO
19acd 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  W ){.    /* This
19ace 20 62 65 68 61 76 69 6f 72 20 6f 63 63 75 72 73   behavior occurs
19acf 20 69 66 20 73 71 6c 69 74 65 33 5f 70 72 65 70   if sqlite3_prep
19ad0 61 72 65 5f 76 32 28 29 20 77 61 73 20 75 73 65  are_v2() was use
19ad1 64 20 74 6f 20 62 75 69 6c 64 0a 20 20 20 20 2a  d to build.    *
19ad2 2a 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  * the prepared s
19ad3 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 72  tatement.  Retur
19ad4 6e 20 65 72 72 6f 72 20 63 6f 64 65 73 20 64 69  n error codes di
19ad5 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20 20 70 2d  rectly */.    p-
19ad6 3e 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70  >db->errCode = p
19ad7 2d 3e 72 63 3b 0a 20 20 20 20 2f 2a 20 73 71 6c  ->rc;.    /* sql
19ad8 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c  ite3Error(p->db,
19ad9 20 70 2d 3e 72 63 2c 20 30 29 3b 20 2a 2f 0a 20   p->rc, 0); */. 
19ada 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 72 63 3b     return p->rc;
19adb 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
19adc 20 54 68 69 73 20 69 73 20 66 6f 72 20 6c 65 67   This is for leg
19add 61 63 79 20 73 71 6c 69 74 65 33 5f 70 72 65 70  acy sqlite3_prep
19ade 61 72 65 28 29 20 62 75 69 6c 64 73 20 61 6e 64  are() builds and
19adf 20 77 68 65 6e 20 74 68 65 20 63 6f 64 65 0a 20   when the code. 
19ae0 20 20 20 2a 2a 20 69 73 20 53 51 4c 49 54 45 5f     ** is SQLITE_
19ae1 52 4f 57 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f  ROW or SQLITE_DO
19ae2 4e 45 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  NE */.    return
19ae3 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   rc;.  }.}../*.*
19ae4 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 74 6f  * This is the to
19ae5 70 2d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d 65 6e  p-level implemen
19ae6 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
19ae7 33 5f 73 74 65 70 28 29 2e 20 20 43 61 6c 6c 0a  3_step().  Call.
19ae8 2a 2a 20 73 71 6c 69 74 65 33 53 74 65 70 28 29  ** sqlite3Step()
19ae9 20 74 6f 20 64 6f 20 6d 6f 73 74 20 6f 66 20 74   to do most of t
19aea 68 65 20 77 6f 72 6b 2e 20 20 49 66 20 61 20 73  he work.  If a s
19aeb 63 68 65 6d 61 20 65 72 72 6f 72 20 6f 63 63 75  chema error occu
19aec 72 73 2c 0a 2a 2a 20 63 61 6c 6c 20 73 71 6c 69  rs,.** call sqli
19aed 74 65 33 52 65 70 72 65 70 61 72 65 28 29 20 61  te3Reprepare() a
19aee 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 0a 2a 2f  nd try again..*/
19aef 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
19af0 4d 49 54 5f 50 41 52 53 45 52 0a 53 51 4c 49 54  MIT_PARSER.SQLIT
19af1 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
19af2 33 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73  3_step(sqlite3_s
19af3 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69  tmt *pStmt){.  i
19af4 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d  nt rc = SQLITE_M
19af5 49 53 55 53 45 3b 0a 20 20 69 66 28 20 70 53 74  ISUSE;.  if( pSt
19af6 6d 74 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a  mt ){.    Vdbe *
19af7 76 3b 0a 20 20 20 20 76 20 3d 20 28 56 64 62 65  v;.    v = (Vdbe
19af8 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c  *)pStmt;.    sql
19af9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
19afa 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (v->db->mutex);.
19afb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
19afc 53 74 65 70 28 76 29 3b 0a 20 20 20 20 73 71 6c  Step(v);.    sql
19afd 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
19afe 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (v->db->mutex);.
19aff 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
19b00 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f  .}.#else.SQLITE_
19b01 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
19b02 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d  step(sqlite3_stm
19b03 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74  t *pStmt){.  int
19b04 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53   rc = SQLITE_MIS
19b05 55 53 45 3b 0a 20 20 69 66 28 20 70 53 74 6d 74  USE;.  if( pStmt
19b06 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20   ){.    int cnt 
19b07 3d 20 30 3b 0a 20 20 20 20 56 64 62 65 20 2a 76  = 0;.    Vdbe *v
19b08 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
19b09 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
19b0a 20 3d 20 76 2d 3e 64 62 3b 0a 20 20 20 20 73 71   = v->db;.    sq
19b0b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
19b0c 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
19b0d 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73    while( (rc = s
19b0e 71 6c 69 74 65 33 53 74 65 70 28 76 29 29 3d 3d  qlite3Step(v))==
19b0f 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 0a 20 20  SQLITE_SCHEMA.  
19b10 20 20 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b           && cnt+
19b11 2b 20 3c 20 35 0a 20 20 20 20 20 20 20 20 20 20  + < 5.          
19b12 20 26 26 20 76 64 62 65 52 65 70 72 65 70 61 72   && vdbeReprepar
19b13 65 28 76 29 20 29 7b 0a 20 20 20 20 20 20 73 71  e(v) ){.      sq
19b14 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d  lite3_reset(pStm
19b15 74 29 3b 0a 20 20 20 20 20 20 76 2d 3e 65 78 70  t);.      v->exp
19b16 69 72 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ired = 0;.    }.
19b17 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
19b18 54 45 5f 53 43 48 45 4d 41 20 26 26 20 76 2d 3e  TE_SCHEMA && v->
19b19 7a 53 71 6c 20 26 26 20 64 62 2d 3e 70 45 72 72  zSql && db->pErr
19b1a 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
19b1b 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 61 66  s case occurs af
19b1c 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f 20 72  ter failing to r
19b1d 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71 6c 20  ecompile an sql 
19b1e 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20 20 20  statement. .    
19b1f 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 6d    ** The error m
19b20 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65 20  essage from the 
19b21 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68 61 73  SQL compiler has
19b22 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c 6f   already been lo
19b23 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 69  aded .      ** i
19b24 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
19b25 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 62 6c   handle. This bl
19b26 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65 20 65  ock copies the e
19b27 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a 20 20  rror message .  
19b28 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20      ** from the 
19b29 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
19b2a 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65 6d 65  into the stateme
19b2b 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68 65 20  nt and sets the 
19b2c 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 20  statement.      
19b2d 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74  ** program count
19b2e 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73 75 72  er to 0 to ensur
19b2f 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 20  e that when the 
19b30 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a 20 20  statement is .  
19b31 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a 65 64      ** finalized
19b32 20 6f 72 20 72 65 73 65 74 20 74 68 65 20 70 61   or reset the pa
19b33 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73 73 61  rser error messa
19b34 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  ge is available 
19b35 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c  via.      ** sql
19b36 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e  ite3_errmsg() an
19b37 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  d sqlite3_errcod
19b38 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  e()..      */.  
19b39 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
19b3a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20 63 68  zErr = (const ch
19b3b 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
19b3c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 72 72  ue_text(db->pErr
19b3d 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ); .      sqlite
19b3e 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d 3e 7a  3DbFree(db, v->z
19b3f 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 69  ErrMsg);.      i
19b40 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
19b41 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  iled ){.        
19b42 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c  v->zErrMsg = sql
19b43 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
19b44 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7d 20   zErr);.      } 
19b45 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 76  else {.        v
19b46 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
19b47 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 53         v->rc = S
19b48 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
19b49 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72     }.    }.    r
19b4a 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
19b4b 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20  it(db, rc);.    
19b4c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
19b4d 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
19b4e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
19b4f 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
19b50 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73 65   Extract the use
19b51 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71  r data from a sq
19b52 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74  lite3_context st
19b53 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75  ructure and retu
19b54 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  rn a.** pointer 
19b55 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  to it..*/.SQLITE
19b56 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
19b57 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c  e3_user_data(sql
19b58 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29  ite3_context *p)
19b59 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26  {.  assert( p &&
19b5a 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72   p->pFunc );.  r
19b5b 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d 3e  eturn p->pFunc->
19b5c 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f 2a  pUserData;.}../*
19b5d 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
19b5e 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61  user data from a
19b5f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
19b60 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72   structure and r
19b61 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74  eturn a.** point
19b62 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c  er to it..*/.SQL
19b63 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20  ITE_API sqlite3 
19b64 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  *sqlite3_context
19b65 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74  _db_handle(sqlit
19b66 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a  e3_context *p){.
19b67 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70    assert( p && p
19b68 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 74  ->pFunc );.  ret
19b69 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a  urn p->s.db;.}..
19b6a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
19b6b 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c 65  ing is the imple
19b6c 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20  mentation of an 
19b6d 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  SQL function tha
19b6e 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 6c  t always.** fail
19b6f 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  s with an error 
19b70 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 20  message stating 
19b71 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f  that the functio
19b72 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65  n is used in the
19b73 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 78  .** wrong contex
19b74 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  t.  The sqlite3_
19b75 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f  overload_functio
19b76 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 6f  n() API might co
19b77 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 66  nstruct.** SQL f
19b78 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65  unction that use
19b79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 6f   this routine so
19b7a 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69   that the functi
19b7b 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a 2a  ons will exist.*
19b7c 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c  * for name resol
19b7d 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 63  ution but are ac
19b7e 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 65  tually overloade
19b7f 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 75  d by the xFindFu
19b80 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f 64  nction.** method
19b81 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c   of virtual tabl
19b82 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
19b83 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
19b84 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f  e3InvalidFunctio
19b85 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  n(.  sqlite3_con
19b86 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 20  text *context,  
19b87 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  /* The function 
19b88 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 20  calling context 
19b89 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  */.  int NotUsed
19b8a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
19b8b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67  /* Number of arg
19b8c 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75  uments to the fu
19b8d 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69  nction */.  sqli
19b8e 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
19b8f 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65 20  sed2   /* Value 
19b90 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74  of each argument
19b91 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63   */.){.  const c
19b92 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f 6e  har *zName = con
19b93 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e 61  text->pFunc->zNa
19b94 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  me;.  char *zErr
19b95 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
19b96 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
19b97 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72 72  otUsed2);.  zErr
19b98 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
19b99 66 28 30 2c 0a 20 20 20 20 20 20 22 75 6e 61 62  f(0,.      "unab
19b9a 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69  le to use functi
19b9b 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 71  on %s in the req
19b9c 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c  uested context",
19b9d 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74   zName);.  sqlit
19b9e 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
19b9f 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
19ba0 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  1);.  sqlite3_fr
19ba1 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a  ee(zErr);.}../*.
19ba2 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72  ** Allocate or r
19ba3 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 67  eturn the aggreg
19ba4 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ate context for 
19ba5 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e  a user function.
19ba6 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65    A new.** conte
19ba7 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  xt is allocated 
19ba8 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  on the first cal
19ba9 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63  l.  Subsequent c
19baa 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a  alls return the.
19bab 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20  ** same context 
19bac 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65  that was returne
19bad 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73  d on prior calls
19bae 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
19baf 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67  void *sqlite3_ag
19bb0 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
19bb1 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
19bb2 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a  *p, int nByte){.
19bb3 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61    Mem *pMem;.  a
19bb4 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70  ssert( p && p->p
19bb5 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63  Func && p->pFunc
19bb6 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73  ->xStep );.  ass
19bb7 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
19bb8 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d  ex_held(p->s.db-
19bb9 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65  >mutex) );.  pMe
19bba 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69  m = p->pMem;.  i
19bbb 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  f( (pMem->flags 
19bbc 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b  & MEM_Agg)==0 ){
19bbd 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d  .    if( nByte==
19bbe 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
19bbf 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
19bc0 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a  External(pMem);.
19bc1 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
19bc2 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
19bc3 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b      pMem->z = 0;
19bc4 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
19bc5 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19bc6 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65  Grow(pMem, nByte
19bc7 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d  , 0);.      pMem
19bc8 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67  ->flags = MEM_Ag
19bc9 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  g;.      pMem->u
19bca 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63  .pDef = p->pFunc
19bcb 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d  ;.      if( pMem
19bcc 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  ->z ){.        m
19bcd 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30  emset(pMem->z, 0
19bce 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  , nByte);.      
19bcf 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
19bd0 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d  turn (void*)pMem
19bd1 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ->z;.}../*.** Re
19bd2 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72  turn the auxilar
19bd3 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20  y data pointer, 
19bd4 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20  if any, for the 
19bd5 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74  iArg'th argument
19bd6 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d   to.** the user-
19bd7 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64  function defined
19bd8 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c   by pCtx..*/.SQL
19bd9 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
19bda 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
19bdb 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  a(sqlite3_contex
19bdc 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72  t *pCtx, int iAr
19bdd 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a  g){.  VdbeFunc *
19bde 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73  pVdbeFunc;..  as
19bdf 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
19be0 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
19be1 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
19be2 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74   pVdbeFunc = pCt
19be3 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20  x->pVdbeFunc;.  
19be4 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c  if( !pVdbeFunc |
19be5 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e  | iArg>=pVdbeFun
19be6 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c  c->nAux || iArg<
19be7 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
19be8 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
19be9 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78  pVdbeFunc->apAux
19bea 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a  [iArg].pAux;.}..
19beb 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75  /*.** Set the au
19bec 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e  xilary data poin
19bed 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66  ter and delete f
19bee 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65  unction, for the
19bef 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75   iArg'th.** argu
19bf0 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72  ment to the user
19bf1 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65  -function define
19bf2 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70  d by pCtx. Any p
19bf3 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73  revious value is
19bf4 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63  .** deleted by c
19bf5 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74  alling the delet
19bf6 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69  e function speci
19bf7 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 73  fied when it was
19bf8 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   set..*/.SQLITE_
19bf9 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
19bfa 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20  _set_auxdata(.  
19bfb 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
19bfc 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41  *pCtx, .  int iA
19bfd 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75  rg, .  void *pAu
19bfe 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  x, .  void (*xDe
19bff 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  lete)(void*).){.
19c00 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61    struct AuxData
19c01 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64   *pAuxData;.  Vd
19c02 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e  beFunc *pVdbeFun
19c03 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20  c;.  if( iArg<0 
19c04 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a  ) goto failed;..
19c05 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
19c06 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
19c07 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
19c08 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d  );.  pVdbeFunc =
19c09 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63   pCtx->pVdbeFunc
19c0a 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75  ;.  if( !pVdbeFu
19c0b 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d  nc || pVdbeFunc-
19c0c 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20  >nAux<=iArg ){. 
19c0d 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70     int nAux = (p
19c0e 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65  VdbeFunc ? pVdbe
19c0f 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b  Func->nAux : 0);
19c10 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63  .    int nMalloc
19c11 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75   = sizeof(VdbeFu
19c12 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72  nc) + sizeof(str
19c13 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 72  uct AuxData)*iAr
19c14 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63  g;.    pVdbeFunc
19c15 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
19c16 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20  loc(pCtx->s.db, 
19c17 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c  pVdbeFunc, nMall
19c18 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56  oc);.    if( !pV
19c19 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20  dbeFunc ){.     
19c1a 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20   goto failed;.  
19c1b 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56    }.    pCtx->pV
19c1c 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46  dbeFunc = pVdbeF
19c1d 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  unc;.    memset(
19c1e 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75  &pVdbeFunc->apAu
19c1f 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65  x[nAux], 0, size
19c20 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61 74  of(struct AuxDat
19c21 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29  a)*(iArg+1-nAux)
19c22 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63  );.    pVdbeFunc
19c23 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b  ->nAux = iArg+1;
19c24 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e  .    pVdbeFunc->
19c25 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46  pFunc = pCtx->pF
19c26 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78  unc;.  }..  pAux
19c27 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e  Data = &pVdbeFun
19c28 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a  c->apAux[iArg];.
19c29 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e    if( pAuxData->
19c2a 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74 61  pAux && pAuxData
19c2b 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20  ->xDelete ){.   
19c2c 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65   pAuxData->xDele
19c2d 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75  te(pAuxData->pAu
19c2e 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61  x);.  }.  pAuxDa
19c2f 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b  ta->pAux = pAux;
19c30 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65  .  pAuxData->xDe
19c31 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a  lete = xDelete;.
19c32 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65    return;..faile
19c33 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65  d:.  if( xDelete
19c34 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28   ){.    xDelete(
19c35 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  pAux);.  }.}..#i
19c36 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
19c37 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a  T_DEPRECATED./*.
19c38 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
19c39 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
19c3a 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20  e Step function 
19c3b 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20 68  of a aggregate h
19c3c 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c  as been .** call
19c3d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ed..**.** This f
19c3e 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65  unction is depre
19c3f 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75  cated.  Do not u
19c40 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f  se it for new co
19c41 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72  de.  It is.** pr
19c42 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76  ovide only to av
19c43 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67  oid breaking leg
19c44 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61  acy code.  New a
19c45 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
19c46 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  n.** implementat
19c47 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70  ions should keep
19c48 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74   their own count
19c49 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 61  s within their a
19c4a 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74  ggregate.** cont
19c4b 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ext..*/.SQLITE_A
19c4c 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61  PI int sqlite3_a
19c4d 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73  ggregate_count(s
19c4e 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
19c4f 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
19c50 26 26 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70  && p->pFunc && p
19c51 2d 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29  ->pFunc->xStep )
19c52 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d  ;.  return p->pM
19c53 65 6d 2d 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a  em->n;.}.#endif.
19c54 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
19c55 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
19c56 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  mns in the resul
19c57 74 20 73 65 74 20 66 6f 72 20 74 68 65 20 73 74  t set for the st
19c58 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a  atement pStmt..*
19c59 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
19c5a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
19c5b 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74  count(sqlite3_st
19c5c 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64  mt *pStmt){.  Vd
19c5d 62 65 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20  be *pVm = (Vdbe 
19c5e 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72  *)pStmt;.  retur
19c5f 6e 20 70 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65  n pVm ? pVm->nRe
19c60 73 43 6f 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a  sColumn : 0;.}..
19c61 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
19c62 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65   number of value
19c63 73 20 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d  s available from
19c64 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
19c65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   of the.** curre
19c66 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73  ntly executing s
19c67 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a  tatement pStmt..
19c68 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
19c69 74 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63  t sqlite3_data_c
19c6a 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  ount(sqlite3_stm
19c6b 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62  t *pStmt){.  Vdb
19c6c 65 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a  e *pVm = (Vdbe *
19c6d 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 56  )pStmt;.  if( pV
19c6e 6d 3d 3d 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65  m==0 || pVm->pRe
19c6f 73 75 6c 74 53 65 74 3d 3d 30 20 29 20 72 65 74  sultSet==0 ) ret
19c70 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  urn 0;.  return 
19c71 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b  pVm->nResColumn;
19c72 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  .}.../*.** Check
19c73 20 74 6f 20 73 65 65 20 69 66 20 63 6f 6c 75 6d   to see if colum
19c74 6e 20 69 43 6f 6c 20 6f 66 20 74 68 65 20 67 69  n iCol of the gi
19c75 76 65 6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ven statement is
19c76 20 76 61 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69   valid.  If.** i
19c77 74 20 69 73 2c 20 72 65 74 75 72 6e 20 61 20 70  t is, return a p
19c78 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65  ointer to the Me
19c79 6d 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20  m for the value 
19c7a 6f 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a  of that column..
19c7b 2a 2a 20 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f  ** If iCol is no
19c7c 74 20 76 61 6c 69 64 2c 20 72 65 74 75 72 6e 20  t valid, return 
19c7d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d  a pointer to a M
19c7e 65 6d 20 77 68 69 63 68 20 68 61 73 20 61 20 76  em which has a v
19c7f 61 6c 75 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e  alue.** of NULL.
19c80 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a  .*/.static Mem *
19c81 63 6f 6c 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65  columnMem(sqlite
19c82 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
19c83 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70  nt i){.  Vdbe *p
19c84 56 6d 3b 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a  Vm;.  int vals;.
19c85 20 20 4d 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20    Mem *pOut;..  
19c86 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53  pVm = (Vdbe *)pS
19c87 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 20 26  tmt;.  if( pVm &
19c88 26 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65  & pVm->pResultSe
19c89 74 21 3d 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e  t!=0 && i<pVm->n
19c8a 52 65 73 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d  ResColumn && i>=
19c8b 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
19c8c 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d  _mutex_enter(pVm
19c8d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
19c8e 20 20 76 61 6c 73 20 3d 20 73 71 6c 69 74 65 33    vals = sqlite3
19c8f 5f 64 61 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d  _data_count(pStm
19c90 74 29 3b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26  t);.    pOut = &
19c91 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b  pVm->pResultSet[
19c92 69 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  i];.  }else{.   
19c93 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 4d 65   static const Me
19c94 6d 20 6e 75 6c 6c 4d 65 6d 20 3d 20 7b 7b 30 7d  m nullMem = {{0}
19c95 2c 20 30 2e 30 2c 20 30 2c 20 22 22 2c 20 30 2c  , 0.0, 0, "", 0,
19c96 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54   MEM_Null, SQLIT
19c97 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20  E_NULL, 0, 0, 0 
19c98 7d 3b 0a 20 20 20 20 69 66 28 20 70 56 6d 2d 3e  };.    if( pVm->
19c99 64 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  db ){.      sqli
19c9a 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
19c9b 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  pVm->db->mutex);
19c9c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
19c9d 72 6f 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c  ror(pVm->db, SQL
19c9e 49 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20  ITE_RANGE, 0);. 
19c9f 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20     }.    pOut = 
19ca0 28 4d 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a  (Mem*)&nullMem;.
19ca1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75    }.  return pOu
19ca2 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  t;.}../*.** This
19ca3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
19ca4 6c 65 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69  led after invoki
19ca5 6e 67 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61  ng an sqlite3_va
19ca6 6c 75 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e  lue_XXX function
19ca7 20 6f 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e   on a .** column
19ca8 20 76 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76   value (i.e. a v
19ca9 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
19caa 20 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53   evaluating an S
19cab 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  QL expression in
19cac 20 74 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c   the.** select l
19cad 69 73 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20  ist of a SELECT 
19cae 73 74 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20  statement) that 
19caf 6d 61 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c  may cause a mall
19cb0 6f 63 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66  oc() failure. If
19cb1 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61   .** malloc() ha
19cb2 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68  s failed, the th
19cb3 72 65 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c  reads mallocFail
19cb4 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72  ed flag is clear
19cb5 65 64 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c  ed and the resul
19cb6 74 0a 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61  t.** code of sta
19cb7 74 65 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74  tement pStmt set
19cb8 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   to SQLITE_NOMEM
19cb9 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63  ..**.** Specific
19cba 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61  ally, this is ca
19cbb 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
19cbc 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69  :.**.**     sqli
19cbd 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29  te3_column_int()
19cbe 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .**     sqlite3_
19cbf 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a  column_int64().*
19cc0 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
19cc1 6c 75 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20  lumn_text().**  
19cc2 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
19cc3 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20  n_text16().**   
19cc4 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
19cc5 5f 72 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73  _real().**     s
19cc6 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
19cc7 74 65 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  tes().**     sql
19cc8 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
19cc9 73 31 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20  s16().**.** But 
19cca 6e 6f 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  not for sqlite3_
19ccb 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77  column_blob(), w
19ccc 68 69 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73  hich never calls
19ccd 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74   malloc()..*/.st
19cce 61 74 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e  atic void column
19ccf 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71  MallocFailure(sq
19cd0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
19cd1 74 29 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c  t).{.  /* If mal
19cd2 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72  loc() failed dur
19cd3 69 6e 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20  ing an encoding 
19cd4 63 6f 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69  conversion withi
19cd5 6e 20 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65  n an.  ** sqlite
19cd6 33 5f 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49  3_column_XXX API
19cd7 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 72  , then set the r
19cd8 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68  eturn code of th
19cd9 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20  e statement to. 
19cda 20 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   ** SQLITE_NOMEM
19cdb 2e 20 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  . The next call 
19cdc 74 6f 20 5f 73 74 65 70 28 29 20 28 69 66 20 61  to _step() (if a
19cdd 6e 79 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ny) will return 
19cde 53 51 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a  SQLITE_ERROR.  *
19cdf 2a 20 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28  * and _finalize(
19ce0 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f  ) will return NO
19ce1 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65  MEM..  */.  Vdbe
19ce2 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53   *p = (Vdbe *)pS
19ce3 74 6d 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  tmt;.  if( p ){.
19ce4 20 20 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69      p->rc = sqli
19ce5 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62  te3ApiExit(p->db
19ce6 2c 20 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71  , p->rc);.    sq
19ce7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
19ce8 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
19ce9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  .  }.}../*******
19cea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ceb 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  ***** sqlite3_co
19cec 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lumn_  *********
19ced 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19cee 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ******.** The fo
19cef 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73  llowing routines
19cf0 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 63 63   are used to acc
19cf1 65 73 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  ess elements of 
19cf2 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a  the current row.
19cf3 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ** in the result
19cf4 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   set..*/.SQLITE_
19cf5 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
19cf6 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
19cf7 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  lob(sqlite3_stmt
19cf8 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b   *pStmt, int i){
19cf9 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76  .  const void *v
19cfa 61 6c 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69  al;.  val = sqli
19cfb 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20  te3_value_blob( 
19cfc 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c  columnMem(pStmt,
19cfd 69 29 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20  i) );.  /* Even 
19cfe 74 68 6f 75 67 68 20 74 68 65 72 65 20 69 73 20  though there is 
19cff 6e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76  no encoding conv
19d00 65 72 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c  ersion, value_bl
19d01 6f 62 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20  ob() might.  ** 
19d02 6e 65 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c  need to call mal
19d03 6c 6f 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20  loc() to expand 
19d04 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20  the result of a 
19d05 7a 65 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a  zeroblob() .  **
19d06 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20   expression. .  
19d07 2a 2f 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  */.  columnMallo
19d08 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
19d09 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
19d0a 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
19d0b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
19d0c 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ytes(sqlite3_stm
19d0d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
19d0e 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71  {.  int val = sq
19d0f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
19d10 73 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  s( columnMem(pSt
19d11 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
19d12 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
19d13 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
19d14 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  val;.}.SQLITE_AP
19d15 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
19d16 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c  lumn_bytes16(sql
19d17 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
19d18 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
19d19 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
19d1a 6c 75 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c  lue_bytes16( col
19d1b 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
19d1c 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
19d1d 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
19d1e 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
19d1f 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62  .SQLITE_API doub
19d20 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  le sqlite3_colum
19d21 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  n_double(sqlite3
19d22 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
19d23 74 20 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76  t i){.  double v
19d24 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
19d25 75 65 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d  ue_double( colum
19d26 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
19d27 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
19d28 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
19d29 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
19d2a 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
19d2b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
19d2c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
19d2d 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
19d2e 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  int val = sqlite
19d2f 33 5f 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c  3_value_int( col
19d30 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
19d31 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
19d32 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
19d33 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
19d34 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
19d35 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  te_int64 sqlite3
19d36 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71  _column_int64(sq
19d37 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
19d38 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c  t, int i){.  sql
19d39 69 74 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20  ite_int64 val = 
19d3a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
19d3b 74 36 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  t64( columnMem(p
19d3c 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c  Stmt,i) );.  col
19d3d 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
19d3e 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
19d3f 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
19d40 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  API const unsign
19d41 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  ed char *sqlite3
19d42 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c  _column_text(sql
19d43 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
19d44 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73  , int i){.  cons
19d45 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
19d46 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76  *val = sqlite3_v
19d47 61 6c 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d  alue_text( colum
19d48 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
19d49 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
19d4a 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
19d4b 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
19d4c 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
19d4d 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33  3_value *sqlite3
19d4e 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71  _column_value(sq
19d4f 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
19d50 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d  t, int i){.  Mem
19d51 20 2a 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d   *pOut = columnM
19d52 65 6d 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20  em(pStmt, i);.  
19d53 69 66 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26  if( pOut->flags&
19d54 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20  MEM_Static ){.  
19d55 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d    pOut->flags &=
19d56 20 7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20   ~MEM_Static;.  
19d57 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d    pOut->flags |=
19d58 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a   MEM_Ephem;.  }.
19d59 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61    columnMallocFa
19d5a 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20  ilure(pStmt);.  
19d5b 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f  return (sqlite3_
19d5c 76 61 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a  value *)pOut;.}.
19d5d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
19d5e 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
19d5f 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
19d60 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
19d61 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73  text16(sqlite3_s
19d62 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
19d63 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  i){.  const void
19d64 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f   *val = sqlite3_
19d65 76 61 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f  value_text16( co
19d66 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
19d67 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   );.  columnMall
19d68 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
19d69 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a  ;.  return val;.
19d6a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
19d6b 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
19d6c 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
19d6d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
19d6e 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ype(sqlite3_stmt
19d6f 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b   *pStmt, int i){
19d70 0a 20 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73  .  int iType = s
19d71 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
19d72 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  e( columnMem(pSt
19d73 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
19d74 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
19d75 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
19d76 69 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65  iType;.}../* The
19d77 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74   following funct
19d78 69 6f 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e  ion is experimen
19d79 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20  tal and subject 
19d7a 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20  to change or.** 
19d7b 72 65 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74  removal */./*int
19d7c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
19d7d 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c  numeric_type(sql
19d7e 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
19d7f 2c 20 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65  , int i){.**  re
19d80 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  turn sqlite3_val
19d81 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
19d82 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
19d83 2c 69 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f  ,i) );.**}.*/../
19d84 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65  *.** Convert the
19d85 20 4e 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66   N-th element of
19d86 20 70 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65   pStmt->pColName
19d87 5b 5d 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67  [] into a string
19d88 20 75 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28   using.** xFunc(
19d89 29 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68  ) then return th
19d8a 61 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e  at string.  If N
19d8b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
19d8c 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a  , return 0..**.*
19d8d 2a 20 54 68 65 72 65 20 61 72 65 20 75 70 20 74  * There are up t
19d8e 6f 20 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61  o 5 names for ea
19d8f 63 68 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54  ch column.  useT
19d90 79 70 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77  ype determines w
19d91 68 69 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20  hich.** name is 
19d92 72 65 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20  returned.  Here 
19d93 61 72 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a  are the names:.*
19d94 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54  *.**    0      T
19d95 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61  he column name a
19d96 73 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64  s it should be d
19d97 69 73 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74  isplayed for out
19d98 70 75 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20  put.**    1     
19d99 20 54 68 65 20 64 61 74 61 74 79 70 65 20 6e 61   The datatype na
19d9a 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  me for the colum
19d9b 6e 0a 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54  n.**    2      T
19d9c 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  he name of the d
19d9d 61 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65  atabase that the
19d9e 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20   column derives 
19d9f 66 72 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20  from.**    3    
19da0 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68    The name of th
19da1 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65  e table that the
19da2 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20   column derives 
19da3 66 72 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20  from.**    4    
19da4 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68    The name of th
19da5 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74  e table column t
19da6 68 61 74 20 74 68 65 20 72 65 73 75 6c 74 20 63  hat the result c
19da7 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72  olumn derives fr
19da8 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  om.**.** If the 
19da9 72 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20  result is not a 
19daa 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65  simple column re
19dab 66 65 72 65 6e 63 65 20 28 69 66 20 69 74 20 69  ference (if it i
19dac 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a  s an expression.
19dad 2a 2a 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74  ** or a constant
19dae 29 20 74 68 65 6e 20 75 73 65 54 79 70 65 73 20  ) then useTypes 
19daf 32 2c 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75  2, 3, and 4 retu
19db0 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74  rn NULL..*/.stat
19db1 69 63 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63  ic const void *c
19db2 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c  olumnName(.  sql
19db3 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
19db4 2c 0a 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e  ,.  int N,.  con
19db5 73 74 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63  st void *(*xFunc
19db6 29 28 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75  )(Mem*),.  int u
19db7 73 65 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73  seType.){.  cons
19db8 74 20 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b  t void *ret = 0;
19db9 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
19dba 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e  be *)pStmt;.  in
19dbb 74 20 6e 3b 0a 20 20 0a 0a 20 20 69 66 28 20 70  t n;.  ..  if( p
19dbc 21 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73  !=0 ){.    n = s
19dbd 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
19dbe 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20  unt(pStmt);.    
19dbf 69 66 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20  if( N<n && N>=0 
19dc0 29 7b 0a 20 20 20 20 20 20 4e 20 2b 3d 20 75 73  ){.      N += us
19dc1 65 54 79 70 65 2a 6e 3b 0a 20 20 20 20 20 20 73  eType*n;.      s
19dc2 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
19dc3 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  er(p->db->mutex)
19dc4 3b 0a 20 20 20 20 20 20 72 65 74 20 3d 20 78 46  ;.      ret = xF
19dc5 75 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65  unc(&p->aColName
19dc6 5b 4e 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  [N]);..      /* 
19dc7 41 20 6d 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76  A malloc may hav
19dc8 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20  e failed inside 
19dc9 6f 66 20 74 68 65 20 78 46 75 6e 63 28 29 20 63  of the xFunc() c
19dca 61 6c 6c 2e 20 49 66 20 74 68 69 73 0a 20 20 20  all. If this.   
19dcb 20 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61 73     ** is the cas
19dcc 65 2c 20 63 6c 65 61 72 20 74 68 65 20 6d 61 6c  e, clear the mal
19dcd 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 61  locFailed flag a
19dce 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  nd return NULL..
19dcf 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
19dd0 66 28 20 70 2d 3e 64 62 20 26 26 20 70 2d 3e 64  f( p->db && p->d
19dd1 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
19dd2 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 64 62  ){.        p->db
19dd3 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
19dd4 20 30 3b 0a 20 20 20 20 20 20 20 20 72 65 74 20   0;.        ret 
19dd5 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
19dd6 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
19dd7 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
19dd8 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  tex);.    }.  }.
19dd9 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a    return ret;.}.
19dda 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
19ddb 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 4e 74  e name of the Nt
19ddc 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
19ddd 72 65 73 75 6c 74 20 73 65 74 20 72 65 74 75 72  result set retur
19dde 6e 65 64 20 62 79 20 53 51 4c 0a 2a 2a 20 73 74  ned by SQL.** st
19ddf 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a  atement pStmt..*
19de0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
19de1 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
19de2 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c  _column_name(sql
19de3 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
19de4 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
19de5 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
19de6 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
19de7 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
19de8 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
19de9 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45  ue_text, COLNAME
19dea 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 65  _NAME);.}.#ifnde
19deb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
19dec 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  F16.SQLITE_API c
19ded 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
19dee 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
19def 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
19df0 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
19df1 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
19df2 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
19df3 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
19df4 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
19df5 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43  _value_text16, C
19df6 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a  OLNAME_NAME);.}.
19df7 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f  #endif../*.** Co
19df8 6e 73 74 72 61 69 6e 74 3a 20 20 49 66 20 79 6f  nstraint:  If yo
19df9 75 20 68 61 76 65 20 45 4e 41 42 4c 45 5f 43 4f  u have ENABLE_CO
19dfa 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 74 68  LUMN_METADATA th
19dfb 65 6e 20 79 6f 75 20 6d 75 73 74 0a 2a 2a 20 6e  en you must.** n
19dfc 6f 74 20 64 65 66 69 6e 65 20 4f 4d 49 54 5f 44  ot define OMIT_D
19dfd 45 43 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69 66 20  ECLTYPE..*/.#if 
19dfe 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
19dff 4d 49 54 5f 44 45 43 4c 54 59 50 45 29 20 26 26  MIT_DECLTYPE) &&
19e00 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
19e01 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
19e02 54 41 44 41 54 41 29 0a 23 20 65 72 72 6f 72 20  TADATA).# error 
19e03 22 4d 75 73 74 20 6e 6f 74 20 64 65 66 69 6e 65  "Must not define
19e04 20 62 6f 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49   both SQLITE_OMI
19e05 54 5f 44 45 43 4c 54 59 50 45 20 5c 0a 20 20 20  T_DECLTYPE \.   
19e06 20 20 20 20 20 20 61 6e 64 20 53 51 4c 49 54 45        and SQLITE
19e07 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
19e08 45 54 41 44 41 54 41 22 0a 23 65 6e 64 69 66 0a  ETADATA".#endif.
19e09 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
19e0a 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a 2f 2a  OMIT_DECLTYPE./*
19e0b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63  .** Return the c
19e0c 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f  olumn declaratio
19e0d 6e 20 74 79 70 65 20 28 69 66 20 61 70 70 6c 69  n type (if appli
19e0e 63 61 62 6c 65 29 20 6f 66 20 74 68 65 20 27 69  cable) of the 'i
19e0f 27 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66  'th column.** of
19e10 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
19e11 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  of SQL statement
19e12 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54   pStmt..*/.SQLIT
19e13 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
19e14 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
19e15 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65  _decltype(sqlite
19e16 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
19e17 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
19e18 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20  columnName(.    
19e19 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e    pStmt, N, (con
19e1a 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a  st void*(*)(Mem*
19e1b 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ))sqlite3_value_
19e1c 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45  text, COLNAME_DE
19e1d 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 69 66 6e 64  CLTYPE);.}.#ifnd
19e1e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
19e1f 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20  TF16.SQLITE_API 
19e20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
19e21 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
19e22 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ype16(sqlite3_st
19e23 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
19e24 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75  ){.  return colu
19e25 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53  mnName(.      pS
19e26 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76  tmt, N, (const v
19e27 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71  oid*(*)(Mem*))sq
19e28 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
19e29 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c  16, COLNAME_DECL
19e2a 54 59 50 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  TYPE);.}.#endif 
19e2b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
19e2c 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  TF16 */.#endif /
19e2d 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  * SQLITE_OMIT_DE
19e2e 43 4c 54 59 50 45 20 2a 2f 0a 0a 23 69 66 64 65  CLTYPE */..#ifde
19e2f 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
19e30 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a  COLUMN_METADATA.
19e31 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
19e32 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
19e33 61 62 61 73 65 20 66 72 6f 6d 20 77 68 69 63 68  abase from which
19e34 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e   a result column
19e35 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c   derives..** NUL
19e36 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  L is returned if
19e37 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75   the result colu
19e38 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73  mn is an express
19e39 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20  ion or constant 
19e3a 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65  or.** anything e
19e3b 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74  lse which is not
19e3c 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72   an unabiguous r
19e3d 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61  eference to a da
19e3e 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a  tabase column..*
19e3f 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
19e40 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
19e41 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
19e42 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
19e43 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
19e44 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75  ){.  return colu
19e45 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53  mnName(.      pS
19e46 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76  tmt, N, (const v
19e47 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71  oid*(*)(Mem*))sq
19e48 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
19e49 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41  , COLNAME_DATABA
19e4a 53 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  SE);.}.#ifndef S
19e4b 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
19e4c 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
19e4d 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
19e4e 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
19e4f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73  name16(sqlite3_s
19e50 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
19e51 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
19e52 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
19e53 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
19e54 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
19e55 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
19e56 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54  t16, COLNAME_DAT
19e57 41 42 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 69 66  ABASE);.}.#endif
19e58 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
19e59 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  UTF16 */../*.** 
19e5a 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  Return the name 
19e5b 6f 66 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f  of the table fro
19e5c 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74  m which a result
19e5d 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e   column derives.
19e5e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  .** NULL is retu
19e5f 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75  rned if the resu
19e60 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20  lt column is an 
19e61 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f  expression or co
19e62 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79  nstant or.** any
19e63 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68  thing else which
19e64 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69   is not an unabi
19e65 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20  guous reference 
19e66 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  to a database co
19e67 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lumn..*/.SQLITE_
19e68 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
19e69 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
19e6a 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65  able_name(sqlite
19e6b 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
19e6c 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
19e6d 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20  columnName(.    
19e6e 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e    pStmt, N, (con
19e6f 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a  st void*(*)(Mem*
19e70 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ))sqlite3_value_
19e71 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41  text, COLNAME_TA
19e72 42 4c 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  BLE);.}.#ifndef 
19e73 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
19e74 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  6.SQLITE_API con
19e75 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
19e76 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
19e77 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
19e78 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
19e79 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
19e7a 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
19e7b 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
19e7c 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
19e7d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
19e7e 36 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45  6, COLNAME_TABLE
19e7f 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
19e80 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
19e81 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   */../*.** Retur
19e82 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
19e83 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66  e table column f
19e84 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75  rom which a resu
19e85 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65  lt column derive
19e86 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65  s..** NULL is re
19e87 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65  turned if the re
19e88 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61  sult column is a
19e89 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20  n expression or 
19e8a 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61  constant or.** a
19e8b 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69  nything else whi
19e8c 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61  ch is not an una
19e8d 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63  biguous referenc
19e8e 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  e to a database 
19e8f 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  column..*/.SQLIT
19e90 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
19e91 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
19e92 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c  _origin_name(sql
19e93 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
19e94 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
19e95 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
19e96 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
19e97 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
19e98 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
19e99 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45  ue_text, COLNAME
19e9a 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 6e  _COLUMN);.}.#ifn
19e9b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19e9c 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
19e9d 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
19e9e 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
19e9f 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  in_name16(sqlite
19ea0 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
19ea1 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
19ea2 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20  columnName(.    
19ea3 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e    pStmt, N, (con
19ea4 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a  st void*(*)(Mem*
19ea5 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ))sqlite3_value_
19ea6 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f  text16, COLNAME_
19ea7 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64 69  COLUMN);.}.#endi
19ea8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
19ea9 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66  _UTF16 */.#endif
19eaa 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
19eab 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
19eac 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  A */.../********
19ead 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eae 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f  ******* sqlite3_
19eaf 62 69 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  bind_  *********
19eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eb1 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69 6e  **.** .** Routin
19eb2 65 73 20 75 73 65 64 20 74 6f 20 61 74 74 61 63  es used to attac
19eb3 68 20 76 61 6c 75 65 73 20 74 6f 20 77 69 6c 64  h values to wild
19eb4 63 61 72 64 73 20 69 6e 20 61 20 63 6f 6d 70 69  cards in a compi
19eb5 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  led SQL statemen
19eb6 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62 69  t..*/./*.** Unbi
19eb7 6e 64 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75  nd the value bou
19eb8 6e 64 20 74 6f 20 76 61 72 69 61 62 6c 65 20 69  nd to variable i
19eb9 20 69 6e 20 76 69 72 74 75 61 6c 20 6d 61 63 68   in virtual mach
19eba 69 6e 65 20 70 2e 20 54 68 69 73 20 69 73 20 74  ine p. This is t
19ebb 68 65 20 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20  he .** the same 
19ebc 61 73 20 62 69 6e 64 69 6e 67 20 61 20 4e 55 4c  as binding a NUL
19ebd 4c 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 63  L value to the c
19ebe 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 20 22 69  olumn. If the "i
19ebf 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a  " parameter is.*
19ec0 2a 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20  * out of range, 
19ec1 74 68 65 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47  then SQLITE_RANG
19ec2 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  E is returned. O
19ec3 74 68 65 77 69 73 65 20 53 51 4c 49 54 45 5f 4f  thewise SQLITE_O
19ec4 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63 63 65  K..**.** A succe
19ec5 73 73 66 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e  ssful evaluation
19ec6 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
19ec7 20 61 63 71 75 69 72 65 73 20 74 68 65 20 6d 75   acquires the mu
19ec8 74 65 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68 65  tex on p..** the
19ec9 20 6d 75 74 65 78 20 69 73 20 72 65 6c 65 61 73   mutex is releas
19eca 65 64 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f  ed if any kind o
19ecb 66 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  f error occurs..
19ecc 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20  **.** The error 
19ecd 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e 20 64  code stored in d
19ece 61 74 61 62 61 73 65 20 70 2d 3e 64 62 20 69 73  atabase p->db is
19ecf 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74   overwritten wit
19ed0 68 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20  h the return.** 
19ed1 76 61 6c 75 65 20 69 6e 20 61 6e 79 20 63 61 73  value in any cas
19ed2 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
19ed3 20 76 64 62 65 55 6e 62 69 6e 64 28 56 64 62 65   vdbeUnbind(Vdbe
19ed4 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d   *p, int i){.  M
19ed5 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 66 28 20  em *pVar;.  if( 
19ed6 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  p==0 ) return SQ
19ed7 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 73  LITE_MISUSE;.  s
19ed8 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
19ed9 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  er(p->db->mutex)
19eda 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63  ;.  if( p->magic
19edb 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  !=VDBE_MAGIC_RUN
19edc 20 7c 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a   || p->pc>=0 ){.
19edd 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
19ede 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4d  (p->db, SQLITE_M
19edf 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 73  ISUSE, 0);.    s
19ee0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
19ee1 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
19ee2 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
19ee3 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
19ee4 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70    if( i<1 || i>p
19ee5 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 73 71  ->nVar ){.    sq
19ee6 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62  lite3Error(p->db
19ee7 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20  , SQLITE_RANGE, 
19ee8 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
19ee9 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64  mutex_leave(p->d
19eea 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72  b->mutex);.    r
19eeb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 41 4e  eturn SQLITE_RAN
19eec 47 45 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a 20  GE;.  }.  i--;. 
19eed 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72   pVar = &p->aVar
19eee 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  [i];.  sqlite3Vd
19eef 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 56 61  beMemRelease(pVa
19ef0 72 29 3b 0a 20 20 70 56 61 72 2d 3e 66 6c 61 67  r);.  pVar->flag
19ef1 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
19ef2 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e  sqlite3Error(p->
19ef3 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30  db, SQLITE_OK, 0
19ef4 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
19ef5 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
19ef6 42 69 6e 64 20 61 20 74 65 78 74 20 6f 72 20 42  Bind a text or B
19ef7 4c 4f 42 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74  LOB value..*/.st
19ef8 61 74 69 63 20 69 6e 74 20 62 69 6e 64 54 65 78  atic int bindTex
19ef9 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  t(.  sqlite3_stm
19efa 74 20 2a 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54  t *pStmt,   /* T
19efb 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  he statement to 
19efc 62 69 6e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a  bind against */.
19efd 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20    int i,        
19efe 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
19eff 78 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74  x of the paramet
19f00 65 72 20 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20  er to bind */.  
19f01 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74  const void *zDat
19f02 61 2c 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  a,     /* Pointe
19f03 72 20 74 6f 20 74 68 65 20 64 61 74 61 20 74 6f  r to the data to
19f04 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69   be bound */.  i
19f05 6e 74 20 6e 44 61 74 61 2c 20 20 20 20 20 20 20  nt nData,       
19f06 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
19f07 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
19f08 20 74 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a   to be bound */.
19f09 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
19f0a 6f 69 64 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74  oid*),   /* Dest
19f0b 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 64  ructor for the d
19f0c 61 74 61 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f  ata */.  u8 enco
19f0d 64 69 6e 67 20 20 20 20 20 20 20 20 20 20 20 20  ding            
19f0e 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20  /* Encoding for 
19f0f 74 68 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20  the data */.){. 
19f10 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
19f11 20 2a 29 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20   *)pStmt;.  Mem 
19f12 2a 70 56 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b  *pVar;.  int rc;
19f13 0a 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62  ..  rc = vdbeUnb
19f14 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28  ind(p, i);.  if(
19f15 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
19f16 7b 0a 20 20 20 20 69 66 28 20 7a 44 61 74 61 21  {.    if( zData!
19f17 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 56 61 72  =0 ){.      pVar
19f18 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d   = &p->aVar[i-1]
19f19 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
19f1a 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
19f1b 72 28 70 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e  r(pVar, zData, n
19f1c 44 61 74 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20  Data, encoding, 
19f1d 78 44 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28  xDel);.      if(
19f1e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
19f1f 26 20 65 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b  & encoding!=0 ){
19f20 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
19f21 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
19f22 6e 63 6f 64 69 6e 67 28 70 56 61 72 2c 20 45 4e  ncoding(pVar, EN
19f23 43 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20  C(p->db));.     
19f24 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
19f25 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c  Error(p->db, rc,
19f26 20 30 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   0);.      rc = 
19f27 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70  sqlite3ApiExit(p
19f28 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d  ->db, rc);.    }
19f29 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
19f2a 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e  ex_leave(p->db->
19f2b 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65  mutex);.  }.  re
19f2c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
19f2d 2a 2a 20 42 69 6e 64 20 61 20 62 6c 6f 62 20 76  ** Bind a blob v
19f2e 61 6c 75 65 20 74 6f 20 61 6e 20 53 51 4c 20 73  alue to an SQL s
19f2f 74 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c  tatement variabl
19f30 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
19f31 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
19f32 64 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65  d_blob(.  sqlite
19f33 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a  3_stmt *pStmt, .
19f34 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73    int i, .  cons
19f35 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a  t void *zData, .
19f36 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20    int nData, .  
19f37 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
19f38 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20  d*).){.  return 
19f39 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20  bindText(pStmt, 
19f3a 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c  i, zData, nData,
19f3b 20 78 44 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c   xDel, 0);.}.SQL
19f3c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
19f3d 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28  te3_bind_double(
19f3e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
19f3f 74 6d 74 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62  tmt, int i, doub
19f40 6c 65 20 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e  le rValue){.  in
19f41 74 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20  t rc;.  Vdbe *p 
19f42 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
19f43 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69  .  rc = vdbeUnbi
19f44 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20  nd(p, i);.  if( 
19f45 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
19f46 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19f47 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d  MemSetDouble(&p-
19f48 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c  >aVar[i-1], rVal
19f49 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ue);.    sqlite3
19f4a 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
19f4b 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  db->mutex);.  }.
19f4c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
19f4d 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
19f4e 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73  lite3_bind_int(s
19f4f 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20  qlite3_stmt *p, 
19f50 69 6e 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75  int i, int iValu
19f51 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  e){.  return sql
19f52 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
19f53 70 2c 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75  p, i, (i64)iValu
19f54 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  e);.}.SQLITE_API
19f55 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
19f56 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f  d_int64(sqlite3_
19f57 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
19f58 20 69 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34   i, sqlite_int64
19f59 20 69 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20   iValue){.  int 
19f5a 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  rc;.  Vdbe *p = 
19f5b 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
19f5c 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64   rc = vdbeUnbind
19f5d 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63  (p, i);.  if( rc
19f5e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
19f5f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19f60 6d 53 65 74 49 6e 74 36 34 28 26 70 2d 3e 61 56  mSetInt64(&p->aV
19f61 61 72 5b 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29  ar[i-1], iValue)
19f62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
19f63 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
19f64 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
19f65 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
19f66 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
19f67 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c  e3_bind_null(sql
19f68 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
19f69 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
19f6a 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  rc;.  Vdbe *p = 
19f6b 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
19f6c 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28  rc = vdbeUnbind(
19f6d 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d  p, i);.  if( rc=
19f6e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
19f6f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
19f70 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
19f71 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
19f72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  n rc;.}.SQLITE_A
19f73 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
19f74 69 6e 64 5f 74 65 78 74 28 20 0a 20 20 73 71 6c  ind_text( .  sql
19f75 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
19f76 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63  , .  int i, .  c
19f77 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61  onst char *zData
19f78 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20  , .  int nData, 
19f79 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
19f7a 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75  void*).){.  retu
19f7b 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d  rn bindText(pStm
19f7c 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61  t, i, zData, nDa
19f7d 74 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45  ta, xDel, SQLITE
19f7e 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65  _UTF8);.}.#ifnde
19f7f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
19f80 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  F16.SQLITE_API i
19f81 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
19f82 74 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65  text16(.  sqlite
19f83 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a  3_stmt *pStmt, .
19f84 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73    int i, .  cons
19f85 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a  t void *zData, .
19f86 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20    int nData, .  
19f87 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
19f88 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20  d*).){.  return 
19f89 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20  bindText(pStmt, 
19f8a 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c  i, zData, nData,
19f8b 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54   xDel, SQLITE_UT
19f8c 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65  F16NATIVE);.}.#e
19f8d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
19f8e 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c  MIT_UTF16 */.SQL
19f8f 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
19f90 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73  te3_bind_value(s
19f91 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
19f92 6d 74 2c 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74  mt, int i, const
19f93 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
19f94 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72  pValue){.  int r
19f95 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  c;.  Vdbe *p = (
19f96 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
19f97 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28  rc = vdbeUnbind(
19f98 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d  p, i);.  if( rc=
19f99 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
19f9a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
19f9b 62 65 4d 65 6d 43 6f 70 79 28 26 70 2d 3e 61 56  beMemCopy(&p->aV
19f9c 61 72 5b 69 2d 31 5d 2c 20 70 56 61 6c 75 65 29  ar[i-1], pValue)
19f9d 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
19f9e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
19f9f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
19fa0 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
19fa1 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 45  &p->aVar[i-1], E
19fa2 4e 43 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20  NC(p->db));.    
19fa3 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  }.    sqlite3_mu
19fa4 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
19fa5 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
19fa6 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
19fa7 69 74 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a 20  it(p->db, rc);. 
19fa8 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51   return rc;.}.SQ
19fa9 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
19faa 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c  ite3_bind_zerobl
19fab 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ob(sqlite3_stmt 
19fac 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69  *pStmt, int i, i
19fad 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b  nt n){.  int rc;
19fae 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
19faf 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63  be *)pStmt;.  rc
19fb0 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
19fb1 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   i);.  if( rc==S
19fb2 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
19fb3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19fb4 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56  tZeroBlob(&p->aV
19fb5 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20  ar[i-1], n);.   
19fb6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
19fb7 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
19fb8 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  x);.  }.  return
19fb9 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
19fba 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
19fbb 6f 66 20 77 69 6c 64 63 61 72 64 73 20 74 68 61  of wildcards tha
19fbc 74 20 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 69  t can be potenti
19fbd 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a  ally bound to..*
19fbe 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
19fbf 73 20 61 64 64 65 64 20 74 6f 20 73 75 70 70 6f  s added to suppo
19fc0 72 74 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20  rt DBD::SQLite. 
19fc1 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20   .*/.SQLITE_API 
19fc2 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
19fc3 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
19fc4 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
19fc5 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70  Stmt){.  Vdbe *p
19fc6 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
19fc7 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d  .  return p ? p-
19fc8 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a  >nVar : 0;.}../*
19fc9 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6d 61 70  .** Create a map
19fca 70 69 6e 67 20 66 72 6f 6d 20 76 61 72 69 61 62  ping from variab
19fcb 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f 20 76 61  le numbers to va
19fcc 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20  riable names.** 
19fcd 69 6e 20 74 68 65 20 56 64 62 65 2e 61 7a 56 61  in the Vdbe.azVa
19fce 72 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 73 75  r[] array, if su
19fcf 63 68 20 61 20 6d 61 70 70 69 6e 67 20 64 6f 65  ch a mapping doe
19fd0 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a  s not already.**
19fd1 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69   exist..*/.stati
19fd2 63 20 76 6f 69 64 20 63 72 65 61 74 65 56 61 72  c void createVar
19fd3 4d 61 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  Map(Vdbe *p){.  
19fd4 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b  if( !p->okVar ){
19fd5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
19fd6 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e  ex_enter(p->db->
19fd7 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20  mutex);.    if( 
19fd8 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20  !p->okVar ){.   
19fd9 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
19fda 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 66  Op *pOp;.      f
19fdb 6f 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61  or(j=0, pOp=p->a
19fdc 4f 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b  Op; j<p->nOp; j+
19fdd 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20  +, pOp++){.     
19fde 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f     if( pOp->opco
19fdf 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20  de==OP_Variable 
19fe0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  ){.          ass
19fe1 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26  ert( pOp->p1>0 &
19fe2 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56  & pOp->p1<=p->nV
19fe3 61 72 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ar );.          
19fe4 70 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 31  p->azVar[pOp->p1
19fe5 2d 31 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b  -1] = pOp->p4.z;
19fe6 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
19fe7 20 7d 0a 20 20 20 20 20 20 70 2d 3e 6f 6b 56 61   }.      p->okVa
19fe8 72 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  r = 1;.    }.   
19fe9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
19fea 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
19feb 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  x);.  }.}../*.**
19fec 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   Return the name
19fed 20 6f 66 20 61 20 77 69 6c 64 63 61 72 64 20 70   of a wildcard p
19fee 61 72 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72  arameter.  Retur
19fef 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e  n NULL if the in
19ff0 64 65 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66  dex.** is out of
19ff1 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65   range or if the
19ff2 20 77 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e   wildcard is unn
19ff3 61 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  amed..**.** The 
19ff4 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73  result is always
19ff5 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54   UTF-8..*/.SQLIT
19ff6 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
19ff7 20 2a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   *sqlite3_bind_p
19ff8 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71  arameter_name(sq
19ff9 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
19ffa 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62  t, int i){.  Vdb
19ffb 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53  e *p = (Vdbe*)pS
19ffc 74 6d 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  tmt;.  if( p==0 
19ffd 7c 7c 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e  || i<1 || i>p->n
19ffe 56 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Var ){.    retur
19fff 6e 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74  n 0;.  }.  creat
1a000 65 56 61 72 4d 61 70 28 70 29 3b 0a 20 20 72 65  eVarMap(p);.  re
1a001 74 75 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d  turn p->azVar[i-
1a002 31 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76  1];.}../*.** Giv
1a003 65 6e 20 61 20 77 69 6c 64 63 61 72 64 20 70 61  en a wildcard pa
1a004 72 61 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65  rameter name, re
1a005 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f  turn the index o
1a006 66 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a  f the variable.*
1a007 2a 20 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65  * with that name
1a008 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  .  If there is n
1a009 6f 20 76 61 72 69 61 62 6c 65 20 77 69 74 68 20  o variable with 
1a00a 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a  the given name,.
1a00b 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a  ** return 0..*/.
1a00c 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1a00d 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
1a00e 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69  meter_index(sqli
1a00f 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1a010 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1a011 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d  me){.  Vdbe *p =
1a012 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
1a013 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d   int i;.  if( p=
1a014 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1a015 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65   0;.  }.  create
1a016 56 61 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66  VarMap(p); .  if
1a017 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66  ( zName ){.    f
1a018 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61  or(i=0; i<p->nVa
1a019 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63  r; i++){.      c
1a01a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70  onst char *z = p
1a01b 2d 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20  ->azVar[i];.    
1a01c 20 20 69 66 28 20 7a 20 26 26 20 73 74 72 63 6d    if( z && strcm
1a01d 70 28 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  p(z,zName)==0 ){
1a01e 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1a01f 69 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i+1;.      }.   
1a020 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1a021 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e  0;.}../*.** Tran
1a022 73 66 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67  sfer all binding
1a023 73 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74  s from the first
1a024 20 73 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20   statement over 
1a025 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a  to the second..*
1a026 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 61  * If the two sta
1a027 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20  tements contain 
1a028 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62  a different numb
1a029 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20  er of bindings, 
1a02a 74 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54  then.** an SQLIT
1a02b 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72  E_ERROR is retur
1a02c 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
1a02d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1a02e 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e  e3TransferBindin
1a02f 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  gs(sqlite3_stmt 
1a030 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69  *pFromStmt, sqli
1a031 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d  te3_stmt *pToStm
1a032 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f  t){.  Vdbe *pFro
1a033 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d  m = (Vdbe*)pFrom
1a034 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54  Stmt;.  Vdbe *pT
1a035 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74  o = (Vdbe*)pToSt
1a036 6d 74 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 20  mt;.  int i, rc 
1a037 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
1a038 66 28 20 28 70 46 72 6f 6d 2d 3e 6d 61 67 69 63  f( (pFrom->magic
1a039 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  !=VDBE_MAGIC_RUN
1a03a 20 26 26 20 70 46 72 6f 6d 2d 3e 6d 61 67 69 63   && pFrom->magic
1a03b 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c  !=VDBE_MAGIC_HAL
1a03c 54 29 0a 20 20 20 20 7c 7c 20 28 70 54 6f 2d 3e  T).    || (pTo->
1a03d 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49  magic!=VDBE_MAGI
1a03e 43 5f 52 55 4e 20 26 26 20 70 54 6f 2d 3e 6d 61  C_RUN && pTo->ma
1a03f 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic!=VDBE_MAGIC_
1a040 48 41 4c 54 29 0a 20 20 20 20 7c 7c 20 70 54 6f  HALT).    || pTo
1a041 2d 3e 64 62 21 3d 70 46 72 6f 6d 2d 3e 64 62 20  ->db!=pFrom->db 
1a042 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
1a043 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
1a044 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56  .  if( pFrom->nV
1a045 61 72 21 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b  ar!=pTo->nVar ){
1a046 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1a047 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
1a048 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1a049 74 65 72 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74  ter(pTo->db->mut
1a04a 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ex);.  for(i=0; 
1a04b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1a04c 20 69 3c 70 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20   i<pFrom->nVar; 
1a04d 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
1a04e 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 70 54  3VdbeMemMove(&pT
1a04f 6f 2d 3e 61 56 61 72 5b 69 5d 2c 20 26 70 46 72  o->aVar[i], &pFr
1a050 6f 6d 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20  om->aVar[i]);.  
1a051 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
1a052 78 5f 6c 65 61 76 65 28 70 54 6f 2d 3e 64 62 2d  x_leave(pTo->db-
1a053 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72  >mutex);.  asser
1a054 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
1a055 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   || rc==SQLITE_N
1a056 4f 4d 45 4d 20 29 3b 0a 20 20 72 65 74 75 72 6e  OMEM );.  return
1a057 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20   rc;.}..#ifndef 
1a058 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
1a059 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70  ECATED./*.** Dep
1a05a 72 65 63 61 74 65 64 20 65 78 74 65 72 6e 61 6c  recated external
1a05b 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 6e 74   interface.  Int
1a05c 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74  ernal/core SQLit
1a05d 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64  e code.** should
1a05e 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 54 72 61   call sqlite3Tra
1a05f 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 2e 0a 2a  nsferBindings..*
1a060 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1a061 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65   sqlite3_transfe
1a062 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  r_bindings(sqlit
1a063 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74  e3_stmt *pFromSt
1a064 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  mt, sqlite3_stmt
1a065 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 72 65   *pToStmt){.  re
1a066 74 75 72 6e 20 73 71 6c 69 74 65 33 54 72 61 6e  turn sqlite3Tran
1a067 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70 46 72  sferBindings(pFr
1a068 6f 6d 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29  omStmt, pToStmt)
1a069 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1a06a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 71 6c  * Return the sql
1a06b 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68  ite3* database h
1a06c 61 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20 74  andle to which t
1a06d 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
1a06e 65 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69  ement given.** i
1a06f 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 62  n the argument b
1a070 65 6c 6f 6e 67 73 2e 20 20 54 68 69 73 20 69 73  elongs.  This is
1a071 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
1a072 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77  se handle that w
1a073 61 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  as.** the first 
1a074 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
1a075 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
1a076 29 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20  ) that was used 
1a077 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68 65  to create.** the
1a078 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68   statement in th
1a079 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a  e first place..*
1a07a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
1a07b 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62  ite3 *sqlite3_db
1a07c 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f  _handle(sqlite3_
1a07d 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
1a07e 72 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20 28  return pStmt ? (
1a07f 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64  (Vdbe*)pStmt)->d
1a080 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  b : 0;.}../*.** 
1a081 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
1a082 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70 72 65   to the next pre
1a083 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
1a084 61 66 74 65 72 20 70 53 74 6d 74 20 61 73 73 6f  after pStmt asso
1a085 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 64  ciated.** with d
1a086 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1a087 6f 6e 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d  on pDb.  If pStm
1a088 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72  t is NULL, retur
1a089 6e 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70  n the first.** p
1a08a 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1a08b 74 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61  t for the databa
1a08c 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20  se connection.  
1a08d 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74  Return NULL if t
1a08e 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d  here.** are no m
1a08f 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ore..*/.SQLITE_A
1a090 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  PI sqlite3_stmt 
1a091 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74  *sqlite3_next_st
1a092 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c  mt(sqlite3 *pDb,
1a093 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
1a094 53 74 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  Stmt){.  sqlite3
1a095 5f 73 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20  _stmt *pNext;.  
1a096 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1a097 74 65 72 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b  ter(pDb->mutex);
1a098 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20  .  if( pStmt==0 
1a099 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28  ){.    pNext = (
1a09a 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 44  sqlite3_stmt*)pD
1a09b 62 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73  b->pVdbe;.  }els
1a09c 65 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28  e{.    pNext = (
1a09d 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28 28  sqlite3_stmt*)((
1a09e 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e  Vdbe*)pStmt)->pN
1a09f 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ext;.  }.  sqlit
1a0a0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
1a0a1 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  Db->mutex);.  re
1a0a2 74 75 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f  turn pNext;.}../
1a0a3 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1a0a4 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75  value of a statu
1a0a5 73 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 61 20  s counter for a 
1a0a6 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1a0a7 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  nt.*/.SQLITE_API
1a0a8 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d   int sqlite3_stm
1a0a9 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33  t_status(sqlite3
1a0aa 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1a0ab 74 20 6f 70 2c 20 69 6e 74 20 72 65 73 65 74 46  t op, int resetF
1a0ac 6c 61 67 29 7b 0a 20 20 56 64 62 65 20 2a 70 56  lag){.  Vdbe *pV
1a0ad 64 62 65 20 3d 20 28 56 64 62 65 2a 29 70 53 74  dbe = (Vdbe*)pSt
1a0ae 6d 74 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70 56  mt;.  int v = pV
1a0af 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70  dbe->aCounter[op
1a0b0 2d 31 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74  -1];.  if( reset
1a0b1 46 6c 61 67 20 29 20 70 56 64 62 65 2d 3e 61 43  Flag ) pVdbe->aC
1a0b2 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30  ounter[op-1] = 0
1a0b3 3b 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a  ;.  return v;.}.
1a0b4 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1a0b5 20 45 6e 64 20 6f 66 20 76 64 62 65 61 70 69 2e   End of vdbeapi.
1a0b6 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1a0b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1a0b9 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1a0ba 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
1a0bb 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1a0bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1a0be 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
1a0bf 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
1a0c0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1a0c1 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1a0c2 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1a0c3 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1a0c4 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1a0c5 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1a0c6 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1a0c7 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1a0c8 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1a0c9 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1a0ca 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1a0cb 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1a0cc 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1a0cd 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1a0ce 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1a0cf 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1a0d0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1a0d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0d5 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63  *******.** The c
1a0d6 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
1a0d7 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 63   implements exec
1a0d8 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20  ution method of 
1a0d9 74 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20  the .** Virtual 
1a0da 44 61 74 61 62 61 73 65 20 45 6e 67 69 6e 65 20  Database Engine 
1a0db 28 56 44 42 45 29 2e 20 20 41 20 73 65 70 61 72  (VDBE).  A separ
1a0dc 61 74 65 20 66 69 6c 65 20 28 22 76 64 62 65 61  ate file ("vdbea
1a0dd 75 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65  ux.c").** handle
1a0de 73 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 64  s housekeeping d
1a0df 65 74 61 69 6c 73 20 73 75 63 68 20 61 73 20 63  etails such as c
1a0e0 72 65 61 74 69 6e 67 20 61 6e 64 20 64 65 6c 65  reating and dele
1a0e1 74 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73  ting.** VDBE ins
1a0e2 74 61 6e 63 65 73 2e 20 20 54 68 69 73 20 66 69  tances.  This fi
1a0e3 6c 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74  le is solely int
1a0e4 65 72 65 73 74 65 64 20 69 6e 20 65 78 65 63 75  erested in execu
1a0e5 74 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 45  ting.** the VDBE
1a0e6 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20   program..**.** 
1a0e7 49 6e 20 74 68 65 20 65 78 74 65 72 6e 61 6c 20  In the external 
1a0e8 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 22 73  interface, an "s
1a0e9 71 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 73  qlite3_stmt*" is
1a0ea 20 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e 74   an opaque point
1a0eb 65 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e  er.** to a VDBE.
1a0ec 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70  .**.** The SQL p
1a0ed 61 72 73 65 72 20 67 65 6e 65 72 61 74 65 73 20  arser generates 
1a0ee 61 20 70 72 6f 67 72 61 6d 20 77 68 69 63 68 20  a program which 
1a0ef 69 73 20 74 68 65 6e 20 65 78 65 63 75 74 65 64  is then executed
1a0f0 20 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 20   by.** the VDBE 
1a0f1 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f  to do the work o
1a0f2 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  f the SQL statem
1a0f3 65 6e 74 2e 20 20 56 44 42 45 20 70 72 6f 67 72  ent.  VDBE progr
1a0f4 61 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69  ams are .** simi
1a0f5 6c 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61  lar in form to a
1a0f6 73 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65  ssembly language
1a0f7 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63  .  The program c
1a0f8 6f 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20  onsists of.** a 
1a0f9 6c 69 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20  linear sequence 
1a0fa 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20  of operations.  
1a0fb 45 61 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68  Each operation h
1a0fc 61 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a  as an opcode .**
1a0fd 20 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e   and 5 operands.
1a0fe 20 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50    Operands P1, P
1a0ff 32 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e  2, and P3 are in
1a100 74 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64  tegers.  Operand
1a101 20 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c   P4 .** is a nul
1a102 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  l-terminated str
1a103 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35  ing.  Operand P5
1a104 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20   is an unsigned 
1a105 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65  character..** Fe
1a106 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61 6c  w opcodes use al
1a107 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a  l 5 operands..**
1a108 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20  .** Computation 
1a109 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72  results are stor
1a10a 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 72  ed on a set of r
1a10b 65 67 69 73 74 65 72 73 20 6e 75 6d 62 65 72 65  egisters numbere
1a10c 64 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77  d beginning.** w
1a10d 69 74 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 20  ith 1 and going 
1a10e 75 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e  up to Vdbe.nMem.
1a10f 20 20 45 61 63 68 20 72 65 67 69 73 74 65 72 20    Each register 
1a110 63 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74  can store.** eit
1a111 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 2c 20  her an integer, 
1a112 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  a null-terminate
1a113 64 20 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61  d string, a floa
1a114 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75  ting point.** nu
1a115 6d 62 65 72 2c 20 6f 72 20 74 68 65 20 53 51 4c  mber, or the SQL
1a116 20 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20   "NULL" value.  
1a117 41 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76  An implicit conv
1a118 65 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a  ersion from one.
1a119 2a 2a 20 74 79 70 65 20 74 6f 20 74 68 65 20 6f  ** type to the o
1a11a 74 68 65 72 20 6f 63 63 75 72 73 20 61 73 20 6e  ther occurs as n
1a11b 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a  ecessary..** .**
1a11c 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64   Most of the cod
1a11d 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
1a11e 73 20 74 61 6b 65 6e 20 75 70 20 62 79 20 74 68  s taken up by th
1a11f 65 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  e sqlite3VdbeExe
1a120 63 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  c().** function 
1a121 77 68 69 63 68 20 64 6f 65 73 20 74 68 65 20 77  which does the w
1a122 6f 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 74  ork of interpret
1a123 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72  ing a VDBE progr
1a124 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72  am..** But other
1a125 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c   routines are al
1a126 73 6f 20 70 72 6f 76 69 64 65 64 20 74 6f 20 68  so provided to h
1a127 65 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 20  elp in building 
1a128 75 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20  up.** a program 
1a129 69 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 20 69  instruction by i
1a12a 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
1a12b 2a 20 56 61 72 69 6f 75 73 20 73 63 72 69 70 74  * Various script
1a12c 73 20 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72  s scan this sour
1a12d 63 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72  ce file in order
1a12e 20 74 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d   to generate HTM
1a12f 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69  L.** documentati
1a130 6f 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65  on, headers file
1a131 73 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69  s, or other deri
1a132 76 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20  ved files.  The 
1a133 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66  formatting.** of
1a134 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   the code in thi
1a135 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65  s file is, there
1a136 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e  fore, important.
1a137 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d    See other comm
1a138 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20  ents.** in this 
1a139 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73  file for details
1a13a 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c 20  .  If in doubt, 
1a13b 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66  do not deviate f
1a13c 72 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20  rom existing.** 
1a13d 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69  commenting and i
1a13e 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 74  ndentation pract
1a13f 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e 67 69  ices when changi
1a140 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63 6f 64  ng or adding cod
1a141 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64  e..**.** $Id: vd
1a142 62 65 2e 63 2c 76 20 31 2e 38 31 31 20 32 30 30  be.c,v 1.811 200
1a143 39 2f 30 31 2f 31 34 20 30 30 3a 35 35 3a 31 30  9/01/14 00:55:10
1a144 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
1a145 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1a146 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  ng global variab
1a147 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  le is incremente
1a148 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 20 63  d every time a c
1a149 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20  ursor.** moves, 
1a14a 65 69 74 68 65 72 20 62 79 20 74 68 65 20 4f 50  either by the OP
1a14b 5f 53 65 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74  _SeekXX, OP_Next
1a14c 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70 63  , or OP_Prev opc
1a14d 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 74 0a  odes.  The test.
1a14e 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 75 73  ** procedures us
1a14f 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  e this informati
1a150 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  on to make sure 
1a151 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72 65  that indices are
1a152 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72  .** working corr
1a153 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 61 72  ectly.  This var
1a154 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e  iable has no fun
1a155 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e  ction other than
1a156 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69   to.** help veri
1a157 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  fy the correct o
1a158 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  peration of the 
1a159 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64  library..*/.#ifd
1a15a 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
1a15b 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1a15c 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75  lite3_search_cou
1a15d 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
1a15e 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  /*.** When this 
1a15f 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1a160 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 74 20  is positive, it 
1a161 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 65 64  gets decremented
1a162 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20   once before.** 
1a163 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e  each instruction
1a164 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 57   in the VDBE.  W
1a165 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65 72 6f  hen reaches zero
1a166 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74 65 72  , the u1.isInter
1a167 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20  rupted.** field 
1a168 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 20 73  of the sqlite3 s
1a169 74 72 75 63 74 75 72 65 20 69 73 20 73 65 74 20  tructure is set 
1a16a 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 6d 75  in order to simu
1a16b 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72 72 75  late and interru
1a16c 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  pt..**.** This f
1a16d 61 63 69 6c 69 74 79 20 69 73 20 75 73 65 64 20  acility is used 
1a16e 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70  for testing purp
1a16f 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64  oses only.  It d
1a170 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e  oes not function
1a171 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61  .** in an ordina
1a172 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66  ry build..*/.#if
1a173 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
1a174 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1a175 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1a176 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64  _count = 0;.#end
1a177 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65  if../*.** The ne
1a178 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  xt global variab
1a179 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  le is incremente
1a17a 64 20 65 61 63 68 20 74 79 70 65 20 74 68 65 20  d each type the 
1a17b 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a  OP_Sort opcode.*
1a17c 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 20  * is executed.  
1a17d 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 75  The test procedu
1a17e 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e 66  res use this inf
1a17f 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65  ormation to make
1a180 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f   sure that.** so
1a181 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 72 69  rting is occurri
1a182 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72  ng or not occurr
1a183 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69 61  ing at appropria
1a184 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69 73  te times.   This
1a185 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73   variable.** has
1a186 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68   no function oth
1a187 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 20  er than to help 
1a188 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65  verify the corre
1a189 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20  ct operation of 
1a18a 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a  the.** library..
1a18b 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1a18c 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
1a18d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f 72   int sqlite3_sor
1a18e 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  t_count = 0;.#en
1a18f 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  dif../*.** The n
1a190 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ext global varia
1a191 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65 20  ble records the 
1a192 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67  size of the larg
1a193 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20  est MEM_Blob.** 
1a194 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 74 20  or MEM_Str that 
1a195 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 79  has been used by
1a196 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 2e 20   a VDBE opcode. 
1a197 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64   The test proced
1a198 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 69 73  ures.** use this
1a199 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
1a19a 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
1a19b 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e  he zero-blob fun
1a19c 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73  ctionality.** is
1a19d 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74   working correct
1a19e 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 69 61  ly.   This varia
1a19f 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74  ble has no funct
1a1a0 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74  ion other than t
1a1a1 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79  o.** help verify
1a1a2 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65   the correct ope
1a1a3 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  ration of the li
1a1a4 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66  brary..*/.#ifdef
1a1a5 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
1a1a6 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1a1a7 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65  te3_max_blobsize
1a1a8 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f 69   = 0;.static voi
1a1a9 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73  d updateMaxBlobs
1a1aa 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69  ize(Mem *p){.  i
1a1ab 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28  f( (p->flags & (
1a1ac 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
1a1ad 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71  ))!=0 && p->n>sq
1a1ae 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69  lite3_max_blobsi
1a1af 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ze ){.    sqlite
1a1b0 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d  3_max_blobsize =
1a1b1 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e   p->n;.  }.}.#en
1a1b2 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20  dif../*.** Test 
1a1b3 61 20 72 65 67 69 73 74 65 72 20 74 6f 20 73 65  a register to se
1a1b4 65 20 69 66 20 69 74 20 65 78 63 65 65 64 73 20  e if it exceeds 
1a1b5 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69  the current maxi
1a1b6 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a  mum blob size..*
1a1b7 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65  * If it does, re
1a1b8 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78  cord the new max
1a1b9 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a  imum blob size..
1a1ba 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
1a1bb 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 21  QLITE_TEST) && !
1a1bc 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1a1bd 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
1a1be 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41 54  ).# define UPDAT
1a1bf 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50  E_MAX_BLOBSIZE(P
1a1c0 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62  )  updateMaxBlob
1a1c1 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20  size(P).#else.# 
1a1c2 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41  define UPDATE_MA
1a1c3 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65  X_BLOBSIZE(P).#e
1a1c4 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  ndif../*.** Conv
1a1c5 65 72 74 20 74 68 65 20 67 69 76 65 6e 20 72 65  ert the given re
1a1c6 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74  gister into a st
1a1c7 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74  ring if it isn't
1a1c8 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e   one.** already.
1a1c9 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f   Return non-zero
1a1ca 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   if a malloc() f
1a1cb 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ails..*/.#define
1a1cc 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e   Stringify(P, en
1a1cd 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d  c) \.   if(((P)-
1a1ce 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c  >flags&(MEM_Str|
1a1cf 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26  MEM_Blob))==0 &&
1a1d0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1a1d1 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29  tringify(P,enc))
1a1d2 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e   \.     { goto n
1a1d3 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20  o_mem; }../*.** 
1a1d4 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72  An ephemeral str
1a1d5 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69  ing value (signi
1a1d6 66 69 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f  fied by the MEM_
1a1d7 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74  Ephem flag) cont
1a1d8 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  ains.** a pointe
1a1d9 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c  r to a dynamical
1a1da 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72  ly allocated str
1a1db 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f  ing where some o
1a1dc 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69  ther entity.** i
1a1dd 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
1a1de 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74  r deallocating t
1a1df 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63  hat string.  Bec
1a1e0 61 75 73 65 20 74 68 65 20 72 65 67 69 73 74 65  ause the registe
1a1e1 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f  r.** does not co
1a1e2 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67  ntrol the string
1a1e3 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 65  , it might be de
1a1e4 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20 74 68  leted without th
1a1e5 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e  e register.** kn
1a1e6 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  owing it..**.** 
1a1e7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e  This routine con
1a1e8 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72  verts an ephemer
1a1e9 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61  al string into a
1a1ea 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
1a1eb 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67  ocated.** string
1a1ec 20 74 68 61 74 20 74 68 65 20 72 65 67 69 73 74   that the regist
1a1ed 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f  er itself contro
1a1ee 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  ls.  In other wo
1a1ef 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65  rds, it.** conve
1a1f0 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d  rts an MEM_Ephem
1a1f1 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20   string into an 
1a1f2 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a  MEM_Dyn string..
1a1f3 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68  */.#define Deeph
1a1f4 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20  emeralize(P) \. 
1a1f5 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67    if( ((P)->flag
1a1f6 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20  s&MEM_Ephem)!=0 
1a1f7 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69  \.       && sqli
1a1f8 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72  te3VdbeMemMakeWr
1a1f9 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f  iteable(P) ){ go
1a1fa 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a  to no_mem;}../*.
1a1fb 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56  ** Call sqlite3V
1a1fc 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
1a1fd 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69  () on the suppli
1a1fe 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d  ed value (type M
1a1ff 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71  em*).** P if req
1a200 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  uired..*/.#defin
1a201 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20  e ExpandBlob(P) 
1a202 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d  (((P)->flags&MEM
1a203 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64  _Zero)?sqlite3Vd
1a204 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28  beMemExpandBlob(
1a205 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67  P):0)../*.** Arg
1a206 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74  ument pMem point
1a207 73 20 61 74 20 61 20 72 65 67 69 73 74 65 72 20  s at a register 
1a208 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73  that will be pas
1a209 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72  sed to a.** user
1a20a 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
1a20b 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f  n or returned to
1a20c 20 74 68 65 20 75 73 65 72 20 61 73 20 74 68 65   the user as the
1a20d 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65   result of a que
1a20e 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  ry..** The secon
1a20f 64 20 61 72 67 75 6d 65 6e 74 2c 20 27 64 62 5f  d argument, 'db_
1a210 65 6e 63 27 20 69 73 20 74 68 65 20 74 65 78 74  enc' is the text
1a211 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62   encoding used b
1a212 79 20 74 68 65 20 76 64 62 65 20 66 6f 72 0a 2a  y the vdbe for.*
1a213 2a 20 72 65 67 69 73 74 65 72 20 76 61 72 69 61  * register varia
1a214 62 6c 65 73 2e 20 20 54 68 69 73 20 72 6f 75 74  bles.  This rout
1a215 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 4d 65  ine sets the pMe
1a216 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d 65 6d 2d  m->enc and pMem-
1a217 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69 61 62 6c  >type.** variabl
1a218 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73  es used by the s
1a219 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29  qlite3_value_*()
1a21a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 64   routines..*/.#d
1a21b 65 66 69 6e 65 20 73 74 6f 72 65 54 79 70 65 49  efine storeTypeI
1a21c 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f 72 65 54  nfo(A,B) _storeT
1a21d 79 70 65 49 6e 66 6f 28 41 29 0a 73 74 61 74 69  ypeInfo(A).stati
1a21e 63 20 76 6f 69 64 20 5f 73 74 6f 72 65 54 79 70  c void _storeTyp
1a21f 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d 29  eInfo(Mem *pMem)
1a220 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20  {.  int flags = 
1a221 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69  pMem->flags;.  i
1a222 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  f( flags & MEM_N
1a223 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  ull ){.    pMem-
1a224 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e  >type = SQLITE_N
1a225 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20  ULL;.  }.  else 
1a226 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f  if( flags & MEM_
1a227 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  Int ){.    pMem-
1a228 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
1a229 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65 6c  NTEGER;.  }.  el
1a22a 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  se if( flags & M
1a22b 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 70  EM_Real ){.    p
1a22c 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1a22d 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 20  TE_FLOAT;.  }.  
1a22e 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
1a22f 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20   MEM_Str ){.    
1a230 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1a231 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c 73  ITE_TEXT;.  }els
1a232 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  e{.    pMem->typ
1a233 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b  e = SQLITE_BLOB;
1a234 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  .  }.}../*.** Pr
1a235 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 63 6f  operties of opco
1a236 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f  des.  The OPFLG_
1a237 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 72  INITIALIZER macr
1a238 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20  o is.** created 
1a239 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b  by mkopcodeh.awk
1a23a 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74   during compilat
1a23b 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 6f 62  ion.  Data is ob
1a23c 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74  tained.** from t
1a23d 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c  he comments foll
1a23e 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 20  owing the "case 
1a23f 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 65 6d  OP_xxxx:" statem
1a240 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 69 73 20  ents in.** this 
1a241 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69  file.  .*/.stati
1a242 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
1a243 20 63 68 61 72 20 6f 70 63 6f 64 65 50 72 6f 70   char opcodeProp
1a244 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49  erty[] = OPFLG_I
1a245 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 0a  NITIALIZER;../*.
1a246 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
1a247 66 20 61 6e 20 6f 70 63 6f 64 65 20 68 61 73 20  f an opcode has 
1a248 61 6e 79 20 6f 66 20 74 68 65 20 4f 50 46 4c 47  any of the OPFLG
1a249 5f 78 78 78 20 70 72 6f 70 65 72 74 69 65 73 0a  _xxx properties.
1a24a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ** specified by 
1a24b 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mask..*/.SQLITE_
1a24c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1a24d 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73  te3VdbeOpcodeHas
1a24e 50 72 6f 70 65 72 74 79 28 69 6e 74 20 6f 70 63  Property(int opc
1a24f 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a  ode, int mask){.
1a250 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65    assert( opcode
1a251 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c 28 69 6e  >0 && opcode<(in
1a252 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f 64 65 50  t)sizeof(opcodeP
1a253 72 6f 70 65 72 74 79 29 20 29 3b 0a 20 20 72 65  roperty) );.  re
1a254 74 75 72 6e 20 28 6f 70 63 6f 64 65 50 72 6f 70  turn (opcodeProp
1a255 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26 6d 61 73  erty[opcode]&mas
1a256 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  k)!=0;.}../*.** 
1a257 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 75 72  Allocate VdbeCur
1a258 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2e  sor number iCur.
1a259 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
1a25a 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74 75 72  er to it.  Retur
1a25b 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20  n NULL.** if we 
1a25c 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  run out of memor
1a25d 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 64 62  y..*/.static Vdb
1a25e 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 61 74  eCursor *allocat
1a25f 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62 65 20  eCursor(.  Vdbe 
1a260 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
1a261 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
1a262 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  machine */.  int
1a263 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20   iCur,          
1a264 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
1a265 68 65 20 6e 65 77 20 56 64 62 65 43 75 72 73 6f  he new VdbeCurso
1a266 72 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 2c 20  r */.  Op *pOp, 
1a267 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a268 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
1a269 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68             /* Wh
1a26a 65 6e 20 64 61 74 61 62 61 73 65 20 74 68 65 20  en database the 
1a26b 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74  cursor belongs t
1a26c 6f 2c 20 6f 72 20 2d 31 20 2a 2f 0a 20 20 69 6e  o, or -1 */.  in
1a26d 74 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 20  t isBtreeCursor 
1a26e 20 20 20 20 2f 2a 20 2a 2f 0a 29 7b 0a 20 20 2f      /* */.){.  /
1a26f 2a 20 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72  * Find the memor
1a270 79 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c  y cell that will
1a271 20 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72   be used to stor
1a272 65 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65  e the blob of me
1a273 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72  mory.  ** requir
1a274 65 64 20 66 6f 72 20 74 68 69 73 20 56 64 62 65  ed for this Vdbe
1a275 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  Cursor structure
1a276 2e 20 49 74 20 69 73 20 63 6f 6e 76 65 6e 69 65  . It is convenie
1a277 6e 74 20 74 6f 20 75 73 65 20 61 20 0a 20 20 2a  nt to use a .  *
1a278 2a 20 76 64 62 65 20 6d 65 6d 6f 72 79 20 63 65  * vdbe memory ce
1a279 6c 6c 20 74 6f 20 6d 61 6e 61 67 65 20 74 68 65  ll to manage the
1a27a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
1a27b 6f 6e 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  on required for 
1a27c 61 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f  a.  ** VdbeCurso
1a27d 72 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  r structure for 
1a27e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65  the following re
1a27f 61 73 6f 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  asons:.  **.  **
1a280 20 20 20 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63     * Sometimes c
1a281 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72  ursor numbers ar
1a282 65 20 75 73 65 64 20 66 6f 72 20 61 20 63 6f 75  e used for a cou
1a283 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74  ple of different
1a284 0a 20 20 2a 2a 20 20 20 20 20 70 75 72 70 6f 73  .  **     purpos
1a285 65 73 20 69 6e 20 61 20 76 64 62 65 20 70 72 6f  es in a vdbe pro
1a286 67 72 61 6d 2e 20 54 68 65 20 64 69 66 66 65 72  gram. The differ
1a287 65 6e 74 20 75 73 65 73 20 6d 69 67 68 74 20 72  ent uses might r
1a288 65 71 75 69 72 65 0a 20 20 2a 2a 20 20 20 20 20  equire.  **     
1a289 64 69 66 66 65 72 65 6e 74 20 73 69 7a 65 64 20  different sized 
1a28a 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d  allocations. Mem
1a28b 6f 72 79 20 63 65 6c 6c 73 20 70 72 6f 76 69 64  ory cells provid
1a28c 65 20 67 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20  e growable.  ** 
1a28d 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e      allocations.
1a28e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57  .  **.  **   * W
1a28f 68 65 6e 20 75 73 69 6e 67 20 45 4e 41 42 4c 45  hen using ENABLE
1a290 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
1a291 4e 54 2c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  NT, memory cell 
1a292 62 75 66 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a  buffers can.  **
1a293 20 20 20 20 20 62 65 20 66 72 65 65 64 20 6c 61       be freed la
1a294 7a 69 6c 79 20 76 69 61 20 74 68 65 20 73 71 6c  zily via the sql
1a295 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
1a296 6f 72 79 28 29 20 41 50 49 2e 20 54 68 69 73 0a  ory() API. This.
1a297 20 20 2a 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a    **     minimiz
1a298 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  es the number of
1a299 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61   malloc calls ma
1a29a 64 65 20 62 79 20 74 68 65 20 73 79 73 74 65 6d  de by the system
1a29b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f  ..  **.  ** Memo
1a29c 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 75 72  ry cells for cur
1a29d 73 6f 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74  sors are allocat
1a29e 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66  ed at the top of
1a29f 20 74 68 65 20 61 64 64 72 65 73 73 0a 20 20 2a   the address.  *
1a2a0 2a 20 73 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20  * space. Memory 
1a2a1 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63  cell (p->nMem) c
1a2a2 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 75  orresponds to cu
1a2a3 72 73 6f 72 20 30 2e 20 53 70 61 63 65 20 66 6f  rsor 0. Space fo
1a2a4 72 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 31 20  r.  ** cursor 1 
1a2a5 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 6d 65  is managed by me
1a2a6 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d  mory cell (p->nM
1a2a7 65 6d 2d 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f  em-1), etc..  */
1a2a8 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26  .  Mem *pMem = &
1a2a9 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d  p->aMem[p->nMem-
1a2aa 69 43 75 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42  iCur];..  int nB
1a2ab 79 74 65 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  yte;.  VdbeCurso
1a2ac 72 20 2a 70 43 78 20 3d 20 30 3b 0a 20 20 2f 2a  r *pCx = 0;.  /*
1a2ad 20 49 66 20 74 68 65 20 6f 70 63 6f 64 65 20 6f   If the opcode o
1a2ae 66 20 70 4f 70 20 69 73 20 4f 50 5f 53 65 74 4e  f pOp is OP_SetN
1a2af 75 6d 43 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20  umColumns, then 
1a2b0 70 4f 70 2d 3e 70 32 20 63 6f 6e 74 61 69 6e 73  pOp->p2 contains
1a2b1 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  .  ** the number
1a2b2 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68   of fields in th
1a2b3 65 20 72 65 63 6f 72 64 73 20 63 6f 6e 74 61 69  e records contai
1a2b4 6e 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ned in the table
1a2b5 20 6f 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20 62   or.  ** index b
1a2b6 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 55 73 65  eing opened. Use
1a2b7 20 74 68 69 73 20 74 6f 20 72 65 73 65 72 76 65   this to reserve
1a2b8 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 0a   space for the .
1a2b9 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2e    ** VdbeCursor.
1a2ba 61 54 79 70 65 5b 5d 20 61 72 72 61 79 2e 0a 20  aType[] array.. 
1a2bb 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64   */.  int nField
1a2bc 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4f 70 2d   = 0;.  if( pOp-
1a2bd 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 65 74 4e  >opcode==OP_SetN
1a2be 75 6d 43 6f 6c 75 6d 6e 73 20 7c 7c 20 70 4f 70  umColumns || pOp
1a2bf 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65  ->opcode==OP_Ope
1a2c0 6e 45 70 68 65 6d 65 72 61 6c 20 29 7b 0a 20 20  nEphemeral ){.  
1a2c1 20 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e    nField = pOp->
1a2c2 70 32 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20  p2;.  }.  nByte 
1a2c3 3d 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28  = .      sizeof(
1a2c4 56 64 62 65 43 75 72 73 6f 72 29 20 2b 20 0a 20  VdbeCursor) + . 
1a2c5 20 20 20 20 20 28 69 73 42 74 72 65 65 43 75 72       (isBtreeCur
1a2c6 73 6f 72 3f 73 71 6c 69 74 65 33 42 74 72 65 65  sor?sqlite3Btree
1a2c7 43 75 72 73 6f 72 53 69 7a 65 28 29 3a 30 29 20  CursorSize():0) 
1a2c8 2b 20 0a 20 20 20 20 20 20 32 2a 6e 46 69 65 6c  + .      2*nFiel
1a2c9 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a  d*sizeof(u32);..
1a2ca 20 20 61 73 73 65 72 74 28 20 69 43 75 72 3c 70    assert( iCur<p
1a2cb 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 69  ->nCursor );.  i
1a2cc 66 28 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72  f( p->apCsr[iCur
1a2cd 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
1a2ce 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70  VdbeFreeCursor(p
1a2cf 2c 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d  , p->apCsr[iCur]
1a2d0 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b  );.    p->apCsr[
1a2d1 69 43 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20  iCur] = 0;.  }. 
1a2d2 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
1a2d3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1a2d4 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20  ow(pMem, nByte, 
1a2d5 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43  0) ){.    p->apC
1a2d6 73 72 5b 69 43 75 72 5d 20 3d 20 70 43 78 20 3d  sr[iCur] = pCx =
1a2d7 20 28 56 64 62 65 43 75 72 73 6f 72 2a 29 70 4d   (VdbeCursor*)pM
1a2d8 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65  em->z;.    memse
1a2d9 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42  t(pMem->z, 0, nB
1a2da 79 74 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69  yte);.    pCx->i
1a2db 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43  Db = iDb;.    pC
1a2dc 78 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65  x->nField = nFie
1a2dd 6c 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65  ld;.    if( nFie
1a2de 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d  ld ){.      pCx-
1a2df 3e 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29  >aType = (u32 *)
1a2e0 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28  &pMem->z[sizeof(
1a2e1 56 64 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20  VdbeCursor)];.  
1a2e2 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 42 74    }.    if( isBt
1a2e3 72 65 65 43 75 72 73 6f 72 20 29 7b 0a 20 20 20  reeCursor ){.   
1a2e4 20 20 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20     pCx->pCursor 
1a2e5 3d 20 28 42 74 43 75 72 73 6f 72 2a 29 0a 20 20  = (BtCursor*).  
1a2e6 20 20 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a          &pMem->z
1a2e7 5b 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73  [sizeof(VdbeCurs
1a2e8 6f 72 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a  or)+2*nField*siz
1a2e9 65 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d  eof(u32)];.    }
1a2ea 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43  .  }.  return pC
1a2eb 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20  x;.}../*.** Try 
1a2ec 74 6f 20 63 6f 6e 76 65 72 74 20 61 20 76 61 6c  to convert a val
1a2ed 75 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69  ue into a numeri
1a2ee 63 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  c representation
1a2ef 20 69 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f   if we can.** do
1a2f0 20 73 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73 73   so without loss
1a2f1 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e   of information.
1a2f2 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
1a2f3 2c 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a  , if the string.
1a2f4 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20  ** looks like a 
1a2f5 6e 75 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74 20  number, convert 
1a2f6 69 74 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72  it into a number
1a2f7 2e 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f  .  If it does no
1a2f8 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61  t.** look like a
1a2f9 20 6e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20 69   number, leave i
1a2fa 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74  t alone..*/.stat
1a2fb 69 63 20 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d  ic void applyNum
1a2fc 65 72 69 63 41 66 66 69 6e 69 74 79 28 4d 65 6d  ericAffinity(Mem
1a2fd 20 2a 70 52 65 63 29 7b 0a 20 20 69 66 28 20 28   *pRec){.  if( (
1a2fe 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pRec->flags & (M
1a2ff 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29  EM_Real|MEM_Int)
1a300 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
1a301 72 65 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c  realnum;.    sql
1a302 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
1a303 72 6d 69 6e 61 74 65 28 70 52 65 63 29 3b 0a 20  rminate(pRec);. 
1a304 20 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c     if( (pRec->fl
1a305 61 67 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20  ags&MEM_Str).   
1a306 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
1a307 49 73 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a  IsNumber(pRec->z
1a308 2c 20 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63  , &realnum, pRec
1a309 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20 20  ->enc) ){.      
1a30a 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20  i64 value;.     
1a30b 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1a30c 67 65 45 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c  geEncoding(pRec,
1a30d 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20   SQLITE_UTF8);. 
1a30e 20 20 20 20 20 69 66 28 20 21 72 65 61 6c 6e 75       if( !realnu
1a30f 6d 20 26 26 20 73 71 6c 69 74 65 33 41 74 6f 69  m && sqlite3Atoi
1a310 36 34 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c  64(pRec->z, &val
1a311 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ue) ){.        p
1a312 52 65 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65  Rec->u.i = value
1a313 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 65 74  ;.        MemSet
1a314 54 79 70 65 46 6c 61 67 28 70 52 65 63 2c 20 4d  TypeFlag(pRec, M
1a315 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d  EM_Int);.      }
1a316 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
1a317 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c  lite3VdbeMemReal
1a318 69 66 79 28 70 52 65 63 29 3b 0a 20 20 20 20 20  ify(pRec);.     
1a319 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
1a31a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67  /*.** Processing
1a31b 20 69 73 20 64 65 74 65 72 6d 69 6e 65 20 62 79   is determine by
1a31c 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 70 61   the affinity pa
1a31d 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53  rameter:.**.** S
1a31e 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
1a31f 52 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46  R:.** SQLITE_AFF
1a320 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45  _REAL:.** SQLITE
1a321 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a  _AFF_NUMERIC:.**
1a322 20 20 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65      Try to conve
1a323 72 74 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e  rt pRec to an in
1a324 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61  teger representa
1a325 74 69 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20  tion or a .**   
1a326 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20   floating-point 
1a327 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69  representation i
1a328 66 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70  f an integer rep
1a329 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20  resentation.**  
1a32a 20 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c    is not possibl
1a32b 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68  e.  Note that th
1a32c 65 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73  e integer repres
1a32d 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20  entation is.**  
1a32e 20 20 61 6c 77 61 79 73 20 70 72 65 66 65 72 72    always preferr
1a32f 65 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20  ed, even if the 
1a330 61 66 66 69 6e 69 74 79 20 69 73 20 52 45 41 4c  affinity is REAL
1a331 2c 20 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 20  , because.**    
1a332 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65  an integer repre
1a333 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72  sentation is mor
1a334 65 20 73 70 61 63 65 20 65 66 66 69 63 69 65 6e  e space efficien
1a335 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  t on disk..**.**
1a336 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
1a337 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 20  :.**    Convert 
1a338 70 52 65 63 20 74 6f 20 61 20 74 65 78 74 20 72  pRec to a text r
1a339 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a  epresentation..*
1a33a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  *.** SQLITE_AFF_
1a33b 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f  NONE:.**    No-o
1a33c 70 2e 20 20 70 52 65 63 20 69 73 20 75 6e 63 68  p.  pRec is unch
1a33d 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  anged..*/.static
1a33e 20 76 6f 69 64 20 61 70 70 6c 79 41 66 66 69 6e   void applyAffin
1a33f 69 74 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63  ity(.  Mem *pRec
1a340 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
1a341 65 20 76 61 6c 75 65 20 74 6f 20 61 70 70 6c 79  e value to apply
1a342 20 61 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a   affinity to */.
1a343 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c    char affinity,
1a344 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66        /* The aff
1a345 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c  inity to be appl
1a346 69 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20  ied */.  u8 enc 
1a347 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a348 55 73 65 20 74 68 69 73 20 74 65 78 74 20 65 6e  Use this text en
1a349 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69  coding */.){.  i
1a34a 66 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  f( affinity==SQL
1a34b 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a  ITE_AFF_TEXT ){.
1a34c 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65      /* Only atte
1a34d 6d 70 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69  mpt the conversi
1a34e 6f 6e 20 74 6f 20 54 45 58 54 20 69 66 20 74 68  on to TEXT if th
1a34f 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65  ere is an intege
1a350 72 20 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a  r or real.    **
1a351 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1a352 28 62 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64  (blob and NULL d
1a353 6f 20 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 72  o not get conver
1a354 74 65 64 29 20 62 75 74 20 6e 6f 20 73 74 72 69  ted) but no stri
1a355 6e 67 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73  ng.    ** repres
1a356 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  entation..    */
1a357 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 52 65  .    if( 0==(pRe
1a358 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72  c->flags&MEM_Str
1a359 29 20 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 67  ) && (pRec->flag
1a35a 73 26 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f  s&(MEM_Real|MEM_
1a35b 49 6e 74 29 29 20 29 7b 0a 20 20 20 20 20 20 73  Int)) ){.      s
1a35c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
1a35d 69 6e 67 69 66 79 28 70 52 65 63 2c 20 65 6e 63  ingify(pRec, enc
1a35e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65  );.    }.    pRe
1a35f 63 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45  c->flags &= ~(ME
1a360 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b  M_Real|MEM_Int);
1a361 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 66 66  .  }else if( aff
1a362 69 6e 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 46  inity!=SQLITE_AF
1a363 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73  F_NONE ){.    as
1a364 73 65 72 74 28 20 61 66 66 69 6e 69 74 79 3d 3d  sert( affinity==
1a365 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
1a366 45 52 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d  ER || affinity==
1a367 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a  SQLITE_AFF_REAL.
1a368 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
1a369 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
1a36a 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a  _AFF_NUMERIC );.
1a36b 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63      applyNumeric
1a36c 41 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a  Affinity(pRec);.
1a36d 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c      if( pRec->fl
1a36e 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29  ags & MEM_Real )
1a36f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1a370 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69  dbeIntegerAffini
1a371 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a  ty(pRec);.    }.
1a372 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79    }.}../*.** Try
1a373 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20   to convert the 
1a374 74 79 70 65 20 6f 66 20 61 20 66 75 6e 63 74 69  type of a functi
1a375 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 61  on argument or a
1a376 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a   result column.*
1a377 2a 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63  * into a numeric
1a378 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e   representation.
1a379 20 20 55 73 65 20 65 69 74 68 65 72 20 49 4e 54    Use either INT
1a37a 45 47 45 52 20 6f 72 20 52 45 41 4c 20 77 68 69  EGER or REAL whi
1a37b 63 68 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 70  chever.** is app
1a37c 72 6f 70 72 69 61 74 65 2e 20 20 42 75 74 20 6f  ropriate.  But o
1a37d 6e 6c 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 65  nly do the conve
1a37e 72 73 69 6f 6e 20 69 66 20 69 74 20 69 73 20 70  rsion if it is p
1a37f 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 0a  ossible without.
1a380 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72  ** loss of infor
1a381 6d 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72  mation and retur
1a382 6e 20 74 68 65 20 72 65 76 69 73 65 64 20 74 79  n the revised ty
1a383 70 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65  pe of the argume
1a384 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  nt..**.** This i
1a385 73 20 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 41  s an EXPERIMENTA
1a386 4c 20 61 70 69 20 61 6e 64 20 69 73 20 73 75 62  L api and is sub
1a387 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f  ject to change o
1a388 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51  r removal..*/.SQ
1a389 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1a38a 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72  ite3_value_numer
1a38b 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f  ic_type(sqlite3_
1a38c 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
1a38d 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d  Mem *pMem = (Mem
1a38e 2a 29 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e  *)pVal;.  applyN
1a38f 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70  umericAffinity(p
1a390 4d 65 6d 29 3b 0a 20 20 73 74 6f 72 65 54 79 70  Mem);.  storeTyp
1a391 65 49 6e 66 6f 28 70 4d 65 6d 2c 20 30 29 3b 0a  eInfo(pMem, 0);.
1a392 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74    return pMem->t
1a393 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78  ype;.}../*.** Ex
1a394 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e 20 6f  ported version o
1a395 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28  f applyAffinity(
1a396 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b  ). This one work
1a397 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  s on sqlite3_val
1a398 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65  ue*, .** not the
1a399 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74   internal Mem* t
1a39a 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ype..*/.SQLITE_P
1a39b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a39c 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66  te3ValueApplyAff
1a39d 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 65 33  inity(.  sqlite3
1a39e 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20  _value *pVal, . 
1a39f 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 0a 20   u8 affinity, . 
1a3a0 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70   u8 enc.){.  app
1a3a1 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20  lyAffinity((Mem 
1a3a2 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79  *)pVal, affinity
1a3a3 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65  , enc);.}..#ifde
1a3a4 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
1a3a5 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63  *.** Write a nic
1a3a6 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  e string represe
1a3a7 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63  ntation of the c
1a3a8 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20  ontents of cell 
1a3a9 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66  pMem.** into buf
1a3aa 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68  fer zBuf, length
1a3ab 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45   nBuf..*/.SQLITE
1a3ac 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a3ad 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74  lite3VdbeMemPret
1a3ae 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65  tyPrint(Mem *pMe
1a3af 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  m, char *zBuf){.
1a3b0 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a    char *zCsr = z
1a3b1 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70  Buf;.  int f = p
1a3b2 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73  Mem->flags;..  s
1a3b3 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
1a3b4 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73   *const encnames
1a3b5 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38  [] = {"(X)", "(8
1a3b6 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28  )", "(16LE)", "(
1a3b7 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20  16BE)"};..  if( 
1a3b8 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20  f&MEM_Blob ){.  
1a3b9 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61    int i;.    cha
1a3ba 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26  r c;.    if( f &
1a3bb 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20   MEM_Dyn ){.    
1a3bc 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20    c = 'z';.     
1a3bd 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
1a3be 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70  EM_Static|MEM_Ep
1a3bf 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  hem))==0 );.    
1a3c0 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45  }else if( f & ME
1a3c1 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  M_Static ){.    
1a3c2 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20    c = 't';.     
1a3c3 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
1a3c4 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d  EM_Dyn|MEM_Ephem
1a3c5 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c  ))==0 );.    }el
1a3c6 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45  se if( f & MEM_E
1a3c7 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20  phem ){.      c 
1a3c8 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73  = 'e';.      ass
1a3c9 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53  ert( (f & (MEM_S
1a3ca 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d  tatic|MEM_Dyn))=
1a3cb 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  =0 );.    }else{
1a3cc 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a  .      c = 's';.
1a3cd 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
1a3ce 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c  e3_snprintf(100,
1a3cf 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b   zCsr, "%c", c);
1a3d0 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c  .    zCsr += sql
1a3d1 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73  ite3Strlen30(zCs
1a3d2 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  r);.    sqlite3_
1a3d3 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43  snprintf(100, zC
1a3d4 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d  sr, "%d[", pMem-
1a3d5 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d  >n);.    zCsr +=
1a3d6 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1a3d7 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 72 28  (zCsr);.    for(
1a3d8 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c 70  i=0; i<16 && i<p
1a3d9 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Mem->n; i++){.  
1a3da 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1a3db 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20  intf(100, zCsr, 
1a3dc 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29 70 4d  "%02X", ((int)pM
1a3dd 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 46 29  em->z[i] & 0xFF)
1a3de 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d  );.      zCsr +=
1a3df 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1a3e0 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20  (zCsr);.    }.  
1a3e1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20    for(i=0; i<16 
1a3e2 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b  && i<pMem->n; i+
1a3e3 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a  +){.      char z
1a3e4 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20   = pMem->z[i];. 
1a3e5 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20 7c 7c       if( z<32 ||
1a3e6 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 2b 2b   z>126 ) *zCsr++
1a3e7 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 65 6c   = '.';.      el
1a3e8 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b 0a  se *zCsr++ = z;.
1a3e9 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
1a3ea 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c  e3_snprintf(100,
1a3eb 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 65 6e   zCsr, "]%s", en
1a3ec 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63  cnames[pMem->enc
1a3ed 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20  ]);.    zCsr += 
1a3ee 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1a3ef 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 66  zCsr);.    if( f
1a3f0 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20   & MEM_Zero ){. 
1a3f1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1a3f2 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c  rintf(100, zCsr,
1a3f3 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 6e  "+%dz",pMem->u.n
1a3f4 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a 43 73  Zero);.      zCs
1a3f5 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  r += sqlite3Strl
1a3f6 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20  en30(zCsr);.    
1a3f7 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c  }.    *zCsr = '\
1a3f8 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  0';.  }else if( 
1a3f9 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20  f & MEM_Str ){. 
1a3fa 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20     int j, k;.   
1a3fb 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a   zBuf[0] = ' ';.
1a3fc 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f      if( f & MEM_
1a3fd 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75  Dyn ){.      zBu
1a3fe 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20  f[1] = 'z';.    
1a3ff 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28    assert( (f & (
1a400 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45  MEM_Static|MEM_E
1a401 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20  phem))==0 );.   
1a402 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d   }else if( f & M
1a403 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20  EM_Static ){.   
1a404 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27     zBuf[1] = 't'
1a405 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a406 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45  (f & (MEM_Dyn|ME
1a407 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a  M_Ephem))==0 );.
1a408 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20      }else if( f 
1a409 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20  & MEM_Ephem ){. 
1a40a 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27       zBuf[1] = '
1a40b 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  e';.      assert
1a40c 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74  ( (f & (MEM_Stat
1a40d 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20  ic|MEM_Dyn))==0 
1a40e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1a40f 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73      zBuf[1] = 's
1a410 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d  ';.    }.    k =
1a411 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   2;.    sqlite3_
1a412 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a  snprintf(100, &z
1a413 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d  Buf[k], "%d", pM
1a414 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d  em->n);.    k +=
1a415 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1a416 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20  (&zBuf[k]);.    
1a417 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b  zBuf[k++] = '[';
1a418 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
1a419 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b  15 && j<pMem->n;
1a41a 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20   j++){.      u8 
1a41b 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a  c = pMem->z[j];.
1a41c 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30 78 32        if( c>=0x2
1a41d 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b 0a 20  0 && c<0x7f ){. 
1a41e 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d         zBuf[k++]
1a41f 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = c;.      }els
1a420 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b  e{.        zBuf[
1a421 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20  k++] = '.';.    
1a422 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42    }.    }.    zB
1a423 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20  uf[k++] = ']';. 
1a424 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1a425 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d  ntf(100,&zBuf[k]
1a426 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d  , encnames[pMem-
1a427 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d  >enc]);.    k +=
1a428 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1a429 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20  (&zBuf[k]);.    
1a42a 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20  zBuf[k++] = 0;. 
1a42b 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66   }.}.#endif..#if
1a42c 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1a42d 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65  ./*.** Print the
1a42e 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65 67 69   value of a regi
1a42f 73 74 65 72 20 66 6f 72 20 74 72 61 63 69 6e 67  ster for tracing
1a430 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74   purposes:.*/.st
1a431 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 72 61  atic void memTra
1a432 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75  cePrint(FILE *ou
1a433 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66  t, Mem *p){.  if
1a434 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
1a435 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72  _Null ){.    fpr
1a436 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c  intf(out, " NULL
1a437 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ");.  }else if( 
1a438 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  (p->flags & (MEM
1a439 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d  _Int|MEM_Str))==
1a43a 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72  (MEM_Int|MEM_Str
1a43b 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66  ) ){.    fprintf
1a43c 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22  (out, " si:%lld"
1a43d 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c  , p->u.i);.  }el
1a43e 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  se if( p->flags 
1a43f 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
1a440 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
1a441 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29  i:%lld", p->u.i)
1a442 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
1a443 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
1a444 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66  l ){.    fprintf
1a445 28 6f 75 74 2c 20 22 20 72 3a 25 67 22 2c 20 70  (out, " r:%g", p
1a446 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ->r);.  }else{. 
1a447 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30     char zBuf[200
1a448 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
1a449 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
1a44a 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66  (p, zBuf);.    f
1a44b 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 29  printf(out, " ")
1a44c 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ;.    fprintf(ou
1a44d 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a  t, "%s", zBuf);.
1a44e 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
1a44f 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28  d registerTrace(
1a450 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69  FILE *out, int i
1a451 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20  Reg, Mem *p){.  
1a452 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 45  fprintf(out, "RE
1a453 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 29  G[%d] = ", iReg)
1a454 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 6e  ;.  memTracePrin
1a455 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 72  t(out, p);.  fpr
1a456 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
1a457 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  .}.#endif..#ifde
1a458 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23  f SQLITE_DEBUG.#
1a459 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45    define REGISTE
1a45a 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 28  R_TRACE(R,M) if(
1a45b 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 65  p->trace)registe
1a45c 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c  rTrace(p->trace,
1a45d 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 65  R,M).#else.#  de
1a45e 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52  fine REGISTER_TR
1a45f 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a  ACE(R,M).#endif.
1a460 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  ..#ifdef VDBE_PR
1a461 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77  OFILE../* .** hw
1a462 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20  time.h contains 
1a463 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72  inline assembler
1a464 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d   code for implem
1a465 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d  enting .** high-
1a466 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69  performance timi
1a467 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  ng routines..*/.
1a468 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1a469 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68  Include hwtime.h
1a46a 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
1a46b 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.c *******
1a46c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1a46d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1a46e 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d  Begin file hwtim
1a46f 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
1a470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a471 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1a472 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
1a473 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
1a474 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1a475 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1a476 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1a477 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1a478 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1a479 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1a47a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1a47b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1a47c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1a47d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1a47e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1a47f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1a480 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1a481 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1a482 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1a483 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1a484 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1a485 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a486 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a487 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a488 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a489 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1a48a 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69   file contains i
1a48b 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66  nline asm code f
1a48c 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68  or retrieving "h
1a48d 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22  igh-performance"
1a48e 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72  .** counters for
1a48f 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e   x86 class CPUs.
1a490 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69  .**.** $Id: hwti
1a491 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f  me.h,v 1.3 2008/
1a492 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73  08/01 14:33:15 s
1a493 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69  hane Exp $.*/.#i
1a494 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f  fndef _HWTIME_H_
1a495 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45  .#define _HWTIME
1a496 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  _H_../*.** The f
1a497 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
1a498 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70   only works on p
1a499 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72  entium-class (or
1a49a 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f   newer) processo
1a49b 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74  rs..** It uses t
1a49c 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20  he RDTSC opcode 
1a49d 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c  to read the cycl
1a49e 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75  e count value ou
1a49f 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63  t of the.** proc
1a4a0 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e  essor and return
1a4a1 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54  s that value.  T
1a4a2 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
1a4a3 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20  for high-res.** 
1a4a4 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69  profiling..*/.#i
1a4a5 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  f (defined(__GNU
1a4a6 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  C__) || defined(
1a4a7 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a  _MSC_VER)) && \.
1a4a8 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69        (defined(i
1a4a9 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  386) || defined(
1a4aa 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66  __i386__) || def
1a4ab 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a  ined(_M_IX86))..
1a4ac 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f    #if defined(__
1a4ad 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c  GNUC__)..  __inl
1a4ae 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
1a4af 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
1a4b0 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e  e(void){.     un
1a4b1 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68  signed int lo, h
1a4b2 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  i;.     __asm__ 
1a4b3 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72  __volatile__ ("r
1a4b4 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f  dtsc" : "=a" (lo
1a4b5 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20  ), "=d" (hi));. 
1a4b6 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69      return (sqli
1a4b7 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20  te_uint64)hi << 
1a4b8 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20  32 | lo;.  }..  
1a4b9 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d  #elif defined(_M
1a4ba 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63  SC_VER)..  __dec
1a4bb 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69  lspec(naked) __i
1a4bc 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e  nline sqlite_uin
1a4bd 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69  t64 __cdecl sqli
1a4be 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
1a4bf 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20  .     __asm {.  
1a4c0 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20        rdtsc.    
1a4c1 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20      ret       ; 
1a4c2 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20  return value at 
1a4c3 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20  EDX:EAX.     }. 
1a4c4 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65   }..  #endif..#e
1a4c5 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  lif (defined(__G
1a4c6 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65  NUC__) && define
1a4c7 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a  d(__x86_64__))..
1a4c8 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
1a4c9 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
1a4ca 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
1a4cb 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c        unsigned l
1a4cc 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f  ong val;.      _
1a4cd 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
1a4ce 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22  e__ ("rdtsc" : "
1a4cf 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20  =A" (val));.    
1a4d0 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20    return val;.  
1a4d1 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  }. .#elif (defin
1a4d2 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
1a4d3 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29  defined(__ppc__)
1a4d4 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  )..  __inline__ 
1a4d5 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
1a4d6 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
1a4d7 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  ){.      unsigne
1a4d8 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76  d long long retv
1a4d9 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  al;.      unsign
1a4da 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20  ed long junk;.  
1a4db 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f      __asm__ __vo
1a4dc 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20  latile__ ("\n\. 
1a4dd 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20           1:     
1a4de 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20   mftbu   %1\n\. 
1a4df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a4e0 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a   mftb    %L0\n\.
1a4e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a4e2 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a    mftbu   %0\n\.
1a4e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a4e4 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c    cmpw    %0,%1\
1a4e5 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
1a4e6 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22       bne     1b"
1a4e7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1a4e8 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61     : "=r" (retva
1a4e9 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29  l), "=r" (junk))
1a4ea 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
1a4eb 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73  etval;.  }..#els
1a4ec 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64  e..  #error Need
1a4ed 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
1a4ee 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  of sqlite3Hwtime
1a4ef 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
1a4f0 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  form...  /*.  **
1a4f1 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68   To compile with
1a4f2 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  out implementing
1a4f3 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
1a4f4 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f   for your platfo
1a4f5 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e  rm,.  ** you can
1a4f6 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76   remove the abov
1a4f7 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65  e #error and use
1a4f8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
1a4f9 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f   ** stub functio
1a4fa 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73  n.  You will los
1a4fb 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74  e timing support
1a4fc 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f   for many.  ** o
1a4fd 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20  f the debugging 
1a4fe 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c  and testing util
1a4ff 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68  ities, but it sh
1a500 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61  ould at.  ** lea
1a501 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72  st compile and r
1a502 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f  un..  */.SQLITE_
1a503 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65  PRIVATE   sqlite
1a504 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
1a505 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74  wtime(void){ ret
1a506 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e  urn ((sqlite_uin
1a507 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69  t64)0); }..#endi
1a508 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
1a509 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f  fined(_HWTIME_H_
1a50a 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
1a50b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74  ***** End of hwt
1a50c 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
1a50d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a50e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a50f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1a510 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
1a511 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
1a512 66 66 20 69 6e 20 76 64 62 65 2e 63 20 2a 2a 2a  ff in vdbe.c ***
1a513 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a514 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66 0a 0a 2f  ****/..#endif../
1a515 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 46  *.** The CHECK_F
1a516 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 6d 61 63  OR_INTERRUPT mac
1a517 72 6f 20 64 65 66 69 6e 65 64 20 68 65 72 65 20  ro defined here 
1a518 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 20 69 66 20  looks to see if 
1a519 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69  the.** sqlite3_i
1a51a 6e 74 65 72 72 75 70 74 28 29 20 72 6f 75 74 69  nterrupt() routi
1a51b 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  ne has been call
1a51c 65 64 2e 20 20 49 66 20 69 74 20 68 61 73 20 62  ed.  If it has b
1a51d 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20 70 72 6f  een, then.** pro
1a51e 63 65 73 73 69 6e 67 20 6f 66 20 74 68 65 20 56  cessing of the V
1a51f 44 42 45 20 70 72 6f 67 72 61 6d 20 69 73 20 69  DBE program is i
1a520 6e 74 65 72 72 75 70 74 65 64 2e 0a 2a 2a 0a 2a  nterrupted..**.*
1a521 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 61 64 64  * This macro add
1a522 65 64 20 74 6f 20 65 76 65 72 79 20 69 6e 73 74  ed to every inst
1a523 72 75 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65  ruction that doe
1a524 73 20 61 20 6a 75 6d 70 20 69 6e 20 6f 72 64 65  s a jump in orde
1a525 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  r to.** implemen
1a526 74 20 61 20 6c 6f 6f 70 2e 20 20 54 68 69 73 20  t a loop.  This 
1a527 74 65 73 74 20 75 73 65 64 20 74 6f 20 62 65 20  test used to be 
1a528 6f 6e 20 65 76 65 72 79 20 73 69 6e 67 6c 65 20  on every single 
1a529 69 6e 73 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a 20  instruction,.** 
1a52a 62 75 74 20 74 68 61 74 20 6d 65 61 6e 74 20 77  but that meant w
1a52b 65 20 6d 6f 72 65 20 74 65 73 74 69 6e 67 20 74  e more testing t
1a52c 68 61 74 20 77 65 20 6e 65 65 64 65 64 2e 20 20  hat we needed.  
1a52d 42 79 20 6f 6e 6c 79 20 74 65 73 74 69 6e 67 20  By only testing 
1a52e 74 68 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a  the.** flag on j
1a52f 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ump instructions
1a530 2c 20 77 65 20 67 65 74 20 61 20 28 73 6d 61 6c  , we get a (smal
1a531 6c 29 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65  l) speed improve
1a532 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ment..*/.#define
1a533 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52   CHECK_FOR_INTER
1a534 52 55 50 54 20 5c 0a 20 20 20 69 66 28 20 64 62  RUPT \.   if( db
1a535 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
1a536 65 64 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  ed ) goto abort_
1a537 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74  due_to_interrupt
1a538 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
1a539 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e  _DEBUG.static in
1a53a 74 20 66 69 6c 65 45 78 69 73 74 73 28 73 71 6c  t fileExists(sql
1a53b 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
1a53c 63 68 61 72 20 2a 7a 46 69 6c 65 29 7b 0a 20 20  char *zFile){.  
1a53d 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20 69  int res = 0;.  i
1a53e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1a53f 4b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  K;.#ifdef SQLITE
1a540 5f 54 45 53 54 0a 20 20 2f 2a 20 49 66 20 77 65  _TEST.  /* If we
1a541 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 74   are currently t
1a542 65 73 74 69 6e 67 20 49 4f 20 65 72 72 6f 72 73  esting IO errors
1a543 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 61  , then do not ca
1a544 6c 6c 20 4f 73 41 63 63 65 73 73 28 29 20 74 6f  ll OsAccess() to
1a545 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f 72 20 74  .  ** test for t
1a546 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 7a  he presence of z
1a547 46 69 6c 65 2e 20 54 68 69 73 20 69 73 20 62 65  File. This is be
1a548 63 61 75 73 65 20 61 6e 79 20 49 4f 20 65 72 72  cause any IO err
1a549 6f 72 20 74 68 61 74 0a 20 20 2a 2a 20 6f 63 63  or that.  ** occ
1a54a 75 72 73 20 68 65 72 65 20 77 69 6c 6c 20 6e 6f  urs here will no
1a54b 74 20 62 65 20 72 65 70 6f 72 74 65 64 2c 20 63  t be reported, c
1a54c 61 75 73 69 6e 67 20 74 68 65 20 74 65 73 74 20  ausing the test 
1a54d 74 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20  to fail..  */.  
1a54e 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
1a54f 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
1a550 69 6e 67 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ing;.  if( sqlit
1a551 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
1a552 69 6e 67 3c 3d 30 20 29 0a 23 65 6e 64 69 66 0a  ing<=0 ).#endif.
1a553 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1a554 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66  OsAccess(db->pVf
1a555 73 2c 20 7a 46 69 6c 65 2c 20 53 51 4c 49 54 45  s, zFile, SQLITE
1a556 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20  _ACCESS_EXISTS, 
1a557 26 72 65 73 29 3b 0a 20 20 72 65 74 75 72 6e 20  &res);.  return 
1a558 28 72 65 73 20 26 26 20 72 63 3d 3d 53 51 4c 49  (res && rc==SQLI
1a559 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66  TE_OK);.}.#endif
1a55a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
1a55b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
1a55c 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c  tion is only cal
1a55d 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  led from within 
1a55e 61 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 72  an assert() expr
1a55f 65 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a 20 63 68  ession. It.** ch
1a560 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 73 71  ecks that the sq
1a561 6c 69 74 65 33 2e 6e 54 72 61 6e 73 61 63 74 69  lite3.nTransacti
1a562 6f 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20 63  on variable is c
1a563 6f 72 72 65 63 74 6c 79 20 73 65 74 20 74 6f 0a  orrectly set to.
1a564 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
1a565 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e   non-transaction
1a566 20 73 61 76 65 70 6f 69 6e 74 73 20 63 75 72 72   savepoints curr
1a567 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 0a 2a 2a  ently in the .**
1a568 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 61   linked list sta
1a569 72 74 69 6e 67 20 61 74 20 73 71 6c 69 74 65 33  rting at sqlite3
1a56a 2e 70 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20  .pSavepoint..** 
1a56b 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a 2a  .** Usage:.**.**
1a56c 20 20 20 20 20 61 73 73 65 72 74 28 20 63 68 65       assert( che
1a56d 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74  ckSavepointCount
1a56e 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69  (db) );.*/.stati
1a56f 63 20 69 6e 74 20 63 68 65 63 6b 53 61 76 65 70  c int checkSavep
1a570 6f 69 6e 74 43 6f 75 6e 74 28 73 71 6c 69 74 65  ointCount(sqlite
1a571 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 6e 20  3 *db){.  int n 
1a572 3d 20 30 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74  = 0;.  Savepoint
1a573 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 64 62 2d   *p;.  for(p=db-
1a574 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 70 3b 20  >pSavepoint; p; 
1a575 70 3d 70 2d 3e 70 4e 65 78 74 29 20 6e 2b 2b 3b  p=p->pNext) n++;
1a576 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 28 64  .  assert( n==(d
1a577 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b 20  b->nSavepoint + 
1a578 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f  db->isTransactio
1a579 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 20  nSavepoint) );. 
1a57a 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e   return 1;.}.#en
1a57b 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75  dif../*.** Execu
1a57c 74 65 20 61 73 20 6d 75 63 68 20 6f 66 20 61 20  te as much of a 
1a57d 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61 73 20  VDBE program as 
1a57e 77 65 20 63 61 6e 20 74 68 65 6e 20 72 65 74 75  we can then retu
1a57f 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65  rn..**.** sqlite
1a580 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 29  3VdbeMakeReady()
1a581 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
1a582 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74  before this rout
1a583 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a  ine in order to.
1a584 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 70 72 6f  ** close the pro
1a585 67 72 61 6d 20 77 69 74 68 20 61 20 66 69 6e 61  gram with a fina
1a586 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20 74 6f  l OP_Halt and to
1a587 20 73 65 74 20 75 70 20 74 68 65 20 63 61 6c 6c   set up the call
1a588 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74 68 65  backs.** and the
1a589 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70   error message p
1a58a 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68  ointer..**.** Wh
1a58b 65 6e 65 76 65 72 20 61 20 72 6f 77 20 6f 72 20  enever a row or 
1a58c 72 65 73 75 6c 74 20 64 61 74 61 20 69 73 20 61  result data is a
1a58d 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 72  vailable, this r
1a58e 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 69 74 68  outine will eith
1a58f 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65  er.** invoke the
1a590 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 61 63 6b   result callback
1a591 20 28 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e   (if there is on
1a592 65 29 20 6f 72 20 72 65 74 75 72 6e 20 77 69 74  e) or return wit
1a593 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57 2e  h.** SQLITE_ROW.
1a594 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61 74 74  .**.** If an att
1a595 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
1a596 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20 64 61  open a locked da
1a597 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68 69  tabase, then thi
1a598 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 69 6c  s routine.** wil
1a599 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b 65 20  l either invoke 
1a59a 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
1a59b 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20 6f  k (if there is o
1a59c 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c 0a 2a  ne) or it will.*
1a59d 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * return SQLITE_
1a59e 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  BUSY..**.** If a
1a59f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
1a5a0 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1a5a1 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6d   is written to m
1a5a2 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a  emory obtained.*
1a5a3 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  * from sqlite3_m
1a5a4 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d 3e 7a  alloc() and p->z
1a5a5 45 72 72 4d 73 67 20 69 73 20 6d 61 64 65 20 74  ErrMsg is made t
1a5a6 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20  o point to that 
1a5a7 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 20 65  memory..** The e
1a5a8 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 74 6f  rror code is sto
1a5a9 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 61 6e 64  red in p->rc and
1a5aa 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
1a5ab 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 52 52  turns SQLITE_ERR
1a5ac 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  OR..**.** If the
1a5ad 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 72 20 72   callback ever r
1a5ae 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  eturns non-zero,
1a5af 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 61   then the progra
1a5b0 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d 65 64  m exits.** immed
1a5b1 69 61 74 65 6c 79 2e 20 20 54 68 65 72 65 20 77  iately.  There w
1a5b2 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f 72 20  ill be no error 
1a5b3 6d 65 73 73 61 67 65 20 62 75 74 20 74 68 65 20  message but the 
1a5b4 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 73 0a 2a  p->rc field is.*
1a5b5 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  * set to SQLITE_
1a5b6 41 42 4f 52 54 20 61 6e 64 20 74 68 69 73 20 72  ABORT and this r
1a5b7 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75  outine will retu
1a5b8 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e  rn SQLITE_ERROR.
1a5b9 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20  .**.** A memory 
1a5ba 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
1a5bb 20 63 61 75 73 65 73 20 70 2d 3e 72 63 20 74 6f   causes p->rc to
1a5bc 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54   be set to SQLIT
1a5bd 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68 69 73  E_NOMEM and this
1a5be 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 72  .** routine to r
1a5bf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1a5c0 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20  OR..**.** Other 
1a5c1 66 61 74 61 6c 20 65 72 72 6f 72 73 20 72 65 74  fatal errors ret
1a5c2 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
1a5c3 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68  ..**.** After th
1a5c4 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66  is routine has f
1a5c5 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74 65 33  inished, sqlite3
1a5c6 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29 20 73  VdbeFinalize() s
1a5c7 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73 65 64  hould be.** used
1a5c8 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 68 65   to clean up the
1a5c9 20 6d 65 73 73 20 74 68 61 74 20 77 61 73 20 6c   mess that was l
1a5ca 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f 0a 53  eft behind..*/.S
1a5cb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1a5cc 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  t sqlite3VdbeExe
1a5cd 63 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20  c(.  Vdbe *p    
1a5ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5cf 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29  /* The VDBE */.)
1a5d0 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20  {.  int pc;     
1a5d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a5d2 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f  * The program co
1a5d3 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 70  unter */.  Op *p
1a5d4 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Op;             
1a5d5 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
1a5d6 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
1a5d7 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1a5d8 4f 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61  OK;        /* Va
1a5d9 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  lue to return */
1a5da 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1a5db 20 70 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a   p->db;       /*
1a5dc 20 54 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   The database */
1a5dd 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 3d  .  u8 encoding =
1a5de 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f 2a   ENC(db);     /*
1a5df 20 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e   The database en
1a5e0 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65 6d 20  coding */.  Mem 
1a5e1 2a 70 49 6e 31 20 3d 20 30 3b 20 20 20 20 20 20  *pIn1 = 0;      
1a5e2 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 69 6e         /* 1st in
1a5e3 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  put operand */. 
1a5e4 20 4d 65 6d 20 2a 70 49 6e 32 20 3d 20 30 3b 20   Mem *pIn2 = 0; 
1a5e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32              /* 2
1a5e6 6e 64 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64  nd input operand
1a5e7 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 33 20   */.  Mem *pIn3 
1a5e8 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1a5e9 20 2f 2a 20 33 72 64 20 69 6e 70 75 74 20 6f 70   /* 3rd input op
1a5ea 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a  erand */.  Mem *
1a5eb 70 4f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20  pOut = 0;       
1a5ec 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
1a5ed 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 75 38 20  operand */.  u8 
1a5ee 6f 70 50 72 6f 70 65 72 74 79 3b 0a 20 20 69 6e  opProperty;.  in
1a5ef 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 30 3b 20  t iCompare = 0; 
1a5f0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
1a5f1 6c 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f 43 6f  lt of last OP_Co
1a5f2 6d 70 61 72 65 20 6f 70 65 72 61 74 69 6f 6e 20  mpare operation 
1a5f3 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d 75  */.  int *aPermu
1a5f4 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  te = 0;         
1a5f5 2f 2a 20 50 65 72 6d 75 61 74 69 6f 6e 20 6f 66  /* Permuation of
1a5f6 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 4f 50 5f   columns for OP_
1a5f7 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69 66 64 65  Compare */.#ifde
1a5f8 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20  f VDBE_PROFILE. 
1a5f9 20 75 36 34 20 73 74 61 72 74 3b 20 20 20 20 20   u64 start;     
1a5fa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1a5fb 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e 74 20 61  PU clock count a
1a5fc 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63 6f 64  t start of opcod
1a5fd 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 69 67 50  e */.  int origP
1a5fe 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
1a5ff 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75    /* Program cou
1a600 6e 74 65 72 20 61 74 20 73 74 61 72 74 20 6f 66  nter at start of
1a601 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65 6e 64 69   opcode */.#endi
1a602 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
1a603 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43  _OMIT_PROGRESS_C
1a604 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 6e 50  ALLBACK.  int nP
1a605 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 20  rogressOps = 0; 
1a606 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20       /* Opcodes 
1a607 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 20 70  executed since p
1a608 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
1a609 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 55 6e  . */.#endif.  Un
1a60a 70 61 63 6b 65 64 52 65 63 6f 72 64 20 61 54 65  packedRecord aTe
1a60b 6d 70 52 65 63 5b 31 36 5d 3b 20 2f 2a 20 53 70  mpRec[16]; /* Sp
1a60c 61 63 65 20 74 6f 20 68 6f 6c 64 20 61 20 74 72  ace to hold a tr
1a60d 61 6e 73 69 65 6e 74 20 55 6e 70 61 63 6b 65 64  ansient Unpacked
1a60e 52 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 61 73 73  Record */..  ass
1a60f 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
1a610 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b  DBE_MAGIC_RUN );
1a611 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65    /* sqlite3_ste
1a612 70 28 29 20 76 65 72 69 66 69 65 73 20 74 68 69  p() verifies thi
1a613 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64  s */.  assert( d
1a614 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45  b->magic==SQLITE
1a615 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20  _MAGIC_BUSY );. 
1a616 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
1a617 65 78 41 72 72 61 79 45 6e 74 65 72 28 26 70 2d  exArrayEnter(&p-
1a618 3e 61 4d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  >aMutex);.  if( 
1a619 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  p->rc==SQLITE_NO
1a61a 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  MEM ){.    /* Th
1a61b 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 20  is happens if a 
1a61c 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20  malloc() inside 
1a61d 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
1a61e 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20  3_column_text() 
1a61f 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  or.    ** sqlite
1a620 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
1a621 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20  ) failed.  */.  
1a622 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1a623 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e   }.  assert( p->
1a624 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
1a625 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42   p->rc==SQLITE_B
1a626 55 53 59 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d  USY );.  p->rc =
1a627 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73   SQLITE_OK;.  as
1a628 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e  sert( p->explain
1a629 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 70 52 65 73  ==0 );.  p->pRes
1a62a 75 6c 74 53 65 74 20 3d 20 30 3b 0a 20 20 64 62  ultSet = 0;.  db
1a62b 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42  ->busyHandler.nB
1a62c 75 73 79 20 3d 20 30 3b 0a 20 20 43 48 45 43 4b  usy = 0;.  CHECK
1a62d 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a  _FOR_INTERRUPT;.
1a62e 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54    sqlite3VdbeIOT
1a62f 72 61 63 65 53 71 6c 28 70 29 3b 0a 23 69 66 64  raceSql(p);.#ifd
1a630 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1a631 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
1a632 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
1a633 69 66 28 20 70 2d 3e 70 63 3d 3d 30 20 0a 20 20  if( p->pc==0 .  
1a634 20 26 26 20 28 28 70 2d 3e 64 62 2d 3e 66 6c 61   && ((p->db->fla
1a635 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65  gs & SQLITE_Vdbe
1a636 4c 69 73 74 69 6e 67 29 20 7c 7c 20 66 69 6c 65  Listing) || file
1a637 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65  Exists(db, "vdbe
1a638 5f 65 78 70 6c 61 69 6e 22 29 29 0a 20 20 29 7b  _explain")).  ){
1a639 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
1a63a 70 72 69 6e 74 66 28 22 56 44 42 45 20 50 72 6f  printf("VDBE Pro
1a63b 67 72 61 6d 20 4c 69 73 74 69 6e 67 3a 5c 6e 22  gram Listing:\n"
1a63c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1a63d 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20  bePrintSql(p);. 
1a63e 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
1a63f 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nOp; i++){.    
1a640 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1a641 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 69 2c 20  ntOp(stdout, i, 
1a642 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20  &p->aOp[i]);.   
1a643 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 66 69 6c   }.  }.  if( fil
1a644 65 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62  eExists(db, "vdb
1a645 65 5f 74 72 61 63 65 22 29 20 29 7b 0a 20 20 20  e_trace") ){.   
1a646 20 70 2d 3e 74 72 61 63 65 20 3d 20 73 74 64 6f   p->trace = stdo
1a647 75 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ut;.  }.  sqlite
1a648 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
1a649 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72  ();.#endif.  for
1a64a 28 70 63 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53  (pc=p->pc; rc==S
1a64b 51 4c 49 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b  QLITE_OK; pc++){
1a64c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e  .    assert( pc>
1a64d 3d 30 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20  =0 && pc<p->nOp 
1a64e 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  );.    if( db->m
1a64f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
1a650 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65  to no_mem;.#ifde
1a651 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20  f VDBE_PROFILE. 
1a652 20 20 20 6f 72 69 67 50 63 20 3d 20 70 63 3b 0a     origPc = pc;.
1a653 20 20 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69      start = sqli
1a654 74 65 33 48 77 74 69 6d 65 28 29 3b 0a 23 65 6e  te3Hwtime();.#en
1a655 64 69 66 0a 20 20 20 20 70 4f 70 20 3d 20 26 70  dif.    pOp = &p
1a656 2d 3e 61 4f 70 5b 70 63 5d 3b 0a 0a 20 20 20 20  ->aOp[pc];..    
1a657 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20 74 72  /* Only allow tr
1a658 61 63 69 6e 67 20 69 66 20 53 51 4c 49 54 45 5f  acing if SQLITE_
1a659 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64  DEBUG is defined
1a65a 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20  ..    */.#ifdef 
1a65b 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
1a65c 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b   if( p->trace ){
1a65d 0a 20 20 20 20 20 20 69 66 28 20 70 63 3d 3d 30  .      if( pc==0
1a65e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 69 6e   ){.        prin
1a65f 74 66 28 22 56 44 42 45 20 45 78 65 63 75 74 69  tf("VDBE Executi
1a660 6f 6e 20 54 72 61 63 65 3a 5c 6e 22 29 3b 0a 20  on Trace:\n");. 
1a661 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1a662 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20  bePrintSql(p);. 
1a663 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
1a664 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
1a665 70 2d 3e 74 72 61 63 65 2c 20 70 63 2c 20 70 4f  p->trace, pc, pO
1a666 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  p);.    }.    if
1a667 28 20 70 2d 3e 74 72 61 63 65 3d 3d 30 20 26 26  ( p->trace==0 &&
1a668 20 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   pc==0 ){.      
1a669 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
1a66a 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
1a66b 20 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 73    if( fileExists
1a66c 28 64 62 2c 20 22 76 64 62 65 5f 73 71 6c 74 72  (db, "vdbe_sqltr
1a66d 61 63 65 22 29 20 29 7b 0a 20 20 20 20 20 20 20  ace") ){.       
1a66e 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
1a66f 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d  tSql(p);.      }
1a670 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e  .      sqlite3En
1a671 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
1a672 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1a673 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65      ..    /* Che
1a674 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65 20  ck to see if we 
1a675 6e 65 65 64 20 74 6f 20 73 69 6d 75 6c 61 74 65  need to simulate
1a676 20 61 6e 20 69 6e 74 65 72 72 75 70 74 2e 20 20   an interrupt.  
1a677 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e  This only happen
1a678 73 0a 20 20 20 20 2a 2a 20 69 66 20 77 65 20 68  s.    ** if we h
1a679 61 76 65 20 61 20 73 70 65 63 69 61 6c 20 74 65  ave a special te
1a67a 73 74 20 62 75 69 6c 64 2e 0a 20 20 20 20 2a 2f  st build..    */
1a67b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1a67c 45 53 54 0a 20 20 20 20 69 66 28 20 73 71 6c 69  EST.    if( sqli
1a67d 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f  te3_interrupt_co
1a67e 75 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 73  unt>0 ){.      s
1a67f 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1a680 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 20 20  _count--;.      
1a681 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  if( sqlite3_inte
1a682 72 72 75 70 74 5f 63 6f 75 6e 74 3d 3d 30 20 29  rrupt_count==0 )
1a683 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1a684 33 5f 69 6e 74 65 72 72 75 70 74 28 64 62 29 3b  3_interrupt(db);
1a685 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
1a686 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
1a687 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52  QLITE_OMIT_PROGR
1a688 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 20  ESS_CALLBACK.   
1a689 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 72 6f   /* Call the pro
1a68a 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69  gress callback i
1a68b 66 20 69 74 20 69 73 20 63 6f 6e 66 69 67 75 72  f it is configur
1a68c 65 64 20 61 6e 64 20 74 68 65 20 72 65 71 75 69  ed and the requi
1a68d 72 65 64 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a  red number.    *
1a68e 2a 20 6f 66 20 56 44 42 45 20 6f 70 73 20 68 61  * of VDBE ops ha
1a68f 76 65 20 62 65 65 6e 20 65 78 65 63 75 74 65 64  ve been executed
1a690 20 28 65 69 74 68 65 72 20 73 69 6e 63 65 20 74   (either since t
1a691 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  his invocation o
1a692 66 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  f.    ** sqlite3
1a693 56 64 62 65 45 78 65 63 28 29 20 6f 72 20 73 69  VdbeExec() or si
1a694 6e 63 65 20 6c 61 73 74 20 74 69 6d 65 20 74 68  nce last time th
1a695 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
1a696 61 63 6b 20 77 61 73 20 63 61 6c 6c 65 64 29 2e  ack was called).
1a697 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70  .    ** If the p
1a698 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
1a699 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
1a69a 6f 2c 20 65 78 69 74 20 74 68 65 20 76 69 72 74  o, exit the virt
1a69b 75 61 6c 20 6d 61 63 68 69 6e 65 20 77 69 74 68  ual machine with
1a69c 0a 20 20 20 20 2a 2a 20 61 20 72 65 74 75 72 6e  .    ** a return
1a69d 20 63 6f 64 65 20 53 51 4c 49 54 45 5f 41 42 4f   code SQLITE_ABO
1a69e 52 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  RT..    */.    i
1a69f 66 28 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73  f( db->xProgress
1a6a0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62   ){.      if( db
1a6a1 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 3d 3d  ->nProgressOps==
1a6a2 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 29 7b 0a  nProgressOps ){.
1a6a3 20 20 20 20 20 20 20 20 69 6e 74 20 70 72 63 3b          int prc;
1a6a4 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
1a6a5 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1a6a6 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1a6a7 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
1a6a8 20 20 20 20 20 20 70 72 63 20 3d 64 62 2d 3e 78        prc =db->x
1a6a9 50 72 6f 67 72 65 73 73 28 64 62 2d 3e 70 50 72  Progress(db->pPr
1a6aa 6f 67 72 65 73 73 41 72 67 29 3b 0a 20 20 20 20  ogressArg);.    
1a6ab 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1a6ac 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f  afetyOn(db) ) go
1a6ad 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1a6ae 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20  misuse;.        
1a6af 69 66 28 20 70 72 63 21 3d 30 20 29 7b 0a 20 20  if( prc!=0 ){.  
1a6b0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1a6b1 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20  ITE_INTERRUPT;. 
1a6b2 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64           goto vd
1a6b3 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20  be_error_halt;. 
1a6b4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1a6b5 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20   nProgressOps = 
1a6b6 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
1a6b7 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b   nProgressOps++;
1a6b8 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
1a6b9 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20     /* Do common 
1a6ba 73 65 74 75 70 20 70 72 6f 63 65 73 73 69 6e 67  setup processing
1a6bb 20 66 6f 72 20 61 6e 79 20 6f 70 63 6f 64 65 20   for any opcode 
1a6bc 74 68 61 74 20 69 73 20 6d 61 72 6b 65 64 0a 20  that is marked. 
1a6bd 20 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 22     ** with the "
1a6be 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 22  out2-prerelease"
1a6bf 20 74 61 67 2e 20 20 53 75 63 68 20 6f 70 63 6f   tag.  Such opco
1a6c0 64 65 73 20 68 61 76 65 20 61 20 73 69 6e 67 6c  des have a singl
1a6c1 65 0a 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20  e.    ** output 
1a6c2 77 68 69 63 68 20 69 73 20 73 70 65 63 69 66 69  which is specifi
1a6c3 65 64 20 62 79 20 74 68 65 20 50 32 20 70 61 72  ed by the P2 par
1a6c4 61 6d 65 74 65 72 2e 20 20 54 68 65 20 50 32 20  ameter.  The P2 
1a6c5 72 65 67 69 73 74 65 72 0a 20 20 20 20 2a 2a 20  register.    ** 
1a6c6 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
1a6c7 6f 20 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f  o a NULL..    */
1a6c8 0a 20 20 20 20 6f 70 50 72 6f 70 65 72 74 79 20  .    opProperty 
1a6c9 3d 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79  = opcodeProperty
1a6ca 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20  [pOp->opcode];. 
1a6cb 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72     if( (opProper
1a6cc 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f  ty & OPFLG_OUT2_
1a6cd 50 52 45 52 45 4c 45 41 53 45 29 21 3d 30 20 29  PRERELEASE)!=0 )
1a6ce 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1a6cf 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20  pOp->p2>0 );.   
1a6d0 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1a6d1 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p2<=p->nMem );. 
1a6d2 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e       pOut = &p->
1a6d3 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1a6d4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1a6d5 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
1a6d6 61 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20  al(pOut);.      
1a6d7 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pOut->flags = ME
1a6d8 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73  M_Null;.    }els
1a6d9 65 0a 20 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f  e. .    /* Do co
1a6da 6d 6d 6f 6e 20 73 65 74 75 70 20 66 6f 72 20 6f  mmon setup for o
1a6db 70 63 6f 64 65 73 20 6d 61 72 6b 65 64 20 77 69  pcodes marked wi
1a6dc 74 68 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  th one of the fo
1a6dd 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63  llowing.    ** c
1a6de 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70  ombinations of p
1a6df 72 6f 70 65 72 74 69 65 73 2e 0a 20 20 20 20 2a  roperties..    *
1a6e0 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
1a6e1 20 20 20 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20     in1.    **   
1a6e2 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32 0a          in1 in2.
1a6e3 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
1a6e4 20 69 6e 31 20 69 6e 32 20 6f 75 74 33 0a 20 20   in1 in2 out3.  
1a6e5 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69    **           i
1a6e6 6e 31 20 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20  n1 in3.    **.  
1a6e7 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 70    ** Variables p
1a6e8 49 6e 31 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70  In1, pIn2, and p
1a6e9 49 6e 33 20 61 72 65 20 6d 61 64 65 20 74 6f 20  In3 are made to 
1a6ea 70 6f 69 6e 74 20 74 6f 20 61 70 70 72 6f 70 72  point to appropr
1a6eb 69 61 74 65 0a 20 20 20 20 2a 2a 20 72 65 67 69  iate.    ** regi
1a6ec 73 74 65 72 73 20 66 6f 72 20 69 6e 70 75 74 73  sters for inputs
1a6ed 2e 20 20 56 61 72 69 61 62 6c 65 20 70 4f 75 74  .  Variable pOut
1a6ee 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6f   points to the o
1a6ef 75 74 70 75 74 20 72 65 67 69 73 74 65 72 2e 0a  utput register..
1a6f0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28      */.    if( (
1a6f1 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
1a6f2 4c 47 5f 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20  LG_IN1)!=0 ){.  
1a6f3 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1a6f4 3e 70 31 3e 30 20 29 3b 0a 20 20 20 20 20 20 61  >p1>0 );.      a
1a6f5 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d  ssert( pOp->p1<=
1a6f6 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  p->nMem );.     
1a6f7 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn1 = &p->aMem
1a6f8 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20  [pOp->p1];.     
1a6f9 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1a6fa 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a  pOp->p1, pIn1);.
1a6fb 20 20 20 20 20 20 69 66 28 20 28 6f 70 50 72 6f        if( (opPro
1a6fc 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e  perty & OPFLG_IN
1a6fd 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  2)!=0 ){.       
1a6fe 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1a6ff 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  >0 );.        as
1a700 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70  sert( pOp->p2<=p
1a701 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20  ->nMem );.      
1a702 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65    pIn2 = &p->aMe
1a703 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
1a704 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1a705 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32  CE(pOp->p2, pIn2
1a706 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28  );.        if( (
1a707 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
1a708 4c 47 5f 4f 55 54 33 29 21 3d 30 20 29 7b 0a 20  LG_OUT3)!=0 ){. 
1a709 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
1a70a 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20   pOp->p3>0 );.  
1a70b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1a70c 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
1a70d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f   );.          pO
1a70e 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1a70f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 20  p->p3];.        
1a710 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  }.      }else if
1a711 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20  ( (opProperty & 
1a712 4f 50 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b  OPFLG_IN3)!=0 ){
1a713 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1a714 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20   pOp->p3>0 );.  
1a715 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1a716 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1a717 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 33 20 3d  ;.        pIn3 =
1a718 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1a719 33 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49  3];.        REGI
1a71a 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1a71b 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20 20  p3, pIn3);.     
1a71c 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
1a71d 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f   (opProperty & O
1a71e 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a  PFLG_IN2)!=0 ){.
1a71f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1a720 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20  p->p2>0 );.     
1a721 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1a722 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  <=p->nMem );.   
1a723 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d     pIn2 = &p->aM
1a724 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20  em[pOp->p2];.   
1a725 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
1a726 45 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29  E(pOp->p2, pIn2)
1a727 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1a728 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50  (opProperty & OP
1a729 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20  FLG_IN3)!=0 ){. 
1a72a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1a72b 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20  ->p3>0 );.      
1a72c 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c  assert( pOp->p3<
1a72d 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20  =p->nMem );.    
1a72e 20 20 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65    pIn3 = &p->aMe
1a72f 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20  m[pOp->p3];.    
1a730 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1a731 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b  (pOp->p3, pIn3);
1a732 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 74  .    }..    swit
1a733 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20  ch( pOp->opcode 
1a734 29 7b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ){../***********
1a735 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a736 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a737 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a738 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a739 2a 2a 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f  **.** What follo
1a73a 77 73 20 69 73 20 61 20 6d 61 73 73 69 76 65 20  ws is a massive 
1a73b 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74  switch statement
1a73c 20 77 68 65 72 65 20 65 61 63 68 20 63 61 73 65   where each case
1a73d 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a   implements a.**
1a73e 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 72 75   separate instru
1a73f 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 69 72  ction in the vir
1a740 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49  tual machine.  I
1a741 66 20 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20  f we follow the 
1a742 75 73 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61  usual.** indenta
1a743 74 69 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e 73  tion conventions
1a744 2c 20 65 61 63 68 20 63 61 73 65 20 73 68 6f 75  , each case shou
1a745 6c 64 20 62 65 20 69 6e 64 65 6e 74 65 64 20 62  ld be indented b
1a746 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75 74  y 6 spaces.  But
1a747 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 20 6c 6f  .** that is a lo
1a748 74 20 6f 66 20 77 61 73 74 65 64 20 73 70 61 63  t of wasted spac
1a749 65 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d 61  e on the left ma
1a74a 72 67 69 6e 2e 20 20 53 6f 20 74 68 65 20 63 6f  rgin.  So the co
1a74b 64 65 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65  de within.** the
1a74c 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e   switch statemen
1a74d 74 20 77 69 6c 6c 20 62 72 65 61 6b 20 77 69 74  t will break wit
1a74e 68 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e 64  h convention and
1a74f 20 62 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e 20   be flush-left. 
1a750 41 6e 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20 63  Another.** big c
1a751 6f 6d 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72 20  omment (similar 
1a752 74 6f 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c  to this one) wil
1a753 6c 20 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e 74  l mark the point
1a754 20 69 6e 20 74 68 65 20 63 6f 64 65 20 77 68 65   in the code whe
1a755 72 65 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69 74  re.** we transit
1a756 69 6f 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 6d  ion back to norm
1a757 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a  al indentation..
1a758 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74  **.** The format
1a759 74 69 6e 67 20 6f 66 20 65 61 63 68 20 63 61 73  ting of each cas
1a75a 65 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20  e is important. 
1a75b 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 66 6f   The makefile fo
1a75c 72 20 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e 65  r SQLite.** gene
1a75d 72 61 74 65 73 20 74 77 6f 20 43 20 66 69 6c 65  rates two C file
1a75e 73 20 22 6f 70 63 6f 64 65 73 2e 68 22 20 61 6e  s "opcodes.h" an
1a75f 64 20 22 6f 70 63 6f 64 65 73 2e 63 22 20 62 79  d "opcodes.c" by
1a760 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a 2a   scanning this.*
1a761 2a 20 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66  * file looking f
1a762 6f 72 20 6c 69 6e 65 73 20 74 68 61 74 20 62 65  or lines that be
1a763 67 69 6e 20 77 69 74 68 20 22 63 61 73 65 20 4f  gin with "case O
1a764 50 5f 22 2e 20 20 54 68 65 20 6f 70 63 6f 64 65  P_".  The opcode
1a765 73 2e 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69 6c  s.h files.** wil
1a766 6c 20 62 65 20 66 69 6c 6c 65 64 20 77 69 74 68  l be filled with
1a767 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20 67   #defines that g
1a768 69 76 65 20 75 6e 69 71 75 65 20 69 6e 74 65 67  ive unique integ
1a769 65 72 20 76 61 6c 75 65 73 20 74 6f 20 65 61 63  er values to eac
1a76a 68 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64 20  h.** opcode and 
1a76b 74 68 65 20 6f 70 63 6f 64 65 73 2e 63 20 66 69  the opcodes.c fi
1a76c 6c 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74  le is filled wit
1a76d 68 20 61 6e 20 61 72 72 61 79 20 6f 66 20 73 74  h an array of st
1a76e 72 69 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20 65  rings where.** e
1a76f 61 63 68 20 73 74 72 69 6e 67 20 69 73 20 74 68  ach string is th
1a770 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20  e symbolic name 
1a771 66 6f 72 20 74 68 65 20 63 6f 72 72 65 73 70 6f  for the correspo
1a772 6e 64 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20 49  nding opcode.  I
1a773 66 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 73 74  f the.** case st
1a774 61 74 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f  atement is follo
1a775 77 65 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74  wed by a comment
1a776 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2f 23   of the form "/#
1a777 20 73 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22   same as ... #/"
1a778 0a 2a 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74  .** that comment
1a779 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65   is used to dete
1a77a 72 6d 69 6e 65 20 74 68 65 20 70 61 72 74 69 63  rmine the partic
1a77b 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68  ular value of th
1a77c 65 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  e opcode..**.** 
1a77d 4f 74 68 65 72 20 6b 65 79 77 6f 72 64 73 20 69  Other keywords i
1a77e 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68  n the comment th
1a77f 61 74 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20  at follows each 
1a780 63 61 73 65 20 61 72 65 20 75 73 65 64 20 74 6f  case are used to
1a781 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20 74 68  .** construct th
1a782 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49  e OPFLG_INITIALI
1a783 5a 45 52 20 76 61 6c 75 65 20 74 68 61 74 20 69  ZER value that i
1a784 6e 69 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f 64  nitializes opcod
1a785 65 50 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20  eProperty[]..** 
1a786 4b 65 79 77 6f 72 64 73 20 69 6e 63 6c 75 64 65  Keywords include
1a787 3a 20 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 2c  : in1, in2, in3,
1a788 20 6f 75 74 32 5f 70 72 65 72 65 6c 65 61 73 65   out2_prerelease
1a789 2c 20 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20 53  , out2, out3.  S
1a78a 65 65 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f  ee.** the mkopco
1a78b 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66  deh.awk script f
1a78c 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
1a78d 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
1a78e 20 44 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 61   Documentation a
1a78f 62 6f 75 74 20 56 44 42 45 20 6f 70 63 6f 64 65  bout VDBE opcode
1a790 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62  s is generated b
1a791 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 20  y scanning this 
1a792 66 69 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65  file.** for line
1a793 73 20 6f 66 20 74 68 61 74 20 63 6f 6e 74 61 69  s of that contai
1a794 6e 20 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54 68  n "Opcode:".  Th
1a795 61 74 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c 20  at line and all 
1a796 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 6f  subsequent.** co
1a797 6d 6d 65 6e 74 20 6c 69 6e 65 73 20 61 72 65 20  mment lines are 
1a798 75 73 65 64 20 69 6e 20 74 68 65 20 67 65 6e 65  used in the gene
1a799 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70  ration of the op
1a79a 63 6f 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d 65  code.html docume
1a79b 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e  ntation.** file.
1a79c 0a 2a 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a  .**.** SUMMARY:.
1a79d 2a 2a 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61 74  **.**     Format
1a79e 74 69 6e 67 20 69 73 20 69 6d 70 6f 72 74 61 6e  ting is importan
1a79f 74 20 74 6f 20 73 63 72 69 70 74 73 20 74 68 61  t to scripts tha
1a7a0 74 20 73 63 61 6e 20 74 68 69 73 20 66 69 6c 65  t scan this file
1a7a1 2e 0a 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20  ..**     Do not 
1a7a2 64 65 76 69 61 74 65 20 66 72 6f 6d 20 74 68 65  deviate from the
1a7a3 20 66 6f 72 6d 61 74 74 69 6e 67 20 73 74 79 6c   formatting styl
1a7a4 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  e currently in u
1a7a5 73 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  se..**.*********
1a7a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a7a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a7a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a7a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a7aa 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ****/../* Opcode
1a7ab 3a 20 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20 2a  :  Goto * P2 * *
1a7ac 20 2a 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f   *.**.** An unco
1a7ad 6e 64 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 74  nditional jump t
1a7ae 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a  o address P2..**
1a7af 20 54 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   The next instru
1a7b0 63 74 69 6f 6e 20 65 78 65 63 75 74 65 64 20 77  ction executed w
1a7b1 69 6c 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20 6f  ill be .** the o
1a7b2 6e 65 20 61 74 20 69 6e 64 65 78 20 50 32 20 66  ne at index P2 f
1a7b3 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
1a7b4 67 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 67  g of.** the prog
1a7b5 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ram..*/.case OP_
1a7b6 47 6f 74 6f 3a 20 7b 20 20 20 20 20 20 20 20 20  Goto: {         
1a7b7 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
1a7b8 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52   CHECK_FOR_INTER
1a7b9 52 55 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f 70  RUPT;.  pc = pOp
1a7ba 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61  ->p2 - 1;.  brea
1a7bb 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1a7bc 20 20 47 6f 73 75 62 20 50 31 20 50 32 20 2a 20    Gosub P1 P2 * 
1a7bd 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
1a7be 74 68 65 20 63 75 72 72 65 6e 74 20 61 64 64 72  the current addr
1a7bf 65 73 73 20 6f 6e 74 6f 20 72 65 67 69 73 74 65  ess onto registe
1a7c0 72 20 50 31 0a 2a 2a 20 61 6e 64 20 74 68 65 6e  r P1.** and then
1a7c1 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73   jump to address
1a7c2 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P2..*/.case OP_
1a7c3 47 6f 73 75 62 3a 20 7b 20 20 20 20 20 20 20 20  Gosub: {        
1a7c4 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
1a7c5 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1a7c6 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
1a7c7 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p1<=p->nMem
1a7c8 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d   );.  pIn1 = &p-
1a7c9 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
1a7ca 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d    assert( (pIn1-
1a7cb 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e  >flags & MEM_Dyn
1a7cc 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e  )==0 );.  pIn1->
1a7cd 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1a7ce 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70  .  pIn1->u.i = p
1a7cf 63 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  c;.  REGISTER_TR
1a7d0 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e  ACE(pOp->p1, pIn
1a7d1 31 29 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e  1);.  pc = pOp->
1a7d2 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b  p2 - 1;.  break;
1a7d3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  .}../* Opcode:  
1a7d4 52 65 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a 20  Return P1 * * * 
1a7d5 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20  *.**.** Jump to 
1a7d6 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
1a7d7 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20 61  tion after the a
1a7d8 64 64 72 65 73 73 20 69 6e 20 72 65 67 69 73 74  ddress in regist
1a7d9 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P1..*/.case O
1a7da 50 5f 52 65 74 75 72 6e 3a 20 7b 20 20 20 20 20  P_Return: {     
1a7db 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a        /* in1 */.
1a7dc 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e    assert( pIn1->
1a7dd 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
1a7de 29 3b 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 70  );.  pc = (int)p
1a7df 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61  In1->u.i;.  brea
1a7e0 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1a7e1 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a    Yield P1 * * *
1a7e2 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68   *.**.** Swap th
1a7e3 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  e program counte
1a7e4 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65  r with the value
1a7e5 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e   in register P1.
1a7e6 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c  .*/.case OP_Yiel
1a7e7 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  d: {            
1a7e8 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74 20  /* in1 */.  int 
1a7e9 70 63 44 65 73 74 3b 0a 20 20 61 73 73 65 72 74  pcDest;.  assert
1a7ea 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1a7eb 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a   MEM_Dyn)==0 );.
1a7ec 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20    pIn1->flags = 
1a7ed 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 63 44 65 73  MEM_Int;.  pcDes
1a7ee 74 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75  t = (int)pIn1->u
1a7ef 2e 69 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20  .i;.  pIn1->u.i 
1a7f0 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54 45 52  = pc;.  REGISTER
1a7f1 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20  _TRACE(pOp->p1, 
1a7f2 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20 70 63  pIn1);.  pc = pc
1a7f3 44 65 73 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Dest;.  break;.}
1a7f4 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48  .../* Opcode:  H
1a7f5 61 6c 74 20 50 31 20 50 32 20 2a 20 50 34 20 2a  alt P1 P2 * P4 *
1a7f6 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d 6d 65  .**.** Exit imme
1a7f7 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 6f 70  diately.  All op
1a7f8 65 6e 20 63 75 72 73 6f 72 73 2c 20 65 74 63 20  en cursors, etc 
1a7f9 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a 20 61 75  are closed.** au
1a7fa 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a  tomatically..**.
1a7fb 2a 2a 20 50 31 20 69 73 20 74 68 65 20 72 65 73  ** P1 is the res
1a7fc 75 6c 74 20 63 6f 64 65 20 72 65 74 75 72 6e 65  ult code returne
1a7fd 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65 78 65  d by sqlite3_exe
1a7fe 63 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73  c(), sqlite3_res
1a7ff 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69  et(),.** or sqli
1a800 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 2e 20  te3_finalize(). 
1a801 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c 20 68 61   For a normal ha
1a802 6c 74 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20  lt, this should 
1a803 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29  be SQLITE_OK (0)
1a804 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f 72 73 2c  ..** For errors,
1a805 20 69 74 20 63 61 6e 20 62 65 20 73 6f 6d 65 20   it can be some 
1a806 6f 74 68 65 72 20 76 61 6c 75 65 2e 20 20 49 66  other value.  If
1a807 20 50 31 21 3d 30 20 74 68 65 6e 20 50 32 20 77   P1!=0 then P2 w
1a808 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a  ill determine.**
1a809 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1a80a 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  to rollback the 
1a80b 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
1a80c 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72 6f 6c  ion.  Do not rol
1a80d 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d  lback.** if P2==
1a80e 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74 68 65 20  OE_Fail. Do the 
1a80f 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d  rollback if P2==
1a810 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66  OE_Rollback.  If
1a811 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a   P2==OE_Abort,.*
1a812 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f 75 74 20  * then back out 
1a813 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 68 61 74  all changes that
1a814 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 64   have occurred d
1a815 75 72 69 6e 67 20 74 68 69 73 20 65 78 65 63 75  uring this execu
1a816 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 56  tion of the.** V
1a817 44 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20  DBE, but do not 
1a818 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61  rollback the tra
1a819 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a  nsaction. .**.**
1a81a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 6e 75   If P4 is not nu
1a81b 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 20 61 6e  ll then it is an
1a81c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73   error message s
1a81d 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tring..**.** The
1a81e 72 65 20 69 73 20 61 6e 20 69 6d 70 6c 69 65 64  re is an implied
1a81f 20 22 48 61 6c 74 20 30 20 30 20 30 22 20 69 6e   "Halt 0 0 0" in
1a820 73 74 72 75 63 74 69 6f 6e 20 69 6e 73 65 72 74  struction insert
1a821 65 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65  ed at the very e
1a822 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72 79 20 70  nd of.** every p
1a823 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61 20 6a 75  rogram.  So a ju
1a824 6d 70 20 70 61 73 74 20 74 68 65 20 6c 61 73 74  mp past the last
1a825 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20   instruction of 
1a826 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69  the program.** i
1a827 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 65 78  s the same as ex
1a828 65 63 75 74 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f  ecuting Halt..*/
1a829 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 3a 20 7b  .case OP_Halt: {
1a82a 0a 20 20 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e  .  p->rc = pOp->
1a82b 70 31 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63  p1;.  p->pc = pc
1a82c 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69  ;.  p->errorActi
1a82d 6f 6e 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20  on = pOp->p2;.  
1a82e 69 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b  if( pOp->p4.z ){
1a82f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1a830 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1a831 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 70 4f 70  g, db, "%s", pOp
1a832 2d 3e 70 34 2e 7a 29 3b 0a 20 20 7d 0a 20 20 72  ->p4.z);.  }.  r
1a833 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 48  c = sqlite3VdbeH
1a834 61 6c 74 28 70 29 3b 0a 20 20 61 73 73 65 72 74  alt(p);.  assert
1a835 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc==SQLITE_BUS
1a836 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  Y || rc==SQLITE_
1a837 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  OK );.  if( rc==
1a838 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20  SQLITE_BUSY ){. 
1a839 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20     p->rc = rc = 
1a83a 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
1a83b 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70  else{.    rc = p
1a83c 2d 3e 72 63 20 3f 20 53 51 4c 49 54 45 5f 45 52  ->rc ? SQLITE_ER
1a83d 52 4f 52 20 3a 20 53 51 4c 49 54 45 5f 44 4f 4e  ROR : SQLITE_DON
1a83e 45 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64  E;.  }.  goto vd
1a83f 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  be_return;.}../*
1a840 20 4f 70 63 6f 64 65 3a 20 49 6e 74 65 67 65 72   Opcode: Integer
1a841 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1a842 2a 2a 20 54 68 65 20 33 32 2d 62 69 74 20 69 6e  ** The 32-bit in
1a843 74 65 67 65 72 20 76 61 6c 75 65 20 50 31 20 69  teger value P1 i
1a844 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 72  s written into r
1a845 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63  egister P2..*/.c
1a846 61 73 65 20 4f 50 5f 49 6e 74 65 67 65 72 3a 20  ase OP_Integer: 
1a847 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74  {         /* out
1a848 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
1a849 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20    pOut->flags = 
1a84a 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d  MEM_Int;.  pOut-
1a84b 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  >u.i = pOp->p1;.
1a84c 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1a84d 70 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 50  pcode: Int64 * P
1a84e 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50  2 * P4 *.**.** P
1a84f 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
1a850 6f 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67  o a 64-bit integ
1a851 65 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69  er value..** Wri
1a852 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e  te that value in
1a853 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1a854 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 34  */.case OP_Int64
1a855 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  : {           /*
1a856 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1a857 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
1a858 70 2d 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 3b  p->p4.pI64!=0 );
1a859 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d  .  pOut->flags =
1a85a 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74   MEM_Int;.  pOut
1a85b 2d 3e 75 2e 69 20 3d 20 2a 70 4f 70 2d 3e 70 34  ->u.i = *pOp->p4
1a85c 2e 70 49 36 34 3b 0a 20 20 62 72 65 61 6b 3b 0a  .pI64;.  break;.
1a85d 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65  }../* Opcode: Re
1a85e 61 6c 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a  al * P2 * P4 *.*
1a85f 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69  *.** P4 is a poi
1a860 6e 74 65 72 20 74 6f 20 61 20 36 34 2d 62 69 74  nter to a 64-bit
1a861 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
1a862 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20  value..** Write 
1a863 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 20  that value into 
1a864 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a  register P2..*/.
1a865 63 61 73 65 20 4f 50 5f 52 65 61 6c 3a 20 7b 20  case OP_Real: { 
1a866 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1a867 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 2c 20  me as TK_FLOAT, 
1a868 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1a869 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  */.  pOut->flags
1a86a 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 61   = MEM_Real;.  a
1a86b 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 49  ssert( !sqlite3I
1a86c 73 4e 61 4e 28 2a 70 4f 70 2d 3e 70 34 2e 70 52  sNaN(*pOp->p4.pR
1a86d 65 61 6c 29 20 29 3b 0a 20 20 70 4f 75 74 2d 3e  eal) );.  pOut->
1a86e 72 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65  r = *pOp->p4.pRe
1a86f 61 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  al;.  break;.}..
1a870 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e  /* Opcode: Strin
1a871 67 38 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a  g8 * P2 * P4 *.*
1a872 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74  *.** P4 points t
1a873 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74  o a nul terminat
1a874 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e  ed UTF-8 string.
1a875 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20   This opcode is 
1a876 74 72 61 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a 20  transformed .** 
1a877 69 6e 74 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e  into an OP_Strin
1a878 67 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 65  g before it is e
1a879 78 65 63 75 74 65 64 20 66 6f 72 20 74 68 65 20  xecuted for the 
1a87a 66 69 72 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a 63  first time..*/.c
1a87b 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20  ase OP_String8: 
1a87c 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d  {         /* sam
1a87d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 2c 20  e as TK_STRING, 
1a87e 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1a87f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1a880 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70  ->p4.z!=0 );.  p
1a881 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f  Op->opcode = OP_
1a882 53 74 72 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e 70  String;.  pOp->p
1a883 31 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  1 = sqlite3Strle
1a884 6e 33 30 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a  n30(pOp->p4.z);.
1a885 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a886 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28  OMIT_UTF16.  if(
1a887 20 65 6e 63 6f 64 69 6e 67 21 3d 53 51 4c 49 54   encoding!=SQLIT
1a888 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 73 71  E_UTF8 ){.    sq
1a889 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
1a88a 74 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34  tr(pOut, pOp->p4
1a88b 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55  .z, -1, SQLITE_U
1a88c 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  TF8, SQLITE_STAT
1a88d 49 43 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  IC);.    if( SQL
1a88e 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56  ITE_OK!=sqlite3V
1a88f 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
1a890 67 28 70 4f 75 74 2c 20 65 6e 63 6f 64 69 6e 67  g(pOut, encoding
1a891 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  ) ) goto no_mem;
1a892 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
1a893 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62 65 4d  OK!=sqlite3VdbeM
1a894 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
1a895 70 4f 75 74 29 20 29 20 67 6f 74 6f 20 6e 6f 5f  pOut) ) goto no_
1a896 6d 65 6d 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a  mem;.    pOut->z
1a897 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20  Malloc = 0;.    
1a898 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pOut->flags |= M
1a899 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70  EM_Static;.    p
1a89a 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d  Out->flags &= ~M
1a89b 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20  EM_Dyn;.    if( 
1a89c 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1a89d 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20  DYNAMIC ){.     
1a89e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1a89f 62 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  b, pOp->p4.z);. 
1a8a0 20 20 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34     }.    pOp->p4
1a8a1 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49  type = P4_DYNAMI
1a8a2 43 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a  C;.    pOp->p4.z
1a8a3 20 3d 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20   = pOut->z;.    
1a8a4 70 4f 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e  pOp->p1 = pOut->
1a8a5 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  n;.    if( pOp->
1a8a6 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  p1>db->aLimit[SQ
1a8a7 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1a8a8 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  H] ){.      goto
1a8a9 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a   too_big;.    }.
1a8aa 20 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42      UPDATE_MAX_B
1a8ab 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20  LOBSIZE(pOut);. 
1a8ac 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 23 65     break;.  }.#e
1a8ad 6e 64 69 66 0a 20 20 69 66 28 20 70 4f 70 2d 3e  ndif.  if( pOp->
1a8ae 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  p1>db->aLimit[SQ
1a8af 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1a8b0 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  H] ){.    goto t
1a8b1 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 2f 2a  oo_big;.  }.  /*
1a8b2 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   Fall through to
1a8b3 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 2c 20   the next case, 
1a8b4 4f 50 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20  OP_String */.}. 
1a8b5 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72   ./* Opcode: Str
1a8b6 69 6e 67 20 50 31 20 50 32 20 2a 20 50 34 20 2a  ing P1 P2 * P4 *
1a8b7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e  .**.** The strin
1a8b8 67 20 76 61 6c 75 65 20 50 34 20 6f 66 20 6c 65  g value P4 of le
1a8b9 6e 67 74 68 20 50 31 20 28 62 79 74 65 73 29 20  ngth P1 (bytes) 
1a8ba 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  is stored in reg
1a8bb 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73  ister P2..*/.cas
1a8bc 65 20 4f 50 5f 53 74 72 69 6e 67 3a 20 7b 20 20  e OP_String: {  
1a8bd 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1a8be 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1a8bf 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
1a8c0 7a 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e  z!=0 );.  pOut->
1a8c1 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c  flags = MEM_Str|
1a8c2 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 54  MEM_Static|MEM_T
1a8c3 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e 7a 20 3d  erm;.  pOut->z =
1a8c4 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 70 4f   pOp->p4.z;.  pO
1a8c5 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b  ut->n = pOp->p1;
1a8c6 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65  .  pOut->enc = e
1a8c7 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54  ncoding;.  UPDAT
1a8c8 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1a8c9 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1a8ca 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c  ../* Opcode: Nul
1a8cb 6c 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a  l * P2 * * *.**.
1a8cc 2a 2a 20 57 72 69 74 65 20 61 20 4e 55 4c 4c 20  ** Write a NULL 
1a8cd 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
1a8ce 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c  ..*/.case OP_Nul
1a8cf 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f  l: {           /
1a8d0 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1a8d1 65 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  e */.  break;.}.
1a8d2 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f  ../* Opcode: Blo
1a8d3 62 20 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a 0a  b P1 P2 * P4.**.
1a8d4 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20  ** P4 points to 
1a8d5 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50  a blob of data P
1a8d6 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53  1 bytes long.  S
1a8d7 74 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c 6f  tore this.** blo
1a8d8 62 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  b in register P2
1a8d9 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  . This instructi
1a8da 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 20  on is not coded 
1a8db 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 74  directly.** by t
1a8dc 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e 73  he compiler. Ins
1a8dd 74 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 6c  tead, the compil
1a8de 65 72 20 6c 61 79 65 72 20 73 70 65 63 69 66 69  er layer specifi
1a8df 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 42  es.** an OP_HexB
1a8e0 6c 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 68  lob opcode, with
1a8e1 20 74 68 65 20 68 65 78 20 73 74 72 69 6e 67 20   the hex string 
1a8e2 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
1a8e3 66 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 73  f.** the blob as
1a8e4 20 50 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 65   P4. This opcode
1a8e5 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20   is transformed 
1a8e6 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a  to an OP_Blob.**
1a8e7 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   the first time 
1a8e8 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a  it is executed..
1a8e9 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a  */.case OP_Blob:
1a8ea 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
1a8eb 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1a8ec 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74  ease */.  assert
1a8ed 28 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 4c  ( pOp->p1 <= SQL
1a8ee 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29  ITE_MAX_LENGTH )
1a8ef 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1a8f0 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70  emSetStr(pOut, p
1a8f1 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70  Op->p4.z, pOp->p
1a8f2 31 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 74  1, 0, 0);.  pOut
1a8f3 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67  ->enc = encoding
1a8f4 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  ;.  UPDATE_MAX_B
1a8f5 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20  LOBSIZE(pOut);. 
1a8f6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1a8f7 63 6f 64 65 3a 20 56 61 72 69 61 62 6c 65 20 50  code: Variable P
1a8f8 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1a8f9 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 76 61   The value of va
1a8fa 72 69 61 62 6c 65 20 50 31 20 69 73 20 77 72 69  riable P1 is wri
1a8fb 74 74 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74  tten into regist
1a8fc 65 72 20 50 32 2e 20 41 20 76 61 72 69 61 62 6c  er P2. A variabl
1a8fd 65 20 69 73 0a 2a 2a 20 61 6e 20 75 6e 6b 6e 6f  e is.** an unkno
1a8fe 77 6e 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e  wn in the origin
1a8ff 61 6c 20 53 51 4c 20 73 74 72 69 6e 67 20 61 73  al SQL string as
1a900 20 68 61 6e 64 65 64 20 74 6f 20 73 71 6c 69 74   handed to sqlit
1a901 65 33 5f 63 6f 6d 70 69 6c 65 28 29 2e 0a 2a 2a  e3_compile()..**
1a902 20 41 6e 79 20 6f 63 63 75 72 72 65 6e 63 65 20   Any occurrence 
1a903 6f 66 20 74 68 65 20 27 3f 27 20 63 68 61 72 61  of the '?' chara
1a904 63 74 65 72 20 69 6e 20 74 68 65 20 6f 72 69 67  cter in the orig
1a905 69 6e 61 6c 20 53 51 4c 20 69 73 20 63 6f 6e 73  inal SQL is cons
1a906 69 64 65 72 65 64 0a 2a 2a 20 61 20 76 61 72 69  idered.** a vari
1a907 61 62 6c 65 2e 20 20 56 61 72 69 61 62 6c 65 73  able.  Variables
1a908 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69   in the SQL stri
1a909 6e 67 20 61 72 65 20 6e 75 6d 62 65 72 20 66 72  ng are number fr
1a90a 6f 6d 20 6c 65 66 74 20 74 6f 0a 2a 2a 20 72 69  om left to.** ri
1a90b 67 68 74 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ght beginning wi
1a90c 74 68 20 31 2e 20 20 54 68 65 20 76 61 6c 75 65  th 1.  The value
1a90d 73 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 61  s of variables a
1a90e 72 65 20 73 65 74 20 75 73 69 6e 67 20 74 68 65  re set using the
1a90f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  .** sqlite3_bind
1a910 28 29 20 41 50 49 2e 0a 2a 2f 0a 63 61 73 65 20  () API..*/.case 
1a911 4f 50 5f 56 61 72 69 61 62 6c 65 3a 20 7b 20 20  OP_Variable: {  
1a912 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1a913 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20  -prerelease */. 
1a914 20 69 6e 74 20 6a 20 3d 20 70 4f 70 2d 3e 70 31   int j = pOp->p1
1a915 20 2d 20 31 3b 0a 20 20 4d 65 6d 20 2a 70 56 61   - 1;.  Mem *pVa
1a916 72 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d  r;.  assert( j>=
1a917 30 20 26 26 20 6a 3c 70 2d 3e 6e 56 61 72 20 29  0 && j<p->nVar )
1a918 3b 0a 0a 20 20 70 56 61 72 20 3d 20 26 70 2d 3e  ;..  pVar = &p->
1a919 61 56 61 72 5b 6a 5d 3b 0a 20 20 69 66 28 20 73  aVar[j];.  if( s
1a91a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f  qlite3VdbeMemToo
1a91b 42 69 67 28 70 56 61 72 29 20 29 7b 0a 20 20 20  Big(pVar) ){.   
1a91c 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
1a91d 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
1a91e 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70  MemShallowCopy(p
1a91f 4f 75 74 2c 20 26 70 2d 3e 61 56 61 72 5b 6a 5d  Out, &p->aVar[j]
1a920 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20  , MEM_Static);. 
1a921 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1a922 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72  SIZE(pOut);.  br
1a923 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1a924 65 3a 20 4d 6f 76 65 20 50 31 20 50 32 20 50 33  e: Move P1 P2 P3
1a925 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20   * *.**.** Move 
1a926 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65  the values in re
1a927 67 69 73 74 65 72 20 50 31 2e 2e 50 31 2b 50 33  gister P1..P1+P3
1a928 2d 31 20 6f 76 65 72 20 69 6e 74 6f 0a 2a 2a 20  -1 over into.** 
1a929 72 65 67 69 73 74 65 72 73 20 50 32 2e 2e 50 32  registers P2..P2
1a92a 2b 50 33 2d 31 2e 20 20 52 65 67 69 73 74 65 72  +P3-1.  Register
1a92b 73 20 50 31 2e 2e 50 31 2b 50 31 2d 31 20 61 72  s P1..P1+P1-1 ar
1a92c 65 0a 2a 2a 20 6c 65 66 74 20 68 6f 6c 64 69 6e  e.** left holdin
1a92d 67 20 61 20 4e 55 4c 4c 2e 20 20 49 74 20 69 73  g a NULL.  It is
1a92e 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 72 65   an error for re
1a92f 67 69 73 74 65 72 20 72 61 6e 67 65 73 0a 2a 2a  gister ranges.**
1a930 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 61 6e 64   P1..P1+P3-1 and
1a931 20 50 32 2e 2e 50 32 2b 50 33 2d 31 20 74 6f 20   P2..P2+P3-1 to 
1a932 6f 76 65 72 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65  overlap..*/.case
1a933 20 4f 50 5f 4d 6f 76 65 3a 20 7b 0a 20 20 63 68   OP_Move: {.  ch
1a934 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69  ar *zMalloc;.  i
1a935 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a  nt n = pOp->p3;.
1a936 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e    int p1 = pOp->
1a937 70 31 3b 0a 20 20 69 6e 74 20 70 32 20 3d 20 70  p1;.  int p2 = p
1a938 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74  Op->p2;.  assert
1a939 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ( n>0 );.  asser
1a93a 74 28 20 70 31 3e 30 20 29 3b 0a 20 20 61 73 73  t( p1>0 );.  ass
1a93b 65 72 74 28 20 70 31 2b 6e 3c 70 2d 3e 6e 4d 65  ert( p1+n<p->nMe
1a93c 6d 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70  m );.  pIn1 = &p
1a93d 2d 3e 61 4d 65 6d 5b 70 31 5d 3b 0a 20 20 61 73  ->aMem[p1];.  as
1a93e 73 65 72 74 28 20 70 32 3e 30 20 29 3b 0a 20 20  sert( p2>0 );.  
1a93f 61 73 73 65 72 74 28 20 70 32 2b 6e 3c 70 2d 3e  assert( p2+n<p->
1a940 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d  nMem );.  pOut =
1a941 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 5d 3b 0a 20   &p->aMem[p2];. 
1a942 20 61 73 73 65 72 74 28 20 70 31 2b 6e 3c 3d 70   assert( p1+n<=p
1a943 32 20 7c 7c 20 70 32 2b 6e 3c 3d 70 31 20 29 3b  2 || p2+n<=p1 );
1a944 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29 7b  .  while( n-- ){
1a945 0a 20 20 20 20 7a 4d 61 6c 6c 6f 63 20 3d 20 70  .    zMalloc = p
1a946 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20  Out->zMalloc;.  
1a947 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20    pOut->zMalloc 
1a948 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
1a949 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74  VdbeMemMove(pOut
1a94a 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20 70 49 6e  , pIn1);.    pIn
1a94b 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4d 61  1->zMalloc = zMa
1a94c 6c 6c 6f 63 3b 0a 20 20 20 20 52 45 47 49 53 54  lloc;.    REGIST
1a94d 45 52 5f 54 52 41 43 45 28 70 32 2b 2b 2c 20 70  ER_TRACE(p2++, p
1a94e 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b  Out);.    pIn1++
1a94f 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20  ;.    pOut++;.  
1a950 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1a951 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31   Opcode: Copy P1
1a952 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1a953 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72  Make a copy of r
1a954 65 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20  egister P1 into 
1a955 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a  register P2..**.
1a956 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
1a957 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70  ion makes a deep
1a958 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c   copy of the val
1a959 75 65 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65  ue.  A duplicate
1a95a 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61  .** is made of a
1a95b 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f  ny string or blo
1a95c 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65  b constant.  See
1a95d 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a   also OP_SCopy..
1a95e 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a  */.case OP_Copy:
1a95f 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
1a960 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72  * in1 */.  asser
1a961 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a  t( pOp->p2>0 );.
1a962 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1a963 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  2<=p->nMem );.  
1a964 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
1a965 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65  pOp->p2];.  asse
1a966 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29  rt( pOut!=pIn1 )
1a967 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1a968 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f  emShallowCopy(pO
1a969 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70  ut, pIn1, MEM_Ep
1a96a 68 65 6d 29 3b 0a 20 20 44 65 65 70 68 65 6d 65  hem);.  Deepheme
1a96b 72 61 6c 69 7a 65 28 70 4f 75 74 29 3b 0a 20 20  ralize(pOut);.  
1a96c 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1a96d 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20  Op->p2, pOut);. 
1a96e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1a96f 63 6f 64 65 3a 20 53 43 6f 70 79 20 50 31 20 50  code: SCopy P1 P
1a970 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61  2 * * *.**.** Ma
1a971 6b 65 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70  ke a shallow cop
1a972 79 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31  y of register P1
1a973 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1a974 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  2..**.** This in
1a975 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20  struction makes 
1a976 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f  a shallow copy o
1a977 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 49 66  f the value.  If
1a978 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73   the value.** is
1a979 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
1a97a 62 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 70 79  b, then the copy
1a97b 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74   is only a point
1a97c 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6f 72 69  er to the.** ori
1a97d 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e 63 65 20  ginal and hence 
1a97e 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  if the original 
1a97f 63 68 61 6e 67 65 73 20 73 6f 20 77 69 6c 6c 20  changes so will 
1a980 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 6f 72  the copy..** Wor
1a981 73 65 2c 20 69 66 20 74 68 65 20 6f 72 69 67 69  se, if the origi
1a982 6e 61 6c 20 69 73 20 64 65 61 6c 6c 6f 63 61 74  nal is deallocat
1a983 65 64 2c 20 74 68 65 20 63 6f 70 79 20 62 65 63  ed, the copy bec
1a984 6f 6d 65 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a  omes invalid..**
1a985 20 54 68 75 73 20 74 68 65 20 70 72 6f 67 72 61   Thus the progra
1a986 6d 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65  m must guarantee
1a987 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e   that the origin
1a988 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e  al will not chan
1a989 67 65 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65  ge.** during the
1a98a 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65   lifetime of the
1a98b 20 63 6f 70 79 2e 20 20 55 73 65 20 4f 50 5f 43   copy.  Use OP_C
1a98c 6f 70 79 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f  opy to make a co
1a98d 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 2e 0a  mplete.** copy..
1a98e 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 43 6f 70 79  */.case OP_SCopy
1a98f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
1a990 2a 20 69 6e 31 20 2a 2f 0a 20 20 52 45 47 49 53  * in1 */.  REGIS
1a991 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1a992 31 2c 20 70 49 6e 31 29 3b 0a 20 20 61 73 73 65  1, pIn1);.  asse
1a993 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b  rt( pOp->p2>0 );
1a994 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1a995 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p2<=p->nMem );. 
1a996 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d   pOut = &p->aMem
1a997 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73  [pOp->p2];.  ass
1a998 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20  ert( pOut!=pIn1 
1a999 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1a99a 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70  MemShallowCopy(p
1a99b 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45  Out, pIn1, MEM_E
1a99c 70 68 65 6d 29 3b 0a 20 20 52 45 47 49 53 54 45  phem);.  REGISTE
1a99d 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c  R_TRACE(pOp->p2,
1a99e 20 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b   pOut);.  break;
1a99f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .}../* Opcode: R
1a9a0 65 73 75 6c 74 52 6f 77 20 50 31 20 50 32 20 2a  esultRow P1 P2 *
1a9a1 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72   * *.**.** The r
1a9a2 65 67 69 73 74 65 72 73 20 50 31 20 74 68 72 6f  egisters P1 thro
1a9a3 75 67 68 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74  ugh P1+P2-1 cont
1a9a4 61 69 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  ain a single row
1a9a5 20 6f 66 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20   of.** results. 
1a9a6 54 68 69 73 20 6f 70 63 6f 64 65 20 63 61 75 73  This opcode caus
1a9a7 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  es the sqlite3_s
1a9a8 74 65 70 28 29 20 63 61 6c 6c 20 74 6f 20 74 65  tep() call to te
1a9a9 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20  rminate.** with 
1a9aa 61 6e 20 53 51 4c 49 54 45 5f 52 4f 57 20 72 65  an SQLITE_ROW re
1a9ab 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 69 74  turn code and it
1a9ac 20 73 65 74 73 20 75 70 20 74 68 65 20 73 71 6c   sets up the sql
1a9ad 69 74 65 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72  ite3_stmt.** str
1a9ae 75 63 74 75 72 65 20 74 6f 20 70 72 6f 76 69 64  ucture to provid
1a9af 65 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  e access to the 
1a9b0 74 6f 70 20 50 31 20 76 61 6c 75 65 73 20 61 73  top P1 values as
1a9b1 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72   the result.** r
1a9b2 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  ow..*/.case OP_R
1a9b3 65 73 75 6c 74 52 6f 77 3a 20 7b 0a 20 20 4d 65  esultRow: {.  Me
1a9b4 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 69  m *pMem;.  int i
1a9b5 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
1a9b6 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e  ResColumn==pOp->
1a9b7 70 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p2 );.  assert( 
1a9b8 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61  pOp->p1>0 );.  a
1a9b9 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 2b 70  ssert( pOp->p1+p
1a9ba 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p2<=p->nMem 
1a9bb 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 64  );..  /* Invalid
1a9bc 61 74 65 20 61 6c 6c 20 65 70 68 65 6d 65 72 61  ate all ephemera
1a9bd 6c 20 63 75 72 73 6f 72 20 72 6f 77 20 63 61 63  l cursor row cac
1a9be 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63 61 63 68  hes */.  p->cach
1a9bf 65 43 74 72 20 3d 20 28 70 2d 3e 63 61 63 68 65  eCtr = (p->cache
1a9c0 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f  Ctr + 2)|1;..  /
1a9c1 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
1a9c2 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 63  results of the c
1a9c3 75 72 72 65 6e 74 20 72 6f 77 20 61 72 65 20 5c  urrent row are \
1a9c4 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 20  000 terminated. 
1a9c5 20 2a 2a 20 61 6e 64 20 68 61 76 65 20 61 6e 20   ** and have an 
1a9c6 61 73 73 69 67 6e 65 64 20 74 79 70 65 2e 20 20  assigned type.  
1a9c7 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  The results are 
1a9c8 64 65 2d 65 70 68 65 6d 65 72 61 6c 69 7a 65 64  de-ephemeralized
1a9c9 20 61 73 0a 20 20 2a 2a 20 61 73 20 73 69 64 65   as.  ** as side
1a9ca 20 65 66 66 65 63 74 2e 0a 20 20 2a 2f 0a 20 20   effect..  */.  
1a9cb 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c  pMem = p->pResul
1a9cc 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  tSet = &p->aMem[
1a9cd 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28  pOp->p1];.  for(
1a9ce 69 3d 30 3b 20 69 3c 70 4f 70 2d 3e 70 32 3b 20  i=0; i<pOp->p2; 
1a9cf 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
1a9d0 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
1a9d1 6e 61 74 65 28 26 70 4d 65 6d 5b 69 5d 29 3b 0a  nate(&pMem[i]);.
1a9d2 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66      storeTypeInf
1a9d3 6f 28 26 70 4d 65 6d 5b 69 5d 2c 20 65 6e 63 6f  o(&pMem[i], enco
1a9d4 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 47 49 53  ding);.    REGIS
1a9d5 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1a9d6 31 2b 69 2c 20 26 70 4d 65 6d 5b 69 5d 29 3b 0a  1+i, &pMem[i]);.
1a9d7 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
1a9d8 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
1a9d9 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20  o no_mem;..  /* 
1a9da 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f  Return SQLITE_RO
1a9db 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 6e 43 61 6c  W.  */.  p->nCal
1a9dc 6c 62 61 63 6b 2b 2b 3b 0a 20 20 70 2d 3e 70 63  lback++;.  p->pc
1a9dd 20 3d 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20   = pc + 1;.  rc 
1a9de 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20  = SQLITE_ROW;.  
1a9df 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1a9e0 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1a9e1 43 6f 6e 63 61 74 20 50 31 20 50 32 20 50 33 20  Concat P1 P2 P3 
1a9e2 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68  * *.**.** Add th
1a9e3 65 20 74 65 78 74 20 69 6e 20 72 65 67 69 73 74  e text in regist
1a9e4 65 72 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65  er P1 onto the e
1a9e5 6e 64 20 6f 66 20 74 68 65 20 74 65 78 74 20 69  nd of the text i
1a9e6 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32  n.** register P2
1a9e7 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
1a9e8 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65  esult in registe
1a9e9 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68  r P3..** If eith
1a9ea 65 72 20 74 68 65 20 50 31 20 6f 72 20 50 32 20  er the P1 or P2 
1a9eb 74 65 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68  text are NULL th
1a9ec 65 6e 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e  en store NULL in
1a9ed 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20   P3..**.**   P3 
1a9ee 3d 20 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a  = P2 || P1.**.**
1a9ef 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66   It is illegal f
1a9f0 6f 72 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20  or P1 and P3 to 
1a9f1 62 65 20 74 68 65 20 73 61 6d 65 20 72 65 67 69  be the same regi
1a9f2 73 74 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c  ster. Sometimes,
1a9f3 0a 2a 2a 20 69 66 20 50 33 20 69 73 20 74 68 65  .** if P3 is the
1a9f4 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 20 61   same register a
1a9f5 73 20 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d  s P2, the implem
1a9f6 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65  entation is able
1a9f7 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d  .** to avoid a m
1a9f8 65 6d 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65  emcpy()..*/.case
1a9f9 20 4f 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20   OP_Concat: {   
1a9fa 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1a9fb 61 73 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e  as TK_CONCAT, in
1a9fc 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a  1, in2, out3 */.
1a9fd 20 20 69 36 34 20 6e 42 79 74 65 3b 0a 0a 20 20    i64 nByte;..  
1a9fe 61 73 73 65 72 74 28 20 70 49 6e 31 21 3d 70 4f  assert( pIn1!=pO
1a9ff 75 74 20 29 3b 0a 20 20 69 66 28 20 28 70 49 6e  ut );.  if( (pIn
1aa00 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d  1->flags | pIn2-
1aa01 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75  >flags) & MEM_Nu
1aa02 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ll ){.    sqlite
1aa03 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
1aa04 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b  pOut);.    break
1aa05 3b 0a 20 20 7d 0a 20 20 45 78 70 61 6e 64 42 6c  ;.  }.  ExpandBl
1aa06 6f 62 28 70 49 6e 31 29 3b 0a 20 20 53 74 72 69  ob(pIn1);.  Stri
1aa07 6e 67 69 66 79 28 70 49 6e 31 2c 20 65 6e 63 6f  ngify(pIn1, enco
1aa08 64 69 6e 67 29 3b 0a 20 20 45 78 70 61 6e 64 42  ding);.  ExpandB
1aa09 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 53 74 72  lob(pIn2);.  Str
1aa0a 69 6e 67 69 66 79 28 70 49 6e 32 2c 20 65 6e 63  ingify(pIn2, enc
1aa0b 6f 64 69 6e 67 29 3b 0a 20 20 6e 42 79 74 65 20  oding);.  nByte 
1aa0c 3d 20 70 49 6e 31 2d 3e 6e 20 2b 20 70 49 6e 32  = pIn1->n + pIn2
1aa0d 2d 3e 6e 3b 0a 20 20 69 66 28 20 6e 42 79 74 65  ->n;.  if( nByte
1aa0e 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
1aa0f 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
1aa10 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f   ){.    goto too
1aa11 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53  _big;.  }.  MemS
1aa12 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1aa13 20 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28   MEM_Str);.  if(
1aa14 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
1aa15 72 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 6e  row(pOut, (int)n
1aa16 42 79 74 65 2b 32 2c 20 70 4f 75 74 3d 3d 70 49  Byte+2, pOut==pI
1aa17 6e 32 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  n2) ){.    goto 
1aa18 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 69 66  no_mem;.  }.  if
1aa19 28 20 70 4f 75 74 21 3d 70 49 6e 32 20 29 7b 0a  ( pOut!=pIn2 ){.
1aa1a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 75 74 2d      memcpy(pOut-
1aa1b 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c 20 70 49 6e  >z, pIn2->z, pIn
1aa1c 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d  2->n);.  }.  mem
1aa1d 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b 70 49 6e  cpy(&pOut->z[pIn
1aa1e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e 7a 2c 20  2->n], pIn1->z, 
1aa1f 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 70 4f 75 74  pIn1->n);.  pOut
1aa20 2d 3e 7a 5b 6e 42 79 74 65 5d 20 3d 20 30 3b 0a  ->z[nByte] = 0;.
1aa21 20 20 70 4f 75 74 2d 3e 7a 5b 6e 42 79 74 65 2b    pOut->z[nByte+
1aa22 31 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e  1] = 0;.  pOut->
1aa23 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72  flags |= MEM_Ter
1aa24 6d 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28  m;.  pOut->n = (
1aa25 69 6e 74 29 6e 42 79 74 65 3b 0a 20 20 70 4f 75  int)nByte;.  pOu
1aa26 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e  t->enc = encodin
1aa27 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  g;.  UPDATE_MAX_
1aa28 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1aa29 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1aa2a 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20 50 32  pcode: Add P1 P2
1aa2b 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64   P3 * *.**.** Ad
1aa2c 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  d the value in r
1aa2d 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 68  egister P1 to th
1aa2e 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1aa2f 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74  ter P2.** and st
1aa30 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1aa31 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1aa32 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
1aa33 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1aa34 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1aa35 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 6c  /./* Opcode: Mul
1aa36 74 69 70 6c 79 20 50 31 20 50 32 20 50 33 20 2a  tiply P1 P2 P3 *
1aa37 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74   *.**.**.** Mult
1aa38 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65 20 69  iply the value i
1aa39 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79  n register P1 by
1aa3a 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1aa3b 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64  gister P2.** and
1aa3c 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
1aa3d 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1aa3e 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1aa3f 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1aa40 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1aa41 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1aa42 53 75 62 74 72 61 63 74 20 50 31 20 50 32 20 50  Subtract P1 P2 P
1aa43 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75 62 74  3 * *.**.** Subt
1aa44 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 69  ract the value i
1aa45 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 66 72  n register P1 fr
1aa46 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  om the value in 
1aa47 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61  register P2.** a
1aa48 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
1aa49 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1aa4a 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1aa4b 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
1aa4c 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
1aa4d 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
1aa4e 3a 20 44 69 76 69 64 65 20 50 31 20 50 32 20 50  : Divide P1 P2 P
1aa4f 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69 76 69  3 * *.**.** Divi
1aa50 64 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  de the value in 
1aa51 72 65 67 69 73 74 65 72 20 50 31 20 62 79 20 74  register P1 by t
1aa52 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1aa53 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73  ster P2.** and s
1aa54 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1aa55 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 20  in register P3. 
1aa56 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   If the value in
1aa57 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20   register P2.** 
1aa58 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  is zero, then th
1aa59 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1aa5a 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1aa5b 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1aa5c 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1aa5d 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1aa5e 52 65 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20  Remainder P1 P2 
1aa5f 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d  P3 * *.**.** Com
1aa60 70 75 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64  pute the remaind
1aa61 65 72 20 61 66 74 65 72 20 69 6e 74 65 67 65 72  er after integer
1aa62 20 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65   division of the
1aa63 20 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67   value in.** reg
1aa64 69 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20  ister P1 by the 
1aa65 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1aa66 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74  r P2 and store t
1aa67 68 65 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e  he result in P3.
1aa68 20 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75   .** If the valu
1aa69 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  e in register P2
1aa6a 20 69 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73   is zero the res
1aa6b 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20  ult is NULL..** 
1aa6c 49 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e  If either operan
1aa6d 64 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  d is NULL, the r
1aa6e 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1aa6f 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20  /.case OP_Add:  
1aa70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aa71 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50   /* same as TK_P
1aa72 4c 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  LUS, in1, in2, o
1aa73 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  ut3 */.case OP_S
1aa74 75 62 74 72 61 63 74 3a 20 20 20 20 20 20 20 20  ubtract:        
1aa75 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1aa76 20 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20   TK_MINUS, in1, 
1aa77 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73  in2, out3 */.cas
1aa78 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20  e OP_Multiply:  
1aa79 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1aa7a 61 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20  ame as TK_STAR, 
1aa7b 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1aa7c 2f 0a 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65  /.case OP_Divide
1aa7d 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
1aa7e 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53   /* same as TK_S
1aa7f 4c 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  LASH, in1, in2, 
1aa80 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  out3 */.case OP_
1aa81 52 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20  Remainder: {    
1aa82 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1aa83 73 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69  s TK_REM, in1, i
1aa84 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69 6e  n2, out3 */.  in
1aa85 74 20 66 6c 61 67 73 3b 0a 20 20 61 70 70 6c 79  t flags;.  apply
1aa86 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28  NumericAffinity(
1aa87 70 49 6e 31 29 3b 0a 20 20 61 70 70 6c 79 4e 75  pIn1);.  applyNu
1aa88 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49  mericAffinity(pI
1aa89 6e 32 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20 70  n2);.  flags = p
1aa8a 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e  In1->flags | pIn
1aa8b 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20  2->flags;.  if( 
1aa8c 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  (flags & MEM_Nul
1aa8d 6c 29 21 3d 30 20 29 20 67 6f 74 6f 20 61 72 69  l)!=0 ) goto ari
1aa8e 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
1aa8f 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 28 70  s_null;.  if( (p
1aa90 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e  In1->flags & pIn
1aa91 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  2->flags & MEM_I
1aa92 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0a  nt)==MEM_Int ){.
1aa93 20 20 20 20 69 36 34 20 61 2c 20 62 3b 0a 20 20      i64 a, b;.  
1aa94 20 20 61 20 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b    a = pIn1->u.i;
1aa95 0a 20 20 20 20 62 20 3d 20 70 49 6e 32 2d 3e 75  .    b = pIn2->u
1aa96 2e 69 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20  .i;.    switch( 
1aa97 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20  pOp->opcode ){. 
1aa98 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64       case OP_Add
1aa99 3a 20 20 20 20 20 20 20 20 20 62 20 2b 3d 20 61  :         b += a
1aa9a 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;       break;. 
1aa9b 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62       case OP_Sub
1aa9c 74 72 61 63 74 3a 20 20 20 20 62 20 2d 3d 20 61  tract:    b -= a
1aa9d 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;       break;. 
1aa9e 20 20 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c       case OP_Mul
1aa9f 74 69 70 6c 79 3a 20 20 20 20 62 20 2a 3d 20 61  tiply:    b *= a
1aaa0 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;       break;. 
1aaa1 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76       case OP_Div
1aaa2 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  ide: {.        i
1aaa3 66 28 20 61 3d 3d 30 20 29 20 67 6f 74 6f 20 61  f( a==0 ) goto a
1aaa4 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74  rithmetic_result
1aaa5 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20  _is_null;.      
1aaa6 20 20 2f 2a 20 44 69 76 69 64 69 6e 67 20 74 68    /* Dividing th
1aaa7 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62  e largest possib
1aaa8 6c 65 20 6e 65 67 61 74 69 76 65 20 36 34 2d 62  le negative 64-b
1aaa9 69 74 20 69 6e 74 65 67 65 72 20 28 31 3c 3c 36  it integer (1<<6
1aaaa 33 29 20 62 79 20 0a 20 20 20 20 20 20 20 20 2a  3) by .        *
1aaab 2a 20 2d 31 20 72 65 74 75 72 6e 73 20 61 6e 20  * -1 returns an 
1aaac 69 6e 74 65 67 65 72 20 74 6f 6f 20 6c 61 72 67  integer too larg
1aaad 65 20 74 6f 20 73 74 6f 72 65 20 69 6e 20 61 20  e to store in a 
1aaae 36 34 2d 62 69 74 20 64 61 74 61 2d 74 79 70 65  64-bit data-type
1aaaf 2e 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  . On.        ** 
1aab0 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72  some architectur
1aab1 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 76  es, the value ov
1aab2 65 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c 3c 36  erflows to (1<<6
1aab3 33 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c 0a 20  3). On others,. 
1aab4 20 20 20 20 20 20 20 2a 2a 20 61 20 53 49 47 46         ** a SIGF
1aab5 50 45 20 69 73 20 69 73 73 75 65 64 2e 20 54 68  PE is issued. Th
1aab6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74  e following stat
1aab7 65 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a 65 73  ement normalizes
1aab8 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a   this.        **
1aab9 20 62 65 68 61 76 69 6f 72 20 73 6f 20 74 68 61   behavior so tha
1aaba 74 20 61 6c 6c 20 61 72 63 68 69 74 65 63 74 75  t all architectu
1aabb 72 65 73 20 62 65 68 61 76 65 20 61 73 20 69 66  res behave as if
1aabc 20 69 6e 74 65 67 65 72 20 0a 20 20 20 20 20 20   integer .      
1aabd 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6f 63    ** overflow oc
1aabe 63 75 72 72 65 64 2e 0a 20 20 20 20 20 20 20 20  curred..        
1aabf 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  */.        if( a
1aac0 3d 3d 2d 31 20 26 26 20 62 3d 3d 53 4d 41 4c 4c  ==-1 && b==SMALL
1aac1 45 53 54 5f 49 4e 54 36 34 20 29 20 61 20 3d 20  EST_INT64 ) a = 
1aac2 31 3b 0a 20 20 20 20 20 20 20 20 62 20 2f 3d 20  1;.        b /= 
1aac3 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  a;.        break
1aac4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1aac5 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
1aac6 20 20 20 69 66 28 20 61 3d 3d 30 20 29 20 67 6f     if( a==0 ) go
1aac7 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65  to arithmetic_re
1aac8 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20  sult_is_null;.  
1aac9 20 20 20 20 20 20 69 66 28 20 61 3d 3d 2d 31 20        if( a==-1 
1aaca 29 20 61 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ) a = 1;.       
1aacb 20 62 20 25 3d 20 61 3b 0a 20 20 20 20 20 20 20   b %= a;.       
1aacc 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1aacd 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e      }.    pOut->
1aace 75 2e 69 20 3d 20 62 3b 0a 20 20 20 20 4d 65 6d  u.i = b;.    Mem
1aacf 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1aad0 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65  , MEM_Int);.  }e
1aad1 6c 73 65 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20  lse{.    double 
1aad2 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20 73 71  a, b;.    a = sq
1aad3 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
1aad4 75 65 28 70 49 6e 31 29 3b 0a 20 20 20 20 62 20  ue(pIn1);.    b 
1aad5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  = sqlite3VdbeRea
1aad6 6c 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20  lValue(pIn2);.  
1aad7 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f    switch( pOp->o
1aad8 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 63  pcode ){.      c
1aad9 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20  ase OP_Add:     
1aada 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20 20      b += a;     
1aadb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1aadc 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a  ase OP_Subtract:
1aadd 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20 20      b -= a;     
1aade 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1aadf 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a  ase OP_Multiply:
1aae0 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20 20      b *= a;     
1aae1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1aae2 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b  ase OP_Divide: {
1aae3 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d  .        if( a==
1aae4 30 2e 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68  0.0 ) goto arith
1aae5 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
1aae6 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 62 20  null;.        b 
1aae7 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 62 72  /= a;.        br
1aae8 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1aae9 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
1aaea 20 20 20 20 20 20 69 36 34 20 69 61 20 3d 20 28        i64 ia = (
1aaeb 69 36 34 29 61 3b 0a 20 20 20 20 20 20 20 20 69  i64)a;.        i
1aaec 36 34 20 69 62 20 3d 20 28 69 36 34 29 62 3b 0a  64 ib = (i64)b;.
1aaed 20 20 20 20 20 20 20 20 69 66 28 20 69 61 3d 3d          if( ia==
1aaee 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65  0 ) goto arithme
1aaef 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75  tic_result_is_nu
1aaf0 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ll;.        if( 
1aaf1 69 61 3d 3d 2d 31 20 29 20 69 61 20 3d 20 31 3b  ia==-1 ) ia = 1;
1aaf2 0a 20 20 20 20 20 20 20 20 62 20 3d 20 28 64 6f  .        b = (do
1aaf3 75 62 6c 65 29 28 69 62 20 25 20 69 61 29 3b 0a  uble)(ib % ia);.
1aaf4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1aaf5 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1aaf6 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61   if( sqlite3IsNa
1aaf7 4e 28 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  N(b) ){.      go
1aaf8 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65  to arithmetic_re
1aaf9 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20  sult_is_null;.  
1aafa 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e 72 20    }.    pOut->r 
1aafb 3d 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54  = b;.    MemSetT
1aafc 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1aafd 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66 28  M_Real);.    if(
1aafe 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65   (flags & MEM_Re
1aaff 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  al)==0 ){.      
1ab00 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67  sqlite3VdbeInteg
1ab01 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75 74 29  erAffinity(pOut)
1ab02 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
1ab03 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69 63  eak;..arithmetic
1ab04 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a  _result_is_null:
1ab05 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1ab06 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a  mSetNull(pOut);.
1ab07 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1ab08 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a  pcode: CollSeq *
1ab09 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69   * P4.**.** P4 i
1ab0a 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1ab0b 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e   CollSeq struct.
1ab0c 20 49 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c   If the next cal
1ab0d 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63  l to a user func
1ab0e 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65  tion.** or aggre
1ab0f 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 74  gate calls sqlit
1ab10 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71  e3GetFuncCollSeq
1ab11 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 69  (), this collati
1ab12 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c  on sequence will
1ab13 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 64 2e  .** be returned.
1ab14 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
1ab15 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69   the built-in mi
1ab16 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e  n(), max() and n
1ab17 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74  ullif().** funct
1ab18 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ions..**.** The 
1ab19 69 6e 74 65 72 66 61 63 65 20 75 73 65 64 20 62  interface used b
1ab1a 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  y the implementa
1ab1b 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f 72  tion of the afor
1ab1c 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74  ementioned funct
1ab1d 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69  ions.** to retri
1ab1e 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  eve the collatio
1ab1f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74 20 62  n sequence set b
1ab20 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73  y this opcode is
1ab21 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a   not available.*
1ab22 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79  * publicly, only
1ab23 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f   to user functio
1ab24 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20 66 75  ns defined in fu
1ab25 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  nc.c..*/.case OP
1ab26 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61 73  _CollSeq: {.  as
1ab27 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70  sert( pOp->p4typ
1ab28 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b  e==P4_COLLSEQ );
1ab29 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1ab2a 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f 6e  Opcode: Function
1ab2b 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1ab2c 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 75  **.** Invoke a u
1ab2d 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 34  ser function (P4
1ab2e 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1ab2f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 75   a Function stru
1ab30 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 64 65  cture that.** de
1ab31 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74 69  fines the functi
1ab32 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72 67 75  on) with P5 argu
1ab33 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f 6d  ments taken from
1ab34 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64   register P2 and
1ab35 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 20  .** successors. 
1ab36 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   The result of t
1ab37 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73  he function is s
1ab38 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1ab39 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74 65  r P3..** Registe
1ab3a 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 62 65  r P3 must not be
1ab3b 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 6e 63   one of the func
1ab3c 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a  tion inputs..**.
1ab3d 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62 69  ** P1 is a 32-bi
1ab3e 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61  t bitmask indica
1ab3f 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20  ting whether or 
1ab40 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e  not each argumen
1ab41 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e  t to the .** fun
1ab42 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d  ction was determ
1ab43 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74  ined to be const
1ab44 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74  ant at compile t
1ab45 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72 73  ime. If the firs
1ab46 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61  t.** argument wa
1ab47 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20  s constant then 
1ab48 62 69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73  bit 0 of P1 is s
1ab49 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  et. This is used
1ab4a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a   to determine.**
1ab4b 20 77 68 65 74 68 65 72 20 6d 65 74 61 20 64 61   whether meta da
1ab4c 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ta associated wi
1ab4d 74 68 20 61 20 75 73 65 72 20 66 75 6e 63 74 69  th a user functi
1ab4e 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e  on argument usin
1ab4f 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  g the.** sqlite3
1ab50 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41  _set_auxdata() A
1ab51 50 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79  PI may be safely
1ab52 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20   retained until 
1ab53 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f  the next.** invo
1ab54 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f  cation of this o
1ab55 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  pcode..**.** See
1ab56 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61   also: AggStep a
1ab57 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63  nd AggFinal.*/.c
1ab58 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a  ase OP_Function:
1ab59 20 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65   {.  int i;.  Me
1ab5a 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74  m *pArg;.  sqlit
1ab5b 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a  e3_context ctx;.
1ab5c 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1ab5d 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e  **apVal;.  int n
1ab5e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 0a 20 20 61   = pOp->p5;..  a
1ab5f 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b  pVal = p->apArg;
1ab60 0a 20 20 61 73 73 65 72 74 28 20 61 70 56 61 6c  .  assert( apVal
1ab61 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20 20 61   || n==0 );..  a
1ab62 73 73 65 72 74 28 20 6e 3d 3d 30 20 7c 7c 20 28  ssert( n==0 || (
1ab63 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70  pOp->p2>0 && pOp
1ab64 2d 3e 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d 29  ->p2+n<=p->nMem)
1ab65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1ab66 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c  p->p3<pOp->p2 ||
1ab67 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70   pOp->p3>=pOp->p
1ab68 32 2b 6e 20 29 3b 0a 20 20 70 41 72 67 20 3d 20  2+n );.  pArg = 
1ab69 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1ab6a 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ];.  for(i=0; i<
1ab6b 6e 3b 20 69 2b 2b 2c 20 70 41 72 67 2b 2b 29 7b  n; i++, pArg++){
1ab6c 0a 20 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20  .    apVal[i] = 
1ab6d 70 41 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54  pArg;.    storeT
1ab6e 79 70 65 49 6e 66 6f 28 70 41 72 67 2c 20 65 6e  ypeInfo(pArg, en
1ab6f 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 47  coding);.    REG
1ab70 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1ab71 3e 70 32 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a  >p2, pArg);.  }.
1ab72 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1ab73 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44  p4type==P4_FUNCD
1ab74 45 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70  EF || pOp->p4typ
1ab75 65 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43 20 29  e==P4_VDBEFUNC )
1ab76 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74  ;.  if( pOp->p4t
1ab77 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20  ype==P4_FUNCDEF 
1ab78 29 7b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63  ){.    ctx.pFunc
1ab79 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63   = pOp->p4.pFunc
1ab7a 3b 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46  ;.    ctx.pVdbeF
1ab7b 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  unc = 0;.  }else
1ab7c 7b 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46  {.    ctx.pVdbeF
1ab7d 75 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a  unc = (VdbeFunc*
1ab7e 29 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75  )pOp->p4.pVdbeFu
1ab7f 6e 63 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e  nc;.    ctx.pFun
1ab80 63 20 3d 20 63 74 78 2e 70 56 64 62 65 46 75 6e  c = ctx.pVdbeFun
1ab81 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20  c->pFunc;.  }.. 
1ab82 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1ab83 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70  >0 && pOp->p3<=p
1ab84 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74  ->nMem );.  pOut
1ab85 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1ab86 3e 70 33 5d 3b 0a 20 20 63 74 78 2e 73 2e 66 6c  >p3];.  ctx.s.fl
1ab87 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
1ab88 20 20 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b    ctx.s.db = db;
1ab89 0a 20 20 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20  .  ctx.s.xDel = 
1ab8a 30 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 6c 6c  0;.  ctx.s.zMall
1ab8b 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68  oc = 0;..  /* Th
1ab8c 65 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61  e output cell ma
1ab8d 79 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61  y already have a
1ab8e 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65   buffer allocate
1ab8f 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65  d. Move.  ** the
1ab90 20 70 6f 69 6e 74 65 72 20 74 6f 20 63 74 78 2e   pointer to ctx.
1ab91 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65  s so in case the
1ab92 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 63   user-function c
1ab93 61 6e 20 75 73 65 0a 20 20 2a 2a 20 74 68 65 20  an use.  ** the 
1ab94 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65  already allocate
1ab95 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64  d buffer instead
1ab96 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61   of allocating a
1ab97 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20   new one..  */. 
1ab98 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
1ab99 6f 76 65 28 26 63 74 78 2e 73 2c 20 70 4f 75 74  ove(&ctx.s, pOut
1ab9a 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  );.  MemSetTypeF
1ab9b 6c 61 67 28 26 63 74 78 2e 73 2c 20 4d 45 4d 5f  lag(&ctx.s, MEM_
1ab9c 4e 75 6c 6c 29 3b 0a 0a 20 20 63 74 78 2e 69 73  Null);..  ctx.is
1ab9d 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28  Error = 0;.  if(
1ab9e 20 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67   ctx.pFunc->flag
1ab9f 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  s & SQLITE_FUNC_
1aba0 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20  NEEDCOLL ){.    
1aba1 61 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61  assert( pOp>p->a
1aba2 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Op );.    assert
1aba3 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65  ( pOp[-1].p4type
1aba4 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a  ==P4_COLLSEQ );.
1aba5 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b      assert( pOp[
1aba6 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43  -1].opcode==OP_C
1aba7 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74  ollSeq );.    ct
1aba8 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31  x.pColl = pOp[-1
1aba9 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a  ].p4.pColl;.  }.
1abaa 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1abab 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74  etyOff(db) ) got
1abac 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1abad 69 73 75 73 65 3b 0a 20 20 28 2a 63 74 78 2e 70  isuse;.  (*ctx.p
1abae 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 63 74  Func->xFunc)(&ct
1abaf 78 2c 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20  x, n, apVal);.  
1abb0 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1abb1 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73  yOn(db) ){.    s
1abb2 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1abb3 65 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20  ease(&ctx.s);.  
1abb4 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
1abb5 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a  _to_misuse;.  }.
1abb6 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
1abb7 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a  Failed ){.    /*
1abb8 20 45 76 65 6e 20 74 68 6f 75 67 68 20 61 20 6d   Even though a m
1abb9 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c  alloc() has fail
1abba 65 64 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ed, the implemen
1abbb 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20  tation of the.  
1abbc 20 20 2a 2a 20 75 73 65 72 20 66 75 6e 63 74 69    ** user functi
1abbd 6f 6e 20 6d 61 79 20 68 61 76 65 20 63 61 6c 6c  on may have call
1abbe 65 64 20 61 6e 20 73 71 6c 69 74 65 33 5f 72 65  ed an sqlite3_re
1abbf 73 75 6c 74 5f 58 58 58 28 29 20 66 75 6e 63 74  sult_XXX() funct
1abc0 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65  ion.    ** to re
1abc1 74 75 72 6e 20 61 20 76 61 6c 75 65 2e 20 54 68  turn a value. Th
1abc2 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c  e following call
1abc3 20 72 65 6c 65 61 73 65 73 20 61 6e 79 20 72 65   releases any re
1abc4 73 6f 75 72 63 65 73 0a 20 20 20 20 2a 2a 20 61  sources.    ** a
1abc5 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73  ssociated with s
1abc6 75 63 68 20 61 20 76 61 6c 75 65 2e 0a 20 20 20  uch a value..   
1abc7 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a   **.    ** Note:
1abc8 20 4d 61 79 62 65 20 4d 65 6d 52 65 6c 65 61 73   Maybe MemReleas
1abc9 65 28 29 20 73 68 6f 75 6c 64 20 62 65 20 63 61  e() should be ca
1abca 6c 6c 65 64 20 69 66 20 73 71 6c 69 74 65 33 53  lled if sqlite3S
1abcb 61 66 65 74 79 4f 6e 28 29 0a 20 20 20 20 2a 2a  afetyOn().    **
1abcc 20 66 61 69 6c 73 20 61 6c 73 6f 20 28 74 68 65   fails also (the
1abcd 20 69 66 28 2e 2e 2e 29 20 73 74 61 74 65 6d 65   if(...) stateme
1abce 6e 74 20 61 62 6f 76 65 29 2e 20 42 75 74 20 69  nt above). But i
1abcf 66 20 70 65 6f 70 6c 65 20 61 72 65 0a 20 20 20  f people are.   
1abd0 20 2a 2a 20 6d 69 73 75 73 69 6e 67 20 73 71 6c   ** misusing sql
1abd1 69 74 65 2c 20 74 68 65 79 20 68 61 76 65 20 62  ite, they have b
1abd2 69 67 67 65 72 20 70 72 6f 62 6c 65 6d 73 20 74  igger problems t
1abd3 68 61 6e 20 61 20 6c 65 61 6b 65 64 20 76 61 6c  han a leaked val
1abd4 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ue..    */.    s
1abd5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1abd6 65 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20  ease(&ctx.s);.  
1abd7 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1abd8 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20   }..  /* If any 
1abd9 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 20 66  auxiliary data f
1abda 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65  unctions have be
1abdb 65 6e 20 63 61 6c 6c 65 64 20 62 79 20 74 68 69  en called by thi
1abdc 73 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c  s user function,
1abdd 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  .  ** immediatel
1abde 79 20 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72  y call the destr
1abdf 75 63 74 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f  uctor for any no
1abe0 6e 2d 73 74 61 74 69 63 20 76 61 6c 75 65 73 2e  n-static values.
1abe1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 74 78 2e  .  */.  if( ctx.
1abe2 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20  pVdbeFunc ){.   
1abe3 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
1abe4 74 65 41 75 78 44 61 74 61 28 63 74 78 2e 70 56  teAuxData(ctx.pV
1abe5 64 62 65 46 75 6e 63 2c 20 70 4f 70 2d 3e 70 31  dbeFunc, pOp->p1
1abe6 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70  );.    pOp->p4.p
1abe7 56 64 62 65 46 75 6e 63 20 3d 20 63 74 78 2e 70  VdbeFunc = ctx.p
1abe8 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 70 4f  VdbeFunc;.    pO
1abe9 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56  p->p4type = P4_V
1abea 44 42 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20  DBEFUNC;.  }..  
1abeb 2f 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  /* If the functi
1abec 6f 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65  on returned an e
1abed 72 72 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20 65  rror, throw an e
1abee 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 66  xception */.  if
1abef 28 20 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b  ( ctx.isError ){
1abf0 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1abf1 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1abf2 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
1abf3 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1abf4 26 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63  &ctx.s));.    rc
1abf5 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a   = ctx.isError;.
1abf6 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74    }..  /* Copy t
1abf7 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1abf8 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72   function into r
1abf9 65 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20  egister P3 */.  
1abfa 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1abfb 65 45 6e 63 6f 64 69 6e 67 28 26 63 74 78 2e 73  eEncoding(&ctx.s
1abfc 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73  , encoding);.  s
1abfd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1abfe 65 28 70 4f 75 74 2c 20 26 63 74 78 2e 73 29 3b  e(pOut, &ctx.s);
1abff 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
1ac00 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f 75 74  beMemTooBig(pOut
1ac01 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f  ) ){.    goto to
1ac02 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52 45 47  o_big;.  }.  REG
1ac03 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1ac04 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50  >p3, pOut);.  UP
1ac05 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1ac06 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  E(pOut);.  break
1ac07 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1ac08 42 69 74 41 6e 64 20 50 31 20 50 32 20 50 33 20  BitAnd P1 P2 P3 
1ac09 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74  * *.**.** Take t
1ac0a 68 65 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20  he bit-wise AND 
1ac0b 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
1ac0c 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64   register P1 and
1ac0d 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65   P2 and.** store
1ac0e 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
1ac0f 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
1ac10 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1ac11 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
1ac12 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
1ac13 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20  * Opcode: BitOr 
1ac14 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1ac15 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d  ** Take the bit-
1ac16 77 69 73 65 20 4f 52 20 6f 66 20 74 68 65 20 76  wise OR of the v
1ac17 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65  alues in registe
1ac18 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a  r P1 and P2 and.
1ac19 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  ** store the res
1ac1a 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1ac1b 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1ac1c 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
1ac1d 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
1ac1e 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
1ac1f 3a 20 53 68 69 66 74 4c 65 66 74 20 50 31 20 50  : ShiftLeft P1 P
1ac20 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  2 P3 * *.**.** S
1ac21 68 69 66 74 20 74 68 65 20 69 6e 74 65 67 65 72  hift the integer
1ac22 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1ac23 65 72 20 50 32 20 74 6f 20 74 68 65 20 6c 65 66  er P2 to the lef
1ac24 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  t by the.** numb
1ac25 65 72 20 6f 66 20 62 69 74 73 20 73 70 65 63 69  er of bits speci
1ac26 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 74 65  fied by the inte
1ac27 67 65 72 20 69 6e 20 72 65 67 69 73 65 72 20 50  ger in regiser P
1ac28 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  1..** Store the 
1ac29 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1ac2a 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1ac2b 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1ac2c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1ac2d 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1ac2e 6f 64 65 3a 20 53 68 69 66 74 52 69 67 68 74 20  ode: ShiftRight 
1ac2f 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1ac30 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74  ** Shift the int
1ac31 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65  eger value in re
1ac32 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65  gister P2 to the
1ac33 20 72 69 67 68 74 20 62 79 20 74 68 65 0a 2a 2a   right by the.**
1ac34 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20   number of bits 
1ac35 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
1ac36 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69   integer in regi
1ac37 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72  ster P1..** Stor
1ac38 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  e the result in 
1ac39 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20  register P3..** 
1ac3a 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
1ac3b 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73  is NULL, the res
1ac3c 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ult is NULL..*/.
1ac3d 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20  case OP_BitAnd: 
1ac3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ac3f 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49  /* same as TK_BI
1ac40 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  TAND, in1, in2, 
1ac41 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  out3 */.case OP_
1ac42 42 69 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20  BitOr:          
1ac43 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1ac44 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69 6e 31  as TK_BITOR, in1
1ac45 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63  , in2, out3 */.c
1ac46 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74  ase OP_ShiftLeft
1ac47 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  :              /
1ac48 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48  * same as TK_LSH
1ac49 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  IFT, in1, in2, o
1ac4a 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  ut3 */.case OP_S
1ac4b 68 69 66 74 52 69 67 68 74 3a 20 7b 20 20 20 20  hiftRight: {    
1ac4c 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1ac4d 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 69 6e 31  s TK_RSHIFT, in1
1ac4e 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20  , in2, out3 */. 
1ac4f 20 69 36 34 20 61 2c 20 62 3b 0a 0a 20 20 69 66   i64 a, b;..  if
1ac50 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c  ( (pIn1->flags |
1ac51 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20   pIn2->flags) & 
1ac52 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1ac53 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1ac54 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20  tNull(pOut);.   
1ac55 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 61 20   break;.  }.  a 
1ac56 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
1ac57 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 62  Value(pIn2);.  b
1ac58 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
1ac59 74 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20  tValue(pIn1);.  
1ac5a 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63  switch( pOp->opc
1ac5b 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ode ){.    case 
1ac5c 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20 20 20 20  OP_BitAnd:      
1ac5d 61 20 26 3d 20 62 3b 20 20 20 20 20 62 72 65 61  a &= b;     brea
1ac5e 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42  k;.    case OP_B
1ac5f 69 74 4f 72 3a 20 20 20 20 20 20 20 61 20 7c 3d  itOr:       a |=
1ac60 20 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20   b;     break;. 
1ac61 20 20 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74     case OP_Shift
1ac62 4c 65 66 74 3a 20 20 20 61 20 3c 3c 3d 20 62 3b  Left:   a <<= b;
1ac63 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
1ac64 65 66 61 75 6c 74 3a 20 20 61 73 73 65 72 74 28  efault:  assert(
1ac65 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1ac66 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20  _ShiftRight );. 
1ac67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ac68 20 20 20 20 20 20 20 20 61 20 3e 3e 3d 20 62 3b          a >>= b;
1ac69 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
1ac6a 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 61 3b 0a   pOut->u.i = a;.
1ac6b 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1ac6c 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1ac6d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1ac6e 4f 70 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 20  Opcode: AddImm  
1ac6f 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a  P1 P2 * * *.** .
1ac70 2a 2a 20 41 64 64 20 74 68 65 20 63 6f 6e 73 74  ** Add the const
1ac71 61 6e 74 20 50 32 20 74 6f 20 74 68 65 20 76 61  ant P2 to the va
1ac72 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1ac73 50 31 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  P1..** The resul
1ac74 74 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69  t is always an i
1ac75 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f  nteger..**.** To
1ac76 20 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73   force any regis
1ac77 74 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74  ter to be an int
1ac78 65 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30  eger, just add 0
1ac79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64  ..*/.case OP_Add
1ac7a 49 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20  Imm: {          
1ac7b 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 71    /* in1 */.  sq
1ac7c 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
1ac7d 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20  gerify(pIn1);.  
1ac7e 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70  pIn1->u.i += pOp
1ac7f 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  ->p2;.  break;.}
1ac80 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73  ../* Opcode: Mus
1ac81 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a  tBeInt P1 P2 * *
1ac82 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20   *.** .** Force 
1ac83 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1ac84 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 61  ister P1 to be a
1ac85 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74  n integer.  If t
1ac86 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50  he value.** in P
1ac87 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  1 is not an inte
1ac88 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62  ger and cannot b
1ac89 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  e converted into
1ac8a 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77   an integer.** w
1ac8b 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73  ithout data loss
1ac8c 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65  , then jump imme
1ac8d 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f  diately to P2, o
1ac8e 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61  r if P2==0.** ra
1ac8f 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49  ise an SQLITE_MI
1ac90 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e  SMATCH exception
1ac91 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73  ..*/.case OP_Mus
1ac92 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20  tBeInt: {       
1ac93 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1ac94 31 20 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66 69  1 */.  applyAffi
1ac95 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54  nity(pIn1, SQLIT
1ac96 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65  E_AFF_NUMERIC, e
1ac97 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28 20  ncoding);.  if( 
1ac98 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
1ac99 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20  EM_Int)==0 ){.  
1ac9a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30    if( pOp->p2==0
1ac9b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
1ac9c 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b 0a  QLITE_MISMATCH;.
1ac9d 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
1ac9e 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1ac9f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1aca0 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1aca1 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1aca2 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
1aca3 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49 6e  lag(pIn1, MEM_In
1aca4 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  t);.  }.  break;
1aca5 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .}../* Opcode: R
1aca6 65 61 6c 41 66 66 69 6e 69 74 79 20 50 31 20 2a  ealAffinity P1 *
1aca7 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20   * * *.**.** If 
1aca8 72 65 67 69 73 74 65 72 20 50 31 20 68 6f 6c 64  register P1 hold
1aca9 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e  s an integer con
1acaa 76 65 72 74 20 69 74 20 74 6f 20 61 20 72 65 61  vert it to a rea
1acab 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  l value..**.** T
1acac 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73  his opcode is us
1acad 65 64 20 77 68 65 6e 20 65 78 74 72 61 63 74 69  ed when extracti
1acae 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ng information f
1acaf 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61  rom a column tha
1acb0 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c 20 61 66  t.** has REAL af
1acb1 66 69 6e 69 74 79 2e 20 20 53 75 63 68 20 63 6f  finity.  Such co
1acb2 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79 20  lumn values may 
1acb3 73 74 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20  still be stored 
1acb4 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c 20  as.** integers, 
1acb5 66 6f 72 20 73 70 61 63 65 20 65 66 66 69 63 69  for space effici
1acb6 65 6e 63 79 2c 20 62 75 74 20 61 66 74 65 72 20  ency, but after 
1acb7 65 78 74 72 61 63 74 69 6f 6e 20 77 65 20 77 61  extraction we wa
1acb8 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68 61  nt them.** to ha
1acb9 76 65 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20 76  ve only a real v
1acba 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  alue..*/.case OP
1acbb 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 3a 20 7b  _RealAffinity: {
1acbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1acbd 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 66    /* in1 */.  if
1acbe 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
1acbf 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73  MEM_Int ){.    s
1acc0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61  qlite3VdbeMemRea
1acc1 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a  lify(pIn1);.  }.
1acc2 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e    break;.}..#ifn
1acc3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1acc4 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  CAST./* Opcode: 
1acc5 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a 20  ToText P1 * * * 
1acc6 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68  *.**.** Force th
1acc7 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1acc8 74 65 72 20 50 31 20 74 6f 20 62 65 20 74 65 78  ter P1 to be tex
1acc9 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c  t..** If the val
1acca 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63  ue is numeric, c
1accb 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73  onvert it to a s
1accc 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a  tring using the.
1accd 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66  ** equivalent of
1acce 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62   printf().  Blob
1accf 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68   values are unch
1acd0 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65  anged and.** are
1acd1 20 61 66 74 65 72 77 61 72 64 73 20 73 69 6d 70   afterwards simp
1acd2 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  ly interpreted a
1acd3 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20  s text..**.** A 
1acd4 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f  NULL value is no
1acd5 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  t changed by thi
1acd6 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72  s routine.  It r
1acd7 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  emains NULL..*/.
1acd8 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20  case OP_ToText: 
1acd9 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
1acda 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1acdb 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f  _TO_TEXT, in1 */
1acdc 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61  .  if( pIn1->fla
1acdd 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20  gs & MEM_Null ) 
1acde 62 72 65 61 6b 3b 0a 20 20 61 73 73 65 72 74 28  break;.  assert(
1acdf 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f 42   MEM_Str==(MEM_B
1ace0 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49 6e  lob>>3) );.  pIn
1ace1 31 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49 6e  1->flags |= (pIn
1ace2 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f  1->flags&MEM_Blo
1ace3 62 29 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41 66  b)>>3;.  applyAf
1ace4 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c  finity(pIn1, SQL
1ace5 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e  ITE_AFF_TEXT, en
1ace6 63 6f 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d 20  coding);.  rc = 
1ace7 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29  ExpandBlob(pIn1)
1ace8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31  ;.  assert( pIn1
1ace9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74  ->flags & MEM_St
1acea 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
1aceb 61 69 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31 2d  ailed );.  pIn1-
1acec 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
1aced 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d  Int|MEM_Real|MEM
1acee 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 29 3b  _Blob|MEM_Zero);
1acef 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
1acf0 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20  OBSIZE(pIn1);.  
1acf1 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1acf2 6f 64 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 2a  ode: ToBlob P1 *
1acf3 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72   * * *.**.** For
1acf4 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ce the value in 
1acf5 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62  register P1 to b
1acf6 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 20  e a BLOB..** If 
1acf7 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d  the value is num
1acf8 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74  eric, convert it
1acf9 20 74 6f 20 61 20 73 74 72 69 6e 67 20 66 69 72   to a string fir
1acfa 73 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 61  st..** Strings a
1acfb 72 65 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 65  re simply reinte
1acfc 72 70 72 65 74 65 64 20 61 73 20 62 6c 6f 62 73  rpreted as blobs
1acfd 20 77 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 0a   with no change.
1acfe 2a 2a 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c  ** to the underl
1acff 79 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a  ying data..**.**
1ad00 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73   A NULL value is
1ad01 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20   not changed by 
1ad02 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49  this routine.  I
1ad03 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a  t remains NULL..
1ad04 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f  */.case OP_ToBlo
1ad05 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  b: {            
1ad06 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1ad07 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31   TK_TO_BLOB, in1
1ad08 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   */.  if( pIn1->
1ad09 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1ad0a 20 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20   ) break;.  if( 
1ad0b 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
1ad0c 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20  EM_Blob)==0 ){. 
1ad0d 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79     applyAffinity
1ad0e 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46  (pIn1, SQLITE_AF
1ad0f 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67  F_TEXT, encoding
1ad10 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1ad11 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1ad12 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  _Str || db->mall
1ad13 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
1ad14 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1ad15 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a  In1, MEM_Blob);.
1ad16 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e    }else{.    pIn
1ad17 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45  1->flags &= ~(ME
1ad18 4d 5f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d 5f  M_TypeMask&~MEM_
1ad19 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50 44  Blob);.  }.  UPD
1ad1a 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1ad1b 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b  (pIn1);.  break;
1ad1c 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  .}../* Opcode: T
1ad1d 6f 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a 20  oNumeric P1 * * 
1ad1e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20  * *.**.** Force 
1ad1f 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1ad20 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 6e  ister P1 to be n
1ad21 75 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 61  umeric (either a
1ad22 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20  n.** integer or 
1ad23 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74  a floating-point
1ad24 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20   number.).** If 
1ad25 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78  the value is tex
1ad26 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74  t or blob, try t
1ad27 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20  o convert it to 
1ad28 61 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  an using the.** 
1ad29 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74  equivalent of at
1ad2a 6f 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61  oi() or atof() a
1ad2b 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f  nd store 0 if no
1ad2c 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e   such conversion
1ad2d 20 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65   .** is possible
1ad2e 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76  ..**.** A NULL v
1ad2f 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e  alue is not chan
1ad30 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ged by this rout
1ad31 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73  ine.  It remains
1ad32 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f   NULL..*/.case O
1ad33 50 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20  P_ToNumeric: {  
1ad34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad35 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
1ad36 5f 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f  _NUMERIC, in1 */
1ad37 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
1ad38 61 67 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c  ags & (MEM_Null|
1ad39 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c  MEM_Int|MEM_Real
1ad3a 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ))==0 ){.    sql
1ad3b 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72  ite3VdbeMemNumer
1ad3c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20  ify(pIn1);.  }. 
1ad3d 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1ad3e 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1ad3f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f  CAST */../* Opco
1ad40 64 65 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a  de: ToInt P1 * *
1ad41 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65   * *.**.** Force
1ad42 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1ad43 67 69 73 74 65 72 20 50 31 20 62 65 20 61 6e 20  gister P1 be an 
1ad44 69 6e 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20  integer.  If.** 
1ad45 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 72  The value is cur
1ad46 72 65 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e 75  rently a real nu
1ad47 6d 62 65 72 2c 20 64 72 6f 70 20 69 74 73 20 66  mber, drop its f
1ad48 72 61 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a  ractional part..
1ad49 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
1ad4a 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c  is text or blob,
1ad4b 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20   try to convert 
1ad4c 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  it to an integer
1ad4d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71   using the.** eq
1ad4e 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69  uivalent of atoi
1ad4f 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69  () and store 0 i
1ad50 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72  f no such conver
1ad51 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65  sion is possible
1ad52 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76  ..**.** A NULL v
1ad53 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e  alue is not chan
1ad54 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ged by this rout
1ad55 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73  ine.  It remains
1ad56 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f   NULL..*/.case O
1ad57 50 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20  P_ToInt: {      
1ad58 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1ad59 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54  ame as TK_TO_INT
1ad5a 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28  , in1 */.  if( (
1ad5b 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1ad5c 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Null)==0 ){.  
1ad5d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1ad5e 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29  Integerify(pIn1)
1ad5f 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1ad60 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1ad61 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70  _OMIT_CAST./* Op
1ad62 63 6f 64 65 3a 20 54 6f 52 65 61 6c 20 50 31 20  code: ToReal P1 
1ad63 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f  * * * *.**.** Fo
1ad64 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  rce the value in
1ad65 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1ad66 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  be a floating po
1ad67 69 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 49  int number..** I
1ad68 66 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 63  f The value is c
1ad69 75 72 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 65  urrently an inte
1ad6a 67 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 2e  ger, convert it.
1ad6b 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1ad6c 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62   is text or blob
1ad6d 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74  , try to convert
1ad6e 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65   it to an intege
1ad6f 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65  r using the.** e
1ad70 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f  quivalent of ato
1ad71 69 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 2e  i() and store 0.
1ad72 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e  0 if no such con
1ad73 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69  version is possi
1ad74 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c  ble..**.** A NUL
1ad75 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63  L value is not c
1ad76 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72  hanged by this r
1ad77 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61  outine.  It rema
1ad78 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73  ins NULL..*/.cas
1ad79 65 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20 20  e OP_ToReal: {  
1ad7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad7b 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
1ad7c 5f 52 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20 20  _REAL, in1 */.  
1ad7d 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1ad7e 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20   & MEM_Null)==0 
1ad7f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1ad80 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e  beMemRealify(pIn
1ad81 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  1);.  }.  break;
1ad82 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1ad83 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f  ITE_OMIT_CAST */
1ad84 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20  ../* Opcode: Lt 
1ad85 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1ad86 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65  *.** Compare the
1ad87 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73   values in regis
1ad88 74 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20  ter P1 and P3.  
1ad89 49 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50  If reg(P3)<reg(P
1ad8a 31 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20  1) then.** jump 
1ad8b 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20  to address P2.  
1ad8c 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51  .**.** If the SQ
1ad8d 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20  LITE_JUMPIFNULL 
1ad8e 62 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74  bit of P5 is set
1ad8f 20 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28   and either reg(
1ad90 50 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33  P1) or.** reg(P3
1ad91 29 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74  ) is NULL then t
1ad92 61 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49  ake the jump.  I
1ad93 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d  f the SQLITE_JUM
1ad94 50 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20  PIFNULL .** bit 
1ad95 69 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61  is clear then fa
1ad96 6c 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65  ll thru if eithe
1ad97 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c  r operand is NUL
1ad98 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  L..**.** The SQL
1ad99 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72  ITE_AFF_MASK por
1ad9a 74 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20  tion of P5 must 
1ad9b 62 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63  be an affinity c
1ad9c 68 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51  haracter -.** SQ
1ad9d 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53  LITE_AFF_TEXT, S
1ad9e 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
1ad9f 52 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e  R, and so forth.
1ada0 20 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   An attempt is m
1ada1 61 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63  ade .** to coerc
1ada2 65 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63  e both inputs ac
1ada3 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20  cording to this 
1ada4 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20  affinity before 
1ada5 74 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f  the.** compariso
1ada6 6e 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68  n is made. If th
1ada7 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53  e SQLITE_AFF_MAS
1ada8 4b 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20  K is 0x00, then 
1ada9 6e 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e  numeric.** affin
1adaa 69 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74  ity is used. Not
1adab 65 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e  e that the affin
1adac 69 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  ity conversions 
1adad 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61  are stored.** ba
1adae 63 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75  ck into the inpu
1adaf 74 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61  t registers P1 a
1adb0 6e 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20  nd P3.  So this 
1adb1 6f 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65  opcode can cause
1adb2 0a 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63  .** persistent c
1adb3 68 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74  hanges to regist
1adb4 65 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a  ers P1 and P3..*
1adb5 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f  *.** Once any co
1adb6 6e 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74  nversions have t
1adb7 61 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20  aken place, and 
1adb8 6e 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73  neither value is
1adb9 20 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76   NULL, .** the v
1adba 61 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72  alues are compar
1adbb 65 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75  ed. If both valu
1adbc 65 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65  es are blobs the
1adbd 6e 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a  n memcmp() is.**
1adbe 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
1adbf 6e 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f  ne the results o
1adc0 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  f the comparison
1adc1 2e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65  .  If both value
1adc2 73 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74  s.** are text, t
1adc3 68 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69  hen the appropri
1adc4 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75  ate collating fu
1adc5 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64  nction specified
1adc6 20 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73   in.** P4 is  us
1adc7 65 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d  ed to do the com
1adc8 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20  parison.  If P4 
1adc9 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64  is not specified
1adca 20 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28   then.** memcmp(
1adcb 29 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  ) is used to com
1adcc 70 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67  pare text string
1adcd 2e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65  .  If both value
1adce 73 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63  s are.** numeric
1adcf 2c 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63  , then a numeric
1add0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75   comparison is u
1add1 73 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20  sed. If the two 
1add2 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66  values.** are of
1add3 20 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73   different types
1add4 2c 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61  , then numbers a
1add5 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65  re considered le
1add6 73 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e  ss than.** strin
1add7 67 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61  gs and strings a
1add8 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65  re considered le
1add9 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a  ss than blobs..*
1adda 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49  *.** If the SQLI
1addb 54 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f  TE_STOREP2 bit o
1addc 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65  f P5 is set, the
1addd 6e 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20  n do not jump.  
1adde 49 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72  Instead,.** stor
1addf 65 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75  e a boolean resu
1ade0 6c 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72  lt (either 0, or
1ade1 20 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20   1, or NULL) in 
1ade2 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a  register P2..*/.
1ade3 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31  /* Opcode: Ne P1
1ade4 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
1ade5 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75  ** This works ju
1ade6 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f  st like the Lt o
1ade7 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61  pcode except tha
1ade8 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  t the jump is ta
1ade9 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70  ken if.** the op
1adea 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74  erands in regist
1adeb 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72  ers P1 and P3 ar
1adec 65 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65  e not equal.  Se
1aded 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
1adee 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61  for.** additiona
1adef 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
1adf0 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 71 20  /./* Opcode: Eq 
1adf1 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1adf2 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  *.** This works 
1adf3 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74  just like the Lt
1adf4 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74   opcode except t
1adf5 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  hat the jump is 
1adf6 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20  taken if.** the 
1adf7 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69  operands in regi
1adf8 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 20  sters P1 and P3 
1adf9 61 72 65 20 65 71 75 61 6c 2e 0a 2a 2a 20 53 65  are equal..** Se
1adfa 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
1adfb 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
1adfc 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f  nformation..*/./
1adfd 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20  * Opcode: Le P1 
1adfe 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1adff 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73  * This works jus
1ae00 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70  t like the Lt op
1ae01 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74  code except that
1ae02 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b   the jump is tak
1ae03 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e  en if.** the con
1ae04 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72  tent of register
1ae05 20 50 33 20 69 73 20 6c 65 73 73 20 74 68 61 6e   P3 is less than
1ae06 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
1ae07 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72   content of.** r
1ae08 65 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65  egister P1.  See
1ae09 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66   the Lt opcode f
1ae0a 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
1ae0b 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a  formation..*/./*
1ae0c 20 4f 70 63 6f 64 65 3a 20 47 74 20 50 31 20 50   Opcode: Gt P1 P
1ae0d 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
1ae0e 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74   This works just
1ae0f 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63   like the Lt opc
1ae10 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20  ode except that 
1ae11 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65  the jump is take
1ae12 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  n if.** the cont
1ae13 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20  ent of register 
1ae14 50 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68  P3 is greater th
1ae15 61 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  an the content o
1ae16 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31  f.** register P1
1ae17 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70  .  See the Lt op
1ae18 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f  code for additio
1ae19 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
1ae1a 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47  .*/./* Opcode: G
1ae1b 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  e P1 P2 P3 P4 P5
1ae1c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b  .**.** This work
1ae1d 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20  s just like the 
1ae1e 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74  Lt opcode except
1ae1f 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69   that the jump i
1ae20 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68  s taken if.** th
1ae21 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67  e content of reg
1ae22 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61  ister P3 is grea
1ae23 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
1ae24 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74  l to the content
1ae25 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20   of.** register 
1ae26 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20  P1.  See the Lt 
1ae27 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74  opcode for addit
1ae28 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
1ae29 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 71  n..*/.case OP_Eq
1ae2a 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
1ae2b 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51  /* same as TK_EQ
1ae2c 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33  , jump, in1, in3
1ae2d 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 20   */.case OP_Ne: 
1ae2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ae2f 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c 20   same as TK_NE, 
1ae30 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
1ae31 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20  /.case OP_Lt:   
1ae32 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1ae33 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a 75  ame as TK_LT, ju
1ae34 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
1ae35 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 20  case OP_Le:     
1ae36 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1ae37 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d 70  e as TK_LE, jump
1ae38 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61  , in1, in3 */.ca
1ae39 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 20 20 20  se OP_Gt:       
1ae3a 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1ae3b 61 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c 20  as TK_GT, jump, 
1ae3c 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65  in1, in3 */.case
1ae3d 20 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20 20 20   OP_Ge: {       
1ae3e 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1ae3f 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69 6e   TK_GE, jump, in
1ae40 31 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20  1, in3 */.  int 
1ae41 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 73  flags;.  int res
1ae42 3b 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74  ;.  char affinit
1ae43 79 3b 0a 0a 20 20 66 6c 61 67 73 20 3d 20 70 49  y;..  flags = pI
1ae44 6e 31 2d 3e 66 6c 61 67 73 7c 70 49 6e 33 2d 3e  n1->flags|pIn3->
1ae45 66 6c 61 67 73 3b 0a 0a 20 20 69 66 28 20 66 6c  flags;..  if( fl
1ae46 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  ags&MEM_Null ){.
1ae47 20 20 20 20 2f 2a 20 49 66 20 65 69 74 68 65 72      /* If either
1ae48 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
1ae49 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
1ae4a 20 69 73 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e   is always NULL.
1ae4b 0a 20 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70  .    ** The jump
1ae4c 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65   is taken if the
1ae4d 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
1ae4e 4c 4c 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20  LL bit is set.. 
1ae4f 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f     */.    if( pO
1ae50 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53  p->p5 & SQLITE_S
1ae51 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20  TOREP2 ){.      
1ae52 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
1ae53 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20  pOp->p2];.      
1ae54 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1ae55 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a  Out, MEM_Null);.
1ae56 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54        REGISTER_T
1ae57 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f  RACE(pOp->p2, pO
1ae58 75 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ut);.    }else i
1ae59 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c  f( pOp->p5 & SQL
1ae5a 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29  ITE_JUMPIFNULL )
1ae5b 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
1ae5c 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20  ->p2-1;.    }.  
1ae5d 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20    break;.  }..  
1ae5e 61 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e  affinity = pOp->
1ae5f 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f  p5 & SQLITE_AFF_
1ae60 4d 41 53 4b 3b 0a 20 20 69 66 28 20 61 66 66 69  MASK;.  if( affi
1ae61 6e 69 74 79 20 29 7b 0a 20 20 20 20 61 70 70 6c  nity ){.    appl
1ae62 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20  yAffinity(pIn1, 
1ae63 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69  affinity, encodi
1ae64 6e 67 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66  ng);.    applyAf
1ae65 66 69 6e 69 74 79 28 70 49 6e 33 2c 20 61 66 66  finity(pIn3, aff
1ae66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29  inity, encoding)
1ae67 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ;.    if( db->ma
1ae68 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
1ae69 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20  o no_mem;.  }.. 
1ae6a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1ae6b 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51  type==P4_COLLSEQ
1ae6c 20 7c 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c   || pOp->p4.pCol
1ae6d 6c 3d 3d 30 20 29 3b 0a 20 20 45 78 70 61 6e 64  l==0 );.  Expand
1ae6e 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 45 78  Blob(pIn1);.  Ex
1ae6f 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a  pandBlob(pIn3);.
1ae70 20 20 72 65 73 20 3d 20 73 71 6c 69 74 65 33 4d    res = sqlite3M
1ae71 65 6d 43 6f 6d 70 61 72 65 28 70 49 6e 33 2c 20  emCompare(pIn3, 
1ae72 70 49 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e 70 43  pIn1, pOp->p4.pC
1ae73 6f 6c 6c 29 3b 0a 20 20 73 77 69 74 63 68 28 20  oll);.  switch( 
1ae74 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20  pOp->opcode ){. 
1ae75 20 20 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20     case OP_Eq:  
1ae76 20 20 72 65 73 20 3d 20 72 65 73 3d 3d 30 3b 20    res = res==0; 
1ae77 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
1ae78 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 20 72 65  ase OP_Ne:    re
1ae79 73 20 3d 20 72 65 73 21 3d 30 3b 20 20 20 20 20  s = res!=0;     
1ae7a 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
1ae7b 4f 50 5f 4c 74 3a 20 20 20 20 72 65 73 20 3d 20  OP_Lt:    res = 
1ae7c 72 65 73 3c 30 3b 20 20 20 20 20 20 62 72 65 61  res<0;      brea
1ae7d 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c  k;.    case OP_L
1ae7e 65 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3c  e:    res = res<
1ae7f 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  =0;     break;. 
1ae80 20 20 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20     case OP_Gt:  
1ae81 20 20 72 65 73 20 3d 20 72 65 73 3e 30 3b 20 20    res = res>0;  
1ae82 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
1ae83 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 72 65  efault:       re
1ae84 73 20 3d 20 72 65 73 3e 3d 30 3b 20 20 20 20 20  s = res>=0;     
1ae85 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66  break;.  }..  if
1ae86 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49  ( pOp->p5 & SQLI
1ae87 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20  TE_STOREP2 ){.  
1ae88 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65    pOut = &p->aMe
1ae89 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
1ae8a 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1ae8b 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Out, MEM_Int);. 
1ae8c 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 72     pOut->u.i = r
1ae8d 65 73 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52  es;.    REGISTER
1ae8e 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20  _TRACE(pOp->p2, 
1ae8f 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69  pOut);.  }else i
1ae90 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63  f( res ){.    pc
1ae91 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20   = pOp->p2-1;.  
1ae92 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1ae93 20 4f 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61   Opcode: Permuta
1ae94 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a  tion * * * P4 *.
1ae95 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65  **.** Set the pe
1ae96 72 6d 75 61 74 69 6f 6e 20 75 73 65 64 20 62 79  rmuation used by
1ae97 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20   the OP_Compare 
1ae98 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74  operator to be t
1ae99 68 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69  he array.** of i
1ae9a 6e 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a  ntegers in P4..*
1ae9b 2a 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61  *.** The permuta
1ae9c 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c  tion is only val
1ae9d 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78  id until the nex
1ae9e 74 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e  t OP_Permutation
1ae9f 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a  , OP_Compare,.**
1aea0 20 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f   OP_Halt, or OP_
1aea1 52 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69  ResultRow.  Typi
1aea2 63 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72  cally the OP_Per
1aea3 6d 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20  mutation should 
1aea4 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61  occur.** immedia
1aea5 74 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68  tely prior to th
1aea6 65 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f  e OP_Compare..*/
1aea7 0a 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61  .case OP_Permuta
1aea8 74 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74  tion: {.  assert
1aea9 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1aeaa 34 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20  4_INTARRAY );.  
1aeab 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
1aeac 61 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65  ai );.  aPermute
1aead 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20   = pOp->p4.ai;. 
1aeae 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1aeaf 63 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31  code: Compare P1
1aeb0 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1aeb1 2a 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63  * Compare to vec
1aeb2 74 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65 72  tors of register
1aeb3 73 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65  s in reg(P1)..re
1aeb4 67 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20  g(P1+P3-1) (all 
1aeb5 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29  this.** one "A")
1aeb6 20 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e   and in reg(P2).
1aeb7 2e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22  .reg(P2+P3-1) ("
1aeb8 42 22 29 2e 20 20 53 61 76 65 20 74 68 65 20 72  B").  Save the r
1aeb9 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20  esult of.** the 
1aeba 63 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75  comparison for u
1aebb 73 65 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f  se by the next O
1aebc 50 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e  P_Jump instruct.
1aebd 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b  .**.** P4 is a K
1aebe 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
1aebf 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f   that defines co
1aec0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
1aec1 73 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72  s and sort.** or
1aec2 64 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d  ders for the com
1aec3 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65  parison.  The pe
1aec4 72 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65  rmutation applie
1aec5 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a  s to registers.*
1aec6 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79  * only.  The Key
1aec7 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72  Info elements ar
1aec8 65 20 75 73 65 64 20 73 65 71 75 65 6e 74 69 61  e used sequentia
1aec9 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  lly..**.** The c
1aeca 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73  omparison is a s
1aecb 6f 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20  ort comparison, 
1aecc 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65  so NULLs compare
1aecd 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73   equal,.** NULLs
1aece 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e   are less than n
1aecf 75 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20  umbers, numbers 
1aed0 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74  are less than st
1aed1 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74  rings,.** and st
1aed2 72 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74  rings are less t
1aed3 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61  han blobs..*/.ca
1aed4 73 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b  se OP_Compare: {
1aed5 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e  .  int n = pOp->
1aed6 70 33 3b 0a 20 20 69 6e 74 20 69 2c 20 70 31 2c  p3;.  int i, p1,
1aed7 20 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79   p2;.  const Key
1aed8 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d  Info *pKeyInfo =
1aed9 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66   pOp->p4.pKeyInf
1aeda 6f 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30  o;.  assert( n>0
1aedb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b   );.  assert( pK
1aedc 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 70  eyInfo!=0 );.  p
1aedd 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61  1 = pOp->p1;.  a
1aede 73 73 65 72 74 28 20 70 31 3e 30 20 26 26 20 70  ssert( p1>0 && p
1aedf 31 2b 6e 2d 31 3c 70 2d 3e 6e 4d 65 6d 20 29 3b  1+n-1<p->nMem );
1aee0 0a 20 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b  .  p2 = pOp->p2;
1aee1 0a 20 20 61 73 73 65 72 74 28 20 70 32 3e 30 20  .  assert( p2>0 
1aee2 26 26 20 70 32 2b 6e 2d 31 3c 70 2d 3e 6e 4d 65  && p2+n-1<p->nMe
1aee3 6d 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  m );.  for(i=0; 
1aee4 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  i<n; i++){.    i
1aee5 6e 74 20 69 64 78 20 3d 20 61 50 65 72 6d 75 74  nt idx = aPermut
1aee6 65 20 3f 20 61 50 65 72 6d 75 74 65 5b 69 5d 20  e ? aPermute[i] 
1aee7 3a 20 69 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71  : i;.    CollSeq
1aee8 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43   *pColl;    /* C
1aee9 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1aeea 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73  e to use on this
1aeeb 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74   term */.    int
1aeec 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20   bRev;          
1aeed 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43  /* True for DESC
1aeee 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65  ENDING sort orde
1aeef 72 20 2a 2f 0a 20 20 20 20 52 45 47 49 53 54 45  r */.    REGISTE
1aef0 52 5f 54 52 41 43 45 28 70 31 2b 69 64 78 2c 20  R_TRACE(p1+idx, 
1aef1 26 70 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d  &p->aMem[p1+idx]
1aef2 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f  );.    REGISTER_
1aef3 54 52 41 43 45 28 70 32 2b 69 64 78 2c 20 26 70  TRACE(p2+idx, &p
1aef4 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78 5d 29 3b  ->aMem[p2+idx]);
1aef5 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 70  .    assert( i<p
1aef6 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20  KeyInfo->nField 
1aef7 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70  );.    pColl = p
1aef8 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69  KeyInfo->aColl[i
1aef9 5d 3b 0a 20 20 20 20 62 52 65 76 20 3d 20 70 4b  ];.    bRev = pK
1aefa 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64  eyInfo->aSortOrd
1aefb 65 72 5b 69 5d 3b 0a 20 20 20 20 69 43 6f 6d 70  er[i];.    iComp
1aefc 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d  are = sqlite3Mem
1aefd 43 6f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65 6d  Compare(&p->aMem
1aefe 5b 70 31 2b 69 64 78 5d 2c 20 26 70 2d 3e 61 4d  [p1+idx], &p->aM
1aeff 65 6d 5b 70 32 2b 69 64 78 5d 2c 20 70 43 6f 6c  em[p2+idx], pCol
1af00 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6d  l);.    if( iCom
1af01 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20 69 66  pare ){.      if
1af02 28 20 62 52 65 76 20 29 20 69 43 6f 6d 70 61 72  ( bRev ) iCompar
1af03 65 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a 20  e = -iCompare;. 
1af04 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1af05 7d 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75 74 65  }.  }.  aPermute
1af06 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
1af07 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75 6d  ../* Opcode: Jum
1af08 70 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  p P1 P2 P3 * *.*
1af09 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65  *.** Jump to the
1af0a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 20   instruction at 
1af0b 61 64 64 72 65 73 73 20 50 31 2c 20 50 32 2c 20  address P1, P2, 
1af0c 6f 72 20 50 33 20 64 65 70 65 6e 64 69 6e 67 20  or P3 depending 
1af0d 6f 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20 69 6e  on whether.** in
1af0e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
1af0f 20 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73 74   OP_Compare inst
1af10 72 75 63 74 69 6f 6e 20 74 68 65 20 50 31 20 76  ruction the P1 v
1af11 65 63 74 6f 72 20 77 61 73 20 6c 65 73 73 20 74  ector was less t
1af12 68 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c  han.** equal to,
1af13 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e   or greater than
1af14 20 74 68 65 20 50 32 20 76 65 63 74 6f 72 2c 20   the P2 vector, 
1af15 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f  respectively..*/
1af16 0a 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20 7b  .case OP_Jump: {
1af17 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1af18 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69 43  jump */.  if( iC
1af19 6f 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20 20 20  ompare<0 ){.    
1af1a 70 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31  pc = pOp->p1 - 1
1af1b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 43  ;.  }else if( iC
1af1c 6f 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20  ompare==0 ){.   
1af1d 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1af1e 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
1af1f 70 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 31  pc = pOp->p3 - 1
1af20 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1af21 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e 64  ../* Opcode: And
1af22 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1af23 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67  .** Take the log
1af24 69 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 20  ical AND of the 
1af25 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74  values in regist
1af26 65 72 73 20 50 31 20 61 6e 64 20 50 32 20 61 6e  ers P1 and P2 an
1af27 64 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20 72  d.** write the r
1af28 65 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73  esult into regis
1af29 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66  ter P3..**.** If
1af2a 20 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32   either P1 or P2
1af2b 20 69 73 20 30 20 28 66 61 6c 73 65 29 20 74 68   is 0 (false) th
1af2c 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
1af2d 20 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74 68   0 even if.** th
1af2e 65 20 6f 74 68 65 72 20 69 6e 70 75 74 20 69 73  e other input is
1af2f 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 61   NULL.  A NULL a
1af30 6e 64 20 74 72 75 65 20 6f 72 20 74 77 6f 20 4e  nd true or two N
1af31 55 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20 4e  ULLs give.** a N
1af32 55 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 2f  ULL output..*/./
1af33 2a 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31 20  * Opcode: Or P1 
1af34 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1af35 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c  Take the logical
1af36 20 4f 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65   OR of the value
1af37 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  s in register P1
1af38 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73   and P2 and.** s
1af39 74 6f 72 65 20 74 68 65 20 61 6e 73 77 65 72 20  tore the answer 
1af3a 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a  in register P3..
1af3b 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20  **.** If either 
1af3c 50 31 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e 7a  P1 or P2 is nonz
1af3d 65 72 6f 20 28 74 72 75 65 29 20 74 68 65 6e 20  ero (true) then 
1af3e 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 31 20  the result is 1 
1af3f 28 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20 69  (true).** even i
1af40 66 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75  f the other inpu
1af41 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55  t is NULL.  A NU
1af42 4c 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f 72 20  LL and false or 
1af43 74 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69 76  two NULLs.** giv
1af44 65 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e  e a NULL output.
1af45 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 3a  .*/.case OP_And:
1af46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1af47 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 2c   same as TK_AND,
1af48 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
1af49 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 7b  */.case OP_Or: {
1af4a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1af4b 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 69  same as TK_OR, i
1af4c 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1af4d 0a 20 20 69 6e 74 20 76 31 2c 20 76 32 3b 20 20  .  int v1, v2;  
1af4e 20 20 2f 2a 20 30 3d 3d 46 41 4c 53 45 2c 20 31    /* 0==FALSE, 1
1af4f 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f  ==TRUE, 2==UNKNO
1af50 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 0a 20  WN or NULL */.. 
1af51 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73   if( pIn1->flags
1af52 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1af53 20 20 20 76 31 20 3d 20 32 3b 0a 20 20 7d 65 6c     v1 = 2;.  }el
1af54 73 65 7b 0a 20 20 20 20 76 31 20 3d 20 73 71 6c  se{.    v1 = sql
1af55 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
1af56 28 70 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a 20  (pIn1)!=0;.  }. 
1af57 20 69 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73   if( pIn2->flags
1af58 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1af59 20 20 20 76 32 20 3d 20 32 3b 0a 20 20 7d 65 6c     v2 = 2;.  }el
1af5a 73 65 7b 0a 20 20 20 20 76 32 20 3d 20 73 71 6c  se{.    v2 = sql
1af5b 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
1af5c 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a 20  (pIn2)!=0;.  }. 
1af5d 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1af5e 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 20  ==OP_And ){.    
1af5f 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
1af60 69 67 6e 65 64 20 63 68 61 72 20 61 6e 64 5f 6c  igned char and_l
1af61 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c  ogic[] = { 0, 0,
1af62 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20   0, 0, 1, 2, 0, 
1af63 32 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 3d  2, 2 };.    v1 =
1af64 20 61 6e 64 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b   and_logic[v1*3+
1af65 76 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  v2];.  }else{.  
1af66 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
1af67 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f  nsigned char or_
1af68 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31  logic[] = { 0, 1
1af69 2c 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c  , 2, 1, 1, 1, 2,
1af6a 20 31 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 20   1, 2 };.    v1 
1af6b 3d 20 6f 72 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b  = or_logic[v1*3+
1af6c 76 32 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76  v2];.  }.  if( v
1af6d 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d 65 6d 53  1==2 ){.    MemS
1af6e 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1af6f 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 65   MEM_Null);.  }e
1af70 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75  lse{.    pOut->u
1af71 2e 69 20 3d 20 76 31 3b 0a 20 20 20 20 4d 65 6d  .i = v1;.    Mem
1af72 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1af73 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a  , MEM_Int);.  }.
1af74 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1af75 70 63 6f 64 65 3a 20 4e 6f 74 20 50 31 20 50 32  pcode: Not P1 P2
1af76 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74   * * *.**.** Int
1af77 65 72 70 72 65 74 20 74 68 65 20 76 61 6c 75 65  erpret the value
1af78 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1af79 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c  as a boolean val
1af7a 75 65 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a  ue.  Store the.*
1af7b 2a 20 62 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c 65  * boolean comple
1af7c 6d 65 6e 74 20 69 6e 20 72 65 67 69 73 74 65 72  ment in register
1af7d 20 50 32 2e 20 20 49 66 20 74 68 65 20 76 61 6c   P2.  If the val
1af7e 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1af7f 31 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74  1 is .** NULL, t
1af80 68 65 6e 20 61 20 4e 55 4c 4c 20 69 73 20 73 74  hen a NULL is st
1af81 6f 72 65 64 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63  ored in P2..*/.c
1af82 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 20  ase OP_Not: {   
1af83 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1af84 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 2c 20  same as TK_NOT, 
1af85 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d 20  in1 */.  pOut = 
1af86 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1af87 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  ];.  if( pIn1->f
1af88 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1af89 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1af8a 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75  beMemSetNull(pOu
1af8b 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
1af8c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1af8d 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 21 73  etInt64(pOut, !s
1af8e 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1af8f 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20  ue(pIn1));.  }. 
1af90 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1af91 63 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31 20  code: BitNot P1 
1af92 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49  P2 * * *.**.** I
1af93 6e 74 65 72 70 72 65 74 20 74 68 65 20 63 6f 6e  nterpret the con
1af94 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72  tent of register
1af95 20 50 31 20 61 73 20 61 6e 20 69 6e 74 65 67 65   P1 as an intege
1af96 72 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a 2a  r.  Store the.**
1af97 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74   ones-complement
1af98 20 6f 66 20 74 68 65 20 50 31 20 76 61 6c 75 65   of the P1 value
1af99 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1af9a 32 2e 20 20 49 66 20 50 31 20 68 6f 6c 64 73 0a  2.  If P1 holds.
1af9b 2a 2a 20 61 20 4e 55 4c 4c 20 74 68 65 6e 20 73  ** a NULL then s
1af9c 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 50  tore a NULL in P
1af9d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69  2..*/.case OP_Bi
1af9e 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20  tNot: {         
1af9f 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1afa0 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a 2f  K_BITNOT, in1 */
1afa1 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d  .  pOut = &p->aM
1afa2 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69  em[pOp->p2];.  i
1afa3 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
1afa4 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1afa5 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1afa6 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20  etNull(pOut);.  
1afa7 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
1afa8 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
1afa9 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65 33  4(pOut, ~sqlite3
1afaa 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e  VdbeIntValue(pIn
1afab 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  1));.  }.  break
1afac 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1afad 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  If P1 P2 P3 * *.
1afae 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32  **.** Jump to P2
1afaf 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   if the value in
1afb0 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1afb1 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65  true.  The value
1afb2 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64   is.** is consid
1afb3 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20  ered true if it 
1afb4 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e  is numeric and n
1afb5 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  on-zero.  If the
1afb6 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20   value.** in P1 
1afb7 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b  is NULL then tak
1afb8 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33  e the jump if P3
1afb9 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20   is true..*/./* 
1afba 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31  Opcode: IfNot P1
1afbb 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1afbc 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74   Jump to P2 if t
1afbd 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1afbe 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65  ster P1 is False
1afbf 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a  .  The value is.
1afc0 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
1afc1 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20   true if it has 
1afc2 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20  a numeric value 
1afc3 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  of zero.  If the
1afc4 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20   value.** in P1 
1afc5 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b  is NULL then tak
1afc6 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33  e the jump if P3
1afc7 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73   is true..*/.cas
1afc8 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20  e OP_If:        
1afc9 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
1afca 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50  , in1 */.case OP
1afcb 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20  _IfNot: {       
1afcc 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1afcd 31 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 20 20  1 */.  int c;.  
1afce 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  if( pIn1->flags 
1afcf 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
1afd0 20 20 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20    c = pOp->p3;. 
1afd1 20 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53   }else{.#ifdef S
1afd2 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
1afd3 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 63 20  ING_POINT.    c 
1afd4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
1afd5 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 23 65 6c  Value(pIn1);.#el
1afd6 73 65 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74  se.    c = sqlit
1afd7 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
1afd8 70 49 6e 31 29 21 3d 30 2e 30 3b 0a 23 65 6e 64  pIn1)!=0.0;.#end
1afd9 69 66 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  if.    if( pOp->
1afda 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74  opcode==OP_IfNot
1afdb 20 29 20 63 20 3d 20 21 63 3b 0a 20 20 7d 0a 20   ) c = !c;.  }. 
1afdc 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 70 63   if( c ){.    pc
1afdd 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20   = pOp->p2-1;.  
1afde 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1afdf 20 4f 70 63 6f 64 65 3a 20 49 73 4e 75 6c 6c 20   Opcode: IsNull 
1afe0 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1afe1 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66  ** Jump to P2 if
1afe2 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1afe3 67 69 73 74 65 72 20 50 31 20 69 73 20 4e 55 4c  gister P1 is NUL
1afe4 4c 2e 20 20 49 66 20 50 33 20 69 73 20 67 72 65  L.  If P3 is gre
1afe5 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 7a 65 72  ater.** than zer
1afe6 6f 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 61 6c  o, then check al
1afe7 6c 20 76 61 6c 75 65 73 20 72 65 67 28 50 31 29  l values reg(P1)
1afe8 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 0a 2a 2a  , reg(P1+1), .**
1afe9 20 72 65 67 28 50 31 2b 32 29 2c 20 2e 2e 2e 2c   reg(P1+2), ...,
1afea 20 72 65 67 28 50 31 2b 50 33 2d 31 29 2e 0a 2a   reg(P1+P3-1)..*
1afeb 2f 0a 63 61 73 65 20 4f 50 5f 49 73 4e 75 6c 6c  /.case OP_IsNull
1afec 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
1afed 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e  * same as TK_ISN
1afee 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a  ULL, jump, in1 *
1afef 2f 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d  /.  int n = pOp-
1aff0 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 20 70  >p3;.  assert( p
1aff1 4f 70 2d 3e 70 33 3d 3d 30 20 7c 7c 20 70 4f 70  Op->p3==0 || pOp
1aff2 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 64 6f 7b 0a  ->p1>0 );.  do{.
1aff3 20 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66      if( (pIn1->f
1aff4 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1aff5 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20  !=0 ){.      pc 
1aff6 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1aff7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1aff8 7d 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20  }.    pIn1++;.  
1aff9 7d 77 68 69 6c 65 28 20 2d 2d 6e 20 3e 20 30 20  }while( --n > 0 
1affa 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1affb 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c  * Opcode: NotNul
1affc 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  l P1 P2 * * *.**
1affd 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69  .** Jump to P2 i
1affe 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  f the value in r
1afff 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6e 6f  egister P1 is no
1b000 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73  t NULL.  .*/.cas
1b001 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20  e OP_NotNull: { 
1b002 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1b003 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c  me as TK_NOTNULL
1b004 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20  , jump, in1 */. 
1b005 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
1b006 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
1b007 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
1b008 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20  ->p2 - 1;.  }.  
1b009 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1b00a 6f 64 65 3a 20 53 65 74 4e 75 6d 43 6f 6c 75 6d  ode: SetNumColum
1b00b 6e 73 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  ns * P2 * * *.**
1b00c 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1b00d 73 65 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20  sets the number 
1b00e 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74  of columns for t
1b00f 68 65 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64  he cursor opened
1b010 20 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f   by the.** follo
1b011 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e  wing instruction
1b012 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 6e   to P2..**.** An
1b013 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e   OP_SetNumColumn
1b014 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 66 75 6c  s is only useful
1b015 20 69 66 20 69 74 20 6f 63 63 75 72 73 20 69 6d   if it occurs im
1b016 6d 65 64 69 61 74 65 6c 79 20 62 65 66 6f 72 65  mediately before
1b017 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20   .** one of the 
1b018 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 65  following opcode
1b019 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4f 70 65  s:.**.**     Ope
1b01a 6e 52 65 61 64 0a 2a 2a 20 20 20 20 20 4f 70 65  nRead.**     Ope
1b01b 6e 57 72 69 74 65 0a 2a 2a 20 20 20 20 20 4f 70  nWrite.**     Op
1b01c 65 6e 50 73 65 75 64 6f 0a 2a 2a 0a 2a 2a 20 49  enPseudo.**.** I
1b01d 66 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20  f the OP_Column 
1b01e 6f 70 63 6f 64 65 20 69 73 20 74 6f 20 62 65 20  opcode is to be 
1b01f 65 78 65 63 75 74 65 64 20 6f 6e 20 61 20 63 75  executed on a cu
1b020 72 73 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  rsor, then.** th
1b021 69 73 20 6f 70 63 6f 64 65 20 6d 75 73 74 20 62  is opcode must b
1b022 65 20 70 72 65 73 65 6e 74 20 69 6d 6d 65 64 69  e present immedi
1b023 61 74 65 6c 79 20 62 65 66 6f 72 65 20 74 68 65  ately before the
1b024 20 6f 70 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20   opcode that.** 
1b025 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73 6f 72  opens the cursor
1b026 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74  ..*/.case OP_Set
1b027 4e 75 6d 43 6f 6c 75 6d 6e 73 3a 20 7b 0a 20 20  NumColumns: {.  
1b028 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1b029 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20 50 31 20 50  ode: Column P1 P
1b02a 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1b02b 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 64 61  Interpret the da
1b02c 74 61 20 74 68 61 74 20 63 75 72 73 6f 72 20 50  ta that cursor P
1b02d 31 20 70 6f 69 6e 74 73 20 74 6f 20 61 73 20 61  1 points to as a
1b02e 20 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74   structure built
1b02f 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 4d 61   using.** the Ma
1b030 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63  keRecord instruc
1b031 74 69 6f 6e 2e 20 20 28 53 65 65 20 74 68 65 20  tion.  (See the 
1b032 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64  MakeRecord opcod
1b033 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  e for additional
1b034 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
1b035 61 62 6f 75 74 20 74 68 65 20 66 6f 72 6d 61 74  about the format
1b036 20 6f 66 20 74 68 65 20 64 61 74 61 2e 29 20 20   of the data.)  
1b037 45 78 74 72 61 63 74 20 74 68 65 20 50 32 2d 74  Extract the P2-t
1b038 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f 6d  h column.** from
1b039 20 74 68 69 73 20 72 65 63 6f 72 64 2e 20 20 49   this record.  I
1b03a 66 20 74 68 65 72 65 20 61 72 65 20 6c 65 73 73  f there are less
1b03b 20 74 68 61 74 20 28 50 32 2b 31 29 20 0a 2a 2a   that (P2+1) .**
1b03c 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 72   values in the r
1b03d 65 63 6f 72 64 2c 20 65 78 74 72 61 63 74 20 61  ecord, extract a
1b03e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   NULL..**.** The
1b03f 20 76 61 6c 75 65 20 65 78 74 72 61 63 74 65 64   value extracted
1b040 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65   is stored in re
1b041 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a  gister P3..**.**
1b042 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63   If the column c
1b043 6f 6e 74 61 69 6e 73 20 66 65 77 65 72 20 74 68  ontains fewer th
1b044 61 6e 20 50 32 20 66 69 65 6c 64 73 2c 20 74 68  an P2 fields, th
1b045 65 6e 20 65 78 74 72 61 63 74 20 61 20 4e 55 4c  en extract a NUL
1b046 4c 2e 20 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68  L.  Or,.** if th
1b047 65 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73  e P4 argument is
1b048 20 61 20 50 34 5f 4d 45 4d 20 75 73 65 20 74 68   a P4_MEM use th
1b049 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
1b04a 34 20 61 72 67 75 6d 65 6e 74 20 61 73 0a 2a 2a  4 argument as.**
1b04b 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a   the result..*/.
1b04c 63 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a 20  case OP_Column: 
1b04d 7b 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53  {.  int payloadS
1b04e 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ize;   /* Number
1b04f 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
1b050 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
1b051 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20   p1 = pOp->p1;  
1b052 2f 2a 20 50 31 20 76 61 6c 75 65 20 6f 66 20 74  /* P1 value of t
1b053 68 65 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69  he opcode */.  i
1b054 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b  nt p2 = pOp->p2;
1b055 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62    /* column numb
1b056 65 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a  er to retrieve *
1b057 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1b058 70 43 20 3d 20 30 3b 2f 2a 20 54 68 65 20 56 44  pC = 0;/* The VD
1b059 42 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63  BE cursor */.  c
1b05a 68 61 72 20 2a 7a 52 65 63 3b 20 20 20 20 20 20  har *zRec;      
1b05b 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
1b05c 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 2d  complete record-
1b05d 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 72 73  data */.  BtCurs
1b05e 6f 72 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a 20  or *pCrsr;   /* 
1b05f 54 68 65 20 42 54 72 65 65 20 63 75 72 73 6f 72  The BTree cursor
1b060 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65   */.  u32 *aType
1b061 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70  ;        /* aTyp
1b062 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e  e[i] holds the n
1b063 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20 74  umeric type of t
1b064 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a  he i-th column *
1b065 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74  /.  u32 *aOffset
1b066 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73 65  ;      /* aOffse
1b067 74 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20 74  t[i] is offset t
1b068 6f 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 20  o start of data 
1b069 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20  for i-th column 
1b06a 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b  */.  int nField;
1b06b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65          /* numbe
1b06c 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
1b06d 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
1b06e 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20  nt len;         
1b06f 20 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20    /* The length 
1b070 6f 66 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65  of the serialize
1b071 64 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 63  d data for the c
1b072 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  olumn */.  int i
1b073 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1b074 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
1b075 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20  .  char *zData; 
1b076 20 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f 66        /* Part of
1b077 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e   the record bein
1b078 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 4d  g decoded */.  M
1b079 65 6d 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20  em *pDest;      
1b07a 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72    /* Where to wr
1b07b 69 74 65 20 74 68 65 20 65 78 74 72 61 63 74 65  ite the extracte
1b07c 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20 4d 65 6d  d value */.  Mem
1b07d 20 73 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20   sMem;          
1b07e 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74  /* For storing t
1b07f 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20  he record being 
1b080 64 65 63 6f 64 65 64 20 2a 2f 0a 0a 20 20 6d 65  decoded */..  me
1b081 6d 73 65 74 28 26 73 4d 65 6d 2c 20 30 2c 20 73  mset(&sMem, 0, s
1b082 69 7a 65 6f 66 28 73 4d 65 6d 29 29 3b 0a 20 20  izeof(sMem));.  
1b083 61 73 73 65 72 74 28 20 70 31 3c 70 2d 3e 6e 43  assert( p1<p->nC
1b084 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72  ursor );.  asser
1b085 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
1b086 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
1b087 20 29 3b 0a 20 20 70 44 65 73 74 20 3d 20 26 70   );.  pDest = &p
1b088 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1b089 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1b08a 67 28 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c  g(pDest, MEM_Nul
1b08b 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62  l);..  /* This b
1b08c 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76 61  lock sets the va
1b08d 72 69 61 62 6c 65 20 70 61 79 6c 6f 61 64 53 69  riable payloadSi
1b08e 7a 65 20 74 6f 20 62 65 20 74 68 65 20 74 6f 74  ze to be the tot
1b08f 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a  al number of.  *
1b090 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72  * bytes in the r
1b091 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ecord..  **.  **
1b092 20 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f 20   zRec is set to 
1b093 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  be the complete 
1b094 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 63 6f  text of the reco
1b095 72 64 20 69 66 20 69 74 20 69 73 20 61 76 61 69  rd if it is avai
1b096 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68 65 20  lable..  ** The 
1b097 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 20  complete record 
1b098 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 20 61  text is always a
1b099 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 73 65  vailable for pse
1b09a 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a 2a 20  udo-tables.  ** 
1b09b 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73  If the record is
1b09c 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63 75 72   stored in a cur
1b09d 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c 65 74  sor, the complet
1b09e 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a 20 20  e record text.  
1b09f 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76 61 69  ** might be avai
1b0a0 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20 70 43  lable in the  pC
1b0a1 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 4f  ->aRow cache.  O
1b0a2 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62  r it might not b
1b0a3 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64  e..  ** If the d
1b0a4 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c 61 62  ata is unavailab
1b0a5 6c 65 2c 20 20 7a 52 65 63 20 69 73 20 73 65 74  le,  zRec is set
1b0a6 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2a 0a 20   to NULL..  **. 
1b0a7 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 6f 6d 70   ** We also comp
1b0a8 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ute the number o
1b0a9 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
1b0aa 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 20 63 75   record.  For cu
1b0ab 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74 68 65 20  rsors,.  ** the 
1b0ac 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
1b0ad 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  s is stored in t
1b0ae 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 6e 46  he VdbeCursor.nF
1b0af 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e 0a 20 20  ield element..  
1b0b0 2a 2f 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43  */.  pC = p->apC
1b0b1 73 72 5b 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  sr[p1];.  assert
1b0b2 28 20 70 43 21 3d 30 20 29 3b 0a 23 69 66 6e 64  ( pC!=0 );.#ifnd
1b0b3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1b0b4 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 61 73  IRTUALTABLE.  as
1b0b5 73 65 72 74 28 20 70 43 2d 3e 70 56 74 61 62 43  sert( pC->pVtabC
1b0b6 75 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e 64  ursor==0 );.#end
1b0b7 69 66 0a 20 20 69 66 28 20 70 43 2d 3e 70 43 75  if.  if( pC->pCu
1b0b8 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f  rsor!=0 ){.    /
1b0b9 2a 20 54 68 65 20 72 65 63 6f 72 64 20 69 73 20  * The record is 
1b0ba 73 74 6f 72 65 64 20 69 6e 20 61 20 42 2d 54 72  stored in a B-Tr
1b0bb 65 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  ee */.    rc = s
1b0bc 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
1b0bd 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 20 20  Moveto(pC);.    
1b0be 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62  if( rc ) goto ab
1b0bf 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1b0c0 3b 0a 20 20 20 20 7a 52 65 63 20 3d 20 30 3b 0a  ;.    zRec = 0;.
1b0c1 20 20 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e      pCrsr = pC->
1b0c2 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 28  pCursor;.    if(
1b0c3 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a   pC->nullRow ){.
1b0c4 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a        payloadSiz
1b0c5 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  e = 0;.    }else
1b0c6 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65 53 74   if( pC->cacheSt
1b0c7 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74  atus==p->cacheCt
1b0c8 72 20 29 7b 0a 20 20 20 20 20 20 70 61 79 6c 6f  r ){.      paylo
1b0c9 61 64 53 69 7a 65 20 3d 20 70 43 2d 3e 70 61 79  adSize = pC->pay
1b0ca 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 20  loadSize;.      
1b0cb 7a 52 65 63 20 3d 20 28 63 68 61 72 2a 29 70 43  zRec = (char*)pC
1b0cc 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 6c 73  ->aRow;.    }els
1b0cd 65 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65  e if( pC->isInde
1b0ce 78 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 70  x ){.      i64 p
1b0cf 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20  ayloadSize64;.  
1b0d0 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1b0d1 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c 20 26  KeySize(pCrsr, &
1b0d2 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29 3b 0a  payloadSize64);.
1b0d3 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a        payloadSiz
1b0d4 65 20 3d 20 28 69 6e 74 29 70 61 79 6c 6f 61 64  e = (int)payload
1b0d5 53 69 7a 65 36 34 3b 0a 20 20 20 20 7d 65 6c 73  Size64;.    }els
1b0d6 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
1b0d7 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 70 43  BtreeDataSize(pC
1b0d8 72 73 72 2c 20 28 75 33 32 20 2a 29 26 70 61 79  rsr, (u32 *)&pay
1b0d9 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20 20 20 7d  loadSize);.    }
1b0da 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43  .    nField = pC
1b0db 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 7d 65 6c 73  ->nField;.  }els
1b0dc 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
1b0dd 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29  C->pseudoTable )
1b0de 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63  ;.    /* The rec
1b0df 6f 72 64 20 69 73 20 74 68 65 20 73 6f 6c 65 20  ord is the sole 
1b0e0 65 6e 74 72 79 20 6f 66 20 61 20 70 73 65 75 64  entry of a pseud
1b0e1 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 70  o-table */.    p
1b0e2 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 43 2d  ayloadSize = pC-
1b0e3 3e 6e 44 61 74 61 3b 0a 20 20 20 20 7a 52 65 63  >nData;.    zRec
1b0e4 20 3d 20 70 43 2d 3e 70 44 61 74 61 3b 0a 20 20   = pC->pData;.  
1b0e5 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75    pC->cacheStatu
1b0e6 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1b0e7 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 79  .    assert( pay
1b0e8 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 7c 7c 20 7a  loadSize==0 || z
1b0e9 52 65 63 21 3d 30 20 29 3b 0a 20 20 20 20 6e 46  Rec!=0 );.    nF
1b0ea 69 65 6c 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c  ield = pC->nFiel
1b0eb 64 3b 0a 20 20 20 20 70 43 72 73 72 20 3d 20 30  d;.    pCrsr = 0
1b0ec 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 70  ;.  }..  /* If p
1b0ed 61 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c  ayloadSize is 0,
1b0ee 20 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65   then just store
1b0ef 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28   a NULL */.  if(
1b0f0 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20   payloadSize==0 
1b0f1 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
1b0f2 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  Dest->flags&MEM_
1b0f3 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 67 6f 74 6f  Null );.    goto
1b0f4 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a   op_column_out;.
1b0f5 20 20 7d 0a 20 20 69 66 28 20 70 61 79 6c 6f 61    }.  if( payloa
1b0f6 64 53 69 7a 65 3e 64 62 2d 3e 61 4c 69 6d 69 74  dSize>db->aLimit
1b0f7 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
1b0f8 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74  NGTH] ){.    got
1b0f9 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a  o too_big;.  }..
1b0fa 20 20 61 73 73 65 72 74 28 20 70 32 3c 6e 46 69    assert( p2<nFi
1b0fb 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  eld );..  /* Rea
1b0fc 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20  d and parse the 
1b0fd 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53  table header.  S
1b0fe 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
1b0ff 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20   of the parse.  
1b100 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f  ** into the reco
1b101 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20  rd header cache 
1b102 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75  fields of the cu
1b103 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 61 54 79  rsor..  */.  aTy
1b104 70 65 20 3d 20 70 43 2d 3e 61 54 79 70 65 3b 0a  pe = pC->aType;.
1b105 20 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65 53    if( pC->cacheS
1b106 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43  tatus==p->cacheC
1b107 74 72 20 29 7b 0a 20 20 20 20 61 4f 66 66 73 65  tr ){.    aOffse
1b108 74 20 3d 20 70 43 2d 3e 61 4f 66 66 73 65 74 3b  t = pC->aOffset;
1b109 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 38  .  }else{.    u8
1b10a 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20 2f   *zIdx;        /
1b10b 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 61  * Index into hea
1b10c 64 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 7a  der */.    u8 *z
1b10d 45 6e 64 48 64 72 3b 20 20 20 20 20 2f 2a 20 50  EndHdr;     /* P
1b10e 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20  ointer to first 
1b10f 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 68  byte after the h
1b110 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  eader */.    int
1b111 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a   offset;      /*
1b112 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65   Offset into the
1b113 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69 6e 74   data */.    int
1b114 20 73 7a 48 64 72 53 7a 3b 20 20 20 20 20 2f 2a   szHdrSz;     /*
1b115 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61   Size of the hea
1b116 64 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 61  der size field a
1b117 74 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f 72  t start of recor
1b118 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 76 61  d */.    int ava
1b119 69 6c 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 6d  il = 0;   /* Num
1b11a 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
1b11b 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 2a  available data *
1b11c 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 61 54  /..    assert(aT
1b11d 79 70 65 29 3b 0a 20 20 20 20 70 43 2d 3e 61 4f  ype);.    pC->aO
1b11e 66 66 73 65 74 20 3d 20 61 4f 66 66 73 65 74 20  ffset = aOffset 
1b11f 3d 20 26 61 54 79 70 65 5b 6e 46 69 65 6c 64 5d  = &aType[nField]
1b120 3b 0a 20 20 20 20 70 43 2d 3e 70 61 79 6c 6f 61  ;.    pC->payloa
1b121 64 53 69 7a 65 20 3d 20 70 61 79 6c 6f 61 64 53  dSize = payloadS
1b122 69 7a 65 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63  ize;.    pC->cac
1b123 68 65 53 74 61 74 75 73 20 3d 20 70 2d 3e 63 61  heStatus = p->ca
1b124 63 68 65 43 74 72 3b 0a 0a 20 20 20 20 2f 2a 20  cheCtr;..    /* 
1b125 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d  Figure out how m
1b126 61 6e 79 20 62 79 74 65 73 20 61 72 65 20 69 6e  any bytes are in
1b127 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20   the header */. 
1b128 20 20 20 69 66 28 20 7a 52 65 63 20 29 7b 0a 20     if( zRec ){. 
1b129 20 20 20 20 20 7a 44 61 74 61 20 3d 20 7a 52 65       zData = zRe
1b12a 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  c;.    }else{.  
1b12b 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73 49 6e      if( pC->isIn
1b12c 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  dex ){.        z
1b12d 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71  Data = (char*)sq
1b12e 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74  lite3BtreeKeyFet
1b12f 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c  ch(pCrsr, &avail
1b130 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1b131 20 20 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20          zData = 
1b132 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 42 74  (char*)sqlite3Bt
1b133 72 65 65 44 61 74 61 46 65 74 63 68 28 70 43 72  reeDataFetch(pCr
1b134 73 72 2c 20 26 61 76 61 69 6c 29 3b 0a 20 20 20  sr, &avail);.   
1b135 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66     }.      /* If
1b136 20 4b 65 79 46 65 74 63 68 28 29 2f 44 61 74 61   KeyFetch()/Data
1b137 46 65 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20  Fetch() managed 
1b138 74 6f 20 67 65 74 20 74 68 65 20 65 6e 74 69 72  to get the entir
1b139 65 20 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20  e payload,.     
1b13a 20 2a 2a 20 73 61 76 65 20 74 68 65 20 70 61 79   ** save the pay
1b13b 6c 6f 61 64 20 69 6e 20 74 68 65 20 70 43 2d 3e  load in the pC->
1b13c 61 52 6f 77 20 63 61 63 68 65 2e 20 20 54 68 61  aRow cache.  Tha
1b13d 74 20 77 69 6c 6c 20 73 61 76 65 20 75 73 20 66  t will save us f
1b13e 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 68 61 76  rom.      ** hav
1b13f 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 64 64 69  ing to make addi
1b140 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 74 6f 20  tional calls to 
1b141 66 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e  fetch the conten
1b142 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a 20 20 20  t portion of.   
1b143 20 20 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 64     ** the record
1b144 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1b145 20 69 66 28 20 61 76 61 69 6c 3e 3d 70 61 79 6c   if( avail>=payl
1b146 6f 61 64 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  oadSize ){.     
1b147 20 20 20 7a 52 65 63 20 3d 20 7a 44 61 74 61 3b     zRec = zData;
1b148 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61 52 6f  .        pC->aRo
1b149 77 20 3d 20 28 75 38 2a 29 7a 44 61 74 61 3b 0a  w = (u8*)zData;.
1b14a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1b14b 20 20 20 20 20 70 43 2d 3e 61 52 6f 77 20 3d 20       pC->aRow = 
1b14c 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
1b14d 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  .    /* The foll
1b14e 6f 77 69 6e 67 20 61 73 73 65 72 74 20 69 73 20  owing assert is 
1b14f 74 72 75 65 20 69 6e 20 61 6c 6c 20 63 61 73 65  true in all case
1b150 73 20 61 63 63 65 70 74 20 77 68 65 6e 0a 20 20  s accept when.  
1b151 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73    ** the databas
1b152 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  e file has been 
1b153 63 6f 72 72 75 70 74 65 64 20 65 78 74 65 72 6e  corrupted extern
1b154 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a 20 20 20 20  ally..    **    
1b155 61 73 73 65 72 74 28 20 7a 52 65 63 21 3d 30 20  assert( zRec!=0 
1b156 7c 7c 20 61 76 61 69 6c 3e 3d 70 61 79 6c 6f 61  || avail>=payloa
1b157 64 53 69 7a 65 20 7c 7c 20 61 76 61 69 6c 3e 3d  dSize || avail>=
1b158 39 20 29 3b 20 2a 2f 0a 20 20 20 20 73 7a 48 64  9 ); */.    szHd
1b159 72 53 7a 20 3d 20 67 65 74 56 61 72 69 6e 74 33  rSz = getVarint3
1b15a 32 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 6f 66  2((u8*)zData, of
1b15b 66 73 65 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 54  fset);..    /* T
1b15c 68 65 20 4b 65 79 46 65 74 63 68 28 29 20 6f 72  he KeyFetch() or
1b15d 20 44 61 74 61 46 65 74 63 68 28 29 20 61 62 6f   DataFetch() abo
1b15e 76 65 20 61 72 65 20 66 61 73 74 20 61 6e 64 20  ve are fast and 
1b15f 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65 6e 74  will get the ent
1b160 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72  ire.    ** recor
1b161 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f 73 74  d header in most
1b162 20 63 61 73 65 73 2e 20 20 42 75 74 20 74 68 65   cases.  But the
1b163 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f 20 67  y will fail to g
1b164 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a  et the complete.
1b165 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 65      ** record he
1b166 61 64 65 72 20 69 66 20 74 68 65 20 72 65 63 6f  ader if the reco
1b167 72 64 20 68 65 61 64 65 72 20 64 6f 65 73 20 6e  rd header does n
1b168 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67  ot fit on a sing
1b169 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 69  le page.    ** i
1b16a 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 20 57  n the B-Tree.  W
1b16b 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73  hen that happens
1b16c 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56 64 62  , use sqlite3Vdb
1b16d 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20  eMemFromBtree() 
1b16e 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 69 72  to.    ** acquir
1b16f 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 68  e the complete h
1b170 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20 20 20  eader text..    
1b171 2a 2f 0a 20 20 20 20 69 66 28 20 21 7a 52 65 63  */.    if( !zRec
1b172 20 26 26 20 61 76 61 69 6c 3c 6f 66 66 73 65 74   && avail<offset
1b173 20 29 7b 0a 20 20 20 20 20 20 73 4d 65 6d 2e 66   ){.      sMem.f
1b174 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  lags = 0;.      
1b175 73 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 20  sMem.db = 0;.   
1b176 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1b177 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
1b178 70 43 72 73 72 2c 20 30 2c 20 6f 66 66 73 65 74  pCrsr, 0, offset
1b179 2c 20 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20 26  , pC->isIndex, &
1b17a 73 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28  sMem);.      if(
1b17b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1b17c 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6f  {.        goto o
1b17d 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20  p_column_out;.  
1b17e 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 44 61 74      }.      zDat
1b17f 61 20 3d 20 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20  a = sMem.z;.    
1b180 7d 0a 20 20 20 20 7a 45 6e 64 48 64 72 20 3d 20  }.    zEndHdr = 
1b181 28 75 38 20 2a 29 26 7a 44 61 74 61 5b 6f 66 66  (u8 *)&zData[off
1b182 73 65 74 5d 3b 0a 20 20 20 20 7a 49 64 78 20 3d  set];.    zIdx =
1b183 20 28 75 38 20 2a 29 26 7a 44 61 74 61 5b 73 7a   (u8 *)&zData[sz
1b184 48 64 72 53 7a 5d 3b 0a 0a 20 20 20 20 2f 2a 20  HdrSz];..    /* 
1b185 53 63 61 6e 20 74 68 65 20 68 65 61 64 65 72 20  Scan the header 
1b186 61 6e 64 20 75 73 65 20 69 74 20 74 6f 20 66 69  and use it to fi
1b187 6c 6c 20 69 6e 20 74 68 65 20 61 54 79 70 65 5b  ll in the aType[
1b188 5d 20 61 6e 64 20 61 4f 66 66 73 65 74 5b 5d 0a  ] and aOffset[].
1b189 20 20 20 20 2a 2a 20 61 72 72 61 79 73 2e 20 20      ** arrays.  
1b18a 61 54 79 70 65 5b 69 5d 20 77 69 6c 6c 20 63 6f  aType[i] will co
1b18b 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69  ntain the type i
1b18c 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 69  nteger for the i
1b18d 2d 74 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d  -th.    ** colum
1b18e 6e 20 61 6e 64 20 61 4f 66 66 73 65 74 5b 69 5d  n and aOffset[i]
1b18f 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68   will contain th
1b190 65 20 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68  e offset from th
1b191 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20  e beginning.    
1b192 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  ** of the record
1b193 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
1b194 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68   the data for th
1b195 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 0a 20 20  e i-th column.  
1b196 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
1b197 3b 20 69 3c 6e 46 69 65 6c 64 3b 20 69 2b 2b 29  ; i<nField; i++)
1b198 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 49 64 78  {.      if( zIdx
1b199 3c 7a 45 6e 64 48 64 72 20 29 7b 0a 20 20 20 20  <zEndHdr ){.    
1b19a 20 20 20 20 61 4f 66 66 73 65 74 5b 69 5d 20 3d      aOffset[i] =
1b19b 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20   offset;.       
1b19c 20 7a 49 64 78 20 2b 3d 20 67 65 74 56 61 72 69   zIdx += getVari
1b19d 6e 74 33 32 28 7a 49 64 78 2c 20 61 54 79 70 65  nt32(zIdx, aType
1b19e 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 6f 66  [i]);.        of
1b19f 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 56  fset += sqlite3V
1b1a0 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
1b1a1 28 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 20  (aType[i]);.    
1b1a2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1b1a3 20 2f 2a 20 49 66 20 69 20 69 73 20 6c 65 73 73   /* If i is less
1b1a4 20 74 68 61 74 20 6e 46 69 65 6c 64 2c 20 74 68   that nField, th
1b1a5 65 6e 20 74 68 65 72 65 20 61 72 65 20 6c 65 73  en there are les
1b1a6 73 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69 73  s fields in this
1b1a7 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 63 6f  .        ** reco
1b1a8 72 64 20 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f  rd than SetNumCo
1b1a9 6c 75 6d 6e 73 20 69 6e 64 69 63 61 74 65 64 20  lumns indicated 
1b1aa 74 68 65 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e  there are column
1b1ab 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20  s in the.       
1b1ac 20 2a 2a 20 74 61 62 6c 65 2e 20 53 65 74 20 74   ** table. Set t
1b1ad 68 65 20 6f 66 66 73 65 74 20 66 6f 72 20 61 6e  he offset for an
1b1ae 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 73 20  y extra columns 
1b1af 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 0a 20  not present in. 
1b1b0 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65         ** the re
1b1b1 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 73 20  cord to 0. This 
1b1b2 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c 6f 77  tells code below
1b1b3 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c   to store a NULL
1b1b4 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 74  .        ** inst
1b1b5 65 61 64 20 6f 66 20 64 65 73 65 72 69 61 6c 69  ead of deseriali
1b1b6 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66 72 6f  zing a value fro
1b1b7 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20  m the record..  
1b1b8 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1b1b9 20 61 4f 66 66 73 65 74 5b 69 5d 20 3d 20 30 3b   aOffset[i] = 0;
1b1ba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1b1bb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1b1bc 6d 52 65 6c 65 61 73 65 28 26 73 4d 65 6d 29 3b  mRelease(&sMem);
1b1bd 0a 20 20 20 20 73 4d 65 6d 2e 66 6c 61 67 73 20  .    sMem.flags 
1b1be 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 20  = MEM_Null;..   
1b1bf 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 72   /* If we have r
1b1c0 65 61 64 20 6d 6f 72 65 20 68 65 61 64 65 72 20  ead more header 
1b1c1 64 61 74 61 20 74 68 61 6e 20 77 61 73 20 63 6f  data than was co
1b1c2 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 68  ntained in the h
1b1c3 65 61 64 65 72 2c 0a 20 20 20 20 2a 2a 20 6f 72  eader,.    ** or
1b1c4 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   if the end of t
1b1c5 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70  he last field ap
1b1c6 70 65 61 72 73 20 74 6f 20 62 65 20 70 61 73 74  pears to be past
1b1c7 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a   the end of the.
1b1c8 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2c 20 6f      ** record, o
1b1c9 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20  r if the end of 
1b1ca 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61  the last field a
1b1cb 70 70 65 61 72 73 20 74 6f 20 62 65 20 62 65 66  ppears to be bef
1b1cc 6f 72 65 20 74 68 65 20 65 6e 64 0a 20 20 20 20  ore the end.    
1b1cd 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  ** of the record
1b1ce 20 28 77 68 65 6e 20 61 6c 6c 20 66 69 65 6c 64   (when all field
1b1cf 73 20 70 72 65 73 65 6e 74 29 2c 20 74 68 65 6e  s present), then
1b1d0 20 77 65 20 6d 75 73 74 20 62 65 20 64 65 61 6c   we must be deal
1b1d1 69 6e 67 20 0a 20 20 20 20 2a 2a 20 77 69 74 68  ing .    ** with
1b1d2 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62   a corrupt datab
1b1d3 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
1b1d4 69 66 28 20 7a 49 64 78 3e 7a 45 6e 64 48 64 72  if( zIdx>zEndHdr
1b1d5 20 7c 7c 20 6f 66 66 73 65 74 3e 70 61 79 6c 6f   || offset>paylo
1b1d6 61 64 53 69 7a 65 20 0a 20 20 20 20 20 7c 7c 20  adSize .     || 
1b1d7 28 7a 49 64 78 3d 3d 7a 45 6e 64 48 64 72 20 26  (zIdx==zEndHdr &
1b1d8 26 20 6f 66 66 73 65 74 21 3d 70 61 79 6c 6f 61  & offset!=payloa
1b1d9 64 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 20 20  dSize) ){.      
1b1da 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
1b1db 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
1b1dc 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f  goto op_column_o
1b1dd 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ut;.    }.  }.. 
1b1de 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6c 75   /* Get the colu
1b1df 6d 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20  mn information. 
1b1e0 49 66 20 61 4f 66 66 73 65 74 5b 70 32 5d 20 69  If aOffset[p2] i
1b1e1 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1b1e2 20 0a 20 20 2a 2a 20 64 65 73 65 72 69 61 6c 69   .  ** deseriali
1b1e3 7a 65 20 74 68 65 20 76 61 6c 75 65 20 66 72 6f  ze the value fro
1b1e4 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 20 49 66  m the record. If
1b1e5 20 61 4f 66 66 73 65 74 5b 70 32 5d 20 69 73 20   aOffset[p2] is 
1b1e6 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20  zero,.  ** then 
1b1e7 74 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e  there are not en
1b1e8 6f 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74  ough fields in t
1b1e9 68 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74  he record to sat
1b1ea 69 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65  isfy the.  ** re
1b1eb 71 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20  quest.  In this 
1b1ec 63 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61  case, set the va
1b1ed 6c 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50  lue NULL or to P
1b1ee 34 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20  4 if P4 is.  ** 
1b1ef 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d  a pointer to a M
1b1f0 65 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a  em object..  */.
1b1f1 20 20 69 66 28 20 61 4f 66 66 73 65 74 5b 70 32    if( aOffset[p2
1b1f2 5d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ] ){.    assert(
1b1f3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1b1f4 3b 0a 20 20 20 20 69 66 28 20 7a 52 65 63 20 29  ;.    if( zRec )
1b1f5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1b1f6 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74  dbeMemReleaseExt
1b1f7 65 72 6e 61 6c 28 70 44 65 73 74 29 3b 0a 20 20  ernal(pDest);.  
1b1f8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
1b1f9 65 72 69 61 6c 47 65 74 28 28 75 38 20 2a 29 26  erialGet((u8 *)&
1b1fa 7a 52 65 63 5b 61 4f 66 66 73 65 74 5b 70 32 5d  zRec[aOffset[p2]
1b1fb 5d 2c 20 61 54 79 70 65 5b 70 32 5d 2c 20 70 44  ], aType[p2], pD
1b1fc 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  est);.    }else{
1b1fd 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c  .      len = sql
1b1fe 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
1b1ff 70 65 4c 65 6e 28 61 54 79 70 65 5b 70 32 5d 29  peLen(aType[p2])
1b200 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1b201 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 4d 65 6d  dbeMemMove(&sMem
1b202 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20  , pDest);.      
1b203 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1b204 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 72  MemFromBtree(pCr
1b205 73 72 2c 20 61 4f 66 66 73 65 74 5b 70 32 5d 2c  sr, aOffset[p2],
1b206 20 6c 65 6e 2c 20 70 43 2d 3e 69 73 49 6e 64 65   len, pC->isInde
1b207 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20 20 20  x, &sMem);.     
1b208 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1b209 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
1b20a 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74  to op_column_out
1b20b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1b20c 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a 3b 0a  zData = sMem.z;.
1b20d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1b20e 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a 29  eSerialGet((u8*)
1b20f 7a 44 61 74 61 2c 20 61 54 79 70 65 5b 70 32 5d  zData, aType[p2]
1b210 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 0a  , pDest);.    }.
1b211 20 20 20 20 70 44 65 73 74 2d 3e 65 6e 63 20 3d      pDest->enc =
1b212 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 7d 65 6c   encoding;.  }el
1b213 73 65 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  se{.    if( pOp-
1b214 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4d 45 4d 20  >p4type==P4_MEM 
1b215 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b216 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
1b217 70 79 28 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70  py(pDest, pOp->p
1b218 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74  4.pMem, MEM_Stat
1b219 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ic);.    }else{.
1b21a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 44        assert( pD
1b21b 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e  est->flags&MEM_N
1b21c 75 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ull );.    }.  }
1b21d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 64 79 6e  ..  /* If we dyn
1b21e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
1b21f 65 64 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64  ed space to hold
1b220 20 74 68 65 20 64 61 74 61 20 28 69 6e 20 74 68   the data (in th
1b221 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64  e.  ** sqlite3Vd
1b222 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29  beMemFromBtree()
1b223 20 63 61 6c 6c 20 61 62 6f 76 65 29 20 74 68 65   call above) the
1b224 6e 20 74 72 61 6e 73 66 65 72 20 63 6f 6e 74 72  n transfer contr
1b225 6f 6c 20 6f 66 20 74 68 61 74 0a 20 20 2a 2a 20  ol of that.  ** 
1b226 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
1b227 63 61 74 65 64 20 73 70 61 63 65 20 6f 76 65 72  cated space over
1b228 20 74 6f 20 74 68 65 20 70 44 65 73 74 20 73 74   to the pDest st
1b229 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68  ructure..  ** Th
1b22a 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 6d 65  is prevents a me
1b22b 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 20 2a 2f 0a  mory copy..  */.
1b22c 20 20 69 66 28 20 73 4d 65 6d 2e 7a 4d 61 6c 6c    if( sMem.zMall
1b22d 6f 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  oc ){.    assert
1b22e 28 20 73 4d 65 6d 2e 7a 3d 3d 73 4d 65 6d 2e 7a  ( sMem.z==sMem.z
1b22f 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 61 73  Malloc );.    as
1b230 73 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e 66  sert( !(pDest->f
1b231 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20  lags & MEM_Dyn) 
1b232 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
1b233 28 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 20  (pDest->flags & 
1b234 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74  (MEM_Blob|MEM_St
1b235 72 29 29 20 7c 7c 20 70 44 65 73 74 2d 3e 7a 3d  r)) || pDest->z=
1b236 3d 73 4d 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 70  =sMem.z );.    p
1b237 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  Dest->flags &= ~
1b238 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53  (MEM_Ephem|MEM_S
1b239 74 61 74 69 63 29 3b 0a 20 20 20 20 70 44 65 73  tatic);.    pDes
1b23a 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  t->flags |= MEM_
1b23b 54 65 72 6d 3b 0a 20 20 20 20 70 44 65 73 74 2d  Term;.    pDest-
1b23c 3e 7a 20 3d 20 73 4d 65 6d 2e 7a 3b 0a 20 20 20  >z = sMem.z;.   
1b23d 20 70 44 65 73 74 2d 3e 7a 4d 61 6c 6c 6f 63 20   pDest->zMalloc 
1b23e 3d 20 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a  = sMem.zMalloc;.
1b23f 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69    }..  rc = sqli
1b240 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72  te3VdbeMemMakeWr
1b241 69 74 65 61 62 6c 65 28 70 44 65 73 74 29 3b 0a  iteable(pDest);.
1b242 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a  .op_column_out:.
1b243 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1b244 42 53 49 5a 45 28 70 44 65 73 74 29 3b 0a 20 20  BSIZE(pDest);.  
1b245 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1b246 4f 70 2d 3e 70 33 2c 20 70 44 65 73 74 29 3b 0a  Op->p3, pDest);.
1b247 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1b248 70 63 6f 64 65 3a 20 41 66 66 69 6e 69 74 79 20  pcode: Affinity 
1b249 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a  P1 P2 * P4 *.**.
1b24a 2a 2a 20 41 70 70 6c 79 20 61 66 66 69 6e 69 74  ** Apply affinit
1b24b 69 65 73 20 74 6f 20 61 20 72 61 6e 67 65 20 6f  ies to a range o
1b24c 66 20 50 32 20 72 65 67 69 73 74 65 72 73 20 73  f P2 registers s
1b24d 74 61 72 74 69 6e 67 20 77 69 74 68 20 50 31 2e  tarting with P1.
1b24e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 73  .**.** P4 is a s
1b24f 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50 32  tring that is P2
1b250 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67   characters long
1b251 2e 20 54 68 65 20 6e 74 68 20 63 68 61 72 61 63  . The nth charac
1b252 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74  ter of the.** st
1b253 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 20 74  ring indicates t
1b254 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69  he column affini
1b255 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  ty that should b
1b256 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6e  e used for the n
1b257 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 6c  th.** memory cel
1b258 6c 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a  l in the range..
1b259 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 66 66 69 6e  */.case OP_Affin
1b25a 69 74 79 3a 20 7b 0a 20 20 63 68 61 72 20 2a 7a  ity: {.  char *z
1b25b 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e  Affinity = pOp->
1b25c 70 34 2e 7a 3b 0a 20 20 4d 65 6d 20 2a 70 44 61  p4.z;.  Mem *pDa
1b25d 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  ta0 = &p->aMem[p
1b25e 4f 70 2d 3e 70 31 5d 3b 0a 20 20 4d 65 6d 20 2a  Op->p1];.  Mem *
1b25f 70 4c 61 73 74 20 3d 20 26 70 44 61 74 61 30 5b  pLast = &pData0[
1b260 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a 20 20 4d 65  pOp->p2-1];.  Me
1b261 6d 20 2a 70 52 65 63 3b 0a 0a 20 20 66 6f 72 28  m *pRec;..  for(
1b262 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 65  pRec=pData0; pRe
1b263 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b 2b  c<=pLast; pRec++
1b264 29 7b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f  ){.    ExpandBlo
1b265 62 28 70 52 65 63 29 3b 0a 20 20 20 20 61 70 70  b(pRec);.    app
1b266 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 63 2c  lyAffinity(pRec,
1b267 20 7a 41 66 66 69 6e 69 74 79 5b 70 52 65 63 2d   zAffinity[pRec-
1b268 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e  pData0], encodin
1b269 67 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  g);.  }.  break;
1b26a 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d  .}../* Opcode: M
1b26b 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50 32 20  akeRecord P1 P2 
1b26c 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f  P3 P4 *.**.** Co
1b26d 6e 76 65 72 74 20 50 32 20 72 65 67 69 73 74 65  nvert P2 registe
1b26e 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  rs beginning wit
1b26f 68 20 50 31 20 69 6e 74 6f 20 61 20 73 69 6e 67  h P1 into a sing
1b270 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 73 75 69 74  le entry.** suit
1b271 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20  able for use as 
1b272 61 20 64 61 74 61 20 72 65 63 6f 72 64 20 69 6e  a data record in
1b273 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c   a database tabl
1b274 65 20 6f 72 20 61 73 20 61 20 6b 65 79 0a 2a 2a  e or as a key.**
1b275 20 69 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 54   in an index.  T
1b276 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68  he details of th
1b277 65 20 66 6f 72 6d 61 74 20 61 72 65 20 69 72 72  e format are irr
1b278 65 6c 65 76 61 6e 74 20 61 73 20 6c 6f 6e 67 20  elevant as long 
1b279 61 73 0a 2a 2a 20 74 68 65 20 4f 50 5f 43 6f 6c  as.** the OP_Col
1b27a 75 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e 20 64  umn opcode can d
1b27b 65 63 6f 64 65 20 74 68 65 20 72 65 63 6f 72 64  ecode the record
1b27c 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 65 66 65 72   later..** Refer
1b27d 20 74 6f 20 73 6f 75 72 63 65 20 63 6f 64 65 20   to source code 
1b27e 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 74 68 65  comments for the
1b27f 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20   details of the 
1b280 72 65 63 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74  record.** format
1b281 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62  ..**.** P4 may b
1b282 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  e a string that 
1b283 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72 73  is P2 characters
1b284 20 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 20   long.  The nth 
1b285 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65  character of the
1b286 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63  .** string indic
1b287 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  ates the column 
1b288 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
1b289 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
1b28a 20 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65 6c   the nth.** fiel
1b28b 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b  d of the index k
1b28c 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61  ey..**.** The ma
1b28d 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61 72 61  pping from chara
1b28e 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69 74 79  cter to affinity
1b28f 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65   is given by the
1b290 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a 2a 20   SQLITE_AFF_.** 
1b291 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 69  macros defined i
1b292 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a  n sqliteInt.h..*
1b293 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 4e 55  *.** If P4 is NU
1b294 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 64 65  LL then all inde
1b295 78 20 66 69 65 6c 64 73 20 68 61 76 65 20 74 68  x fields have th
1b296 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e 45 2e  e affinity NONE.
1b297 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 61 6b 65  .*/.case OP_Make
1b298 52 65 63 6f 72 64 3a 20 7b 0a 20 20 2f 2a 20 41  Record: {.  /* A
1b299 73 73 75 6d 69 6e 67 20 74 68 65 20 72 65 63 6f  ssuming the reco
1b29a 72 64 20 63 6f 6e 74 61 69 6e 73 20 4e 20 66 69  rd contains N fi
1b29b 65 6c 64 73 2c 20 74 68 65 20 72 65 63 6f 72 64  elds, the record
1b29c 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20   format looks.  
1b29d 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20  ** like this:.  
1b29e 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d  **.  ** --------
1b29f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b2a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b2a1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b2a2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b2a3 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65  .  ** | hdr-size
1b2a4 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79 70 65   | type 0 | type
1b2a5 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20   1 | ... | type 
1b2a6 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e  N-1 | data0 | ..
1b2a7 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c 20 0a  . | data N-1 | .
1b2a8 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    ** -----------
1b2a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b2aa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b2ab 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b2ac 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20  -------------.  
1b2ad 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 28 30 29 20  **.  ** Data(0) 
1b2ae 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65  is taken from re
1b2af 67 69 73 74 65 72 20 50 31 2e 20 20 44 61 74 61  gister P1.  Data
1b2b0 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d 20 72  (1) comes from r
1b2b1 65 67 69 73 74 65 72 20 50 31 2b 31 0a 20 20 2a  egister P1+1.  *
1b2b2 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74 68 2e 0a  * and so froth..
1b2b3 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20 74    **.  ** Each t
1b2b4 79 70 65 20 66 69 65 6c 64 20 69 73 20 61 20 76  ype field is a v
1b2b5 61 72 69 6e 74 20 72 65 70 72 65 73 65 6e 74 69  arint representi
1b2b6 6e 67 20 74 68 65 20 73 65 72 69 61 6c 20 74 79  ng the serial ty
1b2b7 70 65 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20  pe of the .  ** 
1b2b8 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 61  corresponding da
1b2b9 74 61 20 65 6c 65 6d 65 6e 74 20 28 73 65 65 20  ta element (see 
1b2ba 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1b2bb 6c 54 79 70 65 28 29 29 2e 20 54 68 65 0a 20 20  lType()). The.  
1b2bc 2a 2a 20 68 64 72 2d 73 69 7a 65 20 66 69 65 6c  ** hdr-size fiel
1b2bd 64 20 69 73 20 61 6c 73 6f 20 61 20 76 61 72 69  d is also a vari
1b2be 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65 20  nt which is the 
1b2bf 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20  offset from the 
1b2c0 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f  beginning.  ** o
1b2c1 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20  f the record to 
1b2c2 64 61 74 61 30 2e 0a 20 20 2a 2f 0a 20 20 75 38  data0..  */.  u8
1b2c3 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b 20 20 20   *zNewRecord;   
1b2c4 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72       /* A buffer
1b2c5 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74   to hold the dat
1b2c6 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65  a for the new re
1b2c7 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  cord */.  Mem *p
1b2c8 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
1b2c9 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65 63 6f   /* The new reco
1b2ca 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e 44 61 74  rd */.  u64 nDat
1b2cb 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  a = 0;         /
1b2cc 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1b2cd 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20  s of data space 
1b2ce 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72 20 3d 20  */.  int nHdr = 
1b2cf 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  0;          /* N
1b2d0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1b2d1 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 2a  f header space *
1b2d2 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 20 3d 20  /.  i64 nByte = 
1b2d3 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  0;         /* Da
1b2d4 74 61 20 73 70 61 63 65 20 72 65 71 75 69 72 65  ta space require
1b2d5 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72  d for this recor
1b2d6 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f  d */.  int nZero
1b2d7 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
1b2d8 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20   Number of zero 
1b2d9 62 79 74 65 73 20 61 74 20 74 68 65 20 65 6e 64  bytes at the end
1b2da 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a   of the record *
1b2db 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 3b  /.  int nVarint;
1b2dc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1b2dd 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
1b2de 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 75   a varint */.  u
1b2df 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 20  32 serial_type; 
1b2e0 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 66 69        /* Type fi
1b2e1 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44  eld */.  Mem *pD
1b2e2 61 74 61 30 3b 20 20 20 20 20 20 20 20 20 20 20  ata0;           
1b2e3 2f 2a 20 46 69 72 73 74 20 66 69 65 6c 64 20 74  /* First field t
1b2e4 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64 20 69 6e  o be combined in
1b2e5 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  to the record */
1b2e6 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20  .  Mem *pLast;  
1b2e7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
1b2e8 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 72  t field of the r
1b2e9 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
1b2ea 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20  Field;          
1b2eb 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
1b2ec 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63  ields in the rec
1b2ed 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ord */.  char *z
1b2ee 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20  Affinity;       
1b2ef 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20  /* The affinity 
1b2f0 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 72  string for the r
1b2f1 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 66  ecord */.  int f
1b2f2 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20  ile_format;     
1b2f3 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 6d 61 74    /* File format
1b2f4 20 74 6f 20 75 73 65 20 66 6f 72 20 65 6e 63 6f   to use for enco
1b2f5 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  ding */.  int i;
1b2f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b2f7 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 20 69   /* Space used i
1b2f8 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a  n zNewRecord[] *
1b2f9 2f 0a 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4f  /..  nField = pO
1b2fa 70 2d 3e 70 31 3b 0a 20 20 7a 41 66 66 69 6e 69  p->p1;.  zAffini
1b2fb 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a  ty = pOp->p4.z;.
1b2fc 20 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c 64    assert( nField
1b2fd 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 20  >0 && pOp->p2>0 
1b2fe 26 26 20 70 4f 70 2d 3e 70 32 2b 6e 46 69 65 6c  && pOp->p2+nFiel
1b2ff 64 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  d<=p->nMem );.  
1b300 70 44 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65  pData0 = &p->aMe
1b301 6d 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 6e 46 69  m[nField];.  nFi
1b302 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  eld = pOp->p2;. 
1b303 20 70 4c 61 73 74 20 3d 20 26 70 44 61 74 61 30   pLast = &pData0
1b304 5b 6e 46 69 65 6c 64 2d 31 5d 3b 0a 20 20 66 69  [nField-1];.  fi
1b305 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 2d 3e 6d  le_format = p->m
1b306 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61  inWriteFileForma
1b307 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68  t;..  /* Loop th
1b308 72 6f 75 67 68 20 74 68 65 20 65 6c 65 6d 65 6e  rough the elemen
1b309 74 73 20 74 68 61 74 20 77 69 6c 6c 20 6d 61 6b  ts that will mak
1b30a 65 20 75 70 20 74 68 65 20 72 65 63 6f 72 64 20  e up the record 
1b30b 74 6f 20 66 69 67 75 72 65 0a 20 20 2a 2a 20 6f  to figure.  ** o
1b30c 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63  ut how much spac
1b30d 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f  e is required fo
1b30e 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64  r the new record
1b30f 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 52 65  ..  */.  for(pRe
1b310 63 3d 70 44 61 74 61 30 3b 20 70 52 65 63 3c 3d  c=pData0; pRec<=
1b311 70 4c 61 73 74 3b 20 70 52 65 63 2b 2b 29 7b 0a  pLast; pRec++){.
1b312 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
1b313 20 69 66 28 20 7a 41 66 66 69 6e 69 74 79 20 29   if( zAffinity )
1b314 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 66  {.      applyAff
1b315 69 6e 69 74 79 28 70 52 65 63 2c 20 7a 41 66 66  inity(pRec, zAff
1b316 69 6e 69 74 79 5b 70 52 65 63 2d 70 44 61 74 61  inity[pRec-pData
1b317 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  0], encoding);. 
1b318 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 52 65     }.    if( pRe
1b319 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 72  c->flags&MEM_Zer
1b31a 6f 20 26 26 20 70 52 65 63 2d 3e 6e 3e 30 20 29  o && pRec->n>0 )
1b31b 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1b31c 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
1b31d 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20  (pRec);.    }.  
1b31e 20 20 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20    serial_type = 
1b31f 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1b320 6c 54 79 70 65 28 70 52 65 63 2c 20 66 69 6c 65  lType(pRec, file
1b321 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 6c 65  _format);.    le
1b322 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  n = sqlite3VdbeS
1b323 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72  erialTypeLen(ser
1b324 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 6e  ial_type);.    n
1b325 44 61 74 61 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20  Data += len;.   
1b326 20 6e 48 64 72 20 2b 3d 20 73 71 6c 69 74 65 33   nHdr += sqlite3
1b327 56 61 72 69 6e 74 4c 65 6e 28 73 65 72 69 61 6c  VarintLen(serial
1b328 5f 74 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20  _type);.    if( 
1b329 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pRec->flags & ME
1b32a 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
1b32b 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 72  /* Only pure zer
1b32c 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 63  o-filled BLOBs c
1b32d 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20 74  an be input to t
1b32e 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 20  his Opcode..    
1b32f 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 61    ** We do not a
1b330 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 20  llow blobs with 
1b331 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20 7a  a prefix and a z
1b332 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e  ero-filled tail.
1b333 20 2a 2f 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20   */.      nZero 
1b334 2b 3d 20 70 52 65 63 2d 3e 75 2e 6e 5a 65 72 6f  += pRec->u.nZero
1b335 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1b336 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65  len ){.      nZe
1b337 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ro = 0;.    }.  
1b338 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20  }..  /* Add the 
1b339 69 6e 69 74 69 61 6c 20 68 65 61 64 65 72 20 76  initial header v
1b33a 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 61 6c 20  arint and total 
1b33b 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 6e 48  the size */.  nH
1b33c 64 72 20 2b 3d 20 6e 56 61 72 69 6e 74 20 3d 20  dr += nVarint = 
1b33d 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
1b33e 28 6e 48 64 72 29 3b 0a 20 20 69 66 28 20 6e 56  (nHdr);.  if( nV
1b33f 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56 61 72  arint<sqlite3Var
1b340 69 6e 74 4c 65 6e 28 6e 48 64 72 29 20 29 7b 0a  intLen(nHdr) ){.
1b341 20 20 20 20 6e 48 64 72 2b 2b 3b 0a 20 20 7d 0a      nHdr++;.  }.
1b342 20 20 6e 42 79 74 65 20 3d 20 6e 48 64 72 2b 6e    nByte = nHdr+n
1b343 44 61 74 61 2d 6e 5a 65 72 6f 3b 0a 20 20 69 66  Data-nZero;.  if
1b344 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d  ( nByte>db->aLim
1b345 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1b346 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67  LENGTH] ){.    g
1b347 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d  oto too_big;.  }
1b348 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
1b349 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 67 69   the output regi
1b34a 73 74 65 72 20 68 61 73 20 61 20 62 75 66 66 65  ster has a buffe
1b34b 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74  r large enough t
1b34c 6f 20 73 74 6f 72 65 20 0a 20 20 2a 2a 20 74 68  o store .  ** th
1b34d 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 54 68  e new record. Th
1b34e 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65  e output registe
1b34f 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73 20 6e  r (pOp->p3) is n
1b350 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 20 20  ot allowed to.  
1b351 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65  ** be one of the
1b352 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73   input registers
1b353 20 28 62 65 63 61 75 73 65 20 74 68 65 20 66 6f   (because the fo
1b354 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 0a  llowing call to.
1b355 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65    ** sqlite3Vdbe
1b356 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c 64 20  MemGrow() could 
1b357 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61 6c 75  clobber the valu
1b358 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 75  e before it is u
1b359 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  sed)..  */.  ass
1b35a 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70  ert( pOp->p3<pOp
1b35b 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e  ->p1 || pOp->p3>
1b35c 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32  =pOp->p1+pOp->p2
1b35d 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d   );.  pOut = &p-
1b35e 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1b35f 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1b360 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28  eMemGrow(pOut, (
1b361 69 6e 74 29 6e 42 79 74 65 2c 20 30 29 20 29 7b  int)nByte, 0) ){
1b362 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  .    goto no_mem
1b363 3b 0a 20 20 7d 0a 20 20 7a 4e 65 77 52 65 63 6f  ;.  }.  zNewReco
1b364 72 64 20 3d 20 28 75 38 20 2a 29 70 4f 75 74 2d  rd = (u8 *)pOut-
1b365 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20  >z;..  /* Write 
1b366 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1b367 69 20 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28  i = putVarint32(
1b368 7a 4e 65 77 52 65 63 6f 72 64 2c 20 6e 48 64 72  zNewRecord, nHdr
1b369 29 3b 0a 20 20 66 6f 72 28 70 52 65 63 3d 70 44  );.  for(pRec=pD
1b36a 61 74 61 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73  ata0; pRec<=pLas
1b36b 74 3b 20 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20  t; pRec++){.    
1b36c 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71  serial_type = sq
1b36d 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1b36e 79 70 65 28 70 52 65 63 2c 20 66 69 6c 65 5f 66  ype(pRec, file_f
1b36f 6f 72 6d 61 74 29 3b 0a 20 20 20 20 69 20 2b 3d  ormat);.    i +=
1b370 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 7a 4e   putVarint32(&zN
1b371 65 77 52 65 63 6f 72 64 5b 69 5d 2c 20 73 65 72  ewRecord[i], ser
1b372 69 61 6c 5f 74 79 70 65 29 3b 20 20 20 20 20 20  ial_type);      
1b373 2f 2a 20 73 65 72 69 61 6c 20 74 79 70 65 20 2a  /* serial type *
1b374 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 70 52 65 63  /.  }.  for(pRec
1b375 3d 70 44 61 74 61 30 3b 20 70 52 65 63 3c 3d 70  =pData0; pRec<=p
1b376 4c 61 73 74 3b 20 70 52 65 63 2b 2b 29 7b 20 20  Last; pRec++){  
1b377 2f 2a 20 73 65 72 69 61 6c 20 64 61 74 61 20 2a  /* serial data *
1b378 2f 0a 20 20 20 20 69 20 2b 3d 20 73 71 6c 69 74  /.    i += sqlit
1b379 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28  e3VdbeSerialPut(
1b37a 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69 5d 2c 20  &zNewRecord[i], 
1b37b 28 69 6e 74 29 28 6e 42 79 74 65 2d 69 29 2c 20  (int)(nByte-i), 
1b37c 70 52 65 63 2c 66 69 6c 65 5f 66 6f 72 6d 61 74  pRec,file_format
1b37d 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  );.  }.  assert(
1b37e 20 69 3d 3d 6e 42 79 74 65 20 29 3b 0a 0a 20 20   i==nByte );..  
1b37f 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
1b380 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
1b381 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 2d  >nMem );.  pOut-
1b382 3e 6e 20 3d 20 28 69 6e 74 29 6e 42 79 74 65 3b  >n = (int)nByte;
1b383 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d  .  pOut->flags =
1b384 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f   MEM_Blob | MEM_
1b385 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78 44 65  Dyn;.  pOut->xDe
1b386 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 5a 65  l = 0;.  if( nZe
1b387 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e  ro ){.    pOut->
1b388 75 2e 6e 5a 65 72 6f 20 3d 20 6e 5a 65 72 6f 3b  u.nZero = nZero;
1b389 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  .    pOut->flags
1b38a 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20   |= MEM_Zero;.  
1b38b 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20  }.  pOut->enc = 
1b38c 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a  SQLITE_UTF8;  /*
1b38d 20 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f   In case the blo
1b38e 62 20 69 73 20 65 76 65 72 20 63 6f 6e 76 65 72  b is ever conver
1b38f 74 65 64 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20  ted to text */. 
1b390 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1b391 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a  pOp->p3, pOut);.
1b392 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1b393 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62  BSIZE(pOut);.  b
1b394 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1b395 64 65 3a 20 53 74 61 74 65 6d 65 6e 74 20 50 31  de: Statement P1
1b396 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42   * * * *.**.** B
1b397 65 67 69 6e 20 61 6e 20 69 6e 64 69 76 69 64 75  egin an individu
1b398 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  al statement tra
1b399 6e 73 61 63 74 69 6f 6e 20 77 68 69 63 68 20 69  nsaction which i
1b39a 73 20 70 61 72 74 20 6f 66 20 61 20 6c 61 72 67  s part of a larg
1b39b 65 72 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  er.** transactio
1b39c 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64  n.  This is need
1b39d 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 73  ed so that the s
1b39e 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20  tatement.** can 
1b39f 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  be rolled back a
1b3a0 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 77 69  fter an error wi
1b3a1 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
1b3a2 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a  roll back the.**
1b3a3 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74   entire transact
1b3a4 69 6f 6e 2e 20 20 54 68 65 20 73 74 61 74 65 6d  ion.  The statem
1b3a5 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
1b3a6 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c  will automatical
1b3a7 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 77 68 65  ly.** commit whe
1b3a8 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74 73  n the VDBE halts
1b3a9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64  ..**.** If the d
1b3aa 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1b3ab 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  on is currently 
1b3ac 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  in autocommit mo
1b3ad 64 65 20 28 74 68 61 74 20 0a 2a 2a 20 69 73 20  de (that .** is 
1b3ae 74 6f 20 73 61 79 2c 20 69 66 20 69 74 20 69 73  to say, if it is
1b3af 20 69 6e 20 62 65 74 77 65 65 6e 20 42 45 47 49   in between BEGI
1b3b0 4e 20 61 6e 64 20 43 4f 4d 4d 49 54 29 0a 2a 2a  N and COMMIT).**
1b3b1 20 61 6e 64 20 69 66 20 74 68 65 72 65 20 61 72   and if there ar
1b3b2 65 20 6e 6f 20 6f 74 68 65 72 20 61 63 74 69 76  e no other activ
1b3b3 65 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 20  e statements on 
1b3b4 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
1b3b5 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c  e.** connection,
1b3b6 20 74 68 65 6e 20 74 68 69 73 20 6f 70 65 72 61   then this opera
1b3b7 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
1b3b8 20 20 4e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74    No statement t
1b3b9 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73  ransaction.** is
1b3ba 20 6e 65 65 64 65 64 20 73 69 6e 63 65 20 61 6e   needed since an
1b3bb 79 20 65 72 72 6f 72 20 63 61 6e 20 75 73 65 20  y error can use 
1b3bc 74 68 65 20 6e 6f 72 6d 61 6c 20 52 4f 4c 4c 42  the normal ROLLB
1b3bd 41 43 4b 20 70 72 6f 63 65 73 73 20 74 6f 0a 2a  ACK process to.*
1b3be 2a 20 75 6e 64 6f 20 63 68 61 6e 67 65 73 2e 0a  * undo changes..
1b3bf 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 74 61 74 65  **.** If a state
1b3c0 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1b3c1 20 69 73 20 73 74 61 72 74 65 64 2c 20 74 68 65   is started, the
1b3c2 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  n a statement jo
1b3c3 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 77 69  urnal file.** wi
1b3c4 6c 6c 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20  ll be allocated 
1b3c5 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e  and initialized.
1b3c6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61 74 65  .**.** The state
1b3c7 6d 65 6e 74 20 69 73 20 62 65 67 75 6e 20 6f 6e  ment is begun on
1b3c8 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1b3c9 6c 65 20 77 69 74 68 20 69 6e 64 65 78 20 50 31  le with index P1
1b3ca 2e 20 20 54 68 65 20 6d 61 69 6e 0a 2a 2a 20 64  .  The main.** d
1b3cb 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73  atabase file has
1b3cc 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 61   an index of 0 a
1b3cd 6e 64 20 74 68 65 20 66 69 6c 65 20 75 73 65 64  nd the file used
1b3ce 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74   for temporary t
1b3cf 61 62 6c 65 73 0a 2a 2a 20 68 61 73 20 61 6e 20  ables.** has an 
1b3d0 69 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2f 0a 63  index of 1..*/.c
1b3d1 61 73 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74  ase OP_Statement
1b3d2 3a 20 7b 0a 20 20 69 66 28 20 64 62 2d 3e 61 75  : {.  if( db->au
1b3d3 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64  toCommit==0 || d
1b3d4 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
1b3d5 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20  >1 ){.    int i 
1b3d6 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 42  = pOp->p1;.    B
1b3d7 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 61  tree *pBt;.    a
1b3d8 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
1b3d9 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20  <db->nDb );.    
1b3da 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b  assert( db->aDb[
1b3db 69 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 20  i].pBt!=0 );.   
1b3dc 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69   pBt = db->aDb[i
1b3dd 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72  ].pBt;.    asser
1b3de 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  t( sqlite3BtreeI
1b3df 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29 3b  sInTrans(pBt) );
1b3e0 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d  .    assert( (p-
1b3e1 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1b3e2 3c 69 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 69  <i))!=0 );.    i
1b3e3 66 28 20 21 73 71 6c 69 74 65 33 42 74 72 65 65  f( !sqlite3Btree
1b3e4 49 73 49 6e 53 74 6d 74 28 70 42 74 29 20 29 7b  IsInStmt(pBt) ){
1b3e5 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1b3e6 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d  te3BtreeBeginStm
1b3e7 74 28 70 42 74 29 3b 0a 20 20 20 20 20 20 70 2d  t(pBt);.      p-
1b3e8 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e 74  >openedStatement
1b3e9 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
1b3ea 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1b3eb 70 63 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74  pcode: Savepoint
1b3ec 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a   P1 * * P4 *.**.
1b3ed 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65  ** Open, release
1b3ee 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65   or rollback the
1b3ef 20 73 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64   savepoint named
1b3f0 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 50 34   by parameter P4
1b3f1 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f  , depending.** o
1b3f2 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50  n the value of P
1b3f3 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77  1. To open a new
1b3f4 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d   savepoint, P1==
1b3f5 30 2e 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63  0. To release (c
1b3f6 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69  ommit) an.** exi
1b3f7 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c  sting savepoint,
1b3f8 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f   P1==1, or to ro
1b3f9 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69  llback an existi
1b3fa 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d  ng savepoint P1=
1b3fb 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  =2..*/.case OP_S
1b3fc 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 20 20 69 6e  avepoint: {.  in
1b3fd 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  t p1 = pOp->p1;.
1b3fe 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
1b3ff 70 4f 70 2d 3e 70 34 2e 7a 3b 20 20 20 20 20 20  pOp->p4.z;      
1b400 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61     /* Name of sa
1b401 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 2f 2a  vepoint */..  /*
1b402 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65   Assert that the
1b403 20 70 31 20 70 61 72 61 6d 65 74 65 72 20 69 73   p1 parameter is
1b404 20 76 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61   valid. Also tha
1b405 74 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  t if there is no
1b406 20 6f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73   open.  ** trans
1b407 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  action, then the
1b408 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79  re cannot be any
1b409 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 0a 20 20   savepoints. .  
1b40a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  */.  assert( db-
1b40b 3e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c  >pSavepoint==0 |
1b40c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74  | db->autoCommit
1b40d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
1b40e 20 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42   p1==SAVEPOINT_B
1b40f 45 47 49 4e 7c 7c 70 31 3d 3d 53 41 56 45 50 4f  EGIN||p1==SAVEPO
1b410 49 4e 54 5f 52 45 4c 45 41 53 45 7c 7c 70 31 3d  INT_RELEASE||p1=
1b411 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
1b412 41 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ACK );.  assert(
1b413 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20   db->pSavepoint 
1b414 7c 7c 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63  || db->isTransac
1b415 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30  tionSavepoint==0
1b416 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68   );.  assert( ch
1b417 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e  eckSavepointCoun
1b418 74 28 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20  t(db) );..  if( 
1b419 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45  p1==SAVEPOINT_BE
1b41a 47 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64  GIN ){.    if( d
1b41b 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e  b->writeVdbeCnt>
1b41c 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  0 ){.      /* A 
1b41d 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61  new savepoint ca
1b41e 6e 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 20  nnot be created 
1b41f 69 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74  if there are act
1b420 69 76 65 20 77 72 69 74 65 20 0a 20 20 20 20 20  ive write .     
1b421 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28   ** statements (
1b422 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77  i.e. open read/w
1b423 72 69 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c  rite incremental
1b424 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a   blob handles)..
1b425 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
1b426 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1b427 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1b428 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61   "cannot open sa
1b429 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20  vepoint - ".    
1b42a 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65      "SQL stateme
1b42b 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22  nts in progress"
1b42c 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
1b42d 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
1b42e 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
1b42f 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
1b430 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
1b431 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20        Savepoint 
1b432 2a 70 4e 65 77 3b 0a 0a 20 20 20 20 20 20 2f 2a  *pNew;..      /*
1b433 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 61   Create a new sa
1b434 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72  vepoint structur
1b435 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77  e. */.      pNew
1b436 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1b437 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
1b438 28 53 61 76 65 70 6f 69 6e 74 29 2b 6e 4e 61 6d  (Savepoint)+nNam
1b439 65 2b 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20  e+1);.      if( 
1b43a 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
1b43b 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63  pNew->zName = (c
1b43c 68 61 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a  har *)&pNew[1];.
1b43d 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70          memcpy(p
1b43e 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  New->zName, zNam
1b43f 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20  e, nName+1);.   
1b440 20 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20   .        /* If 
1b441 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e  there is no open
1b442 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
1b443 65 6e 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20  en mark this as 
1b444 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20  a special.      
1b445 20 20 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f    ** "transactio
1b446 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f  n savepoint". */
1b447 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d  .        if( db-
1b448 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
1b449 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74           db->aut
1b44a 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20  oCommit = 0;.   
1b44b 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61         db->isTra
1b44c 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e  nsactionSavepoin
1b44d 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  t = 1;.        }
1b44e 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1b44f 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b  db->nSavepoint++
1b450 3b 0a 09 7d 0a 20 20 20 20 0a 20 20 20 20 20 20  ;..}.    .      
1b451 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65    /* Link the ne
1b452 77 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74 6f  w savepoint into
1b453 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
1b454 6e 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f 0a  ndle's list. */.
1b455 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e          pNew->pN
1b456 65 78 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70  ext = db->pSavep
1b457 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62  oint;.        db
1b458 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70  ->pSavepoint = p
1b459 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  New;.      }.   
1b45a 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
1b45b 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65  Savepoint *pSave
1b45c 70 6f 69 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69  point;.    int i
1b45d 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a  Savepoint = 0;..
1b45e 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20      /* Find the 
1b45f 6e 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74 2e  named savepoint.
1b460 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
1b461 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c 20  such savepoint, 
1b462 74 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 61  then an.    ** a
1b463 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
1b464 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e  ned to the user.
1b465 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20 20    */.    for(.  
1b466 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 64      pSavepoint=d
1b467 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 0a  b->pSavepoint; .
1b468 20 20 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74        pSavepoint
1b469 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1b46a 6d 70 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 7a  mp(pSavepoint->z
1b46b 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  Name, zName);.  
1b46c 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 70      pSavepoint=p
1b46d 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74  Savepoint->pNext
1b46e 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 53  .    ){.      iS
1b46f 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20  avepoint++;.    
1b470 7d 0a 20 20 20 20 69 66 28 20 21 70 53 61 76 65  }.    if( !pSave
1b471 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 73  point ){.      s
1b472 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1b473 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1b474 20 22 6e 6f 20 73 75 63 68 20 73 61 76 65 70 6f   "no such savepo
1b475 69 6e 74 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29  int: %s", zName)
1b476 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
1b477 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d  ITE_ERROR;.    }
1b478 65 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20 20  else if( .      
1b479 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43    db->writeVdbeC
1b47a 6e 74 3e 30 20 7c 7c 20 28 70 31 3d 3d 53 41 56  nt>0 || (p1==SAV
1b47b 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1b47c 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  && db->activeVdb
1b47d 65 43 6e 74 3e 31 29 20 0a 20 20 20 20 29 7b 0a  eCnt>1) .    ){.
1b47e 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e        /* It is n
1b47f 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72  ot possible to r
1b480 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20  elease (commit) 
1b481 61 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74  a savepoint if t
1b482 68 65 72 65 20 61 72 65 20 0a 20 20 20 20 20 20  here are .      
1b483 2a 2a 20 61 63 74 69 76 65 20 77 72 69 74 65 20  ** active write 
1b484 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69  statements. It i
1b485 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  s not possible t
1b486 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76  o rollback a sav
1b487 65 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20  epoint.      ** 
1b488 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
1b489 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e   active statemen
1b48a 74 73 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20  ts at all..     
1b48b 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1b48c 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1b48d 45 72 72 4d 73 67 2c 20 64 62 2c 20 0a 20 20 20  ErrMsg, db, .   
1b48e 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 25 73 20       "cannot %s 
1b48f 73 61 76 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20  savepoint - SQL 
1b490 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72  statements in pr
1b491 6f 67 72 65 73 73 22 2c 0a 20 20 20 20 20 20 20  ogress",.       
1b492 20 28 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f   (p1==SAVEPOINT_
1b493 52 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c  ROLLBACK ? "roll
1b494 62 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22  back": "release"
1b495 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20  ).      );.     
1b496 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
1b497 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20  Y;.    }else{.. 
1b498 20 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e       /* Determin
1b499 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
1b49a 20 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73   this is a trans
1b49b 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74  action savepoint
1b49c 2e 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a  . If so,.      *
1b49d 2a 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20  * and this is a 
1b49e 52 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c  RELEASE command,
1b49f 20 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e   then the curren
1b4a0 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20  t transaction . 
1b4a1 20 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69       ** is commi
1b4a2 74 74 65 64 2e 20 0a 20 20 20 20 20 20 2a 2f 0a  tted. .      */.
1b4a3 20 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e        int isTran
1b4a4 73 61 63 74 69 6f 6e 20 3d 20 70 53 61 76 65 70  saction = pSavep
1b4a5 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 20 26  oint->pNext==0 &
1b4a6 26 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74  & db->isTransact
1b4a7 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20  ionSavepoint;.  
1b4a8 20 20 20 20 69 66 28 20 69 73 54 72 61 6e 73 61      if( isTransa
1b4a9 63 74 69 6f 6e 20 26 26 20 70 31 3d 3d 53 41 56  ction && p1==SAV
1b4aa 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29  EPOINT_RELEASE )
1b4ab 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75  {.        db->au
1b4ac 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20  toCommit = 1;.  
1b4ad 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1b4ae 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51  3VdbeHalt(p)==SQ
1b4af 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20  LITE_BUSY ){.   
1b4b0 20 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70         p->pc = p
1b4b1 63 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  c;.          db-
1b4b2 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b  >autoCommit = 0;
1b4b3 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63  .          p->rc
1b4b4 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42   = rc = SQLITE_B
1b4b5 55 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 67  USY;.          g
1b4b6 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b  oto vdbe_return;
1b4b7 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1b4b8 20 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63     db->isTransac
1b4b9 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20  tionSavepoint = 
1b4ba 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  0;.        rc = 
1b4bb 70 2d 3e 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c  p->rc;.      }el
1b4bc 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  se{.        int 
1b4bd 69 69 3b 0a 20 20 20 20 20 20 20 20 69 53 61 76  ii;.        iSav
1b4be 65 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61  epoint = db->nSa
1b4bf 76 65 70 6f 69 6e 74 20 2d 20 69 53 61 76 65 70  vepoint - iSavep
1b4c0 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 20  oint - 1;.      
1b4c1 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 64    for(ii=0; ii<d
1b4c2 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20  b->nDb; ii++){. 
1b4c3 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
1b4c4 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f  lite3BtreeSavepo
1b4c5 69 6e 74 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e  int(db->aDb[ii].
1b4c6 70 42 74 2c 20 70 31 2c 20 69 53 61 76 65 70 6f  pBt, p1, iSavepo
1b4c7 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  int);.          
1b4c8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1b4c9 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
1b4ca 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1b4cb 74 6f 5f 65 72 72 6f 72 3b 0a 09 20 20 7d 0a 20  to_error;..  }. 
1b4cc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1b4cd 20 69 66 28 20 70 31 3d 3d 53 41 56 45 50 4f 49   if( p1==SAVEPOI
1b4ce 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 28  NT_ROLLBACK && (
1b4cf 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
1b4d0 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 21  _InternChanges)!
1b4d1 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1b4d2 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
1b4d3 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
1b4d4 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  db);.          s
1b4d5 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
1b4d6 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
1b4d7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1b4d8 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20    }.  .      /* 
1b4d9 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  Regardless of wh
1b4da 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20  ether this is a 
1b4db 52 45 4c 45 41 53 45 20 6f 72 20 52 4f 4c 4c 42  RELEASE or ROLLB
1b4dc 41 43 4b 2c 20 64 65 73 74 72 6f 79 20 61 6c 6c  ACK, destroy all
1b4dd 20 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 70   .      ** savep
1b4de 6f 69 6e 74 73 20 6e 65 73 74 65 64 20 69 6e 73  oints nested ins
1b4df 69 64 65 20 6f 66 20 74 68 65 20 73 61 76 65 70  ide of the savep
1b4e0 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61  oint being opera
1b4e1 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20  ted on. */.     
1b4e2 20 77 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76   while( db->pSav
1b4e3 65 70 6f 69 6e 74 21 3d 70 53 61 76 65 70 6f 69  epoint!=pSavepoi
1b4e4 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 53 61  nt ){.        Sa
1b4e5 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 20 3d 20  vepoint *pTmp = 
1b4e6 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a  db->pSavepoint;.
1b4e7 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76          db->pSav
1b4e8 65 70 6f 69 6e 74 20 3d 20 70 54 6d 70 2d 3e 70  epoint = pTmp->p
1b4e9 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71  Next;.        sq
1b4ea 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1b4eb 70 54 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 64  pTmp);.        d
1b4ec 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b  b->nSavepoint--;
1b4ed 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1b4ee 2f 2a 20 49 66 20 69 74 20 69 73 20 61 20 52 45  /* If it is a RE
1b4ef 4c 45 41 53 45 2c 20 74 68 65 6e 20 64 65 73 74  LEASE, then dest
1b4f0 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f 69 6e  roy the savepoin
1b4f1 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64  t being operated
1b4f2 20 6f 6e 20 74 6f 6f 20 2a 2f 0a 20 20 20 20 20   on too */.     
1b4f3 20 69 66 28 20 70 31 3d 3d 53 41 56 45 50 4f 49   if( p1==SAVEPOI
1b4f4 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20  NT_RELEASE ){.  
1b4f5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
1b4f6 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d 3e 70 53  avepoint==db->pS
1b4f7 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 20 20  avepoint );.    
1b4f8 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69      db->pSavepoi
1b4f9 6e 74 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d  nt = pSavepoint-
1b4fa 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
1b4fb 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1b4fc 2c 20 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20  , pSavepoint);. 
1b4fd 20 20 20 20 20 20 20 69 66 28 20 21 69 73 54 72         if( !isTr
1b4fe 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20  ansaction ){.   
1b4ff 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65         db->nSave
1b500 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20  point--;.       
1b501 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1b502 0a 20 20 7d 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d  .  }..  break;.}
1b503 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 75 74  ../* Opcode: Aut
1b504 6f 43 6f 6d 6d 69 74 20 50 31 20 50 32 20 2a 20  oCommit P1 P2 * 
1b505 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68  * *.**.** Set th
1b506 65 20 64 61 74 61 62 61 73 65 20 61 75 74 6f 2d  e database auto-
1b507 63 6f 6d 6d 69 74 20 66 6c 61 67 20 74 6f 20 50  commit flag to P
1b508 31 20 28 31 20 6f 72 20 30 29 2e 20 49 66 20 50  1 (1 or 0). If P
1b509 32 20 69 73 20 74 72 75 65 2c 20 72 6f 6c 6c 0a  2 is true, roll.
1b50a 2a 2a 20 62 61 63 6b 20 61 6e 79 20 63 75 72 72  ** back any curr
1b50b 65 6e 74 6c 79 20 61 63 74 69 76 65 20 62 74 72  ently active btr
1b50c 65 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e  ee transactions.
1b50d 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   If there are an
1b50e 79 20 61 63 74 69 76 65 0a 2a 2a 20 56 4d 73 20  y active.** VMs 
1b50f 28 61 70 61 72 74 20 66 72 6f 6d 20 74 68 69 73  (apart from this
1b510 20 6f 6e 65 29 2c 20 74 68 65 6e 20 74 68 65 20   one), then the 
1b511 43 4f 4d 4d 49 54 20 6f 72 20 52 4f 4c 4c 42 41  COMMIT or ROLLBA
1b512 43 4b 20 73 74 61 74 65 6d 65 6e 74 20 66 61 69  CK statement fai
1b513 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ls..**.** This i
1b514 6e 73 74 72 75 63 74 69 6f 6e 20 63 61 75 73 65  nstruction cause
1b515 73 20 74 68 65 20 56 4d 20 74 6f 20 68 61 6c 74  s the VM to halt
1b516 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 75 74  ..*/.case OP_Aut
1b517 6f 43 6f 6d 6d 69 74 3a 20 7b 0a 20 20 69 6e 74  oCommit: {.  int
1b518 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d   desiredAutoComm
1b519 69 74 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  it = pOp->p1;.  
1b51a 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 3d 20 70  int rollback = p
1b51b 4f 70 2d 3e 70 32 3b 0a 20 20 69 6e 74 20 74 75  Op->p2;.  int tu
1b51c 72 6e 4f 6e 41 43 20 3d 20 64 65 73 69 72 65 64  rnOnAC = desired
1b51d 41 75 74 6f 43 6f 6d 6d 69 74 20 26 26 20 21 64  AutoCommit && !d
1b51e 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 0a  b->autoCommit;..
1b51f 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65    assert( desire
1b520 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c  dAutoCommit==1 |
1b521 7c 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  | desiredAutoCom
1b522 6d 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  mit==0 );.  asse
1b523 72 74 28 20 64 65 73 69 72 65 64 41 75 74 6f 43  rt( desiredAutoC
1b524 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 72 6f 6c 6c  ommit==1 || roll
1b525 62 61 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 61 73  back==0 );..  as
1b526 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65  sert( db->active
1b527 56 64 62 65 43 6e 74 3e 30 20 29 3b 20 20 2f 2a  VdbeCnt>0 );  /*
1b528 20 41 74 20 6c 65 61 73 74 20 74 68 69 73 20 6f   At least this o
1b529 6e 65 20 56 4d 20 69 73 20 61 63 74 69 76 65 20  ne VM is active 
1b52a 2a 2f 0a 0a 20 20 69 66 28 20 74 75 72 6e 4f 6e  */..  if( turnOn
1b52b 41 43 20 26 26 20 72 6f 6c 6c 62 61 63 6b 20 26  AC && rollback &
1b52c 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  & db->activeVdbe
1b52d 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20  Cnt>1 ){.    /* 
1b52e 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  If this instruct
1b52f 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ion implements a
1b530 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 6f 74   ROLLBACK and ot
1b531 68 65 72 20 56 4d 73 20 61 72 65 0a 20 20 20 20  her VMs are.    
1b532 2a 2a 20 73 74 69 6c 6c 20 72 75 6e 6e 69 6e 67  ** still running
1b533 2c 20 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74  , and a transact
1b534 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20 72  ion is active, r
1b535 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69  eturn an error i
1b536 6e 64 69 63 61 74 69 6e 67 0a 20 20 20 20 2a 2a  ndicating.    **
1b537 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20   that the other 
1b538 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74  VMs must complet
1b539 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20 2a 2f  e first. .    */
1b53a 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1b53b 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1b53c 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 72  g, db, "cannot r
1b53d 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74  ollback transact
1b53e 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20  ion - ".        
1b53f 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20  "SQL statements 
1b540 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20  in progress");. 
1b541 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
1b542 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  USY;.  }else if(
1b543 20 74 75 72 6e 4f 6e 41 43 20 26 26 20 21 72 6f   turnOnAC && !ro
1b544 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72  llback && db->wr
1b545 69 74 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a  iteVdbeCnt>1 ){.
1b546 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
1b547 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65  nstruction imple
1b548 6d 65 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61  ments a COMMIT a
1b549 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65  nd other VMs are
1b54a 20 77 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20   writing.    ** 
1b54b 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
1b54c 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20  indicating that 
1b54d 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75  the other VMs mu
1b54e 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73  st complete firs
1b54f 74 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  t. .    */.    s
1b550 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1b551 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1b552 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20   "cannot commit 
1b553 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a  transaction - ".
1b554 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61          "SQL sta
1b555 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72  tements in progr
1b556 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20  ess");.    rc = 
1b557 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
1b558 65 6c 73 65 20 69 66 28 20 64 65 73 69 72 65 64  else if( desired
1b559 41 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d 3e  AutoCommit!=db->
1b55a 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20  autoCommit ){.  
1b55b 20 20 69 66 28 20 72 6f 6c 6c 62 61 63 6b 20 29    if( rollback )
1b55c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1b55d 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
1b55e 74 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71  t==1 );.      sq
1b55f 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c  lite3RollbackAll
1b560 28 64 62 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  (db);.      db->
1b561 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a  autoCommit = 1;.
1b562 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1b563 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
1b564 3d 20 28 75 38 29 64 65 73 69 72 65 64 41 75 74  = (u8)desiredAut
1b565 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 69  oCommit;.      i
1b566 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48 61  f( sqlite3VdbeHa
1b567 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55  lt(p)==SQLITE_BU
1b568 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  SY ){.        p-
1b569 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20  >pc = pc;.      
1b56a 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74    db->autoCommit
1b56b 20 3d 20 28 75 38 29 28 31 2d 64 65 73 69 72 65   = (u8)(1-desire
1b56c 64 41 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20  dAutoCommit);.  
1b56d 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63        p->rc = rc
1b56e 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
1b56f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62          goto vdb
1b570 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20  e_return;.      
1b571 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
1b572 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e  te3CloseSavepoin
1b573 74 73 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20  ts(db);.    if( 
1b574 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  p->rc==SQLITE_OK
1b575 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
1b576 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
1b577 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
1b578 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1b579 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76      }.    goto v
1b57a 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65  dbe_return;.  }e
1b57b 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
1b57c 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1b57d 72 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20  rrMsg, db,.     
1b57e 20 20 20 28 21 64 65 73 69 72 65 64 41 75 74 6f     (!desiredAuto
1b57f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20  Commit)?"cannot 
1b580 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74  start a transact
1b581 69 6f 6e 20 77 69 74 68 69 6e 20 61 20 74 72 61  ion within a tra
1b582 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20  nsaction":(.    
1b583 20 20 20 20 28 72 6f 6c 6c 62 61 63 6b 29 3f 22      (rollback)?"
1b584 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20  cannot rollback 
1b585 2d 20 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e  - no transaction
1b586 20 69 73 20 61 63 74 69 76 65 22 3a 0a 20 20 20   is active":.   
1b587 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b588 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 2d  "cannot commit -
1b589 20 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   no transaction 
1b58a 69 73 20 61 63 74 69 76 65 22 29 29 3b 0a 20 20  is active"));.  
1b58b 20 20 20 20 20 20 20 0a 20 20 20 20 72 63 20 3d         .    rc =
1b58c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1b58d 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1b58e 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61  * Opcode: Transa
1b58f 63 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20  ction P1 P2 * * 
1b590 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20  *.**.** Begin a 
1b591 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68  transaction.  Th
1b592 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e  e transaction en
1b593 64 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74  ds when a Commit
1b594 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20   or Rollback.** 
1b595 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e  opcode is encoun
1b596 74 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e  tered.  Dependin
1b597 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46  g on the ON CONF
1b598 4c 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68  LICT setting, th
1b599 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  e.** transaction
1b59a 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72   might also be r
1b59b 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e  olled back if an
1b59c 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
1b59d 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20  tered..**.** P1 
1b59e 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
1b59f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1b5a0 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74  e on which the t
1b5a1 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  ransaction is.**
1b5a2 20 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78   started.  Index
1b5a3 20 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64   0 is the main d
1b5a4 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1b5a5 20 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a   index 1 is the.
1b5a6 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72  ** file used for
1b5a7 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1b5a8 73 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32  s.  Indices of 2
1b5a9 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65   or more are use
1b5aa 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65  d for.** attache
1b5ab 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a  d databases..**.
1b5ac 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d  ** If P2 is non-
1b5ad 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69  zero, then a wri
1b5ae 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te-transaction i
1b5af 73 20 73 74 61 72 74 65 64 2e 20 20 41 20 52 45  s started.  A RE
1b5b0 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a  SERVED lock is.*
1b5b1 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68  * obtained on th
1b5b2 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1b5b3 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61  when a write-tra
1b5b4 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72  nsaction is star
1b5b5 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65  ted.  No.** othe
1b5b6 72 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74  r process can st
1b5b7 61 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74  art another writ
1b5b8 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68  e transaction wh
1b5b9 69 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61 63  ile this transac
1b5ba 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72  tion is.** under
1b5bb 77 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61  way.  Starting a
1b5bc 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1b5bd 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20  on also creates 
1b5be 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
1b5bf 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74  al. A.** write t
1b5c0 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20  ransaction must 
1b5c1 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72  be started befor
1b5c2 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61  e any changes ca
1b5c3 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65  n be made to the
1b5c4 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49  .** database.  I
1b5c5 66 20 50 32 20 69 73 20 32 20 6f 72 20 67 72 65  f P2 is 2 or gre
1b5c6 61 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43  ater then an EXC
1b5c7 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61  LUSIVE lock is a
1b5c8 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20  lso obtained.** 
1b5c9 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a  on the file..**.
1b5ca 2a 2a 20 49 66 20 50 32 20 69 73 20 7a 65 72 6f  ** If P2 is zero
1b5cb 2c 20 74 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f  , then a read-lo
1b5cc 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f  ck is obtained o
1b5cd 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
1b5ce 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ile..*/.case OP_
1b5cf 54 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 20  Transaction: {. 
1b5d0 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31   int i = pOp->p1
1b5d1 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a  ;.  Btree *pBt;.
1b5d2 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1b5d3 26 26 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  && i<db->nDb );.
1b5d4 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1b5d5 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29  reeMask & (1<<i)
1b5d6 29 21 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20  )!=0 );.  pBt = 
1b5d7 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1b5d8 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20  .  if( pBt ){.  
1b5d9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1b5da 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 42  reeBeginTrans(pB
1b5db 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20  t, pOp->p2);.   
1b5dc 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1b5dd 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 70 2d  BUSY ){.      p-
1b5de 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20  >pc = pc;.      
1b5df 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c  p->rc = rc = SQL
1b5e0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
1b5e1 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1b5e2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1b5e3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc!=SQLITE_OK &&
1b5e4 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 45 41 44   rc!=SQLITE_READ
1b5e5 4f 4e 4c 59 20 2f 2a 20 26 26 20 72 63 21 3d 53  ONLY /* && rc!=S
1b5e6 51 4c 49 54 45 5f 42 55 53 59 20 2a 2f 20 29 7b  QLITE_BUSY */ ){
1b5e7 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
1b5e8 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1b5e9 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
1b5ea 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1b5eb 20 52 65 61 64 43 6f 6f 6b 69 65 20 50 31 20 50   ReadCookie P1 P
1b5ec 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52  2 P3 * *.**.** R
1b5ed 65 61 64 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65  ead cookie numbe
1b5ee 72 20 50 33 20 66 72 6f 6d 20 64 61 74 61 62 61  r P3 from databa
1b5ef 73 65 20 50 31 20 61 6e 64 20 77 72 69 74 65 20  se P1 and write 
1b5f0 69 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  it into register
1b5f1 20 50 32 2e 0a 2a 2a 20 50 33 3d 3d 30 20 69 73   P2..** P3==0 is
1b5f2 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73   the schema vers
1b5f3 69 6f 6e 2e 20 20 50 33 3d 3d 31 20 69 73 20 74  ion.  P3==1 is t
1b5f4 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d  he database form
1b5f5 61 74 2e 0a 2a 2a 20 50 33 3d 3d 32 20 69 73 20  at..** P3==2 is 
1b5f6 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20  the recommended 
1b5f7 70 61 67 65 72 20 63 61 63 68 65 20 73 69 7a 65  pager cache size
1b5f8 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20  , and so forth. 
1b5f9 20 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65   P1==0 is.** the
1b5fa 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
1b5fb 69 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73  ile and P1==1 is
1b5fc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1b5fd 6c 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  le used to store
1b5fe 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61  .** temporary ta
1b5ff 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  bles..**.** If P
1b600 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74  1 is negative, t
1b601 68 65 6e 20 74 68 69 73 20 69 73 20 61 20 72 65  hen this is a re
1b602 71 75 65 73 74 20 74 6f 20 72 65 61 64 20 74 68  quest to read th
1b603 65 20 73 69 7a 65 20 6f 66 20 61 0a 2a 2a 20 64  e size of a.** d
1b604 61 74 61 62 61 73 65 73 20 66 72 65 65 2d 6c 69  atabases free-li
1b605 73 74 2e 20 50 33 20 6d 75 73 74 20 62 65 20 73  st. P3 must be s
1b606 65 74 20 74 6f 20 31 20 69 6e 20 74 68 69 73 20  et to 1 in this 
1b607 63 61 73 65 2e 20 54 68 65 20 61 63 74 75 61 6c  case. The actual
1b608 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 63  .** database acc
1b609 65 73 73 65 64 20 69 73 20 28 28 50 31 2b 31 29  essed is ((P1+1)
1b60a 2a 2d 31 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c  *-1). For exampl
1b60b 65 2c 20 61 20 50 31 20 70 61 72 61 6d 65 74 65  e, a P1 paramete
1b60c 72 20 6f 66 20 2d 31 0a 2a 2a 20 63 6f 72 72 65  r of -1.** corre
1b60d 73 70 6f 6e 64 73 20 74 6f 20 64 61 74 61 62 61  sponds to databa
1b60e 73 65 20 30 20 28 22 6d 61 69 6e 22 29 2c 20 61  se 0 ("main"), a
1b60f 20 50 31 20 6f 66 20 2d 32 20 69 73 20 64 61 74   P1 of -2 is dat
1b610 61 62 61 73 65 20 31 20 28 22 74 65 6d 70 22 29  abase 1 ("temp")
1b611 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75  ..**.** There mu
1b612 73 74 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63  st be a read-loc
1b613 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
1b614 65 20 28 65 69 74 68 65 72 20 61 20 74 72 61 6e  e (either a tran
1b615 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20  saction.** must 
1b616 62 65 20 73 74 61 72 74 65 64 20 6f 72 20 74 68  be started or th
1b617 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f  ere must be an o
1b618 70 65 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f  pen cursor) befo
1b619 72 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20  re.** executing 
1b61a 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
1b61b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61  ..*/.case OP_Rea
1b61c 64 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20  dCookie: {      
1b61d 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1b61e 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20  -prerelease */. 
1b61f 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 69 6e   int iMeta;.  in
1b620 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b  t iDb = pOp->p1;
1b621 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 20 3d  .  int iCookie =
1b622 20 70 4f 70 2d 3e 70 33 3b 0a 0a 20 20 61 73 73   pOp->p3;..  ass
1b623 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 53 51 4c  ert( pOp->p3<SQL
1b624 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41  ITE_N_BTREE_META
1b625 20 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20   );.  if( iDb<0 
1b626 29 7b 0a 20 20 20 20 69 44 62 20 3d 20 28 2d 31  ){.    iDb = (-1
1b627 2a 28 69 44 62 2b 31 29 29 3b 0a 20 20 20 20 69  *(iDb+1));.    i
1b628 43 6f 6f 6b 69 65 20 2a 3d 20 2d 31 3b 0a 20 20  Cookie *= -1;.  
1b629 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  }.  assert( iDb>
1b62a 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
1b62b 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  b );.  assert( d
1b62c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 21  b->aDb[iDb].pBt!
1b62d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1b62e 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
1b62f 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a  (1<<iDb))!=0 );.
1b630 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 69 6e    /* The indexin
1b631 67 20 6f 66 20 6d 65 74 61 20 76 61 6c 75 65 73  g of meta values
1b632 20 61 74 20 74 68 65 20 73 63 68 65 6d 61 20 6c   at the schema l
1b633 61 79 65 72 20 69 73 20 6f 66 66 20 62 79 20 6f  ayer is off by o
1b634 6e 65 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65  ne from.  ** the
1b635 20 69 6e 64 65 78 69 6e 67 20 69 6e 20 74 68 65   indexing in the
1b636 20 62 74 72 65 65 20 6c 61 79 65 72 2e 20 20 54   btree layer.  T
1b637 68 65 20 62 74 72 65 65 20 63 6f 6e 73 69 64 65  he btree conside
1b638 72 73 20 6d 65 74 61 5b 30 5d 20 74 6f 0a 20 20  rs meta[0] to.  
1b639 2a 2a 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72  ** be the number
1b63a 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 69   of free pages i
1b63b 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28  n the database (
1b63c 61 20 72 65 61 64 2d 6f 6e 6c 79 20 76 61 6c 75  a read-only valu
1b63d 65 29 0a 20 20 2a 2a 20 61 6e 64 20 6d 65 74 61  e).  ** and meta
1b63e 5b 31 5d 20 74 6f 20 62 65 20 74 68 65 20 73 63  [1] to be the sc
1b63f 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 54 68  hema cookie.  Th
1b640 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 63  e schema layer c
1b641 6f 6e 73 69 64 65 72 73 0a 20 20 2a 2a 20 6d 65  onsiders.  ** me
1b642 74 61 5b 31 5d 20 74 6f 20 62 65 20 74 68 65 20  ta[1] to be the 
1b643 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20  schema cookie.  
1b644 53 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73 68  So we have to sh
1b645 69 66 74 20 74 68 65 20 69 6e 64 65 78 0a 20 20  ift the index.  
1b646 2a 2a 20 62 79 20 6f 6e 65 20 69 6e 20 74 68 65  ** by one in the
1b647 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65   following state
1b648 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  ment..  */.  rc 
1b649 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
1b64a 74 4d 65 74 61 28 64 62 2d 3e 61 44 62 5b 69 44  tMeta(db->aDb[iD
1b64b 62 5d 2e 70 42 74 2c 20 31 20 2b 20 69 43 6f 6f  b].pBt, 1 + iCoo
1b64c 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 69 4d 65  kie, (u32 *)&iMe
1b64d 74 61 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69  ta);.  pOut->u.i
1b64e 20 3d 20 69 4d 65 74 61 3b 0a 20 20 4d 65 6d 53   = iMeta;.  MemS
1b64f 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1b650 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65   MEM_Int);.  bre
1b651 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1b652 3a 20 53 65 74 43 6f 6f 6b 69 65 20 50 31 20 50  : SetCookie P1 P
1b653 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57  2 P3 * *.**.** W
1b654 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  rite the content
1b655 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20   of register P3 
1b656 28 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20  (interpreted as 
1b657 61 6e 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20 69  an integer).** i
1b658 6e 74 6f 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65  nto cookie numbe
1b659 72 20 50 32 20 6f 66 20 64 61 74 61 62 61 73 65  r P2 of database
1b65a 20 50 31 2e 0a 2a 2a 20 50 32 3d 3d 30 20 69 73   P1..** P2==0 is
1b65b 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73   the schema vers
1b65c 69 6f 6e 2e 20 20 50 32 3d 3d 31 20 69 73 20 74  ion.  P2==1 is t
1b65d 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d  he database form
1b65e 61 74 2e 0a 2a 2a 20 50 32 3d 3d 32 20 69 73 20  at..** P2==2 is 
1b65f 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20  the recommended 
1b660 70 61 67 65 72 20 63 61 63 68 65 20 73 69 7a 65  pager cache size
1b661 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20  , and so forth. 
1b662 20 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65   P1==0 is.** the
1b663 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
1b664 69 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73  ile and P1==1 is
1b665 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1b666 6c 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  le used to store
1b667 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61  .** temporary ta
1b668 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72  bles..**.** A tr
1b669 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
1b66a 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65  e started before
1b66b 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20   executing this 
1b66c 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20  opcode..*/.case 
1b66d 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 3a 20 7b 20  OP_SetCookie: { 
1b66e 20 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a        /* in3 */.
1b66f 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 61 73 73    Db *pDb;.  ass
1b670 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 53 51 4c  ert( pOp->p2<SQL
1b671 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41  ITE_N_BTREE_META
1b672 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1b673 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1b674 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  >p1<db->nDb );. 
1b675 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
1b676 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70  eeMask & (1<<pOp
1b677 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 70  ->p1))!=0 );.  p
1b678 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f  Db = &db->aDb[pO
1b679 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1b67a 28 20 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b  ( pDb->pBt!=0 );
1b67b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1b67c 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33  mIntegerify(pIn3
1b67d 29 3b 0a 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65  );.  /* See note
1b67e 20 61 62 6f 75 74 20 69 6e 64 65 78 20 73 68 69   about index shi
1b67f 66 74 69 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64  fting on OP_Read
1b680 43 6f 6f 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d  Cookie */.  rc =
1b681 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
1b682 61 74 65 4d 65 74 61 28 70 44 62 2d 3e 70 42 74  ateMeta(pDb->pBt
1b683 2c 20 31 2b 70 4f 70 2d 3e 70 32 2c 20 28 69 6e  , 1+pOp->p2, (in
1b684 74 29 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20  t)pIn3->u.i);.  
1b685 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29  if( pOp->p2==0 )
1b686 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68  {.    /* When th
1b687 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
1b688 63 68 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20  changes, record 
1b689 74 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69  the new cookie i
1b68a 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20  nternally */.   
1b68b 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73   pDb->pSchema->s
1b68c 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 28  chema_cookie = (
1b68d 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20  int)pIn3->u.i;. 
1b68e 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20     db->flags |= 
1b68f 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
1b690 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66  nges;.  }else if
1b691 28 20 70 4f 70 2d 3e 70 32 3d 3d 31 20 29 7b 0a  ( pOp->p2==1 ){.
1b692 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63 68      /* Record ch
1b693 61 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c  anges in the fil
1b694 65 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20  e format */.    
1b695 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
1b696 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29  le_format = (u8)
1b697 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20  pIn3->u.i;.  }. 
1b698 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20   if( pOp->p1==1 
1b699 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69  ){.    /* Invali
1b69a 64 61 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65  date all prepare
1b69b 64 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65  d statements whe
1b69c 6e 65 76 65 72 20 74 68 65 20 54 45 4d 50 20 64  never the TEMP d
1b69d 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 73  atabase.    ** s
1b69e 63 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64  chema is changed
1b69f 2e 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20  .  Ticket #1644 
1b6a0 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  */.    sqlite3Ex
1b6a1 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
1b6a2 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 0a  ements(db);.  }.
1b6a3 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1b6a4 70 63 6f 64 65 3a 20 56 65 72 69 66 79 43 6f 6f  pcode: VerifyCoo
1b6a5 6b 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a  kie P1 P2 *.**.*
1b6a6 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75  * Check the valu
1b6a7 65 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61 74 61  e of global data
1b6a8 62 61 73 65 20 70 61 72 61 6d 65 74 65 72 20 6e  base parameter n
1b6a9 75 6d 62 65 72 20 30 20 28 74 68 65 0a 2a 2a 20  umber 0 (the.** 
1b6aa 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 29 20  schema version) 
1b6ab 61 6e 64 20 6d 61 6b 65 20 73 75 72 65 20 69 74  and make sure it
1b6ac 20 69 73 20 65 71 75 61 6c 20 74 6f 20 50 32 2e   is equal to P2.
1b6ad 20 20 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20    .** P1 is the 
1b6ae 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20  database number 
1b6af 77 68 69 63 68 20 69 73 20 30 20 66 6f 72 20 74  which is 0 for t
1b6b0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1b6b1 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66   file.** and 1 f
1b6b2 6f 72 20 74 68 65 20 66 69 6c 65 20 68 6f 6c 64  or the file hold
1b6b3 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ing temporary ta
1b6b4 62 6c 65 73 20 61 6e 64 20 73 6f 6d 65 20 68 69  bles and some hi
1b6b5 67 68 65 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66  gher number.** f
1b6b6 6f 72 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  or auxiliary dat
1b6b7 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  abases..**.** Th
1b6b8 65 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73  e cookie changes
1b6b9 20 69 74 73 20 76 61 6c 75 65 20 77 68 65 6e 65   its value whene
1b6ba 76 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65  ver the database
1b6bb 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e   schema changes.
1b6bc 0a 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69  .** This operati
1b6bd 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64 65  on is used to de
1b6be 74 65 63 74 20 77 68 65 6e 20 74 68 61 74 20 74  tect when that t
1b6bf 68 65 20 63 6f 6f 6b 69 65 20 68 61 73 20 63 68  he cookie has ch
1b6c0 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61  anged.** and tha
1b6c1 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72  t the current pr
1b6c2 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 72  ocess needs to r
1b6c3 65 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61  eread the schema
1b6c4 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61  ..**.** Either a
1b6c5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65   transaction nee
1b6c6 64 73 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20  ds to have been 
1b6c7 73 74 61 72 74 65 64 20 6f 72 20 61 6e 20 4f 50  started or an OP
1b6c8 5f 4f 70 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74  _Open needs.** t
1b6c9 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 28 74  o be executed (t
1b6ca 6f 20 65 73 74 61 62 6c 69 73 68 20 61 20 72 65  o establish a re
1b6cb 61 64 20 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20  ad lock) before 
1b6cc 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a  this opcode is.*
1b6cd 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61  * invoked..*/.ca
1b6ce 73 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b  se OP_VerifyCook
1b6cf 69 65 3a 20 7b 0a 20 20 69 6e 74 20 69 4d 65 74  ie: {.  int iMet
1b6d0 61 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b  a;.  Btree *pBt;
1b6d1 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1b6d2 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1b6d3 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73  <db->nDb );.  as
1b6d4 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d  sert( (p->btreeM
1b6d5 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70  ask & (1<<pOp->p
1b6d6 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 42 74 20  1))!=0 );.  pBt 
1b6d7 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70  = db->aDb[pOp->p
1b6d8 31 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20 70 42  1].pBt;.  if( pB
1b6d9 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  t ){.    rc = sq
1b6da 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
1b6db 61 28 70 42 74 2c 20 31 2c 20 28 75 33 32 20 2a  a(pBt, 1, (u32 *
1b6dc 29 26 69 4d 65 74 61 29 3b 0a 20 20 7d 65 6c 73  )&iMeta);.  }els
1b6dd 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  e{.    rc = SQLI
1b6de 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 4d 65 74 61  TE_OK;.    iMeta
1b6df 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
1b6e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1b6e1 20 69 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20   iMeta!=pOp->p2 
1b6e2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
1b6e3 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72  Free(db, p->zErr
1b6e4 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72  Msg);.    p->zEr
1b6e5 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62  rMsg = sqlite3Db
1b6e6 53 74 72 44 75 70 28 64 62 2c 20 22 64 61 74 61  StrDup(db, "data
1b6e7 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20  base schema has 
1b6e8 63 68 61 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f  changed");.    /
1b6e9 2a 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 2d  * If the schema-
1b6ea 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20  cookie from the 
1b6eb 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61  database file ma
1b6ec 74 63 68 65 73 20 74 68 65 20 63 6f 6f 6b 69 65  tches the cookie
1b6ed 20 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20   .    ** stored 
1b6ee 77 69 74 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  with the in-memo
1b6ef 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ry representatio
1b6f0 6e 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2c  n of the schema,
1b6f1 20 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72   do.    ** not r
1b6f2 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61  eload the schema
1b6f3 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
1b6f4 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a  se file..    **.
1b6f5 20 20 20 20 2a 2a 20 49 66 20 76 69 72 74 75 61      ** If virtua
1b6f6 6c 2d 74 61 62 6c 65 73 20 61 72 65 20 69 6e 20  l-tables are in 
1b6f7 75 73 65 2c 20 74 68 69 73 20 69 73 20 6e 6f 74  use, this is not
1b6f8 20 6a 75 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a   just an optimiz
1b6f9 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66  ation..    ** Of
1b6fa 74 65 6e 2c 20 76 2d 74 61 62 6c 65 73 20 73 74  ten, v-tables st
1b6fb 6f 72 65 20 74 68 65 69 72 20 64 61 74 61 20 69  ore their data i
1b6fc 6e 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 74  n other SQLite t
1b6fd 61 62 6c 65 73 2c 20 77 68 69 63 68 0a 20 20 20  ables, which.   
1b6fe 20 2a 2a 20 61 72 65 20 71 75 65 72 69 65 64 20   ** are queried 
1b6ff 66 72 6f 6d 20 77 69 74 68 69 6e 20 78 4e 65 78  from within xNex
1b700 74 28 29 20 61 6e 64 20 6f 74 68 65 72 20 76 2d  t() and other v-
1b701 74 61 62 6c 65 20 6d 65 74 68 6f 64 73 20 75 73  table methods us
1b702 69 6e 67 0a 20 20 20 20 2a 2a 20 70 72 65 70 61  ing.    ** prepa
1b703 72 65 64 20 71 75 65 72 69 65 73 2e 20 49 66 20  red queries. If 
1b704 73 75 63 68 20 61 20 71 75 65 72 79 20 69 73 20  such a query is 
1b705 6f 75 74 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20  out-of-date, we 
1b706 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20  do not want to. 
1b707 20 20 20 2a 2a 20 64 69 73 63 61 72 64 20 74 68     ** discard th
1b708 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
1b709 61 2c 20 61 73 20 74 68 65 20 75 73 65 72 20 63  a, as the user c
1b70a 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  ode implementing
1b70b 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61   the.    ** v-ta
1b70c 62 6c 65 20 77 6f 75 6c 64 20 68 61 76 65 20 74  ble would have t
1b70d 6f 20 62 65 20 72 65 61 64 79 20 66 6f 72 20 74  o be ready for t
1b70e 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  he sqlite3_vtab 
1b70f 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
1b710 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e  .    ** to be in
1b711 76 61 6c 69 64 61 74 65 64 20 77 68 65 6e 65 76  validated whenev
1b712 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  er sqlite3_step(
1b713 29 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d  ) is called from
1b714 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20   within .    ** 
1b715 61 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64  a v-table method
1b716 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1b717 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31   db->aDb[pOp->p1
1b718 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d  ].pSchema->schem
1b719 61 5f 63 6f 6f 6b 69 65 21 3d 69 4d 65 74 61 20  a_cookie!=iMeta 
1b71a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b71b 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
1b71c 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29  ema(db, pOp->p1)
1b71d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
1b71e 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
1b71f 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
1b720 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1b721 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20  E_SCHEMA;.  }.  
1b722 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1b723 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 31  ode: OpenRead P1
1b724 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
1b725 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f  ** Open a read-o
1b726 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20 74  nly cursor for t
1b727 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
1b728 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67  e whose root pag
1b729 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 20  e is.** P2 in a 
1b72a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
1b72b 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
1b72c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  e is determined 
1b72d 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30  by P3. .** P3==0
1b72e 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20   means the main 
1b72f 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 20  database, P3==1 
1b730 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61  means the databa
1b731 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a 20  se used for .** 
1b732 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1b733 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e 73  , and P3>1 means
1b734 20 75 73 65 64 20 74 68 65 20 63 6f 72 72 65 73   used the corres
1b735 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65 64  ponding attached
1b736 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 47  .** database.  G
1b737 69 76 65 20 74 68 65 20 6e 65 77 20 63 75 72 73  ive the new curs
1b738 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  or an identifier
1b739 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31 0a   of P1.  The P1.
1b73a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20 6e  ** values need n
1b73b 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 73  ot be contiguous
1b73c 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c 75   but all P1 valu
1b73d 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d 61  es should be sma
1b73e 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20  ll integers..** 
1b73f 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66  It is an error f
1b740 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67 61  or P1 to be nega
1b741 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  tive..**.** If P
1b742 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74 68  5!=0 then use th
1b743 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67  e content of reg
1b744 69 73 74 65 72 20 50 32 20 61 73 20 74 68 65 20  ister P2 as the 
1b745 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a 2a  root page, not.*
1b746 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50  * the value of P
1b747 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20  2 itself..**.** 
1b748 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61 20  There will be a 
1b749 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  read lock on the
1b74a 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 65 76   database whenev
1b74b 65 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a 2a  er there is an.*
1b74c 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 20  * open cursor.  
1b74d 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
1b74e 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 69  was unlocked pri
1b74f 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74 72  or to this instr
1b750 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61  uction.** then a
1b751 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 63   read lock is ac
1b752 71 75 69 72 65 64 20 61 73 20 70 61 72 74 20 6f  quired as part o
1b753 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  f this instructi
1b754 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 6c  on.  A read.** l
1b755 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72  ock allows other
1b756 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65   processes to re
1b757 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ad the database 
1b758 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a 2a  but prohibits.**
1b759 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
1b75a 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 6e  ss from modifyin
1b75b 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  g the database. 
1b75c 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 69   The read lock i
1b75d 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68  s.** released wh
1b75e 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61  en all cursors a
1b75f 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74  re closed.  If t
1b760 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1b761 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 67  attempts.** to g
1b762 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 62  et a read lock b
1b763 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73 63  ut fails, the sc
1b764 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73 20  ript terminates 
1b765 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54  with an.** SQLIT
1b766 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f 64  E_BUSY error cod
1b767 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20  e..**.** The P4 
1b768 76 61 6c 75 65 20 69 73 20 61 20 70 6f 69 6e 74  value is a point
1b769 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20  er to a KeyInfo 
1b76a 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64  structure that d
1b76b 65 66 69 6e 65 73 20 74 68 65 0a 2a 2a 20 63 6f  efines the.** co
1b76c 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74  ntent and collat
1b76d 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20  ing sequence of 
1b76e 69 6e 64 69 63 65 73 2e 20 20 50 34 20 69 73 20  indices.  P4 is 
1b76f 4e 55 4c 4c 20 66 6f 72 20 63 75 72 73 6f 72 73  NULL for cursors
1b770 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74  .** that are not
1b771 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 64   pointing to ind
1b772 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ices..**.** See 
1b773 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a  also OpenWrite..
1b774 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70  */./* Opcode: Op
1b775 65 6e 57 72 69 74 65 20 50 31 20 50 32 20 50 33  enWrite P1 P2 P3
1b776 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65   P4 P5.**.** Ope
1b777 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63  n a read/write c
1b778 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f  ursor named P1 o
1b779 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  n the table or i
1b77a 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a  ndex whose root.
1b77b 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e 20 20  ** page is P2.  
1b77c 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73 65 20  Or if P5!=0 use 
1b77d 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1b77e 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 66 69  egister P2 to fi
1b77f 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70  nd the.** root p
1b780 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50  age..**.** The P
1b781 34 20 76 61 6c 75 65 20 69 73 20 61 20 70 6f 69  4 value is a poi
1b782 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66  nter to a KeyInf
1b783 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
1b784 20 64 65 66 69 6e 65 73 20 74 68 65 0a 2a 2a 20   defines the.** 
1b785 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c  content and coll
1b786 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f  ating sequence o
1b787 66 20 69 6e 64 69 63 65 73 2e 20 20 50 34 20 69  f indices.  P4 i
1b788 73 20 4e 55 4c 4c 20 66 6f 72 20 63 75 72 73 6f  s NULL for curso
1b789 72 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 6e  rs.** that are n
1b78a 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69  ot pointing to i
1b78b 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ndices..**.** Th
1b78c 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77  is instruction w
1b78d 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f  orks just like O
1b78e 70 65 6e 52 65 61 64 20 65 78 63 65 70 74 20 74  penRead except t
1b78f 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68 65  hat it opens the
1b790 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65   cursor.** in re
1b791 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20 20  ad/write mode.  
1b792 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62 6c  For a given tabl
1b793 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65 20  e, there can be 
1b794 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64  one or more read
1b795 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73  -only.** cursors
1b796 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 61   or a single rea
1b797 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 62  d/write cursor b
1b798 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a  ut not both..**.
1b799 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e  ** See also Open
1b79a 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  Read..*/.case OP
1b79b 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65 20  _OpenRead:.case 
1b79c 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a  OP_OpenWrite: {.
1b79d 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
1b79e 31 3b 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 4f  1;.  int p2 = pO
1b79f 70 2d 3e 70 32 3b 0a 20 20 69 6e 74 20 69 44 62  p->p2;.  int iDb
1b7a0 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e   = pOp->p3;.  in
1b7a1 74 20 77 72 46 6c 61 67 3b 0a 20 20 42 74 72 65  t wrFlag;.  Btre
1b7a2 65 20 2a 70 58 3b 0a 20 20 56 64 62 65 43 75 72  e *pX;.  VdbeCur
1b7a3 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 44 62 20  sor *pCur;.  Db 
1b7a4 2a 70 44 62 3b 0a 20 20 0a 20 20 61 73 73 65 72  *pDb;.  .  asser
1b7a5 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
1b7a6 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73  <db->nDb );.  as
1b7a7 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d  sert( (p->btreeM
1b7a8 61 73 6b 20 26 20 28 31 3c 3c 69 44 62 29 29 21  ask & (1<<iDb))!
1b7a9 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64  =0 );.  pDb = &d
1b7aa 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 70  b->aDb[iDb];.  p
1b7ab 58 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20  X = pDb->pBt;.  
1b7ac 61 73 73 65 72 74 28 20 70 58 21 3d 30 20 29 3b  assert( pX!=0 );
1b7ad 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  .  if( pOp->opco
1b7ae 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65  de==OP_OpenWrite
1b7af 20 29 7b 0a 20 20 20 20 77 72 46 6c 61 67 20 3d   ){.    wrFlag =
1b7b0 20 31 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d   1;.    if( pDb-
1b7b1 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
1b7b2 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e 57 72  ormat < p->minWr
1b7b3 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 29 7b  iteFileFormat ){
1b7b4 0a 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57 72 69  .      p->minWri
1b7b5 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 70  teFileFormat = p
1b7b6 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c  Db->pSchema->fil
1b7b7 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 20 20 7d 0a  e_format;.    }.
1b7b8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 72 46    }else{.    wrF
1b7b9 6c 61 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  lag = 0;.  }.  i
1b7ba 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20  f( pOp->p5 ){.  
1b7bb 20 20 61 73 73 65 72 74 28 20 70 32 3e 30 20 29    assert( p2>0 )
1b7bc 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 32  ;.    assert( p2
1b7bd 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  <=p->nMem );.   
1b7be 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn2 = &p->aMem
1b7bf 5b 70 32 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65  [p2];.    sqlite
1b7c0 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
1b7c1 66 79 28 70 49 6e 32 29 3b 0a 20 20 20 20 70 32  fy(pIn2);.    p2
1b7c2 20 3d 20 28 69 6e 74 29 70 49 6e 32 2d 3e 75 2e   = (int)pIn2->u.
1b7c3 69 3b 0a 20 20 20 20 69 66 28 20 70 32 3c 32 20  i;.    if( p2<2 
1b7c4 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ) {.      rc = S
1b7c5 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1b7c6 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61  PT;.      goto a
1b7c7 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1b7c8 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  r;.    }.  }.  a
1b7c9 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20  ssert( i>=0 );. 
1b7ca 20 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65   pCur = allocate
1b7cb 43 75 72 73 6f 72 28 70 2c 20 69 2c 20 26 70 4f  Cursor(p, i, &pO
1b7cc 70 5b 2d 31 5d 2c 20 69 44 62 2c 20 31 29 3b 0a  p[-1], iDb, 1);.
1b7cd 20 20 69 66 28 20 70 43 75 72 3d 3d 30 20 29 20    if( pCur==0 ) 
1b7ce 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70  goto no_mem;.  p
1b7cf 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31  Cur->nullRow = 1
1b7d0 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1b7d1 42 74 72 65 65 43 75 72 73 6f 72 28 70 58 2c 20  BtreeCursor(pX, 
1b7d2 70 32 2c 20 77 72 46 6c 61 67 2c 20 70 4f 70 2d  p2, wrFlag, pOp-
1b7d3 3e 70 34 2e 70 2c 20 70 43 75 72 2d 3e 70 43 75  >p4.p, pCur->pCu
1b7d4 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 70 4f 70  rsor);.  if( pOp
1b7d5 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59  ->p4type==P4_KEY
1b7d6 49 4e 46 4f 20 29 7b 0a 20 20 20 20 70 43 75 72  INFO ){.    pCur
1b7d7 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70  ->pKeyInfo = pOp
1b7d8 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20  ->p4.pKeyInfo;. 
1b7d9 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66     pCur->pKeyInf
1b7da 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e  o->enc = ENC(p->
1b7db 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  db);.  }else{.  
1b7dc 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f    pCur->pKeyInfo
1b7dd 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 77 69 74   = 0;.  }.  swit
1b7de 63 68 28 20 72 63 20 29 7b 0a 20 20 20 20 63 61  ch( rc ){.    ca
1b7df 73 65 20 53 51 4c 49 54 45 5f 42 55 53 59 3a 20  se SQLITE_BUSY: 
1b7e0 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20  {.      p->pc = 
1b7e1 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20  pc;.      p->rc 
1b7e2 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55  = rc = SQLITE_BU
1b7e3 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 76  SY;.      goto v
1b7e4 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20  dbe_return;.    
1b7e5 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
1b7e6 45 5f 4f 4b 3a 20 7b 0a 20 20 20 20 20 20 69 6e  E_OK: {.      in
1b7e7 74 20 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65  t flags = sqlite
1b7e8 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 75 72  3BtreeFlags(pCur
1b7e9 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->pCursor);.    
1b7ea 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63    /* Sanity chec
1b7eb 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 74 68 65 20  king.  Only the 
1b7ec 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73 20  lower four bits 
1b7ed 6f 66 20 74 68 65 20 66 6c 61 67 73 20 62 79 74  of the flags byt
1b7ee 65 20 73 68 6f 75 6c 64 0a 20 20 20 20 20 20 2a  e should.      *
1b7ef 2a 20 62 65 20 75 73 65 64 2e 20 20 42 69 74 20  * be used.  Bit 
1b7f0 33 20 28 6d 61 73 6b 20 30 78 30 38 29 20 69 73  3 (mask 0x08) is
1b7f1 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 2e 20   unpredictable. 
1b7f2 20 54 68 65 20 6c 6f 77 65 72 20 33 20 62 69 74   The lower 3 bit
1b7f3 73 0a 20 20 20 20 20 20 2a 2a 20 28 6d 61 73 6b  s.      ** (mask
1b7f4 20 30 78 30 37 29 20 73 68 6f 75 6c 64 20 62 65   0x07) should be
1b7f5 20 65 69 74 68 65 72 20 35 20 28 69 6e 74 6b 65   either 5 (intke
1b7f6 79 2b 6c 65 61 66 64 61 74 61 20 66 6f 72 20 74  y+leafdata for t
1b7f7 61 62 6c 65 73 29 20 6f 72 0a 20 20 20 20 20 20  ables) or.      
1b7f8 2a 2a 20 32 20 28 7a 65 72 6f 64 61 74 61 20 66  ** 2 (zerodata f
1b7f9 6f 72 20 69 6e 64 69 63 65 73 29 2e 20 20 49 66  or indices).  If
1b7fa 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e   these condition
1b7fb 73 20 61 72 65 20 6e 6f 74 20 6d 65 74 20 69 74  s are not met it
1b7fc 20 63 61 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 6e   can.      ** on
1b7fd 6c 79 20 6d 65 61 6e 20 74 68 61 74 20 77 65 20  ly mean that we 
1b7fe 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
1b7ff 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62   a corrupt datab
1b800 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a  ase file.      *
1b801 2f 0a 20 20 20 20 20 20 69 66 28 20 28 66 6c 61  /.      if( (fla
1b802 67 73 20 26 20 30 78 66 30 29 21 3d 30 20 7c 7c  gs & 0xf0)!=0 ||
1b803 20 28 28 66 6c 61 67 73 20 26 20 30 78 30 37 29   ((flags & 0x07)
1b804 21 3d 35 20 26 26 20 28 66 6c 61 67 73 20 26 20  !=5 && (flags & 
1b805 30 78 30 37 29 21 3d 32 29 20 29 7b 0a 20 20 20  0x07)!=2) ){.   
1b806 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1b807 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1b808 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
1b809 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1b80a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43        }.      pC
1b80b 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20 28 66  ur->isTable = (f
1b80c 6c 61 67 73 20 26 20 42 54 52 45 45 5f 49 4e 54  lags & BTREE_INT
1b80d 4b 45 59 29 21 3d 30 20 3f 31 3a 30 3b 0a 20 20  KEY)!=0 ?1:0;.  
1b80e 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64 65      pCur->isInde
1b80f 78 20 3d 20 28 66 6c 61 67 73 20 26 20 42 54 52  x = (flags & BTR
1b810 45 45 5f 5a 45 52 4f 44 41 54 41 29 21 3d 30 20  EE_ZERODATA)!=0 
1b811 3f 31 3a 30 3b 0a 20 20 20 20 20 20 2f 2a 20 49  ?1:0;.      /* I
1b812 66 20 50 34 3d 3d 30 20 69 74 20 6d 65 61 6e 73  f P4==0 it means
1b813 20 77 65 20 61 72 65 20 65 78 70 65 63 74 65 64   we are expected
1b814 20 74 6f 20 6f 70 65 6e 20 61 20 74 61 62 6c 65   to open a table
1b815 2e 20 20 49 66 20 50 34 21 3d 30 20 74 68 65 6e  .  If P4!=0 then
1b816 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 65 78 70  .      ** we exp
1b817 65 63 74 20 74 6f 20 62 65 20 6f 70 65 6e 69 6e  ect to be openin
1b818 67 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20  g an index.  If 
1b819 74 68 69 73 20 69 73 20 6e 6f 74 20 77 68 61 74  this is not what
1b81a 20 68 61 70 70 65 6e 65 64 2c 0a 20 20 20 20 20   happened,.     
1b81b 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 64 61 74   ** then the dat
1b81c 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74  abase is corrupt
1b81d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1b81e 69 66 28 20 28 70 43 75 72 2d 3e 69 73 54 61 62  if( (pCur->isTab
1b81f 6c 65 20 26 26 20 70 4f 70 2d 3e 70 34 74 79 70  le && pOp->p4typ
1b820 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 29 0a 20  e==P4_KEYINFO). 
1b821 20 20 20 20 20 20 7c 7c 20 28 70 43 75 72 2d 3e        || (pCur->
1b822 69 73 49 6e 64 65 78 20 26 26 20 70 4f 70 2d 3e  isIndex && pOp->
1b823 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 59 49 4e  p4type!=P4_KEYIN
1b824 46 4f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  FO) ){.        r
1b825 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
1b826 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20  PT_BKPT;.       
1b827 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1b828 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1b829 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1b82a 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
1b82b 4c 49 54 45 5f 45 4d 50 54 59 3a 20 7b 0a 20 20  LITE_EMPTY: {.  
1b82c 20 20 20 20 70 43 75 72 2d 3e 69 73 54 61 62 6c      pCur->isTabl
1b82d 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70 65 21  e = pOp->p4type!
1b82e 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20  =P4_KEYINFO;.   
1b82f 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64 65 78     pCur->isIndex
1b830 20 3d 20 21 70 43 75 72 2d 3e 69 73 54 61 62 6c   = !pCur->isTabl
1b831 65 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70  e;.      pCur->p
1b832 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20  Cursor = 0;.    
1b833 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
1b834 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1b835 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74     }.    default
1b836 3a 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61  : {.      goto a
1b837 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1b838 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  r;.    }.  }.  b
1b839 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1b83a 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 72 61  de: OpenEphemera
1b83b 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  l P1 P2 * P4 *.*
1b83c 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20  *.** Open a new 
1b83d 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 74  cursor P1 to a t
1b83e 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a  ransient table..
1b83f 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 69 73  ** The cursor is
1b840 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 20 72   always opened r
1b841 65 61 64 2f 77 72 69 74 65 20 65 76 65 6e 20 69  ead/write even i
1b842 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  f .** the main d
1b843 61 74 61 62 61 73 65 20 69 73 20 72 65 61 64 2d  atabase is read-
1b844 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72 61 6e 73  only.  The trans
1b845 69 65 6e 74 20 6f 72 20 76 69 72 74 75 61 6c 0a  ient or virtual.
1b846 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 65 6c 65  ** table is dele
1b847 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ted automaticall
1b848 79 20 77 68 65 6e 20 74 68 65 20 63 75 72 73 6f  y when the curso
1b849 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a  r is closed..**.
1b84a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d  ** P2 is the num
1b84b 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
1b84c 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  n the virtual ta
1b84d 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73  ble..** The curs
1b84e 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 42  or points to a B
1b84f 54 72 65 65 20 74 61 62 6c 65 20 69 66 20 50 34  Tree table if P4
1b850 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20 42 54 72  ==0 and to a BTr
1b851 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66 20 50  ee index.** if P
1b852 34 20 69 73 20 6e 6f 74 20 30 2e 20 20 49 66 20  4 is not 0.  If 
1b853 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  P4 is not NULL, 
1b854 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b  it points to a K
1b855 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
1b856 0a 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65 73  .** that defines
1b857 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 6b   the format of k
1b858 65 79 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78  eys in the index
1b859 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
1b85a 6f 64 65 20 77 61 73 20 6f 6e 63 65 20 63 61 6c  ode was once cal
1b85b 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20 20 42  led OpenTemp.  B
1b85c 75 74 20 74 68 61 74 20 63 72 65 61 74 65 64 0a  ut that created.
1b85d 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62 65 63  ** confusion bec
1b85e 61 75 73 65 20 74 68 65 20 74 65 72 6d 20 22 74  ause the term "t
1b85f 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69 67 68  emp table", migh
1b860 74 20 72 65 66 65 72 20 65 69 74 68 65 72 0a 2a  t refer either.*
1b861 2a 20 74 6f 20 61 20 54 45 4d 50 20 74 61 62 6c  * to a TEMP tabl
1b862 65 20 61 74 20 74 68 65 20 53 51 4c 20 6c 65 76  e at the SQL lev
1b863 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61 62 6c  el, or to a tabl
1b864 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a 20 74  e opened by.** t
1b865 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 54 68 65  his opcode.  The
1b866 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 77 61  n this opcode wa
1b867 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75  s call OpenVirtu
1b868 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74  al.  But.** that
1b869 20 63 72 65 61 74 65 64 20 63 6f 6e 66 75 73 69   created confusi
1b86a 6f 6e 20 77 69 74 68 20 74 68 65 20 77 68 6f 6c  on with the whol
1b86b 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  e virtual-table 
1b86c 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  idea..*/.case OP
1b86d 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 3a 20  _OpenEphemeral: 
1b86e 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  {.  int i = pOp-
1b86f 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  >p1;.  VdbeCurso
1b870 72 20 2a 70 43 78 3b 0a 20 20 73 74 61 74 69 63  r *pCx;.  static
1b871 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65 6e 46   const int openF
1b872 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20 53 51  lags = .      SQ
1b873 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
1b874 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49  ITE |.      SQLI
1b875 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c  TE_OPEN_CREATE |
1b876 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  .      SQLITE_OP
1b877 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20  EN_EXCLUSIVE |. 
1b878 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
1b879 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c  _DELETEONCLOSE |
1b87a 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  .      SQLITE_OP
1b87b 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 3b  EN_TRANSIENT_DB;
1b87c 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ..  assert( i>=0
1b87d 20 29 3b 0a 20 20 70 43 78 20 3d 20 61 6c 6c 6f   );.  pCx = allo
1b87e 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 69 2c  cateCursor(p, i,
1b87f 20 70 4f 70 2c 20 2d 31 2c 20 31 29 3b 0a 20 20   pOp, -1, 1);.  
1b880 69 66 28 20 70 43 78 3d 3d 30 20 29 20 67 6f 74  if( pCx==0 ) got
1b881 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 78 2d  o no_mem;.  pCx-
1b882 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20  >nullRow = 1;.  
1b883 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1b884 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c 20  eFactory(db, 0, 
1b885 31 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  1, SQLITE_DEFAUL
1b886 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a  T_TEMP_CACHE_SIZ
1b887 45 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20  E, openFlags,.  
1b888 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b889 20 20 20 20 20 20 20 20 20 26 70 43 78 2d 3e 70           &pCx->p
1b88a 42 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  Bt);.  if( rc==S
1b88b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1b88c 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1b88d 65 42 65 67 69 6e 54 72 61 6e 73 28 70 43 78 2d  eBeginTrans(pCx-
1b88e 3e 70 42 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20  >pBt, 1);.  }.  
1b88f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1b890 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61  K ){.    /* If a
1b891 20 74 72 61 6e 73 69 65 6e 74 20 69 6e 64 65 78   transient index
1b892 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 63 72   is required, cr
1b893 65 61 74 65 20 69 74 20 62 79 20 63 61 6c 6c 69  eate it by calli
1b894 6e 67 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  ng.    ** sqlite
1b895 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c  3BtreeCreateTabl
1b896 65 28 29 20 77 69 74 68 20 74 68 65 20 42 54 52  e() with the BTR
1b897 45 45 5f 5a 45 52 4f 44 41 54 41 20 66 6c 61 67  EE_ZERODATA flag
1b898 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 6f   before.    ** o
1b899 70 65 6e 69 6e 67 20 69 74 2e 20 49 66 20 61 20  pening it. If a 
1b89a 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20  transient table 
1b89b 69 73 20 72 65 71 75 69 72 65 64 2c 20 6a 75 73  is required, jus
1b89c 74 20 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a  t use the.    **
1b89d 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
1b89e 72 65 61 74 65 64 20 74 61 62 6c 65 20 77 69 74  reated table wit
1b89f 68 20 72 6f 6f 74 2d 70 61 67 65 20 31 20 28 61  h root-page 1 (a
1b8a0 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 29 2e  n INTKEY table).
1b8a1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1b8a2 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f  pOp->p4.pKeyInfo
1b8a3 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 67   ){.      int pg
1b8a4 6e 6f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  no;.      assert
1b8a5 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1b8a6 34 5f 4b 45 59 49 4e 46 4f 20 29 3b 0a 20 20 20  4_KEYINFO );.   
1b8a7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1b8a8 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28  treeCreateTable(
1b8a9 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 6e 6f 2c  pCx->pBt, &pgno,
1b8aa 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29   BTREE_ZERODATA)
1b8ab 3b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  ; .      if( rc=
1b8ac 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1b8ad 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 67        assert( pg
1b8ae 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 2b  no==MASTER_ROOT+
1b8af 31 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  1 );.        rc 
1b8b0 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  = sqlite3BtreeCu
1b8b1 72 73 6f 72 28 70 43 78 2d 3e 70 42 74 2c 20 70  rsor(pCx->pBt, p
1b8b2 67 6e 6f 2c 20 31 2c 20 0a 20 20 20 20 20 20 20  gno, 1, .       
1b8b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b8b4 20 20 20 20 20 20 20 20 20 28 4b 65 79 49 6e 66           (KeyInf
1b8b5 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 43  o*)pOp->p4.z, pC
1b8b6 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20  x->pCursor);.   
1b8b7 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e       pCx->pKeyIn
1b8b8 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65  fo = pOp->p4.pKe
1b8b9 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 70  yInfo;.        p
1b8ba 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e  Cx->pKeyInfo->en
1b8bb 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a  c = ENC(p->db);.
1b8bc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43        }.      pC
1b8bd 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 30 3b 0a  x->isTable = 0;.
1b8be 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1b8bf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1b8c0 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 42  eeCursor(pCx->pB
1b8c1 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  t, MASTER_ROOT, 
1b8c2 31 2c 20 30 2c 20 70 43 78 2d 3e 70 43 75 72 73  1, 0, pCx->pCurs
1b8c3 6f 72 29 3b 0a 20 20 20 20 20 20 70 43 78 2d 3e  or);.      pCx->
1b8c4 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20  isTable = 1;.   
1b8c5 20 7d 0a 20 20 7d 0a 20 20 70 43 78 2d 3e 69 73   }.  }.  pCx->is
1b8c6 49 6e 64 65 78 20 3d 20 21 70 43 78 2d 3e 69 73  Index = !pCx->is
1b8c7 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a  Table;.  break;.
1b8c8 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70  }../* Opcode: Op
1b8c9 65 6e 50 73 65 75 64 6f 20 50 31 20 50 32 20 2a  enPseudo P1 P2 *
1b8ca 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20   * *.**.** Open 
1b8cb 61 20 6e 65 77 20 63 75 72 73 6f 72 20 74 68 61  a new cursor tha
1b8cc 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 61  t points to a fa
1b8cd 6b 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f  ke table that co
1b8ce 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a  ntains a single.
1b8cf 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 20  ** row of data. 
1b8d0 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
1b8d1 77 72 69 74 65 20 61 20 73 65 63 6f 6e 64 20 72  write a second r
1b8d2 6f 77 20 6f 66 20 64 61 74 61 20 63 61 75 73 65  ow of data cause
1b8d3 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 72  s the.** first r
1b8d4 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  ow to be deleted
1b8d5 2e 20 20 41 6c 6c 20 64 61 74 61 20 69 73 20 64  .  All data is d
1b8d6 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20  eleted when the 
1b8d7 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 6c 6f  cursor is.** clo
1b8d8 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 73 65  sed..**.** A pse
1b8d9 75 64 6f 2d 74 61 62 6c 65 20 63 72 65 61 74 65  udo-table create
1b8da 64 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65  d by this opcode
1b8db 20 69 73 20 75 73 65 66 75 6c 20 66 6f 72 20 68   is useful for h
1b8dc 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 45  olding the.** NE
1b8dd 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65 73 20  W or OLD tables 
1b8de 69 6e 20 61 20 74 72 69 67 67 65 72 2e 20 20 41  in a trigger.  A
1b8df 6c 73 6f 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  lso used to hold
1b8e0 20 74 68 65 20 61 20 73 69 6e 67 6c 65 0a 2a 2a   the a single.**
1b8e1 20 72 6f 77 20 6f 75 74 70 75 74 20 66 72 6f 6d   row output from
1b8e2 20 74 68 65 20 73 6f 72 74 65 72 20 73 6f 20 74   the sorter so t
1b8e3 68 61 74 20 74 68 65 20 72 6f 77 20 63 61 6e 20  hat the row can 
1b8e4 62 65 20 64 65 63 6f 6d 70 6f 73 65 64 20 69 6e  be decomposed in
1b8e5 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c  to.** individual
1b8e6 20 63 6f 6c 75 6d 6e 73 20 75 73 69 6e 67 20 74   columns using t
1b8e7 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63  he OP_Column opc
1b8e8 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ode..**.** When 
1b8e9 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 65 78 65  OP_Insert is exe
1b8ea 63 75 74 65 64 20 74 6f 20 69 6e 73 65 72 74 20  cuted to insert 
1b8eb 61 20 72 6f 77 20 69 6e 20 74 6f 20 74 68 65 20  a row in to the 
1b8ec 70 73 65 75 64 6f 20 74 61 62 6c 65 2c 0a 2a 2a  pseudo table,.**
1b8ed 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c   the pseudo-tabl
1b8ee 65 20 63 75 72 73 6f 72 20 6d 61 79 20 6f 72 20  e cursor may or 
1b8ef 6d 61 79 20 6e 6f 74 20 6d 61 6b 65 20 69 74 27  may not make it'
1b8f0 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66 20 74 68  s own copy of th
1b8f1 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 72 6f  e.** original ro
1b8f2 77 20 64 61 74 61 2e 20 49 66 20 50 32 20 69 73  w data. If P2 is
1b8f3 20 30 2c 20 74 68 65 6e 20 74 68 65 20 70 73 65   0, then the pse
1b8f4 75 64 6f 2d 74 61 62 6c 65 20 77 69 6c 6c 20 63  udo-table will c
1b8f5 6f 70 79 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69  opy the.** origi
1b8f6 6e 61 6c 20 72 6f 77 20 64 61 74 61 2e 20 4f 74  nal row data. Ot
1b8f7 68 65 72 77 69 73 65 2c 20 61 20 70 6f 69 6e 74  herwise, a point
1b8f8 65 72 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e  er to the origin
1b8f9 61 6c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 0a 2a  al memory cell.*
1b8fa 2a 20 69 73 20 73 74 6f 72 65 64 2e 20 49 6e 20  * is stored. In 
1b8fb 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20 76  this case, the v
1b8fc 64 62 65 20 70 72 6f 67 72 61 6d 20 6d 75 73 74  dbe program must
1b8fd 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65   ensure that the
1b8fe 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   .** memory cell
1b8ff 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1b900 72 6f 77 20 64 61 74 61 20 69 73 20 6e 6f 74 20  row data is not 
1b901 6f 76 65 72 77 72 69 74 74 65 6e 20 75 6e 74 69  overwritten unti
1b902 6c 20 74 68 65 0a 2a 2a 20 70 73 65 75 64 6f 20  l the.** pseudo 
1b903 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 65 64 20  table is closed 
1b904 28 6f 72 20 61 20 6e 65 77 20 72 6f 77 20 69 73  (or a new row is
1b905 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69   inserted into i
1b906 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f  t)..*/.case OP_O
1b907 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a 20 20 69  penPseudo: {.  i
1b908 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
1b909 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1b90a 78 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  x;.  assert( i>=
1b90b 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 61 6c 6c  0 );.  pCx = all
1b90c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 69  ocateCursor(p, i
1b90d 2c 20 26 70 4f 70 5b 2d 31 5d 2c 20 2d 31 2c 20  , &pOp[-1], -1, 
1b90e 30 29 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30  0);.  if( pCx==0
1b90f 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1b910 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d    pCx->nullRow =
1b911 20 31 3b 0a 20 20 70 43 78 2d 3e 70 73 65 75 64   1;.  pCx->pseud
1b912 6f 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 70 43  oTable = 1;.  pC
1b913 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61  x->ephemPseudoTa
1b914 62 6c 65 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70  ble = (u8)pOp->p
1b915 32 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61 62 6c  2;.  pCx->isTabl
1b916 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 69 73  e = 1;.  pCx->is
1b917 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 72 65  Index = 0;.  bre
1b918 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1b919 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a  : Close P1 * * *
1b91a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61   *.**.** Close a
1b91b 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73   cursor previous
1b91c 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e  ly opened as P1.
1b91d 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a    If P1 is not.*
1b91e 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e  * currently open
1b91f 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  , this instructi
1b920 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  on is a no-op..*
1b921 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a  /.case OP_Close:
1b922 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70   {.  int i = pOp
1b923 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p1;.  assert( 
1b924 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
1b925 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65  rsor );.  sqlite
1b926 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28  3VdbeFreeCursor(
1b927 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 3b  p, p->apCsr[i]);
1b928 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d  .  p->apCsr[i] =
1b929 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   0;.  break;.}..
1b92a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47  /* Opcode: SeekG
1b92b 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  e P1 P2 P3 P4 *.
1b92c 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20  **.** If cursor 
1b92d 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20  P1 refers to an 
1b92e 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65  SQL table (B-Tre
1b92f 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65  e that uses inte
1b930 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75  ger keys), .** u
1b931 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  se the value in 
1b932 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 74  register P3 as t
1b933 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75 72 73  he key.  If curs
1b934 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a  or P1 refers .**
1b935 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78   to an SQL index
1b936 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65  , then P3 is the
1b937 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72   first in an arr
1b938 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65  ay of P4 registe
1b939 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  rs .** that are 
1b93a 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63  used as an unpac
1b93b 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a  ked index key. .
1b93c 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e  **.** Reposition
1b93d 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
1b93e 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  at  it points to
1b93f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e   the smallest en
1b940 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20  try that .** is 
1b941 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
1b942 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79  equal to the key
1b943 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65   value. If there
1b944 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20   are no records 
1b945 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
1b946 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
1b947 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e   key and P2 is n
1b948 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75  ot zero, then ju
1b949 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  mp to P2..**.** 
1b94a 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c  See also: Found,
1b94b 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69   NotFound, Disti
1b94c 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65  nct, SeekLt, See
1b94d 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f  kGt, SeekLe.*/./
1b94e 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 74  * Opcode: SeekGt
1b94f 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1b950 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50  *.** If cursor P
1b951 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53  1 refers to an S
1b952 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65  QL table (B-Tree
1b953 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67   that uses integ
1b954 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73  er keys), .** us
1b955 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1b956 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20  egister P3 as a 
1b957 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50  key. If cursor P
1b958 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20  1 refers .** to 
1b959 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68  an SQL index, th
1b95a 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72  en P3 is the fir
1b95b 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f  st in an array o
1b95c 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a  f P4 registers .
1b95d 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64  ** that are used
1b95e 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20   as an unpacked 
1b95f 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a  index key. .**.*
1b960 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72  * Reposition cur
1b961 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20  sor P1 so that  
1b962 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
1b963 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20   smallest entry 
1b964 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61  that .** is grea
1b965 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b 65 79  ter than the key
1b966 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65   value. If there
1b967 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20   are no records 
1b968 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a  greater than .**
1b969 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
1b96a 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
1b96b 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1b96c 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
1b96d 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44  und, NotFound, D
1b96e 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c  istinct, SeekLt,
1b96f 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a   SeekGe, SeekLe.
1b970 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  */./* Opcode: Se
1b971 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20 50 34  ekLt P1 P2 P3 P4
1b972 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72   * .**.** If cur
1b973 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f  sor P1 refers to
1b974 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42   an SQL table (B
1b975 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20  -Tree that uses 
1b976 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a  integer keys), .
1b977 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65  ** use the value
1b978 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20   in register P3 
1b979 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72  as a key. If cur
1b97a 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a  sor P1 refers .*
1b97b 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65  * to an SQL inde
1b97c 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68  x, then P3 is th
1b97d 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72  e first in an ar
1b97e 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74  ray of P4 regist
1b97f 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65  ers .** that are
1b980 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61   used as an unpa
1b981 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20  cked index key. 
1b982 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f  .**.** Repositio
1b983 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  n cursor P1 so t
1b984 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74  hat  it points t
1b985 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e  o the largest en
1b986 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20  try that .** is 
1b987 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65  less than the ke
1b988 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72  y value. If ther
1b989 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73  e are no records
1b98a 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74   less than .** t
1b98b 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73  he key and P2 is
1b98c 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20   not zero, then 
1b98d 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  jump to P2..**.*
1b98e 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
1b98f 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73  d, NotFound, Dis
1b990 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53  tinct, SeekGt, S
1b991 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f  eekGe, SeekLe.*/
1b992 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b  ./* Opcode: Seek
1b993 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  Le P1 P2 P3 P4 *
1b994 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72  .**.** If cursor
1b995 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e   P1 refers to an
1b996 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72   SQL table (B-Tr
1b997 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74  ee that uses int
1b998 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20  eger keys), .** 
1b999 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  use the value in
1b99a 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
1b99b 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72  a key. If cursor
1b99c 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
1b99d 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
1b99e 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
1b99f 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
1b9a0 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
1b9a1 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
1b9a2 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
1b9a3 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
1b9a4 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
1b9a5 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1b9a6 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
1b9a7 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20  e largest entry 
1b9a8 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73  that .** is less
1b9a9 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
1b9aa 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e  o the key value.
1b9ab 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1b9ac 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73   records .** les
1b9ad 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
1b9ae 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50  to the key and P
1b9af 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74  2 is not zero, t
1b9b0 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a  hen jump to P2..
1b9b1 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1b9b2 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c  Found, NotFound,
1b9b3 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47   Distinct, SeekG
1b9b4 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c  t, SeekGe, SeekL
1b9b5 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65  t.*/.case OP_See
1b9b6 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f 2a 20  kLt:         /* 
1b9b7 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73  jump, in3 */.cas
1b9b8 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20  e OP_SeekLe:    
1b9b9 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1b9ba 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65  3 */.case OP_See
1b9bb 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20  kGe:         /* 
1b9bc 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73  jump, in3 */.cas
1b9bd 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20  e OP_SeekGt: {  
1b9be 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1b9bf 33 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  3 */.  int i = p
1b9c0 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75  Op->p1;.  VdbeCu
1b9c1 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73  rsor *pC;..  ass
1b9c2 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
1b9c3 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
1b9c4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d  ssert( pOp->p2!=
1b9c5 30 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61  0 );.  pC = p->a
1b9c6 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72  pCsr[i];.  asser
1b9c7 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 69 66  t( pC!=0 );.  if
1b9c8 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30  ( pC->pCursor!=0
1b9c9 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c   ){.    int res,
1b9ca 20 6f 63 3b 0a 20 20 20 20 6f 63 20 3d 20 70 4f   oc;.    oc = pO
1b9cb 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 70  p->opcode;.    p
1b9cc 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a  C->nullRow = 0;.
1b9cd 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73 54 61      if( pC->isTa
1b9ce 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 36 34  ble ){.      i64
1b9cf 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54   iKey;      /* T
1b9d0 68 65 20 72 6f 77 69 64 20 77 65 20 61 72 65 20  he rowid we are 
1b9d1 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a 0a 20  to seek to */.. 
1b9d2 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75       /* The inpu
1b9d3 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20 6d 69  t value in P3 mi
1b9d4 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20 74 79  ght be of any ty
1b9d5 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72 65 61  pe: integer, rea
1b9d6 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20 20 20  l, string,.     
1b9d7 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e 55 4c   ** blob, or NUL
1b9d8 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65 64 73  L.  But it needs
1b9d9 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65   to be an intege
1b9da 72 20 62 65 66 6f 72 65 20 77 65 20 63 61 6e 20  r before we can 
1b9db 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  do.      ** the 
1b9dc 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72 74 20  seek, so covert 
1b9dd 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 61 70 70  it. */.      app
1b9de 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  lyNumericAffinit
1b9df 79 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 69  y(pIn3);.      i
1b9e0 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Key = sqlite3Vdb
1b9e1 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b  eIntValue(pIn3);
1b9e2 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64  .      pC->rowid
1b9e3 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20  IsValid = 0;..  
1b9e4 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33      /* If the P3
1b9e5 20 76 61 6c 75 65 20 63 6f 75 6c 64 20 6e 6f 74   value could not
1b9e6 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e   be converted in
1b9e7 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 77 69  to an integer wi
1b9e8 74 68 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20 6c  thout.      ** l
1b9e9 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69  oss of informati
1b9ea 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63 69 61 6c  on, then special
1b9eb 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 72   processing is r
1b9ec 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f 0a 20 20  equired... */.  
1b9ed 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d 3e 66      if( (pIn3->f
1b9ee 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d  lags & MEM_Int)=
1b9ef 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =0 ){.        if
1b9f0 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26  ( (pIn3->flags &
1b9f1 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b   MEM_Real)==0 ){
1b9f2 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  .          /* If
1b9f3 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 63 61   the P3 value ca
1b9f4 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65  nnot be converte
1b9f5 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69 6e 64 20  d into any kind 
1b9f6 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a 20 20 20  of a number,.   
1b9f7 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74         ** then t
1b9f8 68 65 20 73 65 65 6b 20 69 73 20 6e 6f 74 20 70  he seek is not p
1b9f9 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a 75 6d 70  ossible, so jump
1b9fa 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 20 20   to P2 */.      
1b9fb 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1b9fc 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   - 1;.          
1b9fd 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
1b9fe 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77  .        /* If w
1b9ff 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69  e reach this poi
1ba00 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 50 33 20  nt, then the P3 
1ba01 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 61 20  value must be a 
1ba02 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20 20 20 20  floating.       
1ba03 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72   ** point number
1ba04 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73  . */.        ass
1ba05 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67  ert( (pIn3->flag
1ba06 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 21 3d 30  s & MEM_Real)!=0
1ba07 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28   );..        if(
1ba08 20 69 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53 54 5f   iKey==SMALLEST_
1ba09 49 4e 54 36 34 20 26 26 20 28 70 49 6e 33 2d 3e  INT64 && (pIn3->
1ba0a 72 3c 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 7c  r<(double)iKey |
1ba0b 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0a  | pIn3->r>0) ){.
1ba0c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1ba0d 20 50 33 20 76 61 6c 75 65 20 69 73 20 74 6f 20   P3 value is to 
1ba0e 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69 74 75  large in magnitu
1ba0f 64 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73  de to be express
1ba10 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 20  ed as an.       
1ba11 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 2a     ** integer. *
1ba12 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 20  /.          res 
1ba13 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 69  = 1;.          i
1ba14 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20 29 7b 0a  f( pIn3->r<0 ){.
1ba15 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1ba16 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c  oc==OP_SeekGt ||
1ba17 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29   oc==OP_SeekGe )
1ba18 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1ba19 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1ba1a 65 46 69 72 73 74 28 70 43 2d 3e 70 43 75 72 73  eFirst(pC->pCurs
1ba1b 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  or, &res);.     
1ba1c 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
1ba1d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
1ba1e 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1ba1f 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20  rror;.          
1ba20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 65    }.          }e
1ba21 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
1ba22 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b   if( oc==OP_Seek
1ba23 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65  Lt || oc==OP_See
1ba24 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  kLe ){.         
1ba25 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1ba26 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d 3e 70  3BtreeLast(pC->p
1ba27 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20  Cursor, &res);. 
1ba28 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
1ba29 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1ba2a 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1ba2b 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1ba2c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1ba2d 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
1ba2e 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20  ( res ){.       
1ba2f 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1ba30 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  2 - 1;.         
1ba31 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65   }.          bre
1ba32 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
1ba33 65 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 65  e if( oc==OP_See
1ba34 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65  kLt || oc==OP_Se
1ba35 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20 20 20  ekGe ){.        
1ba36 20 20 2f 2a 20 55 73 65 20 74 68 65 20 63 65 69    /* Use the cei
1ba37 6c 69 6e 67 28 29 20 66 75 6e 63 74 69 6f 6e 20  ling() function 
1ba38 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d  to convert real-
1ba39 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  >int */.        
1ba3a 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3e 20    if( pIn3->r > 
1ba3b 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29 20 69  (double)iKey ) i
1ba3c 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  Key++;.        }
1ba3d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1ba3e 2f 2a 20 55 73 65 20 74 68 65 20 66 6c 6f 6f 72  /* Use the floor
1ba3f 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63  () function to c
1ba40 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74  onvert real->int
1ba41 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73   */.          as
1ba42 73 65 72 74 28 20 6f 63 3d 3d 4f 50 5f 53 65 65  sert( oc==OP_See
1ba43 6b 4c 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65  kLe || oc==OP_Se
1ba44 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 20 20  ekGt );.        
1ba45 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3c 20    if( pIn3->r < 
1ba46 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29 20 69  (double)iKey ) i
1ba47 4b 65 79 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d  Key--;.        }
1ba48 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20  .      } .      
1ba49 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1ba4a 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1ba4b 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20  pC->pCursor, 0, 
1ba4c 28 75 36 34 29 69 4b 65 79 2c 20 30 2c 20 26 72  (u64)iKey, 0, &r
1ba4d 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  es);.      if( r
1ba4e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1ba4f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
1ba50 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1ba51 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1ba52 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20  f( res==0 ){.   
1ba53 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73       pC->rowidIs
1ba54 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Valid = 1;.     
1ba55 20 20 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64     pC->lastRowid
1ba56 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 20 20 7d   = iKey;.      }
1ba57 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1ba58 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1ba59 20 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46   r;.      int nF
1ba5a 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69  ield = pOp->p4.i
1ba5b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ba5c 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1ba5d 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20 20 61  INT32 );.      a
1ba5e 73 73 65 72 74 28 20 6e 46 69 65 6c 64 3e 30 20  ssert( nField>0 
1ba5f 29 3b 0a 20 20 20 20 20 20 72 2e 70 4b 65 79 49  );.      r.pKeyI
1ba60 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e  nfo = pC->pKeyIn
1ba61 66 6f 3b 0a 20 20 20 20 20 20 72 2e 6e 46 69 65  fo;.      r.nFie
1ba62 6c 64 20 3d 20 28 75 31 36 29 6e 46 69 65 6c 64  ld = (u16)nField
1ba63 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 63 3d 3d  ;.      if( oc==
1ba64 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 6f 63 3d  OP_SeekGt || oc=
1ba65 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 20  =OP_SeekLe ){.  
1ba66 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20        r.flags = 
1ba67 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
1ba68 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1ba69 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d         r.flags =
1ba6a 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1ba6b 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61    r.aMem = &p->a
1ba6c 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1ba6d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ba6e 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
1ba6f 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c  ked(pC->pCursor,
1ba70 20 26 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29   &r, 0, 0, &res)
1ba71 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
1ba72 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1ba73 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1ba74 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1ba75 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 2d 3e      }.      pC->
1ba76 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1ba77 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e  ;.    }.    pC->
1ba78 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d  deferredMoveto =
1ba79 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68   0;.    pC->cach
1ba7a 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1ba7b 53 54 41 4c 45 3b 0a 23 69 66 64 65 66 20 53 51  STALE;.#ifdef SQ
1ba7c 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71  LITE_TEST.    sq
1ba7d 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75  lite3_search_cou
1ba7e 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20  nt++;.#endif.   
1ba7f 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b   if( oc==OP_Seek
1ba80 47 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65  Ge || oc==OP_See
1ba81 6b 47 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28  kGt ){.      if(
1ba82 20 72 65 73 3c 30 20 7c 7c 20 28 72 65 73 3d 3d   res<0 || (res==
1ba83 30 20 26 26 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b  0 && oc==OP_Seek
1ba84 47 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  Gt) ){.        r
1ba85 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1ba86 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73 6f 72  Next(pC->pCursor
1ba87 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20  , &res);.       
1ba88 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1ba89 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  OK ) goto abort_
1ba8a 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1ba8b 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49        pC->rowidI
1ba8c 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20  sValid = 0;.    
1ba8d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1ba8e 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20   res = 0;.      
1ba8f 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
1ba90 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d 3d 4f     assert( oc==O
1ba91 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d  P_SeekLt || oc==
1ba92 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20  OP_SeekLe );.   
1ba93 20 20 20 69 66 28 20 72 65 73 3e 30 20 7c 7c 20     if( res>0 || 
1ba94 28 72 65 73 3d 3d 30 20 26 26 20 6f 63 3d 3d 4f  (res==0 && oc==O
1ba95 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a 20 20 20  P_SeekLt) ){.   
1ba96 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1ba97 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70  3BtreePrevious(p
1ba98 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73  C->pCursor, &res
1ba99 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1ba9a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
1ba9b 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1ba9c 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  _error;.        
1ba9d 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1ba9e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 0;.      }els
1ba9f 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 72 65  e{.        /* re
1baa0 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67 61 74  s might be negat
1baa1 69 76 65 20 62 65 63 61 75 73 65 20 74 68 65 20  ive because the 
1baa2 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 20  table is empty. 
1baa3 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 20 20   Check to.      
1baa4 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68 69 73    ** see if this
1baa5 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20   is the case..  
1baa6 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1baa7 20 72 65 73 20 3d 20 73 71 6c 69 74 65 33 42 74   res = sqlite3Bt
1baa8 72 65 65 45 6f 66 28 70 43 2d 3e 70 43 75 72 73  reeEof(pC->pCurs
1baa9 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  or);.      }.   
1baaa 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
1baab 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20  Op->p2>0 );.    
1baac 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20  if( res ){.     
1baad 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1baae 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
1baaf 20 69 66 28 20 21 70 43 2d 3e 70 73 65 75 64 6f   if( !pC->pseudo
1bab0 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20  Table ){.    /* 
1bab1 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65  This happens whe
1bab2 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  n attempting to 
1bab3 6f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33  open the sqlite3
1bab4 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20  _master table.  
1bab5 20 20 2a 2a 20 66 6f 72 20 72 65 61 64 20 61 63    ** for read ac
1bab6 63 65 73 73 20 72 65 74 75 72 6e 73 20 53 51 4c  cess returns SQL
1bab7 49 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20 74 68  ITE_EMPTY. In th
1bab8 69 73 20 63 61 73 65 20 61 6c 77 61 79 73 0a 20  is case always. 
1bab9 20 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a     ** take the j
1baba 75 6d 70 20 28 73 69 6e 63 65 20 74 68 65 72 65  ump (since there
1babb 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20   are no records 
1babc 69 6e 20 74 68 65 20 74 61 62 6c 65 29 2e 0a 20  in the table).. 
1babd 20 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70     */.    pc = p
1babe 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
1babf 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1bac0 70 63 6f 64 65 3a 20 53 65 65 6b 20 50 31 20 50  pcode: Seek P1 P
1bac1 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31  2 * * *.**.** P1
1bac2 20 69 73 20 61 6e 20 6f 70 65 6e 20 74 61 62 6c   is an open tabl
1bac3 65 20 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20  e cursor and P2 
1bac4 69 73 20 61 20 72 6f 77 69 64 20 69 6e 74 65 67  is a rowid integ
1bac5 65 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a 2a 20  er.  Arrange.** 
1bac6 66 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65 20 73  for P1 to move s
1bac7 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73  o that it points
1bac8 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 67 69   to the rowid gi
1bac9 76 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a 2a 2a  ven by P2..**.**
1baca 20 54 68 69 73 20 69 73 20 61 63 74 75 61 6c 6c   This is actuall
1bacb 79 20 61 20 64 65 66 65 72 72 65 64 20 73 65 65  y a deferred see
1bacc 6b 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63 74 75  k.  Nothing actu
1bacd 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 75 6e 74  ally happens unt
1bace 69 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72  il.** the cursor
1bacf 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64   is used to read
1bad0 20 61 20 72 65 63 6f 72 64 2e 20 20 54 68 61 74   a record.  That
1bad1 20 77 61 79 2c 20 69 66 20 6e 6f 20 72 65 61 64   way, if no read
1bad2 73 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f 20 75  s.** occur, no u
1bad3 6e 6e 65 63 65 73 73 61 72 79 20 49 2f 4f 20 68  nnecessary I/O h
1bad4 61 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73 65 20  appens..*/.case 
1bad5 4f 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20 2f 2a  OP_Seek: {    /*
1bad6 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20 69 20   in2 */.  int i 
1bad7 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62  = pOp->p1;.  Vdb
1bad8 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20  eCursor *pC;..  
1bad9 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
1bada 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  i<p->nCursor );.
1badb 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b    pC = p->apCsr[
1badc 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  i];.  assert( pC
1badd 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 2d  !=0 );.  if( pC-
1bade 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20  >pCursor!=0 ){. 
1badf 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69     assert( pC->i
1bae0 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 43  sTable );.    pC
1bae1 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20  ->nullRow = 0;. 
1bae2 20 20 20 70 43 2d 3e 6d 6f 76 65 74 6f 54 61 72     pC->movetoTar
1bae3 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  get = sqlite3Vdb
1bae4 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b  eIntValue(pIn2);
1bae5 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73  .    pC->rowidIs
1bae6 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 70  Valid = 0;.    p
1bae7 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1bae8 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  o = 1;.  }.  bre
1bae9 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63  ak;.}.  ../* Opc
1baea 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20 50 32  ode: Found P1 P2
1baeb 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65   P3 * *.**.** Re
1baec 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20  gister P3 holds 
1baed 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74  a blob construct
1baee 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64  ed by MakeRecord
1baef 2e 20 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65  .  P1 is an inde
1baf0 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 6e 74 72  x..** If an entr
1baf1 79 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74  y that matches t
1baf2 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1baf3 73 74 65 72 20 70 33 20 65 78 69 73 74 73 20 69  ster p3 exists i
1baf4 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d  n P1 then.** jum
1baf5 70 20 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65  p to P2.  If the
1baf6 20 50 33 20 76 61 6c 75 65 20 64 6f 65 73 20 6e   P3 value does n
1baf7 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 65 6e 74  ot match any ent
1baf8 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74 68 65 6e  ry in P1.** then
1baf9 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 68 65   fall thru.  The
1bafa 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 6c 65   P1 cursor is le
1bafb 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74  ft pointing at t
1bafc 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72  he matching entr
1bafd 79 0a 2a 2a 20 69 66 20 69 74 20 65 78 69 73 74  y.** if it exist
1bafe 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  s..**.** This in
1baff 73 74 72 75 63 74 69 6f 6e 20 69 73 20 75 73 65  struction is use
1bb00 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
1bb01 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77  he IN operator w
1bb02 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c 65 66 74  here the.** left
1bb03 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 20  -hand side is a 
1bb04 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1bb05 2e 20 20 50 31 20 6d 61 79 20 62 65 20 61 20 74  .  P1 may be a t
1bb06 72 75 65 20 69 6e 64 65 78 2c 20 6f 72 20 69 74  rue index, or it
1bb07 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20 74 65 6d  .** may be a tem
1bb08 70 6f 72 61 72 79 20 69 6e 64 65 78 20 74 68 61  porary index tha
1bb09 74 20 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75  t holds the resu
1bb0a 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43  lts of the SELEC
1bb0b 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20  T.** statement. 
1bb0c 20 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69    This instructi
1bb0d 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  on is also used 
1bb0e 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1bb0f 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20 6b 65 79  .** DISTINCT key
1bb10 77 6f 72 64 20 69 6e 20 53 45 4c 45 43 54 20 73  word in SELECT s
1bb11 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
1bb12 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1bb13 6e 20 63 68 65 63 6b 73 20 69 66 20 69 6e 64 65  n checks if inde
1bb14 78 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 20  x P1 contains a 
1bb15 72 65 63 6f 72 64 20 66 6f 72 20 77 68 69 63 68  record for which
1bb16 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 4e   .** the first N
1bb17 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75   serialized valu
1bb18 65 73 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68  es exactly match
1bb19 20 74 68 65 20 4e 20 73 65 72 69 61 6c 69 7a 65   the N serialize
1bb1a 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 74  d values.** in t
1bb1b 68 65 20 72 65 63 6f 72 64 20 69 6e 20 72 65 67  he record in reg
1bb1c 69 73 74 65 72 20 50 33 2c 20 77 68 65 72 65 20  ister P3, where 
1bb1d 4e 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  N is the total n
1bb1e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20  umber of values 
1bb1f 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20 72 65 63  in.** the P3 rec
1bb20 6f 72 64 20 28 74 68 65 20 50 33 20 72 65 63 6f  ord (the P3 reco
1bb21 72 64 20 69 73 20 61 20 70 72 65 66 69 78 20 6f  rd is a prefix o
1bb22 66 20 74 68 65 20 50 31 20 72 65 63 6f 72 64 29  f the P1 record)
1bb23 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  . .**.** See als
1bb24 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55  o: NotFound, IsU
1bb25 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69 73 74 73  nique, NotExists
1bb26 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .*/./* Opcode: N
1bb27 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33  otFound P1 P2 P3
1bb28 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73   * *.**.** Regis
1bb29 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62  ter P3 holds a b
1bb2a 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20  lob constructed 
1bb2b 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20  by MakeRecord.  
1bb2c 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65  P1 is.** an inde
1bb2d 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 72 79 20  x.  If no entry 
1bb2e 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 61  exists in P1 tha
1bb2f 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 62 6c  t matches the bl
1bb30 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20  ob then jump.** 
1bb31 74 6f 20 50 32 2e 20 20 49 66 20 61 6e 20 65 6e  to P2.  If an en
1bb32 74 72 79 20 64 6f 65 73 20 65 78 69 73 74 69 6e  try does existin
1bb33 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e  g, fall through.
1bb34 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20    The cursor is 
1bb35 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67  left.** pointing
1bb36 20 74 6f 20 74 68 65 20 65 6e 74 72 79 20 74 68   to the entry th
1bb37 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a 2a 0a 2a  at matches..**.*
1bb38 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
1bb39 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 49 73  d, NotExists, Is
1bb3a 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f  Unique.*/.case O
1bb3b 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 20 20  P_NotFound:     
1bb3c 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1bb3d 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e 64 3a  /.case OP_Found:
1bb3e 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1bb3f 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20  p, in3 */.  int 
1bb40 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69  i = pOp->p1;.  i
1bb41 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73 74 73  nt alreadyExists
1bb42 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75 72 73   = 0;.  VdbeCurs
1bb43 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 72 74  or *pC;.  assert
1bb44 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
1bb45 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65  Cursor );.  asse
1bb46 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21  rt( p->apCsr[i]!
1bb47 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 20  =0 );.  if( (pC 
1bb48 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e  = p->apCsr[i])->
1bb49 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20  pCursor!=0 ){.  
1bb4a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 55    int res;.    U
1bb4b 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70  npackedRecord *p
1bb4c 49 64 78 4b 65 79 3b 0a 0a 20 20 20 20 61 73 73  IdxKey;..    ass
1bb4d 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65  ert( pC->isTable
1bb4e 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
1bb4f 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26  t( pIn3->flags &
1bb50 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20   MEM_Blob );.   
1bb51 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74   pIdxKey = sqlit
1bb52 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61  e3VdbeRecordUnpa
1bb53 63 6b 28 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c  ck(pC->pKeyInfo,
1bb54 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e   pIn3->n, pIn3->
1bb55 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  z,.             
1bb56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bb57 20 20 20 20 20 20 20 20 20 61 54 65 6d 70 52 65           aTempRe
1bb58 63 2c 20 73 69 7a 65 6f 66 28 61 54 65 6d 70 52  c, sizeof(aTempR
1bb59 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49  ec));.    if( pI
1bb5a 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20  dxKey==0 ){.    
1bb5b 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1bb5c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70     }.    if( pOp
1bb5d 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75  ->opcode==OP_Fou
1bb5e 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 49 64 78  nd ){.      pIdx
1bb5f 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55 4e  Key->flags |= UN
1bb60 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41  PACKED_PREFIX_MA
1bb61 54 43 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  TCH;.    }.    r
1bb62 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1bb63 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70  MovetoUnpacked(p
1bb64 43 2d 3e 70 43 75 72 73 6f 72 2c 20 70 49 64 78  C->pCursor, pIdx
1bb65 4b 65 79 2c 20 30 2c 20 30 2c 20 26 72 65 73 29  Key, 0, 0, &res)
1bb66 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1bb67 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52  eDeleteUnpackedR
1bb68 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a  ecord(pIdxKey);.
1bb69 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1bb6a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62  TE_OK ){.      b
1bb6b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1bb6c 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 3d 20  alreadyExists = 
1bb6d 28 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20 70 43  (res==0);.    pC
1bb6e 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1bb6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61   = 0;.    pC->ca
1bb70 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1bb71 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 69  E_STALE;.  }.  i
1bb72 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
1bb73 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20  OP_Found ){.    
1bb74 69 66 28 20 61 6c 72 65 61 64 79 45 78 69 73 74  if( alreadyExist
1bb75 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32  s ) pc = pOp->p2
1bb76 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   - 1;.  }else{. 
1bb77 20 20 20 69 66 28 20 21 61 6c 72 65 61 64 79 45     if( !alreadyE
1bb78 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70  xists ) pc = pOp
1bb79 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20  ->p2 - 1;.  }.  
1bb7a 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1bb7b 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31  ode: IsUnique P1
1bb7c 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1bb7d 2a 20 54 68 65 20 50 33 20 72 65 67 69 73 74 65  * The P3 registe
1bb7e 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e  r contains an in
1bb7f 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d  teger record num
1bb80 62 65 72 2e 20 20 43 61 6c 6c 20 74 68 69 73 0a  ber.  Call this.
1bb81 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ** record number
1bb82 20 52 2e 20 20 54 68 65 20 50 34 20 72 65 67 69   R.  The P4 regi
1bb83 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e  ster contains an
1bb84 20 69 6e 64 65 78 20 6b 65 79 20 63 72 65 61 74   index key creat
1bb85 65 64 0a 2a 2a 20 75 73 69 6e 67 20 4d 61 6b 65  ed.** using Make
1bb86 52 65 63 6f 72 64 2e 20 20 43 61 6c 6c 20 69 74  Record.  Call it
1bb87 20 4b 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20   K..**.** P1 is 
1bb88 61 6e 20 69 6e 64 65 78 2e 20 20 53 6f 20 69 74  an index.  So it
1bb89 20 68 61 73 20 6e 6f 20 64 61 74 61 20 61 6e 64   has no data and
1bb8a 20 69 74 73 20 6b 65 79 20 63 6f 6e 73 69 73 74   its key consist
1bb8b 73 20 6f 66 20 61 0a 2a 2a 20 72 65 63 6f 72 64  s of a.** record
1bb8c 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 4f 50   generated by OP
1bb8d 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77 68 65 72  _MakeRecord wher
1bb8e 65 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64  e the last field
1bb8f 20 69 73 20 74 68 65 20 0a 2a 2a 20 72 6f 77 69   is the .** rowi
1bb90 64 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 74  d of the entry t
1bb91 68 61 74 20 74 68 65 20 69 6e 64 65 78 20 72 65  hat the index re
1bb92 66 65 72 73 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20  fers to..** .** 
1bb93 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1bb94 20 61 73 6b 73 20 69 66 20 74 68 65 72 65 20 69   asks if there i
1bb95 73 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50 31  s an entry in P1
1bb96 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 66 69   where the.** fi
1bb97 65 6c 64 73 20 6d 61 74 63 68 65 73 20 4b 20 62  elds matches K b
1bb98 75 74 20 74 68 65 20 72 6f 77 69 64 20 69 73 20  ut the rowid is 
1bb99 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 52  different from R
1bb9a 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  ..** If there is
1bb9b 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20   no such entry, 
1bb9c 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6e  then there is an
1bb9d 20 69 6d 6d 65 64 69 61 74 65 0a 2a 2a 20 6a 75   immediate.** ju
1bb9e 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20 61 6e  mp to P2.  If an
1bb9f 79 20 65 6e 74 72 79 20 64 6f 65 73 20 65 78 69  y entry does exi
1bba0 73 74 20 77 68 65 72 65 20 74 68 65 20 69 6e 64  st where the ind
1bba1 65 78 20 73 74 72 69 6e 67 0a 2a 2a 20 6d 61 74  ex string.** mat
1bba2 63 68 65 73 20 4b 20 62 75 74 20 74 68 65 20 72  ches K but the r
1bba3 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
1bba4 6e 6f 74 20 52 2c 20 74 68 65 6e 20 74 68 65 20  not R, then the 
1bba5 72 65 63 6f 72 64 0a 2a 2a 20 6e 75 6d 62 65 72  record.** number
1bba6 20 66 6f 72 20 74 68 61 74 20 65 6e 74 72 79 20   for that entry 
1bba7 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  is written into 
1bba8 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 0a 2a  P3 and control.*
1bba9 2a 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 20  * falls through 
1bbaa 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74  to the next inst
1bbab 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53  ruction..**.** S
1bbac 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e  ee also: NotFoun
1bbad 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 46 6f  d, NotExists, Fo
1bbae 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  und.*/.case OP_I
1bbaf 73 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 20 20  sUnique: {      
1bbb0 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1bbb1 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  /.  int i = pOp-
1bbb2 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  >p1;.  VdbeCurso
1bbb3 72 20 2a 70 43 78 3b 0a 20 20 42 74 43 75 72 73  r *pCx;.  BtCurs
1bbb4 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 4d 65 6d  or *pCrsr;.  Mem
1bbb5 20 2a 70 4b 3b 0a 20 20 69 36 34 20 52 3b 0a 0a   *pK;.  i64 R;..
1bbb6 20 20 2f 2a 20 50 6f 70 20 74 68 65 20 76 61 6c    /* Pop the val
1bbb7 75 65 20 52 20 6f 66 66 20 74 68 65 20 74 6f 70  ue R off the top
1bbb8 20 6f 66 20 74 68 65 20 73 74 61 63 6b 0a 20 20   of the stack.  
1bbb9 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1bbba 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54  ->p4type==P4_INT
1bbbb 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  32 );.  assert( 
1bbbc 70 4f 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70  pOp->p4.i>0 && p
1bbbd 4f 70 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65  Op->p4.i<=p->nMe
1bbbe 6d 20 29 3b 0a 20 20 70 4b 20 3d 20 26 70 2d 3e  m );.  pK = &p->
1bbbf 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e 69 5d 3b  aMem[pOp->p4.i];
1bbc0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1bbc1 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33  mIntegerify(pIn3
1bbc2 29 3b 0a 20 20 52 20 3d 20 70 49 6e 33 2d 3e 75  );.  R = pIn3->u
1bbc3 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e  .i;.  assert( i>
1bbc4 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
1bbc5 6f 72 20 29 3b 0a 20 20 70 43 78 20 3d 20 70 2d  or );.  pCx = p-
1bbc6 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73  >apCsr[i];.  ass
1bbc7 65 72 74 28 20 70 43 78 21 3d 30 20 29 3b 0a 20  ert( pCx!=0 );. 
1bbc8 20 70 43 72 73 72 20 3d 20 70 43 78 2d 3e 70 43   pCrsr = pCx->pC
1bbc9 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 72  ursor;.  if( pCr
1bbca 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  sr!=0 ){.    int
1bbcb 20 72 65 73 3b 0a 20 20 20 20 69 36 34 20 76 3b   res;.    i64 v;
1bbcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bbcd 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f       /* The reco
1bbce 72 64 20 6e 75 6d 62 65 72 20 74 68 61 74 20 6d  rd number that m
1bbcf 61 74 63 68 65 73 20 4b 20 2a 2f 0a 20 20 20 20  atches K */.    
1bbd0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
1bbd1 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20 55 6e  pIdxKey;   /* Un
1bbd2 70 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f  packed version o
1bbd3 66 20 50 34 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  f P4 */..    /* 
1bbd4 4d 61 6b 65 20 73 75 72 65 20 4b 20 69 73 20 61  Make sure K is a
1bbd5 20 73 74 72 69 6e 67 20 61 6e 64 20 6d 61 6b 65   string and make
1bbd6 20 7a 4b 65 79 20 70 6f 69 6e 74 20 74 6f 20 4b   zKey point to K
1bbd7 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
1bbd8 72 74 28 20 70 4b 2d 3e 66 6c 61 67 73 20 26 20  rt( pK->flags & 
1bbd9 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20  MEM_Blob );.    
1bbda 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65  pIdxKey = sqlite
1bbdb 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63  3VdbeRecordUnpac
1bbdc 6b 28 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2c  k(pCx->pKeyInfo,
1bbdd 20 70 4b 2d 3e 6e 2c 20 70 4b 2d 3e 7a 2c 0a 20   pK->n, pK->z,. 
1bbde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bbdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bbe0 20 20 20 20 20 61 54 65 6d 70 52 65 63 2c 20 73       aTempRec, s
1bbe1 69 7a 65 6f 66 28 61 54 65 6d 70 52 65 63 29 29  izeof(aTempRec))
1bbe2 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 4b 65  ;.    if( pIdxKe
1bbe3 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  y==0 ){.      go
1bbe4 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d  to no_mem;.    }
1bbe5 0a 20 20 20 20 70 49 64 78 4b 65 79 2d 3e 66 6c  .    pIdxKey->fl
1bbe6 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f  ags |= UNPACKED_
1bbe7 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a 0a 20  IGNORE_ROWID;.. 
1bbe8 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72     /* Search for
1bbe9 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50 31 20   an entry in P1 
1bbea 77 68 65 72 65 20 61 6c 6c 20 62 75 74 20 74 68  where all but th
1bbeb 65 20 6c 61 73 74 20 72 6f 77 69 64 20 6d 61 74  e last rowid mat
1bbec 63 68 20 4b 0a 20 20 20 20 2a 2a 20 49 66 20 74  ch K.    ** If t
1bbed 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20  here is no such 
1bbee 65 6e 74 72 79 2c 20 6a 75 6d 70 20 69 6d 6d 65  entry, jump imme
1bbef 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 20  diately to P2.. 
1bbf0 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
1bbf1 28 20 70 43 78 2d 3e 64 65 66 65 72 72 65 64 4d  ( pCx->deferredM
1bbf2 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20  oveto==0 );.    
1bbf3 70 43 78 2d 3e 63 61 63 68 65 53 74 61 74 75 73  pCx->cacheStatus
1bbf4 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1bbf5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1bbf6 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
1bbf7 6b 65 64 28 70 43 72 73 72 2c 20 70 49 64 78 4b  ked(pCrsr, pIdxK
1bbf8 65 79 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b  ey, 0, 0, &res);
1bbf9 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1bbfa 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1bbfb 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
1bbfc 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28  eUnpackedRecord(
1bbfd 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20  pIdxKey);.      
1bbfe 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1bbff 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  o_error;.    }. 
1bc00 20 20 20 69 66 28 20 72 65 73 3c 30 20 29 7b 0a     if( res<0 ){.
1bc01 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1bc02 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 72 73  e3BtreeNext(pCrs
1bc03 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20  r, &res);.      
1bc04 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20  if( res ){.     
1bc05 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1bc06 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  - 1;.        sql
1bc07 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e  ite3VdbeDeleteUn
1bc08 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 64  packedRecord(pId
1bc09 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62  xKey);.        b
1bc0a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1bc0b 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
1bc0c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f  ite3VdbeIdxKeyCo
1bc0d 6d 70 61 72 65 28 70 43 78 2c 20 70 49 64 78 4b  mpare(pCx, pIdxK
1bc0e 65 79 2c 20 26 72 65 73 29 3b 20 0a 20 20 20 20  ey, &res); .    
1bc0f 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
1bc10 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28  eUnpackedRecord(
1bc11 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 66  pIdxKey);.    if
1bc12 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1bc13 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1bc14 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 69  _to_error;.    i
1bc15 66 28 20 72 65 73 3e 30 20 29 7b 0a 20 20 20 20  f( res>0 ){.    
1bc16 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1bc17 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   1;.      break;
1bc18 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
1bc19 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 70 43  t this point, pC
1bc1a 72 73 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  rsr is pointing 
1bc1b 74 6f 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50  to an entry in P
1bc1c 31 20 77 68 65 72 65 20 61 6c 6c 20 62 75 74 0a  1 where all but.
1bc1d 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6e 61 6c      ** the final
1bc1e 20 65 6e 74 72 79 20 28 74 68 65 20 72 6f 77 69   entry (the rowi
1bc1f 64 29 20 6d 61 74 63 68 65 73 20 4b 2e 20 20 43  d) matches K.  C
1bc20 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1bc21 68 65 0a 20 20 20 20 2a 2a 20 66 69 6e 61 6c 20  he.    ** final 
1bc22 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20  rowid column is 
1bc23 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 52  different from R
1bc24 2e 20 20 49 66 20 69 74 20 65 71 75 61 6c 73 20  .  If it equals 
1bc25 52 20 74 68 65 6e 20 6a 75 6d 70 0a 20 20 20 20  R then jump.    
1bc26 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  ** immediately t
1bc27 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  o P2..    */.   
1bc28 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1bc29 65 49 64 78 52 6f 77 69 64 28 70 43 72 73 72 2c  eIdxRowid(pCrsr,
1bc2a 20 26 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63   &v);.    if( rc
1bc2b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1bc2c 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1bc2d 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1bc2e 20 20 7d 0a 20 20 20 20 69 66 28 20 76 3d 3d 52    }.    if( v==R
1bc2f 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70   ){.      pc = p
1bc30 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1bc31 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
1bc32 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6e 61 6c      /* The final
1bc33 20 76 61 72 69 6e 74 20 6f 66 20 74 68 65 20 6b   varint of the k
1bc34 65 79 20 69 73 20 64 69 66 66 65 72 65 6e 74 20  ey is different 
1bc35 66 72 6f 6d 20 52 2e 20 20 53 74 6f 72 65 20 69  from R.  Store i
1bc36 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20 69 6e  t back.    ** in
1bc37 74 6f 20 72 65 67 69 73 74 65 72 20 52 33 2e 20  to register R3. 
1bc38 20 28 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d   (The record num
1bc39 62 65 72 20 6f 66 20 61 6e 20 65 6e 74 72 79 20  ber of an entry 
1bc3a 74 68 61 74 20 76 69 6f 6c 61 74 65 73 0a 20 20  that violates.  
1bc3b 20 20 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f    ** a UNIQUE co
1bc3c 6e 73 74 72 61 69 6e 74 2e 29 0a 20 20 20 20 2a  nstraint.).    *
1bc3d 2f 0a 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69 20  /.    pIn3->u.i 
1bc3e 3d 20 76 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = v;.    assert(
1bc3f 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d 45 4d   pIn3->flags&MEM
1bc40 5f 49 6e 74 20 29 3b 0a 20 20 7d 0a 20 20 62 72  _Int );.  }.  br
1bc41 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1bc42 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31 20  e: NotExists P1 
1bc43 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1bc44 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  Use the content 
1bc45 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 61  of register P3 a
1bc46 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79 2e  s a integer key.
1bc47 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a 2a    If a record .*
1bc48 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79 20  * with that key 
1bc49 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69  does not exist i
1bc4a 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 74  n table of P1, t
1bc4b 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20  hen jump to P2. 
1bc4c 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72  .** If the recor
1bc4d 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74 68  d does exist, th
1bc4e 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54  en fall thru.  T
1bc4f 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66  he cursor is lef
1bc50 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74  t .** pointing t
1bc51 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20  o the record if 
1bc52 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a  it exists..**.**
1bc53 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20   The difference 
1bc54 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70 65  between this ope
1bc55 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46 6f  ration and NotFo
1bc56 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69 73  und is that this
1bc57 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61 73  .** operation as
1bc58 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69 73  sumes the key is
1bc59 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20   an integer and 
1bc5a 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61 62  that P1 is a tab
1bc5b 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e 6f  le whereas.** No
1bc5c 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20 6b  tFound assumes k
1bc5d 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e  ey is a blob con
1bc5e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d 61  structed from Ma
1bc5f 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a 20  keRecord and.** 
1bc60 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e 0a  P1 is an index..
1bc61 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1bc62 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c  Found, NotFound,
1bc63 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73   IsUnique.*/.cas
1bc64 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a 20  e OP_NotExists: 
1bc65 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1bc66 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69  , in3 */.  int i
1bc67 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64   = pOp->p1;.  Vd
1bc68 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1bc69 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1bc6a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1bc6b 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
1bc6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
1bc6d 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20  apCsr[i]!=0 );. 
1bc6e 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70   if( (pCrsr = (p
1bc6f 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
1bc70 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b  ->pCursor)!=0 ){
1bc71 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d 20 30  .    int res = 0
1bc72 3b 0a 20 20 20 20 75 36 34 20 69 4b 65 79 3b 0a  ;.    u64 iKey;.
1bc73 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33      assert( pIn3
1bc74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1bc75 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
1bc76 20 70 2d 3e 61 70 43 73 72 5b 69 5d 2d 3e 69 73   p->apCsr[i]->is
1bc77 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 69 4b 65  Table );.    iKe
1bc78 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 70 49 6e  y = intToKey(pIn
1bc79 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 72 63 20  3->u.i);.    rc 
1bc7a 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
1bc7b 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 72  vetoUnpacked(pCr
1bc7c 73 72 2c 20 30 2c 20 69 4b 65 79 2c 20 30 2c 26  sr, 0, iKey, 0,&
1bc7d 72 65 73 29 3b 0a 20 20 20 20 70 43 2d 3e 6c 61  res);.    pC->la
1bc7e 73 74 52 6f 77 69 64 20 3d 20 70 49 6e 33 2d 3e  stRowid = pIn3->
1bc7f 75 2e 69 3b 0a 20 20 20 20 70 43 2d 3e 72 6f 77  u.i;.    pC->row
1bc80 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 73 3d  idIsValid = res=
1bc81 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70 43 2d  =0 ?1:0;.    pC-
1bc82 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20  >nullRow = 0;.  
1bc83 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75    pC->cacheStatu
1bc84 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1bc85 0a 20 20 20 20 69 66 28 20 72 65 73 21 3d 30 20  .    if( res!=0 
1bc86 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
1bc87 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
1bc88 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 6f 77   assert( pC->row
1bc89 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a  idIsValid==0 );.
1bc8a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
1bc8b 28 20 21 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  ( !pC->pseudoTab
1bc8c 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  le ){.    /* Thi
1bc8d 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61  s happens when a
1bc8e 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 70 65  n attempt to ope
1bc8f 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 20  n a read cursor 
1bc90 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73  on the .    ** s
1bc91 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
1bc92 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  le returns SQLIT
1bc93 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f 0a  E_EMPTY..    */.
1bc94 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
1bc95 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70  isTable );.    p
1bc96 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1bc97 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1bc98 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30  >rowidIsValid==0
1bc99 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   );.  }.  break;
1bc9a 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
1bc9b 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a 20  equence P1 P2 * 
1bc9c 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74  * *.**.** Find t
1bc9d 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c  he next availabl
1bc9e 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65  e sequence numbe
1bc9f 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e  r for cursor P1.
1bca0 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 65  .** Write the se
1bca1 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e  quence number in
1bca2 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1bca3 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 20  ** The sequence 
1bca4 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 75  number on the cu
1bca5 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 6e  rsor is incremen
1bca6 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a 2a  ted after this.*
1bca7 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20  * instruction.  
1bca8 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 75  .*/.case OP_Sequ
1bca9 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20  ence: {         
1bcaa 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1bcab 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20  ease */.  int i 
1bcac 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73  = pOp->p1;.  ass
1bcad 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
1bcae 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
1bcaf 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b  ssert( p->apCsr[
1bcb0 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d  i]!=0 );.  pOut-
1bcb1 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b  >u.i = p->apCsr[
1bcb2 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a  i]->seqCount++;.
1bcb3 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1bcb4 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1bcb5 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a  .  break;.}.../*
1bcb6 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69   Opcode: NewRowi
1bcb7 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  d P1 P2 P3 * *.*
1bcb8 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69  *.** Get a new i
1bcb9 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75  nteger record nu
1bcba 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77  mber (a.k.a "row
1bcbb 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65  id") used as the
1bcbc 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e   key to a table.
1bcbd 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e  .** The record n
1bcbe 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65  umber is not pre
1bcbf 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20  viously used as 
1bcc0 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74  a key in the dat
1bcc1 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74  abase.** table t
1bcc2 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f  hat cursor P1 po
1bcc3 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65  ints to.  The ne
1bcc4 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  w record number 
1bcc5 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72  is written.** wr
1bcc6 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65  itten to registe
1bcc7 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  r P2..**.** If P
1bcc8 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61  3>0 then P3 is a
1bcc9 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 68   register that h
1bcca 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65 73 74  olds the largest
1bccb 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 67   previously.** g
1bccc 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20  enerated record 
1bccd 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65 77 20  number.  No new 
1bcce 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61  record numbers a
1bccf 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65  re allowed to be
1bcd0 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68   less.** than th
1bcd1 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65 6e 20  is value.  When 
1bcd2 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 68  this value reach
1bcd3 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20  es its maximum, 
1bcd4 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a  a SQLITE_FULL.**
1bcd5 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61   error is genera
1bcd6 74 65 64 2e 20 20 54 68 65 20 50 33 20 72 65 67  ted.  The P3 reg
1bcd7 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65 64  ister is updated
1bcd8 20 77 69 74 68 20 74 68 65 20 67 65 6e 65 72 61   with the genera
1bcd9 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20 6e 75  ted.** record nu
1bcda 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33 20 6d  mber.  This P3 m
1bcdb 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64  echanism is used
1bcdc 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65   to help impleme
1bcdd 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e  nt the.** AUTOIN
1bcde 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e  CREMENT feature.
1bcdf 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52  .*/.case OP_NewR
1bce0 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20  owid: {         
1bce1 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1bce2 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20  ease */.  int i 
1bce3 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 34  = pOp->p1;.  i64
1bce4 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75   v = 0;.  VdbeCu
1bce5 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
1bce6 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1bce7 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
1bce8 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69  sert( p->apCsr[i
1bce9 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70  ]!=0 );.  if( (p
1bcea 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
1bceb 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b 0a  ->pCursor==0 ){.
1bcec 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f 20      /* The zero 
1bced 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61  initialization a
1bcee 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 74  bove is all that
1bcef 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20   is needed */.  
1bcf0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
1bcf1 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 20  e next rowid or 
1bcf2 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 64  record number (d
1bcf3 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 66  ifferent terms f
1bcf4 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20  or the same.    
1bcf5 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74  ** thing) is obt
1bcf6 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73  ained in a two-s
1bcf7 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20  tep algorithm.. 
1bcf8 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72     **.    ** Fir
1bcf9 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f  st we attempt to
1bcfa 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73   find the larges
1bcfb 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
1bcfc 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 20   and add one.   
1bcfd 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 75   ** to that.  Bu
1bcfe 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 74  t if the largest
1bcff 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20   existing rowid 
1bd00 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 6d  is already the m
1bd01 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 6f  aximum.    ** po
1bd02 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c 20  sitive integer, 
1bd03 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c 20  we have to fall 
1bd04 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73  through to the s
1bd05 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 6f  econd.    ** pro
1bd06 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f 72  babilistic algor
1bd07 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ithm.    **.    
1bd08 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6c  ** The second al
1bd09 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 65  gorithm is to se
1bd0a 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 20  lect a rowid at 
1bd0b 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 69  random and see i
1bd0c 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 65  f.    ** it alre
1bd0d 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 68  ady exists in th
1bd0e 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20  e table.  If it 
1bd0f 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20  does not exist, 
1bd10 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 73  we have.    ** s
1bd11 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 68  ucceeded.  If th
1bd12 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 64  e random rowid d
1bd13 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 65  oes exist, we se
1bd14 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a 20  lect a new one. 
1bd15 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 67     ** and try ag
1bd16 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 30 20  ain, up to 1000 
1bd17 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20  times..    **.  
1bd18 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65    ** For a table
1bd19 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 6e 20   with less than 
1bd1a 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 69 65  2 billion entrie
1bd1b 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69  s, the probabili
1bd1c 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e 6f 74  ty.    ** of not
1bd1d 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 73 65   finding a unuse
1bd1e 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f 75 74  d rowid is about
1bd1f 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 69 73   1.0e-300.  This
1bd20 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 6e 6f   is a .    ** no
1bd21 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69  n-zero probabili
1bd22 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 73 74  ty, but it is st
1bd23 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c 79 20  ill vanishingly 
1bd24 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 6c 64  small and should
1bd25 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 63 61  .    ** never ca
1bd26 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e 20 20  use a problem.  
1bd27 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 6d 75  You are much, mu
1bd28 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74  ch more likely t
1bd29 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a 20  o have a.    ** 
1bd2a 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 72 65  hardware failure
1bd2b 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 20 61   than for this a
1bd2c 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 69 6c  lgorithm to fail
1bd2d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1bd2e 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 6e 20  The analysis in 
1bd2f 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72  the previous par
1bd30 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 20 74  agraph assumes t
1bd31 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 20 67  hat you have a g
1bd32 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63  ood.    ** sourc
1bd33 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62  e of random numb
1bd34 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 72 61  ers.  Is a libra
1bd35 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65  ry function like
1bd36 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 20 2a   lrand48().    *
1bd37 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f 20 20  * good enough?  
1bd38 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e 6f 74  Maybe. Maybe not
1bd39 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f 20 6b  . It's hard to k
1bd3a 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 65 72  now whether ther
1bd3b 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 62  e.    ** might b
1bd3c 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 69 73  e subtle bugs is
1bd3d 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61   some implementa
1bd3e 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 34 38  tions of lrand48
1bd3f 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 63  () that.    ** c
1bd40 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c  ould cause probl
1bd41 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 75 6e  ems. To avoid un
1bd42 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c 69 74  certainty, SQLit
1bd43 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e 20 0a  e uses its own .
1bd44 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 6e 75      ** random nu
1bd45 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62  mber generator b
1bd46 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 34 20  ased on the RC4 
1bd47 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a  algorithm..    *
1bd48 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 6f 6d  *.    ** To prom
1bd49 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f 66 20  ote locality of 
1bd4a 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 72 65  reference for re
1bd4b 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 74 73  petitive inserts
1bd4c 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72  , the.    ** fir
1bd4d 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 73 20  st few attempts 
1bd4e 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20 72 61  at choosing a ra
1bd4f 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b 20  ndom rowid pick 
1bd50 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c 69  values just a li
1bd51 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67  ttle.    ** larg
1bd52 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 76  er than the prev
1bd53 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68 69  ious rowid.  Thi
1bd54 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77 6e  s has been shown
1bd55 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79 0a   experimentally.
1bd56 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c 65      ** to double
1bd57 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74 68   the speed of th
1bd58 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f 6e  e COPY operation
1bd59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
1bd5a 20 72 65 73 2c 20 72 78 3d 53 51 4c 49 54 45 5f   res, rx=SQLITE_
1bd5b 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69 36 34  OK, cnt;.    i64
1bd5c 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20 30 3b   x;.    cnt = 0;
1bd5d 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 65  .    if( (sqlite
1bd5e 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e  3BtreeFlags(pC->
1bd5f 70 43 75 72 73 6f 72 29 26 28 42 54 52 45 45 5f  pCursor)&(BTREE_
1bd60 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a 45 52  INTKEY|BTREE_ZER
1bd61 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20 20 20  ODATA)) !=.     
1bd62 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b 45       BTREE_INTKE
1bd63 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  Y ){.      rc = 
1bd64 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1bd65 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  KPT;.      goto 
1bd66 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1bd67 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  or;.    }.    as
1bd68 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 42 74  sert( (sqlite3Bt
1bd69 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 43 75  reeFlags(pC->pCu
1bd6a 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f 49 4e  rsor) & BTREE_IN
1bd6b 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20 20 20  TKEY)!=0 );.    
1bd6c 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33  assert( (sqlite3
1bd6d 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70  BtreeFlags(pC->p
1bd6e 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f  Cursor) & BTREE_
1bd6f 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29 3b 0a  ZERODATA)==0 );.
1bd70 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33  .#ifdef SQLITE_3
1bd71 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64  2BIT_ROWID.#   d
1bd72 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20  efine MAX_ROWID 
1bd73 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65  0x7fffffff.#else
1bd74 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d  .    /* Some com
1bd75 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20  pilers complain 
1bd76 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20  about constants 
1bd77 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66  of the form 0x7f
1bd78 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a  ffffffffffffff..
1bd79 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f      ** Others co
1bd7a 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37  mplain about 0x7
1bd7b 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  ffffffffffffffff
1bd7c 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  LL.  The followi
1bd7d 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20  ng macro seems. 
1bd7e 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65     ** to provide
1bd7f 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68   the constant wh
1bd80 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63  ile making all c
1bd81 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a  ompilers happy..
1bd82 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e      */.#   defin
1bd83 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36  e MAX_ROWID  (i6
1bd84 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66  4)( (((u64)0x7ff
1bd85 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75  fffff)<<32) | (u
1bd86 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a  64)0xffffffff ).
1bd87 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20  #endif..    if( 
1bd88 21 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f  !pC->useRandomRo
1bd89 77 69 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28  wid ){.      if(
1bd8a 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61   pC->nextRowidVa
1bd8b 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 76  lid ){.        v
1bd8c 20 3d 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64   = pC->nextRowid
1bd8d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1bd8e 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1bd8f 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d  te3BtreeLast(pC-
1bd90 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b  >pCursor, &res);
1bd91 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
1bd92 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1bd93 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
1bd94 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1bd95 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1bd96 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20     if( res ){.  
1bd97 20 20 20 20 20 20 20 20 76 20 3d 20 31 3b 0a 20          v = 1;. 
1bd98 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1bd99 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
1bd9a 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d 3e  treeKeySize(pC->
1bd9b 70 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20  pCursor, &v);.  
1bd9c 20 20 20 20 20 20 20 20 76 20 3d 20 6b 65 79 54          v = keyT
1bd9d 6f 49 6e 74 28 76 29 3b 0a 20 20 20 20 20 20 20  oInt(v);.       
1bd9e 20 20 20 69 66 28 20 76 3d 3d 4d 41 58 5f 52 4f     if( v==MAX_RO
1bd9f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20  WID ){.         
1bda0 20 20 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d     pC->useRandom
1bda1 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Rowid = 1;.     
1bda2 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1bda3 20 20 20 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20          v++;.   
1bda4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1bda5 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e   }.      }..#ifn
1bda6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1bda7 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
1bda8 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20      if( pOp->p3 
1bda9 29 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 2a  ){.        Mem *
1bdaa 70 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 61 73  pMem;.        as
1bdab 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20  sert( pOp->p3>0 
1bdac 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e  && pOp->p3<=p->n
1bdad 4d 65 6d 20 29 3b 20 2f 2a 20 50 33 20 69 73 20  Mem ); /* P3 is 
1bdae 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63  a valid memory c
1bdaf 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 70  ell */.        p
1bdb0 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Mem = &p->aMem[p
1bdb1 4f 70 2d 3e 70 33 5d 3b 0a 09 52 45 47 49 53 54  Op->p3];..REGIST
1bdb2 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33  ER_TRACE(pOp->p3
1bdb3 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20  , pMem);.       
1bdb4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1bdb5 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b  ntegerify(pMem);
1bdb6 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1bdb7 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
1bdb8 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 20  MEM_Int)!=0 );  
1bdb9 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 73  /* mem(P3) holds
1bdba 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20   an integer */. 
1bdbb 20 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d         if( pMem-
1bdbc 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  >u.i==MAX_ROWID 
1bdbd 7c 7c 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d  || pC->useRandom
1bdbe 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Rowid ){.       
1bdbf 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46     rc = SQLITE_F
1bdc0 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 67  ULL;.          g
1bdc1 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1bdc2 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  _error;.        
1bdc3 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3c  }.        if( v<
1bdc4 70 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20  pMem->u.i+1 ){. 
1bdc5 20 20 20 20 20 20 20 20 20 76 20 3d 20 70 4d 65           v = pMe
1bdc6 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20  m->u.i + 1;.    
1bdc7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d      }.        pM
1bdc8 65 6d 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20  em->u.i = v;.   
1bdc9 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
1bdca 20 20 20 69 66 28 20 76 3c 4d 41 58 5f 52 4f 57     if( v<MAX_ROW
1bdcb 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  ID ){.        pC
1bdcc 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64  ->nextRowidValid
1bdcd 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43   = 1;.        pC
1bdce 2d 3e 6e 65 78 74 52 6f 77 69 64 20 3d 20 76 2b  ->nextRowid = v+
1bdcf 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
1bdd0 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65 78 74          pC->next
1bdd1 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a  RowidValid = 0;.
1bdd2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1bdd3 20 20 69 66 28 20 70 43 2d 3e 75 73 65 52 61 6e    if( pC->useRan
1bdd4 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  domRowid ){.    
1bdd5 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1bdd6 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 51 4c 49  3==0 );  /* SQLI
1bdd7 54 45 5f 46 55 4c 4c 20 6d 75 73 74 20 68 61 76  TE_FULL must hav
1bdd8 65 20 6f 63 63 75 72 72 65 64 20 70 72 69 6f 72  e occurred prior
1bdd9 20 74 6f 20 74 68 69 73 20 2a 2f 0a 20 20 20 20   to this */.    
1bdda 20 20 76 20 3d 20 64 62 2d 3e 70 72 69 6f 72 4e    v = db->priorN
1bddb 65 77 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 63  ewRowid;.      c
1bddc 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f  nt = 0;.      do
1bddd 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6e  {.        if( cn
1bdde 74 3d 3d 30 20 26 26 20 28 76 26 30 78 66 66 66  t==0 && (v&0xfff
1bddf 66 66 66 29 3d 3d 76 20 29 7b 0a 20 20 20 20 20  fff)==v ){.     
1bde0 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20       v++;.      
1bde1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1bde2 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f     sqlite3_rando
1bde3 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 76 29 2c  mness(sizeof(v),
1bde4 20 26 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20   &v);.          
1bde5 69 66 28 20 63 6e 74 3c 35 20 29 20 76 20 26 3d  if( cnt<5 ) v &=
1bde6 20 30 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20   0xffffff;.     
1bde7 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1bde8 20 76 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65   v==0 ) continue
1bde9 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 69 6e  ;.        x = in
1bdea 74 54 6f 4b 65 79 28 76 29 3b 0a 20 20 20 20 20  tToKey(v);.     
1bdeb 20 20 20 72 78 20 3d 20 73 71 6c 69 74 65 33 42     rx = sqlite3B
1bdec 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
1bded 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  ed(pC->pCursor, 
1bdee 30 2c 20 28 75 36 34 29 78 2c 20 30 2c 20 26 72  0, (u64)x, 0, &r
1bdef 65 73 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74  es);.        cnt
1bdf0 2b 2b 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65  ++;.      }while
1bdf1 28 20 63 6e 74 3c 31 30 30 20 26 26 20 72 78 3d  ( cnt<100 && rx=
1bdf2 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65  =SQLITE_OK && re
1bdf3 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62  s==0 );.      db
1bdf4 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 20  ->priorNewRowid 
1bdf5 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 72  = v;.      if( r
1bdf6 78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  x==SQLITE_OK && 
1bdf7 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  res==0 ){.      
1bdf8 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55    rc = SQLITE_FU
1bdf9 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  LL;.        goto
1bdfa 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1bdfb 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
1bdfc 20 7d 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64   }.    pC->rowid
1bdfd 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
1bdfe 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
1bdff 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d  eto = 0;.    pC-
1be00 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
1be01 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a  ACHE_STALE;.  }.
1be02 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1be03 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1be04 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76  .  pOut->u.i = v
1be05 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1be06 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 20   Opcode: Insert 
1be07 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1be08 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e  *.** Write an en
1be09 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 61 62  try into the tab
1be0a 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 31 2e  le of cursor P1.
1be0b 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73    A new entry is
1be0c 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 20 69  .** created if i
1be0d 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61 64  t doesn't alread
1be0e 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 20 64  y exist or the d
1be0f 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 73 74  ata for an exist
1be10 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20  ing.** entry is 
1be11 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 54 68  overwritten.  Th
1be12 65 20 64 61 74 61 20 69 73 20 74 68 65 20 76 61  e data is the va
1be13 6c 75 65 20 73 74 6f 72 65 64 20 72 65 67 69 73  lue stored regis
1be14 74 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32  ter.** number P2
1be15 2e 20 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f  . The key is sto
1be16 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1be17 50 33 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74  P3. The key must
1be18 0a 2a 2a 20 62 65 20 61 6e 20 69 6e 74 65 67 65  .** be an intege
1be19 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
1be1a 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66  OPFLAG_NCHANGE f
1be1b 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74  lag of P5 is set
1be1c 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63  , then the row c
1be1d 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a  hange count is.*
1be1e 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f  * incremented (o
1be1f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 20 20  therwise not).  
1be20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41  If the OPFLAG_LA
1be21 53 54 52 4f 57 49 44 20 66 6c 61 67 20 6f 66 20  STROWID flag of 
1be22 50 35 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68  P5 is set,.** th
1be23 65 6e 20 72 6f 77 69 64 20 69 73 20 73 74 6f 72  en rowid is stor
1be24 65 64 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e  ed for subsequen
1be25 74 20 72 65 74 75 72 6e 20 62 79 20 74 68 65 0a  t return by the.
1be26 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  ** sqlite3_last_
1be27 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 66  insert_rowid() f
1be28 75 6e 63 74 69 6f 6e 20 28 6f 74 68 65 72 77 69  unction (otherwi
1be29 73 65 20 69 74 20 69 73 20 75 6e 6d 6f 64 69 66  se it is unmodif
1be2a 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61  ied)..**.** Para
1be2b 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69  meter P4 may poi
1be2c 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63  nt to a string c
1be2d 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
1be2e 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20  ble-name, or.** 
1be2f 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20  may be NULL. If 
1be30 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  it is not NULL, 
1be31 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d  then the update-
1be32 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65  hook .** (sqlite
1be33 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63  3.xUpdateCallbac
1be34 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f  k) is invoked fo
1be35 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73  llowing a succes
1be36 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a  sful insert..**.
1be37 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f  ** (WARNING/TODO
1be38 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65  : If P1 is a pse
1be39 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50  udo-cursor and P
1be3a 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79  2 is dynamically
1be3b 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74  .** allocated, t
1be3c 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66  hen ownership of
1be3d 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72   P2 is transferr
1be3e 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f  ed to the pseudo
1be3f 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72  -cursor.** and r
1be40 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d  egister P2 becom
1be41 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49  es ephemeral.  I
1be42 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
1be43 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20  changed, the.** 
1be44 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65  value of registe
1be45 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63  r P2 will then c
1be46 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72  hange.  Make sur
1be47 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a  e this does not.
1be48 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f  ** cause any pro
1be49 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68  blems.).**.** Th
1be4a 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  is instruction o
1be4b 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62  nly works on tab
1be4c 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61  les.  The equiva
1be4d 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  lent instruction
1be4e 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20  .** for indices 
1be4f 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e  is OP_IdxInsert.
1be50 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65  .*/.case OP_Inse
1be51 72 74 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 44 61  rt: {.  Mem *pDa
1be52 74 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ta = &p->aMem[pO
1be53 70 2d 3e 70 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70  p->p2];.  Mem *p
1be54 4b 65 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Key = &p->aMem[p
1be55 4f 70 2d 3e 70 33 5d 3b 0a 0a 20 20 69 36 34 20  Op->p3];..  i64 
1be56 69 4b 65 79 3b 20 20 20 2f 2a 20 54 68 65 20 69  iKey;   /* The i
1be57 6e 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20  nteger ROWID or 
1be58 6b 65 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f  key for the reco
1be59 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  rd to be inserte
1be5a 64 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  d */.  int i = p
1be5b 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75  Op->p1;.  VdbeCu
1be5c 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
1be5d 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1be5e 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43  >nCursor );.  pC
1be5f 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a   = p->apCsr[i];.
1be60 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20    assert( pC!=0 
1be61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d  );.  assert( pC-
1be62 3e 70 43 75 72 73 6f 72 21 3d 30 20 7c 7c 20 70  >pCursor!=0 || p
1be63 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29  C->pseudoTable )
1be64 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79  ;.  assert( pKey
1be65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1be66 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t );.  assert( p
1be67 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1be68 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1be69 4f 70 2d 3e 70 32 2c 20 70 44 61 74 61 29 3b 0a  Op->p2, pData);.
1be6a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1be6b 28 70 4f 70 2d 3e 70 33 2c 20 70 4b 65 79 29 3b  (pOp->p3, pKey);
1be6c 0a 0a 20 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f  ..  iKey = intTo
1be6d 4b 65 79 28 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a  Key(pKey->u.i);.
1be6e 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20    if( pOp->p5 & 
1be6f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29  OPFLAG_NCHANGE )
1be70 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20   p->nChange++;. 
1be71 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f   if( pOp->p5 & O
1be72 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20  PFLAG_LASTROWID 
1be73 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20  ) db->lastRowid 
1be74 3d 20 70 4b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69  = pKey->u.i;.  i
1be75 66 28 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64  f( pC->nextRowid
1be76 56 61 6c 69 64 20 26 26 20 70 4b 65 79 2d 3e 75  Valid && pKey->u
1be77 2e 69 3e 3d 70 43 2d 3e 6e 65 78 74 52 6f 77 69  .i>=pC->nextRowi
1be78 64 20 29 7b 0a 20 20 20 20 70 43 2d 3e 6e 65 78  d ){.    pC->nex
1be79 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b  tRowidValid = 0;
1be7a 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 61 74 61  .  }.  if( pData
1be7b 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1be7c 6c 6c 20 29 7b 0a 20 20 20 20 70 44 61 74 61 2d  ll ){.    pData-
1be7d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 70 44 61 74  >z = 0;.    pDat
1be7e 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  a->n = 0;.  }els
1be7f 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
1be80 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 28 4d  Data->flags & (M
1be81 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29  EM_Blob|MEM_Str)
1be82 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43   );.  }.  if( pC
1be83 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 7b  ->pseudoTable ){
1be84 0a 20 20 20 20 69 66 28 20 21 70 43 2d 3e 65 70  .    if( !pC->ep
1be85 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20 29  hemPseudoTable )
1be86 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
1be87 62 46 72 65 65 28 64 62 2c 20 70 43 2d 3e 70 44  bFree(db, pC->pD
1be88 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ata);.    }.    
1be89 70 43 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b  pC->iKey = iKey;
1be8a 0a 20 20 20 20 70 43 2d 3e 6e 44 61 74 61 20 3d  .    pC->nData =
1be8b 20 70 44 61 74 61 2d 3e 6e 3b 0a 20 20 20 20 69   pData->n;.    i
1be8c 66 28 20 70 44 61 74 61 2d 3e 7a 3d 3d 70 44 61  f( pData->z==pDa
1be8d 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20 7c 7c 20 70  ta->zMalloc || p
1be8e 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61  C->ephemPseudoTa
1be8f 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 2d  ble ){.      pC-
1be90 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 2d 3e  >pData = pData->
1be91 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43  z;.      if( !pC
1be92 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62  ->ephemPseudoTab
1be93 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  le ){.        pD
1be94 61 74 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d  ata->flags &= ~M
1be95 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 20 20 20 20  EM_Dyn;.        
1be96 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 7c 3d 20  pData->flags |= 
1be97 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20  MEM_Ephem;.     
1be98 20 20 20 70 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f     pData->zMallo
1be99 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 0;.      }. 
1be9a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1be9b 70 43 2d 3e 70 44 61 74 61 20 3d 20 73 71 6c 69  pC->pData = sqli
1be9c 74 65 33 4d 61 6c 6c 6f 63 28 20 70 43 2d 3e 6e  te3Malloc( pC->n
1be9d 44 61 74 61 2b 32 20 29 3b 0a 20 20 20 20 20 20  Data+2 );.      
1be9e 69 66 28 20 21 70 43 2d 3e 70 44 61 74 61 20 29  if( !pC->pData )
1be9f 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1bea0 20 20 20 20 6d 65 6d 63 70 79 28 70 43 2d 3e 70      memcpy(pC->p
1bea1 44 61 74 61 2c 20 70 44 61 74 61 2d 3e 7a 2c 20  Data, pData->z, 
1bea2 70 43 2d 3e 6e 44 61 74 61 29 3b 0a 20 20 20 20  pC->nData);.    
1bea3 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e    pC->pData[pC->
1bea4 6e 44 61 74 61 5d 20 3d 20 30 3b 0a 20 20 20 20  nData] = 0;.    
1bea5 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e    pC->pData[pC->
1bea6 6e 44 61 74 61 2b 31 5d 20 3d 20 30 3b 0a 20 20  nData+1] = 0;.  
1bea7 20 20 7d 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c    }.    pC->null
1bea8 52 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Row = 0;.  }else
1bea9 7b 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b  {.    int nZero;
1beaa 0a 20 20 20 20 69 66 28 20 70 44 61 74 61 2d 3e  .    if( pData->
1beab 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
1beac 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20   ){.      nZero 
1bead 3d 20 70 44 61 74 61 2d 3e 75 2e 6e 5a 65 72 6f  = pData->u.nZero
1beae 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1beaf 20 20 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20     nZero = 0;.  
1beb0 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
1beb1 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28  ite3BtreeInsert(
1beb2 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20  pC->pCursor, 0, 
1beb3 69 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20  iKey,.          
1beb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1beb5 20 20 70 44 61 74 61 2d 3e 7a 2c 20 70 44 61 74    pData->z, pDat
1beb6 61 2d 3e 6e 2c 20 6e 5a 65 72 6f 2c 0a 20 20 20  a->n, nZero,.   
1beb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1beb8 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 35           pOp->p5
1beb9 20 26 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44   & OPFLAG_APPEND
1beba 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 70 43 2d 3e  );.  }.  .  pC->
1bebb 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1bebc 3b 0a 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64  ;.  pC->deferred
1bebd 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 70 43  Moveto = 0;.  pC
1bebe 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1bebf 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20  CACHE_STALE;..  
1bec0 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 70  /* Invoke the up
1bec1 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71  date-hook if req
1bec2 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20  uired. */.  if( 
1bec3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1bec4 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c   db->xUpdateCall
1bec5 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e  back && pOp->p4.
1bec6 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  z ){.    const c
1bec7 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
1bec8 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d  Db[pC->iDb].zNam
1bec9 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e;.    const cha
1beca 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70  r *zTbl = pOp->p
1becb 34 2e 7a 3b 0a 20 20 20 20 69 6e 74 20 6f 70 20  4.z;.    int op 
1becc 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50  = ((pOp->p5 & OP
1becd 46 4c 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f  FLAG_ISUPDATE) ?
1bece 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a   SQLITE_UPDATE :
1becf 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b   SQLITE_INSERT);
1bed0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1bed1 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
1bed2 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62  db->xUpdateCallb
1bed3 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41  ack(db->pUpdateA
1bed4 72 67 2c 20 6f 70 2c 20 7a 44 62 2c 20 7a 54 62  rg, op, zDb, zTb
1bed5 6c 2c 20 69 4b 65 79 29 3b 0a 20 20 20 20 61 73  l, iKey);.    as
1bed6 73 65 72 74 28 20 70 43 2d 3e 69 44 62 3e 3d 30  sert( pC->iDb>=0
1bed7 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   );.  }.  break;
1bed8 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44  .}../* Opcode: D
1bed9 65 6c 65 74 65 20 50 31 20 50 32 20 2a 20 50 34  elete P1 P2 * P4
1beda 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20   *.**.** Delete 
1bedb 74 68 65 20 72 65 63 6f 72 64 20 61 74 20 77 68  the record at wh
1bedc 69 63 68 20 74 68 65 20 50 31 20 63 75 72 73 6f  ich the P1 curso
1bedd 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  r is currently p
1bede 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  ointing..**.** T
1bedf 68 65 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 62  he cursor will b
1bee0 65 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  e left pointing 
1bee1 61 74 20 65 69 74 68 65 72 20 74 68 65 20 6e 65  at either the ne
1bee2 78 74 20 6f 72 20 74 68 65 20 70 72 65 76 69 6f  xt or the previo
1bee3 75 73 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 20  us.** record in 
1bee4 74 68 65 20 74 61 62 6c 65 2e 20 49 66 20 69 74  the table. If it
1bee5 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
1bee6 67 20 61 74 20 74 68 65 20 6e 65 78 74 20 72 65  g at the next re
1bee7 63 6f 72 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  cord, then.** th
1bee8 65 20 6e 65 78 74 20 4e 65 78 74 20 69 6e 73 74  e next Next inst
1bee9 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  ruction will be 
1beea 61 20 6e 6f 2d 6f 70 2e 20 20 48 65 6e 63 65 20  a no-op.  Hence 
1beeb 69 74 20 69 73 20 4f 4b 20 74 6f 20 64 65 6c 65  it is OK to dele
1beec 74 65 0a 2a 2a 20 61 20 72 65 63 6f 72 64 20 66  te.** a record f
1beed 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 4e 65  rom within an Ne
1beee 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49  xt loop..**.** I
1beef 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48  f the OPFLAG_NCH
1bef0 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 32 20  ANGE flag of P2 
1bef1 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  is set, then the
1bef2 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e   row change coun
1bef3 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e  t is.** incremen
1bef4 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e  ted (otherwise n
1bef5 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75  ot)..**.** P1 mu
1bef6 73 74 20 6e 6f 74 20 62 65 20 70 73 65 75 64 6f  st not be pseudo
1bef7 2d 74 61 62 6c 65 2e 20 20 49 74 20 68 61 73 20  -table.  It has 
1bef8 74 6f 20 62 65 20 61 20 72 65 61 6c 20 74 61 62  to be a real tab
1bef9 6c 65 20 77 69 74 68 0a 2a 2a 20 6d 75 6c 74 69  le with.** multi
1befa 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20  ple rows..**.** 
1befb 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c  If P4 is not NUL
1befc 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 68  L, then it is th
1befd 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
1befe 62 6c 65 20 74 68 61 74 20 50 31 20 69 73 0a 2a  ble that P1 is.*
1beff 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20  * pointing to.  
1bf00 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20  The update hook 
1bf01 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c  will be invoked,
1bf02 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a   if it exists..*
1bf03 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e  * If P4 is not N
1bf04 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 50 31 20  ULL then the P1 
1bf05 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65  cursor must have
1bf06 20 62 65 65 6e 20 70 6f 73 69 74 69 6f 6e 65 64   been positioned
1bf07 0a 2a 2a 20 75 73 69 6e 67 20 4f 50 5f 4e 6f 74  .** using OP_Not
1bf08 46 6f 75 6e 64 20 70 72 69 6f 72 20 74 6f 20 69  Found prior to i
1bf09 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 6f 70 63  nvoking this opc
1bf0a 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ode..*/.case OP_
1bf0b 44 65 6c 65 74 65 3a 20 7b 0a 20 20 69 6e 74 20  Delete: {.  int 
1bf0c 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69  i = pOp->p1;.  i
1bf0d 36 34 20 69 4b 65 79 3b 0a 20 20 56 64 62 65 43  64 iKey;.  VdbeC
1bf0e 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73  ursor *pC;..  as
1bf0f 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1bf10 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1bf11 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
1bf12 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d  ;.  assert( pC!=
1bf13 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1bf14 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b  C->pCursor!=0 );
1bf15 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69 64 20    /* Only valid 
1bf16 66 6f 72 20 72 65 61 6c 20 74 61 62 6c 65 73 2c  for real tables,
1bf17 20 6e 6f 20 70 73 65 75 64 6f 74 61 62 6c 65 73   no pseudotables
1bf18 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   */..  /* If the
1bf19 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 77 69 6c   update-hook wil
1bf1a 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 73 65  l be invoked, se
1bf1b 74 20 69 4b 65 79 20 74 6f 20 74 68 65 20 72 6f  t iKey to the ro
1bf1c 77 69 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  wid of the.  ** 
1bf1d 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65  row being delete
1bf1e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  d..  */.  if( db
1bf1f 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63  ->xUpdateCallbac
1bf20 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29  k && pOp->p4.z )
1bf21 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  {.    assert( pC
1bf22 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20  ->isTable );.   
1bf23 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 6f 77   assert( pC->row
1bf24 69 64 49 73 56 61 6c 69 64 20 29 3b 20 20 2f 2a  idIsValid );  /*
1bf25 20 6c 61 73 74 52 6f 77 69 64 20 73 65 74 20 62   lastRowid set b
1bf26 79 20 70 72 65 76 69 6f 75 73 20 4f 50 5f 4e 6f  y previous OP_No
1bf27 74 46 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 69 4b  tFound */.    iK
1bf28 65 79 20 3d 20 70 43 2d 3e 6c 61 73 74 52 6f 77  ey = pC->lastRow
1bf29 69 64 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  id;.  }..  rc = 
1bf2a 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
1bf2b 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 69  rMoveto(pC);.  i
1bf2c 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f  f( rc ) goto abo
1bf2d 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1bf2e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
1bf2f 74 72 65 65 44 65 6c 65 74 65 28 70 43 2d 3e 70  treeDelete(pC->p
1bf30 43 75 72 73 6f 72 29 3b 0a 20 20 70 43 2d 3e 6e  Cursor);.  pC->n
1bf31 65 78 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20  extRowidValid = 
1bf32 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74  0;.  pC->cacheSt
1bf33 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1bf34 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65  LE;..  /* Invoke
1bf35 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b   the update-hook
1bf36 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f   if required. */
1bf37 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1bf38 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64  E_OK && db->xUpd
1bf39 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70  ateCallback && p
1bf3a 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20  Op->p4.z ){.    
1bf3b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
1bf3c 3d 20 64 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 44  = db->aDb[pC->iD
1bf3d 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f  b].zName;.    co
1bf3e 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d  nst char *zTbl =
1bf3f 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20   pOp->p4.z;.    
1bf40 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62  db->xUpdateCallb
1bf41 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41  ack(db->pUpdateA
1bf42 72 67 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54  rg, SQLITE_DELET
1bf43 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b  E, zDb, zTbl, iK
1bf44 65 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ey);.    assert(
1bf45 20 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20   pC->iDb>=0 );. 
1bf46 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32   }.  if( pOp->p2
1bf47 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47   & OPFLAG_NCHANG
1bf48 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b  E ) p->nChange++
1bf49 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1bf4a 20 4f 70 63 6f 64 65 3a 20 52 65 73 65 74 43 6f   Opcode: ResetCo
1bf4b 75 6e 74 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a  unt P1 * *.**.**
1bf4c 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 73   This opcode res
1bf4d 65 74 73 20 74 68 65 20 56 4d 73 20 69 6e 74 65  ets the VMs inte
1bf4e 72 6e 61 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e  rnal change coun
1bf4f 74 65 72 20 74 6f 20 30 2e 20 49 66 20 50 31 20  ter to 0. If P1 
1bf50 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e  is true,.** then
1bf51 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1bf52 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  e change counter
1bf53 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68   is copied to th
1bf54 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1bf55 65 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e  e.** change coun
1bf56 74 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79  ter (returned by
1bf57 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
1bf58 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61  s to sqlite3_cha
1bf59 6e 67 65 73 28 29 29 0a 2a 2a 20 62 65 66 6f 72  nges()).** befor
1bf5a 65 20 69 74 20 69 73 20 72 65 73 65 74 2e 20 54  e it is reset. T
1bf5b 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74  his is used by t
1bf5c 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e  rigger programs.
1bf5d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 65  .*/.case OP_Rese
1bf5e 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 69 66 28 20  tCount: {.  if( 
1bf5f 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73  pOp->p1 ){.    s
1bf60 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61  qlite3VdbeSetCha
1bf61 6e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61  nges(db, p->nCha
1bf62 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e  nge);.  }.  p->n
1bf63 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 62 72  Change = 0;.  br
1bf64 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1bf65 65 3a 20 52 6f 77 44 61 74 61 20 50 31 20 50 32  e: RowData P1 P2
1bf66 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69   * * *.**.** Wri
1bf67 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  te into register
1bf68 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65 74 65   P2 the complete
1bf69 20 72 6f 77 20 64 61 74 61 20 66 6f 72 20 63 75   row data for cu
1bf6a 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72  rsor P1..** Ther
1bf6b 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65  e is no interpre
1bf6c 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61  tation of the da
1bf6d 74 61 2e 20 20 0a 2a 2a 20 49 74 20 69 73 20 6a  ta.  .** It is j
1bf6e 75 73 74 20 63 6f 70 69 65 64 20 6f 6e 74 6f 20  ust copied onto 
1bf6f 74 68 65 20 50 32 20 72 65 67 69 73 74 65 72 20  the P2 register 
1bf70 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20 69  exactly as .** i
1bf71 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68  t is found in th
1bf72 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1bf73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31  .**.** If the P1
1bf74 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20   cursor must be 
1bf75 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61  pointing to a va
1bf76 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 4e  lid row (not a N
1bf77 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20 61  ULL row).** of a
1bf78 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74   real table, not
1bf79 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e   a pseudo-table.
1bf7a 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .*/./* Opcode: R
1bf7b 6f 77 4b 65 79 20 50 31 20 50 32 20 2a 20 2a 20  owKey P1 P2 * * 
1bf7c 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e  *.**.** Write in
1bf7d 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74  to register P2 t
1bf7e 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20  he complete row 
1bf7f 6b 65 79 20 66 6f 72 20 63 75 72 73 6f 72 20 50  key for cursor P
1bf80 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e  1..** There is n
1bf81 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e  o interpretation
1bf82 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a   of the data.  .
1bf83 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 63 6f  ** The key is co
1bf84 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 33  pied onto the P3
1bf85 20 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c   register exactl
1bf86 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66  y as .** it is f
1bf87 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61  ound in the data
1bf88 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  base file..**.**
1bf89 20 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f   If the P1 curso
1bf8a 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69  r must be pointi
1bf8b 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f  ng to a valid ro
1bf8c 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f  w (not a NULL ro
1bf8d 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20  w).** of a real 
1bf8e 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65  table, not a pse
1bf8f 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61  udo-table..*/.ca
1bf90 73 65 20 4f 50 5f 52 6f 77 4b 65 79 3a 0a 63 61  se OP_RowKey:.ca
1bf91 73 65 20 4f 50 5f 52 6f 77 44 61 74 61 3a 20 7b  se OP_RowData: {
1bf92 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
1bf93 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  p1;.  VdbeCursor
1bf94 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72   *pC;.  BtCursor
1bf95 20 2a 70 43 72 73 72 3b 0a 20 20 75 33 32 20 6e   *pCrsr;.  u32 n
1bf96 3b 0a 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e  ;..  pOut = &p->
1bf97 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a  aMem[pOp->p2];..
1bf98 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 52    /* Note that R
1bf99 6f 77 4b 65 79 20 61 6e 64 20 52 6f 77 44 61 74  owKey and RowDat
1bf9a 61 20 61 72 65 20 72 65 61 6c 6c 79 20 65 78 61  a are really exa
1bf9b 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 69 6e  ctly the same in
1bf9c 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61  struction */.  a
1bf9d 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
1bf9e 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1bf9f 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69   pC = p->apCsr[i
1bfa0 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d  ];.  assert( pC-
1bfa1 3e 69 73 54 61 62 6c 65 20 7c 7c 20 70 4f 70 2d  >isTable || pOp-
1bfa2 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b  >opcode==OP_RowK
1bfa3 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ey );.  assert( 
1bfa4 70 43 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70  pC->isIndex || p
1bfa5 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52  Op->opcode==OP_R
1bfa6 6f 77 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65  owData );.  asse
1bfa7 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61  rt( pC!=0 );.  a
1bfa8 73 73 65 72 74 28 20 70 43 2d 3e 6e 75 6c 6c 52  ssert( pC->nullR
1bfa9 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ow==0 );.  asser
1bfaa 74 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  t( pC->pseudoTab
1bfab 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  le==0 );.  asser
1bfac 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  t( pC->pCursor!=
1bfad 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 70  0 );.  pCrsr = p
1bfae 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 72 63  C->pCursor;.  rc
1bfaf 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
1bfb0 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a  rsorMoveto(pC);.
1bfb1 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
1bfb2 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1bfb3 6f 72 3b 0a 20 20 69 66 28 20 70 43 2d 3e 69 73  or;.  if( pC->is
1bfb4 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 69 36 34  Index ){.    i64
1bfb5 20 6e 36 34 3b 0a 20 20 20 20 61 73 73 65 72 74   n64;.    assert
1bfb6 28 20 21 70 43 2d 3e 69 73 54 61 62 6c 65 20 29  ( !pC->isTable )
1bfb7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1bfb8 65 65 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c  eeKeySize(pCrsr,
1bfb9 20 26 6e 36 34 29 3b 0a 20 20 20 20 69 66 28 20   &n64);.    if( 
1bfba 6e 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  n64>db->aLimit[S
1bfbb 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
1bfbc 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  TH] ){.      got
1bfbd 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d  o too_big;.    }
1bfbe 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 6e 36  .    n = (int)n6
1bfbf 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  4;.  }else{.    
1bfc0 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
1bfc1 53 69 7a 65 28 70 43 72 73 72 2c 20 26 6e 29 3b  Size(pCrsr, &n);
1bfc2 0a 20 20 20 20 69 66 28 20 28 69 6e 74 29 6e 3e  .    if( (int)n>
1bfc3 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1bfc4 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
1bfc5 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f  ){.      goto to
1bfc6 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d  o_big;.    }.  }
1bfc7 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
1bfc8 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20  beMemGrow(pOut, 
1bfc9 6e 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74  n, 0) ){.    got
1bfca 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20  o no_mem;.  }.  
1bfcb 70 4f 75 74 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 4d  pOut->n = n;.  M
1bfcc 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
1bfcd 75 74 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20  ut, MEM_Blob);. 
1bfce 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78   if( pC->isIndex
1bfcf 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1bfd0 69 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 72  ite3BtreeKey(pCr
1bfd1 73 72 2c 20 30 2c 20 6e 2c 20 70 4f 75 74 2d 3e  sr, 0, n, pOut->
1bfd2 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  z);.  }else{.   
1bfd3 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1bfd4 65 65 44 61 74 61 28 70 43 72 73 72 2c 20 30 2c  eeData(pCrsr, 0,
1bfd5 20 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20   n, pOut->z);.  
1bfd6 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20  }.  pOut->enc = 
1bfd7 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a  SQLITE_UTF8;  /*
1bfd8 20 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f   In case the blo
1bfd9 62 20 69 73 20 65 76 65 72 20 63 61 73 74 20 74  b is ever cast t
1bfda 6f 20 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41  o text */.  UPDA
1bfdb 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1bfdc 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pOut);.  break;.
1bfdd 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f  }../* Opcode: Ro
1bfde 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  wid P1 P2 * * *.
1bfdf 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72  **.** Store in r
1bfe0 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e  egister P2 an in
1bfe1 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
1bfe2 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61  he key of the ta
1bfe3 62 6c 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a  ble entry that.*
1bfe4 2a 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c  * P1 is currentl
1bfe5 79 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2f 0a 63  y point to..*/.c
1bfe6 61 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b 20  ase OP_Rowid: { 
1bfe7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bfe8 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1bfe9 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20  se */.  int i = 
1bfea 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43  pOp->p1;.  VdbeC
1bfeb 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34  ursor *pC;.  i64
1bfec 20 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69   v;..  assert( i
1bfed 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72  >=0 && i<p->nCur
1bfee 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d  sor );.  pC = p-
1bfef 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73  >apCsr[i];.  ass
1bff0 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20  ert( pC!=0 );.  
1bff1 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1bff2 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29  CursorMoveto(pC)
1bff3 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74  ;.  if( rc ) got
1bff4 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1bff5 72 72 6f 72 3b 0a 20 20 69 66 28 20 70 43 2d 3e  rror;.  if( pC->
1bff6 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 7b 0a  rowidIsValid ){.
1bff7 20 20 20 20 76 20 3d 20 70 43 2d 3e 6c 61 73 74      v = pC->last
1bff8 52 6f 77 69 64 3b 0a 20 20 7d 65 6c 73 65 20 69  Rowid;.  }else i
1bff9 66 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  f( pC->pseudoTab
1bffa 6c 65 20 29 7b 0a 20 20 20 20 76 20 3d 20 6b 65  le ){.    v = ke
1bffb 79 54 6f 49 6e 74 28 70 43 2d 3e 69 4b 65 79 29  yToInt(pC->iKey)
1bffc 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43  ;.  }else if( pC
1bffd 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20  ->nullRow ){.   
1bffe 20 2f 2a 20 4c 65 61 76 65 20 74 68 65 20 72 6f   /* Leave the ro
1bfff 77 69 64 20 73 65 74 20 74 6f 20 61 20 4e 55 4c  wid set to a NUL
1c000 4c 20 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a  L */.    break;.
1c001 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1c002 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72  ert( pC->pCursor
1c003 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  !=0 );.    sqlit
1c004 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70  e3BtreeKeySize(p
1c005 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 76 29 3b  C->pCursor, &v);
1c006 0a 20 20 20 20 76 20 3d 20 6b 65 79 54 6f 49 6e  .    v = keyToIn
1c007 74 28 76 29 3b 0a 20 20 7d 0a 20 20 70 4f 75 74  t(v);.  }.  pOut
1c008 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 4d 65 6d  ->u.i = v;.  Mem
1c009 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1c00a 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72  , MEM_Int);.  br
1c00b 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1c00c 65 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20  e: NullRow P1 * 
1c00d 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65  * * *.**.** Move
1c00e 20 74 68 65 20 63 75 72 73 6f 72 20 50 31 20 74   the cursor P1 t
1c00f 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41  o a null row.  A
1c010 6e 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65  ny OP_Column ope
1c011 72 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20  rations.** that 
1c012 6f 63 63 75 72 20 77 68 69 6c 65 20 74 68 65 20  occur while the 
1c013 63 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65  cursor is on the
1c014 20 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61   null row will a
1c015 6c 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61  lways.** write a
1c016 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f   NULL..*/.case O
1c017 50 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 20 20 69  P_NullRow: {.  i
1c018 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
1c019 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c01a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  ;..  assert( i>=
1c01b 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
1c01c 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61  r );.  pC = p->a
1c01d 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72  pCsr[i];.  asser
1c01e 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 70 43  t( pC!=0 );.  pC
1c01f 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20  ->nullRow = 1;. 
1c020 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69   pC->rowidIsVali
1c021 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 2d  d = 0;.  if( pC-
1c022 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20  >pCursor ){.    
1c023 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61  sqlite3BtreeClea
1c024 72 43 75 72 73 6f 72 28 70 43 2d 3e 70 43 75 72  rCursor(pC->pCur
1c025 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  sor);.  }.  brea
1c026 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1c027 20 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20   Last P1 P2 * * 
1c028 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  *.**.** The next
1c029 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69   use of the Rowi
1c02a 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e  d or Column or N
1c02b 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1c02c 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20  for P1 .** will 
1c02d 72 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73  refer to the las
1c02e 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64  t entry in the d
1c02f 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
1c030 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68   index..** If th
1c031 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
1c032 20 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32   is empty and P2
1c033 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d  >0, then jump im
1c034 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e  mediately to P2.
1c035 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f  .** If P2 is 0 o
1c036 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f  r if the table o
1c037 72 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65  r index is not e
1c038 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75  mpty, fall throu
1c039 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c  gh.** to the fol
1c03a 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69  lowing instructi
1c03b 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c  on..*/.case OP_L
1c03c 61 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  ast: {        /*
1c03d 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69   jump */.  int i
1c03e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64   = pOp->p1;.  Vd
1c03f 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1c040 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1c041 0a 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61  .  int res;..  a
1c042 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
1c043 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1c044 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69   pC = p->apCsr[i
1c045 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21  ];.  assert( pC!
1c046 3d 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20  =0 );.  pCrsr = 
1c047 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61  pC->pCursor;.  a
1c048 73 73 65 72 74 28 20 70 43 72 73 72 21 3d 30 20  ssert( pCrsr!=0 
1c049 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1c04a 33 42 74 72 65 65 4c 61 73 74 28 70 43 72 73 72  3BtreeLast(pCrsr
1c04b 2c 20 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e  , &res);.  pC->n
1c04c 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72 65 73  ullRow = (u8)res
1c04d 3b 0a 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64  ;.  pC->deferred
1c04e 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 70 43  Moveto = 0;.  pC
1c04f 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
1c050 20 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53   0;.  pC->cacheS
1c051 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1c052 41 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 20 26  ALE;.  if( res &
1c053 26 20 70 4f 70 2d 3e 70 32 3e 30 20 29 7b 0a 20  & pOp->p2>0 ){. 
1c054 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1c055 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  - 1;.  }.  break
1c056 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ;.}.../* Opcode:
1c057 20 53 6f 72 74 20 50 31 20 50 32 20 2a 20 2a 20   Sort P1 P2 * * 
1c058 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  *.**.** This opc
1c059 6f 64 65 20 64 6f 65 73 20 65 78 61 63 74 6c 79  ode does exactly
1c05a 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20   the same thing 
1c05b 61 73 20 4f 50 5f 52 65 77 69 6e 64 20 65 78 63  as OP_Rewind exc
1c05c 65 70 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69  ept that.** it i
1c05d 6e 63 72 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64  ncrements an und
1c05e 6f 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c  ocumented global
1c05f 20 76 61 72 69 61 62 6c 65 20 75 73 65 64 20 66   variable used f
1c060 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a  or testing..**.*
1c061 2a 20 53 6f 72 74 69 6e 67 20 69 73 20 61 63 63  * Sorting is acc
1c062 6f 6d 70 6c 69 73 68 65 64 20 62 79 20 77 72 69  omplished by wri
1c063 74 69 6e 67 20 72 65 63 6f 72 64 73 20 69 6e 74  ting records int
1c064 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  o a sorting inde
1c065 78 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e  x,.** then rewin
1c066 64 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20  ding that index 
1c067 61 6e 64 20 70 6c 61 79 69 6e 67 20 69 74 20 62  and playing it b
1c068 61 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69  ack from beginni
1c069 6e 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57  ng to.** end.  W
1c06a 65 20 75 73 65 20 74 68 65 20 4f 50 5f 53 6f 72  e use the OP_Sor
1c06b 74 20 6f 70 63 6f 64 65 20 69 6e 73 74 65 61 64  t opcode instead
1c06c 20 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f   of OP_Rewind to
1c06d 20 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e   do the.** rewin
1c06e 64 69 6e 67 20 73 6f 20 74 68 61 74 20 74 68 65  ding so that the
1c06f 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
1c070 20 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65   will be increme
1c071 6e 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72  nted and.** regr
1c072 65 73 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e  ession tests can
1c073 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   determine wheth
1c074 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70  er or not the op
1c075 74 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f  timizer is.** co
1c076 72 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69  rrectly optimizi
1c077 6e 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f  ng out sorts..*/
1c078 0a 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b  .case OP_Sort: {
1c079 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20          /* jump 
1c07a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1c07b 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f  _TEST.  sqlite3_
1c07c 73 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20  sort_count++;.  
1c07d 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63  sqlite3_search_c
1c07e 6f 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20  ount--;.#endif. 
1c07f 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c   p->aCounter[SQL
1c080 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53  ITE_STMTSTATUS_S
1c081 4f 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46  ORT-1]++;.  /* F
1c082 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f  all through into
1c083 20 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a   OP_Rewind */.}.
1c084 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e  /* Opcode: Rewin
1c085 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  d P1 P2 * * *.**
1c086 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65  .** The next use
1c087 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72   of the Rowid or
1c088 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20   Column or Next 
1c089 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20  instruction for 
1c08a 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65  P1 .** will refe
1c08b 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  r to the first e
1c08c 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61  ntry in the data
1c08d 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e  base table or in
1c08e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74  dex..** If the t
1c08f 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73  able or index is
1c090 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c   empty and P2>0,
1c091 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64   then jump immed
1c092 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a  iately to P2..**
1c093 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69   If P2 is 0 or i
1c094 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  f the table or i
1c095 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74  ndex is not empt
1c096 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a  y, fall through.
1c097 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  ** to the follow
1c098 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ing instruction.
1c099 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 77 69  .*/.case OP_Rewi
1c09a 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  nd: {        /* 
1c09b 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20  jump */.  int i 
1c09c 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62  = pOp->p1;.  Vdb
1c09d 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1c09e 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c09f 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73    int res;..  as
1c0a0 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1c0a1 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1c0a2 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
1c0a3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d  ;.  assert( pC!=
1c0a4 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73  0 );.  if( (pCrs
1c0a5 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 29  r = pC->pCursor)
1c0a6 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  !=0 ){.    rc = 
1c0a7 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73  sqlite3BtreeFirs
1c0a8 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a  t(pCrsr, &res);.
1c0a9 20 20 20 20 70 43 2d 3e 61 74 46 69 72 73 74 20      pC->atFirst 
1c0aa 3d 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20  = res==0 ?1:0;. 
1c0ab 20 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d     pC->deferredM
1c0ac 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70  oveto = 0;.    p
1c0ad 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1c0ae 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1c0af 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c    pC->rowidIsVal
1c0b0 69 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  id = 0;.  }else{
1c0b1 0a 20 20 20 20 72 65 73 20 3d 20 31 3b 0a 20 20  .    res = 1;.  
1c0b2 7d 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  }.  pC->nullRow 
1c0b3 3d 20 28 75 38 29 72 65 73 3b 0a 20 20 61 73 73  = (u8)res;.  ass
1c0b4 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26  ert( pOp->p2>0 &
1c0b5 26 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70  & pOp->p2<p->nOp
1c0b6 20 29 3b 0a 20 20 69 66 28 20 72 65 73 20 29 7b   );.  if( res ){
1c0b7 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1c0b8 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
1c0b9 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1c0ba 3a 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a  : Next P1 P2 * *
1c0bb 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65   *.**.** Advance
1c0bc 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
1c0bd 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
1c0be 74 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61 74  the next key/dat
1c0bf 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a  a pair in its.**
1c0c0 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e   table or index.
1c0c1 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e    If there are n
1c0c2 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65  o more key/value
1c0c3 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c   pairs then fall
1c0c4 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74   through.** to t
1c0c5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73  he following ins
1c0c6 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69  truction.  But i
1c0c7 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 76  f the cursor adv
1c0c8 61 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 73  ance was success
1c0c9 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d  ful,.** jump imm
1c0ca 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
1c0cb 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72  **.** The P1 cur
1c0cc 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20  sor must be for 
1c0cd 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f  a real table, no
1c0ce 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  t a pseudo-table
1c0cf 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1c0d0 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63  : Prev.*/./* Opc
1c0d1 6f 64 65 3a 20 50 72 65 76 20 50 31 20 50 32 20  ode: Prev P1 P2 
1c0d2 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b  * * *.**.** Back
1c0d3 20 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 6f   up cursor P1 so
1c0d4 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
1c0d5 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  to the previous 
1c0d6 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e  key/data pair in
1c0d7 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72   its.** table or
1c0d8 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72   index.  If ther
1c0d9 65 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73  e is no previous
1c0da 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73   key/value pairs
1c0db 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75   then fall throu
1c0dc 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c  gh.** to the fol
1c0dd 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69  lowing instructi
1c0de 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20  on.  But if the 
1c0df 63 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 61  cursor backup wa
1c0e0 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a  s successful,.**
1c0e1 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c   jump immediatel
1c0e2 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54  y to P2..**.** T
1c0e3 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73  he P1 cursor mus
1c0e4 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20  t be for a real 
1c0e5 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65  table, not a pse
1c0e6 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61  udo-table..*/.ca
1c0e7 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 20  se OP_Prev:     
1c0e8 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1c0e9 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20  case OP_Next: { 
1c0ea 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
1c0eb 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1c0ec 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  pC;.  BtCursor *
1c0ed 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73  pCrsr;.  int res
1c0ee 3b 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49  ;..  CHECK_FOR_I
1c0ef 4e 54 45 52 52 55 50 54 3b 0a 20 20 61 73 73 65  NTERRUPT;.  asse
1c0f0 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1c0f1 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1c0f2 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70  rsor );.  pC = p
1c0f3 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1c0f4 3b 0a 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b  ;.  if( pC==0 ){
1c0f5 0a 20 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20  .    break;  /* 
1c0f6 53 65 65 20 74 69 63 6b 65 74 20 23 32 32 37 33  See ticket #2273
1c0f7 20 2a 2f 0a 20 20 7d 0a 20 20 70 43 72 73 72 20   */.  }.  pCrsr 
1c0f8 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  = pC->pCursor;. 
1c0f9 20 61 73 73 65 72 74 28 20 70 43 72 73 72 20 29   assert( pCrsr )
1c0fa 3b 0a 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 61  ;.  res = 1;.  a
1c0fb 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72  ssert( pC->defer
1c0fc 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a  redMoveto==0 );.
1c0fd 20 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f    rc = pOp->opco
1c0fe 64 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71  de==OP_Next ? sq
1c0ff 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70  lite3BtreeNext(p
1c100 43 72 73 72 2c 20 26 72 65 73 29 20 3a 0a 20 20  Crsr, &res) :.  
1c101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c102 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1c103 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
1c104 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a 20  (pCrsr, &res);. 
1c105 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28   pC->nullRow = (
1c106 75 38 29 72 65 73 3b 0a 20 20 70 43 2d 3e 63 61  u8)res;.  pC->ca
1c107 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1c108 45 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 72  E_STALE;.  if( r
1c109 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20  es==0 ){.    pc 
1c10a 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1c10b 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29     if( pOp->p5 )
1c10c 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70   p->aCounter[pOp
1c10d 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65  ->p5-1]++;.#ifde
1c10e 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
1c10f 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68    sqlite3_search
1c110 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
1c111 0a 20 20 7d 0a 20 20 70 43 2d 3e 72 6f 77 69 64  .  }.  pC->rowid
1c112 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 62  IsValid = 0;.  b
1c113 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1c114 64 65 3a 20 49 64 78 49 6e 73 65 72 74 20 50 31  de: IdxInsert P1
1c115 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1c116 20 52 65 67 69 73 74 65 72 20 50 32 20 68 6f 6c   Register P2 hol
1c117 64 73 20 61 20 53 51 4c 20 69 6e 64 65 78 20 6b  ds a SQL index k
1c118 65 79 20 6d 61 64 65 20 75 73 69 6e 67 20 74 68  ey made using th
1c119 65 0a 2a 2a 20 4d 61 6b 65 49 64 78 52 65 63 20  e.** MakeIdxRec 
1c11a 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 54  instructions.  T
1c11b 68 69 73 20 6f 70 63 6f 64 65 20 77 72 69 74 65  his opcode write
1c11c 73 20 74 68 61 74 20 6b 65 79 0a 2a 2a 20 69 6e  s that key.** in
1c11d 74 6f 20 74 68 65 20 69 6e 64 65 78 20 50 31 2e  to the index P1.
1c11e 20 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 65    Data for the e
1c11f 6e 74 72 79 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a  ntry is nil..**.
1c120 2a 2a 20 50 33 20 69 73 20 61 20 66 6c 61 67 20  ** P3 is a flag 
1c121 74 68 61 74 20 70 72 6f 76 69 64 65 73 20 61 20  that provides a 
1c122 68 69 6e 74 20 74 6f 20 74 68 65 20 62 2d 74 72  hint to the b-tr
1c123 65 65 20 6c 61 79 65 72 20 74 68 61 74 20 74 68  ee layer that th
1c124 69 73 0a 2a 2a 20 69 6e 73 65 72 74 20 69 73 20  is.** insert is 
1c125 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20  likely to be an 
1c126 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  append..**.** Th
1c127 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  is instruction o
1c128 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 69 6e  nly works for in
1c129 64 69 63 65 73 2e 20 20 54 68 65 20 65 71 75 69  dices.  The equi
1c12a 76 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69  valent instructi
1c12b 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61 62 6c 65 73  on.** for tables
1c12c 20 69 73 20 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a   is OP_Insert..*
1c12d 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 49 6e 73  /.case OP_IdxIns
1c12e 65 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  ert: {        /*
1c12f 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20 69 20   in2 */.  int i 
1c130 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62  = pOp->p1;.  Vdb
1c131 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1c132 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c133 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1c134 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1c135 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
1c136 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  pCsr[i]!=0 );.  
1c137 61 73 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c  assert( pIn2->fl
1c138 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29  ags & MEM_Blob )
1c139 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d  ;.  if( (pCrsr =
1c13a 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b   (pC = p->apCsr[
1c13b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30  i])->pCursor)!=0
1c13c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1c13d 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29  pC->isTable==0 )
1c13e 3b 0a 20 20 20 20 72 63 20 3d 20 45 78 70 61 6e  ;.    rc = Expan
1c13f 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 20  dBlob(pIn2);.   
1c140 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1c141 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
1c142 6e 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0a  nKey = pIn2->n;.
1c143 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1c144 20 2a 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a   *zKey = pIn2->z
1c145 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
1c146 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28  ite3BtreeInsert(
1c147 70 43 72 73 72 2c 20 7a 4b 65 79 2c 20 6e 4b 65  pCrsr, zKey, nKe
1c148 79 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70  y, "", 0, 0, pOp
1c149 2d 3e 70 33 29 3b 0a 20 20 20 20 20 20 61 73 73  ->p3);.      ass
1c14a 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65  ert( pC->deferre
1c14b 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
1c14c 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61      pC->cacheSta
1c14d 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1c14e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  E;.    }.  }.  b
1c14f 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1c150 64 65 3a 20 49 64 78 44 65 6c 65 74 65 20 50 31  de: IdxDelete P1
1c151 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1c152 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   The content of 
1c153 50 33 20 72 65 67 69 73 74 65 72 73 20 73 74 61  P3 registers sta
1c154 72 74 69 6e 67 20 61 74 20 72 65 67 69 73 74 65  rting at registe
1c155 72 20 50 32 20 66 6f 72 6d 0a 2a 2a 20 61 6e 20  r P2 form.** an 
1c156 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b  unpacked index k
1c157 65 79 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20  ey. This opcode 
1c158 72 65 6d 6f 76 65 73 20 74 68 61 74 20 65 6e 74  removes that ent
1c159 72 79 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20  ry from the .** 
1c15a 69 6e 64 65 78 20 6f 70 65 6e 65 64 20 62 79 20  index opened by 
1c15b 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2f 0a 63 61  cursor P1..*/.ca
1c15c 73 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 3a  se OP_IdxDelete:
1c15d 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70   {.  int i = pOp
1c15e 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73  ->p1;.  VdbeCurs
1c15f 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73  or *pC;.  BtCurs
1c160 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73  or *pCrsr;.  ass
1c161 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29  ert( pOp->p3>0 )
1c162 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1c163 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32  >p2>0 && pOp->p2
1c164 2b 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65  +pOp->p3<=p->nMe
1c165 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  m );.  assert( i
1c166 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72  >=0 && i<p->nCur
1c167 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  sor );.  assert(
1c168 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20   p->apCsr[i]!=0 
1c169 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20  );.  if( (pCrsr 
1c16a 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  = (pC = p->apCsr
1c16b 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d  [i])->pCursor)!=
1c16c 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73  0 ){.    int res
1c16d 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65  ;.    UnpackedRe
1c16e 63 6f 72 64 20 72 3b 0a 20 20 20 20 72 2e 70 4b  cord r;.    r.pK
1c16f 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65  eyInfo = pC->pKe
1c170 79 49 6e 66 6f 3b 0a 20 20 20 20 72 2e 6e 46 69  yInfo;.    r.nFi
1c171 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e  eld = (u16)pOp->
1c172 70 33 3b 0a 20 20 20 20 72 2e 66 6c 61 67 73 20  p3;.    r.flags 
1c173 3d 20 30 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20  = 0;.    r.aMem 
1c174 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1c175 70 32 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  p2];.    rc = sq
1c176 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
1c177 55 6e 70 61 63 6b 65 64 28 70 43 72 73 72 2c 20  Unpacked(pCrsr, 
1c178 26 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b  &r, 0, 0, &res);
1c179 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1c17a 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30  ITE_OK && res==0
1c17b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
1c17c 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74  qlite3BtreeDelet
1c17d 65 28 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a  e(pCrsr);.    }.
1c17e 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
1c17f 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d  deferredMoveto==
1c180 30 20 29 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63  0 );.    pC->cac
1c181 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1c182 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72  _STALE;.  }.  br
1c183 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1c184 65 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50  e: IdxRowid P1 P
1c185 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72  2 * * *.**.** Wr
1c186 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ite into registe
1c187 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20  r P2 an integer 
1c188 77 68 69 63 68 20 69 73 20 74 68 65 20 6c 61 73  which is the las
1c189 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72  t entry in the r
1c18a 65 63 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20  ecord at.** the 
1c18b 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78  end of the index
1c18c 20 6b 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20   key pointed to 
1c18d 62 79 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54  by cursor P1.  T
1c18e 68 69 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75  his integer shou
1c18f 6c 64 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77  ld be.** the row
1c190 69 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  id of the table 
1c191 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74  entry to which t
1c192 68 69 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20  his index entry 
1c193 70 6f 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65  points..**.** Se
1c194 65 20 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d  e also: Rowid, M
1c195 61 6b 65 49 64 78 52 65 63 2e 0a 2a 2f 0a 63 61  akeIdxRec..*/.ca
1c196 73 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20  se OP_IdxRowid: 
1c197 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  {              /
1c198 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1c199 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  e */.  int i = p
1c19a 4f 70 2d 3e 70 31 3b 0a 20 20 42 74 43 75 72 73  Op->p1;.  BtCurs
1c19b 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 56 64 62  or *pCrsr;.  Vdb
1c19c 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20  eCursor *pC;..  
1c19d 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
1c19e 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  i<p->nCursor );.
1c19f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43    assert( p->apC
1c1a0 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66  sr[i]!=0 );.  if
1c1a1 28 20 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d  ( (pCrsr = (pC =
1c1a2 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70   p->apCsr[i])->p
1c1a3 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20  Cursor)!=0 ){.  
1c1a4 20 20 69 36 34 20 72 6f 77 69 64 3b 0a 0a 20 20    i64 rowid;..  
1c1a5 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65    assert( pC->de
1c1a6 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20  ferredMoveto==0 
1c1a7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1c1a8 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b  C->isTable==0 );
1c1a9 0a 20 20 20 20 69 66 28 20 21 70 43 2d 3e 6e 75  .    if( !pC->nu
1c1aa 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72  llRow ){.      r
1c1ab 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  c = sqlite3VdbeI
1c1ac 64 78 52 6f 77 69 64 28 70 43 72 73 72 2c 20 26  dxRowid(pCrsr, &
1c1ad 72 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66  rowid);.      if
1c1ae 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1c1af 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
1c1b0 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1c1b1 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  or;.      }.    
1c1b2 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1c1b3 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1c1b4 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69  .      pOut->u.i
1c1b5 20 3d 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a   = rowid;.    }.
1c1b6 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1c1b7 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 47 45  /* Opcode: IdxGE
1c1b8 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1c1b9 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67  **.** The P4 reg
1c1ba 69 73 74 65 72 20 76 61 6c 75 65 73 20 62 65 67  ister values beg
1c1bb 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66  inning with P3 f
1c1bc 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20  orm an unpacked 
1c1bd 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68  index .** key th
1c1be 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57  at omits the ROW
1c1bf 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69  ID.  Compare thi
1c1c0 73 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69  s key value agai
1c1c1 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a  nst the index .*
1c1c2 2a 20 74 68 61 74 20 50 31 20 69 73 20 63 75 72  * that P1 is cur
1c1c3 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
1c1c4 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65  to, ignoring the
1c1c5 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31   ROWID on the P1
1c1c6 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66   index..**.** If
1c1c7 20 74 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e   the P1 index en
1c1c8 74 72 79 20 69 73 20 67 72 65 61 74 65 72 20 74  try is greater t
1c1c9 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
1c1ca 74 68 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a  the key value.**
1c1cb 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
1c1cc 2e 20 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c  .  Otherwise fal
1c1cd 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
1c1ce 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1c1cf 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69  n..**.** If P5 i
1c1d0 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20  s non-zero then 
1c1d1 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73  the key value is
1c1d2 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e   increased by an
1c1d3 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69   epsilon .** pri
1c1d4 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72  or to the compar
1c1d5 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65  ison.  This make
1c1d6 20 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b   the opcode work
1c1d7 20 6c 69 6b 65 20 49 64 78 47 54 20 65 78 63 65   like IdxGT exce
1c1d8 70 74 0a 2a 2a 20 74 68 61 74 20 69 66 20 74 68  pt.** that if th
1c1d9 65 20 6b 65 79 20 66 72 6f 6d 20 72 65 67 69 73  e key from regis
1c1da 74 65 72 20 50 33 20 69 73 20 61 20 70 72 65 66  ter P3 is a pref
1c1db 69 78 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e  ix of the key in
1c1dc 20 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20   the cursor,.** 
1c1dd 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 66 61  the result is fa
1c1de 6c 73 65 20 77 68 65 72 65 61 73 20 69 74 20 77  lse whereas it w
1c1df 6f 75 6c 64 20 62 65 20 74 72 75 65 20 77 69 74  ould be true wit
1c1e0 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f  h IdxGT..*/./* O
1c1e1 70 63 6f 64 65 3a 20 49 64 78 4c 54 20 50 31 20  pcode: IdxLT P1 
1c1e2 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a  P2 P3 * P5.**.**
1c1e3 20 54 68 65 20 50 34 20 72 65 67 69 73 74 65 72   The P4 register
1c1e4 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e   values beginnin
1c1e5 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61  g with P3 form a
1c1e6 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78  n unpacked index
1c1e7 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d   .** key that om
1c1e8 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20  its the ROWID.  
1c1e9 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79  Compare this key
1c1ea 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74   value against t
1c1eb 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61  he index .** tha
1c1ec 74 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c  t P1 is currentl
1c1ed 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69  y pointing to, i
1c1ee 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49  gnoring the ROWI
1c1ef 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65  D on the P1 inde
1c1f0 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  x..**.** If the 
1c1f1 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69  P1 index entry i
1c1f2 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  s less than the 
1c1f3 6b 65 79 20 76 61 6c 75 65 20 74 68 65 6e 20 6a  key value then j
1c1f4 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74  ump to P2..** Ot
1c1f5 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72  herwise fall thr
1c1f6 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
1c1f7 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
1c1f8 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e  .** If P5 is non
1c1f9 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b  -zero then the k
1c1fa 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72  ey value is incr
1c1fb 65 61 73 65 64 20 62 79 20 61 6e 20 65 70 73 69  eased by an epsi
1c1fc 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f  lon prior .** to
1c1fd 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e   the comparison.
1c1fe 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65    This makes the
1c1ff 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b   opcode work lik
1c200 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65  e IdxLE..*/.case
1c201 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 20   OP_IdxLT:      
1c202 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33      /* jump, in3
1c203 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 47   */.case OP_IdxG
1c204 45 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  E: {        /* j
1c205 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e  ump, in3 */.  in
1c206 74 20 69 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  t i= pOp->p1;.  
1c207 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c208 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1c209 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
1c20a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
1c20b 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20  apCsr[i]!=0 );. 
1c20c 20 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70   if( (pC = p->ap
1c20d 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72  Csr[i])->pCursor
1c20e 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  !=0 ){.    int r
1c20f 65 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64  es;.    Unpacked
1c210 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 61 73  Record r;.    as
1c211 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72  sert( pC->deferr
1c212 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20  edMoveto==0 );. 
1c213 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1c214 70 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35  p5==0 || pOp->p5
1c215 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==1 );.    asser
1c216 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1c217 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20  P4_INT32 );.    
1c218 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d  r.pKeyInfo = pC-
1c219 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72  >pKeyInfo;.    r
1c21a 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70  .nField = (u16)p
1c21b 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66  Op->p4.i;.    if
1c21c 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20  ( pOp->p5 ){.   
1c21d 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50     r.flags = UNP
1c21e 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c 20  ACKED_INCRKEY | 
1c21f 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f  UNPACKED_IGNORE_
1c220 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65 6c 73 65  ROWID;.    }else
1c221 7b 0a 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20  {.      r.flags 
1c222 3d 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52  = UNPACKED_IGNOR
1c223 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20  E_ROWID;.    }. 
1c224 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e     r.aMem = &p->
1c225 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1c226 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1c227 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65  dbeIdxKeyCompare
1c228 28 70 43 2c 20 26 72 2c 20 26 72 65 73 29 3b 0a  (pC, &r, &res);.
1c229 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63      if( pOp->opc
1c22a 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b  ode==OP_IdxLT ){
1c22b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 2d 72 65  .      res = -re
1c22c 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  s;.    }else{.  
1c22d 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1c22e 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 47  >opcode==OP_IdxG
1c22f 45 20 29 3b 0a 20 20 20 20 20 20 72 65 73 2b 2b  E );.      res++
1c230 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1c231 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70  res>0 ){.      p
1c232 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20  c = pOp->p2 - 1 
1c233 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
1c234 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1c235 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 32  e: Destroy P1 P2
1c236 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65   P3 * *.**.** De
1c237 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64  lete an entire d
1c238 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
1c239 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f   index whose roo
1c23a 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61  t page in the da
1c23b 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69  tabase.** file i
1c23c 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a  s given by P1..*
1c23d 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62  *.** The table b
1c23e 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69  eing destroyed i
1c23f 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  s in the main da
1c240 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50  tabase file if P
1c241 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d  3==0.  If.** P3=
1c242 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c  =1 then the tabl
1c243 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73  e to be clear is
1c244 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72   in the auxiliar
1c245 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  y database file.
1c246 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20  ** that is used 
1c247 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20  to store tables 
1c248 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45  create using CRE
1c249 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41  ATE TEMPORARY TA
1c24a 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55  BLE..**.** If AU
1c24b 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62  TOVACUUM is enab
1c24c 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 70  led then it is p
1c24d 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f  ossible that ano
1c24e 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a  ther root page.*
1c24f 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64  * might be moved
1c250 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20   into the newly 
1c251 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67  deleted root pag
1c252 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65  e in order to ke
1c253 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70  ep all.** root p
1c254 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20  ages contiguous 
1c255 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
1c256 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1c257 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a  .  The former.**
1c258 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f   value of the ro
1c259 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76  ot page that mov
1c25a 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62  ed - its value b
1c25b 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f  efore the move o
1c25c 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20  ccurred -.** is 
1c25d 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1c25e 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61  er P2.  If no pa
1c25f 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20  ge .** movement 
1c260 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62 65  was required (be
1c261 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20  cause the table 
1c262 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61  being dropped wa
1c263 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68  s already .** th
1c264 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68  e last one in th
1c265 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65 6e  e database) then
1c266 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65   a zero is store
1c267 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  d in register P2
1c268 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55  ..** If AUTOVACU
1c269 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 74  UM is disabled t
1c26a 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74  hen a zero is st
1c26b 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
1c26c 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
1c26d 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61  lso: Clear.*/.ca
1c26e 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b  se OP_Destroy: {
1c26f 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1c270 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74  release */.  int
1c271 20 69 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69   iMoved;.  int i
1c272 43 6e 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  Cnt;.#ifndef SQL
1c273 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1c274 54 41 42 4c 45 0a 20 20 56 64 62 65 20 2a 70 56  TABLE.  Vdbe *pV
1c275 64 62 65 3b 0a 20 20 69 43 6e 74 20 3d 20 30 3b  dbe;.  iCnt = 0;
1c276 0a 20 20 66 6f 72 28 70 56 64 62 65 3d 64 62 2d  .  for(pVdbe=db-
1c277 3e 70 56 64 62 65 3b 20 70 56 64 62 65 3b 20 70  >pVdbe; pVdbe; p
1c278 56 64 62 65 3d 70 56 64 62 65 2d 3e 70 4e 65 78  Vdbe=pVdbe->pNex
1c279 74 29 7b 0a 20 20 20 20 69 66 28 20 70 56 64 62  t){.    if( pVdb
1c27a 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  e->magic==VDBE_M
1c27b 41 47 49 43 5f 52 55 4e 20 26 26 20 70 56 64 62  AGIC_RUN && pVdb
1c27c 65 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 3c  e->inVtabMethod<
1c27d 32 20 26 26 20 70 56 64 62 65 2d 3e 70 63 3e 3d  2 && pVdbe->pc>=
1c27e 30 20 29 7b 0a 20 20 20 20 20 20 69 43 6e 74 2b  0 ){.      iCnt+
1c27f 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c  +;.    }.  }.#el
1c280 73 65 0a 20 20 69 43 6e 74 20 3d 20 64 62 2d 3e  se.  iCnt = db->
1c281 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 0a 23  activeVdbeCnt;.#
1c282 65 6e 64 69 66 0a 20 20 69 66 28 20 69 43 6e 74  endif.  if( iCnt
1c283 3e 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  >1 ){.    rc = S
1c284 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20  QLITE_LOCKED;.  
1c285 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e    p->errorAction
1c286 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d   = OE_Abort;.  }
1c287 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 44  else{.    int iD
1c288 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20  b = pOp->p3;.   
1c289 20 61 73 73 65 72 74 28 20 69 43 6e 74 3d 3d 31   assert( iCnt==1
1c28a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1c28b 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
1c28c 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a  (1<<iDb))!=0 );.
1c28d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1c28e 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 64  BtreeDropTable(d
1c28f 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 2c  b->aDb[iDb].pBt,
1c290 20 70 4f 70 2d 3e 70 31 2c 20 26 69 4d 6f 76 65   pOp->p1, &iMove
1c291 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79  d);.    MemSetTy
1c292 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1c293 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d  _Int);.    pOut-
1c294 3e 75 2e 69 20 3d 20 69 4d 6f 76 65 64 3b 0a 23  >u.i = iMoved;.#
1c295 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1c296 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
1c297 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1c298 5f 4f 4b 20 26 26 20 69 4d 6f 76 65 64 21 3d 30  _OK && iMoved!=0
1c299 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1c29a 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26  3RootPageMoved(&
1c29b 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2c 20 69 4d  db->aDb[iDb], iM
1c29c 6f 76 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a  oved, pOp->p1);.
1c29d 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
1c29e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1c29f 4f 70 63 6f 64 65 3a 20 43 6c 65 61 72 20 50 31  Opcode: Clear P1
1c2a0 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44 65 6c   P2 P3.**.** Del
1c2a1 65 74 65 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 73  ete all contents
1c2a2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1c2a3 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1c2a4 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 0a  whose root page.
1c2a5 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ** in the databa
1c2a6 73 65 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e  se file is given
1c2a7 20 62 79 20 50 31 2e 20 20 42 75 74 2c 20 75 6e   by P1.  But, un
1c2a8 6c 69 6b 65 20 44 65 73 74 72 6f 79 2c 20 64 6f  like Destroy, do
1c2a9 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65 20 74   not.** remove t
1c2aa 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  he table or inde
1c2ab 78 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  x from the datab
1c2ac 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
1c2ad 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  The table being 
1c2ae 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 20  clear is in the 
1c2af 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1c2b0 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20 20 49 66  le if P2==0.  If
1c2b1 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20 74  .** P2==1 then t
1c2b2 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63  he table to be c
1c2b3 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 61  lear is in the a
1c2b4 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
1c2b5 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69  e file.** that i
1c2b6 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  s used to store 
1c2b7 74 61 62 6c 65 73 20 63 72 65 61 74 65 20 75 73  tables create us
1c2b8 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f  ing CREATE TEMPO
1c2b9 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a  RARY TABLE..**.*
1c2ba 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75  * If the P3 valu
1c2bb 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  e is non-zero, t
1c2bc 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 72 65  hen the table re
1c2bd 66 65 72 65 64 20 74 6f 20 6d 75 73 74 20 62 65  fered to must be
1c2be 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 74 61   an.** intkey ta
1c2bf 62 6c 65 20 28 61 6e 20 53 51 4c 20 74 61 62 6c  ble (an SQL tabl
1c2c0 65 2c 20 6e 6f 74 20 61 6e 20 69 6e 64 65 78 29  e, not an index)
1c2c1 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
1c2c2 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 0a 2a  he row change .*
1c2c3 2a 20 63 6f 75 6e 74 20 69 73 20 69 6e 63 72 65  * count is incre
1c2c4 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75  mented by the nu
1c2c5 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
1c2c6 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
1c2c7 63 6c 65 61 72 65 64 2e 20 0a 2a 2a 20 49 66 20  cleared. .** If 
1c2c8 50 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68  P3 is greater th
1c2c9 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  an zero, then th
1c2ca 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
1c2cb 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73  n register P3 is
1c2cc 0a 2a 2a 20 61 6c 73 6f 20 69 6e 63 72 65 6d 65  .** also increme
1c2cd 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62  nted by the numb
1c2ce 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
1c2cf 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c  e table being cl
1c2d0 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  eared..**.** See
1c2d1 20 61 6c 73 6f 3a 20 44 65 73 74 72 6f 79 0a 2a   also: Destroy.*
1c2d2 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 65 61 72 3a  /.case OP_Clear:
1c2d3 20 7b 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65   {.  int nChange
1c2d4 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
1c2d5 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
1c2d6 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30  (1<<pOp->p2))!=0
1c2d7 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
1c2d8 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c  e3BtreeClearTabl
1c2d9 65 28 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62  e(.      db->aDb
1c2da 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70  [pOp->p2].pBt, p
1c2db 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 33  Op->p1, (pOp->p3
1c2dc 20 3f 20 26 6e 43 68 61 6e 67 65 20 3a 20 30 29   ? &nChange : 0)
1c2dd 0a 20 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d  .  );.  if( pOp-
1c2de 3e 70 33 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 43  >p3 ){.    p->nC
1c2df 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65  hange += nChange
1c2e0 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  ;.    if( pOp->p
1c2e1 33 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  3>0 ){.      p->
1c2e2 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e  aMem[pOp->p3].u.
1c2e3 69 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20  i += nChange;.  
1c2e4 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b    }.  }.  break;
1c2e5 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
1c2e6 72 65 61 74 65 54 61 62 6c 65 20 50 31 20 50 32  reateTable P1 P2
1c2e7 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c   * * *.**.** All
1c2e8 6f 63 61 74 65 20 61 20 6e 65 77 20 74 61 62 6c  ocate a new tabl
1c2e9 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  e in the main da
1c2ea 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50  tabase file if P
1c2eb 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a  1==0 or in the.*
1c2ec 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  * auxiliary data
1c2ed 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d  base file if P1=
1c2ee 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 61  =1 or in an atta
1c2ef 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 66  ched database if
1c2f0 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 65  .** P1>1.  Write
1c2f1 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
1c2f2 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77  umber of the new
1c2f3 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72   table into.** r
1c2f4 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a  egister P2.**.**
1c2f5 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20   The difference 
1c2f6 62 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 20  between a table 
1c2f7 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 20  and an index is 
1c2f8 74 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 6d  this:  A table m
1c2f9 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d  ust.** have a 4-
1c2fa 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 79  byte integer key
1c2fb 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 72   and can have ar
1c2fc 62 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 41  bitrary data.  A
1c2fd 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20 61  n index.** has a
1c2fe 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65 79 20  n arbitrary key 
1c2ff 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a  but no data..**.
1c300 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72 65  ** See also: Cre
1c301 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f  ateIndex.*/./* O
1c302 70 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e 64  pcode: CreateInd
1c303 65 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ex P1 P2 * * *.*
1c304 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
1c305 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68 65  new index in the
1c306 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
1c307 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 20  ile if P1==0 or 
1c308 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69  in the.** auxili
1c309 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c  ary database fil
1c30a 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 6e  e if P1==1 or in
1c30b 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74   an attached dat
1c30c 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e 31  abase if.** P1>1
1c30d 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 6f  .  Write the roo
1c30e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  t page number of
1c30f 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69   the new table i
1c310 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  nto.** register 
1c311 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f  P2..**.** See do
1c312 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f  cumentation on O
1c313 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66 6f  P_CreateTable fo
1c314 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
1c315 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  ormation..*/.cas
1c316 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78  e OP_CreateIndex
1c317 3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  :            /* 
1c318 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1c319 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74  */.case OP_Creat
1c31a 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 20  eTable: {       
1c31b 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1c31c 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 70  lease */.  int p
1c31d 67 6e 6f 20 3d 20 30 3b 0a 20 20 69 6e 74 20 66  gno = 0;.  int f
1c31e 6c 61 67 73 3b 0a 20 20 44 62 20 2a 70 44 62 3b  lags;.  Db *pDb;
1c31f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1c320 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1c321 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73  <db->nDb );.  as
1c322 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d  sert( (p->btreeM
1c323 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70  ask & (1<<pOp->p
1c324 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20  1))!=0 );.  pDb 
1c325 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e  = &db->aDb[pOp->
1c326 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p1];.  assert( p
1c327 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20  Db->pBt!=0 );.  
1c328 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1c329 3d 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20  =OP_CreateTable 
1c32a 29 7b 0a 20 20 20 20 2f 2a 20 66 6c 61 67 73 20  ){.    /* flags 
1c32b 3d 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b 20  = BTREE_INTKEY; 
1c32c 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 42  */.    flags = B
1c32d 54 52 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54  TREE_LEAFDATA|BT
1c32e 52 45 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65  REE_INTKEY;.  }e
1c32f 6c 73 65 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d  lse{.    flags =
1c330 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b   BTREE_ZERODATA;
1c331 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
1c332 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61  te3BtreeCreateTa
1c333 62 6c 65 28 70 44 62 2d 3e 70 42 74 2c 20 26 70  ble(pDb->pBt, &p
1c334 67 6e 6f 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  gno, flags);.  p
1c335 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 67 6e 6f 3b  Out->u.i = pgno;
1c336 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1c337 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
1c338 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1c339 20 4f 70 63 6f 64 65 3a 20 50 61 72 73 65 53 63   Opcode: ParseSc
1c33a 68 65 6d 61 20 50 31 20 50 32 20 2a 20 50 34 20  hema P1 P2 * P4 
1c33b 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 64  *.**.** Read and
1c33c 20 70 61 72 73 65 20 61 6c 6c 20 65 6e 74 72 69   parse all entri
1c33d 65 73 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49  es from the SQLI
1c33e 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20  TE_MASTER table 
1c33f 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 0a 2a  of database P1.*
1c340 2a 20 74 68 61 74 20 6d 61 74 63 68 20 74 68 65  * that match the
1c341 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 50 34   WHERE clause P4
1c342 2e 20 20 50 32 20 69 73 20 74 68 65 20 22 66 6f  .  P2 is the "fo
1c343 72 63 65 22 20 66 6c 61 67 2e 20 20 20 41 6c 77  rce" flag.   Alw
1c344 61 79 73 20 64 6f 0a 2a 2a 20 74 68 65 20 70 61  ays do.** the pa
1c345 72 73 69 6e 67 20 69 66 20 50 32 20 69 73 20 74  rsing if P2 is t
1c346 72 75 65 2e 20 20 49 66 20 50 32 20 69 73 20 66  rue.  If P2 is f
1c347 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20  alse, then this 
1c348 72 6f 75 74 69 6e 65 20 69 73 20 61 0a 2a 2a 20  routine is a.** 
1c349 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 63 68  no-op if the sch
1c34a 65 6d 61 20 69 73 20 6e 6f 74 20 63 75 72 72 65  ema is not curre
1c34b 6e 74 6c 79 20 6c 6f 61 64 65 64 2e 20 20 49 6e  ntly loaded.  In
1c34c 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66   other words, if
1c34d 20 50 32 0a 2a 2a 20 69 73 20 66 61 6c 73 65 2c   P2.** is false,
1c34e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54   the SQLITE_MAST
1c34f 45 52 20 74 61 62 6c 65 20 69 73 20 6f 6e 6c 79  ER table is only
1c350 20 70 61 72 73 65 64 20 69 66 20 74 68 65 20 72   parsed if the r
1c351 65 73 74 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63  est of the.** sc
1c352 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20  hema is already 
1c353 6c 6f 61 64 65 64 20 69 6e 74 6f 20 74 68 65 20  loaded into the 
1c354 73 79 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a  symbol table..**
1c355 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1c356 69 6e 76 6f 6b 65 73 20 74 68 65 20 70 61 72 73  invokes the pars
1c357 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  er to create a n
1c358 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  ew virtual machi
1c359 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 72 75 6e 73  ne,.** then runs
1c35a 20 74 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c   the new virtual
1c35b 20 6d 61 63 68 69 6e 65 2e 20 20 49 74 20 69 73   machine.  It is
1c35c 20 74 68 75 73 20 61 20 72 65 2d 65 6e 74 72 61   thus a re-entra
1c35d 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61  nt opcode..*/.ca
1c35e 73 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  se OP_ParseSchem
1c35f 61 3a 20 7b 0a 20 20 63 68 61 72 20 2a 7a 53 71  a: {.  char *zSq
1c360 6c 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70  l;.  int iDb = p
1c361 4f 70 2d 3e 70 31 3b 0a 20 20 63 6f 6e 73 74 20  Op->p1;.  const 
1c362 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 3b 0a 20  char *zMaster;. 
1c363 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61   InitData initDa
1c364 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  ta;..  assert( i
1c365 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d  Db>=0 && iDb<db-
1c366 3e 6e 44 62 20 29 3b 0a 20 20 69 66 28 20 21 70  >nDb );.  if( !p
1c367 4f 70 2d 3e 70 32 20 26 26 20 21 44 62 48 61 73  Op->p2 && !DbHas
1c368 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62  Property(db, iDb
1c369 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
1c36a 64 29 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b  d) ){.    break;
1c36b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d  .  }.  zMaster =
1c36c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
1c36d 62 29 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 64  b);.  initData.d
1c36e 62 20 3d 20 64 62 3b 0a 20 20 69 6e 69 74 44 61  b = db;.  initDa
1c36f 74 61 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31  ta.iDb = pOp->p1
1c370 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 70 7a 45  ;.  initData.pzE
1c371 72 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72  rrMsg = &p->zErr
1c372 4d 73 67 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71  Msg;.  zSql = sq
1c373 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
1c374 0a 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61  .     "SELECT na
1c375 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71  me, rootpage, sq
1c376 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 20 57  l FROM '%q'.%s W
1c377 48 45 52 45 20 25 73 22 2c 0a 20 20 20 20 20 64  HERE %s",.     d
1c378 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1c379 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4f 70 2d  e, zMaster, pOp-
1c37a 3e 70 34 2e 7a 29 3b 0a 20 20 69 66 28 20 7a 53  >p4.z);.  if( zS
1c37b 71 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  ql==0 ) goto no_
1c37c 6d 65 6d 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c  mem;.  (void)sql
1c37d 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1c37e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
1c37f 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 3b  >init.busy==0 );
1c380 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  .  db->init.busy
1c381 20 3d 20 31 3b 0a 20 20 69 6e 69 74 44 61 74 61   = 1;.  initData
1c382 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  .rc = SQLITE_OK;
1c383 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e  .  assert( !db->
1c384 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
1c385 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65    rc = sqlite3_e
1c386 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71  xec(db, zSql, sq
1c387 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63  lite3InitCallbac
1c388 6b 2c 20 26 69 6e 69 74 44 61 74 61 2c 20 30 29  k, &initData, 0)
1c389 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1c38a 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69 6e 69  TE_OK ) rc = ini
1c38b 74 44 61 74 61 2e 72 63 3b 0a 20 20 73 71 6c 69  tData.rc;.  sqli
1c38c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
1c38d 71 6c 29 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e  ql);.  db->init.
1c38e 62 75 73 79 20 3d 20 30 3b 0a 20 20 28 76 6f 69  busy = 0;.  (voi
1c38f 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
1c390 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d  n(db);.  if( rc=
1c391 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
1c392 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  .    goto no_mem
1c393 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 20 20  ;.  }.  break;  
1c394 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .}..#if !defined
1c395 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41  (SQLITE_OMIT_ANA
1c396 4c 59 5a 45 29 20 26 26 20 21 64 65 66 69 6e 65  LYZE) && !define
1c397 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  d(SQLITE_OMIT_PA
1c398 52 53 45 52 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a  RSER)./* Opcode:
1c399 20 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 50 31   LoadAnalysis P1
1c39a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52   * * * *.**.** R
1c39b 65 61 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73  ead the sqlite_s
1c39c 74 61 74 31 20 74 61 62 6c 65 20 66 6f 72 20 64  tat1 table for d
1c39d 61 74 61 62 61 73 65 20 50 31 20 61 6e 64 20 6c  atabase P1 and l
1c39e 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a  oad the content.
1c39f 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65  ** of that table
1c3a0 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 65 72 6e   into the intern
1c3a1 61 6c 20 69 6e 64 65 78 20 68 61 73 68 20 74 61  al index hash ta
1c3a2 62 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20  ble.  This will 
1c3a3 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 61 6e 61  cause.** the ana
1c3a4 6c 79 73 69 73 20 74 6f 20 62 65 20 75 73 65 64  lysis to be used
1c3a5 20 77 68 65 6e 20 70 72 65 70 61 72 69 6e 67 20   when preparing 
1c3a6 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 71  all subsequent q
1c3a7 75 65 72 69 65 73 2e 0a 2a 2f 0a 63 61 73 65 20  ueries..*/.case 
1c3a8 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 3a  OP_LoadAnalysis:
1c3a9 20 7b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70   {.  int iDb = p
1c3aa 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74  Op->p1;.  assert
1c3ab 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c  ( iDb>=0 && iDb<
1c3ac 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 72 63 20  db->nDb );.  rc 
1c3ad 3d 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69  = sqlite3Analysi
1c3ae 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29 3b 0a  sLoad(db, iDb);.
1c3af 20 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e    break;  .}.#en
1c3b0 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
1c3b1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
1c3b2 59 5a 45 29 20 26 26 20 21 64 65 66 69 6e 65 64  YZE) && !defined
1c3b3 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52  (SQLITE_OMIT_PAR
1c3b4 53 45 52 29 20 20 2a 2f 0a 0a 2f 2a 20 4f 70 63  SER)  */../* Opc
1c3b5 6f 64 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50  ode: DropTable P
1c3b6 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  1 * * P4 *.**.**
1c3b7 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65   Remove the inte
1c3b8 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29  rnal (in-memory)
1c3b9 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
1c3ba 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a   that describe.*
1c3bb 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  * the table name
1c3bc 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65  d P4 in database
1c3bd 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61   P1.  This is ca
1c3be 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 61 62  lled after a tab
1c3bf 6c 65 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64  le.** is dropped
1c3c0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65   in order to kee
1c3c1 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72  p the internal r
1c3c2 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
1c3c3 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63   the.** schema c
1c3c4 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77  onsistent with w
1c3c5 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a  hat is on disk..
1c3c6 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54  */.case OP_DropT
1c3c7 61 62 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65  able: {.  sqlite
1c3c8 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
1c3c9 54 61 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70  Table(db, pOp->p
1c3ca 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  1, pOp->p4.z);. 
1c3cb 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1c3cc 63 6f 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20  code: DropIndex 
1c3cd 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  P1 * * P4 *.**.*
1c3ce 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74  * Remove the int
1c3cf 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79  ernal (in-memory
1c3d0 29 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  ) data structure
1c3d1 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0a  s that describe.
1c3d2 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d  ** the index nam
1c3d3 65 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73  ed P4 in databas
1c3d4 65 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63  e P1.  This is c
1c3d5 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69  alled after an i
1c3d6 6e 64 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70  ndex.** is dropp
1c3d7 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  ed in order to k
1c3d8 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  eep the internal
1c3d9 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1c3da 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61  of the.** schema
1c3db 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68   consistent with
1c3dc 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b   what is on disk
1c3dd 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f  ..*/.case OP_Dro
1c3de 70 49 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69  pIndex: {.  sqli
1c3df 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
1c3e0 74 65 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d  teIndex(db, pOp-
1c3e1 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b  >p1, pOp->p4.z);
1c3e2 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1c3e3 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67  Opcode: DropTrig
1c3e4 67 65 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a  ger P1 * * P4 *.
1c3e5 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  **.** Remove the
1c3e6 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65   internal (in-me
1c3e7 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63  mory) data struc
1c3e8 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72  tures that descr
1c3e9 69 62 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67  ibe.** the trigg
1c3ea 65 72 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64  er named P4 in d
1c3eb 61 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69  atabase P1.  Thi
1c3ec 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  s is called afte
1c3ed 72 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69  r a trigger.** i
1c3ee 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64  s dropped in ord
1c3ef 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69  er to keep the i
1c3f0 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e  nternal represen
1c3f1 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  tation of the.**
1c3f2 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65   schema consiste
1c3f3 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20  nt with what is 
1c3f4 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65  on disk..*/.case
1c3f5 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a   OP_DropTrigger:
1c3f6 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69   {.  sqlite3Unli
1c3f7 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67  nkAndDeleteTrigg
1c3f8 65 72 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20  er(db, pOp->p1, 
1c3f9 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72  pOp->p4.z);.  br
1c3fa 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66  eak;.}...#ifndef
1c3fb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
1c3fc 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20  EGRITY_CHECK./* 
1c3fd 4f 70 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74  Opcode: Integrit
1c3fe 79 43 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50  yCk P1 P2 P3 * P
1c3ff 35 0a 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e  5.**.** Do an an
1c400 61 6c 79 73 69 73 20 6f 66 20 74 68 65 20 63 75  alysis of the cu
1c401 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74  rrently open dat
1c402 61 62 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e  abase.  Store in
1c403 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 20  .** register P1 
1c404 74 68 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65  the text of an e
1c405 72 72 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73  rror message des
1c406 63 72 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62  cribing any prob
1c407 6c 65 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70  lems..** If no p
1c408 72 6f 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e  roblems are foun
1c409 64 2c 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20  d, store a NULL 
1c40a 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a  in register P1..
1c40b 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74  **.** The regist
1c40c 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74  er P3 contains t
1c40d 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1c40e 72 20 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72  r of allowed err
1c40f 6f 72 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20  ors..** At most 
1c410 72 65 67 28 50 33 29 20 65 72 72 6f 72 73 20 77  reg(P3) errors w
1c411 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e  ill be reported.
1c412 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
1c413 64 73 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73  ds, the analysis
1c414 20 73 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61   stops as soon a
1c415 73 20 72 65 67 28 50 31 29 20 65 72 72 6f 72 73  s reg(P1) errors
1c416 20 61 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20   are .** seen.  
1c417 52 65 67 28 50 31 29 20 69 73 20 75 70 64 61 74  Reg(P1) is updat
1c418 65 64 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62  ed with the numb
1c419 65 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d  er of errors rem
1c41a 61 69 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  aining..**.** Th
1c41b 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  e root page numb
1c41c 65 72 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65  ers of all table
1c41d 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
1c41e 65 20 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a  e are integer.**
1c41f 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50   stored in reg(P
1c420 31 29 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72  1), reg(P1+1), r
1c421 65 67 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20  eg(P1+2), ....  
1c422 54 68 65 72 65 20 61 72 65 20 50 32 20 74 61 62  There are P2 tab
1c423 6c 65 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a  les.** total..**
1c424 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74  .** If P5 is not
1c425 20 7a 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b   zero, the check
1c426 20 69 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20   is done on the 
1c427 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
1c428 73 65 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20  se.** file, not 
1c429 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1c42a 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
1c42b 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65  is opcode is use
1c42c 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
1c42d 68 65 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65  he integrity_che
1c42e 63 6b 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61  ck pragma..*/.ca
1c42f 73 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43  se OP_IntegrityC
1c430 6b 3a 20 7b 0a 20 20 69 6e 74 20 6e 52 6f 6f 74  k: {.  int nRoot
1c431 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ;      /* Number
1c432 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 63 68   of tables to ch
1c433 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72 20 6f 66  eck.  (Number of
1c434 20 72 6f 6f 74 20 70 61 67 65 73 2e 29 20 2a 2f   root pages.) */
1c435 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b 20 20  .  int *aRoot;  
1c436 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 72     /* Array of r
1c437 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72 73 20  ootpage numbers 
1c438 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20 62 65  for tables to be
1c439 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 69 6e   checked */.  in
1c43a 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  t j;          /*
1c43b 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
1c43c 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20  .  int nErr;    
1c43d 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c43e 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65 64 20  errors reported 
1c43f 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20  */.  char *z;   
1c440 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20       /* Text of 
1c441 74 68 65 20 65 72 72 6f 72 20 72 65 70 6f 72 74  the error report
1c442 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 6e 45 72 72   */.  Mem *pnErr
1c443 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65  ;     /* Registe
1c444 72 20 6b 65 65 70 69 6e 67 20 74 72 61 63 6b 20  r keeping track 
1c445 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e  of errors remain
1c446 69 6e 67 20 2a 2f 0a 20 20 0a 20 20 6e 52 6f 6f  ing */.  .  nRoo
1c447 74 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61  t = pOp->p2;.  a
1c448 73 73 65 72 74 28 20 6e 52 6f 6f 74 3e 30 20 29  ssert( nRoot>0 )
1c449 3b 0a 20 20 61 52 6f 6f 74 20 3d 20 73 71 6c 69  ;.  aRoot = sqli
1c44a 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
1c44b 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28  b, sizeof(int)*(
1c44c 6e 52 6f 6f 74 2b 31 29 20 29 3b 0a 20 20 69 66  nRoot+1) );.  if
1c44d 28 20 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74  ( aRoot==0 ) got
1c44e 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65  o no_mem;.  asse
1c44f 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26  rt( pOp->p3>0 &&
1c450 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65   pOp->p3<=p->nMe
1c451 6d 20 29 3b 0a 20 20 70 6e 45 72 72 20 3d 20 26  m );.  pnErr = &
1c452 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1c453 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 6e 45  ;.  assert( (pnE
1c454 72 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  rr->flags & MEM_
1c455 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 61 73 73  Int)!=0 );.  ass
1c456 65 72 74 28 20 28 70 6e 45 72 72 2d 3e 66 6c 61  ert( (pnErr->fla
1c457 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45  gs & (MEM_Str|ME
1c458 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20  M_Blob))==0 );. 
1c459 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn1 = &p->aMem
1c45a 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72  [pOp->p1];.  for
1c45b 28 6a 3d 30 3b 20 6a 3c 6e 52 6f 6f 74 3b 20 6a  (j=0; j<nRoot; j
1c45c 2b 2b 29 7b 0a 20 20 20 20 61 52 6f 6f 74 5b 6a  ++){.    aRoot[j
1c45d 5d 20 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33  ] = (int)sqlite3
1c45e 56 64 62 65 49 6e 74 56 61 6c 75 65 28 26 70 49  VdbeIntValue(&pI
1c45f 6e 31 5b 6a 5d 29 3b 0a 20 20 7d 0a 20 20 61 52  n1[j]);.  }.  aR
1c460 6f 6f 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 61 73  oot[j] = 0;.  as
1c461 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64 62  sert( pOp->p5<db
1c462 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1c463 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1c464 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29 29   & (1<<pOp->p5))
1c465 21 3d 30 20 29 3b 0a 20 20 7a 20 3d 20 73 71 6c  !=0 );.  z = sql
1c466 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69  ite3BtreeIntegri
1c467 74 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b  tyCheck(db->aDb[
1c468 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 61 52  pOp->p5].pBt, aR
1c469 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 0a 20 20 20 20  oot, nRoot,.    
1c46a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c46b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e               (in
1c46c 74 29 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 6e  t)pnErr->u.i, &n
1c46d 45 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Err);.  sqlite3D
1c46e 62 46 72 65 65 28 64 62 2c 20 61 52 6f 6f 74 29  bFree(db, aRoot)
1c46f 3b 0a 20 20 70 6e 45 72 72 2d 3e 75 2e 69 20 2d  ;.  pnErr->u.i -
1c470 3d 20 6e 45 72 72 3b 0a 20 20 73 71 6c 69 74 65  = nErr;.  sqlite
1c471 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
1c472 70 49 6e 31 29 3b 0a 20 20 69 66 28 20 6e 45 72  pIn1);.  if( nEr
1c473 72 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  r==0 ){.    asse
1c474 72 74 28 20 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65  rt( z==0 );.  }e
1c475 6c 73 65 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a  lse if( z==0 ){.
1c476 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1c477 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
1c478 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
1c479 74 72 28 70 49 6e 31 2c 20 7a 2c 20 2d 31 2c 20  tr(pIn1, z, -1, 
1c47a 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 73 71 6c  SQLITE_UTF8, sql
1c47b 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a  ite3_free);.  }.
1c47c 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1c47d 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 73  BSIZE(pIn1);.  s
1c47e 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1c47f 45 6e 63 6f 64 69 6e 67 28 70 49 6e 31 2c 20 65  Encoding(pIn1, e
1c480 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 62 72 65 61  ncoding);.  brea
1c481 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
1c482 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
1c483 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f  RITY_CHECK */../
1c484 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74  * Opcode: RowSet
1c485 41 64 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Add P1 P2 * * *.
1c486 2a 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65  **.** Insert the
1c487 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 68   integer value h
1c488 65 6c 64 20 62 79 20 72 65 67 69 73 74 65 72 20  eld by register 
1c489 50 32 20 69 6e 74 6f 20 61 20 62 6f 6f 6c 65 61  P2 into a boolea
1c48a 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 65 6c 64 20  n index.** held 
1c48b 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a  in register P1..
1c48c 2a 2a 0a 2a 2a 20 41 6e 20 61 73 73 65 72 74 69  **.** An asserti
1c48d 6f 6e 20 66 61 69 6c 73 20 69 66 20 50 32 20 69  on fails if P2 i
1c48e 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72  s not an integer
1c48f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77  ..*/.case OP_Row
1c490 53 65 74 41 64 64 3a 20 7b 20 20 20 20 20 20 20  SetAdd: {       
1c491 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 4d 65 6d 20  /* in2 */.  Mem 
1c492 2a 70 49 64 78 3b 0a 20 20 4d 65 6d 20 2a 70 56  *pIdx;.  Mem *pV
1c493 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  al;.  assert( pO
1c494 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p1>0 && pOp->
1c495 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p1<=p->nMem );. 
1c496 20 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIdx = &p->aMem
1c497 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1c498 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26  ert( pOp->p2>0 &
1c499 26 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d  & pOp->p2<=p->nM
1c49a 65 6d 20 29 3b 0a 20 20 70 56 61 6c 20 3d 20 26  em );.  pVal = &
1c49b 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  p->aMem[pOp->p2]
1c49c 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 56 61  ;.  assert( (pVa
1c49d 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  l->flags & MEM_I
1c49e 6e 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  nt)!=0 );.  if( 
1c49f 28 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIdx->flags & M
1c4a0 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b  EM_RowSet)==0 ){
1c4a1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1c4a2 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 64  MemSetRowSet(pId
1c4a3 78 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 64  x);.    if( (pId
1c4a4 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  x->flags & MEM_R
1c4a5 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f  owSet)==0 ) goto
1c4a6 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73   no_mem;.  }.  s
1c4a7 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65  qlite3RowSetInse
1c4a8 72 74 28 70 49 64 78 2d 3e 75 2e 70 52 6f 77 53  rt(pIdx->u.pRowS
1c4a9 65 74 2c 20 70 56 61 6c 2d 3e 75 2e 69 29 3b 0a  et, pVal->u.i);.
1c4aa 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c4ab 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 52 65 61  pcode: RowSetRea
1c4ac 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  d P1 P2 P3 * *.*
1c4ad 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
1c4ae 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20   smallest value 
1c4af 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e 20 69 6e 64  from boolean ind
1c4b0 65 78 20 50 31 20 61 6e 64 20 70 75 74 20 74 68  ex P1 and put th
1c4b1 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a  at value into.**
1c4b2 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 20 4f   register P3.  O
1c4b3 72 2c 20 69 66 20 62 6f 6f 6c 65 61 6e 20 69 6e  r, if boolean in
1c4b4 64 65 78 20 50 31 20 69 73 20 69 6e 69 74 69 61  dex P1 is initia
1c4b5 6c 6c 79 20 65 6d 70 74 79 2c 20 6c 65 61 76 65  lly empty, leave
1c4b6 20 50 33 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64   P3.** unchanged
1c4b7 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 69 6e 73   and jump to ins
1c4b8 74 72 75 63 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a  truction P2..*/.
1c4b9 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 52 65  case OP_RowSetRe
1c4ba 61 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a  ad: {       /* j
1c4bb 75 6d 70 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 4d  ump, out3 */.  M
1c4bc 65 6d 20 2a 70 49 64 78 3b 0a 20 20 69 36 34 20  em *pIdx;.  i64 
1c4bd 76 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70  val;.  assert( p
1c4be 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d  Op->p1>0 && pOp-
1c4bf 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p1<=p->nMem );.
1c4c0 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45    CHECK_FOR_INTE
1c4c1 52 52 55 50 54 3b 0a 20 20 70 49 64 78 20 3d 20  RRUPT;.  pIdx = 
1c4c2 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
1c4c3 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e  ];.  pOut = &p->
1c4c4 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1c4c5 20 69 66 28 20 28 70 49 64 78 2d 3e 66 6c 61 67   if( (pIdx->flag
1c4c6 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
1c4c7 3d 30 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65  =0 .   || sqlite
1c4c8 33 52 6f 77 53 65 74 4e 65 78 74 28 70 49 64 78  3RowSetNext(pIdx
1c4c9 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26 76 61  ->u.pRowSet, &va
1c4ca 6c 29 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f  l)==0.  ){.    /
1c4cb 2a 20 54 68 65 20 62 6f 6f 6c 65 61 6e 20 69 6e  * The boolean in
1c4cc 64 65 78 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a  dex is empty */.
1c4cd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1c4ce 65 6d 53 65 74 4e 75 6c 6c 28 70 49 64 78 29 3b  emSetNull(pIdx);
1c4cf 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1c4d0 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  2 - 1;.  }else{.
1c4d1 20 20 20 20 2f 2a 20 41 20 76 61 6c 75 65 20 77      /* A value w
1c4d2 61 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 74  as pulled from t
1c4d3 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20  he index */.    
1c4d4 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
1c4d5 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
1c4d6 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 73 71 6c  >nMem );.    sql
1c4d7 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e  ite3VdbeMemSetIn
1c4d8 74 36 34 28 70 4f 75 74 2c 20 76 61 6c 29 3b 0a  t64(pOut, val);.
1c4d9 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1c4da 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1c4db 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a 20  OMIT_TRIGGER./* 
1c4dc 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 65 78 74 50  Opcode: ContextP
1c4dd 75 73 68 20 2a 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a  ush * * * .**.**
1c4de 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e   Save the curren
1c4df 74 20 56 64 62 65 20 63 6f 6e 74 65 78 74 20 73  t Vdbe context s
1c4e0 75 63 68 20 74 68 61 74 20 69 74 20 63 61 6e 20  uch that it can 
1c4e1 62 65 20 72 65 73 74 6f 72 65 64 20 62 79 20 61  be restored by a
1c4e2 20 43 6f 6e 74 65 78 74 50 6f 70 0a 2a 2a 20 6f   ContextPop.** o
1c4e3 70 63 6f 64 65 2e 20 54 68 65 20 63 6f 6e 74 65  pcode. The conte
1c4e4 78 74 20 73 74 6f 72 65 73 20 74 68 65 20 6c 61  xt stores the la
1c4e5 73 74 20 69 6e 73 65 72 74 20 72 6f 77 20 69 64  st insert row id
1c4e6 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 74 65  , the last state
1c4e7 6d 65 6e 74 20 63 68 61 6e 67 65 0a 2a 2a 20 63  ment change.** c
1c4e8 6f 75 6e 74 2c 20 61 6e 64 20 74 68 65 20 63 75  ount, and the cu
1c4e9 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 20  rrent statement 
1c4ea 63 68 61 6e 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f  change count..*/
1c4eb 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 74 65 78 74  .case OP_Context
1c4ec 50 75 73 68 3a 20 7b 0a 20 20 69 6e 74 20 69 20  Push: {.  int i 
1c4ed 3d 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63  = p->contextStac
1c4ee 6b 54 6f 70 2b 2b 3b 0a 20 20 43 6f 6e 74 65 78  kTop++;.  Contex
1c4ef 74 20 2a 70 43 6f 6e 74 65 78 74 3b 0a 0a 20 20  t *pContext;..  
1c4f0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a  assert( i>=0 );.
1c4f1 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 54 68 69    /* FIX ME: Thi
1c4f2 73 20 73 68 6f 75 6c 64 20 62 65 20 61 6c 6c 6f  s should be allo
1c4f3 63 61 74 65 64 20 61 73 20 70 61 72 74 20 6f 66  cated as part of
1c4f4 20 74 68 65 20 76 64 62 65 20 61 74 20 63 6f 6d   the vdbe at com
1c4f5 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 69  pile-time */.  i
1c4f6 66 28 20 69 3e 3d 70 2d 3e 63 6f 6e 74 65 78 74  f( i>=p->context
1c4f7 53 74 61 63 6b 44 65 70 74 68 20 29 7b 0a 20 20  StackDepth ){.  
1c4f8 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63    p->contextStac
1c4f9 6b 44 65 70 74 68 20 3d 20 69 2b 31 3b 0a 20 20  kDepth = i+1;.  
1c4fa 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63    p->contextStac
1c4fb 6b 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  k = sqlite3DbRea
1c4fc 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70  llocOrFree(db, p
1c4fd 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 2c 0a  ->contextStack,.
1c4fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c4ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c500 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
1c501 28 43 6f 6e 74 65 78 74 29 2a 28 69 2b 31 29 29  (Context)*(i+1))
1c502 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 6f 6e  ;.    if( p->con
1c503 74 65 78 74 53 74 61 63 6b 3d 3d 30 20 29 20 67  textStack==0 ) g
1c504 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1c505 20 20 70 43 6f 6e 74 65 78 74 20 3d 20 26 70 2d    pContext = &p-
1c506 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b 69 5d  >contextStack[i]
1c507 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 6c 61  ;.  pContext->la
1c508 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61  stRowid = db->la
1c509 73 74 52 6f 77 69 64 3b 0a 20 20 70 43 6f 6e 74  stRowid;.  pCont
1c50a 65 78 74 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70  ext->nChange = p
1c50b 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 62 72 65  ->nChange;.  bre
1c50c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1c50d 3a 20 43 6f 6e 74 65 78 74 50 6f 70 20 2a 20 2a  : ContextPop * *
1c50e 20 2a 20 0a 2a 2a 0a 2a 2a 20 52 65 73 74 6f 72   * .**.** Restor
1c50f 65 20 74 68 65 20 56 64 62 65 20 63 6f 6e 74 65  e the Vdbe conte
1c510 78 74 20 74 6f 20 74 68 65 20 73 74 61 74 65 20  xt to the state 
1c511 69 74 20 77 61 73 20 69 6e 20 77 68 65 6e 20 63  it was in when c
1c512 6f 6e 74 65 78 74 50 75 73 68 20 77 61 73 20 6c  ontextPush was l
1c513 61 73 74 0a 2a 2a 20 65 78 65 63 75 74 65 64 2e  ast.** executed.
1c514 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74 6f   The context sto
1c515 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73  res the last ins
1c516 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65 20  ert row id, the 
1c517 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a  last statement.*
1c518 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c 20  * change count, 
1c519 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  and the current 
1c51a 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65  statement change
1c51b 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20   count..*/.case 
1c51c 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 3a 20 7b  OP_ContextPop: {
1c51d 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  .  Context *pCon
1c51e 74 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e 74 65  text = &p->conte
1c51f 78 74 53 74 61 63 6b 5b 2d 2d 70 2d 3e 63 6f 6e  xtStack[--p->con
1c520 74 65 78 74 53 74 61 63 6b 54 6f 70 5d 3b 0a 20  textStackTop];. 
1c521 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6f 6e 74   assert( p->cont
1c522 65 78 74 53 74 61 63 6b 54 6f 70 3e 3d 30 20 29  extStackTop>=0 )
1c523 3b 0a 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69  ;.  db->lastRowi
1c524 64 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 6c 61  d = pContext->la
1c525 73 74 52 6f 77 69 64 3b 0a 20 20 70 2d 3e 6e 43  stRowid;.  p->nC
1c526 68 61 6e 67 65 20 3d 20 70 43 6f 6e 74 65 78 74  hange = pContext
1c527 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 62 72 65  ->nChange;.  bre
1c528 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1c529 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1c52a 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a  MIT_TRIGGER */..
1c52b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1c52c 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
1c52d 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 65 6d  T./* Opcode: Mem
1c52e 4d 61 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Max P1 P2 * * *.
1c52f 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61  **.** Set the va
1c530 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20  lue of register 
1c531 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  P1 to the maximu
1c532 6d 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e 74  m of its current
1c533 20 76 61 6c 75 65 0a 2a 2a 20 61 6e 64 20 74 68   value.** and th
1c534 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1c535 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68  ter P2..**.** Th
1c536 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  is instruction t
1c537 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 20 69  hrows an error i
1c538 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c  f the memory cel
1c539 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c  l is not initial
1c53a 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72  ly.** an integer
1c53b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 6d  ..*/.case OP_Mem
1c53c 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  Max: {        /*
1c53d 20 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a 20 20 73   in1, in2 */.  s
1c53e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1c53f 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20  egerify(pIn1);. 
1c540 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1c541 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b  ntegerify(pIn2);
1c542 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69  .  if( pIn1->u.i
1c543 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20 20  <pIn2->u.i){.   
1c544 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49 6e   pIn1->u.i = pIn
1c545 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72  2->u.i;.  }.  br
1c546 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1c547 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1c548 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f  OINCREMENT */../
1c549 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73 20  * Opcode: IfPos 
1c54a 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1c54b 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f  * If the value o
1c54c 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  f register P1 is
1c54d 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20 6a   1 or greater, j
1c54e 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a  ump to P2..**.**
1c54f 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
1c550 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72  o use this instr
1c551 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69  uction on a regi
1c552 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a  ster that does.*
1c553 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e  * not contain an
1c554 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73   integer.  An as
1c555 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69  sertion fault wi
1c556 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75  ll result if you
1c557 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50   try..*/.case OP
1c558 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20 20  _IfPos: {       
1c559 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
1c55a 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d  .  assert( pIn1-
1c55b 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29  >flags&MEM_Int )
1c55c 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e  ;.  if( pIn1->u.
1c55d 69 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d  i>0 ){.     pc =
1c55e 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1c55f 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1c560 20 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 20 50   Opcode: IfNeg P
1c561 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1c562 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66   If the value of
1c563 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1c564 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20  less than zero, 
1c565 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a  jump to P2. .**.
1c566 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c  ** It is illegal
1c567 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73   to use this ins
1c568 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65  truction on a re
1c569 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73  gister that does
1c56a 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  .** not contain 
1c56b 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20  an integer.  An 
1c56c 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
1c56d 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79  will result if y
1c56e 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20  ou try..*/.case 
1c56f 4f 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20 20  OP_IfNeg: {     
1c570 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20     /* jump, in1 
1c571 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  */.  assert( pIn
1c572 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74  1->flags&MEM_Int
1c573 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   );.  if( pIn1->
1c574 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 70 63  u.i<0 ){.     pc
1c575 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1c576 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1c577 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a 65 72  /* Opcode: IfZer
1c578 6f 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  o P1 P2 * * *.**
1c579 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1c57a 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20   of register P1 
1c57b 69 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a 75  is exactly 0, ju
1c57c 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a  mp to P2. .**.**
1c57d 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
1c57e 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72  o use this instr
1c57f 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69  uction on a regi
1c580 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a  ster that does.*
1c581 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e  * not contain an
1c582 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73   integer.  An as
1c583 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69  sertion fault wi
1c584 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75  ll result if you
1c585 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50   try..*/.case OP
1c586 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20  _IfZero: {      
1c587 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a    /* jump, in1 *
1c588 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31  /.  assert( pIn1
1c589 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20  ->flags&MEM_Int 
1c58a 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75  );.  if( pIn1->u
1c58b 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70 63  .i==0 ){.     pc
1c58c 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1c58d 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1c58e 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53 74  /* Opcode: AggSt
1c58f 65 70 20 2a 20 50 32 20 50 33 20 50 34 20 50 35  ep * P2 P3 P4 P5
1c590 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74  .**.** Execute t
1c591 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e  he step function
1c592 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74   for an aggregat
1c593 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63 74  e.  The.** funct
1c594 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75 6d  ion has P5 argum
1c595 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61 20  ents.   P4 is a 
1c596 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46  pointer to the F
1c597 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74  uncDef.** struct
1c598 75 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69  ure that specifi
1c599 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  es the function.
1c59a 20 20 55 73 65 20 72 65 67 69 73 74 65 72 0a 2a    Use register.*
1c59b 2a 20 50 33 20 61 73 20 74 68 65 20 61 63 63 75  * P3 as the accu
1c59c 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  mulator..**.** T
1c59d 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20  he P5 arguments 
1c59e 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72  are taken from r
1c59f 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 69  egister P2 and i
1c5a0 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73  ts.** successors
1c5a1 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67  ..*/.case OP_Agg
1c5a2 53 74 65 70 3a 20 7b 0a 20 20 69 6e 74 20 6e 20  Step: {.  int n 
1c5a3 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 69 6e 74  = pOp->p5;.  int
1c5a4 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c   i;.  Mem *pMem,
1c5a5 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 74 65   *pRec;.  sqlite
1c5a6 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20  3_context ctx;. 
1c5a7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1c5a8 2a 61 70 56 61 6c 3b 0a 0a 20 20 61 73 73 65 72  *apVal;..  asser
1c5a9 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 70 52 65  t( n>=0 );.  pRe
1c5aa 63 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  c = &p->aMem[pOp
1c5ab 2d 3e 70 32 5d 3b 0a 20 20 61 70 56 61 6c 20 3d  ->p2];.  apVal =
1c5ac 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 73   p->apArg;.  ass
1c5ad 65 72 74 28 20 61 70 56 61 6c 20 7c 7c 20 6e 3d  ert( apVal || n=
1c5ae 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  =0 );.  for(i=0;
1c5af 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 52 65 63 2b   i<n; i++, pRec+
1c5b0 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b 69 5d  +){.    apVal[i]
1c5b1 20 3d 20 70 52 65 63 3b 0a 20 20 20 20 73 74 6f   = pRec;.    sto
1c5b2 72 65 54 79 70 65 49 6e 66 6f 28 70 52 65 63 2c  reTypeInfo(pRec,
1c5b3 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a   encoding);.  }.
1c5b4 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f    ctx.pFunc = pO
1c5b5 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61  p->p4.pFunc;.  a
1c5b6 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1c5b7 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e   && pOp->p3<=p->
1c5b8 6e 4d 65 6d 20 29 3b 0a 20 20 63 74 78 2e 70 4d  nMem );.  ctx.pM
1c5b9 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 70 2d 3e  em = pMem = &p->
1c5ba 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1c5bb 20 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 63 74   pMem->n++;.  ct
1c5bc 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  x.s.flags = MEM_
1c5bd 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 7a 20  Null;.  ctx.s.z 
1c5be 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61  = 0;.  ctx.s.zMa
1c5bf 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 63 74 78 2e  lloc = 0;.  ctx.
1c5c0 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 63 74  s.xDel = 0;.  ct
1c5c1 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 63  x.s.db = db;.  c
1c5c2 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a  tx.isError = 0;.
1c5c3 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b    ctx.pColl = 0;
1c5c4 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75 6e 63  .  if( ctx.pFunc
1c5c5 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1c5c6 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29  _FUNC_NEEDCOLL )
1c5c7 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  {.    assert( pO
1c5c8 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20  p>p->aOp );.    
1c5c9 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e  assert( pOp[-1].
1c5ca 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53  p4type==P4_COLLS
1c5cb 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  EQ );.    assert
1c5cc 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65  ( pOp[-1].opcode
1c5cd 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a  ==OP_CollSeq );.
1c5ce 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20      ctx.pColl = 
1c5cf 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c  pOp[-1].p4.pColl
1c5d0 3b 0a 20 20 7d 0a 20 20 28 63 74 78 2e 70 46 75  ;.  }.  (ctx.pFu
1c5d1 6e 63 2d 3e 78 53 74 65 70 29 28 26 63 74 78 2c  nc->xStep)(&ctx,
1c5d2 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66   n, apVal);.  if
1c5d3 28 20 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b  ( ctx.isError ){
1c5d4 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1c5d5 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1c5d6 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
1c5d7 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1c5d8 26 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63  &ctx.s));.    rc
1c5d9 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a   = ctx.isError;.
1c5da 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
1c5db 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 74 78  eMemRelease(&ctx
1c5dc 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .s);.  break;.}.
1c5dd 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 46  ./* Opcode: AggF
1c5de 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20  inal P1 P2 * P4 
1c5df 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  *.**.** Execute 
1c5e0 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 66 75  the finalizer fu
1c5e1 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67  nction for an ag
1c5e2 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 73 0a  gregate.  P1 is.
1c5e3 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f  ** the memory lo
1c5e4 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 74  cation that is t
1c5e5 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 66  he accumulator f
1c5e6 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 74 65  or the aggregate
1c5e7 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68  ..**.** P2 is th
1c5e8 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  e number of argu
1c5e9 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 73  ments that the s
1c5ea 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  tep function tak
1c5eb 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 73 20  es and.** P4 is 
1c5ec 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1c5ed 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 68 69   FuncDef for thi
1c5ee 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  s function.  The
1c5ef 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20   P2.** argument 
1c5f0 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20 74  is not used by t
1c5f1 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 74 20  his opcode.  It 
1c5f2 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 74 6f  is only there to
1c5f3 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a 2a 2a   disambiguate.**
1c5f4 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
1c5f5 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 6e 67  can take varying
1c5f6 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 67 75   numbers of argu
1c5f7 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a 20 50  ments.  The.** P
1c5f8 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e  4 argument is on
1c5f9 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 74 68  ly needed for th
1c5fa 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 61 73  e degenerate cas
1c5fb 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 73  e where.** the s
1c5fc 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 61 73  tep function was
1c5fd 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20   not previously 
1c5fe 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65 20  called..*/.case 
1c5ff 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b 0a 20  OP_AggFinal: {. 
1c600 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73   Mem *pMem;.  as
1c601 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20  sert( pOp->p1>0 
1c602 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e  && pOp->p1<=p->n
1c603 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20  Mem );.  pMem = 
1c604 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
1c605 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  ];.  assert( (pM
1c606 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e 28 4d 45  em->flags & ~(ME
1c607 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29  M_Null|MEM_Agg))
1c608 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ==0 );.  rc = sq
1c609 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61  lite3VdbeMemFina
1c60a 6c 69 7a 65 28 70 4d 65 6d 2c 20 70 4f 70 2d 3e  lize(pMem, pOp->
1c60b 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 66 28  p4.pFunc);.  if(
1c60c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f   rc==SQLITE_ERRO
1c60d 52 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  R ){.    sqlite3
1c60e 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1c60f 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
1c610 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1c611 65 78 74 28 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a  ext(pMem));.  }.
1c612 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1c613 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
1c614 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 55  , encoding);.  U
1c615 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1c616 5a 45 28 70 4d 65 6d 29 3b 0a 20 20 69 66 28 20  ZE(pMem);.  if( 
1c617 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f  sqlite3VdbeMemTo
1c618 6f 42 69 67 28 70 4d 65 6d 29 20 29 7b 0a 20 20  oBig(pMem) ){.  
1c619 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1c61a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1c61b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1c61c 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
1c61d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
1c61e 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
1c61f 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63  )./* Opcode: Vac
1c620 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  uum * * * * *.**
1c621 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 65  .** Vacuum the e
1c622 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e 20  ntire database. 
1c623 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c   This opcode wil
1c624 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 69  l cause other vi
1c625 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65  rtual.** machine
1c626 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  s to be created 
1c627 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 79  and run.  It may
1c628 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 66   not be called f
1c629 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20  rom within.** a 
1c62a 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a  transaction..*/.
1c62b 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20  case OP_Vacuum: 
1c62c 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  {.  if( sqlite3S
1c62d 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67  afetyOff(db) ) g
1c62e 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1c62f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 3d  _misuse; .  rc =
1c630 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75   sqlite3RunVacuu
1c631 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  m(&p->zErrMsg, d
1c632 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  b);.  if( sqlite
1c633 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
1c634 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1c635 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 61  o_misuse;.  brea
1c636 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  k;.}.#endif..#if
1c637 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1c638 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1c639 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 63  )./* Opcode: Inc
1c63a 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a 20  rVacuum P1 P2 * 
1c63b 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72  * *.**.** Perfor
1c63c 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20  m a single step 
1c63d 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74  of the increment
1c63e 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64  al vacuum proced
1c63f 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31  ure on.** the P1
1c640 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68   database. If th
1c641 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e  e vacuum has fin
1c642 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69  ished, jump to i
1c643 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32  nstruction.** P2
1c644 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c  . Otherwise, fal
1c645 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
1c646 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1c647 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  n..*/.case OP_In
1c648 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20  crVacuum: {     
1c649 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20     /* jump */.  
1c64a 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61  Btree *pBt;..  a
1c64b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1c64c 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d  0 && pOp->p1<db-
1c64d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1c64e 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1c64f 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21  & (1<<pOp->p1))!
1c650 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62  =0 );.  pBt = db
1c651 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
1c652 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  Bt;.  rc = sqlit
1c653 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75  e3BtreeIncrVacuu
1c654 6d 28 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63  m(pBt);.  if( rc
1c655 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
1c656 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1c657 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  2 - 1;.    rc = 
1c658 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1c659 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1c65a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 78 70  ../* Opcode: Exp
1c65b 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  ire P1 * * * *.*
1c65c 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 63 6f  *.** Cause preco
1c65d 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74  mpiled statement
1c65e 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 70 69  s to become expi
1c65f 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65 64 20  red. An expired 
1c660 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 61 69  statement.** fai
1c661 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ls with an error
1c662 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f   code of SQLITE_
1c663 53 43 48 45 4d 41 20 69 66 20 69 74 20 69 73 20  SCHEMA if it is 
1c664 65 76 65 72 20 65 78 65 63 75 74 65 64 20 0a 2a  ever executed .*
1c665 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33 5f 73  * (via sqlite3_s
1c666 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49  tep())..** .** I
1c667 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 6e 20  f P1 is 0, then 
1c668 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  all SQL statemen
1c669 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65  ts become expire
1c66a 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d  d. If P1 is non-
1c66b 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e  zero,.** then on
1c66c 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79  ly the currently
1c66d 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65   executing state
1c66e 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 65 64  ment is affected
1c66f 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 78  . .*/.case OP_Ex
1c670 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20 21 70  pire: {.  if( !p
1c671 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 71  Op->p1 ){.    sq
1c672 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
1c673 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
1c674 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1c675 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a  p->expired = 1;.
1c676 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1c677 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1c678 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1c679 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 62 6c  ./* Opcode: Tabl
1c67a 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 20 50  eLock P1 P2 P3 P
1c67b 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e  4 *.**.** Obtain
1c67c 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 61 72   a lock on a par
1c67d 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 54  ticular table. T
1c67e 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1c67f 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65  is only used whe
1c680 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d  n.** the shared-
1c681 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73  cache feature is
1c682 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a   enabled. .**.**
1c683 20 49 66 20 50 31 20 69 73 20 20 74 68 65 20 69   If P1 is  the i
1c684 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
1c685 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e  base in sqlite3.
1c686 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74  aDb[] of the dat
1c687 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63  abase.** on whic
1c688 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63  h the lock is ac
1c689 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c  quired.  A readl
1c68a 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
1c68b 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61  if P3==0 or.** a
1c68c 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50   write lock if P
1c68d 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63  3==1..**.** P2 c
1c68e 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74  ontains the root
1c68f 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62  -page of the tab
1c690 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  le to lock..**.*
1c691 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20  * P4 contains a 
1c692 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
1c693 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1c694 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54   being locked. T
1c695 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75  his is only.** u
1c696 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  sed to generate 
1c697 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1c698 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e   if the lock can
1c699 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e  not be obtained.
1c69a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c  .*/.case OP_Tabl
1c69b 65 4c 6f 63 6b 3a 20 7b 0a 20 20 69 6e 74 20 70  eLock: {.  int p
1c69c 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20  1 = pOp->p1; .  
1c69d 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d  u8 isWriteLock =
1c69e 20 28 75 38 29 70 4f 70 2d 3e 70 33 3b 0a 20 20   (u8)pOp->p3;.  
1c69f 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26  assert( p1>=0 &&
1c6a0 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20   p1<db->nDb );. 
1c6a1 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
1c6a2 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 31 29  eeMask & (1<<p1)
1c6a3 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )!=0 );.  assert
1c6a4 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30  ( isWriteLock==0
1c6a5 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d   || isWriteLock=
1c6a6 3d 31 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  =1 );.  rc = sql
1c6a7 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62  ite3BtreeLockTab
1c6a8 6c 65 28 64 62 2d 3e 61 44 62 5b 70 31 5d 2e 70  le(db->aDb[p1].p
1c6a9 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 69 73 57  Bt, pOp->p2, isW
1c6aa 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 69 66 28  riteLock);.  if(
1c6ab 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b   rc==SQLITE_LOCK
1c6ac 45 44 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  ED ){.    const 
1c6ad 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70  char *z = pOp->p
1c6ae 34 2e 7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  4.z;.    sqlite3
1c6af 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1c6b0 72 72 4d 73 67 2c 20 64 62 2c 20 22 64 61 74 61  rrMsg, db, "data
1c6b1 62 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c 6f  base table is lo
1c6b2 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29 3b 0a 20  cked: %s", z);. 
1c6b3 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1c6b4 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1c6b5 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1c6b6 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1c6b7 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1c6b8 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
1c6b9 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20 50 34   VBegin * * * P4
1c6ba 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20   *.**.** P4 may 
1c6bb 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  be a pointer to 
1c6bc 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  an sqlite3_vtab 
1c6bd 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 73 6f  structure. If so
1c6be 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a 2a 20 78  , call the .** x
1c6bf 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 66 6f 72  Begin method for
1c6c0 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a   that table..**.
1c6c1 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74 68 65 72  ** Also, whether
1c6c2 20 6f 72 20 6e 6f 74 20 50 34 20 69 73 20 73 65   or not P4 is se
1c6c3 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68  t, check that th
1c6c4 69 73 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  is is not being 
1c6c5 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a 2a 20 77  called from.** w
1c6c6 69 74 68 69 6e 20 61 20 63 61 6c 6c 62 61 63 6b  ithin a callback
1c6c7 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61   to a virtual ta
1c6c8 62 6c 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68  ble xSync() meth
1c6c9 6f 64 2e 20 49 66 20 69 74 20 69 73 2c 20 73 65  od. If it is, se
1c6ca 74 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63  t the.** error c
1c6cb 6f 64 65 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f  ode to SQLITE_LO
1c6cc 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  CKED..*/.case OP
1c6cd 5f 56 42 65 67 69 6e 3a 20 7b 0a 20 20 73 71 6c  _VBegin: {.  sql
1c6ce 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1c6cf 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62   = pOp->p4.pVtab
1c6d0 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1c6d1 56 74 61 62 42 65 67 69 6e 28 64 62 2c 20 70 56  VtabBegin(db, pV
1c6d2 74 61 62 29 3b 0a 20 20 69 66 28 20 70 56 74 61  tab);.  if( pVta
1c6d3 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
1c6d4 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1c6d5 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
1c6d6 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e  ErrMsg = pVtab->
1c6d7 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74  zErrMsg;.    pVt
1c6d8 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1c6d9 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1c6da 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1c6db 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1c6dc 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  LE */..#ifndef S
1c6dd 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1c6de 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
1c6df 65 3a 20 56 43 72 65 61 74 65 20 50 31 20 2a 20  e: VCreate P1 * 
1c6e0 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1c6e1 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  is the name of a
1c6e2 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
1c6e3 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 43  n database P1. C
1c6e4 61 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65 20  all the xCreate 
1c6e5 6d 65 74 68 6f 64 0a 2a 2a 20 66 6f 72 20 74 68  method.** for th
1c6e6 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73  at table..*/.cas
1c6e7 65 20 4f 50 5f 56 43 72 65 61 74 65 3a 20 7b 0a  e OP_VCreate: {.
1c6e8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74    rc = sqlite3Vt
1c6e9 61 62 43 61 6c 6c 43 72 65 61 74 65 28 64 62 2c  abCallCreate(db,
1c6ea 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
1c6eb 34 2e 7a 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67  4.z, &p->zErrMsg
1c6ec 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65  );.  break;.}.#e
1c6ed 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1c6ee 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1c6ef 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1c6f0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1c6f1 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
1c6f2 20 56 44 65 73 74 72 6f 79 20 50 31 20 2a 20 2a   VDestroy P1 * *
1c6f3 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69   P4 *.**.** P4 i
1c6f4 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  s the name of a 
1c6f5 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  virtual table in
1c6f6 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 43   database P1.  C
1c6f7 61 6c 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79  all the xDestroy
1c6f8 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68   method.** of th
1c6f9 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73  at table..*/.cas
1c6fa 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 3a 20 7b  e OP_VDestroy: {
1c6fb 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68  .  p->inVtabMeth
1c6fc 6f 64 20 3d 20 32 3b 0a 20 20 72 63 20 3d 20 73  od = 2;.  rc = s
1c6fd 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65  qlite3VtabCallDe
1c6fe 73 74 72 6f 79 28 64 62 2c 20 70 4f 70 2d 3e 70  stroy(db, pOp->p
1c6ff 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  1, pOp->p4.z);. 
1c700 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64   p->inVtabMethod
1c701 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
1c702 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1c703 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1c704 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BLE */..#ifndef 
1c705 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1c706 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f  UALTABLE./* Opco
1c707 64 65 3a 20 56 4f 70 65 6e 20 50 31 20 2a 20 2a  de: VOpen P1 * *
1c708 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69   P4 *.**.** P4 i
1c709 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1c70a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f   virtual table o
1c70b 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65  bject, an sqlite
1c70c 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
1c70d 2e 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72  ..** P1 is a cur
1c70e 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20 54 68 69  sor number.  Thi
1c70f 73 20 6f 70 63 6f 64 65 20 6f 70 65 6e 73 20 61  s opcode opens a
1c710 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 76   cursor to the v
1c711 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20  irtual.** table 
1c712 61 6e 64 20 73 74 6f 72 65 73 20 74 68 61 74 20  and stores that 
1c713 63 75 72 73 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f  cursor in P1..*/
1c714 0a 63 61 73 65 20 4f 50 5f 56 4f 70 65 6e 3a 20  .case OP_VOpen: 
1c715 7b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  {.  VdbeCursor *
1c716 70 43 75 72 20 3d 20 30 3b 0a 20 20 73 71 6c 69  pCur = 0;.  sqli
1c717 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
1c718 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 30  *pVtabCursor = 0
1c719 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  ;..  sqlite3_vta
1c71a 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e  b *pVtab = pOp->
1c71b 70 34 2e 70 56 74 61 62 3b 0a 20 20 73 71 6c 69  p4.pVtab;.  sqli
1c71c 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1c71d 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d  ule = (sqlite3_m
1c71e 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70  odule *)pVtab->p
1c71f 4d 6f 64 75 6c 65 3b 0a 0a 20 20 61 73 73 65 72  Module;..  asser
1c720 74 28 70 56 74 61 62 20 26 26 20 70 4d 6f 64 75  t(pVtab && pModu
1c721 6c 65 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  le);.  if( sqlit
1c722 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1c723 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1c724 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63  _to_misuse;.  rc
1c725 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65   = pModule->xOpe
1c726 6e 28 70 56 74 61 62 2c 20 26 70 56 74 61 62 43  n(pVtab, &pVtabC
1c727 75 72 73 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65  ursor);.  sqlite
1c728 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1c729 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
1c72a 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a  rrMsg = pVtab->z
1c72b 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d  ErrMsg;.  pVtab-
1c72c 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1c72d 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1c72e 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61  yOn(db) ) goto a
1c72f 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1c730 73 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45  se;.  if( SQLITE
1c731 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f  _OK==rc ){.    /
1c732 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c  * Initialize sql
1c733 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
1c734 20 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20   base class */. 
1c735 20 20 20 70 56 74 61 62 43 75 72 73 6f 72 2d 3e     pVtabCursor->
1c736 70 56 74 61 62 20 3d 20 70 56 74 61 62 3b 0a 0a  pVtab = pVtab;..
1c737 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 73      /* Initialis
1c738 65 20 76 64 62 65 20 63 75 72 73 6f 72 20 6f 62  e vdbe cursor ob
1c739 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 43 75 72  ject */.    pCur
1c73a 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f   = allocateCurso
1c73b 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 26 70  r(p, pOp->p1, &p
1c73c 4f 70 5b 2d 31 5d 2c 20 2d 31 2c 20 30 29 3b 0a  Op[-1], -1, 0);.
1c73d 20 20 20 20 69 66 28 20 70 43 75 72 20 29 7b 0a      if( pCur ){.
1c73e 20 20 20 20 20 20 70 43 75 72 2d 3e 70 56 74 61        pCur->pVta
1c73f 62 43 75 72 73 6f 72 20 3d 20 70 56 74 61 62 43  bCursor = pVtabC
1c740 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 43 75  ursor;.      pCu
1c741 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74  r->pModule = pVt
1c742 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d  abCursor->pVtab-
1c743 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65  >pModule;.    }e
1c744 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  lse{.      db->m
1c745 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1c746 0a 20 20 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e  .      pModule->
1c747 78 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73  xClose(pVtabCurs
1c748 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  or);.    }.  }. 
1c749 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1c74a 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1c74b 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1c74c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1c74d 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1c74e 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46 69  E./* Opcode: VFi
1c74f 6c 74 65 72 20 50 31 20 50 32 20 50 33 20 50 34  lter P1 P2 P3 P4
1c750 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61   *.**.** P1 is a
1c751 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 75   cursor opened u
1c752 73 69 6e 67 20 56 4f 70 65 6e 2e 20 20 50 32 20  sing VOpen.  P2 
1c753 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 74 6f  is an address to
1c754 20 6a 75 6d 70 20 74 6f 20 69 66 0a 2a 2a 20 74   jump to if.** t
1c755 68 65 20 66 69 6c 74 65 72 65 64 20 72 65 73 75  he filtered resu
1c756 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 2e  lt set is empty.
1c757 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 65 69 74  .**.** P4 is eit
1c758 68 65 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74  her NULL or a st
1c759 72 69 6e 67 20 74 68 61 74 20 77 61 73 20 67 65  ring that was ge
1c75a 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 78  nerated by the x
1c75b 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74  BestIndex.** met
1c75c 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  hod of the modul
1c75d 65 2e 20 20 54 68 65 20 69 6e 74 65 72 70 72 65  e.  The interpre
1c75e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 50 34  tation of the P4
1c75f 20 73 74 72 69 6e 67 20 69 73 20 6c 65 66 74 0a   string is left.
1c760 2a 2a 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65  ** to the module
1c761 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
1c762 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1c763 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78  de invokes the x
1c764 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 6f 6e  Filter method on
1c765 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1c766 6c 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  le specified.** 
1c767 62 79 20 50 31 2e 20 20 54 68 65 20 69 6e 74 65  by P1.  The inte
1c768 67 65 72 20 71 75 65 72 79 20 70 6c 61 6e 20 70  ger query plan p
1c769 61 72 61 6d 65 74 65 72 20 74 6f 20 78 46 69 6c  arameter to xFil
1c76a 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ter is stored in
1c76b 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 2e   register.** P3.
1c76c 20 52 65 67 69 73 74 65 72 20 50 33 2b 31 20 73   Register P3+1 s
1c76d 74 6f 72 65 73 20 74 68 65 20 61 72 67 63 20 70  tores the argc p
1c76e 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 70  arameter to be p
1c76f 61 73 73 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20  assed to the.** 
1c770 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 20  xFilter method. 
1c771 52 65 67 69 73 74 65 72 73 20 50 33 2b 32 2e 2e  Registers P3+2..
1c772 50 33 2b 31 2b 61 72 67 63 20 61 72 65 20 74 68  P3+1+argc are th
1c773 65 20 61 72 67 63 0a 2a 2a 20 61 64 64 69 74 69  e argc.** additi
1c774 6f 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20  onal parameters 
1c775 77 68 69 63 68 20 61 72 65 20 70 61 73 73 65 64  which are passed
1c776 20 74 6f 0a 2a 2a 20 78 46 69 6c 74 65 72 20 61   to.** xFilter a
1c777 73 20 61 72 67 76 2e 20 52 65 67 69 73 74 65 72  s argv. Register
1c778 20 50 33 2b 32 20 62 65 63 6f 6d 65 73 20 61 72   P3+2 becomes ar
1c779 67 76 5b 30 5d 20 77 68 65 6e 20 70 61 73 73 65  gv[0] when passe
1c77a 64 20 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a  d to xFilter..**
1c77b 0a 2a 2a 20 41 20 6a 75 6d 70 20 69 73 20 6d 61  .** A jump is ma
1c77c 64 65 20 74 6f 20 50 32 20 69 66 20 74 68 65 20  de to P2 if the 
1c77d 72 65 73 75 6c 74 20 73 65 74 20 61 66 74 65 72  result set after
1c77e 20 66 69 6c 74 65 72 69 6e 67 20 77 6f 75 6c 64   filtering would
1c77f 20 62 65 20 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61   be empty..*/.ca
1c780 73 65 20 4f 50 5f 56 46 69 6c 74 65 72 3a 20 7b  se OP_VFilter: {
1c781 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20     /* jump */.  
1c782 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e 74 20  int nArg;.  int 
1c783 69 51 75 65 72 79 3b 0a 20 20 63 6f 6e 73 74 20  iQuery;.  const 
1c784 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
1c785 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a  pModule;.  Mem *
1c786 70 51 75 65 72 79 20 3d 20 26 70 2d 3e 61 4d 65  pQuery = &p->aMe
1c787 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65  m[pOp->p3];.  Me
1c788 6d 20 2a 70 41 72 67 63 20 3d 20 26 70 51 75 65  m *pArgc = &pQue
1c789 72 79 5b 31 5d 3b 0a 20 20 73 71 6c 69 74 65 33  ry[1];.  sqlite3
1c78a 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56  _vtab_cursor *pV
1c78b 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c  tabCursor;.  sql
1c78c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1c78d 3b 0a 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  ;..  VdbeCursor 
1c78e 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72  *pCur = p->apCsr
1c78f 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a 20 20 52 45  [pOp->p1];..  RE
1c790 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1c791 2d 3e 70 33 2c 20 70 51 75 65 72 79 29 3b 0a 20  ->p3, pQuery);. 
1c792 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70   assert( pCur->p
1c793 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20  VtabCursor );.  
1c794 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43  pVtabCursor = pC
1c795 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b  ur->pVtabCursor;
1c796 0a 20 20 70 56 74 61 62 20 3d 20 70 56 74 61 62  .  pVtab = pVtab
1c797 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20  Cursor->pVtab;. 
1c798 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 62   pModule = pVtab
1c799 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a  ->pModule;..  /*
1c79a 20 47 72 61 62 20 74 68 65 20 69 6e 64 65 78 20   Grab the index 
1c79b 6e 75 6d 62 65 72 20 61 6e 64 20 61 72 67 63 20  number and argc 
1c79c 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20  parameters */.  
1c79d 61 73 73 65 72 74 28 20 28 70 51 75 65 72 79 2d  assert( (pQuery-
1c79e 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29 21  >flags&MEM_Int)!
1c79f 3d 30 20 26 26 20 70 41 72 67 63 2d 3e 66 6c 61  =0 && pArgc->fla
1c7a0 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  gs==MEM_Int );. 
1c7a1 20 6e 41 72 67 20 3d 20 28 69 6e 74 29 70 41 72   nArg = (int)pAr
1c7a2 67 63 2d 3e 75 2e 69 3b 0a 20 20 69 51 75 65 72  gc->u.i;.  iQuer
1c7a3 79 20 3d 20 28 69 6e 74 29 70 51 75 65 72 79 2d  y = (int)pQuery-
1c7a4 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f  >u.i;..  /* Invo
1c7a5 6b 65 20 74 68 65 20 78 46 69 6c 74 65 72 20 6d  ke the xFilter m
1c7a6 65 74 68 6f 64 20 2a 2f 0a 20 20 7b 0a 20 20 20  ethod */.  {.   
1c7a7 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20   int res = 0;.  
1c7a8 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4d 65 6d    int i;.    Mem
1c7a9 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d 3e 61 70   **apArg = p->ap
1c7aa 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 69 20 3d  Arg;.    for(i =
1c7ab 20 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b 2b 29   0; i<nArg; i++)
1c7ac 7b 0a 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d  {.      apArg[i]
1c7ad 20 3d 20 26 70 41 72 67 63 5b 69 2b 31 5d 3b 0a   = &pArgc[i+1];.
1c7ae 20 20 20 20 20 20 73 74 6f 72 65 54 79 70 65 49        storeTypeI
1c7af 6e 66 6f 28 61 70 41 72 67 5b 69 5d 2c 20 30 29  nfo(apArg[i], 0)
1c7b0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
1c7b1 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1c7b2 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1c7b3 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1c7b4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61  ;.    sqlite3Vta
1c7b5 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20  bLock(pVtab);.  
1c7b6 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
1c7b7 64 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  d = 1;.    rc = 
1c7b8 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 65 72  pModule->xFilter
1c7b9 28 70 56 74 61 62 43 75 72 73 6f 72 2c 20 69 51  (pVtabCursor, iQ
1c7ba 75 65 72 79 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c  uery, pOp->p4.z,
1c7bb 20 6e 41 72 67 2c 20 61 70 41 72 67 29 3b 0a 20   nArg, apArg);. 
1c7bc 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68     p->inVtabMeth
1c7bd 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  od = 0;.    sqli
1c7be 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1c7bf 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70  >zErrMsg);.    p
1c7c0 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61  ->zErrMsg = pVta
1c7c1 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20  b->zErrMsg;.    
1c7c2 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
1c7c3 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   0;.    sqlite3V
1c7c4 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56  tabUnlock(db, pV
1c7c5 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63  tab);.    if( rc
1c7c6 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1c7c7 20 20 20 20 20 72 65 73 20 3d 20 70 4d 6f 64 75       res = pModu
1c7c8 6c 65 2d 3e 78 45 6f 66 28 70 56 74 61 62 43 75  le->xEof(pVtabCu
1c7c9 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rsor);.    }.   
1c7ca 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1c7cb 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20  tyOn(db) ) goto 
1c7cc 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1c7cd 75 73 65 3b 0a 0a 20 20 20 20 69 66 28 20 72 65  use;..    if( re
1c7ce 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20  s ){.      pc = 
1c7cf 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
1c7d0 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 6e   }.  }.  pCur->n
1c7d1 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20 62  ullRow = 0;..  b
1c7d2 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1c7d3 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1c7d4 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1c7d5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1c7d6 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1c7d7 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 6f 77 69  /* Opcode: VRowi
1c7d8 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  d P1 P2 * * *.**
1c7d9 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 74 6f 20 72  .** Store into r
1c7da 65 67 69 73 74 65 72 20 50 32 20 20 74 68 65 20  egister P2  the 
1c7db 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 74 68 65 20  rowid of.** the 
1c7dc 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68  virtual-table th
1c7dd 61 74 20 74 68 65 20 50 31 20 63 75 72 73 6f 72  at the P1 cursor
1c7de 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e   is pointing to.
1c7df 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52 6f 77  .*/.case OP_VRow
1c7e0 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  id: {           
1c7e1 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1c7e2 65 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ease */.  sqlite
1c7e3 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20  3_vtab *pVtab;. 
1c7e4 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
1c7e5 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
1c7e6 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
1c7e7 52 6f 77 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  Row;.  VdbeCurso
1c7e8 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43  r *pCur = p->apC
1c7e9 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a 20 20  sr[pOp->p1];..  
1c7ea 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56  assert( pCur->pV
1c7eb 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69  tabCursor );.  i
1c7ec 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77  f( pCur->nullRow
1c7ed 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20   ){.    break;. 
1c7ee 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75   }.  pVtab = pCu
1c7ef 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e  r->pVtabCursor->
1c7f0 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65  pVtab;.  pModule
1c7f1 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   = pVtab->pModul
1c7f2 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f  e;.  assert( pMo
1c7f3 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b 0a  dule->xRowid );.
1c7f4 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1c7f5 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74  etyOff(db) ) got
1c7f6 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1c7f7 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70 4d  isuse;.  rc = pM
1c7f8 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 70 43  odule->xRowid(pC
1c7f9 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2c  ur->pVtabCursor,
1c7fa 20 26 69 52 6f 77 29 3b 0a 20 20 73 71 6c 69 74   &iRow);.  sqlit
1c7fb 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1c7fc 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a  zErrMsg);.  p->z
1c7fd 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e  ErrMsg = pVtab->
1c7fe 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62  zErrMsg;.  pVtab
1c7ff 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1c800 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1c801 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20  tyOn(db) ) goto 
1c802 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1c803 75 73 65 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70  use;.  MemSetTyp
1c804 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1c805 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e  Int);.  pOut->u.
1c806 69 20 3d 20 69 52 6f 77 3b 0a 20 20 62 72 65 61  i = iRow;.  brea
1c807 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
1c808 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1c809 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e  ALTABLE */..#ifn
1c80a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1c80b 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20  VIRTUALTABLE./* 
1c80c 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d 6e 20  Opcode: VColumn 
1c80d 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1c80e 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 76 61 6c  ** Store the val
1c80f 75 65 20 6f 66 20 74 68 65 20 50 32 2d 74 68 20  ue of the P2-th 
1c810 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 68 65  column of.** the
1c811 20 72 6f 77 20 6f 66 20 74 68 65 20 76 69 72 74   row of the virt
1c812 75 61 6c 2d 74 61 62 6c 65 20 74 68 61 74 20 74  ual-table that t
1c813 68 65 20 0a 2a 2a 20 50 31 20 63 75 72 73 6f 72  he .** P1 cursor
1c814 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   is pointing to 
1c815 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 33  into register P3
1c816 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 6f  ..*/.case OP_VCo
1c817 6c 75 6d 6e 3a 20 7b 0a 20 20 73 71 6c 69 74 65  lumn: {.  sqlite
1c818 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20  3_vtab *pVtab;. 
1c819 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
1c81a 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
1c81b 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20    Mem *pDest;.  
1c81c 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1c81d 73 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 56 64 62  sContext;..  Vdb
1c81e 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20  eCursor *pCur = 
1c81f 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1c820 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  ];.  assert( pCu
1c821 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29  r->pVtabCursor )
1c822 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1c823 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33  >p3>0 && pOp->p3
1c824 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
1c825 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  Dest = &p->aMem[
1c826 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20  pOp->p3];.  if( 
1c827 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b  pCur->nullRow ){
1c828 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1c829 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 44 65 73 74  MemSetNull(pDest
1c82a 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  );.    break;.  
1c82b 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75 72  }.  pVtab = pCur
1c82c 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  ->pVtabCursor->p
1c82d 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20  Vtab;.  pModule 
1c82e 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  = pVtab->pModule
1c82f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f 64  ;.  assert( pMod
1c830 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a  ule->xColumn );.
1c831 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74 65    memset(&sConte
1c832 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 43  xt, 0, sizeof(sC
1c833 6f 6e 74 65 78 74 29 29 3b 0a 0a 20 20 2f 2a 20  ontext));..  /* 
1c834 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c 20  The output cell 
1c835 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 76 65  may already have
1c836 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61   a buffer alloca
1c837 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74  ted. Move.  ** t
1c838 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65  he current conte
1c839 6e 74 73 20 74 6f 20 73 43 6f 6e 74 65 78 74 2e  nts to sContext.
1c83a 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65  s so in case the
1c83b 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 0a   user-function .
1c83c 20 20 2a 2a 20 63 61 6e 20 75 73 65 20 74 68 65    ** can use the
1c83d 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74   already allocat
1c83e 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61  ed buffer instea
1c83f 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20  d of allocating 
1c840 61 20 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e  a .  ** new one.
1c841 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
1c842 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 43 6f 6e  dbeMemMove(&sCon
1c843 74 65 78 74 2e 73 2c 20 70 44 65 73 74 29 3b 0a  text.s, pDest);.
1c844 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1c845 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 4d 45  (&sContext.s, ME
1c846 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 69 66 28 20  M_Null);..  if( 
1c847 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1c848 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1c849 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1c84a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d  .  rc = pModule-
1c84b 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e 70  >xColumn(pCur->p
1c84c 56 74 61 62 43 75 72 73 6f 72 2c 20 26 73 43 6f  VtabCursor, &sCo
1c84d 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b  ntext, pOp->p2);
1c84e 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1c84f 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1c850 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d  ;.  p->zErrMsg =
1c851 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b   pVtab->zErrMsg;
1c852 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  .  pVtab->zErrMs
1c853 67 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 70  g = 0;..  /* Cop
1c854 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  y the result of 
1c855 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  the function to 
1c856 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72 2e  the P3 register.
1c857 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73   We.  ** do this
1c858 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
1c859 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e  hether or not an
1c85a 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 74   error occured t
1c85b 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a  o ensure any.  *
1c85c 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61  * dynamic alloca
1c85d 74 69 6f 6e 20 69 6e 20 73 43 6f 6e 74 65 78 74  tion in sContext
1c85e 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 75 63 74  .s (a Mem struct
1c85f 29 20 69 73 20 20 72 65 6c 65 61 73 65 64 2e 0a  ) is  released..
1c860 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
1c861 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
1c862 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e  (&sContext.s, en
1c863 63 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53  coding);.  REGIS
1c864 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1c865 33 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 71 6c  3, pDest);.  sql
1c866 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
1c867 70 44 65 73 74 2c 20 26 73 43 6f 6e 74 65 78 74  pDest, &sContext
1c868 2e 73 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41  .s);.  UPDATE_MA
1c869 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 73 74  X_BLOBSIZE(pDest
1c86a 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  );..  if( sqlite
1c86b 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b  3SafetyOn(db) ){
1c86c 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f  .    goto abort_
1c86d 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1c86e 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1c86f 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 44  VdbeMemTooBig(pD
1c870 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  est) ){.    goto
1c871 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20   too_big;.  }.  
1c872 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1c873 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1c874 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1c875 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1c876 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1c877 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e 65 78  ./* Opcode: VNex
1c878 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  t P1 P2 * * *.**
1c879 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69 72 74  .** Advance virt
1c87a 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74 6f 20  ual table P1 to 
1c87b 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 20  the next row in 
1c87c 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 20 61  its result set a
1c87d 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69 6e  nd.** jump to in
1c87e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 20 4f  struction P2.  O
1c87f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74 75 61  r, if the virtua
1c880 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65 61 63  l table has reac
1c881 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f  hed.** the end o
1c882 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74  f its result set
1c883 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f  , then fall thro
1c884 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
1c885 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
1c886 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a 20 7b  case OP_VNext: {
1c887 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20     /* jump */.  
1c888 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1c889 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  tab;.  const sql
1c88a 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1c88b 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 20  dule;.  int res 
1c88c 3d 20 30 3b 0a 0a 20 20 56 64 62 65 43 75 72 73  = 0;..  VdbeCurs
1c88d 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70  or *pCur = p->ap
1c88e 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1c88f 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56  assert( pCur->pV
1c890 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69  tabCursor );.  i
1c891 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77  f( pCur->nullRow
1c892 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20   ){.    break;. 
1c893 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75   }.  pVtab = pCu
1c894 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e  r->pVtabCursor->
1c895 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65  pVtab;.  pModule
1c896 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   = pVtab->pModul
1c897 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f  e;.  assert( pMo
1c898 64 75 6c 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a  dule->xNext );..
1c899 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
1c89a 78 4e 65 78 74 28 29 20 6d 65 74 68 6f 64 20 6f  xNext() method o
1c89b 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68  f the module. Th
1c89c 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f  ere is no way fo
1c89d 72 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72  r the.  ** under
1c89e 6c 79 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61  lying implementa
1c89f 74 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 61  tion to return a
1c8a0 6e 20 65 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f  n error if one o
1c8a1 63 63 75 72 73 20 64 75 72 69 6e 67 0a 20 20 2a  ccurs during.  *
1c8a2 2a 20 78 4e 65 78 74 28 29 2e 20 49 6e 73 74 65  * xNext(). Inste
1c8a3 61 64 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ad, if an error 
1c8a4 6f 63 63 75 72 73 2c 20 74 72 75 65 20 69 73 20  occurs, true is 
1c8a5 72 65 74 75 72 6e 65 64 20 28 69 6e 64 69 63 61  returned (indica
1c8a6 74 69 6e 67 20 74 68 61 74 20 0a 20 20 2a 2a 20  ting that .  ** 
1c8a7 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c  data is availabl
1c8a8 65 29 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72  e) and the error
1c8a9 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 77   code returned w
1c8aa 68 65 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20  hen xColumn or. 
1c8ab 20 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d   ** some other m
1c8ac 65 74 68 6f 64 20 69 73 20 6e 65 78 74 20 69 6e  ethod is next in
1c8ad 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 73 61 76  voked on the sav
1c8ae 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
1c8af 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69  cursor..  */.  i
1c8b0 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1c8b1 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61  Off(db) ) goto a
1c8b2 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1c8b3 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61  se;.  sqlite3Vta
1c8b4 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20  bLock(pVtab);.  
1c8b5 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20  p->inVtabMethod 
1c8b6 3d 20 31 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64  = 1;.  rc = pMod
1c8b7 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 2d  ule->xNext(pCur-
1c8b8 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  >pVtabCursor);. 
1c8b9 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64   p->inVtabMethod
1c8ba 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44   = 0;.  sqlite3D
1c8bb 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
1c8bc 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72  rMsg);.  p->zErr
1c8bd 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72  Msg = pVtab->zEr
1c8be 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a  rMsg;.  pVtab->z
1c8bf 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71  ErrMsg = 0;.  sq
1c8c0 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
1c8c1 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 69 66  db, pVtab);.  if
1c8c2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1c8c3 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 70 4d 6f  ){.    res = pMo
1c8c4 64 75 6c 65 2d 3e 78 45 6f 66 28 70 43 75 72 2d  dule->xEof(pCur-
1c8c5 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  >pVtabCursor);. 
1c8c6 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1c8c7 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1c8c8 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1c8c9 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20  _misuse;..  if( 
1c8ca 21 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49  !res ){.    /* I
1c8cb 66 20 74 68 65 72 65 20 69 73 20 64 61 74 61 2c  f there is data,
1c8cc 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20   jump to P2 */. 
1c8cd 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1c8ce 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  - 1;.  }.  break
1c8cf 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1c8d0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1c8d1 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
1c8d2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1c8d3 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
1c8d4 70 63 6f 64 65 3a 20 56 52 65 6e 61 6d 65 20 50  pcode: VRename P
1c8d5 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  1 * * P4 *.**.**
1c8d6 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
1c8d7 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61   to a virtual ta
1c8d8 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73  ble object, an s
1c8d9 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
1c8da 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f  cture..** This o
1c8db 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68  pcode invokes th
1c8dc 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
1c8dd 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 20  xRename method. 
1c8de 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20  The value.** in 
1c8df 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 70  register P1 is p
1c8e0 61 73 73 65 64 20 61 73 20 74 68 65 20 7a 4e 61  assed as the zNa
1c8e1 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  me argument to t
1c8e2 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f  he xRename metho
1c8e3 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52  d..*/.case OP_VR
1c8e4 65 6e 61 6d 65 3a 20 7b 0a 20 20 73 71 6c 69 74  ename: {.  sqlit
1c8e5 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d  e3_vtab *pVtab =
1c8e6 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a   pOp->p4.pVtab;.
1c8e7 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 20 3d 20 26    Mem *pName = &
1c8e8 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  p->aMem[pOp->p1]
1c8e9 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61  ;.  assert( pVta
1c8ea 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e  b->pModule->xRen
1c8eb 61 6d 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45  ame );.  REGISTE
1c8ec 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c  R_TRACE(pOp->p1,
1c8ed 20 70 4e 61 6d 65 29 3b 0a 0a 20 20 53 74 72 69   pName);..  Stri
1c8ee 6e 67 69 66 79 28 70 4e 61 6d 65 2c 20 65 6e 63  ngify(pName, enc
1c8ef 6f 64 69 6e 67 29 3b 0a 0a 20 20 69 66 28 20 73  oding);..  if( s
1c8f0 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1c8f1 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1c8f2 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1c8f3 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63    sqlite3VtabLoc
1c8f4 6b 28 70 56 74 61 62 29 3b 0a 20 20 72 63 20 3d  k(pVtab);.  rc =
1c8f5 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d   pVtab->pModule-
1c8f6 3e 78 52 65 6e 61 6d 65 28 70 56 74 61 62 2c 20  >xRename(pVtab, 
1c8f7 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71 6c  pName->z);.  sql
1c8f8 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1c8f9 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d  ->zErrMsg);.  p-
1c8fa 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62  >zErrMsg = pVtab
1c8fb 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74  ->zErrMsg;.  pVt
1c8fc 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1c8fd 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e  .  sqlite3VtabUn
1c8fe 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b  lock(db, pVtab);
1c8ff 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1c900 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1c901 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1c902 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 61 6b 3b  isuse;..  break;
1c903 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
1c904 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1c905 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
1c906 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65 20 50  pcode: VUpdate P
1c907 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1c908 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1c909 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20  er to a virtual 
1c90a 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e  table object, an
1c90b 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74   sqlite3_vtab st
1c90c 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73  ructure..** This
1c90d 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20   opcode invokes 
1c90e 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1c90f 67 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64  g xUpdate method
1c910 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a 20 61  . P2 values.** a
1c911 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 6d 65  re contiguous me
1c912 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 61 72 74  mory cells start
1c913 69 6e 67 20 61 74 20 50 33 20 74 6f 20 70 61 73  ing at P3 to pas
1c914 73 20 74 6f 20 74 68 65 20 78 55 70 64 61 74 65  s to the xUpdate
1c915 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 2e   .** invocation.
1c916 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   The value in re
1c917 67 69 73 74 65 72 20 28 50 33 2b 50 32 2d 31 29  gister (P3+P2-1)
1c918 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
1c919 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 65 6c 65  the .** p2th ele
1c91a 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 67 76  ment of the argv
1c91b 20 61 72 72 61 79 20 70 61 73 73 65 64 20 74 6f   array passed to
1c91c 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20   xUpdate..**.** 
1c91d 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 74 68  The xUpdate meth
1c91e 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 44 45 4c  od will do a DEL
1c91f 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 45 52 54  ETE or an INSERT
1c920 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 68 65   or both..** The
1c921 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65 6e 74   argv[0] element
1c922 20 28 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f   (which correspo
1c923 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 20 63 65  nds to memory ce
1c924 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 74 68 65  ll P3).** is the
1c925 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f 77 20   rowid of a row 
1c926 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 66 20 61  to delete.  If a
1c927 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c 4c 20 74  rgv[0] is NULL t
1c928 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c 65 74  hen no .** delet
1c929 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54 68 65  ion occurs.  The
1c92a 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d 65 6e 74   argv[1] element
1c92b 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66   is the rowid of
1c92c 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77   the new .** row
1c92d 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 4e  .  This can be N
1c92e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68 65 20  ULL to have the 
1c92f 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 65  virtual table se
1c930 6c 65 63 74 20 74 68 65 20 6e 65 77 20 0a 2a 2a  lect the new .**
1c931 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73 65 6c   rowid for itsel
1c932 66 2e 20 20 54 68 65 20 73 75 62 73 65 71 75 65  f.  The subseque
1c933 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  nt elements in t
1c934 68 65 20 61 72 72 61 79 20 61 72 65 20 0a 2a 2a  he array are .**
1c935 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 63   the values of c
1c936 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6e 65  olumns in the ne
1c937 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  w row..**.** If 
1c938 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 69 6e  P2==1 then no in
1c939 73 65 72 74 20 69 73 20 70 65 72 66 6f 72 6d 65  sert is performe
1c93a 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74  d.  argv[0] is t
1c93b 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 61  he rowid of.** a
1c93c 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 0a   row to delete..
1c93d 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 62 6f  **.** P1 is a bo
1c93e 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66 20 69  olean flag. If i
1c93f 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65  t is set to true
1c940 20 61 6e 64 20 74 68 65 20 78 55 70 64 61 74 65   and the xUpdate
1c941 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 63 63   call.** is succ
1c942 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74 68 65  essful, then the
1c943 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
1c944 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  by sqlite3_last_
1c945 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 0a  insert_rowid() .
1c946 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ** is set to the
1c947 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f   value of the ro
1c948 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77 20  wid for the row 
1c949 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e 0a 2a  just inserted..*
1c94a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64 61 74  /.case OP_VUpdat
1c94b 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  e: {.  sqlite3_v
1c94c 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70  tab *pVtab = pOp
1c94d 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 73 71  ->p4.pVtab;.  sq
1c94e 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1c94f 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33  odule = (sqlite3
1c950 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d  _module *)pVtab-
1c951 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20  >pModule;.  int 
1c952 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  nArg = pOp->p2;.
1c953 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c954 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29  4type==P4_VTAB )
1c955 3b 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d  ;.  if( pModule-
1c956 3e 78 55 70 64 61 74 65 3d 3d 30 20 29 7b 0a 20  >xUpdate==0 ){. 
1c957 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1c958 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1c959 20 64 62 2c 20 22 72 65 61 64 2d 6f 6e 6c 79 20   db, "read-only 
1c95a 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 72 63 20  table");.    rc 
1c95b 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1c95c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
1c95d 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   i;.    sqlite_i
1c95e 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20  nt64 rowid;.    
1c95f 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d  Mem **apArg = p-
1c960 3e 61 70 41 72 67 3b 0a 20 20 20 20 4d 65 6d 20  >apArg;.    Mem 
1c961 2a 70 58 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  *pX = &p->aMem[p
1c962 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 6f 72  Op->p3];.    for
1c963 28 69 3d 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b  (i=0; i<nArg; i+
1c964 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54  +){.      storeT
1c965 79 70 65 49 6e 66 6f 28 70 58 2c 20 30 29 3b 0a  ypeInfo(pX, 0);.
1c966 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d 20 3d        apArg[i] =
1c967 20 70 58 3b 0a 20 20 20 20 20 20 70 58 2b 2b 3b   pX;.      pX++;
1c968 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
1c969 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1c96a 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1c96b 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1c96c 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c      sqlite3VtabL
1c96d 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 20 20  ock(pVtab);.    
1c96e 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 55  rc = pModule->xU
1c96f 70 64 61 74 65 28 70 56 74 61 62 2c 20 6e 41 72  pdate(pVtab, nAr
1c970 67 2c 20 61 70 41 72 67 2c 20 26 72 6f 77 69 64  g, apArg, &rowid
1c971 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
1c972 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72  Free(db, p->zErr
1c973 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72  Msg);.    p->zEr
1c974 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45  rMsg = pVtab->zE
1c975 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62  rrMsg;.    pVtab
1c976 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1c977 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e     sqlite3VtabUn
1c978 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b  lock(db, pVtab);
1c979 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1c97a 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1c97b 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1c97c 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 69 66 28  _misuse;.    if(
1c97d 20 70 4f 70 2d 3e 70 31 20 26 26 20 72 63 3d 3d   pOp->p1 && rc==
1c97e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1c97f 20 20 20 61 73 73 65 72 74 28 20 6e 41 72 67 3e     assert( nArg>
1c980 31 20 26 26 20 61 70 41 72 67 5b 30 5d 20 26 26  1 && apArg[0] &&
1c981 20 28 61 70 41 72 67 5b 30 5d 2d 3e 66 6c 61 67   (apArg[0]->flag
1c982 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20  s&MEM_Null) );. 
1c983 20 20 20 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77       db->lastRow
1c984 69 64 20 3d 20 72 6f 77 69 64 3b 0a 20 20 20 20  id = rowid;.    
1c985 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65  }.    p->nChange
1c986 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  ++;.  }.  break;
1c987 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1c988 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1c989 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  TABLE */..#ifnde
1c98a 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  f  SQLITE_OMIT_P
1c98b 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 20  AGER_PRAGMAS./* 
1c98c 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f 75 6e  Opcode: Pagecoun
1c98d 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  t P1 P2 * * *.**
1c98e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75  .** Write the cu
1c98f 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
1c990 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73  pages in databas
1c991 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20 63  e P1 to memory c
1c992 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20  ell P2..*/.case 
1c993 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b 20  OP_Pagecount: { 
1c994 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75             /* ou
1c995 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
1c996 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d  .  int p1 = pOp-
1c997 3e 70 31 3b 20 0a 20 20 69 6e 74 20 6e 50 61 67  >p1; .  int nPag
1c998 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  e;.  Pager *pPag
1c999 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
1c99a 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 70  ePager(db->aDb[p
1c99b 31 5d 2e 70 42 74 29 3b 0a 0a 20 20 72 63 20 3d  1].pBt);..  rc =
1c99c 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
1c99d 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26  ecount(pPager, &
1c99e 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  nPage);.  if( rc
1c99f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1c9a0 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d     pOut->flags =
1c9a1 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4f   MEM_Int;.    pO
1c9a2 75 74 2d 3e 75 2e 69 20 3d 20 6e 50 61 67 65 3b  ut->u.i = nPage;
1c9a3 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1c9a4 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
1c9a5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1c9a6 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61  E./* Opcode: Tra
1c9a7 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  ce * * * P4 *.**
1c9a8 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e 67 20 69  .** If tracing i
1c9a9 73 20 65 6e 61 62 6c 65 64 20 28 62 79 20 74 68  s enabled (by th
1c9aa 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28  e sqlite3_trace(
1c9ab 29 29 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68  )) interface, th
1c9ac 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38 20  en.** the UTF-8 
1c9ad 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65 64  string contained
1c9ae 20 69 6e 20 50 34 20 69 73 20 65 6d 69 74 74 65   in P4 is emitte
1c9af 64 20 6f 6e 20 74 68 65 20 74 72 61 63 65 20 63  d on the trace c
1c9b0 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65  allback..*/.case
1c9b1 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 20 20 69   OP_Trace: {.  i
1c9b2 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a  f( pOp->p4.z ){.
1c9b3 20 20 20 20 69 66 28 20 64 62 2d 3e 78 54 72 61      if( db->xTra
1c9b4 63 65 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  ce ){.      db->
1c9b5 78 54 72 61 63 65 28 64 62 2d 3e 70 54 72 61 63  xTrace(db->pTrac
1c9b6 65 41 72 67 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29  eArg, pOp->p4.z)
1c9b7 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53  ;.    }.#ifdef S
1c9b8 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
1c9b9 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  if( (db->flags &
1c9ba 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65   SQLITE_SqlTrace
1c9bb 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
1c9bc 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
1c9bd 28 22 53 51 4c 2d 74 72 61 63 65 3a 20 25 73 5c  ("SQL-trace: %s\
1c9be 6e 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a  n", pOp->p4.z);.
1c9bf 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
1c9c0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a  SQLITE_DEBUG */.
1c9c1 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1c9c2 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4f 70 63 6f 64  endif.../* Opcod
1c9c3 65 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a 20 2a 20  e: Noop * * * * 
1c9c4 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f 74 68 69  *.**.** Do nothi
1c9c5 6e 67 2e 20 20 54 68 69 73 20 69 6e 73 74 72 75  ng.  This instru
1c9c6 63 74 69 6f 6e 20 69 73 20 6f 66 74 65 6e 20 75  ction is often u
1c9c7 73 65 66 75 6c 20 61 73 20 61 20 6a 75 6d 70 0a  seful as a jump.
1c9c8 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a  ** destination..
1c9c9 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 67  */./*.** The mag
1c9ca 69 63 20 45 78 70 6c 61 69 6e 20 6f 70 63 6f 64  ic Explain opcod
1c9cb 65 20 61 72 65 20 6f 6e 6c 79 20 69 6e 73 65 72  e are only inser
1c9cc 74 65 64 20 77 68 65 6e 20 65 78 70 6c 61 69 6e  ted when explain
1c9cd 3d 3d 32 20 28 77 68 69 63 68 0a 2a 2a 20 69 73  ==2 (which.** is
1c9ce 20 74 6f 20 73 61 79 20 77 68 65 6e 20 74 68 65   to say when the
1c9cf 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50   EXPLAIN QUERY P
1c9d0 4c 41 4e 20 73 79 6e 74 61 78 20 69 73 20 75 73  LAN syntax is us
1c9d1 65 64 2e 29 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ed.).** This opc
1c9d2 6f 64 65 20 72 65 63 6f 72 64 73 20 69 6e 66 6f  ode records info
1c9d3 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65  rmation from the
1c9d4 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 49 74 20   optimizer.  It 
1c9d5 69 73 20 74 68 65 0a 2a 2a 20 74 68 65 20 73 61  is the.** the sa
1c9d6 6d 65 20 61 73 20 61 20 6e 6f 2d 6f 70 2e 20 20  me as a no-op.  
1c9d7 54 68 69 73 20 6f 70 63 6f 64 65 73 6e 65 76 65  This opcodesneve
1c9d8 72 20 61 70 70 65 61 72 73 20 69 6e 20 61 20 72  r appears in a r
1c9d9 65 61 6c 20 56 4d 20 70 72 6f 67 72 61 6d 2e 0a  eal VM program..
1c9da 2a 2f 0a 64 65 66 61 75 6c 74 3a 20 7b 20 20 20  */.default: {   
1c9db 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69         /* This i
1c9dc 73 20 72 65 61 6c 6c 79 20 4f 50 5f 4e 6f 6f 70  s really OP_Noop
1c9dd 20 61 6e 64 20 4f 50 5f 45 78 70 6c 61 69 6e 20   and OP_Explain 
1c9de 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  */.  break;.}../
1c9df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c9e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c9e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c9e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1c9e4 20 54 68 65 20 63 61 73 65 73 20 6f 66 20 74 68   The cases of th
1c9e5 65 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65  e switch stateme
1c9e6 6e 74 20 61 62 6f 76 65 20 74 68 69 73 20 6c 69  nt above this li
1c9e7 6e 65 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65  ne should all be
1c9e8 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 62 79 20   indented.** by 
1c9e9 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 20 74  6 spaces.  But t
1c9ea 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 36 20 73  he left-most 6 s
1c9eb 70 61 63 65 73 20 68 61 76 65 20 62 65 65 6e 20  paces have been 
1c9ec 72 65 6d 6f 76 65 64 20 74 6f 20 69 6d 70 72 6f  removed to impro
1c9ed 76 65 20 74 68 65 0a 2a 2a 20 72 65 61 64 61 62  ve the.** readab
1c9ee 69 6c 69 74 79 2e 20 20 46 72 6f 6d 20 74 68 69  ility.  From thi
1c9ef 73 20 70 6f 69 6e 74 20 6f 6e 20 64 6f 77 6e 2c  s point on down,
1c9f0 20 74 68 65 20 6e 6f 72 6d 61 6c 20 69 6e 64 65   the normal inde
1c9f1 6e 74 61 74 69 6f 6e 20 72 75 6c 65 73 20 61 72  ntation rules ar
1c9f2 65 0a 2a 2a 20 72 65 73 74 6f 72 65 64 2e 0a 2a  e.** restored..*
1c9f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c9f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c9f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c9f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c9f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20  ************/.  
1c9f8 20 20 7d 0a 0a 23 69 66 64 65 66 20 56 44 42 45    }..#ifdef VDBE
1c9f9 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 7b 0a 20  _PROFILE.    {. 
1c9fa 20 20 20 20 20 75 36 34 20 65 6c 61 70 73 65 64       u64 elapsed
1c9fb 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65   = sqlite3Hwtime
1c9fc 28 29 20 2d 20 73 74 61 72 74 3b 0a 20 20 20 20  () - start;.    
1c9fd 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 20 2b 3d    pOp->cycles +=
1c9fe 20 65 6c 61 70 73 65 64 3b 0a 20 20 20 20 20 20   elapsed;.      
1c9ff 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23 69 66 20  pOp->cnt++;.#if 
1ca00 30 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74  0.        fprint
1ca01 66 28 73 74 64 6f 75 74 2c 20 22 25 31 30 6c 6c  f(stdout, "%10ll
1ca02 75 20 22 2c 20 65 6c 61 70 73 65 64 29 3b 0a 20  u ", elapsed);. 
1ca03 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ca04 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f 75 74  bePrintOp(stdout
1ca05 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d 3e 61 4f  , origPc, &p->aO
1ca06 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 65 6e 64  p[origPc]);.#end
1ca07 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  if.    }.#endif.
1ca08 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  .    /* The foll
1ca09 6f 77 69 6e 67 20 63 6f 64 65 20 61 64 64 73 20  owing code adds 
1ca0a 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 61  nothing to the a
1ca0b 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e 61 6c  ctual functional
1ca0c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  ity.    ** of th
1ca0d 65 20 70 72 6f 67 72 61 6d 2e 20 20 49 74 20 69  e program.  It i
1ca0e 73 20 6f 6e 6c 79 20 68 65 72 65 20 66 6f 72 20  s only here for 
1ca0f 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
1ca10 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a 20 4f 6e  gging..    ** On
1ca11 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
1ca12 20 69 74 20 64 6f 65 73 20 62 75 72 6e 20 43 50   it does burn CP
1ca13 55 20 63 79 63 6c 65 73 20 65 76 65 72 79 20 74  U cycles every t
1ca14 69 6d 65 20 74 68 72 6f 75 67 68 0a 20 20 20 20  ime through.    
1ca15 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 74 6f 72  ** the evaluator
1ca16 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20 63 61   loop.  So we ca
1ca17 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 74 20 77  n leave it out w
1ca18 68 65 6e 20 4e 44 45 42 55 47 20 69 73 20 64 65  hen NDEBUG is de
1ca19 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69  fined..    */.#i
1ca1a 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
1ca1b 20 61 73 73 65 72 74 28 20 70 63 3e 3d 2d 31 20   assert( pc>=-1 
1ca1c 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a  && pc<p->nOp );.
1ca1d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1ca1e 45 42 55 47 0a 20 20 20 20 69 66 28 20 70 2d 3e  EBUG.    if( p->
1ca1f 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 69  trace ){.      i
1ca20 66 28 20 72 63 21 3d 30 20 29 20 66 70 72 69 6e  f( rc!=0 ) fprin
1ca21 74 66 28 70 2d 3e 74 72 61 63 65 2c 22 72 63 3d  tf(p->trace,"rc=
1ca22 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 20 20 20 20  %d\n",rc);.     
1ca23 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74 79 20   if( opProperty 
1ca24 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45  & OPFLG_OUT2_PRE
1ca25 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20  RELEASE ){.     
1ca26 20 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65     registerTrace
1ca27 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e  (p->trace, pOp->
1ca28 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20  p2, pOut);.     
1ca29 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f 70 50   }.      if( opP
1ca2a 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f  roperty & OPFLG_
1ca2b 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 20 20 20  OUT3 ){.        
1ca2c 72 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d  registerTrace(p-
1ca2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 33 2c  >trace, pOp->p3,
1ca2e 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a   pOut);.      }.
1ca2f 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 20 2f 2a      }.#endif  /*
1ca30 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f   SQLITE_DEBUG */
1ca31 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42  .#endif  /* NDEB
1ca32 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a 20 54 68  UG */.  }  /* Th
1ca33 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 6f 72  e end of the for
1ca34 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65 20 6c 6f  (;;) loop the lo
1ca35 6f 70 73 20 74 68 72 6f 75 67 68 20 6f 70 63 6f  ops through opco
1ca36 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  des */..  /* If 
1ca37 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f  we reach this po
1ca38 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  int, it means th
1ca39 61 74 20 65 78 65 63 75 74 69 6f 6e 20 69 73 20  at execution is 
1ca3a 66 69 6e 69 73 68 65 64 20 77 69 74 68 0a 20 20  finished with.  
1ca3b 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 66 20 73  ** an error of s
1ca3c 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20 2a 2f 0a 76  ome kind..  */.v
1ca3d 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3a 0a  dbe_error_halt:.
1ca3e 20 20 61 73 73 65 72 74 28 20 72 63 20 29 3b 0a    assert( rc );.
1ca3f 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
1ca40 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28  sqlite3VdbeHalt(
1ca41 70 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  p);.  if( rc==SQ
1ca42 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
1ca43 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69   ) db->mallocFai
1ca44 6c 65 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20  led = 1;.  rc = 
1ca45 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20  SQLITE_ERROR;.. 
1ca46 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20   /* This is the 
1ca47 6f 6e 6c 79 20 77 61 79 20 6f 75 74 20 6f 66 20  only way out of 
1ca48 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 20  this procedure. 
1ca49 20 57 65 20 68 61 76 65 20 74 6f 0a 20 20 2a 2a   We have to.  **
1ca4a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 75 74   release the mut
1ca4b 65 78 65 73 20 6f 6e 20 62 74 72 65 65 73 20 74  exes on btrees t
1ca4c 68 61 74 20 77 65 72 65 20 61 63 71 75 69 72 65  hat were acquire
1ca4d 64 20 61 74 20 74 68 65 0a 20 20 2a 2a 20 74 6f  d at the.  ** to
1ca4e 70 2e 20 2a 2f 0a 76 64 62 65 5f 72 65 74 75 72  p. */.vdbe_retur
1ca4f 6e 3a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  n:.  sqlite3Btre
1ca50 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65  eMutexArrayLeave
1ca51 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20  (&p->aMutex);.  
1ca52 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
1ca53 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66   Jump to here if
1ca54 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
1ca55 62 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 51  b larger than SQ
1ca56 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a  LITE_MAX_LENGTH.
1ca57 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65    ** is encounte
1ca58 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f 62 69  red..  */.too_bi
1ca59 67 3a 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53  g:.  sqlite3SetS
1ca5a 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1ca5b 67 2c 20 64 62 2c 20 22 73 74 72 69 6e 67 20 6f  g, db, "string o
1ca5c 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 29  r blob too big")
1ca5d 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  ;.  rc = SQLITE_
1ca5e 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74 6f 20 76  TOOBIG;.  goto v
1ca5f 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a  dbe_error_halt;.
1ca60 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65  .  /* Jump to he
1ca61 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  re if a malloc()
1ca62 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f   fails..  */.no_
1ca63 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f  mem:.  db->mallo
1ca64 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73  cFailed = 1;.  s
1ca65 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1ca66 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1ca67 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22   "out of memory"
1ca68 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  );.  rc = SQLITE
1ca69 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 6f 20 76  _NOMEM;.  goto v
1ca6a 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a  dbe_error_halt;.
1ca6b 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65  .  /* Jump to he
1ca6c 72 65 20 66 6f 72 20 61 6e 20 53 51 4c 49 54 45  re for an SQLITE
1ca6d 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 2e 0a 20  _MISUSE error.. 
1ca6e 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f   */.abort_due_to
1ca6f 5f 6d 69 73 75 73 65 3a 0a 20 20 72 63 20 3d 20  _misuse:.  rc = 
1ca70 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1ca71 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e   /* Fall thru in
1ca72 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1ca73 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 4a  error */..  /* J
1ca74 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72 20  ump to here for 
1ca75 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f  any other kind o
1ca76 66 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20  f fatal error.  
1ca77 54 68 65 20 22 72 63 22 20 76 61 72 69 61 62 6c  The "rc" variabl
1ca78 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 6f  e.  ** should ho
1ca79 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6e 75 6d  ld the error num
1ca7a 62 65 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f  ber..  */.abort_
1ca7b 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3a 0a 20 20  due_to_error:.  
1ca7c 61 73 73 65 72 74 28 20 70 2d 3e 7a 45 72 72 4d  assert( p->zErrM
1ca7d 73 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 64  sg==0 );.  if( d
1ca7e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1ca7f 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ) rc = SQLITE_NO
1ca80 4d 45 4d 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  MEM;.  if( rc!=S
1ca81 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
1ca82 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  M ){.    sqlite3
1ca83 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1ca84 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
1ca85 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72   sqlite3ErrStr(r
1ca86 63 29 29 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f 20  c));.  }.  goto 
1ca87 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b  vdbe_error_halt;
1ca88 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68  ..  /* Jump to h
1ca89 65 72 65 20 69 66 20 74 68 65 20 73 71 6c 69 74  ere if the sqlit
1ca8a 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 41  e3_interrupt() A
1ca8b 50 49 20 73 65 74 73 20 74 68 65 20 69 6e 74 65  PI sets the inte
1ca8c 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c 61 67 2e  rrupt.  ** flag.
1ca8d 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f  .  */.abort_due_
1ca8e 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a 0a 20 20  to_interrupt:.  
1ca8f 61 73 73 65 72 74 28 20 64 62 2d 3e 75 31 2e 69  assert( db->u1.i
1ca90 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 3b 0a  sInterrupted );.
1ca91 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e    rc = SQLITE_IN
1ca92 54 45 52 52 55 50 54 3b 0a 20 20 70 2d 3e 72 63  TERRUPT;.  p->rc
1ca93 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33   = rc;.  sqlite3
1ca94 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1ca95 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
1ca96 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72   sqlite3ErrStr(r
1ca97 63 29 29 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65  c));.  goto vdbe
1ca98 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 7d 0a 0a  _error_halt;.}..
1ca99 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1ca9a 45 6e 64 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a  End of vdbe.c **
1ca9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1ca9e 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1ca9f 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 62  Begin file vdbeb
1caa0 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  lob.c **********
1caa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1caa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1caa3 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 31  /*.** 2007 May 1
1caa4 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1caa5 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1caa6 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1caa7 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1caa8 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1caa9 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1caaa 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1caab 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1caac 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1caad 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1caae 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1caaf 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1cab0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1cab1 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1cab2 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1cab3 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1cab4 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1cab5 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1cab6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cab7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cab8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cab9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1caba 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
1cabb 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
1cabc 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
1cabd 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42  incremental BLOB
1cabe 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a   I/O..**.** $Id:
1cabf 20 76 64 62 65 62 6c 6f 62 2e 63 2c 76 20 31 2e   vdbeblob.c,v 1.
1cac0 32 36 20 32 30 30 38 2f 31 30 2f 30 32 20 31 34  26 2008/10/02 14
1cac1 3a 34 39 3a 30 32 20 64 61 6e 69 65 6c 6b 31 39  :49:02 danielk19
1cac2 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69  77 Exp $.*/...#i
1cac3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1cac4 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a  T_INCRBLOB../*.*
1cac5 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65 33 5f  * Valid sqlite3_
1cac6 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20 70 6f  blob* handles po
1cac7 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f 62 20  int to Incrblob 
1cac8 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 74  structures..*/.t
1cac9 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e  ypedef struct In
1caca 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c 6f 62 3b  crblob Incrblob;
1cacb 0a 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f 62  .struct Incrblob
1cacc 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20   {.  int flags; 
1cacd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cace 43 6f 70 79 20 6f 66 20 22 66 6c 61 67 73 22 20  Copy of "flags" 
1cacf 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
1cad0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 20 2a 2f  3_blob_open() */
1cad1 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20  .  int nByte;   
1cad2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
1cad3 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c 6f 62 2c  ze of open blob,
1cad4 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69   in bytes */.  i
1cad5 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20  nt iOffset;     
1cad6 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f         /* Byte o
1cad7 66 66 73 65 74 20 6f 66 20 62 6c 6f 62 20 69 6e  ffset of blob in
1cad8 20 63 75 72 73 6f 72 20 64 61 74 61 20 2a 2f 0a   cursor data */.
1cad9 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 73 72    BtCursor *pCsr
1cada 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  ;         /* Cur
1cadb 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  sor pointing at 
1cadc 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20 20 73 71  blob row */.  sq
1cadd 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1cade 74 3b 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65  t;    /* Stateme
1cadf 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75 72 73 6f  nt holding curso
1cae0 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69  r open */.  sqli
1cae1 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
1cae2 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63      /* The assoc
1cae3 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 2a  iated database *
1cae4 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  /.};../*.** Open
1cae5 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a   a blob handle..
1cae6 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1cae7 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f  t sqlite3_blob_o
1cae8 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  pen(.  sqlite3* 
1cae9 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  db,            /
1caea 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
1caeb 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63  onnection */.  c
1caec 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20  onst char *zDb, 
1caed 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 74         /* The at
1caee 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20  tached database 
1caef 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62  containing the b
1caf0 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  lob */.  const c
1caf1 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20 20 20 20  har *zTable,    
1caf2 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 63 6f   /* The table co
1caf3 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f  ntaining the blo
1caf4 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  b */.  const cha
1caf5 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f  r *zColumn,    /
1caf6 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e  * The column con
1caf7 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62  taining the blob
1caf8 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74   */.  sqlite_int
1caf9 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 2f 2a  64 iRow,      /*
1cafa 20 54 68 65 20 72 6f 77 20 63 6f 6e 74 61 69 6e   The row contain
1cafb 69 6e 67 20 74 68 65 20 67 6c 6f 62 20 2a 2f 0a  ing the glob */.
1cafc 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
1cafd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1cafe 65 20 2d 3e 20 72 65 61 64 2f 77 72 69 74 65 20  e -> read/write 
1caff 61 63 63 65 73 73 2c 20 66 61 6c 73 65 20 2d 3e  access, false ->
1cb00 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20   read-only */.  
1cb01 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70  sqlite3_blob **p
1cb02 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61 6e 64 6c  pBlob   /* Handl
1cb03 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  e for accessing 
1cb04 74 68 65 20 62 6c 6f 62 20 72 65 74 75 72 6e 65  the blob returne
1cb05 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  d here */.){.  i
1cb06 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20 30 3b  nt nAttempt = 0;
1cb07 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20  .  int iCol;    
1cb08 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1cb09 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e 20 69  dex of zColumn i
1cb0a 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a 2f 0a  n row-record */.
1cb0b 0a 20 20 2f 2a 20 54 68 69 73 20 56 44 42 45 20  .  /* This VDBE 
1cb0c 70 72 6f 67 72 61 6d 20 73 65 65 6b 73 20 61 20  program seeks a 
1cb0d 62 74 72 65 65 20 63 75 72 73 6f 72 20 74 6f 20  btree cursor to 
1cb0e 74 68 65 20 69 64 65 6e 74 69 66 69 65 64 20 0a  the identified .
1cb0f 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65 2f 72 6f    ** db/table/ro
1cb10 77 20 65 6e 74 72 79 2e 20 54 68 65 20 72 65 61  w entry. The rea
1cb11 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 61 20  son for using a 
1cb12 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 6e 73  vdbe program ins
1cb13 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 77 72 69  tead.  ** of wri
1cb14 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75 73 65  ting code to use
1cb15 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65   the b-tree laye
1cb16 72 20 64 69 72 65 63 74 6c 79 20 69 73 20 74 68  r directly is th
1cb17 61 74 20 74 68 65 0a 20 20 2a 2a 20 76 64 62 65  at the.  ** vdbe
1cb18 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 74 61   program will ta
1cb19 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20  ke advantage of 
1cb1a 74 68 65 20 76 61 72 69 6f 75 73 20 74 72 61 6e  the various tran
1cb1b 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f  saction,.  ** lo
1cb1c 63 6b 69 6e 67 20 61 6e 64 20 65 72 72 6f 72 20  cking and error 
1cb1d 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72 61 73 74  handling infrast
1cb1e 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 69 6e  ructure built in
1cb1f 74 6f 20 74 68 65 20 76 64 62 65 2e 0a 20 20 2a  to the vdbe..  *
1cb20 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20 73 65 65  *.  ** After see
1cb21 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 2c  king the cursor,
1cb22 20 74 68 65 20 76 64 62 65 20 65 78 65 63 75 74   the vdbe execut
1cb23 65 73 20 61 6e 20 4f 50 5f 52 65 73 75 6c 74 52  es an OP_ResultR
1cb24 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 65 20 65 78  ow..  ** Code ex
1cb25 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20 56 64  ternal to the Vd
1cb26 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f 77 73  be then "borrows
1cb27 22 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72  " the b-tree cur
1cb28 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75 73 65  sor and.  ** use
1cb29 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  s it to implemen
1cb2a 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61 64 28  t the blob_read(
1cb2b 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28 29 20  ), blob_write() 
1cb2c 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62 5f 62  and .  ** blob_b
1cb2d 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f 6e 73  ytes() functions
1cb2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
1cb2f 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
1cb30 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 69  se() function fi
1cb31 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64 62 65  nalizes the vdbe
1cb32 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a 20 77   program,.  ** w
1cb33 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68 65 20  hich closes the 
1cb34 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 6e  b-tree cursor an
1cb35 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63 6f 6d  d (possibly) com
1cb36 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a 20 74  mits the .  ** t
1cb37 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f  ransaction..  */
1cb38 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
1cb39 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65 6e 42  VdbeOpList openB
1cb3a 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 4f  lob[] = {.    {O
1cb3b 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 30  P_Transaction, 0
1cb3c 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f 2a 20  , 0, 0},     /* 
1cb3d 30 3a 20 53 74 61 72 74 20 61 20 74 72 61 6e 73  0: Start a trans
1cb3e 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 7b 4f  action */.    {O
1cb3f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c 20  P_VerifyCookie, 
1cb40 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f 2a 20  0, 0, 0},    /* 
1cb41 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73 63 68  1: Check the sch
1cb42 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 0a 20  ema cookie */.. 
1cb43 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65     /* One of the
1cb44 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69   following two i
1cb45 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 73 20 72  nstructions is r
1cb46 65 70 6c 61 63 65 64 20 62 79 20 61 6e 0a 20 20  eplaced by an.  
1cb47 20 20 2a 2a 20 4f 50 5f 4e 6f 6f 70 20 62 65 66    ** OP_Noop bef
1cb48 6f 72 65 20 65 78 65 63 74 69 6f 6e 2e 0a 20 20  ore exection..  
1cb49 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 53 65 74    */.    {OP_Set
1cb4a 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 30  NumColumns, 0, 0
1cb4b 2c 20 30 7d 2c 20 20 20 2f 2a 20 32 3a 20 4e 75  , 0},   /* 2: Nu
1cb4c 6d 20 63 6f 6c 73 20 66 6f 72 20 63 75 72 73 6f  m cols for curso
1cb4d 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65  r */.    {OP_Ope
1cb4e 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30 7d 2c  nRead, 0, 0, 0},
1cb4f 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 4f 70          /* 3: Op
1cb50 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20  en cursor 0 for 
1cb51 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20 20 7b  reading */.    {
1cb52 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
1cb53 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 2f 2a  , 0, 0, 0},   /*
1cb54 20 34 3a 20 4e 75 6d 20 63 6f 6c 73 20 66 6f 72   4: Num cols for
1cb55 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b   cursor */.    {
1cb56 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c  OP_OpenWrite, 0,
1cb57 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a   0, 0},       /*
1cb58 20 35 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20   5: Open cursor 
1cb59 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65  0 for read/write
1cb5a 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56 61 72   */..    {OP_Var
1cb5b 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 30 7d 2c  iable, 1, 1, 0},
1cb5c 20 20 20 20 20 20 20 20 2f 2a 20 36 3a 20 50 75          /* 6: Pu
1cb5d 73 68 20 74 68 65 20 72 6f 77 69 64 20 74 6f 20  sh the rowid to 
1cb5e 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20  the stack */.   
1cb5f 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20   {OP_NotExists, 
1cb60 30 2c 20 31 30 2c 20 31 7d 2c 20 20 20 20 20 20  0, 10, 1},      
1cb61 2f 2a 20 37 3a 20 53 65 65 6b 20 74 68 65 20 63  /* 7: Seek the c
1cb62 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50  ursor */.    {OP
1cb63 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 31  _Column, 0, 0, 1
1cb64 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 38  },          /* 8
1cb65 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52 65 73    */.    {OP_Res
1cb66 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20 30 7d  ultRow, 1, 0, 0}
1cb67 2c 20 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f  ,       /* 9  */
1cb68 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 2c 20  .    {OP_Close, 
1cb69 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20  0, 0, 0},       
1cb6a 20 20 20 20 2f 2a 20 31 30 20 20 2a 2f 0a 20 20      /* 10  */.  
1cb6b 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30    {OP_Halt, 0, 0
1cb6c 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20  , 0},           
1cb6d 20 2f 2a 20 31 31 20 2a 2f 0a 20 20 7d 3b 0a 0a   /* 11 */.  };..
1cb6e 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20    Vdbe *v = 0;. 
1cb6f 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1cb70 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 7a 45 72 72  _OK;.  char zErr
1cb71 5b 31 32 38 5d 3b 0a 0a 20 20 7a 45 72 72 5b 30  [128];..  zErr[0
1cb72 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  ] = 0;.  sqlite3
1cb73 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
1cb74 3e 6d 75 74 65 78 29 3b 0a 20 20 64 6f 20 7b 0a  >mutex);.  do {.
1cb75 20 20 20 20 50 61 72 73 65 20 73 50 61 72 73 65      Parse sParse
1cb76 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ;.    Table *pTa
1cb77 62 3b 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  b;..    memset(&
1cb78 73 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f  sParse, 0, sizeo
1cb79 66 28 50 61 72 73 65 29 29 3b 0a 20 20 20 20 73  f(Parse));.    s
1cb7a 50 61 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 0a  Parse.db = db;..
1cb7b 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1cb7c 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20  afetyOn(db) ){. 
1cb7d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
1cb7e 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
1cb7f 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ex);.      retur
1cb80 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
1cb81 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
1cb82 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
1cb83 28 64 62 29 3b 0a 20 20 20 20 70 54 61 62 20 3d  (db);.    pTab =
1cb84 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
1cb85 62 6c 65 28 26 73 50 61 72 73 65 2c 20 30 2c 20  ble(&sParse, 0, 
1cb86 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20 20  zTable, zDb);.  
1cb87 20 20 69 66 28 20 70 54 61 62 20 26 26 20 49 73    if( pTab && Is
1cb88 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
1cb89 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b  .      pTab = 0;
1cb8a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1cb8b 72 6f 72 4d 73 67 28 26 73 50 61 72 73 65 2c 20  rorMsg(&sParse, 
1cb8c 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 72  "cannot open vir
1cb8d 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 2c  tual table: %s",
1cb8e 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a   zTable);.    }.
1cb8f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1cb90 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69 66 28  MIT_VIEW.    if(
1cb91 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70   pTab && pTab->p
1cb92 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
1cb93 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20  pTab = 0;.      
1cb94 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1cb95 26 73 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  &sParse, "cannot
1cb96 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 2c   open view: %s",
1cb97 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a   zTable);.    }.
1cb98 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21  #endif.    if( !
1cb99 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 66  pTab ){.      if
1cb9a 28 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67  ( sParse.zErrMsg
1cb9b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1cb9c 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
1cb9d 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
1cb9e 20 22 25 73 22 2c 20 73 50 61 72 73 65 2e 7a 45   "%s", sParse.zE
1cb9f 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a  rrMsg);.      }.
1cba0 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1cba1 72 65 65 28 64 62 2c 20 73 50 61 72 73 65 2e 7a  ree(db, sParse.z
1cba2 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 72  ErrMsg);.      r
1cba3 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1cba4 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71  ;.      (void)sq
1cba5 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1cba6 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
1cba7 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
1cba8 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  b);.      goto b
1cba9 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20  lob_open_out;.  
1cbaa 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20    }..    /* Now 
1cbab 73 65 61 72 63 68 20 70 54 61 62 20 66 6f 72 20  search pTab for 
1cbac 74 68 65 20 65 78 61 63 74 20 63 6f 6c 75 6d 6e  the exact column
1cbad 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 6f  . */.    for(iCo
1cbae 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61 62  l=0; iCol < pTab
1cbaf 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20  ->nCol; iCol++) 
1cbb0 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
1cbb1 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d  te3StrICmp(pTab-
1cbb2 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d  >aCol[iCol].zNam
1cbb3 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29  e, zColumn)==0 )
1cbb4 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
1cbb5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1cbb6 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61     if( iCol==pTa
1cbb7 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
1cbb8 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1cbb9 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20  f(sizeof(zErr), 
1cbba 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 63  zErr, "no such c
1cbbb 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20  olumn: \"%s\"", 
1cbbc 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20  zColumn);.      
1cbbd 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1cbbe 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  R;.      (void)s
1cbbf 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1cbc0 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  db);.      sqlit
1cbc1 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28  e3BtreeLeaveAll(
1cbc2 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  db);.      goto 
1cbc3 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  blob_open_out;. 
1cbc4 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
1cbc5 74 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69  the value is bei
1cbc6 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72  ng opened for wr
1cbc7 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61  iting, check tha
1cbc8 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c  t the.    ** col
1cbc9 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78  umn is not index
1cbca 65 64 2e 20 49 74 20 69 73 20 61 67 61 69 6e 73  ed. It is agains
1cbcb 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f 20 6f  t the rules to o
1cbcc 70 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 69 6e  pen an.    ** in
1cbcd 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66 6f 72  dexed column for
1cbce 20 77 72 69 74 69 6e 67 2e 0a 20 20 20 20 2a 2f   writing..    */
1cbcf 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 29  .    if( flags )
1cbd0 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70  {.      Index *p
1cbd1 49 64 78 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  Idx;.      for(p
1cbd2 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
1cbd3 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
1cbd4 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
1cbd5 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
1cbd6 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64    for(j=0; j<pId
1cbd7 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29  x->nColumn; j++)
1cbd8 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
1cbd9 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a  pIdx->aiColumn[j
1cbda 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20  ]==iCol ){.     
1cbdb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
1cbdc 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
1cbdd 45 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20  Err), zErr,.    
1cbde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cbdf 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74           "cannot
1cbe0 20 6f 70 65 6e 20 69 6e 64 65 78 65 64 20 63 6f   open indexed co
1cbe1 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67  lumn for writing
1cbe2 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ");.            
1cbe3 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1cbe4 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 28  R;.            (
1cbe5 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
1cbe6 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
1cbe7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
1cbe8 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
1cbe9 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
1cbea 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b  o blob_open_out;
1cbeb 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1cbec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1cbed 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73 71     }..    v = sq
1cbee 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28  lite3VdbeCreate(
1cbef 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20 29  db);.    if( v )
1cbf0 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20  {.      int iDb 
1cbf1 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
1cbf2 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
1cbf3 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20  >pSchema);.     
1cbf4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1cbf5 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66 28  pList(v, sizeof(
1cbf6 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f 66  openBlob)/sizeof
1cbf7 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f 70  (VdbeOpList), op
1cbf8 65 6e 42 6c 6f 62 29 3b 0a 0a 20 20 20 20 20 20  enBlob);..      
1cbf9 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  /* Configure the
1cbfa 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20   OP_Transaction 
1cbfb 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1cbfc 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
1cbfd 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  0, iDb);.      s
1cbfe 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1cbff 50 32 28 76 2c 20 30 2c 20 28 66 6c 61 67 73 20  P2(v, 0, (flags 
1cc00 3f 20 31 20 3a 20 30 29 29 3b 0a 0a 20 20 20 20  ? 1 : 0));..    
1cc01 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74    /* Configure t
1cc02 68 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b  he OP_VerifyCook
1cc03 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ie */.      sqli
1cc04 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
1cc05 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20  v, 1, iDb);.    
1cc06 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1cc07 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 54 61 62  ngeP2(v, 1, pTab
1cc08 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d  ->pSchema->schem
1cc09 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20  a_cookie);..    
1cc0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61    /* Make sure a
1cc0b 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f   mutex is held o
1cc0c 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  n the table to b
1cc0d 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20  e accessed */.  
1cc0e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55      sqlite3VdbeU
1cc0f 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29  sesBtree(v, iDb)
1cc10 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d  ; ..      /* Rem
1cc11 6f 76 65 20 65 69 74 68 65 72 20 74 68 65 20 4f  ove either the O
1cc12 50 5f 4f 70 65 6e 57 72 69 74 65 20 6f 72 20 4f  P_OpenWrite or O
1cc13 70 65 6e 52 65 61 64 2e 20 53 65 74 20 74 68 65  penRead. Set the
1cc14 20 50 32 20 0a 20 20 20 20 20 20 2a 2a 20 70 61   P2 .      ** pa
1cc15 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 6f  rameter of the o
1cc16 74 68 65 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e  ther to pTab->tn
1cc17 75 6d 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20  um. .      */.  
1cc18 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1cc19 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 28  hangeToNoop(v, (
1cc1a 66 6c 61 67 73 20 3f 20 33 20 3a 20 35 29 2c 20  flags ? 3 : 5), 
1cc1b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
1cc1c 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
1cc1d 20 28 66 6c 61 67 73 20 3f 20 35 20 3a 20 33 29   (flags ? 5 : 3)
1cc1e 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20  , pTab->tnum);. 
1cc1f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cc20 43 68 61 6e 67 65 50 33 28 76 2c 20 28 66 6c 61  ChangeP3(v, (fla
1cc21 67 73 20 3f 20 35 20 3a 20 33 29 2c 20 69 44 62  gs ? 5 : 3), iDb
1cc22 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e  );..      /* Con
1cc23 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 53 65  figure the OP_Se
1cc24 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2e 20 43 6f 6e  tNumColumns. Con
1cc25 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73 6f  figure the curso
1cc26 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  r to.      ** th
1cc27 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61 62  ink that the tab
1cc28 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 20  le has one more 
1cc29 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 72  column than it r
1cc2a 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 64  eally.      ** d
1cc2b 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 6d  oes. An OP_Colum
1cc2c 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  n to retrieve th
1cc2d 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f 6c  is imaginary col
1cc2e 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a  umn will.      *
1cc2f 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20  * always return 
1cc30 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 69  an SQL NULL. Thi
1cc31 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63 61  s is useful beca
1cc32 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 20  use it means.   
1cc33 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e 76     ** we can inv
1cc34 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f  oke OP_Column to
1cc35 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 62   fill in the vdb
1cc36 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20 0a  e cursors type .
1cc37 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 66        ** and off
1cc38 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f 75  set cache withou
1cc39 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49 4f  t causing any IO
1cc3a 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1cc3b 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1cc3c 67 65 50 32 28 76 2c 20 66 6c 61 67 73 20 3f 20  geP2(v, flags ? 
1cc3d 34 20 3a 20 32 2c 20 70 54 61 62 2d 3e 6e 43 6f  4 : 2, pTab->nCo
1cc3e 6c 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  l+1);.      sqli
1cc3f 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1cc40 76 2c 20 38 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  v, 8, pTab->nCol
1cc41 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 64 62  );.      if( !db
1cc42 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1cc43 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1cc44 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 76  3VdbeMakeReady(v
1cc45 2c 20 31 2c 20 31 2c 20 31 2c 20 30 29 3b 0a 20  , 1, 1, 1, 0);. 
1cc46 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1cc47 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1cc48 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20  eLeaveAll(db);. 
1cc49 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
1cc4a 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
1cc4b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1cc4c 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  _OK || db->mallo
1cc4d 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
1cc4e 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f   goto blob_open_
1cc4f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  out;.    }..    
1cc50 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
1cc51 36 34 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  64((sqlite3_stmt
1cc52 20 2a 29 76 2c 20 31 2c 20 69 52 6f 77 29 3b 0a   *)v, 1, iRow);.
1cc53 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1cc54 5f 73 74 65 70 28 28 73 71 6c 69 74 65 33 5f 73  _step((sqlite3_s
1cc55 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 69 66  tmt *)v);.    if
1cc56 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
1cc57 20 29 7b 0a 20 20 20 20 20 20 6e 41 74 74 65 6d   ){.      nAttem
1cc58 70 74 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d  pt++;.      rc =
1cc59 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
1cc5a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  e((sqlite3_stmt 
1cc5b 2a 29 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  *)v);.      sqli
1cc5c 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
1cc5d 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
1cc5e 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
1cc5f 64 62 29 29 3b 0a 20 20 20 20 20 20 76 20 3d 20  db));.      v = 
1cc60 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68 69  0;.    }.  } whi
1cc61 6c 65 28 20 6e 41 74 74 65 6d 70 74 3c 35 20 26  le( nAttempt<5 &
1cc62 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48  & rc==SQLITE_SCH
1cc63 45 4d 41 20 29 3b 0a 0a 20 20 69 66 28 20 72 63  EMA );..  if( rc
1cc64 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
1cc65 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72      /* The row-r
1cc66 65 63 6f 72 64 20 68 61 73 20 62 65 65 6e 20 6f  ecord has been o
1cc67 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c  pened successful
1cc68 6c 79 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74  ly. Check that t
1cc69 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e  he.    ** column
1cc6a 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e   in question con
1cc6b 74 61 69 6e 73 20 74 65 78 74 20 6f 72 20 61 20  tains text or a 
1cc6c 62 6c 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e 74  blob. If it cont
1cc6d 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78 74  ains.    ** text
1cc6e 2c 20 69 74 20 69 73 20 75 70 20 74 6f 20 74 68  , it is up to th
1cc6f 65 20 63 61 6c 6c 65 72 20 74 6f 20 67 65 74 20  e caller to get 
1cc70 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 72 69 67  the encoding rig
1cc71 68 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49  ht..    */.    I
1cc72 6e 63 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a  ncrblob *pBlob;.
1cc73 20 20 20 20 75 33 32 20 74 79 70 65 20 3d 20 76      u32 type = v
1cc74 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70  ->apCsr[0]->aTyp
1cc75 65 5b 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66  e[iCol];..    if
1cc76 28 20 74 79 70 65 3c 31 32 20 29 7b 0a 20 20 20  ( type<12 ){.   
1cc77 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1cc78 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
1cc79 2c 20 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20  , zErr, "cannot 
1cc7a 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 74 79  open value of ty
1cc7b 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20  pe %s",.        
1cc7c 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22    type==0?"null"
1cc7d 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c 22  : type==7?"real"
1cc7e 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 20 20  : "integer".    
1cc7f 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20    );.      rc = 
1cc80 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1cc81 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
1cc82 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  en_out;.    }.  
1cc83 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72 62    pBlob = (Incrb
1cc84 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 62 4d  lob *)sqlite3DbM
1cc85 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
1cc86 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29 3b  zeof(Incrblob));
1cc87 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
1cc88 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1cc89 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1cc8a 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20  (db, pBlob);.   
1cc8b 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65     goto blob_ope
1cc8c 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  n_out;.    }.   
1cc8d 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d 20   pBlob->flags = 
1cc8e 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c 6f 62  flags;.    pBlob
1cc8f 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 70 43  ->pCsr =  v->apC
1cc90 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a  sr[0]->pCursor;.
1cc91 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1cc92 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42 6c 6f  EnterCursor(pBlo
1cc93 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71  b->pCsr);.    sq
1cc94 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65 4f  lite3BtreeCacheO
1cc95 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70  verflow(pBlob->p
1cc96 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Csr);.    sqlite
1cc97 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f  3BtreeLeaveCurso
1cc98 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a  r(pBlob->pCsr);.
1cc99 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74      pBlob->pStmt
1cc9a 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74   = (sqlite3_stmt
1cc9b 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f 62 2d   *)v;.    pBlob-
1cc9c 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e 61 70  >iOffset = v->ap
1cc9d 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 74 5b  Csr[0]->aOffset[
1cc9e 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f 62  iCol];.    pBlob
1cc9f 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65  ->nByte = sqlite
1cca0 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
1cca1 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 70 42  en(type);.    pB
1cca2 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20  lob->db = db;.  
1cca3 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 71 6c    *ppBlob = (sql
1cca4 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f  ite3_blob *)pBlo
1cca5 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  b;.    rc = SQLI
1cca6 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 69  TE_OK;.  }else i
1cca7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1cca8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1cca9 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
1ccaa 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 6e 6f  zErr), zErr, "no
1ccab 20 73 75 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c   such rowid: %ll
1ccac 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72  d", iRow);.    r
1ccad 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1ccae 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e  ;.  }..blob_open
1ccaf 5f 6f 75 74 3a 0a 20 20 7a 45 72 72 5b 73 69 7a  _out:.  zErr[siz
1ccb0 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d 20 27  eof(zErr)-1] = '
1ccb1 5c 30 27 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  \0';.  if( rc!=S
1ccb2 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e  QLITE_OK || db->
1ccb3 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1ccb4 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
1ccb5 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74  lize((sqlite3_st
1ccb6 6d 74 20 2a 29 76 29 3b 0a 20 20 7d 0a 20 20 73  mt *)v);.  }.  s
1ccb7 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
1ccb8 72 63 2c 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  rc, (rc==SQLITE_
1ccb9 4f 4b 3f 30 3a 7a 45 72 72 29 29 3b 0a 20 20 72  OK?0:zErr));.  r
1ccba 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
1ccbb 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
1ccbc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1ccbd 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1ccbe 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1ccbf 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c 6f 62  .** Close a blob
1ccc0 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73   handle that was
1ccc1 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 65 61   previously crea
1ccc2 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c  ted using.** sql
1ccc3 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29  ite3_blob_open()
1ccc4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1ccc5 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1ccc6 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62  _close(sqlite3_b
1ccc7 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49  lob *pBlob){.  I
1ccc8 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e  ncrblob *p = (In
1ccc9 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a  crblob *)pBlob;.
1ccca 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20    int rc;..  rc 
1cccb 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  = sqlite3_finali
1cccc 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  ze(p->pStmt);.  
1cccd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d  sqlite3DbFree(p-
1ccce 3e 64 62 2c 20 70 29 3b 0a 20 20 72 65 74 75 72  >db, p);.  retur
1cccf 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  n rc;.}../*.** P
1ccd0 65 72 66 6f 72 6d 20 61 20 72 65 61 64 20 6f 72  erform a read or
1ccd1 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e   write operation
1ccd2 20 6f 6e 20 61 20 62 6c 6f 62 0a 2a 2f 0a 73 74   on a blob.*/.st
1ccd3 61 74 69 63 20 69 6e 74 20 62 6c 6f 62 52 65 61  atic int blobRea
1ccd4 64 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65  dWrite(.  sqlite
1ccd5 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 0a  3_blob *pBlob, .
1ccd6 20 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e    void *z, .  in
1ccd7 74 20 6e 2c 20 0a 20 20 69 6e 74 20 69 4f 66 66  t n, .  int iOff
1ccd8 73 65 74 2c 20 0a 20 20 69 6e 74 20 28 2a 78 43  set, .  int (*xC
1ccd9 61 6c 6c 29 28 42 74 43 75 72 73 6f 72 2a 2c 20  all)(BtCursor*, 
1ccda 75 33 32 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29  u32, u32, void*)
1ccdb 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
1ccdc 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49  Incrblob *p = (I
1ccdd 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b  ncrblob *)pBlob;
1ccde 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71  .  Vdbe *v;.  sq
1ccdf 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
1cce0 62 3b 20 20 0a 0a 20 20 73 71 6c 69 74 65 33 5f  b;  ..  sqlite3_
1cce1 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
1cce2 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d 20 28 56  mutex);.  v = (V
1cce3 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a  dbe*)p->pStmt;..
1cce4 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66    if( n<0 || iOf
1cce5 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73  fset<0 || (iOffs
1cce6 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29  et+n)>p->nByte )
1cce7 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 65 73 74  {.    /* Request
1cce8 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
1cce9 2e 20 52 65 74 75 72 6e 20 61 20 74 72 61 6e 73  . Return a trans
1ccea 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20  ient error. */. 
1cceb 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1ccec 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65  RROR;.    sqlite
1cced 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
1ccee 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d  E_ERROR, 0);.  }
1ccef 20 65 6c 73 65 20 69 66 28 20 76 3d 3d 30 20 29   else if( v==0 )
1ccf0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  {.    /* If ther
1ccf1 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e  e is no statemen
1ccf2 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74  t handle, then t
1ccf3 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68  he blob-handle h
1ccf4 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64  as.    ** alread
1ccf5 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74  y been invalidat
1ccf6 65 64 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54  ed. Return SQLIT
1ccf7 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20  E_ABORT in this 
1ccf8 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  case..    */.   
1ccf9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f   rc = SQLITE_ABO
1ccfa 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  RT;.  }else{.   
1ccfb 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65 72 20   /* Call either 
1ccfc 42 74 72 65 65 44 61 74 61 28 29 20 6f 72 20 42  BtreeData() or B
1ccfd 74 72 65 65 50 75 74 44 61 74 61 28 29 2e 20 49  treePutData(). I
1ccfe 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69  f SQLITE_ABORT i
1ccff 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65  s.    ** returne
1cd00 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68 65 20  d, clean-up the 
1cd01 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
1cd02 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
1cd03 65 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62  ert( db == v->db
1cd04 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   );.    sqlite3B
1cd05 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28  treeEnterCursor(
1cd06 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 72 63  p->pCsr);.    rc
1cd07 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72   = xCall(p->pCsr
1cd08 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66  , iOffset+p->iOf
1cd09 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20  fset, n, z);.   
1cd0a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1cd0b 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72  veCursor(p->pCsr
1cd0c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
1cd0d 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20  QLITE_ABORT ){. 
1cd0e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cd0f 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20  Finalize(v);.   
1cd10 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b     p->pStmt = 0;
1cd11 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1cd12 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20    db->errCode = 
1cd13 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72 63 20  rc;.      v->rc 
1cd14 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  = rc;.    }.  }.
1cd15 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
1cd16 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
1cd17 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1cd18 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
1cd19 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1cd1a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
1cd1b 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e   from a blob han
1cd1c 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
1cd1d 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1cd1e 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33  lob_read(sqlite3
1cd1f 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f  _blob *pBlob, vo
1cd20 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e  id *z, int n, in
1cd21 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65  t iOffset){.  re
1cd22 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69  turn blobReadWri
1cd23 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20  te(pBlob, z, n, 
1cd24 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33  iOffset, sqlite3
1cd25 42 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f  BtreeData);.}../
1cd26 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  *.** Write data 
1cd27 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  to a blob handle
1cd28 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1cd29 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1cd2a 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62  _write(sqlite3_b
1cd2b 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73  lob *pBlob, cons
1cd2c 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e  t void *z, int n
1cd2d 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a  , int iOffset){.
1cd2e 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61    return blobRea
1cd2f 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 28 76  dWrite(pBlob, (v
1cd30 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66  oid *)z, n, iOff
1cd31 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65  set, sqlite3Btre
1cd32 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a  ePutData);.}../*
1cd33 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 6c 6f 62  .** Query a blob
1cd34 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20   handle for the 
1cd35 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
1cd36 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72  ..**.** The Incr
1cd37 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65 6c 64  blob.nByte field
1cd38 20 69 73 20 66 69 78 65 64 20 66 6f 72 20 74 68   is fixed for th
1cd39 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68  e lifetime of th
1cd3a 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f  e Incrblob.** so
1cd3b 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 72 65 71   no mutex is req
1cd3c 75 69 72 65 64 20 66 6f 72 20 61 63 63 65 73 73  uired for access
1cd3d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1cd3e 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1cd3f 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62  _bytes(sqlite3_b
1cd40 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49  lob *pBlob){.  I
1cd41 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e  ncrblob *p = (In
1cd42 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a  crblob *)pBlob;.
1cd43 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 42 79 74    return p->nByt
1cd44 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  e;.}..#endif /* 
1cd45 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1cd46 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a  MIT_INCRBLOB */.
1cd47 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1cd48 20 45 6e 64 20 6f 66 20 76 64 62 65 62 6c 6f 62   End of vdbeblob
1cd49 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1cd4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cd4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1cd4c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1cd4d 20 42 65 67 69 6e 20 66 69 6c 65 20 6a 6f 75 72   Begin file jour
1cd4e 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nal.c **********
1cd4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1cd51 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
1cd52 73 74 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 22.**.** The 
1cd53 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1cd54 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1cd55 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1cd56 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1cd57 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1cd58 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1cd59 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1cd5a 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1cd5b 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1cd5c 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1cd5d 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1cd5e 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1cd5f 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1cd60 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1cd61 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1cd62 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1cd63 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1cd64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cd65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cd66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cd67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cd68 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20  ****.**.** @(#) 
1cd69 24 49 64 3a 20 6a 6f 75 72 6e 61 6c 2e 63 2c 76  $Id: journal.c,v
1cd6a 20 31 2e 38 20 32 30 30 38 2f 30 35 2f 30 31 20   1.8 2008/05/01 
1cd6b 31 38 3a 30 31 3a 34 37 20 64 72 68 20 45 78 70  18:01:47 drh Exp
1cd6c 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51   $.*/..#ifdef SQ
1cd6d 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d  LITE_ENABLE_ATOM
1cd6e 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a 20  IC_WRITE../*.** 
1cd6f 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  This file implem
1cd70 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c 20 6b  ents a special k
1cd71 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 66  ind of sqlite3_f
1cd72 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 64 0a  ile object used.
1cd73 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20  ** by SQLite to 
1cd74 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66  create journal f
1cd75 69 6c 65 73 20 69 66 20 74 68 65 20 61 74 6f 6d  iles if the atom
1cd76 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a  ic-write optimiz
1cd77 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62  ation.** is enab
1cd78 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  led..**.** The d
1cd79 69 73 74 69 6e 63 74 69 76 65 20 63 68 61 72 61  istinctive chara
1cd7a 63 74 65 72 69 73 74 69 63 20 6f 66 20 74 68 69  cteristic of thi
1cd7b 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 69  s sqlite3_file i
1cd7c 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 63  s that the.** ac
1cd7d 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c  tual on disk fil
1cd7e 65 20 69 73 20 63 72 65 61 74 65 64 20 6c 61 7a  e is created laz
1cd7f 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 66 69  ily. When the fi
1cd80 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 0a 2a  le is created,.*
1cd81 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73 70 65  * the caller spe
1cd82 63 69 66 69 65 73 20 61 20 62 75 66 66 65 72 20  cifies a buffer 
1cd83 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d  size for an in-m
1cd84 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f 0a  emory buffer to.
1cd85 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  ** be used to se
1cd86 72 76 69 63 65 20 72 65 61 64 28 29 20 61 6e 64  rvice read() and
1cd87 20 77 72 69 74 65 28 29 20 72 65 71 75 65 73 74   write() request
1cd88 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20 66 69  s. The actual fi
1cd89 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73  le.** on disk is
1cd8a 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f 72 20   not created or 
1cd8b 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 6c 20  populated until 
1cd8c 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20  either:.**.**   
1cd8d 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  1) The in-memory
1cd8e 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1cd8f 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 65 20  grows too large 
1cd90 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 74 65  for the allocate
1cd91 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 66 65  d .**      buffe
1cd92 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68  r, or.**   2) Th
1cd93 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64  e xSync() method
1cd94 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a   is called..*/..
1cd95 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61  ../*.** A Journa
1cd96 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 20  lFile object is 
1cd97 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71  a subclass of sq
1cd98 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65 64 20  lite3_file used 
1cd99 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e  by.** as an open
1cd9a 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72   file handle for
1cd9b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a   journal files..
1cd9c 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e 61  */.struct Journa
1cd9d 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65  lFile {.  sqlite
1cd9e 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
1cd9f 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f  ethod;    /* I/O
1cda0 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72   methods on jour
1cda1 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69  nal files */.  i
1cda2 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20 20 20  nt nBuf;        
1cda3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1cda4 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d  * Size of zBuf[]
1cda5 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63   in bytes */.  c
1cda6 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 20  har *zBuf;      
1cda7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1cda8 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66 66 65  * Space to buffe
1cda9 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 73  r journal writes
1cdaa 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b   */.  int iSize;
1cdab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cdac 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20        /* Amount 
1cdad 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e  of zBuf[] curren
1cdae 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e  tly used */.  in
1cdaf 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  t flags;        
1cdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1cdb1 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a   xOpen flags */.
1cdb2 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
1cdb3 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  Vfs;            
1cdb4 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20    /* The "real" 
1cdb5 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 20 2a  underlying VFS *
1cdb6 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
1cdb7 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20   *pReal;        
1cdb8 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c      /* The "real
1cdb9 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c  " underlying fil
1cdba 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a  e descriptor */.
1cdbb 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a    const char *zJ
1cdbc 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20  ournal;         
1cdbd 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1cdbe 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
1cdbf 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  .};.typedef stru
1cdc0 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a  ct JournalFile J
1cdc1 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a  ournalFile;../*.
1cdc2 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f  ** If it does no
1cdc3 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  t already exists
1cdc4 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70 6f 70  , create and pop
1cdc5 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 73  ulate the on-dis
1cdc6 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a  k file .** for J
1cdc7 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f  ournalFile p..*/
1cdc8 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 65 61  .static int crea
1cdc9 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69  teFile(JournalFi
1cdca 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  le *p){.  int rc
1cdcb 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1cdcc 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b  if( !p->pReal ){
1cdcd 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  .    sqlite3_fil
1cdce 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c 69  e *pReal = (sqli
1cdcf 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d  te3_file *)&p[1]
1cdd0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1cdd1 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73  e3OsOpen(p->pVfs
1cdd2 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70  , p->zJournal, p
1cdd3 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20  Real, p->flags, 
1cdd4 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  0);.    if( rc==
1cdd5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1cdd6 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52     p->pReal = pR
1cdd7 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70  eal;.      if( p
1cdd8 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20  ->iSize>0 ){.   
1cdd9 20 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 69       assert(p->i
1cdda 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a  Size<=p->nBuf);.
1cddb 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1cddc 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70  ite3OsWrite(p->p
1cddd 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70  Real, p->zBuf, p
1cdde 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20  ->iSize, 0);.   
1cddf 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1cde0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1cde1 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66  *.** Close the f
1cde2 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
1cde3 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c  nt jrnlClose(sql
1cde4 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29  ite3_file *pJfd)
1cde5 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20  {.  JournalFile 
1cde6 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c  *p = (JournalFil
1cde7 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20  e *)pJfd;.  if( 
1cde8 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20  p->pReal ){.    
1cde9 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
1cdea 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20  ->pReal);.  }.  
1cdeb 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
1cdec 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20  zBuf);.  return 
1cded 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
1cdee 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72  .** Read data fr
1cdef 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  om the file..*/.
1cdf0 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52  static int jrnlR
1cdf1 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66  ead(.  sqlite3_f
1cdf2 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a  ile *pJfd,    /*
1cdf3 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
1cdf4 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20  e from which to 
1cdf5 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  read */.  void *
1cdf6 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20  zBuf,           
1cdf7 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 73 75   /* Put the resu
1cdf8 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  lts here */.  in
1cdf9 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20  t iAmt,         
1cdfa 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1cdfb 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20  f bytes to read 
1cdfc 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  */.  sqlite_int6
1cdfd 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42  4 iOfst     /* B
1cdfe 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20  egin reading at 
1cdff 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29  this offset */.)
1ce00 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
1ce01 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61  ITE_OK;.  Journa
1ce02 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72  lFile *p = (Jour
1ce03 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a  nalFile *)pJfd;.
1ce04 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29    if( p->pReal )
1ce05 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1ce06 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 61  e3OsRead(p->pRea
1ce07 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69  l, zBuf, iAmt, i
1ce08 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Ofst);.  }else{.
1ce09 20 20 20 20 61 73 73 65 72 74 28 20 69 41 6d 74      assert( iAmt
1ce0a 2b 69 4f 66 73 74 3c 3d 70 2d 3e 69 53 69 7a 65  +iOfst<=p->iSize
1ce0b 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a   );.    memcpy(z
1ce0c 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b 69 4f  Buf, &p->zBuf[iO
1ce0d 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20 20 7d  fst], iAmt);.  }
1ce0e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1ce0f 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74  ./*.** Write dat
1ce10 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a  a to the file..*
1ce11 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  /.static int jrn
1ce12 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65  lWrite(.  sqlite
1ce13 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20  3_file *pJfd,   
1ce14 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20   /* The journal 
1ce15 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  file into which 
1ce16 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 6f  to write */.  co
1ce17 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20  nst void *zBuf, 
1ce18 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 74       /* Take dat
1ce19 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  a to be written 
1ce1a 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 69  from here */.  i
1ce1b 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20  nt iAmt,        
1ce1c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1ce1d 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74  of bytes to writ
1ce1e 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  e */.  sqlite_in
1ce1f 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
1ce20 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61   Begin writing a
1ce21 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 6e  t this offset in
1ce22 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29  to the file */.)
1ce23 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
1ce24 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61  ITE_OK;.  Journa
1ce25 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72  lFile *p = (Jour
1ce26 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a  nalFile *)pJfd;.
1ce27 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20    if( !p->pReal 
1ce28 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 3e  && (iOfst+iAmt)>
1ce29 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20 20 72  p->nBuf ){.    r
1ce2a 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65 28 70  c = createFile(p
1ce2b 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
1ce2c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1ce2d 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29    if( p->pReal )
1ce2e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1ce2f 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70  ite3OsWrite(p->p
1ce30 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74  Real, zBuf, iAmt
1ce31 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20 7d 65  , iOfst);.    }e
1ce32 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  lse{.      memcp
1ce33 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 74  y(&p->zBuf[iOfst
1ce34 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29 3b 0a  ], zBuf, iAmt);.
1ce35 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53 69        if( p->iSi
1ce36 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74 29 20  ze<(iOfst+iAmt) 
1ce37 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 53  ){.        p->iS
1ce38 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69 41 6d  ize = (iOfst+iAm
1ce39 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
1ce3a 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
1ce3b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e  c;.}../*.** Trun
1ce3c 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a  cate the file..*
1ce3d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  /.static int jrn
1ce3e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65  lTruncate(sqlite
1ce3f 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71  3_file *pJfd, sq
1ce40 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29  lite_int64 size)
1ce41 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
1ce42 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61  ITE_OK;.  Journa
1ce43 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72  lFile *p = (Jour
1ce44 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a  nalFile *)pJfd;.
1ce45 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29    if( p->pReal )
1ce46 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1ce47 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 2d 3e  e3OsTruncate(p->
1ce48 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a 20 20  pReal, size);.  
1ce49 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 3c 70  }else if( size<p
1ce4a 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 70  ->iSize ){.    p
1ce4b 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a  ->iSize = size;.
1ce4c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1ce4d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74  .}../*.** Sync t
1ce4e 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
1ce4f 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e 63 28  ic int jrnlSync(
1ce50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1ce51 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  fd, int flags){.
1ce52 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f 75 72    int rc;.  Jour
1ce53 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1ce54 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1ce55 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c  ;.  if( p->pReal
1ce56 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1ce57 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e 70 52  ite3OsSync(p->pR
1ce58 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20 20 7d  eal, flags);.  }
1ce59 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53  else{.    rc = S
1ce5a 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
1ce5b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1ce5c 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69  .** Query the si
1ce5d 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
1ce5e 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  n bytes..*/.stat
1ce5f 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c 65 53  ic int jrnlFileS
1ce60 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ize(sqlite3_file
1ce61 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69   *pJfd, sqlite_i
1ce62 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20  nt64 *pSize){.  
1ce63 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1ce64 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c  OK;.  JournalFil
1ce65 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46  e *p = (JournalF
1ce66 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66  ile *)pJfd;.  if
1ce67 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20  ( p->pReal ){.  
1ce68 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1ce69 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52 65 61  FileSize(p->pRea
1ce6a 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d 65 6c  l, pSize);.  }el
1ce6b 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d  se{.    *pSize =
1ce6c 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 20   (sqlite_int64) 
1ce6d 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20  p->iSize;.  }.  
1ce6e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1ce6f 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74  .** Table of met
1ce70 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e 61 6c  hods for Journal
1ce71 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c  File sqlite3_fil
1ce72 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61  e object..*/.sta
1ce73 74 69 63 20 73 74 72 75 63 74 20 73 71 6c 69 74  tic struct sqlit
1ce74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a 6f  e3_io_methods Jo
1ce75 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73  urnalFileMethods
1ce76 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20   = {.  1,       
1ce77 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
1ce78 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f 73 65  n */.  jrnlClose
1ce79 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20  ,     /* xClose 
1ce7a 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c 20 20  */.  jrnlRead,  
1ce7b 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a      /* xRead */.
1ce7c 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20    jrnlWrite,    
1ce7d 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20   /* xWrite */.  
1ce7e 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20 2f  jrnlTruncate,  /
1ce7f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a 20  * xTruncate */. 
1ce80 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20   jrnlSync,      
1ce81 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6a 72  /* xSync */.  jr
1ce82 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a 20  nlFileSize,  /* 
1ce83 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 30  xFileSize */.  0
1ce84 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
1ce85 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20   xLock */.  0,  
1ce86 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55             /* xU
1ce87 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20  nlock */.  0,   
1ce88 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 68            /* xCh
1ce89 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
1ce8a 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
1ce8b 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74      /* xFileCont
1ce8c 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  rol */.  0,     
1ce8d 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74          /* xSect
1ce8e 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 20  orSize */.  0   
1ce8f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44             /* xD
1ce90 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
1ce91 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a  tics */.};../* .
1ce92 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61  ** Open a journa
1ce93 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  l file..*/.SQLIT
1ce94 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1ce95 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e  lite3JournalOpen
1ce96 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
1ce97 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f  *pVfs,         /
1ce98 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73 65  * The VFS to use
1ce99 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69 6c 65   for actual file
1ce9a 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   I/O */.  const 
1ce9b 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
1ce9c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1ce9d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1ce9e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
1ce9f 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20 20 20  le *pJfd,       
1cea0 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 64   /* Preallocated
1cea1 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68 61 6e  , blank file han
1cea2 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  dle */.  int fla
1cea3 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
1cea4 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20 66      /* Opening f
1cea5 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  lags */.  int nB
1cea6 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  uf              
1cea7 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 62 75       /* Bytes bu
1cea8 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 6f 70  ffered before op
1cea9 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a  ening the file *
1ceaa 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69  /.){.  JournalFi
1ceab 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
1ceac 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 6d  File *)pJfd;.  m
1cead 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c 69  emset(p, 0, sqli
1ceae 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70  te3JournalSize(p
1ceaf 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e 42 75  Vfs));.  if( nBu
1ceb0 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 42  f>0 ){.    p->zB
1ceb1 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  uf = sqlite3Mall
1ceb2 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a 20 20  ocZero(nBuf);.  
1ceb3 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66 20 29    if( !p->zBuf )
1ceb4 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
1ceb5 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1ceb6 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
1ceb7 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73  return sqlite3Os
1ceb8 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d 65  Open(pVfs, zName
1ceb9 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c 20 30  , pJfd, flags, 0
1ceba 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d 65 74  );.  }.  p->pMet
1cebb 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c 46 69  hod = &JournalFi
1cebc 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70 2d 3e  leMethods;.  p->
1cebd 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 20 70  nBuf = nBuf;.  p
1cebe 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b  ->flags = flags;
1cebf 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d  .  p->zJournal =
1cec0 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70 56 66   zName;.  p->pVf
1cec1 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65 74 75  s = pVfs;.  retu
1cec2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1cec3 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 72  ./*.** If the ar
1cec4 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74 73 20  gument p points 
1cec5 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  to a JournalFile
1cec6 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e 64 20   structure, and 
1cec7 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a  the underlying.*
1cec8 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 79  * file has not y
1cec9 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c  et been created,
1ceca 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77 2e 0a   create it now..
1cecb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1cecc 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75  E int sqlite3Jou
1cecd 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69 74  rnalCreate(sqlit
1cece 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 20 69  e3_file *p){.  i
1cecf 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73 21 3d  f( p->pMethods!=
1ced0 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68  &JournalFileMeth
1ced1 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ods ){.    retur
1ced2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1ced3 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 65  .  return create
1ced4 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46 69 6c  File((JournalFil
1ced5 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  e *)p);.}../* .*
1ced6 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1ced7 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71  ber of bytes req
1ced8 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61  uired to store a
1ced9 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68 61   JournalFile tha
1ceda 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 56  t uses vfs.** pV
1cedb 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  fs to create the
1cedc 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64   underlying on-d
1cedd 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51  isk files..*/.SQ
1cede 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1cedf 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
1cee0 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ize(sqlite3_vfs 
1cee1 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 72 6e  *pVfs){.  return
1cee2 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65   (pVfs->szOsFile
1cee3 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c 46  +sizeof(JournalF
1cee4 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  ile));.}.#endif.
1cee5 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1cee6 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 2e   End of journal.
1cee7 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1cee8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cee9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1ceea 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1ceeb 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 6a   Begin file memj
1ceec 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ournal.c *******
1ceed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ceee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1ceef 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f  ./*.** 2008 Octo
1cef0 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ber 7.**.** The 
1cef1 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1cef2 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1cef3 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1cef4 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1cef5 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1cef6 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1cef7 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1cef8 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1cef9 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1cefa 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1cefb 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1cefc 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1cefd 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1cefe 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1ceff 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1cf00 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1cf01 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1cf02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cf03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cf04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cf05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cf06 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
1cf07 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
1cf08 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d  de use to implem
1cf09 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ent an in-memory
1cf0a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
1cf0b 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d  l..** The in-mem
1cf0c 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  ory rollback jou
1cf0d 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 6f 20  rnal is used to 
1cf0e 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 63 74  journal transact
1cf0f 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a 6d 65  ions for.** ":me
1cf10 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 65 73  mory:" databases
1cf11 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 6a 6f   and when the jo
1cf12 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f 52  urnal_mode=MEMOR
1cf13 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 65 64  Y pragma is used
1cf14 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
1cf15 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 2c 76  : memjournal.c,v
1cf16 20 31 2e 38 20 32 30 30 38 2f 31 32 2f 32 30 20   1.8 2008/12/20 
1cf17 30 32 3a 31 34 3a 34 30 20 64 72 68 20 45 78 70  02:14:40 drh Exp
1cf18 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72   $.*/../* Forwar
1cf19 64 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  d references to 
1cf1a 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75  internal structu
1cf1b 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73  res */.typedef s
1cf1c 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c  truct MemJournal
1cf1d 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 79 70   MemJournal;.typ
1cf1e 65 64 65 66 20 73 74 72 75 63 74 20 46 69 6c 65  edef struct File
1cf1f 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e 74 3b  Point FilePoint;
1cf20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1cf21 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68  FileChunk FileCh
1cf22 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 20 74  unk;../* Space t
1cf23 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62  o hold the rollb
1cf24 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61  ack journal is a
1cf25 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72  llocated in incr
1cf26 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 69  ements of.** thi
1cf27 73 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a 2a 2f  s many bytes..*/
1cf28 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c  .#define JOURNAL
1cf29 5f 43 48 55 4e 4b 53 49 5a 45 20 31 30 32 34 0a  _CHUNKSIZE 1024.
1cf2a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e  ./* Macro to fin
1cf2b 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66  d the minimum of
1cf2c 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c   two numeric val
1cf2d 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ues..*/.#ifndef 
1cf2e 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e  MIN.# define MIN
1cf2f 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f 28  (x,y) ((x)<(y)?(
1cf30 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a  x):(y)).#endif..
1cf31 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61  /*.** The rollba
1cf32 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 6f  ck journal is co
1cf33 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e 6b  mposed of a link
1cf34 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65  ed list of these
1cf35 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a   structures..*/.
1cf36 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b  struct FileChunk
1cf37 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a   {.  FileChunk *
1cf38 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  pNext;          
1cf39 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75       /* Next chu
1cf3a 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  nk in the journa
1cf3b 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e 6b  l */.  u8 zChunk
1cf3c 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49  [JOURNAL_CHUNKSI
1cf3d 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65 6e  ZE];   /* Conten
1cf3e 74 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 20  t of this chunk 
1cf3f 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
1cf40 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
1cf41 20 6f 62 6a 65 63 74 20 73 65 72 76 65 73 20 61   object serves a
1cf42 73 20 61 20 63 75 72 73 6f 72 20 69 6e 74 6f 20  s a cursor into 
1cf43 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
1cf44 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75 72  rnal..** The cur
1cf45 73 6f 72 20 63 61 6e 20 62 65 20 65 69 74 68 65  sor can be eithe
1cf46 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72  r for reading or
1cf47 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74 72   writing..*/.str
1cf48 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b 0a  uct FilePoint {.
1cf49 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
1cf4a 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  iOffset;        
1cf4b 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d    /* Offset from
1cf4c 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
1cf4d 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  f the file */.  
1cf4e 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e  FileChunk *pChun
1cf4f 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
1cf50 2f 2a 20 53 70 65 63 69 66 69 63 20 63 68 75 6e  /* Specific chun
1cf51 6b 20 69 6e 74 6f 20 77 68 69 63 68 20 63 75 72  k into which cur
1cf52 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d 3b  sor points */.};
1cf53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75 62  ../*.** This sub
1cf54 63 6c 61 73 73 20 69 73 20 61 20 73 75 62 63 6c  class is a subcl
1cf55 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66  ass of sqlite3_f
1cf56 69 6c 65 2e 20 20 45 61 63 68 20 6f 70 65 6e 20  ile.  Each open 
1cf57 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a 2a  memory-journal.*
1cf58 2a 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  * is an instance
1cf59 20 6f 66 20 74 68 69 73 20 63 6c 61 73 73 2e 0a   of this class..
1cf5a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75  */.struct MemJou
1cf5b 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33  rnal {.  sqlite3
1cf5c 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65  _io_methods *pMe
1cf5d 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72 65  thod;    /* Pare
1cf5e 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53 54 20 42  nt class. MUST B
1cf5f 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 46 69 6c  E FIRST */.  Fil
1cf60 65 43 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b 20  eChunk *pFirst; 
1cf61 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cf62 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72  Head of in-memor
1cf63 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f 0a  y chunk-list */.
1cf64 20 20 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 70    FilePoint endp
1cf65 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  oint;           
1cf66 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
1cf67 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  the end of the f
1cf68 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69  ile */.  FilePoi
1cf69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 20  nt readpoint;   
1cf6a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
1cf6b 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ter to the end o
1cf6c 66 20 74 68 65 20 6c 61 73 74 20 78 52 65 61 64  f the last xRead
1cf6d 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  () */.};../*.** 
1cf6e 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 74  Read data from t
1cf6f 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
1cf70 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65  ic int memjrnlRe
1cf71 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  ad(.  sqlite3_fi
1cf72 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20  le *pJfd,    /* 
1cf73 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  The journal file
1cf74 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72   from which to r
1cf75 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a  ead */.  void *z
1cf76 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
1cf77 2f 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c  /* Put the resul
1cf78 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  ts here */.  int
1cf79 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20   iAmt,          
1cf7a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1cf7b 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a   bytes to read *
1cf7c 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  /.  sqlite_int64
1cf7d 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65   iOfst     /* Be
1cf7e 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74  gin reading at t
1cf7f 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b  his offset */.){
1cf80 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70  .  MemJournal *p
1cf81 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a   = (MemJournal *
1cf82 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75  )pJfd;.  u8 *zOu
1cf83 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20  t = zBuf;.  int 
1cf84 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20  nRead = iAmt;.  
1cf85 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  int iChunkOffset
1cf86 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70  ;.  FileChunk *p
1cf87 43 68 75 6e 6b 3b 0a 0a 20 20 61 73 73 65 72 74  Chunk;..  assert
1cf88 28 20 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d  ( iOfst+iAmt<=p-
1cf89 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65  >endpoint.iOffse
1cf8a 74 20 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72  t );..  if( p->r
1cf8b 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74  eadpoint.iOffset
1cf8c 21 3d 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74  !=iOfst || iOfst
1cf8d 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1cf8e 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20  e3_int64 iOff = 
1cf8f 30 3b 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e  0;.    for(pChun
1cf90 6b 3d 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20  k=p->pFirst; .  
1cf91 20 20 20 20 20 20 70 43 68 75 6e 6b 20 26 26 20        pChunk && 
1cf92 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48  (iOff+JOURNAL_CH
1cf93 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b  UNKSIZE)<=iOfst;
1cf94 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d  .        pChunk=
1cf95 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20  pChunk->pNext.  
1cf96 20 20 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20    ){.      iOff 
1cf97 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  += JOURNAL_CHUNK
1cf98 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  SIZE;.    }.  }e
1cf99 6c 73 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20  lse{.    pChunk 
1cf9a 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70  = p->readpoint.p
1cf9b 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43  Chunk;.  }..  iC
1cf9c 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e  hunkOffset = (in
1cf9d 74 29 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c  t)(iOfst%JOURNAL
1cf9e 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64  _CHUNKSIZE);.  d
1cf9f 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61  o {.    int iSpa
1cfa0 63 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55  ce = JOURNAL_CHU
1cfa1 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f  NKSIZE - iChunkO
1cfa2 66 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e  ffset;.    int n
1cfa3 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64  Copy = MIN(nRead
1cfa4 2c 20 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  , (JOURNAL_CHUNK
1cfa5 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66  SIZE - iChunkOff
1cfa6 73 65 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70  set));.    memcp
1cfa7 79 28 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d  y(zOut, &pChunk-
1cfa8 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66  >zChunk[iChunkOf
1cfa9 66 73 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20  fset], nCopy);. 
1cfaa 20 20 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79     zOut += nCopy
1cfab 3b 0a 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69  ;.    nRead -= i
1cfac 53 70 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e  Space;.    iChun
1cfad 6b 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d  kOffset = 0;.  }
1cfae 20 77 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30   while( nRead>=0
1cfaf 20 26 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75   && (pChunk=pChu
1cfb0 6e 6b 2d 3e 70 4e 65 78 74 29 21 3d 30 20 26 26  nk->pNext)!=0 &&
1cfb1 20 6e 52 65 61 64 3e 30 20 29 3b 0a 20 20 70 2d   nRead>0 );.  p-
1cfb2 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73  >readpoint.iOffs
1cfb3 65 74 20 3d 20 69 4f 66 73 74 2b 69 41 6d 74 3b  et = iOfst+iAmt;
1cfb4 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e  .  p->readpoint.
1cfb5 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b  pChunk = pChunk;
1cfb6 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
1cfb7 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  E_OK;.}../*.** W
1cfb8 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65  rite data to the
1cfb9 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1cfba 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57 72 69 74   int memjrnlWrit
1cfbb 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  e(.  sqlite3_fil
1cfbc 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54  e *pJfd,    /* T
1cfbd 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1cfbe 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  into which to wr
1cfbf 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ite */.  const v
1cfc0 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20  oid *zBuf,      
1cfc1 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20  /* Take data to 
1cfc2 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20  be written from 
1cfc3 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41  here */.  int iA
1cfc4 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
1cfc5 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1cfc6 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  tes to write */.
1cfc7 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
1cfc8 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69  Ofst     /* Begi
1cfc9 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69  n writing at thi
1cfca 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68  s offset into th
1cfcb 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d  e file */.){.  M
1cfcc 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28  emJournal *p = (
1cfcd 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66  MemJournal *)pJf
1cfce 64 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20  d;.  int nWrite 
1cfcf 3d 20 69 41 6d 74 3b 0a 20 20 75 38 20 2a 7a 57  = iAmt;.  u8 *zW
1cfd0 72 69 74 65 20 3d 20 28 75 38 20 2a 29 7a 42 75  rite = (u8 *)zBu
1cfd1 66 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d  f;..  /* An in-m
1cfd2 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69  emory journal fi
1cfd3 6c 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 65  le should only e
1cfd4 76 65 72 20 62 65 20 61 70 70 65 6e 64 65 64 20  ver be appended 
1cfd5 74 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20  to. Random.  ** 
1cfd6 61 63 63 65 73 73 20 77 72 69 74 65 73 20 61 72  access writes ar
1cfd7 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 62  e not required b
1cfd8 79 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20  y sqlite..  */. 
1cfd9 20 61 73 73 65 72 74 28 69 4f 66 73 74 3d 3d 70   assert(iOfst==p
1cfda 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73  ->endpoint.iOffs
1cfdb 65 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  et);.  UNUSED_PA
1cfdc 52 41 4d 45 54 45 52 28 69 4f 66 73 74 29 3b 0a  RAMETER(iOfst);.
1cfdd 0a 20 20 77 68 69 6c 65 28 20 6e 57 72 69 74 65  .  while( nWrite
1cfde 3e 30 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68  >0 ){.    FileCh
1cfdf 75 6e 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 70 2d  unk *pChunk = p-
1cfe0 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b  >endpoint.pChunk
1cfe1 3b 0a 20 20 20 20 69 6e 74 20 69 43 68 75 6e 6b  ;.    int iChunk
1cfe2 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 70  Offset = (int)(p
1cfe3 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73  ->endpoint.iOffs
1cfe4 65 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  et%JOURNAL_CHUNK
1cfe5 53 49 5a 45 29 3b 0a 20 20 20 20 69 6e 74 20 69  SIZE);.    int i
1cfe6 53 70 61 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69  Space = MIN(nWri
1cfe7 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e  te, JOURNAL_CHUN
1cfe8 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66  KSIZE - iChunkOf
1cfe9 66 73 65 74 29 3b 0a 0a 20 20 20 20 69 66 28 20  fset);..    if( 
1cfea 69 43 68 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20  iChunkOffset==0 
1cfeb 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 65 77 20  ){.      /* New 
1cfec 63 68 75 6e 6b 20 69 73 20 72 65 71 75 69 72 65  chunk is require
1cfed 64 20 74 6f 20 65 78 74 65 6e 64 20 74 68 65 20  d to extend the 
1cfee 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46  file. */.      F
1cfef 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 77 20 3d  ileChunk *pNew =
1cff0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
1cff1 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e 6b  sizeof(FileChunk
1cff2 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  ));.      if( !p
1cff3 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72  New ){.        r
1cff4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
1cff5 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  RR_NOMEM;.      
1cff6 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e  }.      pNew->pN
1cff7 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ext = 0;.      i
1cff8 66 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20  f( pChunk ){.   
1cff9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
1cffa 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20  pFirst );.      
1cffb 20 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 20    pChunk->pNext 
1cffc 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65  = pNew;.      }e
1cffd 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
1cffe 65 72 74 28 20 21 70 2d 3e 70 46 69 72 73 74 20  ert( !p->pFirst 
1cfff 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46  );.        p->pF
1d000 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20  irst = pNew;.   
1d001 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 65 6e     }.      p->en
1d002 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20  dpoint.pChunk = 
1d003 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  pNew;.    }..   
1d004 20 6d 65 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70   memcpy(&p->endp
1d005 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68  oint.pChunk->zCh
1d006 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74  unk[iChunkOffset
1d007 5d 2c 20 7a 57 72 69 74 65 2c 20 69 53 70 61 63  ], zWrite, iSpac
1d008 65 29 3b 0a 20 20 20 20 7a 57 72 69 74 65 20 2b  e);.    zWrite +
1d009 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20 6e 57  = iSpace;.    nW
1d00a 72 69 74 65 20 2d 3d 20 69 53 70 61 63 65 3b 0a  rite -= iSpace;.
1d00b 20 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e      p->endpoint.
1d00c 69 4f 66 66 73 65 74 20 2b 3d 20 69 53 70 61 63  iOffset += iSpac
1d00d 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  e;.  }..  return
1d00e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1d00f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68  *.** Truncate th
1d010 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  e file..*/.stati
1d011 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 75  c int memjrnlTru
1d012 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69  ncate(sqlite3_fi
1d013 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65  le *pJfd, sqlite
1d014 5f 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20  _int64 size){.  
1d015 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20  MemJournal *p = 
1d016 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a  (MemJournal *)pJ
1d017 66 64 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20  fd;.  FileChunk 
1d018 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 73 73 65 72  *pChunk;.  asser
1d019 74 28 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 4e  t(size==0);.  UN
1d01a 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73  USED_PARAMETER(s
1d01b 69 7a 65 29 3b 0a 20 20 70 43 68 75 6e 6b 20 3d  ize);.  pChunk =
1d01c 20 70 2d 3e 70 46 69 72 73 74 3b 0a 20 20 77 68   p->pFirst;.  wh
1d01d 69 6c 65 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20  ile( pChunk ){. 
1d01e 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 54     FileChunk *pT
1d01f 6d 70 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 20  mp = pChunk;.   
1d020 20 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b   pChunk = pChunk
1d021 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c  ->pNext;.    sql
1d022 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70 29 3b  ite3_free(pTmp);
1d023 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d 65  .  }.  sqlite3Me
1d024 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a 66  mJournalOpen(pJf
1d025 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  d);.  return SQL
1d026 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
1d027 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e   Close the file.
1d028 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
1d029 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69  emjrnlClose(sqli
1d02a 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b  te3_file *pJfd){
1d02b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61  .  memjrnlTrunca
1d02c 74 65 28 70 4a 66 64 2c 20 30 29 3b 0a 20 20 72  te(pJfd, 0);.  r
1d02d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1d02e 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20  .}.../*.** Sync 
1d02f 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1d030 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53  tic int memjrnlS
1d031 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ync(sqlite3_file
1d032 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e   *NotUsed, int N
1d033 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53  otUsed2){.  UNUS
1d034 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
1d035 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
1d036 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1d037 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  E_OK;.}../*.** Q
1d038 75 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66  uery the size of
1d039 20 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74   the file in byt
1d03a 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
1d03b 74 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a  t memjrnlFileSiz
1d03c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
1d03d 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74  pJfd, sqlite_int
1d03e 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 4d 65  64 *pSize){.  Me
1d03f 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d  mJournal *p = (M
1d040 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64  emJournal *)pJfd
1d041 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71  ;.  *pSize = (sq
1d042 6c 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 65  lite_int64) p->e
1d043 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 3b  ndpoint.iOffset;
1d044 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1d045 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61  _OK;.}../*.** Ta
1d046 62 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66  ble of methods f
1d047 6f 72 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 73 71  or MemJournal sq
1d048 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63  lite3_file objec
1d049 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72  t..*/.static str
1d04a 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  uct sqlite3_io_m
1d04b 65 74 68 6f 64 73 20 4d 65 6d 4a 6f 75 72 6e 61  ethods MemJourna
1d04c 6c 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31  lMethods = {.  1
1d04d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1d04e 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
1d04f 20 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20    memjrnlClose, 
1d050 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f      /* xClose */
1d051 0a 20 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 2c 20  .  memjrnlRead, 
1d052 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f       /* xRead */
1d053 0a 20 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 2c  .  memjrnlWrite,
1d054 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a       /* xWrite *
1d055 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63  /.  memjrnlTrunc
1d056 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61  ate,  /* xTrunca
1d057 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53  te */.  memjrnlS
1d058 79 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79  ync,      /* xSy
1d059 6e 63 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46  nc */.  memjrnlF
1d05a 69 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69  ileSize,  /* xFi
1d05b 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20  leSize */.  0,  
1d05c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d05d 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20   xLock */.  0,  
1d05e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d05f 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c   xUnlock */.  0,
1d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d061 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
1d062 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20  dLock */.  0,   
1d063 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d064 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a  xFileControl */.
1d065 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
1d066 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69      /* xSectorSi
1d067 7a 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20  ze */.  0       
1d068 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65            /* xDe
1d069 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
1d06a 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a  ics */.};../* .*
1d06b 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c  * Open a journal
1d06c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   file..*/.SQLITE
1d06d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1d06e 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f  lite3MemJournalO
1d06f 70 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  pen(sqlite3_file
1d070 20 2a 70 4a 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f   *pJfd){.  MemJo
1d071 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a  urnal *p = (MemJ
1d072 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20  ournal *)pJfd;. 
1d073 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71   memset(p, 0, sq
1d074 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53  lite3MemJournalS
1d075 69 7a 65 28 29 29 3b 0a 20 20 70 2d 3e 70 4d 65  ize());.  p->pMe
1d076 74 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e  thod = &MemJourn
1d077 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a  alMethods;.}../*
1d078 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
1d079 69 66 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64  if the file-hand
1d07a 6c 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  le passed as an 
1d07b 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20  argument is .** 
1d07c 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  an in-memory jou
1d07d 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rnal .*/.SQLITE_
1d07e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1d07f 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28  te3IsMemJournal(
1d080 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1d081 66 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 4a  fd){.  return pJ
1d082 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26 4d  fd->pMethods==&M
1d083 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73  emJournalMethods
1d084 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75  ;.}../* .** Retu
1d085 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
1d086 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20   bytes required 
1d087 74 6f 20 73 74 6f 72 65 20 61 20 4d 65 6d 4a 6f  to store a MemJo
1d088 75 72 6e 61 6c 20 74 68 61 74 20 75 73 65 73 20  urnal that uses 
1d089 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63  vfs.** pVfs to c
1d08a 72 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c  reate the underl
1d08b 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c  ying on-disk fil
1d08c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
1d08d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1d08e 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  3MemJournalSize(
1d08f 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20  void){.  return 
1d090 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61  sizeof(MemJourna
1d091 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  l);.}../********
1d092 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
1d093 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a  mjournal.c *****
1d094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d095 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d096 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1d097 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1d098 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a  e walker.c *****
1d099 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d09a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d09b 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1d09c 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a  8 August 16.**.*
1d09d 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
1d09e 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
1d09f 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
1d0a0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1d0a1 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1d0a2 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1d0a3 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1d0a4 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1d0a5 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1d0a6 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1d0a7 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1d0a8 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1d0a9 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1d0aa 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1d0ab 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1d0ac 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1d0ad 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
1d0ae 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1d0af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1d0b3 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1d0b4 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66   routines used f
1d0b5 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70  or walking the p
1d0b6 61 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a  arser tree for.*
1d0b7 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  * an SQL stateme
1d0b8 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77  nt..**.** $Id: w
1d0b9 61 6c 6b 65 72 2e 63 2c 76 20 31 2e 31 20 32 30  alker.c,v 1.1 20
1d0ba 30 38 2f 30 38 2f 32 30 20 31 36 3a 33 35 3a 31  08/08/20 16:35:1
1d0bb 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  0 drh Exp $.*/..
1d0bc 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  ./*.** Walk an e
1d0bd 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
1d0be 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c   Invoke the call
1d0bf 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61  back once for ea
1d0c0 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68  ch node.** of th
1d0c1 65 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68  e expression, wh
1d0c2 69 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20  ile decending.  
1d0c3 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (In other words,
1d0c4 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a   the callback.**
1d0c5 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f   is invoked befo
1d0c6 72 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c  re visiting chil
1d0c7 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65  dren.).**.** The
1d0c8 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
1d0c9 6f 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  om the callback 
1d0ca 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66  should be one of
1d0cb 20 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f   the WRC_*.** co
1d0cc 6e 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69  nstants to speci
1d0cd 66 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65  fy how to procee
1d0ce 64 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e  d with the walk.
1d0cf 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f  .**.**    WRC_Co
1d0d0 6e 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74  ntinue      Cont
1d0d1 69 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20  inue descending 
1d0d2 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  down the tree..*
1d0d3 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e  *.**    WRC_Prun
1d0d4 65 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74  e         Do not
1d0d5 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68   descend into ch
1d0d6 69 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20  ild nodes.  But 
1d0d7 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  allow.**        
1d0d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
1d0d9 65 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e  e walk to contin
1d0da 75 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20  ue with sibling 
1d0db 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  nodes..**.**    
1d0dc 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20  WRC_Abort       
1d0dd 20 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c    Do no more cal
1d0de 6c 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20  lbacks.  Unwind 
1d0df 74 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a  the stack and.**
1d0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d0e1 20 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65        return the
1d0e2 20 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20   top-level walk 
1d0e3 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  call..**.** The 
1d0e4 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f  return value fro
1d0e5 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  m this routine i
1d0e6 73 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61  s WRC_Abort to a
1d0e7 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20  bandon the tree 
1d0e8 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f  walk.** and WRC_
1d0e9 43 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74  Continue to cont
1d0ea 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  inue..*/.SQLITE_
1d0eb 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1d0ec 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b  te3WalkExpr(Walk
1d0ed 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70  er *pWalker, Exp
1d0ee 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74  r *pExpr){.  int
1d0ef 20 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72   rc;.  if( pExpr
1d0f0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
1d0f1 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20  _Continue;.  rc 
1d0f2 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72  = pWalker->xExpr
1d0f3 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72  Callback(pWalker
1d0f4 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20  , pExpr);.  if( 
1d0f5 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65  rc==WRC_Continue
1d0f6 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
1d0f7 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1d0f8 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  ker, pExpr->pLef
1d0f9 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
1d0fa 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73  Abort;.    if( s
1d0fb 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
1d0fc 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70  Walker, pExpr->p
1d0fd 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
1d0fe 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69  WRC_Abort;.    i
1d0ff 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  f( sqlite3WalkEx
1d100 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20  prList(pWalker, 
1d101 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 20 29 20  pExpr->pList) ) 
1d102 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1d103 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1d104 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c  3WalkSelect(pWal
1d105 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c  ker, pExpr->pSel
1d106 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ect) ){.      re
1d107 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1d108 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1d109 72 6e 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72  rn rc & WRC_Abor
1d10a 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  t;.}../*.** Call
1d10b 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
1d10c 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70  () for every exp
1d10d 72 65 73 73 69 6f 6e 20 69 6e 20 6c 69 73 74 20  ression in list 
1d10e 70 20 6f 72 20 75 6e 74 69 6c 0a 2a 2a 20 61 6e  p or until.** an
1d10f 20 61 62 6f 72 74 20 72 65 71 75 65 73 74 20 69   abort request i
1d110 73 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  s seen..*/.SQLIT
1d111 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1d112 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
1d113 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  t(Walker *pWalke
1d114 72 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 29 7b  r, ExprList *p){
1d115 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 57  .  int i, rc = W
1d116 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 73  RC_Continue;.  s
1d117 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
1d118 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66  tem *pItem;.  if
1d119 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ( p ){.    for(i
1d11a 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d  =p->nExpr, pItem
1d11b 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c  =p->a; i>0; i--,
1d11c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1d11d 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1d11e 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 49  Expr(pWalker, pI
1d11f 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 72 65  tem->pExpr) ) re
1d120 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1d121 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1d122 72 6e 20 72 63 20 26 20 57 52 43 5f 43 6f 6e 74  rn rc & WRC_Cont
1d123 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  inue;.}../*.** W
1d124 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69  alk all expressi
1d125 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ons associated w
1d126 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 74 65  ith SELECT state
1d127 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e  ment p.  Do.** n
1d128 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45  ot invoke the SE
1d129 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e  LECT callback on
1d12a 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63   p, but do (of c
1d12b 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a  ourse) invoke.**
1d12c 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61   any expr callba
1d12d 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63  cks and SELECT c
1d12e 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f  allbacks that co
1d12f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69  me from subqueri
1d130 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52  es..** Return WR
1d131 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43  C_Abort or WRC_C
1d132 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49  ontinue..*/.SQLI
1d133 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1d134 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
1d135 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61  Expr(Walker *pWa
1d136 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
1d137 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  {.  if( sqlite3W
1d138 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1d139 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20  ker, p->pEList) 
1d13a 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1d13b 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
1d13c 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
1d13d 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20  r, p->pWhere) ) 
1d13e 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1d13f 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  ;.  if( sqlite3W
1d140 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1d141 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79  ker, p->pGroupBy
1d142 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1d143 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  bort;.  if( sqli
1d144 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1d145 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29  ker, p->pHaving)
1d146 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1d147 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ort;.  if( sqlit
1d148 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  e3WalkExprList(p
1d149 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65  Walker, p->pOrde
1d14a 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52  rBy) ) return WR
1d14b 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73  C_Abort;.  if( s
1d14c 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
1d14d 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69  Walker, p->pLimi
1d14e 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
1d14f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c  Abort;.  if( sql
1d150 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1d151 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74  lker, p->pOffset
1d152 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1d153 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57  bort;.  return W
1d154 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a  RC_Continue;.}..
1d155 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70  /*.** Walk the p
1d156 61 72 73 65 20 74 72 65 65 73 20 61 73 73 6f 63  arse trees assoc
1d157 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73  iated with all s
1d158 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65  ubqueries in the
1d159 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  .** FROM clause 
1d15a 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  of SELECT statem
1d15b 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69  ent p.  Do not i
1d15c 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74  nvoke the select
1d15d 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20  .** callback on 
1d15e 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65  p, but do invoke
1d15f 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d   it on each FROM
1d160 20 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79   clause subquery
1d161 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73  .** and on any s
1d162 75 62 71 75 65 72 69 65 73 20 66 75 72 74 68 65  ubqueries furthe
1d163 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72  r down in the tr
1d164 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20  ee.  Return .** 
1d165 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43  WRC_Abort or WRC
1d166 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51  _Continue;.*/.SQ
1d167 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1d168 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
1d169 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70  ctFrom(Walker *p
1d16a 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
1d16b 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  p){.  SrcList *p
1d16c 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Src;.  int i;.  
1d16d 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
1d16e 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70  tem *pItem;..  p
1d16f 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  Src = p->pSrc;. 
1d170 20 69 66 28 20 70 53 72 63 20 29 7b 0a 20 20 20   if( pSrc ){.   
1d171 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72   for(i=pSrc->nSr
1d172 63 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61  c, pItem=pSrc->a
1d173 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
1d174 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  m++){.      if( 
1d175 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1d176 74 28 70 57 61 6c 6b 65 72 2c 20 70 49 74 65 6d  t(pWalker, pItem
1d177 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  ->pSelect) ){.  
1d178 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1d179 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a  _Abort;.      }.
1d17a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1d17b 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
1d17c 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  .} ../*.** Call 
1d17d 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1d17e 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 72  ) for every expr
1d17f 65 73 73 69 6f 6e 20 69 6e 20 53 65 6c 65 63 74  ession in Select
1d180 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 2a 2a   statement p..**
1d181 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 57   Invoke sqlite3W
1d182 61 6c 6b 53 65 6c 65 63 74 28 29 20 66 6f 72 20  alkSelect() for 
1d183 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68  subqueries in th
1d184 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e  e FROM clause an
1d185 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70  d.** on the comp
1d186 6f 75 6e 64 20 73 65 6c 65 63 74 20 63 68 61 69  ound select chai
1d187 6e 2c 20 70 2d 3e 70 50 72 69 6f 72 2e 0a 2a 2a  n, p->pPrior..**
1d188 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 43  .** Return WRC_C
1d189 6f 6e 74 69 6e 75 65 20 75 6e 64 65 72 20 6e 6f  ontinue under no
1d18a 72 6d 61 6c 20 63 6f 6e 64 69 74 69 6f 6e 73 2e  rmal conditions.
1d18b 20 20 52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    Return WRC_Abo
1d18c 72 74 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 69  rt if.** there i
1d18d 73 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75 65  s an abort reque
1d18e 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  st..**.** If the
1d18f 20 57 61 6c 6b 65 72 20 64 6f 65 73 20 6e 6f 74   Walker does not
1d190 20 68 61 76 65 20 61 6e 20 78 53 65 6c 65 63 74   have an xSelect
1d191 43 61 6c 6c 62 61 63 6b 28 29 20 74 68 65 6e 20  Callback() then 
1d192 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1d193 69 73 20 61 20 6e 6f 2d 6f 70 20 72 65 74 75 72  is a no-op retur
1d194 6e 69 6e 67 20 57 52 43 5f 43 6f 6e 74 69 6e 75  ning WRC_Continu
1d195 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1d196 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1d197 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65  WalkSelect(Walke
1d198 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
1d199 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  ct *p){.  int rc
1d19a 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
1d19b 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74  pWalker->xSelect
1d19c 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65  Callback==0 ) re
1d19d 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
1d19e 65 3b 0a 20 20 72 63 20 3d 20 57 52 43 5f 43 6f  e;.  rc = WRC_Co
1d19f 6e 74 69 6e 75 65 3b 0a 20 20 77 68 69 6c 65 28  ntinue;.  while(
1d1a0 20 70 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20   p  ){.    rc = 
1d1a1 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74  pWalker->xSelect
1d1a2 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72  Callback(pWalker
1d1a3 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , p);.    if( rc
1d1a4 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66   ) break;.    if
1d1a5 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  ( sqlite3WalkSel
1d1a6 65 63 74 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  ectExpr(pWalker,
1d1a7 20 70 29 20 29 20 72 65 74 75 72 6e 20 57 52 43   p) ) return WRC
1d1a8 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20  _Abort;.    if( 
1d1a9 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1d1aa 74 46 72 6f 6d 28 70 57 61 6c 6b 65 72 2c 20 70  tFrom(pWalker, p
1d1ab 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1d1ac 62 6f 72 74 3b 0a 20 20 20 20 70 20 3d 20 70 2d  bort;.    p = p-
1d1ad 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72  >pPrior;.  }.  r
1d1ae 65 74 75 72 6e 20 72 63 20 26 20 57 52 43 5f 41  eturn rc & WRC_A
1d1af 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  bort;.}../******
1d1b0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1d1b1 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  walker.c *******
1d1b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1b4 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1d1b5 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1d1b6 69 6c 65 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a  ile resolve.c **
1d1b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1b9 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1d1ba 30 30 38 20 41 75 67 75 73 74 20 31 38 0a 2a 2a  008 August 18.**
1d1bb 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1d1bc 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1d1bd 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1d1be 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1d1bf 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1d1c0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1d1c1 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1d1c2 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1d1c3 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1d1c4 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1d1c5 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1d1c6 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1d1c7 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1d1c8 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1d1c9 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1d1ca 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1d1cb 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1d1cc 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1d1cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
1d1d1 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1d1d2 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75  tains routines u
1d1d3 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20  sed for walking 
1d1d4 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20  the parser tree 
1d1d5 61 6e 64 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 61  and.** resolve a
1d1d6 6c 6c 20 69 64 65 6e 74 69 66 69 65 72 73 20 62  ll identifiers b
1d1d7 79 20 61 73 73 6f 63 69 61 74 69 6e 67 20 74 68  y associating th
1d1d8 65 6d 20 77 69 74 68 20 61 20 70 61 72 74 69 63  em with a partic
1d1d9 75 6c 61 72 0a 2a 2a 20 74 61 62 6c 65 20 61 6e  ular.** table an
1d1da 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  d column..**.** 
1d1db 24 49 64 3a 20 72 65 73 6f 6c 76 65 2e 63 2c 76  $Id: resolve.c,v
1d1dc 20 31 2e 31 35 20 32 30 30 38 2f 31 32 2f 31 30   1.15 2008/12/10
1d1dd 20 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45 78   19:26:24 drh Ex
1d1de 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75  p $.*/../*.** Tu
1d1df 72 6e 20 74 68 65 20 70 45 78 70 72 20 65 78 70  rn the pExpr exp
1d1e0 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 6e 20  ression into an 
1d1e1 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 69 43  alias for the iC
1d1e2 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20  ol-th column of 
1d1e3 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65  the.** result se
1d1e4 74 20 69 6e 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a  t in pEList..**.
1d1e5 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74  ** If the result
1d1e6 20 73 65 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61   set column is a
1d1e7 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72   simple column r
1d1e8 65 66 65 72 65 6e 63 65 2c 20 74 68 65 6e 20 74  eference, then t
1d1e9 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d  his routine.** m
1d1ea 61 6b 65 73 20 61 6e 20 65 78 61 63 74 20 63 6f  akes an exact co
1d1eb 70 79 2e 20 20 42 75 74 20 66 6f 72 20 61 6e 79  py.  But for any
1d1ec 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 65   other kind of e
1d1ed 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 69 73 0a  xpression, this.
1d1ee 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20  ** routine make 
1d1ef 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65  a copy of the re
1d1f0 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 20  sult set column 
1d1f1 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  as the argument 
1d1f2 74 6f 20 74 68 65 0a 2a 2a 20 54 4b 5f 41 53 20  to the.** TK_AS 
1d1f3 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 54  operator.  The T
1d1f4 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20 63 61  K_AS operator ca
1d1f5 75 73 65 73 20 74 68 65 20 65 78 70 72 65 73 73  uses the express
1d1f6 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 76 61  ion to be.** eva
1d1f7 6c 75 61 74 65 64 20 6a 75 73 74 20 6f 6e 63 65  luated just once
1d1f8 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64   and then reused
1d1f9 20 66 6f 72 20 65 61 63 68 20 61 6c 69 61 73 2e   for each alias.
1d1fa 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 61 73 6f  .**.** The reaso
1d1fb 6e 20 66 6f 72 20 73 75 70 70 72 65 73 73 69 6e  n for suppressin
1d1fc 67 20 74 68 65 20 54 4b 5f 41 53 20 74 65 72 6d  g the TK_AS term
1d1fd 20 77 68 65 6e 20 74 68 65 20 65 78 70 72 65 73   when the expres
1d1fe 73 69 6f 6e 20 69 73 20 61 20 73 69 6d 70 6c 65  sion is a simple
1d1ff 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72  .** column refer
1d200 65 6e 63 65 20 69 73 20 73 6f 20 74 68 61 74 20  ence is so that 
1d201 74 68 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72  the column refer
1d202 65 6e 63 65 20 77 69 6c 6c 20 62 65 20 72 65 63  ence will be rec
1d203 6f 67 6e 69 7a 65 64 20 61 73 0a 2a 2a 20 75 73  ognized as.** us
1d204 61 62 6c 65 20 62 79 20 69 6e 64 69 63 65 73 20  able by indices 
1d205 77 69 74 68 69 6e 20 74 68 65 20 57 48 45 52 45  within the WHERE
1d206 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69   clause processi
1d207 6e 67 20 6c 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a  ng logic. .**.**
1d208 20 48 61 63 6b 3a 20 20 54 68 65 20 54 4b 5f 41   Hack:  The TK_A
1d209 53 20 6f 70 65 72 61 74 6f 72 20 69 73 20 69 6e  S operator is in
1d20a 68 69 62 69 74 65 64 20 69 66 20 7a 54 79 70 65  hibited if zType
1d20b 5b 30 5d 3d 3d 27 47 27 2e 20 20 54 68 69 73 20  [0]=='G'.  This 
1d20c 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 6e  means.** that in
1d20d 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75   a GROUP BY clau
1d20e 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69  se, the expressi
1d20f 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65 64 20  on is evaluated 
1d210 74 77 69 63 65 2e 20 20 48 65 6e 63 65 3a 0a 2a  twice.  Hence:.*
1d211 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
1d212 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c  random()%5 AS x,
1d213 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74   count(*) FROM t
1d214 61 62 20 47 52 4f 55 50 20 42 59 20 78 0a 2a 2a  ab GROUP BY x.**
1d215 0a 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e  .** Is equivalen
1d216 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  t to:.**.**     
1d217 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25  SELECT random()%
1d218 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29  5 AS x, count(*)
1d219 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20   FROM tab GROUP 
1d21a 42 59 20 72 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a  BY random()%5.**
1d21b 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f  .** The result o
1d21c 66 20 72 61 6e 64 6f 6d 28 29 25 35 20 69 6e 20  f random()%5 in 
1d21d 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  the GROUP BY cla
1d21e 75 73 65 20 69 73 20 70 72 6f 62 61 62 6c 79 20  use is probably 
1d21f 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f  different.** fro
1d220 6d 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  m the result in 
1d221 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 2e 20  the result-set. 
1d222 20 57 65 20 6d 69 67 68 74 20 66 69 78 20 74 68   We might fix th
1d223 69 73 20 73 6f 6d 65 64 61 79 2e 20 20 4f 72 0a  is someday.  Or.
1d224 2a 2a 20 74 68 65 6e 20 61 67 61 69 6e 2c 20 77  ** then again, w
1d225 65 20 6d 69 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a  e might not....*
1d226 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
1d227 73 6f 6c 76 65 41 6c 69 61 73 28 0a 20 20 50 61  solveAlias(.  Pa
1d228 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1d229 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
1d22a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
1d22b 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
1d22c 20 20 20 20 2f 2a 20 41 20 72 65 73 75 6c 74 20      /* A result 
1d22d 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  set */.  int iCo
1d22e 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
1d22f 2f 2a 20 41 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  /* A column in t
1d230 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20  he result set.  
1d231 30 2e 2e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  0..pEList->nExpr
1d232 2d 31 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  -1 */.  Expr *pE
1d233 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f  xpr,           /
1d234 2a 20 54 72 61 6e 73 66 6f 72 6d 20 74 68 69 73  * Transform this
1d235 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 74   into an alias t
1d236 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  o the result set
1d237 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1d238 20 2a 7a 54 79 70 65 20 20 20 20 20 20 2f 2a 20   *zType      /* 
1d239 22 47 52 4f 55 50 22 20 6f 72 20 22 4f 52 44 45  "GROUP" or "ORDE
1d23a 52 22 20 6f 72 20 22 22 20 2a 2f 0a 29 7b 0a 20  R" or "" */.){. 
1d23b 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 20 20 20   Expr *pOrig;   
1d23c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
1d23d 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  Col-th column of
1d23e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1d23f 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 75 70 3b  */.  Expr *pDup;
1d240 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1d241 6f 70 79 20 6f 66 20 70 4f 72 69 67 20 2a 2f 0a  opy of pOrig */.
1d242 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
1d243 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1d244 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1d245 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ion */..  assert
1d246 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f  ( iCol>=0 && iCo
1d247 6c 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  l<pEList->nExpr 
1d248 29 3b 0a 20 20 70 4f 72 69 67 20 3d 20 70 45 4c  );.  pOrig = pEL
1d249 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78  ist->a[iCol].pEx
1d24a 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  pr;.  assert( pO
1d24b 72 69 67 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  rig!=0 );.  asse
1d24c 72 74 28 20 70 4f 72 69 67 2d 3e 66 6c 61 67 73  rt( pOrig->flags
1d24d 20 26 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 29   & EP_Resolved )
1d24e 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
1d24f 3e 64 62 3b 0a 20 20 70 44 75 70 20 3d 20 73 71  >db;.  pDup = sq
1d250 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
1d251 20 70 4f 72 69 67 29 3b 0a 20 20 69 66 28 20 70   pOrig);.  if( p
1d252 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Dup==0 ) return;
1d253 0a 20 20 69 66 28 20 70 44 75 70 2d 3e 6f 70 21  .  if( pDup->op!
1d254 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54  =TK_COLUMN && zT
1d255 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20  ype[0]!='G' ){. 
1d256 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
1d257 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
1d258 4b 5f 41 53 2c 20 70 44 75 70 2c 20 30 2c 20 30  K_AS, pDup, 0, 0
1d259 29 3b 0a 20 20 20 20 69 66 28 20 70 44 75 70 3d  );.    if( pDup=
1d25a 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
1d25b 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 69   if( pEList->a[i
1d25c 43 6f 6c 5d 2e 69 41 6c 69 61 73 3d 3d 30 20 29  Col].iAlias==0 )
1d25d 7b 0a 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e  {.      pEList->
1d25e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 20 3d  a[iCol].iAlias =
1d25f 20 28 75 31 36 29 28 2b 2b 70 50 61 72 73 65 2d   (u16)(++pParse-
1d260 3e 6e 41 6c 69 61 73 29 3b 0a 20 20 20 20 7d 0a  >nAlias);.    }.
1d261 20 20 20 20 70 44 75 70 2d 3e 69 54 61 62 6c 65      pDup->iTable
1d262 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f   = pEList->a[iCo
1d263 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20  l].iAlias;.  }. 
1d264 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67   if( pExpr->flag
1d265 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
1d266 65 20 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e 70  e ){.    pDup->p
1d267 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43  Coll = pExpr->pC
1d268 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e 66  oll;.    pDup->f
1d269 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f  lags |= EP_ExpCo
1d26a 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 20 73 71 6c  llate;.  }.  sql
1d26b 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 62  ite3ExprClear(db
1d26c 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d 63  , pExpr);.  memc
1d26d 70 79 28 70 45 78 70 72 2c 20 70 44 75 70 2c 20  py(pExpr, pDup, 
1d26e 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29 29 3b  sizeof(*pExpr));
1d26f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1d270 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a 2f  (db, pDup);.}../
1d271 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e  *.** Given the n
1d272 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20  ame of a column 
1d273 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 2e 59 2e  of the form X.Y.
1d274 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74  Z or Y.Z or just
1d275 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74   Z, look up.** t
1d276 68 61 74 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  hat name in the 
1d277 73 65 74 20 6f 66 20 73 6f 75 72 63 65 20 74 61  set of source ta
1d278 62 6c 65 73 20 69 6e 20 70 53 72 63 4c 69 73 74  bles in pSrcList
1d279 20 61 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 45   and make the pE
1d27a 78 70 72 20 0a 2a 2a 20 65 78 70 72 65 73 73 69  xpr .** expressi
1d27b 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72 20 62 61  on node refer ba
1d27c 63 6b 20 74 6f 20 74 68 61 74 20 73 6f 75 72 63  ck to that sourc
1d27d 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 66  e column.  The f
1d27e 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73  ollowing changes
1d27f 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20  .** are made to 
1d280 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  pExpr:.**.**    
1d281 70 45 78 70 72 2d 3e 69 44 62 20 20 20 20 20 20  pExpr->iDb      
1d282 20 20 20 20 20 53 65 74 20 74 68 65 20 69 6e 64       Set the ind
1d283 65 78 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20  ex in db->aDb[] 
1d284 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1d285 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  X.**            
1d286 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 76               (ev
1d287 65 6e 20 69 66 20 58 20 69 73 20 69 6d 70 6c 69  en if X is impli
1d288 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72  ed)..**    pExpr
1d289 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20 20  ->iTable        
1d28a 53 65 74 20 74 6f 20 74 68 65 20 63 75 72 73 6f  Set to the curso
1d28b 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  r number for the
1d28c 20 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 0a   table obtained.
1d28d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1d28e 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20             from 
1d28f 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 20  pSrcList..**    
1d290 70 45 78 70 72 2d 3e 70 54 61 62 20 20 20 20 20  pExpr->pTab     
1d291 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74       Points to t
1d292 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
1d293 72 65 20 6f 66 20 58 2e 59 20 28 65 76 65 6e 20  re of X.Y (even 
1d294 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  if.**           
1d295 20 20 20 20 20 20 20 20 20 20 20 20 20 20 58 20                X 
1d296 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 69 6d 70  and/or Y are imp
1d297 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45 78  lied.).**    pEx
1d298 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 20  pr->iColumn     
1d299 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 6f 6c    Set to the col
1d29a 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69  umn number withi
1d29b 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20  n the table..** 
1d29c 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20     pExpr->op    
1d29d 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 54          Set to T
1d29e 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 20  K_COLUMN..**    
1d29f 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 20 20 20  pExpr->pLeft    
1d2a0 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 73       Any express
1d2a1 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20  ion this points 
1d2a2 74 6f 20 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a  to is deleted.**
1d2a3 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68      pExpr->pRigh
1d2a4 74 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70  t        Any exp
1d2a5 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69  ression this poi
1d2a6 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65  nts to is delete
1d2a7 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 44 62  d..**.** The pDb
1d2a8 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 6e 61 6d  Token is the nam
1d2a9 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1d2aa 65 20 28 74 68 65 20 22 58 22 29 2e 20 20 54 68  e (the "X").  Th
1d2ab 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a  is value may be.
1d2ac 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20  ** NULL meaning 
1d2ad 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20  that name is of 
1d2ae 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20  the form Y.Z or 
1d2af 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c  Z.  Any availabl
1d2b0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61  e database.** ca
1d2b1 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68 65 20  n be used.  The 
1d2b2 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20 74  pTableToken is t
1d2b3 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
1d2b4 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 2e 20  able (the "Y"). 
1d2b5 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20 63   This.** value c
1d2b6 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 70 44  an be NULL if pD
1d2b7 62 54 6f 6b 65 6e 20 69 73 20 61 6c 73 6f 20 4e  bToken is also N
1d2b8 55 4c 4c 2e 20 20 49 66 20 70 54 61 62 6c 65 54  ULL.  If pTableT
1d2b9 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 69 74 0a  oken is NULL it.
1d2ba 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ** means that th
1d2bb 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e 61  e form of the na
1d2bc 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68 61 74  me is Z and that
1d2bd 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61 6e   columns from an
1d2be 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20 62  y table.** can b
1d2bf 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  e used..**.** If
1d2c0 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f 74   the name cannot
1d2c1 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75 6e 61   be resolved una
1d2c2 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61 76  mbiguously, leav
1d2c3 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
1d2c4 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 20  ge.** in pParse 
1d2c5 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a  and return non-z
1d2c6 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 72  ero.  Return zer
1d2c7 6f 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f  o on success..*/
1d2c8 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b  .static int look
1d2c9 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20  upName(.  Parse 
1d2ca 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
1d2cb 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
1d2cc 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ntext */.  Token
1d2cd 20 2a 70 44 62 54 6f 6b 65 6e 2c 20 20 20 20 20   *pDbToken,     
1d2ce 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
1d2cf 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
1d2d0 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c  ng table, or NUL
1d2d1 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  L */.  Token *pT
1d2d2 61 62 6c 65 54 6f 6b 65 6e 2c 20 20 2f 2a 20 4e  ableToken,  /* N
1d2d3 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e  ame of table con
1d2d4 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20  taining column, 
1d2d5 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b  or NULL */.  Tok
1d2d6 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e  en *pColumnToken
1d2d7 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65  , /* Name of the
1d2d8 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61   column. */.  Na
1d2d9 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20  meContext *pNC, 
1d2da 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63     /* The name c
1d2db 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20 72  ontext used to r
1d2dc 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20  esolve the name 
1d2dd 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
1d2de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b            /* Mak
1d2df 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65  e this EXPR node
1d2e0 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65   point to the se
1d2e1 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f  lected column */
1d2e2 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 62 20  .){.  char *zDb 
1d2e3 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61  = 0;       /* Na
1d2e4 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1d2e5 73 65 2e 20 20 54 68 65 20 22 58 22 20 69 6e 20  se.  The "X" in 
1d2e6 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72 20  X.Y.Z */.  char 
1d2e7 2a 7a 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20  *zTab = 0;      
1d2e8 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
1d2e9 61 62 6c 65 2e 20 20 54 68 65 20 22 59 22 20 69  able.  The "Y" i
1d2ea 6e 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 2a  n X.Y.Z or Y.Z *
1d2eb 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d  /.  char *zCol =
1d2ec 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65   0;      /* Name
1d2ed 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20   of the column. 
1d2ee 20 54 68 65 20 22 5a 22 20 2a 2f 0a 20 20 69 6e   The "Z" */.  in
1d2ef 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
1d2f0 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1d2f1 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  ers */.  int cnt
1d2f2 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1d2f3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1d2f4 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67  mber of matching
1d2f5 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f   column names */
1d2f6 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d 20  .  int cntTab = 
1d2f7 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
1d2f8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d2f9 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65  f matching table
1d2fa 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c 69   names */.  sqli
1d2fb 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1d2fc 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a  ->db;         /*
1d2fd 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
1d2fe 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74  nnection */.  st
1d2ff 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
1d300 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 20  m *pItem;       
1d301 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 69  /* Use for loopi
1d302 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74  ng over pSrcList
1d303 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 75   items */.  stru
1d304 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
1d305 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f 2a  *pMatch = 0;  /*
1d306 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70 53   The matching pS
1d307 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a 20  rcList item */. 
1d308 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 54   NameContext *pT
1d309 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 20  opNC = pNC;     
1d30a 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d 65     /* First name
1d30b 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 6c  context in the l
1d30c 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20  ist */.  Schema 
1d30d 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 20  *pSchema = 0;   
1d30e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63             /* Sc
1d30f 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72  hema of the expr
1d310 65 73 73 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73  ession */..  ass
1d311 65 72 74 28 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65  ert( pColumnToke
1d312 6e 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65  n && pColumnToke
1d313 6e 2d 3e 7a 20 29 3b 20 2f 2a 20 54 68 65 20 5a  n->z ); /* The Z
1d314 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74   in X.Y.Z cannot
1d315 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20 2f   be NULL */..  /
1d316 2a 20 44 65 71 75 6f 74 65 20 61 6e 64 20 7a 65  * Dequote and ze
1d317 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20 74 68 65  ro-terminate the
1d318 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 7a 44 62 20   names */.  zDb 
1d319 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
1d31a 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 62 54 6f  mToken(db, pDbTo
1d31b 6b 65 6e 29 3b 0a 20 20 7a 54 61 62 20 3d 20 73  ken);.  zTab = s
1d31c 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1d31d 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 54 6f  ken(db, pTableTo
1d31e 6b 65 6e 29 3b 0a 20 20 7a 43 6f 6c 20 3d 20 73  ken);.  zCol = s
1d31f 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1d320 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 54  ken(db, pColumnT
1d321 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 64 62 2d  oken);.  if( db-
1d322 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
1d323 0a 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70  .    goto lookup
1d324 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20  name_end;.  }.. 
1d325 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
1d326 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61  he node to no-ma
1d327 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e  tch */.  pExpr->
1d328 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70  iTable = -1;.  p
1d329 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a  Expr->pTab = 0;.
1d32a 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20 74  .  /* Start at t
1d32b 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f  he inner-most co
1d32c 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 6f  ntext and move o
1d32d 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20 6d  utward until a m
1d32e 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a 2f  atch is found */
1d32f 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26  .  while( pNC &&
1d330 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 45   cnt==0 ){.    E
1d331 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
1d332 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53  .    SrcList *pS
1d333 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  rcList = pNC->pS
1d334 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 28  rcList;..    if(
1d335 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20   pSrcList ){.   
1d336 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
1d337 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 69  m=pSrcList->a; i
1d338 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pSrcList->nSrc;
1d339 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
1d33a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70          Table *p
1d33b 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  Tab;.        int
1d33c 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43 6f   iDb;.        Co
1d33d 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20  lumn *pCol;.  . 
1d33e 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 49         pTab = pI
1d33f 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20  tem->pTab;.     
1d340 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21     assert( pTab!
1d341 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 6d  =0 && pTab->zNam
1d342 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  e!=0 );.        
1d343 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1d344 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
1d345 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
1d346 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1d347 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20  Tab->nCol>0 );. 
1d348 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 20         if( zTab 
1d349 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
1d34a 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29   pItem->zAlias )
1d34b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  {.            ch
1d34c 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70  ar *zTabName = p
1d34d 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20  Item->zAlias;.  
1d34e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
1d34f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61  lite3StrICmp(zTa
1d350 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20  bName, zTab)!=0 
1d351 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
1d352 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1d353 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
1d354 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e  TabName = pTab->
1d355 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  zName;.         
1d356 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65 3d     if( zTabName=
1d357 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72  =0 || sqlite3Str
1d358 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a  ICmp(zTabName, z
1d359 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e  Tab)!=0 ) contin
1d35a 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
1d35b 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71  if( zDb!=0 && sq
1d35c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d  lite3StrICmp(db-
1d35d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
1d35e 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20   zDb)!=0 ){.    
1d35f 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
1d360 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
1d361 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
1d362 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1d363 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b  if( 0==(cntTab++
1d364 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
1d365 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70  Expr->iTable = p
1d366 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20  Item->iCursor;. 
1d367 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
1d368 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20  pTab = pTab;.   
1d369 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d         pSchema =
1d36a 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
1d36b 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
1d36c 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20   = pItem;.      
1d36d 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28    }.        for(
1d36e 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e  j=0, pCol=pTab->
1d36f 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  aCol; j<pTab->nC
1d370 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; j++, pCol++)
1d371 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
1d372 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
1d373 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  Col->zName, zCol
1d374 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1d375 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69      IdList *pUsi
1d376 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ng;.            
1d377 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  cnt++;.         
1d378 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1d379 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f   = pItem->iCurso
1d37a 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  r;.            p
1d37b 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61  Expr->pTab = pTa
1d37c 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  b;.            p
1d37d 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20  Match = pItem;. 
1d37e 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65             pSche
1d37f 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65  ma = pTab->pSche
1d380 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ma;.            
1d381 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68  /* Substitute th
1d382 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20  e rowid (column 
1d383 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45  -1) for the INTE
1d384 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
1d385 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  */.            p
1d386 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  Expr->iColumn = 
1d387 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f  j==pTab->iPKey ?
1d388 20 2d 31 20 3a 20 6a 3b 0a 20 20 20 20 20 20 20   -1 : j;.       
1d389 20 20 20 20 20 69 66 28 20 69 3c 70 53 72 63 4c       if( i<pSrcL
1d38a 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20  ist->nSrc-1 ){. 
1d38b 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
1d38c 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 79   pItem[1].jointy
1d38d 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20  pe & JT_NATURAL 
1d38e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1d38f 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61     /* If this ma
1d390 74 63 68 20 6f 63 63 75 72 72 65 64 20 69 6e 20  tch occurred in 
1d391 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f  the left table o
1d392 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e  f a natural join
1d393 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1d394 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 74    ** then skip t
1d395 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 74  he right table t
1d396 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63  o avoid a duplic
1d397 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20  ate match */.   
1d398 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74               pIt
1d399 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  em++;.          
1d39a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
1d39b 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
1d39c 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70 49 74  f( (pUsing = pIt
1d39d 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d 30  em[1].pUsing)!=0
1d39e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1d39f 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d      /* If this m
1d3a0 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 61  atch occurs on a
1d3a1 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20   column that is 
1d3a2 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61  in the USING cla
1d3a3 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  use.            
1d3a4 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 6e      ** of a join
1d3a5 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61 72 63  , skip the searc
1d3a6 68 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 74  h of the right t
1d3a7 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e  able of the join
1d3a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1d3a9 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 64   ** to avoid a d
1d3aa 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 74  uplicate match t
1d3ab 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20  here. */.       
1d3ac 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a           int k;.
1d3ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d3ae 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 6e  for(k=0; k<pUsin
1d3af 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 20  g->nId; k++){.  
1d3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d3b1 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1d3b2 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d 2e  mp(pUsing->a[k].
1d3b3 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20  zName, zCol)==0 
1d3b4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1d3b5 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a         pItem++;.
1d3b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d3b7 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20      i++;.       
1d3b8 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
1d3b9 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
1d3ba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1d3bb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1d3bc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1d3bd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1d3be 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1d3bf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1d3c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1d3c1 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1d3c2 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20  MIT_TRIGGER.    
1d3c3 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f  /* If we have no
1d3c4 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f 6c 76  t already resolv
1d3c5 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 65  ed the name, the
1d3c6 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a 20  n maybe .    ** 
1d3c7 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f 72  it is a new.* or
1d3c8 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 61   old.* trigger a
1d3c9 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65 6e 63  rgument referenc
1d3ca 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  e.    */.    if(
1d3cb 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 21   zDb==0 && zTab!
1d3cc 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 20  =0 && cnt==0 && 
1d3cd 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63  pParse->trigStac
1d3ce 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 54 72  k!=0 ){.      Tr
1d3cf 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54 72 69  iggerStack *pTri
1d3d0 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50 61 72  ggerStack = pPar
1d3d1 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a 20  se->trigStack;. 
1d3d2 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
1d3d3 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 33 32 20   = 0;.      u32 
1d3d4 2a 70 69 43 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a  *piColMask = 0;.
1d3d5 20 20 20 20 20 20 69 66 28 20 70 54 72 69 67 67        if( pTrigg
1d3d6 65 72 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20  erStack->newIdx 
1d3d7 21 3d 20 2d 31 20 26 26 20 73 71 6c 69 74 65 33  != -1 && sqlite3
1d3d8 53 74 72 49 43 6d 70 28 22 6e 65 77 22 2c 20 7a  StrICmp("new", z
1d3d9 54 61 62 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20  Tab) == 0 ){.   
1d3da 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
1d3db 6c 65 20 3d 20 70 54 72 69 67 67 65 72 53 74 61  le = pTriggerSta
1d3dc 63 6b 2d 3e 6e 65 77 49 64 78 3b 0a 20 20 20 20  ck->newIdx;.    
1d3dd 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69      assert( pTri
1d3de 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 20  ggerStack->pTab 
1d3df 29 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20  );.        pTab 
1d3e0 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  = pTriggerStack-
1d3e1 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 70  >pTab;.        p
1d3e2 69 43 6f 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72  iColMask = &(pTr
1d3e3 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e 65 77 43  iggerStack->newC
1d3e4 6f 6c 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d  olMask);.      }
1d3e5 65 6c 73 65 20 69 66 28 20 70 54 72 69 67 67 65  else if( pTrigge
1d3e6 72 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 21  rStack->oldIdx !
1d3e7 3d 20 2d 31 20 26 26 20 73 71 6c 69 74 65 33 53  = -1 && sqlite3S
1d3e8 74 72 49 43 6d 70 28 22 6f 6c 64 22 2c 20 7a 54  trICmp("old", zT
1d3e9 61 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ab)==0 ){.      
1d3ea 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1d3eb 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  = pTriggerStack-
1d3ec 3e 6f 6c 64 49 64 78 3b 0a 20 20 20 20 20 20 20  >oldIdx;.       
1d3ed 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65   assert( pTrigge
1d3ee 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a  rStack->pTab );.
1d3ef 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
1d3f0 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54  TriggerStack->pT
1d3f1 61 62 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f  ab;.        piCo
1d3f2 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67  lMask = &(pTrigg
1d3f3 65 72 53 74 61 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d  erStack->oldColM
1d3f4 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  ask);.      }.. 
1d3f5 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b       if( pTab ){
1d3f6 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43   .        int iC
1d3f7 6f 6c 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75  ol;.        Colu
1d3f8 6d 6e 20 2a 70 43 6f 6c 20 3d 20 70 54 61 62 2d  mn *pCol = pTab-
1d3f9 3e 61 43 6f 6c 3b 0a 0a 20 20 20 20 20 20 20 20  >aCol;..        
1d3fa 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e  pSchema = pTab->
1d3fb 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20  pSchema;.       
1d3fc 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20   cntTab++;.     
1d3fd 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
1d3fe 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c  Col < pTab->nCol
1d3ff 3b 20 69 43 6f 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b  ; iCol++, pCol++
1d400 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ) {.          if
1d401 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1d402 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43  (pCol->zName, zC
1d403 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
1d404 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
1d405 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
1d406 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3d 3d  iColumn = iCol==
1d407 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31  pTab->iPKey ? -1
1d408 20 3a 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20   : iCol;.       
1d409 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62       pExpr->pTab
1d40a 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20   = pTab;.       
1d40b 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30       if( iCol>=0
1d40c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1d40d 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c    testcase( iCol
1d40e 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20  ==31 );.        
1d40f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1d410 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20  iCol==32 );.    
1d411 20 20 20 20 20 20 20 20 20 20 2a 70 69 43 6f 6c            *piCol
1d412 4d 61 73 6b 20 7c 3d 20 28 28 75 33 32 29 31 3c  Mask |= ((u32)1<
1d413 3c 69 43 6f 6c 29 20 7c 20 28 69 43 6f 6c 3e 3d  <iCol) | (iCol>=
1d414 33 32 3f 30 78 66 66 66 66 66 66 66 66 3a 30 29  32?0xffffffff:0)
1d415 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
1d416 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
1d417 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
1d418 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1d419 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
1d41a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1d41b 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a  _OMIT_TRIGGER) *
1d41c 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  /..    /*.    **
1d41d 20 50 65 72 68 61 70 73 20 74 68 65 20 6e 61 6d   Perhaps the nam
1d41e 65 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65  e is a reference
1d41f 20 74 6f 20 74 68 65 20 52 4f 57 49 44 0a 20 20   to the ROWID.  
1d420 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74    */.    if( cnt
1d421 3d 3d 30 20 26 26 20 63 6e 74 54 61 62 3d 3d 31  ==0 && cntTab==1
1d422 20 26 26 20 73 71 6c 69 74 65 33 49 73 52 6f 77   && sqlite3IsRow
1d423 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20  id(zCol) ){.    
1d424 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20    cnt = 1;.     
1d425 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
1d426 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70  = -1;.      pExp
1d427 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51  r->affinity = SQ
1d428 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
1d429 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a  ;.    }..    /*.
1d42a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 69 6e      ** If the in
1d42b 70 75 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f  put is of the fo
1d42c 72 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72  rm Z (not Y.Z or
1d42d 20 58 2e 59 2e 5a 29 20 74 68 65 6e 20 74 68 65   X.Y.Z) then the
1d42e 20 6e 61 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d   name Z.    ** m
1d42f 69 67 68 74 20 72 65 66 65 72 20 74 6f 20 61 6e  ight refer to an
1d430 20 72 65 73 75 6c 74 2d 73 65 74 20 61 6c 69 61   result-set alia
1d431 73 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73  s.  This happens
1d432 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77  , for example, w
1d433 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20 61 72  hen.    ** we ar
1d434 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65  e resolving name
1d435 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  s in the WHERE c
1d436 6c 61 75 73 65 20 6f 66 20 74 68 65 20 66 6f 6c  lause of the fol
1d437 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a  lowing command:.
1d438 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
1d439 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20    SELECT a+b AS 
1d43a 78 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45  x FROM table WHE
1d43b 52 45 20 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a  RE x<10;.    **.
1d43c 20 20 20 20 2a 2a 20 49 6e 20 63 61 73 65 73 20      ** In cases 
1d43d 6c 69 6b 65 20 74 68 69 73 2c 20 72 65 70 6c 61  like this, repla
1d43e 63 65 20 70 45 78 70 72 20 77 69 74 68 20 61 20  ce pExpr with a 
1d43f 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 72  copy of the expr
1d440 65 73 73 69 6f 6e 20 74 68 61 74 0a 20 20 20 20  ession that.    
1d441 2a 2a 20 66 6f 72 6d 73 20 74 68 65 20 72 65 73  ** forms the res
1d442 75 6c 74 20 73 65 74 20 65 6e 74 72 79 20 28 22  ult set entry ("
1d443 61 2b 62 22 20 69 6e 20 74 68 65 20 65 78 61 6d  a+b" in the exam
1d444 70 6c 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20  ple) and return 
1d445 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20  immediately..   
1d446 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68   ** Note that th
1d447 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  e expression in 
1d448 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 73  the result set s
1d449 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61  hould have alrea
1d44a 64 79 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72  dy been.    ** r
1d44b 65 73 6f 6c 76 65 64 20 62 79 20 74 68 65 20 74  esolved by the t
1d44c 69 6d 65 20 74 68 65 20 57 48 45 52 45 20 63 6c  ime the WHERE cl
1d44d 61 75 73 65 20 69 73 20 72 65 73 6f 6c 76 65 64  ause is resolved
1d44e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1d44f 20 63 6e 74 3d 3d 30 20 26 26 20 28 70 45 4c 69   cnt==0 && (pELi
1d450 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74  st = pNC->pEList
1d451 29 21 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 20  )!=0 && zTab==0 
1d452 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  ){.      for(j=0
1d453 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; j<pEList->nExp
1d454 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  r; j++){.       
1d455 20 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c   char *zAs = pEL
1d456 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b  ist->a[j].zName;
1d457 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 41 73  .        if( zAs
1d458 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74  !=0 && sqlite3St
1d459 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29  rICmp(zAs, zCol)
1d45a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1d45b 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20   Expr *pOrig;.  
1d45c 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1d45d 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20  pExpr->pLeft==0 
1d45e 26 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  && pExpr->pRight
1d45f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ==0 );.         
1d460 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1d461 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20  pList==0 );.    
1d462 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
1d463 78 70 72 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  xpr->pSelect==0 
1d464 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72  );.          pOr
1d465 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a  ig = pEList->a[j
1d466 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ].pExpr;.       
1d467 20 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c     if( !pNC->all
1d468 6f 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73  owAgg && ExprHas
1d469 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20  Property(pOrig, 
1d46a 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20  EP_Agg) ){.     
1d46b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
1d46c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1d46d 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65  misuse of aliase
1d46e 64 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c  d aggregate %s",
1d46f 20 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20   zAs);.         
1d470 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1d471 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  (db, zCol);.    
1d472 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
1d473 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1d474 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41          resolveA
1d475 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c  lias(pParse, pEL
1d476 69 73 74 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22  ist, j, pExpr, "
1d477 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e  ");.          cn
1d478 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  t = 1;.         
1d479 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20   pMatch = 0;.   
1d47a 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a         assert( z
1d47b 54 61 62 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30  Tab==0 && zDb==0
1d47c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   );.          go
1d47d 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e  to lookupname_en
1d47e 64 5f 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  d_2;.        }. 
1d47f 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20       } .    }.. 
1d480 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f     /* Advance to
1d481 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63   the next name c
1d482 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f  ontext.  The loo
1d483 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e  p will exit when
1d484 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 77   either.    ** w
1d485 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 28  e have a match (
1d486 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77  cnt>0) or when w
1d487 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d  e run out of nam
1d488 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20  e contexts..    
1d489 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d  */.    if( cnt==
1d48a 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d  0 ){.      pNC =
1d48b 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   pNC->pNext;.   
1d48c 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a   }.  }..  /*.  *
1d48d 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61 72 65  * If X and Y are
1d48e 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20   NULL (in other 
1d48f 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 68  words if only th
1d490 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20  e column name Z 
1d491 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 64  is.  ** supplied
1d492 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20  ) and the value 
1d493 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64  of Z is enclosed
1d494 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65   in double-quote
1d495 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69  s, then.  ** Z i
1d496 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72  s a string liter
1d497 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74  al if it doesn't
1d498 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d   match any colum
1d499 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 61  n names.  In tha
1d49a 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 20  t.  ** case, we 
1d49b 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 72  need to return r
1d49c 69 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e 6f  ight away and no
1d49d 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67  t make any chang
1d49e 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72  es to.  ** pExpr
1d49f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61  ..  **.  ** Beca
1d4a0 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65  use no reference
1d4a1 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 74   was made to out
1d4a2 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 65  er contexts, the
1d4a3 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20   pNC->nRef.  ** 
1d4a4 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 63  fields are not c
1d4a5 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 6f  hanged in any co
1d4a6 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  ntext..  */.  if
1d4a7 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62  ( cnt==0 && zTab
1d4a8 3d 3d 30 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f  ==0 && pColumnTo
1d4a9 6b 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29  ken->z[0]=='"' )
1d4aa 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
1d4ab 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20  ree(db, zCol);. 
1d4ac 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
1d4ad 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45  K_STRING;.    pE
1d4ae 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20  xpr->pTab = 0;. 
1d4af 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1d4b0 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d  ..  /*.  ** cnt=
1d4b1 3d 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77  =0 means there w
1d4b2 61 73 20 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63  as not match.  c
1d4b3 6e 74 3e 31 20 6d 65 61 6e 73 20 74 68 65 72 65  nt>1 means there
1d4b4 20 77 65 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a   were two or.  *
1d4b5 2a 20 6d 6f 72 65 20 6d 61 74 63 68 65 73 2e 20  * more matches. 
1d4b6 20 45 69 74 68 65 72 20 77 61 79 2c 20 77 65 20   Either way, we 
1d4b7 68 61 76 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20  have an error.. 
1d4b8 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 21 3d 31   */.  if( cnt!=1
1d4b9 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   ){.    const ch
1d4ba 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45  ar *zErr;.    zE
1d4bb 72 72 20 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e  rr = cnt==0 ? "n
1d4bc 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a  o such column" :
1d4bd 20 22 61 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75   "ambiguous colu
1d4be 6d 6e 20 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66  mn name";.    if
1d4bf 28 20 7a 44 62 20 29 7b 0a 20 20 20 20 20 20 73  ( zDb ){.      s
1d4c0 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1d4c1 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25  Parse, "%s: %s.%
1d4c2 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62  s.%s", zErr, zDb
1d4c3 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20  , zTab, zCol);. 
1d4c4 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 54 61     }else if( zTa
1d4c5 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  b ){.      sqlit
1d4c6 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1d4c7 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20  e, "%s: %s.%s", 
1d4c8 7a 45 72 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c  zErr, zTab, zCol
1d4c9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1d4ca 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1d4cb 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a  Msg(pParse, "%s:
1d4cc 20 25 73 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c   %s", zErr, zCol
1d4cd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 6f  );.    }.    pTo
1d4ce 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d  pNC->nErr++;.  }
1d4cf 0a 0a 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75  ..  /* If a colu
1d4d0 6d 6e 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20  mn from a table 
1d4d1 69 6e 20 70 53 72 63 4c 69 73 74 20 69 73 20 72  in pSrcList is r
1d4d2 65 66 65 72 65 6e 63 65 64 2c 20 74 68 65 6e 20  eferenced, then 
1d4d3 72 65 63 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73  record.  ** this
1d4d4 20 66 61 63 74 20 69 6e 20 74 68 65 20 70 53 72   fact in the pSr
1d4d5 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65  cList.a[].colUse
1d4d6 64 20 62 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75  d bitmask.  Colu
1d4d7 6d 6e 20 30 20 63 61 75 73 65 73 0a 20 20 2a 2a  mn 0 causes.  **
1d4d8 20 62 69 74 20 30 20 74 6f 20 62 65 20 73 65 74   bit 0 to be set
1d4d9 2e 20 20 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73  .  Column 1 sets
1d4da 20 62 69 74 20 31 2e 20 20 41 6e 64 20 73 6f 20   bit 1.  And so 
1d4db 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20  forth.  If the. 
1d4dc 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65   ** column numbe
1d4dd 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  r is greater tha
1d4de 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1d4df 62 69 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d  bits in the bitm
1d4e0 61 73 6b 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65  ask.  ** then se
1d4e1 74 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72  t the high-order
1d4e2 20 62 69 74 20 6f 66 20 74 68 65 20 62 69 74 6d   bit of the bitm
1d4e3 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ask..  */.  if( 
1d4e4 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d  pExpr->iColumn>=
1d4e5 30 20 26 26 20 70 4d 61 74 63 68 21 3d 30 20 29  0 && pMatch!=0 )
1d4e6 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 45  {.    int n = pE
1d4e7 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  xpr->iColumn;.  
1d4e8 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 42    testcase( n==B
1d4e9 4d 53 2d 31 20 29 3b 0a 20 20 20 20 69 66 28 20  MS-1 );.    if( 
1d4ea 6e 3e 3d 42 4d 53 20 29 7b 0a 20 20 20 20 20 20  n>=BMS ){.      
1d4eb 6e 20 3d 20 42 4d 53 2d 31 3b 0a 20 20 20 20 7d  n = BMS-1;.    }
1d4ec 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 61  .    assert( pMa
1d4ed 74 63 68 2d 3e 69 43 75 72 73 6f 72 3d 3d 70 45  tch->iCursor==pE
1d4ee 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20  xpr->iTable );. 
1d4ef 20 20 20 70 4d 61 74 63 68 2d 3e 63 6f 6c 55 73     pMatch->colUs
1d4f0 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73 6b 29  ed |= ((Bitmask)
1d4f1 31 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a 6c 6f 6f 6b  1)<<n;.  }..look
1d4f2 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20 2f 2a  upname_end:.  /*
1d4f3 20 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 72 65   Clean up and re
1d4f4 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  turn.  */.  sqli
1d4f5 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 44  te3DbFree(db, zD
1d4f6 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  b);.  sqlite3DbF
1d4f7 72 65 65 28 64 62 2c 20 7a 54 61 62 29 3b 0a 20  ree(db, zTab);. 
1d4f8 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
1d4f9 74 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c  te(db, pExpr->pL
1d4fa 65 66 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70  eft);.  pExpr->p
1d4fb 4c 65 66 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69  Left = 0;.  sqli
1d4fc 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1d4fd 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
1d4fe 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ;.  pExpr->pRigh
1d4ff 74 20 3d 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e  t = 0;.  pExpr->
1d500 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a  op = TK_COLUMN;.
1d501 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32  lookupname_end_2
1d502 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  :.  sqlite3DbFre
1d503 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 69  e(db, zCol);.  i
1d504 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20  f( cnt==1 ){.   
1d505 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20   assert( pNC!=0 
1d506 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  );.    sqlite3Au
1d507 74 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 70  thRead(pParse, p
1d508 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 70  Expr, pSchema, p
1d509 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20  NC->pSrcList);. 
1d50a 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20     /* Increment 
1d50b 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 6f  the nRef value o
1d50c 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65  n all name conte
1d50d 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75  xts from TopNC u
1d50e 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  p to.    ** the 
1d50f 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65 20  point where the 
1d510 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f  name matched. */
1d511 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20  .    for(;;){.  
1d512 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 70      assert( pTop
1d513 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  NC!=0 );.      p
1d514 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  TopNC->nRef++;. 
1d515 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43 3d       if( pTopNC=
1d516 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 20  =pNC ) break;.  
1d517 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f      pTopNC = pTo
1d518 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pNC->pNext;.    
1d519 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  }.    return 0;.
1d51a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72    } else {.    r
1d51b 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a  eturn 1;.  }.}..
1d51c 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1d51d 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20 66  ne is callback f
1d51e 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  or sqlite3WalkEx
1d51f 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f  pr()..**.** Reso
1d520 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d  lve symbolic nam
1d521 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d  es into TK_COLUM
1d522 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72 20  N operators for 
1d523 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 6e  the current.** n
1d524 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 65  ode in the expre
1d525 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74  ssion tree.  Ret
1d526 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75  urn 0 to continu
1d527 65 20 74 68 65 20 73 65 61 72 63 68 20 64 6f 77  e the search dow
1d528 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f 72  n.** the tree or
1d529 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20   2 to abort the 
1d52a 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a  tree walk..**.**
1d52b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   This routine al
1d52c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63 68  so does error ch
1d52d 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65 20  ecking and name 
1d52e 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a  resolution for.*
1d52f 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73  * function names
1d530 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20  .  The operator 
1d531 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75  for aggregate fu
1d532 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e 67  nctions is chang
1d533 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f  ed.** to TK_AGG_
1d534 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61  FUNCTION..*/.sta
1d535 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 45  tic int resolveE
1d536 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 2a  xprStep(Walker *
1d537 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70  pWalker, Expr *p
1d538 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e  Expr){.  NameCon
1d539 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61 72  text *pNC;.  Par
1d53a 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 70  se *pParse;..  p
1d53b 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e  NC = pWalker->u.
1d53c 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 70  pNC;.  assert( p
1d53d 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 73  NC!=0 );.  pPars
1d53e 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b  e = pNC->pParse;
1d53f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
1d540 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72  e==pWalker->pPar
1d541 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78 70  se );..  if( Exp
1d542 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
1d543 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76  pExpr, EP_Resolv
1d544 65 64 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  ed) ) return WRC
1d545 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53 65  _Prune;.  ExprSe
1d546 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  tProperty(pExpr,
1d547 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23   EP_Resolved);.#
1d548 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
1d549 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  if( pNC->pSrcLis
1d54a 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69  t && pNC->pSrcLi
1d54b 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a  st->nAlloc>0 ){.
1d54c 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72      SrcList *pSr
1d54d 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  cList = pNC->pSr
1d54e 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69  cList;.    int i
1d54f 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1d550 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e  <pNC->pSrcList->
1d551 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSrc; i++){.    
1d552 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69    assert( pSrcLi
1d553 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72  st->a[i].iCursor
1d554 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 2d  >=0 && pSrcList-
1d555 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50  >a[i].iCursor<pP
1d556 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20  arse->nTab);.   
1d557 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
1d558 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f  switch( pExpr->o
1d559 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65  p ){..#if define
1d55a 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
1d55b 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49  UPDATE_DELETE_LI
1d55c 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64  MIT) && !defined
1d55d 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
1d55e 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54 68  QUERY).    /* Th
1d55f 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74  e special operat
1d560 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 20  or TK_ROW means 
1d561 75 73 65 20 74 68 65 20 72 6f 77 69 64 20 66 6f  use the rowid fo
1d562 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20  r the first.    
1d563 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  ** column in the
1d564 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54   FROM clause.  T
1d565 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74  his is used by t
1d566 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52 44  he LIMIT and ORD
1d567 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61  ER BY.    ** cla
1d568 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f  use processing o
1d569 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44 45 4c  n UPDATE and DEL
1d56a 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ETE statements..
1d56b 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
1d56c 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20 20  TK_ROW: {.      
1d56d 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73  SrcList *pSrcLis
1d56e 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  t = pNC->pSrcLis
1d56f 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  t;.      struct 
1d570 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
1d571 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  tem;.      asser
1d572 74 28 20 70 53 72 63 4c 69 73 74 20 26 26 20 70  t( pSrcList && p
1d573 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  SrcList->nSrc==1
1d574 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 20   );.      pItem 
1d575 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0a  = pSrcList->a; .
1d576 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20        pExpr->op 
1d577 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20  = TK_COLUMN;.   
1d578 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d     pExpr->pTab =
1d579 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20   pItem->pTab;.  
1d57a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
1d57b 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73  e = pItem->iCurs
1d57c 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  or;.      pExpr-
1d57d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20  >iColumn = -1;. 
1d57e 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69       pExpr->affi
1d57f 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46  nity = SQLITE_AF
1d580 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20  F_INTEGER;.     
1d581 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
1d582 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28  ndif /* defined(
1d583 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50  SQLITE_ENABLE_UP
1d584 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49  DATE_DELETE_LIMI
1d585 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  T) && !defined(S
1d586 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1d587 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  ERY) */..    /* 
1d588 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69 65  A lone identifie
1d589 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  r is the name of
1d58a 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a   a column..    *
1d58b 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44  /.    case TK_ID
1d58c 3a 20 7b 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70  : {.      lookup
1d58d 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 30 2c 20  Name(pParse, 0, 
1d58e 30 2c 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  0, &pExpr->token
1d58f 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20  , pNC, pExpr);. 
1d590 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
1d591 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 0a  Prune;.    }.  .
1d592 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e      /* A table n
1d593 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e  ame and column n
1d594 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 0a 20  ame:     ID.ID. 
1d595 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 61 62     ** Or a datab
1d596 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20 63  ase, table and c
1d597 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 44  olumn:  ID.ID.ID
1d598 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
1d599 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 20 20   TK_DOT: {.     
1d59a 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 3b   Token *pColumn;
1d59b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54  .      Token *pT
1d59c 61 62 6c 65 3b 0a 20 20 20 20 20 20 54 6f 6b 65  able;.      Toke
1d59d 6e 20 2a 70 44 62 3b 0a 20 20 20 20 20 20 45 78  n *pDb;.      Ex
1d59e 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20  pr *pRight;..   
1d59f 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c 69     /* if( pSrcLi
1d5a0 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a  st==0 ) break; *
1d5a1 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d  /.      pRight =
1d5a2 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a   pExpr->pRight;.
1d5a3 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74        if( pRight
1d5a4 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20  ->op==TK_ID ){. 
1d5a5 20 20 20 20 20 20 20 70 44 62 20 3d 20 30 3b 0a         pDb = 0;.
1d5a6 20 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d          pTable =
1d5a7 20 26 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e   &pExpr->pLeft->
1d5a8 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70  token;.        p
1d5a9 43 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74  Column = &pRight
1d5aa 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d  ->token;.      }
1d5ab 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
1d5ac 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e 6f 70  sert( pRight->op
1d5ad 3d 3d 54 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20  ==TK_DOT );.    
1d5ae 20 20 20 20 70 44 62 20 3d 20 26 70 45 78 70 72      pDb = &pExpr
1d5af 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a  ->pLeft->token;.
1d5b0 20 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d          pTable =
1d5b1 20 26 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d   &pRight->pLeft-
1d5b2 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  >token;.        
1d5b3 70 43 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68  pColumn = &pRigh
1d5b4 74 2d 3e 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e  t->pRight->token
1d5b5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1d5b6 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 73  lookupName(pPars
1d5b7 65 2c 20 70 44 62 2c 20 70 54 61 62 6c 65 2c 20  e, pDb, pTable, 
1d5b8 70 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70 45  pColumn, pNC, pE
1d5b9 78 70 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75  xpr);.      retu
1d5ba 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
1d5bb 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f    }..    /* Reso
1d5bc 6c 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  lve function nam
1d5bd 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  es.    */.    ca
1d5be 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43  se TK_CONST_FUNC
1d5bf 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55  :.    case TK_FU
1d5c0 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20  NCTION: {.      
1d5c1 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
1d5c2 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 20  = pExpr->pList; 
1d5c3 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d 65     /* The argume
1d5c4 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20  nt list */.     
1d5c5 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 20 3f   int n = pList ?
1d5c6 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20   pList->nExpr : 
1d5c7 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  0;  /* Number of
1d5c8 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
1d5c9 20 20 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f      int no_such_
1d5ca 66 75 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20  func = 0;       
1d5cb 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75  /* True if no su
1d5cc 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73  ch function exis
1d5cd 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ts */.      int 
1d5ce 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d  wrong_num_args =
1d5cf 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20   0;     /* True 
1d5d0 69 66 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20  if wrong number 
1d5d1 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  of arguments */.
1d5d2 20 20 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67        int is_agg
1d5d3 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1d5d4 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20    /* True if is 
1d5d5 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  an aggregate fun
1d5d6 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69  ction */.      i
1d5d7 6e 74 20 61 75 74 68 3b 20 20 20 20 20 20 20 20  nt auth;        
1d5d8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75             /* Au
1d5d9 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75  thorization to u
1d5da 73 65 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  se the function 
1d5db 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64  */.      int nId
1d5dc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1d5dd 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d5de 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  f characters in 
1d5df 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
1d5e0 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
1d5e1 72 20 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20  r *zId;         
1d5e2 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69     /* The functi
1d5e3 6f 6e 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20  on name. */.    
1d5e4 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b    FuncDef *pDef;
1d5e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d5e6 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
1d5e7 75 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ut the function 
1d5e8 2a 2f 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20  */.      u8 enc 
1d5e9 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62  = ENC(pParse->db
1d5ea 29 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  );   /* The data
1d5eb 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f  base encoding */
1d5ec 0a 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 63  ..      zId = (c
1d5ed 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
1d5ee 6e 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d  n.z;.      nId =
1d5ef 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b   pExpr->token.n;
1d5f0 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71  .      pDef = sq
1d5f1 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
1d5f2 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49  n(pParse->db, zI
1d5f3 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20  d, nId, n, enc, 
1d5f4 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44  0);.      if( pD
1d5f5 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ef==0 ){.       
1d5f6 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46   pDef = sqlite3F
1d5f7 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  indFunction(pPar
1d5f8 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64  se->db, zId, nId
1d5f9 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a 20  , -1, enc, 0);. 
1d5fa 20 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d         if( pDef=
1d5fb 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1d5fc 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 31  no_such_func = 1
1d5fd 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
1d5fe 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e 67  .          wrong
1d5ff 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20  _num_args = 1;. 
1d600 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1d601 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 73  else{.        is
1d602 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 75  _agg = pDef->xFu
1d603 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23  nc==0;.      }.#
1d604 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1d605 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
1d606 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 20  .      if( pDef 
1d607 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 20  ){.        auth 
1d608 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  = sqlite3AuthChe
1d609 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
1d60a 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70  E_FUNCTION, 0, p
1d60b 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Def->zName, 0);.
1d60c 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68          if( auth
1d60d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1d60e 20 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74           if( aut
1d60f 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29  h==SQLITE_DENY )
1d610 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  {.            sq
1d611 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1d612 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f  arse, "not autho
1d613 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 6e  rized to use fun
1d614 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20  ction: %s",.    
1d615 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d616 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d617 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pDef->zName);.  
1d618 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e            pNC->n
1d619 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  Err++;.         
1d61a 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 78   }.          pEx
1d61b 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c  pr->op = TK_NULL
1d61c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
1d61d 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
1d61e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1d61f 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28  #endif.      if(
1d620 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43 2d   is_agg && !pNC-
1d621 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20 20  >allowAgg ){.   
1d622 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1d623 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69  rMsg(pParse, "mi
1d624 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74  suse of aggregat
1d625 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28  e function %.*s(
1d626 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 20  )", nId,zId);.  
1d627 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b        pNC->nErr+
1d628 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67  +;.        is_ag
1d629 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  g = 0;.      }el
1d62a 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f 66  se if( no_such_f
1d62b 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73  unc ){.        s
1d62c 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1d62d 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
1d62e 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c  function: %.*s",
1d62f 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20   nId, zId);.    
1d630 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b      pNC->nErr++;
1d631 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1d632 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20   wrong_num_args 
1d633 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1d634 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1d635 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20  e,"wrong number 
1d636 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
1d637 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22  function %.*s()"
1d638 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 6e  ,.             n
1d639 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20  Id, zId);.      
1d63a 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20    pNC->nErr++;. 
1d63b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1d63c 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20 20   is_agg ){.     
1d63d 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
1d63e 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a  K_AGG_FUNCTION;.
1d63f 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61 73          pNC->has
1d640 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  Agg = 1;.      }
1d641 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67  .      if( is_ag
1d642 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67  g ) pNC->allowAg
1d643 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  g = 0;.      sql
1d644 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74  ite3WalkExprList
1d645 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74 29  (pWalker, pList)
1d646 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61  ;.      if( is_a
1d647 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41  gg ) pNC->allowA
1d648 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a  gg = 1;.      /*
1d649 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75 74   FIX ME:  Comput
1d64a 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  e pExpr->affinit
1d64b 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 65  y based on the e
1d64c 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a 20  xpected return. 
1d64d 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66 20       ** type of 
1d64e 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20  the function .  
1d64f 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74      */.      ret
1d650 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
1d651 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
1d652 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
1d653 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45  Y.    case TK_SE
1d654 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54  LECT:.    case T
1d655 4b 5f 45 58 49 53 54 53 3a 0a 23 65 6e 64 69 66  K_EXISTS:.#endif
1d656 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a  .    case TK_IN:
1d657 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78   {.      if( pEx
1d658 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  pr->pSelect ){. 
1d659 20 20 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20         int nRef 
1d65a 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66  = pNC->nRef;.#if
1d65b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d65c 5f 43 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69  _CHECK.        i
1d65d 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20  f( pNC->isCheck 
1d65e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1d65f 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1d660 72 73 65 2c 22 73 75 62 71 75 65 72 69 65 73 20  rse,"subqueries 
1d661 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48  prohibited in CH
1d662 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22  ECK constraints"
1d663 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  );.        }.#en
1d664 64 69 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69  dif.        sqli
1d665 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57  te3WalkSelect(pW
1d666 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 53  alker, pExpr->pS
1d667 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20  elect);.        
1d668 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65  assert( pNC->nRe
1d669 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20  f>=nRef );.     
1d66a 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e 43     if( nRef!=pNC
1d66b 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20  ->nRef ){.      
1d66c 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65      ExprSetPrope
1d66d 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61  rty(pExpr, EP_Va
1d66e 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  rSelect);.      
1d66f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1d670 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1d671 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1d672 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61 73  IT_CHECK.    cas
1d673 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b  e TK_VARIABLE: {
1d674 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e  .      if( pNC->
1d675 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20  isCheck ){.     
1d676 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1d677 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 61 6d  sg(pParse,"param
1d678 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 65 64  eters prohibited
1d679 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72   in CHECK constr
1d67a 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d  aints");.      }
1d67b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1d67c 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
1d67d 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d   return (pParse-
1d67e 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d  >nErr || pParse-
1d67f 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1d680 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a  d) ? WRC_Abort :
1d681 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
1d682 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69  ../*.** pEList i
1d683 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70 72  s a list of expr
1d684 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 72  essions which ar
1d685 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65 73  e really the res
1d686 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a 2a  ult set of the.*
1d687 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  * a SELECT state
1d688 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20 74  ment.  pE is a t
1d689 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20  erm in an ORDER 
1d68a 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63  BY or GROUP BY c
1d68b 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72  lause..** This r
1d68c 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 6f  outine checks to
1d68d 20 73 65 65 20 69 66 20 70 45 20 69 73 20 61 20   see if pE is a 
1d68e 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65  simple identifie
1d68f 72 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f  r which correspo
1d690 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53  nds.** to the AS
1d691 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20  -name of one of 
1d692 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
1d693 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1d694 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20  .  If it is,.** 
1d695 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
1d696 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 62  urn an integer b
1d697 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 77  etween 1 and N w
1d698 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75  here N is the nu
1d699 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65  mber of.** eleme
1d69a 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 63  nts in pEList, c
1d69b 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
1d69c 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74  the matching ent
1d69d 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73  ry.  If there is
1d69e 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72  .** no match, or
1d69f 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61 20   if pE is not a 
1d6a0 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65  simple identifie
1d6a1 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  r, then this rou
1d6a2 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30  tine.** return 0
1d6a3 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68  ..**.** pEList h
1d6a4 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64  as been resolved
1d6a5 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a  .  pE has not..*
1d6a6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
1d6a7 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50 61  olveAsName(.  Pa
1d6a8 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1d6a9 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
1d6aa 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65  ext for error me
1d6ab 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70 72  ssages */.  Expr
1d6ac 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f  List *pEList,  /
1d6ad 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65 73  * List of expres
1d6ae 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f  sions to scan */
1d6af 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20  .  Expr *pE     
1d6b0 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
1d6b1 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69 6e  ion we are tryin
1d6b2 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b  g to match */.){
1d6b3 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
1d6b4 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
1d6b5 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20  unter */..  if( 
1d6b6 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 7c 7c  pE->op==TK_ID ||
1d6b7 20 28 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52   (pE->op==TK_STR
1d6b8 49 4e 47 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e  ING && pE->token
1d6b9 2e 7a 5b 30 5d 21 3d 27 5c 27 27 29 20 29 7b 0a  .z[0]!='\'') ){.
1d6ba 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
1d6bb 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1d6bc 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 73    char *zCol = s
1d6bd 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1d6be 6b 65 6e 28 64 62 2c 20 26 70 45 2d 3e 74 6f 6b  ken(db, &pE->tok
1d6bf 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f  en);.    if( zCo
1d6c0 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  l==0 ){.      re
1d6c1 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  turn -1;.    }. 
1d6c2 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45     for(i=0; i<pE
1d6c3 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
1d6c4 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
1d6c5 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69  As = pEList->a[i
1d6c6 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69  ].zName;.      i
1d6c7 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c  f( zAs!=0 && sql
1d6c8 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c  ite3StrICmp(zAs,
1d6c9 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20   zCol)==0 ){.   
1d6ca 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1d6cb 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20  ee(db, zCol);.  
1d6cc 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31        return i+1
1d6cd 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1d6ce 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1d6cf 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d  e(db, zCol);.  }
1d6d0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1d6d1 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70 6f  /*.** pE is a po
1d6d2 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72  inter to an expr
1d6d3 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73 20  ession which is 
1d6d4 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e  a single term in
1d6d5 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59   the.** ORDER BY
1d6d6 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53   of a compound S
1d6d7 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70 72  ELECT.  The expr
1d6d8 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62  ession has not b
1d6d9 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f  een.** name reso
1d6da 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74  lved..**.** At t
1d6db 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f  he point this ro
1d6dc 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
1d6dd 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77   we already know
1d6de 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44   that the.** ORD
1d6df 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e 6f  ER BY term is no
1d6e0 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 64  t an integer ind
1d6e1 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73 75  ex into the resu
1d6e2 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a  lt set.  That.**
1d6e3 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64   case is handled
1d6e4 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   by the calling 
1d6e5 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41  routine..**.** A
1d6e6 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68 20  ttempt to match 
1d6e7 70 45 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c  pE against resul
1d6e8 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e  t set columns in
1d6e9 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a   the left-most.*
1d6ea 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  * SELECT stateme
1d6eb 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  nt.  Return the 
1d6ec 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20 6d  index i of the m
1d6ed 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a  atching column,.
1d6ee 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61 74  ** as an indicat
1d6ef 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ion to the calle
1d6f0 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64  r that it should
1d6f1 20 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d 74   sort by the i-t
1d6f2 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65  h column..** The
1d6f3 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d   left-most colum
1d6f4 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68 65  n is 1.  In othe
1d6f5 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61 6c  r words, the val
1d6f6 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  ue returned is t
1d6f7 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67  he.** same integ
1d6f8 65 72 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f  er value that wo
1d6f9 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74  uld be used in t
1d6fa 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1d6fb 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20   to indicate.** 
1d6fc 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  the column..**.*
1d6fd 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
1d6fe 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30   match, return 0
1d6ff 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20  .  Return -1 if 
1d700 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  an error occurs.
1d701 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
1d702 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72  esolveOrderByTer
1d703 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 50  mToExprList(.  P
1d704 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
1d705 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
1d706 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d  text for error m
1d707 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c  essages */.  Sel
1d708 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
1d709 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
1d70a 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65  atement with the
1d70b 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
1d70c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20   */.  Expr *pE  
1d70d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1d70e 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42  specific ORDER B
1d70f 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69  Y term */.){.  i
1d710 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1d711 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1d712 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  r */.  ExprList 
1d713 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65  *pEList;  /* The
1d714 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
1d715 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
1d716 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20  NameContext nc; 
1d717 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65     /* Name conte
1d718 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67  xt for resolving
1d719 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74   pE */..  assert
1d71a 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  ( sqlite3ExprIsI
1d71b 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d 3d  nteger(pE, &i)==
1d71c 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20  0 );.  pEList = 
1d71d 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
1d71e 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61  ..  /* Resolve a
1d71f 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20  ll names in the 
1d720 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 78  ORDER BY term ex
1d721 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20  pression.  */.  
1d722 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20 73  memset(&nc, 0, s
1d723 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63  izeof(nc));.  nc
1d724 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
1d725 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74 20  ;.  nc.pSrcList 
1d726 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b  = pSelect->pSrc;
1d727 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70  .  nc.pEList = p
1d728 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f  EList;.  nc.allo
1d729 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e  wAgg = 1;.  nc.n
1d72a 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  Err = 0;.  if( s
1d72b 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
1d72c 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29 20  rNames(&nc, pE) 
1d72d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1d72e 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29  rorClear(pParse)
1d72f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
1d730 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f    }..  /* Try to
1d731 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 52   match the ORDER
1d732 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 61   BY expression a
1d733 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73  gainst an expres
1d734 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65  sion.  ** in the
1d735 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 65   result set.  Re
1d736 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 20  turn an 1-based 
1d737 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74  index of the mat
1d738 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c  ching.  ** resul
1d739 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a  t-set entry..  *
1d73a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
1d73b 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  EList->nExpr; i+
1d73c 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  +){.    if( sqli
1d73d 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
1d73e 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  EList->a[i].pExp
1d73f 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20  r, pE) ){.      
1d740 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20  return i+1;.    
1d741 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e  }.  }..  /* If n
1d742 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20  o match, return 
1d743 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30  0. */.  return 0
1d744 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
1d745 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ate an ORDER BY 
1d746 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d  or GROUP BY term
1d747 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72   out-of-range er
1d748 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ror..*/.static v
1d749 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66  oid resolveOutOf
1d74a 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50 61  RangeError(.  Pa
1d74b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1d74c 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72 6f       /* The erro
1d74d 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77  r context into w
1d74e 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68  hich to write th
1d74f 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e  e error */.  con
1d750 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20  st char *zType, 
1d751 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f      /* "ORDER" o
1d752 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69  r "GROUP" */.  i
1d753 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20  nt i,           
1d754 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
1d755 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66 20  ex (1-based) of 
1d756 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66 20  the term out of 
1d757 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d  range */.  int m
1d758 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
1d759 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65 72    /* Largest per
1d75a 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20 6f  missible value o
1d75b 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  f i */.){.  sqli
1d75c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1d75d 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73 20  se, .    "%r %s 
1d75e 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72  BY term out of r
1d75f 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65  ange - should be
1d760 20 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e 20   ".    "between 
1d761 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54  1 and %d", i, zT
1d762 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a  ype, mx);.}../*.
1d763 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f  ** Analyze the O
1d764 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69  RDER BY clause i
1d765 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  n a compound SEL
1d766 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
1d767 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68 20   Modify.** each 
1d768 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45  term of the ORDE
1d769 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61  R BY clause is a
1d76a 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65   constant intege
1d76b 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61  r between 1.** a
1d76c 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20  nd N where N is 
1d76d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
1d76e 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d  lumns in the com
1d76f 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a  pound SELECT..**
1d770 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72  .** ORDER BY ter
1d771 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c 72 65  ms that are alre
1d772 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20 62  ady an integer b
1d773 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 61  etween 1 and N a
1d774 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64  re.** unmodified
1d775 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  .  ORDER BY term
1d776 73 20 74 68 61 74 20 61 72 65 20 69 6e 74 65 67  s that are integ
1d777 65 72 73 20 6f 75 74 73 69 64 65 20 74 68 65 20  ers outside the 
1d778 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68  range of.** 1 th
1d779 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74 65  rough N generate
1d77a 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45   an error.  ORDE
1d77b 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20  R BY terms that 
1d77c 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a  are expressions.
1d77d 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20 61  ** are matched a
1d77e 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65  gainst result se
1d77f 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66  t expressions of
1d780 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
1d781 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  .** beginning wi
1d782 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  th the left-most
1d783 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b   SELECT and work
1d784 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 72  ing toward the r
1d785 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20  ight..** At the 
1d786 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68 65  first match, the
1d787 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73   ORDER BY expres
1d788 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f 72  sion is transfor
1d789 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20  med into.** the 
1d78a 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e  integer column n
1d78b 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  umber..**.** Ret
1d78c 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1d78d 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a  f errors seen..*
1d78e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
1d78f 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65  olveCompoundOrde
1d790 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50  rBy(.  Parse *pP
1d791 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
1d792 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e  Parsing context.
1d793 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65    Leave error me
1d794 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20  ssages here */. 
1d795 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
1d796 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45         /* The SE
1d797 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63  LECT statement c
1d798 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52  ontaining the OR
1d799 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69  DER BY */.){.  i
1d79a 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73 74  nt i;.  ExprList
1d79b 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45 78   *pOrderBy;.  Ex
1d79c 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
1d79d 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
1d79e 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20   int moreToDo = 
1d79f 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d  1;..  pOrderBy =
1d7a0 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72   pSelect->pOrder
1d7a1 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72  By;.  if( pOrder
1d7a2 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  By==0 ) return 0
1d7a3 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
1d7a4 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  >db;.#if SQLITE_
1d7a5 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28  MAX_COLUMN.  if(
1d7a6 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
1d7a7 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
1d7a8 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d  TE_LIMIT_COLUMN]
1d7a9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1d7aa 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1d7ab 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20  "too many terms 
1d7ac 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  in ORDER BY clau
1d7ad 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  se");.    return
1d7ae 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20   1;.  }.#endif. 
1d7af 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64   for(i=0; i<pOrd
1d7b0 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  erBy->nExpr; i++
1d7b1 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d  ){.    pOrderBy-
1d7b2 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a  >a[i].done = 0;.
1d7b3 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70    }.  pSelect->p
1d7b4 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c  Next = 0;.  whil
1d7b5 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69  e( pSelect->pPri
1d7b6 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65 63  or ){.    pSelec
1d7b7 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74  t->pPrior->pNext
1d7b8 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20   = pSelect;.    
1d7b9 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
1d7ba 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  t->pPrior;.  }. 
1d7bb 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20   while( pSelect 
1d7bc 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20  && moreToDo ){. 
1d7bd 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
1d7be 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
1d7bf 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30      moreToDo = 0
1d7c0 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70  ;.    pEList = p
1d7c1 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a  Select->pEList;.
1d7c2 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69      assert( pELi
1d7c3 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72  st!=0 );.    for
1d7c4 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64  (i=0, pItem=pOrd
1d7c5 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65  erBy->a; i<pOrde
1d7c6 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  rBy->nExpr; i++,
1d7c7 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1d7c8 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a   int iCol = -1;.
1d7c9 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c 20        Expr *pE, 
1d7ca 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28  *pDup;.      if(
1d7cb 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63   pItem->done ) c
1d7cc 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70  ontinue;.      p
1d7cd 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  E = pItem->pExpr
1d7ce 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
1d7cf 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
1d7d0 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20  (pE, &iCol) ){. 
1d7d1 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c         if( iCol<
1d7d2 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 74  0 || iCol>pEList
1d7d3 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20  ->nExpr ){.     
1d7d4 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f       resolveOutO
1d7d5 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72  fRangeError(pPar
1d7d6 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 2b 31  se, "ORDER", i+1
1d7d7 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
1d7d8 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
1d7d9 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rn 1;.        }.
1d7da 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1d7db 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f       iCol = reso
1d7dc 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65  lveAsName(pParse
1d7dd 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20  , pEList, pE);. 
1d7de 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3d         if( iCol=
1d7df 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1d7e0 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
1d7e1 70 72 44 75 70 28 64 62 2c 20 70 45 29 3b 0a 20  prDup(db, pE);. 
1d7e2 20 20 20 20 20 20 20 20 20 69 66 28 20 21 64 62           if( !db
1d7e3 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1d7e4 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  {.            as
1d7e5 73 65 72 74 28 70 44 75 70 29 3b 0a 20 20 20 20  sert(pDup);.    
1d7e6 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72          iCol = r
1d7e7 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72  esolveOrderByTer
1d7e8 6d 54 6f 45 78 70 72 4c 69 73 74 28 70 50 61 72  mToExprList(pPar
1d7e9 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 44 75  se, pSelect, pDu
1d7ea 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  p);.          }.
1d7eb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1d7ec 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
1d7ed 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 7d  pDup);.        }
1d7ee 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
1d7ef 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  l<0 ){.         
1d7f0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
1d7f1 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1d7f2 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b     if( iCol>0 ){
1d7f3 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  .        CollSeq
1d7f4 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43   *pColl = pE->pC
1d7f5 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  oll;.        int
1d7f6 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61   flags = pE->fla
1d7f7 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
1d7f8 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  te;.        sqli
1d7f9 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1d7fa 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70  , pE);.        p
1d7fb 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45  Item->pExpr = pE
1d7fc 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
1d7fd 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30  b, TK_INTEGER, 0
1d7fe 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
1d7ff 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72 65 74   if( pE==0 ) ret
1d800 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 70  urn 1;.        p
1d801 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c  E->pColl = pColl
1d802 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 66 6c  ;.        pE->fl
1d803 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c  ags |= EP_IntVal
1d804 75 65 20 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20  ue | flags;.    
1d805 20 20 20 20 70 45 2d 3e 69 54 61 62 6c 65 20 3d      pE->iTable =
1d806 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70   iCol;.        p
1d807 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31  Item->iCol = (u1
1d808 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  6)iCol;.        
1d809 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b  pItem->done = 1;
1d80a 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1d80b 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d        moreToDo =
1d80c 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
1d80d 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20  }.    pSelect = 
1d80e 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 3b 0a  pSelect->pNext;.
1d80f 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
1d810 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  <pOrderBy->nExpr
1d811 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
1d812 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64  pOrderBy->a[i].d
1d813 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  one==0 ){.      
1d814 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1d815 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52 44 45  pParse, "%r ORDE
1d816 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73 20 6e  R BY term does n
1d817 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22 0a 20  ot match any ". 
1d818 20 20 20 20 20 20 20 20 20 20 20 22 63 6f 6c 75             "colu
1d819 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  mn in the result
1d81a 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20 20 20   set", i+1);.   
1d81b 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1d81c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1d81d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  0;.}../*.** Chec
1d81e 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20  k every term in 
1d81f 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20  the ORDER BY or 
1d820 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1d821 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a 20 74  pOrderBy of.** t
1d822 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
1d823 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20 49 66  ent pSelect.  If
1d824 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72 65 66   any term is ref
1d825 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a 20 72  erence to a.** r
1d826 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73  esult set expres
1d827 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72 6d 69  sion (as determi
1d828 6e 65 64 20 62 79 20 74 68 65 20 45 78 70 72 4c  ned by the ExprL
1d829 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65 6c 64  ist.a.iCol field
1d82a 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76 65 72  ).** then conver
1d82b 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e 74 6f  t that term into
1d82c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63   a copy of the c
1d82d 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 65 73  orresponding res
1d82e 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d  ult set.** colum
1d82f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20  n..**.** If any 
1d830 65 72 72 6f 72 73 20 61 72 65 20 64 65 74 65 63  errors are detec
1d831 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72 72 6f  ted, add an erro
1d832 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61  r message to pPa
1d833 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72  rse and.** retur
1d834 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74  n non-zero.  Ret
1d835 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f 20 65  urn zero if no e
1d836 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e 2e 0a  rrors are seen..
1d837 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d838 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73  E int sqlite3Res
1d839 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
1d83a 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1d83b 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
1d83c 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c  sing context.  L
1d83d 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61  eave error messa
1d83e 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65  ges here */.  Se
1d83f 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
1d840 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
1d841 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74  T statement cont
1d842 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61 75 73  aining the claus
1d843 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
1d844 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20  *pOrderBy,   /* 
1d845 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20  The ORDER BY or 
1d846 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1d847 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65 64 20  to be processed 
1d848 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1d849 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 22 4f  *zType     /* "O
1d84a 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22  RDER" or "GROUP"
1d84b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
1d84c 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1d84d 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 45 78  pParse->db;.  Ex
1d84e 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
1d84f 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1d850 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a  t_item *pItem;..
1d851 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d    if( pOrderBy==
1d852 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d  0 || pParse->db-
1d853 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
1d854 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51  return 0;.#if SQ
1d855 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a  LITE_MAX_COLUMN.
1d856 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e    if( pOrderBy->
1d857 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74  nExpr>db->aLimit
1d858 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
1d859 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c  LUMN] ){.    sql
1d85a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1d85b 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74  rse, "too many t
1d85c 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20 63 6c  erms in %s BY cl
1d85d 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b 0a 20  ause", zType);. 
1d85e 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
1d85f 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69 73 74  .#endif.  pEList
1d860 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69   = pSelect->pELi
1d861 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45  st;.  assert( pE
1d862 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 73  List!=0 );  /* s
1d863 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
1d864 29 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 69  ) guarantees thi
1d865 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20  s */.  for(i=0, 
1d866 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e  pItem=pOrderBy->
1d867 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e  a; i<pOrderBy->n
1d868 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
1d869 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74  ++){.    if( pIt
1d86a 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20 20 20  em->iCol ){.    
1d86b 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f    if( pItem->iCo
1d86c 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  l>pEList->nExpr 
1d86d 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c  ){.        resol
1d86e 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f  veOutOfRangeErro
1d86f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c  r(pParse, zType,
1d870 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45   i+1, pEList->nE
1d871 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65  xpr);.        re
1d872 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
1d873 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69        resolveAli
1d874 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73  as(pParse, pELis
1d875 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31  t, pItem->iCol-1
1d876 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20  , pItem->pExpr, 
1d877 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20  zType);.    }.  
1d878 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
1d879 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20  ./*.** pOrderBy 
1d87a 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f  is an ORDER BY o
1d87b 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
1d87c 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 74  e in SELECT stat
1d87d 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 0a 2a  ement pSelect..*
1d87e 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e 74 65  * The Name conte
1d87f 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  xt of the SELECT
1d880 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 4e   statement is pN
1d881 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65 69 74  C.  zType is eit
1d882 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22 20 6f  her.** "ORDER" o
1d883 72 20 22 47 52 4f 55 50 22 20 64 65 70 65 6e 64  r "GROUP" depend
1d884 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74 79 70  ing on which typ
1d885 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f 72 64  e of clause pOrd
1d886 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54  erBy is..**.** T
1d887 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 6f  his routine reso
1d888 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d 20 6f  lves each term o
1d889 66 20 74 68 65 20 63 6c 61 75 73 65 20 69 6e 74  f the clause int
1d88a 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e  o an expression.
1d88b 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64 65 72  .** If the order
1d88c 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69  -by term is an i
1d88d 6e 74 65 67 65 72 20 49 20 62 65 74 77 65 65 6e  nteger I between
1d88e 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72 65 20   1 and N (where 
1d88f 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  N is the.** numb
1d890 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
1d891 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1d892 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29 20 74  of the SELECT) t
1d893 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69  hen the expressi
1d894 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73  on.** in the res
1d895 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63 6f 70  olution is a cop
1d896 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20 72 65  y of the I-th re
1d897 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
1d898 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20  ion.  If.** the 
1d899 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73  order-by term is
1d89a 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74 68 61   an identify tha
1d89b 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  t corresponds to
1d89c 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 0a   the AS-name of.
1d89d 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65 74 20  ** a result-set 
1d89e 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e  expression, then
1d89f 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f 6c 76   the term resolv
1d8a0 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20  es to a copy of 
1d8a1 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65  the.** result-se
1d8a2 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4f  t expression.  O
1d8a3 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 65 78  therwise, the ex
1d8a4 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 73 6f  pression is reso
1d8a5 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 75  lved in.** the u
1d8a6 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69 6e 67  sual way - using
1d8a7 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
1d8a8 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a  xprNames()..**.*
1d8a9 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
1d8aa 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
1d8ab 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66  r of errors.  If
1d8ac 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74   errors occur, t
1d8ad 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70  hen.** an approp
1d8ae 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73  riate error mess
1d8af 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c 65 66  age might be lef
1d8b0 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20 28 4f  t in pParse.  (O
1d8b1 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65 78 63  OM errors.** exc
1d8b2 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69  epted.).*/.stati
1d8b3 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64  c int resolveOrd
1d8b4 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e 61 6d  erGroupBy(.  Nam
1d8b5 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20  eContext *pNC,  
1d8b6 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63     /* The name c
1d8b7 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53 45  ontext of the SE
1d8b8 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
1d8b9 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
1d8ba 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  ect,      /* The
1d8bb 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1d8bc 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64 65 72  t holding pOrder
1d8bd 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  By */.  ExprList
1d8be 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a   *pOrderBy,   /*
1d8bf 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20   An ORDER BY or 
1d8c0 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1d8c1 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a 20 20  to resolve */.  
1d8c2 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70  const char *zTyp
1d8c3 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20  e     /* Either 
1d8c4 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55  "ORDER" or "GROU
1d8c5 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61  P", as appropria
1d8c6 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  te */.){.  int i
1d8c7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1d8c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1d8c9 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
1d8ca 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
1d8cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d8cc 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a   Column number *
1d8cd 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  /.  struct ExprL
1d8ce 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
1d8cf 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20     /* A term of 
1d8d0 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
1d8d1 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  use */.  Parse *
1d8d2 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
1d8d3 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
1d8d4 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1d8d5 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20 20 20  int nResult;    
1d8d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d8d7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
1d8d8 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
1d8d9 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4f  set */..  if( pO
1d8da 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75  rderBy==0 ) retu
1d8db 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c 74 20  rn 0;.  nResult 
1d8dc 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  = pSelect->pELis
1d8dd 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50 61 72  t->nExpr;.  pPar
1d8de 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
1d8df 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74  ;.  for(i=0, pIt
1d8e0 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
1d8e1 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
1d8e2 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  r; i++, pItem++)
1d8e3 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d  {.    Expr *pE =
1d8e4 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20   pItem->pExpr;. 
1d8e5 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76     iCol = resolv
1d8e6 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  eAsName(pParse, 
1d8e7 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c  pSelect->pEList,
1d8e8 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20 69 43   pE);.    if( iC
1d8e9 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 65  ol<0 ){.      re
1d8ea 74 75 72 6e 20 31 3b 20 20 2f 2a 20 4f 4f 4d 20  turn 1;  /* OOM 
1d8eb 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 7d 0a 20  error */.    }. 
1d8ec 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b     if( iCol>0 ){
1d8ed 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20  .      /* If an 
1d8ee 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 20 69 73  AS-name match is
1d8ef 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 74 68 69   found, mark thi
1d8f0 73 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 75 6d  s ORDER BY colum
1d8f1 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 20 20 20  n as being.     
1d8f2 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68   ** a copy of th
1d8f3 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74  e iCol-th result
1d8f4 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 68  -set column.  Th
1d8f5 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  e subsequent cal
1d8f6 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 71  l to.      ** sq
1d8f7 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65  lite3ResolveOrde
1d8f8 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c 6c 20  rGroupBy() will 
1d8f9 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72  convert the expr
1d8fa 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 20 20 20  ession to a.    
1d8fb 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 68 65    ** copy of the
1d8fc 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74 2d   iCol-th result-
1d8fd 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  set expression. 
1d8fe 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  */.      pItem->
1d8ff 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c  iCol = (u16)iCol
1d900 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  ;.      continue
1d901 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1d902 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74  sqlite3ExprIsInt
1d903 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c 29 20  eger(pE, &iCol) 
1d904 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ){.      /* The 
1d905 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 69 73  ORDER BY term is
1d906 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e 73   an integer cons
1d907 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c 20 73 65  tant.  Again, se
1d908 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20  t the column.   
1d909 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 73 6f 20     ** number so 
1d90a 74 68 61 74 20 73 71 6c 69 74 65 33 52 65 73 6f  that sqlite3Reso
1d90b 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
1d90c 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74  ) will convert t
1d90d 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65  he.      ** orde
1d90e 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 61 20 63  r-by term to a c
1d90f 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c  opy of the resul
1d910 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
1d911 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 43   */.      if( iC
1d912 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 20 20 20  ol<1 ){.        
1d913 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67  resolveOutOfRang
1d914 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 7a  eError(pParse, z
1d915 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 65 73 75  Type, i+1, nResu
1d916 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  lt);.        ret
1d917 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
1d918 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c       pItem->iCol
1d919 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20   = (u16)iCol;.  
1d91a 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
1d91b 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 74 68 65    }..    /* Othe
1d91c 72 77 69 73 65 2c 20 74 72 65 61 74 20 74 68 65  rwise, treat the
1d91d 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 61   ORDER BY term a
1d91e 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 65 78  s an ordinary ex
1d91f 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20  pression */.    
1d920 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 30 3b  pItem->iCol = 0;
1d921 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1d922 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
1d923 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a 20 20 20  (pNC, pE) ){.   
1d924 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1d925 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1d926 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72  sqlite3ResolveOr
1d927 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73  derGroupBy(pPars
1d928 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 4f 72 64  e, pSelect, pOrd
1d929 65 72 42 79 2c 20 7a 54 79 70 65 29 3b 0a 7d 0a  erBy, zType);.}.
1d92a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6e  ./*.** Resolve n
1d92b 61 6d 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45  ames in the SELE
1d92c 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 20 61  CT statement p a
1d92d 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 65  nd all of its de
1d92e 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f 0a 73 74  scendents..*/.st
1d92f 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65  atic int resolve
1d930 53 65 6c 65 63 74 53 74 65 70 28 57 61 6c 6b 65  SelectStep(Walke
1d931 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
1d932 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d 65 43 6f  ct *p){.  NameCo
1d933 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b  ntext *pOuterNC;
1d934 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 74 68 61    /* Context tha
1d935 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20  t contains this 
1d936 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e 61 6d 65  SELECT */.  Name
1d937 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
1d938 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74      /* Name cont
1d939 65 78 74 20 6f 66 20 74 68 69 73 20 53 45 4c 45  ext of this SELE
1d93a 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f  CT */.  int isCo
1d93b 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20  mpound;         
1d93c 2f 2a 20 54 72 75 65 20 69 66 20 70 20 69 73 20  /* True if p is 
1d93d 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  a compound selec
1d93e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6d 70  t */.  int nComp
1d93f 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f  ound;          /
1d940 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70  * Number of comp
1d941 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 6f 63 65  ound terms proce
1d942 73 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20  ssed so far */. 
1d943 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
1d944 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
1d945 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
1d946 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
1d947 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75  t;       /* Resu
1d948 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt set expressio
1d949 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  n list */.  int 
1d94a 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
1d94b 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1d94c 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ter */.  ExprLis
1d94d 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20  t *pGroupBy;    
1d94e 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59   /* The GROUP BY
1d94f 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c   clause */.  Sel
1d950 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73 74 3b 20  ect *pLeftmost; 
1d951 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d 6d 6f 73       /* Left-mos
1d952 74 20 6f 66 20 53 45 4c 45 43 54 20 6f 66 20 61  t of SELECT of a
1d953 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 73   compound */.  s
1d954 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
1d955 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
1d956 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
1d957 0a 20 20 0a 0a 20 20 61 73 73 65 72 74 28 20 70  .  ..  assert( p
1d958 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  !=0 );.  if( p->
1d959 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65  selFlags & SF_Re
1d95a 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20 20 72 65  solved ){.    re
1d95b 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
1d95c 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d    }.  pOuterNC =
1d95d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b   pWalker->u.pNC;
1d95e 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c  .  pParse = pWal
1d95f 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 64  ker->pParse;.  d
1d960 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1d961 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 20 73  .  /* Normally s
1d962 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61  qlite3SelectExpa
1d963 6e 64 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c  nd() will be cal
1d964 6c 65 64 20 66 69 72 73 74 20 61 6e 64 20 77 69  led first and wi
1d965 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72  ll have.  ** alr
1d966 65 61 64 79 20 65 78 70 61 6e 64 65 64 20 74 68  eady expanded th
1d967 69 73 20 53 45 4c 45 43 54 2e 20 20 48 6f 77 65  is SELECT.  Howe
1d968 76 65 72 2c 20 69 66 20 74 68 69 73 20 69 73 20  ver, if this is 
1d969 61 20 73 75 62 71 75 65 72 79 20 77 69 74 68 69  a subquery withi
1d96a 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73  n.  ** an expres
1d96b 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33 52 65 73  sion, sqlite3Res
1d96c 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20  olveExprNames() 
1d96d 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 77  will be called w
1d96e 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a 20 70 72  ithout a.  ** pr
1d96f 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ior call to sqli
1d970 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
1d971 29 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61  ).  When that ha
1d972 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20 2a 2a 20  ppens, let.  ** 
1d973 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65  sqlite3SelectPre
1d974 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66 20 74 68  p() do all of th
1d975 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72  e processing for
1d976 20 74 68 69 73 20 53 45 4c 45 43 54 2e 0a 20 20   this SELECT..  
1d977 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ** sqlite3Select
1d978 50 72 65 70 28 29 20 77 69 6c 6c 20 69 6e 76 6f  Prep() will invo
1d979 6b 65 20 62 6f 74 68 20 73 71 6c 69 74 65 33 53  ke both sqlite3S
1d97a 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 61 6e  electExpand() an
1d97b 64 0a 20 20 2a 2a 20 74 68 69 73 20 72 6f 75 74  d.  ** this rout
1d97c 69 6e 65 20 69 6e 20 74 68 65 20 63 6f 72 72 65  ine in the corre
1d97d 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20  ct order..  */. 
1d97e 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67   if( (p->selFlag
1d97f 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29  s & SF_Expanded)
1d980 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1d981 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61  e3SelectPrep(pPa
1d982 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43  rse, p, pOuterNC
1d983 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70  );.    return (p
1d984 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
1d985 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
1d986 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57   ? WRC_Abort : W
1d987 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20  RC_Prune;.  }.. 
1d988 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d   isCompound = p-
1d989 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20 20 6e 43  >pPrior!=0;.  nC
1d98a 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 70  ompound = 0;.  p
1d98b 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20  Leftmost = p;.  
1d98c 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
1d98d 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46  assert( (p->selF
1d98e 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64  lags & SF_Expand
1d98f 65 64 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  ed)!=0 );.    as
1d990 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61  sert( (p->selFla
1d991 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64  gs & SF_Resolved
1d992 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 73  )==0 );.    p->s
1d993 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 52 65  elFlags |= SF_Re
1d994 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20 2f 2a 20  solved;..    /* 
1d995 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72  Resolve the expr
1d996 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 4c  essions in the L
1d997 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20  IMIT and OFFSET 
1d998 63 6c 61 75 73 65 73 2e 20 54 68 65 73 65 0a 20  clauses. These. 
1d999 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c     ** are not al
1d99a 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74  lowed to refer t
1d99b 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20  o any names, so 
1d99c 70 61 73 73 20 61 6e 20 65 6d 70 74 79 20 4e 61  pass an empty Na
1d99d 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a  meContext..    *
1d99e 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e  /.    memset(&sN
1d99f 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
1d9a0 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72  ));.    sNC.pPar
1d9a1 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20  se = pParse;.   
1d9a2 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
1d9a3 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
1d9a4 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c  C, p->pLimit) ||
1d9a5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1d9a6 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
1d9a7 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65  (&sNC, p->pOffse
1d9a8 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  t) ){.      retu
1d9a9 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1d9aa 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 53 65    }.  .    /* Se
1d9ab 74 20 75 70 20 74 68 65 20 6c 6f 63 61 6c 20 6e  t up the local n
1d9ac 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 70  ame-context to p
1d9ad 61 73 73 20 74 6f 20 73 71 6c 69 74 65 33 52 65  ass to sqlite3Re
1d9ae 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29  solveExprNames()
1d9af 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c   to.    ** resol
1d9b0 76 65 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65  ve the result-se
1d9b1 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  t expression lis
1d9b2 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e  t..    */.    sN
1d9b3 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a  C.allowAgg = 1;.
1d9b4 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74      sNC.pSrcList
1d9b5 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20   = p->pSrc;.    
1d9b6 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4f 75 74  sNC.pNext = pOut
1d9b7 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20 2f 2a 20  erNC;.  .    /* 
1d9b8 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e  Resolve names in
1d9b9 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
1d9ba 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73 74 20 3d   */.    pEList =
1d9bb 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20   p->pEList;.    
1d9bc 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d  assert( pEList!=
1d9bd 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  0 );.    for(i=0
1d9be 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; i<pEList->nExp
1d9bf 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45  r; i++){.      E
1d9c0 78 70 72 20 2a 70 58 20 3d 20 70 45 4c 69 73 74  xpr *pX = pEList
1d9c1 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
1d9c2 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
1d9c3 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
1d9c4 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a 20 20 20  &sNC, pX) ){.   
1d9c5 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
1d9c6 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20  Abort;.      }. 
1d9c7 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52     }.  .    /* R
1d9c8 65 63 75 72 73 69 76 65 6c 79 20 72 65 73 6f 6c  ecursively resol
1d9c9 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20  ve names in all 
1d9ca 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20 2a  subqueries.    *
1d9cb 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
1d9cc 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20  <p->pSrc->nSrc; 
1d9cd 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75  i++){.      stru
1d9ce 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
1d9cf 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72  *pItem = &p->pSr
1d9d0 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 69  c->a[i];.      i
1d9d1 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  f( pItem->pSelec
1d9d2 74 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e  t ){.        con
1d9d3 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65 64 43  st char *zSavedC
1d9d4 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d  ontext = pParse-
1d9d5 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20  >zAuthContext;. 
1d9d6 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
1d9d7 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61 72 73 65  ->zName ) pParse
1d9d8 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d  ->zAuthContext =
1d9d9 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20   pItem->zName;. 
1d9da 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
1d9db 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73  solveSelectNames
1d9dc 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e  (pParse, pItem->
1d9dd 70 53 65 6c 65 63 74 2c 20 70 4f 75 74 65 72 4e  pSelect, pOuterN
1d9de 43 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72  C);.        pPar
1d9df 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
1d9e0 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74 65 78 74   = zSavedContext
1d9e1 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  ;.        if( pP
1d9e2 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
1d9e3 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1d9e4 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1d9e5 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
1d9e6 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  .  .    /* If th
1d9e7 65 72 65 20 61 72 65 20 6e 6f 20 61 67 67 72 65  ere are no aggre
1d9e8 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69  gate functions i
1d9e9 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74  n the result-set
1d9ea 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55 50 20 42  , and no GROUP B
1d9eb 59 20 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73  Y .    ** expres
1d9ec 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20 61 6c 6c  sion, do not all
1d9ed 6f 77 20 61 67 67 72 65 67 61 74 65 73 20 69 6e  ow aggregates in
1d9ee 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65   any of the othe
1d9ef 72 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20  r expressions.. 
1d9f0 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
1d9f1 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  ( (p->selFlags &
1d9f2 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 3d 3d   SF_Aggregate)==
1d9f3 30 20 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 42  0 );.    pGroupB
1d9f4 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b  y = p->pGroupBy;
1d9f5 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42  .    if( pGroupB
1d9f6 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41 67 67 20  y || sNC.hasAgg 
1d9f7 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73 65 6c 46  ){.      p->selF
1d9f8 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 67 72 65  lags |= SF_Aggre
1d9f9 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  gate;.    }else{
1d9fa 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77  .      sNC.allow
1d9fb 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Agg = 0;.    }. 
1d9fc 20 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 48 41   .    /* If a HA
1d9fd 56 49 4e 47 20 63 6c 61 75 73 65 20 69 73 20 70  VING clause is p
1d9fe 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74 68 65  resent, then the
1d9ff 72 65 20 6d 75 73 74 20 62 65 20 61 20 47 52 4f  re must be a GRO
1da00 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 20 20  UP BY clause..  
1da01 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e    */.    if( p->
1da02 70 48 61 76 69 6e 67 20 26 26 20 21 70 47 72 6f  pHaving && !pGro
1da03 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 73 71  upBy ){.      sq
1da04 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1da05 61 72 73 65 2c 20 22 61 20 47 52 4f 55 50 20 42  arse, "a GROUP B
1da06 59 20 63 6c 61 75 73 65 20 69 73 20 72 65 71 75  Y clause is requ
1da07 69 72 65 64 20 62 65 66 6f 72 65 20 48 41 56 49  ired before HAVI
1da08 4e 47 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75  NG");.      retu
1da09 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1da0a 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 64    }.  .    /* Ad
1da0b 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  d the expression
1da0c 20 6c 69 73 74 20 74 6f 20 74 68 65 20 6e 61 6d   list to the nam
1da0d 65 2d 63 6f 6e 74 65 78 74 20 62 65 66 6f 72 65  e-context before
1da0e 20 70 61 72 73 69 6e 67 20 74 68 65 0a 20 20 20   parsing the.   
1da0f 20 2a 2a 20 6f 74 68 65 72 20 65 78 70 72 65 73   ** other expres
1da10 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 4c  sions in the SEL
1da11 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  ECT statement. T
1da12 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 0a 20  his is so that. 
1da13 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e     ** expression
1da14 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  s in the WHERE c
1da15 6c 61 75 73 65 20 28 65 74 63 2e 29 20 63 61 6e  lause (etc.) can
1da16 20 72 65 66 65 72 20 74 6f 20 65 78 70 72 65 73   refer to expres
1da17 73 69 6f 6e 73 20 62 79 0a 20 20 20 20 2a 2a 20  sions by.    ** 
1da18 61 6c 69 61 73 65 73 20 69 6e 20 74 68 65 20 72  aliases in the r
1da19 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 20 20 2a  esult set..    *
1da1a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f 72 20 70  *.    ** Minor p
1da1b 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 20 69 73  oint: If this is
1da1c 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20   the case, then 
1da1d 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 77  the expression w
1da1e 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20 72 65  ill be.    ** re
1da1f 2d 65 76 61 6c 75 61 74 65 64 20 66 6f 72 20 65  -evaluated for e
1da20 61 63 68 20 72 65 66 65 72 65 6e 63 65 20 74 6f  ach reference to
1da21 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   it..    */.    
1da22 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20 70 2d 3e  sNC.pEList = p->
1da23 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 66 28 20  pEList;.    if( 
1da24 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
1da25 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d  prNames(&sNC, p-
1da26 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20 20 20 20  >pWhere) ||.    
1da27 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76     sqlite3Resolv
1da28 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
1da29 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a 20 20 20   p->pHaving).   
1da2a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1da2b 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
1da2c 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  }..    /* The OR
1da2d 44 45 52 20 42 59 20 61 6e 64 20 47 52 4f 55 50  DER BY and GROUP
1da2e 20 42 59 20 63 6c 61 75 73 65 73 20 6d 61 79 20   BY clauses may 
1da2f 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 74 65 72  not refer to ter
1da30 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20 6f 75 74  ms in.    ** out
1da31 65 72 20 71 75 65 72 69 65 73 20 0a 20 20 20 20  er queries .    
1da32 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 74  */.    sNC.pNext
1da33 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43 2e 61 6c   = 0;.    sNC.al
1da34 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a 20 20 20  lowAgg = 1;..   
1da35 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65 20   /* Process the 
1da36 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
1da37 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e 20 53 45  for singleton SE
1da38 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e  LECT statements.
1da39 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f 52 44 45  .    ** The ORDE
1da3a 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20  R BY clause for 
1da3b 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c 45 43 54  compounds SELECT
1da3c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20 68   statements is h
1da3d 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a 20 62 65  andled.    ** be
1da3e 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f  low, after all o
1da3f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74  f the result-set
1da40 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65  s for all of the
1da41 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a 20 20 20   elements of.   
1da42 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   ** the compound
1da43 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c   have been resol
1da44 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ved..    */.    
1da45 69 66 28 20 21 69 73 43 6f 6d 70 6f 75 6e 64 20  if( !isCompound 
1da46 26 26 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47  && resolveOrderG
1da47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 2c 20  roupBy(&sNC, p, 
1da48 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52  p->pOrderBy, "OR
1da49 44 45 52 22 29 20 29 7b 0a 20 20 20 20 20 20 72  DER") ){.      r
1da4a 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1da4b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
1da4c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1da4d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1da4e 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d  WRC_Abort;.    }
1da4f 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c  .  .    /* Resol
1da50 76 65 20 74 68 65 20 47 52 4f 55 50 20 42 59 20  ve the GROUP BY 
1da51 63 6c 61 75 73 65 2e 20 20 41 74 20 74 68 65 20  clause.  At the 
1da52 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61 6b 65 20  same time, make 
1da53 73 75 72 65 20 0a 20 20 20 20 2a 2a 20 74 68 65  sure .    ** the
1da54 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
1da55 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69   does not contai
1da56 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  n aggregate func
1da57 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  tions..    */.  
1da58 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29    if( pGroupBy )
1da59 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  {.      struct E
1da5a 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
1da5b 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20 20 20 20  tem;.    .      
1da5c 69 66 28 20 72 65 73 6f 6c 76 65 4f 72 64 65 72  if( resolveOrder
1da5d 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 2c  GroupBy(&sNC, p,
1da5e 20 70 47 72 6f 75 70 42 79 2c 20 22 47 52 4f 55   pGroupBy, "GROU
1da5f 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  P") || db->mallo
1da60 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
1da61 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1da62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ort;.      }.   
1da63 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
1da64 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b 20 69  m=pGroupBy->a; i
1da65 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72  <pGroupBy->nExpr
1da66 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
1da67 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70  .        if( Exp
1da68 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 49 74  rHasProperty(pIt
1da69 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50 5f 41 67  em->pExpr, EP_Ag
1da6a 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  g) ){.          
1da6b 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1da6c 70 50 61 72 73 65 2c 20 22 61 67 67 72 65 67 61  pParse, "aggrega
1da6d 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  te functions are
1da6e 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20   not allowed in 
1da6f 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
1da70 22 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c  "the GROUP BY cl
1da71 61 75 73 65 22 29 3b 0a 20 20 20 20 20 20 20 20  ause");.        
1da72 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
1da73 72 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rt;.        }.  
1da74 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
1da75 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74   /* Advance to t
1da76 68 65 20 6e 65 78 74 20 74 65 72 6d 20 6f 66 20  he next term of 
1da77 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a 20 20 20  the compound.   
1da78 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70   */.    p = p->p
1da79 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43 6f 6d 70  Prior;.    nComp
1da7a 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f  ound++;.  }..  /
1da7b 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 4f 52  * Resolve the OR
1da7c 44 45 52 20 42 59 20 6f 6e 20 61 20 63 6f 6d 70  DER BY on a comp
1da7d 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 66 74 65  ound SELECT afte
1da7e 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 0a 20  r all terms of. 
1da7f 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   ** the compound
1da80 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c   have been resol
1da81 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ved..  */.  if( 
1da82 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65  isCompound && re
1da83 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64  solveCompoundOrd
1da84 65 72 42 79 28 70 50 61 72 73 65 2c 20 70 4c 65  erBy(pParse, pLe
1da85 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20 20 20 72  ftmost) ){.    r
1da86 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1da87 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 57  .  }..  return W
1da88 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a 2f 2a 0a  RC_Prune;.}../*.
1da89 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1da8a 77 61 6c 6b 73 20 61 6e 20 65 78 70 72 65 73 73  walks an express
1da8b 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 72 65 73  ion tree and res
1da8c 6f 6c 76 65 73 20 72 65 66 65 72 65 6e 63 65 73  olves references
1da8d 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 63 6f 6c   to.** table col
1da8e 75 6d 6e 73 20 61 6e 64 20 72 65 73 75 6c 74 2d  umns and result-
1da8f 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20 20 41 74  set columns.  At
1da90 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20   the same time, 
1da91 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63 68 65 63  do error.** chec
1da92 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74 69 6f 6e  king on function
1da93 20 75 73 61 67 65 20 61 6e 64 20 73 65 74 20 61   usage and set a
1da94 20 66 6c 61 67 20 69 66 20 61 6e 79 20 61 67 67   flag if any agg
1da95 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
1da96 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2a  .** are seen..**
1da97 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 20 74  .** To resolve t
1da98 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 72 65 66  able columns ref
1da99 65 72 65 6e 63 65 73 20 77 65 20 6c 6f 6f 6b 20  erences we look 
1da9a 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72 20 73 75  for nodes (or su
1da9b 62 74 72 65 65 73 29 20 6f 66 20 74 68 65 20 0a  btrees) of the .
1da9c 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72  ** form X.Y.Z or
1da9d 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 20 77   Y.Z or just Z w
1da9e 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  here.**.**      
1da9f 58 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66  X:   The name of
1daa0 20 61 20 64 61 74 61 62 61 73 65 2e 20 20 45 78   a database.  Ex
1daa1 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20 22 74 65  :  "main" or "te
1daa2 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  mp" or.**       
1daa3 20 20 20 20 74 68 65 20 73 79 6d 62 6f 6c 69 63      the symbolic
1daa4 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74   name assigned t
1daa5 6f 20 61 6e 20 41 54 54 41 43 48 2d 65 64 20 64  o an ATTACH-ed d
1daa6 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20  atabase..**.**  
1daa7 20 20 20 20 59 3a 20 20 20 54 68 65 20 6e 61 6d      Y:   The nam
1daa8 65 20 6f 66 20 61 20 74 61 62 6c 65 20 69 6e 20  e of a table in 
1daa9 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  a FROM clause.  
1daaa 4f 72 20 69 6e 20 61 20 74 72 69 67 67 65 72 0a  Or in a trigger.
1daab 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 6e 65  **           one
1daac 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20   of the special 
1daad 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f 72 20 22  names "old" or "
1daae 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  new"..**.**     
1daaf 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f   Z:   The name o
1dab0 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61  f a column in ta
1dab1 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ble Y..**.** The
1dab2 20 6e 6f 64 65 20 61 74 20 74 68 65 20 72 6f 6f   node at the roo
1dab3 74 20 6f 66 20 74 68 65 20 73 75 62 74 72 65 65  t of the subtree
1dab4 20 69 73 20 6d 6f 64 69 66 69 65 64 20 61 73 20   is modified as 
1dab5 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
1dab6 20 20 45 78 70 72 2e 6f 70 20 20 20 20 20 20 20    Expr.op       
1dab7 20 43 68 61 6e 67 65 64 20 74 6f 20 54 4b 5f 43   Changed to TK_C
1dab8 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45 78 70 72  OLUMN.**    Expr
1dab9 2e 70 54 61 62 20 20 20 20 20 20 50 6f 69 6e 74  .pTab      Point
1daba 73 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 6f  s to the Table o
1dabb 62 6a 65 63 74 20 66 6f 72 20 58 2e 59 0a 2a 2a  bject for X.Y.**
1dabc 20 20 20 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e      Expr.iColumn
1dabd 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 69 6e     The column in
1dabe 64 65 78 20 69 6e 20 58 2e 59 2e 20 20 2d 31 20  dex in X.Y.  -1 
1dabf 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a  for the rowid..*
1dac0 2a 20 20 20 20 45 78 70 72 2e 69 54 61 62 6c 65  *    Expr.iTable
1dac1 20 20 20 20 54 68 65 20 56 44 42 45 20 63 75 72      The VDBE cur
1dac2 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 58  sor number for X
1dac3 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 6f 20 72  .Y.**.**.** To r
1dac4 65 73 6f 6c 76 65 20 72 65 73 75 6c 74 2d 73 65  esolve result-se
1dac5 74 20 72 65 66 65 72 65 6e 63 65 73 2c 20 6c 6f  t references, lo
1dac6 6f 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f  ok for expressio
1dac7 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 0a 2a  n nodes of the.*
1dac8 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74 68 20 6e  * form Z (with n
1dac9 6f 20 58 20 61 6e 64 20 59 20 70 72 65 66 69 78  o X and Y prefix
1daca 29 20 77 68 65 72 65 20 74 68 65 20 5a 20 6d 61  ) where the Z ma
1dacb 74 63 68 65 73 20 74 68 65 20 72 69 67 68 74 2d  tches the right-
1dacc 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  hand.** size of 
1dacd 61 6e 20 41 53 20 63 6c 61 75 73 65 20 69 6e 20  an AS clause in 
1dace 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 6f  the result-set o
1dacf 66 20 61 20 53 45 4c 45 43 54 2e 20 20 54 68 65  f a SELECT.  The
1dad0 20 5a 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a   Z expression.**
1dad1 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20   is replaced by 
1dad2 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6c 65  a copy of the le
1dad3 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ft-hand side of 
1dad4 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65  the result-set e
1dad5 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 61  xpression..** Ta
1dad6 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20 66 75 6e  ble-name and fun
1dad7 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74 69 6f 6e  ction resolution
1dad8 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 20 73   occurs on the s
1dad9 75 62 73 74 69 74 75 74 65 64 20 65 78 70 72 65  ubstituted expre
1dada 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 20 20  ssion.** tree.  
1dadb 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e 3a  For example, in:
1dadc 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45  .**.**      SELE
1dadd 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64  CT a+b AS x, c+d
1dade 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52   AS y FROM t1 OR
1dadf 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a 2a 2a 20  DER BY x;.**.** 
1dae0 54 68 65 20 22 78 22 20 74 65 72 6d 20 6f 66 20  The "x" term of 
1dae1 74 68 65 20 6f 72 64 65 72 20 62 79 20 69 73 20  the order by is 
1dae2 72 65 70 6c 61 63 65 64 20 62 79 20 22 61 2b 62  replaced by "a+b
1dae3 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a 2a 2a 0a  " to render:.**.
1dae4 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54 20 61  **      SELECT a
1dae5 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20  +b AS x, c+d AS 
1dae6 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20  y FROM t1 ORDER 
1dae7 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a 20 46 75  BY a+b;.**.** Fu
1dae8 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 61 72 65  nction calls are
1dae9 20 63 68 65 63 6b 65 64 20 74 6f 20 6d 61 6b 65   checked to make
1daea 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 66   sure that the f
1daeb 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a 2a 20 64  unction is .** d
1daec 65 66 69 6e 65 64 20 61 6e 64 20 74 68 61 74 20  efined and that 
1daed 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62  the correct numb
1daee 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
1daef 61 72 65 20 73 70 65 63 69 66 69 65 64 2e 0a 2a  are specified..*
1daf0 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * If the functio
1daf1 6e 20 69 73 20 61 6e 20 61 67 67 72 65 67 61 74  n is an aggregat
1daf2 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e  e function, then
1daf3 20 74 68 65 20 70 4e 43 2d 3e 68 61 73 41 67 67   the pNC->hasAgg
1daf4 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e 64 20 74   is.** set and t
1daf5 68 65 20 6f 70 63 6f 64 65 20 69 73 20 63 68 61  he opcode is cha
1daf6 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f 46 55 4e  nged from TK_FUN
1daf7 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41 47 47 5f  CTION to TK_AGG_
1daf8 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20 49 66 20  FUNCTION..** If 
1daf9 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f  an expression co
1dafa 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65  ntains aggregate
1dafb 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 65 6e 20   functions then 
1dafc 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a 20 70 72  the EP_Agg.** pr
1dafd 6f 70 65 72 74 79 20 6f 6e 20 74 68 65 20 65 78  operty on the ex
1dafe 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 74 2e  pression is set.
1daff 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20  .**.** An error 
1db00 6d 65 73 73 61 67 65 20 69 73 20 6c 65 66 74 20  message is left 
1db01 69 6e 20 70 50 61 72 73 65 20 69 66 20 61 6e 79  in pParse if any
1db02 74 68 69 6e 67 20 69 73 20 61 6d 69 73 73 2e 20  thing is amiss. 
1db03 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 69   The number.** i
1db04 66 20 65 72 72 6f 72 73 20 69 73 20 72 65 74 75  f errors is retu
1db05 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
1db06 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1db07 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
1db08 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43 6f 6e 74  mes( .  NameCont
1db09 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 20 20  ext *pNC,       
1db0a 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 74 6f 20  /* Namespace to 
1db0b 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69  resolve expressi
1db0c 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20 45 78 70  ons in. */.  Exp
1db0d 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
1db0e 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
1db0f 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61  ession to be ana
1db10 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a 20 20 69  lyzed. */.){.  i
1db11 6e 74 20 73 61 76 65 64 48 61 73 41 67 67 3b 0a  nt savedHasAgg;.
1db12 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 69    Walker w;..  i
1db13 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65  f( pExpr==0 ) re
1db14 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49  turn 0;.#if SQLI
1db15 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
1db16 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50 61 72 73  H>0.  {.    Pars
1db17 65 20 2a 70 50 61 72 73 65 20 3d 20 70 4e 43 2d  e *pParse = pNC-
1db18 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 69 66 28  >pParse;.    if(
1db19 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63   sqlite3ExprChec
1db1a 6b 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  kHeight(pParse, 
1db1b 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 2b 70  pExpr->nHeight+p
1db1c 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69  NC->pParse->nHei
1db1d 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ght) ){.      re
1db1e 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
1db1f 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68    pParse->nHeigh
1db20 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69  t += pExpr->nHei
1db21 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ght;.  }.#endif.
1db22 20 20 73 61 76 65 64 48 61 73 41 67 67 20 3d 20    savedHasAgg = 
1db23 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a 20 20 70  pNC->hasAgg;.  p
1db24 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 30 3b 0a  NC->hasAgg = 0;.
1db25 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63    w.xExprCallbac
1db26 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53  k = resolveExprS
1db27 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74  tep;.  w.xSelect
1db28 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c  Callback = resol
1db29 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20  veSelectStep;.  
1db2a 77 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e  w.pParse = pNC->
1db2b 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e  pParse;.  w.u.pN
1db2c 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71 6c 69 74  C = pNC;.  sqlit
1db2d 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70  e3WalkExpr(&w, p
1db2e 45 78 70 72 29 3b 0a 23 69 66 20 53 51 4c 49 54  Expr);.#if SQLIT
1db2f 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
1db30 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 72 73 65  >0.  pNC->pParse
1db31 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 70 45 78  ->nHeight -= pEx
1db32 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23 65 6e  pr->nHeight;.#en
1db33 64 69 66 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e  dif.  if( pNC->n
1db34 45 72 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70  Err>0 ){.    Exp
1db35 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78  rSetProperty(pEx
1db36 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20  pr, EP_Error);. 
1db37 20 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68 61   }.  if( pNC->ha
1db38 73 41 67 67 20 29 7b 0a 20 20 20 20 45 78 70 72  sAgg ){.    Expr
1db39 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70  SetProperty(pExp
1db3a 72 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65  r, EP_Agg);.  }e
1db3b 6c 73 65 20 69 66 28 20 73 61 76 65 64 48 61 73  lse if( savedHas
1db3c 41 67 67 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e  Agg ){.    pNC->
1db3d 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a  hasAgg = 1;.  }.
1db3e 20 20 72 65 74 75 72 6e 20 45 78 70 72 48 61 73    return ExprHas
1db3f 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
1db40 45 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f  EP_Error);.}.../
1db41 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c  *.** Resolve all
1db42 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 65 78   names in all ex
1db43 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 20 53  pressions of a S
1db44 45 4c 45 43 54 20 61 6e 64 20 69 6e 20 61 6c 6c  ELECT and in all
1db45 0a 2a 2a 20 64 65 63 65 6e 64 65 6e 74 73 20 6f  .** decendents o
1db46 66 20 74 68 65 20 53 45 4c 45 43 54 2c 20 69 6e  f the SELECT, in
1db47 63 6c 75 64 69 6e 67 20 63 6f 6d 70 6f 75 6e 64  cluding compound
1db48 73 20 6f 66 66 20 6f 66 20 70 2d 3e 70 50 72 69  s off of p->pPri
1db49 6f 72 2c 0a 2a 2a 20 73 75 62 71 75 65 72 69 65  or,.** subquerie
1db4a 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73  s in expressions
1db4b 2c 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73  , and subqueries
1db4c 20 75 73 65 64 20 61 73 20 46 52 4f 4d 20 63 6c   used as FROM cl
1db4d 61 75 73 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a 2a  ause.** terms..*
1db4e 2a 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65 33  *.** See sqlite3
1db4f 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
1db50 28 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70  () for a descrip
1db51 74 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64  tion of the kind
1db52 73 20 6f 66 0a 2a 2a 20 74 72 61 6e 73 66 6f 72  s of.** transfor
1db53 6d 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63 63  mations that occ
1db54 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 45  ur..**.** All SE
1db55 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
1db56 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e  should have been
1db57 20 65 78 70 61 6e 64 65 64 20 75 73 69 6e 67 0a   expanded using.
1db58 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ** sqlite3Select
1db59 45 78 70 61 6e 64 28 29 20 70 72 69 6f 72 20 74  Expand() prior t
1db5a 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20  o invoking this 
1db5b 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49  routine..*/.SQLI
1db5c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1db5d 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65  sqlite3ResolveSe
1db5e 6c 65 63 74 4e 61 6d 65 73 28 0a 20 20 50 61 72  lectNames(.  Par
1db5f 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
1db60 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
1db61 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
1db62 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
1db63 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
1db64 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65  ECT statement be
1db65 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20  ing coded. */.  
1db66 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75  NameContext *pOu
1db67 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63  terNC  /* Name c
1db68 6f 6e 74 65 78 74 20 66 6f 72 20 70 61 72 65 6e  ontext for paren
1db69 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  t SELECT stateme
1db6a 6e 74 20 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b 65  nt */.){.  Walke
1db6b 72 20 77 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  r w;..  assert( 
1db6c 70 21 3d 30 20 29 3b 0a 20 20 77 2e 78 45 78 70  p!=0 );.  w.xExp
1db6d 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f  rCallback = reso
1db6e 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 77  lveExprStep;.  w
1db6f 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
1db70 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74   = resolveSelect
1db71 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 65  Step;.  w.pParse
1db72 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 77 2e 75   = pParse;.  w.u
1db73 2e 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b  .pNC = pOuterNC;
1db74 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  .  sqlite3WalkSe
1db75 6c 65 63 74 28 26 77 2c 20 70 29 3b 0a 7d 0a 0a  lect(&w, p);.}..
1db76 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1db77 45 6e 64 20 6f 66 20 72 65 73 6f 6c 76 65 2e 63  End of resolve.c
1db78 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1db79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1db7b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1db7c 42 65 67 69 6e 20 66 69 6c 65 20 65 78 70 72 2e  Begin file expr.
1db7d 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1db7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1db80 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
1db81 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
1db82 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1db83 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1db84 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1db85 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1db86 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1db87 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1db88 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1db89 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1db8a 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1db8b 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1db8c 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1db8d 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1db8e 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1db8f 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1db90 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1db91 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1db92 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1db93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db97 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
1db98 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75  ile contains rou
1db99 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 61  tines used for a
1db9a 6e 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73 73  nalyzing express
1db9b 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20  ions and.** for 
1db9c 67 65 6e 65 72 61 74 69 6e 67 20 56 44 42 45 20  generating VDBE 
1db9d 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 61  code that evalua
1db9e 74 65 73 20 65 78 70 72 65 73 73 69 6f 6e 73 20  tes expressions 
1db9f 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  in SQLite..**.**
1dba0 20 24 49 64 3a 20 65 78 70 72 2e 63 2c 76 20 31   $Id: expr.c,v 1
1dba1 2e 34 30 39 20 32 30 30 39 2f 30 31 2f 31 30 20  .409 2009/01/10 
1dba2 31 33 3a 32 34 3a 35 31 20 64 72 68 20 45 78 70  13:24:51 drh Exp
1dba3 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   $.*/../*.** Ret
1dba4 75 72 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74  urn the 'affinit
1dba5 79 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  y' of the expres
1dba6 73 69 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e  sion pExpr if an
1dba7 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70  y..**.** If pExp
1dba8 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61  r is a column, a
1dba9 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
1dbaa 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41  column via an 'A
1dbab 53 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20  S' alias,.** or 
1dbac 61 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74  a sub-select wit
1dbad 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68  h a column as th
1dbae 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20  e return value, 
1dbaf 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66  then the .** aff
1dbb0 69 6e 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f  inity of that co
1dbb1 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64  lumn is returned
1dbb2 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30  . Otherwise, 0x0
1dbb3 30 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a  0 is returned,.*
1dbb4 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20  * indicating no 
1dbb5 61 66 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65  affinity for the
1dbb6 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a   expression..**.
1dbb7 2a 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52  ** i.e. the WHER
1dbb8 45 20 63 6c 61 75 73 65 20 65 78 70 72 65 73 73  E clause express
1dbb9 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c  sions in the fol
1dbba 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  lowing statement
1dbbb 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e  s all.** have an
1dbbc 20 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a   affinity:.**.**
1dbbd 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
1dbbe 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a  (a);.** SELECT *
1dbbf 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
1dbc0 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53  ;.** SELECT a AS
1dbc1 20 62 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   b FROM t1 WHERE
1dbc2 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20   b;.** SELECT * 
1dbc3 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73  FROM t1 WHERE (s
1dbc4 65 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29  elect a from t1)
1dbc5 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.*/.SQLITE_PRIV
1dbc6 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
1dbc7 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70  ExprAffinity(Exp
1dbc8 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74  r *pExpr){.  int
1dbc9 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b   op = pExpr->op;
1dbca 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45  .  if( op==TK_SE
1dbcb 4c 45 43 54 20 29 7b 0a 20 20 20 20 72 65 74 75  LECT ){.    retu
1dbcc 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41 66  rn sqlite3ExprAf
1dbcd 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 53  finity(pExpr->pS
1dbce 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61  elect->pEList->a
1dbcf 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a  [0].pExpr);.  }.
1dbd0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1dbd1 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f  MIT_CAST.  if( o
1dbd2 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20  p==TK_CAST ){.  
1dbd3 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1dbd4 41 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45  AffinityType(&pE
1dbd5 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d  xpr->token);.  }
1dbd6 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f  .#endif.  if( (o
1dbd7 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
1dbd8 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d   || op==TK_COLUM
1dbd9 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49  N || op==TK_REGI
1dbda 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78  STER) .   && pEx
1dbdb 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b  pr->pTab!=0.  ){
1dbdc 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52  .    /* op==TK_R
1dbdd 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72  EGISTER && pExpr
1dbde 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e  ->pTab!=0 happen
1dbdf 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73  s when pExpr was
1dbe0 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20   originally.    
1dbe1 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62  ** a TK_COLUMN b
1dbe2 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  ut was previousl
1dbe3 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  y evaluated and 
1dbe4 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69  cached in a regi
1dbe5 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ster */.    int 
1dbe6 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  j = pExpr->iColu
1dbe7 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20  mn;.    if( j<0 
1dbe8 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1dbe9 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
1dbea 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1dbeb 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d  pTab && j<pExpr-
1dbec 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  >pTab->nCol );. 
1dbed 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d     return pExpr-
1dbee 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61  >pTab->aCol[j].a
1dbef 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72  ffinity;.  }.  r
1dbf0 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66  eturn pExpr->aff
1dbf1 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  inity;.}../*.** 
1dbf2 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  Set the collatin
1dbf3 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65  g sequence for e
1dbf4 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
1dbf5 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74  to be the collat
1dbf6 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ing.** sequence 
1dbf7 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e  named by pToken.
1dbf8 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e     Return a poin
1dbf9 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73  ter to the revis
1dbfa 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  ed expression..*
1dbfb 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  * The collating 
1dbfc 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b  sequence is mark
1dbfd 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22  ed as "explicit"
1dbfe 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78   using the EP_Ex
1dbff 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67  pCollate.** flag
1dc00 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63  .  An explicit c
1dc01 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1dc02 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20  e will override 
1dc03 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c  implicit.** coll
1dc04 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e  ating sequences.
1dc05 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1dc06 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
1dc07 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73  ExprSetColl(Pars
1dc08 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
1dc09 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70  *pExpr, Token *p
1dc0a 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61  CollName){.  cha
1dc0b 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20  r *zColl = 0;   
1dc0c 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75           /* Dequ
1dc0d 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c  oted name of col
1dc0e 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
1dc0f 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  */.  CollSeq *pC
1dc10 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  oll;.  sqlite3 *
1dc11 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1dc12 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  zColl = sqlit
1dc13 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1dc14 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a  db, pCollName);.
1dc15 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a    if( pExpr && z
1dc16 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Coll ){.    pCol
1dc17 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  l = sqlite3Locat
1dc18 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
1dc19 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20 20 20   zColl, -1);.   
1dc1a 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
1dc1b 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c      pExpr->pColl
1dc1c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20   = pColl;.      
1dc1d 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20  pExpr->flags |= 
1dc1e 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20  EP_ExpCollate;. 
1dc1f 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
1dc20 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f  e3DbFree(db, zCo
1dc21 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 45  ll);.  return pE
1dc22 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  xpr;.}../*.** Re
1dc23 74 75 72 6e 20 74 68 65 20 64 65 66 61 75 6c 74  turn the default
1dc24 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
1dc25 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78 70 72  nce for the expr
1dc26 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 49 66  ession pExpr. If
1dc27 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20  .** there is no 
1dc28 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f  default collatio
1dc29 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e 20 30  n type, return 0
1dc2a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1dc2b 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c  ATE CollSeq *sql
1dc2c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
1dc2d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
1dc2e 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 43  xpr *pExpr){.  C
1dc2f 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
1dc30 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 70  0;.  Expr *p = p
1dc31 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 20 70  Expr;.  while( p
1dc32 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0a   ){.    int op;.
1dc33 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70      pColl = p->p
1dc34 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 20 70 43  Coll;.    if( pC
1dc35 6f 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  oll ) break;.   
1dc36 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20   op = p->op;.   
1dc37 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47   if( (op==TK_AGG
1dc38 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54  _COLUMN || op==T
1dc39 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d  K_COLUMN || op==
1dc3a 54 4b 5f 52 45 47 49 53 54 45 52 29 20 26 26 20  TK_REGISTER) && 
1dc3b 70 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20  p->pTab!=0 ){.  
1dc3c 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45      /* op==TK_RE
1dc3d 47 49 53 54 45 52 20 26 26 20 70 2d 3e 70 54 61  GISTER && p->pTa
1dc3e 62 21 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65  b!=0 happens whe
1dc3f 6e 20 70 45 78 70 72 20 77 61 73 20 6f 72 69 67  n pExpr was orig
1dc40 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20  inally.      ** 
1dc41 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20  a TK_COLUMN but 
1dc42 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65  was previously e
1dc43 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63 61 63  valuated and cac
1dc44 68 65 64 20 69 6e 20 61 20 72 65 67 69 73 74 65  hed in a registe
1dc45 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74  r */.      const
1dc46 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20   char *zColl;.  
1dc47 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d 3e 69      int j = p->i
1dc48 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66  Column;.      if
1dc49 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( j>=0 ){.      
1dc4a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1dc4b 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20  pParse->db;.    
1dc4c 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d 3e 70      zColl = p->p
1dc4d 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f  Tab->aCol[j].zCo
1dc4e 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c  ll;.        pCol
1dc4f 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
1dc50 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64  ollSeq(db, ENC(d
1dc51 62 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30  b), zColl, -1, 0
1dc52 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  );.        pExpr
1dc53 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b  ->pColl = pColl;
1dc54 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1dc55 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1dc56 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20  if( op!=TK_CAST 
1dc57 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20  && op!=TK_UPLUS 
1dc58 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ){.      break;.
1dc59 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d      }.    p = p-
1dc5a 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66  >pLeft;.  }.  if
1dc5b 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f  ( sqlite3CheckCo
1dc5c 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43  llSeq(pParse, pC
1dc5d 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f  oll) ){ .    pCo
1dc5e 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ll = 0;.  }.  re
1dc5f 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  turn pColl;.}../
1dc60 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e  *.** pExpr is an
1dc61 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f   operand of a co
1dc62 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f  mparison operato
1dc63 72 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a  r.  aff2 is the.
1dc64 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79  ** type affinity
1dc65 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70   of the other op
1dc66 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75  erand.  This rou
1dc67 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
1dc68 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74  .** type affinit
1dc69 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  y that should be
1dc6a 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f   used for the co
1dc6b 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f  mparison operato
1dc6c 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
1dc6d 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65  VATE char sqlite
1dc6e 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
1dc6f 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
1dc70 61 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72  ar aff2){.  char
1dc71 20 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45   aff1 = sqlite3E
1dc72 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70  xprAffinity(pExp
1dc73 72 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26  r);.  if( aff1 &
1dc74 26 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a  & aff2 ){.    /*
1dc75 20 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74   Both sides of t
1dc76 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72  he comparison ar
1dc77 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e  e columns. If on
1dc78 65 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20  e has numeric.  
1dc79 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75    ** affinity, u
1dc7a 73 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69  se that. Otherwi
1dc7b 73 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69  se use no affini
1dc7c 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ty..    */.    i
1dc7d 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65  f( sqlite3IsNume
1dc7e 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31  ricAffinity(aff1
1dc7f 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75  ) || sqlite3IsNu
1dc80 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66  mericAffinity(af
1dc81 66 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  f2) ){.      ret
1dc82 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  urn SQLITE_AFF_N
1dc83 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73  UMERIC;.    }els
1dc84 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
1dc85 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
1dc86 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
1dc87 66 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66  f( !aff1 && !aff
1dc88 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74  2 ){.    /* Neit
1dc89 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20  her side of the 
1dc8a 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20  comparison is a 
1dc8b 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65  column.  Compare
1dc8c 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75   the.    ** resu
1dc8d 6c 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20  lts directly..  
1dc8e 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20    */.    return 
1dc8f 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
1dc90 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1dc91 20 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63   One side is a c
1dc92 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72  olumn, the other
1dc93 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65   is not. Use the
1dc94 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74   columns affinit
1dc95 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  y. */.    assert
1dc96 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66  ( aff1==0 || aff
1dc97 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75  2==0 );.    retu
1dc98 72 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29  rn (aff1 + aff2)
1dc99 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  ;.  }.}../*.** p
1dc9a 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72  Expr is a compar
1dc9b 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20  ison operator.  
1dc9c 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20  Return the type 
1dc9d 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
1dc9e 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69  ould.** be appli
1dc9f 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61  ed to both opera
1dca0 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69  nds prior to doi
1dca1 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  ng the compariso
1dca2 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  n..*/.static cha
1dca3 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69  r comparisonAffi
1dca4 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
1dca5 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20  ){.  char aff;. 
1dca6 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1dca7 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78  op==TK_EQ || pEx
1dca8 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c  pr->op==TK_IN ||
1dca9 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c   pExpr->op==TK_L
1dcaa 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70  T ||.          p
1dcab 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20  Expr->op==TK_GT 
1dcac 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  || pExpr->op==TK
1dcad 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  _GE || pExpr->op
1dcae 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20  ==TK_LE ||.     
1dcaf 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d       pExpr->op==
1dcb0 54 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72  TK_NE );.  asser
1dcb1 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20  t( pExpr->pLeft 
1dcb2 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74  );.  aff = sqlit
1dcb3 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70  e3ExprAffinity(p
1dcb4 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  Expr->pLeft);.  
1dcb5 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68  if( pExpr->pRigh
1dcb6 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73  t ){.    aff = s
1dcb7 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
1dcb8 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69  inity(pExpr->pRi
1dcb9 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 0a 20  ght, aff);.  }. 
1dcba 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d   else if( pExpr-
1dcbb 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
1dcbc 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d  aff = sqlite3Com
1dcbd 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 78  pareAffinity(pEx
1dcbe 70 72 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 45 4c  pr->pSelect->pEL
1dcbf 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c  ist->a[0].pExpr,
1dcc0 20 61 66 66 29 3b 0a 20 20 7d 0a 20 20 65 6c 73   aff);.  }.  els
1dcc1 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20 20  e if( !aff ){.  
1dcc2 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
1dcc3 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20 72  FF_NONE;.  }.  r
1dcc4 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a  eturn aff;.}../*
1dcc5 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 63  .** pExpr is a c
1dcc6 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65 73  omparison expres
1dcc7 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20 27  sion, eg. '=', '
1dcc8 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63 2e  <', IN(...) etc.
1dcc9 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74 79  .** idx_affinity
1dcca 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74 79   is the affinity
1dccb 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20 63   of an indexed c
1dccc 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74 72  olumn. Return tr
1dccd 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e 64  ue.** if the ind
1dcce 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74 79  ex with affinity
1dccf 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d 61   idx_affinity ma
1dcd0 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70  y be used to imp
1dcd1 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63 6f  lement.** the co
1dcd2 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78 70  mparison in pExp
1dcd3 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
1dcd4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1dcd5 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 28  IndexAffinityOk(
1dcd6 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
1dcd7 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29 7b  r idx_affinity){
1dcd8 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 6f  .  char aff = co
1dcd9 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79  mparisonAffinity
1dcda 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74 63  (pExpr);.  switc
1dcdb 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63 61  h( aff ){.    ca
1dcdc 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  se SQLITE_AFF_NO
1dcdd 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e  NE:.      return
1dcde 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   1;.    case SQL
1dcdf 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20 20  ITE_AFF_TEXT:.  
1dce0 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f 61      return idx_a
1dce1 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
1dce2 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64 65  AFF_TEXT;.    de
1dce3 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65 74  fault:.      ret
1dce4 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75 6d  urn sqlite3IsNum
1dce5 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64 78  ericAffinity(idx
1dce6 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d 0a  _affinity);.  }.
1dce7 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1dce8 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 68 61  the P5 value tha
1dce9 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  t should be used
1dcea 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63 6f   for a binary co
1dceb 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63 6f  mparison.** opco
1dcec 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47 65  de (OP_Eq, OP_Ge
1dced 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20 63   etc.) used to c
1dcee 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61 6e  ompare pExpr1 an
1dcef 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 74 61  d pExpr2..*/.sta
1dcf0 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 6f 6d  tic u8 binaryCom
1dcf1 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 45 78  pareP5(Expr *pEx
1dcf2 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 70 72  pr1, Expr *pExpr
1dcf3 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c  2, int jumpIfNul
1dcf4 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d 20 28  l){.  u8 aff = (
1dcf5 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 70 72  char)sqlite3Expr
1dcf6 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32 29  Affinity(pExpr2)
1dcf7 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 73 71  ;.  aff = (u8)sq
1dcf8 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
1dcf9 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66 66  nity(pExpr1, aff
1dcfa 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 4e 75  ) | (u8)jumpIfNu
1dcfb 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 66 66  ll;.  return aff
1dcfc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1dcfd 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
1dcfe 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
1dcff 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75 6c  uence that shoul
1dd00 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a 20  d be used by.** 
1dd01 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69  a binary compari
1dd02 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f 6d  son operator com
1dd03 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e 64  paring pLeft and
1dd04 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49   pRight..**.** I
1dd05 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20  f the left hand 
1dd06 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61  expression has a
1dd07 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
1dd08 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20 69  nce type, then i
1dd09 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f 74  t is.** used. Ot
1dd0a 68 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c 6c  herwise the coll
1dd0b 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
1dd0c 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 61 6e  or the right han
1dd0d 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  d expression.** 
1dd0e 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65 20  is used, or the 
1dd0f 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59 29  default (BINARY)
1dd10 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 70 72   if neither expr
1dd11 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c  ession has a col
1dd12 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a  lating.** type..
1dd13 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70  **.** Argument p
1dd14 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20 70  Right (but not p
1dd15 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 20 6e  Left) may be a n
1dd16 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e 20  ull pointer. In 
1dd17 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 74  this case,.** it
1dd18 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72   is not consider
1dd19 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1dd1a 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
1dd1b 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70  qlite3BinaryComp
1dd1c 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50 61  areCollSeq(.  Pa
1dd1d 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20  rse *pParse, .  
1dd1e 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20  Expr *pLeft, .  
1dd1f 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a  Expr *pRight.){.
1dd20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
1dd21 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66  ;.  assert( pLef
1dd22 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66 74  t );.  if( pLeft
1dd23 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
1dd24 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61  Collate ){.    a
1dd25 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70 43  ssert( pLeft->pC
1dd26 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c  oll );.    pColl
1dd27 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b   = pLeft->pColl;
1dd28 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69  .  }else if( pRi
1dd29 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e 66  ght && pRight->f
1dd2a 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
1dd2b 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65  late ){.    asse
1dd2c 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c  rt( pRight->pCol
1dd2d 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  l );.    pColl =
1dd2e 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a   pRight->pColl;.
1dd2f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f    }else{.    pCo
1dd30 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
1dd31 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
1dd32 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20  pLeft);.    if( 
1dd33 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  !pColl ){.      
1dd34 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
1dd35 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
1dd36 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20  e, pRight);.    
1dd37 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
1dd38 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Coll;.}../*.** G
1dd39 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 65 72  enerate the oper
1dd3a 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d 70 61  ands for a compa
1dd3b 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 2e  rison operation.
1dd3c 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 6e 65    Before.** gene
1dd3d 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20  rating the code 
1dd3e 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 6e 64  for each operand
1dd3f 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 6e 79  , set the EP_Any
1dd40 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 74  Aff.** flag on t
1dd41 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 6f  he expression so
1dd42 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65   that it will be
1dd43 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 61 0a   able to used a.
1dd44 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e  ** cached column
1dd45 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 73 20   value that has 
1dd46 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 65 72  previously under
1dd47 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 69 6e  gone an.** affin
1dd48 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73  ity change..*/.s
1dd49 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 43  tatic void codeC
1dd4a 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 0a  ompareOperands(.
1dd4b 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1dd4c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61      /* Parsing a
1dd4d 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69  nd code generati
1dd4e 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1dd4f 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20  Expr *pLeft,    
1dd50 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70    /* The left op
1dd51 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  erand */.  int *
1dd52 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f 2a 20  pRegLeft,    /* 
1dd53 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20 6c  Register where l
1dd54 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73  eft operand is s
1dd55 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  tored */.  int *
1dd56 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f 2a 20  pFreeLeft,   /* 
1dd57 46 72 65 65 20 74 68 69 73 20 72 65 67 69 73 74  Free this regist
1dd58 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a  er when done */.
1dd59 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20    Expr *pRight, 
1dd5a 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74      /* The right
1dd5b 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
1dd5c 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 20 20  t *pRegRight,   
1dd5d 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 72  /* Register wher
1dd5e 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  e right operand 
1dd5f 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69  is stored */.  i
1dd60 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 20 20  nt *pFreeRight  
1dd61 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 20 72   /* Write temp r
1dd62 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 67 68  egister for righ
1dd63 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 65 20  t operand there 
1dd64 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  */.){.  while( p
1dd65 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c  Left->op==TK_UPL
1dd66 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 4c 65  US ) pLeft = pLe
1dd67 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 4c 65  ft->pLeft;.  pLe
1dd68 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  ft->flags |= EP_
1dd69 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 4c  AnyAff;.  *pRegL
1dd6a 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  eft = sqlite3Exp
1dd6b 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
1dd6c 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 4c 65  , pLeft, pFreeLe
1dd6d 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 52  ft);.  while( pR
1dd6e 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c  ight->op==TK_UPL
1dd6f 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 70 52  US ) pRight = pR
1dd70 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70  ight->pLeft;.  p
1dd71 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c 3d 20  Right->flags |= 
1dd72 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52  EP_AnyAff;.  *pR
1dd73 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65  egRight = sqlite
1dd74 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1dd75 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 70 46  arse, pRight, pF
1dd76 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a  reeRight);.}../*
1dd77 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1dd78 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73  e for a comparis
1dd79 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a  on operator..*/.
1dd7a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 43  static int codeC
1dd7b 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 65 20  ompare(.  Parse 
1dd7c 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 54  *pParse,    /* T
1dd7d 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e 64 20  he parsing (and 
1dd7e 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 29  code generating)
1dd7f 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
1dd80 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20  pr *pLeft,      
1dd81 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72  /* The left oper
1dd82 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  and */.  Expr *p
1dd83 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 54 68  Right,     /* Th
1dd84 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  e right operand 
1dd85 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65 2c  */.  int opcode,
1dd86 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
1dd87 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 65 20  mparison opcode 
1dd88 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 69 6e  */.  int in1, in
1dd89 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 73 74  t in2, /* Regist
1dd8a 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 72 61  er holding opera
1dd8b 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73  nds */.  int des
1dd8c 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  t,         /* Ju
1dd8d 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 65 2e  mp here if true.
1dd8e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70 49    */.  int jumpI
1dd8f 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 20 74  fNull    /* If t
1dd90 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 69 74  rue, jump if eit
1dd91 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e  her operand is N
1dd92 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ULL */.){.  int 
1dd93 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a  p5;.  int addr;.
1dd94 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a    CollSeq *p4;..
1dd95 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 42 69    p4 = sqlite3Bi
1dd96 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53  naryCompareCollS
1dd97 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  eq(pParse, pLeft
1dd98 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 35 20  , pRight);.  p5 
1dd99 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50  = binaryCompareP
1dd9a 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  5(pLeft, pRight,
1dd9b 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
1dd9c 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1dd9d 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 65 2d  beAddOp4(pParse-
1dd9e 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 2c 20  >pVdbe, opcode, 
1dd9f 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 2c 0a  in2, dest, in1,.
1dda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dda1 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64             (void
1dda2 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 45 51  *)p4, P4_COLLSEQ
1dda3 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1dda4 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 65 2d  ChangeP5(pParse-
1dda5 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 29 3b  >pVdbe, (u8)p5);
1dda6 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 51 4c  .  if( (p5 & SQL
1dda7 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 3d 53  ITE_AFF_MASK)!=S
1dda8 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29  QLITE_AFF_NONE )
1dda9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
1ddaa 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
1ddab 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 31  ange(pParse, in1
1ddac 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
1ddad 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
1ddae 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
1ddaf 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a 20 20   in2, 1);.  }.  
1ddb0 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a  return addr;.}..
1ddb1 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45  #if SQLITE_MAX_E
1ddb2 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a 2a  XPR_DEPTH>0./*.*
1ddb3 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 72 67  * Check that arg
1ddb4 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 69 73  ument nHeight is
1ddb5 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
1ddb6 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 6d  ual to the maxim
1ddb7 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  um.** expression
1ddb8 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 2e 20   depth allowed. 
1ddb9 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 6c 65  If it is not, le
1ddba 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
1ddbb 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 72 73  sage in.** pPars
1ddbc 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1ddbd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1ddbe 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28  ExprCheckHeight(
1ddbf 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
1ddc0 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 20 69  nt nHeight){.  i
1ddc1 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1ddc2 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 67 68  K;.  int mxHeigh
1ddc3 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  t = pParse->db->
1ddc4 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1ddc5 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d 3b  MIT_EXPR_DEPTH];
1ddc6 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e 6d  .  if( nHeight>m
1ddc7 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 73  xHeight ){.    s
1ddc8 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1ddc9 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 22  Parse, .       "
1ddca 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  Expression tree 
1ddcb 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 6d 61  is too large (ma
1ddcc 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 29 22  ximum depth %d)"
1ddcd 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 20 29  , mxHeight.    )
1ddce 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1ddcf 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72  E_ERROR;.  }.  r
1ddd0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
1ddd1 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  The following th
1ddd2 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68  ree functions, h
1ddd3 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20 68  eightOfExpr(), h
1ddd4 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28  eightOfExprList(
1ddd5 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 4f  ).** and heightO
1ddd6 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20 75  fSelect(), are u
1ddd7 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
1ddd8 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69   the maximum hei
1ddd9 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78  ght.** of any ex
1ddda 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65  pression tree re
1dddb 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 20  ferenced by the 
1dddc 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64  structure passed
1dddd 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74   as the.** first
1ddde 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
1dddf 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 6d   If this maximum
1dde0 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74   height is great
1dde1 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72  er than the curr
1dde2 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 65  ent value pointe
1dde3 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 69  d.** to by pnHei
1dde4 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  ght, the second 
1dde5 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20  parameter, then 
1dde6 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 6f  set *pnHeight to
1dde7 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a   that.** value..
1dde8 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68  */.static void h
1dde9 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 70 72  eightOfExpr(Expr
1ddea 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67   *p, int *pnHeig
1ddeb 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  ht){.  if( p ){.
1ddec 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 67      if( p->nHeig
1dded 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a  ht>*pnHeight ){.
1ddee 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74 20        *pnHeight 
1ddef 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20  = p->nHeight;.  
1ddf0 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63    }.  }.}.static
1ddf1 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78   void heightOfEx
1ddf2 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20  prList(ExprList 
1ddf3 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68  *p, int *pnHeigh
1ddf4 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  t){.  if( p ){. 
1ddf5 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
1ddf6 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70  r(i=0; i<p->nExp
1ddf7 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68  r; i++){.      h
1ddf8 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 61  eightOfExpr(p->a
1ddf9 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 69  [i].pExpr, pnHei
1ddfa 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ght);.    }.  }.
1ddfb 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65  }.static void he
1ddfc 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65 6c  ightOfSelect(Sel
1ddfd 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48  ect *p, int *pnH
1ddfe 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20  eight){.  if( p 
1ddff 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45  ){.    heightOfE
1de00 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 70  xpr(p->pWhere, p
1de01 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
1de02 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 48  ightOfExpr(p->pH
1de03 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 29  aving, pnHeight)
1de04 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  ;.    heightOfEx
1de05 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e  pr(p->pLimit, pn
1de06 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69  Height);.    hei
1de07 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66  ghtOfExpr(p->pOf
1de08 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29 3b  fset, pnHeight);
1de09 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70  .    heightOfExp
1de0a 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c  rList(p->pEList,
1de0b 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20   pnHeight);.    
1de0c 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74  heightOfExprList
1de0d 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e  (p->pGroupBy, pn
1de0e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69  Height);.    hei
1de0f 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d  ghtOfExprList(p-
1de10 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 69  >pOrderBy, pnHei
1de11 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74  ght);.    height
1de12 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 69  OfSelect(p->pPri
1de13 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20  or, pnHeight);. 
1de14 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20   }.}../*.** Set 
1de15 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74  the Expr.nHeight
1de16 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65   variable in the
1de17 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65   structure passe
1de18 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75  d as an .** argu
1de19 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 73  ment. An express
1de1a 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 6c  ion with no chil
1de1b 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74  dren, Expr.pList
1de1c 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65   or .** Expr.pSe
1de1d 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73 20  lect member has 
1de1e 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20 41  a height of 1. A
1de1f 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 73 73  ny other express
1de20 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 69  ion.** has a hei
1de21 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68 65  ght equal to the
1de22 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20   maximum height 
1de23 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a  of any other .**
1de24 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 70 72   referenced Expr
1de25 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 74   plus one..*/.st
1de26 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 53 65  atic void exprSe
1de27 74 48 65 69 67 68 74 28 45 78 70 72 20 2a 70 29  tHeight(Expr *p)
1de28 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20  {.  int nHeight 
1de29 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 45  = 0;.  heightOfE
1de2a 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e  xpr(p->pLeft, &n
1de2b 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 67 68  Height);.  heigh
1de2c 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 67 68  tOfExpr(p->pRigh
1de2d 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20  t, &nHeight);.  
1de2e 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74  heightOfExprList
1de2f 28 70 2d 3e 70 4c 69 73 74 2c 20 26 6e 48 65 69  (p->pList, &nHei
1de30 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66  ght);.  heightOf
1de31 53 65 6c 65 63 74 28 70 2d 3e 70 53 65 6c 65 63  Select(p->pSelec
1de32 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20  t, &nHeight);.  
1de33 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65  p->nHeight = nHe
1de34 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a  ight + 1;.}../*.
1de35 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e  ** Set the Expr.
1de36 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65  nHeight variable
1de37 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 53   using the exprS
1de38 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 74  etHeight() funct
1de39 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 68  ion. If.** the h
1de3a 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72  eight is greater
1de3b 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75   than the maximu
1de3c 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 73  m allowed expres
1de3d 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 6c  sion depth,.** l
1de3e 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e  eave an error in
1de3f 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49   pParse..*/.SQLI
1de40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1de41 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
1de42 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72  ight(Parse *pPar
1de43 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20  se, Expr *p){.  
1de44 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 29  exprSetHeight(p)
1de45 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
1de46 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73  heckHeight(pPars
1de47 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b 0a  e, p->nHeight);.
1de48 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1de49 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67  the maximum heig
1de4a 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73  ht of any expres
1de4b 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65  sion tree refere
1de4c 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73  nced.** by the s
1de4d 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
1de4e 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
1de4f 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ument..*/.SQLITE
1de50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1de51 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65  ite3SelectExprHe
1de52 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b  ight(Select *p){
1de53 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d  .  int nHeight =
1de54 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65   0;.  heightOfSe
1de55 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 74  lect(p, &nHeight
1de56 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 69  );.  return nHei
1de57 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23  ght;.}.#else.  #
1de58 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 65  define exprSetHe
1de59 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 2f  ight(y).#endif /
1de5a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  * SQLITE_MAX_EXP
1de5b 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a  R_DEPTH>0 */../*
1de5c 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20  .** Construct a 
1de5d 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  new expression n
1de5e 6f 64 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ode and return a
1de5f 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
1de60 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f 72 20 74   Memory.** for t
1de61 68 69 73 20 6e 6f 64 65 20 69 73 20 6f 62 74 61  his node is obta
1de62 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
1de63 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65  3_malloc().  The
1de64 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
1de65 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69  n.** is responsi
1de66 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73  ble for making s
1de67 75 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65  ure the node eve
1de68 6e 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65  ntually gets fre
1de69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1de6a 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
1de6b 74 65 33 45 78 70 72 28 0a 20 20 73 71 6c 69 74  te3Expr(.  sqlit
1de6c 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
1de6d 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72     /* Handle for
1de6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1de6f 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e  Zero() (may be n
1de70 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  ull) */.  int op
1de71 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1de72 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
1de73 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72  opcode */.  Expr
1de74 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20   *pLeft,        
1de75 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72      /* Left oper
1de76 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  and */.  Expr *p
1de77 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20  Right,          
1de78 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e   /* Right operan
1de79 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b  d */.  const Tok
1de7a 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f  en *pToken     /
1de7b 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e  * Argument token
1de7c 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
1de7d 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  New;.  pNew = sq
1de7e 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
1de7f 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70  o(db, sizeof(Exp
1de80 72 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d  r));.  if( pNew=
1de81 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65  =0 ){.    /* Whe
1de82 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20  n malloc fails, 
1de83 64 65 6c 65 74 65 20 70 4c 65 66 74 20 61 6e 64  delete pLeft and
1de84 20 70 52 69 67 68 74 2e 20 45 78 70 72 65 73 73   pRight. Express
1de85 69 6f 6e 73 20 70 61 73 73 65 64 20 74 6f 20 0a  ions passed to .
1de86 20 20 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63      ** this func
1de87 74 69 6f 6e 20 6d 75 73 74 20 61 6c 77 61 79 73  tion must always
1de88 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 77 69   be allocated wi
1de89 74 68 20 73 71 6c 69 74 65 33 45 78 70 72 28 29  th sqlite3Expr()
1de8a 20 66 6f 72 20 74 68 69 73 20 0a 20 20 20 20 2a   for this .    *
1de8b 2a 20 72 65 61 73 6f 6e 2e 20 0a 20 20 20 20 2a  * reason. .    *
1de8c 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  /.    sqlite3Exp
1de8d 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 65 66  rDelete(db, pLef
1de8e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  t);.    sqlite3E
1de8f 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 52  xprDelete(db, pR
1de90 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72  ight);.    retur
1de91 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d  n 0;.  }.  pNew-
1de92 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20  >op = (u8)op;.  
1de93 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70 4c  pNew->pLeft = pL
1de94 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69  eft;.  pNew->pRi
1de95 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20  ght = pRight;.  
1de96 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b  pNew->iAgg = -1;
1de97 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20  .  pNew->span.z 
1de98 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 69 66 28  = (u8*)"";.  if(
1de99 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 61   pToken ){.    a
1de9a 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 64  ssert( pToken->d
1de9b 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 70 4e 65  yn==0 );.    pNe
1de9c 77 2d 3e 73 70 61 6e 20 3d 20 70 4e 65 77 2d 3e  w->span = pNew->
1de9d 74 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b  token = *pToken;
1de9e 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65  .  }else if( pLe
1de9f 66 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52  ft ){.    if( pR
1dea0 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66  ight ){.      if
1dea1 28 20 70 52 69 67 68 74 2d 3e 73 70 61 6e 2e 64  ( pRight->span.d
1dea2 79 6e 3d 3d 30 20 26 26 20 70 4c 65 66 74 2d 3e  yn==0 && pLeft->
1dea3 73 70 61 6e 2e 64 79 6e 3d 3d 30 20 29 7b 0a 20  span.dyn==0 ){. 
1dea4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
1dea5 70 72 53 70 61 6e 28 70 4e 65 77 2c 20 26 70 4c  prSpan(pNew, &pL
1dea6 65 66 74 2d 3e 73 70 61 6e 2c 20 26 70 52 69 67  eft->span, &pRig
1dea7 68 74 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20  ht->span);.     
1dea8 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 52 69   }.      if( pRi
1dea9 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  ght->flags & EP_
1deaa 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20  ExpCollate ){.  
1deab 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67        pNew->flag
1deac 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61  s |= EP_ExpColla
1dead 74 65 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  te;.        pNew
1deae 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68 74  ->pColl = pRight
1deaf 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d  ->pColl;.      }
1deb0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1deb1 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50  Left->flags & EP
1deb2 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20  _ExpCollate ){. 
1deb3 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73       pNew->flags
1deb4 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74   |= EP_ExpCollat
1deb5 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  e;.      pNew->p
1deb6 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43  Coll = pLeft->pC
1deb7 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  oll;.    }.  }..
1deb8 20 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28    exprSetHeight(
1deb9 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20  pNew);.  return 
1deba 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  pNew;.}../*.** W
1debb 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65  orks like sqlite
1debc 33 45 78 70 72 28 29 20 65 78 63 65 70 74 20 74  3Expr() except t
1debd 68 61 74 20 69 74 20 74 61 6b 65 73 20 61 6e 20  hat it takes an 
1debe 65 78 74 72 61 20 50 61 72 73 65 2a 0a 2a 2a 20  extra Parse*.** 
1debf 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 6e 6f 74  argument and not
1dec0 69 66 69 65 73 20 74 68 65 20 61 73 73 6f 63 69  ifies the associ
1dec1 61 74 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ated connection 
1dec2 6f 62 6a 65 63 74 20 69 66 20 6d 61 6c 6c 6f 63  object if malloc
1dec3 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54   fails..*/.SQLIT
1dec4 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
1dec5 73 71 6c 69 74 65 33 50 45 78 70 72 28 0a 20 20  sqlite3PExpr(.  
1dec6 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1dec7 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
1dec8 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1dec9 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
1deca 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
1decb 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a  ssion opcode */.
1decc 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20    Expr *pLeft,  
1decd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66            /* Lef
1dece 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45  t operand */.  E
1decf 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20  xpr *pRight,    
1ded0 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20         /* Right 
1ded1 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e  operand */.  con
1ded2 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  st Token *pToken
1ded3 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
1ded4 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45   token */.){.  E
1ded5 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  xpr *p = sqlite3
1ded6 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64 62 2c  Expr(pParse->db,
1ded7 20 6f 70 2c 20 70 4c 65 66 74 2c 20 70 52 69 67   op, pLeft, pRig
1ded8 68 74 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69  ht, pToken);.  i
1ded9 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( p ){.    sqli
1deda 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67  te3ExprCheckHeig
1dedb 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48  ht(pParse, p->nH
1dedc 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 72 65  eight);.  }.  re
1dedd 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
1dede 20 57 68 65 6e 20 64 6f 69 6e 67 20 61 20 6e 65   When doing a ne
1dedf 73 74 65 64 20 70 61 72 73 65 2c 20 79 6f 75 20  sted parse, you 
1dee0 63 61 6e 20 69 6e 63 6c 75 64 65 20 74 65 72 6d  can include term
1dee1 73 20 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69  s in an expressi
1dee2 6f 6e 0a 2a 2a 20 74 68 61 74 20 6c 6f 6f 6b 20  on.** that look 
1dee3 6c 69 6b 65 20 74 68 69 73 3a 20 20 20 23 31 20  like this:   #1 
1dee4 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20 74 65  #2 ...  These te
1dee5 72 6d 73 20 72 65 66 65 72 20 74 6f 20 72 65 67  rms refer to reg
1dee6 69 73 74 65 72 73 0a 2a 2a 20 69 6e 20 74 68 65  isters.** in the
1dee7 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1dee8 2e 20 20 23 4e 20 69 73 20 74 68 65 20 4e 2d 74  .  #N is the N-t
1dee9 68 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a  h register..**.*
1deea 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1deeb 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
1deec 70 61 72 73 65 72 20 74 6f 20 64 65 61 6c 20 77  parser to deal w
1deed 69 74 68 20 6f 6e 20 6f 66 20 74 68 6f 73 65 20  ith on of those 
1deee 74 65 72 6d 73 2e 0a 2a 2a 20 49 74 20 69 6d 6d  terms..** It imm
1deef 65 64 69 61 74 65 6c 79 20 67 65 6e 65 72 61 74  ediately generat
1def0 65 73 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65  es code to store
1def1 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 61 20   the value in a 
1def2 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 2e  memory location.
1def3 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 73 20  .** The returns 
1def4 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  an expression th
1def5 61 74 20 77 69 6c 6c 20 63 6f 64 65 20 74 6f 20  at will code to 
1def6 65 78 74 72 61 63 74 20 74 68 65 20 76 61 6c 75  extract the valu
1def7 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 61 74 20 6d  e from.** that m
1def8 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 61  emory location a
1def9 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c  s needed..*/.SQL
1defa 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
1defb 20 2a 73 71 6c 69 74 65 33 52 65 67 69 73 74 65   *sqlite3Registe
1defc 72 45 78 70 72 28 50 61 72 73 65 20 2a 70 50 61  rExpr(Parse *pPa
1defd 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  rse, Token *pTok
1defe 65 6e 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  en){.  Vdbe *v =
1deff 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
1df00 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 69 66 28    Expr *p;.  if(
1df01 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
1df02 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1df03 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1df04 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20  , "near \"%T\": 
1df05 73 79 6e 74 61 78 20 65 72 72 6f 72 22 2c 20 70  syntax error", p
1df06 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 72 65 74 75  Token);.    retu
1df07 72 6e 20 73 71 6c 69 74 65 33 50 45 78 70 72 28  rn sqlite3PExpr(
1df08 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c 4c 2c  pParse, TK_NULL,
1df09 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20   0, 0, 0);.  }. 
1df0a 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
1df0b 72 6e 20 30 3b 0a 20 20 70 20 3d 20 73 71 6c 69  rn 0;.  p = sqli
1df0c 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
1df0d 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 30 2c   TK_REGISTER, 0,
1df0e 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69   0, pToken);.  i
1df0f 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72  f( p==0 ){.    r
1df10 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4d 61 6c  eturn 0;  /* Mal
1df11 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20  loc failed */.  
1df12 7d 0a 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20  }.  p->iTable = 
1df13 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70 54 6f  atoi((char*)&pTo
1df14 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20 72 65  ken->z[1]);.  re
1df15 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
1df16 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72 65 73   Join two expres
1df17 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e 20 41  sions using an A
1df18 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20 49 66  ND operator.  If
1df19 20 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69   either expressi
1df1a 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74  on is.** NULL, t
1df1b 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e 20  hen just return 
1df1c 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 65 73  the other expres
1df1d 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sion..*/.SQLITE_
1df1e 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
1df1f 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c  lite3ExprAnd(sql
1df20 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a  ite3 *db, Expr *
1df21 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70 52 69  pLeft, Expr *pRi
1df22 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c 65 66  ght){.  if( pLef
1df23 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  t==0 ){.    retu
1df24 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d 65 6c  rn pRight;.  }el
1df25 73 65 20 69 66 28 20 70 52 69 67 68 74 3d 3d 30  se if( pRight==0
1df26 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
1df27 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Left;.  }else{. 
1df28 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
1df29 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 41 4e 44  3Expr(db, TK_AND
1df2a 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  , pLeft, pRight,
1df2b 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   0);.  }.}../*.*
1df2c 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e 73  * Set the Expr.s
1df2d 70 61 6e 20 66 69 65 6c 64 20 6f 66 20 74 68 65  pan field of the
1df2e 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f   given expressio
1df2f 6e 20 74 6f 20 73 70 61 6e 20 61 6c 6c 0a 2a 2a  n to span all.**
1df30 20 74 65 78 74 20 62 65 74 77 65 65 6e 20 74 68   text between th
1df31 65 20 74 77 6f 20 67 69 76 65 6e 20 74 6f 6b 65  e two given toke
1df32 6e 73 2e 20 20 42 6f 74 68 20 74 6f 6b 65 6e 73  ns.  Both tokens
1df33 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e   must be pointin
1df34 67 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65  g.** at the same
1df35 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49   string..*/.SQLI
1df36 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1df37 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
1df38 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54 6f 6b  Expr *pExpr, Tok
1df39 65 6e 20 2a 70 4c 65 66 74 2c 20 54 6f 6b 65 6e  en *pLeft, Token
1df3a 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 73 73   *pRight){.  ass
1df3b 65 72 74 28 20 70 52 69 67 68 74 21 3d 30 20 29  ert( pRight!=0 )
1df3c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66  ;.  assert( pLef
1df3d 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 45  t!=0 );.  if( pE
1df3e 78 70 72 20 29 7b 0a 20 20 20 20 70 45 78 70 72  xpr ){.    pExpr
1df3f 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 4c 65 66 74  ->span.z = pLeft
1df40 2d 3e 7a 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e  ->z;.    pExpr->
1df41 73 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68 74 2d  span.n = pRight-
1df42 3e 6e 20 2b 20 28 70 52 69 67 68 74 2d 3e 7a 20  >n + (pRight->z 
1df43 2d 20 70 4c 65 66 74 2d 3e 7a 29 3b 0a 20 20 7d  - pLeft->z);.  }
1df44 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72  .}../*.** Constr
1df45 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 65 73  uct a new expres
1df46 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20  sion node for a 
1df47 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d 75  function with mu
1df48 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75 6d 65  ltiple.** argume
1df49 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts..*/.SQLITE_P
1df4a 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
1df4b 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
1df4c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1df4d 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
1df4e 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b   Token *pToken){
1df4f 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20  .  Expr *pNew;. 
1df50 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1df51 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73  Parse->db;.  ass
1df52 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a 20  ert( pToken );. 
1df53 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
1df54 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1df55 73 69 7a 65 6f 66 28 45 78 70 72 29 20 29 3b 0a  sizeof(Expr) );.
1df56 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
1df57 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
1df58 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
1df59 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69 64 20  List); /* Avoid 
1df5a 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 77  leaking memory w
1df5b 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  hen malloc fails
1df5c 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30   */.    return 0
1df5d 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 6f 70  ;.  }.  pNew->op
1df5e 20 3d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3b 0a   = TK_FUNCTION;.
1df5f 20 20 70 4e 65 77 2d 3e 70 4c 69 73 74 20 3d 20    pNew->pList = 
1df60 70 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28  pList;.  assert(
1df61 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d 30 20   pToken->dyn==0 
1df62 29 3b 0a 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e  );.  pNew->token
1df63 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 70 4e   = *pToken;.  pN
1df64 65 77 2d 3e 73 70 61 6e 20 3d 20 70 4e 65 77 2d  ew->span = pNew-
1df65 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 73 71 6c 69 74  >token;..  sqlit
1df66 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
1df67 70 50 61 72 73 65 2c 20 70 4e 65 77 29 3b 0a 20  pParse, pNew);. 
1df68 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
1df69 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20  ./*.** Assign a 
1df6a 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20  variable number 
1df6b 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  to an expression
1df6c 20 74 68 61 74 20 65 6e 63 6f 64 65 73 20 61 20   that encodes a 
1df6d 77 69 6c 64 63 61 72 64 0a 2a 2a 20 69 6e 20 74  wildcard.** in t
1df6e 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  he original SQL 
1df6f 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a  statement.  .**.
1df70 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 63 6f 6e  ** Wildcards con
1df71 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 69 6e  sisting of a sin
1df72 67 6c 65 20 22 3f 22 20 61 72 65 20 61 73 73 69  gle "?" are assi
1df73 67 6e 65 64 20 74 68 65 20 6e 65 78 74 20 73 65  gned the next se
1df74 71 75 65 6e 74 69 61 6c 0a 2a 2a 20 76 61 72 69  quential.** vari
1df75 61 62 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a  able number..**.
1df76 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20  ** Wildcards of 
1df77 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20  the form "?nnn" 
1df78 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65  are assigned the
1df79 20 6e 75 6d 62 65 72 20 22 6e 6e 6e 22 2e 20 20   number "nnn".  
1df7a 57 65 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20  We make.** sure 
1df7b 22 6e 6e 6e 22 20 69 73 20 6e 6f 74 20 74 6f 6f  "nnn" is not too
1df7c 20 62 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64   be to avoid a d
1df7d 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65  enial of service
1df7e 20 61 74 74 61 63 6b 20 77 68 65 6e 0a 2a 2a 20   attack when.** 
1df7f 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
1df80 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 6e 20  t comes from an 
1df81 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 2e  external source.
1df82 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73  .**.** Wildcards
1df83 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3a 61   of the form ":a
1df84 61 61 22 20 6f 72 20 22 24 61 61 61 22 20 61 72  aa" or "$aaa" ar
1df85 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 73  e assigned the s
1df86 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 61 73  ame number.** as
1df87 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e   the previous in
1df88 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61  stance of the sa
1df89 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 4f 72  me wildcard.  Or
1df8a 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20   if this is the 
1df8b 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 6e 63  first.** instanc
1df8c 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 61 72  e of the wildcar
1df8d 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 71 75  d, the next sequ
1df8e 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 20 6e  enial variable n
1df8f 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 73 69  umber is.** assi
1df90 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gned..*/.SQLITE_
1df91 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1df92 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61  ite3ExprAssignVa
1df93 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 2a 70  rNumber(Parse *p
1df94 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
1df95 70 72 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  pr){.  Token *pT
1df96 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20  oken;.  sqlite3 
1df97 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1df98 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d  ;..  if( pExpr==
1df99 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 54  0 ) return;.  pT
1df9a 6f 6b 65 6e 20 3d 20 26 70 45 78 70 72 2d 3e 74  oken = &pExpr->t
1df9b 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  oken;.  assert( 
1df9c 70 54 6f 6b 65 6e 2d 3e 6e 3e 3d 31 20 29 3b 0a  pToken->n>=1 );.
1df9d 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e    assert( pToken
1df9e 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ->z!=0 );.  asse
1df9f 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d  rt( pToken->z[0]
1dfa0 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 54 6f  !=0 );.  if( pTo
1dfa1 6b 65 6e 2d 3e 6e 3d 3d 31 20 29 7b 0a 20 20 20  ken->n==1 ){.   
1dfa2 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20   /* Wildcard of 
1dfa3 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20 20 41  the form "?".  A
1dfa4 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74 20 76  ssign the next v
1dfa5 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a  ariable number *
1dfa6 2f 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61  /.    pExpr->iTa
1dfa7 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  ble = ++pParse->
1dfa8 6e 56 61 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66  nVar;.  }else if
1dfa9 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d 3d 3d  ( pToken->z[0]==
1dfaa 27 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20 57 69  '?' ){.    /* Wi
1dfab 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66 6f  ldcard of the fo
1dfac 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76  rm "?nnn".  Conv
1dfad 65 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61 6e 20  ert "nnn" to an 
1dfae 69 6e 74 65 67 65 72 20 61 6e 64 0a 20 20 20 20  integer and.    
1dfaf 2a 2a 20 75 73 65 20 69 74 20 61 73 20 74 68 65  ** use it as the
1dfb0 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72   variable number
1dfb1 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   */.    int i;. 
1dfb2 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1dfb3 20 3d 20 69 20 3d 20 61 74 6f 69 28 28 63 68 61   = i = atoi((cha
1dfb4 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d  r*)&pToken->z[1]
1dfb5 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
1dfb6 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65 73   i==0 );.    tes
1dfb7 74 63 61 73 65 28 20 69 3d 3d 31 20 29 3b 0a 20  tcase( i==1 );. 
1dfb8 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
1dfb9 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1dfba 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45  E_LIMIT_VARIABLE
1dfbb 5f 4e 55 4d 42 45 52 5d 2d 31 20 29 3b 0a 20 20  _NUMBER]-1 );.  
1dfbc 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 64    testcase( i==d
1dfbd 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
1dfbe 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f  _LIMIT_VARIABLE_
1dfbf 4e 55 4d 42 45 52 5d 20 29 3b 0a 20 20 20 20 69  NUMBER] );.    i
1dfc0 66 28 20 69 3c 31 20 7c 7c 20 69 3e 64 62 2d 3e  f( i<1 || i>db->
1dfc1 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1dfc2 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
1dfc3 42 45 52 5d 20 29 7b 0a 20 20 20 20 20 20 73 71  BER] ){.      sq
1dfc4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1dfc5 61 72 73 65 2c 20 22 76 61 72 69 61 62 6c 65 20  arse, "variable 
1dfc6 6e 75 6d 62 65 72 20 6d 75 73 74 20 62 65 20 62  number must be b
1dfc7 65 74 77 65 65 6e 20 3f 31 20 61 6e 64 20 3f 25  etween ?1 and ?%
1dfc8 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 64 62  d",.          db
1dfc9 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1dfca 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
1dfcb 55 4d 42 45 52 5d 29 3b 0a 20 20 20 20 7d 0a 20  UMBER]);.    }. 
1dfcc 20 20 20 69 66 28 20 69 3e 70 50 61 72 73 65 2d     if( i>pParse-
1dfcd 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 20 20 70  >nVar ){.      p
1dfce 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 69 3b  Parse->nVar = i;
1dfcf 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
1dfd0 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 73      /* Wildcards
1dfd1 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3a 61   of the form ":a
1dfd2 61 61 22 20 6f 72 20 22 24 61 61 61 22 2e 20 20  aa" or "$aaa".  
1dfd3 52 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 76  Reuse the same v
1dfd4 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 6e  ariable.    ** n
1dfd5 75 6d 62 65 72 20 61 73 20 74 68 65 20 70 72 69  umber as the pri
1dfd6 6f 72 20 61 70 70 65 61 72 61 6e 63 65 20 6f 66  or appearance of
1dfd7 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20   the same name, 
1dfd8 6f 72 20 69 66 20 74 68 65 20 6e 61 6d 65 0a 20  or if the name. 
1dfd9 20 20 20 2a 2a 20 68 61 73 20 6e 65 76 65 72 20     ** has never 
1dfda 61 70 70 65 61 72 65 64 20 62 65 66 6f 72 65 2c  appeared before,
1dfdb 20 72 65 75 73 65 20 74 68 65 20 73 61 6d 65 20   reuse the same 
1dfdc 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 0a  variable number.
1dfdd 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69      */.    int i
1dfde 3b 0a 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 20  ;.    u32 n;.   
1dfdf 20 6e 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e 3b 0a   n = pToken->n;.
1dfe0 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1dfe1 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b  Parse->nVarExpr;
1dfe2 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70   i++){.      Exp
1dfe3 72 20 2a 70 45 3b 0a 20 20 20 20 20 20 69 66 28  r *pE;.      if(
1dfe4 20 28 70 45 20 3d 20 70 50 61 72 73 65 2d 3e 61   (pE = pParse->a
1dfe5 70 56 61 72 45 78 70 72 5b 69 5d 29 21 3d 30 0a  pVarExpr[i])!=0.
1dfe6 20 20 20 20 20 20 20 20 20 20 26 26 20 70 45 2d            && pE-
1dfe7 3e 74 6f 6b 65 6e 2e 6e 3d 3d 6e 0a 20 20 20 20  >token.n==n.    
1dfe8 20 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28        && memcmp(
1dfe9 70 45 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 54 6f  pE->token.z, pTo
1dfea 6b 65 6e 2d 3e 7a 2c 20 6e 29 3d 3d 30 20 29 7b  ken->z, n)==0 ){
1dfeb 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
1dfec 69 54 61 62 6c 65 20 3d 20 70 45 2d 3e 69 54 61  iTable = pE->iTa
1dfed 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ble;.        bre
1dfee 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1dfef 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d 70 50 61  }.    if( i>=pPa
1dff0 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 20 29 7b  rse->nVarExpr ){
1dff1 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54  .      pExpr->iT
1dff2 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d  able = ++pParse-
1dff3 3e 6e 56 61 72 3b 0a 20 20 20 20 20 20 69 66 28  >nVar;.      if(
1dff4 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70   pParse->nVarExp
1dff5 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  r>=pParse->nVarE
1dff6 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20  xprAlloc-1 ){.  
1dff7 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56        pParse->nV
1dff8 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d 20 70  arExprAlloc += p
1dff9 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41  Parse->nVarExprA
1dffa 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20 20 20  lloc + 10;.     
1dffb 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72     pParse->apVar
1dffc 45 78 70 72 20 3d 0a 20 20 20 20 20 20 20 20 20  Expr =.         
1dffd 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c     sqlite3DbReal
1dffe 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20 20 20 20  locOrFree(.     
1dfff 20 20 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20           db,.   
1e000 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
1e001 65 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a 20 20  e->apVarExpr,.  
1e002 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72              pPar
1e003 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f  se->nVarExprAllo
1e004 63 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d  c*sizeof(pParse-
1e005 3e 61 70 56 61 72 45 78 70 72 5b 30 5d 29 0a 20  >apVarExpr[0]). 
1e006 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20             );.  
1e007 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1e008 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
1e009 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  d ){.        ass
1e00a 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70 56  ert( pParse->apV
1e00b 61 72 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20  arExpr!=0 );.   
1e00c 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56       pParse->apV
1e00d 61 72 45 78 70 72 5b 70 50 61 72 73 65 2d 3e 6e  arExpr[pParse->n
1e00e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70 45 78  VarExpr++] = pEx
1e00f 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  pr;.      }.    
1e010 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 21 70 50  }.  } .  if( !pP
1e011 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 20 70 50  arse->nErr && pP
1e012 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61  arse->nVar>db->a
1e013 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1e014 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  IT_VARIABLE_NUMB
1e015 45 52 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ER] ){.    sqlit
1e016 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1e017 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 53 51 4c  e, "too many SQL
1e018 20 76 61 72 69 61 62 6c 65 73 22 29 3b 0a 20 20   variables");.  
1e019 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  }.}../*.** Clear
1e01a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73   an expression s
1e01b 74 72 75 63 74 75 72 65 20 77 69 74 68 6f 75 74  tructure without
1e01c 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 73 74   deleting the st
1e01d 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a  ructure itself..
1e01e 2a 2a 20 53 75 62 73 74 72 75 63 74 75 72 65 20  ** Substructure 
1e01f 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53  is deleted..*/.S
1e020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1e021 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c  id sqlite3ExprCl
1e022 65 61 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ear(sqlite3 *db,
1e023 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28   Expr *p){.  if(
1e024 20 70 2d 3e 73 70 61 6e 2e 64 79 6e 20 29 20 73   p->span.dyn ) s
1e025 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1e026 20 28 63 68 61 72 2a 29 70 2d 3e 73 70 61 6e 2e   (char*)p->span.
1e027 7a 29 3b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6b  z);.  if( p->tok
1e028 65 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33  en.dyn ) sqlite3
1e029 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 61 72  DbFree(db, (char
1e02a 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 20  *)p->token.z);. 
1e02b 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
1e02c 74 65 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29  te(db, p->pLeft)
1e02d 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
1e02e 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 69  elete(db, p->pRi
1e02f 67 68 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ght);.  sqlite3E
1e030 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
1e031 2c 20 70 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 73  , p->pList);.  s
1e032 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
1e033 74 65 28 64 62 2c 20 70 2d 3e 70 53 65 6c 65 63  te(db, p->pSelec
1e034 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63  t);.}../*.** Rec
1e035 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65 20  ursively delete 
1e036 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
1e037 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ee..*/.SQLITE_PR
1e038 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1e039 65 33 45 78 70 72 44 65 6c 65 74 65 28 73 71 6c  e3ExprDelete(sql
1e03a 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a  ite3 *db, Expr *
1e03b 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  p){.  if( p==0 )
1e03c 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
1e03d 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 2c 20  e3ExprClear(db, 
1e03e 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  p);.  sqlite3DbF
1e03f 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f  ree(db, p);.}../
1e040 2a 0a 2a 2a 20 54 68 65 20 45 78 70 72 2e 74 6f  *.** The Expr.to
1e041 6b 65 6e 20 66 69 65 6c 64 20 6d 69 67 68 74 20  ken field might 
1e042 62 65 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65  be a string lite
1e043 72 61 6c 20 74 68 61 74 20 69 73 20 71 75 6f 74  ral that is quot
1e044 65 64 2e 0a 2a 2a 20 49 66 20 73 6f 2c 20 72 65  ed..** If so, re
1e045 6d 6f 76 65 20 74 68 65 20 71 75 6f 74 61 74 69  move the quotati
1e046 6f 6e 20 6d 61 72 6b 73 2e 0a 2a 2f 0a 53 51 4c  on marks..*/.SQL
1e047 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1e048 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 45   sqlite3DequoteE
1e049 78 70 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  xpr(sqlite3 *db,
1e04a 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28   Expr *p){.  if(
1e04b 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65   ExprHasAnyPrope
1e04c 72 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f 74  rty(p, EP_Dequot
1e04d 65 64 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ed) ){.    retur
1e04e 6e 3b 0a 20 20 7d 0a 20 20 45 78 70 72 53 65 74  n;.  }.  ExprSet
1e04f 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 44  Property(p, EP_D
1e050 65 71 75 6f 74 65 64 29 3b 0a 20 20 69 66 28 20  equoted);.  if( 
1e051 70 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20  p->token.dyn==0 
1e052 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 6f  ){.    sqlite3To
1e053 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 2d 3e  kenCopy(db, &p->
1e054 74 6f 6b 65 6e 2c 20 26 70 2d 3e 74 6f 6b 65 6e  token, &p->token
1e055 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
1e056 44 65 71 75 6f 74 65 28 28 63 68 61 72 2a 29 70  Dequote((char*)p
1e057 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 7d 0a 0a 2f  ->token.z);.}../
1e058 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1e059 6e 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74  ng group of rout
1e05a 69 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 63  ines make deep c
1e05b 6f 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 73  opies of express
1e05c 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73  ions,.** express
1e05d 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c 69  ion lists, ID li
1e05e 73 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 20  sts, and select 
1e05f 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65  statements.  The
1e060 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62   copies can.** b
1e061 65 20 64 65 6c 65 74 65 64 20 28 62 79 20 62 65  e deleted (by be
1e062 69 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74 68  ing passed to th
1e063 65 69 72 20 72 65 73 70 65 63 74 69 76 65 20 2e  eir respective .
1e064 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74 69  ..Delete() routi
1e065 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20  nes).** without 
1e066 65 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f 72  effecting the or
1e067 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54  iginals..**.** T
1e068 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
1e069 73 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75 72  st, ID, and sour
1e06a 63 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e 20  ce lists return 
1e06b 62 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  by sqlite3ExprLi
1e06c 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69  stDup(),.** sqli
1e06d 74 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c 20  te3IdListDup(), 
1e06e 61 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  and sqlite3SrcLi
1e06f 73 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74 20  stDup() can not 
1e070 62 65 20 66 75 72 74 68 65 72 20 65 78 70 61 6e  be further expan
1e071 64 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73 65  ded .** by subse
1e072 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73  quent calls to s
1e073 71 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e 64  qlite*ListAppend
1e074 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a  () routines..**.
1e075 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74 68  ** Any tables th
1e076 61 74 20 74 68 65 20 53 72 63 4c 69 73 74 20 6d  at the SrcList m
1e077 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 72  ight point to ar
1e078 65 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64  e not duplicated
1e079 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e07a 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
1e07b 33 45 78 70 72 44 75 70 28 73 71 6c 69 74 65 33  3ExprDup(sqlite3
1e07c 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a   *db, Expr *p){.
1e07d 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20    Expr *pNew;.  
1e07e 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1e07f 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  n 0;.  pNew = sq
1e080 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
1e081 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20  (db, sizeof(*p) 
1e082 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
1e083 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d   ) return 0;.  m
1e084 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 73  emcpy(pNew, p, s
1e085 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20  izeof(*pNew));. 
1e086 20 69 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 21   if( p->token.z!
1e087 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e  =0 ){.    pNew->
1e088 74 6f 6b 65 6e 2e 7a 20 3d 20 28 75 38 2a 29 73  token.z = (u8*)s
1e089 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
1e08a 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 74 6f  db, (char*)p->to
1e08b 6b 65 6e 2e 7a 2c 20 70 2d 3e 74 6f 6b 65 6e 2e  ken.z, p->token.
1e08c 6e 29 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 74 6f  n);.    pNew->to
1e08d 6b 65 6e 2e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d  ken.dyn = 1;.  }
1e08e 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
1e08f 28 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 3d  ( pNew->token.z=
1e090 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 70 4e 65 77  =0 );.  }.  pNew
1e091 2d 3e 73 70 61 6e 2e 7a 20 3d 20 30 3b 0a 20 20  ->span.z = 0;.  
1e092 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 73 71  pNew->pLeft = sq
1e093 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
1e094 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 4e   p->pLeft);.  pN
1e095 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c  ew->pRight = sql
1e096 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
1e097 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 70 4e  p->pRight);.  pN
1e098 65 77 2d 3e 70 4c 69 73 74 20 3d 20 73 71 6c 69  ew->pList = sqli
1e099 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
1e09a 62 2c 20 70 2d 3e 70 4c 69 73 74 29 3b 0a 20 20  b, p->pList);.  
1e09b 70 4e 65 77 2d 3e 70 53 65 6c 65 63 74 20 3d 20  pNew->pSelect = 
1e09c 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
1e09d 28 64 62 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29  (db, p->pSelect)
1e09e 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  ;.  return pNew;
1e09f 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
1e0a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 6f  E void sqlite3To
1e0a1 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33 20  kenCopy(sqlite3 
1e0a2 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c  *db, Token *pTo,
1e0a3 20 54 6f 6b 65 6e 20 2a 70 46 72 6f 6d 29 7b 0a   Token *pFrom){.
1e0a4 20 20 69 66 28 20 70 54 6f 2d 3e 64 79 6e 20 29    if( pTo->dyn )
1e0a5 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1e0a6 62 2c 20 28 63 68 61 72 2a 29 70 54 6f 2d 3e 7a  b, (char*)pTo->z
1e0a7 29 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e  );.  if( pFrom->
1e0a8 7a 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 6e 20  z ){.    pTo->n 
1e0a9 3d 20 70 46 72 6f 6d 2d 3e 6e 3b 0a 20 20 20 20  = pFrom->n;.    
1e0aa 70 54 6f 2d 3e 7a 20 3d 20 28 75 38 2a 29 73 71  pTo->z = (u8*)sq
1e0ab 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64  lite3DbStrNDup(d
1e0ac 62 2c 20 28 63 68 61 72 2a 29 70 46 72 6f 6d 2d  b, (char*)pFrom-
1e0ad 3e 7a 2c 20 70 46 72 6f 6d 2d 3e 6e 29 3b 0a 20  >z, pFrom->n);. 
1e0ae 20 20 20 70 54 6f 2d 3e 64 79 6e 20 3d 20 31 3b     pTo->dyn = 1;
1e0af 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54  .  }else{.    pT
1e0b0 6f 2d 3e 7a 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  o->z = 0;.  }.}.
1e0b1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
1e0b2 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33  xprList *sqlite3
1e0b3 45 78 70 72 4c 69 73 74 44 75 70 28 73 71 6c 69  ExprListDup(sqli
1e0b4 74 65 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73  te3 *db, ExprLis
1e0b5 74 20 2a 70 29 7b 0a 20 20 45 78 70 72 4c 69 73  t *p){.  ExprLis
1e0b6 74 20 2a 70 4e 65 77 3b 0a 20 20 73 74 72 75 63  t *pNew;.  struc
1e0b7 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
1e0b8 2a 70 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 74 65  *pItem, *pOldIte
1e0b9 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  m;.  int i;.  if
1e0ba 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
1e0bb 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  0;.  pNew = sqli
1e0bc 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
1e0bd 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  b, sizeof(*pNew)
1e0be 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d   );.  if( pNew==
1e0bf 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
1e0c0 70 4e 65 77 2d 3e 69 45 43 75 72 73 6f 72 20 3d  pNew->iECursor =
1e0c1 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 78 70   0;.  pNew->nExp
1e0c2 72 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63  r = pNew->nAlloc
1e0c3 20 3d 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 20 70   = p->nExpr;.  p
1e0c4 4e 65 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 3d  New->a = pItem =
1e0c5 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1e0c6 52 61 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78 70  Raw(db,  p->nExp
1e0c7 72 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d  r*sizeof(p->a[0]
1e0c8 29 20 29 3b 0a 20 20 69 66 28 20 70 49 74 65 6d  ) );.  if( pItem
1e0c9 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1e0ca 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65  e3DbFree(db, pNe
1e0cb 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  w);.    return 0
1e0cc 3b 0a 20 20 7d 20 0a 20 20 70 4f 6c 64 49 74 65  ;.  } .  pOldIte
1e0cd 6d 20 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f 72 28  m = p->a;.  for(
1e0ce 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b  i=0; i<p->nExpr;
1e0cf 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c 20 70   i++, pItem++, p
1e0d0 4f 6c 64 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  OldItem++){.    
1e0d1 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 2c 20  Expr *pNewExpr, 
1e0d2 2a 70 4f 6c 64 45 78 70 72 3b 0a 20 20 20 20 70  *pOldExpr;.    p
1e0d3 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 4e  Item->pExpr = pN
1e0d4 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  ewExpr = sqlite3
1e0d5 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  ExprDup(db, pOld
1e0d6 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  Expr = pOldItem-
1e0d7 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28  >pExpr);.    if(
1e0d8 20 70 4f 6c 64 45 78 70 72 2d 3e 73 70 61 6e 2e   pOldExpr->span.
1e0d9 7a 21 3d 30 20 26 26 20 70 4e 65 77 45 78 70 72  z!=0 && pNewExpr
1e0da 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6c 77   ){.      /* Alw
1e0db 61 79 73 20 6d 61 6b 65 20 61 20 63 6f 70 79 20  ays make a copy 
1e0dc 6f 66 20 74 68 65 20 73 70 61 6e 20 66 6f 72 20  of the span for 
1e0dd 74 6f 70 2d 6c 65 76 65 6c 20 65 78 70 72 65 73  top-level expres
1e0de 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20 20  sions in the.   
1e0df 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e     ** expression
1e0e0 20 6c 69 73 74 2e 20 20 54 68 65 20 6c 6f 67 69   list.  The logi
1e0e1 63 20 69 6e 20 53 45 4c 45 43 54 20 70 72 6f 63  c in SELECT proc
1e0e2 65 73 73 69 6e 67 20 74 68 61 74 20 64 65 74 65  essing that dete
1e0e3 72 6d 69 6e 65 73 0a 20 20 20 20 20 20 2a 2a 20  rmines.      ** 
1e0e4 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 63 6f 6c  the names of col
1e0e5 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75  umns in the resu
1e0e6 6c 74 20 73 65 74 20 6e 65 65 64 73 20 74 68 69  lt set needs thi
1e0e7 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  s information */
1e0e8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 6f  .      sqlite3To
1e0e9 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 4e 65  kenCopy(db, &pNe
1e0ea 77 45 78 70 72 2d 3e 73 70 61 6e 2c 20 26 70 4f  wExpr->span, &pO
1e0eb 6c 64 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20  ldExpr->span);. 
1e0ec 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1e0ed 20 70 4e 65 77 45 78 70 72 3d 3d 30 20 7c 7c 20   pNewExpr==0 || 
1e0ee 70 4e 65 77 45 78 70 72 2d 3e 73 70 61 6e 2e 7a  pNewExpr->span.z
1e0ef 21 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20  !=0 .           
1e0f0 20 7c 7c 20 70 4f 6c 64 45 78 70 72 2d 3e 73 70   || pOldExpr->sp
1e0f1 61 6e 2e 7a 3d 3d 30 0a 20 20 20 20 20 20 20 20  an.z==0.        
1e0f2 20 20 20 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f      || db->mallo
1e0f3 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 70  cFailed );.    p
1e0f4 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71  Item->zName = sq
1e0f5 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
1e0f6 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d  , pOldItem->zNam
1e0f7 65 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73  e);.    pItem->s
1e0f8 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49  ortOrder = pOldI
1e0f9 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a  tem->sortOrder;.
1e0fa 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20      pItem->done 
1e0fb 3d 20 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  = 0;.    pItem->
1e0fc 69 43 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  iCol = pOldItem-
1e0fd 3e 69 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d  >iCol;.    pItem
1e0fe 2d 3e 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49  ->iAlias = pOldI
1e0ff 74 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d  tem->iAlias;.  }
1e100 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
1e101 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73  }../*.** If curs
1e102 6f 72 73 2c 20 74 72 69 67 67 65 72 73 2c 20 76  ors, triggers, v
1e103 69 65 77 73 20 61 6e 64 20 73 75 62 71 75 65 72  iews and subquer
1e104 69 65 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74  ies are all omit
1e105 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ted from.** the 
1e106 62 75 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65  build, then none
1e107 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
1e108 67 20 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 65  g routines, exce
1e109 70 74 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74  pt for .** sqlit
1e10a 65 33 53 65 6c 65 63 74 44 75 70 28 29 2c 20 63  e3SelectDup(), c
1e10b 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 71  an be called. sq
1e10c 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 29  lite3SelectDup()
1e10d 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a   is sometimes.**
1e10e 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e   called with a N
1e10f 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  ULL argument..*/
1e110 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1e111 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
1e112 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
1e113 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
1e114 20 5c 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28   \. || !defined(
1e115 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
1e116 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49  UERY).SQLITE_PRI
1e117 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71  VATE SrcList *sq
1e118 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28  lite3SrcListDup(
1e119 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63  sqlite3 *db, Src
1e11a 4c 69 73 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c  List *p){.  SrcL
1e11b 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74  ist *pNew;.  int
1e11c 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b   i;.  int nByte;
1e11d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
1e11e 74 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74 65 20  turn 0;.  nByte 
1e11f 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20 28  = sizeof(*p) + (
1e120 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69 7a 65  p->nSrc>0 ? size
1e121 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 28 70  of(p->a[0]) * (p
1e122 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29 3b 0a  ->nSrc-1) : 0);.
1e123 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
1e124 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
1e125 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 70  nByte );.  if( p
1e126 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  New==0 ) return 
1e127 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 53 72 63 20  0;.  pNew->nSrc 
1e128 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d  = pNew->nAlloc =
1e129 20 70 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f 72 28   p->nSrc;.  for(
1e12a 69 3d 30 3b 20 69 3c 70 2d 3e 6e 53 72 63 3b 20  i=0; i<p->nSrc; 
1e12b 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74  i++){.    struct
1e12c 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
1e12d 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d  NewItem = &pNew-
1e12e 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63  >a[i];.    struc
1e12f 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
1e130 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61  pOldItem = &p->a
1e131 5b 69 5d 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  [i];.    Table *
1e132 70 54 61 62 3b 0a 20 20 20 20 70 4e 65 77 49 74  pTab;.    pNewIt
1e133 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20  em->zDatabase = 
1e134 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1e135 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 44  db, pOldItem->zD
1e136 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 70 4e  atabase);.    pN
1e137 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20  ewItem->zName = 
1e138 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1e139 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e  db, pOldItem->zN
1e13a 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74  ame);.    pNewIt
1e13b 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c  em->zAlias = sql
1e13c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
1e13d 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c 69 61   pOldItem->zAlia
1e13e 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  s);.    pNewItem
1e13f 2d 3e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 4f 6c  ->jointype = pOl
1e140 64 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b  dItem->jointype;
1e141 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69  .    pNewItem->i
1e142 43 75 72 73 6f 72 20 3d 20 70 4f 6c 64 49 74 65  Cursor = pOldIte
1e143 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20  m->iCursor;.    
1e144 70 4e 65 77 49 74 65 6d 2d 3e 69 73 50 6f 70 75  pNewItem->isPopu
1e145 6c 61 74 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d  lated = pOldItem
1e146 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3b 0a 20  ->isPopulated;. 
1e147 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 49 6e     pNewItem->zIn
1e148 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 62 53  dex = sqlite3DbS
1e149 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74  trDup(db, pOldIt
1e14a 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20 20  em->zIndex);.   
1e14b 20 70 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74 49 6e   pNewItem->notIn
1e14c 64 65 78 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d  dexed = pOldItem
1e14d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3b 0a 20 20  ->notIndexed;.  
1e14e 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 49 6e 64    pNewItem->pInd
1e14f 65 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70  ex = pOldItem->p
1e150 49 6e 64 65 78 3b 0a 20 20 20 20 70 54 61 62 20  Index;.    pTab 
1e151 3d 20 70 4e 65 77 49 74 65 6d 2d 3e 70 54 61 62  = pNewItem->pTab
1e152 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 54 61   = pOldItem->pTa
1e153 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 20  b;.    if( pTab 
1e154 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e  ){.      pTab->n
1e155 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  Ref++;.    }.   
1e156 20 70 4e 65 77 49 74 65 6d 2d 3e 70 53 65 6c 65   pNewItem->pSele
1e157 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ct = sqlite3Sele
1e158 63 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74  ctDup(db, pOldIt
1e159 65 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  em->pSelect);.  
1e15a 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e 20    pNewItem->pOn 
1e15b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
1e15c 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70  (db, pOldItem->p
1e15d 4f 6e 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  On);.    pNewIte
1e15e 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 73 71 6c 69  m->pUsing = sqli
1e15f 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c  te3IdListDup(db,
1e160 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 73 69 6e   pOldItem->pUsin
1e161 67 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  g);.    pNewItem
1e162 2d 3e 63 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64  ->colUsed = pOld
1e163 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20  Item->colUsed;. 
1e164 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
1e165 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
1e166 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74  TE IdList *sqlit
1e167 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69  e3IdListDup(sqli
1e168 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20  te3 *db, IdList 
1e169 2a 70 29 7b 0a 20 20 49 64 4c 69 73 74 20 2a 70  *p){.  IdList *p
1e16a 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  New;.  int i;.  
1e16b 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1e16c 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  n 0;.  pNew = sq
1e16d 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
1e16e 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65  (db, sizeof(*pNe
1e16f 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  w) );.  if( pNew
1e170 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1e171 20 20 70 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e    pNew->nId = pN
1e172 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e  ew->nAlloc = p->
1e173 6e 49 64 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d  nId;.  pNew->a =
1e174 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1e175 52 61 77 28 64 62 2c 20 70 2d 3e 6e 49 64 2a 73  Raw(db, p->nId*s
1e176 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29  izeof(p->a[0]) )
1e177 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 61 3d  ;.  if( pNew->a=
1e178 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1e179 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77  3DbFree(db, pNew
1e17a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
1e17b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
1e17c 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a  i<p->nId; i++){.
1e17d 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73      struct IdLis
1e17e 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d  t_item *pNewItem
1e17f 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a   = &pNew->a[i];.
1e180 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73      struct IdLis
1e181 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d  t_item *pOldItem
1e182 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20   = &p->a[i];.   
1e183 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65   pNewItem->zName
1e184 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
1e185 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
1e186 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65  >zName);.    pNe
1e187 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c  wItem->idx = pOl
1e188 64 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a  dItem->idx;.  }.
1e189 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
1e18a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1e18b 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53  Select *sqlite3S
1e18c 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33  electDup(sqlite3
1e18d 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29   *db, Select *p)
1e18e 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77  {.  Select *pNew
1e18f 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
1e190 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20  eturn 0;.  pNew 
1e191 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
1e192 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
1e193 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65  *p) );.  if( pNe
1e194 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  w==0 ) return 0;
1e195 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20  .  pNew->pEList 
1e196 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
1e197 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69  tDup(db, p->pELi
1e198 73 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 53 72  st);.  pNew->pSr
1e199 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  c = sqlite3SrcLi
1e19a 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 53 72  stDup(db, p->pSr
1e19b 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65  c);.  pNew->pWhe
1e19c 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
1e19d 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72  Dup(db, p->pWher
1e19e 65 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f  e);.  pNew->pGro
1e19f 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78  upBy = sqlite3Ex
1e1a0 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  prListDup(db, p-
1e1a1 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 70 4e  >pGroupBy);.  pN
1e1a2 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71  ew->pHaving = sq
1e1a3 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
1e1a4 20 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20   p->pHaving);.  
1e1a5 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d  pNew->pOrderBy =
1e1a6 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
1e1a7 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65  Dup(db, p->pOrde
1e1a8 72 42 79 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70  rBy);.  pNew->op
1e1a9 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77   = p->op;.  pNew
1e1aa 2d 3e 70 50 72 69 6f 72 20 3d 20 73 71 6c 69 74  ->pPrior = sqlit
1e1ab 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
1e1ac 70 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 70 4e  p->pPrior);.  pN
1e1ad 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c  ew->pLimit = sql
1e1ae 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
1e1af 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 70 4e  p->pLimit);.  pN
1e1b0 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20 73 71  ew->pOffset = sq
1e1b1 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
1e1b2 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 20 20   p->pOffset);.  
1e1b3 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30  pNew->iLimit = 0
1e1b4 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65  ;.  pNew->iOffse
1e1b5 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73  t = 0;.  pNew->s
1e1b6 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c  elFlags = p->sel
1e1b7 46 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 73  Flags & ~SF_Uses
1e1b8 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65  Ephemeral;.  pNe
1e1b9 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  w->pRightmost = 
1e1ba 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f  0;.  pNew->addrO
1e1bb 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b  penEphm[0] = -1;
1e1bc 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65  .  pNew->addrOpe
1e1bd 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20  nEphm[1] = -1;. 
1e1be 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45   pNew->addrOpenE
1e1bf 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72  phm[2] = -1;.  r
1e1c0 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65  eturn pNew;.}.#e
1e1c1 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  lse.SQLITE_PRIVA
1e1c2 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74  TE Select *sqlit
1e1c3 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69  e3SelectDup(sqli
1e1c4 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20  te3 *db, Select 
1e1c5 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
1e1c6 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ==0 );.  return 
1e1c7 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  0;.}.#endif.../*
1e1c8 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c  .** Add a new el
1e1c9 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 65 6e 64  ement to the end
1e1ca 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f   of an expressio
1e1cb 6e 20 6c 69 73 74 2e 20 20 49 66 20 70 4c 69 73  n list.  If pLis
1e1cc 74 20 69 73 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c  t is.** initiall
1e1cd 79 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 72 65  y NULL, then cre
1e1ce 61 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73  ate a new expres
1e1cf 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51  sion list..*/.SQ
1e1d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
1e1d1 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78  rList *sqlite3Ex
1e1d2 70 72 4c 69 73 74 41 70 70 65 6e 64 28 0a 20 20  prListAppend(.  
1e1d3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1e1d4 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
1e1d5 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1e1d6 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
1e1d7 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
1e1d8 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 65  to which to appe
1e1d9 6e 64 2e 20 4d 69 67 68 74 20 62 65 20 4e 55 4c  nd. Might be NUL
1e1da 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  L */.  Expr *pEx
1e1db 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  pr,            /
1e1dc 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  * Expression to 
1e1dd 62 65 20 61 70 70 65 6e 64 65 64 20 2a 2f 0a 20  be appended */. 
1e1de 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20   Token *pName   
1e1df 20 20 20 20 20 20 20 20 20 2f 2a 20 41 53 20 6b           /* AS k
1e1e0 65 79 77 6f 72 64 20 66 6f 72 20 74 68 65 20 65  eyword for the e
1e1e1 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a  xpression */.){.
1e1e2 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1e1e3 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
1e1e4 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  ( pList==0 ){.  
1e1e5 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
1e1e6 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
1e1e7 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 4c 69 73  , sizeof(ExprLis
1e1e8 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  t) );.    if( pL
1e1e9 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ist==0 ){.      
1e1ea 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
1e1eb 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
1e1ec 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20  List->nAlloc==0 
1e1ed 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69  );.  }.  if( pLi
1e1ee 73 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73  st->nAlloc<=pLis
1e1ef 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20  t->nExpr ){.    
1e1f0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1e1f1 69 74 65 6d 20 2a 61 3b 0a 20 20 20 20 69 6e 74  item *a;.    int
1e1f2 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c   n = pList->nAll
1e1f3 6f 63 2a 32 20 2b 20 34 3b 0a 20 20 20 20 61 20  oc*2 + 4;.    a 
1e1f4 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
1e1f5 6f 63 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c  oc(db, pList->a,
1e1f6 20 6e 2a 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d   n*sizeof(pList-
1e1f7 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28  >a[0]));.    if(
1e1f8 20 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67   a==0 ){.      g
1e1f9 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20  oto no_mem;.    
1e1fa 7d 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 20 3d  }.    pList->a =
1e1fb 20 61 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e   a;.    pList->n
1e1fc 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  Alloc = sqlite3D
1e1fd 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
1e1fe 61 29 2f 73 69 7a 65 6f 66 28 61 5b 30 5d 29 3b  a)/sizeof(a[0]);
1e1ff 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1e200 4c 69 73 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20  List->a!=0 );.  
1e201 69 66 28 20 70 45 78 70 72 20 7c 7c 20 70 4e 61  if( pExpr || pNa
1e202 6d 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  me ){.    struct
1e203 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1e204 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e  pItem = &pList->
1e205 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b  a[pList->nExpr++
1e206 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49  ];.    memset(pI
1e207 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  tem, 0, sizeof(*
1e208 70 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74  pItem));.    pIt
1e209 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  em->zName = sqli
1e20a 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
1e20b 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20  (db, pName);.   
1e20c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20   pItem->pExpr = 
1e20d 70 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d  pExpr;.    pItem
1e20e 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20  ->iAlias = 0;.  
1e20f 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74  }.  return pList
1e210 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 20 0a  ;..no_mem:     .
1e211 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b 69    /* Avoid leaki
1e212 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d 61 6c  ng memory if mal
1e213 6c 6f 63 20 68 61 73 20 66 61 69 6c 65 64 2e 20  loc has failed. 
1e214 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  */.  sqlite3Expr
1e215 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72  Delete(db, pExpr
1e216 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
1e217 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
1e218 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  List);.  return 
1e219 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  0;.}../*.** If t
1e21a 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
1e21b 73 74 20 70 45 4c 69 73 74 20 63 6f 6e 74 61 69  st pEList contai
1e21c 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69  ns more than iLi
1e21d 6d 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a  mit elements,.**
1e21e 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20   leave an error 
1e21f 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73  message in pPars
1e220 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1e221 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1e222 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65  3ExprListCheckLe
1e223 6e 67 74 68 28 0a 20 20 50 61 72 73 65 20 2a 70  ngth(.  Parse *p
1e224 50 61 72 73 65 2c 0a 20 20 45 78 70 72 4c 69 73  Parse,.  ExprLis
1e225 74 20 2a 70 45 4c 69 73 74 2c 0a 20 20 63 6f 6e  t *pEList,.  con
1e226 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a 65 63 74  st char *zObject
1e227 0a 29 7b 0a 20 20 69 6e 74 20 6d 78 20 3d 20 70  .){.  int mx = p
1e228 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69  Parse->db->aLimi
1e229 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  t[SQLITE_LIMIT_C
1e22a 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 63 61  OLUMN];.  testca
1e22b 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45  se( pEList && pE
1e22c 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 20  List->nExpr==mx 
1e22d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  );.  testcase( p
1e22e 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d  EList && pEList-
1e22f 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 29 3b 0a  >nExpr==mx+1 );.
1e230 20 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20    if( pEList && 
1e231 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 6d 78  pEList->nExpr>mx
1e232 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1e233 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1e234 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e  "too many column
1e235 73 20 69 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63  s in %s", zObjec
1e236 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  t);.  }.}../*.**
1e237 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72   Delete an entir
1e238 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
1e239 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
1e23a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1e23b 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
1e23c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
1e23d 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20  rList *pList){. 
1e23e 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
1e23f 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1e240 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69  pItem;.  if( pLi
1e241 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  st==0 ) return;.
1e242 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d    assert( pList-
1e243 3e 61 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d  >a!=0 || (pList-
1e244 3e 6e 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69  >nExpr==0 && pLi
1e245 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29  st->nAlloc==0) )
1e246 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73  ;.  assert( pLis
1e247 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d  t->nExpr<=pList-
1e248 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72  >nAlloc );.  for
1e249 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c  (pItem=pList->a,
1e24a 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e   i=0; i<pList->n
1e24b 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
1e24c 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
1e24d 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1e24e 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20  Item->pExpr);.  
1e24f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1e250 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  db, pItem->zName
1e251 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
1e252 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74  DbFree(db, pList
1e253 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ->a);.  sqlite3D
1e254 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29  bFree(db, pList)
1e255 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65  ;.}../*.** These
1e256 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 57 61   routines are Wa
1e257 6c 6b 65 72 20 63 61 6c 6c 62 61 63 6b 73 2e 20  lker callbacks. 
1e258 20 57 61 6c 6b 65 72 2e 75 2e 70 69 20 69 73 20   Walker.u.pi is 
1e259 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
1e25a 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65  an integer.  The
1e25b 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
1e25c 63 68 65 63 6b 69 6e 67 20 61 6e 20 65 78 70 72  checking an expr
1e25d 65 73 73 69 6f 6e 20 74 6f 20 73 65 65 0a 2a 2a  ession to see.**
1e25e 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6e 73   if it is a cons
1e25f 74 61 6e 74 2e 20 20 53 65 74 20 2a 57 61 6c 6b  tant.  Set *Walk
1e260 65 72 2e 75 2e 70 69 20 74 6f 20 30 20 69 66 20  er.u.pi to 0 if 
1e261 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
1e262 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 73 74 61 6e  s.** not constan
1e263 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  t..**.** These c
1e264 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 73  allback routines
1e265 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70   are used to imp
1e266 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f  lement the follo
1e267 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  wing:.**.**     
1e268 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
1e269 73 74 61 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73  stant().**     s
1e26a 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
1e26b 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29 0a 2a 2a  tantNotJoin().**
1e26c 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1e26d 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63  IsConstantOrFunc
1e26e 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a 73 74 61  tion().**.*/.sta
1e26f 74 69 63 20 69 6e 74 20 65 78 70 72 4e 6f 64 65  tic int exprNode
1e270 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65  IsConstant(Walke
1e271 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72  r *pWalker, Expr
1e272 20 2a 70 45 78 70 72 29 7b 0a 0a 20 20 2f 2a 20   *pExpr){..  /* 
1e273 49 66 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20  If pWalker->u.i 
1e274 69 73 20 33 20 74 68 65 6e 20 61 6e 79 20 74 65  is 3 then any te
1e275 72 6d 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  rm of the expres
1e276 73 69 6f 6e 20 74 68 61 74 20 63 6f 6d 65 73 20  sion that comes 
1e277 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 4f 4e  from.  ** the ON
1e278 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
1e279 73 20 6f 66 20 61 20 6a 6f 69 6e 20 64 69 73 71  s of a join disq
1e27a 75 61 6c 69 66 69 65 73 20 74 68 65 20 65 78 70  ualifies the exp
1e27b 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 66 72 6f  ression.  ** fro
1e27c 6d 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72  m being consider
1e27d 65 64 20 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f 0a  ed constant. */.
1e27e 20 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75    if( pWalker->u
1e27f 2e 69 3d 3d 33 20 26 26 20 45 78 70 72 48 61 73  .i==3 && ExprHas
1e280 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
1e281 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20  r, EP_FromJoin) 
1e282 29 7b 0a 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e  ){.    pWalker->
1e283 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  u.i = 0;.    ret
1e284 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1e285 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45   }..  switch( pE
1e286 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f  xpr->op ){.    /
1e287 2a 20 43 6f 6e 73 69 64 65 72 20 66 75 6e 63 74  * Consider funct
1e288 69 6f 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73 74  ions to be const
1e289 61 6e 74 20 69 66 20 61 6c 6c 20 74 68 65 69 72  ant if all their
1e28a 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 63   arguments are c
1e28b 6f 6e 73 74 61 6e 74 0a 20 20 20 20 2a 2a 20 61  onstant.    ** a
1e28c 6e 64 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d  nd pWalker->u.i=
1e28d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54  =2 */.    case T
1e28e 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20  K_FUNCTION:.    
1e28f 20 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75    if( pWalker->u
1e290 2e 69 3d 3d 32 20 29 20 72 65 74 75 72 6e 20 30  .i==2 ) return 0
1e291 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20  ;.      /* Fall 
1e292 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 20 20 63  through */.    c
1e293 61 73 65 20 54 4b 5f 49 44 3a 0a 20 20 20 20 63  ase TK_ID:.    c
1e294 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20  ase TK_COLUMN:. 
1e295 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46     case TK_AGG_F
1e296 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73  UNCTION:.    cas
1e297 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a  e TK_AGG_COLUMN:
1e298 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1e299 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20  OMIT_SUBQUERY.  
1e29a 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54    case TK_SELECT
1e29b 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58  :.    case TK_EX
1e29c 49 53 54 53 3a 0a 20 20 20 20 20 20 74 65 73 74  ISTS:.      test
1e29d 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
1e29e 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20  =TK_SELECT );.  
1e29f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
1e2a0 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53  xpr->op==TK_EXIS
1e2a1 54 53 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  TS );.#endif.   
1e2a2 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
1e2a3 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b  pr->op==TK_ID );
1e2a4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1e2a5 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43   pExpr->op==TK_C
1e2a6 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74  OLUMN );.      t
1e2a7 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
1e2a8 6f 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54  op==TK_AGG_FUNCT
1e2a9 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ION );.      tes
1e2aa 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
1e2ab 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  ==TK_AGG_COLUMN 
1e2ac 29 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b 65 72  );.      pWalker
1e2ad 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20  ->u.i = 0;.     
1e2ae 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1e2af 74 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a  t;.    default:.
1e2b0 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1e2b1 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d  _Continue;.  }.}
1e2b2 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65  .static int sele
1e2b3 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74  ctNodeIsConstant
1e2b4 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
1e2b5 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65  , Select *NotUse
1e2b6 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
1e2b7 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
1e2b8 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20  .  pWalker->u.i 
1e2b9 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52  = 0;.  return WR
1e2ba 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69  C_Abort;.}.stati
1e2bb 63 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73  c int exprIsCons
1e2bc 74 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69  t(Expr *p, int i
1e2bd 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b  nitFlag){.  Walk
1e2be 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20  er w;.  w.u.i = 
1e2bf 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45  initFlag;.  w.xE
1e2c0 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78  xprCallback = ex
1e2c1 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74  prNodeIsConstant
1e2c2 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c  ;.  w.xSelectCal
1e2c3 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f  lback = selectNo
1e2c4 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20  deIsConstant;.  
1e2c5 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1e2c6 26 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e  &w, p);.  return
1e2c7 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   w.u.i;.}../*.**
1e2c8 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73   Walk an express
1e2c9 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72  ion tree.  Retur
1e2ca 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65  n 1 if the expre
1e2cb 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e  ssion is constan
1e2cc 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74  t.** and 0 if it
1e2cd 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62   involves variab
1e2ce 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20  les or function 
1e2cf 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  calls..**.** For
1e2d0 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66   the purposes of
1e2d1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
1e2d2 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20  a double-quoted 
1e2d3 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63  string (ex: "abc
1e2d4 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  ").** is conside
1e2d5 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62  red a variable b
1e2d6 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74  ut a single-quot
1e2d7 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27  ed string (ex: '
1e2d8 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f  abc') is.** a co
1e2d9 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  nstant..*/.SQLIT
1e2da 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1e2db 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
1e2dc 61 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20  ant(Expr *p){.  
1e2dd 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e  return exprIsCon
1e2de 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a  st(p, 1);.}../*.
1e2df 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65  ** Walk an expre
1e2e0 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74  ssion tree.  Ret
1e2e1 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70  urn 1 if the exp
1e2e2 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74  ression is const
1e2e3 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73  ant.** that does
1e2e4 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72   no originate fr
1e2e5 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  om the ON or USI
1e2e6 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20  NG clauses of a 
1e2e7 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  join..** Return 
1e2e8 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73  0 if it involves
1e2e9 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75   variables or fu
1e2ea 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20  nction calls or 
1e2eb 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e  terms from.** an
1e2ec 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
1e2ed 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  use..*/.SQLITE_P
1e2ee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1e2ef 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
1e2f0 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29  NotJoin(Expr *p)
1e2f1 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49  {.  return exprI
1e2f2 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a  sConst(p, 3);.}.
1e2f3 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  ./*.** Walk an e
1e2f4 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
1e2f5 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
1e2f6 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63   expression is c
1e2f7 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20  onstant.** or a 
1e2f8 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69  function call wi
1e2f9 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75  th constant argu
1e2fa 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61  ments.  Return a
1e2fb 6e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a  nd 0 if there.**
1e2fc 20 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c   are any variabl
1e2fd 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  es..**.** For th
1e2fe 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68  e purposes of th
1e2ff 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64  is function, a d
1e300 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72  ouble-quoted str
1e301 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a  ing (ex: "abc").
1e302 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
1e303 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20   a variable but 
1e304 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20  a single-quoted 
1e305 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63  string (ex: 'abc
1e306 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74  ') is.** a const
1e307 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ant..*/.SQLITE_P
1e308 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1e309 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
1e30a 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20  OrFunction(Expr 
1e30b 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78  *p){.  return ex
1e30c 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b  prIsConst(p, 2);
1e30d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
1e30e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f   expression p co
1e30f 64 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69  des a constant i
1e310 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73  nteger that is s
1e311 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74  mall enough.** t
1e312 6f 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69  o fit in a 32-bi
1e313 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72  t integer, retur
1e314 6e 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20  n 1 and put the 
1e315 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74  value of the int
1e316 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c  eger.** in *pVal
1e317 75 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72  ue.  If the expr
1e318 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e  ession is not an
1e319 20 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69   integer or if i
1e31a 74 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20  t is too big.** 
1e31b 74 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e  to fit in a sign
1e31c 65 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65  ed 32-bit intege
1e31d 72 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20  r, return 0 and 
1e31e 6c 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e  leave *pValue un
1e31f 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49  changed..*/.SQLI
1e320 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1e321 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65  qlite3ExprIsInte
1e322 67 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74  ger(Expr *p, int
1e323 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74   *pValue){.  int
1e324 20 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70   rc = 0;.  if( p
1e325 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74  ->flags & EP_Int
1e326 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56  Value ){.    *pV
1e327 61 6c 75 65 20 3d 20 70 2d 3e 69 54 61 62 6c 65  alue = p->iTable
1e328 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
1e329 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 2d    }.  switch( p-
1e32a 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
1e32b 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20  TK_INTEGER: {.  
1e32c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e32d 47 65 74 49 6e 74 33 32 28 28 63 68 61 72 2a 29  GetInt32((char*)
1e32e 70 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 56 61 6c  p->token.z, pVal
1e32f 75 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ue);.      break
1e330 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1e331 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20   TK_UPLUS: {.   
1e332 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 45     rc = sqlite3E
1e333 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e  xprIsInteger(p->
1e334 70 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a  pLeft, pValue);.
1e335 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e336 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55   }.    case TK_U
1e337 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69  MINUS: {.      i
1e338 6e 74 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20  nt v;.      if( 
1e339 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74  sqlite3ExprIsInt
1e33a 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26  eger(p->pLeft, &
1e33b 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70  v) ){.        *p
1e33c 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20  Value = -v;.    
1e33d 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20      rc = 1;.    
1e33e 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1e33f 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
1e340 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20  lt: break;.  }. 
1e341 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70   if( rc ){.    p
1e342 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45  ->op = TK_INTEGE
1e343 52 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  R;.    p->flags 
1e344 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a  |= EP_IntValue;.
1e345 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20      p->iTable = 
1e346 2a 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20 20 72  *pValue;.  }.  r
1e347 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1e348 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
1e349 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  f the given stri
1e34a 6e 67 20 69 73 20 61 20 72 6f 77 2d 69 64 20 63  ng is a row-id c
1e34b 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53  olumn name..*/.S
1e34c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1e34d 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64  t sqlite3IsRowid
1e34e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
1e34f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
1e350 72 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49 44  rICmp(z, "_ROWID
1e351 5f 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  _")==0 ) return 
1e352 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  1;.  if( sqlite3
1e353 53 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57 49  StrICmp(z, "ROWI
1e354 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  D")==0 ) return 
1e355 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  1;.  if( sqlite3
1e356 53 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44 22  StrICmp(z, "OID"
1e357 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  )==0 ) return 1;
1e358 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1e359 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
1e35a 65 20 69 66 20 74 68 65 20 49 4e 20 6f 70 65 72  e if the IN oper
1e35b 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  ator optimizatio
1e35c 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64  n is enabled and
1e35d 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73  .** the SELECT s
1e35e 74 61 74 65 6d 65 6e 74 20 70 20 65 78 69 73 74  tatement p exist
1e35f 73 20 61 6e 64 20 69 73 20 6f 66 20 74 68 65 0a  s and is of the.
1e360 2a 2a 20 73 69 6d 70 6c 65 20 66 6f 72 6d 3a 0a  ** simple form:.
1e361 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
1e362 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c   <column> FROM <
1e363 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20  table>.**.** If 
1e364 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65  this is the case
1e365 2c 20 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73  , it may be poss
1e366 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65  ible to use an e
1e367 78 69 73 74 69 6e 67 20 74 61 62 6c 65 0a 2a 2a  xisting table.**
1e368 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61   or index instea
1e369 64 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20  d of generating 
1e36a 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62  an epheremal tab
1e36b 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  le..*/.#ifndef S
1e36c 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1e36d 45 52 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69  ERY.static int i
1e36e 73 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f  sCandidateForInO
1e36f 70 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  pt(Select *p){. 
1e370 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a   SrcList *pSrc;.
1e371 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
1e372 73 74 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  st;.  Table *pTa
1e373 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  b;.  if( p==0 ) 
1e374 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
1e375 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72              /* r
1e376 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  ight-hand side o
1e377 66 20 49 4e 20 69 73 20 53 45 4c 45 43 54 20 2a  f IN is SELECT *
1e378 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f  /.  if( p->pPrio
1e379 72 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  r ) return 0;   
1e37a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f             /* No
1e37b 74 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  t a compound SEL
1e37c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ECT */.  if( p->
1e37d 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44  selFlags & (SF_D
1e37e 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65  istinct|SF_Aggre
1e37f 67 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 72  gate) ){.      r
1e380 65 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44  eturn 0; /* No D
1e381 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
1e382 61 6e 64 20 6e 6f 20 61 67 67 72 65 67 61 74 65  and no aggregate
1e383 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20   functions */.  
1e384 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47 72 6f 75  }.  if( p->pGrou
1e385 70 42 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20  pBy ) return 0; 
1e386 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
1e387 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c  s no GROUP BY cl
1e388 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d  ause */.  if( p-
1e389 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e  >pLimit ) return
1e38a 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1e38b 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54   /* Has no LIMIT
1e38c 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28   clause */.  if(
1e38d 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 20 72 65   p->pOffset ) re
1e38e 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 2d  turn 0;.  if( p-
1e38f 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e  >pWhere ) return
1e390 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1e391 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45 52 45   /* Has no WHERE
1e392 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70 53 72   clause */.  pSr
1e393 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61  c = p->pSrc;.  a
1e394 73 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29  ssert( pSrc!=0 )
1e395 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53  ;.  if( pSrc->nS
1e396 72 63 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30  rc!=1 ) return 0
1e397 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69  ;          /* Si
1e398 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 46 52 4f  ngle term in FRO
1e399 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66  M clause */.  if
1e39a 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65  ( pSrc->a[0].pSe
1e39b 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b  lect ) return 0;
1e39c 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61       /* FROM cla
1e39d 75 73 65 20 69 73 20 6e 6f 74 20 61 20 73 75 62  use is not a sub
1e39e 71 75 65 72 79 20 2a 2f 0a 20 20 70 54 61 62 20  query */.  pTab 
1e39f 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61  = pSrc->a[0].pTa
1e3a0 62 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  b;.  if( pTab==0
1e3a1 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
1e3a2 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  f( pTab->pSelect
1e3a3 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
1e3a4 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c        /* FROM cl
1e3a5 61 75 73 65 20 69 73 20 6e 6f 74 20 61 20 76 69  ause is not a vi
1e3a6 65 77 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69  ew */.  if( IsVi
1e3a7 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65  rtual(pTab) ) re
1e3a8 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 2f  turn 0;        /
1e3a9 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f  * FROM clause no
1e3aa 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  t a virtual tabl
1e3ab 65 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20  e */.  pEList = 
1e3ac 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28  p->pEList;.  if(
1e3ad 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d   pEList->nExpr!=
1e3ae 31 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  1 ) return 0;   
1e3af 20 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d      /* One colum
1e3b0 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  n in the result 
1e3b1 73 65 74 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c  set */.  if( pEL
1e3b2 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d  ist->a[0].pExpr-
1e3b3 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29  >op!=TK_COLUMN )
1e3b4 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65   return 0; /* Re
1e3b5 73 75 6c 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e  sult is a column
1e3b6 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a   */.  return 1;.
1e3b7 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1e3b8 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
1e3b9 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20   */../*.** This 
1e3ba 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
1e3bb 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   by the implemen
1e3bc 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e  tation of the IN
1e3bd 20 28 2e 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e   (...) operator.
1e3be 0a 2a 2a 20 49 74 27 73 20 6a 6f 62 20 69 73 20  .** It's job is 
1e3bf 74 6f 20 66 69 6e 64 20 6f 72 20 63 72 65 61 74  to find or creat
1e3c0 65 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63  e a b-tree struc
1e3c1 74 75 72 65 20 74 68 61 74 20 6d 61 79 20 62 65  ture that may be
1e3c2 20 75 73 65 64 0a 2a 2a 20 65 69 74 68 65 72 20   used.** either 
1e3c3 74 6f 20 74 65 73 74 20 66 6f 72 20 6d 65 6d 62  to test for memb
1e3c4 65 72 73 68 69 70 20 6f 66 20 74 68 65 20 28 2e  ership of the (.
1e3c5 2e 2e 29 20 73 65 74 20 6f 72 20 74 6f 20 69 74  ..) set or to it
1e3c6 65 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a  erate through.**
1e3c7 20 69 74 73 20 6d 65 6d 62 65 72 73 2c 20 73 6b   its members, sk
1e3c8 69 70 70 69 6e 67 20 64 75 70 6c 69 63 61 74 65  ipping duplicate
1e3c9 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72  s..**.** The cur
1e3ca 73 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68  sor opened on th
1e3cb 65 20 73 74 72 75 63 74 75 72 65 20 28 64 61 74  e structure (dat
1e3cc 61 62 61 73 65 20 74 61 62 6c 65 2c 20 64 61 74  abase table, dat
1e3cd 61 62 61 73 65 20 69 6e 64 65 78 20 0a 2a 2a 20  abase index .** 
1e3ce 6f 72 20 65 70 68 65 72 6d 61 6c 20 74 61 62 6c  or ephermal tabl
1e3cf 65 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  e) is stored in 
1e3d0 70 58 2d 3e 69 54 61 62 6c 65 20 62 65 66 6f 72  pX->iTable befor
1e3d1 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  e this function 
1e3d2 72 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68 65 20  returns..** The 
1e3d3 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
1e3d4 6e 64 69 63 61 74 65 73 20 74 68 65 20 73 74 72  ndicates the str
1e3d5 75 63 74 75 72 65 20 74 79 70 65 2c 20 61 73 20  ucture type, as 
1e3d6 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
1e3d7 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20   IN_INDEX_ROWID 
1e3d8 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73  - The cursor was
1e3d9 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74   opened on a dat
1e3da 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20  abase table..** 
1e3db 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58    IN_INDEX_INDEX
1e3dc 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61   - The cursor wa
1e3dd 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61  s opened on a da
1e3de 74 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a  tabase index..**
1e3df 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20     IN_INDEX_EPH 
1e3e0 2d 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77  -   The cursor w
1e3e1 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73  as opened on a s
1e3e2 70 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64  pecially created
1e3e3 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
1e3e4 20 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c             popul
1e3e5 61 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74  ated epheremal t
1e3e6 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  able..**.** An e
1e3e7 78 69 73 74 69 6e 67 20 73 74 72 75 63 74 75 72  xisting structur
1e3e8 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73  e may only be us
1e3e9 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 54  ed if the SELECT
1e3ea 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 6c   is of the simpl
1e3eb 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a  e.** form:.**.**
1e3ec 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c       SELECT <col
1e3ed 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65  umn> FROM <table
1e3ee 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 72 4e 6f 74  >.**.** If prNot
1e3ef 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20  Found parameter 
1e3f0 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 73  is 0, then the s
1e3f1 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 62 65  tructure will be
1e3f2 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 65   used to iterate
1e3f3 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20  .** through the 
1e3f4 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69  set members, ski
1e3f5 70 70 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63  pping any duplic
1e3f6 61 74 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61  ates. In this ca
1e3f7 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d  se an.** epherem
1e3f8 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65  al table must be
1e3f9 20 75 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65   used unless the
1e3fa 20 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d   selected <colum
1e3fb 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  n> is guaranteed
1e3fc 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65  .** to be unique
1e3fd 20 2d 20 65 69 74 68 65 72 20 62 65 63 61 75 73   - either becaus
1e3fe 65 20 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47  e it is an INTEG
1e3ff 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f  ER PRIMARY KEY o
1e400 72 20 69 74 0a 2a 2a 20 69 73 20 75 6e 69 71 75  r it.** is uniqu
1e401 65 20 62 79 20 76 69 72 74 75 65 20 6f 66 20 61  e by virtue of a
1e402 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72 20 69   constraint or i
1e403 6d 70 6c 69 63 69 74 20 69 6e 64 65 78 2e 0a 2a  mplicit index..*
1e404 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f  *.** If the prNo
1e405 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72  tFound parameter
1e406 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20   is not 0, then 
1e407 74 68 65 20 73 74 72 75 63 74 75 72 65 20 77 69  the structure wi
1e408 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66  ll be used .** f
1e409 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d 62  or fast set memb
1e40a 65 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 6e  ership tests. In
1e40b 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 70   this case an ep
1e40c 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75  heremal table mu
1e40d 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 75  st .** be used u
1e40e 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69  nless <column> i
1e40f 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  s an INTEGER PRI
1e410 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 69  MARY KEY or an i
1e411 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20  ndex can .** be 
1e412 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75  found with <colu
1e413 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 2d  mn> as its left-
1e414 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a  most column..**.
1e415 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 74 72 75  ** When the stru
1e416 63 74 75 72 65 20 69 73 20 62 65 69 6e 67 20 75  cture is being u
1e417 73 65 64 20 66 6f 72 20 73 65 74 20 6d 65 6d 62  sed for set memb
1e418 65 72 73 68 69 70 20 74 65 73 74 73 2c 20 74 68  ership tests, th
1e419 65 20 75 73 65 72 0a 2a 2a 20 6e 65 65 64 73 20  e user.** needs 
1e41a 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20  to know whether 
1e41b 6f 72 20 6e 6f 74 20 74 68 65 20 73 74 72 75 63  or not the struc
1e41c 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e  ture contains an
1e41d 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61   SQL NULL .** va
1e41e 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  lue in order to 
1e41f 63 6f 72 72 65 63 74 6c 79 20 65 76 61 6c 75 61  correctly evalua
1e420 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c  te expressions l
1e421 69 6b 65 20 22 58 20 49 4e 20 28 59 2c 20 5a 29  ike "X IN (Y, Z)
1e422 22 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  "..** If there i
1e423 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20  s a chance that 
1e424 74 68 65 20 73 74 72 75 63 74 75 72 65 20 6d 61  the structure ma
1e425 79 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c  y contain a NULL
1e426 20 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72 75 6e   value at.** run
1e427 74 69 6d 65 2c 20 74 68 65 6e 20 61 20 72 65 67  time, then a reg
1e428 69 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74  ister is allocat
1e429 65 64 20 61 6e 64 20 74 68 65 20 72 65 67 69 73  ed and the regis
1e42a 74 65 72 20 6e 75 6d 62 65 72 20 77 72 69 74 74  ter number writt
1e42b 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f 74 46  en.** to *prNotF
1e42c 6f 75 6e 64 2e 20 49 66 20 74 68 65 72 65 20 69  ound. If there i
1e42d 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74  s no chance that
1e42e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 63   the structure c
1e42f 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c  ontains a.** NUL
1e430 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70  L value, then *p
1e431 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66  rNotFound is lef
1e432 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a  t unchanged..**.
1e433 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74 65 72  ** If a register
1e434 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
1e435 64 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73  d its location s
1e436 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46  tored in *prNotF
1e437 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74  ound, then.** it
1e438 73 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20  s initial value 
1e439 69 73 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20  is NULL. If the 
1e43a 73 74 72 75 63 74 75 72 65 20 64 6f 65 73 20 6e  structure does n
1e43b 6f 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61  ot remain consta
1e43c 6e 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75  nt.** for the du
1e43d 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75  ration of the qu
1e43e 65 72 79 20 28 69 2e 65 2e 20 74 68 65 20 73 65  ery (i.e. the se
1e43f 74 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65  t is a correlate
1e440 64 20 73 75 62 2d 73 65 6c 65 63 74 29 2c 20 0a  d sub-select), .
1e441 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
1e442 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65  the allocated re
1e443 67 69 73 74 65 72 20 69 73 20 72 65 73 65 74 20  gister is reset 
1e444 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69 6d  to NULL each tim
1e445 65 20 74 68 65 20 0a 2a 2a 20 73 74 72 75 63 74  e the .** struct
1e446 75 72 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74  ure is repopulat
1e447 65 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20  ed. This allows 
1e448 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 75 73  the caller to us
1e449 65 20 76 64 62 65 20 63 6f 64 65 20 0a 2a 2a 20  e vdbe code .** 
1e44a 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68  equivalent to th
1e44b 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
1e44c 2a 2a 20 20 20 69 66 28 20 72 65 67 69 73 74 65  **   if( registe
1e44d 72 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20  r==NULL ){.**   
1e44e 20 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65    has_null = <te
1e44f 73 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63  st if data struc
1e450 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75  ture contains nu
1e451 6c 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73  ll>.**     regis
1e452 74 65 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a  ter = 1.**   }.*
1e453 2a 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f  *.** in order to
1e454 20 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74   avoid running t
1e455 68 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61  he <test if data
1e456 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
1e457 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73  ins null>.** tes
1e458 74 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61  t more often tha
1e459 6e 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a  n is necessary..
1e45a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1e45b 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
1e45c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1e45d 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e  nt sqlite3FindIn
1e45e 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61  Index(Parse *pPa
1e45f 72 73 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69  rse, Expr *pX, i
1e460 6e 74 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b  nt *prNotFound){
1e461 0a 20 20 53 65 6c 65 63 74 20 2a 70 3b 0a 20 20  .  Select *p;.  
1e462 69 6e 74 20 65 54 79 70 65 20 3d 20 30 3b 0a 20  int eType = 0;. 
1e463 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72   int iTab = pPar
1e464 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 6e  se->nTab++;.  in
1e465 74 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d  t mustBeUnique =
1e466 20 21 70 72 4e 6f 74 46 6f 75 6e 64 3b 0a 0a 20   !prNotFound;.. 
1e467 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 77 69 6e 67   /* The follwing
1e468 20 69 66 28 2e 2e 2e 29 20 65 78 70 72 65 73 73   if(...) express
1e469 69 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74  ion is true if t
1e46a 68 65 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20  he SELECT is of 
1e46b 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65  the .  ** simple
1e46c 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a   form:.  **.  **
1e46d 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c       SELECT <col
1e46e 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65  umn> FROM <table
1e46f 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  >.  **.  ** If t
1e470 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c  his is the case,
1e471 20 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69   it may be possi
1e472 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78  ble to use an ex
1e473 69 73 74 69 6e 67 20 74 61 62 6c 65 0a 20 20 2a  isting table.  *
1e474 2a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65  * or index inste
1e475 61 64 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67  ad of generating
1e476 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61   an epheremal ta
1e477 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20  ble..  */.  p = 
1e478 70 58 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 69  pX->pSelect;.  i
1e479 66 28 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f  f( isCandidateFo
1e47a 72 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20  rInOpt(p) ){.   
1e47b 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1e47c 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 49  Parse->db;.    I
1e47d 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20  ndex *pIdx;.    
1e47e 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 2d  Expr *pExpr = p-
1e47f 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
1e480 78 70 72 3b 0a 20 20 20 20 69 6e 74 20 69 43 6f  xpr;.    int iCo
1e481 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  l = pExpr->iColu
1e482 6d 6e 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20  mn;.    Vdbe *v 
1e483 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1e484 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 2f  (pParse);..    /
1e485 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1e486 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66  is only called f
1e487 72 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73 2e 20  rom two places. 
1e488 49 6e 20 62 6f 74 68 20 63 61 73 65 73 20 74 68  In both cases th
1e489 65 20 76 64 62 65 0a 20 20 20 20 2a 2a 20 68 61  e vdbe.    ** ha
1e48a 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61  s already been a
1e48b 6c 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61 73 73  llocated. So ass
1e48c 75 6d 65 20 73 71 6c 69 74 65 33 47 65 74 56 64  ume sqlite3GetVd
1e48d 62 65 28 29 20 69 73 20 61 6c 77 61 79 73 0a 20  be() is always. 
1e48e 20 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c     ** successful
1e48f 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   here..    */.  
1e490 20 20 61 73 73 65 72 74 28 76 29 3b 0a 20 20 20    assert(v);.   
1e491 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20   if( iCol<0 ){. 
1e492 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20       int iMem = 
1e493 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
1e494 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b        int iAddr;
1e495 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
1e496 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b  ab = p->pSrc->a[
1e497 30 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 69  0].pTab;.      i
1e498 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
1e499 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
1e49a 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
1e49b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1e49c 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
1e49d 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 69 41 64  iDb);..      iAd
1e49e 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
1e49f 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c  AddOp1(v, OP_If,
1e4a0 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71   iMem);.      sq
1e4a1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1e4a2 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
1e4a3 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 20 20  , iMem);..      
1e4a4 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
1e4a5 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20 69  (pParse, iTab, i
1e4a6 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65  Db, pTab, OP_Ope
1e4a7 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 65 54  nRead);.      eT
1e4a8 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52  ype = IN_INDEX_R
1e4a9 4f 57 49 44 3b 0a 0a 20 20 20 20 20 20 73 71 6c  OWID;..      sql
1e4aa 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1e4ab 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20  (v, iAddr);.    
1e4ac 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
1e4ad 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  The collation se
1e4ae 71 75 65 6e 63 65 20 75 73 65 64 20 62 79 20 74  quence used by t
1e4af 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49  he comparison. I
1e4b0 66 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 6f  f an index is to
1e4b1 20 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 75 73   .      ** be us
1e4b2 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61  ed in place of a
1e4b3 20 74 65 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20   temp-table, it 
1e4b4 6d 75 73 74 20 62 65 20 6f 72 64 65 72 65 64 20  must be ordered 
1e4b5 61 63 63 6f 72 64 69 6e 67 0a 20 20 20 20 20 20  according.      
1e4b6 2a 2a 20 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61  ** to this colla
1e4b7 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 20  tion sequence.. 
1e4b8 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f       */.      Co
1e4b9 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73 71  llSeq *pReq = sq
1e4ba 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61  lite3BinaryCompa
1e4bb 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  reCollSeq(pParse
1e4bc 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78  , pX->pLeft, pEx
1e4bd 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43  pr);..      /* C
1e4be 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 66  heck that the af
1e4bf 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c 6c  finity that will
1e4c0 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 66   be used to perf
1e4c1 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20 2a  orm the .      *
1e4c2 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  * comparison is 
1e4c3 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
1e4c4 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20  affinity of the 
1e4c5 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20  column. If.     
1e4c6 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20 69   ** it is not, i
1e4c7 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
1e4c8 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e 64  e to use any ind
1e4c9 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ex..      */.   
1e4ca 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
1e4cb 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70   p->pSrc->a[0].p
1e4cc 54 61 62 3b 0a 20 20 20 20 20 20 63 68 61 72 20  Tab;.      char 
1e4cd 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e  aff = comparison
1e4ce 41 66 66 69 6e 69 74 79 28 70 58 29 3b 0a 20 20  Affinity(pX);.  
1e4cf 20 20 20 20 69 6e 74 20 61 66 66 69 6e 69 74 79      int affinity
1e4d0 5f 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f  _ok = (pTab->aCo
1e4d1 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79  l[iCol].affinity
1e4d2 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49  ==aff||aff==SQLI
1e4d3 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20  TE_AFF_NONE);.. 
1e4d4 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54       for(pIdx=pT
1e4d5 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
1e4d6 20 26 26 20 65 54 79 70 65 3d 3d 30 20 26 26 20   && eType==0 && 
1e4d7 61 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64  affinity_ok; pId
1e4d8 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
1e4d9 20 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64          if( (pId
1e4da 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d  x->aiColumn[0]==
1e4db 69 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 26  iCol).         &
1e4dc 26 20 28 70 52 65 71 3d 3d 73 71 6c 69 74 65 33  & (pReq==sqlite3
1e4dd 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
1e4de 45 4e 43 28 64 62 29 2c 20 70 49 64 78 2d 3e 61  ENC(db), pIdx->a
1e4df 7a 43 6f 6c 6c 5b 30 5d 2c 20 2d 31 2c 20 30 29  zColl[0], -1, 0)
1e4e0 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 21  ).         && (!
1e4e1 6d 75 73 74 42 65 55 6e 69 71 75 65 20 7c 7c 20  mustBeUnique || 
1e4e2 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d  (pIdx->nColumn==
1e4e3 31 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72  1 && pIdx->onErr
1e4e4 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20  or!=OE_None)).  
1e4e5 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
1e4e6 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20     int iDb;.    
1e4e7 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d        int iMem =
1e4e8 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
1e4e9 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
1e4ea 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Addr;.          
1e4eb 63 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20  char *pKey;.  . 
1e4ec 20 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d 20           pKey = 
1e4ed 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 49  (char *)sqlite3I
1e4ee 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
1e4ef 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  se, pIdx);.     
1e4f0 20 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74       iDb = sqlit
1e4f1 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1e4f2 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d  db, pIdx->pSchem
1e4f3 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  a);.          sq
1e4f4 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72  lite3VdbeUsesBtr
1e4f5 65 65 28 76 2c 20 69 44 62 29 3b 0a 0a 20 20 20  ee(v, iDb);..   
1e4f6 20 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73         iAddr = s
1e4f7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
1e4f8 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29  (v, OP_If, iMem)
1e4f9 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1e4fa 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1e4fb 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
1e4fc 69 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20  iMem);.  .      
1e4fd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1e4fe 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e  ddOp2(v, OP_SetN
1e4ff 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70 49  umColumns, 0, pI
1e500 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  dx->nColumn);.  
1e501 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1e502 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1e503 4f 70 65 6e 52 65 61 64 2c 20 69 54 61 62 2c 20  OpenRead, iTab, 
1e504 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c  pIdx->tnum, iDb,
1e505 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1e506 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e507 70 4b 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f 5f  pKey,P4_KEYINFO_
1e508 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20  HANDOFF);.      
1e509 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
1e50a 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d 3e  (v, "%s", pIdx->
1e50b 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 20  zName));.       
1e50c 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e     eType = IN_IN
1e50d 44 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20 20  DEX_INDEX;..    
1e50e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1e50f 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64  eJumpHere(v, iAd
1e510 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  dr);.          i
1e511 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26 26  f( prNotFound &&
1e512 20 21 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f   !pTab->aCol[iCo
1e513 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20  l].notNull ){.  
1e514 20 20 20 20 20 20 20 20 20 20 2a 70 72 4e 6f 74            *prNot
1e515 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65  Found = ++pParse
1e516 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20  ->nMem;.        
1e517 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1e518 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1e519 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 30 20  .  if( eType==0 
1e51a 29 7b 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48  ){.    int rMayH
1e51b 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20  aveNull = 0;.   
1e51c 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45   eType = IN_INDE
1e51d 58 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70  X_EPH;.    if( p
1e51e 72 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20  rNotFound ){.   
1e51f 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d     *prNotFound =
1e520 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20   rMayHaveNull = 
1e521 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
1e522 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58      }else if( pX
1e523 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e  ->pLeft->iColumn
1e524 3c 30 20 26 26 20 70 58 2d 3e 70 53 65 6c 65 63  <0 && pX->pSelec
1e525 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 54  t==0 ){.      eT
1e526 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52  ype = IN_INDEX_R
1e527 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OWID;.    }.    
1e528 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65  sqlite3CodeSubse
1e529 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c  lect(pParse, pX,
1e52a 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65   rMayHaveNull, e
1e52b 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52  Type==IN_INDEX_R
1e52c 4f 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  OWID);.  }else{.
1e52d 20 20 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d      pX->iTable =
1e52e 20 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74   iTab;.  }.  ret
1e52f 75 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e  urn eType;.}.#en
1e530 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  dif../*.** Gener
1e531 61 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61  ate code for sca
1e532 6c 61 72 20 73 75 62 71 75 65 72 69 65 73 20 75  lar subqueries u
1e533 73 65 64 20 61 73 20 61 6e 20 65 78 70 72 65 73  sed as an expres
1e534 73 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f  sion.** and IN o
1e535 70 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70  perators.  Examp
1e536 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  les:.**.**     (
1e537 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29  SELECT a FROM b)
1e538 20 20 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62            -- sub
1e539 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49  query.**     EXI
1e53a 53 54 53 20 28 53 45 4c 45 43 54 20 61 20 46 52  STS (SELECT a FR
1e53b 4f 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54  OM b)   -- EXIST
1e53c 53 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20  S subquery.**   
1e53d 20 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20    x IN (4,5,11) 
1e53e 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
1e53f 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68  IN operator with
1e540 20 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68   list on right-h
1e541 61 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20  and side.**     
1e542 78 20 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46  x IN (SELECT a F
1e543 52 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e  ROM b)     -- IN
1e544 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 73   operator with s
1e545 75 62 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72  ubquery on the r
1e546 69 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ight.**.** The p
1e547 45 78 70 72 20 70 61 72 61 6d 65 74 65 72 20 64  Expr parameter d
1e548 65 73 63 72 69 62 65 73 20 74 68 65 20 65 78 70  escribes the exp
1e549 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e  ression that con
1e54a 74 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20  tains the IN.** 
1e54b 6f 70 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71  operator or subq
1e54c 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  uery..**.** If p
1e54d 61 72 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64  arameter isRowid
1e54e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
1e54f 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  en expression pE
1e550 78 70 72 20 69 73 20 67 75 61 72 61 6e 74 65 65  xpr is guarantee
1e551 64 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68  d.** to be of th
1e552 65 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20  e form "<rowid> 
1e553 49 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77  IN (?, ?, ?)", w
1e554 68 65 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20  here <rowid> is 
1e555 61 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74  a reference.** t
1e556 6f 20 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b  o some integer k
1e557 65 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74  ey column of a t
1e558 61 62 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20  able B-Tree. In 
1e559 74 68 69 73 20 63 61 73 65 2c 20 75 73 65 20 61  this case, use a
1e55a 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72  n.** intkey B-Tr
1e55b 65 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ee to store the 
1e55c 73 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76  set of IN(...) v
1e55d 61 6c 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66  alues instead of
1e55e 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73   the usual.** (s
1e55f 6c 6f 77 65 72 29 20 76 61 72 69 61 62 6c 65 20  lower) variable 
1e560 6c 65 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72  length keys B-Tr
1e561 65 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ee..*/.#ifndef S
1e562 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1e563 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ERY.SQLITE_PRIVA
1e564 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
1e565 6f 64 65 53 75 62 73 65 6c 65 63 74 28 0a 20 20  odeSubselect(.  
1e566 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a  Parse *pParse, .
1e567 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 0a    Expr *pExpr, .
1e568 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75    int rMayHaveNu
1e569 6c 6c 2c 0a 20 20 69 6e 74 20 69 73 52 6f 77 69  ll,.  int isRowi
1e56a 64 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41  d.){.  int testA
1e56b 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ddr = 0;        
1e56c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e56d 2a 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20  * One-time test 
1e56e 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64 62  address */.  Vdb
1e56f 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
1e570 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
1e571 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
1e572 72 6e 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20  rn;...  /* This 
1e573 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e  code must be run
1e574 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79   in its entirety
1e575 20 65 76 65 72 79 20 74 69 6d 65 20 69 74 20 69   every time it i
1e576 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20  s encountered.  
1e577 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65  ** if any of the
1e578 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72   following is tr
1e579 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ue:.  **.  **   
1e57a 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61   *  The right-ha
1e57b 6e 64 20 73 69 64 65 20 69 73 20 61 20 63 6f 72  nd side is a cor
1e57c 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79  related subquery
1e57d 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20  .  **    *  The 
1e57e 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
1e57f 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  is an expression
1e580 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67   list containing
1e581 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20   variables.  ** 
1e582 20 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73     *  We are ins
1e583 69 64 65 20 61 20 74 72 69 67 67 65 72 0a 20 20  ide a trigger.  
1e584 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f  **.  ** If all o
1e585 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 20  f the above are 
1e586 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63  false, then we c
1e587 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65  an run this code
1e588 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20   just once.  ** 
1e589 73 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 73  save the results
1e58a 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68 65 20  , and reuse the 
1e58b 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73  same result on s
1e58c 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61  ubsequent invoca
1e58d 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  tions..  */.  if
1e58e 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f  ( !ExprHasAnyPro
1e58f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1e590 56 61 72 53 65 6c 65 63 74 29 20 26 26 20 21 70  VarSelect) && !p
1e591 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
1e592 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20   ){.    int mem 
1e593 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
1e594 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1e595 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
1e596 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74  , mem);.    test
1e597 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  Addr = sqlite3Vd
1e598 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
1e599 6e 74 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b  nteger, 1, mem);
1e59a 0a 20 20 20 20 61 73 73 65 72 74 28 20 74 65 73  .    assert( tes
1e59b 74 41 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73  tAddr>0 || pPars
1e59c 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
1e59d 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77  led );.  }..  sw
1e59e 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
1e59f 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  ){.    case TK_I
1e5a0 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20  N: {.      char 
1e5a1 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20  affinity;.      
1e5a2 4b 65 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b  KeyInfo keyInfo;
1e5a3 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
1e5a4 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
1e5a5 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68  ss of OP_OpenEph
1e5a6 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69  emeral instructi
1e5a7 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  on */.      Expr
1e5a8 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d   *pLeft = pExpr-
1e5a9 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69  >pLeft;..      i
1e5aa 66 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20  f( rMayHaveNull 
1e5ab 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1e5ac 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1e5ad 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79  OP_Null, 0, rMay
1e5ae 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  HaveNull);.     
1e5af 20 7d 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69   }..      affini
1e5b0 74 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ty = sqlite3Expr
1e5b1 41 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b  Affinity(pLeft);
1e5b2 0a 0a 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68  ..      /* Wheth
1e5b3 65 72 20 74 68 69 73 20 69 73 20 61 6e 20 27 78  er this is an 'x
1e5b4 20 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20   IN(SELECT...)' 
1e5b5 6f 72 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70  or an 'x IN(<exp
1e5b6 72 6c 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a  rlist>)'.      *
1e5b7 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20  * expression it 
1e5b8 69 73 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73  is handled the s
1e5b9 61 6d 65 20 77 61 79 2e 20 41 20 76 69 72 74 75  ame way. A virtu
1e5ba 61 6c 20 74 61 62 6c 65 20 69 73 20 0a 20 20 20  al table is .   
1e5bb 20 20 20 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74     ** filled wit
1e5bc 68 20 73 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69  h single-field i
1e5bd 6e 64 65 78 20 6b 65 79 73 20 72 65 70 72 65 73  ndex keys repres
1e5be 65 6e 74 69 6e 67 20 74 68 65 20 72 65 73 75 6c  enting the resul
1e5bf 74 73 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d  ts.      ** from
1e5c0 20 74 68 65 20 53 45 4c 45 43 54 20 6f 72 20 74   the SELECT or t
1e5c1 68 65 20 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20  he <exprlist>.. 
1e5c2 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
1e5c3 20 49 66 20 74 68 65 20 27 78 27 20 65 78 70 72   If the 'x' expr
1e5c4 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75  ession is a colu
1e5c5 6d 6e 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 65  mn value, or the
1e5c6 20 53 45 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20   SELECT....     
1e5c7 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 65   ** statement re
1e5c8 74 75 72 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76  turns a column v
1e5c9 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 61  alue, then the a
1e5ca 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 0a  ffinity of that.
1e5cb 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20        ** column 
1e5cc 69 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64  is used to build
1e5cd 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 73 2e   the index keys.
1e5ce 20 49 66 20 62 6f 74 68 20 27 78 27 20 61 6e 64   If both 'x' and
1e5cf 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 53 45   the.      ** SE
1e5d0 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e  LECT... statemen
1e5d1 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74  t are columns, t
1e5d2 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69  hen numeric affi
1e5d3 6e 69 74 79 20 69 73 20 75 73 65 64 0a 20 20 20  nity is used.   
1e5d4 20 20 20 2a 2a 20 69 66 20 65 69 74 68 65 72 20     ** if either 
1e5d5 63 6f 6c 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52  column has NUMER
1e5d6 49 43 20 6f 72 20 49 4e 54 45 47 45 52 20 61 66  IC or INTEGER af
1e5d7 66 69 6e 69 74 79 2e 20 49 66 20 6e 65 69 74 68  finity. If neith
1e5d8 65 72 0a 20 20 20 20 20 20 2a 2a 20 27 78 27 20  er.      ** 'x' 
1e5d9 6e 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e  nor the SELECT..
1e5da 2e 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20  . statement are 
1e5db 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75  columns, then nu
1e5dc 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79 0a 20  meric affinity. 
1e5dd 20 20 20 20 20 2a 2a 20 69 73 20 75 73 65 64 2e       ** is used.
1e5de 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1e5df 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
1e5e0 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
1e5e1 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
1e5e2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1e5e3 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
1e5e4 61 6c 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  al, pExpr->iTabl
1e5e5 65 2c 20 21 69 73 52 6f 77 69 64 29 3b 0a 20 20  e, !isRowid);.  
1e5e6 20 20 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49      memset(&keyI
1e5e7 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b  nfo, 0, sizeof(k
1e5e8 65 79 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20  eyInfo));.      
1e5e9 6b 65 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d  keyInfo.nField =
1e5ea 20 31 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70   1;..      if( p
1e5eb 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b  Expr->pSelect ){
1e5ec 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65  .        /* Case
1e5ed 20 31 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20   1:     expr IN 
1e5ee 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20  (SELECT ...).   
1e5ef 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
1e5f0 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1e5f1 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 72 65   to write the re
1e5f2 73 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c  sults of the sel
1e5f3 65 63 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d  ect into the tem
1e5f4 70 6f 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a  porary.        *
1e5f5 2a 20 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65  * table allocate
1e5f6 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f  d and opened abo
1e5f7 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ve..        */. 
1e5f8 20 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73         SelectDes
1e5f9 74 20 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20  t dest;.        
1e5fa 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1e5fb 3b 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ;..        asser
1e5fc 74 28 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20  t( !isRowid );. 
1e5fd 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
1e5fe 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
1e5ff 73 74 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78  st, SRT_Set, pEx
1e600 70 72 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20  pr->iTable);.   
1e601 20 20 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69       dest.affini
1e602 74 79 20 3d 20 28 75 38 29 61 66 66 69 6e 69 74  ty = (u8)affinit
1e603 79 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  y;.        asser
1e604 74 28 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c  t( (pExpr->iTabl
1e605 65 26 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70  e&0x0000FFFF)==p
1e606 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a  Expr->iTable );.
1e607 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
1e608 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
1e609 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74  , pExpr->pSelect
1e60a 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20  , &dest) ){.    
1e60b 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
1e60c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e60d 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  pEList = pExpr->
1e60e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
1e60f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 4c  .        if( pEL
1e610 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e  ist && pEList->n
1e611 45 78 70 72 3e 30 20 29 7b 20 0a 20 20 20 20 20  Expr>0 ){ .     
1e612 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f       keyInfo.aCo
1e613 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42  ll[0] = sqlite3B
1e614 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
1e615 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70  Seq(pParse, pExp
1e616 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20  r->pLeft,.      
1e617 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e          pEList->
1e618 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[0].pExpr);.   
1e619 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1e61a 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 70 4c  se if( pExpr->pL
1e61b 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ist ){.        /
1e61c 2a 20 43 61 73 65 20 32 3a 20 20 20 20 20 65 78  * Case 2:     ex
1e61d 70 72 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29  pr IN (exprlist)
1e61e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
1e61f 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20      ** For each 
1e620 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c  expression, buil
1e621 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66  d an index key f
1e622 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69  rom the evaluati
1e623 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a  on and.        *
1e624 2a 20 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68  * store it in th
1e625 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
1e626 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20  e. If <expr> is 
1e627 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75  a column, then u
1e628 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  se.        ** th
1e629 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e  at columns affin
1e62a 69 74 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e  ity when buildin
1e62b 67 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66  g index keys. If
1e62c 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20   <expr> is not. 
1e62d 20 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75         ** a colu
1e62e 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20  mn, use numeric 
1e62f 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20  affinity..      
1e630 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
1e631 20 69 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72   i;.        Expr
1e632 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45  List *pList = pE
1e633 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20  xpr->pList;.    
1e634 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
1e635 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
1e636 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c  .        int r1,
1e637 20 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20   r2, r3;..      
1e638 20 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20    if( !affinity 
1e639 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66  ){.          aff
1e63a 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
1e63b 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20  FF_NONE;.       
1e63c 20 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 49 6e   }.        keyIn
1e63d 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71  fo.aColl[0] = sq
1e63e 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
1e63f 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
1e640 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20  pLeft);..       
1e641 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
1e642 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   each expression
1e643 20 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20   in <exprlist>. 
1e644 2a 2f 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20  */.        r1 = 
1e645 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
1e646 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
1e647 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47     r2 = sqlite3G
1e648 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
1e649 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1e64a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1e64b 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b  OP_Null, 0, r2);
1e64c 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 70  .        for(i=p
1e64d 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74  List->nExpr, pIt
1e64e 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30  em=pList->a; i>0
1e64f 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i--, pItem++){
1e650 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20  .          Expr 
1e651 2a 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45  *pE2 = pItem->pE
1e652 78 70 72 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  xpr;..          
1e653 2f 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  /* If the expres
1e654 73 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73  sion is not cons
1e655 74 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c  tant then we wil
1e656 6c 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20  l need to.      
1e657 20 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74      ** disable t
1e658 68 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73  he test that was
1e659 20 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65   generated above
1e65a 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65   that makes sure
1e65b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
1e65c 69 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65  is code only exe
1e65d 63 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63  cutes once.  Bec
1e65e 61 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63  ause for a non-c
1e65f 6f 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20  onstant.        
1e660 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
1e661 77 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e  we need to rerun
1e662 20 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20   this code each 
1e663 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20  time..          
1e664 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  */.          if(
1e665 20 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71   testAddr && !sq
1e666 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
1e667 61 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20  ant(pE2) ){.    
1e668 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1e669 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
1e66a 76 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32  v, testAddr-1, 2
1e66b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74  );.            t
1e66c 65 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20  estAddr = 0;.   
1e66d 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
1e66e 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20      /* Evaluate 
1e66f 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61  the expression a
1e670 6e 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74  nd insert it int
1e671 6f 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65  o the temp table
1e672 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 50   */.          pP
1e673 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1e674 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 20  Cache++;.       
1e675 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 45     r3 = sqlite3E
1e676 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
1e677 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a  arse, pE2, r1);.
1e678 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1e679 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
1e67a 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20  eColCache>0 );. 
1e67b 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d           pParse-
1e67c 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65  >disableColCache
1e67d 2d 2d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 69  --;..          i
1e67e 66 28 20 69 73 52 6f 77 69 64 20 29 7b 0a 20 20  f( isRowid ){.  
1e67f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1e680 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1e681 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 33 2c  P_MustBeInt, r3,
1e682 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
1e683 65 6e 74 41 64 64 72 28 76 29 2b 32 29 3b 0a 20  entAddr(v)+2);. 
1e684 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1e685 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
1e686 4f 50 5f 49 6e 73 65 72 74 2c 20 70 45 78 70 72  OP_Insert, pExpr
1e687 2d 3e 69 54 61 62 6c 65 2c 20 72 32 2c 20 72 33  ->iTable, r2, r3
1e688 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
1e689 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
1e68a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1e68b 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  4(v, OP_MakeReco
1e68c 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c 20 26  rd, r3, 1, r2, &
1e68d 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20  affinity, 1);.  
1e68e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1e68f 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
1e690 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
1e691 20 72 33 2c 20 31 29 3b 0a 20 20 20 20 20 20 20   r3, 1);.       
1e692 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1e693 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
1e694 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69  Insert, pExpr->i
1e695 54 61 62 6c 65 2c 20 72 32 29 3b 0a 20 20 20 20  Table, r2);.    
1e696 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e697 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
1e698 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1e699 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
1e69a 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
1e69b 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
1e69c 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a  e, r2);.      }.
1e69d 20 20 20 20 20 20 69 66 28 20 21 69 73 52 6f 77        if( !isRow
1e69e 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  id ){.        sq
1e69f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1e6a0 34 28 76 2c 20 61 64 64 72 2c 20 28 76 6f 69 64  4(v, addr, (void
1e6a1 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20 50 34 5f   *)&keyInfo, P4_
1e6a2 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20  KEYINFO);.      
1e6a3 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1e6a4 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54     }..    case T
1e6a5 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61  K_EXISTS:.    ca
1e6a6 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a  se TK_SELECT: {.
1e6a7 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61        /* This ha
1e6a8 73 20 74 6f 20 62 65 20 61 20 73 63 61 6c 61 72  s to be a scalar
1e6a9 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 72 61   SELECT.  Genera
1e6aa 74 65 20 63 6f 64 65 20 74 6f 20 70 75 74 20 74  te code to put t
1e6ab 68 65 0a 20 20 20 20 20 20 2a 2a 20 76 61 6c 75  he.      ** valu
1e6ac 65 20 6f 66 20 74 68 69 73 20 73 65 6c 65 63 74  e of this select
1e6ad 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c   in a memory cel
1e6ae 6c 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68 65  l and record the
1e6af 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 2a 2a   number.      **
1e6b0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63   of the memory c
1e6b1 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d 6e 2e 0a  ell in iColumn..
1e6b2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
1e6b3 74 61 74 69 63 20 63 6f 6e 73 74 20 54 6f 6b 65  tatic const Toke
1e6b4 6e 20 6f 6e 65 20 3d 20 7b 20 28 75 38 2a 29 22  n one = { (u8*)"
1e6b5 31 22 2c 20 30 2c 20 31 20 7d 3b 0a 20 20 20 20  1", 0, 1 };.    
1e6b6 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 0a    Select *pSel;.
1e6b7 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74        SelectDest
1e6b8 20 64 65 73 74 3b 0a 0a 20 20 20 20 20 20 70 53   dest;..      pS
1e6b9 65 6c 20 3d 20 70 45 78 70 72 2d 3e 70 53 65 6c  el = pExpr->pSel
1e6ba 65 63 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ect;.      sqlit
1e6bb 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
1e6bc 28 26 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61  (&dest, 0, ++pPa
1e6bd 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  rse->nMem);.    
1e6be 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d    if( pExpr->op=
1e6bf 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20  =TK_SELECT ){.  
1e6c0 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 74        dest.eDest
1e6c1 20 3d 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20   = SRT_Mem;.    
1e6c2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1e6c3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
1e6c4 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29  , 0, dest.iParm)
1e6c5 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f  ;.        VdbeCo
1e6c6 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20  mment((v, "Init 
1e6c7 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 22  subquery result"
1e6c8 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ));.      }else{
1e6c9 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 65 44  .        dest.eD
1e6ca 65 73 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73  est = SRT_Exists
1e6cb 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1e6cc 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1e6cd 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65  P_Integer, 0, de
1e6ce 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20  st.iParm);.     
1e6cf 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
1e6d0 76 2c 20 22 49 6e 69 74 20 45 58 49 53 54 53 20  v, "Init EXISTS 
1e6d1 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20  result"));.     
1e6d2 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1e6d3 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73  ExprDelete(pPars
1e6d4 65 2d 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69  e->db, pSel->pLi
1e6d5 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c  mit);.      pSel
1e6d6 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74  ->pLimit = sqlit
1e6d7 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
1e6d8 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30  TK_INTEGER, 0, 0
1e6d9 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69  , &one);.      i
1e6da 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  f( sqlite3Select
1e6db 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26  (pParse, pSel, &
1e6dc 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20  dest) ){.       
1e6dd 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
1e6de 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .      pExpr->iC
1e6df 6f 6c 75 6d 6e 20 3d 20 64 65 73 74 2e 69 50 61  olumn = dest.iPa
1e6e0 72 6d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  rm;.      break;
1e6e1 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
1e6e2 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20  ( testAddr ){.  
1e6e3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1e6e4 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64 64  pHere(v, testAdd
1e6e5 72 2d 31 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  r-1);.  }..  ret
1e6e6 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  urn;.}.#endif /*
1e6e7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
1e6e8 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  QUERY */../*.** 
1e6e9 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62  Duplicate an 8-b
1e6ea 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61  yte value.*/.sta
1e6eb 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62 79  tic char *dup8by
1e6ec 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e  tes(Vdbe *v, con
1e6ed 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20  st char *in){.  
1e6ee 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69  char *out = sqli
1e6ef 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73  te3DbMallocRaw(s
1e6f0 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c  qlite3VdbeDb(v),
1e6f1 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29   8);.  if( out )
1e6f2 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74  {.    memcpy(out
1e6f3 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20  , in, 8);.  }.  
1e6f4 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f  return out;.}../
1e6f5 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  *.** Generate an
1e6f6 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
1e6f7 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66  t will put the f
1e6f8 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a  loating point.**
1e6f9 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65 64   value described
1e6fa 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e   by z[0..n-1] in
1e6fb 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d  to register iMem
1e6fc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20  ..**.** The z[] 
1e6fd 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62  string will prob
1e6fe 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f  ably not be zero
1e6ff 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75  -terminated.  Bu
1e700 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63  t the .** z[n] c
1e701 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72  haracter is guar
1e702 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d  anteed to be som
1e703 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73  ething that does
1e704 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b   not look.** lik
1e705 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69  e the continuati
1e706 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  on of the number
1e707 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1e708 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a   codeReal(Vdbe *
1e709 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  v, const char *z
1e70a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6e 65 67  , int n, int neg
1e70b 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65  ateFlag, int iMe
1e70c 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 20  m){.  assert( z 
1e70d 7c 7c 20 76 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  || v==0 || sqlit
1e70e 65 33 56 64 62 65 44 62 28 76 29 2d 3e 6d 61 6c  e3VdbeDb(v)->mal
1e70f 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61  locFailed );.  a
1e710 73 73 65 72 74 28 20 21 7a 20 7c 7c 20 21 69 73  ssert( !z || !is
1e711 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20  digit(z[n]) );. 
1e712 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1e713 52 28 6e 29 3b 0a 20 20 69 66 28 20 7a 20 29 7b  R(n);.  if( z ){
1e714 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75  .    double valu
1e715 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b  e;.    char *zV;
1e716 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46  .    sqlite3AtoF
1e717 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20  (z, &value);.   
1e718 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61   if( sqlite3IsNa
1e719 4e 28 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20  N(value) ){.    
1e71a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1e71b 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
1e71c 30 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 65  0, iMem);.    }e
1e71d 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  lse{.      if( n
1e71e 65 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75  egateFlag ) valu
1e71f 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20  e = -value;.    
1e720 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73    zV = dup8bytes
1e721 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75  (v, (char*)&valu
1e722 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
1e723 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1e724 50 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c  P_Real, 0, iMem,
1e725 20 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29   0, zV, P4_REAL)
1e726 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a  ;.    }.  }.}...
1e727 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
1e728 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  n instruction th
1e729 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20  at will put the 
1e72a 69 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65  integer describe
1e72b 20 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e   by.** text z[0.
1e72c 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73  .n-1] into regis
1e72d 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20  ter iMem..**.** 
1e72e 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77  The z[] string w
1e72f 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74  ill probably not
1e730 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   be zero-termina
1e731 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a  ted.  But the .*
1e732 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72  * z[n] character
1e733 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
1e734 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74  o be something t
1e735 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f  hat does not loo
1e736 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f  k.** like the co
1e737 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68  ntinuation of th
1e738 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
1e739 74 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74  tic void codeInt
1e73a 65 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78  eger(Vdbe *v, Ex
1e73b 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e  pr *pExpr, int n
1e73c 65 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d  egFlag, int iMem
1e73d 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
1e73e 2a 7a 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  *z;.  if( pExpr-
1e73f 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56  >flags & EP_IntV
1e740 61 6c 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20  alue ){.    int 
1e741 69 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  i = pExpr->iTabl
1e742 65 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46 6c  e;.    if( negFl
1e743 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20  ag ) i = -i;.   
1e744 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1e745 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
1e746 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 65  , i, iMem);.  }e
1e747 6c 73 65 20 69 66 28 20 28 7a 20 3d 20 28 63 68  lse if( (z = (ch
1e748 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
1e749 2e 7a 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  .z)!=0 ){.    in
1e74a 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  t i;.    int n =
1e74b 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b   pExpr->token.n;
1e74c 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73  .    assert( !is
1e74d 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20  digit(z[n]) );. 
1e74e 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 65     if( sqlite3Ge
1e74f 74 49 6e 74 33 32 28 7a 2c 20 26 69 29 20 29 7b  tInt32(z, &i) ){
1e750 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c  .      if( negFl
1e751 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20  ag ) i = -i;.   
1e752 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e753 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
1e754 65 72 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20  er, i, iMem);.  
1e755 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
1e756 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28  te3FitsIn64Bits(
1e757 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20  z, negFlag) ){. 
1e758 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a       i64 value;.
1e759 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a        char *zV;.
1e75a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f        sqlite3Ato
1e75b 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a  i64(z, &value);.
1e75c 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61        if( negFla
1e75d 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c  g ) value = -val
1e75e 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64  ue;.      zV = d
1e75f 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61  up8bytes(v, (cha
1e760 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20  r*)&value);.    
1e761 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1e762 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c  Op4(v, OP_Int64,
1e763 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c   0, iMem, 0, zV,
1e764 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20   P4_INT64);.    
1e765 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64  }else{.      cod
1e766 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 2c 20 6e  eReal(v, z, n, n
1e767 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20  egFlag, iMem);. 
1e768 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a     }.  }.}.../*.
1e769 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1e76a 20 74 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61   that will extra
1e76b 63 74 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74  ct the iColumn-t
1e76c 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a 2a 2a  h column from.**
1e76d 20 74 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20   table pTab and 
1e76e 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e  store the column
1e76f 20 76 61 6c 75 65 20 69 6e 20 61 20 72 65 67 69   value in a regi
1e770 73 74 65 72 2e 20 20 41 6e 20 65 66 66 6f 72 74  ster.  An effort
1e771 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 73  .** is made to s
1e772 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  tore the column 
1e773 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1e774 72 20 69 52 65 67 2c 20 62 75 74 20 74 68 69 73  r iReg, but this
1e775 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 61 72 61   is.** not guara
1e776 6e 74 65 65 64 2e 20 20 54 68 65 20 6c 6f 63 61  nteed.  The loca
1e777 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 75  tion of the colu
1e778 6d 6e 20 76 61 6c 75 65 20 69 73 20 72 65 74 75  mn value is retu
1e779 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  rned..**.** Ther
1e77a 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65  e must be an ope
1e77b 6e 20 63 75 72 73 6f 72 20 74 6f 20 70 54 61 62  n cursor to pTab
1e77c 20 69 6e 20 69 54 61 62 6c 65 20 77 68 65 6e 20   in iTable when 
1e77d 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1e77e 69 73 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69  is called.  If i
1e77f 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e 20 63 6f  Column<0 then co
1e780 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  de is generated 
1e781 74 68 61 74 20 65 78 74 72 61 63 74 73 20 74 68  that extracts th
1e782 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54  e rowid..**.** T
1e783 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68  his routine migh
1e784 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 75  t attempt to reu
1e785 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  se the value of 
1e786 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a  the column that.
1e787 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ** has already b
1e788 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20  een loaded into 
1e789 61 20 72 65 67 69 73 74 65 72 2e 20 20 54 68 65  a register.  The
1e78a 20 76 61 6c 75 65 20 77 69 6c 6c 20 61 6c 77 61   value will alwa
1e78b 79 73 0a 2a 2a 20 62 65 20 75 73 65 64 20 69 66  ys.** be used if
1e78c 20 69 74 20 68 61 73 20 6e 6f 74 20 75 6e 64 65   it has not unde
1e78d 72 67 6f 6e 65 20 61 6e 79 20 61 66 66 69 6e 69  rgone any affini
1e78e 74 79 20 63 68 61 6e 67 65 73 2e 20 20 42 75 74  ty changes.  But
1e78f 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66 69 6e 69   if.** an affini
1e790 74 79 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63  ty change has oc
1e791 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65  curred, then the
1e792 20 63 61 63 68 65 64 20 76 61 6c 75 65 20 77 69   cached value wi
1e793 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 75 73  ll only be.** us
1e794 65 64 20 69 66 20 61 6c 6c 6f 77 41 66 66 43 68  ed if allowAffCh
1e795 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 53  ng is true..*/.S
1e796 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1e797 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
1e798 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 20 50 61  eGetColumn(.  Pa
1e799 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f  rse *pParse,   /
1e79a 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
1e79b 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
1e79c 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
1e79d 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 44   *pTab,     /* D
1e79e 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
1e79f 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 72  e table we are r
1e7a0 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a 2f 0a 20  eading from */. 
1e7a1 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20   int iColumn,   
1e7a2 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
1e7a3 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a  e table column *
1e7a4 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
1e7a5 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73       /* The curs
1e7a6 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  or pointing to t
1e7a7 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
1e7a8 74 20 69 52 65 67 2c 20 20 20 20 20 20 20 20 2f  t iReg,        /
1e7a9 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20  * Store results 
1e7aa 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 61 6c  here */.  int al
1e7ab 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a 20 54 72  lowAffChng /* Tr
1e7ac 75 65 20 69 66 20 70 72 69 6f 72 20 61 66 66 69  ue if prior affi
1e7ad 6e 69 74 79 20 63 68 61 6e 67 65 73 20 61 72 65  nity changes are
1e7ae 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65   OK */.){.  Vdbe
1e7af 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
1e7b0 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  dbe;.  int i;.  
1e7b1 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
1e7b2 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c   *p;..  for(i=0,
1e7b3 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   p=pParse->aColC
1e7b4 61 63 68 65 3b 20 69 3c 70 50 61 72 73 65 2d 3e  ache; i<pParse->
1e7b5 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 2c 20  nColCache; i++, 
1e7b6 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  p++){.    if( p-
1e7b7 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20  >iTable==iTable 
1e7b8 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69  && p->iColumn==i
1e7b9 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20  Column.         
1e7ba 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68 61    && (!p->affCha
1e7bb 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66 43  nge || allowAffC
1e7bc 68 6e 67 29 20 29 7b 0a 23 69 66 20 30 0a 20 20  hng) ){.#if 0.  
1e7bd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1e7be 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f 6f 70  ddOp0(v, OP_Noop
1e7bf 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
1e7c0 6d 65 6e 74 28 28 76 2c 20 22 4f 50 54 3a 20 74  ment((v, "OPT: t
1e7c1 61 62 25 64 2e 63 6f 6c 25 64 20 2d 3e 20 72 25  ab%d.col%d -> r%
1e7c2 64 22 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c  d", iTable, iCol
1e7c3 75 6d 6e 2c 20 70 2d 3e 69 52 65 67 29 29 3b 0a  umn, p->iReg));.
1e7c4 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74  #endif.      ret
1e7c5 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20  urn p->iReg;.   
1e7c6 20 7d 0a 20 20 7d 20 20 0a 20 20 61 73 73 65 72   }.  }  .  asser
1e7c7 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28  t( v!=0 );.  if(
1e7c8 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20   iColumn<0 ){.  
1e7c9 20 20 69 6e 74 20 6f 70 20 3d 20 28 70 54 61 62    int op = (pTab
1e7ca 20 26 26 20 49 73 56 69 72 74 75 61 6c 28 70 54   && IsVirtual(pT
1e7cb 61 62 29 29 20 3f 20 4f 50 5f 56 52 6f 77 69 64  ab)) ? OP_VRowid
1e7cc 20 3a 20 4f 50 5f 52 6f 77 69 64 3b 0a 20 20 20   : OP_Rowid;.   
1e7cd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1e7ce 70 32 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65  p2(v, op, iTable
1e7cf 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65  , iReg);.  }else
1e7d0 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a   if( pTab==0 ){.
1e7d1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1e7d2 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
1e7d3 6d 6e 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c  mn, iTable, iCol
1e7d4 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65  umn, iReg);.  }e
1e7d5 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20  lse{.    int op 
1e7d6 3d 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  = IsVirtual(pTab
1e7d7 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a  ) ? OP_VColumn :
1e7d8 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20   OP_Column;.    
1e7d9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1e7da 33 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c  3(v, op, iTable,
1e7db 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b   iColumn, iReg);
1e7dc 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75  .    sqlite3Colu
1e7dd 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61  mnDefault(v, pTa
1e7de 62 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 23 69 66  b, iColumn);.#if
1e7df 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1e7e0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
1e7e1 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 61 43      if( pTab->aC
1e7e2 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69  ol[iColumn].affi
1e7e3 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
1e7e4 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20 20 20 73  _REAL ){.      s
1e7e5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
1e7e6 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e  (v, OP_RealAffin
1e7e7 69 74 79 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  ity, iReg);.    
1e7e8 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69  }.#endif.  }.  i
1e7e9 66 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62  f( pParse->disab
1e7ea 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 7b  leColCache==0 ){
1e7eb 0a 20 20 20 20 69 20 3d 20 70 50 61 72 73 65 2d  .    i = pParse-
1e7ec 3e 69 43 6f 6c 43 61 63 68 65 3b 0a 20 20 20 20  >iColCache;.    
1e7ed 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 43 6f  p = &pParse->aCo
1e7ee 6c 43 61 63 68 65 5b 69 5d 3b 0a 20 20 20 20 70  lCache[i];.    p
1e7ef 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c  ->iTable = iTabl
1e7f0 65 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d  e;.    p->iColum
1e7f1 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  n = iColumn;.   
1e7f2 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b   p->iReg = iReg;
1e7f3 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67  .    p->affChang
1e7f4 65 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a  e = 0;.    i++;.
1e7f5 20 20 20 20 69 66 28 20 69 3e 3d 41 72 72 61 79      if( i>=Array
1e7f6 53 69 7a 65 28 70 50 61 72 73 65 2d 3e 61 43 6f  Size(pParse->aCo
1e7f7 6c 43 61 63 68 65 29 20 29 20 69 20 3d 20 30 3b  lCache) ) i = 0;
1e7f8 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 72 73  .    if( i>pPars
1e7f9 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 20 29 20 70  e->nColCache ) p
1e7fa 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65  Parse->nColCache
1e7fb 20 3d 20 69 3b 0a 20 20 20 20 70 50 61 72 73 65   = i;.    pParse
1e7fc 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 69 3b  ->iColCache = i;
1e7fd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52  .  }.  return iR
1e7fe 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  eg;.}../*.** Cle
1e7ff 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61  ar all column ca
1e800 63 68 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f  che entries asso
1e801 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
1e802 76 64 62 65 0a 2a 2a 20 63 75 72 73 6f 72 20 77  vdbe.** cursor w
1e803 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ith cursor numbe
1e804 72 20 69 54 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  r iTable..*/.SQL
1e805 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1e806 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61   sqlite3ExprClea
1e807 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72  rColumnCache(Par
1e808 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
1e809 69 54 61 62 6c 65 29 7b 0a 20 20 69 66 28 20 69  iTable){.  if( i
1e80a 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 70  Table<0 ){.    p
1e80b 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65  Parse->nColCache
1e80c 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 65   = 0;.    pParse
1e80d 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 30 3b  ->iColCache = 0;
1e80e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
1e80f 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
1e810 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c  ; i<pParse->nCol
1e811 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  Cache; i++){.   
1e812 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61     if( pParse->a
1e813 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 54 61 62  ColCache[i].iTab
1e814 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20  le==iTable ){.  
1e815 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1e816 69 3d 3d 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43  i==pParse->nColC
1e817 61 63 68 65 2d 31 20 29 3b 0a 20 20 20 20 20 20  ache-1 );.      
1e818 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61    pParse->aColCa
1e819 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d  che[i] = pParse-
1e81a 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 50 61  >aColCache[--pPa
1e81b 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 5d 3b  rse->nColCache];
1e81c 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
1e81d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 61  >iColCache = pPa
1e81e 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 0a  rse->nColCache;.
1e81f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1e820 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72  }.}../*.** Recor
1e821 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20  d the fact that 
1e822 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e  an affinity chan
1e823 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 20  ge has occurred 
1e824 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a 20 72 65 67  on iCount.** reg
1e825 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20  isters starting 
1e826 77 69 74 68 20 69 53 74 61 72 74 2e 0a 2a 2f 0a  with iStart..*/.
1e827 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1e828 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
1e829 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
1e82a 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ge(Parse *pParse
1e82b 2c 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 6e  , int iStart, in
1e82c 74 20 69 43 6f 75 6e 74 29 7b 0a 20 20 69 6e 74  t iCount){.  int
1e82d 20 69 45 6e 64 20 3d 20 69 53 74 61 72 74 20 2b   iEnd = iStart +
1e82e 20 69 43 6f 75 6e 74 20 2d 20 31 3b 0a 20 20 69   iCount - 1;.  i
1e82f 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
1e830 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43   i<pParse->nColC
1e831 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ache; i++){.    
1e832 69 6e 74 20 72 20 3d 20 70 50 61 72 73 65 2d 3e  int r = pParse->
1e833 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65  aColCache[i].iRe
1e834 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 53  g;.    if( r>=iS
1e835 74 61 72 74 20 26 26 20 72 3c 3d 69 45 6e 64 20  tart && r<=iEnd 
1e836 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
1e837 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 61 66  >aColCache[i].af
1e838 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 20 20  fChange = 1;.   
1e839 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
1e83a 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1e83b 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 66 72   move content fr
1e83c 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46 72  om registers iFr
1e83d 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d  om...iFrom+nReg-
1e83e 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f  1.** over to iTo
1e83f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 4b 65  ..iTo+nReg-1. Ke
1e840 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61  ep the column ca
1e841 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 2e 0a  che up-to-date..
1e842 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e843 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
1e844 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65  prCodeMove(Parse
1e845 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46   *pParse, int iF
1e846 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e  rom, int iTo, in
1e847 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69  t nReg){.  int i
1e848 3b 0a 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69  ;.  if( iFrom==i
1e849 54 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  To ) return;.  s
1e84a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1e84b 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20  (pParse->pVdbe, 
1e84c 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20  OP_Move, iFrom, 
1e84d 69 54 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f  iTo, nReg);.  fo
1e84e 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d  r(i=0; i<pParse-
1e84f 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29  >nColCache; i++)
1e850 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 70 50  {.    int x = pP
1e851 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b  arse->aColCache[
1e852 69 5d 2e 69 52 65 67 3b 0a 20 20 20 20 69 66 28  i].iReg;.    if(
1e853 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69   x>=iFrom && x<i
1e854 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20  From+nReg ){.   
1e855 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43     pParse->aColC
1e856 61 63 68 65 5b 69 5d 2e 69 52 65 67 20 2b 3d 20  ache[i].iReg += 
1e857 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d  iTo-iFrom;.    }
1e858 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
1e859 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63  nerate code to c
1e85a 6f 70 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d  opy content from
1e85b 20 72 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d   registers iFrom
1e85c 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a  ...iFrom+nReg-1.
1e85d 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e  ** over to iTo..
1e85e 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53  iTo+nReg-1..*/.S
1e85f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1e860 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  id sqlite3ExprCo
1e861 64 65 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50  deCopy(Parse *pP
1e862 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c  arse, int iFrom,
1e863 20 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52   int iTo, int nR
1e864 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  eg){.  int i;.  
1e865 69 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29  if( iFrom==iTo )
1e866 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
1e867 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b 29  =0; i<nReg; i++)
1e868 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1e869 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e  eAddOp2(pParse->
1e86a 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c 20  pVdbe, OP_Copy, 
1e86b 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b  iFrom+i, iTo+i);
1e86c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
1e86d 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 79  turn true if any
1e86e 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65   register in the
1e86f 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 54   range iFrom..iT
1e870 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a 2a  o (inclusive).**
1e871 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74   is used as part
1e872 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63   of the column c
1e873 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ache..*/.static 
1e874 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e  int usedAsColumn
1e875 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61  Cache(Parse *pPa
1e876 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20  rse, int iFrom, 
1e877 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 20  int iTo){.  int 
1e878 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
1e879 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68  pParse->nColCach
1e87a 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  e; i++){.    int
1e87b 20 72 20 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f   r = pParse->aCo
1e87c 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 3b 0a  lCache[i].iReg;.
1e87d 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d      if( r>=iFrom
1e87e 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74   && r<=iTo ) ret
1e87f 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 1;.  }.  ret
1e880 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
1e881 54 68 65 72 65 20 69 73 20 61 20 76 61 6c 75 65  There is a value
1e882 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65   in register iRe
1e883 67 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 72 65 20  g..**.** We are 
1e884 67 6f 69 6e 67 20 74 6f 20 6d 6f 64 69 66 79 20  going to modify 
1e885 74 68 65 20 76 61 6c 75 65 2c 20 73 6f 20 77 65  the value, so we
1e886 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 73 75   need to make su
1e887 72 65 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20  re it.** is not 
1e888 61 20 63 61 63 68 65 64 20 72 65 67 69 73 74 65  a cached registe
1e889 72 2e 20 20 49 66 20 69 52 65 67 20 69 73 20 61  r.  If iReg is a
1e88a 20 63 61 63 68 65 64 20 72 65 67 69 73 74 65 72   cached register
1e88b 2c 0a 2a 2a 20 74 68 65 6e 20 63 6c 65 61 72 20  ,.** then clear 
1e88c 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1e88d 67 20 63 61 63 68 65 20 6c 69 6e 65 2e 0a 2a 2f  g cache line..*/
1e88e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1e88f 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1e890 57 72 69 74 61 62 6c 65 52 65 67 69 73 74 65 72  WritableRegister
1e891 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1e892 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74  int iReg){.  int
1e893 20 69 3b 0a 20 20 69 66 28 20 75 73 65 64 41 73   i;.  if( usedAs
1e894 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72  ColumnCache(pPar
1e895 73 65 2c 20 69 52 65 67 2c 20 69 52 65 67 29 20  se, iReg, iReg) 
1e896 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
1e897 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61  i<pParse->nColCa
1e898 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  che; i++){.     
1e899 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 43 6f   if( pParse->aCo
1e89a 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 3d 3d  lCache[i].iReg==
1e89b 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 20 20  iReg ){.        
1e89c 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
1e89d 65 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d 3e 61  e[i] = pParse->a
1e89e 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 50 61 72 73  ColCache[--pPars
1e89f 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 5d 3b 0a 20  e->nColCache];. 
1e8a0 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 69         pParse->i
1e8a1 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 61 72 73  ColCache = pPars
1e8a2 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 0a 20 20  e->nColCache;.  
1e8a3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1e8a4 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
1e8a5 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  last instruction
1e8a6 20 63 6f 64 65 64 20 69 73 20 61 6e 20 65 70 68   coded is an eph
1e8a7 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f 66 20 61  emeral copy of a
1e8a8 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 67  ny of.** the reg
1e8a9 69 73 74 65 72 73 20 69 6e 20 74 68 65 20 6e 52  isters in the nR
1e8aa 65 67 20 72 65 67 69 73 74 65 72 73 20 62 65 67  eg registers beg
1e8ab 69 6e 6e 69 6e 67 20 77 69 74 68 20 69 52 65 67  inning with iReg
1e8ac 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72  , then.** conver
1e8ad 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72  t the last instr
1e8ae 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f 50 5f 53  uction from OP_S
1e8af 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f 70 79 2e  Copy to OP_Copy.
1e8b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1e8b1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
1e8b2 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72 73  xprHardCopy(Pars
1e8b3 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
1e8b4 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a  Reg, int nReg){.
1e8b5 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 56 64    int addr;.  Vd
1e8b6 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62  beOp *pOp;.  Vdb
1e8b7 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20 70 50 61  e *v;..  v = pPa
1e8b8 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 64  rse->pVdbe;.  ad
1e8b9 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
1e8ba 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
1e8bb 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56    pOp = sqlite3V
1e8bc 64 62 65 47 65 74 4f 70 28 76 2c 20 61 64 64 72  dbeGetOp(v, addr
1e8bd 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  -1);.  assert( p
1e8be 4f 70 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62  Op || pParse->db
1e8bf 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1e8c0 3b 0a 20 20 69 66 28 20 70 4f 70 20 26 26 20 70  ;.  if( pOp && p
1e8c1 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53  Op->opcode==OP_S
1e8c2 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e  Copy && pOp->p1>
1e8c3 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e 70 31  =iReg && pOp->p1
1e8c4 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20  <iReg+nReg ){.  
1e8c5 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
1e8c6 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a  OP_Copy;.  }.}..
1e8c7 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
1e8c8 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ode to store the
1e8c9 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 41   value of the iA
1e8ca 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 6e  lias-th alias in
1e8cb 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 61 72   register.** tar
1e8cc 67 65 74 2e 20 20 54 68 65 20 66 69 72 73 74 20  get.  The first 
1e8cd 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c  time this is cal
1e8ce 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20 65 76  led, pExpr is ev
1e8cf 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d 70 75  aluated to compu
1e8d0 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  te.** the value 
1e8d1 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20 20 54  of the alias.  T
1e8d2 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72  he value is stor
1e8d3 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c 69 61  ed in an auxilia
1e8d4 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61  ry register.** a
1e8d5 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nd the number of
1e8d6 20 74 68 61 74 20 72 65 67 69 73 74 65 72 20 69   that register i
1e8d7 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20  s returned.  On 
1e8d8 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1e8d9 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65  ,.** the registe
1e8da 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65 74 75  r number is retu
1e8db 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67 65 6e  rned without gen
1e8dc 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f 64 65  erating any code
1e8dd 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
1e8de 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 74  t in order for t
1e8df 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64  his to work, cod
1e8e0 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65 72 61  e must be genera
1e8e1 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61  ted in the.** sa
1e8e2 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20 69 74  me order that it
1e8e3 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a   is executed..**
1e8e4 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72 65 20  .** Aliases are 
1e8e5 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e  numbered startin
1e8e6 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20 69 41  g with 1.  So iA
1e8e7 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65 20 72  lias is in the r
1e8e8 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20  ange.** of 1 to 
1e8e9 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69  pParse->nAlias i
1e8ea 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a  nclusive.  .**.*
1e8eb 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73  * pParse->aAlias
1e8ec 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72  [iAlias-1] recor
1e8ed 64 73 20 74 68 65 20 72 65 67 69 73 74 65 72 20  ds the register 
1e8ee 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 68 65  number where the
1e8ef 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65   value.** of the
1e8f0 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73   iAlias-th alias
1e8f1 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49 66 20   is stored.  If 
1e8f2 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73  zero, that means
1e8f3 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69   that the.** ali
1e8f4 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  as has not yet b
1e8f5 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f  een computed..*/
1e8f6 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65  .static int code
1e8f7 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70 50 61  Alias(Parse *pPa
1e8f8 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c  rse, int iAlias,
1e8f9 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
1e8fa 74 20 74 61 72 67 65 74 29 7b 0a 20 20 73 71 6c  t target){.  sql
1e8fb 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1e8fc 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 52 65  e->db;.  int iRe
1e8fd 67 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  g;.  if( pParse-
1e8fe 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c 70 50 61  >nAliasAlloc<pPa
1e8ff 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 7b 0a 20  rse->nAlias ){. 
1e900 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61     pParse->aAlia
1e901 73 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  s = sqlite3DbRea
1e902 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70  llocOrFree(db, p
1e903 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 2c 0a 20  Parse->aAlias,. 
1e904 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e905 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e906 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61  sizeof(pParse->a
1e907 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65  Alias[0])*pParse
1e908 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20  ->nAlias );.    
1e909 74 65 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61  testcase( db->ma
1e90a 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 70 50  llocFailed && pP
1e90b 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f  arse->nAliasAllo
1e90c 63 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 64  c>0 );.    if( d
1e90d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1e90e 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
1e90f 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e  memset(&pParse->
1e910 61 41 6c 69 61 73 5b 70 50 61 72 73 65 2d 3e 6e  aAlias[pParse->n
1e911 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20 30 2c 0a  AliasAlloc], 0,.
1e912 20 20 20 20 20 20 20 20 20 20 20 28 70 50 61 72             (pPar
1e913 73 65 2d 3e 6e 41 6c 69 61 73 2d 70 50 61 72 73  se->nAlias-pPars
1e914 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 29 2a  e->nAliasAlloc)*
1e915 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61  sizeof(pParse->a
1e916 41 6c 69 61 73 5b 30 5d 29 29 3b 0a 20 20 20 20  Alias[0]));.    
1e917 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c  pParse->nAliasAl
1e918 6c 6f 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 41  loc = pParse->nA
1e919 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65  lias;.  }.  asse
1e91a 72 74 28 20 69 41 6c 69 61 73 3e 30 20 26 26 20  rt( iAlias>0 && 
1e91b 69 41 6c 69 61 73 3c 3d 70 50 61 72 73 65 2d 3e  iAlias<=pParse->
1e91c 6e 41 6c 69 61 73 20 29 3b 0a 20 20 69 52 65 67  nAlias );.  iReg
1e91d 20 3d 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61   = pParse->aAlia
1e91e 73 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a 20 20 69  s[iAlias-1];.  i
1e91f 66 28 20 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20  f( iReg==0 ){.  
1e920 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69    if( pParse->di
1e921 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 20 29 7b  sableColCache ){
1e922 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 73 71  .      iReg = sq
1e923 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72  lite3ExprCodeTar
1e924 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70  get(pParse, pExp
1e925 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  r, target);.    
1e926 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 52 65  }else{.      iRe
1e927 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  g = ++pParse->nM
1e928 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  em;.      sqlite
1e929 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
1e92a 2c 20 70 45 78 70 72 2c 20 69 52 65 67 29 3b 0a  , pExpr, iReg);.
1e92b 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41        pParse->aA
1e92c 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 3d  lias[iAlias-1] =
1e92d 20 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d   iReg;.    }.  }
1e92e 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a  .  return iReg;.
1e92f 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
1e930 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 65 20  e code into the 
1e931 63 75 72 72 65 6e 74 20 56 64 62 65 20 74 6f 20  current Vdbe to 
1e932 65 76 61 6c 75 61 74 65 20 74 68 65 20 67 69 76  evaluate the giv
1e933 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  en.** expression
1e934 2e 20 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74  .  Attempt to st
1e935 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ore the results 
1e936 69 6e 20 72 65 67 69 73 74 65 72 20 22 74 61 72  in register "tar
1e937 67 65 74 22 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  get"..** Return 
1e938 74 68 65 20 72 65 67 69 73 74 65 72 20 77 68 65  the register whe
1e939 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 73  re results are s
1e93a 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 69 74  tored..**.** Wit
1e93b 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  h this routine, 
1e93c 74 68 65 72 65 20 69 73 20 6e 6f 20 67 75 61 72  there is no guar
1e93d 61 6e 74 65 65 20 74 68 61 74 20 72 65 73 75 6c  antee that resul
1e93e 74 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 73 74  ts will.** be st
1e93f 6f 72 65 64 20 69 6e 20 74 61 72 67 65 74 2e 20  ored in target. 
1e940 20 54 68 65 20 72 65 73 75 6c 74 20 6d 69 67 68   The result migh
1e941 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 73  t be stored in s
1e942 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 72 65 67  ome other.** reg
1e943 69 73 74 65 72 20 69 66 20 69 74 20 69 73 20 63  ister if it is c
1e944 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64 6f 20  onvenient to do 
1e945 73 6f 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  so.  The calling
1e946 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73   function.** mus
1e947 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75  t check the retu
1e948 72 6e 20 63 6f 64 65 20 61 6e 64 20 6d 6f 76 65  rn code and move
1e949 20 74 68 65 20 72 65 73 75 6c 74 73 20 74 6f 20   the results to 
1e94a 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a 20 72  the desired.** r
1e94b 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  egister..*/.SQLI
1e94c 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1e94d 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
1e94e 72 67 65 74 28 50 61 72 73 65 20 2a 70 50 61 72  rget(Parse *pPar
1e94f 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c  se, Expr *pExpr,
1e950 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20   int target){.  
1e951 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
1e952 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 54 68 65  ->pVdbe;  /* The
1e953 20 56 4d 20 75 6e 64 65 72 20 63 6f 6e 73 74 72   VM under constr
1e954 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  uction */.  int 
1e955 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
1e956 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 63        /* The opc
1e957 6f 64 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20  ode being coded 
1e958 2a 2f 0a 20 20 69 6e 74 20 69 6e 52 65 67 20 3d  */.  int inReg =
1e959 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 20 2f   target;       /
1e95a 2a 20 52 65 73 75 6c 74 73 20 73 74 6f 72 65 64  * Results stored
1e95b 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 6e 52   in register inR
1e95c 65 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46  eg */.  int regF
1e95d 72 65 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20  ree1 = 0;       
1e95e 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f    /* If non-zero
1e95f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f   free this tempo
1e960 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f  rary register */
1e961 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 32 20  .  int regFree2 
1e962 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
1e963 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65  If non-zero free
1e964 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20   this temporary 
1e965 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e  register */.  in
1e966 74 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 72 34  t r1, r2, r3, r4
1e967 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f  ;       /* Vario
1e968 75 73 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62  us register numb
1e969 65 72 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ers */.  sqlite3
1e96a 20 2a 64 62 3b 0a 0a 20 20 64 62 20 3d 20 70 50   *db;..  db = pP
1e96b 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65  arse->db;.  asse
1e96c 72 74 28 20 76 21 3d 30 20 7c 7c 20 64 62 2d 3e  rt( v!=0 || db->
1e96d 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
1e96e 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74    assert( target
1e96f 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50  >0 && target<=pP
1e970 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  arse->nMem );.  
1e971 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72  if( v==0 ) retur
1e972 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 45 78 70  n 0;..  if( pExp
1e973 72 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d  r==0 ){.    op =
1e974 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73   TK_NULL;.  }els
1e975 65 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70  e{.    op = pExp
1e976 72 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69  r->op;.  }.  swi
1e977 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
1e978 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  ase TK_AGG_COLUM
1e979 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e  N: {.      AggIn
1e97a 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70  fo *pAggInfo = p
1e97b 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a  Expr->pAggInfo;.
1e97c 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67        struct Agg
1e97d 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d  Info_col *pCol =
1e97e 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c   &pAggInfo->aCol
1e97f 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 3b 0a 20  [pExpr->iAgg];. 
1e980 20 20 20 20 20 69 66 28 20 21 70 41 67 67 49 6e       if( !pAggIn
1e981 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 29  fo->directMode )
1e982 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1e983 28 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 20 29  ( pCol->iMem>0 )
1e984 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20  ;.        inReg 
1e985 3d 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a 20 20  = pCol->iMem;.  
1e986 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e987 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67     }else if( pAg
1e988 67 49 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e  gInfo->useSortin
1e989 67 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20  gIdx ){.        
1e98a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1e98b 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
1e98c 70 41 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e  pAggInfo->sortin
1e98d 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20  gIdx,.          
1e98e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e98f 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65      pCol->iSorte
1e990 72 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29  rColumn, target)
1e991 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1e992 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
1e993 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c  * Otherwise, fal
1e994 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20  l thru into the 
1e995 54 4b 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a  TK_COLUMN case *
1e996 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  /.    }.    case
1e997 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20   TK_COLUMN: {.  
1e998 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69      if( pExpr->i
1e999 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20  Table<0 ){.     
1e99a 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20     /* This only 
1e99b 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64  happens when cod
1e99c 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72  ing check constr
1e99d 61 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20  aints */.       
1e99e 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
1e99f 3e 63 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20  >ckBase>0 );.   
1e9a0 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78       inReg = pEx
1e9a1 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50  pr->iColumn + pP
1e9a2 61 72 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20  arse->ckBase;.  
1e9a3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e9a4 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70 45     testcase( (pE
1e9a5 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
1e9a6 41 6e 79 41 66 66 29 21 3d 30 20 29 3b 0a 20 20  AnyAff)!=0 );.  
1e9a7 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71        inReg = sq
1e9a8 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
1e9a9 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70  Column(pParse, p
1e9aa 45 78 70 72 2d 3e 70 54 61 62 2c 0a 20 20 20 20  Expr->pTab,.    
1e9ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9ac 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
1e9ad 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45 78  pr->iColumn, pEx
1e9ae 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67  pr->iTable, targ
1e9af 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  et,.            
1e9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9b1 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67       pExpr->flag
1e9b2 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 3b 0a  s & EP_AnyAff);.
1e9b3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1e9b4 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1e9b5 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20  ase TK_INTEGER: 
1e9b6 7b 0a 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65  {.      codeInte
1e9b7 67 65 72 28 76 2c 20 70 45 78 70 72 2c 20 30 2c  ger(v, pExpr, 0,
1e9b8 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
1e9b9 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1e9ba 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20   case TK_FLOAT: 
1e9bb 7b 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c  {.      codeReal
1e9bc 28 76 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72  (v, (char*)pExpr
1e9bd 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72  ->token.z, pExpr
1e9be 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 30 2c 20 74 61  ->token.n, 0, ta
1e9bf 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65  rget);.      bre
1e9c0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1e9c1 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a  se TK_STRING: {.
1e9c2 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 71        sqlite3Deq
1e9c3 75 6f 74 65 45 78 70 72 28 64 62 2c 20 70 45 78  uoteExpr(db, pEx
1e9c4 70 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  pr);.      sqlit
1e9c5 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 4f  e3VdbeAddOp4(v,O
1e9c6 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 74 61  P_String8, 0, ta
1e9c7 72 67 65 74 2c 20 30 2c 0a 20 20 20 20 20 20 20  rget, 0,.       
1e9c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9c9 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74   (char*)pExpr->t
1e9ca 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74  oken.z, pExpr->t
1e9cb 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 62  oken.n);.      b
1e9cc 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1e9cd 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a  case TK_NULL: {.
1e9ce 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1e9cf 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
1e9d0 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a  ll, 0, target);.
1e9d1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e9d2 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1e9d3 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
1e9d4 52 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  RAL.    case TK_
1e9d5 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e  BLOB: {.      in
1e9d6 74 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  t n;.      const
1e9d7 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20   char *z;.      
1e9d8 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20  char *zBlob;.   
1e9d9 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1e9da 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a  ->token.n>=3 );.
1e9db 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
1e9dc 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d  xpr->token.z[0]=
1e9dd 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 74  ='x' || pExpr->t
1e9de 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 58 27 20 29  oken.z[0]=='X' )
1e9df 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1e9e0 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31  pExpr->token.z[1
1e9e1 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20  ]=='\'' );.     
1e9e2 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1e9e3 74 6f 6b 65 6e 2e 7a 5b 70 45 78 70 72 2d 3e 74  token.z[pExpr->t
1e9e4 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d 27 5c 27 27 20  oken.n-1]=='\'' 
1e9e5 29 3b 0a 20 20 20 20 20 20 6e 20 3d 20 70 45 78  );.      n = pEx
1e9e6 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 2d 20 33 3b  pr->token.n - 3;
1e9e7 0a 20 20 20 20 20 20 7a 20 3d 20 28 63 68 61 72  .      z = (char
1e9e8 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  *)pExpr->token.z
1e9e9 20 2b 20 32 3b 0a 20 20 20 20 20 20 7a 42 6c 6f   + 2;.      zBlo
1e9ea 62 20 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f  b = sqlite3HexTo
1e9eb 42 6c 6f 62 28 73 71 6c 69 74 65 33 56 64 62 65  Blob(sqlite3Vdbe
1e9ec 44 62 28 76 29 2c 20 7a 2c 20 6e 29 3b 0a 20 20  Db(v), z, n);.  
1e9ed 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1e9ee 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62  ddOp4(v, OP_Blob
1e9ef 2c 20 6e 2f 32 2c 20 74 61 72 67 65 74 2c 20 30  , n/2, target, 0
1e9f0 2c 20 7a 42 6c 6f 62 2c 20 50 34 5f 44 59 4e 41  , zBlob, P4_DYNA
1e9f1 4d 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61  MIC);.      brea
1e9f2 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
1e9f3 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49      case TK_VARI
1e9f4 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 73 71  ABLE: {.      sq
1e9f5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1e9f6 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20  v, OP_Variable, 
1e9f7 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74  pExpr->iTable, t
1e9f8 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 69 66  arget);.      if
1e9f9 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  ( pExpr->token.n
1e9fa 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  >1 ){.        sq
1e9fb 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1e9fc 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 2a 29  4(v, -1, (char*)
1e9fd 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20  pExpr->token.z, 
1e9fe 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b  pExpr->token.n);
1e9ff 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1ea00 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1ea01 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52  case TK_REGISTER
1ea02 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20  : {.      inReg 
1ea03 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b  = pExpr->iTable;
1ea04 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1ea05 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1ea06 41 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65  AS: {.      inRe
1ea07 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 50  g = codeAlias(pP
1ea08 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 69 54 61  arse, pExpr->iTa
1ea09 62 6c 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  ble, pExpr->pLef
1ea0a 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
1ea0b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1ea0c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1ea0d 49 54 5f 43 41 53 54 0a 20 20 20 20 63 61 73 65  IT_CAST.    case
1ea0e 20 54 4b 5f 43 41 53 54 3a 20 7b 0a 20 20 20 20   TK_CAST: {.    
1ea0f 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73    /* Expressions
1ea10 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 20   of the form:   
1ea11 43 41 53 54 28 70 4c 65 66 74 20 41 53 20 74 6f  CAST(pLeft AS to
1ea12 6b 65 6e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ken) */.      in
1ea13 74 20 61 66 66 2c 20 74 6f 5f 6f 70 3b 0a 20 20  t aff, to_op;.  
1ea14 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69      inReg = sqli
1ea15 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65  te3ExprCodeTarge
1ea16 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  t(pParse, pExpr-
1ea17 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b  >pLeft, target);
1ea18 0a 20 20 20 20 20 20 61 66 66 20 3d 20 73 71 6c  .      aff = sql
1ea19 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
1ea1a 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b  (&pExpr->token);
1ea1b 0a 20 20 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61  .      to_op = a
1ea1c 66 66 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff - SQLITE_AFF_
1ea1d 54 45 58 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74  TEXT + OP_ToText
1ea1e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ea1f 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74  to_op==OP_ToText
1ea20 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
1ea21 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20 29  TE_AFF_TEXT    )
1ea22 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ea23 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62  to_op==OP_ToBlob
1ea24 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
1ea25 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29  TE_AFF_NONE    )
1ea26 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ea27 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65  to_op==OP_ToNume
1ea28 72 69 63 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49  ric || aff!=SQLI
1ea29 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29  TE_AFF_NUMERIC )
1ea2a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ea2b 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20  to_op==OP_ToInt 
1ea2c 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
1ea2d 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29  TE_AFF_INTEGER )
1ea2e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ea2f 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c  to_op==OP_ToReal
1ea30 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
1ea31 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 29  TE_AFF_REAL    )
1ea32 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1ea33 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65  ( to_op==OP_ToTe
1ea34 78 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  xt );.      test
1ea35 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  case( to_op==OP_
1ea36 54 6f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20  ToBlob );.      
1ea37 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d  testcase( to_op=
1ea38 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b  =OP_ToNumeric );
1ea39 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1ea3a 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74   to_op==OP_ToInt
1ea3b 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1ea3c 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  se( to_op==OP_To
1ea3d 52 65 61 6c 20 29 3b 0a 20 20 20 20 20 20 69 66  Real );.      if
1ea3e 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20  ( inReg!=target 
1ea3f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1ea40 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1ea41 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c  OP_SCopy, inReg,
1ea42 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
1ea43 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74    inReg = target
1ea44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1ea45 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ea46 31 28 76 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65  1(v, to_op, inRe
1ea47 67 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  g);.      testca
1ea48 73 65 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e  se( usedAsColumn
1ea49 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e  Cache(pParse, in
1ea4a 52 65 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20  Reg, inReg) );. 
1ea4b 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1ea4c 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
1ea4d 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65  nge(pParse, inRe
1ea4e 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65  g, 1);.      bre
1ea4f 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
1ea50 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1ea51 43 41 53 54 20 2a 2f 0a 20 20 20 20 63 61 73 65  CAST */.    case
1ea52 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65   TK_LT:.    case
1ea53 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65   TK_LE:.    case
1ea54 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65   TK_GT:.    case
1ea55 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65   TK_GE:.    case
1ea56 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65   TK_NE:.    case
1ea57 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20   TK_EQ: {.      
1ea58 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f  assert( TK_LT==O
1ea59 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Lt );.      as
1ea5a 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f  sert( TK_LE==OP_
1ea5b 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Le );.      asse
1ea5c 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74  rt( TK_GT==OP_Gt
1ea5d 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1ea5e 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29  ( TK_GE==OP_Ge )
1ea5f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ea60 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a  TK_EQ==OP_Eq );.
1ea61 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1ea62 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20  _NE==OP_Ne );.  
1ea63 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1ea64 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20  ==TK_LT );.     
1ea65 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1ea66 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_LE );.      te
1ea67 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
1ea68 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
1ea69 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29  ase( op==TK_GE )
1ea6a 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1ea6b 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20  ( op==TK_EQ );. 
1ea6c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1ea6d 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20  p==TK_NE );.    
1ea6e 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
1ea6f 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
1ea70 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
1ea71 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
1ea72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ea73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1ea74 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
1ea75 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
1ea76 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
1ea77 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1ea78 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
1ea79 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20  ght, op,.       
1ea7a 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72             r1, r
1ea7b 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45  2, inReg, SQLITE
1ea7c 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20  _STOREP2);.     
1ea7d 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
1ea7e 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
1ea7f 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1ea80 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e2==0 );.      b
1ea81 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1ea82 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20  case TK_AND:.   
1ea83 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20   case TK_OR:.   
1ea84 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20   case TK_PLUS:. 
1ea85 20 20 20 63 61 73 65 20 54 4b 5f 53 54 41 52 3a     case TK_STAR:
1ea86 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e  .    case TK_MIN
1ea87 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  US:.    case TK_
1ea88 52 45 4d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  REM:.    case TK
1ea89 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20 63 61 73  _BITAND:.    cas
1ea8a 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20  e TK_BITOR:.    
1ea8b 63 61 73 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20  case TK_SLASH:. 
1ea8c 20 20 20 63 61 73 65 20 54 4b 5f 4c 53 48 49 46     case TK_LSHIF
1ea8d 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52  T:.    case TK_R
1ea8e 53 48 49 46 54 3a 20 0a 20 20 20 20 63 61 73 65  SHIFT: .    case
1ea8f 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20   TK_CONCAT: {.  
1ea90 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 41      assert( TK_A
1ea91 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20  ND==OP_And );.  
1ea92 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4f      assert( TK_O
1ea93 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20  R==OP_Or );.    
1ea94 20 20 61 73 73 65 72 74 28 20 54 4b 5f 50 4c 55    assert( TK_PLU
1ea95 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20  S==OP_Add );.   
1ea96 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4d 49     assert( TK_MI
1ea97 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74  NUS==OP_Subtract
1ea98 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1ea99 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d  ( TK_REM==OP_Rem
1ea9a 61 69 6e 64 65 72 20 29 3b 0a 20 20 20 20 20 20  ainder );.      
1ea9b 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 41 4e  assert( TK_BITAN
1ea9c 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a  D==OP_BitAnd );.
1ea9d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1ea9e 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72  _BITOR==OP_BitOr
1ea9f 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1eaa0 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44  ( TK_SLASH==OP_D
1eaa1 69 76 69 64 65 20 29 3b 0a 20 20 20 20 20 20 61  ivide );.      a
1eaa2 73 73 65 72 74 28 20 54 4b 5f 4c 53 48 49 46 54  ssert( TK_LSHIFT
1eaa3 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29  ==OP_ShiftLeft )
1eaa4 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1eaa5 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68  TK_RSHIFT==OP_Sh
1eaa6 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20  iftRight );.    
1eaa7 20 20 61 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e    assert( TK_CON
1eaa8 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29  CAT==OP_Concat )
1eaa9 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1eaaa 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a  ( op==TK_AND );.
1eaab 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1eaac 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 20  op==TK_OR );.   
1eaad 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1eaae 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a 20 20 20 20  =TK_PLUS );.    
1eaaf 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1eab0 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a 20 20 20 20  TK_MINUS );.    
1eab1 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1eab2 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 20 20 20 20  TK_REM );.      
1eab3 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1eab4 5f 42 49 54 41 4e 44 20 29 3b 0a 20 20 20 20 20  _BITAND );.     
1eab5 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1eab6 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 20 20 20 20  K_BITOR );.     
1eab7 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1eab8 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 20 20 20 20  K_SLASH );.     
1eab9 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1eaba 4b 5f 4c 53 48 49 46 54 20 29 3b 0a 20 20 20 20  K_LSHIFT );.    
1eabb 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1eabc 54 4b 5f 52 53 48 49 46 54 20 29 3b 0a 20 20 20  TK_RSHIFT );.   
1eabd 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1eabe 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b 0a 20 20  =TK_CONCAT );.  
1eabf 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
1eac0 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
1eac1 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1eac2 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
1eac3 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65       r2 = sqlite
1eac4 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1eac5 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69  arse, pExpr->pRi
1eac6 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b  ght, &regFree2);
1eac7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1eac8 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20  beAddOp3(v, op, 
1eac9 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b  r2, r1, target);
1eaca 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1eacb 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
1eacc 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1eacd 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20  regFree2==0 );. 
1eace 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1eacf 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d  }.    case TK_UM
1ead0 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 45 78  INUS: {.      Ex
1ead1 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70  pr *pLeft = pExp
1ead2 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r->pLeft;.      
1ead3 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b  assert( pLeft );
1ead4 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74  .      if( pLeft
1ead5 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c  ->op==TK_FLOAT |
1ead6 7c 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  | pLeft->op==TK_
1ead7 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20  INTEGER ){.     
1ead8 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70     if( pLeft->op
1ead9 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 7b 0a 20 20  ==TK_FLOAT ){.  
1eada 20 20 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c          codeReal
1eadb 28 76 2c 20 28 63 68 61 72 2a 29 70 4c 65 66 74  (v, (char*)pLeft
1eadc 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 4c 65 66 74  ->token.z, pLeft
1eadd 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 31 2c 20 74 61  ->token.n, 1, ta
1eade 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  rget);.        }
1eadf 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1eae0 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70  codeInteger(v, p
1eae1 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74 29  Left, 1, target)
1eae2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1eae3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1eae4 20 72 65 67 46 72 65 65 31 20 3d 20 72 31 20 3d   regFree1 = r1 =
1eae5 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
1eae6 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
1eae7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1eae8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
1eae9 67 65 72 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20  ger, 0, r1);.   
1eaea 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65       r2 = sqlite
1eaeb 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1eaec 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
1eaed 66 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a  ft, &regFree2);.
1eaee 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1eaef 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1eaf0 53 75 62 74 72 61 63 74 2c 20 72 32 2c 20 72 31  Subtract, r2, r1
1eaf1 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
1eaf2 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1eaf3 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20  Free2==0 );.    
1eaf4 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 67 20    }.      inReg 
1eaf5 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20  = target;.      
1eaf6 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1eaf7 20 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54 3a   case TK_BITNOT:
1eaf8 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
1eaf9 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
1eafa 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f 50 5f  ( TK_BITNOT==OP_
1eafb 42 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20  BitNot );.      
1eafc 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 3d 3d  assert( TK_NOT==
1eafd 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20  OP_Not );.      
1eafe 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1eaff 5f 42 49 54 4e 4f 54 20 29 3b 0a 20 20 20 20 20  _BITNOT );.     
1eb00 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1eb01 4b 5f 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 72  K_NOT );.      r
1eb02 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
1eb03 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
1eb04 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
1eb05 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20  egFree1);.      
1eb06 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1eb07 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  e1==0 );.      i
1eb08 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20  nReg = target;. 
1eb09 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1eb0a 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31  AddOp2(v, op, r1
1eb0b 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20  , inReg);.      
1eb0c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1eb0d 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a   case TK_ISNULL:
1eb0e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
1eb0f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e  NULL: {.      in
1eb10 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61 73  t addr;.      as
1eb11 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d  sert( TK_ISNULL=
1eb12 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20  =OP_IsNull );.  
1eb13 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e      assert( TK_N
1eb14 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75  OTNULL==OP_NotNu
1eb15 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ll );.      test
1eb16 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e  case( op==TK_ISN
1eb17 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ULL );.      tes
1eb18 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f  tcase( op==TK_NO
1eb19 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73  TNULL );.      s
1eb1a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1eb1b 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1eb1c 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  1, target);.    
1eb1d 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
1eb1e 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
1eb1f 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
1eb20 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20   &regFree1);.   
1eb21 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1eb22 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
1eb23 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
1eb24 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 6f 70  VdbeAddOp1(v, op
1eb25 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
1eb26 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1eb27 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 61 72  , OP_AddImm, tar
1eb28 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  get, -1);.      
1eb29 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1eb2a 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20  ere(v, addr);.  
1eb2b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1eb2c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47  .    case TK_AGG
1eb2d 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20  _FUNCTION: {.   
1eb2e 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66     AggInfo *pInf
1eb2f 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49  o = pExpr->pAggI
1eb30 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70  nfo;.      if( p
1eb31 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Info==0 ){.     
1eb32 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1eb33 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75  sg(pParse, "misu
1eb34 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 3a  se of aggregate:
1eb35 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %T",.          
1eb36 20 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b    &pExpr->span);
1eb37 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1eb38 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 49        inReg = pI
1eb39 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78 70 72  nfo->aFunc[pExpr
1eb3a 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20  ->iAgg].iMem;.  
1eb3b 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
1eb3c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1eb3d 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a  e TK_CONST_FUNC:
1eb3e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e  .    case TK_FUN
1eb3f 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45  CTION: {.      E
1eb40 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
1eb41 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20   pExpr->pList;. 
1eb42 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72 20 3d       int nExpr =
1eb43 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e   pList ? pList->
1eb44 6e 45 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 20  nExpr : 0;.     
1eb45 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a   FuncDef *pDef;.
1eb46 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 0a 20        int nId;. 
1eb47 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1eb48 2a 7a 49 64 3b 0a 20 20 20 20 20 20 69 6e 74 20  *zId;.      int 
1eb49 63 6f 6e 73 74 4d 61 73 6b 20 3d 20 30 3b 0a 20  constMask = 0;. 
1eb4a 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
1eb4b 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64    u8 enc = ENC(d
1eb4c 62 29 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65  b);.      CollSe
1eb4d 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 0a 20  q *pColl = 0;.. 
1eb4e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1eb4f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43  p==TK_CONST_FUNC
1eb50 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1eb51 73 65 28 20 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54  se( op==TK_FUNCT
1eb52 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 7a 49 64  ION );.      zId
1eb53 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d   = (char*)pExpr-
1eb54 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20  >token.z;.      
1eb55 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b  nId = pExpr->tok
1eb56 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44 65 66  en.n;.      pDef
1eb57 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
1eb58 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49 64 2c 20  nction(db, zId, 
1eb59 6e 49 64 2c 20 6e 45 78 70 72 2c 20 65 6e 63 2c  nId, nExpr, enc,
1eb5a 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72   0);.      asser
1eb5b 74 28 20 70 44 65 66 21 3d 30 20 29 3b 0a 20 20  t( pDef!=0 );.  
1eb5c 20 20 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b      if( pList ){
1eb5d 0a 20 20 20 20 20 20 20 20 6e 45 78 70 72 20 3d  .        nExpr =
1eb5e 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20   pList->nExpr;. 
1eb5f 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69         r1 = sqli
1eb60 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
1eb61 70 50 61 72 73 65 2c 20 6e 45 78 70 72 29 3b 0a  pParse, nExpr);.
1eb62 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1eb63 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
1eb64 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 72  pParse, pList, r
1eb65 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c  1, 1);.      }el
1eb66 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 45 78 70  se{.        nExp
1eb67 72 20 3d 20 72 31 20 3d 20 30 3b 0a 20 20 20 20  r = r1 = 0;.    
1eb68 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
1eb69 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1eb6a 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 6f  ABLE.      /* Po
1eb6b 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20  ssibly overload 
1eb6c 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  the function if 
1eb6d 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
1eb6e 6e 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61  nt is.      ** a
1eb6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63   virtual table c
1eb70 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a  olumn..      **.
1eb71 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e 66        ** For inf
1eb72 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c 49  ix functions (LI
1eb73 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 50  KE, GLOB, REGEXP
1eb74 2c 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 65  , and MATCH) use
1eb75 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65   the.      ** se
1eb76 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 6e  cond argument, n
1eb77 6f 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 73  ot the first, as
1eb78 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f   the argument to
1eb79 20 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 2a   test to.      *
1eb7a 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20 61  * see if it is a
1eb7b 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72   column in a vir
1eb7c 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69  tual table.  Thi
1eb7d 73 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 73  s is done becaus
1eb7e 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c  e.      ** the l
1eb7f 65 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 69  eft operand of i
1eb80 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28  nfix functions (
1eb81 74 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 77  the operand we w
1eb82 61 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ant to.      ** 
1eb83 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64  control overload
1eb84 69 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20  ing) ends up as 
1eb85 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
1eb86 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20  ent to the.     
1eb87 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54   ** function.  T
1eb88 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41  he expression "A
1eb89 20 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69   glob B" is equi
1eb8a 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20  valent to .     
1eb8b 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20   ** "glob(B,A). 
1eb8c 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20   We want to use 
1eb8d 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62  the A in "A glob
1eb8e 20 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 20   B" to test.    
1eb8f 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 6f    ** for functio
1eb90 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 20  n overloading.  
1eb91 42 75 74 20 77 65 20 75 73 65 20 74 68 65 20 42  But we use the B
1eb92 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 42   term in "glob(B
1eb93 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ,A)"..      */. 
1eb94 20 20 20 20 20 69 66 28 20 6e 45 78 70 72 3e 3d       if( nExpr>=
1eb95 32 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c 61  2 && (pExpr->fla
1eb96 67 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 6e  gs & EP_InfixFun
1eb97 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  c) ){.        pD
1eb98 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62  ef = sqlite3Vtab
1eb99 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e  OverloadFunction
1eb9a 28 64 62 2c 20 70 44 65 66 2c 20 6e 45 78 70 72  (db, pDef, nExpr
1eb9b 2c 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  , pList->a[1].pE
1eb9c 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  xpr);.      }els
1eb9d 65 20 69 66 28 20 6e 45 78 70 72 3e 30 20 29 7b  e if( nExpr>0 ){
1eb9e 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20  .        pDef = 
1eb9f 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c  sqlite3VtabOverl
1eba0 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20  oadFunction(db, 
1eba1 70 44 65 66 2c 20 6e 45 78 70 72 2c 20 70 4c 69  pDef, nExpr, pLi
1eba2 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b  st->a[0].pExpr);
1eba3 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
1eba4 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
1eba5 3c 6e 45 78 70 72 20 26 26 20 69 3c 33 32 3b 20  <nExpr && i<32; 
1eba6 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  i++){.        if
1eba7 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43  ( sqlite3ExprIsC
1eba8 6f 6e 73 74 61 6e 74 28 70 4c 69 73 74 2d 3e 61  onstant(pList->a
1eba9 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20  [i].pExpr) ){.  
1ebaa 20 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 73          constMas
1ebab 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 20  k |= (1<<i);.   
1ebac 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1ebad 66 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20  f( (pDef->flags 
1ebae 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  & SQLITE_FUNC_NE
1ebaf 45 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21 70  EDCOLL)!=0 && !p
1ebb0 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Coll ){.        
1ebb1 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
1ebb2 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
1ebb3 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  rse, pList->a[i]
1ebb4 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20  .pExpr);.       
1ebb5 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1ebb6 20 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73   if( pDef->flags
1ebb7 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e   & SQLITE_FUNC_N
1ebb8 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20  EEDCOLL ){.     
1ebb9 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 20     if( !pColl ) 
1ebba 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  pColl = db->pDfl
1ebbb 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 20  tColl; .        
1ebbc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ebbd 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c  4(v, OP_CollSeq,
1ebbe 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20   0, 0, 0, (char 
1ebbf 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c  *)pColl, P4_COLL
1ebc0 53 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  SEQ);.      }.  
1ebc1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ebc2 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63  ddOp4(v, OP_Func
1ebc3 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c  tion, constMask,
1ebc4 20 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20 20   r1, target,.   
1ebc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ebc6 20 20 20 20 20 28 63 68 61 72 2a 29 70 44 65 66       (char*)pDef
1ebc7 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20  , P4_FUNCDEF);. 
1ebc8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ebc9 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29  ChangeP5(v, (u8)
1ebca 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66  nExpr);.      if
1ebcb 28 20 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20  ( nExpr ){.     
1ebcc 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
1ebcd 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
1ebce 65 2c 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a 20  e, r1, nExpr);. 
1ebcf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
1ebd0 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
1ebd1 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
1ebd2 73 65 2c 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a  se, r1, nExpr);.
1ebd3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1ebd4 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1ebd5 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
1ebd6 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53      case TK_EXIS
1ebd7 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  TS:.    case TK_
1ebd8 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20  SELECT: {.      
1ebd9 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1ebda 5f 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20  _EXISTS );.     
1ebdb 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1ebdc 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20  K_SELECT );.    
1ebdd 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f    if( pExpr->iCo
1ebde 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  lumn==0 ){.     
1ebdf 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75     sqlite3CodeSu
1ebe0 62 73 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  bselect(pParse, 
1ebe1 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
1ebe2 20 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65      }.      inRe
1ebe3 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  g = pExpr->iColu
1ebe4 6d 6e 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  mn;.      break;
1ebe5 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1ebe6 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69  TK_IN: {.      i
1ebe7 6e 74 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 30  nt rNotFound = 0
1ebe8 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 4d 61 79  ;.      int rMay
1ebe9 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20  HaveNull = 0;.  
1ebea 20 20 20 20 69 6e 74 20 6a 32 2c 20 6a 33 2c 20      int j2, j3, 
1ebeb 6a 34 2c 20 6a 35 3b 0a 20 20 20 20 20 20 63 68  j4, j5;.      ch
1ebec 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  ar affinity;.   
1ebed 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 0a 20     int eType;.. 
1ebee 20 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d       VdbeNoopCom
1ebef 6d 65 6e 74 28 28 76 2c 20 22 62 65 67 69 6e 20  ment((v, "begin 
1ebf0 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20 74 61  IN expr r%d", ta
1ebf1 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20 65 54  rget));.      eT
1ebf2 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  ype = sqlite3Fin
1ebf3 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c  dInIndex(pParse,
1ebf4 20 70 45 78 70 72 2c 20 26 72 4d 61 79 48 61 76   pExpr, &rMayHav
1ebf5 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66  eNull);.      if
1ebf6 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29  ( rMayHaveNull )
1ebf7 7b 0a 20 20 20 20 20 20 20 20 72 4e 6f 74 46 6f  {.        rNotFo
1ebf8 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  und = ++pParse->
1ebf9 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  nMem;.      }.. 
1ebfa 20 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f       /* Figure o
1ebfb 75 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  ut the affinity 
1ebfc 74 6f 20 75 73 65 20 74 6f 20 63 72 65 61 74 65  to use to create
1ebfd 20 61 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20   a key from the 
1ebfe 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a  results.      **
1ebff 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
1ec00 6f 6e 2e 20 61 66 66 69 6e 69 74 79 53 74 72 20  on. affinityStr 
1ec01 73 74 6f 72 65 73 20 61 20 73 74 61 74 69 63 20  stores a static 
1ec02 73 74 72 69 6e 67 20 73 75 69 74 61 62 6c 65 20  string suitable 
1ec03 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 50 34 20  for.      ** P4 
1ec04 6f 66 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  of OP_MakeRecord
1ec05 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1ec06 20 61 66 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70   affinity = comp
1ec07 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70  arisonAffinity(p
1ec08 45 78 70 72 29 3b 0a 0a 0a 20 20 20 20 20 20 2f  Expr);...      /
1ec09 2a 20 43 6f 64 65 20 74 68 65 20 3c 65 78 70 72  * Code the <expr
1ec0a 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49  > from "<expr> I
1ec0b 4e 20 28 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65  N (...)". The te
1ec0c 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 20 20  mporary table.  
1ec0d 20 20 20 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54      ** pExpr->iT
1ec0e 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  able contains th
1ec0f 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d 61  e values that ma
1ec10 6b 65 20 75 70 20 74 68 65 20 28 2e 2e 2e 29 20  ke up the (...) 
1ec11 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  set..      */.  
1ec12 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61      pParse->disa
1ec13 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20  bleColCache++;. 
1ec14 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1ec15 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78  Code(pParse, pEx
1ec16 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65  pr->pLeft, targe
1ec17 74 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  t);.      pParse
1ec18 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
1ec19 65 2d 2d 3b 0a 20 20 20 20 20 20 6a 32 20 3d 20  e--;.      j2 = 
1ec1a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ec1b 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  1(v, OP_IsNull, 
1ec1c 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 69  target);.      i
1ec1d 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44  f( eType==IN_IND
1ec1e 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20  EX_ROWID ){.    
1ec1f 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33      j3 = sqlite3
1ec20 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
1ec21 5f 4d 75 73 74 42 65 49 6e 74 2c 20 74 61 72 67  _MustBeInt, targ
1ec22 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 34 20  et);.        j4 
1ec23 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1ec24 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
1ec25 73 74 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62  sts, pExpr->iTab
1ec26 6c 65 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a  le, 0, target);.
1ec27 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1ec28 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1ec29 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67  Integer, 1, targ
1ec2a 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 35 20  et);.        j5 
1ec2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1ec2c 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b  Op0(v, OP_Goto);
1ec2d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ec2e 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
1ec2f 6a 33 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  j3);.        sql
1ec30 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1ec31 28 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20 20  (v, j4);.       
1ec32 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ec33 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
1ec34 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 0, target);.  
1ec35 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ec36 20 20 20 72 32 20 3d 20 72 65 67 46 72 65 65 32     r2 = regFree2
1ec37 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
1ec38 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 0a 20  pReg(pParse);.. 
1ec39 20 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65         /* Create
1ec3a 20 61 20 72 65 63 6f 72 64 20 61 6e 64 20 74 65   a record and te
1ec3b 73 74 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65  st for set membe
1ec3c 72 73 68 69 70 2e 20 49 66 20 74 68 65 20 73 65  rship. If the se
1ec3d 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20  t contains.     
1ec3e 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 2c     ** the value,
1ec3f 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68   then jump to th
1ec40 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74 65 73  e end of the tes
1ec41 74 20 63 6f 64 65 2e 20 54 68 65 20 74 61 72 67  t code. The targ
1ec42 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65  et.        ** re
1ec43 67 69 73 74 65 72 20 73 74 69 6c 6c 20 63 6f 6e  gister still con
1ec44 74 61 69 6e 73 20 74 68 65 20 74 72 75 65 20 28  tains the true (
1ec45 31 29 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e  1) value written
1ec46 20 74 6f 20 69 74 20 65 61 72 6c 69 65 72 2e 0a   to it earlier..
1ec47 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1ec48 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ec49 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp4(v, OP_MakeR
1ec4a 65 63 6f 72 64 2c 20 74 61 72 67 65 74 2c 20 31  ecord, target, 1
1ec4b 2c 20 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c  , r2, &affinity,
1ec4c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
1ec4d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1ec4e 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
1ec4f 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
1ec50 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64    j5 = sqlite3Vd
1ec51 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46  beAddOp3(v, OP_F
1ec52 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61  ound, pExpr->iTa
1ec53 62 6c 65 2c 20 30 2c 20 72 32 29 3b 0a 0a 20 20  ble, 0, r2);..  
1ec54 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
1ec55 73 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74  set membership t
1ec56 65 73 74 20 66 61 69 6c 73 2c 20 74 68 65 6e 20  est fails, then 
1ec57 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
1ec58 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 78  e .        ** "x
1ec59 20 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65   IN (...)" expre
1ec5a 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69  ssion must be ei
1ec5b 74 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20  ther 0 or NULL. 
1ec5c 49 66 20 74 68 65 20 73 65 74 0a 20 20 20 20 20  If the set.     
1ec5d 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e     ** contains n
1ec5e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74  o NULL values, t
1ec5f 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69  hen the result i
1ec60 73 20 30 2e 20 49 66 20 74 68 65 20 73 65 74 20  s 0. If the set 
1ec61 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74  .        ** cont
1ec62 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ains one or more
1ec63 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68   NULL values, th
1ec64 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  en the result of
1ec65 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
1ec66 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6c  expression is al
1ec67 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20  so NULL..       
1ec68 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
1ec69 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a  rNotFound==0 ){.
1ec6a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
1ec6b 73 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69 66  s branch runs if
1ec6c 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20   it is known at 
1ec6d 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 28 6e 6f  compile time (no
1ec6e 77 29 20 74 68 61 74 20 0a 20 20 20 20 20 20 20  w) that .       
1ec6f 20 20 20 2a 2a 20 74 68 65 20 73 65 74 20 63 6f     ** the set co
1ec70 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76  ntains no NULL v
1ec71 61 6c 75 65 73 2e 20 54 68 69 73 20 68 61 70 70  alues. This happ
1ec72 65 6e 73 20 61 73 20 74 68 65 20 72 65 73 75 6c  ens as the resul
1ec73 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f  t.          ** o
1ec74 66 20 61 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63  f a "NOT NULL" c
1ec75 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 68 65  onstraint in the
1ec76 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1ec77 2e 20 4e 6f 20 6e 65 65 64 0a 20 20 20 20 20 20  . No need.      
1ec78 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74      ** to test t
1ec79 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72  he data structur
1ec7a 65 20 61 74 20 72 75 6e 74 69 6d 65 20 69 6e 20  e at runtime in 
1ec7b 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20  this case..     
1ec7c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1ec7d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ec7e 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
1ec7f 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20  r, 0, target);. 
1ec80 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1ec81 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
1ec82 62 6c 6f 63 6b 20 70 6f 70 75 6c 61 74 65 73 20  block populates 
1ec83 74 68 65 20 72 4e 6f 74 46 6f 75 6e 64 20 72 65  the rNotFound re
1ec84 67 69 73 74 65 72 20 77 69 74 68 20 65 69 74 68  gister with eith
1ec85 65 72 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20  er NULL.        
1ec86 20 20 2a 2a 20 6f 72 20 30 20 28 61 6e 20 69 6e    ** or 0 (an in
1ec87 74 65 67 65 72 20 76 61 6c 75 65 29 2e 20 49 66  teger value). If
1ec88 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74   the data struct
1ec89 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65  ure contains one
1ec8a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72  .          ** or
1ec8b 20 6d 6f 72 65 20 4e 55 4c 4c 73 2c 20 74 68 65   more NULLs, the
1ec8c 6e 20 73 65 74 20 72 4e 6f 74 46 6f 75 6e 64 20  n set rNotFound 
1ec8d 74 6f 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69  to NULL. Otherwi
1ec8e 73 65 2c 20 73 65 74 20 69 74 0a 20 20 20 20 20  se, set it.     
1ec8f 20 20 20 20 20 2a 2a 20 74 6f 20 30 2e 20 49 66       ** to 0. If
1ec90 20 72 65 67 69 73 74 65 72 20 72 4d 61 79 48 61   register rMayHa
1ec91 76 65 4e 75 6c 6c 20 69 73 20 61 6c 72 65 61 64  veNull is alread
1ec92 79 20 73 65 74 20 74 6f 20 73 6f 6d 65 20 76 61  y set to some va
1ec93 6c 75 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  lue.          **
1ec94 20 6f 74 68 65 72 20 74 68 61 6e 20 4e 55 4c 4c   other than NULL
1ec95 2c 20 74 68 65 6e 20 74 68 65 20 74 65 73 74 20  , then the test 
1ec96 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
1ec97 20 72 75 6e 20 61 6e 64 20 0a 20 20 20 20 20 20   run and .      
1ec98 20 20 20 20 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64      ** rNotFound
1ec99 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 70 75   is already popu
1ec9a 6c 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 20  lated..         
1ec9b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74   */.          st
1ec9c 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
1ec9d 6e 75 6c 6c 52 65 63 6f 72 64 5b 5d 20 3d 20 7b  nullRecord[] = {
1ec9e 20 30 78 30 32 2c 20 30 78 30 30 20 7d 3b 0a 20   0x02, 0x00 };. 
1ec9f 20 20 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71           j3 = sq
1eca0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
1eca1 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72  v, OP_NotNull, r
1eca2 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20  MayHaveNull);.  
1eca3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1eca4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1eca5 4e 75 6c 6c 2c 20 30 2c 20 72 4e 6f 74 46 6f 75  Null, 0, rNotFou
1eca6 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  nd);.          s
1eca7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1eca8 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20  (v, OP_Blob, 2, 
1eca9 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 30 2c  rMayHaveNull, 0,
1ecaa 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1ecab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e                 n
1ecac 75 6c 6c 52 65 63 6f 72 64 2c 20 50 34 5f 53 54  ullRecord, P4_ST
1ecad 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20  ATIC);.         
1ecae 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j4 = sqlite3Vdb
1ecaf 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f  eAddOp3(v, OP_Fo
1ecb0 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62  und, pExpr->iTab
1ecb1 6c 65 2c 20 30 2c 20 72 4d 61 79 48 61 76 65 4e  le, 0, rMayHaveN
1ecb2 75 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ull);.          
1ecb3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ecb4 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
1ecb5 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a   0, rNotFound);.
1ecb6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1ecb7 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1ecb8 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20   j4);.          
1ecb9 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1ecba 65 72 65 28 76 2c 20 6a 33 29 3b 0a 0a 20 20 20  ere(v, j3);..   
1ecbb 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74         /* Copy t
1ecbc 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69  he value of regi
1ecbd 73 74 65 72 20 72 4e 6f 74 46 6f 75 6e 64 20 28  ster rNotFound (
1ecbe 77 68 69 63 68 20 69 73 20 65 69 74 68 65 72 20  which is either 
1ecbf 4e 55 4c 4c 20 6f 72 20 30 29 0a 20 20 20 20 20  NULL or 0).     
1ecc0 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65       ** into the
1ecc1 20 74 61 72 67 65 74 20 72 65 67 69 73 74 65 72   target register
1ecc2 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74  . This will be t
1ecc3 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1ecc4 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78  .          ** ex
1ecc5 70 72 65 73 73 69 6f 6e 2e 0a 20 20 20 20 20 20  pression..      
1ecc6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
1ecc7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ecc8 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72  p2(v, OP_Copy, r
1ecc9 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 72 67 65 74  NotFound, target
1ecca 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1eccb 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1eccc 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1eccd 2c 20 6a 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  , j2);.      sql
1ecce 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1eccf 28 76 2c 20 6a 35 29 3b 0a 20 20 20 20 20 20 56  (v, j5);.      V
1ecd0 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
1ecd1 65 6e 64 20 49 4e 20 65 78 70 72 20 72 25 64 22  end IN expr r%d"
1ecd2 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20 20 20  , target));.    
1ecd3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1ecd4 65 6e 64 69 66 0a 20 20 20 20 2f 2a 0a 20 20 20  endif.    /*.   
1ecd5 20 2a 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e   **    x BETWEEN
1ecd6 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a   y AND z.    **.
1ecd7 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65      ** This is e
1ecd8 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20  quivalent to.   
1ecd9 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e   **.    **    x>
1ecda 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20  =y AND x<=z.    
1ecdb 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73  **.    ** X is s
1ecdc 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e  tored in pExpr->
1ecdd 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20  pLeft..    ** Y 
1ecde 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78  is stored in pEx
1ecdf 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e  pr->pList->a[0].
1ece0 70 45 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20  pExpr..    ** Z 
1ece1 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78  is stored in pEx
1ece2 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  pr->pList->a[1].
1ece3 70 45 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  pExpr..    */.  
1ece4 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45    case TK_BETWEE
1ece5 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20  N: {.      Expr 
1ece6 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e  *pLeft = pExpr->
1ece7 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72  pLeft;.      str
1ece8 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1ece9 6d 20 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70  m *pLItem = pExp
1ecea 72 2d 3e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20  r->pList->a;.   
1eceb 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20     Expr *pRight 
1ecec 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b  = pLItem->pExpr;
1eced 0a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ..      codeComp
1ecee 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72  areOperands(pPar
1ecef 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20  se, pLeft, &r1, 
1ecf0 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20  &regFree1,.     
1ecf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ecf2 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69               pRi
1ecf3 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72  ght, &r2, &regFr
1ecf4 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ee2);.      test
1ecf5 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
1ecf6 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
1ecf7 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
1ecf8 20 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73   );.      r3 = s
1ecf9 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
1ecfa 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
1ecfb 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r4 = sqlite3GetT
1ecfc 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
1ecfd 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
1ecfe 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c  e(pParse, pLeft,
1ecff 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a   pRight, OP_Ge,.
1ed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ed01 20 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51    r1, r2, r3, SQ
1ed02 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20  LITE_STOREP2);. 
1ed03 20 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20       pLItem++;. 
1ed04 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c       pRight = pL
1ed05 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Item->pExpr;.   
1ed06 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
1ed07 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
1ed08 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20   regFree2);.    
1ed09 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78    r2 = sqlite3Ex
1ed0a 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
1ed0b 65 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67 46  e, pRight, &regF
1ed0c 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73  ree2);.      tes
1ed0d 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
1ed0e 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65  =0 );.      code
1ed0f 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
1ed10 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f  pLeft, pRight, O
1ed11 50 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34  P_Le, r1, r2, r4
1ed12 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  , SQLITE_STOREP2
1ed13 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1ed14 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1ed15 5f 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61  _And, r3, r4, ta
1ed16 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  rget);.      sql
1ed17 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
1ed18 65 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a  eg(pParse, r3);.
1ed19 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
1ed1a 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
1ed1b 73 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62  se, r4);.      b
1ed1c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1ed1d 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b  case TK_UPLUS: {
1ed1e 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73  .      inReg = s
1ed1f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
1ed20 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
1ed21 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65  pr->pLeft, targe
1ed22 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
1ed23 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
1ed24 20 20 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20     ** Form A:.  
1ed25 20 20 2a 2a 20 20 20 43 41 53 45 20 78 20 57 48    **   CASE x WH
1ed26 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48  EN e1 THEN r1 WH
1ed27 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e  EN e2 THEN r2 ..
1ed28 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72  . WHEN eN THEN r
1ed29 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20  N ELSE y END.   
1ed2a 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20   **.    ** Form 
1ed2b 42 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45  B:.    **   CASE
1ed2c 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31   WHEN e1 THEN r1
1ed2d 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32   WHEN e2 THEN r2
1ed2e 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45   ... WHEN eN THE
1ed2f 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a  N rN ELSE y END.
1ed30 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f      **.    ** Fo
1ed31 72 6d 20 41 20 69 73 20 63 61 6e 20 62 65 20 74  rm A is can be t
1ed32 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20  ransformed into 
1ed33 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66  the equivalent f
1ed34 6f 72 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73  orm B as follows
1ed35 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20  :.    **   CASE 
1ed36 57 48 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72  WHEN x=e1 THEN r
1ed37 31 20 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e  1 WHEN x=e2 THEN
1ed38 20 72 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20   r2 ....    **  
1ed39 20 20 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20        WHEN x=eN 
1ed3a 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45  THEN rN ELSE y E
1ed3b 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ND.    **.    **
1ed3c 20 58 20 28 69 66 20 69 74 20 65 78 69 73 74 73   X (if it exists
1ed3d 29 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70  ) is in pExpr->p
1ed3e 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69  Left..    ** Y i
1ed3f 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 52 69 67  s in pExpr->pRig
1ed40 68 74 2e 20 20 54 68 65 20 59 20 69 73 20 61 6c  ht.  The Y is al
1ed41 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66  so optional.  If
1ed42 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20   there is no.   
1ed43 20 2a 2a 20 45 4c 53 45 20 63 6c 61 75 73 65 20   ** ELSE clause 
1ed44 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 74 65 72  and no other ter
1ed45 6d 20 6d 61 74 63 68 65 73 2c 20 74 68 65 6e 20  m matches, then 
1ed46 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
1ed47 65 0a 20 20 20 20 2a 2a 20 65 78 70 72 73 73 69  e.    ** exprssi
1ed48 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20  on is NULL..    
1ed49 2a 2a 20 45 69 20 69 73 20 69 6e 20 70 45 78 70  ** Ei is in pExp
1ed4a 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d  r->pList->a[i*2]
1ed4b 20 61 6e 64 20 52 69 20 69 73 20 70 45 78 70 72   and Ri is pExpr
1ed4c 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31  ->pList->a[i*2+1
1ed4d 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ]..    **.    **
1ed4e 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   The result of t
1ed4f 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1ed50 20 74 68 65 20 52 69 20 66 6f 72 20 74 68 65 20   the Ri for the 
1ed51 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 45  first matching E
1ed52 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20  i,.    ** or if 
1ed53 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63  there is no matc
1ed54 68 69 6e 67 20 45 69 2c 20 74 68 65 20 45 4c 53  hing Ei, the ELS
1ed55 45 20 74 65 72 6d 20 59 2c 20 6f 72 20 69 66 20  E term Y, or if 
1ed56 74 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20  there is.    ** 
1ed57 6e 6f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55  no ELSE term, NU
1ed58 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  LL..    */.    c
1ed59 61 73 65 20 54 4b 5f 43 41 53 45 3a 20 7b 0a 20  ase TK_CASE: {. 
1ed5a 20 20 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 65       int endLabe
1ed5b 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
1ed5c 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c         /* GOTO l
1ed5d 61 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 20  abel for end of 
1ed5e 43 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20 20  CASE stmt */.   
1ed5f 20 20 20 69 6e 74 20 6e 65 78 74 43 61 73 65 3b     int nextCase;
1ed60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ed61 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62       /* GOTO lab
1ed62 65 6c 20 66 6f 72 20 6e 65 78 74 20 57 48 45 4e  el for next WHEN
1ed63 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20   clause */.     
1ed64 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20   int nExpr;     
1ed65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ed66 20 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72 20     /* 2x number 
1ed67 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f  of WHEN terms */
1ed68 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20  .      int i;   
1ed69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ed6a 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1ed6b 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
1ed6c 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
1ed6d 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
1ed6e 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 57      /* List of W
1ed6f 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20  HEN terms */.   
1ed70 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
1ed71 73 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65 6c  st_item *aListel
1ed72 65 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66  em;  /* Array of
1ed73 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20   WHEN terms */. 
1ed74 20 20 20 20 20 45 78 70 72 20 6f 70 43 6f 6d 70       Expr opComp
1ed75 61 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  are;            
1ed76 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 3d         /* The X=
1ed77 3d 45 69 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  =Ei expression *
1ed78 2f 0a 20 20 20 20 20 20 45 78 70 72 20 63 61 63  /.      Expr cac
1ed79 68 65 58 3b 20 20 20 20 20 20 20 20 20 20 20 20  heX;            
1ed7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63            /* Cac
1ed7b 68 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 58  hed expression X
1ed7c 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
1ed7d 70 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pX;             
1ed7e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1ed7f 68 65 20 58 20 65 78 70 72 65 73 73 69 6f 6e 20  he X expression 
1ed80 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  */.      Expr *p
1ed81 54 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20  Test = 0;       
1ed82 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 3d             /* X=
1ed83 3d 45 69 20 28 66 6f 72 6d 20 41 29 20 6f 72 20  =Ei (form A) or 
1ed84 6a 75 73 74 20 45 69 20 28 66 6f 72 6d 20 42 29  just Ei (form B)
1ed85 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72   */..      asser
1ed86 74 28 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 3b  t(pExpr->pList);
1ed87 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 28 70  .      assert((p
1ed88 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78  Expr->pList->nEx
1ed89 70 72 20 25 20 32 29 20 3d 3d 20 30 29 3b 0a 20  pr % 2) == 0);. 
1ed8a 20 20 20 20 20 61 73 73 65 72 74 28 70 45 78 70       assert(pExp
1ed8b 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  r->pList->nExpr 
1ed8c 3e 20 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69  > 0);.      pELi
1ed8d 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73  st = pExpr->pLis
1ed8e 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74 65 6c  t;.      aListel
1ed8f 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a  em = pEList->a;.
1ed90 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70 45        nExpr = pE
1ed91 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  List->nExpr;.   
1ed92 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71     endLabel = sq
1ed93 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1ed94 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28  el(v);.      if(
1ed95 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c   (pX = pExpr->pL
1ed96 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  eft)!=0 ){.     
1ed97 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70 58 3b     cacheX = *pX;
1ed98 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
1ed99 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  e( pX->op==TK_CO
1ed9a 4c 55 4d 4e 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d  LUMN || pX->op==
1ed9b 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20  TK_REGISTER );. 
1ed9c 20 20 20 20 20 20 20 63 61 63 68 65 58 2e 69 54         cacheX.iT
1ed9d 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78  able = sqlite3Ex
1ed9e 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
1ed9f 65 2c 20 70 58 2c 20 26 72 65 67 46 72 65 65 31  e, pX, &regFree1
1eda0 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
1eda1 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
1eda2 20 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68   );.        cach
1eda3 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53  eX.op = TK_REGIS
1eda4 54 45 52 3b 0a 20 20 20 20 20 20 20 20 6f 70 43  TER;.        opC
1eda5 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45  ompare.op = TK_E
1eda6 51 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d  Q;.        opCom
1eda7 70 61 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61  pare.pLeft = &ca
1eda8 63 68 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54  cheX;.        pT
1eda9 65 73 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65  est = &opCompare
1edaa 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1edab 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1edac 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20  olCache++;.     
1edad 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70   for(i=0; i<nExp
1edae 72 3b 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20  r; i=i+2){.     
1edaf 20 20 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20     if( pX ){.   
1edb0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1edb1 54 65 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 20  Test!=0 );.     
1edb2 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70       opCompare.p
1edb3 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65  Right = aListele
1edb4 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  m[i].pExpr;.    
1edb5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1edb6 20 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69       pTest = aLi
1edb7 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b  stelem[i].pExpr;
1edb8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1edb9 20 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71     nextCase = sq
1edba 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1edbb 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74  el(v);.        t
1edbc 65 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e  estcase( pTest->
1edbd 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op==TK_COLUMN ||
1edbe 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 52   pTest->op==TK_R
1edbf 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20  EGISTER );.     
1edc0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
1edc1 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54  False(pParse, pT
1edc2 65 73 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53  est, nextCase, S
1edc3 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1edc4 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
1edc5 61 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69  ase( aListelem[i
1edc6 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  +1].pExpr->op==T
1edc7 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20  K_COLUMN );.    
1edc8 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c      testcase( aL
1edc9 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78  istelem[i+1].pEx
1edca 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  pr->op==TK_REGIS
1edcb 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73  TER );.        s
1edcc 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1edcd 50 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d  Parse, aListelem
1edce 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72  [i+1].pExpr, tar
1edcf 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  get);.        sq
1edd0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1edd1 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65  v, OP_Goto, 0, e
1edd2 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20  ndLabel);.      
1edd3 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
1edd4 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78  olveLabel(v, nex
1edd5 74 43 61 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a  tCase);.      }.
1edd6 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
1edd7 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  >pRight ){.     
1edd8 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
1edd9 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
1edda 2d 3e 70 52 69 67 68 74 2c 20 74 61 72 67 65 74  ->pRight, target
1eddb 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1eddc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1eddd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1edde 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29  Null, 0, target)
1eddf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1ede0 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
1ede1 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61  veLabel(v, endLa
1ede2 62 65 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65  bel);.      asse
1ede3 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61  rt( pParse->disa
1ede4 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b  bleColCache>0 );
1ede5 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64  .      pParse->d
1ede6 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d  isableColCache--
1ede7 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1ede8 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
1ede9 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1edea 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 41 49  .    case TK_RAI
1edeb 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  SE: {.      if( 
1edec 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  !pParse->trigSta
1eded 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ck ){.        sq
1edee 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1edef 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
1edf0 20 20 20 20 20 20 20 20 20 20 20 20 20 22 52 41               "RA
1edf1 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79 20 62  ISE() may only b
1edf2 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20  e used within a 
1edf3 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 22  trigger-program"
1edf4 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1edf5 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
1edf6 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43     if( pExpr->iC
1edf7 6f 6c 75 6d 6e 21 3d 4f 45 5f 49 67 6e 6f 72 65  olumn!=OE_Ignore
1edf8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 61 73 73   ){.         ass
1edf9 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c  ert( pExpr->iCol
1edfa 75 6d 6e 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b  umn==OE_Rollback
1edfb 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
1edfc 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
1edfd 75 6d 6e 20 3d 3d 20 4f 45 5f 41 62 6f 72 74 20  umn == OE_Abort 
1edfe 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ||.             
1edff 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
1ee00 6d 6e 20 3d 3d 20 4f 45 5f 46 61 69 6c 20 29 3b  mn == OE_Fail );
1ee01 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
1ee02 33 44 65 71 75 6f 74 65 45 78 70 72 28 64 62 2c  3DequoteExpr(db,
1ee03 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20   pExpr);.       
1ee04 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ee05 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op4(v, OP_Halt, 
1ee06 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
1ee07 54 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  T, pExpr->iColum
1ee08 6e 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  n, 0,.          
1ee09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
1ee0a 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
1ee0b 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65  n.z, pExpr->toke
1ee0c 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c  n.n);.      } el
1ee0d 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 61 73  se {.         as
1ee0e 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f  sert( pExpr->iCo
1ee0f 6c 75 6d 6e 20 3d 3d 20 4f 45 5f 49 67 6e 6f 72  lumn == OE_Ignor
1ee10 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71  e );.         sq
1ee11 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1ee12 76 2c 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70  v, OP_ContextPop
1ee13 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
1ee14 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ee15 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
1ee16 30 2c 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53  0, pParse->trigS
1ee17 74 61 63 6b 2d 3e 69 67 6e 6f 72 65 4a 75 6d 70  tack->ignoreJump
1ee18 29 3b 0a 20 20 20 20 20 20 20 20 20 56 64 62 65  );.         Vdbe
1ee19 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 61 69  Comment((v, "rai
1ee1a 73 65 28 49 47 4e 4f 52 45 29 22 29 29 3b 0a 20  se(IGNORE)"));. 
1ee1b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
1ee1c 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
1ee1d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65  .  }.  sqlite3Re
1ee1e 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
1ee1f 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a  rse, regFree1);.
1ee20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
1ee21 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
1ee22 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 65 74  regFree2);.  ret
1ee23 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a  urn inReg;.}../*
1ee24 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1ee25 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6e  e to evaluate an
1ee26 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20   expression and 
1ee27 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
1ee28 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 69  s.** into a regi
1ee29 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68  ster.  Return th
1ee2a 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  e register numbe
1ee2b 72 20 77 68 65 72 65 20 74 68 65 20 72 65 73 75  r where the resu
1ee2c 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65  lts.** are store
1ee2d 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1ee2e 72 65 67 69 73 74 65 72 20 69 73 20 61 20 74 65  register is a te
1ee2f 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72  mporary register
1ee30 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 65 61   that can be dea
1ee31 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68 65  llocated,.** the
1ee32 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 6d 62  n write its numb
1ee33 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20 20  er into *pReg.  
1ee34 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 72 65  If the result re
1ee35 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a 2a  gister is not.**
1ee36 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 74 68   a temporary, th
1ee37 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 6f 20  en set *pReg to 
1ee38 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  zero..*/.SQLITE_
1ee39 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1ee3a 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
1ee3b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
1ee3c 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
1ee3d 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 72 31  *pReg){.  int r1
1ee3e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
1ee3f 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
1ee40 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33  int r2 = sqlite3
1ee41 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
1ee42 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72 31  Parse, pExpr, r1
1ee43 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 31 20  );.  if( r2==r1 
1ee44 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 72  ){.    *pReg = r
1ee45 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
1ee46 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
1ee47 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
1ee48 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 30  );.    *pReg = 0
1ee49 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1ee4a 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  2;.}../*.** Gene
1ee4b 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
1ee4c 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 78 70  ill evaluate exp
1ee4d 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61 6e  ression pExpr an
1ee4e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72  d store the.** r
1ee4f 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74  esults in regist
1ee50 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 65 20  er target.  The 
1ee51 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 61 72  results are guar
1ee52 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 61 72  anteed to appear
1ee53 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20  .** in register 
1ee54 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54  target..*/.SQLIT
1ee55 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1ee56 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 50 61  lite3ExprCode(Pa
1ee57 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
1ee58 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61  r *pExpr, int ta
1ee59 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e 52  rget){.  int inR
1ee5a 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 74  eg;..  assert( t
1ee5b 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 65  arget>0 && targe
1ee5c 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  t<=pParse->nMem 
1ee5d 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c  );.  inReg = sql
1ee5e 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67  ite3ExprCodeTarg
1ee5f 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  et(pParse, pExpr
1ee60 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73  , target);.  ass
1ee61 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56 64  ert( pParse->pVd
1ee62 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62  be || pParse->db
1ee63 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1ee64 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74  ;.  if( inReg!=t
1ee65 61 72 67 65 74 20 26 26 20 70 50 61 72 73 65 2d  arget && pParse-
1ee66 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73 71  >pVdbe ){.    sq
1ee67 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1ee68 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f  pParse->pVdbe, O
1ee69 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20  P_SCopy, inReg, 
1ee6a 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20 72  target);.  }.  r
1ee6b 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d 0a  eturn target;.}.
1ee6c 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1ee6d 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 74  code that evalut
1ee6e 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  es the given exp
1ee6f 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 74 73  ression and puts
1ee70 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69   the result.** i
1ee71 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65  n register targe
1ee72 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61  t..**.** Also ma
1ee73 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ke a copy of the
1ee74 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 73 75   expression resu
1ee75 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65 72  lts into another
1ee76 20 22 63 61 63 68 65 22 20 72 65 67 69 73 74 65   "cache" registe
1ee77 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79 20  r.** and modify 
1ee78 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73  the expression s
1ee79 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20  o that the next 
1ee7a 74 69 6d 65 20 69 74 20 69 73 20 65 76 61 6c 75  time it is evalu
1ee7b 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65 73  ated,.** the res
1ee7c 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ult is a copy of
1ee7d 20 74 68 65 20 63 61 63 68 65 20 72 65 67 69 73   the cache regis
1ee7e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ter..**.** This 
1ee7f 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
1ee80 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20  for expressions 
1ee81 74 68 61 74 20 61 72 65 20 75 73 65 64 20 6d 75  that are used mu
1ee82 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 73  ltiple .** times
1ee83 2e 20 20 54 68 65 79 20 61 72 65 20 65 76 61 6c  .  They are eval
1ee84 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64 20 74  uated once and t
1ee85 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  he results of th
1ee86 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  e expression.** 
1ee87 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a 53  are reused..*/.S
1ee88 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1ee89 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
1ee8a 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65 20  eAndCache(Parse 
1ee8b 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
1ee8c 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74  Expr, int target
1ee8d 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
1ee8e 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
1ee8f 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e 52  int inReg;.  inR
1ee90 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  eg = sqlite3Expr
1ee91 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78  Code(pParse, pEx
1ee92 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61  pr, target);.  a
1ee93 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20  ssert( target>0 
1ee94 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  );.  if( pExpr->
1ee95 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52 20  op!=TK_REGISTER 
1ee96 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69 4d 65  ){  .    int iMe
1ee97 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20 2b 2b  m;.    iMem = ++
1ee98 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
1ee99 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ee9a 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20  Op2(v, OP_Copy, 
1ee9b 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a 20 20  inReg, iMem);.  
1ee9c 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1ee9d 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45 78 70  = iMem;.    pExp
1ee9e 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53  r->op = TK_REGIS
1ee9f 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  TER;.  }.  retur
1eea0 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a  n inReg;.}../*.*
1eea1 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
1eea2 20 70 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e   pExpr is an con
1eea3 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e  stant expression
1eea4 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72   that is appropr
1eea5 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 74  iate.** for fact
1eea6 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c  oring out of a l
1eea7 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74  oop.  Appropriat
1eea8 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72  e expressions ar
1eea9 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41  e:.**.**    *  A
1eeaa 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  ny expression th
1eeab 61 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20  at evaluates to 
1eeac 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 6f  two or more opco
1eead 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  des..**.**    * 
1eeae 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 2c   Any OP_Integer,
1eeaf 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 72   OP_Real, OP_Str
1eeb0 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50  ing, OP_Blob, OP
1eeb1 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20  _Null, .**      
1eeb2 20 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 20   or OP_Variable 
1eeb3 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65  that does not ne
1eeb4 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20  ed to be placed 
1eeb5 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 73  in a .**       s
1eeb6 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65 72  pecific register
1eeb7 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73  ..**.** There is
1eeb8 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 63   no point in fac
1eeb9 74 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 6c  toring out singl
1eeba 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f  e-instruction co
1eebb 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73  nstant.** expres
1eebc 73 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20  sions that need 
1eebd 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20  to be placed in 
1eebe 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67  a particular reg
1eebf 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 63  ister.  .** We c
1eec0 6f 75 6c 64 20 66 61 63 74 6f 72 20 74 68 65 6d  ould factor them
1eec1 20 6f 75 74 2c 20 62 75 74 20 74 68 65 6e 20 77   out, but then w
1eec2 65 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 61  e would end up a
1eec3 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53  dding an.** OP_S
1eec4 43 6f 70 79 20 69 6e 73 74 72 75 63 74 69 6f 6e  Copy instruction
1eec5 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c   to move the val
1eec6 75 65 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 72  ue into the corr
1eec7 65 63 74 20 72 65 67 69 73 74 65 72 0a 2a 2a 20  ect register.** 
1eec8 6c 61 74 65 72 2e 20 20 57 65 20 6d 69 67 68 74  later.  We might
1eec9 20 61 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 73   as well just us
1eeca 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69  e the original i
1eecb 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a 2a  nstruction and.*
1eecc 2a 20 61 76 6f 69 64 20 74 68 65 20 4f 50 5f 53  * avoid the OP_S
1eecd 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Copy..*/.static 
1eece 69 6e 74 20 69 73 41 70 70 72 6f 70 72 69 61 74  int isAppropriat
1eecf 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78  eForFactoring(Ex
1eed0 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 73  pr *p){.  if( !s
1eed1 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
1eed2 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29  tantNotJoin(p) )
1eed3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
1eed4 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e   /* Only constan
1eed5 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72  t expressions ar
1eed6 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  e appropriate fo
1eed7 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a 20  r factoring */. 
1eed8 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61   }.  if( (p->fla
1eed9 67 73 20 26 20 45 50 5f 46 69 78 65 64 44 65 73  gs & EP_FixedDes
1eeda 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  t)==0 ){.    ret
1eedb 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 63  urn 1;  /* Any c
1eedc 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f 75 74 20  onstant without 
1eedd 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74  a fixed destinat
1eede 69 6f 6e 20 69 73 20 61 70 70 72 6f 70 72 69 61  ion is appropria
1eedf 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c  te */.  }.  whil
1eee0 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c  e( p->op==TK_UPL
1eee1 55 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 66  US ) p = p->pLef
1eee2 74 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e  t;.  switch( p->
1eee3 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  op ){.#ifndef SQ
1eee4 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c  LITE_OMIT_BLOB_L
1eee5 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20  ITERAL.    case 
1eee6 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a  TK_BLOB:.#endif.
1eee7 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49      case TK_VARI
1eee8 41 42 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54  ABLE:.    case T
1eee9 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63  K_INTEGER:.    c
1eeea 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20  ase TK_FLOAT:.  
1eeeb 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a    case TK_NULL:.
1eeec 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49      case TK_STRI
1eeed 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  NG: {.      test
1eeee 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
1eeef 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 65  BLOB );.      te
1eef0 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
1eef1 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 20  K_VARIABLE );.  
1eef2 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
1eef3 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20  >op==TK_INTEGER 
1eef4 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1eef5 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f  e( p->op==TK_FLO
1eef6 41 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  AT );.      test
1eef7 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
1eef8 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65  NULL );.      te
1eef9 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
1eefa 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 20  K_STRING );.    
1eefb 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 74    /* Single-inst
1eefc 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74  ruction constant
1eefd 73 20 77 69 74 68 20 61 20 66 69 78 65 64 20 64  s with a fixed d
1eefe 65 73 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a 20  estination are. 
1eeff 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 64       ** better d
1ef00 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 66  one in-line.  If
1ef01 20 77 65 20 66 61 63 74 6f 72 20 74 68 65 6d 2c   we factor them,
1ef02 20 74 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 20   they will just 
1ef03 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 20  end.      ** up 
1ef04 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f 50  generating an OP
1ef05 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 74  _SCopy to move t
1ef06 68 65 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20  he value to the 
1ef07 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20  destination.    
1ef08 20 20 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 2a    ** register. *
1ef09 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  /.      return 0
1ef0a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1ef0b 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20   TK_UMINUS: {.  
1ef0c 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66       if( p->pLef
1ef0d 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20  t->op==TK_FLOAT 
1ef0e 7c 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d  || p->pLeft->op=
1ef0f 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20  =TK_INTEGER ){. 
1ef10 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
1ef11 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ;.       }.     
1ef12 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1ef13 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
1ef14 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1ef15 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
1ef16 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 45 78  .}../*.** If pEx
1ef17 70 72 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74  pr is a constant
1ef18 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
1ef19 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   is appropriate 
1ef1a 66 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69 6e 67  for.** factoring
1ef1b 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2c 20   out of a loop, 
1ef1c 74 68 65 6e 20 65 76 61 6c 75 61 74 65 20 74 68  then evaluate th
1ef1d 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  e expression.** 
1ef1e 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 20  into a register 
1ef1f 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 68 65 20  and convert the 
1ef20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20  expression into 
1ef21 61 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 2a 2a  a TK_REGISTER.**
1ef22 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a   expression..*/.
1ef23 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c 43  static int evalC
1ef24 6f 6e 73 74 45 78 70 72 28 57 61 6c 6b 65 72 20  onstExpr(Walker 
1ef25 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a  *pWalker, Expr *
1ef26 70 45 78 70 72 29 7b 0a 20 20 50 61 72 73 65 20  pExpr){.  Parse 
1ef27 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65  *pParse = pWalke
1ef28 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 77 69  r->pParse;.  swi
1ef29 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
1ef2a 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45  {.    case TK_RE
1ef2b 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20  GISTER: {.      
1ef2c 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1ef2d 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43      case TK_FUNC
1ef2e 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54  TION:.    case T
1ef2f 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a  K_AGG_FUNCTION:.
1ef30 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53      case TK_CONS
1ef31 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20  T_FUNC: {.      
1ef32 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73  /* The arguments
1ef33 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 68   to a function h
1ef34 61 76 65 20 61 20 66 69 78 65 64 20 64 65 73 74  ave a fixed dest
1ef35 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a  ination..      *
1ef36 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 69 73  * Mark them this
1ef37 20 77 61 79 20 74 6f 20 61 76 6f 69 64 20 67 65   way to avoid ge
1ef38 6e 65 72 61 74 65 64 20 75 6e 6e 65 65 64 65 64  nerated unneeded
1ef39 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 20 20   OP_SCopy.      
1ef3a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e  ** instructions.
1ef3b 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20   .      */.     
1ef3c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1ef3d 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b   = pExpr->pList;
1ef3e 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74  .      if( pList
1ef3f 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
1ef40 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  i = pList->nExpr
1ef41 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  ;.        struct
1ef42 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1ef43 70 49 74 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61  pItem = pList->a
1ef44 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20  ;.        for(; 
1ef45 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b  i>0; i--, pItem+
1ef46 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
1ef47 28 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 29  ( pItem->pExpr )
1ef48 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 66   pItem->pExpr->f
1ef49 6c 61 67 73 20 7c 3d 20 45 50 5f 46 69 78 65 64  lags |= EP_Fixed
1ef4a 44 65 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Dest;.        }.
1ef4b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1ef4c 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
1ef4d 20 69 66 28 20 69 73 41 70 70 72 6f 70 72 69 61   if( isAppropria
1ef4e 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 70  teForFactoring(p
1ef4f 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74  Expr) ){.    int
1ef50 20 72 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e   r1 = ++pParse->
1ef51 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e 74 20 72 32  nMem;.    int r2
1ef52 3b 0a 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74  ;.    r2 = sqlit
1ef53 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
1ef54 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
1ef55 72 31 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21  r1);.    if( r1!
1ef56 3d 72 32 20 29 20 73 71 6c 69 74 65 33 52 65 6c  =r2 ) sqlite3Rel
1ef57 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
1ef58 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 70 45 78  se, r1);.    pEx
1ef59 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49  pr->op = TK_REGI
1ef5a 53 54 45 52 3b 0a 20 20 20 20 70 45 78 70 72 2d  STER;.    pExpr-
1ef5b 3e 69 54 61 62 6c 65 20 3d 20 72 32 3b 0a 20 20  >iTable = r2;.  
1ef5c 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
1ef5d 6e 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ne;.  }.  return
1ef5e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
1ef5f 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 65 76 61 6c 75  ../*.** Preevalu
1ef60 61 74 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62  ate constant sub
1ef61 65 78 70 72 65 73 73 69 6f 6e 73 20 77 69 74 68  expressions with
1ef62 69 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f  in pExpr and sto
1ef63 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74  re the.** result
1ef64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 2e 20  s in registers. 
1ef65 20 4d 6f 64 69 66 79 20 70 45 78 70 72 20 73 6f   Modify pExpr so
1ef66 20 74 68 61 74 20 74 68 65 20 63 6f 6e 73 74 61   that the consta
1ef67 6e 74 20 73 75 62 65 78 70 72 65 73 69 6f 6e 73  nt subexpresions
1ef68 0a 2a 2a 20 61 72 65 20 54 4b 5f 52 45 47 49 53  .** are TK_REGIS
1ef69 54 45 52 20 6f 70 63 6f 64 65 73 20 74 68 61 74  TER opcodes that
1ef6a 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 72   refer to the pr
1ef6b 65 63 6f 6d 70 75 74 65 64 20 76 61 6c 75 65 73  ecomputed values
1ef6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1ef6d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1ef6e 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74  ExprCodeConstant
1ef6f 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  s(Parse *pParse,
1ef70 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
1ef71 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78   Walker w;.  w.x
1ef72 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65  ExprCallback = e
1ef73 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b 0a 20 20  valConstExpr;.  
1ef74 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63  w.xSelectCallbac
1ef75 6b 20 3d 20 30 3b 0a 20 20 77 2e 70 50 61 72 73  k = 0;.  w.pPars
1ef76 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71  e = pParse;.  sq
1ef77 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77  lite3WalkExpr(&w
1ef78 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 0a 2f 2a  , pExpr);.}.../*
1ef79 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1ef7a 65 20 74 68 61 74 20 70 75 73 68 65 73 20 74 68  e that pushes th
1ef7b 65 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79  e value of every
1ef7c 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
1ef7d 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73  given.** express
1ef7e 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f 20 61 20  ion list into a 
1ef7f 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69  sequence of regi
1ef80 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20  sters beginning 
1ef81 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a  at target..**.**
1ef82 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1ef83 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 65  er of elements e
1ef84 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c  valuated..*/.SQL
1ef85 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1ef86 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45  sqlite3ExprCodeE
1ef87 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65  xprList(.  Parse
1ef88 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
1ef89 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
1ef8a 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1ef8b 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20  pList,   /* The 
1ef8c 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  expression list 
1ef8d 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20  to be coded */. 
1ef8e 20 69 6e 74 20 74 61 72 67 65 74 2c 20 20 20 20   int target,    
1ef8f 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20      /* Where to 
1ef90 77 72 69 74 65 20 72 65 73 75 6c 74 73 20 2a 2f  write results */
1ef91 0a 20 20 69 6e 74 20 64 6f 48 61 72 64 43 6f 70  .  int doHardCop
1ef92 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61 20  y     /* Make a 
1ef93 68 61 72 64 20 63 6f 70 79 20 6f 66 20 65 76 65  hard copy of eve
1ef94 72 79 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b  ry element */.){
1ef95 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
1ef96 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
1ef97 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 61 73    int i, n;.  as
1ef98 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29  sert( pList!=0 )
1ef99 3b 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67  ;.  assert( targ
1ef9a 65 74 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 70 4c  et>0 );.  n = pL
1ef9b 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 66 6f  ist->nExpr;.  fo
1ef9c 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  r(pItem=pList->a
1ef9d 2c 20 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c  , i=0; i<n; i++,
1ef9e 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69   pItem++){.    i
1ef9f 66 28 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73  f( pItem->iAlias
1efa0 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 52   ){.      int iR
1efa1 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70  eg = codeAlias(p
1efa2 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 69 41  Parse, pItem->iA
1efa3 6c 69 61 73 2c 20 70 49 74 65 6d 2d 3e 70 45 78  lias, pItem->pEx
1efa4 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20  pr, target+i);. 
1efa5 20 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73       Vdbe *v = s
1efa6 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1efa7 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28  arse);.      if(
1efa8 20 69 52 65 67 21 3d 74 61 72 67 65 74 2b 69 20   iReg!=target+i 
1efa9 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1efaa 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1efab 4f 50 5f 53 43 6f 70 79 2c 20 69 52 65 67 2c 20  OP_SCopy, iReg, 
1efac 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20  target+i);.     
1efad 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
1efae 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
1efaf 6f 64 65 28 70 50 61 72 73 65 2c 20 70 49 74 65  ode(pParse, pIte
1efb0 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74  m->pExpr, target
1efb1 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  +i);.    }.    i
1efb2 66 28 20 64 6f 48 61 72 64 43 6f 70 79 20 29 7b  f( doHardCopy ){
1efb3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
1efb4 70 72 48 61 72 64 43 6f 70 79 28 70 50 61 72 73  prHardCopy(pPars
1efb5 65 2c 20 74 61 72 67 65 74 2c 20 6e 29 3b 0a 20  e, target, n);. 
1efb6 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1efb7 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  n n;.}../*.** Ge
1efb8 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
1efb9 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73  a boolean expres
1efba 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61  sion such that a
1efbb 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a   jump is made.**
1efbc 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64   to the label "d
1efbd 65 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72  est" if the expr
1efbe 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 62  ession is true b
1efbf 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20  ut execution.** 
1efc0 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67  continues straig
1efc1 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65  ht thru if the e
1efc2 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c  xpression is fal
1efc3 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
1efc4 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c   expression eval
1efc5 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e  uates to NULL (n
1efc6 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20  either true nor 
1efc7 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a 2a 2a 20  false), then.** 
1efc8 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66  take the jump if
1efc9 20 74 68 65 20 6a 75 6d 70 49 66 4e 75 6c 6c 20   the jumpIfNull 
1efca 66 6c 61 67 20 69 73 20 53 51 4c 49 54 45 5f 4a  flag is SQLITE_J
1efcb 55 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  UMPIFNULL..**.**
1efcc 20 54 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e   This code depen
1efcd 64 73 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74  ds on the fact t
1efce 68 61 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65  hat certain toke
1efcf 6e 20 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b  n values (ex: TK
1efd0 5f 45 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20  _EQ).** are the 
1efd1 73 61 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76  same as opcode v
1efd2 61 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71  alues (ex: OP_Eq
1efd3 29 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  ) that implement
1efd4 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1efd5 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e  ng.** operation.
1efd6 20 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e    Special commen
1efd7 74 73 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64  ts in vdbe.c and
1efd8 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61   the mkopcodeh.a
1efd9 77 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20  wk script in.** 
1efda 74 68 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73  the make process
1efdb 20 63 61 75 73 65 20 74 68 65 73 65 20 76 61 6c   cause these val
1efdc 75 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41  ues to align.  A
1efdd 73 73 65 72 74 28 29 73 20 69 6e 20 74 68 65 20  ssert()s in the 
1efde 63 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65  code.** below ve
1efdf 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75  rify that the nu
1efe0 6d 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65  mbers are aligne
1efe1 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a  d correctly..*/.
1efe2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1efe3 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
1efe4 66 54 72 75 65 28 50 61 72 73 65 20 2a 70 50 61  fTrue(Parse *pPa
1efe5 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
1efe6 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20  , int dest, int 
1efe7 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56  jumpIfNull){.  V
1efe8 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
1efe9 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70  >pVdbe;.  int op
1efea 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46   = 0;.  int regF
1efeb 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ree1 = 0;.  int 
1efec 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20  regFree2 = 0;.  
1efed 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61  int r1, r2;..  a
1efee 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c  ssert( jumpIfNul
1efef 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  l==SQLITE_JUMPIF
1eff0 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75  NULL || jumpIfNu
1eff1 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 76  ll==0 );.  if( v
1eff2 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30 20  ==0 || pExpr==0 
1eff3 29 20 72 65 74 75 72 6e 3b 0a 20 20 6f 70 20 3d  ) return;.  op =
1eff4 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 73 77   pExpr->op;.  sw
1eff5 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20  itch( op ){.    
1eff6 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20  case TK_AND: {. 
1eff7 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 73 71       int d2 = sq
1eff8 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1eff9 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 74 65 73  el(v);.      tes
1effa 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
1effb 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  l==0 );.      te
1effc 73 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e  stcase( pParse->
1effd 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d  disableColCache=
1effe 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
1efff 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
1f000 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1f001 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75  eft, d2,jumpIfNu
1f002 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  ll^SQLITE_JUMPIF
1f003 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 70 50 61  NULL);.      pPa
1f004 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1f005 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71  ache++;.      sq
1f006 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
1f007 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1f008 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d  Right, dest, jum
1f009 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
1f00a 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
1f00b 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e  disableColCache>
1f00c 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  0 );.      pPars
1f00d 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63  e->disableColCac
1f00e 68 65 2d 2d 3b 0a 20 20 20 20 20 20 73 71 6c 69  he--;.      sqli
1f00f 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
1f010 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20  bel(v, d2);.    
1f011 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1f012 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 20 7b     case TK_OR: {
1f013 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1f014 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
1f015 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1f016 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
1f017 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 3b 0a  eColCache==0 );.
1f018 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1f019 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
1f01a 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65  pExpr->pLeft, de
1f01b 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
1f01c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64  .      pParse->d
1f01d 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b  isableColCache++
1f01e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
1f01f 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
1f020 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
1f021 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
1f022 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  l);.      assert
1f023 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
1f024 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20  eColCache>0 );. 
1f025 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73       pParse->dis
1f026 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a  ableColCache--;.
1f027 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1f028 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e   }.    case TK_N
1f029 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  OT: {.      test
1f02a 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
1f02b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
1f02c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
1f02d 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1f02e 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  Left, dest, jump
1f02f 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62  IfNull);.      b
1f030 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1f031 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20  case TK_LT:.    
1f032 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20  case TK_LE:.    
1f033 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20  case TK_GT:.    
1f034 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20  case TK_GE:.    
1f035 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20  case TK_NE:.    
1f036 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20  case TK_EQ: {.  
1f037 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
1f038 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20  T==OP_Lt );.    
1f039 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d    assert( TK_LE=
1f03a 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20  =OP_Le );.      
1f03b 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f  assert( TK_GT==O
1f03c 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Gt );.      as
1f03d 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f  sert( TK_GE==OP_
1f03e 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Ge );.      asse
1f03f 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71  rt( TK_EQ==OP_Eq
1f040 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1f041 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29  ( TK_NE==OP_Ne )
1f042 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1f043 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20  ( op==TK_LT );. 
1f044 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1f045 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20  p==TK_LE );.    
1f046 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1f047 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74  TK_GT );.      t
1f048 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1f049 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  GE );.      test
1f04a 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20  case( op==TK_EQ 
1f04b 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1f04c 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a  e( op==TK_NE );.
1f04d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1f04e 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
1f04f 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
1f050 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73  reOperands(pPars
1f051 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
1f052 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c   &r1, &regFree1,
1f053 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1f054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f055 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74     pExpr->pRight
1f056 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32  , &r2, &regFree2
1f057 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
1f058 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78  pare(pParse, pEx
1f059 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72  pr->pLeft, pExpr
1f05a 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20  ->pRight, op,.  
1f05b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f05c 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75  r1, r2, dest, ju
1f05d 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
1f05e 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
1f05f 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
1f060 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1f061 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e2==0 );.      b
1f062 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1f063 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a  case TK_ISNULL:.
1f064 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e      case TK_NOTN
1f065 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ULL: {.      ass
1f066 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d  ert( TK_ISNULL==
1f067 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20  OP_IsNull );.   
1f068 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f     assert( TK_NO
1f069 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c  TNULL==OP_NotNul
1f06a 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  l );.      testc
1f06b 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55  ase( op==TK_ISNU
1f06c 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LL );.      test
1f06d 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54  case( op==TK_NOT
1f06e 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31  NULL );.      r1
1f06f 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1f070 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
1f071 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65  Expr->pLeft, &re
1f072 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73  gFree1);.      s
1f073 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1f074 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74  (v, op, r1, dest
1f075 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1f076 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
1f077 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1f078 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
1f079 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20  _BETWEEN: {.    
1f07a 20 20 2f 2a 20 20 20 20 78 20 42 45 54 57 45 45    /*    x BETWEE
1f07b 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 20  N y AND z.      
1f07c 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 65  **.      ** Is e
1f07d 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20  quivalent to .  
1f07e 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
1f07f 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a     x>=y AND x<=z
1f080 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
1f081 2a 2a 20 43 6f 64 65 20 69 74 20 61 73 20 73 75  ** Code it as su
1f082 63 68 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20  ch, taking care 
1f083 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e  to do the common
1f084 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 20   subexpression. 
1f085 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61       ** elementa
1f086 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20  tion of x..     
1f087 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 65   */.      Expr e
1f088 78 70 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 78  xprAnd;.      Ex
1f089 70 72 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20  pr compLeft;.   
1f08a 20 20 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68     Expr compRigh
1f08b 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 65 78  t;.      Expr ex
1f08c 70 72 58 3b 0a 0a 20 20 20 20 20 20 65 78 70 72  prX;..      expr
1f08d 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66  X = *pExpr->pLef
1f08e 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64  t;.      exprAnd
1f08f 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20  .op = TK_AND;.  
1f090 20 20 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 66      exprAnd.pLef
1f091 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20  t = &compLeft;. 
1f092 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 52 69       exprAnd.pRi
1f093 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74  ght = &compRight
1f094 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74  ;.      compLeft
1f095 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20  .op = TK_GE;.   
1f096 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66     compLeft.pLef
1f097 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20  t = &exprX;.    
1f098 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68    compLeft.pRigh
1f099 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  t = pExpr->pList
1f09a 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
1f09b 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70      compRight.op
1f09c 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20   = TK_LE;.      
1f09d 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20  compRight.pLeft 
1f09e 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20  = &exprX;.      
1f09f 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74  compRight.pRight
1f0a0 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d   = pExpr->pList-
1f0a1 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[1].pExpr;.   
1f0a2 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20     exprX.iTable 
1f0a3 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
1f0a4 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65  eTemp(pParse, &e
1f0a5 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29  xprX, &regFree1)
1f0a6 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1f0a7 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
1f0a8 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20  .      exprX.op 
1f0a9 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20  = TK_REGISTER;. 
1f0aa 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
1f0ab 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
1f0ac 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1f0ad 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
1f0ae 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20  &exprAnd, dest, 
1f0af 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
1f0b0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1f0b1 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
1f0b2 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
1f0b3 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1f0b4 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65  arse, pExpr, &re
1f0b5 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73  gFree1);.      s
1f0b6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1f0b7 28 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64  (v, OP_If, r1, d
1f0b8 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21  est, jumpIfNull!
1f0b9 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  =0);.      testc
1f0ba 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
1f0bb 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f0bc 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
1f0bd 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
1f0be 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
1f0bf 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
1f0c0 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46  Reg(pParse, regF
1f0c1 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  ree1);.  sqlite3
1f0c2 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
1f0c3 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29  Parse, regFree2)
1f0c4 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  ;  .}../*.** Gen
1f0c5 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
1f0c6 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73   boolean express
1f0c7 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20  ion such that a 
1f0c8 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20  jump is made.** 
1f0c9 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65  to the label "de
1f0ca 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65  st" if the expre
1f0cb 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 20 62  ssion is false b
1f0cc 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20  ut execution.** 
1f0cd 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67  continues straig
1f0ce 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65  ht thru if the e
1f0cf 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75  xpression is tru
1f0d0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
1f0d1 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75  expression evalu
1f0d2 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65  ates to NULL (ne
1f0d3 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66  ither true nor f
1f0d4 61 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75  alse) then.** ju
1f0d5 6d 70 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c  mp if jumpIfNull
1f0d6 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49   is SQLITE_JUMPI
1f0d7 46 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68  FNULL or fall th
1f0d8 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49 66 4e  rough if jumpIfN
1f0d9 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a  ull.** is 0..*/.
1f0da 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1f0db 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
1f0dc 66 46 61 6c 73 65 28 50 61 72 73 65 20 2a 70 50  fFalse(Parse *pP
1f0dd 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
1f0de 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74  r, int dest, int
1f0df 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20   jumpIfNull){.  
1f0e0 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
1f0e1 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f  ->pVdbe;.  int o
1f0e2 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67  p = 0;.  int reg
1f0e3 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74  Free1 = 0;.  int
1f0e4 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20   regFree2 = 0;. 
1f0e5 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20   int r1, r2;..  
1f0e6 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75  assert( jumpIfNu
1f0e7 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49  ll==SQLITE_JUMPI
1f0e8 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e  FNULL || jumpIfN
1f0e9 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20  ull==0 );.  if( 
1f0ea 76 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30  v==0 || pExpr==0
1f0eb 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a   ) return;..  /*
1f0ec 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45   The value of pE
1f0ed 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61  xpr->op and op a
1f0ee 72 65 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f  re related as fo
1f0ef 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  llows:.  **.  **
1f0f0 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
1f0f1 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20              op. 
1f0f2 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d   **       ------
1f0f3 2d 2d 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d  ---          ---
1f0f4 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20  -------.  **    
1f0f5 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20     TK_ISNULL    
1f0f6 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c        OP_NotNull
1f0f7 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e  .  **       TK_N
1f0f8 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f  OTNULL         O
1f0f9 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20  P_IsNull.  **   
1f0fa 20 20 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20      TK_NE       
1f0fb 20 20 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a         OP_Eq.  *
1f0fc 2a 20 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20  *       TK_EQ   
1f0fd 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65             OP_Ne
1f0fe 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47  .  **       TK_G
1f0ff 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  T              O
1f100 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20  P_Le.  **       
1f101 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20  TK_LE           
1f102 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20     OP_Gt.  **   
1f103 20 20 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20      TK_GE       
1f104 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a         OP_Lt.  *
1f105 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20  *       TK_LT   
1f106 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65             OP_Ge
1f107 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f  .  **.  ** For o
1f108 74 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70  ther values of p
1f109 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20  Expr->op, op is 
1f10a 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e  undefined and un
1f10b 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76  used..  ** The v
1f10c 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20  alue of TK_ and 
1f10d 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  OP_ constants ar
1f10e 65 20 61 72 72 61 6e 67 65 64 20 73 75 63 68 20  e arranged such 
1f10f 74 68 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e  that we.  ** can
1f110 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70   compute the map
1f111 70 69 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67  ping above using
1f112 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
1f113 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20  xpression..  ** 
1f114 41 73 73 65 72 74 28 29 73 20 76 65 72 69 66 79  Assert()s verify
1f115 20 74 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74   that the comput
1f116 61 74 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74  ation is correct
1f117 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28  ..  */.  op = ((
1f118 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53  pExpr->op+(TK_IS
1f119 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f  NULL&1))^1)-(TK_
1f11a 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a  ISNULL&1);..  /*
1f11b 20 56 65 72 69 66 79 20 63 6f 72 72 65 63 74 20   Verify correct 
1f11c 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f  alignment of TK_
1f11d 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e   and OP_ constan
1f11e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  ts.  */.  assert
1f11f 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
1f120 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50  ISNULL || op==OP
1f121 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73  _NotNull );.  as
1f122 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
1f123 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f  =TK_NOTNULL || o
1f124 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a  p==OP_IsNull );.
1f125 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1f126 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70  >op!=TK_NE || op
1f127 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73  ==OP_Eq );.  ass
1f128 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
1f129 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  TK_EQ || op==OP_
1f12a 4e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Ne );.  assert( 
1f12b 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54  pExpr->op!=TK_LT
1f12c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b   || op==OP_Ge );
1f12d 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72  .  assert( pExpr
1f12e 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f  ->op!=TK_LE || o
1f12f 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73  p==OP_Gt );.  as
1f130 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
1f131 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50  =TK_GT || op==OP
1f132 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _Le );.  assert(
1f133 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47   pExpr->op!=TK_G
1f134 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29  E || op==OP_Lt )
1f135 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  ;..  switch( pEx
1f136 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61  pr->op ){.    ca
1f137 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20  se TK_AND: {.   
1f138 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
1f139 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
1f13a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 50      testcase( pP
1f13b 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1f13c 43 61 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20  Cache==0 );.    
1f13d 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
1f13e 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
1f13f 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c  pr->pLeft, dest,
1f140 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
1f141 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61      pParse->disa
1f142 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20  bleColCache++;. 
1f143 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1f144 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
1f145 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64  pExpr->pRight, d
1f146 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
1f147 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1f148 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1f149 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20  olCache>0 );.   
1f14a 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62     pParse->disab
1f14b 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20  leColCache--;.  
1f14c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1f14d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a  .    case TK_OR:
1f14e 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20   {.      int d2 
1f14f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
1f150 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
1f151 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
1f152 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
1f153 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72    testcase( pPar
1f154 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
1f155 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  che==0 );.      
1f156 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
1f157 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
1f158 3e 70 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70  >pLeft, d2, jump
1f159 49 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55  IfNull^SQLITE_JU
1f15a 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20  MPIFNULL);.     
1f15b 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
1f15c 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20  ColCache++;.    
1f15d 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
1f15e 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
1f15f 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74  pr->pRight, dest
1f160 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
1f161 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
1f162 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1f163 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20  ache>0 );.      
1f164 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1f165 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20  olCache--;.     
1f166 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
1f167 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b  lveLabel(v, d2);
1f168 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1f169 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1f16a 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  NOT: {.      sql
1f16b 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
1f16c 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1f16d 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  eft, dest, jumpI
1f16e 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72  fNull);.      br
1f16f 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1f170 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63  ase TK_LT:.    c
1f171 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63  ase TK_LE:.    c
1f172 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63  ase TK_GT:.    c
1f173 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63  ase TK_GE:.    c
1f174 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63  ase TK_NE:.    c
1f175 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20  ase TK_EQ: {.   
1f176 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1f177 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20  =TK_LT );.      
1f178 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1f179 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _LE );.      tes
1f17a 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54  tcase( op==TK_GT
1f17b 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f17c 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b  se( op==TK_GE );
1f17d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1f17e 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20   op==TK_EQ );.  
1f17f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1f180 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20  ==TK_NE );.     
1f181 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
1f182 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
1f183 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
1f184 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
1f185 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
1f186 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
1f187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f188 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1f189 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
1f18a 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
1f18b 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
1f18c 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1f18d 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
1f18e 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20  ght, op,.       
1f18f 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72             r1, r
1f190 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  2, dest, jumpIfN
1f191 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ull);.      test
1f192 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
1f193 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
1f194 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
1f195 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
1f196 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1f197 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63  TK_ISNULL:.    c
1f198 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20  ase TK_NOTNULL: 
1f199 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
1f19a 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
1f19b 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1f19c 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c  e( op==TK_NOTNUL
1f19d 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  L );.      r1 = 
1f19e 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1f19f 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
1f1a0 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
1f1a1 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee1);.      sqli
1f1a2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1f1a3 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a   op, r1, dest);.
1f1a4 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1f1a5 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
1f1a6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1f1a7 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45  }.    case TK_BE
1f1a8 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f  TWEEN: {.      /
1f1a9 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79  *    x BETWEEN y
1f1aa 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a   AND z.      **.
1f1ab 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75 69        ** Is equi
1f1ac 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20  valent to .     
1f1ad 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   **.      **    
1f1ae 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20  x>=y AND x<=z.  
1f1af 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
1f1b0 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68 2c  Code it as such,
1f1b1 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f 20   taking care to 
1f1b2 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75  do the common su
1f1b3 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20  bexpression.    
1f1b4 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f    ** elementatio
1f1b5 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f  n of x..      */
1f1b6 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72  .      Expr expr
1f1b7 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72 20  And;.      Expr 
1f1b8 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  compLeft;.      
1f1b9 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a  Expr compRight;.
1f1ba 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 58        Expr exprX
1f1bb 3b 0a 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d  ;..      exprX =
1f1bc 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a   *pExpr->pLeft;.
1f1bd 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70        exprAnd.op
1f1be 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20   = TK_AND;.     
1f1bf 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d   exprAnd.pLeft =
1f1c0 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20   &compLeft;.    
1f1c1 20 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74    exprAnd.pRight
1f1c2 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20   = &compRight;. 
1f1c3 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70       compLeft.op
1f1c4 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20   = TK_GE;.      
1f1c5 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d  compLeft.pLeft =
1f1c6 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63   &exprX;.      c
1f1c7 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d  ompLeft.pRight =
1f1c8 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61   pExpr->pList->a
1f1c9 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [0].pExpr;.     
1f1ca 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20   compRight.op = 
1f1cb 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d  TK_LE;.      com
1f1cc 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26  pRight.pLeft = &
1f1cd 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d  exprX;.      com
1f1ce 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20  pRight.pRight = 
1f1cf 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b  pExpr->pList->a[
1f1d0 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  1].pExpr;.      
1f1d1 65 78 70 72 58 2e 69 54 61 62 6c 65 20 3d 20 73  exprX.iTable = s
1f1d2 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
1f1d3 6d 70 28 70 50 61 72 73 65 2c 20 26 65 78 70 72  mp(pParse, &expr
1f1d4 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  X, &regFree1);. 
1f1d5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
1f1d6 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
1f1d7 20 20 20 20 65 78 70 72 58 2e 6f 70 20 3d 20 54      exprX.op = T
1f1d8 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20  K_REGISTER;.    
1f1d9 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
1f1da 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
1f1db 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
1f1dc 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 26 65  False(pParse, &e
1f1dd 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75  xprAnd, dest, ju
1f1de 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
1f1df 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1f1e0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1f1e1 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
1f1e2 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
1f1e3 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46  se, pExpr, &regF
1f1e4 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ree1);.      sql
1f1e5 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1f1e6 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31 2c 20  , OP_IfNot, r1, 
1f1e7 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
1f1e8 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74  !=0);.      test
1f1e9 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
1f1ea 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
1f1eb 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
1f1ec 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
1f1ed 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  k;.    }.  }.  s
1f1ee 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
1f1ef 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
1f1f0 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65  Free1);.  sqlite
1f1f1 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1f1f2 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32  pParse, regFree2
1f1f3 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61  );.}../*.** Do a
1f1f4 20 64 65 65 70 20 63 6f 6d 70 61 72 69 73 6f 6e   deep comparison
1f1f5 20 6f 66 20 74 77 6f 20 65 78 70 72 65 73 73 69   of two expressi
1f1f6 6f 6e 20 74 72 65 65 73 2e 20 20 52 65 74 75 72  on trees.  Retur
1f1f7 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f  n TRUE (non-zero
1f1f8 29 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65  ).** if they are
1f1f9 20 69 64 65 6e 74 69 63 61 6c 20 61 6e 64 20 72   identical and r
1f1fa 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74  eturn FALSE if t
1f1fb 68 65 79 20 64 69 66 66 65 72 20 69 6e 20 61 6e  hey differ in an
1f1fc 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d  y way..**.** Som
1f1fd 65 74 69 6d 65 73 20 74 68 69 73 20 72 6f 75 74  etimes this rout
1f1fe 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ine will return 
1f1ff 46 41 4c 53 45 20 65 76 65 6e 20 69 66 20 74 68  FALSE even if th
1f200 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e  e two expression
1f201 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 72 65 20  s.** really are 
1f202 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 49 66 20  equivalent.  If 
1f203 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f 76 65 20  we cannot prove 
1f204 74 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73  that the express
1f205 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 64 65 6e  ions are.** iden
1f206 74 69 63 61 6c 2c 20 77 65 20 72 65 74 75 72 6e  tical, we return
1f207 20 46 41 4c 53 45 20 6a 75 73 74 20 74 6f 20 62   FALSE just to b
1f208 65 20 73 61 66 65 2e 20 20 53 6f 20 69 66 20 74  e safe.  So if t
1f209 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72  his routine.** r
1f20a 65 74 75 72 6e 73 20 66 61 6c 73 65 2c 20 74 68  eturns false, th
1f20b 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 20 72 65  en you do not re
1f20c 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 20 63 65  ally know for ce
1f20d 72 74 61 69 6e 20 69 66 20 74 68 65 20 74 77 6f  rtain if the two
1f20e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20  .** expressions 
1f20f 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 42  are the same.  B
1f210 75 74 20 69 66 20 79 6f 75 20 67 65 74 20 61 20  ut if you get a 
1f211 54 52 55 45 20 72 65 74 75 72 6e 2c 20 74 68 65  TRUE return, the
1f212 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 62 65 20  n you.** can be 
1f213 73 75 72 65 20 74 68 65 20 65 78 70 72 65 73 73  sure the express
1f214 69 6f 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d  ions are the sam
1f215 65 2e 20 20 49 6e 20 74 68 65 20 70 6c 61 63 65  e.  In the place
1f216 73 20 77 68 65 72 65 0a 2a 2a 20 74 68 69 73 20  s where.** this 
1f217 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 2c  routine is used,
1f218 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 68 75 72   it does not hur
1f219 74 20 74 6f 20 67 65 74 20 61 6e 20 65 78 74 72  t to get an extr
1f21a 61 20 46 41 4c 53 45 20 2d 20 74 68 61 74 0a 2a  a FALSE - that.*
1f21b 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 72 65 73  * just might res
1f21c 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c 69 67  ult in some slig
1f21d 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f 64 65  htly slower code
1f21e 2e 20 20 42 75 74 20 72 65 74 75 72 6e 69 6e 67  .  But returning
1f21f 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74  .** an incorrect
1f220 20 54 52 55 45 20 63 6f 75 6c 64 20 6c 65 61 64   TRUE could lead
1f221 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f   to a malfunctio
1f222 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1f223 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1f224 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78 70 72  ExprCompare(Expr
1f225 20 2a 70 41 2c 20 45 78 70 72 20 2a 70 42 29 7b   *pA, Expr *pB){
1f226 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
1f227 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a  pA==0||pB==0 ){.
1f228 20 20 20 20 72 65 74 75 72 6e 20 70 42 3d 3d 70      return pB==p
1f229 41 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 2d  A;.  }.  if( pA-
1f22a 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72 65  >op!=pB->op ) re
1f22b 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 70  turn 0;.  if( (p
1f22c 41 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69  A->flags & EP_Di
1f22d 73 74 69 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c  stinct)!=(pB->fl
1f22e 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63  ags & EP_Distinc
1f22f 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  t) ) return 0;. 
1f230 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70   if( !sqlite3Exp
1f231 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65  rCompare(pA->pLe
1f232 66 74 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20 29  ft, pB->pLeft) )
1f233 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
1f234 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d   !sqlite3ExprCom
1f235 70 61 72 65 28 70 41 2d 3e 70 52 69 67 68 74 2c  pare(pA->pRight,
1f236 20 70 42 2d 3e 70 52 69 67 68 74 29 20 29 20 72   pB->pRight) ) r
1f237 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70  eturn 0;.  if( p
1f238 41 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  A->pList ){.    
1f239 69 66 28 20 70 42 2d 3e 70 4c 69 73 74 3d 3d 30  if( pB->pList==0
1f23a 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
1f23b 20 69 66 28 20 70 41 2d 3e 70 4c 69 73 74 2d 3e   if( pA->pList->
1f23c 6e 45 78 70 72 21 3d 70 42 2d 3e 70 4c 69 73 74  nExpr!=pB->pList
1f23d 2d 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e  ->nExpr ) return
1f23e 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   0;.    for(i=0;
1f23f 20 69 3c 70 41 2d 3e 70 4c 69 73 74 2d 3e 6e 45   i<pA->pList->nE
1f240 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
1f241 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70   if( !sqlite3Exp
1f242 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 69  rCompare(pA->pLi
1f243 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20  st->a[i].pExpr, 
1f244 70 42 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  pB->pList->a[i].
1f245 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
1f246 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
1f247 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
1f248 65 20 69 66 28 20 70 42 2d 3e 70 4c 69 73 74 20  e if( pB->pList 
1f249 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
1f24a 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 2d 3e 70  .  }.  if( pA->p
1f24b 53 65 6c 65 63 74 20 7c 7c 20 70 42 2d 3e 70 53  Select || pB->pS
1f24c 65 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30  elect ) return 0
1f24d 3b 0a 20 20 69 66 28 20 70 41 2d 3e 69 54 61 62  ;.  if( pA->iTab
1f24e 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c 65 20 7c  le!=pB->iTable |
1f24f 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70  | pA->iColumn!=p
1f250 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20 72 65 74  B->iColumn ) ret
1f251 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 2d  urn 0;.  if( pA-
1f252 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26  >op!=TK_COLUMN &
1f253 26 20 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 20 29 7b  & pA->token.z ){
1f254 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b  .    if( pB->tok
1f255 65 6e 2e 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  en.z==0 ) return
1f256 20 30 3b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e   0;.    if( pB->
1f257 74 6f 6b 65 6e 2e 6e 21 3d 70 41 2d 3e 74 6f 6b  token.n!=pA->tok
1f258 65 6e 2e 6e 20 29 20 72 65 74 75 72 6e 20 30 3b  en.n ) return 0;
1f259 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1f25a 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29  StrNICmp((char*)
1f25b 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 2c 28 63 68 61  pA->token.z,(cha
1f25c 72 2a 29 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 2c 70  r*)pB->token.z,p
1f25d 42 2d 3e 74 6f 6b 65 6e 2e 6e 29 21 3d 30 20 29  B->token.n)!=0 )
1f25e 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
1f25f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1f260 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn 1;.}.../*.*
1f261 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d  * Add a new elem
1f262 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49  ent to the pAggI
1f263 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61  nfo->aCol[] arra
1f264 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69  y.  Return the i
1f265 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e  ndex of.** the n
1f266 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74  ew element.  Ret
1f267 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e  urn a negative n
1f268 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20  umber if malloc 
1f269 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  fails..*/.static
1f26a 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 43   int addAggInfoC
1f26b 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64  olumn(sqlite3 *d
1f26c 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66  b, AggInfo *pInf
1f26d 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70  o){.  int i;.  p
1f26e 49 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c  Info->aCol = sql
1f26f 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74  ite3ArrayAllocat
1f270 65 28 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 20  e(.       db,.  
1f271 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c       pInfo->aCol
1f272 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28  ,.       sizeof(
1f273 70 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c  pInfo->aCol[0]),
1f274 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20  .       3,.     
1f275 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d    &pInfo->nColum
1f276 6e 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f  n,.       &pInfo
1f277 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a  ->nColumnAlloc,.
1f278 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20         &i.  );. 
1f279 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20   return i;.}    
1f27a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
1f27b 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65  w element to the
1f27c 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63   pAggInfo->aFunc
1f27d 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72  [] array.  Retur
1f27e 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a  n the index of.*
1f27f 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e  * the new elemen
1f280 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67  t.  Return a neg
1f281 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20  ative number if 
1f282 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f  malloc fails..*/
1f283 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 41  .static int addA
1f284 67 67 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74  ggInfoFunc(sqlit
1f285 65 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20  e3 *db, AggInfo 
1f286 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69  *pInfo){.  int i
1f287 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63  ;.  pInfo->aFunc
1f288 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41   = sqlite3ArrayA
1f289 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20  llocate(.       
1f28a 64 62 2c 20 0a 20 20 20 20 20 20 20 70 49 6e 66  db, .       pInf
1f28b 6f 2d 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 20  o->aFunc,.      
1f28c 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61   sizeof(pInfo->a
1f28d 46 75 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20  Func[0]),.      
1f28e 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66   3,.       &pInf
1f28f 6f 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20  o->nFunc,.      
1f290 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c   &pInfo->nFuncAl
1f291 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20  loc,.       &i. 
1f292 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a   );.  return i;.
1f293 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  }    ../*.** Thi
1f294 73 20 69 73 20 74 68 65 20 78 45 78 70 72 43 61  s is the xExprCa
1f295 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65  llback for a tre
1f296 65 20 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73  e walker.  It is
1f297 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c   used to.** impl
1f298 65 6d 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70  ement sqlite3Exp
1f299 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
1f29a 65 73 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74  es().  See sqlit
1f29b 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
1f29c 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61  regates.** for a
1f29d 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1f29e 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
1f29f 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72   int analyzeAggr
1f2a0 65 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57  egate(Walker *pW
1f2a1 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78  alker, Expr *pEx
1f2a2 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  pr){.  int i;.  
1f2a3 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
1f2a4 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e   = pWalker->u.pN
1f2a5 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  C;.  Parse *pPar
1f2a6 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
1f2a7 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  ;.  SrcList *pSr
1f2a8 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  cList = pNC->pSr
1f2a9 63 4c 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f  cList;.  AggInfo
1f2aa 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43   *pAggInfo = pNC
1f2ab 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73  ->pAggInfo;..  s
1f2ac 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70  witch( pExpr->op
1f2ad 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
1f2ae 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20  AGG_COLUMN:.    
1f2af 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20  case TK_COLUMN: 
1f2b0 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
1f2b1 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
1f2b2 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  AGG_COLUMN );.  
1f2b3 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
1f2b4 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
1f2b5 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43  MN );.      /* C
1f2b6 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1f2b7 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20  he column is in 
1f2b8 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  one of the table
1f2b9 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20  s in the FROM.  
1f2ba 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66      ** clause of
1f2bb 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71   the aggregate q
1f2bc 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66  uery */.      if
1f2bd 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20  ( pSrcList ){.  
1f2be 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63        struct Src
1f2bf 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1f2c0 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 0a   = pSrcList->a;.
1f2c1 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
1f2c2 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72   i<pSrcList->nSr
1f2c3 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  c; i++, pItem++)
1f2c4 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75  {.          stru
1f2c5 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a  ct AggInfo_col *
1f2c6 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20  pCol;.          
1f2c7 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  if( pExpr->iTabl
1f2c8 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f  e==pItem->iCurso
1f2c9 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  r ){.           
1f2ca 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20   /* If we reach 
1f2cb 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d  this point, it m
1f2cc 65 61 6e 73 20 74 68 61 74 20 70 45 78 70 72 20  eans that pExpr 
1f2cd 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c  refers to a tabl
1f2ce 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  e.            **
1f2cf 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20   that is in the 
1f2d0 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
1f2d1 68 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65  he aggregate que
1f2d2 72 79 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20  ry.  .          
1f2d3 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20    **.           
1f2d4 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72   ** Make an entr
1f2d5 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e  y for the column
1f2d6 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43   in pAggInfo->aC
1f2d7 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20  ol[] if there.  
1f2d8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20            ** is 
1f2d9 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65  not an entry the
1f2da 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20  re already..    
1f2db 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1f2dc 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20         int k;.  
1f2dd 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d            pCol =
1f2de 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b   pAggInfo->aCol;
1f2df 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72  .            for
1f2e0 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f  (k=0; k<pAggInfo
1f2e1 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20  ->nColumn; k++, 
1f2e2 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20  pCol++){.       
1f2e3 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
1f2e4 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e  >iTable==pExpr->
1f2e5 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20  iTable &&.      
1f2e6 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
1f2e7 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72  ->iColumn==pExpr
1f2e8 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->iColumn ){.   
1f2e9 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
1f2ea 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
1f2eb 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1f2ec 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  }.            if
1f2ed 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e  ( (k>=pAggInfo->
1f2ee 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20  nColumn).       
1f2ef 20 20 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64        && (k = ad
1f2f0 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70  dAggInfoColumn(p
1f2f1 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49  Parse->db, pAggI
1f2f2 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20  nfo))>=0 .      
1f2f3 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
1f2f4 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70         pCol = &p
1f2f5 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d  AggInfo->aCol[k]
1f2f6 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1f2f7 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78  pCol->pTab = pEx
1f2f8 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20  pr->pTab;.      
1f2f9 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54          pCol->iT
1f2fa 61 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54  able = pExpr->iT
1f2fb 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20  able;.          
1f2fc 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d      pCol->iColum
1f2fd 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  n = pExpr->iColu
1f2fe 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  mn;.            
1f2ff 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b    pCol->iMem = +
1f300 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
1f301 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
1f302 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
1f303 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20   = -1;.         
1f304 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72       pCol->pExpr
1f305 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20   = pExpr;.      
1f306 20 20 20 20 20 20 20 20 69 66 28 20 70 41 67 67          if( pAgg
1f307 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29  Info->pGroupBy )
1f308 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1f309 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20    int j, n;.    
1f30a 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72              Expr
1f30b 4c 69 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67  List *pGB = pAgg
1f30c 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a  Info->pGroupBy;.
1f30d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f30e 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1f30f 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47  item *pTerm = pG
1f310 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20  B->a;.          
1f311 20 20 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e        n = pGB->n
1f312 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Expr;.          
1f313 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
1f314 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b  <n; j++, pTerm++
1f315 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1f316 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20       Expr *pE = 
1f317 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
1f318 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f319 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43  if( pE->op==TK_C
1f31a 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61  OLUMN && pE->iTa
1f31b 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62  ble==pExpr->iTab
1f31c 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  le &&.          
1f31d 20 20 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e              pE->
1f31e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e  iColumn==pExpr->
1f31f 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20  iColumn ){.     
1f320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1f321 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75  Col->iSorterColu
1f322 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20  mn = j;.        
1f323 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
1f324 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
1f325 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1f326 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1f327 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1f328 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
1f329 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30  >iSorterColumn<0
1f32a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1f32b 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65      pCol->iSorte
1f32c 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e  rColumn = pAggIn
1f32d 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75  fo->nSortingColu
1f32e 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  mn++;.          
1f32f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1f330 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1f331 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20  /* There is now 
1f332 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78  an entry for pEx
1f333 70 72 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e  pr in pAggInfo->
1f334 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20  aCol[] (either. 
1f335 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65             ** be
1f336 63 61 75 73 65 20 69 74 20 77 61 73 20 74 68 65  cause it was the
1f337 72 65 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63  re before or bec
1f338 61 75 73 65 20 77 65 20 6a 75 73 74 20 63 72 65  ause we just cre
1f339 61 74 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20  ated it)..      
1f33a 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74        ** Convert
1f33b 20 74 68 65 20 70 45 78 70 72 20 74 6f 20 62 65   the pExpr to be
1f33c 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e   a TK_AGG_COLUMN
1f33d 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68   referring to th
1f33e 61 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  at.            *
1f33f 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  * pAggInfo->aCol
1f340 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20  [] entry..      
1f341 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1f342 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67       pExpr->pAgg
1f343 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b  Info = pAggInfo;
1f344 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78  .            pEx
1f345 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f  pr->op = TK_AGG_
1f346 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20  COLUMN;.        
1f347 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20      pExpr->iAgg 
1f348 3d 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = k;.           
1f349 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1f34a 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78    } /* endif pEx
1f34b 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65  pr->iTable==pIte
1f34c 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20  m->iCursor */.  
1f34d 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c        } /* end l
1f34e 6f 6f 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73  oop over pSrcLis
1f34f 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  t */.      }.   
1f350 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
1f351 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  une;.    }.    c
1f352 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  ase TK_AGG_FUNCT
1f353 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  ION: {.      /* 
1f354 54 68 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d  The pNC->nDepth=
1f355 3d 30 20 74 65 73 74 20 63 61 75 73 65 73 20 61  =0 test causes a
1f356 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
1f357 6e 73 20 69 6e 20 73 75 62 71 75 65 72 69 65 73  ns in subqueries
1f358 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20  .      ** to be 
1f359 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20  ignored */.     
1f35a 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68   if( pNC->nDepth
1f35b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
1f35c 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1f35d 66 20 70 45 78 70 72 20 69 73 20 61 20 64 75 70  f pExpr is a dup
1f35e 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65  licate of anothe
1f35f 72 20 61 67 67 72 65 67 61 74 65 20 0a 20 20 20  r aggregate .   
1f360 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e       ** function
1f361 20 74 68 61 74 20 69 73 20 61 6c 72 65 61 64 79   that is already
1f362 20 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f   in the pAggInfo
1f363 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 20   structure.     
1f364 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74     */.        st
1f365 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
1f366 63 20 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49  c *pItem = pAggI
1f367 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20  nfo->aFunc;.    
1f368 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1f369 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20  AggInfo->nFunc; 
1f36a 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
1f36b 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
1f36c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
1f36d 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45  pItem->pExpr, pE
1f36e 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20  xpr) ){.        
1f36f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1f370 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1f371 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d  .        if( i>=
1f372 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20  pAggInfo->nFunc 
1f373 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
1f374 70 45 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61  pExpr is origina
1f375 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65  l.  Make a new e
1f376 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f  ntry in pAggInfo
1f377 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20  ->aFunc[].      
1f378 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
1f379 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50   u8 enc = ENC(pP
1f37a 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20  arse->db);.     
1f37b 20 20 20 20 20 69 20 3d 20 61 64 64 41 67 67 49       i = addAggI
1f37c 6e 66 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e  nfoFunc(pParse->
1f37d 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20  db, pAggInfo);. 
1f37e 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d           if( i>=
1f37f 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1f380 20 70 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e   pItem = &pAggIn
1f381 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20  fo->aFunc[i];.  
1f382 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d            pItem-
1f383 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a  >pExpr = pExpr;.
1f384 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65              pIte
1f385 6d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72  m->iMem = ++pPar
1f386 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
1f387 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 46 75        pItem->pFu
1f388 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  nc = sqlite3Find
1f389 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d  Function(pParse-
1f38a 3e 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  >db,.           
1f38b 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
1f38c 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70  Expr->token.z, p
1f38d 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 0a 20  Expr->token.n,. 
1f38e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f38f 20 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 20 3f    pExpr->pList ?
1f390 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e   pExpr->pList->n
1f391 45 78 70 72 20 3a 20 30 2c 20 65 6e 63 2c 20 30  Expr : 0, enc, 0
1f392 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
1f393 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  f( pExpr->flags 
1f394 26 20 45 50 5f 44 69 73 74 69 6e 63 74 20 29 7b  & EP_Distinct ){
1f395 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
1f396 49 74 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20  Item->iDistinct 
1f397 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
1f398 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65  ;.            }e
1f399 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
1f39a 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69     pItem->iDisti
1f39b 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  nct = -1;.      
1f39c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1f39d 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1f39e 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 70 45        /* Make pE
1f39f 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 74 68 65  xpr point to the
1f3a0 20 61 70 70 72 6f 70 72 69 61 74 65 20 70 41 67   appropriate pAg
1f3a1 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 65  gInfo->aFunc[] e
1f3a2 6e 74 72 79 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ntry.        */.
1f3a3 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1f3a4 41 67 67 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  Agg = i;.       
1f3a5 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f   pExpr->pAggInfo
1f3a6 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20   = pAggInfo;.   
1f3a7 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
1f3a8 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 7d 0a 20  Prune;.      }. 
1f3a9 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1f3aa 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  n WRC_Continue;.
1f3ab 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61  }.static int ana
1f3ac 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 49 6e  lyzeAggregatesIn
1f3ad 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70  Select(Walker *p
1f3ae 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
1f3af 70 53 65 6c 65 63 74 29 7b 0a 20 20 4e 61 6d 65  pSelect){.  Name
1f3b0 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70  Context *pNC = p
1f3b1 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20  Walker->u.pNC;. 
1f3b2 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68   if( pNC->nDepth
1f3b3 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e  ==0 ){.    pNC->
1f3b4 6e 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 73 71  nDepth++;.    sq
1f3b5 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
1f3b6 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c 65 63 74  pWalker, pSelect
1f3b7 29 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70  );.    pNC->nDep
1f3b8 74 68 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e  th--;.    return
1f3b9 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 65   WRC_Prune;.  }e
1f3ba 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
1f3bb 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  WRC_Continue;.  
1f3bc 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79  }.}../*.** Analy
1f3bd 7a 65 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  ze the given exp
1f3be 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69 6e 67 20  ression looking 
1f3bf 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75  for aggregate fu
1f3c0 6e 63 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66  nctions and.** f
1f3c1 6f 72 20 76 61 72 69 61 62 6c 65 73 20 74 68 61  or variables tha
1f3c2 74 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64 64  t need to be add
1f3c3 65 64 20 74 6f 20 74 68 65 20 70 50 61 72 73 65  ed to the pParse
1f3c4 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 2e 0a  ->aAgg[] array..
1f3c5 2a 2a 20 4d 61 6b 65 20 61 64 64 69 74 69 6f 6e  ** Make addition
1f3c6 61 6c 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68  al entries to th
1f3c7 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d  e pParse->aAgg[]
1f3c8 20 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73   array as necess
1f3c9 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ary..**.** This 
1f3ca 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f  routine should o
1f3cb 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 61 66  nly be called af
1f3cc 74 65 72 20 74 68 65 20 65 78 70 72 65 73 73 69  ter the expressi
1f3cd 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61  on has been.** a
1f3ce 6e 61 6c 79 7a 65 64 20 62 79 20 73 71 6c 69 74  nalyzed by sqlit
1f3cf 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
1f3d0 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  es()..*/.SQLITE_
1f3d1 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1f3d2 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
1f3d3 67 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f  ggregates(NameCo
1f3d4 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72  ntext *pNC, Expr
1f3d5 20 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b   *pExpr){.  Walk
1f3d6 65 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43  er w;.  w.xExprC
1f3d7 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a  allback = analyz
1f3d8 65 41 67 67 72 65 67 61 74 65 3b 0a 20 20 77 2e  eAggregate;.  w.
1f3d9 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
1f3da 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  = analyzeAggrega
1f3db 74 65 73 49 6e 53 65 6c 65 63 74 3b 0a 20 20 77  tesInSelect;.  w
1f3dc 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20  .u.pNC = pNC;.  
1f3dd 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1f3de 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f  &w, pExpr);.}../
1f3df 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
1f3e0 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72  3ExprAnalyzeAggr
1f3e1 65 67 61 74 65 73 28 29 20 66 6f 72 20 65 76 65  egates() for eve
1f3e2 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ry expression in
1f3e3 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f   an.** expressio
1f3e4 6e 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 20  n list.  Return 
1f3e5 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  the number of er
1f3e6 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rors..**.** If a
1f3e7 6e 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e 64  n error is found
1f3e8 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 69  , the analysis i
1f3e9 73 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f 0a  s cut short..*/.
1f3ea 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1f3eb 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
1f3ec 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61  nalyzeAggList(Na
1f3ed 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20  meContext *pNC, 
1f3ee 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
1f3ef 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  {.  struct ExprL
1f3f0 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
1f3f1 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
1f3f2 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72  pList ){.    for
1f3f3 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c  (pItem=pList->a,
1f3f4 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e   i=0; i<pList->n
1f3f5 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
1f3f6 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
1f3f7 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
1f3f8 72 65 67 61 74 65 73 28 70 4e 43 2c 20 70 49 74  regates(pNC, pIt
1f3f9 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20  em->pExpr);.    
1f3fa 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  }.  }.}../*.** A
1f3fb 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61 6c 6c  llocate or deall
1f3fc 6f 63 61 74 65 20 74 65 6d 70 6f 72 61 72 79 20  ocate temporary 
1f3fd 75 73 65 20 72 65 67 69 73 74 65 72 73 20 64 75  use registers du
1f3fe 72 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61  ring code genera
1f3ff 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
1f400 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1f401 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 61  te3GetTempReg(Pa
1f402 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
1f403 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d  if( pParse->nTem
1f404 70 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 72  pReg==0 ){.    r
1f405 65 74 75 72 6e 20 2b 2b 70 50 61 72 73 65 2d 3e  eturn ++pParse->
1f406 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  nMem;.  }.  retu
1f407 72 6e 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70  rn pParse->aTemp
1f408 52 65 67 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e 54  Reg[--pParse->nT
1f409 65 6d 70 52 65 67 5d 3b 0a 7d 0a 53 51 4c 49 54  empReg];.}.SQLIT
1f40a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1f40b 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
1f40c 70 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72  pReg(Parse *pPar
1f40d 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20  se, int iReg){. 
1f40e 20 69 66 28 20 69 52 65 67 20 26 26 20 70 50 61   if( iReg && pPa
1f40f 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c 41 72  rse->nTempReg<Ar
1f410 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d 3e  raySize(pParse->
1f411 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 20 20  aTempReg) ){.   
1f412 20 73 71 6c 69 74 65 33 45 78 70 72 57 72 69 74   sqlite3ExprWrit
1f413 61 62 6c 65 52 65 67 69 73 74 65 72 28 70 50 61  ableRegister(pPa
1f414 72 73 65 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  rse, iReg);.    
1f415 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67  pParse->aTempReg
1f416 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65  [pParse->nTempRe
1f417 67 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20 20 7d  g++] = iReg;.  }
1f418 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
1f419 74 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65  te or deallocate
1f41a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52 65 67   a block of nReg
1f41b 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67   consecutive reg
1f41c 69 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49 54 45  isters.*/.SQLITE
1f41d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1f41e 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
1f41f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1f420 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74  int nReg){.  int
1f421 20 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70 50 61   i, n;.  i = pPa
1f422 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0a  rse->iRangeReg;.
1f423 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 52    n = pParse->nR
1f424 61 6e 67 65 52 65 67 3b 0a 20 20 69 66 28 20 6e  angeReg;.  if( n
1f425 52 65 67 3c 3d 6e 20 26 26 20 21 75 73 65 64 41  Reg<=n && !usedA
1f426 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61  sColumnCache(pPa
1f427 72 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29  rse, i, i+n-1) )
1f428 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52  {.    pParse->iR
1f429 61 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b  angeReg += nReg;
1f42a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52 61  .    pParse->nRa
1f42b 6e 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b 0a  ngeReg -= nReg;.
1f42c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 20 3d    }else{.    i =
1f42d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b   pParse->nMem+1;
1f42e 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
1f42f 6d 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a 20  m += nReg;.  }. 
1f430 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c   return i;.}.SQL
1f431 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1f432 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
1f433 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a  empRange(Parse *
1f434 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67  pParse, int iReg
1f435 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69  , int nReg){.  i
1f436 66 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d 3e  f( nReg>pParse->
1f437 6e 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20 20  nRangeReg ){.   
1f438 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52   pParse->nRangeR
1f439 65 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70  eg = nReg;.    p
1f43a 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67  Parse->iRangeReg
1f43b 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a   = iReg;.  }.}..
1f43c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1f43d 45 6e 64 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a  End of expr.c **
1f43e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f43f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1f441 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1f442 42 65 67 69 6e 20 66 69 6c 65 20 61 6c 74 65 72  Begin file alter
1f443 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1f444 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f445 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1f446 2f 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75  /*.** 2005 Febru
1f447 61 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ary 15.**.** The
1f448 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1f449 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1f44a 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1f44b 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1f44c 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1f44d 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1f44e 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1f44f 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1f450 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1f451 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1f452 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1f453 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1f454 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1f455 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1f456 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1f457 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1f458 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1f459 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f45a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f45b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f45c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f45d 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
1f45e 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
1f45f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  de routines that
1f460 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74   used to generat
1f461 65 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74  e VDBE code.** t
1f462 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
1f463 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63  he ALTER TABLE c
1f464 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49  ommand..**.** $I
1f465 64 3a 20 61 6c 74 65 72 2e 63 2c 76 20 31 2e 35  d: alter.c,v 1.5
1f466 31 20 32 30 30 38 2f 31 32 2f 31 30 20 31 39 3a  1 2008/12/10 19:
1f467 32 36 3a 32 32 20 64 72 68 20 45 78 70 20 24 0a  26:22 drh Exp $.
1f468 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
1f469 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
1f46a 6f 6e 6c 79 20 65 78 69 73 74 73 20 69 66 20 77  only exists if w
1f46b 65 20 61 72 65 20 6e 6f 74 20 6f 6d 69 74 74 69  e are not omitti
1f46c 6e 67 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20  ng the.** ALTER 
1f46d 54 41 42 4c 45 20 6c 6f 67 69 63 20 66 72 6f 6d  TABLE logic from
1f46e 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23   the build..*/.#
1f46f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f470 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 0a 0a  IT_ALTERTABLE...
1f471 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1f472 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 53  ion is used by S
1f473 51 4c 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  QL generated to 
1f474 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 0a 2a  implement the .*
1f475 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f  * ALTER TABLE co
1f476 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74  mmand. The first
1f477 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
1f478 20 74 65 78 74 20 6f 66 20 61 20 43 52 45 41 54   text of a CREAT
1f479 45 20 54 41 42 4c 45 20 6f 72 0a 2a 2a 20 43 52  E TABLE or.** CR
1f47a 45 41 54 45 20 49 4e 44 45 58 20 63 6f 6d 6d 61  EATE INDEX comma
1f47b 6e 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69  nd. The second i
1f47c 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  s a table name. 
1f47d 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  The table name i
1f47e 6e 20 0a 2a 2a 20 74 68 65 20 43 52 45 41 54 45  n .** the CREATE
1f47f 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45   TABLE or CREATE
1f480 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
1f481 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 69 74   is replaced wit
1f482 68 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 61  h the third.** a
1f483 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20  rgument and the 
1f484 72 65 73 75 6c 74 20 72 65 74 75 72 6e 65 64 2e  result returned.
1f485 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a   Examples:.**.**
1f486 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74   sqlite_rename_t
1f487 61 62 6c 65 28 27 43 52 45 41 54 45 20 54 41 42  able('CREATE TAB
1f488 4c 45 20 61 62 63 28 61 2c 20 62 2c 20 63 29 27  LE abc(a, b, c)'
1f489 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20 20 20 20  , 'def').**     
1f48a 2d 3e 20 27 43 52 45 41 54 45 20 54 41 42 4c 45  -> 'CREATE TABLE
1f48b 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a   def(a, b, c)'.*
1f48c 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61  *.** sqlite_rena
1f48d 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45  me_table('CREATE
1f48e 20 49 4e 44 45 58 20 69 20 4f 4e 20 61 62 63 28   INDEX i ON abc(
1f48f 61 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20  a)', 'def').**  
1f490 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20 49 4e     -> 'CREATE IN
1f491 44 45 58 20 69 20 4f 4e 20 64 65 66 28 61 2c 20  DEX i ON def(a, 
1f492 62 2c 20 63 29 27 0a 2a 2f 0a 73 74 61 74 69 63  b, c)'.*/.static
1f493 20 76 6f 69 64 20 72 65 6e 61 6d 65 54 61 62 6c   void renameTabl
1f494 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
1f495 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
1f496 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
1f497 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
1f498 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e  e **argv.){.  un
1f499 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
1f49a 74 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  t *zSql = sqlite
1f49b 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
1f49c 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65  v[0]);.  unsigne
1f49d 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54  d char const *zT
1f49e 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74  ableName = sqlit
1f49f 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
1f4a0 67 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74  gv[1]);..  int t
1f4a1 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e  oken;.  Token tn
1f4a2 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  ame;.  unsigned 
1f4a3 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72  char const *zCsr
1f4a4 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c   = zSql;.  int l
1f4a5 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  en = 0;.  char *
1f4a6 7a 52 65 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33  zRet;..  sqlite3
1f4a7 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
1f4a8 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
1f4a9 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e  (context);..  UN
1f4aa 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
1f4ab 6f 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54  otUsed);..  /* T
1f4ac 68 65 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65  he principle use
1f4ad 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  d to locate the 
1f4ae 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68  table name in th
1f4af 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 0a  e CREATE TABLE .
1f4b0 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69    ** statement i
1f4b1 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  s that the table
1f4b2 20 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72   name is the fir
1f4b3 73 74 20 6e 6f 6e 2d 73 70 61 63 65 20 74 6f 6b  st non-space tok
1f4b4 65 6e 20 74 68 61 74 0a 20 20 2a 2a 20 69 73 20  en that.  ** is 
1f4b5 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
1f4b6 6f 77 65 64 20 62 79 20 61 20 54 4b 5f 4c 50 20  owed by a TK_LP 
1f4b7 6f 72 20 54 4b 5f 55 53 49 4e 47 20 74 6f 6b 65  or TK_USING toke
1f4b8 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53  n..  */.  if( zS
1f4b9 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 20  ql ){.    do {. 
1f4ba 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72 20       if( !*zCsr 
1f4bb 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 61  ){.        /* Ra
1f4bc 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 62  n out of input b
1f4bd 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 61 6e  efore finding an
1f4be 20 6f 70 65 6e 69 6e 67 20 62 72 61 63 6b 65 74   opening bracket
1f4bf 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a  . Return NULL. *
1f4c0 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
1f4c1 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
1f4c2 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f   /* Store the to
1f4c3 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f  ken that zCsr po
1f4c4 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65  ints to in tname
1f4c5 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65  . */.      tname
1f4c6 2e 7a 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20  .z = zCsr;.     
1f4c7 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a   tname.n = len;.
1f4c8 0a 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63  .      /* Advanc
1f4c9 65 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65  e zCsr to the ne
1f4ca 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20  xt token. Store 
1f4cb 74 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20  that token type 
1f4cc 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20  in 'token',.    
1f4cd 20 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e    ** and its len
1f4ce 67 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f  gth in 'len' (to
1f4cf 20 62 65 20 75 73 65 64 20 6e 65 78 74 20 69 74   be used next it
1f4d0 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  eration of this 
1f4d1 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a  loop)..      */.
1f4d2 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20        do {.     
1f4d3 20 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a     zCsr += len;.
1f4d4 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71          len = sq
1f4d5 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43  lite3GetToken(zC
1f4d6 73 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20  sr, &token);.   
1f4d7 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65     } while( toke
1f4d8 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20  n==TK_SPACE );. 
1f4d9 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e       assert( len
1f4da 3e 30 20 29 3b 0a 20 20 20 20 7d 20 77 68 69 6c  >0 );.    } whil
1f4db 65 28 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20  e( token!=TK_LP 
1f4dc 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49  && token!=TK_USI
1f4dd 4e 47 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74 20  NG );..    zRet 
1f4de 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
1f4df 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22  (db, "%.*s\"%w\"
1f4e0 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 7a  %s", tname.z - z
1f4e1 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20  Sql, zSql, .    
1f4e2 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74     zTableName, t
1f4e3 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b  name.z+tname.n);
1f4e4 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
1f4e5 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
1f4e6 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49  , zRet, -1, SQLI
1f4e7 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d  TE_DYNAMIC);.  }
1f4e8 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
1f4e9 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
1f4ea 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
1f4eb 20 69 73 20 75 73 65 64 20 62 79 20 53 51 4c 20   is used by SQL 
1f4ec 67 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d 70  generated to imp
1f4ed 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c  lement the.** AL
1f4ee 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e  TER TABLE comman
1f4ef 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67  d. The first arg
1f4f0 75 6d 65 6e 74 20 69 73 20 74 68 65 20 74 65 78  ument is the tex
1f4f1 74 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52  t of a CREATE TR
1f4f2 49 47 47 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d  IGGER .** statem
1f4f3 65 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20  ent. The second 
1f4f4 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e  is a table name.
1f4f5 20 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   The table name 
1f4f6 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 0a 2a  in the CREATE .*
1f4f7 2a 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d  * TRIGGER statem
1f4f8 65 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64 20  ent is replaced 
1f4f9 77 69 74 68 20 74 68 65 20 74 68 69 72 64 20 61  with the third a
1f4fa 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20  rgument and the 
1f4fb 72 65 73 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72  result .** retur
1f4fc 6e 65 64 2e 20 54 68 69 73 20 69 73 20 61 6e 61  ned. This is ana
1f4fd 6c 61 67 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65  lagous to rename
1f4fe 54 61 62 6c 65 46 75 6e 63 28 29 20 61 62 6f 76  TableFunc() abov
1f4ff 65 2c 20 65 78 63 65 70 74 20 66 6f 72 20 43 52  e, except for CR
1f500 45 41 54 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c  EATE.** TRIGGER,
1f501 20 6e 6f 74 20 43 52 45 41 54 45 20 49 4e 44 45   not CREATE INDE
1f502 58 20 61 6e 64 20 43 52 45 41 54 45 20 54 41 42  X and CREATE TAB
1f503 4c 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  LE..*/.static vo
1f504 69 64 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72  id renameTrigger
1f505 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
1f506 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
1f507 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
1f508 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
1f509 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73   **argv.){.  uns
1f50a 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74  igned char const
1f50b 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33   *zSql = sqlite3
1f50c 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
1f50d 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64  [0]);.  unsigned
1f50e 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61   char const *zTa
1f50f 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  bleName = sqlite
1f510 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
1f511 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f  v[1]);..  int to
1f512 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61  ken;.  Token tna
1f513 6d 65 3b 0a 20 20 69 6e 74 20 64 69 73 74 20 3d  me;.  int dist =
1f514 20 33 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   3;.  unsigned c
1f515 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20  har const *zCsr 
1f516 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65  = zSql;.  int le
1f517 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a  n = 0;.  char *z
1f518 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Ret;.  sqlite3 *
1f519 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
1f51a 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
1f51b 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53  ontext);..  UNUS
1f51c 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
1f51d 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  Used);..  /* The
1f51e 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20   principle used 
1f51f 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61  to locate the ta
1f520 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  ble name in the 
1f521 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 0a  CREATE TRIGGER .
1f522 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69    ** statement i
1f523 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  s that the table
1f524 20 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72   name is the fir
1f525 73 74 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73  st token that is
1f526 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 0a 20 20   immediatedly.  
1f527 2a 2a 20 70 72 65 63 65 64 65 64 20 62 79 20 65  ** preceded by e
1f528 69 74 68 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54  ither TK_ON or T
1f529 4b 5f 44 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69  K_DOT and immedi
1f52a 61 74 65 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20  atedly followed 
1f52b 62 79 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54  by one.  ** of T
1f52c 4b 5f 57 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e  K_WHEN, TK_BEGIN
1f52d 20 6f 72 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f   or TK_FOR..  */
1f52e 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20  .  if( zSql ){. 
1f52f 20 20 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69     do {..      i
1f530 66 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20  f( !*zCsr ){.   
1f531 20 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20       /* Ran out 
1f532 6f 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20  of input before 
1f533 66 69 6e 64 69 6e 67 20 74 68 65 20 74 61 62 6c  finding the tabl
1f534 65 20 6e 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e  e name. Return N
1f535 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ULL. */.        
1f536 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
1f537 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20  .      /* Store 
1f538 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a  the token that z
1f539 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e  Csr points to in
1f53a 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20   tname. */.     
1f53b 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72 3b   tname.z = zCsr;
1f53c 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d  .      tname.n =
1f53d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   len;..      /* 
1f53e 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f 20  Advance zCsr to 
1f53f 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20  the next token. 
1f540 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 6e  Store that token
1f541 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27   type in 'token'
1f542 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 69  ,.      ** and i
1f543 74 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65  ts length in 'le
1f544 6e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20 6e  n' (to be used n
1f545 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  ext iteration of
1f546 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20   this loop)..   
1f547 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b     */.      do {
1f548 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d  .        zCsr +=
1f549 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65   len;.        le
1f54a 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f  n = sqlite3GetTo
1f54b 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e  ken(zCsr, &token
1f54c 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28  );.      }while(
1f54d 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45   token==TK_SPACE
1f54e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1f54f 28 20 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20  ( len>0 );..    
1f550 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 27 64    /* Variable 'd
1f551 69 73 74 27 20 73 74 6f 72 65 73 20 74 68 65 20  ist' stores the 
1f552 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73  number of tokens
1f553 20 72 65 61 64 20 73 69 6e 63 65 20 74 68 65 20   read since the 
1f554 6d 6f 73 74 0a 20 20 20 20 20 20 2a 2a 20 72 65  most.      ** re
1f555 63 65 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54  cent TK_DOT or T
1f556 4b 5f 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73  K_ON. This means
1f557 20 74 68 61 74 20 77 68 65 6e 20 61 20 57 48 45   that when a WHE
1f558 4e 2c 20 46 4f 52 20 6f 72 20 42 45 47 49 4e 20  N, FOR or BEGIN 
1f559 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20  .      ** token 
1f55a 69 73 20 72 65 61 64 20 61 6e 64 20 27 64 69 73  is read and 'dis
1f55b 74 27 20 65 71 75 61 6c 73 20 32 2c 20 74 68 65  t' equals 2, the
1f55c 20 63 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74 65   condition state
1f55d 64 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a  d above.      **
1f55e 20 74 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20 20   to be met..    
1f55f 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f    **.      ** No
1f560 74 65 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f  te that ON canno
1f561 74 20 62 65 20 61 20 64 61 74 61 62 61 73 65 2c  t be a database,
1f562 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e   table or column
1f563 20 6e 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20   name, so.      
1f564 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e  ** there is no n
1f565 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f  eed to worry abo
1f566 75 74 20 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a  ut syntax like .
1f567 20 20 20 20 20 20 2a 2a 20 22 43 52 45 41 54 45        ** "CREATE
1f568 20 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20   TRIGGER ... ON 
1f569 4f 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22  ON.ON BEGIN ..."
1f56a 20 65 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20   etc..      */. 
1f56b 20 20 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20       dist++;.   
1f56c 20 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b     if( token==TK
1f56d 5f 44 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54  _DOT || token==T
1f56e 4b 5f 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  K_ON ){.        
1f56f 64 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  dist = 0;.      
1f570 7d 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 64  }.    } while( d
1f571 69 73 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e  ist!=2 || (token
1f572 21 3d 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b  !=TK_WHEN && tok
1f573 65 6e 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f  en!=TK_FOR && to
1f574 6b 65 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29  ken!=TK_BEGIN) )
1f575 3b 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62  ;..    /* Variab
1f576 6c 65 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e  le tname now con
1f577 74 61 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20  tains the token 
1f578 74 68 61 74 20 69 73 20 74 68 65 20 6f 6c 64 20  that is the old 
1f579 74 61 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a  table-name.    *
1f57a 2a 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20  * in the CREATE 
1f57b 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e  TRIGGER statemen
1f57c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52  t..    */.    zR
1f57d 65 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  et = sqlite3MPri
1f57e 6e 74 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25  ntf(db, "%.*s\"%
1f57f 77 5c 22 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20  w\"%s", tname.z 
1f580 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20  - zSql, zSql, . 
1f581 20 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65        zTableName
1f582 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e  , tname.z+tname.
1f583 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  n);.    sqlite3_
1f584 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
1f585 65 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53  ext, zRet, -1, S
1f586 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a  QLITE_DYNAMIC);.
1f587 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f    }.}.#endif   /
1f588 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  * !SQLITE_OMIT_T
1f589 52 49 47 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  RIGGER */../*.**
1f58a 20 52 65 67 69 73 74 65 72 20 62 75 69 6c 74 2d   Register built-
1f58b 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  in functions use
1f58c 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d  d to help implem
1f58d 65 6e 74 20 41 4c 54 45 52 20 54 41 42 4c 45 0a  ent ALTER TABLE.
1f58e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f58f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
1f590 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c  terFunctions(sql
1f591 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c  ite3 *db){.  sql
1f592 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
1f593 62 2c 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d  b, "sqlite_renam
1f594 65 5f 74 61 62 6c 65 22 2c 20 32 2c 20 53 51 4c  e_table", 2, SQL
1f595 49 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20  ITE_UTF8, 0,.   
1f596 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f597 20 20 20 20 20 20 72 65 6e 61 6d 65 54 61 62 6c        renameTabl
1f598 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 69  eFunc, 0, 0);.#i
1f599 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f59a 54 5f 54 52 49 47 47 45 52 0a 20 20 73 71 6c 69  T_TRIGGER.  sqli
1f59b 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
1f59c 2c 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65  , "sqlite_rename
1f59d 5f 74 72 69 67 67 65 72 22 2c 20 32 2c 20 53 51  _trigger", 2, SQ
1f59e 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20  LITE_UTF8, 0,.  
1f59f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5a0 20 20 20 20 20 20 20 72 65 6e 61 6d 65 54 72 69         renameTri
1f5a1 67 67 65 72 46 75 6e 63 2c 20 30 2c 20 30 29 3b  ggerFunc, 0, 0);
1f5a2 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
1f5a3 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65   Generate the te
1f5a4 78 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78  xt of a WHERE ex
1f5a5 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63  pression which c
1f5a6 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
1f5a7 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70  lect all.** temp
1f5a8 6f 72 61 72 79 20 74 72 69 67 67 65 72 73 20 6f  orary triggers o
1f5a9 6e 20 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f  n table pTab fro
1f5aa 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d  m the sqlite_tem
1f5ab 70 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20  p_master table. 
1f5ac 49 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62  If.** table pTab
1f5ad 20 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72   has no temporar
1f5ae 79 20 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69  y triggers, or i
1f5af 73 20 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20  s itself stored 
1f5b0 69 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f  in the .** tempo
1f5b1 72 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e  rary database, N
1f5b2 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
1f5b3 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
1f5b4 2a 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65  *whereTempTrigge
1f5b5 72 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  rs(Parse *pParse
1f5b6 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
1f5b7 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
1f5b8 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65  ;.  char *zWhere
1f5b9 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 74 6d   = 0;.  char *tm
1f5ba 70 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53  p = 0;.  const S
1f5bb 63 68 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65  chema *pTempSche
1f5bc 6d 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d  ma = pParse->db-
1f5bd 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b  >aDb[1].pSchema;
1f5be 20 2f 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65   /* Temp db sche
1f5bf 6d 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  ma */..  /* If t
1f5c0 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  he table is not 
1f5c1 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 74  located in the t
1f5c2 65 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68  emp-db (in which
1f5c3 20 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20   case NULL is . 
1f5c4 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f   ** returned, lo
1f5c5 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 74  op through the t
1f5c6 61 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72  ables list of tr
1f5c7 69 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68  iggers. For each
1f5c8 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68   trigger.  ** th
1f5c9 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  at is not part o
1f5ca 66 20 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63  f the temp-db sc
1f5cb 68 65 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75  hema, add a clau
1f5cc 73 65 20 74 6f 20 74 68 65 20 57 48 45 52 45 20  se to the WHERE 
1f5cd 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  .  ** expression
1f5ce 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20   being built up 
1f5cf 69 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a  in zWhere..  */.
1f5d0 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68    if( pTab->pSch
1f5d1 65 6d 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61  ema!=pTempSchema
1f5d2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20   ){.    sqlite3 
1f5d3 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1f5d4 3b 0a 20 20 20 20 66 6f 72 28 20 70 54 72 69 67  ;.    for( pTrig
1f5d5 3d 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b  =pTab->pTrigger;
1f5d6 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54   pTrig; pTrig=pT
1f5d7 72 69 67 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  rig->pNext ){.  
1f5d8 20 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70      if( pTrig->p
1f5d9 53 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68  Schema==pTempSch
1f5da 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ema ){.        i
1f5db 66 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20  f( !zWhere ){.  
1f5dc 20 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d          zWhere =
1f5dd 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1f5de 64 62 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 70  db, "name=%Q", p
1f5df 54 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20  Trig->name);.   
1f5e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1f5e1 20 20 20 20 20 20 74 6d 70 20 3d 20 7a 57 68 65        tmp = zWhe
1f5e2 72 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 57  re;.          zW
1f5e3 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  here = sqlite3MP
1f5e4 72 69 6e 74 66 28 64 62 2c 20 22 25 73 20 4f 52  rintf(db, "%s OR
1f5e5 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65 72   name=%Q", zWher
1f5e6 65 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 29 3b  e, pTrig->name);
1f5e7 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1f5e8 65 33 44 62 46 72 65 65 28 64 62 2c 20 74 6d 70  e3DbFree(db, tmp
1f5e9 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1f5ea 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1f5eb 20 72 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a   return zWhere;.
1f5ec 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
1f5ed 65 20 63 6f 64 65 20 74 6f 20 64 72 6f 70 20 61  e code to drop a
1f5ee 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e  nd reload the in
1f5ef 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74  ternal represent
1f5f0 61 74 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a 2a  ation of table.*
1f5f1 2a 20 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20  * pTab from the 
1f5f2 64 61 74 61 62 61 73 65 2c 20 69 6e 63 6c 75 64  database, includ
1f5f3 69 6e 67 20 74 72 69 67 67 65 72 73 20 61 6e 64  ing triggers and
1f5f4 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67   temporary trigg
1f5f5 65 72 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ers..** Argument
1f5f6 20 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61   zName is the na
1f5f7 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1f5f8 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1f5f9 73 63 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68 65  schema at.** the
1f5fa 20 74 69 6d 65 20 74 68 65 20 67 65 6e 65 72 61   time the genera
1f5fb 74 65 64 20 63 6f 64 65 20 69 73 20 65 78 65 63  ted code is exec
1f5fc 75 74 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62  uted. This can b
1f5fd 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
1f5fe 0a 2a 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20  .** pTab->zName 
1f5ff 69 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  if this function
1f600 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64   is being called
1f601 20 74 6f 20 63 6f 64 65 20 70 61 72 74 20 6f 66   to code part of
1f602 20 61 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20 54   an .** "ALTER T
1f603 41 42 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22 20  ABLE RENAME TO" 
1f604 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74  statement..*/.st
1f605 61 74 69 63 20 76 6f 69 64 20 72 65 6c 6f 61 64  atic void reload
1f606 54 61 62 6c 65 53 63 68 65 6d 61 28 50 61 72 73  TableSchema(Pars
1f607 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
1f608 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68   *pTab, const ch
1f609 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64  ar *zName){.  Vd
1f60a 62 65 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a 7a  be *v;.  char *z
1f60b 57 68 65 72 65 3b 0a 20 20 69 6e 74 20 69 44 62  Where;.  int iDb
1f60c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1f60d 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
1f60e 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
1f60f 69 6e 67 20 70 54 61 62 20 2a 2f 0a 23 69 66 6e  ing pTab */.#ifn
1f610 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f611 54 52 49 47 47 45 52 0a 20 20 54 72 69 67 67 65  TRIGGER.  Trigge
1f612 72 20 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69 66  r *pTrig;.#endif
1f613 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
1f614 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1f615 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75 72    if( !v ) retur
1f616 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  n;.  assert( sql
1f617 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
1f618 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 2d  lMutexes(pParse-
1f619 3e 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20  >db) );.  iDb = 
1f61a 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1f61b 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
1f61c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
1f61d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
1f61e 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  0 );..#ifndef SQ
1f61f 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1f620 52 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6e 79 20  R.  /* Drop any 
1f621 74 61 62 6c 65 20 74 72 69 67 67 65 72 73 20 66  table triggers f
1f622 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  rom the internal
1f623 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 66 6f   schema. */.  fo
1f624 72 28 70 54 72 69 67 3d 70 54 61 62 2d 3e 70 54  r(pTrig=pTab->pT
1f625 72 69 67 67 65 72 3b 20 70 54 72 69 67 3b 20 70  rigger; pTrig; p
1f626 54 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78  Trig=pTrig->pNex
1f627 74 29 7b 0a 20 20 20 20 69 6e 74 20 69 54 72 69  t){.    int iTri
1f628 67 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  gDb = sqlite3Sch
1f629 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
1f62a 65 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53  e->db, pTrig->pS
1f62b 63 68 65 6d 61 29 3b 0a 20 20 20 20 61 73 73 65  chema);.    asse
1f62c 72 74 28 20 69 54 72 69 67 44 62 3d 3d 69 44 62  rt( iTrigDb==iDb
1f62d 20 7c 7c 20 69 54 72 69 67 44 62 3d 3d 31 20 29   || iTrigDb==1 )
1f62e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1f62f 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72  eAddOp4(v, OP_Dr
1f630 6f 70 54 72 69 67 67 65 72 2c 20 69 54 72 69 67  opTrigger, iTrig
1f631 44 62 2c 20 30 2c 20 30 2c 20 70 54 72 69 67 2d  Db, 0, 0, pTrig-
1f632 3e 6e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 23  >name, 0);.  }.#
1f633 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70  endif..  /* Drop
1f634 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69   the table and i
1f635 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e  ndex from the in
1f636 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 2a 2f  ternal schema */
1f637 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1f638 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54  dOp4(v, OP_DropT
1f639 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c  able, iDb, 0, 0,
1f63a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pTab->zName, 0)
1f63b 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74  ;..  /* Reload t
1f63c 68 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 20  he table, index 
1f63d 61 6e 64 20 70 65 72 6d 61 6e 65 6e 74 20 74 72  and permanent tr
1f63e 69 67 67 65 72 20 73 63 68 65 6d 61 73 2e 20 2a  igger schemas. *
1f63f 2f 0a 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c  /.  zWhere = sql
1f640 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72  ite3MPrintf(pPar
1f641 73 65 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e 61 6d  se->db, "tbl_nam
1f642 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  e=%Q", zName);. 
1f643 20 69 66 28 20 21 7a 57 68 65 72 65 20 29 20 72   if( !zWhere ) r
1f644 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
1f645 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1f646 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44  _ParseSchema, iD
1f647 62 2c 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c  b, 0, 0, zWhere,
1f648 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 0a 23   P4_DYNAMIC);..#
1f649 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f64a 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20  IT_TRIGGER.  /* 
1f64b 4e 6f 77 2c 20 69 66 20 74 68 65 20 74 61 62 6c  Now, if the tabl
1f64c 65 20 69 73 20 6e 6f 74 20 73 74 6f 72 65 64 20  e is not stored 
1f64d 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61 74 61  in the temp data
1f64e 62 61 73 65 2c 20 72 65 6c 6f 61 64 20 61 6e 79  base, reload any
1f64f 20 74 65 6d 70 20 0a 20 20 2a 2a 20 74 72 69 67   temp .  ** trig
1f650 67 65 72 73 2e 20 44 6f 6e 27 74 20 75 73 65 20  gers. Don't use 
1f651 49 4e 28 2e 2e 2e 29 20 69 6e 20 63 61 73 65 20  IN(...) in case 
1f652 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
1f653 55 45 52 59 20 69 73 20 64 65 66 69 6e 65 64 2e  UERY is defined.
1f654 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57   .  */.  if( (zW
1f655 68 65 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72  here=whereTempTr
1f656 69 67 67 65 72 73 28 70 50 61 72 73 65 2c 20 70  iggers(pParse, p
1f657 54 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  Tab))!=0 ){.    
1f658 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f659 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68  4(v, OP_ParseSch
1f65a 65 6d 61 2c 20 31 2c 20 30 2c 20 30 2c 20 7a 57  ema, 1, 0, 0, zW
1f65b 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43  here, P4_DYNAMIC
1f65c 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  );.  }.#endif.}.
1f65d 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1f65e 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  code to implemen
1f65f 74 20 74 68 65 20 22 41 4c 54 45 52 20 54 41 42  t the "ALTER TAB
1f660 4c 45 20 78 78 78 20 52 45 4e 41 4d 45 20 54 4f  LE xxx RENAME TO
1f661 20 79 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d 61 6e   yyy" .** comman
1f662 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  d. .*/.SQLITE_PR
1f663 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1f664 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62  e3AlterRenameTab
1f665 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  le(.  Parse *pPa
1f666 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
1f667 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
1f668 74 2e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  t. */.  SrcList 
1f669 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 20 20  *pSrc,          
1f66a 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74    /* The table t
1f66b 6f 20 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 54  o rename. */.  T
1f66c 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20  oken *pName     
1f66d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1f66e 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  new table name. 
1f66f 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b  */.){.  int iDb;
1f670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f671 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 68    /* Database th
1f672 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
1f673 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20  table */.  char 
1f674 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20  *zDb;           
1f675 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1f676 64 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a  database iDb */.
1f677 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
1f678 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1f679 61 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d  able being renam
1f67a 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e  ed */.  char *zN
1f67b 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ame = 0;        
1f67c 20 20 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e    /* NULL-termin
1f67d 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  ated version of 
1f67e 70 4e 61 6d 65 20 2a 2f 20 0a 20 20 73 71 6c 69  pName */ .  sqli
1f67f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1f680 2d 3e 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73  ->db; /* Databas
1f681 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
1f682 20 20 69 6e 74 20 6e 54 61 62 4e 61 6d 65 3b 20    int nTabName; 
1f683 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1f684 75 6d 62 65 72 20 6f 66 20 55 54 46 2d 38 20 63  umber of UTF-8 c
1f685 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 54 61  haracters in zTa
1f686 62 4e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  bName */.  const
1f687 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 3b   char *zTabName;
1f688 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c       /* Original
1f689 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
1f68a 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  le */.  Vdbe *v;
1f68b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f68c 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 63  OMIT_TRIGGER.  c
1f68d 68 61 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b  har *zWhere = 0;
1f68e 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72           /* Wher
1f68f 65 20 63 6c 61 75 73 65 20 74 6f 20 6c 6f 63 61  e clause to loca
1f690 74 65 20 74 65 6d 70 20 74 72 69 67 67 65 72 73  te temp triggers
1f691 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74   */.#endif.  int
1f692 20 69 73 56 69 72 74 75 61 6c 52 65 6e 61 6d 65   isVirtualRename
1f693 20 3d 20 30 3b 20 20 2f 2a 20 54 72 75 65 20 69   = 0;  /* True i
1f694 66 20 74 68 69 73 20 69 73 20 61 20 76 2d 74 61  f this is a v-ta
1f695 62 6c 65 20 77 69 74 68 20 61 6e 20 78 52 65 6e  ble with an xRen
1f696 61 6d 65 28 29 20 2a 2f 0a 20 20 0a 20 20 69 66  ame() */.  .  if
1f697 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
1f698 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72  ed ) goto exit_r
1f699 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 61  ename_table;.  a
1f69a 73 73 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 72  ssert( pSrc->nSr
1f69b 63 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74  c==1 );.  assert
1f69c 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
1f69d 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50  ldsAllMutexes(pP
1f69e 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 0a 20 20  arse->db) );..  
1f69f 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pTab = sqlite3Lo
1f6a0 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
1f6a1 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e  , 0, pSrc->a[0].
1f6a2 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b 30  zName, pSrc->a[0
1f6a3 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ].zDatabase);.  
1f6a4 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f  if( !pTab ) goto
1f6a5 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
1f6a6 6c 65 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69  le;.  iDb = sqli
1f6a7 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
1f6a8 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
1f6a9 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a  b->pSchema);.  z
1f6aa 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
1f6ab 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 47  ].zName;..  /* G
1f6ac 65 74 20 61 20 4e 55 4c 4c 20 74 65 72 6d 69 6e  et a NULL termin
1f6ad 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  ated version of 
1f6ae 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61  the new table na
1f6af 6d 65 2e 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d  me. */.  zName =
1f6b0 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
1f6b1 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29  Token(db, pName)
1f6b2 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29  ;.  if( !zName )
1f6b3 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
1f6b4 65 5f 74 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43  e_table;..  /* C
1f6b5 68 65 63 6b 20 74 68 61 74 20 61 20 74 61 62 6c  heck that a tabl
1f6b6 65 20 6f 72 20 69 6e 64 65 78 20 6e 61 6d 65 64  e or index named
1f6b7 20 27 7a 4e 61 6d 65 27 20 64 6f 65 73 20 6e 6f   'zName' does no
1f6b8 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 0a  t already exist.
1f6b9 20 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65    ** in database
1f6ba 20 69 44 62 2e 20 49 66 20 73 6f 2c 20 74 68 69   iDb. If so, thi
1f6bb 73 20 69 73 20 61 6e 20 65 72 72 6f 72 2e 0a 20  s is an error.. 
1f6bc 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
1f6bd 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
1f6be 4e 61 6d 65 2c 20 7a 44 62 29 20 7c 7c 20 73 71  Name, zDb) || sq
1f6bf 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
1f6c0 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29  b, zName, zDb) )
1f6c1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1f6c2 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
1f6c3 20 20 20 20 20 20 20 22 74 68 65 72 65 20 69 73         "there is
1f6c4 20 61 6c 72 65 61 64 79 20 61 6e 6f 74 68 65 72   already another
1f6c5 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1f6c6 77 69 74 68 20 74 68 69 73 20 6e 61 6d 65 3a 20  with this name: 
1f6c7 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  %s", zName);.   
1f6c8 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
1f6c9 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20  e_table;.  }..  
1f6ca 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20  /* Make sure it 
1f6cb 69 73 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20  is not a system 
1f6cc 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65  table being alte
1f6cd 72 65 64 2c 20 6f 72 20 61 20 72 65 73 65 72 76  red, or a reserv
1f6ce 65 64 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 68 61  ed name.  ** tha
1f6cf 74 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 62  t the table is b
1f6d0 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 74 6f 2e  eing renamed to.
1f6d1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
1f6d2 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61 62  te3Strlen30(pTab
1f6d3 2d 3e 7a 4e 61 6d 65 29 3e 36 20 0a 20 20 20 26  ->zName)>6 .   &
1f6d4 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e  & 0==sqlite3StrN
1f6d5 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65  ICmp(pTab->zName
1f6d6 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 0a  , "sqlite_", 7).
1f6d7 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33    ){.    sqlite3
1f6d8 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1f6d9 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e   "table %s may n
1f6da 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20  ot be altered", 
1f6db 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
1f6dc 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61    goto exit_rena
1f6dd 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20  me_table;.  }.  
1f6de 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
1f6df 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63  qlite3CheckObjec
1f6e0 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e  tName(pParse, zN
1f6e1 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ame) ){.    goto
1f6e2 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
1f6e3 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  le;.  }..#ifndef
1f6e4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
1f6e5 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53  W.  if( pTab->pS
1f6e6 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c  elect ){.    sql
1f6e7 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1f6e8 72 73 65 2c 20 22 76 69 65 77 20 25 73 20 6d 61  rse, "view %s ma
1f6e9 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64  y not be altered
1f6ea 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
1f6eb 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72  .    goto exit_r
1f6ec 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d  ename_table;.  }
1f6ed 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
1f6ee 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1f6ef 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20  HORIZATION.  /* 
1f6f0 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f  Invoke the autho
1f6f1 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
1f6f2 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  k. */.  if( sqli
1f6f3 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
1f6f4 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45  rse, SQLITE_ALTE
1f6f5 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54  R_TABLE, zDb, pT
1f6f6 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b  ab->zName, 0) ){
1f6f7 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72  .    goto exit_r
1f6f8 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d  ename_table;.  }
1f6f9 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
1f6fa 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1f6fb 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
1f6fc 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
1f6fd 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
1f6fe 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67  , pTab) ){.    g
1f6ff 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f  oto exit_rename_
1f700 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28  table;.  }.  if(
1f701 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
1f702 20 26 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e   && pTab->pMod->
1f703 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65  pModule->xRename
1f704 20 29 7b 0a 20 20 20 20 69 73 56 69 72 74 75 61   ){.    isVirtua
1f705 6c 52 65 6e 61 6d 65 20 3d 20 31 3b 0a 20 20 7d  lRename = 1;.  }
1f706 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65  .#endif..  /* Be
1f707 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  gin a transactio
1f708 6e 20 61 6e 64 20 63 6f 64 65 20 74 68 65 20 56  n and code the V
1f709 65 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72 20  erifyCookie for 
1f70a 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a 20  database iDb. . 
1f70b 20 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79 20   ** Then modify 
1f70c 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
1f70d 65 20 28 73 69 6e 63 65 20 74 68 65 20 41 4c 54  e (since the ALT
1f70e 45 52 20 54 41 42 4c 45 20 6d 6f 64 69 66 69 65  ER TABLE modifie
1f70f 73 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d  s the.  ** schem
1f710 61 29 2e 20 4f 70 65 6e 20 61 20 73 74 61 74 65  a). Open a state
1f711 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1f712 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   if the table is
1f713 20 61 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20   a virtual.  ** 
1f714 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 20  table..  */.  v 
1f715 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1f716 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
1f717 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f  v==0 ){.    goto
1f718 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
1f719 6c 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  le;.  }.  sqlite
1f71a 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
1f71b 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 69 73 56  tion(pParse, isV
1f71c 69 72 74 75 61 6c 52 65 6e 61 6d 65 2c 20 69 44  irtualRename, iD
1f71d 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61  b);.  sqlite3Cha
1f71e 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
1f71f 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49 66  , iDb);..  /* If
1f720 20 74 68 69 73 20 69 73 20 61 20 76 69 72 74 75   this is a virtu
1f721 61 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b 65  al table, invoke
1f722 20 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20 66   the xRename() f
1f723 75 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a 20  unction if.  ** 
1f724 6f 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e 20  one is defined. 
1f725 54 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63 61  The xRename() ca
1f726 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64 69  llback will modi
1f727 66 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a  fy the names.  *
1f728 2a 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72 63  * of any resourc
1f729 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76  es used by the v
1f72a 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  -table implement
1f72b 61 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67  ation (including
1f72c 20 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c 69   other.  ** SQLi
1f72d 74 65 20 74 61 62 6c 65 73 29 20 74 68 61 74 20  te tables) that 
1f72e 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 62  are identified b
1f72f 79 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  y the name of th
1f730 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
1f731 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
1f732 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1f733 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 69 73 56  LTABLE.  if( isV
1f734 69 72 74 75 61 6c 52 65 6e 61 6d 65 20 29 7b 0a  irtualRename ){.
1f735 20 20 20 20 69 6e 74 20 69 20 3d 20 2b 2b 70 50      int i = ++pP
1f736 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
1f737 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f738 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
1f739 20 30 2c 20 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c   0, i, 0, zName,
1f73a 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
1f73b 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1f73c 5f 56 52 65 6e 61 6d 65 2c 20 69 2c 20 30 2c 20  _VRename, i, 0, 
1f73d 30 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  0,(const char*)p
1f73e 54 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f 56  Tab->pVtab, P4_V
1f73f 54 41 42 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  TAB);.  }.#endif
1f740 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f 75  ..  /* figure ou
1f741 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d 38  t how many UTF-8
1f742 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20   characters are 
1f743 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54  in zName */.  zT
1f744 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a  abName = pTab->z
1f745 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d 65  Name;.  nTabName
1f746 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 68   = sqlite3Utf8Ch
1f747 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c 20  arLen(zTabName, 
1f748 2d 31 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66  -1);..  /* Modif
1f749 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  y the sqlite_mas
1f74a 74 65 72 20 74 61 62 6c 65 20 74 6f 20 75 73 65  ter table to use
1f74b 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   the new table n
1f74c 61 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ame. */.  sqlite
1f74d 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
1f74e 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41  rse,.      "UPDA
1f74f 54 45 20 25 51 2e 25 73 20 53 45 54 20 22 0a 23  TE %Q.%s SET ".#
1f750 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1f751 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20  T_TRIGGER.      
1f752 20 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74      "sql = sqlit
1f753 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73  e_rename_table(s
1f754 71 6c 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73 65  ql, %Q), ".#else
1f755 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20  .          "sql 
1f756 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 20  = CASE ".       
1f757 20 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 20       "WHEN type 
1f758 3d 20 27 74 72 69 67 67 65 72 27 20 54 48 45 4e  = 'trigger' THEN
1f759 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74   sqlite_rename_t
1f75a 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 22  rigger(sql, %Q)"
1f75b 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c  .            "EL
1f75c 53 45 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65  SE sqlite_rename
1f75d 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 20  _table(sql, %Q) 
1f75e 45 4e 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20 20  END, ".#endif.  
1f75f 20 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d          "tbl_nam
1f760 65 20 3d 20 25 51 2c 20 22 0a 20 20 20 20 20 20  e = %Q, ".      
1f761 20 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 45      "name = CASE
1f762 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
1f763 57 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c 65  WHEN type='table
1f764 27 20 54 48 45 4e 20 25 51 20 22 0a 20 20 20 20  ' THEN %Q ".    
1f765 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e 61          "WHEN na
1f766 6d 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f  me LIKE 'sqlite_
1f767 61 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e 44  autoindex%%' AND
1f768 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54 48   type='index' TH
1f769 45 4e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  EN ".           
1f76a 20 20 22 27 73 71 6c 69 74 65 5f 61 75 74 6f 69    "'sqlite_autoi
1f76b 6e 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c 20  ndex_' || %Q || 
1f76c 73 75 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b 31  substr(name,%d+1
1f76d 38 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  8) ".           
1f76e 20 22 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44 20   "ELSE name END 
1f76f 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74  ".      "WHERE t
1f770 62 6c 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20 22  bl_name=%Q AND "
1f771 0a 20 20 20 20 20 20 20 20 20 20 22 28 74 79 70  .          "(typ
1f772 65 3d 27 74 61 62 6c 65 27 20 4f 52 20 74 79 70  e='table' OR typ
1f773 65 3d 27 69 6e 64 65 78 27 20 4f 52 20 74 79 70  e='index' OR typ
1f774 65 3d 27 74 72 69 67 67 65 72 27 29 3b 22 2c 20  e='trigger');", 
1f775 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45  .      zDb, SCHE
1f776 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a  MA_TABLE(iDb), z
1f777 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61  Name, zName, zNa
1f778 6d 65 2c 20 0a 23 69 66 6e 64 65 66 20 53 51 4c  me, .#ifndef SQL
1f779 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1f77a 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 65  .      zName,.#e
1f77b 6e 64 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d 65  ndif.      zName
1f77c 2c 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62  , nTabName, zTab
1f77d 4e 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64  Name.  );..#ifnd
1f77e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1f77f 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f  UTOINCREMENT.  /
1f780 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f  * If the sqlite_
1f781 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65  sequence table e
1f782 78 69 73 74 73 20 69 6e 20 74 68 69 73 20 64 61  xists in this da
1f783 74 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64  tabase, then upd
1f784 61 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 74  ate .  ** it wit
1f785 68 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  h the new table 
1f786 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  name..  */.  if(
1f787 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
1f788 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 65  e(db, "sqlite_se
1f789 71 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b  quence", zDb) ){
1f78a 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
1f78b 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
1f78c 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20          "UPDATE 
1f78d 5c 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 65  \"%w\".sqlite_se
1f78e 71 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 20  quence set name 
1f78f 3d 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65 20  = %Q WHERE name 
1f790 3d 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20 7a  = %Q",.        z
1f791 44 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  Db, zName, pTab-
1f792 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e  >zName);.  }.#en
1f793 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
1f794 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1f795 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
1f796 72 65 20 54 45 4d 50 20 74 72 69 67 67 65 72 73  re TEMP triggers
1f797 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20   on this table, 
1f798 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74  modify the sqlit
1f799 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20 20  e_temp_master.  
1f79a 2a 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74 20  ** table. Don't 
1f79b 64 6f 20 74 68 69 73 20 69 66 20 74 68 65 20 74  do this if the t
1f79c 61 62 6c 65 20 62 65 69 6e 67 20 41 4c 54 45 52  able being ALTER
1f79d 65 64 20 69 73 20 69 74 73 65 6c 66 20 6c 6f 63  ed is itself loc
1f79e 61 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68 65  ated in.  ** the
1f79f 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 0a   temp database..
1f7a0 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65    */.  if( (zWhe
1f7a1 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67  re=whereTempTrig
1f7a2 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61  gers(pParse, pTa
1f7a3 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  b))!=0 ){.    sq
1f7a4 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
1f7a5 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
1f7a6 20 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65    "UPDATE sqlite
1f7a7 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54  _temp_master SET
1f7a8 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
1f7a9 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e  sql = sqlite_ren
1f7aa 61 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c  ame_trigger(sql,
1f7ab 20 25 51 29 2c 20 22 0a 20 20 20 20 20 20 20 20   %Q), ".        
1f7ac 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20      "tbl_name = 
1f7ad 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  %Q ".           
1f7ae 20 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a 4e   "WHERE %s;", zN
1f7af 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65  ame, zName, zWhe
1f7b0 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  re);.    sqlite3
1f7b1 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72  DbFree(db, zWher
1f7b2 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  e);.  }.#endif..
1f7b3 20 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72 65    /* Drop and re
1f7b4 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61  load the interna
1f7b5 6c 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 20  l table schema. 
1f7b6 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65  */.  reloadTable
1f7b7 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70  Schema(pParse, p
1f7b8 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78  Tab, zName);..ex
1f7b9 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3a  it_rename_table:
1f7ba 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
1f7bb 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63  tDelete(db, pSrc
1f7bc 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1f7bd 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d  ee(db, zName);.}
1f7be 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
1f7bf 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1f7c0 20 61 66 74 65 72 20 61 6e 20 22 41 4c 54 45 52   after an "ALTER
1f7c1 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20   TABLE ... ADD" 
1f7c2 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73  statement.** has
1f7c3 20 62 65 65 6e 20 70 61 72 73 65 64 2e 20 41 72   been parsed. Ar
1f7c4 67 75 6d 65 6e 74 20 70 43 6f 6c 44 65 66 20 63  gument pColDef c
1f7c5 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78 74  ontains the text
1f7c6 20 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63   of the new.** c
1f7c7 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
1f7c8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c  ..**.** The Tabl
1f7c9 65 20 73 74 72 75 63 74 75 72 65 20 70 50 61 72  e structure pPar
1f7ca 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 77 61  se->pNewTable wa
1f7cb 73 20 65 78 74 65 6e 64 65 64 20 74 6f 20 69 6e  s extended to in
1f7cc 63 6c 75 64 65 0a 2a 2a 20 74 68 65 20 6e 65 77  clude.** the new
1f7cd 20 63 6f 6c 75 6d 6e 20 64 75 72 69 6e 67 20 70   column during p
1f7ce 61 72 73 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  arsing..*/.SQLIT
1f7cf 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1f7d0 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73  qlite3AlterFinis
1f7d1 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65  hAddColumn(Parse
1f7d2 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
1f7d3 2a 70 43 6f 6c 44 65 66 29 7b 0a 20 20 54 61 62  *pColDef){.  Tab
1f7d4 6c 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20  le *pNew;       
1f7d5 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
1f7d6 66 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  f pParse->pNewTa
1f7d7 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ble */.  Table *
1f7d8 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
1f7d9 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e     /* Table bein
1f7da 67 20 61 6c 74 65 72 65 64 20 2a 2f 0a 20 20 69  g altered */.  i
1f7db 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
1f7dc 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1f7dd 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  base number */. 
1f7de 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
1f7df 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
1f7e0 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20  tabase name */. 
1f7e1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
1f7e2 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  b;         /* Ta
1f7e3 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68  ble name */.  ch
1f7e4 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20  ar *zCol;       
1f7e5 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d          /* Null-
1f7e6 74 65 72 6d 69 6e 61 74 65 64 20 63 6f 6c 75 6d  terminated colum
1f7e7 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a  n definition */.
1f7e8 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20    Column *pCol; 
1f7e9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1f7ea 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f  he new column */
1f7eb 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20  .  Expr *pDflt; 
1f7ec 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f7ed 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f  Default value fo
1f7ee 72 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  r the new column
1f7ef 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
1f7f0 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
1f7f1 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1f7f2 63 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0a 0a  connection; */..
1f7f3 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
1f7f4 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  b;.  if( pParse-
1f7f5 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
1f7f6 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
1f7f7 72 6e 3b 0a 20 20 70 4e 65 77 20 3d 20 70 50 61  rn;.  pNew = pPa
1f7f8 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
1f7f9 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 20 29    assert( pNew )
1f7fa 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1f7fb 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
1f7fc 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a  lMutexes(db) );.
1f7fd 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
1f7fe 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
1f7ff 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 29 3b   pNew->pSchema);
1f800 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  .  zDb = db->aDb
1f801 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7a  [iDb].zName;.  z
1f802 54 61 62 20 3d 20 70 4e 65 77 2d 3e 7a 4e 61 6d  Tab = pNew->zNam
1f803 65 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 4e 65  e;.  pCol = &pNe
1f804 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43  w->aCol[pNew->nC
1f805 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66 6c 74 20 3d  ol-1];.  pDflt =
1f806 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20   pCol->pDflt;.  
1f807 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
1f808 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
1f809 2c 20 7a 44 62 29 3b 0a 20 20 61 73 73 65 72 74  , zDb);.  assert
1f80a 28 20 70 54 61 62 20 29 3b 0a 0a 23 69 66 6e 64  ( pTab );..#ifnd
1f80b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1f80c 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f  UTHORIZATION.  /
1f80d 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74  * Invoke the aut
1f80e 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
1f80f 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71  ack. */.  if( sq
1f810 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
1f811 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c  Parse, SQLITE_AL
1f812 54 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20  TER_TABLE, zDb, 
1f813 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20  pTab->zName, 0) 
1f814 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
1f815 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
1f816 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  If the default v
1f817 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77  alue for the new
1f818 20 63 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65 63   column was spec
1f819 69 66 69 65 64 20 77 69 74 68 20 61 20 0a 20 20  ified with a .  
1f81a 2a 2a 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c  ** literal NULL,
1f81b 20 74 68 65 6e 20 73 65 74 20 70 44 66 6c 74 20   then set pDflt 
1f81c 74 6f 20 30 2e 20 54 68 69 73 20 73 69 6d 70 6c  to 0. This simpl
1f81d 69 66 69 65 73 20 63 68 65 63 6b 69 6e 67 0a 20  ifies checking. 
1f81e 20 2a 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20 4e   ** for an SQL N
1f81f 55 4c 4c 20 64 65 66 61 75 6c 74 20 62 65 6c 6f  ULL default belo
1f820 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44  w..  */.  if( pD
1f821 66 6c 74 20 26 26 20 70 44 66 6c 74 2d 3e 6f 70  flt && pDflt->op
1f822 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20  ==TK_NULL ){.   
1f823 20 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a   pDflt = 0;.  }.
1f824 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
1f825 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20   the new column 
1f826 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64  is not specified
1f827 20 61 73 20 50 52 49 4d 41 52 59 20 4b 45 59 20   as PRIMARY KEY 
1f828 6f 72 20 55 4e 49 51 55 45 2e 0a 20 20 2a 2a 20  or UNIQUE..  ** 
1f829 49 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f  If there is a NO
1f82a 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
1f82b 74 2c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61  t, then the defa
1f82c 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ult value for th
1f82d 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75  e.  ** column mu
1f82e 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a  st not be NULL..
1f82f 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 2d    */.  if( pCol-
1f830 3e 69 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20  >isPrimKey ){.  
1f831 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1f832 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f  g(pParse, "Canno
1f833 74 20 61 64 64 20 61 20 50 52 49 4d 41 52 59 20  t add a PRIMARY 
1f834 4b 45 59 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20  KEY column");.  
1f835 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
1f836 69 66 28 20 70 4e 65 77 2d 3e 70 49 6e 64 65 78  if( pNew->pIndex
1f837 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1f838 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1f839 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 55 4e  "Cannot add a UN
1f83a 49 51 55 45 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20  IQUE column");. 
1f83b 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
1f83c 20 69 66 28 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75   if( pCol->notNu
1f83d 6c 6c 20 26 26 20 21 70 44 66 6c 74 20 29 7b 0a  ll && !pDflt ){.
1f83e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1f83f 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
1f840 20 20 20 20 20 22 43 61 6e 6e 6f 74 20 61 64 64       "Cannot add
1f841 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75   a NOT NULL colu
1f842 6d 6e 20 77 69 74 68 20 64 65 66 61 75 6c 74 20  mn with default 
1f843 76 61 6c 75 65 20 4e 55 4c 4c 22 29 3b 0a 20 20  value NULL");.  
1f844 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
1f845 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 64   /* Ensure the d
1f846 65 66 61 75 6c 74 20 65 78 70 72 65 73 73 69 6f  efault expressio
1f847 6e 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 74  n is something t
1f848 68 61 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65  hat sqlite3Value
1f849 46 72 6f 6d 45 78 70 72 28 29 0a 20 20 2a 2a 20  FromExpr().  ** 
1f84a 63 61 6e 20 68 61 6e 64 6c 65 20 28 69 2e 65 2e  can handle (i.e.
1f84b 20 6e 6f 74 20 43 55 52 52 45 4e 54 5f 54 49 4d   not CURRENT_TIM
1f84c 45 20 65 74 63 2e 29 0a 20 20 2a 2f 0a 20 20 69  E etc.).  */.  i
1f84d 66 28 20 70 44 66 6c 74 20 29 7b 0a 20 20 20 20  f( pDflt ){.    
1f84e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1f84f 56 61 6c 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Val;.    if( sql
1f850 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
1f851 72 28 64 62 2c 20 70 44 66 6c 74 2c 20 53 51 4c  r(db, pDflt, SQL
1f852 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
1f853 5f 41 46 46 5f 4e 4f 4e 45 2c 20 26 70 56 61 6c  _AFF_NONE, &pVal
1f854 29 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  ) ){.      db->m
1f855 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1f856 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
1f857 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 56     }.    if( !pV
1f858 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  al ){.      sqli
1f859 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1f85a 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20  se, "Cannot add 
1f85b 61 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f  a column with no
1f85c 6e 2d 63 6f 6e 73 74 61 6e 74 20 64 65 66 61 75  n-constant defau
1f85d 6c 74 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75  lt");.      retu
1f85e 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  rn;.    }.    sq
1f85f 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70  lite3ValueFree(p
1f860 56 61 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Val);.  }..  /* 
1f861 4d 6f 64 69 66 79 20 74 68 65 20 43 52 45 41 54  Modify the CREAT
1f862 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
1f863 74 2e 20 2a 2f 0a 20 20 7a 43 6f 6c 20 3d 20 73  t. */.  zCol = s
1f864 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
1f865 64 62 2c 20 28 63 68 61 72 2a 29 70 43 6f 6c 44  db, (char*)pColD
1f866 65 66 2d 3e 7a 2c 20 70 43 6f 6c 44 65 66 2d 3e  ef->z, pColDef->
1f867 6e 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6c 20 29  n);.  if( zCol )
1f868 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 6e 64  {.    char *zEnd
1f869 20 3d 20 26 7a 43 6f 6c 5b 70 43 6f 6c 44 65 66   = &zCol[pColDef
1f86a 2d 3e 6e 2d 31 5d 3b 0a 20 20 20 20 77 68 69 6c  ->n-1];.    whil
1f86b 65 28 20 28 7a 45 6e 64 3e 7a 43 6f 6c 20 26 26  e( (zEnd>zCol &&
1f86c 20 2a 7a 45 6e 64 3d 3d 27 3b 27 29 20 7c 7c 20   *zEnd==';') || 
1f86d 69 73 73 70 61 63 65 28 2a 28 75 6e 73 69 67 6e  isspace(*(unsign
1f86e 65 64 20 63 68 61 72 20 2a 29 7a 45 6e 64 29 20  ed char *)zEnd) 
1f86f 29 7b 0a 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d  ){.      *zEnd--
1f870 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20   = '\0';.    }. 
1f871 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
1f872 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20  Parse(pParse, . 
1f873 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c         "UPDATE \
1f874 22 25 77 5c 22 2e 25 73 20 53 45 54 20 22 0a 20  "%w\".%s SET ". 
1f875 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20           "sql = 
1f876 73 75 62 73 74 72 28 73 71 6c 2c 31 2c 25 64 29  substr(sql,1,%d)
1f877 20 7c 7c 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c   || ', ' || %Q |
1f878 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 25 64 29  | substr(sql,%d)
1f879 20 22 0a 20 20 20 20 20 20 20 20 22 57 48 45 52   ".        "WHER
1f87a 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27  E type = 'table'
1f87b 20 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c   AND name = %Q",
1f87c 20 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48   .      zDb, SCH
1f87d 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20  EMA_TABLE(iDb), 
1f87e 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73  pNew->addColOffs
1f87f 65 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e  et, zCol, pNew->
1f880 61 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a  addColOffset+1,.
1f881 20 20 20 20 20 20 7a 54 61 62 0a 20 20 20 20 29        zTab.    )
1f882 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
1f883 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20  ree(db, zCol);. 
1f884 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
1f885 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66  default value of
1f886 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20   the new column 
1f887 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65  is NULL, then se
1f888 74 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20  t the file.  ** 
1f889 66 6f 72 6d 61 74 20 74 6f 20 32 2e 20 49 66 20  format to 2. If 
1f88a 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
1f88b 65 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c  e of the new col
1f88c 75 6d 6e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c  umn is not NULL,
1f88d 0a 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 66  .  ** the file f
1f88e 6f 72 6d 61 74 20 62 65 63 6f 6d 65 73 20 33 2e  ormat becomes 3.
1f88f 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d  .  */.  sqlite3M
1f890 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74  inimumFileFormat
1f891 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 44  (pParse, iDb, pD
1f892 66 6c 74 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20  flt ? 3 : 2);.. 
1f893 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 73   /* Reload the s
1f894 63 68 65 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64  chema of the mod
1f895 69 66 69 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a  ified table. */.
1f896 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68    reloadTableSch
1f897 65 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61 62  ema(pParse, pTab
1f898 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
1f899 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
1f89a 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1f89b 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 61   by the parser a
1f89c 66 74 65 72 20 74 68 65 20 74 61 62 6c 65 2d 6e  fter the table-n
1f89d 61 6d 65 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c  ame in.** an "AL
1f89e 54 45 52 20 54 41 42 4c 45 20 3c 74 61 62 6c 65  TER TABLE <table
1f89f 2d 6e 61 6d 65 3e 20 41 44 44 22 20 73 74 61 74  -name> ADD" stat
1f8a0 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2e  ement is parsed.
1f8a1 20 41 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53   Argument .** pS
1f8a2 72 63 20 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e  rc is the full-n
1f8a3 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1f8a4 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a   being altered..
1f8a5 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1f8a6 6e 65 20 6d 61 6b 65 73 20 61 20 28 70 61 72 74  ne makes a (part
1f8a7 69 61 6c 29 20 63 6f 70 79 20 6f 66 20 74 68 65  ial) copy of the
1f8a8 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
1f8a9 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c  .** for the tabl
1f8aa 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20  e being altered 
1f8ab 61 6e 64 20 73 65 74 73 20 50 61 72 73 65 2e 70  and sets Parse.p
1f8ac 4e 65 77 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e  NewTable to poin
1f8ad 74 0a 2a 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74  t.** to it. Rout
1f8ae 69 6e 65 73 20 63 61 6c 6c 65 64 20 62 79 20 74  ines called by t
1f8af 68 65 20 70 61 72 73 65 72 20 61 73 20 74 68 65  he parser as the
1f8b0 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69   column definiti
1f8b1 6f 6e 0a 2a 2a 20 69 73 20 70 61 72 73 65 64 20  on.** is parsed 
1f8b2 28 69 2e 65 2e 20 73 71 6c 69 74 65 33 41 64 64  (i.e. sqlite3Add
1f8b3 43 6f 6c 75 6d 6e 28 29 29 20 61 64 64 20 74 68  Column()) add th
1f8b4 65 20 6e 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74  e new Column dat
1f8b5 61 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70  a to .** the cop
1f8b6 79 2e 20 54 68 65 20 63 6f 70 79 20 6f 66 20 74  y. The copy of t
1f8b7 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
1f8b8 72 65 20 69 73 20 64 65 6c 65 74 65 64 20 62 79  re is deleted by
1f8b9 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20   tokenize.c .** 
1f8ba 61 66 74 65 72 20 70 61 72 73 69 6e 67 20 69 73  after parsing is
1f8bb 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a   finished..**.**
1f8bc 20 52 6f 75 74 69 6e 65 20 73 71 6c 69 74 65 33   Routine sqlite3
1f8bd 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f  AlterFinishAddCo
1f8be 6c 75 6d 6e 28 29 20 77 69 6c 6c 20 62 65 20 63  lumn() will be c
1f8bf 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74  alled to complet
1f8c0 65 0a 2a 2a 20 63 6f 64 69 6e 67 20 74 68 65 20  e.** coding the 
1f8c1 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e  "ALTER TABLE ...
1f8c2 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 2e   ADD" statement.
1f8c3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1f8c4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
1f8c5 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75  lterBeginAddColu
1f8c6 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  mn(Parse *pParse
1f8c7 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29  , SrcList *pSrc)
1f8c8 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b  {.  Table *pNew;
1f8c9 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
1f8ca 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74    Vdbe *v;.  int
1f8cb 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   iDb;.  int i;. 
1f8cc 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73   int nAlloc;.  s
1f8cd 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1f8ce 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c  rse->db;..  /* L
1f8cf 6f 6f 6b 20 75 70 20 74 68 65 20 74 61 62 6c 65  ook up the table
1f8d0 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 20   being altered. 
1f8d1 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
1f8d2 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d  rse->pNewTable==
1f8d3 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
1f8d4 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
1f8d5 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29  AllMutexes(db) )
1f8d6 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
1f8d7 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20  ocFailed ) goto 
1f8d8 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63  exit_begin_add_c
1f8d9 6f 6c 75 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20  olumn;.  pTab = 
1f8da 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
1f8db 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53  le(pParse, 0, pS
1f8dc 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20  rc->a[0].zName, 
1f8dd 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61  pSrc->a[0].zData
1f8de 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54  base);.  if( !pT
1f8df 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62  ab ) goto exit_b
1f8e0 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
1f8e1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1f8e2 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1f8e3 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  LE.  if( IsVirtu
1f8e4 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
1f8e5 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1f8e6 70 50 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c  pParse, "virtual
1f8e7 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20   tables may not 
1f8e8 62 65 20 61 6c 74 65 72 65 64 22 29 3b 0a 20 20  be altered");.  
1f8e9 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69    goto exit_begi
1f8ea 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20  n_add_column;.  
1f8eb 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d  }.#endif..  /* M
1f8ec 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 69 73  ake sure this is
1f8ed 20 6e 6f 74 20 61 6e 20 61 74 74 65 6d 70 74 20   not an attempt 
1f8ee 74 6f 20 41 4c 54 45 52 20 61 20 76 69 65 77 2e  to ALTER a view.
1f8ef 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e   */.  if( pTab->
1f8f0 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73  pSelect ){.    s
1f8f1 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1f8f2 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61  Parse, "Cannot a
1f8f3 64 64 20 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61  dd a column to a
1f8f4 20 76 69 65 77 22 29 3b 0a 20 20 20 20 67 6f 74   view");.    got
1f8f5 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  o exit_begin_add
1f8f6 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20  _column;.  }..  
1f8f7 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 61 64  assert( pTab->ad
1f8f8 64 43 6f 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a  dColOffset>0 );.
1f8f9 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
1f8fa 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
1f8fb 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
1f8fc 0a 0a 20 20 2f 2a 20 50 75 74 20 61 20 63 6f 70  ..  /* Put a cop
1f8fd 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73  y of the Table s
1f8fe 74 72 75 63 74 20 69 6e 20 50 61 72 73 65 2e 70  truct in Parse.p
1f8ff 4e 65 77 54 61 62 6c 65 20 66 6f 72 20 74 68 65  NewTable for the
1f900 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 41 64 64  .  ** sqlite3Add
1f901 43 6f 6c 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f  Column() functio
1f902 6e 20 61 6e 64 20 66 72 69 65 6e 64 73 20 74 6f  n and friends to
1f903 20 6d 6f 64 69 66 79 2e 0a 20 20 2a 2f 0a 20 20   modify..  */.  
1f904 70 4e 65 77 20 3d 20 28 54 61 62 6c 65 2a 29 73  pNew = (Table*)s
1f905 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
1f906 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61  ro(db, sizeof(Ta
1f907 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 70 4e  ble));.  if( !pN
1f908 65 77 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62  ew ) goto exit_b
1f909 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
1f90a 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  .  pParse->pNewT
1f90b 61 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 70  able = pNew;.  p
1f90c 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  New->nRef = 1;. 
1f90d 20 70 4e 65 77 2d 3e 64 62 20 3d 20 64 62 3b 0a   pNew->db = db;.
1f90e 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70    pNew->nCol = p
1f90f 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73  Tab->nCol;.  ass
1f910 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e  ert( pNew->nCol>
1f911 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20  0 );.  nAlloc = 
1f912 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29  (((pNew->nCol-1)
1f913 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65  /8)*8)+8;.  asse
1f914 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77  rt( nAlloc>=pNew
1f915 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63  ->nCol && nAlloc
1f916 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d  %8==0 && nAlloc-
1f917 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a  pNew->nCol<8 );.
1f918 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28    pNew->aCol = (
1f919 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44  Column*)sqlite3D
1f91a 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1f91b 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e  sizeof(Column)*n
1f91c 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d 3e  Alloc);.  pNew->
1f91d 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
1f91e 62 53 74 72 44 75 70 28 64 62 2c 20 70 54 61 62  bStrDup(db, pTab
1f91f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ->zName);.  if( 
1f920 21 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c 7c 20 21  !pNew->aCol || !
1f921 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20  pNew->zName ){. 
1f922 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
1f923 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74  led = 1;.    got
1f924 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  o exit_begin_add
1f925 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20 6d  _column;.  }.  m
1f926 65 6d 63 70 79 28 70 4e 65 77 2d 3e 61 43 6f 6c  emcpy(pNew->aCol
1f927 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 73 69  , pTab->aCol, si
1f928 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 70 4e 65  zeof(Column)*pNe
1f929 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28  w->nCol);.  for(
1f92a 69 3d 30 3b 20 69 3c 70 4e 65 77 2d 3e 6e 43 6f  i=0; i<pNew->nCo
1f92b 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 43 6f 6c  l; i++){.    Col
1f92c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 4e 65  umn *pCol = &pNe
1f92d 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20  w->aCol[i];.    
1f92e 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71  pCol->zName = sq
1f92f 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
1f930 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pCol->zName);.
1f931 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20      pCol->zColl 
1f932 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a  = 0;.    pCol->z
1f933 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70 43  Type = 0;.    pC
1f934 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 30 3b 0a 20  ol->pDflt = 0;. 
1f935 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 53 63 68 65   }.  pNew->pSche
1f936 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  ma = db->aDb[iDb
1f937 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 4e 65  ].pSchema;.  pNe
1f938 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20  w->addColOffset 
1f939 3d 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66  = pTab->addColOf
1f93a 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52  fset;.  pNew->nR
1f93b 65 66 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 42 65  ef = 1;..  /* Be
1f93c 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  gin a transactio
1f93d 6e 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20  n and increment 
1f93e 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
1f93f 65 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  e.  */.  sqlite3
1f940 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
1f941 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
1f942 44 62 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74  Db);.  v = sqlit
1f943 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1f944 29 3b 0a 20 20 69 66 28 20 21 76 20 29 20 67 6f  );.  if( !v ) go
1f945 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64  to exit_begin_ad
1f946 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 73 71 6c 69  d_column;.  sqli
1f947 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28  te3ChangeCookie(
1f948 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 65  pParse, iDb);..e
1f949 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
1f94a 6c 75 6d 6e 3a 0a 20 20 73 71 6c 69 74 65 33 53  lumn:.  sqlite3S
1f94b 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
1f94c 20 70 53 72 63 29 3b 0a 20 20 72 65 74 75 72 6e   pSrc);.  return
1f94d 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53  ;.}.#endif  /* S
1f94e 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c  QLITE_ALTER_TABL
1f94f 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
1f950 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6c 74  ***** End of alt
1f951 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
1f952 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f953 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f954 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1f955 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1f956 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a   analyze.c *****
1f957 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f958 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f959 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35  ****/./*.** 2005
1f95a 20 4a 75 6c 79 20 38 0a 2a 2a 0a 2a 2a 20 54 68   July 8.**.** Th
1f95b 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1f95c 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1f95d 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1f95e 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1f95f 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1f960 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1f961 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1f962 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1f963 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1f964 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1f965 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1f966 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1f967 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1f968 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1f969 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1f96a 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1f96b 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1f96c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f96d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f96e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f96f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f970 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
1f971 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
1f972 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
1f973 68 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f  h the ANALYZE co
1f974 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  mmand..**.** @(#
1f975 29 20 24 49 64 3a 20 61 6e 61 6c 79 7a 65 2e 63  ) $Id: analyze.c
1f976 2c 76 20 31 2e 34 37 20 32 30 30 38 2f 31 32 2f  ,v 1.47 2008/12/
1f977 31 30 20 31 36 3a 34 35 3a 35 31 20 64 72 68 20  10 16:45:51 drh 
1f978 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
1f979 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41   SQLITE_OMIT_ANA
1f97a 4c 59 5a 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  LYZE../*.** This
1f97b 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
1f97c 65 73 20 63 6f 64 65 20 74 68 61 74 20 6f 70 65  es code that ope
1f97d 6e 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  ns the sqlite_st
1f97e 61 74 31 20 74 61 62 6c 65 20 6f 6e 20 63 75 72  at1 table on cur
1f97f 73 6f 72 0a 2a 2a 20 69 53 74 61 74 43 75 72 2e  sor.** iStatCur.
1f980 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 71  .**.** If the sq
1f981 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
1f982 73 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69  s does not previ
1f983 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 69 74 20  ously exist, it 
1f984 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 20 49  is created..** I
1f985 66 20 69 74 20 64 6f 65 73 20 70 72 65 76 69 6f  f it does previo
1f986 75 73 6c 79 20 65 78 69 73 74 2c 20 61 6c 6c 20  usly exist, all 
1f987 65 6e 74 69 72 65 73 20 61 73 73 6f 63 69 61 74  entires associat
1f988 65 64 20 77 69 74 68 20 74 61 62 6c 65 20 7a 57  ed with table zW
1f989 68 65 72 65 0a 2a 2a 20 61 72 65 20 72 65 6d 6f  here.** are remo
1f98a 76 65 64 2e 20 20 49 66 20 7a 57 68 65 72 65 3d  ved.  If zWhere=
1f98b 3d 30 20 74 68 65 6e 20 61 6c 6c 20 65 6e 74 72  =0 then all entr
1f98c 69 65 73 20 61 72 65 20 72 65 6d 6f 76 65 64 2e  ies are removed.
1f98d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1f98e 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 0a 20  openStatTable(. 
1f98f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1f990 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
1f991 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
1f992 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20   int iDb,       
1f993 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1f994 64 61 74 61 62 61 73 65 20 77 65 20 61 72 65 20  database we are 
1f995 6c 6f 6f 6b 69 6e 67 20 69 6e 20 2a 2f 0a 20 20  looking in */.  
1f996 69 6e 74 20 69 53 74 61 74 43 75 72 2c 20 20 20  int iStatCur,   
1f997 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20          /* Open 
1f998 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
1f999 20 74 61 62 6c 65 20 6f 6e 20 74 68 69 73 20 63   table on this c
1f99a 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  ursor */.  const
1f99b 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 20 20   char *zWhere   
1f99c 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 6e 74     /* Delete ent
1f99d 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ries associated 
1f99e 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 20  with this table 
1f99f 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
1f9a0 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1f9a1 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 69  ;.  Db *pDb;.  i
1f9a2 6e 74 20 69 52 6f 6f 74 50 61 67 65 3b 0a 20 20  nt iRootPage;.  
1f9a3 75 38 20 63 72 65 61 74 65 53 74 61 74 31 20 3d  u8 createStat1 =
1f9a4 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 53 74   0;.  Table *pSt
1f9a5 61 74 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  at;.  Vdbe *v = 
1f9a6 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
1f9a7 50 61 72 73 65 29 3b 0a 0a 20 20 69 66 28 20 76  Parse);..  if( v
1f9a8 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1f9a9 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1f9aa 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
1f9ab 78 65 73 28 64 62 29 20 29 3b 0a 20 20 61 73 73  xes(db) );.  ass
1f9ac 65 72 74 28 20 73 71 6c 69 74 65 33 56 64 62 65  ert( sqlite3Vdbe
1f9ad 44 62 28 76 29 3d 3d 64 62 20 29 3b 0a 20 20 70  Db(v)==db );.  p
1f9ae 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  Db = &db->aDb[iD
1f9af 62 5d 3b 0a 20 20 69 66 28 20 28 70 53 74 61 74  b];.  if( (pStat
1f9b0 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
1f9b1 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f  ble(db, "sqlite_
1f9b2 73 74 61 74 31 22 2c 20 70 44 62 2d 3e 7a 4e 61  stat1", pDb->zNa
1f9b3 6d 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  me))==0 ){.    /
1f9b4 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61  * The sqlite_sta
1f9b5 74 31 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e  t1 tables does n
1f9b6 6f 74 20 65 78 69 73 74 2e 20 20 43 72 65 61 74  ot exist.  Creat
1f9b7 65 20 69 74 2e 20 20 0a 20 20 20 20 2a 2a 20 4e  e it.  .    ** N
1f9b8 6f 74 65 20 74 68 61 74 20 61 20 73 69 64 65 2d  ote that a side-
1f9b9 65 66 66 65 63 74 20 6f 66 20 74 68 65 20 43 52  effect of the CR
1f9ba 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
1f9bb 6d 65 6e 74 20 69 73 20 74 6f 20 6c 65 61 76 65  ment is to leave
1f9bc 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 6f 6f 74  .    ** the root
1f9bd 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77 20  page of the new 
1f9be 74 61 62 6c 65 20 69 6e 20 72 65 67 69 73 74 65  table in registe
1f9bf 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f  r pParse->regRoo
1f9c0 74 2e 20 20 54 68 69 73 20 69 73 0a 20 20 20 20  t.  This is.    
1f9c1 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 62 65 63  ** important bec
1f9c2 61 75 73 65 20 74 68 65 20 4f 70 65 6e 57 72 69  ause the OpenWri
1f9c3 74 65 20 6f 70 63 6f 64 65 20 62 65 6c 6f 77 20  te opcode below 
1f9c4 77 69 6c 6c 20 62 65 20 6e 65 65 64 69 6e 67 20  will be needing 
1f9c5 69 74 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  it. */.    sqlit
1f9c6 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
1f9c7 61 72 73 65 2c 0a 20 20 20 20 20 20 22 43 52 45  arse,.      "CRE
1f9c8 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73 71 6c  ATE TABLE %Q.sql
1f9c9 69 74 65 5f 73 74 61 74 31 28 74 62 6c 2c 69 64  ite_stat1(tbl,id
1f9ca 78 2c 73 74 61 74 29 22 2c 0a 20 20 20 20 20 20  x,stat)",.      
1f9cb 70 44 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29  pDb->zName.    )
1f9cc 3b 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 65 20  ;.    iRootPage 
1f9cd 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f  = pParse->regRoo
1f9ce 74 3b 0a 20 20 20 20 63 72 65 61 74 65 53 74 61  t;.    createSta
1f9cf 74 31 20 3d 20 31 3b 20 20 2f 2a 20 43 61 75 73  t1 = 1;  /* Caus
1f9d0 65 20 72 6f 6f 74 70 61 67 65 20 74 6f 20 62 65  e rootpage to be
1f9d1 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 6f 70 20   taken from top 
1f9d2 6f 66 20 73 74 61 63 6b 20 2a 2f 0a 20 20 7d 65  of stack */.  }e
1f9d3 6c 73 65 20 69 66 28 20 7a 57 68 65 72 65 20 29  lse if( zWhere )
1f9d4 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c  {.    /* The sql
1f9d5 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20  ite_stat1 table 
1f9d6 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20  exists.  Delete 
1f9d7 61 6c 6c 20 65 6e 74 72 69 65 73 20 61 73 73 6f  all entries asso
1f9d8 63 69 61 74 65 64 20 77 69 74 68 0a 20 20 20 20  ciated with.    
1f9d9 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 7a 57 68  ** the table zWh
1f9da 65 72 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  ere. */.    sqli
1f9db 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
1f9dc 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 22 44  Parse,.       "D
1f9dd 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71  ELETE FROM %Q.sq
1f9de 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45 52 45  lite_stat1 WHERE
1f9df 20 74 62 6c 3d 25 51 22 2c 0a 20 20 20 20 20 20   tbl=%Q",.      
1f9e0 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 57 68   pDb->zName, zWh
1f9e1 65 72 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69  ere.    );.    i
1f9e2 52 6f 6f 74 50 61 67 65 20 3d 20 70 53 74 61 74  RootPage = pStat
1f9e3 2d 3e 74 6e 75 6d 3b 0a 20 20 7d 65 6c 73 65 7b  ->tnum;.  }else{
1f9e4 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69  .    /* The sqli
1f9e5 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 61  te_stat1 table a
1f9e6 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 20 20  lready exists.  
1f9e7 44 65 6c 65 74 65 20 61 6c 6c 20 72 6f 77 73 2e  Delete all rows.
1f9e8 20 2a 2f 0a 20 20 20 20 69 52 6f 6f 74 50 61 67   */.    iRootPag
1f9e9 65 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b  e = pStat->tnum;
1f9ea 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1f9eb 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65  AddOp2(v, OP_Cle
1f9ec 61 72 2c 20 70 53 74 61 74 2d 3e 74 6e 75 6d 2c  ar, pStat->tnum,
1f9ed 20 69 44 62 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a   iDb);.  }..  /*
1f9ee 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65   Open the sqlite
1f9ef 5f 73 74 61 74 31 20 74 61 62 6c 65 20 66 6f 72  _stat1 table for
1f9f0 20 77 72 69 74 69 6e 67 2e 20 55 6e 6c 65 73 73   writing. Unless
1f9f1 20 69 74 20 77 61 73 20 63 72 65 61 74 65 64 0a   it was created.
1f9f2 20 20 2a 2a 20 62 79 20 74 68 69 73 20 76 64 62    ** by this vdb
1f9f3 65 20 70 72 6f 67 72 61 6d 2c 20 6c 6f 63 6b 20  e program, lock 
1f9f4 69 74 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61  it for writing a
1f9f5 74 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63  t the shared-cac
1f9f6 68 65 20 6c 65 76 65 6c 2e 20 0a 20 20 2a 2a 20  he level. .  ** 
1f9f7 49 66 20 74 68 69 73 20 76 64 62 65 20 64 69 64  If this vdbe did
1f9f8 20 63 72 65 61 74 65 20 74 68 65 20 73 71 6c 69   create the sqli
1f9f9 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2c 20  te_stat1 table, 
1f9fa 74 68 65 6e 20 69 74 20 6d 75 73 74 20 68 61 76  then it must hav
1f9fb 65 20 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  e .  ** already 
1f9fc 6f 62 74 61 69 6e 65 64 20 61 20 73 63 68 65 6d  obtained a schem
1f9fd 61 2d 6c 6f 63 6b 2c 20 6d 61 6b 69 6e 67 20 74  a-lock, making t
1f9fe 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 72 65  he write-lock re
1f9ff 64 75 6e 64 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20  dundant..  */.  
1fa00 69 66 28 20 21 63 72 65 61 74 65 53 74 61 74 31  if( !createStat1
1fa01 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 54   ){.    sqlite3T
1fa02 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
1fa03 20 69 44 62 2c 20 69 52 6f 6f 74 50 61 67 65 2c   iDb, iRootPage,
1fa04 20 31 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74   1, "sqlite_stat
1fa05 31 22 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  1");.  }.  sqlit
1fa06 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1fa07 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
1fa08 2c 20 30 2c 20 33 29 3b 0a 20 20 73 71 6c 69 74  , 0, 3);.  sqlit
1fa09 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
1fa0a 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 53  OP_OpenWrite, iS
1fa0b 74 61 74 43 75 72 2c 20 69 52 6f 6f 74 50 61 67  tatCur, iRootPag
1fa0c 65 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74  e, iDb);.  sqlit
1fa0d 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
1fa0e 2c 20 63 72 65 61 74 65 53 74 61 74 31 29 3b 0a  , createStat1);.
1fa0f 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
1fa10 65 20 63 6f 64 65 20 74 6f 20 64 6f 20 61 6e 20  e code to do an 
1fa11 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6c 6c 20  analysis of all 
1fa12 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74  indices associat
1fa13 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e  ed with.** a sin
1fa14 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  gle table..*/.st
1fa15 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a  atic void analyz
1fa16 65 4f 6e 65 54 61 62 6c 65 28 0a 20 20 50 61 72  eOneTable(.  Par
1fa17 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a  se *pParse,   /*
1fa18 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
1fa19 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
1fa1a 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 77  ,     /* Table w
1fa1b 68 6f 73 65 20 69 6e 64 69 63 65 73 20 61 72 65  hose indices are
1fa1c 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20   to be analyzed 
1fa1d 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75  */.  int iStatCu
1fa1e 72 2c 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f  r,    /* Index o
1fa1f 66 20 56 64 62 65 43 75 72 73 6f 72 20 74 68 61  f VdbeCursor tha
1fa20 74 20 77 72 69 74 65 73 20 74 68 65 20 73 71 6c  t writes the sql
1fa21 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20  ite_stat1 table 
1fa22 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20  */.  int iMem   
1fa23 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62        /* Availab
1fa24 6c 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  le memory locati
1fa25 6f 6e 73 20 62 65 67 69 6e 20 68 65 72 65 20 2a  ons begin here *
1fa26 2f 0a 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49  /.){.  Index *pI
1fa27 64 78 3b 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e  dx;     /* An in
1fa28 64 65 78 20 74 6f 20 62 65 69 6e 67 20 61 6e 61  dex to being ana
1fa29 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  lyzed */.  int i
1fa2a 49 64 78 43 75 72 3b 20 20 20 20 20 2f 2a 20 49  IdxCur;     /* I
1fa2b 6e 64 65 78 20 6f 66 20 56 64 62 65 43 75 72 73  ndex of VdbeCurs
1fa2c 6f 72 20 66 6f 72 20 69 6e 64 65 78 20 62 65 69  or for index bei
1fa2d 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20  ng analyzed */. 
1fa2e 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20   int nCol;      
1fa2f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
1fa30 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e  olumns in the in
1fa31 64 65 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  dex */.  Vdbe *v
1fa32 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
1fa33 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1fa34 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20   being built up 
1fa35 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
1fa36 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
1fa37 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74  unter */.  int t
1fa38 6f 70 4f 66 4c 6f 6f 70 3b 20 20 20 2f 2a 20 54  opOfLoop;   /* T
1fa39 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f  he top of the lo
1fa3a 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f  op */.  int endO
1fa3b 66 4c 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65 20  fLoop;   /* The 
1fa3c 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  end of the loop 
1fa3d 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20  */.  int addr;  
1fa3e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64        /* The add
1fa3f 72 65 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72  ress of an instr
1fa40 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  uction */.  int 
1fa41 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  iDb;         /* 
1fa42 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73  Index of databas
1fa43 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61  e containing pTa
1fa44 62 20 2a 2f 0a 0a 20 20 76 20 3d 20 73 71 6c 69  b */..  v = sqli
1fa45 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
1fa46 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c  e);.  if( v==0 |
1fa47 7c 20 70 54 61 62 3d 3d 30 20 7c 7c 20 70 54 61  | pTab==0 || pTa
1fa48 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a  b->pIndex==0 ){.
1fa49 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61      /* Do no ana
1fa4a 6c 79 73 69 73 20 66 6f 72 20 74 61 62 6c 65 73  lysis for tables
1fa4b 20 74 68 61 74 20 68 61 76 65 20 6e 6f 20 69 6e   that have no in
1fa4c 64 69 63 65 73 20 2a 2f 0a 20 20 20 20 72 65 74  dices */.    ret
1fa4d 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
1fa4e 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
1fa4f 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70  oldsAllMutexes(p
1fa50 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20  Parse->db) );.  
1fa51 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1fa52 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
1fa53 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
1fa54 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28  hema);.  assert(
1fa55 20 69 44 62 3e 3d 30 20 29 3b 0a 23 69 66 6e 64   iDb>=0 );.#ifnd
1fa56 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1fa57 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69  UTHORIZATION.  i
1fa58 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
1fa59 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
1fa5a 54 45 5f 41 4e 41 4c 59 5a 45 2c 20 70 54 61 62  TE_ANALYZE, pTab
1fa5b 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20  ->zName, 0,.    
1fa5c 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44    pParse->db->aD
1fa5d 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29  b[iDb].zName ) )
1fa5e 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
1fa5f 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45  }.#endif..  /* E
1fa60 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 2d  stablish a read-
1fa61 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c  lock on the tabl
1fa62 65 20 61 74 20 74 68 65 20 73 68 61 72 65 64 2d  e at the shared-
1fa63 63 61 63 68 65 20 6c 65 76 65 6c 2e 20 2a 2f 0a  cache level. */.
1fa64 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
1fa65 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  ck(pParse, iDb, 
1fa66 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70  pTab->tnum, 0, p
1fa67 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20  Tab->zName);..  
1fa68 69 49 64 78 43 75 72 20 3d 20 70 50 61 72 73 65  iIdxCur = pParse
1fa69 2d 3e 6e 54 61 62 3b 0a 20 20 66 6f 72 28 70 49  ->nTab;.  for(pI
1fa6a 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
1fa6b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
1fa6c 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 4b 65  ->pNext){.    Ke
1fa6d 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71  yInfo *pKey = sq
1fa6e 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
1fa6f 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b  o(pParse, pIdx);
1fa70 0a 20 20 20 20 69 6e 74 20 72 65 67 46 69 65 6c  .    int regFiel
1fa71 64 73 3b 20 20 20 20 2f 2a 20 52 65 67 69 73 74  ds;    /* Regist
1fa72 65 72 20 62 6c 6f 63 6b 20 66 6f 72 20 62 75 69  er block for bui
1fa73 6c 64 69 6e 67 20 72 65 63 6f 72 64 73 20 2a 2f  lding records */
1fa74 0a 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b  .    int regRec;
1fa75 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
1fa76 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6d 70 6c  er holding compl
1fa77 65 74 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20  eted record */. 
1fa78 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 3b 20     int regTemp; 
1fa79 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
1fa7a 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20 2a  y use register *
1fa7b 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 43 6f 6c  /.    int regCol
1fa7c 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ;       /* Conte
1fa7d 6e 74 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 66  nt of a column f
1fa7e 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 62 65  rom the table be
1fa7f 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a  ing analyzed */.
1fa80 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64      int regRowid
1fa81 3b 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 66  ;     /* Rowid f
1fa82 6f 72 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  or the inserted 
1fa83 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e  record */.    in
1fa84 74 20 72 65 67 46 32 3b 0a 0a 20 20 20 20 2f 2a  t regF2;..    /*
1fa85 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72 20 74   Open a cursor t
1fa86 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 62  o the index to b
1fa87 65 20 61 6e 61 6c 79 7a 65 64 0a 20 20 20 20 2a  e analyzed.    *
1fa88 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  /.    assert( iD
1fa89 62 3d 3d 73 71 6c 69 74 65 33 53 63 68 65 6d 61  b==sqlite3Schema
1fa8a 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
1fa8b 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d  db, pIdx->pSchem
1fa8c 61 29 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d  a) );.    nCol =
1fa8d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a   pIdx->nColumn;.
1fa8e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fa8f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e  ddOp2(v, OP_SetN
1fa90 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 6e 43  umColumns, 0, nC
1fa91 6f 6c 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  ol+1);.    sqlit
1fa92 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
1fa93 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64  OP_OpenRead, iId
1fa94 78 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d  xCur, pIdx->tnum
1fa95 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 28  , iDb,.        (
1fa96 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 50 34 5f  char *)pKey, P4_
1fa97 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
1fa98 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
1fa99 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78  t((v, "%s", pIdx
1fa9a 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 72  ->zName));.    r
1fa9b 65 67 46 69 65 6c 64 73 20 3d 20 69 4d 65 6d 2b  egFields = iMem+
1fa9c 6e 43 6f 6c 2a 32 3b 0a 20 20 20 20 72 65 67 54  nCol*2;.    regT
1fa9d 65 6d 70 20 3d 20 72 65 67 52 6f 77 69 64 20 3d  emp = regRowid =
1fa9e 20 72 65 67 43 6f 6c 20 3d 20 72 65 67 46 69 65   regCol = regFie
1fa9f 6c 64 73 2b 33 3b 0a 20 20 20 20 72 65 67 52 65  lds+3;.    regRe
1faa0 63 20 3d 20 72 65 67 43 6f 6c 2b 31 3b 0a 20 20  c = regCol+1;.  
1faa1 20 20 69 66 28 20 72 65 67 52 65 63 3e 70 50 61    if( regRec>pPa
1faa2 72 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20 20 20  rse->nMem ){.   
1faa3 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
1faa4 3d 20 72 65 67 52 65 63 3b 0a 20 20 20 20 7d 0a  = regRec;.    }.
1faa5 0a 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63  .    /* Memory c
1faa6 65 6c 6c 73 20 61 72 65 20 75 73 65 64 20 61 73  ells are used as
1faa7 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a   follows:.    **
1faa8 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69  .    **    mem[i
1faa9 4d 65 6d 5d 3a 20 20 20 20 20 20 20 20 20 20 20  Mem]:           
1faaa 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62    The total numb
1faab 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
1faac 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20  e table..    ** 
1faad 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 31 5d 3a 20     mem[iMem+1]: 
1faae 20 20 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72            Number
1faaf 20 6f 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c   of distinct val
1fab0 75 65 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 31 0a  ues in column 1.
1fab1 20 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20      **    ....  
1fab2 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d    **    mem[iMem
1fab3 2b 6e 43 6f 6c 5d 3a 20 20 20 20 20 20 20 20 4e  +nCol]:        N
1fab4 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63  umber of distinc
1fab5 74 20 76 61 6c 75 65 73 20 69 6e 20 63 6f 6c 75  t values in colu
1fab6 6d 6e 20 4e 0a 20 20 20 20 2a 2a 20 20 20 20 6d  mn N.    **    m
1fab7 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 2b 31 5d 20  em[iMem+nCol+1] 
1fab8 20 20 20 20 20 20 4c 61 73 74 20 6f 62 73 65 72        Last obser
1fab9 76 65 64 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c  ved value of col
1faba 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 20 20 20  umn 1.    **    
1fabb 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65  ....    **    me
1fabc 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 2b 6e 43 6f 6c  m[iMem+nCol+nCol
1fabd 5d 3a 20 20 20 4c 61 73 74 20 6f 62 73 65 72 76  ]:   Last observ
1fabe 65 64 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75  ed value of colu
1fabf 6d 6e 20 4e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  mn N.    **.    
1fac0 2a 2a 20 43 65 6c 6c 73 20 69 4d 65 6d 20 74 68  ** Cells iMem th
1fac1 72 6f 75 67 68 20 69 4d 65 6d 2b 6e 43 6f 6c 20  rough iMem+nCol 
1fac2 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  are initialized 
1fac3 74 6f 20 30 2e 20 20 54 68 65 20 6f 74 68 65 72  to 0.  The other
1fac4 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 69 6e 69  s.    ** are ini
1fac5 74 69 61 6c 69 7a 65 64 20 74 6f 20 4e 55 4c 4c  tialized to NULL
1fac6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
1fac7 28 69 3d 30 3b 20 69 3c 3d 6e 43 6f 6c 3b 20 69  (i=0; i<=nCol; i
1fac8 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
1fac9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1faca 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69  OP_Integer, 0, i
1facb 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20  Mem+i);.    }.  
1facc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
1facd 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  l; i++){.      s
1face 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1facf 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
1fad0 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a  iMem+nCol+i+1);.
1fad1 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f      }..    /* Do
1fad2 20 74 68 65 20 61 6e 61 6c 79 73 69 73 2e 0a 20   the analysis.. 
1fad3 20 20 20 2a 2f 0a 20 20 20 20 65 6e 64 4f 66 4c     */.    endOfL
1fad4 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oop = sqlite3Vdb
1fad5 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
1fad6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1fad7 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
1fad8 64 2c 20 69 49 64 78 43 75 72 2c 20 65 6e 64 4f  d, iIdxCur, endO
1fad9 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 74 6f 70 4f  fLoop);.    topO
1fada 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  fLoop = sqlite3V
1fadb 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
1fadc 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1fadd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
1fade 64 64 49 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29 3b  ddImm, iMem, 1);
1fadf 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1fae0 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
1fae1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fae2 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
1fae3 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20 72 65  , iIdxCur, i, re
1fae4 67 43 6f 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c  gCol);.      sql
1fae5 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1fae6 2c 20 4f 50 5f 4e 65 2c 20 72 65 67 43 6f 6c 2c  , OP_Ne, regCol,
1fae7 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b   0, iMem+nCol+i+
1fae8 31 29 3b 0a 20 20 20 20 20 20 2f 2a 2a 2a 2a 20  1);.      /**** 
1fae9 54 4f 44 4f 3a 20 20 61 64 64 20 63 6f 6c 6c 61  TODO:  add colla
1faea 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2a  ting sequence **
1faeb 2a 2a 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  ***/.      sqlit
1faec 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
1faed 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
1faee 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ULL);.    }.    
1faef 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1faf0 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
1faf1 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20   endOfLoop);.   
1faf2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
1faf3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
1faf4 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1faf5 65 28 76 2c 20 74 6f 70 4f 66 4c 6f 6f 70 20 2b  e(v, topOfLoop +
1faf6 20 32 2a 28 69 20 2b 20 31 29 29 3b 0a 20 20 20   2*(i + 1));.   
1faf7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1faf8 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
1faf9 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 31 29 3b  m, iMem+i+1, 1);
1fafa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1fafb 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
1fafc 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20  olumn, iIdxCur, 
1fafd 69 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31  i, iMem+nCol+i+1
1fafe 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
1faff 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
1fb00 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f  abel(v, endOfLoo
1fb01 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  p);.    sqlite3V
1fb02 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1fb03 4e 65 78 74 2c 20 69 49 64 78 43 75 72 2c 20 74  Next, iIdxCur, t
1fb04 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73  opOfLoop);.    s
1fb05 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
1fb06 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49  (v, OP_Close, iI
1fb07 64 78 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20  dxCur);..    /* 
1fb08 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  Store the result
1fb09 73 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20  s.  .    **.    
1fb0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73  ** The result is
1fb0b 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
1fb0c 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
1fb0d 31 20 74 61 62 6c 65 2e 20 20 54 68 65 20 66 69  1 table.  The fi
1fb0e 72 73 74 0a 20 20 20 20 2a 2a 20 74 77 6f 20 63  rst.    ** two c
1fb0f 6f 6c 75 6d 6e 73 20 61 72 65 20 74 68 65 20 6e  olumns are the n
1fb10 61 6d 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ames of the tabl
1fb11 65 20 61 6e 64 20 69 6e 64 65 78 2e 20 20 54 68  e and index.  Th
1fb12 65 20 74 68 69 72 64 20 63 6f 6c 75 6d 6e 0a 20  e third column. 
1fb13 20 20 20 2a 2a 20 69 73 20 61 20 73 74 72 69 6e     ** is a strin
1fb14 67 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20  g composed of a 
1fb15 6c 69 73 74 20 6f 66 20 69 6e 74 65 67 65 72 20  list of integer 
1fb16 73 74 61 74 69 73 74 69 63 73 20 61 62 6f 75 74  statistics about
1fb17 20 74 68 65 0a 20 20 20 20 2a 2a 20 69 6e 64 65   the.    ** inde
1fb18 78 2e 20 20 54 68 65 20 66 69 72 73 74 20 69 6e  x.  The first in
1fb19 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73  teger in the lis
1fb1a 74 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  t is the total n
1fb1b 75 6d 62 65 72 20 6f 66 20 65 6e 74 69 72 65 73  umber of entires
1fb1c 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 69  .    ** in the i
1fb1d 6e 64 65 78 2e 20 20 54 68 65 72 65 20 69 73 20  ndex.  There is 
1fb1e 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  one additional i
1fb1f 6e 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69  nteger in the li
1fb20 73 74 20 66 6f 72 20 65 61 63 68 0a 20 20 20 20  st for each.    
1fb21 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  ** column of the
1fb22 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 61 64   table.  This ad
1fb23 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72  ditional integer
1fb24 20 69 73 20 61 20 67 75 65 73 73 20 6f 66 20 68   is a guess of h
1fb25 6f 77 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 72  ow many.    ** r
1fb26 6f 77 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ows of the table
1fb27 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c 20   the index will 
1fb28 73 65 6c 65 63 74 2e 20 20 49 66 20 44 20 69 73  select.  If D is
1fb29 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 64 69   the count of di
1fb2a 73 74 69 6e 63 74 0a 20 20 20 20 2a 2a 20 76 61  stinct.    ** va
1fb2b 6c 75 65 73 20 61 6e 64 20 4b 20 69 73 20 74 68  lues and K is th
1fb2c 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
1fb2d 66 20 72 6f 77 73 2c 20 74 68 65 6e 20 74 68 65  f rows, then the
1fb2e 20 69 6e 74 65 67 65 72 20 69 73 20 63 6f 6d 70   integer is comp
1fb2f 75 74 65 64 0a 20 20 20 20 2a 2a 20 61 73 3a 0a  uted.    ** as:.
1fb30 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
1fb31 20 20 20 20 20 49 20 3d 20 28 4b 2b 44 2d 31 29       I = (K+D-1)
1fb32 2f 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  /D.    **.    **
1fb33 20 49 66 20 4b 3d 3d 30 20 74 68 65 6e 20 6e 6f   If K==0 then no
1fb34 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69   entry is made i
1fb35 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73  nto the sqlite_s
1fb36 74 61 74 31 20 74 61 62 6c 65 2e 20 20 0a 20 20  tat1 table.  .  
1fb37 20 20 2a 2a 20 49 66 20 4b 3e 30 20 74 68 65 6e    ** If K>0 then
1fb38 20 69 74 20 69 73 20 61 6c 77 61 79 73 20 74 68   it is always th
1fb39 65 20 63 61 73 65 20 74 68 65 20 44 3e 30 20 73  e case the D>0 s
1fb3a 6f 20 64 69 76 69 73 69 6f 6e 20 62 79 20 7a 65  o division by ze
1fb3b 72 6f 0a 20 20 20 20 2a 2a 20 69 73 20 6e 65 76  ro.    ** is nev
1fb3c 65 72 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20  er possible..   
1fb3d 20 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 73   */.    addr = s
1fb3e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
1fb3f 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 69 4d  (v, OP_IfNot, iM
1fb40 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  em);.    sqlite3
1fb41 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1fb42 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67  _String8, 0, reg
1fb43 46 69 65 6c 64 73 2c 20 30 2c 20 70 54 61 62 2d  Fields, 0, pTab-
1fb44 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
1fb45 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fb46 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
1fb47 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2b 31 2c   0, regFields+1,
1fb48 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c   0, pIdx->zName,
1fb49 20 30 29 3b 0a 20 20 20 20 72 65 67 46 32 20 3d   0);.    regF2 =
1fb4a 20 72 65 67 46 69 65 6c 64 73 2b 32 3b 0a 20 20   regFields+2;.  
1fb4b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fb4c 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
1fb4d 20 69 4d 65 6d 2c 20 72 65 67 46 32 29 3b 0a 20   iMem, regF2);. 
1fb4e 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
1fb4f 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
1fb50 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fb51 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
1fb52 20 30 2c 20 72 65 67 54 65 6d 70 2c 20 30 2c 20   0, regTemp, 0, 
1fb53 22 20 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 73  " ", 0);.      s
1fb54 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1fb55 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 72  (v, OP_Concat, r
1fb56 65 67 54 65 6d 70 2c 20 72 65 67 46 32 2c 20 72  egTemp, regF2, r
1fb57 65 67 46 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  egF2);.      sql
1fb58 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1fb59 2c 20 4f 50 5f 41 64 64 2c 20 69 4d 65 6d 2c 20  , OP_Add, iMem, 
1fb5a 69 4d 65 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d  iMem+i+1, regTem
1fb5b 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
1fb5c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1fb5d 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 54 65 6d  P_AddImm, regTem
1fb5e 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71  p, -1);.      sq
1fb5f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1fb60 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 69 4d  v, OP_Divide, iM
1fb61 65 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d 70 2c  em+i+1, regTemp,
1fb62 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20   regTemp);.     
1fb63 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1fb64 70 31 28 76 2c 20 4f 50 5f 54 6f 49 6e 74 2c 20  p1(v, OP_ToInt, 
1fb65 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20  regTemp);.      
1fb66 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fb67 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20  3(v, OP_Concat, 
1fb68 72 65 67 54 65 6d 70 2c 20 72 65 67 46 32 2c 20  regTemp, regF2, 
1fb69 72 65 67 46 32 29 3b 0a 20 20 20 20 7d 0a 20 20  regF2);.    }.  
1fb6a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fb6b 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
1fb6c 63 6f 72 64 2c 20 72 65 67 46 69 65 6c 64 73 2c  cord, regFields,
1fb6d 20 33 2c 20 72 65 67 52 65 63 2c 20 22 61 61 61   3, regRec, "aaa
1fb6e 22 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  ", 0);.    sqlit
1fb6f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1fb70 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53 74  OP_NewRowid, iSt
1fb71 61 74 43 75 72 2c 20 72 65 67 52 6f 77 69 64 29  atCur, regRowid)
1fb72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1fb73 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
1fb74 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2c 20  sert, iStatCur, 
1fb75 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64  regRec, regRowid
1fb76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1fb77 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
1fb78 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
1fb79 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1fb7a 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
1fb7b 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e    }.}../*.** Gen
1fb7c 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
1fb7d 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20 6d  will cause the m
1fb7e 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 64 65 78  ost recent index
1fb7f 20 61 6e 61 6c 79 73 69 73 20 74 6f 0a 2a 2a 20   analysis to.** 
1fb80 62 65 20 6c 61 6f 64 65 64 20 69 6e 74 6f 20 69  be laoded into i
1fb81 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62  nternal hash tab
1fb82 6c 65 73 20 77 68 65 72 65 20 69 73 20 63 61 6e  les where is can
1fb83 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61   be used..*/.sta
1fb84 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 41 6e 61  tic void loadAna
1fb85 6c 79 73 69 73 28 50 61 72 73 65 20 2a 70 50 61  lysis(Parse *pPa
1fb86 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20  rse, int iDb){. 
1fb87 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
1fb88 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1fb89 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
1fb8a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fb8b 4f 70 31 28 76 2c 20 4f 50 5f 4c 6f 61 64 41 6e  Op1(v, OP_LoadAn
1fb8c 61 6c 79 73 69 73 2c 20 69 44 62 29 3b 0a 20 20  alysis, iDb);.  
1fb8d 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
1fb8e 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
1fb8f 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69  ll do an analysi
1fb90 73 20 6f 66 20 61 6e 20 65 6e 74 69 72 65 20 64  s of an entire d
1fb91 61 74 61 62 61 73 65 0a 2a 2f 0a 73 74 61 74 69  atabase.*/.stati
1fb92 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 44 61  c void analyzeDa
1fb93 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 70 50  tabase(Parse *pP
1fb94 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a  arse, int iDb){.
1fb95 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1fb96 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 53 63  pParse->db;.  Sc
1fb97 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20  hema *pSchema = 
1fb98 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
1fb99 68 65 6d 61 3b 20 20 20 20 2f 2a 20 53 63 68 65  hema;    /* Sche
1fb9a 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 69  ma of database i
1fb9b 44 62 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  Db */.  HashElem
1fb9c 20 2a 6b 3b 0a 20 20 69 6e 74 20 69 53 74 61 74   *k;.  int iStat
1fb9d 43 75 72 3b 0a 20 20 69 6e 74 20 69 4d 65 6d 3b  Cur;.  int iMem;
1fb9e 0a 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  ..  sqlite3Begin
1fb9f 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
1fba0 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
1fba1 20 20 69 53 74 61 74 43 75 72 20 3d 20 70 50 61    iStatCur = pPa
1fba2 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 6f  rse->nTab++;.  o
1fba3 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50 61  penStatTable(pPa
1fba4 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74 43  rse, iDb, iStatC
1fba5 75 72 2c 20 30 29 3b 0a 20 20 69 4d 65 6d 20 3d  ur, 0);.  iMem =
1fba6 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b   pParse->nMem+1;
1fba7 0a 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74 65 48  .  for(k=sqliteH
1fba8 61 73 68 46 69 72 73 74 28 26 70 53 63 68 65 6d  ashFirst(&pSchem
1fba9 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 6b 3b 20  a->tblHash); k; 
1fbaa 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  k=sqliteHashNext
1fbab 28 6b 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20  (k)){.    Table 
1fbac 2a 70 54 61 62 20 3d 20 28 54 61 62 6c 65 2a 29  *pTab = (Table*)
1fbad 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 6b  sqliteHashData(k
1fbae 29 3b 0a 20 20 20 20 61 6e 61 6c 79 7a 65 4f 6e  );.    analyzeOn
1fbaf 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  eTable(pParse, p
1fbb0 54 61 62 2c 20 69 53 74 61 74 43 75 72 2c 20 69  Tab, iStatCur, i
1fbb1 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 6c 6f 61 64  Mem);.  }.  load
1fbb2 41 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c  Analysis(pParse,
1fbb3 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   iDb);.}../*.** 
1fbb4 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
1fbb5 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e  at will do an an
1fbb6 61 6c 79 73 69 73 20 6f 66 20 61 20 73 69 6e 67  alysis of a sing
1fbb7 6c 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 61  le table in.** a
1fbb8 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74   database..*/.st
1fbb9 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a  atic void analyz
1fbba 65 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  eTable(Parse *pP
1fbbb 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
1fbbc 62 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20  b){.  int iDb;. 
1fbbd 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 0a   int iStatCur;..
1fbbe 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d    assert( pTab!=
1fbbf 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
1fbc0 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
1fbc1 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73  AllMutexes(pPars
1fbc2 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44 62 20  e->db) );.  iDb 
1fbc3 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
1fbc4 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
1fbc5 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
1fbc6 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  );.  sqlite3Begi
1fbc7 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
1fbc8 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
1fbc9 0a 20 20 69 53 74 61 74 43 75 72 20 3d 20 70 50  .  iStatCur = pP
1fbca 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
1fbcb 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50  openStatTable(pP
1fbcc 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74  arse, iDb, iStat
1fbcd 43 75 72 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  Cur, pTab->zName
1fbce 29 3b 0a 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54  );.  analyzeOneT
1fbcf 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
1fbd0 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 50 61  b, iStatCur, pPa
1fbd1 72 73 65 2d 3e 6e 4d 65 6d 2b 31 29 3b 0a 20 20  rse->nMem+1);.  
1fbd2 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61  loadAnalysis(pPa
1fbd3 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a  rse, iDb);.}../*
1fbd4 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1fbd5 65 20 66 6f 72 20 74 68 65 20 41 4e 41 4c 59 5a  e for the ANALYZ
1fbd6 45 20 63 6f 6d 6d 61 6e 64 2e 20 20 54 68 65 20  E command.  The 
1fbd7 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
1fbd8 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 68 65  s routine.** whe
1fbd9 6e 20 69 74 20 72 65 63 6f 67 6e 69 7a 65 73 20  n it recognizes 
1fbda 61 6e 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  an ANALYZE comma
1fbdb 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  nd..**.**       
1fbdc 20 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20   ANALYZE        
1fbdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbde 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20      -- 1.**     
1fbdf 20 20 20 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74     ANALYZE  <dat
1fbe0 61 62 61 73 65 3e 20 20 20 20 20 20 20 20 20 20  abase>          
1fbe1 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20        -- 2.**   
1fbe2 20 20 20 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c       ANALYZE  ?<
1fbe3 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c  database>.?<tabl
1fbe4 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a  ename>  -- 3.**.
1fbe5 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73  ** Form 1 causes
1fbe6 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20   all indices in 
1fbe7 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
1fbe8 61 62 61 73 65 73 20 74 6f 20 62 65 20 61 6e 61  abases to be ana
1fbe9 6c 79 7a 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32  lyzed..** Form 2
1fbea 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e   analyzes all in
1fbeb 64 69 63 65 73 20 74 68 65 20 73 69 6e 67 6c 65  dices the single
1fbec 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 2e   database named.
1fbed 0a 2a 2a 20 46 6f 72 6d 20 33 20 61 6e 61 6c 79  .** Form 3 analy
1fbee 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  zes all indices 
1fbef 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1fbf0 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e  the named table.
1fbf1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1fbf2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
1fbf3 6e 61 6c 79 7a 65 28 50 61 72 73 65 20 2a 70 50  nalyze(Parse *pP
1fbf4 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  arse, Token *pNa
1fbf5 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  me1, Token *pNam
1fbf6 65 32 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  e2){.  sqlite3 *
1fbf7 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1fbf8 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e  .  int iDb;.  in
1fbf9 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20  t i;.  char *z, 
1fbfa 2a 7a 44 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70  *zDb;.  Table *p
1fbfb 54 61 62 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  Tab;.  Token *pT
1fbfc 61 62 6c 65 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20  ableName;..  /* 
1fbfd 52 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73  Read the databas
1fbfe 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20  e schema. If an 
1fbff 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65  error occurs, le
1fc00 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
1fc01 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f  sage.  ** and co
1fc02 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64  de in pParse and
1fc03 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f   return NULL. */
1fc04 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1fc05 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
1fc06 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64  utexes(pParse->d
1fc07 62 29 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  b) );.  if( SQLI
1fc08 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65  TE_OK!=sqlite3Re
1fc09 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
1fc0a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
1fc0b 20 20 7d 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65    }..  if( pName
1fc0c 31 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46  1==0 ){.    /* F
1fc0d 6f 72 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20  orm 1:  Analyze 
1fc0e 65 76 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20  everything */.  
1fc0f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
1fc10 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
1fc11 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e    if( i==1 ) con
1fc12 74 69 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f  tinue;  /* Do no
1fc13 74 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 54 45  t analyze the TE
1fc14 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  MP database */. 
1fc15 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61       analyzeData
1fc16 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 29 3b  base(pParse, i);
1fc17 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
1fc18 66 28 20 70 4e 61 6d 65 32 3d 3d 30 20 7c 7c 20  f( pName2==0 || 
1fc19 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29 7b 0a  pName2->n==0 ){.
1fc1a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32 3a 20 20      /* Form 2:  
1fc1b 41 6e 61 6c 79 7a 65 20 74 68 65 20 64 61 74 61  Analyze the data
1fc1c 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6e 61  base or table na
1fc1d 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d  med */.    iDb =
1fc1e 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64   sqlite3FindDb(d
1fc1f 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20  b, pName1);.    
1fc20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20  if( iDb>=0 ){.  
1fc21 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62      analyzeDatab
1fc22 61 73 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  ase(pParse, iDb)
1fc23 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1fc24 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61     z = sqlite3Na
1fc25 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
1fc26 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 69  pName1);.      i
1fc27 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  f( z ){.        
1fc28 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pTab = sqlite3Lo
1fc29 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
1fc2a 2c 20 30 2c 20 7a 2c 20 30 29 3b 0a 20 20 20 20  , 0, z, 0);.    
1fc2b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1fc2c 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20  e(db, z);.      
1fc2d 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
1fc2e 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 54          analyzeT
1fc2f 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
1fc30 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  b);.        }.  
1fc31 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
1fc32 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d  lse{.    /* Form
1fc33 20 33 3a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20   3: Analyze the 
1fc34 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 20  fully qualified 
1fc35 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  table name */.  
1fc36 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54    iDb = sqlite3T
1fc37 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73  woPartName(pPars
1fc38 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65  e, pName1, pName
1fc39 32 2c 20 26 70 54 61 62 6c 65 4e 61 6d 65 29 3b  2, &pTableName);
1fc3a 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20  .    if( iDb>=0 
1fc3b 29 7b 0a 20 20 20 20 20 20 7a 44 62 20 3d 20 64  ){.      zDb = d
1fc3c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1fc3d 65 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c  e;.      z = sql
1fc3e 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
1fc3f 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65  n(db, pTableName
1fc40 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29  );.      if( z )
1fc41 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d  {.        pTab =
1fc42 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
1fc43 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a  ble(pParse, 0, z
1fc44 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20  , zDb);.        
1fc45 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1fc46 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , z);.        if
1fc47 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ( pTab ){.      
1fc48 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65      analyzeTable
1fc49 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
1fc4a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1fc4b 7d 0a 20 20 20 20 7d 20 20 20 0a 20 20 7d 0a 7d  }.    }   .  }.}
1fc4c 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 64 20 74 6f 20  ../*.** Used to 
1fc4d 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  pass information
1fc4e 20 66 72 6f 6d 20 74 68 65 20 61 6e 61 6c 79 7a   from the analyz
1fc4f 65 72 20 72 65 61 64 65 72 20 74 68 72 6f 75 67  er reader throug
1fc50 68 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  h to the.** call
1fc51 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f  back routine..*/
1fc52 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1fc53 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 61 6e 61  analysisInfo ana
1fc54 6c 79 73 69 73 49 6e 66 6f 3b 0a 73 74 72 75 63  lysisInfo;.struc
1fc55 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 7b  t analysisInfo {
1fc56 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1fc57 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
1fc58 61 74 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a  atabase;.};../*.
1fc59 2a 2a 20 54 68 69 73 20 63 61 6c 6c 62 61 63 6b  ** This callback
1fc5a 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65   is invoked once
1fc5b 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20   for each index 
1fc5c 77 68 65 6e 20 72 65 61 64 69 6e 67 20 74 68 65  when reading the
1fc5d 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74 61 74 31  .** sqlite_stat1
1fc5e 20 74 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a 20   table.  .**.** 
1fc5f 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61      argv[0] = na
1fc60 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a  me of the index.
1fc61 2a 2a 20 20 20 20 20 61 72 67 76 5b 31 5d 20 3d  **     argv[1] =
1fc62 20 72 65 73 75 6c 74 73 20 6f 66 20 61 6e 61 6c   results of anal
1fc63 79 73 69 73 20 2d 20 6f 6e 20 69 6e 74 65 67 65  ysis - on intege
1fc64 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  r for each colum
1fc65 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n.*/.static int 
1fc66 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 28 76  analysisLoader(v
1fc67 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  oid *pData, int 
1fc68 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67  argc, char **arg
1fc69 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55 73 65  v, char **NotUse
1fc6a 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e  d){.  analysisIn
1fc6b 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61 6e 61  fo *pInfo = (ana
1fc6c 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44 61 74 61  lysisInfo*)pData
1fc6d 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  ;.  Index *pInde
1fc6e 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20  x;.  int i, c;. 
1fc6f 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 76 3b   unsigned int v;
1fc70 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1fc71 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ;..  assert( arg
1fc72 63 3d 3d 32 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==2 );.  UNUSED
1fc73 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
1fc74 73 65 64 2c 20 61 72 67 63 29 3b 0a 0a 20 20 69  sed, argc);..  i
1fc75 66 28 20 61 72 67 76 3d 3d 30 20 7c 7c 20 61 72  f( argv==0 || ar
1fc76 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20 61 72 67 76  gv[0]==0 || argv
1fc77 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  [1]==0 ){.    re
1fc78 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49  turn 0;.  }.  pI
1fc79 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69  ndex = sqlite3Fi
1fc7a 6e 64 49 6e 64 65 78 28 70 49 6e 66 6f 2d 3e 64  ndIndex(pInfo->d
1fc7b 62 2c 20 61 72 67 76 5b 30 5d 2c 20 70 49 6e 66  b, argv[0], pInf
1fc7c 6f 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  o->zDatabase);. 
1fc7d 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29   if( pIndex==0 )
1fc7e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
1fc7f 20 20 7d 0a 20 20 7a 20 3d 20 61 72 67 76 5b 31    }.  z = argv[1
1fc80 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 2a 7a  ];.  for(i=0; *z
1fc81 20 26 26 20 69 3c 3d 70 49 6e 64 65 78 2d 3e 6e   && i<=pIndex->n
1fc82 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
1fc83 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 77 68 69    v = 0;.    whi
1fc84 6c 65 28 20 28 63 3d 7a 5b 30 5d 29 3e 3d 27 30  le( (c=z[0])>='0
1fc85 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20  ' && c<='9' ){. 
1fc86 20 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20       v = v*10 + 
1fc87 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a  c - '0';.      z
1fc88 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49  ++;.    }.    pI
1fc89 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b 69  ndex->aiRowEst[i
1fc8a 5d 20 3d 20 76 3b 0a 20 20 20 20 69 66 28 20 2a  ] = v;.    if( *
1fc8b 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b 3b 0a 20 20  z==' ' ) z++;.  
1fc8c 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
1fc8d 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 74 68 65 20  ./*.** Load the 
1fc8e 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 73  content of the s
1fc8f 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
1fc90 65 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78  e into the index
1fc91 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a 2f   hash tables..*/
1fc92 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1fc93 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79  int sqlite3Analy
1fc94 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 20  sisLoad(sqlite3 
1fc95 2a 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20  *db, int iDb){. 
1fc96 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 73 49   analysisInfo sI
1fc97 6e 66 6f 3b 0a 20 20 48 61 73 68 45 6c 65 6d 20  nfo;.  HashElem 
1fc98 2a 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  *i;.  char *zSql
1fc99 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
1fc9a 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
1fc9b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
1fc9c 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
1fc9d 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b  b[iDb].pBt!=0 );
1fc9e 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1fc9f 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
1fca0 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  x(db->aDb[iDb].p
1fca1 42 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65  Bt) );..  /* Cle
1fca2 61 72 20 61 6e 79 20 70 72 69 6f 72 20 73 74 61  ar any prior sta
1fca3 74 69 73 74 69 63 73 20 2a 2f 0a 20 20 66 6f 72  tistics */.  for
1fca4 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (i=sqliteHashFir
1fca5 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d  st(&db->aDb[iDb]
1fca6 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73  .pSchema->idxHas
1fca7 68 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48 61 73  h);i;i=sqliteHas
1fca8 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 49  hNext(i)){.    I
1fca9 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c  ndex *pIdx = sql
1fcaa 69 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a  iteHashData(i);.
1fcab 20 20 20 20 73 71 6c 69 74 65 33 44 65 66 61 75      sqlite3Defau
1fcac 6c 74 52 6f 77 45 73 74 28 70 49 64 78 29 3b 0a  ltRowEst(pIdx);.
1fcad 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
1fcae 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
1fcaf 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
1fcb0 62 6c 65 20 65 78 69 73 74 73 73 20 2a 2f 0a 20  ble existss */. 
1fcb1 20 73 49 6e 66 6f 2e 64 62 20 3d 20 64 62 3b 0a   sInfo.db = db;.
1fcb2 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73    sInfo.zDatabas
1fcb3 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  e = db->aDb[iDb]
1fcb4 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71  .zName;.  if( sq
1fcb5 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
1fcb6 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31  b, "sqlite_stat1
1fcb7 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61  ", sInfo.zDataba
1fcb8 73 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 72  se)==0 ){.     r
1fcb9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1fcba 4f 52 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 4c  OR;.  }...  /* L
1fcbb 6f 61 64 20 6e 65 77 20 73 74 61 74 69 73 74 69  oad new statisti
1fcbc 63 73 20 6f 75 74 20 6f 66 20 74 68 65 20 73 71  cs out of the sq
1fcbd 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
1fcbe 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c   */.  zSql = sql
1fcbf 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
1fcc0 22 53 45 4c 45 43 54 20 69 64 78 2c 20 73 74 61  "SELECT idx, sta
1fcc1 74 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65  t FROM %Q.sqlite
1fcc2 5f 73 74 61 74 31 22 2c 0a 20 20 20 20 20 20 20  _stat1",.       
1fcc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcc4 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65   sInfo.zDatabase
1fcc5 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  );.  (void)sqlit
1fcc6 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
1fcc7 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
1fcc8 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 61  exec(db, zSql, a
1fcc9 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 2c 20 26  nalysisLoader, &
1fcca 73 49 6e 66 6f 2c 20 30 29 3b 0a 20 20 28 76 6f  sInfo, 0);.  (vo
1fccb 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
1fccc 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  On(db);.  sqlite
1fccd 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c  3DbFree(db, zSql
1fcce 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1fccf 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  }...#endif /* SQ
1fcd0 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a  LITE_OMIT_ANALYZ
1fcd1 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
1fcd2 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6e 61  ***** End of ana
1fcd3 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lyze.c *********
1fcd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fcd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fcd6 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1fcd7 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1fcd8 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a   attach.c ******
1fcd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fcda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fcdb 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33  ****/./*.** 2003
1fcdc 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54   April 6.**.** T
1fcdd 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1fcde 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1fcdf 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1fce0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1fce1 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1fce2 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1fce3 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1fce4 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1fce5 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1fce6 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1fce7 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1fce8 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1fce9 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1fcea 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1fceb 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1fcec 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1fced 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1fcee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fcef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fcf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fcf2 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
1fcf3 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
1fcf4 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
1fcf5 6d 65 6e 74 20 74 68 65 20 41 54 54 41 43 48 20  ment the ATTACH 
1fcf6 61 6e 64 20 44 45 54 41 43 48 20 63 6f 6d 6d 61  and DETACH comma
1fcf7 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  nds..**.** $Id: 
1fcf8 61 74 74 61 63 68 2e 63 2c 76 20 31 2e 38 31 20  attach.c,v 1.81 
1fcf9 32 30 30 38 2f 31 32 2f 31 30 20 31 36 3a 34 35  2008/12/10 16:45
1fcfa 3a 35 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :51 drh Exp $.*/
1fcfb 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1fcfc 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a  _OMIT_ATTACH./*.
1fcfd 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6e 20 65 78  ** Resolve an ex
1fcfe 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 61  pression that wa
1fcff 73 20 70 61 72 74 20 6f 66 20 61 6e 20 41 54 54  s part of an ATT
1fd00 41 43 48 20 6f 72 20 44 45 54 41 43 48 20 73 74  ACH or DETACH st
1fd01 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a  atement. This.**
1fd02 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66   is slightly dif
1fd03 66 65 72 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f  ferent from reso
1fd04 6c 76 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53  lving a normal S
1fd05 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62  QL expression, b
1fd06 65 63 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a  ecause simple.**
1fd07 20 69 64 65 6e 74 69 66 69 65 72 73 20 61 72 65   identifiers are
1fd08 20 74 72 65 61 74 65 64 20 61 73 20 73 74 72 69   treated as stri
1fd09 6e 67 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c  ngs, not possibl
1fd0a 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f  e column names o
1fd0b 72 20 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a  r aliases..**.**
1fd0c 20 69 2e 65 2e 20 69 66 20 74 68 65 20 70 61 72   i.e. if the par
1fd0d 73 65 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20  ser sees:.**.** 
1fd0e 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42      ATTACH DATAB
1fd0f 41 53 45 20 61 62 63 20 41 53 20 64 65 66 0a 2a  ASE abc AS def.*
1fd10 2a 0a 2a 2a 20 69 74 20 74 72 65 61 74 73 20 74  *.** it treats t
1fd11 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f  he two expressio
1fd12 6e 73 20 61 73 20 6c 69 74 65 72 61 6c 20 73 74  ns as literal st
1fd13 72 69 6e 67 73 20 27 61 62 63 27 20 61 6e 64 20  rings 'abc' and 
1fd14 27 64 65 66 27 20 69 6e 73 74 65 61 64 20 6f 66  'def' instead of
1fd15 0a 2a 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  .** looking for 
1fd16 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73  columns of the s
1fd17 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  ame name..**.** 
1fd18 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65  This only applie
1fd19 73 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f  s to the root no
1fd1a 64 65 20 6f 66 20 70 45 78 70 72 2c 20 73 6f 20  de of pExpr, so 
1fd1b 74 68 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a  the statement:.*
1fd1c 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20  *.**     ATTACH 
1fd1d 44 41 54 41 42 41 53 45 20 61 62 63 7c 7c 64 65  DATABASE abc||de
1fd1e 66 20 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a  f AS 'db2'.**.**
1fd1f 20 77 69 6c 6c 20 66 61 69 6c 20 62 65 63 61 75   will fail becau
1fd20 73 65 20 6e 65 69 74 68 65 72 20 61 62 63 20 6f  se neither abc o
1fd21 72 20 64 65 66 20 63 61 6e 20 62 65 20 72 65 73  r def can be res
1fd22 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  olved..*/.static
1fd23 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 74 74 61   int resolveAtta
1fd24 63 68 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65  chExpr(NameConte
1fd25 78 74 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20  xt *pName, Expr 
1fd26 2a 70 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20  *pExpr).{.  int 
1fd27 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1fd28 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20    if( pExpr ){. 
1fd29 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70     if( pExpr->op
1fd2a 21 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20  !=TK_ID ){.     
1fd2b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73   rc = sqlite3Res
1fd2c 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e  olveExprNames(pN
1fd2d 61 6d 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  ame, pExpr);.   
1fd2e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1fd2f 45 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74 65 33  E_OK && !sqlite3
1fd30 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70  ExprIsConstant(p
1fd31 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20  Expr) ){.       
1fd32 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1fd33 28 70 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20  (pName->pParse, 
1fd34 22 69 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c  "invalid name: \
1fd35 22 25 54 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e  "%T\"", &pExpr->
1fd36 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 72  span);.        r
1fd37 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1fd38 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OR;.      }.    
1fd39 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78  }else{.      pEx
1fd3a 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49  pr->op = TK_STRI
1fd3b 4e 47 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  NG;.    }.  }.  
1fd3c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1fd3d 0a 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d  .** An SQL user-
1fd3e 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65  function registe
1fd3f 72 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f  red to do the wo
1fd40 72 6b 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20  rk of an ATTACH 
1fd41 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a  statement. The.*
1fd42 2a 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74  * three argument
1fd43 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  s to the functio
1fd44 6e 20 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20  n come directly 
1fd45 66 72 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73  from an attach s
1fd46 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20  tatement:.**.** 
1fd47 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42      ATTACH DATAB
1fd48 41 53 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a  ASE x AS y KEY z
1fd49 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
1fd4a 54 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28  T sqlite_attach(
1fd4b 78 2c 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49  x, y, z).**.** I
1fd4c 66 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22  f the optional "
1fd4d 4b 45 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73  KEY z" syntax is
1fd4e 20 6f 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c   omitted, an SQL
1fd4f 20 4e 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20   NULL is passed 
1fd50 61 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20  as the.** third 
1fd51 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61  argument..*/.sta
1fd52 74 69 63 20 76 6f 69 64 20 61 74 74 61 63 68 46  tic void attachF
1fd53 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
1fd54 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
1fd55 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
1fd56 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1fd57 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
1fd58 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b  i;.  int rc = 0;
1fd59 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1fd5a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1fd5b 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
1fd5c 78 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  xt);.  const cha
1fd5d 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73  r *zName;.  cons
1fd5e 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20  t char *zFile;. 
1fd5f 20 44 62 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61   Db *aNew;.  cha
1fd60 72 20 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a  r *zErrDyn = 0;.
1fd61 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d    char zErr[128]
1fd62 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
1fd63 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
1fd64 0a 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73  .  zFile = (cons
1fd65 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
1fd66 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
1fd67 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  [0]);.  zName = 
1fd68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
1fd69 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1fd6a 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28  (argv[1]);.  if(
1fd6b 20 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c   zFile==0 ) zFil
1fd6c 65 20 3d 20 22 22 3b 0a 20 20 69 66 28 20 7a 4e  e = "";.  if( zN
1fd6d 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d  ame==0 ) zName =
1fd6e 20 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b   "";..  /* Check
1fd6f 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69   for the followi
1fd70 6e 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a  ng errors:.  **.
1fd71 20 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f 20 6d    **     * Too m
1fd72 61 6e 79 20 61 74 74 61 63 68 65 64 20 64 61 74  any attached dat
1fd73 61 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20 20 20  abases,.  **    
1fd74 20 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 63   * Transaction c
1fd75 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a 20 20  urrently open.  
1fd76 2a 2a 20 20 20 20 20 2a 20 53 70 65 63 69 66 69  **     * Specifi
1fd77 65 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  ed database name
1fd78 20 61 6c 72 65 61 64 79 20 62 65 69 6e 67 20 75   already being u
1fd79 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  sed..  */.  if( 
1fd7a 64 62 2d 3e 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69  db->nDb>=db->aLi
1fd7b 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
1fd7c 5f 41 54 54 41 43 48 45 44 5d 2b 32 20 29 7b 0a  _ATTACHED]+2 ){.
1fd7d 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1fd7e 69 6e 74 66 28 0a 20 20 20 20 20 20 73 69 7a 65  intf(.      size
1fd7f 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20  of(zErr), zErr, 
1fd80 22 74 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68  "too many attach
1fd81 65 64 20 64 61 74 61 62 61 73 65 73 20 2d 20 6d  ed databases - m
1fd82 61 78 20 25 64 22 2c 20 0a 20 20 20 20 20 20 64  ax %d", .      d
1fd83 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
1fd84 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d  _LIMIT_ATTACHED]
1fd85 0a 20 20 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f  .    );.    goto
1fd86 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
1fd87 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75   }.  if( !db->au
1fd88 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20  toCommit ){.    
1fd89 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1fd8a 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1fd8b 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Err,.           
1fd8c 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f            "canno
1fd8d 74 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73  t ATTACH databas
1fd8e 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63  e within transac
1fd8f 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f  tion");.    goto
1fd90 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
1fd91 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
1fd92 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
1fd93 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d     char *z = db-
1fd94 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20  >aDb[i].zName;. 
1fd95 20 20 20 69 66 28 20 7a 20 26 26 20 7a 4e 61 6d     if( z && zNam
1fd96 65 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  e && sqlite3StrI
1fd97 43 6d 70 28 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30  Cmp(z, zName)==0
1fd98 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1fd99 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1fd9a 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 0a  f(zErr), zErr, .
1fd9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fd9c 20 20 20 20 20 20 20 22 64 61 74 61 62 61 73 65         "database
1fd9d 20 25 73 20 69 73 20 61 6c 72 65 61 64 79 20 69   %s is already i
1fd9e 6e 20 75 73 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a  n use", zName);.
1fd9f 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63        goto attac
1fda0 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  h_error;.    }. 
1fda1 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74   }..  /* Allocat
1fda2 65 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20  e the new entry 
1fda3 69 6e 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d  in the db->aDb[]
1fda4 20 61 72 72 61 79 20 61 6e 64 20 69 6e 69 74 69   array and initi
1fda5 61 6c 69 73 65 20 74 68 65 20 73 63 68 65 6d 61  alise the schema
1fda6 0a 20 20 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  .  ** hash table
1fda7 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  s..  */.  if( db
1fda8 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74  ->aDb==db->aDbSt
1fda9 61 74 69 63 20 29 7b 0a 20 20 20 20 61 4e 65 77  atic ){.    aNew
1fdaa 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1fdab 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
1fdac 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29  (db->aDb[0])*3 )
1fdad 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d  ;.    if( aNew==
1fdae 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
1fdaf 6d 65 6d 63 70 79 28 61 4e 65 77 2c 20 64 62 2d  memcpy(aNew, db-
1fdb0 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d  >aDb, sizeof(db-
1fdb1 3e 61 44 62 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d  >aDb[0])*2);.  }
1fdb2 65 6c 73 65 7b 0a 20 20 20 20 61 4e 65 77 20 3d  else{.    aNew =
1fdb3 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
1fdb4 63 28 64 62 2c 20 64 62 2d 3e 61 44 62 2c 20 73  c(db, db->aDb, s
1fdb5 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d  izeof(db->aDb[0]
1fdb6 29 2a 28 64 62 2d 3e 6e 44 62 2b 31 29 20 29 3b  )*(db->nDb+1) );
1fdb7 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30  .    if( aNew==0
1fdb8 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20   ) return;.  }. 
1fdb9 20 64 62 2d 3e 61 44 62 20 3d 20 61 4e 65 77 3b   db->aDb = aNew;
1fdba 0a 20 20 61 4e 65 77 20 3d 20 26 64 62 2d 3e 61  .  aNew = &db->a
1fdbb 44 62 5b 64 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20  Db[db->nDb++];. 
1fdbc 20 6d 65 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c   memset(aNew, 0,
1fdbd 20 73 69 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b   sizeof(*aNew));
1fdbe 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
1fdbf 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
1fdc0 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 73  f the btree is s
1fdc1 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e  uccessfully open
1fdc2 65 64 2c 20 75 73 65 0a 20 20 2a 2a 20 69 74 20  ed, use.  ** it 
1fdc3 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 64 61  to obtain the da
1fdc4 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 41  tabase schema. A
1fdc5 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
1fdc6 20 73 63 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a   schema may.  **
1fdc7 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 69   or may not be i
1fdc8 6e 69 74 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f  nitialised..  */
1fdc9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
1fdca 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20  treeFactory(db, 
1fdcb 7a 46 69 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45  zFile, 0, SQLITE
1fdcc 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1fdcd 49 5a 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20  IZE,.           
1fdce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdcf 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20  db->openFlags | 
1fdd0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
1fdd1 5f 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20  _DB,.           
1fdd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdd3 26 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 69  &aNew->pBt);.  i
1fdd4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1fdd5 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70   ){.    Pager *p
1fdd6 50 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d  Pager;.    aNew-
1fdd7 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74  >pSchema = sqlit
1fdd8 65 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20  e3SchemaGet(db, 
1fdd9 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20  aNew->pBt);.    
1fdda 69 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65  if( !aNew->pSche
1fddb 6d 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ma ){.      rc =
1fddc 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1fddd 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65     }else if( aNe
1fdde 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  w->pSchema->file
1fddf 5f 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d  _format && aNew-
1fde0 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45  >pSchema->enc!=E
1fde1 4e 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20  NC(db) ){.      
1fde2 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1fde3 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1fde4 45 72 72 2c 20 0a 20 20 20 20 20 20 20 20 22 61  Err, .        "a
1fde5 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
1fde6 73 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73  s must use the s
1fde7 61 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  ame text encodin
1fde8 67 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61  g as main databa
1fde9 73 65 22 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  se");.      goto
1fdea 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
1fdeb 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 20     }.    pPager 
1fdec 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
1fded 67 65 72 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a  ger(aNew->pBt);.
1fdee 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
1fdef 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67  LockingMode(pPag
1fdf0 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b  er, db->dfltLock
1fdf1 4d 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Mode);.    sqlit
1fdf2 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f  e3PagerJournalMo
1fdf3 64 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64  de(pPager, db->d
1fdf4 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b  fltJournalMode);
1fdf5 0a 20 20 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61  .  }.  aNew->zNa
1fdf6 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
1fdf7 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b  rDup(db, zName);
1fdf8 0a 20 20 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f  .  aNew->safety_
1fdf9 6c 65 76 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20  level = 3;..#if 
1fdfa 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
1fdfb 0a 20 20 7b 0a 20 20 20 20 65 78 74 65 72 6e 20  .  {.    extern 
1fdfc 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 63  int sqlite3Codec
1fdfd 41 74 74 61 63 68 28 73 71 6c 69 74 65 33 2a 2c  Attach(sqlite3*,
1fdfe 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64   int, const void
1fdff 2a 2c 20 69 6e 74 29 3b 0a 20 20 20 20 65 78 74  *, int);.    ext
1fe00 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ern void sqlite3
1fe01 43 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69  CodecGetKey(sqli
1fe02 74 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a  te3*, int, void*
1fe03 2a 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e  *, int*);.    in
1fe04 74 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72  t nKey;.    char
1fe05 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20   *zKey;.    int 
1fe06 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
1fe07 65 5f 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3b  e_type(argv[2]);
1fe08 0a 20 20 20 20 73 77 69 74 63 68 28 20 74 20 29  .    switch( t )
1fe09 7b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c  {.      case SQL
1fe0a 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20  ITE_INTEGER:.   
1fe0b 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
1fe0c 4c 4f 41 54 3a 0a 20 20 20 20 20 20 20 20 7a 45  LOAT:.        zE
1fe0d 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 44  rrDyn = sqlite3D
1fe0e 62 53 74 72 44 75 70 28 64 62 2c 20 22 49 6e 76  bStrDup(db, "Inv
1fe0f 61 6c 69 64 20 6b 65 79 20 76 61 6c 75 65 22 29  alid key value")
1fe10 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ;.        rc = S
1fe11 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1fe12 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1fe13 20 20 20 20 0a 20 20 20 20 20 20 63 61 73 65 20      .      case 
1fe14 53 51 4c 49 54 45 5f 54 45 58 54 3a 0a 20 20 20  SQLITE_TEXT:.   
1fe15 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42     case SQLITE_B
1fe16 4c 4f 42 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65  LOB:.        nKe
1fe17 79 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  y = sqlite3_valu
1fe18 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d 29  e_bytes(argv[2])
1fe19 3b 0a 20 20 20 20 20 20 20 20 7a 4b 65 79 20 3d  ;.        zKey =
1fe1a 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
1fe1b 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76  _value_blob(argv
1fe1c 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 73 71  [2]);.        sq
1fe1d 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68  lite3CodecAttach
1fe1e 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20  (db, db->nDb-1, 
1fe1f 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20  zKey, nKey);.   
1fe20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20       break;..   
1fe21 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e     case SQLITE_N
1fe22 55 4c 4c 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20  ULL:.        /* 
1fe23 4e 6f 20 6b 65 79 20 73 70 65 63 69 66 69 65 64  No key specified
1fe24 2e 20 20 55 73 65 20 74 68 65 20 6b 65 79 20 66  .  Use the key f
1fe25 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74  rom the main dat
1fe26 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 20  abase */.       
1fe27 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74   sqlite3CodecGet
1fe28 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64  Key(db, 0, (void
1fe29 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29  **)&zKey, &nKey)
1fe2a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1fe2b 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c  3CodecAttach(db,
1fe2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79   db->nDb-1, zKey
1fe2d 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  , nKey);.       
1fe2e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1fe2f 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
1fe30 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 6f  f the file was o
1fe31 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c  pened successful
1fe32 6c 79 2c 20 72 65 61 64 20 74 68 65 20 73 63 68  ly, read the sch
1fe33 65 6d 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ema for the new 
1fe34 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49  database..  ** I
1fe35 66 20 74 68 69 73 20 66 61 69 6c 73 2c 20 6f 72  f this fails, or
1fe36 20 69 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20   if opening the 
1fe37 66 69 6c 65 20 66 61 69 6c 65 64 2c 20 74 68 65  file failed, the
1fe38 6e 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  n close the file
1fe39 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76   and .  ** remov
1fe3a 65 20 74 68 65 20 65 6e 74 72 79 20 66 72 6f 6d  e the entry from
1fe3b 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61   the db->aDb[] a
1fe3c 72 72 61 79 2e 20 69 2e 65 2e 20 70 75 74 20 65  rray. i.e. put e
1fe3d 76 65 72 79 74 68 69 6e 67 20 62 61 63 6b 20 74  verything back t
1fe3e 68 65 20 77 61 79 0a 20 20 2a 2a 20 77 65 20 66  he way.  ** we f
1fe3f 6f 75 6e 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20  ound it..  */.  
1fe40 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1fe41 4b 20 29 7b 0a 20 20 20 20 28 76 6f 69 64 29 73  K ){.    (void)s
1fe42 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1fe43 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  b);.    sqlite3B
1fe44 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29  treeEnterAll(db)
1fe45 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1fe46 65 33 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72  e3Init(db, &zErr
1fe47 44 79 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Dyn);.    sqlite
1fe48 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
1fe49 62 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71  b);.    (void)sq
1fe4a 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1fe4b 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  b);.  }.  if( rc
1fe4c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20   ){.    int iDb 
1fe4d 3d 20 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20  = db->nDb - 1;. 
1fe4e 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d     assert( iDb>=
1fe4f 32 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  2 );.    if( db-
1fe50 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b  >aDb[iDb].pBt ){
1fe51 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
1fe52 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62  reeClose(db->aDb
1fe53 5b 69 44 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20  [iDb].pBt);.    
1fe54 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70    db->aDb[iDb].p
1fe55 42 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 62  Bt = 0;.      db
1fe56 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
1fe57 6d 61 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ma = 0;.    }.  
1fe58 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
1fe59 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
1fe5a 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62   0);.    db->nDb
1fe5b 20 3d 20 69 44 62 3b 0a 20 20 20 20 69 66 28 20   = iDb;.    if( 
1fe5c 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
1fe5d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
1fe5e 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  OERR_NOMEM ){.  
1fe5f 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
1fe60 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
1fe61 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1fe62 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45  (sizeof(zErr),zE
1fe63 72 72 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  rr, "out of memo
1fe64 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ry");.    }else{
1fe65 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1fe66 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
1fe67 45 72 72 29 2c 7a 45 72 72 2c 20 22 75 6e 61 62  Err),zErr, "unab
1fe68 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61 62  le to open datab
1fe69 61 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c 65 29  ase: %s", zFile)
1fe6a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f  ;.    }.    goto
1fe6b 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
1fe6c 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 3b 0a   }.  .  return;.
1fe6d 0a 61 74 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20  .attach_error:. 
1fe6e 20 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72   /* Return an er
1fe6f 72 6f 72 20 69 66 20 77 65 20 67 65 74 20 68 65  ror if we get he
1fe70 72 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45 72 72  re */.  if( zErr
1fe71 44 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Dyn ){.    sqlit
1fe72 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
1fe73 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79 6e  context, zErrDyn
1fe74 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , -1);.    sqlit
1fe75 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
1fe76 72 44 79 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rDyn);.  }else{.
1fe77 20 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28      zErr[sizeof(
1fe78 7a 45 72 72 29 2d 31 5d 20 3d 20 30 3b 0a 20 20  zErr)-1] = 0;.  
1fe79 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1fe7a 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
1fe7b 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20  zErr, -1);.  }. 
1fe7c 20 69 66 28 20 72 63 20 29 20 73 71 6c 69 74 65   if( rc ) sqlite
1fe7d 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
1fe7e 6f 64 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29  ode(context, rc)
1fe7f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51  ;.}../*.** An SQ
1fe80 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20  L user-function 
1fe81 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f  registered to do
1fe82 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20   the work of an 
1fe83 44 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74  DETACH statement
1fe84 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61  . The.** three a
1fe85 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
1fe86 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69  function come di
1fe87 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 20 64 65  rectly from a de
1fe88 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a  tach statement:.
1fe89 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48  **.**     DETACH
1fe8a 20 44 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a   DATABASE x.**.*
1fe8b 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c  *     SELECT sql
1fe8c 69 74 65 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f  ite_detach(x).*/
1fe8d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 74  .static void det
1fe8e 61 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  achFunc(.  sqlit
1fe8f 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
1fe90 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
1fe91 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
1fe92 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
1fe93 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1fe94 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
1fe95 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
1fe96 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
1fe97 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
1fe98 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
1fe99 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
1fe9a 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44 62  );.  int i;.  Db
1fe9b 20 2a 70 44 62 20 3d 20 30 3b 0a 20 20 63 68 61   *pDb = 0;.  cha
1fe9c 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20  r zErr[128];..  
1fe9d 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1fe9e 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66  (NotUsed);..  if
1fe9f 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61  ( zName==0 ) zNa
1fea0 6d 65 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69  me = "";.  for(i
1fea1 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1fea2 2b 2b 29 7b 0a 20 20 20 20 70 44 62 20 3d 20 26  ++){.    pDb = &
1fea3 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20  db->aDb[i];.    
1fea4 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20  if( pDb->pBt==0 
1fea5 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
1fea6 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1fea7 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  mp(pDb->zName, z
1fea8 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b  Name)==0 ) break
1fea9 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d  ;.  }..  if( i>=
1feaa 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73  db->nDb ){.    s
1feab 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1feac 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72  sizeof(zErr),zEr
1fead 72 2c 20 22 6e 6f 20 73 75 63 68 20 64 61 74 61  r, "no such data
1feae 62 61 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65  base: %s", zName
1feaf 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61  );.    goto deta
1feb0 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20  ch_error;.  }.  
1feb1 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 73  if( i<2 ){.    s
1feb2 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1feb3 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72  sizeof(zErr),zEr
1feb4 72 2c 20 22 63 61 6e 6e 6f 74 20 64 65 74 61 63  r, "cannot detac
1feb5 68 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20  h database %s", 
1feb6 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
1feb7 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   detach_error;. 
1feb8 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75   }.  if( !db->au
1feb9 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20  toCommit ){.    
1feba 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1febb 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1febc 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Err,.           
1febd 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f            "canno
1febe 74 20 44 45 54 41 43 48 20 64 61 74 61 62 61 73  t DETACH databas
1febf 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63  e within transac
1fec0 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f  tion");.    goto
1fec1 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   detach_error;. 
1fec2 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1fec3 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61  BtreeIsInReadTra
1fec4 6e 73 28 70 44 62 2d 3e 70 42 74 29 20 29 7b 0a  ns(pDb->pBt) ){.
1fec5 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1fec6 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
1fec7 29 2c 7a 45 72 72 2c 20 22 64 61 74 61 62 61 73  ),zErr, "databas
1fec8 65 20 25 73 20 69 73 20 6c 6f 63 6b 65 64 22 2c  e %s is locked",
1fec9 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74   zName);.    got
1feca 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a  o detach_error;.
1fecb 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 74    }..  sqlite3Bt
1fecc 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42  reeClose(pDb->pB
1fecd 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42 74 20 3d  t);.  pDb->pBt =
1fece 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53 63 68 65   0;.  pDb->pSche
1fecf 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  ma = 0;.  sqlite
1fed0 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
1fed1 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 72  hema(db, 0);.  r
1fed2 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63 68 5f 65  eturn;..detach_e
1fed3 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 5f  rror:.  sqlite3_
1fed4 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
1fed5 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b  text, zErr, -1);
1fed6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70  .}../*.** This p
1fed7 72 6f 63 65 64 75 72 65 20 67 65 6e 65 72 61 74  rocedure generat
1fed8 65 73 20 56 44 42 45 20 63 6f 64 65 20 66 6f 72  es VDBE code for
1fed9 20 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f 63 61   a single invoca
1feda 74 69 6f 6e 20 6f 66 20 65 69 74 68 65 72 20 74  tion of either t
1fedb 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 64 65 74  he.** sqlite_det
1fedc 61 63 68 28 29 20 6f 72 20 73 71 6c 69 74 65 5f  ach() or sqlite_
1fedd 61 74 74 61 63 68 28 29 20 53 51 4c 20 75 73 65  attach() SQL use
1fede 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  r functions..*/.
1fedf 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
1fee0 41 74 74 61 63 68 28 0a 20 20 50 61 72 73 65 20  Attach(.  Parse 
1fee1 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
1fee2 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
1fee3 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 74 79  text */.  int ty
1fee4 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  pe,            /
1fee5 2a 20 45 69 74 68 65 72 20 53 51 4c 49 54 45 5f  * Either SQLITE_
1fee6 41 54 54 41 43 48 20 6f 72 20 53 51 4c 49 54 45  ATTACH or SQLITE
1fee7 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20 46 75 6e  _DETACH */.  Fun
1fee8 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 20 20 20  cDef *pFunc,    
1fee9 20 20 2f 2a 20 46 75 6e 63 44 65 66 20 77 72 61    /* FuncDef wra
1feea 70 70 65 72 20 66 6f 72 20 64 65 74 61 63 68 46  pper for detachF
1feeb 75 6e 63 28 29 20 6f 72 20 61 74 74 61 63 68 46  unc() or attachF
1feec 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78 70 72 20  unc() */.  Expr 
1feed 2a 70 41 75 74 68 41 72 67 2c 20 20 20 20 20 20  *pAuthArg,      
1feee 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f  /* Expression to
1feef 20 70 61 73 73 20 74 6f 20 61 75 74 68 6f 72 69   pass to authori
1fef0 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20  zation callback 
1fef1 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46 69 6c 65  */.  Expr *pFile
1fef2 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d  name,     /* Nam
1fef3 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69  e of database fi
1fef4 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44  le */.  Expr *pD
1fef5 62 6e 61 6d 65 2c 20 20 20 20 20 20 20 2f 2a 20  bname,       /* 
1fef6 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
1fef7 62 61 73 65 20 74 6f 20 75 73 65 20 69 6e 74 65  base to use inte
1fef8 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 78 70 72  rnally */.  Expr
1fef9 20 2a 70 4b 65 79 20 20 20 20 20 20 20 20 20 20   *pKey          
1fefa 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6b 65 79   /* Database key
1fefb 20 66 6f 72 20 65 6e 63 72 79 70 74 69 6f 6e 20   for encryption 
1fefc 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a  extension */.){.
1fefd 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e 61 6d 65    int rc;.  Name
1fefe 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 3b 0a 20  Context sName;. 
1feff 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69   Vdbe *v;.  sqli
1ff00 74 65 33 2a 20 64 62 20 3d 20 70 50 61 72 73 65  te3* db = pParse
1ff01 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 65 67 41  ->db;.  int regA
1ff02 72 67 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  rgs;..#ifndef SQ
1ff03 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
1ff04 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72 74  IZATION.  assert
1ff05 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
1ff06 65 64 20 7c 7c 20 70 41 75 74 68 41 72 67 20 29  ed || pAuthArg )
1ff07 3b 0a 20 20 69 66 28 20 70 41 75 74 68 41 72 67  ;.  if( pAuthArg
1ff08 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 41   ){.    char *zA
1ff09 75 74 68 41 72 67 20 3d 20 73 71 6c 69 74 65 33  uthArg = sqlite3
1ff0a 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
1ff0b 2c 20 26 70 41 75 74 68 41 72 67 2d 3e 73 70 61  , &pAuthArg->spa
1ff0c 6e 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 41 75  n);.    if( !zAu
1ff0d 74 68 41 72 67 20 29 7b 0a 20 20 20 20 20 20 67  thArg ){.      g
1ff0e 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a  oto attach_end;.
1ff0f 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
1ff10 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1ff11 70 50 61 72 73 65 2c 20 74 79 70 65 2c 20 7a 41  pParse, type, zA
1ff12 75 74 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a 20  uthArg, 0, 0);. 
1ff13 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1ff14 28 64 62 2c 20 7a 41 75 74 68 41 72 67 29 3b 0a  (db, zAuthArg);.
1ff15 20 20 20 20 69 66 28 72 63 21 3d 53 51 4c 49 54      if(rc!=SQLIT
1ff16 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
1ff17 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20  to attach_end;. 
1ff18 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20     }.  }.#endif 
1ff19 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  /* SQLITE_OMIT_A
1ff1a 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a  UTHORIZATION */.
1ff1b 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 61 6d 65  .  memset(&sName
1ff1c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65  , 0, sizeof(Name
1ff1d 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e 61  Context));.  sNa
1ff1e 6d 65 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  me.pParse = pPar
1ff1f 73 65 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20 20  se;..  if( .    
1ff20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63    SQLITE_OK!=(rc
1ff21 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68   = resolveAttach
1ff22 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 46 69  Expr(&sName, pFi
1ff23 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20  lename)) ||.    
1ff24 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63    SQLITE_OK!=(rc
1ff25 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68   = resolveAttach
1ff26 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 44 62  Expr(&sName, pDb
1ff27 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20  name)) ||.      
1ff28 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
1ff29 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78   resolveAttachEx
1ff2a 70 72 28 26 73 4e 61 6d 65 2c 20 70 4b 65 79 29  pr(&sName, pKey)
1ff2b 29 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 72 73  ).  ){.    pPars
1ff2c 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67  e->nErr++;.    g
1ff2d 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a  oto attach_end;.
1ff2e 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74    }..  v = sqlit
1ff2f 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1ff30 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d 20 73  );.  regArgs = s
1ff31 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
1ff32 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b 0a 20  ge(pParse, 4);. 
1ff33 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1ff34 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e 61  (pParse, pFilena
1ff35 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a 20 20  me, regArgs);.  
1ff36 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
1ff37 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65 2c  pParse, pDbname,
1ff38 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20 20 73   regArgs+1);.  s
1ff39 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1ff3a 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72 65 67  Parse, pKey, reg
1ff3b 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73 73 65  Args+2);..  asse
1ff3c 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c  rt( v || db->mal
1ff3d 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
1ff3e 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
1ff3f 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
1ff40 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 2c   OP_Function, 0,
1ff41 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75 6e 63   regArgs+3-pFunc
1ff42 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67 73 2b  ->nArg, regArgs+
1ff43 33 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  3);.    assert( 
1ff44 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d 2d 31 20  pFunc->nArg==-1 
1ff45 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 72 67 26  || (pFunc->nArg&
1ff46 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d 3e 6e 41  0xff)==pFunc->nA
1ff47 72 67 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rg );.    sqlite
1ff48 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
1ff49 20 28 75 38 29 28 70 46 75 6e 63 2d 3e 6e 41 72   (u8)(pFunc->nAr
1ff4a 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g));.    sqlite3
1ff4b 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
1ff4c 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 46 75 6e  -1, (char *)pFun
1ff4d 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a  c, P4_FUNCDEF);.
1ff4e 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20  .    /* Code an 
1ff4f 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f 72 20 61  OP_Expire. For a
1ff50 6e 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65  n ATTACH stateme
1ff51 6e 74 2c 20 73 65 74 20 50 31 20 74 6f 20 74 72  nt, set P1 to tr
1ff52 75 65 20 28 65 78 70 69 72 65 20 74 68 69 73 0a  ue (expire this.
1ff53 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
1ff54 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45 54 41   only). For DETA
1ff55 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20 66 61  CH, set it to fa
1ff56 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c 6c 20  lse (expire all 
1ff57 65 78 69 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20  existing.    ** 
1ff58 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 20 20 20  statements)..   
1ff59 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
1ff5a 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
1ff5b 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d 3d 53  Expire, (type==S
1ff5c 51 4c 49 54 45 5f 41 54 54 41 43 48 29 29 3b 0a  QLITE_ATTACH));.
1ff5d 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 5f 65 6e    }.  .attach_en
1ff5e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  d:.  sqlite3Expr
1ff5f 44 65 6c 65 74 65 28 64 62 2c 20 70 46 69 6c 65  Delete(db, pFile
1ff60 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  name);.  sqlite3
1ff61 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1ff62 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  Dbname);.  sqlit
1ff63 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
1ff64 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pKey);.}../*.**
1ff65 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   Called by the p
1ff66 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c 65  arser to compile
1ff67 20 61 20 44 45 54 41 43 48 20 73 74 61 74 65 6d   a DETACH statem
1ff68 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44  ent..**.**     D
1ff69 45 54 41 43 48 20 70 44 62 6e 61 6d 65 0a 2a 2f  ETACH pDbname.*/
1ff6a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1ff6b 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 61  void sqlite3Deta
1ff6c 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ch(Parse *pParse
1ff6d 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 29  , Expr *pDbname)
1ff6e 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63 44  {.  static FuncD
1ff6f 65 66 20 64 65 74 61 63 68 5f 66 75 6e 63 20 3d  ef detach_func =
1ff70 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   {.    1,       
1ff71 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67           /* nArg
1ff72 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55   */.    SQLITE_U
1ff73 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72  TF8,      /* iPr
1ff74 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20  efEnc */.    0, 
1ff75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ff76 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30  * flags */.    0
1ff77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1ff78 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f   /* pUserData */
1ff79 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
1ff7a 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20         /* pNext 
1ff7b 2a 2f 0a 20 20 20 20 64 65 74 61 63 68 46 75 6e  */.    detachFun
1ff7c 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e  c,       /* xFun
1ff7d 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  c */.    0,     
1ff7e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
1ff7f 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  tep */.    0,   
1ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ff81 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20  xFinalize */.   
1ff82 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68 22   "sqlite_detach"
1ff83 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20  ,  /* zName */. 
1ff84 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
1ff85 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f       /* pHash */
1ff86 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61  .  };.  codeAtta
1ff87 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ch(pParse, SQLIT
1ff88 45 5f 44 45 54 41 43 48 2c 20 26 64 65 74 61 63  E_DETACH, &detac
1ff89 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 6d 65 2c  h_func, pDbname,
1ff8a 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d 65 29 3b   0, 0, pDbname);
1ff8b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64  .}../*.** Called
1ff8c 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74   by the parser t
1ff8d 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41 54 54  o compile an ATT
1ff8e 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ACH statement..*
1ff8f 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20  *.**     ATTACH 
1ff90 70 20 41 53 20 70 44 62 6e 61 6d 65 20 4b 45 59  p AS pDbname KEY
1ff91 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f   pKey.*/.SQLITE_
1ff92 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1ff93 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73 65  ite3Attach(Parse
1ff94 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
1ff95 70 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65  p, Expr *pDbname
1ff96 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 7b 0a 20  , Expr *pKey){. 
1ff97 20 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 20   static FuncDef 
1ff98 61 74 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a  attach_func = {.
1ff99 20 20 20 20 33 2c 20 20 20 20 20 20 20 20 20 20      3,          
1ff9a 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f        /* nArg */
1ff9b 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38  .    SQLITE_UTF8
1ff9c 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45  ,      /* iPrefE
1ff9d 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  nc */.    0,    
1ff9e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
1ff9f 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20  lags */.    0,  
1ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ffa1 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20 20   pUserData */.  
1ffa2 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
1ffa3 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a      /* pNext */.
1ffa4 20 20 20 20 61 74 74 61 63 68 46 75 6e 63 2c 20      attachFunc, 
1ffa5 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a        /* xFunc *
1ffa6 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
1ffa7 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65 70          /* xStep
1ffa8 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
1ffa9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
1ffaa 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73  nalize */.    "s
1ffab 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20 20  qlite_attach",  
1ffac 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  /* zName */.    
1ffad 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
1ffae 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20    /* pHash */.  
1ffaf 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68 28  };.  codeAttach(
1ffb0 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41  pParse, SQLITE_A
1ffb1 54 54 41 43 48 2c 20 26 61 74 74 61 63 68 5f 66  TTACH, &attach_f
1ffb2 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 62 6e 61  unc, p, p, pDbna
1ffb3 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e  me, pKey);.}.#en
1ffb4 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1ffb5 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a  IT_ATTACH */../*
1ffb6 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
1ffb7 20 44 62 46 69 78 65 72 20 73 74 72 75 63 74 75   DbFixer structu
1ffb8 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  re.  This routin
1ffb9 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
1ffba 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70 61 73   prior.** to pas
1ffbb 73 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75  sing the structu
1ffbc 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65  re to one of the
1ffbd 20 73 71 6c 69 74 65 46 69 78 41 41 41 41 28 29   sqliteFixAAAA()
1ffbe 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e   routines below.
1ffbf 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
1ffc0 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65  n value indicate
1ffc1 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
1ffc2 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72 65 71   fixation is req
1ffc3 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a 2a 20  uired.  TRUE.** 
1ffc4 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65 65 64  means we do need
1ffc5 20 74 6f 20 66 69 78 20 74 68 65 20 64 61 74 61   to fix the data
1ffc6 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73 2c  base references,
1ffc7 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77 65 20   FALSE means we 
1ffc8 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54  do not..*/.SQLIT
1ffc9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1ffca 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a 20 20  lite3FixInit(.  
1ffcb 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
1ffcc 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78 65 72      /* The fixer
1ffcd 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a   to be initializ
1ffce 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70  ed */.  Parse *p
1ffcf 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 45  Parse,      /* E
1ffd0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 69  rror messages wi
1ffd1 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 68 65  ll be written he
1ffd2 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  re */.  int iDb,
1ffd3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1ffd4 68 69 73 20 69 73 20 74 68 65 20 64 61 74 61 62  his is the datab
1ffd5 61 73 65 20 74 68 61 74 20 6d 75 73 74 20 62 65  ase that must be
1ffd6 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74   used */.  const
1ffd7 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 2f   char *zType,  /
1ffd8 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69 67 67  * "view", "trigg
1ffd9 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78 22 20  er", or "index" 
1ffda 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  */.  const Token
1ffdb 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65   *pName  /* Name
1ffdc 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20 74 72   of the view, tr
1ffdd 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65 78 20  igger, or index 
1ffde 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
1ffdf 2a 64 62 3b 0a 0a 20 20 69 66 28 20 69 44 62 3c  *db;..  if( iDb<
1ffe0 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 20 72 65  0 || iDb==1 ) re
1ffe1 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 70  turn 0;.  db = p
1ffe2 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73  Parse->db;.  ass
1ffe3 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e 69 44 62  ert( db->nDb>iDb
1ffe4 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70 50 61 72   );.  pFix->pPar
1ffe5 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70  se = pParse;.  p
1ffe6 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62 2d 3e 61  Fix->zDb = db->a
1ffe7 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
1ffe8 20 70 46 69 78 2d 3e 7a 54 79 70 65 20 3d 20 7a   pFix->zType = z
1ffe9 54 79 70 65 3b 0a 20 20 70 46 69 78 2d 3e 70 4e  Type;.  pFix->pN
1ffea 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a 20 20 72  ame = pName;.  r
1ffeb 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
1ffec 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1ffed 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20  set of routines 
1ffee 77 61 6c 6b 20 74 68 72 6f 75 67 68 20 74 68 65  walk through the
1ffef 20 70 61 72 73 65 20 74 72 65 65 20 61 6e 64 20   parse tree and 
1fff0 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73 70 65 63  assign.** a spec
1fff1 69 66 69 63 20 64 61 74 61 62 61 73 65 20 74 6f  ific database to
1fff2 20 61 6c 6c 20 74 61 62 6c 65 20 72 65 66 65 72   all table refer
1fff3 65 6e 63 65 73 20 77 68 65 72 65 20 74 68 65 20  ences where the 
1fff4 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a  database name.**
1fff5 20 77 61 73 20 6c 65 66 74 20 75 6e 73 70 65 63   was left unspec
1fff6 69 66 69 65 64 20 69 6e 20 74 68 65 20 6f 72 69  ified in the ori
1fff7 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d  ginal SQL statem
1fff8 65 6e 74 2e 20 20 54 68 65 20 70 46 69 78 20 73  ent.  The pFix s
1fff9 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d 75 73 74  tructure.** must
1fffa 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69   have been initi
1fffb 61 6c 69 7a 65 64 20 62 79 20 61 20 70 72 69 6f  alized by a prio
1fffc 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  r call to sqlite
1fffd 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a 2a 0a 2a  3FixInit()..**.*
1fffe 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
1ffff 20 61 72 65 20 75 73 65 64 20 74 6f 20 6d 61 6b   are used to mak
20000 65 20 73 75 72 65 20 74 68 61 74 20 61 6e 20 69  e sure that an i
20001 6e 64 65 78 2c 20 74 72 69 67 67 65 72 2c 20 6f  ndex, trigger, o
20002 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20 6f 6e 65  r.** view in one
20003 20 64 61 74 61 62 61 73 65 20 64 6f 65 73 20 6e   database does n
20004 6f 74 20 72 65 66 65 72 20 74 6f 20 6f 62 6a 65  ot refer to obje
20005 63 74 73 20 69 6e 20 61 20 64 69 66 66 65 72 65  cts in a differe
20006 6e 74 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20  nt database..** 
20007 28 45 78 63 65 70 74 69 6f 6e 3a 20 69 6e 64 69  (Exception: indi
20008 63 65 73 2c 20 74 72 69 67 67 65 72 73 2c 20 61  ces, triggers, a
20009 6e 64 20 76 69 65 77 73 20 69 6e 20 74 68 65 20  nd views in the 
2000a 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 61 72  TEMP database ar
2000b 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20  e.** allowed to 
2000c 72 65 66 65 72 20 74 6f 20 61 6e 79 74 68 69 6e  refer to anythin
2000d 67 2e 29 20 20 49 66 20 61 20 72 65 66 65 72 65  g.)  If a refere
2000e 6e 63 65 20 69 73 20 65 78 70 6c 69 63 69 74 6c  nce is explicitl
2000f 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 61 6e 20  y made.** to an 
20010 6f 62 6a 65 63 74 20 69 6e 20 61 20 64 69 66 66  object in a diff
20011 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2c 20  erent database, 
20012 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
20013 20 69 73 20 61 64 64 65 64 20 74 6f 0a 2a 2a 20   is added to.** 
20014 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
20015 61 6e 64 20 74 68 65 73 65 20 72 6f 75 74 69 6e  and these routin
20016 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  es return non-ze
20017 72 6f 2e 20 20 49 66 20 65 76 65 72 79 74 68 69  ro.  If everythi
20018 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20 6f 75 74  ng.** checks out
20019 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  , these routines
2001a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51   return 0..*/.SQ
2001b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2001c 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69   sqlite3FixSrcLi
2001d 73 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70  st(.  DbFixer *p
2001e 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f  Fix,       /* Co
2001f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78  ntext of the fix
20020 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69  ation */.  SrcLi
20021 73 74 20 2a 70 4c 69 73 74 20 20 20 20 20 20 20  st *pList       
20022 2f 2a 20 54 68 65 20 53 6f 75 72 63 65 20 6c 69  /* The Source li
20023 73 74 20 74 6f 20 63 68 65 63 6b 20 61 6e 64 20  st to check and 
20024 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a 20 20 69  modify */.){.  i
20025 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68  nt i;.  const ch
20026 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74 72 75 63  ar *zDb;.  struc
20027 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
20028 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20 70 4c  pItem;..  if( pL
20029 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ist==0 ) return 
2002a 30 3b 0a 20 20 7a 44 62 20 3d 20 70 46 69 78 2d  0;.  zDb = pFix-
2002b 3e 7a 44 62 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  >zDb;.  for(i=0,
2002c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b   pItem=pList->a;
2002d 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20   i<pList->nSrc; 
2002e 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
2002f 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44     if( pItem->zD
20030 61 74 61 62 61 73 65 3d 3d 30 20 29 7b 0a 20 20  atabase==0 ){.  
20031 20 20 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61      pItem->zData
20032 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62  base = sqlite3Db
20033 53 74 72 44 75 70 28 70 46 69 78 2d 3e 70 50 61  StrDup(pFix->pPa
20034 72 73 65 2d 3e 64 62 2c 20 7a 44 62 29 3b 0a 20  rse->db, zDb);. 
20035 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c     }else if( sql
20036 69 74 65 33 53 74 72 49 43 6d 70 28 70 49 74 65  ite3StrICmp(pIte
20037 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 7a 44 62  m->zDatabase,zDb
20038 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
20039 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46  lite3ErrorMsg(pF
2003a 69 78 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20  ix->pParse,.    
2003b 20 20 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e       "%s %T cann
2003c 6f 74 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a  ot reference obj
2003d 65 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65  ects in database
2003e 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70   %s",.         p
2003f 46 69 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78  Fix->zType, pFix
20040 2d 3e 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e  ->pName, pItem->
20041 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
20042 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
20043 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  }.#if !defined(S
20044 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
20045 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
20046 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
20047 29 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ).    if( sqlite
20048 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c  3FixSelect(pFix,
20049 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29   pItem->pSelect)
2004a 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
2004b 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
2004c 78 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d  xpr(pFix, pItem-
2004d 3e 70 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31  >pOn) ) return 1
2004e 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72  ;.#endif.  }.  r
2004f 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21  eturn 0;.}.#if !
20050 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
20051 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
20052 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
20053 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 54  T_TRIGGER).SQLIT
20054 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20055 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 0a  lite3FixSelect(.
20056 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
20057 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
20058 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f  t of the fixatio
20059 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  n */.  Select *p
2005a 53 65 6c 65 63 74 20 20 20 20 20 20 2f 2a 20 54  Select      /* T
2005b 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2005c 65 6e 74 20 74 6f 20 62 65 20 66 69 78 65 64 20  ent to be fixed 
2005d 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  to one database 
2005e 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  */.){.  while( p
2005f 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 69 66  Select ){.    if
20060 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
20061 4c 69 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65  List(pFix, pSele
20062 63 74 2d 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20  ct->pEList) ){. 
20063 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
20064 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
20065 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 70  ite3FixSrcList(p
20066 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53  Fix, pSelect->pS
20067 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  rc) ){.      ret
20068 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
20069 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
2006a 78 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63  xpr(pFix, pSelec
2006b 74 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20  t->pWhere) ){.  
2006c 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2006d 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
2006e 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c  te3FixExpr(pFix,
2006f 20 70 53 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e   pSelect->pHavin
20070 67 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  g) ){.      retu
20071 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
20072 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
20073 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  t->pPrior;.  }. 
20074 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c   return 0;.}.SQL
20075 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
20076 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 0a  sqlite3FixExpr(.
20077 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
20078 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
20079 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20  of the fixation 
2007a 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
2007b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
2007c 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20  xpression to be 
2007d 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74  fixed to one dat
2007e 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68  abase */.){.  wh
2007f 69 6c 65 28 20 70 45 78 70 72 20 29 7b 0a 20 20  ile( pExpr ){.  
20080 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
20081 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 45 78  Select(pFix, pEx
20082 70 72 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a  pr->pSelect) ){.
20083 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
20084 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
20085 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74  lite3FixExprList
20086 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 70 4c  (pFix, pExpr->pL
20087 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ist) ){.      re
20088 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
20089 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
2008a 45 78 70 72 28 70 46 69 78 2c 20 70 45 78 70 72  Expr(pFix, pExpr
2008b 2d 3e 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20  ->pRight) ){.   
2008c 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
2008d 20 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70   }.    pExpr = p
2008e 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d  Expr->pLeft;.  }
2008f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53  .  return 0;.}.S
20090 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
20091 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  t sqlite3FixExpr
20092 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20  List(.  DbFixer 
20093 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f  *pFix,     /* Co
20094 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78  ntext of the fix
20095 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c  ation */.  ExprL
20096 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a  ist *pList    /*
20097 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
20098 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f  to be fixed to o
20099 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29  ne database */.)
2009a 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
2009b 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
2009c 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20  m *pItem;.  if( 
2009d 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
2009e 6e 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  n 0;.  for(i=0, 
2009f 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20  pItem=pList->a; 
200a0 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
200a1 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
200a2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
200a3 78 45 78 70 72 28 70 46 69 78 2c 20 70 49 74 65  xExpr(pFix, pIte
200a4 6d 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20  m->pExpr) ){.   
200a5 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
200a6 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
200a7 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  0;.}.#endif..#if
200a8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
200a9 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 5f  _TRIGGER.SQLITE_
200aa 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
200ab 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
200ac 70 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  p(.  DbFixer *pF
200ad 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ix,     /* Conte
200ae 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
200af 6f 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53  on */.  TriggerS
200b0 74 65 70 20 2a 70 53 74 65 70 20 2f 2a 20 54 68  tep *pStep /* Th
200b1 65 20 74 72 69 67 67 65 72 20 73 74 65 70 20 62  e trigger step b
200b2 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64  e fixed to one d
200b3 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20  atabase */.){.  
200b4 77 68 69 6c 65 28 20 70 53 74 65 70 20 29 7b 0a  while( pStep ){.
200b5 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
200b6 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70  ixSelect(pFix, p
200b7 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 20 29  Step->pSelect) )
200b8 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
200b9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
200ba 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70  sqlite3FixExpr(p
200bb 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 57 68 65  Fix, pStep->pWhe
200bc 72 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  re) ){.      ret
200bd 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
200be 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
200bf 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53  xprList(pFix, pS
200c0 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 20  tep->pExprList) 
200c1 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
200c2 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 74  1;.    }.    pSt
200c3 65 70 20 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78  ep = pStep->pNex
200c4 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
200c5 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  0;.}.#endif../**
200c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
200c7 20 6f 66 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a   of attach.c ***
200c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
200c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
200ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
200cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
200cc 69 6e 20 66 69 6c 65 20 61 75 74 68 2e 63 20 2a  in file auth.c *
200cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
200ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
200cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
200d0 2a 2a 20 32 30 30 33 20 4a 61 6e 75 61 72 79 20  ** 2003 January 
200d1 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  11.**.** The aut
200d2 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
200d3 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
200d4 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
200d5 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
200d6 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
200d7 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
200d8 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
200d9 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
200da 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
200db 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
200dc 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
200dd 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
200de 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
200df 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
200e0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
200e1 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
200e2 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
200e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
200e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
200e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
200e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
200e7 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
200e8 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
200e9 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
200ea 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  he sqlite3_set_a
200eb 75 74 68 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41  uthorizer().** A
200ec 50 49 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69  PI.  This facili
200ed 74 79 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61  ty is an optiona
200ee 6c 20 66 65 61 74 75 72 65 20 6f 66 20 74 68 65  l feature of the
200ef 20 6c 69 62 72 61 72 79 2e 20 20 45 6d 62 65 64   library.  Embed
200f0 64 65 64 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74  ded.** systems t
200f1 68 61 74 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20  hat do not need 
200f2 74 68 69 73 20 66 61 63 69 6c 69 74 79 20 6d 61  this facility ma
200f3 79 20 6f 6d 69 74 20 69 74 20 62 79 20 72 65 63  y omit it by rec
200f4 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20  ompiling.** the 
200f5 6c 69 62 72 61 72 79 20 77 69 74 68 20 2d 44 53  library with -DS
200f6 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
200f7 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2a 0a 2a 2a  RIZATION=1.**.**
200f8 20 24 49 64 3a 20 61 75 74 68 2e 63 2c 76 20 31   $Id: auth.c,v 1
200f9 2e 32 39 20 32 30 30 37 2f 30 39 2f 31 38 20 31  .29 2007/09/18 1
200fa 35 3a 35 35 3a 30 37 20 64 72 68 20 45 78 70 20  5:55:07 drh Exp 
200fb 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20  $.*/../*.** All 
200fc 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74  of the code in t
200fd 68 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20  his file may be 
200fe 6f 6d 69 74 74 65 64 20 62 79 20 64 65 66 69 6e  omitted by defin
200ff 69 6e 67 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  ing a single.** 
20100 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65  macro..*/.#ifnde
20101 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
20102 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a  THORIZATION../*.
20103 2a 2a 20 53 65 74 20 6f 72 20 63 6c 65 61 72 20  ** Set or clear 
20104 74 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f  the access autho
20105 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
20106 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63  n..**.** The acc
20107 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  ess authorizatio
20108 6e 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  n function is be
20109 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74   called during t
2010a 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a  he compilation.*
2010b 2a 20 70 68 61 73 65 20 74 6f 20 76 65 72 69 66  * phase to verif
2010c 79 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20  y that the user 
2010d 68 61 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20  has read and/or 
2010e 77 72 69 74 65 20 61 63 63 65 73 73 20 70 65 72  write access per
2010f 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61  mission on.** va
20110 72 69 6f 75 73 20 66 69 65 6c 64 73 20 6f 66 20  rious fields of 
20111 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
20112 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
20113 74 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75  t to the auth fu
20114 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63  nction.** is a c
20115 6f 70 79 20 6f 66 20 74 68 65 20 33 72 64 20 61  opy of the 3rd a
20116 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
20117 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 65  routine.  The se
20118 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
20119 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e   to the auth fun
2011a 63 74 69 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20  ction is one of 
2011b 74 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a  these constants:
2011c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  .**.**       SQL
2011d 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58  ITE_CREATE_INDEX
2011e 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
2011f 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a  _CREATE_TABLE.**
20120 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
20121 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a  EATE_TEMP_INDEX.
20122 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
20123 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c  CREATE_TEMP_TABL
20124 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  E.**       SQLIT
20125 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
20126 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53  IGGER.**       S
20127 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
20128 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  P_VIEW.**       
20129 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52  SQLITE_CREATE_TR
2012a 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53  IGGER.**       S
2012b 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45  QLITE_CREATE_VIE
2012c 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  W.**       SQLIT
2012d 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20  E_DELETE.**     
2012e 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e    SQLITE_DROP_IN
2012f 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  DEX.**       SQL
20130 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a  ITE_DROP_TABLE.*
20131 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
20132 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a  ROP_TEMP_INDEX.*
20133 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
20134 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a  ROP_TEMP_TABLE.*
20135 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
20136 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52  ROP_TEMP_TRIGGER
20137 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
20138 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a  _DROP_TEMP_VIEW.
20139 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
2013a 44 52 4f 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20  DROP_TRIGGER.** 
2013b 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
2013c 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  P_VIEW.**       
2013d 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a  SQLITE_INSERT.**
2013e 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 50 52         SQLITE_PR
2013f 41 47 4d 41 0a 2a 2a 20 20 20 20 20 20 20 53 51  AGMA.**       SQ
20140 4c 49 54 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20  LITE_READ.**    
20141 20 20 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54     SQLITE_SELECT
20142 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
20143 5f 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20  _TRANSACTION.** 
20144 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 50 44        SQLITE_UPD
20145 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  ATE.**.** The th
20146 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61  ird and fourth a
20147 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
20148 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 61 72  auth function ar
20149 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a  e the name of.**
2014a 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74   the table and t
2014b 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61  he column that a
2014c 72 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65  re being accesse
2014d 64 2e 20 20 54 68 65 20 61 75 74 68 20 66 75 6e  d.  The auth fun
2014e 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20  ction.** should 
2014f 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51  return either SQ
20150 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f  LITE_OK, SQLITE_
20151 44 45 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f  DENY, or SQLITE_
20152 49 47 4e 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53  IGNORE.  If.** S
20153 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
20154 72 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74  rned, it means t
20155 68 61 74 20 61 63 63 65 73 73 20 69 73 20 61 6c  hat access is al
20156 6c 6f 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44  lowed.  SQLITE_D
20157 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61  ENY.** means tha
20158 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  t the SQL statem
20159 65 6e 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72  ent will never-r
2015a 75 6e 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33  un - the sqlite3
2015b 5f 65 78 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20  _exec() call.** 
2015c 77 69 6c 6c 20 72 65 74 75 72 6e 20 77 69 74 68  will return with
2015d 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49   an error.  SQLI
2015e 54 45 5f 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20  TE_IGNORE means 
2015f 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 61  that the SQL sta
20160 74 65 6d 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64  tement.** should
20161 20 72 75 6e 20 62 75 74 20 61 74 74 65 6d 70 74   run but attempt
20162 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 70  s to read the sp
20163 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77  ecified column w
20164 69 6c 6c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a  ill return NULL.
20165 2a 2a 20 61 6e 64 20 61 74 74 65 6d 70 74 73 20  ** and attempts 
20166 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6c  to write the col
20167 75 6d 6e 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f  umn will be igno
20168 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69  red..**.** Setti
20169 6e 67 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  ng the auth func
2016a 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73  tion to NULL dis
2016b 61 62 6c 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e  ables this hook.
2016c 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a    The default.**
2016d 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20   setting of the 
2016e 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73  auth function is
2016f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45   NULL..*/.SQLITE
20170 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
20171 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
20172 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
20173 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
20174 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
20175 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
20176 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
20177 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69  st char*),.  voi
20178 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c  d *pArg.){.  sql
20179 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
2017a 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
2017b 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68  b->xAuth = xAuth
2017c 3b 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67  ;.  db->pAuthArg
2017d 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
2017e 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
2017f 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
20180 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
20181 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
20182 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
20183 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  E_OK;.}../*.** W
20184 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rite an error me
20185 73 73 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73  ssage into pPars
20186 65 2d 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20  e->zErrMsg that 
20187 65 78 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68  explains that th
20188 65 0a 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69  e.** user-suppli
20189 65 64 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ed authorization
2018a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
2018b 65 64 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61  ed an illegal va
2018c 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lue..*/.static v
2018d 6f 69 64 20 73 71 6c 69 74 65 41 75 74 68 42 61  oid sqliteAuthBa
2018e 64 52 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73  dReturnCode(Pars
2018f 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 72  e *pParse, int r
20190 63 29 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72  c){.  sqlite3Err
20191 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69  orMsg(pParse, "i
20192 6c 6c 65 67 61 6c 20 72 65 74 75 72 6e 20 76 61  llegal return va
20193 6c 75 65 20 28 25 64 29 20 66 72 6f 6d 20 74 68  lue (%d) from th
20194 65 20 22 0a 20 20 20 20 22 61 75 74 68 6f 72 69  e ".    "authori
20195 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  zation function 
20196 2d 20 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49  - should be SQLI
20197 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 49 47  TE_OK, SQLITE_IG
20198 4e 4f 52 45 2c 20 22 0a 20 20 20 20 22 6f 72 20  NORE, ".    "or 
20199 53 51 4c 49 54 45 5f 44 45 4e 59 22 2c 20 72 63  SQLITE_DENY", rc
2019a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20  );.  pParse->rc 
2019b 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2019c 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 45 78  }../*.** The pEx
2019d 70 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 54  pr should be a T
2019e 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73  K_COLUMN express
2019f 69 6f 6e 2e 20 20 54 68 65 20 74 61 62 6c 65 20  ion.  The table 
201a0 72 65 66 65 72 72 65 64 20 74 6f 0a 2a 2a 20 69  referred to.** i
201a1 73 20 69 6e 20 70 54 61 62 4c 69 73 74 20 6f 72  s in pTabList or
201a2 20 65 6c 73 65 20 69 74 20 69 73 20 74 68 65 20   else it is the 
201a3 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65  NEW or OLD table
201a4 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 20 20   of a trigger.  
201a5 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
201a6 20 69 66 20 69 74 20 69 73 20 4f 4b 20 74 6f 20   if it is OK to 
201a7 72 65 61 64 20 74 68 69 73 20 70 61 72 74 69 63  read this partic
201a8 75 6c 61 72 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a  ular column..**.
201a9 2a 2a 20 49 66 20 74 68 65 20 61 75 74 68 20 66  ** If the auth f
201aa 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
201ab 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 63  SQLITE_IGNORE, c
201ac 68 61 6e 67 65 20 74 68 65 20 54 4b 5f 43 4f 4c  hange the TK_COL
201ad 55 4d 4e 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74  UMN .** instruct
201ae 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 4e 55  ion into a TK_NU
201af 4c 4c 2e 20 20 49 66 20 74 68 65 20 61 75 74 68  LL.  If the auth
201b0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
201b1 73 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a  s SQLITE_DENY,.*
201b2 2a 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20  * then generate 
201b3 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  an error..*/.SQL
201b4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
201b5 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64   sqlite3AuthRead
201b6 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
201b7 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  e,        /* The
201b8 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
201b9 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
201ba 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
201bb 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
201bc 63 68 65 63 6b 20 61 75 74 68 6f 72 69 7a 61 74  check authorizat
201bd 69 6f 6e 20 6f 6e 20 2a 2f 0a 20 20 53 63 68 65  ion on */.  Sche
201be 6d 61 20 2a 70 53 63 68 65 6d 61 2c 20 20 20 20  ma *pSchema,    
201bf 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 61 20    /* The schema 
201c0 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
201c1 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  n */.  SrcList *
201c2 70 54 61 62 4c 69 73 74 20 20 20 20 20 2f 2a 20  pTabList     /* 
201c3 41 6c 6c 20 74 61 62 6c 65 20 74 68 61 74 20 70  All table that p
201c4 45 78 70 72 20 6d 69 67 68 74 20 72 65 66 65 72  Expr might refer
201c5 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69   to */.){.  sqli
201c6 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
201c7 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  ->db;.  int rc;.
201c8 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
201c9 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74  0;      /* The t
201ca 61 62 6c 65 20 62 65 69 6e 67 20 72 65 61 64 20  able being read 
201cb 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
201cc 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f 2a 20 4e 61  *zCol;     /* Na
201cd 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  me of the column
201ce 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
201cf 0a 20 20 69 6e 74 20 69 53 72 63 3b 20 20 20 20  .  int iSrc;    
201d0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
201d1 78 20 69 6e 20 70 54 61 62 4c 69 73 74 2d 3e 61  x in pTabList->a
201d2 5b 5d 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e  [] of table bein
201d3 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73  g read */.  cons
201d4 74 20 63 68 61 72 20 2a 7a 44 42 61 73 65 3b 20  t char *zDBase; 
201d5 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
201d6 61 62 61 73 65 20 62 65 69 6e 67 20 61 63 63 65  abase being acce
201d7 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65  ssed */.  Trigge
201d8 72 53 74 61 63 6b 20 2a 70 53 74 61 63 6b 3b 20  rStack *pStack; 
201d9 2f 2a 20 54 68 65 20 73 74 61 63 6b 20 6f 66 20  /* The stack of 
201da 63 75 72 72 65 6e 74 20 74 72 69 67 67 65 72 73  current triggers
201db 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
201dc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
201dd 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
201de 64 61 74 61 62 61 73 65 20 74 68 65 20 65 78 70  database the exp
201df 72 65 73 73 69 6f 6e 20 72 65 66 65 72 73 20 74  ression refers t
201e0 6f 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e  o */..  if( db->
201e1 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74 75 72  xAuth==0 ) retur
201e2 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  n;.  if( pExpr->
201e3 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
201e4 72 65 74 75 72 6e 3b 0a 20 20 69 44 62 20 3d 20  return;.  iDb = 
201e5 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
201e6 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
201e7 20 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28   pSchema);.  if(
201e8 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 2f 2a   iDb<0 ){.    /*
201e9 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72   An attempt to r
201ea 65 61 64 20 61 20 63 6f 6c 75 6d 6e 20 6f 75 74  ead a column out
201eb 20 6f 66 20 61 20 73 75 62 71 75 65 72 79 20 6f   of a subquery o
201ec 72 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74  r other.    ** t
201ed 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20  emporary table. 
201ee 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  */.    return;. 
201ef 20 7d 0a 20 20 66 6f 72 28 69 53 72 63 3d 30 3b   }.  for(iSrc=0;
201f0 20 70 54 61 62 4c 69 73 74 20 26 26 20 69 53 72   pTabList && iSr
201f1 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  c<pTabList->nSrc
201f2 3b 20 69 53 72 63 2b 2b 29 7b 0a 20 20 20 20 69  ; iSrc++){.    i
201f3 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  f( pExpr->iTable
201f4 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53  ==pTabList->a[iS
201f5 72 63 5d 2e 69 43 75 72 73 6f 72 20 29 20 62 72  rc].iCursor ) br
201f6 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  eak;.  }.  if( i
201f7 53 72 63 3e 3d 30 20 26 26 20 70 54 61 62 4c 69  Src>=0 && pTabLi
201f8 73 74 20 26 26 20 69 53 72 63 3c 70 54 61 62 4c  st && iSrc<pTabL
201f9 69 73 74 2d 3e 6e 53 72 63 20 29 7b 0a 20 20 20  ist->nSrc ){.   
201fa 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74   pTab = pTabList
201fb 2d 3e 61 5b 69 53 72 63 5d 2e 70 54 61 62 3b 0a  ->a[iSrc].pTab;.
201fc 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 53 74    }else if( (pSt
201fd 61 63 6b 20 3d 20 70 50 61 72 73 65 2d 3e 74 72  ack = pParse->tr
201fe 69 67 53 74 61 63 6b 29 21 3d 30 20 29 7b 0a 20  igStack)!=0 ){. 
201ff 20 20 20 2f 2a 20 54 68 69 73 20 6d 75 73 74 20     /* This must 
20200 62 65 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f  be an attempt to
20201 20 72 65 61 64 20 74 68 65 20 4e 45 57 20 6f 72   read the NEW or
20202 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c   OLD pseudo-tabl
20203 65 73 0a 20 20 20 20 2a 2a 20 6f 66 20 61 20 74  es.    ** of a t
20204 72 69 67 67 65 72 2e 0a 20 20 20 20 2a 2f 0a 20  rigger..    */. 
20205 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
20206 2d 3e 69 54 61 62 6c 65 3d 3d 70 53 74 61 63 6b  ->iTable==pStack
20207 2d 3e 6e 65 77 49 64 78 20 7c 7c 20 70 45 78 70  ->newIdx || pExp
20208 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 53 74 61 63  r->iTable==pStac
20209 6b 2d 3e 6f 6c 64 49 64 78 20 29 3b 0a 20 20 20  k->oldIdx );.   
2020a 20 70 54 61 62 20 3d 20 70 53 74 61 63 6b 2d 3e   pTab = pStack->
2020b 70 54 61 62 3b 0a 20 20 7d 0a 20 20 69 66 28 20  pTab;.  }.  if( 
2020c 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e  pTab==0 ) return
2020d 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69  ;.  if( pExpr->i
2020e 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20  Column>=0 ){.   
2020f 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
20210 69 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43  iColumn<pTab->nC
20211 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d  ol );.    zCol =
20212 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70   pTab->aCol[pExp
20213 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d  r->iColumn].zNam
20214 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  e;.  }else if( p
20215 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b  Tab->iPKey>=0 ){
20216 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61  .    assert( pTa
20217 62 2d 3e 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e  b->iPKey<pTab->n
20218 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20  Col );.    zCol 
20219 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61  = pTab->aCol[pTa
2021a 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b  b->iPKey].zName;
2021b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43  .  }else{.    zC
2021c 6f 6c 20 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20  ol = "ROWID";.  
2021d 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  }.  assert( iDb>
2021e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
2021f 62 20 29 3b 0a 20 20 7a 44 42 61 73 65 20 3d 20  b );.  zDBase = 
20220 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
20221 6d 65 3b 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78  me;.  rc = db->x
20222 41 75 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72  Auth(db->pAuthAr
20223 67 2c 20 53 51 4c 49 54 45 5f 52 45 41 44 2c 20  g, SQLITE_READ, 
20224 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  pTab->zName, zCo
20225 6c 2c 20 7a 44 42 61 73 65 2c 20 0a 20 20 20 20  l, zDBase, .    
20226 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
20227 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
20228 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
20229 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20  LITE_IGNORE ){. 
2022a 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
2022b 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 20  K_NULL;.  }else 
2022c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
2022d 45 4e 59 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ENY ){.    if( d
2022e 62 2d 3e 6e 44 62 3e 32 20 7c 7c 20 69 44 62 21  b->nDb>2 || iDb!
2022f 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
20230 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
20231 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25  se, "access to %
20232 73 2e 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69  s.%s.%s is prohi
20233 62 69 74 65 64 22 2c 20 0a 20 20 20 20 20 20 20  bited", .       
20234 20 20 7a 44 42 61 73 65 2c 20 70 54 61 62 2d 3e    zDBase, pTab->
20235 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20  zName, zCol);.  
20236 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
20237 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
20238 50 61 72 73 65 2c 20 22 61 63 63 65 73 73 20 74  Parse, "access t
20239 6f 20 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69  o %s.%s is prohi
2023a 62 69 74 65 64 22 2c 70 54 61 62 2d 3e 7a 4e 61  bited",pTab->zNa
2023b 6d 65 2c 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a  me,zCol);.    }.
2023c 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
2023d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20   SQLITE_AUTH;.  
2023e 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
2023f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
20240 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 74 75  qliteAuthBadRetu
20241 72 6e 43 6f 64 65 28 70 50 61 72 73 65 2c 20 72  rnCode(pParse, r
20242 63 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  c);.  }.}../*.**
20243 20 44 6f 20 61 6e 20 61 75 74 68 6f 72 69 7a 61   Do an authoriza
20244 74 69 6f 6e 20 63 68 65 63 6b 20 75 73 69 6e 67  tion check using
20245 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20 61 72   the code and ar
20246 67 75 6d 65 6e 74 73 20 67 69 76 65 6e 2e 20 20  guments given.  
20247 52 65 74 75 72 6e 0a 2a 2a 20 65 69 74 68 65 72  Return.** either
20248 20 53 51 4c 49 54 45 5f 4f 4b 20 28 7a 65 72 6f   SQLITE_OK (zero
20249 29 20 6f 72 20 53 51 4c 49 54 45 5f 49 47 4e 4f  ) or SQLITE_IGNO
2024a 52 45 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 4e  RE or SQLITE_DEN
2024b 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 44 45  Y.  If SQLITE_DE
2024c 4e 59 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  NY.** is returne
2024d 64 2c 20 74 68 65 6e 20 74 68 65 20 65 72 72 6f  d, then the erro
2024e 72 20 63 6f 75 6e 74 20 61 6e 64 20 65 72 72 6f  r count and erro
2024f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61  r message in pPa
20250 72 73 65 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66  rse are.** modif
20251 69 65 64 20 61 70 70 72 6f 70 72 69 61 74 65 6c  ied appropriatel
20252 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
20253 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
20254 41 75 74 68 43 68 65 63 6b 28 0a 20 20 50 61 72  AuthCheck(.  Par
20255 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 69 6e  se *pParse,.  in
20256 74 20 63 6f 64 65 2c 0a 20 20 63 6f 6e 73 74 20  t code,.  const 
20257 63 68 61 72 20 2a 7a 41 72 67 31 2c 0a 20 20 63  char *zArg1,.  c
20258 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 32  onst char *zArg2
20259 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2025a 7a 41 72 67 33 0a 29 7b 0a 20 20 73 71 6c 69 74  zArg3.){.  sqlit
2025b 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2025c 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  >db;.  int rc;..
2025d 20 20 2f 2a 20 44 6f 6e 27 74 20 64 6f 20 61 6e    /* Don't do an
2025e 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  y authorization 
2025f 63 68 65 63 6b 73 20 69 66 20 74 68 65 20 64 61  checks if the da
20260 74 61 62 61 73 65 20 69 73 20 69 6e 69 74 69 61  tabase is initia
20261 6c 69 73 69 6e 67 0a 20 20 2a 2a 20 6f 72 20 69  lising.  ** or i
20262 66 20 74 68 65 20 70 61 72 73 65 72 20 69 73 20  f the parser is 
20263 62 65 69 6e 67 20 69 6e 76 6f 6b 65 64 20 66 72  being invoked fr
20264 6f 6d 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65  om within sqlite
20265 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 2e 0a  3_declare_vtab..
20266 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69    */.  if( db->i
20267 6e 69 74 2e 62 75 73 79 20 7c 7c 20 49 4e 5f 44  nit.busy || IN_D
20268 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20  ECLARE_VTAB ){. 
20269 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2026a 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  _OK;.  }..  if( 
2026b 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29 7b 0a  db->xAuth==0 ){.
2026c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2026d 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  E_OK;.  }.  rc =
2026e 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70   db->xAuth(db->p
2026f 41 75 74 68 41 72 67 2c 20 63 6f 64 65 2c 20 7a  AuthArg, code, z
20270 41 72 67 31 2c 20 7a 41 72 67 32 2c 20 7a 41 72  Arg1, zArg2, zAr
20271 67 33 2c 20 70 50 61 72 73 65 2d 3e 7a 41 75 74  g3, pParse->zAut
20272 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28  hContext);.  if(
20273 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59   rc==SQLITE_DENY
20274 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
20275 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
20276 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22  "not authorized"
20277 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  );.    pParse->r
20278 63 20 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b  c = SQLITE_AUTH;
20279 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21  .  }else if( rc!
2027a 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63  =SQLITE_OK && rc
2027b 21 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20  !=SQLITE_IGNORE 
2027c 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
2027d 54 45 5f 44 45 4e 59 3b 0a 20 20 20 20 73 71 6c  TE_DENY;.    sql
2027e 69 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e  iteAuthBadReturn
2027f 43 6f 64 65 28 70 50 61 72 73 65 2c 20 72 63 29  Code(pParse, rc)
20280 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
20281 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68  c;.}../*.** Push
20282 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   an authorizatio
20283 6e 20 63 6f 6e 74 65 78 74 2e 20 20 41 66 74 65  n context.  Afte
20284 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  r this routine i
20285 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a 2a 2a  s called, the.**
20286 20 7a 41 72 67 33 20 61 72 67 75 6d 65 6e 74 20   zArg3 argument 
20287 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  to authorization
20288 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20   callbacks will 
20289 62 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e 74 69  be zContext unti
2028a 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20 4f 72  l.** popped.  Or
2028b 20 69 66 20 70 50 61 72 73 65 3d 3d 30 2c 20 74   if pParse==0, t
2028c 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
2028d 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
2028e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2028f 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
20290 74 50 75 73 68 28 0a 20 20 50 61 72 73 65 20 2a  tPush(.  Parse *
20291 70 50 61 72 73 65 2c 0a 20 20 41 75 74 68 43 6f  pParse,.  AuthCo
20292 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c  ntext *pContext,
20293 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
20294 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20 70 43  zContext.){.  pC
20295 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 3d  ontext->pParse =
20296 20 70 50 61 72 73 65 3b 0a 20 20 69 66 28 20 70   pParse;.  if( p
20297 50 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 6f  Parse ){.    pCo
20298 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74  ntext->zAuthCont
20299 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41  ext = pParse->zA
2029a 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  uthContext;.    
2029b 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
2029c 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74 3b  text = zContext;
2029d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f  .  }.}../*.** Po
2029e 70 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69  p an authorizati
2029f 6f 6e 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20  on context that 
202a0 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70  was previously p
202a1 75 73 68 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69  ushed.** by sqli
202a2 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
202a3 73 68 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  sh.*/.SQLITE_PRI
202a4 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
202a5 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28  3AuthContextPop(
202a6 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f  AuthContext *pCo
202a7 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 70 43  ntext){.  if( pC
202a8 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 29  ontext->pParse )
202a9 7b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e  {.    pContext->
202aa 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
202ab 74 65 78 74 20 3d 20 70 43 6f 6e 74 65 78 74 2d  text = pContext-
202ac 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20  >zAuthContext;. 
202ad 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61     pContext->pPa
202ae 72 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  rse = 0;.  }.}..
202af 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
202b0 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
202b1 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ION */../*******
202b2 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61  ******* End of a
202b3 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  uth.c **********
202b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202b6 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
202b7 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
202b8 6c 65 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a  le build.c *****
202b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202bb 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
202bc 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
202bd 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
202be 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
202bf 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
202c0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
202c1 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
202c2 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
202c3 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
202c4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
202c5 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
202c6 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
202c7 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
202c8 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
202c9 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
202ca 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
202cb 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
202cc 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
202cd 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
202ce 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
202cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
202d3 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
202d4 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
202d5 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
202d6 6c 6c 65 64 20 62 79 20 74 68 65 20 53 51 4c 69  lled by the SQLi
202d7 74 65 20 70 61 72 73 65 72 0a 2a 2a 20 77 68 65  te parser.** whe
202d8 6e 20 73 79 6e 74 61 78 20 72 75 6c 65 73 20 61  n syntax rules a
202d9 72 65 20 72 65 64 75 63 65 64 2e 20 20 54 68 65  re reduced.  The
202da 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69   routines in thi
202db 73 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 74 68  s file handle th
202dc 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b  e.** following k
202dd 69 6e 64 73 20 6f 66 20 53 51 4c 20 73 79 6e 74  inds of SQL synt
202de 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52  ax:.**.**     CR
202df 45 41 54 45 20 54 41 42 4c 45 0a 2a 2a 20 20 20  EATE TABLE.**   
202e0 20 20 44 52 4f 50 20 54 41 42 4c 45 0a 2a 2a 20    DROP TABLE.** 
202e1 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58      CREATE INDEX
202e2 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 49 4e 44  .**     DROP IND
202e3 45 58 0a 2a 2a 20 20 20 20 20 63 72 65 61 74 69  EX.**     creati
202e4 6e 67 20 49 44 20 6c 69 73 74 73 0a 2a 2a 20 20  ng ID lists.**  
202e5 20 20 20 42 45 47 49 4e 20 54 52 41 4e 53 41 43     BEGIN TRANSAC
202e6 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d  TION.**     COMM
202e7 49 54 0a 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41  IT.**     ROLLBA
202e8 43 4b 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 75  CK.**.** $Id: bu
202e9 69 6c 64 2e 63 2c 76 20 31 2e 35 31 31 20 32 30  ild.c,v 1.511 20
202ea 30 38 2f 31 32 2f 33 30 20 30 36 3a 32 34 3a 35  08/12/30 06:24:5
202eb 38 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  8 danielk1977 Ex
202ec 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  p $.*/../*.** Th
202ed 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
202ee 6c 6c 65 64 20 77 68 65 6e 20 61 20 6e 65 77 20  lled when a new 
202ef 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73  SQL statement is
202f0 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a   beginning to.**
202f1 20 62 65 20 70 61 72 73 65 64 2e 20 20 49 6e 69   be parsed.  Ini
202f2 74 69 61 6c 69 7a 65 20 74 68 65 20 70 50 61 72  tialize the pPar
202f3 73 65 20 73 74 72 75 63 74 75 72 65 20 61 73 20  se structure as 
202f4 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  needed..*/.SQLIT
202f5 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
202f6 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65  qlite3BeginParse
202f7 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
202f8 69 6e 74 20 65 78 70 6c 61 69 6e 46 6c 61 67 29  int explainFlag)
202f9 7b 0a 20 20 70 50 61 72 73 65 2d 3e 65 78 70 6c  {.  pParse->expl
202fa 61 69 6e 20 3d 20 28 75 38 29 65 78 70 6c 61 69  ain = (u8)explai
202fb 6e 46 6c 61 67 3b 0a 20 20 70 50 61 72 73 65 2d  nFlag;.  pParse-
202fc 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d 0a 0a 23 69  >nVar = 0;.}..#i
202fd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
202fe 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f  T_SHARED_CACHE./
202ff 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 4c 6f  *.** The TableLo
20300 63 6b 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ck structure is 
20301 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 74 68 65  only used by the
20302 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
20303 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 6f 64 65 54  k() and.** codeT
20304 61 62 6c 65 4c 6f 63 6b 73 28 29 20 66 75 6e 63  ableLocks() func
20305 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  tions..*/.struct
20306 20 54 61 62 6c 65 4c 6f 63 6b 20 7b 0a 20 20 69   TableLock {.  i
20307 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
20308 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
20309 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ase containing t
2030a 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c  he table to be l
2030b 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  ocked */.  int i
2030c 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tab;            
2030d 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65  /* The root page
2030e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
2030f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20   be locked */.  
20310 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 20  u8 isWriteLock; 
20311 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
20312 20 77 72 69 74 65 20 6c 6f 63 6b 2e 20 20 46 61   write lock.  Fa
20313 6c 73 65 20 66 6f 72 20 61 20 72 65 61 64 20 6c  lse for a read l
20314 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ock */.  const c
20315 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a  har *zName;   /*
20316 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   Name of the tab
20317 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  le */.};../*.** 
20318 52 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20  Record the fact 
20319 74 68 61 74 20 77 65 20 77 61 6e 74 20 74 6f 20  that we want to 
2031a 6c 6f 63 6b 20 61 20 74 61 62 6c 65 20 61 74 20  lock a table at 
2031b 72 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a 2a 0a 2a  run-time.  .**.*
2031c 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62  * The table to b
2031d 65 20 6c 6f 63 6b 65 64 20 68 61 73 20 72 6f 6f  e locked has roo
2031e 74 20 70 61 67 65 20 69 54 61 62 20 61 6e 64 20  t page iTab and 
2031f 69 73 20 66 6f 75 6e 64 20 69 6e 20 64 61 74 61  is found in data
20320 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41 20 72  base iDb..** A r
20321 65 61 64 20 6f 72 20 61 20 77 72 69 74 65 20 6c  ead or a write l
20322 6f 63 6b 20 63 61 6e 20 62 65 20 74 61 6b 65 6e  ock can be taken
20323 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 69 73   depending on is
20324 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a  Writelock..**.**
20325 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75   This routine ju
20326 73 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 66  st records the f
20327 61 63 74 20 74 68 61 74 20 74 68 65 20 6c 6f 63  act that the loc
20328 6b 20 69 73 20 64 65 73 69 72 65 64 2e 20 20 54  k is desired.  T
20329 68 65 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 6d 61  he.** code to ma
2032a 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f 63 63 75  ke the lock occu
2032b 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62  r is generated b
2032c 79 20 61 20 6c 61 74 65 72 20 63 61 6c 6c 20 74  y a later call t
2032d 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f  o.** codeTableLo
2032e 63 6b 73 28 29 20 77 68 69 63 68 20 6f 63 63 75  cks() which occu
2032f 72 73 20 64 75 72 69 6e 67 20 73 71 6c 69 74 65  rs during sqlite
20330 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e  3FinishCoding().
20331 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20332 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54  TE void sqlite3T
20333 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50 61 72 73  ableLock(.  Pars
20334 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
20335 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
20336 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20  t */.  int iDb, 
20337 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
20338 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
20339 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  se containing th
2033a 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 20  e table to lock 
2033b 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20  */.  int iTab,  
2033c 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20          /* Root 
2033d 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
2033e 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c  he table to be l
2033f 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 73  ocked */.  u8 is
20340 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20 20 2f 2a  WriteLock,    /*
20341 20 54 72 75 65 20 66 6f 72 20 61 20 77 72 69 74   True for a writ
20342 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73  e lock */.  cons
20343 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20 2f  t char *zName  /
20344 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
20345 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64  ble to be locked
20346 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
20347 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 0a 20 20    int nBytes;.  
20348 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b 0a 0a 20  TableLock *p;.. 
20349 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20   if( iDb<0 ){.  
2034a 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
2034b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72   for(i=0; i<pPar
2034c 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20  se->nTableLock; 
2034d 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 26 70  i++){.    p = &p
2034e 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63  Parse->aTableLoc
2034f 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d  k[i];.    if( p-
20350 3e 69 44 62 3d 3d 69 44 62 20 26 26 20 70 2d 3e  >iDb==iDb && p->
20351 69 54 61 62 3d 3d 69 54 61 62 20 29 7b 0a 20 20  iTab==iTab ){.  
20352 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f      p->isWriteLo
20353 63 6b 20 3d 20 28 70 2d 3e 69 73 57 72 69 74 65  ck = (p->isWrite
20354 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 69 74 65 4c  Lock || isWriteL
20355 6f 63 6b 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ock);.      retu
20356 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rn;.    }.  }.. 
20357 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66   nBytes = sizeof
20358 28 54 61 62 6c 65 4c 6f 63 6b 29 20 2a 20 28 70  (TableLock) * (p
20359 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63  Parse->nTableLoc
2035a 6b 2b 31 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  k+1);.  pParse->
2035b 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 0a 20 20  aTableLock = .  
2035c 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61      sqlite3DbRea
2035d 6c 6c 6f 63 4f 72 46 72 65 65 28 70 50 61 72 73  llocOrFree(pPars
2035e 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e 61  e->db, pParse->a
2035f 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e 42 79 74 65  TableLock, nByte
20360 73 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  s);.  if( pParse
20361 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 29 7b 0a  ->aTableLock ){.
20362 20 20 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d      p = &pParse-
20363 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 70 50 61 72  >aTableLock[pPar
20364 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 2b  se->nTableLock++
20365 5d 3b 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d 20  ];.    p->iDb = 
20366 69 44 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62  iDb;.    p->iTab
20367 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e   = iTab;.    p->
20368 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 69 73  isWriteLock = is
20369 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 70  WriteLock;.    p
2036a 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b  ->zName = zName;
2036b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50  .  }else{.    pP
2036c 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b  arse->nTableLock
2036d 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 65   = 0;.    pParse
2036e 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
2036f 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ed = 1;.  }.}../
20370 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f  *.** Code an OP_
20371 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 75  TableLock instru
20372 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74  ction for each t
20373 61 62 6c 65 20 6c 6f 63 6b 65 64 20 62 79 20 74  able locked by t
20374 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  he.** statement 
20375 28 63 6f 6e 66 69 67 75 72 65 64 20 62 79 20 63  (configured by c
20376 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 54  alls to sqlite3T
20377 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a 2a 2f 0a  ableLock())..*/.
20378 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
20379 54 61 62 6c 65 4c 6f 63 6b 73 28 50 61 72 73 65  TableLocks(Parse
2037a 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74   *pParse){.  int
2037b 20 69 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 62   i;.  Vdbe *pVdb
2037c 65 3b 20 0a 0a 20 20 69 66 28 20 30 3d 3d 28 70  e; ..  if( 0==(p
2037d 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33 47 65  Vdbe = sqlite3Ge
2037e 74 56 64 62 65 28 70 50 61 72 73 65 29 29 20 29  tVdbe(pParse)) )
2037f 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
20380 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  }..  for(i=0; i<
20381 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
20382 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 54 61  ck; i++){.    Ta
20383 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d 20 26 70 50  bleLock *p = &pP
20384 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  arse->aTableLock
20385 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 70 31 20  [i];.    int p1 
20386 3d 20 70 2d 3e 69 44 62 3b 0a 20 20 20 20 73 71  = p->iDb;.    sq
20387 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
20388 70 56 64 62 65 2c 20 4f 50 5f 54 61 62 6c 65 4c  pVdbe, OP_TableL
20389 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e 69 54 61 62  ock, p1, p->iTab
2038a 2c 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b  , p->isWriteLock
2038b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2038c 20 20 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65          p->zName
2038d 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
2038e 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  }.}.#else.  #def
2038f 69 6e 65 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63  ine codeTableLoc
20390 6b 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ks(x).#endif../*
20391 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
20392 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72   is called after
20393 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74   a single SQL st
20394 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65 6e  atement has been
20395 0a 2a 2a 20 70 61 72 73 65 64 20 61 6e 64 20 61  .** parsed and a
20396 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 74 6f   VDBE program to
20397 20 65 78 65 63 75 74 65 20 74 68 61 74 20 73 74   execute that st
20398 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65 6e  atement has been
20399 0a 2a 2a 20 70 72 65 70 61 72 65 64 2e 20 20 54  .** prepared.  T
2039a 68 69 73 20 72 6f 75 74 69 6e 65 20 70 75 74 73  his routine puts
2039b 20 74 68 65 20 66 69 6e 69 73 68 69 6e 67 20 74   the finishing t
2039c 6f 75 63 68 65 73 20 6f 6e 20 74 68 65 0a 2a 2a  ouches on the.**
2039d 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61 6e   VDBE program an
2039e 64 20 72 65 73 65 74 73 20 74 68 65 20 70 50 61  d resets the pPa
2039f 72 73 65 20 73 74 72 75 63 74 75 72 65 20 66 6f  rse structure fo
203a0 72 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 70 61  r the next.** pa
203a1 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  rse..**.** Note 
203a2 74 68 61 74 20 69 66 20 61 6e 20 65 72 72 6f 72  that if an error
203a3 20 6f 63 63 75 72 72 65 64 2c 20 69 74 20 6d 69   occurred, it mi
203a4 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65 20  ght be the case 
203a5 74 68 61 74 0a 2a 2a 20 6e 6f 20 56 44 42 45 20  that.** no VDBE 
203a6 63 6f 64 65 20 77 61 73 20 67 65 6e 65 72 61 74  code was generat
203a7 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
203a8 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
203a9 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 50  e3FinishCoding(P
203aa 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
203ab 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
203ac 56 64 62 65 20 2a 76 3b 0a 0a 20 20 64 62 20 3d  Vdbe *v;..  db =
203ad 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
203ae 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
203af 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  led ) return;.  
203b0 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
203b1 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  ed ) return;.  i
203b2 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
203b3 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20  ) return;..  /* 
203b4 42 65 67 69 6e 20 62 79 20 67 65 6e 65 72 61 74  Begin by generat
203b5 69 6e 67 20 73 6f 6d 65 20 74 65 72 6d 69 6e 61  ing some termina
203b6 74 69 6f 6e 20 63 6f 64 65 20 61 74 20 74 68 65  tion code at the
203b7 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a   end of the.  **
203b8 20 76 64 62 65 20 70 72 6f 67 72 61 6d 0a 20 20   vdbe program.  
203b9 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  */.  v = sqlite3
203ba 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
203bb 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
203bc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
203bd 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a 0a  0(v, OP_Halt);..
203be 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6f 6b 69      /* The cooki
203bf 65 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 73 20  e mask contains 
203c0 6f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68  one bit for each
203c1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f   database file o
203c2 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 28 42 69 74  pen..    ** (Bit
203c3 20 30 20 69 73 20 66 6f 72 20 6d 61 69 6e 2c 20   0 is for main, 
203c4 62 69 74 20 31 20 69 73 20 66 6f 72 20 74 65 6d  bit 1 is for tem
203c5 70 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e  p, and so forth.
203c6 29 20 20 42 69 74 73 20 61 72 65 0a 20 20 20 20  )  Bits are.    
203c7 2a 2a 20 73 65 74 20 66 6f 72 20 65 61 63 68 20  ** set for each 
203c8 64 61 74 61 62 61 73 65 20 74 68 61 74 20 69 73  database that is
203c9 20 75 73 65 64 2e 20 20 47 65 6e 65 72 61 74 65   used.  Generate
203ca 20 63 6f 64 65 20 74 6f 20 73 74 61 72 74 20 61   code to start a
203cb 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  .    ** transact
203cc 69 6f 6e 20 6f 6e 20 65 61 63 68 20 75 73 65 64  ion on each used
203cd 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 6f   database and to
203ce 20 76 65 72 69 66 79 20 74 68 65 20 73 63 68 65   verify the sche
203cf 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a  ma cookie.    **
203d0 20 6f 6e 20 65 61 63 68 20 75 73 65 64 20 64 61   on each used da
203d1 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  tabase..    */. 
203d2 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63     if( pParse->c
203d3 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20 29 7b 0a 20  ookieGoto>0 ){. 
203d4 20 20 20 20 20 75 33 32 20 6d 61 73 6b 3b 0a 20       u32 mask;. 
203d5 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20       int iDb;.  
203d6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
203d7 75 6d 70 48 65 72 65 28 76 2c 20 70 50 61 72 73  umpHere(v, pPars
203d8 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 2d 31 29  e->cookieGoto-1)
203d9 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d  ;.      for(iDb=
203da 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 44 62 3c 64  0, mask=1; iDb<d
203db 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b 3c 3c 3d 31  b->nDb; mask<<=1
203dc 2c 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20  , iDb++){.      
203dd 20 20 69 66 28 20 28 6d 61 73 6b 20 26 20 70 50    if( (mask & pP
203de 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b  arse->cookieMask
203df 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )==0 ) continue;
203e0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
203e1 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
203e2 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 73   iDb);.        s
203e3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
203e4 28 76 2c 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f  (v,OP_Transactio
203e5 6e 2c 20 69 44 62 2c 20 28 6d 61 73 6b 20 26 20  n, iDb, (mask & 
203e6 70 50 61 72 73 65 2d 3e 77 72 69 74 65 4d 61 73  pParse->writeMas
203e7 6b 29 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20  k)!=0);.        
203e8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
203e9 32 28 76 2c 4f 50 5f 56 65 72 69 66 79 43 6f 6f  2(v,OP_VerifyCoo
203ea 6b 69 65 2c 20 69 44 62 2c 20 70 50 61 72 73 65  kie, iDb, pParse
203eb 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44  ->cookieValue[iD
203ec 62 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66  b]);.      }.#if
203ed 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
203ee 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
203ef 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e      {.        in
203f0 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  t i;.        for
203f1 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e  (i=0; i<pParse->
203f2 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b  nVtabLock; i++){
203f3 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
203f4 2a 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29  *vtab = (char *)
203f5 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f  pParse->apVtabLo
203f6 63 6b 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a 20 20  ck[i]->pVtab;.  
203f7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
203f8 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
203f9 56 42 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30 2c  VBegin, 0, 0, 0,
203fa 20 76 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b   vtab, P4_VTAB);
203fb 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
203fc 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 61 62     pParse->nVtab
203fd 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20  Lock = 0;.      
203fe 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20  }.#endif..      
203ff 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20  /* Once all the 
20400 63 6f 6f 6b 69 65 73 20 68 61 76 65 20 62 65 65  cookies have bee
20401 6e 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 74  n verified and t
20402 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e  ransactions open
20403 65 64 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62  ed, .      ** ob
20404 74 61 69 6e 20 74 68 65 20 72 65 71 75 69 72 65  tain the require
20405 64 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54  d table-locks. T
20406 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75  his is a no-op u
20407 6e 6c 65 73 73 20 74 68 65 20 0a 20 20 20 20 20  nless the .     
20408 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65   ** shared-cache
20409 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62   feature is enab
2040a 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  led..      */.  
2040b 20 20 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63      codeTableLoc
2040c 6b 73 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  ks(pParse);.    
2040d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2040e 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2040f 30 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  0, pParse->cooki
20410 65 47 6f 74 6f 29 3b 0a 20 20 20 20 7d 0a 0a 23  eGoto);.    }..#
20411 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20412 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28  IT_TRACE.    if(
20413 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
20414 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 68 61 6e  ){.      /* Chan
20415 67 65 20 74 68 65 20 50 34 20 61 72 67 75 6d 65  ge the P4 argume
20416 6e 74 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  nt of the first 
20417 6f 70 63 6f 64 65 20 28 77 68 69 63 68 20 77 69  opcode (which wi
20418 6c 6c 20 61 6c 77 61 79 73 20 62 65 0a 20 20 20  ll always be.   
20419 20 20 20 2a 2a 20 61 6e 20 4f 50 5f 54 72 61 63     ** an OP_Trac
2041a 65 29 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6d  e) to be the com
2041b 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68  plete text of th
2041c 65 20 63 75 72 72 65 6e 74 20 53 51 4c 20 73 74  e current SQL st
2041d 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 2a  atement..      *
2041e 2f 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a  /.      VdbeOp *
2041f 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  pOp = sqlite3Vdb
20420 65 47 65 74 4f 70 28 76 2c 20 30 29 3b 0a 20 20  eGetOp(v, 0);.  
20421 20 20 20 20 69 66 28 20 70 4f 70 20 26 26 20 70      if( pOp && p
20422 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54  Op->opcode==OP_T
20423 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20  race ){.        
20424 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
20425 65 50 34 28 76 2c 20 30 2c 20 70 50 61 72 73 65  eP4(v, 0, pParse
20426 2d 3e 7a 53 71 6c 2c 0a 20 20 20 20 20 20 20 20  ->zSql,.        
20427 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20428 20 20 20 20 28 69 6e 74 29 28 70 50 61 72 73 65      (int)(pParse
20429 2d 3e 7a 54 61 69 6c 20 2d 20 70 50 61 72 73 65  ->zTail - pParse
2042a 2d 3e 7a 53 71 6c 29 29 3b 0a 20 20 20 20 20 20  ->zSql));.      
2042b 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  }.    }.#endif /
2042c 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  * SQLITE_OMIT_TR
2042d 41 43 45 20 2a 2f 0a 20 20 7d 0a 0a 0a 20 20 2f  ACE */.  }...  /
2042e 2a 20 47 65 74 20 74 68 65 20 56 44 42 45 20 70  * Get the VDBE p
2042f 72 6f 67 72 61 6d 20 72 65 61 64 79 20 66 6f 72  rogram ready for
20430 20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a 2f 0a   execution.  */.
20431 20 20 69 66 28 20 76 20 26 26 20 70 50 61 72 73    if( v && pPars
20432 65 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 21 64  e->nErr==0 && !d
20433 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
20434 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
20435 5f 44 45 42 55 47 0a 20 20 20 20 46 49 4c 45 20  _DEBUG.    FILE 
20436 2a 74 72 61 63 65 20 3d 20 28 64 62 2d 3e 66 6c  *trace = (db->fl
20437 61 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62  ags & SQLITE_Vdb
20438 65 54 72 61 63 65 29 21 3d 30 20 3f 20 73 74 64  eTrace)!=0 ? std
20439 6f 75 74 20 3a 20 30 3b 0a 20 20 20 20 73 71 6c  out : 0;.    sql
2043a 69 74 65 33 56 64 62 65 54 72 61 63 65 28 76 2c  ite3VdbeTrace(v,
2043b 20 74 72 61 63 65 29 3b 0a 23 65 6e 64 69 66 0a   trace);.#endif.
2043c 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
2043d 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
2043e 63 68 65 3d 3d 30 20 29 3b 20 20 2f 2a 20 44 69  che==0 );  /* Di
2043f 73 61 62 6c 65 73 20 61 6e 64 20 72 65 2d 65 6e  sables and re-en
20440 61 62 6c 65 73 20 6d 61 74 63 68 20 2a 2f 0a 20  ables match */. 
20441 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61     sqlite3VdbeMa
20442 6b 65 52 65 61 64 79 28 76 2c 20 70 50 61 72 73  keReady(v, pPars
20443 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72 73 65 2d  e->nVar, pParse-
20444 3e 6e 4d 65 6d 2b 33 2c 0a 20 20 20 20 20 20 20  >nMem+3,.       
20445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20446 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 33    pParse->nTab+3
20447 2c 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69  , pParse->explai
20448 6e 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  n);.    pParse->
20449 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45  rc = SQLITE_DONE
2044a 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f  ;.    pParse->co
2044b 6c 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a 20  lNamesSet = 0;. 
2044c 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 73   }else if( pPars
2044d 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e->rc==SQLITE_OK
2044e 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
2044f 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
20450 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d  R;.  }.  pParse-
20451 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50 61  >nTab = 0;.  pPa
20452 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 20  rse->nMem = 0;. 
20453 20 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20   pParse->nSet = 
20454 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61  0;.  pParse->nVa
20455 72 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d  r = 0;.  pParse-
20456 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30 3b  >cookieMask = 0;
20457 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  .  pParse->cooki
20458 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  eGoto = 0;.}../*
20459 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73  .** Run the pars
2045a 65 72 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  er and code gene
2045b 72 61 74 6f 72 20 72 65 63 75 72 73 69 76 65 6c  rator recursivel
2045c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65  y in order to ge
2045d 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
2045e 6f 72 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  or the SQL state
2045f 6d 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20  ment given onto 
20460 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70  the end of the p
20461 50 61 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a  Parse context.**
20462 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
20463 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20   construction.  
20464 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20  When the parser 
20465 69 73 20 72 75 6e 20 72 65 63 75 72 73 69 76 65  is run recursive
20466 6c 79 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c 20  ly.** this way, 
20467 74 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c  the final OP_Hal
20468 74 20 69 73 20 6e 6f 74 20 61 70 70 65 6e 64 65  t is not appende
20469 64 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 69 74  d and other init
2046a 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e  ialization.** an
2046b 64 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73  d finalization s
2046c 74 65 70 73 20 61 72 65 20 6f 6d 69 74 74 65 64  teps are omitted
2046d 20 62 65 63 61 75 73 65 20 74 68 6f 73 65 20 61   because those a
2046e 72 65 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74  re handling by t
2046f 68 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20  he.** outermost 
20470 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  parser..**.** No
20471 74 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20  t everything is 
20472 6e 65 73 74 61 62 6c 65 2e 20 20 54 68 69 73 20  nestable.  This 
20473 66 61 63 69 6c 69 74 79 20 69 73 20 64 65 73 69  facility is desi
20474 67 6e 65 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a  gned to permit.*
20475 2a 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  * INSERT, UPDATE
20476 2c 20 61 6e 64 20 44 45 4c 45 54 45 20 6f 70 65  , and DELETE ope
20477 72 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20  rations against 
20478 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20  SQLITE_MASTER.  
20479 55 73 65 0a 2a 2a 20 63 61 72 65 20 69 66 20 79  Use.** care if y
2047a 6f 75 20 64 65 63 69 64 65 20 74 6f 20 74 72 79  ou decide to try
2047b 20 74 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75   to use this rou
2047c 74 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74  tine for some ot
2047d 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f  her purposes..*/
2047e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2047f 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74  void sqlite3Nest
20480 65 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70  edParse(Parse *p
20481 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
20482 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
20483 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
20484 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20    char *zSql;.  
20485 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20  char *zErrMsg = 
20486 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  0;.  sqlite3 *db
20487 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23   = pParse->db;.#
20488 20 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a 20   define SAVE_SZ 
20489 20 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29 20   (sizeof(Parse) 
2048a 2d 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73 65  - offsetof(Parse
2048b 2c 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20 73  ,nVar)).  char s
2048c 61 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b  aveBuf[SAVE_SZ];
2048d 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
2048e 6e 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20  nErr ) return;. 
2048f 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
20490 3e 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20 2f  >nested<10 );  /
20491 2a 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64  * Nesting should
20492 20 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69   only be of limi
20493 74 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20 76  ted depth */.  v
20494 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
20495 6d 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73  mat);.  zSql = s
20496 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64  qlite3VMPrintf(d
20497 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
20498 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
20499 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a   if( zSql==0 ){.
2049a 20 20 20 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a      return;   /*
2049b 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68   A malloc must h
2049c 61 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20  ave failed */.  
2049d 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  }.  pParse->nest
2049e 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 73  ed++;.  memcpy(s
2049f 61 76 65 42 75 66 2c 20 26 70 50 61 72 73 65 2d  aveBuf, &pParse-
204a0 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29 3b  >nVar, SAVE_SZ);
204a1 0a 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73  .  memset(&pPars
204a2 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41 56 45  e->nVar, 0, SAVE
204a3 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  _SZ);.  sqlite3R
204a4 75 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c  unParser(pParse,
204a5 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29   zSql, &zErrMsg)
204a6 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
204a7 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a  e(db, zErrMsg);.
204a8 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
204a9 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d  db, zSql);.  mem
204aa 63 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56 61  cpy(&pParse->nVa
204ab 72 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56 45  r, saveBuf, SAVE
204ac 5f 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  _SZ);.  pParse->
204ad 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a  nested--;.}../*.
204ae 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e  ** Locate the in
204af 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72  -memory structur
204b0 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
204b1 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61   a particular da
204b2 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20  tabase.** table 
204b3 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f  given the name o
204b4 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64  f that table and
204b5 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68   (optionally) th
204b6 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a  e name of the.**
204b7 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
204b8 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20  ning the table. 
204b9 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
204ba 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a  not found..**.**
204bb 20 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73   If zDatabase is
204bc 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65   0, all database
204bd 73 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66  s are searched f
204be 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  or the table and
204bf 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61   the.** first ma
204c0 74 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20  tching table is 
204c1 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63  returned.  (No c
204c2 68 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c  hecking for dupl
204c3 69 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e  icate table.** n
204c4 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20  ames is done.)  
204c5 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72  The search order
204c6 20 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c 20   is TEMP first, 
204c7 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20  then MAIN, then 
204c8 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79  any.** auxiliary
204c9 20 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64   databases added
204ca 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43   using the ATTAC
204cb 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a  H command..**.**
204cc 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65   See also sqlite
204cd 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 0a  3LocateTable()..
204ce 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
204cf 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
204d0 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65  FindTable(sqlite
204d1 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
204d2 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20  r *zName, const 
204d3 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 29  char *zDatabase)
204d4 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 30  {.  Table *p = 0
204d5 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
204d6 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74   nName;.  assert
204d7 28 20 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20  ( zName!=0 );.  
204d8 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
204d9 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 6d 65 29  trlen(db, zName)
204da 20 2b 20 31 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d   + 1;.  for(i=OM
204db 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d  IT_TEMPDB; i<db-
204dc 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
204dd 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20  int j = (i<2) ? 
204de 69 5e 31 20 3a 20 69 3b 20 20 20 2f 2a 20 53 65  i^1 : i;   /* Se
204df 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65  arch TEMP before
204e0 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28   MAIN */.    if(
204e1 20 7a 44 61 74 61 62 61 73 65 21 3d 30 20 26 26   zDatabase!=0 &&
204e2 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
204e3 7a 44 61 74 61 62 61 73 65 2c 20 64 62 2d 3e 61  zDatabase, db->a
204e4 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63  Db[j].zName) ) c
204e5 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 20 3d  ontinue;.    p =
204e6 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
204e7 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63  (&db->aDb[j].pSc
204e8 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a  hema->tblHash, z
204e9 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
204ea 20 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b    if( p ) break;
204eb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
204ec 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65  .}../*.** Locate
204ed 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73   the in-memory s
204ee 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65  tructure that de
204ef 73 63 72 69 62 65 73 20 61 20 70 61 72 74 69 63  scribes a partic
204f0 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a  ular database.**
204f1 20 74 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65   table given the
204f2 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61   name of that ta
204f3 62 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61  ble and (optiona
204f4 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66  lly) the name of
204f5 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
204f6 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
204f7 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e  table.  Return N
204f8 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64  ULL if not found
204f9 2e 20 20 41 6c 73 6f 20 6c 65 61 76 65 20 61 6e  .  Also leave an
204fa 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67  .** error messag
204fb 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72  e in pParse->zEr
204fc 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rMsg..**.** The 
204fd 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
204fe 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
204ff 61 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64 54  and sqlite3FindT
20500 61 62 6c 65 28 29 20 69 73 20 74 68 61 74 20 74  able() is that t
20501 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6c  his.** routine l
20502 65 61 76 65 73 20 61 6e 20 65 72 72 6f 72 20 6d  eaves an error m
20503 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
20504 2d 3e 7a 45 72 72 4d 73 67 20 77 68 65 72 65 0a  ->zErrMsg where.
20505 2a 2a 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61  ** sqlite3FindTa
20506 62 6c 65 28 29 20 64 6f 65 73 20 6e 6f 74 2e 0a  ble() does not..
20507 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20508 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
20509 4c 6f 63 61 74 65 54 61 62 6c 65 28 0a 20 20 50  LocateTable(.  P
2050a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2050b 20 20 20 20 20 20 2f 2a 20 63 6f 6e 74 65 78 74        /* context
2050c 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 70   in which to rep
2050d 6f 72 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  ort errors */.  
2050e 69 6e 74 20 69 73 56 69 65 77 2c 20 20 20 20 20  int isView,     
2050f 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
20510 66 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20  f looking for a 
20511 56 49 45 57 20 72 61 74 68 65 72 20 74 68 61 6e  VIEW rather than
20512 20 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 6f   a TABLE */.  co
20513 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
20514 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
20515 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65  the table we are
20516 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 2a 2f 0a   looking for */.
20517 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
20518 62 61 73 65 20 20 20 20 20 2f 2a 20 4e 61 6d 65  base     /* Name
20519 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
2051a 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c  .  Might be NULL
2051b 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
2051c 70 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68  p;..  /* Read th
2051d 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
2051e 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  a. If an error o
2051f 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20  ccurs, leave an 
20520 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20  error message.  
20521 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70  ** and code in p
20522 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e  Parse and return
20523 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20   NULL. */.  if( 
20524 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
20525 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
20526 72 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75  rse) ){.    retu
20527 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 20 3d  rn 0;.  }..  p =
20528 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
20529 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e  e(pParse->db, zN
2052a 61 6d 65 2c 20 7a 44 62 61 73 65 29 3b 0a 20 20  ame, zDbase);.  
2052b 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20  if( p==0 ){.    
2052c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 73 67  const char *zMsg
2052d 20 3d 20 69 73 56 69 65 77 20 3f 20 22 6e 6f 20   = isView ? "no 
2052e 73 75 63 68 20 76 69 65 77 22 20 3a 20 22 6e 6f  such view" : "no
2052f 20 73 75 63 68 20 74 61 62 6c 65 22 3b 0a 20 20   such table";.  
20530 20 20 69 66 28 20 7a 44 62 61 73 65 20 29 7b 0a    if( zDbase ){.
20531 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
20532 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
20533 73 3a 20 25 73 2e 25 73 22 2c 20 7a 4d 73 67 2c  s: %s.%s", zMsg,
20534 20 7a 44 62 61 73 65 2c 20 7a 4e 61 6d 65 29 3b   zDbase, zName);
20535 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
20536 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
20537 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25  g(pParse, "%s: %
20538 73 22 2c 20 7a 4d 73 67 2c 20 7a 4e 61 6d 65 29  s", zMsg, zName)
20539 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  ;.    }.    pPar
2053a 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20  se->checkSchema 
2053b 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
2053c 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  n p;.}../*.** Lo
2053d 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  cate the in-memo
2053e 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ry structure tha
2053f 74 20 64 65 73 63 72 69 62 65 73 20 0a 2a 2a 20  t describes .** 
20540 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64  a particular ind
20541 65 78 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d  ex given the nam
20542 65 20 6f 66 20 74 68 61 74 20 69 6e 64 65 78 0a  e of that index.
20543 2a 2a 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 20  ** and the name 
20544 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
20545 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
20546 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 52 65 74 75  e index..** Retu
20547 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66  rn NULL if not f
20548 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a  ound..**.** If z
20549 44 61 74 61 62 61 73 65 20 69 73 20 30 2c 20 61  Database is 0, a
2054a 6c 6c 20 64 61 74 61 62 61 73 65 73 20 61 72 65  ll databases are
2054b 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68   searched for th
2054c 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 74  e.** table and t
2054d 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e  he first matchin
2054e 67 20 69 6e 64 65 78 20 69 73 20 72 65 74 75 72  g index is retur
2054f 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69  ned.  (No checki
20550 6e 67 0a 2a 2a 20 66 6f 72 20 64 75 70 6c 69 63  ng.** for duplic
20551 61 74 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20  ate index names 
20552 69 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73  is done.)  The s
20553 65 61 72 63 68 20 6f 72 64 65 72 20 69 73 0a 2a  earch order is.*
20554 2a 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74 68  * TEMP first, th
20555 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 6e  en MAIN, then an
20556 79 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  y auxiliary data
20557 62 61 73 65 73 20 61 64 64 65 64 0a 2a 2a 20 75  bases added.** u
20558 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48 20  sing the ATTACH 
20559 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49  command..*/.SQLI
2055a 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78  TE_PRIVATE Index
2055b 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64   *sqlite3FindInd
2055c 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ex(sqlite3 *db, 
2055d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
2055e 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
2055f 44 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 20  Db){.  Index *p 
20560 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  = 0;.  int i;.  
20561 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  int nName = sqli
20562 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e  te3Strlen(db, zN
20563 61 6d 65 29 2b 31 3b 0a 20 20 66 6f 72 28 69 3d  ame)+1;.  for(i=
20564 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64  OMIT_TEMPDB; i<d
20565 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
20566 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20    int j = (i<2) 
20567 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53  ? i^1 : i;  /* S
20568 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72  earch TEMP befor
20569 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 53 63  e MAIN */.    Sc
2056a 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20  hema *pSchema = 
2056b 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65  db->aDb[j].pSche
2056c 6d 61 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20  ma;.    if( zDb 
2056d 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
2056e 70 28 7a 44 62 2c 20 64 62 2d 3e 61 44 62 5b 6a  p(zDb, db->aDb[j
2056f 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69  ].zName) ) conti
20570 6e 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  nue;.    assert(
20571 20 70 53 63 68 65 6d 61 20 7c 7c 20 28 6a 3d 3d   pSchema || (j==
20572 31 20 26 26 20 21 64 62 2d 3e 61 44 62 5b 31 5d  1 && !db->aDb[1]
20573 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28  .pBt) );.    if(
20574 20 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20   pSchema ){.    
20575 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73    p = sqlite3Has
20576 68 46 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e  hFind(&pSchema->
20577 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20  idxHash, zName, 
20578 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20  nName);.    }.  
20579 20 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b    if( p ) break;
2057a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
2057b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69  .}../*.** Reclai
2057c 6d 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65  m the memory use
2057d 64 20 62 79 20 61 6e 20 69 6e 64 65 78 0a 2a 2f  d by an index.*/
2057e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
2057f 65 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29  eIndex(Index *p)
20580 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
20581 3d 20 70 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 3b  = p->pTable->db;
20582 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
20583 28 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29  (db, p->zColAff)
20584 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
20585 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a  e(db, p);.}../*.
20586 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 67 69  ** Remove the gi
20587 76 65 6e 20 69 6e 64 65 78 20 66 72 6f 6d 20 74  ven index from t
20588 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61  he index hash ta
20589 62 6c 65 2c 20 61 6e 64 20 66 72 65 65 0a 2a 2a  ble, and free.**
2058a 20 69 74 73 20 6d 65 6d 6f 72 79 20 73 74 72 75   its memory stru
2058b 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ctures..**.** Th
2058c 65 20 69 6e 64 65 78 20 69 73 20 72 65 6d 6f 76  e index is remov
2058d 65 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ed from the data
2058e 62 61 73 65 20 68 61 73 68 20 74 61 62 6c 65 73  base hash tables
2058f 20 62 75 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f   but.** it is no
20590 74 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20  t unlinked from 
20591 74 68 65 20 54 61 62 6c 65 20 74 68 61 74 20 69  the Table that i
20592 74 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 55 6e  t indexes..** Un
20593 6c 69 6e 6b 69 6e 67 20 66 72 6f 6d 20 74 68 65  linking from the
20594 20 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 64   Table must be d
20595 6f 6e 65 20 62 79 20 74 68 65 20 63 61 6c 6c 69  one by the calli
20596 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  ng function..*/.
20597 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
20598 74 65 44 65 6c 65 74 65 49 6e 64 65 78 28 49 6e  teDeleteIndex(In
20599 64 65 78 20 2a 70 29 7b 0a 20 20 49 6e 64 65 78  dex *p){.  Index
2059a 20 2a 70 4f 6c 64 3b 0a 20 20 63 6f 6e 73 74 20   *pOld;.  const 
2059b 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d  char *zName = p-
2059c 3e 7a 4e 61 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20  >zName;..  pOld 
2059d 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
2059e 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d  ert(&p->pSchema-
2059f 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  >idxHash, zName,
205a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
205a1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
205a2 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
205a3 65 29 2b 31 2c 20 30 29 3b 0a 20 20 61 73 73 65  e)+1, 0);.  asse
205a4 72 74 28 20 70 4f 6c 64 3d 3d 30 20 7c 7c 20 70  rt( pOld==0 || p
205a5 4f 6c 64 3d 3d 70 20 29 3b 0a 20 20 66 72 65 65  Old==p );.  free
205a6 49 6e 64 65 78 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  Index(p);.}../*.
205a7 2a 2a 20 46 6f 72 20 74 68 65 20 69 6e 64 65 78  ** For the index
205a8 20 63 61 6c 6c 65 64 20 7a 49 64 78 4e 61 6d 65   called zIdxName
205a9 20 77 68 69 63 68 20 69 73 20 66 6f 75 6e 64 20   which is found 
205aa 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
205ab 69 44 62 2c 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74  iDb,.** unlike t
205ac 68 61 74 20 69 6e 64 65 78 20 66 72 6f 6d 20 69  hat index from i
205ad 74 73 20 54 61 62 6c 65 20 74 68 65 6e 20 72 65  ts Table then re
205ae 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 66  move the index f
205af 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78  rom.** the index
205b0 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20   hash table and 
205b1 66 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  free all memory 
205b2 73 74 72 75 63 74 75 72 65 73 20 61 73 73 6f 63  structures assoc
205b3 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  iated.** with th
205b4 65 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49  e index..*/.SQLI
205b5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
205b6 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
205b7 44 65 6c 65 74 65 49 6e 64 65 78 28 73 71 6c 69  DeleteIndex(sqli
205b8 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
205b9 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49  , const char *zI
205ba 64 78 4e 61 6d 65 29 7b 0a 20 20 49 6e 64 65 78  dxName){.  Index
205bb 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20   *pIndex;.  int 
205bc 6c 65 6e 3b 0a 20 20 48 61 73 68 20 2a 70 48 61  len;.  Hash *pHa
205bd 73 68 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  sh = &db->aDb[iD
205be 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48  b].pSchema->idxH
205bf 61 73 68 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 71  ash;..  len = sq
205c0 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20  lite3Strlen(db, 
205c1 7a 49 64 78 4e 61 6d 65 29 3b 0a 20 20 70 49 6e  zIdxName);.  pIn
205c2 64 65 78 20 3d 20 73 71 6c 69 74 65 33 48 61 73  dex = sqlite3Has
205c3 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a  hInsert(pHash, z
205c4 49 64 78 4e 61 6d 65 2c 20 6c 65 6e 2b 31 2c 20  IdxName, len+1, 
205c5 30 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78  0);.  if( pIndex
205c6 20 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e 64   ){.    if( pInd
205c7 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64  ex->pTable->pInd
205c8 65 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20  ex==pIndex ){.  
205c9 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62      pIndex->pTab
205ca 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e  le->pIndex = pIn
205cb 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  dex->pNext;.    
205cc 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64  }else{.      Ind
205cd 65 78 20 2a 70 3b 0a 20 20 20 20 20 20 66 6f 72  ex *p;.      for
205ce 28 70 3d 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c  (p=pIndex->pTabl
205cf 65 2d 3e 70 49 6e 64 65 78 3b 20 70 20 26 26 20  e->pIndex; p && 
205d0 70 2d 3e 70 4e 65 78 74 21 3d 70 49 6e 64 65 78  p->pNext!=pIndex
205d1 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 7d 0a  ; p=p->pNext){}.
205d2 20 20 20 20 20 20 69 66 28 20 70 20 26 26 20 70        if( p && p
205d3 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64 65 78 20  ->pNext==pIndex 
205d4 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4e  ){.        p->pN
205d5 65 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e  ext = pIndex->pN
205d6 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ext;.      }.   
205d7 20 7d 0a 20 20 20 20 66 72 65 65 49 6e 64 65 78   }.    freeIndex
205d8 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20  (pIndex);.  }.  
205d9 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
205da 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
205db 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73  s;.}../*.** Eras
205dc 65 20 61 6c 6c 20 73 63 68 65 6d 61 20 69 6e 66  e all schema inf
205dd 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68  ormation from th
205de 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68  e in-memory hash
205df 20 74 61 62 6c 65 73 20 6f 66 0a 2a 2a 20 61 20   tables of.** a 
205e0 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 2e  single database.
205e1 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
205e2 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 63 6c  s called to recl
205e3 61 69 6d 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65  aim memory.** be
205e4 66 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73  fore the databas
205e5 65 20 63 6c 6f 73 65 73 2e 20 20 49 74 20 69 73  e closes.  It is
205e6 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20 64 75 72   also called dur
205e7 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a  ing a rollback.*
205e8 2a 20 69 66 20 74 68 65 72 65 20 77 65 72 65 20  * if there were 
205e9 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 64  schema changes d
205ea 75 72 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61  uring the transa
205eb 63 74 69 6f 6e 20 6f 72 20 69 66 20 61 0a 2a 2a  ction or if a.**
205ec 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 6d   schema-cookie m
205ed 69 73 6d 61 74 63 68 20 6f 63 63 75 72 73 2e 0a  ismatch occurs..
205ee 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 3c 3d 30 20  **.** If iDb<=0 
205ef 74 68 65 6e 20 72 65 73 65 74 20 74 68 65 20 69  then reset the i
205f0 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 74  nternal schema t
205f1 61 62 6c 65 73 20 66 6f 72 20 61 6c 6c 20 64 61  ables for all da
205f2 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 73 2e  tabase.** files.
205f3 20 20 49 66 20 69 44 62 3e 3d 32 20 74 68 65 6e    If iDb>=2 then
205f4 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72   reset the inter
205f5 6e 61 6c 20 73 63 68 65 6d 61 20 66 6f 72 20 6f  nal schema for o
205f6 6e 6c 79 20 74 68 65 0a 2a 2a 20 73 69 6e 67 6c  nly the.** singl
205f7 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64  e file indicated
205f8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
205f9 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
205fa 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
205fb 65 6d 61 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ema(sqlite3 *db,
205fc 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e 74   int iDb){.  int
205fd 20 69 2c 20 6a 3b 0a 20 20 61 73 73 65 72 74 28   i, j;.  assert(
205fe 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64   iDb>=0 && iDb<d
205ff 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 69 66 28  b->nDb );..  if(
20600 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73   iDb==0 ){.    s
20601 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
20602 41 6c 6c 28 64 62 29 3b 0a 20 20 7d 0a 20 20 66  All(db);.  }.  f
20603 6f 72 28 69 3d 69 44 62 3b 20 69 3c 64 62 2d 3e  or(i=iDb; i<db->
20604 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44  nDb; i++){.    D
20605 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  b *pDb = &db->aD
20606 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44  b[i];.    if( pD
20607 62 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  b->pSchema ){.  
20608 20 20 20 20 61 73 73 65 72 74 28 69 3d 3d 31 20      assert(i==1 
20609 7c 7c 20 28 70 44 62 2d 3e 70 42 74 20 26 26 20  || (pDb->pBt && 
2060a 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
2060b 73 4d 75 74 65 78 28 70 44 62 2d 3e 70 42 74 29  sMutex(pDb->pBt)
2060c 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
2060d 33 53 63 68 65 6d 61 46 72 65 65 28 70 44 62 2d  3SchemaFree(pDb-
2060e 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d  >pSchema);.    }
2060f 0a 20 20 20 20 69 66 28 20 69 44 62 3e 30 20 29  .    if( iDb>0 )
20610 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
20611 73 73 65 72 74 28 20 69 44 62 3d 3d 30 20 29 3b  ssert( iDb==0 );
20612 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20  .  db->flags &= 
20613 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  ~SQLITE_InternCh
20614 61 6e 67 65 73 3b 0a 20 20 73 71 6c 69 74 65 33  anges;.  sqlite3
20615 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
20616 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20  );..  /* If one 
20617 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 61  or more of the a
20618 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
20619 65 20 66 69 6c 65 73 20 68 61 73 20 62 65 65 6e  e files has been
2061a 20 63 6c 6f 73 65 64 2c 0a 20 20 2a 2a 20 74 68   closed,.  ** th
2061b 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66  en remove them f
2061c 72 6f 6d 20 74 68 65 20 61 75 78 69 6c 69 61 72  rom the auxiliar
2061d 79 20 64 61 74 61 62 61 73 65 20 6c 69 73 74 2e  y database list.
2061e 20 20 57 65 20 74 61 6b 65 20 74 68 65 0a 20 20    We take the.  
2061f 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74  ** opportunity t
20620 6f 20 64 6f 20 74 68 69 73 20 68 65 72 65 20 73  o do this here s
20621 69 6e 63 65 20 77 65 20 68 61 76 65 20 6a 75 73  ince we have jus
20622 74 20 64 65 6c 65 74 65 64 20 61 6c 6c 20 6f 66  t deleted all of
20623 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61   the.  ** schema
20624 20 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e 64   hash tables and
20625 20 74 68 65 72 65 66 6f 72 65 20 64 6f 20 6e 6f   therefore do no
20626 74 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61  t have to make a
20627 6e 79 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20  ny changes.  ** 
20628 74 6f 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 20  to any of those 
20629 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66  tables..  */.  f
2062a 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
2062b 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72  b; i++){.    str
2062c 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64  uct Db *pDb = &d
2062d 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69  b->aDb[i];.    i
2062e 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29  f( pDb->pBt==0 )
2062f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 44 62 2d  {.      if( pDb-
20630 3e 70 41 75 78 20 26 26 20 70 44 62 2d 3e 78 46  >pAux && pDb->xF
20631 72 65 65 41 75 78 20 29 20 70 44 62 2d 3e 78 46  reeAux ) pDb->xF
20632 72 65 65 41 75 78 28 70 44 62 2d 3e 70 41 75 78  reeAux(pDb->pAux
20633 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 41  );.      pDb->pA
20634 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ux = 0;.    }.  
20635 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 32 3b 20 69  }.  for(i=j=2; i
20636 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
20637 20 20 20 20 73 74 72 75 63 74 20 44 62 20 2a 70      struct Db *p
20638 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d  Db = &db->aDb[i]
20639 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70  ;.    if( pDb->p
2063a 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  Bt==0 ){.      s
2063b 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2063c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20   pDb->zName);.  
2063d 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3d      pDb->zName =
2063e 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e   0;.      contin
2063f 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ue;.    }.    if
20640 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20 20 20 64  ( j<i ){.      d
20641 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64 62 2d 3e  b->aDb[j] = db->
20642 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  aDb[i];.    }.  
20643 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 6d 65 6d    j++;.  }.  mem
20644 73 65 74 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2c  set(&db->aDb[j],
20645 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d 6a 29 2a   0, (db->nDb-j)*
20646 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 6a  sizeof(db->aDb[j
20647 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d  ]));.  db->nDb =
20648 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44   j;.  if( db->nD
20649 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61 44 62 21  b<=2 && db->aDb!
2064a 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29  =db->aDbStatic )
2064b 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 62 2d  {.    memcpy(db-
2064c 3e 61 44 62 53 74 61 74 69 63 2c 20 64 62 2d 3e  >aDbStatic, db->
2064d 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66 28 64 62  aDb, 2*sizeof(db
2064e 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20 20 20 20  ->aDb[0]));.    
2064f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
20650 2c 20 64 62 2d 3e 61 44 62 29 3b 0a 20 20 20 20  , db->aDb);.    
20651 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44  db->aDb = db->aD
20652 62 53 74 61 74 69 63 3b 0a 20 20 7d 0a 7d 0a 0a  bStatic;.  }.}..
20653 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
20654 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  ne is called whe
20655 6e 20 61 20 63 6f 6d 6d 69 74 20 6f 63 63 75 72  n a commit occur
20656 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
20657 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
20658 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43  3CommitInternalC
20659 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a  hanges(sqlite3 *
2065a 64 62 29 7b 0a 20 20 64 62 2d 3e 66 6c 61 67 73  db){.  db->flags
2065b 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65   &= ~SQLITE_Inte
2065c 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a  rnChanges;.}../*
2065d 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f  .** Clear the co
2065e 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20  lumn names from 
2065f 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2e  a table or view.
20660 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
20661 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d  sqliteResetColum
20662 6e 4e 61 6d 65 73 28 54 61 62 6c 65 20 2a 70 54  nNames(Table *pT
20663 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  able){.  int i;.
20664 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
20665 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
20666 70 54 61 62 6c 65 2d 3e 64 62 3b 0a 20 20 61 73  pTable->db;.  as
20667 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20  sert( pTable!=0 
20668 29 3b 0a 20 20 69 66 28 20 28 70 43 6f 6c 20 3d  );.  if( (pCol =
20669 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 21 3d   pTable->aCol)!=
2066a 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  0 ){.    for(i=0
2066b 3b 20 69 3c 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  ; i<pTable->nCol
2066c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
2066d 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2066e 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e  ree(db, pCol->zN
2066f 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ame);.      sqli
20670 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
20671 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a  , pCol->pDflt);.
20672 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
20673 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54  ree(db, pCol->zT
20674 79 70 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ype);.      sqli
20675 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
20676 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20  ol->zColl);.    
20677 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
20678 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ree(db, pTable->
20679 61 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 70 54 61  aCol);.  }.  pTa
2067a 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20  ble->aCol = 0;. 
2067b 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
2067c 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f  0;.}../*.** Remo
2067d 76 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61  ve the memory da
2067e 74 61 20 73 74 72 75 63 74 75 72 65 73 20 61 73  ta structures as
2067f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
20680 65 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c 65  e given.** Table
20681 2e 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72  .  No changes ar
20682 65 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 62  e made to disk b
20683 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  y this routine..
20684 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
20685 6e 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73 20  ne just deletes 
20686 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75  the data structu
20687 72 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74  re.  It does not
20688 20 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 74   unlink.** the t
20689 61 62 6c 65 20 64 61 74 61 20 73 74 72 75 63 74  able data struct
2068a 75 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73  ure from the has
2068b 68 20 74 61 62 6c 65 2e 20 20 4e 6f 72 20 64 6f  h table.  Nor do
2068c 65 73 20 69 74 20 72 65 6d 6f 76 65 0a 2a 2a 20  es it remove.** 
2068d 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 66 72 6f  foreign keys fro
2068e 6d 20 74 68 65 20 73 71 6c 69 74 65 2e 61 46 4b  m the sqlite.aFK
2068f 65 79 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20  ey hash table.  
20690 42 75 74 20 69 74 20 64 6f 65 73 20 64 65 73 74  But it does dest
20691 72 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74  roy.** memory st
20692 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20  ructures of the 
20693 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65  indices and fore
20694 69 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61  ign keys associa
20695 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65  ted with .** the
20696 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
20697 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20698 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
20699 65 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29  e(Table *pTable)
2069a 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  {.  Index *pInde
2069b 78 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 46 4b 65  x, *pNext;.  FKe
2069c 79 20 2a 70 46 4b 65 79 2c 20 2a 70 4e 65 78 74  y *pFKey, *pNext
2069d 46 4b 65 79 3b 0a 20 20 73 71 6c 69 74 65 33 20  FKey;.  sqlite3 
2069e 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 54 61 62  *db;..  if( pTab
2069f 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  le==0 ) return;.
206a0 20 20 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 64    db = pTable->d
206a1 62 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20  b;..  /* Do not 
206a2 64 65 6c 65 74 65 20 74 68 65 20 74 61 62 6c 65  delete the table
206a3 20 75 6e 74 69 6c 20 74 68 65 20 72 65 66 65 72   until the refer
206a4 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68  ence count reach
206a5 65 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 70 54  es zero. */.  pT
206a6 61 62 6c 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  able->nRef--;.  
206a7 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66  if( pTable->nRef
206a8 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  >0 ){.    return
206a9 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
206aa 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20  pTable->nRef==0 
206ab 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20  );..  /* Delete 
206ac 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f  all indices asso
206ad 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
206ae 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f   table.  */.  fo
206af 72 28 70 49 6e 64 65 78 20 3d 20 70 54 61 62 6c  r(pIndex = pTabl
206b0 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  e->pIndex; pInde
206b1 78 3b 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 29  x; pIndex=pNext)
206b2 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 49  {.    pNext = pI
206b3 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ndex->pNext;.   
206b4 20 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d   assert( pIndex-
206b5 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65  >pSchema==pTable
206b6 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20  ->pSchema );.   
206b7 20 73 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64   sqliteDeleteInd
206b8 65 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a  ex(pIndex);.  }.
206b9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
206ba 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
206bb 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c  .  /* Delete all
206bc 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73   foreign keys as
206bd 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
206be 69 73 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6b  is table.  The k
206bf 65 79 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20  eys.  ** should 
206c0 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
206c1 6e 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20  n unlinked from 
206c2 74 68 65 20 70 53 63 68 65 6d 61 2d 3e 61 46 4b  the pSchema->aFK
206c3 65 79 20 68 61 73 68 20 74 61 62 6c 65 20 0a 20  ey hash table . 
206c4 20 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d   */.  for(pFKey=
206c5 70 54 61 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70  pTable->pFKey; p
206c6 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78  FKey; pFKey=pNex
206c7 74 46 4b 65 79 29 7b 0a 20 20 20 20 70 4e 65 78  tFKey){.    pNex
206c8 74 46 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70  tFKey = pFKey->p
206c9 4e 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 61 73  NextFrom;.    as
206ca 73 65 72 74 28 20 73 71 6c 69 74 65 33 48 61 73  sert( sqlite3Has
206cb 68 46 69 6e 64 28 26 70 54 61 62 6c 65 2d 3e 70  hFind(&pTable->p
206cc 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 0a 20  Schema->aFKey,. 
206cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
206ce 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d            pFKey-
206cf 3e 7a 54 6f 2c 20 73 71 6c 69 74 65 33 53 74 72  >zTo, sqlite3Str
206d0 6c 65 6e 33 30 28 70 46 4b 65 79 2d 3e 7a 54 6f  len30(pFKey->zTo
206d1 29 2b 31 29 21 3d 70 46 4b 65 79 20 29 3b 0a 20  )+1)!=pFKey );. 
206d2 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
206d3 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 20 20 7d  (db, pFKey);.  }
206d4 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 65  .#endif..  /* De
206d5 6c 65 74 65 20 74 68 65 20 54 61 62 6c 65 20 73  lete the Table s
206d6 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e  tructure itself.
206d7 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 52 65  .  */.  sqliteRe
206d8 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  setColumnNames(p
206d9 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65  Table);.  sqlite
206da 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62  3DbFree(db, pTab
206db 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71  le->zName);.  sq
206dc 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
206dd 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29  pTable->zColAff)
206de 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
206df 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62  tDelete(db, pTab
206e0 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 23 69  le->pSelect);.#i
206e1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
206e2 54 5f 43 48 45 43 4b 0a 20 20 73 71 6c 69 74 65  T_CHECK.  sqlite
206e3 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
206e4 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b  pTable->pCheck);
206e5 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65  .#endif.  sqlite
206e6 33 56 74 61 62 43 6c 65 61 72 28 70 54 61 62 6c  3VtabClear(pTabl
206e7 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e);.  sqlite3DbF
206e8 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 29 3b  ree(db, pTable);
206e9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b  .}../*.** Unlink
206ea 20 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65   the given table
206eb 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74   from the hash t
206ec 61 62 6c 65 73 20 61 6e 64 20 74 68 65 20 64 65  ables and the de
206ed 6c 65 74 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c  lete the.** tabl
206ee 65 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68  e structure with
206ef 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73   all its indices
206f0 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79   and foreign key
206f1 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
206f2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
206f3 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
206f4 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64  Table(sqlite3 *d
206f5 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73  b, int iDb, cons
206f6 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65  t char *zTabName
206f7 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20  ){.  Table *p;. 
206f8 20 46 4b 65 79 20 2a 70 46 31 2c 20 2a 70 46 32   FKey *pF1, *pF2
206f9 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a 20 20  ;.  Db *pDb;..  
206fa 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
206fb 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
206fc 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
206fd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 54   );.  assert( zT
206fe 61 62 4e 61 6d 65 20 26 26 20 7a 54 61 62 4e 61  abName && zTabNa
206ff 6d 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62 20 3d  me[0] );.  pDb =
20700 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a   &db->aDb[iDb];.
20701 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73    p = sqlite3Has
20702 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53  hInsert(&pDb->pS
20703 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20  chema->tblHash, 
20704 7a 54 61 62 4e 61 6d 65 2c 0a 20 20 20 20 20 20  zTabName,.      
20705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20706 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33    sqlite3Strlen3
20707 30 28 7a 54 61 62 4e 61 6d 65 29 2b 31 2c 30 29  0(zTabName)+1,0)
20708 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66  ;.  if( p ){.#if
20709 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2070a 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20  _FOREIGN_KEY.   
2070b 20 66 6f 72 28 70 46 31 3d 70 2d 3e 70 46 4b 65   for(pF1=p->pFKe
2070c 79 3b 20 70 46 31 3b 20 70 46 31 3d 70 46 31 2d  y; pF1; pF1=pF1-
2070d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20  >pNextFrom){.   
2070e 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73 71 6c     int nTo = sql
2070f 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 46 31  ite3Strlen30(pF1
20710 2d 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20 20 20 20  ->zTo) + 1;.    
20711 20 20 70 46 32 20 3d 20 73 71 6c 69 74 65 33 48    pF2 = sqlite3H
20712 61 73 68 46 69 6e 64 28 26 70 44 62 2d 3e 70 53  ashFind(&pDb->pS
20713 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46  chema->aFKey, pF
20714 31 2d 3e 7a 54 6f 2c 20 6e 54 6f 29 3b 0a 20 20  1->zTo, nTo);.  
20715 20 20 20 20 69 66 28 20 70 46 32 3d 3d 70 46 31      if( pF2==pF1
20716 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
20717 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70  te3HashInsert(&p
20718 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 61 46 4b  Db->pSchema->aFK
20719 65 79 2c 20 70 46 31 2d 3e 7a 54 6f 2c 20 6e 54  ey, pF1->zTo, nT
2071a 6f 2c 20 70 46 31 2d 3e 70 4e 65 78 74 54 6f 29  o, pF1->pNextTo)
2071b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2071c 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 46         while( pF
2071d 32 20 26 26 20 70 46 32 2d 3e 70 4e 65 78 74 54  2 && pF2->pNextT
2071e 6f 21 3d 70 46 31 20 29 7b 20 70 46 32 3d 70 46  o!=pF1 ){ pF2=pF
2071f 32 2d 3e 70 4e 65 78 74 54 6f 3b 20 7d 0a 20 20  2->pNextTo; }.  
20720 20 20 20 20 20 20 69 66 28 20 70 46 32 20 29 7b        if( pF2 ){
20721 0a 20 20 20 20 20 20 20 20 20 20 70 46 32 2d 3e  .          pF2->
20722 70 4e 65 78 74 54 6f 20 3d 20 70 46 31 2d 3e 70  pNextTo = pF1->p
20723 4e 65 78 74 54 6f 3b 0a 20 20 20 20 20 20 20 20  NextTo;.        
20724 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
20725 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
20726 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 29  e3DeleteTable(p)
20727 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67  ;.  }.  db->flag
20728 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65  s |= SQLITE_Inte
20729 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a  rnChanges;.}../*
2072a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 74 6f 6b 65  .** Given a toke
2072b 6e 2c 20 72 65 74 75 72 6e 20 61 20 73 74 72 69  n, return a stri
2072c 6e 67 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73  ng that consists
2072d 20 6f 66 20 74 68 65 20 74 65 78 74 20 6f 66 20   of the text of 
2072e 74 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e 20 77 69  that.** token wi
2072f 74 68 20 61 6e 79 20 71 75 6f 74 61 74 69 6f 6e  th any quotation
20730 73 20 72 65 6d 6f 76 65 64 2e 20 20 53 70 61 63  s removed.  Spac
20731 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65  e to hold the re
20732 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a  turned string.**
20733 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
20734 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  m sqliteMalloc()
20735 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65   and must be fre
20736 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ed by the callin
20737 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  g.** function..*
20738 2a 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20  *.** Tokens are 
20739 6f 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74  often just point
2073a 65 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69  ers into the ori
2073b 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61  ginal SQL text a
2073c 6e 64 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74  nd so.** are not
2073d 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64   \000 terminated
2073e 20 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72   and are not per
2073f 73 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65  sistent.  The re
20740 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a  turned string.**
20741 20 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e 61   is \000 termina
20742 74 65 64 20 61 6e 64 20 69 73 20 70 65 72 73 69  ted and is persi
20743 73 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  stent..*/.SQLITE
20744 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
20745 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
20746 6b 65 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ken(sqlite3 *db,
20747 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a   Token *pName){.
20748 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20    char *zName;. 
20749 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20   if( pName ){.  
2074a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
2074b 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
2074c 63 68 61 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20  char*)pName->z, 
2074d 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 73  pName->n);.    s
2074e 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e  qlite3Dequote(zN
2074f 61 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ame);.  }else{. 
20750 20 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20     zName = 0;.  
20751 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65  }.  return zName
20752 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
20753 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
20754 72 20 74 61 62 6c 65 20 73 74 6f 72 65 64 20 69  r table stored i
20755 6e 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65  n database numbe
20756 72 20 69 44 62 20 66 6f 72 0a 2a 2a 20 77 72 69  r iDb for.** wri
20757 74 69 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 20  ting. The table 
20758 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20  is opened using 
20759 63 75 72 73 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c  cursor 0..*/.SQL
2075a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2075b 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74   sqlite3OpenMast
2075c 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70  erTable(Parse *p
2075d 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64  , int iDb){.  Vd
2075e 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
2075f 65 74 56 64 62 65 28 70 29 3b 0a 20 20 73 71 6c  etVdbe(p);.  sql
20760 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  ite3TableLock(p,
20761 20 69 44 62 2c 20 4d 41 53 54 45 52 5f 52 4f 4f   iDb, MASTER_ROO
20762 54 2c 20 31 2c 20 53 43 48 45 4d 41 5f 54 41 42  T, 1, SCHEMA_TAB
20763 4c 45 28 69 44 62 29 29 3b 0a 20 20 73 71 6c 69  LE(iDb));.  sqli
20764 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
20765 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e   OP_SetNumColumn
20766 73 2c 20 30 2c 20 35 29 3b 2f 2a 20 73 71 6c 69  s, 0, 5);/* sqli
20767 74 65 5f 6d 61 73 74 65 72 20 68 61 73 20 35 20  te_master has 5 
20768 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 73 71 6c  columns */.  sql
20769 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2076a 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20  , OP_OpenWrite, 
2076b 30 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  0, MASTER_ROOT, 
2076c 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  iDb);.}../*.** T
2076d 68 65 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20  he token *pName 
2076e 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d  contains the nam
2076f 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  e of a database 
20770 28 65 69 74 68 65 72 20 22 6d 61 69 6e 22 20 6f  (either "main" o
20771 72 0a 2a 2a 20 22 74 65 6d 70 22 20 6f 72 20 74  r.** "temp" or t
20772 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74  he name of an at
20773 74 61 63 68 65 64 20 64 62 29 2e 20 54 68 69 73  tached db). This
20774 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
20775 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66   the.** index of
20776 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62   the named datab
20777 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d  ase in db->aDb[]
20778 2c 20 6f 72 20 2d 31 20 69 66 20 74 68 65 20 6e  , or -1 if the n
20779 61 6d 65 64 20 64 62 20 0a 2a 2a 20 64 6f 65 73  amed db .** does
2077a 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2f 0a 53   not exist..*/.S
2077b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2077c 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28  t sqlite3FindDb(
2077d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b  sqlite3 *db, Tok
2077e 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e  en *pName){.  in
2077f 74 20 69 20 3d 20 2d 31 3b 20 20 20 20 2f 2a 20  t i = -1;    /* 
20780 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20  Database number 
20781 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20  */.  int n;     
20782 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
20783 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74   characters in t
20784 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 44 62 20  he name */.  Db 
20785 2a 70 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 41  *pDb;       /* A
20786 20 64 61 74 61 62 61 73 65 20 77 68 6f 73 65 20   database whose 
20787 6e 61 6d 65 20 73 70 61 63 65 20 69 73 20 62 65  name space is be
20788 69 6e 67 20 73 65 61 72 63 68 65 64 20 2a 2f 0a  ing searched */.
20789 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
2078a 20 2f 2a 20 4e 61 6d 65 20 77 65 20 61 72 65 20   /* Name we are 
2078b 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f  searching for */
2078c 0a 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  ..  zName = sqli
2078d 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
2078e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69  (db, pName);.  i
2078f 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  f( zName ){.    
20790 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
20791 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  n30(zName);.    
20792 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31  for(i=(db->nDb-1
20793 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b  ), pDb=&db->aDb[
20794 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70  i]; i>=0; i--, p
20795 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 28  Db--){.      if(
20796 20 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c   (!OMIT_TEMPDB |
20797 7c 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73  | i!=1 ) && n==s
20798 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
20799 44 62 2d 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20  Db->zName) && . 
2079a 20 20 20 20 20 20 20 20 20 30 3d 3d 73 71 6c 69           0==sqli
2079b 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e  te3StrICmp(pDb->
2079c 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b  zName, zName) ){
2079d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2079e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2079f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
207a0 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a  db, zName);.  }.
207a1 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f    return i;.}../
207a2 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76  * The table or v
207a3 69 65 77 20 6f 72 20 74 72 69 67 67 65 72 20 6e  iew or trigger n
207a4 61 6d 65 20 69 73 20 70 61 73 73 65 64 20 74 6f  ame is passed to
207a5 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69   this routine vi
207a6 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70 4e 61 6d  a tokens.** pNam
207a7 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49  e1 and pName2. I
207a8 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  f the table name
207a9 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69   was fully quali
207aa 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c  fied, for exampl
207ab 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20  e:.**.** CREATE 
207ac 54 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e  TABLE xxx.yyy (.
207ad 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 6e  ..);.** .** Then
207ae 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74   pName1 is set t
207af 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d  o "xxx" and pNam
207b0 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65  e2 "yyy". On the
207b1 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a 2a   other hand if.*
207b2 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  * the table name
207b3 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75   is not fully qu
207b4 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 2a  alified, i.e.:.*
207b5 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c  *.** CREATE TABL
207b6 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a  E yyy(...);.**.*
207b7 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73  * Then pName1 is
207b8 20 73 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e   set to "yyy" an
207b9 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a  d pName2 is ""..
207ba 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
207bb 6e 65 20 73 65 74 73 20 74 68 65 20 2a 70 70 55  ne sets the *ppU
207bc 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72 20 74 6f  nqual pointer to
207bd 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f   point at the to
207be 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 2a  ken (pName1 or.*
207bf 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73  * pName2) that s
207c0 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c  tores the unqual
207c1 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65  ified table name
207c2 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  .  The index of 
207c3 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
207c4 22 78 78 78 22 20 69 73 20 72 65 74 75 72 6e 65  "xxx" is returne
207c5 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
207c6 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
207c7 54 77 6f 50 61 72 74 4e 61 6d 65 28 0a 20 20 50  TwoPartName(.  P
207c8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
207c9 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
207ca 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
207cb 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
207cc 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20  oken *pName1,   
207cd 20 20 20 2f 2a 20 54 68 65 20 22 78 78 78 22 20     /* The "xxx" 
207ce 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78  in the name "xxx
207cf 2e 79 79 79 22 20 6f 72 20 22 78 78 78 22 20 2a  .yyy" or "xxx" *
207d0 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
207d1 32 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22  2,      /* The "
207d2 79 79 79 22 20 69 6e 20 74 68 65 20 6e 61 6d 65  yyy" in the name
207d3 20 22 78 78 78 2e 79 79 79 22 20 2a 2f 0a 20 20   "xxx.yyy" */.  
207d4 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75 61 6c 20  Token **pUnqual 
207d5 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
207d6 20 75 6e 71 75 61 6c 69 66 69 65 64 20 6f 62 6a   unqualified obj
207d7 65 63 74 20 6e 61 6d 65 20 68 65 72 65 20 2a 2f  ect name here */
207d8 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
207d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
207da 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 6f    /* Database ho
207db 6c 64 69 6e 67 20 74 68 65 20 6f 62 6a 65 63 74  lding the object
207dc 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
207dd 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
207de 0a 20 20 69 66 28 20 70 4e 61 6d 65 32 20 26 26  .  if( pName2 &&
207df 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a   pName2->n>0 ){.
207e0 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74      if( db->init
207e1 2e 62 75 73 79 20 29 20 7b 0a 20 20 20 20 20 20  .busy ) {.      
207e2 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
207e3 70 50 61 72 73 65 2c 20 22 63 6f 72 72 75 70 74  pParse, "corrupt
207e4 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20   database");.   
207e5 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b     pParse->nErr+
207e6 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  +;.      return 
207e7 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  -1;.    }.    *p
207e8 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b  Unqual = pName2;
207e9 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
207ea 65 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61  e3FindDb(db, pNa
207eb 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44  me1);.    if( iD
207ec 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  b<0 ){.      sql
207ed 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
207ee 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61  rse, "unknown da
207ef 74 61 62 61 73 65 20 25 54 22 2c 20 70 4e 61 6d  tabase %T", pNam
207f0 65 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  e1);.      pPars
207f1 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20  e->nErr++;.     
207f2 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20   return -1;.    
207f3 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  }.  }else{.    a
207f4 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
207f5 69 44 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e  iDb==0 || db->in
207f6 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 69  it.busy );.    i
207f7 44 62 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 69 44  Db = db->init.iD
207f8 62 3b 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20  b;.    *pUnqual 
207f9 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 0a 20 20  = pName1;.  }.  
207fa 72 65 74 75 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f  return iDb;.}../
207fb 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
207fc 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 65  e is used to che
207fd 63 6b 20 69 66 20 74 68 65 20 55 54 46 2d 38 20  ck if the UTF-8 
207fe 73 74 72 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20  string zName is 
207ff 61 20 6c 65 67 61 6c 0a 2a 2a 20 75 6e 71 75 61  a legal.** unqua
20800 6c 69 66 69 65 64 20 6e 61 6d 65 20 66 6f 72 20  lified name for 
20801 61 20 6e 65 77 20 73 63 68 65 6d 61 20 6f 62 6a  a new schema obj
20802 65 63 74 20 28 74 61 62 6c 65 2c 20 69 6e 64 65  ect (table, inde
20803 78 2c 20 76 69 65 77 20 6f 72 0a 2a 2a 20 74 72  x, view or.** tr
20804 69 67 67 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65  igger). All name
20805 73 20 61 72 65 20 6c 65 67 61 6c 20 65 78 63 65  s are legal exce
20806 70 74 20 74 68 6f 73 65 20 74 68 61 74 20 62 65  pt those that be
20807 67 69 6e 20 77 69 74 68 20 74 68 65 20 73 74 72  gin with the str
20808 69 6e 67 0a 2a 2a 20 22 73 71 6c 69 74 65 5f 22  ing.** "sqlite_"
20809 20 28 69 6e 20 75 70 70 65 72 2c 20 6c 6f 77 65   (in upper, lowe
2080a 72 20 6f 72 20 6d 69 78 65 64 20 63 61 73 65 29  r or mixed case)
2080b 2e 20 54 68 69 73 20 70 6f 72 74 69 6f 6e 20 6f  . This portion o
2080c 66 20 74 68 65 20 6e 61 6d 65 73 70 61 63 65 0a  f the namespace.
2080d 2a 2a 20 69 73 20 72 65 73 65 72 76 65 64 20 66  ** is reserved f
2080e 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 2e  or internal use.
2080f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20810 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68  TE int sqlite3Ch
20811 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61  eckObjectName(Pa
20812 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
20813 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b  st char *zName){
20814 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
20815 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26  db->init.busy &&
20816 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
20817 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 26 26  =0 .          &&
20818 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c   (pParse->db->fl
20819 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57 72 69  ags & SQLITE_Wri
2081a 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20  teSchema)==0.   
2081b 20 20 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c         && 0==sql
2081c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61  ite3StrNICmp(zNa
2081d 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37  me, "sqlite_", 7
2081e 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
2081f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
20820 20 22 6f 62 6a 65 63 74 20 6e 61 6d 65 20 72 65   "object name re
20821 73 65 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72  served for inter
20822 6e 61 6c 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e  nal use: %s", zN
20823 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
20824 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
20825 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
20826 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
20827 42 65 67 69 6e 20 63 6f 6e 73 74 72 75 63 74 69  Begin constructi
20828 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65 20 72  ng a new table r
20829 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e  epresentation in
2082a 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69   memory.  This i
2082b 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f  s.** the first o
2082c 66 20 73 65 76 65 72 61 6c 20 61 63 74 69 6f 6e  f several action
2082d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 67   routines that g
2082e 65 74 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73  et called in res
2082f 70 6f 6e 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52  ponse.** to a CR
20830 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
20831 6d 65 6e 74 2e 20 20 49 6e 20 70 61 72 74 69 63  ment.  In partic
20832 75 6c 61 72 2c 20 74 68 69 73 20 72 6f 75 74 69  ular, this routi
20833 6e 65 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20  ne is called.** 
20834 61 66 74 65 72 20 73 65 65 69 6e 67 20 74 6f 6b  after seeing tok
20835 65 6e 73 20 22 43 52 45 41 54 45 22 20 61 6e 64  ens "CREATE" and
20836 20 22 54 41 42 4c 45 22 20 61 6e 64 20 74 68 65   "TABLE" and the
20837 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65   table name. The
20838 20 69 73 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20   isTemp.** flag 
20839 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74  is true if the t
2083a 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73  able should be s
2083b 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 75 78  tored in the aux
2083c 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 0a  iliary database.
2083d 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20  ** file instead 
2083e 6f 66 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  of in the main d
2083f 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
20840 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20  his is normally 
20841 74 68 65 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e  the case.** when
20842 20 74 68 65 20 22 54 45 4d 50 22 20 6f 72 20 22   the "TEMP" or "
20843 54 45 4d 50 4f 52 41 52 59 22 20 6b 65 79 77 6f  TEMPORARY" keywo
20844 72 64 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74  rd occurs in bet
20845 77 65 65 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61  ween.** CREATE a
20846 6e 64 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20  nd TABLE..**.** 
20847 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65  The new table re
20848 63 6f 72 64 20 69 73 20 69 6e 69 74 69 61 6c 69  cord is initiali
20849 7a 65 64 20 61 6e 64 20 70 75 74 20 69 6e 20 70  zed and put in p
2084a 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2084b 2e 0a 2a 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20  ..** As more of 
2084c 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
2084d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61   statement is pa
2084e 72 73 65 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c  rsed, additional
2084f 20 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69   action.** routi
20850 6e 65 73 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  nes will be call
20851 65 64 20 74 6f 20 61 64 64 20 6d 6f 72 65 20 69  ed to add more i
20852 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68  nformation to th
20853 69 73 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74  is record..** At
20854 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
20855 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
20856 74 65 6d 65 6e 74 2c 20 74 68 65 20 73 71 6c 69  tement, the sqli
20857 74 65 33 45 6e 64 54 61 62 6c 65 28 29 20 72 6f  te3EndTable() ro
20858 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c  utine.** is call
20859 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74  ed to complete t
2085a 68 65 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  he construction 
2085b 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
2085c 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49   record..*/.SQLI
2085d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2085e 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
2085f 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
20860 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20  se,   /* Parser 
20861 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b  context */.  Tok
20862 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a  en *pName1,   /*
20863 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 74   First part of t
20864 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
20865 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a  able or view */.
20866 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c    Token *pName2,
20867 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72     /* Second par
20868 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66  t of the name of
20869 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69   the table or vi
2086a 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65  ew */.  int isTe
2086b 6d 70 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65  mp,      /* True
2086c 20 69 66 20 74 68 69 73 20 69 73 20 61 20 54 45   if this is a TE
2086d 4d 50 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  MP table */.  in
2086e 74 20 69 73 56 69 65 77 2c 20 20 20 20 20 20 2f  t isView,      /
2086f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
20870 73 20 61 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e  s a VIEW */.  in
20871 74 20 69 73 56 69 72 74 75 61 6c 2c 20 20 20 2f  t isVirtual,   /
20872 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
20873 73 20 61 20 56 49 52 54 55 41 4c 20 74 61 62 6c  s a VIRTUAL tabl
20874 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72  e */.  int noErr
20875 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
20876 74 68 69 6e 67 20 69 66 20 74 61 62 6c 65 20 61  thing if table a
20877 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f  lready exists */
20878 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  .){.  Table *pTa
20879 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  ble;.  char *zNa
2087a 6d 65 20 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e  me = 0; /* The n
2087b 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74  ame of the new t
2087c 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  able */.  sqlite
2087d 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2087e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  db;.  Vdbe *v;. 
2087f 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
20880 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75    /* Database nu
20881 6d 62 65 72 20 74 6f 20 63 72 65 61 74 65 20 74  mber to create t
20882 68 65 20 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20  he table in */. 
20883 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20   Token *pName;  
20884 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64    /* Unqualified
20885 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
20886 6c 65 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a  le to create */.
20887 0a 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20  .  /* The table 
20888 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 74 6f 20  or view name to 
20889 63 72 65 61 74 65 20 69 73 20 70 61 73 73 65 64  create is passed
2088a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
2088b 20 76 69 61 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a   via tokens.  **
2088c 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d   pName1 and pNam
2088d 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65  e2. If the table
2088e 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20   name was fully 
2088f 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65  qualified, for e
20890 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a  xample:.  **.  *
20891 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78  * CREATE TABLE x
20892 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 20 20  xx.yyy (...);.  
20893 2a 2a 20 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e  ** .  ** Then pN
20894 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22  ame1 is set to "
20895 78 78 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20  xxx" and pName2 
20896 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74  "yyy". On the ot
20897 68 65 72 20 68 61 6e 64 20 69 66 0a 20 20 2a 2a  her hand if.  **
20898 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
20899 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61  is not fully qua
2089a 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 20 20  lified, i.e.:.  
2089b 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54  **.  ** CREATE T
2089c 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 20  ABLE yyy(...);. 
2089d 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e   **.  ** Then pN
2089e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22  ame1 is set to "
2089f 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20  yyy" and pName2 
208a0 69 73 20 22 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  is ""..  **.  **
208a1 20 54 68 65 20 63 61 6c 6c 20 62 65 6c 6f 77 20   The call below 
208a2 73 65 74 73 20 74 68 65 20 70 4e 61 6d 65 20 70  sets the pName p
208a3 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20  ointer to point 
208a4 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e  at the token (pN
208a5 61 6d 65 31 20 6f 72 0a 20 20 2a 2a 20 70 4e 61  ame1 or.  ** pNa
208a6 6d 65 32 29 20 74 68 61 74 20 73 74 6f 72 65 73  me2) that stores
208a7 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64   the unqualified
208a8 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65   table name. The
208a9 20 76 61 72 69 61 62 6c 65 20 69 44 62 20 69 73   variable iDb is
208aa 0a 20 20 2a 2a 20 73 65 74 20 74 6f 20 74 68 65  .  ** set to the
208ab 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   index of the da
208ac 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20  tabase that the 
208ad 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 69 73  table or view is
208ae 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 72 65 61   to be.  ** crea
208af 74 65 64 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69  ted in..  */.  i
208b0 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50  Db = sqlite3TwoP
208b1 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  artName(pParse, 
208b2 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
208b3 26 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69  &pName);.  if( i
208b4 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  Db<0 ) return;. 
208b5 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
208b6 42 20 26 26 20 69 73 54 65 6d 70 20 26 26 20 69  B && isTemp && i
208b7 44 62 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Db>1 ){.    /* I
208b8 66 20 63 72 65 61 74 69 6e 67 20 61 20 74 65 6d  f creating a tem
208b9 70 20 74 61 62 6c 65 2c 20 74 68 65 20 6e 61 6d  p table, the nam
208ba 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61  e may not be qua
208bb 6c 69 66 69 65 64 20 2a 2f 0a 20 20 20 20 73 71  lified */.    sq
208bc 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
208bd 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79  arse, "temporary
208be 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74   table name must
208bf 20 62 65 20 75 6e 71 75 61 6c 69 66 69 65 64 22   be unqualified"
208c0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
208c1 20 7d 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54   }.  if( !OMIT_T
208c2 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20  EMPDB && isTemp 
208c3 29 20 69 44 62 20 3d 20 31 3b 0a 0a 20 20 70 50  ) iDb = 1;..  pP
208c4 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
208c5 20 3d 20 2a 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61   = *pName;.  zNa
208c6 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  me = sqlite3Name
208c7 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e  FromToken(db, pN
208c8 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d  ame);.  if( zNam
208c9 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  e==0 ) return;. 
208ca 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
208cb 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65  sqlite3CheckObje
208cc 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a  ctName(pParse, z
208cd 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  Name) ){.    got
208ce 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72  o begin_table_er
208cf 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64  ror;.  }.  if( d
208d0 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29  b->init.iDb==1 )
208d1 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66   isTemp = 1;.#if
208d2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
208d3 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
208d4 20 61 73 73 65 72 74 28 20 28 69 73 54 65 6d 70   assert( (isTemp
208d5 20 26 20 31 29 3d 3d 69 73 54 65 6d 70 20 29 3b   & 1)==isTemp );
208d6 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64  .  {.    int cod
208d7 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62  e;.    char *zDb
208d8 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
208d9 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73  zName;.    if( s
208da 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
208db 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49  pParse, SQLITE_I
208dc 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41  NSERT, SCHEMA_TA
208dd 42 4c 45 28 69 73 54 65 6d 70 29 2c 20 30 2c 20  BLE(isTemp), 0, 
208de 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  zDb) ){.      go
208df 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65  to begin_table_e
208e0 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rror;.    }.    
208e1 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20  if( isView ){.  
208e2 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45      if( !OMIT_TE
208e3 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29  MPDB && isTemp )
208e4 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
208e5 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
208e6 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20  EMP_VIEW;.      
208e7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
208e8 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45  ode = SQLITE_CRE
208e9 41 54 45 5f 56 49 45 57 3b 0a 20 20 20 20 20 20  ATE_VIEW;.      
208ea 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
208eb 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
208ec 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b  PDB && isTemp ){
208ed 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
208ee 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
208ef 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20  MP_TABLE;.      
208f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
208f1 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45  ode = SQLITE_CRE
208f2 41 54 45 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20  ATE_TABLE;.     
208f3 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
208f4 20 21 69 73 56 69 72 74 75 61 6c 20 26 26 20 73   !isVirtual && s
208f5 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
208f6 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e  pParse, code, zN
208f7 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  ame, 0, zDb) ){.
208f8 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e        goto begin
208f9 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20  _table_error;.  
208fa 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
208fb 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
208fc 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d  he new table nam
208fd 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69  e does not colli
208fe 64 65 20 77 69 74 68 20 61 6e 20 65 78 69 73 74  de with an exist
208ff 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f  ing.  ** index o
20900 72 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  r table name in 
20901 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
20902 65 2e 20 20 49 73 73 75 65 20 61 6e 20 65 72 72  e.  Issue an err
20903 6f 72 20 6d 65 73 73 61 67 65 20 69 66 0a 20 20  or message if.  
20904 2a 2a 20 69 74 20 64 6f 65 73 2e 20 54 68 65 20  ** it does. The 
20905 65 78 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20  exception is if 
20906 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65  the statement be
20907 69 6e 67 20 70 61 72 73 65 64 20 77 61 73 20 70  ing parsed was p
20908 61 73 73 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e  assed.  ** to an
20909 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
2090a 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e  _vtab() call. In
2090b 20 74 68 61 74 20 63 61 73 65 20 6f 6e 6c 79 20   that case only 
2090c 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
2090d 0a 20 20 2a 2a 20 61 6e 64 20 74 79 70 65 73 20  .  ** and types 
2090e 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 73 6f  will be used, so
2090f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
20910 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6e 61  d to test for na
20911 6d 65 73 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c  mespace.  ** col
20912 6c 69 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  lisions..  */.  
20913 69 66 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f  if( !IN_DECLARE_
20914 56 54 41 42 20 29 7b 0a 20 20 20 20 69 66 28 20  VTAB ){.    if( 
20915 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
20916 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
20917 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  rse) ){.      go
20918 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65  to begin_table_e
20919 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rror;.    }.    
2091a 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  pTable = sqlite3
2091b 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e  FindTable(db, zN
2091c 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  ame, db->aDb[iDb
2091d 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  ].zName);.    if
2091e 28 20 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  ( pTable ){.    
2091f 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a    if( !noErr ){.
20920 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
20921 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
20922 22 74 61 62 6c 65 20 25 54 20 61 6c 72 65 61 64  "table %T alread
20923 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65  y exists", pName
20924 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
20925 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
20926 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  e_error;.    }. 
20927 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
20928 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d  ndIndex(db, zNam
20929 65 2c 20 30 29 21 3d 30 20 26 26 20 28 69 44 62  e, 0)!=0 && (iDb
2092a 3d 3d 30 20 7c 7c 20 21 64 62 2d 3e 69 6e 69 74  ==0 || !db->init
2092b 2e 62 75 73 79 29 20 29 7b 0a 20 20 20 20 20 20  .busy) ){.      
2092c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2092d 70 50 61 72 73 65 2c 20 22 74 68 65 72 65 20 69  pParse, "there i
2092e 73 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64  s already an ind
2092f 65 78 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e  ex named %s", zN
20930 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  ame);.      goto
20931 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
20932 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  or;.    }.  }.. 
20933 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65   pTable = sqlite
20934 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
20935 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29  , sizeof(Table))
20936 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d  ;.  if( pTable==
20937 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  0 ){.    db->mal
20938 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
20939 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
2093a 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2093b 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
2093c 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e  ;.    goto begin
2093d 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20  _table_error;.  
2093e 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d  }.  pTable->zNam
2093f 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61  e = zName;.  pTa
20940 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b  ble->iPKey = -1;
20941 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65  .  pTable->pSche
20942 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  ma = db->aDb[iDb
20943 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61  ].pSchema;.  pTa
20944 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  ble->nRef = 1;. 
20945 20 70 54 61 62 6c 65 2d 3e 64 62 20 3d 20 64 62   pTable->db = db
20946 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
20947 70 4e 65 77 54 61 62 6c 65 20 29 20 73 71 6c 69  pNewTable ) sqli
20948 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
20949 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2094a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65  );.  pParse->pNe
2094b 77 54 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b  wTable = pTable;
2094c 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
2094d 73 20 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69  s the magic sqli
2094e 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
2094f 65 20 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e  e used by autoin
20950 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68  crement,.  ** th
20951 65 6e 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e  en record a poin
20952 74 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c  ter to this tabl
20953 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  e in the main da
20954 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
20955 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e  .  ** so that IN
20956 53 45 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68  SERT can find th
20957 65 20 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a  e table easily..
20958 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
20959 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
2095a 52 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50  REMENT.  if( !pP
2095b 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20  arse->nested && 
2095c 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73  strcmp(zName, "s
2095d 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29  qlite_sequence")
2095e 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 61 62 6c  ==0 ){.    pTabl
2095f 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71  e->pSchema->pSeq
20960 54 61 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20  Tab = pTable;.  
20961 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42  }.#endif..  /* B
20962 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20  egin generating 
20963 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  the code that wi
20964 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 20 74 61  ll insert the ta
20965 62 6c 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a  ble record into.
20966 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f    ** the SQLITE_
20967 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e  MASTER table.  N
20968 6f 74 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61  ote in particula
20969 72 20 74 68 61 74 20 77 65 20 6d 75 73 74 20 67  r that we must g
2096a 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64  o ahead.  ** and
2096b 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65   allocate the re
2096c 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20  cord number for 
2096d 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20  the table entry 
2096e 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79  now.  Before any
2096f 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45  .  ** PRIMARY KE
20970 59 20 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77  Y or UNIQUE keyw
20971 6f 72 64 73 20 61 72 65 20 70 61 72 73 65 64 2e  ords are parsed.
20972 20 20 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73    Those keywords
20973 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a   will cause.  **
20974 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63   indices to be c
20975 72 65 61 74 65 64 20 61 6e 64 20 74 68 65 20 74  reated and the t
20976 61 62 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74  able record must
20977 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65   come before the
20978 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20   .  ** indices. 
20979 20 48 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f   Hence, the reco
2097a 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  rd number for th
2097b 65 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20  e table must be 
2097c 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e  allocated.  ** n
2097d 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ow..  */.  if( !
2097e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26  db->init.busy &&
2097f 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   (v = sqlite3Get
20980 56 64 62 65 28 70 50 61 72 73 65 29 29 21 3d 30  Vdbe(pParse))!=0
20981 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a   ){.    int j1;.
20982 20 20 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d      int fileForm
20983 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 31  at;.    int reg1
20984 2c 20 72 65 67 32 2c 20 72 65 67 33 3b 0a 20 20  , reg2, reg3;.  
20985 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
20986 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
20987 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23  rse, 0, iDb);..#
20988 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20989 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2098a 20 20 20 20 69 66 28 20 69 73 56 69 72 74 75 61      if( isVirtua
2098b 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  l ){.      sqlit
2098c 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
2098d 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20  OP_VBegin);.    
2098e 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
2098f 20 49 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72   If the file for
20990 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67  mat and encoding
20991 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
20992 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 73   have not been s
20993 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20  et, .    ** set 
20994 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f  them now..    */
20995 0a 20 20 20 20 72 65 67 31 20 3d 20 70 50 61 72  .    reg1 = pPar
20996 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b  se->regRowid = +
20997 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
20998 20 20 20 72 65 67 32 20 3d 20 70 50 61 72 73 65     reg2 = pParse
20999 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70 50  ->regRoot = ++pP
2099a 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
2099b 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 73 65 2d  reg3 = ++pParse-
2099c 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >nMem;.    sqlit
2099d 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2099e 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69  OP_ReadCookie, i
2099f 44 62 2c 20 72 65 67 33 2c 20 31 29 3b 20 20 20  Db, reg3, 1);   
209a0 2f 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 2a  /* file_format *
209a1 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
209a2 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
209a3 62 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c  b);.    j1 = sql
209a4 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
209a5 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b 0a  , OP_If, reg3);.
209a6 20 20 20 20 66 69 6c 65 46 6f 72 6d 61 74 20 3d      fileFormat =
209a7 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
209a8 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46  LITE_LegacyFileF
209a9 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20  mt)!=0 ?.       
209aa 20 20 20 20 20 20 20 20 20 20 20 31 20 3a 20 53             1 : S
209ab 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46  QLITE_MAX_FILE_F
209ac 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71 6c 69 74  ORMAT;.    sqlit
209ad 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
209ae 4f 50 5f 49 6e 74 65 67 65 72 2c 20 66 69 6c 65  OP_Integer, file
209af 46 6f 72 6d 61 74 2c 20 72 65 67 33 29 3b 0a 20  Format, reg3);. 
209b0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
209b1 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f  dOp3(v, OP_SetCo
209b2 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c 20 72 65  okie, iDb, 1, re
209b3 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g3);.    sqlite3
209b4 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
209b5 5f 49 6e 74 65 67 65 72 2c 20 45 4e 43 28 64 62  _Integer, ENC(db
209b6 29 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71  ), reg3);.    sq
209b7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
209b8 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c  v, OP_SetCookie,
209b9 20 69 44 62 2c 20 34 2c 20 72 65 67 33 29 3b 0a   iDb, 4, reg3);.
209ba 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
209bb 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
209bc 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 6a 75 73  .    /* This jus
209bd 74 20 63 72 65 61 74 65 73 20 61 20 70 6c 61 63  t creates a plac
209be 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f 72 64 20  e-holder record 
209bf 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  in the sqlite_ma
209c0 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 20 20  ster table..    
209c1 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 63 72  ** The record cr
209c2 65 61 74 65 64 20 64 6f 65 73 20 6e 6f 74 20 63  eated does not c
209c3 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69 6e 67 20  ontain anything 
209c4 79 65 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65  yet.  It will be
209c5 20 72 65 70 6c 61 63 65 64 0a 20 20 20 20 2a 2a   replaced.    **
209c6 20 62 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74   by the real ent
209c7 72 79 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72  ry in code gener
209c8 61 74 65 64 20 61 74 20 73 71 6c 69 74 65 33 45  ated at sqlite3E
209c9 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a  ndTable()..    *
209ca 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77  *.    ** The row
209cb 69 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65  id for the new e
209cc 6e 74 72 79 20 69 73 20 6c 65 66 74 20 6f 6e 20  ntry is left on 
209cd 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
209ce 74 61 63 6b 2e 0a 20 20 20 20 2a 2a 20 54 68 65  tack..    ** The
209cf 20 72 6f 77 69 64 20 76 61 6c 75 65 20 69 73 20   rowid value is 
209d0 6e 65 65 64 65 64 20 62 79 20 74 68 65 20 63 6f  needed by the co
209d1 64 65 20 74 68 61 74 20 73 71 6c 69 74 65 33 45  de that sqlite3E
209d2 6e 64 54 61 62 6c 65 20 77 69 6c 6c 0a 20 20 20  ndTable will.   
209d3 20 2a 2a 20 67 65 6e 65 72 61 74 65 2e 0a 20 20   ** generate..  
209d4 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65    */.#if !define
209d5 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
209d6 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  EW) || !defined(
209d7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
209d8 55 41 4c 54 41 42 4c 45 29 0a 20 20 20 20 69 66  UALTABLE).    if
209d9 28 20 69 73 56 69 65 77 20 7c 7c 20 69 73 56 69  ( isView || isVi
209da 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73  rtual ){.      s
209db 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
209dc 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
209dd 30 2c 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 65  0, reg2);.    }e
209de 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b  lse.#endif.    {
209df 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
209e0 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
209e1 72 65 61 74 65 54 61 62 6c 65 2c 20 69 44 62 2c  reateTable, iDb,
209e2 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 0a 20 20   reg2);.    }.  
209e3 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73    sqlite3OpenMas
209e4 74 65 72 54 61 62 6c 65 28 70 50 61 72 73 65 2c  terTable(pParse,
209e5 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
209e6 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
209e7 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c 20  OP_NewRowid, 0, 
209e8 72 65 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  reg1);.    sqlit
209e9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
209ea 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 33  OP_Null, 0, reg3
209eb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
209ec 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
209ed 6e 73 65 72 74 2c 20 30 2c 20 72 65 67 33 2c 20  nsert, 0, reg3, 
209ee 72 65 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  reg1);.    sqlit
209ef 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
209f0 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  , OPFLAG_APPEND)
209f1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
209f2 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c  eAddOp0(v, OP_Cl
209f3 6f 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ose);.  }..  /* 
209f4 4e 6f 72 6d 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f  Normal (non-erro
209f5 72 29 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20  r) return. */.  
209f6 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66  return;..  /* If
209f7 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
209f8 2c 20 77 65 20 6a 75 6d 70 20 68 65 72 65 20 2a  , we jump here *
209f9 2f 0a 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72  /.begin_table_er
209fa 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 44 62  ror:.  sqlite3Db
209fb 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b  Free(db, zName);
209fc 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
209fd 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69  .** This macro i
209fe 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72  s used to compar
209ff 65 20 74 77 6f 20 73 74 72 69 6e 67 73 20 69 6e  e two strings in
20a00 20 61 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74   a case-insensit
20a01 69 76 65 20 6d 61 6e 6e 65 72 2e 0a 2a 2a 20 49  ive manner..** I
20a02 74 20 69 73 20 73 6c 69 67 68 74 6c 79 20 66 61  t is slightly fa
20a03 73 74 65 72 20 74 68 61 6e 20 63 61 6c 6c 69 6e  ster than callin
20a04 67 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  g sqlite3StrICmp
20a05 28 29 20 64 69 72 65 63 74 6c 79 2c 20 62 75 74  () directly, but
20a06 0a 2a 2a 20 70 72 6f 64 75 63 65 73 20 6c 61 72  .** produces lar
20a07 67 65 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ger code..**.** 
20a08 57 41 52 4e 49 4e 47 3a 20 54 68 69 73 20 6d 61  WARNING: This ma
20a09 63 72 6f 20 69 73 20 6e 6f 74 20 63 6f 6d 70 61  cro is not compa
20a0a 74 69 62 6c 65 20 77 69 74 68 20 74 68 65 20 73  tible with the s
20a0b 74 72 63 6d 70 28 29 20 66 61 6d 69 6c 79 2e 20  trcmp() family. 
20a0c 49 74 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 72  It.** returns tr
20a0d 75 65 20 69 66 20 74 68 65 20 74 77 6f 20 73 74  ue if the two st
20a0e 72 69 6e 67 73 20 61 72 65 20 65 71 75 61 6c 2c  rings are equal,
20a0f 20 6f 74 68 65 72 77 69 73 65 20 66 61 6c 73 65   otherwise false
20a10 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 54 52  ..*/.#define STR
20a11 49 43 4d 50 28 78 2c 20 79 29 20 28 5c 0a 73 71  ICMP(x, y) (\.sq
20a12 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
20a13 72 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61  r[*(unsigned cha
20a14 72 20 2a 29 28 78 29 5d 3d 3d 20 20 20 5c 0a 73  r *)(x)]==   \.s
20a15 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
20a16 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68  er[*(unsigned ch
20a17 61 72 20 2a 29 28 79 29 5d 20 20 20 20 20 5c 0a  ar *)(y)]     \.
20a18 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
20a19 70 28 28 78 29 2b 31 2c 28 79 29 2b 31 29 3d 3d  p((x)+1,(y)+1)==
20a1a 30 20 29 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  0 )../*.** Add a
20a1b 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 74 6f 20 74   new column to t
20a1c 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74  he table current
20a1d 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75  ly being constru
20a1e 63 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  cted..**.** The 
20a1f 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
20a20 73 20 72 6f 75 74 69 6e 65 20 6f 6e 63 65 20 66  s routine once f
20a21 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 64  or each column d
20a22 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e  eclaration.** in
20a23 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   a CREATE TABLE 
20a24 73 74 61 74 65 6d 65 6e 74 2e 20 20 73 71 6c 69  statement.  sqli
20a25 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 29 20  te3StartTable() 
20a26 67 65 74 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 66  gets called.** f
20a27 69 72 73 74 20 74 6f 20 67 65 74 20 74 68 69 6e  irst to get thin
20a28 67 73 20 67 6f 69 6e 67 2e 20 20 54 68 65 6e 20  gs going.  Then 
20a29 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
20a2a 63 61 6c 6c 65 64 20 66 6f 72 20 65 61 63 68 0a  called for each.
20a2b 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51  ** column..*/.SQ
20a2c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20a2d 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75  d sqlite3AddColu
20a2e 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  mn(Parse *pParse
20a2f 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b  , Token *pName){
20a30 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69  .  Table *p;.  i
20a31 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 3b  nt i;.  char *z;
20a32 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b  .  Column *pCol;
20a33 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
20a34 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
20a35 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e  f( (p = pParse->
20a36 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20  pNewTable)==0 ) 
20a37 72 65 74 75 72 6e 3b 0a 23 69 66 20 53 51 4c 49  return;.#if SQLI
20a38 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20  TE_MAX_COLUMN.  
20a39 69 66 28 20 70 2d 3e 6e 43 6f 6c 2b 31 3e 64 62  if( p->nCol+1>db
20a3a 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
20a3b 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b  LIMIT_COLUMN] ){
20a3c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
20a3d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f  rMsg(pParse, "to
20a3e 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f  o many columns o
20a3f 6e 20 25 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29  n %s", p->zName)
20a40 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
20a41 7d 0a 23 65 6e 64 69 66 0a 20 20 7a 20 3d 20 73  }.#endif.  z = s
20a42 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
20a43 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ken(db, pName);.
20a44 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
20a45 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  urn;.  for(i=0; 
20a46 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  i<p->nCol; i++){
20a47 0a 20 20 20 20 69 66 28 20 53 54 52 49 43 4d 50  .    if( STRICMP
20a48 28 7a 2c 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a  (z, p->aCol[i].z
20a49 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73  Name) ){.      s
20a4a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
20a4b 50 61 72 73 65 2c 20 22 64 75 70 6c 69 63 61 74  Parse, "duplicat
20a4c 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25  e column name: %
20a4d 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 73 71  s", z);.      sq
20a4e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
20a4f 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  z);.      return
20a50 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
20a51 28 20 28 70 2d 3e 6e 43 6f 6c 20 26 20 30 78 37  ( (p->nCol & 0x7
20a52 29 3d 3d 30 20 29 7b 0a 20 20 20 20 43 6f 6c 75  )==0 ){.    Colu
20a53 6d 6e 20 2a 61 4e 65 77 3b 0a 20 20 20 20 61 4e  mn *aNew;.    aN
20a54 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  ew = sqlite3DbRe
20a55 61 6c 6c 6f 63 28 64 62 2c 70 2d 3e 61 43 6f 6c  alloc(db,p->aCol
20a56 2c 28 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a  ,(p->nCol+8)*siz
20a57 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29  eof(p->aCol[0]))
20a58 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d  ;.    if( aNew==
20a59 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
20a5a 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b  e3DbFree(db, z);
20a5b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
20a5c 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 43 6f 6c     }.    p->aCol
20a5d 20 3d 20 61 4e 65 77 3b 0a 20 20 7d 0a 20 20 70   = aNew;.  }.  p
20a5e 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70  Col = &p->aCol[p
20a5f 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 65  ->nCol];.  memse
20a60 74 28 70 43 6f 6c 2c 20 30 2c 20 73 69 7a 65 6f  t(pCol, 0, sizeo
20a61 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a  f(p->aCol[0]));.
20a62 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20    pCol->zName = 
20a63 7a 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 74 68 65  z;. .  /* If the
20a64 72 65 20 69 73 20 6e 6f 20 74 79 70 65 20 73 70  re is no type sp
20a65 65 63 69 66 69 65 64 2c 20 63 6f 6c 75 6d 6e 73  ecified, columns
20a66 20 68 61 76 65 20 74 68 65 20 64 65 66 61 75 6c   have the defaul
20a67 74 20 61 66 66 69 6e 69 74 79 0a 20 20 2a 2a 20  t affinity.  ** 
20a68 27 4e 4f 4e 45 27 2e 20 49 66 20 74 68 65 72 65  'NONE'. If there
20a69 20 69 73 20 61 20 74 79 70 65 20 73 70 65 63 69   is a type speci
20a6a 66 69 65 64 2c 20 74 68 65 6e 20 73 71 6c 69 74  fied, then sqlit
20a6b 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28  e3AddColumnType(
20a6c 29 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 63  ) will.  ** be c
20a6d 61 6c 6c 65 64 20 6e 65 78 74 20 74 6f 20 73 65  alled next to se
20a6e 74 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  t pCol->affinity
20a6f 20 63 6f 72 72 65 63 74 6c 79 2e 0a 20 20 2a 2f   correctly..  */
20a70 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  .  pCol->affinit
20a71 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  y = SQLITE_AFF_N
20a72 4f 4e 45 3b 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b  ONE;.  p->nCol++
20a73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
20a74 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
20a75 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
20a76 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64  while in the mid
20a77 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e  dle of.** parsin
20a78 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  g a CREATE TABLE
20a79 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 22   statement.  A "
20a7a 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72  NOT NULL" constr
20a7b 61 69 6e 74 20 68 61 73 0a 2a 2a 20 62 65 65 6e  aint has.** been
20a7c 20 73 65 65 6e 20 6f 6e 20 61 20 63 6f 6c 75 6d   seen on a colum
20a7d 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  n.  This routine
20a7e 20 73 65 74 73 20 74 68 65 20 6e 6f 74 4e 75 6c   sets the notNul
20a7f 6c 20 66 6c 61 67 20 6f 6e 0a 2a 2a 20 74 68 65  l flag on.** the
20a80 20 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c   column currentl
20a81 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  y under construc
20a82 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
20a83 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
20a84 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50  ite3AddNotNull(P
20a85 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
20a86 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 54 61  t onError){.  Ta
20a87 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b  ble *p;.  int i;
20a88 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72  .  if( (p = pPar
20a89 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d  se->pNewTable)==
20a8a 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20  0 ) return;.  i 
20a8b 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69  = p->nCol-1;.  i
20a8c 66 28 20 69 3e 3d 30 20 29 20 70 2d 3e 61 43 6f  f( i>=0 ) p->aCo
20a8d 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d 20 28  l[i].notNull = (
20a8e 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 7d 0a 0a 2f  u8)onError;.}../
20a8f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 65 20 63 6f  *.** Scan the co
20a90 6c 75 6d 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a  lumn type name z
20a91 54 79 70 65 20 28 6c 65 6e 67 74 68 20 6e 54 79  Type (length nTy
20a92 70 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20 74  pe) and return t
20a93 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  he.** associated
20a94 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 2e 0a   affinity type..
20a95 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
20a96 6e 65 20 64 6f 65 73 20 61 20 63 61 73 65 2d 69  ne does a case-i
20a97 6e 64 65 70 65 6e 64 65 6e 74 20 73 65 61 72 63  ndependent searc
20a98 68 20 6f 66 20 7a 54 79 70 65 20 66 6f 72 20 74  h of zType for t
20a99 68 65 20 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67  he .** substring
20a9a 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s in the followi
20a9b 6e 67 20 74 61 62 6c 65 2e 20 49 66 20 6f 6e 65  ng table. If one
20a9c 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 6e   of the substrin
20a9d 67 73 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20  gs is.** found, 
20a9e 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
20a9f 67 20 61 66 66 69 6e 69 74 79 20 69 73 20 72 65  g affinity is re
20aa0 74 75 72 6e 65 64 2e 20 49 66 20 7a 54 79 70 65  turned. If zType
20aa1 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 6d 6f 72   contains.** mor
20aa2 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68  e than one of th
20aa3 65 20 73 75 62 73 74 72 69 6e 67 73 2c 20 65 6e  e substrings, en
20aa4 74 72 69 65 73 20 74 6f 77 61 72 64 20 74 68 65  tries toward the
20aa5 20 74 6f 70 20 6f 66 20 0a 2a 2a 20 74 68 65 20   top of .** the 
20aa6 74 61 62 6c 65 20 74 61 6b 65 20 70 72 69 6f 72  table take prior
20aa7 69 74 79 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ity. For example
20aa8 2c 20 69 66 20 7a 54 79 70 65 20 69 73 20 27 42  , if zType is 'B
20aa9 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a 20 53 51 4c  LOBINT', .** SQL
20aaa 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20  ITE_AFF_INTEGER 
20aab 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
20aac 2a 2a 20 53 75 62 73 74 72 69 6e 67 20 20 20 20  ** Substring    
20aad 20 7c 20 41 66 66 69 6e 69 74 79 0a 2a 2a 20 2d   | Affinity.** -
20aae 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20aaf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
20ab0 2a 2a 20 27 49 4e 54 27 20 20 20 20 20 20 20 20  ** 'INT'        
20ab1 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e   | SQLITE_AFF_IN
20ab2 54 45 47 45 52 0a 2a 2a 20 27 43 48 41 52 27 20  TEGER.** 'CHAR' 
20ab3 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
20ab4 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 43 4c 4f  AFF_TEXT.** 'CLO
20ab5 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49  B'        | SQLI
20ab6 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27  TE_AFF_TEXT.** '
20ab7 54 45 58 54 27 20 20 20 20 20 20 20 20 7c 20 53  TEXT'        | S
20ab8 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a  QLITE_AFF_TEXT.*
20ab9 2a 20 27 42 4c 4f 42 27 20 20 20 20 20 20 20 20  * 'BLOB'        
20aba 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  | SQLITE_AFF_NON
20abb 45 0a 2a 2a 20 27 52 45 41 4c 27 20 20 20 20 20  E.** 'REAL'     
20abc 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
20abd 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f 41 27 20 20  REAL.** 'FLOA'  
20abe 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
20abf 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 44 4f 55 42  FF_REAL.** 'DOUB
20ac0 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54  '        | SQLIT
20ac1 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 0a 2a 2a  E_AFF_REAL.**.**
20ac2 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   If none of the 
20ac3 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74 68  substrings in th
20ac4 65 20 61 62 6f 76 65 20 74 61 62 6c 65 20 61 72  e above table ar
20ac5 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20 53 51 4c 49  e found,.** SQLI
20ac6 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 69  TE_AFF_NUMERIC i
20ac7 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
20ac8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
20ac9 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69  ar sqlite3Affini
20aca 74 79 54 79 70 65 28 63 6f 6e 73 74 20 54 6f 6b  tyType(const Tok
20acb 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 20 75 33  en *pType){.  u3
20acc 32 20 68 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  2 h = 0;.  char 
20acd 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
20ace 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 63 6f 6e 73  _NUMERIC;.  cons
20acf 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
20ad0 2a 7a 49 6e 20 3d 20 70 54 79 70 65 2d 3e 7a 3b  *zIn = pType->z;
20ad1 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
20ad2 64 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 26  d char *zEnd = &
20ad3 70 54 79 70 65 2d 3e 7a 5b 70 54 79 70 65 2d 3e  pType->z[pType->
20ad4 6e 5d 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49  n];..  while( zI
20ad5 6e 21 3d 7a 45 6e 64 20 29 7b 0a 20 20 20 20 68  n!=zEnd ){.    h
20ad6 20 3d 20 28 68 3c 3c 38 29 20 2b 20 73 71 6c 69   = (h<<8) + sqli
20ad7 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
20ad8 2a 7a 49 6e 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b  *zIn];.    zIn++
20ad9 3b 0a 20 20 20 20 69 66 28 20 68 3d 3d 28 28 27  ;.    if( h==(('
20ada 63 27 3c 3c 32 34 29 2b 28 27 68 27 3c 3c 31 36  c'<<24)+('h'<<16
20adb 29 2b 28 27 61 27 3c 3c 38 29 2b 27 72 27 29 20  )+('a'<<8)+'r') 
20adc 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ){             /
20add 2a 20 43 48 41 52 20 2a 2f 0a 20 20 20 20 20 20  * CHAR */.      
20ade 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
20adf 5f 54 45 58 54 3b 20 0a 20 20 20 20 7d 65 6c 73  _TEXT; .    }els
20ae0 65 20 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c  e if( h==(('c'<<
20ae1 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27  24)+('l'<<16)+('
20ae2 6f 27 3c 3c 38 29 2b 27 62 27 29 20 29 7b 20 20  o'<<8)+'b') ){  
20ae3 20 20 20 20 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a       /* CLOB */.
20ae4 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49        aff = SQLI
20ae5 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20  TE_AFF_TEXT;.   
20ae6 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28   }else if( h==((
20ae7 27 74 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31  't'<<24)+('e'<<1
20ae8 36 29 2b 28 27 78 27 3c 3c 38 29 2b 27 74 27 29  6)+('x'<<8)+'t')
20ae9 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 54 45 58   ){       /* TEX
20aea 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d  T */.      aff =
20aeb 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
20aec 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
20aed 68 3d 3d 28 28 27 62 27 3c 3c 32 34 29 2b 28 27  h==(('b'<<24)+('
20aee 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29  l'<<16)+('o'<<8)
20aef 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 20 2f  +'b')          /
20af0 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20  * BLOB */.      
20af1 20 20 26 26 20 28 61 66 66 3d 3d 53 51 4c 49 54    && (aff==SQLIT
20af2 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 7c  E_AFF_NUMERIC ||
20af3 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46   aff==SQLITE_AFF
20af4 5f 52 45 41 4c 29 20 29 7b 0a 20 20 20 20 20 20  _REAL) ){.      
20af5 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
20af6 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64 65 66 20 53  _NONE;.#ifndef S
20af7 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
20af8 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 7d 65  ING_POINT.    }e
20af9 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 72 27  lse if( h==(('r'
20afa 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b  <<24)+('e'<<16)+
20afb 28 27 61 27 3c 3c 38 29 2b 27 6c 27 29 20 20 20  ('a'<<8)+'l')   
20afc 20 20 20 20 20 20 20 2f 2a 20 52 45 41 4c 20 2a         /* REAL *
20afd 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66  /.        && aff
20afe 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  ==SQLITE_AFF_NUM
20aff 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66  ERIC ){.      af
20b00 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52  f = SQLITE_AFF_R
20b01 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  EAL;.    }else i
20b02 66 28 20 68 3d 3d 28 28 27 66 27 3c 3c 32 34 29  f( h==(('f'<<24)
20b03 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c  +('l'<<16)+('o'<
20b04 3c 38 29 2b 27 61 27 29 20 20 20 20 20 20 20 20  <8)+'a')        
20b05 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f 0a 20 20 20    /* FLOA */.   
20b06 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c       && aff==SQL
20b07 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20  ITE_AFF_NUMERIC 
20b08 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  ){.      aff = S
20b09 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a  QLITE_AFF_REAL;.
20b0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d      }else if( h=
20b0b 3d 28 28 27 64 27 3c 3c 32 34 29 2b 28 27 6f 27  =(('d'<<24)+('o'
20b0c 3c 3c 31 36 29 2b 28 27 75 27 3c 3c 38 29 2b 27  <<16)+('u'<<8)+'
20b0d 62 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b')          /* 
20b0e 44 4f 55 42 20 2a 2f 0a 20 20 20 20 20 20 20 20  DOUB */.        
20b0f 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41  && aff==SQLITE_A
20b10 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20  FF_NUMERIC ){.  
20b11 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
20b12 5f 41 46 46 5f 52 45 41 4c 3b 0a 23 65 6e 64 69  _AFF_REAL;.#endi
20b13 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  f.    }else if( 
20b14 28 68 26 30 78 30 30 46 46 46 46 46 46 29 3d 3d  (h&0x00FFFFFF)==
20b15 28 28 27 69 27 3c 3c 31 36 29 2b 28 27 6e 27 3c  (('i'<<16)+('n'<
20b16 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20 2f  <8)+'t') ){    /
20b17 2a 20 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 61  * INT */.      a
20b18 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
20b19 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 62  INTEGER;.      b
20b1a 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
20b1b 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d  .  return aff;.}
20b1c 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
20b1d 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62  tine is called b
20b1e 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69  y the parser whi
20b1f 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  le in the middle
20b20 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61   of.** parsing a
20b21 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
20b22 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46  atement.  The pF
20b23 69 72 73 74 20 74 6f 6b 65 6e 20 69 73 20 74 68  irst token is th
20b24 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e  e first.** token
20b25 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65   in the sequence
20b26 20 6f 66 20 74 6f 6b 65 6e 73 20 74 68 61 74 20   of tokens that 
20b27 64 65 73 63 72 69 62 65 20 74 68 65 20 74 79 70  describe the typ
20b28 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75  e of the.** colu
20b29 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64  mn currently und
20b2a 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e  er construction.
20b2b 20 20 20 70 4c 61 73 74 20 69 73 20 74 68 65 20     pLast is the 
20b2c 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e  last token.** in
20b2d 20 74 68 65 20 73 65 71 75 65 6e 63 65 2e 20 20   the sequence.  
20b2e 55 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61  Use this informa
20b2f 74 69 6f 6e 20 74 6f 20 63 6f 6e 73 74 72 75 63  tion to construc
20b30 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68  t a string.** th
20b31 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
20b32 74 79 70 65 6e 61 6d 65 20 6f 66 20 74 68 65 20  typename of the 
20b33 63 6f 6c 75 6d 6e 20 61 6e 64 20 73 74 6f 72 65  column and store
20b34 20 74 68 61 74 20 73 74 72 69 6e 67 0a 2a 2a 20   that string.** 
20b35 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f 20 0a 53 51  in zType..*/ .SQ
20b36 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20b37 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75  d sqlite3AddColu
20b38 6d 6e 54 79 70 65 28 50 61 72 73 65 20 2a 70 50  mnType(Parse *pP
20b39 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 79  arse, Token *pTy
20b3a 70 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  pe){.  Table *p;
20b3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c 75  .  int i;.  Colu
20b3c 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69  mn *pCol;.  sqli
20b3d 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20  te3 *db;..  if( 
20b3e 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  (p = pParse->pNe
20b3f 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74  wTable)==0 ) ret
20b40 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43  urn;.  i = p->nC
20b41 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69 3c 30 20  ol-1;.  if( i<0 
20b42 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 6f 6c  ) return;.  pCol
20b43 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a   = &p->aCol[i];.
20b44 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
20b45 62 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  b;.  sqlite3DbFr
20b46 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79  ee(db, pCol->zTy
20b47 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79  pe);.  pCol->zTy
20b48 70 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  pe = sqlite3Name
20b49 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54  FromToken(db, pT
20b4a 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 61 66  ype);.  pCol->af
20b4b 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33  finity = sqlite3
20b4c 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 54 79  AffinityType(pTy
20b4d 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  pe);.}../*.** Th
20b4e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
20b4f 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
20b50 65 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72  e for the most r
20b51 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 63 6f  ecently added co
20b52 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 74  lumn.** of the t
20b53 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75  able currently u
20b54 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
20b55 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 61 75 6c 74  n..**.** Default
20b56 20 76 61 6c 75 65 20 65 78 70 72 65 73 73 69 6f   value expressio
20b57 6e 73 20 6d 75 73 74 20 62 65 20 63 6f 6e 73 74  ns must be const
20b58 61 6e 74 2e 20 20 52 61 69 73 65 20 61 6e 20 65  ant.  Raise an e
20b59 78 63 65 70 74 69 6f 6e 20 69 66 20 74 68 69 73  xception if this
20b5a 0a 2a 2a 20 69 73 20 6e 6f 74 20 74 68 65 20 63  .** is not the c
20b5b 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ase..**.** This 
20b5c 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
20b5d 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
20b5e 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64  while in the mid
20b5f 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e  dle of.** parsin
20b60 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  g a CREATE TABLE
20b61 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
20b62 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
20b63 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66  id sqlite3AddDef
20b64 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 20  aultValue(Parse 
20b65 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
20b66 45 78 70 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a  Expr){.  Table *
20b67 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  p;.  Column *pCo
20b68 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  l;.  sqlite3 *db
20b69 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
20b6a 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65   if( (p = pParse
20b6b 2d 3e 70 4e 65 77 54 61 62 6c 65 29 21 3d 30 20  ->pNewTable)!=0 
20b6c 29 7b 0a 20 20 20 20 70 43 6f 6c 20 3d 20 26 28  ){.    pCol = &(
20b6d 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d  p->aCol[p->nCol-
20b6e 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 73 71  1]);.    if( !sq
20b6f 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
20b70 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 70 45  antOrFunction(pE
20b71 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71  xpr) ){.      sq
20b72 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
20b73 61 72 73 65 2c 20 22 64 65 66 61 75 6c 74 20 76  arse, "default v
20b74 61 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 5b  alue of column [
20b75 25 73 5d 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74  %s] is not const
20b76 61 6e 74 22 2c 0a 20 20 20 20 20 20 20 20 20 20  ant",.          
20b77 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pCol->zName);.  
20b78 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 45    }else{.      E
20b79 78 70 72 20 2a 70 43 6f 70 79 3b 0a 20 20 20 20  xpr *pCopy;.    
20b7a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
20b7b 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44  ete(db, pCol->pD
20b7c 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c  flt);.      pCol
20b7d 2d 3e 70 44 66 6c 74 20 3d 20 70 43 6f 70 79 20  ->pDflt = pCopy 
20b7e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
20b7f 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  (db, pExpr);.   
20b80 20 20 20 69 66 28 20 70 43 6f 70 79 20 29 7b 0a     if( pCopy ){.
20b81 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 54          sqlite3T
20b82 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 43  okenCopy(db, &pC
20b83 6f 70 79 2d 3e 73 70 61 6e 2c 20 26 70 45 78 70  opy->span, &pExp
20b84 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20 20  r->span);.      
20b85 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  }.    }.  }.  sq
20b86 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
20b87 64 62 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f  db, pExpr);.}../
20b88 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65 20 74  *.** Designate t
20b89 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 66  he PRIMARY KEY f
20b8a 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20 70  or the table.  p
20b8b 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f  List is a list o
20b8c 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63  f names .** of c
20b8d 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d  olumns that form
20b8e 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
20b8f 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 20 4e  .  If pList is N
20b90 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ULL, then the.**
20b91 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61   most recently a
20b92 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  dded column of t
20b93 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  he table is the 
20b94 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a  primary key..**.
20b95 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e 20 68  ** A table can h
20b96 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20  ave at most one 
20b97 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66  primary key.  If
20b98 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61   the table alrea
20b99 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d  dy has.** a prim
20b9a 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74 68 69  ary key (and thi
20b9b 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  s is the second 
20b9c 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74 68 65  primary key) the
20b9d 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65  n create an.** e
20b9e 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rror..**.** If t
20b9f 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69  he PRIMARY KEY i
20ba0 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f  s on a single co
20ba1 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74 61 74  lumn whose datat
20ba2 79 70 65 20 69 73 20 49 4e 54 45 47 45 52 2c 0a  ype is INTEGER,.
20ba3 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20  ** then we will 
20ba4 74 72 79 20 74 6f 20 75 73 65 20 74 68 61 74 20  try to use that 
20ba5 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 72 6f  column as the ro
20ba6 77 69 64 2e 20 20 53 65 74 20 74 68 65 20 54 61  wid.  Set the Ta
20ba7 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65  ble.iPKey.** fie
20ba8 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ld of the table 
20ba9 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
20baa 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69 6e 64  on to be the ind
20bab 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54  ex of the.** INT
20bac 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
20bad 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e   column.  Table.
20bae 69 50 4b 65 79 20 69 73 20 73 65 74 20 74 6f 20  iPKey is set to 
20baf 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a  -1 if there is.*
20bb0 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49  * no INTEGER PRI
20bb1 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20  MARY KEY..**.** 
20bb2 49 66 20 74 68 65 20 6b 65 79 20 69 73 20 6e 6f  If the key is no
20bb3 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  t an INTEGER PRI
20bb4 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 63  MARY KEY, then c
20bb5 72 65 61 74 65 20 61 20 75 6e 69 71 75 65 0a 2a  reate a unique.*
20bb6 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68 65 20  * index for the 
20bb7 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69  key.  No index i
20bb8 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 49 4e  s created for IN
20bb9 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
20bba 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  Ys..*/.SQLITE_PR
20bbb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
20bbc 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28  e3AddPrimaryKey(
20bbd 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
20bbe 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  ,    /* Parsing 
20bbf 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
20bc0 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f  rList *pList,  /
20bc1 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c 64 20  * List of field 
20bc2 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e 64 65  names to be inde
20bc3 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45  xed */.  int onE
20bc4 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 57 68  rror,      /* Wh
20bc5 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 61 20  at to do with a 
20bc6 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c  uniqueness confl
20bc7 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74  ict */.  int aut
20bc8 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 54 72  oInc,      /* Tr
20bc9 75 65 20 69 66 20 74 68 65 20 41 55 54 4f 49 4e  ue if the AUTOIN
20bca 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20  CREMENT keyword 
20bcb 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
20bcc 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 20 20  int sortOrder   
20bcd 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41    /* SQLITE_SO_A
20bce 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f  SC or SQLITE_SO_
20bcf 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62  DESC */.){.  Tab
20bd0 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73  le *pTab = pPars
20bd1 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20  e->pNewTable;.  
20bd2 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b  char *zType = 0;
20bd3 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31  .  int iCol = -1
20bd4 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  , i;.  if( pTab=
20bd5 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45  =0 || IN_DECLARE
20bd6 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70 72 69  _VTAB ) goto pri
20bd7 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20  mary_key_exit;. 
20bd8 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c   if( pTab->tabFl
20bd9 61 67 73 20 26 20 54 46 5f 48 61 73 50 72 69 6d  ags & TF_HasPrim
20bda 61 72 79 4b 65 79 20 29 7b 0a 20 20 20 20 73 71  aryKey ){.    sq
20bdb 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
20bdc 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 74 61  arse, .      "ta
20bdd 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73 20 6d  ble \"%s\" has m
20bde 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70 72 69  ore than one pri
20bdf 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61 62 2d  mary key", pTab-
20be0 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
20be1 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78  o primary_key_ex
20be2 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e  it;.  }.  pTab->
20be3 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48  tabFlags |= TF_H
20be4 61 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a 20 20  asPrimaryKey;.  
20be5 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a  if( pList==0 ){.
20be6 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d      iCol = pTab-
20be7 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 70  >nCol - 1;.    p
20be8 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
20be9 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20  isPrimKey = 1;. 
20bea 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28   }else{.    for(
20beb 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
20bec 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
20bed 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f   for(iCol=0; iCo
20bee 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43  l<pTab->nCol; iC
20bef 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  ol++){.        i
20bf0 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
20bf1 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e  p(pList->a[i].zN
20bf2 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ame, pTab->aCol[
20bf3 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20  iCol].zName)==0 
20bf4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ){.          bre
20bf5 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
20bf6 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
20bf7 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20  iCol<pTab->nCol 
20bf8 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d  ){.        pTab-
20bf9 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72  >aCol[iCol].isPr
20bfa 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 20  imKey = 1;.     
20bfb 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
20bfc 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 31 20   pList->nExpr>1 
20bfd 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 7d  ) iCol = -1;.  }
20bfe 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26  .  if( iCol>=0 &
20bff 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  & iCol<pTab->nCo
20c00 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d  l ){.    zType =
20c01 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
20c02 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20 20 69  ].zType;.  }.  i
20c03 66 28 20 7a 54 79 70 65 20 26 26 20 73 71 6c 69  f( zType && sqli
20c04 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79 70 65  te3StrICmp(zType
20c05 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a  , "INTEGER")==0.
20c06 20 20 20 20 20 20 20 20 26 26 20 73 6f 72 74 4f          && sortO
20c07 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f  rder==SQLITE_SO_
20c08 41 53 43 20 29 7b 0a 20 20 20 20 70 54 61 62 2d  ASC ){.    pTab-
20c09 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20  >iPKey = iCol;. 
20c0a 20 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66     pTab->keyConf
20c0b 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a   = (u8)onError;.
20c0c 20 20 20 20 61 73 73 65 72 74 28 20 61 75 74 6f      assert( auto
20c0d 49 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e  Inc==0 || autoIn
20c0e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54 61 62  c==1 );.    pTab
20c0f 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 61 75  ->tabFlags |= au
20c10 74 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63  toInc*TF_Autoinc
20c11 72 65 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20  rement;.  }else 
20c12 69 66 28 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23  if( autoInc ){.#
20c13 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20c14 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
20c15 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
20c16 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 41 55  rMsg(pParse, "AU
20c17 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f  TOINCREMENT is o
20c18 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61  nly allowed on a
20c19 6e 20 22 0a 20 20 20 20 20 20 20 22 49 4e 54 45  n ".       "INTE
20c1a 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 22  GER PRIMARY KEY"
20c1b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73  );.#endif.  }els
20c1c 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72  e{.    sqlite3Cr
20c1d 65 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65  eateIndex(pParse
20c1e 2c 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74  , 0, 0, 0, pList
20c1f 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c  , onError, 0, 0,
20c20 20 73 6f 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a   sortOrder, 0);.
20c21 20 20 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20      pList = 0;. 
20c22 20 7d 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f   }..primary_key_
20c23 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45  exit:.  sqlite3E
20c24 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
20c25 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29  arse->db, pList)
20c26 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;.  return;.}../
20c27 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43  *.** Add a new C
20c28 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20  HECK constraint 
20c29 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
20c2a 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
20c2b 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  struction..*/.SQ
20c2c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20c2d 64 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63  d sqlite3AddChec
20c2e 6b 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50  kConstraint(.  P
20c2f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
20c30 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
20c31 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ext */.  Expr *p
20c32 43 68 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68  CheckExpr  /* Th
20c33 65 20 63 68 65 63 6b 20 65 78 70 72 65 73 73 69  e check expressi
20c34 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  on */.){.  sqlit
20c35 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
20c36 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  >db;.#ifndef SQL
20c37 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20  ITE_OMIT_CHECK. 
20c38 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
20c39 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
20c3a 3b 0a 20 20 69 66 28 20 70 54 61 62 20 26 26 20  ;.  if( pTab && 
20c3b 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42  !IN_DECLARE_VTAB
20c3c 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 43   ){.    /* The C
20c3d 48 45 43 4b 20 65 78 70 72 65 73 73 69 6f 6e 20  HECK expression 
20c3e 6d 75 73 74 20 62 65 20 64 75 70 6c 69 63 61 74  must be duplicat
20c3f 65 64 20 73 6f 20 74 68 61 74 20 74 6f 6b 65 6e  ed so that token
20c40 73 20 72 65 66 65 72 0a 20 20 20 20 2a 2a 20 74  s refer.    ** t
20c41 6f 20 6d 61 6c 6c 6f 63 65 64 20 73 70 61 63 65  o malloced space
20c42 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 28 65 70   and not the (ep
20c43 68 65 6d 65 72 61 6c 29 20 74 65 78 74 20 6f 66  hemeral) text of
20c44 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c   the CREATE TABL
20c45 45 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65  E.    ** stateme
20c46 6e 74 20 2a 2f 0a 20 20 20 20 70 54 61 62 2d 3e  nt */.    pTab->
20c47 70 43 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33  pCheck = sqlite3
20c48 45 78 70 72 41 6e 64 28 64 62 2c 20 70 54 61 62  ExprAnd(db, pTab
20c49 2d 3e 70 43 68 65 63 6b 2c 20 0a 20 20 20 20 20  ->pCheck, .     
20c4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20c4b 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
20c4c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
20c4d 70 43 68 65 63 6b 45 78 70 72 29 29 3b 0a 20 20  pCheckExpr));.  
20c4e 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  }.#endif.  sqlit
20c4f 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
20c50 20 70 43 68 65 63 6b 45 78 70 72 29 3b 0a 7d 0a   pCheckExpr);.}.
20c51 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63  ./*.** Set the c
20c52 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  ollation functio
20c53 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  n of the most re
20c54 63 65 6e 74 6c 79 20 70 61 72 73 65 64 20 74 61  cently parsed ta
20c55 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f  ble column.** to
20c56 20 74 68 65 20 43 6f 6c 6c 53 65 71 20 67 69 76   the CollSeq giv
20c57 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  en..*/.SQLITE_PR
20c58 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
20c59 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65  e3AddCollateType
20c5a 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
20c5b 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a  Token *pToken){.
20c5c 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e    Table *p;.  in
20c5d 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f  t i;.  char *zCo
20c5e 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
20c5f 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d   /* Dequoted nam
20c60 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  e of collation s
20c61 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 73 71 6c  equence */.  sql
20c62 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28  ite3 *db;..  if(
20c63 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e   (p = pParse->pN
20c64 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65  ewTable)==0 ) re
20c65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e  turn;.  i = p->n
20c66 43 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d 20 70 50  Col-1;.  db = pP
20c67 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c  arse->db;.  zCol
20c68 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  l = sqlite3NameF
20c69 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f  romToken(db, pTo
20c6a 6b 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f  ken);.  if( !zCo
20c6b 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  ll ) return;..  
20c6c 69 66 28 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  if( sqlite3Locat
20c6d 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
20c6e 20 7a 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20   zColl, -1) ){. 
20c6f 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
20c70 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a      p->aCol[i].z
20c71 43 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20  Coll = zColl;.  
20c72 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63  .    /* If the c
20c73 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65  olumn is declare
20c74 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49  d as "<name> PRI
20c75 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45  MARY KEY COLLATE
20c76 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a   <type>",.    **
20c77 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d   then an index m
20c78 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65  ay have been cre
20c79 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c  ated on this col
20c7a 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20  umn before the. 
20c7b 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20     ** collation 
20c7c 74 79 70 65 20 77 61 73 20 61 64 64 65 64 2e 20  type was added. 
20c7d 43 6f 72 72 65 63 74 20 74 68 69 73 20 69 66 20  Correct this if 
20c7e 69 74 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a  it is the case..
20c7f 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70      */.    for(p
20c80 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70  Idx=p->pIndex; p
20c81 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
20c82 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73  pNext){.      as
20c83 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c  sert( pIdx->nCol
20c84 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
20c85 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  if( pIdx->aiColu
20c86 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20  mn[0]==i ){.    
20c87 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c      pIdx->azColl
20c88 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d  [0] = p->aCol[i]
20c89 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a  .zColl;.      }.
20c8a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
20c8b 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
20c8c 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d  (db, zColl);.  }
20c8d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
20c8e 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
20c8f 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
20c90 71 75 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62  quence for datab
20c91 61 73 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a  ase native text.
20c92 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e  ** encoding iden
20c93 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 74  tified by the st
20c94 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67  ring zName, leng
20c95 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  th nName..**.** 
20c96 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  If the requested
20c97 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
20c98 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c  nce is not avail
20c99 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61  able, or not ava
20c9a 69 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65  ilable.** in the
20c9b 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65   database native
20c9c 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63   encoding, the c
20c9d 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79  ollation factory
20c9e 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a   is invoked to.*
20c9f 2a 20 72 65 71 75 65 73 74 20 69 74 2e 20 49 66  * request it. If
20ca0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
20ca1 61 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20  actory does not 
20ca2 73 75 70 70 6c 79 20 73 75 63 68 20 61 20 73 65  supply such a se
20ca3 71 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74  quence,.** and t
20ca4 68 65 20 73 65 71 75 65 6e 63 65 20 69 73 20 61  he sequence is a
20ca5 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74  vailable in anot
20ca6 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  her text encodin
20ca7 67 2c 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a  g, then that is.
20ca8 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74  ** returned inst
20ca9 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  ead..**.** If no
20caa 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
20cab 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61   requested colla
20cac 74 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61  tions sequence a
20cad 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72  re available, or
20cae 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f  .** another erro
20caf 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69  r occurs, NULL i
20cb0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61  s returned and a
20cb1 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
20cb2 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20  written into.** 
20cb3 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  pParse..**.** Th
20cb4 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
20cb5 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73  wrapper around s
20cb6 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
20cb7 71 28 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69  q().  This routi
20cb8 6e 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68  ne.** invokes th
20cb9 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74  e collation fact
20cba 6f 72 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64  ory if the named
20cbb 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f   collation canno
20cbc 74 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e  t be found.** an
20cbd 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65  d generates an e
20cbe 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f  rror message..*/
20cbf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20cc0 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
20cc1 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61  LocateCollSeq(Pa
20cc2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
20cc3 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
20cc4 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 73 71  int nName){.  sq
20cc5 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
20cc6 73 65 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63  se->db;.  u8 enc
20cc7 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38   = ENC(db);.  u8
20cc8 20 69 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e   initbusy = db->
20cc9 69 6e 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c  init.busy;.  Col
20cca 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20  lSeq *pColl;..  
20ccb 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  pColl = sqlite3F
20ccc 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65  indCollSeq(db, e
20ccd 6e 63 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  nc, zName, nName
20cce 2c 20 69 6e 69 74 62 75 73 79 29 3b 0a 20 20 69  , initbusy);.  i
20ccf 66 28 20 21 69 6e 69 74 62 75 73 79 20 26 26 20  f( !initbusy && 
20cd0 28 21 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c  (!pColl || !pCol
20cd1 6c 2d 3e 78 43 6d 70 29 20 29 7b 0a 20 20 20 20  l->xCmp) ){.    
20cd2 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
20cd3 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 70 43  etCollSeq(db, pC
20cd4 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  oll, zName, nNam
20cd5 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f  e);.    if( !pCo
20cd6 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
20cd7 6e 4e 61 6d 65 3c 30 20 29 7b 0a 20 20 20 20 20  nName<0 ){.     
20cd8 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74     nName = sqlit
20cd9 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61  e3Strlen(db, zNa
20cda 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  me);.      }.   
20cdb 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
20cdc 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
20cdd 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  uch collation se
20cde 71 75 65 6e 63 65 3a 20 25 2e 2a 73 22 2c 20 6e  quence: %.*s", n
20cdf 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  Name, zName);.  
20ce0 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20      pColl = 0;. 
20ce1 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
20ce2 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a  rn pColl;.}.../*
20ce3 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
20ce4 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 63 72  e that will incr
20ce5 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61  ement the schema
20ce6 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54   cookie..**.** T
20ce7 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
20ce8 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65   is used to dete
20ce9 72 6d 69 6e 65 20 77 68 65 6e 20 74 68 65 20 73  rmine when the s
20cea 63 68 65 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a  chema for the.**
20ceb 20 64 61 74 61 62 61 73 65 20 63 68 61 6e 67 65   database change
20cec 73 2e 20 20 41 66 74 65 72 20 65 61 63 68 20 73  s.  After each s
20ced 63 68 65 6d 61 20 63 68 61 6e 67 65 2c 20 74 68  chema change, th
20cee 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a  e cookie value.*
20cef 2a 20 63 68 61 6e 67 65 73 2e 20 20 57 68 65 6e  * changes.  When
20cf0 20 61 20 70 72 6f 63 65 73 73 20 66 69 72 73 74   a process first
20cf1 20 72 65 61 64 73 20 74 68 65 20 73 63 68 65 6d   reads the schem
20cf2 61 20 69 74 20 72 65 63 6f 72 64 73 20 74 68 65  a it records the
20cf3 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65  .** cookie.  The
20cf4 72 65 61 66 74 65 72 2c 20 77 68 65 6e 65 76 65  reafter, wheneve
20cf5 72 20 69 74 20 67 6f 65 73 20 74 6f 20 61 63 63  r it goes to acc
20cf6 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
20cf7 2c 0a 2a 2a 20 69 74 20 63 68 65 63 6b 73 20 74  ,.** it checks t
20cf8 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b  he cookie to mak
20cf9 65 20 73 75 72 65 20 74 68 65 20 73 63 68 65 6d  e sure the schem
20cfa 61 20 68 61 73 20 6e 6f 74 20 63 68 61 6e 67 65  a has not change
20cfb 64 0a 2a 2a 20 73 69 6e 63 65 20 69 74 20 77 61  d.** since it wa
20cfc 73 20 6c 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a  s last read..**.
20cfd 2a 2a 20 54 68 69 73 20 70 6c 61 6e 20 69 73 20  ** This plan is 
20cfe 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62  not completely b
20cff 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74  ullet-proof.  It
20d00 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72   is possible for
20d01 0a 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 74  .** the schema t
20d02 6f 20 63 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c  o change multipl
20d03 65 20 74 69 6d 65 73 20 61 6e 64 20 66 6f 72 20  e times and for 
20d04 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65  the cookie to be
20d05 0a 2a 2a 20 73 65 74 20 62 61 63 6b 20 74 6f 20  .** set back to 
20d06 70 72 69 6f 72 20 76 61 6c 75 65 2e 20 20 42 75  prior value.  Bu
20d07 74 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  t schema changes
20d08 20 61 72 65 20 69 6e 66 72 65 71 75 65 6e 74 0a   are infrequent.
20d09 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 6f 62 61  ** and the proba
20d0a 62 69 6c 69 74 79 20 6f 66 20 68 69 74 74 69 6e  bility of hittin
20d0b 67 20 74 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69  g the same cooki
20d0c 65 20 76 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a  e value is only.
20d0d 2a 2a 20 31 20 63 68 61 6e 63 65 20 69 6e 20 32  ** 1 chance in 2
20d0e 5e 33 32 2e 20 20 53 6f 20 77 65 27 72 65 20 73  ^32.  So we're s
20d0f 61 66 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53  afe enough..*/.S
20d10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
20d11 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65  id sqlite3Change
20d12 43 6f 6f 6b 69 65 28 50 61 72 73 65 20 2a 70 50  Cookie(Parse *pP
20d13 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a  arse, int iDb){.
20d14 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
20d15 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
20d16 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 20  rse);.  sqlite3 
20d17 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
20d18 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ;.  Vdbe *v = pP
20d19 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73  arse->pVdbe;.  s
20d1a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
20d1b 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
20d1c 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
20d1d 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
20d1e 6b 69 65 2b 31 2c 20 72 31 29 3b 0a 20 20 73 71  kie+1, r1);.  sq
20d1f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
20d20 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c  v, OP_SetCookie,
20d21 20 69 44 62 2c 20 30 2c 20 72 31 29 3b 0a 20 20   iDb, 0, r1);.  
20d22 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
20d23 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
20d24 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73  );.}../*.** Meas
20d25 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ure the number o
20d26 66 20 63 68 61 72 61 63 74 65 72 73 20 6e 65 65  f characters nee
20d27 64 65 64 20 74 6f 20 6f 75 74 70 75 74 20 74 68  ded to output th
20d28 65 20 67 69 76 65 6e 0a 2a 2a 20 69 64 65 6e 74  e given.** ident
20d29 69 66 69 65 72 2e 20 20 54 68 65 20 6e 75 6d 62  ifier.  The numb
20d2a 65 72 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c  er returned incl
20d2b 75 64 65 73 20 61 6e 79 20 71 75 6f 74 65 73 20  udes any quotes 
20d2c 75 73 65 64 0a 2a 2a 20 62 75 74 20 64 6f 65 73  used.** but does
20d2d 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65   not include the
20d2e 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72   null terminator
20d2f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69  ..**.** The esti
20d30 6d 61 74 65 20 69 73 20 63 6f 6e 73 65 72 76 61  mate is conserva
20d31 74 69 76 65 2e 20 20 49 74 20 6d 69 67 68 74 20  tive.  It might 
20d32 62 65 20 6c 61 72 67 65 72 20 74 68 61 74 20 77  be larger that w
20d33 68 61 74 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79  hat is.** really
20d34 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74   needed..*/.stat
20d35 69 63 20 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67  ic int identLeng
20d36 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  th(const char *z
20d37 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 6f  ){.  int n;.  fo
20d38 72 28 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20  r(n=0; *z; n++, 
20d39 7a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a  z++){.    if( *z
20d3a 3d 3d 27 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a  =='"' ){ n++; }.
20d3b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b    }.  return n +
20d3c 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69   2;.}../*.** Wri
20d3d 74 65 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  te an identifier
20d3e 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66   onto the end of
20d3f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e   the given strin
20d40 67 2e 20 20 41 64 64 0a 2a 2a 20 71 75 6f 74 65  g.  Add.** quote
20d41 20 63 68 61 72 61 63 74 65 72 73 20 61 73 20 6e   characters as n
20d42 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  eeded..*/.static
20d43 20 76 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63   void identPut(c
20d44 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64  har *z, int *pId
20d45 78 2c 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64  x, char *zSigned
20d46 49 64 65 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e  Ident){.  unsign
20d47 65 64 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20  ed char *zIdent 
20d48 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
20d49 2a 29 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a  *)zSignedIdent;.
20d4a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64    int i, j, need
20d4b 51 75 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49  Quote;.  i = *pI
20d4c 64 78 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a  dx;.  for(j=0; z
20d4d 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a  Ident[j]; j++){.
20d4e 20 20 20 20 69 66 28 20 21 69 73 61 6c 6e 75 6d      if( !isalnum
20d4f 28 7a 49 64 65 6e 74 5b 6a 5d 29 20 26 26 20 7a  (zIdent[j]) && z
20d50 49 64 65 6e 74 5b 6a 5d 21 3d 27 5f 27 20 29 20  Ident[j]!='_' ) 
20d51 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65  break;.  }.  nee
20d52 64 51 75 6f 74 65 20 3d 20 20 7a 49 64 65 6e 74  dQuote =  zIdent
20d53 5b 6a 5d 21 3d 30 20 7c 7c 20 69 73 64 69 67 69  [j]!=0 || isdigi
20d54 74 28 7a 49 64 65 6e 74 5b 30 5d 29 0a 20 20 20  t(zIdent[0]).   
20d55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
20d56 7c 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64  | sqlite3Keyword
20d57 43 6f 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29 21  Code(zIdent, j)!
20d58 3d 54 4b 5f 49 44 3b 0a 20 20 69 66 28 20 6e 65  =TK_ID;.  if( ne
20d59 65 64 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d  edQuote ) z[i++]
20d5a 20 3d 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d   = '"';.  for(j=
20d5b 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b  0; zIdent[j]; j+
20d5c 2b 29 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d  +){.    z[i++] =
20d5d 20 7a 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20   zIdent[j];.    
20d5e 69 66 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27  if( zIdent[j]=='
20d5f 22 27 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22  "' ) z[i++] = '"
20d60 27 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65  ';.  }.  if( nee
20d61 64 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20  dQuote ) z[i++] 
20d62 3d 20 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20  = '"';.  z[i] = 
20d63 30 3b 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a  0;.  *pIdx = i;.
20d64 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
20d65 65 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  e a CREATE TABLE
20d66 20 73 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f   statement appro
20d67 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 67  priate for the g
20d68 69 76 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20  iven.** table.  
20d69 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74  Memory to hold t
20d6a 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 73  he text of the s
20d6b 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61  tatement is obta
20d6c 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c  ined.** from sql
20d6d 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  iteMalloc() and 
20d6e 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79  must be freed by
20d6f 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   the calling fun
20d70 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
20d71 20 63 68 61 72 20 2a 63 72 65 61 74 65 54 61 62   char *createTab
20d72 6c 65 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a  leStmt(sqlite3 *
20d73 64 62 2c 20 54 61 62 6c 65 20 2a 70 2c 20 69 6e  db, Table *p, in
20d74 74 20 69 73 54 65 6d 70 29 7b 0a 20 20 69 6e 74  t isTemp){.  int
20d75 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 72   i, k, n;.  char
20d76 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72 20   *zStmt;.  char 
20d77 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a  *zSep, *zSep2, *
20d78 7a 45 6e 64 2c 20 2a 7a 3b 0a 20 20 43 6f 6c 75  zEnd, *z;.  Colu
20d79 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20  mn *pCol;.  n = 
20d7a 30 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20  0;.  for(pCol = 
20d7b 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c  p->aCol, i=0; i<
20d7c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43  p->nCol; i++, pC
20d7d 6f 6c 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20  ol++){.    n += 
20d7e 69 64 65 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c  identLength(pCol
20d7f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7a 20  ->zName);.    z 
20d80 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a 20  = pCol->zType;. 
20d81 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20     if( z ){.    
20d82 20 20 6e 20 2b 3d 20 28 73 71 6c 69 74 65 33 53    n += (sqlite3S
20d83 74 72 6c 65 6e 33 30 28 7a 29 20 2b 20 31 29 3b  trlen30(z) + 1);
20d84 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6e 20 2b  .    }.  }.  n +
20d85 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 70 2d  = identLength(p-
20d86 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 6e  >zName);.  if( n
20d87 3c 35 30 20 29 7b 0a 20 20 20 20 7a 53 65 70 20  <50 ){.    zSep 
20d88 3d 20 22 22 3b 0a 20 20 20 20 7a 53 65 70 32 20  = "";.    zSep2 
20d89 3d 20 22 2c 22 3b 0a 20 20 20 20 7a 45 6e 64 20  = ",";.    zEnd 
20d8a 3d 20 22 29 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a  = ")";.  }else{.
20d8b 20 20 20 20 7a 53 65 70 20 3d 20 22 5c 6e 20 20      zSep = "\n  
20d8c 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 22  ";.    zSep2 = "
20d8d 2c 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 45 6e 64  ,\n  ";.    zEnd
20d8e 20 3d 20 22 5c 6e 29 22 3b 0a 20 20 7d 0a 20 20   = "\n)";.  }.  
20d8f 6e 20 2b 3d 20 33 35 20 2b 20 36 2a 70 2d 3e 6e  n += 35 + 6*p->n
20d90 43 6f 6c 3b 0a 20 20 7a 53 74 6d 74 20 3d 20 73  Col;.  zStmt = s
20d91 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 20  qlite3Malloc( n 
20d92 29 3b 0a 20 20 69 66 28 20 7a 53 74 6d 74 3d 3d  );.  if( zStmt==
20d93 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  0 ){.    db->mal
20d94 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
20d95 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
20d96 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
20d97 6e 74 66 28 6e 2c 20 7a 53 74 6d 74 2c 0a 20 20  ntf(n, zStmt,.  
20d98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d99 21 4f 4d 49 54 5f 54 45 4d 50 44 42 26 26 69 73  !OMIT_TEMPDB&&is
20d9a 54 65 6d 70 20 3f 20 22 43 52 45 41 54 45 20 54  Temp ? "CREATE T
20d9b 45 4d 50 20 54 41 42 4c 45 20 22 3a 22 43 52 45  EMP TABLE ":"CRE
20d9c 41 54 45 20 54 41 42 4c 45 20 22 29 3b 0a 20 20  ATE TABLE ");.  
20d9d 6b 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  k = sqlite3Strle
20d9e 6e 33 30 28 7a 53 74 6d 74 29 3b 0a 20 20 69 64  n30(zStmt);.  id
20d9f 65 6e 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b  entPut(zStmt, &k
20da0 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7a  , p->zName);.  z
20da1 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b  Stmt[k++] = '(';
20da2 0a 20 20 66 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61  .  for(pCol=p->a
20da3 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e  Col, i=0; i<p->n
20da4 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b  Col; i++, pCol++
20da5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
20da6 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53  nprintf(n-k, &zS
20da7 74 6d 74 5b 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20  tmt[k], zSep);. 
20da8 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53     k += sqlite3S
20da9 74 72 6c 65 6e 33 30 28 26 7a 53 74 6d 74 5b 6b  trlen30(&zStmt[k
20daa 5d 29 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 7a  ]);.    zSep = z
20dab 53 65 70 32 3b 0a 20 20 20 20 69 64 65 6e 74 50  Sep2;.    identP
20dac 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 43  ut(zStmt, &k, pC
20dad 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ol->zName);.    
20dae 69 66 28 20 28 7a 20 3d 20 70 43 6f 6c 2d 3e 7a  if( (z = pCol->z
20daf 54 79 70 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Type)!=0 ){.    
20db0 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27    zStmt[k++] = '
20db1 20 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ';.      assert
20db2 28 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33 53  ( (int)(sqlite3S
20db3 74 72 6c 65 6e 33 30 28 7a 29 2b 6b 2b 31 29 3c  trlen30(z)+k+1)<
20db4 3d 6e 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =n );.      sqli
20db5 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b  te3_snprintf(n-k
20db6 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25 73  , &zStmt[k], "%s
20db7 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 6b 20 2b  ", z);.      k +
20db8 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
20db9 30 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  0(z);.    }.  }.
20dba 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
20dbb 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b  tf(n-k, &zStmt[k
20dbc 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29 3b 0a  ], "%s", zEnd);.
20dbd 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74 3b 0a    return zStmt;.
20dbe 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
20dbf 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
20dc0 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20 66 69  to report the fi
20dc1 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74 65 72  nal ")" that ter
20dc2 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43 52 45  minates.** a CRE
20dc3 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
20dc4 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ent..**.** The t
20dc5 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74  able structure t
20dc6 68 61 74 20 6f 74 68 65 72 20 61 63 74 69 6f 6e  hat other action
20dc7 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 62   routines have b
20dc8 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a 2a 20  een building.** 
20dc9 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  is added to the 
20dca 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61  internal hash ta
20dcb 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67 20 6e  bles, assuming n
20dcc 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a 2a 2a  o errors have.**
20dcd 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a   occurred..**.**
20dce 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20 74 68   An entry for th
20dcf 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64 65 20  e table is made 
20dd0 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 74 61  in the master ta
20dd1 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75 6e 6c  ble on disk, unl
20dd2 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73 20 61  ess.** this is a
20dd3 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
20dd4 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73   or db->init.bus
20dd5 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62 2d 3e  y==1.  When db->
20dd6 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a 2a 20  init.busy==1.** 
20dd7 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20  it means we are 
20dd8 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69  reading the sqli
20dd9 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
20dda 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 0a  because we just.
20ddb 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20  ** connected to 
20ddc 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
20ddd 62 65 63 61 75 73 65 20 74 68 65 20 73 71 6c 69  because the sqli
20dde 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
20ddf 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c 79 20  has.** recently 
20de0 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68 65 20  changed, so the 
20de1 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20 74  entry for this t
20de2 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69  able already exi
20de3 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71  sts in.** the sq
20de4 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
20de5 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61  e.  We do not wa
20de6 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69 74 20  nt to create it 
20de7 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  again..**.** If 
20de8 74 68 65 20 70 53 65 6c 65 63 74 20 61 72 67 75  the pSelect argu
20de9 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ment is not NULL
20dea 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
20deb 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
20dec 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72  was called to cr
20ded 65 61 74 65 20 61 20 74 61 62 6c 65 20 67 65 6e  eate a table gen
20dee 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20 0a 2a  erated from a .*
20def 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20  * "CREATE TABLE 
20df0 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20 2e 2e  ... AS SELECT ..
20df1 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  ." statement.  T
20df2 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
20df3 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 74 61  of.** the new ta
20df4 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68 20 74  ble will match t
20df5 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
20df6 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a   the SELECT..*/.
20df7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
20df8 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61  oid sqlite3EndTa
20df9 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
20dfa 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
20dfb 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
20dfc 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 6f 6e  */.  Token *pCon
20dfd 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
20dfe 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20 61 66  The ',' token af
20dff 74 65 72 20 74 68 65 20 6c 61 73 74 20 63 6f 6c  ter the last col
20e00 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20 20 54  umn defn. */.  T
20e01 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20 20  oken *pEnd,     
20e02 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69         /* The fi
20e03 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20 69 6e  nal ')' token in
20e04 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c   the CREATE TABL
20e05 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  E */.  Select *p
20e06 53 65 6c 65 63 74 20 20 20 20 20 20 20 20 20 2f  Select         /
20e07 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20 61 20  * Select from a 
20e08 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53 20 53  "CREATE ... AS S
20e09 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20 20 54  ELECT" */.){.  T
20e0a 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69 74  able *p;.  sqlit
20e0b 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
20e0c 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  >db;.  int iDb;.
20e0d 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d 30 20  .  if( (pEnd==0 
20e0e 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29 20 7c  && pSelect==0) |
20e0f 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  | pParse->nErr |
20e10 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
20e11 65 64 20 29 20 7b 0a 20 20 20 20 72 65 74 75 72  ed ) {.    retur
20e12 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 50 61  n;.  }.  p = pPa
20e13 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
20e14 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
20e15 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  urn;..  assert( 
20e16 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c  !db->init.busy |
20e17 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a 0a 20  | !pSelect );.. 
20e18 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
20e19 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
20e1a 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69  p->pSchema);..#i
20e1b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
20e1c 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 65 73  T_CHECK.  /* Res
20e1d 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c  olve names in al
20e1e 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  l CHECK constrai
20e1f 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a  nt expressions..
20e20 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 43    */.  if( p->pC
20e21 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72 63 4c  heck ){.    SrcL
20e22 69 73 74 20 73 53 72 63 3b 20 20 20 20 20 20 20  ist sSrc;       
20e23 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
20e24 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f 72 20  ake SrcList for 
20e25 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
20e26 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e  e */.    NameCon
20e27 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20  text sNC;       
20e28 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
20e29 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 50 61   context for pPa
20e2a 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a  rse->pNewTable *
20e2b 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73  /..    memset(&s
20e2c 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e  NC, 0, sizeof(sN
20e2d 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  C));.    memset(
20e2e 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65 6f 66  &sSrc, 0, sizeof
20e2f 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73 53 72  (sSrc));.    sSr
20e30 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 20  c.nSrc = 1;.    
20e31 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d 65 20  sSrc.a[0].zName 
20e32 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  = p->zName;.    
20e33 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62 20 3d  sSrc.a[0].pTab =
20e34 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b 30   p;.    sSrc.a[0
20e35 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a  ].iCursor = -1;.
20e36 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d      sNC.pParse =
20e37 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e 43   pParse;.    sNC
20e38 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73 53 72  .pSrcList = &sSr
20e39 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43 68 65  c;.    sNC.isChe
20e3a 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20  ck = 1;.    if( 
20e3b 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
20e3c 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d  prNames(&sNC, p-
20e3d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20 20 20  >pCheck) ){.    
20e3e 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
20e3f 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64    }.#endif /* !d
20e40 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
20e41 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20  IT_CHECK) */..  
20e42 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69 6e  /* If the db->in
20e43 69 74 2e 62 75 73 79 20 69 73 20 31 20 69 74 20  it.busy is 1 it 
20e44 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 61  means we are rea
20e45 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f 66 66  ding the SQL off
20e46 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c 69 74   the.  ** "sqlit
20e47 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 73 71  e_master" or "sq
20e48 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72  lite_temp_master
20e49 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 64  " table on the d
20e4a 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64 6f 20  isk..  ** So do 
20e4b 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 65  not write to the
20e4c 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20 45 78   disk again.  Ex
20e4d 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74 20 70  tract the root p
20e4e 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20  age number.  ** 
20e4f 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 66 72  for the table fr
20e50 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e  om the db->init.
20e51 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e 20 20  newTnum field.  
20e52 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72  (The page number
20e53 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 76  .  ** should hav
20e54 65 20 62 65 65 6e 20 70 75 74 20 74 68 65 72 65  e been put there
20e55 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 4f 70   by the sqliteOp
20e56 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29 0a 20  enCb routine.). 
20e57 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   */.  if( db->in
20e58 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 70  it.busy ){.    p
20e59 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e 69  ->tnum = db->ini
20e5a 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d 0a 0a  t.newTnum;.  }..
20e5b 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e 69 74    /* If not init
20e5c 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e 20 63  ializing, then c
20e5d 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 20 66  reate a record f
20e5e 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  or the new table
20e5f 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53 51 4c  .  ** in the SQL
20e60 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65  ITE_MASTER table
20e61 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
20e62 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75  .  The record nu
20e63 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74 68  mber.  ** for th
20e64 65 20 6e 65 77 20 74 61 62 6c 65 20 65 6e 74 72  e new table entr
20e65 79 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79  y should already
20e66 20 62 65 20 6f 6e 20 74 68 65 20 73 74 61 63 6b   be on the stack
20e67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
20e68 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f 52 41  his is a TEMPORA
20e69 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74 65 20  RY table, write 
20e6a 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 74  the entry into t
20e6b 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 2a  he auxiliary.  *
20e6c 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f  * file instead o
20e6d 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20  f into the main 
20e6e 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20  database file.. 
20e6f 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69   */.  if( !db->i
20e70 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
20e71 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62 65 20  int n;.    Vdbe 
20e72 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54  *v;.    char *zT
20e73 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69 65 77  ype;    /* "view
20e74 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a 2f 0a  " or "table" */.
20e75 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 32      char *zType2
20e76 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20 6f 72  ;   /* "VIEW" or
20e77 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20 20 20   "TABLE" */.    
20e78 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20 20 20  char *zStmt;    
20e79 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 43  /* Text of the C
20e7a 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20 43  REATE TABLE or C
20e7b 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65  REATE VIEW state
20e7c 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76 20 3d  ment */..    v =
20e7d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
20e7e 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28  pParse);.    if(
20e7f 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   v==0 ) return;.
20e80 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
20e81 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
20e82 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20  se, 0);..    /* 
20e83 43 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74 70  Create the rootp
20e84 61 67 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20  age for the new 
20e85 74 61 62 6c 65 20 61 6e 64 20 70 75 73 68 20 69  table and push i
20e86 74 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  t onto the stack
20e87 2e 0a 20 20 20 20 2a 2a 20 41 20 76 69 65 77 20  ..    ** A view 
20e88 68 61 73 20 6e 6f 20 72 6f 6f 74 70 61 67 65 2c  has no rootpage,
20e89 20 73 6f 20 6a 75 73 74 20 70 75 73 68 20 61 20   so just push a 
20e8a 7a 65 72 6f 20 6f 6e 74 6f 20 74 68 65 20 73 74  zero onto the st
20e8b 61 63 6b 20 66 6f 72 0a 20 20 20 20 2a 2a 20 76  ack for.    ** v
20e8c 69 65 77 73 2e 20 20 49 6e 69 74 69 61 6c 69 7a  iews.  Initializ
20e8d 65 20 7a 54 79 70 65 20 61 74 20 74 68 65 20 73  e zType at the s
20e8e 61 6d 65 20 74 69 6d 65 2e 0a 20 20 20 20 2a 2f  ame time..    */
20e8f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 53 65 6c  .    if( p->pSel
20e90 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ect==0 ){.      
20e91 2f 2a 20 41 20 72 65 67 75 6c 61 72 20 74 61 62  /* A regular tab
20e92 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70  le */.      zTyp
20e93 65 20 3d 20 22 74 61 62 6c 65 22 3b 0a 20 20 20  e = "table";.   
20e94 20 20 20 7a 54 79 70 65 32 20 3d 20 22 54 41 42     zType2 = "TAB
20e95 4c 45 22 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  LE";.#ifndef SQL
20e96 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
20e97 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
20e98 2a 20 41 20 76 69 65 77 20 2a 2f 0a 20 20 20 20  * A view */.    
20e99 20 20 7a 54 79 70 65 20 3d 20 22 76 69 65 77 22    zType = "view"
20e9a 3b 0a 20 20 20 20 20 20 7a 54 79 70 65 32 20 3d  ;.      zType2 =
20e9b 20 22 56 49 45 57 22 3b 0a 23 65 6e 64 69 66 0a   "VIEW";.#endif.
20e9c 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
20e9d 20 74 68 69 73 20 69 73 20 61 20 43 52 45 41 54   this is a CREAT
20e9e 45 20 54 41 42 4c 45 20 78 78 20 41 53 20 53 45  E TABLE xx AS SE
20e9f 4c 45 43 54 20 2e 2e 2e 2c 20 65 78 65 63 75 74  LECT ..., execut
20ea0 65 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20  e the SELECT.   
20ea1 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 6f   ** statement to
20ea2 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65   populate the ne
20ea3 77 20 74 61 62 6c 65 2e 20 54 68 65 20 72 6f 6f  w table. The roo
20ea4 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  t-page number fo
20ea5 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 65 77  r the.    ** new
20ea6 20 74 61 62 6c 65 20 69 73 20 6f 6e 20 74 68 65   table is on the
20ea7 20 74 6f 70 20 6f 66 20 74 68 65 20 76 64 62 65   top of the vdbe
20ea8 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2a 0a 20   stack..    **. 
20ea9 20 20 20 2a 2a 20 4f 6e 63 65 20 74 68 65 20 53     ** Once the S
20eaa 45 4c 45 43 54 20 68 61 73 20 62 65 65 6e 20 63  ELECT has been c
20eab 6f 64 65 64 20 62 79 20 73 71 6c 69 74 65 33 53  oded by sqlite3S
20eac 65 6c 65 63 74 28 29 2c 20 69 74 20 69 73 20 69  elect(), it is i
20ead 6e 20 61 0a 20 20 20 20 2a 2a 20 73 75 69 74 61  n a.    ** suita
20eae 62 6c 65 20 73 74 61 74 65 20 74 6f 20 71 75 65  ble state to que
20eaf 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ry for the colum
20eb0 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74 79 70 65  n names and type
20eb1 73 20 74 6f 20 62 65 20 75 73 65 64 0a 20 20 20  s to be used.   
20eb2 20 2a 2a 20 62 79 20 74 68 65 20 6e 65 77 20 74   ** by the new t
20eb3 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  able..    **.   
20eb4 20 2a 2a 20 41 20 73 68 61 72 65 64 2d 63 61 63   ** A shared-cac
20eb5 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73  he write-lock is
20eb6 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f   not required to
20eb7 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 6e 65   write to the ne
20eb8 77 20 74 61 62 6c 65 2c 0a 20 20 20 20 2a 2a 20  w table,.    ** 
20eb9 61 73 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b  as a schema-lock
20eba 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72 65 61   must have alrea
20ebb 64 79 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64  dy been obtained
20ebc 20 74 6f 20 63 72 65 61 74 65 20 69 74 2e 20 53   to create it. S
20ebd 69 6e 63 65 0a 20 20 20 20 2a 2a 20 61 20 73 63  ince.    ** a sc
20ebe 68 65 6d 61 2d 6c 6f 63 6b 20 65 78 63 6c 75 64  hema-lock exclud
20ebf 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 64 61 74  es all other dat
20ec0 61 62 61 73 65 20 75 73 65 72 73 2c 20 74 68 65  abase users, the
20ec1 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 6f 75 6c   write-lock woul
20ec2 64 0a 20 20 20 20 2a 2a 20 62 65 20 72 65 64 75  d.    ** be redu
20ec3 6e 64 61 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ndant..    */.  
20ec4 20 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b    if( pSelect ){
20ec5 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73  .      SelectDes
20ec6 74 20 64 65 73 74 3b 0a 20 20 20 20 20 20 54 61  t dest;.      Ta
20ec7 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 0a 0a 20  ble *pSelTab;.. 
20ec8 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 72       assert(pPar
20ec9 73 65 2d 3e 6e 54 61 62 3d 3d 30 29 3b 0a 20 20  se->nTab==0);.  
20eca 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20ecb 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp3(v, OP_Open
20ecc 57 72 69 74 65 2c 20 31 2c 20 70 50 61 72 73 65  Write, 1, pParse
20ecd 2d 3e 72 65 67 52 6f 6f 74 2c 20 69 44 62 29 3b  ->regRoot, iDb);
20ece 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
20ecf 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 31 29  beChangeP5(v, 1)
20ed0 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
20ed1 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20 20 20 20  nTab = 2;.      
20ed2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
20ed3 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54  tInit(&dest, SRT
20ed4 5f 54 61 62 6c 65 2c 20 31 29 3b 0a 20 20 20 20  _Table, 1);.    
20ed5 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28    sqlite3Select(
20ed6 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c  pParse, pSelect,
20ed7 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 73   &dest);.      s
20ed8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
20ed9 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 31 29  (v, OP_Close, 1)
20eda 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72  ;.      if( pPar
20edb 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20  se->nErr==0 ){. 
20edc 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 20 3d         pSelTab =
20edd 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65   sqlite3ResultSe
20ede 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 65  tOfSelect(pParse
20edf 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  , pSelect);.    
20ee0 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 3d      if( pSelTab=
20ee1 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
20ee2 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
20ee3 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  aCol==0 );.     
20ee4 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65     p->nCol = pSe
20ee5 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20  lTab->nCol;.    
20ee6 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 70 53      p->aCol = pS
20ee7 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20  elTab->aCol;.   
20ee8 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43       pSelTab->nC
20ee9 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ol = 0;.        
20eea 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20  pSelTab->aCol = 
20eeb 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  0;.        sqlit
20eec 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 53  e3DeleteTable(pS
20eed 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 7d 0a  elTab);.      }.
20eee 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f      }..    /* Co
20eef 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65  mpute the comple
20ef0 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43  te text of the C
20ef1 52 45 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  REATE statement 
20ef2 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65  */.    if( pSele
20ef3 63 74 20 29 7b 0a 20 20 20 20 20 20 7a 53 74 6d  ct ){.      zStm
20ef4 74 20 3d 20 63 72 65 61 74 65 54 61 62 6c 65 53  t = createTableS
20ef5 74 6d 74 28 64 62 2c 20 70 2c 20 70 2d 3e 70 53  tmt(db, p, p->pS
20ef6 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31  chema==db->aDb[1
20ef7 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  ].pSchema);.    
20ef8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 20 3d  }else{.      n =
20ef9 20 28 69 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d   (int)(pEnd->z -
20efa 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f   pParse->sNameTo
20efb 6b 65 6e 2e 7a 29 20 2b 20 31 3b 0a 20 20 20 20  ken.z) + 1;.    
20efc 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65    zStmt = sqlite
20efd 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20  3MPrintf(db, .  
20efe 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20          "CREATE 
20eff 25 73 20 25 2e 2a 73 22 2c 20 7a 54 79 70 65 32  %s %.*s", zType2
20f00 2c 20 6e 2c 20 70 50 61 72 73 65 2d 3e 73 4e 61  , n, pParse->sNa
20f01 6d 65 54 6f 6b 65 6e 2e 7a 0a 20 20 20 20 20 20  meToken.z.      
20f02 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
20f03 20 41 20 73 6c 6f 74 20 66 6f 72 20 74 68 65 20   A slot for the 
20f04 72 65 63 6f 72 64 20 68 61 73 20 61 6c 72 65 61  record has alrea
20f05 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  dy been allocate
20f06 64 20 69 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a  d in the .    **
20f07 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74   SQLITE_MASTER t
20f08 61 62 6c 65 2e 20 20 57 65 20 6a 75 73 74 20 6e  able.  We just n
20f09 65 65 64 20 74 6f 20 75 70 64 61 74 65 20 74 68  eed to update th
20f0a 61 74 20 73 6c 6f 74 20 77 69 74 68 20 61 6c 6c  at slot with all
20f0b 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 66 6f  .    ** the info
20f0c 72 6d 61 74 69 6f 6e 20 77 65 27 76 65 20 63 6f  rmation we've co
20f0d 6c 6c 65 63 74 65 64 2e 20 20 54 68 65 20 72 6f  llected.  The ro
20f0e 77 69 64 20 66 6f 72 20 74 68 65 20 70 72 65 61  wid for the prea
20f0f 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a 2a 20  llocated.    ** 
20f10 73 6c 6f 74 20 69 73 20 74 68 65 20 32 6e 64 20  slot is the 2nd 
20f11 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74 61 63  item on the stac
20f12 6b 2e 20 20 54 68 65 20 74 6f 70 20 6f 66 20 74  k.  The top of t
20f13 68 65 20 73 74 61 63 6b 20 69 73 20 74 68 65 0a  he stack is the.
20f14 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65      ** root page
20f15 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62   for the new tab
20f16 6c 65 20 28 6f 72 20 61 20 30 20 69 66 20 74 68  le (or a 0 if th
20f17 69 73 20 69 73 20 61 20 76 69 65 77 29 2e 0a 20  is is a view).. 
20f18 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
20f19 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
20f1a 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41  rse,.      "UPDA
20f1b 54 45 20 25 51 2e 25 73 20 22 0a 20 20 20 20 20  TE %Q.%s ".     
20f1c 20 20 20 20 22 53 45 54 20 74 79 70 65 3d 27 25      "SET type='%
20f1d 73 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c  s', name=%Q, tbl
20f1e 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61  _name=%Q, rootpa
20f1f 67 65 3d 23 25 64 2c 20 73 71 6c 3d 25 51 20 22  ge=#%d, sql=%Q "
20f20 0a 20 20 20 20 20 20 20 22 57 48 45 52 45 20 72  .       "WHERE r
20f21 6f 77 69 64 3d 23 25 64 22 2c 0a 20 20 20 20 20  owid=#%d",.     
20f22 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
20f23 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ame, SCHEMA_TABL
20f24 45 28 69 44 62 29 2c 0a 20 20 20 20 20 20 7a 54  E(iDb),.      zT
20f25 79 70 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e  ype,.      p->zN
20f26 61 6d 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e  ame,.      p->zN
20f27 61 6d 65 2c 0a 20 20 20 20 20 20 70 50 61 72 73  ame,.      pPars
20f28 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a 20 20 20 20  e->regRoot,.    
20f29 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70    zStmt,.      p
20f2a 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a  Parse->regRowid.
20f2b 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74      );.    sqlit
20f2c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74  e3DbFree(db, zSt
20f2d 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  mt);.    sqlite3
20f2e 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
20f2f 72 73 65 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e  rse, iDb);..#ifn
20f30 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
20f31 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
20f32 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
20f33 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20  e if we need to 
20f34 63 72 65 61 74 65 20 61 6e 20 73 71 6c 69 74 65  create an sqlite
20f35 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
20f36 66 6f 72 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69  for.    ** keepi
20f37 6e 67 20 74 72 61 63 6b 20 6f 66 20 61 75 74 6f  ng track of auto
20f38 69 6e 63 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a  increment keys..
20f39 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
20f3a 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
20f3b 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b  Autoincrement ){
20f3c 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62 20 3d  .      Db *pDb =
20f3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a   &db->aDb[iDb];.
20f3e 20 20 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70        if( pDb->p
20f3f 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 3d  Schema->pSeqTab=
20f40 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
20f41 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
20f42 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
20f43 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45     "CREATE TABLE
20f44 20 25 51 2e 73 71 6c 69 74 65 5f 73 65 71 75 65   %Q.sqlite_seque
20f45 6e 63 65 28 6e 61 6d 65 2c 73 65 71 29 22 2c 0a  nce(name,seq)",.
20f46 20 20 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a            pDb->z
20f47 4e 61 6d 65 0a 20 20 20 20 20 20 20 20 29 3b 0a  Name.        );.
20f48 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
20f49 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 52 65 70  ndif..    /* Rep
20f4a 61 72 73 65 20 65 76 65 72 79 74 68 69 6e 67 20  arse everything 
20f4b 74 6f 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e  to update our in
20f4c 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75  ternal data stru
20f4d 63 74 75 72 65 73 20 2a 2f 0a 20 20 20 20 73 71  ctures */.    sq
20f4e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
20f4f 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  v, OP_ParseSchem
20f50 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 0a 20 20  a, iDb, 0, 0,.  
20f51 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72        sqlite3MPr
20f52 69 6e 74 66 28 64 62 2c 20 22 74 62 6c 5f 6e 61  intf(db, "tbl_na
20f53 6d 65 3d 27 25 71 27 22 2c 70 2d 3e 7a 4e 61 6d  me='%q'",p->zNam
20f54 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  e), P4_DYNAMIC);
20f55 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64 20  .  }...  /* Add 
20f56 74 68 65 20 74 61 62 6c 65 20 74 6f 20 74 68 65  the table to the
20f57 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65   in-memory repre
20f58 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
20f59 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a   database..  */.
20f5a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
20f5b 75 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  usy && pParse->n
20f5c 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 54 61  Err==0 ){.    Ta
20f5d 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 46  ble *pOld;.    F
20f5e 4b 65 79 20 2a 70 46 4b 65 79 3b 20 0a 20 20 20  Key *pFKey; .   
20f5f 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
20f60 20 3d 20 70 2d 3e 70 53 63 68 65 6d 61 3b 0a 20   = p->pSchema;. 
20f61 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65     pOld = sqlite
20f62 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 53 63  3HashInsert(&pSc
20f63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70  hema->tblHash, p
20f64 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  ->zName,.       
20f65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f66 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
20f67 6c 65 6e 33 30 28 70 2d 3e 7a 4e 61 6d 65 29 2b  len30(p->zName)+
20f68 31 2c 70 29 3b 0a 20 20 20 20 69 66 28 20 70 4f  1,p);.    if( pO
20f69 6c 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  ld ){.      asse
20f6a 72 74 28 20 70 3d 3d 70 4f 6c 64 20 29 3b 20 20  rt( p==pOld );  
20f6b 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68  /* Malloc must h
20f6c 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64  ave failed insid
20f6d 65 20 48 61 73 68 49 6e 73 65 72 74 28 29 20 2a  e HashInsert() *
20f6e 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  /.      db->mall
20f6f 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
20f70 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
20f71 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
20f72 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
20f73 59 0a 20 20 20 20 66 6f 72 28 70 46 4b 65 79 3d  Y.    for(pFKey=
20f74 70 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b  p->pFKey; pFKey;
20f75 20 70 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e   pFKey=pFKey->pN
20f76 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20  extFrom){.      
20f77 76 6f 69 64 20 2a 64 61 74 61 3b 0a 20 20 20 20  void *data;.    
20f78 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73 71 6c 69    int nTo = sqli
20f79 74 65 33 53 74 72 6c 65 6e 33 30 28 70 46 4b 65  te3Strlen30(pFKe
20f7a 79 2d 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20 20 20  y->zTo) + 1;.   
20f7b 20 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54     pFKey->pNextT
20f7c 6f 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  o = sqlite3HashF
20f7d 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e 61 46  ind(&pSchema->aF
20f7e 4b 65 79 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c  Key, pFKey->zTo,
20f7f 20 6e 54 6f 29 3b 0a 20 20 20 20 20 20 64 61 74   nTo);.      dat
20f80 61 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  a = sqlite3HashI
20f81 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e  nsert(&pSchema->
20f82 61 46 4b 65 79 2c 20 70 46 4b 65 79 2d 3e 7a 54  aFKey, pFKey->zT
20f83 6f 2c 20 6e 54 6f 2c 20 70 46 4b 65 79 29 3b 0a  o, nTo, pFKey);.
20f84 20 20 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d        if( data==
20f85 28 76 6f 69 64 20 2a 29 70 46 4b 65 79 20 29 7b  (void *)pFKey ){
20f86 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  .        db->mal
20f87 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
20f88 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
20f89 64 69 66 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  dif.    pParse->
20f8a 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20  pNewTable = 0;. 
20f8b 20 20 20 64 62 2d 3e 6e 54 61 62 6c 65 2b 2b 3b     db->nTable++;
20f8c 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  .    db->flags |
20f8d 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  = SQLITE_InternC
20f8e 68 61 6e 67 65 73 3b 0a 0a 23 69 66 6e 64 65 66  hanges;..#ifndef
20f8f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54   SQLITE_OMIT_ALT
20f90 45 52 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20  ERTABLE.    if( 
20f91 21 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  !p->pSelect ){. 
20f92 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
20f93 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20  *zName = (const 
20f94 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e 73  char *)pParse->s
20f95 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0a 20 20 20  NameToken.z;.   
20f96 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20     int nName;.  
20f97 20 20 20 20 61 73 73 65 72 74 28 20 21 70 53 65      assert( !pSe
20f98 6c 65 63 74 20 26 26 20 70 43 6f 6e 73 20 26 26  lect && pCons &&
20f99 20 70 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 69   pEnd );.      i
20f9a 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d 30 20 29  f( pCons->z==0 )
20f9b 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6e 73 20  {.        pCons 
20f9c 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  = pEnd;.      }.
20f9d 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20 28 69        nName = (i
20f9e 6e 74 29 28 28 63 6f 6e 73 74 20 63 68 61 72 20  nt)((const char 
20f9f 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d 20 7a 4e 61  *)pCons->z - zNa
20fa0 6d 65 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64  me);.      p->ad
20fa1 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 31 33 20  dColOffset = 13 
20fa2 2b 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61  + sqlite3Utf8Cha
20fa3 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  rLen(zName, nNam
20fa4 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  e);.    }.#endif
20fa5 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
20fa6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
20fa7 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65  ./*.** The parse
20fa8 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
20fa9 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  tine in order to
20faa 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 56 49   create a new VI
20fab 45 57 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  EW.*/.SQLITE_PRI
20fac 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
20fad 33 43 72 65 61 74 65 56 69 65 77 28 0a 20 20 50  3CreateView(.  P
20fae 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
20faf 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
20fb0 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f   context */.  To
20fb1 6b 65 6e 20 2a 70 42 65 67 69 6e 2c 20 20 20 20  ken *pBegin,    
20fb2 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20 74   /* The CREATE t
20fb3 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73  oken that begins
20fb4 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 2a   the statement *
20fb5 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
20fb6 31 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f  1,     /* The to
20fb7 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ken that holds t
20fb8 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76  he name of the v
20fb9 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  iew */.  Token *
20fba 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20 54  pName2,     /* T
20fbb 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f  he token that ho
20fbc 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  lds the name of 
20fbd 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 53 65  the view */.  Se
20fbe 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
20fbf 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61   /* A SELECT sta
20fc0 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c  tement that will
20fc1 20 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20   become the new 
20fc2 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73  view */.  int is
20fc3 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20  Temp,        /* 
20fc4 54 52 55 45 20 66 6f 72 20 61 20 54 45 4d 50 4f  TRUE for a TEMPO
20fc5 52 41 52 59 20 76 69 65 77 20 2a 2f 0a 20 20 69  RARY view */.  i
20fc6 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20  nt noErr        
20fc7 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20 65 72    /* Suppress er
20fc8 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 66 20  ror messages if 
20fc9 56 49 45 57 20 61 6c 72 65 61 64 79 20 65 78 69  VIEW already exi
20fca 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  sts */.){.  Tabl
20fcb 65 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20  e *p;.  int n;. 
20fcc 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
20fcd 63 68 61 72 20 2a 7a 3b 0a 20 20 54 6f 6b 65 6e  char *z;.  Token
20fce 20 73 45 6e 64 3b 0a 20 20 44 62 46 69 78 65 72   sEnd;.  DbFixer
20fcf 20 73 46 69 78 3b 0a 20 20 54 6f 6b 65 6e 20 2a   sFix;.  Token *
20fd0 70 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 69 44 62  pName;.  int iDb
20fd1 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
20fd2 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
20fd3 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61   if( pParse->nVa
20fd4 72 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  r>0 ){.    sqlit
20fd5 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
20fd6 65 2c 20 22 70 61 72 61 6d 65 74 65 72 73 20 61  e, "parameters a
20fd7 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69  re not allowed i
20fd8 6e 20 76 69 65 77 73 22 29 3b 0a 20 20 20 20 73  n views");.    s
20fd9 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
20fda 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
20fdb 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
20fdc 0a 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54  .  sqlite3StartT
20fdd 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61  able(pParse, pNa
20fde 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 69 73 54  me1, pName2, isT
20fdf 65 6d 70 2c 20 31 2c 20 30 2c 20 6e 6f 45 72 72  emp, 1, 0, noErr
20fe0 29 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d  );.  p = pParse-
20fe1 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66  >pNewTable;.  if
20fe2 28 20 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65  ( p==0 || pParse
20fe3 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 73 71  ->nErr ){.    sq
20fe4 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
20fe5 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  e(db, pSelect);.
20fe6 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
20fe7 20 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74    sqlite3TwoPart
20fe8 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
20fe9 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e  me1, pName2, &pN
20fea 61 6d 65 29 3b 0a 20 20 69 44 62 20 3d 20 73 71  ame);.  iDb = sq
20feb 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
20fec 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d  ex(db, p->pSchem
20fed 61 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  a);.  if( sqlite
20fee 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20  3FixInit(&sFix, 
20fef 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 76 69  pParse, iDb, "vi
20ff0 65 77 22 2c 20 70 4e 61 6d 65 29 0a 20 20 20 20  ew", pName).    
20ff1 26 26 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c  && sqlite3FixSel
20ff2 65 63 74 28 26 73 46 69 78 2c 20 70 53 65 6c 65  ect(&sFix, pSele
20ff3 63 74 29 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c  ct).  ){.    sql
20ff4 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
20ff5 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20  (db, pSelect);. 
20ff6 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
20ff7 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79    /* Make a copy
20ff8 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 53   of the entire S
20ff9 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
20ffa 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65  that defines the
20ffb 20 76 69 65 77 2e 0a 20 20 2a 2a 20 54 68 69 73   view..  ** This
20ffc 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 6c 6c 20   will force all 
20ffd 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a  the Expr.token.z
20ffe 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 64 79   values to be dy
20fff 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 61  namically.  ** a
21000 6c 6c 6f 63 61 74 65 64 20 72 61 74 68 65 72 20  llocated rather 
21001 74 68 61 6e 20 70 6f 69 6e 74 20 74 6f 20 74 68  than point to th
21002 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 2d  e input string -
21003 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61   which means tha
21004 74 0a 20 20 2a 2a 20 74 68 65 79 20 77 69 6c 6c  t.  ** they will
21005 20 70 65 72 73 69 73 74 20 61 66 74 65 72 20 74   persist after t
21006 68 65 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74  he current sqlit
21007 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 72  e3_exec() call r
21008 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70  eturns..  */.  p
21009 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  ->pSelect = sqli
2100a 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
2100b 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 73 71 6c   pSelect);.  sql
2100c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2100d 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20  (db, pSelect);. 
2100e 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2100f 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74  ailed ){.    ret
21010 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  urn;.  }.  if( !
21011 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
21012 0a 20 20 20 20 73 71 6c 69 74 65 33 56 69 65 77  .    sqlite3View
21013 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  GetColumnNames(p
21014 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 7d 0a 0a  Parse, p);.  }..
21015 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
21016 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54  end of the CREAT
21017 45 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e 74  E VIEW statement
21018 2e 20 20 4d 61 6b 65 20 73 45 6e 64 20 70 6f 69  .  Make sEnd poi
21019 6e 74 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 65  nt to.  ** the e
2101a 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 45 6e 64 20  nd..  */.  sEnd 
2101b 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54  = pParse->sLastT
2101c 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 73 45 6e 64  oken;.  if( sEnd
2101d 2e 7a 5b 30 5d 21 3d 30 20 26 26 20 73 45 6e 64  .z[0]!=0 && sEnd
2101e 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b 0a 20 20  .z[0]!=';' ){.  
2101f 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 73 45 6e 64    sEnd.z += sEnd
21020 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e 64 2e 6e  .n;.  }.  sEnd.n
21021 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 28 69 6e 74   = 0;.  n = (int
21022 29 28 73 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69  )(sEnd.z - pBegi
21023 6e 2d 3e 7a 29 3b 0a 20 20 7a 20 3d 20 28 63 6f  n->z);.  z = (co
21024 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
21025 72 2a 29 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20  r*)pBegin->z;.  
21026 77 68 69 6c 65 28 20 6e 3e 30 20 26 26 20 28 7a  while( n>0 && (z
21027 5b 6e 2d 31 5d 3d 3d 27 3b 27 20 7c 7c 20 69 73  [n-1]==';' || is
21028 73 70 61 63 65 28 7a 5b 6e 2d 31 5d 29 29 20 29  space(z[n-1])) )
21029 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e  { n--; }.  sEnd.
2102a 7a 20 3d 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73  z = &z[n-1];.  s
2102b 45 6e 64 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a  End.n = 1;..  /*
2102c 20 55 73 65 20 73 71 6c 69 74 65 33 45 6e 64 54   Use sqlite3EndT
2102d 61 62 6c 65 28 29 20 74 6f 20 61 64 64 20 74 68  able() to add th
2102e 65 20 76 69 65 77 20 74 6f 20 74 68 65 20 53 51  e view to the SQ
2102f 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
21030 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 6e  e */.  sqlite3En
21031 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  dTable(pParse, 0
21032 2c 20 26 73 45 6e 64 2c 20 30 29 3b 0a 20 20 72  , &sEnd, 0);.  r
21033 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20  eturn;.}.#endif 
21034 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
21035 49 45 57 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66  IEW */..#if !def
21036 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
21037 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
21038 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
21039 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a  IRTUALTABLE)./*.
2103a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74 72  ** The Table str
2103b 75 63 74 75 72 65 20 70 54 61 62 6c 65 20 69 73  ucture pTable is
2103c 20 72 65 61 6c 6c 79 20 61 20 56 49 45 57 2e 20   really a VIEW. 
2103d 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 6e 61 6d   Fill in the nam
2103e 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c  es of.** the col
2103f 75 6d 6e 73 20 6f 66 20 74 68 65 20 76 69 65 77  umns of the view
21040 20 69 6e 20 74 68 65 20 70 54 61 62 6c 65 20 73   in the pTable s
21041 74 72 75 63 74 75 72 65 2e 20 20 52 65 74 75 72  tructure.  Retur
21042 6e 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20  n the number.** 
21043 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20 61  of errors.  If a
21044 6e 20 65 72 72 6f 72 20 69 73 20 73 65 65 6e 20  n error is seen 
21045 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
21046 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
21047 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51  ->zErrMsg..*/.SQ
21048 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
21049 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
2104a 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65  olumnNames(Parse
2104b 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20   *pParse, Table 
2104c 2a 70 54 61 62 6c 65 29 7b 0a 20 20 54 61 62 6c  *pTable){.  Tabl
2104d 65 20 2a 70 53 65 6c 54 61 62 3b 20 20 20 2f 2a  e *pSelTab;   /*
2104e 20 41 20 66 61 6b 65 20 74 61 62 6c 65 20 66 72   A fake table fr
2104f 6f 6d 20 77 68 69 63 68 20 77 65 20 67 65 74 20  om which we get 
21050 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
21051 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
21052 3b 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66  ;     /* Copy of
21053 20 74 68 65 20 53 45 4c 45 43 54 20 74 68 61 74   the SELECT that
21054 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
21055 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  view */.  int nE
21056 72 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e  rr = 0;     /* N
21057 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
21058 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20  encountered */. 
21059 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
2105a 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c     /* Temporaril
2105b 79 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62  y holds the numb
2105c 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 61 73  er of cursors as
2105d 73 69 67 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69  signed */.  sqli
2105e 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2105f 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61  ->db;  /* Databa
21060 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f  se connection fo
21061 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20  r malloc errors 
21062 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68  */.  int (*xAuth
21063 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
21064 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
21065 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
21066 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a 20  const char*);.. 
21067 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 20   assert( pTable 
21068 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
21069 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2106a 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74  ABLE.  if( sqlit
2106b 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63  e3VtabCallConnec
2106c 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65  t(pParse, pTable
2106d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2106e 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
2106f 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  }.  if( IsVirtua
21070 6c 28 70 54 61 62 6c 65 29 20 29 20 72 65 74 75  l(pTable) ) retu
21071 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69  rn 0;.#endif..#i
21072 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
21073 54 5f 56 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f  T_VIEW.  /* A po
21074 73 69 74 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e  sitive nCol mean
21075 73 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61  s the columns na
21076 6d 65 73 20 66 6f 72 20 74 68 69 73 20 76 69 65  mes for this vie
21077 77 20 61 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61  w are.  ** alrea
21078 64 79 20 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20  dy known..  */. 
21079 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f   if( pTable->nCo
2107a 6c 3e 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  l>0 ) return 0;.
2107b 0a 20 20 2f 2a 20 41 20 6e 65 67 61 74 69 76 65  .  /* A negative
2107c 20 6e 43 6f 6c 20 69 73 20 61 20 73 70 65 63 69   nCol is a speci
2107d 61 6c 20 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e  al marker meanin
2107e 67 20 74 68 61 74 20 77 65 20 61 72 65 20 63 75  g that we are cu
2107f 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79  rrently.  ** try
21080 69 6e 67 20 74 6f 20 63 6f 6d 70 75 74 65 20 74  ing to compute t
21081 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e  he column names.
21082 20 20 49 66 20 77 65 20 65 6e 74 65 72 20 74 68    If we enter th
21083 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 0a  is routine with.
21084 20 20 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20    ** a negative 
21085 6e 43 6f 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74  nCol, it means t
21086 77 6f 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73  wo or more views
21087 20 66 6f 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69   form a loop, li
21088 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20  ke this:.  **.  
21089 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49  **     CREATE VI
2108a 45 57 20 6f 6e 65 20 41 53 20 53 45 4c 45 43 54  EW one AS SELECT
2108b 20 2a 20 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a   * FROM two;.  *
2108c 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49 45  *     CREATE VIE
2108d 57 20 74 77 6f 20 41 53 20 53 45 4c 45 43 54 20  W two AS SELECT 
2108e 2a 20 46 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a  * FROM one;.  **
2108f 0a 20 20 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20  .  ** Actually, 
21090 74 68 69 73 20 65 72 72 6f 72 20 69 73 20 63 61  this error is ca
21091 75 67 68 74 20 70 72 65 76 69 6f 75 73 6c 79 20  ught previously 
21092 61 6e 64 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f  and so the follo
21093 77 69 6e 67 20 74 65 73 74 0a 20 20 2a 2a 20 73  wing test.  ** s
21094 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 66 61 69  hould always fai
21095 6c 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20  l.  But we will 
21096 6c 65 61 76 65 20 69 74 20 69 6e 20 70 6c 61 63  leave it in plac
21097 65 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61 66  e just to be saf
21098 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  e..  */.  if( pT
21099 61 62 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a  able->nCol<0 ){.
2109a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2109b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 65  Msg(pParse, "vie
2109c 77 20 25 73 20 69 73 20 63 69 72 63 75 6c 61 72  w %s is circular
2109d 6c 79 20 64 65 66 69 6e 65 64 22 2c 20 70 54 61  ly defined", pTa
2109e 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  ble->zName);.   
2109f 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
210a0 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d   assert( pTable-
210a1 3e 6e 43 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f  >nCol>=0 );..  /
210a2 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69 73  * If we get this
210a3 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 77   far, it means w
210a4 65 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 75 74  e need to comput
210a5 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  e the table name
210a6 73 2e 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61  s..  ** Note tha
210a7 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71  t the call to sq
210a8 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66  lite3ResultSetOf
210a9 53 65 6c 65 63 74 28 29 20 77 69 6c 6c 20 65 78  Select() will ex
210aa 70 61 6e 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a  pand any.  ** "*
210ab 22 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  " elements in th
210ac 65 20 72 65 73 75 6c 74 73 20 73 65 74 20 6f 66  e results set of
210ad 20 74 68 65 20 76 69 65 77 20 61 6e 64 20 77 69   the view and wi
210ae 6c 6c 20 61 73 73 69 67 6e 20 63 75 72 73 6f 72  ll assign cursor
210af 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c  s.  ** to the el
210b0 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 46 52  ements of the FR
210b1 4f 4d 20 63 6c 61 75 73 65 2e 20 20 42 75 74 20  OM clause.  But 
210b2 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74  we do not want t
210b3 68 65 73 65 20 63 68 61 6e 67 65 73 0a 20 20 2a  hese changes.  *
210b4 2a 20 74 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e  * to be permanen
210b5 74 2e 20 20 53 6f 20 74 68 65 20 63 6f 6d 70 75  t.  So the compu
210b6 74 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f  tation is done o
210b7 6e 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  n a copy of the 
210b8 53 45 4c 45 43 54 0a 20 20 2a 2a 20 73 74 61 74  SELECT.  ** stat
210b9 65 6d 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e  ement that defin
210ba 65 73 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a  es the view..  *
210bb 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  /.  assert( pTab
210bc 6c 65 2d 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20  le->pSelect );. 
210bd 20 70 53 65 6c 20 3d 20 73 71 6c 69 74 65 33 53   pSel = sqlite3S
210be 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54 61  electDup(db, pTa
210bf 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  ble->pSelect);. 
210c0 20 69 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20   if( pSel ){.   
210c1 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61   n = pParse->nTa
210c2 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72  b;.    sqlite3Sr
210c3 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f  cListAssignCurso
210c4 72 73 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d  rs(pParse, pSel-
210c5 3e 70 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62  >pSrc);.    pTab
210c6 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 23  le->nCol = -1;.#
210c7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
210c8 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
210c9 0a 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d  .    xAuth = db-
210ca 3e 78 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e  >xAuth;.    db->
210cb 78 41 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70  xAuth = 0;.    p
210cc 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33  SelTab = sqlite3
210cd 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63  ResultSetOfSelec
210ce 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b  t(pParse, pSel);
210cf 0a 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d  .    db->xAuth =
210d0 20 78 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20   xAuth;.#else.  
210d1 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69    pSelTab = sqli
210d2 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65  te3ResultSetOfSe
210d3 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65  lect(pParse, pSe
210d4 6c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70  l);.#endif.    p
210d5 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b  Parse->nTab = n;
210d6 0a 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 62  .    if( pSelTab
210d7 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
210d8 28 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d  ( pTable->aCol==
210d9 30 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c  0 );.      pTabl
210da 65 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61  e->nCol = pSelTa
210db 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70  b->nCol;.      p
210dc 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53  Table->aCol = pS
210dd 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20  elTab->aCol;.   
210de 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c     pSelTab->nCol
210df 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 53 65 6c   = 0;.      pSel
210e0 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20  Tab->aCol = 0;. 
210e1 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65       sqlite3Dele
210e2 74 65 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29  teTable(pSelTab)
210e3 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e  ;.      pTable->
210e4 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c  pSchema->flags |
210e5 3d 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  = DB_UnresetView
210e6 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  s;.    }else{.  
210e7 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c      pTable->nCol
210e8 20 3d 20 30 3b 0a 20 20 20 20 20 20 6e 45 72 72   = 0;.      nErr
210e9 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ++;.    }.    sq
210ea 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
210eb 65 28 64 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d  e(db, pSel);.  }
210ec 20 65 6c 73 65 20 7b 0a 20 20 20 20 6e 45 72 72   else {.    nErr
210ed 2b 2b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ++;.  }.#endif /
210ee 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
210ef 45 57 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e  EW */.  return n
210f0 45 72 72 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20  Err;  .}.#endif 
210f1 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
210f2 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c  TE_OMIT_VIEW) ||
210f3 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
210f4 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
210f5 4c 45 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  LE) */..#ifndef 
210f6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
210f7 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
210f8 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72   column names fr
210f9 6f 6d 20 65 76 65 72 79 20 56 49 45 57 20 69 6e  om every VIEW in
210fa 20 64 61 74 61 62 61 73 65 20 69 64 78 2e 0a 2a   database idx..*
210fb 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
210fc 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c  liteViewResetAll
210fd 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
210fe 74 20 69 64 78 29 7b 0a 20 20 48 61 73 68 45 6c  t idx){.  HashEl
210ff 65 6d 20 2a 69 3b 0a 20 20 69 66 28 20 21 44 62  em *i;.  if( !Db
21100 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20  HasProperty(db, 
21101 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56  idx, DB_UnresetV
21102 69 65 77 73 29 20 29 20 72 65 74 75 72 6e 3b 0a  iews) ) return;.
21103 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61    for(i=sqliteHa
21104 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 44 62  shFirst(&db->aDb
21105 5b 69 64 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74  [idx].pSchema->t
21106 62 6c 48 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c  blHash); i;i=sql
21107 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b  iteHashNext(i)){
21108 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
21109 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74   = sqliteHashDat
2110a 61 28 69 29 3b 0a 20 20 20 20 69 66 28 20 70 54  a(i);.    if( pT
2110b 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  ab->pSelect ){. 
2110c 20 20 20 20 20 73 71 6c 69 74 65 52 65 73 65 74       sqliteReset
2110d 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62  ColumnNames(pTab
2110e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44  );.    }.  }.  D
2110f 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64  bClearProperty(d
21110 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73  b, idx, DB_Unres
21111 65 74 56 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73  etViews);.}.#els
21112 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  e.# define sqlit
21113 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 41 2c  eViewResetAll(A,
21114 42 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  B).#endif /* SQL
21115 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f  ITE_OMIT_VIEW */
21116 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
21117 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
21118 62 79 20 74 68 65 20 56 44 42 45 20 74 6f 20 61  by the VDBE to a
21119 64 6a 75 73 74 20 74 68 65 20 69 6e 74 65 72 6e  djust the intern
2111a 61 6c 20 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65  al schema.** use
2111b 64 20 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e  d by SQLite when
2111c 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   the btree layer
2111d 20 6d 6f 76 65 73 20 61 20 74 61 62 6c 65 20 72   moves a table r
2111e 6f 6f 74 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a  oot page. The.**
2111f 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 61 20   root-page of a 
21120 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69  table or index i
21121 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 20 68  n database iDb h
21122 61 73 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20  as changed from 
21123 69 46 72 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e  iFrom.** to iTo.
21124 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31  .**.** Ticket #1
21125 37 32 38 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c  728:  The symbol
21126 20 74 61 62 6c 65 20 6d 69 67 68 74 20 73 74 69   table might sti
21127 6c 6c 20 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72  ll contain infor
21128 6d 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62  mation.** on tab
21129 6c 65 73 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63  les and/or indic
2112a 65 73 20 74 68 61 74 20 61 72 65 20 74 68 65 20  es that are the 
2112b 70 72 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67  process of being
2112c 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20   deleted..** If 
2112d 79 6f 75 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c  you are unlucky,
2112e 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 64 65   one of those de
2112f 6c 65 74 65 64 20 69 6e 64 69 63 65 73 20 6f 72  leted indices or
21130 20 74 61 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a   tables might.**
21131 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 72   have the same r
21132 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72 20 61  ootpage number a
21133 73 20 74 68 65 20 72 65 61 6c 20 74 61 62 6c 65  s the real table
21134 20 6f 72 20 69 6e 64 65 78 20 74 68 61 74 20 69   or index that i
21135 73 0a 2a 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64  s.** being moved
21136 2e 20 20 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20  .  So we cannot 
21137 73 74 6f 70 20 73 65 61 72 63 68 69 6e 67 20 61  stop searching a
21138 66 74 65 72 20 74 68 65 20 66 69 72 73 74 20 6d  fter the first m
21139 61 74 63 68 20 0a 2a 2a 20 62 65 63 61 75 73 65  atch .** because
2113a 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68   the first match
2113b 20 6d 69 67 68 74 20 62 65 20 66 6f 72 20 6f 6e   might be for on
2113c 65 20 6f 66 20 74 68 65 20 64 65 6c 65 74 65 64  e of the deleted
2113d 20 69 6e 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74   indices.** or t
2113e 61 62 6c 65 73 20 61 6e 64 20 6e 6f 74 20 74 68  ables and not th
2113f 65 20 74 61 62 6c 65 2f 69 6e 64 65 78 20 74 68  e table/index th
21140 61 74 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62  at is actually b
21141 65 69 6e 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57  eing moved..** W
21142 65 20 6d 75 73 74 20 63 6f 6e 74 69 6e 75 65 20  e must continue 
21143 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c  looping until al
21144 6c 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64  l tables and ind
21145 69 63 65 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f  ices with.** roo
21146 74 70 61 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76  tpage==iFrom hav
21147 65 20 62 65 65 6e 20 63 6f 6e 76 65 72 74 65 64  e been converted
21148 20 74 6f 20 68 61 76 65 20 61 20 72 6f 6f 74 70   to have a rootp
21149 61 67 65 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e  age of iTo.** in
2114a 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63 65 72   order to be cer
2114b 74 61 69 6e 20 74 68 61 74 20 77 65 20 67 6f 74  tain that we got
2114c 20 74 68 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a   the right one..
2114d 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2114e 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
2114f 4d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  M.SQLITE_PRIVATE
21150 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f   void sqlite3Roo
21151 74 50 61 67 65 4d 6f 76 65 64 28 44 62 20 2a 70  tPageMoved(Db *p
21152 44 62 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69  Db, int iFrom, i
21153 6e 74 20 69 54 6f 29 7b 0a 20 20 48 61 73 68 45  nt iTo){.  HashE
21154 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61  lem *pElem;.  Ha
21155 73 68 20 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48  sh *pHash;..  pH
21156 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68  ash = &pDb->pSch
21157 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20  ema->tblHash;.  
21158 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65  for(pElem=sqlite
21159 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29  HashFirst(pHash)
2115a 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73  ; pElem; pElem=s
2115b 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45  qliteHashNext(pE
2115c 6c 65 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65  lem)){.    Table
2115d 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48   *pTab = sqliteH
2115e 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a  ashData(pElem);.
2115f 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 74 6e      if( pTab->tn
21160 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  um==iFrom ){.   
21161 20 20 20 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20     pTab->tnum = 
21162 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  iTo;.    }.  }. 
21163 20 70 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70   pHash = &pDb->p
21164 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b  Schema->idxHash;
21165 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
21166 69 74 65 48 61 73 68 46 69 72 73 74 28 70 48 61  iteHashFirst(pHa
21167 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65  sh); pElem; pEle
21168 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  m=sqliteHashNext
21169 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e  (pElem)){.    In
2116a 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69  dex *pIdx = sqli
2116b 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d  teHashData(pElem
2116c 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 2d  );.    if( pIdx-
2116d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a  >tnum==iFrom ){.
2116e 20 20 20 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d        pIdx->tnum
2116f 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20   = iTo;.    }.  
21170 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
21171 2a 20 57 72 69 74 65 20 63 6f 64 65 20 74 6f 20  * Write code to 
21172 65 72 61 73 65 20 74 68 65 20 74 61 62 6c 65 20  erase the table 
21173 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69  with root-page i
21174 54 61 62 6c 65 20 66 72 6f 6d 20 64 61 74 61 62  Table from datab
21175 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f  ase iDb..** Also
21176 20 77 72 69 74 65 20 63 6f 64 65 20 74 6f 20 6d   write code to m
21177 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74 65  odify the sqlite
21178 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e  _master table an
21179 64 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  d internal schem
2117a 61 0a 2a 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70  a.** if a root-p
2117b 61 67 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 74  age of another t
2117c 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79  able is moved by
2117d 20 74 68 65 20 62 74 72 65 65 2d 6c 61 79 65 72   the btree-layer
2117e 20 77 68 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69   whilst.** erasi
2117f 6e 67 20 69 54 61 62 6c 65 20 28 74 68 69 73 20  ng iTable (this 
21180 63 61 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20  can happen with 
21181 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64  an auto-vacuum d
21182 61 74 61 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74  atabase)..*/ .st
21183 61 74 69 63 20 76 6f 69 64 20 64 65 73 74 72 6f  atic void destro
21184 79 52 6f 6f 74 50 61 67 65 28 50 61 72 73 65 20  yRootPage(Parse 
21185 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 54 61  *pParse, int iTa
21186 62 6c 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20  ble, int iDb){. 
21187 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
21188 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
21189 29 3b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71  );.  int r1 = sq
2118a 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2118b 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
2118c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2118d 4f 50 5f 44 65 73 74 72 6f 79 2c 20 69 54 61 62  OP_Destroy, iTab
2118e 6c 65 2c 20 72 31 2c 20 69 44 62 29 3b 0a 23 69  le, r1, iDb);.#i
2118f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
21190 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f  T_AUTOVACUUM.  /
21191 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 73 74 6f  * OP_Destroy sto
21192 72 65 73 20 61 6e 20 69 6e 20 69 6e 74 65 67 65  res an in intege
21193 72 20 72 31 2e 20 49 66 20 74 68 69 73 20 69 6e  r r1. If this in
21194 74 65 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f  teger.  ** is no
21195 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20  n-zero, then it 
21196 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  is the root page
21197 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62   number of a tab
21198 6c 65 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a  le moved to.  **
21199 20 6c 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65   location iTable
2119a 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
2119b 63 6f 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68  code modifies th
2119c 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
2119d 74 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65  table to.  ** re
2119e 66 6c 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a  flect this..  **
2119f 0a 20 20 2a 2a 20 54 68 65 20 22 23 25 64 22 20  .  ** The "#%d" 
211a0 69 6e 20 74 68 65 20 53 51 4c 20 69 73 20 61 20  in the SQL is a 
211a1 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74  special constant
211a2 20 74 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74   that means what
211a3 65 76 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20  ever value.  ** 
211a4 69 73 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66  is on the top of
211a5 20 74 68 65 20 73 74 61 63 6b 2e 20 20 53 65 65   the stack.  See
211a6 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
211a7 45 78 70 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 73  Expr()..  */.  s
211a8 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
211a9 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  e(pParse, .     
211aa 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 53 45  "UPDATE %Q.%s SE
211ab 54 20 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48  T rootpage=%d WH
211ac 45 52 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74  ERE #%d AND root
211ad 70 61 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20  page=#%d",.     
211ae 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b  pParse->db->aDb[
211af 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45  iDb].zName, SCHE
211b0 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69  MA_TABLE(iDb), i
211b1 54 61 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a  Table, r1, r1);.
211b2 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
211b3 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
211b4 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f  Parse, r1);.}../
211b5 2a 0a 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20  *.** Write VDBE 
211b6 63 6f 64 65 20 74 6f 20 65 72 61 73 65 20 74 61  code to erase ta
211b7 62 6c 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c  ble pTab and all
211b8 20 61 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69   associated indi
211b9 63 65 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20  ces on disk..** 
211ba 43 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20 74  Code to update t
211bb 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
211bc 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65   tables and inte
211bd 72 6e 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69  rnal schema defi
211be 6e 69 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61  nitions.** in ca
211bf 73 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62  se a root-page b
211c0 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74  elonging to anot
211c1 68 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76  her table is mov
211c2 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20  ed by the btree 
211c3 6c 61 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f  layer.** is also
211c4 20 61 64 64 65 64 20 28 74 68 69 73 20 63 61 6e   added (this can
211c5 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20   happen with an 
211c6 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
211c7 62 61 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  base)..*/.static
211c8 20 76 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62   void destroyTab
211c9 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  le(Parse *pParse
211ca 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
211cb 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
211cc 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
211cd 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69  Index *pIdx;.  i
211ce 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
211cf 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
211d0 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
211d1 70 53 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74  pSchema);.  dest
211d2 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72  royRootPage(pPar
211d3 73 65 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  se, pTab->tnum, 
211d4 69 44 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78  iDb);.  for(pIdx
211d5 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
211d6 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
211d7 70 4e 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74  pNext){.    dest
211d8 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72  royRootPage(pPar
211d9 73 65 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  se, pIdx->tnum, 
211da 69 44 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  iDb);.  }.#else.
211db 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61    /* If the data
211dc 62 61 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f  base may be auto
211dd 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20  -vacuum capable 
211de 28 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  (if SQLITE_OMIT_
211df 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20  AUTOVACUUM.  ** 
211e0 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
211e1 20 74 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f   then it is impo
211e2 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50  rtant to call OP
211e3 5f 44 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a  _Destroy on the.
211e4 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69    ** table and i
211e5 6e 64 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20  ndex root-pages 
211e6 69 6e 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69  in order, starti
211e7 6e 67 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65  ng with the nume
211e8 72 69 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61  rically .  ** la
211e9 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20  rgest root-page 
211ea 6e 75 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61  number. This gua
211eb 72 61 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e  rantees that non
211ec 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61  e of the root-pa
211ed 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64  ges.  ** to be d
211ee 65 73 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f  estroyed is relo
211ef 63 61 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c  cated by an earl
211f0 69 65 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20  ier OP_Destroy. 
211f1 69 2e 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a  i.e. if the.  **
211f2 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20   following were 
211f3 63 6f 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  coded:.  **.  **
211f4 20 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a   OP_Destroy 4 0.
211f5 20 20 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50    ** ....  ** OP
211f6 5f 44 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a  _Destroy 5 0.  *
211f7 2a 0a 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20  *.  ** and root 
211f8 70 61 67 65 20 35 20 68 61 70 70 65 6e 65 64 20  page 5 happened 
211f9 74 6f 20 62 65 20 74 68 65 20 6c 61 72 67 65 73  to be the larges
211fa 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62  t root-page numb
211fb 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64  er in the.  ** d
211fc 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f  atabase, then ro
211fd 6f 74 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20  ot page 5 would 
211fe 62 65 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65  be moved to page
211ff 20 34 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20   4 by the .  ** 
21200 22 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22  "OP_Destroy 4 0"
21201 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62   opcode. The sub
21202 73 65 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74  sequent "OP_Dest
21203 72 6f 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68  roy 5 0" would h
21204 69 74 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c  it.  ** a free-l
21205 69 73 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20  ist page..  */. 
21206 20 69 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62   int iTab = pTab
21207 2d 3e 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44  ->tnum;.  int iD
21208 65 73 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20  estroyed = 0;.. 
21209 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20   while( 1 ){.   
2120a 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
2120b 20 20 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d    int iLargest =
2120c 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65   0;..    if( iDe
2120d 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54  stroyed==0 || iT
2120e 61 62 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b  ab<iDestroyed ){
2120f 0a 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20  .      iLargest 
21210 3d 20 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20  = iTab;.    }.  
21211 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
21212 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
21213 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
21214 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 49 64 78  {.      int iIdx
21215 20 3d 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20   = pIdx->tnum;. 
21216 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
21217 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62  x->pSchema==pTab
21218 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20  ->pSchema );.   
21219 20 20 20 69 66 28 20 28 69 44 65 73 74 72 6f 79     if( (iDestroy
2121a 65 64 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69  ed==0 || (iIdx<i
2121b 44 65 73 74 72 6f 79 65 64 29 29 20 26 26 20 69  Destroyed)) && i
2121c 49 64 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a  Idx>iLargest ){.
2121d 20 20 20 20 20 20 20 20 69 4c 61 72 67 65 73 74          iLargest
2121e 20 3d 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d   = iIdx;.      }
2121f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
21220 4c 61 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20  Largest==0 ){.  
21221 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
21222 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
21223 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
21224 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
21225 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
21226 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65  chema);.      de
21227 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50  stroyRootPage(pP
21228 61 72 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20  arse, iLargest, 
21229 69 44 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73  iDb);.      iDes
2122a 74 72 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73  troyed = iLarges
2122b 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  t;.    }.  }.#en
2122c 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  dif.}../*.** Thi
2122d 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
2122e 6c 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f  led to do the wo
2122f 72 6b 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42  rk of a DROP TAB
21230 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  LE statement..**
21231 20 70 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61   pName is the na
21232 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
21233 74 6f 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a  to be dropped..*
21234 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21235 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
21236 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  pTable(Parse *pP
21237 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
21238 4e 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77  Name, int isView
21239 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20  , int noErr){.  
2123a 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56  Table *pTab;.  V
2123b 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65  dbe *v;.  sqlite
2123c 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2123d 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a  db;.  int iDb;..
2123e 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
2123f 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
21240 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
21241 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
21242 6c 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  le;.  }.  assert
21243 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31  ( pName->nSrc==1
21244 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   );.  pTab = sql
21245 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
21246 70 50 61 72 73 65 2c 20 69 73 56 69 65 77 2c 20  pParse, isView, 
21247 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21248 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 61               pNa
21249 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20  me->a[0].zName, 
2124a 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74  pName->a[0].zDat
2124b 61 62 61 73 65 29 3b 0a 0a 20 20 69 66 28 20 70  abase);..  if( p
2124c 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  Tab==0 ){.    if
2124d 28 20 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20  ( noErr ){.     
2124e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65   sqlite3ErrorCle
2124f 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  ar(pParse);.    
21250 7d 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  }.    goto exit_
21251 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  drop_table;.  }.
21252 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
21253 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
21254 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
21255 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
21256 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
21257 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61   );..  /* If pTa
21258 62 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74  b is a virtual t
21259 61 62 6c 65 2c 20 63 61 6c 6c 20 56 69 65 77 47  able, call ViewG
2125a 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20  etColumnNames() 
2125b 74 6f 20 65 6e 73 75 72 65 0a 20 20 2a 2a 20 69  to ensure.  ** i
2125c 74 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  t is initialized
2125d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 73 56  ..  */.  if( IsV
2125e 69 72 74 75 61 6c 28 70 54 61 62 29 20 26 26 20  irtual(pTab) && 
2125f 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
21260 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
21261 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67  , pTab) ){.    g
21262 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
21263 62 6c 65 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  ble;.  }.#ifndef
21264 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
21265 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20  HORIZATION.  {. 
21266 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20     int code;.   
21267 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
21268 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  b = SCHEMA_TABLE
21269 28 69 44 62 29 3b 0a 20 20 20 20 63 6f 6e 73 74  (iDb);.    const
2126a 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d   char *zDb = db-
2126b 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
2126c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2126d 2a 7a 41 72 67 32 20 3d 20 30 3b 0a 20 20 20 20  *zArg2 = 0;.    
2126e 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
2126f 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
21270 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62  ITE_DELETE, zTab
21271 2c 20 30 2c 20 7a 44 62 29 29 7b 0a 20 20 20 20  , 0, zDb)){.    
21272 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
21273 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20  _table;.    }.  
21274 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a    if( isView ){.
21275 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f        if( !OMIT_
21276 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31  TEMPDB && iDb==1
21277 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65   ){.        code
21278 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54   = SQLITE_DROP_T
21279 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20  EMP_VIEW;.      
2127a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
2127b 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
2127c 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a  P_VIEW;.      }.
2127d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2127e 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2127f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 49  .    }else if( I
21280 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
21281 7b 0a 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53  {.      code = S
21282 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c  QLITE_DROP_VTABL
21283 45 3b 0a 20 20 20 20 20 20 7a 41 72 67 32 20 3d  E;.      zArg2 =
21284 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61   pTab->pMod->zNa
21285 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  me;.#endif.    }
21286 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
21287 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
21288 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  iDb==1 ){.      
21289 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
2128a 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b  DROP_TEMP_TABLE;
2128b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2128c 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c        code = SQL
2128d 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a  ITE_DROP_TABLE;.
2128e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2128f 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
21290 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63  hCheck(pParse, c
21291 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ode, pTab->zName
21292 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29 20 29 7b  , zArg2, zDb) ){
21293 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
21294 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20  _drop_table;.   
21295 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
21296 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
21297 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54  se, SQLITE_DELET
21298 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  E, pTab->zName, 
21299 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  0, zDb) ){.     
2129a 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
2129b 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d  table;.    }.  }
2129c 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 73 71  .#endif.  if( sq
2129d 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54  lite3StrNICmp(pT
2129e 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69  ab->zName, "sqli
2129f 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20  te_", 7)==0 ){. 
212a0 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
212a1 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c  sg(pParse, "tabl
212a2 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  e %s may not be 
212a3 64 72 6f 70 70 65 64 22 2c 20 70 54 61 62 2d 3e  dropped", pTab->
212a4 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
212a5 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
212a6 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
212a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
212a8 20 20 2f 2a 20 45 6e 73 75 72 65 20 44 52 4f 50    /* Ensure DROP
212a9 20 54 41 42 4c 45 20 69 73 20 6e 6f 74 20 75 73   TABLE is not us
212aa 65 64 20 6f 6e 20 61 20 76 69 65 77 2c 20 61 6e  ed on a view, an
212ab 64 20 44 52 4f 50 20 56 49 45 57 20 69 73 20 6e  d DROP VIEW is n
212ac 6f 74 20 75 73 65 64 0a 20 20 2a 2a 20 6f 6e 20  ot used.  ** on 
212ad 61 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  a table..  */.  
212ae 69 66 28 20 69 73 56 69 65 77 20 26 26 20 70 54  if( isView && pT
212af 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29  ab->pSelect==0 )
212b0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
212b1 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75  orMsg(pParse, "u
212b2 73 65 20 44 52 4f 50 20 54 41 42 4c 45 20 74 6f  se DROP TABLE to
212b3 20 64 65 6c 65 74 65 20 74 61 62 6c 65 20 25 73   delete table %s
212b4 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
212b5 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
212b6 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20  rop_table;.  }. 
212b7 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26 20   if( !isView && 
212b8 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pTab->pSelect ){
212b9 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
212ba 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 73  rMsg(pParse, "us
212bb 65 20 44 52 4f 50 20 56 49 45 57 20 74 6f 20 64  e DROP VIEW to d
212bc 65 6c 65 74 65 20 76 69 65 77 20 25 73 22 2c 20  elete view %s", 
212bd 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
212be 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
212bf 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64  _table;.  }.#end
212c0 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  if..  /* Generat
212c1 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65  e code to remove
212c2 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20   the table from 
212c3 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65  the master table
212c4 0a 20 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e 0a 20  .  ** on disk.. 
212c5 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
212c6 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
212c7 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
212c8 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
212c9 65 72 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20  er;.    Db *pDb 
212ca 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
212cb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
212cc 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
212cd 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b  pParse, 1, iDb);
212ce 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
212cf 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
212d0 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56 69 72  LE.    if( IsVir
212d1 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
212d2 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20      if( v ){.   
212d3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
212d4 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42 65  AddOp0(v, OP_VBe
212d5 67 69 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  gin);.      }.  
212d6 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
212d7 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 74 72 69 67  /* Drop all trig
212d8 67 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20  gers associated 
212d9 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 62  with the table b
212da 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 43 6f  eing dropped. Co
212db 64 65 0a 20 20 20 20 2a 2a 20 69 73 20 67 65 6e  de.    ** is gen
212dc 65 72 61 74 65 64 20 74 6f 20 72 65 6d 6f 76 65  erated to remove
212dd 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 73 71   entries from sq
212de 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 2f  lite_master and/
212df 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  or.    ** sqlite
212e0 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 69 66 20  _temp_master if 
212e1 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f  required..    */
212e2 0a 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20  .    pTrigger = 
212e3 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0a  pTab->pTrigger;.
212e4 20 20 20 20 77 68 69 6c 65 28 20 70 54 72 69 67      while( pTrig
212e5 67 65 72 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ger ){.      ass
212e6 65 72 74 28 20 70 54 72 69 67 67 65 72 2d 3e 70  ert( pTrigger->p
212e7 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
212e8 63 68 65 6d 61 20 7c 7c 20 0a 20 20 20 20 20 20  chema || .      
212e9 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53      pTrigger->pS
212ea 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31  chema==db->aDb[1
212eb 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20  ].pSchema );.   
212ec 20 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72     sqlite3DropTr
212ed 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65 2c  iggerPtr(pParse,
212ee 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20   pTrigger);.    
212ef 20 20 70 54 72 69 67 67 65 72 20 3d 20 70 54 72    pTrigger = pTr
212f0 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20  igger->pNext;.  
212f1 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
212f2 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
212f3 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 52 65  REMENT.    /* Re
212f4 6d 6f 76 65 20 61 6e 79 20 65 6e 74 72 69 65 73  move any entries
212f5 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73   of the sqlite_s
212f6 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 61 73  equence table as
212f7 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 20 20  sociated with.  
212f8 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62    ** the table b
212f9 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 54 68  eing dropped. Th
212fa 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66 6f 72  is is done befor
212fb 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 64  e the table is d
212fc 72 6f 70 70 65 64 0a 20 20 20 20 2a 2a 20 61 74  ropped.    ** at
212fd 20 74 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c   the btree level
212fe 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 71  , in case the sq
212ff 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
21300 62 6c 65 20 6e 65 65 64 73 20 74 6f 0a 20 20 20  ble needs to.   
21301 20 2a 2a 20 6d 6f 76 65 20 61 73 20 61 20 72 65   ** move as a re
21302 73 75 6c 74 20 6f 66 20 74 68 65 20 64 72 6f 70  sult of the drop
21303 20 28 63 61 6e 20 68 61 70 70 65 6e 20 69 6e 20   (can happen in 
21304 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65  auto-vacuum mode
21305 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  )..    */.    if
21306 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73  ( pTab->tabFlags
21307 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d   & TF_Autoincrem
21308 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ent ){.      sql
21309 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
2130a 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
2130b 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 73 2e  "DELETE FROM %s.
2130c 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
2130d 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a  WHERE name=%Q",.
2130e 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61          pDb->zNa
2130f 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a  me, pTab->zName.
21310 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 23        );.    }.#
21311 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 72  endif..    /* Dr
21312 6f 70 20 61 6c 6c 20 53 51 4c 49 54 45 5f 4d 41  op all SQLITE_MA
21313 53 54 45 52 20 74 61 62 6c 65 20 61 6e 64 20 69  STER table and i
21314 6e 64 65 78 20 65 6e 74 72 69 65 73 20 74 68 61  ndex entries tha
21315 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 0a 20  t refer to the. 
21316 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65     ** table. The
21317 20 70 72 6f 67 72 61 6d 20 6e 61 6d 65 20 6c 6f   program name lo
21318 6f 70 73 20 74 68 72 6f 75 67 68 20 74 68 65 20  ops through the 
21319 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64  master table and
2131a 20 64 65 6c 65 74 65 73 0a 20 20 20 20 2a 2a 20   deletes.    ** 
2131b 65 76 65 72 79 20 72 6f 77 20 74 68 61 74 20 72  every row that r
2131c 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65  efers to a table
2131d 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d   of the same nam
2131e 65 20 61 73 20 74 68 65 20 6f 6e 65 20 62 65 69  e as the one bei
2131f 6e 67 0a 20 20 20 20 2a 2a 20 64 72 6f 70 70 65  ng.    ** droppe
21320 64 2e 20 54 72 69 67 67 65 72 73 20 61 72 65 20  d. Triggers are 
21321 68 61 6e 64 6c 65 64 20 73 65 70 65 72 61 74 65  handled seperate
21322 6c 79 20 62 65 63 61 75 73 65 20 61 20 74 72 69  ly because a tri
21323 67 67 65 72 20 63 61 6e 20 62 65 0a 20 20 20 20  gger can be.    
21324 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 20 74 68  ** created in th
21325 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 20  e temp database 
21326 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 61  that refers to a
21327 20 74 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65   table in anothe
21328 72 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73  r.    ** databas
21329 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  e..    */.    sq
2132a 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2132b 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
2132c 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
2132d 51 2e 25 73 20 57 48 45 52 45 20 74 62 6c 5f 6e  Q.%s WHERE tbl_n
2132e 61 6d 65 3d 25 51 20 61 6e 64 20 74 79 70 65 21  ame=%Q and type!
2132f 3d 27 74 72 69 67 67 65 72 27 22 2c 0a 20 20 20  ='trigger'",.   
21330 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c       pDb->zName,
21331 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
21332 62 29 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  b), pTab->zName)
21333 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61  ;..    /* Drop a
21334 6e 79 20 73 74 61 74 69 73 74 69 63 73 20 66 72  ny statistics fr
21335 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  om the sqlite_st
21336 61 74 31 20 74 61 62 6c 65 2c 20 69 66 20 69 74  at1 table, if it
21337 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 69   exists */.    i
21338 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61  f( sqlite3FindTa
21339 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f  ble(db, "sqlite_
2133a 73 74 61 74 31 22 2c 20 64 62 2d 3e 61 44 62 5b  stat1", db->aDb[
2133b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20  iDb].zName) ){. 
2133c 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74       sqlite3Nest
2133d 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
2133e 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20          "DELETE 
2133f 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73  FROM %Q.sqlite_s
21340 74 61 74 31 20 57 48 45 52 45 20 74 62 6c 3d 25  tat1 WHERE tbl=%
21341 51 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20  Q", pDb->zName, 
21342 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20  pTab->zName.    
21343 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20    );.    }..    
21344 69 66 28 20 21 69 73 56 69 65 77 20 26 26 20 21  if( !isView && !
21345 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
21346 29 7b 0a 20 20 20 20 20 20 64 65 73 74 72 6f 79  ){.      destroy
21347 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
21348 61 62 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ab);.    }..    
21349 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 74 61  /* Remove the ta
2134a 62 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 53  ble entry from S
2134b 51 4c 69 74 65 27 73 20 69 6e 74 65 72 6e 61 6c  QLite's internal
2134c 20 73 63 68 65 6d 61 20 61 6e 64 20 6d 6f 64 69   schema and modi
2134d 66 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63  fy.    ** the sc
2134e 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 20 20 20  hema cookie..   
2134f 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 73 56 69   */.    if( IsVi
21350 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
21351 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21352 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 44 65  AddOp4(v, OP_VDe
21353 73 74 72 6f 79 2c 20 69 44 62 2c 20 30 2c 20 30  stroy, iDb, 0, 0
21354 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30  , pTab->zName, 0
21355 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
21356 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
21357 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20  , OP_DropTable, 
21358 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d  iDb, 0, 0, pTab-
21359 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
2135a 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
2135b 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  kie(pParse, iDb)
2135c 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 56 69  ;.  }.  sqliteVi
2135d 65 77 52 65 73 65 74 41 6c 6c 28 64 62 2c 20 69  ewResetAll(db, i
2135e 44 62 29 3b 0a 0a 65 78 69 74 5f 64 72 6f 70 5f  Db);..exit_drop_
2135f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65 33  table:.  sqlite3
21360 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
21361 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a  , pName);.}../*.
21362 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
21363 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65  is called to cre
21364 61 74 65 20 61 20 6e 65 77 20 66 6f 72 65 69 67  ate a new foreig
21365 6e 20 6b 65 79 20 6f 6e 20 74 68 65 20 74 61 62  n key on the tab
21366 6c 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  le.** currently 
21367 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
21368 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f 6c 20 64 65  on.  pFromCol de
21369 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20 63  termines which c
2136a 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65  olumns.** in the
2136b 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 20 70   current table p
2136c 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 6f 72 65  oint to the fore
2136d 69 67 6e 20 6b 65 79 2e 20 20 49 66 20 70 46 72  ign key.  If pFr
2136e 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65 6e 0a 2a 2a  omCol==0 then.**
2136f 20 63 6f 6e 6e 65 63 74 20 74 68 65 20 6b 65 79   connect the key
21370 20 74 6f 20 74 68 65 20 6c 61 73 74 20 63 6f 6c   to the last col
21371 75 6d 6e 20 69 6e 73 65 72 74 65 64 2e 20 20 70  umn inserted.  p
21372 54 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  To is the name o
21373 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 72  f.** the table r
21374 65 66 65 72 72 65 64 20 74 6f 2e 20 20 70 54 6f  eferred to.  pTo
21375 43 6f 6c 20 69 73 20 61 20 6c 69 73 74 20 6f 66  Col is a list of
21376 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6f   tables in the o
21377 74 68 65 72 0a 2a 2a 20 70 54 6f 20 74 61 62 6c  ther.** pTo tabl
21378 65 20 74 68 61 74 20 74 68 65 20 66 6f 72 65 69  e that the forei
21379 67 6e 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f  gn key points to
2137a 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74 61 69 6e  .  flags contain
2137b 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61  s all.** informa
2137c 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63  tion about the c
2137d 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69  onflict resoluti
2137e 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 73 70  on algorithms sp
2137f 65 63 69 66 69 65 64 0a 2a 2a 20 69 6e 20 74 68  ecified.** in th
21380 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20 4f 4e 20  e ON DELETE, ON 
21381 55 50 44 41 54 45 20 61 6e 64 20 4f 4e 20 49 4e  UPDATE and ON IN
21382 53 45 52 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a  SERT clauses..**
21383 0a 2a 2a 20 41 6e 20 46 4b 65 79 20 73 74 72 75  .** An FKey stru
21384 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64  cture is created
21385 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68   and added to th
21386 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c  e table currentl
21387 79 0a 2a 2a 20 75 6e 64 65 72 20 63 6f 6e 73 74  y.** under const
21388 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 70  ruction in the p
21389 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2138a 20 66 69 65 6c 64 2e 20 20 54 68 65 20 6e 65 77   field.  The new
2138b 20 46 4b 65 79 0a 2a 2a 20 69 73 20 6e 6f 74 20   FKey.** is not 
2138c 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 64 62 2d 3e  linked into db->
2138d 61 46 4b 65 79 20 61 74 20 74 68 69 73 20 70 6f  aFKey at this po
2138e 69 6e 74 20 2d 20 74 68 61 74 20 64 6f 65 73 20  int - that does 
2138f 6e 6f 74 20 68 61 70 70 65 6e 0a 2a 2a 20 75 6e  not happen.** un
21390 74 69 6c 20 73 71 6c 69 74 65 33 45 6e 64 54 61  til sqlite3EndTa
21391 62 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ble()..**.** The
21392 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20   foreign key is 
21393 73 65 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 54  set for IMMEDIAT
21394 45 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 41  E processing.  A
21395 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
21396 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 44 65  .** to sqlite3De
21397 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 29 20  ferForeignKey() 
21398 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 69  might change thi
21399 73 20 74 6f 20 44 45 46 45 52 52 45 44 2e 0a 2a  s to DEFERRED..*
2139a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2139b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65   void sqlite3Cre
2139c 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 0a 20  ateForeignKey(. 
2139d 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2139e 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2139f 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
213a0 70 72 4c 69 73 74 20 2a 70 46 72 6f 6d 43 6f 6c  prList *pFromCol
213a1 2c 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e  ,  /* Columns in
213a2 20 74 68 69 73 20 74 61 62 6c 65 20 74 68 61 74   this table that
213a3 20 70 6f 69 6e 74 20 74 6f 20 6f 74 68 65 72 20   point to other 
213a4 74 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e  table */.  Token
213a5 20 2a 70 54 6f 2c 20 20 20 20 20 20 20 20 20 20   *pTo,          
213a6 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f  /* Name of the o
213a7 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ther table */.  
213a8 45 78 70 72 4c 69 73 74 20 2a 70 54 6f 43 6f 6c  ExprList *pToCol
213a9 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20  ,    /* Columns 
213aa 69 6e 20 74 68 65 20 6f 74 68 65 72 20 74 61 62  in the other tab
213ab 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  le */.  int flag
213ac 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s            /* 
213ad 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74  Conflict resolut
213ae 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 2e 20  ion algorithms. 
213af 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
213b0 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
213b1 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
213b2 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
213b3 59 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 20  Y.  FKey *pFKey 
213b4 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 20  = 0;.  Table *p 
213b5 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
213b6 62 6c 65 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65  ble;.  int nByte
213b7 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
213b8 20 6e 43 6f 6c 3b 0a 20 20 63 68 61 72 20 2a 7a   nCol;.  char *z
213b9 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  ;..  assert( pTo
213ba 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d  !=0 );.  if( p==
213bb 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72  0 || pParse->nEr
213bc 72 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f  r || IN_DECLARE_
213bd 56 54 41 42 20 29 20 67 6f 74 6f 20 66 6b 5f 65  VTAB ) goto fk_e
213be 6e 64 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 43  nd;.  if( pFromC
213bf 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ol==0 ){.    int
213c0 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43 6f 6c 2d   iCol = p->nCol-
213c1 31 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  1;.    if( iCol<
213c2 30 20 29 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b  0 ) goto fk_end;
213c3 0a 20 20 20 20 69 66 28 20 70 54 6f 43 6f 6c 20  .    if( pToCol 
213c4 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72  && pToCol->nExpr
213c5 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=1 ){.      sql
213c6 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
213c7 72 73 65 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65  rse, "foreign ke
213c8 79 20 6f 6e 20 25 73 22 0a 20 20 20 20 20 20 20  y on %s".       
213c9 20 20 22 20 73 68 6f 75 6c 64 20 72 65 66 65 72    " should refer
213ca 65 6e 63 65 20 6f 6e 6c 79 20 6f 6e 65 20 63 6f  ence only one co
213cb 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 25 54  lumn of table %T
213cc 22 2c 0a 20 20 20 20 20 20 20 20 20 70 2d 3e 61  ",.         p->a
213cd 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c  Col[iCol].zName,
213ce 20 70 54 6f 29 3b 0a 20 20 20 20 20 20 67 6f 74   pTo);.      got
213cf 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a  o fk_end;.    }.
213d0 20 20 20 20 6e 43 6f 6c 20 3d 20 31 3b 0a 20 20      nCol = 1;.  
213d1 7d 65 6c 73 65 20 69 66 28 20 70 54 6f 43 6f 6c  }else if( pToCol
213d2 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70   && pToCol->nExp
213d3 72 21 3d 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78  r!=pFromCol->nEx
213d4 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  pr ){.    sqlite
213d5 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
213d6 2c 0a 20 20 20 20 20 20 20 20 22 6e 75 6d 62 65  ,.        "numbe
213d7 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
213d8 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f 65 73  foreign key does
213d9 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e   not match the n
213da 75 6d 62 65 72 20 6f 66 20 22 0a 20 20 20 20 20  umber of ".     
213db 20 20 20 22 63 6f 6c 75 6d 6e 73 20 69 6e 20 74     "columns in t
213dc 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 74 61  he referenced ta
213dd 62 6c 65 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  ble");.    goto 
213de 66 6b 5f 65 6e 64 3b 0a 20 20 7d 65 6c 73 65 7b  fk_end;.  }else{
213df 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 46 72 6f  .    nCol = pFro
213e0 6d 43 6f 6c 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d  mCol->nExpr;.  }
213e1 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f  .  nByte = sizeo
213e2 66 28 2a 70 46 4b 65 79 29 20 2b 20 6e 43 6f 6c  f(*pFKey) + nCol
213e3 2a 73 69 7a 65 6f 66 28 70 46 4b 65 79 2d 3e 61  *sizeof(pFKey->a
213e4 43 6f 6c 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e 6e  Col[0]) + pTo->n
213e5 20 2b 20 31 3b 0a 20 20 69 66 28 20 70 54 6f 43   + 1;.  if( pToC
213e6 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ol ){.    for(i=
213e7 30 3b 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45 78  0; i<pToCol->nEx
213e8 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
213e9 6e 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33  nByte += sqlite3
213ea 53 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d  Strlen30(pToCol-
213eb 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31  >a[i].zName) + 1
213ec 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46  ;.    }.  }.  pF
213ed 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Key = sqlite3DbM
213ee 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42  allocZero(db, nB
213ef 79 74 65 20 29 3b 0a 20 20 69 66 28 20 70 46 4b  yte );.  if( pFK
213f0 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74  ey==0 ){.    got
213f1 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20  o fk_end;.  }.  
213f2 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70  pFKey->pFrom = p
213f3 3b 0a 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74  ;.  pFKey->pNext
213f4 46 72 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b  From = p->pFKey;
213f5 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 70  .  z = (char*)&p
213f6 46 4b 65 79 5b 31 5d 3b 0a 20 20 70 46 4b 65 79  FKey[1];.  pFKey
213f7 2d 3e 61 43 6f 6c 20 3d 20 28 73 74 72 75 63 74  ->aCol = (struct
213f8 20 73 43 6f 6c 4d 61 70 2a 29 7a 3b 0a 20 20 7a   sColMap*)z;.  z
213f9 20 2b 3d 20 73 69 7a 65 6f 66 28 73 74 72 75 63   += sizeof(struc
213fa 74 20 73 43 6f 6c 4d 61 70 29 2a 6e 43 6f 6c 3b  t sColMap)*nCol;
213fb 0a 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20  .  pFKey->zTo = 
213fc 7a 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70  z;.  memcpy(z, p
213fd 54 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a  To->z, pTo->n);.
213fe 20 20 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b    z[pTo->n] = 0;
213ff 0a 20 20 7a 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31  .  z += pTo->n+1
21400 3b 0a 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74  ;.  pFKey->pNext
21401 54 6f 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d  To = 0;.  pFKey-
21402 3e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20  >nCol = nCol;.  
21403 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20  if( pFromCol==0 
21404 29 7b 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 43  ){.    pFKey->aC
21405 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d  ol[0].iFrom = p-
21406 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65  >nCol-1;.  }else
21407 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
21408 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
21409 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
2140a 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43  for(j=0; j<p->nC
2140b 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; j++){.      
2140c 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2140d 49 43 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e  ICmp(p->aCol[j].
2140e 7a 4e 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d  zName, pFromCol-
2140f 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20  >a[i].zName)==0 
21410 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b  ){.          pFK
21411 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f  ey->aCol[i].iFro
21412 6d 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20  m = j;.         
21413 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
21414 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
21415 69 66 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29  if( j>=p->nCol )
21416 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21417 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
21418 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e  , .          "un
21419 6b 6e 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25  known column \"%
2141a 73 5c 22 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b  s\" in foreign k
2141b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20  ey definition", 
2141c 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d  .          pFrom
2141d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  Col->a[i].zName)
2141e 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66  ;.        goto f
2141f 6b 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20  k_end;.      }. 
21420 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
21421 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72  ToCol ){.    for
21422 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
21423 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20  +){.      int n 
21424 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
21425 30 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a  0(pToCol->a[i].z
21426 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 46 4b  Name);.      pFK
21427 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c  ey->aCol[i].zCol
21428 20 3d 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d 63   = z;.      memc
21429 70 79 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61 5b  py(z, pToCol->a[
2142a 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20  i].zName, n);.  
2142b 20 20 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20      z[n] = 0;.  
2142c 20 20 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20 20      z += n+1;.  
2142d 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d    }.  }.  pFKey-
2142e 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20 30 3b  >isDeferred = 0;
2142f 0a 20 20 70 46 4b 65 79 2d 3e 64 65 6c 65 74 65  .  pFKey->delete
21430 43 6f 6e 66 20 3d 20 28 75 38 29 28 66 6c 61 67  Conf = (u8)(flag
21431 73 20 26 20 30 78 66 66 29 3b 0a 20 20 70 46 4b  s & 0xff);.  pFK
21432 65 79 2d 3e 75 70 64 61 74 65 43 6f 6e 66 20 3d  ey->updateConf =
21433 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e 3e 20   (u8)((flags >> 
21434 38 20 29 20 26 20 30 78 66 66 29 3b 0a 20 20 70  8 ) & 0xff);.  p
21435 46 4b 65 79 2d 3e 69 6e 73 65 72 74 43 6f 6e 66  FKey->insertConf
21436 20 3d 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e   = (u8)((flags >
21437 3e 20 31 36 20 29 20 26 20 30 78 66 66 29 3b 0a  > 16 ) & 0xff);.
21438 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 66  .  /* Link the f
21439 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20 74 68  oreign key to th
2143a 65 20 74 61 62 6c 65 20 61 73 20 74 68 65 20 6c  e table as the l
2143b 61 73 74 20 73 74 65 70 2e 0a 20 20 2a 2f 0a 20  ast step..  */. 
2143c 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70 46 4b 65   p->pFKey = pFKe
2143d 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20 30 3b 0a  y;.  pFKey = 0;.
2143e 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74  .fk_end:.  sqlit
2143f 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b  e3DbFree(db, pFK
21440 65 79 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 21  ey);.#endif /* !
21441 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
21442 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29  MIT_FOREIGN_KEY)
21443 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70   */.  sqlite3Exp
21444 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
21445 70 46 72 6f 6d 43 6f 6c 29 3b 0a 20 20 73 71 6c  pFromCol);.  sql
21446 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
21447 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c 29 3b 0a  te(db, pToCol);.
21448 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
21449 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
2144a 77 68 65 6e 20 61 6e 20 49 4e 49 54 49 41 4c 4c  when an INITIALL
2144b 59 20 49 4d 4d 45 44 49 41 54 45 20 6f 72 20 49  Y IMMEDIATE or I
2144c 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45  NITIALLY DEFERRE
2144d 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 73  D.** clause is s
2144e 65 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20 61  een as part of a
2144f 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66   foreign key def
21450 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20 69 73  inition.  The is
21451 44 65 66 65 72 72 65 64 0a 2a 2a 20 70 61 72 61  Deferred.** para
21452 6d 65 74 65 72 20 69 73 20 31 20 66 6f 72 20 49  meter is 1 for I
21453 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45  NITIALLY DEFERRE
21454 44 20 61 6e 64 20 30 20 66 6f 72 20 49 4e 49 54  D and 0 for INIT
21455 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 2e  IALLY IMMEDIATE.
21456 0a 2a 2a 20 54 68 65 20 62 65 68 61 76 69 6f 72  .** The behavior
21457 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
21458 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 66 6f  ently created fo
21459 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 64 6a  reign key is adj
2145a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f 72 64 69  usted.** accordi
2145b 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ngly..*/.SQLITE_
2145c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2145d 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
2145e 4b 65 79 28 50 61 72 73 65 20 2a 70 50 61 72 73  Key(Parse *pPars
2145f 65 2c 20 69 6e 74 20 69 73 44 65 66 65 72 72 65  e, int isDeferre
21460 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  d){.#ifndef SQLI
21461 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
21462 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  KEY.  Table *pTa
21463 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79  b;.  FKey *pFKey
21464 3b 0a 20 20 69 66 28 20 28 70 54 61 62 20 3d 20  ;.  if( (pTab = 
21465 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
21466 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 79 20  e)==0 || (pFKey 
21467 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29 3d 3d  = pTab->pFKey)==
21468 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  0 ) return;.  as
21469 73 65 72 74 28 20 69 73 44 65 66 65 72 72 65 64  sert( isDeferred
2146a 3d 3d 30 20 7c 7c 20 69 73 44 65 66 65 72 72 65  ==0 || isDeferre
2146b 64 3d 3d 31 20 29 3b 0a 20 20 70 46 4b 65 79 2d  d==1 );.  pFKey-
2146c 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20 28 75  >isDeferred = (u
2146d 38 29 69 73 44 65 66 65 72 72 65 64 3b 0a 23 65  8)isDeferred;.#e
2146e 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ndif.}../*.** Ge
2146f 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
21470 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e 64 20   will erase and 
21471 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70 49  refill index *pI
21472 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20  dx.  This is.** 
21473 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69  used to initiali
21474 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65 61 74  ze a newly creat
21475 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f 20 72  ed index or to r
21476 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20  ecompute the.** 
21477 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 69 6e  content of an in
21478 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20  dex in response 
21479 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63 6f 6d  to a REINDEX com
2147a 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 20 6d  mand..**.** if m
2147b 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20 6e 6f  emRootPage is no
2147c 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74 20 6d  t negative, it m
2147d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 69 6e  eans that the in
2147e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a 2a 20  dex is newly.** 
2147f 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 72 65  created.  The re
21480 67 69 73 74 65 72 20 73 70 65 63 69 66 69 65 64  gister specified
21481 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20   by memRootPage 
21482 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20  contains the.** 
21483 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
21484 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20   of the index.  
21485 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69  If memRootPage i
21486 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e  s negative, then
21487 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 61 6c  .** the index al
21488 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e 64  ready exists and
21489 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72 65 64   must be cleared
2148a 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 72 65   before being re
2148b 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 74 68  filled and.** th
2148c 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  e root page numb
2148d 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  er of the index 
2148e 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 70 49  is taken from pI
2148f 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f 0a 73  ndex->tnum..*/.s
21490 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
21491 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 50 61  e3RefillIndex(Pa
21492 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e 64  rse *pParse, Ind
21493 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e 74 20  ex *pIndex, int 
21494 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a 20 20  memRootPage){.  
21495 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49  Table *pTab = pI
21496 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20 20 2f  ndex->pTable;  /
21497 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74  * The table that
21498 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20   is indexed */. 
21499 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72   int iTab = pPar
2149a 73 65 2d 3e 6e 54 61 62 3b 20 20 20 20 20 20 20  se->nTab;       
2149b 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72 20  /* Btree cursor 
2149c 75 73 65 64 20 66 6f 72 20 70 54 61 62 20 2a 2f  used for pTab */
2149d 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70 50  .  int iIdx = pP
2149e 61 72 73 65 2d 3e 6e 54 61 62 2b 31 3b 20 20 20  arse->nTab+1;   
2149f 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f    /* Btree curso
214a0 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e 64 65  r used for pInde
214a1 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 31  x */.  int addr1
214a2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
214a3 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
214a4 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20   of top of loop 
214a5 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20  */.  int tnum;  
214a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214a7 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65      /* Root page
214a8 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20 56   of index */.  V
214a9 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
214aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
214ab 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69   Generate code i
214ac 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c  nto this virtual
214ad 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 4b 65   machine */.  Ke
214ae 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20  yInfo *pKey;    
214af 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
214b0 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64 65  KeyInfo for inde
214b1 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 64  x */.  int regId
214b2 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  xKey;           
214b3 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
214b4 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  rs containing th
214b5 65 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20  e index key */. 
214b6 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 20   int regRecord; 
214b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214b8 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
214b9 69 6e 67 20 61 73 73 65 6d 62 6c 69 65 64 20 69  ing assemblied i
214ba 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20  ndex record */. 
214bb 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
214bc 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20  Parse->db;      
214bd 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
214be 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
214bf 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65  int iDb = sqlite
214c0 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
214c1 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65  b, pIndex->pSche
214c2 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  ma);..#ifndef SQ
214c3 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
214c4 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71  IZATION.  if( sq
214c5 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
214c6 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 52 45  Parse, SQLITE_RE
214c7 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e 7a  INDEX, pIndex->z
214c8 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 64  Name, 0,.      d
214c9 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
214ca 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  e ) ){.    retur
214cb 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  n;.  }.#endif.. 
214cc 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20 77 72   /* Require a wr
214cd 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ite-lock on the 
214ce 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f 72 6d  table to perform
214cf 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
214d0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c  */.  sqlite3Tabl
214d1 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44  eLock(pParse, iD
214d2 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 31  b, pTab->tnum, 1
214d3 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
214d4 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
214d5 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
214d6 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
214d7 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f  rn;.  if( memRoo
214d8 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20  tPage>=0 ){.    
214d9 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50 61  tnum = memRootPa
214da 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ge;.  }else{.   
214db 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d 3e   tnum = pIndex->
214dc 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  tnum;.    sqlite
214dd 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
214de 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d 2c 20 69  P_Clear, tnum, i
214df 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b 65 79 20  Db);.  }.  pKey 
214e0 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65  = sqlite3IndexKe
214e1 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49  yinfo(pParse, pI
214e2 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33  ndex);.  sqlite3
214e3 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
214e4 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 64 78  _OpenWrite, iIdx
214e5 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a 20 20  , tnum, iDb, .  
214e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214e7 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c 20    (char *)pKey, 
214e8 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
214e9 46 46 29 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f  FF);.  if( memRo
214ea 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20  otPage>=0 ){.   
214eb 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
214ec 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 7d 0a  geP5(v, 1);.  }.
214ed 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
214ee 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c  le(pParse, iTab,
214ef 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f   iDb, pTab, OP_O
214f0 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64 64 72  penRead);.  addr
214f1 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
214f2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp2(v, OP_Rewi
214f3 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20  nd, iTab, 0);.  
214f4 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69  regRecord = sqli
214f5 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
214f6 61 72 73 65 29 3b 0a 20 20 72 65 67 49 64 78 4b  arse);.  regIdxK
214f7 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65 6e 65  ey = sqlite3Gene
214f8 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61  rateIndexKey(pPa
214f9 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 54 61  rse, pIndex, iTa
214fa 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 31 29  b, regRecord, 1)
214fb 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e  ;.  if( pIndex->
214fc 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
214fd 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 2c 20   ){.    int j1, 
214fe 6a 32 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 52  j2;.    int regR
214ff 6f 77 69 64 3b 0a 0a 20 20 20 20 72 65 67 52 6f  owid;..    regRo
21500 77 69 64 20 3d 20 72 65 67 49 64 78 4b 65 79 20  wid = regIdxKey 
21501 2b 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d  + pIndex->nColum
21502 6e 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69  n;.    j1 = sqli
21503 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
21504 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 49   OP_IsNull, regI
21505 64 78 4b 65 79 2c 20 30 2c 20 70 49 6e 64 65 78  dxKey, 0, pIndex
21506 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  ->nColumn);.    
21507 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j2 = sqlite3Vdbe
21508 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55  AddOp4(v, OP_IsU
21509 6e 69 71 75 65 2c 20 69 49 64 78 2c 0a 20 20 20  nique, iIdx,.   
2150a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2150b 20 20 20 20 20 20 20 20 30 2c 20 72 65 67 52 6f          0, regRo
2150c 77 69 64 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f  wid, SQLITE_INT_
2150d 54 4f 5f 50 54 52 28 72 65 67 52 65 63 6f 72 64  TO_PTR(regRecord
2150e 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  ), P4_INT32);.  
2150f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21510 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op4(v, OP_Halt, 
21511 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
21512 54 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30 2c 0a  T, OE_Abort, 0,.
21513 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21514 20 20 20 20 22 69 6e 64 65 78 65 64 20 63 6f 6c      "indexed col
21515 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75 6e 69  umns are not uni
21516 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29  que", P4_STATIC)
21517 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21518 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29  eJumpHere(v, j1)
21519 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2151a 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29  eJumpHere(v, j2)
2151b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
2151c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2151d 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64 78 2c  IdxInsert, iIdx,
2151e 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73   regRecord);.  s
2151f 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
21520 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
21521 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74  Record);.  sqlit
21522 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21523 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c 20 61  OP_Next, iTab, a
21524 64 64 72 31 2b 31 29 3b 0a 20 20 73 71 6c 69 74  ddr1+1);.  sqlit
21525 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
21526 2c 20 61 64 64 72 31 29 3b 0a 20 20 73 71 6c 69  , addr1);.  sqli
21527 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
21528 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61 62 29   OP_Close, iTab)
21529 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2152a 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
2152b 65 2c 20 69 49 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a  e, iIdx);.}../*.
2152c 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
2152d 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 53 51 4c  index for an SQL
2152e 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65 31 2e   table.  pName1.
2152f 70 4e 61 6d 65 32 20 69 73 20 74 68 65 20 6e 61  pName2 is the na
21530 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  me of the index 
21531 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69 73 74  .** and pTblList
21532 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
21533 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69  the table that i
21534 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e  s to be indexed.
21535 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a 2a 20    Both will .** 
21536 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20 70 72  be NULL for a pr
21537 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 61 6e 20  imary key or an 
21538 69 6e 64 65 78 20 74 68 61 74 20 69 73 20 63 72  index that is cr
21539 65 61 74 65 64 20 74 6f 20 73 61 74 69 73 66 79  eated to satisfy
2153a 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63 6f 6e   a.** UNIQUE con
2153b 73 74 72 61 69 6e 74 2e 20 20 49 66 20 70 54 61  straint.  If pTa
2153c 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 78 20 61  ble and pIndex a
2153d 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70 50 61  re NULL, use pPa
2153e 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 0a 2a  rse->pNewTable.*
2153f 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 74  * as the table t
21540 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 70  o be indexed.  p
21541 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
21542 20 69 73 20 61 20 74 61 62 6c 65 20 74 68 61 74   is a table that
21543 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79   is.** currently
21544 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74   being construct
21545 65 64 20 62 79 20 61 20 43 52 45 41 54 45 20 54  ed by a CREATE T
21546 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ABLE statement..
21547 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73 20 61  **.** pList is a
21548 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
21549 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20   to be indexed. 
2154a 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65 20 4e   pList will be N
2154b 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a 20 69  ULL if this.** i
2154c 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20  s a primary key 
2154d 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73 74 72  or unique-constr
2154e 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f 73 74  aint on the most
2154f 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 61   recent column a
21550 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 74  dded.** to the t
21551 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75  able currently u
21552 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
21553 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  n.  .*/.SQLITE_P
21554 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21555 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 0a  te3CreateIndex(.
21556 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
21557 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e 66 6f       /* All info
21558 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
21559 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20 54 6f  is parse */.  To
2155a 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20  ken *pName1,    
2155b 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f   /* First part o
2155c 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d 61  f index name. Ma
2155d 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54  y be NULL */.  T
2155e 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20  oken *pName2,   
2155f 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74    /* Second part
21560 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20   of index name. 
21561 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
21562 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c 4e 61   SrcList *pTblNa
21563 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20  me, /* Table to 
21564 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61 72 73  index. Use pPars
21565 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 66 20  e->pNewTable if 
21566 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  0 */.  ExprList 
21567 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41 20 6c  *pList,   /* A l
21568 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74  ist of columns t
21569 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a  o be indexed */.
2156a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20    int onError,  
2156b 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74       /* OE_Abort
2156c 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f  , OE_Ignore, OE_
2156d 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e  Replace, or OE_N
2156e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  one */.  Token *
2156f 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a 20 54  pStart,     /* T
21570 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e 20  he CREATE token 
21571 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 69 73  that begins this
21572 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
21573 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20  Token *pEnd,    
21574 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20 74 68     /* The ")" th
21575 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20 43 52  at closes the CR
21576 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65  EATE INDEX state
21577 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f  ment */.  int so
21578 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f 2a 20  rtOrder,     /* 
21579 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20 70 72  Sort order of pr
2157a 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e 20 70  imary key when p
2157b 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20 20  List==NULL */.  
2157c 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74 20 20  int ifNotExist  
2157d 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72 6f 72     /* Omit error
2157e 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65 61 64   if index alread
2157f 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
21580 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30   Table *pTab = 0
21581 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74  ;     /* Table t
21582 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a  o be indexed */.
21583 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 20    Index *pIndex 
21584 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e  = 0;   /* The in
21585 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 74 65  dex to be create
21586 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  d */.  char *zNa
21587 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e  me = 0;     /* N
21588 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ame of the index
21589 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b   */.  int nName;
2158a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2158b 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
2158c 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20  rs in zName */. 
2158d 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 6f 6b   int i, j;.  Tok
2158e 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20 20 20  en nullId;      
2158f 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 6e 20    /* Fake token 
21590 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49 44 20  for an empty ID 
21591 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69 78 65  list */.  DbFixe
21592 72 20 73 46 69 78 3b 20 20 20 20 20 20 20 20 2f  r sFix;        /
21593 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e 67 20  * For assigning 
21594 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73 20 74  database names t
21595 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  o pTable */.  in
21596 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b  t sortOrderMask;
21597 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f 72     /* 1 to honor
21598 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78 2e 20   DESC in index. 
21599 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20 2a 2f   0 to ignore. */
2159a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2159b 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44   pParse->db;.  D
2159c 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20  b *pDb;         
2159d 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69      /* The speci
2159e 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  fic table contai
2159f 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 65 64  ning the indexed
215a0 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
215a1 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
215a2 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
215a3 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61  the database tha
215a4 74 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74  t is being writt
215a5 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  en */.  Token *p
215a6 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f 2a 20  Name = 0;    /* 
215a7 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65  Unqualified name
215a8 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 74 6f   of the index to
215a9 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73 74 72   create */.  str
215aa 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
215ab 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20 2f 2a  m *pListItem; /*
215ac 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
215ad 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  r pList */.  int
215ae 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45 78   nCol;.  int nEx
215af 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  tra = 0;.  char 
215b0 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 69 66 28 20  *zExtra;..  if( 
215b1 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
215b2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
215b3 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56   || IN_DECLARE_V
215b4 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  TAB ){.    goto 
215b5 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
215b6 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  x;.  }..  /*.  *
215b7 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62 6c 65  * Find the table
215b8 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69   that is to be i
215b9 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72 6e 20  ndexed.  Return 
215ba 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66 6f 75  early if not fou
215bb 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  nd..  */.  if( p
215bc 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a 20  TblName!=0 ){.. 
215bd 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 74 77     /* Use the tw
215be 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e 61 6d  o-part index nam
215bf 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  e to determine t
215c0 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 20  he database .   
215c1 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 66 6f   ** to search fo
215c2 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27 46 69  r the table. 'Fi
215c3 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  x' the table nam
215c4 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20 20 20  e to this db.   
215c5 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b 69   ** before looki
215c6 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c 65 2e  ng up the table.
215c7 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
215c8 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20 70 4e  rt( pName1 && pN
215c9 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44 62 20  ame2 );.    iDb 
215ca 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
215cb 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
215cc 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e  me1, pName2, &pN
215cd 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44  ame);.    if( iD
215ce 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  b<0 ) goto exit_
215cf 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 23  create_index;..#
215d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
215d1 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20 2f 2a  IT_TEMPDB.    /*
215d2 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61   If the index na
215d3 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69  me was unqualifi
215d4 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74 68 65  ed, check if the
215d5 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a   the table.    *
215d6 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c  * is a temp tabl
215d7 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68  e. If so, set th
215d8 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 31 2e  e database to 1.
215d9 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 0a   Do not do this.
215da 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74 69 61      ** if initia
215db 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62 61 73  lising a databas
215dc 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f  e schema..    */
215dd 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  .    if( !db->in
215de 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20  it.busy ){.     
215df 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53   pTab = sqlite3S
215e0 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61  rcListLookup(pPa
215e1 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a  rse, pTblName);.
215e2 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32        if( pName2
215e3 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30   && pName2->n==0
215e4 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61 62   && pTab && pTab
215e5 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61  ->pSchema==db->a
215e6 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 7b  Db[1].pSchema ){
215e7 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20 31  .        iDb = 1
215e8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
215e9 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20  #endif..    if( 
215ea 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26  sqlite3FixInit(&
215eb 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44  sFix, pParse, iD
215ec 62 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e 61 6d  b, "index", pNam
215ed 65 29 20 26 26 0a 20 20 20 20 20 20 20 20 73 71  e) &&.        sq
215ee 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28  lite3FixSrcList(
215ef 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d 65 29  &sFix, pTblName)
215f0 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a  .    ){.      /*
215f1 20 42 65 63 61 75 73 65 20 74 68 65 20 70 61 72   Because the par
215f2 73 65 72 20 63 6f 6e 73 74 72 75 63 74 73 20 70  ser constructs p
215f3 54 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20 73  TblName from a s
215f4 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72  ingle identifier
215f5 2c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74  ,.      ** sqlit
215f6 65 33 46 69 78 53 72 63 4c 69 73 74 20 63 61 6e  e3FixSrcList can
215f7 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f 0a   never fail. */.
215f8 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29 3b        assert(0);
215f9 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62 20  .    }.    pTab 
215fa 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54  = sqlite3LocateT
215fb 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20  able(pParse, 0, 
215fc 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a  pTblName->a[0].z
215fd 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20 70  Name, .        p
215fe 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44  TblName->a[0].zD
215ff 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 69 66  atabase);.    if
21600 28 20 21 70 54 61 62 20 7c 7c 20 64 62 2d 3e 6d  ( !pTab || db->m
21601 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
21602 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
21603 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65 72 74  ndex;.    assert
21604 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  ( db->aDb[iDb].p
21605 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
21606 63 68 65 6d 61 20 29 3b 0a 20 20 7d 65 6c 73 65  chema );.  }else
21607 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  {.    assert( pN
21608 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 54  ame==0 );.    pT
21609 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ab = pParse->pNe
2160a 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 28 20  wTable;.    if( 
2160b 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69  !pTab ) goto exi
2160c 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
2160d 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
2160e 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
2160f 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
21610 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20 26  );.  }.  pDb = &
21611 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20  db->aDb[iDb];.. 
21612 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c 20   if( pTab==0 || 
21613 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 67  pParse->nErr ) g
21614 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
21615 69 6e 64 65 78 3b 0a 20 20 69 66 28 20 73 71 6c  index;.  if( sql
21616 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61  ite3StrNICmp(pTa
21617 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74  b->zName, "sqlit
21618 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20  e_", 7)==0 ){.  
21619 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2161a 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65  g(pParse, "table
2161b 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69   %s may not be i
2161c 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d 3e 7a  ndexed", pTab->z
2161d 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
2161e 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
2161f 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  x;.  }.#ifndef S
21620 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
21621 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c    if( pTab->pSel
21622 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ect ){.    sqlit
21623 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
21624 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20 6e 6f  e, "views may no
21625 74 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0a  t be indexed");.
21626 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
21627 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  eate_index;.  }.
21628 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
21629 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2162a 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49 73  ALTABLE.  if( Is
2162b 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
2162c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2162d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69  rMsg(pParse, "vi
2162e 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 61 79  rtual tables may
2162f 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22   not be indexed"
21630 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
21631 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
21632 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a   }.#endif..  /*.
21633 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 61    ** Find the na
21634 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  me of the index.
21635 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72    Make sure ther
21636 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  e is not already
21637 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e   another.  ** in
21638 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77 69 74  dex or table wit
21639 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e  h the same name.
2163a 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63    .  **.  ** Exc
2163b 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 20 61  eption:  If we a
2163c 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6e  re reading the n
2163d 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e 65 6e  ames of permanen
2163e 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d 20 74  t indices from t
2163f 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d  he.  ** sqlite_m
21640 61 73 74 65 72 20 74 61 62 6c 65 20 28 62 65 63  aster table (bec
21641 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 72 20  ause some other 
21642 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65 64 20  process changed 
21643 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e 64 0a  the schema) and.
21644 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20    ** one of the 
21645 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f 6c 6c  index names coll
21646 69 64 65 73 20 77 69 74 68 20 74 68 65 20 6e 61  ides with the na
21647 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61 72  me of a temporar
21648 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a 20  y table or.  ** 
21649 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65 20 77  index, then we w
2164a 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20  ill continue to 
2164b 70 72 6f 63 65 73 73 20 74 68 69 73 20 69 6e 64  process this ind
2164c 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  ex..  **.  ** If
2164d 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 61   pName==0 it mea
2164e 6e 73 20 74 68 61 74 20 77 65 20 61 72 65 0a 20  ns that we are. 
2164f 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69 74 68   ** dealing with
21650 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f   a primary key o
21651 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61  r UNIQUE constra
21652 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20 74 6f  int.  We have to
21653 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20 2a 2a   invent our.  **
21654 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a   own name..  */.
21655 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20    if( pName ){. 
21656 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
21657 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
21658 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20  db, pName);.    
21659 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
2165a 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
2165b 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
2165c 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
2165d 78 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d 65  x;.    if( zName
2165e 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  ==0 ) goto exit_
2165f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
21660 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
21661 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a  =sqlite3CheckObj
21662 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  ectName(pParse, 
21663 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20  zName) ){.      
21664 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
21665 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20  _index;.    }.  
21666 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
21667 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 69 66  busy ){.      if
21668 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
21669 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
2166a 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 65 78  Parse) ) goto ex
2166b 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
2166c 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
2166d 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
2166e 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29 7b 0a  zName, 0)!=0 ){.
2166f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
21670 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
21671 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64  "there is alread
21672 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 64 20  y a table named 
21673 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  %s", zName);.   
21674 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
21675 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
21676 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
21677 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e  f( sqlite3FindIn
21678 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70  dex(db, zName, p
21679 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 29 7b  Db->zName)!=0 ){
2167a 0a 20 20 20 20 20 20 69 66 28 20 21 69 66 4e 6f  .      if( !ifNo
2167b 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20  tExist ){.      
2167c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2167d 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64 65 78  g(pParse, "index
2167e 20 25 73 20 61 6c 72 65 61 64 79 20 65 78 69 73   %s already exis
2167f 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  ts", zName);.   
21680 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20     }.      goto 
21681 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
21682 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  x;.    }.  }else
21683 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20  {.    int n;.   
21684 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b 0a 20   Index *pLoop;. 
21685 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 54 61     for(pLoop=pTa
21686 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31 3b 20  b->pIndex, n=1; 
21687 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f  pLoop; pLoop=pLo
21688 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b 29 7b  op->pNext, n++){
21689 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71  }.    zName = sq
2168a 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2168b 20 22 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64   "sqlite_autoind
2168c 65 78 5f 25 73 5f 25 64 22 2c 20 70 54 61 62 2d  ex_%s_%d", pTab-
2168d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20  >zName, n);.    
2168e 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a  if( zName==0 ){.
2168f 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
21690 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
21691 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68    }.  }..  /* Ch
21692 65 63 6b 20 66 6f 72 20 61 75 74 68 6f 72 69 7a  eck for authoriz
21693 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20  ation to create 
21694 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a 23  an index..  */.#
21695 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21696 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
21697 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  .  {.    const c
21698 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d 3e  har *zDb = pDb->
21699 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73  zName;.    if( s
2169a 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2169b 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49  pParse, SQLITE_I
2169c 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41  NSERT, SCHEMA_TA
2169d 42 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a 44 62  BLE(iDb), 0, zDb
2169e 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
2169f 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
216a0 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d  x;.    }.    i =
216a1 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49   SQLITE_CREATE_I
216a2 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 21 4f  NDEX;.    if( !O
216a3 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44  MIT_TEMPDB && iD
216a4 62 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c 49 54  b==1 ) i = SQLIT
216a5 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e  E_CREATE_TEMP_IN
216a6 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  DEX;.    if( sql
216a7 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
216a8 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 2c 20  arse, i, zName, 
216a9 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62  pTab->zName, zDb
216aa 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
216ab 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
216ac 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  x;.    }.  }.#en
216ad 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 4c 69  dif..  /* If pLi
216ae 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73 20  st==0, it means 
216af 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73  this routine was
216b0 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65 20   called to make 
216b1 61 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b  a primary.  ** k
216b2 65 79 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 61  ey out of the la
216b3 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64 20  st column added 
216b4 74 6f 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64  to the table und
216b5 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e  er construction.
216b6 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 74 65 20  .  ** So create 
216b7 61 20 66 61 6b 65 20 6c 69 73 74 20 74 6f 20 73  a fake list to s
216b8 69 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a 20 20  imulate this..  
216b9 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  */.  if( pList==
216ba 30 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e  0 ){.    nullId.
216bb 7a 20 3d 20 28 75 38 2a 29 70 54 61 62 2d 3e 61  z = (u8*)pTab->a
216bc 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31  Col[pTab->nCol-1
216bd 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75 6c  ].zName;.    nul
216be 6c 49 64 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53  lId.n = sqlite3S
216bf 74 72 6c 65 6e 33 30 28 28 63 68 61 72 2a 29 6e  trlen30((char*)n
216c0 75 6c 6c 49 64 2e 7a 29 3b 0a 20 20 20 20 70 4c  ullId.z);.    pL
216c1 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
216c2 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
216c3 73 65 2c 20 30 2c 20 30 2c 20 26 6e 75 6c 6c 49  se, 0, 0, &nullI
216c4 64 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73  d);.    if( pLis
216c5 74 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74  t==0 ) goto exit
216c6 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
216c7 20 20 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73     pList->a[0].s
216c8 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29 73  ortOrder = (u8)s
216c9 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 0a 20  ortOrder;.  }.. 
216ca 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
216cb 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 6f 66  ow many bytes of
216cc 20 73 70 61 63 65 20 61 72 65 20 72 65 71 75 69   space are requi
216cd 72 65 64 20 74 6f 20 73 74 6f 72 65 20 65 78 70  red to store exp
216ce 6c 69 63 69 74 6c 79 0a 20 20 2a 2a 20 73 70 65  licitly.  ** spe
216cf 63 69 66 69 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  cified collation
216d0 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 2e   sequence names.
216d1 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
216d2 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
216d3 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20   i++){.    Expr 
216d4 2a 70 45 78 70 72 3b 0a 20 20 20 20 43 6f 6c 6c  *pExpr;.    Coll
216d5 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20  Seq *pColl;.    
216d6 69 66 28 20 28 70 45 78 70 72 20 3d 20 70 4c 69  if( (pExpr = pLi
216d7 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 21  st->a[i].pExpr)!
216d8 3d 30 20 26 26 20 28 70 43 6f 6c 6c 20 3d 20 70  =0 && (pColl = p
216d9 45 78 70 72 2d 3e 70 43 6f 6c 6c 29 21 3d 30 20  Expr->pColl)!=0 
216da 29 7b 0a 20 20 20 20 20 20 6e 45 78 74 72 61 20  ){.      nExtra 
216db 2b 3d 20 28 31 20 2b 20 73 71 6c 69 74 65 33 53  += (1 + sqlite3S
216dc 74 72 6c 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a  trlen30(pColl->z
216dd 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20 20  Name));.    }.  
216de 7d 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 41 6c  }..  /* .  ** Al
216df 6c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65 78  locate the index
216e0 20 73 74 72 75 63 74 75 72 65 2e 20 0a 20 20 2a   structure. .  *
216e1 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  /.  nName = sqli
216e2 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
216e3 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 70 4c 69  e);.  nCol = pLi
216e4 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e  st->nExpr;.  pIn
216e5 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  dex = sqlite3DbM
216e6 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20  allocZero(db, . 
216e7 20 20 20 20 20 73 69 7a 65 6f 66 28 49 6e 64 65       sizeof(Inde
216e8 78 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 20  x) +            
216e9 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72 75 63    /* Index struc
216ea 74 75 72 65 20 20 2a 2f 0a 20 20 20 20 20 20 73  ture  */.      s
216eb 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 6f 6c 20  izeof(int)*nCol 
216ec 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  +           /* I
216ed 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e 20 20 20  ndex.aiColumn   
216ee 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28  */.      sizeof(
216ef 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29 20 2b 20  int)*(nCol+1) + 
216f0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61        /* Index.a
216f1 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a 20 20 20  iRowEst   */.   
216f2 20 20 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a     sizeof(char *
216f3 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20  )*nCol +        
216f4 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20  /* Index.azColl 
216f5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a      */.      siz
216f6 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20 2b 20 20  eof(u8)*nCol +  
216f7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
216f8 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20 2a 2f  ex.aSortOrder */
216f9 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b 20 31  .      nName + 1
216fa 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   +              
216fb 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 7a 4e 61      /* Index.zNa
216fc 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  me      */.     
216fd 20 6e 45 78 74 72 61 20 20 20 20 20 20 20 20 20   nExtra         
216fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
216ff 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   Collation seque
21700 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 29  nce names */.  )
21701 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
21702 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
21703 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
21704 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70 49  _index;.  }.  pI
21705 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 28  ndex->azColl = (
21706 63 68 61 72 2a 2a 29 28 26 70 49 6e 64 65 78 5b  char**)(&pIndex[
21707 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61  1]);.  pIndex->a
21708 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74 20 2a  iColumn = (int *
21709 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c  )(&pIndex->azCol
2170a 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64  l[nCol]);.  pInd
2170b 65 78 2d 3e 61 69 52 6f 77 45 73 74 20 3d 20 28  ex->aiRowEst = (
2170c 75 6e 73 69 67 6e 65 64 20 2a 29 28 26 70 49 6e  unsigned *)(&pIn
2170d 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 43  dex->aiColumn[nC
2170e 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  ol]);.  pIndex->
2170f 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  aSortOrder = (u8
21710 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 69 52   *)(&pIndex->aiR
21711 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d 29 3b 0a  owEst[nCol+1]);.
21712 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 20    pIndex->zName 
21713 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64  = (char *)(&pInd
21714 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e  ex->aSortOrder[n
21715 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74 72 61 20  Col]);.  zExtra 
21716 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64  = (char *)(&pInd
21717 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 2b  ex->zName[nName+
21718 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 49  1]);.  memcpy(pI
21719 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61  ndex->zName, zNa
2171a 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20  me, nName+1);.  
2171b 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 20 3d  pIndex->pTable =
2171c 20 70 54 61 62 3b 0a 20 20 70 49 6e 64 65 78 2d   pTab;.  pIndex-
2171d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73 74  >nColumn = pList
2171e 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 64 65  ->nExpr;.  pInde
2171f 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 28 75 38  x->onError = (u8
21720 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 70 49 6e 64  )onError;.  pInd
21721 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d 20  ex->autoIndex = 
21722 28 75 38 29 28 70 4e 61 6d 65 3d 3d 30 29 3b 0a  (u8)(pName==0);.
21723 20 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d    pIndex->pSchem
21724 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  a = db->aDb[iDb]
21725 2e 70 53 63 68 65 6d 61 3b 0a 0a 20 20 2f 2a 20  .pSchema;..  /* 
21726 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
21727 77 65 20 73 68 6f 75 6c 64 20 68 6f 6e 6f 72 20  we should honor 
21728 44 45 53 43 20 72 65 71 75 65 73 74 73 20 6f 6e  DESC requests on
21729 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 0a 20   index columns. 
2172a 20 2a 2f 0a 20 20 69 66 28 20 70 44 62 2d 3e 70   */.  if( pDb->p
2172b 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72  Schema->file_for
2172c 6d 61 74 3e 3d 34 20 29 7b 0a 20 20 20 20 73 6f  mat>=4 ){.    so
2172d 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20 2d 31  rtOrderMask = -1
2172e 3b 20 20 20 2f 2a 20 48 6f 6e 6f 72 20 44 45 53  ;   /* Honor DES
2172f 43 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  C */.  }else{.  
21730 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20    sortOrderMask 
21731 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67 6e 6f 72  = 0;    /* Ignor
21732 65 20 44 45 53 43 20 2a 2f 0a 20 20 7d 0a 0a 20  e DESC */.  }.. 
21733 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 6e 61 6d   /* Scan the nam
21734 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  es of the column
21735 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  s of the table t
21736 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 6e 64  o be indexed and
21737 0a 20 20 2a 2a 20 6c 6f 61 64 20 74 68 65 20 63  .  ** load the c
21738 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73 20 69 6e  olumn indices in
21739 74 6f 20 74 68 65 20 49 6e 64 65 78 20 73 74 72  to the Index str
2173a 75 63 74 75 72 65 2e 20 20 52 65 70 6f 72 74 20  ucture.  Report 
2173b 61 6e 20 65 72 72 6f 72 0a 20 20 2a 2a 20 69 66  an error.  ** if
2173c 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 69 73 20 6e   any column is n
2173d 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20  ot found..  */. 
2173e 20 66 6f 72 28 69 3d 30 2c 20 70 4c 69 73 74 49   for(i=0, pListI
2173f 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c  tem=pList->a; i<
21740 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
21741 2b 2c 20 70 4c 69 73 74 49 74 65 6d 2b 2b 29 7b  +, pListItem++){
21742 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
21743 2a 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70 4c 69 73  *zColName = pLis
21744 74 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  tItem->zName;.  
21745 20 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62 43 6f    Column *pTabCo
21746 6c 3b 0a 20 20 20 20 69 6e 74 20 72 65 71 75 65  l;.    int reque
21747 73 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20  stedSortOrder;. 
21748 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20     char *zColl; 
21749 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2174a 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73    /* Collation s
2174b 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a  equence name */.
2174c 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 54  .    for(j=0, pT
2174d 61 62 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c  abCol=pTab->aCol
2174e 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; j<pTab->nCol; 
2174f 6a 2b 2b 2c 20 70 54 61 62 43 6f 6c 2b 2b 29 7b  j++, pTabCol++){
21750 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
21751 65 33 53 74 72 49 43 6d 70 28 7a 43 6f 6c 4e 61  e3StrICmp(zColNa
21752 6d 65 2c 20 70 54 61 62 43 6f 6c 2d 3e 7a 4e 61  me, pTabCol->zNa
21753 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  me)==0 ) break;.
21754 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e      }.    if( j>
21755 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20  =pTab->nCol ){. 
21756 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
21757 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
21758 62 6c 65 20 25 73 20 68 61 73 20 6e 6f 20 63 6f  ble %s has no co
21759 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22 2c 0a  lumn named %s",.
2175a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e          pTab->zN
2175b 61 6d 65 2c 20 7a 43 6f 6c 4e 61 6d 65 29 3b 0a  ame, zColName);.
2175c 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
2175d 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
2175e 20 20 7d 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a    }.    /* TODO:
2175f 20 20 41 64 64 20 61 20 74 65 73 74 20 74 6f 20    Add a test to 
21760 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
21761 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69  he same column i
21762 73 20 6e 6f 74 20 6e 61 6d 65 64 0a 20 20 20 20  s not named.    
21763 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ** more than onc
21764 65 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d  e within the sam
21765 65 20 69 6e 64 65 78 2e 20 20 4f 6e 6c 79 20 74  e index.  Only t
21766 68 65 20 66 69 72 73 74 20 69 6e 73 74 61 6e 63  he first instanc
21767 65 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20  e of.    ** the 
21768 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 65 76 65 72  column will ever
21769 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20   be used by the 
2176a 6f 70 74 69 6d 69 7a 65 72 2e 20 20 4e 6f 74 65  optimizer.  Note
2176b 20 74 68 61 74 20 75 73 69 6e 67 20 74 68 65 0a   that using the.
2176c 20 20 20 20 2a 2a 20 73 61 6d 65 20 63 6f 6c 75      ** same colu
2176d 6d 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  mn more than onc
2176e 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 65  e cannot be an e
2176f 72 72 6f 72 20 62 65 63 61 75 73 65 20 74 68 61  rror because tha
21770 74 20 77 6f 75 6c 64 20 0a 20 20 20 20 2a 2a 20  t would .    ** 
21771 62 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20  break backwards 
21772 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2d 20  compatibility - 
21773 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61  it needs to be a
21774 20 77 61 72 6e 69 6e 67 2e 0a 20 20 20 20 2a 2f   warning..    */
21775 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 43  .    pIndex->aiC
21776 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 6a 3b 0a 20 20  olumn[i] = j;.  
21777 20 20 69 66 28 20 70 4c 69 73 74 49 74 65 6d 2d    if( pListItem-
21778 3e 70 45 78 70 72 20 26 26 20 70 4c 69 73 74 49  >pExpr && pListI
21779 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c  tem->pExpr->pCol
2177a 6c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  l ){.      asser
2177b 74 28 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45  t( pListItem->pE
2177c 78 70 72 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20 20  xpr->pColl );.  
2177d 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 7a 45 78 74      zColl = zExt
2177e 72 61 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ra;.      sqlite
2177f 33 5f 73 6e 70 72 69 6e 74 66 28 6e 45 78 74 72  3_snprintf(nExtr
21780 61 2c 20 7a 45 78 74 72 61 2c 20 22 25 73 22 2c  a, zExtra, "%s",
21781 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70   pListItem->pExp
21782 72 2d 3e 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29  r->pColl->zName)
21783 3b 0a 20 20 20 20 20 20 7a 45 78 74 72 61 20 2b  ;.      zExtra +
21784 3d 20 28 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  = (sqlite3Strlen
21785 33 30 28 7a 43 6f 6c 6c 29 20 2b 20 31 29 3b 0a  30(zColl) + 1);.
21786 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21787 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61   zColl = pTab->a
21788 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20  Col[j].zColl;.  
21789 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29      if( !zColl )
2178a 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20  {.        zColl 
2178b 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d  = db->pDfltColl-
2178c 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a  >zName;.      }.
2178d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64      }.    if( !d
2178e 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20  b->init.busy && 
2178f 21 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f  !sqlite3LocateCo
21790 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43  llSeq(pParse, zC
21791 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20  oll, -1) ){.    
21792 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
21793 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
21794 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f      pIndex->azCo
21795 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20  ll[i] = zColl;. 
21796 20 20 20 72 65 71 75 65 73 74 65 64 53 6f 72 74     requestedSort
21797 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 49 74 65  Order = pListIte
21798 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 20 73  m->sortOrder & s
21799 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a 20 20  ortOrderMask;.  
2179a 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f    pIndex->aSortO
2179b 72 64 65 72 5b 69 5d 20 3d 20 28 75 38 29 72 65  rder[i] = (u8)re
2179c 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72  questedSortOrder
2179d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  ;.  }.  sqlite3D
2179e 65 66 61 75 6c 74 52 6f 77 45 73 74 28 70 49 6e  efaultRowEst(pIn
2179f 64 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 54 61  dex);..  if( pTa
217a0 62 3d 3d 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  b==pParse->pNewT
217a1 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54  able ){.    /* T
217a2 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20  his routine has 
217a3 62 65 65 6e 20 63 61 6c 6c 65 64 20 74 6f 20 63  been called to c
217a4 72 65 61 74 65 20 61 6e 20 61 75 74 6f 6d 61 74  reate an automat
217a5 69 63 20 69 6e 64 65 78 20 61 73 20 61 0a 20 20  ic index as a.  
217a6 20 20 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 61    ** result of a
217a7 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
217a8 55 4e 49 51 55 45 20 63 6c 61 75 73 65 20 6f 6e  UNIQUE clause on
217a9 20 61 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69   a column defini
217aa 74 69 6f 6e 2c 20 6f 72 0a 20 20 20 20 2a 2a 20  tion, or.    ** 
217ab 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72  a PRIMARY KEY or
217ac 20 55 4e 49 51 55 45 20 63 6c 61 75 73 65 20 66   UNIQUE clause f
217ad 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 63 6f 6c  ollowing the col
217ae 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e  umn definitions.
217af 0a 20 20 20 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65  .    ** i.e. one
217b0 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20   of:.    **.    
217b1 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
217b2 74 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  t(x PRIMARY KEY,
217b3 20 79 29 3b 0a 20 20 20 20 2a 2a 20 43 52 45 41   y);.    ** CREA
217b4 54 45 20 54 41 42 4c 45 20 74 28 78 2c 20 79 2c  TE TABLE t(x, y,
217b5 20 55 4e 49 51 55 45 28 78 2c 20 79 29 29 3b 0a   UNIQUE(x, y));.
217b6 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 69      **.    ** Ei
217b7 74 68 65 72 20 77 61 79 2c 20 63 68 65 63 6b 20  ther way, check 
217b8 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 74 61  to see if the ta
217b9 62 6c 65 20 61 6c 72 65 61 64 79 20 68 61 73 20  ble already has 
217ba 73 75 63 68 20 61 6e 20 69 6e 64 65 78 2e 20 49  such an index. I
217bb 66 0a 20 20 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e  f.    ** so, don
217bc 27 74 20 62 6f 74 68 65 72 20 63 72 65 61 74 69  't bother creati
217bd 6e 67 20 74 68 69 73 20 6f 6e 65 2e 20 54 68 69  ng this one. Thi
217be 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74  s only applies t
217bf 6f 0a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74  o.    ** automat
217c0 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 69  ically created i
217c1 6e 64 69 63 65 73 2e 20 55 73 65 72 73 20 63 61  ndices. Users ca
217c2 6e 20 64 6f 20 61 73 20 74 68 65 79 20 77 69 73  n do as they wis
217c3 68 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 65 78  h with.    ** ex
217c4 70 6c 69 63 69 74 20 69 6e 64 69 63 65 73 2e 0a  plicit indices..
217c5 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78      */.    Index
217c6 20 2a 70 49 64 78 3b 0a 20 20 20 20 66 6f 72 28   *pIdx;.    for(
217c7 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
217c8 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
217c9 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
217ca 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 61    int k;.      a
217cb 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6f 6e 45  ssert( pIdx->onE
217cc 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b  rror!=OE_None );
217cd 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
217ce 49 64 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29  Idx->autoIndex )
217cf 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
217d0 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21  pIndex->onError!
217d1 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 0a 20 20 20  =OE_None );..   
217d2 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6e 43 6f     if( pIdx->nCo
217d3 6c 75 6d 6e 21 3d 70 49 6e 64 65 78 2d 3e 6e 43  lumn!=pIndex->nC
217d4 6f 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65  olumn ) continue
217d5 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b  ;.      for(k=0;
217d6 20 6b 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   k<pIdx->nColumn
217d7 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; k++){.        
217d8 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31 20 3d  const char *z1 =
217d9 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d   pIdx->azColl[k]
217da 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  ;.        const 
217db 63 68 61 72 20 2a 7a 32 20 3d 20 70 49 6e 64 65  char *z2 = pInde
217dc 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20  x->azColl[k];.  
217dd 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
217de 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e  aiColumn[k]!=pIn
217df 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d  dex->aiColumn[k]
217e0 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
217e1 20 20 69 66 28 20 70 49 64 78 2d 3e 61 53 6f 72    if( pIdx->aSor
217e2 74 4f 72 64 65 72 5b 6b 5d 21 3d 70 49 6e 64 65  tOrder[k]!=pInde
217e3 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6b 5d  x->aSortOrder[k]
217e4 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
217e5 20 20 69 66 28 20 7a 31 21 3d 7a 32 20 26 26 20    if( z1!=z2 && 
217e6 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
217e7 31 2c 20 7a 32 29 20 29 20 62 72 65 61 6b 3b 0a  1, z2) ) break;.
217e8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
217e9 28 20 6b 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75  ( k==pIdx->nColu
217ea 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  mn ){.        if
217eb 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21  ( pIdx->onError!
217ec 3d 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72  =pIndex->onError
217ed 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
217ee 20 54 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   This constraint
217ef 20 63 72 65 61 74 65 73 20 74 68 65 20 73 61 6d   creates the sam
217f0 65 20 69 6e 64 65 78 20 61 73 20 61 20 70 72 65  e index as a pre
217f1 76 69 6f 75 73 0a 20 20 20 20 20 20 20 20 20 20  vious.          
217f2 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 70  ** constraint sp
217f3 65 63 69 66 69 65 64 20 73 6f 6d 65 77 68 65 72  ecified somewher
217f4 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20  e in the CREATE 
217f5 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
217f6 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 48 6f  .          ** Ho
217f7 77 65 76 65 72 20 74 68 65 20 4f 4e 20 43 4f 4e  wever the ON CON
217f8 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20 61 72  FLICT clauses ar
217f9 65 20 64 69 66 66 65 72 65 6e 74 2e 20 49 66 20  e different. If 
217fa 62 6f 74 68 20 74 68 69 73 20 0a 20 20 20 20 20  both this .     
217fb 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69       ** constrai
217fc 6e 74 20 61 6e 64 20 74 68 65 20 70 72 65 76 69  nt and the previ
217fd 6f 75 73 20 65 71 75 69 76 61 6c 65 6e 74 20 63  ous equivalent c
217fe 6f 6e 73 74 72 61 69 6e 74 20 68 61 76 65 20 65  onstraint have e
217ff 78 70 6c 69 63 69 74 0a 20 20 20 20 20 20 20 20  xplicit.        
21800 20 20 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54    ** ON CONFLICT
21801 20 63 6c 61 75 73 65 73 20 74 68 69 73 20 69 73   clauses this is
21802 20 61 6e 20 65 72 72 6f 72 2e 20 4f 74 68 65 72   an error. Other
21803 77 69 73 65 2c 20 75 73 65 20 74 68 65 0a 20 20  wise, use the.  
21804 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 6c 69          ** expli
21805 63 69 74 6c 79 20 73 70 65 63 69 66 69 65 64 20  citly specified 
21806 62 65 68 61 76 69 6f 75 72 20 66 6f 72 20 74 68  behaviour for th
21807 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20  e index..       
21808 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
21809 69 66 28 20 21 28 70 49 64 78 2d 3e 6f 6e 45 72  if( !(pIdx->onEr
2180a 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
2180b 7c 7c 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72  || pIndex->onErr
2180c 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 29 20  or==OE_Default) 
2180d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
2180e 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2180f 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
21810 20 20 20 20 20 20 20 20 22 63 6f 6e 66 6c 69 63          "conflic
21811 74 69 6e 67 20 4f 4e 20 43 4f 4e 46 4c 49 43 54  ting ON CONFLICT
21812 20 63 6c 61 75 73 65 73 20 73 70 65 63 69 66 69   clauses specifi
21813 65 64 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ed", 0);.       
21814 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
21815 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  f( pIdx->onError
21816 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a  ==OE_Default ){.
21817 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64 78              pIdx
21818 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 70 49 6e 64  ->onError = pInd
21819 65 78 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20 20  ex->onError;.   
2181a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2181b 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20   }.        goto 
2181c 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
2181d 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  x;.      }.    }
2181e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20  .  }..  /* Link 
2181f 74 68 65 20 6e 65 77 20 49 6e 64 65 78 20 73 74  the new Index st
21820 72 75 63 74 75 72 65 20 74 6f 20 69 74 73 20 74  ructure to its t
21821 61 62 6c 65 20 61 6e 64 20 74 6f 20 74 68 65 20  able and to the 
21822 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e 2d 6d 65  other.  ** in-me
21823 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 73 74  mory database st
21824 72 75 63 74 75 72 65 73 2e 20 0a 20 20 2a 2f 0a  ructures. .  */.
21825 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
21826 75 73 79 20 29 7b 0a 20 20 20 20 49 6e 64 65 78  usy ){.    Index
21827 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c   *p;.    p = sql
21828 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26  ite3HashInsert(&
21829 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d  pIndex->pSchema-
2182a 3e 69 64 78 48 61 73 68 2c 20 0a 20 20 20 20 20  >idxHash, .     
2182b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2182c 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61       pIndex->zNa
2182d 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  me, sqlite3Strle
2182e 6e 33 30 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d  n30(pIndex->zNam
2182f 65 29 2b 31 2c 0a 20 20 20 20 20 20 20 20 20 20  e)+1,.          
21830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21831 70 49 6e 64 65 78 29 3b 0a 20 20 20 20 69 66 28  pIndex);.    if(
21832 20 70 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65   p ){.      asse
21833 72 74 28 20 70 3d 3d 70 49 6e 64 65 78 20 29 3b  rt( p==pIndex );
21834 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74    /* Malloc must
21835 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a   have failed */.
21836 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
21837 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
21838 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
21839 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
2183a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
2183b 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
2183c 61 6e 67 65 73 3b 0a 20 20 20 20 69 66 28 20 70  anges;.    if( p
2183d 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 20 20  TblName!=0 ){.  
2183e 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d      pIndex->tnum
2183f 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54   = db->init.newT
21840 6e 75 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  num;.    }.  }..
21841 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e    /* If the db->
21842 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30 20 74  init.busy is 0 t
21843 68 65 6e 20 63 72 65 61 74 65 20 74 68 65 20 69  hen create the i
21844 6e 64 65 78 20 6f 6e 20 64 69 73 6b 2e 20 20 54  ndex on disk.  T
21845 68 69 73 0a 20 20 2a 2a 20 69 6e 76 6f 6c 76 65  his.  ** involve
21846 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 69 6e  s writing the in
21847 64 65 78 20 69 6e 74 6f 20 74 68 65 20 6d 61 73  dex into the mas
21848 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 66 69  ter table and fi
21849 6c 6c 69 6e 67 20 69 6e 20 74 68 65 0a 20 20 2a  lling in the.  *
2184a 2a 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 65  * index with the
2184b 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 20 63   current table c
2184c 6f 6e 74 65 6e 74 73 2e 0a 20 20 2a 2a 0a 20 20  ontents..  **.  
2184d 2a 2a 20 54 68 65 20 64 62 2d 3e 69 6e 69 74 2e  ** The db->init.
2184e 62 75 73 79 20 69 73 20 30 20 77 68 65 6e 20 74  busy is 0 when t
2184f 68 65 20 75 73 65 72 20 66 69 72 73 74 20 65 6e  he user first en
21850 74 65 72 73 20 61 20 43 52 45 41 54 45 20 49 4e  ters a CREATE IN
21851 44 45 58 20 0a 20 20 2a 2a 20 63 6f 6d 6d 61 6e  DEX .  ** comman
21852 64 2e 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  d.  db->init.bus
21853 79 20 69 73 20 31 20 77 68 65 6e 20 61 20 64 61  y is 1 when a da
21854 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
21855 20 61 6e 64 20 0a 20 20 2a 2a 20 43 52 45 41 54   and .  ** CREAT
21856 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e  E INDEX statemen
21857 74 73 20 61 72 65 20 72 65 61 64 20 6f 75 74 20  ts are read out 
21858 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 74 61  of the master ta
21859 62 6c 65 2e 20 20 49 6e 0a 20 20 2a 2a 20 74 68  ble.  In.  ** th
2185a 65 20 6c 61 74 74 65 72 20 63 61 73 65 20 74 68  e latter case th
2185b 65 20 69 6e 64 65 78 20 61 6c 72 65 61 64 79 20  e index already 
2185c 65 78 69 73 74 73 20 6f 6e 20 64 69 73 6b 2c 20  exists on disk, 
2185d 77 68 69 63 68 20 69 73 20 77 68 79 0a 20 20 2a  which is why.  *
2185e 2a 20 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20  * we don't want 
2185f 74 6f 20 72 65 63 72 65 61 74 65 20 69 74 2e 0a  to recreate it..
21860 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 54 62    **.  ** If pTb
21861 6c 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e  lName==0 it mean
21862 73 20 74 68 69 73 20 69 6e 64 65 78 20 69 73 20  s this index is 
21863 67 65 6e 65 72 61 74 65 64 20 61 73 20 61 20 70  generated as a p
21864 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20  rimary key.  ** 
21865 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  or UNIQUE constr
21866 61 69 6e 74 20 6f 66 20 61 20 43 52 45 41 54 45  aint of a CREATE
21867 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
21868 2e 20 20 53 69 6e 63 65 20 74 68 65 20 74 61 62  .  Since the tab
21869 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6a 75 73 74  le.  ** has just
2186a 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 69   been created, i
2186b 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 61  t contains no da
2186c 74 61 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  ta and the index
2186d 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a   initialization.
2186e 20 20 2a 2a 20 73 74 65 70 20 63 61 6e 20 62 65    ** step can be
2186f 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a 20   skipped..  */. 
21870 20 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 69 6e   else if( db->in
21871 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20  it.busy==0 ){.  
21872 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 63    Vdbe *v;.    c
21873 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20  har *zStmt;.    
21874 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61  int iMem = ++pPa
21875 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20 20 20  rse->nMem;..    
21876 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
21877 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
21878 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20  if( v==0 ) goto 
21879 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
2187a 78 3b 0a 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61  x;...    /* Crea
2187b 74 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20  te the rootpage 
2187c 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20  for the index.  
2187d 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
2187e 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
2187f 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69  ion(pParse, 1, i
21880 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
21881 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21882 5f 43 72 65 61 74 65 49 6e 64 65 78 2c 20 69 44  _CreateIndex, iD
21883 62 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 2f  b, iMem);..    /
21884 2a 20 47 61 74 68 65 72 20 74 68 65 20 63 6f 6d  * Gather the com
21885 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68  plete text of th
21886 65 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73  e CREATE INDEX s
21887 74 61 74 65 6d 65 6e 74 20 69 6e 74 6f 0a 20 20  tatement into.  
21888 20 20 2a 2a 20 74 68 65 20 7a 53 74 6d 74 20 76    ** the zStmt v
21889 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20  ariable.    */. 
2188a 20 20 20 69 66 28 20 70 53 74 61 72 74 20 26 26     if( pStart &&
2188b 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 2f   pEnd ){.      /
2188c 2a 20 41 20 6e 61 6d 65 64 20 69 6e 64 65 78 20  * A named index 
2188d 77 69 74 68 20 61 6e 20 65 78 70 6c 69 63 69 74  with an explicit
2188e 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74   CREATE INDEX st
2188f 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20  atement */.     
21890 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33   zStmt = sqlite3
21891 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 43 52 45  MPrintf(db, "CRE
21892 41 54 45 25 73 20 49 4e 44 45 58 20 25 2e 2a 73  ATE%s INDEX %.*s
21893 22 2c 0a 20 20 20 20 20 20 20 20 6f 6e 45 72 72  ",.        onErr
21894 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 3f 20 22 22  or==OE_None ? ""
21895 20 3a 20 22 20 55 4e 49 51 55 45 22 2c 0a 20 20   : " UNIQUE",.  
21896 20 20 20 20 20 20 70 45 6e 64 2d 3e 7a 20 2d 20        pEnd->z - 
21897 70 4e 61 6d 65 2d 3e 7a 20 2b 20 31 2c 0a 20 20  pName->z + 1,.  
21898 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e 7a 29 3b        pName->z);
21899 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2189a 20 20 2f 2a 20 41 6e 20 61 75 74 6f 6d 61 74 69    /* An automati
2189b 63 20 69 6e 64 65 78 20 63 72 65 61 74 65 64 20  c index created 
2189c 62 79 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59  by a PRIMARY KEY
2189d 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74   or UNIQUE const
2189e 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 2f  raint */.      /
2189f 2a 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65  * zStmt = sqlite
218a0 33 4d 50 72 69 6e 74 66 28 22 22 29 3b 20 2a 2f  3MPrintf(""); */
218a1 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 30  .      zStmt = 0
218a2 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
218a3 41 64 64 20 61 6e 20 65 6e 74 72 79 20 69 6e 20  Add an entry in 
218a4 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 66 6f  sqlite_master fo
218a5 72 20 74 68 69 73 20 69 6e 64 65 78 0a 20 20 20  r this index.   
218a6 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e   */.    sqlite3N
218a7 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
218a8 65 2c 20 0a 20 20 20 20 20 20 20 20 22 49 4e 53  e, .        "INS
218a9 45 52 54 20 49 4e 54 4f 20 25 51 2e 25 73 20 56  ERT INTO %Q.%s V
218aa 41 4c 55 45 53 28 27 69 6e 64 65 78 27 2c 25 51  ALUES('index',%Q
218ab 2c 25 51 2c 23 25 64 2c 25 51 29 3b 22 2c 0a 20  ,%Q,#%d,%Q);",. 
218ac 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69         db->aDb[i
218ad 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d  Db].zName, SCHEM
218ae 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20  A_TABLE(iDb),.  
218af 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e        pIndex->zN
218b0 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 70 54 61  ame,.        pTa
218b1 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  b->zName,.      
218b2 20 20 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20    iMem,.        
218b3 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20  zStmt.    );.   
218b4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
218b5 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20 20 20 20  b, zStmt);..    
218b6 2f 2a 20 46 69 6c 6c 20 74 68 65 20 69 6e 64 65  /* Fill the inde
218b7 78 20 77 69 74 68 20 64 61 74 61 20 61 6e 64 20  x with data and 
218b8 72 65 70 61 72 73 65 20 74 68 65 20 73 63 68 65  reparse the sche
218b9 6d 61 2e 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45  ma. Code an OP_E
218ba 78 70 69 72 65 0a 20 20 20 20 2a 2a 20 74 6f 20  xpire.    ** to 
218bb 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70  invalidate all p
218bc 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  re-compiled stat
218bd 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20  ements..    */. 
218be 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 20     if( pTblName 
218bf 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
218c0 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72  RefillIndex(pPar
218c1 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65 6d  se, pIndex, iMem
218c2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
218c3 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
218c4 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  rse, iDb);.     
218c5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
218c6 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63  p4(v, OP_ParseSc
218c7 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c  hema, iDb, 0, 0,
218c8 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
218c9 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61  3MPrintf(db, "na
218ca 6d 65 3d 27 25 71 27 22 2c 20 70 49 6e 64 65 78  me='%q'", pIndex
218cb 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e  ->zName), P4_DYN
218cc 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  AMIC);.      sql
218cd 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
218ce 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 29 3b  , OP_Expire, 0);
218cf 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
218d0 20 57 68 65 6e 20 61 64 64 69 6e 67 20 61 6e 20   When adding an 
218d1 69 6e 64 65 78 20 74 6f 20 74 68 65 20 6c 69 73  index to the lis
218d2 74 20 6f 66 20 69 6e 64 69 63 65 73 20 66 6f 72  t of indices for
218d3 20 61 20 74 61 62 6c 65 2c 20 6d 61 6b 65 0a 20   a table, make. 
218d4 20 2a 2a 20 73 75 72 65 20 61 6c 6c 20 69 6e 64   ** sure all ind
218d5 69 63 65 73 20 6c 61 62 65 6c 65 64 20 4f 45 5f  ices labeled OE_
218d6 52 65 70 6c 61 63 65 20 63 6f 6d 65 20 61 66 74  Replace come aft
218d7 65 72 20 61 6c 6c 20 74 68 6f 73 65 20 6c 61 62  er all those lab
218d8 65 6c 65 64 0a 20 20 2a 2a 20 4f 45 5f 49 67 6e  eled.  ** OE_Ign
218d9 6f 72 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65  ore.  This is ne
218da 63 65 73 73 61 72 79 20 66 6f 72 20 74 68 65 20  cessary for the 
218db 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f  correct operatio
218dc 6e 20 6f 66 20 55 50 44 41 54 45 0a 20 20 2a 2a  n of UPDATE.  **
218dd 20 61 6e 64 20 49 4e 53 45 52 54 2e 0a 20 20 2a   and INSERT..  *
218de 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74  /.  if( db->init
218df 2e 62 75 73 79 20 7c 7c 20 70 54 62 6c 4e 61 6d  .busy || pTblNam
218e0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  e==0 ){.    if( 
218e1 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c  onError!=OE_Repl
218e2 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e  ace || pTab->pIn
218e3 64 65 78 3d 3d 30 0a 20 20 20 20 20 20 20 20 20  dex==0.         
218e4 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 2d  || pTab->pIndex-
218e5 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70  >onError==OE_Rep
218e6 6c 61 63 65 29 7b 0a 20 20 20 20 20 20 70 49 6e  lace){.      pIn
218e7 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61  dex->pNext = pTa
218e8 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20  b->pIndex;.     
218e9 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 20 3d 20   pTab->pIndex = 
218ea 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d 65 6c 73  pIndex;.    }els
218eb 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a  e{.      Index *
218ec 70 4f 74 68 65 72 20 3d 20 70 54 61 62 2d 3e 70  pOther = pTab->p
218ed 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 77 68 69  Index;.      whi
218ee 6c 65 28 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78  le( pOther->pNex
218ef 74 20 26 26 20 70 4f 74 68 65 72 2d 3e 70 4e 65  t && pOther->pNe
218f0 78 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  xt->onError!=OE_
218f1 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20  Replace ){.     
218f2 20 20 20 70 4f 74 68 65 72 20 3d 20 70 4f 74 68     pOther = pOth
218f3 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  er->pNext;.     
218f4 20 7d 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d   }.      pIndex-
218f5 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65 72 2d  >pNext = pOther-
218f6 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 4f  >pNext;.      pO
218f7 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70 49  ther->pNext = pI
218f8 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
218f9 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 7d 0a  pIndex = 0;.  }.
218fa 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20 62  .  /* Clean up b
218fb 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f  efore exiting */
218fc 0a 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64  .exit_create_ind
218fd 65 78 3a 0a 20 20 69 66 28 20 70 49 6e 64 65 78  ex:.  if( pIndex
218fe 20 29 7b 0a 20 20 20 20 66 72 65 65 49 6e 64 65   ){.    freeInde
218ff 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20  x(pIndex);.  }. 
21900 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
21901 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74  Delete(db, pList
21902 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  );.  sqlite3SrcL
21903 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  istDelete(db, pT
21904 62 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  blName);.  sqlit
21905 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61  e3DbFree(db, zNa
21906 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  me);.  return;.}
21907 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
21908 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20 73 75   code to make su
21909 72 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d  re the file form
2190a 61 74 20 6e 75 6d 62 65 72 20 69 73 20 61 74 20  at number is at 
2190b 6c 65 61 73 74 20 6d 69 6e 46 6f 72 6d 61 74 2e  least minFormat.
2190c 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74 65  .** The generate
2190d 64 20 63 6f 64 65 20 77 69 6c 6c 20 69 6e 63 72  d code will incr
2190e 65 61 73 65 20 74 68 65 20 66 69 6c 65 20 66 6f  ease the file fo
2190f 72 6d 61 74 20 6e 75 6d 62 65 72 20 69 66 20 6e  rmat number if n
21910 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c  ecessary..*/.SQL
21911 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21912 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46   sqlite3MinimumF
21913 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65 20  ileFormat(Parse 
21914 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62  *pParse, int iDb
21915 2c 20 69 6e 74 20 6d 69 6e 46 6f 72 6d 61 74 29  , int minFormat)
21916 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 76  {.  Vdbe *v;.  v
21917 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
21918 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
21919 20 76 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 31   v ){.    int r1
2191a 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2191b 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2191c 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74    int r2 = sqlit
2191d 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2191e 72 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 6a 31  rse);.    int j1
2191f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21920 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65  eAddOp3(v, OP_Re
21921 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 72  adCookie, iDb, r
21922 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  1, 1);.    sqlit
21923 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
21924 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  v, iDb);.    sql
21925 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21926 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6d 69  , OP_Integer, mi
21927 6e 46 6f 72 6d 61 74 2c 20 72 32 29 3b 0a 20 20  nFormat, r2);.  
21928 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
21929 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 47  beAddOp3(v, OP_G
2192a 65 2c 20 72 32 2c 20 30 2c 20 72 31 29 3b 0a 20  e, r2, 0, r1);. 
2192b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2192c 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f  dOp3(v, OP_SetCo
2192d 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c 20 72 32  okie, iDb, 1, r2
2192e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2192f 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
21930 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  );.    sqlite3Re
21931 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
21932 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 73 71  rse, r1);.    sq
21933 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
21934 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b  Reg(pParse, r2);
21935 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69  .  }.}../*.** Fi
21936 6c 6c 20 74 68 65 20 49 6e 64 65 78 2e 61 69 52  ll the Index.aiR
21937 6f 77 45 73 74 5b 5d 20 61 72 72 61 79 20 77 69  owEst[] array wi
21938 74 68 20 64 65 66 61 75 6c 74 20 69 6e 66 6f 72  th default infor
21939 6d 61 74 69 6f 6e 20 2d 20 69 6e 66 6f 72 6d 61  mation - informa
2193a 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 75 73  tion.** to be us
2193b 65 64 20 77 68 65 6e 20 77 65 20 68 61 76 65 20  ed when we have 
2193c 6e 6f 74 20 72 75 6e 20 74 68 65 20 41 4e 41 4c  not run the ANAL
2193d 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  YZE command..**.
2193e 2a 2a 20 61 69 52 6f 77 45 73 74 5b 30 5d 20 69  ** aiRowEst[0] i
2193f 73 20 73 75 70 70 6f 73 65 20 74 6f 20 63 6f 6e  s suppose to con
21940 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  tain the number 
21941 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  of elements in t
21942 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 53 69 6e  he index..** Sin
21943 63 65 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f  ce we do not kno
21944 77 2c 20 67 75 65 73 73 20 31 20 6d 69 6c 6c 69  w, guess 1 milli
21945 6f 6e 2e 20 20 61 69 52 6f 77 45 73 74 5b 31 5d  on.  aiRowEst[1]
21946 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20   is an estimate 
21947 6f 66 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  of the.** number
21948 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20   of rows in the 
21949 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68  table that match
2194a 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 72 20   any particular 
2194b 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20  value of the.** 
2194c 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  first column of 
2194d 74 68 65 20 69 6e 64 65 78 2e 20 20 61 69 52 6f  the index.  aiRo
2194e 77 45 73 74 5b 32 5d 20 69 73 20 61 6e 20 65 73  wEst[2] is an es
2194f 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6e 75  timate of the nu
21950 6d 62 65 72 0a 2a 2a 20 6f 66 20 72 6f 77 73 20  mber.** of rows 
21951 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20 70  that match any p
21952 61 72 74 69 63 75 6c 61 72 20 63 6f 6d 62 69 6e  articular combin
21953 69 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 69  iation of the fi
21954 72 73 74 20 32 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  rst 2 columns.**
21955 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20   of the index.  
21956 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49  And so forth.  I
21957 74 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65  t must always be
21958 20 74 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a   the case that.*
21959 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69  .**           ai
2195a 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52 6f 77  RowEst[N]<=aiRow
2195b 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20 20 20  Est[N-1].**     
2195c 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e        aiRowEst[N
2195d 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61 72 74  ]>=1.**.** Apart
2195e 20 66 72 6f 6d 20 74 68 61 74 2c 20 77 65 20 68   from that, we h
2195f 61 76 65 20 6c 69 74 74 6c 65 20 74 6f 20 67 6f  ave little to go
21960 20 6f 6e 20 62 65 73 69 64 65 73 20 69 6e 74 75   on besides intu
21961 69 74 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a 20 68  ition as to.** h
21962 6f 77 20 61 69 52 6f 77 45 73 74 5b 5d 20 73 68  ow aiRowEst[] sh
21963 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 69  ould be initiali
21964 7a 65 64 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  zed.  The number
21965 73 20 67 65 6e 65 72 61 74 65 64 20 68 65 72 65  s generated here
21966 0a 2a 2a 20 61 72 65 20 62 61 73 65 64 20 6f 6e  .** are based on
21967 20 74 79 70 69 63 61 6c 20 76 61 6c 75 65 73 20   typical values 
21968 66 6f 75 6e 64 20 69 6e 20 61 63 74 75 61 6c 20  found in actual 
21969 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49  indices..*/.SQLI
2196a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2196b 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f  sqlite3DefaultRo
2196c 77 45 73 74 28 49 6e 64 65 78 20 2a 70 49 64 78  wEst(Index *pIdx
2196d 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a 61  ){.  unsigned *a
2196e 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77 45 73   = pIdx->aiRowEs
2196f 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73  t;.  int i;.  as
21970 73 65 72 74 28 20 61 21 3d 30 20 29 3b 0a 20 20  sert( a!=0 );.  
21971 61 5b 30 5d 20 3d 20 31 30 30 30 30 30 30 3b 0a  a[0] = 1000000;.
21972 20 20 66 6f 72 28 69 3d 70 49 64 78 2d 3e 6e 43    for(i=pIdx->nC
21973 6f 6c 75 6d 6e 3b 20 69 3e 3d 35 3b 20 69 2d 2d  olumn; i>=5; i--
21974 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20 35 3b  ){.    a[i] = 5;
21975 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 69 3e  .  }.  while( i>
21976 3d 31 20 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d  =1 ){.    a[i] =
21977 20 31 31 20 2d 20 69 3b 0a 20 20 20 20 69 2d 2d   11 - i;.    i--
21978 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78  ;.  }.  if( pIdx
21979 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
2197a 6e 65 20 29 7b 0a 20 20 20 20 61 5b 70 49 64 78  ne ){.    a[pIdx
2197b 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31 3b 0a  ->nColumn] = 1;.
2197c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
2197d 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64  s routine will d
2197e 72 6f 70 20 61 6e 20 65 78 69 73 74 69 6e 67 20  rop an existing 
2197f 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 20 54 68  named index.  Th
21980 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6d  is routine.** im
21981 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 44 52 4f  plements the DRO
21982 50 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e  P INDEX statemen
21983 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
21984 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
21985 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65  3DropIndex(Parse
21986 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73   *pParse, SrcLis
21987 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69 66  t *pName, int if
21988 45 78 69 73 74 73 29 7b 0a 20 20 49 6e 64 65 78  Exists){.  Index
21989 20 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64 62 65   *pIndex;.  Vdbe
2198a 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a   *v;.  sqlite3 *
2198b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2198c 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69  .  int iDb;..  i
2198d 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
2198e 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
2198f 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
21990 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b  exit_drop_index;
21991 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
21992 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  Name->nSrc==1 );
21993 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
21994 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  !=sqlite3ReadSch
21995 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
21996 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
21997 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70  p_index;.  }.  p
21998 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46  Index = sqlite3F
21999 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 70 4e 61  indIndex(db, pNa
2199a 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20  me->a[0].zName, 
2199b 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74  pName->a[0].zDat
2199c 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49  abase);.  if( pI
2199d 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ndex==0 ){.    i
2199e 66 28 20 21 69 66 45 78 69 73 74 73 20 29 7b 0a  f( !ifExists ){.
2199f 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
219a0 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
219a1 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20 25 53  o such index: %S
219a2 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  ", pName, 0);.  
219a3 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e    }.    pParse->
219a4 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b  checkSchema = 1;
219a5 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
219a6 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20  rop_index;.  }. 
219a7 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 61 75 74   if( pIndex->aut
219a8 6f 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71  oIndex ){.    sq
219a9 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
219aa 61 72 73 65 2c 20 22 69 6e 64 65 78 20 61 73 73  arse, "index ass
219ab 6f 63 69 61 74 65 64 20 77 69 74 68 20 55 4e 49  ociated with UNI
219ac 51 55 45 20 22 0a 20 20 20 20 20 20 22 6f 72 20  QUE ".      "or 
219ad 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73  PRIMARY KEY cons
219ae 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74 20 62 65  traint cannot be
219af 20 64 72 6f 70 70 65 64 22 2c 20 30 29 3b 0a 20   dropped", 0);. 
219b0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
219b1 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69  p_index;.  }.  i
219b2 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
219b3 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 49  maToIndex(db, pI
219b4 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  ndex->pSchema);.
219b5 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
219b6 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
219b7 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f  N.  {.    int co
219b8 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50  de = SQLITE_DROP
219b9 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54 61 62 6c  _INDEX;.    Tabl
219ba 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64 65 78  e *pTab = pIndex
219bb 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 63 6f  ->pTable;.    co
219bc 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
219bd 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
219be 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  me;.    const ch
219bf 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d  ar *zTab = SCHEM
219c0 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20  A_TABLE(iDb);.  
219c1 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
219c2 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
219c3 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54  QLITE_DELETE, zT
219c4 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20  ab, 0, zDb) ){. 
219c5 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64       goto exit_d
219c6 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d  rop_index;.    }
219c7 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  .    if( !OMIT_T
219c8 45 4d 50 44 42 20 26 26 20 69 44 62 20 29 20 63  EMPDB && iDb ) c
219c9 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
219ca 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20  P_TEMP_INDEX;.  
219cb 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
219cc 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63  hCheck(pParse, c
219cd 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61  ode, pIndex->zNa
219ce 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  me, pTab->zName,
219cf 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67   zDb) ){.      g
219d0 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e  oto exit_drop_in
219d1 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  dex;.    }.  }.#
219d2 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65  endif..  /* Gene
219d3 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d  rate code to rem
219d4 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 61 6e  ove the index an
219d5 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74 65  d from the maste
219d6 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 20 3d  r table */.  v =
219d7 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
219d8 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
219d9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
219da 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
219db 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44  on(pParse, 1, iD
219dc 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e  b);.    sqlite3N
219dd 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
219de 65 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45 54  e,.       "DELET
219df 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45  E FROM %Q.%s WHE
219e0 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20  RE name=%Q",.   
219e1 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d      db->aDb[iDb]
219e2 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54  .zName, SCHEMA_T
219e3 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 20  ABLE(iDb),.     
219e4 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a    pIndex->zName.
219e5 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 73      );.    if( s
219e6 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
219e7 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74  db, "sqlite_stat
219e8 31 22 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  1", db->aDb[iDb]
219e9 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20  .zName) ){.     
219ea 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
219eb 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
219ec 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
219ed 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31   %Q.sqlite_stat1
219ee 20 57 48 45 52 45 20 69 64 78 3d 25 51 22 2c 0a   WHERE idx=%Q",.
219ef 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b          db->aDb[
219f0 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 70 49 6e 64  iDb].zName, pInd
219f1 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20  ex->zName.      
219f2 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
219f3 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
219f4 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
219f5 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61     destroyRootPa
219f6 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65  ge(pParse, pInde
219f7 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20  x->tnum, iDb);. 
219f8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
219f9 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 49  dOp4(v, OP_DropI
219fa 6e 64 65 78 2c 20 69 44 62 2c 20 30 2c 20 30 2c  ndex, iDb, 0, 0,
219fb 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20   pIndex->zName, 
219fc 30 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 72  0);.  }..exit_dr
219fd 6f 70 5f 69 6e 64 65 78 3a 0a 20 20 73 71 6c 69  op_index:.  sqli
219fe 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
219ff 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a  (db, pName);.}..
21a00 2f 2a 0a 2a 2a 20 70 41 72 72 61 79 20 69 73 20  /*.** pArray is 
21a01 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
21a02 61 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73  array of objects
21a03 2e 20 20 45 61 63 68 20 6f 62 6a 65 63 74 20 69  .  Each object i
21a04 6e 20 74 68 65 0a 2a 2a 20 61 72 72 61 79 20 69  n the.** array i
21a05 73 20 73 7a 45 6e 74 72 79 20 62 79 74 65 73 20  s szEntry bytes 
21a06 69 6e 20 73 69 7a 65 2e 20 20 54 68 69 73 20 72  in size.  This r
21a07 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
21a08 20 61 20 6e 65 77 0a 2a 2a 20 6f 62 6a 65 63 74   a new.** object
21a09 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 74   on the end of t
21a0a 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20  he array..**.** 
21a0b 2a 70 6e 45 6e 74 72 79 20 69 73 20 74 68 65 20  *pnEntry is the 
21a0c 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
21a0d 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73 65  s already in use
21a0e 2e 20 20 2a 70 6e 41 6c 6c 6f 63 20 69 73 0a 2a  .  *pnAlloc is.*
21a0f 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  * the previously
21a10 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   allocated size 
21a11 6f 66 20 74 68 65 20 61 72 72 61 79 2e 20 20 69  of the array.  i
21a12 6e 69 74 53 69 7a 65 20 69 73 20 74 68 65 0a 2a  nitSize is the.*
21a13 2a 20 73 75 67 67 65 73 74 65 64 20 69 6e 69 74  * suggested init
21a14 69 61 6c 20 61 72 72 61 79 20 73 69 7a 65 20 61  ial array size a
21a15 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  llocation..**.**
21a16 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   The index of th
21a17 65 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 72  e new entry is r
21a18 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 49 64 78  eturned in *pIdx
21a19 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
21a1a 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70  tine returns a p
21a1b 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 72  ointer to the ar
21a1c 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20  ray of objects. 
21a1d 20 54 68 69 73 0a 2a 2a 20 6d 69 67 68 74 20 62   This.** might b
21a1e 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  e the same as th
21a1f 65 20 70 41 72 72 61 79 20 70 61 72 61 6d 65 74  e pArray paramet
21a20 65 72 20 6f 72 20 69 74 20 6d 69 67 68 74 20 62  er or it might b
21a21 65 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a  e a different.**
21a22 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68 65 20   pointer if the 
21a23 61 72 72 61 79 20 77 61 73 20 72 65 73 69 7a 65  array was resize
21a24 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
21a25 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
21a26 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28  e3ArrayAllocate(
21a27 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
21a28 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
21a29 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20  on to notify of 
21a2a 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20  malloc failures 
21a2b 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 72 61  */.  void *pArra
21a2c 79 2c 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20  y,     /* Array 
21a2d 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 4d 69 67  of objects.  Mig
21a2e 68 74 20 62 65 20 72 65 61 6c 6c 6f 63 61 74 65  ht be reallocate
21a2f 64 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 6e 74  d */.  int szEnt
21a30 72 79 2c 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  ry,      /* Size
21a31 20 6f 66 20 65 61 63 68 20 6f 62 6a 65 63 74 20   of each object 
21a32 69 6e 20 74 68 65 20 61 72 72 61 79 20 2a 2f 0a  in the array */.
21a33 20 20 69 6e 74 20 69 6e 69 74 53 69 7a 65 2c 20    int initSize, 
21a34 20 20 20 20 2f 2a 20 53 75 67 67 65 73 74 65 64      /* Suggested
21a35 20 69 6e 69 74 69 61 6c 20 61 6c 6c 6f 63 61 74   initial allocat
21a36 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73  ion, in elements
21a37 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 6e 74   */.  int *pnEnt
21a38 72 79 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ry,     /* Numbe
21a39 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 63 75 72  r of objects cur
21a3a 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f  rently in use */
21a3b 0a 20 20 69 6e 74 20 2a 70 6e 41 6c 6c 6f 63 2c  .  int *pnAlloc,
21a3c 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
21a3d 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f  size of the allo
21a3e 63 61 74 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65  cation, in eleme
21a3f 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 49  nts */.  int *pI
21a40 64 78 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  dx         /* Wr
21a41 69 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f 66  ite the index of
21a42 20 61 20 6e 65 77 20 73 6c 6f 74 20 68 65 72 65   a new slot here
21a43 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a   */.){.  char *z
21a44 3b 0a 20 20 69 66 28 20 2a 70 6e 45 6e 74 72 79  ;.  if( *pnEntry
21a45 20 3e 3d 20 2a 70 6e 41 6c 6c 6f 63 20 29 7b 0a   >= *pnAlloc ){.
21a46 20 20 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a      void *pNew;.
21a47 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b      int newSize;
21a48 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 28  .    newSize = (
21a49 2a 70 6e 41 6c 6c 6f 63 29 2a 32 20 2b 20 69 6e  *pnAlloc)*2 + in
21a4a 69 74 53 69 7a 65 3b 0a 20 20 20 20 70 4e 65 77  itSize;.    pNew
21a4b 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
21a4c 6c 6f 63 28 64 62 2c 20 70 41 72 72 61 79 2c 20  loc(db, pArray, 
21a4d 6e 65 77 53 69 7a 65 2a 73 7a 45 6e 74 72 79 29  newSize*szEntry)
21a4e 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d  ;.    if( pNew==
21a4f 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 49 64 78  0 ){.      *pIdx
21a50 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74   = -1;.      ret
21a51 75 72 6e 20 70 41 72 72 61 79 3b 0a 20 20 20 20  urn pArray;.    
21a52 7d 0a 20 20 20 20 2a 70 6e 41 6c 6c 6f 63 20 3d  }.    *pnAlloc =
21a53 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
21a54 53 69 7a 65 28 64 62 2c 20 70 4e 65 77 29 2f 73  Size(db, pNew)/s
21a55 7a 45 6e 74 72 79 3b 0a 20 20 20 20 70 41 72 72  zEntry;.    pArr
21a56 61 79 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20  ay = pNew;.  }. 
21a57 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 41 72 72   z = (char*)pArr
21a58 61 79 3b 0a 20 20 6d 65 6d 73 65 74 28 26 7a 5b  ay;.  memset(&z[
21a59 2a 70 6e 45 6e 74 72 79 20 2a 20 73 7a 45 6e 74  *pnEntry * szEnt
21a5a 72 79 5d 2c 20 30 2c 20 73 7a 45 6e 74 72 79 29  ry], 0, szEntry)
21a5b 3b 0a 20 20 2a 70 49 64 78 20 3d 20 2a 70 6e 45  ;.  *pIdx = *pnE
21a5c 6e 74 72 79 3b 0a 20 20 2b 2b 2a 70 6e 45 6e 74  ntry;.  ++*pnEnt
21a5d 72 79 3b 0a 20 20 72 65 74 75 72 6e 20 70 41 72  ry;.  return pAr
21a5e 72 61 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70  ray;.}../*.** Ap
21a5f 70 65 6e 64 20 61 20 6e 65 77 20 65 6c 65 6d 65  pend a new eleme
21a60 6e 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20  nt to the given 
21a61 49 64 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20  IdList.  Create 
21a62 61 20 6e 65 77 20 49 64 4c 69 73 74 20 69 66 0a  a new IdList if.
21a63 2a 2a 20 6e 65 65 64 20 62 65 2e 0a 2a 2a 0a 2a  ** need be..**.*
21a64 2a 20 41 20 6e 65 77 20 49 64 4c 69 73 74 20 69  * A new IdList i
21a65 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e  s returned, or N
21a66 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20  ULL if malloc() 
21a67 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fails..*/.SQLITE
21a68 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20  _PRIVATE IdList 
21a69 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70  *sqlite3IdListAp
21a6a 70 65 6e 64 28 73 71 6c 69 74 65 33 20 2a 64 62  pend(sqlite3 *db
21a6b 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c  , IdList *pList,
21a6c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b   Token *pToken){
21a6d 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
21a6e 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pList==0 ){.    
21a6f 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44  pList = sqlite3D
21a70 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
21a71 73 69 7a 65 6f 66 28 49 64 4c 69 73 74 29 20 29  sizeof(IdList) )
21a72 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d  ;.    if( pList=
21a73 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
21a74 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63     pList->nAlloc
21a75 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73   = 0;.  }.  pLis
21a76 74 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 41 72  t->a = sqlite3Ar
21a77 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20  rayAllocate(.   
21a78 20 20 20 64 62 2c 0a 20 20 20 20 20 20 70 4c 69     db,.      pLi
21a79 73 74 2d 3e 61 2c 0a 20 20 20 20 20 20 73 69 7a  st->a,.      siz
21a7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29  eof(pList->a[0])
21a7b 2c 0a 20 20 20 20 20 20 35 2c 0a 20 20 20 20 20  ,.      5,.     
21a7c 20 26 70 4c 69 73 74 2d 3e 6e 49 64 2c 0a 20 20   &pList->nId,.  
21a7d 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e 41 6c 6c      &pList->nAll
21a7e 6f 63 2c 0a 20 20 20 20 20 20 26 69 0a 20 20 29  oc,.      &i.  )
21a7f 3b 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a 20  ;.  if( i<0 ){. 
21a80 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
21a81 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74  Delete(db, pList
21a82 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
21a83 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 5b  .  }.  pList->a[
21a84 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  i].zName = sqlit
21a85 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
21a86 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 72  db, pToken);.  r
21a87 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a  eturn pList;.}..
21a88 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20  /*.** Delete an 
21a89 49 64 4c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54  IdList..*/.SQLIT
21a8a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21a8b 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
21a8c 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
21a8d 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a  IdList *pList){.
21a8e 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70    int i;.  if( p
21a8f 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
21a90 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
21a91 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b  List->nId; i++){
21a92 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
21a93 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b  ee(db, pList->a[
21a94 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20  i].zName);.  }. 
21a95 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
21a96 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 20  b, pList->a);.  
21a97 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
21a98 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a  , pList);.}../*.
21a99 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e  ** Return the in
21a9a 64 65 78 20 69 6e 20 70 4c 69 73 74 20 6f 66 20  dex in pList of 
21a9b 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 6e  the identifier n
21a9c 61 6d 65 64 20 7a 49 64 2e 20 20 52 65 74 75 72  amed zId.  Retur
21a9d 6e 20 2d 31 0a 2a 2a 20 69 66 20 6e 6f 74 20 66  n -1.** if not f
21a9e 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ound..*/.SQLITE_
21a9f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
21aa0 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49  te3IdListIndex(I
21aa1 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 63 6f  dList *pList, co
21aa2 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29  nst char *zName)
21aa3 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
21aa4 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
21aa5 72 6e 20 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30  rn -1;.  for(i=0
21aa6 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20  ; i<pList->nId; 
21aa7 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71  i++){.    if( sq
21aa8 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 4c 69  lite3StrICmp(pLi
21aa9 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  st->a[i].zName, 
21aaa 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 72 65 74 75  zName)==0 ) retu
21aab 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn i;.  }.  retu
21aac 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn -1;.}../*.** 
21aad 45 78 70 61 6e 64 20 74 68 65 20 73 70 61 63 65  Expand the space
21aae 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
21aaf 68 65 20 67 69 76 65 6e 20 53 72 63 4c 69 73 74  he given SrcList
21ab0 20 6f 62 6a 65 63 74 20 62 79 0a 2a 2a 20 63 72   object by.** cr
21ab1 65 61 74 69 6e 67 20 6e 45 78 74 72 61 20 6e 65  eating nExtra ne
21ab2 77 20 73 6c 6f 74 73 20 62 65 67 69 6e 6e 69 6e  w slots beginnin
21ab3 67 20 61 74 20 69 53 74 61 72 74 2e 20 20 69 53  g at iStart.  iS
21ab4 74 61 72 74 20 69 73 20 7a 65 72 6f 20 62 61 73  tart is zero bas
21ab5 65 64 2e 0a 2a 2a 20 4e 65 77 20 73 6c 6f 74 73  ed..** New slots
21ab6 20 61 72 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a   are zeroed..**.
21ab7 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ** For example, 
21ab8 73 75 70 70 6f 73 65 20 61 20 53 72 63 4c 69 73  suppose a SrcLis
21ab9 74 20 69 6e 69 74 69 61 6c 6c 79 20 63 6f 6e 74  t initially cont
21aba 61 69 6e 73 20 74 77 6f 20 65 6e 74 72 69 65 73  ains two entries
21abb 3a 20 41 2c 42 2e 0a 2a 2a 20 54 6f 20 61 70 70  : A,B..** To app
21abc 65 6e 64 20 33 20 6e 65 77 20 65 6e 74 72 69 65  end 3 new entrie
21abd 73 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 2c 20  s onto the end, 
21abe 64 6f 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  do this:.**.**  
21abf 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
21ac0 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 53 72 63  Enlarge(db, pSrc
21ac1 6c 69 73 74 2c 20 33 2c 20 32 29 3b 0a 2a 2a 0a  list, 3, 2);.**.
21ac2 2a 2a 20 41 66 74 65 72 20 74 68 65 20 63 61 6c  ** After the cal
21ac3 6c 20 61 62 6f 76 65 20 69 74 20 77 6f 75 6c 64  l above it would
21ac4 20 63 6f 6e 74 61 69 6e 3a 20 20 41 2c 20 42 2c   contain:  A, B,
21ac5 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2e 0a   nil, nil, nil..
21ac6 2a 2a 20 49 66 20 74 68 65 20 69 53 74 61 72 74  ** If the iStart
21ac7 20 61 72 67 75 6d 65 6e 74 20 68 61 64 20 62 65   argument had be
21ac8 65 6e 20 31 20 69 6e 73 74 65 61 64 20 6f 66 20  en 1 instead of 
21ac9 32 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  2, then the resu
21aca 6c 74 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65  lt.** would have
21acb 20 62 65 65 6e 3a 20 20 41 2c 20 6e 69 6c 2c 20   been:  A, nil, 
21acc 6e 69 6c 2c 20 6e 69 6c 2c 20 42 2e 20 20 54 6f  nil, nil, B.  To
21acd 20 70 72 65 70 65 6e 64 20 74 68 65 20 6e 65 77   prepend the new
21ace 20 73 6c 6f 74 73 2c 0a 2a 2a 20 74 68 65 20 69   slots,.** the i
21acf 53 74 61 72 74 20 76 61 6c 75 65 20 77 6f 75 6c  Start value woul
21ad0 64 20 62 65 20 30 2e 20 20 54 68 65 20 72 65 73  d be 0.  The res
21ad1 75 6c 74 20 74 68 65 6e 20 77 6f 75 6c 64 0a 2a  ult then would.*
21ad2 2a 20 62 65 3a 20 6e 69 6c 2c 20 6e 69 6c 2c 20  * be: nil, nil, 
21ad3 6e 69 6c 2c 20 41 2c 20 42 2e 0a 2a 2a 0a 2a 2a  nil, A, B..**.**
21ad4 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   If a memory all
21ad5 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 74 68  ocation fails th
21ad6 65 20 53 72 63 4c 69 73 74 20 69 73 20 75 6e 63  e SrcList is unc
21ad7 68 61 6e 67 65 64 2e 20 20 54 68 65 0a 2a 2a 20  hanged.  The.** 
21ad8 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
21ad9 20 66 6c 61 67 20 77 69 6c 6c 20 62 65 20 73 65   flag will be se
21ada 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2f 0a 53 51  t to true..*/.SQ
21adb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63  LITE_PRIVATE Src
21adc 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63  List *sqlite3Src
21add 4c 69 73 74 45 6e 6c 61 72 67 65 28 0a 20 20 73  ListEnlarge(.  s
21ade 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
21adf 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
21ae0 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69  nnection to noti
21ae1 66 79 20 6f 66 20 4f 4f 4d 20 65 72 72 6f 72 73  fy of OOM errors
21ae2 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
21ae3 53 72 63 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  Src,     /* The 
21ae4 53 72 63 4c 69 73 74 20 74 6f 20 62 65 20 65 6e  SrcList to be en
21ae5 6c 61 72 67 65 64 20 2a 2f 0a 20 20 69 6e 74 20  larged */.  int 
21ae6 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 2f  nExtra,        /
21ae7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 77 20  * Number of new 
21ae8 73 6c 6f 74 73 20 74 6f 20 61 64 64 20 74 6f 20  slots to add to 
21ae9 70 53 72 63 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 69  pSrc->a[] */.  i
21aea 6e 74 20 69 53 74 61 72 74 20 20 20 20 20 20 20  nt iStart       
21aeb 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 53    /* Index in pS
21aec 72 63 2d 3e 61 5b 5d 20 6f 66 20 66 69 72 73 74  rc->a[] of first
21aed 20 6e 65 77 20 73 6c 6f 74 20 2a 2f 0a 29 7b 0a   new slot */.){.
21aee 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f 2a 20 53    int i;..  /* S
21aef 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f  anity checking o
21af0 6e 20 63 61 6c 6c 69 6e 67 20 70 61 72 61 6d 65  n calling parame
21af1 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74  ters */.  assert
21af2 28 20 69 53 74 61 72 74 3e 3d 30 20 29 3b 0a 20  ( iStart>=0 );. 
21af3 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61 3e   assert( nExtra>
21af4 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63  =1 );.  if( pSrc
21af5 3d 3d 30 20 7c 7c 20 69 53 74 61 72 74 3e 70 53  ==0 || iStart>pS
21af6 72 63 2d 3e 6e 53 72 63 20 29 7b 0a 20 20 20 20  rc->nSrc ){.    
21af7 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c  assert( db->mall
21af8 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
21af9 72 65 74 75 72 6e 20 70 53 72 63 3b 0a 20 20 7d  return pSrc;.  }
21afa 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
21afb 61 64 64 69 74 69 6f 6e 61 6c 20 73 70 61 63 65  additional space
21afc 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20   if needed */.  
21afd 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e  if( pSrc->nSrc+n
21afe 45 78 74 72 61 3e 70 53 72 63 2d 3e 6e 41 6c 6c  Extra>pSrc->nAll
21aff 6f 63 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73  oc ){.    SrcLis
21b00 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74  t *pNew;.    int
21b01 20 6e 41 6c 6c 6f 63 20 3d 20 70 53 72 63 2d 3e   nAlloc = pSrc->
21b02 6e 53 72 63 2b 6e 45 78 74 72 61 3b 0a 20 20 20  nSrc+nExtra;.   
21b03 20 69 6e 74 20 6e 47 6f 74 3b 0a 20 20 20 20 70   int nGot;.    p
21b04 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52  New = sqlite3DbR
21b05 65 61 6c 6c 6f 63 28 64 62 2c 20 70 53 72 63 2c  ealloc(db, pSrc,
21b06 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21b07 73 69 7a 65 6f 66 28 2a 70 53 72 63 29 20 2b 20  sizeof(*pSrc) + 
21b08 28 6e 41 6c 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f  (nAlloc-1)*sizeo
21b09 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 20 29 3b  f(pSrc->a[0]) );
21b0a 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  .    if( pNew==0
21b0b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
21b0c 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
21b0d 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ed );.      retu
21b0e 72 6e 20 70 53 72 63 3b 0a 20 20 20 20 7d 0a 20  rn pSrc;.    }. 
21b0f 20 20 20 70 53 72 63 20 3d 20 70 4e 65 77 3b 0a     pSrc = pNew;.
21b10 20 20 20 20 6e 47 6f 74 20 3d 20 28 73 71 6c 69      nGot = (sqli
21b11 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
21b12 64 62 2c 20 70 4e 65 77 29 20 2d 20 73 69 7a 65  db, pNew) - size
21b13 6f 66 28 2a 70 53 72 63 29 29 2f 73 69 7a 65 6f  of(*pSrc))/sizeo
21b14 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2b 31 3b  f(pSrc->a[0])+1;
21b15 0a 20 20 20 20 70 53 72 63 2d 3e 6e 41 6c 6c 6f  .    pSrc->nAllo
21b16 63 20 3d 20 28 75 31 36 29 6e 47 6f 74 3b 0a 20  c = (u16)nGot;. 
21b17 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 65 78   }..  /* Move ex
21b18 69 73 74 69 6e 67 20 73 6c 6f 74 73 20 74 68 61  isting slots tha
21b19 74 20 63 6f 6d 65 20 61 66 74 65 72 20 74 68 65  t come after the
21b1a 20 6e 65 77 6c 79 20 69 6e 73 65 72 74 65 64 20   newly inserted 
21b1b 73 6c 6f 74 73 0a 20 20 2a 2a 20 6f 75 74 20 6f  slots.  ** out o
21b1c 66 20 74 68 65 20 77 61 79 20 2a 2f 0a 20 20 66  f the way */.  f
21b1d 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2d  or(i=pSrc->nSrc-
21b1e 31 3b 20 69 3e 3d 69 53 74 61 72 74 3b 20 69 2d  1; i>=iStart; i-
21b1f 2d 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b  -){.    pSrc->a[
21b20 69 2b 6e 45 78 74 72 61 5d 20 3d 20 70 53 72 63  i+nExtra] = pSrc
21b21 2d 3e 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 70 53  ->a[i];.  }.  pS
21b22 72 63 2d 3e 6e 53 72 63 20 2b 3d 20 28 69 31 36  rc->nSrc += (i16
21b23 29 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a 20 5a  )nExtra;..  /* Z
21b24 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20 61 6c  ero the newly al
21b25 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 2a 2f  located slots */
21b26 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72 63 2d  .  memset(&pSrc-
21b27 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c 20 73  >a[iStart], 0, s
21b28 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d  izeof(pSrc->a[0]
21b29 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66 6f 72  )*nExtra);.  for
21b2a 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69 53 74  (i=iStart; i<iSt
21b2b 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b 2b 29  art+nExtra; i++)
21b2c 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 5d  {.    pSrc->a[i]
21b2d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20  .iCursor = -1;. 
21b2e 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20   }..  /* Return 
21b2f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
21b30 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c 69 73   enlarged SrcLis
21b31 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 53  t */.  return pS
21b32 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 70  rc;.}.../*.** Ap
21b33 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62 6c 65  pend a new table
21b34 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67 69 76   name to the giv
21b35 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43 72 65  en SrcList.  Cre
21b36 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c 69 73  ate a new SrcLis
21b37 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e  t if.** need be.
21b38 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73    A new entry is
21b39 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20   created in the 
21b3a 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69 66 20  SrcList even if 
21b3b 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 2e 0a  pToken is NULL..
21b3c 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73 74 20  **.** A SrcList 
21b3d 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20  is returned, or 
21b3e 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
21b3f 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20   an OOM error.  
21b40 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  The returned.** 
21b41 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 62 65  SrcList might be
21b42 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
21b43 20 53 72 63 4c 69 73 74 20 74 68 61 74 20 77 61   SrcList that wa
21b44 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20 6d 69  s input or it mi
21b45 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65 77 20  ght be.** a new 
21b46 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f 4d 20  one.  If an OOM 
21b47 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63 75 72  error does occur
21b48 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f  s, then the prio
21b49 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69 73 74  r value of pList
21b4a 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 70 75  .** that is inpu
21b4b 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  t to this routin
21b4c 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  e is automatical
21b4d 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20  ly freed..**.** 
21b4e 49 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20  If pDatabase is 
21b4f 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65 61  not null, it mea
21b50 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c  ns that the tabl
21b51 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e 61  e has an optiona
21b52 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6e 61  l.** database na
21b53 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69 6b 65  me prefix.  Like
21b54 20 74 68 69 73 3a 20 20 22 64 61 74 61 62 61 73   this:  "databas
21b55 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65 20 70  e.table".  The p
21b56 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f 69 6e  Database.** poin
21b57 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ts to the table 
21b58 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70 54 61  name and the pTa
21b59 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ble points to th
21b5a 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e  e database name.
21b5b 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73 74 2e  .** The SrcList.
21b5c 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c 64 20  a[].zName field 
21b5d 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74  is filled with t
21b5e 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 68  he table name wh
21b5f 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d  ich might.** com
21b60 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20 28 69  e from pTable (i
21b61 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20 4e  f pDatabase is N
21b62 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70 44 61  ULL) or from pDa
21b63 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53 72 63  tabase.  .** Src
21b64 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 62 61  List.a[].zDataba
21b65 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74  se is filled wit
21b66 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  h the database n
21b67 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 2c  ame from pTable,
21b68 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55 4c 4c  .** or with NULL
21b69 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73 65 20   if no database 
21b6a 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a  is specified..**
21b6b 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
21b6c 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69 6b 65  ds, if call like
21b6d 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
21b6e 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c       sqlite3SrcL
21b6f 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c  istAppend(D,A,B,
21b70 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42  0);.**.** Then B
21b71 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65   is a table name
21b72 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73   and the databas
21b73 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70 65 63  e name is unspec
21b74 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c 6c 65  ified.  If calle
21b75 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a  d.** like this:.
21b76 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71  **.**         sq
21b77 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
21b78 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a  nd(D,A,B,C);.**.
21b79 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74 68 65  ** Then C is the
21b7a 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20   table name and 
21b7b 42 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  B is the databas
21b7c 65 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54  e name..*/.SQLIT
21b7d 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73  E_PRIVATE SrcLis
21b7e 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  t *sqlite3SrcLis
21b7f 74 41 70 70 65 6e 64 28 0a 20 20 73 71 6c 69 74  tAppend(.  sqlit
21b80 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f  e3 *db,        /
21b81 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  * Connection to 
21b82 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63  notify of malloc
21b83 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 53   failures */.  S
21b84 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  rcList *pList,  
21b85 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 6f 20     /* Append to 
21b86 74 68 69 73 20 53 72 63 4c 69 73 74 2e 20 4e 55  this SrcList. NU
21b87 4c 4c 20 63 72 65 61 74 65 73 20 61 20 6e 65 77  LL creates a new
21b88 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 54 6f   SrcList */.  To
21b89 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20  ken *pTable,    
21b8a 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 61 70    /* Table to ap
21b8b 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  pend */.  Token 
21b8c 2a 70 44 61 74 61 62 61 73 65 20 20 20 20 2f 2a  *pDatabase    /*
21b8d 20 44 61 74 61 62 61 73 65 20 6f 66 20 74 68 65   Database of the
21b8e 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73   table */.){.  s
21b8f 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
21b90 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28  em *pItem;.  if(
21b91 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20   pList==0 ){.   
21b92 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
21b93 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
21b94 20 73 69 7a 65 6f 66 28 53 72 63 4c 69 73 74 29   sizeof(SrcList)
21b95 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73   );.    if( pLis
21b96 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  t==0 ) return 0;
21b97 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c  .    pList->nAll
21b98 6f 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 4c  oc = 1;.  }.  pL
21b99 69 73 74 20 3d 20 73 71 6c 69 74 65 33 53 72 63  ist = sqlite3Src
21b9a 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c 20  ListEnlarge(db, 
21b9b 70 4c 69 73 74 2c 20 31 2c 20 70 4c 69 73 74 2d  pList, 1, pList-
21b9c 3e 6e 53 72 63 29 3b 0a 20 20 69 66 28 20 64 62  >nSrc);.  if( db
21b9d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
21b9e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63  {.    sqlite3Src
21b9f 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
21ba0 4c 69 73 74 29 3b 0a 20 20 20 20 72 65 74 75 72  List);.    retur
21ba1 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d  n 0;.  }.  pItem
21ba2 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69   = &pList->a[pLi
21ba3 73 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 69  st->nSrc-1];.  i
21ba4 66 28 20 70 44 61 74 61 62 61 73 65 20 26 26 20  f( pDatabase && 
21ba5 70 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d 30 20  pDatabase->z==0 
21ba6 29 7b 0a 20 20 20 20 70 44 61 74 61 62 61 73 65  ){.    pDatabase
21ba7 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
21ba8 70 44 61 74 61 62 61 73 65 20 26 26 20 70 54 61  pDatabase && pTa
21ba9 62 6c 65 20 29 7b 0a 20 20 20 20 54 6f 6b 65 6e  ble ){.    Token
21baa 20 2a 70 54 65 6d 70 20 3d 20 70 44 61 74 61 62   *pTemp = pDatab
21bab 61 73 65 3b 0a 20 20 20 20 70 44 61 74 61 62 61  ase;.    pDataba
21bac 73 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 20  se = pTable;.   
21bad 20 70 54 61 62 6c 65 20 3d 20 70 54 65 6d 70 3b   pTable = pTemp;
21bae 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e  .  }.  pItem->zN
21baf 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
21bb0 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
21bb1 54 61 62 6c 65 29 3b 0a 20 20 70 49 74 65 6d 2d  Table);.  pItem-
21bb2 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c  >zDatabase = sql
21bb3 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
21bb4 6e 28 64 62 2c 20 70 44 61 74 61 62 61 73 65 29  n(db, pDatabase)
21bb5 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74  ;.  return pList
21bb6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67  ;.}../*.** Assig
21bb7 6e 20 56 64 62 65 43 75 72 73 6f 72 20 69 6e 64  n VdbeCursor ind
21bb8 65 78 20 6e 75 6d 62 65 72 73 20 74 6f 20 61 6c  ex numbers to al
21bb9 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 20 53 72  l tables in a Sr
21bba 63 4c 69 73 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f  cList.*/.SQLITE_
21bbb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21bbc 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67  ite3SrcListAssig
21bbd 6e 43 75 72 73 6f 72 73 28 50 61 72 73 65 20 2a  nCursors(Parse *
21bbe 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20  pParse, SrcList 
21bbf 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69  *pList){.  int i
21bc0 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  ;.  struct SrcLi
21bc1 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
21bc2 20 20 61 73 73 65 72 74 28 70 4c 69 73 74 20 7c    assert(pList |
21bc3 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
21bc4 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
21bc5 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20  if( pList ){.   
21bc6 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d   for(i=0, pItem=
21bc7 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73  pList->a; i<pLis
21bc8 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49  t->nSrc; i++, pI
21bc9 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  tem++){.      if
21bca 28 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  ( pItem->iCursor
21bcb 3e 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  >=0 ) break;.   
21bcc 20 20 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f     pItem->iCurso
21bcd 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
21bce 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  ++;.      if( pI
21bcf 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  tem->pSelect ){.
21bd0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
21bd1 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73  rcListAssignCurs
21bd2 6f 72 73 28 70 50 61 72 73 65 2c 20 70 49 74 65  ors(pParse, pIte
21bd3 6d 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 53 72 63  m->pSelect->pSrc
21bd4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
21bd5 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  .  }.}../*.** De
21bd6 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 53  lete an entire S
21bd7 72 63 4c 69 73 74 20 69 6e 63 6c 75 64 69 6e 67  rcList including
21bd8 20 61 6c 6c 20 69 74 73 20 73 75 62 73 74 72 75   all its substru
21bd9 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
21bda 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
21bdb 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
21bdc 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
21bdd 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  SrcList *pList){
21bde 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
21bdf 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
21be0 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c  *pItem;.  if( pL
21be1 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ist==0 ) return;
21be2 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69  .  for(pItem=pLi
21be3 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c  st->a, i=0; i<pL
21be4 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
21be5 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71  pItem++){.    sq
21be6 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
21be7 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  pItem->zDatabase
21be8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
21be9 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e  Free(db, pItem->
21bea 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69  zName);.    sqli
21beb 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
21bec 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20  tem->zAlias);.  
21bed 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
21bee 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65  db, pItem->zInde
21bef 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  x);.    sqlite3D
21bf0 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65 6d  eleteTable(pItem
21bf1 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20 73 71 6c  ->pTab);.    sql
21bf2 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
21bf3 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c  (db, pItem->pSel
21bf4 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ect);.    sqlite
21bf5 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
21bf6 70 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20  pItem->pOn);.   
21bf7 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
21bf8 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e  lete(db, pItem->
21bf9 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 73  pUsing);.  }.  s
21bfa 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
21bfb 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   pList);.}../*.*
21bfc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
21bfd 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
21bfe 70 61 72 73 65 72 20 74 6f 20 61 64 64 20 61 20  parser to add a 
21bff 6e 65 77 20 74 65 72 6d 20 74 6f 20 74 68 65 0a  new term to the.
21c00 2a 2a 20 65 6e 64 20 6f 66 20 61 20 67 72 6f 77  ** end of a grow
21c01 69 6e 67 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  ing FROM clause.
21c02 20 20 54 68 65 20 22 70 22 20 70 61 72 61 6d 65    The "p" parame
21c03 74 65 72 20 69 73 20 74 68 65 20 70 61 72 74 20  ter is the part 
21c04 6f 66 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63  of.** the FROM c
21c05 6c 61 75 73 65 20 74 68 61 74 20 68 61 73 20 61  lause that has a
21c06 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6e 73  lready been cons
21c07 74 72 75 63 74 65 64 2e 20 20 22 70 22 20 69 73  tructed.  "p" is
21c08 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 74 68 69 73   NULL.** if this
21c09 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 65   is the first te
21c0a 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  rm of the FROM c
21c0b 6c 61 75 73 65 2e 20 20 70 54 61 62 6c 65 20 61  lause.  pTable a
21c0c 6e 64 20 70 44 61 74 61 62 61 73 65 0a 2a 2a 20  nd pDatabase.** 
21c0d 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  are the name of 
21c0e 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 64 61  the table and da
21c0f 74 61 62 61 73 65 20 6e 61 6d 65 64 20 69 6e 20  tabase named in 
21c10 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
21c11 74 65 72 6d 2e 0a 2a 2a 20 70 44 61 74 61 62 61  term..** pDataba
21c12 73 65 20 69 73 20 4e 55 4c 4c 20 69 66 20 74 68  se is NULL if th
21c13 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
21c14 71 75 61 6c 69 66 69 65 72 20 69 73 20 6d 69 73  qualifier is mis
21c15 73 69 6e 67 20 2d 20 74 68 65 0a 2a 2a 20 75 73  sing - the.** us
21c16 75 61 6c 20 63 61 73 65 2e 20 20 49 66 20 74 68  ual case.  If th
21c17 65 20 74 65 72 6d 20 68 61 73 20 61 20 61 6c 69  e term has a ali
21c18 61 73 2c 20 74 68 65 6e 20 70 41 6c 69 61 73 20  as, then pAlias 
21c19 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a  points to the.**
21c1a 20 61 6c 69 61 73 20 74 6f 6b 65 6e 2e 20 20 49   alias token.  I
21c1b 66 20 74 68 65 20 74 65 72 6d 20 69 73 20 61 20  f the term is a 
21c1c 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e 20 70  subquery, then p
21c1d 53 75 62 71 75 65 72 79 20 69 73 20 74 68 65 0a  Subquery is the.
21c1e 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
21c1f 65 6e 74 20 74 68 61 74 20 74 68 65 20 73 75 62  ent that the sub
21c20 71 75 65 72 79 20 65 6e 63 6f 64 65 73 2e 20 20  query encodes.  
21c21 54 68 65 20 70 54 61 62 6c 65 20 61 6e 64 0a 2a  The pTable and.*
21c22 2a 20 70 44 61 74 61 62 61 73 65 20 70 61 72 61  * pDatabase para
21c23 6d 65 74 65 72 73 20 61 72 65 20 4e 55 4c 4c 20  meters are NULL 
21c24 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 2e 20  for subqueries. 
21c25 20 54 68 65 20 70 4f 6e 20 61 6e 64 20 70 55 73   The pOn and pUs
21c26 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ing.** parameter
21c27 73 20 61 72 65 20 74 68 65 20 63 6f 6e 74 65 6e  s are the conten
21c28 74 20 6f 66 20 74 68 65 20 4f 4e 20 61 6e 64 20  t of the ON and 
21c29 55 53 49 4e 47 20 63 6c 61 75 73 65 73 2e 0a 2a  USING clauses..*
21c2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6e 65  *.** Return a ne
21c2b 77 20 53 72 63 4c 69 73 74 20 77 68 69 63 68 20  w SrcList which 
21c2c 65 6e 63 6f 64 65 73 20 69 73 20 74 68 65 20 46  encodes is the F
21c2d 52 4f 4d 20 77 69 74 68 20 74 68 65 20 6e 65 77  ROM with the new
21c2e 0a 2a 2a 20 74 65 72 6d 20 61 64 64 65 64 2e 0a  .** term added..
21c2f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21c30 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
21c31 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46  e3SrcListAppendF
21c32 72 6f 6d 54 65 72 6d 28 0a 20 20 50 61 72 73 65  romTerm(.  Parse
21c33 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
21c34 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
21c35 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
21c36 73 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  st *p,          
21c37 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 70     /* The left p
21c38 61 72 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  art of the FROM 
21c39 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79 20 73  clause already s
21c3a 65 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  een */.  Token *
21c3b 70 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  pTable,         
21c3c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
21c3d 74 61 62 6c 65 20 74 6f 20 61 64 64 20 74 6f 20  table to add to 
21c3e 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
21c3f 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74  */.  Token *pDat
21c40 61 62 61 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  abase,       /* 
21c41 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
21c42 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
21c43 70 54 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65  pTable */.  Toke
21c44 6e 20 2a 70 41 6c 69 61 73 2c 20 20 20 20 20 20  n *pAlias,      
21c45 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74      /* The right
21c46 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
21c47 65 20 41 53 20 73 75 62 65 78 70 72 65 73 73 69  e AS subexpressi
21c48 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  on */.  Select *
21c49 70 53 75 62 71 75 65 72 79 2c 20 20 20 20 20 20  pSubquery,      
21c4a 2f 2a 20 41 20 73 75 62 71 75 65 72 79 20 75 73  /* A subquery us
21c4b 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61  ed in place of a
21c4c 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   table name */. 
21c4d 20 45 78 70 72 20 2a 70 4f 6e 2c 20 20 20 20 20   Expr *pOn,     
21c4e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
21c4f 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a  ON clause of a j
21c50 6f 69 6e 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20  oin */.  IdList 
21c51 2a 70 55 73 69 6e 67 20 20 20 20 20 20 20 20 20  *pUsing         
21c52 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c   /* The USING cl
21c53 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a  ause of a join *
21c54 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 53 72  /.){.  struct Sr
21c55 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
21c56 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  m;.  sqlite3 *db
21c57 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
21c58 20 70 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c   p = sqlite3SrcL
21c59 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20 70 2c  istAppend(db, p,
21c5a 20 70 54 61 62 6c 65 2c 20 70 44 61 74 61 62 61   pTable, pDataba
21c5b 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  se);.  if( p==0 
21c5c 7c 7c 20 70 2d 3e 6e 53 72 63 3d 3d 30 20 29 7b  || p->nSrc==0 ){
21c5d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
21c5e 44 65 6c 65 74 65 28 64 62 2c 20 70 4f 6e 29 3b  Delete(db, pOn);
21c5f 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69  .    sqlite3IdLi
21c60 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 55 73  stDelete(db, pUs
21c61 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ing);.    sqlite
21c62 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
21c63 2c 20 70 53 75 62 71 75 65 72 79 29 3b 0a 20 20  , pSubquery);.  
21c64 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a    return p;.  }.
21c65 20 20 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b    pItem = &p->a[
21c66 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 66  p->nSrc-1];.  if
21c67 28 20 70 41 6c 69 61 73 20 26 26 20 70 41 6c 69  ( pAlias && pAli
21c68 61 73 2d 3e 6e 20 29 7b 0a 20 20 20 20 70 49 74  as->n ){.    pIt
21c69 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c  em->zAlias = sql
21c6a 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
21c6b 6e 28 64 62 2c 20 70 41 6c 69 61 73 29 3b 0a 20  n(db, pAlias);. 
21c6c 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 70 53 65 6c   }.  pItem->pSel
21c6d 65 63 74 20 3d 20 70 53 75 62 71 75 65 72 79 3b  ect = pSubquery;
21c6e 0a 20 20 70 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20  .  pItem->pOn = 
21c6f 70 4f 6e 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 55  pOn;.  pItem->pU
21c70 73 69 6e 67 20 3d 20 70 55 73 69 6e 67 3b 0a 20  sing = pUsing;. 
21c71 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
21c72 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e 44 45 58  .** Add an INDEX
21c73 45 44 20 42 59 20 6f 72 20 4e 4f 54 20 49 4e 44  ED BY or NOT IND
21c74 45 58 45 44 20 63 6c 61 75 73 65 20 74 6f 20 74  EXED clause to t
21c75 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
21c76 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c 65 6d 65   added .** eleme
21c77 6e 74 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65  nt of the source
21c78 2d 6c 69 73 74 20 70 61 73 73 65 64 20 61 73 20  -list passed as 
21c79 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
21c7a 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
21c7b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21c7c 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65  te3SrcListIndexe
21c7d 64 42 79 28 50 61 72 73 65 20 2a 70 50 61 72 73  dBy(Parse *pPars
21c7e 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 54  e, SrcList *p, T
21c7f 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65 64 42 79  oken *pIndexedBy
21c80 29 7b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 65  ){.  if( pIndexe
21c81 64 42 79 20 26 26 20 70 20 26 26 20 70 2d 3e 6e  dBy && p && p->n
21c82 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 73 74 72  Src>0 ){.    str
21c83 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
21c84 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b   *pItem = &p->a[
21c85 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 20 20  p->nSrc-1];.    
21c86 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 6e  assert( pItem->n
21c87 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 26 26 20  otIndexed==0 && 
21c88 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 3d 3d 30  pItem->zIndex==0
21c89 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 64   );.    if( pInd
21c8a 65 78 65 64 42 79 2d 3e 6e 3d 3d 31 20 26 26 20  exedBy->n==1 && 
21c8b 21 70 49 6e 64 65 78 65 64 42 79 2d 3e 7a 20 29  !pIndexedBy->z )
21c8c 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 22 4e 4f  {.      /* A "NO
21c8d 54 20 49 4e 44 45 58 45 44 22 20 63 6c 61 75 73  T INDEXED" claus
21c8e 65 20 77 61 73 20 73 75 70 70 6c 69 65 64 2e 20  e was supplied. 
21c8f 53 65 65 20 70 61 72 73 65 2e 79 20 0a 20 20 20  See parse.y .   
21c90 20 20 20 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20     ** construct 
21c91 22 69 6e 64 65 78 65 64 5f 6f 70 74 22 20 66 6f  "indexed_opt" fo
21c92 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 20 20  r details. */.  
21c93 20 20 20 20 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e      pItem->notIn
21c94 64 65 78 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d  dexed = 1;.    }
21c95 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 74 65  else{.      pIte
21c96 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c 69  m->zIndex = sqli
21c97 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
21c98 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 6e  (pParse->db, pIn
21c99 64 65 78 65 64 42 79 29 3b 0a 20 20 20 20 7d 0a  dexedBy);.    }.
21c9a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65    }.}../*.** Whe
21c9b 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20 61 20  n building up a 
21c9c 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e 20 74  FROM clause in t
21c9d 68 65 20 70 61 72 73 65 72 2c 20 74 68 65 20 6a  he parser, the j
21c9e 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20  oin operator.** 
21c9f 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61 74 74  is initially att
21ca0 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65 66  ached to the lef
21ca1 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75 74 20  t operand.  But 
21ca2 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  the code generat
21ca3 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20 74 68  or.** expects th
21ca4 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20  e join operator 
21ca5 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72 69 67  to be on the rig
21ca6 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69  ht operand.  Thi
21ca7 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53 68 69  s routine.** Shi
21ca8 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f 70 65  fts all join ope
21ca9 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65 66 74  rators from left
21caa 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20 61 6e   to right for an
21cab 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a 2a 20   entire FROM.** 
21cac 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 45 78  clause..**.** Ex
21cad 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65 20 74  ample: Suppose t
21cae 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b 65 20  he join is like 
21caf 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
21cb0 20 20 20 20 20 20 41 20 6e 61 74 75 72 61 6c 20        A natural 
21cb1 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a 2a 0a  cross join B.**.
21cb2 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20  ** The operator 
21cb3 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72 6f 73  is "natural cros
21cb4 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 41 20  s join".  The A 
21cb5 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 20 61  and B operands a
21cb6 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  re stored.** in 
21cb7 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e 61  p->a[0] and p->a
21cb8 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76 65 6c  [1], respectivel
21cb9 79 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 69  y.  The parser i
21cba 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 73 20  nitially stores 
21cbb 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20  the.** operator 
21cbc 77 69 74 68 20 41 2e 20 20 54 68 69 73 20 72 6f  with A.  This ro
21cbd 75 74 69 6e 65 20 73 68 69 66 74 73 20 74 68 61  utine shifts tha
21cbe 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 72 20  t operator over 
21cbf 74 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  to B..*/.SQLITE_
21cc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21cc1 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74  ite3SrcListShift
21cc2 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74  JoinType(SrcList
21cc3 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 26 26   *p){.  if( p &&
21cc4 20 70 2d 3e 61 20 29 7b 0a 20 20 20 20 69 6e 74   p->a ){.    int
21cc5 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 70 2d   i;.    for(i=p-
21cc6 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20 69 2d  >nSrc-1; i>0; i-
21cc7 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 5b 69  -){.      p->a[i
21cc8 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 2d 3e  ].jointype = p->
21cc9 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65 3b  a[i-1].jointype;
21cca 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 5b  .    }.    p->a[
21ccb 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b  0].jointype = 0;
21ccc 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65  .  }.}../*.** Be
21ccd 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  gin a transactio
21cce 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n.*/.SQLITE_PRIV
21ccf 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
21cd0 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  BeginTransaction
21cd1 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21cd2 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73 71 6c  int type){.  sql
21cd3 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65  ite3 *db;.  Vdbe
21cd4 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20   *v;.  int i;.. 
21cd5 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 7c   if( pParse==0 |
21cd6 7c 20 28 64 62 3d 70 50 61 72 73 65 2d 3e 64 62  | (db=pParse->db
21cd7 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44 62 5b  )==0 || db->aDb[
21cd8 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75  0].pBt==0 ) retu
21cd9 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  rn;.  if( pParse
21cda 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
21cdb 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
21cdc 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  urn;.  if( sqlit
21cdd 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
21cde 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  se, SQLITE_TRANS
21cdf 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e 22 2c  ACTION, "BEGIN",
21ce0 20 30 2c 20 30 29 20 29 20 72 65 74 75 72 6e 3b   0, 0) ) return;
21ce1 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
21ce2 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
21ce3 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75 72    if( !v ) retur
21ce4 6e 3b 0a 20 20 69 66 28 20 74 79 70 65 21 3d 54  n;.  if( type!=T
21ce5 4b 5f 44 45 46 45 52 52 45 44 20 29 7b 0a 20 20  K_DEFERRED ){.  
21ce6 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
21ce7 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
21ce8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21ce9 4f 70 32 28 76 2c 20 4f 50 5f 54 72 61 6e 73 61  Op2(v, OP_Transa
21cea 63 74 69 6f 6e 2c 20 69 2c 20 28 74 79 70 65 3d  ction, i, (type=
21ceb 3d 54 4b 5f 45 58 43 4c 55 53 49 56 45 29 2b 31  =TK_EXCLUSIVE)+1
21cec 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21ced 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
21cee 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   i);.    }.  }. 
21cef 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21cf0 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d  p2(v, OP_AutoCom
21cf1 6d 69 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f  mit, 0, 0);.}../
21cf2 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20 74 72  *.** Commit a tr
21cf3 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c  ansaction.*/.SQL
21cf4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21cf5 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72   sqlite3CommitTr
21cf6 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20  ansaction(Parse 
21cf7 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69  *pParse){.  sqli
21cf8 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20  te3 *db;.  Vdbe 
21cf9 2a 76 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73  *v;..  if( pPars
21cfa 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61 72  e==0 || (db=pPar
21cfb 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62  se->db)==0 || db
21cfc 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20  ->aDb[0].pBt==0 
21cfd 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
21cfe 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
21cff 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
21d00 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
21d01 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
21d02 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
21d03 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 43  _TRANSACTION, "C
21d04 4f 4d 4d 49 54 22 2c 20 30 2c 20 30 29 20 29 20  OMMIT", 0, 0) ) 
21d05 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73  return;..  v = s
21d06 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
21d07 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29  arse);.  if( v )
21d08 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
21d09 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75  eAddOp2(v, OP_Au
21d0a 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 30 29 3b  toCommit, 1, 0);
21d0b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f  .  }.}../*.** Ro
21d0c 6c 6c 62 61 63 6b 20 61 20 74 72 61 6e 73 61 63  llback a transac
21d0d 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tion.*/.SQLITE_P
21d0e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21d0f 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73  te3RollbackTrans
21d10 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50  action(Parse *pP
21d11 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
21d12 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b   *db;.  Vdbe *v;
21d13 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d  ..  if( pParse==
21d14 30 20 7c 7c 20 28 64 62 3d 70 50 61 72 73 65 2d  0 || (db=pParse-
21d15 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61  >db)==0 || db->a
21d16 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72  Db[0].pBt==0 ) r
21d17 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61  eturn;.  if( pPa
21d18 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
21d19 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
21d1a 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71  return;.  if( sq
21d1b 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
21d1c 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52  Parse, SQLITE_TR
21d1d 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52 4f 4c 4c  ANSACTION, "ROLL
21d1e 42 41 43 4b 22 2c 20 30 2c 20 30 29 20 29 20 72  BACK", 0, 0) ) r
21d1f 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 71  eturn;..  v = sq
21d20 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
21d21 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b  rse);.  if( v ){
21d22 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
21d23 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74  AddOp2(v, OP_Aut
21d24 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 31 29 3b 0a  oCommit, 1, 1);.
21d25 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
21d26 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
21d27 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
21d28 65 72 20 77 68 65 6e 20 69 74 20 70 61 72 73 65  er when it parse
21d29 73 20 61 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63  s a command to c
21d2a 72 65 61 74 65 2c 0a 2a 2a 20 72 65 6c 65 61 73  reate,.** releas
21d2b 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 61 6e  e or rollback an
21d2c 20 53 51 4c 20 73 61 76 65 70 6f 69 6e 74 2e 20   SQL savepoint. 
21d2d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
21d2e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
21d2f 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65 20 2a  avepoint(Parse *
21d30 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 70 2c 20  pParse, int op, 
21d31 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20  Token *pName){. 
21d32 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 73   char *zName = s
21d33 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
21d34 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ken(pParse->db, 
21d35 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e  pName);.  if( zN
21d36 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65 20  ame ){.    Vdbe 
21d37 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
21d38 64 62 65 28 70 50 61 72 73 65 29 3b 0a 23 69 66  dbe(pParse);.#if
21d39 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
21d3a 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
21d3b 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
21d3c 63 68 61 72 20 2a 61 7a 5b 5d 20 3d 20 7b 20 22  char *az[] = { "
21d3d 42 45 47 49 4e 22 2c 20 22 52 45 4c 45 41 53 45  BEGIN", "RELEASE
21d3e 22 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 20 7d 3b  ", "ROLLBACK" };
21d3f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 53 41  .    assert( !SA
21d40 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20 26 26  VEPOINT_BEGIN &&
21d41 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41   SAVEPOINT_RELEA
21d42 53 45 3d 3d 31 20 26 26 20 53 41 56 45 50 4f 49  SE==1 && SAVEPOI
21d43 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3d 3d 32 20 29  NT_ROLLBACK==2 )
21d44 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  ;.#endif.    if(
21d45 20 21 76 20 7c 7c 20 73 71 6c 69 74 65 33 41 75   !v || sqlite3Au
21d46 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
21d47 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54  SQLITE_SAVEPOINT
21d48 2c 20 61 7a 5b 6f 70 5d 2c 20 7a 4e 61 6d 65 2c  , az[op], zName,
21d49 20 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c   0) ){.      sql
21d4a 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73  ite3DbFree(pPars
21d4b 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  e->db, zName);. 
21d4c 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
21d4d 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
21d4e 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
21d4f 61 76 65 70 6f 69 6e 74 2c 20 6f 70 2c 20 30 2c  avepoint, op, 0,
21d50 20 30 2c 20 7a 4e 61 6d 65 2c 20 50 34 5f 44 59   0, zName, P4_DY
21d51 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  NAMIC);.  }.}../
21d52 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  *.** Make sure t
21d53 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
21d54 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 61 76 61   is open and ava
21d55 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 2e 20  ilable for use. 
21d56 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e   Return.** the n
21d57 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e  umber of errors.
21d58 20 20 4c 65 61 76 65 20 61 6e 79 20 65 72 72 6f    Leave any erro
21d59 72 20 6d 65 73 73 61 67 65 73 20 69 6e 20 74 68  r messages in th
21d5a 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75  e pParse structu
21d5b 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  re..*/.SQLITE_PR
21d5c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
21d5d 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73  3OpenTempDatabas
21d5e 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  e(Parse *pParse)
21d5f 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
21d60 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
21d61 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  if( db->aDb[1].p
21d62 42 74 3d 3d 30 20 26 26 20 21 70 50 61 72 73 65  Bt==0 && !pParse
21d63 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20  ->explain ){.   
21d64 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73 74 61   int rc;.    sta
21d65 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c  tic const int fl
21d66 61 67 73 20 3d 20 0a 20 20 20 20 20 20 20 20 20  ags = .         
21d67 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
21d68 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 20  DWRITE |.       
21d69 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43     SQLITE_OPEN_C
21d6a 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20  REATE |.        
21d6b 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58    SQLITE_OPEN_EX
21d6c 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20 20  CLUSIVE |.      
21d6d 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
21d6e 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a  DELETEONCLOSE |.
21d6f 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
21d70 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 0a  _OPEN_TEMP_DB;..
21d71 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
21d72 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c  BtreeFactory(db,
21d73 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45   0, 0, SQLITE_DE
21d74 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
21d75 2c 20 66 6c 61 67 73 2c 0a 20 20 20 20 20 20 20  , flags,.       
21d76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d77 20 20 20 20 20 20 20 20 20 20 26 64 62 2d 3e 61            &db->a
21d78 44 62 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20  Db[1].pBt);.    
21d79 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
21d7a 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
21d7b 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
21d7c 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70  e, "unable to op
21d7d 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 64  en a temporary d
21d7e 61 74 61 62 61 73 65 20 22 0a 20 20 20 20 20 20  atabase ".      
21d7f 20 20 22 66 69 6c 65 20 66 6f 72 20 73 74 6f 72    "file for stor
21d80 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ing temporary ta
21d81 62 6c 65 73 22 29 3b 0a 20 20 20 20 20 20 70 50  bles");.      pP
21d82 61 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20  arse->rc = rc;. 
21d83 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
21d84 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
21d85 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
21d86 4c 49 54 45 5f 49 6e 54 72 61 6e 73 29 3d 3d 30  LITE_InTrans)==0
21d87 20 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d   || db->autoComm
21d88 69 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  it );.    assert
21d89 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  ( db->aDb[1].pSc
21d8a 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69  hema );.    sqli
21d8b 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d  te3PagerJournalM
21d8c 6f 64 65 28 73 71 6c 69 74 65 33 42 74 72 65 65  ode(sqlite3Btree
21d8d 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 31 5d  Pager(db->aDb[1]
21d8e 2e 70 42 74 29 2c 0a 20 20 20 20 20 20 20 20 20  .pBt),.         
21d8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d90 20 20 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e     db->dfltJourn
21d91 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72  alMode);.  }.  r
21d92 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
21d93 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20  * Generate VDBE 
21d94 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 76  code that will v
21d95 65 72 69 66 79 20 74 68 65 20 73 63 68 65 6d 61  erify the schema
21d96 20 63 6f 6f 6b 69 65 20 61 6e 64 20 73 74 61 72   cookie and star
21d97 74 0a 2a 2a 20 61 20 72 65 61 64 2d 74 72 61 6e  t.** a read-tran
21d98 73 61 63 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20  saction for all 
21d99 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 66  named database f
21d9a 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  iles..**.** It i
21d9b 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74  s important that
21d9c 20 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b   all schema cook
21d9d 69 65 73 20 62 65 20 76 65 72 69 66 69 65 64 20  ies be verified 
21d9e 61 6e 64 20 61 6c 6c 0a 2a 2a 20 72 65 61 64 20  and all.** read 
21d9f 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 62 65 20  transactions be 
21da0 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61  started before a
21da1 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 68 61 70  nything else hap
21da2 70 65 6e 73 20 69 6e 0a 2a 2a 20 74 68 65 20 56  pens in.** the V
21da3 44 42 45 20 70 72 6f 67 72 61 6d 2e 20 20 42 75  DBE program.  Bu
21da4 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63  t this routine c
21da5 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74  an be called aft
21da6 65 72 20 6d 75 63 68 20 6f 74 68 65 72 0a 2a 2a  er much other.**
21da7 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 20 67   code has been g
21da8 65 6e 65 72 61 74 65 64 2e 20 20 53 6f 20 68 65  enerated.  So he
21da9 72 65 20 69 73 20 77 68 61 74 20 77 65 20 64 6f  re is what we do
21daa 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  :.**.** The firs
21dab 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74  t time this rout
21dac 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 77  ine is called, w
21dad 65 20 63 6f 64 65 20 61 6e 20 4f 50 5f 47 6f 74  e code an OP_Got
21dae 6f 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 6a  o that.** will j
21daf 75 6d 70 20 74 6f 20 61 20 73 75 62 72 6f 75 74  ump to a subrout
21db0 69 6e 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ine at the end o
21db1 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20  f the program.  
21db2 54 68 65 6e 20 77 65 0a 2a 2a 20 72 65 63 6f 72  Then we.** recor
21db3 64 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65  d every database
21db4 20 74 68 61 74 20 6e 65 65 64 73 20 69 74 73 20   that needs its 
21db5 73 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20  schema verified 
21db6 69 6e 20 74 68 65 0a 2a 2a 20 70 50 61 72 73 65  in the.** pParse
21db7 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 66 69 65  ->cookieMask fie
21db8 6c 64 2e 20 20 4c 61 74 65 72 2c 20 61 66 74 65  ld.  Later, afte
21db9 72 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65  r all other code
21dba 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 67 65 6e   has been.** gen
21dbb 65 72 61 74 65 64 2c 20 74 68 65 20 73 75 62 72  erated, the subr
21dbc 6f 75 74 69 6e 65 20 74 68 61 74 20 64 6f 65 73  outine that does
21dbd 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 65 72 69   the cookie veri
21dbe 66 69 63 61 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a  fications and.**
21dbf 20 73 74 61 72 74 73 20 74 68 65 20 74 72 61 6e   starts the tran
21dc0 73 61 63 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65  sactions will be
21dc1 20 63 6f 64 65 64 20 61 6e 64 20 74 68 65 20 4f   coded and the O
21dc2 50 5f 47 6f 74 6f 20 50 32 20 76 61 6c 75 65 0a  P_Goto P2 value.
21dc3 2a 2a 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 20  ** will be made 
21dc4 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74  to point to that
21dc5 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 54 68   subroutine.  Th
21dc6 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20  e generation of 
21dc7 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 20 76 65  the.** cookie ve
21dc8 72 69 66 69 63 61 74 69 6f 6e 20 73 75 62 72 6f  rification subro
21dc9 75 74 69 6e 65 20 63 6f 64 65 20 68 61 70 70 65  utine code happe
21dca 6e 73 20 69 6e 20 73 71 6c 69 74 65 33 46 69 6e  ns in sqlite3Fin
21dcb 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2a 0a  ishCoding()..**.
21dcc 2a 2a 20 49 66 20 69 44 62 3c 30 20 74 68 65 6e  ** If iDb<0 then
21dcd 20 63 6f 64 65 20 74 68 65 20 4f 50 5f 47 6f 74   code the OP_Got
21dce 6f 20 6f 6e 6c 79 20 2d 20 64 6f 6e 27 74 20 73  o only - don't s
21dcf 65 74 20 66 6c 61 67 20 74 6f 20 76 65 72 69 66  et flag to verif
21dd0 79 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20  y the.** schema 
21dd1 6f 6e 20 61 6e 79 20 64 61 74 61 62 61 73 65 73  on any databases
21dd2 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  .  This can be u
21dd3 73 65 64 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20  sed to position 
21dd4 74 68 65 20 4f 50 5f 47 6f 74 6f 0a 2a 2a 20 65  the OP_Goto.** e
21dd5 61 72 6c 79 20 69 6e 20 74 68 65 20 63 6f 64 65  arly in the code
21dd6 2c 20 62 65 66 6f 72 65 20 77 65 20 6b 6e 6f 77  , before we know
21dd7 20 69 66 20 61 6e 79 20 64 61 74 61 62 61 73 65   if any database
21dd8 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 62 65 20   tables will be 
21dd9 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  used..*/.SQLITE_
21dda 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21ddb 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
21ddc 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72  hema(Parse *pPar
21ddd 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  se, int iDb){.  
21dde 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56  sqlite3 *db;.  V
21ddf 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 6d 61  dbe *v;.  int ma
21de0 73 6b 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74  sk;..  v = sqlit
21de1 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
21de2 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
21de3 72 65 74 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73  return;  /* This
21de4 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66   only happens if
21de5 20 74 68 65 72 65 20 77 61 73 20 61 20 70 72 69   there was a pri
21de6 6f 72 20 65 72 72 6f 72 20 2a 2f 0a 20 20 64 62  or error */.  db
21de7 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
21de8 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6f 6f   if( pParse->coo
21de9 6b 69 65 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20  kieGoto==0 ){.  
21dea 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65    pParse->cookie
21deb 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56 64  Goto = sqlite3Vd
21dec 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
21ded 6f 74 6f 2c 20 30 2c 20 30 29 2b 31 3b 0a 20 20  oto, 0, 0)+1;.  
21dee 7d 0a 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29  }.  if( iDb>=0 )
21def 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  {.    assert( iD
21df0 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20  b<db->nDb );.   
21df1 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
21df2 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 7c 7c 20  [iDb].pBt!=0 || 
21df3 69 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73  iDb==1 );.    as
21df4 73 65 72 74 28 20 69 44 62 3c 53 51 4c 49 54 45  sert( iDb<SQLITE
21df5 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32 20  _MAX_ATTACHED+2 
21df6 29 3b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 31 3c  );.    mask = 1<
21df7 3c 69 44 62 3b 0a 20 20 20 20 69 66 28 20 28 70  <iDb;.    if( (p
21df8 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73  Parse->cookieMas
21df9 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a  k & mask)==0 ){.
21dfa 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f        pParse->co
21dfb 6f 6b 69 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b  okieMask |= mask
21dfc 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
21dfd 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d  cookieValue[iDb]
21dfe 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
21dff 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f  pSchema->schema_
21e00 63 6f 6f 6b 69 65 3b 0a 20 20 20 20 20 20 69 66  cookie;.      if
21e01 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
21e02 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20  & iDb==1 ){.    
21e03 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
21e04 65 6d 70 44 61 74 61 62 61 73 65 28 70 50 61 72  empDatabase(pPar
21e05 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  se);.      }.   
21e06 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
21e07 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f  Generate VDBE co
21e08 64 65 20 74 68 61 74 20 70 72 65 70 61 72 65 73  de that prepares
21e09 20 66 6f 72 20 64 6f 69 6e 67 20 61 6e 20 6f 70   for doing an op
21e0a 65 72 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20  eration that.** 
21e0b 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 65  might change the
21e0c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
21e0d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74   This routine st
21e0e 61 72 74 73 20 61 20 6e 65 77 20 74 72 61 6e 73  arts a new trans
21e0f 61 63 74 69 6f 6e 20 69 66 20 77 65 20 61 72 65  action if we are
21e10 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 77 69 74   not already wit
21e11 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63  hin.** a transac
21e12 74 69 6f 6e 2e 20 20 49 66 20 77 65 20 61 72 65  tion.  If we are
21e13 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20   already within 
21e14 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  a transaction, t
21e15 68 65 6e 20 61 20 63 68 65 63 6b 70 6f 69 6e 74  hen a checkpoint
21e16 0a 2a 2a 20 69 73 20 73 65 74 20 69 66 20 74 68  .** is set if th
21e17 65 20 73 65 74 53 74 61 74 65 6d 65 6e 74 20 70  e setStatement p
21e18 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65  arameter is true
21e19 2e 20 20 41 20 63 68 65 63 6b 70 6f 69 6e 74 20  .  A checkpoint 
21e1a 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65 74  should.** be set
21e1b 20 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20   for operations 
21e1c 74 68 61 74 20 6d 69 67 68 74 20 66 61 69 6c 20  that might fail 
21e1d 28 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74 72  (due to a constr
21e1e 61 69 6e 74 29 20 70 61 72 74 20 6f 66 0a 2a 2a  aint) part of.**
21e1f 20 74 68 65 20 77 61 79 20 74 68 72 6f 75 67 68   the way through
21e20 20 61 6e 64 20 77 68 69 63 68 20 77 69 6c 6c 20   and which will 
21e21 6e 65 65 64 20 74 6f 20 75 6e 64 6f 20 73 6f 6d  need to undo som
21e22 65 20 77 72 69 74 65 73 20 77 69 74 68 6f 75 74  e writes without
21e23 20 68 61 76 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f   having to.** ro
21e24 6c 6c 62 61 63 6b 20 74 68 65 20 77 68 6f 6c 65  llback the whole
21e25 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 46   transaction.  F
21e26 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 77 68  or operations wh
21e27 65 72 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69  ere all constrai
21e28 6e 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 63 68  nts.** can be ch
21e29 65 63 6b 65 64 20 62 65 66 6f 72 65 20 61 6e 79  ecked before any
21e2a 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64   changes are mad
21e2b 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
21e2c 65 2c 20 69 74 20 69 73 20 6e 65 76 65 72 0a 2a  e, it is never.*
21e2d 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 75  * necessary to u
21e2e 6e 64 6f 20 61 20 77 72 69 74 65 20 61 6e 64 20  ndo a write and 
21e2f 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 20 73  the checkpoint s
21e30 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 65 74  hould not be set
21e31 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 64 61 74  ..**.** Only dat
21e32 61 62 61 73 65 20 69 44 62 20 61 6e 64 20 74 68  abase iDb and th
21e33 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 20  e temp database 
21e34 61 72 65 20 6d 61 64 65 20 77 72 69 74 61 62 6c  are made writabl
21e35 65 20 62 79 20 74 68 69 73 20 63 61 6c 6c 2e 0a  e by this call..
21e36 2a 2a 20 49 66 20 69 44 62 3d 3d 30 2c 20 74 68  ** If iDb==0, th
21e37 65 6e 20 74 68 65 20 6d 61 69 6e 20 61 6e 64 20  en the main and 
21e38 74 65 6d 70 20 64 61 74 61 62 61 73 65 73 20 61  temp databases a
21e39 72 65 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65  re made writable
21e3a 2e 20 20 20 49 66 0a 2a 2a 20 69 44 62 3d 3d 31  .   If.** iDb==1
21e3b 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 20 74   then only the t
21e3c 65 6d 70 20 64 61 74 61 62 61 73 65 20 69 73 20  emp database is 
21e3d 6d 61 64 65 20 77 72 69 74 61 62 6c 65 2e 20 20  made writable.  
21e3e 49 66 20 69 44 62 3e 31 20 74 68 65 6e 20 74 68  If iDb>1 then th
21e3f 65 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 61  e.** specified a
21e40 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
21e41 65 20 61 6e 64 20 74 68 65 20 74 65 6d 70 20 64  e and the temp d
21e42 61 74 61 62 61 73 65 20 61 72 65 20 6d 61 64 65  atabase are made
21e43 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51   writable..*/.SQ
21e44 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
21e45 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72  d sqlite3BeginWr
21e46 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72  iteOperation(Par
21e47 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
21e48 73 65 74 53 74 61 74 65 6d 65 6e 74 2c 20 69 6e  setStatement, in
21e49 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a  t iDb){.  Vdbe *
21e4a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
21e4b 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
21e4c 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
21e4d 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65  .  sqlite3CodeVe
21e4e 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73  rifySchema(pPars
21e4f 65 2c 20 69 44 62 29 3b 0a 20 20 70 50 61 72 73  e, iDb);.  pPars
21e50 65 2d 3e 77 72 69 74 65 4d 61 73 6b 20 7c 3d 20  e->writeMask |= 
21e51 31 3c 3c 69 44 62 3b 0a 20 20 69 66 28 20 73 65  1<<iDb;.  if( se
21e52 74 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70 50  tStatement && pP
21e53 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20  arse->nested==0 
21e54 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
21e55 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 53  beAddOp1(v, OP_S
21e56 74 61 74 65 6d 65 6e 74 2c 20 69 44 62 29 3b 0a  tatement, iDb);.
21e57 20 20 7d 0a 20 20 69 66 28 20 28 4f 4d 49 54 5f    }.  if( (OMIT_
21e58 54 45 4d 50 44 42 20 7c 7c 20 69 44 62 21 3d 31  TEMPDB || iDb!=1
21e59 29 20 26 26 20 70 50 61 72 73 65 2d 3e 64 62 2d  ) && pParse->db-
21e5a 3e 61 44 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29  >aDb[1].pBt!=0 )
21e5b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67  {.    sqlite3Beg
21e5c 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
21e5d 28 70 50 61 72 73 65 2c 20 73 65 74 53 74 61 74  (pParse, setStat
21e5e 65 6d 65 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 7d  ement, 1);.  }.}
21e5f 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
21e60 20 73 65 65 20 69 66 20 70 49 6e 64 65 78 20 75   see if pIndex u
21e61 73 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  ses the collatin
21e62 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c  g sequence pColl
21e63 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75  .  Return.** tru
21e64 65 20 69 66 20 69 74 20 64 6f 65 73 20 61 6e 64  e if it does and
21e65 20 66 61 6c 73 65 20 69 66 20 69 74 20 64 6f 65   false if it doe
21e66 73 20 6e 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65  s not..*/.#ifnde
21e67 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45  f SQLITE_OMIT_RE
21e68 49 4e 44 45 58 0a 73 74 61 74 69 63 20 69 6e 74  INDEX.static int
21e69 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28   collationMatch(
21e6a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
21e6b 6c 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  l, Index *pIndex
21e6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
21e6d 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65 78 2d  r(i=0; i<pIndex-
21e6e 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  >nColumn; i++){.
21e6f 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
21e70 7a 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f  z = pIndex->azCo
21e71 6c 6c 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 7a  ll[i];.    if( z
21e72 3d 3d 7a 43 6f 6c 6c 20 7c 7c 20 28 7a 20 26 26  ==zColl || (z &&
21e73 20 7a 43 6f 6c 6c 20 26 26 20 30 3d 3d 73 71 6c   zColl && 0==sql
21e74 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a  ite3StrICmp(z, z
21e75 43 6f 6c 6c 29 29 20 29 7b 0a 20 20 20 20 20 20  Coll)) ){.      
21e76 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
21e77 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
21e78 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
21e79 52 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e  Recompute all in
21e7a 64 69 63 65 73 20 6f 66 20 70 54 61 62 20 74 68  dices of pTab th
21e7b 61 74 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61  at use the colla
21e7c 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43  ting sequence pC
21e7d 6f 6c 6c 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c  oll..** If pColl
21e7e 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75  ==0 then recompu
21e7f 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f  te all indices o
21e80 66 20 70 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64  f pTab..*/.#ifnd
21e81 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52  ef SQLITE_OMIT_R
21e82 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20 76 6f  EINDEX.static vo
21e83 69 64 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28  id reindexTable(
21e84 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
21e85 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 68 61 72  able *pTab, char
21e86 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a   const *zColl){.
21e87 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
21e88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21e89 20 41 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69   An index associ
21e8a 61 74 65 64 20 77 69 74 68 20 70 54 61 62 20 2a  ated with pTab *
21e8b 2f 0a 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 3d  /..  for(pIndex=
21e8c 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
21e8d 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e  ndex; pIndex=pIn
21e8e 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  dex->pNext){.   
21e8f 20 69 66 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c   if( zColl==0 ||
21e90 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28   collationMatch(
21e91 7a 43 6f 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29  zColl, pIndex) )
21e92 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20  {.      int iDb 
21e93 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
21e94 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
21e95 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
21e96 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21e97 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
21e98 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
21e99 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
21e9a 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50  e3RefillIndex(pP
21e9b 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31  arse, pIndex, -1
21e9c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23  );.    }.  }.}.#
21e9d 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63  endif../*.** Rec
21e9e 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63  ompute all indic
21e9f 65 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73  es of all tables
21ea0 20 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65   in all database
21ea1 73 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 69  s where the.** i
21ea2 6e 64 69 63 65 73 20 75 73 65 20 74 68 65 20 63  ndices use the c
21ea3 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
21ea4 65 20 70 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f  e pColl.  If pCo
21ea5 6c 6c 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d  ll==0 then recom
21ea6 70 75 74 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69  pute.** all indi
21ea7 63 65 73 20 65 76 65 72 79 77 68 65 72 65 2e 0a  ces everywhere..
21ea8 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
21ea9 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73  E_OMIT_REINDEX.s
21eaa 74 61 74 69 63 20 76 6f 69 64 20 72 65 69 6e 64  tatic void reind
21eab 65 78 44 61 74 61 62 61 73 65 73 28 50 61 72 73  exDatabases(Pars
21eac 65 20 2a 70 50 61 72 73 65 2c 20 63 68 61 72 20  e *pParse, char 
21ead 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20  const *zColl){. 
21eae 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20   Db *pDb;       
21eaf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21eb0 41 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  A single databas
21eb1 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  e */.  int iDb; 
21eb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21eb3 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
21eb4 73 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20  se index number 
21eb5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
21eb6 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
21eb7 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
21eb8 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
21eb9 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20   HashElem *k;   
21eba 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21ebb 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
21ebc 20 74 61 62 6c 65 73 20 69 6e 20 70 44 62 20 2a   tables in pDb *
21ebd 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
21ebe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ebf 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68  /* A table in th
21ec0 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20  e database */.. 
21ec1 20 66 6f 72 28 69 44 62 3d 30 2c 20 70 44 62 3d   for(iDb=0, pDb=
21ec2 64 62 2d 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d  db->aDb; iDb<db-
21ec3 3e 6e 44 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62  >nDb; iDb++, pDb
21ec4 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
21ec5 20 70 44 62 21 3d 30 20 29 3b 0a 20 20 20 20 66   pDb!=0 );.    f
21ec6 6f 72 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46  or(k=sqliteHashF
21ec7 69 72 73 74 28 26 70 44 62 2d 3e 70 53 63 68 65  irst(&pDb->pSche
21ec8 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b  ma->tblHash);  k
21ec9 3b 20 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; k=sqliteHashNe
21eca 78 74 28 6b 29 29 7b 0a 20 20 20 20 20 20 70 54  xt(k)){.      pT
21ecb 61 62 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c  ab = (Table*)sql
21ecc 69 74 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a  iteHashData(k);.
21ecd 20 20 20 20 20 20 72 65 69 6e 64 65 78 54 61 62        reindexTab
21ece 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  le(pParse, pTab,
21ecf 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20   zColl);.    }. 
21ed0 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   }.}.#endif../*.
21ed1 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
21ed2 20 66 6f 72 20 74 68 65 20 52 45 49 4e 44 45 58   for the REINDEX
21ed3 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
21ed4 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20         REINDEX  
21ed5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ed6 20 20 20 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a            -- 1.*
21ed7 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58  *        REINDEX
21ed8 20 20 3c 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20    <collation>   
21ed9 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 32              -- 2
21eda 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44  .**        REIND
21edb 45 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e  EX  ?<database>.
21edc 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d  ?<tablename>  --
21edd 20 33 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49   3.**        REI
21ede 4e 44 45 58 20 20 3f 3c 64 61 74 61 62 61 73 65  NDEX  ?<database
21edf 3e 2e 3f 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20  >.?<indexname>  
21ee0 2d 2d 20 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20  -- 4.**.** Form 
21ee1 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64  1 causes all ind
21ee2 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61  ices in all atta
21ee3 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 74  ched databases t
21ee4 6f 20 62 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a  o be rebuilt..**
21ee5 20 46 6f 72 6d 20 32 20 72 65 62 75 69 6c 64 73   Form 2 rebuilds
21ee6 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20   all indices in 
21ee7 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 74 68  all databases th
21ee8 61 74 20 75 73 65 20 74 68 65 20 6e 61 6d 65 64  at use the named
21ee9 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75  .** collating fu
21eea 6e 63 74 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33  nction.  Forms 3
21eeb 20 61 6e 64 20 34 20 72 65 62 75 69 6c 64 20 74   and 4 rebuild t
21eec 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f  he named index o
21eed 72 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73  r all.** indices
21eee 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
21eef 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65   the named table
21ef0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
21ef1 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58  ITE_OMIT_REINDEX
21ef2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21ef3 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e  void sqlite3Rein
21ef4 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73  dex(Parse *pPars
21ef5 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31  e, Token *pName1
21ef6 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29  , Token *pName2)
21ef7 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
21ef8 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
21ef9 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
21efa 75 65 6e 63 65 20 74 6f 20 62 65 20 72 65 69 6e  uence to be rein
21efb 64 65 78 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a  dexed, or NULL *
21efc 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  /.  char *z;    
21efd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21efe 2f 2a 20 4e 61 6d 65 20 6f 66 20 61 20 74 61 62  /* Name of a tab
21eff 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20  le or index */. 
21f00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
21f01 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
21f02 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
21f03 62 61 73 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20  base */.  Table 
21f04 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20  *pTab;          
21f05 20 20 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65        /* A table
21f06 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
21f07 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e   */.  Index *pIn
21f08 64 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  dex;            
21f09 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73    /* An index as
21f0a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 54  sociated with pT
21f0b 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  ab */.  int iDb;
21f0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f0d 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
21f0e 61 73 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72  ase index number
21f0f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
21f10 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
21f11 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
21f12 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
21f13 20 20 54 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d    Token *pObjNam
21f14 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
21f15 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   Name of the tab
21f16 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 6f 20 62  le or index to b
21f17 65 20 72 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a  e reindexed */..
21f18 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61    /* Read the da
21f19 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49  tabase schema. I
21f1a 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
21f1b 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f  s, leave an erro
21f1c 72 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61  r message.  ** a
21f1d 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73  nd code in pPars
21f1e 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c  e and return NUL
21f1f 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49  L. */.  if( SQLI
21f20 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65  TE_OK!=sqlite3Re
21f21 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
21f22 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
21f23 20 20 7d 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65    }..  if( pName
21f24 31 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 2d 3e  1==0 || pName1->
21f25 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 69 6e  z==0 ){.    rein
21f26 64 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61  dexDatabases(pPa
21f27 72 73 65 2c 20 30 29 3b 0a 20 20 20 20 72 65 74  rse, 0);.    ret
21f28 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  urn;.  }else if(
21f29 20 70 4e 61 6d 65 32 3d 3d 30 20 7c 7c 20 70 4e   pName2==0 || pN
21f2a 61 6d 65 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20  ame2->z==0 ){.  
21f2b 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20    char *zColl;. 
21f2c 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65     assert( pName
21f2d 31 2d 3e 7a 20 29 3b 0a 20 20 20 20 7a 43 6f 6c  1->z );.    zCol
21f2e 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  l = sqlite3NameF
21f2f 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d  romToken(pParse-
21f30 3e 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20  >db, pName1);.  
21f31 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20 72    if( !zColl ) r
21f32 65 74 75 72 6e 3b 0a 20 20 20 20 70 43 6f 6c 6c  eturn;.    pColl
21f33 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
21f34 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62  llSeq(db, ENC(db
21f35 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30 29  ), zColl, -1, 0)
21f36 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ;.    if( pColl 
21f37 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 43 6f  ){.      if( zCo
21f38 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ll ){.        re
21f39 69 6e 64 65 78 44 61 74 61 62 61 73 65 73 28 70  indexDatabases(p
21f3a 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20  Parse, zColl);. 
21f3b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
21f3c 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b  Free(db, zColl);
21f3d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
21f3e 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
21f3f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
21f40 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20  b, zColl);.  }. 
21f41 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77   iDb = sqlite3Tw
21f42 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65  oPartName(pParse
21f43 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32  , pName1, pName2
21f44 2c 20 26 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20  , &pObjName);.  
21f45 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 74 75  if( iDb<0 ) retu
21f46 72 6e 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65  rn;.  z = sqlite
21f47 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
21f48 62 2c 20 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20  b, pObjName);.  
21f49 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72  if( z==0 ) retur
21f4a 6e 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61  n;.  zDb = db->a
21f4b 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
21f4c 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
21f4d 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 2c 20  indTable(db, z, 
21f4e 7a 44 62 29 3b 0a 20 20 69 66 28 20 70 54 61 62  zDb);.  if( pTab
21f4f 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 54   ){.    reindexT
21f50 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
21f51 62 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  b, 0);.    sqlit
21f52 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b  e3DbFree(db, z);
21f53 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
21f54 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69  .  pIndex = sqli
21f55 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
21f56 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69   z, zDb);.  sqli
21f57 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
21f58 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20 29  ;.  if( pIndex )
21f59 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67  {.    sqlite3Beg
21f5a 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
21f5b 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29  (pParse, 0, iDb)
21f5c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 66  ;.    sqlite3Ref
21f5d 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c  illIndex(pParse,
21f5e 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20   pIndex, -1);.  
21f5f 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
21f60 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
21f61 70 50 61 72 73 65 2c 20 22 75 6e 61 62 6c 65 20  pParse, "unable 
21f62 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 20  to identify the 
21f63 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 72 65 69  object to be rei
21f64 6e 64 65 78 65 64 22 29 3b 0a 7d 0a 23 65 6e 64  ndexed");.}.#end
21f65 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
21f66 20 61 20 64 79 6e 61 6d 69 63 6c 79 20 61 6c 6c   a dynamicly all
21f67 6f 63 61 74 65 64 20 4b 65 79 49 6e 66 6f 20 73  ocated KeyInfo s
21f68 74 72 75 63 74 75 72 65 20 74 68 61 74 20 63 61  tructure that ca
21f69 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 77 69 74  n be used.** wit
21f6a 68 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72  h OP_OpenRead or
21f6b 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 74 6f   OP_OpenWrite to
21f6c 20 61 63 63 65 73 73 20 64 61 74 61 62 61 73 65   access database
21f6d 20 69 6e 64 65 78 20 70 49 64 78 2e 0a 2a 2a 0a   index pIdx..**.
21f6e 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
21f6f 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  , a pointer to t
21f70 68 65 20 6e 65 77 20 73 74 72 75 63 74 75 72 65  he new structure
21f71 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 6e   is returned. In
21f72 20 74 68 69 73 20 63 61 73 65 0a 2a 2a 20 74 68   this case.** th
21f73 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70  e caller is resp
21f74 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c  onsible for call
21f75 69 6e 67 20 73 71 6c 69 74 65 33 44 62 46 72 65  ing sqlite3DbFre
21f76 65 28 64 62 2c 20 29 20 6f 6e 20 74 68 65 20 72  e(db, ) on the r
21f77 65 74 75 72 6e 65 64 20 0a 2a 2a 20 70 6f 69 6e  eturned .** poin
21f78 74 65 72 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ter. If an error
21f79 20 6f 63 63 75 72 73 20 28 6f 75 74 20 6f 66 20   occurs (out of 
21f7a 6d 65 6d 6f 72 79 20 6f 72 20 6d 69 73 73 69 6e  memory or missin
21f7b 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20  g collation .** 
21f7c 73 65 71 75 65 6e 63 65 29 2c 20 4e 55 4c 4c 20  sequence), NULL 
21f7d 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
21f7e 74 68 65 20 73 74 61 74 65 20 6f 66 20 70 50 61  the state of pPa
21f7f 72 73 65 20 75 70 64 61 74 65 64 20 74 6f 20 72  rse updated to r
21f80 65 66 6c 65 63 74 0a 2a 2a 20 74 68 65 20 65 72  eflect.** the er
21f81 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ror..*/.SQLITE_P
21f82 52 49 56 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a  RIVATE KeyInfo *
21f83 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
21f84 6e 66 6f 28 50 61 72 73 65 20 2a 70 50 61 72 73  nfo(Parse *pPars
21f85 65 2c 20 49 6e 64 65 78 20 2a 70 49 64 78 29 7b  e, Index *pIdx){
21f86 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
21f87 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f  nCol = pIdx->nCo
21f88 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e 42 79 74  lumn;.  int nByt
21f89 65 73 20 3d 20 73 69 7a 65 6f 66 28 4b 65 79 49  es = sizeof(KeyI
21f8a 6e 66 6f 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a  nfo) + (nCol-1)*
21f8b 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29  sizeof(CollSeq*)
21f8c 20 2b 20 6e 43 6f 6c 3b 0a 20 20 73 71 6c 69 74   + nCol;.  sqlit
21f8d 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
21f8e 3e 64 62 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  >db;.  KeyInfo *
21f8f 70 4b 65 79 20 3d 20 28 4b 65 79 49 6e 66 6f 20  pKey = (KeyInfo 
21f90 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
21f91 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 65 73  cZero(db, nBytes
21f92 29 3b 0a 0a 20 20 69 66 28 20 70 4b 65 79 20 29  );..  if( pKey )
21f93 7b 0a 20 20 20 20 70 4b 65 79 2d 3e 64 62 20 3d  {.    pKey->db =
21f94 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20   pParse->db;.   
21f95 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65   pKey->aSortOrde
21f96 72 20 3d 20 28 75 38 20 2a 29 26 28 70 4b 65 79  r = (u8 *)&(pKey
21f97 2d 3e 61 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a  ->aColl[nCol]);.
21f98 20 20 20 20 61 73 73 65 72 74 28 20 26 70 4b 65      assert( &pKe
21f99 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43  y->aSortOrder[nC
21f9a 6f 6c 5d 3d 3d 26 28 28 28 75 38 20 2a 29 70 4b  ol]==&(((u8 *)pK
21f9b 65 79 29 5b 6e 42 79 74 65 73 5d 29 20 29 3b 0a  ey)[nBytes]) );.
21f9c 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
21f9d 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
21f9e 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70   char *zColl = p
21f9f 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a  Idx->azColl[i];.
21fa0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 43        assert( zC
21fa1 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 70 4b 65  oll );.      pKe
21fa2 79 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 73 71  y->aColl[i] = sq
21fa3 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
21fa4 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c  eq(pParse, zColl
21fa5 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 70 4b 65  , -1);.      pKe
21fa6 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  y->aSortOrder[i]
21fa7 20 3d 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72   = pIdx->aSortOr
21fa8 64 65 72 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  der[i];.    }.  
21fa9 20 20 70 4b 65 79 2d 3e 6e 46 69 65 6c 64 20 3d    pKey->nField =
21faa 20 28 75 31 36 29 6e 43 6f 6c 3b 0a 20 20 7d 0a   (u16)nCol;.  }.
21fab 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
21fac 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Err ){.    sqlit
21fad 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4b 65  e3DbFree(db, pKe
21fae 79 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 30  y);.    pKey = 0
21faf 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
21fb0 4b 65 79 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  Key;.}../*******
21fb1 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62  ******* End of b
21fb2 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  uild.c *********
21fb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fb5 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
21fb6 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
21fb7 6c 65 20 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a  le callback.c **
21fb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fba 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
21fbb 30 35 20 4d 61 79 20 32 33 20 0a 2a 2a 0a 2a 2a  05 May 23 .**.**
21fbc 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
21fbd 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
21fbe 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
21fbf 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
21fc0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
21fc1 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
21fc2 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
21fc3 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
21fc4 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
21fc5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
21fc6 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
21fc7 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
21fc8 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
21fc9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
21fca 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
21fcb 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
21fcc 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
21fcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fcf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
21fd2 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
21fd3 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  ns functions use
21fd4 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  d to access the 
21fd5 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61  internal hash ta
21fd6 62 6c 65 73 0a 2a 2a 20 6f 66 20 75 73 65 72 20  bles.** of user 
21fd7 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
21fd8 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  s and collation 
21fd9 73 65 71 75 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a  sequences..**.**
21fda 20 24 49 64 3a 20 63 61 6c 6c 62 61 63 6b 2e 63   $Id: callback.c
21fdb 2c 76 20 31 2e 33 34 20 32 30 30 38 2f 31 32 2f  ,v 1.34 2008/12/
21fdc 31 30 20 32 31 3a 31 39 3a 35 37 20 64 72 68 20  10 21:19:57 drh 
21fdd 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  Exp $.*/.../*.**
21fde 20 49 6e 76 6f 6b 65 20 74 68 65 20 27 63 6f 6c   Invoke the 'col
21fdf 6c 61 74 69 6f 6e 20 6e 65 65 64 65 64 27 20 63  lation needed' c
21fe0 61 6c 6c 62 61 63 6b 20 74 6f 20 72 65 71 75 65  allback to reque
21fe1 73 74 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  st a collation s
21fe2 65 71 75 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68  equence.** in th
21fe3 65 20 64 61 74 61 62 61 73 65 20 74 65 78 74 20  e database text 
21fe4 65 6e 63 6f 64 69 6e 67 20 6f 66 20 6e 61 6d 65  encoding of name
21fe5 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e   zName, length n
21fe6 4e 61 6d 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20  Name..** If the 
21fe7 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
21fe8 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ce.*/.static voi
21fe9 64 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64  d callCollNeeded
21fea 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
21feb 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
21fec 20 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 61   int nName){.  a
21fed 73 73 65 72 74 28 20 21 64 62 2d 3e 78 43 6f 6c  ssert( !db->xCol
21fee 6c 4e 65 65 64 65 64 20 7c 7c 20 21 64 62 2d 3e  lNeeded || !db->
21fef 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29 3b  xCollNeeded16 );
21ff0 0a 20 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29  .  if( nName<0 )
21ff1 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
21ff2 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 6d 65  Strlen(db, zName
21ff3 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 78 43 6f  );.  if( db->xCo
21ff4 6c 6c 4e 65 65 64 65 64 20 29 7b 0a 20 20 20 20  llNeeded ){.    
21ff5 63 68 61 72 20 2a 7a 45 78 74 65 72 6e 61 6c 20  char *zExternal 
21ff6 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  = sqlite3DbStrND
21ff7 75 70 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  up(db, zName, nN
21ff8 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  ame);.    if( !z
21ff9 45 78 74 65 72 6e 61 6c 20 29 20 72 65 74 75 72  External ) retur
21ffa 6e 3b 0a 20 20 20 20 64 62 2d 3e 78 43 6f 6c 6c  n;.    db->xColl
21ffb 4e 65 65 64 65 64 28 64 62 2d 3e 70 43 6f 6c 6c  Needed(db->pColl
21ffc 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c 20 28  NeededArg, db, (
21ffd 69 6e 74 29 45 4e 43 28 64 62 29 2c 20 7a 45 78  int)ENC(db), zEx
21ffe 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20 73 71 6c  ternal);.    sql
21fff 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
22000 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 7d 0a 23  External);.  }.#
22001 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
22002 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 64  IT_UTF16.  if( d
22003 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36  b->xCollNeeded16
22004 20 29 7b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e   ){.    char con
22005 73 74 20 2a 7a 45 78 74 65 72 6e 61 6c 3b 0a 20  st *zExternal;. 
22006 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
22007 20 2a 70 54 6d 70 20 3d 20 73 71 6c 69 74 65 33   *pTmp = sqlite3
22008 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20  ValueNew(db);.  
22009 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
2200a 74 53 74 72 28 70 54 6d 70 2c 20 6e 4e 61 6d 65  tStr(pTmp, nName
2200b 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f  , zName, SQLITE_
2200c 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
2200d 54 49 43 29 3b 0a 20 20 20 20 7a 45 78 74 65 72  TIC);.    zExter
2200e 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  nal = sqlite3Val
2200f 75 65 54 65 78 74 28 70 54 6d 70 2c 20 53 51 4c  ueText(pTmp, SQL
22010 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29  ITE_UTF16NATIVE)
22011 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74 65 72  ;.    if( zExter
22012 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  nal ){.      db-
22013 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 28 64  >xCollNeeded16(d
22014 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  b->pCollNeededAr
22015 67 2c 20 64 62 2c 20 28 69 6e 74 29 45 4e 43 28  g, db, (int)ENC(
22016 64 62 29 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b  db), zExternal);
22017 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
22018 65 33 56 61 6c 75 65 46 72 65 65 28 70 54 6d 70  e3ValueFree(pTmp
22019 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  );.  }.#endif.}.
2201a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2201b 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66  ine is called if
2201c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
2201d 61 63 74 6f 72 79 20 66 61 69 6c 73 20 74 6f 20  actory fails to 
2201e 64 65 6c 69 76 65 72 20 61 0a 2a 2a 20 63 6f 6c  deliver a.** col
2201f 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  lation function 
22020 69 6e 20 74 68 65 20 62 65 73 74 20 65 6e 63 6f  in the best enco
22021 64 69 6e 67 20 62 75 74 20 74 68 65 72 65 20 6d  ding but there m
22022 61 79 20 62 65 20 6f 74 68 65 72 20 76 65 72 73  ay be other vers
22023 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ions.** of this 
22024 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69  collation functi
22025 6f 6e 20 28 66 6f 72 20 6f 74 68 65 72 20 74 65  on (for other te
22026 78 74 20 65 6e 63 6f 64 69 6e 67 73 29 20 61 76  xt encodings) av
22027 61 69 6c 61 62 6c 65 2e 20 55 73 65 20 6f 6e 65  ailable. Use one
22028 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 69 6e 73  .** of these ins
22029 74 65 61 64 20 69 66 20 74 68 65 79 20 65 78 69  tead if they exi
2202a 73 74 2e 20 41 76 6f 69 64 20 61 20 55 54 46 2d  st. Avoid a UTF-
2202b 38 20 3c 2d 3e 20 55 54 46 2d 31 36 20 63 6f 6e  8 <-> UTF-16 con
2202c 76 65 72 73 69 6f 6e 20 69 66 0a 2a 2a 20 70 6f  version if.** po
2202d 73 73 69 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  ssible..*/.stati
2202e 63 20 69 6e 74 20 73 79 6e 74 68 43 6f 6c 6c 53  c int synthCollS
2202f 65 71 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  eq(sqlite3 *db, 
22030 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b  CollSeq *pColl){
22031 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
22032 6c 32 3b 0a 20 20 63 68 61 72 20 2a 7a 20 3d 20  l2;.  char *z = 
22033 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pColl->zName;.  
22034 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53  int n = sqlite3S
22035 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 69 6e  trlen30(z);.  in
22036 74 20 69 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  t i;.  static co
22037 6e 73 74 20 75 38 20 61 45 6e 63 5b 5d 20 3d 20  nst u8 aEnc[] = 
22038 7b 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  { SQLITE_UTF16BE
22039 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
2203a 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 20 7d 3b  , SQLITE_UTF8 };
2203b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 33 3b  .  for(i=0; i<3;
2203c 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 6f 6c 6c   i++){.    pColl
2203d 32 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  2 = sqlite3FindC
2203e 6f 6c 6c 53 65 71 28 64 62 2c 20 61 45 6e 63 5b  ollSeq(db, aEnc[
2203f 69 5d 2c 20 7a 2c 20 6e 2c 20 30 29 3b 0a 20 20  i], z, n, 0);.  
22040 20 20 69 66 28 20 70 43 6f 6c 6c 32 2d 3e 78 43    if( pColl2->xC
22041 6d 70 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d  mp!=0 ){.      m
22042 65 6d 63 70 79 28 70 43 6f 6c 6c 2c 20 70 43 6f  emcpy(pColl, pCo
22043 6c 6c 32 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 6c  ll2, sizeof(Coll
22044 53 65 71 29 29 3b 0a 20 20 20 20 20 20 70 43 6f  Seq));.      pCo
22045 6c 6c 2d 3e 78 44 65 6c 20 3d 20 30 3b 20 20 20  ll->xDel = 0;   
22046 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20        /* Do not 
22047 63 6f 70 79 20 74 68 65 20 64 65 73 74 72 75 63  copy the destruc
22048 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74  tor */.      ret
22049 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
2204a 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2204b 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
2204c 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
2204d 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e  nction is respon
2204e 73 69 62 6c 65 20 66 6f 72 20 69 6e 76 6f 6b 69  sible for invoki
2204f 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ng the collation
22050 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63   factory callbac
22051 6b 0a 2a 2a 20 6f 72 20 73 75 62 73 74 69 74 75  k.** or substitu
22052 74 69 6e 67 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  ting a collation
22053 20 73 65 71 75 65 6e 63 65 20 6f 66 20 61 20 64   sequence of a d
22054 69 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e  ifferent encodin
22055 67 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 72 65  g when the.** re
22056 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f  quested collatio
22057 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f  n sequence is no
22058 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74  t available in t
22059 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69  he database nati
2205a 76 65 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2e 0a  ve.** encoding..
2205b 2a 2a 20 0a 2a 2a 20 49 66 20 69 74 20 69 73 20  ** .** If it is 
2205c 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70  not NULL, then p
2205d 43 6f 6c 6c 20 6d 75 73 74 20 70 6f 69 6e 74 20  Coll must point 
2205e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
2205f 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20  native encoding 
22060 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  .** collation se
22061 71 75 65 6e 63 65 20 77 69 74 68 20 6e 61 6d 65  quence with name
22062 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e   zName, length n
22063 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  Name..**.** The 
22064 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
22065 65 69 74 68 65 72 20 74 68 65 20 63 6f 6c 6c 61  either the colla
22066 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74 6f  tion sequence to
22067 20 62 65 20 75 73 65 64 20 69 6e 20 64 61 74 61   be used in data
22068 62 61 73 65 0a 2a 2a 20 64 62 20 66 6f 72 20 63  base.** db for c
22069 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6e 61  ollation type na
2206a 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68  me zName, length
2206b 20 6e 4e 61 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c   nName, or NULL,
2206c 20 69 66 20 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e   if no collation
2206d 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 63 61 6e  .** sequence can
2206e 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51   be found..*/.SQ
2206f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
22070 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74  lSeq *sqlite3Get
22071 43 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74  CollSeq(.  sqlit
22072 65 33 2a 20 64 62 2c 20 0a 20 20 43 6f 6c 6c 53  e3* db, .  CollS
22073 65 71 20 2a 70 43 6f 6c 6c 2c 20 0a 20 20 63 6f  eq *pColl, .  co
22074 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
22075 20 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 0a 29 7b   .  int nName.){
22076 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 3b 0a 0a  .  CollSeq *p;..
22077 20 20 70 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 69    p = pColl;.  i
22078 66 28 20 21 70 20 29 7b 0a 20 20 20 20 70 20 3d  f( !p ){.    p =
22079 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
2207a 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c  Seq(db, ENC(db),
2207b 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30   zName, nName, 0
2207c 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20  );.  }.  if( !p 
2207d 7c 7c 20 21 70 2d 3e 78 43 6d 70 20 29 7b 0a 20  || !p->xCmp ){. 
2207e 20 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69     /* No collati
2207f 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74  on sequence of t
22080 68 69 73 20 74 79 70 65 20 66 6f 72 20 74 68 69  his type for thi
22081 73 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65  s encoding is re
22082 67 69 73 74 65 72 65 64 2e 0a 20 20 20 20 2a 2a  gistered..    **
22083 20 43 61 6c 6c 20 74 68 65 20 63 6f 6c 6c 61 74   Call the collat
22084 69 6f 6e 20 66 61 63 74 6f 72 79 20 74 6f 20 73  ion factory to s
22085 65 65 20 69 66 20 69 74 20 63 61 6e 20 73 75 70  ee if it can sup
22086 70 6c 79 20 75 73 20 77 69 74 68 20 6f 6e 65 2e  ply us with one.
22087 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 6c 6c  .    */.    call
22088 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2c 20 7a  CollNeeded(db, z
22089 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
2208a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    p = sqlite3Fin
2208b 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43  dCollSeq(db, ENC
2208c 28 64 62 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  (db), zName, nNa
2208d 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66  me, 0);.  }.  if
2208e 28 20 70 20 26 26 20 21 70 2d 3e 78 43 6d 70 20  ( p && !p->xCmp 
2208f 26 26 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28  && synthCollSeq(
22090 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 70 20  db, p) ){.    p 
22091 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  = 0;.  }.  asser
22092 74 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43 6d 70  t( !p || p->xCmp
22093 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a   );.  return p;.
22094 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
22095 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
22096 6f 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  on a collation s
22097 65 71 75 65 6e 63 65 20 62 65 66 6f 72 65 20 69  equence before i
22098 74 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20  t is used to.** 
22099 63 68 65 63 6b 20 74 68 61 74 20 69 74 20 69 73  check that it is
2209a 20 64 65 66 69 6e 65 64 2e 20 41 6e 20 75 6e 64   defined. An und
2209b 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  efined collation
2209c 20 73 65 71 75 65 6e 63 65 20 65 78 69 73 74 73   sequence exists
2209d 20 77 68 65 6e 0a 2a 2a 20 61 20 64 61 74 61 62   when.** a datab
2209e 61 73 65 20 69 73 20 6c 6f 61 64 65 64 20 74 68  ase is loaded th
2209f 61 74 20 63 6f 6e 74 61 69 6e 73 20 72 65 66 65  at contains refe
220a0 72 65 6e 63 65 73 20 74 6f 20 63 6f 6c 6c 61 74  rences to collat
220a1 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a  ion sequences.**
220a2 20 74 68 61 74 20 68 61 76 65 20 6e 6f 74 20 62   that have not b
220a3 65 65 6e 20 64 65 66 69 6e 65 64 20 62 79 20 73  een defined by s
220a4 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
220a5 6c 6c 61 74 69 6f 6e 28 29 20 65 74 63 2e 0a 2a  llation() etc..*
220a6 2a 0a 2a 2a 20 49 66 20 72 65 71 75 69 72 65 64  *.** If required
220a7 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63  , this routine c
220a8 61 6c 6c 73 20 74 68 65 20 27 63 6f 6c 6c 61 74  alls the 'collat
220a9 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c 6c  ion needed' call
220aa 62 61 63 6b 20 74 6f 0a 2a 2a 20 72 65 71 75 65  back to.** reque
220ab 73 74 20 61 20 64 65 66 69 6e 69 74 69 6f 6e 20  st a definition 
220ac 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  of the collating
220ad 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20 74 68   sequence. If th
220ae 69 73 20 64 6f 65 73 6e 27 74 20 77 6f 72 6b 2c  is doesn't work,
220af 20 0a 2a 2a 20 61 6e 20 65 71 75 69 76 61 6c 65   .** an equivale
220b0 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  nt collating seq
220b1 75 65 6e 63 65 20 74 68 61 74 20 75 73 65 73 20  uence that uses 
220b2 61 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  a text encoding 
220b3 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f  different.** fro
220b4 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  m the main datab
220b5 61 73 65 20 69 73 20 73 75 62 73 74 69 74 75 74  ase is substitut
220b6 65 64 2c 20 69 66 20 6f 6e 65 20 69 73 20 61 76  ed, if one is av
220b7 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  ailable..*/.SQLI
220b8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
220b9 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53  qlite3CheckCollS
220ba 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  eq(Parse *pParse
220bb 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c  , CollSeq *pColl
220bc 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  ){.  if( pColl )
220bd 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
220be 20 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d   *zName = pColl-
220bf 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 6c  >zName;.    Coll
220c0 53 65 71 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  Seq *p = sqlite3
220c1 47 65 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  GetCollSeq(pPars
220c2 65 2d 3e 64 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e  e->db, pColl, zN
220c3 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66  ame, -1);.    if
220c4 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 69 66  ( !p ){.      if
220c5 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d  ( pParse->nErr==
220c6 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
220c7 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
220c8 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f  rse, "no such co
220c9 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
220ca 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  : %s", zName);. 
220cb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61       }.      pPa
220cc 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  rse->nErr++;.   
220cd 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
220ce 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20  _ERROR;.    }.  
220cf 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 43 6f    assert( p==pCo
220d0 6c 6c 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ll );.  }.  retu
220d1 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
220d2 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20  .../*.** Locate 
220d3 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 6e  and return an en
220d4 74 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62 2e  try from the db.
220d5 61 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74 61  aCollSeq hash ta
220d6 62 6c 65 2e 20 49 66 20 74 68 65 20 65 6e 74 72  ble. If the entr
220d7 79 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62  y.** specified b
220d8 79 20 7a 4e 61 6d 65 20 61 6e 64 20 6e 4e 61 6d  y zName and nNam
220d9 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20 61  e is not found a
220da 6e 64 20 70 61 72 61 6d 65 74 65 72 20 27 63 72  nd parameter 'cr
220db 65 61 74 65 27 20 69 73 0a 2a 2a 20 74 72 75 65  eate' is.** true
220dc 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20  , then create a 
220dd 6e 65 77 20 65 6e 74 72 79 2e 20 4f 74 68 65 72  new entry. Other
220de 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c  wise return NULL
220df 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 6f 69  ..**.** Each poi
220e0 6e 74 65 72 20 73 74 6f 72 65 64 20 69 6e 20 74  nter stored in t
220e1 68 65 20 73 71 6c 69 74 65 33 2e 61 43 6f 6c 6c  he sqlite3.aColl
220e2 53 65 71 20 68 61 73 68 20 74 61 62 6c 65 20 63  Seq hash table c
220e3 6f 6e 74 61 69 6e 73 20 61 6e 0a 2a 2a 20 61 72  ontains an.** ar
220e4 72 61 79 20 6f 66 20 74 68 72 65 65 20 43 6f 6c  ray of three Col
220e5 6c 53 65 71 20 73 74 72 75 63 74 75 72 65 73 2e  lSeq structures.
220e6 20 54 68 65 20 66 69 72 73 74 20 69 73 20 74 68   The first is th
220e7 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
220e8 65 6e 63 65 0a 2a 2a 20 70 72 65 66 66 65 72 72  ence.** prefferr
220e9 65 64 20 66 6f 72 20 55 54 46 2d 38 2c 20 74 68  ed for UTF-8, th
220ea 65 20 73 65 63 6f 6e 64 20 55 54 46 2d 31 36 6c  e second UTF-16l
220eb 65 2c 20 61 6e 64 20 74 68 65 20 74 68 69 72 64  e, and the third
220ec 20 55 54 46 2d 31 36 62 65 2e 0a 2a 2a 0a 2a 2a   UTF-16be..**.**
220ed 20 53 74 6f 72 65 64 20 69 6d 6d 65 64 69 61 74   Stored immediat
220ee 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 74 68  ely after the th
220ef 72 65 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ree collation se
220f0 71 75 65 6e 63 65 73 20 69 73 20 61 20 63 6f 70  quences is a cop
220f1 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c  y of.** the coll
220f2 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e  ation sequence n
220f3 61 6d 65 2e 20 41 20 70 6f 69 6e 74 65 72 20 74  ame. A pointer t
220f4 6f 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73  o this string is
220f5 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 65 61   stored in.** ea
220f6 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ch collation seq
220f7 75 65 6e 63 65 20 73 74 72 75 63 74 75 72 65 2e  uence structure.
220f8 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53  .*/.static CollS
220f9 65 71 20 2a 66 69 6e 64 43 6f 6c 6c 53 65 71 45  eq *findCollSeqE
220fa 6e 74 72 79 28 0a 20 20 73 71 6c 69 74 65 33 20  ntry(.  sqlite3 
220fb 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  *db,.  const cha
220fc 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  r *zName,.  int 
220fd 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65  nName,.  int cre
220fe 61 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  ate.){.  CollSeq
220ff 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 6e   *pColl;.  if( n
22100 4e 61 6d 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d  Name<0 ) nName =
22101 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64   sqlite3Strlen(d
22102 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f  b, zName);.  pCo
22103 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  ll = sqlite3Hash
22104 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53  Find(&db->aCollS
22105 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  eq, zName, nName
22106 29 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 70 43 6f  );..  if( 0==pCo
22107 6c 6c 20 26 26 20 63 72 65 61 74 65 20 29 7b 0a  ll && create ){.
22108 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
22109 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2210a 64 62 2c 20 33 2a 73 69 7a 65 6f 66 28 2a 70 43  db, 3*sizeof(*pC
2210b 6f 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31  oll) + nName + 1
2210c 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c   );.    if( pCol
2210d 6c 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  l ){.      CollS
2210e 65 71 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 20 20  eq *pDel = 0;.  
2210f 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61      pColl[0].zNa
22110 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f  me = (char*)&pCo
22111 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f  ll[3];.      pCo
22112 6c 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53 51 4c 49  ll[0].enc = SQLI
22113 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70  TE_UTF8;.      p
22114 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20  Coll[1].zName = 
22115 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d  (char*)&pColl[3]
22116 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31 5d  ;.      pColl[1]
22117 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  .enc = SQLITE_UT
22118 46 31 36 4c 45 3b 0a 20 20 20 20 20 20 70 43 6f  F16LE;.      pCo
22119 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63  ll[2].zName = (c
2211a 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a  har*)&pColl[3];.
2211b 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65        pColl[2].e
2211c 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31  nc = SQLITE_UTF1
2211d 36 42 45 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  6BE;.      memcp
2211e 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65  y(pColl[0].zName
2211f 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
22120 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e  .      pColl[0].
22121 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30  zName[nName] = 0
22122 3b 0a 20 20 20 20 20 20 70 44 65 6c 20 3d 20 73  ;.      pDel = s
22123 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
22124 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20  (&db->aCollSeq, 
22125 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20  pColl[0].zName, 
22126 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29 3b 0a 0a  nName, pColl);..
22127 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61        /* If a ma
22128 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f  lloc() failure o
22129 63 63 75 72 65 64 20 69 6e 20 73 71 6c 69 74 65  ccured in sqlite
2212a 33 48 61 73 68 49 6e 73 65 72 74 28 29 2c 20 69  3HashInsert(), i
2212b 74 20 77 69 6c 6c 20 0a 20 20 20 20 20 20 2a 2a  t will .      **
2212c 20 72 65 74 75 72 6e 20 74 68 65 20 70 43 6f 6c   return the pCol
2212d 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20  l pointer to be 
2212e 64 65 6c 65 74 65 64 20 28 62 65 63 61 75 73 65  deleted (because
2212f 20 69 74 20 77 61 73 6e 27 74 20 61 64 64 65 64   it wasn't added
22130 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  .      ** to the
22131 20 68 61 73 68 20 74 61 62 6c 65 29 2e 0a 20 20   hash table)..  
22132 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73      */.      ass
22133 65 72 74 28 20 70 44 65 6c 3d 3d 30 20 7c 7c 20  ert( pDel==0 || 
22134 70 44 65 6c 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20  pDel==pColl );. 
22135 20 20 20 20 20 69 66 28 20 70 44 65 6c 21 3d 30       if( pDel!=0
22136 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   ){.        db->
22137 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
22138 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
22139 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 6c  3DbFree(db, pDel
2213a 29 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  );.        pColl
2213b 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
2213c 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2213d 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pColl;.}../*.**
2213e 20 50 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65   Parameter zName
2213f 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 55 54 46   points to a UTF
22140 2d 38 20 65 6e 63 6f 64 65 64 20 73 74 72 69 6e  -8 encoded strin
22141 67 20 6e 4e 61 6d 65 20 62 79 74 65 73 20 6c 6f  g nName bytes lo
22142 6e 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ng..** Return th
22143 65 20 43 6f 6c 6c 53 65 71 2a 20 70 6f 69 6e 74  e CollSeq* point
22144 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61  er for the colla
22145 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
22146 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a 20 66 6f 72  med zName.** for
22147 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 27 65   the encoding 'e
22148 6e 63 27 20 66 72 6f 6d 20 74 68 65 20 64 61 74  nc' from the dat
22149 61 62 61 73 65 20 27 64 62 27 2e 0a 2a 2a 0a 2a  abase 'db'..**.*
2214a 2a 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 73  * If the entry s
2214b 70 65 63 69 66 69 65 64 20 69 73 20 6e 6f 74 20  pecified is not 
2214c 66 6f 75 6e 64 20 61 6e 64 20 27 63 72 65 61 74  found and 'creat
2214d 65 27 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  e' is true, then
2214e 20 63 72 65 61 74 65 20 61 0a 2a 2a 20 6e 65 77   create a.** new
2214f 20 65 6e 74 72 79 2e 20 20 4f 74 68 65 72 77 69   entry.  Otherwi
22150 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  se return NULL..
22151 2a 2a 0a 2a 2a 20 41 20 73 65 70 61 72 61 74 65  **.** A separate
22152 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   function sqlite
22153 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29  3LocateCollSeq()
22154 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72   is a wrapper ar
22155 6f 75 6e 64 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ound.** this rou
22156 74 69 6e 65 2e 20 20 73 71 6c 69 74 65 33 4c 6f  tine.  sqlite3Lo
22157 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 6e  cateCollSeq() in
22158 76 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74  vokes the collat
22159 69 6f 6e 20 66 61 63 74 6f 72 79 0a 2a 2a 20 69  ion factory.** i
2215a 66 20 6e 65 63 65 73 73 61 72 79 20 61 6e 64 20  f necessary and 
2215b 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72  generates an err
2215c 6f 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68  or message if th
2215d 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
2215e 65 6e 63 65 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62  ence.** cannot b
2215f 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49  e found..*/.SQLI
22160 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
22161 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43  eq *sqlite3FindC
22162 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74 65  ollSeq(.  sqlite
22163 33 20 2a 64 62 2c 0a 20 20 75 38 20 65 6e 63 2c  3 *db,.  u8 enc,
22164 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
22165 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d  Name,.  int nNam
22166 65 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a  e,.  int create.
22167 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  ){.  CollSeq *pC
22168 6f 6c 6c 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65  oll;.  if( zName
22169 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20   ){.    pColl = 
2216a 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79  findCollSeqEntry
2216b 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  (db, zName, nNam
2216c 65 2c 20 63 72 65 61 74 65 29 3b 0a 20 20 7d 65  e, create);.  }e
2216d 6c 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  lse{.    pColl =
2216e 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a   db->pDfltColl;.
2216f 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 53 51    }.  assert( SQ
22170 4c 49 54 45 5f 55 54 46 38 3d 3d 31 20 26 26 20  LITE_UTF8==1 && 
22171 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3d 3d  SQLITE_UTF16LE==
22172 32 20 26 26 20 53 51 4c 49 54 45 5f 55 54 46 31  2 && SQLITE_UTF1
22173 36 42 45 3d 3d 33 20 29 3b 0a 20 20 61 73 73 65  6BE==3 );.  asse
22174 72 74 28 20 65 6e 63 3e 3d 53 51 4c 49 54 45 5f  rt( enc>=SQLITE_
22175 55 54 46 38 20 26 26 20 65 6e 63 3c 3d 53 51 4c  UTF8 && enc<=SQL
22176 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20  ITE_UTF16BE );. 
22177 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 70 43 6f   if( pColl ) pCo
22178 6c 6c 20 2b 3d 20 65 6e 63 2d 31 3b 0a 20 20 72  ll += enc-1;.  r
22179 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a  eturn pColl;.}..
2217a 2f 2a 20 44 75 72 69 6e 67 20 74 68 65 20 73 65  /* During the se
2217b 61 72 63 68 20 66 6f 72 20 74 68 65 20 62 65 73  arch for the bes
2217c 74 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  t function defin
2217d 69 74 69 6f 6e 2c 20 74 68 69 73 20 70 72 6f 63  ition, this proc
2217e 65 64 75 72 65 0a 2a 2a 20 69 73 20 63 61 6c 6c  edure.** is call
2217f 65 64 20 74 6f 20 74 65 73 74 20 68 6f 77 20 77  ed to test how w
22180 65 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  ell the function
22181 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
22182 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  irst argument.**
22183 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 65 71   matches the req
22184 75 65 73 74 20 66 6f 72 20 61 20 66 75 6e 63 74  uest for a funct
22185 69 6f 6e 20 77 69 74 68 20 6e 41 72 67 20 61 72  ion with nArg ar
22186 67 75 6d 65 6e 74 73 20 69 6e 20 61 20 73 79 73  guments in a sys
22187 74 65 6d 0a 2a 2a 20 74 68 61 74 20 75 73 65 73  tem.** that uses
22188 20 65 6e 63 6f 64 69 6e 67 20 65 6e 63 2e 20 54   encoding enc. T
22189 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
2218a 64 20 69 6e 64 69 63 61 74 65 73 20 68 6f 77 20  d indicates how 
2218b 77 65 6c 6c 20 74 68 65 0a 2a 2a 20 72 65 71 75  well the.** requ
2218c 65 73 74 20 69 73 20 6d 61 74 63 68 65 64 2e 20  est is matched. 
2218d 41 20 68 69 67 68 65 72 20 76 61 6c 75 65 20 69  A higher value i
2218e 6e 64 69 63 61 74 65 73 20 61 20 62 65 74 74 65  ndicates a bette
2218f 72 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 54  r match..**.** T
22190 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
22191 65 20 69 73 20 61 6c 77 61 79 73 20 62 65 74 77  e is always betw
22192 65 65 6e 20 31 20 61 6e 64 20 36 2c 20 61 73 20  een 1 and 6, as 
22193 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 31  follows:.**.** 1
22194 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67  : A variable arg
22195 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20  uments function 
22196 74 68 61 74 20 70 72 65 66 65 72 73 20 55 54 46  that prefers UTF
22197 2d 38 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36  -8 when a UTF-16
22198 0a 2a 2a 20 20 20 20 65 6e 63 6f 64 69 6e 67 20  .**    encoding 
22199 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72  is requested, or
2219a 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20   vice versa..** 
2219b 32 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72  2: A variable ar
2219c 67 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e  guments function
2219d 20 74 68 61 74 20 75 73 65 73 20 55 54 46 2d 31   that uses UTF-1
2219e 36 42 45 20 77 68 65 6e 20 55 54 46 2d 31 36 4c  6BE when UTF-16L
2219f 45 20 69 73 0a 2a 2a 20 20 20 20 72 65 71 75 65  E is.**    reque
221a0 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65  sted, or vice ve
221a1 72 73 61 2e 0a 2a 2a 20 33 3a 20 41 20 76 61 72  rsa..** 3: A var
221a2 69 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20  iable arguments 
221a3 66 75 6e 63 74 69 6f 6e 20 75 73 69 6e 67 20 74  function using t
221a4 68 65 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63  he same text enc
221a5 6f 64 69 6e 67 2e 0a 2a 2a 20 34 3a 20 41 20 66  oding..** 4: A f
221a6 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
221a7 20 65 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66   exact number of
221a8 20 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65   arguments reque
221a9 73 74 65 64 20 74 68 61 74 0a 2a 2a 20 20 20 20  sted that.**    
221aa 70 72 65 66 65 72 73 20 55 54 46 2d 38 20 77 68  prefers UTF-8 wh
221ab 65 6e 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f  en a UTF-16 enco
221ac 64 69 6e 67 20 69 73 20 72 65 71 75 65 73 74 65  ding is requeste
221ad 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61  d, or vice versa
221ae 2e 0a 2a 2a 20 35 3a 20 41 20 66 75 6e 63 74 69  ..** 5: A functi
221af 6f 6e 20 77 69 74 68 20 74 68 65 20 65 78 61 63  on with the exac
221b0 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  t number of argu
221b1 6d 65 6e 74 73 20 72 65 71 75 65 73 74 65 64 20  ments requested 
221b2 74 68 61 74 0a 2a 2a 20 20 20 20 70 72 65 66 65  that.**    prefe
221b3 72 73 20 55 54 46 2d 31 36 4c 45 20 77 68 65 6e  rs UTF-16LE when
221b4 20 55 54 46 2d 31 36 42 45 20 69 73 20 72 65 71   UTF-16BE is req
221b5 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20  uested, or vice 
221b6 76 65 72 73 61 2e 0a 2a 2a 20 36 3a 20 41 6e 20  versa..** 6: An 
221b7 65 78 61 63 74 20 6d 61 74 63 68 2e 0a 2a 2a 0a  exact match..**.
221b8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61  */.static int ma
221b9 74 63 68 51 75 61 6c 69 74 79 28 46 75 6e 63 44  tchQuality(FuncD
221ba 65 66 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c  ef *p, int nArg,
221bb 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20   u8 enc){.  int 
221bc 6d 61 74 63 68 20 3d 20 30 3b 0a 20 20 69 66 28  match = 0;.  if(
221bd 20 70 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20   p->nArg==-1 || 
221be 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c  p->nArg==nArg ||
221bf 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20   nArg==-1 ){.   
221c0 20 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20   match = 1;.    
221c1 69 66 28 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72  if( p->nArg==nAr
221c2 67 20 7c 7c 20 6e 41 72 67 3d 3d 2d 31 20 29 7b  g || nArg==-1 ){
221c3 0a 20 20 20 20 20 20 6d 61 74 63 68 20 3d 20 34  .      match = 4
221c4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
221c5 65 6e 63 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63  enc==p->iPrefEnc
221c6 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20   ){.      match 
221c7 2b 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20  += 2;.    }.    
221c8 65 6c 73 65 20 69 66 28 20 28 65 6e 63 3d 3d 53  else if( (enc==S
221c9 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 26 26  QLITE_UTF16LE &&
221ca 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51   p->iPrefEnc==SQ
221cb 4c 49 54 45 5f 55 54 46 31 36 42 45 29 20 7c 7c  LITE_UTF16BE) ||
221cc 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65  .             (e
221cd 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
221ce 42 45 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e  BE && p->iPrefEn
221cf 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  c==SQLITE_UTF16L
221d0 45 29 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63  E) ){.      matc
221d1 68 20 2b 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  h += 1;.    }.  
221d2 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 74 63 68  }.  return match
221d3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63  ;.}../*.** Searc
221d4 68 20 61 20 46 75 6e 63 44 65 66 48 61 73 68 20  h a FuncDefHash 
221d5 66 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77  for a function w
221d6 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61  ith the given na
221d7 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61  me.  Return.** a
221d8 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
221d9 6d 61 74 63 68 69 6e 67 20 46 75 6e 63 44 65 66  matching FuncDef
221da 20 69 66 20 66 6f 75 6e 64 2c 20 6f 72 20 30 20   if found, or 0 
221db 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
221dc 61 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  atch..*/.static 
221dd 46 75 6e 63 44 65 66 20 2a 66 75 6e 63 74 69 6f  FuncDef *functio
221de 6e 53 65 61 72 63 68 28 0a 20 20 46 75 6e 63 44  nSearch(.  FuncD
221df 65 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20  efHash *pHash,  
221e0 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 74 6f  /* Hash table to
221e1 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74   search */.  int
221e2 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   h,             
221e3 20 20 2f 2a 20 48 61 73 68 20 6f 66 20 74 68 65    /* Hash of the
221e4 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   name */.  const
221e5 20 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20   char *zFunc,   
221e6 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 75 6e 63 74  /* Name of funct
221e7 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75  ion */.  int nFu
221e8 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  nc            /*
221e9 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
221ea 20 69 6e 20 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a   in zFunc */.){.
221eb 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20    FuncDef *p;.  
221ec 66 6f 72 28 70 3d 70 48 61 73 68 2d 3e 61 5b 68  for(p=pHash->a[h
221ed 5d 3b 20 70 3b 20 70 3d 70 2d 3e 70 48 61 73 68  ]; p; p=p->pHash
221ee 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
221ef 65 33 53 74 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e  e3StrNICmp(p->zN
221f0 61 6d 65 2c 20 7a 46 75 6e 63 2c 20 6e 46 75 6e  ame, zFunc, nFun
221f1 63 29 3d 3d 30 20 26 26 20 70 2d 3e 7a 4e 61 6d  c)==0 && p->zNam
221f2 65 5b 6e 46 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20  e[nFunc]==0 ){. 
221f3 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20       return p;. 
221f4 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
221f5 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 0;.}../*.** In
221f6 73 65 72 74 20 61 20 6e 65 77 20 46 75 6e 63 44  sert a new FuncD
221f7 65 66 20 69 6e 74 6f 20 61 20 46 75 6e 63 44 65  ef into a FuncDe
221f8 66 48 61 73 68 20 68 61 73 68 20 74 61 62 6c 65  fHash hash table
221f9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
221fa 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
221fb 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 0a 20  FuncDefInsert(. 
221fc 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48   FuncDefHash *pH
221fd 61 73 68 2c 20 20 2f 2a 20 54 68 65 20 68 61 73  ash,  /* The has
221fe 68 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69  h table into whi
221ff 63 68 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a  ch to insert */.
22200 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20    FuncDef *pDef 
22201 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75         /* The fu
22202 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
22203 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 29  n to insert */.)
22204 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 4f 74  {.  FuncDef *pOt
22205 68 65 72 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  her;.  int nName
22206 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
22207 33 30 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b  30(pDef->zName);
22208 0a 20 20 75 38 20 63 31 20 3d 20 28 75 38 29 70  .  u8 c1 = (u8)p
22209 44 65 66 2d 3e 7a 4e 61 6d 65 5b 30 5d 3b 0a 20  Def->zName[0];. 
2220a 20 69 6e 74 20 68 20 3d 20 28 73 71 6c 69 74 65   int h = (sqlite
2220b 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 31  3UpperToLower[c1
2220c 5d 20 2b 20 6e 4e 61 6d 65 29 20 25 20 41 72 72  ] + nName) % Arr
2220d 61 79 53 69 7a 65 28 70 48 61 73 68 2d 3e 61 29  aySize(pHash->a)
2220e 3b 0a 20 20 70 4f 74 68 65 72 20 3d 20 66 75 6e  ;.  pOther = fun
2220f 63 74 69 6f 6e 53 65 61 72 63 68 28 70 48 61 73  ctionSearch(pHas
22210 68 2c 20 68 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d  h, h, pDef->zNam
22211 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 69 66 28  e, nName);.  if(
22212 20 70 4f 74 68 65 72 20 29 7b 0a 20 20 20 20 70   pOther ){.    p
22213 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 74  Def->pNext = pOt
22214 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  her->pNext;.    
22215 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20  pOther->pNext = 
22216 70 44 65 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pDef;.  }else{. 
22217 20 20 20 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d     pDef->pNext =
22218 20 30 3b 0a 20 20 20 20 70 44 65 66 2d 3e 70 48   0;.    pDef->pH
22219 61 73 68 20 3d 20 70 48 61 73 68 2d 3e 61 5b 68  ash = pHash->a[h
2221a 5d 3b 0a 20 20 20 20 70 48 61 73 68 2d 3e 61 5b  ];.    pHash->a[
2221b 68 5d 20 3d 20 70 44 65 66 3b 0a 20 20 7d 0a 7d  h] = pDef;.  }.}
2221c 0a 20 20 0a 20 20 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  .  .  ../*.** Lo
2221d 63 61 74 65 20 61 20 75 73 65 72 20 66 75 6e 63  cate a user func
2221e 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 6e 61 6d  tion given a nam
2221f 65 2c 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 61  e, a number of a
22220 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 61 20 66  rguments and a f
22221 6c 61 67 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  lag.** indicatin
22222 67 20 77 68 65 74 68 65 72 20 74 68 65 20 66 75  g whether the fu
22223 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 73 20 55  nction prefers U
22224 54 46 2d 31 36 20 6f 76 65 72 20 55 54 46 2d 38  TF-16 over UTF-8
22225 2e 20 20 52 65 74 75 72 6e 20 61 0a 2a 2a 20 70  .  Return a.** p
22226 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75  ointer to the Fu
22227 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20  ncDef structure 
22228 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 61  that defines tha
22229 74 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 72  t function, or r
2222a 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 66  eturn.** NULL if
2222b 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f   the function do
2222c 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a  es not exist..**
2222d 0a 2a 2a 20 49 66 20 74 68 65 20 63 72 65 61 74  .** If the creat
2222e 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69  eFlag argument i
2222f 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 20 6e  s true, then a n
22230 65 77 20 28 62 6c 61 6e 6b 29 20 46 75 6e 63 44  ew (blank) FuncD
22231 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  ef.** structure 
22232 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 6c  is created and l
22233 69 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 22 64  iked into the "d
22234 62 22 20 73 74 72 75 63 74 75 72 65 20 69 66 20  b" structure if 
22235 61 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 69 6e 67  a.** no matching
22236 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 76 69 6f   function previo
22237 75 73 6c 79 20 65 78 69 73 74 65 64 2e 20 20 57  usly existed.  W
22238 68 65 6e 20 63 72 65 61 74 65 46 6c 61 67 20 69  hen createFlag i
22239 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 74 68  s true.** and th
2223a 65 20 6e 41 72 67 20 70 61 72 61 6d 65 74 65 72  e nArg parameter
2223b 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 6f 6e 6c   is -1, then onl
2223c 79 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  y a function tha
2223d 74 20 61 63 63 65 70 74 73 0a 2a 2a 20 61 6e 79  t accepts.** any
2223e 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
2223f 65 6e 74 73 20 77 69 6c 6c 20 62 65 20 72 65 74  ents will be ret
22240 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
22241 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 66 61  createFlag is fa
22242 6c 73 65 20 61 6e 64 20 6e 41 72 67 20 69 73 20  lse and nArg is 
22243 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72  -1, then the fir
22244 73 74 20 76 61 6c 69 64 0a 2a 2a 20 66 75 6e 63  st valid.** func
22245 74 69 6f 6e 20 66 6f 75 6e 64 20 69 73 20 72 65  tion found is re
22246 74 75 72 6e 65 64 2e 20 20 41 20 66 75 6e 63 74  turned.  A funct
22247 69 6f 6e 20 69 73 20 76 61 6c 69 64 20 69 66 20  ion is valid if 
22248 65 69 74 68 65 72 20 78 46 75 6e 63 0a 2a 2a 20  either xFunc.** 
22249 6f 72 20 78 53 74 65 70 20 69 73 20 6e 6f 6e 2d  or xStep is non-
2224a 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63  zero..**.** If c
2224b 72 65 61 74 65 46 6c 61 67 20 69 73 20 66 61 6c  reateFlag is fal
2224c 73 65 2c 20 74 68 65 6e 20 61 20 66 75 6e 63 74  se, then a funct
2224d 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65 71  ion with the req
2224e 75 69 72 65 64 20 6e 61 6d 65 20 61 6e 64 0a 2a  uired name and.*
2224f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  * number of argu
22250 6d 65 6e 74 73 20 6d 61 79 20 62 65 20 72 65 74  ments may be ret
22251 75 72 6e 65 64 20 65 76 65 6e 20 69 66 20 74 68  urned even if th
22252 65 20 65 54 65 78 74 52 65 70 20 66 6c 61 67 20  e eTextRep flag 
22253 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63  does not.** matc
22254 68 20 74 68 61 74 20 72 65 71 75 65 73 74 65 64  h that requested
22255 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
22256 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c  ATE FuncDef *sql
22257 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
22258 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
22259 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65         /* An ope
2225a 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  n database */.  
2225b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
2225c 65 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  e, /* Name of th
2225d 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 4e 6f 74  e function.  Not
2225e 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64   null-terminated
2225f 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c   */.  int nName,
22260 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
22261 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
22262 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a   in the name */.
22263 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20    int nArg,     
22264 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
22265 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31  f arguments.  -1
22266 20 6d 65 61 6e 73 20 61 6e 79 20 6e 75 6d 62 65   means any numbe
22267 72 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20  r */.  u8 enc,  
22268 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
22269 66 65 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f  ferred text enco
2226a 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 63 72  ding */.  int cr
2226b 65 61 74 65 46 6c 61 67 20 20 20 20 20 2f 2a 20  eateFlag     /* 
2226c 43 72 65 61 74 65 20 6e 65 77 20 65 6e 74 72 79  Create new entry
2226d 20 69 66 20 74 72 75 65 20 61 6e 64 20 64 6f 65   if true and doe
2226e 73 20 6e 6f 74 20 6f 74 68 65 72 77 69 73 65 20  s not otherwise 
2226f 65 78 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 46 75  exist */.){.  Fu
22270 6e 63 44 65 66 20 2a 70 3b 20 20 20 20 20 20 20  ncDef *p;       
22271 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
22272 72 69 61 62 6c 65 20 2a 2f 0a 20 20 46 75 6e 63  riable */.  Func
22273 44 65 66 20 2a 70 42 65 73 74 20 3d 20 30 3b 20  Def *pBest = 0; 
22274 2f 2a 20 42 65 73 74 20 6d 61 74 63 68 20 66 6f  /* Best match fo
22275 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20  und so far */.  
22276 69 6e 74 20 62 65 73 74 53 63 6f 72 65 20 3d 20  int bestScore = 
22277 30 3b 20 20 2f 2a 20 53 63 6f 72 65 20 6f 66 20  0;  /* Score of 
22278 62 65 73 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20  best match */.  
22279 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20  int h;          
2227a 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75      /* Hash valu
2227b 65 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28  e */...  assert(
2227c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46   enc==SQLITE_UTF
2227d 38 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45  8 || enc==SQLITE
2227e 5f 55 54 46 31 36 4c 45 20 7c 7c 20 65 6e 63 3d  _UTF16LE || enc=
2227f 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
22280 29 3b 0a 20 20 69 66 28 20 6e 41 72 67 3c 2d 31  );.  if( nArg<-1
22281 20 29 20 6e 41 72 67 20 3d 20 2d 31 3b 0a 20 20   ) nArg = -1;.  
22282 68 20 3d 20 28 73 71 6c 69 74 65 33 55 70 70 65  h = (sqlite3Uppe
22283 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4e 61  rToLower[(u8)zNa
22284 6d 65 5b 30 5d 5d 20 2b 20 6e 4e 61 6d 65 29 20  me[0]] + nName) 
22285 25 20 41 72 72 61 79 53 69 7a 65 28 64 62 2d 3e  % ArraySize(db->
22286 61 46 75 6e 63 2e 61 29 3b 0a 0a 20 20 2f 2a 20  aFunc.a);..  /* 
22287 46 69 72 73 74 20 73 65 61 72 63 68 20 66 6f 72  First search for
22288 20 61 20 6d 61 74 63 68 20 61 6d 6f 6e 67 73 74   a match amongst
22289 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2228a 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
2228b 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 66  ns..  */.  p = f
2228c 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28 26 64  unctionSearch(&d
2228d 62 2d 3e 61 46 75 6e 63 2c 20 68 2c 20 7a 4e 61  b->aFunc, h, zNa
2228e 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 77 68  me, nName);.  wh
2228f 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 69 6e  ile( p ){.    in
22290 74 20 73 63 6f 72 65 20 3d 20 6d 61 74 63 68 51  t score = matchQ
22291 75 61 6c 69 74 79 28 70 2c 20 6e 41 72 67 2c 20  uality(p, nArg, 
22292 65 6e 63 29 3b 0a 20 20 20 20 69 66 28 20 73 63  enc);.    if( sc
22293 6f 72 65 3e 62 65 73 74 53 63 6f 72 65 20 29 7b  ore>bestScore ){
22294 0a 20 20 20 20 20 20 70 42 65 73 74 20 3d 20 70  .      pBest = p
22295 3b 0a 20 20 20 20 20 20 62 65 73 74 53 63 6f 72  ;.      bestScor
22296 65 20 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 7d  e = score;.    }
22297 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78  .    p = p->pNex
22298 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  t;.  }..  /* If 
22299 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e  no match is foun
2229a 64 2c 20 73 65 61 72 63 68 20 74 68 65 20 62 75  d, search the bu
2229b 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73  ilt-in functions
2229c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65  ..  **.  ** Exce
2229d 70 74 2c 20 69 66 20 63 72 65 61 74 65 46 6c 61  pt, if createFla
2229e 67 20 69 73 20 74 72 75 65 2c 20 74 68 61 74 20  g is true, that 
2229f 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 61 72  means that we ar
222a0 65 20 74 72 79 69 6e 67 20 74 6f 0a 20 20 2a 2a  e trying to.  **
222a1 20 69 6e 73 74 61 6c 6c 20 61 20 6e 65 77 20 66   install a new f
222a2 75 6e 63 74 69 6f 6e 2e 20 20 57 68 61 74 65 76  unction.  Whatev
222a3 65 72 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  er FuncDef struc
222a4 74 75 72 65 20 69 73 20 72 65 74 75 72 6e 65 64  ture is returned
222a5 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61 76 65 20   will.  ** have 
222a6 66 69 65 6c 64 73 20 6f 76 65 72 77 72 69 74 74  fields overwritt
222a7 65 6e 20 77 69 74 68 20 6e 65 77 20 69 6e 66 6f  en with new info
222a8 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 69  rmation appropri
222a9 61 74 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ate for the.  **
222aa 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20   new function.  
222ab 42 75 74 20 74 68 65 20 46 75 6e 63 44 65 66 73  But the FuncDefs
222ac 20 66 6f 72 20 62 75 69 6c 74 2d 69 6e 20 66 75   for built-in fu
222ad 6e 63 74 69 6f 6e 73 20 61 72 65 20 72 65 61 64  nctions are read
222ae 2d 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 6f 20 77  -only..  ** So w
222af 65 20 6d 75 73 74 20 6e 6f 74 20 73 65 61 72 63  e must not searc
222b0 68 20 66 6f 72 20 62 75 69 6c 74 2d 69 6e 73 20  h for built-ins 
222b1 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20  when creating a 
222b2 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20  new function..  
222b3 2a 2f 20 0a 20 20 69 66 28 20 21 63 72 65 61 74  */ .  if( !creat
222b4 65 46 6c 61 67 20 26 26 20 21 70 42 65 73 74 20  eFlag && !pBest 
222b5 29 7b 0a 20 20 20 20 46 75 6e 63 44 65 66 48 61  ){.    FuncDefHa
222b6 73 68 20 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f  sh *pHash = &GLO
222b7 42 41 4c 28 46 75 6e 63 44 65 66 48 61 73 68 2c  BAL(FuncDefHash,
222b8 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75   sqlite3GlobalFu
222b9 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 20 20 70 20  nctions);.    p 
222ba 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68  = functionSearch
222bb 28 70 48 61 73 68 2c 20 68 2c 20 7a 4e 61 6d 65  (pHash, h, zName
222bc 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 77 68  , nName);.    wh
222bd 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20  ile( p ){.      
222be 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d 61 74 63  int score = matc
222bf 68 51 75 61 6c 69 74 79 28 70 2c 20 6e 41 72 67  hQuality(p, nArg
222c0 2c 20 65 6e 63 29 3b 0a 20 20 20 20 20 20 69 66  , enc);.      if
222c1 28 20 73 63 6f 72 65 3e 62 65 73 74 53 63 6f 72  ( score>bestScor
222c2 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 65  e ){.        pBe
222c3 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  st = p;.        
222c4 62 65 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72  bestScore = scor
222c5 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
222c6 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20   p = p->pNext;. 
222c7 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
222c8 66 20 74 68 65 20 63 72 65 61 74 65 46 6c 61 67  f the createFlag
222c9 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 72   parameter is tr
222ca 75 65 20 61 6e 64 20 74 68 65 20 73 65 61 72 63  ue and the searc
222cb 68 20 64 69 64 20 6e 6f 74 20 72 65 76 65 61 6c  h did not reveal
222cc 20 61 6e 0a 20 20 2a 2a 20 65 78 61 63 74 20 6d   an.  ** exact m
222cd 61 74 63 68 20 66 6f 72 20 74 68 65 20 6e 61 6d  atch for the nam
222ce 65 2c 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  e, number of arg
222cf 75 6d 65 6e 74 73 20 61 6e 64 20 65 6e 63 6f 64  uments and encod
222d0 69 6e 67 2c 20 74 68 65 6e 20 61 64 64 20 61 0a  ing, then add a.
222d1 20 20 2a 2a 20 6e 65 77 20 65 6e 74 72 79 20 74    ** new entry t
222d2 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
222d3 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 2e 0a   and return it..
222d4 20 20 2a 2f 0a 20 20 69 66 28 20 63 72 65 61 74    */.  if( creat
222d5 65 46 6c 61 67 20 26 26 20 28 62 65 73 74 53 63  eFlag && (bestSc
222d6 6f 72 65 3c 36 20 7c 7c 20 70 42 65 73 74 2d 3e  ore<6 || pBest->
222d7 6e 41 72 67 21 3d 6e 41 72 67 29 20 26 26 20 0a  nArg!=nArg) && .
222d8 20 20 20 20 20 20 28 70 42 65 73 74 20 3d 20 73        (pBest = s
222d9 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
222da 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  ro(db, sizeof(*p
222db 42 65 73 74 29 2b 6e 4e 61 6d 65 2b 31 29 29 21  Best)+nName+1))!
222dc 3d 30 20 29 7b 0a 20 20 20 20 70 42 65 73 74 2d  =0 ){.    pBest-
222dd 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 20 2a  >zName = (char *
222de 29 26 70 42 65 73 74 5b 31 5d 3b 0a 20 20 20 20  )&pBest[1];.    
222df 70 42 65 73 74 2d 3e 6e 41 72 67 20 3d 20 28 75  pBest->nArg = (u
222e0 31 36 29 6e 41 72 67 3b 0a 20 20 20 20 70 42 65  16)nArg;.    pBe
222e1 73 74 2d 3e 69 50 72 65 66 45 6e 63 20 3d 20 65  st->iPrefEnc = e
222e2 6e 63 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  nc;.    memcpy(p
222e3 42 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61  Best->zName, zNa
222e4 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
222e5 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61  pBest->zName[nNa
222e6 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  me] = 0;.    sql
222e7 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72  ite3FuncDefInser
222e8 74 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 70 42  t(&db->aFunc, pB
222e9 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  est);.  }..  if(
222ea 20 70 42 65 73 74 20 26 26 20 28 70 42 65 73 74   pBest && (pBest
222eb 2d 3e 78 53 74 65 70 20 7c 7c 20 70 42 65 73 74  ->xStep || pBest
222ec 2d 3e 78 46 75 6e 63 20 7c 7c 20 63 72 65 61 74  ->xFunc || creat
222ed 65 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 72 65  eFlag) ){.    re
222ee 74 75 72 6e 20 70 42 65 73 74 3b 0a 20 20 7d 0a  turn pBest;.  }.
222ef 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
222f0 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 72 65  *.** Free all re
222f1 73 6f 75 72 63 65 73 20 68 65 6c 64 20 62 79 20  sources held by 
222f2 74 68 65 20 73 63 68 65 6d 61 20 73 74 72 75 63  the schema struc
222f3 74 75 72 65 2e 20 54 68 65 20 76 6f 69 64 2a 20  ture. The void* 
222f4 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 0a  argument points.
222f5 2a 2a 20 61 74 20 61 20 53 63 68 65 6d 61 20 73  ** at a Schema s
222f6 74 72 75 63 74 2e 20 54 68 69 73 20 66 75 6e 63  truct. This func
222f7 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 63 61  tion does not ca
222f8 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  ll sqlite3DbFree
222f9 28 64 62 2c 20 29 20 6f 6e 20 74 68 65 20 0a 2a  (db, ) on the .*
222fa 2a 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66  * pointer itself
222fb 2c 20 69 74 20 6a 75 73 74 20 63 6c 65 61 6e 73  , it just cleans
222fc 20 75 70 20 73 75 62 73 69 64 75 61 72 79 20 72   up subsiduary r
222fd 65 73 6f 75 72 63 65 73 20 28 69 2e 65 2e 20 74  esources (i.e. t
222fe 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f  he contents.** o
222ff 66 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 73  f the schema has
22300 68 20 74 61 62 6c 65 73 29 2e 0a 2a 2a 0a 2a 2a  h tables)..**.**
22301 20 54 68 65 20 53 63 68 65 6d 61 2e 63 61 63 68   The Schema.cach
22302 65 5f 73 69 7a 65 20 76 61 72 69 61 62 6c 65 20  e_size variable 
22303 69 73 20 6e 6f 74 20 63 6c 65 61 72 65 64 2e 0a  is not cleared..
22304 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22305 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63  E void sqlite3Sc
22306 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 70  hemaFree(void *p
22307 29 7b 0a 20 20 48 61 73 68 20 74 65 6d 70 31 3b  ){.  Hash temp1;
22308 0a 20 20 48 61 73 68 20 74 65 6d 70 32 3b 0a 20  .  Hash temp2;. 
22309 20 48 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d   HashElem *pElem
2230a 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  ;.  Schema *pSch
2230b 65 6d 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29  ema = (Schema *)
2230c 70 3b 0a 0a 20 20 74 65 6d 70 31 20 3d 20 70 53  p;..  temp1 = pS
2230d 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a  chema->tblHash;.
2230e 20 20 74 65 6d 70 32 20 3d 20 70 53 63 68 65 6d    temp2 = pSchem
2230f 61 2d 3e 74 72 69 67 48 61 73 68 3b 0a 20 20 73  a->trigHash;.  s
22310 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
22311 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73  pSchema->trigHas
22312 68 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  h, 0);.  sqlite3
22313 48 61 73 68 43 6c 65 61 72 28 26 70 53 63 68 65  HashClear(&pSche
22314 6d 61 2d 3e 61 46 4b 65 79 29 3b 0a 20 20 73 71  ma->aFKey);.  sq
22315 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26  lite3HashClear(&
22316 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
22317 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73  );.  for(pElem=s
22318 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
22319 74 65 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 70  temp2); pElem; p
2231a 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e  Elem=sqliteHashN
2231b 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
2231c 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
2231d 69 67 67 65 72 28 30 2c 20 28 54 72 69 67 67 65  igger(0, (Trigge
2231e 72 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74  r*)sqliteHashDat
2231f 61 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20  a(pElem));.  }. 
22320 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
22321 72 28 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 6c  r(&temp2);.  sql
22322 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53  ite3HashInit(&pS
22323 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20  chema->tblHash, 
22324 30 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d  0);.  for(pElem=
22325 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
22326 26 74 65 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20  &temp1); pElem; 
22327 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68  pElem=sqliteHash
22328 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20  Next(pElem)){.  
22329 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2232a 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70  sqliteHashData(p
2232b 45 6c 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74  Elem);.    sqlit
2232c 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54  e3DeleteTable(pT
2232d 61 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ab);.  }.  sqlit
2232e 65 33 48 61 73 68 43 6c 65 61 72 28 26 74 65 6d  e3HashClear(&tem
2232f 70 31 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e  p1);.  pSchema->
22330 70 53 65 71 54 61 62 20 3d 20 30 3b 0a 20 20 70  pSeqTab = 0;.  p
22331 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26 3d  Schema->flags &=
22332 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65   ~DB_SchemaLoade
22333 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  d;.}../*.** Find
22334 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20   and return the 
22335 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65  schema associate
22336 64 20 77 69 74 68 20 61 20 42 54 72 65 65 2e 20  d with a BTree. 
22337 20 43 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77   Create.** a new
22338 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61 72   one if necessar
22339 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
2233a 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c  VATE Schema *sql
2233b 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 73 71  ite3SchemaGet(sq
2233c 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 72 65 65  lite3 *db, Btree
2233d 20 2a 70 42 74 29 7b 0a 20 20 53 63 68 65 6d 61   *pBt){.  Schema
2233e 20 2a 20 70 3b 0a 20 20 69 66 28 20 70 42 74 20   * p;.  if( pBt 
2233f 29 7b 0a 20 20 20 20 70 20 3d 20 28 53 63 68 65  ){.    p = (Sche
22340 6d 61 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65  ma *)sqlite3Btre
22341 65 53 63 68 65 6d 61 28 70 42 74 2c 20 73 69 7a  eSchema(pBt, siz
22342 65 6f 66 28 53 63 68 65 6d 61 29 2c 20 73 71 6c  eof(Schema), sql
22343 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 29 3b  ite3SchemaFree);
22344 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20  .  }else{.    p 
22345 3d 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69  = (Schema *)sqli
22346 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69  te3MallocZero(si
22347 7a 65 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a 20  zeof(Schema));. 
22348 20 7d 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20   }.  if( !p ){. 
22349 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2234a 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  led = 1;.  }else
2234b 20 69 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c 65   if ( 0==p->file
2234c 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 73  _format ){.    s
2234d 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
2234e 70 2d 3e 74 62 6c 48 61 73 68 2c 20 30 29 3b 0a  p->tblHash, 0);.
2234f 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
22350 6e 69 74 28 26 70 2d 3e 69 64 78 48 61 73 68 2c  nit(&p->idxHash,
22351 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
22352 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 74 72 69  HashInit(&p->tri
22353 67 48 61 73 68 2c 20 30 29 3b 0a 20 20 20 20 73  gHash, 0);.    s
22354 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
22355 70 2d 3e 61 46 4b 65 79 2c 20 31 29 3b 0a 20 20  p->aFKey, 1);.  
22356 20 20 70 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54    p->enc = SQLIT
22357 45 5f 55 54 46 38 3b 0a 20 20 7d 0a 20 20 72 65  E_UTF8;.  }.  re
22358 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  turn p;.}../****
22359 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2235a 66 20 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a  f callback.c ***
2235b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2235c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2235d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2235e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2235f 20 66 69 6c 65 20 64 65 6c 65 74 65 2e 63 20 2a   file delete.c *
22360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22361 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22362 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
22363 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
22364 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
22365 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
22366 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
22367 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
22368 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
22369 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
2236a 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
2236b 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
2236c 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
2236d 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
2236e 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
2236f 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
22370 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
22371 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
22372 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
22373 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
22374 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
22375 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
22376 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22377 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22378 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22379 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2237a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
2237b 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72  ontains C code r
2237c 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65  outines that are
2237d 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
2237e 61 72 73 65 72 0a 2a 2a 20 69 6e 20 6f 72 64 65  arser.** in orde
2237f 72 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f  r to generate co
22380 64 65 20 66 6f 72 20 44 45 4c 45 54 45 20 46 52  de for DELETE FR
22381 4f 4d 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  OM statements..*
22382 2a 0a 2a 2a 20 24 49 64 3a 20 64 65 6c 65 74 65  *.** $Id: delete
22383 2e 63 2c 76 20 31 2e 31 39 31 20 32 30 30 38 2f  .c,v 1.191 2008/
22384 31 32 2f 32 33 20 32 33 3a 35 36 3a 32 32 20 64  12/23 23:56:22 d
22385 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  rh Exp $.*/../*.
22386 2a 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79  ** Look up every
22387 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e   table that is n
22388 61 6d 65 64 20 69 6e 20 70 53 72 63 2e 20 20 49  amed in pSrc.  I
22389 66 20 61 6e 79 20 74 61 62 6c 65 20 69 73 20 6e  f any table is n
2238a 6f 74 20 66 6f 75 6e 64 2c 0a 2a 2a 20 61 64 64  ot found,.** add
2238b 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
2238c 65 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72  e to pParse->zEr
2238d 72 4d 73 67 20 61 6e 64 20 72 65 74 75 72 6e 20  rMsg and return 
2238e 4e 55 4c 4c 2e 20 20 49 66 20 61 6c 6c 20 74 61  NULL.  If all ta
2238f 62 6c 65 73 0a 2a 2a 20 61 72 65 20 66 6f 75 6e  bles.** are foun
22390 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  d, return a poin
22391 74 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20  ter to the last 
22392 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
22393 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a  _PRIVATE Table *
22394 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
22395 6f 6b 75 70 28 50 61 72 73 65 20 2a 70 50 61 72  okup(Parse *pPar
22396 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72  se, SrcList *pSr
22397 63 29 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63  c){.  struct Src
22398 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
22399 20 3d 20 70 53 72 63 2d 3e 61 3b 0a 20 20 54 61   = pSrc->a;.  Ta
2239a 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 61 73 73  ble *pTab;.  ass
2239b 65 72 74 28 20 70 49 74 65 6d 20 26 26 20 70 53  ert( pItem && pS
2239c 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  rc->nSrc==1 );. 
2239d 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
2239e 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
2239f 65 2c 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61  e, 0, pItem->zNa
223a0 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61  me, pItem->zData
223a1 62 61 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  base);.  sqlite3
223a2 44 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65  DeleteTable(pIte
223a3 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 70 49 74 65  m->pTab);.  pIte
223a4 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a  m->pTab = pTab;.
223a5 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
223a6 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a    pTab->nRef++;.
223a7 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
223a8 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70  3IndexedByLookup
223a9 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 29 20  (pParse, pItem) 
223aa 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b  ){.    pTab = 0;
223ab 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54  .  }.  return pT
223ac 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  ab;.}../*.** Che
223ad 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
223ae 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20  the given table 
223af 69 73 20 77 72 69 74 61 62 6c 65 2e 20 20 49 66  is writable.  If
223b0 20 69 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 77 72   it is not.** wr
223b1 69 74 61 62 6c 65 2c 20 67 65 6e 65 72 61 74 65  itable, generate
223b2 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
223b3 65 20 61 6e 64 20 72 65 74 75 72 6e 20 31 2e 20  e and return 1. 
223b4 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 77 72 69   If it is.** wri
223b5 74 61 62 6c 65 20 72 65 74 75 72 6e 20 30 3b 0a  table return 0;.
223b6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
223b7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52  E int sqlite3IsR
223b8 65 61 64 4f 6e 6c 79 28 50 61 72 73 65 20 2a 70  eadOnly(Parse *p
223b9 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
223ba 61 62 2c 20 69 6e 74 20 76 69 65 77 4f 6b 29 7b  ab, int viewOk){
223bb 0a 20 20 69 66 28 20 28 28 70 54 61 62 2d 3e 74  .  if( ((pTab->t
223bc 61 62 46 6c 61 67 73 20 26 20 54 46 5f 52 65 61  abFlags & TF_Rea
223bd 64 6f 6e 6c 79 29 21 3d 30 0a 20 20 20 20 20 20  donly)!=0.      
223be 20 20 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62    && (pParse->db
223bf 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
223c0 5f 57 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30  _WriteSchema)==0
223c1 0a 20 20 20 20 20 20 20 20 26 26 20 70 50 61 72  .        && pPar
223c2 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29 20 0a  se->nested==0) .
223c3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
223c4 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
223c5 0a 20 20 20 20 20 20 7c 7c 20 28 70 54 61 62 2d  .      || (pTab-
223c6 3e 70 4d 6f 64 20 26 26 20 70 54 61 62 2d 3e 70  >pMod && pTab->p
223c7 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 55  Mod->pModule->xU
223c8 70 64 61 74 65 3d 3d 30 29 0a 23 65 6e 64 69 66  pdate==0).#endif
223c9 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  .  ){.    sqlite
223ca 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
223cb 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20  , "table %s may 
223cc 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 22  not be modified"
223cd 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
223ce 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
223cf 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
223d0 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28  _OMIT_VIEW.  if(
223d1 20 21 76 69 65 77 4f 6b 20 26 26 20 70 54 61 62   !viewOk && pTab
223d2 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
223d3 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
223d4 28 70 50 61 72 73 65 2c 22 63 61 6e 6e 6f 74 20  (pParse,"cannot 
223d5 6d 6f 64 69 66 79 20 25 73 20 62 65 63 61 75 73  modify %s becaus
223d6 65 20 69 74 20 69 73 20 61 20 76 69 65 77 22 2c  e it is a view",
223d7 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
223d8 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
223d9 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
223da 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  0;.}../*.** Gene
223db 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
223dc 69 6c 6c 20 6f 70 65 6e 20 61 20 74 61 62 6c 65  ill open a table
223dd 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2f   for reading..*/
223de 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
223df 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e  void sqlite3Open
223e0 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
223e1 70 2c 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65  p,       /* Gene
223e2 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74  rate code into t
223e3 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e  his VDBE */.  in
223e4 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 2f 2a  t iCur,       /*
223e5 20 54 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62   The cursor numb
223e6 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  er of the table 
223e7 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
223e8 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
223e9 62 61 73 65 20 69 6e 64 65 78 20 69 6e 20 73 71  base index in sq
223ea 6c 69 74 65 33 2e 61 44 62 5b 5d 20 2a 2f 0a 20  lite3.aDb[] */. 
223eb 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
223ec 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f   /* The table to
223ed 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
223ee 69 6e 74 20 6f 70 63 6f 64 65 20 20 20 20 20 20  int opcode      
223ef 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f  /* OP_OpenRead o
223f0 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a  r OP_OpenWrite *
223f1 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  /.){.  Vdbe *v;.
223f2 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
223f3 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 3b 0a  pTab) ) return;.
223f4 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
223f5 56 64 62 65 28 70 29 3b 0a 20 20 61 73 73 65 72  Vdbe(p);.  asser
223f6 74 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  t( opcode==OP_Op
223f7 65 6e 57 72 69 74 65 20 7c 7c 20 6f 70 63 6f 64  enWrite || opcod
223f8 65 3d 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 29  e==OP_OpenRead )
223f9 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ;.  sqlite3Table
223fa 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 70 54 61  Lock(p, iDb, pTa
223fb 62 2d 3e 74 6e 75 6d 2c 20 28 6f 70 63 6f 64 65  b->tnum, (opcode
223fc 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3f  ==OP_OpenWrite)?
223fd 31 3a 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  1:0, pTab->zName
223fe 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
223ff 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74  AddOp2(v, OP_Set
22400 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70  NumColumns, 0, p
22401 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 73 71  Tab->nCol);.  sq
22402 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
22403 76 2c 20 6f 70 63 6f 64 65 2c 20 69 43 75 72 2c  v, opcode, iCur,
22404 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62   pTab->tnum, iDb
22405 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  );.  VdbeComment
22406 28 28 76 2c 20 22 25 73 22 2c 20 70 54 61 62 2d  ((v, "%s", pTab-
22407 3e 7a 4e 61 6d 65 29 29 3b 0a 7d 0a 0a 0a 23 69  >zName));.}...#i
22408 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
22409 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20  E_OMIT_VIEW) && 
2240a 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2240b 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a  OMIT_TRIGGER)./*
2240c 0a 2a 2a 20 45 76 61 6c 75 61 74 65 20 61 20 76  .** Evaluate a v
2240d 69 65 77 20 61 6e 64 20 73 74 6f 72 65 20 69 74  iew and store it
2240e 73 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 65  s result in an e
2240f 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 20  phemeral table. 
22410 20 54 68 65 0a 2a 2a 20 70 57 68 65 72 65 20 61   The.** pWhere a
22411 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 6f 70  rgument is an op
22412 74 69 6f 6e 61 6c 20 57 48 45 52 45 20 63 6c 61  tional WHERE cla
22413 75 73 65 20 74 68 61 74 20 72 65 73 74 72 69 63  use that restric
22414 74 73 20 74 68 65 0a 2a 2a 20 73 65 74 20 6f 66  ts the.** set of
22415 20 72 6f 77 73 20 69 6e 20 74 68 65 20 76 69 65   rows in the vie
22416 77 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 65  w that are to be
22417 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 65 70   added to the ep
22418 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a  hemeral table..*
22419 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2241a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74   void sqlite3Mat
2241b 65 72 69 61 6c 69 7a 65 56 69 65 77 28 0a 20 20  erializeView(.  
2241c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2241d 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2241e 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
2241f 6c 65 20 2a 70 56 69 65 77 2c 20 20 20 20 20 20  le *pView,      
22420 20 20 2f 2a 20 56 69 65 77 20 64 65 66 69 6e 69    /* View defini
22421 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  tion */.  Expr *
22422 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 2f  pWhere,        /
22423 2a 20 4f 70 74 69 6f 6e 61 6c 20 57 48 45 52 45  * Optional WHERE
22424 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 61 64   clause to be ad
22425 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75  ded */.  int iCu
22426 72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r             /*
22427 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
22428 6f 72 20 65 70 68 65 6d 65 72 69 61 6c 20 74 61  or ephemerial ta
22429 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65  ble */.){.  Sele
2242a 63 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 53  ctDest dest;.  S
2242b 65 6c 65 63 74 20 2a 70 44 75 70 3b 0a 20 20 73  elect *pDup;.  s
2242c 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2242d 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 70 44 75 70  rse->db;..  pDup
2242e 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
2242f 44 75 70 28 64 62 2c 20 70 56 69 65 77 2d 3e 70  Dup(db, pView->p
22430 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28 20 70  Select);.  if( p
22431 57 68 65 72 65 20 29 7b 0a 20 20 20 20 53 72 63  Where ){.    Src
22432 4c 69 73 74 20 2a 70 46 72 6f 6d 3b 0a 20 20 20  List *pFrom;.   
22433 20 54 6f 6b 65 6e 20 76 69 65 77 4e 61 6d 65 3b   Token viewName;
22434 0a 20 20 20 20 0a 20 20 20 20 70 57 68 65 72 65  .    .    pWhere
22435 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
22436 70 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20  p(db, pWhere);. 
22437 20 20 20 76 69 65 77 4e 61 6d 65 2e 7a 20 3d 20     viewName.z = 
22438 28 75 38 2a 29 70 56 69 65 77 2d 3e 7a 4e 61 6d  (u8*)pView->zNam
22439 65 3b 0a 20 20 20 20 76 69 65 77 4e 61 6d 65 2e  e;.    viewName.
2243a 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 69 6e  n = (unsigned in
2243b 74 29 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  t)sqlite3Strlen3
2243c 30 28 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 76  0((const char*)v
2243d 69 65 77 4e 61 6d 65 2e 7a 29 3b 0a 20 20 20 20  iewName.z);.    
2243e 70 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 53  pFrom = sqlite3S
2243f 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d  rcListAppendFrom
22440 54 65 72 6d 28 70 50 61 72 73 65 2c 20 30 2c 20  Term(pParse, 0, 
22441 30 2c 20 30 2c 20 26 76 69 65 77 4e 61 6d 65 2c  0, 0, &viewName,
22442 20 70 44 75 70 2c 20 30 2c 30 29 3b 0a 20 20 20   pDup, 0,0);.   
22443 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 53   pDup = sqlite3S
22444 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
22445 20 30 2c 20 70 46 72 6f 6d 2c 20 70 57 68 65 72   0, pFrom, pWher
22446 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  e, 0, 0, 0, 0, 0
22447 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  , 0);.  }.  sqli
22448 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
22449 74 28 26 64 65 73 74 2c 20 53 52 54 5f 45 70 68  t(&dest, SRT_Eph
2244a 65 6d 54 61 62 2c 20 69 43 75 72 29 3b 0a 20 20  emTab, iCur);.  
2244b 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
2244c 61 72 73 65 2c 20 70 44 75 70 2c 20 26 64 65 73  arse, pDup, &des
2244d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  t);.  sqlite3Sel
2244e 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44  ectDelete(db, pD
2244f 75 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  up);.}.#endif /*
22450 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
22451 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21  _OMIT_VIEW) && !
22452 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
22453 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a  MIT_TRIGGER) */.
22454 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
22455 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54  ITE_ENABLE_UPDAT
22456 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20  E_DELETE_LIMIT) 
22457 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
22458 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
22459 29 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  )./*.** Generate
2245a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
2245b 72 65 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ree to implement
2245c 20 74 68 65 20 57 48 45 52 45 2c 20 4f 52 44 45   the WHERE, ORDE
2245d 52 20 42 59 2c 0a 2a 2a 20 61 6e 64 20 4c 49 4d  R BY,.** and LIM
2245e 49 54 2f 4f 46 46 53 45 54 20 70 6f 72 74 69 6f  IT/OFFSET portio
2245f 6e 20 6f 66 20 44 45 4c 45 54 45 20 61 6e 64 20  n of DELETE and 
22460 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
22461 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c  s..**.**     DEL
22462 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77  ETE FROM table_w
22463 78 79 7a 20 57 48 45 52 45 20 61 3c 35 20 4f 52  xyz WHERE a<5 OR
22464 44 45 52 20 42 59 20 61 20 4c 49 4d 49 54 20 31  DER BY a LIMIT 1
22465 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ;.**            
22466 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22467 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  \_______________
22468 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20  ___________/.** 
22469 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2246a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4c                pL
2246b 69 6d 69 74 57 68 65 72 65 20 28 70 49 6e 43 6c  imitWhere (pInCl
2246c 61 75 73 65 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ause).*/.SQLITE_
2246d 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
2246e 6c 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28  lite3LimitWhere(
2246f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
22470 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
22471 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
22472 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
22473 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20  st *pSrc,       
22474 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46          /* the F
22475 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20 77 68  ROM clause -- wh
22476 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20 73 63  ich tables to sc
22477 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  an */.  Expr *pW
22478 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 20 20  here,           
22479 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2247a 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  E clause.  May b
2247b 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72  e null */.  Expr
2247c 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
2247d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2247e 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e  ORDER BY clause.
2247f 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f    May be null */
22480 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c  .  Expr *pLimit,
22481 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22482 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61  /* The LIMIT cla
22483 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c  use.  May be nul
22484 6c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66  l */.  Expr *pOf
22485 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20  fset,           
22486 20 20 20 20 2f 2a 20 54 68 65 20 4f 46 46 53 45      /* The OFFSE
22487 54 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  T clause.  May b
22488 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 63 68 61 72  e null */.  char
22489 20 2a 7a 53 74 6d 74 54 79 70 65 20 20 20 20 20   *zStmtType     
2248a 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69 74 68           /* Eith
2248b 65 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44  er DELETE or UPD
2248c 41 54 45 2e 20 20 46 6f 72 20 65 72 72 6f 72 20  ATE.  For error 
2248d 6d 65 73 73 61 67 65 73 2e 20 2a 2f 0a 29 7b 0a  messages. */.){.
2248e 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 52 6f    Expr *pWhereRo
2248f 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 2f  wid = NULL;    /
22490 2a 20 57 48 45 52 45 20 72 6f 77 69 64 20 2e 2e  * WHERE rowid ..
22491 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 49 6e 43   */.  Expr *pInC
22492 6c 61 75 73 65 20 3d 20 4e 55 4c 4c 3b 20 20 20  lause = NULL;   
22493 20 20 20 2f 2a 20 57 48 45 52 45 20 72 6f 77 69     /* WHERE rowi
22494 64 20 49 4e 20 28 20 73 65 6c 65 63 74 20 29 20  d IN ( select ) 
22495 2a 2f 0a 20 20 45 78 70 72 20 2a 70 53 65 6c 65  */.  Expr *pSele
22496 63 74 52 6f 77 69 64 20 3d 20 4e 55 4c 4c 3b 20  ctRowid = NULL; 
22497 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f 77 69    /* SELECT rowi
22498 64 20 2e 2e 2e 20 2a 2f 0a 20 20 45 78 70 72 4c  d ... */.  ExprL
22499 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 4e 55  ist *pEList = NU
2249a 4c 4c 3b 20 20 20 20 20 2f 2a 20 45 78 70 72 65  LL;     /* Expre
2249b 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61  ssion list conta
2249c 6e 69 6e 67 20 6f 6e 6c 79 20 70 53 65 6c 65 63  ning only pSelec
2249d 74 52 6f 77 69 64 20 2a 2f 0a 20 20 53 72 63 4c  tRowid */.  SrcL
2249e 69 73 74 20 2a 70 53 65 6c 65 63 74 53 72 63 20  ist *pSelectSrc 
2249f 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 53 45 4c 45  = NULL;  /* SELE
224a0 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 78 20  CT rowid FROM x 
224a1 2e 2e 2e 20 28 64 75 70 20 6f 66 20 70 53 72 63  ... (dup of pSrc
224a2 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  ) */.  Select *p
224a3 53 65 6c 65 63 74 20 3d 20 4e 55 4c 4c 3b 20 20  Select = NULL;  
224a4 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20      /* Complete 
224a5 53 45 4c 45 43 54 20 74 72 65 65 20 2a 2f 0a 0a  SELECT tree */..
224a6 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
224a7 74 68 65 72 65 20 69 73 6e 27 74 20 61 6e 20 4f  there isn't an O
224a8 52 44 45 52 20 42 59 20 77 69 74 68 6f 75 74 20  RDER BY without 
224a9 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a  a LIMIT clause..
224aa 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65    */.  if( pOrde
224ab 72 42 79 20 26 26 20 28 70 4c 69 6d 69 74 20 3d  rBy && (pLimit =
224ac 3d 20 30 29 20 29 20 7b 0a 20 20 20 20 73 71 6c  = 0) ) {.    sql
224ad 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
224ae 72 73 65 2c 20 22 4f 52 44 45 52 20 42 59 20 77  rse, "ORDER BY w
224af 69 74 68 6f 75 74 20 4c 49 4d 49 54 20 6f 6e 20  ithout LIMIT on 
224b0 25 73 22 2c 20 7a 53 74 6d 74 54 79 70 65 29 3b  %s", zStmtType);
224b1 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 61 72  .    pParse->par
224b2 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 20  seError = 1;.   
224b3 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72   goto limit_wher
224b4 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20 7d  e_cleanup_2;.  }
224b5 0a 0a 20 20 2f 2a 20 57 65 20 6f 6e 6c 79 20 6e  ..  /* We only n
224b6 65 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  eed to generate 
224b7 61 20 73 65 6c 65 63 74 20 65 78 70 72 65 73 73  a select express
224b8 69 6f 6e 20 69 66 20 74 68 65 72 65 0a 20 20 2a  ion if there.  *
224b9 2a 20 69 73 20 61 20 6c 69 6d 69 74 2f 6f 66 66  * is a limit/off
224ba 73 65 74 20 74 65 72 6d 20 74 6f 20 65 6e 66 6f  set term to enfo
224bb 72 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  rce..  */.  if( 
224bc 70 4c 69 6d 69 74 20 3d 3d 20 30 20 29 20 7b 0a  pLimit == 0 ) {.
224bd 20 20 20 20 2f 2a 20 69 66 20 70 4c 69 6d 69 74      /* if pLimit
224be 20 69 73 20 6e 75 6c 6c 2c 20 70 4f 66 66 73 65   is null, pOffse
224bf 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65  t will always be
224c0 20 6e 75 6c 6c 20 61 73 20 77 65 6c 6c 2e 20 2a   null as well. *
224c1 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  /.    assert( pO
224c2 66 66 73 65 74 20 3d 3d 20 30 20 29 3b 0a 20 20  ffset == 0 );.  
224c3 20 20 72 65 74 75 72 6e 20 70 57 68 65 72 65 3b    return pWhere;
224c4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
224c5 61 74 65 20 61 20 73 65 6c 65 63 74 20 65 78 70  ate a select exp
224c6 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74 6f 20  ression tree to 
224c7 65 6e 66 6f 72 63 65 20 74 68 65 20 6c 69 6d 69  enforce the limi
224c8 74 2f 6f 66 66 73 65 74 20 0a 20 20 2a 2a 20 74  t/offset .  ** t
224c9 65 72 6d 20 66 6f 72 20 74 68 65 20 44 45 4c 45  erm for the DELE
224ca 54 45 20 6f 72 20 55 50 44 41 54 45 20 73 74 61  TE or UPDATE sta
224cb 74 65 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78 61  tement.  For exa
224cc 6d 70 6c 65 3a 0a 20 20 2a 2a 20 20 20 44 45 4c  mple:.  **   DEL
224cd 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61  ETE FROM table_a
224ce 20 57 48 45 52 45 20 63 6f 6c 31 3d 31 20 4f 52   WHERE col1=1 OR
224cf 44 45 52 20 42 59 20 63 6f 6c 32 20 4c 49 4d 49  DER BY col2 LIMI
224d0 54 20 31 20 4f 46 46 53 45 54 20 31 0a 20 20 2a  T 1 OFFSET 1.  *
224d1 2a 20 62 65 63 6f 6d 65 73 3a 0a 20 20 2a 2a 20  * becomes:.  ** 
224d2 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61    DELETE FROM ta
224d3 62 6c 65 5f 61 20 57 48 45 52 45 20 72 6f 77 69  ble_a WHERE rowi
224d4 64 20 49 4e 20 28 20 0a 20 20 2a 2a 20 20 20 20  d IN ( .  **    
224d5 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52   SELECT rowid FR
224d6 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52 45  OM table_a WHERE
224d7 20 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42 59   col1=1 ORDER BY
224d8 20 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f 46   col2 LIMIT 1 OF
224d9 46 53 45 54 20 31 0a 20 20 2a 2a 20 20 20 29 3b  FSET 1.  **   );
224da 0a 20 20 2a 2f 0a 0a 20 20 70 53 65 6c 65 63 74  .  */..  pSelect
224db 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 45  Rowid = sqlite3E
224dc 78 70 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  xpr(pParse->db, 
224dd 54 4b 5f 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29  TK_ROW, 0, 0, 0)
224de 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 52  ;.  if( pSelectR
224df 6f 77 69 64 20 3d 3d 20 30 20 29 20 67 6f 74 6f  owid == 0 ) goto
224e0 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65   limit_where_cle
224e1 61 6e 75 70 5f 32 3b 0a 20 20 70 45 4c 69 73 74  anup_2;.  pEList
224e2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
224e3 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
224e4 20 30 2c 20 70 53 65 6c 65 63 74 52 6f 77 69 64   0, pSelectRowid
224e5 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 45 4c 69  , 0);.  if( pELi
224e6 73 74 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c  st == 0 ) goto l
224e7 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
224e8 75 70 5f 32 3b 0a 0a 20 20 2f 2a 20 64 75 70 6c  up_2;..  /* dupl
224e9 69 63 61 74 65 20 74 68 65 20 46 52 4f 4d 20 63  icate the FROM c
224ea 6c 61 75 73 65 20 61 73 20 69 74 20 69 73 20 6e  lause as it is n
224eb 65 65 64 65 64 20 62 79 20 62 6f 74 68 20 74 68  eeded by both th
224ec 65 20 44 45 4c 45 54 45 2f 55 50 44 41 54 45 20  e DELETE/UPDATE 
224ed 74 72 65 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68  tree.  ** and th
224ee 65 20 53 45 4c 45 43 54 20 73 75 62 74 72 65 65  e SELECT subtree
224ef 2e 20 2a 2f 0a 20 20 70 53 65 6c 65 63 74 53 72  . */.  pSelectSr
224f0 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  c = sqlite3SrcLi
224f1 73 74 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62  stDup(pParse->db
224f2 2c 20 70 53 72 63 29 3b 0a 20 20 69 66 28 20 70  , pSrc);.  if( p
224f3 53 65 6c 65 63 74 53 72 63 20 3d 3d 20 30 20 29  SelectSrc == 0 )
224f4 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78   {.    sqlite3Ex
224f5 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  prListDelete(pPa
224f6 72 73 65 2d 3e 64 62 2c 20 70 45 4c 69 73 74 29  rse->db, pEList)
224f7 3b 0a 20 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74  ;.    goto limit
224f8 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32  _where_cleanup_2
224f9 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65  ;.  }..  /* gene
224fa 72 61 74 65 20 74 68 65 20 53 45 4c 45 43 54 20  rate the SELECT 
224fb 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
224fc 20 2a 2f 0a 20 20 70 53 65 6c 65 63 74 20 3d 20   */.  pSelect = 
224fd 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
224fe 28 70 50 61 72 73 65 2c 70 45 4c 69 73 74 2c 70  (pParse,pEList,p
224ff 53 65 6c 65 63 74 53 72 63 2c 70 57 68 65 72 65  SelectSrc,pWhere
22500 2c 30 2c 30 2c 70 4f 72 64 65 72 42 79 2c 30 2c  ,0,0,pOrderBy,0,
22501 70 4c 69 6d 69 74 2c 70 4f 66 66 73 65 74 29 3b  pLimit,pOffset);
22502 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 20 3d  .  if( pSelect =
22503 3d 20 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  = 0 ) return 0;.
22504 0a 20 20 2f 2a 20 6e 6f 77 20 67 65 6e 65 72 61  .  /* now genera
22505 74 65 20 74 68 65 20 6e 65 77 20 57 48 45 52 45  te the new WHERE
22506 20 72 6f 77 69 64 20 49 4e 20 63 6c 61 75 73 65   rowid IN clause
22507 20 66 6f 72 20 74 68 65 20 44 45 4c 45 54 45 2f   for the DELETE/
22508 55 44 50 41 54 45 20 2a 2f 0a 20 20 70 57 68 65  UDPATE */.  pWhe
22509 72 65 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  reRowid = sqlite
2250a 33 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64 62  3Expr(pParse->db
2250b 2c 20 54 4b 5f 52 4f 57 2c 20 30 2c 20 30 2c 20  , TK_ROW, 0, 0, 
2250c 30 29 3b 0a 20 20 69 66 28 20 70 57 68 65 72 65  0);.  if( pWhere
2250d 52 6f 77 69 64 20 3d 3d 20 30 20 29 20 67 6f 74  Rowid == 0 ) got
2250e 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  o limit_where_cl
2250f 65 61 6e 75 70 5f 31 3b 0a 20 20 70 49 6e 43 6c  eanup_1;.  pInCl
22510 61 75 73 65 20 3d 20 73 71 6c 69 74 65 33 50 45  ause = sqlite3PE
22511 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
22512 4e 2c 20 70 57 68 65 72 65 52 6f 77 69 64 2c 20  N, pWhereRowid, 
22513 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 49 6e  0, 0);.  if( pIn
22514 43 6c 61 75 73 65 20 3d 3d 20 30 20 29 20 67 6f  Clause == 0 ) go
22515 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63  to limit_where_c
22516 6c 65 61 6e 75 70 5f 31 3b 0a 0a 20 20 70 49 6e  leanup_1;..  pIn
22517 43 6c 61 75 73 65 2d 3e 70 53 65 6c 65 63 74 20  Clause->pSelect 
22518 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 73 71 6c  = pSelect;.  sql
22519 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
2251a 74 28 70 50 61 72 73 65 2c 20 70 49 6e 43 6c 61  t(pParse, pInCla
2251b 75 73 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  use);.  return p
2251c 49 6e 43 6c 61 75 73 65 3b 0a 0a 20 20 2f 2a 20  InClause;..  /* 
2251d 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 77  something went w
2251e 72 6f 6e 67 2e 20 63 6c 65 61 6e 20 75 70 20 61  rong. clean up a
2251f 6e 79 74 68 69 6e 67 20 61 6c 6c 6f 63 61 74 65  nything allocate
22520 64 2e 20 2a 2f 0a 6c 69 6d 69 74 5f 77 68 65 72  d. */.limit_wher
22521 65 5f 63 6c 65 61 6e 75 70 5f 31 3a 0a 20 20 73  e_cleanup_1:.  s
22522 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
22523 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
22524 53 65 6c 65 63 74 29 3b 0a 20 20 72 65 74 75 72  Select);.  retur
22525 6e 20 30 3b 0a 0a 6c 69 6d 69 74 5f 77 68 65 72  n 0;..limit_wher
22526 65 5f 63 6c 65 61 6e 75 70 5f 32 3a 0a 20 20 73  e_cleanup_2:.  s
22527 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
22528 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 57 68  (pParse->db, pWh
22529 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ere);.  sqlite3E
2252a 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
2252b 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72  arse->db, pOrder
2252c 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  By);.  sqlite3Ex
2252d 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  prDelete(pParse-
2252e 3e 64 62 2c 20 70 4c 69 6d 69 74 29 3b 0a 20 20  >db, pLimit);.  
2252f 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
22530 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f  e(pParse->db, pO
22531 66 66 73 65 74 29 3b 0a 20 20 72 65 74 75 72 6e  ffset);.  return
22532 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
22533 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
22534 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c  NABLE_UPDATE_DEL
22535 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64  ETE_LIMIT) && !d
22536 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
22537 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0a  IT_SUBQUERY) */.
22538 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
22539 63 6f 64 65 20 66 6f 72 20 61 20 44 45 4c 45 54  code for a DELET
2253a 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74  E FROM statement
2253b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45  ..**.**     DELE
2253c 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78  TE FROM table_wx
2253d 79 7a 20 57 48 45 52 45 20 61 3c 35 20 41 4e 44  yz WHERE a<5 AND
2253e 20 62 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20   b NOT NULL;.** 
2253f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22540 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20  \________/      
22541 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f   \______________
22542 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  __/.**          
22543 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74          pTabList
22544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
22545 68 65 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  here.*/.SQLITE_P
22546 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
22547 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 0a 20  te3DeleteFrom(. 
22548 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
22549 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2254a 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2254b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
2254c 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65  List,     /* The
2254d 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63   table from whic
2254e 68 20 77 65 20 73 68 6f 75 6c 64 20 64 65 6c 65  h we should dele
2254f 74 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45  te things */.  E
22550 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20  xpr *pWhere     
22551 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
22552 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  RE clause.  May 
22553 62 65 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20  be null */.){.  
22554 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
22555 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
22556 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
22557 6e 67 69 6e 65 20 2a 2f 0a 20 20 54 61 62 6c 65  ngine */.  Table
22558 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
22559 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66    /* The table f
2255a 72 6f 6d 20 77 68 69 63 68 20 72 65 63 6f 72 64  rom which record
2255b 73 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65  s will be delete
2255c 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
2255d 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a  r *zDb;       /*
2255e 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
2255f 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
22560 2f 0a 20 20 69 6e 74 20 65 6e 64 2c 20 61 64 64  /.  int end, add
22561 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41 20  r = 0;     /* A 
22562 63 6f 75 70 6c 65 20 61 64 64 72 65 73 73 65 73  couple addresses
22563 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20 63 6f   of generated co
22564 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  de */.  int i;  
22565 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22566 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
22567 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70  /.  WhereInfo *p
22568 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49 6e  WInfo;     /* In
22569 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
2256a 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2256b 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
2256c 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x;           /* 
2256d 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
2256e 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68 65 20   indices of the 
2256f 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
22570 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Cur;            
22571 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72    /* VDBE Cursor
22572 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61 62   number for pTab
22573 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
22574 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
22575 4d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  Main database st
22576 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 41 75 74  ructure */.  Aut
22577 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78  hContext sContex
22578 74 3b 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61  t;  /* Authoriza
22579 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  tion context */.
2257a 20 20 69 6e 74 20 6f 6c 64 49 64 78 20 3d 20 2d    int oldIdx = -
2257b 31 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73  1;       /* Curs
2257c 6f 72 20 66 6f 72 20 74 68 65 20 4f 4c 44 20 74  or for the OLD t
2257d 61 62 6c 65 20 6f 66 20 41 46 54 45 52 20 74 72  able of AFTER tr
2257e 69 67 67 65 72 73 20 2a 2f 0a 20 20 4e 61 6d 65  iggers */.  Name
2257f 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
22580 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65     /* Name conte
22581 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78  xt to resolve ex
22582 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a  pressions in */.
22583 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
22584 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
22585 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  base number */. 
22586 20 69 6e 74 20 6d 65 6d 43 6e 74 20 3d 20 2d 31   int memCnt = -1
22587 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72  ;       /* Memor
22588 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20  y cell used for 
22589 63 68 61 6e 67 65 20 63 6f 75 6e 74 69 6e 67 20  change counting 
2258a 2a 2f 0a 20 20 69 6e 74 20 72 63 61 75 74 68 3b  */.  int rcauth;
2258b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
2258c 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
2258d 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
2258e 61 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 23 69 66 6e  allback */..#ifn
2258f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22590 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73  TRIGGER.  int is
22591 56 69 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  View;           
22592 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
22593 66 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  f attempting to 
22594 64 65 6c 65 74 65 20 66 72 6f 6d 20 61 20 76 69  delete from a vi
22595 65 77 20 2a 2f 0a 20 20 69 6e 74 20 74 72 69 67  ew */.  int trig
22596 67 65 72 73 5f 65 78 69 73 74 20 3d 20 30 3b 20  gers_exist = 0; 
22597 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
22598 61 6e 79 20 74 72 69 67 67 65 72 73 20 65 78 69  any triggers exi
22599 73 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69  st */.#endif.  i
2259a 6e 74 20 69 42 65 67 69 6e 41 66 74 65 72 54 72  nt iBeginAfterTr
2259b 69 67 67 65 72 20 3d 20 30 3b 20 20 2f 2a 20 41  igger = 0;  /* A
2259c 64 64 72 65 73 73 20 6f 66 20 61 66 74 65 72 20  ddress of after 
2259d 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20  trigger program 
2259e 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 41 66 74  */.  int iEndAft
2259f 65 72 54 72 69 67 67 65 72 20 3d 20 30 3b 20 20  erTrigger = 0;  
225a0 20 20 2f 2a 20 45 78 69 74 20 6f 66 20 61 66 74    /* Exit of aft
225a1 65 72 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  er trigger progr
225a2 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65 67  am */.  int iBeg
225a3 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 20  inBeforeTrigger 
225a4 3d 20 30 3b 20 2f 2a 20 41 64 64 72 65 73 73 20  = 0; /* Address 
225a5 6f 66 20 62 65 66 6f 72 65 20 74 72 69 67 67 65  of before trigge
225a6 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69  r program */.  i
225a7 6e 74 20 69 45 6e 64 42 65 66 6f 72 65 54 72 69  nt iEndBeforeTri
225a8 67 67 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 45  gger = 0;   /* E
225a9 78 69 74 20 6f 66 20 62 65 66 6f 72 65 20 74 72  xit of before tr
225aa 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f  igger program */
225ab 0a 20 20 75 33 32 20 6f 6c 64 5f 63 6f 6c 5f 6d  .  u32 old_col_m
225ac 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ask = 0;        
225ad 2f 2a 20 4d 61 73 6b 20 6f 66 20 4f 4c 44 2e 2a  /* Mask of OLD.*
225ae 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 75 73 65 20   columns in use 
225af 2a 2f 0a 0a 20 20 73 43 6f 6e 74 65 78 74 2e 70  */..  sContext.p
225b0 50 61 72 73 65 20 3d 20 30 3b 0a 20 20 64 62 20  Parse = 0;.  db 
225b1 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
225b2 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
225b3 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
225b4 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
225b5 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
225b6 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65  anup;.  }.  asse
225b7 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  rt( pTabList->nS
225b8 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c  rc==1 );..  /* L
225b9 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20  ocate the table 
225ba 77 68 69 63 68 20 77 65 20 77 61 6e 74 20 74 6f  which we want to
225bb 20 64 65 6c 65 74 65 2e 20 20 54 68 69 73 20 74   delete.  This t
225bc 61 62 6c 65 20 68 61 73 20 74 6f 20 62 65 0a 20  able has to be. 
225bd 20 2a 2a 20 70 75 74 20 69 6e 20 61 6e 20 53 72   ** put in an Sr
225be 63 4c 69 73 74 20 73 74 72 75 63 74 75 72 65 20  cList structure 
225bf 62 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 66 20  because some of 
225c0 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 73 20  the subroutines 
225c1 77 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20  we.  ** will be 
225c2 63 61 6c 6c 69 6e 67 20 61 72 65 20 64 65 73 69  calling are desi
225c3 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20 77 69 74  gned to work wit
225c4 68 20 6d 75 6c 74 69 70 6c 65 20 74 61 62 6c 65  h multiple table
225c5 73 20 61 6e 64 20 65 78 70 65 63 74 0a 20 20 2a  s and expect.  *
225c6 2a 20 61 6e 20 53 72 63 4c 69 73 74 2a 20 70 61  * an SrcList* pa
225c7 72 61 6d 65 74 65 72 20 69 6e 73 74 65 61 64 20  rameter instead 
225c8 6f 66 20 6a 75 73 74 20 61 20 54 61 62 6c 65 2a  of just a Table*
225c9 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2f   parameter..  */
225ca 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
225cb 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70  3SrcListLookup(p
225cc 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29  Parse, pTabList)
225cd 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
225ce 29 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66  )  goto delete_f
225cf 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 0a 20 20  rom_cleanup;..  
225d0 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66  /* Figure out if
225d1 20 77 65 20 68 61 76 65 20 61 6e 79 20 74 72 69   we have any tri
225d2 67 67 65 72 73 20 61 6e 64 20 69 66 20 74 68 65  ggers and if the
225d3 20 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a   table being.  *
225d4 2a 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 69  * deleted from i
225d5 73 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69  s a view.  */.#i
225d6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
225d7 54 5f 54 52 49 47 47 45 52 0a 20 20 74 72 69 67  T_TRIGGER.  trig
225d8 67 65 72 73 5f 65 78 69 73 74 20 3d 20 73 71 6c  gers_exist = sql
225d9 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
225da 74 28 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54  t(pTab, TK_DELET
225db 45 2c 20 30 29 3b 0a 20 20 69 73 56 69 65 77 20  E, 0);.  isView 
225dc 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 21  = pTab->pSelect!
225dd 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  =0;.#else.# defi
225de 6e 65 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  ne triggers_exis
225df 74 20 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56  t 0.# define isV
225e0 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23 69 66  iew 0.#endif.#if
225e1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
225e2 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56  VIEW.# undef isV
225e3 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56  iew.# define isV
225e4 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20  iew 0.#endif..  
225e5 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 65 61  if( sqlite3IsRea
225e6 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54  dOnly(pParse, pT
225e7 61 62 2c 20 74 72 69 67 67 65 72 73 5f 65 78 69  ab, triggers_exi
225e8 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  st) ){.    goto 
225e9 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61  delete_from_clea
225ea 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d  nup;.  }.  iDb =
225eb 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
225ec 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
225ed 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65  pSchema);.  asse
225ee 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20  rt( iDb<db->nDb 
225ef 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61  );.  zDb = db->a
225f0 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
225f1 20 72 63 61 75 74 68 20 3d 20 73 71 6c 69 74 65   rcauth = sqlite
225f2 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
225f3 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  e, SQLITE_DELETE
225f4 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30  , pTab->zName, 0
225f5 2c 20 7a 44 62 29 3b 0a 20 20 61 73 73 65 72 74  , zDb);.  assert
225f6 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45  ( rcauth==SQLITE
225f7 5f 4f 4b 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53  _OK || rcauth==S
225f8 51 4c 49 54 45 5f 44 45 4e 59 20 7c 7c 20 72 63  QLITE_DENY || rc
225f9 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 49 47 4e  auth==SQLITE_IGN
225fa 4f 52 45 20 29 3b 0a 20 20 69 66 28 20 72 63 61  ORE );.  if( rca
225fb 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59  uth==SQLITE_DENY
225fc 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
225fd 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
225fe 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 21  ;.  }.  assert(!
225ff 69 73 56 69 65 77 20 7c 7c 20 74 72 69 67 67 65  isView || trigge
22600 72 73 5f 65 78 69 73 74 29 3b 0a 0a 20 20 2f 2a  rs_exist);..  /*
22601 20 49 66 20 70 54 61 62 20 69 73 20 72 65 61 6c   If pTab is real
22602 6c 79 20 61 20 76 69 65 77 2c 20 6d 61 6b 65 20  ly a view, make 
22603 73 75 72 65 20 69 74 20 68 61 73 20 62 65 65 6e  sure it has been
22604 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20   initialized..  
22605 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
22606 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
22607 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  es(pParse, pTab)
22608 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
22609 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
2260a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  ;.  }..  /* Allo
2260b 63 61 74 65 20 61 20 63 75 72 73 6f 72 20 75 73  cate a cursor us
2260c 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ed to store the 
2260d 6f 6c 64 2e 2a 20 64 61 74 61 20 66 6f 72 20 61  old.* data for a
2260e 20 74 72 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20   trigger..  */. 
2260f 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78   if( triggers_ex
22610 69 73 74 20 29 7b 20 0a 20 20 20 20 6f 6c 64 49  ist ){ .    oldI
22611 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  dx = pParse->nTa
22612 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  b++;.  }..  /* A
22613 73 73 69 67 6e 20 20 63 75 72 73 6f 72 20 6e 75  ssign  cursor nu
22614 6d 62 65 72 20 74 6f 20 74 68 65 20 74 61 62 6c  mber to the tabl
22615 65 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 69 6e  e and all its in
22616 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73  dices..  */.  as
22617 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e  sert( pTabList->
22618 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69 43 75  nSrc==1 );.  iCu
22619 72 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  r = pTabList->a[
2261a 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 70 50 61  0].iCursor = pPa
2261b 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66  rse->nTab++;.  f
2261c 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
2261d 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
2261e 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
2261f 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b     pParse->nTab+
22620 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 61  +;.  }..  /* Sta
22621 72 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e 74  rt the view cont
22622 65 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  ext.  */.  if( i
22623 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c  sView ){.    sql
22624 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
22625 75 73 68 28 70 50 61 72 73 65 2c 20 26 73 43 6f  ush(pParse, &sCo
22626 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e 61  ntext, pTab->zNa
22627 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42  me);.  }..  /* B
22628 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20  egin generating 
22629 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d  code..  */.  v =
2262a 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2262b 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
2262c 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
2262d 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61  delete_from_clea
2262e 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  nup;.  }.  if( p
2262f 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
22630 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f   ) sqlite3VdbeCo
22631 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20  untChanges(v);. 
22632 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
22633 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
22634 73 65 2c 20 74 72 69 67 67 65 72 73 5f 65 78 69  se, triggers_exi
22635 73 74 2c 20 69 44 62 29 3b 0a 0a 20 20 69 66 28  st, iDb);..  if(
22636 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20   triggers_exist 
22637 29 7b 0a 20 20 20 20 69 6e 74 20 6f 72 63 6f 6e  ){.    int orcon
22638 66 20 3d 20 28 28 70 50 61 72 73 65 2d 3e 74 72  f = ((pParse->tr
22639 69 67 53 74 61 63 6b 29 3f 70 50 61 72 73 65 2d  igStack)?pParse-
2263a 3e 74 72 69 67 53 74 61 63 6b 2d 3e 6f 72 63 6f  >trigStack->orco
2263b 6e 66 3a 4f 45 5f 44 65 66 61 75 6c 74 29 3b 0a  nf:OE_Default);.
2263c 20 20 20 20 69 6e 74 20 69 47 6f 74 6f 20 3d 20      int iGoto = 
2263d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2263e 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20  0(v, OP_Goto);. 
2263f 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
22640 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
22641 29 3b 0a 0a 20 20 20 20 69 42 65 67 69 6e 42 65  );..    iBeginBe
22642 66 6f 72 65 54 72 69 67 67 65 72 20 3d 20 73 71  foreTrigger = sq
22643 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
22644 41 64 64 72 28 76 29 3b 0a 20 20 20 20 28 76 6f  Addr(v);.    (vo
22645 69 64 29 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  id)sqlite3CodeRo
22646 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  wTrigger(pParse,
22647 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 54   TK_DELETE, 0, T
22648 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 70  RIGGER_BEFORE, p
22649 54 61 62 2c 0a 20 20 20 20 20 20 20 20 2d 31 2c  Tab,.        -1,
2264a 20 6f 6c 64 49 64 78 2c 20 6f 72 63 6f 6e 66 2c   oldIdx, orconf,
2264b 20 61 64 64 72 2c 20 26 6f 6c 64 5f 63 6f 6c 5f   addr, &old_col_
2264c 6d 61 73 6b 2c 20 30 29 3b 0a 20 20 20 20 69 45  mask, 0);.    iE
2264d 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 20  ndBeforeTrigger 
2264e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2264f 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b  Op0(v, OP_Goto);
22650 0a 0a 20 20 20 20 69 42 65 67 69 6e 41 66 74 65  ..    iBeginAfte
22651 72 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  rTrigger = sqlit
22652 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
22653 72 28 76 29 3b 0a 20 20 20 20 28 76 6f 69 64 29  r(v);.    (void)
22654 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
22655 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 54 4b  igger(pParse, TK
22656 5f 44 45 4c 45 54 45 2c 20 30 2c 20 54 52 49 47  _DELETE, 0, TRIG
22657 47 45 52 5f 41 46 54 45 52 2c 20 70 54 61 62 2c  GER_AFTER, pTab,
22658 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 6f 6c 64   -1,.        old
22659 49 64 78 2c 20 6f 72 63 6f 6e 66 2c 20 61 64 64  Idx, orconf, add
2265a 72 2c 20 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b  r, &old_col_mask
2265b 2c 20 30 29 3b 0a 20 20 20 20 69 45 6e 64 41 66  , 0);.    iEndAf
2265c 74 65 72 54 72 69 67 67 65 72 20 3d 20 73 71 6c  terTrigger = sql
2265d 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
2265e 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 0a 20 20 20  , OP_Goto);..   
2265f 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
22660 48 65 72 65 28 76 2c 20 69 47 6f 74 6f 29 3b 0a  Here(v, iGoto);.
22661 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20    }..  /* If we 
22662 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 64 65  are trying to de
22663 6c 65 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77  lete from a view
22664 2c 20 72 65 61 6c 69 7a 65 20 74 68 61 74 20 76  , realize that v
22665 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 20  iew into.  ** a 
22666 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e  ephemeral table.
22667 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  .  */.#if !defin
22668 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
22669 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64  IEW) && !defined
2266a 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
2266b 47 47 45 52 29 0a 20 20 69 66 28 20 69 73 56 69  GGER).  if( isVi
2266c 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ew ){.    sqlite
2266d 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77  3MaterializeView
2266e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70  (pParse, pTab, p
2266f 57 68 65 72 65 2c 20 69 43 75 72 29 3b 0a 20 20  Where, iCur);.  
22670 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52  }.#endif..  /* R
22671 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d  esolve the colum
22672 6e 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57  n names in the W
22673 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a  HERE clause..  *
22674 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c  /.  memset(&sNC,
22675 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
22676 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d  ;.  sNC.pParse =
22677 20 70 50 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70   pParse;.  sNC.p
22678 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69  SrcList = pTabLi
22679 73 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  st;.  if( sqlite
2267a 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2267b 73 28 26 73 4e 43 2c 20 70 57 68 65 72 65 29 20  s(&sNC, pWhere) 
2267c 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65  ){.    goto dele
2267d 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b  te_from_cleanup;
2267e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69  .  }..  /* Initi
2267f 61 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 65  alize the counte
22680 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  r of the number 
22681 6f 66 20 72 6f 77 73 20 64 65 6c 65 74 65 64 2c  of rows deleted,
22682 20 69 66 0a 20 20 2a 2a 20 77 65 20 61 72 65 20   if.  ** we are 
22683 63 6f 75 6e 74 69 6e 67 20 72 6f 77 73 2e 0a 20  counting rows.. 
22684 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c   */.  if( db->fl
22685 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75  ags & SQLITE_Cou
22686 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 6d 65  ntRows ){.    me
22687 6d 43 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d  mCnt = ++pParse-
22688 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >nMem;.    sqlit
22689 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2268a 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d  OP_Integer, 0, m
2268b 65 6d 43 6e 74 29 3b 0a 20 20 7d 0a 0a 23 69 66  emCnt);.  }..#if
2268c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2268d 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d 49  _TRUNCATE_OPTIMI
2268e 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 53 70 65 63  ZATION.  /* Spec
2268f 69 61 6c 20 63 61 73 65 3a 20 41 20 44 45 4c 45  ial case: A DELE
22690 54 45 20 77 69 74 68 6f 75 74 20 61 20 57 48 45  TE without a WHE
22691 52 45 20 63 6c 61 75 73 65 20 64 65 6c 65 74 65  RE clause delete
22692 73 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20  s everything..  
22693 2a 2a 20 49 74 20 69 73 20 65 61 73 69 65 72 20  ** It is easier 
22694 6a 75 73 74 20 74 6f 20 65 72 61 73 65 20 74 68  just to erase th
22695 65 20 77 68 6f 6c 65 20 74 61 62 6c 65 2e 20 20  e whole table.  
22696 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74  Note, however, t
22697 68 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65  hat.  ** this me
22698 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 6f 77  ans that the row
22699 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 77 69   change count wi
2269a 6c 6c 20 62 65 20 69 6e 63 6f 72 72 65 63 74 2e  ll be incorrect.
2269b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 61 75  .  */.  if( rcau
2269c 74 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  th==SQLITE_OK &&
2269d 20 70 57 68 65 72 65 3d 3d 30 20 26 26 20 21 74   pWhere==0 && !t
2269e 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 26 26  riggers_exist &&
2269f 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62   !IsVirtual(pTab
226a0 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ) ){.    assert(
226a1 20 21 69 73 56 69 65 77 20 29 3b 0a 20 20 20 20   !isView );.    
226a2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
226a3 33 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70  3(v, OP_Clear, p
226a4 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 20  Tab->tnum, iDb, 
226a5 6d 65 6d 43 6e 74 29 3b 0a 20 20 20 20 69 66 28  memCnt);.    if(
226a6 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64   !pParse->nested
226a7 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
226a8 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
226a9 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65   -1, pTab->zName
226aa 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
226ab 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 49 64 78    }.    for(pIdx
226ac 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
226ad 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
226ae 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73  pNext){.      as
226af 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63 68  sert( pIdx->pSch
226b0 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65  ema==pTab->pSche
226b1 6d 61 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ma );.      sqli
226b2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
226b3 20 4f 50 5f 43 6c 65 61 72 2c 20 70 49 64 78 2d   OP_Clear, pIdx-
226b4 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20  >tnum, iDb);.   
226b5 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
226b6 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
226b7 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d 49  _TRUNCATE_OPTIMI
226b8 5a 41 54 49 4f 4e 20 2a 2f 0a 20 20 2f 2a 20 54  ZATION */.  /* T
226b9 68 65 20 75 73 75 61 6c 20 63 61 73 65 3a 20 54  he usual case: T
226ba 68 65 72 65 20 69 73 20 61 20 57 48 45 52 45 20  here is a WHERE 
226bb 63 6c 61 75 73 65 20 73 6f 20 77 65 20 68 61 76  clause so we hav
226bc 65 20 74 6f 20 73 63 61 6e 20 74 68 72 6f 75 67  e to scan throug
226bd 68 0a 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65  h.  ** the table
226be 20 61 6e 64 20 70 69 63 6b 20 77 68 69 63 68 20   and pick which 
226bf 72 65 63 6f 72 64 73 20 74 6f 20 64 65 6c 65 74  records to delet
226c0 65 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20  e..  */.  {.    
226c1 69 6e 74 20 69 52 6f 77 69 64 20 3d 20 2b 2b 70  int iRowid = ++p
226c2 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20  Parse->nMem;    
226c3 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 74 6f 72  /* Used for stor
226c4 69 6e 67 20 72 6f 77 69 64 20 76 61 6c 75 65 73  ing rowid values
226c5 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52 6f  . */.    int iRo
226c6 77 53 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d  wSet = ++pParse-
226c7 3e 6e 4d 65 6d 3b 20 20 20 2f 2a 20 52 65 67 69  >nMem;   /* Regi
226c8 73 74 65 72 20 66 6f 72 20 72 6f 77 73 65 74 20  ster for rowset 
226c9 6f 66 20 72 6f 77 73 20 74 6f 20 64 65 6c 65 74  of rows to delet
226ca 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 6f 6c  e */..    /* Col
226cb 6c 65 63 74 20 72 6f 77 69 64 73 20 6f 66 20 65  lect rowids of e
226cc 76 65 72 79 20 72 6f 77 20 74 6f 20 62 65 20 64  very row to be d
226cd 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  eleted..    */. 
226ce 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
226cf 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
226d0 20 30 2c 20 69 52 6f 77 53 65 74 29 3b 0a 20 20   0, iRowSet);.  
226d1 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74    pWInfo = sqlit
226d2 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61  e3WhereBegin(pPa
226d3 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
226d4 57 68 65 72 65 2c 20 30 2c 0a 20 20 20 20 20 20  Where, 0,.      
226d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226d6 20 20 20 20 20 20 20 20 20 57 48 45 52 45 5f 46           WHERE_F
226d7 49 4c 4c 5f 52 4f 57 53 45 54 2c 20 69 52 6f 77  ILL_ROWSET, iRow
226d8 53 65 74 29 3b 0a 20 20 20 20 69 66 28 20 70 57  Set);.    if( pW
226d9 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 64  Info==0 ) goto d
226da 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
226db 75 70 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  up;.    if( db->
226dc 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
226dd 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20  ountRows ){.    
226de 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
226df 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
226e0 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20  , memCnt, 1);.  
226e1 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 57    }.    sqlite3W
226e2 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b  hereEnd(pWInfo);
226e3 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68  ..    /* Open th
226e4 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 75  e pseudo-table u
226e5 73 65 64 20 74 6f 20 73 74 6f 72 65 20 4f 4c 44  sed to store OLD
226e6 20 69 66 20 74 68 65 72 65 20 61 72 65 20 74 72   if there are tr
226e7 69 67 67 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20  iggers..    */. 
226e8 20 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f     if( triggers_
226e9 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20 73  exist ){.      s
226ea 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
226eb 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c  (v, OP_SetNumCol
226ec 75 6d 6e 73 2c 20 30 2c 20 70 54 61 62 2d 3e 6e  umns, 0, pTab->n
226ed 43 6f 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Col);.      sqli
226ee 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
226ef 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20   OP_OpenPseudo, 
226f0 6f 6c 64 49 64 78 29 3b 0a 20 20 20 20 7d 0a 0a  oldIdx);.    }..
226f1 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 76      /* Delete ev
226f2 65 72 79 20 69 74 65 6d 20 77 68 6f 73 65 20 6b  ery item whose k
226f3 65 79 20 77 61 73 20 77 72 69 74 74 65 6e 20 74  ey was written t
226f4 6f 20 74 68 65 20 6c 69 73 74 20 64 75 72 69 6e  o the list durin
226f5 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74  g the.    ** dat
226f6 61 62 61 73 65 20 73 63 61 6e 2e 20 20 57 65 20  abase scan.  We 
226f7 68 61 76 65 20 74 6f 20 64 65 6c 65 74 65 20 69  have to delete i
226f8 74 65 6d 73 20 61 66 74 65 72 20 74 68 65 20 73  tems after the s
226f9 63 61 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 0a  can is complete.
226fa 20 20 20 20 2a 2a 20 62 65 63 61 75 73 65 20 64      ** because d
226fb 65 6c 65 74 69 6e 67 20 61 6e 20 69 74 65 6d 20  eleting an item 
226fc 63 61 6e 20 63 68 61 6e 67 65 20 74 68 65 20 73  can change the s
226fd 63 61 6e 20 6f 72 64 65 72 2e 0a 20 20 20 20 2a  can order..    *
226fe 2f 0a 20 20 20 20 65 6e 64 20 3d 20 73 71 6c 69  /.    end = sqli
226ff 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
22700 28 76 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 69  (v);..    if( !i
22701 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 2f  sView ){.      /
22702 2a 20 4f 70 65 6e 20 63 75 72 73 6f 72 73 20 66  * Open cursors f
22703 6f 72 20 74 68 65 20 74 61 62 6c 65 20 77 65 20  or the table we 
22704 61 72 65 20 64 65 6c 65 74 69 6e 67 20 66 72 6f  are deleting fro
22705 6d 20 61 6e 64 20 0a 20 20 20 20 20 20 2a 2a 20  m and .      ** 
22706 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e  all its indices.
22707 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
22708 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
22709 41 6e 64 49 6e 64 69 63 65 73 28 70 50 61 72 73  AndIndices(pPars
2270a 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 4f  e, pTab, iCur, O
2270b 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20  P_OpenWrite);.  
2270c 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73    }..    /* This
2270d 20 69 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   is the beginnin
2270e 67 20 6f 66 20 74 68 65 20 64 65 6c 65 74 65 20  g of the delete 
2270f 6c 6f 6f 70 2e 20 49 66 20 61 20 74 72 69 67 67  loop. If a trigg
22710 65 72 20 65 6e 63 6f 75 6e 74 65 72 73 0a 20 20  er encounters.  
22711 20 20 2a 2a 20 61 6e 20 49 47 4e 4f 52 45 20 63    ** an IGNORE c
22712 6f 6e 73 74 72 61 69 6e 74 2c 20 69 74 20 6a 75  onstraint, it ju
22713 6d 70 73 20 62 61 63 6b 20 74 6f 20 68 65 72 65  mps back to here
22714 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
22715 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20   triggers_exist 
22716 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
22717 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
22718 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 7d  (v, addr);.    }
22719 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
2271a 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2271b 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20   OP_RowSetRead, 
2271c 69 52 6f 77 53 65 74 2c 20 65 6e 64 2c 20 69 52  iRowSet, end, iR
2271d 6f 77 69 64 29 3b 0a 0a 20 20 20 20 69 66 28 20  owid);..    if( 
2271e 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29  triggers_exist )
2271f 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 61 74  {.      int iDat
22720 61 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  a = ++pParse->nM
22721 65 6d 3b 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f  em;   /* For sto
22722 72 69 6e 67 20 72 6f 77 20 64 61 74 61 20 6f 66  ring row data of
22723 20 4f 4c 44 20 74 61 62 6c 65 20 2a 2f 0a 0a 20   OLD table */.. 
22724 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72       /* If the r
22725 65 63 6f 72 64 20 69 73 20 6e 6f 20 6c 6f 6e 67  ecord is no long
22726 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68  er present in th
22727 65 20 74 61 62 6c 65 2c 20 6a 75 6d 70 20 74 6f  e table, jump to
22728 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6e 65   the.      ** ne
22729 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  xt iteration of 
2272a 74 68 65 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68  the loop through
2272b 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
2272c 20 74 68 65 20 66 69 66 6f 2e 0a 20 20 20 20 20   the fifo..     
2272d 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2272e 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2272f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75  P_NotExists, iCu
22730 72 2c 20 61 64 64 72 2c 20 69 52 6f 77 69 64 29  r, addr, iRowid)
22731 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 50 6f 70 75  ;..      /* Popu
22732 6c 61 74 65 20 74 68 65 20 4f 4c 44 2e 2a 20 70  late the OLD.* p
22733 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20  seudo-table */. 
22734 20 20 20 20 20 69 66 28 20 6f 6c 64 5f 63 6f 6c       if( old_col
22735 5f 6d 61 73 6b 20 29 7b 0a 20 20 20 20 20 20 20  _mask ){.       
22736 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22737 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61  p2(v, OP_RowData
22738 2c 20 69 43 75 72 2c 20 69 44 61 74 61 29 3b 0a  , iCur, iData);.
22739 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2273a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2273b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2273c 6c 2c 20 30 2c 20 69 44 61 74 61 29 3b 0a 20 20  l, 0, iData);.  
2273d 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2273e 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2273f 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6f 6c 64 49   OP_Insert, oldI
22740 64 78 2c 20 69 44 61 74 61 2c 20 69 52 6f 77 69  dx, iData, iRowi
22741 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4a 75  d);..      /* Ju
22742 6d 70 20 62 61 63 6b 20 61 6e 64 20 72 75 6e 20  mp back and run 
22743 74 68 65 20 42 45 46 4f 52 45 20 74 72 69 67 67  the BEFORE trigg
22744 65 72 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ers */.      sql
22745 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22746 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42  , OP_Goto, 0, iB
22747 65 67 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65  eginBeforeTrigge
22748 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r);.      sqlite
22749 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2274a 20 69 45 6e 64 42 65 66 6f 72 65 54 72 69 67 67   iEndBeforeTrigg
2274b 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  er);.    }..    
2274c 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20  if( !isView ){. 
2274d 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74       /* Delete t
2274e 68 65 20 72 6f 77 20 2a 2f 0a 23 69 66 6e 64 65  he row */.#ifnde
2274f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
22750 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20  RTUALTABLE.     
22751 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
22752 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Tab) ){.        
22753 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 56 74 61  const char *pVta
22754 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  b = (const char 
22755 2a 29 70 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20  *)pTab->pVtab;. 
22756 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74         sqlite3Vt
22757 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70  abMakeWritable(p
22758 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
22759 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2275a 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55  eAddOp4(v, OP_VU
2275b 70 64 61 74 65 2c 20 30 2c 20 31 2c 20 69 52 6f  pdate, 0, 1, iRo
2275c 77 69 64 2c 20 70 56 74 61 62 2c 20 50 34 5f 56  wid, pVtab, P4_V
2275d 54 41 42 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  TAB);.      }els
2275e 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b  e.#endif.      {
2275f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22760 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74  GenerateRowDelet
22761 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
22762 69 43 75 72 2c 20 69 52 6f 77 69 64 2c 20 70 50  iCur, iRowid, pP
22763 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29  arse->nested==0)
22764 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
22765 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
22766 20 61 72 65 20 72 6f 77 20 74 72 69 67 67 65 72   are row trigger
22767 73 2c 20 63 6c 6f 73 65 20 61 6c 6c 20 63 75 72  s, close all cur
22768 73 6f 72 73 20 74 68 65 6e 20 69 6e 76 6f 6b 65  sors then invoke
22769 0a 20 20 20 20 2a 2a 20 74 68 65 20 41 46 54 45  .    ** the AFTE
2276a 52 20 74 72 69 67 67 65 72 73 0a 20 20 20 20 2a  R triggers.    *
2276b 2f 0a 20 20 20 20 69 66 28 20 74 72 69 67 67 65  /.    if( trigge
2276c 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20  rs_exist ){.    
2276d 20 20 2f 2a 20 4a 75 6d 70 20 62 61 63 6b 20 61    /* Jump back a
2276e 6e 64 20 72 75 6e 20 74 68 65 20 41 46 54 45 52  nd run the AFTER
2276f 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20   triggers */.   
22770 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22771 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
22772 20 30 2c 20 69 42 65 67 69 6e 41 66 74 65 72 54   0, iBeginAfterT
22773 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20 73  rigger);.      s
22774 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
22775 72 65 28 76 2c 20 69 45 6e 64 41 66 74 65 72 54  re(v, iEndAfterT
22776 72 69 67 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a  rigger);.    }..
22777 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68      /* End of th
22778 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70 20 2a 2f  e delete loop */
22779 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2277a 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2277b 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20 20  o, 0, addr);.   
2277c 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2277d 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 29  lveLabel(v, end)
2277e 3b 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20  ;..    /* Close 
2277f 74 68 65 20 63 75 72 73 6f 72 73 20 61 66 74 65  the cursors afte
22780 72 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68  r the loop if th
22781 65 72 65 20 61 72 65 20 6e 6f 20 72 6f 77 20 74  ere are no row t
22782 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 69  riggers */.    i
22783 66 28 20 21 69 73 56 69 65 77 20 20 26 26 20 21  f( !isView  && !
22784 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
22785 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31  ){.      for(i=1
22786 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
22787 64 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20  dex; pIdx; i++, 
22788 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
22789 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2278a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2278b 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 20 2b  OP_Close, iCur +
2278c 20 69 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 29 3b   i, pIdx->tnum);
2278d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
2278e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2278f 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43  (v, OP_Close, iC
22790 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ur);.    }.  }..
22791 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    /*.  ** Return
22792 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
22793 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 64 65  ows that were de
22794 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 72  leted. If this r
22795 6f 75 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20  outine is .  ** 
22796 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20  generating code 
22797 62 65 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c  because of a cal
22798 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74  l to sqlite3Nest
22799 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f  edParse(), do no
2279a 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  t.  ** invoke th
2279b 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
2279c 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ion..  */.  if( 
2279d 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
2279e 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20  TE_CountRows && 
2279f 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
227a0 30 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72  0 && !pParse->tr
227a1 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 73  igStack ){.    s
227a2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
227a3 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
227a4 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20  , memCnt, 1);.  
227a5 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
227a6 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
227a7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
227a8 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
227a9 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f  OLNAME_NAME, "ro
227aa 77 73 20 64 65 6c 65 74 65 64 22 2c 20 53 51 4c  ws deleted", SQL
227ab 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d  ITE_STATIC);.  }
227ac 0a 0a 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c  ..delete_from_cl
227ad 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33  eanup:.  sqlite3
227ae 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26  AuthContextPop(&
227af 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c  sContext);.  sql
227b0 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
227b1 65 28 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b  e(db, pTabList);
227b2 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
227b3 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72 65 29  lete(db, pWhere)
227b4 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;.  return;.}../
227b5 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
227b6 65 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45  e generates VDBE
227b7 20 63 6f 64 65 20 74 68 61 74 20 63 61 75 73 65   code that cause
227b8 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f  s a single row o
227b9 66 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 61  f a.** single ta
227ba 62 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65  ble to be delete
227bb 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42  d..**.** The VDB
227bc 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61 20 70  E must be in a p
227bd 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 65 20  articular state 
227be 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
227bf 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20  e is called..** 
227c0 54 68 65 73 65 20 61 72 65 20 74 68 65 20 72 65  These are the re
227c1 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  quirements:.**.*
227c2 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64 2f 77  *   1.  A read/w
227c3 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e  rite cursor poin
227c4 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74 68  ting to pTab, th
227c5 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  e table containi
227c6 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20  ng the row.**   
227c7 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65 74 65      to be delete
227c8 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65  d, must be opene
227c9 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75 6d 62  d as cursor numb
227ca 65 72 20 22 62 61 73 65 22 2e 0a 2a 2a 0a 2a 2a  er "base"..**.**
227cb 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69 74     2.  Read/writ
227cc 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61 6c  e cursors for al
227cd 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61  l indices of pTa
227ce 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61  b must be open a
227cf 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73 6f  s.**       curso
227d0 72 20 6e 75 6d 62 65 72 20 62 61 73 65 2b 69 20  r number base+i 
227d1 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e 64  for the i-th ind
227d2 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20  ex..**.**   3.  
227d3 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  The record numbe
227d4 72 20 6f 66 20 74 68 65 20 72 6f 77 20 74 6f 20  r of the row to 
227d5 62 65 20 64 65 6c 65 74 65 64 20 6d 75 73 74 20  be deleted must 
227d6 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20  be stored in.** 
227d7 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 63 65 6c        memory cel
227d8 6c 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20  l iRowid..**.** 
227d9 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 6f 70  This routine pop
227da 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  s the top of the
227db 20 73 74 61 63 6b 20 74 6f 20 72 65 6d 6f 76 65   stack to remove
227dc 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
227dd 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 67  er.** and then g
227de 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f  enerates code to
227df 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74 68 65   remove both the
227e0 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 61 6e   table record an
227e1 64 20 61 6c 6c 20 69 6e 64 65 78 0a 2a 2a 20 65  d all index.** e
227e2 6e 74 72 69 65 73 20 74 68 61 74 20 70 6f 69 6e  ntries that poin
227e3 74 20 74 6f 20 74 68 61 74 20 72 65 63 6f 72 64  t to that record
227e4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
227e5 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
227e6 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74  GenerateRowDelet
227e7 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
227e8 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
227e9 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
227ea 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
227eb 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74     /* Table cont
227ec 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74  aining the row t
227ed 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a  o be deleted */.
227ee 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20    int iCur,     
227ef 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
227f0 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61  umber for the ta
227f1 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f  ble */.  int iRo
227f2 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 4d  wid,        /* M
227f3 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20  emory cell that 
227f4 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 77  contains the row
227f5 69 64 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a  id to delete */.
227f6 20 20 69 6e 74 20 63 6f 75 6e 74 20 20 20 20 20    int count     
227f7 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e       /* Incremen
227f8 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65  t the row change
227f9 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 29 7b 0a 20   counter */.){. 
227fa 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 56 64 62   int addr;.  Vdb
227fb 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20 70 50 61  e *v;..  v = pPa
227fc 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 64  rse->pVdbe;.  ad
227fd 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
227fe 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp3(v, OP_Not
227ff 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 30 2c  Exists, iCur, 0,
22800 20 69 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69   iRowid);.  sqli
22801 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e  te3GenerateRowIn
22802 64 65 78 44 65 6c 65 74 65 28 70 50 61 72 73 65  dexDelete(pParse
22803 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 30 29  , pTab, iCur, 0)
22804 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
22805 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c 65  ddOp2(v, OP_Dele
22806 74 65 2c 20 69 43 75 72 2c 20 28 63 6f 75 6e 74  te, iCur, (count
22807 3f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 3a  ?OPFLAG_NCHANGE:
22808 30 29 29 3b 0a 20 20 69 66 28 20 63 6f 75 6e 74  0));.  if( count
22809 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2280a 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
2280b 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  1, pTab->zName, 
2280c 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  P4_STATIC);.  }.
2280d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2280e 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
2280f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
22810 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20  utine generates 
22811 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 63  VDBE code that c
22812 61 75 73 65 73 20 74 68 65 20 64 65 6c 65 74 69  auses the deleti
22813 6f 6e 20 6f 66 20 61 6c 6c 0a 2a 2a 20 69 6e 64  on of all.** ind
22814 65 78 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63  ex entries assoc
22815 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e  iated with a sin
22816 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73 69 6e  gle row of a sin
22817 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  gle table..**.**
22818 20 54 68 65 20 56 44 42 45 20 6d 75 73 74 20 62   The VDBE must b
22819 65 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61  e in a particula
2281a 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69  r state when thi
2281b 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
2281c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61 72  led..** These ar
2281d 65 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e  e the requiremen
2281e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20  ts:.**.**   1.  
2281f 41 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72  A read/write cur
22820 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  sor pointing to 
22821 70 54 61 62 2c 20 74 68 65 20 74 61 62 6c 65 20  pTab, the table 
22822 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
22823 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20 62  ow.**       to b
22824 65 20 64 65 6c 65 74 65 64 2c 20 6d 75 73 74 20  e deleted, must 
22825 62 65 20 6f 70 65 6e 65 64 20 61 73 20 63 75 72  be opened as cur
22826 73 6f 72 20 6e 75 6d 62 65 72 20 22 69 43 75 72  sor number "iCur
22827 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 52  "..**.**   2.  R
22828 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
22829 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65  s for all indice
2282a 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74 20 62  s of pTab must b
2282b 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20 20 20 20  e open as.**    
2282c 20 20 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72     cursor number
2282d 20 69 43 75 72 2b 69 20 66 6f 72 20 74 68 65 20   iCur+i for the 
2282e 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  i-th index..**.*
2282f 2a 20 20 20 33 2e 20 20 54 68 65 20 22 69 43 75  *   3.  The "iCu
22830 72 22 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62  r" cursor must b
22831 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  e pointing to th
22832 65 20 72 6f 77 20 74 68 61 74 20 69 73 20 74 6f  e row that is to
22833 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 64 65 6c   be.**       del
22834 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eted..*/.SQLITE_
22835 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
22836 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49  ite3GenerateRowI
22837 6e 64 65 78 44 65 6c 65 74 65 28 0a 20 20 50 61  ndexDelete(.  Pa
22838 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
22839 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20   /* Parsing and 
2283a 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20  code generating 
2283b 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
2283c 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
2283d 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e  /* Table contain
2283e 69 6e 67 20 74 68 65 20 72 6f 77 20 74 6f 20 62  ing the row to b
2283f 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69  e deleted */.  i
22840 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20  nt iCur,        
22841 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
22842 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  er for the table
22843 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49   */.  int *aRegI
22844 64 78 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79  dx       /* Only
22845 20 64 65 6c 65 74 65 20 69 66 20 61 52 65 67 49   delete if aRegI
22846 64 78 21 3d 30 20 26 26 20 61 52 65 67 49 64 78  dx!=0 && aRegIdx
22847 5b 69 5d 3e 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e  [i]>0 */.){.  in
22848 74 20 69 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49  t i;.  Index *pI
22849 64 78 3b 0a 20 20 69 6e 74 20 72 31 3b 0a 0a 20  dx;.  int r1;.. 
2284a 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d 70   for(i=1, pIdx=p
2284b 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
2284c 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49 64  x; i++, pIdx=pId
2284d 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  x->pNext){.    i
2284e 66 28 20 61 52 65 67 49 64 78 21 3d 30 20 26 26  f( aRegIdx!=0 &&
2284f 20 61 52 65 67 49 64 78 5b 69 2d 31 5d 3d 3d 30   aRegIdx[i-1]==0
22850 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
22851 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 6e   r1 = sqlite3Gen
22852 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50  erateIndexKey(pP
22853 61 72 73 65 2c 20 70 49 64 78 2c 20 69 43 75 72  arse, pIdx, iCur
22854 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
22855 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70  ite3VdbeAddOp3(p
22856 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50  Parse->pVdbe, OP
22857 5f 49 64 78 44 65 6c 65 74 65 2c 20 69 43 75 72  _IdxDelete, iCur
22858 2b 69 2c 20 72 31 2c 70 49 64 78 2d 3e 6e 43 6f  +i, r1,pIdx->nCo
22859 6c 75 6d 6e 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  lumn+1);.  }.}..
2285a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2285b 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 61 73  ode that will as
2285c 73 65 6d 62 6c 65 20 61 6e 20 69 6e 64 65 78 20  semble an index 
2285d 6b 65 79 20 61 6e 64 20 70 75 74 20 69 74 20 69  key and put it i
2285e 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 72 65  n register.** re
2285f 67 4f 75 74 2e 20 20 54 68 65 20 6b 65 79 20 77  gOut.  The key w
22860 69 74 68 20 62 65 20 66 6f 72 20 69 6e 64 65 78  ith be for index
22861 20 70 49 64 78 20 77 68 69 63 68 20 69 73 20 61   pIdx which is a
22862 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e  n index on pTab.
22863 0a 2a 2a 20 69 43 75 72 20 69 73 20 74 68 65 20  .** iCur is the 
22864 69 6e 64 65 78 20 6f 66 20 61 20 63 75 72 73 6f  index of a curso
22865 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 70 54  r open on the pT
22866 61 62 20 74 61 62 6c 65 20 61 6e 64 20 70 6f 69  ab table and poi
22867 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 74 68 65 20  nting to.** the 
22868 65 6e 74 72 79 20 74 68 61 74 20 6e 65 65 64 73  entry that needs
22869 20 69 6e 64 65 78 69 6e 67 2e 0a 2a 2a 0a 2a 2a   indexing..**.**
2286a 20 52 65 74 75 72 6e 20 61 20 72 65 67 69 73 74   Return a regist
2286b 65 72 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20  er number which 
2286c 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20  is the first in 
2286d 61 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 72 65  a block of.** re
2286e 67 69 73 74 65 72 73 20 74 68 61 74 20 68 6f 6c  gisters that hol
2286f 64 73 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20  ds the elements 
22870 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79  of the index key
22871 2e 20 20 54 68 65 0a 2a 2a 20 62 6c 6f 63 6b 20  .  The.** block 
22872 6f 66 20 72 65 67 69 73 74 65 72 73 20 68 61 73  of registers has
22873 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64 65   already been de
22874 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65  allocated by the
22875 20 74 69 6d 65 0a 2a 2a 20 74 68 69 73 20 72 6f   time.** this ro
22876 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a  utine returns..*
22877 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22878 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e 65   int sqlite3Gene
22879 72 61 74 65 49 6e 64 65 78 4b 65 79 28 0a 20 20  rateIndexKey(.  
2287a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2287b 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2287c 6e 74 65 78 74 20 2a 2f 0a 20 20 49 6e 64 65 78  ntext */.  Index
2287d 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 2f 2a   *pIdx,       /*
2287e 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20 77   The index for w
2287f 68 69 63 68 20 74 6f 20 67 65 6e 65 72 61 74 65  hich to generate
22880 20 61 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20   a key */.  int 
22881 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f  iCur,          /
22882 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
22883 66 6f 72 20 74 68 65 20 70 49 64 78 2d 3e 70 54  for the pIdx->pT
22884 61 62 6c 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  able table */.  
22885 69 6e 74 20 72 65 67 4f 75 74 2c 20 20 20 20 20  int regOut,     
22886 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
22887 6e 65 77 20 69 6e 64 65 78 20 6b 65 79 20 74 6f  new index key to
22888 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 2a   this register *
22889 2f 0a 20 20 69 6e 74 20 64 6f 4d 61 6b 65 52 65  /.  int doMakeRe
2288a 63 20 20 20 20 20 20 2f 2a 20 52 75 6e 20 74 68  c      /* Run th
2288b 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20  e OP_MakeRecord 
2288c 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 66 20 74  instruction if t
2288d 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  rue */.){.  Vdbe
2288e 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2288f 64 62 65 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20  dbe;.  int j;.  
22890 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49  Table *pTab = pI
22891 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 69 6e  dx->pTable;.  in
22892 74 20 72 65 67 42 61 73 65 3b 0a 20 20 69 6e 74  t regBase;.  int
22893 20 6e 43 6f 6c 3b 0a 0a 20 20 6e 43 6f 6c 20 3d   nCol;..  nCol =
22894 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a   pIdx->nColumn;.
22895 20 20 72 65 67 42 61 73 65 20 3d 20 73 71 6c 69    regBase = sqli
22896 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
22897 70 50 61 72 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b  pParse, nCol+1);
22898 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
22899 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64  dOp2(v, OP_Rowid
2289a 2c 20 69 43 75 72 2c 20 72 65 67 42 61 73 65 2b  , iCur, regBase+
2289b 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28 6a 3d 30  nCol);.  for(j=0
2289c 3b 20 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a  ; j<nCol; j++){.
2289d 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 49      int idx = pI
2289e 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b  dx->aiColumn[j];
2289f 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d 70 54  .    if( idx==pT
228a0 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
228a1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
228a2 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79  dOp2(v, OP_SCopy
228a3 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f 6c 2c 20  , regBase+nCol, 
228a4 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20  regBase+j);.    
228a5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
228a6 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
228a7 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75  , OP_Column, iCu
228a8 72 2c 20 69 64 78 2c 20 72 65 67 42 61 73 65 2b  r, idx, regBase+
228a9 6a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  j);.      sqlite
228aa 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76  3ColumnDefault(v
228ab 2c 20 70 54 61 62 2c 20 69 64 78 29 3b 0a 20 20  , pTab, idx);.  
228ac 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 64 6f    }.  }.  if( do
228ad 4d 61 6b 65 52 65 63 20 29 7b 0a 20 20 20 20 73  MakeRec ){.    s
228ae 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
228af 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
228b0 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c  d, regBase, nCol
228b1 2b 31 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20 20  +1, regOut);.   
228b2 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66   sqlite3IndexAff
228b3 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64 78  inityStr(v, pIdx
228b4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
228b5 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
228b6 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  hange(pParse, re
228b7 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a  gBase, nCol+1);.
228b8 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c    }.  sqlite3Rel
228b9 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
228ba 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e  arse, regBase, n
228bb 43 6f 6c 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e  Col+1);.  return
228bc 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 20   regBase;.}../* 
228bd 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56 69 65  Make sure "isVie
228be 77 22 20 67 65 74 73 20 75 6e 64 65 66 69 6e 65  w" gets undefine
228bf 64 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 66  d in case this f
228c0 69 6c 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74  ile becomes part
228c1 20 6f 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c 67   of.** the amalg
228c2 61 6d 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68 61  amation - so tha
228c3 74 20 73 75 62 73 65 71 75 65 6e 74 20 66 69 6c  t subsequent fil
228c4 65 73 20 64 6f 20 6e 6f 74 20 73 65 65 20 69 73  es do not see is
228c5 56 69 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61 63  View as a.** mac
228c6 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69 73  ro. */.#undef is
228c7 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  View../*********
228c8 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 65 6c  ***** End of del
228c9 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ete.c **********
228ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228cc 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
228cd 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
228ce 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   func.c ********
228cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228d1 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 32  ****/./*.** 2002
228d2 20 46 65 62 72 75 61 72 79 20 32 33 0a 2a 2a 0a   February 23.**.
228d3 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
228d4 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
228d5 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
228d6 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
228d7 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
228d8 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
228d9 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
228da 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
228db 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
228dc 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
228dd 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
228de 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
228df 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
228e0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
228e1 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
228e2 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
228e3 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
228e4 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
228e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
228e9 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
228ea 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
228eb 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
228ec 20 76 61 72 69 6f 75 73 20 53 51 4c 0a 2a 2a 20   various SQL.** 
228ed 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 53 51 4c  functions of SQL
228ee 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ite.  .**.** The
228ef 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65  re is only one e
228f0 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69  xported symbol i
228f1 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68  n this file - th
228f2 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71  e function.** sq
228f3 6c 69 74 65 52 65 67 69 73 74 65 72 42 75 69 6c  liteRegisterBuil
228f4 64 69 6e 46 75 6e 63 74 69 6f 6e 73 28 29 20 66  dinFunctions() f
228f5 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74  ound at the bott
228f6 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a  om of the file..
228f7 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64  ** All other cod
228f8 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65  e has file scope
228f9 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 75 6e  ..**.** $Id: fun
228fa 63 2e 63 2c 76 20 31 2e 32 30 39 20 32 30 30 38  c.c,v 1.209 2008
228fb 2f 31 32 2f 31 30 20 32 33 3a 30 34 3a 31 33 20  /12/10 23:04:13 
228fc 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
228fd 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63  .** Return the c
228fe 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f  ollating functio
228ff 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  n associated wit
22900 68 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  h a function..*/
22901 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20  .static CollSeq 
22902 2a 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43  *sqlite3GetFuncC
22903 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 5f 63  ollSeq(sqlite3_c
22904 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29  ontext *context)
22905 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e 74 65  {.  return conte
22906 78 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a  xt->pColl;.}../*
22907 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
22908 6f 6e 20 6f 66 20 74 68 65 20 6e 6f 6e 2d 61 67  on of the non-ag
22909 67 72 65 67 61 74 65 20 6d 69 6e 28 29 20 61 6e  gregate min() an
2290a 64 20 6d 61 78 28 29 20 66 75 6e 63 74 69 6f 6e  d max() function
2290b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s.*/.static void
2290c 20 6d 69 6e 6d 61 78 46 75 6e 63 28 0a 20 20 73   minmaxFunc(.  s
2290d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
2290e 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
2290f 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
22910 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
22911 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d 61   int i;.  int ma
22912 73 6b 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72 20  sk;    /* 0 for 
22913 6d 69 6e 28 29 20 6f 72 20 30 78 66 66 66 66 66  min() or 0xfffff
22914 66 66 66 20 66 6f 72 20 6d 61 78 28 29 20 2a 2f  fff for max() */
22915 0a 20 20 69 6e 74 20 69 42 65 73 74 3b 0a 20 20  .  int iBest;.  
22916 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
22917 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 20 29  .  if( argc==0 )
22918 20 72 65 74 75 72 6e 3b 0a 20 20 6d 61 73 6b 20   return;.  mask 
22919 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  = sqlite3_user_d
2291a 61 74 61 28 63 6f 6e 74 65 78 74 29 3d 3d 30 20  ata(context)==0 
2291b 3f 20 30 20 3a 20 2d 31 3b 0a 20 20 70 43 6f 6c  ? 0 : -1;.  pCol
2291c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75  l = sqlite3GetFu
2291d 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78  ncCollSeq(contex
2291e 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  t);.  assert( pC
2291f 6f 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  oll );.  assert(
22920 20 6d 61 73 6b 3d 3d 2d 31 20 7c 7c 20 6d 61 73   mask==-1 || mas
22921 6b 3d 3d 30 20 29 3b 0a 20 20 69 42 65 73 74 20  k==0 );.  iBest 
22922 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  = 0;.  if( sqlit
22923 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
22924 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  gv[0])==SQLITE_N
22925 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ULL ) return;.  
22926 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b  for(i=1; i<argc;
22927 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73   i++){.    if( s
22928 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
22929 65 28 61 72 67 76 5b 69 5d 29 3d 3d 53 51 4c 49  e(argv[i])==SQLI
2292a 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  TE_NULL ) return
2292b 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74  ;.    if( (sqlit
2292c 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 67  e3MemCompare(arg
2292d 76 5b 69 42 65 73 74 5d 2c 20 61 72 67 76 5b 69  v[iBest], argv[i
2292e 5d 2c 20 70 43 6f 6c 6c 29 5e 6d 61 73 6b 29 3e  ], pColl)^mask)>
2292f 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 42 65 73  =0 ){.      iBes
22930 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d  t = i;.    }.  }
22931 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
22932 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
22933 20 61 72 67 76 5b 69 42 65 73 74 5d 29 3b 0a 7d   argv[iBest]);.}
22934 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
22935 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20 61  he type of the a
22936 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  rgument..*/.stat
22937 69 63 20 76 6f 69 64 20 74 79 70 65 6f 66 46 75  ic void typeofFu
22938 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
22939 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
2293a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
2293b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2293c 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
2293d 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20   char *z = 0;.  
2293e 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
2293f 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 73 77 69  (NotUsed);.  swi
22940 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c  tch( sqlite3_val
22941 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
22942 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
22943 49 54 45 5f 4e 55 4c 4c 3a 20 20 20 20 7a 20 3d  ITE_NULL:    z =
22944 20 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65 61   "null";    brea
22945 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
22946 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a 20 3d 20  TE_INTEGER: z = 
22947 22 69 6e 74 65 67 65 72 22 3b 20 62 72 65 61 6b  "integer"; break
22948 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
22949 45 5f 54 45 58 54 3a 20 20 20 20 7a 20 3d 20 22  E_TEXT:    z = "
2294a 74 65 78 74 22 3b 20 20 20 20 62 72 65 61 6b 3b  text";    break;
2294b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2294c 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d 20 22 72  _FLOAT:   z = "r
2294d 65 61 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a  eal";    break;.
2294e 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2294f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20 22 62 6c  BLOB:    z = "bl
22950 6f 62 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20  ob";    break;. 
22951 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73   }.  sqlite3_res
22952 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
22953 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  , z, -1, SQLITE_
22954 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  STATIC);.}.../*.
22955 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
22956 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68 28  n of the length(
22957 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74  ) function.*/.st
22958 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67 74 68  atic void length
22959 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
2295a 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2295b 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
2295c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2295d 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6c 65  argv.){.  int le
2295e 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  n;..  assert( ar
2295f 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
22960 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
22961 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c  );.  switch( sql
22962 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
22963 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20  argv[0]) ){.    
22964 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  case SQLITE_BLOB
22965 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  :.    case SQLIT
22966 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63  E_INTEGER:.    c
22967 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ase SQLITE_FLOAT
22968 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
22969 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e  3_result_int(con
2296a 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76 61  text, sqlite3_va
2296b 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
2296c 5d 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ]));.      break
2296d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2296e 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a   SQLITE_TEXT: {.
2296f 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69        const unsi
22970 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d 20 73  gned char *z = s
22971 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
22972 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20  t(argv[0]);.    
22973 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
22974 75 72 6e 3b 0a 20 20 20 20 20 20 6c 65 6e 20 3d  urn;.      len =
22975 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   0;.      while(
22976 20 2a 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6c   *z ){.        l
22977 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 53 51  en++;.        SQ
22978 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
22979 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2297a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2297b 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 6c 65 6e  int(context, len
2297c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2297d 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
2297e 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t: {.      sqlit
2297f 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63  e3_result_null(c
22980 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62  ontext);.      b
22981 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
22982 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
22983 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61  ntation of the a
22984 62 73 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f  bs() function.*/
22985 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 62 73  .static void abs
22986 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Func(sqlite3_con
22987 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
22988 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
22989 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a  _value **argv){.
2298a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
2298b 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  1 );.  UNUSED_PA
2298c 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
2298d 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33   switch( sqlite3
2298e 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
2298f 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65  [0]) ){.    case
22990 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a   SQLITE_INTEGER:
22991 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 56 61   {.      i64 iVa
22992 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
22993 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29  e_int64(argv[0])
22994 3b 0a 20 20 20 20 20 20 69 66 28 20 69 56 61 6c  ;.      if( iVal
22995 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  <0 ){.        if
22996 28 20 28 69 56 61 6c 3c 3c 31 29 3d 3d 30 20 29  ( (iVal<<1)==0 )
22997 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
22998 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
22999 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e 74 65 67  (context, "integ
2299a 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c 20 2d 31  er overflow", -1
2299b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
2299c 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  urn;.        }. 
2299d 20 20 20 20 20 20 20 69 56 61 6c 20 3d 20 2d 69         iVal = -i
2299e 56 61 6c 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20  Val;.      } .  
2299f 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
229a0 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74  lt_int64(context
229a1 2c 20 69 56 61 6c 29 3b 0a 20 20 20 20 20 20 62  , iVal);.      b
229a2 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
229a3 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  case SQLITE_NULL
229a4 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
229a5 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f  3_result_null(co
229a6 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 72  ntext);.      br
229a7 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
229a8 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
229a9 64 6f 75 62 6c 65 20 72 56 61 6c 20 3d 20 73 71  double rVal = sq
229aa 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
229ab 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  le(argv[0]);.   
229ac 20 20 20 69 66 28 20 72 56 61 6c 3c 30 20 29 20     if( rVal<0 ) 
229ad 72 56 61 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20 20  rVal = -rVal;.  
229ae 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
229af 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78  lt_double(contex
229b0 74 2c 20 72 56 61 6c 29 3b 0a 20 20 20 20 20 20  t, rVal);.      
229b1 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
229b2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
229b3 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
229b4 73 75 62 73 74 72 28 29 20 66 75 6e 63 74 69 6f  substr() functio
229b5 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 73 74 72 28  n..**.** substr(
229b6 78 2c 70 31 2c 70 32 29 20 20 72 65 74 75 72 6e  x,p1,p2)  return
229b7 73 20 70 32 20 63 68 61 72 61 63 74 65 72 73 20  s p2 characters 
229b8 6f 66 20 78 5b 5d 20 62 65 67 69 6e 6e 69 6e 67  of x[] beginning
229b9 20 77 69 74 68 20 70 31 2e 0a 2a 2a 20 70 31 20   with p1..** p1 
229ba 69 73 20 31 2d 69 6e 64 65 78 65 64 2e 20 20 53  is 1-indexed.  S
229bb 6f 20 73 75 62 73 74 72 28 78 2c 31 2c 31 29 20  o substr(x,1,1) 
229bc 72 65 74 75 72 6e 73 20 74 68 65 20 66 69 72 73  returns the firs
229bd 74 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 6f  t character.** o
229be 66 20 78 2e 20 20 49 66 20 78 20 69 73 20 74 65  f x.  If x is te
229bf 78 74 2c 20 74 68 65 6e 20 77 65 20 61 63 74 75  xt, then we actu
229c0 61 6c 6c 79 20 63 6f 75 6e 74 20 55 54 46 2d 38  ally count UTF-8
229c1 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20   characters..** 
229c2 49 66 20 78 20 69 73 20 61 20 62 6c 6f 62 2c 20  If x is a blob, 
229c3 74 68 65 6e 20 77 65 20 63 6f 75 6e 74 20 62 79  then we count by
229c4 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 31  tes..**.** If p1
229c5 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
229c6 65 6e 20 77 65 20 62 65 67 69 6e 20 61 62 73 28  en we begin abs(
229c7 70 31 29 20 66 72 6f 6d 20 74 68 65 20 65 6e 64  p1) from the end
229c8 20 6f 66 20 78 5b 5d 2e 0a 2a 2f 0a 73 74 61 74   of x[]..*/.stat
229c9 69 63 20 76 6f 69 64 20 73 75 62 73 74 72 46 75  ic void substrFu
229ca 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
229cb 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
229cc 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
229cd 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
229ce 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  gv.){.  const un
229cf 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a  signed char *z;.
229d0 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
229d1 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74   char *z2;.  int
229d2 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 70 30 74 79   len;.  int p0ty
229d3 70 65 3b 0a 20 20 69 36 34 20 70 31 2c 20 70 32  pe;.  i64 p1, p2
229d4 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ;..  assert( arg
229d5 63 3d 3d 33 20 7c 7c 20 61 72 67 63 3d 3d 32 20  c==3 || argc==2 
229d6 29 3b 0a 20 20 70 30 74 79 70 65 20 3d 20 73 71  );.  p0type = sq
229d7 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
229d8 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28  (argv[0]);.  if(
229d9 20 70 30 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f   p0type==SQLITE_
229da 42 4c 4f 42 20 29 7b 0a 20 20 20 20 6c 65 6e 20  BLOB ){.    len 
229db 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
229dc 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
229dd 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
229de 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
229df 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d  0]);.    if( z==
229e0 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
229e1 61 73 73 65 72 74 28 20 6c 65 6e 3d 3d 73 71 6c  assert( len==sql
229e2 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
229e3 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 7d  (argv[0]) );.  }
229e4 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71  else{.    z = sq
229e5 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
229e6 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69  (argv[0]);.    i
229e7 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
229e8 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20  ;.    len = 0;. 
229e9 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32     for(z2=z; *z2
229ea 3b 20 6c 65 6e 2b 2b 29 7b 0a 20 20 20 20 20 20  ; len++){.      
229eb 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
229ec 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  (z2);.    }.  }.
229ed 20 20 70 31 20 3d 20 73 71 6c 69 74 65 33 5f 76    p1 = sqlite3_v
229ee 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d  alue_int(argv[1]
229ef 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33  );.  if( argc==3
229f0 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c   ){.    p2 = sql
229f1 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
229f2 72 67 76 5b 32 5d 29 3b 0a 20 20 7d 65 6c 73 65  rgv[2]);.  }else
229f3 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69 74  {.    p2 = sqlit
229f4 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
229f5 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61  ndle(context)->a
229f6 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
229f7 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a  IT_LENGTH];.  }.
229f8 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20    if( p1<0 ){.  
229f9 20 20 70 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20    p1 += len;.   
229fa 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20   if( p1<0 ){.   
229fb 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20     p2 += p1;.   
229fc 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d     p1 = 0;.    }
229fd 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 31 3e  .  }else if( p1>
229fe 30 20 29 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a 20  0 ){.    p1--;. 
229ff 20 7d 0a 20 20 69 66 28 20 70 31 2b 70 32 3e 6c   }.  if( p1+p2>l
22a00 65 6e 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 6c  en ){.    p2 = l
22a01 65 6e 2d 70 31 3b 0a 20 20 7d 0a 20 20 69 66 28  en-p1;.  }.  if(
22a02 20 70 30 74 79 70 65 21 3d 53 51 4c 49 54 45 5f   p0type!=SQLITE_
22a03 42 4c 4f 42 20 29 7b 0a 20 20 20 20 77 68 69 6c  BLOB ){.    whil
22a04 65 28 20 2a 7a 20 26 26 20 70 31 20 29 7b 0a 20  e( *z && p1 ){. 
22a05 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50       SQLITE_SKIP
22a06 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20  _UTF8(z);.      
22a07 70 31 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  p1--;.    }.    
22a08 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26  for(z2=z; *z2 &&
22a09 20 70 32 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 20   p2; p2--){.    
22a0a 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54    SQLITE_SKIP_UT
22a0b 46 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20  F8(z2);.    }.  
22a0c 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
22a0d 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28  _text(context, (
22a0e 63 68 61 72 2a 29 7a 2c 20 28 69 6e 74 29 28 7a  char*)z, (int)(z
22a0f 32 2d 7a 29 2c 20 53 51 4c 49 54 45 5f 54 52 41  2-z), SQLITE_TRA
22a10 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65  NSIENT);.  }else
22a11 7b 0a 20 20 20 20 69 66 28 20 70 32 3c 30 20 29  {.    if( p2<0 )
22a12 20 70 32 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c   p2 = 0;.    sql
22a13 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
22a14 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
22a15 29 26 7a 5b 70 31 5d 2c 20 28 69 6e 74 29 70 32  )&z[p1], (int)p2
22a16 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
22a17 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  NT);.  }.}../*.*
22a18 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
22a19 20 6f 66 20 74 68 65 20 72 6f 75 6e 64 28 29 20   of the round() 
22a1a 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74  function.*/.stat
22a1b 69 63 20 76 6f 69 64 20 72 6f 75 6e 64 46 75 6e  ic void roundFun
22a1c 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
22a1d 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
22a1e 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
22a1f 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 69  lue **argv){.  i
22a20 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 75 62  nt n = 0;.  doub
22a21 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 7a 42 75  le r;.  char zBu
22a22 66 5b 35 30 30 5d 3b 20 20 2f 2a 20 6c 61 72 67  f[500];  /* larg
22a23 65 72 20 74 68 61 6e 20 74 68 65 20 25 66 20 72  er than the %f r
22a24 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
22a25 20 74 68 65 20 6c 61 72 67 65 73 74 20 64 6f 75   the largest dou
22a26 62 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ble */.  assert(
22a27 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63   argc==1 || argc
22a28 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61 72 67  ==2 );.  if( arg
22a29 63 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66 28 20  c==2 ){.    if( 
22a2a 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c  SQLITE_NULL==sql
22a2b 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
22a2c 61 72 67 76 5b 31 5d 29 20 29 20 72 65 74 75 72  argv[1]) ) retur
22a2d 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  n;.    n = sqlit
22a2e 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67  e3_value_int(arg
22a2f 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e  v[1]);.    if( n
22a30 3e 33 30 20 29 20 6e 20 3d 20 33 30 3b 0a 20 20  >30 ) n = 30;.  
22a31 20 20 69 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20    if( n<0 ) n = 
22a32 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  0;.  }.  if( sql
22a33 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
22a34 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45  argv[0])==SQLITE
22a35 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a  _NULL ) return;.
22a36 20 20 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    r = sqlite3_va
22a37 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
22a38 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  0]);.  sqlite3_s
22a39 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
22a3a 42 75 66 29 2c 7a 42 75 66 2c 22 25 2e 2a 66 22  Buf),zBuf,"%.*f"
22a3b 2c 6e 2c 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  ,n,r);.  sqlite3
22a3c 41 74 6f 46 28 7a 42 75 66 2c 20 26 72 29 3b 0a  AtoF(zBuf, &r);.
22a3d 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
22a3e 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c  _double(context,
22a3f 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c   r);.}../*.** Al
22a40 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74  locate nByte byt
22a41 65 73 20 6f 66 20 73 70 61 63 65 20 75 73 69 6e  es of space usin
22a42 67 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  g sqlite3_malloc
22a43 28 29 2e 20 49 66 20 74 68 65 0a 2a 2a 20 61 6c  (). If the.** al
22a44 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
22a45 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73  call sqlite3_res
22a46 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
22a47 29 20 74 6f 20 6e 6f 74 69 66 79 0a 2a 2a 20 74  ) to notify.** t
22a48 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
22a49 6c 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29  le that malloc()
22a4a 20 68 61 73 20 66 61 69 6c 65 64 2e 0a 2a 2f 0a   has failed..*/.
22a4b 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e  static void *con
22a4c 74 65 78 74 4d 61 6c 6c 6f 63 28 73 71 6c 69 74  textMalloc(sqlit
22a4d 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
22a4e 65 78 74 2c 20 69 36 34 20 6e 42 79 74 65 29 7b  ext, i64 nByte){
22a4f 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66  .  char *z;.  if
22a50 28 20 6e 42 79 74 65 3e 73 71 6c 69 74 65 33 5f  ( nByte>sqlite3_
22a51 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
22a52 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d  e(context)->aLim
22a53 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
22a54 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73  LENGTH] ){.    s
22a55 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
22a56 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65  ror_toobig(conte
22a57 78 74 29 3b 0a 20 20 20 20 7a 20 3d 20 30 3b 0a  xt);.    z = 0;.
22a58 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d    }else{.    z =
22a59 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 28   sqlite3Malloc((
22a5a 69 6e 74 29 6e 42 79 74 65 29 3b 0a 20 20 20 20  int)nByte);.    
22a5b 69 66 28 20 21 7a 20 26 26 20 6e 42 79 74 65 3e  if( !z && nByte>
22a5c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
22a5d 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
22a5e 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
22a5f 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
22a60 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  rn z;.}../*.** I
22a61 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
22a62 20 74 68 65 20 75 70 70 65 72 28 29 20 61 6e 64   the upper() and
22a63 20 6c 6f 77 65 72 28 29 20 53 51 4c 20 66 75 6e   lower() SQL fun
22a64 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69  ctions..*/.stati
22a65 63 20 76 6f 69 64 20 75 70 70 65 72 46 75 6e 63  c void upperFunc
22a66 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
22a67 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61   *context, int a
22a68 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
22a69 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63 68  ue **argv){.  ch
22a6a 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20  ar *z1;.  const 
22a6b 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20  char *z2;.  int 
22a6c 69 2c 20 6e 3b 0a 20 20 69 66 28 20 61 72 67 63  i, n;.  if( argc
22a6d 3c 31 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 4c  <1 || SQLITE_NUL
22a6e 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  L==sqlite3_value
22a6f 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29  _type(argv[0]) )
22a70 20 72 65 74 75 72 6e 3b 0a 20 20 7a 32 20 3d 20   return;.  z2 = 
22a71 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76  (char*)sqlite3_v
22a72 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
22a73 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65  ]);.  n = sqlite
22a74 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
22a75 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72  gv[0]);.  /* Ver
22a76 69 66 79 20 74 68 61 74 20 74 68 65 20 63 61 6c  ify that the cal
22a77 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20 64 6f  l to _bytes() do
22a78 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74  es not invalidat
22a79 65 20 74 68 65 20 5f 74 65 78 74 28 29 20 70 6f  e the _text() po
22a7a 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73 65 72  inter */.  asser
22a7b 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71  t( z2==(char*)sq
22a7c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
22a7d 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69  (argv[0]) );.  i
22a7e 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20  f( z2 ){.    z1 
22a7f 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28  = contextMalloc(
22a80 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e  context, ((i64)n
22a81 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 7a 31  )+1);.    if( z1
22a82 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
22a83 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20  (z1, z2, n+1);. 
22a84 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31       for(i=0; z1
22a85 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  [i]; i++){.     
22a86 20 20 20 7a 31 5b 69 5d 20 3d 20 28 63 68 61 72     z1[i] = (char
22a87 29 74 6f 75 70 70 65 72 28 7a 31 5b 69 5d 29 3b  )toupper(z1[i]);
22a88 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
22a89 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
22a8a 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20  xt(context, z1, 
22a8b 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65  -1, sqlite3_free
22a8c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73  );.    }.  }.}.s
22a8d 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 77 65 72  tatic void lower
22a8e 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Func(sqlite3_con
22a8f 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
22a90 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
22a91 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a  _value **argv){.
22a92 20 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f    char *z1;.  co
22a93 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20  nst char *z2;.  
22a94 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20  int i, n;.  if( 
22a95 61 72 67 63 3c 31 20 7c 7c 20 53 51 4c 49 54 45  argc<1 || SQLITE
22a96 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76  _NULL==sqlite3_v
22a97 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
22a98 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a  ]) ) return;.  z
22a99 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  2 = (char*)sqlit
22a9a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
22a9b 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71  gv[0]);.  n = sq
22a9c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
22a9d 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a  s(argv[0]);.  /*
22a9e 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
22a9f 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28   call to _bytes(
22aa0 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c  ) does not inval
22aa1 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28  idate the _text(
22aa2 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61  ) pointer */.  a
22aa3 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72  ssert( z2==(char
22aa4 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
22aa5 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b  text(argv[0]) );
22aa6 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20  .  if( z2 ){.   
22aa7 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c   z1 = contextMal
22aa8 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69  loc(context, ((i
22aa9 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66  64)n)+1);.    if
22aaa 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65  ( z1 ){.      me
22aab 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31  mcpy(z1, z2, n+1
22aac 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
22aad 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  ; z1[i]; i++){. 
22aae 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 28         z1[i] = (
22aaf 63 68 61 72 29 74 6f 6c 6f 77 65 72 28 7a 31 5b  char)tolower(z1[
22ab0 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i]);.      }.   
22ab1 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
22ab2 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
22ab3 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f  z1, -1, sqlite3_
22ab4 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  free);.    }.  }
22ab5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
22ab6 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
22ab7 49 46 4e 55 4c 4c 28 29 2c 20 4e 56 4c 28 29 2c  IFNULL(), NVL(),
22ab8 20 61 6e 64 20 43 4f 41 4c 45 53 43 45 28 29 20   and COALESCE() 
22ab9 66 75 6e 63 74 69 6f 6e 73 2e 20 20 0a 2a 2a 20  functions.  .** 
22aba 41 6c 6c 20 74 68 72 65 65 20 64 6f 20 74 68 65  All three do the
22abb 20 73 61 6d 65 20 74 68 69 6e 67 2e 20 20 54 68   same thing.  Th
22abc 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 66 69  ey return the fi
22abd 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20  rst non-NULL.** 
22abe 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61  argument..*/.sta
22abf 74 69 63 20 76 6f 69 64 20 69 66 6e 75 6c 6c 46  tic void ifnullF
22ac0 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
22ac1 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
22ac2 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
22ac3 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
22ac4 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rgv.){.  int i;.
22ac5 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61 72 67    for(i=0; i<arg
22ac6 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  c; i++){.    if(
22ac7 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71   SQLITE_NULL!=sq
22ac8 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
22ac9 28 61 72 67 76 5b 69 5d 29 20 29 7b 0a 20 20 20  (argv[i]) ){.   
22aca 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
22acb 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
22acc 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 20   argv[i]);.     
22acd 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
22ace 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
22acf 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e  mentation of ran
22ad0 64 6f 6d 28 29 2e 20 20 52 65 74 75 72 6e 20 61  dom().  Return a
22ad1 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 2e   random integer.
22ad2 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69    .*/.static voi
22ad3 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28 0a 20 20  d randomFunc(.  
22ad4 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
22ad5 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
22ad6 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
22ad7 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73  e3_value **NotUs
22ad8 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65 5f  ed2.){.  sqlite_
22ad9 69 6e 74 36 34 20 72 3b 0a 20 20 55 4e 55 53 45  int64 r;.  UNUSE
22ada 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
22adb 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
22adc 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  .  sqlite3_rando
22add 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 29 2c  mness(sizeof(r),
22ade 20 26 72 29 3b 0a 20 20 69 66 28 20 28 72 3c 3c   &r);.  if( (r<<
22adf 31 29 3d 3d 30 20 29 20 72 20 3d 20 30 3b 20 20  1)==0 ) r = 0;  
22ae0 2f 2a 20 50 72 65 76 65 6e 74 20 30 78 38 30 30  /* Prevent 0x800
22ae1 30 2e 2e 2e 2e 20 61 73 20 74 68 65 20 72 65 73  0.... as the res
22ae2 75 6c 74 20 73 6f 20 74 68 61 74 20 77 65 20 2a  ult so that we *
22ae3 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
22ae4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63              /* c
22ae5 61 6e 20 61 6c 77 61 79 73 20 64 6f 20 61 62 73  an always do abs
22ae6 28 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  () of the result
22ae7 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65   */.  sqlite3_re
22ae8 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65  sult_int64(conte
22ae9 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  xt, r);.}../*.**
22aea 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
22aeb 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 4e 29  of randomblob(N)
22aec 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 6e 64  .  Return a rand
22aed 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68 61 74 20  om blob.** that 
22aee 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 2e  is N bytes long.
22aef 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
22af0 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20 20 73 71  randomBlob(.  sq
22af1 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
22af2 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
22af3 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
22af4 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
22af5 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65  int n;.  unsigne
22af6 64 20 63 68 61 72 20 2a 70 3b 0a 20 20 61 73 73  d char *p;.  ass
22af7 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a  ert( argc==1 );.
22af8 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
22af9 45 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20  ER(argc);.  n = 
22afa 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
22afb 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66  t(argv[0]);.  if
22afc 28 20 6e 3c 31 20 29 7b 0a 20 20 20 20 6e 20 3d  ( n<1 ){.    n =
22afd 20 31 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 63 6f   1;.  }.  p = co
22afe 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
22aff 65 78 74 2c 20 6e 29 3b 0a 20 20 69 66 28 20 70  ext, n);.  if( p
22b00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
22b01 72 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c 20 70 29  randomness(n, p)
22b02 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
22b03 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78  sult_blob(contex
22b04 74 2c 20 28 63 68 61 72 2a 29 70 2c 20 6e 2c 20  t, (char*)p, n, 
22b05 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
22b06 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
22b07 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
22b08 65 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  e last_insert_ro
22b09 77 69 64 28 29 20 53 51 4c 20 66 75 6e 63 74 69  wid() SQL functi
22b0a 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a  on.  The return.
22b0b 2a 2a 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  ** value is the 
22b0c 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69  same as the sqli
22b0d 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f  te3_last_insert_
22b0e 72 6f 77 69 64 28 29 20 41 50 49 20 66 75 6e 63  rowid() API func
22b0f 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
22b10 76 6f 69 64 20 6c 61 73 74 5f 69 6e 73 65 72 74  void last_insert
22b11 5f 72 6f 77 69 64 28 0a 20 20 73 71 6c 69 74 65  _rowid(.  sqlite
22b12 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
22b13 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55 73  xt, .  int NotUs
22b14 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76  ed, .  sqlite3_v
22b15 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a  alue **NotUsed2.
22b16 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
22b17 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
22b18 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
22b19 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f  text);.  UNUSED_
22b1a 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
22b1b 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
22b1c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
22b1d 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 73  int64(context, s
22b1e 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
22b1f 72 74 5f 72 6f 77 69 64 28 64 62 29 29 3b 0a 7d  rt_rowid(db));.}
22b20 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
22b21 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68  tation of the ch
22b22 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63  anges() SQL func
22b23 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72  tion.  The retur
22b24 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a  n value is the.*
22b25 2a 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71  * same as the sq
22b26 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20  lite3_changes() 
22b27 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  API function..*/
22b28 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 61  .static void cha
22b29 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65 33 5f  nges(.  sqlite3_
22b2a 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
22b2b 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
22b2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
22b2d 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20   **NotUsed2.){. 
22b2e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
22b2f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
22b30 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
22b31 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
22b32 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
22b33 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c  NotUsed2);.  sql
22b34 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
22b35 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33  context, sqlite3
22b36 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d  _changes(db));.}
22b37 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
22b38 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 6f  tation of the to
22b39 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 53 51  tal_changes() SQ
22b3a 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  L function.  The
22b3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
22b3c 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20  .** the same as 
22b3d 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  the sqlite3_tota
22b3e 6c 5f 63 68 61 6e 67 65 73 28 29 20 41 50 49 20  l_changes() API 
22b3f 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
22b40 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 5f 63  tic void total_c
22b41 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65  hanges(.  sqlite
22b42 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
22b43 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
22b44 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
22b45 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
22b46 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
22b47 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
22b48 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
22b49 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  xt);.  UNUSED_PA
22b4a 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
22b4b 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73  , NotUsed2);.  s
22b4c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
22b4d 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74  t(context, sqlit
22b4e 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
22b4f 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (db));.}../*.** 
22b50 41 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69  A structure defi
22b51 6e 69 6e 67 20 68 6f 77 20 74 6f 20 64 6f 20 47  ning how to do G
22b52 4c 4f 42 2d 73 74 79 6c 65 20 63 6f 6d 70 61 72  LOB-style compar
22b53 69 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  isons..*/.struct
22b54 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 7b 0a 20   compareInfo {. 
22b55 20 75 38 20 6d 61 74 63 68 41 6c 6c 3b 0a 20 20   u8 matchAll;.  
22b56 75 38 20 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75  u8 matchOne;.  u
22b57 38 20 6d 61 74 63 68 53 65 74 3b 0a 20 20 75 38  8 matchSet;.  u8
22b58 20 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a   noCase;.};../*.
22b59 2a 2a 20 46 6f 72 20 4c 49 4b 45 20 61 6e 64 20  ** For LIKE and 
22b5a 47 4c 4f 42 20 6d 61 74 63 68 69 6e 67 20 6f 6e  GLOB matching on
22b5b 20 45 42 43 44 49 43 20 6d 61 63 68 69 6e 65 73   EBCDIC machines
22b5c 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 65 76  , assume that ev
22b5d 65 72 79 0a 2a 2a 20 63 68 61 72 61 63 74 65 72  ery.** character
22b5e 20 69 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20   is exactly one 
22b5f 62 79 74 65 20 69 6e 20 73 69 7a 65 2e 20 20 41  byte in size.  A
22b60 6c 73 6f 2c 20 61 6c 6c 20 63 68 61 72 61 63 74  lso, all charact
22b61 65 72 73 20 61 72 65 0a 2a 2a 20 61 62 6c 65 20  ers are.** able 
22b62 74 6f 20 70 61 72 74 69 63 69 70 61 74 65 20 69  to participate i
22b63 6e 20 75 70 70 65 72 2d 63 61 73 65 2d 74 6f 2d  n upper-case-to-
22b64 6c 6f 77 65 72 2d 63 61 73 65 20 6d 61 70 70 69  lower-case mappi
22b65 6e 67 73 20 69 6e 20 45 42 43 44 49 43 0a 2a 2a  ngs in EBCDIC.**
22b66 20 77 68 65 72 65 61 73 20 6f 6e 6c 79 20 63 68   whereas only ch
22b67 61 72 61 63 74 65 72 73 20 6c 65 73 73 20 74 68  aracters less th
22b68 61 6e 20 30 78 38 30 20 64 6f 20 69 6e 20 41 53  an 0x80 do in AS
22b69 43 49 49 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  CII..*/.#if defi
22b6a 6e 65 64 28 53 51 4c 49 54 45 5f 45 42 43 44 49  ned(SQLITE_EBCDI
22b6b 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  C).# define sqli
22b6c 74 65 33 55 74 66 38 52 65 61 64 28 41 2c 42 2c  te3Utf8Read(A,B,
22b6d 43 29 20 20 28 2a 28 41 2b 2b 29 29 0a 23 20 64  C)  (*(A++)).# d
22b6e 65 66 69 6e 65 20 47 6c 6f 67 55 70 70 65 72 54  efine GlogUpperT
22b6f 6f 4c 6f 77 65 72 28 41 29 20 20 20 20 20 41 20  oLower(A)     A 
22b70 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  = sqlite3UpperTo
22b71 4c 6f 77 65 72 5b 41 5d 0a 23 65 6c 73 65 0a 23  Lower[A].#else.#
22b72 20 64 65 66 69 6e 65 20 47 6c 6f 67 55 70 70 65   define GlogUppe
22b73 72 54 6f 4c 6f 77 65 72 28 41 29 20 20 20 20 20  rToLower(A)     
22b74 69 66 28 20 41 3c 30 78 38 30 20 29 7b 20 41 20  if( A<0x80 ){ A 
22b75 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  = sqlite3UpperTo
22b76 4c 6f 77 65 72 5b 41 5d 3b 20 7d 0a 23 65 6e 64  Lower[A]; }.#end
22b77 69 66 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  if..static const
22b78 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49   struct compareI
22b79 6e 66 6f 20 67 6c 6f 62 49 6e 66 6f 20 3d 20 7b  nfo globInfo = {
22b7a 20 27 2a 27 2c 20 27 3f 27 2c 20 27 5b 27 2c 20   '*', '?', '[', 
22b7b 30 20 7d 3b 0a 2f 2a 20 54 68 65 20 63 6f 72 72  0 };./* The corr
22b7c 65 63 74 20 53 51 4c 2d 39 32 20 62 65 68 61 76  ect SQL-92 behav
22b7d 69 6f 72 20 69 73 20 66 6f 72 20 74 68 65 20 4c  ior is for the L
22b7e 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  IKE operator to 
22b7f 69 67 6e 6f 72 65 0a 2a 2a 20 63 61 73 65 2e 20  ignore.** case. 
22b80 20 54 68 75 73 20 20 27 61 27 20 4c 49 4b 45 20   Thus  'a' LIKE 
22b81 27 41 27 20 77 6f 75 6c 64 20 62 65 20 74 72 75  'A' would be tru
22b82 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  e. */.static con
22b83 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72  st struct compar
22b84 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 4e 6f  eInfo likeInfoNo
22b85 72 6d 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c  rm = { '%', '_',
22b86 20 20 20 30 2c 20 31 20 7d 3b 0a 2f 2a 20 49 66     0, 1 };./* If
22b87 20 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e   SQLITE_CASE_SEN
22b88 53 49 54 49 56 45 5f 4c 49 4b 45 20 69 73 20 64  SITIVE_LIKE is d
22b89 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
22b8a 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 0a 2a   LIKE operator.*
22b8b 2a 20 69 73 20 63 61 73 65 20 73 65 6e 73 69 74  * is case sensit
22b8c 69 76 65 20 63 61 75 73 69 6e 67 20 27 61 27 20  ive causing 'a' 
22b8d 4c 49 4b 45 20 27 41 27 20 74 6f 20 62 65 20 66  LIKE 'A' to be f
22b8e 61 6c 73 65 20 2a 2f 0a 73 74 61 74 69 63 20 63  alse */.static c
22b8f 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70  onst struct comp
22b90 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f  areInfo likeInfo
22b91 41 6c 74 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27  Alt = { '%', '_'
22b92 2c 20 20 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 0a  ,   0, 0 };../*.
22b93 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20 55  ** Compare two U
22b94 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f 72  TF-8 strings for
22b95 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 65 20   equality where 
22b96 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
22b97 20 63 61 6e 0a 2a 2a 20 70 6f 74 65 6e 74 69 61   can.** potentia
22b98 6c 6c 79 20 62 65 20 61 20 22 67 6c 6f 62 22 20  lly be a "glob" 
22b99 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 52 65 74  expression.  Ret
22b9a 75 72 6e 20 74 72 75 65 20 28 31 29 20 69 66 20  urn true (1) if 
22b9b 74 68 65 79 0a 2a 2a 20 61 72 65 20 74 68 65 20  they.** are the 
22b9c 73 61 6d 65 20 61 6e 64 20 66 61 6c 73 65 20 28  same and false (
22b9d 30 29 20 69 66 20 74 68 65 79 20 61 72 65 20 64  0) if they are d
22b9e 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  ifferent..**.** 
22b9f 47 6c 6f 62 62 69 6e 67 20 72 75 6c 65 73 3a 0a  Globbing rules:.
22ba0 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 2a 27 20 20  **.**      '*'  
22ba1 20 20 20 20 20 4d 61 74 63 68 65 73 20 61 6e 79       Matches any
22ba2 20 73 65 71 75 65 6e 63 65 20 6f 66 20 7a 65 72   sequence of zer
22ba3 6f 20 6f 72 20 6d 6f 72 65 20 63 68 61 72 61 63  o or more charac
22ba4 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ters..**.**     
22ba5 20 27 3f 27 20 20 20 20 20 20 20 4d 61 74 63 68   '?'       Match
22ba6 65 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63  es exactly one c
22ba7 68 61 72 61 63 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  haracter..**.** 
22ba8 20 20 20 20 5b 2e 2e 2e 5d 20 20 20 20 20 20 4d      [...]      M
22ba9 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61 72 61  atches one chara
22baa 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 65 6e  cter from the en
22bab 63 6c 6f 73 65 64 20 6c 69 73 74 20 6f 66 0a 2a  closed list of.*
22bac 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
22bad 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a   characters..**.
22bae 2a 2a 20 20 20 20 20 5b 5e 2e 2e 2e 5d 20 20 20  **     [^...]   
22baf 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68    Matches one ch
22bb0 61 72 61 63 74 65 72 20 6e 6f 74 20 69 6e 20 74  aracter not in t
22bb1 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74  he enclosed list
22bb2 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65  ..**.** With the
22bb3 20 5b 2e 2e 2e 5d 20 61 6e 64 20 5b 5e 2e 2e 2e   [...] and [^...
22bb4 5d 20 6d 61 74 63 68 69 6e 67 2c 20 61 20 27 5d  ] matching, a ']
22bb5 27 20 63 68 61 72 61 63 74 65 72 20 63 61 6e 20  ' character can 
22bb6 62 65 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69  be included.** i
22bb7 6e 20 74 68 65 20 6c 69 73 74 20 62 79 20 6d 61  n the list by ma
22bb8 6b 69 6e 67 20 69 74 20 74 68 65 20 66 69 72 73  king it the firs
22bb9 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74 65  t character afte
22bba 72 20 27 5b 27 20 6f 72 20 27 5e 27 2e 20 20 41  r '[' or '^'.  A
22bbb 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 63 68 61  .** range of cha
22bbc 72 61 63 74 65 72 73 20 63 61 6e 20 62 65 20 73  racters can be s
22bbd 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 27  pecified using '
22bbe 2d 27 2e 20 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a  -'.  Example:.**
22bbf 20 22 5b 61 2d 7a 5d 22 20 6d 61 74 63 68 65 73   "[a-z]" matches
22bc0 20 61 6e 79 20 73 69 6e 67 6c 65 20 6c 6f 77 65   any single lowe
22bc1 72 2d 63 61 73 65 20 6c 65 74 74 65 72 2e 20 20  r-case letter.  
22bc2 54 6f 20 6d 61 74 63 68 20 61 20 27 2d 27 2c 20  To match a '-', 
22bc3 6d 61 6b 65 0a 2a 2a 20 69 74 20 74 68 65 20 6c  make.** it the l
22bc4 61 73 74 20 63 68 61 72 61 63 74 65 72 20 69 6e  ast character in
22bc5 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a   the list..**.**
22bc6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
22bc7 20 75 73 75 61 6c 6c 79 20 71 75 69 63 6b 2c 20   usually quick, 
22bc8 62 75 74 20 63 61 6e 20 62 65 20 4e 2a 2a 32 20  but can be N**2 
22bc9 69 6e 20 74 68 65 20 77 6f 72 73 74 20 63 61 73  in the worst cas
22bca 65 2e 0a 2a 2a 0a 2a 2a 20 48 69 6e 74 73 3a 20  e..**.** Hints: 
22bcb 74 6f 20 6d 61 74 63 68 20 27 2a 27 20 6f 72 20  to match '*' or 
22bcc 27 3f 27 2c 20 70 75 74 20 74 68 65 6d 20 69 6e  '?', put them in
22bcd 20 22 5b 5d 22 2e 20 20 4c 69 6b 65 20 74 68 69   "[]".  Like thi
22bce 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
22bcf 20 61 62 63 5b 2a 5d 78 79 7a 20 20 20 20 20 20   abc[*]xyz      
22bd0 20 20 4d 61 74 63 68 65 73 20 22 61 62 63 2a 78    Matches "abc*x
22bd1 79 7a 22 20 6f 6e 6c 79 0a 2a 2f 0a 73 74 61 74  yz" only.*/.stat
22bd2 69 63 20 69 6e 74 20 70 61 74 74 65 72 6e 43 6f  ic int patternCo
22bd3 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 20 75  mpare(.  const u
22bd4 38 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 20 20  8 *zPattern,    
22bd5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22bd6 20 67 6c 6f 62 20 70 61 74 74 65 72 6e 20 2a 2f   glob pattern */
22bd7 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 74  .  const u8 *zSt
22bd8 72 69 6e 67 2c 20 20 20 20 20 20 20 20 20 20 20  ring,           
22bd9 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e      /* The strin
22bda 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61  g to compare aga
22bdb 69 6e 73 74 20 74 68 65 20 67 6c 6f 62 20 2a 2f  inst the glob */
22bdc 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  .  const struct 
22bdd 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e  compareInfo *pIn
22bde 66 6f 2c 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69  fo, /* Informati
22bdf 6f 6e 20 61 62 6f 75 74 20 68 6f 77 20 74 6f 20  on about how to 
22be0 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 65 20 2a  do the compare *
22be1 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 65 73  /.  const int es
22be2 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
22be3 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61       /* The esca
22be4 70 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  pe character */.
22be5 29 7b 0a 20 20 69 6e 74 20 63 2c 20 63 32 3b 0a  ){.  int c, c2;.
22be6 20 20 69 6e 74 20 69 6e 76 65 72 74 3b 0a 20 20    int invert;.  
22be7 69 6e 74 20 73 65 65 6e 3b 0a 20 20 75 38 20 6d  int seen;.  u8 m
22be8 61 74 63 68 4f 6e 65 20 3d 20 70 49 6e 66 6f 2d  atchOne = pInfo-
22be9 3e 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38 20  >matchOne;.  u8 
22bea 6d 61 74 63 68 41 6c 6c 20 3d 20 70 49 6e 66 6f  matchAll = pInfo
22beb 2d 3e 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75 38  ->matchAll;.  u8
22bec 20 6d 61 74 63 68 53 65 74 20 3d 20 70 49 6e 66   matchSet = pInf
22bed 6f 2d 3e 6d 61 74 63 68 53 65 74 3b 0a 20 20 75  o->matchSet;.  u
22bee 38 20 6e 6f 43 61 73 65 20 3d 20 70 49 6e 66 6f  8 noCase = pInfo
22bef 2d 3e 6e 6f 43 61 73 65 3b 20 0a 20 20 69 6e 74  ->noCase; .  int
22bf0 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b   prevEscape = 0;
22bf1 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
22bf2 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 61  the previous cha
22bf3 72 61 63 74 65 72 20 77 61 73 20 27 65 73 63 61  racter was 'esca
22bf4 70 65 27 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28  pe' */..  while(
22bf5 20 28 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66   (c = sqlite3Utf
22bf6 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 30  8Read(zPattern,0
22bf7 2c 26 7a 50 61 74 74 65 72 6e 29 29 21 3d 30 20  ,&zPattern))!=0 
22bf8 29 7b 0a 20 20 20 20 69 66 28 20 21 70 72 65 76  ){.    if( !prev
22bf9 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61 74  Escape && c==mat
22bfa 63 68 41 6c 6c 20 29 7b 0a 20 20 20 20 20 20 77  chAll ){.      w
22bfb 68 69 6c 65 28 20 28 63 3d 73 71 6c 69 74 65 33  hile( (c=sqlite3
22bfc 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
22bfd 6e 2c 30 2c 26 7a 50 61 74 74 65 72 6e 29 29 20  n,0,&zPattern)) 
22bfe 3d 3d 20 6d 61 74 63 68 41 6c 6c 0a 20 20 20 20  == matchAll.    
22bff 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 63 20             || c 
22c00 3d 3d 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20  == matchOne ){. 
22c01 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 6d 61         if( c==ma
22c02 74 63 68 4f 6e 65 20 26 26 20 73 71 6c 69 74 65  tchOne && sqlite
22c03 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e  3Utf8Read(zStrin
22c04 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3d  g, 0, &zString)=
22c05 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
22c06 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
22c07 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
22c08 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20    if( c==0 ){.  
22c09 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
22c0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
22c0b 63 3d 3d 65 73 63 20 29 7b 0a 20 20 20 20 20 20  c==esc ){.      
22c0c 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66    c = sqlite3Utf
22c0d 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20  8Read(zPattern, 
22c0e 30 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  0, &zPattern);. 
22c0f 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20         if( c==0 
22c10 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
22c11 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  urn 0;.        }
22c12 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
22c13 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a   c==matchSet ){.
22c14 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
22c15 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 20 20 20  esc==0 );       
22c16 20 20 2f 2a 20 54 68 69 73 20 69 73 20 47 4c 4f    /* This is GLO
22c17 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20  B, not LIKE */. 
22c18 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d         assert( m
22c19 61 74 63 68 53 65 74 3c 30 78 38 30 20 29 3b 20  atchSet<0x80 ); 
22c1a 20 2f 2a 20 27 5b 27 20 69 73 20 61 20 73 69 6e   /* '[' is a sin
22c1b 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63 74  gle-byte charact
22c1c 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68  er */.        wh
22c1d 69 6c 65 28 20 2a 7a 53 74 72 69 6e 67 20 26 26  ile( *zString &&
22c1e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28   patternCompare(
22c1f 26 7a 50 61 74 74 65 72 6e 5b 2d 31 5d 2c 7a 53  &zPattern[-1],zS
22c20 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 63 29  tring,pInfo,esc)
22c21 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
22c22 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
22c23 38 28 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20  8(zString);.    
22c24 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
22c25 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 21 3d 30  turn *zString!=0
22c26 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
22c27 77 68 69 6c 65 28 20 28 63 32 20 3d 20 73 71 6c  while( (c2 = sql
22c28 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74  ite3Utf8Read(zSt
22c29 72 69 6e 67 2c 30 2c 26 7a 53 74 72 69 6e 67 29  ring,0,&zString)
22c2a 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
22c2b 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20  if( noCase ){.  
22c2c 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
22c2d 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20  rToLower(c2);.  
22c2e 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
22c2f 72 54 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20  rToLower(c);.   
22c30 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32         while( c2
22c31 20 21 3d 20 30 20 26 26 20 63 32 20 21 3d 20 63   != 0 && c2 != c
22c32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
22c33 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
22c34 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c  Read(zString, 0,
22c35 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20   &zString);.    
22c36 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
22c37 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20  rToLower(c2);.  
22c38 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22c39 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
22c3a 20 20 20 77 68 69 6c 65 28 20 63 32 20 21 3d 20     while( c2 != 
22c3b 30 20 26 26 20 63 32 20 21 3d 20 63 20 29 7b 0a  0 && c2 != c ){.
22c3c 20 20 20 20 20 20 20 20 20 20 20 20 63 32 20 3d              c2 =
22c3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
22c3e 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53  (zString, 0, &zS
22c3f 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20  tring);.        
22c40 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
22c41 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20        if( c2==0 
22c42 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
22c43 20 20 20 20 69 66 28 20 70 61 74 74 65 72 6e 43      if( patternC
22c44 6f 6d 70 61 72 65 28 7a 50 61 74 74 65 72 6e 2c  ompare(zPattern,
22c45 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73  zString,pInfo,es
22c46 63 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  c) ) return 1;. 
22c47 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
22c48 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  urn 0;.    }else
22c49 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65   if( !prevEscape
22c4a 20 26 26 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20   && c==matchOne 
22c4b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
22c4c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74  ite3Utf8Read(zSt
22c4d 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e  ring, 0, &zStrin
22c4e 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  g)==0 ){.       
22c4f 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
22c50 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
22c51 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a   c==matchSet ){.
22c52 20 20 20 20 20 20 69 6e 74 20 70 72 69 6f 72 5f        int prior_
22c53 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73  c = 0;.      ass
22c54 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b 20 20  ert( esc==0 );  
22c55 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 6f    /* This only o
22c56 63 63 75 72 73 20 66 6f 72 20 47 4c 4f 42 2c 20  ccurs for GLOB, 
22c57 6e 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20  not LIKE */.    
22c58 20 20 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20    seen = 0;.    
22c59 20 20 69 6e 76 65 72 74 20 3d 20 30 3b 0a 20 20    invert = 0;.  
22c5a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
22c5b 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
22c5c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20   0, &zString);. 
22c5d 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20       if( c==0 ) 
22c5e 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
22c5f 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
22c60 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30  Read(zPattern, 0
22c61 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20  , &zPattern);.  
22c62 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5e 27 20      if( c2=='^' 
22c63 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 65 72  ){.        inver
22c64 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63  t = 1;.        c
22c65 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
22c66 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30 2c  ead(zPattern, 0,
22c67 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20   &zPattern);.   
22c68 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63     }.      if( c
22c69 32 3d 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20 20  2==']' ){.      
22c6a 20 20 69 66 28 20 63 3d 3d 27 5d 27 20 29 20 73    if( c==']' ) s
22c6b 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  een = 1;.       
22c6c 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66   c2 = sqlite3Utf
22c6d 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20  8Read(zPattern, 
22c6e 30 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  0, &zPattern);. 
22c6f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69       }.      whi
22c70 6c 65 28 20 63 32 20 26 26 20 63 32 21 3d 27 5d  le( c2 && c2!=']
22c71 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ' ){.        if(
22c72 20 63 32 3d 3d 27 2d 27 20 26 26 20 7a 50 61 74   c2=='-' && zPat
22c73 74 65 72 6e 5b 30 5d 21 3d 27 5d 27 20 26 26 20  tern[0]!=']' && 
22c74 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 30 20 26  zPattern[0]!=0 &
22c75 26 20 70 72 69 6f 72 5f 63 3e 30 20 29 7b 0a 20  & prior_c>0 ){. 
22c76 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71           c2 = sq
22c77 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50  lite3Utf8Read(zP
22c78 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74  attern, 0, &zPat
22c79 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  tern);.         
22c7a 20 69 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63 20   if( c>=prior_c 
22c7b 26 26 20 63 3c 3d 63 32 20 29 20 73 65 65 6e 20  && c<=c2 ) seen 
22c7c 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70  = 1;.          p
22c7d 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20 20  rior_c = 0;.    
22c7e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
22c7f 20 20 20 20 20 69 66 28 20 63 3d 3d 63 32 20 29       if( c==c2 )
22c80 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 65  {.            se
22c81 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  en = 1;.        
22c82 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 72    }.          pr
22c83 69 6f 72 5f 63 20 3d 20 63 32 3b 0a 20 20 20 20  ior_c = c2;.    
22c84 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 32      }.        c2
22c85 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
22c86 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30 2c 20  ad(zPattern, 0, 
22c87 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20  &zPattern);.    
22c88 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 32    }.      if( c2
22c89 3d 3d 30 20 7c 7c 20 28 73 65 65 6e 20 5e 20 69  ==0 || (seen ^ i
22c8a 6e 76 65 72 74 29 3d 3d 30 20 29 7b 0a 20 20 20  nvert)==0 ){.   
22c8b 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
22c8c 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
22c8d 20 69 66 28 20 65 73 63 3d 3d 63 20 26 26 20 21   if( esc==c && !
22c8e 70 72 65 76 45 73 63 61 70 65 20 29 7b 0a 20 20  prevEscape ){.  
22c8f 20 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d      prevEscape =
22c90 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
22c91 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65       c2 = sqlite
22c92 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e  3Utf8Read(zStrin
22c93 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b  g, 0, &zString);
22c94 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73  .      if( noCas
22c95 65 20 29 7b 0a 20 20 20 20 20 20 20 20 47 6c 6f  e ){.        Glo
22c96 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 29  gUpperToLower(c)
22c97 3b 0a 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70  ;.        GlogUp
22c98 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a  perToLower(c2);.
22c99 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
22c9a 28 20 63 21 3d 63 32 20 29 7b 0a 20 20 20 20 20  ( c!=c2 ){.     
22c9b 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
22c9c 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65 76 45     }.      prevE
22c9d 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20 20 7d  scape = 0;.    }
22c9e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  .  }.  return *z
22c9f 53 74 72 69 6e 67 3d 3d 30 3b 0a 7d 0a 0a 2f 2a  String==0;.}../*
22ca0 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75  .** Count the nu
22ca1 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
22ca2 61 74 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72  at the LIKE oper
22ca3 61 74 6f 72 20 28 6f 72 20 47 4c 4f 42 20 77 68  ator (or GLOB wh
22ca4 69 63 68 20 69 73 0a 2a 2a 20 6a 75 73 74 20 61  ich is.** just a
22ca5 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 4c 49   variation of LI
22ca6 4b 45 29 20 67 65 74 73 20 63 61 6c 6c 65 64 2e  KE) gets called.
22ca7 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66    This is used f
22ca8 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 6f 6e  or testing.** on
22ca9 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ly..*/.#ifdef SQ
22caa 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
22cab 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
22cac 5f 6c 69 6b 65 5f 63 6f 75 6e 74 20 3d 20 30 3b  _like_count = 0;
22cad 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
22cae 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
22caf 66 20 74 68 65 20 6c 69 6b 65 28 29 20 53 51 4c  f the like() SQL
22cb0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73   function.  This
22cb1 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d   function implem
22cb2 65 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c  ents.** the buil
22cb3 64 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74  d-in LIKE operat
22cb4 6f 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 61  or.  The first a
22cb5 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66  rgument to the f
22cb6 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a  unction is the.*
22cb7 2a 20 70 61 74 74 65 72 6e 20 61 6e 64 20 74 68  * pattern and th
22cb8 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
22cb9 74 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 2e  t is the string.
22cba 20 20 53 6f 2c 20 74 68 65 20 53 51 4c 20 73 74    So, the SQL st
22cbb 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20  atements:.**.** 
22cbc 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a        A LIKE B.*
22cbd 2a 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e  *.** is implemen
22cbe 74 65 64 20 61 73 20 6c 69 6b 65 28 42 2c 41 29  ted as like(B,A)
22cbf 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 61 6d  ..**.** This sam
22cc0 65 20 66 75 6e 63 74 69 6f 6e 20 28 77 69 74 68  e function (with
22cc1 20 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6d   a different com
22cc2 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75  pareInfo structu
22cc3 72 65 29 20 63 6f 6d 70 75 74 65 73 0a 2a 2a 20  re) computes.** 
22cc4 74 68 65 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f  the GLOB operato
22cc5 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
22cc6 64 20 6c 69 6b 65 46 75 6e 63 28 0a 20 20 73 71  d likeFunc(.  sq
22cc7 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
22cc8 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20 61  ontext, .  int a
22cc9 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  rgc, .  sqlite3_
22cca 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
22ccb 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
22ccc 20 63 68 61 72 20 2a 7a 41 2c 20 2a 7a 42 3b 0a   char *zA, *zB;.
22ccd 20 20 69 6e 74 20 65 73 63 61 70 65 20 3d 20 30    int escape = 0
22cce 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
22ccf 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
22cd0 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
22cd1 65 78 74 29 3b 0a 0a 20 20 7a 42 20 3d 20 73 71  ext);..  zB = sq
22cd2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
22cd3 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 41 20  (argv[0]);.  zA 
22cd4 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
22cd5 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a  text(argv[1]);..
22cd6 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c    /* Limit the l
22cd7 65 6e 67 74 68 20 6f 66 20 74 68 65 20 4c 49 4b  ength of the LIK
22cd8 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72  E or GLOB patter
22cd9 6e 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c  n to avoid probl
22cda 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70  ems.  ** of deep
22cdb 20 72 65 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e   recursion and N
22cdc 2a 4e 20 62 65 68 61 76 69 6f 72 20 69 6e 20 70  *N behavior in p
22cdd 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e  atternCompare().
22cde 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
22cdf 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
22ce0 61 72 67 76 5b 30 5d 29 20 3e 0a 20 20 20 20 20  argv[0]) >.     
22ce1 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51     db->aLimit[SQ
22ce2 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f  LITE_LIMIT_LIKE_
22ce3 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20  PATTERN_LENGTH] 
22ce4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
22ce5 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
22ce6 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47 4c  ext, "LIKE or GL
22ce7 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20 63  OB pattern too c
22ce8 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20 20  omplex", -1);.  
22ce9 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
22cea 61 73 73 65 72 74 28 20 7a 42 3d 3d 73 71 6c 69  assert( zB==sqli
22ceb 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
22cec 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 45  rgv[0]) );  /* E
22ced 6e 63 6f 64 69 6e 67 20 64 69 64 20 6e 6f 74 20  ncoding did not 
22cee 63 68 61 6e 67 65 20 2a 2f 0a 0a 20 20 69 66 28  change */..  if(
22cef 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20   argc==3 ){.    
22cf0 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 68  /* The escape ch
22cf1 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 6d  aracter string m
22cf2 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 61  ust consist of a
22cf3 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68   single UTF-8 ch
22cf4 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20  aracter..    ** 
22cf5 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
22cf6 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20  n an error..    
22cf7 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  */.    const uns
22cf8 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 73 63  igned char *zEsc
22cf9 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
22cfa 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a  _text(argv[2]);.
22cfb 20 20 20 20 69 66 28 20 7a 45 73 63 3d 3d 30 20      if( zEsc==0 
22cfc 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66  ) return;.    if
22cfd 28 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61  ( sqlite3Utf8Cha
22cfe 72 4c 65 6e 28 28 63 68 61 72 2a 29 7a 45 73 63  rLen((char*)zEsc
22cff 2c 20 2d 31 29 21 3d 31 20 29 7b 0a 20 20 20 20  , -1)!=1 ){.    
22d00 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
22d01 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
22d02 0a 20 20 20 20 20 20 20 20 20 20 22 45 53 43 41  .          "ESCA
22d03 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75  PE expression mu
22d04 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63  st be a single c
22d05 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b 0a  haracter", -1);.
22d06 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
22d07 20 20 7d 0a 20 20 20 20 65 73 63 61 70 65 20 3d    }.    escape =
22d08 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
22d09 28 7a 45 73 63 2c 20 30 2c 20 26 7a 45 73 63 29  (zEsc, 0, &zEsc)
22d0a 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 41 20 26  ;.  }.  if( zA &
22d0b 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 74 72 75  & zB ){.    stru
22d0c 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a  ct compareInfo *
22d0d 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 5f  pInfo = sqlite3_
22d0e 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
22d0f 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  t);.#ifdef SQLIT
22d10 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74  E_TEST.    sqlit
22d11 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b 3b  e3_like_count++;
22d12 0a 23 65 6e 64 69 66 0a 20 20 20 20 0a 20 20 20  .#endif.    .   
22d13 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
22d14 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 61 74  int(context, pat
22d15 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 42 2c 20  ternCompare(zB, 
22d16 7a 41 2c 20 70 49 6e 66 6f 2c 20 65 73 63 61 70  zA, pInfo, escap
22d17 65 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  e));.  }.}../*.*
22d18 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
22d19 20 6f 66 20 74 68 65 20 4e 55 4c 4c 49 46 28 78   of the NULLIF(x
22d1a 2c 79 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  ,y) function.  T
22d1b 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68 65  he result is the
22d1c 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65   first.** argume
22d1d 6e 74 20 69 66 20 74 68 65 20 61 72 67 75 6d 65  nt if the argume
22d1e 6e 74 73 20 61 72 65 20 64 69 66 66 65 72 65 6e  nts are differen
22d1f 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69  t.  The result i
22d20 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 0a 2a 2a  s NULL if the.**
22d21 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 65   arguments are e
22d22 71 75 61 6c 20 74 6f 20 65 61 63 68 20 6f 74 68  qual to each oth
22d23 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
22d24 69 64 20 6e 75 6c 6c 69 66 46 75 6e 63 28 0a 20  id nullifFunc(. 
22d25 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
22d26 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
22d27 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
22d28 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
22d29 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  .){.  CollSeq *p
22d2a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65  Coll = sqlite3Ge
22d2b 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e  tFuncCollSeq(con
22d2c 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f  text);.  UNUSED_
22d2d 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
22d2e 64 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  d);.  if( sqlite
22d2f 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 67 76  3MemCompare(argv
22d30 5b 30 5d 2c 20 61 72 67 76 5b 31 5d 2c 20 70 43  [0], argv[1], pC
22d31 6f 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  oll)!=0 ){.    s
22d32 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
22d33 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  lue(context, arg
22d34 76 5b 30 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  v[0]);.  }.}../*
22d35 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
22d36 6f 6e 20 6f 66 20 74 68 65 20 56 45 52 53 49 4f  on of the VERSIO
22d37 4e 28 2a 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  N(*) function.  
22d38 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68  The result is th
22d39 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20  e version.** of 
22d3a 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
22d3b 72 79 20 74 68 61 74 20 69 73 20 72 75 6e 6e 69  ry that is runni
22d3c 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
22d3d 69 64 20 76 65 72 73 69 6f 6e 46 75 6e 63 28 0a  id versionFunc(.
22d3e 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
22d3f 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
22d40 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
22d41 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
22d42 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45  Used2.){.  UNUSE
22d43 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
22d44 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
22d45 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
22d46 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
22d47 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 2c  sqlite3_version,
22d48 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54   -1, SQLITE_STAT
22d49 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 72 72 61 79  IC);.}../* Array
22d4a 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20   for converting 
22d4b 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74 65 73 20  from half-bytes 
22d4c 28 6e 79 62 62 6c 65 73 29 20 69 6e 74 6f 20 41  (nybbles) into A
22d4d 53 43 49 49 20 68 65 78 0a 2a 2a 20 64 69 67 69  SCII hex.** digi
22d4e 74 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f  ts. */.static co
22d4f 6e 73 74 20 63 68 61 72 20 68 65 78 64 69 67 69  nst char hexdigi
22d50 74 73 5b 5d 20 3d 20 7b 0a 20 20 27 30 27 2c 20  ts[] = {.  '0', 
22d51 27 31 27 2c 20 27 32 27 2c 20 27 33 27 2c 20 27  '1', '2', '3', '
22d52 34 27 2c 20 27 35 27 2c 20 27 36 27 2c 20 27 37  4', '5', '6', '7
22d53 27 2c 0a 20 20 27 38 27 2c 20 27 39 27 2c 20 27  ',.  '8', '9', '
22d54 41 27 2c 20 27 42 27 2c 20 27 43 27 2c 20 27 44  A', 'B', 'C', 'D
22d55 27 2c 20 27 45 27 2c 20 27 46 27 20 0a 7d 3b 0a  ', 'E', 'F' .};.
22d56 0a 2f 2a 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  ./*.** EXPERIMEN
22d57 54 41 4c 20 2d 20 54 68 69 73 20 69 73 20 6e 6f  TAL - This is no
22d58 74 20 61 6e 20 6f 66 66 69 63 69 61 6c 20 66 75  t an official fu
22d59 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 69 6e 74  nction.  The int
22d5a 65 72 66 61 63 65 20 6d 61 79 0a 2a 2a 20 63 68  erface may.** ch
22d5b 61 6e 67 65 2e 20 20 54 68 69 73 20 66 75 6e 63  ange.  This func
22d5c 74 69 6f 6e 20 6d 61 79 20 64 69 73 61 70 70 65  tion may disappe
22d5d 61 72 2e 20 20 44 6f 20 6e 6f 74 20 77 72 69 74  ar.  Do not writ
22d5e 65 20 63 6f 64 65 20 74 68 61 74 20 64 65 70 65  e code that depe
22d5f 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 69 73 20 66  nds.** on this f
22d60 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  unction..**.** I
22d61 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
22d62 20 74 68 65 20 51 55 4f 54 45 28 29 20 66 75 6e   the QUOTE() fun
22d63 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e  ction.  This fun
22d64 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ction takes a si
22d65 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  ngle.** argument
22d66 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d 65  .  If the argume
22d67 6e 74 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 74  nt is numeric, t
22d68 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
22d69 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 0a 2a  is the same as.*
22d6a 2a 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 20  * the argument. 
22d6b 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   If the argument
22d6c 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
22d6d 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68  turn value is th
22d6e 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 4e 55 4c  e string.** "NUL
22d6f 4c 22 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20  L".  Otherwise, 
22d70 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  the argument is 
22d71 65 6e 63 6c 6f 73 65 64 20 69 6e 20 73 69 6e 67  enclosed in sing
22d72 6c 65 20 71 75 6f 74 65 73 20 77 69 74 68 0a 2a  le quotes with.*
22d73 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 20 65  * single-quote e
22d74 73 63 61 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  scapes..*/.stati
22d75 63 20 76 6f 69 64 20 71 75 6f 74 65 46 75 6e 63  c void quoteFunc
22d76 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
22d77 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61   *context, int a
22d78 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
22d79 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 69 66  ue **argv){.  if
22d7a 28 20 61 72 67 63 3c 31 20 29 20 72 65 74 75 72  ( argc<1 ) retur
22d7b 6e 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c  n;.  switch( sql
22d7c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
22d7d 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20  argv[0]) ){.    
22d7e 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  case SQLITE_NULL
22d7f 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
22d80 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
22d81 6e 74 65 78 74 2c 20 22 4e 55 4c 4c 22 2c 20 34  ntext, "NULL", 4
22d82 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
22d83 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
22d84 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
22d85 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20  LITE_INTEGER:.  
22d86 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c    case SQLITE_FL
22d87 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  OAT: {.      sql
22d88 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
22d89 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b  e(context, argv[
22d8a 30 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0]);.      break
22d8b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
22d8c 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a   SQLITE_BLOB: {.
22d8d 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 65 78        char *zTex
22d8e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 68 61  t = 0;.      cha
22d8f 72 20 63 6f 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d  r const *zBlob =
22d90 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
22d91 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  lob(argv[0]);.  
22d92 20 20 20 20 69 6e 74 20 6e 42 6c 6f 62 20 3d 20      int nBlob = 
22d93 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
22d94 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
22d95 20 20 20 20 61 73 73 65 72 74 28 20 7a 42 6c 6f      assert( zBlo
22d96 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  b==sqlite3_value
22d97 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20 29  _blob(argv[0]) )
22d98 3b 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67  ; /* No encoding
22d99 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20   change */.     
22d9a 20 7a 54 65 78 74 20 3d 20 28 63 68 61 72 20 2a   zText = (char *
22d9b 29 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63  )contextMalloc(c
22d9c 6f 6e 74 65 78 74 2c 20 28 32 2a 28 69 36 34 29  ontext, (2*(i64)
22d9d 6e 42 6c 6f 62 29 2b 34 29 3b 20 0a 20 20 20 20  nBlob)+4); .    
22d9e 20 20 69 66 28 20 7a 54 65 78 74 20 29 7b 0a 20    if( zText ){. 
22d9f 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20         int i;.  
22da0 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
22da1 3c 6e 42 6c 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20  <nBlob; i++){.  
22da2 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 69          zText[(i
22da3 2a 32 29 2b 32 5d 20 3d 20 68 65 78 64 69 67 69  *2)+2] = hexdigi
22da4 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29  ts[(zBlob[i]>>4)
22da5 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20  &0x0F];.        
22da6 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b 33 5d    zText[(i*2)+3]
22da7 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42   = hexdigits[(zB
22da8 6c 6f 62 5b 69 5d 29 26 30 78 30 46 5d 3b 0a 20  lob[i])&0x0F];. 
22da9 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
22daa 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29   zText[(nBlob*2)
22dab 2b 32 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20  +2] = '\'';.    
22dac 20 20 20 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62      zText[(nBlob
22dad 2a 32 29 2b 33 5d 20 3d 20 27 5c 30 27 3b 0a 20  *2)+3] = '\0';. 
22dae 20 20 20 20 20 20 20 7a 54 65 78 74 5b 30 5d 20         zText[0] 
22daf 3d 20 27 58 27 3b 0a 20 20 20 20 20 20 20 20 7a  = 'X';.        z
22db0 54 65 78 74 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a  Text[1] = '\'';.
22db1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
22db2 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
22db3 65 78 74 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20  ext, zText, -1, 
22db4 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
22db5 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
22db6 65 33 5f 66 72 65 65 28 7a 54 65 78 74 29 3b 0a  e3_free(zText);.
22db7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
22db8 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
22db9 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a  ase SQLITE_TEXT:
22dba 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 6a   {.      int i,j
22dbb 3b 0a 20 20 20 20 20 20 75 36 34 20 6e 3b 0a 20  ;.      u64 n;. 
22dbc 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67       const unsig
22dbd 6e 65 64 20 63 68 61 72 20 2a 7a 41 72 67 20 3d  ned char *zArg =
22dbe 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
22dbf 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
22dc0 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20      char *z;..  
22dc1 20 20 20 20 69 66 28 20 7a 41 72 67 3d 3d 30 20      if( zArg==0 
22dc2 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20  ) return;.      
22dc3 66 6f 72 28 69 3d 30 2c 20 6e 3d 30 3b 20 7a 41  for(i=0, n=0; zA
22dc4 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 20 69 66 28  rg[i]; i++){ if(
22dc5 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29   zArg[i]=='\'' )
22dc6 20 6e 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 7a 20   n++; }.      z 
22dc7 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28  = contextMalloc(
22dc8 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 69  context, ((i64)i
22dc9 29 2b 28 28 69 36 34 29 6e 29 2b 33 29 3b 0a 20  )+((i64)n)+3);. 
22dca 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20       if( z ){.  
22dcb 20 20 20 20 20 20 7a 5b 30 5d 20 3d 20 27 5c 27        z[0] = '\'
22dcc 27 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ';.        for(i
22dcd 3d 30 2c 20 6a 3d 31 3b 20 7a 41 72 67 5b 69 5d  =0, j=1; zArg[i]
22dce 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
22dcf 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 41 72 67 5b    z[j++] = zArg[
22dd0 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  i];.          if
22dd1 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 20  ( zArg[i]=='\'' 
22dd2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a  ){.            z
22dd3 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20  [j++] = '\'';.  
22dd4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22dd5 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b    }.        z[j+
22dd6 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20  +] = '\'';.     
22dd7 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20     z[j] = 0;.   
22dd8 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
22dd9 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
22dda 2c 20 7a 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f  , z, j, sqlite3_
22ddb 66 72 65 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  free);.      }. 
22ddc 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
22ddd 2a 20 54 68 65 20 68 65 78 28 29 20 66 75 6e 63  * The hex() func
22dde 74 69 6f 6e 2e 20 20 49 6e 74 65 72 70 72 65 74  tion.  Interpret
22ddf 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 61 73   the argument as
22de0 20 61 20 62 6c 6f 62 2e 20 20 52 65 74 75 72 6e   a blob.  Return
22de1 0a 2a 2a 20 61 20 68 65 78 61 64 65 63 69 6d 61  .** a hexadecima
22de2 6c 20 72 65 6e 64 65 72 69 6e 67 20 61 73 20 74  l rendering as t
22de3 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ext..*/.static v
22de4 6f 69 64 20 68 65 78 46 75 6e 63 28 0a 20 20 73  oid hexFunc(.  s
22de5 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
22de6 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
22de7 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
22de8 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
22de9 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 63 6f 6e   int i, n;.  con
22dea 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
22deb 20 2a 70 42 6c 6f 62 3b 0a 20 20 63 68 61 72 20   *pBlob;.  char 
22dec 2a 7a 48 65 78 2c 20 2a 7a 3b 0a 20 20 61 73 73  *zHex, *z;.  ass
22ded 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a  ert( argc==1 );.
22dee 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
22def 45 52 28 61 72 67 63 29 3b 0a 20 20 70 42 6c 6f  ER(argc);.  pBlo
22df0 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  b = sqlite3_valu
22df1 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b  e_blob(argv[0]);
22df2 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
22df3 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
22df4 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0]);.  assert( p
22df5 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61  Blob==sqlite3_va
22df6 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
22df7 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f  ) );  /* No enco
22df8 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20  ding change */. 
22df9 20 7a 20 3d 20 7a 48 65 78 20 3d 20 63 6f 6e 74   z = zHex = cont
22dfa 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78  extMalloc(contex
22dfb 74 2c 20 28 28 69 36 34 29 6e 29 2a 32 20 2b 20  t, ((i64)n)*2 + 
22dfc 31 29 3b 0a 20 20 69 66 28 20 7a 48 65 78 20 29  1);.  if( zHex )
22dfd 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
22dfe 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b 2b  <n; i++, pBlob++
22dff 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  ){.      unsigne
22e00 64 20 63 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f  d char c = *pBlo
22e01 62 3b 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20  b;.      *(z++) 
22e02 3d 20 68 65 78 64 69 67 69 74 73 5b 28 63 3e 3e  = hexdigits[(c>>
22e03 34 29 26 30 78 66 5d 3b 0a 20 20 20 20 20 20 2a  4)&0xf];.      *
22e04 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74  (z++) = hexdigit
22e05 73 5b 63 26 30 78 66 5d 3b 0a 20 20 20 20 7d 0a  s[c&0xf];.    }.
22e06 20 20 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20      *z = 0;.    
22e07 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
22e08 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65  ext(context, zHe
22e09 78 2c 20 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f  x, n*2, sqlite3_
22e0a 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  free);.  }.}../*
22e0b 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 62 6c 6f 62  .** The zeroblob
22e0c 28 4e 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  (N) function ret
22e0d 75 72 6e 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c  urns a zero-fill
22e0e 65 64 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 20  ed blob of size 
22e0f 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  N bytes..*/.stat
22e10 69 63 20 76 6f 69 64 20 7a 65 72 6f 62 6c 6f 62  ic void zeroblob
22e11 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
22e12 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
22e13 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
22e14 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
22e15 61 72 67 76 0a 29 7b 0a 20 20 69 36 34 20 6e 3b  argv.){.  i64 n;
22e16 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
22e17 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =1 );.  UNUSED_P
22e18 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a  ARAMETER(argc);.
22e19 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    n = sqlite3_va
22e1a 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
22e1b 5d 29 3b 0a 20 20 69 66 28 20 6e 3e 53 51 4c 49  ]);.  if( n>SQLI
22e1c 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 7b  TE_MAX_LENGTH ){
22e1d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
22e1e 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
22e1f 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 65 6c  (context);.  }el
22e20 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
22e21 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28  result_zeroblob(
22e22 63 6f 6e 74 65 78 74 2c 20 28 69 6e 74 29 6e 29  context, (int)n)
22e23 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
22e24 68 65 20 72 65 70 6c 61 63 65 28 29 20 66 75 6e  he replace() fun
22e25 63 74 69 6f 6e 2e 20 20 54 68 72 65 65 20 61 72  ction.  Three ar
22e26 67 75 6d 65 6e 74 73 20 61 72 65 20 61 6c 6c 20  guments are all 
22e27 73 74 72 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a 2a  strings: call.**
22e28 20 74 68 65 6d 20 41 2c 20 42 2c 20 61 6e 64 20   them A, B, and 
22e29 43 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69 73  C. The result is
22e2a 20 61 6c 73 6f 20 61 20 73 74 72 69 6e 67 20 77   also a string w
22e2b 68 69 63 68 20 69 73 20 64 65 72 69 76 65 64 0a  hich is derived.
22e2c 2a 2a 20 66 72 6f 6d 20 41 20 62 79 20 72 65 70  ** from A by rep
22e2d 6c 61 63 69 6e 67 20 65 76 65 72 79 20 6f 63 63  lacing every occ
22e2e 75 72 61 6e 63 65 20 6f 66 20 42 20 77 69 74 68  urance of B with
22e2f 20 43 2e 20 20 54 68 65 20 6d 61 74 63 68 0a 2a   C.  The match.*
22e30 2a 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 2e  * must be exact.
22e31 20 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75    Collating sequ
22e32 65 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 75 73  ences are not us
22e33 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
22e34 69 64 20 72 65 70 6c 61 63 65 46 75 6e 63 28 0a  id replaceFunc(.
22e35 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
22e36 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
22e37 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
22e38 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
22e39 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
22e3a 65 64 20 63 68 61 72 20 2a 7a 53 74 72 3b 20 20  ed char *zStr;  
22e3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70        /* The inp
22e3c 75 74 20 73 74 72 69 6e 67 20 41 20 2a 2f 0a 20  ut string A */. 
22e3d 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
22e3e 63 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 3b 20  char *zPattern; 
22e3f 20 20 20 2f 2a 20 54 68 65 20 70 61 74 74 65 72     /* The patter
22e40 6e 20 73 74 72 69 6e 67 20 42 20 2a 2f 0a 20 20  n string B */.  
22e41 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
22e42 68 61 72 20 2a 7a 52 65 70 3b 20 20 20 20 20 20  har *zRep;      
22e43 20 20 2f 2a 20 54 68 65 20 72 65 70 6c 61 63 65    /* The replace
22e44 6d 65 6e 74 20 73 74 72 69 6e 67 20 43 20 2a 2f  ment string C */
22e45 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
22e46 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20   *zOut;         
22e47 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74 70       /* The outp
22e48 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74 72  ut */.  int nStr
22e49 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22e4a 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 53 74 72   /* Size of zStr
22e4b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 74 65   */.  int nPatte
22e4c 72 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  rn;            /
22e4d 2a 20 53 69 7a 65 20 6f 66 20 7a 50 61 74 74 65  * Size of zPatte
22e4e 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 70  rn */.  int nRep
22e4f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22e50 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 52 65 70   /* Size of zRep
22e51 20 2a 2f 0a 20 20 69 36 34 20 6e 4f 75 74 3b 20   */.  i64 nOut; 
22e52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22e53 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f  * Maximum size o
22e54 66 20 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20  f zOut */.  int 
22e55 6c 6f 6f 70 4c 69 6d 69 74 3b 20 20 20 20 20 20  loopLimit;      
22e56 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 7a 53 74       /* Last zSt
22e57 72 5b 5d 20 74 68 61 74 20 6d 69 67 68 74 20 6d  r[] that might m
22e58 61 74 63 68 20 7a 50 61 74 74 65 72 6e 5b 5d 20  atch zPattern[] 
22e59 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  */.  int i, j;  
22e5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22e5b 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
22e5c 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  /..  assert( arg
22e5d 63 3d 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==3 );.  UNUSED
22e5e 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
22e5f 3b 0a 20 20 7a 53 74 72 20 3d 20 73 71 6c 69 74  ;.  zStr = sqlit
22e60 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
22e61 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 53  gv[0]);.  if( zS
22e62 74 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  tr==0 ) return;.
22e63 20 20 6e 53 74 72 20 3d 20 73 71 6c 69 74 65 33    nStr = sqlite3
22e64 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
22e65 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28  v[0]);.  assert(
22e66 20 7a 53 74 72 3d 3d 73 71 6c 69 74 65 33 5f 76   zStr==sqlite3_v
22e67 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
22e68 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63  ]) );  /* No enc
22e69 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a  oding change */.
22e6a 20 20 7a 50 61 74 74 65 72 6e 20 3d 20 73 71 6c    zPattern = sql
22e6b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
22e6c 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20  argv[1]);.  if( 
22e6d 7a 50 61 74 74 65 72 6e 3d 3d 30 20 7c 7c 20 7a  zPattern==0 || z
22e6e 50 61 74 74 65 72 6e 5b 30 5d 3d 3d 30 20 29 20  Pattern[0]==0 ) 
22e6f 72 65 74 75 72 6e 3b 0a 20 20 6e 50 61 74 74 65  return;.  nPatte
22e70 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  rn = sqlite3_val
22e71 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d  ue_bytes(argv[1]
22e72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 50 61  );.  assert( zPa
22e73 74 74 65 72 6e 3d 3d 73 71 6c 69 74 65 33 5f 76  ttern==sqlite3_v
22e74 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
22e75 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63  ]) );  /* No enc
22e76 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a  oding change */.
22e77 20 20 7a 52 65 70 20 3d 20 73 71 6c 69 74 65 33    zRep = sqlite3
22e78 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
22e79 5b 32 5d 29 3b 0a 20 20 69 66 28 20 7a 52 65 70  [2]);.  if( zRep
22e7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
22e7b 6e 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76  nRep = sqlite3_v
22e7c 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
22e7d 32 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  2]);.  assert( z
22e7e 52 65 70 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c  Rep==sqlite3_val
22e7f 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29  ue_text(argv[2])
22e80 20 29 3b 0a 20 20 6e 4f 75 74 20 3d 20 6e 53 74   );.  nOut = nSt
22e81 72 20 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28  r + 1;.  assert(
22e82 20 6e 4f 75 74 3c 53 51 4c 49 54 45 5f 4d 41 58   nOut<SQLITE_MAX
22e83 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 7a 4f 75  _LENGTH );.  zOu
22e84 74 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  t = contextMallo
22e85 63 28 63 6f 6e 74 65 78 74 2c 20 28 69 36 34 29  c(context, (i64)
22e86 6e 4f 75 74 29 3b 0a 20 20 69 66 28 20 7a 4f 75  nOut);.  if( zOu
22e87 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  t==0 ){.    retu
22e88 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f 6f 70 4c 69  rn;.  }.  loopLi
22e89 6d 69 74 20 3d 20 6e 53 74 72 20 2d 20 6e 50 61  mit = nStr - nPa
22e8a 74 74 65 72 6e 3b 20 20 0a 20 20 66 6f 72 28 69  ttern;  .  for(i
22e8b 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f 70 4c 69 6d  =j=0; i<=loopLim
22e8c 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  it; i++){.    if
22e8d 28 20 7a 53 74 72 5b 69 5d 21 3d 7a 50 61 74 74  ( zStr[i]!=zPatt
22e8e 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65 6d 63 6d 70  ern[0] || memcmp
22e8f 28 26 7a 53 74 72 5b 69 5d 2c 20 7a 50 61 74 74  (&zStr[i], zPatt
22e90 65 72 6e 2c 20 6e 50 61 74 74 65 72 6e 29 20 29  ern, nPattern) )
22e91 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 2b 2b  {.      zOut[j++
22e92 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b 0a 20 20 20  ] = zStr[i];.   
22e93 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 38   }else{.      u8
22e94 20 2a 7a 4f 6c 64 3b 0a 20 20 20 20 20 20 73 71   *zOld;.      sq
22e95 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
22e96 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
22e97 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
22e98 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20 6e 52        nOut += nR
22e99 65 70 20 2d 20 6e 50 61 74 74 65 72 6e 3b 0a 20  ep - nPattern;. 
22e9a 20 20 20 20 20 69 66 28 20 6e 4f 75 74 3e 3d 64       if( nOut>=d
22e9b 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
22e9c 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
22e9d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
22e9e 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
22e9f 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a  oobig(context);.
22ea0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
22ea1 62 46 72 65 65 28 64 62 2c 20 7a 4f 75 74 29 3b  bFree(db, zOut);
22ea2 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b  .        return;
22ea3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a  .      }.      z
22ea4 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20 20 20 20  Old = zOut;.    
22ea5 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33    zOut = sqlite3
22ea6 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74 2c 20 28  _realloc(zOut, (
22ea7 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 20 20 20  int)nOut);.     
22ea8 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a   if( zOut==0 ){.
22ea9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
22eaa 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
22eab 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
22eac 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
22ead 65 65 28 64 62 2c 20 7a 4f 6c 64 29 3b 0a 20 20  ee(db, zOld);.  
22eae 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
22eaf 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63      }.      memc
22eb0 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65  py(&zOut[j], zRe
22eb1 70 2c 20 6e 52 65 70 29 3b 0a 20 20 20 20 20 20  p, nRep);.      
22eb2 6a 20 2b 3d 20 6e 52 65 70 3b 0a 20 20 20 20 20  j += nRep;.     
22eb3 20 69 20 2b 3d 20 6e 50 61 74 74 65 72 6e 2d 31   i += nPattern-1
22eb4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73  ;.    }.  }.  as
22eb5 73 65 72 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31  sert( j+nStr-i+1
22eb6 3d 3d 6e 4f 75 74 20 29 3b 0a 20 20 6d 65 6d 63  ==nOut );.  memc
22eb7 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53  py(&zOut[j], &zS
22eb8 74 72 5b 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a  tr[i], nStr-i);.
22eb9 20 20 6a 20 2b 3d 20 6e 53 74 72 20 2d 20 69 3b    j += nStr - i;
22eba 0a 20 20 61 73 73 65 72 74 28 20 6a 3c 3d 6e 4f  .  assert( j<=nO
22ebb 75 74 20 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20  ut );.  zOut[j] 
22ebc 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  = 0;.  sqlite3_r
22ebd 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
22ebe 78 74 2c 20 28 63 68 61 72 2a 29 7a 4f 75 74 2c  xt, (char*)zOut,
22ebf 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65   j, sqlite3_free
22ec0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
22ec1 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
22ec2 65 20 54 52 49 4d 28 29 2c 20 4c 54 52 49 4d 28  e TRIM(), LTRIM(
22ec3 29 2c 20 61 6e 64 20 52 54 52 49 4d 28 29 20 66  ), and RTRIM() f
22ec4 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65  unctions..** The
22ec5 20 75 73 65 72 64 61 74 61 20 69 73 20 30 78 31   userdata is 0x1
22ec6 20 66 6f 72 20 6c 65 66 74 20 74 72 69 6d 2c 20   for left trim, 
22ec7 30 78 32 20 66 6f 72 20 72 69 67 68 74 20 74 72  0x2 for right tr
22ec8 69 6d 2c 20 30 78 33 20 66 6f 72 20 62 6f 74 68  im, 0x3 for both
22ec9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
22eca 20 74 72 69 6d 46 75 6e 63 28 0a 20 20 73 71 6c   trimFunc(.  sql
22ecb 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
22ecc 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
22ecd 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
22ece 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
22ecf 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
22ed0 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20  ar *zIn;        
22ed1 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67   /* Input string
22ed2 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   */.  const unsi
22ed3 67 6e 65 64 20 63 68 61 72 20 2a 7a 43 68 61 72  gned char *zChar
22ed4 53 65 74 3b 20 20 20 20 2f 2a 20 53 65 74 20 6f  Set;    /* Set o
22ed5 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20  f characters to 
22ed6 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 49  trim */.  int nI
22ed7 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
22ed8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
22ed9 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
22eda 6e 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74  n input */.  int
22edb 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
22edc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22edd 2a 20 31 3a 20 74 72 69 6d 6c 65 66 74 20 20 32  * 1: trimleft  2
22ede 3a 20 74 72 69 6d 72 69 67 68 74 20 20 33 3a 20  : trimright  3: 
22edf 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  trim */.  int i;
22ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22ee1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
22ee2 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
22ee3 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
22ee4 61 4c 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  aLen = 0;       
22ee5 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
22ee6 65 61 63 68 20 63 68 61 72 61 63 74 65 72 20 69  each character i
22ee7 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20  n zCharSet */.  
22ee8 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a  unsigned char **
22ee9 61 7a 43 68 61 72 20 3d 20 30 3b 20 20 20 20 20  azChar = 0;     
22eea 20 20 2f 2a 20 49 6e 64 69 76 69 64 75 61 6c 20    /* Individual 
22eeb 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43  characters in zC
22eec 68 61 72 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20  harSet */.  int 
22eed 6e 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20  nChar;          
22eee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22eef 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
22ef0 63 74 65 72 73 20 69 6e 20 7a 43 68 61 72 53 65  cters in zCharSe
22ef1 74 20 2a 2f 0a 0a 20 20 69 66 28 20 73 71 6c 69  t */..  if( sqli
22ef2 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
22ef3 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[0])==SQLITE_
22ef4 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74 75  NULL ){.    retu
22ef5 72 6e 3b 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d 20  rn;.  }.  zIn = 
22ef6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
22ef7 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  xt(argv[0]);.  i
22ef8 66 28 20 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75  f( zIn==0 ) retu
22ef9 72 6e 3b 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69  rn;.  nIn = sqli
22efa 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
22efb 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65  argv[0]);.  asse
22efc 72 74 28 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33  rt( zIn==sqlite3
22efd 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
22efe 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 61 72  [0]) );.  if( ar
22eff 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20 73 74 61  gc==1 ){.    sta
22f00 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
22f01 65 64 20 63 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d  ed char lenOne[]
22f02 20 3d 20 7b 20 31 20 7d 3b 0a 20 20 20 20 73 74   = { 1 };.    st
22f03 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20 63 68  atic unsigned ch
22f04 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 4f 6e 65  ar * const azOne
22f05 5b 5d 20 3d 20 7b 20 28 75 38 2a 29 22 20 22 20  [] = { (u8*)" " 
22f06 7d 3b 0a 20 20 20 20 6e 43 68 61 72 20 3d 20 31  };.    nChar = 1
22f07 3b 0a 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 38  ;.    aLen = (u8
22f08 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a  *)lenOne;.    az
22f09 43 68 61 72 20 3d 20 28 75 6e 73 69 67 6e 65 64  Char = (unsigned
22f0a 20 63 68 61 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a   char **)azOne;.
22f0b 20 20 20 20 7a 43 68 61 72 53 65 74 20 3d 20 30      zCharSet = 0
22f0c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 7a  ;.  }else if( (z
22f0d 43 68 61 72 53 65 74 20 3d 20 73 71 6c 69 74 65  CharSet = sqlite
22f0e 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
22f0f 76 5b 31 5d 29 29 3d 3d 30 20 29 7b 0a 20 20 20  v[1]))==0 ){.   
22f10 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65   return;.  }else
22f11 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69  {.    const unsi
22f12 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20  gned char *z;.  
22f13 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74    for(z=zCharSet
22f14 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e  , nChar=0; *z; n
22f15 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 53  Char++){.      S
22f16 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
22f17 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  z);.    }.    if
22f18 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20 20  ( nChar>0 ){.   
22f19 20 20 20 61 7a 43 68 61 72 20 3d 20 63 6f 6e 74     azChar = cont
22f1a 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78  extMalloc(contex
22f1b 74 2c 20 28 28 69 36 34 29 6e 43 68 61 72 29 2a  t, ((i64)nChar)*
22f1c 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31  (sizeof(char*)+1
22f1d 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61 7a  ));.      if( az
22f1e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Char==0 ){.     
22f1f 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
22f20 20 7d 0a 20 20 20 20 20 20 61 4c 65 6e 20 3d 20   }.      aLen = 
22f21 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
22f22 26 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a  &azChar[nChar];.
22f23 20 20 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61        for(z=zCha
22f24 72 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a  rSet, nChar=0; *
22f25 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20  z; nChar++){.   
22f26 20 20 20 20 20 61 7a 43 68 61 72 5b 6e 43 68 61       azChar[nCha
22f27 72 5d 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  r] = (unsigned c
22f28 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 20 20 20  har *)z;.       
22f29 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
22f2a 38 28 7a 29 3b 0a 20 20 20 20 20 20 20 20 61 4c  8(z);.        aL
22f2b 65 6e 5b 6e 43 68 61 72 5d 20 3d 20 28 75 38 29  en[nChar] = (u8)
22f2c 28 7a 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61  (z - azChar[nCha
22f2d 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  r]);.      }.   
22f2e 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43 68   }.  }.  if( nCh
22f2f 61 72 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61 67  ar>0 ){.    flag
22f30 73 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54  s = SQLITE_PTR_T
22f31 4f 5f 49 4e 54 28 73 71 6c 69 74 65 33 5f 75 73  O_INT(sqlite3_us
22f32 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
22f33 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  );.    if( flags
22f34 20 26 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68   & 1 ){.      wh
22f35 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20  ile( nIn>0 ){.  
22f36 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20        int len = 
22f37 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  0;.        for(i
22f38 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b  =0; i<nChar; i++
22f39 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e  ){.          len
22f3a 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20   = aLen[i];.    
22f3b 20 20 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70        if( memcmp
22f3c 28 7a 49 6e 2c 20 61 7a 43 68 61 72 5b 69 5d 2c  (zIn, azChar[i],
22f3d 20 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b   len)==0 ) break
22f3e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
22f3f 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61 72      if( i>=nChar
22f40 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
22f41 20 20 7a 49 6e 20 2b 3d 20 6c 65 6e 3b 0a 20 20    zIn += len;.  
22f42 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e        nIn -= len
22f43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
22f44 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
22f45 32 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65  2 ){.      while
22f46 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20  ( nIn>0 ){.     
22f47 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a     int len = 0;.
22f48 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
22f49 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a   i<nChar; i++){.
22f4a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20            len = 
22f4b 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20  aLen[i];.       
22f4c 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20     if( len<=nIn 
22f4d 26 26 20 6d 65 6d 63 6d 70 28 26 7a 49 6e 5b 6e  && memcmp(&zIn[n
22f4e 49 6e 2d 6c 65 6e 5d 2c 61 7a 43 68 61 72 5b 69  In-len],azChar[i
22f4f 5d 2c 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61  ],len)==0 ) brea
22f50 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
22f51 20 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61       if( i>=nCha
22f52 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  r ) break;.     
22f53 20 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20     nIn -= len;. 
22f54 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
22f55 20 69 66 28 20 7a 43 68 61 72 53 65 74 20 29 7b   if( zCharSet ){
22f56 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
22f57 72 65 65 28 61 7a 43 68 61 72 29 3b 0a 20 20 20  ree(azChar);.   
22f58 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
22f59 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
22f5a 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 49 6e  text, (char*)zIn
22f5b 2c 20 6e 49 6e 2c 20 53 51 4c 49 54 45 5f 54 52  , nIn, SQLITE_TR
22f5c 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a 0a 23 69  ANSIENT);.}...#i
22f5d 66 64 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e  fdef SQLITE_SOUN
22f5e 44 45 58 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  DEX./*.** Comput
22f5f 65 20 74 68 65 20 73 6f 75 6e 64 65 78 20 65 6e  e the soundex en
22f60 63 6f 64 69 6e 67 20 6f 66 20 61 20 77 6f 72 64  coding of a word
22f61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
22f62 20 73 6f 75 6e 64 65 78 46 75 6e 63 28 0a 20 20   soundexFunc(.  
22f63 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
22f64 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
22f65 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
22f66 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
22f67 20 20 63 68 61 72 20 7a 52 65 73 75 6c 74 5b 38    char zResult[8
22f68 5d 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  ];.  const u8 *z
22f69 49 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  In;.  int i, j;.
22f6a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
22f6b 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 43 6f  nsigned char iCo
22f6c 64 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20  de[] = {.    0, 
22f6d 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
22f6e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
22f6f 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20   0, 0, 0, 0,.   
22f70 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
22f71 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
22f72 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
22f73 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c  .    0, 0, 0, 0,
22f74 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
22f75 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
22f76 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30  , 0,.    0, 0, 0
22f77 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
22f78 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
22f79 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20  0, 0, 0,.    0, 
22f7a 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31  0, 1, 2, 3, 0, 1
22f7b 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c  , 2, 0, 0, 2, 2,
22f7c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20   4, 5, 5, 0,.   
22f7d 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20   1, 2, 6, 2, 3, 
22f7e 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32  0, 1, 0, 2, 0, 2
22f7f 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
22f80 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c  .    0, 0, 1, 2,
22f81 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20   3, 0, 1, 2, 0, 
22f82 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35  0, 2, 2, 4, 5, 5
22f83 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36  , 0,.    1, 2, 6
22f84 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c  , 2, 3, 0, 1, 0,
22f85 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20   2, 0, 2, 0, 0, 
22f86 30 2c 20 30 2c 20 30 2c 0a 20 20 7d 3b 0a 20 20  0, 0, 0,.  };.  
22f87 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
22f88 29 3b 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29  );.  zIn = (u8*)
22f89 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
22f8a 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  xt(argv[0]);.  i
22f8b 66 28 20 7a 49 6e 3d 3d 30 20 29 20 7a 49 6e 20  f( zIn==0 ) zIn 
22f8c 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 66 6f 72  = (u8*)"";.  for
22f8d 28 69 3d 30 3b 20 7a 49 6e 5b 69 5d 20 26 26 20  (i=0; zIn[i] && 
22f8e 21 69 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29  !isalpha(zIn[i])
22f8f 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 7a  ; i++){}.  if( z
22f90 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 38 20  In[i] ){.    u8 
22f91 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 64 65  prevcode = iCode
22f92 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20  [zIn[i]&0x7f];. 
22f93 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20     zResult[0] = 
22f94 74 6f 75 70 70 65 72 28 7a 49 6e 5b 69 5d 29 3b  toupper(zIn[i]);
22f95 0a 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c  .    for(j=1; j<
22f96 34 20 26 26 20 7a 49 6e 5b 69 5d 3b 20 69 2b 2b  4 && zIn[i]; i++
22f97 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 64  ){.      int cod
22f98 65 20 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d  e = iCode[zIn[i]
22f99 26 30 78 37 66 5d 3b 0a 20 20 20 20 20 20 69 66  &0x7f];.      if
22f9a 28 20 63 6f 64 65 3e 30 20 29 7b 0a 20 20 20 20  ( code>0 ){.    
22f9b 20 20 20 20 69 66 28 20 63 6f 64 65 21 3d 70 72      if( code!=pr
22f9c 65 76 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  evcode ){.      
22f9d 20 20 20 20 70 72 65 76 63 6f 64 65 20 3d 20 63      prevcode = c
22f9e 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  ode;.          z
22f9f 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 63 6f  Result[j++] = co
22fa0 64 65 20 2b 20 27 30 27 3b 0a 20 20 20 20 20 20  de + '0';.      
22fa1 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
22fa2 0a 20 20 20 20 20 20 20 20 70 72 65 76 63 6f 64  .        prevcod
22fa3 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  e = 0;.      }. 
22fa4 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20     }.    while( 
22fa5 6a 3c 34 20 29 7b 0a 20 20 20 20 20 20 7a 52 65  j<4 ){.      zRe
22fa6 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 27 30 27 3b  sult[j++] = '0';
22fa7 0a 20 20 20 20 7d 0a 20 20 20 20 7a 52 65 73 75  .    }.    zResu
22fa8 6c 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73  lt[j] = 0;.    s
22fa9 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
22faa 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73  xt(context, zRes
22fab 75 6c 74 2c 20 34 2c 20 53 51 4c 49 54 45 5f 54  ult, 4, SQLITE_T
22fac 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c  RANSIENT);.  }el
22fad 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
22fae 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
22faf 65 78 74 2c 20 22 3f 30 30 30 22 2c 20 34 2c 20  ext, "?000", 4, 
22fb0 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
22fb1 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69    }.}.#endif..#i
22fb2 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22fb3 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
22fb4 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f  ./*.** A functio
22fb5 6e 20 74 68 61 74 20 6c 6f 61 64 73 20 61 20 73  n that loads a s
22fb6 68 61 72 65 64 2d 6c 69 62 72 61 72 79 20 65 78  hared-library ex
22fb7 74 65 6e 73 69 6f 6e 20 74 68 65 6e 20 72 65 74  tension then ret
22fb8 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74  urns NULL..*/.st
22fb9 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 45 78  atic void loadEx
22fba 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  t(sqlite3_contex
22fbb 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
22fbc 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
22fbd 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63  lue **argv){.  c
22fbe 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
22fbf 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
22fc0 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
22fc1 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
22fc2 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f  const char *zPro
22fc3 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  c;.  sqlite3 *db
22fc4 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
22fc5 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
22fc6 74 65 78 74 29 3b 0a 20 20 63 68 61 72 20 2a 7a  text);.  char *z
22fc7 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 69  ErrMsg = 0;..  i
22fc8 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20  f( argc==2 ){.  
22fc9 20 20 7a 50 72 6f 63 20 3d 20 28 63 6f 6e 73 74    zProc = (const
22fca 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
22fcb 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
22fcc 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  1]);.  }else{.  
22fcd 20 20 7a 50 72 6f 63 20 3d 20 30 3b 0a 20 20 7d    zProc = 0;.  }
22fce 0a 20 20 69 66 28 20 7a 46 69 6c 65 20 26 26 20  .  if( zFile && 
22fcf 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74  sqlite3_load_ext
22fd0 65 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65  ension(db, zFile
22fd1 2c 20 7a 50 72 6f 63 2c 20 26 7a 45 72 72 4d 73  , zProc, &zErrMs
22fd2 67 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  g) ){.    sqlite
22fd3 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
22fd4 6f 6e 74 65 78 74 2c 20 7a 45 72 72 4d 73 67 2c  ontext, zErrMsg,
22fd5 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   -1);.    sqlite
22fd6 33 5f 66 72 65 65 28 7a 45 72 72 4d 73 67 29 3b  3_free(zErrMsg);
22fd7 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  .  }.}.#endif...
22fd8 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
22fd9 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
22fda 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  ng structure hol
22fdb 64 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 6f  ds the context o
22fdc 66 20 61 0a 2a 2a 20 73 75 6d 28 29 20 6f 72 20  f a.** sum() or 
22fdd 61 76 67 28 29 20 61 67 67 72 65 67 61 74 65 20  avg() aggregate 
22fde 63 6f 6d 70 75 74 61 74 69 6f 6e 2e 0a 2a 2f 0a  computation..*/.
22fdf 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
22fe0 75 6d 43 74 78 20 53 75 6d 43 74 78 3b 0a 73 74  umCtx SumCtx;.st
22fe1 72 75 63 74 20 53 75 6d 43 74 78 20 7b 0a 20 20  ruct SumCtx {.  
22fe2 64 6f 75 62 6c 65 20 72 53 75 6d 3b 20 20 20 20  double rSum;    
22fe3 20 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f    /* Floating po
22fe4 69 6e 74 20 73 75 6d 20 2a 2f 0a 20 20 69 36 34  int sum */.  i64
22fe5 20 69 53 75 6d 3b 20 20 20 20 20 20 20 20 20 2f   iSum;         /
22fe6 2a 20 49 6e 74 65 67 65 72 20 73 75 6d 20 2a 2f  * Integer sum */
22fe7 20 20 20 0a 20 20 69 36 34 20 63 6e 74 3b 20 20     .  i64 cnt;  
22fe8 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
22fe9 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 73 75  r of elements su
22fea 6d 6d 65 64 20 2a 2f 0a 20 20 75 38 20 6f 76 65  mmed */.  u8 ove
22feb 72 66 6c 6f 77 3b 20 20 20 20 20 20 2f 2a 20 54  rflow;      /* T
22fec 72 75 65 20 69 66 20 69 6e 74 65 67 65 72 20 6f  rue if integer o
22fed 76 65 72 66 6c 6f 77 20 73 65 65 6e 20 2a 2f 0a  verflow seen */.
22fee 20 20 75 38 20 61 70 70 72 6f 78 3b 20 20 20 20    u8 approx;    
22fef 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e      /* True if n
22ff0 6f 6e 2d 69 6e 74 65 67 65 72 20 76 61 6c 75 65  on-integer value
22ff1 20 77 61 73 20 69 6e 70 75 74 20 74 6f 20 74 68   was input to th
22ff2 65 20 73 75 6d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  e sum */.};../*.
22ff3 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64  ** Routines used
22ff4 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20   to compute the 
22ff5 73 75 6d 2c 20 61 76 65 72 61 67 65 2c 20 61 6e  sum, average, an
22ff6 64 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54  d total..**.** T
22ff7 68 65 20 53 55 4d 28 29 20 66 75 6e 63 74 69 6f  he SUM() functio
22ff8 6e 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 28 62  n follows the (b
22ff9 72 6f 6b 65 6e 29 20 53 51 4c 20 73 74 61 6e 64  roken) SQL stand
22ffa 61 72 64 20 77 68 69 63 68 20 6d 65 61 6e 73 0a  ard which means.
22ffb 2a 2a 20 74 68 61 74 20 69 74 20 72 65 74 75 72  ** that it retur
22ffc 6e 73 20 4e 55 4c 4c 20 69 66 20 69 74 20 73 75  ns NULL if it su
22ffd 6d 73 20 6f 76 65 72 20 6e 6f 20 69 6e 70 75 74  ms over no input
22ffe 73 2e 20 20 54 4f 54 41 4c 20 72 65 74 75 72 6e  s.  TOTAL return
22fff 73 0a 2a 2a 20 30 2e 30 20 69 6e 20 74 68 61 74  s.** 0.0 in that
23000 20 63 61 73 65 2e 20 20 49 6e 20 61 64 64 69 74   case.  In addit
23001 69 6f 6e 2c 20 54 4f 54 41 4c 20 61 6c 77 61 79  ion, TOTAL alway
23002 73 20 72 65 74 75 72 6e 73 20 61 20 66 6c 6f 61  s returns a floa
23003 74 20 77 68 65 72 65 0a 2a 2a 20 53 55 4d 20 6d  t where.** SUM m
23004 69 67 68 74 20 72 65 74 75 72 6e 20 61 6e 20 69  ight return an i
23005 6e 74 65 67 65 72 20 69 66 20 69 74 20 6e 65 76  nteger if it nev
23006 65 72 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20  er encounters a 
23007 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a  floating point.*
23008 2a 20 76 61 6c 75 65 2e 20 20 54 4f 54 41 4c 20  * value.  TOTAL 
23009 6e 65 76 65 72 20 66 61 69 6c 73 2c 20 62 75 74  never fails, but
2300a 20 53 55 4d 20 6d 69 67 68 74 20 74 68 72 6f 75   SUM might throu
2300b 67 68 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20  gh an exception 
2300c 69 66 0a 2a 2a 20 69 74 20 6f 76 65 72 66 6c 6f  if.** it overflo
2300d 77 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a  ws an integer..*
2300e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75  /.static void su
2300f 6d 53 74 65 70 28 73 71 6c 69 74 65 33 5f 63 6f  mStep(sqlite3_co
23010 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
23011 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
23012 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
23013 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20  .  SumCtx *p;.  
23014 69 6e 74 20 74 79 70 65 3b 0a 20 20 61 73 73 65  int type;.  asse
23015 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20  rt( argc==1 );. 
23016 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
23017 52 28 61 72 67 63 29 3b 0a 20 20 70 20 3d 20 73  R(argc);.  p = s
23018 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
23019 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  _context(context
2301a 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20  , sizeof(*p));. 
2301b 20 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f   type = sqlite3_
2301c 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
2301d 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  pe(argv[0]);.  i
2301e 66 28 20 70 20 26 26 20 74 79 70 65 21 3d 53 51  f( p && type!=SQ
2301f 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20  LITE_NULL ){.   
23020 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 69   p->cnt++;.    i
23021 66 28 20 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f  f( type==SQLITE_
23022 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20  INTEGER ){.     
23023 20 69 36 34 20 76 20 3d 20 73 71 6c 69 74 65 33   i64 v = sqlite3
23024 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67  _value_int64(arg
23025 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e  v[0]);.      p->
23026 72 53 75 6d 20 2b 3d 20 76 3b 0a 20 20 20 20 20  rSum += v;.     
23027 20 69 66 28 20 28 70 2d 3e 61 70 70 72 6f 78 7c   if( (p->approx|
23028 70 2d 3e 6f 76 65 72 66 6c 6f 77 29 3d 3d 30 20  p->overflow)==0 
23029 29 7b 0a 20 20 20 20 20 20 20 20 69 36 34 20 69  ){.        i64 i
2302a 4e 65 77 53 75 6d 20 3d 20 70 2d 3e 69 53 75 6d  NewSum = p->iSum
2302b 20 2b 20 76 3b 0a 20 20 20 20 20 20 20 20 69 6e   + v;.        in
2302c 74 20 73 31 20 3d 20 28 69 6e 74 29 28 70 2d 3e  t s1 = (int)(p->
2302d 69 53 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28  iSum >> (sizeof(
2302e 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20  i64)*8-1));.    
2302f 20 20 20 20 69 6e 74 20 73 32 20 3d 20 28 69 6e      int s2 = (in
23030 74 29 28 76 20 20 20 20 20 20 20 3e 3e 20 28 73  t)(v       >> (s
23031 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 29  izeof(i64)*8-1))
23032 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 33  ;.        int s3
23033 20 3d 20 28 69 6e 74 29 28 69 4e 65 77 53 75 6d   = (int)(iNewSum
23034 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34 29   >> (sizeof(i64)
23035 2a 38 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20  *8-1));.        
23036 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 3d 20 28 28  p->overflow = ((
23037 73 31 26 73 32 26 7e 73 33 29 20 7c 20 28 7e 73  s1&s2&~s3) | (~s
23038 31 26 7e 73 32 26 73 33 29 29 3f 31 3a 30 3b 0a  1&~s2&s3))?1:0;.
23039 20 20 20 20 20 20 20 20 70 2d 3e 69 53 75 6d 20          p->iSum 
2303a 3d 20 69 4e 65 77 53 75 6d 3b 0a 20 20 20 20 20  = iNewSum;.     
2303b 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
2303c 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 73      p->rSum += s
2303d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
2303e 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ble(argv[0]);.  
2303f 20 20 20 20 70 2d 3e 61 70 70 72 6f 78 20 3d 20      p->approx = 
23040 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73  1;.    }.  }.}.s
23041 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d 46 69  tatic void sumFi
23042 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63  nalize(sqlite3_c
23043 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29  ontext *context)
23044 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20  {.  SumCtx *p;. 
23045 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67   p = sqlite3_agg
23046 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
23047 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66  ontext, 0);.  if
23048 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20  ( p && p->cnt>0 
23049 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 76  ){.    if( p->ov
2304a 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
2304b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2304c 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 22 69 6e  rror(context,"in
2304d 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c  teger overflow",
2304e 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  -1);.    }else i
2304f 66 28 20 70 2d 3e 61 70 70 72 6f 78 20 29 7b 0a  f( p->approx ){.
23050 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
23051 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
23052 65 78 74 2c 20 70 2d 3e 72 53 75 6d 29 3b 0a 20  ext, p->rSum);. 
23053 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
23054 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
23055 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 70 2d  nt64(context, p-
23056 3e 69 53 75 6d 29 3b 0a 20 20 20 20 7d 0a 20 20  >iSum);.    }.  
23057 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
23058 61 76 67 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69  avgFinalize(sqli
23059 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2305a 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20  text){.  SumCtx 
2305b 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  *p;.  p = sqlite
2305c 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
2305d 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  ext(context, 0);
2305e 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 63  .  if( p && p->c
2305f 6e 74 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  nt>0 ){.    sqli
23060 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
23061 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53  e(context, p->rS
23062 75 6d 2f 28 64 6f 75 62 6c 65 29 70 2d 3e 63 6e  um/(double)p->cn
23063 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  t);.  }.}.static
23064 20 76 6f 69 64 20 74 6f 74 61 6c 46 69 6e 61 6c   void totalFinal
23065 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ize(sqlite3_cont
23066 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20  ext *context){. 
23067 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20   SumCtx *p;.  p 
23068 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  = sqlite3_aggreg
23069 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
2306a 65 78 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  ext, 0);.  sqlit
2306b 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
2306c 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d  (context, p ? p-
2306d 3e 72 53 75 6d 20 3a 20 30 2e 30 29 3b 0a 7d 0a  >rSum : 0.0);.}.
2306e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
2306f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b  wing structure k
23070 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 73 74  eeps track of st
23071 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ate information 
23072 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 75 6e 74  for the.** count
23073 28 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  () aggregate fun
23074 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65  ction..*/.typede
23075 66 20 73 74 72 75 63 74 20 43 6f 75 6e 74 43 74  f struct CountCt
23076 78 20 43 6f 75 6e 74 43 74 78 3b 0a 73 74 72 75  x CountCtx;.stru
23077 63 74 20 43 6f 75 6e 74 43 74 78 20 7b 0a 20 20  ct CountCtx {.  
23078 69 36 34 20 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  i64 n;.};../*.**
23079 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 69 6d 70   Routines to imp
2307a 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74  lement the count
2307b 28 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  () aggregate fun
2307c 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
2307d 20 76 6f 69 64 20 63 6f 75 6e 74 53 74 65 70 28   void countStep(
2307e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2307f 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72  *context, int ar
23080 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
23081 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 43 6f 75  e **argv){.  Cou
23082 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20  ntCtx *p;.  p = 
23083 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
23084 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
23085 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a  t, sizeof(*p));.
23086 20 20 69 66 28 20 28 61 72 67 63 3d 3d 30 20 7c    if( (argc==0 |
23087 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73  | SQLITE_NULL!=s
23088 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
23089 65 28 61 72 67 76 5b 30 5d 29 29 20 26 26 20 70  e(argv[0])) && p
2308a 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 2b 2b 3b 0a   ){.    p->n++;.
2308b 20 20 7d 0a 7d 20 20 20 0a 73 74 61 74 69 63 20    }.}   .static 
2308c 76 6f 69 64 20 63 6f 75 6e 74 46 69 6e 61 6c 69  void countFinali
2308d 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ze(sqlite3_conte
2308e 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20  xt *context){.  
2308f 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70  CountCtx *p;.  p
23090 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
23091 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
23092 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69  text, 0);.  sqli
23093 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
23094 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d  (context, p ? p-
23095 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  >n : 0);.}../*.*
23096 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 69 6d  * Routines to im
23097 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 29 20 61 6e  plement min() an
23098 64 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 74  d max() aggregat
23099 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  e functions..*/.
2309a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d  static void minm
2309b 61 78 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65  axStep(.  sqlite
2309c 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
2309d 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55 73  xt, .  int NotUs
2309e 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76  ed, .  sqlite3_v
2309f 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
230a0 20 4d 65 6d 20 2a 70 41 72 67 20 20 3d 20 28 4d   Mem *pArg  = (M
230a1 65 6d 20 2a 29 61 72 67 76 5b 30 5d 3b 0a 20 20  em *)argv[0];.  
230a2 4d 65 6d 20 2a 70 42 65 73 74 3b 0a 20 20 55 4e  Mem *pBest;.  UN
230a3 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
230a4 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20  otUsed);..  if( 
230a5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
230a6 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c  pe(argv[0])==SQL
230a7 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72  ITE_NULL ) retur
230a8 6e 3b 0a 20 20 70 42 65 73 74 20 3d 20 28 4d 65  n;.  pBest = (Me
230a9 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72  m *)sqlite3_aggr
230aa 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
230ab 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70  ntext, sizeof(*p
230ac 42 65 73 74 29 29 3b 0a 20 20 69 66 28 20 21 70  Best));.  if( !p
230ad 42 65 73 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a  Best ) return;..
230ae 20 20 69 66 28 20 70 42 65 73 74 2d 3e 66 6c 61    if( pBest->fla
230af 67 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 61  gs ){.    int ma
230b0 78 3b 0a 20 20 20 20 69 6e 74 20 63 6d 70 3b 0a  x;.    int cmp;.
230b1 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
230b2 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46  ll = sqlite3GetF
230b3 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65  uncCollSeq(conte
230b4 78 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 69 73  xt);.    /* This
230b5 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 69   step function i
230b6 73 20 75 73 65 64 20 66 6f 72 20 62 6f 74 68 20  s used for both 
230b7 74 68 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61  the min() and ma
230b8 78 28 29 20 61 67 67 72 65 67 61 74 65 73 2c 0a  x() aggregates,.
230b9 20 20 20 20 2a 2a 20 74 68 65 20 6f 6e 6c 79 20      ** the only 
230ba 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
230bb 65 6e 20 74 68 65 20 74 77 6f 20 62 65 69 6e 67  en the two being
230bc 20 74 68 61 74 20 74 68 65 20 73 65 6e 73 65 20   that the sense 
230bd 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f  of the.    ** co
230be 6d 70 61 72 69 73 6f 6e 20 69 73 20 69 6e 76 65  mparison is inve
230bf 72 74 65 64 2e 20 46 6f 72 20 74 68 65 20 6d 61  rted. For the ma
230c0 78 28 29 20 61 67 67 72 65 67 61 74 65 2c 20 74  x() aggregate, t
230c1 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  he.    ** sqlite
230c2 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 66 75  3_user_data() fu
230c3 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 28  nction returns (
230c4 76 6f 69 64 20 2a 29 2d 31 2e 20 46 6f 72 20 6d  void *)-1. For m
230c5 69 6e 28 29 20 69 74 0a 20 20 20 20 2a 2a 20 72  in() it.    ** r
230c6 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 64  eturns (void *)d
230c7 62 2c 20 77 68 65 72 65 20 64 62 20 69 73 20 74  b, where db is t
230c8 68 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61  he sqlite3* data
230c9 62 61 73 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20  base pointer..  
230ca 20 20 2a 2a 20 54 68 65 72 65 66 6f 72 65 20 74    ** Therefore t
230cb 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e  he next statemen
230cc 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20  t sets variable 
230cd 27 6d 61 78 27 20 74 6f 20 31 20 66 6f 72 20 74  'max' to 1 for t
230ce 68 65 20 6d 61 78 28 29 0a 20 20 20 20 2a 2a 20  he max().    ** 
230cf 61 67 67 72 65 67 61 74 65 2c 20 6f 72 20 30 20  aggregate, or 0 
230d0 66 6f 72 20 6d 69 6e 28 29 2e 0a 20 20 20 20 2a  for min()..    *
230d1 2f 0a 20 20 20 20 6d 61 78 20 3d 20 73 71 6c 69  /.    max = sqli
230d2 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f  te3_user_data(co
230d3 6e 74 65 78 74 29 21 3d 30 3b 0a 20 20 20 20 63  ntext)!=0;.    c
230d4 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43  mp = sqlite3MemC
230d5 6f 6d 70 61 72 65 28 70 42 65 73 74 2c 20 70 41  ompare(pBest, pA
230d6 72 67 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20  rg, pColl);.    
230d7 69 66 28 20 28 6d 61 78 20 26 26 20 63 6d 70 3c  if( (max && cmp<
230d8 30 29 20 7c 7c 20 28 21 6d 61 78 20 26 26 20 63  0) || (!max && c
230d9 6d 70 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 73  mp>0) ){.      s
230da 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70  qlite3VdbeMemCop
230db 79 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a  y(pBest, pArg);.
230dc 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
230dd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
230de 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72  mCopy(pBest, pAr
230df 67 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  g);.  }.}.static
230e0 20 76 6f 69 64 20 6d 69 6e 4d 61 78 46 69 6e 61   void minMaxFina
230e1 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lize(sqlite3_con
230e2 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a  text *context){.
230e3 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
230e4 2a 70 52 65 73 3b 0a 20 20 70 52 65 73 20 3d 20  *pRes;.  pRes = 
230e5 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
230e6 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  )sqlite3_aggrega
230e7 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
230e8 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 52  xt, 0);.  if( pR
230e9 65 73 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52  es ){.    if( pR
230ea 65 73 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20  es->flags ){.   
230eb 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
230ec 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
230ed 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20   pRes);.    }.  
230ee 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
230ef 52 65 6c 65 61 73 65 28 70 52 65 73 29 3b 0a 20  Release(pRes);. 
230f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 67 72 6f 75   }.}../*.** grou
230f1 70 5f 63 6f 6e 63 61 74 28 45 58 50 52 2c 20 3f  p_concat(EXPR, ?
230f2 53 45 50 41 52 41 54 4f 52 3f 29 0a 2a 2f 0a 73  SEPARATOR?).*/.s
230f3 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70  tatic void group
230f4 43 6f 6e 63 61 74 53 74 65 70 28 0a 20 20 73 71  ConcatStep(.  sq
230f5 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
230f6 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
230f7 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
230f8 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
230f9 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c  const char *zVal
230fa 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41  ;.  StrAccum *pA
230fb 63 63 75 6d 3b 0a 20 20 63 6f 6e 73 74 20 63 68  ccum;.  const ch
230fc 61 72 20 2a 7a 53 65 70 3b 0a 20 20 69 6e 74 20  ar *zSep;.  int 
230fd 6e 56 61 6c 2c 20 6e 53 65 70 2c 20 69 3b 0a 20  nVal, nSep, i;. 
230fe 20 69 66 28 20 61 72 67 63 3d 3d 30 20 7c 7c 20   if( argc==0 || 
230ff 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
23100 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c  pe(argv[0])==SQL
23101 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72  ITE_NULL ) retur
23102 6e 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20 28 53  n;.  pAccum = (S
23103 74 72 41 63 63 75 6d 2a 29 73 71 6c 69 74 65 33  trAccum*)sqlite3
23104 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
23105 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65  xt(context, size
23106 6f 66 28 2a 70 41 63 63 75 6d 29 29 3b 0a 0a 20  of(*pAccum));.. 
23107 20 69 66 28 20 70 41 63 63 75 6d 20 29 7b 0a 20   if( pAccum ){. 
23108 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
23109 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2310a 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
2310b 78 74 29 3b 0a 20 20 20 20 70 41 63 63 75 6d 2d  xt);.    pAccum-
2310c 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a  >useMalloc = 1;.
2310d 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 78 41 6c      pAccum->mxAl
2310e 6c 6f 63 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74  loc = db->aLimit
2310f 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
23110 4e 47 54 48 5d 3b 0a 20 20 20 20 69 66 28 20 70  NGTH];.    if( p
23111 41 63 63 75 6d 2d 3e 6e 43 68 61 72 20 29 7b 0a  Accum->nChar ){.
23112 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3e 31        if( argc>1
23113 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 70   ){.        zSep
23114 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65   = (char*)sqlite
23115 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
23116 76 5b 61 72 67 63 2d 31 5d 29 3b 0a 20 20 20 20  v[argc-1]);.    
23117 20 20 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 74      nSep = sqlit
23118 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
23119 72 67 76 5b 61 72 67 63 2d 31 5d 29 3b 0a 20 20  rgv[argc-1]);.  
2311a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2311b 20 20 20 7a 53 65 70 20 3d 20 22 2c 22 3b 0a 20     zSep = ",";. 
2311c 20 20 20 20 20 20 20 6e 53 65 70 20 3d 20 31 3b         nSep = 1;
2311d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
2311e 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
2311f 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 65  pend(pAccum, zSe
23120 70 2c 20 6e 53 65 70 29 3b 0a 20 20 20 20 7d 0a  p, nSep);.    }.
23121 20 20 20 20 69 20 3d 20 30 3b 0a 20 20 20 20 64      i = 0;.    d
23122 6f 7b 0a 20 20 20 20 20 20 7a 56 61 6c 20 3d 20  o{.      zVal = 
23123 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76  (char*)sqlite3_v
23124 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 69  alue_text(argv[i
23125 5d 29 3b 0a 20 20 20 20 20 20 6e 56 61 6c 20 3d  ]);.      nVal =
23126 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
23127 79 74 65 73 28 61 72 67 76 5b 69 5d 29 3b 0a 20  ytes(argv[i]);. 
23128 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
23129 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
2312a 6d 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 3b 0a  m, zVal, nVal);.
2312b 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d        i++;.    }
2312c 77 68 69 6c 65 28 20 69 3c 61 72 67 63 2d 31 20  while( i<argc-1 
2312d 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  );.  }.}.static 
2312e 76 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74  void groupConcat
2312f 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  Finalize(sqlite3
23130 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23131 74 29 7b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a  t){.  StrAccum *
23132 70 41 63 63 75 6d 3b 0a 20 20 70 41 63 63 75 6d  pAccum;.  pAccum
23133 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
23134 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
23135 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  text, 0);.  if( 
23136 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 69 66  pAccum ){.    if
23137 28 20 70 41 63 63 75 6d 2d 3e 74 6f 6f 42 69 67  ( pAccum->tooBig
23138 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
23139 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
2313a 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a  oobig(context);.
2313b 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41      }else if( pA
2313c 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ccum->mallocFail
2313d 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ed ){.      sqli
2313e 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2313f 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
23140 0a 20 20 20 20 7d 65 6c 73 65 7b 20 20 20 20 0a  .    }else{    .
23141 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
23142 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
23143 74 2c 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  t, sqlite3StrAcc
23144 75 6d 46 69 6e 69 73 68 28 70 41 63 63 75 6d 29  umFinish(pAccum)
23145 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20  , -1, .         
23146 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23147 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
23148 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
23149 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2314a 20 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c 20   registered all 
2314b 6f 66 20 74 68 65 20 61 62 6f 76 65 20 43 20 66  of the above C f
2314c 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a  unctions as SQL.
2314d 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  ** functions.  T
2314e 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68  his should be th
2314f 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69  e only routine i
23150 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68  n this file with
23151 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e  .** external lin
23152 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  kage..*/.SQLITE_
23153 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23154 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69 6c  ite3RegisterBuil
23155 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c  tinFunctions(sql
23156 69 74 65 33 20 2a 64 62 29 7b 0a 23 69 66 6e 64  ite3 *db){.#ifnd
23157 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
23158 4c 54 45 52 54 41 42 4c 45 0a 20 20 73 71 6c 69  LTERTABLE.  sqli
23159 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e  te3AlterFunction
2315a 73 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a 20 20  s(db);.#endif.  
2315b 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
2315c 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 6e 74  ailed ){.    int
2315d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76   rc = sqlite3_ov
2315e 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
2315f 64 62 2c 20 22 4d 41 54 43 48 22 2c 20 32 29 3b  db, "MATCH", 2);
23160 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
23161 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c  =SQLITE_NOMEM ||
23162 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
23163 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
23164 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
23165 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
23166 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  iled = 1;.    }.
23167 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
23168 45 5f 53 53 45 0a 20 20 28 76 6f 69 64 29 73 71  E_SSE.  (void)sq
23169 6c 69 74 65 33 53 73 65 46 75 6e 63 74 69 6f 6e  lite3SseFunction
2316a 73 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  s(db);.#endif.}.
2316b 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 4c  ./*.** Set the L
2316c 49 4b 45 4f 50 54 20 66 6c 61 67 20 6f 6e 20 74  IKEOPT flag on t
2316d 68 65 20 32 2d 61 72 67 75 6d 65 6e 74 20 66 75  he 2-argument fu
2316e 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
2316f 67 69 76 65 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 73  given name..*/.s
23170 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 4c 69  tatic void setLi
23171 6b 65 4f 70 74 46 6c 61 67 28 73 71 6c 69 74 65  keOptFlag(sqlite
23172 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
23173 72 20 2a 7a 4e 61 6d 65 2c 20 75 38 20 66 6c 61  r *zName, u8 fla
23174 67 56 61 6c 29 7b 0a 20 20 46 75 6e 63 44 65 66  gVal){.  FuncDef
23175 20 2a 70 44 65 66 3b 0a 20 20 70 44 65 66 20 3d   *pDef;.  pDef =
23176 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
23177 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  tion(db, zName, 
23178 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
23179 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20  zName),.        
2317a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2317b 20 20 20 20 20 32 2c 20 53 51 4c 49 54 45 5f 55       2, SQLITE_U
2317c 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  TF8, 0);.  if( p
2317d 44 65 66 20 29 7b 0a 20 20 20 20 70 44 65 66 2d  Def ){.    pDef-
2317e 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 56 61 6c  >flags = flagVal
2317f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
23180 65 67 69 73 74 65 72 20 74 68 65 20 62 75 69 6c  egister the buil
23181 74 2d 69 6e 20 4c 49 4b 45 20 61 6e 64 20 47 4c  t-in LIKE and GL
23182 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  OB functions.  T
23183 68 65 20 63 61 73 65 53 65 6e 73 69 74 69 76 65  he caseSensitive
23184 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 64 65  .** parameter de
23185 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72  termines whether
23186 20 6f 72 20 6e 6f 74 20 74 68 65 20 4c 49 4b 45   or not the LIKE
23187 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63 61 73   operator is cas
23188 65 0a 2a 2a 20 73 65 6e 73 69 74 69 76 65 2e 20  e.** sensitive. 
23189 20 47 4c 4f 42 20 69 73 20 61 6c 77 61 79 73 20   GLOB is always 
2318a 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a  case sensitive..
2318b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2318c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
2318d 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69  gisterLikeFuncti
2318e 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ons(sqlite3 *db,
2318f 20 69 6e 74 20 63 61 73 65 53 65 6e 73 69 74 69   int caseSensiti
23190 76 65 29 7b 0a 20 20 73 74 72 75 63 74 20 63 6f  ve){.  struct co
23191 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f  mpareInfo *pInfo
23192 3b 0a 20 20 69 66 28 20 63 61 73 65 53 65 6e 73  ;.  if( caseSens
23193 69 74 69 76 65 20 29 7b 0a 20 20 20 20 70 49 6e  itive ){.    pIn
23194 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d  fo = (struct com
23195 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49  pareInfo*)&likeI
23196 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b  nfoAlt;.  }else{
23197 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 74  .    pInfo = (st
23198 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
23199 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 3b  *)&likeInfoNorm;
2319a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 43 72  .  }.  sqlite3Cr
2319b 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 6c 69  eateFunc(db, "li
2319c 6b 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55  ke", 2, SQLITE_U
2319d 54 46 38 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65  TF8, pInfo, like
2319e 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73  Func, 0, 0);.  s
2319f 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
231a0 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 33 2c 20  (db, "like", 3, 
231a1 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 70 49 6e  SQLITE_UTF8, pIn
231a2 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c  fo, likeFunc, 0,
231a3 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 72   0);.  sqlite3Cr
231a4 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 67 6c  eateFunc(db, "gl
231a5 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55  ob", 2, SQLITE_U
231a6 54 46 38 2c 20 0a 20 20 20 20 20 20 28 73 74 72  TF8, .      (str
231a7 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a  uct compareInfo*
231a8 29 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c 69 6b 65  )&globInfo, like
231a9 46 75 6e 63 2c 20 30 2c 30 29 3b 0a 20 20 73 65  Func, 0,0);.  se
231aa 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64 62 2c  tLikeOptFlag(db,
231ab 20 22 67 6c 6f 62 22 2c 20 53 51 4c 49 54 45 5f   "glob", SQLITE_
231ac 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49  FUNC_LIKE | SQLI
231ad 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3b 0a 20  TE_FUNC_CASE);. 
231ae 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28   setLikeOptFlag(
231af 64 62 2c 20 22 6c 69 6b 65 22 2c 20 0a 20 20 20  db, "like", .   
231b0 20 20 20 63 61 73 65 53 65 6e 73 69 74 69 76 65     caseSensitive
231b1 20 3f 20 28 53 51 4c 49 54 45 5f 46 55 4e 43 5f   ? (SQLITE_FUNC_
231b2 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46 55  LIKE | SQLITE_FU
231b3 4e 43 5f 43 41 53 45 29 20 3a 20 53 51 4c 49 54  NC_CASE) : SQLIT
231b4 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b 0a 7d 0a  E_FUNC_LIKE);.}.
231b5 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 70 6f 69  ./*.** pExpr poi
231b6 6e 74 73 20 74 6f 20 61 6e 20 65 78 70 72 65 73  nts to an expres
231b7 73 69 6f 6e 20 77 68 69 63 68 20 69 6d 70 6c 65  sion which imple
231b8 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f 6e  ments a function
231b9 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 20 61  .  If.** it is a
231ba 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 61 70  ppropriate to ap
231bb 70 6c 79 20 74 68 65 20 4c 49 4b 45 20 6f 70 74  ply the LIKE opt
231bc 69 6d 69 7a 61 74 69 6f 6e 20 74 6f 20 74 68 61  imization to tha
231bd 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68  t function.** th
231be 65 6e 20 73 65 74 20 61 57 63 5b 30 5d 20 74 68  en set aWc[0] th
231bf 72 6f 75 67 68 20 61 57 63 5b 32 5d 20 74 6f 20  rough aWc[2] to 
231c0 74 68 65 20 77 69 6c 64 63 61 72 64 20 63 68 61  the wildcard cha
231c1 72 61 63 74 65 72 73 20 61 6e 64 0a 2a 2a 20 72  racters and.** r
231c2 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20  eturn TRUE.  If 
231c3 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
231c4 6e 6f 74 20 61 20 4c 49 4b 45 2d 73 74 79 6c 65  not a LIKE-style
231c5 20 66 75 6e 63 74 69 6f 6e 20 74 68 65 6e 0a 2a   function then.*
231c6 2a 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a  * return FALSE..
231c7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
231c8 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c  E int sqlite3IsL
231c9 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  ikeFunction(sqli
231ca 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70  te3 *db, Expr *p
231cb 45 78 70 72 2c 20 69 6e 74 20 2a 70 49 73 4e 6f  Expr, int *pIsNo
231cc 63 61 73 65 2c 20 63 68 61 72 20 2a 61 57 63 29  case, char *aWc)
231cd 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65  {.  FuncDef *pDe
231ce 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  f;.  if( pExpr->
231cf 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20  op!=TK_FUNCTION 
231d0 7c 7c 20 21 70 45 78 70 72 2d 3e 70 4c 69 73 74  || !pExpr->pList
231d1 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
231d2 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 78 70  ;.  }.  if( pExp
231d3 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21  r->pList->nExpr!
231d4 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =2 ){.    return
231d5 20 30 3b 0a 20 20 7d 0a 20 20 70 44 65 66 20 3d   0;.  }.  pDef =
231d6 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
231d7 74 69 6f 6e 28 64 62 2c 20 28 63 68 61 72 2a 29  tion(db, (char*)
231d8 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20  pExpr->token.z, 
231d9 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20  pExpr->token.n, 
231da 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  2,.             
231db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
231dc 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b  SQLITE_UTF8, 0);
231dd 0a 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 7c  .  if( pDef==0 |
231de 7c 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26  | (pDef->flags &
231df 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b   SQLITE_FUNC_LIK
231e0 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  E)==0 ){.    ret
231e1 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
231e2 20 54 68 65 20 6d 65 6d 63 70 79 28 29 20 73 74   The memcpy() st
231e3 61 74 65 6d 65 6e 74 20 61 73 73 75 6d 65 73 20  atement assumes 
231e4 74 68 61 74 20 74 68 65 20 77 69 6c 64 63 61 72  that the wildcar
231e5 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  d characters are
231e6 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20  .  ** the first 
231e7 74 68 72 65 65 20 73 74 61 74 65 6d 65 6e 74 73  three statements
231e8 20 69 6e 20 74 68 65 20 63 6f 6d 70 61 72 65 49   in the compareI
231e9 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20  nfo structure.  
231ea 54 68 65 0a 20 20 2a 2a 20 61 73 73 65 72 74 73  The.  ** asserts
231eb 28 29 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 76  () that follow v
231ec 65 72 69 66 79 20 74 68 61 74 20 61 73 73 75 6d  erify that assum
231ed 70 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d  ption.  */.  mem
231ee 63 70 79 28 61 57 63 2c 20 70 44 65 66 2d 3e 70  cpy(aWc, pDef->p
231ef 55 73 65 72 44 61 74 61 2c 20 33 29 3b 0a 20 20  UserData, 3);.  
231f0 61 73 73 65 72 74 28 20 28 63 68 61 72 2a 29 26  assert( (char*)&
231f1 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 3d 20 28  likeInfoAlt == (
231f2 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41  char*)&likeInfoA
231f3 6c 74 2e 6d 61 74 63 68 41 6c 6c 20 29 3b 0a 20  lt.matchAll );. 
231f4 20 61 73 73 65 72 74 28 20 26 28 28 63 68 61 72   assert( &((char
231f5 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b  *)&likeInfoAlt)[
231f6 31 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69  1] == (char*)&li
231f7 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 4f  keInfoAlt.matchO
231f8 6e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ne );.  assert( 
231f9 26 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e  &((char*)&likeIn
231fa 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d 20 28 63 68  foAlt)[2] == (ch
231fb 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74  ar*)&likeInfoAlt
231fc 2e 6d 61 74 63 68 53 65 74 20 29 3b 0a 20 20 2a  .matchSet );.  *
231fd 70 49 73 4e 6f 63 61 73 65 20 3d 20 28 70 44 65  pIsNocase = (pDe
231fe 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f->flags & SQLIT
231ff 45 5f 46 55 4e 43 5f 43 41 53 45 29 3d 3d 30 3b  E_FUNC_CASE)==0;
23200 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
23201 2f 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c 20 6f 66  /*.** All all of
23202 20 74 68 65 20 46 75 6e 63 44 65 66 20 73 74 72   the FuncDef str
23203 75 63 74 75 72 65 73 20 69 6e 20 74 68 65 20 61  uctures in the a
23204 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 61 72  BuiltinFunc[] ar
23205 72 61 79 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20  ray above.** to 
23206 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74  the global funct
23207 69 6f 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20  ion hash table. 
23208 20 54 68 69 73 20 6f 63 63 75 72 73 20 61 74 20   This occurs at 
23209 73 74 61 72 74 2d 74 69 6d 65 20 28 61 73 0a 2a  start-time (as.*
2320a 2a 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 20  * a consequence 
2320b 6f 66 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  of calling sqlit
2320c 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29  e3_initialize())
2320d 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68  ..**.** After th
2320e 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 0a  is routine runs.
2320f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23210 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
23211 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63  gisterGlobalFunc
23212 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20 2f  tions(void){.  /
23213 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  *.  ** The follo
23214 77 69 6e 67 20 61 72 72 61 79 20 68 6f 6c 64 73  wing array holds
23215 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
23216 72 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74  res for all of t
23217 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 20 20 2a  he functions.  *
23218 2a 20 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69  * defined in thi
23219 73 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a  s file..  **.  *
2321a 2a 20 54 68 65 20 61 72 72 61 79 20 63 61 6e 6e  * The array cann
2321b 6f 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 73  ot be constant s
2321c 69 6e 63 65 20 63 68 61 6e 67 65 73 20 61 72 65  ince changes are
2321d 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 20 20 2a   made to the.  *
2321e 2a 20 46 75 6e 63 44 65 66 2e 70 48 61 73 68 20  * FuncDef.pHash 
2321f 65 6c 65 6d 65 6e 74 73 20 61 74 20 73 74 61 72  elements at star
23220 74 2d 74 69 6d 65 2e 20 20 54 68 65 20 65 6c 65  t-time.  The ele
23221 6d 65 6e 74 73 20 6f 66 20 74 68 69 73 20 61 72  ments of this ar
23222 72 61 79 0a 20 20 2a 2a 20 61 72 65 20 72 65 61  ray.  ** are rea
23223 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20 69 6e 69  d-only after ini
23224 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 73 20 63  tialization is c
23225 6f 6d 70 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20 20  omplete..  */.  
23226 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
23227 44 20 46 75 6e 63 44 65 66 20 61 42 75 69 6c 74  D FuncDef aBuilt
23228 69 6e 46 75 6e 63 5b 5d 20 3d 20 7b 0a 20 20 20  inFunc[] = {.   
23229 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d 2c   FUNCTION(ltrim,
2322a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
2322b 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20   1, 0, trimFunc 
2322c 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
2322d 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d 2c 20 20  UNCTION(ltrim,  
2322e 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 31              2, 1
2322f 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20  , 0, trimFunc   
23230 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
23231 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20 20  CTION(rtrim,    
23232 20 20 20 20 20 20 20 20 20 20 31 2c 20 32 2c 20            1, 2, 
23233 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20  0, trimFunc     
23234 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
23235 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20 20 20 20  ION(rtrim,      
23236 20 20 20 20 20 20 20 20 32 2c 20 32 2c 20 30 2c          2, 2, 0,
23237 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
23238 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
23239 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20  N(trim,         
2323a 20 20 20 20 20 20 31 2c 20 33 2c 20 30 2c 20 74        1, 3, 0, t
2323b 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20  rimFunc         
2323c 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
2323d 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20  trim,           
2323e 20 20 20 20 32 2c 20 33 2c 20 30 2c 20 74 72 69      2, 3, 0, tri
2323f 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c  mFunc         ),
23240 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69  .    FUNCTION(mi
23241 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
23242 20 2d 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61   -1, 0, 1, minma
23243 78 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  xFunc       ),. 
23244 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69 6e 2c     FUNCTION(min,
23245 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23246 30 2c 20 30 2c 20 31 2c 20 30 20 20 20 20 20 20  0, 0, 1, 0      
23247 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20            ),.   
23248 20 41 47 47 52 45 47 41 54 45 28 6d 69 6e 2c 20   AGGREGATE(min, 
23249 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
2324a 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65   0, 1, minmaxSte
2324b 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69  p,      minMaxFi
2324c 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46 55  nalize ),.    FU
2324d 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20 20  NCTION(max,     
2324e 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 31 2c            -1, 1,
2324f 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20   1, minmaxFunc  
23250 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
23251 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20 20 20 20  TION(max,       
23252 20 20 20 20 20 20 20 20 20 30 2c 20 31 2c 20 31           0, 1, 1
23253 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20  , 0             
23254 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47     ),.    AGGREG
23255 41 54 45 28 6d 61 78 2c 20 20 20 20 20 20 20 20  ATE(max,        
23256 20 20 20 20 20 20 20 31 2c 20 31 2c 20 31 2c 20         1, 1, 1, 
23257 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20 20 20  minmaxStep,     
23258 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20   minMaxFinalize 
23259 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
2325a 74 79 70 65 6f 66 2c 20 20 20 20 20 20 20 20 20  typeof,         
2325b 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 74 79 70      1, 0, 0, typ
2325c 65 6f 66 46 75 6e 63 20 20 20 20 20 20 20 29 2c  eofFunc       ),
2325d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 65  .    FUNCTION(le
2325e 6e 67 74 68 2c 20 20 20 20 20 20 20 20 20 20 20  ngth,           
2325f 20 20 31 2c 20 30 2c 20 30 2c 20 6c 65 6e 67 74    1, 0, 0, lengt
23260 68 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  hFunc       ),. 
23261 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73     FUNCTION(subs
23262 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  tr,             
23263 32 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46  2, 0, 0, substrF
23264 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20  unc       ),.   
23265 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73 74 72   FUNCTION(substr
23266 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 33 2c  ,             3,
23267 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46 75 6e   0, 0, substrFun
23268 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  c       ),.    F
23269 55 4e 43 54 49 4f 4e 28 61 62 73 2c 20 20 20 20  UNCTION(abs,    
2326a 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
2326b 2c 20 30 2c 20 61 62 73 46 75 6e 63 20 20 20 20  , 0, absFunc    
2326c 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
2326d 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20  CTION(round,    
2326e 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
2326f 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20  0, roundFunc    
23270 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
23271 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 20  ION(round,      
23272 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c          2, 0, 0,
23273 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 20   roundFunc      
23274 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
23275 4e 28 75 70 70 65 72 2c 20 20 20 20 20 20 20 20  N(upper,        
23276 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 75        1, 0, 0, u
23277 70 70 65 72 46 75 6e 63 20 20 20 20 20 20 20 20  pperFunc        
23278 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
23279 6c 6f 77 65 72 2c 20 20 20 20 20 20 20 20 20 20  lower,          
2327a 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 6f 77      1, 0, 0, low
2327b 65 72 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c  erFunc        ),
2327c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f  .    FUNCTION(co
2327d 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 20  alesce,         
2327e 20 20 31 2c 20 30 2c 20 30 2c 20 30 20 20 20 20    1, 0, 0, 0    
2327f 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20              ),. 
23280 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c     FUNCTION(coal
23281 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 20 2d  esce,          -
23282 31 2c 20 30 2c 20 30 2c 20 69 66 6e 75 6c 6c 46  1, 0, 0, ifnullF
23283 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20  unc       ),.   
23284 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73   FUNCTION(coales
23285 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 30 2c  ce,           0,
23286 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20   0, 0, 0        
23287 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
23288 55 4e 43 54 49 4f 4e 28 68 65 78 2c 20 20 20 20  UNCTION(hex,    
23289 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
2328a 2c 20 30 2c 20 68 65 78 46 75 6e 63 20 20 20 20  , 0, hexFunc    
2328b 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
2328c 43 54 49 4f 4e 28 69 66 6e 75 6c 6c 2c 20 20 20  CTION(ifnull,   
2328d 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20            2, 0, 
2328e 31 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 20 20 20  1, ifnullFunc   
2328f 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
23290 49 4f 4e 28 72 61 6e 64 6f 6d 2c 20 20 20 20 20  ION(random,     
23291 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c         -1, 0, 0,
23292 20 72 61 6e 64 6f 6d 46 75 6e 63 20 20 20 20 20   randomFunc     
23293 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
23294 4e 28 72 61 6e 64 6f 6d 62 6c 6f 62 2c 20 20 20  N(randomblob,   
23295 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 72        1, 0, 0, r
23296 61 6e 64 6f 6d 42 6c 6f 62 20 20 20 20 20 20 20  andomBlob       
23297 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
23298 6e 75 6c 6c 69 66 2c 20 20 20 20 20 20 20 20 20  nullif,         
23299 20 20 20 20 32 2c 20 30 2c 20 31 2c 20 6e 75 6c      2, 0, 1, nul
2329a 6c 69 66 46 75 6e 63 20 20 20 20 20 20 20 29 2c  lifFunc       ),
2329b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71  .    FUNCTION(sq
2329c 6c 69 74 65 5f 76 65 72 73 69 6f 6e 2c 20 20 20  lite_version,   
2329d 20 20 30 2c 20 30 2c 20 30 2c 20 76 65 72 73 69    0, 0, 0, versi
2329e 6f 6e 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20  onFunc      ),. 
2329f 20 20 20 46 55 4e 43 54 49 4f 4e 28 71 75 6f 74     FUNCTION(quot
232a0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
232a1 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74 65 46 75  1, 0, 0, quoteFu
232a2 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  nc        ),.   
232a3 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73 74 5f 69   FUNCTION(last_i
232a4 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20 20 30 2c  nsert_rowid,  0,
232a5 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69 6e 73 65   0, 0, last_inse
232a6 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20 20 20 46  rt_rowid),.    F
232a7 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67 65 73 2c  UNCTION(changes,
232a8 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30              0, 0
232a9 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 20 20 20  , 0, changes    
232aa 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
232ab 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63 68 61 6e  CTION(total_chan
232ac 67 65 73 2c 20 20 20 20 20 20 30 2c 20 30 2c 20  ges,      0, 0, 
232ad 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  0, total_changes
232ae 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
232af 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20 20 20 20  ION(replace,    
232b0 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30 2c          3, 0, 0,
232b1 20 72 65 70 6c 61 63 65 46 75 6e 63 20 20 20 20   replaceFunc    
232b2 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
232b3 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20 20 20 20  N(zeroblob,     
232b4 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 7a        1, 0, 0, z
232b5 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20 20 20  eroblobFunc     
232b6 29 2c 0a 20 20 23 69 66 64 65 66 20 53 51 4c 49  ),.  #ifdef SQLI
232b7 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 20 20 46  TE_SOUNDEX.    F
232b8 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64 65 78 2c  UNCTION(soundex,
232b9 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
232ba 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46 75 6e 63  , 0, soundexFunc
232bb 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64 69        ),.  #endi
232bc 66 0a 20 20 23 69 66 6e 64 65 66 20 53 51 4c 49  f.  #ifndef SQLI
232bd 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
232be 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55 4e 43 54  ENSION.    FUNCT
232bf 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69  ION(load_extensi
232c0 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c 20 30 2c  on,     1, 0, 0,
232c1 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 20   loadExt        
232c2 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
232c3 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  N(load_extension
232c4 2c 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 6c  ,     2, 0, 0, l
232c5 6f 61 64 45 78 74 20 20 20 20 20 20 20 20 20 20  oadExt          
232c6 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 20 20  ),.  #endif.    
232c7 41 47 47 52 45 47 41 54 45 28 73 75 6d 2c 20 20  AGGREGATE(sum,  
232c8 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
232c9 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20  0, 0, sumStep,  
232ca 20 20 20 20 20 20 20 73 75 6d 46 69 6e 61 6c 69         sumFinali
232cb 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47  ze    ),.    AGG
232cc 52 45 47 41 54 45 28 74 6f 74 61 6c 2c 20 20 20  REGATE(total,   
232cd 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
232ce 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20  0, sumStep,     
232cf 20 20 20 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a      totalFinaliz
232d0 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52  e    ),.    AGGR
232d1 45 47 41 54 45 28 61 76 67 2c 20 20 20 20 20 20  EGATE(avg,      
232d2 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
232d3 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20 20  , sumStep,      
232d4 20 20 20 61 76 67 46 69 6e 61 6c 69 7a 65 20 20     avgFinalize  
232d5 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41    ),.    AGGREGA
232d6 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 20  TE(count,       
232d7 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63        0, 0, 0, c
232d8 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 20  ountStep,       
232d9 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29  countFinalize  )
232da 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28  ,.    AGGREGATE(
232db 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 20 20  count,          
232dc 20 20 20 31 2c 20 30 2c 20 30 2c 20 63 6f 75 6e     1, 0, 0, coun
232dd 74 53 74 65 70 2c 20 20 20 20 20 20 20 63 6f 75  tStep,       cou
232de 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29 2c 0a 20  ntFinalize  ),. 
232df 20 20 20 41 47 47 52 45 47 41 54 45 28 67 72 6f     AGGREGATE(gro
232e0 75 70 5f 63 6f 6e 63 61 74 2c 20 20 20 20 20 2d  up_concat,     -
232e1 31 2c 20 30 2c 20 30 2c 20 67 72 6f 75 70 43 6f  1, 0, 0, groupCo
232e2 6e 63 61 74 53 74 65 70 2c 20 67 72 6f 75 70 43  ncatStep, groupC
232e3 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65 29 2c 0a  oncatFinalize),.
232e4 20 20 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28    .    LIKEFUNC(
232e5 67 6c 6f 62 2c 20 32 2c 20 26 67 6c 6f 62 49 6e  glob, 2, &globIn
232e6 66 6f 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  fo, SQLITE_FUNC_
232e7 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43  LIKE|SQLITE_FUNC
232e8 5f 43 41 53 45 29 2c 0a 20 20 23 69 66 64 65 66  _CASE),.  #ifdef
232e9 20 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e   SQLITE_CASE_SEN
232ea 53 49 54 49 56 45 5f 4c 49 4b 45 0a 20 20 20 20  SITIVE_LIKE.    
232eb 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32  LIKEFUNC(like, 2
232ec 2c 20 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20  , &likeInfoAlt, 
232ed 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
232ee 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53  |SQLITE_FUNC_CAS
232ef 45 29 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43  E),.    LIKEFUNC
232f0 28 6c 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49  (like, 3, &likeI
232f1 6e 66 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46  nfoAlt, SQLITE_F
232f2 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f  UNC_LIKE|SQLITE_
232f3 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 23 65  FUNC_CASE),.  #e
232f4 6c 73 65 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43  lse.    LIKEFUNC
232f5 28 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49  (like, 2, &likeI
232f6 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f  nfoNorm, SQLITE_
232f7 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 20 20  FUNC_LIKE),.    
232f8 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33  LIKEFUNC(like, 3
232f9 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c  , &likeInfoNorm,
232fa 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b   SQLITE_FUNC_LIK
232fb 45 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 7d  E),.  #endif.  }
232fc 3b 0a 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75  ;..  int i;.  Fu
232fd 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
232fe 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44   = &GLOBAL(FuncD
232ff 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47  efHash, sqlite3G
23300 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b  lobalFunctions);
23301 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e  .  FuncDef *aFun
23302 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47  c = (FuncDef*)&G
23303 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61  LOBAL(FuncDef, a
23304 42 75 69 6c 74 69 6e 46 75 6e 63 29 3b 0a 0a 20  BuiltinFunc);.. 
23305 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61   for(i=0; i<Arra
23306 79 53 69 7a 65 28 61 42 75 69 6c 74 69 6e 46 75  ySize(aBuiltinFu
23307 6e 63 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  nc); i++){.    s
23308 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73  qlite3FuncDefIns
23309 65 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e  ert(pHash, &aFun
2330a 63 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  c[i]);.  }.  sql
2330b 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65  ite3RegisterDate
2330c 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 3b  TimeFunctions();
2330d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
2330e 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 2e  *** End of func.
2330f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
23310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23311 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23312 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
23313 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 69  *** Begin file i
23314 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  nsert.c ********
23315 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23316 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23317 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
23318 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
23319 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2331a 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2331b 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2331c 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2331d 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2331e 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2331f 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
23320 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
23321 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
23322 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
23323 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
23324 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
23325 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
23326 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
23327 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
23328 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
23329 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2332a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2332b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2332c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2332d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2332e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
2332f 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
23330 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73   C code routines
23331 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64   that are called
23332 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a   by the parser.*
23333 2a 20 74 6f 20 68 61 6e 64 6c 65 20 49 4e 53 45  * to handle INSE
23334 52 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  RT statements in
23335 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24   SQLite..**.** $
23336 49 64 3a 20 69 6e 73 65 72 74 2e 63 2c 76 20 31  Id: insert.c,v 1
23337 2e 32 35 36 20 32 30 30 38 2f 31 32 2f 31 30 20  .256 2008/12/10 
23338 32 31 3a 31 39 3a 35 37 20 64 72 68 20 45 78 70  21:19:57 drh Exp
23339 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   $.*/../*.** Set
2333a 20 50 34 20 6f 66 20 74 68 65 20 6d 6f 73 74 20   P4 of the most 
2333b 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65  recently inserte
2333c 64 20 6f 70 63 6f 64 65 20 74 6f 20 61 20 63 6f  d opcode to a co
2333d 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a  lumn affinity.**
2333e 20 73 74 72 69 6e 67 20 66 6f 72 20 69 6e 64 65   string for inde
2333f 78 20 70 49 64 78 2e 20 41 20 63 6f 6c 75 6d 6e  x pIdx. A column
23340 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
23341 20 68 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74   has one charact
23342 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 63  er.** for each c
23343 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 74 61 62  olumn in the tab
23344 6c 65 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  le, according to
23345 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
23346 20 74 68 65 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a   the column:.**.
23347 2a 2a 20 20 43 68 61 72 61 63 74 65 72 20 20 20  **  Character   
23348 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69     Column affini
23349 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ty.**  ---------
2334a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2334b 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20  -----.**  'a'   
2334c 20 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a           TEXT.**
2334d 20 20 27 62 27 20 20 20 20 20 20 20 20 20 20 20    'b'           
2334e 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20   NONE.**  'c'   
2334f 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43           NUMERIC
23350 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20 20  .**  'd'        
23351 20 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20      INTEGER.**  
23352 27 65 27 20 20 20 20 20 20 20 20 20 20 20 20 52  'e'            R
23353 45 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 74  EAL.**.** An ext
23354 72 61 20 27 62 27 20 69 73 20 61 70 70 65 6e 64  ra 'b' is append
23355 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ed to the end of
23356 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63   the string to c
23357 6f 76 65 72 20 74 68 65 0a 2a 2a 20 72 6f 77 69  over the.** rowi
23358 64 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  d that appears a
23359 73 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  s the last colum
2335a 6e 20 69 6e 20 65 76 65 72 79 20 69 6e 64 65 78  n in every index
2335b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2335c 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2335d 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72  IndexAffinityStr
2335e 28 56 64 62 65 20 2a 76 2c 20 49 6e 64 65 78 20  (Vdbe *v, Index 
2335f 2a 70 49 64 78 29 7b 0a 20 20 69 66 28 20 21 70  *pIdx){.  if( !p
23360 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a  Idx->zColAff ){.
23361 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74      /* The first
23362 20 74 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61   time a column a
23363 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66  ffinity string f
23364 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  or a particular 
23365 69 6e 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20  index is.    ** 
23366 72 65 71 75 69 72 65 64 2c 20 69 74 20 69 73 20  required, it is 
23367 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f  allocated and po
23368 70 75 6c 61 74 65 64 20 68 65 72 65 2e 20 49 74  pulated here. It
23369 20 69 73 20 74 68 65 6e 20 73 74 6f 72 65 64 20   is then stored 
2336a 61 73 0a 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62  as.    ** a memb
2336b 65 72 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20  er of the Index 
2336c 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 75  structure for su
2336d 62 73 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 20  bsequent use..  
2336e 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
2336f 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
23370 73 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e  string will even
23371 74 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65  tually be delete
23372 64 20 62 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69  d by.    ** sqli
23373 74 65 44 65 6c 65 74 65 49 6e 64 65 78 28 29 20  teDeleteIndex() 
23374 77 68 65 6e 20 74 68 65 20 49 6e 64 65 78 20 73  when the Index s
23375 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 20  tructure itself 
23376 69 73 20 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a  is cleaned.    *
23377 2a 20 75 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  * up..    */.   
23378 20 69 6e 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c   int n;.    Tabl
23379 65 20 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e  e *pTab = pIdx->
2337a 70 54 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69  pTable;.    sqli
2337b 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
2337c 33 56 64 62 65 44 62 28 76 29 3b 0a 20 20 20 20  3VdbeDb(v);.    
2337d 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20  pIdx->zColAff = 
2337e 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d  (char *)sqlite3M
2337f 61 6c 6c 6f 63 28 70 49 64 78 2d 3e 6e 43 6f 6c  alloc(pIdx->nCol
23380 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 69 66 28 20  umn+2);.    if( 
23381 21 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29  !pIdx->zColAff )
23382 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
23383 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
23384 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
23385 7d 0a 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e  }.    for(n=0; n
23386 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pIdx->nColumn; 
23387 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 70 49 64 78  n++){.      pIdx
23388 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70  ->zColAff[n] = p
23389 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e  Tab->aCol[pIdx->
2338a 61 69 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66  aiColumn[n]].aff
2338b 69 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20  inity;.    }.   
2338c 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e   pIdx->zColAff[n
2338d 2b 2b 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  ++] = SQLITE_AFF
2338e 5f 4e 4f 4e 45 3b 0a 20 20 20 20 70 49 64 78 2d  _NONE;.    pIdx-
2338f 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b  >zColAff[n] = 0;
23390 0a 20 20 7d 0a 20 0a 20 20 73 71 6c 69 74 65 33  .  }. .  sqlite3
23391 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
23392 2d 31 2c 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66  -1, pIdx->zColAf
23393 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  f, 0);.}../*.** 
23394 53 65 74 20 50 34 20 6f 66 20 74 68 65 20 6d 6f  Set P4 of the mo
23395 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65  st recently inse
23396 72 74 65 64 20 6f 70 63 6f 64 65 20 74 6f 20 61  rted opcode to a
23397 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
23398 0a 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72 20 74  .** string for t
23399 61 62 6c 65 20 70 54 61 62 2e 20 41 20 63 6f 6c  able pTab. A col
2339a 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
2339b 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72  ing has one char
2339c 61 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63  acter.** for eac
2339d 68 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 65 64  h column indexed
2339e 20 62 79 20 74 68 65 20 69 6e 64 65 78 2c 20 61   by the index, a
2339f 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
233a0 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 0a  affinity of the.
233a1 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a  ** column:.**.**
233a2 20 20 43 68 61 72 61 63 74 65 72 20 20 20 20 20    Character     
233a3 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   Column affinity
233a4 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**  -----------
233a5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
233a6 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20  ---.**  'a'     
233a7 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20         TEXT.**  
233a8 27 62 27 20 20 20 20 20 20 20 20 20 20 20 20 4e  'b'            N
233a9 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20  ONE.**  'c'     
233aa 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a         NUMERIC.*
233ab 2a 20 20 27 64 27 20 20 20 20 20 20 20 20 20 20  *  'd'          
233ac 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65    INTEGER.**  'e
233ad 27 20 20 20 20 20 20 20 20 20 20 20 20 52 45 41  '            REA
233ae 4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  L.*/.SQLITE_PRIV
233af 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
233b0 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72  TableAffinityStr
233b1 28 56 64 62 65 20 2a 76 2c 20 54 61 62 6c 65 20  (Vdbe *v, Table 
233b2 2a 70 54 61 62 29 7b 0a 20 20 2f 2a 20 54 68 65  *pTab){.  /* The
233b3 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 63 6f   first time a co
233b4 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
233b5 72 69 6e 67 20 66 6f 72 20 61 20 70 61 72 74 69  ring for a parti
233b6 63 75 6c 61 72 20 74 61 62 6c 65 0a 20 20 2a 2a  cular table.  **
233b7 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 69 74   is required, it
233b8 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
233b9 64 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72 65  d populated here
233ba 2e 20 49 74 20 69 73 20 74 68 65 6e 20 0a 20 20  . It is then .  
233bb 2a 2a 20 73 74 6f 72 65 64 20 61 73 20 61 20 6d  ** stored as a m
233bc 65 6d 62 65 72 20 6f 66 20 74 68 65 20 54 61 62  ember of the Tab
233bd 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  le structure for
233be 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 2e   subsequent use.
233bf 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63  .  **.  ** The c
233c0 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73  olumn affinity s
233c1 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74  tring will event
233c2 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64  ually be deleted
233c3 20 62 79 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33   by.  ** sqlite3
233c4 44 65 6c 65 74 65 54 61 62 6c 65 28 29 20 77 68  DeleteTable() wh
233c5 65 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  en the Table str
233c6 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73  ucture itself is
233c7 20 63 6c 65 61 6e 65 64 20 75 70 2e 0a 20 20 2a   cleaned up..  *
233c8 2f 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 7a  /.  if( !pTab->z
233c9 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 63 68  ColAff ){.    ch
233ca 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20 20 20  ar *zColAff;.   
233cb 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69   int i;.    sqli
233cc 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
233cd 33 56 64 62 65 44 62 28 76 29 3b 0a 0a 20 20 20  3VdbeDb(v);..   
233ce 20 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61 72   zColAff = (char
233cf 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   *)sqlite3Malloc
233d0 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a  (pTab->nCol+1);.
233d1 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 41 66 66      if( !zColAff
233d2 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
233d3 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
233d4 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
233d5 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30    }..    for(i=0
233d6 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
233d7 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 43 6f 6c  i++){.      zCol
233d8 41 66 66 5b 69 5d 20 3d 20 70 54 61 62 2d 3e 61  Aff[i] = pTab->a
233d9 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3b  Col[i].affinity;
233da 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c 41  .    }.    zColA
233db 66 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d  ff[pTab->nCol] =
233dc 20 27 5c 30 27 3b 0a 0a 20 20 20 20 70 54 61 62   '\0';..    pTab
233dd 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c  ->zColAff = zCol
233de 41 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  Aff;.  }..  sqli
233df 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
233e0 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 43 6f  v, -1, pTab->zCo
233e1 6c 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  lAff, 0);.}../*.
233e2 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  ** Return non-ze
233e3 72 6f 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  ro if the table 
233e4 70 54 61 62 20 69 6e 20 64 61 74 61 62 61 73 65  pTab in database
233e5 20 69 44 62 20 6f 72 20 61 6e 79 20 6f 66 20 69   iDb or any of i
233e6 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 68 61  ts indices.** ha
233e7 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 61  ve been opened a
233e8 74 20 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74  t any point in t
233e9 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20  he VDBE program 
233ea 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 6c 6f 63  beginning at loc
233eb 61 74 69 6f 6e 0a 2a 2a 20 69 53 74 61 72 74 41  ation.** iStartA
233ec 64 64 72 20 74 68 72 6f 75 67 68 74 20 74 68 65  ddr throught the
233ed 20 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67   end of the prog
233ee 72 61 6d 2e 20 20 54 68 69 73 20 69 73 20 75 73  ram.  This is us
233ef 65 64 20 74 6f 20 73 65 65 20 69 66 20 0a 2a 2a  ed to see if .**
233f0 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20   a statement of 
233f1 74 68 65 20 66 6f 72 6d 20 20 22 49 4e 53 45 52  the form  "INSER
233f2 54 20 49 4e 54 4f 20 3c 69 44 62 2c 20 70 54 61  T INTO <iDb, pTa
233f3 62 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20 63  b> SELECT ..." c
233f4 61 6e 20 0a 2a 2a 20 72 75 6e 20 77 69 74 68 6f  an .** run witho
233f5 75 74 20 75 73 69 6e 67 20 74 65 6d 70 6f 72 61  ut using tempora
233f6 72 79 20 74 61 62 6c 65 20 66 6f 72 20 74 68 65  ry table for the
233f7 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
233f8 53 45 4c 45 43 54 2e 20 0a 2a 2f 0a 73 74 61 74  SELECT. .*/.stat
233f9 69 63 20 69 6e 74 20 72 65 61 64 73 54 61 62 6c  ic int readsTabl
233fa 65 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69  e(Vdbe *v, int i
233fb 53 74 61 72 74 41 64 64 72 2c 20 69 6e 74 20 69  StartAddr, int i
233fc 44 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29  Db, Table *pTab)
233fd 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
233fe 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56   iEnd = sqlite3V
233ff 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
23400 29 3b 0a 20 20 66 6f 72 28 69 3d 69 53 74 61 72  );.  for(i=iStar
23401 74 41 64 64 72 3b 20 69 3c 69 45 6e 64 3b 20 69  tAddr; i<iEnd; i
23402 2b 2b 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20  ++){.    VdbeOp 
23403 2a 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  *pOp = sqlite3Vd
23404 62 65 47 65 74 4f 70 28 76 2c 20 69 29 3b 0a 20  beGetOp(v, i);. 
23405 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 21 3d     assert( pOp!=
23406 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70  0 );.    if( pOp
23407 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65  ->opcode==OP_Ope
23408 6e 52 65 61 64 20 26 26 20 70 4f 70 2d 3e 70 33  nRead && pOp->p3
23409 3d 3d 69 44 62 20 29 7b 0a 20 20 20 20 20 20 49  ==iDb ){.      I
2340a 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20  ndex *pIndex;.  
2340b 20 20 20 20 69 6e 74 20 74 6e 75 6d 20 3d 20 70      int tnum = p
2340c 4f 70 2d 3e 70 32 3b 0a 20 20 20 20 20 20 69 66  Op->p2;.      if
2340d 28 20 74 6e 75 6d 3d 3d 70 54 61 62 2d 3e 74 6e  ( tnum==pTab->tn
2340e 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  um ){.        re
2340f 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
23410 20 20 20 20 20 20 66 6f 72 28 70 49 6e 64 65 78        for(pIndex
23411 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
23412 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49  Index; pIndex=pI
23413 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  ndex->pNext){.  
23414 20 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d        if( tnum==
23415 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 29 7b 0a  pIndex->tnum ){.
23416 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
23417 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
23418 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e      }.    }.#ifn
23419 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2341a 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
2341b 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
2341c 3d 3d 4f 50 5f 56 4f 70 65 6e 20 26 26 20 70 4f  ==OP_VOpen && pO
2341d 70 2d 3e 70 34 2e 70 56 74 61 62 3d 3d 70 54 61  p->p4.pVtab==pTa
2341e 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20 20  b->pVtab ){.    
2341f 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
23420 34 2e 70 56 74 61 62 21 3d 30 20 29 3b 0a 20 20  4.pVtab!=0 );.  
23421 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
23422 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42  >p4type==P4_VTAB
23423 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
23424 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66   1;.    }.#endif
23425 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
23426 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
23427 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
23428 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 57 72 69 74  EMENT./*.** Writ
23429 65 20 6f 75 74 20 63 6f 64 65 20 74 6f 20 69 6e  e out code to in
2342a 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 74  itialize the aut
2342b 6f 69 6e 63 72 65 6d 65 6e 74 20 6c 6f 67 69 63  oincrement logic
2342c 2e 20 20 54 68 69 73 20 63 6f 64 65 0a 2a 2a 20  .  This code.** 
2342d 6c 6f 6f 6b 73 20 75 70 20 74 68 65 20 63 75 72  looks up the cur
2342e 72 65 6e 74 20 61 75 74 6f 69 6e 63 72 65 6d 65  rent autoincreme
2342f 6e 74 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  nt value in the 
23430 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 0a  sqlite_sequence.
23431 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f  ** table and sto
23432 72 65 73 20 74 68 61 74 20 76 61 6c 75 65 20 69  res that value i
23433 6e 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 43  n a register.  C
23434 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79  ode generated by
23435 0a 2a 2a 20 61 75 74 6f 49 6e 63 53 74 65 70 28  .** autoIncStep(
23436 29 20 77 69 6c 6c 20 6b 65 65 70 20 74 68 61 74  ) will keep that
23437 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   register holdin
23438 67 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a  g the largest.**
23439 20 72 6f 77 69 64 20 76 61 6c 75 65 2e 20 20 43   rowid value.  C
2343a 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79  ode generated by
2343b 20 61 75 74 6f 49 6e 63 45 6e 64 28 29 20 77 69   autoIncEnd() wi
2343c 6c 6c 20 77 72 69 74 65 20 74 68 65 20 6e 65 77  ll write the new
2343d 0a 2a 2a 20 6c 61 72 67 65 73 74 20 76 61 6c 75  .** largest valu
2343e 65 20 6f 66 20 74 68 65 20 63 6f 75 6e 74 65 72  e of the counter
2343f 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 73   back into the s
23440 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74  qlite_sequence t
23441 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  able..**.** This
23442 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
23443 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
23444 65 20 6d 65 6d 5b 5d 20 63 65 6c 6c 20 74 68 61  e mem[] cell tha
23445 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68  t contains.** th
23446 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20  e maximum rowid 
23447 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  counter..**.** T
23448 68 72 65 65 20 63 6f 6e 73 65 63 75 74 69 76 65  hree consecutive
23449 20 72 65 67 69 73 74 65 72 73 20 61 72 65 20 61   registers are a
2344a 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73  llocated by this
2344b 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 0a 2a   routine.  The.*
2344c 2a 20 66 69 72 73 74 20 74 77 6f 20 68 6f 6c 64  * first two hold
2344d 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
2344e 20 74 61 72 67 65 74 20 74 61 62 6c 65 20 61 6e   target table an
2344f 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f  d the maximum ro
23450 77 69 64 20 0a 2a 2a 20 69 6e 73 65 72 74 65 64  wid .** inserted
23451 20 69 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74   into the target
23452 20 74 61 62 6c 65 2c 20 72 65 73 70 65 63 74 69   table, respecti
23453 76 65 6c 79 2e 0a 2a 2a 20 54 68 65 20 74 68 69  vely..** The thi
23454 72 64 20 68 6f 6c 64 73 20 74 68 65 20 72 6f 77  rd holds the row
23455 69 64 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71  id in sqlite_seq
23456 75 65 6e 63 65 20 77 68 65 72 65 20 77 65 20 77  uence where we w
23457 69 6c 6c 0a 2a 2a 20 77 72 69 74 65 20 62 61 63  ill.** write bac
23458 6b 20 74 68 65 20 72 65 76 69 73 65 64 20 6d 61  k the revised ma
23459 78 69 6d 75 6d 20 72 6f 77 69 64 2e 20 20 54 68  ximum rowid.  Th
2345a 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
2345b 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20  ns the.** index 
2345c 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 66  of the second of
2345d 20 74 68 65 73 65 20 74 68 72 65 65 20 72 65 67   these three reg
2345e 69 73 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69  isters..*/.stati
2345f 63 20 69 6e 74 20 61 75 74 6f 49 6e 63 42 65 67  c int autoIncBeg
23460 69 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  in(.  Parse *pPa
23461 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72  rse,      /* Par
23462 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
23463 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20    int iDb,      
23464 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
23465 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  f the database h
23466 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20  olding pTab */. 
23467 20 54 61 62 6c 65 20 2a 70 54 61 62 20 20 20 20   Table *pTab    
23468 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
23469 65 20 77 65 20 61 72 65 20 77 72 69 74 69 6e 67  e we are writing
2346a 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20   to */.){.  int 
2346b 6d 65 6d 49 64 20 3d 20 30 3b 20 20 20 20 20 20  memId = 0;      
2346c 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
2346d 69 6e 67 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69  ing maximum rowi
2346e 64 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d  d */.  if( pTab-
2346f 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41  >tabFlags & TF_A
23470 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a  utoincrement ){.
23471 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50      Vdbe *v = pP
23472 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20  arse->pVdbe;.   
23473 20 44 62 20 2a 70 44 62 20 3d 20 26 70 50 61 72   Db *pDb = &pPar
23474 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d  se->db->aDb[iDb]
23475 3b 0a 20 20 20 20 69 6e 74 20 69 43 75 72 20 3d  ;.    int iCur =
23476 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20   pParse->nTab;. 
23477 20 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20     int addr;    
23478 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
23479 64 72 65 73 73 20 6f 66 20 74 68 65 20 74 6f 70  dress of the top
2347a 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a   of the loop */.
2347b 20 20 20 20 61 73 73 65 72 74 28 20 76 20 29 3b      assert( v );
2347c 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
2347d 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  m++;         /* 
2347e 48 6f 6c 64 73 20 6e 61 6d 65 20 6f 66 20 74 61  Holds name of ta
2347f 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 49 64  ble */.    memId
23480 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
23481 6d 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  m;.    pParse->n
23482 4d 65 6d 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74  Mem++;.    sqlit
23483 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
23484 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c 20 70  se, iCur, iDb, p
23485 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65  Db->pSchema->pSe
23486 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  qTab, OP_OpenRea
23487 64 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73  d);.    addr = s
23488 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
23489 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71  tAddr(v);.    sq
2348a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2348b 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
2348c 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c 20 70 54  , memId-1, 0, pT
2348d 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ab->zName, 0);. 
2348e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2348f 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
23490 64 2c 20 69 43 75 72 2c 20 61 64 64 72 2b 39 29  d, iCur, addr+9)
23491 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23492 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
23493 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 30 2c 20 6d  lumn, iCur, 0, m
23494 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  emId);.    sqlit
23495 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
23496 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31 2c 20  OP_Ne, memId-1, 
23497 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64 29 3b 0a  addr+7, memId);.
23498 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
23499 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
2349a 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
2349b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2349c 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64  dOp2(v, OP_Rowid
2349d 2c 20 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31 29  , iCur, memId+1)
2349e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2349f 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
234a0 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 31 2c 20 6d  lumn, iCur, 1, m
234a1 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  emId);.    sqlit
234a2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
234a3 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72  OP_Goto, 0, addr
234a4 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +9);.    sqlite3
234a5 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
234a6 5f 4e 65 78 74 2c 20 69 43 75 72 2c 20 61 64 64  _Next, iCur, add
234a7 72 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  r+2);.    sqlite
234a8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
234a9 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d 65  P_Integer, 0, me
234aa 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  mId);.    sqlite
234ab 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
234ac 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30  P_Close, iCur, 0
234ad 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
234ae 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  memId;.}../*.** 
234af 55 70 64 61 74 65 20 74 68 65 20 6d 61 78 69 6d  Update the maxim
234b0 75 6d 20 72 6f 77 69 64 20 66 6f 72 20 61 6e 20  um rowid for an 
234b1 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63 61  autoincrement ca
234b2 6c 63 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  lculation..**.**
234b3 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
234b4 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77  ould be called w
234b5 68 65 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74  hen the top of t
234b6 68 65 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61  he stack holds a
234b7 0a 2a 2a 20 6e 65 77 20 72 6f 77 69 64 20 74 68  .** new rowid th
234b8 61 74 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62  at is about to b
234b9 65 20 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20  e inserted.  If 
234ba 74 68 61 74 20 6e 65 77 20 72 6f 77 69 64 20 69  that new rowid i
234bb 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e  s.** larger than
234bc 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77   the maximum row
234bd 69 64 20 69 6e 20 74 68 65 20 6d 65 6d 49 64 20  id in the memId 
234be 6d 65 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68 65  memory cell, the
234bf 6e 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  n the.** memory 
234c0 63 65 6c 6c 20 69 73 20 75 70 64 61 74 65 64 2e  cell is updated.
234c1 20 20 54 68 65 20 73 74 61 63 6b 20 69 73 20 75    The stack is u
234c2 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61  nchanged..*/.sta
234c3 74 69 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63  tic void autoInc
234c4 53 74 65 70 28 50 61 72 73 65 20 2a 70 50 61 72  Step(Parse *pPar
234c5 73 65 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20 69  se, int memId, i
234c6 6e 74 20 72 65 67 52 6f 77 69 64 29 7b 0a 20 20  nt regRowid){.  
234c7 69 66 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a 20  if( memId>0 ){. 
234c8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
234c9 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64  dOp2(pParse->pVd
234ca 62 65 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d  be, OP_MemMax, m
234cb 65 6d 49 64 2c 20 72 65 67 52 6f 77 69 64 29 3b  emId, regRowid);
234cc 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 66  .  }.}../*.** Af
234cd 74 65 72 20 64 6f 69 6e 67 20 6f 6e 65 20 6f 72  ter doing one or
234ce 20 6d 6f 72 65 20 69 6e 73 65 72 74 73 2c 20 74   more inserts, t
234cf 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  he maximum rowid
234d0 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e   is stored.** in
234d1 20 72 65 67 5b 6d 65 6d 49 64 5d 2e 20 20 47 65   reg[memId].  Ge
234d2 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77  nerate code to w
234d3 72 69 74 65 20 74 68 69 73 20 76 61 6c 75 65 20  rite this value 
234d4 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 0a 2a 2a  back into the.**
234d5 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
234d6 65 6e 63 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  ence table..*/.s
234d7 74 61 74 69 63 20 76 6f 69 64 20 61 75 74 6f 49  tatic void autoI
234d8 6e 63 45 6e 64 28 0a 20 20 50 61 72 73 65 20 2a  ncEnd(.  Parse *
234d9 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 54  pParse,     /* T
234da 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
234db 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
234dc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
234dd 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62  dex of the datab
234de 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62  ase holding pTab
234df 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
234e0 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c  b,       /* Tabl
234e1 65 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  e we are inserti
234e2 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74  ng into */.  int
234e3 20 6d 65 6d 49 64 20 20 20 20 20 20 20 20 20 20   memId          
234e4 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 68  /* Memory cell h
234e5 6f 6c 64 69 6e 67 20 74 68 65 20 6d 61 78 69 6d  olding the maxim
234e6 75 6d 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20  um rowid */.){. 
234e7 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c   if( pTab->tabFl
234e8 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63  ags & TF_Autoinc
234e9 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e  rement ){.    in
234ea 74 20 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d  t iCur = pParse-
234eb 3e 6e 54 61 62 3b 0a 20 20 20 20 56 64 62 65 20  >nTab;.    Vdbe 
234ec 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
234ed 62 65 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20  be;.    Db *pDb 
234ee 3d 20 26 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61  = &pParse->db->a
234ef 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74  Db[iDb];.    int
234f0 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20 69 52 65   j1;.    int iRe
234f1 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  c = ++pParse->nM
234f2 65 6d 3b 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79  em;    /* Memory
234f3 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 72   cell used for r
234f4 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 20 20 61 73  ecord */..    as
234f5 73 65 72 74 28 20 76 20 29 3b 0a 20 20 20 20 73  sert( v );.    s
234f6 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
234f7 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44  pParse, iCur, iD
234f8 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  b, pDb->pSchema-
234f9 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65  >pSeqTab, OP_Ope
234fa 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 6a 31 20  nWrite);.    j1 
234fb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
234fc 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c  Op1(v, OP_NotNul
234fd 6c 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20  l, memId+1);.   
234fe 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
234ff 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
23500 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31  d, iCur, memId+1
23501 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23502 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
23503 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23504 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
23505 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64  akeRecord, memId
23506 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b 0a 20 20  -1, 2, iRec);.  
23507 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23508 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
23509 2c 20 69 43 75 72 2c 20 69 52 65 63 2c 20 6d 65  , iCur, iRec, me
2350a 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  mId+1);.    sqli
2350b 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
2350c 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  v, OPFLAG_APPEND
2350d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2350e 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
2350f 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d  lose, iCur);.  }
23510 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49  .}.#else./*.** I
23511 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
23512 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64  TOINCREMENT is d
23513 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
23514 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a   three routines.
23515 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c  ** above are all
23516 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66   no-ops.*/.# def
23517 69 6e 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  ine autoIncBegin
23518 28 41 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65  (A,B,C) (0).# de
23519 66 69 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70  fine autoIncStep
2351a 28 41 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e 65  (A,B,C).# define
2351b 20 61 75 74 6f 49 6e 63 45 6e 64 28 41 2c 42 2c   autoIncEnd(A,B,
2351c 43 2c 44 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53  C,D).#endif /* S
2351d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
2351e 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a  NCREMENT */.../*
2351f 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
23520 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69  tion */.static i
23521 6e 74 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74  nt xferOptimizat
23522 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
23523 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
23524 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
23525 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74  /.  Table *pDest
23526 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
23527 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e   table we are in
23528 73 65 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a  serting into */.
23529 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
2352a 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c  t,      /* A SEL
2352b 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
2352c 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61   use as the data
2352d 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74   source */.  int
2352e 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20   onError,       
2352f 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e     /* How to han
23530 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65  dle constraint e
23531 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69  rrors */.  int i
23532 44 62 44 65 73 74 20 20 20 20 20 20 20 20 20 20  DbDest          
23533 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
23534 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a   of pDest */.);.
23535 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
23536 69 6e 65 20 69 73 20 63 61 6c 6c 20 74 6f 20 68  ine is call to h
23537 61 6e 64 6c 65 20 53 51 4c 20 6f 66 20 74 68 65  andle SQL of the
23538 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73   following forms
23539 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72  :.**.**    inser
2353a 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44  t into TABLE (ID
2353b 4c 49 53 54 29 20 76 61 6c 75 65 73 28 45 58 50  LIST) values(EXP
2353c 52 4c 49 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73  RLIST).**    ins
2353d 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28  ert into TABLE (
2353e 49 44 4c 49 53 54 29 20 73 65 6c 65 63 74 0a 2a  IDLIST) select.*
2353f 2a 0a 2a 2a 20 54 68 65 20 49 44 4c 49 53 54 20  *.** The IDLIST 
23540 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 61  following the ta
23541 62 6c 65 20 6e 61 6d 65 20 69 73 20 61 6c 77 61  ble name is alwa
23542 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66  ys optional.  If
23543 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65   omitted,.** the
23544 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
23545 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20  columns for the 
23546 74 61 62 6c 65 20 69 73 20 73 75 62 73 74 69 74  table is substit
23547 75 74 65 64 2e 20 20 54 68 65 20 49 44 4c 49 53  uted.  The IDLIS
23548 54 0a 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20  T.** appears in 
23549 74 68 65 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61  the pColumn para
2354a 6d 65 74 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20  meter.  pColumn 
2354b 69 73 20 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53  is NULL if IDLIS
2354c 54 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a  T is omitted..**
2354d 0a 2a 2a 20 54 68 65 20 70 4c 69 73 74 20 70 61  .** The pList pa
2354e 72 61 6d 65 74 65 72 20 68 6f 6c 64 73 20 45 58  rameter holds EX
2354f 50 52 4c 49 53 54 20 69 6e 20 74 68 65 20 66 69  PRLIST in the fi
23550 72 73 74 20 66 6f 72 6d 20 6f 66 20 74 68 65 20  rst form of the 
23551 49 4e 53 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d  INSERT.** statem
23552 65 6e 74 20 61 62 6f 76 65 2c 20 61 6e 64 20 70  ent above, and p
23553 53 65 6c 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20  Select is NULL. 
23554 20 46 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20   For the second 
23555 66 6f 72 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a  form, pList is.*
23556 2a 20 4e 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65  * NULL and pSele
23557 63 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ct is a pointer 
23558 74 6f 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  to the select st
23559 61 74 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20  atement used to 
2355a 67 65 6e 65 72 61 74 65 0a 2a 2a 20 64 61 74 61  generate.** data
2355b 20 66 6f 72 20 74 68 65 20 69 6e 73 65 72 74 2e   for the insert.
2355c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
2355d 67 65 6e 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77  generated follow
2355e 73 20 6f 6e 65 20 6f 66 20 66 6f 75 72 20 74 65  s one of four te
2355f 6d 70 6c 61 74 65 73 2e 20 20 46 6f 72 20 61 20  mplates.  For a 
23560 73 69 6d 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74  simple.** select
23561 20 77 69 74 68 20 64 61 74 61 20 63 6f 6d 69 6e   with data comin
23562 67 20 66 72 6f 6d 20 61 20 56 41 4c 55 45 53 20  g from a VALUES 
23563 63 6c 61 75 73 65 2c 20 74 68 65 20 63 6f 64 65  clause, the code
23564 20 65 78 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63   executes.** onc
23565 65 20 73 74 72 61 69 67 68 74 20 64 6f 77 6e 20  e straight down 
23566 74 68 72 6f 75 67 68 2e 20 20 50 73 65 75 64 6f  through.  Pseudo
23567 2d 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 28 77  -code follows (w
23568 65 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 74  e call this.** t
23569 68 65 20 22 31 73 74 20 74 65 6d 70 6c 61 74 65  he "1st template
2356a 22 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  "):.**.**       
2356b 20 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72    open write cur
2356c 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61  sor to <table> a
2356d 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a  nd its indices.*
2356e 2a 20 20 20 20 20 20 20 20 20 70 75 74 73 20 56  *         puts V
2356f 41 4c 55 45 53 20 63 6c 61 75 73 65 20 65 78 70  ALUES clause exp
23570 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f 20 74 68  ressions onto th
23571 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20 20 20 20  e stack.**      
23572 20 20 20 77 72 69 74 65 20 74 68 65 20 72 65 73     write the res
23573 75 6c 74 69 6e 67 20 72 65 63 6f 72 64 20 69 6e  ulting record in
23574 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20  to <table>.**   
23575 20 20 20 20 20 20 63 6c 65 61 6e 75 70 0a 2a 2a        cleanup.**
23576 0a 2a 2a 20 54 68 65 20 74 68 72 65 65 20 72 65  .** The three re
23577 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c 61 74 65  maining template
23578 73 20 61 73 73 75 6d 65 20 74 68 65 20 73 74 61  s assume the sta
23579 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65  tement is of the
2357a 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 49 4e   form.**.**   IN
2357b 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65  SERT INTO <table
2357c 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a 2a 2a 0a  > SELECT ....**.
2357d 2a 2a 20 49 66 20 74 68 65 20 53 45 4c 45 43 54  ** If the SELECT
2357e 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20 74 68   clause is of th
2357f 65 20 72 65 73 74 72 69 63 74 65 64 20 66 6f 72  e restricted for
23580 6d 20 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  m "SELECT * FROM
23581 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a 2a 2a 20   <table2>" -.** 
23582 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69  in other words i
23583 66 20 74 68 65 20 53 45 4c 45 43 54 20 70 75 6c  f the SELECT pul
23584 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66  ls all columns f
23585 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62  rom a single tab
23586 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 72 65 20  le.** and there 
23587 69 73 20 6e 6f 20 57 48 45 52 45 20 6f 72 20 4c  is no WHERE or L
23588 49 4d 49 54 20 6f 72 20 47 52 4f 55 50 20 42 59  IMIT or GROUP BY
23589 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61   or ORDER BY cla
2358a 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20 69 66 20  uses, and.** if 
2358b 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20 3c 74 61  <table2> and <ta
2358c 62 6c 65 31 3e 20 61 72 65 20 64 69 73 74 69 6e  ble1> are distin
2358d 63 74 20 74 61 62 6c 65 73 20 62 75 74 20 68 61  ct tables but ha
2358e 76 65 20 69 64 65 6e 74 69 63 61 6c 0a 2a 2a 20  ve identical.** 
2358f 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c 75 64 69  schemas, includi
23590 6e 67 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 20  ng all the same 
23591 69 6e 64 69 63 65 73 2c 20 74 68 65 6e 20 61 20  indices, then a 
23592 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61  special optimiza
23593 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b  tion.** is invok
23594 65 64 20 74 68 61 74 20 63 6f 70 69 65 73 20 72  ed that copies r
23595 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20  aw records from 
23596 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f  <table2> over to
23597 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a 20 53 65   <table1>..** Se
23598 65 20 74 68 65 20 78 66 65 72 4f 70 74 69 6d 69  e the xferOptimi
23599 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f  zation() functio
2359a 6e 20 66 6f 72 20 74 68 65 20 69 6d 70 6c 65 6d  n for the implem
2359b 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  entation of this
2359c 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e 20 20 54  .** template.  T
2359d 68 69 73 20 69 73 20 74 68 65 20 32 6e 64 20 74  his is the 2nd t
2359e 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20  emplate..**.**  
2359f 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72         open a wr
235a0 69 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74  ite cursor to <t
235a1 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20  able>.**        
235a2 20 6f 70 65 6e 20 72 65 61 64 20 63 75 72 73 6f   open read curso
235a3 72 20 6f 6e 20 3c 74 61 62 6c 65 32 3e 0a 2a 2a  r on <table2>.**
235a4 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65           transfe
235a5 72 20 61 6c 6c 20 72 65 63 6f 72 64 73 20 69 6e  r all records in
235a6 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74   <table2> over t
235a7 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20  o <table>.**    
235a8 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72 73 6f       close curso
235a9 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 6f  rs.**         fo
235aa 72 65 61 63 68 20 69 6e 64 65 78 20 6f 6e 20 3c  reach index on <
235ab 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20  table>.**       
235ac 20 20 20 20 6f 70 65 6e 20 61 20 77 72 69 74 65      open a write
235ad 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65 20 3c   cursor on the <
235ae 74 61 62 6c 65 3e 20 69 6e 64 65 78 0a 2a 2a 20  table> index.** 
235af 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61            open a
235b0 20 72 65 61 64 20 63 75 72 73 6f 72 20 6f 6e 20   read cursor on 
235b1 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
235b2 67 20 3c 74 61 62 6c 65 32 3e 20 69 6e 64 65 78  g <table2> index
235b3 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72  .**           tr
235b4 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72  ansfer all recor
235b5 64 73 20 66 72 6f 6d 20 74 68 65 20 72 65 61 64  ds from the read
235b6 20 74 6f 20 74 68 65 20 77 72 69 74 65 20 63 75   to the write cu
235b7 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
235b8 20 20 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73     close cursors
235b9 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20  .**         end 
235ba 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a 2a 20 54 68  foreach.**.** Th
235bb 65 20 33 72 64 20 74 65 6d 70 6c 61 74 65 20 69  e 3rd template i
235bc 73 20 66 6f 72 20 77 68 65 6e 20 74 68 65 20 73  s for when the s
235bd 65 63 6f 6e 64 20 74 65 6d 70 6c 61 74 65 20 64  econd template d
235be 6f 65 73 20 6e 6f 74 20 61 70 70 6c 79 0a 2a 2a  oes not apply.**
235bf 20 61 6e 64 20 74 68 65 20 53 45 4c 45 43 54 20   and the SELECT 
235c0 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20  clause does not 
235c1 72 65 61 64 20 66 72 6f 6d 20 3c 74 61 62 6c 65  read from <table
235c2 3e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a  > at any time..*
235c3 2a 20 54 68 65 20 67 65 6e 65 72 61 74 65 64 20  * The generated 
235c4 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 74 68 69  code follows thi
235c5 73 20 74 65 6d 70 6c 61 74 65 3a 0a 2a 2a 0a 2a  s template:.**.*
235c6 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d  *         EOF <-
235c7 20 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 58 20   0.**         X 
235c8 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20 20 20 20  <- A.**         
235c9 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41  goto B.**      A
235ca 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65 20  : setup for the 
235cb 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
235cc 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20    loop over the 
235cd 72 6f 77 73 20 69 6e 20 74 68 65 20 53 45 4c 45  rows in the SELE
235ce 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  CT.**           
235cf 6c 6f 61 64 20 76 61 6c 75 65 73 20 69 6e 74 6f  load values into
235d0 20 72 65 67 69 73 74 65 72 73 20 52 2e 2e 52 2b   registers R..R+
235d1 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79  n.**           y
235d2 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20  ield X.**       
235d3 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20    end loop.**   
235d4 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66        cleanup af
235d5 74 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a  ter the SELECT.*
235d6 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d  *         EOF <-
235d7 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 79 69   1.**         yi
235d8 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20  eld X.**        
235d9 20 67 6f 74 6f 20 41 0a 2a 2a 20 20 20 20 20 20   goto A.**      
235da 42 3a 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75  B: open write cu
235db 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20  rsor to <table> 
235dc 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a  and its indices.
235dd 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 65 6c 64  **      C: yield
235de 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66   X.**         if
235df 20 45 4f 46 20 67 6f 74 6f 20 44 0a 2a 2a 20 20   EOF goto D.**  
235e0 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 74 68         insert th
235e1 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74 20  e select result 
235e2 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f  into <table> fro
235e3 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20  m R..R+n.**     
235e4 20 20 20 20 67 6f 74 6f 20 43 0a 2a 2a 20 20 20      goto C.**   
235e5 20 20 20 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2a     D: cleanup.**
235e6 0a 2a 2a 20 54 68 65 20 34 74 68 20 74 65 6d 70  .** The 4th temp
235e7 6c 61 74 65 20 69 73 20 75 73 65 64 20 69 66 20  late is used if 
235e8 74 68 65 20 69 6e 73 65 72 74 20 73 74 61 74 65  the insert state
235e9 6d 65 6e 74 20 74 61 6b 65 73 20 69 74 73 0a 2a  ment takes its.*
235ea 2a 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 61 20  * values from a 
235eb 53 45 4c 45 43 54 20 62 75 74 20 74 68 65 20 64  SELECT but the d
235ec 61 74 61 20 69 73 20 62 65 69 6e 67 20 69 6e 73  ata is being ins
235ed 65 72 74 65 64 20 69 6e 74 6f 20 61 20 74 61 62  erted into a tab
235ee 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 6c  le.** that is al
235ef 73 6f 20 72 65 61 64 20 61 73 20 70 61 72 74 20  so read as part 
235f0 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 20 20  of the SELECT.  
235f1 49 6e 20 74 68 65 20 74 68 69 72 64 20 66 6f 72  In the third for
235f2 6d 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f  m,.** we have to
235f3 20 75 73 65 20 61 20 69 6e 74 65 72 6d 65 64 69   use a intermedi
235f4 61 74 65 20 74 61 62 6c 65 20 74 6f 20 73 74 6f  ate table to sto
235f5 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f  re the results o
235f6 66 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 2e  f.** the select.
235f7 20 20 54 68 65 20 74 65 6d 70 6c 61 74 65 20 69    The template i
235f8 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
235f9 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c  **         EOF <
235fa 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 58  - 0.**         X
235fb 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20 20 20   <- A.**        
235fc 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20   goto B.**      
235fd 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65  A: setup for the
235fe 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
235ff 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65     loop over the
23600 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53   tables in the S
23601 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
23602 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e     load value in
23603 74 6f 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52  to register R..R
23604 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  +n.**           
23605 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20  yield X.**      
23606 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20     end loop.**  
23607 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61         cleanup a
23608 66 74 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a  fter the SELECT.
23609 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c  **         EOF <
2360a 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 79  - 1.**         y
2360b 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20  ield X.**       
2360c 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a 2a 2a 20    halt-error.** 
2360d 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d       B: open tem
2360e 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20  p table.**      
2360f 4c 3a 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20  L: yield X.**   
23610 20 20 20 20 20 20 69 66 20 45 4f 46 20 67 6f 74        if EOF got
23611 6f 20 4d 0a 2a 2a 20 20 20 20 20 20 20 20 20 69  o M.**         i
23612 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d 20 52  nsert row from R
23613 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20  ..R+n into temp 
23614 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20  table.**        
23615 20 67 6f 74 6f 20 4c 0a 2a 2a 20 20 20 20 20 20   goto L.**      
23616 4d 3a 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75  M: open write cu
23617 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20  rsor to <table> 
23618 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a  and its indices.
23619 2a 2a 20 20 20 20 20 20 20 20 20 72 65 77 69 6e  **         rewin
2361a 64 20 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20  d temp table.** 
2361b 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65       C: loop ove
2361c 72 20 72 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d  r rows of interm
2361d 65 64 69 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20  ediate table.** 
2361e 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66            transf
2361f 65 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69  er values form i
23620 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
23621 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a  e into <table>.*
23622 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f  *         end lo
23623 6f 70 0a 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c  op.**      D: cl
23624 65 61 6e 75 70 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eanup.*/.SQLITE_
23625 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23626 69 74 65 33 49 6e 73 65 72 74 28 0a 20 20 50 61  ite3Insert(.  Pa
23627 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
23628 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
23629 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
2362a 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20  st *pTabList,   
2362b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c   /* Name of tabl
2362c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20  e into which we 
2362d 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f  are inserting */
2362e 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
2362f 73 74 2c 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  st,      /* List
23630 20 6f 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65   of values to be
23631 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53   inserted */.  S
23632 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
23633 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54       /* A SELECT
23634 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73   statement to us
23635 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f  e as the data so
23636 75 72 63 65 20 2a 2f 0a 20 20 49 64 4c 69 73 74  urce */.  IdList
23637 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20   *pColumn,      
23638 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  /* Column names 
23639 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
2363a 20 49 44 4c 49 53 54 2e 20 2a 2f 0a 20 20 69 6e   IDLIST. */.  in
2363b 74 20 6f 6e 45 72 72 6f 72 20 20 20 20 20 20 20  t onError       
2363c 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61      /* How to ha
2363d 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20  ndle constraint 
2363e 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20 73  errors */.){.  s
2363f 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
23640 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e       /* The main
23641 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
23642 75 72 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ure */.  Table *
23643 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f  pTab;          /
23644 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 69  * The table to i
23645 6e 73 65 72 74 20 69 6e 74 6f 2e 20 20 61 6b 61  nsert into.  aka
23646 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 68 61 72   TABLE */.  char
23647 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 20 20 20   *zTab;         
23648 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
23649 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
2364a 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
2364b 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ng */.  const ch
2364c 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 2f 2a  ar *zDb;      /*
2364d 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
2364e 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68  abase holding th
2364f 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  is table */.  in
23650 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20 20 20 20  t i, j, idx;    
23651 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
23652 74 65 72 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a  ters */.  Vdbe *
23653 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
23654 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
23655 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75   into this virtu
23656 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20  al machine */.  
23657 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
23658 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
23659 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65  ping over indice
2365a 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  s of the table *
2365b 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
2365c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2365d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
2365e 6e 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20  n the data */.  
2365f 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b  int nHidden = 0;
23660 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
23661 6f 66 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e  of hidden column
23662 73 20 69 66 20 54 41 42 4c 45 20 69 73 20 76 69  s if TABLE is vi
23663 72 74 75 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 62  rtual */.  int b
23664 61 73 65 43 75 72 20 3d 20 30 3b 20 20 20 20 20  aseCur = 0;     
23665 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20   /* VDBE Cursor 
23666 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61 62 20  number for pTab 
23667 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 43 6f 6c 75  */.  int keyColu
23668 6d 6e 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 43 6f  mn = -1;   /* Co
23669 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 65  lumn that is the
2366a 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
2366b 20 4b 45 59 20 2a 2f 0a 20 20 69 6e 74 20 65 6e   KEY */.  int en
2366c 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20  dOfLoop;        
2366d 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68 65  /* Label for the
2366e 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 73 65   end of the inse
2366f 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20  rtion loop */.  
23670 69 6e 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65  int useTempTable
23671 20 3d 20 30 3b 20 2f 2a 20 53 74 6f 72 65 20 53   = 0; /* Store S
23672 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20 69 6e  ELECT results in
23673 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61   intermediate ta
23674 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63  ble */.  int src
23675 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  Tab = 0;       /
23676 2a 20 44 61 74 61 20 63 6f 6d 65 73 20 66 72 6f  * Data comes fro
23677 6d 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79  m this temporary
23678 20 63 75 72 73 6f 72 20 69 66 20 3e 3d 30 20 2a   cursor if >=0 *
23679 2f 0a 20 20 69 6e 74 20 61 64 64 72 49 6e 73 54  /.  int addrInsT
2367a 6f 70 20 3d 20 30 3b 20 20 20 2f 2a 20 4a 75 6d  op = 0;   /* Jum
2367b 70 20 74 6f 20 6c 61 62 65 6c 20 22 44 22 20 2a  p to label "D" *
2367c 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74  /.  int addrCont
2367d 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 6f 70   = 0;     /* Top
2367e 20 6f 66 20 69 6e 73 65 72 74 20 6c 6f 6f 70 2e   of insert loop.
2367f 20 4c 61 62 65 6c 20 22 43 22 20 69 6e 20 74 65   Label "C" in te
23680 6d 70 6c 61 74 65 73 20 33 20 61 6e 64 20 34 20  mplates 3 and 4 
23681 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c  */.  int addrSel
23682 65 63 74 20 3d 20 30 3b 20 20 20 2f 2a 20 41 64  ect = 0;   /* Ad
23683 64 72 65 73 73 20 6f 66 20 63 6f 72 6f 75 74 69  dress of corouti
23684 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ne that implemen
23685 74 73 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f  ts the SELECT */
23686 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
23687 73 74 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74  st;      /* Dest
23688 69 6e 61 74 69 6f 6e 20 66 6f 72 20 53 45 4c 45  ination for SELE
23689 43 54 20 6f 6e 20 72 68 73 20 6f 66 20 49 4e 53  CT on rhs of INS
2368a 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77  ERT */.  int new
2368b 49 64 78 20 3d 20 2d 31 3b 20 20 20 20 20 20 2f  Idx = -1;      /
2368c 2a 20 43 75 72 73 6f 72 20 66 6f 72 20 74 68 65  * Cursor for the
2368d 20 4e 45 57 20 70 73 65 75 64 6f 2d 74 61 62 6c   NEW pseudo-tabl
2368e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  e */.  int iDb; 
2368f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23690 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73  Index of databas
23691 65 20 68 6f 6c 64 69 6e 67 20 54 41 42 4c 45 20  e holding TABLE 
23692 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20  */.  Db *pDb;   
23693 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
23694 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  e database conta
23695 69 6e 69 6e 67 20 74 61 62 6c 65 20 62 65 69 6e  ining table bein
23696 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  g inserted into 
23697 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 46  */.  int appendF
23698 6c 61 67 20 3d 20 30 3b 20 20 20 2f 2a 20 54 72  lag = 0;   /* Tr
23699 75 65 20 69 66 20 74 68 65 20 69 6e 73 65 72 74  ue if the insert
2369a 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65   is likely to be
2369b 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 0a 20   an append */.. 
2369c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c   /* Register all
2369d 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  ocations */.  in
2369e 74 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 20  t regFromSelect 
2369f 3d 20 30 3b 2f 2a 20 42 61 73 65 20 72 65 67 69  = 0;/* Base regi
236a0 73 74 65 72 20 66 6f 72 20 64 61 74 61 20 63 6f  ster for data co
236a1 6d 69 6e 67 20 66 72 6f 6d 20 53 45 4c 45 43 54  ming from SELECT
236a2 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74   */.  int regAut
236a3 6f 69 6e 63 20 3d 20 30 3b 20 20 20 2f 2a 20 52  oinc = 0;   /* R
236a4 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
236a5 74 68 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e  the AUTOINCREMEN
236a6 54 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  T counter */.  i
236a7 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d  nt regRowCount =
236a8 20 30 3b 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63   0;  /* Memory c
236a9 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 74 68 65  ell used for the
236aa 20 72 6f 77 20 63 6f 75 6e 74 65 72 20 2a 2f 0a   row counter */.
236ab 20 20 69 6e 74 20 72 65 67 49 6e 73 3b 20 20 20    int regIns;   
236ac 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b          /* Block
236ad 20 6f 66 20 72 65 67 73 20 68 6f 6c 64 69 6e 67   of regs holding
236ae 20 72 6f 77 69 64 2b 64 61 74 61 20 62 65 69 6e   rowid+data bein
236af 67 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  g inserted */.  
236b0 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20  int regRowid;   
236b1 20 20 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65        /* registe
236b2 72 73 20 68 6f 6c 64 69 6e 67 20 69 6e 73 65 72  rs holding inser
236b3 74 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  t rowid */.  int
236b4 20 72 65 67 44 61 74 61 3b 20 20 20 20 20 20 20   regData;       
236b5 20 20 20 2f 2a 20 72 65 67 69 73 74 65 72 20 68     /* register h
236b6 6f 6c 64 69 6e 67 20 66 69 72 73 74 20 63 6f 6c  olding first col
236b7 75 6d 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f  umn to insert */
236b8 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64  .  int regRecord
236b9 3b 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 6c 64  ;        /* Hold
236ba 73 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65 64  s the assemblied
236bb 20 72 6f 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20   row record */. 
236bc 20 69 6e 74 20 72 65 67 45 6f 66 20 3d 20 30 3b   int regEof = 0;
236bd 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
236be 65 72 20 72 65 63 6f 72 64 69 6e 67 20 65 6e 64  er recording end
236bf 20 6f 66 20 53 45 4c 45 43 54 20 64 61 74 61 20   of SELECT data 
236c0 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64  */.  int *aRegId
236c1 78 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4f 6e  x = 0;     /* On
236c2 65 20 72 65 67 69 73 74 65 72 20 61 6c 6c 6f 63  e register alloc
236c3 61 74 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64  ated to each ind
236c4 65 78 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20  ex */...#ifndef 
236c5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
236c6 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77  GER.  int isView
236c7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
236c8 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74    /* True if att
236c9 65 6d 70 74 69 6e 67 20 74 6f 20 69 6e 73 65 72  empting to inser
236ca 74 20 69 6e 74 6f 20 61 20 76 69 65 77 20 2a 2f  t into a view */
236cb 0a 20 20 69 6e 74 20 74 72 69 67 67 65 72 73 5f  .  int triggers_
236cc 65 78 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f  exist = 0;     /
236cd 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20  * True if there 
236ce 61 72 65 20 46 4f 52 20 45 41 43 48 20 52 4f 57  are FOR EACH ROW
236cf 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e   triggers */.#en
236d0 64 69 66 0a 0a 20 20 64 62 20 3d 20 70 50 61 72  dif..  db = pPar
236d1 73 65 2d 3e 64 62 3b 0a 20 20 6d 65 6d 73 65 74  se->db;.  memset
236d2 28 26 64 65 73 74 2c 20 30 2c 20 73 69 7a 65 6f  (&dest, 0, sizeo
236d3 66 28 64 65 73 74 29 29 3b 0a 20 20 69 66 28 20  f(dest));.  if( 
236d4 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
236d5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
236d6 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73   ){.    goto ins
236d7 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ert_cleanup;.  }
236d8 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68  ..  /* Locate th
236d9 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69  e table into whi
236da 63 68 20 77 65 20 77 69 6c 6c 20 62 65 20 69 6e  ch we will be in
236db 73 65 72 74 69 6e 67 20 6e 65 77 20 69 6e 66 6f  serting new info
236dc 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  rmation..  */.  
236dd 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74  assert( pTabList
236de 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 7a  ->nSrc==1 );.  z
236df 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e  Tab = pTabList->
236e0 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66  a[0].zName;.  if
236e1 28 20 7a 54 61 62 3d 3d 30 20 29 20 67 6f 74 6f  ( zTab==0 ) goto
236e2 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
236e3 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
236e4 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70  3SrcListLookup(p
236e5 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29  Parse, pTabList)
236e6 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
236e7 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65  ){.    goto inse
236e8 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  rt_cleanup;.  }.
236e9 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
236ea 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
236eb 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
236ec 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64  .  assert( iDb<d
236ed 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 44 62 20  b->nDb );.  pDb 
236ee 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
236ef 0a 20 20 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e  .  zDb = pDb->zN
236f0 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ame;.  if( sqlit
236f1 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
236f2 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52  se, SQLITE_INSER
236f3 54 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  T, pTab->zName, 
236f4 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 67  0, zDb) ){.    g
236f5 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
236f6 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69  up;.  }..  /* Fi
236f7 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 68  gure out if we h
236f8 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 73  ave any triggers
236f9 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c   and if the tabl
236fa 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69 6e 73  e being.  ** ins
236fb 65 72 74 65 64 20 69 6e 74 6f 20 69 73 20 61 20  erted into is a 
236fc 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  view.  */.#ifnde
236fd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
236fe 49 47 47 45 52 0a 20 20 74 72 69 67 67 65 72 73  IGGER.  triggers
236ff 5f 65 78 69 73 74 20 3d 20 73 71 6c 69 74 65 33  _exist = sqlite3
23700 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70 54  TriggersExist(pT
23701 61 62 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30  ab, TK_INSERT, 0
23702 29 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54  );.  isView = pT
23703 61 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a  ab->pSelect!=0;.
23704 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74  #else.# define t
23705 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 30 0a  riggers_exist 0.
23706 23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20  # define isView 
23707 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  0.#endif.#ifdef 
23708 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
23709 0a 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a  .# undef isView.
2370a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20  # define isView 
2370b 30 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45  0.#endif..  /* E
2370c 6e 73 75 72 65 20 74 68 61 74 3a 0a 20 20 2a 20  nsure that:.  * 
2370d 20 28 61 29 20 74 68 65 20 74 61 62 6c 65 20 69   (a) the table i
2370e 73 20 6e 6f 74 20 72 65 61 64 2d 6f 6e 6c 79 2c  s not read-only,
2370f 20 0a 20 20 2a 20 20 28 62 29 20 74 68 61 74 20   .  *  (b) that 
23710 69 66 20 69 74 20 69 73 20 61 20 76 69 65 77 20  if it is a view 
23711 74 68 65 6e 20 4f 4e 20 49 4e 53 45 52 54 20 74  then ON INSERT t
23712 72 69 67 67 65 72 73 20 65 78 69 73 74 0a 20 20  riggers exist.  
23713 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
23714 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73  IsReadOnly(pPars
23715 65 2c 20 70 54 61 62 2c 20 74 72 69 67 67 65 72  e, pTab, trigger
23716 73 5f 65 78 69 73 74 29 20 29 7b 0a 20 20 20 20  s_exist) ){.    
23717 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
23718 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  nup;.  }.  asser
23719 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 0a 20  t( pTab!=0 );.. 
2371a 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20 72   /* If pTab is r
2371b 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d 61  eally a view, ma
2371c 6b 65 20 73 75 72 65 20 69 74 20 68 61 73 20 62  ke sure it has b
2371d 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e  een initialized.
2371e 0a 20 20 2a 2a 20 56 69 65 77 47 65 74 43 6f 6c  .  ** ViewGetCol
2371f 75 6d 6e 4e 61 6d 65 73 28 29 20 69 73 20 61 20  umnNames() is a 
23720 6e 6f 2d 6f 70 20 69 66 20 70 54 61 62 20 69 73  no-op if pTab is
23721 20 6e 6f 74 20 61 20 76 69 65 77 20 28 6f 72 20   not a view (or 
23722 76 69 72 74 75 61 6c 20 0a 20 20 2a 2a 20 6d 6f  virtual .  ** mo
23723 64 75 6c 65 20 74 61 62 6c 65 29 2e 0a 20 20 2a  dule table)..  *
23724 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  /.  if( sqlite3V
23725 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
23726 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20  s(pParse, pTab) 
23727 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65  ){.    goto inse
23728 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  rt_cleanup;.  }.
23729 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
2372a 20 56 44 42 45 0a 20 20 2a 2f 0a 20 20 76 20 3d   VDBE.  */.  v =
2372b 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2372c 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
2372d 3d 3d 30 20 29 20 67 6f 74 6f 20 69 6e 73 65 72  ==0 ) goto inser
2372e 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28  t_cleanup;.  if(
2372f 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
23730 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 65  =0 ) sqlite3Vdbe
23731 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b  CountChanges(v);
23732 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57  .  sqlite3BeginW
23733 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
23734 61 72 73 65 2c 20 70 53 65 6c 65 63 74 20 7c 7c  arse, pSelect ||
23735 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 2c   triggers_exist,
23736 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 69 66 20   iDb);..  /* if 
23737 74 68 65 72 65 20 61 72 65 20 72 6f 77 20 74 72  there are row tr
23738 69 67 67 65 72 73 2c 20 61 6c 6c 6f 63 61 74 65  iggers, allocate
23739 20 61 20 74 65 6d 70 20 74 61 62 6c 65 20 66 6f   a temp table fo
2373a 72 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63  r new.* referenc
2373b 65 73 2e 20 2a 2f 0a 20 20 69 66 28 20 74 72 69  es. */.  if( tri
2373c 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20  ggers_exist ){. 
2373d 20 20 20 6e 65 77 49 64 78 20 3d 20 70 50 61 72     newIdx = pPar
2373e 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a  se->nTab++;.  }.
2373f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23740 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 0a 20 20  OMIT_XFER_OPT.  
23741 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 6d  /* If the statem
23742 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ent is of the fo
23743 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  rm.  **.  **    
23744 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c     INSERT INTO <
23745 74 61 62 6c 65 31 3e 20 53 45 4c 45 43 54 20 2a  table1> SELECT *
23746 20 46 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 3b 0a   FROM <table2>;.
23747 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 73    **.  ** Then s
23748 70 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74  pecial optimizat
23749 69 6f 6e 73 20 63 61 6e 20 62 65 20 61 70 70 6c  ions can be appl
2374a 69 65 64 20 74 68 61 74 20 6d 61 6b 65 20 74 68  ied that make th
2374b 65 20 74 72 61 6e 73 66 65 72 0a 20 20 2a 2a 20  e transfer.  ** 
2374c 76 65 72 79 20 66 61 73 74 20 61 6e 64 20 77 68  very fast and wh
2374d 69 63 68 20 72 65 64 75 63 65 20 66 72 61 67 6d  ich reduce fragm
2374e 65 6e 74 61 74 69 6f 6e 20 6f 66 20 69 6e 64 69  entation of indi
2374f 63 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ces..  **.  ** T
23750 68 69 73 20 69 73 20 74 68 65 20 32 6e 64 20 74  his is the 2nd t
23751 65 6d 70 6c 61 74 65 2e 0a 20 20 2a 2f 0a 20 20  emplate..  */.  
23752 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26  if( pColumn==0 &
23753 26 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69  & xferOptimizati
23754 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  on(pParse, pTab,
23755 20 70 53 65 6c 65 63 74 2c 20 6f 6e 45 72 72 6f   pSelect, onErro
23756 72 2c 20 69 44 62 29 20 29 7b 0a 20 20 20 20 61  r, iDb) ){.    a
23757 73 73 65 72 74 28 20 21 74 72 69 67 67 65 72 73  ssert( !triggers
23758 5f 65 78 69 73 74 20 29 3b 0a 20 20 20 20 61 73  _exist );.    as
23759 73 65 72 74 28 20 70 4c 69 73 74 3d 3d 30 20 29  sert( pList==0 )
2375a 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72  ;.    goto inser
2375b 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23  t_cleanup;.  }.#
2375c 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2375d 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f  OMIT_XFER_OPT */
2375e 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
2375f 73 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45  s an AUTOINCREME
23760 4e 54 20 74 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75  NT table, look u
23761 70 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e  p the sequence n
23762 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 20 20 2a  umber in the.  *
23763 2a 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  * sqlite_sequenc
23764 65 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  e table and stor
23765 65 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63  e it in memory c
23766 65 6c 6c 20 72 65 67 41 75 74 6f 69 6e 63 2e 0a  ell regAutoinc..
23767 20 20 2a 2f 0a 20 20 72 65 67 41 75 74 6f 69 6e    */.  regAutoin
23768 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  c = autoIncBegin
23769 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
2376a 61 62 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  ab);..  /* Figur
2376b 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 63  e out how many c
2376c 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20 61  olumns of data a
2376d 72 65 20 73 75 70 70 6c 69 65 64 2e 20 20 49 66  re supplied.  If
2376e 20 74 68 65 20 64 61 74 61 0a 20 20 2a 2a 20 69   the data.  ** i
2376f 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20  s coming from a 
23770 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
23771 2c 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20  , then generate 
23772 61 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61  a co-routine tha
23773 74 0a 20 20 2a 2a 20 70 72 6f 64 75 63 65 73 20  t.  ** produces 
23774 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20  a single row of 
23775 74 68 65 20 53 45 4c 45 43 54 20 6f 6e 20 65 61  the SELECT on ea
23776 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 20  ch invocation.  
23777 54 68 65 0a 20 20 2a 2a 20 63 6f 2d 72 6f 75 74  The.  ** co-rout
23778 69 6e 65 20 69 73 20 74 68 65 20 63 6f 6d 6d 6f  ine is the commo
23779 6e 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 20  n header to the 
2377a 33 72 64 20 61 6e 64 20 34 74 68 20 74 65 6d 70  3rd and 4th temp
2377b 6c 61 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  lates..  */.  if
2377c 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
2377d 20 2f 2a 20 44 61 74 61 20 69 73 20 63 6f 6d 69   /* Data is comi
2377e 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54  ng from a SELECT
2377f 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  .  Generate code
23780 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
23781 61 74 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a  at SELECT.    **
23782 20 61 73 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65   as a co-routine
23783 2e 20 20 54 68 65 20 63 6f 64 65 20 69 73 20 63  .  The code is c
23784 6f 6d 6d 6f 6e 20 74 6f 20 62 6f 74 68 20 74 68  ommon to both th
23785 65 20 33 72 64 20 61 6e 64 20 34 74 68 0a 20 20  e 3rd and 4th.  
23786 20 20 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a    ** templates:.
23787 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
23788 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30 0a 20        EOF <- 0. 
23789 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 58 20     **         X 
2378a 3c 2d 20 41 0a 20 20 20 20 2a 2a 20 20 20 20 20  <- A.    **     
2378b 20 20 20 20 67 6f 74 6f 20 42 0a 20 20 20 20 2a      goto B.    *
2378c 2a 20 20 20 20 20 20 41 3a 20 73 65 74 75 70 20  *      A: setup 
2378d 66 6f 72 20 74 68 65 20 53 45 4c 45 43 54 0a 20  for the SELECT. 
2378e 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 6f     **         lo
2378f 6f 70 20 6f 76 65 72 20 74 68 65 20 74 61 62 6c  op over the tabl
23790 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54  es in the SELECT
23791 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
23792 20 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e 74    load value int
23793 6f 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52 2b  o register R..R+
23794 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  n.    **        
23795 20 20 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a     yield X.    *
23796 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f  *         end lo
23797 6f 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  op.    **       
23798 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65 72 20    cleanup after 
23799 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a  the SELECT.    *
2379a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d  *         EOF <-
2379b 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   1.    **       
2379c 20 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a    yield X.    **
2379d 20 20 20 20 20 20 20 20 20 68 61 6c 74 2d 65 72           halt-er
2379e 72 6f 72 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ror.    **.    *
2379f 2a 20 4f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61  * On each invoca
237a0 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 2d 72  tion of the co-r
237a1 6f 75 74 69 6e 65 2c 20 69 74 20 70 75 74 73 20  outine, it puts 
237a2 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20  a single row of 
237a3 74 68 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43  the.    ** SELEC
237a4 54 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72 65  T result into re
237a5 67 69 73 74 65 72 73 20 64 65 73 74 2e 69 4d 65  gisters dest.iMe
237a6 6d 2e 2e 2e 64 65 73 74 2e 69 4d 65 6d 2b 64 65  m...dest.iMem+de
237a7 73 74 2e 6e 4d 65 6d 2d 31 2e 0a 20 20 20 20 2a  st.nMem-1..    *
237a8 2a 20 28 54 68 65 73 65 20 6f 75 74 70 75 74 20  * (These output 
237a9 72 65 67 69 73 74 65 72 73 20 61 72 65 20 61 6c  registers are al
237aa 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74  located by sqlit
237ab 65 33 53 65 6c 65 63 74 28 29 2e 29 20 20 57 68  e3Select().)  Wh
237ac 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 53 45  en.    ** the SE
237ad 4c 45 43 54 20 63 6f 6d 70 6c 65 74 65 73 2c 20  LECT completes, 
237ae 69 74 20 73 65 74 73 20 74 68 65 20 45 4f 46 20  it sets the EOF 
237af 66 6c 61 67 20 73 74 6f 72 65 64 20 69 6e 20 72  flag stored in r
237b0 65 67 45 6f 66 2e 0a 20 20 20 20 2a 2f 0a 20 20  egEof..    */.  
237b1 20 20 69 6e 74 20 72 63 2c 20 6a 31 3b 0a 0a 20    int rc, j1;.. 
237b2 20 20 20 72 65 67 45 6f 66 20 3d 20 2b 2b 70 50     regEof = ++pP
237b3 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
237b4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
237b5 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
237b6 20 30 2c 20 72 65 67 45 6f 66 29 3b 20 20 20 20   0, regEof);    
237b7 20 20 2f 2a 20 45 4f 46 20 3c 2d 20 30 20 2a 2f    /* EOF <- 0 */
237b8 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
237b9 28 28 76 2c 20 22 53 45 4c 45 43 54 20 65 6f 66  ((v, "SELECT eof
237ba 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 73 71   flag"));.    sq
237bb 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
237bc 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 43  nit(&dest, SRT_C
237bd 6f 72 6f 75 74 69 6e 65 2c 20 2b 2b 70 50 61 72  oroutine, ++pPar
237be 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 61  se->nMem);.    a
237bf 64 64 72 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  ddrSelect = sqli
237c0 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
237c1 64 72 28 76 29 2b 32 3b 0a 20 20 20 20 73 71 6c  dr(v)+2;.    sql
237c2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
237c3 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 61 64  , OP_Integer, ad
237c4 64 72 53 65 6c 65 63 74 2d 31 2c 20 64 65 73 74  drSelect-1, dest
237c5 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 6a 31 20  .iParm);.    j1 
237c6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
237c7 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
237c8 30 2c 20 30 29 3b 0a 20 20 20 20 56 64 62 65 43  0, 0);.    VdbeC
237c9 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d 70  omment((v, "Jump
237ca 20 6f 76 65 72 20 53 45 4c 45 43 54 20 63 6f 72   over SELECT cor
237cb 6f 75 74 69 6e 65 22 29 29 3b 0a 0a 20 20 20 20  outine"));..    
237cc 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65  /* Resolve the e
237cd 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
237ce 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
237cf 6e 74 20 61 6e 64 20 65 78 65 63 75 74 65 20 69  nt and execute i
237d0 74 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  t. */.    rc = s
237d1 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
237d2 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 64  rse, pSelect, &d
237d3 65 73 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  est);.    if( rc
237d4 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
237d5 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
237d6 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 67 6f  iled ){.      go
237d7 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
237d8 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  p;.    }.    sql
237d9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
237da 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
237db 20 72 65 67 45 6f 66 29 3b 20 20 20 20 20 20 20   regEof);       
237dc 20 20 2f 2a 20 45 4f 46 20 3c 2d 20 31 20 2a 2f    /* EOF <- 1 */
237dd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
237de 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
237df 6c 64 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b  ld, dest.iParm);
237e0 20 20 20 2f 2a 20 79 69 65 6c 64 20 58 20 2a 2f     /* yield X */
237e1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
237e2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c  AddOp2(v, OP_Hal
237e3 74 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e  t, SQLITE_INTERN
237e4 41 4c 2c 20 4f 45 5f 41 62 6f 72 74 29 3b 0a 20  AL, OE_Abort);. 
237e5 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
237e6 76 2c 20 22 45 6e 64 20 6f 66 20 53 45 4c 45 43  v, "End of SELEC
237e7 54 20 63 6f 72 6f 75 74 69 6e 65 22 29 29 3b 0a  T coroutine"));.
237e8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
237e9 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 20  umpHere(v, j1); 
237ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
237eb 20 20 20 20 20 20 20 20 20 2f 2a 20 6c 61 62 65           /* labe
237ec 6c 20 42 3a 20 2a 2f 0a 0a 20 20 20 20 72 65 67  l B: */..    reg
237ed 46 72 6f 6d 53 65 6c 65 63 74 20 3d 20 64 65 73  FromSelect = des
237ee 74 2e 69 4d 65 6d 3b 0a 20 20 20 20 61 73 73 65  t.iMem;.    asse
237ef 72 74 28 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  rt( pSelect->pEL
237f0 69 73 74 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 75  ist );.    nColu
237f1 6d 6e 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45  mn = pSelect->pE
237f2 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  List->nExpr;.   
237f3 20 61 73 73 65 72 74 28 20 64 65 73 74 2e 6e 4d   assert( dest.nM
237f4 65 6d 3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 0a  em==nColumn );..
237f5 20 20 20 20 2f 2a 20 53 65 74 20 75 73 65 54 65      /* Set useTe
237f6 6d 70 54 61 62 6c 65 20 74 6f 20 54 52 55 45 20  mpTable to TRUE 
237f7 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  if the result of
237f8 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
237f9 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 73 68 6f  ement.    ** sho
237fa 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 69  uld be written i
237fb 6e 74 6f 20 61 20 74 65 6d 70 6f 72 61 72 79 20  nto a temporary 
237fc 74 61 62 6c 65 20 28 74 65 6d 70 6c 61 74 65 20  table (template 
237fd 34 29 2e 20 20 53 65 74 20 74 6f 0a 20 20 20 20  4).  Set to.    
237fe 2a 2a 20 46 41 4c 53 45 20 69 66 20 65 61 63 68  ** FALSE if each
237ff 2a 20 72 6f 77 20 6f 66 20 74 68 65 20 53 45 4c  * row of the SEL
23800 45 43 54 20 63 61 6e 20 62 65 20 77 72 69 74 74  ECT can be writt
23801 65 6e 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f  en directly into
23802 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 65 73 74  .    ** the dest
23803 69 6e 61 74 69 6f 6e 20 74 61 62 6c 65 20 28 74  ination table (t
23804 65 6d 70 6c 61 74 65 20 33 29 2e 0a 20 20 20 20  emplate 3)..    
23805 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 74 65 6d 70  **.    ** A temp
23806 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 75   table must be u
23807 73 65 64 20 69 66 20 74 68 65 20 74 61 62 6c 65  sed if the table
23808 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 20 69   being updated i
23809 73 20 61 6c 73 6f 20 6f 6e 65 0a 20 20 20 20 2a  s also one.    *
2380a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20  * of the tables 
2380b 62 65 69 6e 67 20 72 65 61 64 20 62 79 20 74 68  being read by th
2380c 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2380d 6e 74 2e 20 20 41 6c 73 6f 20 75 73 65 20 61 20  nt.  Also use a 
2380e 0a 20 20 20 20 2a 2a 20 74 65 6d 70 20 74 61 62  .    ** temp tab
2380f 6c 65 20 69 6e 20 74 68 65 20 63 61 73 65 20 6f  le in the case o
23810 66 20 72 6f 77 20 74 72 69 67 67 65 72 73 2e 0a  f row triggers..
23811 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 74      */.    if( t
23812 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 7c 7c  riggers_exist ||
23813 20 72 65 61 64 73 54 61 62 6c 65 28 76 2c 20 61   readsTable(v, a
23814 64 64 72 53 65 6c 65 63 74 2c 20 69 44 62 2c 20  ddrSelect, iDb, 
23815 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 75  pTab) ){.      u
23816 73 65 54 65 6d 70 54 61 62 6c 65 20 3d 20 31 3b  seTempTable = 1;
23817 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
23818 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a  useTempTable ){.
23819 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20        /* Invoke 
2381a 74 68 65 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f  the coroutine to
2381b 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61   extract informa
2381c 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 53 45  tion from the SE
2381d 4c 45 43 54 0a 20 20 20 20 20 20 2a 2a 20 61 6e  LECT.      ** an
2381e 64 20 61 64 64 20 69 74 20 74 6f 20 61 20 74 72  d add it to a tr
2381f 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 73 72  ansient table sr
23820 63 54 61 62 2e 20 20 54 68 65 20 63 6f 64 65 20  cTab.  The code 
23821 67 65 6e 65 72 61 74 65 64 0a 20 20 20 20 20 20  generated.      
23822 2a 2a 20 68 65 72 65 20 69 73 20 66 72 6f 6d 20  ** here is from 
23823 74 68 65 20 34 74 68 20 74 65 6d 70 6c 61 74 65  the 4th template
23824 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  :.      **.     
23825 20 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65 6e   **      B: open
23826 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20 20 20   temp table.    
23827 20 20 2a 2a 20 20 20 20 20 20 4c 3a 20 79 69 65    **      L: yie
23828 6c 64 20 58 0a 20 20 20 20 20 20 2a 2a 20 20 20  ld X.      **   
23829 20 20 20 20 20 20 69 66 20 45 4f 46 20 67 6f 74        if EOF got
2382a 6f 20 4d 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  o M.      **    
2382b 20 20 20 20 20 69 6e 73 65 72 74 20 72 6f 77 20       insert row 
2382c 66 72 6f 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f  from R..R+n into
2382d 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20 20 20   temp table.    
2382e 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74    **         got
2382f 6f 20 4c 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  o L.      **    
23830 20 20 4d 3a 20 2e 2e 2e 0a 20 20 20 20 20 20 2a    M: ....      *
23831 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 52  /.      int regR
23832 65 63 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ec;          /* 
23833 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64  Register to hold
23834 20 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 2a   packed record *
23835 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 54  /.      int regT
23836 65 6d 70 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20  empRowid;    /* 
23837 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64  Register to hold
23838 20 74 65 6d 70 20 74 61 62 6c 65 20 52 4f 57 49   temp table ROWI
23839 44 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61  D */.      int a
2383a 64 64 72 54 6f 70 3b 20 20 20 20 20 20 20 20 20  ddrTop;         
2383b 2f 2a 20 4c 61 62 65 6c 20 22 4c 22 20 2a 2f 0a  /* Label "L" */.
2383c 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 49 66        int addrIf
2383d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  ;          /* Ad
2383e 64 72 65 73 73 20 6f 66 20 6a 75 6d 70 20 74 6f  dress of jump to
2383f 20 4d 20 2a 2f 0a 0a 20 20 20 20 20 20 73 72 63   M */..      src
23840 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
23841 61 62 2b 2b 3b 0a 20 20 20 20 20 20 72 65 67 52  ab++;.      regR
23842 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ec = sqlite3GetT
23843 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
23844 20 20 20 20 20 20 72 65 67 54 65 6d 70 52 6f 77        regTempRow
23845 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  id = sqlite3GetT
23846 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
23847 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23848 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
23849 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 73 72 63  enEphemeral, src
2384a 54 61 62 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  Tab, nColumn);. 
2384b 20 20 20 20 20 61 64 64 72 54 6f 70 20 3d 20 73       addrTop = s
2384c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2384d 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65  (v, OP_Yield, de
2384e 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20  st.iParm);.     
2384f 20 61 64 64 72 49 66 20 3d 20 73 71 6c 69 74 65   addrIf = sqlite
23850 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
23851 50 5f 49 66 2c 20 72 65 67 45 6f 66 29 3b 0a 20  P_If, regEof);. 
23852 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23853 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
23854 65 52 65 63 6f 72 64 2c 20 72 65 67 46 72 6f 6d  eRecord, regFrom
23855 53 65 6c 65 63 74 2c 20 6e 43 6f 6c 75 6d 6e 2c  Select, nColumn,
23856 20 72 65 67 52 65 63 29 3b 0a 20 20 20 20 20 20   regRec);.      
23857 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23858 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  2(v, OP_NewRowid
23859 2c 20 73 72 63 54 61 62 2c 20 72 65 67 54 65 6d  , srcTab, regTem
2385a 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73  pRowid);.      s
2385b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2385c 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 73  (v, OP_Insert, s
2385d 72 63 54 61 62 2c 20 72 65 67 52 65 63 2c 20 72  rcTab, regRec, r
2385e 65 67 54 65 6d 70 52 6f 77 69 64 29 3b 0a 20 20  egTempRowid);.  
2385f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23860 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
23861 2c 20 30 2c 20 61 64 64 72 54 6f 70 29 3b 0a 20  , 0, addrTop);. 
23862 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23863 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
23864 49 66 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  If);.      sqlit
23865 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
23866 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 29  (pParse, regRec)
23867 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
23868 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
23869 61 72 73 65 2c 20 72 65 67 54 65 6d 70 52 6f 77  arse, regTempRow
2386a 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  id);.    }.  }el
2386b 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  se{.    /* This 
2386c 69 73 20 74 68 65 20 63 61 73 65 20 69 66 20 74  is the case if t
2386d 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  he data for the 
2386e 49 4e 53 45 52 54 20 69 73 20 63 6f 6d 69 6e 67  INSERT is coming
2386f 20 66 72 6f 6d 20 61 20 56 41 4c 55 45 53 0a 20   from a VALUES. 
23870 20 20 20 2a 2a 20 63 6c 61 75 73 65 0a 20 20 20     ** clause.   
23871 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74   */.    NameCont
23872 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 6d 65 6d  ext sNC;.    mem
23873 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
23874 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73  eof(sNC));.    s
23875 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  NC.pParse = pPar
23876 73 65 3b 0a 20 20 20 20 73 72 63 54 61 62 20 3d  se;.    srcTab =
23877 20 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28   -1;.    assert(
23878 20 75 73 65 54 65 6d 70 54 61 62 6c 65 3d 3d 30   useTempTable==0
23879 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20   );.    nColumn 
2387a 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d  = pList ? pList-
2387b 3e 6e 45 78 70 72 20 3a 20 30 3b 0a 20 20 20 20  >nExpr : 0;.    
2387c 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75  for(i=0; i<nColu
2387d 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
2387e 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  if( sqlite3Resol
2387f 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
23880 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  , pList->a[i].pE
23881 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20  xpr) ){.        
23882 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
23883 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nup;.      }.   
23884 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b   }.  }..  /* Mak
23885 65 20 73 75 72 65 20 74 68 65 20 6e 75 6d 62 65  e sure the numbe
23886 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
23887 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 20  the source data 
23888 6d 61 74 63 68 65 73 20 74 68 65 20 6e 75 6d 62  matches the numb
23889 65 72 0a 20 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d  er.  ** of colum
2388a 6e 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  ns to be inserte
2388b 64 20 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65  d into the table
2388c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 73 56  ..  */.  if( IsV
2388d 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2388e 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2388f 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
23890 0a 20 20 20 20 20 20 6e 48 69 64 64 65 6e 20 2b  .      nHidden +
23891 3d 20 28 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d  = (IsHiddenColum
23892 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  n(&pTab->aCol[i]
23893 29 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20  ) ? 1 : 0);.    
23894 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c  }.  }.  if( pCol
23895 75 6d 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c 75 6d  umn==0 && nColum
23896 6e 20 26 26 20 6e 43 6f 6c 75 6d 6e 21 3d 28 70  n && nColumn!=(p
23897 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65  Tab->nCol-nHidde
23898 6e 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  n) ){.    sqlite
23899 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2389a 2c 20 0a 20 20 20 20 20 20 20 22 74 61 62 6c 65  , .       "table
2389b 20 25 53 20 68 61 73 20 25 64 20 63 6f 6c 75 6d   %S has %d colum
2389c 6e 73 20 62 75 74 20 25 64 20 76 61 6c 75 65 73  ns but %d values
2389d 20 77 65 72 65 20 73 75 70 70 6c 69 65 64 22 2c   were supplied",
2389e 0a 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74  .       pTabList
2389f 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c  , 0, pTab->nCol,
238a0 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 67   nColumn);.    g
238a1 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
238a2 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43  up;.  }.  if( pC
238a3 6f 6c 75 6d 6e 21 3d 30 20 26 26 20 6e 43 6f 6c  olumn!=0 && nCol
238a4 75 6d 6e 21 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49  umn!=pColumn->nI
238a5 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
238a6 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
238a7 20 22 25 64 20 76 61 6c 75 65 73 20 66 6f 72 20   "%d values for 
238a8 25 64 20 63 6f 6c 75 6d 6e 73 22 2c 20 6e 43 6f  %d columns", nCo
238a9 6c 75 6d 6e 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 6e  lumn, pColumn->n
238aa 49 64 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  Id);.    goto in
238ab 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
238ac 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 49  }..  /* If the I
238ad 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 20  NSERT statement 
238ae 69 6e 63 6c 75 64 65 64 20 61 6e 20 49 44 4c 49  included an IDLI
238af 53 54 20 74 65 72 6d 2c 20 74 68 65 6e 20 6d 61  ST term, then ma
238b0 6b 65 20 73 75 72 65 0a 20 20 2a 2a 20 61 6c 6c  ke sure.  ** all
238b1 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
238b2 20 49 44 4c 49 53 54 20 72 65 61 6c 6c 79 20 61   IDLIST really a
238b3 72 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  re columns of th
238b4 65 20 74 61 62 6c 65 20 61 6e 64 20 0a 20 20 2a  e table and .  *
238b5 2a 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 63  * remember the c
238b6 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73 2e 0a 20  olumn indices.. 
238b7 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20   **.  ** If the 
238b8 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54  table has an INT
238b9 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
238ba 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 61 74   column and that
238bb 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 69 73 20   column.  ** is 
238bc 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 49 44 4c  named in the IDL
238bd 49 53 54 2c 20 74 68 65 6e 20 72 65 63 6f 72 64  IST, then record
238be 20 69 6e 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d   in the keyColum
238bf 6e 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20  n variable.  ** 
238c0 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 49  the index into I
238c1 44 4c 49 53 54 20 6f 66 20 74 68 65 20 70 72 69  DLIST of the pri
238c2 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d 6e 2e  mary key column.
238c3 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 69 73 0a 20    keyColumn is. 
238c4 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 6f 66   ** the index of
238c5 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
238c6 20 61 73 20 69 74 20 61 70 70 65 61 72 73 20 69   as it appears i
238c7 6e 20 49 44 4c 49 53 54 2c 20 6e 6f 74 20 61 73  n IDLIST, not as
238c8 0a 20 20 2a 2a 20 69 73 20 61 70 70 65 61 72 73  .  ** is appears
238c9 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   in the original
238ca 20 74 61 62 6c 65 2e 20 20 28 54 68 65 20 69 6e   table.  (The in
238cb 64 65 78 20 6f 66 20 74 68 65 20 70 72 69 6d 61  dex of the prima
238cc 72 79 0a 20 20 2a 2a 20 6b 65 79 20 69 6e 20 74  ry.  ** key in t
238cd 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c  he original tabl
238ce 65 20 69 73 20 70 54 61 62 2d 3e 69 50 4b 65 79  e is pTab->iPKey
238cf 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43  .).  */.  if( pC
238d0 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 66 6f 72  olumn ){.    for
238d1 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d  (i=0; i<pColumn-
238d2 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nId; i++){.    
238d3 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e    pColumn->a[i].
238d4 69 64 78 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a  idx = -1;.    }.
238d5 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
238d6 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b  Column->nId; i++
238d7 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  ){.      for(j=0
238d8 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; j<pTab->nCol; 
238d9 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  j++){.        if
238da 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
238db 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a  (pColumn->a[i].z
238dc 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  Name, pTab->aCol
238dd 5b 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  [j].zName)==0 ){
238de 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 75  .          pColu
238df 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20 3d 20 6a  mn->a[i].idx = j
238e0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
238e1 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  j==pTab->iPKey )
238e2 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6b 65  {.            ke
238e3 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20 20 20  yColumn = i;.   
238e4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
238e5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
238e6 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
238e7 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e    if( j>=pTab->n
238e8 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 69  Col ){.        i
238e9 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69  f( sqlite3IsRowi
238ea 64 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e  d(pColumn->a[i].
238eb 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20  zName) ){.      
238ec 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20      keyColumn = 
238ed 69 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  i;.        }else
238ee 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
238ef 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
238f0 73 65 2c 20 22 74 61 62 6c 65 20 25 53 20 68 61  se, "table %S ha
238f1 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  s no column name
238f2 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20  d %s",.         
238f3 20 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30       pTabList, 0
238f4 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e  , pColumn->a[i].
238f5 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
238f6 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
238f7 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
238f8 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
238f9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
238fa 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
238fb 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
238fc 6f 20 49 44 4c 49 53 54 20 74 65 72 6d 20 62 75  o IDLIST term bu
238fd 74 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  t the table has 
238fe 61 6e 20 69 6e 74 65 67 65 72 20 70 72 69 6d 61  an integer prima
238ff 72 79 0a 20 20 2a 2a 20 6b 65 79 2c 20 74 68 65  ry.  ** key, the
23900 20 73 65 74 20 74 68 65 20 6b 65 79 43 6f 6c 75   set the keyColu
23901 6d 6e 20 76 61 72 69 61 62 6c 65 20 74 6f 20 74  mn variable to t
23902 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63  he primary key c
23903 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20 2a 2a  olumn index.  **
23904 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   in the original
23905 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f   table definitio
23906 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43  n..  */.  if( pC
23907 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c  olumn==0 && nCol
23908 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 6b 65 79  umn>0 ){.    key
23909 43 6f 6c 75 6d 6e 20 3d 20 70 54 61 62 2d 3e 69  Column = pTab->i
2390a 50 4b 65 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  PKey;.  }..  /* 
2390b 4f 70 65 6e 20 74 68 65 20 74 65 6d 70 20 74 61  Open the temp ta
2390c 62 6c 65 20 66 6f 72 20 46 4f 52 20 45 41 43 48  ble for FOR EACH
2390d 20 52 4f 57 20 74 72 69 67 67 65 72 73 0a 20 20   ROW triggers.  
2390e 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67 65 72  */.  if( trigger
2390f 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 73  s_exist ){.    s
23910 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23911 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c  (v, OP_SetNumCol
23912 75 6d 6e 73 2c 20 30 2c 20 70 54 61 62 2d 3e 6e  umns, 0, pTab->n
23913 43 6f 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Col);.    sqlite
23914 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
23915 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6e 65  P_OpenPseudo, ne
23916 77 49 64 78 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  wIdx, 0);.  }.  
23917 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69    .  /* Initiali
23918 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20  ze the count of 
23919 72 6f 77 73 20 74 6f 20 62 65 20 69 6e 73 65 72  rows to be inser
2391a 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ted.  */.  if( d
2391b 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2391c 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20  E_CountRows ){. 
2391d 20 20 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d     regRowCount =
2391e 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2391f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23920 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
23921 65 67 65 72 2c 20 30 2c 20 72 65 67 52 6f 77 43  eger, 0, regRowC
23922 6f 75 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ount);.  }..  /*
23923 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   If this is not 
23924 61 20 76 69 65 77 2c 20 6f 70 65 6e 20 74 68 65  a view, open the
23925 20 74 61 62 6c 65 20 61 6e 64 20 61 6e 64 20 61   table and and a
23926 6c 6c 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  ll indices */.  
23927 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20  if( !isView ){. 
23928 20 20 20 69 6e 74 20 6e 49 64 78 3b 0a 0a 20 20     int nIdx;..  
23929 20 20 62 61 73 65 43 75 72 20 3d 20 70 50 61 72    baseCur = pPar
2392a 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 6e 49  se->nTab;.    nI
2392b 64 78 20 3d 20 73 71 6c 69 74 65 33 4f 70 65 6e  dx = sqlite3Open
2392c 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28  TableAndIndices(
2392d 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61  pParse, pTab, ba
2392e 73 65 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72  seCur, OP_OpenWr
2392f 69 74 65 29 3b 0a 20 20 20 20 61 52 65 67 49 64  ite);.    aRegId
23930 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  x = sqlite3DbMal
23931 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
23932 66 28 69 6e 74 29 2a 28 6e 49 64 78 2b 31 29 29  f(int)*(nIdx+1))
23933 3b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64  ;.    if( aRegId
23934 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  x==0 ){.      go
23935 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
23936 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  p;.    }.    for
23937 28 69 3d 30 3b 20 69 3c 6e 49 64 78 3b 20 69 2b  (i=0; i<nIdx; i+
23938 2b 29 7b 0a 20 20 20 20 20 20 61 52 65 67 49 64  +){.      aRegId
23939 78 5b 69 5d 20 3d 20 2b 2b 70 50 61 72 73 65 2d  x[i] = ++pParse-
2393a 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  >nMem;.    }.  }
2393b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74  ..  /* This is t
2393c 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6d 61  he top of the ma
2393d 69 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f  in insertion loo
2393e 70 20 2a 2f 0a 20 20 69 66 28 20 75 73 65 54 65  p */.  if( useTe
2393f 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f  mpTable ){.    /
23940 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64  * This block cod
23941 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f  es the top of lo
23942 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f  op only.  The co
23943 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20 74  mplete loop is t
23944 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77  he.    ** follow
23945 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20 28  ing pseudocode (
23946 74 65 6d 70 6c 61 74 65 20 34 29 3a 0a 20 20 20  template 4):.   
23947 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
23948 20 20 20 72 65 77 69 6e 64 20 74 65 6d 70 20 74     rewind temp t
23949 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20 20  able.    **     
2394a 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72 6f   C: loop over ro
2394b 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64 69 61  ws of intermedia
2394c 74 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20  te table.    ** 
2394d 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66            transf
2394e 65 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69  er values form i
2394f 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
23950 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 20  e into <table>. 
23951 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 65 6e     **         en
23952 64 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20  d loop.    **   
23953 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f     D: ....    */
23954 0a 20 20 20 20 61 64 64 72 49 6e 73 54 6f 70 20  .    addrInsTop 
23955 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
23956 4f 70 31 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op1(v, OP_Rewind
23957 2c 20 73 72 63 54 61 62 29 3b 0a 20 20 20 20 61  , srcTab);.    a
23958 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65  ddrCont = sqlite
23959 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2395a 28 76 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  (v);.  }else if(
2395b 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
2395c 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f  /* This block co
2395d 64 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c  des the top of l
2395e 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63  oop only.  The c
2395f 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20  omplete loop is 
23960 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f  the.    ** follo
23961 77 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20  wing pseudocode 
23962 28 74 65 6d 70 6c 61 74 65 20 33 29 3a 0a 20 20  (template 3):.  
23963 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
23964 20 43 3a 20 79 69 65 6c 64 20 58 0a 20 20 20 20   C: yield X.    
23965 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f  **         if EO
23966 46 20 67 6f 74 6f 20 44 0a 20 20 20 20 2a 2a 20  F goto D.    ** 
23967 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 74          insert t
23968 68 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74  he select result
23969 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72   into <table> fr
2396a 6f 6d 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a 2a  om R..R+n.    **
2396b 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a           goto C.
2396c 20 20 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e      **      D: .
2396d 2e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64  ...    */.    ad
2396e 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33  drCont = sqlite3
2396f 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
23970 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61  _Yield, dest.iPa
23971 72 6d 29 3b 0a 20 20 20 20 61 64 64 72 49 6e 73  rm);.    addrIns
23972 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Top = sqlite3Vdb
23973 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
23974 2c 20 72 65 67 45 6f 66 29 3b 0a 20 20 7d 0a 0a  , regEof);.  }..
23975 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65    /* Allocate re
23976 67 69 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64  gisters for hold
23977 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 6f 66  ing the rowid of
23978 20 74 68 65 20 6e 65 77 20 72 6f 77 2c 0a 20 20   the new row,.  
23979 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ** the content o
2397a 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2c 20 61  f the new row, a
2397b 6e 64 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65  nd the assemblie
2397c 64 20 72 6f 77 20 72 65 63 6f 72 64 2e 0a 20 20  d row record..  
2397d 2a 2f 0a 20 20 72 65 67 52 65 63 6f 72 64 20 3d  */.  regRecord =
2397e 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2397f 0a 20 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65  .  regRowid = re
23980 67 49 6e 73 20 3d 20 70 50 61 72 73 65 2d 3e 6e  gIns = pParse->n
23981 4d 65 6d 2b 31 3b 0a 20 20 70 50 61 72 73 65 2d  Mem+1;.  pParse-
23982 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e  >nMem += pTab->n
23983 43 6f 6c 20 2b 20 31 3b 0a 20 20 69 66 28 20 49  Col + 1;.  if( I
23984 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
23985 7b 0a 20 20 20 20 72 65 67 52 6f 77 69 64 2b 2b  {.    regRowid++
23986 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
23987 65 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 67 44  em++;.  }.  regD
23988 61 74 61 20 3d 20 72 65 67 52 6f 77 69 64 2b 31  ata = regRowid+1
23989 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20  ;..  /* Run the 
2398a 42 45 46 4f 52 45 20 61 6e 64 20 49 4e 53 54 45  BEFORE and INSTE
2398b 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 2c 20  AD OF triggers, 
2398c 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
2398d 0a 20 20 2a 2f 0a 20 20 65 6e 64 4f 66 4c 6f 6f  .  */.  endOfLoo
2398e 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  p = sqlite3VdbeM
2398f 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 69  akeLabel(v);.  i
23990 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  f( triggers_exis
23991 74 20 26 20 54 52 49 47 47 45 52 5f 42 45 46 4f  t & TRIGGER_BEFO
23992 52 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  RE ){.    int re
23993 67 54 72 69 67 52 6f 77 69 64 3b 0a 20 20 20 20  gTrigRowid;.    
23994 69 6e 74 20 72 65 67 43 6f 6c 73 3b 0a 20 20 20  int regCols;.   
23995 20 69 6e 74 20 72 65 67 52 65 63 3b 0a 0a 20 20   int regRec;..  
23996 20 20 2f 2a 20 62 75 69 6c 64 20 74 68 65 20 4e    /* build the N
23997 45 57 2e 2a 20 72 65 66 65 72 65 6e 63 65 20 72  EW.* reference r
23998 6f 77 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69  ow.  Note that i
23999 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 49 4e  f there is an IN
2399a 54 45 47 45 52 0a 20 20 20 20 2a 2a 20 50 52 49  TEGER.    ** PRI
2399b 4d 41 52 59 20 4b 45 59 20 69 6e 74 6f 20 77 68  MARY KEY into wh
2399c 69 63 68 20 61 20 4e 55 4c 4c 20 69 73 20 62 65  ich a NULL is be
2399d 69 6e 67 20 69 6e 73 65 72 74 65 64 2c 20 74 68  ing inserted, th
2399e 61 74 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 0a  at NULL will be.
2399f 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 65      ** translate
239a0 64 20 69 6e 74 6f 20 61 20 75 6e 69 71 75 65 20  d into a unique 
239a1 49 44 20 66 6f 72 20 74 68 65 20 72 6f 77 2e 20  ID for the row. 
239a2 20 42 75 74 20 6f 6e 20 61 20 42 45 46 4f 52 45   But on a BEFORE
239a3 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a   trigger,.    **
239a4 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20   we do not know 
239a5 77 68 61 74 20 74 68 65 20 75 6e 69 71 75 65 20  what the unique 
239a6 49 44 20 77 69 6c 6c 20 62 65 20 28 62 65 63 61  ID will be (beca
239a7 75 73 65 20 74 68 65 20 69 6e 73 65 72 74 20 68  use the insert h
239a8 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61  as.    ** not ha
239a9 70 70 65 6e 65 64 20 79 65 74 29 20 73 6f 20 77  ppened yet) so w
239aa 65 20 73 75 62 73 74 69 74 75 74 65 20 61 20 72  e substitute a r
239ab 6f 77 69 64 20 6f 66 20 2d 31 0a 20 20 20 20 2a  owid of -1.    *
239ac 2f 0a 20 20 20 20 72 65 67 54 72 69 67 52 6f 77  /.    regTrigRow
239ad 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  id = sqlite3GetT
239ae 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
239af 20 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d      if( keyColum
239b0 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  n<0 ){.      sql
239b1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
239b2 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31  , OP_Integer, -1
239b3 2c 20 72 65 67 54 72 69 67 52 6f 77 69 64 29 3b  , regTrigRowid);
239b4 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75  .    }else if( u
239b5 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20  seTempTable ){. 
239b6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
239b7 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
239b8 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79  umn, srcTab, key
239b9 43 6f 6c 75 6d 6e 2c 20 72 65 67 54 72 69 67 52  Column, regTrigR
239ba 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65  owid);.    }else
239bb 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 0a  {.      int j1;.
239bc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
239bd 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20  elect==0 );  /* 
239be 4f 74 68 65 72 77 69 73 65 20 75 73 65 54 65 6d  Otherwise useTem
239bf 70 54 61 62 6c 65 20 69 73 20 74 72 75 65 20 2a  pTable is true *
239c0 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  /.      sqlite3E
239c1 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
239c2 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75  pList->a[keyColu
239c3 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67 54 72  mn].pExpr, regTr
239c4 69 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  igRowid);.      
239c5 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
239c6 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
239c7 4e 75 6c 6c 2c 20 72 65 67 54 72 69 67 52 6f 77  Null, regTrigRow
239c8 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
239c9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
239ca 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20  OP_Integer, -1, 
239cb 72 65 67 54 72 69 67 52 6f 77 69 64 29 3b 0a 20  regTrigRowid);. 
239cc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
239cd 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
239ce 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
239cf 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d  beAddOp1(v, OP_M
239d0 75 73 74 42 65 49 6e 74 2c 20 72 65 67 54 72 69  ustBeInt, regTri
239d1 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a  gRowid);.    }..
239d2 20 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61      /* Cannot ha
239d3 76 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20 61  ve triggers on a
239d4 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
239d5 49 66 20 69 74 20 77 65 72 65 20 70 6f 73 73 69  If it were possi
239d6 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 74 68 69 73  ble,.    ** this
239d7 20 62 6c 6f 63 6b 20 77 6f 75 6c 64 20 68 61 76   block would hav
239d8 65 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72  e to account for
239d9 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 2e 0a   hidden column..
239da 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
239db 74 28 21 49 73 56 69 72 74 75 61 6c 28 70 54 61  t(!IsVirtual(pTa
239dc 62 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65  b));..    /* Cre
239dd 61 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c 75  ate the new colu
239de 6d 6e 20 64 61 74 61 0a 20 20 20 20 2a 2f 0a 20  mn data.    */. 
239df 20 20 20 72 65 67 43 6f 6c 73 20 3d 20 73 71 6c     regCols = sql
239e0 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
239e1 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e  (pParse, pTab->n
239e2 43 6f 6c 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  Col);.    for(i=
239e3 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
239e4 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
239e5 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20   pColumn==0 ){. 
239e6 20 20 20 20 20 20 20 6a 20 3d 20 69 3b 0a 20 20         j = i;.  
239e7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
239e8 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43     for(j=0; j<pC
239e9 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29  olumn->nId; j++)
239ea 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
239eb 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64  pColumn->a[j].id
239ec 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20  x==i ) break;.  
239ed 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
239ee 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d        if( pColum
239ef 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d  n && j>=pColumn-
239f0 3e 6e 49 64 20 29 7b 0a 20 20 20 20 20 20 20 20  >nId ){.        
239f1 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
239f2 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43  pParse, pTab->aC
239f3 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 72 65 67  ol[i].pDflt, reg
239f4 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 7d  Cols+i);.      }
239f5 65 6c 73 65 20 69 66 28 20 75 73 65 54 65 6d 70  else if( useTemp
239f6 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20  Table ){.       
239f7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
239f8 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
239f9 20 73 72 63 54 61 62 2c 20 6a 2c 20 72 65 67 43   srcTab, j, regC
239fa 6f 6c 73 2b 69 29 3b 20 0a 20 20 20 20 20 20 7d  ols+i); .      }
239fb 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
239fc 73 65 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30  sert( pSelect==0
239fd 20 29 3b 20 2f 2a 20 4f 74 68 65 72 77 69 73 65   ); /* Otherwise
239fe 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73   useTempTable is
239ff 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 20 20 20   true */.       
23a00 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
23a01 41 6e 64 43 61 63 68 65 28 70 50 61 72 73 65 2c  AndCache(pParse,
23a02 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78   pList->a[j].pEx
23a03 70 72 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a  pr, regCols+i);.
23a04 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
23a05 20 20 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74    regRec = sqlit
23a06 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
23a07 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rse);.    sqlite
23a08 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
23a09 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
23a0a 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f  gCols, pTab->nCo
23a0b 6c 2c 20 72 65 67 52 65 63 29 3b 0a 0a 20 20 20  l, regRec);..   
23a0c 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
23a0d 6e 20 49 4e 53 45 52 54 20 6f 6e 20 61 20 76 69  n INSERT on a vi
23a0e 65 77 20 77 69 74 68 20 61 6e 20 49 4e 53 54 45  ew with an INSTE
23a0f 41 44 20 4f 46 20 49 4e 53 45 52 54 20 74 72 69  AD OF INSERT tri
23a10 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20 64 6f 20  gger,.    ** do 
23a11 6e 6f 74 20 61 74 74 65 6d 70 74 20 61 6e 79 20  not attempt any 
23a12 63 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65 66 6f  conversions befo
23a13 72 65 20 61 73 73 65 6d 62 6c 69 6e 67 20 74 68  re assembling th
23a14 65 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2a  e record..    **
23a15 20 49 66 20 74 68 69 73 20 69 73 20 61 20 72 65   If this is a re
23a16 61 6c 20 74 61 62 6c 65 2c 20 61 74 74 65 6d 70  al table, attemp
23a17 74 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73  t conversions as
23a18 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
23a19 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 63 6f  .    ** table co
23a1a 6c 75 6d 6e 20 61 66 66 69 6e 69 74 69 65 73 2e  lumn affinities.
23a1b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
23a1c 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20  !isView ){.     
23a1d 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66   sqlite3TableAff
23a1e 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54 61 62  inityStr(v, pTab
23a1f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
23a20 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
23a21 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77  , OP_Insert, new
23a22 49 64 78 2c 20 72 65 67 52 65 63 2c 20 72 65 67  Idx, regRec, reg
23a23 54 72 69 67 52 6f 77 69 64 29 3b 0a 20 20 20 20  TrigRowid);.    
23a24 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
23a25 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
23a26 67 52 65 63 29 3b 0a 20 20 20 20 73 71 6c 69 74  gRec);.    sqlit
23a27 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
23a28 28 70 50 61 72 73 65 2c 20 72 65 67 54 72 69 67  (pParse, regTrig
23a29 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69  Rowid);.    sqli
23a2a 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
23a2b 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 43  nge(pParse, regC
23a2c 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  ols, pTab->nCol)
23a2d 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65 20 42  ;..    /* Fire B
23a2e 45 46 4f 52 45 20 6f 72 20 49 4e 53 54 45 41 44  EFORE or INSTEAD
23a2f 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0a   OF triggers */.
23a30 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 43      if( sqlite3C
23a31 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
23a32 61 72 73 65 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  arse, TK_INSERT,
23a33 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f   0, TRIGGER_BEFO
23a34 52 45 2c 20 70 54 61 62 2c 20 0a 20 20 20 20 20  RE, pTab, .     
23a35 20 20 20 6e 65 77 49 64 78 2c 20 2d 31 2c 20 6f     newIdx, -1, o
23a36 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f  nError, endOfLoo
23a37 70 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20  p, 0, 0) ){.    
23a38 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
23a39 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d  eanup;.    }.  }
23a3a 0a 0a 20 20 2f 2a 20 50 75 73 68 20 74 68 65 20  ..  /* Push the 
23a3b 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f  record number fo
23a3c 72 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20  r the new entry 
23a3d 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 20  onto the stack. 
23a3e 20 54 68 65 0a 20 20 2a 2a 20 72 65 63 6f 72 64   The.  ** record
23a3f 20 6e 75 6d 62 65 72 20 69 73 20 61 20 72 61 6e   number is a ran
23a40 64 6f 6d 6c 79 20 67 65 6e 65 72 61 74 65 20 69  domly generate i
23a41 6e 74 65 67 65 72 20 63 72 65 61 74 65 64 20 62  nteger created b
23a42 79 20 4e 65 77 52 6f 77 69 64 0a 20 20 2a 2a 20  y NewRowid.  ** 
23a43 65 78 63 65 70 74 20 77 68 65 6e 20 74 68 65 20  except when the 
23a44 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54  table has an INT
23a45 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
23a46 20 63 6f 6c 75 6d 6e 2c 20 69 6e 20 77 68 69 63   column, in whic
23a47 68 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20  h.  ** case the 
23a48 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73  record number is
23a49 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 61   the same as tha
23a4a 74 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2f 0a  t column. .  */.
23a4b 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
23a4c 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75  .    if( IsVirtu
23a4d 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
23a4e 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 74 68 61    /* The row tha
23a4f 74 20 74 68 65 20 56 55 70 64 61 74 65 20 6f 70  t the VUpdate op
23a50 63 6f 64 65 20 77 69 6c 6c 20 64 65 6c 65 74 65  code will delete
23a51 3a 20 6e 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20  : none */.      
23a52 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23a53 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
23a54 20 72 65 67 49 6e 73 29 3b 0a 20 20 20 20 7d 0a   regIns);.    }.
23a55 20 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d      if( keyColum
23a56 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  n>=0 ){.      if
23a57 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29  ( useTempTable )
23a58 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
23a59 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
23a5a 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62  P_Column, srcTab
23a5b 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67  , keyColumn, reg
23a5c 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 65  Rowid);.      }e
23a5d 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20  lse if( pSelect 
23a5e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
23a5f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23a60 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 46 72 6f  OP_SCopy, regFro
23a61 6d 53 65 6c 65 63 74 2b 6b 65 79 43 6f 6c 75 6d  mSelect+keyColum
23a62 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  n, regRowid);.  
23a63 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23a64 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a     VdbeOp *pOp;.
23a65 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
23a66 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
23a67 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75  pList->a[keyColu
23a68 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67 52 6f  mn].pExpr, regRo
23a69 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  wid);.        pO
23a6a 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47  p = sqlite3VdbeG
23a6b 65 74 4f 70 28 76 2c 20 73 71 6c 69 74 65 33 56  etOp(v, sqlite3V
23a6c 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
23a6d 29 20 2d 20 31 29 3b 0a 20 20 20 20 20 20 20 20  ) - 1);.        
23a6e 69 66 28 20 70 4f 70 20 26 26 20 70 4f 70 2d 3e  if( pOp && pOp->
23a6f 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 20  opcode==OP_Null 
23a70 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  && !IsVirtual(pT
23a71 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ab) ){.         
23a72 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b   appendFlag = 1;
23a73 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  .          pOp->
23a74 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 65 77 52  opcode = OP_NewR
23a75 6f 77 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20  owid;.          
23a76 70 4f 70 2d 3e 70 31 20 3d 20 62 61 73 65 43 75  pOp->p1 = baseCu
23a77 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70  r;.          pOp
23a78 2d 3e 70 32 20 3d 20 72 65 67 52 6f 77 69 64 3b  ->p2 = regRowid;
23a79 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  .          pOp->
23a7a 70 33 20 3d 20 72 65 67 41 75 74 6f 69 6e 63 3b  p3 = regAutoinc;
23a7b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
23a7c 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74   }.      /* If t
23a7d 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 65  he PRIMARY KEY e
23a7e 78 70 72 65 73 73 69 6f 6e 20 69 73 20 4e 55 4c  xpression is NUL
23a7f 4c 2c 20 74 68 65 6e 20 75 73 65 20 4f 50 5f 4e  L, then use OP_N
23a80 65 77 52 6f 77 69 64 0a 20 20 20 20 20 20 2a 2a  ewRowid.      **
23a81 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 75   to generate a u
23a82 6e 69 71 75 65 20 70 72 69 6d 61 72 79 20 6b 65  nique primary ke
23a83 79 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a  y value..      *
23a84 2f 0a 20 20 20 20 20 20 69 66 28 20 21 61 70 70  /.      if( !app
23a85 65 6e 64 46 6c 61 67 20 29 7b 0a 20 20 20 20 20  endFlag ){.     
23a86 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 20     int j1;.     
23a87 20 20 20 69 66 28 20 21 49 73 56 69 72 74 75 61     if( !IsVirtua
23a88 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
23a89 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65       j1 = sqlite
23a8a 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
23a8b 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 52 6f  P_NotNull, regRo
23a8c 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  wid);.          
23a8d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23a8e 33 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  3(v, OP_NewRowid
23a8f 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f  , baseCur, regRo
23a90 77 69 64 2c 20 72 65 67 41 75 74 6f 69 6e 63 29  wid, regAutoinc)
23a91 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
23a92 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
23a93 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 20 20  v, j1);.        
23a94 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
23a95 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
23a96 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
23a97 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
23a98 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23a99 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 52 6f  OP_IsNull, regRo
23a9a 77 69 64 2c 20 6a 31 2b 32 29 3b 0a 20 20 20 20  wid, j1+2);.    
23a9b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
23a9c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
23a9d 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
23a9e 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
23a9f 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
23aa0 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
23aa1 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
23aa2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23aa3 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52  OP_Null, 0, regR
23aa4 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65  owid);.    }else
23aa5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
23aa6 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
23aa7 4e 65 77 52 6f 77 69 64 2c 20 62 61 73 65 43 75  NewRowid, baseCu
23aa8 72 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67  r, regRowid, reg
23aa9 41 75 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20  Autoinc);.      
23aaa 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a  appendFlag = 1;.
23aab 20 20 20 20 7d 0a 20 20 20 20 61 75 74 6f 49 6e      }.    autoIn
23aac 63 53 74 65 70 28 70 50 61 72 73 65 2c 20 72 65  cStep(pParse, re
23aad 67 41 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77  gAutoinc, regRow
23aae 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73  id);..    /* Pus
23aaf 68 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  h onto the stack
23ab0 2c 20 64 61 74 61 20 66 6f 72 20 61 6c 6c 20 63  , data for all c
23ab1 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 6e 65  olumns of the ne
23ab2 77 20 65 6e 74 72 79 2c 20 62 65 67 69 6e 6e 69  w entry, beginni
23ab3 6e 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74  ng.    ** with t
23ab4 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 2e  he first column.
23ab5 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 48 69 64  .    */.    nHid
23ab6 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  den = 0;.    for
23ab7 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
23ab8 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
23ab9 69 6e 74 20 69 52 65 67 53 74 6f 72 65 20 3d 20  int iRegStore = 
23aba 72 65 67 52 6f 77 69 64 2b 31 2b 69 3b 0a 20 20  regRowid+1+i;.  
23abb 20 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d      if( i==pTab-
23abc 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
23abd 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f    /* The value o
23abe 66 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52  f the INTEGER PR
23abf 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e  IMARY KEY column
23ac0 20 69 73 20 61 6c 77 61 79 73 20 61 20 4e 55 4c   is always a NUL
23ac1 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 57 68  L..        ** Wh
23ac2 65 6e 65 76 65 72 20 74 68 69 73 20 63 6f 6c 75  enever this colu
23ac3 6d 6e 20 69 73 20 72 65 61 64 2c 20 74 68 65 20  mn is read, the 
23ac4 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69  record number wi
23ac5 6c 6c 20 62 65 20 73 75 62 73 74 69 74 75 74 65  ll be substitute
23ac6 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20  d.        ** in 
23ac7 69 74 73 20 70 6c 61 63 65 2e 20 20 53 6f 20 77  its place.  So w
23ac8 69 6c 6c 20 66 69 6c 6c 20 74 68 69 73 20 63 6f  ill fill this co
23ac9 6c 75 6d 6e 20 77 69 74 68 20 61 20 4e 55 4c 4c  lumn with a NULL
23aca 20 74 6f 20 61 76 6f 69 64 0a 20 20 20 20 20 20   to avoid.      
23acb 20 20 2a 2a 20 74 61 6b 69 6e 67 20 75 70 20 64    ** taking up d
23acc 61 74 61 20 73 70 61 63 65 20 77 69 74 68 20 69  ata space with i
23acd 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20  nformation that 
23ace 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73  will never be us
23acf 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 73  ed. */.        s
23ad0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23ad1 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
23ad2 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20  iRegStore);.    
23ad3 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
23ad4 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
23ad5 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20  pColumn==0 ){.  
23ad6 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64        if( IsHidd
23ad7 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e  enColumn(&pTab->
23ad8 61 43 6f 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20  aCol[i]) ){.    
23ad9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 49 73        assert( Is
23ada 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b  Virtual(pTab) );
23adb 0a 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20 2d  .          j = -
23adc 31 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 48 69  1;.          nHi
23add 64 64 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20  dden++;.        
23ade 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
23adf 20 6a 20 3d 20 69 20 2d 20 6e 48 69 64 64 65 6e   j = i - nHidden
23ae0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
23ae1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
23ae2 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c   for(j=0; j<pCol
23ae3 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a  umn->nId; j++){.
23ae4 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
23ae5 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d  olumn->a[j].idx=
23ae6 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =i ) break;.    
23ae7 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
23ae8 20 20 20 20 69 66 28 20 6a 3c 30 20 7c 7c 20 6e      if( j<0 || n
23ae9 43 6f 6c 75 6d 6e 3d 3d 30 20 7c 7c 20 28 70 43  Column==0 || (pC
23aea 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c  olumn && j>=pCol
23aeb 75 6d 6e 2d 3e 6e 49 64 29 20 29 7b 0a 20 20 20  umn->nId) ){.   
23aec 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
23aed 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61  Code(pParse, pTa
23aee 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74  b->aCol[i].pDflt
23aef 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20  , iRegStore);.  
23af0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73      }else if( us
23af1 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
23af2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23af3 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
23af4 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c  lumn, srcTab, j,
23af5 20 69 52 65 67 53 74 6f 72 65 29 3b 20 0a 20 20   iRegStore); .  
23af6 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53      }else if( pS
23af7 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
23af8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23af9 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
23afa 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6a 2c  regFromSelect+j,
23afb 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20   iRegStore);.   
23afc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
23afd 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
23afe 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d  e(pParse, pList-
23aff 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 52 65  >a[j].pExpr, iRe
23b00 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d  gStore);.      }
23b01 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47  .    }..    /* G
23b02 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
23b03 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74  check constraint
23b04 73 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 69  s and generate i
23b05 6e 64 65 78 20 6b 65 79 73 20 61 6e 64 0a 20 20  ndex keys and.  
23b06 20 20 2a 2a 20 64 6f 20 74 68 65 20 69 6e 73 65    ** do the inse
23b07 72 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69  rtion..    */.#i
23b08 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
23b09 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
23b0a 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c     if( IsVirtual
23b0b 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
23b0c 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57  sqlite3VtabMakeW
23b0d 72 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20  ritable(pParse, 
23b0e 70 54 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c  pTab);.      sql
23b0f 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
23b10 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20 31 2c  , OP_VUpdate, 1,
23b11 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 72   pTab->nCol+2, r
23b12 65 67 49 6e 73 2c 0a 20 20 20 20 20 20 20 20 20  egIns,.         
23b13 20 20 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e              (con
23b14 73 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70  st char*)pTab->p
23b15 56 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a  Vtab, P4_VTAB);.
23b16 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
23b17 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c  .    {.      sql
23b18 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73  ite3GenerateCons
23b19 74 72 61 69 6e 74 43 68 65 63 6b 73 28 0a 20 20  traintChecks(.  
23b1a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 0a          pParse,.
23b1b 20 20 20 20 20 20 20 20 20 20 70 54 61 62 2c 0a            pTab,.
23b1c 20 20 20 20 20 20 20 20 20 20 62 61 73 65 43 75            baseCu
23b1d 72 2c 0a 20 20 20 20 20 20 20 20 20 20 72 65 67  r,.          reg
23b1e 49 6e 73 2c 0a 20 20 20 20 20 20 20 20 20 20 61  Ins,.          a
23b1f 52 65 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20  RegIdx,.        
23b20 20 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 2c 0a    keyColumn>=0,.
23b21 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20            0,.   
23b22 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 2c 0a         onError,.
23b23 20 20 20 20 20 20 20 20 20 20 65 6e 64 4f 66 4c            endOfL
23b24 6f 6f 70 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  oop.      );.   
23b25 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65     sqlite3Comple
23b26 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20 20  teInsertion(.   
23b27 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 0a 20         pParse,. 
23b28 20 20 20 20 20 20 20 20 20 70 54 61 62 2c 0a 20           pTab,. 
23b29 20 20 20 20 20 20 20 20 20 62 61 73 65 43 75 72           baseCur
23b2a 2c 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 49  ,.          regI
23b2b 6e 73 2c 0a 20 20 20 20 20 20 20 20 20 20 61 52  ns,.          aR
23b2c 65 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20  egIdx,.         
23b2d 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 28 74   0,.          (t
23b2e 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 26 20  riggers_exist & 
23b2f 54 52 49 47 47 45 52 5f 41 46 54 45 52 29 21 3d  TRIGGER_AFTER)!=
23b30 30 20 3f 20 6e 65 77 49 64 78 20 3a 20 2d 31 2c  0 ? newIdx : -1,
23b31 0a 20 20 20 20 20 20 20 20 20 20 61 70 70 65 6e  .          appen
23b32 64 46 6c 61 67 0a 20 20 20 20 20 20 20 29 3b 0a  dFlag.       );.
23b33 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
23b34 55 70 64 61 74 65 20 74 68 65 20 63 6f 75 6e 74  Update the count
23b35 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 61 72   of rows that ar
23b36 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f 0a  e inserted.  */.
23b37 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
23b38 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52   & SQLITE_CountR
23b39 6f 77 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  ows)!=0 ){.    s
23b3a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23b3b 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72  (v, OP_AddImm, r
23b3c 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a  egRowCount, 1);.
23b3d 20 20 7d 0a 0a 20 20 69 66 28 20 74 72 69 67 67    }..  if( trigg
23b3e 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20  ers_exist ){.   
23b3f 20 2f 2a 20 43 6f 64 65 20 41 46 54 45 52 20 74   /* Code AFTER t
23b40 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 69  riggers */.    i
23b41 66 28 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  f( sqlite3CodeRo
23b42 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  wTrigger(pParse,
23b43 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54   TK_INSERT, 0, T
23b44 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70 54  RIGGER_AFTER, pT
23b45 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 65  ab,.          ne
23b46 77 49 64 78 2c 20 2d 31 2c 20 6f 6e 45 72 72 6f  wIdx, -1, onErro
23b47 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 30 2c  r, endOfLoop, 0,
23b48 20 30 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74   0) ){.      got
23b49 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
23b4a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
23b4b 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  * The bottom of 
23b4c 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72 74 69  the main inserti
23b4d 6f 6e 20 6c 6f 6f 70 2c 20 69 66 20 74 68 65 20  on loop, if the 
23b4e 64 61 74 61 20 73 6f 75 72 63 65 0a 20 20 2a 2a  data source.  **
23b4f 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74 61   is a SELECT sta
23b50 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73  tement..  */.  s
23b51 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
23b52 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c  eLabel(v, endOfL
23b53 6f 6f 70 29 3b 0a 20 20 69 66 28 20 75 73 65 54  oop);.  if( useT
23b54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
23b55 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23b56 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 72  2(v, OP_Next, sr
23b57 63 54 61 62 2c 20 61 64 64 72 43 6f 6e 74 29 3b  cTab, addrCont);
23b58 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23b59 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
23b5a 49 6e 73 54 6f 70 29 3b 0a 20 20 20 20 73 71 6c  InsTop);.    sql
23b5b 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
23b5c 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 73 72 63 54  , OP_Close, srcT
23b5d 61 62 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ab);.  }else if(
23b5e 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
23b5f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23b60 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
23b61 20 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20   addrCont);.    
23b62 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
23b63 65 72 65 28 76 2c 20 61 64 64 72 49 6e 73 54 6f  ere(v, addrInsTo
23b64 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  p);.  }..  if( !
23b65 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
23b66 26 26 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  && !isView ){.  
23b67 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74    /* Close all t
23b68 61 62 6c 65 73 20 6f 70 65 6e 65 64 20 2a 2f 0a  ables opened */.
23b69 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23b6a 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
23b6b 65 2c 20 62 61 73 65 43 75 72 29 3b 0a 20 20 20  e, baseCur);.   
23b6c 20 66 6f 72 28 69 64 78 3d 31 2c 20 70 49 64 78   for(idx=1, pIdx
23b6d 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
23b6e 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
23b6f 70 4e 65 78 74 2c 20 69 64 78 2b 2b 29 7b 0a 20  pNext, idx++){. 
23b70 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23b71 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
23b72 73 65 2c 20 69 64 78 2b 62 61 73 65 43 75 72 29  se, idx+baseCur)
23b73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
23b74 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 71 6c  * Update the sql
23b75 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
23b76 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74 68  le by storing th
23b77 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
23b78 0a 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 76 61  .  ** counter va
23b79 6c 75 65 20 69 6e 20 6d 65 6d 6f 72 79 20 72 65  lue in memory re
23b7a 67 41 75 74 6f 69 6e 63 20 62 61 63 6b 20 69 6e  gAutoinc back in
23b7b 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65  to the sqlite_se
23b7c 71 75 65 6e 63 65 0a 20 20 2a 2a 20 74 61 62 6c  quence.  ** tabl
23b7d 65 2e 0a 20 20 2a 2f 0a 20 20 61 75 74 6f 49 6e  e..  */.  autoIn
23b7e 63 45 6e 64 28 70 50 61 72 73 65 2c 20 69 44 62  cEnd(pParse, iDb
23b7f 2c 20 70 54 61 62 2c 20 72 65 67 41 75 74 6f 69  , pTab, regAutoi
23b80 6e 63 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  nc);..  /*.  ** 
23b81 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
23b82 72 20 6f 66 20 72 6f 77 73 20 69 6e 73 65 72 74  r of rows insert
23b83 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74  ed. If this rout
23b84 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e  ine is .  ** gen
23b85 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63  erating code bec
23b86 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74  ause of a call t
23b87 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  o sqlite3NestedP
23b88 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20  arse(), do not. 
23b89 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63   ** invoke the c
23b8a 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
23b8b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
23b8c 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
23b8d 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 70 50 61  CountRows && pPa
23b8e 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26  rse->nested==0 &
23b8f 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53  & !pParse->trigS
23b90 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69  tack ){.    sqli
23b91 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23b92 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72   OP_ResultRow, r
23b93 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a  egRowCount, 1);.
23b94 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
23b95 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
23b96 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23b97 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
23b98 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
23b99 72 6f 77 73 20 69 6e 73 65 72 74 65 64 22 2c 20  rows inserted", 
23b9a 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
23b9b 20 20 7d 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61    }..insert_clea
23b9c 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  nup:.  sqlite3Sr
23b9d 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
23b9e 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c  pTabList);.  sql
23b9f 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
23ba0 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
23ba1 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
23ba2 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
23ba3 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69  );.  sqlite3IdLi
23ba4 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  stDelete(db, pCo
23ba5 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33  lumn);.  sqlite3
23ba6 44 62 46 72 65 65 28 64 62 2c 20 61 52 65 67 49  DbFree(db, aRegI
23ba7 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  dx);.}../*.** Ge
23ba8 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64  nerate code to d
23ba9 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65  o constraint che
23baa 63 6b 73 20 70 72 69 6f 72 20 74 6f 20 61 6e 20  cks prior to an 
23bab 49 4e 53 45 52 54 20 6f 72 20 61 6e 20 55 50 44  INSERT or an UPD
23bac 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ATE..**.** The i
23bad 6e 70 75 74 20 69 73 20 61 20 72 61 6e 67 65 20  nput is a range 
23bae 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72  of consecutive r
23baf 65 67 69 73 74 65 72 73 20 61 73 20 66 6f 6c 6c  egisters as foll
23bb0 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e  ows:.**.**    1.
23bb1 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74    The rowid of t
23bb2 68 65 20 72 6f 77 20 74 6f 20 62 65 20 75 70 64  he row to be upd
23bb3 61 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  ated before the 
23bb4 75 70 64 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a  update.  This.**
23bb5 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 69 73          value is
23bb6 20 6f 6d 69 74 74 65 64 20 75 6e 6c 65 73 73 20   omitted unless 
23bb7 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 6e 20  we are doing an 
23bb8 55 50 44 41 54 45 20 74 68 61 74 20 69 6e 76 6f  UPDATE that invo
23bb9 6c 76 65 73 20 61 0a 2a 2a 20 20 20 20 20 20 20  lves a.**       
23bba 20 63 68 61 6e 67 65 20 74 6f 20 74 68 65 20 72   change to the r
23bbb 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 72 20  ecord number or 
23bbc 77 72 69 74 69 6e 67 20 74 6f 20 61 20 76 69 72  writing to a vir
23bbd 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  tual table..**.*
23bbe 2a 20 20 20 20 32 2e 20 20 54 68 65 20 72 6f 77  *    2.  The row
23bbf 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20 61 66  id of the row af
23bc0 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a  ter the update..
23bc1 2a 2a 0a 2a 2a 20 20 20 20 33 2e 20 20 54 68 65  **.**    3.  The
23bc2 20 64 61 74 61 20 69 6e 20 74 68 65 20 66 69 72   data in the fir
23bc3 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  st column of the
23bc4 20 65 6e 74 72 79 20 61 66 74 65 72 20 74 68 65   entry after the
23bc5 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20   update..**.**  
23bc6 20 20 69 2e 20 20 44 61 74 61 20 66 72 6f 6d 20    i.  Data from 
23bc7 6d 69 64 64 6c 65 20 63 6f 6c 75 6d 6e 73 2e 2e  middle columns..
23bc8 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 2e 20 20 54  ..**.**    N.  T
23bc9 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 6c  he data in the l
23bca 61 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  ast column of th
23bcb 65 20 65 6e 74 72 79 20 61 66 74 65 72 20 74 68  e entry after th
23bcc 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  e update..**.** 
23bcd 54 68 65 20 72 65 67 52 6f 77 69 64 20 70 61 72  The regRowid par
23bce 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 69 6e  ameter is the in
23bcf 64 65 78 20 6f 66 20 74 68 65 20 72 65 67 69 73  dex of the regis
23bd0 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 28  ter containing (
23bd1 32 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c  2)..**.** The ol
23bd2 64 20 72 6f 77 69 64 20 73 68 6f 77 6e 20 61 73  d rowid shown as
23bd3 20 65 6e 74 72 79 20 28 31 29 20 61 62 6f 76 65   entry (1) above
23bd4 20 69 73 20 6f 6d 69 74 74 65 64 20 75 6e 6c 65   is omitted unle
23bd5 73 73 20 62 6f 74 68 20 69 73 55 70 64 61 74 65  ss both isUpdate
23bd6 0a 2a 2a 20 61 6e 64 20 72 6f 77 69 64 43 68 6e  .** and rowidChn
23bd7 67 20 61 72 65 20 31 2e 20 20 69 73 55 70 64 61  g are 1.  isUpda
23bd8 74 65 20 69 73 20 74 72 75 65 20 66 6f 72 20 55  te is true for U
23bd9 50 44 41 54 45 73 20 61 6e 64 20 66 61 6c 73 65  PDATEs and false
23bda 20 66 6f 72 0a 2a 2a 20 49 4e 53 45 52 54 73 2e   for.** INSERTs.
23bdb 20 20 52 6f 77 69 64 43 68 6e 67 20 6d 65 61 6e    RowidChng mean
23bdc 73 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 72  s that the new r
23bdd 6f 77 69 64 20 69 73 20 65 78 70 6c 69 63 69 74  owid is explicit
23bde 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 79 0a  ly specified by.
23bdf 2a 2a 20 74 68 65 20 75 70 64 61 74 65 20 6f 72  ** the update or
23be0 20 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65 6e   insert statemen
23be1 74 2e 20 20 49 66 20 72 6f 77 69 64 43 68 6e 67  t.  If rowidChng
23be2 20 69 73 20 66 61 6c 73 65 2c 20 69 74 20 6d 65   is false, it me
23be3 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ans that.** the 
23be4 72 6f 77 69 64 20 69 73 20 63 6f 6d 70 75 74 65  rowid is compute
23be5 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
23be6 69 6e 20 61 6e 20 69 6e 73 65 72 74 20 6f 72 20  in an insert or 
23be7 74 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 76  that the rowid v
23be8 61 6c 75 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 6d  alue.** is not m
23be9 6f 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75  odified by the u
23bea 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  pdate..**.** The
23beb 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
23bec 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
23bed 73 74 6f 72 65 20 6e 65 77 20 69 6e 64 65 78 20  store new index 
23bee 65 6e 74 72 69 65 73 20 69 6e 74 6f 0a 2a 2a 20  entries into.** 
23bef 72 65 67 69 73 74 65 72 73 20 69 64 65 6e 74 69  registers identi
23bf0 66 69 65 64 20 62 79 20 61 52 65 67 49 64 78 5b  fied by aRegIdx[
23bf1 5d 2e 20 20 4e 6f 20 69 6e 64 65 78 20 65 6e 74  ].  No index ent
23bf2 72 79 20 69 73 20 63 72 65 61 74 65 64 20 66 6f  ry is created fo
23bf3 72 0a 2a 2a 20 69 6e 64 69 63 65 73 20 77 68 65  r.** indices whe
23bf4 72 65 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30  re aRegIdx[i]==0
23bf5 2e 20 20 54 68 65 20 6f 72 64 65 72 20 6f 66 20  .  The order of 
23bf6 69 6e 64 69 63 65 73 20 69 6e 20 61 52 65 67 49  indices in aRegI
23bf7 64 78 5b 5d 20 69 73 0a 2a 2a 20 74 68 65 20 73  dx[] is.** the s
23bf8 61 6d 65 20 61 73 20 74 68 65 20 6f 72 64 65 72  ame as the order
23bf9 20 6f 66 20 69 6e 64 69 63 65 73 20 6f 6e 20 74   of indices on t
23bfa 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f  he linked list o
23bfb 66 20 69 6e 64 69 63 65 73 0a 2a 2a 20 61 74 74  f indices.** att
23bfc 61 63 68 65 64 20 74 6f 20 74 68 65 20 74 61 62  ached to the tab
23bfd 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  le..**.** This r
23bfe 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65  outine also gene
23bff 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20 63 68  rates code to ch
23c00 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  eck constraints.
23c01 20 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 2a 2a 20 43    NOT NULL,.** C
23c02 48 45 43 4b 2c 20 61 6e 64 20 55 4e 49 51 55 45  HECK, and UNIQUE
23c03 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65   constraints are
23c04 20 61 6c 6c 20 63 68 65 63 6b 65 64 2e 20 20 49   all checked.  I
23c05 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  f a constraint f
23c06 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  ails,.** then th
23c07 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 61 63  e appropriate ac
23c08 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65  tion is performe
23c09 64 2e 20 20 54 68 65 72 65 20 61 72 65 20 66 69  d.  There are fi
23c0a 76 65 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 61  ve possible.** a
23c0b 63 74 69 6f 6e 73 3a 20 52 4f 4c 4c 42 41 43 4b  ctions: ROLLBACK
23c0c 2c 20 41 42 4f 52 54 2c 20 46 41 49 4c 2c 20 52  , ABORT, FAIL, R
23c0d 45 50 4c 41 43 45 2c 20 61 6e 64 20 49 47 4e 4f  EPLACE, and IGNO
23c0e 52 45 2e 0a 2a 2a 0a 2a 2a 20 20 43 6f 6e 73 74  RE..**.**  Const
23c0f 72 61 69 6e 74 20 74 79 70 65 20 20 41 63 74 69  raint type  Acti
23c10 6f 6e 20 20 20 20 20 20 20 57 68 61 74 20 48 61  on       What Ha
23c11 70 70 65 6e 73 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d  ppens.**  ------
23c12 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d  ---------  -----
23c13 2d 2d 2d 2d 2d 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  -----   --------
23c14 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
23c15 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
23c16 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20  .**  any        
23c17 20 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 20        ROLLBACK  
23c18 20 20 20 54 68 65 20 63 75 72 72 65 6e 74 20 74     The current t
23c19 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
23c1a 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 0a 2a 2a  lled back and.**
23c1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c1d 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72  sqlite3_exec() r
23c1e 65 74 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65  eturns immediate
23c1f 6c 79 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20  ly with a.**    
23c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c21 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
23c22 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54  rn code of SQLIT
23c23 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a  E_CONSTRAINT..**
23c24 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20  .**  any        
23c25 20 20 20 20 20 20 41 42 4f 52 54 20 20 20 20 20        ABORT     
23c26 20 20 20 42 61 63 6b 20 6f 75 74 20 63 68 61 6e     Back out chan
23c27 67 65 73 20 66 72 6f 6d 20 74 68 65 20 63 75 72  ges from the cur
23c28 72 65 6e 74 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20  rent command.** 
23c29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
23c2b 6e 6c 79 20 28 64 6f 20 6e 6f 74 20 64 6f 20 61  nly (do not do a
23c2c 20 63 6f 6d 70 6c 65 74 65 20 72 6f 6c 6c 62 61   complete rollba
23c2d 63 6b 29 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20  ck) then.**     
23c2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c2f 20 20 20 20 20 20 20 20 20 20 20 63 61 75 73 65             cause
23c30 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
23c31 74 6f 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69  to return immedi
23c32 61 74 65 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20  ately.**        
23c33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c34 20 20 20 20 20 20 20 20 77 69 74 68 20 53 51 4c          with SQL
23c35 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a  ITE_CONSTRAINT..
23c36 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20  **.**  any      
23c37 20 20 20 20 20 20 20 20 46 41 49 4c 20 20 20 20          FAIL    
23c38 20 20 20 20 20 53 71 6c 69 74 65 5f 65 78 65 63       Sqlite_exec
23c39 28 29 20 72 65 74 75 72 6e 73 20 69 6d 6d 65 64  () returns immed
23c3a 69 61 74 65 6c 79 20 77 69 74 68 20 61 0a 2a 2a  iately with a.**
23c3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c3d 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53  return code of S
23c3e 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
23c3f 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20  .  The.**       
23c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c41 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 61 63           transac
23c42 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c  tion is not roll
23c43 65 64 20 62 61 63 6b 20 61 6e 64 20 61 6e 79 0a  ed back and any.
23c44 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
23c45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c46 20 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20    prior changes 
23c47 61 72 65 20 72 65 74 61 69 6e 65 64 2e 0a 2a 2a  are retained..**
23c48 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20  .**  any        
23c49 20 20 20 20 20 20 49 47 4e 4f 52 45 20 20 20 20        IGNORE    
23c4a 20 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75     The record nu
23c4b 6d 62 65 72 20 61 6e 64 20 64 61 74 61 20 69 73  mber and data is
23c4c 20 70 6f 70 70 65 64 20 66 72 6f 6d 0a 2a 2a 20   popped from.** 
23c4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
23c4f 68 65 20 73 74 61 63 6b 20 61 6e 64 20 74 68 65  he stack and the
23c50 72 65 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61  re is an immedia
23c51 74 65 20 6a 75 6d 70 0a 2a 2a 20 20 20 20 20 20  te jump.**      
23c52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c53 20 20 20 20 20 20 20 20 20 20 74 6f 20 6c 61 62            to lab
23c54 65 6c 20 69 67 6e 6f 72 65 44 65 73 74 2e 0a 2a  el ignoreDest..*
23c55 2a 0a 2a 2a 20 20 4e 4f 54 20 4e 55 4c 4c 20 20  *.**  NOT NULL  
23c56 20 20 20 20 20 20 20 52 45 50 4c 41 43 45 20 20         REPLACE  
23c57 20 20 20 20 54 68 65 20 4e 55 4c 4c 20 76 61 6c      The NULL val
23c58 75 65 20 69 73 20 72 65 70 6c 61 63 65 20 62 79  ue is replace by
23c59 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20   the default.** 
23c5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
23c5c 61 6c 75 65 20 66 6f 72 20 74 68 61 74 20 63 6f  alue for that co
23c5d 6c 75 6d 6e 2e 20 20 49 66 20 74 68 65 20 64 65  lumn.  If the de
23c5e 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 20  fault value.**  
23c5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73                is
23c61 20 4e 55 4c 4c 2c 20 74 68 65 20 61 63 74 69 6f   NULL, the actio
23c62 6e 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  n is the same as
23c63 20 41 42 4f 52 54 2e 0a 2a 2a 0a 2a 2a 20 20 55   ABORT..**.**  U
23c64 4e 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20  NIQUE           
23c65 52 45 50 4c 41 43 45 20 20 20 20 20 20 54 68 65  REPLACE      The
23c66 20 6f 74 68 65 72 20 72 6f 77 20 74 68 61 74 20   other row that 
23c67 63 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 74  conflicts with t
23c68 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20  he row.**       
23c69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c6a 20 20 20 20 20 20 20 20 20 62 65 69 6e 67 20 69           being i
23c6b 6e 73 65 72 74 65 64 20 69 73 20 72 65 6d 6f 76  nserted is remov
23c6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 43 48 45 43 4b  ed..**.**  CHECK
23c6d 20 20 20 20 20 20 20 20 20 20 20 20 52 45 50 4c              REPL
23c6e 41 43 45 20 20 20 20 20 20 49 6c 6c 65 67 61 6c  ACE      Illegal
23c6f 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 69  .  The results i
23c70 6e 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a  n an exception..
23c71 2a 2a 0a 2a 2a 20 57 68 69 63 68 20 61 63 74 69  **.** Which acti
23c72 6f 6e 20 74 6f 20 74 61 6b 65 20 69 73 20 64 65  on to take is de
23c73 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
23c74 6f 76 65 72 72 69 64 65 45 72 72 6f 72 20 70 61  overrideError pa
23c75 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 72 20 69  rameter..** Or i
23c76 66 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3d  f overrideError=
23c77 3d 4f 45 5f 44 65 66 61 75 6c 74 2c 20 74 68 65  =OE_Default, the
23c78 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 6f 6e  n the pParse->on
23c79 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72 0a  Error parameter.
23c7a 2a 2a 20 69 73 20 75 73 65 64 2e 20 20 4f 72 20  ** is used.  Or 
23c7b 69 66 20 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72  if pParse->onErr
23c7c 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 74  or==OE_Default t
23c7d 68 65 6e 20 74 68 65 20 6f 6e 45 72 72 6f 72 20  hen the onError 
23c7e 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65  value.** for the
23c7f 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75   constraint is u
23c80 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  sed..**.** The c
23c81 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d  alling routine m
23c82 75 73 74 20 6f 70 65 6e 20 61 20 72 65 61 64 2f  ust open a read/
23c83 77 72 69 74 65 20 63 75 72 73 6f 72 20 66 6f 72  write cursor for
23c84 20 70 54 61 62 20 77 69 74 68 0a 2a 2a 20 63 75   pTab with.** cu
23c85 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 62 61 73  rsor number "bas
23c86 65 43 75 72 22 2e 20 20 41 6c 6c 20 69 6e 64 69  eCur".  All indi
23c87 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74  ces of pTab must
23c88 20 61 6c 73 6f 20 68 61 76 65 20 6f 70 65 6e 0a   also have open.
23c89 2a 2a 20 72 65 61 64 2f 77 72 69 74 65 20 63 75  ** read/write cu
23c8a 72 73 6f 72 73 20 77 69 74 68 20 63 75 72 73 6f  rsors with curso
23c8b 72 20 6e 75 6d 62 65 72 20 62 61 73 65 43 75 72  r number baseCur
23c8c 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20  +i for the i-th 
23c8d 63 75 72 73 6f 72 2e 0a 2a 2a 20 45 78 63 65 70  cursor..** Excep
23c8e 74 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  t, if there is n
23c8f 6f 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66  o possibility of
23c90 20 61 20 52 45 50 4c 41 43 45 20 61 63 74 69 6f   a REPLACE actio
23c91 6e 20 74 68 65 6e 0a 2a 2a 20 63 75 72 73 6f 72  n then.** cursor
23c92 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  s do not need to
23c93 20 62 65 20 6f 70 65 6e 20 66 6f 72 20 69 6e 64   be open for ind
23c94 69 63 65 73 20 77 68 65 72 65 20 61 52 65 67 49  ices where aRegI
23c95 64 78 5b 69 5d 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c  dx[i]==0..*/.SQL
23c96 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
23c97 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
23c98 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
23c99 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
23c9a 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  e,      /* The p
23c9b 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
23c9c 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
23c9d 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 74 61         /* the ta
23c9e 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
23c9f 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
23ca0 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72  */.  int baseCur
23ca1 2c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  ,        /* Inde
23ca2 78 20 6f 66 20 61 20 72 65 61 64 2f 77 72 69 74  x of a read/writ
23ca3 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  e cursor pointin
23ca4 67 20 61 74 20 70 54 61 62 20 2a 2f 0a 20 20 69  g at pTab */.  i
23ca5 6e 74 20 72 65 67 52 6f 77 69 64 2c 20 20 20 20  nt regRowid,    
23ca6 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
23ca7 68 65 20 72 61 6e 67 65 20 6f 66 20 69 6e 70 75  he range of inpu
23ca8 74 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 20  t registers */. 
23ca9 20 69 6e 74 20 2a 61 52 65 67 49 64 78 2c 20 20   int *aRegIdx,  
23caa 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
23cab 20 75 73 65 64 20 62 79 20 65 61 63 68 20 69 6e   used by each in
23cac 64 65 78 2e 20 20 30 20 66 6f 72 20 75 6e 75 73  dex.  0 for unus
23cad 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  ed indices */.  
23cae 69 6e 74 20 72 6f 77 69 64 43 68 6e 67 2c 20 20  int rowidChng,  
23caf 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
23cb0 68 65 20 72 6f 77 69 64 20 6d 69 67 68 74 20 63  he rowid might c
23cb1 6f 6c 6c 69 64 65 20 77 69 74 68 20 65 78 69 73  ollide with exis
23cb2 74 69 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20  ting entry */.  
23cb3 69 6e 74 20 69 73 55 70 64 61 74 65 2c 20 20 20  int isUpdate,   
23cb4 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
23cb5 55 50 44 41 54 45 2c 20 46 61 6c 73 65 20 66 6f  UPDATE, False fo
23cb6 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e  r INSERT */.  in
23cb7 74 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 2c  t overrideError,
23cb8 20 20 2f 2a 20 4f 76 65 72 72 69 64 65 20 6f 6e    /* Override on
23cb9 45 72 72 6f 72 20 74 6f 20 74 68 69 73 20 69 66  Error to this if
23cba 20 6e 6f 74 20 4f 45 5f 44 65 66 61 75 6c 74 20   not OE_Default 
23cbb 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 44  */.  int ignoreD
23cbc 65 73 74 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70  est      /* Jump
23cbd 20 74 6f 20 74 68 69 73 20 6c 61 62 65 6c 20 6f   to this label o
23cbe 6e 20 61 6e 20 4f 45 5f 49 67 6e 6f 72 65 20 72  n an OE_Ignore r
23cbf 65 73 6f 6c 75 74 69 6f 6e 20 2a 2f 0a 29 7b 0a  esolution */.){.
23cc0 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20    int i;.  Vdbe 
23cc1 2a 76 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a  *v;.  int nCol;.
23cc2 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 3b 0a 20    int onError;. 
23cc3 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20   int j1;        
23cc4 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 73       /* Addresss
23cc5 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63   of jump instruc
23cc6 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6a 32  tion */.  int j2
23cc7 20 3d 20 30 2c 20 6a 33 3b 20 20 20 20 20 2f 2a   = 0, j3;     /*
23cc8 20 41 64 64 72 65 73 73 65 73 20 6f 66 20 6a 75   Addresses of ju
23cc9 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  mp instructions 
23cca 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
23ccb 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  ;        /* Regi
23ccc 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ster containing 
23ccd 66 69 72 73 74 20 64 61 74 61 20 63 6f 6c 75 6d  first data colum
23cce 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  n */.  int iCur;
23ccf 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a  .  Index *pIdx;.
23cd0 20 20 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63    int seenReplac
23cd1 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73  e = 0;.  int has
23cd2 54 77 6f 52 6f 77 69 64 73 20 3d 20 28 69 73 55  TwoRowids = (isU
23cd3 70 64 61 74 65 20 26 26 20 72 6f 77 69 64 43 68  pdate && rowidCh
23cd4 6e 67 29 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69  ng);..  v = sqli
23cd5 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
23cd6 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21  e);.  assert( v!
23cd7 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
23cd8 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30  pTab->pSelect==0
23cd9 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62   );  /* This tab
23cda 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57  le is not a VIEW
23cdb 20 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61   */.  nCol = pTa
23cdc 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44 61  b->nCol;.  regDa
23cdd 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20  ta = regRowid + 
23cde 31 3b 0a 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61  1;...  /* Test a
23cdf 6c 6c 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73  ll NOT NULL cons
23ce0 74 72 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20  traints..  */.  
23ce1 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
23ce2 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69   i++){.    if( i
23ce3 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
23ce4 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
23ce5 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6e 45 72 72  .    }.    onErr
23ce6 6f 72 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  or = pTab->aCol[
23ce7 69 5d 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20  i].notNull;.    
23ce8 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
23ce9 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  None ) continue;
23cea 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64  .    if( overrid
23ceb 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75  eError!=OE_Defau
23cec 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72  lt ){.      onEr
23ced 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72  ror = overrideEr
23cee 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ror;.    }else i
23cef 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  f( onError==OE_D
23cf0 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
23cf1 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f  onError = OE_Abo
23cf2 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rt;.    }.    if
23cf3 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  ( onError==OE_Re
23cf4 70 6c 61 63 65 20 26 26 20 70 54 61 62 2d 3e 61  place && pTab->a
23cf5 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30 20  Col[i].pDflt==0 
23cf6 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
23cf7 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
23cf8 20 7d 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69   }.    j1 = sqli
23cf9 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
23cfa 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67   OP_NotNull, reg
23cfb 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 61 73 73  Data+i);.    ass
23cfc 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  ert( onError==OE
23cfd 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45  _Rollback || onE
23cfe 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c  rror==OE_Abort |
23cff 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61  | onError==OE_Fa
23d00 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e  il.        || on
23d01 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65  Error==OE_Ignore
23d02 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
23d03 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 73  Replace );.    s
23d04 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29  witch( onError )
23d05 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f  {.      case OE_
23d06 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20  Rollback:.      
23d07 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20  case OE_Abort:. 
23d08 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69       case OE_Fai
23d09 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 63 68 61  l: {.        cha
23d0a 72 20 2a 7a 4d 73 67 3b 0a 20 20 20 20 20 20 20  r *zMsg;.       
23d0b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23d0c 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p2(v, OP_Halt, S
23d0d 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
23d0e 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a 20 20 20 20  , onError);.    
23d0f 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74      zMsg = sqlit
23d10 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65  e3MPrintf(pParse
23d11 2d 3e 64 62 2c 20 22 25 73 2e 25 73 20 6d 61 79  ->db, "%s.%s may
23d12 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 0a 20   not be NULL",. 
23d13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23d14 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61               pTa
23d15 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  b->zName, pTab->
23d16 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  aCol[i].zName);.
23d17 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
23d18 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
23d19 31 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41  1, zMsg, P4_DYNA
23d1a 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62 72  MIC);.        br
23d1b 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
23d1c 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
23d1d 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e: {.        sql
23d1e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
23d1f 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67  , OP_Goto, 0, ig
23d20 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20  noreDest);.     
23d21 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
23d22 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f  }.      case OE_
23d23 52 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20  Replace: {.     
23d24 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
23d25 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  de(pParse, pTab-
23d26 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20  >aCol[i].pDflt, 
23d27 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20  regData+i);.    
23d28 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
23d29 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
23d2a 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
23d2b 28 76 2c 20 6a 31 29 3b 0a 20 20 7d 0a 0a 20 20  (v, j1);.  }..  
23d2c 2f 2a 20 54 65 73 74 20 61 6c 6c 20 43 48 45 43  /* Test all CHEC
23d2d 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  K constraints.  
23d2e 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
23d2f 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 69  E_OMIT_CHECK.  i
23d30 66 28 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20  f( pTab->pCheck 
23d31 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e  && (pParse->db->
23d32 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49  flags & SQLITE_I
23d33 67 6e 6f 72 65 43 68 65 63 6b 73 29 3d 3d 30 20  gnoreChecks)==0 
23d34 29 7b 0a 20 20 20 20 69 6e 74 20 61 6c 6c 4f 6b  ){.    int allOk
23d35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
23d36 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
23d37 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 20 3d  pParse->ckBase =
23d38 20 72 65 67 44 61 74 61 3b 0a 20 20 20 20 73 71   regData;.    sq
23d39 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
23d3a 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 70 43  pParse, pTab->pC
23d3b 68 65 63 6b 2c 20 61 6c 6c 4f 6b 2c 20 53 51 4c  heck, allOk, SQL
23d3c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
23d3d 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f  .    onError = o
23d3e 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45  verrideError!=OE
23d3f 5f 44 65 66 61 75 6c 74 20 3f 20 6f 76 65 72 72  _Default ? overr
23d40 69 64 65 45 72 72 6f 72 20 3a 20 4f 45 5f 41 62  ideError : OE_Ab
23d41 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45  ort;.    if( onE
23d42 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20  rror==OE_Ignore 
23d43 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
23d44 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
23d45 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65  _Goto, 0, ignore
23d46 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Dest);.    }else
23d47 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
23d48 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
23d49 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e  Halt, SQLITE_CON
23d4a 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72  STRAINT, onError
23d4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
23d4c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
23d4d 61 62 65 6c 28 76 2c 20 61 6c 6c 4f 6b 29 3b 0a  abel(v, allOk);.
23d4e 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64    }.#endif /* !d
23d4f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
23d50 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20  IT_CHECK) */..  
23d51 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61 6e  /* If we have an
23d52 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
23d53 20 4b 45 59 2c 20 6d 61 6b 65 20 73 75 72 65 20   KEY, make sure 
23d54 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 0a  the primary key.
23d55 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20    ** of the new 
23d56 72 65 63 6f 72 64 20 64 6f 65 73 20 6e 6f 74 20  record does not 
23d57 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74  previously exist
23d58 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 74 68  .  Except, if th
23d59 69 73 0a 20 20 2a 2a 20 69 73 20 61 6e 20 55 50  is.  ** is an UP
23d5a 44 41 54 45 20 61 6e 64 20 74 68 65 20 70 72 69  DATE and the pri
23d5b 6d 61 72 79 20 6b 65 79 20 69 73 20 6e 6f 74 20  mary key is not 
23d5c 63 68 61 6e 67 69 6e 67 2c 20 74 68 61 74 20 69  changing, that i
23d5d 73 20 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  s OK..  */.  if(
23d5e 20 72 6f 77 69 64 43 68 6e 67 20 29 7b 0a 20 20   rowidChng ){.  
23d5f 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54 61 62    onError = pTab
23d60 2d 3e 6b 65 79 43 6f 6e 66 3b 0a 20 20 20 20 69  ->keyConf;.    i
23d61 66 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72  f( overrideError
23d62 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a  !=OE_Default ){.
23d63 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20        onError = 
23d64 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20  overrideError;. 
23d65 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45     }else if( onE
23d66 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
23d67 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f   ){.      onErro
23d68 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  r = OE_Abort;.  
23d69 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 69 66 28    }.    .    if(
23d6a 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70   onError!=OE_Rep
23d6b 6c 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70 49  lace || pTab->pI
23d6c 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 69 66  ndex ){.      if
23d6d 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20  ( isUpdate ){.  
23d6e 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74        j2 = sqlit
23d6f 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
23d70 4f 50 5f 45 71 2c 20 72 65 67 52 6f 77 69 64 2c  OP_Eq, regRowid,
23d71 20 30 2c 20 72 65 67 52 6f 77 69 64 2d 31 29 3b   0, regRowid-1);
23d72 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a  .      }.      j
23d73 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  3 = sqlite3VdbeA
23d74 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
23d75 78 69 73 74 73 2c 20 62 61 73 65 43 75 72 2c 20  xists, baseCur, 
23d76 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  0, regRowid);.  
23d77 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72      switch( onEr
23d78 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ror ){.        d
23d79 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
23d7a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45      onError = OE
23d7b 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20  _Abort;.        
23d7c 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69    /* Fall thru i
23d7d 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
23d7e 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20  e */.        }. 
23d7f 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52         case OE_R
23d80 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 20  ollback:.       
23d81 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a   case OE_Abort:.
23d82 20 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f          case OE_
23d83 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20  Fail: {.        
23d84 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23d85 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op4(v, OP_Halt, 
23d86 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
23d87 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 0a 20  T, onError, 0,. 
23d88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23d89 20 20 20 20 20 20 20 20 20 20 22 50 52 49 4d 41            "PRIMA
23d8a 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75  RY KEY must be u
23d8b 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49  nique", P4_STATI
23d8c 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  C);.          br
23d8d 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
23d8e 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52         case OE_R
23d8f 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20 20  eplace: {.      
23d90 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
23d91 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74  ateRowIndexDelet
23d92 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
23d93 62 61 73 65 43 75 72 2c 20 30 29 3b 0a 20 20 20  baseCur, 0);.   
23d94 20 20 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61         seenRepla
23d95 63 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ce = 1;.        
23d96 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
23d97 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20   }.        case 
23d98 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20  OE_Ignore: {.   
23d99 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
23d9a 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b  eenReplace==0 );
23d9b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
23d9c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23d9d 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f  OP_Goto, 0, igno
23d9e 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20  reDest);.       
23d9f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
23da0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
23da1 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
23da2 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20  pHere(v, j3);.  
23da3 20 20 20 20 69 66 28 20 69 73 55 70 64 61 74 65      if( isUpdate
23da4 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
23da5 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
23da6 76 2c 20 6a 32 29 3b 0a 20 20 20 20 20 20 7d 0a  v, j2);.      }.
23da7 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
23da8 54 65 73 74 20 61 6c 6c 20 55 4e 49 51 55 45 20  Test all UNIQUE 
23da9 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 79 20 63  constraints by c
23daa 72 65 61 74 69 6e 67 20 65 6e 74 72 69 65 73 20  reating entries 
23dab 66 6f 72 20 65 61 63 68 20 55 4e 49 51 55 45 0a  for each UNIQUE.
23dac 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 6d    ** index and m
23dad 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 20  aking sure that 
23dae 64 75 70 6c 69 63 61 74 65 20 65 6e 74 72 69 65  duplicate entrie
23daf 73 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79  s do not already
23db0 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 41 64 64   exist..  ** Add
23db1 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 73   the new records
23db2 20 74 6f 20 74 68 65 20 69 6e 64 69 63 65 73 20   to the indices 
23db3 61 73 20 77 65 20 67 6f 2e 0a 20 20 2a 2f 0a 20  as we go..  */. 
23db4 20 66 6f 72 28 69 43 75 72 3d 30 2c 20 70 49 64   for(iCur=0, pId
23db5 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
23db6 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
23db7 3e 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b  >pNext, iCur++){
23db8 0a 20 20 20 20 69 6e 74 20 72 65 67 49 64 78 3b  .    int regIdx;
23db9 0a 20 20 20 20 69 6e 74 20 72 65 67 52 3b 0a 0a  .    int regR;..
23dba 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78 5b      if( aRegIdx[
23dbb 69 43 75 72 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  iCur]==0 ) conti
23dbc 6e 75 65 3b 20 20 2f 2a 20 53 6b 69 70 20 75 6e  nue;  /* Skip un
23dbd 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a  used indices */.
23dbe 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61  .    /* Create a
23dbf 20 6b 65 79 20 66 6f 72 20 61 63 63 65 73 73 69   key for accessi
23dc0 6e 67 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74  ng the index ent
23dc1 72 79 20 2a 2f 0a 20 20 20 20 72 65 67 49 64 78  ry */.    regIdx
23dc2 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
23dc3 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70  pRange(pParse, p
23dc4 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  Idx->nColumn+1);
23dc5 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
23dc6 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
23dc7 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ++){.      int i
23dc8 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c  dx = pIdx->aiCol
23dc9 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66  umn[i];.      if
23dca 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b  ( idx==pTab->iPK
23dcb 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ey ){.        sq
23dcc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
23dcd 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67  v, OP_SCopy, reg
23dce 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69 29  Rowid, regIdx+i)
23dcf 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
23dd0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23dd1 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
23dd2 43 6f 70 79 2c 20 72 65 67 44 61 74 61 2b 69 64  Copy, regData+id
23dd3 78 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20  x, regIdx+i);.  
23dd4 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
23dd5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23dd6 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
23dd7 65 67 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b  egRowid, regIdx+
23dd8 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  i);.    sqlite3V
23dd9 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
23dda 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 49  MakeRecord, regI
23ddb 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  dx, pIdx->nColum
23ddc 6e 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 43 75  n+1, aRegIdx[iCu
23ddd 72 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  r]);.    sqlite3
23dde 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72  IndexAffinityStr
23ddf 28 76 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 73  (v, pIdx);.    s
23de0 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
23de1 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
23de2 61 72 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49  arse, regIdx, pI
23de3 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a  dx->nColumn+1);.
23de4 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
23de5 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
23de6 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78  se, regIdx, pIdx
23de7 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20  ->nColumn+1);.. 
23de8 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77     /* Find out w
23de9 68 61 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61  hat action to ta
23dea 6b 65 20 69 6e 20 63 61 73 65 20 74 68 65 72 65  ke in case there
23deb 20 69 73 20 61 6e 20 69 6e 64 65 78 69 6e 67 20   is an indexing 
23dec 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 20 20  conflict */.    
23ded 6f 6e 45 72 72 6f 72 20 3d 20 70 49 64 78 2d 3e  onError = pIdx->
23dee 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 69 66 28  onError;.    if(
23def 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e   onError==OE_Non
23df0 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f  e ) continue;  /
23df1 2a 20 70 49 64 78 20 69 73 20 6e 6f 74 20 61 20  * pIdx is not a 
23df2 55 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a  UNIQUE index */.
23df3 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64 65      if( override
23df4 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c  Error!=OE_Defaul
23df5 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72  t ){.      onErr
23df6 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72  or = overrideErr
23df7 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  or;.    }else if
23df8 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  ( onError==OE_De
23df9 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f  fault ){.      o
23dfa 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72  nError = OE_Abor
23dfb 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
23dfc 20 73 65 65 6e 52 65 70 6c 61 63 65 20 29 7b 0a   seenReplace ){.
23dfd 20 20 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f        if( onErro
23dfe 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 20 6f  r==OE_Ignore ) o
23dff 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c  nError = OE_Repl
23e00 61 63 65 3b 0a 20 20 20 20 20 20 65 6c 73 65 20  ace;.      else 
23e01 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
23e02 46 61 69 6c 20 29 20 6f 6e 45 72 72 6f 72 20 3d  Fail ) onError =
23e03 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d   OE_Abort;.    }
23e04 0a 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68  .    ..    /* Ch
23e05 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
23e06 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72  e new index entr
23e07 79 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65  y will be unique
23e08 20 2a 2f 0a 20 20 20 20 6a 32 20 3d 20 73 71 6c   */.    j2 = sql
23e09 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
23e0a 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67  , OP_IsNull, reg
23e0b 49 64 78 2c 20 30 2c 20 70 49 64 78 2d 3e 6e 43  Idx, 0, pIdx->nC
23e0c 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 72 65 67 52  olumn);.    regR
23e0d 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
23e0e 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
23e0f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23e10 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
23e11 20 72 65 67 52 6f 77 69 64 2d 68 61 73 54 77 6f   regRowid-hasTwo
23e12 52 6f 77 69 64 73 2c 20 72 65 67 52 29 3b 0a 20  Rowids, regR);. 
23e13 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56     j3 = sqlite3V
23e14 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
23e15 49 73 55 6e 69 71 75 65 2c 20 62 61 73 65 43 75  IsUnique, baseCu
23e16 72 2b 69 43 75 72 2b 31 2c 20 30 2c 0a 20 20 20  r+iCur+1, 0,.   
23e17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23e18 20 20 20 20 20 20 20 20 72 65 67 52 2c 20 53 51          regR, SQ
23e19 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
23e1a 61 52 65 67 49 64 78 5b 69 43 75 72 5d 29 2c 0a  aRegIdx[iCur]),.
23e1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23e1c 20 20 20 20 20 20 20 20 20 20 20 50 34 5f 49 4e             P4_IN
23e1d 54 33 32 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 65  T32);..    /* Ge
23e1e 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
23e1f 20 65 78 65 63 75 74 65 73 20 69 66 20 74 68 65   executes if the
23e20 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79   new index entry
23e21 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 20 2a   is not unique *
23e22 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e  /.    assert( on
23e23 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61  Error==OE_Rollba
23e24 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f  ck || onError==O
23e25 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72  E_Abort || onErr
23e26 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20  or==OE_Fail.    
23e27 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d      || onError==
23e28 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45  OE_Ignore || onE
23e29 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
23e2a 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20   );.    switch( 
23e2b 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20  onError ){.     
23e2c 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63   case OE_Rollbac
23e2d 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45  k:.      case OE
23e2e 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20 63 61  _Abort:.      ca
23e2f 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20  se OE_Fail: {.  
23e30 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 31 2c        int j, n1,
23e31 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 63 68 61   n2;.        cha
23e32 72 20 7a 45 72 72 4d 73 67 5b 32 30 30 5d 3b 0a  r zErrMsg[200];.
23e33 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
23e34 73 6e 70 72 69 6e 74 66 28 41 72 72 61 79 53 69  snprintf(ArraySi
23e35 7a 65 28 7a 45 72 72 4d 73 67 29 2c 20 7a 45 72  ze(zErrMsg), zEr
23e36 72 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20 20  rMsg,.          
23e37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
23e38 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f  Idx->nColumn>1 ?
23e39 20 22 63 6f 6c 75 6d 6e 73 20 22 20 3a 20 22 63   "columns " : "c
23e3a 6f 6c 75 6d 6e 20 22 29 3b 0a 20 20 20 20 20 20  olumn ");.      
23e3b 20 20 6e 31 20 3d 20 73 71 6c 69 74 65 33 53 74    n1 = sqlite3St
23e3c 72 6c 65 6e 33 30 28 7a 45 72 72 4d 73 67 29 3b  rlen30(zErrMsg);
23e3d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  .        for(j=0
23e3e 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; j<pIdx->nColum
23e3f 6e 20 26 26 20 6e 31 3c 41 72 72 61 79 53 69 7a  n && n1<ArraySiz
23e40 65 28 7a 45 72 72 4d 73 67 29 2d 33 30 3b 20 6a  e(zErrMsg)-30; j
23e41 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  ++){.          c
23e42 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 61 62  har *zCol = pTab
23e43 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43  ->aCol[pIdx->aiC
23e44 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b  olumn[j]].zName;
23e45 0a 20 20 20 20 20 20 20 20 20 20 6e 32 20 3d 20  .          n2 = 
23e46 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
23e47 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 20  zCol);.         
23e48 20 69 66 28 20 6a 3e 30 20 29 7b 0a 20 20 20 20   if( j>0 ){.    
23e49 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
23e4a 73 6e 70 72 69 6e 74 66 28 41 72 72 61 79 53 69  snprintf(ArraySi
23e4b 7a 65 28 7a 45 72 72 4d 73 67 29 2d 6e 31 2c 20  ze(zErrMsg)-n1, 
23e4c 26 7a 45 72 72 4d 73 67 5b 6e 31 5d 2c 20 22 2c  &zErrMsg[n1], ",
23e4d 20 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   ");.           
23e4e 20 6e 31 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20   n1 += 2;.      
23e4f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
23e50 69 66 28 20 6e 31 2b 6e 32 3e 41 72 72 61 79 53  if( n1+n2>ArrayS
23e51 69 7a 65 28 7a 45 72 72 4d 73 67 29 2d 33 30 20  ize(zErrMsg)-30 
23e52 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
23e53 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
23e54 41 72 72 61 79 53 69 7a 65 28 7a 45 72 72 4d 73  ArraySize(zErrMs
23e55 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b  g)-n1, &zErrMsg[
23e56 6e 31 5d 2c 20 22 2e 2e 2e 22 29 3b 0a 20 20 20  n1], "...");.   
23e57 20 20 20 20 20 20 20 20 20 6e 31 20 2b 3d 20 33           n1 += 3
23e58 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
23e59 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
23e5a 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
23e5b 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
23e5c 74 66 28 41 72 72 61 79 53 69 7a 65 28 7a 45 72  tf(ArraySize(zEr
23e5d 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d  rMsg)-n1, &zErrM
23e5e 73 67 5b 6e 31 5d 2c 20 22 25 73 22 2c 20 7a 43  sg[n1], "%s", zC
23e5f 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ol);.           
23e60 20 6e 31 20 2b 3d 20 6e 32 3b 0a 20 20 20 20 20   n1 += n2;.     
23e61 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
23e62 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
23e63 5f 73 6e 70 72 69 6e 74 66 28 41 72 72 61 79 53  _snprintf(ArrayS
23e64 69 7a 65 28 7a 45 72 72 4d 73 67 29 2d 6e 31 2c  ize(zErrMsg)-n1,
23e65 20 26 7a 45 72 72 4d 73 67 5b 6e 31 5d 2c 20 0a   &zErrMsg[n1], .
23e66 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64 78              pIdx
23e67 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 20  ->nColumn>1 ? " 
23e68 61 72 65 20 6e 6f 74 20 75 6e 69 71 75 65 22 20  are not unique" 
23e69 3a 20 22 20 69 73 20 6e 6f 74 20 75 6e 69 71 75  : " is not uniqu
23e6a 65 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  e");.        sql
23e6b 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
23e6c 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54  , OP_Halt, SQLIT
23e6d 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e  E_CONSTRAINT, on
23e6e 45 72 72 6f 72 2c 20 30 2c 20 7a 45 72 72 4d 73  Error, 0, zErrMs
23e6f 67 2c 30 29 3b 0a 20 20 20 20 20 20 20 20 62 72  g,0);.        br
23e70 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
23e71 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
23e72 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73  e: {.        ass
23e73 65 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63 65  ert( seenReplace
23e74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73  ==0 );.        s
23e75 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23e76 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
23e77 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20  ignoreDest);.   
23e78 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
23e79 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f    }.      case O
23e7a 45 5f 52 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20  E_Replace: {.   
23e7b 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65       sqlite3Gene
23e7c 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 70 50  rateRowDelete(pP
23e7d 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65  arse, pTab, base
23e7e 43 75 72 2c 20 72 65 67 52 2c 20 30 29 3b 0a 20  Cur, regR, 0);. 
23e7f 20 20 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61         seenRepla
23e80 63 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ce = 1;.        
23e81 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
23e82 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
23e83 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
23e84 6a 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j2);.    sqlite3
23e85 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
23e86 6a 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j3);.    sqlite3
23e87 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
23e88 50 61 72 73 65 2c 20 72 65 67 52 29 3b 0a 20 20  Parse, regR);.  
23e89 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
23e8a 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65  routine generate
23e8b 73 20 63 6f 64 65 20 74 6f 20 66 69 6e 69 73 68  s code to finish
23e8c 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55   the INSERT or U
23e8d 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 0a  PDATE operation.
23e8e 2a 2a 20 74 68 61 74 20 77 61 73 20 73 74 61 72  ** that was star
23e8f 74 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63  ted by a prior c
23e90 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 47 65  all to sqlite3Ge
23e91 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  nerateConstraint
23e92 43 68 65 63 6b 73 2e 0a 2a 2a 20 41 20 63 6f 6e  Checks..** A con
23e93 73 65 63 75 74 69 76 65 20 72 61 6e 67 65 20 6f  secutive range o
23e94 66 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  f registers star
23e95 74 69 6e 67 20 61 74 20 72 65 67 52 6f 77 69 64  ting at regRowid
23e96 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a   contains the.**
23e97 20 72 6f 77 69 64 20 61 6e 64 20 74 68 65 20 63   rowid and the c
23e98 6f 6e 74 65 6e 74 20 74 6f 20 62 65 20 69 6e 73  ontent to be ins
23e99 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  erted..**.** The
23e9a 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
23e9b 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  is routine shoul
23e9c 64 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73  d be the same as
23e9d 20 74 68 65 20 66 69 72 73 74 20 73 69 78 0a 2a   the first six.*
23e9e 2a 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 73  * arguments to s
23e9f 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f  qlite3GenerateCo
23ea0 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 2e 0a  nstraintChecks..
23ea1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23ea2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
23ea3 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28  mpleteInsertion(
23ea4 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
23ea5 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61  ,      /* The pa
23ea6 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
23ea7 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
23ea8 20 20 20 20 20 20 2f 2a 20 74 68 65 20 74 61 62        /* the tab
23ea9 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65  le into which we
23eaa 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a   are inserting *
23eab 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c  /.  int baseCur,
23eac 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
23ead 20 6f 66 20 61 20 72 65 61 64 2f 77 72 69 74 65   of a read/write
23eae 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   cursor pointing
23eaf 20 61 74 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e   at pTab */.  in
23eb0 74 20 72 65 67 52 6f 77 69 64 2c 20 20 20 20 20  t regRowid,     
23eb1 20 20 2f 2a 20 52 61 6e 67 65 20 6f 66 20 63 6f    /* Range of co
23eb2 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a  ntent */.  int *
23eb3 61 52 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f  aRegIdx,       /
23eb4 2a 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20  * Register used 
23eb5 62 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20  by each index.  
23eb6 30 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64  0 for unused ind
23eb7 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ices */.  int is
23eb8 55 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a  Update,       /*
23eb9 20 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45   True for UPDATE
23eba 2c 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45  , False for INSE
23ebb 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49  RT */.  int newI
23ebc 64 78 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49  dx,         /* I
23ebd 6e 64 65 78 20 6f 66 20 4e 45 57 20 74 61 62 6c  ndex of NEW tabl
23ebe 65 20 66 6f 72 20 74 72 69 67 67 65 72 73 2e 20  e for triggers. 
23ebf 20 2d 31 20 69 66 20 6e 6f 6e 65 20 2a 2f 0a 20   -1 if none */. 
23ec0 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 20   int appendBias 
23ec1 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
23ec2 74 68 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 74  this is likely t
23ec3 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a  o be an append *
23ec4 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
23ec5 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 6e  Vdbe *v;.  int n
23ec6 49 64 78 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49  Idx;.  Index *pI
23ec7 64 78 3b 0a 20 20 75 38 20 70 69 6b 5f 66 6c 61  dx;.  u8 pik_fla
23ec8 67 73 3b 0a 20 20 69 6e 74 20 72 65 67 44 61 74  gs;.  int regDat
23ec9 61 3b 0a 20 20 69 6e 74 20 72 65 67 52 65 63 3b  a;.  int regRec;
23eca 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
23ecb 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
23ecc 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
23ecd 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
23ece 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20  ->pSelect==0 ); 
23ecf 20 2f 2a 20 54 68 69 73 20 74 61 62 6c 65 20 69   /* This table i
23ed0 73 20 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f 0a  s not a VIEW */.
23ed1 20 20 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49    for(nIdx=0, pI
23ed2 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
23ed3 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
23ed4 2d 3e 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29  ->pNext, nIdx++)
23ed5 7b 7d 0a 20 20 66 6f 72 28 69 3d 6e 49 64 78 2d  {}.  for(i=nIdx-
23ed6 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20  1; i>=0; i--){. 
23ed7 20 20 20 69 66 28 20 61 52 65 67 49 64 78 5b 69     if( aRegIdx[i
23ed8 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ]==0 ) continue;
23ed9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23eda 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
23edb 49 6e 73 65 72 74 2c 20 62 61 73 65 43 75 72 2b  Insert, baseCur+
23edc 69 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 5d 29  i+1, aRegIdx[i])
23edd 3b 0a 20 20 7d 0a 20 20 72 65 67 44 61 74 61 20  ;.  }.  regData 
23ede 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 31 3b 0a  = regRowid + 1;.
23edf 20 20 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74    regRec = sqlit
23ee0 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
23ee1 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  rse);.  sqlite3V
23ee2 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
23ee3 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 44  MakeRecord, regD
23ee4 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c  ata, pTab->nCol,
23ee5 20 72 65 67 52 65 63 29 3b 0a 20 20 73 71 6c 69   regRec);.  sqli
23ee6 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79  te3TableAffinity
23ee7 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20 20  Str(v, pTab);.  
23ee8 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
23ee9 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
23eea 50 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c 20  Parse, regData, 
23eeb 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 23 69 66  pTab->nCol);.#if
23eec 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23eed 5f 54 52 49 47 47 45 52 0a 20 20 69 66 28 20 6e  _TRIGGER.  if( n
23eee 65 77 49 64 78 3e 3d 30 20 29 7b 0a 20 20 20 20  ewIdx>=0 ){.    
23eef 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23ef0 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
23ef1 6e 65 77 49 64 78 2c 20 72 65 67 52 65 63 2c 20  newIdx, regRec, 
23ef2 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 23  regRowid);.  }.#
23ef3 65 6e 64 69 66 0a 20 20 69 66 28 20 70 50 61 72  endif.  if( pPar
23ef4 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20  se->nested ){.  
23ef5 20 20 70 69 6b 5f 66 6c 61 67 73 20 3d 20 30 3b    pik_flags = 0;
23ef6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 69  .  }else{.    pi
23ef7 6b 5f 66 6c 61 67 73 20 3d 20 4f 50 46 4c 41 47  k_flags = OPFLAG
23ef8 5f 4e 43 48 41 4e 47 45 3b 0a 20 20 20 20 70 69  _NCHANGE;.    pi
23ef9 6b 5f 66 6c 61 67 73 20 7c 3d 20 28 69 73 55 70  k_flags |= (isUp
23efa 64 61 74 65 3f 4f 50 46 4c 41 47 5f 49 53 55 50  date?OPFLAG_ISUP
23efb 44 41 54 45 3a 4f 50 46 4c 41 47 5f 4c 41 53 54  DATE:OPFLAG_LAST
23efc 52 4f 57 49 44 29 3b 0a 20 20 7d 0a 20 20 69 66  ROWID);.  }.  if
23efd 28 20 61 70 70 65 6e 64 42 69 61 73 20 29 7b 0a  ( appendBias ){.
23efe 20 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d      pik_flags |=
23eff 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 3b 0a   OPFLAG_APPEND;.
23f00 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
23f01 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
23f02 73 65 72 74 2c 20 62 61 73 65 43 75 72 2c 20 72  sert, baseCur, r
23f03 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64 29  egRec, regRowid)
23f04 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d  ;.  if( !pParse-
23f05 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 73  >nested ){.    s
23f06 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
23f07 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e  P4(v, -1, pTab->
23f08 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43  zName, P4_STATIC
23f09 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
23f0a 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
23f0b 70 69 6b 5f 66 6c 61 67 73 29 3b 0a 7d 0a 0a 2f  pik_flags);.}../
23f0c 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
23f0d 64 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65  de that will ope
23f0e 6e 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61 20  n cursors for a 
23f0f 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 61 6c  table and for al
23f10 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66 20  l.** indices of 
23f11 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54 68 65  that table.  The
23f12 20 22 62 61 73 65 43 75 72 22 20 70 61 72 61 6d   "baseCur" param
23f13 65 74 65 72 20 69 73 20 74 68 65 20 63 75 72 73  eter is the curs
23f14 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64 0a 2a  or number used.*
23f15 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e  * for the table.
23f16 20 20 49 6e 64 69 63 65 73 20 61 72 65 20 6f 70    Indices are op
23f17 65 6e 65 64 20 6f 6e 20 73 75 62 73 65 71 75 65  ened on subseque
23f18 6e 74 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a  nt cursors..**.*
23f19 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
23f1a 62 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20 6f  ber of indices o
23f1b 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  n the table..*/.
23f1c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
23f1d 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  nt sqlite3OpenTa
23f1e 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 0a 20  bleAndIndices(. 
23f1f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
23f20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
23f21 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
23f22 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 54 61  *pTab,     /* Ta
23f23 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64  ble to be opened
23f24 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75   */.  int baseCu
23f25 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  r,     /* Cursor
23f26 20 6e 75 6d 62 65 72 20 61 73 73 69 67 6e 65 64   number assigned
23f27 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   to the table */
23f28 0a 20 20 69 6e 74 20 6f 70 20 20 20 20 20 20 20  .  int op       
23f29 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65      /* OP_OpenRe
23f2a 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72 69  ad or OP_OpenWri
23f2b 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  te */.){.  int i
23f2c 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 49  ;.  int iDb;.  I
23f2d 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 56 64  ndex *pIdx;.  Vd
23f2e 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 49 73  be *v;..  if( Is
23f2f 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20  Virtual(pTab) ) 
23f30 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 44 62 20  return 0;.  iDb 
23f31 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
23f32 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
23f33 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
23f34 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  );.  v = sqlite3
23f35 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
23f36 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
23f37 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e  );.  sqlite3Open
23f38 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 62 61  Table(pParse, ba
23f39 73 65 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62  seCur, iDb, pTab
23f3a 2c 20 6f 70 29 3b 0a 20 20 66 6f 72 28 69 3d 31  , op);.  for(i=1
23f3b 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
23f3c 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
23f3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b  pIdx->pNext, i++
23f3e 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  ){.    KeyInfo *
23f3f 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e  pKey = sqlite3In
23f40 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
23f41 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 61 73  e, pIdx);.    as
23f42 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63 68  sert( pIdx->pSch
23f43 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65  ema==pTab->pSche
23f44 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ma );.    sqlite
23f45 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 6f  3VdbeAddOp4(v, o
23f46 70 2c 20 69 2b 62 61 73 65 43 75 72 2c 20 70 49  p, i+baseCur, pI
23f47 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20  dx->tnum, iDb,. 
23f48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23f49 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
23f4a 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
23f4b 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43  DOFF);.    VdbeC
23f4c 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
23f4d 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pIdx->zName));.
23f4e 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65    }.  if( pParse
23f4f 2d 3e 6e 54 61 62 3c 3d 62 61 73 65 43 75 72 2b  ->nTab<=baseCur+
23f50 69 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  i ){.    pParse-
23f51 3e 6e 54 61 62 20 3d 20 62 61 73 65 43 75 72 2b  >nTab = baseCur+
23f52 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  i;.  }.  return 
23f53 69 2d 31 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20  i-1;.}...#ifdef 
23f54 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a  SQLITE_TEST./*.*
23f55 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
23f56 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
23f57 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  is incremented w
23f58 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 74  henever the.** t
23f59 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61  ransfer optimiza
23f5a 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 20 20 54  tion is used.  T
23f5b 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
23f5c 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f  testing.** purpo
23f5d 73 65 73 20 6f 6e 6c 79 20 2d 20 74 6f 20 6d 61  ses only - to ma
23f5e 6b 65 20 73 75 72 65 20 74 68 65 20 74 72 61 6e  ke sure the tran
23f5f 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
23f60 6e 20 72 65 61 6c 6c 79 0a 2a 2a 20 69 73 20 68  n really.** is h
23f61 61 70 70 65 6e 69 6e 67 20 77 68 65 6e 20 69 74  appening when it
23f62 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f 2e 0a   is suppose to..
23f63 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
23f64 74 20 73 71 6c 69 74 65 33 5f 78 66 65 72 6f 70  t sqlite3_xferop
23f65 74 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 20  t_count;.#endif 
23f66 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a  /* SQLITE_TEST *
23f67 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  /...#ifndef SQLI
23f68 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54  TE_OMIT_XFER_OPT
23f69 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
23f6a 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65 73 20  collation names 
23f6b 74 6f 20 73 65 65 20 69 66 20 74 68 65 79 20 61  to see if they a
23f6c 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a  re compatible..*
23f6d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66 65  /.static int xfe
23f6e 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61  rCompatibleColla
23f6f 74 69 6f 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  tion(const char 
23f70 2a 7a 31 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *z1, const char 
23f71 2a 7a 32 29 7b 0a 20 20 69 66 28 20 7a 31 3d 3d  *z2){.  if( z1==
23f72 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
23f73 7a 32 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28  z2==0;.  }.  if(
23f74 20 7a 32 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65   z2==0 ){.    re
23f75 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65  turn 0;.  }.  re
23f76 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 49  turn sqlite3StrI
23f77 43 6d 70 28 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0a  Cmp(z1, z2)==0;.
23f78 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  }.../*.** Check 
23f79 74 6f 20 73 65 65 20 69 66 20 69 6e 64 65 78 20  to see if index 
23f7a 70 53 72 63 20 69 73 20 63 6f 6d 70 61 74 69 62  pSrc is compatib
23f7b 6c 65 20 61 73 20 61 20 73 6f 75 72 63 65 20 6f  le as a source o
23f7c 66 20 64 61 74 61 0a 2a 2a 20 66 6f 72 20 69 6e  f data.** for in
23f7d 64 65 78 20 70 44 65 73 74 20 69 6e 20 61 6e 20  dex pDest in an 
23f7e 69 6e 73 65 72 74 20 74 72 61 6e 73 66 65 72 20  insert transfer 
23f7f 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54  optimization.  T
23f80 68 65 20 72 75 6c 65 73 0a 2a 2a 20 66 6f 72 20  he rules.** for 
23f81 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 69 6e 64  a compatible ind
23f82 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  ex:.**.**    *  
23f83 20 54 68 65 20 69 6e 64 65 78 20 69 73 20 6f 76   The index is ov
23f84 65 72 20 74 68 65 20 73 61 6d 65 20 73 65 74 20  er the same set 
23f85 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20  of columns.**   
23f86 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 44 45   *   The same DE
23f87 53 43 20 61 6e 64 20 41 53 43 20 6d 61 72 6b 69  SC and ASC marki
23f88 6e 67 73 20 6f 63 63 75 72 73 20 6f 6e 20 61 6c  ngs occurs on al
23f89 6c 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20  l columns.**    
23f8a 2a 20 20 20 54 68 65 20 73 61 6d 65 20 6f 6e 45  *   The same onE
23f8b 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20  rror processing 
23f8c 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67  (OE_Abort, OE_Ig
23f8d 6e 6f 72 65 2c 20 65 74 63 29 0a 2a 2a 20 20 20  nore, etc).**   
23f8e 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 63 6f   *   The same co
23f8f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
23f90 20 6f 6e 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a   on each column.
23f91 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66  */.static int xf
23f92 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65  erCompatibleInde
23f93 78 28 49 6e 64 65 78 20 2a 70 44 65 73 74 2c 20  x(Index *pDest, 
23f94 49 6e 64 65 78 20 2a 70 53 72 63 29 7b 0a 20 20  Index *pSrc){.  
23f95 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28  int i;.  assert(
23f96 20 70 44 65 73 74 20 26 26 20 70 53 72 63 20 29   pDest && pSrc )
23f97 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 65 73  ;.  assert( pDes
23f98 74 2d 3e 70 54 61 62 6c 65 21 3d 70 53 72 63 2d  t->pTable!=pSrc-
23f99 3e 70 54 61 62 6c 65 20 29 3b 0a 20 20 69 66 28  >pTable );.  if(
23f9a 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 21   pDest->nColumn!
23f9b 3d 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  =pSrc->nColumn )
23f9c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
23f9d 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 6e    /* Different n
23f9e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
23f9f 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   */.  }.  if( pD
23fa0 65 73 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 53  est->onError!=pS
23fa1 72 63 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20  rc->onError ){. 
23fa2 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
23fa3 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6e 66  * Different conf
23fa4 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
23fa5 73 74 72 61 74 65 67 69 65 73 20 2a 2f 0a 20 20  strategies */.  
23fa6 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  }.  for(i=0; i<p
23fa7 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Src->nColumn; i+
23fa8 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 53 72 63  +){.    if( pSrc
23fa9 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 21 3d 70  ->aiColumn[i]!=p
23faa 44 65 73 74 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  Dest->aiColumn[i
23fab 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ] ){.      retur
23fac 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72  n 0;   /* Differ
23fad 65 6e 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 64 65  ent columns inde
23fae 78 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  xed */.    }.   
23faf 20 69 66 28 20 70 53 72 63 2d 3e 61 53 6f 72 74   if( pSrc->aSort
23fb0 4f 72 64 65 72 5b 69 5d 21 3d 70 44 65 73 74 2d  Order[i]!=pDest-
23fb1 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29  >aSortOrder[i] )
23fb2 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
23fb3 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
23fb4 20 73 6f 72 74 20 6f 72 64 65 72 73 20 2a 2f 0a   sort orders */.
23fb5 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
23fb6 72 63 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 21 3d 70  rc->azColl[i]!=p
23fb7 44 65 73 74 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 20  Dest->azColl[i] 
23fb8 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
23fb9 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e  0;   /* Differen
23fba 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  t collating sequ
23fbb 65 6e 63 65 73 20 2a 2f 0a 20 20 20 20 7d 0a 20  ences */.    }. 
23fbc 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 74   }..  /* If no t
23fbd 65 73 74 20 61 62 6f 76 65 20 66 61 69 6c 73 20  est above fails 
23fbe 74 68 65 6e 20 74 68 65 20 69 6e 64 69 63 65 73  then the indices
23fbf 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 61 74 69   must be compati
23fc0 62 6c 65 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ble */.  return 
23fc1 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  1;.}../*.** Atte
23fc2 6d 70 74 20 74 68 65 20 74 72 61 6e 73 66 65 72  mpt the transfer
23fc3 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e   optimization on
23fc4 20 49 4e 53 45 52 54 73 20 6f 66 20 74 68 65 20   INSERTs of the 
23fc5 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49  form.**.**     I
23fc6 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 31 20  NSERT INTO tab1 
23fc7 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 61  SELECT * FROM ta
23fc8 62 32 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  b2;.**.** This o
23fc9 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6f  ptimization is o
23fca 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66  nly attempted if
23fcb 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 74  .**.**    (1)  t
23fcc 61 62 31 20 61 6e 64 20 74 61 62 32 20 68 61 76  ab1 and tab2 hav
23fcd 65 20 69 64 65 6e 74 69 63 61 6c 20 73 63 68 65  e identical sche
23fce 6d 61 73 20 69 6e 63 6c 75 64 69 6e 67 20 61 6c  mas including al
23fcf 6c 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  l the.**        
23fd0 20 73 61 6d 65 20 69 6e 64 69 63 65 73 20 61 6e   same indices an
23fd1 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 2a 2a  d constraints.**
23fd2 0a 2a 2a 20 20 20 20 28 32 29 20 20 74 61 62 31  .**    (2)  tab1
23fd3 20 61 6e 64 20 74 61 62 32 20 61 72 65 20 64 69   and tab2 are di
23fd4 66 66 65 72 65 6e 74 20 74 61 62 6c 65 73 0a 2a  fferent tables.*
23fd5 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20 54 68 65  *.**    (3)  The
23fd6 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 74 72  re must be no tr
23fd7 69 67 67 65 72 73 20 6f 6e 20 74 61 62 31 0a 2a  iggers on tab1.*
23fd8 2a 0a 2a 2a 20 20 20 20 28 34 29 20 20 54 68 65  *.**    (4)  The
23fd9 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
23fda 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
23fdb 65 6e 74 20 69 73 20 22 2a 22 0a 2a 2a 0a 2a 2a  ent is "*".**.**
23fdc 20 20 20 20 28 35 29 20 20 54 68 65 20 53 45 4c      (5)  The SEL
23fdd 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 68 61  ECT statement ha
23fde 73 20 6e 6f 20 57 48 45 52 45 2c 20 48 41 56 49  s no WHERE, HAVI
23fdf 4e 47 2c 20 4f 52 44 45 52 20 42 59 2c 20 47 52  NG, ORDER BY, GR
23fe0 4f 55 50 20 42 59 2c 0a 2a 2a 20 20 20 20 20 20  OUP BY,.**      
23fe1 20 20 20 6f 72 20 4c 49 4d 49 54 20 63 6c 61 75     or LIMIT clau
23fe2 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 36 29  se..**.**    (6)
23fe3 20 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61    The SELECT sta
23fe4 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69 6d 70  tement is a simp
23fe5 6c 65 20 28 6e 6f 74 20 61 20 63 6f 6d 70 6f 75  le (not a compou
23fe6 6e 64 29 20 73 65 6c 65 63 74 20 74 68 61 74 0a  nd) select that.
23fe7 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e 74 61  **         conta
23fe8 69 6e 73 20 6f 6e 6c 79 20 74 61 62 32 20 69 6e  ins only tab2 in
23fe9 20 69 74 73 20 46 52 4f 4d 20 63 6c 61 75 73 65   its FROM clause
23fea 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 74 68  .**.** This meth
23feb 6f 64 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74  od for implement
23fec 69 6e 67 20 74 68 65 20 49 4e 53 45 52 54 20 74  ing the INSERT t
23fed 72 61 6e 73 66 65 72 73 20 72 61 77 20 72 65 63  ransfers raw rec
23fee 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74 61 62  ords from.** tab
23fef 32 20 6f 76 65 72 20 74 6f 20 74 61 62 31 2e 20  2 over to tab1. 
23ff0 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 61 72 65   The columns are
23ff1 20 6e 6f 74 20 64 65 63 6f 64 65 64 2e 20 20 52   not decoded.  R
23ff2 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 0a  aw records from.
23ff3 2a 2a 20 74 68 65 20 69 6e 64 69 63 65 73 20 6f  ** the indices o
23ff4 66 20 74 61 62 32 20 61 72 65 20 74 72 61 6e 73  f tab2 are trans
23ff5 66 65 72 65 64 20 74 6f 20 74 61 62 31 20 61 73  fered to tab1 as
23ff6 20 77 65 6c 6c 2e 20 20 49 6e 20 73 6f 20 64 6f   well.  In so do
23ff7 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75  ing,.** the resu
23ff8 6c 74 69 6e 67 20 74 61 62 31 20 68 61 73 20 6d  lting tab1 has m
23ff9 75 63 68 20 6c 65 73 73 20 66 72 61 67 6d 65 6e  uch less fragmen
23ffa 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  tation..**.** Th
23ffb 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
23ffc 6e 73 20 54 52 55 45 20 69 66 20 74 68 65 20 6f  ns TRUE if the o
23ffd 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 61  ptimization is a
23ffe 74 74 65 6d 70 74 65 64 2e 20 20 49 66 20 61 6e  ttempted.  If an
23fff 79 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f 6e 64  y.** of the cond
24000 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 66 61 69  itions above fai
24001 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 70  l so that the op
24002 74 69 6d 69 7a 61 74 69 6f 6e 20 73 68 6f 75 6c  timization shoul
24003 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 61 74 74 65  d not.** be atte
24004 6d 70 74 65 64 2c 20 74 68 65 6e 20 74 68 69 73  mpted, then this
24005 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
24006 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61 74 69   FALSE..*/.stati
24007 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69 6d 69  c int xferOptimi
24008 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20  zation(.  Parse 
24009 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2400a 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
2400b 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44  t */.  Table *pD
2400c 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  est,         /* 
2400d 54 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65  The table we are
2400e 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 20   inserting into 
2400f 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
24010 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20  lect,      /* A 
24011 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
24012 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64   to use as the d
24013 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20  ata source */.  
24014 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20  int onError,    
24015 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20        /* How to 
24016 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e  handle constrain
24017 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e  t errors */.  in
24018 74 20 69 44 62 44 65 73 74 20 20 20 20 20 20 20  t iDbDest       
24019 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2401a 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a  ase of pDest */.
2401b 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  ){.  ExprList *p
2401c 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  EList;          
2401d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73        /* The res
2401e 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53  ult set of the S
2401f 45 4c 45 43 54 20 2a 2f 0a 20 20 54 61 62 6c 65  ELECT */.  Table
24020 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20 20   *pSrc;         
24021 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
24022 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  he table in the 
24023 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 53  FROM clause of S
24024 45 4c 45 43 54 20 2a 2f 0a 20 20 49 6e 64 65 78  ELECT */.  Index
24025 20 2a 70 53 72 63 49 64 78 2c 20 2a 70 44 65 73   *pSrcIdx, *pDes
24026 74 49 64 78 3b 20 20 20 20 20 20 20 2f 2a 20 53  tIdx;       /* S
24027 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
24028 61 74 69 6f 6e 20 69 6e 64 69 63 65 73 20 2a 2f  ation indices */
24029 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2402a 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20  t_item *pItem;  
2402b 20 20 20 20 2f 2a 20 41 6e 20 65 6c 65 6d 65 6e      /* An elemen
2402c 74 20 6f 66 20 70 53 65 6c 65 63 74 2d 3e 70 53  t of pSelect->pS
2402d 72 63 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  rc */.  int i;  
2402e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2402f 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
24030 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
24031 74 20 69 44 62 53 72 63 3b 20 20 20 20 20 20 20  t iDbSrc;       
24032 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24033 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f  * The database o
24034 66 20 70 53 72 63 20 2a 2f 0a 20 20 69 6e 74 20  f pSrc */.  int 
24035 69 53 72 63 2c 20 69 44 65 73 74 3b 20 20 20 20  iSrc, iDest;    
24036 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24037 43 75 72 73 6f 72 73 20 66 72 6f 6d 20 73 6f 75  Cursors from sou
24038 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74  rce and destinat
24039 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ion */.  int add
2403a 72 31 2c 20 61 64 64 72 32 3b 20 20 20 20 20 20  r1, addr2;      
2403b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2403c 70 20 61 64 64 72 65 73 73 65 73 20 2a 2f 0a 20  p addresses */. 
2403d 20 69 6e 74 20 65 6d 70 74 79 44 65 73 74 54 65   int emptyDestTe
2403e 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
2403f 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
24040 74 65 73 74 20 66 6f 72 20 65 6d 70 74 79 20 70  test for empty p
24041 44 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20 65 6d  Dest */.  int em
24042 70 74 79 53 72 63 54 65 73 74 3b 20 20 20 20 20  ptySrcTest;     
24043 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
24044 64 72 65 73 73 20 6f 66 20 74 65 73 74 20 66 6f  dress of test fo
24045 72 20 65 6d 70 74 79 20 70 53 72 63 20 2a 2f 0a  r empty pSrc */.
24046 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
24047 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24048 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 77     /* The VDBE w
24049 65 20 61 72 65 20 62 75 69 6c 64 69 6e 67 20 2a  e are building *
2404a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  /.  KeyInfo *pKe
2404b 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
2404c 20 20 20 20 20 2f 2a 20 4b 65 79 20 69 6e 66 6f       /* Key info
2404d 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 6e 20 69  rmation for an i
2404e 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ndex */.  int re
2404f 67 41 75 74 6f 69 6e 63 3b 20 20 20 20 20 20 20  gAutoinc;       
24050 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
24051 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20 75 73  mory register us
24052 65 64 20 62 79 20 41 55 54 4f 49 4e 43 20 2a 2f  ed by AUTOINC */
24053 0a 20 20 69 6e 74 20 64 65 73 74 48 61 73 55 6e  .  int destHasUn
24054 69 71 75 65 49 64 78 20 3d 20 30 3b 20 20 20 20  iqueIdx = 0;    
24055 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
24056 44 65 73 74 20 68 61 73 20 61 20 55 4e 49 51 55  Dest has a UNIQU
24057 45 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  E index */.  int
24058 20 72 65 67 44 61 74 61 2c 20 72 65 67 52 6f 77   regData, regRow
24059 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  id;           /*
2405a 20 52 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69   Registers holdi
2405b 6e 67 20 64 61 74 61 20 61 6e 64 20 72 6f 77 69  ng data and rowi
2405c 64 20 2a 2f 0a 0a 20 20 69 66 28 20 70 53 65 6c  d */..  if( pSel
2405d 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ect==0 ){.    re
2405e 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4d 75 73  turn 0;   /* Mus
2405f 74 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  t be of the form
24060 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e    INSERT INTO ..
24061 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a  . SELECT ... */.
24062 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d    }.  if( pDest-
24063 3e 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20  >pTrigger ){.   
24064 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
24065 74 61 62 31 20 6d 75 73 74 20 6e 6f 74 20 68 61  tab1 must not ha
24066 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20  ve triggers */. 
24067 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
24068 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
24069 42 4c 45 0a 20 20 69 66 28 20 70 44 65 73 74 2d  BLE.  if( pDest-
2406a 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56  >tabFlags & TF_V
2406b 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 72 65  irtual ){.    re
2406c 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62  turn 0;   /* tab
2406d 31 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 20  1 must not be a 
2406e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
2406f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66  .  }.#endif.  if
24070 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  ( onError==OE_De
24071 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 6f 6e 45  fault ){.    onE
24072 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
24073 0a 20 20 7d 0a 20 20 69 66 28 20 6f 6e 45 72 72  .  }.  if( onErr
24074 6f 72 21 3d 4f 45 5f 41 62 6f 72 74 20 26 26 20  or!=OE_Abort && 
24075 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 6f 6c 6c  onError!=OE_Roll
24076 62 61 63 6b 20 29 7b 0a 20 20 20 20 72 65 74 75  back ){.    retu
24077 72 6e 20 30 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f  rn 0;   /* Canno
24078 74 20 64 6f 20 4f 52 20 52 45 50 4c 41 43 45 20  t do OR REPLACE 
24079 6f 72 20 4f 52 20 49 47 4e 4f 52 45 20 6f 72 20  or OR IGNORE or 
2407a 4f 52 20 46 41 49 4c 20 2a 2f 0a 20 20 7d 0a 20  OR FAIL */.  }. 
2407b 20 61 73 73 65 72 74 28 70 53 65 6c 65 63 74 2d   assert(pSelect-
2407c 3e 70 53 72 63 29 3b 20 20 20 2f 2a 20 61 6c 6c  >pSrc);   /* all
2407d 6f 63 61 74 65 64 20 65 76 65 6e 20 69 66 20 74  ocated even if t
2407e 68 65 72 65 20 69 73 20 6e 6f 20 46 52 4f 4d 20  here is no FROM 
2407f 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20  clause */.  if( 
24080 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 6e  pSelect->pSrc->n
24081 53 72 63 21 3d 31 20 29 7b 0a 20 20 20 20 72 65  Src!=1 ){.    re
24082 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f  turn 0;   /* FRO
24083 4d 20 63 6c 61 75 73 65 20 6d 75 73 74 20 68 61  M clause must ha
24084 76 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 74  ve exactly one t
24085 65 72 6d 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  erm */.  }.  if(
24086 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e   pSelect->pSrc->
24087 61 5b 30 5d 2e 70 53 65 6c 65 63 74 20 29 7b 0a  a[0].pSelect ){.
24088 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
24089 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 63  /* FROM clause c
2408a 61 6e 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  annot contain a 
2408b 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a  subquery */.  }.
2408c 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70    if( pSelect->p
2408d 57 68 65 72 65 20 29 7b 0a 20 20 20 20 72 65 74  Where ){.    ret
2408e 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45  urn 0;   /* SELE
2408f 43 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20  CT may not have 
24090 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  a WHERE clause *
24091 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c  /.  }.  if( pSel
24092 65 63 74 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b  ect->pOrderBy ){
24093 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
24094 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e   /* SELECT may n
24095 6f 74 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52  ot have an ORDER
24096 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
24097 7d 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6e 65  }.  /* Do not ne
24098 65 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20 61  ed to test for a
24099 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 2e 20   HAVING clause. 
2409a 20 49 66 20 48 41 56 49 4e 47 20 69 73 20 70 72   If HAVING is pr
2409b 65 73 65 6e 74 20 62 75 74 0a 20 20 2a 2a 20 74  esent but.  ** t
2409c 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52  here is no ORDER
2409d 20 42 59 2c 20 77 65 20 77 69 6c 6c 20 67 65 74   BY, we will get
2409e 20 61 6e 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20   an error. */.  
2409f 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 47 72  if( pSelect->pGr
240a0 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 72 65 74  oupBy ){.    ret
240a1 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45  urn 0;   /* SELE
240a2 43 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20  CT may not have 
240a3 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  a GROUP BY claus
240a4 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  e */.  }.  if( p
240a5 53 65 6c 65 63 74 2d 3e 70 4c 69 6d 69 74 20 29  Select->pLimit )
240a6 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
240a7 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20    /* SELECT may 
240a8 6e 6f 74 20 68 61 76 65 20 61 20 4c 49 4d 49 54  not have a LIMIT
240a9 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20   clause */.  }. 
240aa 20 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74   assert( pSelect
240ab 2d 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20  ->pOffset==0 ); 
240ac 20 2f 2a 20 4d 75 73 74 20 62 65 20 73 6f 20 69   /* Must be so i
240ad 66 20 70 4c 69 6d 69 74 3d 3d 30 20 2a 2f 0a 20  f pLimit==0 */. 
240ae 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 50   if( pSelect->pP
240af 72 69 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75  rior ){.    retu
240b0 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
240b1 54 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 63  T may not be a c
240b2 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 2a 2f  ompound query */
240b3 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65  .  }.  if( pSele
240b4 63 74 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  ct->selFlags & S
240b5 46 5f 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20  F_Distinct ){.  
240b6 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
240b7 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
240b8 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20  be DISTINCT */. 
240b9 20 7d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53   }.  pEList = pS
240ba 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20  elect->pEList;. 
240bb 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
240bc 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69  =0 );.  if( pELi
240bd 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a  st->nExpr!=1 ){.
240be 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
240bf 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65  /* The result se
240c0 74 20 6d 75 73 74 20 68 61 76 65 20 65 78 61 63  t must have exac
240c1 74 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 2a  tly one column *
240c2 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  /.  }.  assert( 
240c3 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
240c4 70 72 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69  pr );.  if( pELi
240c5 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e  st->a[0].pExpr->
240c6 6f 70 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20  op!=TK_ALL ){.  
240c7 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
240c8 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   The result set 
240c9 6d 75 73 74 20 62 65 20 74 68 65 20 73 70 65 63  must be the spec
240ca 69 61 6c 20 6f 70 65 72 61 74 6f 72 20 22 2a 22  ial operator "*"
240cb 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74   */.  }..  /* At
240cc 20 74 68 69 73 20 70 6f 69 6e 74 20 77 65 20 68   this point we h
240cd 61 76 65 20 65 73 74 61 62 6c 69 73 68 65 64 20  ave established 
240ce 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d 65  that the stateme
240cf 6e 74 20 69 73 20 6f 66 20 74 68 65 0a 20 20 2a  nt is of the.  *
240d0 2a 20 63 6f 72 72 65 63 74 20 73 79 6e 74 61 63  * correct syntac
240d1 74 69 63 20 66 6f 72 6d 20 74 6f 20 70 61 72 74  tic form to part
240d2 69 63 69 70 61 74 65 20 69 6e 20 74 68 69 73 20  icipate in this 
240d3 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 4e  optimization.  N
240d4 6f 77 0a 20 20 2a 2a 20 77 65 20 68 61 76 65 20  ow.  ** we have 
240d5 74 6f 20 63 68 65 63 6b 20 74 68 65 20 73 65 6d  to check the sem
240d6 61 6e 74 69 63 73 2e 0a 20 20 2a 2f 0a 20 20 70  antics..  */.  p
240d7 49 74 65 6d 20 3d 20 70 53 65 6c 65 63 74 2d 3e  Item = pSelect->
240d8 70 53 72 63 2d 3e 61 3b 0a 20 20 70 53 72 63 20  pSrc->a;.  pSrc 
240d9 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54  = sqlite3LocateT
240da 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20  able(pParse, 0, 
240db 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49  pItem->zName, pI
240dc 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  tem->zDatabase);
240dd 0a 20 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29  .  if( pSrc==0 )
240de 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
240df 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
240e0 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69   does not contai
240e1 6e 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 2a  n a real table *
240e2 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 72 63  /.  }.  if( pSrc
240e3 3d 3d 70 44 65 73 74 20 29 7b 0a 20 20 20 20 72  ==pDest ){.    r
240e4 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61  eturn 0;   /* ta
240e5 62 31 20 61 6e 64 20 74 61 62 32 20 6d 61 79 20  b1 and tab2 may 
240e6 6e 6f 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  not be the same 
240e7 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 69 66  table */.  }.#if
240e8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
240e9 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
240ea 69 66 28 20 70 53 72 63 2d 3e 74 61 62 46 6c 61  if( pSrc->tabFla
240eb 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 20  gs & TF_Virtual 
240ec 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
240ed 20 20 20 2f 2a 20 74 61 62 32 20 6d 75 73 74 20     /* tab2 must 
240ee 6e 6f 74 20 62 65 20 61 20 76 69 72 74 75 61 6c  not be a virtual
240ef 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65   table */.  }.#e
240f0 6e 64 69 66 0a 20 20 69 66 28 20 70 53 72 63 2d  ndif.  if( pSrc-
240f1 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
240f2 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74  return 0;   /* t
240f3 61 62 32 20 6d 61 79 20 6e 6f 74 20 62 65 20 61  ab2 may not be a
240f4 20 76 69 65 77 20 2a 2f 0a 20 20 7d 0a 20 20 69   view */.  }.  i
240f5 66 28 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 21 3d  f( pDest->nCol!=
240f6 70 53 72 63 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pSrc->nCol ){.  
240f7 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
240f8 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
240f9 6e 73 20 6d 75 73 74 20 62 65 20 74 68 65 20 73  ns must be the s
240fa 61 6d 65 20 69 6e 20 74 61 62 31 20 61 6e 64 20  ame in tab1 and 
240fb 74 61 62 32 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  tab2 */.  }.  if
240fc 28 20 70 44 65 73 74 2d 3e 69 50 4b 65 79 21 3d  ( pDest->iPKey!=
240fd 70 53 72 63 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pSrc->iPKey ){. 
240fe 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
240ff 2a 20 42 6f 74 68 20 74 61 62 6c 65 73 20 6d 75  * Both tables mu
24100 73 74 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  st have the same
24101 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
24102 20 4b 45 59 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f   KEY */.  }.  fo
24103 72 28 69 3d 30 3b 20 69 3c 70 44 65 73 74 2d 3e  r(i=0; i<pDest->
24104 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
24105 69 66 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b  if( pDest->aCol[
24106 69 5d 2e 61 66 66 69 6e 69 74 79 21 3d 70 53 72  i].affinity!=pSr
24107 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e  c->aCol[i].affin
24108 69 74 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ity ){.      ret
24109 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 41 66 66  urn 0;    /* Aff
2410a 69 6e 69 74 79 20 6d 75 73 74 20 62 65 20 74 68  inity must be th
2410b 65 20 73 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f  e same on all co
2410c 6c 75 6d 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20  lumns */.    }. 
2410d 20 20 20 69 66 28 20 21 78 66 65 72 43 6f 6d 70     if( !xferComp
2410e 61 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28  atibleCollation(
2410f 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a  pDest->aCol[i].z
24110 43 6f 6c 6c 2c 20 70 53 72 63 2d 3e 61 43 6f 6c  Coll, pSrc->aCol
24111 5b 69 5d 2e 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20  [i].zColl) ){.  
24112 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
24113 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
24114 71 75 65 6e 63 65 20 6d 75 73 74 20 62 65 20 74  quence must be t
24115 68 65 20 73 61 6d 65 20 6f 6e 20 61 6c 6c 20 63  he same on all c
24116 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a  olumns */.    }.
24117 20 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 61      if( pDest->a
24118 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 26  Col[i].notNull &
24119 26 20 21 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d  & !pSrc->aCol[i]
2411a 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  .notNull ){.    
2411b 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f    return 0;    /
2411c 2a 20 74 61 62 32 20 6d 75 73 74 20 62 65 20 4e  * tab2 must be N
2411d 4f 54 20 4e 55 4c 4c 20 69 66 20 74 61 62 31 20  OT NULL if tab1 
2411e 69 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a  is */.    }.  }.
2411f 20 20 66 6f 72 28 70 44 65 73 74 49 64 78 3d 70    for(pDestIdx=p
24120 44 65 73 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44  Dest->pIndex; pD
24121 65 73 74 49 64 78 3b 20 70 44 65 73 74 49 64 78  estIdx; pDestIdx
24122 3d 70 44 65 73 74 49 64 78 2d 3e 70 4e 65 78 74  =pDestIdx->pNext
24123 29 7b 0a 20 20 20 20 69 66 28 20 70 44 65 73 74  ){.    if( pDest
24124 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45  Idx->onError!=OE
24125 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 64  _None ){.      d
24126 65 73 74 48 61 73 55 6e 69 71 75 65 49 64 78 20  estHasUniqueIdx 
24127 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  = 1;.    }.    f
24128 6f 72 28 70 53 72 63 49 64 78 3d 70 53 72 63 2d  or(pSrcIdx=pSrc-
24129 3e 70 49 6e 64 65 78 3b 20 70 53 72 63 49 64 78  >pIndex; pSrcIdx
2412a 3b 20 70 53 72 63 49 64 78 3d 70 53 72 63 49 64  ; pSrcIdx=pSrcId
2412b 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
2412c 20 69 66 28 20 78 66 65 72 43 6f 6d 70 61 74 69   if( xferCompati
2412d 62 6c 65 49 6e 64 65 78 28 70 44 65 73 74 49 64  bleIndex(pDestId
2412e 78 2c 20 70 53 72 63 49 64 78 29 20 29 20 62 72  x, pSrcIdx) ) br
2412f 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
24130 66 28 20 70 53 72 63 49 64 78 3d 3d 30 20 29 7b  f( pSrcIdx==0 ){
24131 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
24132 20 20 20 20 2f 2a 20 70 44 65 73 74 49 64 78 20      /* pDestIdx 
24133 68 61 73 20 6e 6f 20 63 6f 72 72 65 73 70 6f 6e  has no correspon
24134 64 69 6e 67 20 69 6e 64 65 78 20 69 6e 20 70 53  ding index in pS
24135 72 63 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a  rc */.    }.  }.
24136 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
24137 4d 49 54 5f 43 48 45 43 4b 0a 20 20 69 66 28 20  MIT_CHECK.  if( 
24138 70 44 65 73 74 2d 3e 70 43 68 65 63 6b 20 26 26  pDest->pCheck &&
24139 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d   !sqlite3ExprCom
2413a 70 61 72 65 28 70 53 72 63 2d 3e 70 43 68 65 63  pare(pSrc->pChec
2413b 6b 2c 20 70 44 65 73 74 2d 3e 70 43 68 65 63 6b  k, pDest->pCheck
2413c 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2413d 30 3b 20 20 20 2f 2a 20 54 61 62 6c 65 73 20 68  0;   /* Tables h
2413e 61 76 65 20 64 69 66 66 65 72 65 6e 74 20 43 48  ave different CH
2413f 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  ECK constraints.
24140 20 20 54 69 63 6b 65 74 20 23 32 32 35 32 20 2a    Ticket #2252 *
24141 2f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  /.  }.#endif..  
24142 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69  /* If we get thi
24143 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20  s far, it means 
24144 65 69 74 68 65 72 3a 0a 20 20 2a 2a 0a 20 20 2a  either:.  **.  *
24145 2a 20 20 20 20 2a 20 20 20 57 65 20 63 61 6e 20  *    *   We can 
24146 61 6c 77 61 79 73 20 64 6f 20 74 68 65 20 74 72  always do the tr
24147 61 6e 73 66 65 72 20 69 66 20 74 68 65 20 74 61  ansfer if the ta
24148 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a  ble contains an.
24149 20 20 2a 2a 20 20 20 20 20 20 20 20 61 6e 20 69    **        an i
2414a 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b  nteger primary k
2414b 65 79 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  ey.  **.  **    
2414c 2a 20 20 20 57 65 20 63 61 6e 20 63 6f 6e 64 69  *   We can condi
2414d 74 69 6f 6e 61 6c 6c 79 20 64 6f 20 74 68 65 20  tionally do the 
2414e 74 72 61 6e 73 66 65 72 20 69 66 20 74 68 65 20  transfer if the 
2414f 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a  destination.  **
24150 20 20 20 20 20 20 20 20 74 61 62 6c 65 20 69 73          table is
24151 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 23 69 66   empty..  */.#if
24152 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
24153 20 20 73 71 6c 69 74 65 33 5f 78 66 65 72 6f 70    sqlite3_xferop
24154 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69  t_count++;.#endi
24155 66 0a 20 20 69 44 62 53 72 63 20 3d 20 73 71 6c  f.  iDbSrc = sql
24156 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
24157 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  x(pParse->db, pS
24158 72 63 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  rc->pSchema);.  
24159 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2415a 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  be(pParse);.  sq
2415b 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
2415c 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44  chema(pParse, iD
2415d 62 53 72 63 29 3b 0a 20 20 69 53 72 63 20 3d 20  bSrc);.  iSrc = 
2415e 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
2415f 20 20 69 44 65 73 74 20 3d 20 70 50 61 72 73 65    iDest = pParse
24160 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 72 65 67 41  ->nTab++;.  regA
24161 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f 49 6e 63  utoinc = autoInc
24162 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 69 44  Begin(pParse, iD
24163 62 44 65 73 74 2c 20 70 44 65 73 74 29 3b 0a 20  bDest, pDest);. 
24164 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
24165 65 28 70 50 61 72 73 65 2c 20 69 44 65 73 74 2c  e(pParse, iDest,
24166 20 69 44 62 44 65 73 74 2c 20 70 44 65 73 74 2c   iDbDest, pDest,
24167 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a   OP_OpenWrite);.
24168 20 20 69 66 28 20 28 70 44 65 73 74 2d 3e 69 50    if( (pDest->iP
24169 4b 65 79 3c 30 20 26 26 20 70 44 65 73 74 2d 3e  Key<0 && pDest->
2416a 70 49 6e 64 65 78 21 3d 30 29 20 7c 7c 20 64 65  pIndex!=0) || de
2416b 73 74 48 61 73 55 6e 69 71 75 65 49 64 78 20 29  stHasUniqueIdx )
2416c 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 61 62 6c  {.    /* If tabl
2416d 65 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 61  es do not have a
2416e 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  n INTEGER PRIMAR
2416f 59 20 4b 45 59 20 61 6e 64 20 74 68 65 72 65 0a  Y KEY and there.
24170 20 20 20 20 2a 2a 20 61 72 65 20 69 6e 64 69 63      ** are indic
24171 65 73 20 74 6f 20 62 65 20 63 6f 70 69 65 64 20  es to be copied 
24172 61 6e 64 20 74 68 65 20 64 65 73 74 69 6e 61 74  and the destinat
24173 69 6f 6e 20 69 73 20 6e 6f 74 20 65 6d 70 74 79  ion is not empty
24174 2c 0a 20 20 20 20 2a 2a 20 77 65 20 68 61 76 65  ,.    ** we have
24175 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65   to disallow the
24176 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
24177 7a 61 74 69 6f 6e 20 62 65 63 61 75 73 65 20 74  zation because t
24178 68 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 6f  he.    ** the ro
24179 77 69 64 73 20 6d 69 67 68 74 20 63 68 61 6e 67  wids might chang
2417a 65 20 77 68 69 63 68 20 77 69 6c 6c 20 6d 65 73  e which will mes
2417b 73 20 75 70 20 69 6e 64 65 78 69 6e 67 2e 0a 20  s up indexing.. 
2417c 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 72 20     **.    ** Or 
2417d 69 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  if the destinati
2417e 6f 6e 20 68 61 73 20 61 20 55 4e 49 51 55 45 20  on has a UNIQUE 
2417f 69 6e 64 65 78 20 61 6e 64 20 69 73 20 6e 6f 74  index and is not
24180 20 65 6d 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77   empty,.    ** w
24181 65 20 61 6c 73 6f 20 64 69 73 61 6c 6c 6f 77 20  e also disallow 
24182 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74  the transfer opt
24183 69 6d 69 7a 61 74 69 6f 6e 20 62 65 63 61 75 73  imization becaus
24184 65 20 77 65 20 63 61 6e 6e 6f 74 0a 20 20 20 20  e we cannot.    
24185 2a 2a 20 69 6e 73 75 72 65 20 74 68 61 74 20 61  ** insure that a
24186 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  ll entries in th
24187 65 20 75 6e 69 6f 6e 20 6f 66 20 44 45 53 54 20  e union of DEST 
24188 61 6e 64 20 53 52 43 20 77 69 6c 6c 20 62 65 0a  and SRC will be.
24189 20 20 20 20 2a 2a 20 75 6e 69 71 75 65 2e 0a 20      ** unique.. 
2418a 20 20 20 2a 2f 0a 20 20 20 20 61 64 64 72 31 20     */.    addr1 
2418b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2418c 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
2418d 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20  , iDest, 0);.   
2418e 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 20 3d   emptyDestTest =
2418f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24190 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
24191 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
24192 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
24193 20 61 64 64 72 31 29 3b 0a 20 20 7d 65 6c 73 65   addr1);.  }else
24194 7b 0a 20 20 20 20 65 6d 70 74 79 44 65 73 74 54  {.    emptyDestT
24195 65 73 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73  est = 0;.  }.  s
24196 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
24197 70 50 61 72 73 65 2c 20 69 53 72 63 2c 20 69 44  pParse, iSrc, iD
24198 62 53 72 63 2c 20 70 53 72 63 2c 20 4f 50 5f 4f  bSrc, pSrc, OP_O
24199 70 65 6e 52 65 61 64 29 3b 0a 20 20 65 6d 70 74  penRead);.  empt
2419a 79 53 72 63 54 65 73 74 20 3d 20 73 71 6c 69 74  ySrcTest = sqlit
2419b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2419c 4f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72 63 2c  OP_Rewind, iSrc,
2419d 20 30 29 3b 0a 20 20 72 65 67 44 61 74 61 20 3d   0);.  regData =
2419e 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2419f 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 72 65  eg(pParse);.  re
241a0 67 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  gRowid = sqlite3
241a1 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
241a2 65 29 3b 0a 20 20 69 66 28 20 70 44 65 73 74 2d  e);.  if( pDest-
241a3 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20  >iPKey>=0 ){.   
241a4 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33   addr1 = sqlite3
241a5 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
241a6 5f 52 6f 77 69 64 2c 20 69 53 72 63 2c 20 72 65  _Rowid, iSrc, re
241a7 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 61 64 64  gRowid);.    add
241a8 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r2 = sqlite3Vdbe
241a9 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp3(v, OP_Not
241aa 45 78 69 73 74 73 2c 20 69 44 65 73 74 2c 20 30  Exists, iDest, 0
241ab 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
241ac 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
241ad 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p4(v, OP_Halt, S
241ae 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
241af 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 0a 20 20  , onError, 0,.  
241b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
241b1 20 20 20 20 22 50 52 49 4d 41 52 59 20 4b 45 59      "PRIMARY KEY
241b2 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 22   must be unique"
241b3 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
241b4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
241b5 70 48 65 72 65 28 76 2c 20 61 64 64 72 32 29 3b  pHere(v, addr2);
241b6 0a 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65 70  .    autoIncStep
241b7 28 70 50 61 72 73 65 2c 20 72 65 67 41 75 74 6f  (pParse, regAuto
241b8 69 6e 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  inc, regRowid);.
241b9 20 20 7d 65 6c 73 65 20 69 66 28 20 70 44 65 73    }else if( pDes
241ba 74 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a  t->pIndex==0 ){.
241bb 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
241bc 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
241bd 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 44   OP_NewRowid, iD
241be 65 73 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  est, regRowid);.
241bf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64    }else{.    add
241c0 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
241c1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
241c2 69 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f 77  id, iSrc, regRow
241c3 69 64 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  id);.    assert(
241c4 20 28 70 44 65 73 74 2d 3e 74 61 62 46 6c 61 67   (pDest->tabFlag
241c5 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65  s & TF_Autoincre
241c6 6d 65 6e 74 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a  ment)==0 );.  }.
241c7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
241c8 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74  Op2(v, OP_RowDat
241c9 61 2c 20 69 53 72 63 2c 20 72 65 67 44 61 74 61  a, iSrc, regData
241ca 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
241cb 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
241cc 65 72 74 2c 20 69 44 65 73 74 2c 20 72 65 67 44  ert, iDest, regD
241cd 61 74 61 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  ata, regRowid);.
241ce 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
241cf 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f  ngeP5(v, OPFLAG_
241d0 4e 43 48 41 4e 47 45 7c 4f 50 46 4c 41 47 5f 4c  NCHANGE|OPFLAG_L
241d1 41 53 54 52 4f 57 49 44 7c 4f 50 46 4c 41 47 5f  ASTROWID|OPFLAG_
241d2 41 50 50 45 4e 44 29 3b 0a 20 20 73 71 6c 69 74  APPEND);.  sqlit
241d3 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
241d4 2c 20 2d 31 2c 20 70 44 65 73 74 2d 3e 7a 4e 61  , -1, pDest->zNa
241d5 6d 65 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  me, 0);.  sqlite
241d6 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
241d7 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64  P_Next, iSrc, ad
241d8 64 72 31 29 3b 0a 20 20 61 75 74 6f 49 6e 63 45  dr1);.  autoIncE
241d9 6e 64 28 70 50 61 72 73 65 2c 20 69 44 62 44 65  nd(pParse, iDbDe
241da 73 74 2c 20 70 44 65 73 74 2c 20 72 65 67 41 75  st, pDest, regAu
241db 74 6f 69 6e 63 29 3b 0a 20 20 66 6f 72 28 70 44  toinc);.  for(pD
241dc 65 73 74 49 64 78 3d 70 44 65 73 74 2d 3e 70 49  estIdx=pDest->pI
241dd 6e 64 65 78 3b 20 70 44 65 73 74 49 64 78 3b 20  ndex; pDestIdx; 
241de 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 49 64  pDestIdx=pDestId
241df 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 66  x->pNext){.    f
241e0 6f 72 28 70 53 72 63 49 64 78 3d 70 53 72 63 2d  or(pSrcIdx=pSrc-
241e1 3e 70 49 6e 64 65 78 3b 20 70 53 72 63 49 64 78  >pIndex; pSrcIdx
241e2 3b 20 70 53 72 63 49 64 78 3d 70 53 72 63 49 64  ; pSrcIdx=pSrcId
241e3 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
241e4 20 69 66 28 20 78 66 65 72 43 6f 6d 70 61 74 69   if( xferCompati
241e5 62 6c 65 49 6e 64 65 78 28 70 44 65 73 74 49 64  bleIndex(pDestId
241e6 78 2c 20 70 53 72 63 49 64 78 29 20 29 20 62 72  x, pSrcIdx) ) br
241e7 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  eak;.    }.    a
241e8 73 73 65 72 74 28 20 70 53 72 63 49 64 78 20 29  ssert( pSrcIdx )
241e9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
241ea 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
241eb 6f 73 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20  ose, iSrc, 0);. 
241ec 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
241ed 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
241ee 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20  , iDest, 0);.   
241ef 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49   pKey = sqlite3I
241f0 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
241f1 73 65 2c 20 70 53 72 63 49 64 78 29 3b 0a 20 20  se, pSrcIdx);.  
241f2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
241f3 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65  Op4(v, OP_OpenRe
241f4 61 64 2c 20 69 53 72 63 2c 20 70 53 72 63 49 64  ad, iSrc, pSrcId
241f5 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 53 72 63 2c  x->tnum, iDbSrc,
241f6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
241f7 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
241f8 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  ey, P4_KEYINFO_H
241f9 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62  ANDOFF);.    Vdb
241fa 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73  eComment((v, "%s
241fb 22 2c 20 70 53 72 63 49 64 78 2d 3e 7a 4e 61 6d  ", pSrcIdx->zNam
241fc 65 29 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20  e));.    pKey = 
241fd 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
241fe 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 44 65 73  nfo(pParse, pDes
241ff 74 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  tIdx);.    sqlit
24200 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
24201 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 44  OP_OpenWrite, iD
24202 65 73 74 2c 20 70 44 65 73 74 49 64 78 2d 3e 74  est, pDestIdx->t
24203 6e 75 6d 2c 20 69 44 62 44 65 73 74 2c 0a 20 20  num, iDbDest,.  
24204 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24205 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c      (char*)pKey,
24206 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
24207 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  OFF);.    VdbeCo
24208 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20  mment((v, "%s", 
24209 70 44 65 73 74 49 64 78 2d 3e 7a 4e 61 6d 65 29  pDestIdx->zName)
2420a 29 3b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73  );.    addr1 = s
2420b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2420c 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
2420d 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  Src, 0);.    sql
2420e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2420f 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c 20 69 53 72  , OP_RowKey, iSr
24210 63 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20 20  c, regData);.   
24211 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24212 70 33 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  p3(v, OP_IdxInse
24213 72 74 2c 20 69 44 65 73 74 2c 20 72 65 67 44 61  rt, iDest, regDa
24214 74 61 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  ta, 1);.    sqli
24215 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24216 20 4f 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20   OP_Next, iSrc, 
24217 61 64 64 72 31 2b 31 29 3b 0a 20 20 20 20 73 71  addr1+1);.    sq
24218 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
24219 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d  e(v, addr1);.  }
2421a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
2421b 6d 70 48 65 72 65 28 76 2c 20 65 6d 70 74 79 53  mpHere(v, emptyS
2421c 72 63 54 65 73 74 29 3b 0a 20 20 73 71 6c 69 74  rcTest);.  sqlit
2421d 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2421e 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69  (pParse, regRowi
2421f 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  d);.  sqlite3Rel
24220 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
24221 73 65 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20  se, regData);.  
24222 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24223 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  2(v, OP_Close, i
24224 53 72 63 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  Src, 0);.  sqlit
24225 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
24226 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c  OP_Close, iDest,
24227 20 30 29 3b 0a 20 20 69 66 28 20 65 6d 70 74 79   0);.  if( empty
24228 44 65 73 74 54 65 73 74 20 29 7b 0a 20 20 20 20  DestTest ){.    
24229 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2422a 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  2(v, OP_Halt, SQ
2422b 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20  LITE_OK, 0);.   
2422c 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2422d 48 65 72 65 28 76 2c 20 65 6d 70 74 79 44 65 73  Here(v, emptyDes
2422e 74 54 65 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  tTest);.    sqli
2422f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24230 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74   OP_Close, iDest
24231 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , 0);.    return
24232 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
24233 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d   return 1;.  }.}
24234 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
24235 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20  E_OMIT_XFER_OPT 
24236 2a 2f 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65  */../* Make sure
24237 20 22 69 73 56 69 65 77 22 20 67 65 74 73 20 75   "isView" gets u
24238 6e 64 65 66 69 6e 65 64 20 69 6e 20 63 61 73 65  ndefined in case
24239 20 74 68 69 73 20 66 69 6c 65 20 62 65 63 6f 6d   this file becom
2423a 65 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68  es part of.** th
2423b 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 2d  e amalgamation -
2423c 20 73 6f 20 74 68 61 74 20 73 75 62 73 65 71 75   so that subsequ
2423d 65 6e 74 20 66 69 6c 65 73 20 64 6f 20 6e 6f 74  ent files do not
2423e 20 73 65 65 20 69 73 56 69 65 77 20 61 73 20 61   see isView as a
2423f 0a 2a 2a 20 6d 61 63 72 6f 2e 20 2a 2f 0a 23 75  .** macro. */.#u
24240 6e 64 65 66 20 69 73 56 69 65 77 0a 0a 2f 2a 2a  ndef isView../**
24241 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
24242 20 6f 66 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a   of insert.c ***
24243 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24244 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24245 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
24246 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
24247 69 6e 20 66 69 6c 65 20 6c 65 67 61 63 79 2e 63  in file legacy.c
24248 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
24249 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2424a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2424b 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
2424c 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
2424d 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2424e 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2424f 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
24250 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
24251 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
24252 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
24253 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
24254 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
24255 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
24256 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
24257 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
24258 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
24259 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2425a 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2425b 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2425c 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2425d 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2425e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2425f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24261 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24262 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e 20 66 69 6c 65  ***.** Main file
24263 20 66 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20   for the SQLite 
24264 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 72 6f  library.  The ro
24265 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66  utines in this f
24266 69 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ile.** implement
24267 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 65 72 20   the programmer 
24268 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65  interface to the
24269 20 6c 69 62 72 61 72 79 2e 20 20 52 6f 75 74 69   library.  Routi
2426a 6e 65 73 20 69 6e 0a 2a 2a 20 6f 74 68 65 72 20  nes in.** other 
2426b 66 69 6c 65 73 20 61 72 65 20 66 6f 72 20 69 6e  files are for in
2426c 74 65 72 6e 61 6c 20 75 73 65 20 62 79 20 53 51  ternal use by SQ
2426d 4c 69 74 65 20 61 6e 64 20 73 68 6f 75 6c 64 20  Lite and should 
2426e 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73  not be.** access
2426f 65 64 20 62 79 20 75 73 65 72 73 20 6f 66 20 74  ed by users of t
24270 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a  he library..**.*
24271 2a 20 24 49 64 3a 20 6c 65 67 61 63 79 2e 63 2c  * $Id: legacy.c,
24272 76 20 31 2e 33 30 20 32 30 30 38 2f 31 32 2f 31  v 1.30 2008/12/1
24273 30 20 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45  0 19:26:24 drh E
24274 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  xp $.*/.../*.** 
24275 45 78 65 63 75 74 65 20 53 51 4c 20 63 6f 64 65  Execute SQL code
24276 2e 20 20 52 65 74 75 72 6e 20 6f 6e 65 20 6f 66  .  Return one of
24277 20 74 68 65 20 53 51 4c 49 54 45 5f 20 73 75 63   the SQLITE_ suc
24278 63 65 73 73 2f 66 61 69 6c 75 72 65 0a 2a 2a 20  cess/failure.** 
24279 63 6f 64 65 73 2e 20 20 41 6c 73 6f 20 77 72 69  codes.  Also wri
2427a 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  te an error mess
2427b 61 67 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  age into memory 
2427c 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
2427d 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61   malloc() and ma
2427e 6b 65 20 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69  ke *pzErrMsg poi
2427f 6e 74 20 74 6f 20 74 68 61 74 20 6d 65 73 73 61  nt to that messa
24280 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ge..**.** If the
24281 20 53 51 4c 20 69 73 20 61 20 71 75 65 72 79 2c   SQL is a query,
24282 20 74 68 65 6e 20 66 6f 72 20 65 61 63 68 20 72   then for each r
24283 6f 77 20 69 6e 20 74 68 65 20 71 75 65 72 79 20  ow in the query 
24284 72 65 73 75 6c 74 0a 2a 2a 20 74 68 65 20 78 43  result.** the xC
24285 61 6c 6c 62 61 63 6b 28 29 20 66 75 6e 63 74 69  allback() functi
24286 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 70  on is called.  p
24287 41 72 67 20 62 65 63 6f 6d 65 73 20 74 68 65 20  Arg becomes the 
24288 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e  first.** argumen
24289 74 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29  t to xCallback()
2428a 2e 20 20 49 66 20 78 43 61 6c 6c 62 61 63 6b 3d  .  If xCallback=
2428b 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 63 61 6c  NULL then no cal
2428c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f  lback.** is invo
2428d 6b 65 64 2c 20 65 76 65 6e 20 66 6f 72 20 71 75  ked, even for qu
2428e 65 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eries..*/.SQLITE
2428f 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
24290 5f 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65 33  _exec(.  sqlite3
24291 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
24292 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
24293 62 61 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68  base on which th
24294 65 20 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a  e SQL executes *
24295 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
24296 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20  zSql,           
24297 2f 2a 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65  /* The SQL to be
24298 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 73   executed */.  s
24299 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 20  qlite3_callback 
2429a 78 43 61 6c 6c 62 61 63 6b 2c 20 2f 2a 20 49 6e  xCallback, /* In
2429b 76 6f 6b 65 20 74 68 69 73 20 63 61 6c 6c 62 61  voke this callba
2429c 63 6b 20 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  ck routine */.  
2429d 76 6f 69 64 20 2a 70 41 72 67 2c 20 20 20 20 20  void *pArg,     
2429e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
2429f 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
242a0 20 78 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 0a   xCallback() */.
242a1 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73    char **pzErrMs
242a2 67 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  g             /*
242a3 20 57 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73   Write error mes
242a4 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b  sages here */.){
242a5 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
242a6 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 63  TE_OK;.  const c
242a7 68 61 72 20 2a 7a 4c 65 66 74 6f 76 65 72 3b 0a  har *zLeftover;.
242a8 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
242a9 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 63 68 61  pStmt = 0;.  cha
242aa 72 20 2a 2a 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a  r **azCols = 0;.
242ab 0a 20 20 69 6e 74 20 6e 52 65 74 72 79 20 3d 20  .  int nRetry = 
242ac 30 3b 0a 20 20 69 6e 74 20 6e 43 61 6c 6c 62 61  0;.  int nCallba
242ad 63 6b 3b 0a 0a 20 20 69 66 28 20 7a 53 71 6c 3d  ck;..  if( zSql=
242ae 3d 30 20 29 20 7a 53 71 6c 20 3d 20 22 22 3b 0a  =0 ) zSql = "";.
242af 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
242b0 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
242b1 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  );.  sqlite3Erro
242b2 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  r(db, SQLITE_OK,
242b3 20 30 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 72   0);.  while( (r
242b4 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
242b5 28 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45  (rc==SQLITE_SCHE
242b6 4d 41 20 26 26 20 28 2b 2b 6e 52 65 74 72 79 29  MA && (++nRetry)
242b7 3c 32 29 29 20 26 26 20 7a 53 71 6c 5b 30 5d 20  <2)) && zSql[0] 
242b8 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b  ){.    int nCol;
242b9 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 56 61  .    char **azVa
242ba 6c 73 20 3d 20 30 3b 0a 0a 20 20 20 20 70 53 74  ls = 0;..    pSt
242bb 6d 74 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  mt = 0;.    rc =
242bc 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
242bd 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
242be 70 53 74 6d 74 2c 20 26 7a 4c 65 66 74 6f 76 65  pStmt, &zLeftove
242bf 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  r);.    assert( 
242c0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
242c1 20 70 53 74 6d 74 3d 3d 30 20 29 3b 0a 20 20 20   pStmt==0 );.   
242c2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
242c3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74  OK ){.      cont
242c4 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  inue;.    }.    
242c5 69 66 28 20 21 70 53 74 6d 74 20 29 7b 0a 20 20  if( !pStmt ){.  
242c6 20 20 20 20 2f 2a 20 74 68 69 73 20 68 61 70 70      /* this happ
242c7 65 6e 73 20 66 6f 72 20 61 20 63 6f 6d 6d 65 6e  ens for a commen
242c8 74 20 6f 72 20 77 68 69 74 65 2d 73 70 61 63 65  t or white-space
242c9 20 2a 2f 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d   */.      zSql =
242ca 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 20 20   zLeftover;.    
242cb 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
242cc 7d 0a 0a 20 20 20 20 6e 43 61 6c 6c 62 61 63 6b  }..    nCallback
242cd 20 3d 20 30 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d   = 0;.    nCol =
242ce 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
242cf 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 0a 20  count(pStmt);.. 
242d0 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20     while( 1 ){. 
242d1 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
242d2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
242d3 74 65 70 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20  tep(pStmt);..   
242d4 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65     /* Invoke the
242d5 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
242d6 6f 6e 20 69 66 20 72 65 71 75 69 72 65 64 20 2a  on if required *
242d7 2f 0a 20 20 20 20 20 20 69 66 28 20 78 43 61 6c  /.      if( xCal
242d8 6c 62 61 63 6b 20 26 26 20 28 53 51 4c 49 54 45  lback && (SQLITE
242d9 5f 52 4f 57 3d 3d 72 63 20 7c 7c 20 0a 20 20 20  _ROW==rc || .   
242da 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 44         (SQLITE_D
242db 4f 4e 45 3d 3d 72 63 20 26 26 20 21 6e 43 61 6c  ONE==rc && !nCal
242dc 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 66 6c 61  lback && db->fla
242dd 67 73 26 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61  gs&SQLITE_NullCa
242de 6c 6c 62 61 63 6b 29 29 20 29 7b 0a 20 20 20 20  llback)) ){.    
242df 20 20 20 20 69 66 28 20 30 3d 3d 6e 43 61 6c 6c      if( 0==nCall
242e0 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  back ){.        
242e1 20 20 69 66 28 20 61 7a 43 6f 6c 73 3d 3d 30 20    if( azCols==0 
242e2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
242e3 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 44  zCols = sqlite3D
242e4 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
242e5 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 63 6f  2*nCol*sizeof(co
242e6 6e 73 74 20 63 68 61 72 2a 29 20 2b 20 31 29 3b  nst char*) + 1);
242e7 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
242e8 20 61 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20 20   azCols==0 ){.  
242e9 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
242ea 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20   exec_out;.     
242eb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
242ec 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 66     }.          f
242ed 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
242ee 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
242ef 20 20 61 7a 43 6f 6c 73 5b 69 5d 20 3d 20 28 63    azCols[i] = (c
242f0 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f  har *)sqlite3_co
242f1 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c  lumn_name(pStmt,
242f2 20 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   i);.           
242f3 20 2f 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53   /* sqlite3VdbeS
242f4 65 74 43 6f 6c 4e 61 6d 65 28 29 20 69 6e 73 74  etColName() inst
242f5 61 6c 6c 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  alls column name
242f6 73 20 61 73 20 55 54 46 38 0a 20 20 20 20 20 20  s as UTF8.      
242f7 20 20 20 20 20 20 2a 2a 20 73 74 72 69 6e 67 73        ** strings
242f8 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20   so there is no 
242f9 77 61 79 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  way for sqlite3_
242fa 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20 74 6f  column_name() to
242fb 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20   fail. */.      
242fc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61 7a        assert( az
242fd 43 6f 6c 73 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  Cols[i]!=0 );.  
242fe 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
242ff 20 20 20 20 6e 43 61 6c 6c 62 61 63 6b 2b 2b 3b      nCallback++;
24300 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
24301 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
24302 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20  E_ROW ){.       
24303 20 20 20 61 7a 56 61 6c 73 20 3d 20 26 61 7a 43     azVals = &azC
24304 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a 20 20 20 20 20  ols[nCol];.     
24305 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
24306 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
24307 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 5b 69          azVals[i
24308 5d 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  ] = (char *)sqli
24309 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
2430a 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20  pStmt, i);.     
2430b 20 20 20 20 20 20 20 69 66 28 20 21 61 7a 56 61         if( !azVa
2430c 6c 73 5b 69 5d 20 26 26 20 73 71 6c 69 74 65 33  ls[i] && sqlite3
2430d 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74  _column_type(pSt
2430e 6d 74 2c 20 69 29 21 3d 53 51 4c 49 54 45 5f 4e  mt, i)!=SQLITE_N
2430f 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ULL ){.         
24310 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
24311 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
24312 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78           goto ex
24313 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  ec_out;.        
24314 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
24315 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
24316 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63      if( xCallbac
24317 6b 28 70 41 72 67 2c 20 6e 43 6f 6c 2c 20 61 7a  k(pArg, nCol, az
24318 56 61 6c 73 2c 20 61 7a 43 6f 6c 73 29 20 29 7b  Vals, azCols) ){
24319 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
2431a 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20  SQLITE_ABORT;.  
2431b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2431c 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
2431d 0a 20 20 20 20 20 20 20 20 20 20 70 53 74 6d 74  .          pStmt
2431e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
2431f 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
24320 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2c 20 30   SQLITE_ABORT, 0
24321 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
24322 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20  o exec_out;.    
24323 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20      }.      }.. 
24324 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
24325 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
24326 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
24327 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
24328 0a 20 20 20 20 20 20 20 20 70 53 74 6d 74 20 3d  .        pStmt =
24329 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   0;.        if( 
2432a 72 63 21 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d  rc!=SQLITE_SCHEM
2432b 41 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  A ){.          n
2432c 52 65 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20  Retry = 0;.     
2432d 20 20 20 20 20 7a 53 71 6c 20 3d 20 7a 4c 65 66       zSql = zLef
2432e 74 6f 76 65 72 3b 0a 20 20 20 20 20 20 20 20 20  tover;.         
2432f 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28   while( isspace(
24330 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a  (unsigned char)z
24331 53 71 6c 5b 30 5d 29 20 29 20 7a 53 71 6c 2b 2b  Sql[0]) ) zSql++
24332 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
24333 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
24334 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71   }.    }..    sq
24335 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24336 61 7a 43 6f 6c 73 29 3b 0a 20 20 20 20 61 7a 43  azCols);.    azC
24337 6f 6c 73 20 3d 20 30 3b 0a 20 20 7d 0a 0a 65 78  ols = 0;.  }..ex
24338 65 63 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 53  ec_out:.  if( pS
24339 74 6d 74 20 29 20 73 71 6c 69 74 65 33 5f 66 69  tmt ) sqlite3_fi
2433a 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20  nalize(pStmt);. 
2433b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2433c 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 0a 20 20 72  b, azCols);..  r
2433d 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
2433e 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 69 66  it(db, rc);.  if
2433f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
24340 26 26 20 72 63 3d 3d 73 71 6c 69 74 65 33 5f 65  && rc==sqlite3_e
24341 72 72 63 6f 64 65 28 64 62 29 20 26 26 20 70 7a  rrcode(db) && pz
24342 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 69 6e  ErrMsg ){.    in
24343 74 20 6e 45 72 72 4d 73 67 20 3d 20 31 20 2b 20  t nErrMsg = 1 + 
24344 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
24345 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
24346 62 29 29 3b 0a 20 20 20 20 2a 70 7a 45 72 72 4d  b));.    *pzErrM
24347 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  sg = sqlite3Mall
24348 6f 63 28 6e 45 72 72 4d 73 67 29 3b 0a 20 20 20  oc(nErrMsg);.   
24349 20 69 66 28 20 2a 70 7a 45 72 72 4d 73 67 20 29   if( *pzErrMsg )
2434a 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 2a  {.      memcpy(*
2434b 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74 65  pzErrMsg, sqlite
2434c 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20 6e 45  3_errmsg(db), nE
2434d 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20  rrMsg);.    }.  
2434e 7d 65 6c 73 65 20 69 66 28 20 70 7a 45 72 72 4d  }else if( pzErrM
2434f 73 67 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  sg ){.    *pzErr
24350 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  Msg = 0;.  }..  
24351 61 73 73 65 72 74 28 20 28 72 63 26 64 62 2d 3e  assert( (rc&db->
24352 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a  errMask)==rc );.
24353 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
24354 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
24355 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
24356 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
24357 2a 20 45 6e 64 20 6f 66 20 6c 65 67 61 63 79 2e  * End of legacy.
24358 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
24359 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2435a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2435b 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
2435c 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c 6f 61  * Begin file loa
2435d 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  dext.c *********
2435e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2435f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24360 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e  /./*.** 2006 Jun
24361 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  e 7.**.** The au
24362 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
24363 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
24364 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
24365 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
24366 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
24367 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
24368 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
24369 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
2436a 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
2436b 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
2436c 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
2436d 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
2436e 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
2436f 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
24370 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
24371 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
24372 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
24373 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24374 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24375 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24376 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24377 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
24378 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
24379 65 64 20 74 6f 20 64 79 6e 61 6d 69 63 61 6c 6c  ed to dynamicall
2437a 79 20 6c 6f 61 64 20 65 78 74 65 6e 73 69 6f 6e  y load extension
2437b 73 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 53 51  s into.** the SQ
2437c 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a  Lite library..**
2437d 0a 2a 2a 20 24 49 64 3a 20 6c 6f 61 64 65 78 74  .** $Id: loadext
2437e 2e 63 2c 76 20 31 2e 35 37 20 32 30 30 38 2f 31  .c,v 1.57 2008/1
2437f 32 2f 30 38 20 31 38 3a 31 39 3a 31 38 20 64 72  2/08 18:19:18 dr
24380 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e  h Exp $.*/..#ifn
24381 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a  def SQLITE_CORE.
24382 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45    #define SQLITE
24383 5f 43 4f 52 45 20 31 20 20 2f 2a 20 44 69 73 61  _CORE 1  /* Disa
24384 62 6c 65 20 74 68 65 20 41 50 49 20 72 65 64 65  ble the API rede
24385 66 69 6e 69 74 69 6f 6e 20 69 6e 20 73 71 6c 69  finition in sqli
24386 74 65 33 65 78 74 2e 68 20 2a 2f 0a 23 65 6e 64  te3ext.h */.#end
24387 69 66 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if./************
24388 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74  ** Include sqlit
24389 65 33 65 78 74 2e 68 20 69 6e 20 74 68 65 20 6d  e3ext.h in the m
2438a 69 64 64 6c 65 20 6f 66 20 6c 6f 61 64 65 78 74  iddle of loadext
2438b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2438c 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2438d 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71  ** Begin file sq
2438e 6c 69 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a  lite3ext.h *****
2438f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24391 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75  */./*.** 2006 Ju
24392 6e 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ne 7.**.** The a
24393 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
24394 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
24395 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
24396 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
24397 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
24398 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
24399 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2439a 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2439b 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2439c 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2439d 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2439e 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2439f 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
243a0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
243a1 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
243a2 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
243a3 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
243a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243a8 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  ***.** This head
243a9 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20  er file defines 
243aa 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74 65 72  the SQLite inter
243ab 66 61 63 65 20 66 6f 72 20 75 73 65 20 62 79 0a  face for use by.
243ac 2a 2a 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  ** shared librar
243ad 69 65 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f  ies that want to
243ae 20 62 65 20 69 6d 70 6f 72 74 65 64 20 61 73 20   be imported as 
243af 65 78 74 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a  extensions into.
243b0 2a 2a 20 61 6e 20 53 51 4c 69 74 65 20 69 6e 73  ** an SQLite ins
243b1 74 61 6e 63 65 2e 20 20 53 68 61 72 65 64 20 6c  tance.  Shared l
243b2 69 62 72 61 72 69 65 73 20 74 68 61 74 20 69 6e  ibraries that in
243b3 74 65 6e 64 20 74 6f 20 62 65 20 6c 6f 61 64 65  tend to be loade
243b4 64 0a 2a 2a 20 61 73 20 65 78 74 65 6e 73 69 6f  d.** as extensio
243b5 6e 73 20 62 79 20 53 51 4c 69 74 65 20 73 68 6f  ns by SQLite sho
243b6 75 6c 64 20 23 69 6e 63 6c 75 64 65 20 74 68 69  uld #include thi
243b7 73 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f  s file instead o
243b8 66 20 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 68 2e  f .** sqlite3.h.
243b9 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a  .**.** @(#) $Id:
243ba 20 73 71 6c 69 74 65 33 65 78 74 2e 68 2c 76 20   sqlite3ext.h,v 
243bb 31 2e 32 35 20 32 30 30 38 2f 31 30 2f 31 32 20  1.25 2008/10/12 
243bc 30 30 3a 32 37 3a 35 34 20 73 68 61 6e 65 20 45  00:27:54 shane E
243bd 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
243be 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f 0a 23  _SQLITE3EXT_H_.#
243bf 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 45  define _SQLITE3E
243c0 58 54 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20 73  XT_H_..typedef s
243c1 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 61 70  truct sqlite3_ap
243c2 69 5f 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74  i_routines sqlit
243c3 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 3b  e3_api_routines;
243c4 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
243c5 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
243c6 68 6f 6c 64 73 20 70 6f 69 6e 74 65 72 73 20 74  holds pointers t
243c7 6f 20 61 6c 6c 20 6f 66 20 74 68 65 20 53 51 4c  o all of the SQL
243c8 69 74 65 20 41 50 49 0a 2a 2a 20 72 6f 75 74 69  ite API.** routi
243c9 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49  nes..**.** WARNI
243ca 4e 47 3a 20 20 49 6e 20 6f 72 64 65 72 20 74 6f  NG:  In order to
243cb 20 6d 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 61   maintain backwa
243cc 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  rds compatibilit
243cd 79 2c 20 61 64 64 20 6e 65 77 0a 2a 2a 20 69 6e  y, add new.** in
243ce 74 65 72 66 61 63 65 73 20 74 6f 20 74 68 65 20  terfaces to the 
243cf 65 6e 64 20 6f 66 20 74 68 69 73 20 73 74 72 75  end of this stru
243d0 63 74 75 72 65 20 6f 6e 6c 79 2e 20 20 49 66 20  cture only.  If 
243d1 79 6f 75 20 69 6e 73 65 72 74 20 6e 65 77 0a 2a  you insert new.*
243d2 2a 20 69 6e 74 65 72 66 61 63 65 73 20 69 6e 20  * interfaces in 
243d3 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68  the middle of th
243d4 69 73 20 73 74 72 75 63 74 75 72 65 2c 20 74 68  is structure, th
243d5 65 6e 20 6f 6c 64 65 72 20 64 69 66 66 65 72 65  en older differe
243d6 6e 74 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f  nt.** versions o
243d7 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f  f SQLite will no
243d8 74 20 62 65 20 61 62 6c 65 20 74 6f 20 6c 6f 61  t be able to loa
243d9 64 20 65 61 63 68 20 6f 74 68 65 72 73 27 20 73  d each others' s
243da 68 61 72 65 64 0a 2a 2a 20 6c 69 62 72 61 72 69  hared.** librari
243db 65 73 21 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  es!.*/.struct sq
243dc 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
243dd 65 73 20 7b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  es {.  void * (*
243de 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
243df 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
243e0 78 74 2a 2c 69 6e 74 20 6e 42 79 74 65 73 29 3b  xt*,int nBytes);
243e1 0a 20 20 69 6e 74 20 20 28 2a 61 67 67 72 65 67  .  int  (*aggreg
243e2 61 74 65 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74  ate_count)(sqlit
243e3 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20  e3_context*);.  
243e4 69 6e 74 20 20 28 2a 62 69 6e 64 5f 62 6c 6f 62  int  (*bind_blob
243e5 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
243e6 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
243e7 69 6e 74 20 6e 2c 76 6f 69 64 28 2a 29 28 76 6f  int n,void(*)(vo
243e8 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  id*));.  int  (*
243e9 62 69 6e 64 5f 64 6f 75 62 6c 65 29 28 73 71 6c  bind_double)(sql
243ea 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 64  ite3_stmt*,int,d
243eb 6f 75 62 6c 65 29 3b 0a 20 20 69 6e 74 20 20 28  ouble);.  int  (
243ec 2a 62 69 6e 64 5f 69 6e 74 29 28 73 71 6c 69 74  *bind_int)(sqlit
243ed 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74  e3_stmt*,int,int
243ee 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
243ef 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f  _int64)(sqlite3_
243f0 73 74 6d 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  stmt*,int,sqlite
243f1 5f 69 6e 74 36 34 29 3b 0a 20 20 69 6e 74 20 20  _int64);.  int  
243f2 28 2a 62 69 6e 64 5f 6e 75 6c 6c 29 28 73 71 6c  (*bind_null)(sql
243f3 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
243f4 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 70  .  int  (*bind_p
243f5 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 29 28  arameter_count)(
243f6 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
243f7 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61    int  (*bind_pa
243f8 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 29 28 73  rameter_index)(s
243f9 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 63 6f 6e  qlite3_stmt*,con
243fa 73 74 20 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a  st char*zName);.
243fb 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28    const char * (
243fc 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  *bind_parameter_
243fd 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74  name)(sqlite3_st
243fe 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  mt*,int);.  int 
243ff 20 28 2a 62 69 6e 64 5f 74 65 78 74 29 28 73 71   (*bind_text)(sq
24400 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
24401 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 20  const char*,int 
24402 6e 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  n,void(*)(void*)
24403 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
24404 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74 65 33  _text16)(sqlite3
24405 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74  _stmt*,int,const
24406 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28   void*,int,void(
24407 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e  *)(void*));.  in
24408 74 20 20 28 2a 62 69 6e 64 5f 76 61 6c 75 65 29  t  (*bind_value)
24409 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2440a 6e 74 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  nt,const sqlite3
2440b 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
2440c 20 28 2a 62 75 73 79 5f 68 61 6e 64 6c 65 72 29   (*busy_handler)
2440d 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29  (sqlite3*,int(*)
2440e 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 76 6f 69 64  (void*,int),void
2440f 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 75 73  *);.  int  (*bus
24410 79 5f 74 69 6d 65 6f 75 74 29 28 73 71 6c 69 74  y_timeout)(sqlit
24411 65 33 2a 2c 69 6e 74 20 6d 73 29 3b 0a 20 20 69  e3*,int ms);.  i
24412 6e 74 20 20 28 2a 63 68 61 6e 67 65 73 29 28 73  nt  (*changes)(s
24413 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20  qlite3*);.  int 
24414 20 28 2a 63 6c 6f 73 65 29 28 73 71 6c 69 74 65   (*close)(sqlite
24415 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  3*);.  int  (*co
24416 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 29 28  llation_needed)(
24417 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 76  sqlite3*,void*,v
24418 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c  oid(*)(void*,sql
24419 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52  ite3*,int eTextR
2441a 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 29  ep,const char*))
2441b 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 6c 61  ;.  int  (*colla
2441c 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 29 28 73  tion_needed16)(s
2441d 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f  qlite3*,void*,vo
2441e 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69  id(*)(void*,sqli
2441f 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65  te3*,int eTextRe
24420 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b  p,const void*));
24421 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
24422 28 2a 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 29 28 73  (*column_blob)(s
24423 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
24424 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28   iCol);.  int  (
24425 2a 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 29 28 73  *column_bytes)(s
24426 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
24427 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28   iCol);.  int  (
24428 2a 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 29  *column_bytes16)
24429 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2442a 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20  nt iCol);.  int 
2442b 20 28 2a 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 29   (*column_count)
2442c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53  (sqlite3_stmt*pS
2442d 74 6d 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  tmt);.  const ch
2442e 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61  ar * (*column_da
2442f 74 61 62 61 73 65 5f 6e 61 6d 65 29 28 73 71 6c  tabase_name)(sql
24430 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
24431 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
24432 28 2a 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  (*column_databas
24433 65 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65  e_name16)(sqlite
24434 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
24435 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63  const char * (*c
24436 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 29 28  olumn_decltype)(
24437 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
24438 74 20 69 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  t i);.  const vo
24439 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65  id * (*column_de
2443a 63 6c 74 79 70 65 31 36 29 28 73 71 6c 69 74 65  cltype16)(sqlite
2443b 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
2443c 64 6f 75 62 6c 65 20 20 28 2a 63 6f 6c 75 6d 6e  double  (*column
2443d 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74 65 33  _double)(sqlite3
2443e 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
2443f 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d  ;.  int  (*colum
24440 6e 5f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 73  n_int)(sqlite3_s
24441 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
24442 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20    sqlite_int64  
24443 28 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 29 28  (*column_int64)(
24444 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
24445 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73 74  t iCol);.  const
24446 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e   char * (*column
24447 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73  _name)(sqlite3_s
24448 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e  tmt*,int);.  con
24449 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
2444a 6d 6e 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74  mn_name16)(sqlit
2444b 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
2444c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a   const char * (*
2444d 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
2444e 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  me)(sqlite3_stmt
2444f 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
24450 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  void * (*column_
24451 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 29 28 73  origin_name16)(s
24452 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
24453 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
24454 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  * (*column_table
24455 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73  _name)(sqlite3_s
24456 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e  tmt*,int);.  con
24457 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
24458 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 29  mn_table_name16)
24459 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2445a 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  nt);.  const uns
2445b 69 67 6e 65 64 20 63 68 61 72 20 2a 20 28 2a 63  igned char * (*c
2445c 6f 6c 75 6d 6e 5f 74 65 78 74 29 28 73 71 6c 69  olumn_text)(sqli
2445d 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
2445e 6f 6c 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69  ol);.  const voi
2445f 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78  d * (*column_tex
24460 74 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  t16)(sqlite3_stm
24461 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
24462 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 74 79  int  (*column_ty
24463 70 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  pe)(sqlite3_stmt
24464 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 73  *,int iCol);.  s
24465 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 28 2a  qlite3_value* (*
24466 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 29 28 73 71  column_value)(sq
24467 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
24468 69 43 6f 6c 29 3b 0a 20 20 76 6f 69 64 20 2a 20  iCol);.  void * 
24469 28 2a 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 29 28 73  (*commit_hook)(s
2446a 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76  qlite3*,int(*)(v
2446b 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  oid*),void*);.  
2446c 69 6e 74 20 20 28 2a 63 6f 6d 70 6c 65 74 65 29  int  (*complete)
2446d 28 63 6f 6e 73 74 20 63 68 61 72 2a 73 71 6c 29  (const char*sql)
2446e 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70 6c  ;.  int  (*compl
2446f 65 74 65 31 36 29 28 63 6f 6e 73 74 20 76 6f 69  ete16)(const voi
24470 64 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28  d*sql);.  int  (
24471 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  *create_collatio
24472 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  n)(sqlite3*,cons
24473 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64  t char*,int,void
24474 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69  *,int(*)(void*,i
24475 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
24476 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29  nt,const void*))
24477 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74  ;.  int  (*creat
24478 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 29 28 73  e_collation16)(s
24479 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f  qlite3*,const vo
2447a 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e  id*,int,void*,in
2447b 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  t(*)(void*,int,c
2447c 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
2447d 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20  onst void*));.  
2447e 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 66 75  int  (*create_fu
2447f 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a  nction)(sqlite3*
24480 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
24481 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20  ,int,void*,void 
24482 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33  (*xFunc)(sqlite3
24483 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
24484 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76  lite3_value**),v
24485 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
24486 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
24487 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
24488 2a 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  *),void (*xFinal
24489 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
2448a 74 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  t*));.  int  (*c
2448b 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
2448c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
2448d 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 76   void*,int,int,v
2448e 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78 46 75 6e  oid*,void (*xFun
2448f 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
24490 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
24491 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a  value**),void (*
24492 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
24493 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
24494 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69  te3_value**),voi
24495 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69  d (*xFinal)(sqli
24496 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a  te3_context*));.
24497 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65 5f 6d    int (*create_m
24498 6f 64 75 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c  odule)(sqlite3*,
24499 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
2449a 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
2449b 2a 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20  *,void*);.  int 
2449c 20 28 2a 64 61 74 61 5f 63 6f 75 6e 74 29 28 73   (*data_count)(s
2449d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d  qlite3_stmt*pStm
2449e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 20  t);.  sqlite3 * 
2449f 28 2a 64 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c  (*db_handle)(sql
244a0 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69  ite3_stmt*);.  i
244a1 6e 74 20 28 2a 64 65 63 6c 61 72 65 5f 76 74 61  nt (*declare_vta
244a2 62 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  b)(sqlite3*,cons
244a3 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20  t char*);.  int 
244a4 20 28 2a 65 6e 61 62 6c 65 5f 73 68 61 72 65 64   (*enable_shared
244a5 5f 63 61 63 68 65 29 28 69 6e 74 29 3b 0a 20 20  _cache)(int);.  
244a6 69 6e 74 20 20 28 2a 65 72 72 63 6f 64 65 29 28  int  (*errcode)(
244a7 73 71 6c 69 74 65 33 2a 64 62 29 3b 0a 20 20 63  sqlite3*db);.  c
244a8 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 65 72  onst char * (*er
244a9 72 6d 73 67 29 28 73 71 6c 69 74 65 33 2a 29 3b  rmsg)(sqlite3*);
244aa 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
244ab 28 2a 65 72 72 6d 73 67 31 36 29 28 73 71 6c 69  (*errmsg16)(sqli
244ac 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  te3*);.  int  (*
244ad 65 78 65 63 29 28 73 71 6c 69 74 65 33 2a 2c 63  exec)(sqlite3*,c
244ae 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74  onst char*,sqlit
244af 65 33 5f 63 61 6c 6c 62 61 63 6b 2c 76 6f 69 64  e3_callback,void
244b0 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74  *,char**);.  int
244b1 20 20 28 2a 65 78 70 69 72 65 64 29 28 73 71 6c    (*expired)(sql
244b2 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69  ite3_stmt*);.  i
244b3 6e 74 20 20 28 2a 66 69 6e 61 6c 69 7a 65 29 28  nt  (*finalize)(
244b4 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74  sqlite3_stmt*pSt
244b5 6d 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66  mt);.  void  (*f
244b6 72 65 65 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76  ree)(void*);.  v
244b7 6f 69 64 20 20 28 2a 66 72 65 65 5f 74 61 62 6c  oid  (*free_tabl
244b8 65 29 28 63 68 61 72 2a 2a 72 65 73 75 6c 74 29  e)(char**result)
244b9 3b 0a 20 20 69 6e 74 20 20 28 2a 67 65 74 5f 61  ;.  int  (*get_a
244ba 75 74 6f 63 6f 6d 6d 69 74 29 28 73 71 6c 69 74  utocommit)(sqlit
244bb 65 33 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28  e3*);.  void * (
244bc 2a 67 65 74 5f 61 75 78 64 61 74 61 29 28 73 71  *get_auxdata)(sq
244bd 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
244be 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 67 65  nt);.  int  (*ge
244bf 74 5f 74 61 62 6c 65 29 28 73 71 6c 69 74 65 33  t_table)(sqlite3
244c0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68  *,const char*,ch
244c1 61 72 2a 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c  ar***,int*,int*,
244c2 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20  char**);.  int  
244c3 28 2a 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72  (*global_recover
244c4 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20  )(void);.  void 
244c5 20 28 2a 69 6e 74 65 72 72 75 70 74 78 29 28 73   (*interruptx)(s
244c6 71 6c 69 74 65 33 2a 29 3b 0a 20 20 73 71 6c 69  qlite3*);.  sqli
244c7 74 65 5f 69 6e 74 36 34 20 20 28 2a 6c 61 73 74  te_int64  (*last
244c8 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 29 28 73  _insert_rowid)(s
244c9 71 6c 69 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73  qlite3*);.  cons
244ca 74 20 63 68 61 72 20 2a 20 28 2a 6c 69 62 76 65  t char * (*libve
244cb 72 73 69 6f 6e 29 28 76 6f 69 64 29 3b 0a 20 20  rsion)(void);.  
244cc 69 6e 74 20 20 28 2a 6c 69 62 76 65 72 73 69 6f  int  (*libversio
244cd 6e 5f 6e 75 6d 62 65 72 29 28 76 6f 69 64 29 3b  n_number)(void);
244ce 0a 20 20 76 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f  .  void *(*mallo
244cf 63 29 28 69 6e 74 29 3b 0a 20 20 63 68 61 72 20  c)(int);.  char 
244d0 2a 20 28 2a 6d 70 72 69 6e 74 66 29 28 63 6f 6e  * (*mprintf)(con
244d1 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20  st char*,...);. 
244d2 20 69 6e 74 20 20 28 2a 6f 70 65 6e 29 28 63 6f   int  (*open)(co
244d3 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65  nst char*,sqlite
244d4 33 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f  3**);.  int  (*o
244d5 70 65 6e 31 36 29 28 63 6f 6e 73 74 20 76 6f 69  pen16)(const voi
244d6 64 2a 2c 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20  d*,sqlite3**);. 
244d7 20 69 6e 74 20 20 28 2a 70 72 65 70 61 72 65 29   int  (*prepare)
244d8 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
244d9 63 68 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  char*,int,sqlite
244da 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63  3_stmt**,const c
244db 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28  har**);.  int  (
244dc 2a 70 72 65 70 61 72 65 31 36 29 28 73 71 6c 69  *prepare16)(sqli
244dd 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  te3*,const void*
244de 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d  ,int,sqlite3_stm
244df 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a  t**,const void**
244e0 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 70 72  );.  void * (*pr
244e1 6f 66 69 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c  ofile)(sqlite3*,
244e2 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 63 6f  void(*)(void*,co
244e3 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65  nst char*,sqlite
244e4 5f 75 69 6e 74 36 34 29 2c 76 6f 69 64 2a 29 3b  _uint64),void*);
244e5 0a 20 20 76 6f 69 64 20 20 28 2a 70 72 6f 67 72  .  void  (*progr
244e6 65 73 73 5f 68 61 6e 64 6c 65 72 29 28 73 71 6c  ess_handler)(sql
244e7 69 74 65 33 2a 2c 69 6e 74 2c 69 6e 74 28 2a 29  ite3*,int,int(*)
244e8 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a  (void*),void*);.
244e9 20 20 76 6f 69 64 20 2a 28 2a 72 65 61 6c 6c 6f    void *(*reallo
244ea 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20  c)(void*,int);. 
244eb 20 69 6e 74 20 20 28 2a 72 65 73 65 74 29 28 73   int  (*reset)(s
244ec 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d  qlite3_stmt*pStm
244ed 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65  t);.  void  (*re
244ee 73 75 6c 74 5f 62 6c 6f 62 29 28 73 71 6c 69 74  sult_blob)(sqlit
244ef 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
244f0 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64  t void*,int,void
244f1 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76  (*)(void*));.  v
244f2 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 64 6f  oid  (*result_do
244f3 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 63 6f  uble)(sqlite3_co
244f4 6e 74 65 78 74 2a 2c 64 6f 75 62 6c 65 29 3b 0a  ntext*,double);.
244f5 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
244f6 5f 65 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f  _error)(sqlite3_
244f7 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 63  context*,const c
244f8 68 61 72 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69  har*,int);.  voi
244f9 64 20 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f  d  (*result_erro
244fa 72 31 36 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  r16)(sqlite3_con
244fb 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  text*,const void
244fc 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20  *,int);.  void  
244fd 28 2a 72 65 73 75 6c 74 5f 69 6e 74 29 28 73 71  (*result_int)(sq
244fe 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
244ff 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  nt);.  void  (*r
24500 65 73 75 6c 74 5f 69 6e 74 36 34 29 28 73 71 6c  esult_int64)(sql
24501 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71  ite3_context*,sq
24502 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76  lite_int64);.  v
24503 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 6e 75  oid  (*result_nu
24504 6c 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ll)(sqlite3_cont
24505 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28  ext*);.  void  (
24506 2a 72 65 73 75 6c 74 5f 74 65 78 74 29 28 73 71  *result_text)(sq
24507 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
24508 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76  onst char*,int,v
24509 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
2450a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
2450b 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74 65 33  _text16)(sqlite3
2450c 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20  _context*,const 
2450d 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a  void*,int,void(*
2450e 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69  )(void*));.  voi
2450f 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74  d  (*result_text
24510 31 36 62 65 29 28 73 71 6c 69 74 65 33 5f 63 6f  16be)(sqlite3_co
24511 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69  ntext*,const voi
24512 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76  d*,int,void(*)(v
24513 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20  oid*));.  void  
24514 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  (*result_text16l
24515 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
24516 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  xt*,const void*,
24517 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
24518 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  *));.  void  (*r
24519 65 73 75 6c 74 5f 76 61 6c 75 65 29 28 73 71 6c  esult_value)(sql
2451a 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71  ite3_context*,sq
2451b 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
2451c 20 76 6f 69 64 20 2a 20 28 2a 72 6f 6c 6c 62 61   void * (*rollba
2451d 63 6b 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33  ck_hook)(sqlite3
2451e 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  *,void(*)(void*)
2451f 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20  ,void*);.  int  
24520 28 2a 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  (*set_authorizer
24521 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a  )(sqlite3*,int(*
24522 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
24523 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
24524 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
24525 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 76 6f 69  const char*),voi
24526 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 73  d*);.  void  (*s
24527 65 74 5f 61 75 78 64 61 74 61 29 28 73 71 6c 69  et_auxdata)(sqli
24528 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
24529 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 29 28  ,void*,void (*)(
2452a 76 6f 69 64 2a 29 29 3b 0a 20 20 63 68 61 72 20  void*));.  char 
2452b 2a 20 28 2a 73 6e 70 72 69 6e 74 66 29 28 69 6e  * (*snprintf)(in
2452c 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t,char*,const ch
2452d 61 72 2a 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20  ar*,...);.  int 
2452e 20 28 2a 73 74 65 70 29 28 73 71 6c 69 74 65 33   (*step)(sqlite3
2452f 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20  _stmt*);.  int  
24530 28 2a 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d  (*table_column_m
24531 65 74 61 64 61 74 61 29 28 73 71 6c 69 74 65 33  etadata)(sqlite3
24532 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
24533 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
24534 63 68 61 72 2a 2c 63 68 61 72 20 63 6f 6e 73 74  char*,char const
24535 2a 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a 2c  **,char const**,
24536 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b  int*,int*,int*);
24537 0a 20 20 76 6f 69 64 20 20 28 2a 74 68 72 65 61  .  void  (*threa
24538 64 5f 63 6c 65 61 6e 75 70 29 28 76 6f 69 64 29  d_cleanup)(void)
24539 3b 0a 20 20 69 6e 74 20 20 28 2a 74 6f 74 61 6c  ;.  int  (*total
2453a 5f 63 68 61 6e 67 65 73 29 28 73 71 6c 69 74 65  _changes)(sqlite
2453b 33 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  3*);.  void * (*
2453c 74 72 61 63 65 29 28 73 71 6c 69 74 65 33 2a 2c  trace)(sqlite3*,
2453d 76 6f 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f  void(*xTrace)(vo
2453e 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  id*,const char*)
2453f 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20  ,void*);.  int  
24540 28 2a 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69  (*transfer_bindi
24541 6e 67 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ngs)(sqlite3_stm
24542 74 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t*,sqlite3_stmt*
24543 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 75 70  );.  void * (*up
24544 64 61 74 65 5f 68 6f 6f 6b 29 28 73 71 6c 69 74  date_hook)(sqlit
24545 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  e3*,void(*)(void
24546 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73  *,int ,char cons
24547 74 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2c 73  t*,char const*,s
24548 71 6c 69 74 65 5f 69 6e 74 36 34 29 2c 76 6f 69  qlite_int64),voi
24549 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  d*);.  void * (*
2454a 75 73 65 72 5f 64 61 74 61 29 28 73 71 6c 69 74  user_data)(sqlit
2454b 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20  e3_context*);.  
2454c 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76  const void * (*v
2454d 61 6c 75 65 5f 62 6c 6f 62 29 28 73 71 6c 69 74  alue_blob)(sqlit
2454e 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e  e3_value*);.  in
2454f 74 20 20 28 2a 76 61 6c 75 65 5f 62 79 74 65 73  t  (*value_bytes
24550 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
24551 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75  );.  int  (*valu
24552 65 5f 62 79 74 65 73 31 36 29 28 73 71 6c 69 74  e_bytes16)(sqlit
24553 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 64 6f  e3_value*);.  do
24554 75 62 6c 65 20 20 28 2a 76 61 6c 75 65 5f 64 6f  uble  (*value_do
24555 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 76 61  uble)(sqlite3_va
24556 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  lue*);.  int  (*
24557 76 61 6c 75 65 5f 69 6e 74 29 28 73 71 6c 69 74  value_int)(sqlit
24558 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 73 71  e3_value*);.  sq
24559 6c 69 74 65 5f 69 6e 74 36 34 20 20 28 2a 76 61  lite_int64  (*va
2455a 6c 75 65 5f 69 6e 74 36 34 29 28 73 71 6c 69 74  lue_int64)(sqlit
2455b 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e  e3_value*);.  in
2455c 74 20 20 28 2a 76 61 6c 75 65 5f 6e 75 6d 65 72  t  (*value_numer
2455d 69 63 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33  ic_type)(sqlite3
2455e 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73  _value*);.  cons
2455f 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
24560 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74 29 28  * (*value_text)(
24561 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
24562 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
24563 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36 29 28  (*value_text16)(
24564 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
24565 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
24566 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65  (*value_text16be
24567 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
24568 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
24569 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36  * (*value_text16
2456a 6c 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  le)(sqlite3_valu
2456b 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61  e*);.  int  (*va
2456c 6c 75 65 5f 74 79 70 65 29 28 73 71 6c 69 74 65  lue_type)(sqlite
2456d 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 68 61  3_value*);.  cha
2456e 72 20 2a 28 2a 76 6d 70 72 69 6e 74 66 29 28 63  r *(*vmprintf)(c
2456f 6f 6e 73 74 20 63 68 61 72 2a 2c 76 61 5f 6c 69  onst char*,va_li
24570 73 74 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20  st);.  /* Added 
24571 3f 3f 3f 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 6f  ??? */.  int (*o
24572 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
24573 29 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73  )(sqlite3*, cons
24574 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d  t char *zFuncNam
24575 65 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 20 20  e, int nArg);.  
24576 2f 2a 20 41 64 64 65 64 20 62 79 20 33 2e 33 2e  /* Added by 3.3.
24577 31 33 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 70 72  13 */.  int (*pr
24578 65 70 61 72 65 5f 76 32 29 28 73 71 6c 69 74 65  epare_v2)(sqlite
24579 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
2457a 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt,sqlite3_stmt*
2457b 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b  *,const char**);
2457c 0a 20 20 69 6e 74 20 28 2a 70 72 65 70 61 72 65  .  int (*prepare
2457d 31 36 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c  16_v2)(sqlite3*,
2457e 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
2457f 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63  sqlite3_stmt**,c
24580 6f 6e 73 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20  onst void**);.  
24581 69 6e 74 20 28 2a 63 6c 65 61 72 5f 62 69 6e 64  int (*clear_bind
24582 69 6e 67 73 29 28 73 71 6c 69 74 65 33 5f 73 74  ings)(sqlite3_st
24583 6d 74 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64  mt*);.  /* Added
24584 20 62 79 20 33 2e 34 2e 31 20 2a 2f 0a 20 20 69   by 3.4.1 */.  i
24585 6e 74 20 28 2a 63 72 65 61 74 65 5f 6d 6f 64 75  nt (*create_modu
24586 6c 65 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c  le_v2)(sqlite3*,
24587 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
24588 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
24589 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78  *,void*,void (*x
2458a 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29  Destroy)(void *)
2458b 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79  );.  /* Added by
2458c 20 33 2e 35 2e 30 20 2a 2f 0a 20 20 69 6e 74 20   3.5.0 */.  int 
2458d 28 2a 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 29  (*bind_zeroblob)
2458e 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2458f 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  nt,int);.  int (
24590 2a 62 6c 6f 62 5f 62 79 74 65 73 29 28 73 71 6c  *blob_bytes)(sql
24591 69 74 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20 20 69  ite3_blob*);.  i
24592 6e 74 20 28 2a 62 6c 6f 62 5f 63 6c 6f 73 65 29  nt (*blob_close)
24593 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29 3b  (sqlite3_blob*);
24594 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 6f 70  .  int (*blob_op
24595 65 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  en)(sqlite3*,con
24596 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
24597 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
24598 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69  ,sqlite3_int64,i
24599 6e 74 2c 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a  nt,sqlite3_blob*
2459a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62  *);.  int (*blob
2459b 5f 72 65 61 64 29 28 73 71 6c 69 74 65 33 5f 62  _read)(sqlite3_b
2459c 6c 6f 62 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69  lob*,void*,int,i
2459d 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f  nt);.  int (*blo
2459e 62 5f 77 72 69 74 65 29 28 73 71 6c 69 74 65 33  b_write)(sqlite3
2459f 5f 62 6c 6f 62 2a 2c 63 6f 6e 73 74 20 76 6f 69  _blob*,const voi
245a0 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69  d*,int,int);.  i
245a1 6e 74 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c  nt (*create_coll
245a2 61 74 69 6f 6e 5f 76 32 29 28 73 71 6c 69 74 65  ation_v2)(sqlite
245a3 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
245a4 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28  nt,void*,int(*)(
245a5 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
245a6 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
245a7 76 6f 69 64 2a 29 2c 76 6f 69 64 28 2a 29 28 76  void*),void(*)(v
245a8 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a  oid*));.  int (*
245a9 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 29 28 73 71  file_control)(sq
245aa 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
245ab 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 20  r*,int,void*);. 
245ac 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28   sqlite3_int64 (
245ad 2a 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65  *memory_highwate
245ae 72 29 28 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74  r)(int);.  sqlit
245af 65 33 5f 69 6e 74 36 34 20 28 2a 6d 65 6d 6f 72  e3_int64 (*memor
245b0 79 5f 75 73 65 64 29 28 76 6f 69 64 29 3b 0a 20  y_used)(void);. 
245b1 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
245b2 28 2a 6d 75 74 65 78 5f 61 6c 6c 6f 63 29 28 69  (*mutex_alloc)(i
245b3 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75  nt);.  void (*mu
245b4 74 65 78 5f 65 6e 74 65 72 29 28 73 71 6c 69 74  tex_enter)(sqlit
245b5 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 76 6f  e3_mutex*);.  vo
245b6 69 64 20 28 2a 6d 75 74 65 78 5f 66 72 65 65 29  id (*mutex_free)
245b7 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
245b8 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78  ;.  void (*mutex
245b9 5f 6c 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f  _leave)(sqlite3_
245ba 6d 75 74 65 78 2a 29 3b 0a 20 20 69 6e 74 20 28  mutex*);.  int (
245bb 2a 6d 75 74 65 78 5f 74 72 79 29 28 73 71 6c 69  *mutex_try)(sqli
245bc 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 69  te3_mutex*);.  i
245bd 6e 74 20 28 2a 6f 70 65 6e 5f 76 32 29 28 63 6f  nt (*open_v2)(co
245be 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65  nst char*,sqlite
245bf 33 2a 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  3**,int,const ch
245c0 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 72 65  ar*);.  int (*re
245c1 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 29 28 69 6e  lease_memory)(in
245c2 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73  t);.  void (*res
245c3 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 29  ult_error_nomem)
245c4 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
245c5 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73  *);.  void (*res
245c6 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
245c7 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
245c8 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 73 6c 65  t*);.  int (*sle
245c9 65 70 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64  ep)(int);.  void
245ca 20 28 2a 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d   (*soft_heap_lim
245cb 69 74 29 28 69 6e 74 29 3b 0a 20 20 73 71 6c 69  it)(int);.  sqli
245cc 74 65 33 5f 76 66 73 20 2a 28 2a 76 66 73 5f 66  te3_vfs *(*vfs_f
245cd 69 6e 64 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  ind)(const char*
245ce 29 3b 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 72  );.  int (*vfs_r
245cf 65 67 69 73 74 65 72 29 28 73 71 6c 69 74 65 33  egister)(sqlite3
245d0 5f 76 66 73 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e  _vfs*,int);.  in
245d1 74 20 28 2a 76 66 73 5f 75 6e 72 65 67 69 73 74  t (*vfs_unregist
245d2 65 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  er)(sqlite3_vfs*
245d3 29 3b 0a 20 20 69 6e 74 20 28 2a 78 74 68 72 65  );.  int (*xthre
245d4 61 64 73 61 66 65 29 28 76 6f 69 64 29 3b 0a 20  adsafe)(void);. 
245d5 20 76 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 7a   void (*result_z
245d6 65 72 6f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  eroblob)(sqlite3
245d7 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a  _context*,int);.
245d8 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f    void (*result_
245d9 65 72 72 6f 72 5f 63 6f 64 65 29 28 73 71 6c 69  error_code)(sqli
245da 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
245db 29 3b 0a 20 20 69 6e 74 20 28 2a 74 65 73 74 5f  );.  int (*test_
245dc 63 6f 6e 74 72 6f 6c 29 28 69 6e 74 2c 20 2e 2e  control)(int, ..
245dd 2e 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 61 6e  .);.  void (*ran
245de 64 6f 6d 6e 65 73 73 29 28 69 6e 74 2c 76 6f 69  domness)(int,voi
245df 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  d*);.  sqlite3 *
245e0 28 2a 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  (*context_db_han
245e1 64 6c 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  dle)(sqlite3_con
245e2 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  text*);.  int (*
245e3 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f  extended_result_
245e4 63 6f 64 65 73 29 28 73 71 6c 69 74 65 33 2a 2c  codes)(sqlite3*,
245e5 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 6c 69  int);.  int (*li
245e6 6d 69 74 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  mit)(sqlite3*,in
245e7 74 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65  t,int);.  sqlite
245e8 33 5f 73 74 6d 74 20 2a 28 2a 6e 65 78 74 5f 73  3_stmt *(*next_s
245e9 74 6d 74 29 28 73 71 6c 69 74 65 33 2a 2c 73 71  tmt)(sqlite3*,sq
245ea 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20  lite3_stmt*);.  
245eb 63 6f 6e 73 74 20 63 68 61 72 20 2a 28 2a 73 71  const char *(*sq
245ec 6c 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  l)(sqlite3_stmt*
245ed 29 3b 0a 20 20 69 6e 74 20 28 2a 73 74 61 74 75  );.  int (*statu
245ee 73 29 28 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a  s)(int,int*,int*
245ef 2c 69 6e 74 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ,int);.};../*.**
245f0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   The following m
245f1 61 63 72 6f 73 20 72 65 64 65 66 69 6e 65 20 74  acros redefine t
245f2 68 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20  he API routines 
245f3 73 6f 20 74 68 61 74 20 74 68 65 79 20 61 72 65  so that they are
245f4 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64 20 74  .** redirected t
245f5 68 72 6f 75 67 68 74 20 74 68 65 20 67 6c 6f 62  hrought the glob
245f6 61 6c 20 73 71 6c 69 74 65 33 5f 61 70 69 20 73  al sqlite3_api s
245f7 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
245f8 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
245f9 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 62 79   is also used by
245fa 20 74 68 65 20 6c 6f 61 64 65 78 74 2e 63 20 73   the loadext.c s
245fb 6f 75 72 63 65 20 66 69 6c 65 0a 2a 2a 20 28 70  ource file.** (p
245fc 61 72 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  art of the main 
245fd 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 2d  SQLite library -
245fe 20 6e 6f 74 20 61 6e 20 65 78 74 65 6e 73 69 6f   not an extensio
245ff 6e 29 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74  n) so that.** it
24600 20 63 61 6e 20 67 65 74 20 61 63 63 65 73 73 20   can get access 
24601 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 61  to the sqlite3_a
24602 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 74 72 75  pi_routines stru
24603 63 74 75 72 65 0a 2a 2a 20 64 65 66 69 6e 69 74  cture.** definit
24604 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20 6d 61  ion.  But the ma
24605 69 6e 20 6c 69 62 72 61 72 79 20 64 6f 65 73 20  in library does 
24606 6e 6f 74 20 77 61 6e 74 20 74 6f 20 72 65 64 65  not want to rede
24607 66 69 6e 65 0a 2a 2a 20 74 68 65 20 41 50 49 2e  fine.** the API.
24608 20 20 53 6f 20 74 68 65 20 72 65 64 65 66 69 6e    So the redefin
24609 69 74 69 6f 6e 20 6d 61 63 72 6f 73 20 61 72 65  ition macros are
2460a 20 6f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 74   only valid if t
2460b 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52  he.** SQLITE_COR
2460c 45 20 6d 61 63 72 6f 73 20 69 73 20 75 6e 64 65  E macros is unde
2460d 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  fined..*/.#ifnde
2460e 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 23 64  f SQLITE_CORE.#d
2460f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 61 67  efine sqlite3_ag
24610 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 20  gregate_context 
24611 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
24612 2d 3e 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  ->aggregate_cont
24613 65 78 74 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ext.#ifndef SQLI
24614 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
24615 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ED.#define sqlit
24616 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75  e3_aggregate_cou
24617 6e 74 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  nt        sqlite
24618 33 5f 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65  3_api->aggregate
24619 5f 63 6f 75 6e 74 0a 23 65 6e 64 69 66 0a 23 64  _count.#endif.#d
2461a 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
2461b 6e 64 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20  nd_blob         
2461c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2461d 2d 3e 62 69 6e 64 5f 62 6c 6f 62 0a 23 64 65 66  ->bind_blob.#def
2461e 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
2461f 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20  _double         
24620 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
24621 62 69 6e 64 5f 64 6f 75 62 6c 65 0a 23 64 65 66  bind_double.#def
24622 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
24623 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  _int            
24624 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
24625 62 69 6e 64 5f 69 6e 74 0a 23 64 65 66 69 6e 65  bind_int.#define
24626 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
24627 74 36 34 20 20 20 20 20 20 20 20 20 20 20 20 20  t64             
24628 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e  sqlite3_api->bin
24629 64 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20  d_int64.#define 
2462a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
2462b 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  l              s
2462c 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64  qlite3_api->bind
2462d 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71  _null.#define sq
2462e 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
2462f 65 74 65 72 5f 63 6f 75 6e 74 20 20 20 73 71 6c  eter_count   sql
24630 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70  ite3_api->bind_p
24631 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 0a 23  arameter_count.#
24632 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
24633 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
24634 64 65 78 20 20 20 73 71 6c 69 74 65 33 5f 61 70  dex   sqlite3_ap
24635 69 2d 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  i->bind_paramete
24636 72 5f 69 6e 64 65 78 0a 23 64 65 66 69 6e 65 20  r_index.#define 
24637 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
24638 61 6d 65 74 65 72 5f 6e 61 6d 65 20 20 20 20 73  ameter_name    s
24639 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64  qlite3_api->bind
2463a 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 0a  _parameter_name.
2463b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2463c 62 69 6e 64 5f 74 65 78 74 20 20 20 20 20 20 20  bind_text       
2463d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2463e 70 69 2d 3e 62 69 6e 64 5f 74 65 78 74 0a 23 64  pi->bind_text.#d
2463f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
24640 6e 64 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  nd_text16       
24641 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
24642 2d 3e 62 69 6e 64 5f 74 65 78 74 31 36 0a 23 64  ->bind_text16.#d
24643 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
24644 6e 64 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20  nd_value        
24645 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
24646 2d 3e 62 69 6e 64 5f 76 61 6c 75 65 0a 23 64 65  ->bind_value.#de
24647 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73  fine sqlite3_bus
24648 79 5f 68 61 6e 64 6c 65 72 20 20 20 20 20 20 20  y_handler       
24649 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2464a 3e 62 75 73 79 5f 68 61 6e 64 6c 65 72 0a 23 64  >busy_handler.#d
2464b 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 75  efine sqlite3_bu
2464c 73 79 5f 74 69 6d 65 6f 75 74 20 20 20 20 20 20  sy_timeout      
2464d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2464e 2d 3e 62 75 73 79 5f 74 69 6d 65 6f 75 74 0a 23  ->busy_timeout.#
2464f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
24650 68 61 6e 67 65 73 20 20 20 20 20 20 20 20 20 20  hanges          
24651 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
24652 69 2d 3e 63 68 61 6e 67 65 73 0a 23 64 65 66 69  i->changes.#defi
24653 6e 65 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  ne sqlite3_close
24654 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24655 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
24656 6c 6f 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  lose.#define sql
24657 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
24658 65 65 64 65 64 20 20 20 20 20 20 20 73 71 6c 69  eeded       sqli
24659 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61 74 69  te3_api->collati
2465a 6f 6e 5f 6e 65 65 64 65 64 0a 23 64 65 66 69 6e  on_needed.#defin
2465b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  e sqlite3_collat
2465c 69 6f 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20  ion_needed16    
2465d 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
2465e 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
2465f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
24660 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 20 20 20 20  _column_blob    
24661 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
24662 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  api->column_blob
24663 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
24664 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 20 20 20  _column_bytes   
24665 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
24666 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65  api->column_byte
24667 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  s.#define sqlite
24668 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
24669 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2466a 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74  _api->column_byt
2466b 65 73 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  es16.#define sql
2466c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
2466d 74 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  t           sqli
2466e 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
2466f 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  count.#define sq
24670 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
24671 61 62 61 73 65 5f 6e 61 6d 65 20 20 20 73 71 6c  abase_name   sql
24672 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
24673 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 0a 23  _database_name.#
24674 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
24675 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
24676 61 6d 65 31 36 20 73 71 6c 69 74 65 33 5f 61 70  ame16 sqlite3_ap
24677 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  i->column_databa
24678 73 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e  se_name16.#defin
24679 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
2467a 5f 64 65 63 6c 74 79 70 65 20 20 20 20 20 20 20  _decltype       
2467b 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
2467c 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 0a 23 64  lumn_decltype.#d
2467d 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
2467e 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 20  lumn_decltype16 
2467f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
24680 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  ->column_decltyp
24681 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  e16.#define sqli
24682 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c  te3_column_doubl
24683 65 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  e          sqlit
24684 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
24685 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  ouble.#define sq
24686 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
24687 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
24688 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
24689 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  _int.#define sql
2468a 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
2468b 34 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  4           sqli
2468c 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
2468d 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71  int64.#define sq
2468e 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
2468f 65 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  e            sql
24690 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
24691 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71  _name.#define sq
24692 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
24693 65 31 36 20 20 20 20 20 20 20 20 20 20 73 71 6c  e16          sql
24694 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
24695 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20  _name16.#define 
24696 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
24697 72 69 67 69 6e 5f 6e 61 6d 65 20 20 20 20 20 73  rigin_name     s
24698 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
24699 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 0a 23  mn_origin_name.#
2469a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
2469b 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
2469c 65 31 36 20 20 20 73 71 6c 69 74 65 33 5f 61 70  e16   sqlite3_ap
2469d 69 2d 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  i->column_origin
2469e 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20  _name16.#define 
2469f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
246a0 61 62 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 73  able_name      s
246a1 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
246a2 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 0a 23 64  mn_table_name.#d
246a3 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
246a4 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31  lumn_table_name1
246a5 36 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  6    sqlite3_api
246a6 2d 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  ->column_table_n
246a7 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ame16.#define sq
246a8 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
246a9 74 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  t            sql
246aa 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
246ab 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71  _text.#define sq
246ac 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
246ad 74 31 36 20 20 20 20 20 20 20 20 20 20 73 71 6c  t16          sql
246ae 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
246af 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20  _text16.#define 
246b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
246b1 79 70 65 20 20 20 20 20 20 20 20 20 20 20 20 73  ype            s
246b2 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
246b3 6d 6e 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20  mn_type.#define 
246b4 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
246b5 61 6c 75 65 20 20 20 20 20 20 20 20 20 20 20 73  alue           s
246b6 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
246b7 6d 6e 5f 76 61 6c 75 65 0a 23 64 65 66 69 6e 65  mn_value.#define
246b8 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f   sqlite3_commit_
246b9 68 6f 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20  hook            
246ba 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d  sqlite3_api->com
246bb 6d 69 74 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65  mit_hook.#define
246bc 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
246bd 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
246be 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d  sqlite3_api->com
246bf 70 6c 65 74 65 0a 23 64 65 66 69 6e 65 20 73 71  plete.#define sq
246c0 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36  lite3_complete16
246c1 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
246c2 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65  ite3_api->comple
246c3 74 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  te16.#define sql
246c4 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
246c5 61 74 69 6f 6e 20 20 20 20 20 20 20 73 71 6c 69  ation       sqli
246c6 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f  te3_api->create_
246c7 63 6f 6c 6c 61 74 69 6f 6e 0a 23 64 65 66 69 6e  collation.#defin
246c8 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
246c9 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20  _collation16    
246ca 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
246cb 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36  eate_collation16
246cc 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
246cd 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
246ce 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
246cf 61 70 69 2d 3e 63 72 65 61 74 65 5f 66 75 6e 63  api->create_func
246d0 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c  tion.#define sql
246d1 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
246d2 74 69 6f 6e 31 36 20 20 20 20 20 20 73 71 6c 69  tion16      sqli
246d3 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f  te3_api->create_
246d4 66 75 6e 63 74 69 6f 6e 31 36 0a 23 64 65 66 69  function16.#defi
246d5 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
246d6 65 5f 6d 6f 64 75 6c 65 20 20 20 20 20 20 20 20  e_module        
246d7 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
246d8 72 65 61 74 65 5f 6d 6f 64 75 6c 65 0a 23 64 65  reate_module.#de
246d9 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
246da 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 20 20 20  ate_module_v2   
246db 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
246dc 3e 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76  >create_module_v
246dd 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  2.#define sqlite
246de 33 5f 64 61 74 61 5f 63 6f 75 6e 74 20 20 20 20  3_data_count    
246df 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
246e0 5f 61 70 69 2d 3e 64 61 74 61 5f 63 6f 75 6e 74  _api->data_count
246e1 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
246e2 5f 64 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20  _db_handle      
246e3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
246e4 61 70 69 2d 3e 64 62 5f 68 61 6e 64 6c 65 0a 23  api->db_handle.#
246e5 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64  define sqlite3_d
246e6 65 63 6c 61 72 65 5f 76 74 61 62 20 20 20 20 20  eclare_vtab     
246e7 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
246e8 69 2d 3e 64 65 63 6c 61 72 65 5f 76 74 61 62 0a  i->declare_vtab.
246e9 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
246ea 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61  enable_shared_ca
246eb 63 68 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61  che    sqlite3_a
246ec 70 69 2d 3e 65 6e 61 62 6c 65 5f 73 68 61 72 65  pi->enable_share
246ed 64 5f 63 61 63 68 65 0a 23 64 65 66 69 6e 65 20  d_cache.#define 
246ee 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 20  sqlite3_errcode 
246ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
246f0 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 63  qlite3_api->errc
246f1 6f 64 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ode.#define sqli
246f2 74 65 33 5f 65 72 72 6d 73 67 20 20 20 20 20 20  te3_errmsg      
246f3 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
246f4 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67 0a 23  e3_api->errmsg.#
246f5 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65  define sqlite3_e
246f6 72 72 6d 73 67 31 36 20 20 20 20 20 20 20 20 20  rrmsg16         
246f7 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
246f8 69 2d 3e 65 72 72 6d 73 67 31 36 0a 23 64 65 66  i->errmsg16.#def
246f9 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63  ine sqlite3_exec
246fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
246fb 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
246fc 65 78 65 63 0a 23 69 66 6e 64 65 66 20 53 51 4c  exec.#ifndef SQL
246fd 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41  ITE_OMIT_DEPRECA
246fe 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  TED.#define sqli
246ff 74 65 33 5f 65 78 70 69 72 65 64 20 20 20 20 20  te3_expired     
24700 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
24701 65 33 5f 61 70 69 2d 3e 65 78 70 69 72 65 64 0a  e3_api->expired.
24702 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73  #endif.#define s
24703 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20  qlite3_finalize 
24704 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
24705 6c 69 74 65 33 5f 61 70 69 2d 3e 66 69 6e 61 6c  lite3_api->final
24706 69 7a 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ize.#define sqli
24707 74 65 33 5f 66 72 65 65 20 20 20 20 20 20 20 20  te3_free        
24708 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
24709 65 33 5f 61 70 69 2d 3e 66 72 65 65 0a 23 64 65  e3_api->free.#de
2470a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65  fine sqlite3_fre
2470b 65 5f 74 61 62 6c 65 20 20 20 20 20 20 20 20 20  e_table         
2470c 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2470d 3e 66 72 65 65 5f 74 61 62 6c 65 0a 23 64 65 66  >free_table.#def
2470e 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ine sqlite3_get_
2470f 61 75 74 6f 63 6f 6d 6d 69 74 20 20 20 20 20 20  autocommit      
24710 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
24711 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 0a 23  get_autocommit.#
24712 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67  define sqlite3_g
24713 65 74 5f 61 75 78 64 61 74 61 20 20 20 20 20 20  et_auxdata      
24714 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
24715 69 2d 3e 67 65 74 5f 61 75 78 64 61 74 61 0a 23  i->get_auxdata.#
24716 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67  define sqlite3_g
24717 65 74 5f 74 61 62 6c 65 20 20 20 20 20 20 20 20  et_table        
24718 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
24719 69 2d 3e 67 65 74 5f 74 61 62 6c 65 0a 23 69 66  i->get_table.#if
2471a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2471b 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64 65 66  _DEPRECATED.#def
2471c 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62  ine sqlite3_glob
2471d 61 6c 5f 72 65 63 6f 76 65 72 20 20 20 20 20 20  al_recover      
2471e 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2471f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 0a 23  global_recover.#
24720 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71  endif.#define sq
24721 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20  lite3_interrupt 
24722 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
24723 69 74 65 33 5f 61 70 69 2d 3e 69 6e 74 65 72 72  ite3_api->interr
24724 75 70 74 78 0a 23 64 65 66 69 6e 65 20 73 71 6c  uptx.#define sql
24725 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
24726 5f 72 6f 77 69 64 20 20 20 20 20 20 73 71 6c 69  _rowid      sqli
24727 74 65 33 5f 61 70 69 2d 3e 6c 61 73 74 5f 69 6e  te3_api->last_in
24728 73 65 72 74 5f 72 6f 77 69 64 0a 23 64 65 66 69  sert_rowid.#defi
24729 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  ne sqlite3_libve
2472a 72 73 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20  rsion           
2472b 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c    sqlite3_api->l
2472c 69 62 76 65 72 73 69 6f 6e 0a 23 64 65 66 69 6e  ibversion.#defin
2472d 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  e sqlite3_libver
2472e 73 69 6f 6e 5f 6e 75 6d 62 65 72 20 20 20 20 20  sion_number     
2472f 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69   sqlite3_api->li
24730 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 0a  bversion_number.
24731 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
24732 6d 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20  malloc          
24733 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
24734 70 69 2d 3e 6d 61 6c 6c 6f 63 0a 23 64 65 66 69  pi->malloc.#defi
24735 6e 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  ne sqlite3_mprin
24736 74 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  tf              
24737 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
24738 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 73  printf.#define s
24739 71 6c 69 74 65 33 5f 6f 70 65 6e 20 20 20 20 20  qlite3_open     
2473a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2473b 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 0a  lite3_api->open.
2473c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2473d 6f 70 65 6e 31 36 20 20 20 20 20 20 20 20 20 20  open16          
2473e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2473f 70 69 2d 3e 6f 70 65 6e 31 36 0a 23 64 65 66 69  pi->open16.#defi
24740 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ne sqlite3_prepa
24741 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  re              
24742 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70    sqlite3_api->p
24743 72 65 70 61 72 65 0a 23 64 65 66 69 6e 65 20 73  repare.#define s
24744 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
24745 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
24746 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61  lite3_api->prepa
24747 72 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  re16.#define sql
24748 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 20  ite3_prepare_v2 
24749 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2474a 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65  te3_api->prepare
2474b 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _v2.#define sqli
2474c 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
2474d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2474e 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 31  e3_api->prepare1
2474f 36 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  6_v2.#define sql
24750 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20 20 20  ite3_profile    
24751 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
24752 74 65 33 5f 61 70 69 2d 3e 70 72 6f 66 69 6c 65  te3_api->profile
24753 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
24754 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  _progress_handle
24755 72 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  r       sqlite3_
24756 61 70 69 2d 3e 70 72 6f 67 72 65 73 73 5f 68 61  api->progress_ha
24757 6e 64 6c 65 72 0a 23 64 65 66 69 6e 65 20 73 71  ndler.#define sq
24758 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 20 20 20  lite3_realloc   
24759 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2475a 69 74 65 33 5f 61 70 69 2d 3e 72 65 61 6c 6c 6f  ite3_api->reallo
2475b 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  c.#define sqlite
2475c 33 5f 72 65 73 65 74 20 20 20 20 20 20 20 20 20  3_reset         
2475d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2475e 5f 61 70 69 2d 3e 72 65 73 65 74 0a 23 64 65 66  _api->reset.#def
2475f 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
24760 6c 74 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20  lt_blob         
24761 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
24762 72 65 73 75 6c 74 5f 62 6c 6f 62 0a 23 64 65 66  result_blob.#def
24763 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
24764 6c 74 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20  lt_double       
24765 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
24766 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 0a 23 64  result_double.#d
24767 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
24768 73 75 6c 74 5f 65 72 72 6f 72 20 20 20 20 20 20  sult_error      
24769 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2476a 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 0a 23  ->result_error.#
2476b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
2476c 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20  esult_error16   
2476d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2476e 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  i->result_error1
2476f 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
24770 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 20 20 20  3_result_int    
24771 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
24772 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74  _api->result_int
24773 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
24774 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 20 20 20  _result_int64   
24775 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
24776 61 70 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74 36  api->result_int6
24777 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  4.#define sqlite
24778 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 20 20  3_result_null   
24779 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2477a 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 6e 75 6c  _api->result_nul
2477b 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  l.#define sqlite
2477c 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 20 20 20  3_result_text   
2477d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2477e 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78  _api->result_tex
2477f 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
24780 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 20  3_result_text16 
24781 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
24782 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78  _api->result_tex
24783 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t16.#define sqli
24784 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
24785 36 62 65 20 20 20 20 20 20 20 20 73 71 6c 69 74  6be        sqlit
24786 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74  e3_api->result_t
24787 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e 65 20  ext16be.#define 
24788 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
24789 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20 73  ext16le        s
2478a 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
2478b 6c 74 5f 74 65 78 74 31 36 6c 65 0a 23 64 65 66  lt_text16le.#def
2478c 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
2478d 6c 74 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20  lt_value        
2478e 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2478f 72 65 73 75 6c 74 5f 76 61 6c 75 65 0a 23 64 65  result_value.#de
24790 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c  fine sqlite3_rol
24791 6c 62 61 63 6b 5f 68 6f 6f 6b 20 20 20 20 20 20  lback_hook      
24792 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
24793 3e 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 0a 23  >rollback_hook.#
24794 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
24795 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 20 20  et_authorizer   
24796 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
24797 69 2d 3e 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  i->set_authorize
24798 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
24799 33 5f 73 65 74 5f 61 75 78 64 61 74 61 20 20 20  3_set_auxdata   
2479a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2479b 5f 61 70 69 2d 3e 73 65 74 5f 61 75 78 64 61 74  _api->set_auxdat
2479c 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  a.#define sqlite
2479d 33 5f 73 6e 70 72 69 6e 74 66 20 20 20 20 20 20  3_snprintf      
2479e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2479f 5f 61 70 69 2d 3e 73 6e 70 72 69 6e 74 66 0a 23  _api->snprintf.#
247a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
247a1 74 65 70 20 20 20 20 20 20 20 20 20 20 20 20 20  tep             
247a2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
247a3 69 2d 3e 73 74 65 70 0a 23 64 65 66 69 6e 65 20  i->step.#define 
247a4 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f  sqlite3_table_co
247a5 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20 73  lumn_metadata  s
247a6 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 61 62 6c  qlite3_api->tabl
247a7 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74  e_column_metadat
247a8 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  a.#define sqlite
247a9 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70  3_thread_cleanup
247aa 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
247ab 5f 61 70 69 2d 3e 74 68 72 65 61 64 5f 63 6c 65  _api->thread_cle
247ac 61 6e 75 70 0a 23 64 65 66 69 6e 65 20 73 71 6c  anup.#define sql
247ad 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
247ae 65 73 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  es          sqli
247af 74 65 33 5f 61 70 69 2d 3e 74 6f 74 61 6c 5f 63  te3_api->total_c
247b0 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65 20 73  hanges.#define s
247b1 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20 20 20  qlite3_trace    
247b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
247b3 6c 69 74 65 33 5f 61 70 69 2d 3e 74 72 61 63 65  lite3_api->trace
247b4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
247b5 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
247b6 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
247b7 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67  transfer_binding
247b8 73 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  s      sqlite3_a
247b9 70 69 2d 3e 74 72 61 6e 73 66 65 72 5f 62 69 6e  pi->transfer_bin
247ba 64 69 6e 67 73 0a 23 65 6e 64 69 66 0a 23 64 65  dings.#endif.#de
247bb 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 75 70 64  fine sqlite3_upd
247bc 61 74 65 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20  ate_hook        
247bd 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
247be 3e 75 70 64 61 74 65 5f 68 6f 6f 6b 0a 23 64 65  >update_hook.#de
247bf 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 75 73 65  fine sqlite3_use
247c0 72 5f 64 61 74 61 20 20 20 20 20 20 20 20 20 20  r_data          
247c1 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
247c2 3e 75 73 65 72 5f 64 61 74 61 0a 23 64 65 66 69  >user_data.#defi
247c3 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
247c4 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20  _blob           
247c5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
247c6 61 6c 75 65 5f 62 6c 6f 62 0a 23 64 65 66 69 6e  alue_blob.#defin
247c7 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
247c8 62 79 74 65 73 20 20 20 20 20 20 20 20 20 20 20  bytes           
247c9 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
247ca 6c 75 65 5f 62 79 74 65 73 0a 23 64 65 66 69 6e  lue_bytes.#defin
247cb 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
247cc 62 79 74 65 73 31 36 20 20 20 20 20 20 20 20 20  bytes16         
247cd 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
247ce 6c 75 65 5f 62 79 74 65 73 31 36 0a 23 64 65 66  lue_bytes16.#def
247cf 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ine sqlite3_valu
247d0 65 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20  e_double        
247d1 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
247d2 76 61 6c 75 65 5f 64 6f 75 62 6c 65 0a 23 64 65  value_double.#de
247d3 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
247d4 75 65 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20  ue_int          
247d5 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
247d6 3e 76 61 6c 75 65 5f 69 6e 74 0a 23 64 65 66 69  >value_int.#defi
247d7 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
247d8 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20 20  _int64          
247d9 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
247da 61 6c 75 65 5f 69 6e 74 36 34 0a 23 64 65 66 69  alue_int64.#defi
247db 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
247dc 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 20 20 20  _numeric_type   
247dd 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
247de 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70  alue_numeric_typ
247df 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
247e0 33 5f 76 61 6c 75 65 5f 74 65 78 74 20 20 20 20  3_value_text    
247e1 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
247e2 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74  _api->value_text
247e3 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
247e4 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 20 20 20  _value_text16   
247e5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
247e6 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31  api->value_text1
247e7 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
247e8 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65  3_value_text16be
247e9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
247ea 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74  _api->value_text
247eb 31 36 62 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  16be.#define sql
247ec 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
247ed 36 6c 65 20 20 20 20 20 20 20 20 20 73 71 6c 69  6le         sqli
247ee 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74  te3_api->value_t
247ef 65 78 74 31 36 6c 65 0a 23 64 65 66 69 6e 65 20  ext16le.#define 
247f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
247f1 70 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73  pe             s
247f2 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
247f3 65 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73  e_type.#define s
247f4 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 20  qlite3_vmprintf 
247f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
247f6 6c 69 74 65 33 5f 61 70 69 2d 3e 76 6d 70 72 69  lite3_api->vmpri
247f7 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ntf.#define sqli
247f8 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
247f9 63 74 69 6f 6e 20 20 20 20 20 20 73 71 6c 69 74  ction      sqlit
247fa 65 33 5f 61 70 69 2d 3e 6f 76 65 72 6c 6f 61 64  e3_api->overload
247fb 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e  _function.#defin
247fc 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
247fd 65 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 20  e_v2            
247fe 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
247ff 65 70 61 72 65 5f 76 32 0a 23 64 65 66 69 6e 65  epare_v2.#define
24800 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
24801 31 36 5f 76 32 20 20 20 20 20 20 20 20 20 20 20  16_v2           
24802 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
24803 70 61 72 65 31 36 5f 76 32 0a 23 64 65 66 69 6e  pare16_v2.#defin
24804 65 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f  e sqlite3_clear_
24805 62 69 6e 64 69 6e 67 73 20 20 20 20 20 20 20 20  bindings        
24806 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c   sqlite3_api->cl
24807 65 61 72 5f 62 69 6e 64 69 6e 67 73 0a 23 64 65  ear_bindings.#de
24808 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
24809 64 5f 7a 65 72 6f 62 6c 6f 62 20 20 20 20 20 20  d_zeroblob      
2480a 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2480b 3e 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 0a 23  >bind_zeroblob.#
2480c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
2480d 6c 6f 62 5f 62 79 74 65 73 20 20 20 20 20 20 20  lob_bytes       
2480e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2480f 69 2d 3e 62 6c 6f 62 5f 62 79 74 65 73 0a 23 64  i->blob_bytes.#d
24810 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
24811 6f 62 5f 63 6c 6f 73 65 20 20 20 20 20 20 20 20  ob_close        
24812 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
24813 2d 3e 62 6c 6f 62 5f 63 6c 6f 73 65 0a 23 64 65  ->blob_close.#de
24814 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
24815 62 5f 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20  b_open          
24816 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
24817 3e 62 6c 6f 62 5f 6f 70 65 6e 0a 23 64 65 66 69  >blob_open.#defi
24818 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
24819 72 65 61 64 20 20 20 20 20 20 20 20 20 20 20 20  read            
2481a 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
2481b 6c 6f 62 5f 72 65 61 64 0a 23 64 65 66 69 6e 65  lob_read.#define
2481c 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72   sqlite3_blob_wr
2481d 69 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ite             
2481e 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f  sqlite3_api->blo
2481f 62 5f 77 72 69 74 65 0a 23 64 65 66 69 6e 65 20  b_write.#define 
24820 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
24821 6f 6c 6c 61 74 69 6f 6e 5f 76 32 20 20 20 20 73  ollation_v2    s
24822 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
24823 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 0a  te_collation_v2.
24824 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
24825 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20 20 20 20  file_control    
24826 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
24827 70 69 2d 3e 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  pi->file_control
24828 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
24829 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65  _memory_highwate
2482a 72 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  r       sqlite3_
2482b 61 70 69 2d 3e 6d 65 6d 6f 72 79 5f 68 69 67 68  api->memory_high
2482c 77 61 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71  water.#define sq
2482d 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65  lite3_memory_use
2482e 64 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  d            sql
2482f 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79  ite3_api->memory
24830 5f 75 73 65 64 0a 23 64 65 66 69 6e 65 20 73 71  _used.#define sq
24831 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
24832 63 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  c            sql
24833 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f  ite3_api->mutex_
24834 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71  alloc.#define sq
24835 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
24836 72 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  r            sql
24837 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f  ite3_api->mutex_
24838 65 6e 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71  enter.#define sq
24839 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
2483a 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2483b 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f  ite3_api->mutex_
2483c 66 72 65 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  free.#define sql
2483d 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2483e 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2483f 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 6c  te3_api->mutex_l
24840 65 61 76 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  eave.#define sql
24841 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 20 20  ite3_mutex_try  
24842 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
24843 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 74  te3_api->mutex_t
24844 72 79 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ry.#define sqlit
24845 65 33 5f 6f 70 65 6e 5f 76 32 20 20 20 20 20 20  e3_open_v2      
24846 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
24847 33 5f 61 70 69 2d 3e 6f 70 65 6e 5f 76 32 0a 23  3_api->open_v2.#
24848 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
24849 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 20 20 20  elease_memory   
2484a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2484b 69 2d 3e 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  i->release_memor
2484c 79 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  y.#define sqlite
2484d 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  3_result_error_n
2484e 6f 6d 65 6d 20 20 20 20 20 73 71 6c 69 74 65 33  omem     sqlite3
2484f 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72  _api->result_err
24850 6f 72 5f 6e 6f 6d 65 6d 0a 23 64 65 66 69 6e 65  or_nomem.#define
24851 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
24852 65 72 72 6f 72 5f 74 6f 6f 62 69 67 20 20 20 20  error_toobig    
24853 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
24854 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
24855 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
24856 5f 73 6c 65 65 70 20 20 20 20 20 20 20 20 20 20  _sleep          
24857 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
24858 61 70 69 2d 3e 73 6c 65 65 70 0a 23 64 65 66 69  api->sleep.#defi
24859 6e 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f  ne sqlite3_soft_
2485a 68 65 61 70 5f 6c 69 6d 69 74 20 20 20 20 20 20  heap_limit      
2485b 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73    sqlite3_api->s
2485c 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 0a 23  oft_heap_limit.#
2485d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
2485e 66 73 5f 66 69 6e 64 20 20 20 20 20 20 20 20 20  fs_find         
2485f 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
24860 69 2d 3e 76 66 73 5f 66 69 6e 64 0a 23 64 65 66  i->vfs_find.#def
24861 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  ine sqlite3_vfs_
24862 72 65 67 69 73 74 65 72 20 20 20 20 20 20 20 20  register        
24863 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
24864 76 66 73 5f 72 65 67 69 73 74 65 72 0a 23 64 65  vfs_register.#de
24865 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73  fine sqlite3_vfs
24866 5f 75 6e 72 65 67 69 73 74 65 72 20 20 20 20 20  _unregister     
24867 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
24868 3e 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 0a  >vfs_unregister.
24869 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2486a 74 68 72 65 61 64 73 61 66 65 20 20 20 20 20 20  threadsafe      
2486b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2486c 70 69 2d 3e 78 74 68 72 65 61 64 73 61 66 65 0a  pi->xthreadsafe.
2486d 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2486e 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 20  result_zeroblob 
2486f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
24870 70 69 2d 3e 72 65 73 75 6c 74 5f 7a 65 72 6f 62  pi->result_zerob
24871 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  lob.#define sqli
24872 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
24873 5f 63 6f 64 65 20 20 20 20 20 20 73 71 6c 69 74  _code      sqlit
24874 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65  e3_api->result_e
24875 72 72 6f 72 5f 63 6f 64 65 0a 23 64 65 66 69 6e  rror_code.#defin
24876 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  e sqlite3_test_c
24877 6f 6e 74 72 6f 6c 20 20 20 20 20 20 20 20 20 20  ontrol          
24878 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 65   sqlite3_api->te
24879 73 74 5f 63 6f 6e 74 72 6f 6c 0a 23 64 65 66 69  st_control.#defi
2487a 6e 65 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  ne sqlite3_rando
2487b 6d 6e 65 73 73 20 20 20 20 20 20 20 20 20 20 20  mness           
2487c 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
2487d 61 6e 64 6f 6d 6e 65 73 73 0a 23 64 65 66 69 6e  andomness.#defin
2487e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e sqlite3_contex
2487f 74 5f 64 62 5f 68 61 6e 64 6c 65 20 20 20 20 20  t_db_handle     
24880 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
24881 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 0a  ntext_db_handle.
24882 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
24883 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f  extended_result_
24884 63 6f 64 65 73 20 20 73 71 6c 69 74 65 33 5f 61  codes  sqlite3_a
24885 70 69 2d 3e 65 78 74 65 6e 64 65 64 5f 72 65 73  pi->extended_res
24886 75 6c 74 5f 63 6f 64 65 73 0a 23 64 65 66 69 6e  ult_codes.#defin
24887 65 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 20  e sqlite3_limit 
24888 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24889 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69   sqlite3_api->li
2488a 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  mit.#define sqli
2488b 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 20 20 20  te3_next_stmt   
2488c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2488d 65 33 5f 61 70 69 2d 3e 6e 65 78 74 5f 73 74 6d  e3_api->next_stm
2488e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
2488f 33 5f 73 71 6c 20 20 20 20 20 20 20 20 20 20 20  3_sql           
24890 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
24891 5f 61 70 69 2d 3e 73 71 6c 0a 23 64 65 66 69 6e  _api->sql.#defin
24892 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73  e sqlite3_status
24893 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24894 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 74   sqlite3_api->st
24895 61 74 75 73 0a 23 65 6e 64 69 66 20 2f 2a 20 53  atus.#endif /* S
24896 51 4c 49 54 45 5f 43 4f 52 45 20 2a 2f 0a 0a 23  QLITE_CORE */..#
24897 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58  define SQLITE_EX
24898 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 20 20 20  TENSION_INIT1   
24899 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
2489a 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 73 71  api_routines *sq
2489b 6c 69 74 65 33 5f 61 70 69 20 3d 20 30 3b 0a 23  lite3_api = 0;.#
2489c 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58  define SQLITE_EX
2489d 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 76 29  TENSION_INIT2(v)
2489e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 20 3d 20    sqlite3_api = 
2489f 76 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53  v;..#endif /* _S
248a0 51 4c 49 54 45 33 45 58 54 5f 48 5f 20 2a 2f 0a  QLITE3EXT_H_ */.
248a1 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
248a2 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 65   End of sqlite3e
248a3 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xt.h ***********
248a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
248a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
248a6 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
248a7 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
248a8 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
248a9 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a   loadext.c *****
248aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
248ab 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
248ac 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
248ad 53 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65  SION../*.** Some
248ae 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72   API routines ar
248af 65 20 6f 6d 69 74 74 65 64 20 77 68 65 6e 20 76  e omitted when v
248b0 61 72 69 6f 75 73 20 66 65 61 74 75 72 65 73 20  arious features 
248b1 61 72 65 0a 2a 2a 20 65 78 63 6c 75 64 65 64 20  are.** excluded 
248b2 66 72 6f 6d 20 61 20 62 75 69 6c 64 20 6f 66 20  from a build of 
248b3 53 51 4c 69 74 65 2e 20 20 53 75 62 73 74 69 74  SQLite.  Substit
248b4 75 74 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ute a NULL point
248b5 65 72 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 6d 69  er.** for any mi
248b6 73 73 69 6e 67 20 41 50 49 73 2e 0a 2a 2f 0a 23  ssing APIs..*/.#
248b7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  ifndef SQLITE_EN
248b8 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
248b9 44 41 54 41 0a 23 20 64 65 66 69 6e 65 20 73 71  DATA.# define sq
248ba 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
248bb 61 62 61 73 65 5f 6e 61 6d 65 20 20 20 30 0a 23  abase_name   0.#
248bc 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
248bd 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
248be 6e 61 6d 65 31 36 20 30 0a 23 20 64 65 66 69 6e  name16 0.# defin
248bf 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
248c0 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20 20 20 20  _table_name     
248c1 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
248c2 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
248c3 5f 6e 61 6d 65 31 36 20 20 20 20 30 0a 23 20 64  _name16    0.# d
248c4 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
248c5 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
248c6 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
248c7 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
248c8 72 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30  rigin_name16   0
248c9 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
248ca 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d  3_table_column_m
248cb 65 74 61 64 61 74 61 20 20 30 0a 23 65 6e 64 69  etadata  0.#endi
248cc 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
248cd 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
248ce 49 4f 4e 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ION.# define sql
248cf 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
248d0 7a 65 72 20 20 20 20 20 20 20 20 20 30 0a 23 65  zer         0.#e
248d1 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
248d2 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 23  ITE_OMIT_UTF16.#
248d3 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
248d4 62 69 6e 64 5f 74 65 78 74 31 36 20 20 20 20 20  bind_text16     
248d5 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
248d6 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  e sqlite3_collat
248d7 69 6f 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20  ion_needed16    
248d8 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
248d9 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
248da 79 70 65 31 36 20 20 20 20 20 20 30 0a 23 20 64  ype16      0.# d
248db 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
248dc 6c 75 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20  lumn_name16     
248dd 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
248de 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
248df 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20 30  ext16          0
248e0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
248e1 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 20 20 20  3_complete16    
248e2 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
248e3 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ine sqlite3_crea
248e4 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 20 20  te_collation16  
248e5 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
248e6 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
248e7 63 74 69 6f 6e 31 36 20 20 20 20 20 20 30 0a 23  ction16      0.#
248e8 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
248e9 65 72 72 6d 73 67 31 36 20 20 20 20 20 20 20 20  errmsg16        
248ea 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
248eb 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  e sqlite3_open16
248ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
248ed 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
248ee 74 65 33 5f 70 72 65 70 61 72 65 31 36 20 20 20  te3_prepare16   
248ef 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64             0.# d
248f0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
248f1 65 70 61 72 65 31 36 5f 76 32 20 20 20 20 20 20  epare16_v2      
248f2 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
248f3 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
248f4 72 72 6f 72 31 36 20 20 20 20 20 20 20 20 20 30  rror16         0
248f5 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
248f6 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 20  3_result_text16 
248f7 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
248f8 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
248f9 6c 74 5f 74 65 78 74 31 36 62 65 20 20 20 20 20  lt_text16be     
248fa 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
248fb 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
248fc 74 31 36 6c 65 20 20 20 20 20 20 20 20 30 0a 23  t16le        0.#
248fd 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
248fe 76 61 6c 75 65 5f 74 65 78 74 31 36 20 20 20 20  value_text16    
248ff 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
24900 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
24901 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20 20  text16be        
24902 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
24903 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
24904 6c 65 20 20 20 20 20 20 20 20 20 30 0a 23 20 64  le         0.# d
24905 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
24906 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
24907 6d 65 31 36 20 30 0a 23 20 64 65 66 69 6e 65 20  me16 0.# define 
24908 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
24909 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20 30  able_name16    0
2490a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
2490b 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  3_column_origin_
2490c 6e 61 6d 65 31 36 20 20 20 30 0a 23 65 6e 64 69  name16   0.#endi
2490d 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
2490e 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 23  _OMIT_COMPLETE.#
2490f 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
24910 63 6f 6d 70 6c 65 74 65 20 30 0a 23 20 64 65 66  complete 0.# def
24911 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ine sqlite3_comp
24912 6c 65 74 65 31 36 20 30 0a 23 65 6e 64 69 66 0a  lete16 0.#endif.
24913 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
24914 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c  MIT_PROGRESS_CAL
24915 4c 42 41 43 4b 0a 23 20 64 65 66 69 6e 65 20 73  LBACK.# define s
24916 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f  qlite3_progress_
24917 68 61 6e 64 6c 65 72 20 30 0a 23 65 6e 64 69 66  handler 0.#endif
24918 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
24919 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2491a 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  E.# define sqlit
2491b 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
2491c 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
2491d 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
2491e 65 5f 76 32 20 30 0a 23 20 64 65 66 69 6e 65 20  e_v2 0.# define 
2491f 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
24920 76 74 61 62 20 30 0a 23 65 6e 64 69 66 0a 0a 23  vtab 0.#endif..#
24921 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
24922 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 23  T_SHARED_CACHE.#
24923 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
24924 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61  enable_shared_ca
24925 63 68 65 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69  che 0.#endif..#i
24926 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
24927 5f 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20  _TRACE.# define 
24928 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20  sqlite3_profile 
24929 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
2492a 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20   sqlite3_trace  
2492b 20 20 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a         0.#endif.
2492c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
2492d 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0a 23 20  MIT_GET_TABLE.# 
2492e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66  define sqlite3_f
2492f 72 65 65 5f 74 61 62 6c 65 20 20 20 20 30 0a 23  ree_table    0.#
24930 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
24931 67 65 74 5f 74 61 62 6c 65 20 20 20 20 20 30 0a  get_table     0.
24932 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
24933 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
24934 4c 4f 42 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  LOB.#define sqli
24935 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f  te3_bind_zeroblo
24936 62 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c  b  0.#define sql
24937 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20  ite3_blob_bytes 
24938 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71      0.#define sq
24939 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
2493a 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73       0.#define s
2493b 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
2493c 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
2493d 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61  sqlite3_blob_rea
2493e 64 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65  d      0.#define
2493f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72   sqlite3_blob_wr
24940 69 74 65 20 20 20 20 20 30 0a 23 65 6e 64 69 66  ite     0.#endif
24941 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
24942 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
24943 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72  contains pointer
24944 73 20 74 6f 20 61 6c 6c 20 53 51 4c 69 74 65 20  s to all SQLite 
24945 41 50 49 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  API routines..**
24946 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
24947 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
24948 70 61 73 73 65 64 20 69 6e 74 6f 20 65 78 74 65  passed into exte
24949 6e 73 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 79  nsions when they
2494a 20 61 72 65 0a 2a 2a 20 6c 6f 61 64 65 64 20 73   are.** loaded s
2494b 6f 20 74 68 61 74 20 74 68 65 20 65 78 74 65 6e  o that the exten
2494c 73 69 6f 6e 20 63 61 6e 20 6d 61 6b 65 20 63 61  sion can make ca
2494d 6c 6c 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  lls back into th
2494e 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6c 69 62 72  e SQLite.** libr
2494f 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ary..**.** When 
24950 61 64 64 69 6e 67 20 6e 65 77 20 41 50 49 73 2c  adding new APIs,
24951 20 61 64 64 20 74 68 65 6d 20 74 6f 20 74 68 65   add them to the
24952 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 20   bottom of this 
24953 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 6e 20  structure.** in 
24954 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65 72 76  order to preserv
24955 65 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  e backwards comp
24956 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a  atibility..**.**
24957 20 45 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74   Extensions that
24958 20 75 73 65 20 6e 65 77 65 72 20 41 50 49 73 20   use newer APIs 
24959 73 68 6f 75 6c 64 20 66 69 72 73 74 20 63 61 6c  should first cal
2495a 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  l the.** sqlite3
2495b 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  _libversion_numb
2495c 65 72 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72  er() to make sur
2495d 65 20 74 68 61 74 20 74 68 65 20 41 50 49 20 74  e that the API t
2495e 68 65 79 0a 2a 2a 20 69 6e 74 65 6e 64 20 74 6f  hey.** intend to
2495f 20 75 73 65 20 69 73 20 73 75 70 70 6f 72 74 65   use is supporte
24960 64 20 62 79 20 74 68 65 20 6c 69 62 72 61 72 79  d by the library
24961 2e 20 20 45 78 74 65 6e 73 69 6f 6e 73 20 73 68  .  Extensions sh
24962 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 63 68 65  ould.** also che
24963 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
24964 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72  that the pointer
24965 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
24966 20 69 73 0a 2a 2a 20 6e 6f 74 20 4e 55 4c 4c 20   is.** not NULL 
24967 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 69  before calling i
24968 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  t..*/.static con
24969 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
2496a 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33 41  outines sqlite3A
2496b 70 69 73 20 3d 20 7b 0a 20 20 73 71 6c 69 74 65  pis = {.  sqlite
2496c 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
2496d 65 78 74 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c  ext,.#ifndef SQL
2496e 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41  ITE_OMIT_DEPRECA
2496f 54 45 44 0a 20 20 73 71 6c 69 74 65 33 5f 61 67  TED.  sqlite3_ag
24970 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 2c 0a 23  gregate_count,.#
24971 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66  else.  0,.#endif
24972 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
24973 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f  blob,.  sqlite3_
24974 62 69 6e 64 5f 64 6f 75 62 6c 65 2c 0a 20 20 73  bind_double,.  s
24975 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 2c  qlite3_bind_int,
24976 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
24977 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33  int64,.  sqlite3
24978 5f 62 69 6e 64 5f 6e 75 6c 6c 2c 0a 20 20 73 71  _bind_null,.  sq
24979 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
2497a 65 74 65 72 5f 63 6f 75 6e 74 2c 0a 20 20 73 71  eter_count,.  sq
2497b 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
2497c 65 74 65 72 5f 69 6e 64 65 78 2c 0a 20 20 73 71  eter_index,.  sq
2497d 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
2497e 65 74 65 72 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c  eter_name,.  sql
2497f 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 2c 0a  ite3_bind_text,.
24980 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74    sqlite3_bind_t
24981 65 78 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ext16,.  sqlite3
24982 5f 62 69 6e 64 5f 76 61 6c 75 65 2c 0a 20 20 73  _bind_value,.  s
24983 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
24984 6c 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ler,.  sqlite3_b
24985 75 73 79 5f 74 69 6d 65 6f 75 74 2c 0a 20 20 73  usy_timeout,.  s
24986 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 2c 0a  qlite3_changes,.
24987 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 2c    sqlite3_close,
24988 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  .  sqlite3_colla
24989 74 69 6f 6e 5f 6e 65 65 64 65 64 2c 0a 20 20 73  tion_needed,.  s
2498a 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2498b 5f 6e 65 65 64 65 64 31 36 2c 0a 20 20 73 71 6c  _needed16,.  sql
2498c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
2498d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
2498e 6d 6e 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69  mn_bytes,.  sqli
2498f 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
24990 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
24991 6c 75 6d 6e 5f 63 6f 75 6e 74 2c 0a 20 20 73 71  lumn_count,.  sq
24992 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
24993 61 62 61 73 65 5f 6e 61 6d 65 2c 0a 20 20 73 71  abase_name,.  sq
24994 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
24995 61 62 61 73 65 5f 6e 61 6d 65 31 36 2c 0a 20 20  abase_name16,.  
24996 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
24997 65 63 6c 74 79 70 65 2c 0a 20 20 73 71 6c 69 74  ecltype,.  sqlit
24998 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
24999 70 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  pe16,.  sqlite3_
2499a 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 2c 0a 20  column_double,. 
2499b 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2499c 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  int,.  sqlite3_c
2499d 6f 6c 75 6d 6e 5f 69 6e 74 36 34 2c 0a 20 20 73  olumn_int64,.  s
2499e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
2499f 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  me,.  sqlite3_co
249a0 6c 75 6d 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20 73  lumn_name16,.  s
249a1 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
249a2 69 67 69 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c  igin_name,.  sql
249a3 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
249a4 69 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c  in_name16,.  sql
249a5 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
249a6 65 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65  e_name,.  sqlite
249a7 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
249a8 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ame16,.  sqlite3
249a9 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 2c 0a 20 20  _column_text,.  
249aa 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
249ab 65 78 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ext16,.  sqlite3
249ac 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 2c 0a 20 20  _column_type,.  
249ad 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
249ae 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  alue,.  sqlite3_
249af 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 2c 0a 20 20 73  commit_hook,.  s
249b0 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 2c  qlite3_complete,
249b1 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  .  sqlite3_compl
249b2 65 74 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ete16,.  sqlite3
249b3 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
249b4 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  n,.  sqlite3_cre
249b5 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 2c  ate_collation16,
249b6 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
249b7 65 5f 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 73 71  e_function,.  sq
249b8 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
249b9 63 74 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74  ction16,.  sqlit
249ba 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
249bb 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 61 74 61  ,.  sqlite3_data
249bc 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65  _count,.  sqlite
249bd 33 5f 64 62 5f 68 61 6e 64 6c 65 2c 0a 20 20 73  3_db_handle,.  s
249be 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
249bf 74 61 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65  tab,.  sqlite3_e
249c0 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63  nable_shared_cac
249c1 68 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72  he,.  sqlite3_er
249c2 72 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33  rcode,.  sqlite3
249c3 5f 65 72 72 6d 73 67 2c 0a 20 20 73 71 6c 69 74  _errmsg,.  sqlit
249c4 65 33 5f 65 72 72 6d 73 67 31 36 2c 0a 20 20 73  e3_errmsg16,.  s
249c5 71 6c 69 74 65 33 5f 65 78 65 63 2c 0a 23 69 66  qlite3_exec,.#if
249c6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
249c7 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 73 71  _DEPRECATED.  sq
249c8 6c 69 74 65 33 5f 65 78 70 69 72 65 64 2c 0a 23  lite3_expired,.#
249c9 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66  else.  0,.#endif
249ca 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  .  sqlite3_final
249cb 69 7a 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66  ize,.  sqlite3_f
249cc 72 65 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66  ree,.  sqlite3_f
249cd 72 65 65 5f 74 61 62 6c 65 2c 0a 20 20 73 71 6c  ree_table,.  sql
249ce 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d  ite3_get_autocom
249cf 6d 69 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67  mit,.  sqlite3_g
249d0 65 74 5f 61 75 78 64 61 74 61 2c 0a 20 20 73 71  et_auxdata,.  sq
249d1 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 2c  lite3_get_table,
249d2 0a 20 20 30 2c 20 20 20 20 20 2f 2a 20 57 61 73  .  0,     /* Was
249d3 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f   sqlite3_global_
249d4 72 65 63 6f 76 65 72 28 29 2c 20 62 75 74 20 74  recover(), but t
249d5 68 61 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  hat function is 
249d6 64 65 70 72 65 63 61 74 65 64 20 2a 2f 0a 20 20  deprecated */.  
249d7 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
249d8 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 61 73  t,.  sqlite3_las
249d9 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 0a  t_insert_rowid,.
249da 20 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72    sqlite3_libver
249db 73 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  sion,.  sqlite3_
249dc 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
249dd 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 61 6c  r,.  sqlite3_mal
249de 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  loc,.  sqlite3_m
249df 70 72 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65  printf,.  sqlite
249e0 33 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65  3_open,.  sqlite
249e1 33 5f 6f 70 65 6e 31 36 2c 0a 20 20 73 71 6c 69  3_open16,.  sqli
249e2 74 65 33 5f 70 72 65 70 61 72 65 2c 0a 20 20 73  te3_prepare,.  s
249e3 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
249e4 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 6f 66  ,.  sqlite3_prof
249e5 69 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70  ile,.  sqlite3_p
249e6 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 2c  rogress_handler,
249e7 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  .  sqlite3_reall
249e8 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  oc,.  sqlite3_re
249e9 73 65 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  set,.  sqlite3_r
249ea 65 73 75 6c 74 5f 62 6c 6f 62 2c 0a 20 20 73 71  esult_blob,.  sq
249eb 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
249ec 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  ble,.  sqlite3_r
249ed 65 73 75 6c 74 5f 65 72 72 6f 72 2c 0a 20 20 73  esult_error,.  s
249ee 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
249ef 72 6f 72 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ror16,.  sqlite3
249f0 5f 72 65 73 75 6c 74 5f 69 6e 74 2c 0a 20 20 73  _result_int,.  s
249f1 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
249f2 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  t64,.  sqlite3_r
249f3 65 73 75 6c 74 5f 6e 75 6c 6c 2c 0a 20 20 73 71  esult_null,.  sq
249f4 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
249f5 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  t,.  sqlite3_res
249f6 75 6c 74 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  ult_text16,.  sq
249f7 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
249f8 74 31 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33  t16be,.  sqlite3
249f9 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
249fa 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
249fb 6c 74 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69  lt_value,.  sqli
249fc 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f  te3_rollback_hoo
249fd 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74  k,.  sqlite3_set
249fe 5f 61 75 74 68 6f 72 69 7a 65 72 2c 0a 20 20 73  _authorizer,.  s
249ff 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
24a00 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e  ta,.  sqlite3_sn
24a01 70 72 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65  printf,.  sqlite
24a02 33 5f 73 74 65 70 2c 0a 20 20 73 71 6c 69 74 65  3_step,.  sqlite
24a03 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d  3_table_column_m
24a04 65 74 61 64 61 74 61 2c 0a 23 69 66 6e 64 65 66  etadata,.#ifndef
24a05 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
24a06 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69 74 65  RECATED.  sqlite
24a07 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70  3_thread_cleanup
24a08 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e  ,.#else.  0,.#en
24a09 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  dif.  sqlite3_to
24a0a 74 61 6c 5f 63 68 61 6e 67 65 73 2c 0a 20 20 73  tal_changes,.  s
24a0b 71 6c 69 74 65 33 5f 74 72 61 63 65 2c 0a 23 69  qlite3_trace,.#i
24a0c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24a0d 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 73  T_DEPRECATED.  s
24a0e 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f  qlite3_transfer_
24a0f 62 69 6e 64 69 6e 67 73 2c 0a 23 65 6c 73 65 0a  bindings,.#else.
24a10 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71    0,.#endif.  sq
24a11 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
24a12 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 75 73 65  k,.  sqlite3_use
24a13 72 5f 64 61 74 61 2c 0a 20 20 73 71 6c 69 74 65  r_data,.  sqlite
24a14 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 2c 0a 20 20  3_value_blob,.  
24a15 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
24a16 74 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  tes,.  sqlite3_v
24a17 61 6c 75 65 5f 62 79 74 65 73 31 36 2c 0a 20 20  alue_bytes16,.  
24a18 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
24a19 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  uble,.  sqlite3_
24a1a 76 61 6c 75 65 5f 69 6e 74 2c 0a 20 20 73 71 6c  value_int,.  sql
24a1b 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
24a1c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
24a1d 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 2c 0a  e_numeric_type,.
24a1e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
24a1f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  text,.  sqlite3_
24a20 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 0a 20 20  value_text16,.  
24a21 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
24a22 78 74 31 36 62 65 2c 0a 20 20 73 71 6c 69 74 65  xt16be,.  sqlite
24a23 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
24a24 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
24a25 65 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65  e_type,.  sqlite
24a26 33 5f 76 6d 70 72 69 6e 74 66 2c 0a 20 20 2f 2a  3_vmprintf,.  /*
24a27 0a 20 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e  .  ** The origin
24a28 61 6c 20 41 50 49 20 73 65 74 20 65 6e 64 73 20  al API set ends 
24a29 68 65 72 65 2e 20 20 41 6c 6c 20 65 78 74 65 6e  here.  All exten
24a2a 73 69 6f 6e 73 20 63 61 6e 20 63 61 6c 6c 20 61  sions can call a
24a2b 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 41  ny.  ** of the A
24a2c 50 49 73 20 61 62 6f 76 65 20 70 72 6f 76 69 64  PIs above provid
24a2d 65 64 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e  ed that the poin
24a2e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e  ter is not NULL.
24a2f 20 20 42 75 74 0a 20 20 2a 2a 20 62 65 66 6f 72    But.  ** befor
24a30 65 20 63 61 6c 6c 69 6e 67 20 41 50 49 73 20 74  e calling APIs t
24a31 68 61 74 20 66 6f 6c 6c 6f 77 2c 20 65 78 74 65  hat follow, exte
24a32 6e 73 69 6f 6e 20 73 68 6f 75 6c 64 20 63 68 65  nsion should che
24a33 63 6b 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69  ck the.  ** sqli
24a34 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
24a35 75 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20  umber() to make 
24a36 73 75 72 65 20 74 68 65 79 20 61 72 65 20 64 65  sure they are de
24a37 61 6c 69 6e 67 20 77 69 74 68 0a 20 20 2a 2a 20  aling with.  ** 
24a38 61 20 6c 69 62 72 61 72 79 20 74 68 61 74 20 69  a library that i
24a39 73 20 6e 65 77 20 65 6e 6f 75 67 68 20 74 6f 20  s new enough to 
24a3a 73 75 70 70 6f 72 74 20 74 68 61 74 20 41 50 49  support that API
24a3b 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..  ************
24a3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24a3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24a3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24a3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20  *************.  
24a40 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6f 76 65  */.  sqlite3_ove
24a41 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 2c 0a  rload_function,.
24a42 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64  .  /*.  ** Added
24a43 20 61 66 74 65 72 20 33 2e 33 2e 31 33 0a 20 20   after 3.3.13.  
24a44 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65  */.  sqlite3_pre
24a45 70 61 72 65 5f 76 32 2c 0a 20 20 73 71 6c 69 74  pare_v2,.  sqlit
24a46 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 2c  e3_prepare16_v2,
24a47 0a 20 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72  .  sqlite3_clear
24a48 5f 62 69 6e 64 69 6e 67 73 2c 0a 0a 20 20 2f 2a  _bindings,..  /*
24a49 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20  .  ** Added for 
24a4a 33 2e 34 2e 31 0a 20 20 2a 2f 0a 20 20 73 71 6c  3.4.1.  */.  sql
24a4b 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
24a4c 6c 65 5f 76 32 2c 0a 0a 20 20 2f 2a 0a 20 20 2a  le_v2,..  /*.  *
24a4d 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e 35 2e  * Added for 3.5.
24a4e 30 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  0.  */.  sqlite3
24a4f 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 2c 0a  _bind_zeroblob,.
24a50 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62    sqlite3_blob_b
24a51 79 74 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ytes,.  sqlite3_
24a52 62 6c 6f 62 5f 63 6c 6f 73 65 2c 0a 20 20 73 71  blob_close,.  sq
24a53 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 2c  lite3_blob_open,
24a54 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  .  sqlite3_blob_
24a55 72 65 61 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  read,.  sqlite3_
24a56 62 6c 6f 62 5f 77 72 69 74 65 2c 0a 20 20 73 71  blob_write,.  sq
24a57 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
24a58 6c 61 74 69 6f 6e 5f 76 32 2c 0a 20 20 73 71 6c  lation_v2,.  sql
24a59 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
24a5a 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d  l,.  sqlite3_mem
24a5b 6f 72 79 5f 68 69 67 68 77 61 74 65 72 2c 0a 20  ory_highwater,. 
24a5c 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f   sqlite3_memory_
24a5d 75 73 65 64 2c 0a 23 69 66 64 65 66 20 53 51 4c  used,.#ifdef SQL
24a5e 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 20  ITE_MUTEX_OMIT. 
24a5f 20 30 2c 20 0a 20 20 30 2c 20 0a 20 20 30 2c 0a   0, .  0, .  0,.
24a60 20 20 30 2c 0a 20 20 30 2c 0a 23 65 6c 73 65 0a    0,.  0,.#else.
24a61 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
24a62 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33  alloc,.  sqlite3
24a63 5f 6d 75 74 65 78 5f 65 6e 74 65 72 2c 0a 20 20  _mutex_enter,.  
24a64 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72  sqlite3_mutex_fr
24a65 65 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ee,.  sqlite3_mu
24a66 74 65 78 5f 6c 65 61 76 65 2c 0a 20 20 73 71 6c  tex_leave,.  sql
24a67 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 2c 0a  ite3_mutex_try,.
24a68 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
24a69 5f 6f 70 65 6e 5f 76 32 2c 0a 20 20 73 71 6c 69  _open_v2,.  sqli
24a6a 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
24a6b 72 79 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  ry,.  sqlite3_re
24a6c 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
24a6d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
24a6e 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 2c  lt_error_toobig,
24a6f 0a 20 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70  .  sqlite3_sleep
24a70 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 66 74  ,.  sqlite3_soft
24a71 5f 68 65 61 70 5f 6c 69 6d 69 74 2c 0a 20 20 73  _heap_limit,.  s
24a72 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 2c  qlite3_vfs_find,
24a73 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72  .  sqlite3_vfs_r
24a74 65 67 69 73 74 65 72 2c 0a 20 20 73 71 6c 69 74  egister,.  sqlit
24a75 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65  e3_vfs_unregiste
24a76 72 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64  r,..  /*.  ** Ad
24a77 64 65 64 20 66 6f 72 20 33 2e 35 2e 38 0a 20 20  ded for 3.5.8.  
24a78 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 68 72  */.  sqlite3_thr
24a79 65 61 64 73 61 66 65 2c 0a 20 20 73 71 6c 69 74  eadsafe,.  sqlit
24a7a 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c  e3_result_zerobl
24a7b 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  ob,.  sqlite3_re
24a7c 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 2c  sult_error_code,
24a7d 0a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  .  sqlite3_test_
24a7e 63 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74  control,.  sqlit
24a7f 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 2c 0a 20  e3_randomness,. 
24a80 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
24a81 5f 64 62 5f 68 61 6e 64 6c 65 2c 0a 0a 20 20 2f  _db_handle,..  /
24a82 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72  *.  ** Added for
24a83 20 33 2e 36 2e 30 0a 20 20 2a 2f 0a 20 20 73 71   3.6.0.  */.  sq
24a84 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72  lite3_extended_r
24a85 65 73 75 6c 74 5f 63 6f 64 65 73 2c 0a 20 20 73  esult_codes,.  s
24a86 71 6c 69 74 65 33 5f 6c 69 6d 69 74 2c 0a 20 20  qlite3_limit,.  
24a87 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d  sqlite3_next_stm
24a88 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 71 6c  t,.  sqlite3_sql
24a89 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74  ,.  sqlite3_stat
24a8a 75 73 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 74  us,.};../*.** At
24a8b 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 61 6e  tempt to load an
24a8c 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f   SQLite extensio
24a8d 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69  n library contai
24a8e 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 0a  ned in the file.
24a8f 2a 2a 20 7a 46 69 6c 65 2e 20 20 54 68 65 20 65  ** zFile.  The e
24a90 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 20 7a 50  ntry point is zP
24a91 72 6f 63 2e 20 20 7a 50 72 6f 63 20 6d 61 79 20  roc.  zProc may 
24a92 62 65 20 30 20 69 6e 20 77 68 69 63 68 20 63 61  be 0 in which ca
24a93 73 65 20 61 0a 2a 2a 20 64 65 66 61 75 6c 74 20  se a.** default 
24a94 65 6e 74 72 79 20 70 6f 69 6e 74 20 6e 61 6d 65  entry point name
24a95 20 28 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73   (sqlite3_extens
24a96 69 6f 6e 5f 69 6e 69 74 29 20 69 73 20 75 73 65  ion_init) is use
24a97 64 2e 20 20 55 73 65 0a 2a 2a 20 6f 66 20 74 68  d.  Use.** of th
24a98 65 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 69  e default name i
24a99 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a  s recommended..*
24a9a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
24a9b 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
24a9c 20 61 6e 64 20 53 51 4c 49 54 45 5f 45 52 52 4f   and SQLITE_ERRO
24a9d 52 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67  R if something g
24a9e 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a  oes wrong..**.**
24a9f 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
24aa0 75 72 73 20 61 6e 64 20 70 7a 45 72 72 4d 73 67  urs and pzErrMsg
24aa1 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20   is not 0, then 
24aa2 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77  fill *pzErrMsg w
24aa3 69 74 68 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65  ith .** error me
24aa4 73 73 61 67 65 20 74 65 78 74 2e 20 20 54 68 65  ssage text.  The
24aa5 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
24aa6 6e 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74 68  n should free th
24aa7 69 73 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 79 20  is memory.** by 
24aa8 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 44  calling sqlite3D
24aa9 62 46 72 65 65 28 64 62 2c 20 29 2e 0a 2a 2f 0a  bFree(db, )..*/.
24aaa 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
24aab 65 33 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28  e3LoadExtension(
24aac 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
24aad 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64           /* Load
24aae 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69   the extension i
24aaf 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73  nto this databas
24ab0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
24ab1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
24ab2 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ile,    /* Name 
24ab3 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  of the shared li
24ab4 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67  brary containing
24ab5 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20   extension */.  
24ab6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f  const char *zPro
24ab7 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70  c,    /* Entry p
24ab8 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71 6c 69  oint.  Use "sqli
24ab9 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e  te3_extension_in
24aba 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20 63 68  it" if 0 */.  ch
24abb 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
24abc 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72      /* Put error
24abd 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66   message here if
24abe 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 73   not 0 */.){.  s
24abf 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
24ac0 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 76   = db->pVfs;.  v
24ac1 6f 69 64 20 2a 68 61 6e 64 6c 65 3b 0a 20 20 69  oid *handle;.  i
24ac2 6e 74 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69  nt (*xInit)(sqli
24ac3 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73  te3*,char**,cons
24ac4 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
24ac5 75 74 69 6e 65 73 2a 29 3b 0a 20 20 63 68 61 72  utines*);.  char
24ac6 20 2a 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a 20   *zErrmsg = 0;. 
24ac7 20 76 6f 69 64 20 2a 2a 61 48 61 6e 64 6c 65 3b   void **aHandle;
24ac8 0a 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31  ..  /* Ticket #1
24ac9 38 36 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61  863.  To avoid a
24aca 20 63 72 65 61 74 69 6e 67 20 73 65 63 75 72 69   creating securi
24acb 74 79 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  ty problems for 
24acc 6f 6c 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c 69  older.  ** appli
24acd 63 61 74 69 6f 6e 73 20 74 68 61 74 20 72 65 6c  cations that rel
24ace 69 6e 6b 20 61 67 61 69 6e 73 74 20 6e 65 77 65  ink against newe
24acf 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  r versions of SQ
24ad0 4c 69 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20 61  Lite, the.  ** a
24ad1 62 69 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c 6f  bility to run lo
24ad2 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73 20  ad_extension is 
24ad3 74 75 72 6e 65 64 20 6f 66 66 20 62 79 20 64 65  turned off by de
24ad4 66 61 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a  fault.  One.  **
24ad5 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74   must call sqlit
24ad6 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65  e3_enable_load_e
24ad7 78 74 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74 75  xtension() to tu
24ad8 72 6e 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 0a  rn on extension.
24ad9 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20 4f    ** loading.  O
24ada 74 68 65 72 77 69 73 65 20 79 6f 75 20 67 65 74  therwise you get
24adb 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
24adc 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rror..  */.  if(
24add 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
24ade 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69  LITE_LoadExtensi
24adf 6f 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  on)==0 ){.    if
24ae0 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  ( pzErrMsg ){.  
24ae1 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
24ae2 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
24ae3 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22  "not authorized"
24ae4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
24ae5 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
24ae6 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50 72  ;.  }..  if( zPr
24ae7 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50 72  oc==0 ){.    zPr
24ae8 6f 63 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78  oc = "sqlite3_ex
24ae9 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20  tension_init";. 
24aea 20 7d 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20 73   }..  handle = s
24aeb 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 70  qlite3OsDlOpen(p
24aec 56 66 73 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 69  Vfs, zFile);.  i
24aed 66 28 20 68 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a  f( handle==0 ){.
24aee 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
24aef 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a   ){.      char z
24af0 45 72 72 5b 32 35 36 5d 3b 0a 20 20 20 20 20 20  Err[256];.      
24af1 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72  zErr[sizeof(zErr
24af2 29 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20  )-1] = '\0';.   
24af3 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
24af4 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
24af5 2d 31 2c 20 7a 45 72 72 2c 20 0a 20 20 20 20 20  -1, zErr, .     
24af6 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
24af7 6f 70 65 6e 20 73 68 61 72 65 64 20 6c 69 62 72  open shared libr
24af8 61 72 79 20 5b 25 73 5d 22 2c 20 7a 46 69 6c 65  ary [%s]", zFile
24af9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
24afa 4f 73 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20  OsDlError(pVfs, 
24afb 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20  sizeof(zErr)-1, 
24afc 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 2a 70 7a  zErr);.      *pz
24afd 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
24afe 44 62 53 74 72 44 75 70 28 30 2c 20 7a 45 72 72  DbStrDup(0, zErr
24aff 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
24b00 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
24b01 3b 0a 20 20 7d 0a 20 20 78 49 6e 69 74 20 3d 20  ;.  }.  xInit = 
24b02 28 69 6e 74 28 2a 29 28 73 71 6c 69 74 65 33 2a  (int(*)(sqlite3*
24b03 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71  ,char**,const sq
24b04 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
24b05 65 73 2a 29 29 0a 20 20 20 20 20 20 20 20 20 20  es*)).          
24b06 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
24b07 4f 73 44 6c 53 79 6d 28 70 56 66 73 2c 20 68 61  OsDlSym(pVfs, ha
24b08 6e 64 6c 65 2c 20 7a 50 72 6f 63 29 3b 0a 20 20  ndle, zProc);.  
24b09 69 66 28 20 78 49 6e 69 74 3d 3d 30 20 29 7b 0a  if( xInit==0 ){.
24b0a 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
24b0b 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a   ){.      char z
24b0c 45 72 72 5b 32 35 36 5d 3b 0a 20 20 20 20 20 20  Err[256];.      
24b0d 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72  zErr[sizeof(zErr
24b0e 29 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20  )-1] = '\0';.   
24b0f 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
24b10 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
24b11 2d 31 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20  -1, zErr,.      
24b12 20 20 20 20 22 6e 6f 20 65 6e 74 72 79 20 70 6f      "no entry po
24b13 69 6e 74 20 5b 25 73 5d 20 69 6e 20 73 68 61 72  int [%s] in shar
24b14 65 64 20 6c 69 62 72 61 72 79 20 5b 25 73 5d 22  ed library [%s]"
24b15 2c 20 7a 50 72 6f 63 2c 7a 46 69 6c 65 29 3b 0a  , zProc,zFile);.
24b16 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44        sqlite3OsD
24b17 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 73 69 7a  lError(pVfs, siz
24b18 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20 7a 45 72  eof(zErr)-1, zEr
24b19 72 29 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72  r);.      *pzErr
24b1a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53  Msg = sqlite3DbS
24b1b 74 72 44 75 70 28 30 2c 20 7a 45 72 72 29 3b 0a  trDup(0, zErr);.
24b1c 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44        sqlite3OsD
24b1d 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 68 61 6e  lClose(pVfs, han
24b1e 64 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  dle);.    }.    
24b1f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
24b20 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ROR;.  }else if(
24b21 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72   xInit(db, &zErr
24b22 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 41 70 69  msg, &sqlite3Api
24b23 73 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a  s) ){.    if( pz
24b24 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
24b25 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69  *pzErrMsg = sqli
24b26 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 65 72 72  te3_mprintf("err
24b27 6f 72 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61  or during initia
24b28 6c 69 7a 61 74 69 6f 6e 3a 20 25 73 22 2c 20 7a  lization: %s", z
24b29 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 7d 0a 20  Errmsg);.    }. 
24b2a 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
24b2b 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 73 71  zErrmsg);.    sq
24b2c 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70  lite3OsDlClose(p
24b2d 56 66 73 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20  Vfs, handle);.  
24b2e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
24b2f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ERROR;.  }..  /*
24b30 20 41 70 70 65 6e 64 20 74 68 65 20 6e 65 77 20   Append the new 
24b31 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 68  shared library h
24b32 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 64 62 2d  andle to the db-
24b33 3e 61 45 78 74 65 6e 73 69 6f 6e 20 61 72 72 61  >aExtension arra
24b34 79 2e 20 2a 2f 0a 20 20 61 48 61 6e 64 6c 65 20  y. */.  aHandle 
24b35 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
24b36 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
24b37 28 68 61 6e 64 6c 65 29 2a 28 64 62 2d 3e 6e 45  (handle)*(db->nE
24b38 78 74 65 6e 73 69 6f 6e 2b 31 29 29 3b 0a 20 20  xtension+1));.  
24b39 69 66 28 20 61 48 61 6e 64 6c 65 3d 3d 30 20 29  if( aHandle==0 )
24b3a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
24b3b 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
24b3c 20 69 66 28 20 64 62 2d 3e 6e 45 78 74 65 6e 73   if( db->nExtens
24b3d 69 6f 6e 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d  ion>0 ){.    mem
24b3e 63 70 79 28 61 48 61 6e 64 6c 65 2c 20 64 62 2d  cpy(aHandle, db-
24b3f 3e 61 45 78 74 65 6e 73 69 6f 6e 2c 20 73 69 7a  >aExtension, siz
24b40 65 6f 66 28 68 61 6e 64 6c 65 29 2a 64 62 2d 3e  eof(handle)*db->
24b41 6e 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 7d  nExtension);.  }
24b42 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
24b43 28 64 62 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73  (db, db->aExtens
24b44 69 6f 6e 29 3b 0a 20 20 64 62 2d 3e 61 45 78 74  ion);.  db->aExt
24b45 65 6e 73 69 6f 6e 20 3d 20 61 48 61 6e 64 6c 65  ension = aHandle
24b46 3b 0a 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e 73  ;..  db->aExtens
24b47 69 6f 6e 5b 64 62 2d 3e 6e 45 78 74 65 6e 73 69  ion[db->nExtensi
24b48 6f 6e 2b 2b 5d 20 3d 20 68 61 6e 64 6c 65 3b 0a  on++] = handle;.
24b49 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
24b4a 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  OK;.}.SQLITE_API
24b4b 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61   int sqlite3_loa
24b4c 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73  d_extension(.  s
24b4d 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
24b4e 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65       /* Load the
24b4f 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20   extension into 
24b50 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f  this database co
24b51 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f  nnection */.  co
24b52 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c  nst char *zFile,
24b53 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
24b54 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
24b55 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74  y containing ext
24b56 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73  ension */.  cons
24b57 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20  t char *zProc,  
24b58 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74    /* Entry point
24b59 2e 20 20 55 73 65 20 22 73 71 6c 69 74 65 33 5f  .  Use "sqlite3_
24b5a 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 20  extension_init" 
24b5b 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a  if 0 */.  char *
24b5c 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20  *pzErrMsg       
24b5d 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73  /* Put error mes
24b5e 73 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74  sage here if not
24b5f 20 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72   0 */.){.  int r
24b60 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  c;.  sqlite3_mut
24b61 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
24b62 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ex);.  rc = sqli
24b63 74 65 33 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  te3LoadExtension
24b64 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f  (db, zFile, zPro
24b65 63 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20  c, pzErrMsg);.  
24b66 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
24b67 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
24b68 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
24b69 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20  /*.** Call this 
24b6a 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65  routine when the
24b6b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
24b6c 74 69 6f 6e 20 69 73 20 63 6c 6f 73 69 6e 67 20  tion is closing 
24b6d 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 63  in order.** to c
24b6e 6c 65 61 6e 20 75 70 20 6c 6f 61 64 65 64 20 65  lean up loaded e
24b6f 78 74 65 6e 73 69 6f 6e 73 0a 2a 2f 0a 53 51 4c  xtensions.*/.SQL
24b70 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
24b71 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74   sqlite3CloseExt
24b72 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  ensions(sqlite3 
24b73 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
24b74 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
24b75 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
24b76 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28  mutex) );.  for(
24b77 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 45 78 74 65  i=0; i<db->nExte
24b78 6e 73 69 6f 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  nsion; i++){.   
24b79 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73   sqlite3OsDlClos
24b7a 65 28 64 62 2d 3e 70 56 66 73 2c 20 64 62 2d 3e  e(db->pVfs, db->
24b7b 61 45 78 74 65 6e 73 69 6f 6e 5b 69 5d 29 3b 0a  aExtension[i]);.
24b7c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
24b7d 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 45 78 74  ree(db, db->aExt
24b7e 65 6e 73 69 6f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ension);.}../*.*
24b7f 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61  * Enable or disa
24b80 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  ble extension lo
24b81 61 64 69 6e 67 2e 20 20 45 78 74 65 6e 73 69 6f  ading.  Extensio
24b82 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 64 69 73  n loading is dis
24b83 61 62 6c 65 64 20 62 79 0a 2a 2a 20 64 65 66 61  abled by.** defa
24b84 75 6c 74 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f  ult so as not to
24b85 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79 20 68   open security h
24b86 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 61 70  oles in older ap
24b87 70 6c 69 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53  plications..*/.S
24b88 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
24b89 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61  lite3_enable_loa
24b8a 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69  d_extension(sqli
24b8b 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f  te3 *db, int ono
24b8c 66 66 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ff){.  sqlite3_m
24b8d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
24b8e 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f 6e 6f  utex);.  if( ono
24b8f 66 66 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c  ff ){.    db->fl
24b90 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4c 6f  ags |= SQLITE_Lo
24b91 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d  adExtension;.  }
24b92 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 66 6c  else{.    db->fl
24b93 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c  ags &= ~SQLITE_L
24b94 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20  oadExtension;.  
24b95 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
24b96 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
24b97 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
24b98 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69  ITE_OK;.}..#endi
24b99 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
24b9a 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20  _LOAD_EXTENSION 
24b9b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 75  */../*.** The au
24b9c 74 6f 2d 65 78 74 65 6e 73 69 6f 6e 20 63 6f 64  to-extension cod
24b9d 65 20 61 64 64 65 64 20 72 65 67 61 72 64 6c 65  e added regardle
24b9e 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72  ss of whether or
24b9f 20 6e 6f 74 20 65 78 74 65 6e 73 69 6f 6e 0a 2a   not extension.*
24ba0 2a 20 6c 6f 61 64 69 6e 67 20 69 73 20 73 75 70  * loading is sup
24ba1 70 6f 72 74 65 64 2e 20 20 57 65 20 6e 65 65 64  ported.  We need
24ba2 20 61 20 64 75 6d 6d 79 20 73 71 6c 69 74 65 33   a dummy sqlite3
24ba3 41 70 69 73 20 70 6f 69 6e 74 65 72 20 66 6f 72  Apis pointer for
24ba4 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 20 69 66   that.** code if
24ba5 20 72 65 67 75 6c 61 72 20 65 78 74 65 6e 73 69   regular extensi
24ba6 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6e 6f  on loading is no
24ba7 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 54 68  t available.  Th
24ba8 69 73 20 69 73 20 74 68 61 74 0a 2a 2a 20 64 75  is is that.** du
24ba9 6d 6d 79 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  mmy pointer..*/.
24baa 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
24bab 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
24bac 4e 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  N.static const s
24bad 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
24bae 6e 65 73 20 73 71 6c 69 74 65 33 41 70 69 73 20  nes sqlite3Apis 
24baf 3d 20 7b 20 30 20 7d 3b 0a 23 65 6e 64 69 66 0a  = { 0 };.#endif.
24bb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
24bb1 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20 68 6f 6c  owing object hol
24bb2 64 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61  ds the list of a
24bb3 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6c 6f 61  utomatically loa
24bb4 64 65 64 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e  ded.** extension
24bb5 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 69  s..**.** This li
24bb6 73 74 20 69 73 20 73 68 61 72 65 64 20 61 63 72  st is shared acr
24bb7 6f 73 73 20 74 68 72 65 61 64 73 2e 20 20 54 68  oss threads.  Th
24bb8 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
24bb9 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20  TATIC_MASTER.** 
24bba 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
24bbb 6c 64 20 77 68 69 6c 65 20 61 63 63 65 73 73 69  ld while accessi
24bbc 6e 67 20 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2f  ng this list..*/
24bbd 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
24bbe 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69  sqlite3AutoExtLi
24bbf 73 74 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78  st sqlite3AutoEx
24bc0 74 4c 69 73 74 3b 0a 73 74 61 74 69 63 20 53 51  tList;.static SQ
24bc1 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
24bc2 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69  sqlite3AutoExtLi
24bc3 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 74 3b  st {.  int nExt;
24bc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24bc5 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
24bc6 65 73 20 69 6e 20 61 45 78 74 5b 5d 20 2a 2f 20  es in aExt[] */ 
24bc7 20 20 20 20 20 20 20 20 20 0a 20 20 76 6f 69 64           .  void
24bc8 20 28 2a 2a 61 45 78 74 29 28 76 6f 69 64 29 3b   (**aExt)(void);
24bc9 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74     /* Pointers t
24bca 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  o the extension 
24bcb 69 6e 69 74 20 66 75 6e 63 74 69 6f 6e 73 20 2a  init functions *
24bcc 2f 0a 7d 20 73 71 6c 69 74 65 33 41 75 74 6f 65  /.} sqlite3Autoe
24bcd 78 74 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a  xt = { 0, 0 };..
24bce 2f 2a 20 54 68 65 20 22 77 73 64 41 75 74 6f 65  /* The "wsdAutoe
24bcf 78 74 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72  xt" macro will r
24bd0 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 75  esolve to the au
24bd1 74 6f 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 73  toextension.** s
24bd2 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66  tate vector.  If
24bd3 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63   writable static
24bd4 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f   data is unsuppo
24bd5 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67  rted on the targ
24bd6 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74  et,.** we have t
24bd7 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61  o locate the sta
24bd8 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e  te vector at run
24bd9 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d  -time.  In the m
24bda 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61  ore common.** ca
24bdb 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c  se where writabl
24bdc 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73  e static data is
24bdd 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53   supported, wsdS
24bde 74 61 74 20 63 61 6e 20 72 65 66 65 72 20 64 69  tat can refer di
24bdf 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65  rectly.** to the
24be0 20 22 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74   "sqlite3Autoext
24be1 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64  " state vector d
24be2 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a  eclared above..*
24be3 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
24be4 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e  OMIT_WSD.# defin
24be5 65 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74  e wsdAutoextInit
24be6 20 5c 0a 20 20 73 71 6c 69 74 65 33 41 75 74 6f   \.  sqlite3Auto
24be7 45 78 74 4c 69 73 74 20 2a 78 20 3d 20 26 47 4c  ExtList *x = &GL
24be8 4f 42 41 4c 28 73 71 6c 69 74 65 33 41 75 74 6f  OBAL(sqlite3Auto
24be9 45 78 74 4c 69 73 74 2c 73 71 6c 69 74 65 33 41  ExtList,sqlite3A
24bea 75 74 6f 65 78 74 29 0a 23 20 64 65 66 69 6e 65  utoext).# define
24beb 20 77 73 64 41 75 74 6f 65 78 74 20 78 5b 30 5d   wsdAutoext x[0]
24bec 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
24bed 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 0a 23  wsdAutoextInit.#
24bee 20 64 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65   define wsdAutoe
24bef 78 74 20 73 71 6c 69 74 65 33 41 75 74 6f 65 78  xt sqlite3Autoex
24bf0 74 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  t.#endif.../*.**
24bf1 20 52 65 67 69 73 74 65 72 20 61 20 73 74 61 74   Register a stat
24bf2 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78  ically linked ex
24bf3 74 65 6e 73 69 6f 6e 20 74 68 61 74 20 69 73 20  tension that is 
24bf4 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a  automatically.**
24bf5 20 6c 6f 61 64 65 64 20 62 79 20 65 76 65 72 79   loaded by every
24bf6 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f   new database co
24bf7 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nnection..*/.SQL
24bf8 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
24bf9 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69  te3_auto_extensi
24bfa 6f 6e 28 76 6f 69 64 20 28 2a 78 49 6e 69 74 29  on(void (*xInit)
24bfb 28 76 6f 69 64 29 29 7b 0a 20 20 69 6e 74 20 72  (void)){.  int r
24bfc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23  c = SQLITE_OK;.#
24bfd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24bfe 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63  IT_AUTOINIT.  rc
24bff 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69   = sqlite3_initi
24c00 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72  alize();.  if( r
24c01 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
24c02 72 63 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  rc;.  }else.#end
24c03 69 66 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69  if.  {.    int i
24c04 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ;.#if SQLITE_THR
24c05 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69  EADSAFE.    sqli
24c06 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
24c07 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
24c08 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
24c09 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
24c0a 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 77 73 64  ;.#endif.    wsd
24c0b 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 20  AutoextInit;.   
24c0c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
24c0d 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20  nter(mutex);.   
24c0e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 77 73 64 41   for(i=0; i<wsdA
24c0f 75 74 6f 65 78 74 2e 6e 45 78 74 3b 20 69 2b 2b  utoext.nExt; i++
24c10 29 7b 0a 20 20 20 20 20 20 69 66 28 20 77 73 64  ){.      if( wsd
24c11 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3d  Autoext.aExt[i]=
24c12 3d 78 49 6e 69 74 20 29 20 62 72 65 61 6b 3b 0a  =xInit ) break;.
24c13 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3d      }.    if( i=
24c14 3d 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74  =wsdAutoext.nExt
24c15 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 42   ){.      int nB
24c16 79 74 65 20 3d 20 28 77 73 64 41 75 74 6f 65 78  yte = (wsdAutoex
24c17 74 2e 6e 45 78 74 2b 31 29 2a 73 69 7a 65 6f 66  t.nExt+1)*sizeof
24c18 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74  (wsdAutoext.aExt
24c19 5b 30 5d 29 3b 0a 20 20 20 20 20 20 76 6f 69 64  [0]);.      void
24c1a 20 28 2a 2a 61 4e 65 77 29 28 76 6f 69 64 29 3b   (**aNew)(void);
24c1b 0a 20 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71  .      aNew = sq
24c1c 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 77 73  lite3_realloc(ws
24c1d 64 41 75 74 6f 65 78 74 2e 61 45 78 74 2c 20 6e  dAutoext.aExt, n
24c1e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Byte);.      if(
24c1f 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   aNew==0 ){.    
24c20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
24c21 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c  NOMEM;.      }el
24c22 73 65 7b 0a 20 20 20 20 20 20 20 20 77 73 64 41  se{.        wsdA
24c23 75 74 6f 65 78 74 2e 61 45 78 74 20 3d 20 61 4e  utoext.aExt = aN
24c24 65 77 3b 0a 20 20 20 20 20 20 20 20 77 73 64 41  ew;.        wsdA
24c25 75 74 6f 65 78 74 2e 61 45 78 74 5b 77 73 64 41  utoext.aExt[wsdA
24c26 75 74 6f 65 78 74 2e 6e 45 78 74 5d 20 3d 20 78  utoext.nExt] = x
24c27 49 6e 69 74 3b 0a 20 20 20 20 20 20 20 20 77 73  Init;.        ws
24c28 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 2b 2b 3b  dAutoext.nExt++;
24c29 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
24c2a 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
24c2b 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20  _leave(mutex);. 
24c2c 20 20 20 61 73 73 65 72 74 28 20 28 72 63 26 30     assert( (rc&0
24c2d 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 20 20  xff)==rc );.    
24c2e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d  return rc;.  }.}
24c2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68  ../*.** Reset th
24c30 65 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  e automatic exte
24c31 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 65  nsion loading me
24c32 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 53 51 4c 49  chanism..*/.SQLI
24c33 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
24c34 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65  te3_reset_auto_e
24c35 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 7b 0a  xtension(void){.
24c36 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
24c37 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69  MIT_AUTOINIT.  i
24c38 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  f( sqlite3_initi
24c39 61 6c 69 7a 65 28 29 3d 3d 53 51 4c 49 54 45 5f  alize()==SQLITE_
24c3a 4f 4b 20 29 0a 23 65 6e 64 69 66 0a 20 20 7b 0a  OK ).#endif.  {.
24c3b 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
24c3c 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65  DSAFE.    sqlite
24c3d 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d  3_mutex *mutex =
24c3e 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
24c3f 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
24c40 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
24c41 23 65 6e 64 69 66 0a 20 20 20 20 77 73 64 41 75  #endif.    wsdAu
24c42 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 20 20 73  toextInit;.    s
24c43 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
24c44 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73  er(mutex);.    s
24c45 71 6c 69 74 65 33 5f 66 72 65 65 28 77 73 64 41  qlite3_free(wsdA
24c46 75 74 6f 65 78 74 2e 61 45 78 74 29 3b 0a 20 20  utoext.aExt);.  
24c47 20 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78    wsdAutoext.aEx
24c48 74 20 3d 20 30 3b 0a 20 20 20 20 77 73 64 41 75  t = 0;.    wsdAu
24c49 74 6f 65 78 74 2e 6e 45 78 74 20 3d 20 30 3b 0a  toext.nExt = 0;.
24c4a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
24c4b 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
24c4c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61    }.}../*.** Loa
24c4d 64 20 61 6c 6c 20 61 75 74 6f 6d 61 74 69 63 20  d all automatic 
24c4e 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
24c4f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
24c50 74 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61  t sqlite3AutoLoa
24c51 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69  dExtensions(sqli
24c52 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
24c53 69 3b 0a 20 20 69 6e 74 20 67 6f 20 3d 20 31 3b  i;.  int go = 1;
24c54 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
24c55 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 28 2a 78  TE_OK;.  int (*x
24c56 49 6e 69 74 29 28 73 71 6c 69 74 65 33 2a 2c 63  Init)(sqlite3*,c
24c57 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69  har**,const sqli
24c58 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
24c59 2a 29 3b 0a 0a 20 20 77 73 64 41 75 74 6f 65 78  *);..  wsdAutoex
24c5a 74 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64  tInit;.  if( wsd
24c5b 41 75 74 6f 65 78 74 2e 6e 45 78 74 3d 3d 30 20  Autoext.nExt==0 
24c5c 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 6d 6f 6e  ){.    /* Common
24c5d 20 63 61 73 65 3a 20 65 61 72 6c 79 20 6f 75 74   case: early out
24c5e 20 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 68   without every h
24c5f 61 76 69 6e 67 20 74 6f 20 61 63 71 75 69 72 65  aving to acquire
24c60 20 61 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20   a mutex */.    
24c61 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
24c62 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
24c63 20 67 6f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63   go; i++){.    c
24c64 68 61 72 20 2a 7a 45 72 72 6d 73 67 20 3d 20 30  har *zErrmsg = 0
24c65 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ;.#if SQLITE_THR
24c66 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69  EADSAFE.    sqli
24c67 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
24c68 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
24c69 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
24c6a 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
24c6b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c  ;.#endif.    sql
24c6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
24c6d 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28  (mutex);.    if(
24c6e 20 69 3e 3d 77 73 64 41 75 74 6f 65 78 74 2e 6e   i>=wsdAutoext.n
24c6f 45 78 74 20 29 7b 0a 20 20 20 20 20 20 78 49 6e  Ext ){.      xIn
24c70 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f  it = 0;.      go
24c71 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
24c72 0a 20 20 20 20 20 20 78 49 6e 69 74 20 3d 20 28  .      xInit = (
24c73 69 6e 74 28 2a 29 28 73 71 6c 69 74 65 33 2a 2c  int(*)(sqlite3*,
24c74 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c  char**,const sql
24c75 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
24c76 73 2a 29 29 0a 20 20 20 20 20 20 20 20 20 20 20  s*)).           
24c77 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45     wsdAutoext.aE
24c78 78 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  xt[i];.    }.   
24c79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
24c7a 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 20  eave(mutex);.   
24c7b 20 69 66 28 20 78 49 6e 69 74 20 26 26 20 78 49   if( xInit && xI
24c7c 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73 67  nit(db, &zErrmsg
24c7d 2c 20 26 73 71 6c 69 74 65 33 41 70 69 73 29 20  , &sqlite3Apis) 
24c7e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
24c7f 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
24c80 5f 45 52 52 4f 52 2c 0a 20 20 20 20 20 20 20 20  _ERROR,.        
24c81 20 20 20 20 22 61 75 74 6f 6d 61 74 69 63 20 65      "automatic e
24c82 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
24c83 20 66 61 69 6c 65 64 3a 20 25 73 22 2c 20 7a 45   failed: %s", zE
24c84 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 67 6f  rrmsg);.      go
24c85 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
24c86 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
24c87 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
24c88 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20  e(zErrmsg);.    
24c89 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
24c8a 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  c;.}../*********
24c8b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f 61  ***** End of loa
24c8c 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  dext.c *********
24c8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24c8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24c8f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
24c90 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
24c91 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a   pragma.c ******
24c92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24c93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24c94 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33  ****/./*.** 2003
24c95 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54   April 6.**.** T
24c96 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
24c97 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
24c98 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
24c99 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
24c9a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
24c9b 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
24c9c 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
24c9d 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
24c9e 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
24c9f 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
24ca0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
24ca1 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
24ca2 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
24ca3 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
24ca4 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
24ca5 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
24ca6 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
24ca7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24ca8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24ca9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24caa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24cab 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
24cac 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
24cad 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
24cae 6d 65 6e 74 20 74 68 65 20 50 52 41 47 4d 41 20  ment the PRAGMA 
24caf 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24  command..**.** $
24cb0 49 64 3a 20 70 72 61 67 6d 61 2e 63 2c 76 20 31  Id: pragma.c,v 1
24cb1 2e 32 30 31 20 32 30 30 39 2f 30 31 2f 31 33 20  .201 2009/01/13 
24cb2 32 30 3a 31 34 3a 31 36 20 64 72 68 20 45 78 70  20:14:16 drh Exp
24cb3 20 24 0a 2a 2f 0a 0a 2f 2a 20 49 67 6e 6f 72 65   $.*/../* Ignore
24cb4 20 74 68 69 73 20 77 68 6f 6c 65 20 66 69 6c 65   this whole file
24cb5 20 69 66 20 70 72 61 67 6d 61 73 20 61 72 65 20   if pragmas are 
24cb6 64 69 73 61 62 6c 65 64 0a 2a 2f 0a 23 69 66 20  disabled.*/.#if 
24cb7 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
24cb8 4f 4d 49 54 5f 50 52 41 47 4d 41 29 20 26 26 20  OMIT_PRAGMA) && 
24cb9 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
24cba 4f 4d 49 54 5f 50 41 52 53 45 52 29 0a 0a 2f 2a  OMIT_PARSER)../*
24cbb 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68  .** Interpret th
24cbc 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61  e given string a
24cbd 73 20 61 20 73 61 66 65 74 79 20 6c 65 76 65 6c  s a safety level
24cbe 2e 20 20 52 65 74 75 72 6e 20 30 20 66 6f 72 20  .  Return 0 for 
24cbf 4f 46 46 2c 0a 2a 2a 20 31 20 66 6f 72 20 4f 4e  OFF,.** 1 for ON
24cc0 20 6f 72 20 4e 4f 52 4d 41 4c 20 61 6e 64 20 32   or NORMAL and 2
24cc1 20 66 6f 72 20 46 55 4c 4c 2e 20 20 52 65 74 75   for FULL.  Retu
24cc2 72 6e 20 31 20 66 6f 72 20 61 6e 20 65 6d 70 74  rn 1 for an empt
24cc3 79 20 6f 72 20 0a 2a 2a 20 75 6e 72 65 63 6f 67  y or .** unrecog
24cc4 6e 69 7a 65 64 20 73 74 72 69 6e 67 20 61 72 67  nized string arg
24cc5 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  ument..**.** Not
24cc6 65 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  e that the value
24cc7 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 20 6f  s returned are o
24cc8 6e 65 20 6c 65 73 73 20 74 68 61 74 20 74 68 65  ne less that the
24cc9 20 76 61 6c 75 65 73 20 74 68 61 74 0a 2a 2a 20   values that.** 
24cca 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
24ccb 20 69 6e 74 6f 20 73 71 6c 69 74 65 33 42 74 72   into sqlite3Btr
24ccc 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c  eeSetSafetyLevel
24ccd 28 29 2e 20 20 54 68 65 20 69 73 20 64 6f 6e 65  ().  The is done
24cce 0a 2a 2a 20 74 6f 20 73 75 70 70 6f 72 74 20 6c  .** to support l
24ccf 65 67 61 63 79 20 53 51 4c 20 63 6f 64 65 2e 20  egacy SQL code. 
24cd0 20 54 68 65 20 73 61 66 65 74 79 20 6c 65 76 65   The safety leve
24cd1 6c 20 75 73 65 64 20 74 6f 20 62 65 20 62 6f 6f  l used to be boo
24cd2 6c 65 61 6e 0a 2a 2a 20 61 6e 64 20 6f 6c 64 65  lean.** and olde
24cd3 72 20 73 63 72 69 70 74 73 20 6d 61 79 20 68 61  r scripts may ha
24cd4 76 65 20 75 73 65 64 20 6e 75 6d 62 65 72 73 20  ve used numbers 
24cd5 30 20 66 6f 72 20 4f 46 46 20 61 6e 64 20 31 20  0 for OFF and 1 
24cd6 66 6f 72 20 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69  for ON..*/.stati
24cd7 63 20 75 38 20 67 65 74 53 61 66 65 74 79 4c 65  c u8 getSafetyLe
24cd8 76 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  vel(const char *
24cd9 7a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  z){.            
24cda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24cdb 20 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32   /* 123456789 12
24cdc 33 34 35 36 37 38 39 20 2a 2f 0a 20 20 73 74 61  3456789 */.  sta
24cdd 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  tic const char z
24cde 54 65 78 74 5b 5d 20 3d 20 22 6f 6e 6f 66 66 61  Text[] = "onoffa
24cdf 6c 73 65 79 65 73 74 72 75 65 66 75 6c 6c 22 3b  lseyestruefull";
24ce0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
24ce1 75 38 20 69 4f 66 66 73 65 74 5b 5d 20 3d 20 7b  u8 iOffset[] = {
24ce2 30 2c 20 31 2c 20 32 2c 20 34 2c 20 39 2c 20 31  0, 1, 2, 4, 9, 1
24ce3 32 2c 20 31 36 7d 3b 0a 20 20 73 74 61 74 69 63  2, 16};.  static
24ce4 20 63 6f 6e 73 74 20 75 38 20 69 4c 65 6e 67 74   const u8 iLengt
24ce5 68 5b 5d 20 3d 20 7b 32 2c 20 32 2c 20 33 2c 20  h[] = {2, 2, 3, 
24ce6 35 2c 20 33 2c 20 34 2c 20 34 7d 3b 0a 20 20 73  5, 3, 4, 4};.  s
24ce7 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69  tatic const u8 i
24ce8 56 61 6c 75 65 5b 5d 20 3d 20 20 7b 31 2c 20 30  Value[] =  {1, 0
24ce9 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 32 7d  , 0, 0, 1, 1, 2}
24cea 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  ;.  int i, n;.  
24ceb 69 66 28 20 69 73 64 69 67 69 74 28 2a 7a 29 20  if( isdigit(*z) 
24cec 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 75  ){.    return (u
24ced 38 29 61 74 6f 69 28 7a 29 3b 0a 20 20 7d 0a 20  8)atoi(z);.  }. 
24cee 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
24cef 65 6e 33 30 28 7a 29 3b 0a 20 20 66 6f 72 28 69  en30(z);.  for(i
24cf0 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
24cf1 69 4c 65 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0a  iLength); i++){.
24cf2 20 20 20 20 69 66 28 20 69 4c 65 6e 67 74 68 5b      if( iLength[
24cf3 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33  i]==n && sqlite3
24cf4 53 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b  StrNICmp(&zText[
24cf5 69 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29  iOffset[i]],z,n)
24cf6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
24cf7 75 72 6e 20 69 56 61 6c 75 65 5b 69 5d 3b 0a 20  urn iValue[i];. 
24cf8 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
24cf9 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 1;.}../*.** In
24cfa 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65  terpret the give
24cfb 6e 20 73 74 72 69 6e 67 20 61 73 20 61 20 62 6f  n string as a bo
24cfc 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a  olean value..*/.
24cfd 73 74 61 74 69 63 20 75 38 20 67 65 74 42 6f 6f  static u8 getBoo
24cfe 6c 65 61 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  lean(const char 
24cff 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 65  *z){.  return ge
24d00 74 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 29 26  tSafetyLevel(z)&
24d01 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  1;.}../*.** Inte
24d02 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20  rpret the given 
24d03 73 74 72 69 6e 67 20 61 73 20 61 20 6c 6f 63 6b  string as a lock
24d04 69 6e 67 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a  ing mode value..
24d05 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
24d06 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e  tLockingMode(con
24d07 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
24d08 66 28 20 7a 20 29 7b 0a 20 20 20 20 69 66 28 20  f( z ){.    if( 
24d09 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
24d0a 70 28 7a 2c 20 22 65 78 63 6c 75 73 69 76 65 22  p(z, "exclusive"
24d0b 29 20 29 20 72 65 74 75 72 6e 20 50 41 47 45 52  ) ) return PAGER
24d0c 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
24d0d 4c 55 53 49 56 45 3b 0a 20 20 20 20 69 66 28 20  LUSIVE;.    if( 
24d0e 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
24d0f 70 28 7a 2c 20 22 6e 6f 72 6d 61 6c 22 29 20 29  p(z, "normal") )
24d10 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f   return PAGER_LO
24d11 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
24d12 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 50  ;.  }.  return P
24d13 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
24d14 5f 51 55 45 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64  _QUERY;.}..#ifnd
24d15 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
24d16 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
24d17 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69  Interpret the gi
24d18 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 6e  ven string as an
24d19 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64   auto-vacuum mod
24d1a 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  e value..**.** T
24d1b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
24d1c 69 6e 67 73 2c 20 22 6e 6f 6e 65 22 2c 20 22 66  ings, "none", "f
24d1d 75 6c 6c 22 20 61 6e 64 20 22 69 6e 63 72 65 6d  ull" and "increm
24d1e 65 6e 74 61 6c 22 20 61 72 65 20 0a 2a 2a 20 61  ental" are .** a
24d1f 63 63 65 70 74 61 62 6c 65 2c 20 61 73 20 61 72  cceptable, as ar
24d20 65 20 74 68 65 69 72 20 6e 75 6d 65 72 69 63 20  e their numeric 
24d21 65 71 75 69 76 61 6c 65 6e 74 73 3a 20 30 2c 20  equivalents: 0, 
24d22 31 20 61 6e 64 20 32 20 72 65 73 70 65 63 74 69  1 and 2 respecti
24d23 76 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  vely..*/.static 
24d24 69 6e 74 20 67 65 74 41 75 74 6f 56 61 63 75 75  int getAutoVacuu
24d25 6d 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  m(const char *z)
24d26 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
24d27 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
24d28 6d 70 28 7a 2c 20 22 6e 6f 6e 65 22 29 20 29 20  mp(z, "none") ) 
24d29 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54  return BTREE_AUT
24d2a 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20  OVACUUM_NONE;.  
24d2b 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
24d2c 72 49 43 6d 70 28 7a 2c 20 22 66 75 6c 6c 22 29  rICmp(z, "full")
24d2d 20 29 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f   ) return BTREE_
24d2e 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3b  AUTOVACUUM_FULL;
24d2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65  .  if( 0==sqlite
24d30 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 69 6e 63  3StrICmp(z, "inc
24d31 72 65 6d 65 6e 74 61 6c 22 29 20 29 20 72 65 74  remental") ) ret
24d32 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41  urn BTREE_AUTOVA
24d33 43 55 55 4d 5f 49 4e 43 52 3b 0a 20 20 69 20 3d  CUUM_INCR;.  i =
24d34 20 61 74 6f 69 28 7a 29 3b 0a 20 20 72 65 74 75   atoi(z);.  retu
24d35 72 6e 20 28 75 38 29 28 28 69 3e 3d 30 26 26 69  rn (u8)((i>=0&&i
24d36 3c 3d 32 29 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e  <=2)?i:0);.}.#en
24d37 64 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51  dif /* ifndef SQ
24d38 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
24d39 43 55 55 4d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  CUUM */..#ifndef
24d3a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
24d3b 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a  ER_PRAGMAS./*.**
24d3c 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67   Interpret the g
24d3d 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61  iven string as a
24d3e 20 74 65 6d 70 20 64 62 20 6c 6f 63 61 74 69 6f   temp db locatio
24d3f 6e 2e 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20  n. Return 1 for 
24d40 66 69 6c 65 0a 2a 2a 20 62 61 63 6b 65 64 20 74  file.** backed t
24d41 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
24d42 65 73 2c 20 32 20 66 6f 72 20 74 68 65 20 52 65  es, 2 for the Re
24d43 64 2d 42 6c 61 63 6b 20 74 72 65 65 20 69 6e 20  d-Black tree in 
24d44 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0a  memory database.
24d45 2a 2a 20 61 6e 64 20 30 20 74 6f 20 75 73 65 20  ** and 0 to use 
24d46 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  the compile-time
24d47 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61   default..*/.sta
24d48 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 53  tic int getTempS
24d49 74 6f 72 65 28 63 6f 6e 73 74 20 63 68 61 72 20  tore(const char 
24d4a 2a 7a 29 7b 0a 20 20 69 66 28 20 7a 5b 30 5d 3e  *z){.  if( z[0]>
24d4b 3d 27 30 27 20 26 26 20 7a 5b 30 5d 3c 3d 27 32  ='0' && z[0]<='2
24d4c 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ' ){.    return 
24d4d 7a 5b 30 5d 20 2d 20 27 30 27 3b 0a 20 20 7d 65  z[0] - '0';.  }e
24d4e 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53  lse if( sqlite3S
24d4f 74 72 49 43 6d 70 28 7a 2c 20 22 66 69 6c 65 22  trICmp(z, "file"
24d50 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
24d51 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
24d52 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
24d53 28 7a 2c 20 22 6d 65 6d 6f 72 79 22 29 3d 3d 30  (z, "memory")==0
24d54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   ){.    return 2
24d55 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
24d56 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23  eturn 0;.  }.}.#
24d57 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
24d58 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
24d59 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
24d5a 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
24d5b 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69  MAS./*.** Invali
24d5c 64 61 74 65 20 74 65 6d 70 20 73 74 6f 72 61 67  date temp storag
24d5d 65 2c 20 65 69 74 68 65 72 20 77 68 65 6e 20 74  e, either when t
24d5e 68 65 20 74 65 6d 70 20 73 74 6f 72 61 67 65 20  he temp storage 
24d5f 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 66 72  is changed.** fr
24d60 6f 6d 20 64 65 66 61 75 6c 74 2c 20 6f 72 20 77  om default, or w
24d61 68 65 6e 20 27 66 69 6c 65 27 20 61 6e 64 20 74  hen 'file' and t
24d62 68 65 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  he temp_store_di
24d63 72 65 63 74 6f 72 79 20 68 61 73 20 63 68 61 6e  rectory has chan
24d64 67 65 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ged.*/.static in
24d65 74 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70  t invalidateTemp
24d66 53 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a 70  Storage(Parse *p
24d67 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
24d68 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
24d69 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 44  db;.  if( db->aD
24d6a 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20  b[1].pBt!=0 ){. 
24d6b 20 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f     if( !db->auto
24d6c 43 6f 6d 6d 69 74 20 7c 7c 20 73 71 6c 69 74 65  Commit || sqlite
24d6d 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72  3BtreeIsInReadTr
24d6e 61 6e 73 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ans(db->aDb[1].p
24d6f 42 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Bt) ){.      sql
24d70 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
24d71 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20  rse, "temporary 
24d72 73 74 6f 72 61 67 65 20 63 61 6e 6e 6f 74 20 62  storage cannot b
24d73 65 20 63 68 61 6e 67 65 64 20 22 0a 20 20 20 20  e changed ".    
24d74 20 20 20 20 22 66 72 6f 6d 20 77 69 74 68 69 6e      "from within
24d75 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29   a transaction")
24d76 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
24d77 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
24d78 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74   }.    sqlite3Bt
24d79 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62  reeClose(db->aDb
24d7a 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 64 62  [1].pBt);.    db
24d7b 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 20 3d 20 30  ->aDb[1].pBt = 0
24d7c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  ;.    sqlite3Res
24d7d 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
24d7e 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  (db, 0);.  }.  r
24d7f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
24d80 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
24d81 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  ITE_PAGER_PRAGMA
24d82 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  S */..#ifndef SQ
24d83 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
24d84 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 66  PRAGMAS./*.** If
24d85 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
24d86 73 65 20 69 73 20 6f 70 65 6e 2c 20 63 6c 6f 73  se is open, clos
24d87 65 20 69 74 20 61 6e 64 20 6d 61 72 6b 20 74 68  e it and mark th
24d88 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
24d89 61 0a 2a 2a 20 61 73 20 6e 65 65 64 69 6e 67 20  a.** as needing 
24d8a 72 65 6c 6f 61 64 69 6e 67 2e 20 20 54 68 69 73  reloading.  This
24d8b 20 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 77 68   must be done wh
24d8c 65 6e 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c  en using the SQL
24d8d 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 2a  ITE_TEMP_STORE.*
24d8e 2a 20 6f 72 20 44 45 46 41 55 4c 54 5f 54 45 4d  * or DEFAULT_TEM
24d8f 50 5f 53 54 4f 52 45 20 70 72 61 67 6d 61 73 2e  P_STORE pragmas.
24d90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
24d91 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65  hangeTempStorage
24d92 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
24d93 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 6f  const char *zSto
24d94 72 61 67 65 54 79 70 65 29 7b 0a 20 20 69 6e 74  rageType){.  int
24d95 20 74 73 20 3d 20 67 65 74 54 65 6d 70 53 74 6f   ts = getTempSto
24d96 72 65 28 7a 53 74 6f 72 61 67 65 54 79 70 65 29  re(zStorageType)
24d97 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
24d98 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
24d99 69 66 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f  if( db->temp_sto
24d9a 72 65 3d 3d 74 73 20 29 20 72 65 74 75 72 6e 20  re==ts ) return 
24d9b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
24d9c 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53   invalidateTempS
24d9d 74 6f 72 61 67 65 28 20 70 50 61 72 73 65 20 29  torage( pParse )
24d9e 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b   != SQLITE_OK ){
24d9f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
24da0 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
24da1 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 3d  db->temp_store =
24da2 20 28 75 38 29 74 73 3b 0a 20 20 72 65 74 75 72   (u8)ts;.  retur
24da3 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
24da4 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
24da5 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
24da6 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
24da7 20 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e 20   code to return 
24da8 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72  a single integer
24da9 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
24daa 63 20 76 6f 69 64 20 72 65 74 75 72 6e 53 69 6e  c void returnSin
24dab 67 6c 65 49 6e 74 28 50 61 72 73 65 20 2a 70 50  gleInt(Parse *pP
24dac 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
24dad 20 2a 7a 4c 61 62 65 6c 2c 20 69 6e 74 20 76 61   *zLabel, int va
24dae 6c 75 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  lue){.  Vdbe *v 
24daf 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
24db0 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20  (pParse);.  int 
24db1 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mem = ++pParse->
24db2 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 56  nMem;.  sqlite3V
24db3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24db4 49 6e 74 65 67 65 72 2c 20 76 61 6c 75 65 2c 20  Integer, value, 
24db5 6d 65 6d 29 3b 0a 20 20 69 66 28 20 70 50 61 72  mem);.  if( pPar
24db6 73 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29  se->explain==0 )
24db7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
24db8 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
24db9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
24dba 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
24dbb 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
24dbc 20 7a 4c 61 62 65 6c 2c 20 53 51 4c 49 54 45 5f   zLabel, SQLITE_
24dbd 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20 73  STATIC);.  }.  s
24dbe 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24dbf 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
24dc0 2c 20 6d 65 6d 2c 20 31 29 3b 0a 7d 0a 0a 23 69  , mem, 1);.}..#i
24dc1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24dc2 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 2f  T_FLAG_PRAGMAS./
24dc3 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  *.** Check to se
24dc4 65 20 69 66 20 7a 52 69 67 68 74 20 61 6e 64 20  e if zRight and 
24dc5 7a 4c 65 66 74 20 72 65 66 65 72 20 74 6f 20 61  zLeft refer to a
24dc6 20 70 72 61 67 6d 61 20 74 68 61 74 20 71 75 65   pragma that que
24dc7 72 69 65 73 0a 2a 2a 20 6f 72 20 63 68 61 6e 67  ries.** or chang
24dc8 65 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c  es one of the fl
24dc9 61 67 73 20 69 6e 20 64 62 2d 3e 66 6c 61 67 73  ags in db->flags
24dca 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 73  .  Return 1 if s
24dcb 6f 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a  o and 0 if not..
24dcc 2a 2a 20 41 6c 73 6f 2c 20 69 6d 70 6c 65 6d 65  ** Also, impleme
24dcd 6e 74 20 74 68 65 20 70 72 61 67 6d 61 2e 0a 2a  nt the pragma..*
24dce 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61  /.static int fla
24dcf 67 50 72 61 67 6d 61 28 50 61 72 73 65 20 2a 70  gPragma(Parse *p
24dd0 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
24dd1 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20  r *zLeft, const 
24dd2 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20  char *zRight){. 
24dd3 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74   static const st
24dd4 72 75 63 74 20 73 50 72 61 67 6d 61 54 79 70 65  ruct sPragmaType
24dd5 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61   {.    const cha
24dd6 72 20 2a 7a 4e 61 6d 65 3b 20 20 2f 2a 20 4e 61  r *zName;  /* Na
24dd7 6d 65 20 6f 66 20 74 68 65 20 70 72 61 67 6d 61  me of the pragma
24dd8 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 73 6b   */.    int mask
24dd9 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ;           /* M
24dda 61 73 6b 20 66 6f 72 20 74 68 65 20 64 62 2d 3e  ask for the db->
24ddb 66 6c 61 67 73 20 76 61 6c 75 65 20 2a 2f 0a 20  flags value */. 
24ddc 20 7d 20 61 50 72 61 67 6d 61 5b 5d 20 3d 20 7b   } aPragma[] = {
24ddd 0a 20 20 20 20 7b 20 22 66 75 6c 6c 5f 63 6f 6c  .    { "full_col
24dde 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20  umn_names",     
24ddf 20 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f     SQLITE_FullCo
24de0 6c 4e 61 6d 65 73 20 20 7d 2c 0a 20 20 20 20 7b  lNames  },.    {
24de1 20 22 73 68 6f 72 74 5f 63 6f 6c 75 6d 6e 5f 6e   "short_column_n
24de2 61 6d 65 73 22 2c 20 20 20 20 20 20 20 53 51 4c  ames",       SQL
24de3 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65  ITE_ShortColName
24de4 73 20 7d 2c 0a 20 20 20 20 7b 20 22 63 6f 75 6e  s },.    { "coun
24de5 74 5f 63 68 61 6e 67 65 73 22 2c 20 20 20 20 20  t_changes",     
24de6 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 6f         SQLITE_Co
24de7 75 6e 74 52 6f 77 73 20 20 20 20 20 7d 2c 0a 20  untRows     },. 
24de8 20 20 20 7b 20 22 65 6d 70 74 79 5f 72 65 73 75     { "empty_resu
24de9 6c 74 5f 63 61 6c 6c 62 61 63 6b 73 22 2c 20 20  lt_callbacks",  
24dea 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c   SQLITE_NullCall
24deb 62 61 63 6b 20 20 7d 2c 0a 20 20 20 20 7b 20 22  back  },.    { "
24dec 6c 65 67 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d  legacy_file_form
24ded 61 74 22 2c 20 20 20 20 20 20 20 53 51 4c 49 54  at",       SQLIT
24dee 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 20  E_LegacyFileFmt 
24def 7d 2c 0a 20 20 20 20 7b 20 22 66 75 6c 6c 66 73  },.    { "fullfs
24df0 79 6e 63 22 2c 20 20 20 20 20 20 20 20 20 20 20  ync",           
24df1 20 20 20 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c       SQLITE_Full
24df2 46 53 79 6e 63 20 20 20 20 20 7d 2c 0a 23 69 66  FSync     },.#if
24df3 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
24df4 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74 72 61 63  .    { "sql_trac
24df5 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e",             
24df6 20 20 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61     SQLITE_SqlTra
24df7 63 65 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b  ce      },.    {
24df8 20 22 76 64 62 65 5f 6c 69 73 74 69 6e 67 22 2c   "vdbe_listing",
24df9 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
24dfa 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20  ITE_VdbeListing 
24dfb 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76 64 62 65    },.    { "vdbe
24dfc 5f 74 72 61 63 65 22 2c 20 20 20 20 20 20 20 20  _trace",        
24dfd 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 56 64         SQLITE_Vd
24dfe 62 65 54 72 61 63 65 20 20 20 20 20 7d 2c 0a 23  beTrace     },.#
24dff 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
24e00 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
24e01 20 20 20 20 7b 20 22 69 67 6e 6f 72 65 5f 63 68      { "ignore_ch
24e02 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e 74 73 22  eck_constraints"
24e03 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43  , SQLITE_IgnoreC
24e04 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e 64 69 66  hecks  },.#endif
24e05 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  .    /* The foll
24e06 6f 77 69 6e 67 20 69 73 20 56 45 52 59 20 65 78  owing is VERY ex
24e07 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f 0a 20 20  perimental */.  
24e08 20 20 7b 20 22 77 72 69 74 61 62 6c 65 5f 73 63    { "writable_sc
24e09 68 65 6d 61 22 2c 20 20 20 20 20 20 20 20 20 20  hema",          
24e0a 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65  SQLITE_WriteSche
24e0b 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63 6f 76 65  ma|SQLITE_Recove
24e0c 72 79 4d 6f 64 65 20 7d 2c 0a 20 20 20 20 7b 20  ryMode },.    { 
24e0d 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63 6b 22 2c  "omit_readlock",
24e0e 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
24e0f 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20  TE_NoReadlock   
24e10 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f   },..    /* TODO
24e11 3a 20 4d 61 79 62 65 20 69 74 20 73 68 6f 75 6c  : Maybe it shoul
24e12 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c 65  dn't be possible
24e13 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 52   to change the R
24e14 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 0a 20  eadUncommitted. 
24e15 20 20 20 2a 2a 20 66 6c 61 67 20 69 66 20 74 68     ** flag if th
24e16 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69  ere are any acti
24e17 76 65 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 2a  ve statements. *
24e18 2f 0a 20 20 20 20 7b 20 22 72 65 61 64 5f 75 6e  /.    { "read_un
24e19 63 6f 6d 6d 69 74 74 65 64 22 2c 20 20 20 20 20  committed",     
24e1a 20 20 20 20 53 51 4c 49 54 45 5f 52 65 61 64 55      SQLITE_ReadU
24e1b 6e 63 6f 6d 6d 69 74 74 65 64 20 7d 2c 0a 20 20  ncommitted },.  
24e1c 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f  };.  int i;.  co
24e1d 6e 73 74 20 73 74 72 75 63 74 20 73 50 72 61 67  nst struct sPrag
24e1e 6d 61 54 79 70 65 20 2a 70 3b 0a 20 20 66 6f 72  maType *p;.  for
24e1f 28 69 3d 30 2c 20 70 3d 61 50 72 61 67 6d 61 3b  (i=0, p=aPragma;
24e20 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 50 72   i<ArraySize(aPr
24e21 61 67 6d 61 29 3b 20 69 2b 2b 2c 20 70 2b 2b 29  agma); i++, p++)
24e22 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
24e23 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
24e24 70 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  p->zName)==0 ){.
24e25 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64        sqlite3 *d
24e26 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
24e27 20 20 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20        Vdbe *v;. 
24e28 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33       v = sqlite3
24e29 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
24e2a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 76  .      assert( v
24e2b 21 3d 30 20 29 3b 20 20 2f 2a 20 41 6c 72 65 61  !=0 );  /* Alrea
24e2c 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  dy allocated by 
24e2d 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 29 20  sqlite3Pragma() 
24e2e 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57  */.      if( ALW
24e2f 41 59 53 28 76 29 20 29 7b 0a 20 20 20 20 20 20  AYS(v) ){.      
24e30 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20    if( zRight==0 
24e31 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
24e32 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
24e33 72 73 65 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 28  rse, p->zName, (
24e34 64 62 2d 3e 66 6c 61 67 73 20 26 20 70 2d 3e 6d  db->flags & p->m
24e35 61 73 6b 29 21 3d 30 20 29 3b 0a 20 20 20 20 20  ask)!=0 );.     
24e36 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24e37 20 20 20 20 69 66 28 20 67 65 74 42 6f 6f 6c 65      if( getBoole
24e38 61 6e 28 7a 52 69 67 68 74 29 20 29 7b 0a 20 20  an(zRight) ){.  
24e39 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 66 6c            db->fl
24e3a 61 67 73 20 7c 3d 20 70 2d 3e 6d 61 73 6b 3b 0a  ags |= p->mask;.
24e3b 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
24e3c 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d  .            db-
24e3d 3e 66 6c 61 67 73 20 26 3d 20 7e 70 2d 3e 6d 61  >flags &= ~p->ma
24e3e 73 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  sk;.          }.
24e3f 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  .          /* Ma
24e40 6e 79 20 6f 66 20 74 68 65 20 66 6c 61 67 2d 70  ny of the flag-p
24e41 72 61 67 6d 61 73 20 6d 6f 64 69 66 79 20 74 68  ragmas modify th
24e42 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
24e43 20 62 79 20 74 68 65 20 53 51 4c 20 0a 20 20 20   by the SQL .   
24e44 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c         ** compil
24e45 65 72 20 28 65 67 2e 20 63 6f 75 6e 74 5f 63 68  er (eg. count_ch
24e46 61 6e 67 65 73 29 2e 20 53 6f 20 61 64 64 20 61  anges). So add a
24e47 6e 20 6f 70 63 6f 64 65 20 74 6f 20 65 78 70 69  n opcode to expi
24e48 72 65 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 20  re all.         
24e49 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c   ** compiled SQL
24e4a 20 73 74 61 74 65 6d 65 6e 74 73 20 61 66 74 65   statements afte
24e4b 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 20 70 72  r modifying a pr
24e4c 61 67 6d 61 20 76 61 6c 75 65 2e 0a 20 20 20 20  agma value..    
24e4d 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
24e4e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24e4f 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72  dOp2(v, OP_Expir
24e50 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  e, 0, 0);.      
24e51 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
24e52 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
24e53 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
24e54 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  0;.}.#endif /* S
24e55 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f  QLITE_OMIT_FLAG_
24e56 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a  PRAGMAS */../*.*
24e57 2a 20 52 65 74 75 72 6e 20 61 20 68 75 6d 61 6e  * Return a human
24e58 2d 72 65 61 64 61 62 6c 65 20 6e 61 6d 65 20 66  -readable name f
24e59 6f 72 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20  or a constraint 
24e5a 72 65 73 6f 6c 75 74 69 6f 6e 20 61 63 74 69 6f  resolution actio
24e5b 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  n..*/.static con
24e5c 73 74 20 63 68 61 72 20 2a 61 63 74 69 6f 6e 4e  st char *actionN
24e5d 61 6d 65 28 75 38 20 61 63 74 69 6f 6e 29 7b 0a  ame(u8 action){.
24e5e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
24e5f 61 6d 65 3b 0a 20 20 73 77 69 74 63 68 28 20 61  ame;.  switch( a
24e60 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 63 61 73  ction ){.    cas
24e61 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 3a 20 20 7a  e OE_SetNull:  z
24e62 4e 61 6d 65 20 3d 20 22 53 45 54 20 4e 55 4c 4c  Name = "SET NULL
24e63 22 3b 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ";            br
24e64 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  eak;.    case OE
24e65 5f 53 65 74 44 66 6c 74 3a 20 20 7a 4e 61 6d 65  _SetDflt:  zName
24e66 20 3d 20 22 53 45 54 20 44 45 46 41 55 4c 54 22   = "SET DEFAULT"
24e67 3b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;         break;
24e68 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 43 61 73  .    case OE_Cas
24e69 63 61 64 65 3a 20 20 7a 4e 61 6d 65 20 3d 20 22  cade:  zName = "
24e6a 43 41 53 43 41 44 45 22 3b 20 20 20 20 20 20 20  CASCADE";       
24e6b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
24e6c 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20   default:       
24e6d 20 20 20 7a 4e 61 6d 65 20 3d 20 22 52 45 53 54     zName = "REST
24e6e 52 49 43 54 22 3b 20 20 0a 20 20 20 20 20 20 20  RICT";  .       
24e6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
24e70 73 73 65 72 74 28 20 61 63 74 69 6f 6e 3d 3d 4f  ssert( action==O
24e71 45 5f 52 65 73 74 72 69 63 74 20 29 3b 20 62 72  E_Restrict ); br
24e72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  eak;.  }.  retur
24e73 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  n zName;.}../*.*
24e74 2a 20 50 72 6f 63 65 73 73 20 61 20 70 72 61 67  * Process a prag
24e75 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a  ma statement.  .
24e76 2a 2a 0a 2a 2a 20 50 72 61 67 6d 61 73 20 61 72  **.** Pragmas ar
24e77 65 20 6f 66 20 74 68 69 73 20 66 6f 72 6d 3a 0a  e of this form:.
24e78 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 52 41 47 4d  **.**      PRAGM
24e79 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64 20  A [database.]id 
24e7a 5b 3d 20 76 61 6c 75 65 5d 0a 2a 2a 0a 2a 2a 20  [= value].**.** 
24e7b 54 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 6d  The identifier m
24e7c 69 67 68 74 20 61 6c 73 6f 20 62 65 20 61 20 73  ight also be a s
24e7d 74 72 69 6e 67 2e 20 20 54 68 65 20 76 61 6c 75  tring.  The valu
24e7e 65 20 69 73 20 61 20 73 74 72 69 6e 67 2c 20 61  e is a string, a
24e7f 6e 64 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65 72  nd.** identifier
24e80 2c 20 6f 72 20 61 20 6e 75 6d 62 65 72 2e 20 20  , or a number.  
24e81 49 66 20 6d 69 6e 75 73 46 6c 61 67 20 69 73 20  If minusFlag is 
24e82 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76  true, then the v
24e83 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 6e 75 6d  alue is.** a num
24e84 62 65 72 20 74 68 61 74 20 77 61 73 20 70 72 65  ber that was pre
24e85 63 65 64 65 64 20 62 79 20 61 20 6d 69 6e 75 73  ceded by a minus
24e86 20 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   sign..**.** If 
24e87 74 68 65 20 6c 65 66 74 20 73 69 64 65 20 69 73  the left side is
24e88 20 22 64 61 74 61 62 61 73 65 2e 69 64 22 20 74   "database.id" t
24e89 68 65 6e 20 70 49 64 31 20 69 73 20 74 68 65 20  hen pId1 is the 
24e8a 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a  database name.**
24e8b 20 61 6e 64 20 70 49 64 32 20 69 73 20 74 68 65   and pId2 is the
24e8c 20 69 64 2e 20 20 49 66 20 74 68 65 20 6c 65 66   id.  If the lef
24e8d 74 20 73 69 64 65 20 69 73 20 6a 75 73 74 20 22  t side is just "
24e8e 69 64 22 20 74 68 65 6e 20 70 49 64 31 20 69 73  id" then pId1 is
24e8f 20 74 68 65 0a 2a 2a 20 69 64 20 61 6e 64 20 70   the.** id and p
24e90 49 64 32 20 69 73 20 61 6e 79 20 65 6d 70 74 79  Id2 is any empty
24e91 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49   string..*/.SQLI
24e92 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
24e93 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 0a 20  sqlite3Pragma(. 
24e94 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
24e95 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 31 2c 20  .  Token *pId1, 
24e96 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
24e97 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73  part of [databas
24e98 65 2e 5d 69 64 20 66 69 65 6c 64 20 2a 2f 0a 20  e.]id field */. 
24e99 20 54 6f 6b 65 6e 20 2a 70 49 64 32 2c 20 20 20   Token *pId2,   
24e9a 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70       /* Second p
24e9b 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73 65  art of [database
24e9c 2e 5d 69 64 20 66 69 65 6c 64 2c 20 6f 72 20 4e  .]id field, or N
24e9d 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ULL */.  Token *
24e9e 70 56 61 6c 75 65 2c 20 20 20 20 20 20 2f 2a 20  pValue,      /* 
24e9f 54 6f 6b 65 6e 20 66 6f 72 20 3c 76 61 6c 75 65  Token for <value
24ea0 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  >, or NULL */.  
24ea1 69 6e 74 20 6d 69 6e 75 73 46 6c 61 67 20 20 20  int minusFlag   
24ea2 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
24ea3 20 27 2d 27 20 73 69 67 6e 20 70 72 65 63 65 64   '-' sign preced
24ea4 65 64 20 3c 76 61 6c 75 65 3e 20 2a 2f 0a 29 7b  ed <value> */.){
24ea5 0a 20 20 63 68 61 72 20 2a 7a 4c 65 66 74 20 3d  .  char *zLeft =
24ea6 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c   0;       /* Nul
24ea7 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
24ea8 38 20 73 74 72 69 6e 67 20 3c 69 64 3e 20 2a 2f  8 string <id> */
24ea9 0a 20 20 63 68 61 72 20 2a 7a 52 69 67 68 74 20  .  char *zRight 
24eaa 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6c  = 0;      /* Nul
24eab 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
24eac 38 20 73 74 72 69 6e 67 20 3c 76 61 6c 75 65 3e  8 string <value>
24ead 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  , or NULL */.  c
24eae 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
24eaf 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74   0;   /* The dat
24eb0 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  abase name */.  
24eb1 54 6f 6b 65 6e 20 2a 70 49 64 3b 20 20 20 20 20  Token *pId;     
24eb2 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
24eb3 72 20 74 6f 20 3c 69 64 3e 20 74 6f 6b 65 6e 20  r to <id> token 
24eb4 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
24eb5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
24eb6 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 66 6f  atabase index fo
24eb7 72 20 3c 64 61 74 61 62 61 73 65 3e 20 2a 2f 0a  r <database> */.
24eb8 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
24eb9 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62  pParse->db;.  Db
24eba 20 2a 70 44 62 3b 0a 20 20 56 64 62 65 20 2a 76   *pDb;.  Vdbe *v
24ebb 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
24ebc 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 72   = sqlite3VdbeCr
24ebd 65 61 74 65 28 64 62 29 3b 0a 20 20 69 66 28 20  eate(db);.  if( 
24ebe 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  v==0 ) return;. 
24ebf 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
24ec0 32 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 70 72  2;..  /* Interpr
24ec1 65 74 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  et the [database
24ec2 2e 5d 20 70 61 72 74 20 6f 66 20 74 68 65 20 70  .] part of the p
24ec3 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e  ragma statement.
24ec4 20 69 44 62 20 69 73 20 74 68 65 0a 20 20 2a 2a   iDb is the.  **
24ec5 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   index of the da
24ec6 74 61 62 61 73 65 20 74 68 69 73 20 70 72 61 67  tabase this prag
24ec7 6d 61 20 69 73 20 62 65 69 6e 67 20 61 70 70 6c  ma is being appl
24ec8 69 65 64 20 74 6f 20 69 6e 20 64 62 2e 61 44 62  ied to in db.aDb
24ec9 5b 5d 2e 20 2a 2f 0a 20 20 69 44 62 20 3d 20 73  []. */.  iDb = s
24eca 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
24ecb 65 28 70 50 61 72 73 65 2c 20 70 49 64 31 2c 20  e(pParse, pId1, 
24ecc 70 49 64 32 2c 20 26 70 49 64 29 3b 0a 20 20 69  pId2, &pId);.  i
24ecd 66 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72  f( iDb<0 ) retur
24ece 6e 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  n;.  pDb = &db->
24ecf 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 2f 2a 20  aDb[iDb];..  /* 
24ed0 49 66 20 74 68 65 20 74 65 6d 70 20 64 61 74 61  If the temp data
24ed1 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 65 78  base has been ex
24ed2 70 6c 69 63 69 74 6c 79 20 6e 61 6d 65 64 20 61  plicitly named a
24ed3 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 0a 20  s part of the . 
24ed4 20 2a 2a 20 70 72 61 67 6d 61 2c 20 6d 61 6b 65   ** pragma, make
24ed5 20 73 75 72 65 20 69 74 20 69 73 20 6f 70 65 6e   sure it is open
24ed6 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 44  . .  */.  if( iD
24ed7 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33 4f  b==1 && sqlite3O
24ed8 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28  penTempDatabase(
24ed9 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72  pParse) ){.    r
24eda 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 7a 4c  eturn;.  }..  zL
24edb 65 66 74 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  eft = sqlite3Nam
24edc 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
24edd 49 64 29 3b 0a 20 20 69 66 28 20 21 7a 4c 65 66  Id);.  if( !zLef
24ede 74 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  t ) return;.  if
24edf 28 20 6d 69 6e 75 73 46 6c 61 67 20 29 7b 0a 20  ( minusFlag ){. 
24ee0 20 20 20 7a 52 69 67 68 74 20 3d 20 73 71 6c 69     zRight = sqli
24ee1 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
24ee2 2d 25 54 22 2c 20 70 56 61 6c 75 65 29 3b 0a 20  -%T", pValue);. 
24ee3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 52 69 67   }else{.    zRig
24ee4 68 74 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  ht = sqlite3Name
24ee5 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 56  FromToken(db, pV
24ee6 61 6c 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 61 73  alue);.  }..  as
24ee7 73 65 72 74 28 20 70 49 64 32 20 29 3b 0a 20 20  sert( pId2 );.  
24ee8 7a 44 62 20 3d 20 70 49 64 32 2d 3e 6e 3e 30 20  zDb = pId2->n>0 
24ee9 3f 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3a 20 30  ? pDb->zName : 0
24eea 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  ;.  if( sqlite3A
24eeb 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
24eec 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 2c 20   SQLITE_PRAGMA, 
24eed 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74 2c 20 7a  zLeft, zRight, z
24eee 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  Db) ){.    goto 
24eef 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 7d 0a  pragma_out;.  }.
24ef0 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45   .#ifndef SQLITE
24ef1 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
24ef2 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  MAS.  /*.  **  P
24ef3 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
24ef4 5d 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73  ]default_cache_s
24ef5 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41  ize.  **  PRAGMA
24ef6 20 5b 64 61 74 61 62 61 73 65 2e 5d 64 65 66 61   [database.]defa
24ef7 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 3d 4e  ult_cache_size=N
24ef8 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66  .  **.  ** The f
24ef9 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74  irst form report
24efa 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 65  s the current pe
24efb 72 73 69 73 74 65 6e 74 20 73 65 74 74 69 6e 67  rsistent setting
24efc 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70 61   for the.  ** pa
24efd 67 65 20 63 61 63 68 65 20 73 69 7a 65 2e 20 20  ge cache size.  
24efe 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
24eff 65 64 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75  ed is the maximu
24f00 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a  m number of.  **
24f01 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70 61   pages in the pa
24f02 67 65 20 63 61 63 68 65 2e 20 20 54 68 65 20 73  ge cache.  The s
24f03 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20  econd form sets 
24f04 62 6f 74 68 20 74 68 65 20 63 75 72 72 65 6e 74  both the current
24f05 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65  .  ** page cache
24f06 20 73 69 7a 65 20 76 61 6c 75 65 20 61 6e 64 20   size value and 
24f07 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 70  the persistent p
24f08 61 67 65 20 63 61 63 68 65 20 73 69 7a 65 20 76  age cache size v
24f09 61 6c 75 65 0a 20 20 2a 2a 20 73 74 6f 72 65 64  alue.  ** stored
24f0a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
24f0b 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   file..  **.  **
24f0c 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 61 63   The default cac
24f0d 68 65 20 73 69 7a 65 20 69 73 20 73 74 6f 72 65  he size is store
24f0e 64 20 69 6e 20 6d 65 74 61 2d 76 61 6c 75 65 20  d in meta-value 
24f0f 32 20 6f 66 20 70 61 67 65 20 31 20 6f 66 20 74  2 of page 1 of t
24f10 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  he.  ** database
24f11 20 66 69 6c 65 2e 20 20 54 68 65 20 63 61 63 68   file.  The cach
24f12 65 20 73 69 7a 65 20 69 73 20 61 63 74 75 61 6c  e size is actual
24f13 6c 79 20 74 68 65 20 61 62 73 6f 6c 75 74 65 20  ly the absolute 
24f14 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a 20 74 68  value of.  ** th
24f15 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  is memory locati
24f16 6f 6e 2e 20 20 54 68 65 20 73 69 67 6e 20 6f 66  on.  The sign of
24f17 20 6d 65 74 61 2d 76 61 6c 75 65 20 32 20 64 65   meta-value 2 de
24f18 74 65 72 6d 69 6e 65 73 20 74 68 65 0a 20 20 2a  termines the.  *
24f19 2a 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 73 65  * synchronous se
24f1a 74 74 69 6e 67 2e 20 20 41 20 6e 65 67 61 74 69  tting.  A negati
24f1b 76 65 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 73  ve value means s
24f1c 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66  ynchronous is of
24f1d 66 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 6f 73  f.  ** and a pos
24f1e 69 74 69 76 65 20 76 61 6c 75 65 20 6d 65 61 6e  itive value mean
24f1f 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73  s synchronous is
24f20 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   on..  */.  if( 
24f21 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
24f22 4c 65 66 74 2c 22 64 65 66 61 75 6c 74 5f 63 61  Left,"default_ca
24f23 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b  che_size")==0 ){
24f24 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
24f25 74 20 56 64 62 65 4f 70 4c 69 73 74 20 67 65 74  t VdbeOpList get
24f26 43 61 63 68 65 53 69 7a 65 5b 5d 20 3d 20 7b 0a  CacheSize[] = {.
24f27 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43        { OP_ReadC
24f28 6f 6f 6b 69 65 2c 20 20 30 2c 20 31 2c 20 20 20  ookie,  0, 1,   
24f29 20 20 20 20 20 32 7d 2c 20 20 2f 2a 20 30 20 2a       2},  /* 0 *
24f2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50  /.      { OP_IfP
24f2b 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 36 2c 20  os,       1, 6, 
24f2c 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20         0},.     
24f2d 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20   { OP_Integer,  
24f2e 20 20 20 30 2c 20 32 2c 20 20 20 20 20 20 20 20     0, 2,        
24f2f 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53  0},.      { OP_S
24f30 75 62 74 72 61 63 74 2c 20 20 20 20 31 2c 20 32  ubtract,    1, 2
24f31 2c 20 20 20 20 20 20 20 20 31 7d 2c 0a 20 20 20  ,        1},.   
24f32 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20     { OP_IfPos,  
24f33 20 20 20 20 20 31 2c 20 36 2c 20 20 20 20 20 20       1, 6,      
24f34 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    0},.      { OP
24f35 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 30 2c  _Integer,     0,
24f36 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20   1,        0},  
24f37 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20 7b 20  /* 5 */.      { 
24f38 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20  OP_ResultRow,   
24f39 31 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c  1, 1,        0},
24f3a 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20  .    };.    int 
24f3b 61 64 64 72 3b 0a 20 20 20 20 69 66 28 20 73 71  addr;.    if( sq
24f3c 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
24f3d 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
24f3e 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73  ragma_out;.    s
24f3f 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
24f40 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20  ree(v, iDb);.   
24f41 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a   if( !zRight ){.
24f42 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24f43 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
24f44 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
24f45 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
24f46 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
24f47 45 2c 20 22 63 61 63 68 65 5f 73 69 7a 65 22 2c  E, "cache_size",
24f48 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
24f49 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
24f4a 4d 65 6d 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20  Mem += 2;.      
24f4b 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
24f4c 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41  beAddOpList(v, A
24f4d 72 72 61 79 53 69 7a 65 28 67 65 74 43 61 63 68  rraySize(getCach
24f4e 65 53 69 7a 65 29 2c 20 67 65 74 43 61 63 68 65  eSize), getCache
24f4f 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  Size);.      sql
24f50 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
24f51 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29 3b 0a  (v, addr, iDb);.
24f52 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24f53 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64  eChangeP1(v, add
24f54 72 2b 35 2c 20 53 51 4c 49 54 45 5f 44 45 46 41  r+5, SQLITE_DEFA
24f55 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 29 3b  ULT_CACHE_SIZE);
24f56 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
24f57 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f    int size = ato
24f58 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  i(zRight);.     
24f59 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69   if( size<0 ) si
24f5a 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20  ze = -size;.    
24f5b 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
24f5c 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
24f5d 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20  rse, 0, iDb);.  
24f5e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24f5f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
24f60 67 65 72 2c 20 73 69 7a 65 2c 20 31 29 3b 0a 20  ger, size, 1);. 
24f61 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24f62 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61  AddOp3(v, OP_Rea
24f63 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 32 2c  dCookie, iDb, 2,
24f64 20 32 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20   2);.      addr 
24f65 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
24f66 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c  Op2(v, OP_IfPos,
24f67 20 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71   2, 0);.      sq
24f68 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24f69 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d  v, OP_Integer, -
24f6a 73 69 7a 65 2c 20 31 29 3b 0a 20 20 20 20 20 20  size, 1);.      
24f6b 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
24f6c 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20  ere(v, addr);.  
24f6d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24f6e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43  ddOp3(v, OP_SetC
24f6f 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 32 2c 20 31  ookie, iDb, 2, 1
24f70 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 53  );.      pDb->pS
24f71 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a  chema->cache_siz
24f72 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20  e = size;.      
24f73 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43  sqlite3BtreeSetC
24f74 61 63 68 65 53 69 7a 65 28 70 44 62 2d 3e 70 42  acheSize(pDb->pB
24f75 74 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  t, pDb->pSchema-
24f76 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20  >cache_size);.  
24f77 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f    }.  }else..  /
24f78 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
24f79 64 61 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 73  database.]page_s
24f7a 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41  ize.  **  PRAGMA
24f7b 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67 65   [database.]page
24f7c 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a  _size=N.  **.  *
24f7d 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d  * The first form
24f7e 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75 72   reports the cur
24f7f 72 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72  rent setting for
24f80 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
24f81 73 65 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20  se page size in 
24f82 62 79 74 65 73 2e 20 20 54 68 65 20 73 65 63 6f  bytes.  The seco
24f83 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65  nd form sets the
24f84 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70  .  ** database p
24f85 61 67 65 20 73 69 7a 65 20 76 61 6c 75 65 2e 20  age size value. 
24f86 20 54 68 65 20 76 61 6c 75 65 20 63 61 6e 20 6f   The value can o
24f87 6e 6c 79 20 62 65 20 73 65 74 20 69 66 0a 20 20  nly be set if.  
24f88 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
24f89 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
24f8a 20 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f 0a 20   created..  */. 
24f8b 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
24f8c 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61 67 65 5f  Cmp(zLeft,"page_
24f8d 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  size")==0 ){.   
24f8e 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44   Btree *pBt = pD
24f8f 62 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65  b->pBt;.    asse
24f90 72 74 28 20 70 42 74 21 3d 30 20 29 3b 0a 20 20  rt( pBt!=0 );.  
24f91 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
24f92 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20  .      int size 
24f93 3d 20 41 4c 57 41 59 53 28 70 42 74 29 20 3f 20  = ALWAYS(pBt) ? 
24f94 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
24f95 61 67 65 53 69 7a 65 28 70 42 74 29 20 3a 20 30  ageSize(pBt) : 0
24f96 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53 69  ;.      returnSi
24f97 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20  ngleInt(pParse, 
24f98 22 70 61 67 65 5f 73 69 7a 65 22 2c 20 73 69 7a  "page_size", siz
24f99 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
24f9a 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d       /* Malloc m
24f9b 61 79 20 66 61 69 6c 20 77 68 65 6e 20 73 65 74  ay fail when set
24f9c 74 69 6e 67 20 74 68 65 20 70 61 67 65 2d 73 69  ting the page-si
24f9d 7a 65 2c 20 61 73 20 74 68 65 72 65 20 69 73 20  ze, as there is 
24f9e 61 6e 20 69 6e 74 65 72 6e 61 6c 0a 20 20 20 20  an internal.    
24f9f 20 20 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74    ** buffer that
24fa0 20 74 68 65 20 70 61 67 65 72 20 6d 6f 64 75 6c   the pager modul
24fa1 65 20 72 65 73 69 7a 65 73 20 75 73 69 6e 67 20  e resizes using 
24fa2 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
24fa3 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
24fa4 20 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69    db->nextPagesi
24fa5 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74  ze = atoi(zRight
24fa6 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c  );.      if( SQL
24fa7 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74  ITE_NOMEM==sqlit
24fa8 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69  e3BtreeSetPageSi
24fa9 7a 65 28 70 42 74 2c 20 64 62 2d 3e 6e 65 78 74  ze(pBt, db->next
24faa 50 61 67 65 73 69 7a 65 2c 20 2d 31 29 20 29 7b  Pagesize, -1) ){
24fab 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  .        db->mal
24fac 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
24fad 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
24fae 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  else..  /*.  ** 
24faf 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
24fb0 65 2e 5d 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e  e.]max_page_coun
24fb1 74 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  t.  **  PRAGMA [
24fb2 64 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61  database.]max_pa
24fb3 67 65 5f 63 6f 75 6e 74 3d 4e 0a 20 20 2a 2a 0a  ge_count=N.  **.
24fb4 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66    ** The first f
24fb5 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20  orm reports the 
24fb6 63 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67 20  current setting 
24fb7 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78  for the.  ** max
24fb8 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70  imum number of p
24fb9 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ages in the data
24fba 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
24fbb 0a 20 20 2a 2a 20 73 65 63 6f 6e 64 20 66 6f 72  .  ** second for
24fbc 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63 68  m attempts to ch
24fbd 61 6e 67 65 20 74 68 69 73 20 73 65 74 74 69 6e  ange this settin
24fbe 67 2e 20 20 42 6f 74 68 0a 20 20 2a 2a 20 66 6f  g.  Both.  ** fo
24fbf 72 6d 73 20 72 65 74 75 72 6e 20 74 68 65 20 63  rms return the c
24fc0 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67 2e 0a  urrent setting..
24fc1 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
24fc2 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
24fc3 22 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 22  "max_page_count"
24fc4 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72 65  )==0 ){.    Btre
24fc5 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42  e *pBt = pDb->pB
24fc6 74 3b 0a 20 20 20 20 69 6e 74 20 6e 65 77 4d 61  t;.    int newMa
24fc7 78 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72  x = 0;.    asser
24fc8 74 28 20 70 42 74 21 3d 30 20 29 3b 0a 20 20 20  t( pBt!=0 );.   
24fc9 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20   if( zRight ){. 
24fca 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 61 74       newMax = at
24fcb 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20  oi(zRight);.    
24fcc 7d 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59 53  }.    if( ALWAYS
24fcd 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e  (pBt) ){.      n
24fce 65 77 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 42  ewMax = sqlite3B
24fcf 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74  treeMaxPageCount
24fd0 28 70 42 74 2c 20 6e 65 77 4d 61 78 29 3b 0a 20  (pBt, newMax);. 
24fd1 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 53     }.    returnS
24fd2 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
24fd3 20 22 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74   "max_page_count
24fd4 22 2c 20 6e 65 77 4d 61 78 29 3b 0a 20 20 7d 65  ", newMax);.  }e
24fd5 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  lse..  /*.  **  
24fd6 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
24fd7 2e 5d 70 61 67 65 5f 63 6f 75 6e 74 0a 20 20 2a  .]page_count.  *
24fd8 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68  *.  ** Return th
24fd9 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
24fda 73 20 69 6e 20 74 68 65 20 73 70 65 63 69 66 69  s in the specifi
24fdb 65 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a  ed database..  *
24fdc 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
24fdd 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61  trICmp(zLeft,"pa
24fde 67 65 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b  ge_count")==0 ){
24fdf 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 0a 20  .    int iReg;. 
24fe0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
24fe1 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
24fe2 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
24fe3 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  ut;.    sqlite3C
24fe4 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
24fe5 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20  pParse, iDb);.  
24fe6 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73    iReg = ++pPars
24fe7 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
24fe8 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24fe9 2c 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 2c 20  , OP_Pagecount, 
24fea 69 44 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  iDb, iReg);.    
24feb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24fec 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
24fed 77 2c 20 69 52 65 67 2c 20 31 29 3b 0a 20 20 20  w, iReg, 1);.   
24fee 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
24fef 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
24ff0 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
24ff1 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
24ff2 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 61 67  LNAME_NAME, "pag
24ff3 65 5f 63 6f 75 6e 74 22 2c 20 53 51 4c 49 54 45  e_count", SQLITE
24ff4 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 65 6c 73  _STATIC);.  }els
24ff5 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52  e..  /*.  **  PR
24ff6 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
24ff7 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 0a 20 20 2a  locking_mode.  *
24ff8 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
24ff9 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  ase.]locking_mod
24ffa 65 20 3d 20 28 6e 6f 72 6d 61 6c 7c 65 78 63 6c  e = (normal|excl
24ffb 75 73 69 76 65 29 0a 20 20 2a 2f 0a 20 20 69 66  usive).  */.  if
24ffc 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
24ffd 28 7a 4c 65 66 74 2c 22 6c 6f 63 6b 69 6e 67 5f  (zLeft,"locking_
24ffe 6d 6f 64 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  mode")==0 ){.   
24fff 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65   const char *zRe
25000 74 20 3d 20 22 6e 6f 72 6d 61 6c 22 3b 0a 20 20  t = "normal";.  
25001 20 20 69 6e 74 20 65 4d 6f 64 65 20 3d 20 67 65    int eMode = ge
25002 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 7a 52 69  tLockingMode(zRi
25003 67 68 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 70  ght);..    if( p
25004 49 64 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f  Id2->n==0 && eMo
25005 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  de==PAGER_LOCKIN
25006 47 4d 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a 20  GMODE_QUERY ){. 
25007 20 20 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22       /* Simple "
25008 50 52 41 47 4d 41 20 6c 6f 63 6b 69 6e 67 5f 6d  PRAGMA locking_m
25009 6f 64 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e  ode;" statement.
2500a 20 54 68 69 73 20 69 73 20 61 20 71 75 65 72 79   This is a query
2500b 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68   for.      ** th
2500c 65 20 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c  e current defaul
2500d 74 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 28  t locking mode (
2500e 77 68 69 63 68 20 6d 61 79 20 62 65 20 64 69 66  which may be dif
2500f 66 65 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20  ferent to.      
25010 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d  ** the locking-m
25011 6f 64 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  ode of the main 
25012 64 61 74 61 62 61 73 65 29 2e 0a 20 20 20 20 20  database)..     
25013 20 2a 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20   */.      eMode 
25014 3d 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f  = db->dfltLockMo
25015 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  de;.    }else{. 
25016 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67       Pager *pPag
25017 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  er;.      if( pI
25018 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  d2->n==0 ){.    
25019 20 20 20 20 2f 2a 20 54 68 69 73 20 69 6e 64 69      /* This indi
2501a 63 61 74 65 73 20 74 68 61 74 20 6e 6f 20 64 61  cates that no da
2501b 74 61 62 61 73 65 20 6e 61 6d 65 20 77 61 73 20  tabase name was 
2501c 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72  specified as par
2501d 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20  t.        ** of 
2501e 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61  the PRAGMA comma
2501f 6e 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  nd. In this case
25020 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64   the locking-mod
25021 65 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20 20  e must be.      
25022 20 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c 20    ** set on all 
25023 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
25024 65 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74  es, as well as t
25025 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 2e  he main db file.
25026 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
25027 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65      ** Also, the
25028 20 73 71 6c 69 74 65 33 2e 64 66 6c 74 4c 6f 63   sqlite3.dfltLoc
25029 6b 4d 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69  kMode variable i
2502a 73 20 73 65 74 20 73 6f 20 74 68 61 74 0a 20 20  s set so that.  
2502b 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 62        ** any sub
2502c 73 65 71 75 65 6e 74 6c 79 20 61 74 74 61 63 68  sequently attach
2502d 65 64 20 64 61 74 61 62 61 73 65 73 20 61 6c 73  ed databases als
2502e 6f 20 75 73 65 20 74 68 65 20 73 70 65 63 69 66  o use the specif
2502f 69 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c  ied.        ** l
25030 6f 63 6b 69 6e 67 20 6d 6f 64 65 2e 0a 20 20 20  ocking mode..   
25031 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
25032 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20  int ii;.        
25033 61 73 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d  assert(pDb==&db-
25034 3e 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20  >aDb[0]);.      
25035 20 20 66 6f 72 28 69 69 3d 32 3b 20 69 69 3c 64    for(ii=2; ii<d
25036 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20  b->nDb; ii++){. 
25037 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 20           pPager 
25038 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
25039 67 65 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e  ger(db->aDb[ii].
2503a 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pBt);.          
2503b 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
2503c 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20  ingMode(pPager, 
2503d 65 4d 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20  eMode);.        
2503e 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66  }.        db->df
2503f 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20 28 75 38  ltLockMode = (u8
25040 29 65 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a  )eMode;.      }.
25041 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73        pPager = s
25042 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
25043 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20  (pDb->pBt);.    
25044 20 20 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65    eMode = sqlite
25045 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64  3PagerLockingMod
25046 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29  e(pPager, eMode)
25047 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73  ;.    }..    ass
25048 65 72 74 28 65 4d 6f 64 65 3d 3d 50 41 47 45 52  ert(eMode==PAGER
25049 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52  _LOCKINGMODE_NOR
2504a 4d 41 4c 7c 7c 65 4d 6f 64 65 3d 3d 50 41 47 45  MAL||eMode==PAGE
2504b 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
2504c 43 4c 55 53 49 56 45 29 3b 0a 20 20 20 20 69 66  CLUSIVE);.    if
2504d 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c  ( eMode==PAGER_L
2504e 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
2504f 53 49 56 45 20 29 7b 0a 20 20 20 20 20 20 7a 52  SIVE ){.      zR
25050 65 74 20 3d 20 22 65 78 63 6c 75 73 69 76 65 22  et = "exclusive"
25051 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
25052 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
25053 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  s(v, 1);.    sql
25054 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
25055 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
25056 5f 4e 41 4d 45 2c 20 22 6c 6f 63 6b 69 6e 67 5f  _NAME, "locking_
25057 6d 6f 64 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  mode", SQLITE_ST
25058 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74  ATIC);.    sqlit
25059 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2505a 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31  OP_String8, 0, 1
2505b 2c 20 30 2c 20 7a 52 65 74 2c 20 30 29 3b 0a 20  , 0, zRet, 0);. 
2505c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2505d 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
2505e 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 7d  tRow, 1, 1);.  }
2505f 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  else..  /*.  ** 
25060 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
25061 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 0a  e.]journal_mode.
25062 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
25063 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f  tabase.]journal_
25064 6d 6f 64 65 20 3d 20 28 64 65 6c 65 74 65 7c 70  mode = (delete|p
25065 65 72 73 69 73 74 7c 6f 66 66 7c 74 72 75 6e 63  ersist|off|trunc
25066 61 74 65 7c 6d 65 6d 6f 72 79 29 0a 20 20 2a 2f  ate|memory).  */
25067 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
25068 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6a 6f 75  rICmp(zLeft,"jou
25069 72 6e 61 6c 5f 6d 6f 64 65 22 29 3d 3d 30 20 29  rnal_mode")==0 )
2506a 7b 0a 20 20 20 20 69 6e 74 20 65 4d 6f 64 65 3b  {.    int eMode;
2506b 0a 20 20 20 20 73 74 61 74 69 63 20 63 68 61 72  .    static char
2506c 20 2a 20 63 6f 6e 73 74 20 61 7a 4d 6f 64 65 4e   * const azModeN
2506d 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  ame[] = {.      
2506e 22 64 65 6c 65 74 65 22 2c 20 22 70 65 72 73 69  "delete", "persi
2506f 73 74 22 2c 20 22 6f 66 66 22 2c 20 22 74 72 75  st", "off", "tru
25070 6e 63 61 74 65 22 2c 20 22 6d 65 6d 6f 72 79 22  ncate", "memory"
25071 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 69 66 28  .    };..    if(
25072 20 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20   zRight==0 ){.  
25073 20 20 20 20 65 4d 6f 64 65 20 3d 20 50 41 47 45      eMode = PAGE
25074 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
25075 45 52 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ERY;.    }else{.
25076 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71        int n = sq
25077 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 52  lite3Strlen30(zR
25078 69 67 68 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f  ight);.      eMo
25079 64 65 20 3d 20 73 69 7a 65 6f 66 28 61 7a 4d 6f  de = sizeof(azMo
2507a 64 65 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28 61  deName)/sizeof(a
2507b 7a 4d 6f 64 65 4e 61 6d 65 5b 30 5d 29 20 2d 20  zModeName[0]) - 
2507c 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  1;.      while( 
2507d 65 4d 6f 64 65 3e 3d 30 20 26 26 20 73 71 6c 69  eMode>=0 && sqli
2507e 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 52 69 67  te3StrNICmp(zRig
2507f 68 74 2c 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b 65  ht, azModeName[e
25080 4d 6f 64 65 5d 2c 20 6e 29 21 3d 30 20 29 7b 0a  Mode], n)!=0 ){.
25081 20 20 20 20 20 20 20 20 65 4d 6f 64 65 2d 2d 3b          eMode--;
25082 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
25083 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d     if( pId2->n==
25084 30 20 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47 45  0 && eMode==PAGE
25085 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
25086 45 52 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ERY ){.      /* 
25087 53 69 6d 70 6c 65 20 22 50 52 41 47 4d 41 20 6a  Simple "PRAGMA j
25088 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3b 22 20 73 74  ournal_mode;" st
25089 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  atement. This is
2508a 20 61 20 71 75 65 72 79 20 66 6f 72 0a 20 20 20   a query for.   
2508b 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e     ** the curren
2508c 74 20 64 65 66 61 75 6c 74 20 6a 6f 75 72 6e 61  t default journa
2508d 6c 20 6d 6f 64 65 20 28 77 68 69 63 68 20 6d 61  l mode (which ma
2508e 79 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 74  y be different t
2508f 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a  o.      ** the j
25090 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6f 66 20 74  ournal-mode of t
25091 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
25092 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
25093 20 20 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64 66    eMode = db->df
25094 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a 20  ltJournalMode;. 
25095 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
25096 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20  Pager *pPager;. 
25097 20 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e       if( pId2->n
25098 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
25099 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73  * This indicates
2509a 20 74 68 61 74 20 6e 6f 20 64 61 74 61 62 61 73   that no databas
2509b 65 20 6e 61 6d 65 20 77 61 73 20 73 70 65 63 69  e name was speci
2509c 66 69 65 64 20 61 73 20 70 61 72 74 0a 20 20 20  fied as part.   
2509d 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 50       ** of the P
2509e 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 20 49  RAGMA command. I
2509f 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
250a0 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6d 75 73  journal-mode mus
250a1 74 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  t be.        ** 
250a2 73 65 74 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63  set on all attac
250a3 68 65 64 20 64 61 74 61 62 61 73 65 73 2c 20 61  hed databases, a
250a4 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 6d 61  s well as the ma
250a5 69 6e 20 64 62 20 66 69 6c 65 2e 0a 20 20 20 20  in db file..    
250a6 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
250a7 2a 20 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c 69  * Also, the sqli
250a8 74 65 33 2e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d  te3.dfltJournalM
250a9 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69 73 20  ode variable is 
250aa 73 65 74 20 73 6f 20 74 68 61 74 0a 20 20 20 20  set so that.    
250ab 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 62 73 65      ** any subse
250ac 71 75 65 6e 74 6c 79 20 61 74 74 61 63 68 65 64  quently attached
250ad 20 64 61 74 61 62 61 73 65 73 20 61 6c 73 6f 20   databases also 
250ae 75 73 65 20 74 68 65 20 73 70 65 63 69 66 69 65  use the specifie
250af 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 6a 6f 75  d.        ** jou
250b0 72 6e 61 6c 20 6d 6f 64 65 2e 0a 20 20 20 20 20  rnal mode..     
250b1 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
250b2 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61 73  t ii;.        as
250b3 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e 61  sert(pDb==&db->a
250b4 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20  Db[0]);.        
250b5 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 64 62 2d  for(ii=1; ii<db-
250b6 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  >nDb; ii++){.   
250b7 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61         if( db->a
250b8 44 62 5b 69 69 5d 2e 70 42 74 20 29 7b 0a 20 20  Db[ii].pBt ){.  
250b9 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
250ba 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
250bb 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d  ager(db->aDb[ii]
250bc 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 20  .pBt);.         
250bd 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a     sqlite3PagerJ
250be 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65  ournalMode(pPage
250bf 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 20  r, eMode);.     
250c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
250c1 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66 6c  .        db->dfl
250c2 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 28  tJournalMode = (
250c3 75 38 29 65 4d 6f 64 65 3b 0a 20 20 20 20 20 20  u8)eMode;.      
250c4 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 20 3d  }.      pPager =
250c5 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
250c6 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  er(pDb->pBt);.  
250c7 20 20 20 20 65 4d 6f 64 65 20 3d 20 73 71 6c 69      eMode = sqli
250c8 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d  te3PagerJournalM
250c9 6f 64 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64  ode(pPager, eMod
250ca 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  e);.    }.    as
250cb 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47  sert( eMode==PAG
250cc 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44  ER_JOURNALMODE_D
250cd 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 20 20  ELETE.          
250ce 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41      || eMode==PA
250cf 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
250d0 54 52 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20  TRUNCATE.       
250d1 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d         || eMode=
250d2 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
250d3 44 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 20  DE_PERSIST.     
250d4 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64           || eMod
250d5 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
250d6 4d 4f 44 45 5f 4f 46 46 0a 20 20 20 20 20 20 20  MODE_OFF.       
250d7 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d         || eMode=
250d8 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
250d9 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 20  DE_MEMORY );.   
250da 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
250db 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
250dc 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
250dd 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
250de 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6a 6f 75  LNAME_NAME, "jou
250df 72 6e 61 6c 5f 6d 6f 64 65 22 2c 20 53 51 4c 49  rnal_mode", SQLI
250e0 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
250e1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
250e2 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
250e3 20 30 2c 20 31 2c 20 30 2c 20 0a 20 20 20 20 20   0, 1, 0, .     
250e4 20 20 20 20 20 20 61 7a 4d 6f 64 65 4e 61 6d 65        azModeName
250e5 5b 65 4d 6f 64 65 5d 2c 20 50 34 5f 53 54 41 54  [eMode], P4_STAT
250e6 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
250e7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
250e8 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31  _ResultRow, 1, 1
250e9 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a  );.  }else..  /*
250ea 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
250eb 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c  atabase.]journal
250ec 5f 73 69 7a 65 5f 6c 69 6d 69 74 0a 20 20 2a 2a  _size_limit.  **
250ed 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
250ee 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65  se.]journal_size
250ef 5f 6c 69 6d 69 74 3d 4e 0a 20 20 2a 2a 0a 20 20  _limit=N.  **.  
250f0 2a 2a 20 47 65 74 20 6f 72 20 73 65 74 20 74 68  ** Get or set th
250f1 65 20 73 69 7a 65 20 6c 69 6d 69 74 20 6f 6e 20  e size limit on 
250f2 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
250f3 20 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69   files..  */.  i
250f4 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
250f5 70 28 7a 4c 65 66 74 2c 22 6a 6f 75 72 6e 61 6c  p(zLeft,"journal
250f6 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 29 3d 3d 30  _size_limit")==0
250f7 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70   ){.    Pager *p
250f8 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
250f9 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70  treePager(pDb->p
250fa 42 74 29 3b 0a 20 20 20 20 69 36 34 20 69 4c 69  Bt);.    i64 iLi
250fb 6d 69 74 20 3d 20 2d 32 3b 0a 20 20 20 20 69 66  mit = -2;.    if
250fc 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ( zRight ){.    
250fd 20 20 69 6e 74 20 69 4c 69 6d 69 74 33 32 20 3d    int iLimit32 =
250fe 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20   atoi(zRight);. 
250ff 20 20 20 20 20 69 66 28 20 69 4c 69 6d 69 74 33       if( iLimit3
25100 32 3c 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  2<-1 ){.        
25101 69 4c 69 6d 69 74 33 32 20 3d 20 2d 31 3b 0a 20  iLimit32 = -1;. 
25102 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 4c 69       }.      iLi
25103 6d 69 74 20 3d 20 69 4c 69 6d 69 74 33 32 3b 0a  mit = iLimit32;.
25104 20 20 20 20 7d 0a 20 20 20 20 69 4c 69 6d 69 74      }.    iLimit
25105 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a   = sqlite3PagerJ
25106 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28  ournalSizeLimit(
25107 70 50 61 67 65 72 2c 20 69 4c 69 6d 69 74 29 3b  pPager, iLimit);
25108 0a 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c  .    returnSingl
25109 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 6a 6f  eInt(pParse, "jo
2510a 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74  urnal_size_limit
2510b 22 2c 20 28 69 6e 74 29 69 4c 69 6d 69 74 29 3b  ", (int)iLimit);
2510c 0a 20 20 7d 65 6c 73 65 0a 0a 23 65 6e 64 69 66  .  }else..#endif
2510d 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2510e 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
2510f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
25110 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 61  GMA [database.]a
25111 75 74 6f 5f 76 61 63 75 75 6d 0a 20 20 2a 2a 20  uto_vacuum.  ** 
25112 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
25113 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 3d 4e  e.]auto_vacuum=N
25114 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f  .  **.  ** Get o
25115 72 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  r set the value 
25116 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
25117 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 61  'auto-vacuum' pa
25118 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 54 68  rameter..  ** Th
25119 65 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f  e value is one o
2511a 66 3a 20 20 30 20 4e 4f 4e 45 20 31 20 46 55 4c  f:  0 NONE 1 FUL
2511b 4c 20 32 20 49 4e 43 52 45 4d 45 4e 54 41 4c 0a  L 2 INCREMENTAL.
2511c 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
2511d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
2511e 55 55 4d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  UUM.  if( sqlite
2511f 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
25120 61 75 74 6f 5f 76 61 63 75 75 6d 22 29 3d 3d 30  auto_vacuum")==0
25121 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70   ){.    Btree *p
25122 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20  Bt = pDb->pBt;. 
25123 20 20 20 61 73 73 65 72 74 28 20 70 42 74 21 3d     assert( pBt!=
25124 30 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  0 );.    if( sql
25125 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
25126 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20  Parse) ){.      
25127 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
25128 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
25129 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
2512a 69 6e 74 20 61 75 74 6f 5f 76 61 63 75 75 6d 3b  int auto_vacuum;
2512b 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59  .      if( ALWAY
2512c 53 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  S(pBt) ){.      
2512d 20 20 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d     auto_vacuum =
2512e 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
2512f 41 75 74 6f 56 61 63 75 75 6d 28 70 42 74 29 3b  AutoVacuum(pBt);
25130 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
25131 20 20 20 20 20 20 20 61 75 74 6f 5f 76 61 63 75         auto_vacu
25132 75 6d 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  um = SQLITE_DEFA
25133 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3b 0a  ULT_AUTOVACUUM;.
25134 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
25135 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50  turnSingleInt(pP
25136 61 72 73 65 2c 20 22 61 75 74 6f 5f 76 61 63 75  arse, "auto_vacu
25137 75 6d 22 2c 20 61 75 74 6f 5f 76 61 63 75 75 6d  um", auto_vacuum
25138 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
25139 20 20 20 20 69 6e 74 20 65 41 75 74 6f 20 3d 20      int eAuto = 
2513a 67 65 74 41 75 74 6f 56 61 63 75 75 6d 28 7a 52  getAutoVacuum(zR
2513b 69 67 68 74 29 3b 0a 20 20 20 20 20 20 61 73 73  ight);.      ass
2513c 65 72 74 28 20 65 41 75 74 6f 3e 3d 30 20 26 26  ert( eAuto>=0 &&
2513d 20 65 41 75 74 6f 3c 3d 32 20 29 3b 0a 20 20 20   eAuto<=2 );.   
2513e 20 20 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76     db->nextAutov
2513f 61 63 20 3d 20 28 75 38 29 65 41 75 74 6f 3b 0a  ac = (u8)eAuto;.
25140 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
25141 28 65 41 75 74 6f 3e 3d 30 29 20 29 7b 0a 20 20  (eAuto>=0) ){.  
25142 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 53 65        /* Call Se
25143 74 41 75 74 6f 56 61 63 75 75 6d 28 29 20 74 6f  tAutoVacuum() to
25144 20 73 65 74 20 69 6e 69 74 69 61 6c 69 7a 65 20   set initialize 
25145 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 61 75 74  the internal aut
25146 6f 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a  o and.        **
25147 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 66 6c 61   incr-vacuum fla
25148 67 73 2e 20 54 68 69 73 20 69 73 20 72 65 71 75  gs. This is requ
25149 69 72 65 64 20 69 6e 20 63 61 73 65 20 74 68 69  ired in case thi
2514a 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20  s connection.   
2514b 20 20 20 20 20 2a 2a 20 63 72 65 61 74 65 73 20       ** creates 
2514c 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
2514d 65 2e 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61  e. It is importa
2514e 6e 74 20 74 68 61 74 20 69 74 20 69 73 20 63 72  nt that it is cr
2514f 65 61 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  eated.        **
25150 20 61 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75   as an auto-vacu
25151 75 6d 20 63 61 70 61 62 6c 65 20 64 62 2e 0a 20  um capable db.. 
25152 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
25153 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
25154 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61  e3BtreeSetAutoVa
25155 63 75 75 6d 28 70 42 74 2c 20 65 41 75 74 6f 29  cuum(pBt, eAuto)
25156 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
25157 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28  ==SQLITE_OK && (
25158 65 41 75 74 6f 3d 3d 31 20 7c 7c 20 65 41 75 74  eAuto==1 || eAut
25159 6f 3d 3d 32 29 20 29 7b 0a 20 20 20 20 20 20 20  o==2) ){.       
2515a 20 20 20 2f 2a 20 57 68 65 6e 20 73 65 74 74 69     /* When setti
2515b 6e 67 20 74 68 65 20 61 75 74 6f 5f 76 61 63 75  ng the auto_vacu
2515c 75 6d 20 6d 6f 64 65 20 74 6f 20 65 69 74 68 65  um mode to eithe
2515d 72 20 22 66 75 6c 6c 22 20 6f 72 20 0a 20 20 20  r "full" or .   
2515e 20 20 20 20 20 20 20 2a 2a 20 22 69 6e 63 72 65         ** "incre
2515f 6d 65 6e 74 61 6c 22 2c 20 77 72 69 74 65 20 74  mental", write t
25160 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61  he value of meta
25161 5b 36 5d 20 69 6e 20 74 68 65 20 64 61 74 61 62  [6] in the datab
25162 61 73 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ase.          **
25163 20 66 69 6c 65 2e 20 42 65 66 6f 72 65 20 77 72   file. Before wr
25164 69 74 69 6e 67 20 74 6f 20 6d 65 74 61 5b 36 5d  iting to meta[6]
25165 2c 20 63 68 65 63 6b 20 74 68 61 74 20 6d 65 74  , check that met
25166 61 5b 33 5d 20 69 6e 64 69 63 61 74 65 73 0a 20  a[3] indicates. 
25167 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74           ** that
25168 20 74 68 69 73 20 72 65 61 6c 6c 79 20 69 73 20   this really is 
25169 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63  an auto-vacuum c
2516a 61 70 61 62 6c 65 20 64 61 74 61 62 61 73 65 2e  apable database.
2516b 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
2516c 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63          static c
2516d 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
2516e 73 65 74 4d 65 74 61 36 5b 5d 20 3d 20 7b 0a 20  setMeta6[] = {. 
2516f 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
25170 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20  Transaction,    
25171 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
25172 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20   1,        0},  
25173 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20    /* 0 */.      
25174 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43        { OP_ReadC
25175 6f 6f 6b 69 65 2c 20 20 20 20 20 30 2c 20 20 20  ookie,     0,   
25176 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 20              1,  
25177 20 20 20 20 20 20 33 7d 2c 20 20 20 20 2f 2a 20        3},    /* 
25178 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  1 */.           
25179 20 7b 20 4f 50 5f 49 66 2c 20 20 20 20 20 20 20   { OP_If,       
2517a 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 20 20        1,        
2517b 20 20 20 20 20 20 20 30 2c 20 20 20 20 20 20 20         0,       
2517c 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a   0},    /* 2 */.
2517d 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
2517e 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 20 20  _Halt,          
2517f 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 20 20 20 20   SQLITE_OK,     
25180 20 20 4f 45 5f 41 62 6f 72 74 2c 20 30 7d 2c 20    OE_Abort, 0}, 
25181 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20     /* 3 */.     
25182 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65         { OP_Inte
25183 67 65 72 2c 20 20 20 20 20 20 20 20 30 2c 20 20  ger,        0,  
25184 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
25185 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a         0},    /*
25186 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   4 */.          
25187 20 20 7b 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65    { OP_SetCookie
25188 2c 20 20 20 20 20 20 30 2c 20 20 20 20 20 20 20  ,      0,       
25189 20 20 20 20 20 20 20 20 36 2c 20 20 20 20 20 20          6,      
2518a 20 20 31 7d 2c 20 20 20 20 2f 2a 20 35 20 2a 2f    1},    /* 5 */
2518b 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20  .          };.  
2518c 20 20 20 20 20 20 20 20 69 6e 74 20 69 41 64 64          int iAdd
2518d 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 41 64  r;.          iAd
2518e 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
2518f 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72  AddOpList(v, Arr
25190 61 79 53 69 7a 65 28 73 65 74 4d 65 74 61 36 29  aySize(setMeta6)
25191 2c 20 73 65 74 4d 65 74 61 36 29 3b 0a 20 20 20  , setMeta6);.   
25192 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
25193 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41  beChangeP1(v, iA
25194 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  ddr, iDb);.     
25195 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25196 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64  ChangeP1(v, iAdd
25197 72 2b 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  r+1, iDb);.     
25198 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25199 43 68 61 6e 67 65 50 32 28 76 2c 20 69 41 64 64  ChangeP2(v, iAdd
2519a 72 2b 32 2c 20 69 41 64 64 72 2b 34 29 3b 0a 20  r+2, iAddr+4);. 
2519b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2519c 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
2519d 69 41 64 64 72 2b 34 2c 20 65 41 75 74 6f 2d 31  iAddr+4, eAuto-1
2519e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2519f 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
251a0 28 76 2c 20 69 41 64 64 72 2b 35 2c 20 69 44 62  (v, iAddr+5, iDb
251a1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
251a2 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
251a3 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  e(v, iDb);.     
251a4 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
251a5 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
251a6 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52  f..  /*.  **  PR
251a7 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
251a8 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75  incremental_vacu
251a9 75 6d 28 4e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20  um(N).  **.  ** 
251aa 44 6f 20 4e 20 73 74 65 70 73 20 6f 66 20 69 6e  Do N steps of in
251ab 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
251ac 69 6e 67 20 6f 6e 20 61 20 64 61 74 61 62 61 73  ing on a databas
251ad 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  e..  */.#ifndef 
251ae 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
251af 56 41 43 55 55 4d 0a 20 20 69 66 28 20 73 71 6c  VACUUM.  if( sql
251b0 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
251b1 74 2c 22 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76  t,"incremental_v
251b2 61 63 75 75 6d 22 29 3d 3d 30 20 29 7b 0a 20 20  acuum")==0 ){.  
251b3 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 61 64    int iLimit, ad
251b4 64 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  dr;.    if( sqli
251b5 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
251b6 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67  arse) ){.      g
251b7 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
251b8 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 52      }.    if( zR
251b9 69 67 68 74 3d 3d 30 20 7c 7c 20 21 73 71 6c 69  ight==0 || !sqli
251ba 74 65 33 47 65 74 49 6e 74 33 32 28 7a 52 69 67  te3GetInt32(zRig
251bb 68 74 2c 20 26 69 4c 69 6d 69 74 29 20 7c 7c 20  ht, &iLimit) || 
251bc 69 4c 69 6d 69 74 3c 3d 30 20 29 7b 0a 20 20 20  iLimit<=0 ){.   
251bd 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 78 37 66     iLimit = 0x7f
251be 66 66 66 66 66 66 3b 0a 20 20 20 20 7d 0a 20 20  ffffff;.    }.  
251bf 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
251c0 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
251c1 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20  rse, 0, iDb);.  
251c2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
251c3 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
251c4 72 2c 20 69 4c 69 6d 69 74 2c 20 31 29 3b 0a 20  r, iLimit, 1);. 
251c5 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
251c6 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
251c7 50 5f 49 6e 63 72 56 61 63 75 75 6d 2c 20 69 44  P_IncrVacuum, iD
251c8 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  b);.    sqlite3V
251c9 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
251ca 52 65 73 75 6c 74 52 6f 77 2c 20 31 29 3b 0a 20  ResultRow, 1);. 
251cb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
251cc 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
251cd 6d 2c 20 31 2c 20 2d 31 29 3b 0a 20 20 20 20 73  m, 1, -1);.    s
251ce 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
251cf 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 2c  (v, OP_IfPos, 1,
251d0 20 61 64 64 72 29 3b 0a 20 20 20 20 73 71 6c 69   addr);.    sqli
251d1 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
251d2 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 65 6c 73  v, addr);.  }els
251d3 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  e.#endif..#ifnde
251d4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  f SQLITE_OMIT_PA
251d5 47 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a  GER_PRAGMAS.  /*
251d6 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
251d7 61 74 61 62 61 73 65 2e 5d 63 61 63 68 65 5f 73  atabase.]cache_s
251d8 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41  ize.  **  PRAGMA
251d9 20 5b 64 61 74 61 62 61 73 65 2e 5d 63 61 63 68   [database.]cach
251da 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20  e_size=N.  **.  
251db 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72  ** The first for
251dc 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75  m reports the cu
251dd 72 72 65 6e 74 20 6c 6f 63 61 6c 20 73 65 74 74  rrent local sett
251de 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ing for the.  **
251df 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
251e0 2e 20 20 54 68 65 20 6c 6f 63 61 6c 20 73 65 74  .  The local set
251e1 74 69 6e 67 20 63 61 6e 20 62 65 20 64 69 66 66  ting can be diff
251e2 65 72 65 6e 74 20 66 72 6f 6d 0a 20 20 2a 2a 20  erent from.  ** 
251e3 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 63  the persistent c
251e4 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 20  ache size value 
251e5 74 68 61 74 20 69 73 20 73 74 6f 72 65 64 20 69  that is stored i
251e6 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20  n the database. 
251e7 20 2a 2a 20 66 69 6c 65 20 69 74 73 65 6c 66 2e   ** file itself.
251e8 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75    The value retu
251e9 72 6e 65 64 20 69 73 20 74 68 65 20 6d 61 78 69  rned is the maxi
251ea 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20  mum number of.  
251eb 2a 2a 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ** pages in the 
251ec 70 61 67 65 20 63 61 63 68 65 2e 20 20 54 68 65  page cache.  The
251ed 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74   second form set
251ee 73 20 74 68 65 20 6c 6f 63 61 6c 0a 20 20 2a 2a  s the local.  **
251ef 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
251f0 20 76 61 6c 75 65 2e 20 20 49 74 20 64 6f 65 73   value.  It does
251f1 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20   not change the 
251f2 70 65 72 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20  persistent.  ** 
251f3 63 61 63 68 65 20 73 69 7a 65 20 73 74 6f 72 65  cache size store
251f4 64 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 73 6f  d on the disk so
251f5 20 74 68 65 20 63 61 63 68 65 20 73 69 7a 65 20   the cache size 
251f6 77 69 6c 6c 20 72 65 76 65 72 74 0a 20 20 2a 2a  will revert.  **
251f7 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20   to its default 
251f8 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 20 64  value when the d
251f9 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65  atabase is close
251fa 64 20 61 6e 64 20 72 65 6f 70 65 6e 65 64 2e 0a  d and reopened..
251fb 20 20 2a 2a 20 4e 20 73 68 6f 75 6c 64 20 62 65    ** N should be
251fc 20 61 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65   a positive inte
251fd 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ger..  */.  if( 
251fe 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
251ff 4c 65 66 74 2c 22 63 61 63 68 65 5f 73 69 7a 65  Left,"cache_size
25200 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
25201 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
25202 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
25203 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
25204 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
25205 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e  .      returnSin
25206 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22  gleInt(pParse, "
25207 63 61 63 68 65 5f 73 69 7a 65 22 2c 20 70 44 62  cache_size", pDb
25208 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65  ->pSchema->cache
25209 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73  _size);.    }els
2520a 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a  e{.      int siz
2520b 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29  e = atoi(zRight)
2520c 3b 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a 65  ;.      if( size
2520d 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a  <0 ) size = -siz
2520e 65 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 53  e;.      pDb->pS
2520f 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a  chema->cache_siz
25210 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20  e = size;.      
25211 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43  sqlite3BtreeSetC
25212 61 63 68 65 53 69 7a 65 28 70 44 62 2d 3e 70 42  acheSize(pDb->pB
25213 74 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  t, pDb->pSchema-
25214 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20  >cache_size);.  
25215 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f    }.  }else..  /
25216 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  *.  **   PRAGMA 
25217 74 65 6d 70 5f 73 74 6f 72 65 0a 20 20 2a 2a 20  temp_store.  ** 
25218 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74    PRAGMA temp_st
25219 6f 72 65 20 3d 20 22 64 65 66 61 75 6c 74 22 7c  ore = "default"|
2521a 22 6d 65 6d 6f 72 79 22 7c 22 66 69 6c 65 22 0a  "memory"|"file".
2521b 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    **.  ** Return
2521c 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61   or set the loca
2521d 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74  l value of the t
2521e 65 6d 70 5f 73 74 6f 72 65 20 66 6c 61 67 2e 20  emp_store flag. 
2521f 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74   Changing.  ** t
25220 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 64  he local value d
25221 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63 68 61  oes not make cha
25222 6e 67 65 73 20 74 6f 20 74 68 65 20 64 69 73 6b  nges to the disk
25223 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 64 65   file and the de
25224 66 61 75 6c 74 0a 20 20 2a 2a 20 76 61 6c 75 65  fault.  ** value
25225 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65   will be restore
25226 64 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20  d the next time 
25227 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
25228 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a  opened..  **.  *
25229 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 74 20 69  * Note that it i
2522a 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74  s possible for t
2522b 68 65 20 6c 69 62 72 61 72 79 20 63 6f 6d 70 69  he library compi
2522c 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 73 20  le-time options 
2522d 74 6f 0a 20 20 2a 2a 20 6f 76 65 72 72 69 64 65  to.  ** override
2522e 20 74 68 69 73 20 73 65 74 74 69 6e 67 0a 20 20   this setting.  
2522f 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
25230 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
25231 74 65 6d 70 5f 73 74 6f 72 65 22 29 3d 3d 30 20  temp_store")==0 
25232 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67  ){.    if( !zRig
25233 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ht ){.      retu
25234 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
25235 73 65 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65 22  se, "temp_store"
25236 2c 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65  , db->temp_store
25237 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
25238 20 20 20 20 63 68 61 6e 67 65 54 65 6d 70 53 74      changeTempSt
25239 6f 72 61 67 65 28 70 50 61 72 73 65 2c 20 7a 52  orage(pParse, zR
2523a 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ight);.    }.  }
2523b 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  else..  /*.  ** 
2523c 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74    PRAGMA temp_st
2523d 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 0a 20 20  ore_directory.  
2523e 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70  **   PRAGMA temp
2523f 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
25240 20 3d 20 22 22 7c 22 64 69 72 65 63 74 6f 72 79   = ""|"directory
25241 5f 6e 61 6d 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a  _name".  **.  **
25242 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74   Return or set t
25243 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f  he local value o
25244 66 20 74 68 65 20 74 65 6d 70 5f 73 74 6f 72 65  f the temp_store
25245 5f 64 69 72 65 63 74 6f 72 79 20 66 6c 61 67 2e  _directory flag.
25246 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a 20    Changing.  ** 
25247 74 68 65 20 76 61 6c 75 65 20 73 65 74 73 20 61  the value sets a
25248 20 73 70 65 63 69 66 69 63 20 64 69 72 65 63 74   specific direct
25249 6f 72 79 20 74 6f 20 62 65 20 75 73 65 64 20 66  ory to be used f
2524a 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  or temporary fil
2524b 65 73 2e 0a 20 20 2a 2a 20 53 65 74 74 69 6e 67  es..  ** Setting
2524c 20 74 6f 20 61 20 6e 75 6c 6c 20 73 74 72 69 6e   to a null strin
2524d 67 20 72 65 76 65 72 74 73 20 74 6f 20 74 68 65  g reverts to the
2524e 20 64 65 66 61 75 6c 74 20 74 65 6d 70 6f 72 61   default tempora
2524f 72 79 20 64 69 72 65 63 74 6f 72 79 20 73 65 61  ry directory sea
25250 72 63 68 2e 0a 20 20 2a 2a 20 49 66 20 74 65 6d  rch..  ** If tem
25251 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f 72 79  porary directory
25252 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74 68 65   is changed, the
25253 6e 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70  n invalidateTemp
25254 53 74 6f 72 61 67 65 2e 0a 20 20 2a 2a 0a 20 20  Storage..  **.  
25255 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
25256 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
25257 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
25258 74 6f 72 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20  tory")==0 ){.   
25259 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a   if( !zRight ){.
2525a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2525b 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
2525c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2525d 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
2525e 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  s(v, 1);.       
2525f 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
25260 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
25261 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20  NAME_NAME, .    
25262 20 20 20 20 20 20 20 20 22 74 65 6d 70 5f 73 74          "temp_st
25263 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22 2c 20  ore_directory", 
25264 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
25265 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
25266 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
25267 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30  String8, 0, 1, 0
25268 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64  , sqlite3_temp_d
25269 69 72 65 63 74 6f 72 79 2c 20 30 29 3b 0a 20 20  irectory, 0);.  
2526a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2526b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
2526c 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a  sultRow, 1, 1);.
2526d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
2526e 65 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e{.#ifndef SQLIT
2526f 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 20 20 20  E_OMIT_WSD.     
25270 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 29   if( zRight[0] )
25271 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63  {.        int rc
25272 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 65  ;.        int re
25273 73 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  s;.        rc = 
25274 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
25275 64 62 2d 3e 70 56 66 73 2c 20 7a 52 69 67 68 74  db->pVfs, zRight
25276 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  , SQLITE_ACCESS_
25277 52 45 41 44 57 52 49 54 45 2c 20 26 72 65 73 29  READWRITE, &res)
25278 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
25279 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72  !=SQLITE_OK || r
2527a 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  es==0 ){.       
2527b 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2527c 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20  sg(pParse, "not 
2527d 61 20 77 72 69 74 61 62 6c 65 20 64 69 72 65 63  a writable direc
2527e 74 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 20 20  tory");.        
2527f 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75    goto pragma_ou
25280 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
25281 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 53     }.      if( S
25282 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45  QLITE_TEMP_STORE
25283 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 28 53  ==0.       || (S
25284 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45  QLITE_TEMP_STORE
25285 3d 3d 31 20 26 26 20 64 62 2d 3e 74 65 6d 70 5f  ==1 && db->temp_
25286 73 74 6f 72 65 3c 3d 31 29 0a 20 20 20 20 20 20  store<=1).      
25287 20 7c 7c 20 28 53 51 4c 49 54 45 5f 54 45 4d 50   || (SQLITE_TEMP
25288 5f 53 54 4f 52 45 3d 3d 32 20 26 26 20 64 62 2d  _STORE==2 && db-
25289 3e 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 31 29 0a  >temp_store==1).
2528a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
2528b 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53   invalidateTempS
2528c 74 6f 72 61 67 65 28 70 50 61 72 73 65 29 3b 0a  torage(pParse);.
2528d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2528e 6c 69 74 65 33 5f 66 72 65 65 28 73 71 6c 69 74  lite3_free(sqlit
2528f 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72  e3_temp_director
25290 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 52  y);.      if( zR
25291 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20  ight[0] ){.     
25292 20 20 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f     sqlite3_temp_
25293 64 69 72 65 63 74 6f 72 79 20 3d 20 73 71 6c 69  directory = sqli
25294 74 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 7a  te3DbStrDup(0, z
25295 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 7d 65  Right);.      }e
25296 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
25297 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
25298 6f 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ory = 0;.      }
25299 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2529a 45 5f 4f 4d 49 54 5f 57 53 44 20 2a 2f 0a 20 20  E_OMIT_WSD */.  
2529b 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 23 69 66    }.  }else..#if
2529c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2529d 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
2529e 53 54 59 4c 45 29 0a 23 20 20 69 66 20 64 65 66  STYLE).#  if def
2529f 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a  ined(__APPLE__).
252a0 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  #    define SQLI
252a1 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
252a2 47 5f 53 54 59 4c 45 20 31 0a 23 20 20 65 6c 73  G_STYLE 1.#  els
252a3 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51  e.#    define SQ
252a4 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
252a5 49 4e 47 5f 53 54 59 4c 45 20 30 0a 23 20 20 65  ING_STYLE 0.#  e
252a6 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 20  ndif.#endif.#if 
252a7 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
252a8 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 2f 2a  CKING_STYLE.  /*
252a9 0a 20 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  .   **   PRAGMA 
252aa 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f  [database.]lock_
252ab 70 72 6f 78 79 5f 66 69 6c 65 0a 20 20 20 2a 2a  proxy_file.   **
252ac 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
252ad 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f  ase.]lock_proxy_
252ae 66 69 6c 65 20 3d 20 22 3a 61 75 74 6f 3a 22 7c  file = ":auto:"|
252af 22 6c 6f 63 6b 5f 66 69 6c 65 5f 70 61 74 68 22  "lock_file_path"
252b0 0a 20 20 20 2a 2a 0a 20 20 20 2a 2a 20 52 65 74  .   **.   ** Ret
252b1 75 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20 76  urn or set the v
252b2 61 6c 75 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b  alue of the lock
252b3 5f 70 72 6f 78 79 5f 66 69 6c 65 20 66 6c 61 67  _proxy_file flag
252b4 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 20 2a  .  Changing.   *
252b5 2a 20 74 68 65 20 76 61 6c 75 65 20 73 65 74 73  * the value sets
252b6 20 61 20 73 70 65 63 69 66 69 63 20 66 69 6c 65   a specific file
252b7 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20   to be used for 
252b8 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20  database access 
252b9 6c 6f 63 6b 73 2e 0a 20 20 20 2a 2a 0a 20 20 20  locks..   **.   
252ba 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
252bb 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
252bc 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 22  lock_proxy_file"
252bd 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
252be 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
252bf 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
252c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
252c1 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  er(pDb->pBt);.  
252c2 20 20 20 20 63 68 61 72 20 2a 70 72 6f 78 79 5f      char *proxy_
252c3 66 69 6c 65 5f 70 61 74 68 20 3d 20 4e 55 4c 4c  file_path = NULL
252c4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
252c5 66 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 73 71  file *pFile = sq
252c6 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70  lite3PagerFile(p
252c7 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 73 71  Pager);.      sq
252c8 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
252c9 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45  ol(pFile, SQLITE
252ca 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _GET_LOCKPROXYFI
252cb 4c 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  LE, .           
252cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252cd 26 70 72 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68  &proxy_file_path
252ce 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20 20  );.      .      
252cf 69 66 28 20 70 72 6f 78 79 5f 66 69 6c 65 5f 70  if( proxy_file_p
252d0 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ath ){.        s
252d1 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
252d2 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
252d3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
252d4 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
252d5 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20  COLNAME_NAME, . 
252d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252d7 20 20 20 20 20 20 20 20 20 20 20 20 20 22 6c 6f               "lo
252d8 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 22 2c 20  ck_proxy_file", 
252d9 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
252da 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
252db 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
252dc 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30  String8, 0, 1, 0
252dd 2c 20 70 72 6f 78 79 5f 66 69 6c 65 5f 70 61 74  , proxy_file_pat
252de 68 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  h, 0);.        s
252df 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
252e0 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
252e1 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d  , 1, 1);.      }
252e2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
252e3 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
252e4 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
252e5 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20  ger(pDb->pBt);. 
252e6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c       sqlite3_fil
252e7 65 20 2a 70 46 69 6c 65 20 3d 20 73 71 6c 69 74  e *pFile = sqlit
252e8 65 33 50 61 67 65 72 46 69 6c 65 28 70 50 61 67  e3PagerFile(pPag
252e9 65 72 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  er);.      int r
252ea 65 73 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 52  es;.      if( zR
252eb 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20  ight[0] ){.     
252ec 20 20 20 72 65 73 3d 73 71 6c 69 74 65 33 4f 73     res=sqlite3Os
252ed 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 46 69 6c  FileControl(pFil
252ee 65 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f  e, SQLITE_SET_LO
252ef 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 0a 20 20  CKPROXYFILE, .  
252f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252f2 20 20 20 7a 52 69 67 68 74 29 3b 0a 20 20 20 20     zRight);.    
252f3 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
252f4 20 20 20 72 65 73 3d 73 71 6c 69 74 65 33 4f 73     res=sqlite3Os
252f5 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 46 69 6c  FileControl(pFil
252f6 65 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f  e, SQLITE_SET_LO
252f7 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 0a 20 20  CKPROXYFILE, .  
252f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252fa 20 20 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20     NULL);.      
252fb 7d 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 21  }.      if( res!
252fc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
252fd 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
252fe 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 66  orMsg(pParse, "f
252ff 61 69 6c 65 64 20 74 6f 20 73 65 74 20 6c 6f 63  ailed to set loc
25300 6b 20 70 72 6f 78 79 20 66 69 6c 65 22 29 3b 0a  k proxy file");.
25301 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61          goto pra
25302 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d  gma_out;.      }
25303 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23  .    }.  }else.#
25304 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
25305 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
25306 54 59 4c 45 20 2a 2f 20 20 20 20 20 20 0a 20 20  TYLE */      .  
25307 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50    .  /*.  **   P
25308 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
25309 5d 73 79 6e 63 68 72 6f 6e 6f 75 73 0a 20 20 2a  ]synchronous.  *
2530a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  *   PRAGMA [data
2530b 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e 6f 75  base.]synchronou
2530c 73 3d 4f 46 46 7c 4f 4e 7c 4e 4f 52 4d 41 4c 7c  s=OFF|ON|NORMAL|
2530d 46 55 4c 4c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52  FULL.  **.  ** R
2530e 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68 65  eturn or set the
2530f 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f 66 20   local value of 
25310 74 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20  the synchronous 
25311 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a  flag.  Changing.
25312 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20 76    ** the local v
25313 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61  alue does not ma
25314 6b 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68  ke changes to th
25315 65 20 64 69 73 6b 20 66 69 6c 65 20 61 6e 64 20  e disk file and 
25316 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75 6c 74  the.  ** default
25317 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 72   value will be r
25318 65 73 74 6f 72 65 64 20 74 68 65 20 6e 65 78 74  estored the next
25319 20 74 69 6d 65 20 74 68 65 20 64 61 74 61 62 61   time the databa
2531a 73 65 20 69 73 0a 20 20 2a 2a 20 6f 70 65 6e 65  se is.  ** opene
2531b 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  d..  */.  if( sq
2531c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
2531d 66 74 2c 22 73 79 6e 63 68 72 6f 6e 6f 75 73 22  ft,"synchronous"
2531e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
2531f 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
25320 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
25321 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
25322 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a   if( !zRight ){.
25323 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67        returnSing
25324 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 73  leInt(pParse, "s
25325 79 6e 63 68 72 6f 6e 6f 75 73 22 2c 20 70 44 62  ynchronous", pDb
25326 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 2d 31  ->safety_level-1
25327 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
25328 20 20 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74      if( !db->aut
25329 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 20  oCommit ){.     
2532a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2532b 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
2532c 20 20 20 20 20 20 20 20 22 53 61 66 65 74 79 20          "Safety 
2532d 6c 65 76 65 6c 20 6d 61 79 20 6e 6f 74 20 62 65  level may not be
2532e 20 63 68 61 6e 67 65 64 20 69 6e 73 69 64 65 20   changed inside 
2532f 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b  a transaction");
25330 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
25331 20 20 20 20 20 20 70 44 62 2d 3e 73 61 66 65 74        pDb->safet
25332 79 5f 6c 65 76 65 6c 20 3d 20 67 65 74 53 61 66  y_level = getSaf
25333 65 74 79 4c 65 76 65 6c 28 7a 52 69 67 68 74 29  etyLevel(zRight)
25334 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  +1;.      }.    
25335 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
25336 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
25337 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
25338 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
25339 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d  _OMIT_FLAG_PRAGM
2533a 41 53 0a 20 20 69 66 28 20 66 6c 61 67 50 72 61  AS.  if( flagPra
2533b 67 6d 61 28 70 50 61 72 73 65 2c 20 7a 4c 65 66  gma(pParse, zLef
2533c 74 2c 20 7a 52 69 67 68 74 29 20 29 7b 0a 20 20  t, zRight) ){.  
2533d 20 20 2f 2a 20 54 68 65 20 66 6c 61 67 50 72 61    /* The flagPra
2533e 67 6d 61 28 29 20 73 75 62 72 6f 75 74 69 6e 65  gma() subroutine
2533f 20 61 6c 73 6f 20 67 65 6e 65 72 61 74 65 73 20   also generates 
25340 61 6e 79 20 6e 65 63 65 73 73 61 72 79 20 63 6f  any necessary co
25341 64 65 0a 20 20 20 20 2a 2a 20 74 68 65 72 65 20  de.    ** there 
25342 69 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20  is nothing more 
25343 74 6f 20 64 6f 20 68 65 72 65 20 2a 2f 0a 20 20  to do here */.  
25344 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
25345 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47  SQLITE_OMIT_FLAG
25346 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66  _PRAGMAS */..#if
25347 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
25348 5f 53 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 0a  _SCHEMA_PRAGMAS.
25349 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47    /*.  **   PRAG
2534a 4d 41 20 74 61 62 6c 65 5f 69 6e 66 6f 28 3c 74  MA table_info(<t
2534b 61 62 6c 65 3e 29 0a 20 20 2a 2a 0a 20 20 2a 2a  able>).  **.  **
2534c 20 52 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65   Return a single
2534d 20 72 6f 77 20 66 6f 72 20 65 61 63 68 20 63 6f   row for each co
2534e 6c 75 6d 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65  lumn of the name
2534f 64 20 74 61 62 6c 65 2e 20 54 68 65 20 63 6f 6c  d table. The col
25350 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65  umns of.  ** the
25351 20 72 65 74 75 72 6e 65 64 20 64 61 74 61 20 73   returned data s
25352 65 74 20 61 72 65 3a 0a 20 20 2a 2a 0a 20 20 2a  et are:.  **.  *
25353 2a 20 63 69 64 3a 20 20 20 20 20 20 20 20 43 6f  * cid:        Co
25354 6c 75 6d 6e 20 69 64 20 28 6e 75 6d 62 65 72 65  lumn id (numbere
25355 64 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72  d from left to r
25356 69 67 68 74 2c 20 73 74 61 72 74 69 6e 67 20 61  ight, starting a
25357 74 20 30 29 0a 20 20 2a 2a 20 6e 61 6d 65 3a 20  t 0).  ** name: 
25358 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 6e 61 6d        Column nam
25359 65 0a 20 20 2a 2a 20 74 79 70 65 3a 20 20 20 20  e.  ** type:    
2535a 20 20 20 43 6f 6c 75 6d 6e 20 64 65 63 6c 61 72     Column declar
2535b 61 74 69 6f 6e 20 74 79 70 65 2e 0a 20 20 2a 2a  ation type..  **
2535c 20 6e 6f 74 6e 75 6c 6c 3a 20 20 20 20 54 72 75   notnull:    Tru
2535d 65 20 69 66 20 27 4e 4f 54 20 4e 55 4c 4c 27 20  e if 'NOT NULL' 
2535e 69 73 20 70 61 72 74 20 6f 66 20 63 6f 6c 75 6d  is part of colum
2535f 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 20 20  n declaration.  
25360 2a 2a 20 64 66 6c 74 5f 76 61 6c 75 65 3a 20 54  ** dflt_value: T
25361 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
25362 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 2c   for the column,
25363 20 69 66 20 61 6e 79 2e 0a 20 20 2a 2f 0a 20 20   if any..  */.  
25364 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
25365 6d 70 28 7a 4c 65 66 74 2c 20 22 74 61 62 6c 65  mp(zLeft, "table
25366 5f 69 6e 66 6f 22 29 3d 3d 30 20 26 26 20 7a 52  _info")==0 && zR
25367 69 67 68 74 20 29 7b 0a 20 20 20 20 54 61 62 6c  ight ){.    Tabl
25368 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28  e *pTab;.    if(
25369 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
2536a 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
2536b 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
2536c 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
2536d 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
2536e 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20  ight, zDb);.    
2536f 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
25370 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69    int i;.      i
25371 6e 74 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 0a  nt nHidden = 0;.
25372 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43        Column *pC
25373 6f 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ol;.      sqlite
25374 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
25375 76 2c 20 36 29 3b 0a 20 20 20 20 20 20 70 50 61  v, 6);.      pPa
25376 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b 0a 20  rse->nMem = 6;. 
25377 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25378 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
25379 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
2537a 63 69 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  cid", SQLITE_STA
2537b 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
2537c 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2537d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 1, COLNAME_
2537e 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53 51  NAME, "name", SQ
2537f 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
25380 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
25381 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20  etColName(v, 2, 
25382 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74  COLNAME_NAME, "t
25383 79 70 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ype", SQLITE_STA
25384 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
25385 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
25386 65 28 76 2c 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 3, COLNAME_
25387 4e 41 4d 45 2c 20 22 6e 6f 74 6e 75 6c 6c 22 2c  NAME, "notnull",
25388 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
25389 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2538a 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2538b 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  4, COLNAME_NAME,
2538c 20 22 64 66 6c 74 5f 76 61 6c 75 65 22 2c 20 53   "dflt_value", S
2538d 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2538e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2538f 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c  SetColName(v, 5,
25390 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
25391 70 6b 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  pk", SQLITE_STAT
25392 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
25393 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
25394 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
25395 62 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  b);.      for(i=
25396 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e 61 43  0, pCol=pTab->aC
25397 6f 6c 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  ol; i<pTab->nCol
25398 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
25399 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 54 6f          const To
2539a 6b 65 6e 20 2a 70 44 66 6c 74 3b 0a 20 20 20 20  ken *pDflt;.    
2539b 20 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e      if( IsHidden
2539c 43 6f 6c 75 6d 6e 28 70 43 6f 6c 29 20 29 7b 0a  Column(pCol) ){.
2539d 20 20 20 20 20 20 20 20 20 20 6e 48 69 64 64 65            nHidde
2539e 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 63  n++;.          c
2539f 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
253a0 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
253a1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
253a2 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2d 6e 48  OP_Integer, i-nH
253a3 69 64 64 65 6e 2c 20 31 29 3b 0a 20 20 20 20 20  idden, 1);.     
253a4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
253a5 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
253a6 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f  g8, 0, 2, 0, pCo
253a7 6c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  l->zName, 0);.  
253a8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
253a9 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
253aa 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a  ring8, 0, 3, 0,.
253ab 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
253ac 3e 7a 54 79 70 65 20 3f 20 70 43 6f 6c 2d 3e 7a  >zType ? pCol->z
253ad 54 79 70 65 20 3a 20 22 22 2c 20 30 29 3b 0a 20  Type : "", 0);. 
253ae 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
253af 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
253b0 6e 74 65 67 65 72 2c 20 28 70 43 6f 6c 2d 3e 6e  nteger, (pCol->n
253b1 6f 74 4e 75 6c 6c 20 3f 20 31 20 3a 20 30 29 2c  otNull ? 1 : 0),
253b2 20 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   4);.        if(
253b3 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 29 7b 0a   pCol->pDflt ){.
253b4 20 20 20 20 20 20 20 20 20 20 70 44 66 6c 74 20            pDflt 
253b5 3d 20 26 70 43 6f 6c 2d 3e 70 44 66 6c 74 2d 3e  = &pCol->pDflt->
253b6 73 70 61 6e 3b 0a 20 20 20 20 20 20 20 20 20 20  span;.          
253b7 61 73 73 65 72 74 28 20 70 44 66 6c 74 2d 3e 7a  assert( pDflt->z
253b8 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71   );.          sq
253b9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
253ba 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
253bb 2c 20 35 2c 20 30 2c 20 28 63 68 61 72 2a 29 70  , 5, 0, (char*)p
253bc 44 66 6c 74 2d 3e 7a 2c 20 70 44 66 6c 74 2d 3e  Dflt->z, pDflt->
253bd 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  n);.        }els
253be 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  e{.          sql
253bf 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
253c0 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35 29  , OP_Null, 0, 5)
253c1 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
253c2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
253c3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
253c4 67 65 72 2c 20 70 43 6f 6c 2d 3e 69 73 50 72 69  ger, pCol->isPri
253c5 6d 4b 65 79 2c 20 36 29 3b 0a 20 20 20 20 20 20  mKey, 6);.      
253c6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
253c7 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
253c8 52 6f 77 2c 20 31 2c 20 36 29 3b 0a 20 20 20 20  Row, 1, 6);.    
253c9 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
253ca 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  e..  if( sqlite3
253cb 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
253cc 69 6e 64 65 78 5f 69 6e 66 6f 22 29 3d 3d 30 20  index_info")==0 
253cd 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  && zRight ){.   
253ce 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
253cf 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
253d0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
253d1 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
253d2 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
253d3 75 74 3b 0a 20 20 20 20 70 49 64 78 20 3d 20 73  ut;.    pIdx = s
253d4 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
253d5 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29  db, zRight, zDb)
253d6 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 20 29  ;.    if( pIdx )
253d7 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  {.      int i;. 
253d8 20 20 20 20 20 70 54 61 62 20 3d 20 70 49 64 78       pTab = pIdx
253d9 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->pTable;.      
253da 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
253db 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20  mCols(v, 3);.   
253dc 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
253dd 3d 20 33 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 3;.      sqlit
253de 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
253df 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
253e0 41 4d 45 2c 20 22 73 65 71 6e 6f 22 2c 20 53 51  AME, "seqno", SQ
253e1 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
253e2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
253e3 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20  etColName(v, 1, 
253e4 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63  COLNAME_NAME, "c
253e5 69 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  id", SQLITE_STAT
253e6 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
253e7 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
253e8 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 2, COLNAME_N
253e9 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c  AME, "name", SQL
253ea 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
253eb 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49     for(i=0; i<pI
253ec 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  dx->nColumn; i++
253ed 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63  ){.        int c
253ee 6e 75 6d 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f  num = pIdx->aiCo
253ef 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20  lumn[i];.       
253f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
253f1 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
253f2 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  , i, 1);.       
253f3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
253f4 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
253f5 2c 20 63 6e 75 6d 2c 20 32 29 3b 0a 20 20 20 20  , cnum, 2);.    
253f6 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
253f7 2d 3e 6e 43 6f 6c 3e 63 6e 75 6d 20 29 3b 0a 20  ->nCol>cnum );. 
253f8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
253f9 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
253fa 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c  tring8, 0, 3, 0,
253fb 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 63 6e 75 6d   pTab->aCol[cnum
253fc 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ].zName, 0);.   
253fd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
253fe 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
253ff 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20  ultRow, 1, 3);. 
25400 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
25401 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c 69  else..  if( sqli
25402 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
25403 2c 20 22 69 6e 64 65 78 5f 6c 69 73 74 22 29 3d  , "index_list")=
25404 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a  =0 && zRight ){.
25405 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b      Index *pIdx;
25406 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
25407 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
25408 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
25409 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
2540a 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20  a_out;.    pTab 
2540b 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
2540c 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a  le(db, zRight, z
2540d 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Db);.    if( pTa
2540e 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 73  b ){.      v = s
2540f 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
25410 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 49 64  arse);.      pId
25411 78 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78  x = pTab->pIndex
25412 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78  ;.      if( pIdx
25413 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
25414 69 20 3d 20 30 3b 20 0a 20 20 20 20 20 20 20 20  i = 0; .        
25415 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
25416 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20  mCols(v, 3);.   
25417 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
25418 6d 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 73  m = 3;.        s
25419 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2541a 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
2541b 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20  ME_NAME, "seq", 
2541c 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
2541d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2541e 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
2541f 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
25420 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45  , "name", SQLITE
25421 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
25422 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
25423 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f  ColName(v, 2, CO
25424 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 75 6e 69  LNAME_NAME, "uni
25425 71 75 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  que", SQLITE_STA
25426 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 77 68  TIC);.        wh
25427 69 6c 65 28 70 49 64 78 29 7b 0a 20 20 20 20 20  ile(pIdx){.     
25428 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25429 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2542a 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20  eger, i, 1);.   
2542b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2542c 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
2542d 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c  tring8, 0, 2, 0,
2542e 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pIdx->zName, 0)
2542f 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
25430 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
25431 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 49 64   OP_Integer, pId
25432 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
25433 6f 6e 65 2c 20 33 29 3b 0a 20 20 20 20 20 20 20  one, 3);.       
25434 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25435 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
25436 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20 20 20  tRow, 1, 3);.   
25437 20 20 20 20 20 20 20 2b 2b 69 3b 0a 20 20 20 20         ++i;.    
25438 20 20 20 20 20 20 70 49 64 78 20 3d 20 70 49 64        pIdx = pId
25439 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  x->pNext;.      
2543a 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2543b 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28  }.  }else..  if(
2543c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2543d 7a 4c 65 66 74 2c 20 22 64 61 74 61 62 61 73 65  zLeft, "database
2543e 5f 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a 20 20  _list")==0 ){.  
2543f 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28    int i;.    if(
25440 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
25441 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
25442 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
25443 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
25444 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20  NumCols(v, 3);. 
25445 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
25446 3d 20 33 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 3;.    sqlite3
25447 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
25448 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
25449 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49 54 45  E, "seq", SQLITE
2544a 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
2544b 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2544c 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d  ame(v, 1, COLNAM
2544d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20  E_NAME, "name", 
2544e 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
2544f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
25450 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20  etColName(v, 2, 
25451 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66  COLNAME_NAME, "f
25452 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ile", SQLITE_STA
25453 54 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  TIC);.    for(i=
25454 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
25455 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62  +){.      if( db
25456 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3d 3d 30 20  ->aDb[i].pBt==0 
25457 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
25458 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
25459 62 5b 69 5d 2e 7a 4e 61 6d 65 21 3d 30 20 29 3b  b[i].zName!=0 );
2545a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2545b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2545c 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20  nteger, i, 1);. 
2545d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2545e 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
2545f 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 64  ing8, 0, 2, 0, d
25460 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c  b->aDb[i].zName,
25461 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
25462 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
25463 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33  OP_String8, 0, 3
25464 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
25465 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46  sqlite3BtreeGetF
25466 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b  ilename(db->aDb[
25467 69 5d 2e 70 42 74 29 2c 20 30 29 3b 0a 20 20 20  i].pBt), 0);.   
25468 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25469 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
2546a 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20 20 20  tRow, 1, 3);.   
2546b 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66   }.  }else..  if
2546c 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
2546d 28 7a 4c 65 66 74 2c 20 22 63 6f 6c 6c 61 74 69  (zLeft, "collati
2546e 6f 6e 5f 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a  on_list")==0 ){.
2546f 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20      int i = 0;. 
25470 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a     HashElem *p;.
25471 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
25472 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 32 29 3b  etNumCols(v, 2);
25473 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
25474 6d 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74  m = 2;.    sqlit
25475 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
25476 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
25477 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49  AME, "seq", SQLI
25478 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
25479 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2547a 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
2547b 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
2547c 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2547d 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69  ;.    for(p=sqli
2547e 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d  teHashFirst(&db-
2547f 3e 61 43 6f 6c 6c 53 65 71 29 3b 20 70 3b 20 70  >aCollSeq); p; p
25480 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
25481 70 29 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  p)){.      CollS
25482 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c  eq *pColl = (Col
25483 6c 53 65 71 20 2a 29 73 71 6c 69 74 65 48 61 73  lSeq *)sqliteHas
25484 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20  hData(p);.      
25485 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25486 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
25487 20 69 2b 2b 2c 20 31 29 3b 0a 20 20 20 20 20 20   i++, 1);.      
25488 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25489 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
2548a 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c 6c 2d   0, 2, 0, pColl-
2548b 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
2548c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2548d 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
2548e 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20 20 20 20  Row, 1, 2);.    
2548f 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
25490 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
25491 53 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 20 2a  SCHEMA_PRAGMAS *
25492 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
25493 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
25494 45 59 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  EY.  if( sqlite3
25495 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
25496 66 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69 73 74  foreign_key_list
25497 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20  ")==0 && zRight 
25498 29 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70 46 4b  ){.    FKey *pFK
25499 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ;.    Table *pTa
2549a 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  b;.    if( sqlit
2549b 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
2549c 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
2549d 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62  ma_out;.    pTab
2549e 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
2549f 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  ble(db, zRight, 
254a0 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54  zDb);.    if( pT
254a1 61 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20  ab ){.      v = 
254a2 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
254a3 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 46  Parse);.      pF
254a4 4b 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 3b  K = pTab->pFKey;
254a5 0a 20 20 20 20 20 20 69 66 28 20 70 46 4b 20 29  .      if( pFK )
254a6 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 20  {.        int i 
254a7 3d 20 30 3b 20 0a 20 20 20 20 20 20 20 20 73 71  = 0; .        sq
254a8 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
254a9 6f 6c 73 28 76 2c 20 38 29 3b 0a 20 20 20 20 20  ols(v, 8);.     
254aa 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
254ab 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = 8;.        sql
254ac 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
254ad 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
254ae 5f 4e 41 4d 45 2c 20 22 69 64 22 2c 20 53 51 4c  _NAME, "id", SQL
254af 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
254b0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
254b1 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
254b2 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
254b3 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  seq", SQLITE_STA
254b4 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
254b5 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
254b6 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d  ame(v, 2, COLNAM
254b7 45 5f 4e 41 4d 45 2c 20 22 74 61 62 6c 65 22 2c  E_NAME, "table",
254b8 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
254b9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
254ba 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
254bb 2c 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 3, COLNAME_NAM
254bc 45 2c 20 22 66 72 6f 6d 22 2c 20 53 51 4c 49 54  E, "from", SQLIT
254bd 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
254be 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
254bf 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43  tColName(v, 4, C
254c0 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74 6f  OLNAME_NAME, "to
254c1 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
254c2 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
254c3 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
254c4 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 5, COLNAME_N
254c5 41 4d 45 2c 20 22 6f 6e 5f 75 70 64 61 74 65 22  AME, "on_update"
254c6 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
254c7 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
254c8 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
254c9 76 2c 20 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 6, COLNAME_NA
254ca 4d 45 2c 20 22 6f 6e 5f 64 65 6c 65 74 65 22 2c  ME, "on_delete",
254cb 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
254cc 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
254cd 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
254ce 2c 20 37 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 7, COLNAME_NAM
254cf 45 2c 20 22 6d 61 74 63 68 22 2c 20 53 51 4c 49  E, "match", SQLI
254d0 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
254d1 20 20 20 20 77 68 69 6c 65 28 70 46 4b 29 7b 0a      while(pFK){.
254d2 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b            int j;
254d3 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a  .          for(j
254d4 3d 30 3b 20 6a 3c 70 46 4b 2d 3e 6e 43 6f 6c 3b  =0; j<pFK->nCol;
254d5 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
254d6 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20     char *zCol = 
254d7 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f  pFK->aCol[j].zCo
254d8 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  l;.            c
254d9 68 61 72 20 2a 7a 4f 6e 55 70 64 61 74 65 20 3d  har *zOnUpdate =
254da 20 28 63 68 61 72 20 2a 29 61 63 74 69 6f 6e 4e   (char *)actionN
254db 61 6d 65 28 70 46 4b 2d 3e 75 70 64 61 74 65 43  ame(pFK->updateC
254dc 6f 6e 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20  onf);.          
254dd 20 20 63 68 61 72 20 2a 7a 4f 6e 44 65 6c 65 74    char *zOnDelet
254de 65 20 3d 20 28 63 68 61 72 20 2a 29 61 63 74 69  e = (char *)acti
254df 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e 64 65 6c 65  onName(pFK->dele
254e0 74 65 43 6f 6e 66 29 3b 0a 20 20 20 20 20 20 20  teConf);.       
254e1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
254e2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
254e3 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20  eger, i, 1);.   
254e4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
254e5 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
254e6 5f 49 6e 74 65 67 65 72 2c 20 6a 2c 20 32 29 3b  _Integer, j, 2);
254e7 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
254e8 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
254e9 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
254ea 20 33 2c 20 30 2c 20 70 46 4b 2d 3e 7a 54 6f 2c   3, 0, pFK->zTo,
254eb 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
254ec 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
254ed 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
254ee 2c 20 30 2c 20 34 2c 20 30 2c 0a 20 20 20 20 20  , 0, 4, 0,.     
254ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
254f0 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61           pTab->a
254f1 43 6f 6c 5b 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d  Col[pFK->aCol[j]
254f2 2e 69 46 72 6f 6d 5d 2e 7a 4e 61 6d 65 2c 20 30  .iFrom].zName, 0
254f3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
254f4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
254f5 28 76 2c 20 7a 43 6f 6c 20 3f 20 4f 50 5f 53 74  (v, zCol ? OP_St
254f6 72 69 6e 67 38 20 3a 20 4f 50 5f 4e 75 6c 6c 2c  ring8 : OP_Null,
254f7 20 30 2c 20 35 2c 20 30 2c 20 7a 43 6f 6c 2c 20   0, 5, 0, zCol, 
254f8 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
254f9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
254fa 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
254fb 20 30 2c 20 36 2c 20 30 2c 20 7a 4f 6e 55 70 64   0, 6, 0, zOnUpd
254fc 61 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ate, 0);.       
254fd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
254fe 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
254ff 69 6e 67 38 2c 20 30 2c 20 37 2c 20 30 2c 20 7a  ing8, 0, 7, 0, z
25500 4f 6e 44 65 6c 65 74 65 2c 20 30 29 3b 0a 20 20  OnDelete, 0);.  
25501 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25502 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
25503 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 38 2c  P_String8, 0, 8,
25504 20 30 2c 20 22 4e 4f 4e 45 22 2c 20 30 29 3b 0a   0, "NONE", 0);.
25505 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25506 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
25507 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
25508 2c 20 38 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 8);.          
25509 7d 0a 20 20 20 20 20 20 20 20 20 20 2b 2b 69 3b  }.          ++i;
2550a 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b 20 3d  .          pFK =
2550b 20 70 46 4b 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b   pFK->pNextFrom;
2550c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2550d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
2550e 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
2550f 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
25510 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a  FOREIGN_KEY) */.
25511 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
25512 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
25513 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 70 61 72  ICmp(zLeft, "par
25514 73 65 72 5f 74 72 61 63 65 22 29 3d 3d 30 20 29  ser_trace")==0 )
25515 7b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74  {.    if( zRight
25516 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 67 65   ){.      if( ge
25517 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29  tBoolean(zRight)
25518 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
25519 74 65 33 50 61 72 73 65 72 54 72 61 63 65 28 73  te3ParserTrace(s
2551a 74 64 65 72 72 2c 20 22 70 61 72 73 65 72 3a 20  tderr, "parser: 
2551b 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ");.      }else{
2551c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2551d 50 61 72 73 65 72 54 72 61 63 65 28 30 2c 20 30  ParserTrace(0, 0
2551e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2551f 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a  .  }else.#endif.
25520 0a 20 20 2f 2a 20 52 65 69 6e 73 74 61 6c 6c 20  .  /* Reinstall 
25521 74 68 65 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f  the LIKE and GLO
25522 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  B functions.  Th
25523 65 20 76 61 72 69 61 6e 74 20 6f 66 20 4c 49 4b  e variant of LIK
25524 45 0a 20 20 2a 2a 20 75 73 65 64 20 77 69 6c 6c  E.  ** used will
25525 20 62 65 20 63 61 73 65 20 73 65 6e 73 69 74 69   be case sensiti
25526 76 65 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e 64  ve or not depend
25527 69 6e 67 20 6f 6e 20 74 68 65 20 52 48 53 2e 0a  ing on the RHS..
25528 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
25529 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
2552a 20 22 63 61 73 65 5f 73 65 6e 73 69 74 69 76 65   "case_sensitive
2552b 5f 6c 69 6b 65 22 29 3d 3d 30 20 29 7b 0a 20 20  _like")==0 ){.  
2552c 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a    if( zRight ){.
2552d 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 67        sqlite3Reg
2552e 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f  isterLikeFunctio
2552f 6e 73 28 64 62 2c 20 67 65 74 42 6f 6f 6c 65 61  ns(db, getBoolea
25530 6e 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 20 20  n(zRight));.    
25531 7d 0a 20 20 7d 65 6c 73 65 0a 0a 23 69 66 6e 64  }.  }else..#ifnd
25532 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52  ef SQLITE_INTEGR
25533 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f  ITY_CHECK_ERROR_
25534 4d 41 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  MAX.# define SQL
25535 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  ITE_INTEGRITY_CH
25536 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 20 31 30  ECK_ERROR_MAX 10
25537 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  0.#endif..#ifnde
25538 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
25539 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 20 20  TEGRITY_CHECK.  
2553a 2f 2a 20 50 72 61 67 6d 61 20 22 71 75 69 63 6b  /* Pragma "quick
2553b 5f 63 68 65 63 6b 22 20 69 73 20 61 6e 20 65 78  _check" is an ex
2553c 70 65 72 69 6d 65 6e 74 61 6c 20 72 65 64 75 63  perimental reduc
2553d 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 0a 20  ed version of . 
2553e 20 2a 2a 20 69 6e 74 65 67 72 69 74 79 5f 63 68   ** integrity_ch
2553f 65 63 6b 20 64 65 73 69 67 6e 65 64 20 74 6f 20  eck designed to 
25540 64 65 74 65 63 74 20 6d 6f 73 74 20 64 61 74 61  detect most data
25541 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 0a  base corruption.
25542 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 6d 6f 73    ** without mos
25543 74 20 6f 66 20 74 68 65 20 6f 76 65 72 68 65 61  t of the overhea
25544 64 20 6f 66 20 61 20 66 75 6c 6c 20 69 6e 74 65  d of a full inte
25545 67 72 69 74 79 2d 63 68 65 63 6b 2e 0a 20 20 2a  grity-check..  *
25546 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
25547 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 69  trICmp(zLeft, "i
25548 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 22 29  ntegrity_check")
25549 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65  ==0.   || sqlite
2554a 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
2554b 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 29 3d 3d  "quick_check")==
2554c 30 20 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20  0 .  ){.    int 
2554d 69 2c 20 6a 2c 20 61 64 64 72 2c 20 6d 78 45 72  i, j, addr, mxEr
2554e 72 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20  r;..    /* Code 
2554f 74 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20  that appears at 
25550 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
25551 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 20  ntegrity check. 
25552 20 49 66 20 6e 6f 20 65 72 72 6f 72 0a 20 20 20   If no error.   
25553 20 2a 2a 20 6d 65 73 73 61 67 65 73 20 68 61 76   ** messages hav
25554 65 20 62 65 65 6e 20 67 65 6e 65 72 61 74 65 64  e been generated
25555 2c 20 6f 75 74 70 75 74 20 4f 4b 2e 20 20 4f 74  , output OK.  Ot
25556 68 65 72 77 69 73 65 20 6f 75 74 70 75 74 20 74  herwise output t
25557 68 65 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20  he.    ** error 
25558 6d 65 73 73 61 67 65 0a 20 20 20 20 2a 2f 0a 20  message.    */. 
25559 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
2555a 56 64 62 65 4f 70 4c 69 73 74 20 65 6e 64 43 6f  VdbeOpList endCo
2555b 64 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b  de[] = {.      {
2555c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20   OP_AddImm,     
2555d 20 31 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d   1, 0,        0}
2555e 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20  ,    /* 0 */.   
2555f 20 20 20 7b 20 4f 50 5f 49 66 4e 65 67 2c 20 20     { OP_IfNeg,  
25560 20 20 20 20 20 31 2c 20 30 2c 20 20 20 20 20 20       1, 0,      
25561 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f    0},    /* 1 */
25562 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69  .      { OP_Stri
25563 6e 67 38 2c 20 20 20 20 20 30 2c 20 33 2c 20 20  ng8,     0, 3,  
25564 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20        0},    /* 
25565 32 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f  2 */.      { OP_
25566 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 33 2c 20  ResultRow,   3, 
25567 31 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20  1,        0},.  
25568 20 20 7d 3b 0a 0a 20 20 20 20 69 6e 74 20 69 73    };..    int is
25569 51 75 69 63 6b 20 3d 20 28 7a 4c 65 66 74 5b 30  Quick = (zLeft[0
2556a 5d 3d 3d 27 71 27 29 3b 0a 0a 20 20 20 20 2f 2a  ]=='q');..    /*
2556b 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
2556c 56 44 42 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  VDBE program */.
2556d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
2556e 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
2556f 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
25570 6f 75 74 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  out;.    pParse-
25571 3e 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20 20 73  >nMem = 6;.    s
25572 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
25573 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
25574 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
25575 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
25576 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 6e 74 65 67  AME_NAME, "integ
25577 72 69 74 79 5f 63 68 65 63 6b 22 2c 20 53 51 4c  rity_check", SQL
25578 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 0a 20 20  ITE_STATIC);..  
25579 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d 61 78    /* Set the max
2557a 69 6d 75 6d 20 65 72 72 6f 72 20 63 6f 75 6e 74  imum error count
2557b 20 2a 2f 0a 20 20 20 20 6d 78 45 72 72 20 3d 20   */.    mxErr = 
2557c 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59  SQLITE_INTEGRITY
2557d 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58  _CHECK_ERROR_MAX
2557e 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74  ;.    if( zRight
2557f 20 29 7b 0a 20 20 20 20 20 20 6d 78 45 72 72 20   ){.      mxErr 
25580 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a  = atoi(zRight);.
25581 20 20 20 20 20 20 69 66 28 20 6d 78 45 72 72 3c        if( mxErr<
25582 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 78  =0 ){.        mx
25583 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  Err = SQLITE_INT
25584 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52  EGRITY_CHECK_ERR
25585 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 20 20 7d 0a  OR_MAX;.      }.
25586 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
25587 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25588 50 5f 49 6e 74 65 67 65 72 2c 20 6d 78 45 72 72  P_Integer, mxErr
25589 2c 20 31 29 3b 20 20 2f 2a 20 72 65 67 5b 31 5d  , 1);  /* reg[1]
2558a 20 68 6f 6c 64 73 20 65 72 72 6f 72 73 20 6c 65   holds errors le
2558b 66 74 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 44 6f  ft */..    /* Do
2558c 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68   an integrity ch
2558d 65 63 6b 20 6f 6e 20 65 61 63 68 20 64 61 74 61  eck on each data
2558e 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20  base file */.   
2558f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
25590 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
25591 20 48 61 73 68 45 6c 65 6d 20 2a 78 3b 0a 20 20   HashElem *x;.  
25592 20 20 20 20 48 61 73 68 20 2a 70 54 62 6c 73 3b      Hash *pTbls;
25593 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d  .      int cnt =
25594 20 30 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 4f   0;..      if( O
25595 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 3d  MIT_TEMPDB && i=
25596 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  =1 ) continue;..
25597 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64        sqlite3Cod
25598 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
25599 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 20 20  arse, i);.      
2559a 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
2559b 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
2559c 66 50 6f 73 2c 20 31 29 3b 20 2f 2a 20 48 61 6c  fPos, 1); /* Hal
2559d 74 20 69 66 20 6f 75 74 20 6f 66 20 65 72 72 6f  t if out of erro
2559e 72 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  rs */.      sqli
2559f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
255a0 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b   OP_Halt, 0, 0);
255a1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
255a2 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
255a3 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 44  dr);..      /* D
255a4 6f 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63  o an integrity c
255a5 68 65 63 6b 20 6f 66 20 74 68 65 20 42 2d 54 72  heck of the B-Tr
255a6 65 65 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  ee.      **.    
255a7 20 20 2a 2a 20 42 65 67 69 6e 20 62 79 20 66 69    ** Begin by fi
255a8 6c 6c 69 6e 67 20 72 65 67 69 73 74 65 72 73 20  lling registers 
255a9 32 2c 20 33 2c 20 2e 2e 2e 20 77 69 74 68 20 74  2, 3, ... with t
255aa 68 65 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75  he root pages nu
255ab 6d 62 65 72 73 0a 20 20 20 20 20 20 2a 2a 20 66  mbers.      ** f
255ac 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e  or all tables an
255ad 64 20 69 6e 64 69 63 65 73 20 69 6e 20 74 68 65  d indices in the
255ae 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20   database..     
255af 20 2a 2f 0a 20 20 20 20 20 20 70 54 62 6c 73 20   */.      pTbls 
255b0 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53  = &db->aDb[i].pS
255b1 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a  chema->tblHash;.
255b2 20 20 20 20 20 20 66 6f 72 28 78 3d 73 71 6c 69        for(x=sqli
255b3 74 65 48 61 73 68 46 69 72 73 74 28 70 54 62 6c  teHashFirst(pTbl
255b4 73 29 3b 20 78 3b 20 78 3d 73 71 6c 69 74 65 48  s); x; x=sqliteH
255b5 61 73 68 4e 65 78 74 28 78 29 29 7b 0a 20 20 20  ashNext(x)){.   
255b6 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
255b7 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74   = sqliteHashDat
255b8 61 28 78 29 3b 0a 20 20 20 20 20 20 20 20 49 6e  a(x);.        In
255b9 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 20  dex *pIdx;.     
255ba 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
255bb 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
255bc 65 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  er, pTab->tnum, 
255bd 32 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  2+cnt);.        
255be 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 66  cnt++;.        f
255bf 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
255c0 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
255c1 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
255c2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
255c3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
255c4 5f 49 6e 74 65 67 65 72 2c 20 70 49 64 78 2d 3e  _Integer, pIdx->
255c5 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20 20  tnum, 2+cnt);.  
255c6 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20          cnt++;. 
255c7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
255c8 0a 20 20 20 20 20 20 69 66 28 20 63 6e 74 3d 3d  .      if( cnt==
255c9 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20  0 ) continue;.. 
255ca 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72       /* Make sur
255cb 65 20 73 75 66 66 69 63 69 65 6e 74 20 6e 75 6d  e sufficient num
255cc 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73  ber of registers
255cd 20 68 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63   have been alloc
255ce 61 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66  ated */.      if
255cf 28 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3c  ( pParse->nMem <
255d0 20 63 6e 74 2b 34 20 29 7b 0a 20 20 20 20 20 20   cnt+4 ){.      
255d1 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
255d2 20 63 6e 74 2b 34 3b 0a 20 20 20 20 20 20 7d 0a   cnt+4;.      }.
255d3 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20 74 68 65  .      /* Do the
255d4 20 62 2d 74 72 65 65 20 69 6e 74 65 67 72 69 74   b-tree integrit
255d5 79 20 63 68 65 63 6b 73 20 2a 2f 0a 20 20 20 20  y checks */.    
255d6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
255d7 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 74 65 67 72  Op3(v, OP_Integr
255d8 69 74 79 43 6b 2c 20 32 2c 20 63 6e 74 2c 20 31  ityCk, 2, cnt, 1
255d9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
255da 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
255db 28 75 38 29 69 29 3b 0a 20 20 20 20 20 20 61 64  (u8)i);.      ad
255dc 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
255dd 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e  AddOp1(v, OP_IsN
255de 75 6c 6c 2c 20 32 29 3b 0a 20 20 20 20 20 20 73  ull, 2);.      s
255df 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
255e0 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
255e1 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20 20 20  0, 3, 0,.       
255e2 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66    sqlite3MPrintf
255e3 28 64 62 2c 20 22 2a 2a 2a 20 69 6e 20 64 61 74  (db, "*** in dat
255e4 61 62 61 73 65 20 25 73 20 2a 2a 2a 5c 6e 22 2c  abase %s ***\n",
255e5 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d   db->aDb[i].zNam
255e6 65 29 2c 0a 20 20 20 20 20 20 20 20 20 50 34 5f  e),.         P4_
255e7 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
255e8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
255e9 33 28 76 2c 20 4f 50 5f 4d 6f 76 65 2c 20 32 2c  3(v, OP_Move, 2,
255ea 20 34 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71   4, 1);.      sq
255eb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
255ec 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 34 2c  v, OP_Concat, 4,
255ed 20 33 2c 20 32 29 3b 0a 20 20 20 20 20 20 73 71   3, 2);.      sq
255ee 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
255ef 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
255f0 20 32 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71   2, 1);.      sq
255f1 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
255f2 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20  e(v, addr);..   
255f3 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
255f4 61 6c 6c 20 74 68 65 20 69 6e 64 69 63 65 73 20  all the indices 
255f5 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20  are constructed 
255f6 63 6f 72 72 65 63 74 6c 79 2e 0a 20 20 20 20 20  correctly..     
255f7 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 78 3d   */.      for(x=
255f8 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
255f9 70 54 62 6c 73 29 3b 20 78 20 26 26 20 21 69 73  pTbls); x && !is
255fa 51 75 69 63 6b 3b 20 78 3d 73 71 6c 69 74 65 48  Quick; x=sqliteH
255fb 61 73 68 4e 65 78 74 28 78 29 29 7b 0a 20 20 20  ashNext(x)){.   
255fc 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
255fd 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74   = sqliteHashDat
255fe 61 28 78 29 3b 0a 20 20 20 20 20 20 20 20 49 6e  a(x);.        In
255ff 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 20  dex *pIdx;.     
25600 20 20 20 69 6e 74 20 6c 6f 6f 70 54 6f 70 3b 0a     int loopTop;.
25601 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 61  .        if( pTa
25602 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 20 63  b->pIndex==0 ) c
25603 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
25604 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
25605 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
25606 49 66 50 6f 73 2c 20 31 29 3b 20 20 2f 2a 20 53  IfPos, 1);  /* S
25607 74 6f 70 20 69 66 20 6f 75 74 20 6f 66 20 65 72  top if out of er
25608 72 6f 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  rors */.        
25609 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2560a 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c  2(v, OP_Halt, 0,
2560b 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
2560c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2560d 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20  (v, addr);.     
2560e 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61     sqlite3OpenTa
2560f 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 50  bleAndIndices(pP
25610 61 72 73 65 2c 20 70 54 61 62 2c 20 31 2c 20 4f  arse, pTab, 1, O
25611 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20  P_OpenRead);.   
25612 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25613 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
25614 65 67 65 72 2c 20 30 2c 20 32 29 3b 20 20 2f 2a  eger, 0, 2);  /*
25615 20 72 65 67 28 32 29 20 77 69 6c 6c 20 63 6f 75   reg(2) will cou
25616 6e 74 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20  nt entries */.  
25617 20 20 20 20 20 20 6c 6f 6f 70 54 6f 70 20 3d 20        loopTop = 
25618 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25619 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
2561a 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  1, 0);.        s
2561b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2561c 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 32  (v, OP_AddImm, 2
2561d 2c 20 31 29 3b 20 20 20 2f 2a 20 69 6e 63 72 65  , 1);   /* incre
2561e 6d 65 6e 74 20 65 6e 74 72 79 20 63 6f 75 6e 74  ment entry count
2561f 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28   */.        for(
25620 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  j=0, pIdx=pTab->
25621 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
25622 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
25623 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
25624 69 6e 74 20 6a 6d 70 32 3b 0a 20 20 20 20 20 20  int jmp2;.      
25625 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
25626 20 56 64 62 65 4f 70 4c 69 73 74 20 69 64 78 45   VdbeOpList idxE
25627 72 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  rr[] = {.       
25628 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d       { OP_AddImm
25629 2c 20 20 20 20 20 20 31 2c 20 2d 31 2c 20 20 30  ,      1, -1,  0
2562a 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  },.            {
2562b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
2562c 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20 20 20 20   0,  3,  0},    
2562d 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 1 */.        
2562e 20 20 20 20 7b 20 4f 50 5f 52 6f 77 69 64 2c 20      { OP_Rowid, 
2562f 20 20 20 20 20 20 31 2c 20 20 34 2c 20 20 30 7d        1,  4,  0}
25630 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
25631 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20  OP_String8,     
25632 30 2c 20 20 35 2c 20 20 30 7d 2c 20 20 20 20 2f  0,  5,  0},    /
25633 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 3 */.         
25634 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
25635 20 20 20 20 20 30 2c 20 20 36 2c 20 20 30 7d 2c       0,  6,  0},
25636 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20      /* 4 */.    
25637 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e          { OP_Con
25638 63 61 74 2c 20 20 20 20 20 20 34 2c 20 20 33 2c  cat,      4,  3,
25639 20 20 33 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    3},.          
2563a 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20    { OP_Concat,  
2563b 20 20 20 20 35 2c 20 20 33 2c 20 20 33 7d 2c 0a      5,  3,  3},.
2563c 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
2563d 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 36 2c  _Concat,      6,
2563e 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20    3,  3},.      
2563f 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c        { OP_Resul
25640 74 52 6f 77 2c 20 20 20 33 2c 20 20 31 2c 20 20  tRow,   3,  1,  
25641 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0},.            
25642 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20 20  { OP_IfPos,     
25643 20 20 31 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20    1,  0,  0},   
25644 20 2f 2a 20 39 20 2a 2f 0a 20 20 20 20 20 20 20   /* 9 */.       
25645 20 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74 2c 20       { OP_Halt, 
25646 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30         0,  0,  0
25647 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a  },.          };.
25648 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25649 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65  3GenerateIndexKe
2564a 79 28 70 50 61 72 73 65 2c 20 70 49 64 78 2c 20  y(pParse, pIdx, 
2564b 31 2c 20 33 2c 20 31 29 3b 0a 20 20 20 20 20 20  1, 3, 1);.      
2564c 20 20 20 20 6a 6d 70 32 20 3d 20 73 71 6c 69 74      jmp2 = sqlit
2564d 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2564e 4f 50 5f 46 6f 75 6e 64 2c 20 6a 2b 32 2c 20 30  OP_Found, j+2, 0
2564f 2c 20 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 3);.          
25650 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
25651 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41  beAddOpList(v, A
25652 72 72 61 79 53 69 7a 65 28 69 64 78 45 72 72 29  rraySize(idxErr)
25653 2c 20 69 64 78 45 72 72 29 3b 0a 20 20 20 20 20  , idxErr);.     
25654 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25655 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72  ChangeP4(v, addr
25656 2b 31 2c 20 22 72 6f 77 69 64 20 22 2c 20 50 34  +1, "rowid ", P4
25657 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
25658 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
25659 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b  hangeP4(v, addr+
2565a 33 2c 20 22 20 6d 69 73 73 69 6e 67 20 66 72 6f  3, " missing fro
2565b 6d 20 69 6e 64 65 78 20 22 2c 20 50 34 5f 53 54  m index ", P4_ST
2565c 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20  ATIC);.         
2565d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2565e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 34 2c 20  geP4(v, addr+4, 
2565f 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f  pIdx->zName, P4_
25660 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
25661 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
25662 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 2b 39  mpHere(v, addr+9
25663 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
25664 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
25665 28 76 2c 20 6a 6d 70 32 29 3b 0a 20 20 20 20 20  (v, jmp2);.     
25666 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
25667 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25668 2c 20 4f 50 5f 4e 65 78 74 2c 20 31 2c 20 6c 6f  , OP_Next, 1, lo
25669 6f 70 54 6f 70 2b 31 29 3b 0a 20 20 20 20 20 20  opTop+1);.      
2566a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2566b 70 48 65 72 65 28 76 2c 20 6c 6f 6f 70 54 6f 70  pHere(v, loopTop
2566c 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  );.        for(j
2566d 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =0, pIdx=pTab->p
2566e 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
2566f 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a  x=pIdx->pNext, j
25670 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ++){.          s
25671 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
25672 4f 70 4c 69 73 74 20 63 6e 74 49 64 78 5b 5d 20  OpList cntIdx[] 
25673 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  = {.            
25674 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20   { OP_Integer,  
25675 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 0a      0,  3,  0},.
25676 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
25677 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 20 20  P_Rewind,       
25678 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 2f 2a 20  0,  0,  0},  /* 
25679 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  1 */.           
2567a 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20    { OP_AddImm,  
2567b 20 20 20 20 20 33 2c 20 20 31 2c 20 20 30 7d 2c       3,  1,  0},
2567c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  .             { 
2567d 4f 50 5f 4e 65 78 74 2c 20 20 20 20 20 20 20 20  OP_Next,        
2567e 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 2f 2a   0,  0,  0},  /*
2567f 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   3 */.          
25680 20 20 20 7b 20 4f 50 5f 45 71 2c 20 20 20 20 20     { OP_Eq,     
25681 20 20 20 20 20 20 32 2c 20 20 30 2c 20 20 33 7d        2,  0,  3}
25682 2c 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 20  ,  /* 4 */.     
25683 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64          { OP_Add
25684 49 6d 6d 2c 20 20 20 20 20 20 20 31 2c 20 2d 31  Imm,       1, -1
25685 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  0},.         
25686 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38      { OP_String8
25687 2c 20 20 20 20 20 20 30 2c 20 20 32 2c 20 20 30  ,      0,  2,  0
25688 7d 2c 20 20 2f 2a 20 36 20 2a 2f 0a 20 20 20 20  },  /* 6 */.    
25689 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74           { OP_St
2568a 72 69 6e 67 38 2c 20 20 20 20 20 20 30 2c 20 20  ring8,      0,  
2568b 33 2c 20 20 30 7d 2c 20 20 2f 2a 20 37 20 2a 2f  3,  0},  /* 7 */
2568c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  .             { 
2568d 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20  OP_Concat,      
2568e 20 33 2c 20 20 32 2c 20 20 32 7d 2c 0a 20 20 20   3,  2,  2},.   
2568f 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52            { OP_R
25690 65 73 75 6c 74 52 6f 77 2c 20 20 20 20 32 2c 20  esultRow,    2, 
25691 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20   1,  0},.       
25692 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20     };.          
25693 69 66 28 20 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d  if( pIdx->tnum==
25694 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
25695 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73          addr = s
25696 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
25697 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29  (v, OP_IfPos, 1)
25698 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
25699 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2569a 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b   OP_Halt, 0, 0);
2569b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2569c 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2569d 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 20  , addr);.       
2569e 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
2569f 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
256a0 2c 20 41 72 72 61 79 53 69 7a 65 28 63 6e 74 49  , ArraySize(cntI
256a1 64 78 29 2c 20 63 6e 74 49 64 78 29 3b 0a 20 20  dx), cntIdx);.  
256a2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
256a3 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
256a4 64 64 72 2b 31 2c 20 6a 2b 32 29 3b 0a 20 20 20  ddr+1, j+2);.   
256a5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
256a6 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64  beChangeP2(v, ad
256a7 64 72 2b 31 2c 20 61 64 64 72 2b 34 29 3b 0a 20  dr+1, addr+4);. 
256a8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
256a9 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
256aa 61 64 64 72 2b 33 2c 20 6a 2b 32 29 3b 0a 20 20  addr+3, j+2);.  
256ab 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
256ac 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61  dbeChangeP2(v, a
256ad 64 64 72 2b 33 2c 20 61 64 64 72 2b 32 29 3b 0a  ddr+3, addr+2);.
256ae 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
256af 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
256b0 20 61 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20   addr+4);.      
256b1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
256b2 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b  hangeP4(v, addr+
256b3 36 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  6, .            
256b4 20 20 20 20 20 20 20 20 20 22 77 72 6f 6e 67 20           "wrong 
256b5 23 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  # of entries in 
256b6 69 6e 64 65 78 20 22 2c 20 50 34 5f 53 54 41 54  index ", P4_STAT
256b7 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  IC);.          s
256b8 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
256b9 50 34 28 76 2c 20 61 64 64 72 2b 37 2c 20 70 49  P4(v, addr+7, pI
256ba 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54  dx->zName, P4_ST
256bb 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 7d  ATIC);.        }
256bc 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a  .      } .    }.
256bd 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
256be 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
256bf 76 2c 20 41 72 72 61 79 53 69 7a 65 28 65 6e 64  v, ArraySize(end
256c0 43 6f 64 65 29 2c 20 65 6e 64 43 6f 64 65 29 3b  Code), endCode);
256c1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
256c2 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72  ChangeP2(v, addr
256c3 2c 20 2d 6d 78 45 72 72 29 3b 0a 20 20 20 20 73  , -mxErr);.    s
256c4 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
256c5 72 65 28 76 2c 20 61 64 64 72 2b 31 29 3b 0a 20  re(v, addr+1);. 
256c6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
256c7 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 32  angeP4(v, addr+2
256c8 2c 20 22 6f 6b 22 2c 20 50 34 5f 53 54 41 54 49  , "ok", P4_STATI
256c9 43 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  C);.  }else.#end
256ca 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
256cb 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
256cc 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  K */..#ifndef SQ
256cd 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
256ce 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47    /*.  **   PRAG
256cf 4d 41 20 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a  MA encoding.  **
256d0 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f 64 69     PRAGMA encodi
256d1 6e 67 20 3d 20 22 75 74 66 2d 38 22 7c 22 75 74  ng = "utf-8"|"ut
256d2 66 2d 31 36 22 7c 22 75 74 66 2d 31 36 6c 65 22  f-16"|"utf-16le"
256d3 7c 22 75 74 66 2d 31 36 62 65 22 0a 20 20 2a 2a  |"utf-16be".  **
256d4 0a 20 20 2a 2a 20 49 6e 20 69 74 73 20 66 69 72  .  ** In its fir
256d5 73 74 20 66 6f 72 6d 2c 20 74 68 69 73 20 70 72  st form, this pr
256d6 61 67 6d 61 20 72 65 74 75 72 6e 73 20 74 68 65  agma returns the
256d7 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
256d8 20 6d 61 69 6e 0a 20 20 2a 2a 20 64 61 74 61 62   main.  ** datab
256d9 61 73 65 2e 20 49 66 20 74 68 65 20 64 61 74 61  ase. If the data
256da 62 61 73 65 20 69 73 20 6e 6f 74 20 69 6e 69 74  base is not init
256db 69 61 6c 69 7a 65 64 2c 20 69 74 20 69 73 20 69  ialized, it is i
256dc 6e 69 74 69 61 6c 69 7a 65 64 20 6e 6f 77 2e 0a  nitialized now..
256dd 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65    **.  ** The se
256de 63 6f 6e 64 20 66 6f 72 6d 20 6f 66 20 74 68 69  cond form of thi
256df 73 20 70 72 61 67 6d 61 20 69 73 20 61 20 6e 6f  s pragma is a no
256e0 2d 6f 70 20 69 66 20 74 68 65 20 6d 61 69 6e 20  -op if the main 
256e1 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20  database file.  
256e2 2a 2a 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61  ** has not alrea
256e3 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  dy been initiali
256e4 7a 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  zed. In this cas
256e5 65 20 69 74 20 73 65 74 73 20 74 68 65 20 64 65  e it sets the de
256e6 66 61 75 6c 74 0a 20 20 2a 2a 20 65 6e 63 6f 64  fault.  ** encod
256e7 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ing that will be
256e8 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61   used for the ma
256e9 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
256ea 20 69 66 20 61 20 6e 65 77 20 66 69 6c 65 0a 20   if a new file. 
256eb 20 2a 2a 20 69 73 20 63 72 65 61 74 65 64 2e 20   ** is created. 
256ec 49 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d  If an existing m
256ed 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
256ee 65 20 69 73 20 6f 70 65 6e 65 64 2c 20 74 68 65  e is opened, the
256ef 6e 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75  n the.  ** defau
256f0 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  lt text encoding
256f1 20 66 6f 72 20 74 68 65 20 65 78 69 73 74 69 6e   for the existin
256f2 67 20 64 61 74 61 62 61 73 65 20 69 73 20 75 73  g database is us
256f3 65 64 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 49  ed..  ** .  ** I
256f4 6e 20 61 6c 6c 20 63 61 73 65 73 20 6e 65 77 20  n all cases new 
256f5 64 61 74 61 62 61 73 65 73 20 63 72 65 61 74 65  databases create
256f6 64 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41  d using the ATTA
256f7 43 48 20 63 6f 6d 6d 61 6e 64 20 61 72 65 0a 20  CH command are. 
256f8 20 2a 2a 20 63 72 65 61 74 65 64 20 74 6f 20 75   ** created to u
256f9 73 65 20 74 68 65 20 73 61 6d 65 20 64 65 66 61  se the same defa
256fa 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  ult text encodin
256fb 67 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64 61  g as the main da
256fc 74 61 62 61 73 65 2e 20 49 66 0a 20 20 2a 2a 20  tabase. If.  ** 
256fd 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
256fe 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69  e has not been i
256ff 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e 64 2f 6f  nitialized and/o
25700 72 20 63 72 65 61 74 65 64 20 77 68 65 6e 20 41  r created when A
25701 54 54 41 43 48 0a 20 20 2a 2a 20 69 73 20 65 78  TTACH.  ** is ex
25702 65 63 75 74 65 64 2c 20 74 68 69 73 20 69 73 20  ecuted, this is 
25703 64 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20  done before the 
25704 41 54 54 41 43 48 20 6f 70 65 72 61 74 69 6f 6e  ATTACH operation
25705 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 74  ..  **.  ** In t
25706 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 74  he second form t
25707 68 69 73 20 70 72 61 67 6d 61 20 73 65 74 73 20  his pragma sets 
25708 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  the text encodin
25709 67 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 0a  g to be used in.
2570a 20 20 2a 2a 20 6e 65 77 20 64 61 74 61 62 61 73    ** new databas
2570b 65 20 66 69 6c 65 73 20 63 72 65 61 74 65 64 20  e files created 
2570c 75 73 69 6e 67 20 74 68 69 73 20 64 61 74 61 62  using this datab
2570d 61 73 65 20 68 61 6e 64 6c 65 2e 20 49 74 20 69  ase handle. It i
2570e 73 20 6f 6e 6c 79 0a 20 20 2a 2a 20 75 73 65 66  s only.  ** usef
2570f 75 6c 20 69 66 20 69 6e 76 6f 6b 65 64 20 69 6d  ul if invoked im
25710 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20  mediately after 
25711 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
25712 65 20 69 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  e i.  */.  if( s
25713 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
25714 65 66 74 2c 20 22 65 6e 63 6f 64 69 6e 67 22 29  eft, "encoding")
25715 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61 74 69  ==0 ){.    stati
25716 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 45  c const struct E
25717 6e 63 4e 61 6d 65 20 7b 0a 20 20 20 20 20 20 63  ncName {.      c
25718 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20  har *zName;.    
25719 20 20 75 38 20 65 6e 63 3b 0a 20 20 20 20 7d 20    u8 enc;.    } 
2571a 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 0a 20  encnames[] = {. 
2571b 20 20 20 20 20 7b 20 22 55 54 46 38 22 2c 20 20       { "UTF8",  
2571c 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20     SQLITE_UTF8  
2571d 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b        },.      {
2571e 20 22 55 54 46 2d 38 22 2c 20 20 20 20 53 51 4c   "UTF-8",    SQL
2571f 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20  ITE_UTF8        
25720 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 65  },  /* Must be e
25721 6c 65 6d 65 6e 74 20 5b 31 5d 20 2a 2f 0a 20 20  lement [1] */.  
25722 20 20 20 20 7b 20 22 55 54 46 2d 31 36 6c 65 22      { "UTF-16le"
25723 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
25724 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74       },  /* Must
25725 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b 32 5d 20   be element [2] 
25726 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d  */.      { "UTF-
25727 31 36 62 65 22 2c 20 53 51 4c 49 54 45 5f 55 54  16be", SQLITE_UT
25728 46 31 36 42 45 20 20 20 20 20 7d 2c 20 20 2f 2a  F16BE     },  /*
25729 20 4d 75 73 74 20 62 65 20 65 6c 65 6d 65 6e 74   Must be element
2572a 20 5b 33 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20   [3] */.      { 
2572b 22 55 54 46 31 36 6c 65 22 2c 20 20 53 51 4c 49  "UTF16le",  SQLI
2572c 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 7d  TE_UTF16LE     }
2572d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31 36  ,.      { "UTF16
2572e 62 65 22 2c 20 20 53 51 4c 49 54 45 5f 55 54 46  be",  SQLITE_UTF
2572f 31 36 42 45 20 20 20 20 20 7d 2c 0a 20 20 20 20  16BE     },.    
25730 20 20 7b 20 22 55 54 46 2d 31 36 22 2c 20 20 20    { "UTF-16",   
25731 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
25732 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49 54 45 5f     }, /* SQLITE_
25733 55 54 46 31 36 4e 41 54 49 56 45 20 2a 2f 0a 20  UTF16NATIVE */. 
25734 20 20 20 20 20 7b 20 22 55 54 46 31 36 22 2c 20       { "UTF16", 
25735 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
25736 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49        }, /* SQLI
25737 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 2a  TE_UTF16NATIVE *
25738 2f 0a 20 20 20 20 20 20 7b 20 30 2c 20 30 20 7d  /.      { 0, 0 }
25739 0a 20 20 20 20 7d 3b 0a 20 20 20 20 63 6f 6e 73  .    };.    cons
2573a 74 20 73 74 72 75 63 74 20 45 6e 63 4e 61 6d 65  t struct EncName
2573b 20 2a 70 45 6e 63 3b 0a 20 20 20 20 69 66 28 20   *pEnc;.    if( 
2573c 21 7a 52 69 67 68 74 20 29 7b 20 20 20 20 2f 2a  !zRight ){    /*
2573d 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e   "PRAGMA encodin
2573e 67 22 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  g" */.      if( 
2573f 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
25740 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
25741 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
25742 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25743 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
25744 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25745 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
25746 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
25747 22 65 6e 63 6f 64 69 6e 67 22 2c 20 53 51 4c 49  "encoding", SQLI
25748 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
25749 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2574a 4f 70 32 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op2(v, OP_String
2574b 38 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20 20 20  8, 0, 1);.      
2574c 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d 65 73  assert( encnames
2574d 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2e 65 6e  [SQLITE_UTF8].en
2574e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c==SQLITE_UTF8 )
2574f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
25750 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45 5f  encnames[SQLITE_
25751 55 54 46 31 36 4c 45 5d 2e 65 6e 63 3d 3d 53 51  UTF16LE].enc==SQ
25752 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 3b 0a  LITE_UTF16LE );.
25753 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 6e        assert( en
25754 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45 5f 55 54  cnames[SQLITE_UT
25755 46 31 36 42 45 5d 2e 65 6e 63 3d 3d 53 51 4c 49  F16BE].enc==SQLI
25756 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20  TE_UTF16BE );.  
25757 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
25758 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 65  hangeP4(v, -1, e
25759 6e 63 6e 61 6d 65 73 5b 45 4e 43 28 70 50 61 72  ncnames[ENC(pPar
2575a 73 65 2d 3e 64 62 29 5d 2e 7a 4e 61 6d 65 2c 20  se->db)].zName, 
2575b 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
2575c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2575d 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
2575e 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20  Row, 1, 1);.    
2575f 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20  }else{          
25760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25761 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e   "PRAGMA encodin
25762 67 20 3d 20 58 58 58 22 20 2a 2f 0a 20 20 20 20  g = XXX" */.    
25763 20 20 2f 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65    /* Only change
25764 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 73 71   the value of sq
25765 6c 69 74 65 2e 65 6e 63 20 69 66 20 74 68 65 20  lite.enc if the 
25766 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
25767 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20  is not.      ** 
25768 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49 66 20  initialized. If 
25769 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
2576a 65 20 65 78 69 73 74 73 2c 20 74 68 65 20 6e 65  e exists, the ne
2576b 77 20 73 71 6c 69 74 65 2e 65 6e 63 20 76 61 6c  w sqlite.enc val
2576c 75 65 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c  ue.      ** will
2576d 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20   be overwritten 
2576e 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20  when the schema 
2576f 69 73 20 6e 65 78 74 20 6c 6f 61 64 65 64 2e 20  is next loaded. 
25770 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 20  If it does not. 
25771 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20       ** already 
25772 65 78 69 73 74 73 2c 20 69 74 20 77 69 6c 6c 20  exists, it will 
25773 62 65 20 63 72 65 61 74 65 64 20 74 6f 20 75 73  be created to us
25774 65 20 74 68 65 20 6e 65 77 20 65 6e 63 6f 64 69  e the new encodi
25775 6e 67 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20  ng value..      
25776 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 0a 20 20  */.      if( .  
25777 20 20 20 20 20 20 21 28 44 62 48 61 73 50 72 6f        !(DbHasPro
25778 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44 42 5f  perty(db, 0, DB_
25779 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 29 20 7c  SchemaLoaded)) |
2577a 7c 20 0a 20 20 20 20 20 20 20 20 44 62 48 61 73  | .        DbHas
2577b 50 72 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20  Property(db, 0, 
2577c 44 42 5f 45 6d 70 74 79 29 20 0a 20 20 20 20 20  DB_Empty) .     
2577d 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   ){.        for(
2577e 70 45 6e 63 3d 26 65 6e 63 6e 61 6d 65 73 5b 30  pEnc=&encnames[0
2577f 5d 3b 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 3b 20  ]; pEnc->zName; 
25780 70 45 6e 63 2b 2b 29 7b 0a 20 20 20 20 20 20 20  pEnc++){.       
25781 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65     if( 0==sqlite
25782 33 53 74 72 49 43 6d 70 28 7a 52 69 67 68 74 2c  3StrICmp(zRight,
25783 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 29 20 29 7b   pEnc->zName) ){
25784 0a 20 20 20 20 20 20 20 20 20 20 20 20 45 4e 43  .            ENC
25785 28 70 50 61 72 73 65 2d 3e 64 62 29 20 3d 20 70  (pParse->db) = p
25786 45 6e 63 2d 3e 65 6e 63 20 3f 20 70 45 6e 63 2d  Enc->enc ? pEnc-
25787 3e 65 6e 63 20 3a 20 53 51 4c 49 54 45 5f 55 54  >enc : SQLITE_UT
25788 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20 20 20  F16NATIVE;.     
25789 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2578a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2578b 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2578c 21 70 45 6e 63 2d 3e 7a 4e 61 6d 65 20 29 7b 0a  !pEnc->zName ){.
2578d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2578e 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2578f 2c 20 22 75 6e 73 75 70 70 6f 72 74 65 64 20 65  , "unsupported e
25790 6e 63 6f 64 69 6e 67 3a 20 25 73 22 2c 20 7a 52  ncoding: %s", zR
25791 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  ight);.        }
25792 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
25793 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a   }else.#endif /*
25794 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
25795 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  16 */..#ifndef S
25796 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d  QLITE_OMIT_SCHEM
25797 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41  A_VERSION_PRAGMA
25798 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52  S.  /*.  **   PR
25799 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
2579a 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 0a 20  schema_version. 
2579b 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
2579c 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61 5f 76  tabase.]schema_v
2579d 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65  ersion = <intege
2579e 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 50  r>.  **.  **   P
2579f 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
257a0 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 0a 20 20  ]user_version.  
257a1 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74  **   PRAGMA [dat
257a2 61 62 61 73 65 2e 5d 75 73 65 72 5f 76 65 72 73  abase.]user_vers
257a3 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a  ion = <integer>.
257a4 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 70 72    **.  ** The pr
257a5 61 67 6d 61 27 73 20 73 63 68 65 6d 61 5f 76 65  agma's schema_ve
257a6 72 73 69 6f 6e 20 61 6e 64 20 75 73 65 72 5f 76  rsion and user_v
257a7 65 72 73 69 6f 6e 20 61 72 65 20 75 73 65 64 20  ersion are used 
257a8 74 6f 20 73 65 74 20 6f 72 20 67 65 74 0a 20 20  to set or get.  
257a9 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
257aa 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69  the schema-versi
257ab 6f 6e 20 61 6e 64 20 75 73 65 72 2d 76 65 72 73  on and user-vers
257ac 69 6f 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c  ion, respectivel
257ad 79 2e 20 42 6f 74 68 0a 20 20 2a 2a 20 74 68 65  y. Both.  ** the
257ae 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20   schema-version 
257af 61 6e 64 20 74 68 65 20 75 73 65 72 2d 76 65 72  and the user-ver
257b0 73 69 6f 6e 20 61 72 65 20 33 32 2d 62 69 74 20  sion are 32-bit 
257b1 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 0a  signed integers.
257b2 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74    ** stored in t
257b3 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61 64  he database head
257b4 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  er..  **.  ** Th
257b5 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20  e schema-cookie 
257b6 69 73 20 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20  is usually only 
257b7 6d 61 6e 69 70 75 6c 61 74 65 64 20 69 6e 74 65  manipulated inte
257b8 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65  rnally by SQLite
257b9 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 69 6e 63  . It.  ** is inc
257ba 72 65 6d 65 6e 74 65 64 20 62 79 20 53 51 4c 69  remented by SQLi
257bb 74 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 20  te whenever the 
257bc 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
257bd 69 73 20 6d 6f 64 69 66 69 65 64 20 28 62 79 0a  is modified (by.
257be 20 20 2a 2a 20 63 72 65 61 74 69 6e 67 20 6f 72    ** creating or
257bf 20 64 72 6f 70 70 69 6e 67 20 61 20 74 61 62 6c   dropping a tabl
257c0 65 20 6f 72 20 69 6e 64 65 78 29 2e 20 54 68 65  e or index). The
257c1 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20   schema version 
257c2 69 73 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20  is used by.  ** 
257c3 53 51 4c 69 74 65 20 65 61 63 68 20 74 69 6d 65  SQLite each time
257c4 20 61 20 71 75 65 72 79 20 69 73 20 65 78 65 63   a query is exec
257c5 75 74 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74  uted to ensure t
257c6 68 61 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  hat the internal
257c7 20 63 61 63 68 65 0a 20 20 2a 2a 20 6f 66 20 74   cache.  ** of t
257c8 68 65 20 73 63 68 65 6d 61 20 75 73 65 64 20 77  he schema used w
257c9 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68  hen compiling th
257ca 65 20 53 51 4c 20 71 75 65 72 79 20 6d 61 74 63  e SQL query matc
257cb 68 65 73 20 74 68 65 20 73 63 68 65 6d 61 20 6f  hes the schema o
257cc 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62  f.  ** the datab
257cd 61 73 65 20 61 67 61 69 6e 73 74 20 77 68 69 63  ase against whic
257ce 68 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 71  h the compiled q
257cf 75 65 72 79 20 69 73 20 61 63 74 75 61 6c 6c 79  uery is actually
257d0 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2a 20   executed..  ** 
257d1 53 75 62 76 65 72 74 69 6e 67 20 74 68 69 73 20  Subverting this 
257d2 6d 65 63 68 61 6e 69 73 6d 20 62 79 20 75 73 69  mechanism by usi
257d3 6e 67 20 22 50 52 41 47 4d 41 20 73 63 68 65 6d  ng "PRAGMA schem
257d4 61 5f 76 65 72 73 69 6f 6e 22 20 74 6f 20 6d 6f  a_version" to mo
257d5 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20 73 63  dify.  ** the sc
257d6 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 69 73 20  hema-version is 
257d7 70 6f 74 65 6e 74 69 61 6c 6c 79 20 64 61 6e 67  potentially dang
257d8 65 72 6f 75 73 20 61 6e 64 20 6d 61 79 20 6c 65  erous and may le
257d9 61 64 20 74 6f 20 70 72 6f 67 72 61 6d 0a 20 20  ad to program.  
257da 2a 2a 20 63 72 61 73 68 65 73 20 6f 72 20 64 61  ** crashes or da
257db 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
257dc 6e 2e 20 55 73 65 20 77 69 74 68 20 63 61 75 74  n. Use with caut
257dd 69 6f 6e 21 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ion!.  **.  ** T
257de 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 20  he user-version 
257df 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65  is not used inte
257e0 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65  rnally by SQLite
257e1 2e 20 49 74 20 6d 61 79 20 62 65 20 75 73 65 64  . It may be used
257e2 20 62 79 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61   by.  ** applica
257e3 74 69 6f 6e 73 20 66 6f 72 20 61 6e 79 20 70 75  tions for any pu
257e4 72 70 6f 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  rpose..  */.  if
257e5 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
257e6 28 7a 4c 65 66 74 2c 20 22 73 63 68 65 6d 61 5f  (zLeft, "schema_
257e7 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20  version")==0 .  
257e8 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43   || sqlite3StrIC
257e9 6d 70 28 7a 4c 65 66 74 2c 20 22 75 73 65 72 5f  mp(zLeft, "user_
257ea 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20  version")==0 .  
257eb 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43   || sqlite3StrIC
257ec 6d 70 28 7a 4c 65 66 74 2c 20 22 66 72 65 65 6c  mp(zLeft, "freel
257ed 69 73 74 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 0a  ist_count")==0 .
257ee 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f    ){.    int iCo
257ef 6f 6b 69 65 3b 20 20 20 2f 2a 20 43 6f 6f 6b 69  okie;   /* Cooki
257f0 65 20 69 6e 64 65 78 2e 20 30 20 66 6f 72 20 73  e index. 0 for s
257f1 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 2c 20 36 20  chema-cookie, 6 
257f2 66 6f 72 20 75 73 65 72 2d 63 6f 6f 6b 69 65 2e  for user-cookie.
257f3 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
257f4 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
257f5 69 44 62 29 3b 0a 20 20 20 20 73 77 69 74 63 68  iDb);.    switch
257f6 28 20 7a 4c 65 66 74 5b 30 5d 20 29 7b 0a 20 20  ( zLeft[0] ){.  
257f7 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 63 61      case 's': ca
257f8 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20  se 'S':.        
257f9 69 43 6f 6f 6b 69 65 20 3d 20 30 3b 0a 20 20 20  iCookie = 0;.   
257fa 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
257fb 20 20 63 61 73 65 20 27 66 27 3a 20 63 61 73 65    case 'f': case
257fc 20 27 46 27 3a 0a 20 20 20 20 20 20 20 20 69 43   'F':.        iC
257fd 6f 6f 6b 69 65 20 3d 20 31 3b 0a 20 20 20 20 20  ookie = 1;.     
257fe 20 20 20 69 44 62 20 3d 20 28 2d 31 2a 28 69 44     iDb = (-1*(iD
257ff 62 2b 31 29 29 3b 0a 20 20 20 20 20 20 20 20 61  b+1));.        a
25800 73 73 65 72 74 28 69 44 62 3c 3d 30 29 3b 0a 20  ssert(iDb<=0);. 
25801 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
25802 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
25803 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 35       iCookie = 5
25804 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
25805 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
25806 7a 52 69 67 68 74 20 26 26 20 69 44 62 3e 3d 30  zRight && iDb>=0
25807 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 72 69   ){.      /* Wri
25808 74 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64  te the specified
25809 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f   cookie value */
2580a 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f  .      static co
2580b 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 73  nst VdbeOpList s
2580c 65 74 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20  etCookie[] = {. 
2580d 20 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e         { OP_Tran
2580e 73 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20  saction,    0,  
2580f 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20  1,  0},    /* 0 
25810 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f  */.        { OP_
25811 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20 20  Integer,        
25812 30 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f  0,  1,  0},    /
25813 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b  * 1 */.        {
25814 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 20   OP_SetCookie,  
25815 20 20 20 20 30 2c 20 20 30 2c 20 20 31 7d 2c 20      0,  0,  1}, 
25816 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20     /* 2 */.     
25817 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64   };.      int ad
25818 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
25819 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72  AddOpList(v, Arr
2581a 61 79 53 69 7a 65 28 73 65 74 43 6f 6f 6b 69 65  aySize(setCookie
2581b 29 2c 20 73 65 74 43 6f 6f 6b 69 65 29 3b 0a 20  ), setCookie);. 
2581c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2581d 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
2581e 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
2581f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
25820 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 61 74 6f  1(v, addr+1, ato
25821 69 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 20 20  i(zRight));.    
25822 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
25823 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 32 2c  ngeP1(v, addr+2,
25824 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
25825 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
25826 28 76 2c 20 61 64 64 72 2b 32 2c 20 69 43 6f 6f  (v, addr+2, iCoo
25827 6b 69 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  kie);.    }else{
25828 0a 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74  .      /* Read t
25829 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6f  he specified coo
2582a 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20  kie value */.   
2582b 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
2582c 56 64 62 65 4f 70 4c 69 73 74 20 72 65 61 64 43  VdbeOpList readC
2582d 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20  ookie[] = {.    
2582e 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f      { OP_ReadCoo
2582f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20 31 2c  kie,      0,  1,
25830 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f    0},    /* 0 */
25831 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65  .        { OP_Re
25832 73 75 6c 74 52 6f 77 2c 20 20 20 20 20 20 20 31  sultRow,       1
25833 2c 20 20 31 2c 20 20 30 7d 0a 20 20 20 20 20 20  ,  1,  0}.      
25834 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  };.      int add
25835 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
25836 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61  ddOpList(v, Arra
25837 79 53 69 7a 65 28 72 65 61 64 43 6f 6f 6b 69 65  ySize(readCookie
25838 29 2c 20 72 65 61 64 43 6f 6f 6b 69 65 29 3b 0a  ), readCookie);.
25839 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2583a 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64  eChangeP1(v, add
2583b 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  r, iDb);.      s
2583c 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2583d 50 33 28 76 2c 20 61 64 64 72 2c 20 69 43 6f 6f  P3(v, addr, iCoo
2583e 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  kie);.      sqli
2583f 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
25840 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  s(v, 1);.      s
25841 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
25842 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
25843 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 65 66 74 2c 20  ME_NAME, zLeft, 
25844 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
25845 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
25846 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
25847 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45  E_OMIT_SCHEMA_VE
25848 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 20 2a 2f  RSION_PRAGMAS */
25849 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
2584a 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64  LITE_DEBUG) || d
2584b 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
2584c 53 54 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65  ST).  /*.  ** Re
2584d 70 6f 72 74 20 74 68 65 20 63 75 72 72 65 6e 74  port the current
2584e 20 73 74 61 74 65 20 6f 66 20 66 69 6c 65 20 6c   state of file l
2584f 6f 67 73 20 66 6f 72 20 61 6c 6c 20 64 61 74 61  ogs for all data
25850 62 61 73 65 73 0a 20 20 2a 2f 0a 20 20 69 66 28  bases.  */.  if(
25851 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
25852 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 73 74 61  zLeft, "lock_sta
25853 74 75 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  tus")==0 ){.    
25854 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
25855 72 20 2a 63 6f 6e 73 74 20 61 7a 4c 6f 63 6b 4e  r *const azLockN
25856 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  ame[] = {.      
25857 22 75 6e 6c 6f 63 6b 65 64 22 2c 20 22 73 68 61  "unlocked", "sha
25858 72 65 64 22 2c 20 22 72 65 73 65 72 76 65 64 22  red", "reserved"
25859 2c 20 22 70 65 6e 64 69 6e 67 22 2c 20 22 65 78  , "pending", "ex
2585a 63 6c 75 73 69 76 65 22 0a 20 20 20 20 7d 3b 0a  clusive".    };.
2585b 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73      int i;.    s
2585c 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
2585d 43 6f 6c 73 28 76 2c 20 32 29 3b 0a 20 20 20 20  Cols(v, 2);.    
2585e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32  pParse->nMem = 2
2585f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25860 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
25861 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
25862 22 64 61 74 61 62 61 73 65 22 2c 20 53 51 4c 49  "database", SQLI
25863 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
25864 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
25865 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
25866 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 74 61 74 75  AME_NAME, "statu
25867 73 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  s", SQLITE_STATI
25868 43 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  C);.    for(i=0;
25869 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
2586a 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  {.      Btree *p
2586b 42 74 3b 0a 20 20 20 20 20 20 50 61 67 65 72 20  Bt;.      Pager 
2586c 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20 63  *pPager;.      c
2586d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 74  onst char *zStat
2586e 65 20 3d 20 22 75 6e 6b 6e 6f 77 6e 22 3b 0a 20  e = "unknown";. 
2586f 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
25870 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d    if( db->aDb[i]
25871 2e 7a 4e 61 6d 65 3d 3d 30 20 29 20 63 6f 6e 74  .zName==0 ) cont
25872 69 6e 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  inue;.      sqli
25873 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
25874 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
25875 31 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d  1, 0, db->aDb[i]
25876 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49  .zName, P4_STATI
25877 43 29 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20  C);.      pBt = 
25878 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
25879 20 20 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30        if( pBt==0
2587a 20 7c 7c 20 28 70 50 61 67 65 72 20 3d 20 73 71   || (pPager = sq
2587b 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
2587c 70 42 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  pBt))==0 ){.    
2587d 20 20 20 20 7a 53 74 61 74 65 20 3d 20 22 63 6c      zState = "cl
2587e 6f 73 65 64 22 3b 0a 20 20 20 20 20 20 7d 65 6c  osed";.      }el
2587f 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 66  se if( sqlite3_f
25880 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20  ile_control(db, 
25881 69 20 3f 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a  i ? db->aDb[i].z
25882 4e 61 6d 65 20 3a 20 30 2c 20 0a 20 20 20 20 20  Name : 0, .     
25883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25884 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25885 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
25886 4b 53 54 41 54 45 2c 20 26 6a 29 3d 3d 53 51 4c  KSTATE, &j)==SQL
25887 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
25888 20 20 20 7a 53 74 61 74 65 20 3d 20 61 7a 4c 6f     zState = azLo
25889 63 6b 4e 61 6d 65 5b 6a 5d 3b 0a 20 20 20 20 20  ckName[j];.     
2588a 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2588b 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2588c 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20  _String8, 0, 2, 
2588d 30 2c 20 7a 53 74 61 74 65 2c 20 50 34 5f 53 54  0, zState, P4_ST
2588e 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
2588f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25890 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
25891 31 2c 20 32 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  1, 2);.    }..  
25892 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69  }else.#endif..#i
25893 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a  fdef SQLITE_SSE.
25894 20 20 2f 2a 0a 20 20 2a 2a 20 43 68 65 63 6b 20    /*.  ** Check 
25895 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 73 71  to see if the sq
25896 6c 69 74 65 5f 73 74 61 74 65 6d 65 6e 74 73 20  lite_statements 
25897 74 61 62 6c 65 20 65 78 69 73 74 73 2e 20 20 43  table exists.  C
25898 72 65 61 74 65 20 69 74 0a 20 20 2a 2a 20 69 66  reate it.  ** if
25899 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20   it does not..  
2589a 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
2589b 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
2589c 63 72 65 61 74 65 5f 73 71 6c 69 74 65 5f 73 74  create_sqlite_st
2589d 61 74 65 6d 65 6e 74 5f 74 61 62 6c 65 22 29 3d  atement_table")=
2589e 3d 30 20 29 7b 0a 20 20 20 20 65 78 74 65 72 6e  =0 ){.    extern
2589f 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61   int sqlite3Crea
258a0 74 65 53 74 61 74 65 6d 65 6e 74 73 54 61 62 6c  teStatementsTabl
258a1 65 28 50 61 72 73 65 2a 29 3b 0a 20 20 20 20 73  e(Parse*);.    s
258a2 71 6c 69 74 65 33 43 72 65 61 74 65 53 74 61 74  qlite3CreateStat
258a3 65 6d 65 6e 74 73 54 61 62 6c 65 28 70 50 61 72  ementsTable(pPar
258a4 73 65 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  se);.  }else.#en
258a5 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  dif..#if SQLITE_
258a6 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 20  HAS_CODEC.  if( 
258a7 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
258a8 4c 65 66 74 2c 20 22 6b 65 79 22 29 3d 3d 30 20  Left, "key")==0 
258a9 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  && zRight ){.   
258aa 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c   sqlite3_key(db,
258ab 20 7a 52 69 67 68 74 2c 20 73 71 6c 69 74 65 33   zRight, sqlite3
258ac 53 74 72 6c 65 6e 33 30 28 7a 52 69 67 68 74 29  Strlen30(zRight)
258ad 29 3b 0a 20 20 7d 65 6c 73 65 0a 20 20 69 66 28  );.  }else.  if(
258ae 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
258af 7a 4c 65 66 74 2c 20 22 72 65 6b 65 79 22 29 3d  zLeft, "rekey")=
258b0 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a  =0 && zRight ){.
258b1 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6b 65      sqlite3_reke
258b2 79 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 73 71  y(db, zRight, sq
258b3 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 52  lite3Strlen30(zR
258b4 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a  ight));.  }else.
258b5 20 20 69 66 28 20 7a 52 69 67 68 74 20 26 26 20    if( zRight && 
258b6 28 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  (sqlite3StrICmp(
258b7 7a 4c 65 66 74 2c 20 22 68 65 78 6b 65 79 22 29  zLeft, "hexkey")
258b8 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  ==0 ||.         
258b9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
258ba 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 68  trICmp(zLeft, "h
258bb 65 78 72 65 6b 65 79 22 29 3d 3d 30 29 20 29 7b  exrekey")==0) ){
258bc 0a 20 20 20 20 69 6e 74 20 69 2c 20 68 31 2c 20  .    int i, h1, 
258bd 68 32 3b 0a 20 20 20 20 63 68 61 72 20 7a 4b 65  h2;.    char zKe
258be 79 5b 34 30 5d 3b 0a 20 20 20 20 66 6f 72 28 69  y[40];.    for(i
258bf 3d 30 3b 20 28 68 31 20 3d 20 7a 52 69 67 68 74  =0; (h1 = zRight
258c0 5b 69 5d 29 21 3d 30 20 26 26 20 28 68 32 20 3d  [i])!=0 && (h2 =
258c1 20 7a 52 69 67 68 74 5b 69 2b 31 5d 29 21 3d 30   zRight[i+1])!=0
258c2 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 68  ; i+=2){.      h
258c3 31 20 2b 3d 20 39 2a 28 31 26 28 68 31 3e 3e 36  1 += 9*(1&(h1>>6
258c4 29 29 3b 0a 20 20 20 20 20 20 68 32 20 2b 3d 20  ));.      h2 += 
258c5 39 2a 28 31 26 28 68 32 3e 3e 36 29 29 3b 0a 20  9*(1&(h2>>6));. 
258c6 20 20 20 20 20 7a 4b 65 79 5b 69 2f 32 5d 20 3d       zKey[i/2] =
258c7 20 28 68 32 20 26 20 30 78 30 66 29 20 7c 20 28   (h2 & 0x0f) | (
258c8 28 68 31 20 26 20 30 78 66 29 3c 3c 34 29 3b 0a  (h1 & 0xf)<<4);.
258c9 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 7a      }.    if( (z
258ca 4c 65 66 74 5b 33 5d 20 26 20 30 78 66 29 3d 3d  Left[3] & 0xf)==
258cb 30 78 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  0xb ){.      sql
258cc 69 74 65 33 5f 6b 65 79 28 64 62 2c 20 7a 4b 65  ite3_key(db, zKe
258cd 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d 65 6c  y, i/2);.    }el
258ce 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
258cf 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 4b 65 79  3_rekey(db, zKey
258d0 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20  , i/2);.    }.  
258d1 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 23 69 66  }else.#endif.#if
258d2 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
258d3 43 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  C || defined(SQL
258d4 49 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44  ITE_ENABLE_CEROD
258d5 29 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ).  if( sqlite3S
258d6 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 61  trICmp(zLeft, "a
258d7 63 74 69 76 61 74 65 5f 65 78 74 65 6e 73 69 6f  ctivate_extensio
258d8 6e 73 22 29 3d 3d 30 20 29 7b 0a 23 69 66 20 53  ns")==0 ){.#if S
258d9 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a  QLITE_HAS_CODEC.
258da 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
258db 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20  trNICmp(zRight, 
258dc 22 73 65 65 2d 22 2c 20 34 29 3d 3d 30 20 29 7b  "see-", 4)==0 ){
258dd 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20 76 6f  .      extern vo
258de 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76  id sqlite3_activ
258df 61 74 65 5f 73 65 65 28 63 6f 6e 73 74 20 63 68  ate_see(const ch
258e0 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ar*);.      sqli
258e1 74 65 33 5f 61 63 74 69 76 61 74 65 5f 73 65 65  te3_activate_see
258e2 28 26 7a 52 69 67 68 74 5b 34 5d 29 3b 0a 20 20  (&zRight[4]);.  
258e3 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65    }.#endif.#ifde
258e4 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
258e5 43 45 52 4f 44 0a 20 20 20 20 69 66 28 20 73 71  CEROD.    if( sq
258e6 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 52  lite3StrNICmp(zR
258e7 69 67 68 74 2c 20 22 63 65 72 6f 64 2d 22 2c 20  ight, "cerod-", 
258e8 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65  6)==0 ){.      e
258e9 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74  xtern void sqlit
258ea 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65 72 6f  e3_activate_cero
258eb 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  d(const char*);.
258ec 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 63        sqlite3_ac
258ed 74 69 76 61 74 65 5f 63 65 72 6f 64 28 26 7a 52  tivate_cerod(&zR
258ee 69 67 68 74 5b 36 5d 29 3b 0a 20 20 20 20 7d 0a  ight[6]);.    }.
258ef 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 0a 23  #endif.  }else.#
258f0 65 6e 64 69 66 0a 0a 20 0a 20 20 7b 2f 2a 20 45  endif.. .  {/* E
258f1 6d 70 74 79 20 45 4c 53 45 20 63 6c 61 75 73 65  mpty ELSE clause
258f2 20 2a 2f 7d 0a 0a 20 20 2f 2a 20 43 6f 64 65 20   */}..  /* Code 
258f3 61 6e 20 4f 50 5f 45 78 70 69 72 65 20 61 74 20  an OP_Expire at 
258f4 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20  the end of each 
258f5 50 52 41 47 4d 41 20 70 72 6f 67 72 61 6d 20 74  PRAGMA program t
258f6 6f 20 63 61 75 73 65 0a 20 20 2a 2a 20 74 68 65  o cause.  ** the
258f7 20 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 69   VDBE implementi
258f8 6e 67 20 74 68 65 20 70 72 61 67 6d 61 20 74 6f  ng the pragma to
258f9 20 65 78 70 69 72 65 2e 20 4d 6f 73 74 20 28 61   expire. Most (a
258fa 6c 6c 3f 29 20 70 72 61 67 6d 61 73 0a 20 20 2a  ll?) pragmas.  *
258fb 2a 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64  * are only valid
258fc 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 78   for a single ex
258fd 65 63 75 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  ecution..  */.  
258fe 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
258ff 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20  2(v, OP_Expire, 
25900 31 2c 20 30 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  1, 0);..  /*.  *
25901 2a 20 52 65 73 65 74 20 74 68 65 20 73 61 66 65  * Reset the safe
25902 74 79 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73  ty level, in cas
25903 65 20 74 68 65 20 66 75 6c 6c 66 73 79 6e 63 20  e the fullfsync 
25904 66 6c 61 67 20 6f 72 20 73 79 6e 63 68 72 6f 6e  flag or synchron
25905 6f 75 73 0a 20 20 2a 2a 20 73 65 74 74 69 6e 67  ous.  ** setting
25906 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 23   changed..  */.#
25907 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25908 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
25909 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43  .  if( db->autoC
2590a 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c  ommit ){.    sql
2590b 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65  ite3BtreeSetSafe
2590c 74 79 4c 65 76 65 6c 28 70 44 62 2d 3e 70 42 74  tyLevel(pDb->pBt
2590d 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65  , pDb->safety_le
2590e 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  vel,.           
2590f 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73 26 53      (db->flags&S
25910 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 29  QLITE_FullFSync)
25911 21 3d 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  !=0);.  }.#endif
25912 0a 70 72 61 67 6d 61 5f 6f 75 74 3a 0a 20 20 73  .pragma_out:.  s
25913 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
25914 20 7a 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69 74   zLeft);.  sqlit
25915 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 52 69  e3DbFree(db, zRi
25916 67 68 74 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  ght);.}..#endif 
25917 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  /* SQLITE_OMIT_P
25918 52 41 47 4d 41 20 7c 7c 20 53 51 4c 49 54 45 5f  RAGMA || SQLITE_
25919 4f 4d 49 54 5f 50 41 52 53 45 52 20 2a 2f 0a 0a  OMIT_PARSER */..
2591a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2591b 45 6e 64 20 6f 66 20 70 72 61 67 6d 61 2e 63 20  End of pragma.c 
2591c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2591d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2591e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2591f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
25920 42 65 67 69 6e 20 66 69 6c 65 20 70 72 65 70 61  Begin file prepa
25921 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  re.c ***********
25922 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
25924 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32  /*.** 2005 May 2
25925 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
25926 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
25927 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
25928 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
25929 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2592a 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2592b 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2592c 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2592d 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2592e 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2592f 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
25930 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
25931 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
25932 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
25933 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
25934 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
25935 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
25936 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
25937 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25938 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25939 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2593a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2593b 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
2593c 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65  ntains the imple
2593d 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
2593e 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
2593f 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c  ().** interface,
25940 20 61 6e 64 20 72 6f 75 74 69 6e 65 73 20 74 68   and routines th
25941 61 74 20 63 6f 6e 74 72 69 62 75 74 65 20 74 6f  at contribute to
25942 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 61 74   loading the dat
25943 61 62 61 73 65 20 73 63 68 65 6d 61 0a 2a 2a 20  abase schema.** 
25944 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  from disk..**.**
25945 20 24 49 64 3a 20 70 72 65 70 61 72 65 2e 63 2c   $Id: prepare.c,
25946 76 20 31 2e 31 30 34 20 32 30 30 39 2f 30 31 2f  v 1.104 2009/01/
25947 30 39 20 30 32 3a 34 39 3a 33 32 20 64 72 68 20  09 02:49:32 drh 
25948 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
25949 46 69 6c 6c 20 74 68 65 20 49 6e 69 74 44 61 74  Fill the InitDat
2594a 61 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68  a structure with
2594b 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
2594c 65 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73  e that indicates
2594d 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 74  .** that the dat
2594e 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74  abase is corrupt
2594f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
25950 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 0a   corruptSchema(.
25951 20 20 49 6e 69 74 44 61 74 61 20 2a 70 44 61 74    InitData *pDat
25952 61 2c 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61  a,     /* Initia
25953 6c 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74  lization context
25954 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
25955 20 2a 7a 4f 62 6a 2c 20 20 20 20 2f 2a 20 4f 62   *zObj,    /* Ob
25956 6a 65 63 74 20 62 65 69 6e 67 20 70 61 72 73 65  ject being parse
25957 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20 6f  d at the point o
25958 66 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e  f error */.  con
25959 73 74 20 63 68 61 72 20 2a 7a 45 78 74 72 61 20  st char *zExtra 
2595a 20 20 2f 2a 20 45 72 72 6f 72 20 69 6e 66 6f 72    /* Error infor
2595b 6d 61 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73  mation */.){.  s
2595c 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 44 61  qlite3 *db = pDa
2595d 74 61 2d 3e 64 62 3b 0a 20 20 69 66 28 20 21 64  ta->db;.  if( !d
2595e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2595f 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20  && (db->flags & 
25960 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
25961 6f 64 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ode)==0 ){.    i
25962 66 28 20 7a 4f 62 6a 3d 3d 30 20 29 20 7a 4f 62  f( zObj==0 ) zOb
25963 6a 20 3d 20 22 3f 22 3b 0a 20 20 20 20 73 71 6c  j = "?";.    sql
25964 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 44  ite3SetString(pD
25965 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 70  ata->pzErrMsg, p
25966 44 61 74 61 2d 3e 64 62 2c 0a 20 20 20 20 20 20  Data->db,.      
25967 20 22 6d 61 6c 66 6f 72 6d 65 64 20 64 61 74 61   "malformed data
25968 62 61 73 65 20 73 63 68 65 6d 61 20 28 25 73 29  base schema (%s)
25969 22 2c 20 7a 4f 62 6a 29 3b 0a 20 20 20 20 69 66  ", zObj);.    if
2596a 28 20 7a 45 78 74 72 61 20 26 26 20 7a 45 78 74  ( zExtra && zExt
2596b 72 61 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 2a  ra[0] ){.      *
2596c 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 20  pData->pzErrMsg 
2596d 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64  = sqlite3MAppend
2596e 66 28 70 44 61 74 61 2d 3e 64 62 2c 20 2a 70 44  f(pData->db, *pD
2596f 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 22  ata->pzErrMsg, "
25970 25 73 20 2d 20 25 73 22 2c 0a 20 20 20 20 20 20  %s - %s",.      
25971 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25972 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 44 61              *pDa
25973 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 7a 45  ta->pzErrMsg, zE
25974 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  xtra);.    }.  }
25975 0a 20 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 53  .  pData->rc = S
25976 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 7d  QLITE_CORRUPT;.}
25977 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
25978 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75  the callback rou
25979 74 69 6e 65 20 66 6f 72 20 74 68 65 20 63 6f 64  tine for the cod
2597a 65 20 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a  e that initializ
2597b 65 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  es the.** databa
2597c 73 65 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33  se.  See sqlite3
2597d 49 6e 69 74 28 29 20 62 65 6c 6f 77 20 66 6f 72  Init() below for
2597e 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
2597f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73  rmation..** This
25980 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6c 73 6f   routine is also
25981 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
25982 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20   OP_ParseSchema 
25983 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44  opcode of the VD
25984 42 45 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63  BE..**.** Each c
25985 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73  allback contains
25986 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
25987 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a  nformation:.**.*
25988 2a 20 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20  *     argv[0] = 
25989 6e 61 6d 65 20 6f 66 20 74 68 69 6e 67 20 62 65  name of thing be
2598a 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a 20 20  ing created.**  
2598b 20 20 20 61 72 67 76 5b 31 5d 20 3d 20 72 6f 6f     argv[1] = roo
2598c 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  t page number fo
2598d 72 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  r table or index
2598e 2e 20 30 20 66 6f 72 20 74 72 69 67 67 65 72 20  . 0 for trigger 
2598f 6f 72 20 76 69 65 77 2e 0a 2a 2a 20 20 20 20 20  or view..**     
25990 61 72 67 76 5b 32 5d 20 3d 20 53 51 4c 20 74 65  argv[2] = SQL te
25991 78 74 20 66 6f 72 20 74 68 65 20 43 52 45 41 54  xt for the CREAT
25992 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  E statement..**.
25993 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
25994 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69  E int sqlite3Ini
25995 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 2a  tCallback(void *
25996 70 49 6e 69 74 2c 20 69 6e 74 20 61 72 67 63 2c  pInit, int argc,
25997 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68   char **argv, ch
25998 61 72 20 2a 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  ar **NotUsed){. 
25999 20 49 6e 69 74 44 61 74 61 20 2a 70 44 61 74 61   InitData *pData
2599a 20 3d 20 28 49 6e 69 74 44 61 74 61 2a 29 70 49   = (InitData*)pI
2599b 6e 69 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit;.  sqlite3 *
2599c 64 62 20 3d 20 70 44 61 74 61 2d 3e 64 62 3b 0a  db = pData->db;.
2599d 20 20 69 6e 74 20 69 44 62 20 3d 20 70 44 61 74    int iDb = pDat
2599e 61 2d 3e 69 44 62 3b 0a 0a 20 20 61 73 73 65 72  a->iDb;..  asser
2599f 74 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20  t( argc==3 );.  
259a0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
259a1 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 67 63 29  2(NotUsed, argc)
259a2 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
259a3 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
259a4 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 44  b->mutex) );.  D
259a5 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64  bClearProperty(d
259a6 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d 70 74 79  b, iDb, DB_Empty
259a7 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
259a8 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
259a9 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70   corruptSchema(p
259aa 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 30  Data, argv[0], 0
259ab 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
259ac 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
259ad 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
259ae 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
259af 20 29 3b 0a 20 20 69 66 28 20 61 72 67 76 3d 3d   );.  if( argv==
259b0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  0 ) return 0;   
259b1 2f 2a 20 4d 69 67 68 74 20 68 61 70 70 65 6e 20  /* Might happen 
259b2 69 66 20 45 4d 50 54 59 5f 52 45 53 55 4c 54 5f  if EMPTY_RESULT_
259b3 43 41 4c 4c 42 41 43 4b 53 20 61 72 65 20 6f 6e  CALLBACKS are on
259b4 20 2a 2f 0a 20 20 69 66 28 20 61 72 67 76 5b 31   */.  if( argv[1
259b5 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 72 72  ]==0 ){.    corr
259b6 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c  uptSchema(pData,
259b7 20 61 72 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20   argv[0], 0);.  
259b8 7d 65 6c 73 65 20 69 66 28 20 61 72 67 76 5b 32  }else if( argv[2
259b9 5d 20 26 26 20 61 72 67 76 5b 32 5d 5b 30 5d 20  ] && argv[2][0] 
259ba 29 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74  ){.    /* Call t
259bb 68 65 20 70 61 72 73 65 72 20 74 6f 20 70 72 6f  he parser to pro
259bc 63 65 73 73 20 61 20 43 52 45 41 54 45 20 54 41  cess a CREATE TA
259bd 42 4c 45 2c 20 49 4e 44 45 58 20 6f 72 20 56 49  BLE, INDEX or VI
259be 45 57 2e 0a 20 20 20 20 2a 2a 20 42 75 74 20 62  EW..    ** But b
259bf 65 63 61 75 73 65 20 64 62 2d 3e 69 6e 69 74 2e  ecause db->init.
259c0 62 75 73 79 20 69 73 20 73 65 74 20 74 6f 20 31  busy is set to 1
259c1 2c 20 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 69  , no VDBE code i
259c2 73 20 67 65 6e 65 72 61 74 65 64 0a 20 20 20 20  s generated.    
259c3 2a 2a 20 6f 72 20 65 78 65 63 75 74 65 64 2e 20  ** or executed. 
259c4 20 41 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20   All the parser 
259c5 64 6f 65 73 20 69 73 20 62 75 69 6c 64 20 74 68  does is build th
259c6 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a  e internal data.
259c7 20 20 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65      ** structure
259c8 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20  s that describe 
259c9 74 68 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78  the table, index
259ca 2c 20 6f 72 20 76 69 65 77 2e 0a 20 20 20 20 2a  , or view..    *
259cb 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72  /.    char *zErr
259cc 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  ;.    int rc;.  
259cd 20 20 75 38 20 6c 6f 6f 6b 61 73 69 64 65 45 6e    u8 lookasideEn
259ce 61 62 6c 65 64 3b 0a 20 20 20 20 61 73 73 65 72  abled;.    asser
259cf 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  t( db->init.busy
259d0 20 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74   );.    db->init
259d1 2e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20  .iDb = iDb;.    
259d2 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d  db->init.newTnum
259d3 20 3d 20 61 74 6f 69 28 61 72 67 76 5b 31 5d 29   = atoi(argv[1])
259d4 3b 0a 20 20 20 20 6c 6f 6f 6b 61 73 69 64 65 45  ;.    lookasideE
259d5 6e 61 62 6c 65 64 20 3d 20 64 62 2d 3e 6c 6f 6f  nabled = db->loo
259d6 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b  kaside.bEnabled;
259d7 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  .    db->lookasi
259d8 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b  de.bEnabled = 0;
259d9 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
259da 33 5f 65 78 65 63 28 64 62 2c 20 61 72 67 76 5b  3_exec(db, argv[
259db 32 5d 2c 20 30 2c 20 30 2c 20 26 7a 45 72 72 29  2], 0, 0, &zErr)
259dc 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69  ;.    db->init.i
259dd 44 62 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e  Db = 0;.    db->
259de 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
259df 65 64 20 3d 20 6c 6f 6f 6b 61 73 69 64 65 45 6e  ed = lookasideEn
259e0 61 62 6c 65 64 3b 0a 20 20 20 20 61 73 73 65 72  abled;.    asser
259e1 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
259e2 20 7c 7c 20 7a 45 72 72 3d 3d 30 20 29 3b 0a 20   || zErr==0 );. 
259e3 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
259e4 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 70 44  !=rc ){.      pD
259e5 61 74 61 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20  ata->rc = rc;.  
259e6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
259e7 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
259e8 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
259e9 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
259ea 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
259eb 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 29  LITE_INTERRUPT )
259ec 7b 0a 20 20 20 20 20 20 20 20 63 6f 72 72 75 70  {.        corrup
259ed 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61  tSchema(pData, a
259ee 72 67 76 5b 30 5d 2c 20 7a 45 72 72 29 3b 0a 20  rgv[0], zErr);. 
259ef 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
259f0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
259f1 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  Err);.    }.  }e
259f2 6c 73 65 20 69 66 28 20 61 72 67 76 5b 30 5d 3d  lse if( argv[0]=
259f3 3d 30 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70  =0 ){.    corrup
259f4 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 30  tSchema(pData, 0
259f5 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  , 0);.  }else{. 
259f6 20 20 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c     /* If the SQL
259f7 20 63 6f 6c 75 6d 6e 20 69 73 20 62 6c 61 6e 6b   column is blank
259f8 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69   it means this i
259f9 73 20 61 6e 20 69 6e 64 65 78 20 74 68 61 74 0a  s an index that.
259fa 20 20 20 20 2a 2a 20 77 61 73 20 63 72 65 61 74      ** was creat
259fb 65 64 20 74 6f 20 62 65 20 74 68 65 20 50 52 49  ed to be the PRI
259fc 4d 41 52 59 20 4b 45 59 20 6f 72 20 74 6f 20 66  MARY KEY or to f
259fd 75 6c 66 69 6c 6c 20 61 20 55 4e 49 51 55 45 0a  ulfill a UNIQUE.
259fe 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e      ** constrain
259ff 74 20 66 6f 72 20 61 20 43 52 45 41 54 45 20 54  t for a CREATE T
25a00 41 42 4c 45 2e 20 20 54 68 65 20 69 6e 64 65 78  ABLE.  The index
25a01 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72   should have alr
25a02 65 61 64 79 0a 20 20 20 20 2a 2a 20 62 65 65 6e  eady.    ** been
25a03 20 63 72 65 61 74 65 64 20 77 68 65 6e 20 77 65   created when we
25a04 20 70 72 6f 63 65 73 73 65 64 20 74 68 65 20 43   processed the C
25a05 52 45 41 54 45 20 54 41 42 4c 45 2e 20 20 41 6c  REATE TABLE.  Al
25a06 6c 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a  l we have.    **
25a07 20 74 6f 20 64 6f 20 68 65 72 65 20 69 73 20 72   to do here is r
25a08 65 63 6f 72 64 20 74 68 65 20 72 6f 6f 74 20 70  ecord the root p
25a09 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
25a0a 68 61 74 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a  hat index..    *
25a0b 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e  /.    Index *pIn
25a0c 64 65 78 3b 0a 20 20 20 20 70 49 6e 64 65 78 20  dex;.    pIndex 
25a0d 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64  = sqlite3FindInd
25a0e 65 78 28 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20  ex(db, argv[0], 
25a0f 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
25a10 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e  me);.    if( pIn
25a11 64 65 78 3d 3d 30 20 7c 7c 20 70 49 6e 64 65 78  dex==0 || pIndex
25a12 2d 3e 74 6e 75 6d 21 3d 30 20 29 7b 0a 20 20 20  ->tnum!=0 ){.   
25a13 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6e 20 6f     /* This can o
25a14 63 63 75 72 20 69 66 20 74 68 65 72 65 20 65 78  ccur if there ex
25a15 69 73 74 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e  ists an index on
25a16 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 77 68   a TEMP table wh
25a17 69 63 68 0a 20 20 20 20 20 20 2a 2a 20 68 61 73  ich.      ** has
25a18 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 61   the same name a
25a19 73 20 61 6e 6f 74 68 65 72 20 69 6e 64 65 78 20  s another index 
25a1a 6f 6e 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 69  on a permanent i
25a1b 6e 64 65 78 2e 20 20 53 69 6e 63 65 0a 20 20 20  ndex.  Since.   
25a1c 20 20 20 2a 2a 20 74 68 65 20 70 65 72 6d 61 6e     ** the perman
25a1d 65 6e 74 20 74 61 62 6c 65 20 69 73 20 68 69 64  ent table is hid
25a1e 64 65 6e 20 62 79 20 74 68 65 20 54 45 4d 50 20  den by the TEMP 
25a1f 74 61 62 6c 65 2c 20 77 65 20 63 61 6e 20 61 6c  table, we can al
25a20 73 6f 0a 20 20 20 20 20 20 2a 2a 20 73 61 66 65  so.      ** safe
25a21 6c 79 20 69 67 6e 6f 72 65 20 74 68 65 20 69 6e  ly ignore the in
25a22 64 65 78 20 6f 6e 20 74 68 65 20 70 65 72 6d 61  dex on the perma
25a23 6e 65 6e 74 20 74 61 62 6c 65 2e 0a 20 20 20 20  nent table..    
25a24 20 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 44 6f    */.      /* Do
25a25 20 4e 6f 74 68 69 6e 67 20 2a 2f 3b 0a 20 20 20   Nothing */;.   
25a26 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49   }else{.      pI
25a27 6e 64 65 78 2d 3e 74 6e 75 6d 20 3d 20 61 74 6f  ndex->tnum = ato
25a28 69 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  i(argv[1]);.    
25a29 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
25a2a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  ;.}../*.** Attem
25a2b 70 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 64  pt to read the d
25a2c 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 61  atabase schema a
25a2d 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e  nd initialize in
25a2e 74 65 72 6e 61 6c 0a 2a 2a 20 64 61 74 61 20 73  ternal.** data s
25a2f 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 61 20  tructures for a 
25a30 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20  single database 
25a31 66 69 6c 65 2e 20 20 54 68 65 20 69 6e 64 65 78  file.  The index
25a32 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   of the.** datab
25a33 61 73 65 20 66 69 6c 65 20 69 73 20 67 69 76 65  ase file is give
25a34 6e 20 62 79 20 69 44 62 2e 20 20 69 44 62 3d 3d  n by iDb.  iDb==
25a35 30 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 68  0 is used for th
25a36 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61  e main.** databa
25a37 73 65 2e 20 20 69 44 62 3d 3d 31 20 73 68 6f 75  se.  iDb==1 shou
25a38 6c 64 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  ld never be used
25a39 2e 20 20 69 44 62 3e 3d 32 20 69 73 20 75 73 65  .  iDb>=2 is use
25a3a 64 20 66 6f 72 0a 2a 2a 20 61 75 78 69 6c 69 61  d for.** auxilia
25a3b 72 79 20 64 61 74 61 62 61 73 65 73 2e 20 20 52  ry databases.  R
25a3c 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65  eturn one of the
25a3d 20 53 51 4c 49 54 45 5f 20 65 72 72 6f 72 20 63   SQLITE_ error c
25a3e 6f 64 65 73 20 74 6f 0a 2a 2a 20 69 6e 64 69 63  odes to.** indic
25a3f 61 74 65 20 73 75 63 63 65 73 73 20 6f 72 20 66  ate success or f
25a40 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  ailure..*/.stati
25a41 63 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69  c int sqlite3Ini
25a42 74 4f 6e 65 28 73 71 6c 69 74 65 33 20 2a 64 62  tOne(sqlite3 *db
25a43 2c 20 69 6e 74 20 69 44 62 2c 20 63 68 61 72 20  , int iDb, char 
25a44 2a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69  **pzErrMsg){.  i
25a45 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 73 6f  nt rc;.  BtCurso
25a46 72 20 2a 63 75 72 4d 61 69 6e 3b 0a 20 20 69 6e  r *curMain;.  in
25a47 74 20 73 69 7a 65 3b 0a 20 20 54 61 62 6c 65 20  t size;.  Table 
25a48 2a 70 54 61 62 3b 0a 20 20 44 62 20 2a 70 44 62  *pTab;.  Db *pDb
25a49 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
25a4a 61 7a 41 72 67 5b 34 5d 3b 0a 20 20 69 6e 74 20  azArg[4];.  int 
25a4b 6d 65 74 61 5b 31 30 5d 3b 0a 20 20 49 6e 69 74  meta[10];.  Init
25a4c 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20  Data initData;. 
25a4d 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61   char const *zMa
25a4e 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 63 68  sterSchema;.  ch
25a4f 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65  ar const *zMaste
25a50 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54  rName = SCHEMA_T
25a51 41 42 4c 45 28 69 44 62 29 3b 0a 0a 20 20 2f 2a  ABLE(iDb);..  /*
25a52 0a 20 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72  .  ** The master
25a53 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
25a54 68 61 73 20 61 20 73 74 72 75 63 74 75 72 65 20  has a structure 
25a55 6c 69 6b 65 20 74 68 69 73 0a 20 20 2a 2f 0a 20  like this.  */. 
25a56 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
25a57 61 72 20 6d 61 73 74 65 72 5f 73 63 68 65 6d 61  ar master_schema
25a58 5b 5d 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41  [] = .     "CREA
25a59 54 45 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f  TE TABLE sqlite_
25a5a 6d 61 73 74 65 72 28 5c 6e 22 0a 20 20 20 20 20  master(\n".     
25a5b 22 20 20 74 79 70 65 20 74 65 78 74 2c 5c 6e 22  "  type text,\n"
25a5c 0a 20 20 20 20 20 22 20 20 6e 61 6d 65 20 74 65  .     "  name te
25a5d 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 74  xt,\n".     "  t
25a5e 62 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22  bl_name text,\n"
25a5f 0a 20 20 20 20 20 22 20 20 72 6f 6f 74 70 61 67  .     "  rootpag
25a60 65 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20  e integer,\n".  
25a61 20 20 20 22 20 20 73 71 6c 20 74 65 78 74 5c 6e     "  sql text\n
25a62 22 0a 20 20 20 20 20 22 29 22 0a 20 20 3b 0a 23  ".     ")".  ;.#
25a63 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25a64 49 54 5f 54 45 4d 50 44 42 0a 20 20 73 74 61 74  IT_TEMPDB.  stat
25a65 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 74 65  ic const char te
25a66 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d 61  mp_master_schema
25a67 5b 5d 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41  [] = .     "CREA
25a68 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 73 71  TE TEMP TABLE sq
25a69 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72  lite_temp_master
25a6a 28 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 79 70  (\n".     "  typ
25a6b 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20  e text,\n".     
25a6c 22 20 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22  "  name text,\n"
25a6d 0a 20 20 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d  .     "  tbl_nam
25a6e 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20  e text,\n".     
25a6f 22 20 20 72 6f 6f 74 70 61 67 65 20 69 6e 74 65  "  rootpage inte
25a70 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ger,\n".     "  
25a71 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20 20 20 20  sql text\n".    
25a72 20 22 29 22 0a 20 20 3b 0a 23 65 6c 73 65 0a 20   ")".  ;.#else. 
25a73 20 23 64 65 66 69 6e 65 20 74 65 6d 70 5f 6d 61   #define temp_ma
25a74 73 74 65 72 5f 73 63 68 65 6d 61 20 30 0a 23 65  ster_schema 0.#e
25a75 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20  ndif..  assert( 
25a76 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
25a77 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
25a78 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
25a79 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 61 73 73  pSchema );.  ass
25a7a 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
25a7b 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
25a7c 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
25a7d 69 44 62 3d 3d 31 20 7c 7c 20 73 71 6c 69 74 65  iDb==1 || sqlite
25a7e 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
25a7f 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42  (db->aDb[iDb].pB
25a80 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73  t) );..  /* zMas
25a81 74 65 72 53 63 68 65 6d 61 20 61 6e 64 20 7a 49  terSchema and zI
25a82 6e 69 74 53 63 72 69 70 74 20 61 72 65 20 73 65  nitScript are se
25a83 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  t to point at th
25a84 65 20 6d 61 73 74 65 72 20 73 63 68 65 6d 61 0a  e master schema.
25a85 20 20 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c    ** and initial
25a86 69 73 61 74 69 6f 6e 20 73 63 72 69 70 74 20 61  isation script a
25a87 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74  ppropriate for t
25a88 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e  he database bein
25a89 67 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 73  g.  ** initialis
25a8a 65 64 2e 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20  ed. zMasterName 
25a8b 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
25a8c 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  he master table.
25a8d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 4f 4d 49  .  */.  if( !OMI
25a8e 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
25a8f 3d 31 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65  =1 ){.    zMaste
25a90 72 53 63 68 65 6d 61 20 3d 20 74 65 6d 70 5f 6d  rSchema = temp_m
25a91 61 73 74 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20  aster_schema;.  
25a92 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4d 61 73 74  }else{.    zMast
25a93 65 72 53 63 68 65 6d 61 20 3d 20 6d 61 73 74 65  erSchema = maste
25a94 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 0a 20 20  r_schema;.  }.  
25a95 7a 4d 61 73 74 65 72 4e 61 6d 65 20 3d 20 53 43  zMasterName = SC
25a96 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b  HEMA_TABLE(iDb);
25a97 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74  ..  /* Construct
25a98 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c   the schema tabl
25a99 65 73 2e 20 20 2a 2f 0a 20 20 61 7a 41 72 67 5b  es.  */.  azArg[
25a9a 30 5d 20 3d 20 7a 4d 61 73 74 65 72 4e 61 6d 65  0] = zMasterName
25a9b 3b 0a 20 20 61 7a 41 72 67 5b 31 5d 20 3d 20 22  ;.  azArg[1] = "
25a9c 31 22 3b 0a 20 20 61 7a 41 72 67 5b 32 5d 20 3d  1";.  azArg[2] =
25a9d 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 3b 0a   zMasterSchema;.
25a9e 20 20 61 7a 41 72 67 5b 33 5d 20 3d 20 30 3b 0a    azArg[3] = 0;.
25a9f 20 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20    initData.db = 
25aa0 64 62 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 69  db;.  initData.i
25aa1 44 62 20 3d 20 69 44 62 3b 0a 20 20 69 6e 69 74  Db = iDb;.  init
25aa2 44 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 45  Data.rc = SQLITE
25aa3 5f 4f 4b 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e  _OK;.  initData.
25aa4 70 7a 45 72 72 4d 73 67 20 3d 20 70 7a 45 72 72  pzErrMsg = pzErr
25aa5 4d 73 67 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c  Msg;.  (void)sql
25aa6 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
25aa7 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 6e 69 74  );.  sqlite3Init
25aa8 43 61 6c 6c 62 61 63 6b 28 26 69 6e 69 74 44 61  Callback(&initDa
25aa9 74 61 2c 20 33 2c 20 28 63 68 61 72 20 2a 2a 29  ta, 3, (char **)
25aaa 61 7a 41 72 67 2c 20 30 29 3b 0a 20 20 28 76 6f  azArg, 0);.  (vo
25aab 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
25aac 4f 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 69 6e  On(db);.  if( in
25aad 69 74 44 61 74 61 2e 72 63 20 29 7b 0a 20 20 20  itData.rc ){.   
25aae 20 72 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72   rc = initData.r
25aaf 63 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  c;.    goto erro
25ab0 72 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 54 61  r_out;.  }.  pTa
25ab1 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
25ab2 61 62 6c 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  able(db, zMaster
25ab3 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44  Name, db->aDb[iD
25ab4 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b].zName);.  if(
25ab5 20 70 54 61 62 20 29 7b 0a 20 20 20 20 70 54 61   pTab ){.    pTa
25ab6 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54  b->tabFlags |= T
25ab7 46 5f 52 65 61 64 6f 6e 6c 79 3b 0a 20 20 7d 0a  F_Readonly;.  }.
25ab8 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 63  .  /* Create a c
25ab9 75 72 73 6f 72 20 74 6f 20 68 6f 6c 64 20 74 68  ursor to hold th
25aba 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 6e 0a  e database open.
25abb 20 20 2a 2f 0a 20 20 70 44 62 20 3d 20 26 64 62    */.  pDb = &db
25abc 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66  ->aDb[iDb];.  if
25abd 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b  ( pDb->pBt==0 ){
25abe 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  .    if( !OMIT_T
25abf 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20  EMPDB && iDb==1 
25ac0 29 7b 0a 20 20 20 20 20 20 44 62 53 65 74 50 72  ){.      DbSetPr
25ac1 6f 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42  operty(db, 1, DB
25ac2 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a  _SchemaLoaded);.
25ac3 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
25ac4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
25ac5 20 20 63 75 72 4d 61 69 6e 20 3d 20 73 71 6c 69    curMain = sqli
25ac6 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71  te3MallocZero(sq
25ac7 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
25ac8 53 69 7a 65 28 29 29 3b 0a 20 20 69 66 28 20 21  Size());.  if( !
25ac9 63 75 72 4d 61 69 6e 20 29 7b 0a 20 20 20 20 72  curMain ){.    r
25aca 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
25acb 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ;.    goto error
25acc 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  _out;.  }.  sqli
25acd 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 44  te3BtreeEnter(pD
25ace 62 2d 3e 70 42 74 29 3b 0a 20 20 72 63 20 3d 20  b->pBt);.  rc = 
25acf 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
25ad0 6f 72 28 70 44 62 2d 3e 70 42 74 2c 20 4d 41 53  or(pDb->pBt, MAS
25ad1 54 45 52 5f 52 4f 4f 54 2c 20 30 2c 20 30 2c 20  TER_ROOT, 0, 0, 
25ad2 63 75 72 4d 61 69 6e 29 3b 0a 20 20 69 66 28 20  curMain);.  if( 
25ad3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc!=SQLITE_OK &&
25ad4 20 72 63 21 3d 53 51 4c 49 54 45 5f 45 4d 50 54   rc!=SQLITE_EMPT
25ad5 59 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  Y ){.    sqlite3
25ad6 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d  SetString(pzErrM
25ad7 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
25ad8 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29  lite3ErrStr(rc))
25ad9 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f  ;.    goto inito
25ada 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  ne_error_out;.  
25adb 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20  }..  /* Get the 
25adc 64 61 74 61 62 61 73 65 20 6d 65 74 61 20 69 6e  database meta in
25add 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a  formation..  **.
25ade 20 20 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65 73    ** Meta values
25adf 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a   are as follows:
25ae0 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30 5d  .  **    meta[0]
25ae1 20 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65     Schema cookie
25ae2 2e 20 20 43 68 61 6e 67 65 73 20 77 69 74 68 20  .  Changes with 
25ae3 65 61 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e  each schema chan
25ae4 67 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  ge..  **    meta
25ae5 5b 31 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d 61  [1]   File forma
25ae6 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65  t of schema laye
25ae7 72 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  r..  **    meta[
25ae8 32 5d 20 20 20 53 69 7a 65 20 6f 66 20 74 68 65  2]   Size of the
25ae9 20 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 2a   page cache..  *
25aea 2a 20 20 20 20 6d 65 74 61 5b 33 5d 20 20 20 55  *    meta[3]   U
25aeb 73 65 20 66 72 65 65 6c 69 73 74 20 69 66 20 30  se freelist if 0
25aec 2e 20 20 41 75 74 6f 76 61 63 75 75 6d 20 69 66  .  Autovacuum if
25aed 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65   greater than ze
25aee 72 6f 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  ro..  **    meta
25aef 5b 34 5d 20 20 20 44 62 20 74 65 78 74 20 65 6e  [4]   Db text en
25af0 63 6f 64 69 6e 67 2e 20 31 3a 55 54 46 2d 38 20  coding. 1:UTF-8 
25af1 32 3a 55 54 46 2d 31 36 4c 45 20 33 3a 55 54 46  2:UTF-16LE 3:UTF
25af2 2d 31 36 42 45 0a 20 20 2a 2a 20 20 20 20 6d 65  -16BE.  **    me
25af3 74 61 5b 35 5d 20 20 20 54 68 65 20 75 73 65 72  ta[5]   The user
25af4 20 63 6f 6f 6b 69 65 2e 20 55 73 65 64 20 62 79   cookie. Used by
25af5 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
25af6 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 36  ..  **    meta[6
25af7 5d 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c 2d  ]   Incremental-
25af8 76 61 63 75 75 6d 20 66 6c 61 67 2e 0a 20 20 2a  vacuum flag..  *
25af9 2a 20 20 20 20 6d 65 74 61 5b 37 5d 0a 20 20 2a  *    meta[7].  *
25afa 2a 20 20 20 20 6d 65 74 61 5b 38 5d 0a 20 20 2a  *    meta[8].  *
25afb 2a 20 20 20 20 6d 65 74 61 5b 39 5d 0a 20 20 2a  *    meta[9].  *
25afc 2a 0a 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65  *.  ** Note: The
25afd 20 23 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45   #defined SQLITE
25afe 5f 55 54 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e  _UTF* symbols in
25aff 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72   sqliteInt.h cor
25b00 72 65 73 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a 20  respond to.  ** 
25b01 74 68 65 20 70 6f 73 73 69 62 6c 65 20 76 61 6c  the possible val
25b02 75 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0a  ues of meta[4]..
25b03 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53    */.  if( rc==S
25b04 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
25b05 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
25b06 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
25b07 6d 65 74 61 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  meta); i++){.   
25b08 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
25b09 74 72 65 65 47 65 74 4d 65 74 61 28 70 44 62 2d  treeGetMeta(pDb-
25b0a 3e 70 42 74 2c 20 69 2b 31 2c 20 28 75 33 32 20  >pBt, i+1, (u32 
25b0b 2a 29 26 6d 65 74 61 5b 69 5d 29 3b 0a 20 20 20  *)&meta[i]);.   
25b0c 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
25b0d 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
25b0e 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
25b0f 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
25b10 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
25b11 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f        goto inito
25b12 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  ne_error_out;.  
25b13 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
25b14 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  lse{.    memset(
25b15 6d 65 74 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  meta, 0, sizeof(
25b16 6d 65 74 61 29 29 3b 0a 20 20 7d 0a 20 20 70 44  meta));.  }.  pD
25b17 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65  b->pSchema->sche
25b18 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 6d 65 74 61  ma_cookie = meta
25b19 5b 30 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 70  [0];..  /* If op
25b1a 65 6e 69 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70 74  ening a non-empt
25b1b 79 20 64 61 74 61 62 61 73 65 2c 20 63 68 65 63  y database, chec
25b1c 6b 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64  k the text encod
25b1d 69 6e 67 2e 20 46 6f 72 20 74 68 65 0a 20 20 2a  ing. For the.  *
25b1e 2a 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c  * main database,
25b1f 20 73 65 74 20 73 71 6c 69 74 65 33 2e 65 6e 63   set sqlite3.enc
25b20 20 74 6f 20 74 68 65 20 65 6e 63 6f 64 69 6e 67   to the encoding
25b21 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74   of the main dat
25b22 61 62 61 73 65 2e 0a 20 20 2a 2a 20 46 6f 72 20  abase..  ** For 
25b23 61 6e 20 61 74 74 61 63 68 65 64 20 64 62 2c 20  an attached db, 
25b24 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69  it is an error i
25b25 66 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69  f the encoding i
25b26 73 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 0a 20  s not the same. 
25b27 20 2a 2a 20 61 73 20 73 71 6c 69 74 65 33 2e 65   ** as sqlite3.e
25b28 6e 63 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d  nc..  */.  if( m
25b29 65 74 61 5b 34 5d 20 29 7b 20 20 2f 2a 20 74 65  eta[4] ){  /* te
25b2a 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  xt encoding */. 
25b2b 20 20 20 69 66 28 20 69 44 62 3d 3d 30 20 29 7b     if( iDb==0 ){
25b2c 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6f 70 65  .      /* If ope
25b2d 6e 69 6e 67 20 74 68 65 20 6d 61 69 6e 20 64 61  ning the main da
25b2e 74 61 62 61 73 65 2c 20 73 65 74 20 45 4e 43 28  tabase, set ENC(
25b2f 64 62 29 2e 20 2a 2f 0a 20 20 20 20 20 20 45 4e  db). */.      EN
25b30 43 28 64 62 29 20 3d 20 28 75 38 29 6d 65 74 61  C(db) = (u8)meta
25b31 5b 34 5d 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70  [4];.      db->p
25b32 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  DfltColl = sqlit
25b33 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
25b34 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22  , SQLITE_UTF8, "
25b35 42 49 4e 41 52 59 22 2c 20 36 2c 20 30 29 3b 0a  BINARY", 6, 0);.
25b36 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
25b37 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 61   /* If opening a
25b38 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  n attached datab
25b39 61 73 65 2c 20 74 68 65 20 65 6e 63 6f 64 69 6e  ase, the encodin
25b3a 67 20 6d 75 63 68 20 6d 61 74 63 68 20 45 4e 43  g much match ENC
25b3b 28 64 62 29 20 2a 2f 0a 20 20 20 20 20 20 69 66  (db) */.      if
25b3c 28 20 6d 65 74 61 5b 34 5d 21 3d 45 4e 43 28 64  ( meta[4]!=ENC(d
25b3d 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  b) ){.        sq
25b3e 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70  lite3SetString(p
25b3f 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 61 74  zErrMsg, db, "at
25b40 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
25b41 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73 61   must use the sa
25b42 6d 65 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  me".            
25b43 22 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  " text encoding 
25b44 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  as main database
25b45 22 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ");.        rc =
25b46 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
25b47 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 69 74         goto init
25b48 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20  one_error_out;. 
25b49 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
25b4a 65 6c 73 65 7b 0a 20 20 20 20 44 62 53 65 74 50  else{.    DbSetP
25b4b 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c  roperty(db, iDb,
25b4c 20 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20 7d 0a   DB_Empty);.  }.
25b4d 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e    pDb->pSchema->
25b4e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 0a  enc = ENC(db);..
25b4f 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65    if( pDb->pSche
25b50 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 3d 3d  ma->cache_size==
25b51 30 20 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20  0 ){.    size = 
25b52 6d 65 74 61 5b 32 5d 3b 0a 20 20 20 20 69 66 28  meta[2];.    if(
25b53 20 73 69 7a 65 3d 3d 30 20 29 7b 20 73 69 7a 65   size==0 ){ size
25b54 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   = SQLITE_DEFAUL
25b55 54 5f 43 41 43 48 45 5f 53 49 5a 45 3b 20 7d 0a  T_CACHE_SIZE; }.
25b56 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29      if( size<0 )
25b57 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20   size = -size;. 
25b58 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d     pDb->pSchema-
25b59 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73 69  >cache_size = si
25b5a 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  ze;.    sqlite3B
25b5b 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65  treeSetCacheSize
25b5c 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e  (pDb->pBt, pDb->
25b5d 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
25b5e 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a  ize);.  }..  /*.
25b5f 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74    ** file_format
25b60 3d 3d 31 20 20 20 20 56 65 72 73 69 6f 6e 20 33  ==1    Version 3
25b61 2e 30 2e 30 2e 0a 20 20 2a 2a 20 66 69 6c 65 5f  .0.0..  ** file_
25b62 66 6f 72 6d 61 74 3d 3d 32 20 20 20 20 56 65 72  format==2    Ver
25b63 73 69 6f 6e 20 33 2e 31 2e 33 2e 20 20 2f 2f 20  sion 3.1.3.  // 
25b64 41 4c 54 45 52 20 54 41 42 4c 45 20 41 44 44 20  ALTER TABLE ADD 
25b65 43 4f 4c 55 4d 4e 0a 20 20 2a 2a 20 66 69 6c 65  COLUMN.  ** file
25b66 5f 66 6f 72 6d 61 74 3d 3d 33 20 20 20 20 56 65  _format==3    Ve
25b67 72 73 69 6f 6e 20 33 2e 31 2e 34 2e 20 20 2f 2f  rsion 3.1.4.  //
25b68 20 64 69 74 74 6f 20 62 75 74 20 77 69 74 68 20   ditto but with 
25b69 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74  non-NULL default
25b6a 73 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d  s.  ** file_form
25b6b 61 74 3d 3d 34 20 20 20 20 56 65 72 73 69 6f 6e  at==4    Version
25b6c 20 33 2e 33 2e 30 2e 20 20 2f 2f 20 44 45 53 43   3.3.0.  // DESC
25b6d 20 69 6e 64 69 63 65 73 2e 20 20 42 6f 6f 6c 65   indices.  Boole
25b6e 61 6e 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a  an constants.  *
25b6f 2f 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  /.  pDb->pSchema
25b70 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20  ->file_format = 
25b71 28 75 38 29 6d 65 74 61 5b 31 5d 3b 0a 20 20 69  (u8)meta[1];.  i
25b72 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  f( pDb->pSchema-
25b73 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 30 20  >file_format==0 
25b74 29 7b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68  ){.    pDb->pSch
25b75 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74  ema->file_format
25b76 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 1;.  }.  if( 
25b77 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
25b78 6c 65 5f 66 6f 72 6d 61 74 3e 53 51 4c 49 54 45  le_format>SQLITE
25b79 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  _MAX_FILE_FORMAT
25b7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
25b7b 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73  etString(pzErrMs
25b7c 67 2c 20 64 62 2c 20 22 75 6e 73 75 70 70 6f 72  g, db, "unsuppor
25b7d 74 65 64 20 66 69 6c 65 20 66 6f 72 6d 61 74 22  ted file format"
25b7e 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
25b7f 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f  TE_ERROR;.    go
25b80 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72  to initone_error
25b81 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  _out;.  }..  /* 
25b82 54 69 63 6b 65 74 20 23 32 38 30 34 3a 20 20 57  Ticket #2804:  W
25b83 68 65 6e 20 77 65 20 6f 70 65 6e 20 61 20 64 61  hen we open a da
25b84 74 61 62 61 73 65 20 69 6e 20 74 68 65 20 6e 65  tabase in the ne
25b85 77 65 72 20 66 69 6c 65 20 66 6f 72 6d 61 74 2c  wer file format,
25b86 0a 20 20 2a 2a 20 63 6c 65 61 72 20 74 68 65 20  .  ** clear the 
25b87 6c 65 67 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d  legacy_file_form
25b88 61 74 20 70 72 61 67 6d 61 20 66 6c 61 67 20 73  at pragma flag s
25b89 6f 20 74 68 61 74 20 61 20 56 41 43 55 55 4d 20  o that a VACUUM 
25b8a 77 69 6c 6c 0a 20 20 2a 2a 20 6e 6f 74 20 64 6f  will.  ** not do
25b8b 77 6e 67 72 61 64 65 20 74 68 65 20 64 61 74 61  wngrade the data
25b8c 62 61 73 65 20 61 6e 64 20 74 68 75 73 20 69 6e  base and thus in
25b8d 76 61 6c 69 64 61 74 65 20 61 6e 79 20 64 65 73  validate any des
25b8e 63 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 69 6e 64  cending.  ** ind
25b8f 69 63 65 73 20 74 68 61 74 20 74 68 65 20 75 73  ices that the us
25b90 65 72 20 6d 69 67 68 74 20 68 61 76 65 20 63 72  er might have cr
25b91 65 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  eated..  */.  if
25b92 28 20 69 44 62 3d 3d 30 20 26 26 20 6d 65 74 61  ( iDb==0 && meta
25b93 5b 31 5d 3e 3d 34 20 29 7b 0a 20 20 20 20 64 62  [1]>=4 ){.    db
25b94 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49  ->flags &= ~SQLI
25b95 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74  TE_LegacyFileFmt
25b96 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64  ;.  }..  /* Read
25b97 20 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f   the schema info
25b98 72 6d 61 74 69 6f 6e 20 6f 75 74 20 6f 66 20 74  rmation out of t
25b99 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73  he schema tables
25b9a 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
25b9b 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b  db->init.busy );
25b9c 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
25b9d 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20 20 20 2f  E_EMPTY ){.    /
25b9e 2a 20 46 6f 72 20 61 6e 20 65 6d 70 74 79 20 64  * For an empty d
25b9f 61 74 61 62 61 73 65 2c 20 74 68 65 72 65 20 69  atabase, there i
25ba0 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 72 65 61  s nothing to rea
25ba1 64 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51  d */.    rc = SQ
25ba2 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
25ba3 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c  {.    char *zSql
25ba4 3b 0a 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c  ;.    zSql = sql
25ba5 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
25ba6 0a 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54  .        "SELECT
25ba7 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c   name, rootpage,
25ba8 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25   sql FROM '%q'.%
25ba9 73 22 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e  s",.        db->
25baa 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
25bab 7a 4d 61 73 74 65 72 4e 61 6d 65 29 3b 0a 20 20  zMasterName);.  
25bac 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
25bad 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 23 69  afetyOff(db);.#i
25bae 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
25baf 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
25bb0 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20      {.      int 
25bb1 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69  (*xAuth)(void*,i
25bb2 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  nt,const char*,c
25bb3 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
25bb4 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
25bb5 72 2a 29 3b 0a 20 20 20 20 20 20 78 41 75 74 68  r*);.      xAuth
25bb6 20 3d 20 64 62 2d 3e 78 41 75 74 68 3b 0a 20 20   = db->xAuth;.  
25bb7 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20      db->xAuth = 
25bb8 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  0;.#endif.      
25bb9 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
25bba 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69  c(db, zSql, sqli
25bbb 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c  te3InitCallback,
25bbc 20 26 69 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a   &initData, 0);.
25bbd 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
25bbe 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
25bbf 4e 0a 20 20 20 20 20 20 64 62 2d 3e 78 41 75 74  N.      db->xAut
25bc0 68 20 3d 20 78 41 75 74 68 3b 0a 20 20 20 20 7d  h = xAuth;.    }
25bc1 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
25bc2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc==SQLITE_OK ) 
25bc3 72 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63  rc = initData.rc
25bc4 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
25bc5 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
25bc6 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
25bc7 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 23 69  ee(db, zSql);.#i
25bc8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
25bc9 54 5f 41 4e 41 4c 59 5a 45 0a 20 20 20 20 69 66  T_ANALYZE.    if
25bca 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
25bcb 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
25bcc 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c  AnalysisLoad(db,
25bcd 20 69 44 62 29 3b 0a 20 20 20 20 7d 0a 23 65 6e   iDb);.    }.#en
25bce 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  dif.  }.  if( db
25bcf 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
25bd0 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
25bd1 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 73 71 6c  E_NOMEM;.    sql
25bd2 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
25bd3 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
25bd4 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
25bd5 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 64 62 2d 3e  LITE_OK || (db->
25bd6 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 63  flags&SQLITE_Rec
25bd7 6f 76 65 72 79 4d 6f 64 65 29 29 7b 0a 20 20 20  overyMode)){.   
25bd8 20 2f 2a 20 42 6c 61 63 6b 20 6d 61 67 69 63 3a   /* Black magic:
25bd9 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 52   If the SQLITE_R
25bda 65 63 6f 76 65 72 79 4d 6f 64 65 20 66 6c 61 67  ecoveryMode flag
25bdb 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 63 6f   is set, then co
25bdc 6e 73 69 64 65 72 0a 20 20 20 20 2a 2a 20 74 68  nsider.    ** th
25bdd 65 20 73 63 68 65 6d 61 20 6c 6f 61 64 65 64 2c  e schema loaded,
25bde 20 65 76 65 6e 20 69 66 20 65 72 72 6f 72 73 20   even if errors 
25bdf 6f 63 63 75 72 65 64 2e 20 49 6e 20 74 68 69 73  occured. In this
25be0 20 73 69 74 75 61 74 69 6f 6e 20 74 68 65 20 0a   situation the .
25be1 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 73      ** current s
25be2 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
25be3 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20   operation will 
25be4 66 61 69 6c 2c 20 62 75 74 20 74 68 65 20 66 6f  fail, but the fo
25be5 6c 6c 6f 77 69 6e 67 20 6f 6e 65 0a 20 20 20 20  llowing one.    
25be6 2a 2a 20 77 69 6c 6c 20 61 74 74 65 6d 70 74 20  ** will attempt 
25be7 74 6f 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 73  to compile the s
25be8 75 70 70 6c 69 65 64 20 73 74 61 74 65 6d 65 6e  upplied statemen
25be9 74 20 61 67 61 69 6e 73 74 20 77 68 61 74 65 76  t against whatev
25bea 65 72 20 73 75 62 73 65 74 0a 20 20 20 20 2a 2a  er subset.    **
25beb 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 77   of the schema w
25bec 61 73 20 6c 6f 61 64 65 64 20 62 65 66 6f 72 65  as loaded before
25bed 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72   the error occur
25bee 65 64 2e 20 54 68 65 20 70 72 69 6d 61 72 79 0a  ed. The primary.
25bef 20 20 20 20 2a 2a 20 70 75 72 70 6f 73 65 20 6f      ** purpose o
25bf0 66 20 74 68 69 73 20 69 73 20 74 6f 20 61 6c 6c  f this is to all
25bf1 6f 77 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ow access to the
25bf2 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
25bf3 61 62 6c 65 0a 20 20 20 20 2a 2a 20 65 76 65 6e  able.    ** even
25bf4 20 77 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e   when its conten
25bf5 74 73 20 68 61 76 65 20 62 65 65 6e 20 63 6f 72  ts have been cor
25bf6 72 75 70 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  rupted..    */. 
25bf7 20 20 20 44 62 53 65 74 50 72 6f 70 65 72 74 79     DbSetProperty
25bf8 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 53 63 68  (db, iDb, DB_Sch
25bf9 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20 20  emaLoaded);.    
25bfa 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
25bfb 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68    }..  /* Jump h
25bfc 65 72 65 20 66 6f 72 20 61 6e 20 65 72 72 6f 72  ere for an error
25bfd 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 66 74   that occurs aft
25bfe 65 72 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  er successfully 
25bff 61 6c 6c 6f 63 61 74 69 6e 67 0a 20 20 2a 2a 20  allocating.  ** 
25c00 63 75 72 4d 61 69 6e 20 61 6e 64 20 63 61 6c 6c  curMain and call
25c01 69 6e 67 20 73 71 6c 69 74 65 33 42 74 72 65 65  ing sqlite3Btree
25c02 45 6e 74 65 72 28 29 2e 20 46 6f 72 20 61 6e 20  Enter(). For an 
25c03 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72  error that occur
25c04 73 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68  s.  ** before th
25c05 61 74 20 70 6f 69 6e 74 2c 20 6a 75 6d 70 20 74  at point, jump t
25c06 6f 20 65 72 72 6f 72 5f 6f 75 74 2e 0a 20 20 2a  o error_out..  *
25c07 2f 0a 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f  /.initone_error_
25c08 6f 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 42 74  out:.  sqlite3Bt
25c09 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 63  reeCloseCursor(c
25c0a 75 72 4d 61 69 6e 29 3b 0a 20 20 73 71 6c 69 74  urMain);.  sqlit
25c0b 65 33 5f 66 72 65 65 28 63 75 72 4d 61 69 6e 29  e3_free(curMain)
25c0c 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
25c0d 4c 65 61 76 65 28 70 44 62 2d 3e 70 42 74 29 3b  Leave(pDb->pBt);
25c0e 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69  ..error_out:.  i
25c0f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
25c10 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  MEM || rc==SQLIT
25c11 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b  E_IOERR_NOMEM ){
25c12 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
25c13 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  ailed = 1;.  }. 
25c14 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
25c15 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
25c16 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  all database fil
25c17 65 73 20 2d 20 74 68 65 20 6d 61 69 6e 20 64 61  es - the main da
25c18 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
25c19 20 66 69 6c 65 0a 2a 2a 20 75 73 65 64 20 74 6f   file.** used to
25c1a 20 73 74 6f 72 65 20 74 65 6d 70 6f 72 61 72 79   store temporary
25c1b 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 61 6e 79   tables, and any
25c1c 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 61 74 61   additional data
25c1d 62 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20 63 72  base files.** cr
25c1e 65 61 74 65 64 20 75 73 69 6e 67 20 41 54 54 41  eated using ATTA
25c1f 43 48 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  CH statements.  
25c20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73  Return a success
25c21 20 63 6f 64 65 2e 20 20 49 66 20 61 6e 0a 2a 2a   code.  If an.**
25c22 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77   error occurs, w
25c23 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rite an error me
25c24 73 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a 45 72  ssage into *pzEr
25c25 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  rMsg..**.** Afte
25c26 72 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20  r a database is 
25c27 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65  initialized, the
25c28 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
25c29 20 62 69 74 20 69 73 20 73 65 74 0a 2a 2a 20 62   bit is set.** b
25c2a 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65  it is set in the
25c2b 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 20   flags field of 
25c2c 74 68 65 20 44 62 20 73 74 72 75 63 74 75 72 65  the Db structure
25c2d 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  . If the databas
25c2e 65 0a 2a 2a 20 66 69 6c 65 20 77 61 73 20 6f 66  e.** file was of
25c2f 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c 20 74 68   zero-length, th
25c30 65 6e 20 74 68 65 20 44 42 5f 45 6d 70 74 79 20  en the DB_Empty 
25c31 66 6c 61 67 20 69 73 20 61 6c 73 6f 20 73 65 74  flag is also set
25c32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
25c33 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
25c34 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  nit(sqlite3 *db,
25c35 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
25c36 29 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 3b 0a  ){.  int i, rc;.
25c37 20 20 69 6e 74 20 63 6f 6d 6d 69 74 5f 69 6e 74    int commit_int
25c38 65 72 6e 61 6c 20 3d 20 21 28 64 62 2d 3e 66 6c  ernal = !(db->fl
25c39 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72  ags&SQLITE_Inter
25c3a 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 0a 20 20  nChanges);.  .  
25c3b 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
25c3c 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
25c3d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64  utex) );.  if( d
25c3e 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20 72  b->init.busy ) r
25c3f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
25c40 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  .  rc = SQLITE_O
25c41 4b 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75  K;.  db->init.bu
25c42 73 79 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69 3d  sy = 1;.  for(i=
25c43 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
25c44 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69   && i<db->nDb; i
25c45 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 44 62 48  ++){.    if( DbH
25c46 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69  asProperty(db, i
25c47 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
25c48 64 29 20 7c 7c 20 69 3d 3d 31 20 29 20 63 6f 6e  d) || i==1 ) con
25c49 74 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d 20  tinue;.    rc = 
25c4a 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28 64  sqlite3InitOne(d
25c4b 62 2c 20 69 2c 20 70 7a 45 72 72 4d 73 67 29 3b  b, i, pzErrMsg);
25c4c 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
25c4d 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65       sqlite3Rese
25c4e 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
25c4f 64 62 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20  db, i);.    }.  
25c50 7d 0a 0a 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c  }..  /* Once all
25c51 20 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 62   the other datab
25c52 61 73 65 73 20 68 61 76 65 20 62 65 65 6e 20 69  ases have been i
25c53 6e 69 74 69 61 6c 69 73 65 64 2c 20 6c 6f 61 64  nitialised, load
25c54 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a 2a   the schema.  **
25c55 20 66 6f 72 20 74 68 65 20 54 45 4d 50 20 64 61   for the TEMP da
25c56 74 61 62 61 73 65 2e 20 54 68 69 73 20 69 73 20  tabase. This is 
25c57 6c 6f 61 64 65 64 20 6c 61 73 74 2c 20 61 73 20  loaded last, as 
25c58 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73  the TEMP databas
25c59 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 6d 61  e.  ** schema ma
25c5a 79 20 63 6f 6e 74 61 69 6e 20 72 65 66 65 72 65  y contain refere
25c5b 6e 63 65 73 20 74 6f 20 6f 62 6a 65 63 74 73 20  nces to objects 
25c5c 69 6e 20 6f 74 68 65 72 20 64 61 74 61 62 61 73  in other databas
25c5d 65 73 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  es..  */.#ifndef
25c5e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d   SQLITE_OMIT_TEM
25c5f 50 44 42 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  PDB.  if( rc==SQ
25c60 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 6e  LITE_OK && db->n
25c61 44 62 3e 31 20 26 26 20 21 44 62 48 61 73 50 72  Db>1 && !DbHasPr
25c62 6f 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42  operty(db, 1, DB
25c63 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29  _SchemaLoaded) )
25c64 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
25c65 65 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 31 2c  e3InitOne(db, 1,
25c66 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20   pzErrMsg);.    
25c67 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
25c68 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
25c69 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 31  rnalSchema(db, 1
25c6a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  );.    }.  }.#en
25c6b 64 69 66 0a 0a 20 20 64 62 2d 3e 69 6e 69 74 2e  dif..  db->init.
25c6c 62 75 73 79 20 3d 20 30 3b 0a 20 20 69 66 28 20  busy = 0;.  if( 
25c6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
25c6e 20 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c   commit_internal
25c6f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43   ){.    sqlite3C
25c70 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61  ommitInternalCha
25c71 6e 67 65 73 28 64 62 29 3b 0a 20 20 7d 0a 0a 20  nges(db);.  }.. 
25c72 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a   return rc; .}..
25c73 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
25c74 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  ne is a no-op if
25c75 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
25c76 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20  hema is already 
25c77 69 6e 69 74 69 61 6c 69 73 65 64 2e 0a 2a 2a 20  initialised..** 
25c78 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73  Otherwise, the s
25c79 63 68 65 6d 61 20 69 73 20 6c 6f 61 64 65 64 2e  chema is loaded.
25c7a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   An error code i
25c7b 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
25c7c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
25c7d 74 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  t sqlite3ReadSch
25c7e 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73  ema(Parse *pPars
25c7f 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
25c80 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69  QLITE_OK;.  sqli
25c81 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
25c82 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20  ->db;.  assert( 
25c83 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
25c84 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
25c85 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74  .  if( !db->init
25c86 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 72 63 20  .busy ){.    rc 
25c87 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62  = sqlite3Init(db
25c88 2c 20 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  , &pParse->zErrM
25c89 73 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  sg);.  }.  if( r
25c8a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
25c8b 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
25c8c 20 72 63 3b 0a 20 20 20 20 70 50 61 72 73 65 2d   rc;.    pParse-
25c8d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72  >nErr++;.  }.  r
25c8e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
25c8f 0a 2a 2a 20 43 68 65 63 6b 20 73 63 68 65 6d 61  .** Check schema
25c90 20 63 6f 6f 6b 69 65 73 20 69 6e 20 61 6c 6c 20   cookies in all 
25c91 64 61 74 61 62 61 73 65 73 2e 20 20 49 66 20 61  databases.  If a
25c92 6e 79 20 63 6f 6f 6b 69 65 20 69 73 20 6f 75 74  ny cookie is out
25c93 0a 2a 2a 20 6f 66 20 64 61 74 65 2c 20 72 65 74  .** of date, ret
25c94 75 72 6e 20 30 2e 20 20 49 66 20 61 6c 6c 20 73  urn 0.  If all s
25c95 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 61 72  chema cookies ar
25c96 65 20 63 75 72 72 65 6e 74 2c 20 72 65 74 75 72  e current, retur
25c97 6e 20 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  n 1..*/.static i
25c98 6e 74 20 73 63 68 65 6d 61 49 73 56 61 6c 69 64  nt schemaIsValid
25c99 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
25c9a 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20   int iDb;.  int 
25c9b 72 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  rc;.  BtCursor *
25c9c 63 75 72 54 65 6d 70 3b 0a 20 20 69 6e 74 20 63  curTemp;.  int c
25c9d 6f 6f 6b 69 65 3b 0a 20 20 69 6e 74 20 61 6c 6c  ookie;.  int all
25c9e 4f 6b 20 3d 20 31 3b 0a 0a 20 20 63 75 72 54 65  Ok = 1;..  curTe
25c9f 6d 70 20 3d 20 28 42 74 43 75 72 73 6f 72 20 2a  mp = (BtCursor *
25ca0 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73  )sqlite3Malloc(s
25ca1 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
25ca2 72 53 69 7a 65 28 29 29 3b 0a 20 20 69 66 28 20  rSize());.  if( 
25ca3 63 75 72 54 65 6d 70 20 29 7b 0a 20 20 20 20 61  curTemp ){.    a
25ca4 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
25ca5 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
25ca6 74 65 78 29 20 29 3b 0a 20 20 20 20 66 6f 72 28  tex) );.    for(
25ca7 69 44 62 3d 30 3b 20 61 6c 6c 4f 6b 20 26 26 20  iDb=0; allOk && 
25ca8 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62  iDb<db->nDb; iDb
25ca9 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65  ++){.      Btree
25caa 20 2a 70 42 74 3b 0a 20 20 20 20 20 20 70 42 74   *pBt;.      pBt
25cab 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
25cac 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  pBt;.      if( p
25cad 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  Bt==0 ) continue
25cae 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 63  ;.      memset(c
25caf 75 72 54 65 6d 70 2c 20 30 2c 20 73 71 6c 69 74  urTemp, 0, sqlit
25cb0 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a  e3BtreeCursorSiz
25cb1 65 28 29 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  e());.      rc =
25cb2 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
25cb3 73 6f 72 28 70 42 74 2c 20 4d 41 53 54 45 52 5f  sor(pBt, MASTER_
25cb4 52 4f 4f 54 2c 20 30 2c 20 30 2c 20 63 75 72 54  ROOT, 0, 0, curT
25cb5 65 6d 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20  emp);.      if( 
25cb6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
25cb7 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
25cb8 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
25cb9 61 28 70 42 74 2c 20 31 2c 20 28 75 33 32 20 2a  a(pBt, 1, (u32 *
25cba 29 26 63 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20  )&cookie);.     
25cbb 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
25cbc 45 5f 4f 4b 20 26 26 20 63 6f 6f 6b 69 65 21 3d  E_OK && cookie!=
25cbd 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
25cbe 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
25cbf 6b 69 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  kie ){.         
25cc0 20 61 6c 6c 4f 6b 20 3d 20 30 3b 0a 20 20 20 20   allOk = 0;.    
25cc1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
25cc2 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43  lite3BtreeCloseC
25cc3 75 72 73 6f 72 28 63 75 72 54 65 6d 70 29 3b 0a  ursor(curTemp);.
25cc4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
25cc5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
25cc6 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45  EM || rc==SQLITE
25cc7 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
25cc8 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c          db->mall
25cc9 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
25cca 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
25ccb 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 75 72  sqlite3_free(cur
25ccc 54 65 6d 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Temp);.  }else{.
25ccd 20 20 20 20 61 6c 6c 4f 6b 20 3d 20 30 3b 0a 20      allOk = 0;. 
25cce 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
25ccf 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  led = 1;.  }..  
25cd0 72 65 74 75 72 6e 20 61 6c 6c 4f 6b 3b 0a 7d 0a  return allOk;.}.
25cd1 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
25cd2 20 73 63 68 65 6d 61 20 70 6f 69 6e 74 65 72 20   schema pointer 
25cd3 69 6e 74 6f 20 74 68 65 20 69 44 62 20 69 6e 64  into the iDb ind
25cd4 65 78 20 74 68 61 74 20 69 6e 64 69 63 61 74 65  ex that indicate
25cd5 73 0a 2a 2a 20 77 68 69 63 68 20 64 61 74 61 62  s.** which datab
25cd6 61 73 65 20 66 69 6c 65 20 69 6e 20 64 62 2d 3e  ase file in db->
25cd7 61 44 62 5b 5d 20 74 68 65 20 73 63 68 65 6d 61  aDb[] the schema
25cd8 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a   refers to..**.*
25cd9 2a 20 49 66 20 74 68 65 20 73 61 6d 65 20 64 61  * If the same da
25cda 74 61 62 61 73 65 20 69 73 20 61 74 74 61 63 68  tabase is attach
25cdb 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ed more than onc
25cdc 65 2c 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  e, the first.** 
25cdd 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
25cde 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
25cdf 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
25ce0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65   int sqlite3Sche
25ce1 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65  maToIndex(sqlite
25ce2 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 70  3 *db, Schema *p
25ce3 53 63 68 65 6d 61 29 7b 0a 20 20 69 6e 74 20 69  Schema){.  int i
25ce4 20 3d 20 2d 31 30 30 30 30 30 30 3b 0a 0a 20 20   = -1000000;..  
25ce5 2f 2a 20 49 66 20 70 53 63 68 65 6d 61 20 69 73  /* If pSchema is
25ce6 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65 74 75   NULL, then retu
25ce7 72 6e 20 2d 31 30 30 30 30 30 30 2e 20 54 68 69  rn -1000000. Thi
25ce8 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63  s happens when c
25ce9 6f 64 65 20 69 6e 20 0a 20 20 2a 2a 20 65 78 70  ode in .  ** exp
25cea 72 2e 63 20 69 73 20 74 72 79 69 6e 67 20 74 6f  r.c is trying to
25ceb 20 72 65 73 6f 6c 76 65 20 61 20 72 65 66 65 72   resolve a refer
25cec 65 6e 63 65 20 74 6f 20 61 20 74 72 61 6e 73 69  ence to a transi
25ced 65 6e 74 20 74 61 62 6c 65 20 28 69 2e 65 2e 20  ent table (i.e. 
25cee 6f 6e 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64  one.  ** created
25cef 20 62 79 20 61 20 73 75 62 2d 73 65 6c 65 63 74   by a sub-select
25cf0 29 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ). In this case 
25cf1 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
25cf2 20 6f 66 20 74 68 69 73 20 0a 20 20 2a 2a 20 66   of this .  ** f
25cf3 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e  unction should n
25cf4 65 76 65 72 20 62 65 20 75 73 65 64 2e 0a 20 20  ever be used..  
25cf5 2a 2a 0a 20 20 2a 2a 20 57 65 20 72 65 74 75 72  **.  ** We retur
25cf6 6e 20 2d 31 30 30 30 30 30 30 20 69 6e 73 74 65  n -1000000 inste
25cf7 61 64 20 6f 66 20 74 68 65 20 6d 6f 72 65 20 75  ad of the more u
25cf8 73 75 61 6c 20 2d 31 20 73 69 6d 70 6c 79 20 62  sual -1 simply b
25cf9 65 63 61 75 73 65 20 75 73 69 6e 67 0a 20 20 2a  ecause using.  *
25cfa 2a 20 2d 31 30 30 30 30 30 30 20 61 73 20 74 68  * -1000000 as th
25cfb 65 20 69 6e 63 6f 72 72 65 63 74 20 69 6e 64 65  e incorrect inde
25cfc 78 20 69 6e 74 6f 20 64 62 2d 3e 61 44 62 5b 5d  x into db->aDb[]
25cfd 20 69 73 20 6d 75 63 68 20 0a 20 20 2a 2a 20 6d   is much .  ** m
25cfe 6f 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 63 61  ore likely to ca
25cff 75 73 65 20 61 20 73 65 67 66 61 75 6c 74 20 74  use a segfault t
25d00 68 61 6e 20 2d 31 20 28 6f 66 20 63 6f 75 72 73  han -1 (of cours
25d01 65 20 74 68 65 72 65 20 61 72 65 20 61 73 73 65  e there are asse
25d02 72 74 28 29 0a 20 20 2a 2a 20 73 74 61 74 65 6d  rt().  ** statem
25d03 65 6e 74 73 20 74 6f 6f 2c 20 62 75 74 20 69 74  ents too, but it
25d04 20 6e 65 76 65 72 20 68 75 72 74 73 20 74 6f 20   never hurts to 
25d05 70 6c 61 79 20 74 68 65 20 6f 64 64 73 29 2e 0a  play the odds)..
25d06 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73    */.  assert( s
25d07 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
25d08 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
25d09 20 20 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b    if( pSchema ){
25d0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c  .    for(i=0; AL
25d0b 57 41 59 53 28 69 3c 64 62 2d 3e 6e 44 62 29 3b  WAYS(i<db->nDb);
25d0c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
25d0d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68   db->aDb[i].pSch
25d0e 65 6d 61 3d 3d 70 53 63 68 65 6d 61 20 29 7b 0a  ema==pSchema ){.
25d0f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
25d10 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
25d11 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
25d12 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20   i<db->nDb );.  
25d13 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a  }.  return i;.}.
25d14 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74  ./*.** Compile t
25d15 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  he UTF-8 encoded
25d16 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a   SQL statement z
25d17 53 71 6c 20 69 6e 74 6f 20 61 20 73 74 61 74 65  Sql into a state
25d18 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  ment handle..*/.
25d19 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
25d1a 65 33 50 72 65 70 61 72 65 28 0a 20 20 73 71 6c  e3Prepare(.  sql
25d1b 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
25d1c 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
25d1d 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20  se handle. */.  
25d1e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
25d1f 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46  ,         /* UTF
25d20 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73  -8 encoded SQL s
25d21 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69  tatement. */.  i
25d22 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20  nt nBytes,      
25d23 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
25d24 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79  th of zSql in by
25d25 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61  tes. */.  int sa
25d26 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20  veSqlFlag,      
25d27 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63      /* True to c
25d28 6f 70 79 20 53 51 4c 20 74 65 78 74 20 69 6e 74  opy SQL text int
25d29 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74  o the sqlite3_st
25d2a 6d 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  mt */.  sqlite3_
25d2b 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
25d2c 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e    /* OUT: A poin
25d2d 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61  ter to the prepa
25d2e 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  red statement */
25d2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
25d30 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20  pzTail       /* 
25d31 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73  OUT: End of pars
25d32 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ed string */.){.
25d33 20 20 50 61 72 73 65 20 73 50 61 72 73 65 3b 0a    Parse sParse;.
25d34 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20    char *zErrMsg 
25d35 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  = 0;.  int rc = 
25d36 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
25d37 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   i;..  assert( p
25d38 70 53 74 6d 74 20 29 3b 0a 20 20 2a 70 70 53 74  pStmt );.  *ppSt
25d39 6d 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71  mt = 0;.  if( sq
25d3a 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
25d3b 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
25d3c 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
25d3d 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 64 62   }.  assert( !db
25d3e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
25d3f 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
25d40 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
25d41 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
25d42 2f 2a 20 49 66 20 61 6e 79 20 61 74 74 61 63 68  /* If any attach
25d43 65 64 20 64 61 74 61 62 61 73 65 20 73 63 68 65  ed database sche
25d44 6d 61 73 20 61 72 65 20 6c 6f 63 6b 65 64 2c 20  mas are locked, 
25d45 64 6f 20 6e 6f 74 20 70 72 6f 63 65 65 64 20 77  do not proceed w
25d46 69 74 68 0a 20 20 2a 2a 20 63 6f 6d 70 69 6c 61  ith.  ** compila
25d47 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 72 65  tion. Instead re
25d48 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
25d49 45 44 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a  ED immediately..
25d4a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
25d4b 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 20  i<db->nDb; i++) 
25d4c 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74  {.    Btree *pBt
25d4d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
25d4e 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74 20 29  t;.    if( pBt )
25d4f 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
25d50 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c  ite3BtreeSchemaL
25d51 6f 63 6b 65 64 28 70 42 74 29 3b 0a 20 20 20 20  ocked(pBt);.    
25d52 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
25d53 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
25d54 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  zDb = db->aDb[i]
25d55 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
25d56 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
25d57 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20   SQLITE_LOCKED, 
25d58 22 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61  "database schema
25d59 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c   is locked: %s",
25d5a 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20 28   zDb);.        (
25d5b 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
25d5c 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
25d5d 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
25d5e 33 41 70 69 45 78 69 74 28 64 62 2c 20 53 51 4c  3ApiExit(db, SQL
25d5f 49 54 45 5f 4c 4f 43 4b 45 44 29 3b 0a 20 20 20  ITE_LOCKED);.   
25d60 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
25d61 20 0a 20 20 6d 65 6d 73 65 74 28 26 73 50 61 72   .  memset(&sPar
25d62 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 50  se, 0, sizeof(sP
25d63 61 72 73 65 29 29 3b 0a 20 20 73 50 61 72 73 65  arse));.  sParse
25d64 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20  .db = db;.  if( 
25d65 6e 42 79 74 65 73 3e 3d 30 20 26 26 20 28 6e 42  nBytes>=0 && (nB
25d66 79 74 65 73 3d 3d 30 20 7c 7c 20 7a 53 71 6c 5b  ytes==0 || zSql[
25d67 6e 42 79 74 65 73 2d 31 5d 21 3d 30 29 20 29 7b  nBytes-1]!=0) ){
25d68 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 43  .    char *zSqlC
25d69 6f 70 79 3b 0a 20 20 20 20 69 6e 74 20 6d 78 4c  opy;.    int mxL
25d6a 65 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b  en = db->aLimit[
25d6b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c  SQLITE_LIMIT_SQL
25d6c 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20 69 66  _LENGTH];.    if
25d6d 28 20 6e 42 79 74 65 73 3e 6d 78 4c 65 6e 20 29  ( nBytes>mxLen )
25d6e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
25d6f 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
25d70 54 4f 4f 42 49 47 2c 20 22 73 74 61 74 65 6d 65  TOOBIG, "stateme
25d71 6e 74 20 74 6f 6f 20 6c 6f 6e 67 22 29 3b 0a 20  nt too long");. 
25d72 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
25d73 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
25d74 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71  .      return sq
25d75 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
25d76 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 29 3b   SQLITE_TOOBIG);
25d77 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 71 6c 43  .    }.    zSqlC
25d78 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 44 62 53  opy = sqlite3DbS
25d79 74 72 4e 44 75 70 28 64 62 2c 20 7a 53 71 6c 2c  trNDup(db, zSql,
25d7a 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66   nBytes);.    if
25d7b 28 20 7a 53 71 6c 43 6f 70 79 20 29 7b 0a 20 20  ( zSqlCopy ){.  
25d7c 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61      sqlite3RunPa
25d7d 72 73 65 72 28 26 73 50 61 72 73 65 2c 20 7a 53  rser(&sParse, zS
25d7e 71 6c 43 6f 70 79 2c 20 26 7a 45 72 72 4d 73 67  qlCopy, &zErrMsg
25d7f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
25d80 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 43  DbFree(db, zSqlC
25d81 6f 70 79 29 3b 0a 20 20 20 20 20 20 73 50 61 72  opy);.      sPar
25d82 73 65 2e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  se.zTail = &zSql
25d83 5b 73 50 61 72 73 65 2e 7a 54 61 69 6c 2d 7a 53  [sParse.zTail-zS
25d84 71 6c 43 6f 70 79 5d 3b 0a 20 20 20 20 7d 65 6c  qlCopy];.    }el
25d85 73 65 7b 0a 20 20 20 20 20 20 73 50 61 72 73 65  se{.      sParse
25d86 2e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 6e  .zTail = &zSql[n
25d87 42 79 74 65 73 5d 3b 0a 20 20 20 20 7d 0a 20 20  Bytes];.    }.  
25d88 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
25d89 65 33 52 75 6e 50 61 72 73 65 72 28 26 73 50 61  e3RunParser(&sPa
25d8a 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72  rse, zSql, &zErr
25d8b 4d 73 67 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  Msg);.  }..  if(
25d8c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
25d8d 64 20 29 7b 0a 20 20 20 20 73 50 61 72 73 65 2e  d ){.    sParse.
25d8e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
25d8f 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 50 61  M;.  }.  if( sPa
25d90 72 73 65 2e 72 63 3d 3d 53 51 4c 49 54 45 5f 44  rse.rc==SQLITE_D
25d91 4f 4e 45 20 29 20 73 50 61 72 73 65 2e 72 63 20  ONE ) sParse.rc 
25d92 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
25d93 66 28 20 73 50 61 72 73 65 2e 63 68 65 63 6b 53  f( sParse.checkS
25d94 63 68 65 6d 61 20 26 26 20 21 73 63 68 65 6d 61  chema && !schema
25d95 49 73 56 61 6c 69 64 28 64 62 29 20 29 7b 0a 20  IsValid(db) ){. 
25d96 20 20 20 73 50 61 72 73 65 2e 72 63 20 3d 20 53     sParse.rc = S
25d97 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20  QLITE_SCHEMA;.  
25d98 7d 0a 20 20 69 66 28 20 73 50 61 72 73 65 2e 72  }.  if( sParse.r
25d99 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  c==SQLITE_SCHEMA
25d9a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52   ){.    sqlite3R
25d9b 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
25d9c 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20  ma(db, 0);.  }. 
25d9d 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
25d9e 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 50 61  ailed ){.    sPa
25d9f 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f  rse.rc = SQLITE_
25da0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
25da1 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20 2a   pzTail ){.    *
25da2 70 7a 54 61 69 6c 20 3d 20 73 50 61 72 73 65 2e  pzTail = sParse.
25da3 7a 54 61 69 6c 3b 0a 20 20 7d 0a 20 20 72 63 20  zTail;.  }.  rc 
25da4 3d 20 73 50 61 72 73 65 2e 72 63 3b 0a 0a 23 69  = sParse.rc;..#i
25da5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
25da6 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 20  T_EXPLAIN.  if( 
25da7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
25da8 20 73 50 61 72 73 65 2e 70 56 64 62 65 20 26 26   sParse.pVdbe &&
25da9 20 73 50 61 72 73 65 2e 65 78 70 6c 61 69 6e 20   sParse.explain 
25daa 29 7b 0a 20 20 20 20 69 66 28 20 73 50 61 72 73  ){.    if( sPars
25dab 65 2e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a  e.explain==2 ){.
25dac 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25dad 65 53 65 74 4e 75 6d 43 6f 6c 73 28 73 50 61 72  eSetNumCols(sPar
25dae 73 65 2e 70 56 64 62 65 2c 20 33 29 3b 0a 20 20  se.pVdbe, 3);.  
25daf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
25db0 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65  etColName(sParse
25db1 2e 70 56 64 62 65 2c 20 30 2c 20 43 4f 4c 4e 41  .pVdbe, 0, COLNA
25db2 4d 45 5f 4e 41 4d 45 2c 20 22 6f 72 64 65 72 22  ME_NAME, "order"
25db3 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
25db4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25db5 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50  dbeSetColName(sP
25db6 61 72 73 65 2e 70 56 64 62 65 2c 20 31 2c 20 43  arse.pVdbe, 1, C
25db7 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 72  OLNAME_NAME, "fr
25db8 6f 6d 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  om", SQLITE_STAT
25db9 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
25dba 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
25dbb 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 32  (sParse.pVdbe, 2
25dbc 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
25dbd 22 64 65 74 61 69 6c 22 2c 20 53 51 4c 49 54 45  "detail", SQLITE
25dbe 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 65  _STATIC);.    }e
25dbf 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
25dc0 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
25dc1 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 38  (sParse.pVdbe, 8
25dc2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
25dc3 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73  VdbeSetColName(s
25dc4 50 61 72 73 65 2e 70 56 64 62 65 2c 20 30 2c 20  Parse.pVdbe, 0, 
25dc5 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 61  COLNAME_NAME, "a
25dc6 64 64 72 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ddr", SQLITE_STA
25dc7 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
25dc8 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
25dc9 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20  e(sParse.pVdbe, 
25dca 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
25dcb 20 22 6f 70 63 6f 64 65 22 2c 20 53 51 4c 49 54   "opcode", SQLIT
25dcc 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
25dcd 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
25dce 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56  olName(sParse.pV
25dcf 64 62 65 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f  dbe, 2, COLNAME_
25dd0 4e 41 4d 45 2c 20 22 70 31 22 2c 20 53 51 4c 49  NAME, "p1", SQLI
25dd1 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
25dd2 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
25dd3 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70  ColName(sParse.p
25dd4 56 64 62 65 2c 20 33 2c 20 43 4f 4c 4e 41 4d 45  Vdbe, 3, COLNAME
25dd5 5f 4e 41 4d 45 2c 20 22 70 32 22 2c 20 53 51 4c  _NAME, "p2", SQL
25dd6 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
25dd7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25dd8 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e  tColName(sParse.
25dd9 70 56 64 62 65 2c 20 34 2c 20 43 4f 4c 4e 41 4d  pVdbe, 4, COLNAM
25dda 45 5f 4e 41 4d 45 2c 20 22 70 33 22 2c 20 53 51  E_NAME, "p3", SQ
25ddb 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
25ddc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
25ddd 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65  etColName(sParse
25dde 2e 70 56 64 62 65 2c 20 35 2c 20 43 4f 4c 4e 41  .pVdbe, 5, COLNA
25ddf 4d 45 5f 4e 41 4d 45 2c 20 22 70 34 22 2c 20 53  ME_NAME, "p4", S
25de0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
25de1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25de2 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73  SetColName(sPars
25de3 65 2e 70 56 64 62 65 2c 20 36 2c 20 43 4f 4c 4e  e.pVdbe, 6, COLN
25de4 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 35 22 2c 20  AME_NAME, "p5", 
25de5 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
25de6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25de7 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
25de8 73 65 2e 70 56 64 62 65 2c 20 37 2c 20 43 4f 4c  se.pVdbe, 7, COL
25de9 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 6f 6d 6d  NAME_NAME, "comm
25dea 65 6e 74 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ent", SQLITE_STA
25deb 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TIC);.    }.  }.
25dec 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71  #endif..  if( sq
25ded 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
25dee 62 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  b) ){.    rc = S
25def 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
25df0 7d 0a 0a 20 20 69 66 28 20 73 61 76 65 53 71 6c  }..  if( saveSql
25df1 46 6c 61 67 20 29 7b 0a 20 20 20 20 73 71 6c 69  Flag ){.    sqli
25df2 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 73 50  te3VdbeSetSql(sP
25df3 61 72 73 65 2e 70 56 64 62 65 2c 20 7a 53 71 6c  arse.pVdbe, zSql
25df4 2c 20 28 69 6e 74 29 28 73 50 61 72 73 65 2e 7a  , (int)(sParse.z
25df5 54 61 69 6c 20 2d 20 7a 53 71 6c 29 29 3b 0a 20  Tail - zSql));. 
25df6 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
25df7 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61  ITE_OK || db->ma
25df8 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
25df9 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
25dfa 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ze((sqlite3_stmt
25dfb 2a 29 73 50 61 72 73 65 2e 70 56 64 62 65 29 3b  *)sParse.pVdbe);
25dfc 0a 20 20 20 20 61 73 73 65 72 74 28 21 28 2a 70  .    assert(!(*p
25dfd 70 53 74 6d 74 29 29 3b 0a 20 20 7d 65 6c 73 65  pStmt));.  }else
25dfe 7b 0a 20 20 20 20 2a 70 70 53 74 6d 74 20 3d 20  {.    *ppStmt = 
25dff 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 73  (sqlite3_stmt*)s
25e00 50 61 72 73 65 2e 70 56 64 62 65 3b 0a 20 20 7d  Parse.pVdbe;.  }
25e01 0a 0a 20 20 69 66 28 20 7a 45 72 72 4d 73 67 20  ..  if( zErrMsg 
25e02 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
25e03 72 6f 72 28 64 62 2c 20 72 63 2c 20 22 25 73 22  ror(db, rc, "%s"
25e04 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  , zErrMsg);.    
25e05 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
25e06 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 65  , zErrMsg);.  }e
25e07 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
25e08 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29  Error(db, rc, 0)
25e09 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71  ;.  }..  rc = sq
25e0a 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
25e0b 20 72 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20   rc);.  assert( 
25e0c 28 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29  (rc&db->errMask)
25e0d 3d 3d 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e  ==rc );.  return
25e0e 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e   rc;.}.static in
25e0f 74 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64  t sqlite3LockAnd
25e10 50 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74  Prepare(.  sqlit
25e11 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
25e12 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
25e13 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f   handle. */.  co
25e14 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
25e15 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38          /* UTF-8
25e16 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
25e17 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  tement. */.  int
25e18 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
25e19 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
25e1a 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
25e1b 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  s. */.  int save
25e1c 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20  SqlFlag,        
25e1d 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70    /* True to cop
25e1e 79 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20  y SQL text into 
25e1f 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  the sqlite3_stmt
25e20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
25e21 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
25e22 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
25e23 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
25e24 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
25e25 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
25e26 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
25e27 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
25e28 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
25e29 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 21 73  int rc;.  if( !s
25e2a 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
25e2b 6b 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72  kOk(db) ){.    r
25e2c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
25e2d 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  USE;.  }.  sqlit
25e2e 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
25e2f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
25e30 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
25e31 6c 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71  l(db);.  rc = sq
25e32 6c 69 74 65 33 50 72 65 70 61 72 65 28 64 62 2c  lite3Prepare(db,
25e33 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 73   zSql, nBytes, s
25e34 61 76 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74  aveSqlFlag, ppSt
25e35 6d 74 2c 20 70 7a 54 61 69 6c 29 3b 0a 20 20 73  mt, pzTail);.  s
25e36 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
25e37 41 6c 6c 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  All(db);.  sqlit
25e38 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
25e39 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
25e3a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
25e3b 20 52 65 72 75 6e 20 74 68 65 20 63 6f 6d 70 69   Rerun the compi
25e3c 6c 61 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74  lation of a stat
25e3d 65 6d 65 6e 74 20 61 66 74 65 72 20 61 20 73 63  ement after a sc
25e3e 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 20  hema change..** 
25e3f 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
25e40 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73  he statement was
25e41 20 72 65 63 6f 6d 70 69 6c 65 64 20 73 75 63 63   recompiled succ
25e42 65 73 73 66 75 6c 6c 79 2e 0a 2a 2a 20 52 65 74  essfully..** Ret
25e43 75 72 6e 20 66 61 6c 73 65 20 69 66 20 74 68 65  urn false if the
25e44 72 65 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6f  re is an error o
25e45 66 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 2a 2f 0a  f some kind..*/.
25e46 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
25e47 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70  nt sqlite3Reprep
25e48 61 72 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  are(Vdbe *p){.  
25e49 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
25e4a 33 5f 73 74 6d 74 20 2a 70 4e 65 77 3b 0a 20 20  3_stmt *pNew;.  
25e4b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
25e4c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
25e4d 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
25e4e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73  te3_mutex_held(s
25e4f 71 6c 69 74 65 33 56 64 62 65 44 62 28 70 29 2d  qlite3VdbeDb(p)-
25e50 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 7a 53 71  >mutex) );.  zSq
25e51 6c 20 3d 20 73 71 6c 69 74 65 33 5f 73 71 6c 28  l = sqlite3_sql(
25e52 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
25e53 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53  p);.  assert( zS
25e54 71 6c 21 3d 30 20 29 3b 20 20 2f 2a 20 52 65 70  ql!=0 );  /* Rep
25e55 72 65 70 61 72 65 20 6f 6e 6c 79 20 63 61 6c 6c  repare only call
25e56 65 64 20 66 6f 72 20 70 72 65 70 61 72 65 5f 76  ed for prepare_v
25e57 32 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a  2() statements *
25e58 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33  /.  db = sqlite3
25e59 56 64 62 65 44 62 28 70 29 3b 0a 20 20 61 73 73  VdbeDb(p);.  ass
25e5a 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
25e5b 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
25e5c 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  x) );.  rc = sql
25e5d 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61  ite3LockAndPrepa
25e5e 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  re(db, zSql, -1,
25e5f 20 30 2c 20 26 70 4e 65 77 2c 20 30 29 3b 0a 20   0, &pNew, 0);. 
25e60 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69   if( rc ){.    i
25e61 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
25e62 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  MEM ){.      db-
25e63 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
25e64 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  1;.    }.    ass
25e65 65 72 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b 0a  ert( pNew==0 );.
25e66 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
25e67 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
25e68 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20  t( pNew!=0 );.  
25e69 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53  }.  sqlite3VdbeS
25e6a 77 61 70 28 28 56 64 62 65 2a 29 70 4e 65 77 2c  wap((Vdbe*)pNew,
25e6b 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 72   p);.  sqlite3Tr
25e6c 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70  ansferBindings(p
25e6d 4e 65 77 2c 20 28 73 71 6c 69 74 65 33 5f 73 74  New, (sqlite3_st
25e6e 6d 74 2a 29 70 29 3b 0a 20 20 73 71 6c 69 74 65  mt*)p);.  sqlite
25e6f 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65  3VdbeResetStepRe
25e70 73 75 6c 74 28 28 56 64 62 65 2a 29 70 4e 65 77  sult((Vdbe*)pNew
25e71 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
25e72 46 69 6e 61 6c 69 7a 65 28 28 56 64 62 65 2a 29  Finalize((Vdbe*)
25e73 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20  pNew);.  return 
25e74 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f  1;.}.../*.** Two
25e75 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
25e76 20 6f 66 66 69 63 69 61 6c 20 41 50 49 2e 20 20   official API.  
25e77 4c 65 67 61 63 79 20 61 6e 64 20 6e 65 77 20 75  Legacy and new u
25e78 73 65 2e 20 20 49 6e 20 74 68 65 20 6c 65 67 61  se.  In the lega
25e79 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20 74  cy.** version, t
25e7a 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  he original SQL 
25e7b 74 65 78 74 20 69 73 20 6e 6f 74 20 73 61 76 65  text is not save
25e7c 64 20 69 6e 20 74 68 65 20 70 72 65 70 61 72 65  d in the prepare
25e7d 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61  d statement.** a
25e7e 6e 64 20 73 6f 20 69 66 20 61 20 73 63 68 65 6d  nd so if a schem
25e7f 61 20 63 68 61 6e 67 65 20 6f 63 63 75 72 73 2c  a change occurs,
25e80 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 69   SQLITE_SCHEMA i
25e81 73 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  s returned by.**
25e82 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e   sqlite3_step().
25e83 20 20 49 6e 20 74 68 65 20 6e 65 77 20 76 65 72    In the new ver
25e84 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e  sion, the origin
25e85 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 72  al SQL text is r
25e86 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 74  etained.** and t
25e87 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
25e88 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65  automatically re
25e89 63 6f 6d 70 69 6c 65 64 20 69 66 20 61 6e 20 73  compiled if an s
25e8a 63 68 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a 20  chema change.** 
25e8b 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  occurs..*/.SQLIT
25e8c 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
25e8d 33 5f 70 72 65 70 61 72 65 28 0a 20 20 73 71 6c  3_prepare(.  sql
25e8e 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
25e8f 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
25e90 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20  se handle. */.  
25e91 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
25e92 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46  ,         /* UTF
25e93 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73  -8 encoded SQL s
25e94 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69  tatement. */.  i
25e95 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20  nt nBytes,      
25e96 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
25e97 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79  th of zSql in by
25e98 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  tes. */.  sqlite
25e99 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c  3_stmt **ppStmt,
25e9a 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f      /* OUT: A po
25e9b 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72 65  inter to the pre
25e9c 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
25e9d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
25e9e 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f  **pzTail       /
25e9f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61  * OUT: End of pa
25ea0 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29  rsed string */.)
25ea1 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63  {.  int rc;.  rc
25ea2 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e   = sqlite3LockAn
25ea3 64 50 72 65 70 61 72 65 28 64 62 2c 7a 53 71 6c  dPrepare(db,zSql
25ea4 2c 6e 42 79 74 65 73 2c 30 2c 70 70 53 74 6d 74  ,nBytes,0,ppStmt
25ea5 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65  ,pzTail);.  asse
25ea6 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
25ea7 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c  K || ppStmt==0 |
25ea8 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20  | *ppStmt==0 ); 
25ea9 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30   /* VERIFY: F130
25eaa 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  21 */.  return r
25eab 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  c;.}.SQLITE_API 
25eac 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  int sqlite3_prep
25ead 61 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  are_v2(.  sqlite
25eae 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
25eaf 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
25eb0 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e  handle. */.  con
25eb1 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
25eb2 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20         /* UTF-8 
25eb3 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
25eb4 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20  ement. */.  int 
25eb5 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
25eb6 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
25eb7 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
25eb8 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
25eb9 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20  tmt **ppStmt,   
25eba 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74   /* OUT: A point
25ebb 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72  er to the prepar
25ebc 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ed statement */.
25ebd 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
25ebe 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f  zTail       /* O
25ebf 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65  UT: End of parse
25ec0 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20  d string */.){. 
25ec1 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20   int rc;.  rc = 
25ec2 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72  sqlite3LockAndPr
25ec3 65 70 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42  epare(db,zSql,nB
25ec4 79 74 65 73 2c 31 2c 70 70 53 74 6d 74 2c 70 7a  ytes,1,ppStmt,pz
25ec5 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28  Tail);.  assert(
25ec6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
25ec7 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a  | ppStmt==0 || *
25ec8 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a  ppStmt==0 );  /*
25ec9 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31 20   VERIFY: F13021 
25eca 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  */.  return rc;.
25ecb 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  }...#ifndef SQLI
25ecc 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
25ecd 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65 20  .** Compile the 
25ece 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 53  UTF-16 encoded S
25ecf 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71  QL statement zSq
25ed0 6c 20 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65  l into a stateme
25ed1 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74  nt handle..*/.st
25ed2 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
25ed3 50 72 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c  Prepare16(.  sql
25ed4 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
25ed5 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
25ed6 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20  se handle. */ . 
25ed7 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71   const void *zSq
25ed8 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54  l,         /* UT
25ed9 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  F-8 encoded SQL 
25eda 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
25edb 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
25edc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
25edd 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
25ede 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73  ytes. */.  int s
25edf 61 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20  aveSqlFlag,     
25ee0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
25ee1 73 61 76 65 20 53 51 4c 20 74 65 78 74 20 69 6e  save SQL text in
25ee2 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  to the sqlite3_s
25ee3 74 6d 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  tmt */.  sqlite3
25ee4 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
25ee5 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69     /* OUT: A poi
25ee6 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70  nter to the prep
25ee7 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a  ared statement *
25ee8 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
25ee9 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a  *pzTail       /*
25eea 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72   OUT: End of par
25eeb 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b  sed string */.){
25eec 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74  .  /* This funct
25eed 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 77 6f  ion currently wo
25eee 72 6b 73 20 62 79 20 66 69 72 73 74 20 74 72 61  rks by first tra
25eef 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65 20 55 54  nsforming the UT
25ef0 46 2d 31 36 0a 20 20 2a 2a 20 65 6e 63 6f 64 65  F-16.  ** encode
25ef1 64 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d  d string to UTF-
25ef2 38 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 69 6e 67  8, then invoking
25ef3 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
25ef4 28 29 2e 20 54 68 65 0a 20 20 2a 2a 20 74 72 69  (). The.  ** tri
25ef5 63 6b 79 20 62 69 74 20 69 73 20 66 69 67 75 72  cky bit is figur
25ef6 69 6e 67 20 6f 75 74 20 74 68 65 20 70 6f 69 6e  ing out the poin
25ef7 74 65 72 20 74 6f 20 72 65 74 75 72 6e 20 69 6e  ter to return in
25ef8 20 2a 70 7a 54 61 69 6c 2e 0a 20 20 2a 2f 0a 20   *pzTail..  */. 
25ef9 20 63 68 61 72 20 2a 7a 53 71 6c 38 3b 0a 20 20   char *zSql8;.  
25efa 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 69  const char *zTai
25efb 6c 38 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  l8 = 0;.  int rc
25efc 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
25efd 20 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66   if( !sqlite3Saf
25efe 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29  etyCheckOk(db) )
25eff 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
25f00 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
25f01 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
25f02 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
25f03 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69  ;.  zSql8 = sqli
25f04 74 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20  te3Utf16to8(db, 
25f05 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20  zSql, nBytes);. 
25f06 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20   if( zSql8 ){.  
25f07 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f    rc = sqlite3Lo
25f08 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c  ckAndPrepare(db,
25f09 20 7a 53 71 6c 38 2c 20 2d 31 2c 20 73 61 76 65   zSql8, -1, save
25f0a 53 71 6c 46 6c 61 67 2c 20 70 70 53 74 6d 74 2c  SqlFlag, ppStmt,
25f0b 20 26 7a 54 61 69 6c 38 29 3b 0a 20 20 7d 0a 0a   &zTail8);.  }..
25f0c 20 20 69 66 28 20 7a 54 61 69 6c 38 20 26 26 20    if( zTail8 && 
25f0d 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20 2f 2a  pzTail ){.    /*
25f0e 20 49 66 20 73 71 6c 69 74 65 33 5f 70 72 65 70   If sqlite3_prep
25f0f 61 72 65 20 72 65 74 75 72 6e 73 20 61 20 74 61  are returns a ta
25f10 69 6c 20 70 6f 69 6e 74 65 72 2c 20 77 65 20 63  il pointer, we c
25f11 61 6c 63 75 6c 61 74 65 20 74 68 65 0a 20 20 20  alculate the.   
25f12 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 70   ** equivalent p
25f13 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20  ointer into the 
25f14 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 62 79  UTF-16 string by
25f15 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 20 75 6e   counting the un
25f16 69 63 6f 64 65 0a 20 20 20 20 2a 2a 20 63 68 61  icode.    ** cha
25f17 72 61 63 74 65 72 73 20 62 65 74 77 65 65 6e 20  racters between 
25f18 7a 53 71 6c 38 20 61 6e 64 20 7a 54 61 69 6c 38  zSql8 and zTail8
25f19 2c 20 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72  , and then retur
25f1a 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 0a 20  ning a pointer. 
25f1b 20 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 6e     ** the same n
25f1c 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
25f1d 65 72 73 20 69 6e 74 6f 20 74 68 65 20 55 54 46  ers into the UTF
25f1e 2d 31 36 20 73 74 72 69 6e 67 2e 0a 20 20 20 20  -16 string..    
25f1f 2a 2f 0a 20 20 20 20 69 6e 74 20 63 68 61 72 73  */.    int chars
25f20 5f 70 61 72 73 65 64 20 3d 20 73 71 6c 69 74 65  _parsed = sqlite
25f21 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a 53 71  3Utf8CharLen(zSq
25f22 6c 38 2c 20 28 69 6e 74 29 28 7a 54 61 69 6c 38  l8, (int)(zTail8
25f23 2d 7a 53 71 6c 38 29 29 3b 0a 20 20 20 20 2a 70  -zSql8));.    *p
25f24 7a 54 61 69 6c 20 3d 20 28 75 38 20 2a 29 7a 53  zTail = (u8 *)zS
25f25 71 6c 20 2b 20 73 71 6c 69 74 65 33 55 74 66 31  ql + sqlite3Utf1
25f26 36 42 79 74 65 4c 65 6e 28 7a 53 71 6c 2c 20 63  6ByteLen(zSql, c
25f27 68 61 72 73 5f 70 61 72 73 65 64 29 3b 0a 20 20  hars_parsed);.  
25f28 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
25f29 65 28 64 62 2c 20 7a 53 71 6c 38 29 3b 20 0a 20  e(db, zSql8); . 
25f2a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
25f2b 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
25f2c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
25f2d 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
25f2e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
25f2f 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f  /*.** Two versio
25f30 6e 73 20 6f 66 20 74 68 65 20 6f 66 66 69 63 69  ns of the offici
25f31 61 6c 20 41 50 49 2e 20 20 4c 65 67 61 63 79 20  al API.  Legacy 
25f32 61 6e 64 20 6e 65 77 20 75 73 65 2e 20 20 49 6e  and new use.  In
25f33 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76   the legacy.** v
25f34 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67  ersion, the orig
25f35 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73  inal SQL text is
25f36 20 6e 6f 74 20 73 61 76 65 64 20 69 6e 20 74 68   not saved in th
25f37 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
25f38 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69  ment.** and so i
25f39 66 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67  f a schema chang
25f3a 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45  e occurs, SQLITE
25f3b 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74 75 72  _SCHEMA is retur
25f3c 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65  ned by.** sqlite
25f3d 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20 74 68  3_step().  In th
25f3e 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20 74  e new version, t
25f3f 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  he original SQL 
25f40 74 65 78 74 20 69 73 20 72 65 74 61 69 6e 65 64  text is retained
25f41 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74  .** and the stat
25f42 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61 74  ement is automat
25f43 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65  ically recompile
25f44 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61 20 63  d if an schema c
25f45 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e  hange.** occurs.
25f46 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
25f47 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nt sqlite3_prepa
25f48 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20  re16(.  sqlite3 
25f49 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
25f4a 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
25f4b 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73  ndle. */ .  cons
25f4c 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20  t void *zSql,   
25f4d 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
25f4e 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
25f4f 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
25f50 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
25f51 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
25f52 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
25f53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
25f54 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
25f55 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
25f56 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
25f57 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
25f58 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a   const void **pz
25f59 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
25f5a 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
25f5b 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
25f5c 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73  int rc;.  rc = s
25f5d 71 6c 69 74 65 33 50 72 65 70 61 72 65 31 36 28  qlite3Prepare16(
25f5e 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30  db,zSql,nBytes,0
25f5f 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b  ,ppStmt,pzTail);
25f60 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
25f61 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74  QLITE_OK || ppSt
25f62 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74  mt==0 || *ppStmt
25f63 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46  ==0 );  /* VERIF
25f64 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72  Y: F13021 */.  r
25f65 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
25f66 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
25f67 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
25f68 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
25f69 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25f6a 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
25f6b 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69   */ .  const voi
25f6c 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  d *zSql,        
25f6d 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
25f6e 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
25f6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
25f70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
25f71 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
25f72 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
25f73 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
25f74 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55  ppStmt,    /* OU
25f75 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  T: A pointer to 
25f76 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
25f77 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73  tement */.  cons
25f78 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20  t void **pzTail 
25f79 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e        /* OUT: En
25f7a 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69  d of parsed stri
25f7b 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ng */.){.  int r
25f7c 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  c;.  rc = sqlite
25f7d 33 50 72 65 70 61 72 65 31 36 28 64 62 2c 7a 53  3Prepare16(db,zS
25f7e 71 6c 2c 6e 42 79 74 65 73 2c 31 2c 70 70 53 74  ql,nBytes,1,ppSt
25f7f 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73  mt,pzTail);.  as
25f80 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
25f81 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30  _OK || ppStmt==0
25f82 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29   || *ppStmt==0 )
25f83 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31  ;  /* VERIFY: F1
25f84 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e  3021 */.  return
25f85 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f   rc;.}..#endif /
25f86 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
25f87 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  F16 */../*******
25f88 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
25f89 72 65 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  repare.c *******
25f8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25f8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25f8c 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
25f8d 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
25f8e 6c 65 20 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a  le select.c ****
25f8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25f91 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
25f92 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
25f93 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
25f94 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
25f95 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
25f96 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
25f97 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
25f98 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
25f99 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
25f9a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
25f9b 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
25f9c 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
25f9d 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
25f9e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
25f9f 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
25fa0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
25fa1 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
25fa2 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
25fa3 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
25fa4 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
25fa5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fa6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
25fa9 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
25faa 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
25fab 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
25fac 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
25fad 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20  er.** to handle 
25fae 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
25faf 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s in SQLite..**.
25fb0 2a 2a 20 24 49 64 3a 20 73 65 6c 65 63 74 2e 63  ** $Id: select.c
25fb1 2c 76 20 31 2e 34 39 38 20 32 30 30 39 2f 30 31  ,v 1.498 2009/01
25fb2 2f 30 39 20 30 32 3a 34 39 3a 33 32 20 64 72 68  /09 02:49:32 drh
25fb3 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a   Exp $.*/.../*.*
25fb4 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 74 68 65  * Delete all the
25fb5 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 53 65   content of a Se
25fb6 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20 62  lect structure b
25fb7 75 74 20 64 6f 20 6e 6f 74 20 64 65 61 6c 6c 6f  ut do not deallo
25fb8 63 61 74 65 0a 2a 2a 20 74 68 65 20 73 65 6c 65  cate.** the sele
25fb9 63 74 20 73 74 72 75 63 74 75 72 65 20 69 74 73  ct structure its
25fba 65 6c 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  elf..*/.static v
25fbb 6f 69 64 20 63 6c 65 61 72 53 65 6c 65 63 74 28  oid clearSelect(
25fbc 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c  sqlite3 *db, Sel
25fbd 65 63 74 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  ect *p){.  sqlit
25fbe 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
25fbf 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b  (db, p->pEList);
25fc0 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
25fc1 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  tDelete(db, p->p
25fc2 53 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  Src);.  sqlite3E
25fc3 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
25fc4 3e 70 57 68 65 72 65 29 3b 0a 20 20 73 71 6c 69  >pWhere);.  sqli
25fc5 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
25fc6 65 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42  e(db, p->pGroupB
25fc7 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  y);.  sqlite3Exp
25fc8 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
25fc9 48 61 76 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74  Having);.  sqlit
25fca 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
25fcb 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  (db, p->pOrderBy
25fcc 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
25fcd 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  ctDelete(db, p->
25fce 70 50 72 69 6f 72 29 3b 0a 20 20 73 71 6c 69 74  pPrior);.  sqlit
25fcf 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
25fd0 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 73   p->pLimit);.  s
25fd1 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
25fd2 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29  (db, p->pOffset)
25fd3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
25fd4 61 6c 69 7a 65 20 61 20 53 65 6c 65 63 74 44 65  alize a SelectDe
25fd5 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  st structure..*/
25fd6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
25fd7 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65  void sqlite3Sele
25fd8 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63  ctDestInit(Selec
25fd9 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20 69 6e  tDest *pDest, in
25fda 74 20 65 44 65 73 74 2c 20 69 6e 74 20 69 50 61  t eDest, int iPa
25fdb 72 6d 29 7b 0a 20 20 70 44 65 73 74 2d 3e 65 44  rm){.  pDest->eD
25fdc 65 73 74 20 3d 20 28 75 38 29 65 44 65 73 74 3b  est = (u8)eDest;
25fdd 0a 20 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20  .  pDest->iParm 
25fde 3d 20 69 50 61 72 6d 3b 0a 20 20 70 44 65 73 74  = iParm;.  pDest
25fdf 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 30 3b 0a  ->affinity = 0;.
25fe0 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20    pDest->iMem = 
25fe1 30 3b 0a 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d  0;.  pDest->nMem
25fe2 20 3d 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20   = 0;.}.../*.** 
25fe3 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 53  Allocate a new S
25fe4 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20  elect structure 
25fe5 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
25fe6 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20  nter to that.** 
25fe7 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
25fe8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c  LITE_PRIVATE Sel
25fe9 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65  ect *sqlite3Sele
25fea 63 74 4e 65 77 28 0a 20 20 50 61 72 73 65 20 2a  ctNew(.  Parse *
25feb 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
25fec 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
25fed 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
25fee 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20  *pEList,     /* 
25fef 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 74 6f  which columns to
25ff0 20 69 6e 63 6c 75 64 65 20 69 6e 20 74 68 65 20   include in the 
25ff1 72 65 73 75 6c 74 20 2a 2f 0a 20 20 53 72 63 4c  result */.  SrcL
25ff2 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
25ff3 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c    /* the FROM cl
25ff4 61 75 73 65 20 2d 2d 20 77 68 69 63 68 20 74 61  ause -- which ta
25ff5 62 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a  bles to scan */.
25ff6 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20    Expr *pWhere, 
25ff7 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 57          /* the W
25ff8 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
25ff9 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75   ExprList *pGrou
25ffa 70 42 79 2c 20 20 20 2f 2a 20 74 68 65 20 47 52  pBy,   /* the GR
25ffb 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  OUP BY clause */
25ffc 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67  .  Expr *pHaving
25ffd 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20  ,        /* the 
25ffe 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f  HAVING clause */
25fff 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
26000 64 65 72 42 79 2c 20 20 20 2f 2a 20 74 68 65 20  derBy,   /* the 
26001 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
26002 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69 73 74 69  */.  int isDisti
26003 6e 63 74 2c 20 20 20 20 20 20 20 2f 2a 20 74 72  nct,       /* tr
26004 75 65 20 69 66 20 74 68 65 20 44 49 53 54 49 4e  ue if the DISTIN
26005 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72  CT keyword is pr
26006 65 73 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  esent */.  Expr 
26007 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20 20 20 20  *pLimit,        
26008 20 2f 2a 20 4c 49 4d 49 54 20 76 61 6c 75 65 2e   /* LIMIT value.
26009 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74    NULL means not
2600a 20 75 73 65 64 20 2a 2f 0a 20 20 45 78 70 72 20   used */.  Expr 
2600b 2a 70 4f 66 66 73 65 74 20 20 20 20 20 20 20 20  *pOffset        
2600c 20 2f 2a 20 4f 46 46 53 45 54 20 76 61 6c 75 65   /* OFFSET value
2600d 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f  .  NULL means no
2600e 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20   offset */.){.  
2600f 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20  Select *pNew;.  
26010 53 65 6c 65 63 74 20 73 74 61 6e 64 69 6e 3b 0a  Select standin;.
26011 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
26012 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 4e  pParse->db;.  pN
26013 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
26014 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
26015 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20  eof(*pNew) );.  
26016 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c  assert( db->mall
26017 6f 63 46 61 69 6c 65 64 20 7c 7c 20 21 70 4f 66  ocFailed || !pOf
26018 66 73 65 74 20 7c 7c 20 70 4c 69 6d 69 74 20 29  fset || pLimit )
26019 3b 20 2f 2a 20 4f 46 46 53 45 54 20 69 6d 70 6c  ; /* OFFSET impl
2601a 69 65 73 20 4c 49 4d 49 54 20 2a 2f 0a 20 20 69  ies LIMIT */.  i
2601b 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( pNew==0 ){.  
2601c 20 20 70 4e 65 77 20 3d 20 26 73 74 61 6e 64 69    pNew = &standi
2601d 6e 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 4e  n;.    memset(pN
2601e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70  ew, 0, sizeof(*p
2601f 4e 65 77 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28  New));.  }.  if(
26020 20 70 45 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20   pEList==0 ){.  
26021 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74    pEList = sqlit
26022 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
26023 28 70 50 61 72 73 65 2c 20 30 2c 20 73 71 6c 69  (pParse, 0, sqli
26024 74 65 33 45 78 70 72 28 64 62 2c 54 4b 5f 41 4c  te3Expr(db,TK_AL
26025 4c 2c 30 2c 30 2c 30 29 2c 20 30 29 3b 0a 20 20  L,0,0,0), 0);.  
26026 7d 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74  }.  pNew->pEList
26027 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 70 4e 65   = pEList;.  pNe
26028 77 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 3b 0a  w->pSrc = pSrc;.
26029 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d    pNew->pWhere =
2602a 20 70 57 68 65 72 65 3b 0a 20 20 70 4e 65 77 2d   pWhere;.  pNew-
2602b 3e 70 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f  >pGroupBy = pGro
2602c 75 70 42 79 3b 0a 20 20 70 4e 65 77 2d 3e 70 48  upBy;.  pNew->pH
2602d 61 76 69 6e 67 20 3d 20 70 48 61 76 69 6e 67 3b  aving = pHaving;
2602e 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42  .  pNew->pOrderB
2602f 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20  y = pOrderBy;.  
26030 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d  pNew->selFlags =
26031 20 69 73 44 69 73 74 69 6e 63 74 20 3f 20 53 46   isDistinct ? SF
26032 5f 44 69 73 74 69 6e 63 74 20 3a 20 30 3b 0a 20  _Distinct : 0;. 
26033 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 53   pNew->op = TK_S
26034 45 4c 45 43 54 3b 0a 20 20 70 4e 65 77 2d 3e 70  ELECT;.  pNew->p
26035 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a  Limit = pLimit;.
26036 20 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20    pNew->pOffset 
26037 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20 70 4e 65  = pOffset;.  pNe
26038 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  w->addrOpenEphm[
26039 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d  0] = -1;.  pNew-
2603a 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d  >addrOpenEphm[1]
2603b 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61   = -1;.  pNew->a
2603c 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d  ddrOpenEphm[2] =
2603d 20 2d 31 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d   -1;.  if( db->m
2603e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 7b 0a  allocFailed ) {.
2603f 20 20 20 20 63 6c 65 61 72 53 65 6c 65 63 74 28      clearSelect(
26040 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 69  db, pNew);.    i
26041 66 28 20 70 4e 65 77 21 3d 26 73 74 61 6e 64 69  f( pNew!=&standi
26042 6e 20 29 20 73 71 6c 69 74 65 33 44 62 46 72 65  n ) sqlite3DbFre
26043 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20  e(db, pNew);.   
26044 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 20   pNew = 0;.  }. 
26045 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
26046 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68  ./*.** Delete th
26047 65 20 67 69 76 65 6e 20 53 65 6c 65 63 74 20 73  e given Select s
26048 74 72 75 63 74 75 72 65 20 61 6e 64 20 61 6c 6c  tructure and all
26049 20 6f 66 20 69 74 73 20 73 75 62 73 74 72 75 63   of its substruc
2604a 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tures..*/.SQLITE
2604b 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2604c 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
2604d 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53  e(sqlite3 *db, S
2604e 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 66 28  elect *p){.  if(
2604f 20 70 20 29 7b 0a 20 20 20 20 63 6c 65 61 72 53   p ){.    clearS
26050 65 6c 65 63 74 28 64 62 2c 20 70 29 3b 0a 20 20  elect(db, p);.  
26051 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
26052 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  db, p);.  }.}../
26053 2a 0a 2a 2a 20 47 69 76 65 6e 20 31 20 74 6f 20  *.** Given 1 to 
26054 33 20 69 64 65 6e 74 69 66 69 65 72 73 20 70 72  3 identifiers pr
26055 65 63 65 65 64 69 6e 67 20 74 68 65 20 4a 4f 49  eceeding the JOI
26056 4e 20 6b 65 79 77 6f 72 64 2c 20 64 65 74 65 72  N keyword, deter
26057 6d 69 6e 65 20 74 68 65 0a 2a 2a 20 74 79 70 65  mine the.** type
26058 20 6f 66 20 6a 6f 69 6e 2e 20 20 52 65 74 75 72   of join.  Retur
26059 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e  n an integer con
2605a 73 74 61 6e 74 20 74 68 61 74 20 65 78 70 72 65  stant that expre
2605b 73 73 65 73 20 74 68 61 74 20 74 79 70 65 0a 2a  sses that type.*
2605c 2a 20 69 6e 20 74 65 72 6d 73 20 6f 66 20 74 68  * in terms of th
2605d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 69 74 20  e following bit 
2605e 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  values:.**.**   
2605f 20 20 4a 54 5f 49 4e 4e 45 52 0a 2a 2a 20 20 20    JT_INNER.**   
26060 20 20 4a 54 5f 43 52 4f 53 53 0a 2a 2a 20 20 20    JT_CROSS.**   
26061 20 20 4a 54 5f 4f 55 54 45 52 0a 2a 2a 20 20 20    JT_OUTER.**   
26062 20 20 4a 54 5f 4e 41 54 55 52 41 4c 0a 2a 2a 20    JT_NATURAL.** 
26063 20 20 20 20 4a 54 5f 4c 45 46 54 0a 2a 2a 20 20      JT_LEFT.**  
26064 20 20 20 4a 54 5f 52 49 47 48 54 0a 2a 2a 0a 2a     JT_RIGHT.**.*
26065 2a 20 41 20 66 75 6c 6c 20 6f 75 74 65 72 20 6a  * A full outer j
26066 6f 69 6e 20 69 73 20 74 68 65 20 63 6f 6d 62 69  oin is the combi
26067 6e 61 74 69 6f 6e 20 6f 66 20 4a 54 5f 4c 45 46  nation of JT_LEF
26068 54 20 61 6e 64 20 4a 54 5f 52 49 47 48 54 2e 0a  T and JT_RIGHT..
26069 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6c 6c 65  **.** If an ille
2606a 67 61 6c 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  gal or unsupport
2606b 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 69 73 20  ed join type is 
2606c 73 65 65 6e 2c 20 74 68 65 6e 20 73 74 69 6c 6c  seen, then still
2606d 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 6a 6f 69   return.** a joi
2606e 6e 20 74 79 70 65 2c 20 62 75 74 20 70 75 74 20  n type, but put 
2606f 61 6e 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20  an error in the 
26070 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65  pParse structure
26071 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
26072 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  ATE int sqlite3J
26073 6f 69 6e 54 79 70 65 28 50 61 72 73 65 20 2a 70  oinType(Parse *p
26074 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 41  Parse, Token *pA
26075 2c 20 54 6f 6b 65 6e 20 2a 70 42 2c 20 54 6f 6b  , Token *pB, Tok
26076 65 6e 20 2a 70 43 29 7b 0a 20 20 69 6e 74 20 6a  en *pC){.  int j
26077 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a 20 20 54  ointype = 0;.  T
26078 6f 6b 65 6e 20 2a 61 70 41 6c 6c 5b 33 5d 3b 0a  oken *apAll[3];.
26079 20 20 54 6f 6b 65 6e 20 2a 70 3b 0a 20 20 73 74    Token *p;.  st
2607a 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
2607b 74 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  t {.    const ch
2607c 61 72 20 7a 4b 65 79 77 6f 72 64 5b 38 5d 3b 0a  ar zKeyword[8];.
2607d 20 20 20 20 75 38 20 6e 43 68 61 72 3b 0a 20 20      u8 nChar;.  
2607e 20 20 75 38 20 63 6f 64 65 3b 0a 20 20 7d 20 6b    u8 code;.  } k
2607f 65 79 77 6f 72 64 73 5b 5d 20 3d 20 7b 0a 20 20  eywords[] = {.  
26080 20 20 7b 20 22 6e 61 74 75 72 61 6c 22 2c 20 37    { "natural", 7
26081 2c 20 4a 54 5f 4e 41 54 55 52 41 4c 20 7d 2c 0a  , JT_NATURAL },.
26082 20 20 20 20 7b 20 22 6c 65 66 74 22 2c 20 20 20      { "left",   
26083 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 4f   4, JT_LEFT|JT_O
26084 55 54 45 52 20 7d 2c 0a 20 20 20 20 7b 20 22 72  UTER },.    { "r
26085 69 67 68 74 22 2c 20 20 20 35 2c 20 4a 54 5f 52  ight",   5, JT_R
26086 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c  IGHT|JT_OUTER },
26087 0a 20 20 20 20 7b 20 22 66 75 6c 6c 22 2c 20 20  .    { "full",  
26088 20 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f    4, JT_LEFT|JT_
26089 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 7d  RIGHT|JT_OUTER }
2608a 2c 0a 20 20 20 20 7b 20 22 6f 75 74 65 72 22 2c  ,.    { "outer",
2608b 20 20 20 35 2c 20 4a 54 5f 4f 55 54 45 52 20 7d     5, JT_OUTER }
2608c 2c 0a 20 20 20 20 7b 20 22 69 6e 6e 65 72 22 2c  ,.    { "inner",
2608d 20 20 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 20 7d     5, JT_INNER }
2608e 2c 0a 20 20 20 20 7b 20 22 63 72 6f 73 73 22 2c  ,.    { "cross",
2608f 20 20 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 7c 4a     5, JT_INNER|J
26090 54 5f 43 52 4f 53 53 20 7d 2c 0a 20 20 7d 3b 0a  T_CROSS },.  };.
26091 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 70    int i, j;.  ap
26092 41 6c 6c 5b 30 5d 20 3d 20 70 41 3b 0a 20 20 61  All[0] = pA;.  a
26093 70 41 6c 6c 5b 31 5d 20 3d 20 70 42 3b 0a 20 20  pAll[1] = pB;.  
26094 61 70 41 6c 6c 5b 32 5d 20 3d 20 70 43 3b 0a 20  apAll[2] = pC;. 
26095 20 66 6f 72 28 69 3d 30 3b 20 69 3c 33 20 26 26   for(i=0; i<3 &&
26096 20 61 70 41 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b   apAll[i]; i++){
26097 0a 20 20 20 20 70 20 3d 20 61 70 41 6c 6c 5b 69  .    p = apAll[i
26098 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  ];.    for(j=0; 
26099 6a 3c 41 72 72 61 79 53 69 7a 65 28 6b 65 79 77  j<ArraySize(keyw
2609a 6f 72 64 73 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ords); j++){.   
2609b 20 20 20 69 66 28 20 70 2d 3e 6e 3d 3d 6b 65 79     if( p->n==key
2609c 77 6f 72 64 73 5b 6a 5d 2e 6e 43 68 61 72 20 0a  words[j].nChar .
2609d 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c            && sql
2609e 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63 68  ite3StrNICmp((ch
2609f 61 72 2a 29 70 2d 3e 7a 2c 20 6b 65 79 77 6f 72  ar*)p->z, keywor
260a0 64 73 5b 6a 5d 2e 7a 4b 65 79 77 6f 72 64 2c 20  ds[j].zKeyword, 
260a1 70 2d 3e 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20  p->n)==0 ){.    
260a2 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d 20      jointype |= 
260a3 6b 65 79 77 6f 72 64 73 5b 6a 5d 2e 63 6f 64 65  keywords[j].code
260a4 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
260a5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
260a6 20 20 20 69 66 28 20 6a 3e 3d 41 72 72 61 79 53     if( j>=ArrayS
260a7 69 7a 65 28 6b 65 79 77 6f 72 64 73 29 20 29 7b  ize(keywords) ){
260a8 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20  .      jointype 
260a9 7c 3d 20 4a 54 5f 45 52 52 4f 52 3b 0a 20 20 20  |= JT_ERROR;.   
260aa 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
260ab 20 20 7d 0a 20 20 69 66 28 0a 20 20 20 20 20 28    }.  if(.     (
260ac 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54 5f 49  jointype & (JT_I
260ad 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52 29 29 3d  NNER|JT_OUTER))=
260ae 3d 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55  =(JT_INNER|JT_OU
260af 54 45 52 29 20 7c 7c 0a 20 20 20 20 20 28 6a 6f  TER) ||.     (jo
260b0 69 6e 74 79 70 65 20 26 20 4a 54 5f 45 52 52 4f  intype & JT_ERRO
260b1 52 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 63  R)!=0.  ){.    c
260b2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 70 20 3d  onst char *zSp =
260b3 20 22 20 22 3b 0a 20 20 20 20 61 73 73 65 72 74   " ";.    assert
260b4 28 20 70 42 21 3d 30 20 29 3b 0a 20 20 20 20 69  ( pB!=0 );.    i
260b5 66 28 20 70 43 3d 3d 30 20 29 7b 20 7a 53 70 2b  f( pC==0 ){ zSp+
260b6 2b 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33  +; }.    sqlite3
260b7 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
260b8 20 22 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73   "unknown or uns
260b9 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79  upported join ty
260ba 70 65 3a 20 22 0a 20 20 20 20 20 20 20 22 25 54  pe: ".       "%T
260bb 20 25 54 25 73 25 54 22 2c 20 70 41 2c 20 70 42   %T%s%T", pA, pB
260bc 2c 20 7a 53 70 2c 20 70 43 29 3b 0a 20 20 20 20  , zSp, pC);.    
260bd 6a 6f 69 6e 74 79 70 65 20 3d 20 4a 54 5f 49 4e  jointype = JT_IN
260be 4e 45 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  NER;.  }else if(
260bf 20 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 52   jointype & JT_R
260c0 49 47 48 54 20 29 7b 0a 20 20 20 20 73 71 6c 69  IGHT ){.    sqli
260c1 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
260c2 73 65 2c 20 0a 20 20 20 20 20 20 22 52 49 47 48  se, .      "RIGH
260c3 54 20 61 6e 64 20 46 55 4c 4c 20 4f 55 54 45 52  T and FULL OUTER
260c4 20 4a 4f 49 4e 73 20 61 72 65 20 6e 6f 74 20 63   JOINs are not c
260c5 75 72 72 65 6e 74 6c 79 20 73 75 70 70 6f 72 74  urrently support
260c6 65 64 22 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79  ed");.    jointy
260c7 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20  pe = JT_INNER;. 
260c8 20 7d 0a 20 20 72 65 74 75 72 6e 20 6a 6f 69 6e   }.  return join
260c9 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  type;.}../*.** R
260ca 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
260cb 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61  of a column in a
260cc 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20   table.  Return 
260cd 2d 31 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e  -1 if the column
260ce 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61  .** is not conta
260cf 69 6e 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c  ined in the tabl
260d0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
260d1 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 54 61 62   columnIndex(Tab
260d2 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20  le *pTab, const 
260d3 63 68 61 72 20 2a 7a 43 6f 6c 29 7b 0a 20 20 69  char *zCol){.  i
260d4 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
260d5 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
260d6 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
260d7 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62  ite3StrICmp(pTab
260d8 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c  ->aCol[i].zName,
260d9 20 7a 43 6f 6c 29 3d 3d 30 20 29 20 72 65 74 75   zCol)==0 ) retu
260da 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn i;.  }.  retu
260db 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn -1;.}../*.** 
260dc 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  Set the value of
260dd 20 61 20 74 6f 6b 65 6e 20 74 6f 20 61 20 27 5c   a token to a '\
260de 30 30 30 27 2d 74 65 72 6d 69 6e 61 74 65 64 20  000'-terminated 
260df 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  string..*/.stati
260e0 63 20 76 6f 69 64 20 73 65 74 54 6f 6b 65 6e 28  c void setToken(
260e1 54 6f 6b 65 6e 20 2a 70 2c 20 63 6f 6e 73 74 20  Token *p, const 
260e2 63 68 61 72 20 2a 7a 29 7b 0a 20 20 70 2d 3e 7a  char *z){.  p->z
260e3 20 3d 20 28 75 38 2a 29 7a 3b 0a 20 20 70 2d 3e   = (u8*)z;.  p->
260e4 6e 20 3d 20 7a 20 3f 20 73 71 6c 69 74 65 33 53  n = z ? sqlite3S
260e5 74 72 6c 65 6e 33 30 28 7a 29 20 3a 20 30 3b 0a  trlen30(z) : 0;.
260e6 20 20 70 2d 3e 64 79 6e 20 3d 20 30 3b 0a 7d 0a    p->dyn = 0;.}.
260e7 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74  ./*.** Set the t
260e8 6f 6b 65 6e 20 74 6f 20 74 68 65 20 64 6f 75 62  oken to the doub
260e9 6c 65 2d 71 75 6f 74 65 64 20 61 6e 64 20 65 73  le-quoted and es
260ea 63 61 70 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  caped version of
260eb 20 74 68 65 20 73 74 72 69 6e 67 20 70 6f 69 6e   the string poin
260ec 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 7a 2e 20  ted.** to by z. 
260ed 46 6f 72 20 65 78 61 6d 70 6c 65 3b 0a 2a 2a 0a  For example;.**.
260ee 2a 2a 20 20 20 20 7b 61 22 62 63 7d 20 20 2d 3e  **    {a"bc}  ->
260ef 20 20 7b 22 61 22 22 62 63 22 7d 0a 2a 2f 0a 73    {"a""bc"}.*/.s
260f0 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 51 75  tatic void setQu
260f1 6f 74 65 64 54 6f 6b 65 6e 28 50 61 72 73 65 20  otedToken(Parse 
260f2 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a  *pParse, Token *
260f3 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  p, const char *z
260f4 29 7b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69  ){..  /* Check i
260f5 66 20 74 68 65 20 73 74 72 69 6e 67 20 61 70 70  f the string app
260f6 65 61 72 73 20 74 6f 20 62 65 20 71 75 6f 74 65  ears to be quote
260f7 64 20 75 73 69 6e 67 20 22 2e 2e 2e 22 20 6f 72  d using "..." or
260f8 20 60 2e 2e 2e 60 0a 20 20 2a 2a 20 6f 72 20 5b   `...`.  ** or [
260f9 2e 2e 2e 5d 20 6f 72 20 27 2e 2e 2e 27 20 6f 72  ...] or '...' or
260fa 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 63   if the string c
260fb 6f 6e 74 61 69 6e 73 20 61 6e 79 20 22 20 63 68  ontains any " ch
260fc 61 72 61 63 74 65 72 73 2e 20 20 0a 20 20 2a 2a  aracters.  .  **
260fd 20 49 66 20 69 74 20 64 6f 65 73 2c 20 74 68 65   If it does, the
260fe 6e 20 72 65 63 6f 72 64 20 61 20 76 65 72 73 69  n record a versi
260ff 6f 6e 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  on of the string
26100 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61   with the specia
26101 6c 0a 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72  l.  ** character
26102 73 20 65 73 63 61 70 65 64 2e 0a 20 20 2a 2f 0a  s escaped..  */.
26103 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32    const char *z2
26104 20 3d 20 7a 3b 0a 20 20 69 66 28 20 2a 7a 32 21   = z;.  if( *z2!
26105 3d 27 5b 27 20 26 26 20 2a 7a 32 21 3d 27 60 27  ='[' && *z2!='`'
26106 20 26 26 20 2a 7a 32 21 3d 27 5c 27 27 20 29 7b   && *z2!='\'' ){
26107 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 32 20  .    while( *z2 
26108 29 7b 0a 20 20 20 20 20 20 69 66 28 20 2a 7a 32  ){.      if( *z2
26109 3d 3d 27 22 27 20 29 20 62 72 65 61 6b 3b 0a 20  =='"' ) break;. 
2610a 20 20 20 20 20 7a 32 2b 2b 3b 0a 20 20 20 20 7d       z2++;.    }
2610b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 2a 7a 32 20  .  }..  if( *z2 
2610c 29 7b 0a 20 20 20 20 2f 2a 20 53 74 72 69 6e 67  ){.    /* String
2610d 20 63 6f 6e 74 61 69 6e 73 20 22 20 63 68 61 72   contains " char
2610e 61 63 74 65 72 73 20 2d 20 63 6f 70 79 20 61 6e  acters - copy an
2610f 64 20 71 75 6f 74 65 20 74 68 65 20 73 74 72 69  d quote the stri
26110 6e 67 2e 20 2a 2f 0a 20 20 20 20 70 2d 3e 7a 20  ng. */.    p->z 
26111 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 4d  = (u8 *)sqlite3M
26112 50 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64  Printf(pParse->d
26113 62 2c 20 22 5c 22 25 77 5c 22 22 2c 20 7a 29 3b  b, "\"%w\"", z);
26114 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 20 29 7b  .    if( p->z ){
26115 0a 20 20 20 20 20 20 70 2d 3e 6e 20 3d 20 73 71  .      p->n = sq
26116 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 28 63  lite3Strlen30((c
26117 68 61 72 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20  har *)p->z);.   
26118 20 20 20 70 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20     p->dyn = 1;. 
26119 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
2611a 20 20 2f 2a 20 53 74 72 69 6e 67 20 63 6f 6e 74    /* String cont
2611b 61 69 6e 73 20 6e 6f 20 22 20 63 68 61 72 61 63  ains no " charac
2611c 74 65 72 73 20 2d 20 63 6f 70 79 20 74 68 65 20  ters - copy the 
2611d 70 6f 69 6e 74 65 72 2e 20 2a 2f 0a 20 20 20 20  pointer. */.    
2611e 70 2d 3e 7a 20 3d 20 28 75 38 2a 29 7a 3b 0a 20  p->z = (u8*)z;. 
2611f 20 20 20 70 2d 3e 6e 20 3d 20 28 69 6e 74 29 28     p->n = (int)(
26120 7a 32 20 2d 20 7a 29 3b 0a 20 20 20 20 70 2d 3e  z2 - z);.    p->
26121 64 79 6e 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  dyn = 0;.  }.}..
26122 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e 20  /*.** Create an 
26123 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
26124 66 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65  for an identifie
26125 72 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 20  r with the name 
26126 6f 66 20 7a 4e 61 6d 65 0a 2a 2f 0a 53 51 4c 49  of zName.*/.SQLI
26127 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
26128 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64  *sqlite3CreateId
26129 45 78 70 72 28 50 61 72 73 65 20 2a 70 50 61 72  Expr(Parse *pPar
2612a 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
2612b 7a 4e 61 6d 65 29 7b 0a 20 20 54 6f 6b 65 6e 20  zName){.  Token 
2612c 64 75 6d 6d 79 3b 0a 20 20 73 65 74 54 6f 6b 65  dummy;.  setToke
2612d 6e 28 26 64 75 6d 6d 79 2c 20 7a 4e 61 6d 65 29  n(&dummy, zName)
2612e 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
2612f 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
26130 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 64 75  TK_ID, 0, 0, &du
26131 6d 6d 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  mmy);.}../*.** A
26132 64 64 20 61 20 74 65 72 6d 20 74 6f 20 74 68 65  dd a term to the
26133 20 57 48 45 52 45 20 65 78 70 72 65 73 73 69 6f   WHERE expressio
26134 6e 20 69 6e 20 2a 70 70 45 78 70 72 20 74 68 61  n in *ppExpr tha
26135 74 20 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a  t requires the.*
26136 2a 20 7a 43 6f 6c 20 63 6f 6c 75 6d 6e 20 74 6f  * zCol column to
26137 20 62 65 20 65 71 75 61 6c 20 69 6e 20 74 68 65   be equal in the
26138 20 74 77 6f 20 74 61 62 6c 65 73 20 70 54 61 62   two tables pTab
26139 31 20 61 6e 64 20 70 54 61 62 32 2e 0a 2a 2f 0a  1 and pTab2..*/.
2613a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 57  static void addW
2613b 68 65 72 65 54 65 72 6d 28 0a 20 20 50 61 72 73  hereTerm(.  Pars
2613c 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2613d 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2613e 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e  context */.  con
2613f 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20  st char *zCol,  
26140 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
26141 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20   the column */. 
26142 20 63 6f 6e 73 74 20 54 61 62 6c 65 20 2a 70 54   const Table *pT
26143 61 62 31 2c 20 20 20 20 20 20 2f 2a 20 46 69 72  ab1,      /* Fir
26144 73 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f  st table */.  co
26145 6e 73 74 20 63 68 61 72 20 2a 7a 41 6c 69 61 73  nst char *zAlias
26146 31 2c 20 20 20 20 20 2f 2a 20 41 6c 69 61 73 20  1,     /* Alias 
26147 66 6f 72 20 66 69 72 73 74 20 74 61 62 6c 65 2e  for first table.
26148 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f    May be NULL */
26149 0a 20 20 63 6f 6e 73 74 20 54 61 62 6c 65 20 2a  .  const Table *
2614a 70 54 61 62 32 2c 20 20 20 20 20 20 2f 2a 20 53  pTab2,      /* S
2614b 65 63 6f 6e 64 20 74 61 62 6c 65 20 2a 2f 0a 20  econd table */. 
2614c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 6c   const char *zAl
2614d 69 61 73 32 2c 20 20 20 20 20 2f 2a 20 41 6c 69  ias2,     /* Ali
2614e 61 73 20 66 6f 72 20 73 65 63 6f 6e 64 20 74 61  as for second ta
2614f 62 6c 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  ble.  May be NUL
26150 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 52 69 67 68  L */.  int iRigh
26151 74 4a 6f 69 6e 54 61 62 6c 65 2c 20 20 20 20 20  tJoinTable,     
26152 2f 2a 20 56 44 42 45 20 63 75 72 73 6f 72 20 66  /* VDBE cursor f
26153 6f 72 20 74 68 65 20 72 69 67 68 74 20 74 61 62  or the right tab
26154 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 70  le */.  Expr **p
26155 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20  pExpr,          
26156 20 2f 2a 20 41 64 64 20 74 68 65 20 65 71 75 61   /* Add the equa
26157 6c 69 74 79 20 74 65 72 6d 20 74 6f 20 74 68 69  lity term to thi
26158 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  s expression */.
26159 20 20 69 6e 74 20 69 73 4f 75 74 65 72 4a 6f 69    int isOuterJoi
2615a 6e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  n          /* Tr
2615b 75 65 20 69 66 20 64 65 61 6c 69 6e 67 20 77 69  ue if dealing wi
2615c 74 68 20 61 6e 20 4f 55 54 45 52 20 6a 6f 69 6e  th an OUTER join
2615d 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
2615e 45 31 61 2c 20 2a 70 45 31 62 2c 20 2a 70 45 31  E1a, *pE1b, *pE1
2615f 63 3b 0a 20 20 45 78 70 72 20 2a 70 45 32 61 2c  c;.  Expr *pE2a,
26160 20 2a 70 45 32 62 2c 20 2a 70 45 32 63 3b 0a 20   *pE2b, *pE2c;. 
26161 20 45 78 70 72 20 2a 70 45 3b 0a 0a 20 20 70 45   Expr *pE;..  pE
26162 31 61 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  1a = sqlite3Crea
26163 74 65 49 64 45 78 70 72 28 70 50 61 72 73 65 2c  teIdExpr(pParse,
26164 20 7a 43 6f 6c 29 3b 0a 20 20 70 45 32 61 20 3d   zCol);.  pE2a =
26165 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64   sqlite3CreateId
26166 45 78 70 72 28 70 50 61 72 73 65 2c 20 7a 43 6f  Expr(pParse, zCo
26167 6c 29 3b 0a 20 20 69 66 28 20 7a 41 6c 69 61 73  l);.  if( zAlias
26168 31 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 41 6c 69  1==0 ){.    zAli
26169 61 73 31 20 3d 20 70 54 61 62 31 2d 3e 7a 4e 61  as1 = pTab1->zNa
2616a 6d 65 3b 0a 20 20 7d 0a 20 20 70 45 31 62 20 3d  me;.  }.  pE1b =
2616b 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64   sqlite3CreateId
2616c 45 78 70 72 28 70 50 61 72 73 65 2c 20 7a 41 6c  Expr(pParse, zAl
2616d 69 61 73 31 29 3b 0a 20 20 69 66 28 20 7a 41 6c  ias1);.  if( zAl
2616e 69 61 73 32 3d 3d 30 20 29 7b 0a 20 20 20 20 7a  ias2==0 ){.    z
2616f 41 6c 69 61 73 32 20 3d 20 70 54 61 62 32 2d 3e  Alias2 = pTab2->
26170 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 70 45 32  zName;.  }.  pE2
26171 62 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  b = sqlite3Creat
26172 65 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20  eIdExpr(pParse, 
26173 7a 41 6c 69 61 73 32 29 3b 0a 20 20 70 45 31 63  zAlias2);.  pE1c
26174 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
26175 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
26176 70 45 31 62 2c 20 70 45 31 61 2c 20 30 29 3b 0a  pE1b, pE1a, 0);.
26177 20 20 70 45 32 63 20 3d 20 73 71 6c 69 74 65 33    pE2c = sqlite3
26178 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
26179 5f 44 4f 54 2c 20 70 45 32 62 2c 20 70 45 32 61  _DOT, pE2b, pE2a
2617a 2c 20 30 29 3b 0a 20 20 70 45 20 3d 20 73 71 6c  , 0);.  pE = sql
2617b 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2617c 2c 20 54 4b 5f 45 51 2c 20 70 45 31 63 2c 20 70  , TK_EQ, pE1c, p
2617d 45 32 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  E2c, 0);.  if( p
2617e 45 20 26 26 20 69 73 4f 75 74 65 72 4a 6f 69 6e  E && isOuterJoin
2617f 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50   ){.    ExprSetP
26180 72 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46  roperty(pE, EP_F
26181 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 70 45  romJoin);.    pE
26182 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  ->iRightJoinTabl
26183 65 20 3d 20 69 52 69 67 68 74 4a 6f 69 6e 54 61  e = iRightJoinTa
26184 62 6c 65 3b 0a 20 20 7d 0a 20 20 2a 70 70 45 78  ble;.  }.  *ppEx
26185 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  pr = sqlite3Expr
26186 41 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 2a  And(pParse->db,*
26187 70 70 45 78 70 72 2c 20 70 45 29 3b 0a 7d 0a 0a  ppExpr, pE);.}..
26188 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 50  /*.** Set the EP
26189 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72  _FromJoin proper
2618a 74 79 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20  ty on all terms 
2618b 6f 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  of the given exp
2618c 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 41 6e 64 20  ression..** And 
2618d 73 65 74 20 74 68 65 20 45 78 70 72 2e 69 52 69  set the Expr.iRi
2618e 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74 6f 20  ghtJoinTable to 
2618f 69 54 61 62 6c 65 20 66 6f 72 20 65 76 65 72 79  iTable for every
26190 20 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20   term in the.** 
26191 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a  expression..**.*
26192 2a 20 54 68 65 20 45 50 5f 46 72 6f 6d 4a 6f 69  * The EP_FromJoi
26193 6e 20 70 72 6f 70 65 72 74 79 20 69 73 20 75 73  n property is us
26194 65 64 20 6f 6e 20 74 65 72 6d 73 20 6f 66 20 61  ed on terms of a
26195 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  n expression to 
26196 74 65 6c 6c 0a 2a 2a 20 74 68 65 20 4c 45 46 54  tell.** the LEFT
26197 20 4f 55 54 45 52 20 4a 4f 49 4e 20 70 72 6f 63   OUTER JOIN proc
26198 65 73 73 69 6e 67 20 6c 6f 67 69 63 20 74 68 61  essing logic tha
26199 74 20 74 68 69 73 20 74 65 72 6d 20 69 73 20 70  t this term is p
2619a 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6a 6f  art of the.** jo
2619b 69 6e 20 72 65 73 74 72 69 63 74 69 6f 6e 20 73  in restriction s
2619c 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
2619d 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
2619e 73 65 20 61 6e 64 20 6e 6f 74 20 61 20 70 61 72  se and not a par
2619f 74 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 6f 72 65  t.** of the more
261a0 20 67 65 6e 65 72 61 6c 20 57 48 45 52 45 20 63   general WHERE c
261a1 6c 61 75 73 65 2e 20 20 54 68 65 73 65 20 74 65  lause.  These te
261a2 72 6d 73 20 61 72 65 20 6d 6f 76 65 64 20 6f 76  rms are moved ov
261a3 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 57 48 45  er to the.** WHE
261a4 52 45 20 63 6c 61 75 73 65 20 64 75 72 69 6e 67  RE clause during
261a5 20 6a 6f 69 6e 20 70 72 6f 63 65 73 73 69 6e 67   join processing
261a6 20 62 75 74 20 77 65 20 6e 65 65 64 20 74 6f 20   but we need to 
261a7 72 65 6d 65 6d 62 65 72 20 74 68 61 74 20 74 68  remember that th
261a8 65 79 0a 2a 2a 20 6f 72 69 67 69 6e 61 74 65 64  ey.** originated
261a9 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53   in the ON or US
261aa 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a  ING clause..**.*
261ab 2a 20 54 68 65 20 45 78 70 72 2e 69 52 69 67 68  * The Expr.iRigh
261ac 74 4a 6f 69 6e 54 61 62 6c 65 20 74 65 6c 6c 73  tJoinTable tells
261ad 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
261ae 65 20 70 72 6f 63 65 73 73 69 6e 67 20 74 68 61  e processing tha
261af 74 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73  t the.** express
261b0 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 20 74  ion depends on t
261b1 61 62 6c 65 20 69 52 69 67 68 74 4a 6f 69 6e 54  able iRightJoinT
261b2 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 61  able even if tha
261b3 74 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 0a 2a  t table is not.*
261b4 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 6d 65 6e  * explicitly men
261b5 74 69 6f 6e 65 64 20 69 6e 20 74 68 65 20 65 78  tioned in the ex
261b6 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 61 74 20  pression.  That 
261b7 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6e  information is n
261b8 65 65 64 65 64 0a 2a 2a 20 66 6f 72 20 63 61 73  eeded.** for cas
261b9 65 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  es like this:.**
261ba 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  .**    SELECT * 
261bb 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49  FROM t1 LEFT JOI
261bc 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e  N t2 ON t1.a=t2.
261bd 62 20 41 4e 44 20 74 31 2e 78 3d 35 0a 2a 2a 0a  b AND t1.x=5.**.
261be 2a 2a 20 54 68 65 20 77 68 65 72 65 20 63 6c 61  ** The where cla
261bf 75 73 65 20 6e 65 65 64 73 20 74 6f 20 64 65 66  use needs to def
261c0 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20  er the handling 
261c1 6f 66 20 74 68 65 20 74 31 2e 78 3d 35 0a 2a 2a  of the t1.x=5.**
261c2 20 74 65 72 6d 20 75 6e 74 69 6c 20 61 66 74 65   term until afte
261c3 72 20 74 68 65 20 74 32 20 6c 6f 6f 70 20 6f 66  r the t2 loop of
261c4 20 74 68 65 20 6a 6f 69 6e 2e 20 20 49 6e 20 74   the join.  In t
261c5 68 61 74 20 77 61 79 2c 20 61 0a 2a 2a 20 4e 55  hat way, a.** NU
261c6 4c 4c 20 74 32 20 72 6f 77 20 77 69 6c 6c 20 62  LL t2 row will b
261c7 65 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e 65  e inserted whene
261c8 76 65 72 20 74 31 2e 78 21 3d 35 2e 20 20 49 66  ver t1.x!=5.  If
261c9 20 77 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65   we do not.** de
261ca 66 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67  fer the handling
261cb 20 6f 66 20 74 31 2e 78 3d 35 2c 20 69 74 20 77   of t1.x=5, it w
261cc 69 6c 6c 20 62 65 20 70 72 6f 63 65 73 73 65 64  ill be processed
261cd 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20   immediately.** 
261ce 61 66 74 65 72 20 74 68 65 20 74 31 20 6c 6f 6f  after the t1 loo
261cf 70 20 61 6e 64 20 72 6f 77 73 20 77 69 74 68 20  p and rows with 
261d0 74 31 2e 78 21 3d 35 20 77 69 6c 6c 20 6e 65 76  t1.x!=5 will nev
261d1 65 72 20 61 70 70 65 61 72 20 69 6e 0a 2a 2a 20  er appear in.** 
261d2 74 68 65 20 6f 75 74 70 75 74 2c 20 77 68 69 63  the output, whic
261d3 68 20 69 73 20 69 6e 63 6f 72 72 65 63 74 2e 0a  h is incorrect..
261d4 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
261d5 65 74 4a 6f 69 6e 45 78 70 72 28 45 78 70 72 20  etJoinExpr(Expr 
261d6 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 29 7b  *p, int iTable){
261d7 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20  .  while( p ){. 
261d8 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
261d9 74 79 28 70 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69  ty(p, EP_FromJoi
261da 6e 29 3b 0a 20 20 20 20 70 2d 3e 69 52 69 67 68  n);.    p->iRigh
261db 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20 69 54 61  tJoinTable = iTa
261dc 62 6c 65 3b 0a 20 20 20 20 73 65 74 4a 6f 69 6e  ble;.    setJoin
261dd 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 69  Expr(p->pLeft, i
261de 54 61 62 6c 65 29 3b 0a 20 20 20 20 70 20 3d 20  Table);.    p = 
261df 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 7d 20 0a  p->pRight;.  } .
261e0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
261e1 75 74 69 6e 65 20 70 72 6f 63 65 73 73 65 73 20  utine processes 
261e2 74 68 65 20 6a 6f 69 6e 20 69 6e 66 6f 72 6d 61  the join informa
261e3 74 69 6f 6e 20 66 6f 72 20 61 20 53 45 4c 45 43  tion for a SELEC
261e4 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  T statement..** 
261e5 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61  ON and USING cla
261e6 75 73 65 73 20 61 72 65 20 63 6f 6e 76 65 72 74  uses are convert
261e7 65 64 20 69 6e 74 6f 20 65 78 74 72 61 20 74 65  ed into extra te
261e8 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45  rms of the WHERE
261e9 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 4e 41 54 55   clause..** NATU
261ea 52 41 4c 20 6a 6f 69 6e 73 20 61 6c 73 6f 20 63  RAL joins also c
261eb 72 65 61 74 65 20 65 78 74 72 61 20 57 48 45 52  reate extra WHER
261ec 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a  E clause terms..
261ed 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20  **.** The terms 
261ee 6f 66 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65  of a FROM clause
261ef 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69   are contained i
261f0 6e 20 74 68 65 20 53 65 6c 65 63 74 2e 70 53 72  n the Select.pSr
261f1 63 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  c structure..** 
261f2 54 68 65 20 6c 65 66 74 20 6d 6f 73 74 20 74 61  The left most ta
261f3 62 6c 65 20 69 73 20 74 68 65 20 66 69 72 73 74  ble is the first
261f4 20 65 6e 74 72 79 20 69 6e 20 53 65 6c 65 63 74   entry in Select
261f5 2e 70 53 72 63 2e 20 20 54 68 65 20 72 69 67 68  .pSrc.  The righ
261f6 74 2d 6d 6f 73 74 0a 2a 2a 20 74 61 62 6c 65 20  t-most.** table 
261f7 69 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  is the last entr
261f8 79 2e 20 20 54 68 65 20 6a 6f 69 6e 20 6f 70 65  y.  The join ope
261f9 72 61 74 6f 72 20 69 73 20 68 65 6c 64 20 69 6e  rator is held in
261fa 20 74 68 65 20 65 6e 74 72 79 20 74 6f 0a 2a 2a   the entry to.**
261fb 20 74 68 65 20 6c 65 66 74 2e 20 20 54 68 75 73   the left.  Thus
261fc 20 65 6e 74 72 79 20 30 20 63 6f 6e 74 61 69 6e   entry 0 contain
261fd 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61  s the join opera
261fe 74 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 69 6e  tor for the join
261ff 20 62 65 74 77 65 65 6e 0a 2a 2a 20 65 6e 74 72   between.** entr
26200 69 65 73 20 30 20 61 6e 64 20 31 2e 20 20 41 6e  ies 0 and 1.  An
26201 79 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  y ON or USING cl
26202 61 75 73 65 73 20 61 73 73 6f 63 69 61 74 65 64  auses associated
26203 20 77 69 74 68 20 74 68 65 20 6a 6f 69 6e 20 61   with the join a
26204 72 65 0a 2a 2a 20 61 6c 73 6f 20 61 74 74 61 63  re.** also attac
26205 68 65 64 20 74 6f 20 74 68 65 20 6c 65 66 74 20  hed to the left 
26206 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  entry..**.** Thi
26207 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
26208 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
26209 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72  errors encounter
2620a 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
2620b 74 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a  t sqliteProcessJ
2620c 6f 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  oin(Parse *pPars
2620d 65 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  e, Select *p){. 
2620e 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20   SrcList *pSrc; 
2620f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26210 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20 69   /* All tables i
26211 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
26212 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  e */.  int i, j;
26213 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26214 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
26215 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 73 74 72  ounters */.  str
26216 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
26217 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20   *pLeft;     /* 
26218 4c 65 66 74 20 74 61 62 6c 65 20 62 65 69 6e 67  Left table being
26219 20 6a 6f 69 6e 65 64 20 2a 2f 0a 20 20 73 74 72   joined */.  str
2621a 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2621b 20 2a 70 52 69 67 68 74 3b 20 20 20 20 2f 2a 20   *pRight;    /* 
2621c 52 69 67 68 74 20 74 61 62 6c 65 20 62 65 69 6e  Right table bein
2621d 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a 0a 20 20 70  g joined */..  p
2621e 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  Src = p->pSrc;. 
2621f 20 70 4c 65 66 74 20 3d 20 26 70 53 72 63 2d 3e   pLeft = &pSrc->
26220 61 5b 30 5d 3b 0a 20 20 70 52 69 67 68 74 20 3d  a[0];.  pRight =
26221 20 26 70 4c 65 66 74 5b 31 5d 3b 0a 20 20 66 6f   &pLeft[1];.  fo
26222 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e  r(i=0; i<pSrc->n
26223 53 72 63 2d 31 3b 20 69 2b 2b 2c 20 70 52 69 67  Src-1; i++, pRig
26224 68 74 2b 2b 2c 20 70 4c 65 66 74 2b 2b 29 7b 0a  ht++, pLeft++){.
26225 20 20 20 20 54 61 62 6c 65 20 2a 70 4c 65 66 74      Table *pLeft
26226 54 61 62 20 3d 20 70 4c 65 66 74 2d 3e 70 54 61  Tab = pLeft->pTa
26227 62 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 52  b;.    Table *pR
26228 69 67 68 74 54 61 62 20 3d 20 70 52 69 67 68 74  ightTab = pRight
26229 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 6e 74 20  ->pTab;.    int 
2622a 69 73 4f 75 74 65 72 3b 0a 0a 20 20 20 20 69 66  isOuter;..    if
2622b 28 20 4e 45 56 45 52 28 70 4c 65 66 74 54 61 62  ( NEVER(pLeftTab
2622c 3d 3d 30 20 7c 7c 20 70 52 69 67 68 74 54 61 62  ==0 || pRightTab
2622d 3d 3d 30 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ==0) ) continue;
2622e 0a 20 20 20 20 69 73 4f 75 74 65 72 20 3d 20 28  .    isOuter = (
2622f 70 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65  pRight->jointype
26230 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 3b   & JT_OUTER)!=0;
26231 0a 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68  ..    /* When th
26232 65 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72  e NATURAL keywor
26233 64 20 69 73 20 70 72 65 73 65 6e 74 2c 20 61 64  d is present, ad
26234 64 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  d WHERE clause t
26235 65 72 6d 73 20 66 6f 72 0a 20 20 20 20 2a 2a 20  erms for.    ** 
26236 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 74 68 61  every column tha
26237 74 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73  t the two tables
26238 20 68 61 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e   have in common.
26239 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
2623a 70 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65  pRight->jointype
2623b 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b   & JT_NATURAL ){
2623c 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68  .      if( pRigh
2623d 74 2d 3e 70 4f 6e 20 7c 7c 20 70 52 69 67 68 74  t->pOn || pRight
2623e 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20  ->pUsing ){.    
2623f 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
26240 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 4e  Msg(pParse, "a N
26241 41 54 55 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 20  ATURAL join may 
26242 6e 6f 74 20 68 61 76 65 20 22 0a 20 20 20 20 20  not have ".     
26243 20 20 20 20 20 20 22 61 6e 20 4f 4e 20 6f 72 20        "an ON or 
26244 55 53 49 4e 47 20 63 6c 61 75 73 65 22 2c 20 30  USING clause", 0
26245 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
26246 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
26247 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c     for(j=0; j<pL
26248 65 66 74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b  eftTab->nCol; j+
26249 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72  +){.        char
2624a 20 2a 7a 4e 61 6d 65 20 3d 20 70 4c 65 66 74 54   *zName = pLeftT
2624b 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d  ab->aCol[j].zNam
2624c 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  e;.        if( c
2624d 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 52 69 67 68  olumnIndex(pRigh
2624e 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20  tTab, zName)>=0 
2624f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64  ){.          add
26250 57 68 65 72 65 54 65 72 6d 28 70 50 61 72 73 65  WhereTerm(pParse
26251 2c 20 7a 4e 61 6d 65 2c 20 70 4c 65 66 74 54 61  , zName, pLeftTa
26252 62 2c 20 70 4c 65 66 74 2d 3e 7a 41 6c 69 61 73  b, pLeft->zAlias
26253 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
26254 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26255 20 70 52 69 67 68 74 54 61 62 2c 20 70 52 69 67   pRightTab, pRig
26256 68 74 2d 3e 7a 41 6c 69 61 73 2c 0a 20 20 20 20  ht->zAlias,.    
26257 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26258 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74            pRight
26259 2d 3e 69 43 75 72 73 6f 72 2c 20 26 70 2d 3e 70  ->iCursor, &p->p
2625a 57 68 65 72 65 2c 20 69 73 4f 75 74 65 72 29 3b  Where, isOuter);
2625b 0a 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20  .          .    
2625c 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2625d 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61    }..    /* Disa
2625e 6c 6c 6f 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64  llow both ON and
2625f 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 69   USING clauses i
26260 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0a  n the same join.
26261 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
26262 52 69 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52  Right->pOn && pR
26263 69 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a  ight->pUsing ){.
26264 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
26265 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
26266 61 6e 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20  annot have both 
26267 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 22 0a 20  ON and USING ". 
26268 20 20 20 20 20 20 20 22 63 6c 61 75 73 65 73 20         "clauses 
26269 69 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e  in the same join
2626a 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ");.      return
2626b 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   1;.    }..    /
2626c 2a 20 41 64 64 20 74 68 65 20 4f 4e 20 63 6c 61  * Add the ON cla
2626d 75 73 65 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  use to the end o
2626e 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
2626f 73 65 2c 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  se, connected by
26270 0a 20 20 20 20 2a 2a 20 61 6e 20 41 4e 44 20 6f  .    ** an AND o
26271 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a  perator..    */.
26272 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
26273 70 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28  pOn ){.      if(
26274 20 69 73 4f 75 74 65 72 20 29 20 73 65 74 4a 6f   isOuter ) setJo
26275 69 6e 45 78 70 72 28 70 52 69 67 68 74 2d 3e 70  inExpr(pRight->p
26276 4f 6e 2c 20 70 52 69 67 68 74 2d 3e 69 43 75 72  On, pRight->iCur
26277 73 6f 72 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70  sor);.      p->p
26278 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
26279 78 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64  xprAnd(pParse->d
2627a 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 70 52  b, p->pWhere, pR
2627b 69 67 68 74 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20  ight->pOn);.    
2627c 20 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 3d 20    pRight->pOn = 
2627d 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  0;.    }..    /*
2627e 20 43 72 65 61 74 65 20 65 78 74 72 61 20 74 65   Create extra te
2627f 72 6d 73 20 6f 6e 20 74 68 65 20 57 48 45 52 45  rms on the WHERE
26280 20 63 6c 61 75 73 65 20 66 6f 72 20 65 61 63 68   clause for each
26281 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 0a 20 20   column named.  
26282 20 20 2a 2a 20 69 6e 20 74 68 65 20 55 53 49 4e    ** in the USIN
26283 47 20 63 6c 61 75 73 65 2e 20 20 45 78 61 6d 70  G clause.  Examp
26284 6c 65 3a 20 49 66 20 74 68 65 20 74 77 6f 20 74  le: If the two t
26285 61 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e  ables to be join
26286 65 64 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 41  ed are .    ** A
26287 20 61 6e 64 20 42 20 61 6e 64 20 74 68 65 20 55   and B and the U
26288 53 49 4e 47 20 63 6c 61 75 73 65 20 6e 61 6d 65  SING clause name
26289 73 20 58 2c 20 59 2c 20 61 6e 64 20 5a 2c 20 74  s X, Y, and Z, t
2628a 68 65 6e 20 61 64 64 20 74 68 69 73 0a 20 20 20  hen add this.   
2628b 20 2a 2a 20 74 6f 20 74 68 65 20 57 48 45 52 45   ** to the WHERE
2628c 20 63 6c 61 75 73 65 3a 20 20 20 20 41 2e 58 3d   clause:    A.X=
2628d 42 2e 58 20 41 4e 44 20 41 2e 59 3d 42 2e 59 20  B.X AND A.Y=B.Y 
2628e 41 4e 44 20 41 2e 5a 3d 42 2e 5a 0a 20 20 20 20  AND A.Z=B.Z.    
2628f 2a 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72  ** Report an err
26290 6f 72 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e  or if any column
26291 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68   mentioned in th
26292 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 69  e USING clause i
26293 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6e  s.    ** not con
26294 74 61 69 6e 65 64 20 69 6e 20 62 6f 74 68 20 74  tained in both t
26295 61 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e  ables to be join
26296 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
26297 66 28 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e  f( pRight->pUsin
26298 67 20 29 7b 0a 20 20 20 20 20 20 49 64 4c 69 73  g ){.      IdLis
26299 74 20 2a 70 4c 69 73 74 20 3d 20 70 52 69 67 68  t *pList = pRigh
2629a 74 2d 3e 70 55 73 69 6e 67 3b 0a 20 20 20 20 20  t->pUsing;.     
2629b 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 69 73   for(j=0; j<pLis
2629c 74 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20  t->nId; j++){.  
2629d 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
2629e 65 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e  e = pList->a[j].
2629f 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69  zName;.        i
262a0 66 28 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70  f( columnIndex(p
262a1 4c 65 66 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3c  LeftTab, zName)<
262a2 30 20 7c 7c 20 63 6f 6c 75 6d 6e 49 6e 64 65 78  0 || columnIndex
262a3 28 70 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d  (pRightTab, zNam
262a4 65 29 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e)<0 ){.        
262a5 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
262a6 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
262a7 74 20 6a 6f 69 6e 20 75 73 69 6e 67 20 63 6f 6c  t join using col
262a8 75 6d 6e 20 25 73 20 2d 20 63 6f 6c 75 6d 6e 20  umn %s - column 
262a9 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 6e  ".            "n
262aa 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 62 6f  ot present in bo
262ab 74 68 20 74 61 62 6c 65 73 22 2c 20 7a 4e 61 6d  th tables", zNam
262ac 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  e);.          re
262ad 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20  turn 1;.        
262ae 7d 0a 20 20 20 20 20 20 20 20 61 64 64 57 68 65  }.        addWhe
262af 72 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20 7a  reTerm(pParse, z
262b0 4e 61 6d 65 2c 20 70 4c 65 66 74 54 61 62 2c 20  Name, pLeftTab, 
262b1 70 4c 65 66 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a  pLeft->zAlias, .
262b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262b3 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67              pRig
262b4 68 74 54 61 62 2c 20 70 52 69 67 68 74 2d 3e 7a  htTab, pRight->z
262b5 41 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20  Alias,.         
262b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262b7 20 20 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73     pRight->iCurs
262b8 6f 72 2c 20 26 70 2d 3e 70 57 68 65 72 65 2c 20  or, &p->pWhere, 
262b9 69 73 4f 75 74 65 72 29 3b 0a 20 20 20 20 20 20  isOuter);.      
262ba 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
262bb 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
262bc 20 49 6e 73 65 72 74 20 63 6f 64 65 20 69 6e 74   Insert code int
262bd 6f 20 22 76 22 20 74 68 61 74 20 77 69 6c 6c 20  o "v" that will 
262be 70 75 73 68 20 74 68 65 20 72 65 63 6f 72 64 20  push the record 
262bf 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  on the top of th
262c0 65 0a 2a 2a 20 73 74 61 63 6b 20 69 6e 74 6f 20  e.** stack into 
262c1 74 68 65 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73  the sorter..*/.s
262c2 74 61 74 69 63 20 76 6f 69 64 20 70 75 73 68 4f  tatic void pushO
262c3 6e 74 6f 53 6f 72 74 65 72 28 0a 20 20 50 61 72  ntoSorter(.  Par
262c4 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
262c5 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
262c6 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
262c7 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
262c8 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
262c9 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65  Y clause */.  Se
262ca 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
262cb 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c       /* The whol
262cc 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
262cd 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44  nt */.  int regD
262ce 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20 2f  ata            /
262cf 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
262d0 6e 67 20 64 61 74 61 20 74 6f 20 62 65 20 73 6f  ng data to be so
262d1 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  rted */.){.  Vdb
262d2 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
262d3 56 64 62 65 3b 0a 20 20 69 6e 74 20 6e 45 78 70  Vdbe;.  int nExp
262d4 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45  r = pOrderBy->nE
262d5 78 70 72 3b 0a 20 20 69 6e 74 20 72 65 67 42 61  xpr;.  int regBa
262d6 73 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  se = sqlite3GetT
262d7 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
262d8 20 6e 45 78 70 72 2b 32 29 3b 0a 20 20 69 6e 74   nExpr+2);.  int
262d9 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c   regRecord = sql
262da 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
262db 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
262dc 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
262dd 74 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  t(pParse, pOrder
262de 42 79 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b  By, regBase, 0);
262df 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
262e0 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65  dOp2(v, OP_Seque
262e1 6e 63 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69  nce, pOrderBy->i
262e2 45 43 75 72 73 6f 72 2c 20 72 65 67 42 61 73 65  ECursor, regBase
262e3 2b 6e 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74  +nExpr);.  sqlit
262e4 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70  e3ExprCodeMove(p
262e5 50 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c 20  Parse, regData, 
262e6 72 65 67 42 61 73 65 2b 6e 45 78 70 72 2b 31 2c  regBase+nExpr+1,
262e7 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   1);.  sqlite3Vd
262e8 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
262e9 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61  akeRecord, regBa
262ea 73 65 2c 20 6e 45 78 70 72 20 2b 20 32 2c 20 72  se, nExpr + 2, r
262eb 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c  egRecord);.  sql
262ec 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
262ed 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
262ee 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73  pOrderBy->iECurs
262ef 6f 72 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a  or, regRecord);.
262f0 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
262f1 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
262f2 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71  regRecord);.  sq
262f3 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
262f4 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
262f5 67 42 61 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b  gBase, nExpr+2);
262f6 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
262f7 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 69 6e  iLimit ){.    in
262f8 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 0a  t addr1, addr2;.
262f9 20 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 0a      int iLimit;.
262fa 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d      if( pSelect-
262fb 3e 69 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  >iOffset ){.    
262fc 20 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65    iLimit = pSele
262fd 63 74 2d 3e 69 4f 66 66 73 65 74 2b 31 3b 0a 20  ct->iOffset+1;. 
262fe 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
262ff 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74  iLimit = pSelect
26300 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 7d 0a  ->iLimit;.    }.
26301 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
26302 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
26303 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d   OP_IfZero, iLim
26304 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  it);.    sqlite3
26305 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26306 5f 41 64 64 49 6d 6d 2c 20 69 4c 69 6d 69 74 2c  _AddImm, iLimit,
26307 20 2d 31 29 3b 0a 20 20 20 20 61 64 64 72 32 20   -1);.    addr2 
26308 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
26309 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b  Op0(v, OP_Goto);
2630a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2630b 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2630c 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2630d 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2630e 4c 61 73 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e  Last, pOrderBy->
2630f 69 45 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73  iECursor);.    s
26310 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
26311 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70  (v, OP_Delete, p
26312 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
26313 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  r);.    sqlite3V
26314 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
26315 64 64 72 32 29 3b 0a 20 20 20 20 70 53 65 6c 65  ddr2);.    pSele
26316 63 74 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a  ct->iLimit = 0;.
26317 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64    }.}../*.** Add
26318 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
26319 6e 74 20 74 68 65 20 4f 46 46 53 45 54 0a 2a 2f  nt the OFFSET.*/
2631a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
2631b 65 4f 66 66 73 65 74 28 0a 20 20 56 64 62 65 20  eOffset(.  Vdbe 
2631c 2a 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  *v,          /* 
2631d 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
2631e 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20  to this VM */.  
2631f 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
26320 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
26321 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
26322 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  coded */.  int i
26323 43 6f 6e 74 69 6e 75 65 20 20 20 20 20 2f 2a 20  Continue     /* 
26324 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69  Jump here to ski
26325 70 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 65  p the current re
26326 63 6f 72 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  cord */.){.  if(
26327 20 70 2d 3e 69 4f 66 66 73 65 74 20 26 26 20 69   p->iOffset && i
26328 43 6f 6e 74 69 6e 75 65 21 3d 30 20 29 7b 0a 20  Continue!=0 ){. 
26329 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
2632a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2632b 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
2632c 20 70 2d 3e 69 4f 66 66 73 65 74 2c 20 2d 31 29   p->iOffset, -1)
2632d 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ;.    addr = sql
2632e 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2632f 2c 20 4f 50 5f 49 66 4e 65 67 2c 20 70 2d 3e 69  , OP_IfNeg, p->i
26330 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c  Offset);.    sql
26331 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26332 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43  , OP_Goto, 0, iC
26333 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 56 64  ontinue);.    Vd
26334 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73  beComment((v, "s
26335 6b 69 70 20 4f 46 46 53 45 54 20 72 65 63 6f 72  kip OFFSET recor
26336 64 73 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  ds"));.    sqlit
26337 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
26338 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a  , addr);.  }.}..
26339 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64 65 20 74  /*.** Add code t
2633a 68 61 74 20 77 69 6c 6c 20 63 68 65 63 6b 20 74  hat will check t
2633b 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
2633c 4e 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  N registers star
2633d 74 69 6e 67 20 61 74 20 69 4d 65 6d 0a 2a 2a 20  ting at iMem.** 
2633e 66 6f 72 6d 20 61 20 64 69 73 74 69 6e 63 74 20  form a distinct 
2633f 65 6e 74 72 79 2e 20 20 69 54 61 62 20 69 73 20  entry.  iTab is 
26340 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  a sorting index 
26341 74 68 61 74 20 68 6f 6c 64 73 20 70 72 65 76 69  that holds previ
26342 6f 75 73 6c 79 0a 2a 2a 20 73 65 65 6e 20 63 6f  ously.** seen co
26343 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 74 68  mbinations of th
26344 65 20 4e 20 76 61 6c 75 65 73 2e 20 20 41 20 6e  e N values.  A n
26345 65 77 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65  ew entry is made
26346 20 69 6e 20 69 54 61 62 0a 2a 2a 20 69 66 20 74   in iTab.** if t
26347 68 65 20 63 75 72 72 65 6e 74 20 4e 20 76 61 6c  he current N val
26348 75 65 73 20 61 72 65 20 6e 65 77 2e 0a 2a 2a 0a  ues are new..**.
26349 2a 2a 20 41 20 6a 75 6d 70 20 74 6f 20 61 64 64  ** A jump to add
2634a 72 52 65 70 65 61 74 20 69 73 20 6d 61 64 65 20  rRepeat is made 
2634b 61 6e 64 20 74 68 65 20 4e 2b 31 20 76 61 6c 75  and the N+1 valu
2634c 65 73 20 61 72 65 20 70 6f 70 70 65 64 20 66 72  es are popped fr
2634d 6f 6d 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20  om the.** stack 
2634e 69 66 20 74 68 65 20 74 6f 70 20 4e 20 65 6c 65  if the top N ele
2634f 6d 65 6e 74 73 20 61 72 65 20 6e 6f 74 20 64 69  ments are not di
26350 73 74 69 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74 69  stinct..*/.stati
26351 63 20 76 6f 69 64 20 63 6f 64 65 44 69 73 74 69  c void codeDisti
26352 6e 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  nct(.  Parse *pP
26353 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72  arse,     /* Par
26354 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65  sing and code ge
26355 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74  nerating context
26356 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20   */.  int iTab, 
26357 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 6f           /* A so
26358 72 74 69 6e 67 20 69 6e 64 65 78 20 75 73 65 64  rting index used
26359 20 74 6f 20 74 65 73 74 20 66 6f 72 20 64 69 73   to test for dis
2635a 74 69 6e 63 74 6e 65 73 73 20 2a 2f 0a 20 20 69  tinctness */.  i
2635b 6e 74 20 61 64 64 72 52 65 70 65 61 74 2c 20 20  nt addrRepeat,  
2635c 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72    /* Jump to her
2635d 65 20 69 66 20 6e 6f 74 20 64 69 73 74 69 6e 63  e if not distinc
2635e 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20  t */.  int N,   
2635f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
26360 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
26361 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20  */.  int iMem   
26362 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
26363 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20   element */.){. 
26364 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20   Vdbe *v;.  int 
26365 72 31 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73  r1;..  v = pPars
26366 65 2d 3e 70 56 64 62 65 3b 0a 20 20 72 31 20 3d  e->pVdbe;.  r1 =
26367 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
26368 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
26369 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2636a 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
2636b 2c 20 69 4d 65 6d 2c 20 4e 2c 20 72 31 29 3b 0a  , iMem, N, r1);.
2636c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2636d 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Op3(v, OP_Found,
2636e 20 69 54 61 62 2c 20 61 64 64 72 52 65 70 65 61   iTab, addrRepea
2636f 74 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65  t, r1);.  sqlite
26370 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26371 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 54 61  P_IdxInsert, iTa
26372 62 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65  b, r1);.  sqlite
26373 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
26374 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a  pParse, r1);.}..
26375 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
26376 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
26377 77 68 65 6e 20 61 20 53 45 4c 45 43 54 20 69 73  when a SELECT is
26378 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 73   used within a s
26379 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  ubexpression.** 
2637a 28 65 78 61 6d 70 6c 65 3a 20 20 22 61 20 49 4e  (example:  "a IN
2637b 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20   (SELECT * FROM 
2637c 74 61 62 6c 65 29 22 29 20 62 75 74 20 69 74 20  table)") but it 
2637d 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 31 20  has more than 1 
2637e 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e  result.** column
2637f 2e 20 20 57 65 20 64 6f 20 74 68 69 73 20 69 6e  .  We do this in
26380 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 62 65   a subroutine be
26381 63 61 75 73 65 20 74 68 65 20 65 72 72 6f 72 20  cause the error 
26382 6f 63 63 75 72 73 20 69 6e 20 6d 75 6c 74 69 70  occurs in multip
26383 6c 65 0a 2a 2a 20 70 6c 61 63 65 73 2e 0a 2a 2f  le.** places..*/
26384 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63  .static int chec
26385 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53  kForMultiColumnS
26386 65 6c 65 63 74 45 72 72 6f 72 28 0a 20 20 50 61  electError(.  Pa
26387 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
26388 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
26389 65 78 74 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ext. */.  Select
2638a 44 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20 2f  Dest *pDest,   /
2638b 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 6f 66  * Destination of
2638c 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20   SELECT results 
2638d 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 70 72 20 20  */.  int nExpr  
2638e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2638f 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f  ber of result co
26390 6c 75 6d 6e 73 20 72 65 74 75 72 6e 65 64 20 62  lumns returned b
26391 79 20 53 45 4c 45 43 54 20 2a 2f 0a 29 7b 0a 20  y SELECT */.){. 
26392 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65   int eDest = pDe
26393 73 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69 66 28  st->eDest;.  if(
26394 20 6e 45 78 70 72 3e 31 20 26 26 20 28 65 44 65   nExpr>1 && (eDe
26395 73 74 3d 3d 53 52 54 5f 4d 65 6d 20 7c 7c 20 65  st==SRT_Mem || e
26396 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 29 20 29  Dest==SRT_Set) )
26397 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
26398 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f  orMsg(pParse, "o
26399 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 65 73  nly a single res
2639a 75 6c 74 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20  ult allowed for 
2639b 22 0a 20 20 20 20 20 20 20 22 61 20 53 45 4c 45  ".       "a SELE
2639c 43 54 20 74 68 61 74 20 69 73 20 70 61 72 74 20  CT that is part 
2639d 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
2639e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  ");.    return 1
2639f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
263a0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  eturn 0;.  }.}..
263a1 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
263a2 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  ne generates the
263a3 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 69 6e   code for the in
263a4 73 69 64 65 20 6f 66 20 74 68 65 20 69 6e 6e 65  side of the inne
263a5 72 20 6c 6f 6f 70 0a 2a 2a 20 6f 66 20 61 20 53  r loop.** of a S
263a6 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ELECT..**.** If 
263a7 73 72 63 54 61 62 20 61 6e 64 20 6e 43 6f 6c 75  srcTab and nColu
263a8 6d 6e 20 61 72 65 20 62 6f 74 68 20 7a 65 72 6f  mn are both zero
263a9 2c 20 74 68 65 6e 20 74 68 65 20 70 45 4c 69 73  , then the pELis
263aa 74 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a  t expressions.**
263ab 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 69   are evaluated i
263ac 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 74  n order to get t
263ad 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73  he data for this
263ae 20 72 6f 77 2e 20 20 49 66 20 6e 43 6f 6c 75 6d   row.  If nColum
263af 6e 3e 30 0a 2a 2a 20 74 68 65 6e 20 64 61 74 61  n>0.** then data
263b0 20 69 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20   is pulled from 
263b1 73 72 63 54 61 62 20 61 6e 64 20 70 45 4c 69 73  srcTab and pELis
263b2 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74  t is used only t
263b3 6f 20 67 65 74 20 74 68 65 0a 2a 2a 20 64 61 74  o get the.** dat
263b4 61 74 79 70 65 73 20 66 6f 72 20 65 61 63 68 20  atypes for each 
263b5 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69  column..*/.stati
263b6 63 20 76 6f 69 64 20 73 65 6c 65 63 74 49 6e 6e  c void selectInn
263b7 65 72 4c 6f 6f 70 28 0a 20 20 50 61 72 73 65 20  erLoop(.  Parse 
263b8 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
263b9 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
263ba 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
263bb 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ect *p,         
263bc 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70       /* The comp
263bd 6c 65 74 65 20 73 65 6c 65 63 74 20 73 74 61 74  lete select stat
263be 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65  ement being code
263bf 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  d */.  ExprList 
263c0 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f  *pEList,       /
263c1 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73  * List of values
263c2 20 62 65 69 6e 67 20 65 78 74 72 61 63 74 65 64   being extracted
263c3 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63 54 61 62   */.  int srcTab
263c4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
263c5 20 50 75 6c 6c 20 64 61 74 61 20 66 72 6f 6d 20   Pull data from 
263c6 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
263c7 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20  int nColumn,    
263c8 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
263c9 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
263ca 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65  the source table
263cb 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
263cc 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a  pOrderBy,     /*
263cd 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 6f   If not NULL, so
263ce 72 74 20 72 65 73 75 6c 74 73 20 75 73 69 6e 67  rt results using
263cf 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 69   this key */.  i
263d0 6e 74 20 64 69 73 74 69 6e 63 74 2c 20 20 20 20  nt distinct,    
263d1 20 20 20 20 20 20 20 2f 2a 20 49 66 20 3e 3d 30         /* If >=0
263d2 2c 20 6d 61 6b 65 20 73 75 72 65 20 72 65 73 75  , make sure resu
263d3 6c 74 73 20 61 72 65 20 64 69 73 74 69 6e 63 74  lts are distinct
263d4 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
263d5 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f 2a   *pDest,      /*
263d6 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20   How to dispose 
263d7 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a  of the results *
263d8 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75  /.  int iContinu
263d9 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a  e,          /* J
263da 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74  ump here to cont
263db 69 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20 72  inue with next r
263dc 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65  ow */.  int iBre
263dd 61 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ak              
263de 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
263df 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
263e0 20 69 6e 6e 65 72 20 6c 6f 6f 70 20 2a 2f 0a 29   inner loop */.)
263e1 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
263e2 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
263e3 6e 74 20 69 3b 0a 20 20 69 6e 74 20 68 61 73 44  nt i;.  int hasD
263e4 69 73 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20  istinct;        
263e5 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 44  /* True if the D
263e6 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
263e7 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
263e8 69 6e 74 20 72 65 67 52 65 73 75 6c 74 3b 20 20  int regResult;  
263e9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
263ea 74 61 72 74 20 6f 66 20 6d 65 6d 6f 72 79 20 68  tart of memory h
263eb 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65  olding result se
263ec 74 20 2a 2f 0a 20 20 69 6e 74 20 65 44 65 73 74  t */.  int eDest
263ed 20 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b   = pDest->eDest;
263ee 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73     /* How to dis
263ef 70 6f 73 65 20 6f 66 20 72 65 73 75 6c 74 73 20  pose of results 
263f0 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 3d  */.  int iParm =
263f1 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 3b 20 20   pDest->iParm;  
263f2 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65   /* First argume
263f3 6e 74 20 74 6f 20 64 69 73 70 6f 73 61 6c 20 6d  nt to disposal m
263f4 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ethod */.  int n
263f5 52 65 73 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20  ResultCol;      
263f6 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
263f7 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
263f8 6e 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ns */..  assert(
263f9 20 76 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45   v );.  if( NEVE
263fa 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  R(v==0) ) return
263fb 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69  ;.  assert( pELi
263fc 73 74 21 3d 30 20 29 3b 0a 20 20 68 61 73 44 69  st!=0 );.  hasDi
263fd 73 74 69 6e 63 74 20 3d 20 64 69 73 74 69 6e 63  stinct = distinc
263fe 74 3e 3d 30 3b 0a 20 20 69 66 28 20 70 4f 72 64  t>=0;.  if( pOrd
263ff 65 72 42 79 3d 3d 30 20 26 26 20 21 68 61 73 44  erBy==0 && !hasD
26400 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 63  istinct ){.    c
26401 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20  odeOffset(v, p, 
26402 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 7d 0a  iContinue);.  }.
26403 0a 20 20 2f 2a 20 50 75 6c 6c 20 74 68 65 20 72  .  /* Pull the r
26404 65 71 75 65 73 74 65 64 20 63 6f 6c 75 6d 6e 73  equested columns
26405 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 43 6f  ..  */.  if( nCo
26406 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 6e 52  lumn>0 ){.    nR
26407 65 73 75 6c 74 43 6f 6c 20 3d 20 6e 43 6f 6c 75  esultCol = nColu
26408 6d 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  mn;.  }else{.   
26409 20 6e 52 65 73 75 6c 74 43 6f 6c 20 3d 20 70 45   nResultCol = pE
2640a 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d  List->nExpr;.  }
2640b 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 4d  .  if( pDest->iM
2640c 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 65  em==0 ){.    pDe
2640d 73 74 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73  st->iMem = pPars
2640e 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70  e->nMem+1;.    p
2640f 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 6e 52 65  Dest->nMem = nRe
26410 73 75 6c 74 43 6f 6c 3b 0a 20 20 20 20 70 50 61  sultCol;.    pPa
26411 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65  rse->nMem += nRe
26412 73 75 6c 74 43 6f 6c 3b 0a 20 20 7d 65 6c 73 65  sultCol;.  }else
26413 7b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  { .    assert( p
26414 44 65 73 74 2d 3e 6e 4d 65 6d 3d 3d 6e 52 65 73  Dest->nMem==nRes
26415 75 6c 74 43 6f 6c 20 29 3b 0a 20 20 7d 0a 20 20  ultCol );.  }.  
26416 72 65 67 52 65 73 75 6c 74 20 3d 20 70 44 65 73  regResult = pDes
26417 74 2d 3e 69 4d 65 6d 3b 0a 20 20 69 66 28 20 6e  t->iMem;.  if( n
26418 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20  Column>0 ){.    
26419 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75  for(i=0; i<nColu
2641a 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
2641b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2641c 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2641d 73 72 63 54 61 62 2c 20 69 2c 20 72 65 67 52 65  srcTab, i, regRe
2641e 73 75 6c 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20  sult+i);.    }. 
2641f 20 7d 65 6c 73 65 20 69 66 28 20 65 44 65 73 74   }else if( eDest
26420 21 3d 53 52 54 5f 45 78 69 73 74 73 20 29 7b 0a  !=SRT_Exists ){.
26421 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65      /* If the de
26422 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20  stination is an 
26423 45 58 49 53 54 53 28 2e 2e 2e 29 20 65 78 70 72  EXISTS(...) expr
26424 65 73 73 69 6f 6e 2c 20 74 68 65 20 61 63 74 75  ession, the actu
26425 61 6c 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73  al.    ** values
26426 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
26427 20 53 45 4c 45 43 54 20 61 72 65 20 6e 6f 74 20   SELECT are not 
26428 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f  required..    */
26429 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2642a 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61  CodeExprList(pPa
2642b 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 72 65 67  rse, pEList, reg
2642c 52 65 73 75 6c 74 2c 20 65 44 65 73 74 3d 3d 53  Result, eDest==S
2642d 52 54 5f 4f 75 74 70 75 74 29 3b 0a 20 20 7d 0a  RT_Output);.  }.
2642e 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73    nColumn = nRes
2642f 75 6c 74 43 6f 6c 3b 0a 0a 20 20 2f 2a 20 49 66  ultCol;..  /* If
26430 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65   the DISTINCT ke
26431 79 77 6f 72 64 20 77 61 73 20 70 72 65 73 65 6e  yword was presen
26432 74 20 6f 6e 20 74 68 65 20 53 45 4c 45 43 54 20  t on the SELECT 
26433 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 61  statement.  ** a
26434 6e 64 20 74 68 69 73 20 72 6f 77 20 68 61 73 20  nd this row has 
26435 62 65 65 6e 20 73 65 65 6e 20 62 65 66 6f 72 65  been seen before
26436 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6d 61  , then do not ma
26437 6b 65 20 74 68 69 73 20 72 6f 77 0a 20 20 2a 2a  ke this row.  **
26438 20 70 61 72 74 20 6f 66 20 74 68 65 20 72 65 73   part of the res
26439 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ult..  */.  if( 
2643a 68 61 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20  hasDistinct ){. 
2643b 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73     assert( pELis
2643c 74 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t!=0 );.    asse
2643d 72 74 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  rt( pEList->nExp
2643e 72 3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20  r==nColumn );.  
2643f 20 20 63 6f 64 65 44 69 73 74 69 6e 63 74 28 70    codeDistinct(p
26440 50 61 72 73 65 2c 20 64 69 73 74 69 6e 63 74 2c  Parse, distinct,
26441 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c   iContinue, nCol
26442 75 6d 6e 2c 20 72 65 67 52 65 73 75 6c 74 29 3b  umn, regResult);
26443 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42  .    if( pOrderB
26444 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f  y==0 ){.      co
26445 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69  deOffset(v, p, i
26446 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 7d  Continue);.    }
26447 0a 20 20 7d 0a 0a 20 20 69 66 28 20 63 68 65 63  .  }..  if( chec
26448 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53  kForMultiColumnS
26449 65 6c 65 63 74 45 72 72 6f 72 28 70 50 61 72 73  electError(pPars
2644a 65 2c 20 70 44 65 73 74 2c 20 70 45 4c 69 73 74  e, pDest, pEList
2644b 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ->nExpr) ){.    
2644c 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73  return;.  }..  s
2644d 77 69 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a  witch( eDest ){.
2644e 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 6d      /* In this m
2644f 6f 64 65 2c 20 77 72 69 74 65 20 65 61 63 68 20  ode, write each 
26450 71 75 65 72 79 20 72 65 73 75 6c 74 20 74 6f 20  query result to 
26451 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74  the key of the t
26452 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 2a 2a 20  emporary.    ** 
26453 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20  table iParm..   
26454 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
26455 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
26456 5f 53 45 4c 45 43 54 0a 20 20 20 20 63 61 73 65  _SELECT.    case
26457 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20   SRT_Union: {.  
26458 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20      int r1;.    
26459 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
2645a 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2645b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2645c 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2645d 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52  MakeRecord, regR
2645e 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20  esult, nColumn, 
2645f 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r1);.      sqlit
26460 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26461 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50  OP_IdxInsert, iP
26462 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  arm, r1);.      
26463 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
26464 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
26465 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
26466 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f      }..    /* Co
26467 6e 73 74 72 75 63 74 20 61 20 72 65 63 6f 72 64  nstruct a record
26468 20 66 72 6f 6d 20 74 68 65 20 71 75 65 72 79 20   from the query 
26469 72 65 73 75 6c 74 2c 20 62 75 74 20 69 6e 73 74  result, but inst
2646a 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a 20 73 61  ead of.    ** sa
2646b 76 69 6e 67 20 74 68 61 74 20 72 65 63 6f 72 64  ving that record
2646c 2c 20 75 73 65 20 69 74 20 61 73 20 61 20 6b 65  , use it as a ke
2646d 79 20 74 6f 20 64 65 6c 65 74 65 20 65 6c 65 6d  y to delete elem
2646e 65 6e 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a  ents from.    **
2646f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74   the temporary t
26470 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20  able iParm..    
26471 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
26472 45 78 63 65 70 74 3a 20 7b 0a 20 20 20 20 20 20  Except: {.      
26473 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26474 33 28 76 2c 20 4f 50 5f 49 64 78 44 65 6c 65 74  3(v, OP_IdxDelet
26475 65 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 65 73  e, iParm, regRes
26476 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  ult, nColumn);. 
26477 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26478 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
26479 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   Store the resul
2647a 74 20 61 73 20 64 61 74 61 20 75 73 69 6e 67 20  t as data using 
2647b 61 20 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20  a unique key..  
2647c 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
2647d 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73  T_Table:.    cas
2647e 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20  e SRT_EphemTab: 
2647f 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d  {.      int r1 =
26480 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
26481 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
26482 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26483 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
26484 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c  cord, regResult,
26485 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20   nColumn, r1);. 
26486 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42       if( pOrderB
26487 79 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 73  y ){.        pus
26488 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72  hOntoSorter(pPar
26489 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c  se, pOrderBy, p,
2648a 20 72 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73   r1);.      }els
2648b 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  e{.        int r
2648c 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  2 = sqlite3GetTe
2648d 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2648e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2648f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
26490 65 77 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20  ewRowid, iParm, 
26491 72 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  r2);.        sql
26492 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
26493 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61  , OP_Insert, iPa
26494 72 6d 2c 20 72 31 2c 20 72 32 29 3b 0a 20 20 20  rm, r1, r2);.   
26495 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26496 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
26497 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20  AG_APPEND);.    
26498 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
26499 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2649a 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20  , r2);.      }. 
2649b 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
2649c 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2649d 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72  e, r1);.      br
2649e 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e  eak;.    }..#ifn
2649f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
264a0 53 55 42 51 55 45 52 59 0a 20 20 20 20 2f 2a 20  SUBQUERY.    /* 
264a1 49 66 20 77 65 20 61 72 65 20 63 72 65 61 74 69  If we are creati
264a2 6e 67 20 61 20 73 65 74 20 66 6f 72 20 61 6e 20  ng a set for an 
264a3 22 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54  "expr IN (SELECT
264a4 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63 74   ...)" construct
264a5 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68  ,.    ** then th
264a6 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20  ere should be a 
264a7 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74  single item on t
264a8 68 65 20 73 74 61 63 6b 2e 20 20 57 72 69 74 65  he stack.  Write
264a9 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 74 65   this.    ** ite
264aa 6d 20 69 6e 74 6f 20 74 68 65 20 73 65 74 20 74  m into the set t
264ab 61 62 6c 65 20 77 69 74 68 20 62 6f 67 75 73 20  able with bogus 
264ac 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  data..    */.   
264ad 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b   case SRT_Set: {
264ae 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
264af 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20  Column==1 );.   
264b0 20 20 20 70 2d 3e 61 66 66 69 6e 69 74 79 20 3d     p->affinity =
264b1 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41   sqlite3CompareA
264b2 66 66 69 6e 69 74 79 28 70 45 4c 69 73 74 2d 3e  ffinity(pEList->
264b3 61 5b 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73  a[0].pExpr, pDes
264b4 74 2d 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20  t->affinity);.  
264b5 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
264b6 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41   ){.        /* A
264b7 74 20 66 69 72 73 74 20 67 6c 61 6e 63 65 20 79  t first glance y
264b8 6f 75 20 77 6f 75 6c 64 20 74 68 69 6e 6b 20 77  ou would think w
264b9 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65  e could optimize
264ba 20 6f 75 74 20 74 68 65 0a 20 20 20 20 20 20 20   out the.       
264bb 20 2a 2a 20 4f 52 44 45 52 20 42 59 20 69 6e 20   ** ORDER BY in 
264bc 74 68 69 73 20 63 61 73 65 20 73 69 6e 63 65 20  this case since 
264bd 74 68 65 20 6f 72 64 65 72 20 6f 66 20 65 6e 74  the order of ent
264be 72 69 65 73 20 69 6e 20 74 68 65 20 73 65 74 0a  ries in the set.
264bf 20 20 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 20          ** does 
264c0 6e 6f 74 20 6d 61 74 74 65 72 2e 20 20 42 75 74  not matter.  But
264c1 20 74 68 65 72 65 20 6d 69 67 68 74 20 62 65 20   there might be 
264c2 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c 20  a LIMIT clause, 
264c3 69 6e 20 77 68 69 63 68 0a 20 20 20 20 20 20 20  in which.       
264c4 20 2a 2a 20 63 61 73 65 20 74 68 65 20 6f 72 64   ** case the ord
264c5 65 72 20 64 6f 65 73 20 6d 61 74 74 65 72 20 2a  er does matter *
264c6 2f 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e  /.        pushOn
264c7 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c  toSorter(pParse,
264c8 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 65   pOrderBy, p, re
264c9 67 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20  gResult);.      
264ca 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
264cb 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47  nt r1 = sqlite3G
264cc 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
264cd 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
264ce 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
264cf 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
264d0 65 67 52 65 73 75 6c 74 2c 20 31 2c 20 72 31 2c  egResult, 1, r1,
264d1 20 26 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31   &p->affinity, 1
264d2 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
264d3 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
264d4 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
264d5 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 31 29 3b  , regResult, 1);
264d6 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
264d7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
264d8 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72  _IdxInsert, iPar
264d9 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  m, r1);.        
264da 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
264db 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
264dc 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
264dd 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
264de 20 20 20 2f 2a 20 49 66 20 61 6e 79 20 72 6f 77     /* If any row
264df 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 72 65   exist in the re
264e0 73 75 6c 74 20 73 65 74 2c 20 72 65 63 6f 72 64  sult set, record
264e1 20 74 68 61 74 20 66 61 63 74 20 61 6e 64 20 61   that fact and a
264e2 62 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  bort..    */.   
264e3 20 63 61 73 65 20 53 52 54 5f 45 78 69 73 74 73   case SRT_Exists
264e4 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
264e5 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
264e6 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 50  P_Integer, 1, iP
264e7 61 72 6d 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  arm);.      /* T
264e8 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
264e9 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74  will terminate t
264ea 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a  he loop for us *
264eb 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
264ec 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
264ed 74 68 69 73 20 69 73 20 61 20 73 63 61 6c 61 72  this is a scalar
264ee 20 73 65 6c 65 63 74 20 74 68 61 74 20 69 73 20   select that is 
264ef 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65  part of an expre
264f0 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20  ssion, then.    
264f1 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  ** store the res
264f2 75 6c 74 73 20 69 6e 20 74 68 65 20 61 70 70 72  ults in the appr
264f3 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72 79 20 63  opriate memory c
264f4 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b 20 6f 75  ell and break ou
264f5 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  t.    ** of the 
264f6 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a  scan loop..    *
264f7 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d  /.    case SRT_M
264f8 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  em: {.      asse
264f9 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29  rt( nColumn==1 )
264fa 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64  ;.      if( pOrd
264fb 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20  erBy ){.        
264fc 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70  pushOntoSorter(p
264fd 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c  Parse, pOrderBy,
264fe 20 70 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a   p, regResult);.
264ff 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
26500 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
26501 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c  CodeMove(pParse,
26502 20 72 65 67 52 65 73 75 6c 74 2c 20 69 50 61 72   regResult, iPar
26503 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 2f  m, 1);.        /
26504 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75  * The LIMIT clau
26505 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74  se will jump out
26506 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72   of the loop for
26507 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20   us */.      }. 
26508 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26509 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e  }.#endif /* #ifn
2650a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2650b 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20  SUBQUERY */..   
2650c 20 2f 2a 20 53 65 6e 64 20 74 68 65 20 64 61 74   /* Send the dat
2650d 61 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  a to the callbac
2650e 6b 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 74 6f  k function or to
2650f 20 61 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20   a subroutine.  
26510 49 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61  In the.    ** ca
26511 73 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69  se of a subrouti
26512 6e 65 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69  ne, the subrouti
26513 6e 65 20 69 74 73 65 6c 66 20 69 73 20 72 65 73  ne itself is res
26514 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 20 20 20  ponsible for.   
26515 20 2a 2a 20 70 6f 70 70 69 6e 67 20 74 68 65 20   ** popping the 
26516 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 73 74  data from the st
26517 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ack..    */.    
26518 63 61 73 65 20 53 52 54 5f 43 6f 72 6f 75 74 69  case SRT_Corouti
26519 6e 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54  ne:.    case SRT
2651a 5f 4f 75 74 70 75 74 3a 20 7b 0a 20 20 20 20 20  _Output: {.     
2651b 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
2651c 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 20  .        int r1 
2651d 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2651e 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
2651f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26520 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
26521 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75  eRecord, regResu
26522 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29  lt, nColumn, r1)
26523 3b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e  ;.        pushOn
26524 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c  toSorter(pParse,
26525 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 31   pOrderBy, p, r1
26526 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
26527 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
26528 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
26529 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 44      }else if( eD
2652a 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69  est==SRT_Corouti
2652b 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ne ){.        sq
2652c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2652d 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65  v, OP_Yield, pDe
2652e 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20  st->iParm);.    
2652f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
26530 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26531 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
26532 6f 77 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e  ow, regResult, n
26533 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20  Column);.       
26534 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
26535 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
26536 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c  pParse, regResul
26537 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  t, nColumn);.   
26538 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
26539 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 21 64 65  ;.    }..#if !de
2653a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2653b 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20 2f  T_TRIGGER).    /
2653c 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 72 65  * Discard the re
2653d 73 75 6c 74 73 2e 20 20 54 68 69 73 20 69 73 20  sults.  This is 
2653e 75 73 65 64 20 66 6f 72 20 53 45 4c 45 43 54 20  used for SELECT 
2653f 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64  statements insid
26540 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 6f 64  e.    ** the bod
26541 79 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20  y of a TRIGGER. 
26542 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20   The purpose of 
26543 73 75 63 68 20 73 65 6c 65 63 74 73 20 69 73 20  such selects is 
26544 74 6f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 75  to call.    ** u
26545 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ser-defined func
26546 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20  tions that have 
26547 73 69 64 65 20 65 66 66 65 63 74 73 2e 20 20 57  side effects.  W
26548 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20 20  e do not care.  
26549 20 20 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 61    ** about the a
2654a 63 74 75 61 6c 20 72 65 73 75 6c 74 73 20 6f 66  ctual results of
2654b 20 74 68 65 20 73 65 6c 65 63 74 2e 0a 20 20 20   the select..   
2654c 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a   */.    default:
2654d 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
2654e 20 65 44 65 73 74 3d 3d 53 52 54 5f 44 69 73 63   eDest==SRT_Disc
2654f 61 72 64 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ard );.      bre
26550 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
26551 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20  .  }..  /* Jump 
26552 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
26553 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c 49  e loop if the LI
26554 4d 49 54 20 69 73 20 72 65 61 63 68 65 64 2e 0a  MIT is reached..
26555 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c    */.  if( p->iL
26556 69 6d 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65  imit ){.    asse
26557 72 74 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20  rt( pOrderBy==0 
26558 29 3b 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  );  /* If there 
26559 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 2c 20  is an ORDER BY, 
2655a 74 68 65 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20  the call to.    
2655b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2655c 20 20 20 20 20 20 20 20 2a 2a 20 70 75 73 68 4f          ** pushO
2655d 6e 74 6f 53 6f 72 74 65 72 28 29 20 77 6f 75 6c  ntoSorter() woul
2655e 64 20 68 61 76 65 20 63 6c 65 61 72 65 64 20 70  d have cleared p
2655f 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 20 20  ->iLimit */.    
26560 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26561 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
26562 70 2d 3e 69 4c 69 6d 69 74 2c 20 2d 31 29 3b 0a  p->iLimit, -1);.
26563 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26564 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65  ddOp2(v, OP_IfZe
26565 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69  ro, p->iLimit, i
26566 42 72 65 61 6b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Break);.  }.}../
26567 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 65 78  *.** Given an ex
26568 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 67  pression list, g
26569 65 6e 65 72 61 74 65 20 61 20 4b 65 79 49 6e 66  enerate a KeyInf
2656a 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
2656b 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65 20   records.** the 
2656c 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2656d 63 65 20 66 6f 72 20 65 61 63 68 20 65 78 70 72  ce for each expr
2656e 65 73 73 69 6f 6e 20 69 6e 20 74 68 61 74 20 65  ession in that e
2656f 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a  xpression list..
26570 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 70  **.** If the Exp
26571 72 4c 69 73 74 20 69 73 20 61 6e 20 4f 52 44 45  rList is an ORDE
26572 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59  R BY or GROUP BY
26573 20 63 6c 61 75 73 65 20 74 68 65 6e 20 74 68 65   clause then the
26574 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 4b 65   resulting.** Ke
26575 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
26576 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
26577 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  or initializing 
26578 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20  a virtual index 
26579 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  to.** implement 
2657a 74 68 61 74 20 63 6c 61 75 73 65 2e 20 20 49 66  that clause.  If
2657b 20 74 68 65 20 45 78 70 72 4c 69 73 74 20 69 73   the ExprList is
2657c 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2657d 6f 66 20 61 20 53 45 4c 45 43 54 0a 2a 2a 20 74  of a SELECT.** t
2657e 68 65 6e 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  hen the KeyInfo 
2657f 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70 70  structure is app
26580 72 6f 70 72 69 61 74 65 20 66 6f 72 20 69 6e 69  ropriate for ini
26581 74 69 61 6c 69 7a 69 6e 67 20 61 20 76 69 72 74  tializing a virt
26582 75 61 6c 0a 2a 2a 20 69 6e 64 65 78 20 74 6f 20  ual.** index to 
26583 69 6d 70 6c 65 6d 65 6e 74 20 61 20 44 49 53 54  implement a DIST
26584 49 4e 43 54 20 74 65 73 74 2e 0a 2a 2a 0a 2a 2a  INCT test..**.**
26585 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
26586 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63  he KeyInfo struc
26587 74 75 72 65 20 69 73 20 6f 62 74 61 69 6e 20 66  ture is obtain f
26588 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 20 54 68 65  rom malloc.  The
26589 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63   calling.** func
2658a 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69  tion is responsi
2658b 62 6c 65 20 66 6f 72 20 73 65 65 69 6e 67 20 74  ble for seeing t
2658c 68 61 74 20 74 68 69 73 20 73 74 72 75 63 74 75  hat this structu
2658d 72 65 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79  re is eventually
2658e 0a 2a 2a 20 66 72 65 65 64 2e 20 20 41 64 64 20  .** freed.  Add 
2658f 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  the KeyInfo stru
26590 63 74 75 72 65 20 74 6f 20 74 68 65 20 50 34 20  cture to the P4 
26591 66 69 65 6c 64 20 6f 66 20 61 6e 20 6f 70 63 6f  field of an opco
26592 64 65 20 75 73 69 6e 67 0a 2a 2a 20 50 34 5f 4b  de using.** P4_K
26593 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69  EYINFO_HANDOFF i
26594 73 20 74 68 65 20 75 73 75 61 6c 20 77 61 79 20  s the usual way 
26595 6f 66 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  of dealing with 
26596 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  this..*/.static 
26597 4b 65 79 49 6e 66 6f 20 2a 6b 65 79 49 6e 66 6f  KeyInfo *keyInfo
26598 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 50 61 72  FromExprList(Par
26599 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
2659a 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
2659b 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2659c 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
2659d 6e 45 78 70 72 3b 0a 20 20 4b 65 79 49 6e 66 6f  nExpr;.  KeyInfo
2659e 20 2a 70 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63   *pInfo;.  struc
2659f 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
265a0 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b  *pItem;.  int i;
265a1 0a 0a 20 20 6e 45 78 70 72 20 3d 20 70 4c 69 73  ..  nExpr = pLis
265a2 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 66  t->nExpr;.  pInf
265a3 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  o = sqlite3DbMal
265a4 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
265a5 6f 66 28 2a 70 49 6e 66 6f 29 20 2b 20 6e 45 78  of(*pInfo) + nEx
265a6 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  pr*(sizeof(CollS
265a7 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20 69 66 28  eq*)+1) );.  if(
265a8 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20 20 70 49   pInfo ){.    pI
265a9 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  nfo->aSortOrder 
265aa 3d 20 28 75 38 2a 29 26 70 49 6e 66 6f 2d 3e 61  = (u8*)&pInfo->a
265ab 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20 20 20  Coll[nExpr];.   
265ac 20 70 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d   pInfo->nField =
265ad 20 28 75 31 36 29 6e 45 78 70 72 3b 0a 20 20 20   (u16)nExpr;.   
265ae 20 70 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e   pInfo->enc = EN
265af 43 28 64 62 29 3b 0a 20 20 20 20 70 49 6e 66 6f  C(db);.    pInfo
265b0 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 66  ->db = db;.    f
265b1 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c  or(i=0, pItem=pL
265b2 69 73 74 2d 3e 61 3b 20 69 3c 6e 45 78 70 72 3b  ist->a; i<nExpr;
265b3 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
265b4 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
265b5 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 43 6f 6c  Coll;.      pCol
265b6 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
265b7 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
265b8 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20  Item->pExpr);.  
265b9 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29      if( !pColl )
265ba 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20  {.        pColl 
265bb 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b  = db->pDfltColl;
265bc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
265bd 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d  Info->aColl[i] =
265be 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 49   pColl;.      pI
265bf 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  nfo->aSortOrder[
265c0 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 73 6f 72 74  i] = pItem->sort
265c1 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  Order;.    }.  }
265c2 0a 20 20 72 65 74 75 72 6e 20 70 49 6e 66 6f 3b  .  return pInfo;
265c3 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  .}.../*.** If th
265c4 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 20 77 61 73  e inner loop was
265c5 20 67 65 6e 65 72 61 74 65 64 20 75 73 69 6e 67   generated using
265c6 20 61 20 6e 6f 6e 2d 6e 75 6c 6c 20 70 4f 72 64   a non-null pOrd
265c7 65 72 42 79 20 61 72 67 75 6d 65 6e 74 2c 0a 2a  erBy argument,.*
265c8 2a 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  * then the resul
265c9 74 73 20 77 65 72 65 20 70 6c 61 63 65 64 20 69  ts were placed i
265ca 6e 20 61 20 73 6f 72 74 65 72 2e 20 20 41 66 74  n a sorter.  Aft
265cb 65 72 20 74 68 65 20 6c 6f 6f 70 20 69 73 20 74  er the loop is t
265cc 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 77 65 20  erminated.** we 
265cd 6e 65 65 64 20 74 6f 20 72 75 6e 20 74 68 65 20  need to run the 
265ce 73 6f 72 74 65 72 20 61 6e 64 20 6f 75 74 70 75  sorter and outpu
265cf 74 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20  t the results.  
265d0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  The following.**
265d1 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
265d2 65 73 20 74 68 65 20 63 6f 64 65 20 6e 65 65 64  es the code need
265d3 65 64 20 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a  ed to do that..*
265d4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  /.static void ge
265d5 6e 65 72 61 74 65 53 6f 72 74 54 61 69 6c 28 0a  nerateSortTail(.
265d6 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
265d7 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
265d8 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
265d9 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 2f 2a  ct *p,        /*
265da 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
265db 65 6d 65 6e 74 20 2a 2f 0a 20 20 56 64 62 65 20  ement */.  Vdbe 
265dc 2a 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  *v,          /* 
265dd 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
265de 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a  to this VDBE */.
265df 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20    int nColumn,  
265e0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
265e1 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61   columns of data
265e2 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
265e3 20 2a 70 44 65 73 74 20 2f 2a 20 57 72 69 74 65   *pDest /* Write
265e4 20 74 68 65 20 73 6f 72 74 65 64 20 72 65 73 75   the sorted resu
265e5 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  lts here */.){. 
265e6 20 69 6e 74 20 61 64 64 72 42 72 65 61 6b 20 3d   int addrBreak =
265e7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
265e8 4c 61 62 65 6c 28 76 29 3b 20 20 20 20 20 2f 2a  Label(v);     /*
265e9 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 65 78   Jump here to ex
265ea 69 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  it loop */.  int
265eb 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 20 3d 20   addrContinue = 
265ec 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
265ed 61 62 65 6c 28 76 29 3b 20 20 2f 2a 20 4a 75 6d  abel(v);  /* Jum
265ee 70 20 68 65 72 65 20 66 6f 72 20 6e 65 78 74 20  p here for next 
265ef 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61  cycle */.  int a
265f0 64 64 72 3b 0a 20 20 69 6e 74 20 69 54 61 62 3b  ddr;.  int iTab;
265f1 0a 20 20 69 6e 74 20 70 73 65 75 64 6f 54 61 62  .  int pseudoTab
265f2 20 3d 20 30 3b 0a 20 20 45 78 70 72 4c 69 73 74   = 0;.  ExprList
265f3 20 2a 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e   *pOrderBy = p->
265f4 70 4f 72 64 65 72 42 79 3b 0a 0a 20 20 69 6e 74  pOrderBy;..  int
265f5 20 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e   eDest = pDest->
265f6 65 44 65 73 74 3b 0a 20 20 69 6e 74 20 69 50 61  eDest;.  int iPa
265f7 72 6d 20 3d 20 70 44 65 73 74 2d 3e 69 50 61 72  rm = pDest->iPar
265f8 6d 3b 0a 0a 20 20 69 6e 74 20 72 65 67 52 6f 77  m;..  int regRow
265f9 3b 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  ;.  int regRowid
265fa 3b 0a 0a 20 20 69 54 61 62 20 3d 20 70 4f 72 64  ;..  iTab = pOrd
265fb 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 3b 0a  erBy->iECursor;.
265fc 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54    if( eDest==SRT
265fd 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44 65 73 74  _Output || eDest
265fe 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20  ==SRT_Coroutine 
265ff 29 7b 0a 20 20 20 20 70 73 65 75 64 6f 54 61 62  ){.    pseudoTab
26600 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
26601 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  +;.    sqlite3Vd
26602 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
26603 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c  etNumColumns, 0,
26604 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73   nColumn);.    s
26605 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26606 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64  (v, OP_OpenPseud
26607 6f 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 65 44  o, pseudoTab, eD
26608 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 29  est==SRT_Output)
26609 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 31  ;.  }.  addr = 1
2660a 20 2b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   + sqlite3VdbeAd
2660b 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 2c  dOp2(v, OP_Sort,
2660c 20 69 54 61 62 2c 20 61 64 64 72 42 72 65 61 6b   iTab, addrBreak
2660d 29 3b 0a 20 20 63 6f 64 65 4f 66 66 73 65 74 28  );.  codeOffset(
2660e 76 2c 20 70 2c 20 61 64 64 72 43 6f 6e 74 69 6e  v, p, addrContin
2660f 75 65 29 3b 0a 20 20 72 65 67 52 6f 77 20 3d 20  ue);.  regRow = 
26610 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
26611 67 28 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67  g(pParse);.  reg
26612 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47  Rowid = sqlite3G
26613 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
26614 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
26615 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
26616 75 6d 6e 2c 20 69 54 61 62 2c 20 70 4f 72 64 65  umn, iTab, pOrde
26617 72 42 79 2d 3e 6e 45 78 70 72 20 2b 20 31 2c 20  rBy->nExpr + 1, 
26618 72 65 67 52 6f 77 29 3b 0a 20 20 73 77 69 74 63  regRow);.  switc
26619 68 28 20 65 44 65 73 74 20 29 7b 0a 20 20 20 20  h( eDest ){.    
2661a 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a  case SRT_Table:.
2661b 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 70 68      case SRT_Eph
2661c 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 74  emTab: {.      t
2661d 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d  estcase( eDest==
2661e 53 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20 20  SRT_Table );.   
2661f 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65     testcase( eDe
26620 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62  st==SRT_EphemTab
26621 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
26622 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26623 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 50 61 72  P_NewRowid, iPar
26624 6d 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  m, regRowid);.  
26625 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26626 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
26627 72 74 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f  rt, iParm, regRo
26628 77 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  w, regRowid);.  
26629 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2662a 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
2662b 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20  G_APPEND);.     
2662c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69   break;.    }.#i
2662d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2662e 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 63  T_SUBQUERY.    c
2662f 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20  ase SRT_Set: {. 
26630 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f       assert( nCo
26631 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20  lumn==1 );.     
26632 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26633 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p4(v, OP_MakeRec
26634 6f 72 64 2c 20 72 65 67 52 6f 77 2c 20 31 2c 20  ord, regRow, 1, 
26635 72 65 67 52 6f 77 69 64 2c 20 26 70 2d 3e 61 66  regRowid, &p->af
26636 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20  finity, 1);.    
26637 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
26638 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
26639 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 2c  (pParse, regRow,
2663a 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
2663b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2663c 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50  OP_IdxInsert, iP
2663d 61 72 6d 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  arm, regRowid);.
2663e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2663f 20 7d 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f   }.    case SRT_
26640 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  Mem: {.      ass
26641 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20  ert( nColumn==1 
26642 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26643 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61  ExprCodeMove(pPa
26644 72 73 65 2c 20 72 65 67 52 6f 77 2c 20 69 50 61  rse, regRow, iPa
26645 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a  rm, 1);.      /*
26646 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73   The LIMIT claus
26647 65 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65  e will terminate
26648 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73   the loop for us
26649 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   */.      break;
2664a 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
2664b 20 20 63 61 73 65 20 53 52 54 5f 4f 75 74 70 75    case SRT_Outpu
2664c 74 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  t:.    case SRT_
2664d 43 6f 72 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20  Coroutine: {.   
2664e 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
2664f 74 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d  testcase( eDest=
26650 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20  =SRT_Output );. 
26651 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65       testcase( e
26652 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74  Dest==SRT_Corout
26653 69 6e 65 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ine );.      sql
26654 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26655 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
26656 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
26657 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26658 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
26659 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 72 65 67  , pseudoTab, reg
2665a 52 6f 77 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  Row, regRowid);.
2665b 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2665c 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  <nColumn; i++){.
2665d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2665e 72 65 67 52 6f 77 21 3d 70 44 65 73 74 2d 3e 69  regRow!=pDest->i
2665f 4d 65 6d 2b 69 20 29 3b 0a 20 20 20 20 20 20 20  Mem+i );.       
26660 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26661 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
26662 20 70 73 65 75 64 6f 54 61 62 2c 20 69 2c 20 70   pseudoTab, i, p
26663 44 65 73 74 2d 3e 69 4d 65 6d 2b 69 29 3b 0a 20  Dest->iMem+i);. 
26664 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
26665 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70   eDest==SRT_Outp
26666 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ut ){.        sq
26667 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26668 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
26669 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43   pDest->iMem, nC
2666a 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20  olumn);.        
2666b 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2666c 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
2666d 50 61 72 73 65 2c 20 70 44 65 73 74 2d 3e 69 4d  Parse, pDest->iM
2666e 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  em, nColumn);.  
2666f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
26670 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26671 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
26672 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b  , pDest->iParm);
26673 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
26674 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
26675 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
26676 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 2a   /* Do nothing *
26677 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
26678 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
26679 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2667a 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 29  (pParse, regRow)
2667b 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
2667c 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2667d 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20  , regRowid);..  
2667e 2f 2a 20 4c 49 4d 49 54 20 68 61 73 20 62 65 65  /* LIMIT has bee
2667f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79  n implemented by
26680 20 74 68 65 20 70 75 73 68 4f 6e 74 6f 53 6f 72   the pushOntoSor
26681 74 65 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 20  ter() routine.. 
26682 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
26683 3e 69 4c 69 6d 69 74 3d 3d 30 20 29 3b 0a 0a 20  >iLimit==0 );.. 
26684 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f   /* The bottom o
26685 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a  f the loop.  */.
26686 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
26687 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64  olveLabel(v, add
26688 72 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 73 71  rContinue);.  sq
26689 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2668a 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62  v, OP_Next, iTab
2668b 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74  , addr);.  sqlit
2668c 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2668d 65 6c 28 76 2c 20 61 64 64 72 42 72 65 61 6b 29  el(v, addrBreak)
2668e 3b 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53  ;.  if( eDest==S
2668f 52 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44 65  RT_Output || eDe
26690 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e  st==SRT_Coroutin
26691 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
26692 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26693 5f 43 6c 6f 73 65 2c 20 70 73 65 75 64 6f 54 61  _Close, pseudoTa
26694 62 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  b, 0);.  }.}../*
26695 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
26696 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67  nter to a string
26697 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
26698 27 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70  'declaration typ
26699 65 27 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78 70  e' of the.** exp
2669a 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 54  ression pExpr. T
2669b 68 65 20 73 74 72 69 6e 67 20 6d 61 79 20 62 65  he string may be
2669c 20 74 72 65 61 74 65 64 20 61 73 20 73 74 61 74   treated as stat
2669d 69 63 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ic by the caller
2669e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 63 6c  ..**.** The decl
2669f 61 72 61 74 69 6f 6e 20 74 79 70 65 20 69 73 20  aration type is 
266a0 74 68 65 20 65 78 61 63 74 20 64 61 74 61 74 79  the exact dataty
266a1 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 65 78  pe definition ex
266a2 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65  tracted from the
266a3 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 43 52 45  .** original CRE
266a4 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
266a5 65 6e 74 20 69 66 20 74 68 65 20 65 78 70 72 65  ent if the expre
266a6 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d  ssion is a colum
266a7 6e 2e 20 54 68 65 0a 2a 2a 20 64 65 63 6c 61 72  n. The.** declar
266a8 61 74 69 6f 6e 20 74 79 70 65 20 66 6f 72 20 61  ation type for a
266a9 20 52 4f 57 49 44 20 66 69 65 6c 64 20 69 73 20   ROWID field is 
266aa 49 4e 54 45 47 45 52 2e 20 45 78 61 63 74 6c 79  INTEGER. Exactly
266ab 20 77 68 65 6e 20 61 6e 20 65 78 70 72 65 73 73   when an express
266ac 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64  ion.** is consid
266ad 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20 63 61  ered a column ca
266ae 6e 20 62 65 20 63 6f 6d 70 6c 65 78 20 69 6e 20  n be complex in 
266af 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20  the presence of 
266b0 73 75 62 71 75 65 72 69 65 73 2e 20 54 68 65 0a  subqueries. The.
266b1 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78  ** result-set ex
266b2 70 72 65 73 73 69 6f 6e 20 69 6e 20 61 6c 6c 20  pression in all 
266b3 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
266b4 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
266b5 74 73 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69 64  ts is .** consid
266b6 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20 62 79  ered a column by
266b7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
266b8 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 63  **.**   SELECT c
266b9 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a 2a 2a 20  ol FROM tbl;.** 
266ba 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45 43 54    SELECT (SELECT
266bb 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a 2a   col FROM tbl;.*
266bc 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45  *   SELECT (SELE
266bd 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 29  CT col FROM tbl)
266be 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 61 62  ;.**   SELECT ab
266bf 63 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 63  c FROM (SELECT c
266c0 6f 6c 20 41 53 20 61 62 63 20 46 52 4f 4d 20 74  ol AS abc FROM t
266c1 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  bl);.** .** The 
266c2 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
266c3 20 66 6f 72 20 61 6e 79 20 65 78 70 72 65 73 73   for any express
266c4 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 61  ion other than a
266c5 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 2e   column is NULL.
266c6 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
266c7 20 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 54 79 70   char *columnTyp
266c8 65 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  e(.  NameContext
266c9 20 2a 70 4e 43 2c 20 0a 20 20 45 78 70 72 20 2a   *pNC, .  Expr *
266ca 70 45 78 70 72 2c 0a 20 20 63 6f 6e 73 74 20 63  pExpr,.  const c
266cb 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 44 62  har **pzOriginDb
266cc 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
266cd 2a 70 7a 4f 72 69 67 69 6e 54 61 62 2c 0a 20 20  *pzOriginTab,.  
266ce 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f  const char **pzO
266cf 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a 20 20 63 68  riginCol.){.  ch
266d0 61 72 20 63 6f 6e 73 74 20 2a 7a 54 79 70 65 20  ar const *zType 
266d1 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  = 0;.  char cons
266d2 74 20 2a 7a 4f 72 69 67 69 6e 44 62 20 3d 20 30  t *zOriginDb = 0
266d3 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
266d4 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 30 3b 0a  zOriginTab = 0;.
266d5 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f    char const *zO
266d6 72 69 67 69 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20  riginCol = 0;.  
266d7 69 6e 74 20 6a 3b 0a 20 20 69 66 28 20 70 45 78  int j;.  if( pEx
266d8 70 72 3d 3d 30 20 7c 7c 20 70 4e 43 2d 3e 70 53  pr==0 || pNC->pS
266d9 72 63 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75  rcList==0 ) retu
266da 72 6e 20 30 3b 0a 0a 20 20 73 77 69 74 63 68 28  rn 0;..  switch(
266db 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20   pExpr->op ){.  
266dc 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f    case TK_AGG_CO
266dd 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54  LUMN:.    case T
266de 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20  K_COLUMN: {.    
266df 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
266e0 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e  ion is a column.
266e1 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c   Locate the tabl
266e2 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20  e the column is 
266e3 62 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 65  being.      ** e
266e4 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 69 6e  xtracted from in
266e5 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70 53 72   NameContext.pSr
266e6 63 4c 69 73 74 2e 20 54 68 69 73 20 74 61 62 6c  cList. This tabl
266e7 65 20 6d 61 79 20 62 65 20 72 65 61 6c 0a 20 20  e may be real.  
266e8 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
266e9 74 61 62 6c 65 20 6f 72 20 61 20 73 75 62 71 75  table or a subqu
266ea 65 72 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ery..      */.  
266eb 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
266ec 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
266ed 2f 2a 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  /* Table structu
266ee 72 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74  re column is ext
266ef 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20  racted from */. 
266f0 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20       Select *pS 
266f1 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
266f2 20 2f 2a 20 53 65 6c 65 63 74 20 74 68 65 20 63   /* Select the c
266f3 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72 61 63 74  olumn is extract
266f4 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20  ed from */.     
266f5 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78 70   int iCol = pExp
266f6 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 2f 2a 20  r->iColumn;  /* 
266f7 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
266f8 69 6e 20 70 54 61 62 20 2a 2f 0a 20 20 20 20 20  in pTab */.     
266f9 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26 20 21   while( pNC && !
266fa 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  pTab ){.        
266fb 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
266fc 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  t = pNC->pSrcLis
266fd 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  t;.        for(j
266fe 3d 30 3b 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  =0;j<pTabList->n
266ff 53 72 63 20 26 26 20 70 54 61 62 4c 69 73 74 2d  Src && pTabList-
26700 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 21 3d 70  >a[j].iCursor!=p
26701 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 6a 2b 2b  Expr->iTable;j++
26702 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a  );.        if( j
26703 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20  <pTabList->nSrc 
26704 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54 61  ){.          pTa
26705 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  b = pTabList->a[
26706 6a 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 20  j].pTab;.       
26707 20 20 20 70 53 20 3d 20 70 54 61 62 4c 69 73 74     pS = pTabList
26708 2d 3e 61 5b 6a 5d 2e 70 53 65 6c 65 63 74 3b 0a  ->a[j].pSelect;.
26709 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2670a 20 20 20 20 20 20 20 20 20 70 4e 43 20 3d 20 70           pNC = p
2670b 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  NC->pNext;.     
2670c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
2670d 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20      if( pTab==0 
2670e 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 49  ){.        /* FI
2670f 58 20 4d 45 3a 0a 20 20 20 20 20 20 20 20 2a 2a  X ME:.        **
26710 20 54 68 69 73 20 63 61 6e 20 6f 63 63 75 72 73   This can occurs
26711 20 69 66 20 79 6f 75 20 68 61 76 65 20 73 6f 6d   if you have som
26712 65 74 68 69 6e 67 20 6c 69 6b 65 20 22 53 45 4c  ething like "SEL
26713 45 43 54 20 6e 65 77 2e 78 3b 22 20 69 6e 73 69  ECT new.x;" insi
26714 64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20  de.        ** a 
26715 74 72 69 67 67 65 72 2e 20 20 49 6e 20 6f 74 68  trigger.  In oth
26716 65 72 20 77 6f 72 64 73 2c 20 69 66 20 79 6f 75  er words, if you
26717 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 73   reference the s
26718 70 65 63 69 61 6c 20 22 6e 65 77 22 0a 20 20 20  pecial "new".   
26719 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 69 6e       ** table in
2671a 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2671b 6f 66 20 61 20 73 65 6c 65 63 74 2e 20 20 57 65  of a select.  We
2671c 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 20 67   do not have a g
2671d 6f 6f 64 20 77 61 79 0a 20 20 20 20 20 20 20 20  ood way.        
2671e 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65 20 61  ** to find the a
2671f 63 74 75 61 6c 20 74 61 62 6c 65 20 74 79 70 65  ctual table type
26720 2c 20 73 6f 20 63 61 6c 6c 20 69 74 20 22 54 45  , so call it "TE
26721 58 54 22 2e 20 20 54 68 69 73 20 69 73 20 72 65  XT".  This is re
26722 61 6c 6c 79 0a 20 20 20 20 20 20 20 20 2a 2a 20  ally.        ** 
26723 73 6f 6d 65 74 68 69 6e 67 20 6f 66 20 61 20 62  something of a b
26724 75 67 2c 20 62 75 74 20 49 20 64 6f 20 6e 6f 74  ug, but I do not
26725 20 6b 6e 6f 77 20 68 6f 77 20 74 6f 20 66 69 78   know how to fix
26726 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a   it..        **.
26727 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20          ** This 
26728 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20 70 72  code does not pr
26729 6f 64 75 63 65 20 74 68 65 20 63 6f 72 72 65 63  oduce the correc
2672a 74 20 61 6e 73 77 65 72 20 2d 20 69 74 20 6a 75  t answer - it ju
2672b 73 74 20 70 72 65 76 65 6e 74 73 0a 20 20 20 20  st prevents.    
2672c 20 20 20 20 2a 2a 20 61 20 73 65 67 66 61 75 6c      ** a segfaul
2672d 74 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23  t.  See ticket #
2672e 31 32 32 39 2e 0a 20 20 20 20 20 20 20 20 2a 2f  1229..        */
2672f 0a 20 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d  .        zType =
26730 20 22 54 45 58 54 22 3b 0a 20 20 20 20 20 20 20   "TEXT";.       
26731 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
26732 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
26733 54 61 62 20 29 3b 0a 20 20 20 20 20 20 69 66 28  Tab );.      if(
26734 20 70 53 20 29 7b 0a 20 20 20 20 20 20 20 20 2f   pS ){.        /
26735 2a 20 54 68 65 20 22 74 61 62 6c 65 22 20 69 73  * The "table" is
26736 20 61 63 74 75 61 6c 6c 79 20 61 20 73 75 62 2d   actually a sub-
26737 73 65 6c 65 63 74 20 6f 72 20 61 20 76 69 65 77  select or a view
26738 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
26739 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  use.        ** o
2673a 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  f the SELECT sta
2673b 74 65 6d 65 6e 74 2e 20 52 65 74 75 72 6e 20 74  tement. Return t
2673c 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74  he declaration t
2673d 79 70 65 20 61 6e 64 20 6f 72 69 67 69 6e 0a 20  ype and origin. 
2673e 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20 66         ** data f
2673f 6f 72 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65  or the result-se
26740 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
26741 73 75 62 2d 73 65 6c 65 63 74 2e 0a 20 20 20 20  sub-select..    
26742 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
26743 66 28 20 41 4c 57 41 59 53 28 69 43 6f 6c 3e 3d  f( ALWAYS(iCol>=
26744 30 20 26 26 20 69 43 6f 6c 3c 70 53 2d 3e 70 45  0 && iCol<pS->pE
26745 4c 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a  List->nExpr) ){.
26746 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
26747 69 43 6f 6c 20 69 73 20 6c 65 73 73 20 74 68 61  iCol is less tha
26748 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n zero, then the
26749 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 71 75   expression requ
2674a 65 73 74 73 20 74 68 65 0a 20 20 20 20 20 20 20  ests the.       
2674b 20 20 20 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74     ** rowid of t
2674c 68 65 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72  he sub-select or
2674d 20 76 69 65 77 2e 20 54 68 69 73 20 65 78 70 72   view. This expr
2674e 65 73 73 69 6f 6e 20 69 73 20 6c 65 67 61 6c 20  ession is legal 
2674f 28 73 65 65 20 0a 20 20 20 20 20 20 20 20 20 20  (see .          
26750 2a 2a 20 74 65 73 74 20 63 61 73 65 20 6d 69 73  ** test case mis
26751 63 32 2e 32 2e 32 29 20 2d 20 69 74 20 61 6c 77  c2.2.2) - it alw
26752 61 79 73 20 65 76 61 6c 75 61 74 65 73 20 74 6f  ays evaluates to
26753 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 20   NULL..         
26754 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 4e 61   */.          Na
26755 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20  meContext sNC;. 
26756 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70           Expr *p
26757 20 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61   = pS->pEList->a
26758 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20  [iCol].pExpr;.  
26759 20 20 20 20 20 20 20 20 73 4e 43 2e 70 53 72 63          sNC.pSrc
2675a 4c 69 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b  List = pS->pSrc;
2675b 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e 70  .          sNC.p
2675c 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Next = 0;.      
2675d 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d      sNC.pParse =
2675e 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20   pNC->pParse;.  
2675f 20 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20          zType = 
26760 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c  columnType(&sNC,
26761 20 70 2c 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20   p, &zOriginDb, 
26762 26 7a 4f 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f  &zOriginTab, &zO
26763 72 69 67 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20  riginCol); .    
26764 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
26765 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61  e if( ALWAYS(pTa
26766 62 2d 3e 70 53 63 68 65 6d 61 29 20 29 7b 0a 20  b->pSchema) ){. 
26767 20 20 20 20 20 20 20 2f 2a 20 41 20 72 65 61 6c         /* A real
26768 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20   table */.      
26769 20 20 61 73 73 65 72 74 28 20 21 70 53 20 29 3b    assert( !pS );
2676a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
2676b 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61  l<0 ) iCol = pTa
2676c 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20  b->iPKey;.      
2676d 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3d 3d    assert( iCol==
2676e 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20 26  -1 || (iCol>=0 &
2676f 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  & iCol<pTab->nCo
26770 6c 29 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  l) );.        if
26771 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20  ( iCol<0 ){.    
26772 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 49        zType = "I
26773 4e 54 45 47 45 52 22 3b 0a 20 20 20 20 20 20 20  NTEGER";.       
26774 20 20 20 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20     zOriginCol = 
26775 22 72 6f 77 69 64 22 3b 0a 20 20 20 20 20 20 20  "rowid";.       
26776 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
26777 20 20 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e    zType = pTab->
26778 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65  aCol[iCol].zType
26779 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69  ;.          zOri
2677a 67 69 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61  ginCol = pTab->a
2677b 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b  Col[iCol].zName;
2677c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2677d 20 20 20 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20     zOriginTab = 
2677e 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20  pTab->zName;.   
2677f 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 70 50       if( pNC->pP
26780 61 72 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20  arse ){.        
26781 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69    int iDb = sqli
26782 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
26783 28 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62  (pNC->pParse->db
26784 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
26785 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69  ;.          zOri
26786 67 69 6e 44 62 20 3d 20 70 4e 43 2d 3e 70 50 61  ginDb = pNC->pPa
26787 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62  rse->db->aDb[iDb
26788 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ].zName;.       
26789 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
2678a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69   break;.    }.#i
2678b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2678c 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 63  T_SUBQUERY.    c
2678d 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b  ase TK_SELECT: {
2678e 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78  .      /* The ex
2678f 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 75  pression is a su
26790 62 2d 73 65 6c 65 63 74 2e 20 52 65 74 75 72 6e  b-select. Return
26791 20 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e   the declaration
26792 20 74 79 70 65 20 61 6e 64 0a 20 20 20 20 20 20   type and.      
26793 2a 2a 20 6f 72 69 67 69 6e 20 69 6e 66 6f 20 66  ** origin info f
26794 6f 72 20 74 68 65 20 73 69 6e 67 6c 65 20 63 6f  or the single co
26795 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75  lumn in the resu
26796 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45  lt set of the SE
26797 4c 45 43 54 0a 20 20 20 20 20 20 2a 2a 20 73 74  LECT.      ** st
26798 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 2a  atement..      *
26799 2f 0a 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74  /.      NameCont
2679a 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 20 20 53  ext sNC;.      S
2679b 65 6c 65 63 74 20 2a 70 53 20 3d 20 70 45 78 70  elect *pS = pExp
2679c 72 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20  r->pSelect;.    
2679d 20 20 45 78 70 72 20 2a 70 20 3d 20 70 53 2d 3e    Expr *p = pS->
2679e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2679f 70 72 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 53  pr;.      sNC.pS
267a0 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70 53 72  rcList = pS->pSr
267a1 63 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 4e 65  c;.      sNC.pNe
267a2 78 74 20 3d 20 70 4e 43 3b 0a 20 20 20 20 20 20  xt = pNC;.      
267a3 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43  sNC.pParse = pNC
267a4 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20  ->pParse;.      
267a5 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79  zType = columnTy
267a6 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72  pe(&sNC, p, &zOr
267a7 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67 69 6e  iginDb, &zOrigin
267a8 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43 6f 6c  Tab, &zOriginCol
267a9 29 3b 20 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ); .      break;
267aa 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
267ab 7d 0a 20 20 0a 20 20 69 66 28 20 70 7a 4f 72 69  }.  .  if( pzOri
267ac 67 69 6e 44 62 20 29 7b 0a 20 20 20 20 61 73 73  ginDb ){.    ass
267ad 65 72 74 28 20 70 7a 4f 72 69 67 69 6e 54 61 62  ert( pzOriginTab
267ae 20 26 26 20 70 7a 4f 72 69 67 69 6e 43 6f 6c 20   && pzOriginCol 
267af 29 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e  );.    *pzOrigin
267b0 44 62 20 3d 20 7a 4f 72 69 67 69 6e 44 62 3b 0a  Db = zOriginDb;.
267b1 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e 54 61 62      *pzOriginTab
267b2 20 3d 20 7a 4f 72 69 67 69 6e 54 61 62 3b 0a 20   = zOriginTab;. 
267b3 20 20 20 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 20     *pzOriginCol 
267b4 3d 20 7a 4f 72 69 67 69 6e 43 6f 6c 3b 0a 20 20  = zOriginCol;.  
267b5 7d 0a 20 20 72 65 74 75 72 6e 20 7a 54 79 70 65  }.  return zType
267b6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
267b7 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
267b8 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 42 45  ll tell the VDBE
267b9 20 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e   the declaration
267ba 20 74 79 70 65 73 20 6f 66 20 63 6f 6c 75 6d 6e   types of column
267bb 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75  s.** in the resu
267bc 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  lt set..*/.stati
267bd 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65 43  c void generateC
267be 6f 6c 75 6d 6e 54 79 70 65 73 28 0a 20 20 50 61  olumnTypes(.  Pa
267bf 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
267c0 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
267c1 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
267c2 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f 2a 20   *pTabList,  /* 
267c3 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 2a  List of tables *
267c4 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
267c5 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70 72 65  List    /* Expre
267c6 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e 67 20  ssions defining 
267c7 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
267c8 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  /.){.#ifndef SQL
267c9 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ITE_OMIT_DECLTYP
267ca 45 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  E.  Vdbe *v = pP
267cb 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
267cc 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74  nt i;.  NameCont
267cd 65 78 74 20 73 4e 43 3b 0a 20 20 73 4e 43 2e 70  ext sNC;.  sNC.p
267ce 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69  SrcList = pTabLi
267cf 73 74 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65  st;.  sNC.pParse
267d0 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 66 6f 72   = pParse;.  for
267d1 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e  (i=0; i<pEList->
267d2 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
267d3 20 45 78 70 72 20 2a 70 20 3d 20 70 45 4c 69 73   Expr *p = pELis
267d4 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  t->a[i].pExpr;. 
267d5 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
267d6 54 79 70 65 3b 0a 23 69 66 64 65 66 20 53 51 4c  Type;.#ifdef SQL
267d7 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
267d8 4e 5f 4d 45 54 41 44 41 54 41 0a 20 20 20 20 63  N_METADATA.    c
267d9 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67  onst char *zOrig
267da 44 62 20 3d 20 30 3b 0a 20 20 20 20 63 6f 6e 73  Db = 0;.    cons
267db 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 54 61 62  t char *zOrigTab
267dc 20 3d 20 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20   = 0;.    const 
267dd 63 68 61 72 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d  char *zOrigCol =
267de 20 30 3b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20   0;.    zType = 
267df 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c  columnType(&sNC,
267e0 20 70 2c 20 26 7a 4f 72 69 67 44 62 2c 20 26 7a   p, &zOrigDb, &z
267e1 4f 72 69 67 54 61 62 2c 20 26 7a 4f 72 69 67 43  OrigTab, &zOrigC
267e2 6f 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  ol);..    /* The
267e3 20 76 64 62 65 20 6d 75 73 74 20 6d 61 6b 65 20   vdbe must make 
267e4 69 74 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66 20  its own copy of 
267e5 74 68 65 20 63 6f 6c 75 6d 6e 2d 74 79 70 65 20  the column-type 
267e6 61 6e 64 20 6f 74 68 65 72 20 0a 20 20 20 20 2a  and other .    *
267e7 2a 20 63 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69  * column specifi
267e8 63 20 73 74 72 69 6e 67 73 2c 20 69 6e 20 63 61  c strings, in ca
267e9 73 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 73  se the schema is
267ea 20 72 65 73 65 74 20 62 65 66 6f 72 65 20 74 68   reset before th
267eb 69 73 0a 20 20 20 20 2a 2a 20 76 69 72 74 75 61  is.    ** virtua
267ec 6c 20 6d 61 63 68 69 6e 65 20 69 73 20 64 65 6c  l machine is del
267ed 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eted..    */.   
267ee 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
267ef 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
267f0 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 2c 20 7a  NAME_DATABASE, z
267f1 4f 72 69 67 44 62 2c 20 53 51 4c 49 54 45 5f 54  OrigDb, SQLITE_T
267f2 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73  RANSIENT);.    s
267f3 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
267f4 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
267f5 4d 45 5f 54 41 42 4c 45 2c 20 7a 4f 72 69 67 54  ME_TABLE, zOrigT
267f6 61 62 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ab, SQLITE_TRANS
267f7 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74  IENT);.    sqlit
267f8 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
267f9 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43  (v, i, COLNAME_C
267fa 4f 4c 55 4d 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c  OLUMN, zOrigCol,
267fb 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
267fc 54 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 7a 54  T);.#else.    zT
267fd 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65  ype = columnType
267fe 28 26 73 4e 43 2c 20 70 2c 20 30 2c 20 30 2c 20  (&sNC, p, 0, 0, 
267ff 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73  0);.#endif.    s
26800 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
26801 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
26802 4d 45 5f 44 45 43 4c 54 59 50 45 2c 20 7a 54 79  ME_DECLTYPE, zTy
26803 70 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  pe, SQLITE_TRANS
26804 49 45 4e 54 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  IENT);.  }.#endi
26805 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
26806 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0a 7d 0a 0a  _DECLTYPE */.}..
26807 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
26808 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 74 65  ode that will te
26809 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68 65 20  ll the VDBE the 
2680a 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73  names of columns
2680b 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c  .** in the resul
2680c 74 20 73 65 74 2e 20 20 54 68 69 73 20 69 6e 66  t set.  This inf
2680d 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64  ormation is used
2680e 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 0a   to provide the.
2680f 2a 2a 20 61 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65  ** azCol[] value
26810 73 20 69 6e 20 74 68 65 20 63 61 6c 6c 62 61 63  s in the callbac
26811 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
26812 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e  d generateColumn
26813 4e 61 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a  Names(.  Parse *
26814 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
26815 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
26816 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  /.  SrcList *pTa
26817 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20  bList,  /* List 
26818 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 45  of tables */.  E
26819 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
2681a 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
2681b 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 72  s defining the r
2681c 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29 7b 0a  esult set */.){.
2681d 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2681e 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
2681f 20 69 2c 20 6a 3b 0a 20 20 73 71 6c 69 74 65 33   i, j;.  sqlite3
26820 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
26821 62 3b 0a 20 20 69 6e 74 20 66 75 6c 6c 4e 61 6d  b;.  int fullNam
26822 65 73 2c 20 73 68 6f 72 74 4e 61 6d 65 73 3b 0a  es, shortNames;.
26823 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
26824 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 2f  OMIT_EXPLAIN.  /
26825 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
26826 45 58 50 4c 41 49 4e 2c 20 73 6b 69 70 20 74 68  EXPLAIN, skip th
26827 69 73 20 73 74 65 70 20 2a 2f 0a 20 20 69 66 28  is step */.  if(
26828 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
26829 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
2682a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73    }.#endif..  as
2682b 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20  sert( v!=0 );.  
2682c 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e  if( pParse->colN
2682d 61 6d 65 73 53 65 74 20 7c 7c 20 4e 45 56 45 52  amesSet || NEVER
2682e 28 76 3d 3d 30 29 20 7c 7c 20 64 62 2d 3e 6d 61  (v==0) || db->ma
2682f 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
26830 75 72 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63  urn;.  pParse->c
26831 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 31 3b 0a  olNamesSet = 1;.
26832 20 20 66 75 6c 6c 4e 61 6d 65 73 20 3d 20 28 64    fullNames = (d
26833 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
26834 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21  E_FullColNames)!
26835 3d 30 3b 0a 20 20 73 68 6f 72 74 4e 61 6d 65 73  =0;.  shortNames
26836 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20   = (db->flags & 
26837 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
26838 61 6d 65 73 29 21 3d 30 3b 0a 20 20 73 71 6c 69  ames)!=0;.  sqli
26839 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
2683a 73 28 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78  s(v, pEList->nEx
2683b 70 72 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  pr);.  for(i=0; 
2683c 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  i<pEList->nExpr;
2683d 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20   i++){.    Expr 
2683e 2a 70 3b 0a 20 20 20 20 70 20 3d 20 70 45 4c 69  *p;.    p = pELi
2683f 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  st->a[i].pExpr;.
26840 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 20 63      if( p==0 ) c
26841 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28  ontinue;.    if(
26842 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e   pEList->a[i].zN
26843 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 63 68 61  ame ){.      cha
26844 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 45 4c 69 73  r *zName = pELis
26845 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20  t->a[i].zName;. 
26846 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26847 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
26848 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a   COLNAME_NAME, z
26849 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 54 52 41  Name, SQLITE_TRA
2684a 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d 65 6c  NSIENT);.    }el
2684b 73 65 20 69 66 28 20 28 70 2d 3e 6f 70 3d 3d 54  se if( (p->op==T
2684c 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 2d 3e 6f  K_COLUMN || p->o
2684d 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
2684e 29 20 26 26 20 70 54 61 62 4c 69 73 74 20 29 7b  ) && pTabList ){
2684f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
26850 61 62 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  ab;.      char *
26851 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20  zCol;.      int 
26852 69 43 6f 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d  iCol = p->iColum
26853 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  n;.      for(j=0
26854 3b 20 41 4c 57 41 59 53 28 6a 3c 70 54 61 62 4c  ; ALWAYS(j<pTabL
26855 69 73 74 2d 3e 6e 53 72 63 29 3b 20 6a 2b 2b 29  ist->nSrc); j++)
26856 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54  {.        if( pT
26857 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75  abList->a[j].iCu
26858 72 73 6f 72 3d 3d 70 2d 3e 69 54 61 62 6c 65 20  rsor==p->iTable 
26859 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d  ) break;.      }
2685a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a  .      assert( j
2685b 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20  <pTabList->nSrc 
2685c 29 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  );.      pTab = 
2685d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70  pTabList->a[j].p
2685e 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 69  Tab;.      if( i
2685f 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70  Col<0 ) iCol = p
26860 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20  Tab->iPKey;.    
26861 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3d 3d    assert( iCol==
26862 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20 26  -1 || (iCol>=0 &
26863 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  & iCol<pTab->nCo
26864 6c 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  l) );.      if( 
26865 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20  iCol<0 ){.      
26866 20 20 7a 43 6f 6c 20 3d 20 22 72 6f 77 69 64 22    zCol = "rowid"
26867 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
26868 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54         zCol = pT
26869 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
2686a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Name;.      }.  
2686b 20 20 20 20 69 66 28 20 21 73 68 6f 72 74 4e 61      if( !shortNa
2686c 6d 65 73 20 26 26 20 21 66 75 6c 6c 4e 61 6d 65  mes && !fullName
2686d 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  s ){.        sql
2686e 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2686f 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
26870 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20  _NAME, .        
26871 20 20 20 20 73 71 6c 69 74 65 33 44 62 53 74 72      sqlite3DbStr
26872 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29  NDup(db, (char*)
26873 70 2d 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70  p->span.z, p->sp
26874 61 6e 2e 6e 29 2c 20 53 51 4c 49 54 45 5f 44 59  an.n), SQLITE_DY
26875 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 7d 65  NAMIC);.      }e
26876 6c 73 65 20 69 66 28 20 66 75 6c 6c 4e 61 6d 65  lse if( fullName
26877 73 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  s ){.        cha
26878 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20  r *zName = 0;.  
26879 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71        zName = sq
2687a 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2687b 20 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e   "%s.%s", pTab->
2687c 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20  zName, zCol);.  
2687d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2687e 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
2687f 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
26880 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 44 59  zName, SQLITE_DY
26881 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 7d 65  NAMIC);.      }e
26882 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
26883 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
26884 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
26885 5f 4e 41 4d 45 2c 20 7a 43 6f 6c 2c 20 53 51 4c  _NAME, zCol, SQL
26886 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
26887 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
26888 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
26889 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
2688a 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , i, COLNAME_NAM
2688b 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73 71  E, .          sq
2688c 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64  lite3DbStrNDup(d
2688d 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73 70 61  b, (char*)p->spa
2688e 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e 29 2c  n.z, p->span.n),
2688f 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
26890 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 67 65  ;.    }.  }.  ge
26891 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65  nerateColumnType
26892 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  s(pParse, pTabLi
26893 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a  st, pEList);.}..
26894 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
26895 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
26896 45 43 54 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f  ECT./*.** Name o
26897 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  f the connection
26898 20 6f 70 65 72 61 74 6f 72 2c 20 75 73 65 64 20   operator, used 
26899 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67  for error messag
2689a 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  es..*/.static co
2689b 6e 73 74 20 63 68 61 72 20 2a 73 65 6c 65 63 74  nst char *select
2689c 4f 70 4e 61 6d 65 28 69 6e 74 20 69 64 29 7b 0a  OpName(int id){.
2689d 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 77 69    char *z;.  swi
2689e 74 63 68 28 20 69 64 20 29 7b 0a 20 20 20 20 63  tch( id ){.    c
2689f 61 73 65 20 54 4b 5f 41 4c 4c 3a 20 20 20 20 20  ase TK_ALL:     
268a0 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c    z = "UNION ALL
268a1 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ";   break;.    
268a2 63 61 73 65 20 54 4b 5f 49 4e 54 45 52 53 45 43  case TK_INTERSEC
268a3 54 3a 20 7a 20 3d 20 22 49 4e 54 45 52 53 45 43  T: z = "INTERSEC
268a4 54 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  T";   break;.   
268a5 20 63 61 73 65 20 54 4b 5f 45 58 43 45 50 54 3a   case TK_EXCEPT:
268a6 20 20 20 20 7a 20 3d 20 22 45 58 43 45 50 54 22      z = "EXCEPT"
268a7 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;      break;.  
268a8 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20    default:      
268a9 20 20 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 22       z = "UNION"
268aa 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;       break;. 
268ab 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d   }.  return z;.}
268ac 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
268ad 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
268ae 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  SELECT */../*.**
268af 20 47 69 76 65 6e 20 61 20 61 6e 20 65 78 70 72   Given a an expr
268b0 65 73 73 69 6f 6e 20 6c 69 73 74 20 28 77 68 69  ession list (whi
268b1 63 68 20 69 73 20 72 65 61 6c 6c 79 20 74 68 65  ch is really the
268b2 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
268b3 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 66 6f 72  ions.** that for
268b4 6d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  m the result set
268b5 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
268b6 74 65 6d 65 6e 74 29 20 63 6f 6d 70 75 74 65 20  tement) compute 
268b7 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 63  appropriate.** c
268b8 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20  olumn names for 
268b9 61 20 74 61 62 6c 65 20 74 68 61 74 20 77 6f 75  a table that wou
268ba 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 78 70 72  ld hold the expr
268bb 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a  ession list..**.
268bc 2a 2a 20 41 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61  ** All column na
268bd 6d 65 73 20 77 69 6c 6c 20 62 65 20 75 6e 69 71  mes will be uniq
268be 75 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74  ue..**.** Only t
268bf 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
268c0 61 72 65 20 63 6f 6d 70 75 74 65 64 2e 20 20 43  are computed.  C
268c1 6f 6c 75 6d 6e 2e 7a 54 79 70 65 2c 20 43 6f 6c  olumn.zType, Col
268c2 75 6d 6e 2e 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61 6e  umn.zColl,.** an
268c3 64 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20 6f  d other fields o
268c4 66 20 43 6f 6c 75 6d 6e 20 61 72 65 20 7a 65 72  f Column are zer
268c5 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  oed..**.** Retur
268c6 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
268c7 75 63 63 65 73 73 2e 20 20 49 66 20 61 20 6d 65  uccess.  If a me
268c8 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
268c9 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a  error occurs,.**
268ca 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 2a   store NULL in *
268cb 70 61 43 6f 6c 20 61 6e 64 20 30 20 69 6e 20 2a  paCol and 0 in *
268cc 70 6e 43 6f 6c 20 61 6e 64 20 72 65 74 75 72 6e  pnCol and return
268cd 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a   SQLITE_NOMEM..*
268ce 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c  /.static int sel
268cf 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78  ectColumnsFromEx
268d0 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20  prList(.  Parse 
268d1 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
268d2 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
268d3 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
268d4 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20  st *pEList,     
268d5 20 20 2f 2a 20 45 78 70 72 20 6c 69 73 74 20 66    /* Expr list f
268d6 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 64 65 72  rom which to der
268d7 69 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  ive column names
268d8 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c   */.  int *pnCol
268d9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
268da 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65   Write the numbe
268db 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 68 65 72  r of columns her
268dc 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a  e */.  Column **
268dd 70 61 43 6f 6c 20 20 20 20 20 20 20 20 20 20 2f  paCol          /
268de 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20  * Write the new 
268df 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 68 65 72 65  column list here
268e0 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
268e1 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
268e2 62 3b 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  b;   /* Database
268e3 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
268e4 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
268e5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
268e6 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f  Loop counters */
268e7 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20  .  int cnt;     
268e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
268e9 2a 20 49 6e 64 65 78 20 61 64 64 65 64 20 74 6f  * Index added to
268ea 20 6d 61 6b 65 20 74 68 65 20 6e 61 6d 65 20 75   make the name u
268eb 6e 69 71 75 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d  nique */.  Colum
268ec 6e 20 2a 61 43 6f 6c 2c 20 2a 70 43 6f 6c 3b 20  n *aCol, *pCol; 
268ed 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
268ee 6f 70 69 6e 67 20 6f 76 65 72 20 72 65 73 75 6c  oping over resul
268ef 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69  t columns */.  i
268f0 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt nCol;        
268f1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
268f2 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
268f3 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
268f4 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 3b 20  t */.  Expr *p; 
268f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
268f6 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
268f7 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65   for a single re
268f8 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  sult column */. 
268f9 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
268fa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
268fb 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20  Column name */. 
268fc 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20   int nName;     
268fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
268fe 53 69 7a 65 20 6f 66 20 6e 61 6d 65 20 69 6e 20  Size of name in 
268ff 7a 4e 61 6d 65 5b 5d 20 2a 2f 0a 0a 20 20 2a 70  zName[] */..  *p
26900 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 20 3d 20 70 45  nCol = nCol = pE
26901 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 61  List->nExpr;.  a
26902 43 6f 6c 20 3d 20 2a 70 61 43 6f 6c 20 3d 20 73  Col = *paCol = s
26903 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
26904 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 61 43  ro(db, sizeof(aC
26905 6f 6c 5b 30 5d 29 2a 6e 43 6f 6c 29 3b 0a 20 20  ol[0])*nCol);.  
26906 69 66 28 20 61 43 6f 6c 3d 3d 30 20 29 20 72 65  if( aCol==0 ) re
26907 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
26908 4d 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43  M;.  for(i=0, pC
26909 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b  ol=aCol; i<nCol;
2690a 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20   i++, pCol++){. 
2690b 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 61 70 70     /* Get an app
2690c 72 6f 70 72 69 61 74 65 20 6e 61 6d 65 20 66 6f  ropriate name fo
2690d 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20  r the column.   
2690e 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 45 4c 69   */.    p = pELi
2690f 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  st->a[i].pExpr;.
26910 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
26911 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70  Right==0 || p->p
26912 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d  Right->token.z==
26913 30 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e  0 || p->pRight->
26914 74 6f 6b 65 6e 2e 7a 5b 30 5d 21 3d 30 20 29 3b  token.z[0]!=0 );
26915 0a 20 20 20 20 69 66 28 20 28 7a 4e 61 6d 65 20  .    if( (zName 
26916 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  = pEList->a[i].z
26917 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Name)!=0 ){.    
26918 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c 75    /* If the colu
26919 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 22  mn contains an "
2691a 41 53 20 3c 6e 61 6d 65 3e 22 20 70 68 72 61 73  AS <name>" phras
2691b 65 2c 20 75 73 65 20 3c 6e 61 6d 65 3e 20 61 73  e, use <name> as
2691c 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20   the name */.   
2691d 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
2691e 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a  e3DbStrDup(db, z
2691f 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Name);.    }else
26920 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 43  {.      Expr *pC
26921 6f 6c 45 78 70 72 20 3d 20 70 3b 20 20 2f 2a 20  olExpr = p;  /* 
26922 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  The expression t
26923 68 61 74 20 69 73 20 74 68 65 20 72 65 73 75 6c  hat is the resul
26924 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f  t column name */
26925 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
26926 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ab;         /* T
26927 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20  able associated 
26928 77 69 74 68 20 74 68 69 73 20 65 78 70 72 65 73  with this expres
26929 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68  sion */.      wh
2692a 69 6c 65 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f  ile( pColExpr->o
2692b 70 3d 3d 54 4b 5f 44 4f 54 20 29 20 70 43 6f 6c  p==TK_DOT ) pCol
2692c 45 78 70 72 20 3d 20 70 43 6f 6c 45 78 70 72 2d  Expr = pColExpr-
2692d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69  >pRight;.      i
2692e 66 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d  f( pColExpr->op=
2692f 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 28 70  =TK_COLUMN && (p
26930 54 61 62 20 3d 20 70 43 6f 6c 45 78 70 72 2d 3e  Tab = pColExpr->
26931 70 54 61 62 29 21 3d 30 20 29 7b 0a 20 20 20 20  pTab)!=0 ){.    
26932 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d      /* For colum
26933 6e 73 20 75 73 65 20 74 68 65 20 63 6f 6c 75 6d  ns use the colum
26934 6e 20 6e 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20  n name name */. 
26935 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20         int iCol 
26936 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 69 43 6f 6c  = pColExpr->iCol
26937 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 69 66 28  umn;.        if(
26938 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d   iCol<0 ) iCol =
26939 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20   pTab->iPKey;.  
2693a 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71        zName = sq
2693b 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2693c 20 22 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20   "%s",.         
2693d 20 20 20 20 20 20 20 20 69 43 6f 6c 3e 3d 30 20          iCol>=0 
2693e 3f 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  ? pTab->aCol[iCo
2693f 6c 5d 2e 7a 4e 61 6d 65 20 3a 20 22 72 6f 77 69  l].zName : "rowi
26940 64 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  d");.      }else
26941 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65  {.        /* Use
26942 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65   the original te
26943 78 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  xt of the column
26944 20 65 78 70 72 65 73 73 69 6f 6e 20 61 73 20 69   expression as i
26945 74 73 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20  ts name */.     
26946 20 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e     Token *pToken
26947 20 3d 20 28 70 43 6f 6c 45 78 70 72 2d 3e 73 70   = (pColExpr->sp
26948 61 6e 2e 7a 3f 26 70 43 6f 6c 45 78 70 72 2d 3e  an.z?&pColExpr->
26949 73 70 61 6e 3a 26 70 43 6f 6c 45 78 70 72 2d 3e  span:&pColExpr->
2694a 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20  token);.        
2694b 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  zName = sqlite3M
2694c 50 72 69 6e 74 66 28 64 62 2c 20 22 25 54 22 2c  Printf(db, "%T",
2694d 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20   pToken);.      
2694e 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
2694f 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
26950 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
26951 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
26952 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  e);.      break;
26953 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
26954 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65 29  e3Dequote(zName)
26955 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73  ;..    /* Make s
26956 75 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  ure the column n
26957 61 6d 65 20 69 73 20 75 6e 69 71 75 65 2e 20 20  ame is unique.  
26958 49 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6e  If the name is n
26959 6f 74 20 75 6e 69 71 75 65 2c 0a 20 20 20 20 2a  ot unique,.    *
2695a 2a 20 61 70 70 65 6e 64 20 61 20 69 6e 74 65 67  * append a integ
2695b 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 73  er to the name s
2695c 6f 20 74 68 61 74 20 69 74 20 62 65 63 6f 6d 65  o that it become
2695d 73 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f  s unique..    */
2695e 0a 20 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c  .    nName = sql
2695f 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
26960 6d 65 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 63  me);.    for(j=c
26961 6e 74 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b  nt=0; j<i; j++){
26962 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
26963 65 33 53 74 72 49 43 6d 70 28 61 43 6f 6c 5b 6a  e3StrICmp(aCol[j
26964 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d  ].zName, zName)=
26965 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  =0 ){.        ch
26966 61 72 20 2a 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20  ar *zNewName;.  
26967 20 20 20 20 20 20 7a 4e 61 6d 65 5b 6e 4e 61 6d        zName[nNam
26968 65 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  e] = 0;.        
26969 7a 4e 65 77 4e 61 6d 65 20 3d 20 73 71 6c 69 74  zNewName = sqlit
2696a 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
2696b 73 3a 25 64 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b  s:%d", zName, ++
2696c 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  cnt);.        sq
2696d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2696e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
2696f 7a 4e 61 6d 65 20 3d 20 7a 4e 65 77 4e 61 6d 65  zName = zNewName
26970 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 2d 31  ;.        j = -1
26971 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 4e  ;.        if( zN
26972 61 6d 65 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  ame==0 ) break;.
26973 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
26974 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20    pCol->zName = 
26975 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 69 66 28  zName;.  }.  if(
26976 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
26977 64 20 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 30  d ){.    for(j=0
26978 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ; j<i; j++){.   
26979 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2697a 28 64 62 2c 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  (db, aCol[j].zNa
2697b 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  me);.    }.    s
2697c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2697d 20 61 43 6f 6c 29 3b 0a 20 20 20 20 2a 70 61 43   aCol);.    *paC
2697e 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 2a 70 6e 43  ol = 0;.    *pnC
2697f 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  ol = 0;.    retu
26980 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
26981 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
26982 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
26983 2a 20 41 64 64 20 74 79 70 65 20 61 6e 64 20 63  * Add type and c
26984 6f 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61  ollation informa
26985 74 69 6f 6e 20 74 6f 20 61 20 63 6f 6c 75 6d 6e  tion to a column
26986 20 6c 69 73 74 20 62 61 73 65 64 20 6f 6e 0a 2a   list based on.*
26987 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  * a SELECT state
26988 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65  ment..** .** The
26989 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 70 72 65   column list pre
2698a 73 75 6d 61 62 6c 79 20 63 61 6d 65 20 66 72 6f  sumably came fro
2698b 6d 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 4e 61  m selectColumnNa
2698c 6d 65 73 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  mesFromExprList(
2698d 29 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e  )..** The column
2698e 20 6c 69 73 74 20 68 61 73 20 6f 6e 6c 79 20 6e   list has only n
2698f 61 6d 65 73 2c 20 6e 6f 74 20 74 79 70 65 73 20  ames, not types 
26990 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e 20 20  or collations.  
26991 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  This.** routine 
26992 67 6f 65 73 20 74 68 72 6f 75 67 68 20 61 6e 64  goes through and
26993 20 61 64 64 73 20 74 68 65 20 74 79 70 65 73 20   adds the types 
26994 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e 0a  and collations..
26995 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
26996 6e 65 20 72 65 71 75 69 72 65 73 20 74 68 61 74  ne requires that
26997 20 61 6c 6c 20 69 6e 64 65 6e 74 69 66 69 65 72   all indentifier
26998 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a  s in the SELECT.
26999 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 62 65 20  ** statement be 
2699a 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61  resolved..*/.sta
2699b 74 69 63 20 76 6f 69 64 20 73 65 6c 65 63 74 41  tic void selectA
2699c 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43  ddColumnTypeAndC
2699d 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 50 61 72 73  ollation(.  Pars
2699e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2699f 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
269a0 74 65 78 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  texts */.  int n
269a1 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Col,            
269a2 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
269a3 6c 75 6d 6e 73 20 2a 2f 0a 20 20 43 6f 6c 75 6d  lumns */.  Colum
269a4 6e 20 2a 61 43 6f 6c 2c 20 20 20 20 20 20 20 20  n *aCol,        
269a5 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75   /* List of colu
269a6 6d 6e 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  mns */.  Select 
269a7 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f  *pSelect       /
269a8 2a 20 53 45 4c 45 43 54 20 75 73 65 64 20 74 6f  * SELECT used to
269a9 20 64 65 74 65 72 6d 69 6e 65 20 74 79 70 65 73   determine types
269aa 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20   and collations 
269ab 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
269ac 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
269ad 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
269ae 73 4e 43 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70  sNC;.  Column *p
269af 43 6f 6c 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  Col;.  CollSeq *
269b0 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a  pColl;.  int i;.
269b1 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 73 74 72    Expr *p;.  str
269b2 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
269b3 6d 20 2a 61 3b 0a 0a 20 20 61 73 73 65 72 74 28  m *a;..  assert(
269b4 20 70 53 65 6c 65 63 74 21 3d 30 20 29 3b 0a 20   pSelect!=0 );. 
269b5 20 61 73 73 65 72 74 28 20 28 70 53 65 6c 65 63   assert( (pSelec
269b6 74 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  t->selFlags & SF
269b7 5f 52 65 73 6f 6c 76 65 64 29 21 3d 30 20 29 3b  _Resolved)!=0 );
269b8 0a 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 3d  .  assert( nCol=
269b9 3d 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74  =pSelect->pEList
269ba 2d 3e 6e 45 78 70 72 20 7c 7c 20 64 62 2d 3e 6d  ->nExpr || db->m
269bb 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
269bc 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
269bd 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
269be 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
269bf 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
269c0 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
269c1 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a   pSelect->pSrc;.
269c2 20 20 61 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70    a = pSelect->p
269c3 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 66 6f 72 28  EList->a;.  for(
269c4 69 3d 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20  i=0, pCol=aCol; 
269c5 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  i<nCol; i++, pCo
269c6 6c 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 61 5b  l++){.    p = a[
269c7 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 43  i].pExpr;.    pC
269c8 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69  ol->zType = sqli
269c9 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
269ca 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c  columnType(&sNC,
269cb 20 70 2c 20 30 2c 20 30 2c 20 30 29 29 3b 0a 20   p, 0, 0, 0));. 
269cc 20 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74     pCol->affinit
269cd 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  y = sqlite3ExprA
269ce 66 66 69 6e 69 74 79 28 70 29 3b 0a 20 20 20 20  ffinity(p);.    
269cf 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
269d0 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
269d1 65 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70  e, p);.    if( p
269d2 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43  Coll ){.      pC
269d3 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69  ol->zColl = sqli
269d4 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
269d5 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pColl->zName);. 
269d6 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
269d7 2a 20 47 69 76 65 6e 20 61 20 53 45 4c 45 43 54  * Given a SELECT
269d8 20 73 74 61 74 65 6d 65 6e 74 2c 20 67 65 6e 65   statement, gene
269d9 72 61 74 65 20 61 20 54 61 62 6c 65 20 73 74 72  rate a Table str
269da 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63  ucture that desc
269db 72 69 62 65 73 0a 2a 2a 20 74 68 65 20 72 65 73  ribes.** the res
269dc 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 20  ult set of that 
269dd 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54  SELECT..*/.SQLIT
269de 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20  E_PRIVATE Table 
269df 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65  *sqlite3ResultSe
269e0 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 20  tOfSelect(Parse 
269e1 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20  *pParse, Select 
269e2 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 54 61 62  *pSelect){.  Tab
269e3 6c 65 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69  le *pTab;.  sqli
269e4 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
269e5 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 73 61 76 65  ->db;.  int save
269e6 64 46 6c 61 67 73 3b 0a 0a 20 20 73 61 76 65 64  dFlags;..  saved
269e7 46 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67  Flags = db->flag
269e8 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  s;.  db->flags &
269e9 3d 20 7e 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f  = ~SQLITE_FullCo
269ea 6c 4e 61 6d 65 73 3b 0a 20 20 64 62 2d 3e 66 6c  lNames;.  db->fl
269eb 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68  ags |= SQLITE_Sh
269ec 6f 72 74 43 6f 6c 4e 61 6d 65 73 3b 0a 20 20 73  ortColNames;.  s
269ed 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70  qlite3SelectPrep
269ee 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
269ef 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 50 61 72  , 0);.  if( pPar
269f0 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72  se->nErr ) retur
269f1 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53  n 0;.  while( pS
269f2 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 20  elect->pPrior ) 
269f3 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
269f4 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 64 62 2d  t->pPrior;.  db-
269f5 3e 66 6c 61 67 73 20 3d 20 73 61 76 65 64 46 6c  >flags = savedFl
269f6 61 67 73 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  ags;.  pTab = sq
269f7 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
269f8 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62  o(db, sizeof(Tab
269f9 6c 65 29 20 29 3b 0a 20 20 69 66 28 20 70 54 61  le) );.  if( pTa
269fa 62 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  b==0 ){.    retu
269fb 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62  rn 0;.  }.  pTab
269fc 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 54 61  ->db = db;.  pTa
269fd 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70  b->nRef = 1;.  p
269fe 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a  Tab->zName = 0;.
269ff 20 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46    selectColumnsF
26a00 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72  romExprList(pPar
26a01 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  se, pSelect->pEL
26a02 69 73 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f 6c  ist, &pTab->nCol
26a03 2c 20 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a  , &pTab->aCol);.
26a04 20 20 73 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d    selectAddColum
26a05 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f  nTypeAndCollatio
26a06 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e  n(pParse, pTab->
26a07 6e 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  nCol, pTab->aCol
26a08 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 70 54  , pSelect);.  pT
26a09 61 62 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a  ab->iPKey = -1;.
26a0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
26a0b 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71  Failed ){.    sq
26a0c 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
26a0d 28 70 54 61 62 29 3b 0a 20 20 20 20 72 65 74 75  (pTab);.    retu
26a0e 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 0;.  }.  retu
26a0f 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pTab;.}../*.*
26a10 2a 20 47 65 74 20 61 20 56 44 42 45 20 66 6f 72  * Get a VDBE for
26a11 20 74 68 65 20 67 69 76 65 6e 20 70 61 72 73 65   the given parse
26a12 72 20 63 6f 6e 74 65 78 74 2e 20 20 43 72 65 61  r context.  Crea
26a13 74 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20  te a new one if 
26a14 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 49 66  necessary..** If
26a15 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
26a16 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 61 6e  , return NULL an
26a17 64 20 6c 65 61 76 65 20 61 20 6d 65 73 73 61 67  d leave a messag
26a18 65 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a  e in pParse..*/.
26a19 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
26a1a 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56  dbe *sqlite3GetV
26a1b 64 62 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  dbe(Parse *pPars
26a1c 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  e){.  Vdbe *v = 
26a1d 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
26a1e 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20   if( v==0 ){.   
26a1f 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64   v = pParse->pVd
26a20 62 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  be = sqlite3Vdbe
26a21 43 72 65 61 74 65 28 70 50 61 72 73 65 2d 3e 64  Create(pParse->d
26a22 62 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  b);.#ifndef SQLI
26a23 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20  TE_OMIT_TRACE.  
26a24 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20    if( v ){.     
26a25 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26a26 70 30 28 76 2c 20 4f 50 5f 54 72 61 63 65 29 3b  p0(v, OP_Trace);
26a27 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
26a28 7d 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a  }.  return v;.}.
26a29 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ../*.** Compute 
26a2a 74 68 65 20 69 4c 69 6d 69 74 20 61 6e 64 20 69  the iLimit and i
26a2b 4f 66 66 73 65 74 20 66 69 65 6c 64 73 20 6f 66  Offset fields of
26a2c 20 74 68 65 20 53 45 4c 45 43 54 20 62 61 73 65   the SELECT base
26a2d 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 70 4c 69 6d  d on the.** pLim
26a2e 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74 20 65  it and pOffset e
26a2f 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 70 4c 69  xpressions.  pLi
26a30 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74 20  mit and pOffset 
26a31 68 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73  hold the express
26a32 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 70 70  ions.** that app
26a33 65 61 72 20 69 6e 20 74 68 65 20 6f 72 69 67 69  ear in the origi
26a34 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  nal SQL statemen
26a35 74 20 61 66 74 65 72 20 74 68 65 20 4c 49 4d 49  t after the LIMI
26a36 54 20 61 6e 64 20 4f 46 46 53 45 54 0a 2a 2a 20  T and OFFSET.** 
26a37 6b 65 79 77 6f 72 64 73 2e 20 20 4f 72 20 4e 55  keywords.  Or NU
26a38 4c 4c 20 69 66 20 74 68 6f 73 65 20 6b 65 79 77  LL if those keyw
26a39 6f 72 64 73 20 61 72 65 20 6f 6d 69 74 74 65 64  ords are omitted
26a3a 2e 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66  . iLimit and iOf
26a3b 66 73 65 74 20 0a 2a 2a 20 61 72 65 20 74 68 65  fset .** are the
26a3c 20 69 6e 74 65 67 65 72 20 6d 65 6d 6f 72 79 20   integer memory 
26a3d 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73  register numbers
26a3e 20 66 6f 72 20 63 6f 75 6e 74 65 72 73 20 75 73   for counters us
26a3f 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 0a 2a  ed to compute .*
26a40 2a 20 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20  * the limit and 
26a41 6f 66 66 73 65 74 2e 20 20 49 66 20 74 68 65 72  offset.  If ther
26a42 65 20 69 73 20 6e 6f 20 6c 69 6d 69 74 20 61 6e  e is no limit an
26a43 64 2f 6f 72 20 6f 66 66 73 65 74 2c 20 74 68 65  d/or offset, the
26a44 6e 20 0a 2a 2a 20 69 4c 69 6d 69 74 20 61 6e 64  n .** iLimit and
26a45 20 69 4f 66 66 73 65 74 20 61 72 65 20 6e 65 67   iOffset are neg
26a46 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ative..**.** Thi
26a47 73 20 72 6f 75 74 69 6e 65 20 63 68 61 6e 67 65  s routine change
26a48 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  s the values of 
26a49 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73  iLimit and iOffs
26a4a 65 74 20 6f 6e 6c 79 20 69 66 0a 2a 2a 20 61 20  et only if.** a 
26a4b 6c 69 6d 69 74 20 6f 72 20 6f 66 66 73 65 74 20  limit or offset 
26a4c 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 70 4c  is defined by pL
26a4d 69 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74  imit and pOffset
26a4e 2e 20 20 69 4c 69 6d 69 74 20 61 6e 64 0a 2a 2a  .  iLimit and.**
26a4f 20 69 4f 66 66 73 65 74 20 73 68 6f 75 6c 64 20   iOffset should 
26a50 68 61 76 65 20 62 65 65 6e 20 70 72 65 73 65 74  have been preset
26a51 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20   to appropriate 
26a52 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73 0a 2a  default values.*
26a53 2a 20 28 75 73 75 61 6c 6c 79 20 62 75 74 20 6e  * (usually but n
26a54 6f 74 20 61 6c 77 61 79 73 20 2d 31 29 20 70 72  ot always -1) pr
26a55 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74  ior to calling t
26a56 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20  his routine..** 
26a57 4f 6e 6c 79 20 69 66 20 70 4c 69 6d 69 74 21 3d  Only if pLimit!=
26a58 30 20 6f 72 20 70 4f 66 66 73 65 74 21 3d 30 20  0 or pOffset!=0 
26a59 64 6f 20 74 68 65 20 6c 69 6d 69 74 20 72 65 67  do the limit reg
26a5a 69 73 74 65 72 73 20 67 65 74 0a 2a 2a 20 72 65  isters get.** re
26a5b 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 55 4e  defined.  The UN
26a5c 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72  ION ALL operator
26a5d 20 75 73 65 73 20 74 68 69 73 20 70 72 6f 70 65   uses this prope
26a5e 72 74 79 20 74 6f 20 66 6f 72 63 65 0a 2a 2a 20  rty to force.** 
26a5f 74 68 65 20 72 65 75 73 65 20 6f 66 20 74 68 65  the reuse of the
26a60 20 73 61 6d 65 20 6c 69 6d 69 74 20 61 6e 64 20   same limit and 
26a61 6f 66 66 73 65 74 20 72 65 67 69 73 74 65 72 73  offset registers
26a62 20 61 63 72 6f 73 73 20 6d 75 6c 74 69 70 6c 65   across multiple
26a63 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65  .** SELECT state
26a64 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ments..*/.static
26a65 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4c 69 6d   void computeLim
26a66 69 74 52 65 67 69 73 74 65 72 73 28 50 61 72 73  itRegisters(Pars
26a67 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63  e *pParse, Selec
26a68 74 20 2a 70 2c 20 69 6e 74 20 69 42 72 65 61 6b  t *p, int iBreak
26a69 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30  ){.  Vdbe *v = 0
26a6a 3b 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 20 3d  ;.  int iLimit =
26a6b 20 30 3b 0a 20 20 69 6e 74 20 69 4f 66 66 73 65   0;.  int iOffse
26a6c 74 3b 0a 20 20 69 6e 74 20 61 64 64 72 31 3b 0a  t;.  int addr1;.
26a6d 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20    if( p->iLimit 
26a6e 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20  ) return;..  /* 
26a6f 0a 20 20 2a 2a 20 22 4c 49 4d 49 54 20 2d 31 22  .  ** "LIMIT -1"
26a70 20 61 6c 77 61 79 73 20 73 68 6f 77 73 20 61 6c   always shows al
26a71 6c 20 72 6f 77 73 2e 20 20 54 68 65 72 65 20 69  l rows.  There i
26a72 73 20 73 6f 6d 65 0a 20 20 2a 2a 20 63 6f 6e 74  s some.  ** cont
26a73 72 61 76 65 72 73 79 20 61 62 6f 75 74 20 77 68  raversy about wh
26a74 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 62  at the correct b
26a75 65 68 61 76 69 6f 72 20 73 68 6f 75 6c 64 20 62  ehavior should b
26a76 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 75 72 72  e..  ** The curr
26a77 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ent implementati
26a78 6f 6e 20 69 6e 74 65 72 70 72 65 74 73 20 22 4c  on interprets "L
26a79 49 4d 49 54 20 30 22 20 74 6f 20 6d 65 61 6e 0a  IMIT 0" to mean.
26a7a 20 20 2a 2a 20 6e 6f 20 72 6f 77 73 2e 0a 20 20    ** no rows..  
26a7b 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d  */.  if( p->pLim
26a7c 69 74 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4c 69  it ){.    p->iLi
26a7d 6d 69 74 20 3d 20 69 4c 69 6d 69 74 20 3d 20 2b  mit = iLimit = +
26a7e 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
26a7f 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
26a80 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
26a81 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65     if( v==0 ) re
26a82 74 75 72 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  turn;.    sqlite
26a83 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
26a84 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 69 4c 69  , p->pLimit, iLi
26a85 6d 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  mit);.    sqlite
26a86 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
26a87 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4c 69  P_MustBeInt, iLi
26a88 6d 69 74 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  mit);.    VdbeCo
26a89 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49 54  mment((v, "LIMIT
26a8a 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20 20   counter"));.   
26a8b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26a8c 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c  p2(v, OP_IfZero,
26a8d 20 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 29   iLimit, iBreak)
26a8e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
26a8f 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 70 2d  Offset ){.    p-
26a90 3e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 66 73  >iOffset = iOffs
26a91 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  et = ++pParse->n
26a92 4d 65 6d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  Mem;.    if( p->
26a93 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20  pLimit ){.      
26a94 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 20  pParse->nMem++; 
26a95 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e    /* Allocate an
26a96 20 65 78 74 72 61 20 72 65 67 69 73 74 65 72 20   extra register 
26a97 66 6f 72 20 6c 69 6d 69 74 2b 6f 66 66 73 65 74  for limit+offset
26a98 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 76 20   */.    }.    v 
26a99 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
26a9a 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
26a9b 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
26a9c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
26a9d 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d 3e  Code(pParse, p->
26a9e 70 4f 66 66 73 65 74 2c 20 69 4f 66 66 73 65 74  pOffset, iOffset
26a9f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
26aa0 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d  beAddOp1(v, OP_M
26aa1 75 73 74 42 65 49 6e 74 2c 20 69 4f 66 66 73 65  ustBeInt, iOffse
26aa2 74 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  t);.    VdbeComm
26aa3 65 6e 74 28 28 76 2c 20 22 4f 46 46 53 45 54 20  ent((v, "OFFSET 
26aa4 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20 20 20  counter"));.    
26aa5 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56  addr1 = sqlite3V
26aa6 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
26aa7 49 66 50 6f 73 2c 20 69 4f 66 66 73 65 74 29 3b  IfPos, iOffset);
26aa8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26aa9 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
26aaa 65 67 65 72 2c 20 30 2c 20 69 4f 66 66 73 65 74  eger, 0, iOffset
26aab 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
26aac 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
26aad 64 72 31 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  dr1);.    if( p-
26aae 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20  >pLimit ){.     
26aaf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26ab0 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c 20 69 4c  p3(v, OP_Add, iL
26ab1 69 6d 69 74 2c 20 69 4f 66 66 73 65 74 2c 20 69  imit, iOffset, i
26ab2 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20 20 20 20  Offset+1);.     
26ab3 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
26ab4 20 22 4c 49 4d 49 54 2b 4f 46 46 53 45 54 22 29   "LIMIT+OFFSET")
26ab5 29 3b 0a 20 20 20 20 20 20 61 64 64 72 31 20 3d  );.      addr1 =
26ab6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26ab7 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p1(v, OP_IfPos, 
26ab8 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 73  iLimit);.      s
26ab9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26aba 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
26abb 2d 31 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b 0a  -1, iOffset+1);.
26abc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26abd 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
26abe 72 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  r1);.    }.  }.}
26abf 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
26ac0 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
26ac1 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ELECT./*.** Retu
26ac2 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  rn the appropria
26ac3 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  te collating seq
26ac4 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 69 43  uence for the iC
26ac5 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a  ol-th column of.
26ac6 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ** the result se
26ac7 74 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f 75  t for the compou
26ac8 6e 64 2d 73 65 6c 65 63 74 20 73 74 61 74 65 6d  nd-select statem
26ac9 65 6e 74 20 22 70 22 2e 20 20 52 65 74 75 72 6e  ent "p".  Return
26aca 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20 74 68 65 20   NULL if.** the 
26acb 63 6f 6c 75 6d 6e 20 68 61 73 20 6e 6f 20 64 65  column has no de
26acc 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20  fault collating 
26acd 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  sequence..**.** 
26ace 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  The collating se
26acf 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 63  quence for the c
26ad0 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 69  ompound select i
26ad1 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65  s taken from the
26ad2 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74 65  .** left-most te
26ad3 72 6d 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  rm of the select
26ad4 20 74 68 61 74 20 68 61 73 20 61 20 63 6f 6c 6c   that has a coll
26ad5 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a  ating sequence..
26ad6 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65  */.static CollSe
26ad7 71 20 2a 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f  q *multiSelectCo
26ad8 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61  llSeq(Parse *pPa
26ad9 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20  rse, Select *p, 
26ada 69 6e 74 20 69 43 6f 6c 29 7b 0a 20 20 43 6f 6c  int iCol){.  Col
26adb 6c 53 65 71 20 2a 70 52 65 74 3b 0a 20 20 69 66  lSeq *pRet;.  if
26adc 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20  ( p->pPrior ){. 
26add 20 20 20 70 52 65 74 20 3d 20 6d 75 6c 74 69 53     pRet = multiS
26ade 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61  electCollSeq(pPa
26adf 72 73 65 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20  rse, p->pPrior, 
26ae0 69 43 6f 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  iCol);.  }else{.
26ae1 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a 20 20      pRet = 0;.  
26ae2 7d 0a 20 20 69 66 28 20 70 52 65 74 3d 3d 30 20  }.  if( pRet==0 
26ae3 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 73 71  ){.    pRet = sq
26ae4 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
26ae5 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69  (pParse, p->pELi
26ae6 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70  st->a[iCol].pExp
26ae7 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  r);.  }.  return
26ae8 20 70 52 65 74 3b 0a 7d 0a 23 65 6e 64 69 66 20   pRet;.}.#endif 
26ae9 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  /* SQLITE_OMIT_C
26aea 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a  OMPOUND_SELECT *
26aeb 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  /../* Forward re
26aec 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69  ference */.stati
26aed 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c 65 63  c int multiSelec
26aee 74 4f 72 64 65 72 42 79 28 0a 20 20 50 61 72 73  tOrderBy(.  Pars
26aef 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
26af0 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
26af1 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
26af2 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
26af3 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73  /* The right-mos
26af4 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f 20  t of SELECTs to 
26af5 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65  be coded */.  Se
26af6 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20  lectDest *pDest 
26af7 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64      /* What to d
26af8 6f 20 77 69 74 68 20 71 75 65 72 79 20 72 65 73  o with query res
26af9 75 6c 74 73 20 2a 2f 0a 29 3b 0a 0a 0a 23 69 66  ults */.);...#if
26afa 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
26afb 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
26afc 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
26afd 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ine is called to
26afe 20 70 72 6f 63 65 73 73 20 61 20 63 6f 6d 70 6f   process a compo
26aff 75 6e 64 20 71 75 65 72 79 20 66 6f 72 6d 20 66  und query form f
26b00 72 6f 6d 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f  rom.** two or mo
26b01 72 65 20 73 65 70 61 72 61 74 65 20 71 75 65 72  re separate quer
26b02 69 65 73 20 75 73 69 6e 67 20 55 4e 49 4f 4e 2c  ies using UNION,
26b03 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 45 58 43 45   UNION ALL, EXCE
26b04 50 54 2c 20 6f 72 0a 2a 2a 20 49 4e 54 45 52 53  PT, or.** INTERS
26b05 45 43 54 0a 2a 2a 0a 2a 2a 20 22 70 22 20 70 6f  ECT.**.** "p" po
26b06 69 6e 74 73 20 74 6f 20 74 68 65 20 72 69 67 68  ints to the righ
26b07 74 2d 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 77  t-most of the tw
26b08 6f 20 71 75 65 72 69 65 73 2e 20 20 74 68 65 20  o queries.  the 
26b09 71 75 65 72 79 20 6f 6e 20 74 68 65 0a 2a 2a 20  query on the.** 
26b0a 6c 65 66 74 20 69 73 20 70 2d 3e 70 50 72 69 6f  left is p->pPrio
26b0b 72 2e 20 20 54 68 65 20 6c 65 66 74 20 71 75 65  r.  The left que
26b0c 72 79 20 63 6f 75 6c 64 20 61 6c 73 6f 20 62 65  ry could also be
26b0d 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72   a compound quer
26b0e 79 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63 61  y.** in which ca
26b0f 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  se this routine 
26b10 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 72  will be called r
26b11 65 63 75 72 73 69 76 65 6c 79 2e 20 0a 2a 2a 0a  ecursively. .**.
26b12 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f  ** The results o
26b13 66 20 74 68 65 20 74 6f 74 61 6c 20 71 75 65 72  f the total quer
26b14 79 20 61 72 65 20 74 6f 20 62 65 20 77 72 69 74  y are to be writ
26b15 74 65 6e 20 69 6e 74 6f 20 61 20 64 65 73 74 69  ten into a desti
26b16 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 79 70  nation.** of typ
26b17 65 20 65 44 65 73 74 20 77 69 74 68 20 70 61 72  e eDest with par
26b18 61 6d 65 74 65 72 20 69 50 61 72 6d 2e 0a 2a 2a  ameter iParm..**
26b19 0a 2a 2a 20 45 78 61 6d 70 6c 65 20 31 3a 20 20  .** Example 1:  
26b1a 43 6f 6e 73 69 64 65 72 20 61 20 74 68 72 65 65  Consider a three
26b1b 2d 77 61 79 20 63 6f 6d 70 6f 75 6e 64 20 53 51  -way compound SQ
26b1c 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  L statement..**.
26b1d 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 20  **     SELECT a 
26b1e 46 52 4f 4d 20 74 31 20 55 4e 49 4f 4e 20 53 45  FROM t1 UNION SE
26b1f 4c 45 43 54 20 62 20 46 52 4f 4d 20 74 32 20 55  LECT b FROM t2 U
26b20 4e 49 4f 4e 20 53 45 4c 45 43 54 20 63 20 46 52  NION SELECT c FR
26b21 4f 4d 20 74 33 0a 2a 2a 0a 2a 2a 20 54 68 69 73  OM t3.**.** This
26b22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61   statement is pa
26b23 72 73 65 64 20 75 70 20 61 73 20 66 6f 6c 6c 6f  rsed up as follo
26b24 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  ws:.**.**     SE
26b25 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33 0a 2a  LECT c FROM t3.*
26b26 2a 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20  *      |.**     
26b27 20 60 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43 54   `----->  SELECT
26b28 20 62 20 46 52 4f 4d 20 74 32 0a 2a 2a 20 20 20   b FROM t2.**   
26b29 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 0a 2a               |.*
26b2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
26b2b 20 60 2d 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43   `------>  SELEC
26b2c 54 20 61 20 46 52 4f 4d 20 74 31 0a 2a 2a 0a 2a  T a FROM t1.**.*
26b2d 2a 20 54 68 65 20 61 72 72 6f 77 73 20 69 6e 20  * The arrows in 
26b2e 74 68 65 20 64 69 61 67 72 61 6d 20 61 62 6f 76  the diagram abov
26b2f 65 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20  e represent the 
26b30 53 65 6c 65 63 74 2e 70 50 72 69 6f 72 20 70 6f  Select.pPrior po
26b31 69 6e 74 65 72 2e 0a 2a 2a 20 53 6f 20 69 66 20  inter..** So if 
26b32 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
26b33 63 61 6c 6c 65 64 20 77 69 74 68 20 70 20 65 71  called with p eq
26b34 75 61 6c 20 74 6f 20 74 68 65 20 74 33 20 71 75  ual to the t3 qu
26b35 65 72 79 2c 20 74 68 65 6e 0a 2a 2a 20 70 50 72  ery, then.** pPr
26b36 69 6f 72 20 77 69 6c 6c 20 62 65 20 74 68 65 20  ior will be the 
26b37 74 32 20 71 75 65 72 79 2e 20 20 70 2d 3e 6f 70  t2 query.  p->op
26b38 20 77 69 6c 6c 20 62 65 20 54 4b 5f 55 4e 49 4f   will be TK_UNIO
26b39 4e 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  N in this case..
26b3a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65 20 74 68 61  **.** Notice tha
26b3b 74 20 62 65 63 61 75 73 65 20 6f 66 20 74 68 65  t because of the
26b3c 20 77 61 79 20 53 51 4c 69 74 65 20 70 61 72 73   way SQLite pars
26b3d 65 73 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45  es compound SELE
26b3e 43 54 73 2c 20 74 68 65 0a 2a 2a 20 69 6e 64 69  CTs, the.** indi
26b3f 76 69 64 75 61 6c 20 73 65 6c 65 63 74 73 20 61  vidual selects a
26b40 6c 77 61 79 73 20 67 72 6f 75 70 20 66 72 6f 6d  lways group from
26b41 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2e 0a   left to right..
26b42 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75  */.static int mu
26b43 6c 74 69 53 65 6c 65 63 74 28 0a 20 20 50 61 72  ltiSelect(.  Par
26b44 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
26b45 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
26b46 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
26b47 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
26b48 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f   /* The right-mo
26b49 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f  st of SELECTs to
26b4a 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53   be coded */.  S
26b4b 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74  electDest *pDest
26b4c 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20       /* What to 
26b4d 64 6f 20 77 69 74 68 20 71 75 65 72 79 20 72 65  do with query re
26b4e 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e  sults */.){.  in
26b4f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
26b50 3b 20 20 20 2f 2a 20 53 75 63 63 65 73 73 20 63  ;   /* Success c
26b51 6f 64 65 20 66 72 6f 6d 20 61 20 73 75 62 72 6f  ode from a subro
26b52 75 74 69 6e 65 20 2a 2f 0a 20 20 53 65 6c 65 63  utine */.  Selec
26b53 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20  t *pPrior;      
26b54 20 2f 2a 20 41 6e 6f 74 68 65 72 20 53 45 4c 45   /* Another SELE
26b55 43 54 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  CT immediately t
26b56 6f 20 6f 75 72 20 6c 65 66 74 20 2a 2f 0a 20 20  o our left */.  
26b57 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
26b58 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
26b59 65 20 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56  e code to this V
26b5a 44 42 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  DBE */.  SelectD
26b5b 65 73 74 20 64 65 73 74 3b 20 20 20 20 20 20 2f  est dest;      /
26b5c 2a 20 41 6c 74 65 72 6e 61 74 69 76 65 20 64 61  * Alternative da
26b5d 74 61 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a  ta destination *
26b5e 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 44 65 6c  /.  Select *pDel
26b5f 65 74 65 20 3d 20 30 3b 20 20 2f 2a 20 43 68 61  ete = 0;  /* Cha
26b60 69 6e 20 6f 66 20 73 69 6d 70 6c 65 20 73 65 6c  in of simple sel
26b61 65 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 2a  ects to delete *
26b62 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
26b63 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
26b64 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
26b65 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73   */..  /* Make s
26b66 75 72 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ure there is no 
26b67 4f 52 44 45 52 20 42 59 20 6f 72 20 4c 49 4d 49  ORDER BY or LIMI
26b68 54 20 63 6c 61 75 73 65 20 6f 6e 20 70 72 69 6f  T clause on prio
26b69 72 20 53 45 4c 45 43 54 73 2e 20 20 4f 6e 6c 79  r SELECTs.  Only
26b6a 0a 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 28  .  ** the last (
26b6b 72 69 67 68 74 2d 6d 6f 73 74 29 20 53 45 4c 45  right-most) SELE
26b6c 43 54 20 69 6e 20 74 68 65 20 73 65 72 69 65 73  CT in the series
26b6d 20 6d 61 79 20 68 61 76 65 20 61 6e 20 4f 52 44   may have an ORD
26b6e 45 52 20 42 59 20 6f 72 20 4c 49 4d 49 54 2e 0a  ER BY or LIMIT..
26b6f 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
26b70 20 26 26 20 70 2d 3e 70 50 72 69 6f 72 20 29 3b   && p->pPrior );
26b71 20 20 2f 2a 20 43 61 6c 6c 69 6e 67 20 66 75 6e    /* Calling fun
26b72 63 74 69 6f 6e 20 67 75 61 72 61 6e 74 65 65 73  ction guarantees
26b73 20 74 68 69 73 20 6d 75 63 68 20 2a 2f 0a 20 20   this much */.  
26b74 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
26b75 0a 20 20 70 50 72 69 6f 72 20 3d 20 70 2d 3e 70  .  pPrior = p->p
26b76 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28  Prior;.  assert(
26b77 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d   pPrior->pRightm
26b78 6f 73 74 21 3d 70 50 72 69 6f 72 20 29 3b 0a 20  ost!=pPrior );. 
26b79 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d   assert( pPrior-
26b7a 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 2d 3e  >pRightmost==p->
26b7b 70 52 69 67 68 74 6d 6f 73 74 20 29 3b 0a 20 20  pRightmost );.  
26b7c 64 65 73 74 20 3d 20 2a 70 44 65 73 74 3b 0a 20  dest = *pDest;. 
26b7d 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 4f 72   if( pPrior->pOr
26b7e 64 65 72 42 79 20 29 7b 0a 20 20 20 20 73 71 6c  derBy ){.    sql
26b7f 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
26b80 72 73 65 2c 22 4f 52 44 45 52 20 42 59 20 63 6c  rse,"ORDER BY cl
26b81 61 75 73 65 20 73 68 6f 75 6c 64 20 63 6f 6d 65  ause should come
26b82 20 61 66 74 65 72 20 25 73 20 6e 6f 74 20 62 65   after %s not be
26b83 66 6f 72 65 22 2c 0a 20 20 20 20 20 20 73 65 6c  fore",.      sel
26b84 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29  ectOpName(p->op)
26b85 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  );.    rc = 1;. 
26b86 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65     goto multi_se
26b87 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20  lect_end;.  }.  
26b88 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d  if( pPrior->pLim
26b89 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  it ){.    sqlite
26b8a 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
26b8b 2c 22 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 73  ,"LIMIT clause s
26b8c 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 65 72  hould come after
26b8d 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 22 2c   %s not before",
26b8e 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f 70 4e  .      selectOpN
26b8f 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20  ame(p->op));.   
26b90 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74   rc = 1;.    got
26b91 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
26b92 6e 64 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d 20 73  nd;.  }..  v = s
26b93 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
26b94 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28  arse);.  assert(
26b95 20 76 21 3d 30 20 29 3b 20 20 2f 2a 20 54 68 65   v!=0 );  /* The
26b96 20 56 44 42 45 20 61 6c 72 65 61 64 79 20 63 72   VDBE already cr
26b97 65 61 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67  eated by calling
26b98 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20   function */..  
26b99 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 64 65  /* Create the de
26b9a 73 74 69 6e 61 74 69 6f 6e 20 74 65 6d 70 6f 72  stination tempor
26b9b 61 72 79 20 74 61 62 6c 65 20 69 66 20 6e 65 63  ary table if nec
26b9c 65 73 73 61 72 79 0a 20 20 2a 2f 0a 20 20 69 66  essary.  */.  if
26b9d 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 53 52  ( dest.eDest==SR
26b9e 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a 20 20  T_EphemTab ){.  
26b9f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c    assert( p->pEL
26ba0 69 73 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ist );.    sqlit
26ba1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26ba2 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
26ba3 2c 20 64 65 73 74 2e 69 50 61 72 6d 2c 20 70 2d  , dest.iParm, p-
26ba4 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b  >pEList->nExpr);
26ba5 0a 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20  .    dest.eDest 
26ba6 3d 20 53 52 54 5f 54 61 62 6c 65 3b 0a 20 20 7d  = SRT_Table;.  }
26ba7 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
26ba8 20 61 6c 6c 20 53 45 4c 45 43 54 73 20 69 6e 20   all SELECTs in 
26ba9 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61  the statement ha
26baa 76 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62  ve the same numb
26bab 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 0a 20  er of elements. 
26bac 20 2a 2a 20 69 6e 20 74 68 65 69 72 20 72 65 73   ** in their res
26bad 75 6c 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a 20  ult sets..  */. 
26bae 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69   assert( p->pELi
26baf 73 74 20 26 26 20 70 50 72 69 6f 72 2d 3e 70 45  st && pPrior->pE
26bb0 4c 69 73 74 20 29 3b 0a 20 20 69 66 28 20 70 2d  List );.  if( p-
26bb1 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d  >pEList->nExpr!=
26bb2 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 2d 3e  pPrior->pEList->
26bb3 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 71 6c  nExpr ){.    sql
26bb4 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
26bb5 72 73 65 2c 20 22 53 45 4c 45 43 54 73 20 74 6f  rse, "SELECTs to
26bb6 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69   the left and ri
26bb7 67 68 74 20 6f 66 20 25 73 22 0a 20 20 20 20 20  ght of %s".     
26bb8 20 22 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74   " do not have t
26bb9 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f  he same number o
26bba 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
26bbb 22 2c 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28  ", selectOpName(
26bbc 70 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20  p->op));.    rc 
26bbd 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75  = 1;.    goto mu
26bbe 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
26bbf 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 6f 75    }..  /* Compou
26bc0 6e 64 20 53 45 4c 45 43 54 73 20 74 68 61 74 20  nd SELECTs that 
26bc1 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59  have an ORDER BY
26bc2 20 63 6c 61 75 73 65 20 61 72 65 20 68 61 6e 64   clause are hand
26bc3 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 0a  led separately..
26bc4 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4f    */.  if( p->pO
26bc5 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65  rderBy ){.    re
26bc6 74 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74  turn multiSelect
26bc7 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c 20  OrderBy(pParse, 
26bc8 70 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a  p, pDest);.  }..
26bc9 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
26bca 64 65 20 66 6f 72 20 74 68 65 20 6c 65 66 74 20  de for the left 
26bcb 61 6e 64 20 72 69 67 68 74 20 53 45 4c 45 43 54  and right SELECT
26bcc 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a   statements..  *
26bcd 2f 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f  /.  switch( p->o
26bce 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b  p ){.    case TK
26bcf 5f 41 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e  _ALL: {.      in
26bd0 74 20 61 64 64 72 20 3d 20 30 3b 0a 20 20 20 20  t addr = 0;.    
26bd1 20 20 61 73 73 65 72 74 28 20 21 70 50 72 69 6f    assert( !pPrio
26bd2 72 2d 3e 70 4c 69 6d 69 74 20 29 3b 0a 20 20 20  r->pLimit );.   
26bd3 20 20 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69     pPrior->pLimi
26bd4 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20  t = p->pLimit;. 
26bd5 20 20 20 20 20 70 50 72 69 6f 72 2d 3e 70 4f 66       pPrior->pOf
26bd6 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65  fset = p->pOffse
26bd7 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  t;.      rc = sq
26bd8 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
26bd9 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 64 65 73  se, pPrior, &des
26bda 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69  t);.      p->pLi
26bdb 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  mit = 0;.      p
26bdc 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ->pOffset = 0;. 
26bdd 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
26bde 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74         goto mult
26bdf 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
26be0 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70      }.      p->p
26be1 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20  Prior = 0;.     
26be2 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 70 50 72   p->iLimit = pPr
26be3 69 6f 72 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20  ior->iLimit;.   
26be4 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20     p->iOffset = 
26be5 70 50 72 69 6f 72 2d 3e 69 4f 66 66 73 65 74 3b  pPrior->iOffset;
26be6 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 4c  .      if( p->iL
26be7 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  imit ){.        
26be8 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
26be9 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
26bea 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74  fZero, p->iLimit
26beb 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43  );.        VdbeC
26bec 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d 70  omment((v, "Jump
26bed 20 61 68 65 61 64 20 69 66 20 4c 49 4d 49 54 20   ahead if LIMIT 
26bee 72 65 61 63 68 65 64 22 29 29 3b 0a 20 20 20 20  reached"));.    
26bef 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73    }.      rc = s
26bf0 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
26bf1 72 73 65 2c 20 70 2c 20 26 64 65 73 74 29 3b 0a  rse, p, &dest);.
26bf2 20 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20        pDelete = 
26bf3 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20  p->pPrior;.     
26bf4 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72   p->pPrior = pPr
26bf5 69 6f 72 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ior;.      if( r
26bf6 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  c ){.        got
26bf7 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
26bf8 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  nd;.      }.    
26bf9 20 20 69 66 28 20 61 64 64 72 20 29 7b 0a 20 20    if( addr ){.  
26bfa 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26bfb 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
26bfc 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
26bfd 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
26bfe 20 20 20 63 61 73 65 20 54 4b 5f 45 58 43 45 50     case TK_EXCEP
26bff 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55  T:.    case TK_U
26c00 4e 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e  NION: {.      in
26c01 74 20 75 6e 69 6f 6e 54 61 62 3b 20 20 20 20 2f  t unionTab;    /
26c02 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
26c03 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  of the temporary
26c04 20 74 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 72   table holding r
26c05 65 73 75 6c 74 20 2a 2f 0a 20 20 20 20 20 20 75  esult */.      u
26c06 38 20 6f 70 20 3d 20 30 3b 20 20 20 20 20 20 20  8 op = 0;       
26c07 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 52  /* One of the SR
26c08 54 5f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f  T_ operations to
26c09 20 61 70 70 6c 79 20 74 6f 20 73 65 6c 66 20 2a   apply to self *
26c0a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 72 69 6f  /.      int prio
26c0b 72 4f 70 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  rOp;     /* The 
26c0c 53 52 54 5f 20 6f 70 65 72 61 74 69 6f 6e 20 74  SRT_ operation t
26c0d 6f 20 61 70 70 6c 79 20 74 6f 20 70 72 69 6f 72  o apply to prior
26c0e 20 73 65 6c 65 63 74 73 20 2a 2f 0a 20 20 20 20   selects */.    
26c0f 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20    Expr *pLimit, 
26c10 2a 70 4f 66 66 73 65 74 3b 20 2f 2a 20 53 61 76  *pOffset; /* Sav
26c11 65 64 20 76 61 6c 75 65 73 20 6f 66 20 70 2d 3e  ed values of p->
26c12 6e 4c 69 6d 69 74 20 61 6e 64 20 70 2d 3e 6e 4f  nLimit and p->nO
26c13 66 66 73 65 74 20 2a 2f 0a 20 20 20 20 20 20 69  ffset */.      i
26c14 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 53  nt addr;.      S
26c15 65 6c 65 63 74 44 65 73 74 20 75 6e 69 6f 6e 64  electDest uniond
26c16 65 73 74 3b 0a 0a 20 20 20 20 20 20 70 72 69 6f  est;..      prio
26c17 72 4f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b  rOp = SRT_Union;
26c18 0a 20 20 20 20 20 20 69 66 28 20 64 65 73 74 2e  .      if( dest.
26c19 65 44 65 73 74 3d 3d 70 72 69 6f 72 4f 70 20 26  eDest==priorOp &
26c1a 26 20 41 4c 57 41 59 53 28 21 70 2d 3e 70 4c 69  & ALWAYS(!p->pLi
26c1b 6d 69 74 20 26 26 21 70 2d 3e 70 4f 66 66 73 65  mit &&!p->pOffse
26c1c 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  t) ){.        /*
26c1d 20 57 65 20 63 61 6e 20 72 65 75 73 65 20 61 20   We can reuse a 
26c1e 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
26c1f 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 53  generated by a S
26c20 45 4c 45 43 54 20 74 6f 20 6f 75 72 0a 20 20 20  ELECT to our.   
26c21 20 20 20 20 20 2a 2a 20 72 69 67 68 74 2e 0a 20       ** right.. 
26c22 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
26c23 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69    assert( p->pRi
26c24 67 68 74 6d 6f 73 74 21 3d 70 20 29 3b 20 20 2f  ghtmost!=p );  /
26c25 2a 20 43 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65  * Can only happe
26c26 6e 20 66 6f 72 20 6c 65 66 74 77 61 72 64 20 65  n for leftward e
26c27 6c 65 6d 65 6e 74 73 0a 20 20 20 20 20 20 20 20  lements.        
26c28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c29 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
26c2a 6f 66 20 61 20 33 2d 77 61 79 20 6f 72 20 6d 6f  of a 3-way or mo
26c2b 72 65 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20  re compound */. 
26c2c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
26c2d 2d 3e 70 4c 69 6d 69 74 3d 3d 30 20 29 3b 20 20  ->pLimit==0 );  
26c2e 20 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77      /* Not allow
26c2f 65 64 20 6f 6e 20 6c 65 66 74 77 61 72 64 20 65  ed on leftward e
26c30 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  lements */.     
26c31 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f     assert( p->pO
26c32 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 20 20  ffset==0 );     
26c33 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f  /* Not allowed o
26c34 6e 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65  n leftward eleme
26c35 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 75  nts */.        u
26c36 6e 69 6f 6e 54 61 62 20 3d 20 64 65 73 74 2e 69  nionTab = dest.i
26c37 50 61 72 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Parm;.      }els
26c38 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65  e{.        /* We
26c39 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 72   will need to cr
26c3a 65 61 74 65 20 6f 75 72 20 6f 77 6e 20 74 65 6d  eate our own tem
26c3b 70 6f 72 61 72 79 20 74 61 62 6c 65 20 74 6f 20  porary table to 
26c3c 68 6f 6c 64 20 74 68 65 0a 20 20 20 20 20 20 20  hold the.       
26c3d 20 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 74 65   ** intermediate
26c3e 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20 20 20   results..      
26c3f 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 69    */.        uni
26c40 6f 6e 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e  onTab = pParse->
26c41 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nTab++;.        
26c42 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64 65  assert( p->pOrde
26c43 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  rBy==0 );.      
26c44 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
26c45 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26c46 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
26c47 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20  unionTab, 0);.  
26c48 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
26c49 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d  >addrOpenEphm[0]
26c4a 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20   == -1 );.      
26c4b 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
26c4c 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20  m[0] = addr;.   
26c4d 20 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f       p->pRightmo
26c4e 73 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20  st->selFlags |= 
26c4f 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c  SF_UsesEphemeral
26c50 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
26c51 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20  ( p->pEList );. 
26c52 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
26c53 20 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54   Code the SELECT
26c54 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 6f   statements to o
26c55 75 72 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2f  ur left.      */
26c56 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
26c57 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79  pPrior->pOrderBy
26c58 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
26c59 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
26c5a 26 75 6e 69 6f 6e 64 65 73 74 2c 20 70 72 69 6f  &uniondest, prio
26c5b 72 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 29 3b 0a  rOp, unionTab);.
26c5c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
26c5d 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
26c5e 20 70 50 72 69 6f 72 2c 20 26 75 6e 69 6f 6e 64   pPrior, &uniond
26c5f 65 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  est);.      if( 
26c60 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  rc ){.        go
26c61 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
26c62 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  end;.      }..  
26c63 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20      /* Code the 
26c64 63 75 72 72 65 6e 74 20 53 45 4c 45 43 54 20 73  current SELECT s
26c65 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a  tatement.      *
26c66 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f  /.      if( p->o
26c67 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29 7b 0a  p==TK_EXCEPT ){.
26c68 20 20 20 20 20 20 20 20 6f 70 20 3d 20 53 52 54          op = SRT
26c69 5f 45 78 63 65 70 74 3b 0a 20 20 20 20 20 20 7d  _Except;.      }
26c6a 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
26c6b 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  sert( p->op==TK_
26c6c 55 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 20  UNION );.       
26c6d 20 6f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b   op = SRT_Union;
26c6e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
26c6f 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20  ->pPrior = 0;.  
26c70 20 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e      pLimit = p->
26c71 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d  pLimit;.      p-
26c72 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20  >pLimit = 0;.   
26c73 20 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e     pOffset = p->
26c74 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70  pOffset;.      p
26c75 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ->pOffset = 0;. 
26c76 20 20 20 20 20 75 6e 69 6f 6e 64 65 73 74 2e 65       uniondest.e
26c77 44 65 73 74 20 3d 20 6f 70 3b 0a 20 20 20 20 20  Dest = op;.     
26c78 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
26c79 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26  ect(pParse, p, &
26c7a 75 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20  uniondest);.    
26c7b 20 20 2f 2a 20 51 75 65 72 79 20 66 6c 61 74 74    /* Query flatt
26c7c 65 6e 69 6e 67 20 69 6e 20 73 71 6c 69 74 65 33  ening in sqlite3
26c7d 53 65 6c 65 63 74 28 29 20 6d 69 67 68 74 20 72  Select() might r
26c7e 65 66 69 6c 6c 20 70 2d 3e 70 4f 72 64 65 72 42  efill p->pOrderB
26c7f 79 2e 0a 20 20 20 20 20 20 2a 2a 20 42 65 20 73  y..      ** Be s
26c80 75 72 65 20 74 6f 20 64 65 6c 65 74 65 20 70 2d  ure to delete p-
26c81 3e 70 4f 72 64 65 72 42 79 2c 20 74 68 65 72 65  >pOrderBy, there
26c82 66 6f 72 65 2c 20 74 6f 20 61 76 6f 69 64 20 61  fore, to avoid a
26c83 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e 20 2a 2f   memory leak. */
26c84 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
26c85 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
26c86 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20   p->pOrderBy);. 
26c87 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70       pDelete = p
26c88 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
26c89 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  p->pPrior = pPri
26c8a 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 72  or;.      p->pOr
26c8b 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20  derBy = 0;.     
26c8c 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
26c8d 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74  te(db, p->pLimit
26c8e 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  );.      p->pLim
26c8f 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20  it = pLimit;.   
26c90 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20     p->pOffset = 
26c91 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70  pOffset;.      p
26c92 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->iLimit = 0;.  
26c93 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d      p->iOffset =
26c94 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63   0;.      if( rc
26c95 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
26c96 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e   multi_select_en
26c97 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 0a 20 20 20  d;.      }...   
26c98 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68     /* Convert th
26c99 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 74 65  e data in the te
26c9a 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69 6e  mporary table in
26c9b 74 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72 6d  to whatever form
26c9c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69 73 20  .      ** it is 
26c9d 74 68 61 74 20 77 65 20 63 75 72 72 65 6e 74 6c  that we currentl
26c9e 79 20 6e 65 65 64 2e 0a 20 20 20 20 20 20 2a 2f  y need..      */
26c9f 20 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28        .      if(
26ca0 20 64 65 73 74 2e 65 44 65 73 74 21 3d 70 72 69   dest.eDest!=pri
26ca1 6f 72 4f 70 20 7c 7c 20 75 6e 69 6f 6e 54 61 62  orOp || unionTab
26ca2 21 3d 64 65 73 74 2e 69 50 61 72 6d 20 29 7b 0a  !=dest.iParm ){.
26ca3 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6e          int iCon
26ca4 74 2c 20 69 42 72 65 61 6b 2c 20 69 53 74 61 72  t, iBreak, iStar
26ca5 74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  t;.        asser
26ca6 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a  t( p->pEList );.
26ca7 20 20 20 20 20 20 20 20 69 66 28 20 64 65 73 74          if( dest
26ca8 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70  .eDest==SRT_Outp
26ca9 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ut ){.          
26caa 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d  Select *pFirst =
26cab 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68   p;.          wh
26cac 69 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72  ile( pFirst->pPr
26cad 69 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70  ior ) pFirst = p
26cae 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20  First->pPrior;. 
26caf 20 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74           generat
26cb0 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  eColumnNames(pPa
26cb1 72 73 65 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e  rse, 0, pFirst->
26cb2 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  pEList);.       
26cb3 20 7d 0a 20 20 20 20 20 20 20 20 69 42 72 65 61   }.        iBrea
26cb4 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  k = sqlite3VdbeM
26cb5 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
26cb6 20 20 20 20 20 69 43 6f 6e 74 20 3d 20 73 71 6c       iCont = sql
26cb7 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
26cb8 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 63 6f  l(v);.        co
26cb9 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74  mputeLimitRegist
26cba 65 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69  ers(pParse, p, i
26cbb 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20  Break);.        
26cbc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26cbd 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
26cbe 75 6e 69 6f 6e 54 61 62 2c 20 69 42 72 65 61 6b  unionTab, iBreak
26cbf 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61 72  );.        iStar
26cc0 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  t = sqlite3VdbeC
26cc1 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
26cc2 20 20 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e         selectInn
26cc3 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70  erLoop(pParse, p
26cc4 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 75 6e 69  , p->pEList, uni
26cc5 6f 6e 54 61 62 2c 20 70 2d 3e 70 45 4c 69 73 74  onTab, p->pEList
26cc6 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20  ->nExpr,.       
26cc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26cc8 20 30 2c 20 2d 31 2c 20 26 64 65 73 74 2c 20 69   0, -1, &dest, i
26cc9 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20  Cont, iBreak);. 
26cca 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26ccb 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
26ccc 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20  , iCont);.      
26ccd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26cce 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
26ccf 75 6e 69 6f 6e 54 61 62 2c 20 69 53 74 61 72 74  unionTab, iStart
26cd0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
26cd1 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
26cd2 65 6c 28 76 2c 20 69 42 72 65 61 6b 29 3b 0a 20  el(v, iBreak);. 
26cd3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26cd4 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
26cd5 6c 6f 73 65 2c 20 75 6e 69 6f 6e 54 61 62 2c 20  lose, unionTab, 
26cd6 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
26cd7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
26cd8 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 52     case TK_INTER
26cd9 53 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e  SECT: {.      in
26cda 74 20 74 61 62 31 2c 20 74 61 62 32 3b 0a 20 20  t tab1, tab2;.  
26cdb 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69      int iCont, i
26cdc 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20  Break, iStart;. 
26cdd 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69       Expr *pLimi
26cde 74 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20 20 20  t, *pOffset;.   
26cdf 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
26ce0 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 69 6e     SelectDest in
26ce1 74 65 72 73 65 63 74 64 65 73 74 3b 0a 20 20 20  tersectdest;.   
26ce2 20 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 20 20     int r1;..    
26ce3 20 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20 69    /* INTERSECT i
26ce4 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
26ce5 20 74 68 65 20 6f 74 68 65 72 73 20 73 69 6e 63   the others sinc
26ce6 65 20 69 74 20 72 65 71 75 69 72 65 73 0a 20 20  e it requires.  
26ce7 20 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d 70 6f      ** two tempo
26ce8 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 48 65  rary tables.  He
26ce9 6e 63 65 20 69 74 20 68 61 73 20 69 74 73 20 6f  nce it has its o
26cea 77 6e 20 63 61 73 65 2e 20 20 42 65 67 69 6e 0a  wn case.  Begin.
26ceb 20 20 20 20 20 20 2a 2a 20 62 79 20 61 6c 6c 6f        ** by allo
26cec 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65  cating the table
26ced 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 2e 0a  s we will need..
26cee 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 74        */.      t
26cef 61 62 31 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  ab1 = pParse->nT
26cf0 61 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61 62 32  ab++;.      tab2
26cf1 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
26cf2 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  +;.      assert(
26cf3 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20   p->pOrderBy==0 
26cf4 29 3b 0a 0a 20 20 20 20 20 20 61 64 64 72 20 3d  );..      addr =
26cf5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26cf6 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p2(v, OP_OpenEph
26cf7 65 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20 30 29  emeral, tab1, 0)
26cf8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
26cf9 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
26cfa 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20  0] == -1 );.    
26cfb 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
26cfc 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20  m[0] = addr;.   
26cfd 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74     p->pRightmost
26cfe 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
26cff 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a  _UsesEphemeral;.
26d00 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
26d01 3e 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20 20  >pEList );..    
26d02 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45    /* Code the SE
26d03 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65 66  LECTs to our lef
26d04 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79  t into temporary
26d05 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a 20   table "tab1".. 
26d06 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
26d07 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
26d08 6e 69 74 28 26 69 6e 74 65 72 73 65 63 74 64 65  nit(&intersectde
26d09 73 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74  st, SRT_Union, t
26d0a 61 62 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ab1);.      rc =
26d0b 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
26d0c 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26  Parse, pPrior, &
26d0d 69 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b 0a  intersectdest);.
26d0e 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
26d0f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c          goto mul
26d10 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
26d11 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
26d12 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65 6e   Code the curren
26d13 74 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74 65  t SELECT into te
26d14 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22 74  mporary table "t
26d15 61 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ab2".      */.  
26d16 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
26d17 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26d18 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
26d19 2c 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20 20  , tab2, 0);.    
26d1a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64    assert( p->add
26d1b 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20  rOpenEphm[1] == 
26d1c 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  -1 );.      p->a
26d1d 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d  ddrOpenEphm[1] =
26d1e 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e   addr;.      p->
26d1f 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pPrior = 0;.    
26d20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c    pLimit = p->pL
26d21 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70  imit;.      p->p
26d22 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  Limit = 0;.     
26d23 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f   pOffset = p->pO
26d24 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e  ffset;.      p->
26d25 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20  pOffset = 0;.   
26d26 20 20 20 69 6e 74 65 72 73 65 63 74 64 65 73 74     intersectdest
26d27 2e 69 50 61 72 6d 20 3d 20 74 61 62 32 3b 0a 20  .iParm = tab2;. 
26d28 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
26d29 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
26d2a 70 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65 73  p, &intersectdes
26d2b 74 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74  t);.      pDelet
26d2c 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20  e = p->pPrior;. 
26d2d 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d       p->pPrior =
26d2e 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 73   pPrior;.      s
26d2f 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
26d30 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b  (db, p->pLimit);
26d31 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74  .      p->pLimit
26d32 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20   = pLimit;.     
26d33 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f   p->pOffset = pO
26d34 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28  ffset;.      if(
26d35 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67   rc ){.        g
26d36 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74  oto multi_select
26d37 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  _end;.      }.. 
26d38 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
26d39 20 63 6f 64 65 20 74 6f 20 74 61 6b 65 20 74 68   code to take th
26d3a 65 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6f  e intersection o
26d3b 66 20 74 68 65 20 74 77 6f 20 74 65 6d 70 6f 72  f the two tempor
26d3c 61 72 79 0a 20 20 20 20 20 20 2a 2a 20 74 61 62  ary.      ** tab
26d3d 6c 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  les..      */.  
26d3e 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
26d3f 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20 69  EList );.      i
26d40 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 53  f( dest.eDest==S
26d41 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20  RT_Output ){.   
26d42 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 46 69       Select *pFi
26d43 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20  rst = p;.       
26d44 20 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d 3e   while( pFirst->
26d45 70 50 72 69 6f 72 20 29 20 70 46 69 72 73 74 20  pPrior ) pFirst 
26d46 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  = pFirst->pPrior
26d47 3b 0a 20 20 20 20 20 20 20 20 67 65 6e 65 72 61  ;.        genera
26d48 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  teColumnNames(pP
26d49 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73 74 2d  arse, 0, pFirst-
26d4a 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20  >pEList);.      
26d4b 7d 0a 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d  }.      iBreak =
26d4c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
26d4d 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
26d4e 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  iCont = sqlite3V
26d4f 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
26d50 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c 69  .      computeLi
26d51 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61  mitRegisters(pPa
26d52 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29 3b  rse, p, iBreak);
26d53 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26d54 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
26d55 65 77 69 6e 64 2c 20 74 61 62 31 2c 20 69 42 72  ewind, tab1, iBr
26d56 65 61 6b 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  eak);.      r1 =
26d57 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
26d58 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
26d59 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74    iStart = sqlit
26d5a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26d5b 4f 50 5f 52 6f 77 4b 65 79 2c 20 74 61 62 31 2c  OP_RowKey, tab1,
26d5c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
26d5d 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
26d5e 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74 61   OP_NotFound, ta
26d5f 62 32 2c 20 69 43 6f 6e 74 2c 20 72 31 29 3b 0a  b2, iCont, r1);.
26d60 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
26d61 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
26d62 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73  se, r1);.      s
26d63 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70  electInnerLoop(p
26d64 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c  Parse, p, p->pEL
26d65 69 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e 70 45  ist, tab1, p->pE
26d66 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20 20 20  List->nExpr,.   
26d67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d68 20 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74 2c     0, -1, &dest,
26d69 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b   iCont, iBreak);
26d6a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26d6b 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
26d6c 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20  , iCont);.      
26d6d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26d6e 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74 61  2(v, OP_Next, ta
26d6f 62 31 2c 20 69 53 74 61 72 74 29 3b 0a 20 20 20  b1, iStart);.   
26d70 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
26d71 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42  solveLabel(v, iB
26d72 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c  reak);.      sql
26d73 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26d74 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 32  , OP_Close, tab2
26d75 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
26d76 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26d77 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 31 2c   OP_Close, tab1,
26d78 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   0);.      break
26d79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
26d7a 2a 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c 61 74  * Compute collat
26d7b 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 75 73  ing sequences us
26d7c 65 64 20 62 79 20 0a 20 20 2a 2a 20 74 65 6d 70  ed by .  ** temp
26d7d 6f 72 61 72 79 20 74 61 62 6c 65 73 20 6e 65 65  orary tables nee
26d7e 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ded to implement
26d7f 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65   the compound se
26d80 6c 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74 61 63  lect..  ** Attac
26d81 68 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  h the KeyInfo st
26d82 72 75 63 74 75 72 65 20 74 6f 20 61 6c 6c 20 74  ructure to all t
26d83 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
26d84 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
26d85 73 65 63 74 69 6f 6e 20 69 73 20 72 75 6e 20 62  section is run b
26d86 79 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  y the right-most
26d87 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
26d88 74 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 45 4c  t only..  ** SEL
26d89 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74  ECT statements t
26d8a 6f 20 74 68 65 20 6c 65 66 74 20 61 6c 77 61 79  o the left alway
26d8b 73 20 73 6b 69 70 20 74 68 69 73 20 70 61 72 74  s skip this part
26d8c 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73  .  The right-mos
26d8d 74 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 6d 69  t.  ** SELECT mi
26d8e 67 68 74 20 61 6c 73 6f 20 73 6b 69 70 20 74 68  ght also skip th
26d8f 69 73 20 70 61 72 74 20 69 66 20 69 74 20 68 61  is part if it ha
26d90 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 63 6c  s no ORDER BY cl
26d91 61 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20 6e 6f  ause and.  ** no
26d92 20 74 65 6d 70 20 74 61 62 6c 65 73 20 61 72 65   temp tables are
26d93 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2f 0a   required..  */.
26d94 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67    if( p->selFlag
26d95 73 20 26 20 53 46 5f 55 73 65 73 45 70 68 65 6d  s & SF_UsesEphem
26d96 65 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20  eral ){.    int 
26d97 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
26d98 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
26d99 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20  p counter */.   
26d9a 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
26d9b 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  fo;            /
26d9c 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
26d9d 65 6e 63 65 20 66 6f 72 20 74 68 65 20 72 65 73  ence for the res
26d9e 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 53  ult set */.    S
26d9f 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20 20 20  elect *pLoop;   
26da0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26da1 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  For looping thro
26da2 75 67 68 20 53 45 4c 45 43 54 20 73 74 61 74 65  ugh SELECT state
26da3 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43 6f 6c  ments */.    Col
26da4 6c 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b 20 20  lSeq **apColl;  
26da5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
26da6 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67  r looping throug
26da7 68 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c  h pKeyInfo->aCol
26da8 6c 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  l[] */.    int n
26da9 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
26daa 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
26dab 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
26dac 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a   result set */..
26dad 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
26dae 52 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29 3b 0a  Rightmost==p );.
26daf 20 20 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e 70 45      nCol = p->pE
26db0 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  List->nExpr;.   
26db1 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69   pKeyInfo = sqli
26db2 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
26db3 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  db,.            
26db4 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
26db5 66 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e 43 6f  f(*pKeyInfo)+nCo
26db6 6c 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65  l*(sizeof(CollSe
26db7 71 2a 29 20 2b 20 31 29 29 3b 0a 20 20 20 20 69  q*) + 1));.    i
26db8 66 28 20 21 70 4b 65 79 49 6e 66 6f 20 29 7b 0a  f( !pKeyInfo ){.
26db9 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
26dba 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67  E_NOMEM;.      g
26dbb 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74  oto multi_select
26dbc 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _end;.    }..   
26dbd 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d   pKeyInfo->enc =
26dbe 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70 4b   ENC(db);.    pK
26dbf 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d  eyInfo->nField =
26dc0 20 28 75 31 36 29 6e 43 6f 6c 3b 0a 0a 20 20 20   (u16)nCol;..   
26dc1 20 66 6f 72 28 69 3d 30 2c 20 61 70 43 6f 6c 6c   for(i=0, apColl
26dc2 3d 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c  =pKeyInfo->aColl
26dc3 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 61  ; i<nCol; i++, a
26dc4 70 43 6f 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20  pColl++){.      
26dc5 2a 61 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53  *apColl = multiS
26dc6 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61  electCollSeq(pPa
26dc7 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20 20 20  rse, p, i);.    
26dc8 20 20 69 66 28 20 30 3d 3d 2a 61 70 43 6f 6c 6c    if( 0==*apColl
26dc9 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 61 70 43   ){.        *apC
26dca 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43  oll = db->pDfltC
26dcb 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oll;.      }.   
26dcc 20 7d 0a 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f   }..    for(pLoo
26dcd 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f  p=p; pLoop; pLoo
26dce 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 29  p=pLoop->pPrior)
26dcf 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  {.      for(i=0;
26dd0 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<2; i++){.    
26dd1 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20 70      int addr = p
26dd2 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70  Loop->addrOpenEp
26dd3 68 6d 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 69  hm[i];.        i
26dd4 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 20  f( addr<0 ){.   
26dd5 20 20 20 20 20 20 20 2f 2a 20 49 66 20 5b 30 5d         /* If [0]
26dd6 20 69 73 20 75 6e 75 73 65 64 20 74 68 65 6e 20   is unused then 
26dd7 5b 31 5d 20 69 73 20 61 6c 73 6f 20 75 6e 75 73  [1] is also unus
26dd8 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e 0a 20  ed.  So we can. 
26dd9 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 77 61           ** alwa
26dda 79 73 20 73 61 66 65 6c 79 20 61 62 6f 72 74 20  ys safely abort 
26ddb 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 66  as soon as the f
26ddc 69 72 73 74 20 75 6e 75 73 65 64 20 73 6c 6f 74  irst unused slot
26ddd 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 20   is found */.   
26dde 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
26ddf 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70  Loop->addrOpenEp
26de0 68 6d 5b 31 5d 3c 30 20 29 3b 0a 20 20 20 20 20  hm[1]<0 );.     
26de1 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26de2 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
26de3 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
26de4 32 28 76 2c 20 61 64 64 72 2c 20 6e 43 6f 6c 29  2(v, addr, nCol)
26de5 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
26de6 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
26de7 20 61 64 64 72 2c 20 28 63 68 61 72 2a 29 70 4b   addr, (char*)pK
26de8 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e  eyInfo, P4_KEYIN
26de9 46 4f 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f  FO);.        pLo
26dea 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d  op->addrOpenEphm
26deb 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  [i] = -1;.      
26dec 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
26ded 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4b  te3DbFree(db, pK
26dee 65 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a 6d 75  eyInfo);.  }..mu
26def 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3a 0a  lti_select_end:.
26df0 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20    pDest->iMem = 
26df1 64 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 70 44 65  dest.iMem;.  pDe
26df2 73 74 2d 3e 6e 4d 65 6d 20 3d 20 64 65 73 74 2e  st->nMem = dest.
26df3 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53  nMem;.  sqlite3S
26df4 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
26df5 70 44 65 6c 65 74 65 29 3b 0a 20 20 72 65 74 75  pDelete);.  retu
26df6 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20  rn rc;.}.#endif 
26df7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  /* SQLITE_OMIT_C
26df8 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a  OMPOUND_SELECT *
26df9 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e  /../*.** Code an
26dfa 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69   output subrouti
26dfb 6e 65 20 66 6f 72 20 61 20 63 6f 72 6f 75 74 69  ne for a corouti
26dfc 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ne implementatio
26dfd 6e 20 6f 66 20 61 0a 2a 2a 20 53 45 4c 45 43 54  n of a.** SELECT
26dfe 20 73 74 61 74 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   statment..**.**
26dff 20 54 68 65 20 64 61 74 61 20 74 6f 20 62 65 20   The data to be 
26e00 6f 75 74 70 75 74 20 69 73 20 63 6f 6e 74 61 69  output is contai
26e01 6e 65 64 20 69 6e 20 70 49 6e 2d 3e 69 4d 65 6d  ned in pIn->iMem
26e02 2e 20 20 54 68 65 72 65 20 61 72 65 0a 2a 2a 20  .  There are.** 
26e03 70 49 6e 2d 3e 6e 4d 65 6d 20 63 6f 6c 75 6d 6e  pIn->nMem column
26e04 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 20  s to be output. 
26e05 20 70 44 65 73 74 20 69 73 20 77 68 65 72 65 20   pDest is where 
26e06 74 68 65 20 6f 75 74 70 75 74 20 73 68 6f 75 6c  the output shoul
26e07 64 0a 2a 2a 20 62 65 20 73 65 6e 74 2e 0a 2a 2a  d.** be sent..**
26e08 0a 2a 2a 20 72 65 67 52 65 74 75 72 6e 20 69 73  .** regReturn is
26e09 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
26e0a 68 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64  he register hold
26e0b 69 6e 67 20 74 68 65 20 73 75 62 72 6f 75 74 69  ing the subrouti
26e0c 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 61 64 64  ne.** return add
26e0d 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72  ress..**.** If r
26e0e 65 67 50 72 65 76 3e 30 20 74 68 65 6e 20 69 74  egPrev>0 then it
26e0f 20 69 73 20 61 20 74 68 65 20 66 69 72 73 74 20   is a the first 
26e10 72 65 67 69 73 74 65 72 20 69 6e 20 61 20 76 65  register in a ve
26e11 63 74 6f 72 20 74 68 61 74 0a 2a 2a 20 72 65 63  ctor that.** rec
26e12 6f 72 64 73 20 74 68 65 20 70 72 65 76 69 6f 75  ords the previou
26e13 73 20 6f 75 74 70 75 74 2e 20 20 6d 65 6d 5b 72  s output.  mem[r
26e14 65 67 50 72 65 76 5d 20 69 73 20 61 20 66 6c 61  egPrev] is a fla
26e15 67 20 74 68 61 74 20 69 73 20 66 61 6c 73 65 0a  g that is false.
26e16 2a 2a 20 69 66 20 74 68 65 72 65 20 68 61 73 20  ** if there has 
26e17 62 65 65 6e 20 6e 6f 20 70 72 65 76 69 6f 75 73  been no previous
26e18 20 6f 75 74 70 75 74 2e 20 20 49 66 20 72 65 67   output.  If reg
26e19 50 72 65 76 3e 30 20 74 68 65 6e 20 63 6f 64 65  Prev>0 then code
26e1a 20 69 73 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64   is.** generated
26e1b 20 74 6f 20 73 75 70 70 72 65 73 73 20 64 75 70   to suppress dup
26e1c 6c 69 63 61 74 65 73 2e 20 20 70 4b 65 79 49 6e  licates.  pKeyIn
26e1d 66 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20 63  fo is used for c
26e1e 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 6b 65 79 73  omparing.** keys
26e1f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4c  ..**.** If the L
26e20 49 4d 49 54 20 66 6f 75 6e 64 20 69 6e 20 70 2d  IMIT found in p-
26e21 3e 69 4c 69 6d 69 74 20 69 73 20 72 65 61 63 68  >iLimit is reach
26e22 65 64 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61  ed, jump immedia
26e23 74 65 6c 79 20 74 6f 0a 2a 2a 20 69 42 72 65 61  tely to.** iBrea
26e24 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  k..*/.static int
26e25 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74 53   generateOutputS
26e26 75 62 72 6f 75 74 69 6e 65 28 0a 20 20 50 61 72  ubroutine(.  Par
26e27 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
26e28 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
26e29 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
26e2a 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ect *p,         
26e2b 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
26e2c 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  CT statement */.
26e2d 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 49    SelectDest *pI
26e2e 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 72  n,        /* Cor
26e2f 6f 75 74 69 6e 65 20 73 75 70 70 6c 79 69 6e 67  outine supplying
26e30 20 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63   data */.  Selec
26e31 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20  tDest *pDest,   
26e32 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 73     /* Where to s
26e33 65 6e 64 20 74 68 65 20 64 61 74 61 20 2a 2f 0a  end the data */.
26e34 20 20 69 6e 74 20 72 65 67 52 65 74 75 72 6e 2c    int regReturn,
26e35 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
26e36 20 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 20   return address 
26e37 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e  register */.  in
26e38 74 20 72 65 67 50 72 65 76 2c 20 20 20 20 20 20  t regPrev,      
26e39 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75        /* Previou
26e3a 73 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65  s result registe
26e3b 72 2e 20 20 4e 6f 20 75 6e 69 71 75 65 6e 65 73  r.  No uniquenes
26e3c 73 20 69 66 20 30 20 2a 2f 0a 20 20 4b 65 79 49  s if 0 */.  KeyI
26e3d 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20  nfo *pKeyInfo,  
26e3e 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61      /* For compa
26e3f 72 69 6e 67 20 77 69 74 68 20 70 72 65 76 69 6f  ring with previo
26e40 75 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e  us entry */.  in
26e41 74 20 70 34 74 79 70 65 2c 20 20 20 20 20 20 20  t p4type,       
26e42 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 34 20        /* The p4 
26e43 74 79 70 65 20 66 6f 72 20 70 4b 65 79 49 6e 66  type for pKeyInf
26e44 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61  o */.  int iBrea
26e45 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k              /
26e46 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 77  * Jump here if w
26e47 65 20 68 69 74 20 74 68 65 20 4c 49 4d 49 54 20  e hit the LIMIT 
26e48 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  */.){.  Vdbe *v 
26e49 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
26e4a 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65  .  int iContinue
26e4b 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 0a 20  ;.  int addr;.. 
26e4c 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
26e4d 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
26e4e 29 3b 0a 20 20 69 43 6f 6e 74 69 6e 75 65 20 3d  );.  iContinue =
26e4f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
26e50 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20  Label(v);..  /* 
26e51 53 75 70 70 72 65 73 73 20 64 75 70 6c 69 63 61  Suppress duplica
26e52 74 65 73 20 66 6f 72 20 55 4e 49 4f 4e 2c 20 45  tes for UNION, E
26e53 58 43 45 50 54 2c 20 61 6e 64 20 49 4e 54 45 52  XCEPT, and INTER
26e54 53 45 43 54 20 0a 20 20 2a 2f 0a 20 20 69 66 28  SECT .  */.  if(
26e55 20 72 65 67 50 72 65 76 20 29 7b 0a 20 20 20 20   regPrev ){.    
26e56 69 6e 74 20 6a 31 2c 20 6a 32 3b 0a 20 20 20 20  int j1, j2;.    
26e57 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
26e58 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 4e  AddOp1(v, OP_IfN
26e59 6f 74 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20  ot, regPrev);.  
26e5a 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64    j2 = sqlite3Vd
26e5b 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43  beAddOp4(v, OP_C
26e5c 6f 6d 70 61 72 65 2c 20 70 49 6e 2d 3e 69 4d 65  ompare, pIn->iMe
26e5d 6d 2c 20 72 65 67 50 72 65 76 2b 31 2c 20 70 49  m, regPrev+1, pI
26e5e 6e 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20 20 20  n->nMem,.       
26e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26e60 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
26e61 65 79 49 6e 66 6f 2c 20 70 34 74 79 70 65 29 3b  eyInfo, p4type);
26e62 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26e63 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d  AddOp3(v, OP_Jum
26e64 70 2c 20 6a 32 2b 32 2c 20 69 43 6f 6e 74 69 6e  p, j2+2, iContin
26e65 75 65 2c 20 6a 32 2b 32 29 3b 0a 20 20 20 20 73  ue, j2+2);.    s
26e66 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
26e67 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73  re(v, j1);.    s
26e68 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f  qlite3ExprCodeCo
26e69 70 79 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e  py(pParse, pIn->
26e6a 69 4d 65 6d 2c 20 72 65 67 50 72 65 76 2b 31 2c  iMem, regPrev+1,
26e6b 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20   pIn->nMem);.   
26e6c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26e6d 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
26e6e 2c 20 31 2c 20 72 65 67 50 72 65 76 29 3b 0a 20  , 1, regPrev);. 
26e6f 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d   }.  if( pParse-
26e70 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
26e71 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  d ) return 0;.. 
26e72 20 2f 2a 20 53 75 70 70 72 65 73 73 20 74 68 65   /* Suppress the
26e73 20 74 68 65 20 66 69 72 73 74 20 4f 46 46 53 45   the first OFFSE
26e74 54 20 65 6e 74 72 69 65 73 20 69 66 20 74 68 65  T entries if the
26e75 72 65 20 69 73 20 61 6e 20 4f 46 46 53 45 54 20  re is an OFFSET 
26e76 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20 63 6f  clause.  */.  co
26e77 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69  deOffset(v, p, i
26e78 43 6f 6e 74 69 6e 75 65 29 3b 0a 0a 20 20 73 77  Continue);..  sw
26e79 69 74 63 68 28 20 70 44 65 73 74 2d 3e 65 44 65  itch( pDest->eDe
26e7a 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20 53 74 6f  st ){.    /* Sto
26e7b 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 61 73  re the result as
26e7c 20 64 61 74 61 20 75 73 69 6e 67 20 61 20 75 6e   data using a un
26e7d 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20 2a 2f  ique key..    */
26e7e 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 54 61  .    case SRT_Ta
26e7f 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52  ble:.    case SR
26e80 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20  T_EphemTab: {.  
26e81 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c      int r1 = sql
26e82 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
26e83 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 6e  Parse);.      in
26e84 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65  t r2 = sqlite3Ge
26e85 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
26e86 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26e87 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
26e88 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d  MakeRecord, pIn-
26e89 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d  >iMem, pIn->nMem
26e8a 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
26e8b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26e8c 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 70  , OP_NewRowid, p
26e8d 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 72 32 29  Dest->iParm, r2)
26e8e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26e8f 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
26e90 49 6e 73 65 72 74 2c 20 70 44 65 73 74 2d 3e 69  Insert, pDest->i
26e91 50 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b 0a 20  Parm, r1, r2);. 
26e92 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26e93 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
26e94 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20  AG_APPEND);.    
26e95 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
26e96 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
26e97 72 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r2);.      sqlit
26e98 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
26e99 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
26e9a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
26e9b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
26e9c 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
26e9d 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20     /* If we are 
26e9e 63 72 65 61 74 69 6e 67 20 61 20 73 65 74 20 66  creating a set f
26e9f 6f 72 20 61 6e 20 22 65 78 70 72 20 49 4e 20 28  or an "expr IN (
26ea0 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e  SELECT ...)" con
26ea1 73 74 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74  struct,.    ** t
26ea2 68 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c 64  hen there should
26ea3 20 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74 65   be a single ite
26ea4 6d 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20  m on the stack. 
26ea5 20 57 72 69 74 65 20 74 68 69 73 0a 20 20 20 20   Write this.    
26ea6 2a 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68 65  ** item into the
26ea7 20 73 65 74 20 74 61 62 6c 65 20 77 69 74 68 20   set table with 
26ea8 62 6f 67 75 73 20 64 61 74 61 2e 0a 20 20 20 20  bogus data..    
26ea9 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
26eaa 53 65 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  Set: {.      int
26eab 20 72 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72   r1;.      asser
26eac 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20  t( pIn->nMem==1 
26ead 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 69  );.      p->affi
26eae 6e 69 74 79 20 3d 20 0a 20 20 20 20 20 20 20 20  nity = .        
26eaf 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41   sqlite3CompareA
26eb0 66 66 69 6e 69 74 79 28 70 2d 3e 70 45 4c 69 73  ffinity(p->pELis
26eb1 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 70  t->a[0].pExpr, p
26eb2 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 29 3b  Dest->affinity);
26eb3 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
26eb4 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
26eb5 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  arse);.      sql
26eb6 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
26eb7 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
26eb8 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 31 2c 20 72   pIn->iMem, 1, r
26eb9 31 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79 2c  1, &p->affinity,
26eba 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
26ebb 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
26ebc 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
26ebd 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 31 29 3b  , pIn->iMem, 1);
26ebe 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26ebf 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
26ec0 64 78 49 6e 73 65 72 74 2c 20 70 44 65 73 74 2d  dxInsert, pDest-
26ec1 3e 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20  >iParm, r1);.   
26ec2 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
26ec3 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
26ec4 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61   r1);.      brea
26ec5 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 20  k;.    }..#if 0 
26ec6 20 2f 2a 20 4e 65 76 65 72 20 6f 63 63 75 72 73   /* Never occurs
26ec7 20 6f 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20   on an ORDER BY 
26ec8 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 2f 2a 20  query */.    /* 
26ec9 49 66 20 61 6e 79 20 72 6f 77 20 65 78 69 73 74  If any row exist
26eca 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
26ecb 65 74 2c 20 72 65 63 6f 72 64 20 74 68 61 74 20  et, record that 
26ecc 66 61 63 74 20 61 6e 64 20 61 62 6f 72 74 2e 0a  fact and abort..
26ecd 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
26ece 53 52 54 5f 45 78 69 73 74 73 3a 20 7b 0a 20 20  SRT_Exists: {.  
26ecf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26ed0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
26ed1 67 65 72 2c 20 31 2c 20 70 44 65 73 74 2d 3e 69  ger, 1, pDest->i
26ed2 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 2f 2a 20  Parm);.      /* 
26ed3 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  The LIMIT clause
26ed4 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20   will terminate 
26ed5 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20  the loop for us 
26ed6 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  */.      break;.
26ed7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
26ed8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
26ed9 61 20 73 63 61 6c 61 72 20 73 65 6c 65 63 74 20  a scalar select 
26eda 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20  that is part of 
26edb 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74  an expression, t
26edc 68 65 6e 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65  hen.    ** store
26edd 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20   the results in 
26ede 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
26edf 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20  memory cell and 
26ee0 62 72 65 61 6b 20 6f 75 74 0a 20 20 20 20 2a 2a  break out.    **
26ee1 20 6f 66 20 74 68 65 20 73 63 61 6e 20 6c 6f 6f   of the scan loo
26ee2 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  p..    */.    ca
26ee3 73 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20  se SRT_Mem: {.  
26ee4 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 2d      assert( pIn-
26ee5 3e 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20 20 20  >nMem==1 );.    
26ee6 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
26ee7 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49  eMove(pParse, pI
26ee8 6e 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e  n->iMem, pDest->
26ee9 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20  iParm, 1);.     
26eea 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c   /* The LIMIT cl
26eeb 61 75 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20 6f  ause will jump o
26eec 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 66  ut of the loop f
26eed 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62  or us */.      b
26eee 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
26eef 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51  if /* #ifndef SQ
26ef0 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
26ef1 52 59 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 54 68  RY */..    /* Th
26ef2 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74  e results are st
26ef3 6f 72 65 64 20 69 6e 20 61 20 73 65 71 75 65 6e  ored in a sequen
26ef4 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 0a  ce of registers.
26ef5 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20      ** starting 
26ef6 61 74 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2e 20  at pDest->iMem. 
26ef7 20 54 68 65 6e 20 74 68 65 20 63 6f 2d 72 6f 75   Then the co-rou
26ef8 74 69 6e 65 20 79 69 65 6c 64 73 2e 0a 20 20 20  tine yields..   
26ef9 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
26efa 5f 43 6f 72 6f 75 74 69 6e 65 3a 20 7b 0a 20 20  _Coroutine: {.  
26efb 20 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69      if( pDest->i
26efc 4d 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Mem==0 ){.      
26efd 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20    pDest->iMem = 
26efe 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61  sqlite3GetTempRa
26eff 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d  nge(pParse, pIn-
26f00 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20  >nMem);.        
26f01 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 70 49  pDest->nMem = pI
26f02 6e 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 7d  n->nMem;.      }
26f03 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
26f04 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73  prCodeMove(pPars
26f05 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 44  e, pIn->iMem, pD
26f06 65 73 74 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74  est->iMem, pDest
26f07 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73  ->nMem);.      s
26f08 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
26f09 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44  (v, OP_Yield, pD
26f0a 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20  est->iParm);.   
26f0b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
26f0c 0a 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20  .    /* Results 
26f0d 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20  are stored in a 
26f0e 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69  sequence of regi
26f0f 73 74 65 72 73 2e 20 20 54 68 65 6e 20 74 68 65  sters.  Then the
26f10 0a 20 20 20 20 2a 2a 20 4f 50 5f 52 65 73 75 6c  .    ** OP_Resul
26f11 74 52 6f 77 20 6f 70 63 6f 64 65 20 69 73 20 75  tRow opcode is u
26f12 73 65 64 20 74 6f 20 63 61 75 73 65 20 73 71 6c  sed to cause sql
26f13 69 74 65 33 5f 73 74 65 70 28 29 20 74 6f 20 72  ite3_step() to r
26f14 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20 74 68 65  eturn.    ** the
26f15 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 72 65 73   next row of res
26f16 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ult..    */.    
26f17 63 61 73 65 20 53 52 54 5f 4f 75 74 70 75 74 3a  case SRT_Output:
26f18 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
26f19 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26f1a 5f 52 65 73 75 6c 74 52 6f 77 2c 20 70 49 6e 2d  _ResultRow, pIn-
26f1b 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d  >iMem, pIn->nMem
26f1c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26f1d 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
26f1e 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
26f1f 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e  pIn->iMem, pIn->
26f20 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 62 72 65  nMem);.      bre
26f21 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 21  ak;.    }..#if !
26f22 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
26f23 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20  MIT_TRIGGER).   
26f24 20 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65 20   /* Discard the 
26f25 72 65 73 75 6c 74 73 2e 20 20 54 68 69 73 20 69  results.  This i
26f26 73 20 75 73 65 64 20 66 6f 72 20 53 45 4c 45 43  s used for SELEC
26f27 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73  T statements ins
26f28 69 64 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 62  ide.    ** the b
26f29 6f 64 79 20 6f 66 20 61 20 54 52 49 47 47 45 52  ody of a TRIGGER
26f2a 2e 20 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f  .  The purpose o
26f2b 66 20 73 75 63 68 20 73 65 6c 65 63 74 73 20 69  f such selects i
26f2c 73 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a  s to call.    **
26f2d 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75   user-defined fu
26f2e 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76  nctions that hav
26f2f 65 20 73 69 64 65 20 65 66 66 65 63 74 73 2e 20  e side effects. 
26f30 20 57 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 0a   We do not care.
26f31 20 20 20 20 2a 2a 20 61 62 6f 75 74 20 74 68 65      ** about the
26f32 20 61 63 74 75 61 6c 20 72 65 73 75 6c 74 73 20   actual results 
26f33 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e 0a 20  of the select.. 
26f34 20 20 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c     */.    defaul
26f35 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61 6b  t: {.      break
26f36 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
26f37 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f   }..  /* Jump to
26f38 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
26f39 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c 49 4d 49  loop if the LIMI
26f3a 54 20 69 73 20 72 65 61 63 68 65 64 2e 0a 20 20  T is reached..  
26f3b 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d  */.  if( p->iLim
26f3c 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  it ){.    sqlite
26f3d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26f3e 50 5f 41 64 64 49 6d 6d 2c 20 70 2d 3e 69 4c 69  P_AddImm, p->iLi
26f3f 6d 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71  mit, -1);.    sq
26f40 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26f41 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d  v, OP_IfZero, p-
26f42 3e 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 29  >iLimit, iBreak)
26f43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65  ;.  }..  /* Gene
26f44 72 61 74 65 20 74 68 65 20 73 75 62 72 6f 75 74  rate the subrout
26f45 69 6e 65 20 72 65 74 75 72 6e 0a 20 20 2a 2f 0a  ine return.  */.
26f46 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
26f47 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f  olveLabel(v, iCo
26f48 6e 74 69 6e 75 65 29 3b 0a 20 20 73 71 6c 69 74  ntinue);.  sqlit
26f49 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
26f4a 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 52 65  OP_Return, regRe
26f4b 74 75 72 6e 29 3b 0a 0a 20 20 72 65 74 75 72 6e  turn);..  return
26f4c 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   addr;.}../*.** 
26f4d 41 6c 74 65 72 6e 61 74 69 76 65 20 63 6f 6d 70  Alternative comp
26f4e 6f 75 6e 64 20 73 65 6c 65 63 74 20 63 6f 64 65  ound select code
26f4f 20 67 65 6e 65 72 61 74 6f 72 20 66 6f 72 20 63   generator for c
26f50 61 73 65 73 20 77 68 65 6e 20 74 68 65 72 65 0a  ases when there.
26f51 2a 2a 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  ** is an ORDER B
26f52 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20  Y clause..**.** 
26f53 57 65 20 61 73 73 75 6d 65 20 61 20 71 75 65 72  We assume a quer
26f54 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  y of the followi
26f55 6e 67 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20  ng form:.**.**  
26f56 20 20 20 20 3c 73 65 6c 65 63 74 41 3e 20 20 3c      <selectA>  <
26f57 6f 70 65 72 61 74 6f 72 3e 20 20 3c 73 65 6c 65  operator>  <sele
26f58 63 74 42 3e 20 20 4f 52 44 45 52 20 42 59 20 3c  ctB>  ORDER BY <
26f59 6f 72 64 65 72 62 79 6c 69 73 74 3e 0a 2a 2a 0a  orderbylist>.**.
26f5a 2a 2a 20 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73  ** <operator> is
26f5b 20 6f 6e 65 20 6f 66 20 55 4e 49 4f 4e 20 41 4c   one of UNION AL
26f5c 4c 2c 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54  L, UNION, EXCEPT
26f5d 2c 20 6f 72 20 49 4e 54 45 52 53 45 43 54 2e 20  , or INTERSECT. 
26f5e 20 54 68 65 20 69 64 65 61 0a 2a 2a 20 69 73 20   The idea.** is 
26f5f 74 6f 20 63 6f 64 65 20 62 6f 74 68 20 3c 73 65  to code both <se
26f60 6c 65 63 74 41 3e 20 61 6e 64 20 3c 73 65 6c 65  lectA> and <sele
26f61 63 74 42 3e 20 77 69 74 68 20 74 68 65 20 4f 52  ctB> with the OR
26f62 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 73  DER BY clause as
26f63 0a 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 73 2e  .** co-routines.
26f64 20 20 54 68 65 6e 20 72 75 6e 20 74 68 65 20 63    Then run the c
26f65 6f 2d 72 6f 75 74 69 6e 65 73 20 69 6e 20 70 61  o-routines in pa
26f66 72 61 6c 6c 65 6c 20 61 6e 64 20 6d 65 72 67 65  rallel and merge
26f67 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20   the results.** 
26f68 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e  into the output.
26f69 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f    In addition to
26f6a 20 74 68 65 20 74 77 6f 20 63 6f 72 6f 75 74 69   the two corouti
26f6b 6e 65 73 20 28 63 61 6c 6c 65 64 20 73 65 6c 65  nes (called sele
26f6c 63 74 41 20 61 6e 64 0a 2a 2a 20 73 65 6c 65 63  ctA and.** selec
26f6d 74 42 29 20 74 68 65 72 65 20 61 72 65 20 37 20  tB) there are 7 
26f6e 73 75 62 72 6f 75 74 69 6e 65 73 3a 0a 2a 2a 0a  subroutines:.**.
26f6f 2a 2a 20 20 20 20 6f 75 74 41 3a 20 20 20 20 4d  **    outA:    M
26f70 6f 76 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f  ove the output o
26f71 66 20 74 68 65 20 73 65 6c 65 63 74 41 20 63 6f  f the selectA co
26f72 72 6f 75 74 69 6e 65 20 69 6e 74 6f 20 74 68 65  routine into the
26f73 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20   output.**      
26f74 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f         of the co
26f75 6d 70 6f 75 6e 64 20 71 75 65 72 79 2e 0a 2a 2a  mpound query..**
26f76 0a 2a 2a 20 20 20 20 6f 75 74 42 3a 20 20 20 20  .**    outB:    
26f77 4d 6f 76 65 20 74 68 65 20 6f 75 74 70 75 74 20  Move the output 
26f78 6f 66 20 74 68 65 20 73 65 6c 65 63 74 42 20 63  of the selectB c
26f79 6f 72 6f 75 74 69 6e 65 20 69 6e 74 6f 20 74 68  oroutine into th
26f7a 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20  e output.**     
26f7b 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 63          of the c
26f7c 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 2e 20 20  ompound query.  
26f7d 28 4f 6e 6c 79 20 67 65 6e 65 72 61 74 65 64 20  (Only generated 
26f7e 66 6f 72 20 55 4e 49 4f 4e 20 61 6e 64 0a 2a 2a  for UNION and.**
26f7f 20 20 20 20 20 20 20 20 20 20 20 20 20 55 4e 49               UNI
26f80 4f 4e 20 41 4c 4c 2e 20 20 45 58 43 45 50 54 20  ON ALL.  EXCEPT 
26f81 61 6e 64 20 49 4e 53 45 52 54 53 45 43 54 20 6e  and INSERTSECT n
26f82 65 76 65 72 20 6f 75 74 70 75 74 20 61 20 72 6f  ever output a ro
26f83 77 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20  w that.**       
26f84 20 20 20 20 20 20 61 70 70 65 61 72 73 20 6f 6e        appears on
26f85 6c 79 20 69 6e 20 42 2e 29 0a 2a 2a 0a 2a 2a 20  ly in B.).**.** 
26f86 20 20 20 41 6c 74 42 3a 20 20 20 20 43 61 6c 6c     AltB:    Call
26f87 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73  ed when there is
26f88 20 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20   data from both 
26f89 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41  coroutines and A
26f8a 3c 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 65 71  <B..**.**    Aeq
26f8b 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65  B:    Called whe
26f8c 6e 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20  n there is data 
26f8d 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74  from both corout
26f8e 69 6e 65 73 20 61 6e 64 20 41 3d 3d 42 2e 0a 2a  ines and A==B..*
26f8f 2a 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20 20 20  *.**    AgtB:   
26f90 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   Called when the
26f91 72 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20  re is data from 
26f92 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20  both coroutines 
26f93 61 6e 64 20 41 3e 42 2e 0a 2a 2a 0a 2a 2a 20 20  and A>B..**.**  
26f94 20 20 45 6f 66 41 3a 20 20 20 20 43 61 6c 6c 65    EofA:    Calle
26f95 64 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 65  d when data is e
26f96 78 68 61 75 73 74 65 64 20 66 72 6f 6d 20 73 65  xhausted from se
26f97 6c 65 63 74 41 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  lectA..**.**    
26f98 45 6f 66 42 3a 20 20 20 20 43 61 6c 6c 65 64 20  EofB:    Called 
26f99 77 68 65 6e 20 64 61 74 61 20 69 73 20 65 78 68  when data is exh
26f9a 61 75 73 74 65 64 20 66 72 6f 6d 20 73 65 6c 65  austed from sele
26f9b 63 74 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ctB..**.** The i
26f9c 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
26f9d 20 74 68 65 20 6c 61 74 74 65 72 20 66 69 76 65   the latter five
26f9e 20 73 75 62 72 6f 75 74 69 6e 65 73 20 64 65 70   subroutines dep
26f9f 65 6e 64 20 6f 6e 20 77 68 69 63 68 20 0a 2a 2a  end on which .**
26fa0 20 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73 20 75   <operator> is u
26fa1 73 65 64 3a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 20 20  sed:.**.**.**   
26fa2 20 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20            UNION 
26fa3 41 4c 4c 20 20 20 20 20 20 20 20 20 55 4e 49 4f  ALL         UNIO
26fa4 4e 20 20 20 20 20 20 20 20 20 20 20 20 45 58 43  N            EXC
26fa5 45 50 54 20 20 20 20 20 20 20 20 20 20 49 4e 54  EPT          INT
26fa6 45 52 53 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  ERSECT.**       
26fa7 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
26fa8 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
26fa9 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ---  -----------
26faa 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ---  -----------
26fab 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 41 6c 74 42  ------.**   AltB
26fac 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20  :   outA, nextA 
26fad 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41       outA, nextA
26fae 20 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78         outA, nex
26faf 74 41 20 20 20 20 20 20 20 20 20 6e 65 78 74 41  tA         nextA
26fb0 0a 2a 2a 0a 2a 2a 20 20 20 41 65 71 42 3a 20 20  .**.**   AeqB:  
26fb1 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20   outA, nextA    
26fb2 20 20 20 20 20 6e 65 78 74 41 20 20 20 20 20 20       nextA      
26fb3 20 20 20 20 20 20 20 6e 65 78 74 41 20 20 20 20         nextA    
26fb4 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41       outA, nextA
26fb5 0a 2a 2a 0a 2a 2a 20 20 20 41 67 74 42 3a 20 20  .**.**   AgtB:  
26fb6 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20   outB, nextB    
26fb7 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20    outB, nextB   
26fb8 20 20 20 20 20 20 20 6e 65 78 74 42 20 20 20 20         nextB    
26fb9 20 20 20 20 20 20 20 20 6e 65 78 74 42 0a 2a 2a          nextB.**
26fba 0a 2a 2a 20 20 20 45 6f 66 41 3a 20 20 20 6f 75  .**   EofA:   ou
26fbb 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 6f  tB, nextB      o
26fbc 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20  utB, nextB      
26fbd 20 20 20 20 68 61 6c 74 20 20 20 20 20 20 20 20      halt        
26fbe 20 20 20 20 20 68 61 6c 74 0a 2a 2a 0a 2a 2a 20       halt.**.** 
26fbf 20 20 45 6f 66 42 3a 20 20 20 6f 75 74 41 2c 20    EofB:   outA, 
26fc0 6e 65 78 74 41 20 20 20 20 20 20 6f 75 74 41 2c  nextA      outA,
26fc1 20 6e 65 78 74 41 20 20 20 20 20 20 20 6f 75 74   nextA       out
26fc2 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 20  A, nextA        
26fc3 20 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 49 6e 20 74   halt.**.** In t
26fc4 68 65 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 61  he AltB, AeqB, a
26fc5 6e 64 20 41 67 74 42 20 73 75 62 72 6f 75 74 69  nd AgtB subrouti
26fc6 6e 65 73 2c 20 61 6e 20 45 4f 46 20 6f 6e 20 41  nes, an EOF on A
26fc7 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 41   following nextA
26fc8 0a 2a 2a 20 63 61 75 73 65 73 20 61 6e 20 69 6d  .** causes an im
26fc9 6d 65 64 69 61 74 65 20 6a 75 6d 70 20 74 6f 20  mediate jump to 
26fca 45 6f 66 41 20 61 6e 64 20 61 6e 20 45 4f 46 20  EofA and an EOF 
26fcb 6f 6e 20 42 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e  on B following n
26fcc 65 78 74 42 20 63 61 75 73 65 73 0a 2a 2a 20 61  extB causes.** a
26fcd 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70  n immediate jump
26fce 20 74 6f 20 45 6f 66 42 2e 20 20 57 69 74 68 69   to EofB.  Withi
26fcf 6e 20 45 6f 66 41 20 61 6e 64 20 45 6f 66 42 2c  n EofA and EofB,
26fd0 20 61 6e 64 20 45 4f 46 20 6f 6e 20 65 6e 74 72   and EOF on entr
26fd1 79 20 6f 72 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  y or.** followin
26fd2 67 20 6e 65 78 74 58 20 63 61 75 73 65 73 20 61  g nextX causes a
26fd3 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64   jump to the end
26fd4 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 70   of the select p
26fd5 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a  rocessing..**.**
26fd6 20 44 75 70 6c 69 63 61 74 65 20 72 65 6d 6f 76   Duplicate remov
26fd7 61 6c 20 69 6e 20 74 68 65 20 55 4e 49 4f 4e 2c  al in the UNION,
26fd8 20 45 58 43 45 50 54 2c 20 61 6e 64 20 49 4e 54   EXCEPT, and INT
26fd9 45 52 53 45 43 54 20 63 61 73 65 73 20 69 73 20  ERSECT cases is 
26fda 68 61 6e 64 6c 65 64 0a 2a 2a 20 77 69 74 68 69  handled.** withi
26fdb 6e 20 74 68 65 20 6f 75 74 70 75 74 20 73 75 62  n the output sub
26fdc 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 72 65  routine.  The re
26fdd 67 50 72 65 76 20 72 65 67 69 73 74 65 72 20 73  gPrev register s
26fde 65 74 20 68 6f 6c 64 73 20 74 68 65 20 70 72 65  et holds the pre
26fdf 76 69 6f 75 73 6c 79 0a 2a 2a 20 6f 75 74 70 75  viously.** outpu
26fe0 74 20 76 61 6c 75 65 2e 20 20 41 20 63 6f 6d 70  t value.  A comp
26fe1 61 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 20 61  arison is made a
26fe2 67 61 69 6e 73 74 20 74 68 69 73 20 76 61 6c 75  gainst this valu
26fe3 65 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74  e and the output
26fe4 0a 2a 2a 20 69 73 20 73 6b 69 70 70 65 64 20 69  .** is skipped i
26fe5 66 20 74 68 65 20 6e 65 78 74 20 72 65 73 75 6c  f the next resul
26fe6 74 73 20 77 6f 75 6c 64 20 62 65 20 74 68 65 20  ts would be the 
26fe7 73 61 6d 65 20 61 73 20 74 68 65 20 70 72 65 76  same as the prev
26fe8 69 6f 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ious..**.** The 
26fe9 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 70  implementation p
26fea 6c 61 6e 20 69 73 20 74 6f 20 69 6d 70 6c 65 6d  lan is to implem
26feb 65 6e 74 20 74 68 65 20 74 77 6f 20 63 6f 72 6f  ent the two coro
26fec 75 74 69 6e 65 73 20 61 6e 64 20 73 65 76 65 6e  utines and seven
26fed 0a 2a 2a 20 73 75 62 72 6f 75 74 69 6e 65 73 20  .** subroutines 
26fee 66 69 72 73 74 2c 20 74 68 65 6e 20 70 75 74 20  first, then put 
26fef 74 68 65 20 63 6f 6e 74 72 6f 6c 20 6c 6f 67 69  the control logi
26ff0 63 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 2e  c at the bottom.
26ff1 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a    Like this:.**.
26ff2 2a 2a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  **          goto
26ff3 20 49 6e 69 74 0a 2a 2a 20 20 20 20 20 63 6f 41   Init.**     coA
26ff4 3a 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20  : coroutine for 
26ff5 6c 65 66 74 20 71 75 65 72 79 20 28 41 29 0a 2a  left query (A).*
26ff6 2a 20 20 20 20 20 63 6f 42 3a 20 63 6f 72 6f 75  *     coB: corou
26ff7 74 69 6e 65 20 66 6f 72 20 72 69 67 68 74 20 71  tine for right q
26ff8 75 65 72 79 20 28 42 29 0a 2a 2a 20 20 20 20 6f  uery (B).**    o
26ff9 75 74 41 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20  utA: output one 
26ffa 72 6f 77 20 6f 66 20 41 0a 2a 2a 20 20 20 20 6f  row of A.**    o
26ffb 75 74 42 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20  utB: output one 
26ffc 72 6f 77 20 6f 66 20 42 20 28 55 4e 49 4f 4e 20  row of B (UNION 
26ffd 61 6e 64 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 6e  and UNION ALL on
26ffe 6c 79 29 0a 2a 2a 20 20 20 20 45 6f 66 41 3a 20  ly).**    EofA: 
26fff 2e 2e 2e 0a 2a 2a 20 20 20 20 45 6f 66 42 3a 20  ....**    EofB: 
27000 2e 2e 2e 0a 2a 2a 20 20 20 20 41 6c 74 42 3a 20  ....**    AltB: 
27001 2e 2e 2e 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20  ....**    AeqB: 
27002 2e 2e 2e 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20  ....**    AgtB: 
27003 2e 2e 2e 0a 2a 2a 20 20 20 20 49 6e 69 74 3a 20  ....**    Init: 
27004 69 6e 69 74 69 61 6c 69 7a 65 20 63 6f 72 6f 75  initialize corou
27005 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 0a 2a  tine registers.*
27006 2a 20 20 20 20 20 20 20 20 20 20 79 69 65 6c 64  *          yield
27007 20 63 6f 41 0a 2a 2a 20 20 20 20 20 20 20 20 20   coA.**         
27008 20 69 66 20 65 6f 66 28 41 29 20 67 6f 74 6f 20   if eof(A) goto 
27009 45 6f 66 41 0a 2a 2a 20 20 20 20 20 20 20 20 20  EofA.**         
2700a 20 79 69 65 6c 64 20 63 6f 42 0a 2a 2a 20 20 20   yield coB.**   
2700b 20 20 20 20 20 20 20 69 66 20 65 6f 66 28 42 29         if eof(B)
2700c 20 67 6f 74 6f 20 45 6f 66 42 0a 2a 2a 20 20 20   goto EofB.**   
2700d 20 43 6d 70 72 3a 20 43 6f 6d 70 61 72 65 20 41   Cmpr: Compare A
2700e 2c 20 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  , B.**          
2700f 4a 75 6d 70 20 41 6c 74 42 2c 20 41 65 71 42 2c  Jump AltB, AeqB,
27010 20 41 67 74 42 0a 2a 2a 20 20 20 20 20 45 6e 64   AgtB.**     End
27011 3a 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 63  : ....**.** We c
27012 61 6c 6c 20 41 6c 74 42 2c 20 41 65 71 42 2c 20  all AltB, AeqB, 
27013 41 67 74 42 2c 20 45 6f 66 41 2c 20 61 6e 64 20  AgtB, EofA, and 
27014 45 6f 66 42 20 22 73 75 62 72 6f 75 74 69 6e 65  EofB "subroutine
27015 73 22 20 62 75 74 20 74 68 65 79 20 61 72 65 20  s" but they are 
27016 6e 6f 74 0a 2a 2a 20 61 63 74 75 61 6c 6c 79 20  not.** actually 
27017 63 61 6c 6c 65 64 20 75 73 69 6e 67 20 47 6f 73  called using Gos
27018 75 62 20 61 6e 64 20 74 68 65 79 20 64 6f 20 6e  ub and they do n
27019 6f 74 20 52 65 74 75 72 6e 2e 20 20 45 6f 66 41  ot Return.  EofA
2701a 20 61 6e 64 20 45 6f 66 42 20 6c 6f 6f 70 0a 2a   and EofB loop.*
2701b 2a 20 75 6e 74 69 6c 20 61 6c 6c 20 64 61 74 61  * until all data
2701c 20 69 73 20 65 78 68 61 75 73 74 65 64 20 74 68   is exhausted th
2701d 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 22  en jump to the "
2701e 65 6e 64 22 20 6c 61 62 65 2e 20 20 41 6c 74 42  end" labe.  AltB
2701f 2c 20 41 65 71 42 2c 0a 2a 2a 20 61 6e 64 20 41  , AeqB,.** and A
27020 67 74 42 20 6a 75 6d 70 20 74 6f 20 65 69 74 68  gtB jump to eith
27021 65 72 20 4c 32 20 6f 72 20 74 6f 20 6f 6e 65 20  er L2 or to one 
27022 6f 66 20 45 6f 66 41 20 6f 72 20 45 6f 66 42 2e  of EofA or EofB.
27023 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
27024 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
27025 5f 53 45 4c 45 43 54 0a 73 74 61 74 69 63 20 69  _SELECT.static i
27026 6e 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72  nt multiSelectOr
27027 64 65 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a  derBy(.  Parse *
27028 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
27029 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
2702a 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
2702b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2702c 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f  The right-most o
2702d 66 20 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20  f SELECTs to be 
2702e 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63  coded */.  Selec
2702f 74 44 65 73 74 20 2a 70 44 65 73 74 20 20 20 20  tDest *pDest    
27030 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77   /* What to do w
27031 69 74 68 20 71 75 65 72 79 20 72 65 73 75 6c 74  ith query result
27032 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  s */.){.  int i,
27033 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
27034 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
27035 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50   */.  Select *pP
27036 72 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 41  rior;       /* A
27037 6e 6f 74 68 65 72 20 53 45 4c 45 43 54 20 69 6d  nother SELECT im
27038 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6f 75 72  mediately to our
27039 20 6c 65 66 74 20 2a 2f 0a 20 20 56 64 62 65 20   left */.  Vdbe 
2703a 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
2703b 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
2703c 65 20 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a  e to this VDBE *
2703d 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64  /.  SelectDest d
2703e 65 73 74 41 3b 20 20 20 20 20 2f 2a 20 44 65 73  estA;     /* Des
2703f 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20 63 6f 72  tination for cor
27040 6f 75 74 69 6e 65 20 41 20 2a 2f 0a 20 20 53 65  outine A */.  Se
27041 6c 65 63 74 44 65 73 74 20 64 65 73 74 42 3b 20  lectDest destB; 
27042 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69      /* Destinati
27043 6f 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e 65  on for coroutine
27044 20 42 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41   B */.  int regA
27045 64 64 72 41 3b 20 20 20 20 20 20 20 20 20 2f 2a  ddrA;         /*
27046 20 41 64 64 72 65 73 73 20 72 65 67 69 73 74 65   Address registe
27047 72 20 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 63  r for select-A c
27048 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  oroutine */.  in
27049 74 20 72 65 67 45 6f 66 41 3b 20 20 20 20 20 20  t regEofA;      
2704a 20 20 20 20 2f 2a 20 46 6c 61 67 20 74 6f 20 69      /* Flag to i
2704b 6e 64 69 63 61 74 65 20 77 68 65 6e 20 73 65 6c  ndicate when sel
2704c 65 63 74 2d 41 20 69 73 20 63 6f 6d 70 6c 65 74  ect-A is complet
2704d 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 64  e */.  int regAd
2704e 64 72 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  drB;         /* 
2704f 41 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72  Address register
27050 20 66 6f 72 20 73 65 6c 65 63 74 2d 42 20 63 6f   for select-B co
27051 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
27052 20 72 65 67 45 6f 66 42 3b 20 20 20 20 20 20 20   regEofB;       
27053 20 20 20 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e     /* Flag to in
27054 64 69 63 61 74 65 20 77 68 65 6e 20 73 65 6c 65  dicate when sele
27055 63 74 2d 42 20 69 73 20 63 6f 6d 70 6c 65 74 65  ct-B is complete
27056 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65   */.  int addrSe
27057 6c 65 63 74 41 3b 20 20 20 20 20 20 2f 2a 20 41  lectA;      /* A
27058 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65  ddress of the se
27059 6c 65 63 74 2d 41 20 63 6f 72 6f 75 74 69 6e 65  lect-A coroutine
2705a 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65   */.  int addrSe
2705b 6c 65 63 74 42 3b 20 20 20 20 20 20 2f 2a 20 41  lectB;      /* A
2705c 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65  ddress of the se
2705d 6c 65 63 74 2d 42 20 63 6f 72 6f 75 74 69 6e 65  lect-B coroutine
2705e 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 75 74   */.  int regOut
2705f 41 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  A;          /* A
27060 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20  ddress register 
27061 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 2d 41  for the output-A
27062 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20   subroutine */. 
27063 20 69 6e 74 20 72 65 67 4f 75 74 42 3b 20 20 20   int regOutB;   
27064 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
27065 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74  s register for t
27066 68 65 20 6f 75 74 70 75 74 2d 42 20 73 75 62 72  he output-B subr
27067 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
27068 61 64 64 72 4f 75 74 41 3b 20 20 20 20 20 20 20  addrOutA;       
27069 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
2706a 74 68 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62  the output-A sub
2706b 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
2706c 20 61 64 64 72 4f 75 74 42 20 3d 20 30 3b 20 20   addrOutB = 0;  
2706d 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
2706e 20 74 68 65 20 6f 75 74 70 75 74 2d 42 20 73 75   the output-B su
2706f 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
27070 74 20 61 64 64 72 45 6f 66 41 3b 20 20 20 20 20  t addrEofA;     
27071 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
27072 66 20 74 68 65 20 73 65 6c 65 63 74 2d 41 2d 65  f the select-A-e
27073 78 68 61 75 73 74 65 64 20 73 75 62 72 6f 75 74  xhausted subrout
27074 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
27075 72 45 6f 66 42 3b 20 20 20 20 20 20 20 20 20 2f  rEofB;         /
27076 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
27077 20 73 65 6c 65 63 74 2d 42 2d 65 78 68 61 75 73   select-B-exhaus
27078 74 65 64 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  ted subroutine *
27079 2f 0a 20 20 69 6e 74 20 61 64 64 72 41 6c 74 42  /.  int addrAltB
2707a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64  ;         /* Add
2707b 72 65 73 73 20 6f 66 20 74 68 65 20 41 3c 42 20  ress of the A<B 
2707c 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  subroutine */.  
2707d 69 6e 74 20 61 64 64 72 41 65 71 42 3b 20 20 20  int addrAeqB;   
2707e 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
2707f 20 6f 66 20 74 68 65 20 41 3d 3d 42 20 73 75 62   of the A==B sub
27080 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
27081 20 61 64 64 72 41 67 74 42 3b 20 20 20 20 20 20   addrAgtB;      
27082 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
27083 20 74 68 65 20 41 3e 42 20 73 75 62 72 6f 75 74   the A>B subrout
27084 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ine */.  int reg
27085 4c 69 6d 69 74 41 3b 20 20 20 20 20 20 20 20 2f  LimitA;        /
27086 2a 20 4c 69 6d 69 74 20 72 65 67 69 73 74 65 72  * Limit register
27087 20 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f   for select-A */
27088 0a 20 20 69 6e 74 20 72 65 67 4c 69 6d 69 74 42  .  int regLimitB
27089 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6d 69  ;        /* Limi
2708a 74 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73  t register for s
2708b 65 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69 6e 74  elect-A */.  int
2708c 20 72 65 67 50 72 65 76 3b 20 20 20 20 20 20 20   regPrev;       
2708d 20 20 20 2f 2a 20 41 20 72 61 6e 67 65 20 6f 66     /* A range of
2708e 20 72 65 67 69 73 74 65 72 73 20 74 6f 20 68 6f   registers to ho
2708f 6c 64 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70  ld previous outp
27090 75 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  ut */.  int save
27091 64 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a  dLimit;       /*
27092 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20   Saved value of 
27093 70 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 69  p->iLimit */.  i
27094 6e 74 20 73 61 76 65 64 4f 66 66 73 65 74 3b 20  nt savedOffset; 
27095 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61       /* Saved va
27096 6c 75 65 20 6f 66 20 70 2d 3e 69 4f 66 66 73 65  lue of p->iOffse
27097 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c  t */.  int label
27098 43 6d 70 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  Cmpr;        /* 
27099 4c 61 62 65 6c 20 66 6f 72 20 74 68 65 20 73 74  Label for the st
2709a 61 72 74 20 6f 66 20 74 68 65 20 6d 65 72 67 65  art of the merge
2709b 20 61 6c 67 6f 72 69 74 68 6d 20 2a 2f 0a 20 20   algorithm */.  
2709c 69 6e 74 20 6c 61 62 65 6c 45 6e 64 3b 20 20 20  int labelEnd;   
2709d 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66        /* Label f
2709e 6f 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  or the end of th
2709f 65 20 6f 76 65 72 61 6c 6c 20 53 45 4c 45 43 54  e overall SELECT
270a0 20 73 74 6d 74 20 2a 2f 0a 20 20 69 6e 74 20 6a   stmt */.  int j
270a1 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
270a2 20 2f 2a 20 4a 75 6d 70 20 69 6e 73 74 72 75 63   /* Jump instruc
270a3 74 69 6f 6e 73 20 74 68 61 74 20 67 65 74 20 72  tions that get r
270a4 65 74 61 72 67 65 74 74 65 64 20 2a 2f 0a 20 20  etargetted */.  
270a5 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20  int op;         
270a6 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
270a7 54 4b 5f 41 4c 4c 2c 20 54 4b 5f 55 4e 49 4f 4e  TK_ALL, TK_UNION
270a8 2c 20 54 4b 5f 45 58 43 45 50 54 2c 20 54 4b 5f  , TK_EXCEPT, TK_
270a9 49 4e 54 45 52 53 45 43 54 20 2a 2f 0a 20 20 4b  INTERSECT */.  K
270aa 65 79 49 6e 66 6f 20 2a 70 4b 65 79 44 75 70 20  eyInfo *pKeyDup 
270ab 3d 20 30 3b 20 2f 2a 20 43 6f 6d 70 61 72 69 73  = 0; /* Comparis
270ac 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  on information f
270ad 6f 72 20 64 75 70 6c 69 63 61 74 65 20 72 65 6d  or duplicate rem
270ae 6f 76 61 6c 20 2a 2f 0a 20 20 4b 65 79 49 6e 66  oval */.  KeyInf
270af 6f 20 2a 70 4b 65 79 4d 65 72 67 65 3b 20 20 20  o *pKeyMerge;   
270b0 2f 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e  /* Comparison in
270b1 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 6d 65  formation for me
270b2 72 67 69 6e 67 20 72 6f 77 73 20 2a 2f 0a 20 20  rging rows */.  
270b3 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
270b4 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
270b5 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
270b6 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
270b7 65 72 42 79 3b 20 20 20 2f 2a 20 54 68 65 20 4f  erBy;   /* The O
270b8 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
270b9 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79  /.  int nOrderBy
270ba 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
270bb 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
270bc 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
270bd 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50  use */.  int *aP
270be 65 72 6d 75 74 65 3b 20 20 20 20 20 20 20 20 2f  ermute;        /
270bf 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 4f  * Mapping from O
270c0 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 6f  RDER BY terms to
270c1 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75   result set colu
270c2 6d 6e 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  mns */..  assert
270c3 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 21 3d 30  ( p->pOrderBy!=0
270c4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b   );.  assert( pK
270c5 65 79 44 75 70 3d 3d 30 20 29 3b 20 2f 2a 20 22  eyDup==0 ); /* "
270c6 4d 61 6e 61 67 65 64 22 20 63 6f 64 65 20 6e 65  Managed" code ne
270c7 65 64 73 20 74 68 69 73 2e 20 20 54 69 63 6b 65  eds this.  Ticke
270c8 74 20 23 33 33 38 32 2e 20 2a 2f 0a 20 20 64 62  t #3382. */.  db
270c9 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
270ca 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64   v = pParse->pVd
270cb 62 65 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  be;.  if( v==0 )
270cc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
270cd 4f 4d 45 4d 3b 0a 20 20 6c 61 62 65 6c 45 6e 64  OMEM;.  labelEnd
270ce 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
270cf 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 6c 61  keLabel(v);.  la
270d0 62 65 6c 43 6d 70 72 20 3d 20 73 71 6c 69 74 65  belCmpr = sqlite
270d1 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
270d2 29 3b 0a 0a 0a 20 20 2f 2a 20 50 61 74 63 68 20  );...  /* Patch 
270d3 75 70 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  up the ORDER BY 
270d4 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20 6f 70  clause.  */.  op
270d5 20 3d 20 70 2d 3e 6f 70 3b 20 20 0a 20 20 70 50   = p->op;  .  pP
270d6 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72  rior = p->pPrior
270d7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69  ;.  assert( pPri
270d8 6f 72 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20  or->pOrderBy==0 
270d9 29 3b 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20  );.  pOrderBy = 
270da 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 61  p->pOrderBy;.  a
270db 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 20  ssert( pOrderBy 
270dc 29 3b 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 20  );.  nOrderBy = 
270dd 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b  pOrderBy->nExpr;
270de 0a 0a 20 20 2f 2a 20 46 6f 72 20 6f 70 65 72 61  ..  /* For opera
270df 74 6f 72 73 20 6f 74 68 65 72 20 74 68 61 6e 20  tors other than 
270e0 55 4e 49 4f 4e 20 41 4c 4c 20 77 65 20 68 61 76  UNION ALL we hav
270e1 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  e to make sure t
270e2 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 4f 52 44  hat.  ** the ORD
270e3 45 52 20 42 59 20 63 6c 61 75 73 65 20 63 6f 76  ER BY clause cov
270e4 65 72 73 20 65 76 65 72 79 20 74 65 72 6d 20 6f  ers every term o
270e5 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
270e6 2e 20 20 41 64 64 0a 20 20 2a 2a 20 74 65 72 6d  .  Add.  ** term
270e7 73 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20 42  s to the ORDER B
270e8 59 20 63 6c 61 75 73 65 20 61 73 20 6e 65 63 65  Y clause as nece
270e9 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66  ssary..  */.  if
270ea 28 20 6f 70 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a  ( op!=TK_ALL ){.
270eb 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 64 62 2d      for(i=1; db-
270ec 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30  >mallocFailed==0
270ed 20 26 26 20 69 3c 3d 70 2d 3e 70 45 4c 69 73 74   && i<=p->pEList
270ee 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
270ef 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
270f0 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
270f1 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c  ;.      for(j=0,
270f2 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d   pItem=pOrderBy-
270f3 3e 61 3b 20 6a 3c 6e 4f 72 64 65 72 42 79 3b 20  >a; j<nOrderBy; 
270f4 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  j++, pItem++){. 
270f5 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
270f6 49 74 65 6d 2d 3e 69 43 6f 6c 3e 30 20 29 3b 0a  Item->iCol>0 );.
270f7 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65          if( pIte
270f8 6d 2d 3e 69 43 6f 6c 3d 3d 69 20 29 20 62 72 65  m->iCol==i ) bre
270f9 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
270fa 20 20 69 66 28 20 6a 3d 3d 6e 4f 72 64 65 72 42    if( j==nOrderB
270fb 79 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70  y ){.        Exp
270fc 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  r *pNew = sqlite
270fd 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
270fe 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 2c  K_INTEGER, 0, 0,
270ff 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
27100 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72   pNew==0 ) retur
27101 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
27102 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c          pNew->fl
27103 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c  ags |= EP_IntVal
27104 75 65 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  ue;.        pNew
27105 2d 3e 69 54 61 62 6c 65 20 3d 20 69 3b 0a 20 20  ->iTable = i;.  
27106 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d        pOrderBy =
27107 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
27108 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
27109 4f 72 64 65 72 42 79 2c 20 70 4e 65 77 2c 20 30  OrderBy, pNew, 0
2710a 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 64 65  );.        pOrde
2710b 72 42 79 2d 3e 61 5b 6e 4f 72 64 65 72 42 79 2b  rBy->a[nOrderBy+
2710c 2b 5d 2e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69  +].iCol = (u16)i
2710d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2710e 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74    }..  /* Comput
2710f 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  e the comparison
27110 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 6e 64   permutation and
27111 20 6b 65 79 69 6e 66 6f 20 74 68 61 74 20 69 73   keyinfo that is
27112 20 75 73 65 64 20 77 69 74 68 0a 20 20 2a 2a 20   used with.  ** 
27113 74 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20  the permutation 
27114 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6d 70  in order to comp
27115 61 72 69 73 6f 6e 73 20 74 6f 20 64 65 74 65 72  arisons to deter
27116 6d 69 6e 65 20 69 66 20 74 68 65 20 6e 65 78 74  mine if the next
27117 0a 20 20 2a 2a 20 72 6f 77 20 6f 66 20 72 65 73  .  ** row of res
27118 75 6c 74 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20  ults comes from 
27119 73 65 6c 65 63 74 41 20 6f 72 20 73 65 6c 65 63  selectA or selec
2711a 74 42 2e 20 20 41 6c 73 6f 20 61 64 64 20 65 78  tB.  Also add ex
2711b 70 6c 69 63 69 74 0a 20 20 2a 2a 20 63 6f 6c 6c  plicit.  ** coll
2711c 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 4f 52  ations to the OR
2711d 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74 65  DER BY clause te
2711e 72 6d 73 20 73 6f 20 74 68 61 74 20 77 68 65 6e  rms so that when
2711f 20 74 68 65 20 73 75 62 71 75 65 72 69 65 73 0a   the subqueries.
27120 20 20 2a 2a 20 74 6f 20 74 68 65 20 72 69 67 68    ** to the righ
27121 74 20 61 6e 64 20 74 68 65 20 6c 65 66 74 20 61  t and the left a
27122 72 65 20 65 76 61 6c 75 61 74 65 64 2c 20 74 68  re evaluated, th
27123 65 79 20 75 73 65 20 74 68 65 20 63 6f 72 72 65  ey use the corre
27124 63 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f  ct.  ** collatio
27125 6e 2e 0a 20 20 2a 2f 0a 20 20 61 50 65 72 6d 75  n..  */.  aPermu
27126 74 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  te = sqlite3DbMa
27127 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
27128 6f 66 28 69 6e 74 29 2a 6e 4f 72 64 65 72 42 79  of(int)*nOrderBy
27129 29 3b 0a 20 20 69 66 28 20 61 50 65 72 6d 75 74  );.  if( aPermut
2712a 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  e ){.    struct 
2712b 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
2712c 49 74 65 6d 3b 0a 20 20 20 20 66 6f 72 28 69 3d  Item;.    for(i=
2712d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42  0, pItem=pOrderB
2712e 79 2d 3e 61 3b 20 69 3c 6e 4f 72 64 65 72 42 79  y->a; i<nOrderBy
2712f 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
27130 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
27131 49 74 65 6d 2d 3e 69 43 6f 6c 3e 30 20 20 26 26  Item->iCol>0  &&
27132 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3c 3d 70 2d   pItem->iCol<=p-
27133 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  >pEList->nExpr )
27134 3b 0a 20 20 20 20 20 20 61 50 65 72 6d 75 74 65  ;.      aPermute
27135 5b 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 69 43 6f  [i] = pItem->iCo
27136 6c 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  l - 1;.    }.   
27137 20 70 4b 65 79 4d 65 72 67 65 20 3d 0a 20 20 20   pKeyMerge =.   
27138 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c     sqlite3DbMall
27139 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
2713a 28 2a 70 4b 65 79 4d 65 72 67 65 29 2b 6e 4f 72  (*pKeyMerge)+nOr
2713b 64 65 72 42 79 2a 28 73 69 7a 65 6f 66 28 43 6f  derBy*(sizeof(Co
2713c 6c 6c 53 65 71 2a 29 2b 31 29 29 3b 0a 20 20 20  llSeq*)+1));.   
2713d 20 69 66 28 20 70 4b 65 79 4d 65 72 67 65 20 29   if( pKeyMerge )
2713e 7b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67  {.      pKeyMerg
2713f 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  e->aSortOrder = 
27140 28 75 38 2a 29 26 70 4b 65 79 4d 65 72 67 65 2d  (u8*)&pKeyMerge-
27141 3e 61 43 6f 6c 6c 5b 6e 4f 72 64 65 72 42 79 5d  >aColl[nOrderBy]
27142 3b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67  ;.      pKeyMerg
27143 65 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  e->nField = (u16
27144 29 6e 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 20  )nOrderBy;.     
27145 20 70 4b 65 79 4d 65 72 67 65 2d 3e 65 6e 63 20   pKeyMerge->enc 
27146 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 20  = ENC(db);.     
27147 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64   for(i=0; i<nOrd
27148 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  erBy; i++){.    
27149 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
2714a 6c 6c 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72  ll;.        Expr
2714b 20 2a 70 54 65 72 6d 20 3d 20 70 4f 72 64 65 72   *pTerm = pOrder
2714c 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  By->a[i].pExpr;.
2714d 20 20 20 20 20 20 20 20 69 66 28 20 70 54 65 72          if( pTer
2714e 6d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  m->flags & EP_Ex
2714f 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
27150 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 54        pColl = pT
27151 65 72 6d 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20  erm->pColl;.    
27152 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27153 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 6d 75 6c       pColl = mul
27154 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28  tiSelectCollSeq(
27155 70 50 61 72 73 65 2c 20 70 2c 20 61 50 65 72 6d  pParse, p, aPerm
27156 75 74 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20  ute[i]);.       
27157 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20     pTerm->flags 
27158 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  |= EP_ExpCollate
27159 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65 72  ;.          pTer
2715a 6d 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c  m->pColl = pColl
2715b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2715c 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61      pKeyMerge->a
2715d 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b  Coll[i] = pColl;
2715e 0a 20 20 20 20 20 20 20 20 70 4b 65 79 4d 65 72  .        pKeyMer
2715f 67 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69  ge->aSortOrder[i
27160 5d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  ] = pOrderBy->a[
27161 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  i].sortOrder;.  
27162 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
27163 6c 73 65 7b 0a 20 20 20 20 70 4b 65 79 4d 65 72  lse{.    pKeyMer
27164 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  ge = 0;.  }..  /
27165 2a 20 52 65 61 74 74 61 63 68 20 74 68 65 20 4f  * Reattach the O
27166 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74  RDER BY clause t
27167 6f 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20 2a  o the query..  *
27168 2f 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  /.  p->pOrderBy 
27169 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 50  = pOrderBy;.  pP
2716a 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20 3d  rior->pOrderBy =
2716b 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2716c 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  Dup(pParse->db, 
2716d 70 4f 72 64 65 72 42 79 29 3b 0a 0a 20 20 2f 2a  pOrderBy);..  /*
2716e 20 41 6c 6c 6f 63 61 74 65 20 61 20 72 61 6e 67   Allocate a rang
2716f 65 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72  e of temporary r
27170 65 67 69 73 74 65 72 73 20 61 6e 64 20 74 68 65  egisters and the
27171 20 4b 65 79 49 6e 66 6f 20 6e 65 65 64 65 64 0a   KeyInfo needed.
27172 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 6c 6f 67    ** for the log
27173 69 63 20 74 68 61 74 20 72 65 6d 6f 76 65 73 20  ic that removes 
27174 64 75 70 6c 69 63 61 74 65 20 72 65 73 75 6c 74  duplicate result
27175 20 72 6f 77 73 20 77 68 65 6e 20 74 68 65 0a 20   rows when the. 
27176 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20 69 73 20   ** operator is 
27177 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f  UNION, EXCEPT, o
27178 72 20 49 4e 54 45 52 53 45 43 54 20 28 62 75 74  r INTERSECT (but
27179 20 6e 6f 74 20 55 4e 49 4f 4e 20 41 4c 4c 29 2e   not UNION ALL).
2717a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d  .  */.  if( op==
2717b 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65  TK_ALL ){.    re
2717c 67 50 72 65 76 20 3d 20 30 3b 0a 20 20 7d 65 6c  gPrev = 0;.  }el
2717d 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 45 78 70  se{.    int nExp
2717e 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  r = p->pEList->n
2717f 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74  Expr;.    assert
27180 28 20 6e 4f 72 64 65 72 42 79 3e 3d 6e 45 78 70  ( nOrderBy>=nExp
27181 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
27182 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 67  ailed );.    reg
27183 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33 47 65  Prev = sqlite3Ge
27184 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  tTempRange(pPars
27185 65 2c 20 6e 45 78 70 72 2b 31 29 3b 0a 20 20 20  e, nExpr+1);.   
27186 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27187 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
27188 2c 20 30 2c 20 72 65 67 50 72 65 76 29 3b 0a 20  , 0, regPrev);. 
27189 20 20 20 70 4b 65 79 44 75 70 20 3d 20 73 71 6c     pKeyDup = sql
2718a 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2718b 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  (db,.           
2718c 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70         sizeof(*p
2718d 4b 65 79 44 75 70 29 20 2b 20 6e 45 78 70 72 2a  KeyDup) + nExpr*
2718e 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a  (sizeof(CollSeq*
2718f 29 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20  )+1) );.    if( 
27190 70 4b 65 79 44 75 70 20 29 7b 0a 20 20 20 20 20  pKeyDup ){.     
27191 20 70 4b 65 79 44 75 70 2d 3e 61 53 6f 72 74 4f   pKeyDup->aSortO
27192 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70 4b 65  rder = (u8*)&pKe
27193 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70  yDup->aColl[nExp
27194 72 5d 3b 0a 20 20 20 20 20 20 70 4b 65 79 44 75  r];.      pKeyDu
27195 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  p->nField = (u16
27196 29 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4b  )nExpr;.      pK
27197 65 79 44 75 70 2d 3e 65 6e 63 20 3d 20 45 4e 43  eyDup->enc = ENC
27198 28 64 62 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  (db);.      for(
27199 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b  i=0; i<nExpr; i+
2719a 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79  +){.        pKey
2719b 44 75 70 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20  Dup->aColl[i] = 
2719c 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53  multiSelectCollS
2719d 65 71 28 70 50 61 72 73 65 2c 20 70 2c 20 69 29  eq(pParse, p, i)
2719e 3b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 44 75  ;.        pKeyDu
2719f 70 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  p->aSortOrder[i]
271a0 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
271a1 20 20 7d 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 53    }.  }. .  /* S
271a2 65 70 61 72 61 74 65 20 74 68 65 20 6c 65 66 74  eparate the left
271a3 20 61 6e 64 20 74 68 65 20 72 69 67 68 74 20 71   and the right q
271a4 75 65 72 79 20 66 72 6f 6d 20 6f 6e 65 20 61 6e  uery from one an
271a5 6f 74 68 65 72 0a 20 20 2a 2f 0a 20 20 70 2d 3e  other.  */.  p->
271a6 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 70 50  pPrior = 0;.  pP
271a7 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74  rior->pRightmost
271a8 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 52   = 0;.  sqlite3R
271a9 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
271aa 42 79 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  By(pParse, p, p-
271ab 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44 45  >pOrderBy, "ORDE
271ac 52 22 29 3b 0a 20 20 69 66 28 20 70 50 72 69 6f  R");.  if( pPrio
271ad 72 2d 3e 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a  r->pPrior==0 ){.
271ae 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c      sqlite3Resol
271af 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70  veOrderGroupBy(p
271b0 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 70  Parse, pPrior, p
271b1 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 2c  Prior->pOrderBy,
271b2 20 22 4f 52 44 45 52 22 29 3b 0a 20 20 7d 0a 0a   "ORDER");.  }..
271b3 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65    /* Compute the
271b4 20 6c 69 6d 69 74 20 72 65 67 69 73 74 65 72 73   limit registers
271b5 20 2a 2f 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d   */.  computeLim
271b6 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72  itRegisters(pPar
271b7 73 65 2c 20 70 2c 20 6c 61 62 65 6c 45 6e 64 29  se, p, labelEnd)
271b8 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69  ;.  if( p->iLimi
271b9 74 20 26 26 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20  t && op==TK_ALL 
271ba 29 7b 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 41  ){.    regLimitA
271bb 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
271bc 6d 3b 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 42  m;.    regLimitB
271bd 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
271be 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  m;.    sqlite3Vd
271bf 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
271c0 6f 70 79 2c 20 70 2d 3e 69 4f 66 66 73 65 74 20  opy, p->iOffset 
271c1 3f 20 70 2d 3e 69 4f 66 66 73 65 74 2b 31 20 3a  ? p->iOffset+1 :
271c2 20 70 2d 3e 69 4c 69 6d 69 74 2c 0a 20 20 20 20   p->iLimit,.    
271c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
271c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
271c5 67 4c 69 6d 69 74 41 29 3b 0a 20 20 20 20 73 71  gLimitA);.    sq
271c6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
271c7 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4c  v, OP_Copy, regL
271c8 69 6d 69 74 41 2c 20 72 65 67 4c 69 6d 69 74 42  imitA, regLimitB
271c9 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
271ca 72 65 67 4c 69 6d 69 74 41 20 3d 20 72 65 67 4c  regLimitA = regL
271cb 69 6d 69 74 42 20 3d 20 30 3b 0a 20 20 7d 0a 20  imitB = 0;.  }. 
271cc 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
271cd 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74  te(db, p->pLimit
271ce 29 3b 0a 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d  );.  p->pLimit =
271cf 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70   0;.  sqlite3Exp
271d0 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
271d1 4f 66 66 73 65 74 29 3b 0a 20 20 70 2d 3e 70 4f  Offset);.  p->pO
271d2 66 66 73 65 74 20 3d 20 30 3b 0a 0a 20 20 72 65  ffset = 0;..  re
271d3 67 41 64 64 72 41 20 3d 20 2b 2b 70 50 61 72 73  gAddrA = ++pPars
271d4 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 45 6f  e->nMem;.  regEo
271d5 66 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  fA = ++pParse->n
271d6 4d 65 6d 3b 0a 20 20 72 65 67 41 64 64 72 42 20  Mem;.  regAddrB 
271d7 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
271d8 3b 0a 20 20 72 65 67 45 6f 66 42 20 3d 20 2b 2b  ;.  regEofB = ++
271d9 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
271da 72 65 67 4f 75 74 41 20 3d 20 2b 2b 70 50 61 72  regOutA = ++pPar
271db 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f  se->nMem;.  regO
271dc 75 74 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  utB = ++pParse->
271dd 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53  nMem;.  sqlite3S
271de 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64  electDestInit(&d
271df 65 73 74 41 2c 20 53 52 54 5f 43 6f 72 6f 75 74  estA, SRT_Corout
271e0 69 6e 65 2c 20 72 65 67 41 64 64 72 41 29 3b 0a  ine, regAddrA);.
271e1 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
271e2 65 73 74 49 6e 69 74 28 26 64 65 73 74 42 2c 20  estInit(&destB, 
271e3 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72  SRT_Coroutine, r
271e4 65 67 41 64 64 72 42 29 3b 0a 0a 20 20 2f 2a 20  egAddrB);..  /* 
271e5 4a 75 6d 70 20 70 61 73 74 20 74 68 65 20 76 61  Jump past the va
271e6 72 69 6f 75 73 20 73 75 62 72 6f 75 74 69 6e 65  rious subroutine
271e7 73 20 61 6e 64 20 63 6f 72 6f 75 74 69 6e 65 73  s and coroutines
271e8 20 74 6f 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a   to the main.  *
271e9 2a 20 6d 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a  * merge loop.  *
271ea 2f 0a 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33  /.  j1 = sqlite3
271eb 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
271ec 5f 47 6f 74 6f 29 3b 0a 20 20 61 64 64 72 53 65  _Goto);.  addrSe
271ed 6c 65 63 74 41 20 3d 20 73 71 6c 69 74 65 33 56  lectA = sqlite3V
271ee 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
271ef 29 3b 0a 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  );...  /* Genera
271f0 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 74  te a coroutine t
271f1 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 53  o evaluate the S
271f2 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
271f3 74 6f 20 74 68 65 0a 20 20 2a 2a 20 6c 65 66 74  to the.  ** left
271f4 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   of the compound
271f5 20 6f 70 65 72 61 74 6f 72 20 2d 20 74 68 65 20   operator - the 
271f6 22 41 22 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f  "A" select..  */
271f7 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65  .  VdbeNoopComme
271f8 6e 74 28 28 76 2c 20 22 42 65 67 69 6e 20 63 6f  nt((v, "Begin co
271f9 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74  routine for left
271fa 20 53 45 4c 45 43 54 22 29 29 3b 0a 20 20 70 50   SELECT"));.  pP
271fb 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 20 3d 20 72  rior->iLimit = r
271fc 65 67 4c 69 6d 69 74 41 3b 0a 20 20 73 71 6c 69  egLimitA;.  sqli
271fd 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
271fe 2c 20 70 50 72 69 6f 72 2c 20 26 64 65 73 74 41  , pPrior, &destA
271ff 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
27200 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
27201 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 41  eger, 1, regEofA
27202 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
27203 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
27204 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20  ld, regAddrA);. 
27205 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
27206 28 28 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74  ((v, "End corout
27207 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 53 45 4c  ine for left SEL
27208 45 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65  ECT"));..  /* Ge
27209 6e 65 72 61 74 65 20 61 20 63 6f 72 6f 75 74 69  nerate a corouti
2720a 6e 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74  ne to evaluate t
2720b 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2720c 65 6e 74 20 6f 6e 20 0a 20 20 2a 2a 20 74 68 65  ent on .  ** the
2720d 20 72 69 67 68 74 20 2d 20 74 68 65 20 22 42 22   right - the "B"
2720e 20 73 65 6c 65 63 74 0a 20 20 2a 2f 0a 20 20 61   select.  */.  a
2720f 64 64 72 53 65 6c 65 63 74 42 20 3d 20 73 71 6c  ddrSelectB = sql
27210 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
27211 64 64 72 28 76 29 3b 0a 20 20 56 64 62 65 4e 6f  ddr(v);.  VdbeNo
27212 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42  opComment((v, "B
27213 65 67 69 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66  egin coroutine f
27214 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43 54 22  or right SELECT"
27215 29 29 3b 0a 20 20 73 61 76 65 64 4c 69 6d 69 74  ));.  savedLimit
27216 20 3d 20 70 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20   = p->iLimit;.  
27217 73 61 76 65 64 4f 66 66 73 65 74 20 3d 20 70 2d  savedOffset = p-
27218 3e 69 4f 66 66 73 65 74 3b 0a 20 20 70 2d 3e 69  >iOffset;.  p->i
27219 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69 6d 69 74  Limit = regLimit
2721a 42 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74 20  B;.  p->iOffset 
2721b 3d 20 30 3b 20 20 0a 20 20 73 71 6c 69 74 65 33  = 0;  .  sqlite3
2721c 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2721d 2c 20 26 64 65 73 74 42 29 3b 0a 20 20 70 2d 3e  , &destB);.  p->
2721e 69 4c 69 6d 69 74 20 3d 20 73 61 76 65 64 4c 69  iLimit = savedLi
2721f 6d 69 74 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65  mit;.  p->iOffse
27220 74 20 3d 20 73 61 76 65 64 4f 66 66 73 65 74 3b  t = savedOffset;
27221 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27222 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
27223 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 42 29 3b  er, 1, regEofB);
27224 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27225 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
27226 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20 56  , regAddrB);.  V
27227 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
27228 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e  v, "End coroutin
27229 65 20 66 6f 72 20 72 69 67 68 74 20 53 45 4c 45  e for right SELE
2722a 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e  CT"));..  /* Gen
2722b 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
2722c 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20  ne that outputs 
2722d 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
2722e 6f 66 20 74 68 65 20 41 0a 20 20 2a 2a 20 73 65  of the A.  ** se
2722f 6c 65 63 74 20 61 73 20 74 68 65 20 6e 65 78 74  lect as the next
27230 20 6f 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74   output row of t
27231 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
27232 63 74 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e  ct..  */.  VdbeN
27233 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
27234 4f 75 74 70 75 74 20 72 6f 75 74 69 6e 65 20 66  Output routine f
27235 6f 72 20 41 22 29 29 3b 0a 20 20 61 64 64 72 4f  or A"));.  addrO
27236 75 74 41 20 3d 20 67 65 6e 65 72 61 74 65 4f 75  utA = generateOu
27237 74 70 75 74 53 75 62 72 6f 75 74 69 6e 65 28 70  tputSubroutine(p
27238 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20  Parse,.         
27239 20 20 20 20 20 20 20 20 70 2c 20 26 64 65 73 74          p, &dest
2723a 41 2c 20 70 44 65 73 74 2c 20 72 65 67 4f 75 74  A, pDest, regOut
2723b 41 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  A,.             
2723c 20 20 20 20 72 65 67 50 72 65 76 2c 20 70 4b 65      regPrev, pKe
2723d 79 44 75 70 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  yDup, P4_KEYINFO
2723e 5f 48 41 4e 44 4f 46 46 2c 20 6c 61 62 65 6c 45  _HANDOFF, labelE
2723f 6e 64 29 3b 0a 20 20 0a 20 20 2f 2a 20 47 65 6e  nd);.  .  /* Gen
27240 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
27241 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20  ne that outputs 
27242 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
27243 6f 66 20 74 68 65 20 42 0a 20 20 2a 2a 20 73 65  of the B.  ** se
27244 6c 65 63 74 20 61 73 20 74 68 65 20 6e 65 78 74  lect as the next
27245 20 6f 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74   output row of t
27246 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
27247 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f  ct..  */.  if( o
27248 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d  p==TK_ALL || op=
27249 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20  =TK_UNION ){.   
2724a 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
2724b 28 28 76 2c 20 22 4f 75 74 70 75 74 20 72 6f 75  ((v, "Output rou
2724c 74 69 6e 65 20 66 6f 72 20 42 22 29 29 3b 0a 20  tine for B"));. 
2724d 20 20 20 61 64 64 72 4f 75 74 42 20 3d 20 67 65     addrOutB = ge
2724e 6e 65 72 61 74 65 4f 75 74 70 75 74 53 75 62 72  nerateOutputSubr
2724f 6f 75 74 69 6e 65 28 70 50 61 72 73 65 2c 0a 20  outine(pParse,. 
27250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27251 70 2c 20 26 64 65 73 74 42 2c 20 70 44 65 73 74  p, &destB, pDest
27252 2c 20 72 65 67 4f 75 74 42 2c 0a 20 20 20 20 20  , regOutB,.     
27253 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67 50              regP
27254 72 65 76 2c 20 70 4b 65 79 44 75 70 2c 20 50 34  rev, pKeyDup, P4
27255 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 2c  _KEYINFO_STATIC,
27256 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 0a   labelEnd);.  }.
27257 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
27258 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72   subroutine to r
27259 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 75  un when the resu
2725a 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20  lts from select 
2725b 41 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75  A.  ** are exhau
2725c 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61  sted and only da
2725d 74 61 20 69 6e 20 73 65 6c 65 63 74 20 42 20 72  ta in select B r
2725e 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 56  emains..  */.  V
2725f 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
27260 76 2c 20 22 65 6f 66 2d 41 20 73 75 62 72 6f 75  v, "eof-A subrou
27261 74 69 6e 65 22 29 29 3b 0a 20 20 69 66 28 20 6f  tine"));.  if( o
27262 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 7c 7c 20  p==TK_EXCEPT || 
27263 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54  op==TK_INTERSECT
27264 20 29 7b 0a 20 20 20 20 61 64 64 72 45 6f 66 41   ){.    addrEofA
27265 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
27266 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
27267 20 30 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20   0, labelEnd);. 
27268 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 61 64   }else{  .    ad
27269 64 72 45 6f 66 41 20 3d 20 73 71 6c 69 74 65 33  drEofA = sqlite3
2726a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2726b 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 6c 61  _If, regEofB, la
2726c 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c  belEnd);.    sql
2726d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2726e 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f  , OP_Gosub, regO
2726f 75 74 42 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a  utB, addrOutB);.
27270 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27271 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
27272 64 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20  d, regAddrB);.  
27273 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27274 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
27275 30 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20  0, addrEofA);.  
27276 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  }..  /* Generate
27277 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f   a subroutine to
27278 20 72 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65   run when the re
27279 73 75 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63  sults from selec
2727a 74 20 42 0a 20 20 2a 2a 20 61 72 65 20 65 78 68  t B.  ** are exh
2727b 61 75 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20  austed and only 
2727c 64 61 74 61 20 69 6e 20 73 65 6c 65 63 74 20 41  data in select A
2727d 20 72 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20   remains..  */. 
2727e 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45   if( op==TK_INTE
2727f 52 53 45 43 54 20 29 7b 0a 20 20 20 20 61 64 64  RSECT ){.    add
27280 72 45 6f 66 42 20 3d 20 61 64 64 72 45 6f 66 41  rEofB = addrEofA
27281 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20  ;.  }else{  .   
27282 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
27283 28 28 76 2c 20 22 65 6f 66 2d 42 20 73 75 62 72  ((v, "eof-B subr
27284 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 61  outine"));.    a
27285 64 64 72 45 6f 66 42 20 3d 20 73 71 6c 69 74 65  ddrEofB = sqlite
27286 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27287 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 6c  P_If, regEofA, l
27288 61 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71  abelEnd);.    sq
27289 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2728a 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
2728b 4f 75 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b  OutA, addrOutA);
2728c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2728d 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
2728e 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20  ld, regAddrA);. 
2728f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27290 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
27291 20 30 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a 20   0, addrEofB);. 
27292 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
27293 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65  e code to handle
27294 20 74 68 65 20 63 61 73 65 20 6f 66 20 41 3c 42   the case of A<B
27295 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70  .  */.  VdbeNoop
27296 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 6c  Comment((v, "A-l
27297 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29  t-B subroutine")
27298 29 3b 0a 20 20 61 64 64 72 41 6c 74 42 20 3d 20  );.  addrAltB = 
27299 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2729a 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2729b 65 67 4f 75 74 41 2c 20 61 64 64 72 4f 75 74 41  egOutA, addrOutA
2729c 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2729d 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
2729e 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20  ld, regAddrA);. 
2729f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
272a0 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67  p2(v, OP_If, reg
272a1 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b  EofA, addrEofA);
272a2 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
272a3 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
272a4 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a   0, labelCmpr);.
272a5 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63  .  /* Generate c
272a6 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68  ode to handle th
272a7 65 20 63 61 73 65 20 6f 66 20 41 3d 3d 42 0a 20  e case of A==B. 
272a8 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b   */.  if( op==TK
272a9 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 61 64 64 72  _ALL ){.    addr
272aa 41 65 71 42 20 3d 20 61 64 64 72 41 6c 74 42 3b  AeqB = addrAltB;
272ab 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d  .  }else if( op=
272ac 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b  =TK_INTERSECT ){
272ad 0a 20 20 20 20 61 64 64 72 41 65 71 42 20 3d 20  .    addrAeqB = 
272ae 61 64 64 72 41 6c 74 42 3b 0a 20 20 20 20 61 64  addrAltB;.    ad
272af 64 72 41 6c 74 42 2b 2b 3b 0a 20 20 7d 65 6c 73  drAltB++;.  }els
272b0 65 7b 0a 20 20 20 20 56 64 62 65 4e 6f 6f 70 43  e{.    VdbeNoopC
272b1 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 65 71  omment((v, "A-eq
272b2 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29  -B subroutine"))
272b3 3b 0a 20 20 20 20 61 64 64 72 41 65 71 42 20 3d  ;.    addrAeqB =
272b4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
272b5 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
272b6 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20  ld, regAddrA);. 
272b7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
272b8 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp2(v, OP_If, r
272b9 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41  egEofA, addrEofA
272ba 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
272bb 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
272bc 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70  oto, 0, labelCmp
272bd 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65  r);.  }..  /* Ge
272be 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 68  nerate code to h
272bf 61 6e 64 6c 65 20 74 68 65 20 63 61 73 65 20 6f  andle the case o
272c0 66 20 41 3e 42 0a 20 20 2a 2f 0a 20 20 56 64 62  f A>B.  */.  Vdb
272c1 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
272c2 20 22 41 2d 67 74 2d 42 20 73 75 62 72 6f 75 74   "A-gt-B subrout
272c3 69 6e 65 22 29 29 3b 0a 20 20 61 64 64 72 41 67  ine"));.  addrAg
272c4 74 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  tB = sqlite3Vdbe
272c5 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
272c6 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c    if( op==TK_ALL
272c7 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e   || op==TK_UNION
272c8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
272c9 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
272ca 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 42 2c 20  Gosub, regOutB, 
272cb 61 64 64 72 4f 75 74 42 29 3b 0a 20 20 7d 0a 20  addrOutB);.  }. 
272cc 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
272cd 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
272ce 72 65 67 41 64 64 72 42 29 3b 0a 20 20 73 71 6c  regAddrB);.  sql
272cf 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
272d0 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42  , OP_If, regEofB
272d1 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 73  , addrEofB);.  s
272d2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
272d3 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
272d4 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f  labelCmpr);..  /
272d5 2a 20 54 68 69 73 20 63 6f 64 65 20 72 75 6e 73  * This code runs
272d6 20 6f 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c   once to initial
272d7 69 7a 65 20 65 76 65 72 79 74 68 69 6e 67 2e 0a  ize everything..
272d8 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
272d9 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
272da 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
272db 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
272dc 65 67 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 41  eger, 0, regEofA
272dd 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
272de 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
272df 65 67 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 42  eger, 0, regEofB
272e0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
272e1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73  AddOp2(v, OP_Gos
272e2 75 62 2c 20 72 65 67 41 64 64 72 41 2c 20 61 64  ub, regAddrA, ad
272e3 64 72 53 65 6c 65 63 74 41 29 3b 0a 20 20 73 71  drSelectA);.  sq
272e4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
272e5 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
272e6 41 64 64 72 42 2c 20 61 64 64 72 53 65 6c 65 63  AddrB, addrSelec
272e7 74 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  tB);.  sqlite3Vd
272e8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
272e9 66 2c 20 72 65 67 45 6f 66 41 2c 20 61 64 64 72  f, regEofA, addr
272ea 45 6f 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33  EofA);.  sqlite3
272eb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
272ec 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 61 64  _If, regEofB, ad
272ed 64 72 45 6f 66 42 29 3b 0a 0a 20 20 2f 2a 20 49  drEofB);..  /* I
272ee 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 6d 61 69  mplement the mai
272ef 6e 20 6d 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a  n merge loop.  *
272f0 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  /.  sqlite3VdbeR
272f1 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c  esolveLabel(v, l
272f2 61 62 65 6c 43 6d 70 72 29 3b 0a 20 20 73 71 6c  abelCmpr);.  sql
272f3 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
272f4 2c 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e  , OP_Permutation
272f5 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72  , 0, 0, 0, (char
272f6 2a 29 61 50 65 72 6d 75 74 65 2c 20 50 34 5f 49  *)aPermute, P4_I
272f7 4e 54 41 52 52 41 59 29 3b 0a 20 20 73 71 6c 69  NTARRAY);.  sqli
272f8 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
272f9 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 64 65 73   OP_Compare, des
272fa 74 41 2e 69 4d 65 6d 2c 20 64 65 73 74 42 2e 69  tA.iMem, destB.i
272fb 4d 65 6d 2c 20 6e 4f 72 64 65 72 42 79 2c 0a 20  Mem, nOrderBy,. 
272fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
272fd 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
272fe 4b 65 79 4d 65 72 67 65 2c 20 50 34 5f 4b 45 59  KeyMerge, P4_KEY
272ff 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20  INFO_HANDOFF);. 
27300 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27301 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 61  p3(v, OP_Jump, a
27302 64 64 72 41 6c 74 42 2c 20 61 64 64 72 41 65 71  ddrAltB, addrAeq
27303 42 2c 20 61 64 64 72 41 67 74 42 29 3b 0a 0a 20  B, addrAgtB);.. 
27304 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 65 6d 70   /* Release temp
27305 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 0a  orary registers.
27306 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67 50 72    */.  if( regPr
27307 65 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ev ){.    sqlite
27308 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
27309 65 28 70 50 61 72 73 65 2c 20 72 65 67 50 72 65  e(pParse, regPre
2730a 76 2c 20 6e 4f 72 64 65 72 42 79 2b 31 29 3b 0a  v, nOrderBy+1);.
2730b 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74    }..  /* Jump t
2730c 6f 20 74 68 65 20 74 68 69 73 20 70 6f 69 6e 74  o the this point
2730d 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 65 72   in order to ter
2730e 6d 69 6e 61 74 65 20 74 68 65 20 71 75 65 72 79  minate the query
2730f 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
27310 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
27311 28 76 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a  (v, labelEnd);..
27312 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e 75 6d    /* Set the num
27313 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20 63 6f  ber of output co
27314 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28  lumns.  */.  if(
27315 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
27316 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20  RT_Output ){.   
27317 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20   Select *pFirst 
27318 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 77 68  = pPrior;.    wh
27319 69 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72  ile( pFirst->pPr
2731a 69 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70  ior ) pFirst = p
2731b 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20  First->pPrior;. 
2731c 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d     generateColum
2731d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30  nNames(pParse, 0
2731e 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74  , pFirst->pEList
2731f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61  );.  }..  /* Rea
27320 73 73 65 6d 62 6c 79 20 74 68 65 20 63 6f 6d 70  ssembly the comp
27321 6f 75 6e 64 20 71 75 65 72 79 20 73 6f 20 74 68  ound query so th
27322 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72  at it will be fr
27323 65 65 64 20 63 6f 72 72 65 63 74 6c 79 0a 20 20  eed correctly.  
27324 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ** by the callin
27325 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  g function */.  
27326 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b  if( p->pPrior ){
27327 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65  .    sqlite3Sele
27328 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  ctDelete(db, p->
27329 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 70  pPrior);.  }.  p
2732a 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f  ->pPrior = pPrio
2732b 72 3b 0a 0a 20 20 2f 2a 2a 2a 20 54 42 44 3a 20  r;..  /*** TBD: 
2732c 20 49 6e 73 65 72 74 20 73 75 62 72 6f 75 74 69   Insert subrouti
2732d 6e 65 20 63 61 6c 6c 73 20 74 6f 20 63 6c 6f 73  ne calls to clos
2732e 65 20 63 75 72 73 6f 72 73 20 6f 6e 20 69 6e 63  e cursors on inc
2732f 6f 6d 70 6c 65 74 65 0a 20 20 2a 2a 2a 2a 20 73  omplete.  **** s
27330 75 62 71 75 65 72 69 65 73 20 2a 2a 2a 2a 2f 0a  ubqueries ****/.
27331 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
27332 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  OK;.}.#endif..#i
27333 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
27334 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
27335 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
27336 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f  ITE_OMIT_VIEW)./
27337 2a 20 46 6f 72 77 61 72 64 20 44 65 63 6c 61 72  * Forward Declar
27338 61 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63  ations */.static
27339 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 4c   void substExprL
2733a 69 73 74 28 73 71 6c 69 74 65 33 2a 2c 20 45 78  ist(sqlite3*, Ex
2733b 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 45 78  prList*, int, Ex
2733c 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69 63  prList*);.static
2733d 20 76 6f 69 64 20 73 75 62 73 74 53 65 6c 65 63   void substSelec
2733e 74 28 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c 65  t(sqlite3*, Sele
2733f 63 74 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c  ct *, int, ExprL
27340 69 73 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53  ist *);../*.** S
27341 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  can through the 
27342 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72  expression pExpr
27343 2e 20 20 52 65 70 6c 61 63 65 20 65 76 65 72 79  .  Replace every
27344 20 72 65 66 65 72 65 6e 63 65 20 74 6f 0a 2a 2a   reference to.**
27345 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62   a column in tab
27346 6c 65 20 6e 75 6d 62 65 72 20 69 54 61 62 6c 65  le number iTable
27347 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20   with a copy of 
27348 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 0a 2a  the iColumn-th.*
27349 2a 20 65 6e 74 72 79 20 69 6e 20 70 45 4c 69 73  * entry in pELis
2734a 74 2e 20 20 28 42 75 74 20 6c 65 61 76 65 20 72  t.  (But leave r
2734b 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
2734c 20 52 4f 57 49 44 20 63 6f 6c 75 6d 6e 20 0a 2a   ROWID column .*
2734d 2a 20 75 6e 63 68 61 6e 67 65 64 2e 29 0a 2a 2a  * unchanged.).**
2734e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2734f 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   is part of the 
27350 66 6c 61 74 74 65 6e 69 6e 67 20 70 72 6f 63 65  flattening proce
27351 64 75 72 65 2e 20 20 41 20 73 75 62 71 75 65 72  dure.  A subquer
27352 79 0a 2a 2a 20 77 68 6f 73 65 20 72 65 73 75 6c  y.** whose resul
27353 74 20 73 65 74 20 69 73 20 64 65 66 69 6e 65 64  t set is defined
27354 20 62 79 20 70 45 4c 69 73 74 20 61 70 70 65 61   by pEList appea
27355 72 73 20 61 73 20 65 6e 74 72 79 20 69 6e 20 74  rs as entry in t
27356 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73  he.** FROM claus
27357 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 75  e of a SELECT su
27358 63 68 20 74 68 61 74 20 74 68 65 20 56 44 42 45  ch that the VDBE
27359 20 63 75 72 73 6f 72 20 61 73 73 69 67 6e 65 64   cursor assigned
2735a 20 74 6f 20 74 68 61 74 0a 2a 2a 20 46 4f 52 4d   to that.** FORM
2735b 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20 69 73   clause entry is
2735c 20 69 54 61 62 6c 65 2e 20 20 54 68 69 73 20 72   iTable.  This r
2735d 6f 75 74 69 6e 65 20 6d 61 6b 65 20 74 68 65 20  outine make the 
2735e 6e 65 63 65 73 73 61 72 79 20 0a 2a 2a 20 63 68  necessary .** ch
2735f 61 6e 67 65 73 20 74 6f 20 70 45 78 70 72 20 73  anges to pExpr s
27360 6f 20 74 68 61 74 20 69 74 20 72 65 66 65 72 73  o that it refers
27361 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65   directly to the
27362 20 73 6f 75 72 63 65 20 74 61 62 6c 65 0a 2a 2a   source table.**
27363 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
27364 20 72 61 74 68 65 72 20 74 68 65 20 72 65 73 75   rather the resu
27365 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 73 75  lt set of the su
27366 62 71 75 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  bquery..*/.stati
27367 63 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72  c void substExpr
27368 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
27369 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f 72          /* Repor
2736a 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20  t malloc errors 
2736b 74 6f 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69  to this connecti
2736c 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  on */.  Expr *pE
2736d 78 70 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 45  xpr,        /* E
2736e 78 70 72 20 69 6e 20 77 68 69 63 68 20 73 75 62  xpr in which sub
2736f 73 74 69 74 75 74 69 6f 6e 20 6f 63 63 75 72 73  stitution occurs
27370 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
27371 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62  ,         /* Tab
27372 6c 65 20 74 6f 20 62 65 20 73 75 62 73 74 69 74  le to be substit
27373 75 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69  uted */.  ExprLi
27374 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a  st *pEList    /*
27375 20 53 75 62 73 74 69 74 75 74 65 20 65 78 70 72   Substitute expr
27376 65 73 73 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20  essions */.){.  
27377 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72  if( pExpr==0 ) r
27378 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 45 78  eturn;.  if( pEx
27379 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  pr->op==TK_COLUM
2737a 4e 20 26 26 20 70 45 78 70 72 2d 3e 69 54 61 62  N && pExpr->iTab
2737b 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20  le==iTable ){.  
2737c 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f    if( pExpr->iCo
2737d 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20  lumn<0 ){.      
2737e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e  pExpr->op = TK_N
2737f 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ULL;.    }else{.
27380 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77        Expr *pNew
27381 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
27382 70 45 4c 69 73 74 21 3d 30 20 26 26 20 70 45 78  pEList!=0 && pEx
27383 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 45 4c 69  pr->iColumn<pELi
27384 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20  st->nExpr );.   
27385 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
27386 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45  ->pLeft==0 && pE
27387 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 26  xpr->pRight==0 &
27388 26 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3d 3d  & pExpr->pList==
27389 30 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20  0 );.      pNew 
2738a 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 70 45 78 70  = pEList->a[pExp
2738b 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70  r->iColumn].pExp
2738c 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  r;.      assert(
2738d 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20 20 20   pNew!=0 );.    
2738e 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 70 4e    pExpr->op = pN
2738f 65 77 2d 3e 6f 70 3b 0a 20 20 20 20 20 20 61 73  ew->op;.      as
27390 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65  sert( pExpr->pLe
27391 66 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ft==0 );.      p
27392 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 73 71  Expr->pLeft = sq
27393 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
27394 20 70 4e 65 77 2d 3e 70 4c 65 66 74 29 3b 0a 20   pNew->pLeft);. 
27395 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
27396 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b  pr->pRight==0 );
27397 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52  .      pExpr->pR
27398 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78  ight = sqlite3Ex
27399 70 72 44 75 70 28 64 62 2c 20 70 4e 65 77 2d 3e  prDup(db, pNew->
2739a 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 61  pRight);.      a
2739b 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c  ssert( pExpr->pL
2739c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ist==0 );.      
2739d 70 45 78 70 72 2d 3e 70 4c 69 73 74 20 3d 20 73  pExpr->pList = s
2739e 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
2739f 70 28 64 62 2c 20 70 4e 65 77 2d 3e 70 4c 69 73  p(db, pNew->pLis
273a0 74 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  t);.      pExpr-
273a1 3e 69 54 61 62 6c 65 20 3d 20 70 4e 65 77 2d 3e  >iTable = pNew->
273a2 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 45  iTable;.      pE
273a3 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 4e 65 77  xpr->pTab = pNew
273a4 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 70 45  ->pTab;.      pE
273a5 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70  xpr->iColumn = p
273a6 4e 65 77 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  New->iColumn;.  
273a7 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20      pExpr->iAgg 
273a8 3d 20 70 4e 65 77 2d 3e 69 41 67 67 3b 0a 20 20  = pNew->iAgg;.  
273a9 20 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e      sqlite3Token
273aa 43 6f 70 79 28 64 62 2c 20 26 70 45 78 70 72 2d  Copy(db, &pExpr-
273ab 3e 74 6f 6b 65 6e 2c 20 26 70 4e 65 77 2d 3e 74  >token, &pNew->t
273ac 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c  oken);.      sql
273ad 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62  ite3TokenCopy(db
273ae 2c 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 2c 20  , &pExpr->span, 
273af 26 70 4e 65 77 2d 3e 73 70 61 6e 29 3b 0a 20 20  &pNew->span);.  
273b0 20 20 20 20 70 45 78 70 72 2d 3e 70 53 65 6c 65      pExpr->pSele
273b1 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ct = sqlite3Sele
273b2 63 74 44 75 70 28 64 62 2c 20 70 4e 65 77 2d 3e  ctDup(db, pNew->
273b3 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  pSelect);.      
273b4 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 3d 20 70  pExpr->flags = p
273b5 4e 65 77 2d 3e 66 6c 61 67 73 3b 0a 20 20 20 20  New->flags;.    
273b6 20 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66    pExpr->pAggInf
273b7 6f 20 3d 20 70 4e 65 77 2d 3e 70 41 67 67 49 6e  o = pNew->pAggIn
273b8 66 6f 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  fo;.      pNew->
273b9 70 41 67 67 49 6e 66 6f 20 3d 20 30 3b 0a 20 20  pAggInfo = 0;.  
273ba 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
273bb 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70   substExpr(db, p
273bc 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 69 54 61  Expr->pLeft, iTa
273bd 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
273be 20 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20    substExpr(db, 
273bf 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 69  pExpr->pRight, i
273c0 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
273c1 20 20 20 20 73 75 62 73 74 53 65 6c 65 63 74 28      substSelect(
273c2 64 62 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65  db, pExpr->pSele
273c3 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  ct, iTable, pELi
273c4 73 74 29 3b 0a 20 20 20 20 73 75 62 73 74 45 78  st);.    substEx
273c5 70 72 4c 69 73 74 28 64 62 2c 20 70 45 78 70 72  prList(db, pExpr
273c6 2d 3e 70 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c  ->pList, iTable,
273c7 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a   pEList);.  }.}.
273c8 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 62 73  static void subs
273c9 74 45 78 70 72 4c 69 73 74 28 0a 20 20 73 71 6c  tExprList(.  sql
273ca 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
273cb 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c    /* Report mall
273cc 6f 63 20 65 72 72 6f 72 73 20 68 65 72 65 20 2a  oc errors here *
273cd 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  /.  ExprList *pL
273ce 69 73 74 2c 20 20 20 20 20 2f 2a 20 4c 69 73 74  ist,     /* List
273cf 20 74 6f 20 73 63 61 6e 20 61 6e 64 20 69 6e 20   to scan and in 
273d0 77 68 69 63 68 20 74 6f 20 6d 61 6b 65 20 73 75  which to make su
273d1 62 73 74 69 74 75 74 65 73 20 2a 2f 0a 20 20 69  bstitutes */.  i
273d2 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20  nt iTable,      
273d3 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20      /* Table to 
273d4 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20 2a  be substituted *
273d5 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
273d6 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75 62 73  List     /* Subs
273d7 74 69 74 75 74 65 20 76 61 6c 75 65 73 20 2a 2f  titute values */
273d8 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
273d9 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
273da 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  turn;.  for(i=0;
273db 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
273dc 20 69 2b 2b 29 7b 0a 20 20 20 20 73 75 62 73 74   i++){.    subst
273dd 45 78 70 72 28 64 62 2c 20 70 4c 69 73 74 2d 3e  Expr(db, pList->
273de 61 5b 69 5d 2e 70 45 78 70 72 2c 20 69 54 61 62  a[i].pExpr, iTab
273df 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d  le, pEList);.  }
273e0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  .}.static void s
273e1 75 62 73 74 53 65 6c 65 63 74 28 0a 20 20 73 71  ubstSelect(.  sq
273e2 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
273e3 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c     /* Report mal
273e4 6c 6f 63 20 65 72 72 6f 72 73 20 68 65 72 65 20  loc errors here 
273e5 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
273e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c            /* SEL
273e7 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  ECT statement in
273e8 20 77 68 69 63 68 20 74 6f 20 6d 61 6b 65 20 73   which to make s
273e9 75 62 73 74 69 74 75 74 69 6f 6e 73 20 2a 2f 0a  ubstitutions */.
273ea 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
273eb 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
273ec 74 6f 20 62 65 20 72 65 70 6c 61 63 65 64 20 2a  to be replaced *
273ed 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
273ee 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75 62 73  List     /* Subs
273ef 74 69 74 75 74 65 20 76 61 6c 75 65 73 20 2a 2f  titute values */
273f0 0a 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  .){.  SrcList *p
273f1 53 72 63 3b 0a 20 20 73 74 72 75 63 74 20 53 72  Src;.  struct Sr
273f2 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
273f3 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  m;.  int i;.  if
273f4 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20  ( !p ) return;. 
273f5 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
273f6 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 69 54  b, p->pEList, iT
273f7 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
273f8 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
273f9 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20  b, p->pGroupBy, 
273fa 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
273fb 0a 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74  .  substExprList
273fc 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  (db, p->pOrderBy
273fd 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
273fe 29 3b 0a 20 20 73 75 62 73 74 45 78 70 72 28 64  );.  substExpr(d
273ff 62 2c 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 69  b, p->pHaving, i
27400 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
27401 20 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20    substExpr(db, 
27402 70 2d 3e 70 57 68 65 72 65 2c 20 69 54 61 62 6c  p->pWhere, iTabl
27403 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75  e, pEList);.  su
27404 62 73 74 53 65 6c 65 63 74 28 64 62 2c 20 70 2d  bstSelect(db, p-
27405 3e 70 50 72 69 6f 72 2c 20 69 54 61 62 6c 65 2c  >pPrior, iTable,
27406 20 70 45 4c 69 73 74 29 3b 0a 20 20 70 53 72 63   pEList);.  pSrc
27407 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73   = p->pSrc;.  as
27408 73 65 72 74 28 20 70 53 72 63 20 29 3b 20 20 2f  sert( pSrc );  /
27409 2a 20 45 76 65 6e 20 66 6f 72 20 28 53 45 4c 45  * Even for (SELE
2740a 43 54 20 31 29 20 77 65 20 68 61 76 65 3a 20 70  CT 1) we have: p
2740b 53 72 63 21 3d 30 20 62 75 74 20 70 53 72 63 2d  Src!=0 but pSrc-
2740c 3e 6e 53 72 63 3d 3d 30 20 2a 2f 0a 20 20 69 66  >nSrc==0 */.  if
2740d 28 20 41 4c 57 41 59 53 28 70 53 72 63 29 20 29  ( ALWAYS(pSrc) )
2740e 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 72 63  {.    for(i=pSrc
2740f 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70 53  ->nSrc, pItem=pS
27410 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c  rc->a; i>0; i--,
27411 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
27412 20 73 75 62 73 74 53 65 6c 65 63 74 28 64 62 2c   substSelect(db,
27413 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c   pItem->pSelect,
27414 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
27415 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65  ;.    }.  }.}.#e
27416 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
27417 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
27418 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e  QUERY) || !defin
27419 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2741a 49 45 57 29 20 2a 2f 0a 0a 23 69 66 20 21 64 65  IEW) */..#if !de
2741b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2741c 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21  T_SUBQUERY) || !
2741d 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2741e 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 0a 2a 2a 20  MIT_VIEW)./*.** 
2741f 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74  This routine att
27420 65 6d 70 74 73 20 74 6f 20 66 6c 61 74 74 65 6e  empts to flatten
27421 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 6f   subqueries in o
27422 72 64 65 72 20 74 6f 20 73 70 65 65 64 0a 2a 2a  rder to speed.**
27423 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 49 74 20   execution.  It 
27424 72 65 74 75 72 6e 73 20 31 20 69 66 20 69 74 20  returns 1 if it 
27425 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 61 6e  makes changes an
27426 64 20 30 20 69 66 20 6e 6f 20 66 6c 61 74 74 65  d 0 if no flatte
27427 6e 69 6e 67 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a  ning.** occurs..
27428 2a 2a 0a 2a 2a 20 54 6f 20 75 6e 64 65 72 73 74  **.** To underst
27429 61 6e 64 20 74 68 65 20 63 6f 6e 63 65 70 74 20  and the concept 
2742a 6f 66 20 66 6c 61 74 74 65 6e 69 6e 67 2c 20 63  of flattening, c
2742b 6f 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c  onsider the foll
2742c 6f 77 69 6e 67 0a 2a 2a 20 71 75 65 72 79 3a 0a  owing.** query:.
2742d 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
2742e 20 61 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20   a FROM (SELECT 
2742f 78 2b 79 20 41 53 20 61 20 46 52 4f 4d 20 74 31  x+y AS a FROM t1
27430 20 57 48 45 52 45 20 7a 3c 31 30 30 29 20 57 48   WHERE z<100) WH
27431 45 52 45 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68  ERE a>5.**.** Th
27432 65 20 64 65 66 61 75 6c 74 20 77 61 79 20 6f 66  e default way of
27433 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68   implementing th
27434 69 73 20 71 75 65 72 79 20 69 73 20 74 6f 20 65  is query is to e
27435 78 65 63 75 74 65 20 74 68 65 0a 2a 2a 20 73 75  xecute the.** su
27436 62 71 75 65 72 79 20 66 69 72 73 74 20 61 6e 64  bquery first and
27437 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
27438 74 73 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72  ts in a temporar
27439 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 0a 2a 2a  y table, then.**
2743a 20 72 75 6e 20 74 68 65 20 6f 75 74 65 72 20 71   run the outer q
2743b 75 65 72 79 20 6f 6e 20 74 68 61 74 20 74 65 6d  uery on that tem
2743c 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 20 54  porary table.  T
2743d 68 69 73 20 72 65 71 75 69 72 65 73 20 74 77 6f  his requires two
2743e 0a 2a 2a 20 70 61 73 73 65 73 20 6f 76 65 72 20  .** passes over 
2743f 74 68 65 20 64 61 74 61 2e 20 20 46 75 72 74 68  the data.  Furth
27440 65 72 6d 6f 72 65 2c 20 62 65 63 61 75 73 65 20  ermore, because 
27441 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61  the temporary ta
27442 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 69 6e  ble.** has no in
27443 64 69 63 65 73 2c 20 74 68 65 20 57 48 45 52 45  dices, the WHERE
27444 20 63 6c 61 75 73 65 20 6f 6e 20 74 68 65 20 6f   clause on the o
27445 75 74 65 72 20 71 75 65 72 79 20 63 61 6e 6e 6f  uter query canno
27446 74 20 62 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a 65  t be.** optimize
27447 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
27448 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74  utine attempts t
27449 6f 20 72 65 77 72 69 74 65 20 71 75 65 72 69 65  o rewrite querie
2744a 73 20 73 75 63 68 20 61 73 20 74 68 65 20 61 62  s such as the ab
2744b 6f 76 65 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 69  ove into.** a si
2744c 6e 67 6c 65 20 66 6c 61 74 20 73 65 6c 65 63 74  ngle flat select
2744d 2c 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  , like this:.**.
2744e 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b  **     SELECT x+
2744f 79 20 41 53 20 61 20 46 52 4f 4d 20 74 31 20 57  y AS a FROM t1 W
27450 48 45 52 45 20 7a 3c 31 30 30 20 41 4e 44 20 61  HERE z<100 AND a
27451 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  >5.**.** The cod
27452 65 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20  e generated for 
27453 74 68 69 73 20 73 69 6d 70 69 66 69 63 61 74 69  this simpificati
27454 6f 6e 20 67 69 76 65 73 20 74 68 65 20 73 61 6d  on gives the sam
27455 65 20 72 65 73 75 6c 74 0a 2a 2a 20 62 75 74 20  e result.** but 
27456 6f 6e 6c 79 20 68 61 73 20 74 6f 20 73 63 61 6e  only has to scan
27457 20 74 68 65 20 64 61 74 61 20 6f 6e 63 65 2e 20   the data once. 
27458 20 41 6e 64 20 62 65 63 61 75 73 65 20 69 6e 64   And because ind
27459 69 63 65 73 20 6d 69 67 68 74 20 0a 2a 2a 20 65  ices might .** e
2745a 78 69 73 74 20 6f 6e 20 74 68 65 20 74 61 62 6c  xist on the tabl
2745b 65 20 74 31 2c 20 61 20 63 6f 6d 70 6c 65 74 65  e t1, a complete
2745c 20 73 63 61 6e 20 6f 66 20 74 68 65 20 64 61 74   scan of the dat
2745d 61 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61 76  a might be.** av
2745e 6f 69 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6c 61  oided..**.** Fla
2745f 74 74 65 6e 69 6e 67 20 69 73 20 6f 6e 6c 79 20  ttening is only 
27460 61 74 74 65 6d 70 74 65 64 20 69 66 20 61 6c 6c  attempted if all
27461 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
27462 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a  g are true:.**.*
27463 2a 20 20 20 28 31 29 20 20 54 68 65 20 73 75 62  *   (1)  The sub
27464 71 75 65 72 79 20 61 6e 64 20 74 68 65 20 6f 75  query and the ou
27465 74 65 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74  ter query do not
27466 20 62 6f 74 68 20 75 73 65 20 61 67 67 72 65 67   both use aggreg
27467 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32  ates..**.**   (2
27468 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
27469 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67  is not an aggreg
2746a 61 74 65 20 6f 72 20 74 68 65 20 6f 75 74 65 72  ate or the outer
2746b 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20   query is not a 
2746c 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33  join..**.**   (3
2746d 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2746e 69 73 20 6e 6f 74 20 74 68 65 20 72 69 67 68 74  is not the right
2746f 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 6c 65   operand of a le
27470 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 0a 2a 2a  ft outer join.**
27471 20 20 20 20 20 20 20 20 28 4f 72 69 67 69 6e 61          (Origina
27472 6c 6c 79 20 74 69 63 6b 65 74 20 23 33 30 36 2e  lly ticket #306.
27473 20 20 53 74 72 65 6e 67 68 74 65 6e 65 64 20 62    Strenghtened b
27474 79 20 74 69 63 6b 65 74 20 23 33 33 30 30 29 0a  y ticket #3300).
27475 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 20 54 68 65  **.**   (4)  The
27476 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74   subquery is not
27477 20 44 49 53 54 49 4e 43 54 20 6f 72 20 74 68 65   DISTINCT or the
27478 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
27479 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a  not a join..**.*
2747a 2a 20 20 20 28 35 29 20 20 54 68 65 20 73 75 62  *   (5)  The sub
2747b 71 75 65 72 79 20 69 73 20 6e 6f 74 20 44 49 53  query is not DIS
2747c 54 49 4e 43 54 20 6f 72 20 74 68 65 20 6f 75 74  TINCT or the out
2747d 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  er query does no
2747e 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20 20  t use.**        
2747f 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a  aggregates..**.*
27480 2a 20 20 20 28 36 29 20 20 54 68 65 20 73 75 62  *   (6)  The sub
27481 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75  query does not u
27482 73 65 20 61 67 67 72 65 67 61 74 65 73 20 6f 72  se aggregates or
27483 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
27484 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20   is not.**      
27485 20 20 44 49 53 54 49 4e 43 54 2e 0a 2a 2a 0a 2a    DISTINCT..**.*
27486 2a 20 20 20 28 37 29 20 20 54 68 65 20 73 75 62  *   (7)  The sub
27487 71 75 65 72 79 20 68 61 73 20 61 20 46 52 4f 4d  query has a FROM
27488 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20   clause..**.**  
27489 20 28 38 29 20 20 54 68 65 20 73 75 62 71 75 65   (8)  The subque
2748a 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20  ry does not use 
2748b 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f 75 74  LIMIT or the out
2748c 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20  er query is not 
2748d 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  a join..**.**   
2748e 28 39 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (9)  The subquer
2748f 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c  y does not use L
27490 49 4d 49 54 20 6f 72 20 74 68 65 20 6f 75 74 65  IMIT or the oute
27491 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74  r query does not
27492 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 61   use.**        a
27493 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a  ggregates..**.**
27494 20 20 28 31 30 29 20 20 54 68 65 20 73 75 62 71    (10)  The subq
27495 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
27496 65 20 61 67 67 72 65 67 61 74 65 73 20 6f 72 20  e aggregates or 
27497 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
27498 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20  does not.**     
27499 20 20 20 75 73 65 20 4c 49 4d 49 54 2e 0a 2a 2a     use LIMIT..**
2749a 0a 2a 2a 20 20 28 31 31 29 20 20 54 68 65 20 73  .**  (11)  The s
2749b 75 62 71 75 65 72 79 20 61 6e 64 20 74 68 65 20  ubquery and the 
2749c 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 20 6e  outer query do n
2749d 6f 74 20 62 6f 74 68 20 68 61 76 65 20 4f 52 44  ot both have ORD
2749e 45 52 20 42 59 20 63 6c 61 75 73 65 73 2e 0a 2a  ER BY clauses..*
2749f 2a 0a 2a 2a 20 20 28 31 32 29 20 20 4e 6f 74 20  *.**  (12)  Not 
274a0 69 6d 70 6c 65 6d 65 6e 74 65 64 2e 20 20 53 75  implemented.  Su
274a1 62 73 75 6d 65 64 20 69 6e 74 6f 20 72 65 73 74  bsumed into rest
274a2 72 69 63 74 69 6f 6e 20 28 33 29 2e 20 20 57 61  riction (3).  Wa
274a3 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20  s previously.** 
274a4 20 20 20 20 20 20 20 61 20 73 65 70 61 72 61 74         a separat
274a5 65 20 72 65 73 74 72 69 63 74 69 6f 6e 20 64 65  e restriction de
274a6 72 69 76 69 6e 67 20 66 72 6f 6d 20 74 69 63 6b  riving from tick
274a7 65 74 20 23 33 35 30 2e 0a 2a 2a 0a 2a 2a 20 20  et #350..**.**  
274a8 28 31 33 29 20 20 54 68 65 20 73 75 62 71 75 65  (13)  The subque
274a9 72 79 20 61 6e 64 20 6f 75 74 65 72 20 71 75 65  ry and outer que
274aa 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20 75  ry do not both u
274ab 73 65 20 4c 49 4d 49 54 0a 2a 2a 0a 2a 2a 20 20  se LIMIT.**.**  
274ac 28 31 34 29 20 20 54 68 65 20 73 75 62 71 75 65  (14)  The subque
274ad 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20  ry does not use 
274ae 4f 46 46 53 45 54 0a 2a 2a 0a 2a 2a 20 20 28 31  OFFSET.**.**  (1
274af 35 29 20 20 54 68 65 20 6f 75 74 65 72 20 71 75  5)  The outer qu
274b0 65 72 79 20 69 73 20 6e 6f 74 20 70 61 72 74 20  ery is not part 
274b1 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  of a compound se
274b2 6c 65 63 74 20 6f 72 20 74 68 65 0a 2a 2a 20 20  lect or the.**  
274b3 20 20 20 20 20 20 73 75 62 71 75 65 72 79 20 64        subquery d
274b4 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 62 6f 74  oes not have bot
274b5 68 20 61 6e 20 4f 52 44 45 52 20 42 59 20 61 6e  h an ORDER BY an
274b6 64 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  d a LIMIT clause
274b7 2e 0a 2a 2a 20 20 20 20 20 20 20 20 28 53 65 65  ..**        (See
274b8 20 74 69 63 6b 65 74 20 23 32 33 33 39 29 0a 2a   ticket #2339).*
274b9 2a 0a 2a 2a 20 20 28 31 36 29 20 20 54 68 65 20  *.**  (16)  The 
274ba 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e  outer query is n
274bb 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  ot an aggregate 
274bc 6f 72 20 74 68 65 20 73 75 62 71 75 65 72 79 20  or the subquery 
274bd 64 6f 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 6e  does.**        n
274be 6f 74 20 63 6f 6e 74 61 69 6e 20 4f 52 44 45 52  ot contain ORDER
274bf 20 42 59 2e 20 20 28 54 69 63 6b 65 74 20 23 32   BY.  (Ticket #2
274c0 39 34 32 29 20 20 54 68 69 73 20 75 73 65 64 20  942)  This used 
274c1 74 6f 20 6e 6f 74 20 6d 61 74 74 65 72 0a 2a 2a  to not matter.**
274c2 20 20 20 20 20 20 20 20 75 6e 74 69 6c 20 77 65          until we
274c3 20 69 6e 74 72 6f 64 75 63 65 64 20 74 68 65 20   introduced the 
274c4 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28 29 20 66  group_concat() f
274c5 75 6e 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a  unction.  .**.**
274c6 20 20 28 31 37 29 20 20 54 68 65 20 73 75 62 2d    (17)  The sub-
274c7 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20 63  query is not a c
274c8 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20  ompound select, 
274c9 6f 72 20 69 74 20 69 73 20 61 20 55 4e 49 4f 4e  or it is a UNION
274ca 20 41 4c 4c 20 0a 2a 2a 20 20 20 20 20 20 20 20   ALL .**        
274cb 63 6f 6d 70 6f 75 6e 64 20 63 6c 61 75 73 65 20  compound clause 
274cc 6d 61 64 65 20 75 70 20 65 6e 74 69 72 65 6c 79  made up entirely
274cd 20 6f 66 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74   of non-aggregat
274ce 65 20 71 75 65 72 69 65 73 2c 20 61 6e 64 20 0a  e queries, and .
274cf 2a 2a 20 20 20 20 20 20 20 20 74 68 65 20 70 61  **        the pa
274d0 72 65 6e 74 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a  rent query:.**.*
274d1 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69 73 20  *          * is 
274d2 6e 6f 74 20 69 74 73 65 6c 66 20 70 61 72 74 20  not itself part 
274d3 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  of a compound se
274d4 6c 65 63 74 2c 0a 2a 2a 20 20 20 20 20 20 20 20  lect,.**        
274d5 20 20 2a 20 69 73 20 6e 6f 74 20 61 6e 20 61 67    * is not an ag
274d6 67 72 65 67 61 74 65 20 6f 72 20 44 49 53 54 49  gregate or DISTI
274d7 4e 43 54 20 71 75 65 72 79 2c 20 61 6e 64 0a 2a  NCT query, and.*
274d8 2a 20 20 20 20 20 20 20 20 20 20 2a 20 68 61 73  *          * has
274d9 20 6e 6f 20 6f 74 68 65 72 20 74 61 62 6c 65 73   no other tables
274da 20 6f 72 20 73 75 62 2d 73 65 6c 65 63 74 73 20   or sub-selects 
274db 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
274dc 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  se..**.**       
274dd 20 54 68 65 20 70 61 72 65 6e 74 20 61 6e 64 20   The parent and 
274de 73 75 62 2d 71 75 65 72 79 20 6d 61 79 20 63 6f  sub-query may co
274df 6e 74 61 69 6e 20 57 48 45 52 45 20 63 6c 61 75  ntain WHERE clau
274e0 73 65 73 2e 20 53 75 62 6a 65 63 74 20 74 6f 0a  ses. Subject to.
274e1 2a 2a 20 20 20 20 20 20 20 20 72 75 6c 65 73 20  **        rules 
274e2 28 31 31 29 2c 20 28 31 33 29 20 61 6e 64 20 28  (11), (13) and (
274e3 31 34 29 2c 20 74 68 65 79 20 6d 61 79 20 61 6c  14), they may al
274e4 73 6f 20 63 6f 6e 74 61 69 6e 20 4f 52 44 45 52  so contain ORDER
274e5 20 42 59 2c 0a 2a 2a 20 20 20 20 20 20 20 20 4c   BY,.**        L
274e6 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20  IMIT and OFFSET 
274e7 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  clauses..**.**  
274e8 28 31 38 29 20 20 49 66 20 74 68 65 20 73 75 62  (18)  If the sub
274e9 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70  -query is a comp
274ea 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20 74 68 65  ound select, the
274eb 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74  n all terms of t
274ec 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 4f 52 44  he.**        ORD
274ed 45 52 20 62 79 20 63 6c 61 75 73 65 20 6f 66 20  ER by clause of 
274ee 74 68 65 20 70 61 72 65 6e 74 20 6d 75 73 74 20  the parent must 
274ef 62 65 20 73 69 6d 70 6c 65 20 72 65 66 65 72 65  be simple refere
274f0 6e 63 65 73 20 74 6f 20 0a 2a 2a 20 20 20 20 20  nces to .**     
274f1 20 20 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68     columns of th
274f2 65 20 73 75 62 2d 71 75 65 72 79 2e 0a 2a 2a 0a  e sub-query..**.
274f3 2a 2a 20 20 28 31 39 29 20 20 54 68 65 20 73 75  **  (19)  The su
274f4 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
274f5 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65  use LIMIT or the
274f6 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65   outer query doe
274f7 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20  s not.**        
274f8 68 61 76 65 20 61 20 57 48 45 52 45 20 63 6c 61  have a WHERE cla
274f9 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  use..**.** In th
274fa 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 20  is routine, the 
274fb 22 70 22 20 70 61 72 61 6d 65 74 65 72 20 69 73  "p" parameter is
274fc 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
274fd 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 2a  e outer query..*
274fe 2a 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69  * The subquery i
274ff 73 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 46 72  s p->pSrc->a[iFr
27500 6f 6d 5d 2e 20 20 69 73 41 67 67 20 69 73 20 74  om].  isAgg is t
27501 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72  rue if the outer
27502 20 71 75 65 72 79 0a 2a 2a 20 75 73 65 73 20 61   query.** uses a
27503 67 67 72 65 67 61 74 65 73 20 61 6e 64 20 73 75  ggregates and su
27504 62 71 75 65 72 79 49 73 41 67 67 20 69 73 20 74  bqueryIsAgg is t
27505 72 75 65 20 69 66 20 74 68 65 20 73 75 62 71 75  rue if the subqu
27506 65 72 79 20 75 73 65 73 20 61 67 67 72 65 67 61  ery uses aggrega
27507 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 66 6c  tes..**.** If fl
27508 61 74 74 65 6e 69 6e 67 20 69 73 20 6e 6f 74 20  attening is not 
27509 61 74 74 65 6d 70 74 65 64 2c 20 74 68 69 73 20  attempted, this 
2750a 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
2750b 6f 70 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30  op and returns 0
2750c 2e 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e 69  ..** If flatteni
2750d 6e 67 20 69 73 20 61 74 74 65 6d 70 74 65 64 20  ng is attempted 
2750e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
2750f 75 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c  urns 1..**.** Al
27510 6c 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  l of the express
27511 69 6f 6e 20 61 6e 61 6c 79 73 69 73 20 6d 75 73  ion analysis mus
27512 74 20 6f 63 63 75 72 20 6f 6e 20 62 6f 74 68 20  t occur on both 
27513 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
27514 61 6e 64 0a 2a 2a 20 74 68 65 20 73 75 62 71 75  and.** the subqu
27515 65 72 79 20 62 65 66 6f 72 65 20 74 68 69 73 20  ery before this 
27516 72 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0a 2a 2f  routine runs..*/
27517 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61 74  .static int flat
27518 74 65 6e 53 75 62 71 75 65 72 79 28 0a 20 20 50  tenSubquery(.  P
27519 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2751a 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2751b 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
2751c 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
2751d 20 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20 6f   /* The parent o
2751e 72 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20 73  r outer SELECT s
2751f 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e  tatement */.  in
27520 74 20 69 46 72 6f 6d 2c 20 20 20 20 20 20 20 20  t iFrom,        
27521 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70     /* Index in p
27522 2d 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 74  ->pSrc->a[] of t
27523 68 65 20 69 6e 6e 65 72 20 73 75 62 71 75 65 72  he inner subquer
27524 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41 67 67  y */.  int isAgg
27525 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
27526 72 75 65 20 69 66 20 6f 75 74 65 72 20 53 45 4c  rue if outer SEL
27527 45 43 54 20 75 73 65 73 20 61 67 67 72 65 67 61  ECT uses aggrega
27528 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  te functions */.
27529 20 20 69 6e 74 20 73 75 62 71 75 65 72 79 49 73    int subqueryIs
2752a 41 67 67 20 20 20 20 2f 2a 20 54 72 75 65 20 69  Agg    /* True i
2752b 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 75  f the subquery u
2752c 73 65 73 20 61 67 67 72 65 67 61 74 65 20 66 75  ses aggregate fu
2752d 6e 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20  nctions */.){.  
2752e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76  const char *zSav
2752f 65 64 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20  edAuthContext = 
27530 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
27531 74 65 78 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a  text;.  Select *
27532 70 50 61 72 65 6e 74 3b 0a 20 20 53 65 6c 65 63  pParent;.  Selec
27533 74 20 2a 70 53 75 62 3b 20 20 20 20 20 20 20 2f  t *pSub;       /
27534 2a 20 54 68 65 20 69 6e 6e 65 72 20 71 75 65 72  * The inner quer
27535 79 20 6f 72 20 22 73 75 62 71 75 65 72 79 22 20  y or "subquery" 
27536 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75  */.  Select *pSu
27537 62 31 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  b1;      /* Poin
27538 74 65 72 20 74 6f 20 74 68 65 20 72 69 67 68 74  ter to the right
27539 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e 20 73  most select in s
2753a 75 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20 53 72  ub-query */.  Sr
2753b 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20  cList *pSrc;    
2753c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
2753d 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65  ause of the oute
2753e 72 20 71 75 65 72 79 20 2a 2f 0a 20 20 53 72 63  r query */.  Src
2753f 4c 69 73 74 20 2a 70 53 75 62 53 72 63 3b 20 20  List *pSubSrc;  
27540 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61   /* The FROM cla
27541 75 73 65 20 6f 66 20 74 68 65 20 73 75 62 71 75  use of the subqu
27542 65 72 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ery */.  ExprLis
27543 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20  t *pList;    /* 
27544 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  The result set o
27545 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  f the outer quer
27546 79 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65  y */.  int iPare
27547 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 44  nt;        /* VD
27548 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE cursor number
27549 20 6f 66 20 74 68 65 20 70 53 75 62 20 72 65 73   of the pSub res
2754a 75 6c 74 20 73 65 74 20 74 65 6d 70 20 74 61 62  ult set temp tab
2754b 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  le */.  int i;  
2754c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2754d 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
2754e 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20   Expr *pWhere;  
2754f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27550 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
27551 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63  lause */.  struc
27552 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
27553 70 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a 20 54  pSubitem;   /* T
27554 68 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20  he subquery */. 
27555 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
27556 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a  Parse->db;..  /*
27557 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
27558 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 70   flattening is p
27559 65 72 6d 69 74 74 65 64 2e 20 20 52 65 74 75 72  ermitted.  Retur
2755a 6e 20 30 20 69 66 20 6e 6f 74 2e 0a 20 20 2a 2f  n 0 if not..  */
2755b 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
2755c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
2755d 70 50 72 69 6f 72 3d 3d 30 20 29 3b 20 20 2f 2a  pPrior==0 );  /*
2755e 20 55 6e 61 62 6c 65 20 74 6f 20 66 6c 61 74 74   Unable to flatt
2755f 65 6e 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72  en compound quer
27560 69 65 73 20 2a 2f 0a 20 20 70 53 72 63 20 3d 20  ies */.  pSrc = 
27561 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72  p->pSrc;.  asser
27562 74 28 20 70 53 72 63 20 26 26 20 69 46 72 6f 6d  t( pSrc && iFrom
27563 3e 3d 30 20 26 26 20 69 46 72 6f 6d 3c 70 53 72  >=0 && iFrom<pSr
27564 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 70 53 75  c->nSrc );.  pSu
27565 62 69 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61  bitem = &pSrc->a
27566 5b 69 46 72 6f 6d 5d 3b 0a 20 20 69 50 61 72 65  [iFrom];.  iPare
27567 6e 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 69  nt = pSubitem->i
27568 43 75 72 73 6f 72 3b 0a 20 20 70 53 75 62 20 3d  Cursor;.  pSub =
27569 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65   pSubitem->pSele
2756a 63 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53  ct;.  assert( pS
2756b 75 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69  ub!=0 );.  if( i
2756c 73 41 67 67 20 26 26 20 73 75 62 71 75 65 72 79  sAgg && subquery
2756d 49 73 41 67 67 20 29 20 72 65 74 75 72 6e 20 30  IsAgg ) return 0
2756e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2756f 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
27570 20 28 31 29 20 20 2a 2f 0a 20 20 69 66 28 20 73   (1)  */.  if( s
27571 75 62 71 75 65 72 79 49 73 41 67 67 20 26 26 20  ubqueryIsAgg && 
27572 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 29 20 72  pSrc->nSrc>1 ) r
27573 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
27574 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
27575 20 28 32 29 20 20 2a 2f 0a 20 20 70 53 75 62 53   (2)  */.  pSubS
27576 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b  rc = pSub->pSrc;
27577 0a 20 20 61 73 73 65 72 74 28 20 70 53 75 62 53  .  assert( pSubS
27578 72 63 20 29 3b 0a 20 20 2f 2a 20 50 72 69 6f 72  rc );.  /* Prior
27579 20 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e 31 2e   to version 3.1.
2757a 32 2c 20 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e  2, when LIMIT an
2757b 64 20 4f 46 46 53 45 54 20 68 61 64 20 74 6f 20  d OFFSET had to 
2757c 62 65 20 73 69 6d 70 6c 65 20 63 6f 6e 73 74 61  be simple consta
2757d 6e 74 73 2c 0a 20 20 2a 2a 20 6e 6f 74 20 61 72  nts,.  ** not ar
2757e 62 69 74 72 61 72 79 20 65 78 70 72 65 73 73 73  bitrary expresss
2757f 69 6f 6e 73 2c 20 77 65 20 61 6c 6c 6f 77 65 64  ions, we allowed
27580 20 73 6f 6d 65 20 63 6f 6d 62 69 6e 69 6e 67 20   some combining 
27581 6f 66 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46  of LIMIT and OFF
27582 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 75 73 65  SET.  ** because
27583 20 74 68 65 79 20 63 6f 75 6c 64 20 62 65 20 63   they could be c
27584 6f 6d 70 75 74 65 64 20 61 74 20 63 6f 6d 70 69  omputed at compi
27585 6c 65 2d 74 69 6d 65 2e 20 20 42 75 74 20 77 68  le-time.  But wh
27586 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46  en LIMIT and OFF
27587 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 6d 65 20  SET.  ** became 
27588 61 72 62 69 74 72 61 72 79 20 65 78 70 72 65 73  arbitrary expres
27589 73 69 6f 6e 73 2c 20 77 65 20 77 65 72 65 20 66  sions, we were f
2758a 6f 72 63 65 64 20 74 6f 20 61 64 64 20 72 65 73  orced to add res
2758b 74 72 69 63 74 69 6f 6e 73 20 28 31 33 29 0a 20  trictions (13). 
2758c 20 2a 2a 20 61 6e 64 20 28 31 34 29 2e 20 2a 2f   ** and (14). */
2758d 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69  .  if( pSub->pLi
2758e 6d 69 74 20 26 26 20 70 2d 3e 70 4c 69 6d 69 74  mit && p->pLimit
2758f 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
27590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
27591 74 72 69 63 74 69 6f 6e 20 28 31 33 29 20 2a 2f  triction (13) */
27592 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f 66  .  if( pSub->pOf
27593 66 73 65 74 20 29 20 72 65 74 75 72 6e 20 30 3b  fset ) return 0;
27594 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27595 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
27596 74 72 69 63 74 69 6f 6e 20 28 31 34 29 20 2a 2f  triction (14) */
27597 0a 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74  .  if( p->pRight
27598 6d 6f 73 74 20 26 26 20 70 53 75 62 2d 3e 70 4c  most && pSub->pL
27599 69 6d 69 74 20 26 26 20 70 53 75 62 2d 3e 70 4f  imit && pSub->pO
2759a 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65  rderBy ){.    re
2759b 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
2759c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2759d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2759e 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
2759f 6e 20 28 31 35 29 20 2a 2f 0a 20 20 7d 0a 20 20  n (15) */.  }.  
275a0 69 66 28 20 70 53 75 62 53 72 63 2d 3e 6e 53 72  if( pSubSrc->nSr
275a1 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  c==0 ) return 0;
275a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275a3 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
275a4 63 74 69 6f 6e 20 28 37 29 20 20 2a 2f 0a 20 20  ction (7)  */.  
275a5 69 66 28 20 28 28 70 53 75 62 2d 3e 73 65 6c 46  if( ((pSub->selF
275a6 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e  lags & SF_Distin
275a7 63 74 29 21 3d 30 20 7c 7c 20 70 53 75 62 2d 3e  ct)!=0 || pSub->
275a8 70 4c 69 6d 69 74 29 20 0a 20 20 20 20 20 20 20  pLimit) .       
275a9 20 20 26 26 20 28 70 53 72 63 2d 3e 6e 53 72 63    && (pSrc->nSrc
275aa 3e 31 20 7c 7c 20 69 73 41 67 67 29 20 29 7b 20  >1 || isAgg) ){ 
275ab 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
275ac 72 69 63 74 69 6f 6e 73 20 28 34 29 28 35 29 28  rictions (4)(5)(
275ad 38 29 28 39 29 20 2a 2f 0a 20 20 20 20 20 72 65  8)(9) */.     re
275ae 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 0a 20  turn 0;       . 
275af 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c   }.  if( (p->sel
275b0 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69  Flags & SF_Disti
275b1 6e 63 74 29 21 3d 30 20 26 26 20 73 75 62 71 75  nct)!=0 && subqu
275b2 65 72 79 49 73 41 67 67 20 29 7b 0a 20 20 20 20  eryIsAgg ){.    
275b3 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
275b4 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
275b5 6e 20 28 36 29 20 20 2a 2f 0a 20 20 7d 0a 20 20  n (6)  */.  }.  
275b6 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  if( p->pOrderBy 
275b7 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42  && pSub->pOrderB
275b8 79 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72 6e  y ){.     return
275b9 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
275ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
275bc 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 31   Restriction (11
275bd 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 69  ) */.  }.  if( i
275be 73 41 67 67 20 26 26 20 70 53 75 62 2d 3e 70 4f  sAgg && pSub->pO
275bf 72 64 65 72 42 79 20 29 20 72 65 74 75 72 6e 20  rderBy ) return 
275c0 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
275c1 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
275c2 20 28 31 36 29 20 2a 2f 0a 20 20 69 66 28 20 70   (16) */.  if( p
275c3 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70  Sub->pLimit && p
275c4 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72  ->pWhere ) retur
275c5 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
275c6 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
275c7 20 28 31 39 29 20 2a 2f 0a 0a 20 20 2f 2a 20 4f   (19) */..  /* O
275c8 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 20  BSOLETE COMMENT 
275c9 31 3a 0a 20 20 2a 2a 20 52 65 73 74 72 69 63 74  1:.  ** Restrict
275ca 69 6f 6e 20 33 3a 20 20 49 66 20 74 68 65 20 73  ion 3:  If the s
275cb 75 62 71 75 65 72 79 20 69 73 20 61 20 6a 6f 69  ubquery is a joi
275cc 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  n, make sure the
275cd 20 73 75 62 71 75 65 72 79 20 69 73 20 0a 20 20   subquery is .  
275ce 2a 2a 20 6e 6f 74 20 75 73 65 64 20 61 73 20 74  ** not used as t
275cf 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64  he right operand
275d0 20 6f 66 20 61 6e 20 6f 75 74 65 72 20 6a 6f 69   of an outer joi
275d1 6e 2e 20 20 45 78 61 6d 70 6c 65 73 20 6f 66 20  n.  Examples of 
275d2 77 68 79 20 74 68 69 73 0a 20 20 2a 2a 20 69 73  why this.  ** is
275d3 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20 20   not allowed:.  
275d4 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  **.  **         
275d5 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f  t1 LEFT OUTER JO
275d6 49 4e 20 28 74 32 20 4a 4f 49 4e 20 74 33 29 0a  IN (t2 JOIN t3).
275d7 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65 20    **.  ** If we 
275d8 66 6c 61 74 74 65 6e 20 74 68 65 20 61 62 6f 76  flatten the abov
275d9 65 2c 20 77 65 20 77 6f 75 6c 64 20 67 65 74 0a  e, we would get.
275da 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
275db 20 20 28 74 31 20 4c 45 46 54 20 4f 55 54 45 52    (t1 LEFT OUTER
275dc 20 4a 4f 49 4e 20 74 32 29 20 4a 4f 49 4e 20 74   JOIN t2) JOIN t
275dd 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20 77 68 69 63  3.  **.  ** whic
275de 68 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20  h is not at all 
275df 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e 0a  the same thing..
275e0 20 20 2a 2a 0a 20 20 2a 2a 20 4f 42 53 4f 4c 45    **.  ** OBSOLE
275e1 54 45 20 43 4f 4d 4d 45 4e 54 20 32 3a 0a 20 20  TE COMMENT 2:.  
275e2 2a 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31  ** Restriction 1
275e3 32 3a 20 20 49 66 20 74 68 65 20 73 75 62 71 75  2:  If the subqu
275e4 65 72 79 20 69 73 20 74 68 65 20 72 69 67 68 74  ery is the right
275e5 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 6c 65   operand of a le
275e6 66 74 20 6f 75 74 65 72 0a 20 20 2a 2a 20 6a 6f  ft outer.  ** jo
275e7 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68  in, make sure th
275e8 65 20 73 75 62 71 75 65 72 79 20 68 61 73 20 6e  e subquery has n
275e9 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a  o WHERE clause..
275ea 20 20 2a 2a 20 41 6e 20 65 78 61 6d 70 6c 65 73    ** An examples
275eb 20 6f 66 20 77 68 79 20 74 68 69 73 20 69 73 20   of why this is 
275ec 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a  not allowed:.  *
275ed 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74  *.  **         t
275ee 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  1 LEFT OUTER JOI
275ef 4e 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  N (SELECT * FROM
275f0 20 74 32 20 57 48 45 52 45 20 74 32 2e 78 3e 30   t2 WHERE t2.x>0
275f1 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77  ).  **.  ** If w
275f2 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61 62  e flatten the ab
275f3 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67 65  ove, we would ge
275f4 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  t.  **.  **     
275f5 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f 55 54      (t1 LEFT OUT
275f6 45 52 20 4a 4f 49 4e 20 74 32 29 20 57 48 45 52  ER JOIN t2) WHER
275f7 45 20 74 32 2e 78 3e 30 0a 20 20 2a 2a 0a 20 20  E t2.x>0.  **.  
275f8 2a 2a 20 42 75 74 20 74 68 65 20 74 32 2e 78 3e  ** But the t2.x>
275f9 30 20 74 65 73 74 20 77 69 6c 6c 20 61 6c 77 61  0 test will alwa
275fa 79 73 20 66 61 69 6c 20 6f 6e 20 61 20 4e 55 4c  ys fail on a NUL
275fb 4c 20 72 6f 77 20 6f 66 20 74 32 2c 20 77 68 69  L row of t2, whi
275fc 63 68 0a 20 20 2a 2a 20 65 66 66 65 63 74 69 76  ch.  ** effectiv
275fd 65 6c 79 20 63 6f 6e 76 65 72 74 73 20 74 68 65  ely converts the
275fe 20 4f 55 54 45 52 20 4a 4f 49 4e 20 69 6e 74 6f   OUTER JOIN into
275ff 20 61 6e 20 49 4e 4e 45 52 20 4a 4f 49 4e 2e 0a   an INNER JOIN..
27600 20 20 2a 2a 0a 20 20 2a 2a 20 54 48 49 53 20 4f    **.  ** THIS O
27601 56 45 52 52 49 44 45 53 20 4f 42 53 4f 4c 45 54  VERRIDES OBSOLET
27602 45 20 43 4f 4d 4d 45 4e 54 53 20 31 20 41 4e 44  E COMMENTS 1 AND
27603 20 32 20 41 42 4f 56 45 3a 0a 20 20 2a 2a 20 54   2 ABOVE:.  ** T
27604 69 63 6b 65 74 20 23 33 33 30 30 20 73 68 6f 77  icket #3300 show
27605 73 20 74 68 61 74 20 66 6c 61 74 74 65 6e 69 6e  s that flattenin
27606 67 20 74 68 65 20 72 69 67 68 74 20 74 65 72 6d  g the right term
27607 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 0a   of a LEFT JOIN.
27608 20 20 2a 2a 20 69 73 20 66 72 61 75 67 68 74 20    ** is fraught 
27609 77 69 74 68 20 64 61 6e 67 65 72 2e 20 20 42 65  with danger.  Be
2760a 73 74 20 74 6f 20 61 76 6f 69 64 20 74 68 65 20  st to avoid the 
2760b 77 68 6f 6c 65 20 74 68 69 6e 67 2e 20 20 49 66  whole thing.  If
2760c 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65   the.  ** subque
2760d 72 79 20 69 73 20 74 68 65 20 72 69 67 68 74 20  ry is the right 
2760e 74 65 72 6d 20 6f 66 20 61 20 4c 45 46 54 20 4a  term of a LEFT J
2760f 4f 49 4e 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74  OIN, then do not
27610 20 66 6c 61 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20   flatten..  */. 
27611 20 69 66 28 20 28 70 53 75 62 69 74 65 6d 2d 3e   if( (pSubitem->
27612 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4f 55  jointype & JT_OU
27613 54 45 52 29 21 3d 30 20 29 7b 0a 20 20 20 20 72  TER)!=0 ){.    r
27614 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
27615 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31  /* Restriction 1
27616 37 3a 20 49 66 20 74 68 65 20 73 75 62 2d 71 75  7: If the sub-qu
27617 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e  ery is a compoun
27618 64 20 53 45 4c 45 43 54 2c 20 74 68 65 6e 20 69  d SELECT, then i
27619 74 20 6d 75 73 74 0a 20 20 2a 2a 20 75 73 65 20  t must.  ** use 
2761a 6f 6e 6c 79 20 74 68 65 20 55 4e 49 4f 4e 20 41  only the UNION A
2761b 4c 4c 20 6f 70 65 72 61 74 6f 72 2e 20 41 6e 64  LL operator. And
2761c 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 69 6d   none of the sim
2761d 70 6c 65 20 73 65 6c 65 63 74 20 71 75 65 72 69  ple select queri
2761e 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 6d 61 6b  es.  ** that mak
2761f 65 20 75 70 20 74 68 65 20 63 6f 6d 70 6f 75 6e  e up the compoun
27620 64 20 53 45 4c 45 43 54 20 61 72 65 20 61 6c 6c  d SELECT are all
27621 6f 77 65 64 20 74 6f 20 62 65 20 61 67 67 72 65  owed to be aggre
27622 67 61 74 65 20 6f 72 20 64 69 73 74 69 6e 63 74  gate or distinct
27623 0a 20 20 2a 2a 20 71 75 65 72 69 65 73 2e 0a 20  .  ** queries.. 
27624 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e   */.  if( pSub->
27625 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 69 66  pPrior ){.    if
27626 28 20 69 73 41 67 67 20 7c 7c 20 28 70 2d 3e 73  ( isAgg || (p->s
27627 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73  elFlags & SF_Dis
27628 74 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53 72  tinct)!=0 || pSr
27629 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a 20 20  c->nSrc!=1 ){.  
2762a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2762b 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53 75 62    }.    for(pSub
2762c 31 3d 70 53 75 62 3b 20 70 53 75 62 31 3b 20 70  1=pSub; pSub1; p
2762d 53 75 62 31 3d 70 53 75 62 31 2d 3e 70 50 72 69  Sub1=pSub1->pPri
2762e 6f 72 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28  or){.      if( (
2762f 70 53 75 62 31 2d 3e 73 65 6c 46 6c 61 67 73 20  pSub1->selFlags 
27630 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53  & (SF_Distinct|S
27631 46 5f 41 67 67 72 65 67 61 74 65 29 29 21 3d 30  F_Aggregate))!=0
27632 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 53 75 62  .       || (pSub
27633 31 2d 3e 70 50 72 69 6f 72 20 26 26 20 70 53 75  1->pPrior && pSu
27634 62 31 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20  b1->op!=TK_ALL) 
27635 0a 20 20 20 20 20 20 20 7c 7c 20 21 70 53 75 62  .       || !pSub
27636 31 2d 3e 70 53 72 63 20 7c 7c 20 70 53 75 62 31  1->pSrc || pSub1
27637 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 0a  ->pSrc->nSrc!=1.
27638 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
27639 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
2763a 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
2763b 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31 38 2e   Restriction 18.
2763c 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70   */.    if( p->p
2763d 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2763e 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 66   int ii;.      f
2763f 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 2d 3e 70  or(ii=0; ii<p->p
27640 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20  OrderBy->nExpr; 
27641 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  ii++){.        i
27642 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e  f( p->pOrderBy->
27643 61 5b 69 69 5d 2e 69 43 6f 6c 3d 3d 30 20 29 20  a[ii].iCol==0 ) 
27644 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
27645 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
27646 2a 2a 2a 2a 2a 20 49 66 20 77 65 20 72 65 61 63  ***** If we reac
27647 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 66 6c  h this point, fl
27648 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d  attening is perm
27649 69 74 74 65 64 2e 20 2a 2a 2a 2a 2a 2f 0a 0a 20  itted. *****/.. 
2764a 20 2f 2a 20 41 75 74 68 6f 72 69 7a 65 20 74 68   /* Authorize th
2764b 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20  e subquery */.  
2764c 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
2764d 74 65 78 74 20 3d 20 70 53 75 62 69 74 65 6d 2d  text = pSubitem-
2764e 3e 7a 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65  >zName;.  sqlite
2764f 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
27650 65 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54  e, SQLITE_SELECT
27651 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 70 50  , 0, 0, 0);.  pP
27652 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
27653 78 74 20 3d 20 7a 53 61 76 65 64 41 75 74 68 43  xt = zSavedAuthC
27654 6f 6e 74 65 78 74 3b 0a 0a 20 20 2f 2a 20 49 66  ontext;..  /* If
27655 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69   the sub-query i
27656 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  s a compound SEL
27657 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  ECT statement, t
27658 68 65 6e 20 28 62 79 20 72 65 73 74 72 69 63 74  hen (by restrict
27659 69 6f 6e 73 0a 20 20 2a 2a 20 31 37 20 61 6e 64  ions.  ** 17 and
2765a 20 31 38 20 61 62 6f 76 65 29 20 69 74 20 6d 75   18 above) it mu
2765b 73 74 20 62 65 20 61 20 55 4e 49 4f 4e 20 41 4c  st be a UNION AL
2765c 4c 20 61 6e 64 20 74 68 65 20 70 61 72 65 6e 74  L and the parent
2765d 20 71 75 65 72 79 20 6d 75 73 74 20 0a 20 20 2a   query must .  *
2765e 2a 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  * be of the form
2765f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
27660 53 45 4c 45 43 54 20 3c 65 78 70 72 2d 6c 69 73  SELECT <expr-lis
27661 74 3e 20 46 52 4f 4d 20 28 3c 73 75 62 2d 71 75  t> FROM (<sub-qu
27662 65 72 79 3e 29 20 3c 77 68 65 72 65 2d 63 6c 61  ery>) <where-cla
27663 75 73 65 3e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  use> .  **.  ** 
27664 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 79 20  followed by any 
27665 4f 52 44 45 52 20 42 59 2c 20 4c 49 4d 49 54 20  ORDER BY, LIMIT 
27666 61 6e 64 2f 6f 72 20 4f 46 46 53 45 54 20 63 6c  and/or OFFSET cl
27667 61 75 73 65 73 2e 20 54 68 69 73 20 62 6c 6f 63  auses. This bloc
27668 6b 0a 20 20 2a 2a 20 63 72 65 61 74 65 73 20 4e  k.  ** creates N
27669 2d 31 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65  -1 copies of the
2766a 20 70 61 72 65 6e 74 20 71 75 65 72 79 20 77 69   parent query wi
2766b 74 68 6f 75 74 20 61 6e 79 20 4f 52 44 45 52 20  thout any ORDER 
2766c 42 59 2c 20 4c 49 4d 49 54 20 6f 72 20 0a 20 20  BY, LIMIT or .  
2766d 2a 2a 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65  ** OFFSET clause
2766e 73 20 61 6e 64 20 6a 6f 69 6e 73 20 74 68 65 6d  s and joins them
2766f 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 68 61 6e   to the left-han
27670 64 2d 73 69 64 65 20 6f 66 20 74 68 65 20 6f 72  d-side of the or
27671 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 75 73 69 6e  iginal.  ** usin
27672 67 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72  g UNION ALL oper
27673 61 74 6f 72 73 2e 20 49 6e 20 74 68 69 73 20 63  ators. In this c
27674 61 73 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  ase N is the num
27675 62 65 72 20 6f 66 20 73 69 6d 70 6c 65 0a 20 20  ber of simple.  
27676 2a 2a 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d  ** select statem
27677 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70  ents in the comp
27678 6f 75 6e 64 20 73 75 62 2d 71 75 65 72 79 2e 0a  ound sub-query..
27679 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 61 6d 70 6c    **.  ** Exampl
2767a 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  e:.  **.  **    
2767b 20 53 45 4c 45 43 54 20 61 2b 31 20 46 52 4f 4d   SELECT a+1 FROM
2767c 20 28 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53   (.  **        S
2767d 45 4c 45 43 54 20 78 20 46 52 4f 4d 20 74 61 62  ELECT x FROM tab
2767e 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49  .  **        UNI
2767f 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20  ON ALL.  **     
27680 20 20 20 53 45 4c 45 43 54 20 79 20 46 52 4f 4d     SELECT y FROM
27681 20 74 61 62 0a 20 20 2a 2a 20 20 20 20 20 20 20   tab.  **       
27682 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20   UNION ALL.  ** 
27683 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 62         SELECT ab
27684 73 28 7a 2a 32 29 20 46 52 4f 4d 20 74 61 62 32  s(z*2) FROM tab2
27685 0a 20 20 2a 2a 20 20 20 20 20 29 20 57 48 45 52  .  **     ) WHER
27686 45 20 61 21 3d 35 20 4f 52 44 45 52 20 42 59 20  E a!=5 ORDER BY 
27687 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 72 61 6e  1.  **.  ** Tran
27688 73 66 6f 72 6d 65 64 20 69 6e 74 6f 3a 0a 20 20  sformed into:.  
27689 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  **.  **     SELE
2768a 43 54 20 78 2b 31 20 46 52 4f 4d 20 74 61 62 20  CT x+1 FROM tab 
2768b 57 48 45 52 45 20 78 2b 31 21 3d 35 0a 20 20 2a  WHERE x+1!=5.  *
2768c 2a 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a  *     UNION ALL.
2768d 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20    **     SELECT 
2768e 79 2b 31 20 46 52 4f 4d 20 74 61 62 20 57 48 45  y+1 FROM tab WHE
2768f 52 45 20 79 2b 31 21 3d 35 0a 20 20 2a 2a 20 20  RE y+1!=5.  **  
27690 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a     UNION ALL.  *
27691 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 62 73  *     SELECT abs
27692 28 7a 2a 32 29 2b 31 20 46 52 4f 4d 20 74 61 62  (z*2)+1 FROM tab
27693 32 20 57 48 45 52 45 20 61 62 73 28 7a 2a 32 29  2 WHERE abs(z*2)
27694 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 4f  +1!=5.  **     O
27695 52 44 45 52 20 42 59 20 31 0a 20 20 2a 2a 0a 20  RDER BY 1.  **. 
27696 20 2a 2a 20 57 65 20 63 61 6c 6c 20 74 68 69 73   ** We call this
27697 20 74 68 65 20 22 63 6f 6d 70 6f 75 6e 64 2d 73   the "compound-s
27698 75 62 71 75 65 72 79 20 66 6c 61 74 74 65 6e 69  ubquery flatteni
27699 6e 67 22 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ng"..  */.  for(
2769a 70 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f  pSub=pSub->pPrio
2769b 72 3b 20 70 53 75 62 3b 20 70 53 75 62 3d 70 53  r; pSub; pSub=pS
2769c 75 62 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20  ub->pPrior){.   
2769d 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20   Select *pNew;. 
2769e 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72     ExprList *pOr
2769f 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65  derBy = p->pOrde
276a0 72 42 79 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  rBy;.    Expr *p
276a1 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
276a2 74 3b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70  t;.    Select *p
276a3 50 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f  Prior = p->pPrio
276a4 72 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72  r;.    p->pOrder
276a5 42 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70  By = 0;.    p->p
276a6 53 72 63 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e  Src = 0;.    p->
276a7 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pPrior = 0;.    
276a8 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  p->pLimit = 0;. 
276a9 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
276aa 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
276ab 29 3b 0a 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74  );.    p->pLimit
276ac 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 70   = pLimit;.    p
276ad 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72  ->pOrderBy = pOr
276ae 64 65 72 42 79 3b 0a 20 20 20 20 70 2d 3e 70 53  derBy;.    p->pS
276af 72 63 20 3d 20 70 53 72 63 3b 0a 20 20 20 20 70  rc = pSrc;.    p
276b0 2d 3e 6f 70 20 3d 20 54 4b 5f 41 4c 4c 3b 0a 20  ->op = TK_ALL;. 
276b1 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74     p->pRightmost
276b2 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 4e   = 0;.    if( pN
276b3 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  ew==0 ){.      p
276b4 4e 65 77 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20  New = pPrior;.  
276b5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
276b6 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50  New->pPrior = pP
276b7 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 4e 65 77  rior;.      pNew
276b8 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30  ->pRightmost = 0
276b9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70  ;.    }.    p->p
276ba 50 72 69 6f 72 20 3d 20 70 4e 65 77 3b 0a 20 20  Prior = pNew;.  
276bb 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
276bc 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20  Failed ) return 
276bd 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67  1;.  }..  /* Beg
276be 69 6e 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68  in flattening th
276bf 65 20 69 46 72 6f 6d 2d 74 68 20 65 6e 74 72 79  e iFrom-th entry
276c0 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
276c1 75 73 65 20 0a 20 20 2a 2a 20 69 6e 20 74 68 65  use .  ** in the
276c2 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20   outer query..  
276c3 2a 2f 0a 20 20 70 53 75 62 20 3d 20 70 53 75 62  */.  pSub = pSub
276c4 31 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53  1 = pSubitem->pS
276c5 65 6c 65 63 74 3b 0a 0a 20 20 2f 2a 20 44 65 6c  elect;..  /* Del
276c6 65 74 65 20 74 68 65 20 74 72 61 6e 73 69 65 6e  ete the transien
276c7 74 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72  t table structur
276c8 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
276c9 68 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75  h the.  ** subqu
276ca 65 72 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ery.  */.  sqlit
276cb 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53 75  e3DbFree(db, pSu
276cc 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  bitem->zDatabase
276cd 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
276ce 65 65 28 64 62 2c 20 70 53 75 62 69 74 65 6d 2d  ee(db, pSubitem-
276cf 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  >zName);.  sqlit
276d0 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53 75  e3DbFree(db, pSu
276d1 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a  bitem->zAlias);.
276d2 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74    pSubitem->zDat
276d3 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 70 53 75  abase = 0;.  pSu
276d4 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30  bitem->zName = 0
276d5 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41  ;.  pSubitem->zA
276d6 6c 69 61 73 20 3d 20 30 3b 0a 20 20 70 53 75 62  lias = 0;.  pSub
276d7 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20  item->pSelect = 
276d8 30 3b 0a 0a 20 20 2f 2a 20 44 65 66 65 72 20 64  0;..  /* Defer d
276d9 65 6c 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c  eleting the Tabl
276da 65 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61  e object associa
276db 74 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a  ted with the.  *
276dc 2a 20 73 75 62 71 75 65 72 79 20 75 6e 74 69 6c  * subquery until
276dd 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e   code generation
276de 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 70 6c 65 74   is.  ** complet
276df 65 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 6d  e, since there m
276e0 61 79 20 73 74 69 6c 6c 20 65 78 69 73 74 20 45  ay still exist E
276e1 78 70 72 2e 70 54 61 62 20 65 6e 74 72 69 65 73  xpr.pTab entries
276e2 20 74 68 61 74 0a 20 20 2a 2a 20 72 65 66 65 72   that.  ** refer
276e3 20 74 6f 20 74 68 65 20 73 75 62 71 75 65 72 79   to the subquery
276e4 20 65 76 65 6e 20 61 66 74 65 72 20 66 6c 61 74   even after flat
276e5 74 65 6e 69 6e 67 2e 20 20 54 69 63 6b 65 74 20  tening.  Ticket 
276e6 23 33 33 34 36 2e 0a 20 20 2a 2f 0a 20 20 69 66  #3346..  */.  if
276e7 28 20 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62  ( pSubitem->pTab
276e8 21 3d 30 20 29 7b 0a 20 20 20 20 54 61 62 6c 65  !=0 ){.    Table
276e9 20 2a 70 54 61 62 54 6f 44 65 6c 20 3d 20 70 53   *pTabToDel = pS
276ea 75 62 69 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20  ubitem->pTab;.  
276eb 20 20 69 66 28 20 70 54 61 62 54 6f 44 65 6c 2d    if( pTabToDel-
276ec 3e 6e 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20  >nRef==1 ){.    
276ed 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e 70 4e 65    pTabToDel->pNe
276ee 78 74 5a 6f 6d 62 69 65 20 3d 20 70 50 61 72 73  xtZombie = pPars
276ef 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0a 20  e->pZombieTab;. 
276f0 20 20 20 20 20 70 50 61 72 73 65 2d 3e 70 5a 6f       pParse->pZo
276f1 6d 62 69 65 54 61 62 20 3d 20 70 54 61 62 54 6f  mbieTab = pTabTo
276f2 44 65 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  Del;.    }else{.
276f3 20 20 20 20 20 20 70 54 61 62 54 6f 44 65 6c 2d        pTabToDel-
276f4 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 7d 0a 20  >nRef--;.    }. 
276f5 20 20 20 70 53 75 62 69 74 65 6d 2d 3e 70 54 61     pSubitem->pTa
276f6 62 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  b = 0;.  }..  /*
276f7 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c   The following l
276f8 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f  oop runs once fo
276f9 72 20 65 61 63 68 20 74 65 72 6d 20 69 6e 20 61  r each term in a
276fa 20 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65   compound-subque
276fb 72 79 0a 20 20 2a 2a 20 66 6c 61 74 74 65 6e 69  ry.  ** flatteni
276fc 6e 67 20 28 61 73 20 64 65 73 63 72 69 62 65 64  ng (as described
276fd 20 61 62 6f 76 65 29 2e 20 20 49 66 20 77 65 20   above).  If we 
276fe 61 72 65 20 64 6f 69 6e 67 20 61 20 64 69 66 66  are doing a diff
276ff 65 72 65 6e 74 20 6b 69 6e 64 0a 20 20 2a 2a 20  erent kind.  ** 
27700 6f 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 2d 20  of flattening - 
27701 61 20 66 6c 61 74 74 65 6e 69 6e 67 20 6f 74 68  a flattening oth
27702 65 72 20 74 68 61 6e 20 61 20 63 6f 6d 70 6f 75  er than a compou
27703 6e 64 2d 73 75 62 71 75 65 72 79 20 66 6c 61 74  nd-subquery flat
27704 74 65 6e 69 6e 67 20 2d 0a 20 20 2a 2a 20 74 68  tening -.  ** th
27705 65 6e 20 74 68 69 73 20 6c 6f 6f 70 20 6f 6e 6c  en this loop onl
27706 79 20 72 75 6e 73 20 6f 6e 63 65 2e 0a 20 20 2a  y runs once..  *
27707 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70  *.  ** This loop
27708 20 6d 6f 76 65 73 20 61 6c 6c 20 6f 66 20 74 68   moves all of th
27709 65 20 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 73 20  e FROM elements 
2770a 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  of the subquery 
2770b 69 6e 74 6f 20 74 68 65 0a 20 20 2a 2a 20 74 68  into the.  ** th
2770c 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
2770d 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2770e 2e 20 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20  .  Before doing 
2770f 74 68 69 73 2c 20 72 65 6d 65 6d 62 65 72 0a 20  this, remember. 
27710 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 6e   ** the cursor n
27711 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6f 72  umber for the or
27712 69 67 69 6e 61 6c 20 6f 75 74 65 72 20 71 75 65  iginal outer que
27713 72 79 20 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 20  ry FROM element 
27714 69 6e 0a 20 20 2a 2a 20 69 50 61 72 65 6e 74 2e  in.  ** iParent.
27715 20 20 54 68 65 20 69 50 61 72 65 6e 74 20 63 75    The iParent cu
27716 72 73 6f 72 20 77 69 6c 6c 20 6e 65 76 65 72 20  rsor will never 
27717 62 65 20 75 73 65 64 2e 20 20 53 75 62 73 65 71  be used.  Subseq
27718 75 65 6e 74 20 63 6f 64 65 0a 20 20 2a 2a 20 77  uent code.  ** w
27719 69 6c 6c 20 73 63 61 6e 20 65 78 70 72 65 73 73  ill scan express
2771a 69 6f 6e 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  ions looking for
2771b 20 69 50 61 72 65 6e 74 20 72 65 66 65 72 65 6e   iParent referen
2771c 63 65 73 20 61 6e 64 20 72 65 70 6c 61 63 65 0a  ces and replace.
2771d 20 20 2a 2a 20 74 68 6f 73 65 20 72 65 66 65 72    ** those refer
2771e 65 6e 63 65 73 20 77 69 74 68 20 65 78 70 72 65  ences with expre
2771f 73 73 69 6f 6e 73 20 74 68 61 74 20 72 65 73 6f  ssions that reso
27720 6c 76 65 20 74 6f 20 74 68 65 20 73 75 62 71 75  lve to the subqu
27721 65 72 79 20 46 52 4f 4d 0a 20 20 2a 2a 20 65 6c  ery FROM.  ** el
27722 65 6d 65 6e 74 73 20 77 65 20 61 72 65 20 6e 6f  ements we are no
27723 77 20 63 6f 70 79 69 6e 67 20 69 6e 2e 0a 20 20  w copying in..  
27724 2a 2f 0a 20 20 66 6f 72 28 70 50 61 72 65 6e 74  */.  for(pParent
27725 3d 70 3b 20 70 50 61 72 65 6e 74 3b 20 70 50 61  =p; pParent; pPa
27726 72 65 6e 74 3d 70 50 61 72 65 6e 74 2d 3e 70 50  rent=pParent->pP
27727 72 69 6f 72 2c 20 70 53 75 62 3d 70 53 75 62 2d  rior, pSub=pSub-
27728 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20 69 6e  >pPrior){.    in
27729 74 20 6e 53 75 62 53 72 63 3b 0a 20 20 20 20 75  t nSubSrc;.    u
2772a 38 20 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a  8 jointype = 0;.
2772b 20 20 20 20 70 53 75 62 53 72 63 20 3d 20 70 53      pSubSrc = pS
2772c 75 62 2d 3e 70 53 72 63 3b 20 20 20 20 20 2f 2a  ub->pSrc;     /*
2772d 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
2772e 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 20 20  subquery */.    
2772f 6e 53 75 62 53 72 63 20 3d 20 70 53 75 62 53 72  nSubSrc = pSubSr
27730 63 2d 3e 6e 53 72 63 3b 20 20 2f 2a 20 4e 75 6d  c->nSrc;  /* Num
27731 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
27732 73 75 62 71 75 65 72 79 20 46 52 4f 4d 20 63 6c  subquery FROM cl
27733 61 75 73 65 20 2a 2f 0a 20 20 20 20 70 53 72 63  ause */.    pSrc
27734 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63   = pParent->pSrc
27735 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c  ;     /* FROM cl
27736 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65  ause of the oute
27737 72 20 71 75 65 72 79 20 2a 2f 0a 0a 20 20 20 20  r query */..    
27738 69 66 28 20 70 53 72 63 20 29 7b 0a 20 20 20 20  if( pSrc ){.    
27739 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e    assert( pParen
2773a 74 3d 3d 70 20 29 3b 20 20 2f 2a 20 46 69 72 73  t==p );  /* Firs
2773b 74 20 74 69 6d 65 20 74 68 72 6f 75 67 68 20 74  t time through t
2773c 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20  he loop */.     
2773d 20 6a 6f 69 6e 74 79 70 65 20 3d 20 70 53 75 62   jointype = pSub
2773e 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a  item->jointype;.
2773f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27740 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74   assert( pParent
27741 21 3d 70 20 29 3b 20 20 2f 2a 20 32 6e 64 20 61  !=p );  /* 2nd a
27742 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 74 69  nd subsequent ti
27743 6d 65 73 20 74 68 72 6f 75 67 68 20 74 68 65 20  mes through the 
27744 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 70 53  loop */.      pS
27745 72 63 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 53  rc = pParent->pS
27746 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  rc = sqlite3SrcL
27747 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20 30 2c  istAppend(db, 0,
27748 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66   0, 0);.      if
27749 28 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20  ( pSrc==0 ){.   
2774a 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d       assert( db-
2774b 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
2774c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2774d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
2774e 20 20 20 2f 2a 20 54 68 65 20 73 75 62 71 75 65     /* The subque
2774f 72 79 20 75 73 65 73 20 61 20 73 69 6e 67 6c 65  ry uses a single
27750 20 73 6c 6f 74 20 6f 66 20 74 68 65 20 46 52 4f   slot of the FRO
27751 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  M clause of the 
27752 6f 75 74 65 72 0a 20 20 20 20 2a 2a 20 71 75 65  outer.    ** que
27753 72 79 2e 20 20 49 66 20 74 68 65 20 73 75 62 71  ry.  If the subq
27754 75 65 72 79 20 68 61 73 20 6d 6f 72 65 20 74 68  uery has more th
27755 61 6e 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 20 69  an one element i
27756 6e 20 69 74 73 20 46 52 4f 4d 20 63 6c 61 75 73  n its FROM claus
27757 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 65  e,.    ** then e
27758 78 70 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20  xpand the outer 
27759 71 75 65 72 79 20 74 6f 20 6d 61 6b 65 20 73 70  query to make sp
2775a 61 63 65 20 66 6f 72 20 69 74 20 74 6f 20 68 6f  ace for it to ho
2775b 6c 64 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 0a  ld all elements.
2775c 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 75      ** of the su
2775d 62 71 75 65 72 79 2e 0a 20 20 20 20 2a 2a 0a 20  bquery..    **. 
2775e 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20     ** Example:. 
2775f 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
27760 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 61  SELECT * FROM ta
27761 62 41 2c 20 28 53 45 4c 45 43 54 20 2a 20 46 52  bA, (SELECT * FR
27762 4f 4d 20 73 75 62 31 2c 20 73 75 62 32 29 2c 20  OM sub1, sub2), 
27763 74 61 62 42 3b 0a 20 20 20 20 2a 2a 0a 20 20 20  tabB;.    **.   
27764 20 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 71 75   ** The outer qu
27765 65 72 79 20 68 61 73 20 33 20 73 6c 6f 74 73 20  ery has 3 slots 
27766 69 6e 20 69 74 73 20 46 52 4f 4d 20 63 6c 61 75  in its FROM clau
27767 73 65 2e 20 20 4f 6e 65 20 73 6c 6f 74 20 6f 66  se.  One slot of
27768 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65   the.    ** oute
27769 72 20 71 75 65 72 79 20 28 74 68 65 20 6d 69 64  r query (the mid
2776a 64 6c 65 20 73 6c 6f 74 29 20 69 73 20 75 73 65  dle slot) is use
2776b 64 20 62 79 20 74 68 65 20 73 75 62 71 75 65 72  d by the subquer
2776c 79 2e 20 20 54 68 65 20 6e 65 78 74 0a 20 20 20  y.  The next.   
2776d 20 2a 2a 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64   ** block of cod
2776e 65 20 77 69 6c 6c 20 65 78 70 61 6e 64 20 74 68  e will expand th
2776f 65 20 6f 75 74 20 71 75 65 72 79 20 74 6f 20 34  e out query to 4
27770 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 6d 69 64   slots.  The mid
27771 64 6c 65 0a 20 20 20 20 2a 2a 20 73 6c 6f 74 20  dle.    ** slot 
27772 69 73 20 65 78 70 61 6e 64 65 64 20 74 6f 20 74  is expanded to t
27773 77 6f 20 73 6c 6f 74 73 20 69 6e 20 6f 72 64 65  wo slots in orde
27774 72 20 74 6f 20 6d 61 6b 65 20 73 70 61 63 65 20  r to make space 
27775 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 74  for the.    ** t
27776 77 6f 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  wo elements in t
27777 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
27778 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a  f the subquery..
27779 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e      */.    if( n
2777a 53 75 62 53 72 63 3e 31 20 29 7b 0a 20 20 20 20  SubSrc>1 ){.    
2777b 20 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 20    pParent->pSrc 
2777c 3d 20 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33  = pSrc = sqlite3
2777d 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64  SrcListEnlarge(d
2777e 62 2c 20 70 53 72 63 2c 20 6e 53 75 62 53 72 63  b, pSrc, nSubSrc
2777f 2d 31 2c 69 46 72 6f 6d 2b 31 29 3b 0a 20 20 20  -1,iFrom+1);.   
27780 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
27781 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
27782 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
27783 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
27784 54 72 61 6e 73 66 65 72 20 74 68 65 20 46 52 4f  Transfer the FRO
27785 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66  M clause terms f
27786 72 6f 6d 20 74 68 65 20 73 75 62 71 75 65 72 79  rom the subquery
27787 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a   into the.    **
27788 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20   outer query..  
27789 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
2778a 3b 20 69 3c 6e 53 75 62 53 72 63 3b 20 69 2b 2b  ; i<nSubSrc; i++
2778b 29 7b 0a 20 20 20 20 20 20 70 53 72 63 2d 3e 61  ){.      pSrc->a
2778c 5b 69 2b 69 46 72 6f 6d 5d 20 3d 20 70 53 75 62  [i+iFrom] = pSub
2778d 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20  Src->a[i];.     
2778e 20 6d 65 6d 73 65 74 28 26 70 53 75 62 53 72 63   memset(&pSubSrc
2778f 2d 3e 61 5b 69 5d 2c 20 30 2c 20 73 69 7a 65 6f  ->a[i], 0, sizeo
27790 66 28 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 29  f(pSubSrc->a[i])
27791 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 72  );.    }.    pSr
27792 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 6a 6f 69 6e  c->a[iFrom].join
27793 74 79 70 65 20 3d 20 6a 6f 69 6e 74 79 70 65 3b  type = jointype;
27794 0a 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 62  .  .    /* Now b
27795 65 67 69 6e 20 73 75 62 73 74 69 74 75 74 69 6e  egin substitutin
27796 67 20 73 75 62 71 75 65 72 79 20 72 65 73 75 6c  g subquery resul
27797 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t set expression
27798 73 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 72 65  s for .    ** re
27799 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
2779a 69 50 61 72 65 6e 74 20 69 6e 20 74 68 65 20 6f  iParent in the o
2779b 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20 20 20  uter query..    
2779c 2a 2a 20 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70  ** .    ** Examp
2779d 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  le:.    **.    *
2779e 2a 20 20 20 53 45 4c 45 43 54 20 61 2b 35 2c 20  *   SELECT a+5, 
2779f 62 2a 31 30 20 46 52 4f 4d 20 28 53 45 4c 45 43  b*10 FROM (SELEC
277a0 54 20 78 2a 33 20 41 53 20 61 2c 20 79 2b 31 30  T x*3 AS a, y+10
277a1 20 41 53 20 62 20 46 52 4f 4d 20 74 31 29 20 57   AS b FROM t1) W
277a2 48 45 52 45 20 61 3e 62 3b 0a 20 20 20 20 2a 2a  HERE a>b;.    **
277a3 20 20 20 5c 20 20 20 20 20 20 20 20 20 20 20 20     \            
277a4 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f           \______
277a5 5f 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75 65 72 79  _______ subquery
277a6 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20   __________/    
277a7 20 20 20 20 20 20 2f 0a 20 20 20 20 2a 2a 20 20        /.    **  
277a8 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f    \_____________
277a9 5f 5f 5f 5f 5f 5f 5f 5f 20 6f 75 74 65 72 20 71  ________ outer q
277aa 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  uery ___________
277ab 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
277ac 5f 5f 5f 2f 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ___/.    **.    
277ad 2a 2a 20 57 65 20 6c 6f 6f 6b 20 61 74 20 65 76  ** We look at ev
277ae 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ery expression i
277af 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  n the outer quer
277b0 79 20 61 6e 64 20 65 76 65 72 79 20 70 6c 61 63  y and every plac
277b1 65 20 77 65 20 73 65 65 0a 20 20 20 20 2a 2a 20  e we see.    ** 
277b2 22 61 22 20 77 65 20 73 75 62 73 74 69 74 75 74  "a" we substitut
277b3 65 20 22 78 2a 33 22 20 61 6e 64 20 65 76 65 72  e "x*3" and ever
277b4 79 20 70 6c 61 63 65 20 77 65 20 73 65 65 20 22  y place we see "
277b5 62 22 20 77 65 20 73 75 62 73 74 69 74 75 74 65  b" we substitute
277b6 20 22 79 2b 31 30 22 2e 0a 20 20 20 20 2a 2f 0a   "y+10"..    */.
277b7 20 20 20 20 70 4c 69 73 74 20 3d 20 70 50 61 72      pList = pPar
277b8 65 6e 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20  ent->pEList;.   
277b9 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
277ba 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
277bb 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70        Expr *pExp
277bc 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69  r;.      if( pLi
277bd 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d  st->a[i].zName==
277be 30 20 26 26 20 28 70 45 78 70 72 20 3d 20 70 4c  0 && (pExpr = pL
277bf 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
277c0 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 29 7b 0a 20  ->span.z!=0 ){. 
277c1 20 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b         pList->a[
277c2 69 5d 2e 7a 4e 61 6d 65 20 3d 20 0a 20 20 20 20  i].zName = .    
277c3 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
277c4 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
277c5 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 73 70  (char*)pExpr->sp
277c6 61 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 73 70 61  an.z, pExpr->spa
277c7 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n.n);.      }.  
277c8 20 20 7d 0a 20 20 20 20 73 75 62 73 74 45 78 70    }.    substExp
277c9 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e  rList(db, pParen
277ca 74 2d 3e 70 45 4c 69 73 74 2c 20 69 50 61 72 65  t->pEList, iPare
277cb 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74  nt, pSub->pEList
277cc 29 3b 0a 20 20 20 20 69 66 28 20 69 73 41 67 67  );.    if( isAgg
277cd 20 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45   ){.      substE
277ce 78 70 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72  xprList(db, pPar
277cf 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69  ent->pGroupBy, i
277d0 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45  Parent, pSub->pE
277d1 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 73 75 62  List);.      sub
277d2 73 74 45 78 70 72 28 64 62 2c 20 70 50 61 72 65  stExpr(db, pPare
277d3 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61  nt->pHaving, iPa
277d4 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
277d5 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  st);.    }.    i
277d6 66 28 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42  f( pSub->pOrderB
277d7 79 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  y ){.      asser
277d8 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64  t( pParent->pOrd
277d9 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20  erBy==0 );.     
277da 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72   pParent->pOrder
277db 42 79 20 3d 20 70 53 75 62 2d 3e 70 4f 72 64 65  By = pSub->pOrde
277dc 72 42 79 3b 0a 20 20 20 20 20 20 70 53 75 62 2d  rBy;.      pSub-
277dd 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20  >pOrderBy = 0;. 
277de 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61     }else if( pPa
277df 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20 29  rent->pOrderBy )
277e0 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70  {.      substExp
277e1 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e  rList(db, pParen
277e2 74 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 50 61  t->pOrderBy, iPa
277e3 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
277e4 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  st);.    }.    i
277e5 66 28 20 70 53 75 62 2d 3e 70 57 68 65 72 65 20  f( pSub->pWhere 
277e6 29 7b 0a 20 20 20 20 20 20 70 57 68 65 72 65 20  ){.      pWhere 
277e7 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
277e8 28 64 62 2c 20 70 53 75 62 2d 3e 70 57 68 65 72  (db, pSub->pWher
277e9 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
277ea 20 20 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b       pWhere = 0;
277eb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
277ec 75 62 71 75 65 72 79 49 73 41 67 67 20 29 7b 0a  ubqueryIsAgg ){.
277ed 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
277ee 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 3d 3d  arent->pHaving==
277ef 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65  0 );.      pPare
277f0 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70 50  nt->pHaving = pP
277f1 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 3b 0a 20  arent->pWhere;. 
277f2 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 57       pParent->pW
277f3 68 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20  here = pWhere;. 
277f4 20 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64       substExpr(d
277f5 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76  b, pParent->pHav
277f6 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70 53  ing, iParent, pS
277f7 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20  ub->pEList);.   
277f8 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76     pParent->pHav
277f9 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ing = sqlite3Exp
277fa 72 41 6e 64 28 64 62 2c 20 70 50 61 72 65 6e 74  rAnd(db, pParent
277fb 2d 3e 70 48 61 76 69 6e 67 2c 20 0a 20 20 20 20  ->pHaving, .    
277fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
277fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
277fe 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
277ff 20 70 53 75 62 2d 3e 70 48 61 76 69 6e 67 29 29   pSub->pHaving))
27800 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
27801 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70 42  pParent->pGroupB
27802 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50  y==0 );.      pP
27803 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 20  arent->pGroupBy 
27804 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
27805 74 44 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70  tDup(db, pSub->p
27806 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 7d 65  GroupBy);.    }e
27807 6c 73 65 7b 0a 20 20 20 20 20 20 73 75 62 73 74  lse{.      subst
27808 45 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74  Expr(db, pParent
27809 2d 3e 70 57 68 65 72 65 2c 20 69 50 61 72 65 6e  ->pWhere, iParen
2780a 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29  t, pSub->pEList)
2780b 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  ;.      pParent-
2780c 3e 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65  >pWhere = sqlite
2780d 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 50 61  3ExprAnd(db, pPa
2780e 72 65 6e 74 2d 3e 70 57 68 65 72 65 2c 20 70 57  rent->pWhere, pW
2780f 68 65 72 65 29 3b 0a 20 20 20 20 7d 0a 20 20 0a  here);.    }.  .
27810 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 74 74      /* The flatt
27811 65 6e 65 64 20 71 75 65 72 79 20 69 73 20 64 69  ened query is di
27812 73 74 69 6e 63 74 20 69 66 20 65 69 74 68 65 72  stinct if either
27813 20 74 68 65 20 69 6e 6e 65 72 20 6f 72 20 74 68   the inner or th
27814 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71  e.    ** outer q
27815 75 65 72 79 20 69 73 20 64 69 73 74 69 6e 63 74  uery is distinct
27816 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50  . .    */.    pP
27817 61 72 65 6e 74 2d 3e 73 65 6c 46 6c 61 67 73 20  arent->selFlags 
27818 7c 3d 20 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67  |= pSub->selFlag
27819 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 3b  s & SF_Distinct;
2781a 0a 20 20 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a  .  .    /*.    *
2781b 2a 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f  * SELECT ... FRO
2781c 4d 20 28 53 45 4c 45 43 54 20 2e 2e 2e 20 4c 49  M (SELECT ... LI
2781d 4d 49 54 20 61 20 4f 46 46 53 45 54 20 62 29 20  MIT a OFFSET b) 
2781e 4c 49 4d 49 54 20 78 20 4f 46 46 53 45 54 20 79  LIMIT x OFFSET y
2781f 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ;.    **.    ** 
27820 4f 6e 65 20 69 73 20 74 65 6d 70 74 65 64 20 74  One is tempted t
27821 6f 20 74 72 79 20 74 6f 20 61 64 64 20 61 20 61  o try to add a a
27822 6e 64 20 62 20 74 6f 20 63 6f 6d 62 69 6e 65 20  nd b to combine 
27823 74 68 65 20 6c 69 6d 69 74 73 2e 20 20 42 75 74  the limits.  But
27824 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 64 6f 65   this.    ** doe
27825 73 20 6e 6f 74 20 77 6f 72 6b 20 69 66 20 65 69  s not work if ei
27826 74 68 65 72 20 6c 69 6d 69 74 20 69 73 20 6e 65  ther limit is ne
27827 67 61 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20  gative..    */. 
27828 20 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69     if( pSub->pLi
27829 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61  mit ){.      pPa
2782a 72 65 6e 74 2d 3e 70 4c 69 6d 69 74 20 3d 20 70  rent->pLimit = p
2782b 53 75 62 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20  Sub->pLimit;.   
2782c 20 20 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20     pSub->pLimit 
2782d 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  = 0;.    }.  }..
2782e 20 20 2f 2a 20 46 69 6e 69 61 6c 6c 79 2c 20 64    /* Finially, d
2782f 65 6c 65 74 65 20 77 68 61 74 20 69 73 20 6c 65  elete what is le
27830 66 74 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  ft of the subque
27831 72 79 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20  ry and return.  
27832 2a 2a 20 73 75 63 63 65 73 73 2e 0a 20 20 2a 2f  ** success..  */
27833 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
27834 44 65 6c 65 74 65 28 64 62 2c 20 70 53 75 62 31  Delete(db, pSub1
27835 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a  );..  return 1;.
27836 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
27837 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
27838 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64  _SUBQUERY) || !d
27839 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2783a 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a 2f 2a 0a  IT_VIEW) */../*.
2783b 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 53  ** Analyze the S
2783c 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2783d 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
2783e 75 6d 65 6e 74 20 74 6f 20 73 65 65 20 69 66 20  ument to see if 
2783f 69 74 0a 2a 2a 20 69 73 20 61 20 6d 69 6e 28 29  it.** is a min()
27840 20 6f 72 20 6d 61 78 28 29 20 71 75 65 72 79 2e   or max() query.
27841 20 52 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52   Return WHERE_OR
27842 44 45 52 42 59 5f 4d 49 4e 20 6f 72 20 57 48 45  DERBY_MIN or WHE
27843 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 69  RE_ORDERBY_MAX i
27844 66 20 0a 2a 2a 20 69 74 20 69 73 2c 20 6f 72 20  f .** it is, or 
27845 30 20 6f 74 68 65 72 77 69 73 65 2e 20 41 74 20  0 otherwise. At 
27846 70 72 65 73 65 6e 74 2c 20 61 20 71 75 65 72 79  present, a query
27847 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74   is considered t
27848 6f 20 62 65 0a 2a 2a 20 61 20 6d 69 6e 28 29 2f  o be.** a min()/
27849 6d 61 78 28 29 20 71 75 65 72 79 20 69 66 3a 0a  max() query if:.
2784a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 54 68 65 72 65  **.**   1. There
2784b 20 69 73 20 61 20 73 69 6e 67 6c 65 20 6f 62 6a   is a single obj
2784c 65 63 74 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ect in the FROM 
2784d 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  clause..**.**   
2784e 32 2e 20 54 68 65 72 65 20 69 73 20 61 20 73 69  2. There is a si
2784f 6e 67 6c 65 20 65 78 70 72 65 73 73 69 6f 6e 20  ngle expression 
27850 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
27851 74 2c 20 61 6e 64 20 69 74 20 69 73 0a 2a 2a 20  t, and it is.** 
27852 20 20 20 20 20 65 69 74 68 65 72 20 6d 69 6e 28       either min(
27853 78 29 20 6f 72 20 6d 61 78 28 78 29 2c 20 77 68  x) or max(x), wh
27854 65 72 65 20 78 20 69 73 20 61 20 63 6f 6c 75 6d  ere x is a colum
27855 6e 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2f 0a  n reference..*/.
27856 73 74 61 74 69 63 20 75 38 20 6d 69 6e 4d 61 78  static u8 minMax
27857 51 75 65 72 79 28 53 65 6c 65 63 74 20 2a 70 29  Query(Select *p)
27858 7b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  {.  Expr *pExpr;
27859 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
2785a 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b  ist = p->pEList;
2785b 0a 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e  ..  if( pEList->
2785c 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 72  nExpr!=1 ) retur
2785d 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  n WHERE_ORDERBY_
2785e 4e 4f 52 4d 41 4c 3b 0a 20 20 70 45 78 70 72 20  NORMAL;.  pExpr 
2785f 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  = pEList->a[0].p
27860 45 78 70 72 3b 0a 20 20 70 45 4c 69 73 74 20 3d  Expr;.  pEList =
27861 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20   pExpr->pList;. 
27862 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
27863 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
27864 7c 7c 20 70 45 4c 69 73 74 3d 3d 30 20 7c 7c 20  || pEList==0 || 
27865 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
27866 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
27867 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  f( pEList->a[0].
27868 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47  pExpr->op!=TK_AG
27869 47 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72  G_COLUMN ) retur
2786a 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  n WHERE_ORDERBY_
2786b 4e 4f 52 4d 41 4c 3b 0a 20 20 69 66 28 20 70 45  NORMAL;.  if( pE
2786c 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 33 20  xpr->token.n!=3 
2786d 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f  ) return WHERE_O
2786e 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 20  RDERBY_NORMAL;. 
2786f 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
27870 49 43 6d 70 28 28 63 68 61 72 2a 29 70 45 78 70  ICmp((char*)pExp
27871 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d 69 6e 22  r->token.z,"min"
27872 2c 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ,3)==0 ){.    re
27873 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
27874 42 59 5f 4d 49 4e 3b 0a 20 20 7d 65 6c 73 65 20  BY_MIN;.  }else 
27875 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
27876 43 6d 70 28 28 63 68 61 72 2a 29 70 45 78 70 72  Cmp((char*)pExpr
27877 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d 61 78 22 2c  ->token.z,"max",
27878 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  3)==0 ){.    ret
27879 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42  urn WHERE_ORDERB
2787a 59 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 72 65 74  Y_MAX;.  }.  ret
2787b 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42  urn WHERE_ORDERB
2787c 59 5f 4e 4f 52 4d 41 4c 3b 0a 7d 0a 0a 2f 2a 0a  Y_NORMAL;.}../*.
2787d 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65  ** If the source
2787e 2d 6c 69 73 74 20 69 74 65 6d 20 70 61 73 73 65  -list item passe
2787f 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
27880 20 77 61 73 20 61 75 67 6d 65 6e 74 65 64 20 77   was augmented w
27881 69 74 68 20 61 6e 0a 2a 2a 20 49 4e 44 45 58 45  ith an.** INDEXE
27882 44 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65  D BY clause, the
27883 6e 20 74 72 79 20 74 6f 20 6c 6f 63 61 74 65 20  n try to locate 
27884 74 68 65 20 73 70 65 63 69 66 69 65 64 20 69 6e  the specified in
27885 64 65 78 2e 20 49 66 20 74 68 65 72 65 0a 2a 2a  dex. If there.**
27886 20 77 61 73 20 73 75 63 68 20 61 20 63 6c 61 75   was such a clau
27887 73 65 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 64  se and the named
27888 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 62 65   index cannot be
27889 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 0a   found, return .
2788a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  ** SQLITE_ERROR 
2788b 61 6e 64 20 6c 65 61 76 65 20 61 6e 20 65 72 72  and leave an err
2788c 6f 72 20 69 6e 20 70 50 61 72 73 65 2e 20 4f 74  or in pParse. Ot
2788d 68 65 72 77 69 73 65 2c 20 70 6f 70 75 6c 61 74  herwise, populat
2788e 65 20 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 70 49 6e  e .** pFrom->pIn
2788f 64 65 78 20 61 6e 64 20 72 65 74 75 72 6e 20 53  dex and return S
27890 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
27891 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
27892 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79  sqlite3IndexedBy
27893 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 70 50  Lookup(Parse *pP
27894 61 72 73 65 2c 20 73 74 72 75 63 74 20 53 72 63  arse, struct Src
27895 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d  List_item *pFrom
27896 29 7b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e  ){.  if( pFrom->
27897 70 54 61 62 20 26 26 20 70 46 72 6f 6d 2d 3e 7a  pTab && pFrom->z
27898 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 54 61 62  Index ){.    Tab
27899 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d  le *pTab = pFrom
2789a 2d 3e 70 54 61 62 3b 0a 20 20 20 20 63 68 61 72  ->pTab;.    char
2789b 20 2a 7a 49 6e 64 65 78 20 3d 20 70 46 72 6f 6d   *zIndex = pFrom
2789c 2d 3e 7a 49 6e 64 65 78 3b 0a 20 20 20 20 49 6e  ->zIndex;.    In
2789d 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 66  dex *pIdx;.    f
2789e 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
2789f 6e 64 65 78 3b 20 0a 20 20 20 20 20 20 20 20 70  ndex; .        p
278a0 49 64 78 20 26 26 20 73 71 6c 69 74 65 33 53 74  Idx && sqlite3St
278a1 72 49 43 6d 70 28 70 49 64 78 2d 3e 7a 4e 61 6d  rICmp(pIdx->zNam
278a2 65 2c 20 7a 49 6e 64 65 78 29 3b 20 0a 20 20 20  e, zIndex); .   
278a3 20 20 20 20 20 70 49 64 78 3d 70 49 64 78 2d 3e       pIdx=pIdx->
278a4 70 4e 65 78 74 0a 20 20 20 20 29 3b 0a 20 20 20  pNext.    );.   
278a5 20 69 66 28 20 21 70 49 64 78 20 29 7b 0a 20 20   if( !pIdx ){.  
278a6 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
278a7 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
278a8 73 75 63 68 20 69 6e 64 65 78 3a 20 25 73 22 2c  such index: %s",
278a9 20 7a 49 6e 64 65 78 2c 20 30 29 3b 0a 20 20 20   zIndex, 0);.   
278aa 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
278ab 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20  _ERROR;.    }.  
278ac 20 20 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20    pFrom->pIndex 
278ad 3d 20 70 49 64 78 3b 0a 20 20 7d 0a 20 20 72 65  = pIdx;.  }.  re
278ae 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
278af 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
278b0 75 74 69 6e 65 20 69 73 20 61 20 57 61 6c 6b 65  utine is a Walke
278b1 72 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 22  r callback for "
278b2 65 78 70 61 6e 64 69 6e 67 22 20 61 20 53 45 4c  expanding" a SEL
278b3 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
278b4 2a 20 22 45 78 70 61 6e 64 69 6e 67 22 20 6d 65  * "Expanding" me
278b5 61 6e 73 20 74 6f 20 64 6f 20 74 68 65 20 66 6f  ans to do the fo
278b6 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
278b7 20 20 28 31 29 20 20 4d 61 6b 65 20 73 75 72 65    (1)  Make sure
278b8 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
278b9 62 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 61  bers have been a
278ba 73 73 69 67 6e 65 64 20 74 6f 20 65 76 65 72 79  ssigned to every
278bb 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6c 65 6d  .**         elem
278bc 65 6e 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  ent of the FROM 
278bd 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  clause..**.**   
278be 20 28 32 29 20 20 46 69 6c 6c 20 69 6e 20 74 68   (2)  Fill in th
278bf 65 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e  e pTabList->a[].
278c0 70 54 61 62 20 66 69 65 6c 64 73 20 69 6e 20 74  pTab fields in t
278c1 68 65 20 53 72 63 4c 69 73 74 20 74 68 61 74 20  he SrcList that 
278c2 0a 2a 2a 20 20 20 20 20 20 20 20 20 64 65 66 69  .**         defi
278c3 6e 65 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  nes FROM clause.
278c4 20 20 57 68 65 6e 20 76 69 65 77 73 20 61 70 70    When views app
278c5 65 61 72 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ear in the FROM 
278c6 63 6c 61 75 73 65 2c 0a 2a 2a 20 20 20 20 20 20  clause,.**      
278c7 20 20 20 66 69 6c 6c 20 70 54 61 62 4c 69 73 74     fill pTabList
278c8 2d 3e 61 5b 5d 2e 70 53 65 6c 65 63 74 20 77 69  ->a[].pSelect wi
278c9 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  th a copy of the
278ca 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
278cb 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 68 61  t.**         tha
278cc 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  t implements the
278cd 20 76 69 65 77 2e 20 20 41 20 63 6f 70 79 20 69   view.  A copy i
278ce 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 76 69  s made of the vi
278cf 65 77 27 73 20 53 45 4c 45 43 54 0a 2a 2a 20 20  ew's SELECT.**  
278d0 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74         statement
278d1 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20   so that we can 
278d2 66 72 65 65 6c 79 20 6d 6f 64 69 66 79 20 6f 72  freely modify or
278d3 20 64 65 6c 65 74 65 20 74 68 61 74 20 73 74 61   delete that sta
278d4 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20  tement.**       
278d5 20 20 77 69 74 68 6f 75 74 20 77 6f 72 72 79 69    without worryi
278d6 6e 67 20 61 62 6f 75 74 20 6d 65 73 73 69 6e 67  ng about messing
278d7 20 75 70 20 74 68 65 20 70 72 65 73 69 73 74 65   up the presiste
278d8 6e 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  nt representatio
278d9 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 66 20  n.**         of 
278da 74 68 65 20 76 69 65 77 2e 0a 2a 2a 0a 2a 2a 20  the view..**.** 
278db 20 20 20 28 33 29 20 20 41 64 64 20 74 65 72 6d     (3)  Add term
278dc 73 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63  s to the WHERE c
278dd 6c 61 75 73 65 20 74 6f 20 61 63 63 6f 6d 6f 64  lause to accomod
278de 61 74 65 20 74 68 65 20 4e 41 54 55 52 41 4c 20  ate the NATURAL 
278df 6b 65 79 77 6f 72 64 0a 2a 2a 20 20 20 20 20 20  keyword.**      
278e0 20 20 20 6f 6e 20 6a 6f 69 6e 73 20 61 6e 64 20     on joins and 
278e1 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  the ON and USING
278e2 20 63 6c 61 75 73 65 20 6f 66 20 6a 6f 69 6e 73   clause of joins
278e3 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29 20 20  ..**.**    (4)  
278e4 53 63 61 6e 20 74 68 65 20 6c 69 73 74 20 6f 66  Scan the list of
278e5 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
278e6 72 65 73 75 6c 74 20 73 65 74 20 28 70 45 4c 69  result set (pELi
278e7 73 74 29 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 20  st) looking.**  
278e8 20 20 20 20 20 20 20 66 6f 72 20 69 6e 73 74 61         for insta
278e9 6e 63 65 73 20 6f 66 20 74 68 65 20 22 2a 22 20  nces of the "*" 
278ea 6f 70 65 72 61 74 6f 72 20 6f 72 20 74 68 65 20  operator or the 
278eb 54 41 42 4c 45 2e 2a 20 6f 70 65 72 61 74 6f 72  TABLE.* operator
278ec 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 49 66 20  ..**         If 
278ed 66 6f 75 6e 64 2c 20 65 78 70 61 6e 64 20 65 61  found, expand ea
278ee 63 68 20 22 2a 22 20 74 6f 20 62 65 20 65 76 65  ch "*" to be eve
278ef 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 65 76 65  ry column in eve
278f0 72 79 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20  ry table.**     
278f1 20 20 20 20 61 6e 64 20 54 41 42 4c 45 2e 2a 20      and TABLE.* 
278f2 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c 75  to be every colu
278f3 6d 6e 20 69 6e 20 54 41 42 4c 45 2e 0a 2a 2a 0a  mn in TABLE..**.
278f4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
278f5 6c 65 63 74 45 78 70 61 6e 64 65 72 28 57 61 6c  lectExpander(Wal
278f6 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65  ker *pWalker, Se
278f7 6c 65 63 74 20 2a 70 29 7b 0a 20 20 50 61 72 73  lect *p){.  Pars
278f8 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c  e *pParse = pWal
278f9 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 69  ker->pParse;.  i
278fa 6e 74 20 69 2c 20 6a 2c 20 6b 3b 0a 20 20 53 72  nt i, j, k;.  Sr
278fb 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b  cList *pTabList;
278fc 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
278fd 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 53 72  ist;.  struct Sr
278fe 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f  cList_item *pFro
278ff 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  m;.  sqlite3 *db
27900 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
27901 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
27902 46 61 69 6c 65 64 20 20 29 7b 0a 20 20 20 20 72  Failed  ){.    r
27903 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
27904 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 53  .  }.  if( p->pS
27905 72 63 3d 3d 30 20 7c 7c 20 28 70 2d 3e 73 65 6c  rc==0 || (p->sel
27906 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e  Flags & SF_Expan
27907 64 65 64 29 21 3d 30 20 29 7b 0a 20 20 20 20 72  ded)!=0 ){.    r
27908 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
27909 0a 20 20 7d 0a 20 20 70 2d 3e 73 65 6c 46 6c 61  .  }.  p->selFla
2790a 67 73 20 7c 3d 20 53 46 5f 45 78 70 61 6e 64 65  gs |= SF_Expande
2790b 64 3b 0a 20 20 70 54 61 62 4c 69 73 74 20 3d 20  d;.  pTabList = 
2790c 70 2d 3e 70 53 72 63 3b 0a 20 20 70 45 4c 69 73  p->pSrc;.  pELis
2790d 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a  t = p->pEList;..
2790e 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 63    /* Make sure c
2790f 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61  ursor numbers ha
27910 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64  ve been assigned
27911 20 74 6f 20 61 6c 6c 20 65 6e 74 72 69 65 73 20   to all entries 
27912 69 6e 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d  in.  ** the FROM
27913 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 53   clause of the S
27914 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
27915 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53  .  */.  sqlite3S
27916 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73  rcListAssignCurs
27917 6f 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ors(pParse, pTab
27918 4c 69 73 74 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f  List);..  /* Loo
27919 6b 20 75 70 20 65 76 65 72 79 20 74 61 62 6c 65  k up every table
2791a 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 46 52   named in the FR
2791b 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  OM clause of the
2791c 20 73 65 6c 65 63 74 2e 20 20 49 66 0a 20 20 2a   select.  If.  *
2791d 2a 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68  * an entry of th
2791e 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
2791f 20 61 20 73 75 62 71 75 65 72 79 20 69 6e 73 74   a subquery inst
27920 65 61 64 20 6f 66 20 61 20 74 61 62 6c 65 20 6f  ead of a table o
27921 72 20 76 69 65 77 2c 0a 20 20 2a 2a 20 74 68 65  r view,.  ** the
27922 6e 20 63 72 65 61 74 65 20 61 20 74 72 61 6e 73  n create a trans
27923 69 65 6e 74 20 74 61 62 6c 65 20 73 74 72 75 63  ient table struc
27924 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65  ture to describe
27925 20 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20   the subquery.. 
27926 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70   */.  for(i=0, p
27927 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61  From=pTabList->a
27928 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
27929 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b  rc; i++, pFrom++
2792a 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
2792b 61 62 3b 0a 20 20 20 20 69 66 28 20 70 46 72 6f  ab;.    if( pFro
2792c 6d 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20  m->pTab!=0 ){.  
2792d 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74 61 74      /* This stat
2792e 65 6d 65 6e 74 20 68 61 73 20 61 6c 72 65 61 64  ement has alread
2792f 79 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 2e  y been prepared.
27930 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65    There is no ne
27931 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 67  ed.      ** to g
27932 6f 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20  o further. */.  
27933 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30      assert( i==0
27934 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
27935 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20   WRC_Prune;.    
27936 7d 0a 20 20 20 20 69 66 28 20 70 46 72 6f 6d 2d  }.    if( pFrom-
27937 3e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 23 69 66  >zName==0 ){.#if
27938 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27939 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 20 20  _SUBQUERY.      
2793a 53 65 6c 65 63 74 20 2a 70 53 65 6c 20 3d 20 70  Select *pSel = p
2793b 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20  From->pSelect;. 
2793c 20 20 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75       /* A sub-qu
2793d 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ery in the FROM 
2793e 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
2793f 43 54 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  CT */.      asse
27940 72 74 28 20 70 53 65 6c 21 3d 30 20 29 3b 0a 20  rt( pSel!=0 );. 
27941 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72       assert( pFr
27942 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b 0a 20  om->pTab==0 );. 
27943 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b       sqlite3Walk
27944 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20  Select(pWalker, 
27945 70 53 65 6c 29 3b 0a 20 20 20 20 20 20 70 46 72  pSel);.      pFr
27946 6f 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 20  om->pTab = pTab 
27947 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
27948 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
27949 28 54 61 62 6c 65 29 29 3b 0a 20 20 20 20 20 20  (Table));.      
2794a 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65  if( pTab==0 ) re
2794b 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2794c 20 20 20 20 20 20 70 54 61 62 2d 3e 64 62 20 3d        pTab->db =
2794d 20 64 62 3b 0a 20 20 20 20 20 20 70 54 61 62 2d   db;.      pTab-
2794e 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20  >nRef = 1;.     
2794f 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d 20 73   pTab->zName = s
27950 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
27951 2c 20 22 73 71 6c 69 74 65 5f 73 75 62 71 75 65  , "sqlite_subque
27952 72 79 5f 25 70 5f 22 2c 20 28 76 6f 69 64 2a 29  ry_%p_", (void*)
27953 70 54 61 62 29 3b 0a 20 20 20 20 20 20 77 68 69  pTab);.      whi
27954 6c 65 28 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72  le( pSel->pPrior
27955 20 29 7b 20 70 53 65 6c 20 3d 20 70 53 65 6c 2d   ){ pSel = pSel-
27956 3e 70 50 72 69 6f 72 3b 20 7d 0a 20 20 20 20 20  >pPrior; }.     
27957 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46 72   selectColumnsFr
27958 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  omExprList(pPars
27959 65 2c 20 70 53 65 6c 2d 3e 70 45 4c 69 73 74 2c  e, pSel->pEList,
2795a 20 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 26 70   &pTab->nCol, &p
2795b 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20 20 20  Tab->aCol);.    
2795c 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20    pTab->iPKey = 
2795d 2d 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  -1;.      pTab->
2795e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 45  tabFlags |= TF_E
2795f 70 68 65 6d 65 72 61 6c 3b 0a 23 65 6e 64 69 66  phemeral;.#endif
27960 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
27961 20 20 2f 2a 20 41 6e 20 6f 72 64 69 6e 61 72 79    /* An ordinary
27962 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6e   table or view n
27963 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ame in the FROM 
27964 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20  clause */.      
27965 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70  assert( pFrom->p
27966 54 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  Tab==0 );.      
27967 70 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20 70 54  pFrom->pTab = pT
27968 61 62 20 3d 20 0a 20 20 20 20 20 20 20 20 73 71  ab = .        sq
27969 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
2796a 28 70 50 61 72 73 65 2c 30 2c 70 46 72 6f 6d 2d  (pParse,0,pFrom-
2796b 3e 7a 4e 61 6d 65 2c 70 46 72 6f 6d 2d 3e 7a 44  >zName,pFrom->zD
2796c 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 20 20  atabase);.      
2796d 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65  if( pTab==0 ) re
2796e 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2796f 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66        pTab->nRef
27970 2b 2b 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64  ++;.#if !defined
27971 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
27972 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 20 28  W) || !defined (
27973 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
27974 55 41 4c 54 41 42 4c 45 29 0a 20 20 20 20 20 20  UALTABLE).      
27975 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
27976 74 20 7c 7c 20 49 73 56 69 72 74 75 61 6c 28 70  t || IsVirtual(p
27977 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Tab) ){.        
27978 2f 2a 20 57 65 20 72 65 61 63 68 20 68 65 72 65  /* We reach here
27979 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 74 61   if the named ta
2797a 62 6c 65 20 69 73 20 61 20 72 65 61 6c 6c 79 20  ble is a really 
2797b 61 20 76 69 65 77 20 2a 2f 0a 20 20 20 20 20 20  a view */.      
2797c 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65    if( sqlite3Vie
2797d 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
2797e 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 20  pParse, pTab) ) 
2797f 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
27980 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ;..        /* If
27981 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 21   pFrom->pSelect!
27982 3d 30 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61  =0 it means we a
27983 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
27984 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 69 65  a.        ** vie
27985 77 20 77 69 74 68 69 6e 20 61 20 76 69 65 77 2e  w within a view.
27986 20 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 72    The SELECT str
27987 75 63 74 75 72 65 20 68 61 73 20 61 6c 72 65 61  ucture has alrea
27988 64 79 20 62 65 65 6e 0a 20 20 20 20 20 20 20 20  dy been.        
27989 2a 2a 20 63 6f 70 69 65 64 20 62 79 20 74 68 65  ** copied by the
2798a 20 6f 75 74 65 72 20 76 69 65 77 20 73 6f 20 77   outer view so w
2798b 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 20 63  e can skip the c
2798c 6f 70 79 20 73 74 65 70 20 68 65 72 65 0a 20 20  opy step here.  
2798d 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20        ** in the 
2798e 69 6e 6e 65 72 20 76 69 65 77 2e 0a 20 20 20 20  inner view..    
2798f 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
27990 66 28 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63  f( pFrom->pSelec
27991 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
27992 20 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74    pFrom->pSelect
27993 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
27994 44 75 70 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  Dup(db, pTab->pS
27995 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20  elect);.        
27996 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c    sqlite3WalkSel
27997 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 46 72  ect(pWalker, pFr
27998 6f 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  om->pSelect);.  
27999 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2799a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20  #endif.    }..  
2799b 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
2799c 69 6e 64 65 78 20 6e 61 6d 65 64 20 62 79 20 74  index named by t
2799d 68 65 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  he INDEXED BY cl
2799e 61 75 73 65 2c 20 69 66 20 61 6e 79 2e 20 2a 2f  ause, if any. */
2799f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
279a0 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28  IndexedByLookup(
279a1 70 50 61 72 73 65 2c 20 70 46 72 6f 6d 29 20 29  pParse, pFrom) )
279a2 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57  {.      return W
279a3 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a  RC_Abort;.    }.
279a4 20 20 7d 0a 0a 20 20 2f 2a 20 50 72 6f 63 65 73    }..  /* Proces
279a5 73 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72  s NATURAL keywor
279a6 64 73 2c 20 61 6e 64 20 4f 4e 20 61 6e 64 20 55  ds, and ON and U
279a7 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20  SING clauses of 
279a8 6a 6f 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  joins..  */.  if
279a9 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
279aa 65 64 20 7c 7c 20 73 71 6c 69 74 65 50 72 6f 63  ed || sqliteProc
279ab 65 73 73 4a 6f 69 6e 28 70 50 61 72 73 65 2c 20  essJoin(pParse, 
279ac 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  p) ){.    return
279ad 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a   WRC_Abort;.  }.
279ae 0a 20 20 2f 2a 20 46 6f 72 20 65 76 65 72 79 20  .  /* For every 
279af 22 2a 22 20 74 68 61 74 20 6f 63 63 75 72 73 20  "*" that occurs 
279b0 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6c 69  in the column li
279b1 73 74 2c 20 69 6e 73 65 72 74 20 74 68 65 20 6e  st, insert the n
279b2 61 6d 65 73 20 6f 66 0a 20 20 2a 2a 20 61 6c 6c  ames of.  ** all
279b3 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20   columns in all 
279b4 74 61 62 6c 65 73 2e 20 20 41 6e 64 20 66 6f 72  tables.  And for
279b5 20 65 76 65 72 79 20 54 41 42 4c 45 2e 2a 20 69   every TABLE.* i
279b6 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73 0a  nsert the names.
279b7 20 20 2a 2a 20 6f 66 20 61 6c 6c 20 63 6f 6c 75    ** of all colu
279b8 6d 6e 73 20 69 6e 20 54 41 42 4c 45 2e 20 20 54  mns in TABLE.  T
279b9 68 65 20 70 61 72 73 65 72 20 69 6e 73 65 72 74  he parser insert
279ba 65 64 20 61 20 73 70 65 63 69 61 6c 20 65 78 70  ed a special exp
279bb 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 77 69 74  ression.  ** wit
279bc 68 20 74 68 65 20 54 4b 5f 41 4c 4c 20 6f 70 65  h the TK_ALL ope
279bd 72 61 74 6f 72 20 66 6f 72 20 65 61 63 68 20 22  rator for each "
279be 2a 22 20 74 68 61 74 20 69 74 20 66 6f 75 6e 64  *" that it found
279bf 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6c   in the column l
279c0 69 73 74 2e 0a 20 20 2a 2a 20 54 68 65 20 66 6f  ist..  ** The fo
279c1 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6a 75 73  llowing code jus
279c2 74 20 68 61 73 20 74 6f 20 6c 6f 63 61 74 65 20  t has to locate 
279c3 74 68 65 20 54 4b 5f 41 4c 4c 20 65 78 70 72 65  the TK_ALL expre
279c4 73 73 69 6f 6e 73 20 61 6e 64 20 65 78 70 61 6e  ssions and expan
279c5 64 0a 20 20 2a 2a 20 65 61 63 68 20 6f 6e 65 20  d.  ** each one 
279c6 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61  to the list of a
279c7 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c  ll columns in al
279c8 6c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20  l tables..  **. 
279c9 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6c 6f   ** The first lo
279ca 6f 70 20 6a 75 73 74 20 63 68 65 63 6b 73 20 74  op just checks t
279cb 6f 20 73 65 65 20 69 66 20 74 68 65 72 65 20 61  o see if there a
279cc 72 65 20 61 6e 79 20 22 2a 22 20 6f 70 65 72 61  re any "*" opera
279cd 74 6f 72 73 0a 20 20 2a 2a 20 74 68 61 74 20 6e  tors.  ** that n
279ce 65 65 64 20 65 78 70 61 6e 64 69 6e 67 2e 0a 20  eed expanding.. 
279cf 20 2a 2f 0a 20 20 66 6f 72 28 6b 3d 30 3b 20 6b   */.  for(k=0; k
279d0 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
279d1 6b 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a  k++){.    Expr *
279d2 70 45 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6b  pE = pEList->a[k
279d3 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28  ].pExpr;.    if(
279d4 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20   pE->op==TK_ALL 
279d5 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28  ) break;.    if(
279d6 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20   pE->op==TK_DOT 
279d7 26 26 20 70 45 2d 3e 70 52 69 67 68 74 20 26 26  && pE->pRight &&
279d8 20 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 3d   pE->pRight->op=
279d9 3d 54 4b 5f 41 4c 4c 0a 20 20 20 20 20 20 20 20  =TK_ALL.        
279da 20 26 26 20 70 45 2d 3e 70 4c 65 66 74 20 26 26   && pE->pLeft &&
279db 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d   pE->pLeft->op==
279dc 54 4b 5f 49 44 20 29 20 62 72 65 61 6b 3b 0a 20  TK_ID ) break;. 
279dd 20 7d 0a 20 20 69 66 28 20 6b 3c 70 45 4c 69 73   }.  if( k<pELis
279de 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20  t->nExpr ){.    
279df 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20 77 65 20  /*.    ** If we 
279e0 67 65 74 20 68 65 72 65 20 69 74 20 6d 65 61 6e  get here it mean
279e1 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  s the result set
279e2 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72   contains one or
279e3 20 6d 6f 72 65 20 22 2a 22 0a 20 20 20 20 2a 2a   more "*".    **
279e4 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61 74 20   operators that 
279e5 6e 65 65 64 20 74 6f 20 62 65 20 65 78 70 61 6e  need to be expan
279e6 64 65 64 2e 20 20 4c 6f 6f 70 20 74 68 72 6f 75  ded.  Loop throu
279e7 67 68 20 65 61 63 68 20 65 78 70 72 65 73 73 69  gh each expressi
279e8 6f 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  on.    ** in the
279e9 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64 20   result set and 
279ea 65 78 70 61 6e 64 20 74 68 65 6d 20 6f 6e 65 20  expand them one 
279eb 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20  by one..    */. 
279ec 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
279ed 73 74 5f 69 74 65 6d 20 2a 61 20 3d 20 70 45 4c  st_item *a = pEL
279ee 69 73 74 2d 3e 61 3b 0a 20 20 20 20 45 78 70 72  ist->a;.    Expr
279ef 4c 69 73 74 20 2a 70 4e 65 77 20 3d 20 30 3b 0a  List *pNew = 0;.
279f0 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20      int flags = 
279f1 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67  pParse->db->flag
279f2 73 3b 0a 20 20 20 20 69 6e 74 20 6c 6f 6e 67 4e  s;.    int longN
279f3 61 6d 65 73 20 3d 20 28 66 6c 61 67 73 20 26 20  ames = (flags & 
279f4 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61  SQLITE_FullColNa
279f5 6d 65 73 29 21 3d 30 0a 20 20 20 20 20 20 20 20  mes)!=0.        
279f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26                &&
279f7 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
279f8 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29 3d  _ShortColNames)=
279f9 3d 30 3b 0a 0a 20 20 20 20 66 6f 72 28 6b 3d 30  =0;..    for(k=0
279fa 3b 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; k<pEList->nExp
279fb 72 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 45  r; k++){.      E
279fc 78 70 72 20 2a 70 45 20 3d 20 61 5b 6b 5d 2e 70  xpr *pE = a[k].p
279fd 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20  Expr;.      if( 
279fe 70 45 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20 26  pE->op!=TK_ALL &
279ff 26 0a 20 20 20 20 20 20 20 20 20 20 20 28 70 45  &.           (pE
27a00 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20  ->op!=TK_DOT || 
27a01 70 45 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c  pE->pRight==0 ||
27a02 20 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 21   pE->pRight->op!
27a03 3d 54 4b 5f 41 4c 4c 29 20 29 7b 0a 20 20 20 20  =TK_ALL) ){.    
27a04 20 20 20 20 2f 2a 20 54 68 69 73 20 70 61 72 74      /* This part
27a05 69 63 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  icular expressio
27a06 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
27a07 74 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e 0a  to be expanded..
27a08 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
27a09 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
27a0a 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
27a0b 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 61 5b  pParse, pNew, a[
27a0c 6b 5d 2e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20  k].pExpr, 0);.  
27a0d 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29        if( pNew )
27a0e 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77  {.          pNew
27a0f 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 45 78 70 72 2d  ->a[pNew->nExpr-
27a10 31 5d 2e 7a 4e 61 6d 65 20 3d 20 61 5b 6b 5d 2e  1].zName = a[k].
27a11 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d  zName;.        }
27a12 0a 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 70 45  .        a[k].pE
27a13 78 70 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  xpr = 0;.       
27a14 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 20 3d 20 30 3b   a[k].zName = 0;
27a15 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
27a16 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 65 78        /* This ex
27a17 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 22 2a  pression is a "*
27a18 22 20 6f 72 20 61 20 22 54 41 42 4c 45 2e 2a 22  " or a "TABLE.*"
27a19 20 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65   and needs to be
27a1a 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 61  .        ** expa
27a1b 6e 64 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20  nded. */.       
27a1c 20 69 6e 74 20 74 61 62 6c 65 53 65 65 6e 20 3d   int tableSeen =
27a1d 20 30 3b 20 20 20 20 20 20 2f 2a 20 53 65 74 20   0;      /* Set 
27a1e 74 6f 20 31 20 77 68 65 6e 20 54 41 42 4c 45 20  to 1 when TABLE 
27a1f 6d 61 74 63 68 65 73 20 2a 2f 0a 20 20 20 20 20  matches */.     
27a20 20 20 20 63 68 61 72 20 2a 7a 54 4e 61 6d 65 3b     char *zTName;
27a21 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74              /* t
27a22 65 78 74 20 6f 66 20 6e 61 6d 65 20 6f 66 20 54  ext of name of T
27a23 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20 20 20  ABLE */.        
27a24 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44  if( pE->op==TK_D
27a25 4f 54 20 26 26 20 70 45 2d 3e 70 4c 65 66 74 20  OT && pE->pLeft 
27a26 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 4e  ){.          zTN
27a27 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
27a28 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26  eFromToken(db, &
27a29 70 45 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e  pE->pLeft->token
27a2a 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
27a2b 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 4e 61  {.          zTNa
27a2c 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  me = 0;.        
27a2d 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  }.        for(i=
27a2e 30 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73  0, pFrom=pTabLis
27a2f 74 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74  t->a; i<pTabList
27a30 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72  ->nSrc; i++, pFr
27a31 6f 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  om++){.         
27a32 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
27a33 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  From->pTab;.    
27a34 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62        char *zTab
27a35 4e 61 6d 65 20 3d 20 70 46 72 6f 6d 2d 3e 7a 41  Name = pFrom->zA
27a36 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 20 20 20  lias;.          
27a37 69 66 28 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20  if( zTabName==0 
27a38 7c 7c 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 3d 3d  || zTabName[0]==
27a39 30 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20  0 ){ .          
27a3a 20 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61    zTabName = pTa
27a3b 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  b->zName;.      
27a3c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
27a3d 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
27a3e 69 6c 65 64 20 29 20 62 72 65 61 6b 3b 0a 20 20  iled ) break;.  
27a3f 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 4e 61          if( zTNa
27a40 6d 65 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  me && sqlite3Str
27a41 49 43 6d 70 28 7a 54 4e 61 6d 65 2c 20 7a 54 61  ICmp(zTName, zTa
27a42 62 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20  bName)!=0 ){.   
27a43 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75           continu
27a44 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  e;.          }. 
27a45 20 20 20 20 20 20 20 20 20 74 61 62 6c 65 53 65           tableSe
27a46 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  en = 1;.        
27a47 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61    for(j=0; j<pTa
27a48 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20  b->nCol; j++){. 
27a49 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20             Expr 
27a4a 2a 70 45 78 70 72 2c 20 2a 70 52 69 67 68 74 3b  *pExpr, *pRight;
27a4b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61  .            cha
27a4c 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54 61 62 2d  r *zName = pTab-
27a4d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a  >aCol[j].zName;.
27a4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
27a4f 49 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 6d  If a column is m
27a50 61 72 6b 65 64 20 61 73 20 27 68 69 64 64 65 6e  arked as 'hidden
27a51 27 20 28 63 75 72 72 65 6e 74 6c 79 20 6f 6e 6c  ' (currently onl
27a52 79 20 70 6f 73 73 69 62 6c 65 0a 20 20 20 20 20  y possible.     
27a53 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 76 69         ** for vi
27a54 72 74 75 61 6c 20 74 61 62 6c 65 73 29 2c 20 64  rtual tables), d
27a55 6f 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 69 74  o not include it
27a56 20 69 6e 20 74 68 65 20 65 78 70 61 6e 64 65 64   in the expanded
27a57 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
27a58 72 65 73 75 6c 74 2d 73 65 74 20 6c 69 73 74 2e  result-set list.
27a59 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  .            */.
27a5a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
27a5b 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26  IsHiddenColumn(&
27a5c 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 29 20 29  pTab->aCol[j]) )
27a5d 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
27a5e 61 73 73 65 72 74 28 49 73 56 69 72 74 75 61 6c  assert(IsVirtual
27a5f 28 70 54 61 62 29 29 3b 0a 20 20 20 20 20 20 20  (pTab));.       
27a60 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
27a61 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 0a  .            }..
27a62 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
27a63 69 3e 30 20 26 26 20 7a 54 4e 61 6d 65 3d 3d 30  i>0 && zTName==0
27a64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
27a65 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
27a66 5f 69 74 65 6d 20 2a 70 4c 65 66 74 20 3d 20 26  _item *pLeft = &
27a67 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 2d 31 5d  pTabList->a[i-1]
27a68 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
27a69 69 66 28 20 28 70 4c 65 66 74 5b 31 5d 2e 6a 6f  if( (pLeft[1].jo
27a6a 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55  intype & JT_NATU
27a6b 52 41 4c 29 21 3d 30 20 26 26 0a 20 20 20 20 20  RAL)!=0 &&.     
27a6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27a6d 20 20 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70     columnIndex(p
27a6e 4c 65 66 74 2d 3e 70 54 61 62 2c 20 7a 4e 61 6d  Left->pTab, zNam
27a6f 65 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)>=0 ){.       
27a70 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 61           /* In a
27a71 20 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 2c 20 6f   NATURAL join, o
27a72 6d 69 74 20 74 68 65 20 6a 6f 69 6e 20 63 6f 6c  mit the join col
27a73 75 6d 6e 73 20 66 72 6f 6d 20 74 68 65 20 0a 20  umns from the . 
27a74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
27a75 2a 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 72  * table on the r
27a76 69 67 68 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  ight */.        
27a77 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
27a78 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
27a79 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
27a7a 69 66 28 20 73 71 6c 69 74 65 33 49 64 4c 69 73  if( sqlite3IdLis
27a7b 74 49 6e 64 65 78 28 70 4c 65 66 74 5b 31 5d 2e  tIndex(pLeft[1].
27a7c 70 55 73 69 6e 67 2c 20 7a 4e 61 6d 65 29 3e 3d  pUsing, zName)>=
27a7d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
27a7e 20 20 20 20 20 2f 2a 20 49 6e 20 61 20 6a 6f 69       /* In a joi
27a7f 6e 20 77 69 74 68 20 61 20 55 53 49 4e 47 20 63  n with a USING c
27a80 6c 61 75 73 65 2c 20 6f 6d 69 74 20 63 6f 6c 75  lause, omit colu
27a81 6d 6e 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20  mns in the.     
27a82 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73             ** us
27a83 69 6e 67 20 63 6c 61 75 73 65 20 66 72 6f 6d 20  ing clause from 
27a84 74 68 65 20 74 61 62 6c 65 20 6f 6e 20 74 68 65  the table on the
27a85 20 72 69 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20   right. */.     
27a86 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69             conti
27a87 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nue;.           
27a88 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
27a89 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70   }.            p
27a8a 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 50  Right = sqlite3P
27a8b 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
27a8c 49 44 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  ID, 0, 0, 0);.  
27a8d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 52            if( pR
27a8e 69 67 68 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b  ight==0 ) break;
27a8f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 65 74  .            set
27a90 51 75 6f 74 65 64 54 6f 6b 65 6e 28 70 50 61 72  QuotedToken(pPar
27a91 73 65 2c 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b  se, &pRight->tok
27a92 65 6e 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  en, zName);.    
27a93 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67          if( long
27a94 4e 61 6d 65 73 20 7c 7c 20 70 54 61 62 4c 69 73  Names || pTabLis
27a95 74 2d 3e 6e 53 72 63 3e 31 20 29 7b 0a 20 20 20  t->nSrc>1 ){.   
27a96 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20             Expr 
27a97 2a 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33  *pLeft = sqlite3
27a98 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
27a99 5f 49 44 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  _ID, 0, 0, 0);. 
27a9a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
27a9b 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
27a9c 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
27a9d 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  , pLeft, pRight,
27a9e 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
27a9f 20 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20     if( pExpr==0 
27aa0 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
27aa1 20 20 20 20 20 20 20 73 65 74 51 75 6f 74 65 64         setQuoted
27aa2 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c 20 26 70  Token(pParse, &p
27aa3 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2c 20 7a 54 61  Left->token, zTa
27aa4 62 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  bName);.        
27aa5 20 20 20 20 20 20 73 65 74 54 6f 6b 65 6e 28 26        setToken(&
27aa6 70 45 78 70 72 2d 3e 73 70 61 6e 2c 20 0a 20 20  pExpr->span, .  
27aa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27aa8 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
27aa9 62 2c 20 22 25 73 2e 25 73 22 2c 20 7a 54 61 62  b, "%s.%s", zTab
27aaa 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 29 3b 0a 20  Name, zName));. 
27aab 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
27aac 70 72 2d 3e 73 70 61 6e 2e 64 79 6e 20 3d 20 31  pr->span.dyn = 1
27aad 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
27aae 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d  pExpr->token.z =
27aaf 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   0;.            
27ab0 20 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e    pExpr->token.n
27ab1 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
27ab2 20 20 20 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e      pExpr->token
27ab3 2e 64 79 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  .dyn = 0;.      
27ab4 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
27ab5 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
27ab6 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 20   = pRight;.     
27ab7 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
27ab8 73 70 61 6e 20 3d 20 70 45 78 70 72 2d 3e 74 6f  span = pExpr->to
27ab9 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ken;.           
27aba 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e 2e 64     pExpr->span.d
27abb 79 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  yn = 0;.        
27abc 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
27abd 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20    if( longNames 
27abe 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
27abf 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45   pNew = sqlite3E
27ac0 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
27ac1 61 72 73 65 2c 20 70 4e 65 77 2c 20 70 45 78 70  arse, pNew, pExp
27ac2 72 2c 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29  r, &pExpr->span)
27ac3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65  ;.            }e
27ac4 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
27ac5 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
27ac6 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
27ac7 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 70 45  pParse, pNew, pE
27ac8 78 70 72 2c 20 26 70 52 69 67 68 74 2d 3e 74 6f  xpr, &pRight->to
27ac9 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ken);.          
27aca 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
27acb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
27acc 20 20 69 66 28 20 21 74 61 62 6c 65 53 65 65 6e    if( !tableSeen
27acd 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
27ace 28 20 7a 54 4e 61 6d 65 20 29 7b 0a 20 20 20 20  ( zTName ){.    
27acf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
27ad0 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
27ad1 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a 20  "no such table: 
27ad2 25 73 22 2c 20 7a 54 4e 61 6d 65 29 3b 0a 20 20  %s", zTName);.  
27ad3 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
27ad4 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
27ad5 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
27ad6 65 2c 20 22 6e 6f 20 74 61 62 6c 65 73 20 73 70  e, "no tables sp
27ad7 65 63 69 66 69 65 64 22 29 3b 0a 20 20 20 20 20  ecified");.     
27ad8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
27ad9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27ada 44 62 46 72 65 65 28 64 62 2c 20 7a 54 4e 61 6d  DbFree(db, zTNam
27adb 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
27adc 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  }.    sqlite3Exp
27add 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
27ade 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e  pEList);.    p->
27adf 70 45 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20  pEList = pNew;. 
27ae0 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41   }.#if SQLITE_MA
27ae1 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70  X_COLUMN.  if( p
27ae2 2d 3e 70 45 4c 69 73 74 20 26 26 20 70 2d 3e 70  ->pEList && p->p
27ae3 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 64 62 2d  EList->nExpr>db-
27ae4 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
27ae5 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a  IMIT_COLUMN] ){.
27ae6 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
27ae7 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
27ae8 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e   many columns in
27ae9 20 72 65 73 75 6c 74 20 73 65 74 22 29 3b 0a 20   result set");. 
27aea 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
27aeb 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
27aec 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20  .}../*.** No-op 
27aed 72 6f 75 74 69 6e 65 20 66 6f 72 20 74 68 65 20  routine for the 
27aee 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b 65  parse-tree walke
27aef 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  r..**.** When th
27af0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
27af1 65 20 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 61  e Walker.xExprCa
27af2 6c 6c 62 61 63 6b 20 74 68 65 6e 20 65 78 70 72  llback then expr
27af3 65 73 73 69 6f 6e 20 74 72 65 65 73 0a 2a 2a 20  ession trees.** 
27af4 61 72 65 20 77 61 6c 6b 65 64 20 77 69 74 68 6f  are walked witho
27af5 75 74 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 62  ut any actions b
27af6 65 69 6e 67 20 74 61 6b 65 6e 20 61 74 20 65 61  eing taken at ea
27af7 63 68 20 6e 6f 64 65 2e 20 20 50 72 65 73 75 6d  ch node.  Presum
27af8 61 62 6c 79 2c 0a 2a 2a 20 77 68 65 6e 20 74 68  ably,.** when th
27af9 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
27afa 65 64 20 66 6f 72 20 57 61 6c 6b 65 72 2e 78 45  ed for Walker.xE
27afb 78 70 72 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e  xprCallback then
27afc 20 0a 2a 2a 20 57 61 6c 6b 65 72 2e 78 53 65 6c   .** Walker.xSel
27afd 65 63 74 43 61 6c 6c 62 61 63 6b 20 69 73 20 73  ectCallback is s
27afe 65 74 20 74 6f 20 64 6f 20 73 6f 6d 65 74 68 69  et to do somethi
27aff 6e 67 20 75 73 65 66 75 6c 20 66 6f 72 20 65 76  ng useful for ev
27b00 65 72 79 20 0a 2a 2a 20 73 75 62 71 75 65 72 79  ery .** subquery
27b01 20 69 6e 20 74 68 65 20 70 61 72 73 65 72 20 74   in the parser t
27b02 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ree..*/.static i
27b03 6e 74 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 28  nt exprWalkNoop(
27b04 57 61 6c 6b 65 72 20 2a 4e 6f 74 55 73 65 64 2c  Walker *NotUsed,
27b05 20 45 78 70 72 20 2a 4e 6f 74 55 73 65 64 32 29   Expr *NotUsed2)
27b06 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
27b07 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
27b08 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75  otUsed2);.  retu
27b09 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
27b0a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
27b0b 6f 75 74 69 6e 65 20 22 65 78 70 61 6e 64 73 22  outine "expands"
27b0c 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
27b0d 65 6e 74 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69  ent and all of i
27b0e 74 73 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a  ts subqueries..*
27b0f 2a 20 46 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  * For additional
27b10 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20   information on 
27b11 77 68 61 74 20 69 74 20 6d 65 61 6e 73 20 74 6f  what it means to
27b12 20 22 65 78 70 61 6e 64 22 20 61 20 53 45 4c 45   "expand" a SELE
27b13 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2c  CT.** statement,
27b14 20 73 65 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74   see the comment
27b15 20 6f 6e 20 74 68 65 20 73 65 6c 65 63 74 45 78   on the selectEx
27b16 70 61 6e 64 20 77 6f 72 6b 65 72 20 63 61 6c 6c  pand worker call
27b17 62 61 63 6b 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a  back above..**.*
27b18 2a 20 45 78 70 61 6e 64 69 6e 67 20 61 20 53 45  * Expanding a SE
27b19 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
27b1a 73 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70  s the first step
27b1b 20 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61   in processing a
27b1c 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65  .** SELECT state
27b1d 6d 65 6e 74 2e 20 20 54 68 65 20 53 45 4c 45 43  ment.  The SELEC
27b1e 54 20 73 74 61 74 65 6d 65 6e 74 20 6d 75 73 74  T statement must
27b1f 20 62 65 20 65 78 70 61 6e 64 65 64 20 62 65 66   be expanded bef
27b20 6f 72 65 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f  ore.** name reso
27b21 6c 75 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72  lution is perfor
27b22 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  med..**.** If an
27b23 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ything goes wron
27b24 67 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  g, an error mess
27b25 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  age is written i
27b26 6e 74 6f 20 70 50 61 72 73 65 2e 0a 2a 2a 20 54  nto pParse..** T
27b27 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
27b28 69 6f 6e 20 63 61 6e 20 64 65 74 65 63 74 20 74  ion can detect t
27b29 68 65 20 70 72 6f 62 6c 65 6d 20 62 79 20 6c 6f  he problem by lo
27b2a 6f 6b 69 6e 67 20 61 74 20 70 50 61 72 73 65 2d  oking at pParse-
27b2b 3e 6e 45 72 72 0a 2a 2a 20 61 6e 64 2f 6f 72 20  >nErr.** and/or 
27b2c 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
27b2d 6f 63 46 61 69 6c 65 64 2e 0a 2a 2f 0a 73 74 61  ocFailed..*/.sta
27b2e 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
27b2f 53 65 6c 65 63 74 45 78 70 61 6e 64 28 50 61 72  SelectExpand(Par
27b30 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65  se *pParse, Sele
27b31 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20  ct *pSelect){.  
27b32 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 53  Walker w;.  w.xS
27b33 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20  electCallback = 
27b34 73 65 6c 65 63 74 45 78 70 61 6e 64 65 72 3b 0a  selectExpander;.
27b35 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63    w.xExprCallbac
27b36 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70  k = exprWalkNoop
27b37 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70  ;.  w.pParse = p
27b38 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33  Parse;.  sqlite3
27b39 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70  WalkSelect(&w, p
27b3a 53 65 6c 65 63 74 29 3b 0a 7d 0a 0a 0a 23 69 66  Select);.}...#if
27b3b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27b3c 5f 53 55 42 51 55 45 52 59 0a 2f 2a 0a 2a 2a 20  _SUBQUERY./*.** 
27b3d 54 68 69 73 20 69 73 20 61 20 57 61 6c 6b 65 72  This is a Walker
27b3e 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
27b3f 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68   callback for th
27b40 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 54  e sqlite3SelectT
27b41 79 70 65 49 6e 66 6f 28 29 0a 2a 2a 20 69 6e 74  ypeInfo().** int
27b42 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f  erface..**.** Fo
27b43 72 20 65 61 63 68 20 46 52 4f 4d 2d 63 6c 61 75  r each FROM-clau
27b44 73 65 20 73 75 62 71 75 65 72 79 2c 20 61 64 64  se subquery, add
27b45 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 20 61 6e   Column.zType an
27b46 64 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 0a 2a  d Column.zColl.*
27b47 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  * information to
27b48 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
27b49 74 75 72 65 20 74 68 61 74 20 72 65 70 72 65 73  ture that repres
27b4a 65 6e 74 73 20 74 68 65 20 72 65 73 75 6c 74 20  ents the result 
27b4b 73 65 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 73  set.** of that s
27b4c 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 54  ubquery..**.** T
27b4d 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
27b4e 72 65 20 74 68 61 74 20 72 65 70 72 65 73 65 6e  re that represen
27b4f 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ts the result se
27b50 74 20 77 61 73 20 63 6f 6e 73 74 72 75 63 74 65  t was constructe
27b51 64 0a 2a 2a 20 62 79 20 73 65 6c 65 63 74 45 78  d.** by selectEx
27b52 70 61 6e 64 65 72 28 29 20 62 75 74 20 74 68 65  pander() but the
27b53 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74   type and collat
27b54 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ion information 
27b55 77 61 73 20 6f 6d 69 74 74 65 64 0a 2a 2a 20 61  was omitted.** a
27b56 74 20 74 68 61 74 20 70 6f 69 6e 74 20 62 65 63  t that point bec
27b57 61 75 73 65 20 69 64 65 6e 74 69 66 69 65 72 73  ause identifiers
27b58 20 68 61 64 20 6e 6f 74 20 79 65 74 20 62 65 65   had not yet bee
27b59 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20 54 68 69  n resolved.  Thi
27b5a 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
27b5b 63 61 6c 6c 65 64 20 61 66 74 65 72 20 69 64 65  called after ide
27b5c 6e 74 69 66 69 65 72 20 72 65 73 6f 6c 75 74 69  ntifier resoluti
27b5d 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
27b5e 74 20 73 65 6c 65 63 74 41 64 64 53 75 62 71 75  t selectAddSubqu
27b5f 65 72 79 54 79 70 65 49 6e 66 6f 28 57 61 6c 6b  eryTypeInfo(Walk
27b60 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c  er *pWalker, Sel
27b61 65 63 74 20 2a 70 29 7b 0a 20 20 50 61 72 73 65  ect *p){.  Parse
27b62 20 2a 70 50 61 72 73 65 3b 0a 20 20 69 6e 74 20   *pParse;.  int 
27b63 69 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  i;.  SrcList *pT
27b64 61 62 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74  abList;.  struct
27b65 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
27b66 46 72 6f 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28  From;..  assert(
27b67 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53   p->selFlags & S
27b68 46 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20  F_Resolved );.  
27b69 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73  if( (p->selFlags
27b6a 20 26 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66   & SF_HasTypeInf
27b6b 6f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  o)==0 ){.    p->
27b6c 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 48  selFlags |= SF_H
27b6d 61 73 54 79 70 65 49 6e 66 6f 3b 0a 20 20 20 20  asTypeInfo;.    
27b6e 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72  pParse = pWalker
27b6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 70 54  ->pParse;.    pT
27b70 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63  abList = p->pSrc
27b71 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70  ;.    for(i=0, p
27b72 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61  From=pTabList->a
27b73 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
27b74 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b  rc; i++, pFrom++
27b75 29 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a  ){.      Table *
27b76 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54  pTab = pFrom->pT
27b77 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  ab;.      if( pT
27b78 61 62 20 26 26 20 28 70 54 61 62 2d 3e 74 61 62  ab && (pTab->tab
27b79 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d  Flags & TF_Ephem
27b7a 65 72 61 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20  eral)!=0 ){.    
27b7b 20 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65      /* A sub-que
27b7c 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ry in the FROM c
27b7d 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
27b7e 54 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c  T */.        Sel
27b7f 65 63 74 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f  ect *pSel = pFro
27b80 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20  m->pSelect;.    
27b81 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
27b82 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c   );.        whil
27b83 65 28 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20  e( pSel->pPrior 
27b84 29 20 70 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70  ) pSel = pSel->p
27b85 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20 20 73  Prior;.        s
27b86 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79  electAddColumnTy
27b87 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70  peAndCollation(p
27b88 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f  Parse, pTab->nCo
27b89 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70  l, pTab->aCol, p
27b8a 53 65 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Sel);.      }.  
27b8b 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
27b8c 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
27b8d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
27b8e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64  This routine add
27b8f 73 20 64 61 74 61 74 79 70 65 20 61 6e 64 20 63  s datatype and c
27b90 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
27b91 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  e information to
27b92 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65 20 73 74  .** the Table st
27b93 72 75 63 74 75 72 65 73 20 6f 66 20 61 6c 6c 20  ructures of all 
27b94 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71  FROM-clause subq
27b95 75 65 72 69 65 73 20 69 6e 20 61 0a 2a 2a 20 53  ueries in a.** S
27b96 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
27b97 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 69 73 20  .**.** Use this 
27b98 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 6e 61  routine after na
27b99 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a  me resolution..*
27b9a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
27b9b 6c 69 74 65 33 53 65 6c 65 63 74 41 64 64 54 79  lite3SelectAddTy
27b9c 70 65 49 6e 66 6f 28 50 61 72 73 65 20 2a 70 50  peInfo(Parse *pP
27b9d 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53  arse, Select *pS
27b9e 65 6c 65 63 74 29 7b 0a 23 69 66 6e 64 65 66 20  elect){.#ifndef 
27b9f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
27ba0 55 45 52 59 0a 20 20 57 61 6c 6b 65 72 20 77 3b  UERY.  Walker w;
27ba1 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
27ba2 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 41 64 64  back = selectAdd
27ba3 53 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f  SubqueryTypeInfo
27ba4 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
27ba5 61 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f  ack = exprWalkNo
27ba6 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d  op;.  w.pParse =
27ba7 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74   pParse;.  sqlit
27ba8 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c  e3WalkSelect(&w,
27ba9 20 70 53 65 6c 65 63 74 29 3b 0a 23 65 6e 64 69   pSelect);.#endi
27baa 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  f.}.../*.** This
27bab 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 6f 66   routine sets of
27bac 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
27bad 65 6e 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69  ent for processi
27bae 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c  ng.  The.** foll
27baf 6f 77 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c  owing is accompl
27bb0 69 73 68 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ished:.**.**    
27bb1 20 2a 20 20 56 44 42 45 20 43 75 72 73 6f 72 20   *  VDBE Cursor 
27bb2 6e 75 6d 62 65 72 73 20 61 72 65 20 61 73 73 69  numbers are assi
27bb3 67 6e 65 64 20 74 6f 20 61 6c 6c 20 46 52 4f 4d  gned to all FROM
27bb4 2d 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a  -clause terms..*
27bb5 2a 20 20 20 20 20 2a 20 20 45 70 68 65 6d 65 72  *     *  Ephemer
27bb6 61 6c 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 73  al Table objects
27bb7 20 61 72 65 20 63 72 65 61 74 65 64 20 66 6f 72   are created for
27bb8 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65   all FROM-clause
27bb9 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20   subqueries..** 
27bba 20 20 20 20 2a 20 20 4f 4e 20 61 6e 64 20 55 53      *  ON and US
27bbb 49 4e 47 20 63 6c 61 75 73 65 73 20 61 72 65 20  ING clauses are 
27bbc 73 68 69 66 74 65 64 20 69 6e 74 6f 20 57 48 45  shifted into WHE
27bbd 52 45 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  RE statements.**
27bbe 20 20 20 20 20 2a 20 20 57 69 6c 64 63 61 72 64       *  Wildcard
27bbf 73 20 22 2a 22 20 61 6e 64 20 22 54 41 42 4c 45  s "*" and "TABLE
27bc0 2e 2a 22 20 69 6e 20 72 65 73 75 6c 74 20 73 65  .*" in result se
27bc1 74 73 20 61 72 65 20 65 78 70 61 6e 64 65 64 2e  ts are expanded.
27bc2 0a 2a 2a 20 20 20 20 20 2a 20 20 49 64 65 6e 74  .**     *  Ident
27bc3 69 66 69 65 72 73 20 69 6e 20 65 78 70 72 65 73  ifiers in expres
27bc4 73 69 6f 6e 20 61 72 65 20 6d 61 74 63 68 65 64  sion are matched
27bc5 20 74 6f 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a   to tables..**.*
27bc6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
27bc7 63 74 73 20 72 65 63 75 72 73 69 76 65 6c 79 20  cts recursively 
27bc8 6f 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65  on all subquerie
27bc9 73 20 77 69 74 68 69 6e 20 74 68 65 20 53 45 4c  s within the SEL
27bca 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ECT..*/.SQLITE_P
27bcb 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
27bcc 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 0a 20  te3SelectPrep(. 
27bcd 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
27bce 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
27bcf 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
27bd0 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
27bd1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
27bd2 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
27bd3 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a  t being coded. *
27bd4 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
27bd5 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61  *pOuterNC  /* Na
27bd6 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 63  me context for c
27bd7 6f 6e 74 61 69 6e 65 72 20 2a 2f 0a 29 7b 0a 20  ontainer */.){. 
27bd8 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
27bd9 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
27bda 6e 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  n;.  db = pParse
27bdb 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d 3e 73  ->db;.  if( p->s
27bdc 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 48 61 73  elFlags & SF_Has
27bdd 54 79 70 65 49 6e 66 6f 20 29 20 72 65 74 75 72  TypeInfo ) retur
27bde 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  n;.  if( pParse-
27bdf 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
27be0 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
27be1 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  rn;.  sqlite3Sel
27be2 65 63 74 45 78 70 61 6e 64 28 70 50 61 72 73 65  ectExpand(pParse
27be3 2c 20 70 29 3b 0a 20 20 69 66 28 20 70 50 61 72  , p);.  if( pPar
27be4 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
27be5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
27be6 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
27be7 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d  ResolveSelectNam
27be8 65 73 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4f  es(pParse, p, pO
27be9 75 74 65 72 4e 43 29 3b 0a 20 20 69 66 28 20 70  uterNC);.  if( p
27bea 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
27beb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
27bec 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
27bed 74 65 33 53 65 6c 65 63 74 41 64 64 54 79 70 65  te3SelectAddType
27bee 49 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 29 3b  Info(pParse, p);
27bef 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20  .}../*.** Reset 
27bf0 74 68 65 20 61 67 67 72 65 67 61 74 65 20 61 63  the aggregate ac
27bf1 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  cumulator..**.**
27bf2 20 54 68 65 20 61 67 67 72 65 67 61 74 65 20 61   The aggregate a
27bf3 63 63 75 6d 75 6c 61 74 6f 72 20 69 73 20 61 20  ccumulator is a 
27bf4 73 65 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65  set of memory ce
27bf5 6c 6c 73 20 74 68 61 74 20 68 6f 6c 64 0a 2a 2a  lls that hold.**
27bf6 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72 65   intermediate re
27bf7 73 75 6c 74 73 20 77 68 69 6c 65 20 63 61 6c 63  sults while calc
27bf8 75 6c 61 74 69 6e 67 20 61 6e 20 61 67 67 72 65  ulating an aggre
27bf9 67 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a 20 72  gate.  This.** r
27bfa 6f 75 74 69 6e 65 20 73 69 6d 70 6c 79 20 73 74  outine simply st
27bfb 6f 72 65 73 20 4e 55 4c 4c 73 20 69 6e 20 61 6c  ores NULLs in al
27bfc 6c 20 6f 66 20 74 68 6f 73 65 20 6d 65 6d 6f 72  l of those memor
27bfd 79 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74  y cells..*/.stat
27bfe 69 63 20 76 6f 69 64 20 72 65 73 65 74 41 63 63  ic void resetAcc
27bff 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65 20 2a  umulator(Parse *
27c00 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20  pParse, AggInfo 
27c01 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64  *pAggInfo){.  Vd
27c02 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
27c03 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a  pVdbe;.  int i;.
27c04 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f    struct AggInfo
27c05 5f 66 75 6e 63 20 2a 70 46 75 6e 63 3b 0a 20 20  _func *pFunc;.  
27c06 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 46  if( pAggInfo->nF
27c07 75 6e 63 2b 70 41 67 67 49 6e 66 6f 2d 3e 6e 43  unc+pAggInfo->nC
27c08 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  olumn==0 ){.    
27c09 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f  return;.  }.  fo
27c0a 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66  r(i=0; i<pAggInf
27c0b 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  o->nColumn; i++)
27c0c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
27c0d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
27c0e 6c 6c 2c 20 30 2c 20 70 41 67 67 49 6e 66 6f 2d  ll, 0, pAggInfo-
27c0f 3e 61 43 6f 6c 5b 69 5d 2e 69 4d 65 6d 29 3b 0a  >aCol[i].iMem);.
27c10 20 20 7d 0a 20 20 66 6f 72 28 70 46 75 6e 63 3d    }.  for(pFunc=
27c11 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c  pAggInfo->aFunc,
27c12 20 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f   i=0; i<pAggInfo
27c13 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46  ->nFunc; i++, pF
27c14 75 6e 63 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  unc++){.    sqli
27c15 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27c16 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 70 46 75   OP_Null, 0, pFu
27c17 6e 63 2d 3e 69 4d 65 6d 29 3b 0a 20 20 20 20 69  nc->iMem);.    i
27c18 66 28 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69  f( pFunc->iDisti
27c19 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  nct>=0 ){.      
27c1a 45 78 70 72 20 2a 70 45 20 3d 20 70 46 75 6e 63  Expr *pE = pFunc
27c1b 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69  ->pExpr;.      i
27c1c 66 28 20 70 45 2d 3e 70 4c 69 73 74 3d 3d 30 20  f( pE->pList==0 
27c1d 7c 7c 20 70 45 2d 3e 70 4c 69 73 74 2d 3e 6e 45  || pE->pList->nE
27c1e 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 20 20  xpr!=1 ){.      
27c1f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
27c20 67 28 70 50 61 72 73 65 2c 20 22 44 49 53 54 49  g(pParse, "DISTI
27c21 4e 43 54 20 69 6e 20 61 67 67 72 65 67 61 74 65  NCT in aggregate
27c22 20 6d 75 73 74 20 62 65 20 66 6f 6c 6c 6f 77 65   must be followe
27c23 64 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 22  d ".           "
27c24 62 79 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  by an expression
27c25 22 29 3b 0a 20 20 20 20 20 20 20 20 70 46 75 6e  ");.        pFun
27c26 63 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d  c->iDistinct = -
27c27 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
27c28 20 20 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20          KeyInfo 
27c29 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49  *pKeyInfo = keyI
27c2a 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  nfoFromExprList(
27c2b 70 50 61 72 73 65 2c 20 70 45 2d 3e 70 4c 69 73  pParse, pE->pLis
27c2c 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
27c2d 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
27c2e 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
27c2f 6c 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69  l, pFunc->iDisti
27c30 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  nct, 0, 0,.     
27c31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27c32 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
27c33 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  Info, P4_KEYINFO
27c34 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20  _HANDOFF);.     
27c35 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
27c36 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
27c37 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a 65 20   OP_AggFinalize 
27c38 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65 72 79  opcode for every
27c39 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
27c3a 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 41 67  ion.** in the Ag
27c3b 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  gInfo structure.
27c3c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
27c3d 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74  finalizeAggFunct
27c3e 69 6f 6e 73 28 50 61 72 73 65 20 2a 70 50 61 72  ions(Parse *pPar
27c3f 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  se, AggInfo *pAg
27c40 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a  gInfo){.  Vdbe *
27c41 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
27c42 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
27c43 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
27c44 63 20 2a 70 46 3b 0a 20 20 66 6f 72 28 69 3d 30  c *pF;.  for(i=0
27c45 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61  , pF=pAggInfo->a
27c46 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f  Func; i<pAggInfo
27c47 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46  ->nFunc; i++, pF
27c48 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73  ++){.    ExprLis
27c49 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70  t *pList = pF->p
27c4a 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20  Expr->pList;.   
27c4b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27c4c 70 34 28 76 2c 20 4f 50 5f 41 67 67 46 69 6e 61  p4(v, OP_AggFina
27c4d 6c 2c 20 70 46 2d 3e 69 4d 65 6d 2c 20 70 4c 69  l, pF->iMem, pLi
27c4e 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70  st ? pList->nExp
27c4f 72 20 3a 20 30 2c 20 30 2c 0a 20 20 20 20 20 20  r : 0, 0,.      
27c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27c51 28 76 6f 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63  (void*)pF->pFunc
27c52 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20  , P4_FUNCDEF);. 
27c53 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61   }.}../*.** Upda
27c54 74 65 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74  te the accumulat
27c55 6f 72 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  or memory cells 
27c56 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65  for an aggregate
27c57 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65   based on.** the
27c58 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20   current cursor 
27c59 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  position..*/.sta
27c5a 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 65 41  tic void updateA
27c5b 63 63 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65  ccumulator(Parse
27c5c 20 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66   *pParse, AggInf
27c5d 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20  o *pAggInfo){.  
27c5e 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
27c5f 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
27c60 3b 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e  ;.  struct AggIn
27c61 66 6f 5f 66 75 6e 63 20 2a 70 46 3b 0a 20 20 73  fo_func *pF;.  s
27c62 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f  truct AggInfo_co
27c63 6c 20 2a 70 43 3b 0a 0a 20 20 70 41 67 67 49 6e  l *pC;..  pAggIn
27c64 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d  fo->directMode =
27c65 20 31 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70   1;.  for(i=0, p
27c66 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e  F=pAggInfo->aFun
27c67 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e  c; i<pAggInfo->n
27c68 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29  Func; i++, pF++)
27c69 7b 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a  {.    int nArg;.
27c6a 20 20 20 20 69 6e 74 20 61 64 64 72 4e 65 78 74      int addrNext
27c6b 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65   = 0;.    int re
27c6c 67 41 67 67 3b 0a 20 20 20 20 45 78 70 72 4c 69  gAgg;.    ExprLi
27c6d 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e  st *pList = pF->
27c6e 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20  pExpr->pList;.  
27c6f 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20    if( pList ){. 
27c70 20 20 20 20 20 6e 41 72 67 20 3d 20 70 4c 69 73       nArg = pLis
27c71 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  t->nExpr;.      
27c72 72 65 67 41 67 67 20 3d 20 73 71 6c 69 74 65 33  regAgg = sqlite3
27c73 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
27c74 72 73 65 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20  rse, nArg);.    
27c75 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
27c76 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65  eExprList(pParse
27c77 2c 20 70 4c 69 73 74 2c 20 72 65 67 41 67 67 2c  , pList, regAgg,
27c78 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   0);.    }else{.
27c79 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a        nArg = 0;.
27c7a 20 20 20 20 20 20 72 65 67 41 67 67 20 3d 20 30        regAgg = 0
27c7b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
27c7c 70 46 2d 3e 69 44 69 73 74 69 6e 63 74 3e 3d 30  pF->iDistinct>=0
27c7d 20 29 7b 0a 20 20 20 20 20 20 61 64 64 72 4e 65   ){.      addrNe
27c7e 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  xt = sqlite3Vdbe
27c7f 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
27c80 20 20 20 20 61 73 73 65 72 74 28 20 6e 41 72 67      assert( nArg
27c81 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 63 6f 64  ==1 );.      cod
27c82 65 44 69 73 74 69 6e 63 74 28 70 50 61 72 73 65  eDistinct(pParse
27c83 2c 20 70 46 2d 3e 69 44 69 73 74 69 6e 63 74 2c  , pF->iDistinct,
27c84 20 61 64 64 72 4e 65 78 74 2c 20 31 2c 20 72 65   addrNext, 1, re
27c85 67 41 67 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20  gAgg);.    }.   
27c86 20 69 66 28 20 70 46 2d 3e 70 46 75 6e 63 2d 3e   if( pF->pFunc->
27c87 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
27c88 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a  UNC_NEEDCOLL ){.
27c89 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
27c8a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  Coll = 0;.      
27c8b 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
27c8c 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20  item *pItem;.   
27c8d 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
27c8e 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30  assert( pList!=0
27c8f 20 29 3b 20 20 2f 2a 20 70 4c 69 73 74 21 3d 30   );  /* pList!=0
27c90 20 69 66 20 70 46 2d 3e 70 46 75 6e 63 20 68 61   if pF->pFunc ha
27c91 73 20 4e 45 45 44 43 4f 4c 4c 20 2a 2f 0a 20 20  s NEEDCOLL */.  
27c92 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74      for(j=0, pIt
27c93 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 21 70 43  em=pList->a; !pC
27c94 6f 6c 6c 20 26 26 20 6a 3c 6e 41 72 67 3b 20 6a  oll && j<nArg; j
27c95 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
27c96 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71        pColl = sq
27c97 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
27c98 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e  (pParse, pItem->
27c99 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a  pExpr);.      }.
27c9a 20 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c        if( !pColl
27c9b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c   ){.        pCol
27c9c 6c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  l = pParse->db->
27c9d 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20  pDfltColl;.     
27c9e 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
27c9f 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
27ca0 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30 2c 20  _CollSeq, 0, 0, 
27ca1 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c 6c  0, (char *)pColl
27ca2 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20  , P4_COLLSEQ);. 
27ca3 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
27ca4 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
27ca5 5f 41 67 67 53 74 65 70 2c 20 30 2c 20 72 65 67  _AggStep, 0, reg
27ca6 41 67 67 2c 20 70 46 2d 3e 69 4d 65 6d 2c 0a 20  Agg, pF->iMem,. 
27ca7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ca8 20 20 20 20 20 28 76 6f 69 64 2a 29 70 46 2d 3e       (void*)pF->
27ca9 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45  pFunc, P4_FUNCDE
27caa 46 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  F);.    sqlite3V
27cab 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28  dbeChangeP5(v, (
27cac 75 38 29 6e 41 72 67 29 3b 0a 20 20 20 20 73 71  u8)nArg);.    sq
27cad 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
27cae 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
27caf 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20  gAgg, nArg);.   
27cb0 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
27cb1 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
27cb2 70 50 61 72 73 65 2c 20 72 65 67 41 67 67 2c 20  pParse, regAgg, 
27cb3 6e 41 72 67 29 3b 0a 20 20 20 20 69 66 28 20 61  nArg);.    if( a
27cb4 64 64 72 4e 65 78 74 20 29 7b 0a 20 20 20 20 20  ddrNext ){.     
27cb5 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
27cb6 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72  lveLabel(v, addr
27cb7 4e 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Next);.    }.  }
27cb8 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 3d 70  .  for(i=0, pC=p
27cb9 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69  AggInfo->aCol; i
27cba 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 41 63 63 75  <pAggInfo->nAccu
27cbb 6d 75 6c 61 74 6f 72 3b 20 69 2b 2b 2c 20 70 43  mulator; i++, pC
27cbc 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
27cbd 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
27cbe 20 70 43 2d 3e 70 45 78 70 72 2c 20 70 43 2d 3e   pC->pExpr, pC->
27cbf 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 70 41 67  iMem);.  }.  pAg
27cc0 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64  gInfo->directMod
27cc1 65 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e = 0;.}../*.** 
27cc2 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
27cc3 72 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  r the SELECT sta
27cc4 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 69 6e 20  tement given in 
27cc5 74 68 65 20 70 20 61 72 67 75 6d 65 6e 74 2e 20  the p argument. 
27cc6 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75   .**.** The resu
27cc7 6c 74 73 20 61 72 65 20 64 69 73 74 72 69 62 75  lts are distribu
27cc8 74 65 64 20 69 6e 20 76 61 72 69 6f 75 73 20 77  ted in various w
27cc9 61 79 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ays depending on
27cca 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73   the.** contents
27ccb 20 6f 66 20 74 68 65 20 53 65 6c 65 63 74 44 65   of the SelectDe
27ccc 73 74 20 73 74 72 75 63 74 75 72 65 20 70 6f 69  st structure poi
27ccd 6e 74 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d  nted to by argum
27cce 65 6e 74 20 70 44 65 73 74 0a 2a 2a 20 61 73 20  ent pDest.** as 
27ccf 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
27cd0 20 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20     pDest->eDest 
27cd1 20 20 20 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20     Result.**    
27cd2 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20   ------------   
27cd3 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
27cd4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27cd5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
27cd6 20 20 20 20 53 52 54 5f 4f 75 74 70 75 74 20 20      SRT_Output  
27cd7 20 20 20 20 47 65 6e 65 72 61 74 65 20 61 20 72      Generate a r
27cd8 6f 77 20 6f 66 20 6f 75 74 70 75 74 20 28 75 73  ow of output (us
27cd9 69 6e 67 20 74 68 65 20 4f 50 5f 52 65 73 75 6c  ing the OP_Resul
27cda 74 52 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20  tRow.**         
27cdb 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 63 6f              opco
27cdc 64 65 29 20 66 6f 72 20 65 61 63 68 20 72 6f 77  de) for each row
27cdd 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
27cde 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  et..**.**     SR
27cdf 54 5f 4d 65 6d 20 20 20 20 20 20 20 20 20 4f 6e  T_Mem         On
27ce0 6c 79 20 76 61 6c 69 64 20 69 66 20 74 68 65 20  ly valid if the 
27ce1 72 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67  result is a sing
27ce2 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 20 20  le column..**   
27ce3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ce4 20 20 53 74 6f 72 65 20 74 68 65 20 66 69 72 73    Store the firs
27ce5 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
27ce6 66 69 72 73 74 20 72 65 73 75 6c 74 20 72 6f 77  first result row
27ce7 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
27ce8 20 20 20 20 20 20 20 20 69 6e 20 72 65 67 69 73          in regis
27ce9 74 65 72 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  ter pDest->iParm
27cea 20 74 68 65 6e 20 61 62 61 6e 64 6f 6e 20 74 68   then abandon th
27ceb 65 20 72 65 73 74 0a 2a 2a 20 20 20 20 20 20 20  e rest.**       
27cec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66                of
27ced 20 74 68 65 20 71 75 65 72 79 2e 20 20 54 68 69   the query.  Thi
27cee 73 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 6d  s destination im
27cef 70 6c 69 65 73 20 22 4c 49 4d 49 54 20 31 22 2e  plies "LIMIT 1".
27cf0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 53  .**.**     SRT_S
27cf1 65 74 20 20 20 20 20 20 20 20 20 54 68 65 20 72  et         The r
27cf2 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 20  esult must be a 
27cf3 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e 20 20  single column.  
27cf4 53 74 6f 72 65 20 65 61 63 68 0a 2a 2a 20 20 20  Store each.**   
27cf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cf6 20 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20    row of result 
27cf7 61 73 20 74 68 65 20 6b 65 79 20 69 6e 20 74 61  as the key in ta
27cf8 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  ble pDest->iParm
27cf9 2e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  . .**           
27cfa 20 20 20 20 20 20 20 20 20 20 41 70 70 6c 79 20            Apply 
27cfb 74 68 65 20 61 66 66 69 6e 69 74 79 20 70 44 65  the affinity pDe
27cfc 73 74 2d 3e 61 66 66 69 6e 69 74 79 20 62 65 66  st->affinity bef
27cfd 6f 72 65 20 73 74 6f 72 69 6e 67 0a 2a 2a 20 20  ore storing.**  
27cfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cff 20 20 20 72 65 73 75 6c 74 73 2e 20 20 55 73 65     results.  Use
27d00 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22  d to implement "
27d01 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22  IN (SELECT ...)"
27d02 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
27d03 55 6e 69 6f 6e 20 20 20 20 20 20 20 53 74 6f 72  Union       Stor
27d04 65 20 72 65 73 75 6c 74 73 20 61 73 20 61 20 6b  e results as a k
27d05 65 79 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72  ey in a temporar
27d06 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69  y table pDest->i
27d07 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  Parm..**.**     
27d08 53 52 54 5f 45 78 63 65 70 74 20 20 20 20 20 20  SRT_Except      
27d09 52 65 6d 6f 76 65 20 72 65 73 75 6c 74 73 20 66  Remove results f
27d0a 72 6f 6d 20 74 68 65 20 74 65 6d 70 6f 72 61 72  rom the temporar
27d0b 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69  y table pDest->i
27d0c 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  Parm..**.**     
27d0d 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20  SRT_Table       
27d0e 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 69 6e  Store results in
27d0f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
27d10 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a   pDest->iParm..*
27d11 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
27d12 20 20 20 20 20 20 54 68 69 73 20 69 73 20 6c 69        This is li
27d13 6b 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62 20  ke SRT_EphemTab 
27d14 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
27d15 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20  table.**        
27d16 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20               is 
27d17 61 73 73 75 6d 65 64 20 74 6f 20 61 6c 72 65 61  assumed to alrea
27d18 64 79 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 0a 2a  dy be open..**.*
27d19 2a 20 20 20 20 20 53 52 54 5f 45 70 68 65 6d 54  *     SRT_EphemT
27d1a 61 62 20 20 20 20 43 72 65 61 74 65 20 61 6e 20  ab    Create an 
27d1b 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
27d1c 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 61 6e 64  pDest->iParm and
27d1d 20 73 74 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20   store.**       
27d1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
27d1f 65 20 72 65 73 75 6c 74 20 74 68 65 72 65 2e 20  e result there. 
27d20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
27d21 66 74 20 6f 70 65 6e 20 61 66 74 65 72 0a 2a 2a  ft open after.**
27d22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27d23 20 20 20 20 20 72 65 74 75 72 6e 69 6e 67 2e 20       returning. 
27d24 20 54 68 69 73 20 69 73 20 6c 69 6b 65 20 53 52   This is like SR
27d25 54 5f 54 61 62 6c 65 20 65 78 63 65 70 74 20 74  T_Table except t
27d26 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  hat.**          
27d27 20 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20             this 
27d28 64 65 73 74 69 6e 61 74 69 6f 6e 20 75 73 65 73  destination uses
27d29 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
27d2a 6c 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 20  l to create.**  
27d2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27d2c 20 20 20 74 68 65 20 74 61 62 6c 65 20 66 69 72     the table fir
27d2d 73 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  st..**.**     SR
27d2e 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 47 65  T_Coroutine   Ge
27d2f 6e 65 72 61 74 65 20 61 20 63 6f 2d 72 6f 75 74  nerate a co-rout
27d30 69 6e 65 20 74 68 61 74 20 72 65 74 75 72 6e 73  ine that returns
27d31 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 0a 2a 2a   a new row of.**
27d32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27d33 20 20 20 20 20 72 65 73 75 6c 74 73 20 65 61 63       results eac
27d34 68 20 74 69 6d 65 20 69 74 20 69 73 20 69 6e 76  h time it is inv
27d35 6f 6b 65 64 2e 20 20 54 68 65 20 65 6e 74 72 79  oked.  The entry
27d36 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20   point.**       
27d37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66                of
27d38 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20   the co-routine 
27d39 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  is stored in reg
27d3a 69 73 74 65 72 20 70 44 65 73 74 2d 3e 69 50 61  ister pDest->iPa
27d3b 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  rm..**.**     SR
27d3c 54 5f 45 78 69 73 74 73 20 20 20 20 20 20 53 74  T_Exists      St
27d3d 6f 72 65 20 61 20 31 20 69 6e 20 6d 65 6d 6f 72  ore a 1 in memor
27d3e 79 20 63 65 6c 6c 20 70 44 65 73 74 2d 3e 69 50  y cell pDest->iP
27d3f 61 72 6d 20 69 66 20 74 68 65 20 72 65 73 75 6c  arm if the resul
27d40 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
27d41 20 20 20 20 20 20 20 20 20 73 65 74 20 69 73 20           set is 
27d42 6e 6f 74 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a  not empty..**.**
27d43 20 20 20 20 20 53 52 54 5f 44 69 73 63 61 72 64       SRT_Discard
27d44 20 20 20 20 20 54 68 72 6f 77 20 74 68 65 20 72       Throw the r
27d45 65 73 75 6c 74 73 20 61 77 61 79 2e 20 20 54 68  esults away.  Th
27d46 69 73 20 69 73 20 75 73 65 64 20 62 79 20 53 45  is is used by SE
27d47 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
27d48 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74              stat
27d49 65 6d 65 6e 74 73 20 77 69 74 68 69 6e 20 74 72  ements within tr
27d4a 69 67 67 65 72 73 20 77 68 6f 73 65 20 6f 6e 6c  iggers whose onl
27d4b 79 20 70 75 72 70 6f 73 65 20 69 73 0a 2a 2a 20  y purpose is.** 
27d4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27d4d 20 20 20 20 74 68 65 20 73 69 64 65 2d 65 66 66      the side-eff
27d4e 65 63 74 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e  ects of function
27d4f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
27d50 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
27d51 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
27d52 72 73 2e 20 20 49 66 20 61 6e 79 20 65 72 72 6f  rs.  If any erro
27d53 72 73 20 61 72 65 0a 2a 2a 20 65 6e 63 6f 75 6e  rs are.** encoun
27d54 74 65 72 65 64 2c 20 74 68 65 6e 20 61 6e 20 61  tered, then an a
27d55 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72  ppropriate error
27d56 20 6d 65 73 73 61 67 65 20 69 73 20 6c 65 66 74   message is left
27d57 20 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a   in.** pParse->z
27d58 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  ErrMsg..**.** Th
27d59 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  is routine does 
27d5a 4e 4f 54 20 66 72 65 65 20 74 68 65 20 53 65 6c  NOT free the Sel
27d5b 65 63 74 20 73 74 72 75 63 74 75 72 65 20 70 61  ect structure pa
27d5c 73 73 65 64 20 69 6e 2e 20 20 54 68 65 0a 2a 2a  ssed in.  The.**
27d5d 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
27d5e 6e 20 6e 65 65 64 73 20 74 6f 20 64 6f 20 74 68  n needs to do th
27d5f 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  at..*/.SQLITE_PR
27d60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
27d61 33 53 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65  3Select(.  Parse
27d62 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
27d63 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
27d64 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
27d65 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ect *p,         
27d66 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
27d67 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e  T statement bein
27d68 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 53 65  g coded. */.  Se
27d69 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20  lectDest *pDest 
27d6a 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20       /* What to 
27d6b 64 6f 20 77 69 74 68 20 74 68 65 20 71 75 65 72  do with the quer
27d6c 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a  y results */.){.
27d6d 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20    int i, j;     
27d6e 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
27d6f 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57   counters */.  W
27d70 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f  hereInfo *pWInfo
27d71 3b 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20  ;     /* Return 
27d72 66 72 6f 6d 20 73 71 6c 69 74 65 33 57 68 65 72  from sqlite3Wher
27d73 65 42 65 67 69 6e 28 29 20 2a 2f 0a 20 20 56 64  eBegin() */.  Vd
27d74 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
27d75 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74       /* The virt
27d76 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65  ual machine unde
27d77 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a  r construction *
27d78 2f 0a 20 20 69 6e 74 20 69 73 41 67 67 3b 20 20  /.  int isAgg;  
27d79 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
27d7a 75 65 20 66 6f 72 20 73 65 6c 65 63 74 20 6c 69  ue for select li
27d7b 73 74 73 20 6c 69 6b 65 20 22 63 6f 75 6e 74 28  sts like "count(
27d7c 2a 29 22 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  *)" */.  ExprLis
27d7d 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20  t *pEList;      
27d7e 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d  /* List of colum
27d7f 6e 73 20 74 6f 20 65 78 74 72 61 63 74 2e 20 2a  ns to extract. *
27d80 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  /.  SrcList *pTa
27d81 62 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 4c 69  bList;     /* Li
27d82 73 74 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20  st of tables to 
27d83 73 65 6c 65 63 74 20 66 72 6f 6d 20 2a 2f 0a 20  select from */. 
27d84 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20   Expr *pWhere;  
27d85 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
27d86 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61  HERE clause.  Ma
27d87 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45  y be NULL */.  E
27d88 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
27d89 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44  y;    /* The ORD
27d8a 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d  ER BY clause.  M
27d8b 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ay be NULL */.  
27d8c 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70  ExprList *pGroup
27d8d 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52  By;    /* The GR
27d8e 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20  OUP BY clause.  
27d8f 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
27d90 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20   Expr *pHaving; 
27d91 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48          /* The H
27d92 41 56 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 4d  AVING clause.  M
27d93 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ay be NULL */.  
27d94 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74 3b 20  int isDistinct; 
27d95 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
27d96 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b  f the DISTINCT k
27d97 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e  eyword is presen
27d98 74 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 74 69  t */.  int disti
27d99 6e 63 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  nct;          /*
27d9a 20 54 61 62 6c 65 20 74 6f 20 75 73 65 20 66 6f   Table to use fo
27d9b 72 20 74 68 65 20 64 69 73 74 69 6e 63 74 20 73  r the distinct s
27d9c 65 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  et */.  int rc =
27d9d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   1;            /
27d9e 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
27d9f 6e 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63  n from this func
27da0 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64  tion */.  int ad
27da1 64 72 53 6f 72 74 49 6e 64 65 78 3b 20 20 20 20  drSortIndex;    
27da2 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61   /* Address of a
27da3 6e 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  n OP_OpenEphemer
27da4 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a  al instruction *
27da5 2f 0a 20 20 41 67 67 49 6e 66 6f 20 73 41 67 67  /.  AggInfo sAgg
27da6 49 6e 66 6f 3b 20 20 20 20 20 20 2f 2a 20 49 6e  Info;      /* In
27da7 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62  formation used b
27da8 79 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72  y aggregate quer
27da9 69 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e  ies */.  int iEn
27daa 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
27dab 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68  /* Address of th
27dac 65 20 65 6e 64 20 6f 66 20 74 68 65 20 71 75 65  e end of the que
27dad 72 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ry */.  sqlite3 
27dae 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f  *db;           /
27daf 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
27db0 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  onnection */..  
27db1 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
27db2 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 64  .  if( p==0 || d
27db3 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
27db4 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  || pParse->nErr 
27db5 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
27db6 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
27db7 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
27db8 73 65 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43  se, SQLITE_SELEC
27db9 54 2c 20 30 2c 20 30 2c 20 30 29 20 29 20 72 65  T, 0, 0, 0) ) re
27dba 74 75 72 6e 20 31 3b 0a 20 20 6d 65 6d 73 65 74  turn 1;.  memset
27dbb 28 26 73 41 67 67 49 6e 66 6f 2c 20 30 2c 20 73  (&sAggInfo, 0, s
27dbc 69 7a 65 6f 66 28 73 41 67 67 49 6e 66 6f 29 29  izeof(sAggInfo))
27dbd 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20  ;..  pOrderBy = 
27dbe 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69  p->pOrderBy;.  i
27dbf 66 28 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65  f( IgnorableOrde
27dc0 72 62 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20  rby(pDest) ){.  
27dc1 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20    p->pOrderBy = 
27dc2 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68  0;..    /* In th
27dc3 65 73 65 20 63 61 73 65 73 20 74 68 65 20 44 49  ese cases the DI
27dc4 53 54 49 4e 43 54 20 6f 70 65 72 61 74 6f 72 20  STINCT operator 
27dc5 6d 61 6b 65 73 20 6e 6f 20 64 69 66 66 65 72 65  makes no differe
27dc6 6e 63 65 20 74 6f 20 74 68 65 0a 20 20 20 20 2a  nce to the.    *
27dc7 2a 20 72 65 73 75 6c 74 73 2c 20 73 6f 20 72 65  * results, so re
27dc8 6d 6f 76 65 20 69 74 20 69 66 20 69 74 20 77 65  move it if it we
27dc9 72 65 20 73 70 65 63 69 66 69 65 64 2e 0a 20 20  re specified..  
27dca 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
27dcb 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
27dcc 54 5f 45 78 69 73 74 73 20 7c 7c 20 70 44 65 73  T_Exists || pDes
27dcd 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 55 6e  t->eDest==SRT_Un
27dce 69 6f 6e 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  ion || .        
27dcf 20 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d     pDest->eDest=
27dd0 3d 53 52 54 5f 45 78 63 65 70 74 20 7c 7c 20 70  =SRT_Except || p
27dd1 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54  Dest->eDest==SRT
27dd2 5f 44 69 73 63 61 72 64 29 3b 0a 20 20 20 20 70  _Discard);.    p
27dd3 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20 7e 53  ->selFlags &= ~S
27dd4 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a  F_Distinct;.  }.
27dd5 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50    sqlite3SelectP
27dd6 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c 20 30  rep(pParse, p, 0
27dd7 29 3b 0a 20 20 70 54 61 62 4c 69 73 74 20 3d 20  );.  pTabList = 
27dd8 70 2d 3e 70 53 72 63 3b 0a 20 20 70 45 4c 69 73  p->pSrc;.  pELis
27dd9 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20  t = p->pEList;. 
27dda 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
27ddb 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
27ddc 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
27ddd 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  o select_end;.  
27dde 7d 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  }.  p->pOrderBy 
27ddf 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 73  = pOrderBy;.  is
27de0 41 67 67 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61  Agg = (p->selFla
27de1 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74  gs & SF_Aggregat
27de2 65 29 21 3d 30 3b 0a 20 20 69 66 28 20 70 45 4c  e)!=0;.  if( pEL
27de3 69 73 74 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65  ist==0 ) goto se
27de4 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20  lect_end;..  /* 
27de5 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 65 76 65  .  ** Do not eve
27de6 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 67 65 6e  n attempt to gen
27de7 65 72 61 74 65 20 61 6e 79 20 63 6f 64 65 20 69  erate any code i
27de8 66 20 77 65 20 68 61 76 65 20 61 6c 72 65 61 64  f we have alread
27de9 79 20 73 65 65 6e 0a 20 20 2a 2a 20 65 72 72 6f  y seen.  ** erro
27dea 72 73 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  rs before this r
27deb 6f 75 74 69 6e 65 20 73 74 61 72 74 73 2e 0a 20  outine starts.. 
27dec 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65   */.  if( pParse
27ded 2d 3e 6e 45 72 72 3e 30 20 29 20 67 6f 74 6f 20  ->nErr>0 ) goto 
27dee 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f  select_end;..  /
27def 2a 20 4f 52 44 45 52 20 42 59 20 69 73 20 69 67  * ORDER BY is ig
27df0 6e 6f 72 65 64 20 66 6f 72 20 73 6f 6d 65 20 64  nored for some d
27df1 65 73 74 69 6e 61 74 69 6f 6e 73 2e 0a 20 20 2a  estinations..  *
27df2 2f 0a 20 20 69 66 28 20 49 67 6e 6f 72 61 62 6c  /.  if( Ignorabl
27df3 65 4f 72 64 65 72 62 79 28 70 44 65 73 74 29 20  eOrderby(pDest) 
27df4 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 20  ){.    pOrderBy 
27df5 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42  = 0;.  }..  /* B
27df6 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20  egin generating 
27df7 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d  code..  */.  v =
27df8 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
27df9 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
27dfa 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63  ==0 ) goto selec
27dfb 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 47 65 6e  t_end;..  /* Gen
27dfc 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
27dfd 6c 6c 20 73 75 62 2d 71 75 65 72 69 65 73 20 69  ll sub-queries i
27dfe 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
27dff 65 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69  e.  */.#if !defi
27e00 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
27e01 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65  SUBQUERY) || !de
27e02 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
27e03 54 5f 56 49 45 57 29 0a 20 20 66 6f 72 28 69 3d  T_VIEW).  for(i=
27e04 30 3b 20 21 70 2d 3e 70 50 72 69 6f 72 20 26 26  0; !p->pPrior &&
27e05 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   i<pTabList->nSr
27e06 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72  c; i++){.    str
27e07 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
27e08 20 2a 70 49 74 65 6d 20 3d 20 26 70 54 61 62 4c   *pItem = &pTabL
27e09 69 73 74 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 53  ist->a[i];.    S
27e0a 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a  electDest dest;.
27e0b 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62      Select *pSub
27e0c 20 3d 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63   = pItem->pSelec
27e0d 74 3b 0a 20 20 20 20 69 6e 74 20 69 73 41 67 67  t;.    int isAgg
27e0e 53 75 62 3b 0a 0a 20 20 20 20 69 66 28 20 70 53  Sub;..    if( pS
27e0f 75 62 3d 3d 30 20 7c 7c 20 70 49 74 65 6d 2d 3e  ub==0 || pItem->
27e10 69 73 50 6f 70 75 6c 61 74 65 64 20 29 20 63 6f  isPopulated ) co
27e11 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a 20  ntinue;..    /* 
27e12 49 6e 63 72 65 6d 65 6e 74 20 50 61 72 73 65 2e  Increment Parse.
27e13 6e 48 65 69 67 68 74 20 62 79 20 74 68 65 20 68  nHeight by the h
27e14 65 69 67 68 74 20 6f 66 20 74 68 65 20 6c 61 72  eight of the lar
27e15 67 65 73 74 20 65 78 70 72 65 73 73 69 6f 6e 0a  gest expression.
27e16 20 20 20 20 2a 2a 20 74 72 65 65 20 72 65 66 65      ** tree refe
27e17 72 65 64 20 74 6f 20 62 79 20 74 68 69 73 2c 20  red to by this, 
27e18 74 68 65 20 70 61 72 65 6e 74 20 73 65 6c 65 63  the parent selec
27e19 74 2e 20 54 68 65 20 63 68 69 6c 64 20 73 65 6c  t. The child sel
27e1a 65 63 74 0a 20 20 20 20 2a 2a 20 6d 61 79 20 63  ect.    ** may c
27e1b 6f 6e 74 61 69 6e 20 65 78 70 72 65 73 73 69 6f  ontain expressio
27e1c 6e 20 74 72 65 65 73 20 6f 66 20 61 74 20 6d 6f  n trees of at mo
27e1d 73 74 0a 20 20 20 20 2a 2a 20 28 53 51 4c 49 54  st.    ** (SQLIT
27e1e 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
27e1f 2d 50 61 72 73 65 2e 6e 48 65 69 67 68 74 29 20  -Parse.nHeight) 
27e20 68 65 69 67 68 74 2e 20 54 68 69 73 20 69 73 20  height. This is 
27e21 61 20 62 69 74 0a 20 20 20 20 2a 2a 20 6d 6f 72  a bit.    ** mor
27e22 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20 74  e conservative t
27e23 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2c 20 62  han necessary, b
27e24 75 74 20 6d 75 63 68 20 65 61 73 69 65 72 20 74  ut much easier t
27e25 68 61 6e 20 65 6e 66 6f 72 63 69 6e 67 0a 20 20  han enforcing.  
27e26 20 20 2a 2a 20 61 6e 20 65 78 61 63 74 20 6c 69    ** an exact li
27e27 6d 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  mit..    */.    
27e28 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20  pParse->nHeight 
27e29 2b 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  += sqlite3Select
27e2a 45 78 70 72 48 65 69 67 68 74 28 70 29 3b 0a 0a  ExprHeight(p);..
27e2b 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20      /* Check to 
27e2c 73 65 65 20 69 66 20 74 68 65 20 73 75 62 71 75  see if the subqu
27e2d 65 72 79 20 63 61 6e 20 62 65 20 61 62 73 6f 72  ery can be absor
27e2e 62 65 64 20 69 6e 74 6f 20 74 68 65 20 70 61 72  bed into the par
27e2f 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 73 41 67  ent. */.    isAg
27e30 67 53 75 62 20 3d 20 28 70 53 75 62 2d 3e 73 65  gSub = (pSub->se
27e31 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72  lFlags & SF_Aggr
27e32 65 67 61 74 65 29 21 3d 30 3b 0a 20 20 20 20 69  egate)!=0;.    i
27e33 66 28 20 66 6c 61 74 74 65 6e 53 75 62 71 75 65  f( flattenSubque
27e34 72 79 28 70 50 61 72 73 65 2c 20 70 2c 20 69 2c  ry(pParse, p, i,
27e35 20 69 73 41 67 67 2c 20 69 73 41 67 67 53 75 62   isAgg, isAggSub
27e36 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  ) ){.      if( i
27e37 73 41 67 67 53 75 62 20 29 7b 0a 20 20 20 20 20  sAggSub ){.     
27e38 20 20 20 69 73 41 67 67 20 3d 20 31 3b 0a 20 20     isAgg = 1;.  
27e39 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67        p->selFlag
27e3a 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 67 61 74  s |= SF_Aggregat
27e3b 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
27e3c 20 69 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c   i = -1;.    }el
27e3d 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
27e3e 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
27e3f 26 64 65 73 74 2c 20 53 52 54 5f 45 70 68 65 6d  &dest, SRT_Ephem
27e40 54 61 62 2c 20 70 49 74 65 6d 2d 3e 69 43 75 72  Tab, pItem->iCur
27e41 73 6f 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65  sor);.      asse
27e42 72 74 28 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70  rt( pItem->isPop
27e43 75 6c 61 74 65 64 3d 3d 30 20 29 3b 0a 20 20 20  ulated==0 );.   
27e44 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
27e45 28 70 50 61 72 73 65 2c 20 70 53 75 62 2c 20 26  (pParse, pSub, &
27e46 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70 49 74  dest);.      pIt
27e47 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20  em->isPopulated 
27e48 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 1;.    }.    i
27e49 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
27e4a 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
27e4b 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  led ){.      got
27e4c 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  o select_end;.  
27e4d 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e    }.    pParse->
27e4e 6e 48 65 69 67 68 74 20 2d 3d 20 73 71 6c 69 74  nHeight -= sqlit
27e4f 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67  e3SelectExprHeig
27e50 68 74 28 70 29 3b 0a 20 20 20 20 70 54 61 62 4c  ht(p);.    pTabL
27e51 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  ist = p->pSrc;. 
27e52 20 20 20 69 66 28 20 21 49 67 6e 6f 72 61 62 6c     if( !Ignorabl
27e53 65 4f 72 64 65 72 62 79 28 70 44 65 73 74 29 20  eOrderby(pDest) 
27e54 29 7b 0a 20 20 20 20 20 20 70 4f 72 64 65 72 42  ){.      pOrderB
27e55 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b  y = p->pOrderBy;
27e56 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 45 4c  .    }.  }.  pEL
27e57 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b  ist = p->pEList;
27e58 0a 23 65 6e 64 69 66 0a 20 20 70 57 68 65 72 65  .#endif.  pWhere
27e59 20 3d 20 70 2d 3e 70 57 68 65 72 65 3b 0a 20 20   = p->pWhere;.  
27e5a 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47  pGroupBy = p->pG
27e5b 72 6f 75 70 42 79 3b 0a 20 20 70 48 61 76 69 6e  roupBy;.  pHavin
27e5c 67 20 3d 20 70 2d 3e 70 48 61 76 69 6e 67 3b 0a  g = p->pHaving;.
27e5d 20 20 69 73 44 69 73 74 69 6e 63 74 20 3d 20 28    isDistinct = (
27e5e 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
27e5f 5f 44 69 73 74 69 6e 63 74 29 21 3d 30 3b 0a 0a  _Distinct)!=0;..
27e60 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
27e61 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
27e62 45 43 54 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  ECT.  /* If ther
27e63 65 20 69 73 20 61 72 65 20 61 20 73 65 71 75 65  e is are a seque
27e64 6e 63 65 20 6f 66 20 71 75 65 72 69 65 73 2c 20  nce of queries, 
27e65 64 6f 20 74 68 65 20 65 61 72 6c 69 65 72 20 6f  do the earlier o
27e66 6e 65 73 20 66 69 72 73 74 2e 0a 20 20 2a 2f 0a  nes first..  */.
27e67 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20    if( p->pPrior 
27e68 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 52  ){.    if( p->pR
27e69 69 67 68 74 6d 6f 73 74 3d 3d 30 20 29 7b 0a 20  ightmost==0 ){. 
27e6a 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 4c 6f       Select *pLo
27e6b 6f 70 2c 20 2a 70 52 69 67 68 74 20 3d 20 30 3b  op, *pRight = 0;
27e6c 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d  .      int cnt =
27e6d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 6d 78   0;.      int mx
27e6e 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 66 6f  Select;.      fo
27e6f 72 28 70 4c 6f 6f 70 3d 70 3b 20 70 4c 6f 6f 70  r(pLoop=p; pLoop
27e70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70  ; pLoop=pLoop->p
27e71 50 72 69 6f 72 2c 20 63 6e 74 2b 2b 29 7b 0a 20  Prior, cnt++){. 
27e72 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 70 52         pLoop->pR
27e73 69 67 68 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20  ightmost = p;.  
27e74 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 70 4e 65        pLoop->pNe
27e75 78 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20  xt = pRight;.   
27e76 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c       pRight = pL
27e77 6f 6f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oop;.      }.   
27e78 20 20 20 6d 78 53 65 6c 65 63 74 20 3d 20 64 62     mxSelect = db
27e79 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
27e7a 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  LIMIT_COMPOUND_S
27e7b 45 4c 45 43 54 5d 3b 0a 20 20 20 20 20 20 69 66  ELECT];.      if
27e7c 28 20 6d 78 53 65 6c 65 63 74 20 26 26 20 63 6e  ( mxSelect && cn
27e7d 74 3e 6d 78 53 65 6c 65 63 74 20 29 7b 0a 20 20  t>mxSelect ){.  
27e7e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
27e7f 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
27e80 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e  oo many terms in
27e81 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
27e82 22 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  ");.        retu
27e83 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
27e84 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6d    }.    return m
27e85 75 6c 74 69 53 65 6c 65 63 74 28 70 50 61 72 73  ultiSelect(pPars
27e86 65 2c 20 70 2c 20 70 44 65 73 74 29 3b 0a 20 20  e, p, pDest);.  
27e87 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
27e88 66 20 77 72 69 74 69 6e 67 20 74 6f 20 6d 65 6d  f writing to mem
27e89 6f 72 79 20 6f 72 20 67 65 6e 65 72 61 74 69 6e  ory or generatin
27e8a 67 20 61 20 73 65 74 0a 20 20 2a 2a 20 6f 6e 6c  g a set.  ** onl
27e8b 79 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  y a single colum
27e8c 6e 20 6d 61 79 20 62 65 20 6f 75 74 70 75 74 2e  n may be output.
27e8d 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
27e8e 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
27e8f 52 59 0a 20 20 69 66 28 20 63 68 65 63 6b 46 6f  RY.  if( checkFo
27e90 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65  rMultiColumnSele
27e91 63 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20  ctError(pParse, 
27e92 70 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e  pDest, pEList->n
27e93 45 78 70 72 29 20 29 7b 0a 20 20 20 20 67 6f 74  Expr) ){.    got
27e94 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  o select_end;.  
27e95 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
27e96 66 20 70 6f 73 73 69 62 6c 65 2c 20 72 65 77 72  f possible, rewr
27e97 69 74 65 20 74 68 65 20 71 75 65 72 79 20 74 6f  ite the query to
27e98 20 75 73 65 20 47 52 4f 55 50 20 42 59 20 69 6e   use GROUP BY in
27e99 73 74 65 61 64 20 6f 66 20 44 49 53 54 49 4e 43  stead of DISTINC
27e9a 54 2e 0a 20 20 2a 2a 20 47 52 4f 55 50 20 42 59  T..  ** GROUP BY
27e9b 20 6d 69 67 68 74 20 75 73 65 20 61 6e 20 69 6e   might use an in
27e9c 64 65 78 2c 20 44 49 53 54 49 4e 43 54 20 6e 65  dex, DISTINCT ne
27e9d 76 65 72 20 64 6f 65 73 2e 0a 20 20 2a 2f 0a 20  ver does..  */. 
27e9e 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67   if( (p->selFlag
27e9f 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74  s & (SF_Distinct
27ea0 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d  |SF_Aggregate))=
27ea1 3d 53 46 5f 44 69 73 74 69 6e 63 74 20 26 26 20  =SF_Distinct && 
27ea2 21 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a  !p->pGroupBy ){.
27ea3 20 20 20 20 70 2d 3e 70 47 72 6f 75 70 42 79 20      p->pGroupBy 
27ea4 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
27ea5 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69  tDup(db, p->pELi
27ea6 73 74 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 42  st);.    pGroupB
27ea7 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b  y = p->pGroupBy;
27ea8 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73  .    p->selFlags
27ea9 20 26 3d 20 7e 53 46 5f 44 69 73 74 69 6e 63 74   &= ~SF_Distinct
27eaa 3b 0a 20 20 20 20 69 73 44 69 73 74 69 6e 63 74  ;.    isDistinct
27eab 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
27eac 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f  If there is an O
27ead 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20  RDER BY clause, 
27eae 74 68 65 6e 20 74 68 69 73 20 73 6f 72 74 69 6e  then this sortin
27eaf 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6d 69 67  g.  ** index mig
27eb0 68 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20  ht end up being 
27eb1 75 6e 75 73 65 64 20 69 66 20 74 68 65 20 64 61  unused if the da
27eb2 74 61 20 63 61 6e 20 62 65 20 0a 20 20 2a 2a 20  ta can be .  ** 
27eb3 65 78 74 72 61 63 74 65 64 20 69 6e 20 70 72 65  extracted in pre
27eb4 2d 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20  -sorted order.  
27eb5 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63  If that is the c
27eb6 61 73 65 2c 20 74 68 65 6e 20 74 68 65 0a 20 20  ase, then the.  
27eb7 2a 2a 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  ** OP_OpenEpheme
27eb8 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ral instruction 
27eb9 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 20  will be changed 
27eba 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 20 6f 6e  to an OP_Noop on
27ebb 63 65 0a 20 20 2a 2a 20 77 65 20 66 69 67 75 72  ce.  ** we figur
27ebc 65 20 6f 75 74 20 74 68 61 74 20 74 68 65 20 73  e out that the s
27ebd 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 69 73 20  orting index is 
27ebe 6e 6f 74 20 6e 65 65 64 65 64 2e 20 20 54 68 65  not needed.  The
27ebf 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 0a 20   addrSortIndex. 
27ec0 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20   ** variable is 
27ec1 75 73 65 64 20 74 6f 20 66 61 63 69 6c 69 74 61  used to facilita
27ec2 74 65 20 74 68 61 74 20 63 68 61 6e 67 65 2e 0a  te that change..
27ec3 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65    */.  if( pOrde
27ec4 72 42 79 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e  rBy ){.    KeyIn
27ec5 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  fo *pKeyInfo;.  
27ec6 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79    pKeyInfo = key
27ec7 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74  InfoFromExprList
27ec8 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
27ec9 79 29 3b 0a 20 20 20 20 70 4f 72 64 65 72 42 79  y);.    pOrderBy
27eca 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 70 50 61  ->iECursor = pPa
27ecb 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
27ecc 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
27ecd 5b 32 5d 20 3d 20 61 64 64 72 53 6f 72 74 49 6e  [2] = addrSortIn
27ece 64 65 78 20 3d 0a 20 20 20 20 20 20 73 71 6c 69  dex =.      sqli
27ecf 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
27ed0 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
27ed1 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
27ed2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f                pO
27ed3 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72  rderBy->iECursor
27ed4 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  , pOrderBy->nExp
27ed5 72 2b 32 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  r+2, 0,.        
27ed6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ed7 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e     (char*)pKeyIn
27ed8 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  fo, P4_KEYINFO_H
27ed9 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65  ANDOFF);.  }else
27eda 7b 0a 20 20 20 20 61 64 64 72 53 6f 72 74 49 6e  {.    addrSortIn
27edb 64 65 78 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20  dex = -1;.  }.. 
27edc 20 2f 2a 20 49 66 20 74 68 65 20 6f 75 74 70 75   /* If the outpu
27edd 74 20 69 73 20 64 65 73 74 69 6e 65 64 20 66 6f  t is destined fo
27ede 72 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  r a temporary ta
27edf 62 6c 65 2c 20 6f 70 65 6e 20 74 68 61 74 20 74  ble, open that t
27ee0 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
27ee1 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
27ee2 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a 20  RT_EphemTab ){. 
27ee3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27ee4 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
27ee5 70 68 65 6d 65 72 61 6c 2c 20 70 44 65 73 74 2d  phemeral, pDest-
27ee6 3e 69 50 61 72 6d 2c 20 70 45 4c 69 73 74 2d 3e  >iParm, pEList->
27ee7 6e 45 78 70 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f  nExpr);.  }..  /
27ee8 2a 20 53 65 74 20 74 68 65 20 6c 69 6d 69 74 65  * Set the limite
27ee9 72 2e 0a 20 20 2a 2f 0a 20 20 69 45 6e 64 20 3d  r..  */.  iEnd =
27eea 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
27eeb 4c 61 62 65 6c 28 76 29 3b 0a 20 20 63 6f 6d 70  Label(v);.  comp
27eec 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72  uteLimitRegister
27eed 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69 45 6e  s(pParse, p, iEn
27eee 64 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61  d);..  /* Open a
27eef 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20 74   virtual index t
27ef0 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 64 69  o use for the di
27ef1 73 74 69 6e 63 74 20 73 65 74 2e 0a 20 20 2a 2f  stinct set..  */
27ef2 0a 20 20 69 66 28 20 69 73 44 69 73 74 69 6e 63  .  if( isDistinc
27ef3 74 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f  t ){.    KeyInfo
27ef4 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20   *pKeyInfo;.    
27ef5 61 73 73 65 72 74 28 20 69 73 41 67 67 20 7c 7c  assert( isAgg ||
27ef6 20 70 47 72 6f 75 70 42 79 20 29 3b 0a 20 20 20   pGroupBy );.   
27ef7 20 64 69 73 74 69 6e 63 74 20 3d 20 70 50 61 72   distinct = pPar
27ef8 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
27ef9 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e  pKeyInfo = keyIn
27efa 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70  foFromExprList(p
27efb 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69 73 74  Parse, p->pEList
27efc 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
27efd 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
27efe 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64 69  penEphemeral, di
27eff 73 74 69 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20  stinct, 0, 0,.  
27f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f01 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
27f02 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
27f03 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65  O_HANDOFF);.  }e
27f04 6c 73 65 7b 0a 20 20 20 20 64 69 73 74 69 6e 63  lse{.    distinc
27f05 74 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f  t = -1;.  }..  /
27f06 2a 20 41 67 67 72 65 67 61 74 65 20 61 6e 64 20  * Aggregate and 
27f07 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75  non-aggregate qu
27f08 65 72 69 65 73 20 61 72 65 20 68 61 6e 64 6c 65  eries are handle
27f09 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 2a 2f  d differently */
27f0a 0a 20 20 69 66 28 20 21 69 73 41 67 67 20 26 26  .  if( !isAgg &&
27f0b 20 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 7b 0a   pGroupBy==0 ){.
27f0c 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65      /* This case
27f0d 20 69 73 20 66 6f 72 20 6e 6f 6e 2d 61 67 67 72   is for non-aggr
27f0e 65 67 61 74 65 20 71 75 65 72 69 65 73 0a 20 20  egate queries.  
27f0f 20 20 2a 2a 20 42 65 67 69 6e 20 74 68 65 20 64    ** Begin the d
27f10 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20 20  atabase scan.   
27f11 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d   */.    pWInfo =
27f12 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
27f13 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  in(pParse, pTabL
27f14 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70 4f  ist, pWhere, &pO
27f15 72 64 65 72 42 79 2c 20 30 2c 20 30 29 3b 0a 20  rderBy, 0, 0);. 
27f16 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30     if( pWInfo==0
27f17 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65   ) goto select_e
27f18 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 73  nd;..    /* If s
27f19 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61  orting index tha
27f1a 74 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79  t was created by
27f1b 20 61 20 70 72 69 6f 72 20 4f 50 5f 4f 70 65 6e   a prior OP_Open
27f1c 45 70 68 65 6d 65 72 61 6c 20 0a 20 20 20 20 2a  Ephemeral .    *
27f1d 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 6e  * instruction en
27f1e 64 65 64 20 75 70 20 6e 6f 74 20 62 65 69 6e 67  ded up not being
27f1f 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 63 68   needed, then ch
27f20 61 6e 67 65 20 74 68 65 20 4f 50 5f 4f 70 65 6e  ange the OP_Open
27f21 45 70 68 65 6d 65 72 61 6c 0a 20 20 20 20 2a 2a  Ephemeral.    **
27f22 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70   into an OP_Noop
27f23 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
27f24 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 3e 3d   addrSortIndex>=
27f25 30 20 26 26 20 70 4f 72 64 65 72 42 79 3d 3d 30  0 && pOrderBy==0
27f26 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
27f27 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f  3VdbeChangeToNoo
27f28 70 28 76 2c 20 61 64 64 72 53 6f 72 74 49 6e 64  p(v, addrSortInd
27f29 65 78 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 2d  ex, 1);.      p-
27f2a 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d  >addrOpenEphm[2]
27f2b 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20   = -1;.    }..  
27f2c 20 20 2f 2a 20 55 73 65 20 74 68 65 20 73 74 61    /* Use the sta
27f2d 6e 64 61 72 64 20 69 6e 6e 65 72 20 6c 6f 6f 70  ndard inner loop
27f2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
27f2f 72 74 28 21 69 73 44 69 73 74 69 6e 63 74 29 3b  rt(!isDistinct);
27f30 0a 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72  .    selectInner
27f31 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20  Loop(pParse, p, 
27f32 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70 4f  pEList, 0, 0, pO
27f33 72 64 65 72 42 79 2c 20 2d 31 2c 20 70 44 65 73  rderBy, -1, pDes
27f34 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
27f35 20 20 20 20 20 20 20 70 57 49 6e 66 6f 2d 3e 69         pWInfo->i
27f36 43 6f 6e 74 69 6e 75 65 2c 20 70 57 49 6e 66 6f  Continue, pWInfo
27f37 2d 3e 69 42 72 65 61 6b 29 3b 0a 0a 20 20 20 20  ->iBreak);..    
27f38 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74 61 62  /* End the datab
27f39 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20  ase scan loop.. 
27f3a 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
27f3b 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
27f3c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
27f3d 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 70  /* This is the p
27f3e 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61 67  rocessing for ag
27f3f 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73 20  gregate queries 
27f40 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65  */.    NameConte
27f41 78 74 20 73 4e 43 3b 20 20 20 20 2f 2a 20 4e 61  xt sNC;    /* Na
27f42 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70  me context for p
27f43 72 6f 63 65 73 73 69 6e 67 20 61 67 67 72 65 67  rocessing aggreg
27f44 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ate information 
27f45 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 4d 65 6d  */.    int iAMem
27f46 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ;          /* Fi
27f47 72 73 74 20 4d 65 6d 20 61 64 64 72 65 73 73 20  rst Mem address 
27f48 66 6f 72 20 73 74 6f 72 69 6e 67 20 63 75 72 72  for storing curr
27f49 65 6e 74 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a  ent GROUP BY */.
27f4a 20 20 20 20 69 6e 74 20 69 42 4d 65 6d 3b 20 20      int iBMem;  
27f4b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
27f4c 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72   Mem address for
27f4d 20 70 72 65 76 69 6f 75 73 20 47 52 4f 55 50 20   previous GROUP 
27f4e 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 55  BY */.    int iU
27f4f 73 65 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a  seFlag;       /*
27f50 20 4d 65 6d 20 61 64 64 72 65 73 73 20 68 6f 6c   Mem address hol
27f51 64 69 6e 67 20 66 6c 61 67 20 69 6e 64 69 63 61  ding flag indica
27f52 74 69 6e 67 20 74 68 61 74 20 61 74 20 6c 65 61  ting that at lea
27f53 73 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  st.             
27f54 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e             ** on
27f55 65 20 72 6f 77 20 6f 66 20 74 68 65 20 69 6e 70  e row of the inp
27f56 75 74 20 74 6f 20 74 68 65 20 61 67 67 72 65 67  ut to the aggreg
27f57 61 74 6f 72 20 68 61 73 20 62 65 65 6e 0a 20 20  ator has been.  
27f58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f59 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73        ** process
27f5a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41  ed */.    int iA
27f5b 62 6f 72 74 46 6c 61 67 3b 20 20 20 20 20 2f 2a  bortFlag;     /*
27f5c 20 4d 65 6d 20 61 64 64 72 65 73 73 20 77 68 69   Mem address whi
27f5d 63 68 20 63 61 75 73 65 73 20 71 75 65 72 79 20  ch causes query 
27f5e 61 62 6f 72 74 20 69 66 20 70 6f 73 69 74 69 76  abort if positiv
27f5f 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 67 72 6f  e */.    int gro
27f60 75 70 42 79 53 6f 72 74 3b 20 20 20 20 2f 2a 20  upBySort;    /* 
27f61 52 6f 77 73 20 63 6f 6d 65 20 66 72 6f 6d 20 73  Rows come from s
27f62 6f 75 72 63 65 20 69 6e 20 47 52 4f 55 50 20 42  ource in GROUP B
27f63 59 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 69  Y order */.    i
27f64 6e 74 20 61 64 64 72 45 6e 64 3b 20 20 20 20 20  nt addrEnd;     
27f65 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 72 6f     /* End of pro
27f66 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73  cessing for this
27f67 20 53 45 4c 45 43 54 20 2a 2f 0a 0a 20 20 20 20   SELECT */..    
27f68 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 61 6e  /* Remove any an
27f69 64 20 61 6c 6c 20 61 6c 69 61 73 65 73 20 62 65  d all aliases be
27f6a 74 77 65 65 6e 20 74 68 65 20 72 65 73 75 6c 74  tween the result
27f6b 20 73 65 74 20 61 6e 64 20 74 68 65 0a 20 20 20   set and the.   
27f6c 20 2a 2a 20 47 52 4f 55 50 20 42 59 20 63 6c 61   ** GROUP BY cla
27f6d 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  use..    */.    
27f6e 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a  if( pGroupBy ){.
27f6f 20 20 20 20 20 20 69 6e 74 20 6b 3b 20 20 20 20        int k;    
27f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f71 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
27f72 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 73 74 72  ter */.      str
27f73 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
27f74 6d 20 2a 70 49 74 65 6d 3b 20 20 2f 2a 20 46 6f  m *pItem;  /* Fo
27f75 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65  r looping over e
27f76 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 61 20 6c  xpression in a l
27f77 69 73 74 20 2a 2f 0a 0a 20 20 20 20 20 20 66 6f  ist */..      fo
27f78 72 28 6b 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  r(k=p->pEList->n
27f79 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 70  Expr, pItem=p->p
27f7a 45 4c 69 73 74 2d 3e 61 3b 20 6b 3e 30 3b 20 6b  EList->a; k>0; k
27f7b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
27f7c 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c        pItem->iAl
27f7d 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ias = 0;.      }
27f7e 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70 47 72  .      for(k=pGr
27f7f 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2c 20 70 49  oupBy->nExpr, pI
27f80 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b  tem=pGroupBy->a;
27f81 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70 49 74 65 6d   k>0; k--, pItem
27f82 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 49 74  ++){.        pIt
27f83 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a  em->iAlias = 0;.
27f84 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
27f85 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61  .    /* Create a
27f86 20 6c 61 62 65 6c 20 74 6f 20 6a 75 6d 70 20 74   label to jump t
27f87 6f 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74  o when we want t
27f88 6f 20 61 62 6f 72 74 20 74 68 65 20 71 75 65 72  o abort the quer
27f89 79 20 2a 2f 0a 20 20 20 20 61 64 64 72 45 6e 64  y */.    addrEnd
27f8a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
27f8b 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20  keLabel(v);..   
27f8c 20 2f 2a 20 43 6f 6e 76 65 72 74 20 54 4b 5f 43   /* Convert TK_C
27f8d 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20 69 6e 74 6f  OLUMN nodes into
27f8e 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61   TK_AGG_COLUMN a
27f8f 6e 64 20 6d 61 6b 65 20 65 6e 74 72 69 65 73 20  nd make entries 
27f90 69 6e 0a 20 20 20 20 2a 2a 20 73 41 67 67 49 6e  in.    ** sAggIn
27f91 66 6f 20 66 6f 72 20 61 6c 6c 20 54 4b 5f 41 47  fo for all TK_AG
27f92 47 5f 46 55 4e 43 54 49 4f 4e 20 6e 6f 64 65 73  G_FUNCTION nodes
27f93 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20   in expressions 
27f94 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 53 45  of the.    ** SE
27f95 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
27f96 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65      */.    memse
27f97 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f  t(&sNC, 0, sizeo
27f98 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43  f(sNC));.    sNC
27f99 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
27f9a 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69  ;.    sNC.pSrcLi
27f9b 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20  st = pTabList;. 
27f9c 20 20 20 73 4e 43 2e 70 41 67 67 49 6e 66 6f 20     sNC.pAggInfo 
27f9d 3d 20 26 73 41 67 67 49 6e 66 6f 3b 0a 20 20 20  = &sAggInfo;.   
27f9e 20 73 41 67 67 49 6e 66 6f 2e 6e 53 6f 72 74 69   sAggInfo.nSorti
27f9f 6e 67 43 6f 6c 75 6d 6e 20 3d 20 70 47 72 6f 75  ngColumn = pGrou
27fa0 70 42 79 20 3f 20 70 47 72 6f 75 70 42 79 2d 3e  pBy ? pGroupBy->
27fa1 6e 45 78 70 72 2b 31 20 3a 20 30 3b 0a 20 20 20  nExpr+1 : 0;.   
27fa2 20 73 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70   sAggInfo.pGroup
27fa3 42 79 20 3d 20 70 47 72 6f 75 70 42 79 3b 0a 20  By = pGroupBy;. 
27fa4 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e     sqlite3ExprAn
27fa5 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e  alyzeAggList(&sN
27fa6 43 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  C, pEList);.    
27fa7 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
27fa8 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20  zeAggList(&sNC, 
27fa9 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 69  pOrderBy);.    i
27faa 66 28 20 70 48 61 76 69 6e 67 20 29 7b 0a 20 20  f( pHaving ){.  
27fab 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41      sqlite3ExprA
27fac 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
27fad 28 26 73 4e 43 2c 20 70 48 61 76 69 6e 67 29 3b  (&sNC, pHaving);
27fae 0a 20 20 20 20 7d 0a 20 20 20 20 73 41 67 67 49  .    }.    sAggI
27faf 6e 66 6f 2e 6e 41 63 63 75 6d 75 6c 61 74 6f 72  nfo.nAccumulator
27fb0 20 3d 20 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c   = sAggInfo.nCol
27fb1 75 6d 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  umn;.    for(i=0
27fb2 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 46 75  ; i<sAggInfo.nFu
27fb3 6e 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  nc; i++){.      
27fb4 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
27fb5 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20  zeAggList(&sNC, 
27fb6 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69  sAggInfo.aFunc[i
27fb7 5d 2e 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 3b  ].pExpr->pList);
27fb8 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
27fb9 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
27fba 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e  ) goto select_en
27fbb 64 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65  d;..    /* Proce
27fbc 73 73 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67  ssing for aggreg
27fbd 61 74 65 73 20 77 69 74 68 20 47 52 4f 55 50 20  ates with GROUP 
27fbe 42 59 20 69 73 20 76 65 72 79 20 64 69 66 66 65  BY is very diffe
27fbf 72 65 6e 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20  rent and.    ** 
27fc0 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 65  much more comple
27fc1 78 20 74 68 61 6e 20 61 67 67 72 65 67 61 74 65  x than aggregate
27fc2 73 20 77 69 74 68 6f 75 74 20 61 20 47 52 4f 55  s without a GROU
27fc3 50 20 42 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  P BY..    */.   
27fc4 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b   if( pGroupBy ){
27fc5 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  .      KeyInfo *
27fc6 70 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 4b 65  pKeyInfo;  /* Ke
27fc7 79 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ying information
27fc8 20 66 6f 72 20 74 68 65 20 67 72 6f 75 70 20 62   for the group b
27fc9 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  y clause */.    
27fca 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20    int j1;       
27fcb 20 20 20 20 20 20 2f 2a 20 41 2d 76 73 2d 42 20        /* A-vs-B 
27fcc 63 6f 6d 70 61 72 69 73 69 6f 6e 20 6a 75 6d 70  comparision jump
27fcd 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
27fce 64 72 4f 75 74 70 75 74 52 6f 77 3b 20 20 2f 2a  drOutputRow;  /*
27fcf 20 53 74 61 72 74 20 6f 66 20 73 75 62 72 6f 75   Start of subrou
27fd0 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74  tine that output
27fd1 73 20 61 20 72 65 73 75 6c 74 20 72 6f 77 20 2a  s a result row *
27fd2 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 4f  /.      int regO
27fd3 75 74 70 75 74 52 6f 77 3b 20 20 20 2f 2a 20 52  utputRow;   /* R
27fd4 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 72 65  eturn address re
27fd5 67 69 73 74 65 72 20 66 6f 72 20 6f 75 74 70 75  gister for outpu
27fd6 74 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  t subroutine */.
27fd7 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 53 65        int addrSe
27fd8 74 41 62 6f 72 74 3b 20 20 20 2f 2a 20 53 65 74  tAbort;   /* Set
27fd9 20 74 68 65 20 61 62 6f 72 74 20 66 6c 61 67 20   the abort flag 
27fda 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20  and return */.  
27fdb 20 20 20 20 69 6e 74 20 61 64 64 72 54 6f 70 4f      int addrTopO
27fdc 66 4c 6f 6f 70 3b 20 20 2f 2a 20 54 6f 70 20 6f  fLoop;  /* Top o
27fdd 66 20 74 68 65 20 69 6e 70 75 74 20 6c 6f 6f 70  f the input loop
27fde 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
27fdf 64 72 53 6f 72 74 69 6e 67 49 64 78 3b 20 2f 2a  drSortingIdx; /*
27fe0 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65   The OP_OpenEphe
27fe1 6d 65 72 61 6c 20 66 6f 72 20 74 68 65 20 73 6f  meral for the so
27fe2 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20  rting index */. 
27fe3 20 20 20 20 20 69 6e 74 20 61 64 64 72 52 65 73       int addrRes
27fe4 65 74 3b 20 20 20 20 20 20 2f 2a 20 53 75 62 72  et;      /* Subr
27fe5 6f 75 74 69 6e 65 20 66 6f 72 20 72 65 73 65 74  outine for reset
27fe6 74 69 6e 67 20 74 68 65 20 61 63 63 75 6d 75 6c  ting the accumul
27fe7 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ator */.      in
27fe8 74 20 72 65 67 52 65 73 65 74 3b 20 20 20 20 20  t regReset;     
27fe9 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64 72    /* Return addr
27fea 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72  ess register for
27feb 20 72 65 73 65 74 20 73 75 62 72 6f 75 74 69 6e   reset subroutin
27fec 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49  e */..      /* I
27fed 66 20 74 68 65 72 65 20 69 73 20 61 20 47 52 4f  f there is a GRO
27fee 55 50 20 42 59 20 63 6c 61 75 73 65 20 77 65 20  UP BY clause we 
27fef 6d 69 67 68 74 20 6e 65 65 64 20 61 20 73 6f 72  might need a sor
27ff0 74 69 6e 67 20 69 6e 64 65 78 20 74 6f 0a 20 20  ting index to.  
27ff1 20 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74      ** implement
27ff2 20 69 74 2e 20 20 41 6c 6c 6f 63 61 74 65 20 74   it.  Allocate t
27ff3 68 61 74 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  hat sorting inde
27ff4 78 20 6e 6f 77 2e 20 20 49 66 20 69 74 20 74 75  x now.  If it tu
27ff5 72 6e 73 20 6f 75 74 0a 20 20 20 20 20 20 2a 2a  rns out.      **
27ff6 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20   that we do not 
27ff7 6e 65 65 64 20 69 74 20 61 66 74 65 72 20 61 6c  need it after al
27ff8 6c 2c 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d  l, the OpenEphem
27ff9 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  eral instruction
27ffa 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  .      ** will b
27ffb 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  e converted into
27ffc 20 61 20 4e 6f 6f 70 2e 20 20 0a 20 20 20 20 20   a Noop.  .     
27ffd 20 2a 2f 0a 20 20 20 20 20 20 73 41 67 67 49 6e   */.      sAggIn
27ffe 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 20 3d 20  fo.sortingIdx = 
27fff 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
28000 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d        pKeyInfo =
28001 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72   keyInfoFromExpr
28002 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 47 72  List(pParse, pGr
28003 6f 75 70 42 79 29 3b 0a 20 20 20 20 20 20 61 64  oupBy);.      ad
28004 64 72 53 6f 72 74 69 6e 67 49 64 78 20 3d 20 73  drSortingIdx = s
28005 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
28006 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  (v, OP_OpenEphem
28007 65 72 61 6c 2c 20 0a 20 20 20 20 20 20 20 20 20  eral, .         
28008 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
28009 67 49 64 78 2c 20 73 41 67 67 49 6e 66 6f 2e 6e  gIdx, sAggInfo.n
2800a 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2c 20 0a  SortingColumn, .
2800b 20 20 20 20 20 20 20 20 20 20 30 2c 20 28 63 68            0, (ch
2800c 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34  ar*)pKeyInfo, P4
2800d 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
2800e 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 69  );..      /* Ini
2800f 74 69 61 6c 69 7a 65 20 6d 65 6d 6f 72 79 20 6c  tialize memory l
28010 6f 63 61 74 69 6f 6e 73 20 75 73 65 64 20 62 79  ocations used by
28011 20 47 52 4f 55 50 20 42 59 20 61 67 67 72 65 67   GROUP BY aggreg
28012 61 74 65 20 70 72 6f 63 65 73 73 69 6e 67 0a 20  ate processing. 
28013 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 55       */.      iU
28014 73 65 46 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73  seFlag = ++pPars
28015 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69  e->nMem;.      i
28016 41 62 6f 72 74 46 6c 61 67 20 3d 20 2b 2b 70 50  AbortFlag = ++pP
28017 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
28018 20 20 72 65 67 4f 75 74 70 75 74 52 6f 77 20 3d    regOutputRow =
28019 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2801a 0a 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75  .      addrOutpu
2801b 74 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64  tRow = sqlite3Vd
2801c 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2801d 20 20 20 20 20 20 72 65 67 52 65 73 65 74 20 3d        regReset =
2801e 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2801f 0a 20 20 20 20 20 20 61 64 64 72 52 65 73 65 74  .      addrReset
28020 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
28021 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
28022 20 20 69 41 4d 65 6d 20 3d 20 70 50 61 72 73 65    iAMem = pParse
28023 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20  ->nMem + 1;.    
28024 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
28025 3d 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70  = pGroupBy->nExp
28026 72 3b 0a 20 20 20 20 20 20 69 42 4d 65 6d 20 3d  r;.      iBMem =
28027 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20   pParse->nMem + 
28028 31 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  1;.      pParse-
28029 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42  >nMem += pGroupB
2802a 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  y->nExpr;.      
2802b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2802c 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2802d 20 30 2c 20 69 41 62 6f 72 74 46 6c 61 67 29 3b   0, iAbortFlag);
2802e 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2802f 6e 74 28 28 76 2c 20 22 63 6c 65 61 72 20 61 62  nt((v, "clear ab
28030 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20  ort flag"));.   
28031 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28032 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
28033 65 72 2c 20 30 2c 20 69 55 73 65 46 6c 61 67 29  er, 0, iUseFlag)
28034 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ;.      VdbeComm
28035 65 6e 74 28 28 76 2c 20 22 69 6e 64 69 63 61 74  ent((v, "indicat
28036 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 65 6d  e accumulator em
28037 70 74 79 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f  pty"));..      /
28038 2a 20 42 65 67 69 6e 20 61 20 6c 6f 6f 70 20 74  * Begin a loop t
28039 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61 63 74  hat will extract
2803a 20 61 6c 6c 20 73 6f 75 72 63 65 20 72 6f 77 73   all source rows
2803b 20 69 6e 20 47 52 4f 55 50 20 42 59 20 6f 72 64   in GROUP BY ord
2803c 65 72 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69  er..      ** Thi
2803d 73 20 6d 69 67 68 74 20 69 6e 76 6f 6c 76 65 20  s might involve 
2803e 74 77 6f 20 73 65 70 61 72 61 74 65 20 6c 6f 6f  two separate loo
2803f 70 73 20 77 69 74 68 20 61 6e 20 4f 50 5f 53 6f  ps with an OP_So
28040 72 74 20 69 6e 20 62 65 74 77 65 65 6e 2c 20 6f  rt in between, o
28041 72 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 69  r.      ** it mi
28042 67 68 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20  ght be a single 
28043 6c 6f 6f 70 20 74 68 61 74 20 75 73 65 73 20 61  loop that uses a
28044 6e 20 69 6e 64 65 78 20 74 6f 20 65 78 74 72 61  n index to extra
28045 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 20  ct information. 
28046 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 72       ** in the r
28047 69 67 68 74 20 6f 72 64 65 72 20 74 6f 20 62 65  ight order to be
28048 67 69 6e 20 77 69 74 68 2e 0a 20 20 20 20 20 20  gin with..      
28049 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2804a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2804b 5f 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65 74  _Gosub, regReset
2804c 2c 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20  , addrReset);.  
2804d 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c      pWInfo = sql
2804e 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70  ite3WhereBegin(p
2804f 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
28050 20 70 57 68 65 72 65 2c 20 26 70 47 72 6f 75 70   pWhere, &pGroup
28051 42 79 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  By, 0, 0);.     
28052 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29   if( pWInfo==0 )
28053 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
28054 3b 0a 20 20 20 20 20 20 69 66 28 20 70 47 72 6f  ;.      if( pGro
28055 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  upBy==0 ){.     
28056 20 20 20 2f 2a 20 54 68 65 20 6f 70 74 69 6d 69     /* The optimi
28057 7a 65 72 20 69 73 20 61 62 6c 65 20 74 6f 20 64  zer is able to d
28058 65 6c 69 76 65 72 20 72 6f 77 73 20 69 6e 20 67  eliver rows in g
28059 72 6f 75 70 20 62 79 20 6f 72 64 65 72 20 73 6f  roup by order so
2805a 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 20 64  .        ** we d
2805b 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 73 6f  o not have to so
2805c 72 74 2e 20 20 54 68 65 20 4f 50 5f 4f 70 65 6e  rt.  The OP_Open
2805d 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  Ephemeral table 
2805e 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20  will be.        
2805f 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 20 6c 61 74  ** cancelled lat
28060 65 72 20 62 65 63 61 75 73 65 20 77 65 20 73 74  er because we st
28061 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 73 65 20  ill need to use 
28062 74 68 65 20 70 4b 65 79 49 6e 66 6f 0a 20 20 20  the pKeyInfo.   
28063 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
28064 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47  pGroupBy = p->pG
28065 72 6f 75 70 42 79 3b 0a 20 20 20 20 20 20 20 20  roupBy;.        
28066 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 30 3b  groupBySort = 0;
28067 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
28068 20 20 20 20 20 20 2f 2a 20 52 6f 77 73 20 61 72        /* Rows ar
28069 65 20 63 6f 6d 69 6e 67 20 6f 75 74 20 69 6e 20  e coming out in 
2806a 75 6e 64 65 74 65 72 6d 69 6e 65 64 20 6f 72 64  undetermined ord
2806b 65 72 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  er.  We have to 
2806c 70 75 73 68 0a 20 20 20 20 20 20 20 20 2a 2a 20  push.        ** 
2806d 65 61 63 68 20 72 6f 77 20 69 6e 74 6f 20 61 20  each row into a 
2806e 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 20 74  sorting index, t
2806f 65 72 6d 69 6e 61 74 65 20 74 68 65 20 66 69 72  erminate the fir
28070 73 74 20 6c 6f 6f 70 2c 0a 20 20 20 20 20 20 20  st loop,.       
28071 20 2a 2a 20 74 68 65 6e 20 6c 6f 6f 70 20 6f 76   ** then loop ov
28072 65 72 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69  er the sorting i
28073 6e 64 65 78 20 69 6e 20 6f 72 64 65 72 20 74 6f  ndex in order to
28074 20 67 65 74 20 74 68 65 20 6f 75 74 70 75 74 0a   get the output.
28075 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 73 6f          ** in so
28076 72 74 65 64 20 6f 72 64 65 72 0a 20 20 20 20 20  rted order.     
28077 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
28078 74 20 72 65 67 42 61 73 65 3b 0a 20 20 20 20 20  t regBase;.     
28079 20 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64     int regRecord
2807a 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 43  ;.        int nC
2807b 6f 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ol;.        int 
2807c 6e 47 72 6f 75 70 42 79 3b 0a 0a 20 20 20 20 20  nGroupBy;..     
2807d 20 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d     groupBySort =
2807e 20 31 3b 0a 20 20 20 20 20 20 20 20 6e 47 72 6f   1;.        nGro
2807f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79 2d  upBy = pGroupBy-
28080 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  >nExpr;.        
28081 6e 43 6f 6c 20 3d 20 6e 47 72 6f 75 70 42 79 20  nCol = nGroupBy 
28082 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d  + 1;.        j =
28083 20 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20 20 20   nGroupBy+1;.   
28084 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
28085 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e  sAggInfo.nColumn
28086 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
28087 20 20 69 66 28 20 73 41 67 67 49 6e 66 6f 2e 61    if( sAggInfo.a
28088 43 6f 6c 5b 69 5d 2e 69 53 6f 72 74 65 72 43 6f  Col[i].iSorterCo
28089 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20 20  lumn>=j ){.     
2808a 20 20 20 20 20 20 20 6e 43 6f 6c 2b 2b 3b 0a 20         nCol++;. 
2808b 20 20 20 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a             j++;.
2808c 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2808d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
2808e 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33 47  gBase = sqlite3G
2808f 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
28090 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20  se, nCol);.     
28091 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
28092 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  deExprList(pPars
28093 65 2c 20 70 47 72 6f 75 70 42 79 2c 20 72 65 67  e, pGroupBy, reg
28094 42 61 73 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Base, 0);.      
28095 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28096 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65 6e  Op2(v, OP_Sequen
28097 63 65 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72  ce, sAggInfo.sor
28098 74 69 6e 67 49 64 78 2c 72 65 67 42 61 73 65 2b  tingIdx,regBase+
28099 6e 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 20  nGroupBy);.     
2809a 20 20 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79 2b     j = nGroupBy+
2809b 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  1;.        for(i
2809c 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e  =0; i<sAggInfo.n
2809d 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
2809e 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41          struct A
2809f 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c  ggInfo_col *pCol
280a0 20 3d 20 26 73 41 67 67 49 6e 66 6f 2e 61 43 6f   = &sAggInfo.aCo
280a1 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  l[i];.          
280a2 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65  if( pCol->iSorte
280a3 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20  rColumn>=j ){.  
280a4 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 31            int r1
280a5 20 3d 20 6a 20 2b 20 72 65 67 42 61 73 65 3b 0a   = j + regBase;.
280a6 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
280a7 72 32 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 20  r2;..           
280a8 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r2 = sqlite3Exp
280a9 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70  rCodeGetColumn(p
280aa 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
280ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
280ac 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61         pCol->pTa
280ad 62 2c 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e  b, pCol->iColumn
280ae 2c 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 2c 20  , pCol->iTable, 
280af 72 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  r1, 0);.        
280b0 20 20 20 20 69 66 28 20 72 31 21 3d 72 32 20 29      if( r1!=r2 )
280b1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
280b2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
280b3 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
280b4 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  2, r1);.        
280b5 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
280b6 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    j++;.         
280b7 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
280b8 20 20 20 20 20 72 65 67 52 65 63 6f 72 64 20 3d       regRecord =
280b9 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
280ba 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
280bb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
280bc 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
280bd 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73 65 2c  Record, regBase,
280be 20 6e 43 6f 6c 2c 20 72 65 67 52 65 63 6f 72 64   nCol, regRecord
280bf 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
280c0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
280c1 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 73 41  OP_IdxInsert, sA
280c2 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64  ggInfo.sortingId
280c3 78 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  x, regRecord);. 
280c4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
280c5 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
280c6 72 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b  rse, regRecord);
280c7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
280c8 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
280c9 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65  (pParse, regBase
280ca 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20  , nCol);.       
280cb 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
280cc 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  (pWInfo);.      
280cd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
280ce 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 2c 20  Op2(v, OP_Sort, 
280cf 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67  sAggInfo.sorting
280d0 49 64 78 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20  Idx, addrEnd);. 
280d1 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65         VdbeComme
280d2 6e 74 28 28 76 2c 20 22 47 52 4f 55 50 20 42 59  nt((v, "GROUP BY
280d3 20 73 6f 72 74 22 29 29 3b 0a 20 20 20 20 20 20   sort"));.      
280d4 20 20 73 41 67 67 49 6e 66 6f 2e 75 73 65 53 6f    sAggInfo.useSo
280d5 72 74 69 6e 67 49 64 78 20 3d 20 31 3b 0a 20 20  rtingIdx = 1;.  
280d6 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
280d7 45 76 61 6c 75 61 74 65 20 74 68 65 20 63 75 72  Evaluate the cur
280d8 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20 74 65  rent GROUP BY te
280d9 72 6d 73 20 61 6e 64 20 73 74 6f 72 65 20 69 6e  rms and store in
280da 20 62 30 2c 20 62 31 2c 20 62 32 2e 2e 2e 0a 20   b0, b1, b2.... 
280db 20 20 20 20 20 2a 2a 20 28 62 30 20 69 73 20 6d       ** (b0 is m
280dc 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 69  emory location i
280dd 42 4d 65 6d 2b 30 2c 20 62 31 20 69 73 20 69 42  BMem+0, b1 is iB
280de 4d 65 6d 2b 31 2c 20 61 6e 64 20 73 6f 20 66 6f  Mem+1, and so fo
280df 72 74 68 29 0a 20 20 20 20 20 20 2a 2a 20 54 68  rth).      ** Th
280e0 65 6e 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63  en compare the c
280e1 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20  urrent GROUP BY 
280e2 74 65 72 6d 73 20 61 67 61 69 6e 73 74 20 74 68  terms against th
280e3 65 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73  e GROUP BY terms
280e4 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74  .      ** from t
280e5 68 65 20 70 72 65 76 69 6f 75 73 20 72 6f 77 20  he previous row 
280e6 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64  currently stored
280e7 20 69 6e 20 61 30 2c 20 61 31 2c 20 61 32 2e 2e   in a0, a1, a2..
280e8 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
280e9 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 20 3d   addrTopOfLoop =
280ea 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
280eb 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
280ec 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72    for(j=0; j<pGr
280ed 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b  oupBy->nExpr; j+
280ee 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
280ef 67 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20  groupBySort ){. 
280f0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
280f1 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
280f2 5f 43 6f 6c 75 6d 6e 2c 20 73 41 67 67 49 6e 66  _Column, sAggInf
280f3 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 6a 2c  o.sortingIdx, j,
280f4 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20 20   iBMem+j);.     
280f5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
280f6 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 64 69 72      sAggInfo.dir
280f7 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 20  ectMode = 1;.   
280f8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
280f9 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
280fa 47 72 6f 75 70 42 79 2d 3e 61 5b 6a 5d 2e 70 45  GroupBy->a[j].pE
280fb 78 70 72 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20  xpr, iBMem+j);. 
280fc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
280fd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
280fe 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43  beAddOp4(v, OP_C
280ff 6f 6d 70 61 72 65 2c 20 69 41 4d 65 6d 2c 20 69  ompare, iAMem, i
28100 42 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e  BMem, pGroupBy->
28101 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20  nExpr,.         
28102 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28103 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
28104 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20  , P4_KEYINFO);. 
28105 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65       j1 = sqlite
28106 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
28107 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  (v);.      sqlit
28108 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
28109 4f 50 5f 4a 75 6d 70 2c 20 6a 31 2b 31 2c 20 30  OP_Jump, j1+1, 0
2810a 2c 20 6a 31 2b 31 29 3b 0a 0a 20 20 20 20 20 20  , j1+1);..      
2810b 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2810c 20 74 68 61 74 20 72 75 6e 73 20 77 68 65 6e 65   that runs whene
2810d 76 65 72 20 74 68 65 20 47 52 4f 55 50 20 42 59  ver the GROUP BY
2810e 20 63 68 61 6e 67 65 73 2e 0a 20 20 20 20 20 20   changes..      
2810f 2a 2a 20 43 68 61 6e 67 65 73 20 69 6e 20 74 68  ** Changes in th
28110 65 20 47 52 4f 55 50 20 42 59 20 61 72 65 20 64  e GROUP BY are d
28111 65 74 65 63 74 65 64 20 62 79 20 74 68 65 20 70  etected by the p
28112 72 65 76 69 6f 75 73 20 63 6f 64 65 0a 20 20 20  revious code.   
28113 20 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66     ** block.  If
28114 20 74 68 65 72 65 20 77 65 72 65 20 6e 6f 20 63   there were no c
28115 68 61 6e 67 65 73 2c 20 74 68 69 73 20 62 6c 6f  hanges, this blo
28116 63 6b 20 69 73 20 73 6b 69 70 70 65 64 2e 0a 20  ck is skipped.. 
28117 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
28118 20 54 68 69 73 20 63 6f 64 65 20 63 6f 70 69 65   This code copie
28119 73 20 63 75 72 72 65 6e 74 20 67 72 6f 75 70 20  s current group 
2811a 62 79 20 74 65 72 6d 73 20 69 6e 20 62 30 2c 62  by terms in b0,b
2811b 31 2c 62 32 2c 2e 2e 2e 0a 20 20 20 20 20 20 2a  1,b2,....      *
2811c 2a 20 6f 76 65 72 20 74 6f 20 61 30 2c 61 31 2c  * over to a0,a1,
2811d 61 32 2e 20 20 49 74 20 74 68 65 6e 20 63 61 6c  a2.  It then cal
2811e 6c 73 20 74 68 65 20 6f 75 74 70 75 74 20 73 75  ls the output su
2811f 62 72 6f 75 74 69 6e 65 0a 20 20 20 20 20 20 2a  broutine.      *
28120 2a 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65  * and resets the
28121 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d   aggregate accum
28122 75 6c 61 74 6f 72 20 72 65 67 69 73 74 65 72 73  ulator registers
28123 20 69 6e 20 70 72 65 70 61 72 61 74 69 6f 6e 0a   in preparation.
28124 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65        ** for the
28125 20 6e 65 78 74 20 47 52 4f 55 50 20 42 59 20 62   next GROUP BY b
28126 61 74 63 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  atch..      */. 
28127 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
28128 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c  CodeMove(pParse,
28129 20 69 42 4d 65 6d 2c 20 69 41 4d 65 6d 2c 20 70   iBMem, iAMem, p
2812a 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 29 3b  GroupBy->nExpr);
2812b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2812c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2812d 6f 73 75 62 2c 20 72 65 67 4f 75 74 70 75 74 52  osub, regOutputR
2812e 6f 77 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f  ow, addrOutputRo
2812f 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  w);.      VdbeCo
28130 6d 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74 70 75  mment((v, "outpu
28131 74 20 6f 6e 65 20 72 6f 77 22 29 29 3b 0a 20 20  t one row"));.  
28132 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28133 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f  ddOp2(v, OP_IfPo
28134 73 2c 20 69 41 62 6f 72 74 46 6c 61 67 2c 20 61  s, iAbortFlag, a
28135 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 56  ddrEnd);.      V
28136 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
28137 63 68 65 63 6b 20 61 62 6f 72 74 20 66 6c 61 67  check abort flag
28138 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  "));.      sqlit
28139 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2813a 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 52 65 73  OP_Gosub, regRes
2813b 65 74 2c 20 61 64 64 72 52 65 73 65 74 29 3b 0a  et, addrReset);.
2813c 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2813d 74 28 28 76 2c 20 22 72 65 73 65 74 20 61 63 63  t((v, "reset acc
2813e 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20  umulator"));..  
2813f 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68      /* Update th
28140 65 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75  e aggregate accu
28141 6d 75 6c 61 74 6f 72 73 20 62 61 73 65 64 20 6f  mulators based o
28142 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  n the content of
28143 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 75  .      ** the cu
28144 72 72 65 6e 74 20 72 6f 77 0a 20 20 20 20 20 20  rrent row.      
28145 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
28146 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
28147 6a 31 29 3b 0a 20 20 20 20 20 20 75 70 64 61 74  j1);.      updat
28148 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61  eAccumulator(pPa
28149 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b  rse, &sAggInfo);
2814a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2814b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2814c 6e 74 65 67 65 72 2c 20 31 2c 20 69 55 73 65 46  nteger, 1, iUseF
2814d 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65  lag);.      Vdbe
2814e 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e 64  Comment((v, "ind
2814f 69 63 61 74 65 20 64 61 74 61 20 69 6e 20 61 63  icate data in ac
28150 63 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20  cumulator"));.. 
28151 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74       /* End of t
28152 68 65 20 6c 6f 6f 70 0a 20 20 20 20 20 20 2a 2f  he loop.      */
28153 0a 20 20 20 20 20 20 69 66 28 20 67 72 6f 75 70  .      if( group
28154 42 79 53 6f 72 74 20 29 7b 0a 20 20 20 20 20 20  BySort ){.      
28155 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28156 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
28157 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67  sAggInfo.sorting
28158 49 64 78 2c 20 61 64 64 72 54 6f 70 4f 66 4c 6f  Idx, addrTopOfLo
28159 6f 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  op);.      }else
2815a 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2815b 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
2815c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2815d 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f  e3VdbeChangeToNo
2815e 6f 70 28 76 2c 20 61 64 64 72 53 6f 72 74 69 6e  op(v, addrSortin
2815f 67 49 64 78 2c 20 31 29 3b 0a 20 20 20 20 20 20  gIdx, 1);.      
28160 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75 74 70  }..      /* Outp
28161 75 74 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77  ut the final row
28162 20 6f 66 20 72 65 73 75 6c 74 0a 20 20 20 20 20   of result.     
28163 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
28164 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28165 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 70  P_Gosub, regOutp
28166 75 74 52 6f 77 2c 20 61 64 64 72 4f 75 74 70 75  utRow, addrOutpu
28167 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64 62  tRow);.      Vdb
28168 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6f 75  eComment((v, "ou
28169 74 70 75 74 20 66 69 6e 61 6c 20 72 6f 77 22 29  tput final row")
2816a 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d  );..      /* Jum
2816b 70 20 6f 76 65 72 20 74 68 65 20 73 75 62 72 6f  p over the subro
2816c 75 74 69 6e 65 73 0a 20 20 20 20 20 20 2a 2f 0a  utines.      */.
2816d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2816e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2816f 74 6f 2c 20 30 2c 20 61 64 64 72 45 6e 64 29 3b  to, 0, addrEnd);
28170 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72  ..      /* Gener
28171 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65  ate a subroutine
28172 20 74 68 61 74 20 6f 75 74 70 75 74 73 20 61 20   that outputs a 
28173 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68  single row of th
28174 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 2a  e result.      *
28175 2a 20 73 65 74 2e 20 20 54 68 69 73 20 73 75 62  * set.  This sub
28176 72 6f 75 74 69 6e 65 20 66 69 72 73 74 20 6c 6f  routine first lo
28177 6f 6b 73 20 61 74 20 74 68 65 20 69 55 73 65 46  oks at the iUseF
28178 6c 61 67 2e 20 20 49 66 20 69 55 73 65 46 6c 61  lag.  If iUseFla
28179 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 6c 65  g.      ** is le
2817a 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
2817b 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20 73 75   to zero, the su
2817c 62 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f  broutine is a no
2817d 2d 6f 70 2e 20 20 49 66 0a 20 20 20 20 20 20 2a  -op.  If.      *
2817e 2a 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67  * the processing
2817f 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 71   calls for the q
28180 75 65 72 79 20 74 6f 20 61 62 6f 72 74 2c 20 74  uery to abort, t
28181 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 0a 20  his subroutine. 
28182 20 20 20 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e       ** incremen
28183 74 73 20 74 68 65 20 69 41 62 6f 72 74 46 6c 61  ts the iAbortFla
28184 67 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  g memory locatio
28185 6e 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  n before returni
28186 6e 67 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 6f  ng in.      ** o
28187 72 64 65 72 20 74 6f 20 73 69 67 6e 61 6c 20 74  rder to signal t
28188 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 61 62 6f  he caller to abo
28189 72 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  rt..      */.   
2818a 20 20 20 61 64 64 72 53 65 74 41 62 6f 72 74 20     addrSetAbort 
2818b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2818c 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
2818d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2818e 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2818f 65 72 2c 20 31 2c 20 69 41 62 6f 72 74 46 6c 61  er, 1, iAbortFla
28190 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  g);.      VdbeCo
28191 6d 6d 65 6e 74 28 28 76 2c 20 22 73 65 74 20 61  mment((v, "set a
28192 62 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20  bort flag"));.  
28193 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28194 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75  ddOp1(v, OP_Retu
28195 72 6e 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77  rn, regOutputRow
28196 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
28197 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
28198 28 76 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f  (v, addrOutputRo
28199 77 29 3b 0a 20 20 20 20 20 20 61 64 64 72 4f 75  w);.      addrOu
2819a 74 70 75 74 52 6f 77 20 3d 20 73 71 6c 69 74 65  tputRow = sqlite
2819b 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2819c 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  (v);.      sqlit
2819d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2819e 4f 50 5f 49 66 50 6f 73 2c 20 69 55 73 65 46 6c  OP_IfPos, iUseFl
2819f 61 67 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f  ag, addrOutputRo
281a0 77 2b 32 29 3b 0a 20 20 20 20 20 20 56 64 62 65  w+2);.      Vdbe
281a1 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47 72 6f  Comment((v, "Gro
281a2 75 70 62 79 20 72 65 73 75 6c 74 20 67 65 6e 65  upby result gene
281a3 72 61 74 6f 72 20 65 6e 74 72 79 20 70 6f 69 6e  rator entry poin
281a4 74 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  t"));.      sqli
281a5 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
281a6 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f   OP_Return, regO
281a7 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20  utputRow);.     
281a8 20 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63   finalizeAggFunc
281a9 74 69 6f 6e 73 28 70 50 61 72 73 65 2c 20 26 73  tions(pParse, &s
281aa 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  AggInfo);.      
281ab 69 66 28 20 70 48 61 76 69 6e 67 20 29 7b 0a 20  if( pHaving ){. 
281ac 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
281ad 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
281ae 2c 20 70 48 61 76 69 6e 67 2c 20 61 64 64 72 4f  , pHaving, addrO
281af 75 74 70 75 74 52 6f 77 2b 31 2c 20 53 51 4c 49  utputRow+1, SQLI
281b0 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
281b1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 65        }.      se
281b2 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50  lectInnerLoop(pP
281b3 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69  arse, p, p->pELi
281b4 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65 72  st, 0, 0, pOrder
281b5 42 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  By,.            
281b6 20 20 20 20 20 20 20 20 20 20 64 69 73 74 69 6e            distin
281b7 63 74 2c 20 70 44 65 73 74 2c 0a 20 20 20 20 20  ct, pDest,.     
281b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
281b9 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 31   addrOutputRow+1
281ba 2c 20 61 64 64 72 53 65 74 41 62 6f 72 74 29 3b  , addrSetAbort);
281bb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
281bc 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52  beAddOp1(v, OP_R
281bd 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70 75 74  eturn, regOutput
281be 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65  Row);.      Vdbe
281bf 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64  Comment((v, "end
281c0 20 67 72 6f 75 70 62 79 20 72 65 73 75 6c 74 20   groupby result 
281c1 67 65 6e 65 72 61 74 6f 72 22 29 29 3b 0a 0a 20  generator"));.. 
281c2 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
281c3 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
281c4 61 74 20 77 69 6c 6c 20 72 65 73 65 74 20 74 68  at will reset th
281c5 65 20 67 72 6f 75 70 2d 62 79 20 61 63 63 75 6d  e group-by accum
281c6 75 6c 61 74 6f 72 0a 20 20 20 20 20 20 2a 2f 0a  ulator.      */.
281c7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
281c8 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
281c9 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20   addrReset);.   
281ca 20 20 20 72 65 73 65 74 41 63 63 75 6d 75 6c 61     resetAccumula
281cb 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41 67  tor(pParse, &sAg
281cc 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71  gInfo);.      sq
281cd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
281ce 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65  v, OP_Return, re
281cf 67 52 65 73 65 74 29 3b 0a 20 20 20 20 20 0a 20  gReset);.     . 
281d0 20 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 47     } /* endif pG
281d1 72 6f 75 70 42 79 20 2a 2f 0a 20 20 20 20 65 6c  roupBy */.    el
281d2 73 65 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c  se {.      ExprL
281d3 69 73 74 20 2a 70 4d 69 6e 4d 61 78 20 3d 20 30  ist *pMinMax = 0
281d4 3b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74  ;.      ExprList
281d5 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20   *pDel = 0;.    
281d6 20 20 75 38 20 66 6c 61 67 3b 0a 0a 20 20 20 20    u8 flag;..    
281d7 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68    /* Check if th
281d8 65 20 71 75 65 72 79 20 69 73 20 6f 66 20 6f 6e  e query is of on
281d9 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
281da 6e 67 20 66 6f 72 6d 73 3a 0a 20 20 20 20 20 20  ng forms:.      
281db 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 53 45  **.      **   SE
281dc 4c 45 43 54 20 6d 69 6e 28 78 29 20 46 52 4f 4d  LECT min(x) FROM
281dd 20 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 20 20   ....      **   
281de 53 45 4c 45 43 54 20 6d 61 78 28 78 29 20 46 52  SELECT max(x) FR
281df 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 0a  OM ....      **.
281e0 20 20 20 20 20 20 2a 2a 20 49 66 20 69 74 20 69        ** If it i
281e1 73 2c 20 74 68 65 6e 20 61 73 6b 20 74 68 65 20  s, then ask the 
281e2 63 6f 64 65 20 69 6e 20 77 68 65 72 65 2e 63 20  code in where.c 
281e3 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 73 6f  to attempt to so
281e4 72 74 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20  rt results.     
281e5 20 2a 2a 20 61 73 20 69 66 20 74 68 65 72 65 20   ** as if there 
281e6 77 61 73 20 61 6e 20 22 4f 52 44 45 52 20 4f 4e  was an "ORDER ON
281e7 20 78 22 20 6f 72 20 22 4f 52 44 45 52 20 4f 4e   x" or "ORDER ON
281e8 20 78 20 44 45 53 43 22 20 63 6c 61 75 73 65 2e   x DESC" clause.
281e9 20 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 77 68   .      ** If wh
281ea 65 72 65 2e 63 20 69 73 20 61 62 6c 65 20 74 6f  ere.c is able to
281eb 20 70 72 6f 64 75 63 65 20 72 65 73 75 6c 74 73   produce results
281ec 20 73 6f 72 74 65 64 20 69 6e 20 74 68 69 73 20   sorted in this 
281ed 6f 72 64 65 72 2c 20 74 68 65 6e 0a 20 20 20 20  order, then.    
281ee 20 20 2a 2a 20 61 64 64 20 76 64 62 65 20 63 6f    ** add vdbe co
281ef 64 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20  de to break out 
281f0 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e  of the processin
281f1 67 20 6c 6f 6f 70 20 61 66 74 65 72 20 74 68 65  g loop after the
281f2 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 72 73 74   .      ** first
281f3 20 69 74 65 72 61 74 69 6f 6e 20 28 73 69 6e 63   iteration (sinc
281f4 65 20 74 68 65 20 66 69 72 73 74 20 69 74 65 72  e the first iter
281f5 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f  ation of the loo
281f6 70 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 67  p is .      ** g
281f7 75 61 72 61 6e 74 65 65 64 20 74 6f 20 6f 70 65  uaranteed to ope
281f8 72 61 74 65 20 6f 6e 20 74 68 65 20 72 6f 77 20  rate on the row 
281f9 77 69 74 68 20 74 68 65 20 6d 69 6e 69 6d 75 6d  with the minimum
281fa 20 6f 72 20 6d 61 78 69 6d 75 6d 20 0a 20 20 20   or maximum .   
281fb 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 78     ** value of x
281fc 2c 20 74 68 65 20 6f 6e 6c 79 20 72 6f 77 20 72  , the only row r
281fd 65 71 75 69 72 65 64 29 2e 0a 20 20 20 20 20 20  equired)..      
281fe 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 20 73 70  **.      ** A sp
281ff 65 63 69 61 6c 20 66 6c 61 67 20 6d 75 73 74 20  ecial flag must 
28200 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  be passed to sql
28201 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
28202 20 74 6f 20 73 6c 69 67 68 74 6c 79 0a 20 20 20   to slightly.   
28203 20 20 20 2a 2a 20 6d 6f 64 69 66 79 20 62 65 68     ** modify beh
28204 61 76 69 6f 75 72 20 61 73 20 66 6f 6c 6c 6f 77  aviour as follow
28205 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  s:.      **.    
28206 20 20 2a 2a 20 20 20 2b 20 49 66 20 74 68 65 20    **   + If the 
28207 71 75 65 72 79 20 69 73 20 61 20 22 53 45 4c 45  query is a "SELE
28208 43 54 20 6d 69 6e 28 78 29 22 2c 20 74 68 65 6e  CT min(x)", then
28209 20 74 68 65 20 6c 6f 6f 70 20 63 6f 64 65 64 20   the loop coded 
2820a 62 79 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  by.      **     
2820b 77 68 65 72 65 2e 63 20 73 68 6f 75 6c 64 20 6e  where.c should n
2820c 6f 74 20 69 74 65 72 61 74 65 20 6f 76 65 72 20  ot iterate over 
2820d 61 6e 79 20 76 61 6c 75 65 73 20 77 69 74 68 20  any values with 
2820e 61 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 20 20 20  a NULL value.   
2820f 20 20 20 2a 2a 20 20 20 20 20 66 6f 72 20 78 2e     **     for x.
28210 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
28211 2a 2a 20 20 20 2b 20 54 68 65 20 6f 70 74 69 6d  **   + The optim
28212 69 7a 65 72 20 63 6f 64 65 20 69 6e 20 77 68 65  izer code in whe
28213 72 65 2e 63 20 28 74 68 65 20 74 68 69 6e 67 20  re.c (the thing 
28214 74 68 61 74 20 64 65 63 69 64 65 73 20 77 68 69  that decides whi
28215 63 68 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  ch.      **     
28216 69 6e 64 65 78 20 6f 72 20 69 6e 64 69 63 65 73  index or indices
28217 20 74 6f 20 75 73 65 29 20 73 68 6f 75 6c 64 20   to use) should 
28218 70 6c 61 63 65 20 61 20 64 69 66 66 65 72 65 6e  place a differen
28219 74 20 70 72 69 6f 72 69 74 79 20 6f 6e 20 0a 20  t priority on . 
2821a 20 20 20 20 20 2a 2a 20 20 20 20 20 73 61 74 69       **     sati
2821b 73 66 79 69 6e 67 20 74 68 65 20 27 4f 52 44 45  sfying the 'ORDE
2821c 52 20 42 59 27 20 63 6c 61 75 73 65 20 74 68 61  R BY' clause tha
2821d 6e 20 69 74 20 64 6f 65 73 20 69 6e 20 6f 74 68  n it does in oth
2821e 65 72 20 63 61 73 65 73 2e 0a 20 20 20 20 20 20  er cases..      
2821f 2a 2a 20 20 20 20 20 52 65 66 65 72 20 74 6f 20  **     Refer to 
28220 63 6f 64 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74  code and comment
28221 73 20 69 6e 20 77 68 65 72 65 2e 63 20 66 6f 72  s in where.c for
28222 20 64 65 74 61 69 6c 73 2e 0a 20 20 20 20 20 20   details..      
28223 2a 2f 0a 20 20 20 20 20 20 66 6c 61 67 20 3d 20  */.      flag = 
28224 6d 69 6e 4d 61 78 51 75 65 72 79 28 70 29 3b 0a  minMaxQuery(p);.
28225 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 20 29        if( flag )
28226 7b 0a 20 20 20 20 20 20 20 20 70 44 65 6c 20 3d  {.        pDel =
28227 20 70 4d 69 6e 4d 61 78 20 3d 20 73 71 6c 69 74   pMinMax = sqlit
28228 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
28229 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30  , p->pEList->a[0
2822a 5d 2e 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 3b  ].pExpr->pList);
2822b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4d 69  .        if( pMi
2822c 6e 4d 61 78 20 26 26 20 21 64 62 2d 3e 6d 61 6c  nMax && !db->mal
2822d 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2822e 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e         pMinMax->
2822f 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d  a[0].sortOrder =
28230 20 66 6c 61 67 21 3d 57 48 45 52 45 5f 4f 52 44   flag!=WHERE_ORD
28231 45 52 42 59 5f 4d 49 4e 20 3f 31 3a 30 3b 0a 20  ERBY_MIN ?1:0;. 
28232 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78           pMinMax
28233 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70  ->a[0].pExpr->op
28234 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20   = TK_COLUMN;.  
28235 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
28236 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63  .      /* This c
28237 61 73 65 20 72 75 6e 73 20 69 66 20 74 68 65 20  ase runs if the 
28238 61 67 67 72 65 67 61 74 65 20 68 61 73 20 6e 6f  aggregate has no
28239 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
2823a 2e 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20  .  The.      ** 
2823b 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 6d 75  processing is mu
2823c 63 68 20 73 69 6d 70 6c 65 72 20 73 69 6e 63 65  ch simpler since
2823d 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61   there is only a
2823e 20 73 69 6e 67 6c 65 20 72 6f 77 0a 20 20 20 20   single row.    
2823f 20 20 2a 2a 20 6f 66 20 6f 75 74 70 75 74 2e 0a    ** of output..
28240 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
28241 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28  esetAccumulator(
28242 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66  pParse, &sAggInf
28243 6f 29 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f  o);.      pWInfo
28244 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42   = sqlite3WhereB
28245 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61  egin(pParse, pTa
28246 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26  bList, pWhere, &
28247 70 4d 69 6e 4d 61 78 2c 20 66 6c 61 67 2c 20 30  pMinMax, flag, 0
28248 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 57 49  );.      if( pWI
28249 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  nfo==0 ){.      
2824a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2824b 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c  tDelete(db, pDel
2824c 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  );.        goto 
2824d 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
2824e 20 20 7d 0a 20 20 20 20 20 20 75 70 64 61 74 65    }.      update
2824f 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72  Accumulator(pPar
28250 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
28251 20 20 20 20 20 20 69 66 28 20 21 70 4d 69 6e 4d        if( !pMinM
28252 61 78 20 26 26 20 66 6c 61 67 20 29 7b 0a 20 20  ax && flag ){.  
28253 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28254 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
28255 74 6f 2c 20 30 2c 20 70 57 49 6e 66 6f 2d 3e 69  to, 0, pWInfo->i
28256 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20  Break);.        
28257 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
28258 22 25 73 28 29 20 62 79 20 69 6e 64 65 78 22 2c  "%s() by index",
28259 28 66 6c 61 67 3d 3d 57 48 45 52 45 5f 4f 52 44  (flag==WHERE_ORD
2825a 45 52 42 59 5f 4d 49 4e 3f 22 6d 69 6e 22 3a 22  ERBY_MIN?"min":"
2825b 6d 61 78 22 29 29 29 3b 0a 20 20 20 20 20 20 7d  max")));.      }
2825c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68  .      sqlite3Wh
2825d 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
2825e 20 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41 67        finalizeAg
2825f 67 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72 73  gFunctions(pPars
28260 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20  e, &sAggInfo);. 
28261 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20       pOrderBy = 
28262 30 3b 0a 20 20 20 20 20 20 69 66 28 20 70 48 61  0;.      if( pHa
28263 76 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20  ving ){.        
28264 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c  sqlite3ExprIfFal
28265 73 65 28 70 50 61 72 73 65 2c 20 70 48 61 76 69  se(pParse, pHavi
28266 6e 67 2c 20 61 64 64 72 45 6e 64 2c 20 53 51 4c  ng, addrEnd, SQL
28267 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
28268 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
28269 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70  electInnerLoop(p
2826a 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c  Parse, p, p->pEL
2826b 69 73 74 2c 20 30 2c 20 30 2c 20 30 2c 20 2d 31  ist, 0, 0, 0, -1
2826c 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2826d 20 20 20 20 20 20 20 20 20 70 44 65 73 74 2c 20           pDest, 
2826e 61 64 64 72 45 6e 64 2c 20 61 64 64 72 45 6e 64  addrEnd, addrEnd
2826f 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
28270 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
28271 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 7d  db, pDel);.    }
28272 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
28273 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
28274 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 0a 20  addrEnd);.    . 
28275 20 7d 20 2f 2a 20 65 6e 64 69 66 20 61 67 67 72   } /* endif aggr
28276 65 67 61 74 65 20 71 75 65 72 79 20 2a 2f 0a 0a  egate query */..
28277 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
28278 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
28279 75 73 65 2c 20 74 68 65 6e 20 77 65 20 6e 65 65  use, then we nee
2827a 64 20 74 6f 20 73 6f 72 74 20 74 68 65 20 72 65  d to sort the re
2827b 73 75 6c 74 73 0a 20 20 2a 2a 20 61 6e 64 20 73  sults.  ** and s
2827c 65 6e 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20  end them to the 
2827d 63 61 6c 6c 62 61 63 6b 20 6f 6e 65 20 62 79 20  callback one by 
2827e 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  one..  */.  if( 
2827f 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
28280 67 65 6e 65 72 61 74 65 53 6f 72 74 54 61 69 6c  generateSortTail
28281 28 70 50 61 72 73 65 2c 20 70 2c 20 76 2c 20 70  (pParse, p, v, p
28282 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 44  EList->nExpr, pD
28283 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  est);.  }..  /* 
28284 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69  Jump here to ski
28285 70 20 74 68 69 73 20 71 75 65 72 79 0a 20 20 2a  p this query.  *
28286 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  /.  sqlite3VdbeR
28287 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
28288 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  End);..  /* The 
28289 53 45 4c 45 43 54 20 77 61 73 20 73 75 63 63 65  SELECT was succe
2828a 73 73 66 75 6c 6c 79 20 63 6f 64 65 64 2e 20 20  ssfully coded.  
2828b 20 53 65 74 20 74 68 65 20 72 65 74 75 72 6e 20   Set the return 
2828c 63 6f 64 65 20 74 6f 20 30 0a 20 20 2a 2a 20 74  code to 0.  ** t
2828d 6f 20 69 6e 64 69 63 61 74 65 20 6e 6f 20 65 72  o indicate no er
2828e 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  rors..  */.  rc 
2828f 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 74 72  = 0;..  /* Contr
28290 6f 6c 20 6a 75 6d 70 73 20 74 6f 20 68 65 72 65  ol jumps to here
28291 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20   if an error is 
28292 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 62 6f 76  encountered abov
28293 65 2c 20 6f 72 20 75 70 6f 6e 0a 20 20 2a 2a 20  e, or upon.  ** 
28294 73 75 63 63 65 73 73 66 75 6c 20 63 6f 64 69 6e  successful codin
28295 67 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e  g of the SELECT.
28296 0a 20 20 2a 2f 0a 73 65 6c 65 63 74 5f 65 6e 64  .  */.select_end
28297 3a 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66 79  :..  /* Identify
28298 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 66   column names if
28299 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
2829a 53 45 4c 45 43 54 20 61 72 65 20 74 6f 20 62 65  SELECT are to be
2829b 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 20 20   output..  */.  
2829c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2829d 4b 20 26 26 20 70 44 65 73 74 2d 3e 65 44 65 73  K && pDest->eDes
2829e 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b  t==SRT_Output ){
2829f 0a 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c  .    generateCol
282a0 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
282a1 20 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69 73   pTabList, pELis
282a2 74 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  t);.  }..  sqlit
282a3 65 33 44 62 46 72 65 65 28 64 62 2c 20 73 41 67  e3DbFree(db, sAg
282a4 67 49 6e 66 6f 2e 61 43 6f 6c 29 3b 0a 20 20 73  gInfo.aCol);.  s
282a5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
282a6 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 29   sAggInfo.aFunc)
282a7 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
282a8 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
282a9 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
282aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
282af 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
282b0 63 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f 72  code is used for
282b1 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62   testing and deb
282b2 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68  ugging only.  Th
282b3 65 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 66  e code.** that f
282b4 6f 6c 6c 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20  ollows does not 
282b5 61 70 70 65 61 72 20 69 6e 20 6e 6f 72 6d 61 6c  appear in normal
282b6 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54   builds..**.** T
282b7 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
282b8 65 20 75 73 65 64 20 74 6f 20 70 72 69 6e 74 20  e used to print 
282b9 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  out the content 
282ba 6f 66 20 61 6c 6c 20 6f 72 20 70 61 72 74 20 6f  of all or part o
282bb 66 20 61 20 0a 2a 2a 20 70 61 72 73 65 20 73 74  f a .** parse st
282bc 72 75 63 74 75 72 65 73 20 73 75 63 68 20 61 73  ructures such as
282bd 20 53 65 6c 65 63 74 20 6f 72 20 45 78 70 72 2e   Select or Expr.
282be 20 20 53 75 63 68 20 70 72 69 6e 74 6f 75 74 73    Such printouts
282bf 20 61 72 65 20 75 73 65 66 75 6c 0a 2a 2a 20 66   are useful.** f
282c0 6f 72 20 68 65 6c 70 69 6e 67 20 74 6f 20 75 6e  or helping to un
282c1 64 65 72 73 74 61 6e 64 20 77 68 61 74 20 69 73  derstand what is
282c2 20 68 61 70 70 65 6e 69 6e 67 20 69 6e 73 69 64   happening insid
282c3 65 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  e the code gener
282c4 61 74 6f 72 0a 2a 2a 20 64 75 72 69 6e 67 20 74  ator.** during t
282c5 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  he execution of 
282c6 63 6f 6d 70 6c 65 78 20 53 45 4c 45 43 54 20 73  complex SELECT s
282c7 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
282c8 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20 61   These routine a
282c9 72 65 20 6e 6f 74 20 63 61 6c 6c 65 64 20 61 6e  re not called an
282ca 79 77 68 65 72 65 20 66 72 6f 6d 20 77 69 74 68  ywhere from with
282cb 69 6e 20 74 68 65 20 6e 6f 72 6d 61 6c 0a 2a 2a  in the normal.**
282cc 20 63 6f 64 65 20 62 61 73 65 2e 20 20 54 68 65   code base.  The
282cd 6e 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74  n are intended t
282ce 6f 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  o be called from
282cf 20 77 69 74 68 69 6e 20 74 68 65 20 64 65 62 75   within the debu
282d0 67 67 65 72 0a 2a 2a 20 6f 72 20 66 72 6f 6d 20  gger.** or from 
282d1 74 65 6d 70 6f 72 61 72 79 20 22 70 72 69 6e 74  temporary "print
282d2 66 22 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  f" statements in
282d3 73 65 72 74 65 64 20 66 6f 72 20 64 65 62 75 67  serted for debug
282d4 67 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ging..*/.SQLITE_
282d5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
282d6 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 45 78  ite3PrintExpr(Ex
282d7 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  pr *p){.  if( p-
282d8 3e 74 6f 6b 65 6e 2e 7a 20 26 26 20 70 2d 3e 74  >token.z && p->t
282d9 6f 6b 65 6e 2e 6e 3e 30 20 29 7b 0a 20 20 20 20  oken.n>0 ){.    
282da 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
282db 74 66 28 22 28 25 2e 2a 73 22 2c 20 70 2d 3e 74  tf("(%.*s", p->t
282dc 6f 6b 65 6e 2e 6e 2c 20 70 2d 3e 74 6f 6b 65 6e  oken.n, p->token
282dd 2e 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  .z);.  }else{.  
282de 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
282df 69 6e 74 66 28 22 28 25 64 22 2c 20 70 2d 3e 6f  intf("(%d", p->o
282e0 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  p);.  }.  if( p-
282e1 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 71  >pLeft ){.    sq
282e2 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
282e3 28 22 20 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  (" ");.    sqlit
282e4 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70  e3PrintExpr(p->p
282e5 4c 65 66 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28  Left);.  }.  if(
282e6 20 70 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20   p->pRight ){.  
282e7 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
282e8 69 6e 74 66 28 22 20 22 29 3b 0a 20 20 20 20 73  intf(" ");.    s
282e9 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28  qlite3PrintExpr(
282ea 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a  p->pRight);.  }.
282eb 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
282ec 69 6e 74 66 28 22 29 22 29 3b 0a 7d 0a 53 51 4c  intf(")");.}.SQL
282ed 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
282ee 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70   sqlite3PrintExp
282ef 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a  rList(ExprList *
282f0 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pList){.  int i;
282f1 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c  .  for(i=0; i<pL
282f2 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
282f3 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69  {.    sqlite3Pri
282f4 6e 74 45 78 70 72 28 70 4c 69 73 74 2d 3e 61 5b  ntExpr(pList->a[
282f5 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 69  i].pExpr);.    i
282f6 66 28 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  f( i<pList->nExp
282f7 72 2d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  r-1 ){.      sql
282f8 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
282f9 22 2c 20 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ", ");.    }.  }
282fa 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
282fb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
282fc 69 6e 74 53 65 6c 65 63 74 28 53 65 6c 65 63 74  intSelect(Select
282fd 20 2a 70 2c 20 69 6e 74 20 69 6e 64 65 6e 74 29   *p, int indent)
282fe 7b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67  {.  sqlite3Debug
282ff 50 72 69 6e 74 66 28 22 25 2a 73 53 45 4c 45 43  Printf("%*sSELEC
28300 54 28 25 70 29 20 22 2c 20 69 6e 64 65 6e 74 2c  T(%p) ", indent,
28301 20 22 22 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74   "", p);.  sqlit
28302 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28  e3PrintExprList(
28303 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73 71  p->pEList);.  sq
28304 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
28305 28 22 5c 6e 22 29 3b 0a 20 20 69 66 28 20 70 2d  ("\n");.  if( p-
28306 3e 70 53 72 63 20 29 7b 0a 20 20 20 20 63 68 61  >pSrc ){.    cha
28307 72 20 2a 7a 50 72 65 66 69 78 3b 0a 20 20 20 20  r *zPrefix;.    
28308 69 6e 74 20 69 3b 0a 20 20 20 20 7a 50 72 65 66  int i;.    zPref
28309 69 78 20 3d 20 22 46 52 4f 4d 22 3b 0a 20 20 20  ix = "FROM";.   
2830a 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70   for(i=0; i<p->p
2830b 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  Src->nSrc; i++){
2830c 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72  .      struct Sr
2830d 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
2830e 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b  m = &p->pSrc->a[
2830f 69 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  i];.      sqlite
28310 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a  3DebugPrintf("%*
28311 73 20 22 2c 20 69 6e 64 65 6e 74 2b 36 2c 20 7a  s ", indent+6, z
28312 50 72 65 66 69 78 29 3b 0a 20 20 20 20 20 20 7a  Prefix);.      z
28313 50 72 65 66 69 78 20 3d 20 22 22 3b 0a 20 20 20  Prefix = "";.   
28314 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53     if( pItem->pS
28315 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
28316 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
28317 6e 74 66 28 22 28 5c 6e 22 29 3b 0a 20 20 20 20  ntf("(\n");.    
28318 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
28319 53 65 6c 65 63 74 28 70 49 74 65 6d 2d 3e 70 53  Select(pItem->pS
2831a 65 6c 65 63 74 2c 20 69 6e 64 65 6e 74 2b 31 30  elect, indent+10
2831b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2831c 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
2831d 2a 73 29 22 2c 20 69 6e 64 65 6e 74 2b 38 2c 20  *s)", indent+8, 
2831e 22 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  "");.      }else
2831f 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d   if( pItem->zNam
28320 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
28321 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
28322 22 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61  "%s", pItem->zNa
28323 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  me);.      }.   
28324 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 54     if( pItem->pT
28325 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ab ){.        sq
28326 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
28327 28 22 28 74 61 62 6c 65 3a 20 25 73 29 22 2c 20  ("(table: %s)", 
28328 70 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61  pItem->pTab->zNa
28329 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  me);.      }.   
2832a 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41     if( pItem->zA
2832b 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20  lias ){.        
2832c 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2832d 74 66 28 22 20 41 53 20 25 73 22 2c 20 70 49 74  tf(" AS %s", pIt
2832e 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20  em->zAlias);.   
2832f 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
28330 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 2d 31  <p->pSrc->nSrc-1
28331 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
28332 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
28333 2c 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ,");.      }.   
28334 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
28335 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 20  rintf("\n");.   
28336 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e   }.  }.  if( p->
28337 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 73 71  pWhere ){.    sq
28338 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
28339 28 22 25 2a 73 20 57 48 45 52 45 20 22 2c 20 69  ("%*s WHERE ", i
2833a 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20  ndent, "");.    
2833b 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
2833c 28 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20  (p->pWhere);.   
2833d 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2833e 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20  ntf("\n");.  }. 
2833f 20 69 66 28 20 70 2d 3e 70 47 72 6f 75 70 42 79   if( p->pGroupBy
28340 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
28341 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20  ebugPrintf("%*s 
28342 47 52 4f 55 50 20 42 59 20 22 2c 20 69 6e 64 65  GROUP BY ", inde
28343 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c  nt, "");.    sql
28344 69 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73  ite3PrintExprLis
28345 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a  t(p->pGroupBy);.
28346 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
28347 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20  Printf("\n");.  
28348 7d 0a 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69  }.  if( p->pHavi
28349 6e 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ng ){.    sqlite
2834a 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a  3DebugPrintf("%*
2834b 73 20 48 41 56 49 4e 47 20 22 2c 20 69 6e 64 65  s HAVING ", inde
2834c 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c  nt, "");.    sql
2834d 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d  ite3PrintExpr(p-
2834e 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 73  >pHaving);.    s
2834f 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
28350 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69  f("\n");.  }.  i
28351 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29  f( p->pOrderBy )
28352 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
28353 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 4f 52  ugPrintf("%*s OR
28354 44 45 52 20 42 59 20 22 2c 20 69 6e 64 65 6e 74  DER BY ", indent
28355 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  , "");.    sqlit
28356 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28  e3PrintExprList(
28357 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20  p->pOrderBy);.  
28358 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
28359 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a  intf("\n");.  }.
2835a 7d 0a 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  }./* End of the 
2835b 73 74 72 75 63 74 75 72 65 20 64 65 62 75 67 20  structure debug 
2835c 70 72 69 6e 74 69 6e 67 20 63 6f 64 65 0a 2a 2a  printing code.**
2835d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2835e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2835f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28361 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e  ***********/.#en
28362 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
28363 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64  QLITE_TEST) || d
28364 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
28365 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  BUG) */../******
28366 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
28367 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  select.c *******
28368 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28369 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2836a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2836b 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2836c 69 6c 65 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a  ile table.c ****
2836d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2836e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2836f 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
28370 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
28371 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
28372 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
28373 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
28374 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
28375 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
28376 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
28377 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
28378 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
28379 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
2837a 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
2837b 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
2837c 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
2837d 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
2837e 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
2837f 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
28380 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
28381 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
28382 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
28383 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28384 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28385 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28386 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
28387 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
28388 74 61 69 6e 73 20 74 68 65 20 73 71 6c 69 74 65  tains the sqlite
28389 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 61 6e  3_get_table() an
2838a 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  d sqlite3_free_t
2838b 61 62 6c 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66  able().** interf
2838c 61 63 65 20 72 6f 75 74 69 6e 65 73 2e 20 20 54  ace routines.  T
2838d 68 65 73 65 20 61 72 65 20 6a 75 73 74 20 77 72  hese are just wr
2838e 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68  appers around th
2838f 65 20 6d 61 69 6e 0a 2a 2a 20 69 6e 74 65 72 66  e main.** interf
28390 61 63 65 20 72 6f 75 74 69 6e 65 20 6f 66 20 73  ace routine of s
28391 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a  qlite3_exec()..*
28392 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
28393 6e 65 73 20 61 72 65 20 69 6e 20 61 20 73 65 70  nes are in a sep
28394 61 72 61 74 65 20 66 69 6c 65 73 20 73 6f 20 74  arate files so t
28395 68 61 74 20 74 68 65 79 20 77 69 6c 6c 20 6e 6f  hat they will no
28396 74 20 62 65 20 6c 69 6e 6b 65 64 0a 2a 2a 20 69  t be linked.** i
28397 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 75  f they are not u
28398 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  sed..**.** $Id: 
28399 74 61 62 6c 65 2e 63 2c 76 20 31 2e 33 38 20 32  table.c,v 1.38 2
2839a 30 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a  008/12/10 19:26:
2839b 32 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  24 drh Exp $.*/.
2839c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2839d 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0a 0a  OMIT_GET_TABLE..
2839e 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  /*.** This struc
2839f 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20  ture is used to 
283a0 70 61 73 73 20 64 61 74 61 20 66 72 6f 6d 20 73  pass data from s
283a1 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
283a2 28 29 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f  () through.** to
283a3 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   the callback fu
283a4 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 73 20 74  nction is uses t
283a5 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65 73 75  o build the resu
283a6 6c 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  lt..*/.typedef s
283a7 74 72 75 63 74 20 54 61 62 52 65 73 75 6c 74 20  truct TabResult 
283a8 7b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65 73  {.  char **azRes
283a9 75 6c 74 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72  ult;.  char *zEr
283aa 72 4d 73 67 3b 0a 20 20 69 6e 74 20 6e 52 65 73  rMsg;.  int nRes
283ab 75 6c 74 3b 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f  ult;.  int nAllo
283ac 63 3b 0a 20 20 69 6e 74 20 6e 52 6f 77 3b 0a 20  c;.  int nRow;. 
283ad 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20   int nColumn;.  
283ae 69 6e 74 20 6e 44 61 74 61 3b 0a 20 20 69 6e 74  int nData;.  int
283af 20 72 63 3b 0a 7d 20 54 61 62 52 65 73 75 6c 74   rc;.} TabResult
283b0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  ;../*.** This ro
283b1 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
283b2 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f  once for each ro
283b3 77 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  w in the result 
283b4 74 61 62 6c 65 2e 20 20 49 74 73 20 6a 6f 62 0a  table.  Its job.
283b5 2a 2a 20 69 73 20 74 6f 20 66 69 6c 6c 20 69 6e  ** is to fill in
283b6 20 74 68 65 20 54 61 62 52 65 73 75 6c 74 20 73   the TabResult s
283b7 74 72 75 63 74 75 72 65 20 61 70 70 72 6f 70 72  tructure appropr
283b8 69 61 74 65 6c 79 2c 20 61 6c 6c 6f 63 61 74 69  iately, allocati
283b9 6e 67 20 6e 65 77 0a 2a 2a 20 6d 65 6d 6f 72 79  ng new.** memory
283ba 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   as necessary..*
283bb 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
283bc 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63  ite3_get_table_c
283bd 62 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 69 6e  b(void *pArg, in
283be 74 20 6e 43 6f 6c 2c 20 63 68 61 72 20 2a 2a 61  t nCol, char **a
283bf 72 67 76 2c 20 63 68 61 72 20 2a 2a 63 6f 6c 76  rgv, char **colv
283c0 29 7b 0a 20 20 54 61 62 52 65 73 75 6c 74 20 2a  ){.  TabResult *
283c1 70 20 3d 20 28 54 61 62 52 65 73 75 6c 74 2a 29  p = (TabResult*)
283c2 70 41 72 67 3b 0a 20 20 69 6e 74 20 6e 65 65 64  pArg;.  int need
283c3 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61  ;.  int i;.  cha
283c4 72 20 2a 7a 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65  r *z;..  /* Make
283c5 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20 65   sure there is e
283c6 6e 6f 75 67 68 20 73 70 61 63 65 20 69 6e 20 70  nough space in p
283c7 2d 3e 61 7a 52 65 73 75 6c 74 20 74 6f 20 68 6f  ->azResult to ho
283c8 6c 64 20 65 76 65 72 79 74 68 69 6e 67 0a 20 20  ld everything.  
283c9 2a 2a 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65  ** we need to re
283ca 6d 65 6d 62 65 72 20 66 72 6f 6d 20 74 68 69 73  member from this
283cb 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
283cc 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 20 20 2a  he callback..  *
283cd 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 52 6f 77 3d  /.  if( p->nRow=
283ce 3d 30 20 26 26 20 61 72 67 76 21 3d 30 20 29 7b  =0 && argv!=0 ){
283cf 0a 20 20 20 20 6e 65 65 64 20 3d 20 6e 43 6f 6c  .    need = nCol
283d0 2a 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  *2;.  }else{.   
283d1 20 6e 65 65 64 20 3d 20 6e 43 6f 6c 3b 0a 20 20   need = nCol;.  
283d2 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 44 61 74 61  }.  if( p->nData
283d3 20 2b 20 6e 65 65 64 20 3e 3d 20 70 2d 3e 6e 41   + need >= p->nA
283d4 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 72  lloc ){.    char
283d5 20 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20 70 2d   **azNew;.    p-
283d6 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c  >nAlloc = p->nAl
283d7 6c 6f 63 2a 32 20 2b 20 6e 65 65 64 20 2b 20 31  loc*2 + need + 1
283d8 3b 0a 20 20 20 20 61 7a 4e 65 77 20 3d 20 73 71  ;.    azNew = sq
283d9 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 20 70  lite3_realloc( p
283da 2d 3e 61 7a 52 65 73 75 6c 74 2c 20 73 69 7a 65  ->azResult, size
283db 6f 66 28 63 68 61 72 2a 29 2a 70 2d 3e 6e 41 6c  of(char*)*p->nAl
283dc 6c 6f 63 20 29 3b 0a 20 20 20 20 69 66 28 20 61  loc );.    if( a
283dd 7a 4e 65 77 3d 3d 30 20 29 20 67 6f 74 6f 20 6d  zNew==0 ) goto m
283de 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20  alloc_failed;.  
283df 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 20 3d 20    p->azResult = 
283e0 61 7a 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  azNew;.  }..  /*
283e1 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
283e2 66 69 72 73 74 20 72 6f 77 2c 20 74 68 65 6e 20  first row, then 
283e3 67 65 6e 65 72 61 74 65 20 61 6e 20 65 78 74 72  generate an extr
283e4 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67  a row containing
283e5 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20  .  ** the names 
283e6 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 2e 0a  of all columns..
283e7 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 52    */.  if( p->nR
283e8 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  ow==0 ){.    p->
283e9 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 3b 0a  nColumn = nCol;.
283ea 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
283eb 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
283ec 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72   z = sqlite3_mpr
283ed 69 6e 74 66 28 22 25 73 22 2c 20 63 6f 6c 76 5b  intf("%s", colv[
283ee 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  i]);.      if( z
283ef 3d 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f  ==0 ) goto mallo
283f0 63 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20  c_failed;.      
283f1 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e 6e  p->azResult[p->n
283f2 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20  Data++] = z;.   
283f3 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70   }.  }else if( p
283f4 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 6e 43 6f 6c 20  ->nColumn!=nCol 
283f5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
283f6 72 65 65 28 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  ree(p->zErrMsg);
283f7 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  .    p->zErrMsg 
283f8 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
283f9 66 28 0a 20 20 20 20 20 20 20 22 73 71 6c 69 74  f(.       "sqlit
283fa 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 63  e3_get_table() c
283fb 61 6c 6c 65 64 20 77 69 74 68 20 74 77 6f 20 6f  alled with two o
283fc 72 20 6d 6f 72 65 20 69 6e 63 6f 6d 70 61 74 69  r more incompati
283fd 62 6c 65 20 71 75 65 72 69 65 73 22 0a 20 20 20  ble queries".   
283fe 20 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   );.    p->rc = 
283ff 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
28400 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
28401 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20  .  /* Copy over 
28402 74 68 65 20 72 6f 77 20 64 61 74 61 0a 20 20 2a  the row data.  *
28403 2f 0a 20 20 69 66 28 20 61 72 67 76 21 3d 30 20  /.  if( argv!=0 
28404 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
28405 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
28406 20 20 20 20 69 66 28 20 61 72 67 76 5b 69 5d 3d      if( argv[i]=
28407 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 20  =0 ){.        z 
28408 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
28409 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20  {.        int n 
2840a 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2840b 30 28 61 72 67 76 5b 69 5d 29 2b 31 3b 0a 20 20  0(argv[i])+1;.  
2840c 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
2840d 33 5f 6d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20  3_malloc( n );. 
2840e 20 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20         if( z==0 
2840f 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61  ) goto malloc_fa
28410 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 6d 65  iled;.        me
28411 6d 63 70 79 28 7a 2c 20 61 72 67 76 5b 69 5d 2c  mcpy(z, argv[i],
28412 20 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   n);.      }.   
28413 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70     p->azResult[p
28414 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a  ->nData++] = z;.
28415 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 6f      }.    p->nRo
28416 77 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  w++;.  }.  retur
28417 6e 20 30 3b 0a 0a 6d 61 6c 6c 6f 63 5f 66 61 69  n 0;..malloc_fai
28418 6c 65 64 3a 0a 20 20 70 2d 3e 72 63 20 3d 20 53  led:.  p->rc = S
28419 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72  QLITE_NOMEM;.  r
2841a 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
2841b 2a 20 51 75 65 72 79 20 74 68 65 20 64 61 74 61  * Query the data
2841c 62 61 73 65 2e 20 20 42 75 74 20 69 6e 73 74 65  base.  But inste
2841d 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 61  ad of invoking a
2841e 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65 61   callback for ea
2841f 63 68 20 72 6f 77 2c 0a 2a 2a 20 6d 61 6c 6c 6f  ch row,.** mallo
28420 63 28 29 20 66 6f 72 20 73 70 61 63 65 20 74 6f  c() for space to
28421 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74   hold the result
28422 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20   and return the 
28423 65 6e 74 69 72 65 20 72 65 73 75 6c 74 73 0a 2a  entire results.*
28424 2a 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73  * at the conclus
28425 69 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 2e  ion of the call.
28426 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  .**.** The resul
28427 74 20 74 68 61 74 20 69 73 20 77 72 69 74 74 65  t that is writte
28428 6e 20 74 6f 20 2a 2a 2a 70 61 7a 52 65 73 75 6c  n to ***pazResul
28429 74 20 69 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d  t is held in mem
2842a 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20  ory obtained.** 
2842b 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20 20  from malloc().  
2842c 42 75 74 20 74 68 65 20 63 61 6c 6c 65 72 20 63  But the caller c
2842d 61 6e 6e 6f 74 20 66 72 65 65 20 74 68 69 73 20  annot free this 
2842e 6d 65 6d 6f 72 79 20 64 69 72 65 63 74 6c 79 2e  memory directly.
2842f 20 20 0a 2a 2a 20 49 6e 73 74 65 61 64 2c 20 74    .** Instead, t
28430 68 65 20 65 6e 74 69 72 65 20 74 61 62 6c 65 20  he entire table 
28431 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
28432 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65   to sqlite3_free
28433 5f 74 61 62 6c 65 28 29 20 77 68 65 6e 0a 2a 2a  _table() when.**
28434 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f   the calling pro
28435 63 65 64 75 72 65 20 69 73 20 66 69 6e 69 73 68  cedure is finish
28436 65 64 20 75 73 69 6e 67 20 69 74 2e 0a 2a 2f 0a  ed using it..*/.
28437 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
28438 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
28439 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2843a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2843b 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2843c 6f 6e 20 77 68 69 63 68 20 74 68 65 20 53 51 4c  on which the SQL
2843d 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 20 20 63   executes */.  c
2843e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
2843f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
28440 65 20 53 51 4c 20 74 6f 20 62 65 20 65 78 65 63  e SQL to be exec
28441 75 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  uted */.  char *
28442 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20 20  **pazResult,    
28443 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
28444 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20  he result table 
28445 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  here */.  int *p
28446 6e 52 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20  nRow,           
28447 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
28448 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
28449 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
2844a 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  here */.  int *p
2844b 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20  nColumn,        
2844c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
2844d 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
2844e 75 6d 6e 73 20 6f 66 20 72 65 73 75 6c 74 20 68  umns of result h
2844f 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ere */.  char **
28450 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 20  pzErrMsg        
28451 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72       /* Write er
28452 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 72  ror messages her
28453 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
28454 3b 0a 20 20 54 61 62 52 65 73 75 6c 74 20 72 65  ;.  TabResult re
28455 73 3b 0a 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74  s;..  *pazResult
28456 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 6e 43 6f   = 0;.  if( pnCo
28457 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e  lumn ) *pnColumn
28458 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 6e 52 6f   = 0;.  if( pnRo
28459 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20 30 3b 0a  w ) *pnRow = 0;.
2845a 20 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 3d 20    res.zErrMsg = 
2845b 30 3b 0a 20 20 72 65 73 2e 6e 52 65 73 75 6c 74  0;.  res.nResult
2845c 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 52 6f 77   = 0;.  res.nRow
2845d 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 43 6f 6c   = 0;.  res.nCol
2845e 75 6d 6e 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e  umn = 0;.  res.n
2845f 44 61 74 61 20 3d 20 31 3b 0a 20 20 72 65 73 2e  Data = 1;.  res.
28460 6e 41 6c 6c 6f 63 20 3d 20 32 30 3b 0a 20 20 72  nAlloc = 20;.  r
28461 65 73 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  es.rc = SQLITE_O
28462 4b 3b 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c  K;.  res.azResul
28463 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  t = sqlite3_mall
28464 6f 63 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29  oc(sizeof(char*)
28465 2a 72 65 73 2e 6e 41 6c 6c 6f 63 20 29 3b 0a 20  *res.nAlloc );. 
28466 20 69 66 28 20 72 65 73 2e 61 7a 52 65 73 75 6c   if( res.azResul
28467 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 64 62 2d  t==0 ){.     db-
28468 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54  >errCode = SQLIT
28469 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 72 65  E_NOMEM;.     re
2846a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2846b 4d 3b 0a 20 20 7d 0a 20 20 72 65 73 2e 61 7a 52  M;.  }.  res.azR
2846c 65 73 75 6c 74 5b 30 5d 20 3d 20 30 3b 0a 20 20  esult[0] = 0;.  
2846d 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
2846e 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69  c(db, zSql, sqli
2846f 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62  te3_get_table_cb
28470 2c 20 26 72 65 73 2c 20 70 7a 45 72 72 4d 73 67  , &res, pzErrMsg
28471 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  );.  assert( siz
28472 65 6f 66 28 72 65 73 2e 61 7a 52 65 73 75 6c 74  eof(res.azResult
28473 5b 30 5d 29 3e 3d 20 73 69 7a 65 6f 66 28 72 65  [0])>= sizeof(re
28474 73 2e 6e 44 61 74 61 29 20 29 3b 0a 20 20 72 65  s.nData) );.  re
28475 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20  s.azResult[0] = 
28476 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54  SQLITE_INT_TO_PT
28477 52 28 72 65 73 2e 6e 44 61 74 61 29 3b 0a 20 20  R(res.nData);.  
28478 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53  if( (rc&0xff)==S
28479 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20  QLITE_ABORT ){. 
2847a 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f     sqlite3_free_
2847b 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73  table(&res.azRes
2847c 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28  ult[1]);.    if(
2847d 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 29 7b 0a   res.zErrMsg ){.
2847e 20 20 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d        if( pzErrM
2847f 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  sg ){.        sq
28480 6c 69 74 65 33 5f 66 72 65 65 28 2a 70 7a 45 72  lite3_free(*pzEr
28481 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a  rMsg);.        *
28482 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
28483 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
28484 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  res.zErrMsg);.  
28485 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
28486 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72  te3_free(res.zEr
28487 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rMsg);.    }.   
28488 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72   db->errCode = r
28489 65 73 2e 72 63 3b 20 20 2f 2a 20 41 73 73 75 6d  es.rc;  /* Assum
2848a 65 20 33 32 2d 62 69 74 20 61 73 73 69 67 6e 6d  e 32-bit assignm
2848b 65 6e 74 20 69 73 20 61 74 6f 6d 69 63 20 2a 2f  ent is atomic */
2848c 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65 73 2e  .    return res.
2848d 72 63 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  rc;.  }.  sqlite
2848e 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d  3_free(res.zErrM
2848f 73 67 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  sg);.  if( rc!=S
28490 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
28491 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
28492 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74  le(&res.azResult
28493 5b 31 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  [1]);.    return
28494 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72   rc;.  }.  if( r
28495 65 73 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e 6e 44  es.nAlloc>res.nD
28496 61 74 61 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ata ){.    char 
28497 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20 61 7a 4e  **azNew;.    azN
28498 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
28499 6c 6c 6f 63 28 20 72 65 73 2e 61 7a 52 65 73 75  lloc( res.azResu
2849a 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a  lt, sizeof(char*
2849b 29 2a 28 72 65 73 2e 6e 44 61 74 61 2b 31 29 20  )*(res.nData+1) 
2849c 29 3b 0a 20 20 20 20 69 66 28 20 61 7a 4e 65 77  );.    if( azNew
2849d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
2849e 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
2849f 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d  &res.azResult[1]
284a0 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72  );.      db->err
284a1 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  Code = SQLITE_NO
284a2 4d 45 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72  MEM;.      retur
284a3 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
284a4 20 20 20 20 7d 0a 20 20 20 20 72 65 73 2e 6e 41      }.    res.nA
284a5 6c 6c 6f 63 20 3d 20 72 65 73 2e 6e 44 61 74 61  lloc = res.nData
284a6 2b 31 3b 0a 20 20 20 20 72 65 73 2e 61 7a 52 65  +1;.    res.azRe
284a7 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20  sult = azNew;.  
284a8 7d 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d  }.  *pazResult =
284a9 20 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31   &res.azResult[1
284aa 5d 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d  ];.  if( pnColum
284ab 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20  n ) *pnColumn = 
284ac 72 65 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69  res.nColumn;.  i
284ad 66 28 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f  f( pnRow ) *pnRo
284ae 77 20 3d 20 72 65 73 2e 6e 52 6f 77 3b 0a 20 20  w = res.nRow;.  
284af 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
284b0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
284b1 20 66 72 65 65 73 20 74 68 65 20 73 70 61 63 65   frees the space
284b2 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74   the sqlite3_get
284b3 5f 74 61 62 6c 65 28 29 20 6d 61 6c 6c 6f 63 65  _table() malloce
284b4 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
284b5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72   void sqlite3_fr
284b6 65 65 5f 74 61 62 6c 65 28 0a 20 20 63 68 61 72  ee_table(.  char
284b7 20 2a 2a 61 7a 52 65 73 75 6c 74 20 20 20 20 20   **azResult     
284b8 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
284b9 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 66   returned from f
284ba 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  rom sqlite3_get_
284bb 74 61 62 6c 65 28 29 20 2a 2f 0a 29 7b 0a 20 20  table() */.){.  
284bc 69 66 28 20 61 7a 52 65 73 75 6c 74 20 29 7b 0a  if( azResult ){.
284bd 20 20 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20      int i, n;.  
284be 20 20 61 7a 52 65 73 75 6c 74 2d 2d 3b 0a 20 20    azResult--;.  
284bf 20 20 61 73 73 65 72 74 28 20 61 7a 52 65 73 75    assert( azResu
284c0 6c 74 21 3d 30 20 29 3b 0a 20 20 20 20 6e 20 3d  lt!=0 );.    n =
284c1 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
284c2 4e 54 28 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3b  NT(azResult[0]);
284c3 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  .    for(i=1; i<
284c4 6e 3b 20 69 2b 2b 29 7b 20 69 66 28 20 61 7a 52  n; i++){ if( azR
284c5 65 73 75 6c 74 5b 69 5d 20 29 20 73 71 6c 69 74  esult[i] ) sqlit
284c6 65 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74  e3_free(azResult
284c7 5b 69 5d 29 3b 20 7d 0a 20 20 20 20 73 71 6c 69  [i]); }.    sqli
284c8 74 65 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c  te3_free(azResul
284c9 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69  t);.  }.}..#endi
284ca 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
284cb 5f 47 45 54 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f  _GET_TABLE */../
284cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
284cd 6e 64 20 6f 66 20 74 61 62 6c 65 2e 63 20 2a 2a  nd of table.c **
284ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
284cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
284d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
284d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
284d2 65 67 69 6e 20 66 69 6c 65 20 74 72 69 67 67 65  egin file trigge
284d3 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
284d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
284d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
284d6 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  *.**.** The auth
284d7 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
284d8 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
284d9 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
284da 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
284db 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
284dc 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
284dd 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
284de 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
284df 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
284e0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
284e1 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
284e2 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
284e3 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
284e4 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
284e5 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
284e6 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
284e7 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
284e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
284e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
284ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
284eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
284ec 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74  .**.**.** $Id: t
284ed 72 69 67 67 65 72 2e 63 2c 76 20 31 2e 31 33 33  rigger.c,v 1.133
284ee 20 32 30 30 38 2f 31 32 2f 32 36 20 30 37 3a 35   2008/12/26 07:5
284ef 36 3a 33 39 20 64 61 6e 69 65 6c 6b 31 39 37 37  6:39 danielk1977
284f0 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64   Exp $.*/..#ifnd
284f1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
284f2 52 49 47 47 45 52 0a 2f 2a 0a 2a 2a 20 44 65 6c  RIGGER./*.** Del
284f3 65 74 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  ete a linked lis
284f4 74 20 6f 66 20 54 72 69 67 67 65 72 53 74 65 70  t of TriggerStep
284f5 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a   structures..*/.
284f6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
284f7 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
284f8 65 54 72 69 67 67 65 72 53 74 65 70 28 73 71 6c  eTriggerStep(sql
284f9 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65  ite3 *db, Trigge
284fa 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53  rStep *pTriggerS
284fb 74 65 70 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  tep){.  while( p
284fc 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20  TriggerStep ){. 
284fd 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a     TriggerStep *
284fe 20 70 54 6d 70 20 3d 20 70 54 72 69 67 67 65 72   pTmp = pTrigger
284ff 53 74 65 70 3b 0a 20 20 20 20 70 54 72 69 67 67  Step;.    pTrigg
28500 65 72 53 74 65 70 20 3d 20 70 54 72 69 67 67 65  erStep = pTrigge
28501 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 0a 20  rStep->pNext;.. 
28502 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 74 61 72     if( pTmp->tar
28503 67 65 74 2e 64 79 6e 20 29 20 73 71 6c 69 74 65  get.dyn ) sqlite
28504 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 61  3DbFree(db, (cha
28505 72 2a 29 70 54 6d 70 2d 3e 74 61 72 67 65 74 2e  r*)pTmp->target.
28506 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  z);.    sqlite3E
28507 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 54  xprDelete(db, pT
28508 6d 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20  mp->pWhere);.   
28509 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2850a 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d  Delete(db, pTmp-
2850b 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a 20 20 20  >pExprList);.   
2850c 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2850d 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70  lete(db, pTmp->p
2850e 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c  Select);.    sql
2850f 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
28510 28 64 62 2c 20 70 54 6d 70 2d 3e 70 49 64 4c 69  (db, pTmp->pIdLi
28511 73 74 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  st);..    sqlite
28512 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70  3DbFree(db, pTmp
28513 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
28514 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 62  This is called b
28515 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68 65  y the parser whe
28516 6e 20 69 74 20 73 65 65 73 20 61 20 43 52 45 41  n it sees a CREA
28517 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65  TE TRIGGER state
28518 6d 65 6e 74 0a 2a 2a 20 75 70 20 74 6f 20 74 68  ment.** up to th
28519 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 65 20 42  e point of the B
2851a 45 47 49 4e 20 62 65 66 6f 72 65 20 74 68 65 20  EGIN before the 
2851b 74 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73 2e  trigger actions.
2851c 20 20 41 20 54 72 69 67 67 65 72 0a 2a 2a 20 73    A Trigger.** s
2851d 74 72 75 63 74 75 72 65 20 69 73 20 67 65 6e 65  tructure is gene
2851e 72 61 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74  rated based on t
2851f 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  he information a
28520 76 61 69 6c 61 62 6c 65 20 61 6e 64 20 73 74 6f  vailable and sto
28521 72 65 64 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65  red.** in pParse
28522 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 2e 20 20  ->pNewTrigger.  
28523 41 66 74 65 72 20 74 68 65 20 74 72 69 67 67 65  After the trigge
28524 72 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62  r actions have b
28525 65 65 6e 20 70 61 72 73 65 64 2c 20 74 68 65 0a  een parsed, the.
28526 2a 2a 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  ** sqlite3Finish
28527 54 72 69 67 67 65 72 28 29 20 66 75 6e 63 74 69  Trigger() functi
28528 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
28529 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 74 72 69  complete the tri
2852a 67 67 65 72 0a 2a 2a 20 63 6f 6e 73 74 72 75 63  gger.** construc
2852b 74 69 6f 6e 20 70 72 6f 63 65 73 73 2e 0a 2a 2f  tion process..*/
2852c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2852d 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
2852e 6e 54 72 69 67 67 65 72 28 0a 20 20 50 61 72 73  nTrigger(.  Pars
2852f 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
28530 2f 2a 20 54 68 65 20 70 61 72 73 65 20 63 6f 6e  /* The parse con
28531 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41  text of the CREA
28532 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65  TE TRIGGER state
28533 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  ment */.  Token 
28534 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a  *pName1,      /*
28535 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
28536 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 54 6f   trigger */.  To
28537 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20  ken *pName2,    
28538 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66    /* The name of
28539 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a   the trigger */.
2853a 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20    int tr_tm,    
2853b 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
2853c 54 4b 5f 42 45 46 4f 52 45 2c 20 54 4b 5f 41 46  TK_BEFORE, TK_AF
2853d 54 45 52 2c 20 54 4b 5f 49 4e 53 54 45 41 44 20  TER, TK_INSTEAD 
2853e 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
2853f 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
28540 6f 66 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b  of TK_INSERT, TK
28541 5f 55 50 44 41 54 45 2c 20 54 4b 5f 44 45 4c 45  _UPDATE, TK_DELE
28542 54 45 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a  TE */.  IdList *
28543 70 43 6f 6c 75 6d 6e 73 2c 20 20 20 2f 2a 20 63  pColumns,   /* c
28544 6f 6c 75 6d 6e 20 6c 69 73 74 20 69 66 20 74 68  olumn list if th
28545 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 45 20  is is an UPDATE 
28546 4f 46 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  OF trigger */.  
28547 53 72 63 4c 69 73 74 20 2a 70 54 61 62 6c 65 4e  SrcList *pTableN
28548 61 6d 65 2c 2f 2a 20 54 68 65 20 6e 61 6d 65 20  ame,/* The name 
28549 6f 66 20 74 68 65 20 74 61 62 6c 65 2f 76 69 65  of the table/vie
2854a 77 20 74 68 65 20 74 72 69 67 67 65 72 20 61 70  w the trigger ap
2854b 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 45 78  plies to */.  Ex
2854c 70 72 20 2a 70 57 68 65 6e 2c 20 20 20 20 20 20  pr *pWhen,      
2854d 20 20 2f 2a 20 57 48 45 4e 20 63 6c 61 75 73 65    /* WHEN clause
2854e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70   */.  int isTemp
2854f 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ,         /* Tru
28550 65 20 69 66 20 74 68 65 20 54 45 4d 50 4f 52 41  e if the TEMPORA
28551 52 59 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72  RY keyword is pr
28552 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  esent */.  int n
28553 6f 45 72 72 20 20 20 20 20 20 20 20 20 20 20 2f  oErr           /
28554 2a 20 53 75 70 70 72 65 73 73 20 65 72 72 6f 72  * Suppress error
28555 73 20 69 66 20 74 68 65 20 74 72 69 67 67 65 72  s if the trigger
28556 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
28557 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 20  */.){.  Trigger 
28558 2a 70 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20  *pTrigger = 0;. 
28559 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
2855a 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b  char *zName = 0;
2855b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2855c 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20 2a  of the trigger *
2855d 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2855e 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2855f 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
28560 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
28561 61 74 61 62 61 73 65 20 74 6f 20 73 74 6f 72 65  atabase to store
28562 20 74 68 65 20 74 72 69 67 67 65 72 20 69 6e 20   the trigger in 
28563 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
28564 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
28565 54 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20  The unqualified 
28566 64 62 20 6e 61 6d 65 20 2a 2f 0a 20 20 44 62 46  db name */.  DbF
28567 69 78 65 72 20 73 46 69 78 3b 0a 20 20 69 6e 74  ixer sFix;.  int
28568 20 69 54 61 62 44 62 3b 0a 0a 20 20 61 73 73 65   iTabDb;..  asse
28569 72 74 28 20 70 4e 61 6d 65 31 21 3d 30 20 29 3b  rt( pName1!=0 );
2856a 20 20 20 2f 2a 20 70 4e 61 6d 65 31 2d 3e 7a 20     /* pName1->z 
2856b 6d 69 67 68 74 20 62 65 20 4e 55 4c 4c 2c 20 62  might be NULL, b
2856c 75 74 20 6e 6f 74 20 70 4e 61 6d 65 31 20 69 74  ut not pName1 it
2856d 73 65 6c 66 20 2a 2f 0a 20 20 61 73 73 65 72 74  self */.  assert
2856e 28 20 70 4e 61 6d 65 32 21 3d 30 20 29 3b 0a 20  ( pName2!=0 );. 
2856f 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f   assert( op==TK_
28570 49 4e 53 45 52 54 20 7c 7c 20 6f 70 3d 3d 54 4b  INSERT || op==TK
28571 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54  _UPDATE || op==T
28572 4b 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 61 73  K_DELETE );.  as
28573 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20 6f 70  sert( op>0 && op
28574 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28 20 69  <0xff );.  if( i
28575 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 2f 2a 20  sTemp ){.    /* 
28576 49 66 20 54 45 4d 50 20 77 61 73 20 73 70 65 63  If TEMP was spec
28577 69 66 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20  ified, then the 
28578 74 72 69 67 67 65 72 20 6e 61 6d 65 20 6d 61 79  trigger name may
28579 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65   not be qualifie
2857a 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4e  d. */.    if( pN
2857b 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20  ame2->n>0 ){.   
2857c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2857d 73 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70  sg(pParse, "temp
2857e 6f 72 61 72 79 20 74 72 69 67 67 65 72 20 6d 61  orary trigger ma
2857f 79 20 6e 6f 74 20 68 61 76 65 20 71 75 61 6c 69  y not have quali
28580 66 69 65 64 20 6e 61 6d 65 22 29 3b 0a 20 20 20  fied name");.   
28581 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
28582 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20  cleanup;.    }. 
28583 20 20 20 69 44 62 20 3d 20 31 3b 0a 20 20 20 20     iDb = 1;.    
28584 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 31 3b 0a  pName = pName1;.
28585 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
28586 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 64  Figure out the d
28587 62 20 74 68 61 74 20 74 68 65 20 74 68 65 20 74  b that the the t
28588 72 69 67 67 65 72 20 77 69 6c 6c 20 62 65 20 63  rigger will be c
28589 72 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 20  reated in */.   
2858a 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77   iDb = sqlite3Tw
2858b 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65  oPartName(pParse
2858c 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32  , pName1, pName2
2858d 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69  , &pName);.    i
2858e 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20  f( iDb<0 ){.    
2858f 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
28590 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
28591 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74  }..  /* If the t
28592 72 69 67 67 65 72 20 6e 61 6d 65 20 77 61 73 20  rigger name was 
28593 75 6e 71 75 61 6c 69 66 69 65 64 2c 20 61 6e 64  unqualified, and
28594 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20   the table is a 
28595 74 65 6d 70 20 74 61 62 6c 65 2c 0a 20 20 2a 2a  temp table,.  **
28596 20 74 68 65 6e 20 73 65 74 20 69 44 62 20 74 6f   then set iDb to
28597 20 31 20 74 6f 20 63 72 65 61 74 65 20 74 68 65   1 to create the
28598 20 74 72 69 67 67 65 72 20 69 6e 20 74 68 65 20   trigger in the 
28599 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
2859a 73 65 2e 0a 20 20 2a 2a 20 49 66 20 73 71 6c 69  se..  ** If sqli
2859b 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70  te3SrcListLookup
2859c 28 29 20 72 65 74 75 72 6e 73 20 30 2c 20 69 6e  () returns 0, in
2859d 64 69 63 61 74 69 6e 67 20 74 68 65 20 74 61 62  dicating the tab
2859e 6c 65 20 64 6f 65 73 20 6e 6f 74 0a 20 20 2a 2a  le does not.  **
2859f 20 65 78 69 73 74 2c 20 74 68 65 20 65 72 72 6f   exist, the erro
285a0 72 20 69 73 20 63 61 75 67 68 74 20 62 79 20 74  r is caught by t
285a1 68 65 20 62 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0a  he block below..
285a2 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62    */.  if( !pTab
285a3 6c 65 4e 61 6d 65 20 7c 7c 20 64 62 2d 3e 6d 61  leName || db->ma
285a4 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
285a5 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
285a6 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54  leanup;.  }.  pT
285a7 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  ab = sqlite3SrcL
285a8 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  istLookup(pParse
285a9 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20  , pTableName);. 
285aa 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d   if( pName2->n==
285ab 30 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61  0 && pTab && pTa
285ac 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e  b->pSchema==db->
285ad 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29  aDb[1].pSchema )
285ae 7b 0a 20 20 20 20 69 44 62 20 3d 20 31 3b 0a 20  {.    iDb = 1;. 
285af 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20   }..  /* Ensure 
285b0 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d  the table name m
285b1 61 74 63 68 65 73 20 64 61 74 61 62 61 73 65 20  atches database 
285b2 6e 61 6d 65 20 61 6e 64 20 74 68 61 74 20 74 68  name and that th
285b3 65 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 2a  e table exists *
285b4 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  /.  if( db->mall
285b5 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20  ocFailed ) goto 
285b6 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
285b7 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c  .  assert( pTabl
285b8 65 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29  eName->nSrc==1 )
285b9 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46  ;.  if( sqlite3F
285ba 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50  ixInit(&sFix, pP
285bb 61 72 73 65 2c 20 69 44 62 2c 20 22 74 72 69 67  arse, iDb, "trig
285bc 67 65 72 22 2c 20 70 4e 61 6d 65 29 20 26 26 20  ger", pName) && 
285bd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 69  .      sqlite3Fi
285be 78 53 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20  xSrcList(&sFix, 
285bf 70 54 61 62 6c 65 4e 61 6d 65 29 20 29 7b 0a 20  pTableName) ){. 
285c0 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
285c1 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70  cleanup;.  }.  p
285c2 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
285c3 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
285c4 65 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a  e, pTableName);.
285c5 20 20 69 66 28 20 21 70 54 61 62 20 29 7b 0a 20    if( !pTab ){. 
285c6 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
285c7 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20  does not exist. 
285c8 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  */.    goto trig
285c9 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
285ca 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
285cb 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71  (pTab) ){.    sq
285cc 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
285cd 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72  arse, "cannot cr
285ce 65 61 74 65 20 74 72 69 67 67 65 72 73 20 6f 6e  eate triggers on
285cf 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 22   virtual tables"
285d0 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  );.    goto trig
285d1 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
285d2 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
285d3 74 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61  t the trigger na
285d4 6d 65 20 69 73 20 6e 6f 74 20 72 65 73 65 72 76  me is not reserv
285d5 65 64 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 74  ed and that no t
285d6 72 69 67 67 65 72 20 6f 66 20 74 68 65 0a 20 20  rigger of the.  
285d7 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e 61 6d  ** specified nam
285d8 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 7a 4e  e exists */.  zN
285d9 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
285da 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
285db 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 4e  Name);.  if( !zN
285dc 61 6d 65 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b  ame || SQLITE_OK
285dd 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62  !=sqlite3CheckOb
285de 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  jectName(pParse,
285df 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67   zName) ){.    g
285e0 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
285e1 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  nup;.  }.  if( s
285e2 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
285e3 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53  (db->aDb[iDb].pS
285e4 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29  chema->trigHash)
285e5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
285e6 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 73          zName, s
285e7 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
285e8 4e 61 6d 65 29 29 20 29 7b 0a 20 20 20 20 69 66  Name)) ){.    if
285e9 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20  ( !noErr ){.    
285ea 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
285eb 67 28 70 50 61 72 73 65 2c 20 22 74 72 69 67 67  g(pParse, "trigg
285ec 65 72 20 25 54 20 61 6c 72 65 61 64 79 20 65 78  er %T already ex
285ed 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a 20  ists", pName);. 
285ee 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 74 72     }.    goto tr
285ef 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
285f0 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20   }..  /* Do not 
285f1 63 72 65 61 74 65 20 61 20 74 72 69 67 67 65 72  create a trigger
285f2 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 61 62   on a system tab
285f3 6c 65 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  le */.  if( sqli
285f4 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62  te3StrNICmp(pTab
285f5 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65  ->zName, "sqlite
285f6 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20  _", 7)==0 ){.   
285f7 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
285f8 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
285f9 20 63 72 65 61 74 65 20 74 72 69 67 67 65 72 20   create trigger 
285fa 6f 6e 20 73 79 73 74 65 6d 20 74 61 62 6c 65 22  on system table"
285fb 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  );.    pParse->n
285fc 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20  Err++;.    goto 
285fd 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
285fe 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 4e 53 54 45  .  }..  /* INSTE
285ff 41 44 20 6f 66 20 74 72 69 67 67 65 72 73 20 61  AD of triggers a
28600 72 65 20 6f 6e 6c 79 20 66 6f 72 20 76 69 65 77  re only for view
28601 73 20 61 6e 64 20 76 69 65 77 73 20 6f 6e 6c 79  s and views only
28602 20 73 75 70 70 6f 72 74 20 49 4e 53 54 45 41 44   support INSTEAD
28603 0a 20 20 2a 2a 20 6f 66 20 74 72 69 67 67 65 72  .  ** of trigger
28604 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  s..  */.  if( pT
28605 61 62 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 74  ab->pSelect && t
28606 72 5f 74 6d 21 3d 54 4b 5f 49 4e 53 54 45 41 44  r_tm!=TK_INSTEAD
28607 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
28608 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
28609 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 25  "cannot create %
2860a 73 20 74 72 69 67 67 65 72 20 6f 6e 20 76 69 65  s trigger on vie
2860b 77 3a 20 25 53 22 2c 20 0a 20 20 20 20 20 20 20  w: %S", .       
2860c 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f 42 45   (tr_tm == TK_BE
2860d 46 4f 52 45 29 3f 22 42 45 46 4f 52 45 22 3a 22  FORE)?"BEFORE":"
2860e 41 46 54 45 52 22 2c 20 70 54 61 62 6c 65 4e 61  AFTER", pTableNa
2860f 6d 65 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f  me, 0);.    goto
28610 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
28611 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 54 61  ;.  }.  if( !pTa
28612 62 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 74 72  b->pSelect && tr
28613 5f 74 6d 3d 3d 54 4b 5f 49 4e 53 54 45 41 44 20  _tm==TK_INSTEAD 
28614 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
28615 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
28616 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 49 4e  cannot create IN
28617 53 54 45 41 44 20 4f 46 22 0a 20 20 20 20 20 20  STEAD OF".      
28618 20 20 22 20 74 72 69 67 67 65 72 20 6f 6e 20 74    " trigger on t
28619 61 62 6c 65 3a 20 25 53 22 2c 20 70 54 61 62 6c  able: %S", pTabl
2861a 65 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 67  eName, 0);.    g
2861b 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2861c 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 54 61 62 44  nup;.  }.  iTabD
2861d 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2861e 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
2861f 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69  b->pSchema);..#i
28620 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28621 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
28622 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65    {.    int code
28623 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
28624 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 63 6f  _TRIGGER;.    co
28625 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
28626 64 62 2d 3e 61 44 62 5b 69 54 61 62 44 62 5d 2e  db->aDb[iTabDb].
28627 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74  zName;.    const
28628 20 63 68 61 72 20 2a 7a 44 62 54 72 69 67 20 3d   char *zDbTrig =
28629 20 69 73 54 65 6d 70 20 3f 20 64 62 2d 3e 61 44   isTemp ? db->aD
2862a 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3a 20 7a 44 62  b[1].zName : zDb
2862b 3b 0a 20 20 20 20 69 66 28 20 69 54 61 62 44 62  ;.    if( iTabDb
2862c 3d 3d 31 20 7c 7c 20 69 73 54 65 6d 70 20 29 20  ==1 || isTemp ) 
2862d 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52  code = SQLITE_CR
2862e 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 47 45  EATE_TEMP_TRIGGE
2862f 52 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  R;.    if( sqlit
28630 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
28631 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c  se, code, zName,
28632 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44   pTab->zName, zD
28633 62 54 72 69 67 29 20 29 7b 0a 20 20 20 20 20 20  bTrig) ){.      
28634 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
28635 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20  anup;.    }.    
28636 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
28637 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
28638 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45  ITE_INSERT, SCHE
28639 4d 41 5f 54 41 42 4c 45 28 69 54 61 62 44 62 29  MA_TABLE(iTabDb)
2863a 2c 30 2c 7a 44 62 29 29 7b 0a 20 20 20 20 20 20  ,0,zDb)){.      
2863b 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
2863c 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  anup;.    }.  }.
2863d 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 4e 53  #endif..  /* INS
2863e 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 73  TEAD OF triggers
2863f 20 63 61 6e 20 6f 6e 6c 79 20 61 70 70 65 61 72   can only appear
28640 20 6f 6e 20 76 69 65 77 73 20 61 6e 64 20 42 45   on views and BE
28641 46 4f 52 45 20 74 72 69 67 67 65 72 73 0a 20 20  FORE triggers.  
28642 2a 2a 20 63 61 6e 6e 6f 74 20 61 70 70 65 61 72  ** cannot appear
28643 20 6f 6e 20 76 69 65 77 73 2e 20 20 53 6f 20 77   on views.  So w
28644 65 20 6d 69 67 68 74 20 61 73 20 77 65 6c 6c 20  e might as well 
28645 74 72 61 6e 73 6c 61 74 65 20 65 76 65 72 79 0a  translate every.
28646 20 20 2a 2a 20 49 4e 53 54 45 41 44 20 4f 46 20    ** INSTEAD OF 
28647 74 72 69 67 67 65 72 20 69 6e 74 6f 20 61 20 42  trigger into a B
28648 45 46 4f 52 45 20 74 72 69 67 67 65 72 2e 20 20  EFORE trigger.  
28649 49 74 20 73 69 6d 70 6c 69 66 69 65 73 20 63 6f  It simplifies co
2864a 64 65 0a 20 20 2a 2a 20 65 6c 73 65 77 68 65 72  de.  ** elsewher
2864b 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 20 28 74 72  e..  */.  if (tr
2864c 5f 74 6d 20 3d 3d 20 54 4b 5f 49 4e 53 54 45 41  _tm == TK_INSTEA
2864d 44 29 7b 0a 20 20 20 20 74 72 5f 74 6d 20 3d 20  D){.    tr_tm = 
2864e 54 4b 5f 42 45 46 4f 52 45 3b 0a 20 20 7d 0a 0a  TK_BEFORE;.  }..
2864f 20 20 2f 2a 20 42 75 69 6c 64 20 74 68 65 20 54    /* Build the T
28650 72 69 67 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f  rigger object */
28651 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 28 54  .  pTrigger = (T
28652 72 69 67 67 65 72 2a 29 73 71 6c 69 74 65 33 44  rigger*)sqlite3D
28653 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
28654 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 29 29  sizeof(Trigger))
28655 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ;.  if( pTrigger
28656 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72 69 67 67  ==0 ) goto trigg
28657 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 70 54  er_cleanup;.  pT
28658 72 69 67 67 65 72 2d 3e 6e 61 6d 65 20 3d 20 7a  rigger->name = z
28659 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  Name;.  zName = 
2865a 30 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 74  0;.  pTrigger->t
2865b 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62  able = sqlite3Db
2865c 53 74 72 44 75 70 28 64 62 2c 20 70 54 61 62 6c  StrDup(db, pTabl
2865d 65 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  eName->a[0].zNam
2865e 65 29 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e  e);.  pTrigger->
2865f 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44  pSchema = db->aD
28660 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a  b[iDb].pSchema;.
28661 20 20 70 54 72 69 67 67 65 72 2d 3e 70 54 61 62    pTrigger->pTab
28662 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70  Schema = pTab->p
28663 53 63 68 65 6d 61 3b 0a 20 20 70 54 72 69 67 67  Schema;.  pTrigg
28664 65 72 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b  er->op = (u8)op;
28665 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 74 72 5f  .  pTrigger->tr_
28666 74 6d 20 3d 20 74 72 5f 74 6d 3d 3d 54 4b 5f 42  tm = tr_tm==TK_B
28667 45 46 4f 52 45 20 3f 20 54 52 49 47 47 45 52 5f  EFORE ? TRIGGER_
28668 42 45 46 4f 52 45 20 3a 20 54 52 49 47 47 45 52  BEFORE : TRIGGER
28669 5f 41 46 54 45 52 3b 0a 20 20 70 54 72 69 67 67  _AFTER;.  pTrigg
2866a 65 72 2d 3e 70 57 68 65 6e 20 3d 20 73 71 6c 69  er->pWhen = sqli
2866b 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
2866c 57 68 65 6e 29 3b 0a 20 20 70 54 72 69 67 67 65  When);.  pTrigge
2866d 72 2d 3e 70 43 6f 6c 75 6d 6e 73 20 3d 20 73 71  r->pColumns = sq
2866e 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 64  lite3IdListDup(d
2866f 62 2c 20 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20  b, pColumns);.  
28670 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79  sqlite3TokenCopy
28671 28 64 62 2c 20 26 70 54 72 69 67 67 65 72 2d 3e  (db, &pTrigger->
28672 6e 61 6d 65 54 6f 6b 65 6e 2c 70 4e 61 6d 65 29  nameToken,pName)
28673 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
28674 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3d  se->pNewTrigger=
28675 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  =0 );.  pParse->
28676 70 4e 65 77 54 72 69 67 67 65 72 20 3d 20 70 54  pNewTrigger = pT
28677 72 69 67 67 65 72 3b 0a 0a 74 72 69 67 67 65 72  rigger;..trigger
28678 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69  _cleanup:.  sqli
28679 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
2867a 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ame);.  sqlite3S
2867b 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
2867c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20   pTableName);.  
2867d 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
2867e 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 73  ete(db, pColumns
2867f 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
28680 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 6e  Delete(db, pWhen
28681 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73 65  );.  if( !pParse
28682 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 29 7b  ->pNewTrigger ){
28683 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
28684 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 54  teTrigger(db, pT
28685 72 69 67 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65  rigger);.  }else
28686 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
28687 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
28688 72 3d 3d 70 54 72 69 67 67 65 72 20 29 3b 0a 20  r==pTrigger );. 
28689 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
2868a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2868b 65 64 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20  ed after all of 
2868c 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74 69  the trigger acti
2868d 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70 61  ons have been pa
2868e 72 73 65 64 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  rsed.** in order
2868f 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
28690 20 70 72 6f 63 65 73 73 20 6f 66 20 62 75 69 6c   process of buil
28691 64 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72  ding the trigger
28692 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
28693 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
28694 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 0a 20  FinishTrigger(. 
28695 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
28696 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
28697 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
28698 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
28699 65 70 4c 69 73 74 2c 20 2f 2a 20 54 68 65 20 74  epList, /* The t
2869a 72 69 67 67 65 72 65 64 20 70 72 6f 67 72 61 6d  riggered program
2869b 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 6c   */.  Token *pAl
2869c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l             /*
2869d 20 54 6f 6b 65 6e 20 74 68 61 74 20 64 65 73 63   Token that desc
2869e 72 69 62 65 73 20 74 68 65 20 63 6f 6d 70 6c 65  ribes the comple
2869f 74 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45  te CREATE TRIGGE
286a0 52 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65  R */.){.  Trigge
286a1 72 20 2a 70 54 72 69 67 20 3d 20 30 3b 20 20 20  r *pTrig = 0;   
286a2 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
286a3 20 77 68 6f 73 65 20 63 6f 6e 73 74 72 75 63 74   whose construct
286a4 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 69 6e 67  ion is finishing
286a5 20 75 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33   up */.  sqlite3
286a6 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
286a7 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  b;  /* The datab
286a8 61 73 65 20 2a 2f 0a 20 20 44 62 46 69 78 65 72  ase */.  DbFixer
286a9 20 73 46 69 78 3b 0a 20 20 69 6e 74 20 69 44 62   sFix;.  int iDb
286aa 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
286ab 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
286ac 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
286ad 72 69 67 67 65 72 20 2a 2f 0a 0a 20 20 70 54 72  rigger */..  pTr
286ae 69 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ig = pParse->pNe
286af 77 54 72 69 67 67 65 72 3b 0a 20 20 70 50 61 72  wTrigger;.  pPar
286b0 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20  se->pNewTrigger 
286b1 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 72 73  = 0;.  if( pPars
286b2 65 2d 3e 6e 45 72 72 20 7c 7c 20 21 70 54 72 69  e->nErr || !pTri
286b3 67 20 29 20 67 6f 74 6f 20 74 72 69 67 67 65 72  g ) goto trigger
286b4 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0a  finish_cleanup;.
286b5 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
286b6 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
286b7 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e  rse->db, pTrig->
286b8 70 53 63 68 65 6d 61 29 3b 0a 20 20 70 54 72 69  pSchema);.  pTri
286b9 67 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d 20 70  g->step_list = p
286ba 53 74 65 70 4c 69 73 74 3b 0a 20 20 77 68 69 6c  StepList;.  whil
286bb 65 28 20 70 53 74 65 70 4c 69 73 74 20 29 7b 0a  e( pStepList ){.
286bc 20 20 20 20 70 53 74 65 70 4c 69 73 74 2d 3e 70      pStepList->p
286bd 54 72 69 67 20 3d 20 70 54 72 69 67 3b 0a 20 20  Trig = pTrig;.  
286be 20 20 70 53 74 65 70 4c 69 73 74 20 3d 20 70 53    pStepList = pS
286bf 74 65 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a  tepList->pNext;.
286c0 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
286c1 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20  3FixInit(&sFix, 
286c2 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 74 72  pParse, iDb, "tr
286c3 69 67 67 65 72 22 2c 20 26 70 54 72 69 67 2d 3e  igger", &pTrig->
286c4 6e 61 6d 65 54 6f 6b 65 6e 29 20 0a 20 20 20 20  nameToken) .    
286c5 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
286c6 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 26  FixTriggerStep(&
286c7 73 46 69 78 2c 20 70 54 72 69 67 2d 3e 73 74 65  sFix, pTrig->ste
286c8 70 5f 6c 69 73 74 29 20 29 7b 0a 20 20 20 20 67  p_list) ){.    g
286c9 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73  oto triggerfinis
286ca 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  h_cleanup;.  }..
286cb 20 20 2f 2a 20 69 66 20 77 65 20 61 72 65 20 6e    /* if we are n
286cc 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 2c  ot initializing,
286cd 20 61 6e 64 20 74 68 69 73 20 74 72 69 67 67 65   and this trigge
286ce 72 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 54 45  r is not on a TE
286cf 4d 50 20 74 61 62 6c 65 2c 20 0a 20 20 2a 2a 20  MP table, .  ** 
286d0 62 75 69 6c 64 20 74 68 65 20 73 71 6c 69 74 65  build the sqlite
286d1 5f 6d 61 73 74 65 72 20 65 6e 74 72 79 0a 20 20  _master entry.  
286d2 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  */.  if( !db->in
286d3 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 56  it.busy ){.    V
286d4 64 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72  dbe *v;.    char
286d5 20 2a 7a 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b   *z;..    /* Mak
286d6 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68  e an entry in th
286d7 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
286d8 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 76 20 3d  table */.    v =
286d9 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
286da 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28  pParse);.    if(
286db 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72 69   v==0 ) goto tri
286dc 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e  ggerfinish_clean
286dd 75 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  up;.    sqlite3B
286de 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
286df 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
286e0 62 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  b);.    z = sqli
286e1 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
286e2 20 28 63 68 61 72 2a 29 70 41 6c 6c 2d 3e 7a 2c   (char*)pAll->z,
286e3 20 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20 20 20 73   pAll->n);.    s
286e4 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
286e5 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
286e6 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51   "INSERT INTO %Q
286e7 2e 25 73 20 56 41 4c 55 45 53 28 27 74 72 69 67  .%s VALUES('trig
286e8 67 65 72 27 2c 25 51 2c 25 51 2c 30 2c 27 43 52  ger',%Q,%Q,0,'CR
286e9 45 41 54 45 20 54 52 49 47 47 45 52 20 25 71 27  EATE TRIGGER %q'
286ea 29 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61  )",.       db->a
286eb 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
286ec 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
286ed 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 2c 0a 20  , pTrig->name,. 
286ee 20 20 20 20 20 20 70 54 72 69 67 2d 3e 74 61 62        pTrig->tab
286ef 6c 65 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69  le, z);.    sqli
286f0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
286f1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
286f2 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
286f3 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
286f4 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
286f5 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c   OP_ParseSchema,
286f6 20 69 44 62 2c 20 30 2c 20 30 2c 20 73 71 6c 69   iDb, 0, 0, sqli
286f7 74 65 33 4d 50 72 69 6e 74 66 28 0a 20 20 20 20  te3MPrintf(.    
286f8 20 20 20 20 64 62 2c 20 22 74 79 70 65 3d 27 74      db, "type='t
286f9 72 69 67 67 65 72 27 20 41 4e 44 20 6e 61 6d 65  rigger' AND name
286fa 3d 27 25 71 27 22 2c 20 70 54 72 69 67 2d 3e 6e  ='%q'", pTrig->n
286fb 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43  ame), P4_DYNAMIC
286fc 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  .    );.  }..  i
286fd 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
286fe 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20   ){.    int n;. 
286ff 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
28700 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 44 65      Trigger *pDe
28701 6c 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20 73 71  l;.    pDel = sq
28702 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
28703 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53  &db->aDb[iDb].pS
28704 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 2c  chema->trigHash,
28705 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
28706 20 20 20 20 20 20 20 70 54 72 69 67 2d 3e 6e 61         pTrig->na
28707 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  me, sqlite3Strle
28708 6e 33 30 28 70 54 72 69 67 2d 3e 6e 61 6d 65 29  n30(pTrig->name)
28709 2c 20 70 54 72 69 67 29 3b 0a 20 20 20 20 69 66  , pTrig);.    if
2870a 28 20 70 44 65 6c 20 29 7b 0a 20 20 20 20 20 20  ( pDel ){.      
2870b 61 73 73 65 72 74 28 20 70 44 65 6c 3d 3d 70 54  assert( pDel==pT
2870c 72 69 67 20 29 3b 0a 20 20 20 20 20 20 64 62 2d  rig );.      db-
2870d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2870e 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72  1;.      goto tr
2870f 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61  iggerfinish_clea
28710 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  nup;.    }.    n
28711 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
28712 33 30 28 70 54 72 69 67 2d 3e 74 61 62 6c 65 29  30(pTrig->table)
28713 20 2b 20 31 3b 0a 20 20 20 20 70 54 61 62 20 3d   + 1;.    pTab =
28714 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
28715 28 26 70 54 72 69 67 2d 3e 70 54 61 62 53 63 68  (&pTrig->pTabSch
28716 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54  ema->tblHash, pT
28717 72 69 67 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a  rig->table, n);.
28718 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
28719 21 3d 30 20 29 3b 0a 20 20 20 20 70 54 72 69 67  !=0 );.    pTrig
2871a 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e  ->pNext = pTab->
2871b 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20 70 54  pTrigger;.    pT
2871c 61 62 2d 3e 70 54 72 69 67 67 65 72 20 3d 20 70  ab->pTrigger = p
2871d 54 72 69 67 3b 0a 20 20 20 20 70 54 72 69 67 20  Trig;.    pTrig 
2871e 3d 20 30 3b 0a 20 20 7d 0a 0a 74 72 69 67 67 65  = 0;.  }..trigge
2871f 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a  rfinish_cleanup:
28720 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  .  sqlite3Delete
28721 54 72 69 67 67 65 72 28 64 62 2c 20 70 54 72 69  Trigger(db, pTri
28722 67 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70  g);.  assert( !p
28723 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
28724 65 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  er );.  sqlite3D
28725 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70  eleteTriggerStep
28726 28 64 62 2c 20 70 53 74 65 70 4c 69 73 74 29 3b  (db, pStepList);
28727 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61  .}../*.** Make a
28728 20 63 6f 70 79 20 6f 66 20 61 6c 6c 20 63 6f 6d   copy of all com
28729 70 6f 6e 65 6e 74 73 20 6f 66 20 74 68 65 20 67  ponents of the g
2872a 69 76 65 6e 20 74 72 69 67 67 65 72 20 73 74 65  iven trigger ste
2872b 70 2e 20 20 54 68 69 73 20 68 61 73 0a 2a 2a 20  p.  This has.** 
2872c 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 63 6f  the effect of co
2872d 70 79 69 6e 67 20 61 6c 6c 20 45 78 70 72 2e 74  pying all Expr.t
2872e 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65 73 20 69 6e  oken.z values in
2872f 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
28730 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  ed.** from sqlit
28731 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 41 73  e3_malloc().  As
28732 20 69 6e 69 74 69 61 6c 6c 79 20 63 72 65 61 74   initially creat
28733 65 64 2c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b  ed, the Expr.tok
28734 65 6e 2e 7a 20 76 61 6c 75 65 73 0a 2a 2a 20 61  en.z values.** a
28735 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  ll point to the 
28736 69 6e 70 75 74 20 73 74 72 69 6e 67 20 74 68 61  input string tha
28737 74 20 77 61 73 20 66 65 64 20 74 6f 20 74 68 65  t was fed to the
28738 20 70 61 72 73 65 72 2e 20 20 42 75 74 20 74 68   parser.  But th
28739 61 74 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20  at.** string is 
2873a 65 70 68 65 6d 65 72 61 6c 20 2d 20 69 74 20 77  ephemeral - it w
2873b 69 6c 6c 20 67 6f 20 61 77 61 79 20 61 73 20 73  ill go away as s
2873c 6f 6f 6e 20 61 73 20 74 68 65 20 73 71 6c 69 74  oon as the sqlit
2873d 65 33 5f 65 78 65 63 28 29 0a 2a 2a 20 63 61 6c  e3_exec().** cal
2873e 6c 20 74 68 61 74 20 73 74 61 72 74 65 64 20 74  l that started t
2873f 68 65 20 70 61 72 73 65 72 20 65 78 69 74 73 2e  he parser exits.
28740 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d    This routine m
28741 61 6b 65 73 20 61 20 70 65 72 73 69 73 74 65 6e  akes a persisten
28742 74 0a 2a 2a 20 63 6f 70 79 20 6f 66 20 61 6c 6c  t.** copy of all
28743 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e   the Expr.token.
28744 7a 20 73 74 72 69 6e 67 73 20 73 6f 20 74 68 61  z strings so tha
28745 74 20 74 68 65 20 54 72 69 67 67 65 72 53 74 65  t the TriggerSte
28746 70 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77  p structure.** w
28747 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 65 76 65  ill be valid eve
28748 6e 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69  n after the sqli
28749 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20  te3_exec() call 
2874a 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 73 74 61 74  returns..*/.stat
2874b 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 50 65  ic void sqlitePe
2874c 72 73 69 73 74 54 72 69 67 67 65 72 53 74 65 70  rsistTriggerStep
2874d 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72  (sqlite3 *db, Tr
2874e 69 67 67 65 72 53 74 65 70 20 2a 70 29 7b 0a 20  iggerStep *p){. 
2874f 20 69 66 28 20 70 2d 3e 74 61 72 67 65 74 2e 7a   if( p->target.z
28750 20 29 7b 0a 20 20 20 20 70 2d 3e 74 61 72 67 65   ){.    p->targe
28751 74 2e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74  t.z = (u8*)sqlit
28752 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
28753 28 63 68 61 72 2a 29 70 2d 3e 74 61 72 67 65 74  (char*)p->target
28754 2e 7a 2c 20 70 2d 3e 74 61 72 67 65 74 2e 6e 29  .z, p->target.n)
28755 3b 0a 20 20 20 20 70 2d 3e 74 61 72 67 65 74 2e  ;.    p->target.
28756 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69  dyn = 1;.  }.  i
28757 66 28 20 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b  f( p->pSelect ){
28758 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 4e 65  .    Select *pNe
28759 77 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  w = sqlite3Selec
2875a 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 53 65 6c  tDup(db, p->pSel
2875b 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ect);.    sqlite
2875c 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2875d 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  , p->pSelect);. 
2875e 20 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20     p->pSelect = 
2875f 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 66 28 20  pNew;.  }.  if( 
28760 70 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20  p->pWhere ){.   
28761 20 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71   Expr *pNew = sq
28762 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
28763 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20   p->pWhere);.   
28764 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
28765 74 65 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65  te(db, p->pWhere
28766 29 3b 0a 20 20 20 20 70 2d 3e 70 57 68 65 72 65  );.    p->pWhere
28767 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69   = pNew;.  }.  i
28768 66 28 20 70 2d 3e 70 45 78 70 72 4c 69 73 74 20  f( p->pExprList 
28769 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  ){.    ExprList 
2876a 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45  *pNew = sqlite3E
2876b 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
2876c 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a 20 20  ->pExprList);.  
2876d 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2876e 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  tDelete(db, p->p
2876f 45 78 70 72 4c 69 73 74 29 3b 0a 20 20 20 20 70  ExprList);.    p
28770 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 70 4e  ->pExprList = pN
28771 65 77 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ew;.  }.  if( p-
28772 3e 70 49 64 4c 69 73 74 20 29 7b 0a 20 20 20 20  >pIdList ){.    
28773 49 64 4c 69 73 74 20 2a 70 4e 65 77 20 3d 20 73  IdList *pNew = s
28774 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28  qlite3IdListDup(
28775 64 62 2c 20 70 2d 3e 70 49 64 4c 69 73 74 29 3b  db, p->pIdList);
28776 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69  .    sqlite3IdLi
28777 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  stDelete(db, p->
28778 70 49 64 4c 69 73 74 29 3b 0a 20 20 20 20 70 2d  pIdList);.    p-
28779 3e 70 49 64 4c 69 73 74 20 3d 20 70 4e 65 77 3b  >pIdList = pNew;
2877a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75  .  }.}../*.** Tu
2877b 72 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  rn a SELECT stat
2877c 65 6d 65 6e 74 20 28 74 68 61 74 20 74 68 65 20  ement (that the 
2877d 70 53 65 6c 65 63 74 20 70 61 72 61 6d 65 74 65  pSelect paramete
2877e 72 20 70 6f 69 6e 74 73 20 74 6f 29 20 69 6e 74  r points to) int
2877f 6f 0a 2a 2a 20 61 20 74 72 69 67 67 65 72 20 73  o.** a trigger s
28780 74 65 70 2e 20 20 52 65 74 75 72 6e 20 61 20 70  tep.  Return a p
28781 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 72 69 67  ointer to a Trig
28782 67 65 72 53 74 65 70 20 73 74 72 75 63 74 75 72  gerStep structur
28783 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72  e..**.** The par
28784 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
28785 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20 66  outine when it f
28786 69 6e 64 73 20 61 20 53 45 4c 45 43 54 20 73 74  inds a SELECT st
28787 61 74 65 6d 65 6e 74 20 69 6e 0a 2a 2a 20 62 6f  atement in.** bo
28788 64 79 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e  dy of a TRIGGER.
28789 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49    .*/.SQLITE_PRI
2878a 56 41 54 45 20 54 72 69 67 67 65 72 53 74 65 70  VATE TriggerStep
2878b 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72   *sqlite3Trigger
2878c 53 65 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74  SelectStep(sqlit
2878d 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a  e3 *db, Select *
2878e 70 53 65 6c 65 63 74 29 7b 0a 20 20 54 72 69 67  pSelect){.  Trig
2878f 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65  gerStep *pTrigge
28790 72 53 74 65 70 20 3d 20 73 71 6c 69 74 65 33 44  rStep = sqlite3D
28791 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
28792 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 53 74  sizeof(TriggerSt
28793 65 70 29 29 3b 0a 20 20 69 66 28 20 70 54 72 69  ep));.  if( pTri
28794 67 67 65 72 53 74 65 70 3d 3d 30 20 29 20 7b 0a  ggerStep==0 ) {.
28795 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
28796 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
28797 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ect);.    return
28798 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54 72 69 67   0;.  }..  pTrig
28799 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b  gerStep->op = TK
2879a 5f 53 45 4c 45 43 54 3b 0a 20 20 70 54 72 69 67  _SELECT;.  pTrig
2879b 67 65 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74  gerStep->pSelect
2879c 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 70 54   = pSelect;.  pT
2879d 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f  riggerStep->orco
2879e 6e 66 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b  nf = OE_Default;
2879f 0a 20 20 73 71 6c 69 74 65 50 65 72 73 69 73 74  .  sqlitePersist
287a0 54 72 69 67 67 65 72 53 74 65 70 28 64 62 2c 20  TriggerStep(db, 
287a1 70 54 72 69 67 67 65 72 53 74 65 70 29 3b 0a 0a  pTriggerStep);..
287a2 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67 65    return pTrigge
287a3 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rStep;.}../*.** 
287a4 42 75 69 6c 64 20 61 20 74 72 69 67 67 65 72 20  Build a trigger 
287a5 73 74 65 70 20 6f 75 74 20 6f 66 20 61 6e 20 49  step out of an I
287a6 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e  NSERT statement.
287a7 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
287a8 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 65 77  er.** to the new
287a9 20 74 72 69 67 67 65 72 20 73 74 65 70 2e 0a 2a   trigger step..*
287aa 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20  *.** The parser 
287ab 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
287ac 6e 65 20 77 68 65 6e 20 69 74 20 73 65 65 73 20  ne when it sees 
287ad 61 6e 20 49 4e 53 45 52 54 20 69 6e 73 69 64 65  an INSERT inside
287ae 20 74 68 65 0a 2a 2a 20 62 6f 64 79 20 6f 66 20   the.** body of 
287af 61 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51  a trigger..*/.SQ
287b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69  LITE_PRIVATE Tri
287b1 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
287b2 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74  3TriggerInsertSt
287b3 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ep(.  sqlite3 *d
287b4 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b,        /* The
287b5 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
287b6 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  tion */.  Token 
287b7 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 2f 2a  *pTableName,  /*
287b8 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   Name of the tab
287b9 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65  le into which we
287ba 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 49 64 4c   insert */.  IdL
287bb 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20  ist *pColumn,   
287bc 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75   /* List of colu
287bd 6d 6e 73 20 69 6e 20 70 54 61 62 6c 65 4e 61 6d  mns in pTableNam
287be 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f  e to insert into
287bf 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
287c0 70 45 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65  pEList,   /* The
287c1 20 56 41 4c 55 45 20 63 6c 61 75 73 65 3a 20 61   VALUE clause: a
287c2 20 6c 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20   list of values 
287c3 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a  to be inserted *
287c4 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
287c5 65 63 74 2c 20 20 20 20 2f 2a 20 41 20 53 45 4c  ect,    /* A SEL
287c6 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68  ECT statement th
287c7 61 74 20 73 75 70 70 6c 69 65 73 20 76 61 6c 75  at supplies valu
287c8 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f  es */.  int orco
287c9 6e 66 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  nf          /* T
287ca 68 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f  he conflict algo
287cb 72 69 74 68 6d 20 28 4f 45 5f 41 62 6f 72 74 2c  rithm (OE_Abort,
287cc 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 65 74 63   OE_Replace, etc
287cd 2e 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67  .) */.){.  Trigg
287ce 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72  erStep *pTrigger
287cf 53 74 65 70 3b 0a 0a 20 20 61 73 73 65 72 74 28  Step;..  assert(
287d0 70 45 4c 69 73 74 20 3d 3d 20 30 20 7c 7c 20 70  pEList == 0 || p
287d1 53 65 6c 65 63 74 20 3d 3d 20 30 29 3b 0a 20 20  Select == 0);.  
287d2 61 73 73 65 72 74 28 70 45 4c 69 73 74 20 21 3d  assert(pEList !=
287d3 20 30 20 7c 7c 20 70 53 65 6c 65 63 74 20 21 3d   0 || pSelect !=
287d4 20 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63   0 || db->malloc
287d5 46 61 69 6c 65 64 29 3b 0a 0a 20 20 70 54 72 69  Failed);..  pTri
287d6 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74  ggerStep = sqlit
287d7 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
287d8 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65  b, sizeof(Trigge
287d9 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28 20 70  rStep));.  if( p
287da 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20  TriggerStep ){. 
287db 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
287dc 3e 6f 70 20 3d 20 54 4b 5f 49 4e 53 45 52 54 3b  >op = TK_INSERT;
287dd 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65  .    pTriggerSte
287de 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 65  p->pSelect = pSe
287df 6c 65 63 74 3b 0a 20 20 20 20 70 54 72 69 67 67  lect;.    pTrigg
287e0 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74 20 20  erStep->target  
287e1 3d 20 2a 70 54 61 62 6c 65 4e 61 6d 65 3b 0a 20  = *pTableName;. 
287e2 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
287e3 3e 70 49 64 4c 69 73 74 20 3d 20 70 43 6f 6c 75  >pIdList = pColu
287e4 6d 6e 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  mn;.    pTrigger
287e5 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 20  Step->pExprList 
287e6 3d 20 70 45 4c 69 73 74 3b 0a 20 20 20 20 70 54  = pEList;.    pT
287e7 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f  riggerStep->orco
287e8 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 20  nf = orconf;.   
287e9 20 73 71 6c 69 74 65 50 65 72 73 69 73 74 54 72   sqlitePersistTr
287ea 69 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54  iggerStep(db, pT
287eb 72 69 67 67 65 72 53 74 65 70 29 3b 0a 20 20 7d  riggerStep);.  }
287ec 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
287ed 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
287ee 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , pColumn);.    
287ef 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
287f0 65 6c 65 74 65 28 64 62 2c 20 70 45 4c 69 73 74  elete(db, pEList
287f1 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  );.    sqlite3Se
287f2 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
287f3 53 65 6c 65 63 74 29 3b 0a 20 20 7d 0a 0a 20 20  Select);.  }..  
287f4 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
287f5 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  tep;.}../*.** Co
287f6 6e 73 74 72 75 63 74 20 61 20 74 72 69 67 67 65  nstruct a trigge
287f7 72 20 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c  r step that impl
287f8 65 6d 65 6e 74 73 20 61 6e 20 55 50 44 41 54 45  ements an UPDATE
287f9 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72   statement and r
287fa 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
287fb 65 72 20 74 6f 20 74 68 61 74 20 74 72 69 67 67  er to that trigg
287fc 65 72 20 73 74 65 70 2e 20 20 54 68 65 20 70 61  er step.  The pa
287fd 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
287fe 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a  routine when it.
287ff 2a 2a 20 73 65 65 73 20 61 6e 20 55 50 44 41 54  ** sees an UPDAT
28800 45 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 69  E statement insi
28801 64 65 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61  de the body of a
28802 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 2e   CREATE TRIGGER.
28803 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
28804 54 45 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  TE TriggerStep *
28805 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 55 70  sqlite3TriggerUp
28806 64 61 74 65 53 74 65 70 28 0a 20 20 73 71 6c 69  dateStep(.  sqli
28807 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
28808 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
28809 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
2880a 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61   Token *pTableNa
2880b 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  me,   /* Name of
2880c 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
2880d 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 45 78   updated */.  Ex
2880e 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20  prList *pEList, 
2880f 20 20 20 2f 2a 20 54 68 65 20 53 45 54 20 63 6c     /* The SET cl
28810 61 75 73 65 3a 20 6c 69 73 74 20 6f 66 20 63 6f  ause: list of co
28811 6c 75 6d 6e 20 61 6e 64 20 6e 65 77 20 76 61 6c  lumn and new val
28812 75 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ues */.  Expr *p
28813 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a  Where,        /*
28814 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
28815 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e  e */.  int orcon
28816 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  f           /* T
28817 68 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f  he conflict algo
28818 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74  rithm. (OE_Abort
28819 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 65 74 63  , OE_Ignore, etc
2881a 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65  ) */.){.  Trigge
2881b 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53  rStep *pTriggerS
2881c 74 65 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  tep = sqlite3DbM
2881d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2881e 7a 65 6f 66 28 54 72 69 67 67 65 72 53 74 65 70  zeof(TriggerStep
2881f 29 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67  ));.  if( pTrigg
28820 65 72 53 74 65 70 3d 3d 30 20 29 7b 0a 20 20 20  erStep==0 ){.   
28821 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
28822 74 44 65 6c 65 74 65 28 64 62 2c 20 70 45 4c 69  tDelete(db, pELi
28823 73 74 29 3b 0a 20 20 20 20 20 73 71 6c 69 74 65  st);.     sqlite
28824 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
28825 70 57 68 65 72 65 29 3b 0a 20 20 20 20 20 72 65  pWhere);.     re
28826 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70  turn 0;.  }..  p
28827 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20  TriggerStep->op 
28828 3d 20 54 4b 5f 55 50 44 41 54 45 3b 0a 20 20 70  = TK_UPDATE;.  p
28829 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72  TriggerStep->tar
2882a 67 65 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61  get  = *pTableNa
2882b 6d 65 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74  me;.  pTriggerSt
2882c 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d 20  ep->pExprList = 
2882d 70 45 4c 69 73 74 3b 0a 20 20 70 54 72 69 67 67  pEList;.  pTrigg
2882e 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d  erStep->pWhere =
2882f 20 70 57 68 65 72 65 3b 0a 20 20 70 54 72 69 67   pWhere;.  pTrig
28830 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20  gerStep->orconf 
28831 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 73 71 6c 69  = orconf;.  sqli
28832 74 65 50 65 72 73 69 73 74 54 72 69 67 67 65 72  tePersistTrigger
28833 53 74 65 70 28 64 62 2c 20 70 54 72 69 67 67 65  Step(db, pTrigge
28834 72 53 74 65 70 29 3b 0a 0a 20 20 72 65 74 75 72  rStep);..  retur
28835 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a  n pTriggerStep;.
28836 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75  }../*.** Constru
28837 63 74 20 61 20 74 72 69 67 67 65 72 20 73 74 65  ct a trigger ste
28838 70 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  p that implement
28839 73 20 61 20 44 45 4c 45 54 45 20 73 74 61 74 65  s a DELETE state
2883a 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a  ment and return.
2883b 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
2883c 74 68 61 74 20 74 72 69 67 67 65 72 20 73 74 65  that trigger ste
2883d 70 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63  p.  The parser c
2883e 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2883f 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65  e when it.** see
28840 73 20 61 20 44 45 4c 45 54 45 20 73 74 61 74 65  s a DELETE state
28841 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65 20  ment inside the 
28842 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54 45  body of a CREATE
28843 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c   TRIGGER..*/.SQL
28844 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67  ITE_PRIVATE Trig
28845 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
28846 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65  TriggerDeleteSte
28847 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  p(.  sqlite3 *db
28848 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
28849 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2884a 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ion */.  Token *
2884b 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20  pTableName,     
2884c 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72   /* The table fr
2884d 6f 6d 20 77 68 69 63 68 20 72 6f 77 73 20 61 72  om which rows ar
2884e 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 45  e deleted */.  E
2884f 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20  xpr *pWhere     
28850 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
28851 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 29 7b  ERE clause */.){
28852 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
28853 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73  pTriggerStep = s
28854 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
28855 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72  ro(db, sizeof(Tr
28856 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69  iggerStep));.  i
28857 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70 3d  f( pTriggerStep=
28858 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
28859 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2885a 70 57 68 65 72 65 29 3b 0a 20 20 20 20 72 65 74  pWhere);.    ret
2885b 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54  urn 0;.  }..  pT
2885c 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d  riggerStep->op =
2885d 20 54 4b 5f 44 45 4c 45 54 45 3b 0a 20 20 70 54   TK_DELETE;.  pT
2885e 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67  riggerStep->targ
2885f 65 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61 6d  et  = *pTableNam
28860 65 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  e;.  pTriggerSte
28861 70 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65  p->pWhere = pWhe
28862 72 65 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74  re;.  pTriggerSt
28863 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f  ep->orconf = OE_
28864 44 65 66 61 75 6c 74 3b 0a 20 20 73 71 6c 69 74  Default;.  sqlit
28865 65 50 65 72 73 69 73 74 54 72 69 67 67 65 72 53  ePersistTriggerS
28866 74 65 70 28 64 62 2c 20 70 54 72 69 67 67 65 72  tep(db, pTrigger
28867 53 74 65 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e  Step);..  return
28868 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d   pTriggerStep;.}
28869 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 63 75 72 73 69  ../* .** Recursi
2886a 76 65 6c 79 20 64 65 6c 65 74 65 20 61 20 54 72  vely delete a Tr
2886b 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65 0a  igger structure.
2886c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2886d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
2886e 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69  leteTrigger(sqli
2886f 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65 72  te3 *db, Trigger
28870 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 69   *pTrigger){.  i
28871 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20 29  f( pTrigger==0 )
28872 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
28873 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53  e3DeleteTriggerS
28874 74 65 70 28 64 62 2c 20 70 54 72 69 67 67 65 72  tep(db, pTrigger
28875 2d 3e 73 74 65 70 5f 6c 69 73 74 29 3b 0a 20 20  ->step_list);.  
28876 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
28877 2c 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65  , pTrigger->name
28878 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
28879 65 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d  ee(db, pTrigger-
2887a 3e 74 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74  >table);.  sqlit
2887b 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2887c 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e   pTrigger->pWhen
2887d 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69  );.  sqlite3IdLi
2887e 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 72  stDelete(db, pTr
2887f 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e 73 29  igger->pColumns)
28880 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ;.  if( pTrigger
28881 2d 3e 6e 61 6d 65 54 6f 6b 65 6e 2e 64 79 6e 20  ->nameToken.dyn 
28882 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  ) sqlite3DbFree(
28883 64 62 2c 20 28 63 68 61 72 2a 29 70 54 72 69 67  db, (char*)pTrig
28884 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e 2e 7a  ger->nameToken.z
28885 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
28886 65 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 29  ee(db, pTrigger)
28887 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
28888 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
28889 65 64 20 74 6f 20 64 72 6f 70 20 61 20 74 72 69  ed to drop a tri
2888a 67 67 65 72 20 66 72 6f 6d 20 74 68 65 20 64 61  gger from the da
2888b 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 0a  tabase schema. .
2888c 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 79 20 62  **.** This may b
2888d 65 20 63 61 6c 6c 65 64 20 64 69 72 65 63 74 6c  e called directl
2888e 79 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73 65  y from the parse
2888f 72 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20  r and therefore 
28890 69 64 65 6e 74 69 66 69 65 73 0a 2a 2a 20 74 68  identifies.** th
28891 65 20 74 72 69 67 67 65 72 20 62 79 20 6e 61 6d  e trigger by nam
28892 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 44  e.  The sqlite3D
28893 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 29 20  ropTriggerPtr() 
28894 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65  routine does the
28895 0a 2a 2a 20 73 61 6d 65 20 6a 6f 62 20 61 73 20  .** same job as 
28896 74 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78 63  this routine exc
28897 65 70 74 20 69 74 20 74 61 6b 65 73 20 61 20 70  ept it takes a p
28898 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74 72  ointer to the tr
28899 69 67 67 65 72 0a 2a 2a 20 69 6e 73 74 65 61 64  igger.** instead
2889a 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20   of the trigger 
2889b 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53 51 4c 49 54 45  name..**/.SQLITE
2889c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2889d 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72  lite3DropTrigger
2889e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2889f 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20  SrcList *pName, 
288a0 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 72  int noErr){.  Tr
288a1 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 20  igger *pTrigger 
288a2 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  = 0;.  int i;.  
288a3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
288a4 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
288a5 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d  Name;.  int nNam
288a6 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e;.  sqlite3 *db
288a7 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
288a8 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
288a9 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 64 72  Failed ) goto dr
288aa 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  op_trigger_clean
288ab 75 70 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45  up;.  if( SQLITE
288ac 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
288ad 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
288ae 7b 0a 20 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f  {.    goto drop_
288af 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
288b0 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
288b1 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29  pName->nSrc==1 )
288b2 3b 0a 20 20 7a 44 62 20 3d 20 70 4e 61 6d 65 2d  ;.  zDb = pName-
288b3 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 3b  >a[0].zDatabase;
288b4 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 4e 61 6d 65  .  zName = pName
288b5 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  ->a[0].zName;.  
288b6 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
288b7 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
288b8 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d    for(i=OMIT_TEM
288b9 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  PDB; i<db->nDb; 
288ba 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20  i++){.    int j 
288bb 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20  = (i<2) ? i^1 : 
288bc 69 3b 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45  i;  /* Search TE
288bd 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a  MP before MAIN *
288be 2f 0a 20 20 20 20 69 66 28 20 7a 44 62 20 26 26  /.    if( zDb &&
288bf 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
288c0 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65  db->aDb[j].zName
288c1 2c 20 7a 44 62 29 20 29 20 63 6f 6e 74 69 6e 75  , zDb) ) continu
288c2 65 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 20  e;.    pTrigger 
288c3 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  = sqlite3HashFin
288c4 64 28 26 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70  d(&(db->aDb[j].p
288c5 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68  Schema->trigHash
288c6 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  ), zName, nName)
288c7 3b 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67  ;.    if( pTrigg
288c8 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  er ) break;.  }.
288c9 20 20 69 66 28 20 21 70 54 72 69 67 67 65 72 20    if( !pTrigger 
288ca 29 7b 0a 20 20 20 20 69 66 28 20 21 6e 6f 45 72  ){.    if( !noEr
288cb 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
288cc 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
288cd 65 2c 20 22 6e 6f 20 73 75 63 68 20 74 72 69 67  e, "no such trig
288ce 67 65 72 3a 20 25 53 22 2c 20 70 4e 61 6d 65 2c  ger: %S", pName,
288cf 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67   0);.    }.    g
288d0 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65 72  oto drop_trigger
288d1 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
288d2 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67  sqlite3DropTrigg
288d3 65 72 50 74 72 28 70 50 61 72 73 65 2c 20 70 54  erPtr(pParse, pT
288d4 72 69 67 67 65 72 29 3b 0a 0a 64 72 6f 70 5f 74  rigger);..drop_t
288d5 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a 0a  rigger_cleanup:.
288d6 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
288d7 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d 65  Delete(db, pName
288d8 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
288d9 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
288da 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
288db 75 72 65 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ure for the tabl
288dc 65 20 74 68 61 74 20 61 20 74 72 69 67 67 65 72  e that a trigger
288dd 0a 2a 2a 20 69 73 20 73 65 74 20 6f 6e 2e 0a 2a  .** is set on..*
288de 2f 0a 73 74 61 74 69 63 20 54 61 62 6c 65 20 2a  /.static Table *
288df 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 54  tableOfTrigger(T
288e0 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
288e1 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c  ){.  int n = sql
288e2 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 72  ite3Strlen30(pTr
288e3 69 67 67 65 72 2d 3e 74 61 62 6c 65 29 20 2b 20  igger->table) + 
288e4 31 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  1;.  return sqli
288e5 74 65 33 48 61 73 68 46 69 6e 64 28 26 70 54 72  te3HashFind(&pTr
288e6 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d  igger->pTabSchem
288e7 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54 72 69  a->tblHash, pTri
288e8 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b  gger->table, n);
288e9 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20  .}.../*.** Drop 
288ea 61 20 74 72 69 67 67 65 72 20 67 69 76 65 6e 20  a trigger given 
288eb 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61  a pointer to tha
288ec 74 20 74 72 69 67 67 65 72 2e 20 0a 2a 2f 0a 53  t trigger. .*/.S
288ed 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
288ee 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  id sqlite3DropTr
288ef 69 67 67 65 72 50 74 72 28 50 61 72 73 65 20 2a  iggerPtr(Parse *
288f0 70 50 61 72 73 65 2c 20 54 72 69 67 67 65 72 20  pParse, Trigger 
288f1 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 54 61  *pTrigger){.  Ta
288f2 62 6c 65 20 20 20 2a 70 54 61 62 6c 65 3b 0a 20  ble   *pTable;. 
288f3 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69   Vdbe *v;.  sqli
288f4 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
288f5 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b  ->db;.  int iDb;
288f6 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ..  iDb = sqlite
288f7 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
288f8 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 67  Parse->db, pTrig
288f9 67 65 72 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  ger->pSchema);. 
288fa 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
288fb 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
288fc 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 74 61 62  ;.  pTable = tab
288fd 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54 72 69  leOfTrigger(pTri
288fe 67 67 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28  gger);.  assert(
288ff 20 70 54 61 62 6c 65 20 29 3b 0a 20 20 61 73 73   pTable );.  ass
28900 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70 53 63  ert( pTable->pSc
28901 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d 3e  hema==pTrigger->
28902 70 53 63 68 65 6d 61 20 7c 7c 20 69 44 62 3d 3d  pSchema || iDb==
28903 31 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  1 );.#ifndef SQL
28904 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
28905 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69  ZATION.  {.    i
28906 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45  nt code = SQLITE
28907 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 3b 0a 20  _DROP_TRIGGER;. 
28908 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
28909 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
2890a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e  ].zName;.    con
2890b 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20  st char *zTab = 
2890c 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
2890d 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d  );.    if( iDb==
2890e 31 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  1 ) code = SQLIT
2890f 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47  E_DROP_TEMP_TRIG
28910 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  GER;.    if( sql
28911 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
28912 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 54 72 69  arse, code, pTri
28913 67 67 65 72 2d 3e 6e 61 6d 65 2c 20 70 54 61 62  gger->name, pTab
28914 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20  le->zName, zDb) 
28915 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ||.      sqlite3
28916 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
28917 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
28918 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29   zTab, 0, zDb) )
28919 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  {.      return;.
2891a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
2891b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
2891c 63 6f 64 65 20 74 6f 20 64 65 73 74 72 6f 79 20  code to destroy 
2891d 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65 63  the database rec
2891e 6f 72 64 20 6f 66 20 74 68 65 20 74 72 69 67 67  ord of the trigg
2891f 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  er..  */.  asser
28920 74 28 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a  t( pTable!=0 );.
28921 20 20 69 66 28 20 28 76 20 3d 20 73 71 6c 69 74    if( (v = sqlit
28922 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
28923 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ))!=0 ){.    int
28924 20 62 61 73 65 3b 0a 20 20 20 20 73 74 61 74 69   base;.    stati
28925 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
28926 73 74 20 64 72 6f 70 54 72 69 67 67 65 72 5b 5d  st dropTrigger[]
28927 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f   = {.      { OP_
28928 52 65 77 69 6e 64 2c 20 20 20 20 20 30 2c 20 41  Rewind,     0, A
28929 44 44 52 28 39 29 2c 20 20 30 7d 2c 0a 20 20 20  DDR(9),  0},.   
2892a 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
2892b 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20      0, 1,       
2892c 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0a 20 20 20   0}, /* 1 */.   
2892d 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20     { OP_Column, 
2892e 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20      0, 1,       
2892f 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f   2},.      { OP_
28930 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c 20 41  Ne,         2, A
28931 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20 20  DDR(8),  1},.   
28932 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
28933 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20      0, 1,       
28934 20 30 7d 2c 20 2f 2a 20 34 3a 20 22 74 72 69 67   0}, /* 4: "trig
28935 67 65 72 22 20 2a 2f 0a 20 20 20 20 20 20 7b 20  ger" */.      { 
28936 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 30  OP_Column,     0
28937 2c 20 30 2c 20 20 20 20 20 20 20 20 32 7d 2c 0a  , 0,        2},.
28938 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20 20        { OP_Ne,  
28939 20 20 20 20 20 20 20 32 2c 20 41 44 44 52 28 38         2, ADDR(8
2893a 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b 20  ),  1},.      { 
2893b 4f 50 5f 44 65 6c 65 74 65 2c 20 20 20 20 20 30  OP_Delete,     0
2893c 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a  , 0,        0},.
2893d 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c        { OP_Next,
2893e 20 20 20 20 20 20 20 30 2c 20 41 44 44 52 28 31         0, ADDR(1
2893f 29 2c 20 20 30 7d 2c 20 2f 2a 20 38 20 2a 2f 0a  ),  0}, /* 8 */.
28940 20 20 20 20 7d 3b 0a 0a 20 20 20 20 73 71 6c 69      };..    sqli
28941 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
28942 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
28943 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
28944 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62  te3OpenMasterTab
28945 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  le(pParse, iDb);
28946 0a 20 20 20 20 62 61 73 65 20 3d 20 73 71 6c 69  .    base = sqli
28947 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
28948 28 76 2c 20 20 41 72 72 61 79 53 69 7a 65 28 64  (v,  ArraySize(d
28949 72 6f 70 54 72 69 67 67 65 72 29 2c 20 64 72 6f  ropTrigger), dro
2894a 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 73  pTrigger);.    s
2894b 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2894c 50 34 28 76 2c 20 62 61 73 65 2b 31 2c 20 70 54  P4(v, base+1, pT
2894d 72 69 67 67 65 72 2d 3e 6e 61 6d 65 2c 20 30 29  rigger->name, 0)
2894e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2894f 65 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61 73  eChangeP4(v, bas
28950 65 2b 34 2c 20 22 74 72 69 67 67 65 72 22 2c 20  e+4, "trigger", 
28951 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
28952 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
28953 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  kie(pParse, iDb)
28954 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28955 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
28956 6f 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ose, 0, 0);.    
28957 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28958 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67  4(v, OP_DropTrig
28959 67 65 72 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20  ger, iDb, 0, 0, 
2895a 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 2c 20  pTrigger->name, 
2895b 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  0);.  }.}../*.**
2895c 20 52 65 6d 6f 76 65 20 61 20 74 72 69 67 67 65   Remove a trigge
2895d 72 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  r from the hash 
2895e 74 61 62 6c 65 73 20 6f 66 20 74 68 65 20 73 71  tables of the sq
2895f 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a  lite* pointer..*
28960 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
28961 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c   void sqlite3Unl
28962 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67  inkAndDeleteTrig
28963 67 65 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ger(sqlite3 *db,
28964 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20   int iDb, const 
28965 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
28966 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
28967 72 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d  r;.  int nName =
28968 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
28969 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 54 72 69 67  (zName);.  pTrig
2896a 67 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73  ger = sqlite3Has
2896b 68 49 6e 73 65 72 74 28 26 28 64 62 2d 3e 61 44  hInsert(&(db->aD
2896c 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
2896d 74 72 69 67 48 61 73 68 29 2c 0a 20 20 20 20 20  trigHash),.     
2896e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2896f 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c            zName,
28970 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 69 66   nName, 0);.  if
28971 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20  ( pTrigger ){.  
28972 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 20    Table *pTable 
28973 3d 20 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72  = tableOfTrigger
28974 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20  (pTrigger);.    
28975 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d  assert( pTable!=
28976 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  0 );.    if( pTa
28977 62 6c 65 2d 3e 70 54 72 69 67 67 65 72 20 3d 3d  ble->pTrigger ==
28978 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
28979 20 20 20 70 54 61 62 6c 65 2d 3e 70 54 72 69 67     pTable->pTrig
2897a 67 65 72 20 3d 20 70 54 72 69 67 67 65 72 2d 3e  ger = pTrigger->
2897b 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65  pNext;.    }else
2897c 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 20  {.      Trigger 
2897d 2a 63 63 20 3d 20 70 54 61 62 6c 65 2d 3e 70 54  *cc = pTable->pT
2897e 72 69 67 67 65 72 3b 0a 20 20 20 20 20 20 77 68  rigger;.      wh
2897f 69 6c 65 28 20 63 63 20 29 7b 20 0a 20 20 20 20  ile( cc ){ .    
28980 20 20 20 20 69 66 28 20 63 63 2d 3e 70 4e 65 78      if( cc->pNex
28981 74 20 3d 3d 20 70 54 72 69 67 67 65 72 20 29 7b  t == pTrigger ){
28982 0a 20 20 20 20 20 20 20 20 20 20 63 63 2d 3e 70  .          cc->p
28983 4e 65 78 74 20 3d 20 63 63 2d 3e 70 4e 65 78 74  Next = cc->pNext
28984 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
28985 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
28986 20 20 7d 0a 20 20 20 20 20 20 20 20 63 63 20 3d    }.        cc =
28987 20 63 63 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20   cc->pNext;.    
28988 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
28989 28 63 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  (cc);.    }.    
2898a 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
2898b 67 67 65 72 28 64 62 2c 20 70 54 72 69 67 67 65  gger(db, pTrigge
2898c 72 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67  r);.    db->flag
2898d 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65  s |= SQLITE_Inte
2898e 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 0a 7d  rnChanges;.  }.}
2898f 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69  ../*.** pEList i
28990 73 20 74 68 65 20 53 45 54 20 63 6c 61 75 73 65  s the SET clause
28991 20 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73 74   of an UPDATE st
28992 61 74 65 6d 65 6e 74 2e 20 20 45 61 63 68 20 65  atement.  Each e
28993 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 45 4c 69 73  ntry.** in pELis
28994 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  t is of the form
28995 61 74 20 3c 69 64 3e 3d 3c 65 78 70 72 3e 2e 20  at <id>=<expr>. 
28996 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20 65   If any of the e
28997 6e 74 72 69 65 73 0a 2a 2a 20 69 6e 20 70 45 4c  ntries.** in pEL
28998 69 73 74 20 68 61 76 65 20 61 6e 20 3c 69 64 3e  ist have an <id>
28999 20 77 68 69 63 68 20 6d 61 74 63 68 65 73 20 61   which matches a
2899a 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69 6e 20  n identifier in 
2899b 70 49 64 4c 69 73 74 2c 0a 2a 2a 20 74 68 65 6e  pIdList,.** then
2899c 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49   return TRUE.  I
2899d 66 20 70 49 64 4c 69 73 74 3d 3d 4e 55 4c 4c 2c  f pIdList==NULL,
2899e 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f 6e 73   then it is cons
2899f 69 64 65 72 65 64 20 61 0a 2a 2a 20 77 69 6c 64  idered a.** wild
289a0 63 61 72 64 20 74 68 61 74 20 6d 61 74 63 68 65  card that matche
289a1 73 20 61 6e 79 74 68 69 6e 67 2e 20 20 4c 69 6b  s anything.  Lik
289a2 65 77 69 73 65 20 69 66 20 70 45 4c 69 73 74 3d  ewise if pEList=
289a3 3d 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20 69 74  =NULL then.** it
289a4 20 6d 61 74 63 68 65 73 20 61 6e 79 74 68 69 6e   matches anythin
289a5 67 20 73 6f 20 61 6c 77 61 79 73 20 72 65 74 75  g so always retu
289a6 72 6e 20 74 72 75 65 2e 20 20 52 65 74 75 72 6e  rn true.  Return
289a7 20 66 61 6c 73 65 20 6f 6e 6c 79 0a 2a 2a 20 69   false only.** i
289a8 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
289a9 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tch..*/.static i
289aa 6e 74 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76  nt checkColumnOv
289ab 65 72 4c 61 70 28 49 64 4c 69 73 74 20 2a 70 49  erLap(IdList *pI
289ac 64 4c 69 73 74 2c 20 45 78 70 72 4c 69 73 74 20  dList, ExprList 
289ad 2a 70 45 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20  *pEList){.  int 
289ae 65 3b 0a 20 20 69 66 28 20 21 70 49 64 4c 69 73  e;.  if( !pIdLis
289af 74 20 7c 7c 20 21 70 45 4c 69 73 74 20 29 20 72  t || !pEList ) r
289b0 65 74 75 72 6e 20 31 3b 0a 20 20 66 6f 72 28 65  eturn 1;.  for(e
289b1 3d 30 3b 20 65 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; e<pEList->nE
289b2 78 70 72 3b 20 65 2b 2b 29 7b 0a 20 20 20 20 69  xpr; e++){.    i
289b3 66 28 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  f( sqlite3IdList
289b4 49 6e 64 65 78 28 70 49 64 4c 69 73 74 2c 20 70  Index(pIdList, p
289b5 45 4c 69 73 74 2d 3e 61 5b 65 5d 2e 7a 4e 61 6d  EList->a[e].zNam
289b6 65 29 3e 3d 30 20 29 20 72 65 74 75 72 6e 20 31  e)>=0 ) return 1
289b7 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
289b8 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ; .}../*.** Retu
289b9 72 6e 20 61 20 62 69 74 20 76 65 63 74 6f 72 20  rn a bit vector 
289ba 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 61 74  to indicate what
289bb 20 6b 69 6e 64 20 6f 66 20 74 72 69 67 67 65 72   kind of trigger
289bc 73 20 65 78 69 73 74 20 66 6f 72 20 6f 70 65 72  s exist for oper
289bd 61 74 69 6f 6e 0a 2a 2a 20 22 6f 70 22 20 6f 6e  ation.** "op" on
289be 20 74 61 62 6c 65 20 70 54 61 62 2e 20 20 49 66   table pTab.  If
289bf 20 70 43 68 61 6e 67 65 73 20 69 73 20 6e 6f 74   pChanges is not
289c0 20 4e 55 4c 4c 20 74 68 65 6e 20 69 74 20 69 73   NULL then it is
289c1 20 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d   a list of colum
289c2 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 62  ns.** that are b
289c3 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 20 54  eing updated.  T
289c4 72 69 67 67 65 72 73 20 6f 6e 6c 79 20 6d 61 74  riggers only mat
289c5 63 68 20 69 66 20 74 68 65 20 4f 4e 20 63 6c 61  ch if the ON cla
289c6 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72  use of the.** tr
289c7 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e  igger definition
289c8 20 6f 76 65 72 6c 61 70 73 20 74 68 65 20 73 65   overlaps the se
289c9 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 62 65 69  t of columns bei
289ca 6e 67 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a  ng updated..**.*
289cb 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 62  * The returned b
289cc 69 74 20 76 65 63 74 6f 72 20 69 73 20 73 6f 6d  it vector is som
289cd 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  e combination of
289ce 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20   TRIGGER_BEFORE 
289cf 61 6e 64 0a 2a 2a 20 54 52 49 47 47 45 52 5f 41  and.** TRIGGER_A
289d0 46 54 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  FTER..*/.SQLITE_
289d1 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
289d2 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74  te3TriggersExist
289d3 28 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  (.  Table *pTab,
289d4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
289d5 68 65 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6e  he table the con
289d6 74 61 69 6e 73 20 74 68 65 20 74 72 69 67 67 65  tains the trigge
289d7 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20  rs */.  int op, 
289d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
289d9 2f 2a 20 6f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c  /* one of TK_DEL
289da 45 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20  ETE, TK_INSERT, 
289db 54 4b 5f 55 50 44 41 54 45 20 2a 2f 0a 20 20 45  TK_UPDATE */.  E
289dc 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65  xprList *pChange
289dd 73 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e  s      /* Column
289de 73 20 74 68 61 74 20 63 68 61 6e 67 65 20 69 6e  s that change in
289df 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65   an UPDATE state
289e0 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  ment */.){.  Tri
289e1 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a  gger *pTrigger;.
289e2 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20 30 3b 0a    int mask = 0;.
289e3 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 49 73  .  pTrigger = Is
289e4 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20  Virtual(pTab) ? 
289e5 30 20 3a 20 70 54 61 62 2d 3e 70 54 72 69 67 67  0 : pTab->pTrigg
289e6 65 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 54 72  er;.  while( pTr
289e7 69 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66 28  igger ){.    if(
289e8 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 6f   pTrigger->op==o
289e9 70 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e  p && checkColumn
289ea 4f 76 65 72 4c 61 70 28 70 54 72 69 67 67 65 72  OverLap(pTrigger
289eb 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70 43 68 61  ->pColumns, pCha
289ec 6e 67 65 73 29 20 29 7b 0a 20 20 20 20 20 20 6d  nges) ){.      m
289ed 61 73 6b 20 7c 3d 20 70 54 72 69 67 67 65 72 2d  ask |= pTrigger-
289ee 3e 74 72 5f 74 6d 3b 0a 20 20 20 20 7d 0a 20 20  >tr_tm;.    }.  
289ef 20 20 70 54 72 69 67 67 65 72 20 3d 20 70 54 72    pTrigger = pTr
289f0 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20  igger->pNext;.  
289f1 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b  }.  return mask;
289f2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
289f3 74 20 74 68 65 20 70 53 74 65 70 2d 3e 74 61 72  t the pStep->tar
289f4 67 65 74 20 74 6f 6b 65 6e 20 69 6e 74 6f 20 61  get token into a
289f5 20 53 72 63 4c 69 73 74 20 61 6e 64 20 72 65 74   SrcList and ret
289f6 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a  urn a pointer.**
289f7 20 74 6f 20 74 68 61 74 20 53 72 63 4c 69 73 74   to that SrcList
289f8 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
289f9 74 69 6e 65 20 61 64 64 73 20 61 20 73 70 65 63  tine adds a spec
289fa 69 66 69 63 20 64 61 74 61 62 61 73 65 20 6e 61  ific database na
289fb 6d 65 2c 20 69 66 20 6e 65 65 64 65 64 2c 20 74  me, if needed, t
289fc 6f 20 74 68 65 20 74 61 72 67 65 74 20 77 68 65  o the target whe
289fd 6e 0a 2a 2a 20 66 6f 72 6d 69 6e 67 20 74 68 65  n.** forming the
289fe 20 53 72 63 4c 69 73 74 2e 20 20 54 68 69 73 20   SrcList.  This 
289ff 70 72 65 76 65 6e 74 73 20 61 20 74 72 69 67 67  prevents a trigg
28a00 65 72 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61  er in one databa
28a01 73 65 20 66 72 6f 6d 0a 2a 2a 20 72 65 66 65 72  se from.** refer
28a02 72 69 6e 67 20 74 6f 20 61 20 74 61 72 67 65 74  ring to a target
28a03 20 69 6e 20 61 6e 6f 74 68 65 72 20 64 61 74 61   in another data
28a04 62 61 73 65 2e 20 20 41 6e 20 65 78 63 65 70 74  base.  An except
28a05 69 6f 6e 20 69 73 20 77 68 65 6e 20 74 68 65 0a  ion is when the.
28a06 2a 2a 20 74 72 69 67 67 65 72 20 69 73 20 69 6e  ** trigger is in
28a07 20 54 45 4d 50 20 69 6e 20 77 68 69 63 68 20 63   TEMP in which c
28a08 61 73 65 20 69 74 20 63 61 6e 20 72 65 66 65 72  ase it can refer
28a09 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 64 61   to any other da
28a0a 74 61 62 61 73 65 20 69 74 0a 2a 2a 20 77 61 6e  tabase it.** wan
28a0b 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 72  ts..*/.static Sr
28a0c 63 4c 69 73 74 20 2a 74 61 72 67 65 74 53 72 63  cList *targetSrc
28a0d 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70  List(.  Parse *p
28a0e 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
28a0f 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
28a10 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ext */.  Trigger
28a11 53 74 65 70 20 2a 70 53 74 65 70 20 20 20 2f 2a  Step *pStep   /*
28a12 20 54 68 65 20 74 72 69 67 67 65 72 20 63 6f 6e   The trigger con
28a13 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 72 67  taining the targ
28a14 65 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  et token */.){. 
28a15 20 54 6f 6b 65 6e 20 73 44 62 3b 20 20 20 20 20   Token sDb;     
28a16 20 20 20 20 20 20 2f 2a 20 44 75 6d 6d 79 20 64        /* Dummy d
28a17 61 74 61 62 61 73 65 20 6e 61 6d 65 20 74 6f 6b  atabase name tok
28a18 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  en */.  int iDb;
28a19 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28a1a 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  Index of the dat
28a1b 61 62 61 73 65 20 74 6f 20 75 73 65 20 2a 2f 0a  abase to use */.
28a1c 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
28a1d 20 20 20 20 20 20 20 2f 2a 20 53 72 63 4c 69 73         /* SrcLis
28a1e 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  t to be returned
28a1f 20 2a 2f 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c   */..  iDb = sql
28a20 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
28a21 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  x(pParse->db, pS
28a22 74 65 70 2d 3e 70 54 72 69 67 2d 3e 70 53 63 68  tep->pTrig->pSch
28a23 65 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62 3d  ema);.  if( iDb=
28a24 3d 30 20 7c 7c 20 69 44 62 3e 3d 32 20 29 7b 0a  =0 || iDb>=2 ){.
28a25 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c      assert( iDb<
28a26 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 20  pParse->db->nDb 
28a27 29 3b 0a 20 20 20 20 73 44 62 2e 7a 20 3d 20 28  );.    sDb.z = (
28a28 75 38 2a 29 70 50 61 72 73 65 2d 3e 64 62 2d 3e  u8*)pParse->db->
28a29 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
28a2a 20 20 20 20 73 44 62 2e 6e 20 3d 20 73 71 6c 69      sDb.n = sqli
28a2b 74 65 33 53 74 72 6c 65 6e 33 30 28 28 63 68 61  te3Strlen30((cha
28a2c 72 2a 29 73 44 62 2e 7a 29 3b 0a 20 20 20 20 70  r*)sDb.z);.    p
28a2d 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Src = sqlite3Src
28a2e 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
28a2f 65 2d 3e 64 62 2c 20 30 2c 20 26 73 44 62 2c 20  e->db, 0, &sDb, 
28a30 26 70 53 74 65 70 2d 3e 74 61 72 67 65 74 29 3b  &pStep->target);
28a31 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
28a32 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72  pSrc = sqlite3Sr
28a33 63 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  cListAppend(pPar
28a34 73 65 2d 3e 64 62 2c 20 30 2c 20 26 70 53 74 65  se->db, 0, &pSte
28a35 70 2d 3e 74 61 72 67 65 74 2c 20 30 29 3b 0a 20  p->target, 0);. 
28a36 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 53 72 63   }.  return pSrc
28a37 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
28a38 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 66 6f  ate VDBE code fo
28a39 72 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 73  r zero or more s
28a3a 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65  tatements inside
28a3b 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 0a 2a   the body of a.*
28a3c 2a 20 74 72 69 67 67 65 72 2e 20 20 0a 2a 2f 0a  * trigger.  .*/.
28a3d 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 54  static int codeT
28a3e 72 69 67 67 65 72 50 72 6f 67 72 61 6d 28 0a 20  riggerProgram(. 
28a3f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
28a40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
28a41 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
28a42 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
28a43 70 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 20 20  p *pStepList,   
28a44 2f 2a 20 4c 69 73 74 20 6f 66 20 73 74 61 74 65  /* List of state
28a45 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65  ments inside the
28a46 20 74 72 69 67 67 65 72 20 62 6f 64 79 20 2a 2f   trigger body */
28a47 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 69 6e 20  .  int orconfin 
28a48 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28a49 43 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69 74  Conflict algorit
28a4a 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c 20 65  hm. (OE_Abort, e
28a4b 74 63 29 20 2a 2f 20 20 0a 29 7b 0a 20 20 54 72  tc) */  .){.  Tr
28a4c 69 67 67 65 72 53 74 65 70 20 2a 20 70 54 72 69  iggerStep * pTri
28a4d 67 67 65 72 53 74 65 70 20 3d 20 70 53 74 65 70  ggerStep = pStep
28a4e 4c 69 73 74 3b 0a 20 20 69 6e 74 20 6f 72 63 6f  List;.  int orco
28a4f 6e 66 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  nf;.  Vdbe *v = 
28a50 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
28a51 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
28a52 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61 73  Parse->db;..  as
28a53 73 65 72 74 28 20 70 54 72 69 67 67 65 72 53 74  sert( pTriggerSt
28a54 65 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ep!=0 );.  asser
28a55 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 73 71 6c  t( v!=0 );.  sql
28a56 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28a57 2c 20 4f 50 5f 43 6f 6e 74 65 78 74 50 75 73 68  , OP_ContextPush
28a58 2c 20 30 2c 20 30 29 3b 0a 20 20 56 64 62 65 43  , 0, 0);.  VdbeC
28a59 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62 65 67 69  omment((v, "begi
28a5a 6e 20 74 72 69 67 67 65 72 20 25 73 22 2c 20 70  n trigger %s", p
28a5b 53 74 65 70 4c 69 73 74 2d 3e 70 54 72 69 67 2d  StepList->pTrig-
28a5c 3e 6e 61 6d 65 29 29 3b 0a 20 20 77 68 69 6c 65  >name));.  while
28a5d 28 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29  ( pTriggerStep )
28a5e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
28a5f 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68  rClearColumnCach
28a60 65 28 70 50 61 72 73 65 2c 20 2d 31 29 3b 0a 20  e(pParse, -1);. 
28a61 20 20 20 6f 72 63 6f 6e 66 20 3d 20 28 6f 72 63     orconf = (orc
28a62 6f 6e 66 69 6e 20 3d 3d 20 4f 45 5f 44 65 66 61  onfin == OE_Defa
28a63 75 6c 74 29 3f 70 54 72 69 67 67 65 72 53 74 65  ult)?pTriggerSte
28a64 70 2d 3e 6f 72 63 6f 6e 66 3a 6f 72 63 6f 6e 66  p->orconf:orconf
28a65 69 6e 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  in;.    pParse->
28a66 74 72 69 67 53 74 61 63 6b 2d 3e 6f 72 63 6f 6e  trigStack->orcon
28a67 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 20 20  f = orconf;.    
28a68 73 77 69 74 63 68 28 20 70 54 72 69 67 67 65 72  switch( pTrigger
28a69 53 74 65 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  Step->op ){.    
28a6a 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54    case TK_SELECT
28a6b 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 65 6c 65  : {.        Sele
28a6c 63 74 20 2a 73 73 20 3d 20 73 71 6c 69 74 65 33  ct *ss = sqlite3
28a6d 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54  SelectDup(db, pT
28a6e 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
28a6f 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ect);.        if
28a70 28 20 73 73 20 29 7b 0a 20 20 20 20 20 20 20 20  ( ss ){.        
28a71 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73    SelectDest des
28a72 74 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 73 71  t;..          sq
28a73 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
28a74 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 44  nit(&dest, SRT_D
28a75 69 73 63 61 72 64 2c 20 30 29 3b 0a 20 20 20 20  iscard, 0);.    
28a76 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
28a77 65 63 74 28 70 50 61 72 73 65 2c 20 73 73 2c 20  ect(pParse, ss, 
28a78 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20  &dest);.        
28a79 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
28a7a 65 6c 65 74 65 28 64 62 2c 20 73 73 29 3b 0a 20  elete(db, ss);. 
28a7b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
28a7c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
28a7d 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50        case TK_UP
28a7e 44 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20  DATE: {.        
28a7f 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20  SrcList *pSrc;. 
28a80 20 20 20 20 20 20 20 70 53 72 63 20 3d 20 74 61         pSrc = ta
28a81 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61 72  rgetSrcList(pPar
28a82 73 65 2c 20 70 54 72 69 67 67 65 72 53 74 65 70  se, pTriggerStep
28a83 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
28a84 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28a85 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20 30  OP_ResetCount, 0
28a86 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
28a87 6c 69 74 65 33 55 70 64 61 74 65 28 70 50 61 72  lite3Update(pPar
28a88 73 65 2c 20 70 53 72 63 2c 0a 20 20 20 20 20 20  se, pSrc,.      
28a89 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28a8a 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
28a8b 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
28a8c 45 78 70 72 4c 69 73 74 29 2c 20 0a 20 20 20 20  ExprList), .    
28a8d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28a8e 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
28a8f 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68  TriggerStep->pWh
28a90 65 72 65 29 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20  ere), orconf);. 
28a91 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
28a92 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
28a93 65 73 65 74 43 6f 75 6e 74 2c 20 31 2c 20 30 29  esetCount, 1, 0)
28a94 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
28a95 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
28a96 61 73 65 20 54 4b 5f 49 4e 53 45 52 54 3a 20 7b  ase TK_INSERT: {
28a97 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74  .        SrcList
28a98 20 2a 70 53 72 63 3b 0a 20 20 20 20 20 20 20 20   *pSrc;.        
28a99 70 53 72 63 20 3d 20 74 61 72 67 65 74 53 72 63  pSrc = targetSrc
28a9a 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 72  List(pParse, pTr
28a9b 69 67 67 65 72 53 74 65 70 29 3b 0a 20 20 20 20  iggerStep);.    
28a9c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28a9d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 65  ddOp2(v, OP_Rese
28a9e 74 43 6f 75 6e 74 2c 20 30 2c 20 30 29 3b 0a 20  tCount, 0, 0);. 
28a9f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 49 6e         sqlite3In
28aa0 73 65 72 74 28 70 50 61 72 73 65 2c 20 70 53 72  sert(pParse, pSr
28aa1 63 2c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  c,.          sql
28aa2 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
28aa3 64 62 2c 20 70 54 72 69 67 67 65 72 53 74 65 70  db, pTriggerStep
28aa4 2d 3e 70 45 78 70 72 4c 69 73 74 29 2c 20 0a 20  ->pExprList), . 
28aa5 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28aa6 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54  SelectDup(db, pT
28aa7 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
28aa8 65 63 74 29 2c 20 0a 20 20 20 20 20 20 20 20 20  ect), .         
28aa9 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75   sqlite3IdListDu
28aaa 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74  p(db, pTriggerSt
28aab 65 70 2d 3e 70 49 64 4c 69 73 74 29 2c 20 6f 72  ep->pIdList), or
28aac 63 6f 6e 66 29 3b 0a 20 20 20 20 20 20 20 20 73  conf);.        s
28aad 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28aae 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e  (v, OP_ResetCoun
28aaf 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 1, 0);.      
28ab0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
28ab1 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 44  .      case TK_D
28ab2 45 4c 45 54 45 3a 20 7b 0a 20 20 20 20 20 20 20  ELETE: {.       
28ab3 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a   SrcList *pSrc;.
28ab4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28ab5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28ab6 52 65 73 65 74 43 6f 75 6e 74 2c 20 30 2c 20 30  ResetCount, 0, 0
28ab7 29 3b 0a 20 20 20 20 20 20 20 20 70 53 72 63 20  );.        pSrc 
28ab8 3d 20 74 61 72 67 65 74 53 72 63 4c 69 73 74 28  = targetSrcList(
28ab9 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
28aba 53 74 65 70 29 3b 0a 20 20 20 20 20 20 20 20 73  Step);.        s
28abb 71 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d  qlite3DeleteFrom
28abc 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c 20 0a  (pParse, pSrc, .
28abd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28abe 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28abf 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 54 72  3ExprDup(db, pTr
28ac0 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72  iggerStep->pWher
28ac1 65 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  e));.        sql
28ac2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28ac3 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c  , OP_ResetCount,
28ac4 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   1, 0);.        
28ac5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
28ac6 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20       default:.  
28ac7 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29 3b        assert(0);
28ac8 0a 20 20 20 20 7d 20 0a 20 20 20 20 70 54 72 69  .    } .    pTri
28ac9 67 67 65 72 53 74 65 70 20 3d 20 70 54 72 69 67  ggerStep = pTrig
28aca 67 65 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a  gerStep->pNext;.
28acb 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
28acc 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
28acd 6e 74 65 78 74 50 6f 70 2c 20 30 2c 20 30 29 3b  ntextPop, 0, 0);
28ace 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28  .  VdbeComment((
28acf 76 2c 20 22 65 6e 64 20 74 72 69 67 67 65 72 20  v, "end trigger 
28ad0 25 73 22 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e  %s", pStepList->
28ad1 70 54 72 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 0a  pTrig->name));..
28ad2 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
28ad3 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c  *.** This is cal
28ad4 6c 65 64 20 74 6f 20 63 6f 64 65 20 46 4f 52 20  led to code FOR 
28ad5 45 41 43 48 20 52 4f 57 20 74 72 69 67 67 65 72  EACH ROW trigger
28ad6 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  s..**.** When th
28ad7 65 20 63 6f 64 65 20 74 68 61 74 20 74 68 69 73  e code that this
28ad8 20 66 75 6e 63 74 69 6f 6e 20 67 65 6e 65 72 61   function genera
28ad9 74 65 73 20 69 73 20 65 78 65 63 75 74 65 64 2c  tes is executed,
28ada 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a   the following .
28adb 2a 2a 20 6d 75 73 74 20 62 65 20 74 72 75 65 3a  ** must be true:
28adc 0a 2a 2a 0a 2a 2a 20 31 2e 20 4e 6f 20 63 75 72  .**.** 1. No cur
28add 73 6f 72 73 20 6d 61 79 20 62 65 20 6f 70 65 6e  sors may be open
28ade 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
28adf 61 62 61 73 65 2e 20 20 28 42 75 74 20 6e 65 77  abase.  (But new
28ae0 49 64 78 20 61 6e 64 20 6f 6c 64 49 64 78 0a 2a  Idx and oldIdx.*
28ae1 2a 20 20 20 20 63 61 6e 20 62 65 20 69 6e 64 69  *    can be indi
28ae2 63 65 73 20 6f 66 20 63 75 72 73 6f 72 73 20 69  ces of cursors i
28ae3 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  n temporary tabl
28ae4 65 73 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 29  es.  See below.)
28ae5 0a 2a 2a 0a 2a 2a 20 32 2e 20 49 66 20 74 68 65  .**.** 2. If the
28ae6 20 74 72 69 67 67 65 72 73 20 62 65 69 6e 67 20   triggers being 
28ae7 63 6f 64 65 64 20 61 72 65 20 4f 4e 20 49 4e 53  coded are ON INS
28ae8 45 52 54 20 6f 72 20 4f 4e 20 55 50 44 41 54 45  ERT or ON UPDATE
28ae9 20 74 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a   triggers, then.
28aea 2a 2a 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72  **    a temporar
28aeb 79 20 76 64 62 65 20 63 75 72 73 6f 72 20 28 69  y vdbe cursor (i
28aec 6e 64 65 78 20 6e 65 77 49 64 78 29 20 6d 75 73  ndex newIdx) mus
28aed 74 20 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f  t be open and po
28aee 69 6e 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20  inting at.**    
28aef 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67  a row containing
28af0 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75   values to be su
28af1 62 73 74 69 74 75 74 65 64 20 66 6f 72 20 6e 65  bstituted for ne
28af2 77 2e 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20  w.* expressions 
28af3 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69  in the.**    tri
28af4 67 67 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e  gger program(s).
28af5 0a 2a 2a 0a 2a 2a 20 33 2e 20 49 66 20 74 68 65  .**.** 3. If the
28af6 20 74 72 69 67 67 65 72 73 20 62 65 69 6e 67 20   triggers being 
28af7 63 6f 64 65 64 20 61 72 65 20 4f 4e 20 44 45 4c  coded are ON DEL
28af8 45 54 45 20 6f 72 20 4f 4e 20 55 50 44 41 54 45  ETE or ON UPDATE
28af9 20 74 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a   triggers, then.
28afa 2a 2a 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72  **    a temporar
28afb 79 20 76 64 62 65 20 63 75 72 73 6f 72 20 28 69  y vdbe cursor (i
28afc 6e 64 65 78 20 6f 6c 64 49 64 78 29 20 6d 75 73  ndex oldIdx) mus
28afd 74 20 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f  t be open and po
28afe 69 6e 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20  inting at.**    
28aff 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67  a row containing
28b00 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75   values to be su
28b01 62 73 74 69 74 75 74 65 64 20 66 6f 72 20 6f 6c  bstituted for ol
28b02 64 2e 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20  d.* expressions 
28b03 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69  in the.**    tri
28b04 67 67 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e  gger program(s).
28b05 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 79 20 61  .**.** If they a
28b06 72 65 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  re not NULL, the
28b07 20 70 69 4f 6c 64 43 6f 6c 4d 61 73 6b 20 61 6e   piOldColMask an
28b08 64 20 70 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 6f  d piNewColMask o
28b09 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73 0a  utput variables.
28b0a 2a 2a 20 61 72 65 20 73 65 74 20 74 6f 20 76 61  ** are set to va
28b0b 6c 75 65 73 20 74 68 61 74 20 64 65 73 63 72 69  lues that descri
28b0c 62 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 75  be the columns u
28b0d 73 65 64 20 62 79 20 74 68 65 20 74 72 69 67 67  sed by the trigg
28b0e 65 72 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 6e  er program.** in
28b0f 20 74 68 65 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e   the OLD.* and N
28b10 45 57 2e 2a 20 74 61 62 6c 65 73 20 72 65 73 70  EW.* tables resp
28b11 65 63 74 69 76 65 6c 79 2e 20 49 66 20 63 6f 6c  ectively. If col
28b12 75 6d 6e 20 4e 20 6f 66 20 74 68 65 20 0a 2a 2a  umn N of the .**
28b13 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 69 73   pseudo-table is
28b14 20 72 65 61 64 20 61 74 20 6c 65 61 73 74 20 6f   read at least o
28b15 6e 63 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70  nce, the corresp
28b16 6f 6e 64 69 6e 67 20 62 69 74 20 6f 66 20 74 68  onding bit of th
28b17 65 20 6f 75 74 70 75 74 0a 2a 2a 20 6d 61 73 6b  e output.** mask
28b18 20 69 73 20 73 65 74 2e 20 49 66 20 61 20 63 6f   is set. If a co
28b19 6c 75 6d 6e 20 77 69 74 68 20 61 6e 20 69 6e 64  lumn with an ind
28b1a 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  ex greater than 
28b1b 33 32 20 69 73 20 72 65 61 64 2c 20 74 68 65 0a  32 is read, the.
28b1c 2a 2a 20 6f 75 74 70 75 74 20 6d 61 73 6b 20 69  ** output mask i
28b1d 73 20 73 65 74 20 74 6f 20 74 68 65 20 73 70 65  s set to the spe
28b1e 63 69 61 6c 20 76 61 6c 75 65 20 30 78 66 66 66  cial value 0xfff
28b1f 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c  fffff..**.*/.SQL
28b20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
28b21 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
28b22 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a  igger(.  Parse *
28b23 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
28b24 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
28b25 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  /.  int op,     
28b26 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
28b27 6f 66 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  of TK_UPDATE, TK
28b28 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 44 45 4c 45  _INSERT, TK_DELE
28b29 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  TE */.  ExprList
28b2a 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20   *pChanges,  /* 
28b2b 43 68 61 6e 67 65 73 20 6c 69 73 74 20 66 6f 72  Changes list for
28b2c 20 61 6e 79 20 55 50 44 41 54 45 20 4f 46 20 74   any UPDATE OF t
28b2d 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74  riggers */.  int
28b2e 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20   tr_tm,         
28b2f 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47    /* One of TRIG
28b30 47 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49 47  GER_BEFORE, TRIG
28b31 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20 54  GER_AFTER */.  T
28b32 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
28b33 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
28b34 20 74 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72   to code trigger
28b35 73 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20  s from */.  int 
28b36 6e 65 77 49 64 78 2c 20 20 20 20 20 20 20 20 20  newIdx,         
28b37 20 2f 2a 20 54 68 65 20 69 6e 64 69 63 65 20 6f   /* The indice o
28b38 66 20 74 68 65 20 22 6e 65 77 22 20 72 6f 77 20  f the "new" row 
28b39 74 6f 20 61 63 63 65 73 73 20 2a 2f 0a 20 20 69  to access */.  i
28b3a 6e 74 20 6f 6c 64 49 64 78 2c 20 20 20 20 20 20  nt oldIdx,      
28b3b 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 69 63      /* The indic
28b3c 65 20 6f 66 20 74 68 65 20 22 6f 6c 64 22 20 72  e of the "old" r
28b3d 6f 77 20 74 6f 20 61 63 63 65 73 73 20 2a 2f 0a  ow to access */.
28b3e 20 20 69 6e 74 20 6f 72 63 6f 6e 66 2c 20 20 20    int orconf,   
28b3f 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e         /* ON CON
28b40 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a  FLICT policy */.
28b41 20 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70    int ignoreJump
28b42 2c 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 72 75  ,      /* Instru
28b43 63 74 69 6f 6e 20 74 6f 20 6a 75 6d 70 20 74 6f  ction to jump to
28b44 20 66 6f 72 20 52 41 49 53 45 28 49 47 4e 4f 52   for RAISE(IGNOR
28b45 45 29 20 2a 2f 0a 20 20 75 33 32 20 2a 70 69 4f  E) */.  u32 *piO
28b46 6c 64 43 6f 6c 4d 61 73 6b 2c 20 20 20 2f 2a 20  ldColMask,   /* 
28b47 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20 63 6f 6c  OUT: Mask of col
28b48 75 6d 6e 73 20 75 73 65 64 20 66 72 6f 6d 20 74  umns used from t
28b49 68 65 20 4f 4c 44 2e 2a 20 74 61 62 6c 65 20 2a  he OLD.* table *
28b4a 2f 0a 20 20 75 33 32 20 2a 70 69 4e 65 77 43 6f  /.  u32 *piNewCo
28b4b 6c 4d 61 73 6b 20 20 20 20 2f 2a 20 4f 55 54 3a  lMask    /* OUT:
28b4c 20 4d 61 73 6b 20 6f 66 20 63 6f 6c 75 6d 6e 73   Mask of columns
28b4d 20 75 73 65 64 20 66 72 6f 6d 20 74 68 65 20 4e   used from the N
28b4e 45 57 2e 2a 20 74 61 62 6c 65 20 2a 2f 0a 29 7b  EW.* table */.){
28b4f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 3b 0a 20  .  Trigger *p;. 
28b50 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
28b51 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 54 72 69  Parse->db;.  Tri
28b52 67 67 65 72 53 74 61 63 6b 20 74 72 69 67 53 74  ggerStack trigSt
28b53 61 63 6b 45 6e 74 72 79 3b 0a 0a 20 20 74 72 69  ackEntry;..  tri
28b54 67 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c 64 43  gStackEntry.oldC
28b55 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 74 72  olMask = 0;.  tr
28b56 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e 65 77  igStackEntry.new
28b57 43 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a 0a 20 20  ColMask = 0;..  
28b58 61 73 73 65 72 74 28 6f 70 20 3d 3d 20 54 4b 5f  assert(op == TK_
28b59 55 50 44 41 54 45 20 7c 7c 20 6f 70 20 3d 3d 20  UPDATE || op == 
28b5a 54 4b 5f 49 4e 53 45 52 54 20 7c 7c 20 6f 70 20  TK_INSERT || op 
28b5b 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 3b 0a 20  == TK_DELETE);. 
28b5c 20 61 73 73 65 72 74 28 74 72 5f 74 6d 20 3d 3d   assert(tr_tm ==
28b5d 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20   TRIGGER_BEFORE 
28b5e 7c 7c 20 74 72 5f 74 6d 20 3d 3d 20 54 52 49 47  || tr_tm == TRIG
28b5f 47 45 52 5f 41 46 54 45 52 20 29 3b 0a 0a 20 20  GER_AFTER );..  
28b60 61 73 73 65 72 74 28 6e 65 77 49 64 78 20 21 3d  assert(newIdx !=
28b61 20 2d 31 20 7c 7c 20 6f 6c 64 49 64 78 20 21 3d   -1 || oldIdx !=
28b62 20 2d 31 29 3b 0a 0a 20 20 66 6f 72 28 70 3d 70   -1);..  for(p=p
28b63 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20 70  Tab->pTrigger; p
28b64 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20  ; p=p->pNext){. 
28b65 20 20 20 69 6e 74 20 66 69 72 65 5f 74 68 69 73     int fire_this
28b66 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 44 65   = 0;..    /* De
28b67 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20  termine whether 
28b68 77 65 20 73 68 6f 75 6c 64 20 63 6f 64 65 20 74  we should code t
28b69 68 69 73 20 74 72 69 67 67 65 72 20 2a 2f 0a 20  his trigger */. 
28b6a 20 20 20 69 66 28 20 0a 20 20 20 20 20 20 70 2d     if( .      p-
28b6b 3e 6f 70 3d 3d 6f 70 20 26 26 20 0a 20 20 20 20  >op==op && .    
28b6c 20 20 70 2d 3e 74 72 5f 74 6d 3d 3d 74 72 5f 74    p->tr_tm==tr_t
28b6d 6d 20 26 26 20 0a 20 20 20 20 20 20 28 70 2d 3e  m && .      (p->
28b6e 70 53 63 68 65 6d 61 3d 3d 70 2d 3e 70 54 61 62  pSchema==p->pTab
28b6f 53 63 68 65 6d 61 20 7c 7c 20 70 2d 3e 70 53 63  Schema || p->pSc
28b70 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d  hema==db->aDb[1]
28b71 2e 70 53 63 68 65 6d 61 29 20 26 26 0a 20 20 20  .pSchema) &&.   
28b72 20 20 20 28 6f 70 21 3d 54 4b 5f 55 50 44 41 54     (op!=TK_UPDAT
28b73 45 7c 7c 21 70 2d 3e 70 43 6f 6c 75 6d 6e 73 7c  E||!p->pColumns|
28b74 7c 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72  |checkColumnOver
28b75 4c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c  Lap(p->pColumns,
28b76 70 43 68 61 6e 67 65 73 29 29 0a 20 20 20 20 29  pChanges)).    )
28b77 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 53  {.      TriggerS
28b78 74 61 63 6b 20 2a 70 53 3b 20 20 20 20 20 20 2f  tack *pS;      /
28b79 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 72 69  * Pointer to tri
28b7a 67 67 65 72 2d 73 74 61 63 6b 20 65 6e 74 72 79  gger-stack entry
28b7b 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 70 53   */.      for(pS
28b7c 3d 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  =pParse->trigSta
28b7d 63 6b 3b 20 70 53 20 26 26 20 70 21 3d 70 53 2d  ck; pS && p!=pS-
28b7e 3e 70 54 72 69 67 67 65 72 3b 20 70 53 3d 70 53  >pTrigger; pS=pS
28b7f 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20  ->pNext){}.     
28b80 20 69 66 28 20 21 70 53 20 29 7b 0a 20 20 20 20   if( !pS ){.    
28b81 20 20 20 20 66 69 72 65 5f 74 68 69 73 20 3d 20      fire_this = 
28b82 31 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 20 30  1;.      }.#if 0
28b83 20 20 20 20 2f 2a 20 47 69 76 65 20 6e 6f 20 77      /* Give no w
28b84 61 72 6e 69 6e 67 20 66 6f 72 20 72 65 63 75 72  arning for recur
28b85 73 69 76 65 20 74 72 69 67 67 65 72 73 2e 20 20  sive triggers.  
28b86 4a 75 73 74 20 64 6f 20 6e 6f 74 20 64 6f 20 74  Just do not do t
28b87 68 65 6d 20 2a 2f 0a 20 20 20 20 20 20 65 6c 73  hem */.      els
28b88 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
28b89 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
28b8a 65 2c 20 22 72 65 63 75 72 73 69 76 65 20 74 72  e, "recursive tr
28b8b 69 67 67 65 72 73 20 6e 6f 74 20 73 75 70 70 6f  iggers not suppo
28b8c 72 74 65 64 20 28 25 73 29 22 2c 0a 20 20 20 20  rted (%s)",.    
28b8d 20 20 20 20 20 20 20 20 70 2d 3e 6e 61 6d 65 29          p->name)
28b8e 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
28b8f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
28b90 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
28b91 20 20 7d 0a 20 0a 20 20 20 20 69 66 28 20 66 69    }. .    if( fi
28b92 72 65 5f 74 68 69 73 20 29 7b 0a 20 20 20 20 20  re_this ){.     
28b93 20 69 6e 74 20 65 6e 64 54 72 69 67 67 65 72 3b   int endTrigger;
28b94 0a 20 20 20 20 20 20 45 78 70 72 20 2a 20 77 68  .      Expr * wh
28b95 65 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 41 75  enExpr;.      Au
28b96 74 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65  thContext sConte
28b97 78 74 3b 0a 20 20 20 20 20 20 4e 61 6d 65 43 6f  xt;.      NameCo
28b98 6e 74 65 78 74 20 73 4e 43 3b 0a 0a 23 69 66 6e  ntext sNC;..#ifn
28b99 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28b9a 54 52 41 43 45 0a 20 20 20 20 20 20 73 71 6c 69  TRACE.      sqli
28b9b 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 70 50  te3VdbeAddOp4(pP
28b9c 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f  arse->pVdbe, OP_
28b9d 54 72 61 63 65 2c 20 30 2c 20 30 2c 20 30 2c 0a  Trace, 0, 0, 0,.
28b9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b9f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d          sqlite3M
28ba0 50 72 69 6e 74 66 28 64 62 2c 20 22 2d 2d 20 54  Printf(db, "-- T
28ba1 52 49 47 47 45 52 20 25 73 22 2c 20 70 2d 3e 6e  RIGGER %s", p->n
28ba2 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ame),.          
28ba3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 34                P4
28ba4 5f 44 59 4e 41 4d 49 43 29 3b 0a 23 65 6e 64 69  _DYNAMIC);.#endi
28ba5 66 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26  f.      memset(&
28ba6 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
28ba7 4e 43 29 29 3b 0a 20 20 20 20 20 20 73 4e 43 2e  NC));.      sNC.
28ba8 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
28ba9 0a 0a 20 20 20 20 20 20 2f 2a 20 50 75 73 68 20  ..      /* Push 
28baa 61 6e 20 65 6e 74 72 79 20 6f 6e 20 74 6f 20 74  an entry on to t
28bab 68 65 20 74 72 69 67 67 65 72 20 73 74 61 63 6b  he trigger stack
28bac 20 2a 2f 0a 20 20 20 20 20 20 74 72 69 67 53 74   */.      trigSt
28bad 61 63 6b 45 6e 74 72 79 2e 70 54 72 69 67 67 65  ackEntry.pTrigge
28bae 72 20 3d 20 70 3b 0a 20 20 20 20 20 20 74 72 69  r = p;.      tri
28baf 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e 65 77 49  gStackEntry.newI
28bb0 64 78 20 3d 20 6e 65 77 49 64 78 3b 0a 20 20 20  dx = newIdx;.   
28bb1 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72     trigStackEntr
28bb2 79 2e 6f 6c 64 49 64 78 20 3d 20 6f 6c 64 49 64  y.oldIdx = oldId
28bb3 78 3b 0a 20 20 20 20 20 20 74 72 69 67 53 74 61  x;.      trigSta
28bb4 63 6b 45 6e 74 72 79 2e 70 54 61 62 20 3d 20 70  ckEntry.pTab = p
28bb5 54 61 62 3b 0a 20 20 20 20 20 20 74 72 69 67 53  Tab;.      trigS
28bb6 74 61 63 6b 45 6e 74 72 79 2e 70 4e 65 78 74 20  tackEntry.pNext 
28bb7 3d 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74  = pParse->trigSt
28bb8 61 63 6b 3b 0a 20 20 20 20 20 20 74 72 69 67 53  ack;.      trigS
28bb9 74 61 63 6b 45 6e 74 72 79 2e 69 67 6e 6f 72 65  tackEntry.ignore
28bba 4a 75 6d 70 20 3d 20 69 67 6e 6f 72 65 4a 75 6d  Jump = ignoreJum
28bbb 70 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  p;.      pParse-
28bbc 3e 74 72 69 67 53 74 61 63 6b 20 3d 20 26 74 72  >trigStack = &tr
28bbd 69 67 53 74 61 63 6b 45 6e 74 72 79 3b 0a 20 20  igStackEntry;.  
28bbe 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 43      sqlite3AuthC
28bbf 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61 72 73  ontextPush(pPars
28bc0 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 2d  e, &sContext, p-
28bc1 3e 6e 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 2f  >name);..      /
28bc2 2a 20 63 6f 64 65 20 74 68 65 20 57 48 45 4e 20  * code the WHEN 
28bc3 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20  clause */.      
28bc4 65 6e 64 54 72 69 67 67 65 72 20 3d 20 73 71 6c  endTrigger = sql
28bc5 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
28bc6 6c 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29  l(pParse->pVdbe)
28bc7 3b 0a 20 20 20 20 20 20 77 68 65 6e 45 78 70 72  ;.      whenExpr
28bc8 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
28bc9 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 6e 29 3b  p(db, p->pWhen);
28bca 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  .      if( db->m
28bcb 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 73  allocFailed || s
28bcc 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
28bcd 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 77 68 65  rNames(&sNC, whe
28bce 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  nExpr) ){.      
28bcf 20 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74    pParse->trigSt
28bd0 61 63 6b 20 3d 20 74 72 69 67 53 74 61 63 6b 45  ack = trigStackE
28bd1 6e 74 72 79 2e 70 4e 65 78 74 3b 0a 20 20 20 20  ntry.pNext;.    
28bd2 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
28bd3 65 6c 65 74 65 28 64 62 2c 20 77 68 65 6e 45 78  elete(db, whenEx
28bd4 70 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  pr);.        ret
28bd5 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
28bd6 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
28bd7 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
28bd8 77 68 65 6e 45 78 70 72 2c 20 65 6e 64 54 72 69  whenExpr, endTri
28bd9 67 67 65 72 2c 20 53 51 4c 49 54 45 5f 4a 55 4d  gger, SQLITE_JUM
28bda 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20  PIFNULL);.      
28bdb 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
28bdc 65 28 64 62 2c 20 77 68 65 6e 45 78 70 72 29 3b  e(db, whenExpr);
28bdd 0a 0a 20 20 20 20 20 20 63 6f 64 65 54 72 69 67  ..      codeTrig
28bde 67 65 72 50 72 6f 67 72 61 6d 28 70 50 61 72 73  gerProgram(pPars
28bdf 65 2c 20 70 2d 3e 73 74 65 70 5f 6c 69 73 74 2c  e, p->step_list,
28be0 20 6f 72 63 6f 6e 66 29 3b 20 0a 0a 20 20 20 20   orconf); ..    
28be1 20 20 2f 2a 20 50 6f 70 20 74 68 65 20 65 6e 74    /* Pop the ent
28be2 72 79 20 6f 66 66 20 74 68 65 20 74 72 69 67 67  ry off the trigg
28be3 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20 20  er stack */.    
28be4 20 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74    pParse->trigSt
28be5 61 63 6b 20 3d 20 74 72 69 67 53 74 61 63 6b 45  ack = trigStackE
28be6 6e 74 72 79 2e 70 4e 65 78 74 3b 0a 20 20 20 20  ntry.pNext;.    
28be7 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e    sqlite3AuthCon
28be8 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78  textPop(&sContex
28be9 74 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  t);..      sqlit
28bea 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
28beb 65 6c 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  el(pParse->pVdbe
28bec 2c 20 65 6e 64 54 72 69 67 67 65 72 29 3b 0a 20  , endTrigger);. 
28bed 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
28bee 69 4f 6c 64 43 6f 6c 4d 61 73 6b 20 29 20 2a 70  iOldColMask ) *p
28bef 69 4f 6c 64 43 6f 6c 4d 61 73 6b 20 7c 3d 20 74  iOldColMask |= t
28bf0 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c  rigStackEntry.ol
28bf1 64 43 6f 6c 4d 61 73 6b 3b 0a 20 20 69 66 28 20  dColMask;.  if( 
28bf2 70 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 29 20 2a  piNewColMask ) *
28bf3 70 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 7c 3d 20  piNewColMask |= 
28bf4 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e  trigStackEntry.n
28bf5 65 77 43 6f 6c 4d 61 73 6b 3b 0a 20 20 72 65 74  ewColMask;.  ret
28bf6 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 0;.}.#endif 
28bf7 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
28bf8 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
28bf9 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
28bfa 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 72 69 67  **** End of trig
28bfb 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ger.c **********
28bfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28bfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28bfe 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
28bff 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
28c00 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  update.c *******
28c01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c03 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
28c04 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
28c05 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
28c06 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
28c07 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
28c08 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
28c09 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
28c0a 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
28c0b 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
28c0c 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
28c0d 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
28c0e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
28c0f 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
28c10 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
28c11 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
28c12 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
28c13 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
28c14 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
28c15 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
28c16 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
28c17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
28c1b 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
28c1c 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65  s C code routine
28c1d 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65  s that are calle
28c1e 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a  d by the parser.
28c1f 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 55 50 44  ** to handle UPD
28c20 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ATE statements..
28c21 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 70 64 61 74  **.** $Id: updat
28c22 65 2e 63 2c 76 20 31 2e 31 39 31 20 32 30 30 38  e.c,v 1.191 2008
28c23 2f 31 32 2f 32 33 20 32 33 3a 35 36 3a 32 32 20  /12/23 23:56:22 
28c24 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69  drh Exp $.*/..#i
28c25 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28c26 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
28c27 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
28c28 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20  ation */.static 
28c29 76 6f 69 64 20 75 70 64 61 74 65 56 69 72 74 75  void updateVirtu
28c2a 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  alTable(.  Parse
28c2b 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
28c2c 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
28c2d 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
28c2e 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
28c2f 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
28c30 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64 69  table to be modi
28c31 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20  fied */.  Table 
28c32 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f  *pTab,         /
28c33 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74 61  * The virtual ta
28c34 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ble */.  ExprLis
28c35 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a  t *pChanges,  /*
28c36 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20   The columns to 
28c37 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20 55 50  change in the UP
28c38 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a  DATE statement *
28c39 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64  /.  Expr *pRowid
28c3a 45 78 70 72 2c 20 20 20 20 2f 2a 20 45 78 70 72  Expr,    /* Expr
28c3b 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f 20 72  ession used to r
28c3c 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72 6f 77  ecompute the row
28c3d 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52  id */.  int *aXR
28c3e 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ef,          /* 
28c3f 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f 6c  Mapping from col
28c40 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74 6f 20  umns of pTab to 
28c41 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68 61 6e  entries in pChan
28c42 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ges */.  Expr *p
28c43 57 68 65 72 65 20 20 20 20 20 20 20 20 20 2f 2a  Where         /*
28c44 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66   WHERE clause of
28c45 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74   the UPDATE stat
28c46 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 23 65 6e 64  ement */.);.#end
28c47 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
28c48 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
28c49 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 6f 73  /../*.** The mos
28c4a 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 65 64  t recently coded
28c4b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73   instruction was
28c4c 20 61 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f   an OP_Column to
28c4d 20 72 65 74 72 69 65 76 65 20 74 68 65 0a 2a 2a   retrieve the.**
28c4e 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   i-th column of 
28c4f 74 61 62 6c 65 20 70 54 61 62 2e 20 54 68 69 73  table pTab. This
28c50 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
28c51 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 6f  e P4 parameter o
28c52 66 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 43 6f 6c  f the .** OP_Col
28c53 75 6d 6e 20 74 6f 20 74 68 65 20 64 65 66 61 75  umn to the defau
28c54 6c 74 20 76 61 6c 75 65 2c 20 69 66 20 61 6e 79  lt value, if any
28c55 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ..**.** The defa
28c56 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 61 20 63  ult value of a c
28c57 6f 6c 75 6d 6e 20 69 73 20 73 70 65 63 69 66 69  olumn is specifi
28c58 65 64 20 62 79 20 61 20 44 45 46 41 55 4c 54 20  ed by a DEFAULT 
28c59 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 0a 2a  clause in the .*
28c5a 2a 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74  * column definit
28c5b 69 6f 6e 2e 20 54 68 69 73 20 77 61 73 20 65 69  ion. This was ei
28c5c 74 68 65 72 20 73 75 70 70 6c 69 65 64 20 62 79  ther supplied by
28c5d 20 74 68 65 20 75 73 65 72 20 77 68 65 6e 20 74   the user when t
28c5e 68 65 20 74 61 62 6c 65 0a 2a 2a 20 77 61 73 20  he table.** was 
28c5f 63 72 65 61 74 65 64 2c 20 6f 72 20 61 64 64 65  created, or adde
28c60 64 20 6c 61 74 65 72 20 74 6f 20 74 68 65 20 74  d later to the t
28c61 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 20  able definition 
28c62 62 79 20 61 6e 20 41 4c 54 45 52 20 54 41 42 4c  by an ALTER TABL
28c63 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 49 66  E.** command. If
28c64 20 74 68 65 20 6c 61 74 74 65 72 2c 20 74 68 65   the latter, the
28c65 6e 20 74 68 65 20 72 6f 77 2d 72 65 63 6f 72 64  n the row-record
28c66 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62  s in the table b
28c67 74 72 65 65 20 6f 6e 20 64 69 73 6b 0a 2a 2a 20  tree on disk.** 
28c68 6d 61 79 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  may not contain 
28c69 61 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  a value for the 
28c6a 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20 64  column and the d
28c6b 65 66 61 75 6c 74 20 76 61 6c 75 65 2c 20 74 61  efault value, ta
28c6c 6b 65 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ken.** from the 
28c6d 50 34 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  P4 parameter of 
28c6e 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 69 6e  the OP_Column in
28c6f 73 74 72 75 63 74 69 6f 6e 2c 20 69 73 20 72 65  struction, is re
28c70 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a  turned instead..
28c71 2a 2a 20 49 66 20 74 68 65 20 66 6f 72 6d 65 72  ** If the former
28c72 2c 20 74 68 65 6e 20 61 6c 6c 20 72 6f 77 2d 72  , then all row-r
28c73 65 63 6f 72 64 73 20 61 72 65 20 67 75 61 72 61  ecords are guara
28c74 6e 74 65 65 64 20 74 6f 20 69 6e 63 6c 75 64 65  nteed to include
28c75 20 61 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20   a value.** for 
28c76 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74  the column and t
28c77 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20 6e  he P4 value is n
28c78 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a  ot required..**.
28c79 2a 2a 20 43 6f 6c 75 6d 6e 20 64 65 66 69 6e 69  ** Column defini
28c7a 74 69 6f 6e 73 20 63 72 65 61 74 65 64 20 62 79  tions created by
28c7b 20 61 6e 20 41 4c 54 45 52 20 54 41 42 4c 45 20   an ALTER TABLE 
28c7c 63 6f 6d 6d 61 6e 64 20 6d 61 79 20 6f 6e 6c 79  command may only
28c7d 20 68 61 76 65 20 0a 2a 2a 20 6c 69 74 65 72 61   have .** litera
28c7e 6c 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73  l default values
28c7f 20 73 70 65 63 69 66 69 65 64 3a 20 61 20 6e 75   specified: a nu
28c80 6d 62 65 72 2c 20 6e 75 6c 6c 20 6f 72 20 61 20  mber, null or a 
28c81 73 74 72 69 6e 67 2e 20 28 49 66 20 61 20 6d 6f  string. (If a mo
28c82 72 65 0a 2a 2a 20 63 6f 6d 70 6c 69 63 61 74 65  re.** complicate
28c83 64 20 64 65 66 61 75 6c 74 20 65 78 70 72 65 73  d default expres
28c84 73 69 6f 6e 20 76 61 6c 75 65 20 77 61 73 20 70  sion value was p
28c85 72 6f 76 69 64 65 64 2c 20 69 74 20 69 73 20 65  rovided, it is e
28c86 76 61 6c 75 61 74 65 64 20 0a 2a 2a 20 77 68 65  valuated .** whe
28c87 6e 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c  n the ALTER TABL
28c88 45 20 69 73 20 65 78 65 63 75 74 65 64 20 61 6e  E is executed an
28c89 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 69 74  d one of the lit
28c8a 65 72 61 6c 20 76 61 6c 75 65 73 20 77 72 69 74  eral values writ
28c8b 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20  ten.** into the 
28c8c 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
28c8d 62 6c 65 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 72  ble.).**.** Ther
28c8e 65 66 6f 72 65 2c 20 74 68 65 20 50 34 20 70 61  efore, the P4 pa
28c8f 72 61 6d 65 74 65 72 20 69 73 20 6f 6e 6c 79 20  rameter is only 
28c90 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65 20  required if the 
28c91 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f  default value fo
28c92 72 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20  r.** the column 
28c93 69 73 20 61 20 6c 69 74 65 72 61 6c 20 6e 75 6d  is a literal num
28c94 62 65 72 2c 20 73 74 72 69 6e 67 20 6f 72 20 6e  ber, string or n
28c95 75 6c 6c 2e 20 54 68 65 20 73 71 6c 69 74 65 33  ull. The sqlite3
28c96 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 29 0a  ValueFromExpr().
28c97 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  ** function is c
28c98 61 70 61 62 6c 65 20 6f 66 20 74 72 61 6e 73 66  apable of transf
28c99 6f 72 6d 69 6e 67 20 74 68 65 73 65 20 74 79 70  orming these typ
28c9a 65 73 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  es of expression
28c9b 73 20 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  s into.** sqlite
28c9c 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e  3_value objects.
28c9d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
28c9e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
28c9f 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62  olumnDefault(Vdb
28ca0 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70 54 61  e *v, Table *pTa
28ca1 62 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 66 28  b, int i){.  if(
28ca2 20 70 54 61 62 20 26 26 20 21 70 54 61 62 2d 3e   pTab && !pTab->
28ca3 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73  pSelect ){.    s
28ca4 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
28ca5 61 6c 75 65 3b 0a 20 20 20 20 75 38 20 65 6e 63  alue;.    u8 enc
28ca6 20 3d 20 45 4e 43 28 73 71 6c 69 74 65 33 56 64   = ENC(sqlite3Vd
28ca7 62 65 44 62 28 76 29 29 3b 0a 20 20 20 20 43 6f  beDb(v));.    Co
28ca8 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54  lumn *pCol = &pT
28ca9 61 62 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20  ab->aCol[i];.   
28caa 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
28cab 20 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e   "%s.%s", pTab->
28cac 7a 4e 61 6d 65 2c 20 70 43 6f 6c 2d 3e 7a 4e 61  zName, pCol->zNa
28cad 6d 65 29 29 3b 0a 20 20 20 20 61 73 73 65 72 74  me));.    assert
28cae 28 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  ( i<pTab->nCol )
28caf 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  ;.    sqlite3Val
28cb0 75 65 46 72 6f 6d 45 78 70 72 28 73 71 6c 69 74  ueFromExpr(sqlit
28cb1 65 33 56 64 62 65 44 62 28 76 29 2c 20 70 43 6f  e3VdbeDb(v), pCo
28cb2 6c 2d 3e 70 44 66 6c 74 2c 20 65 6e 63 2c 20 0a  l->pDflt, enc, .
28cb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28cb4 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 61           pCol->a
28cb5 66 66 69 6e 69 74 79 2c 20 26 70 56 61 6c 75 65  ffinity, &pValue
28cb6 29 3b 0a 20 20 20 20 69 66 28 20 70 56 61 6c 75  );.    if( pValu
28cb7 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
28cb8 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
28cb9 2c 20 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68 61  , -1, (const cha
28cba 72 20 2a 29 70 56 61 6c 75 65 2c 20 50 34 5f 4d  r *)pValue, P4_M
28cbb 45 4d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  EM);.    }.  }.}
28cbc 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20  ../*.** Process 
28cbd 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
28cbe 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 55 50 44  ent..**.**   UPD
28cbf 41 54 45 20 4f 52 20 49 47 4e 4f 52 45 20 74 61  ATE OR IGNORE ta
28cc0 62 6c 65 5f 77 78 79 7a 20 53 45 54 20 61 3d 62  ble_wxyz SET a=b
28cc1 2c 20 63 3d 64 20 57 48 45 52 45 20 65 3c 35 20  , c=d WHERE e<5 
28cc2 41 4e 44 20 66 20 4e 4f 54 20 4e 55 4c 4c 3b 0a  AND f NOT NULL;.
28cc3 2a 2a 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f  **          \___
28cc4 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f  ____/ \________/
28cc5 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 2f 20 20 20       \______/   
28cc6 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f      \___________
28cc7 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20 20 20 20 20 20  _____/.*        
28cc8 20 20 20 20 6f 6e 45 72 72 6f 72 20 20 20 70 54      onError   pT
28cc9 61 62 4c 69 73 74 20 20 20 20 20 20 70 43 68 61  abList      pCha
28cca 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 20 20  nges            
28ccb 20 70 57 68 65 72 65 0a 2a 2f 0a 53 51 4c 49 54   pWhere.*/.SQLIT
28ccc 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
28ccd 71 6c 69 74 65 33 55 70 64 61 74 65 28 0a 20 20  qlite3Update(.  
28cce 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
28ccf 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
28cd0 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
28cd1 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
28cd2 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  ist,     /* The 
28cd3 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20 77  table in which w
28cd4 65 20 73 68 6f 75 6c 64 20 63 68 61 6e 67 65 20  e should change 
28cd5 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70 72  things */.  Expr
28cd6 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20  List *pChanges, 
28cd7 20 20 20 2f 2a 20 54 68 69 6e 67 73 20 74 6f 20     /* Things to 
28cd8 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20  be changed */.  
28cd9 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20  Expr *pWhere,   
28cda 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
28cdb 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  ERE clause.  May
28cdc 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 69 6e   be null */.  in
28cdd 74 20 6f 6e 45 72 72 6f 72 20 20 20 20 20 20 20  t onError       
28cde 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68       /* How to h
28cdf 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74  andle constraint
28ce0 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20   errors */.){.  
28ce1 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
28ce2 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
28ce3 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 54 61 62  ounters */.  Tab
28ce4 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
28ce5 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
28ce6 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a   to be updated *
28ce7 2f 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 30  /.  int addr = 0
28ce8 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44  ;          /* VD
28ce9 42 45 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61  BE instruction a
28cea 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 74  ddress of the st
28ceb 61 72 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  art of the loop 
28cec 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  */.  WhereInfo *
28ced 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49  pWInfo;     /* I
28cee 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
28cef 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
28cf0 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  e */.  Vdbe *v; 
28cf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28cf2 20 54 68 65 20 76 69 72 74 75 61 6c 20 64 61 74   The virtual dat
28cf3 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f 0a  abase engine */.
28cf4 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
28cf5 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
28cf6 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64  looping over ind
28cf7 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 49  ices */.  int nI
28cf8 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
28cf9 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e   /* Number of in
28cfa 64 69 63 65 73 20 74 68 61 74 20 6e 65 65 64 20  dices that need 
28cfb 75 70 64 61 74 69 6e 67 20 2a 2f 0a 20 20 69 6e  updating */.  in
28cfc 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20 20  t iCur;         
28cfd 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72       /* VDBE Cur
28cfe 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 70 54  sor number of pT
28cff 61 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ab */.  sqlite3 
28d00 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f  *db;           /
28d01 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73  * The database s
28d02 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e  tructure */.  in
28d03 74 20 2a 61 52 65 67 49 64 78 20 3d 20 30 3b 20  t *aRegIdx = 0; 
28d04 20 20 20 20 20 2f 2a 20 4f 6e 65 20 72 65 67 69       /* One regi
28d05 73 74 65 72 20 61 73 73 69 67 6e 65 64 20 74 6f  ster assigned to
28d06 20 65 61 63 68 20 69 6e 64 65 78 20 74 6f 20 62   each index to b
28d07 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69  e updated */.  i
28d08 6e 74 20 2a 61 58 52 65 66 20 3d 20 30 3b 20 20  nt *aXRef = 0;  
28d09 20 20 20 20 20 20 2f 2a 20 61 58 52 65 66 5b 69        /* aXRef[i
28d0a 5d 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69  ] is the index i
28d0b 6e 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 5d 20  n pChanges->a[] 
28d0c 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 20  of the.         
28d0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28d0e 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ** an expression
28d0f 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 6f   for the i-th co
28d10 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c  lumn of the tabl
28d11 65 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e..             
28d12 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61              ** a
28d13 58 52 65 66 5b 69 5d 3d 3d 2d 31 20 69 66 20 74  XRef[i]==-1 if t
28d14 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 69  he i-th column i
28d15 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2e 20 2a  s not changed. *
28d16 2f 0a 20 20 69 6e 74 20 63 68 6e 67 52 6f 77 69  /.  int chngRowi
28d17 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  d;         /* Tr
28d18 75 65 20 69 66 20 74 68 65 20 72 65 63 6f 72 64  ue if the record
28d19 20 6e 75 6d 62 65 72 20 69 73 20 62 65 69 6e 67   number is being
28d1a 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 45 78   changed */.  Ex
28d1b 70 72 20 2a 70 52 6f 77 69 64 45 78 70 72 20 3d  pr *pRowidExpr =
28d1c 20 30 3b 20 20 2f 2a 20 45 78 70 72 65 73 73 69   0;  /* Expressi
28d1d 6f 6e 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20  on defining the 
28d1e 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  new record numbe
28d1f 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 41  r */.  int openA
28d20 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  ll = 0;       /*
28d21 20 54 72 75 65 20 69 66 20 61 6c 6c 20 69 6e 64   True if all ind
28d22 69 63 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20  ices need to be 
28d23 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 41 75 74 68  opened */.  Auth
28d24 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74  Context sContext
28d25 3b 20 20 2f 2a 20 54 68 65 20 61 75 74 68 6f 72  ;  /* The author
28d26 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20  ization context 
28d27 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  */.  NameContext
28d28 20 73 4e 43 3b 20 20 20 20 20 20 20 2f 2a 20 54   sNC;       /* T
28d29 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20  he name-context 
28d2a 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65  to resolve expre
28d2b 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69  ssions in */.  i
28d2c 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
28d2d 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
28d2e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
28d2f 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64   table being upd
28d30 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 31  ated */.  int j1
28d31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
28d32 20 2f 2a 20 41 64 64 72 65 73 73 65 73 20 6f 66   /* Addresses of
28d33 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f   jump instructio
28d34 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6b 4f 6e  ns */.  int okOn
28d35 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 20 2f  ePass;         /
28d36 2a 20 54 72 75 65 20 66 6f 72 20 6f 6e 65 2d 70  * True for one-p
28d37 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 77 69  ass algorithm wi
28d38 74 68 6f 75 74 20 74 68 65 20 46 49 46 4f 20 2a  thout the FIFO *
28d39 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
28d3a 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
28d3b 20 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20   int isView;    
28d3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28d3d 20 54 72 79 69 6e 67 20 74 6f 20 75 70 64 61 74   Trying to updat
28d3e 65 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e  e a view */.  in
28d3f 74 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74  t triggers_exist
28d40 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 72   = 0;      /* Tr
28d41 75 65 20 69 66 20 61 6e 79 20 72 6f 77 20 74 72  ue if any row tr
28d42 69 67 67 65 72 73 20 65 78 69 73 74 20 2a 2f 0a  iggers exist */.
28d43 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 42 65  #endif.  int iBe
28d44 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 72 20  ginAfterTrigger 
28d45 3d 20 30 3b 20 20 2f 2a 20 41 64 64 72 65 73 73  = 0;  /* Address
28d46 20 6f 66 20 61 66 74 65 72 20 74 72 69 67 67 65   of after trigge
28d47 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69  r program */.  i
28d48 6e 74 20 69 45 6e 64 41 66 74 65 72 54 72 69 67  nt iEndAfterTrig
28d49 67 65 72 20 3d 20 30 3b 20 20 20 20 2f 2a 20 45  ger = 0;    /* E
28d4a 78 69 74 20 6f 66 20 61 66 74 65 72 20 74 72 69  xit of after tri
28d4b 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  gger program */.
28d4c 20 20 69 6e 74 20 69 42 65 67 69 6e 42 65 66 6f    int iBeginBefo
28d4d 72 65 54 72 69 67 67 65 72 20 3d 20 30 3b 20 2f  reTrigger = 0; /
28d4e 2a 20 41 64 64 72 65 73 73 20 6f 66 20 62 65 66  * Address of bef
28d4f 6f 72 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  ore trigger prog
28d50 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e  ram */.  int iEn
28d51 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 20 3d  dBeforeTrigger =
28d52 20 30 3b 20 20 20 2f 2a 20 45 78 69 74 20 6f 66   0;   /* Exit of
28d53 20 62 65 66 6f 72 65 20 74 72 69 67 67 65 72 20   before trigger 
28d54 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 75 33 32  program */.  u32
28d55 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 3d 20   old_col_mask = 
28d56 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73  0;        /* Mas
28d57 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d  k of OLD.* colum
28d58 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 75  ns in use */.  u
28d59 33 32 20 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 20  32 new_col_mask 
28d5a 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  = 0;        /* M
28d5b 61 73 6b 20 6f 66 20 4e 45 57 2e 2a 20 63 6f 6c  ask of NEW.* col
28d5c 75 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 0a  umns in use */..
28d5d 20 20 69 6e 74 20 6e 65 77 49 64 78 20 20 20 20    int newIdx    
28d5e 20 20 3d 20 2d 31 3b 20 20 2f 2a 20 69 6e 64 65    = -1;  /* inde
28d5f 78 20 6f 66 20 74 72 69 67 67 65 72 20 22 6e 65  x of trigger "ne
28d60 77 22 20 74 65 6d 70 20 74 61 62 6c 65 20 20 20  w" temp table   
28d61 20 20 20 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64      */.  int old
28d62 49 64 78 20 20 20 20 20 20 3d 20 2d 31 3b 20 20  Idx      = -1;  
28d63 2f 2a 20 69 6e 64 65 78 20 6f 66 20 74 72 69 67  /* index of trig
28d64 67 65 72 20 22 6f 6c 64 22 20 74 65 6d 70 20 74  ger "old" temp t
28d65 61 62 6c 65 20 20 20 20 20 20 20 2a 2f 0a 0a 20  able       */.. 
28d66 20 2f 2a 20 52 65 67 69 73 74 65 72 20 41 6c 6c   /* Register All
28d67 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  ocations */.  in
28d68 74 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20  t regRowCount = 
28d69 30 3b 20 20 20 2f 2a 20 41 20 63 6f 75 6e 74 20  0;   /* A count 
28d6a 6f 66 20 72 6f 77 73 20 63 68 61 6e 67 65 64 20  of rows changed 
28d6b 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64 52  */.  int regOldR
28d6c 6f 77 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 54  owid;       /* T
28d6d 68 65 20 6f 6c 64 20 72 6f 77 69 64 20 2a 2f 0a  he old rowid */.
28d6e 20 20 69 6e 74 20 72 65 67 4e 65 77 52 6f 77 69    int regNewRowi
28d6f 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  d;       /* The 
28d70 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  new rowid */.  i
28d71 6e 74 20 72 65 67 44 61 74 61 3b 20 20 20 20 20  nt regData;     
28d72 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 64 61 74        /* New dat
28d73 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 2a 2f  a for the row */
28d74 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74  .  int regRowSet
28d75 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 52 6f 77   = 0;     /* Row
28d76 73 65 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 62  set of rows to b
28d77 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 0a 20 20  e updated */..  
28d78 73 43 6f 6e 74 65 78 74 2e 70 50 61 72 73 65 20  sContext.pParse 
28d79 3d 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 72  = 0;.  db = pPar
28d7a 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50  se->db;.  if( pP
28d7b 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
28d7c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
28d7d 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74  {.    goto updat
28d7e 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  e_cleanup;.  }. 
28d7f 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73   assert( pTabLis
28d80 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20  t->nSrc==1 );.. 
28d81 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74   /* Locate the t
28d82 61 62 6c 65 20 77 68 69 63 68 20 77 65 20 77 61  able which we wa
28d83 6e 74 20 74 6f 20 75 70 64 61 74 65 2e 20 0a 20  nt to update. . 
28d84 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   */.  pTab = sql
28d85 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
28d86 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  p(pParse, pTabLi
28d87 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  st);.  if( pTab=
28d88 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65  =0 ) goto update
28d89 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 44 62 20  _cleanup;.  iDb 
28d8a 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
28d8b 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
28d8c 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
28d8d 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20  );..  /* Figure 
28d8e 6f 75 74 20 69 66 20 77 65 20 68 61 76 65 20 61  out if we have a
28d8f 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20  ny triggers and 
28d90 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  if the table bei
28d91 6e 67 0a 20 20 2a 2a 20 75 70 64 61 74 65 64 20  ng.  ** updated 
28d92 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23  is a view.  */.#
28d93 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
28d94 49 54 5f 54 52 49 47 47 45 52 0a 20 20 74 72 69  IT_TRIGGER.  tri
28d95 67 67 65 72 73 5f 65 78 69 73 74 20 3d 20 73 71  ggers_exist = sq
28d96 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69  lite3TriggersExi
28d97 73 74 28 70 54 61 62 2c 20 54 4b 5f 55 50 44 41  st(pTab, TK_UPDA
28d98 54 45 2c 20 70 43 68 61 6e 67 65 73 29 3b 0a 20  TE, pChanges);. 
28d99 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e   isView = pTab->
28d9a 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73  pSelect!=0;.#els
28d9b 65 0a 23 20 64 65 66 69 6e 65 20 74 72 69 67 67  e.# define trigg
28d9c 65 72 73 5f 65 78 69 73 74 20 30 0a 23 20 64 65  ers_exist 0.# de
28d9d 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65  fine isView 0.#e
28d9e 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
28d9f 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75  TE_OMIT_VIEW.# u
28da0 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20 64 65  ndef isView.# de
28da1 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65  fine isView 0.#e
28da2 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69  ndif..  if( sqli
28da3 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50  te3IsReadOnly(pP
28da4 61 72 73 65 2c 20 70 54 61 62 2c 20 74 72 69 67  arse, pTab, trig
28da5 67 65 72 73 5f 65 78 69 73 74 29 20 29 7b 0a 20  gers_exist) ){. 
28da6 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
28da7 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66  leanup;.  }.  if
28da8 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  ( sqlite3ViewGet
28da9 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
28daa 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  se, pTab) ){.   
28dab 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
28dac 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 58 52 65  anup;.  }.  aXRe
28dad 66 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  f = sqlite3DbMal
28dae 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
28daf 66 28 69 6e 74 29 20 2a 20 70 54 61 62 2d 3e 6e  f(int) * pTab->n
28db0 43 6f 6c 20 29 3b 0a 20 20 69 66 28 20 61 58 52  Col );.  if( aXR
28db1 65 66 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64  ef==0 ) goto upd
28db2 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 66  ate_cleanup;.  f
28db3 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
28db4 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61 58 52 65 66  nCol; i++) aXRef
28db5 5b 69 5d 20 3d 20 2d 31 3b 0a 0a 20 20 2f 2a 20  [i] = -1;..  /* 
28db6 49 66 20 74 68 65 72 65 20 61 72 65 20 46 4f 52  If there are FOR
28db7 20 45 41 43 48 20 52 4f 57 20 74 72 69 67 67 65   EACH ROW trigge
28db8 72 73 2c 20 61 6c 6c 6f 63 61 74 65 20 63 75 72  rs, allocate cur
28db9 73 6f 72 73 20 66 6f 72 20 74 68 65 0a 20 20 2a  sors for the.  *
28dba 2a 20 73 70 65 63 69 61 6c 20 4f 4c 44 20 61 6e  * special OLD an
28dbb 64 20 4e 45 57 20 74 61 62 6c 65 73 0a 20 20 2a  d NEW tables.  *
28dbc 2f 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73  /.  if( triggers
28dbd 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 6e 65  _exist ){.    ne
28dbe 77 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e  wIdx = pParse->n
28dbf 54 61 62 2b 2b 3b 0a 20 20 20 20 6f 6c 64 49 64  Tab++;.    oldId
28dc0 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  x = pParse->nTab
28dc1 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c  ++;.  }..  /* Al
28dc2 6c 6f 63 61 74 65 20 61 20 63 75 72 73 6f 72 73  locate a cursors
28dc3 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
28dc4 74 61 62 61 73 65 20 74 61 62 6c 65 20 61 6e 64  tabase table and
28dc5 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73   for all indices
28dc6 2e 0a 20 20 2a 2a 20 54 68 65 20 69 6e 64 65 78  ..  ** The index
28dc7 20 63 75 72 73 6f 72 73 20 6d 69 67 68 74 20 6e   cursors might n
28dc8 6f 74 20 62 65 20 75 73 65 64 2c 20 62 75 74 20  ot be used, but 
28dc9 69 66 20 74 68 65 79 20 61 72 65 20 75 73 65 64  if they are used
28dca 20 74 68 65 79 0a 20 20 2a 2a 20 6e 65 65 64 20   they.  ** need 
28dcb 74 6f 20 6f 63 63 75 72 20 72 69 67 68 74 20 61  to occur right a
28dcc 66 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73  fter the databas
28dcd 65 20 63 75 72 73 6f 72 2e 20 20 53 6f 20 67 6f  e cursor.  So go
28dce 20 61 68 65 61 64 20 61 6e 64 0a 20 20 2a 2a 20   ahead and.  ** 
28dcf 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 20  allocate enough 
28dd0 73 70 61 63 65 2c 20 6a 75 73 74 20 69 6e 20 63  space, just in c
28dd1 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62  ase..  */.  pTab
28dd2 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72 73  List->a[0].iCurs
28dd3 6f 72 20 3d 20 69 43 75 72 20 3d 20 70 50 61 72  or = iCur = pPar
28dd4 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f  se->nTab++;.  fo
28dd5 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
28dd6 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
28dd7 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
28dd8 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b    pParse->nTab++
28dd9 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74  ;.  }..  /* Init
28dda 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 2d  ialize the name-
28ddb 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 6d 65 6d  context */.  mem
28ddc 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
28ddd 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43  eof(sNC));.  sNC
28dde 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
28ddf 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74  ;.  sNC.pSrcList
28de0 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 0a 20 20   = pTabList;..  
28de1 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63  /* Resolve the c
28de2 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61  olumn names in a
28de3 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  ll the expressio
28de4 6e 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6f  ns of the.  ** o
28de5 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  f the UPDATE sta
28de6 74 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 66 69  tement.  Also fi
28de7 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e  nd the column in
28de8 64 65 78 0a 20 20 2a 2a 20 66 6f 72 20 65 61 63  dex.  ** for eac
28de9 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75  h column to be u
28dea 70 64 61 74 65 64 20 69 6e 20 74 68 65 20 70 43  pdated in the pC
28deb 68 61 6e 67 65 73 20 61 72 72 61 79 2e 20 20 46  hanges array.  F
28dec 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 63 6f 6c  or each.  ** col
28ded 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61 74 65  umn to be update
28dee 64 2c 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20  d, make sure we 
28def 68 61 76 65 20 61 75 74 68 6f 72 69 7a 61 74 69  have authorizati
28df0 6f 6e 20 74 6f 20 63 68 61 6e 67 65 0a 20 20 2a  on to change.  *
28df1 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 20  * that column.. 
28df2 20 2a 2f 0a 20 20 63 68 6e 67 52 6f 77 69 64 20   */.  chngRowid 
28df3 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
28df4 69 3c 70 43 68 61 6e 67 65 73 2d 3e 6e 45 78 70  i<pChanges->nExp
28df5 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  r; i++){.    if(
28df6 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
28df7 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
28df8 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45  Changes->a[i].pE
28df9 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  xpr) ){.      go
28dfa 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
28dfb 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  p;.    }.    for
28dfc 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  (j=0; j<pTab->nC
28dfd 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; j++){.      
28dfe 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
28dff 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  mp(pTab->aCol[j]
28e00 2e 7a 4e 61 6d 65 2c 20 70 43 68 61 6e 67 65 73  .zName, pChanges
28e01 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30  ->a[i].zName)==0
28e02 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
28e03 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  j==pTab->iPKey )
28e04 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68 6e 67  {.          chng
28e05 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Rowid = 1;.     
28e06 20 20 20 20 20 70 52 6f 77 69 64 45 78 70 72 20       pRowidExpr 
28e07 3d 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d  = pChanges->a[i]
28e08 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  .pExpr;.        
28e09 7d 0a 20 20 20 20 20 20 20 20 61 58 52 65 66 5b  }.        aXRef[
28e0a 6a 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20  j] = i;.        
28e0b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
28e0c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d     }.    if( j>=
28e0d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
28e0e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49      if( sqlite3I
28e0f 73 52 6f 77 69 64 28 70 43 68 61 6e 67 65 73 2d  sRowid(pChanges-
28e10 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a  >a[i].zName) ){.
28e11 20 20 20 20 20 20 20 20 63 68 6e 67 52 6f 77 69          chngRowi
28e12 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70  d = 1;.        p
28e13 52 6f 77 69 64 45 78 70 72 20 3d 20 70 43 68 61  RowidExpr = pCha
28e14 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  nges->a[i].pExpr
28e15 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
28e16 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
28e17 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
28e18 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20  no such column: 
28e19 25 73 22 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61  %s", pChanges->a
28e1a 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
28e1b 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
28e1c 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d  cleanup;.      }
28e1d 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
28e1e 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
28e1f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a 20  RIZATION.    {. 
28e20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
28e21 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
28e22 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
28e23 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 2c 20   SQLITE_UPDATE, 
28e24 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  pTab->zName,.   
28e25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28e26 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43          pTab->aC
28e27 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 64 62 2d  ol[j].zName, db-
28e28 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
28e29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
28e2a 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20  SQLITE_DENY ){. 
28e2b 20 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61         goto upda
28e2c 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  te_cleanup;.    
28e2d 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d    }else if( rc==
28e2e 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b  SQLITE_IGNORE ){
28e2f 0a 20 20 20 20 20 20 20 20 61 58 52 65 66 5b 6a  .        aXRef[j
28e30 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a  ] = -1;.      }.
28e31 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
28e32 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
28e33 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 61  memory for the a
28e34 72 72 61 79 20 61 52 65 67 49 64 78 5b 5d 2e 20  rray aRegIdx[]. 
28e35 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 65 6e   There is one en
28e36 74 72 79 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  try in the.  ** 
28e37 61 72 72 61 79 20 66 6f 72 20 65 61 63 68 20 69  array for each i
28e38 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  ndex associated 
28e39 77 69 74 68 20 74 61 62 6c 65 20 62 65 69 6e 67  with table being
28e3a 20 75 70 64 61 74 65 64 2e 20 20 46 69 6c 6c 20   updated.  Fill 
28e3b 69 6e 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75  in.  ** the valu
28e3c 65 20 77 69 74 68 20 61 20 72 65 67 69 73 74 65  e with a registe
28e3d 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 69 6e 64  r number for ind
28e3e 69 63 65 73 20 74 68 61 74 20 61 72 65 20 74 6f  ices that are to
28e3f 20 62 65 20 75 73 65 64 0a 20 20 2a 2a 20 61 6e   be used.  ** an
28e40 64 20 77 69 74 68 20 7a 65 72 6f 20 66 6f 72 20  d with zero for 
28e41 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73 2e 0a  unused indices..
28e42 20 20 2a 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d    */.  for(nIdx=
28e43 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
28e44 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
28e45 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49  =pIdx->pNext, nI
28e46 64 78 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 6e 49  dx++){}.  if( nI
28e47 64 78 3e 30 20 29 7b 0a 20 20 20 20 61 52 65 67  dx>0 ){.    aReg
28e48 49 64 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Idx = sqlite3DbM
28e49 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
28e4a 65 6f 66 28 49 6e 64 65 78 2a 29 20 2a 20 6e 49  eof(Index*) * nI
28e4b 64 78 20 29 3b 0a 20 20 20 20 69 66 28 20 61 52  dx );.    if( aR
28e4c 65 67 49 64 78 3d 3d 30 20 29 20 67 6f 74 6f 20  egIdx==0 ) goto 
28e4d 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
28e4e 20 20 7d 0a 20 20 66 6f 72 28 6a 3d 30 2c 20 70    }.  for(j=0, p
28e4f 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
28e50 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
28e51 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a  x->pNext, j++){.
28e52 20 20 20 20 69 6e 74 20 72 65 67 3b 0a 20 20 20      int reg;.   
28e53 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29   if( chngRowid )
28e54 7b 0a 20 20 20 20 20 20 72 65 67 20 3d 20 2b 2b  {.      reg = ++
28e55 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
28e56 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
28e57 65 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f  eg = 0;.      fo
28e58 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e  r(i=0; i<pIdx->n
28e59 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
28e5a 20 20 20 20 20 20 69 66 28 20 61 58 52 65 66 5b        if( aXRef[
28e5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  pIdx->aiColumn[i
28e5c 5d 5d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ]]>=0 ){.       
28e5d 20 20 20 72 65 67 20 3d 20 2b 2b 70 50 61 72 73     reg = ++pPars
28e5e 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20  e->nMem;.       
28e5f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
28e60 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
28e61 7d 0a 20 20 20 20 61 52 65 67 49 64 78 5b 6a 5d  }.    aRegIdx[j]
28e62 20 3d 20 72 65 67 3b 0a 20 20 7d 0a 0a 20 20 2f   = reg;.  }..  /
28e63 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f  * Allocate a blo
28e64 63 6b 20 6f 66 20 72 65 67 69 73 74 65 72 20 75  ck of register u
28e65 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
28e66 20 63 68 61 6e 67 65 20 72 65 63 6f 72 64 0a 20   change record. 
28e67 20 2a 2a 20 73 65 6e 74 20 74 6f 20 73 71 6c 69   ** sent to sqli
28e68 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
28e69 72 61 69 6e 74 43 68 65 63 6b 73 28 29 2e 20 20  raintChecks().  
28e6a 54 68 65 72 65 20 61 72 65 20 65 69 74 68 65 72  There are either
28e6b 0a 20 20 2a 2a 20 6f 6e 65 20 6f 72 20 74 77 6f  .  ** one or two
28e6c 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 68   registers for h
28e6d 6f 6c 64 69 6e 67 20 74 68 65 20 72 6f 77 69 64  olding the rowid
28e6e 2e 20 20 4f 6e 65 20 72 6f 77 69 64 20 72 65 67  .  One rowid reg
28e6f 69 73 74 65 72 0a 20 20 2a 2a 20 69 73 20 75 73  ister.  ** is us
28e70 65 64 20 69 66 20 63 68 6e 67 52 6f 77 69 64 20  ed if chngRowid 
28e71 69 73 20 66 61 6c 73 65 20 61 6e 64 20 74 77 6f  is false and two
28e72 20 61 72 65 20 75 73 65 64 20 69 66 20 63 68 6e   are used if chn
28e73 67 52 6f 77 69 64 20 69 73 0a 20 20 2a 2a 20 74  gRowid is.  ** t
28e74 72 75 65 2e 20 20 46 6f 6c 6c 6f 77 69 6e 67 20  rue.  Following 
28e75 74 68 65 73 65 20 61 72 65 20 70 54 61 62 2d 3e  these are pTab->
28e76 6e 43 6f 6c 20 72 65 67 69 73 74 65 72 20 68 6f  nCol register ho
28e77 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e 0a 20 20 2a  lding column.  *
28e78 2a 20 64 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 72  * data..  */.  r
28e79 65 67 4f 6c 64 52 6f 77 69 64 20 3d 20 72 65 67  egOldRowid = reg
28e7a 4e 65 77 52 6f 77 69 64 20 3d 20 70 50 61 72 73  NewRowid = pPars
28e7b 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 70  e->nMem + 1;.  p
28e7c 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70  Parse->nMem += p
28e7d 54 61 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b 0a 20  Tab->nCol + 1;. 
28e7e 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29   if( chngRowid )
28e7f 7b 0a 20 20 20 20 72 65 67 4e 65 77 52 6f 77 69  {.    regNewRowi
28e80 64 2b 2b 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  d++;.    pParse-
28e81 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 72  >nMem++;.  }.  r
28e82 65 67 44 61 74 61 20 3d 20 72 65 67 4e 65 77 52  egData = regNewR
28e83 6f 77 69 64 2b 31 3b 0a 20 0a 0a 20 20 2f 2a 20  owid+1;. ..  /* 
28e84 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  Begin generating
28e85 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20   code..  */.  v 
28e86 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
28e87 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
28e88 76 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61  v==0 ) goto upda
28e89 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66  te_cleanup;.  if
28e8a 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
28e8b 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 62  ==0 ) sqlite3Vdb
28e8c 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29  eCountChanges(v)
28e8d 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  ;.  sqlite3Begin
28e8e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
28e8f 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a  Parse, 1, iDb);.
28e90 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28e91 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
28e92 45 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74  E.  /* Virtual t
28e93 61 62 6c 65 73 20 6d 75 73 74 20 62 65 20 68 61  ables must be ha
28e94 6e 64 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79  ndled separately
28e95 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74   */.  if( IsVirt
28e96 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
28e97 20 75 70 64 61 74 65 56 69 72 74 75 61 6c 54 61   updateVirtualTa
28e98 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ble(pParse, pTab
28e99 4c 69 73 74 2c 20 70 54 61 62 2c 20 70 43 68 61  List, pTab, pCha
28e9a 6e 67 65 73 2c 20 70 52 6f 77 69 64 45 78 70 72  nges, pRowidExpr
28e9b 2c 20 61 58 52 65 66 2c 0a 20 20 20 20 20 20 20  , aXRef,.       
28e9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28e9d 70 57 68 65 72 65 29 3b 0a 20 20 20 20 70 57 68  pWhere);.    pWh
28e9e 65 72 65 20 3d 20 30 3b 0a 20 20 20 20 70 54 61  ere = 0;.    pTa
28e9f 62 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 67  bList = 0;.    g
28ea0 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
28ea1 75 70 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  up;.  }.#endif..
28ea2 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20 76    /* Start the v
28ea3 69 65 77 20 63 6f 6e 74 65 78 74 0a 20 20 2a 2f  iew context.  */
28ea4 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b  .  if( isView ){
28ea5 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68  .    sqlite3Auth
28ea6 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61 72  ContextPush(pPar
28ea7 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70  se, &sContext, p
28ea8 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d  Tab->zName);.  }
28ea9 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
28eaa 74 68 65 20 63 6f 64 65 20 66 6f 72 20 74 72 69  the code for tri
28eab 67 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  ggers..  */.  if
28eac 28 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74  ( triggers_exist
28ead 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 47 6f 74   ){.    int iGot
28eae 6f 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74  o;..    /* Creat
28eaf 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20  e pseudo-tables 
28eb0 66 6f 72 20 4e 45 57 20 61 6e 64 20 4f 4c 44 0a  for NEW and OLD.
28eb1 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
28eb2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28eb3 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
28eb4 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  , 0, pTab->nCol)
28eb5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28eb6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
28eb7 65 6e 50 73 65 75 64 6f 2c 20 6f 6c 64 49 64 78  enPseudo, oldIdx
28eb8 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
28eb9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28eba 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c  P_SetNumColumns,
28ebb 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b   0, pTab->nCol);
28ebc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
28ebd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
28ebe 6e 50 73 65 75 64 6f 2c 20 6e 65 77 49 64 78 2c  nPseudo, newIdx,
28ebf 20 30 29 3b 0a 0a 20 20 20 20 69 47 6f 74 6f 20   0);..    iGoto 
28ec0 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
28ec1 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
28ec2 30 2c 20 30 29 3b 0a 20 20 20 20 61 64 64 72 20  0, 0);.    addr 
28ec3 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
28ec4 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 69  eLabel(v);.    i
28ec5 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69 67 67  BeginBeforeTrigg
28ec6 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  er = sqlite3Vdbe
28ec7 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
28ec8 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 43      if( sqlite3C
28ec9 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
28eca 61 72 73 65 2c 20 54 4b 5f 55 50 44 41 54 45 2c  arse, TK_UPDATE,
28ecb 20 70 43 68 61 6e 67 65 73 2c 20 54 52 49 47 47   pChanges, TRIGG
28ecc 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c  ER_BEFORE, pTab,
28ecd 0a 20 20 20 20 20 20 20 20 20 20 6e 65 77 49 64  .          newId
28ece 78 2c 20 6f 6c 64 49 64 78 2c 20 6f 6e 45 72 72  x, oldIdx, onErr
28ecf 6f 72 2c 20 61 64 64 72 2c 20 26 6f 6c 64 5f 63  or, addr, &old_c
28ed0 6f 6c 5f 6d 61 73 6b 2c 20 26 6e 65 77 5f 63 6f  ol_mask, &new_co
28ed1 6c 5f 6d 61 73 6b 29 20 29 7b 0a 20 20 20 20 20  l_mask) ){.     
28ed2 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
28ed3 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20  anup;.    }.    
28ed4 69 45 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65  iEndBeforeTrigge
28ed5 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
28ed6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
28ed7 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 42 65  , 0, 0);.    iBe
28ed8 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 72 20  ginAfterTrigger 
28ed9 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
28eda 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
28edb 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f 64 65   if( sqlite3Code
28edc 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
28edd 65 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43  e, TK_UPDATE, pC
28ede 68 61 6e 67 65 73 2c 20 54 52 49 47 47 45 52 5f  hanges, TRIGGER_
28edf 41 46 54 45 52 2c 20 70 54 61 62 2c 20 0a 20 20  AFTER, pTab, .  
28ee0 20 20 20 20 20 20 20 20 6e 65 77 49 64 78 2c 20          newIdx, 
28ee1 6f 6c 64 49 64 78 2c 20 6f 6e 45 72 72 6f 72 2c  oldIdx, onError,
28ee2 20 61 64 64 72 2c 20 26 6f 6c 64 5f 63 6f 6c 5f   addr, &old_col_
28ee3 6d 61 73 6b 2c 20 26 6e 65 77 5f 63 6f 6c 5f 6d  mask, &new_col_m
28ee4 61 73 6b 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  ask) ){.      go
28ee5 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
28ee6 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e  p;.    }.    iEn
28ee7 64 41 66 74 65 72 54 72 69 67 67 65 72 20 3d 20  dAfterTrigger = 
28ee8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28ee9 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
28eea 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
28eeb 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
28eec 69 47 6f 74 6f 29 3b 0a 20 20 7d 0a 0a 20 20 2f  iGoto);.  }..  /
28eed 2a 20 49 66 20 77 65 20 61 72 65 20 74 72 79 69  * If we are tryi
28eee 6e 67 20 74 6f 20 75 70 64 61 74 65 20 61 20 76  ng to update a v
28eef 69 65 77 2c 20 72 65 61 6c 69 7a 65 20 74 68 61  iew, realize tha
28ef0 74 20 76 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a  t view into.  **
28ef1 20 61 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62   a ephemeral tab
28ef2 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65  le..  */.#if !de
28ef3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
28ef4 54 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69  T_VIEW) && !defi
28ef5 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
28ef6 54 52 49 47 47 45 52 29 0a 20 20 69 66 28 20 69  TRIGGER).  if( i
28ef7 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c  sView ){.    sql
28ef8 69 74 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56  ite3MaterializeV
28ef9 69 65 77 28 70 50 61 72 73 65 2c 20 70 54 61 62  iew(pParse, pTab
28efa 2c 20 70 57 68 65 72 65 2c 20 69 43 75 72 29 3b  , pWhere, iCur);
28efb 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
28efc 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f  * Resolve the co
28efd 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c  lumn names in al
28efe 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  l the expression
28eff 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 57 48  s in the.  ** WH
28f00 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f  ERE clause..  */
28f01 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65  .  if( sqlite3Re
28f02 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
28f03 73 4e 43 2c 20 70 57 68 65 72 65 29 20 29 7b 0a  sNC, pWhere) ){.
28f04 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
28f05 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
28f06 2f 2a 20 42 65 67 69 6e 20 74 68 65 20 64 61 74  /* Begin the dat
28f07 61 62 61 73 65 20 73 63 61 6e 0a 20 20 2a 2f 0a  abase scan.  */.
28f08 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28f09 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
28f0a 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b  0, regOldRowid);
28f0b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69  .  pWInfo = sqli
28f0c 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50  te3WhereBegin(pP
28f0d 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
28f0e 70 57 68 65 72 65 2c 20 30 2c 0a 20 20 20 20 20  pWhere, 0,.     
28f0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28f10 20 20 20 20 20 20 20 20 57 48 45 52 45 5f 4f 4e          WHERE_ON
28f11 45 50 41 53 53 5f 44 45 53 49 52 45 44 2c 20 30  EPASS_DESIRED, 0
28f12 29 3b 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 3d  );.  if( pWInfo=
28f13 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65  =0 ) goto update
28f14 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 6f 6b 4f 6e  _cleanup;.  okOn
28f15 65 50 61 73 73 20 3d 20 70 57 49 6e 66 6f 2d 3e  ePass = pWInfo->
28f16 6f 6b 4f 6e 65 50 61 73 73 3b 0a 0a 20 20 2f 2a  okOnePass;..  /*
28f17 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20 72 6f   Remember the ro
28f18 77 69 64 20 6f 66 20 65 76 65 72 79 20 69 74 65  wid of every ite
28f19 6d 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e  m to be updated.
28f1a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
28f1b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 49 73 56  dbeAddOp2(v, IsV
28f1c 69 72 74 75 61 6c 28 70 54 61 62 29 3f 4f 50 5f  irtual(pTab)?OP_
28f1d 56 52 6f 77 69 64 3a 4f 50 5f 52 6f 77 69 64 2c  VRowid:OP_Rowid,
28f1e 20 69 43 75 72 2c 20 72 65 67 4f 6c 64 52 6f 77   iCur, regOldRow
28f1f 69 64 29 3b 0a 20 20 69 66 28 20 21 6f 6b 4f 6e  id);.  if( !okOn
28f20 65 50 61 73 73 20 29 7b 0a 20 20 20 20 72 65 67  ePass ){.    reg
28f21 52 6f 77 53 65 74 20 3d 20 2b 2b 70 50 61 72 73  RowSet = ++pPars
28f22 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
28f23 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28f24 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 2c 20  , OP_RowSetAdd, 
28f25 72 65 67 52 6f 77 53 65 74 2c 20 72 65 67 4f 6c  regRowSet, regOl
28f26 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20  dRowid);.  }..  
28f27 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74 61 62  /* End the datab
28f28 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20  ase scan loop.. 
28f29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 57 68 65   */.  sqlite3Whe
28f2a 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 0a  reEnd(pWInfo);..
28f2b 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
28f2c 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 75 70 64  the count of upd
28f2d 61 74 65 64 20 72 6f 77 73 0a 20 20 2a 2f 0a 20  ated rows.  */. 
28f2e 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26   if( db->flags &
28f2f 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
28f30 73 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72  s && !pParse->tr
28f31 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 72  igStack ){.    r
28f32 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70  egRowCount = ++p
28f33 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
28f34 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28f35 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
28f36 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74  , 0, regRowCount
28f37 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 69  );.  }..  if( !i
28f38 73 56 69 65 77 20 26 26 20 21 49 73 56 69 72 74  sView && !IsVirt
28f39 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
28f3a 20 2f 2a 20 0a 20 20 20 20 2a 2a 20 4f 70 65 6e   /* .    ** Open
28f3b 20 65 76 65 72 79 20 69 6e 64 65 78 20 74 68 61   every index tha
28f3c 74 20 6e 65 65 64 73 20 75 70 64 61 74 69 6e 67  t needs updating
28f3d 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20  .  Note that if 
28f3e 61 6e 79 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78  any.    ** index
28f3f 20 63 6f 75 6c 64 20 70 6f 74 65 6e 74 69 61 6c   could potential
28f40 6c 79 20 69 6e 76 6f 6b 65 20 61 20 52 45 50 4c  ly invoke a REPL
28f41 41 43 45 20 63 6f 6e 66 6c 69 63 74 20 72 65 73  ACE conflict res
28f42 6f 6c 75 74 69 6f 6e 20 0a 20 20 20 20 2a 2a 20  olution .    ** 
28f43 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 77 65 20  action, then we 
28f44 6e 65 65 64 20 74 6f 20 6f 70 65 6e 20 61 6c 6c  need to open all
28f45 20 69 6e 64 69 63 65 73 20 62 65 63 61 75 73 65   indices because
28f46 20 77 65 20 6d 69 67 68 74 20 6e 65 65 64 0a 20   we might need. 
28f47 20 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 6c 65     ** to be dele
28f48 74 69 6e 67 20 73 6f 6d 65 20 72 65 63 6f 72 64  ting some record
28f49 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
28f4a 28 20 21 6f 6b 4f 6e 65 50 61 73 73 20 29 20 73  ( !okOnePass ) s
28f4b 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
28f4c 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44  pParse, iCur, iD
28f4d 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e  b, pTab, OP_Open
28f4e 57 72 69 74 65 29 3b 20 0a 20 20 20 20 69 66 28  Write); .    if(
28f4f 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70   onError==OE_Rep
28f50 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20 6f 70  lace ){.      op
28f51 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 7d  enAll = 1;.    }
28f52 65 6c 73 65 7b 0a 20 20 20 20 20 20 6f 70 65 6e  else{.      open
28f53 41 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 66  All = 0;.      f
28f54 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
28f55 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
28f56 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
28f57 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
28f58 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70  >onError==OE_Rep
28f59 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20  lace ){.        
28f5a 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20    openAll = 1;. 
28f5b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
28f5c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
28f5d 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  }.    }.    for(
28f5e 69 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=0, pIdx=pTab->
28f5f 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
28f60 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
28f61 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
28f62 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49  openAll || aRegI
28f63 64 78 5b 69 5d 3e 30 20 29 7b 0a 20 20 20 20 20  dx[i]>0 ){.     
28f64 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
28f65 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
28f66 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
28f67 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71  Idx);.        sq
28f68 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
28f69 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c  v, OP_OpenWrite,
28f6a 20 69 43 75 72 2b 69 2b 31 2c 20 70 49 64 78 2d   iCur+i+1, pIdx-
28f6b 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20  >tnum, iDb,.    
28f6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28f6d 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20     (char*)pKey, 
28f6e 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
28f6f 46 46 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  FF);.        ass
28f70 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 54 61  ert( pParse->nTa
28f71 62 3e 69 43 75 72 2b 69 2b 31 20 29 3b 0a 20 20  b>iCur+i+1 );.  
28f72 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
28f73 20 20 0a 20 20 2f 2a 20 4a 75 6d 70 20 62 61 63    .  /* Jump bac
28f74 6b 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 20  k to this point 
28f75 69 66 20 61 20 74 72 69 67 67 65 72 20 65 6e 63  if a trigger enc
28f76 6f 75 6e 74 65 72 73 20 61 6e 20 49 47 4e 4f 52  ounters an IGNOR
28f77 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 2a 2f  E constraint. */
28f78 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f  .  if( triggers_
28f79 65 78 69 73 74 20 29 7b 0a 20 20 20 20 73 71 6c  exist ){.    sql
28f7a 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
28f7b 61 62 65 6c 28 76 2c 20 61 64 64 72 29 3b 0a 20  abel(v, addr);. 
28f7c 20 7d 0a 0a 20 20 2f 2a 20 54 6f 70 20 6f 66 20   }..  /* Top of 
28f7d 74 68 65 20 75 70 64 61 74 65 20 6c 6f 6f 70 20  the update loop 
28f7e 2a 2f 0a 20 20 69 66 28 20 6f 6b 4f 6e 65 50 61  */.  if( okOnePa
28f7f 73 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 31  ss ){.    int a1
28f80 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
28f81 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75  dOp1(v, OP_NotNu
28f82 6c 6c 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29  ll, regOldRowid)
28f83 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ;.    addr = sql
28f84 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
28f85 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 20  , OP_Goto);.    
28f86 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
28f87 65 72 65 28 76 2c 20 61 31 29 3b 0a 20 20 7d 65  ere(v, a1);.  }e
28f88 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 20 3d 20  lse{.    addr = 
28f89 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28f8a 33 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 52 65  3(v, OP_RowSetRe
28f8b 61 64 2c 20 72 65 67 52 6f 77 53 65 74 2c 20 30  ad, regRowSet, 0
28f8c 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a  , regOldRowid);.
28f8d 20 20 7d 0a 0a 20 20 69 66 28 20 74 72 69 67 67    }..  if( trigg
28f8e 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20  ers_exist ){.   
28f8f 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 0a 20   int regRowid;. 
28f90 20 20 20 69 6e 74 20 72 65 67 52 6f 77 3b 0a 20     int regRow;. 
28f91 20 20 20 69 6e 74 20 72 65 67 43 6f 6c 73 3b 0a     int regCols;.
28f92 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 63 75 72  .    /* Make cur
28f93 73 6f 72 20 69 43 75 72 20 70 6f 69 6e 74 20 74  sor iCur point t
28f94 6f 20 74 68 65 20 72 65 63 6f 72 64 20 74 68 61  o the record tha
28f95 74 20 69 73 20 62 65 69 6e 67 20 75 70 64 61 74  t is being updat
28f96 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ed..    */.    s
28f97 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
28f98 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
28f99 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20 72 65  , iCur, addr, re
28f9a 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20 20 20  gOldRowid);..   
28f9b 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65   /* Generate the
28f9c 20 4f 4c 44 20 74 61 62 6c 65 0a 20 20 20 20 2a   OLD table.    *
28f9d 2f 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d  /.    regRowid =
28f9e 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
28f9f 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
28fa0 72 65 67 52 6f 77 20 3d 20 73 71 6c 69 74 65 33  regRow = sqlite3
28fa1 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
28fa2 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
28fa3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28fa4 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 72 65 67  Rowid, iCur, reg
28fa5 52 6f 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20  Rowid);.    if( 
28fa6 21 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 29 7b  !old_col_mask ){
28fa7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
28fa8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
28fa9 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 29 3b  ull, 0, regRow);
28faa 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
28fab 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28fac 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74  Op2(v, OP_RowDat
28fad 61 2c 20 69 43 75 72 2c 20 72 65 67 52 6f 77 29  a, iCur, regRow)
28fae 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
28faf 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
28fb0 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6f 6c 64 49   OP_Insert, oldI
28fb1 64 78 2c 20 72 65 67 52 6f 77 2c 20 72 65 67 52  dx, regRow, regR
28fb2 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 47  owid);..    /* G
28fb3 65 6e 65 72 61 74 65 20 74 68 65 20 4e 45 57 20  enerate the NEW 
28fb4 74 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20  table.    */.   
28fb5 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29   if( chngRowid )
28fb6 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
28fb7 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28  xprCodeAndCache(
28fb8 70 50 61 72 73 65 2c 20 70 52 6f 77 69 64 45 78  pParse, pRowidEx
28fb9 70 72 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  pr, regRowid);. 
28fba 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28fbb 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73  AddOp1(v, OP_Mus
28fbc 74 42 65 49 6e 74 2c 20 72 65 67 52 6f 77 69 64  tBeInt, regRowid
28fbd 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
28fbe 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28fbf 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
28fc0 64 2c 20 69 43 75 72 2c 20 72 65 67 52 6f 77 69  d, iCur, regRowi
28fc1 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  d);.    }.    re
28fc2 67 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 47  gCols = sqlite3G
28fc3 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
28fc4 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b  se, pTab->nCol);
28fc5 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
28fc6 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
28fc7 7b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 70  {.      if( i==p
28fc8 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
28fc9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28fca 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
28fcb 6c 6c 2c 20 30 2c 20 72 65 67 43 6f 6c 73 2b 69  ll, 0, regCols+i
28fcc 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69  );.        conti
28fcd 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nue;.      }.   
28fce 20 20 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b     j = aXRef[i];
28fcf 0a 20 20 20 20 20 20 69 66 28 20 6e 65 77 5f 63  .      if( new_c
28fd0 6f 6c 5f 6d 61 73 6b 26 28 28 75 33 32 29 31 3c  ol_mask&((u32)1<
28fd1 3c 69 29 20 7c 7c 20 6e 65 77 5f 63 6f 6c 5f 6d  <i) || new_col_m
28fd2 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 66 20  ask==0xffffffff 
28fd3 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a  ){.        if( j
28fd4 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
28fd5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28fd6 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
28fd7 69 43 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c 73  iCur, i, regCols
28fd8 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +i);.          s
28fd9 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
28fda 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 29 3b  ult(v, pTab, i);
28fdb 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
28fdc 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28fdd 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68  3ExprCodeAndCach
28fde 65 28 70 50 61 72 73 65 2c 20 70 43 68 61 6e 67  e(pParse, pChang
28fdf 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20  es->a[j].pExpr, 
28fe0 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 20  regCols+i);.    
28fe1 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
28fe2 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
28fe3 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28fe4 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 43  OP_Null, 0, regC
28fe5 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a  ols+i);.      }.
28fe6 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
28fe7 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
28fe8 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
28fe9 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f  gCols, pTab->nCo
28fea 6c 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 20 20  l, regRow);.    
28feb 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20  if( !isView ){. 
28fec 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c       sqlite3Tabl
28fed 65 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20  eAffinityStr(v, 
28fee 70 54 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c  pTab);.      sql
28fef 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
28ff0 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
28ff1 73 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54 61  se, regCols, pTa
28ff2 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a  b->nCol);.    }.
28ff3 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
28ff4 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
28ff5 73 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54 61  se, regCols, pTa
28ff6 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 2f 2a  b->nCol);.    /*
28ff7 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
28ff8 72 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  r ) goto update_
28ff9 63 6c 65 61 6e 75 70 3b 20 2a 2f 0a 20 20 20 20  cleanup; */.    
28ffa 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28ffb 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
28ffc 6e 65 77 49 64 78 2c 20 72 65 67 52 6f 77 2c 20  newIdx, regRow, 
28ffd 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  regRowid);.    s
28ffe 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
28fff 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
29000 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69  Rowid);.    sqli
29001 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
29002 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77  g(pParse, regRow
29003 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
29004 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29005 47 6f 74 6f 2c 20 30 2c 20 69 42 65 67 69 6e 42  Goto, 0, iBeginB
29006 65 66 6f 72 65 54 72 69 67 67 65 72 29 3b 0a 20  eforeTrigger);. 
29007 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
29008 6d 70 48 65 72 65 28 76 2c 20 69 45 6e 64 42 65  mpHere(v, iEndBe
29009 66 6f 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20  foreTrigger);.  
2900a 7d 0a 0a 20 20 69 66 28 20 21 69 73 56 69 65 77  }..  if( !isView
2900b 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70   && !IsVirtual(p
2900c 54 61 62 29 20 29 7b 0a 20 20 20 20 2f 2a 20 4c  Tab) ){.    /* L
2900d 6f 6f 70 20 6f 76 65 72 20 65 76 65 72 79 20 72  oop over every r
2900e 65 63 6f 72 64 20 74 68 61 74 20 6e 65 65 64 73  ecord that needs
2900f 20 75 70 64 61 74 69 6e 67 2e 20 20 57 65 20 68   updating.  We h
29010 61 76 65 20 74 6f 20 6c 6f 61 64 0a 20 20 20 20  ave to load.    
29011 2a 2a 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20  ** the old data 
29012 66 6f 72 20 65 61 63 68 20 72 65 63 6f 72 64 20  for each record 
29013 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 62 65  to be updated be
29014 63 61 75 73 65 20 73 6f 6d 65 20 63 6f 6c 75 6d  cause some colum
29015 6e 73 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20  ns.    ** might 
29016 6e 6f 74 20 63 68 61 6e 67 65 20 61 6e 64 20 77  not change and w
29017 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63  e will need to c
29018 6f 70 79 20 74 68 65 20 6f 6c 64 20 76 61 6c 75  opy the old valu
29019 65 2e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20  e..    ** Also, 
2901a 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20  the old data is 
2901b 6e 65 65 64 65 64 20 74 6f 20 64 65 6c 65 74 65  needed to delete
2901c 20 74 68 65 20 6f 6c 64 20 69 6e 64 65 78 20 65   the old index e
2901d 6e 74 72 69 65 73 2e 0a 20 20 20 20 2a 2a 20 53  ntries..    ** S
2901e 6f 20 6d 61 6b 65 20 74 68 65 20 63 75 72 73 6f  o make the curso
2901f 72 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6f  r point at the o
29020 6c 64 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a  ld record..    *
29021 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
29022 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
29023 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61  tExists, iCur, a
29024 64 64 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  ddr, regOldRowid
29025 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
29026 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
29027 77 69 6c 6c 20 63 68 61 6e 67 65 2c 20 70 75 73  will change, pus
29028 68 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  h the record num
29029 62 65 72 20 61 73 20 69 74 0a 20 20 20 20 2a 2a  ber as it.    **
2902a 20 77 69 6c 6c 20 62 65 20 61 66 74 65 72 20 74   will be after t
2902b 68 65 20 75 70 64 61 74 65 2e 20 28 54 68 65 20  he update. (The 
2902c 6f 6c 64 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  old record numbe
2902d 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 20  r is currently. 
2902e 20 20 20 2a 2a 20 6f 6e 20 74 6f 70 20 6f 66 20     ** on top of 
2902f 74 68 65 20 73 74 61 63 6b 2e 29 0a 20 20 20 20  the stack.).    
29030 2a 2f 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52  */.    if( chngR
29031 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71  owid ){.      sq
29032 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
29033 61 72 73 65 2c 20 70 52 6f 77 69 64 45 78 70 72  arse, pRowidExpr
29034 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a  , regNewRowid);.
29035 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29036 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75  eAddOp1(v, OP_Mu
29037 73 74 42 65 49 6e 74 2c 20 72 65 67 4e 65 77 52  stBeInt, regNewR
29038 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  owid);.    }..  
29039 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 6e 65 77    /* Compute new
2903a 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 72   data for this r
2903b 65 63 6f 72 64 2e 20 20 0a 20 20 20 20 2a 2f 0a  ecord.  .    */.
2903c 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2903d 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
2903e 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 70 54  .      if( i==pT
2903f 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
29040 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29041 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
29042 6c 2c 20 30 2c 20 72 65 67 44 61 74 61 2b 69 29  l, 0, regData+i)
29043 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e  ;.        contin
29044 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ue;.      }.    
29045 20 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b 0a    j = aXRef[i];.
29046 20 20 20 20 20 20 69 66 28 20 6a 3c 30 20 29 7b        if( j<0 ){
29047 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29048 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
29049 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69  _Column, iCur, i
2904a 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20  , regData+i);.  
2904b 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c        sqlite3Col
2904c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54  umnDefault(v, pT
2904d 61 62 2c 20 69 29 3b 0a 20 20 20 20 20 20 7d 65  ab, i);.      }e
2904e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
2904f 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
29050 72 73 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61  rse, pChanges->a
29051 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 44 61  [j].pExpr, regDa
29052 74 61 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ta+i);.      }. 
29053 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20     }..    /* Do 
29054 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b  constraint check
29055 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  s.    */.    sql
29056 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73  ite3GenerateCons
29057 74 72 61 69 6e 74 43 68 65 63 6b 73 28 70 50 61  traintChecks(pPa
29058 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c  rse, pTab, iCur,
29059 20 72 65 67 4e 65 77 52 6f 77 69 64 2c 0a 20 20   regNewRowid,.  
2905a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2905b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2905c 20 20 61 52 65 67 49 64 78 2c 20 63 68 6e 67 52    aRegIdx, chngR
2905d 6f 77 69 64 2c 20 31 2c 0a 20 20 20 20 20 20 20  owid, 1,.       
2905e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2905f 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e 45               onE
29060 72 72 6f 72 2c 20 61 64 64 72 29 3b 0a 0a 20 20  rror, addr);..  
29061 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
29062 6f 6c 64 20 69 6e 64 69 63 65 73 20 66 6f 72 20  old indices for 
29063 74 68 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f  the current reco
29064 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6a  rd..    */.    j
29065 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
29066 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
29067 78 69 73 74 73 2c 20 69 43 75 72 2c 20 30 2c 20  xists, iCur, 0, 
29068 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20  regOldRowid);.  
29069 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74    sqlite3Generat
2906a 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28  eRowIndexDelete(
2906b 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43  pParse, pTab, iC
2906c 75 72 2c 20 61 52 65 67 49 64 78 29 3b 0a 0a 20  ur, aRegIdx);.. 
2906d 20 20 20 2f 2a 20 49 66 20 63 68 61 6e 67 69 6e     /* If changin
2906e 67 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  g the record num
2906f 62 65 72 2c 20 64 65 6c 65 74 65 20 74 68 65 20  ber, delete the 
29070 6f 6c 64 20 72 65 63 6f 72 64 2e 0a 20 20 20 20  old record..    
29071 2a 2f 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52  */.    if( chngR
29072 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71  owid ){.      sq
29073 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29074 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43  v, OP_Delete, iC
29075 75 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  ur, 0);.    }.  
29076 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
29077 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20  pHere(v, j1);.. 
29078 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65     /* Create the
29079 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69   new index entri
2907a 65 73 20 61 6e 64 20 74 68 65 20 6e 65 77 20 72  es and the new r
2907b 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  ecord..    */.  
2907c 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74    sqlite3Complet
2907d 65 49 6e 73 65 72 74 69 6f 6e 28 70 50 61 72 73  eInsertion(pPars
2907e 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 72  e, pTab, iCur, r
2907f 65 67 4e 65 77 52 6f 77 69 64 2c 20 0a 20 20 20  egNewRowid, .   
29080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29081 20 20 20 20 20 20 20 20 20 20 61 52 65 67 49 64            aRegId
29082 78 2c 20 31 2c 20 2d 31 2c 20 30 29 3b 0a 20 20  x, 1, -1, 0);.  
29083 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e  }..  /* Incremen
29084 74 20 74 68 65 20 72 6f 77 20 63 6f 75 6e 74 65  t the row counte
29085 72 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  r .  */.  if( db
29086 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
29087 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 21 70  _CountRows && !p
29088 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
29089 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2908a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
2908b 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f 75  ddImm, regRowCou
2908c 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f  nt, 1);.  }..  /
2908d 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 74  * If there are t
2908e 72 69 67 67 65 72 73 2c 20 63 6c 6f 73 65 20 61  riggers, close a
2908f 6c 6c 20 74 68 65 20 63 75 72 73 6f 72 73 20 61  ll the cursors a
29090 66 74 65 72 20 65 61 63 68 20 69 74 65 72 61 74  fter each iterat
29091 69 6f 6e 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68  ion.  ** through
29092 20 74 68 65 20 6c 6f 6f 70 2e 20 20 54 68 65 20   the loop.  The 
29093 66 69 72 65 20 74 68 65 20 61 66 74 65 72 20 74  fire the after t
29094 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20  riggers..  */.  
29095 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69  if( triggers_exi
29096 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  st ){.    sqlite
29097 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29098 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 65 67 69  P_Goto, 0, iBegi
29099 6e 41 66 74 65 72 54 72 69 67 67 65 72 29 3b 0a  nAfterTrigger);.
2909a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
2909b 75 6d 70 48 65 72 65 28 76 2c 20 69 45 6e 64 41  umpHere(v, iEndA
2909c 66 74 65 72 54 72 69 67 67 65 72 29 3b 0a 20 20  fterTrigger);.  
2909d 7d 0a 0a 20 20 2f 2a 20 52 65 70 65 61 74 20 74  }..  /* Repeat t
2909e 68 65 20 61 62 6f 76 65 20 77 69 74 68 20 74 68  he above with th
2909f 65 20 6e 65 78 74 20 72 65 63 6f 72 64 20 74 6f  e next record to
290a0 20 62 65 20 75 70 64 61 74 65 64 2c 20 75 6e 74   be updated, unt
290a1 69 6c 0a 20 20 2a 2a 20 61 6c 6c 20 72 65 63 6f  il.  ** all reco
290a2 72 64 20 73 65 6c 65 63 74 65 64 20 62 79 20 74  rd selected by t
290a3 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
290a4 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 65  have been update
290a5 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  d..  */.  sqlite
290a6 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
290a7 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29  P_Goto, 0, addr)
290a8 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  ;.  sqlite3VdbeJ
290a9 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
290aa 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c  ;..  /* Close al
290ab 6c 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 66 6f  l tables */.  fo
290ac 72 28 69 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  r(i=0, pIdx=pTab
290ad 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
290ae 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
290af 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , i++){.    if( 
290b0 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49  openAll || aRegI
290b1 64 78 5b 69 5d 3e 30 20 29 7b 0a 20 20 20 20 20  dx[i]>0 ){.     
290b2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
290b3 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
290b4 69 43 75 72 2b 69 2b 31 2c 20 30 29 3b 0a 20 20  iCur+i+1, 0);.  
290b5 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
290b6 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
290b7 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30  P_Close, iCur, 0
290b8 29 3b 0a 20 20 69 66 28 20 74 72 69 67 67 65 72  );.  if( trigger
290b9 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 73  s_exist ){.    s
290ba 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
290bb 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 6e 65  (v, OP_Close, ne
290bc 77 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 73 71  wIdx, 0);.    sq
290bd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
290be 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 6f 6c 64  v, OP_Close, old
290bf 49 64 78 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  Idx, 0);.  }..  
290c0 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74  /*.  ** Return t
290c1 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
290c2 73 20 74 68 61 74 20 77 65 72 65 20 63 68 61 6e  s that were chan
290c3 67 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75  ged. If this rou
290c4 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65  tine is .  ** ge
290c5 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65  nerating code be
290c6 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20  cause of a call 
290c7 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64  to sqlite3Nested
290c8 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a  Parse(), do not.
290c9 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20    ** invoke the 
290ca 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
290cb 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  n..  */.  if( db
290cc 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
290cd 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 21 70  _CountRows && !p
290ce 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
290cf 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74   && pParse->nest
290d0 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ed==0 ){.    sql
290d1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
290d2 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
290d3 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b  regRowCount, 1);
290d4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
290d5 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
290d6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
290d7 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
290d8 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
290d9 22 72 6f 77 73 20 75 70 64 61 74 65 64 22 2c 20  "rows updated", 
290da 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
290db 20 20 7d 0a 0a 75 70 64 61 74 65 5f 63 6c 65 61    }..update_clea
290dc 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 41 75  nup:.  sqlite3Au
290dd 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26 73 43  thContextPop(&sC
290de 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74  ontext);.  sqlit
290df 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 52 65  e3DbFree(db, aRe
290e0 67 49 64 78 29 3b 0a 20 20 73 71 6c 69 74 65 33  gIdx);.  sqlite3
290e1 44 62 46 72 65 65 28 64 62 2c 20 61 58 52 65 66  DbFree(db, aXRef
290e2 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  );.  sqlite3SrcL
290e3 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  istDelete(db, pT
290e4 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74  abList);.  sqlit
290e5 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
290e6 28 64 62 2c 20 70 43 68 61 6e 67 65 73 29 3b 0a  (db, pChanges);.
290e7 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
290e8 65 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b  ete(db, pWhere);
290e9 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23 69  .  return;.}..#i
290ea 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
290eb 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
290ec 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
290ed 64 65 20 66 6f 72 20 61 6e 20 55 50 44 41 54 45  de for an UPDATE
290ee 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61   of a virtual ta
290ef 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ble..**.** The s
290f0 74 72 61 74 65 67 79 20 69 73 20 74 68 61 74 20  trategy is that 
290f1 77 65 20 63 72 65 61 74 65 20 61 6e 20 65 70 68  we create an eph
290f2 65 6d 65 72 69 61 6c 20 74 61 62 6c 65 20 74 68  emerial table th
290f3 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 66  at contains.** f
290f4 6f 72 20 65 61 63 68 20 72 6f 77 20 74 6f 20 62  or each row to b
290f5 65 20 63 68 61 6e 67 65 64 3a 0a 2a 2a 0a 2a 2a  e changed:.**.**
290f6 20 20 20 28 41 29 20 20 54 68 65 20 6f 72 69 67     (A)  The orig
290f7 69 6e 61 6c 20 72 6f 77 69 64 20 6f 66 20 74 68  inal rowid of th
290f8 61 74 20 72 6f 77 2e 0a 2a 2a 20 20 20 28 42 29  at row..**   (B)
290f9 20 20 54 68 65 20 72 65 76 69 73 65 64 20 72 6f    The revised ro
290fa 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77 2e  wid for the row.
290fb 20 28 6e 6f 74 65 31 29 0a 2a 2a 20 20 20 28 43   (note1).**   (C
290fc 29 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f  )  The content o
290fd 66 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69  f every column i
290fe 6e 20 74 68 65 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a  n the row..**.**
290ff 20 54 68 65 6e 20 77 65 20 6c 6f 6f 70 20 6f 76   Then we loop ov
29100 65 72 20 74 68 69 73 20 65 70 68 65 6d 65 72 61  er this ephemera
29101 6c 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20  l table and for 
29102 65 61 63 68 20 72 6f 77 20 69 6e 0a 2a 2a 20 74  each row in.** t
29103 68 65 20 65 70 68 65 72 6d 65 72 61 6c 20 74 61  he ephermeral ta
29104 62 6c 65 20 63 61 6c 6c 20 56 55 70 64 61 74 65  ble call VUpdate
29105 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 66 69 6e  ..**.** When fin
29106 69 73 68 65 64 2c 20 64 72 6f 70 20 74 68 65 20  ished, drop the 
29107 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e  ephemeral table.
29108 0a 2a 2a 0a 2a 2a 20 28 6e 6f 74 65 31 29 20 41  .**.** (note1) A
29109 63 74 75 61 6c 6c 79 2c 20 69 66 20 77 65 20 6b  ctually, if we k
2910a 6e 6f 77 20 69 6e 20 61 64 76 61 6e 63 65 20 74  now in advance t
2910b 68 61 74 20 28 41 29 20 69 73 20 61 6c 77 61 79  hat (A) is alway
2910c 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 61 73  s the same.** as
2910d 20 28 42 29 20 77 65 20 6f 6e 6c 79 20 73 74 6f   (B) we only sto
2910e 72 65 20 28 41 29 2c 20 74 68 65 6e 20 64 75 70  re (A), then dup
2910f 6c 69 63 61 74 65 20 28 41 29 20 77 68 65 6e 20  licate (A) when 
29110 70 75 6c 6c 69 6e 67 0a 2a 2a 20 69 74 20 6f 75  pulling.** it ou
29111 74 20 6f 66 20 74 68 65 20 65 70 68 65 6d 65 72  t of the ephemer
29112 61 6c 20 74 61 62 6c 65 20 62 65 66 6f 72 65 20  al table before 
29113 63 61 6c 6c 69 6e 67 20 56 55 70 64 61 74 65 2e  calling VUpdate.
29114 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
29115 75 70 64 61 74 65 56 69 72 74 75 61 6c 54 61 62  updateVirtualTab
29116 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  le(.  Parse *pPa
29117 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  rse,       /* Th
29118 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
29119 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
2911a 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a 20 54  pSrc,       /* T
2911b 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2911c 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65 64 20   to be modified 
2911d 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2911e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2911f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
29120 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43  /.  ExprList *pC
29121 68 61 6e 67 65 73 2c 20 20 2f 2a 20 54 68 65 20  hanges,  /* The 
29122 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67  columns to chang
29123 65 20 69 6e 20 74 68 65 20 55 50 44 41 54 45 20  e in the UPDATE 
29124 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45  statement */.  E
29125 78 70 72 20 2a 70 52 6f 77 69 64 2c 20 20 20 20  xpr *pRowid,    
29126 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
29127 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 6d 70  n used to recomp
29128 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 2a 2f  ute the rowid */
29129 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 2c 20 20  .  int *aXRef,  
2912a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70 69          /* Mappi
2912b 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20  ng from columns 
2912c 6f 66 20 70 54 61 62 20 74 6f 20 65 6e 74 72 69  of pTab to entri
2912d 65 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20 2a  es in pChanges *
2912e 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
2912f 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52           /* WHER
29130 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  E clause of the 
29131 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
29132 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
29133 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
29134 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61  ;  /* Virtual ma
29135 63 68 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73  chine under cons
29136 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  truction */.  Ex
29137 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d  prList *pEList =
29138 20 30 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 72   0;     /* The r
29139 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
2913a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2913b 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
2913c 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 20 20  Select = 0;     
2913d 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
2913e 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78  tatement */.  Ex
2913f 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20  pr *pExpr;      
29140 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f          /* Tempo
29141 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  rary expression 
29142 2a 2f 0a 20 20 69 6e 74 20 65 70 68 65 6d 54 61  */.  int ephemTa
29143 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
29144 2a 20 54 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20  * Table holding 
29145 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
29146 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e  e SELECT */.  in
29147 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
29148 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
29149 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
2914a 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 20 20   addr;          
2914b 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2914c 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70  s of top of loop
2914d 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 3b 20   */.  int iReg; 
2914e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2914f 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74 65  /* First registe
29150 72 20 69 6e 20 73 65 74 20 70 61 73 73 65 64 20  r in set passed 
29151 74 6f 20 4f 50 5f 56 55 70 64 61 74 65 20 2a 2f  to OP_VUpdate */
29152 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
29153 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20   pParse->db; /* 
29154 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
29155 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
29156 68 61 72 20 2a 70 56 74 61 62 20 3d 20 28 63 6f  har *pVtab = (co
29157 6e 73 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e  nst char*)pTab->
29158 70 56 74 61 62 3b 0a 20 20 53 65 6c 65 63 74 44  pVtab;.  SelectD
29159 65 73 74 20 64 65 73 74 3b 0a 0a 20 20 2f 2a 20  est dest;..  /* 
2915a 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 53 45  Construct the SE
2915b 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
2915c 68 61 74 20 77 69 6c 6c 20 66 69 6e 64 20 74 68  hat will find th
2915d 65 20 6e 65 77 20 76 61 6c 75 65 73 20 66 6f 72  e new values for
2915e 0a 20 20 2a 2a 20 61 6c 6c 20 75 70 64 61 74 65  .  ** all update
2915f 64 20 72 6f 77 73 2e 20 0a 20 20 2a 2f 0a 20 20  d rows. .  */.  
29160 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  pEList = sqlite3
29161 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
29162 50 61 72 73 65 2c 20 30 2c 20 0a 20 20 20 20 20  Parse, 0, .     
29163 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29164 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
29165 74 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28  te3CreateIdExpr(
29166 70 50 61 72 73 65 2c 20 22 5f 72 6f 77 69 64 5f  pParse, "_rowid_
29167 22 29 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 52  "), 0);.  if( pR
29168 6f 77 69 64 20 29 7b 0a 20 20 20 20 70 45 4c 69  owid ){.    pELi
29169 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2916a 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2916b 65 2c 20 70 45 4c 69 73 74 2c 0a 20 20 20 20 20  e, pEList,.     
2916c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2916d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2916e 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2916f 20 70 52 6f 77 69 64 29 2c 20 30 29 3b 0a 20 20   pRowid), 0);.  
29170 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  }.  assert( pTab
29171 2d 3e 69 50 4b 65 79 3c 30 20 29 3b 0a 20 20 66  ->iPKey<0 );.  f
29172 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
29173 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
29174 69 66 28 20 61 58 52 65 66 5b 69 5d 3e 3d 30 20  if( aXRef[i]>=0 
29175 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 20 3d  ){.      pExpr =
29176 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
29177 64 62 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b  db, pChanges->a[
29178 61 58 52 65 66 5b 69 5d 5d 2e 70 45 78 70 72 29  aXRef[i]].pExpr)
29179 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2917a 20 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74     pExpr = sqlit
2917b 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28 70  e3CreateIdExpr(p
2917c 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f  Parse, pTab->aCo
2917d 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  l[i].zName);.   
2917e 20 7d 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20   }.    pEList = 
2917f 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
29180 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 45  ppend(pParse, pE
29181 4c 69 73 74 2c 20 70 45 78 70 72 2c 20 30 29 3b  List, pExpr, 0);
29182 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 20 3d  .  }.  pSelect =
29183 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
29184 77 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  w(pParse, pEList
29185 2c 20 70 53 72 63 2c 20 70 57 68 65 72 65 2c 20  , pSrc, pWhere, 
29186 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
29187 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 72 65 61 74  );.  .  /* Creat
29188 65 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20  e the ephemeral 
29189 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68  table into which
2918a 20 74 68 65 20 75 70 64 61 74 65 20 72 65 73 75   the update resu
2918b 6c 74 73 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65  lts will.  ** be
2918c 20 73 74 6f 72 65 64 2e 0a 20 20 2a 2f 0a 20 20   stored..  */.  
2918d 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20 65  assert( v );.  e
2918e 70 68 65 6d 54 61 62 20 3d 20 70 50 61 72 73 65  phemTab = pParse
2918f 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 73 71 6c 69  ->nTab++;.  sqli
29190 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29191 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
29192 6c 2c 20 65 70 68 65 6d 54 61 62 2c 20 70 54 61  l, ephemTab, pTa
29193 62 2d 3e 6e 43 6f 6c 2b 31 2b 28 70 52 6f 77 69  b->nCol+1+(pRowi
29194 64 21 3d 30 29 29 3b 0a 0a 20 20 2f 2a 20 66 69  d!=0));..  /* fi
29195 6c 6c 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c  ll the ephemeral
29196 20 74 61 62 6c 65 20 0a 20 20 2a 2f 0a 20 20 73   table .  */.  s
29197 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
29198 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f  Init(&dest, SRT_
29199 54 61 62 6c 65 2c 20 65 70 68 65 6d 54 61 62 29  Table, ephemTab)
2919a 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2919b 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  t(pParse, pSelec
2919c 74 2c 20 26 64 65 73 74 29 3b 0a 0a 20 20 2f 2a  t, &dest);..  /*
2919d 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2919e 6f 20 73 63 61 6e 20 74 68 65 20 65 70 68 65 6d  o scan the ephem
2919f 65 72 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 63  eral table and c
291a0 61 6c 6c 20 56 55 70 64 61 74 65 2e 20 2a 2f 0a  all VUpdate. */.
291a1 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73    iReg = ++pPars
291a2 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 70 50 61 72 73  e->nMem;.  pPars
291a3 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d  e->nMem += pTab-
291a4 3e 6e 43 6f 6c 2b 31 3b 0a 20 20 73 71 6c 69 74  >nCol+1;.  sqlit
291a5 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
291a6 4f 50 5f 52 65 77 69 6e 64 2c 20 65 70 68 65 6d  OP_Rewind, ephem
291a7 54 61 62 2c 20 30 29 3b 0a 20 20 61 64 64 72 20  Tab, 0);.  addr 
291a8 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
291a9 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 73  rentAddr(v);.  s
291aa 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
291ab 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20  (v, OP_Column,  
291ac 65 70 68 65 6d 54 61 62 2c 20 30 2c 20 69 52 65  ephemTab, 0, iRe
291ad 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  g);.  sqlite3Vdb
291ae 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
291af 6c 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20  lumn, ephemTab, 
291b0 28 70 52 6f 77 69 64 3f 31 3a 30 29 2c 20 69 52  (pRowid?1:0), iR
291b1 65 67 2b 31 29 3b 0a 20 20 66 6f 72 28 69 3d 30  eg+1);.  for(i=0
291b2 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
291b3 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
291b4 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
291b5 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68 65 6d 54  P_Column, ephemT
291b6 61 62 2c 20 69 2b 31 2b 28 70 52 6f 77 69 64 21  ab, i+1+(pRowid!
291b7 3d 30 29 2c 20 69 52 65 67 2b 32 2b 69 29 3b 0a  =0), iReg+2+i);.
291b8 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 74 61    }.  sqlite3Vta
291b9 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50  bMakeWritable(pP
291ba 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 73  arse, pTab);.  s
291bb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
291bc 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20  (v, OP_VUpdate, 
291bd 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c  0, pTab->nCol+2,
291be 20 69 52 65 67 2c 20 70 56 74 61 62 2c 20 50 34   iReg, pVtab, P4
291bf 5f 56 54 41 42 29 3b 0a 20 20 73 71 6c 69 74 65  _VTAB);.  sqlite
291c0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
291c1 50 5f 4e 65 78 74 2c 20 65 70 68 65 6d 54 61 62  P_Next, ephemTab
291c2 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74  , addr);.  sqlit
291c3 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
291c4 2c 20 61 64 64 72 2d 31 29 3b 0a 20 20 73 71 6c  , addr-1);.  sql
291c5 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
291c6 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 65 70 68 65  , OP_Close, ephe
291c7 6d 54 61 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20  mTab, 0);..  /* 
291c8 43 6c 65 61 6e 75 70 20 2a 2f 0a 20 20 73 71 6c  Cleanup */.  sql
291c9 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
291ca 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 20 20  (db, pSelect);  
291cb 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
291cc 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
291cd 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 20 4d 61 6b  TABLE */../* Mak
291ce 65 20 73 75 72 65 20 22 69 73 56 69 65 77 22 20  e sure "isView" 
291cf 67 65 74 73 20 75 6e 64 65 66 69 6e 65 64 20 69  gets undefined i
291d0 6e 20 63 61 73 65 20 74 68 69 73 20 66 69 6c 65  n case this file
291d1 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f 66   becomes part of
291d2 0a 2a 2a 20 74 68 65 20 61 6d 61 6c 67 61 6d 61  .** the amalgama
291d3 74 69 6f 6e 20 2d 20 73 6f 20 74 68 61 74 20 73  tion - so that s
291d4 75 62 73 65 71 75 65 6e 74 20 66 69 6c 65 73 20  ubsequent files 
291d5 64 6f 20 6e 6f 74 20 73 65 65 20 69 73 56 69 65  do not see isVie
291d6 77 20 61 73 20 61 0a 2a 2a 20 6d 61 63 72 6f 2e  w as a.** macro.
291d7 20 2a 2f 0a 23 75 6e 64 65 66 20 69 73 56 69 65   */.#undef isVie
291d8 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  w../************
291d9 2a 2a 20 45 6e 64 20 6f 66 20 75 70 64 61 74 65  ** End of update
291da 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
291db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291dd 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
291de 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 61  ** Begin file va
291df 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  cuum.c *********
291e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291e2 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41 70  */./*.** 2003 Ap
291e3 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ril 6.**.** The 
291e4 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
291e5 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
291e6 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
291e7 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
291e8 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
291e9 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
291ea 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
291eb 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
291ec 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
291ed 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
291ee 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
291ef 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
291f0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
291f1 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
291f2 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
291f3 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
291f4 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
291f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291f9 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
291fa 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
291fb 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
291fc 74 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d  t the VACUUM com
291fd 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74  mand..**.** Most
291fe 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20   of the code in 
291ff 74 68 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65  this file may be
29200 20 6f 6d 69 74 74 65 64 20 62 79 20 64 65 66 69   omitted by defi
29201 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49  ning the.** SQLI
29202 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 6d  TE_OMIT_VACUUM m
29203 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  acro..**.** $Id:
29204 20 76 61 63 75 75 6d 2e 63 2c 76 20 31 2e 38 34   vacuum.c,v 1.84
29205 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39 3a 31   2008/11/17 19:1
29206 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39 37 37  8:55 danielk1977
29207 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21   Exp $.*/..#if !
29208 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
29209 4d 49 54 5f 56 41 43 55 55 4d 29 20 26 26 20 21  MIT_VACUUM) && !
2920a 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2920b 4d 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a 0a 2a  MIT_ATTACH)./*.*
2920c 2a 20 45 78 65 63 75 74 65 20 7a 53 71 6c 20 6f  * Execute zSql o
2920d 6e 20 64 61 74 61 62 61 73 65 20 64 62 2e 20 52  n database db. R
2920e 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
2920f 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ode..*/.static i
29210 6e 74 20 65 78 65 63 53 71 6c 28 73 71 6c 69 74  nt execSql(sqlit
29211 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
29212 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c  ar *zSql){.  sql
29213 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
29214 3b 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b  ;.  if( !zSql ){
29215 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
29216 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
29217 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
29218 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64  qlite3_prepare(d
29219 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53  b, zSql, -1, &pS
2921a 74 6d 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 72  tmt, 0) ){.    r
2921b 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 65 72  eturn sqlite3_er
2921c 72 63 6f 64 65 28 64 62 29 3b 0a 20 20 7d 0a 20  rcode(db);.  }. 
2921d 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52   while( SQLITE_R
2921e 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70  OW==sqlite3_step
2921f 28 70 53 74 6d 74 29 20 29 7b 7d 0a 20 20 72 65  (pStmt) ){}.  re
29220 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e  turn sqlite3_fin
29221 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a  alize(pStmt);.}.
29222 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 7a  ./*.** Execute z
29223 53 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65 20  Sql on database 
29224 64 62 2e 20 54 68 65 20 73 74 61 74 65 6d 65 6e  db. The statemen
29225 74 20 72 65 74 75 72 6e 73 20 65 78 61 63 74 6c  t returns exactl
29226 79 0a 2a 2a 20 6f 6e 65 20 63 6f 6c 75 6d 6e 2e  y.** one column.
29227 20 45 78 65 63 75 74 65 20 74 68 69 73 20 61 73   Execute this as
29228 20 53 51 4c 20 6f 6e 20 74 68 65 20 73 61 6d 65   SQL on the same
29229 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74   database..*/.st
2922a 61 74 69 63 20 69 6e 74 20 65 78 65 63 45 78 65  atic int execExe
2922b 63 53 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62  cSql(sqlite3 *db
2922c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
2922d 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  ql){.  sqlite3_s
2922e 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e  tmt *pStmt;.  in
2922f 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  t rc;..  rc = sq
29230 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62  lite3_prepare(db
29231 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
29232 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  mt, 0);.  if( rc
29233 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
29234 74 75 72 6e 20 72 63 3b 0a 0a 20 20 77 68 69 6c  turn rc;..  whil
29235 65 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73  e( SQLITE_ROW==s
29236 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
29237 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 65  t) ){.    rc = e
29238 78 65 63 53 71 6c 28 64 62 2c 20 28 63 68 61 72  xecSql(db, (char
29239 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  *)sqlite3_column
2923a 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 30 29 29  _text(pStmt, 0))
2923b 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2923c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
2923d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
2923e 65 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20  e(pStmt);.      
2923f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
29240 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73  .  }..  return s
29241 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
29242 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pStmt);.}../*.**
29243 20 54 68 65 20 6e 6f 6e 2d 73 74 61 6e 64 61 72   The non-standar
29244 64 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64  d VACUUM command
29245 20 69 73 20 75 73 65 64 20 74 6f 20 63 6c 65 61   is used to clea
29246 6e 20 75 70 20 74 68 65 20 64 61 74 61 62 61 73  n up the databas
29247 65 2c 0a 2a 2a 20 63 6f 6c 6c 61 70 73 65 20 66  e,.** collapse f
29248 72 65 65 20 73 70 61 63 65 2c 20 65 74 63 2e 20  ree space, etc. 
29249 20 49 74 20 69 73 20 6d 6f 64 65 6c 6c 65 64 20   It is modelled 
2924a 61 66 74 65 72 20 74 68 65 20 56 41 43 55 55 4d  after the VACUUM
2924b 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 69 6e 20 50   command.** in P
2924c 6f 73 74 67 72 65 53 51 4c 2e 0a 2a 2a 0a 2a 2a  ostgreSQL..**.**
2924d 20 49 6e 20 76 65 72 73 69 6f 6e 20 31 2e 30 2e   In version 1.0.
2924e 78 20 6f 66 20 53 51 4c 69 74 65 2c 20 74 68 65  x of SQLite, the
2924f 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20   VACUUM command 
29250 77 6f 75 6c 64 20 63 61 6c 6c 0a 2a 2a 20 67 64  would call.** gd
29251 62 6d 5f 72 65 6f 72 67 61 6e 69 7a 65 28 29 20  bm_reorganize() 
29252 6f 6e 20 61 6c 6c 20 74 68 65 20 64 61 74 61 62  on all the datab
29253 61 73 65 20 74 61 62 6c 65 73 2e 20 20 42 75 74  ase tables.  But
29254 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69   beginning.** wi
29255 74 68 20 32 2e 30 2e 30 2c 20 53 51 4c 69 74 65  th 2.0.0, SQLite
29256 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73 20   no longer uses 
29257 47 44 42 4d 20 73 6f 20 74 68 69 73 20 63 6f 6d  GDBM so this com
29258 6d 61 6e 64 20 68 61 73 0a 2a 2a 20 62 65 63 6f  mand has.** beco
29259 6d 65 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  me a no-op..*/.S
2925a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2925b 69 64 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d  id sqlite3Vacuum
2925c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
2925d 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
2925e 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2925f 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  se);.  if( v ){.
29260 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29261 64 64 4f 70 32 28 76 2c 20 4f 50 5f 56 61 63 75  ddOp2(v, OP_Vacu
29262 75 6d 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20  um, 0, 0);.  }. 
29263 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a   return;.}../*.*
29264 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
29265 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 4f 50  mplements the OP
29266 5f 56 61 63 75 75 6d 20 6f 70 63 6f 64 65 20 6f  _Vacuum opcode o
29267 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 53  f the VDBE..*/.S
29268 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
29269 74 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75  t sqlite3RunVacu
2926a 75 6d 28 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  um(char **pzErrM
2926b 73 67 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 29  sg, sqlite3 *db)
2926c 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
2926d 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 52  ITE_OK;     /* R
2926e 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
2926f 73 65 72 76 69 63 65 20 72 6f 75 74 69 6e 65 73  service routines
29270 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4d 61   */.  Btree *pMa
29271 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  in;           /*
29272 20 54 68 65 20 64 61 74 61 62 61 73 65 20 62 65   The database be
29273 69 6e 67 20 76 61 63 75 75 6d 65 64 20 2a 2f 0a  ing vacuumed */.
29274 20 20 50 61 67 65 72 20 2a 70 4d 61 69 6e 50 61    Pager *pMainPa
29275 67 65 72 3b 20 20 20 20 20 20 2f 2a 20 50 61 67  ger;      /* Pag
29276 65 72 20 66 6f 72 20 64 61 74 61 62 61 73 65 20  er for database 
29277 62 65 69 6e 67 20 76 61 63 75 75 6d 65 64 20 2a  being vacuumed *
29278 2f 0a 20 20 42 74 72 65 65 20 2a 70 54 65 6d 70  /.  Btree *pTemp
29279 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
2927a 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  he temporary dat
2927b 61 62 61 73 65 20 77 65 20 76 61 63 75 75 6d 20  abase we vacuum 
2927c 69 6e 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 2a  into */.  char *
2927d 7a 53 71 6c 20 3d 20 30 3b 20 20 20 20 20 20 20  zSql = 0;       
2927e 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65    /* SQL stateme
2927f 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76  nts */.  int sav
29280 65 64 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20  ed_flags;       
29281 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20   /* Saved value 
29282 6f 66 20 74 68 65 20 64 62 2d 3e 66 6c 61 67 73  of the db->flags
29283 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f   */.  int saved_
29284 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a  nChange;      /*
29285 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20   Saved value of 
29286 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 20  db->nChange */. 
29287 20 69 6e 74 20 73 61 76 65 64 5f 6e 54 6f 74 61   int saved_nTota
29288 6c 43 68 61 6e 67 65 3b 20 2f 2a 20 53 61 76 65  lChange; /* Save
29289 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6e  d value of db->n
2928a 54 6f 74 61 6c 43 68 61 6e 67 65 20 2a 2f 0a 20  TotalChange */. 
2928b 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 20 20 20   Db *pDb = 0;   
2928c 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2928d 62 61 73 65 20 74 6f 20 64 65 74 61 63 68 20 61  base to detach a
2928e 74 20 65 6e 64 20 6f 66 20 76 61 63 75 75 6d 20  t end of vacuum 
2928f 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 65 6d 44 62  */.  int isMemDb
29290 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
29291 54 72 75 65 20 69 73 20 76 61 63 75 75 6d 69 6e  True is vacuumin
29292 67 20 61 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74  g a :memory: dat
29293 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  abase */.  int n
29294 52 65 73 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20  Res;..  /* Save 
29295 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
29296 65 20 6f 66 20 74 68 65 20 77 72 69 74 65 2d 73  e of the write-s
29297 63 68 65 6d 61 20 66 6c 61 67 20 62 65 66 6f 72  chema flag befor
29298 65 20 73 65 74 74 69 6e 67 20 69 74 2e 20 2a 2f  e setting it. */
29299 0a 20 20 73 61 76 65 64 5f 66 6c 61 67 73 20 3d  .  saved_flags =
2929a 20 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 73 61   db->flags;.  sa
2929b 76 65 64 5f 6e 43 68 61 6e 67 65 20 3d 20 64 62  ved_nChange = db
2929c 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 73 61 76  ->nChange;.  sav
2929d 65 64 5f 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20  ed_nTotalChange 
2929e 3d 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e  = db->nTotalChan
2929f 67 65 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20  ge;.  db->flags 
292a0 7c 3d 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53  |= SQLITE_WriteS
292a1 63 68 65 6d 61 20 7c 20 53 51 4c 49 54 45 5f 49  chema | SQLITE_I
292a2 67 6e 6f 72 65 43 68 65 63 6b 73 3b 0a 0a 20 20  gnoreChecks;..  
292a3 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d  if( !db->autoCom
292a4 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  mit ){.    sqlit
292a5 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72  e3SetString(pzEr
292a6 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f  rMsg, db, "canno
292a7 74 20 56 41 43 55 55 4d 20 66 72 6f 6d 20 77 69  t VACUUM from wi
292a8 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  thin a transacti
292a9 6f 6e 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  on");.    rc = S
292aa 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
292ab 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
292ac 75 75 6d 3b 0a 20 20 7d 0a 20 20 70 4d 61 69 6e  uum;.  }.  pMain
292ad 20 3d 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42   = db->aDb[0].pB
292ae 74 3b 0a 20 20 70 4d 61 69 6e 50 61 67 65 72 20  t;.  pMainPager 
292af 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
292b0 67 65 72 28 70 4d 61 69 6e 29 3b 0a 20 20 69 73  ger(pMain);.  is
292b1 4d 65 6d 44 62 20 3d 20 73 71 6c 69 74 65 33 50  MemDb = sqlite3P
292b2 61 67 65 72 46 69 6c 65 28 70 4d 61 69 6e 50 61  agerFile(pMainPa
292b3 67 65 72 29 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d  ger)->pMethods==
292b4 30 3b 0a 0a 20 20 2f 2a 20 41 74 74 61 63 68 20  0;..  /* Attach 
292b5 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
292b6 74 61 62 61 73 65 20 61 73 20 27 76 61 63 75 75  tabase as 'vacuu
292b7 6d 5f 64 62 27 2e 20 54 68 65 20 73 79 6e 63 68  m_db'. The synch
292b8 72 6f 6e 6f 75 73 20 70 72 61 67 6d 61 0a 20 20  ronous pragma.  
292b9 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 74 6f  ** can be set to
292ba 20 27 6f 66 66 27 20 66 6f 72 20 74 68 69 73 20   'off' for this 
292bb 66 69 6c 65 2c 20 61 73 20 69 74 20 69 73 20 6e  file, as it is n
292bc 6f 74 20 72 65 63 6f 76 65 72 65 64 20 69 66 20  ot recovered if 
292bd 61 20 63 72 61 73 68 0a 20 20 2a 2a 20 6f 63 63  a crash.  ** occ
292be 75 72 73 20 61 6e 79 77 61 79 2e 20 54 68 65 20  urs anyway. The 
292bf 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65  integrity of the
292c0 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 61 69   database is mai
292c1 6e 74 61 69 6e 65 64 20 62 79 20 61 0a 20 20 2a  ntained by a.  *
292c2 2a 20 28 70 6f 73 73 69 62 6c 79 20 73 79 6e 63  * (possibly sync
292c3 68 72 6f 6e 6f 75 73 29 20 74 72 61 6e 73 61 63  hronous) transac
292c4 74 69 6f 6e 20 6f 70 65 6e 65 64 20 6f 6e 20 74  tion opened on t
292c5 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
292c6 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 73 71 6c   before.  ** sql
292c7 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c  ite3BtreeCopyFil
292c8 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20  e() is called.. 
292c9 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 6f 70 74 69   **.  ** An opti
292ca 6d 69 73 61 74 69 6f 6e 20 77 6f 75 6c 64 20 62  misation would b
292cb 65 20 74 6f 20 75 73 65 20 61 20 6e 6f 6e 2d 6a  e to use a non-j
292cc 6f 75 72 6e 61 6c 65 64 20 70 61 67 65 72 2e 0a  ournaled pager..
292cd 20 20 2a 2a 20 28 4c 61 74 65 72 3a 29 20 49 20    ** (Later:) I 
292ce 74 72 69 65 64 20 73 65 74 74 69 6e 67 20 22 50  tried setting "P
292cf 52 41 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e  RAGMA vacuum_db.
292d0 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46  journal_mode=OFF
292d1 22 20 62 75 74 0a 20 20 2a 2a 20 74 68 61 74 20  " but.  ** that 
292d2 61 63 74 75 61 6c 6c 79 20 6d 61 64 65 20 74 68  actually made th
292d3 65 20 56 41 43 55 55 4d 20 72 75 6e 20 73 6c 6f  e VACUUM run slo
292d4 77 65 72 2e 20 20 56 65 72 79 20 6c 69 74 74 6c  wer.  Very littl
292d5 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 0a 20 20  e journalling.  
292d6 2a 2a 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75  ** actually occu
292d7 72 73 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20  rs when doing a 
292d8 76 61 63 75 75 6d 20 73 69 6e 63 65 20 74 68 65  vacuum since the
292d9 20 76 61 63 75 75 6d 5f 64 62 20 69 73 20 69 6e   vacuum_db is in
292da 69 74 69 61 6c 6c 79 0a 20 20 2a 2a 20 65 6d 70  itially.  ** emp
292db 74 79 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6a 6f  ty.  Only the jo
292dc 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20  urnal header is 
292dd 77 72 69 74 74 65 6e 2e 20 20 41 70 70 61 72 65  written.  Appare
292de 6e 74 6c 79 20 69 74 20 74 61 6b 65 73 20 6d 6f  ntly it takes mo
292df 72 65 0a 20 20 2a 2a 20 74 69 6d 65 20 74 6f 20  re.  ** time to 
292e0 70 61 72 73 65 20 61 6e 64 20 72 75 6e 20 74 68  parse and run th
292e1 65 20 50 52 41 47 4d 41 20 74 6f 20 74 75 72 6e  e PRAGMA to turn
292e2 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f 66 66   journalling off
292e3 20 74 68 61 6e 20 69 74 20 64 6f 65 73 0a 20 20   than it does.  
292e4 2a 2a 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  ** to write the 
292e5 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
292e6 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 7a 53 71 6c  ile..  */.  zSql
292e7 20 3d 20 22 41 54 54 41 43 48 20 27 27 20 41 53   = "ATTACH '' AS
292e8 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0a 20 20   vacuum_db;";.  
292e9 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
292ea 20 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63   zSql);.  if( rc
292eb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
292ec 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
292ed 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  ;.  pDb = &db->a
292ee 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20  Db[db->nDb-1];. 
292ef 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28   assert( strcmp(
292f0 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d  db->aDb[db->nDb-
292f1 31 5d 2e 7a 4e 61 6d 65 2c 22 76 61 63 75 75 6d  1].zName,"vacuum
292f2 5f 64 62 22 29 3d 3d 30 20 29 3b 0a 20 20 70 54  _db")==0 );.  pT
292f3 65 6d 70 20 3d 20 64 62 2d 3e 61 44 62 5b 64 62  emp = db->aDb[db
292f4 2d 3e 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a 0a 20  ->nDb-1].pBt;.. 
292f5 20 6e 52 65 73 20 3d 20 73 71 6c 69 74 65 33 42   nRes = sqlite3B
292f6 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 70  treeGetReserve(p
292f7 4d 61 69 6e 29 3b 0a 0a 20 20 2f 2a 20 41 20 56  Main);..  /* A V
292f8 41 43 55 55 4d 20 63 61 6e 6e 6f 74 20 63 68 61  ACUUM cannot cha
292f9 6e 67 65 20 74 68 65 20 70 61 67 65 73 69 7a 65  nge the pagesize
292fa 20 6f 66 20 61 6e 20 65 6e 63 72 79 70 74 65 64   of an encrypted
292fb 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 23 69   database. */.#i
292fc 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
292fd 43 4f 44 45 43 0a 20 20 69 66 28 20 64 62 2d 3e  CODEC.  if( db->
292fe 6e 65 78 74 50 61 67 65 73 69 7a 65 20 29 7b 0a  nextPagesize ){.
292ff 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20      extern void 
29300 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b  sqlite3CodecGetK
29301 65 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ey(sqlite3*, int
29302 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b  , void**, int*);
29303 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20  .    int nKey;. 
29304 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20     char *zKey;. 
29305 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47     sqlite3CodecG
29306 65 74 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f  etKey(db, 0, (vo
29307 69 64 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65  id**)&zKey, &nKe
29308 79 29 3b 0a 20 20 20 20 69 66 28 20 6e 4b 65 79  y);.    if( nKey
29309 20 29 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73   ) db->nextPages
2930a 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e  ize = 0;.  }.#en
2930b 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  dif..  if( sqlit
2930c 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69  e3BtreeSetPageSi
2930d 7a 65 28 70 54 65 6d 70 2c 20 73 71 6c 69 74 65  ze(pTemp, sqlite
2930e 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
2930f 65 28 70 4d 61 69 6e 29 2c 20 6e 52 65 73 29 0a  e(pMain), nRes).
29310 20 20 20 7c 7c 20 28 21 69 73 4d 65 6d 44 62 20     || (!isMemDb 
29311 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  && sqlite3BtreeS
29312 65 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70  etPageSize(pTemp
29313 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69  , db->nextPagesi
29314 7a 65 2c 20 6e 52 65 73 29 29 0a 20 20 20 7c 7c  ze, nRes)).   ||
29315 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
29316 64 20 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d  d .  ){.    rc =
29317 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
29318 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76     goto end_of_v
29319 61 63 75 75 6d 3b 0a 20 20 7d 0a 20 20 72 63 20  acuum;.  }.  rc 
2931a 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20 22 50  = execSql(db, "P
2931b 52 41 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e  RAGMA vacuum_db.
2931c 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 22  synchronous=OFF"
2931d 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2931e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f  ITE_OK ){.    go
2931f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
29320 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
29321 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
29322 41 43 55 55 4d 0a 20 20 73 71 6c 69 74 65 33 42  ACUUM.  sqlite3B
29323 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75  treeSetAutoVacuu
29324 6d 28 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78  m(pTemp, db->nex
29325 74 41 75 74 6f 76 61 63 3e 3d 30 20 3f 20 64 62  tAutovac>=0 ? db
29326 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 20 3a 0a  ->nextAutovac :.
29327 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
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 20 20 20 73 71 6c 69 74             sqlit
2932a 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61  e3BtreeGetAutoVa
2932b 63 75 75 6d 28 70 4d 61 69 6e 29 29 3b 0a 23 65  cuum(pMain));.#e
2932c 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  ndif..  /* Begin
2932d 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a   a transaction *
2932e 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c  /.  rc = execSql
2932f 28 64 62 2c 20 22 42 45 47 49 4e 20 45 58 43 4c  (db, "BEGIN EXCL
29330 55 53 49 56 45 3b 22 29 3b 0a 20 20 69 66 28 20  USIVE;");.  if( 
29331 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
29332 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
29333 75 6d 3b 0a 0a 20 20 2f 2a 20 51 75 65 72 79 20  um;..  /* Query 
29334 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 74 68  the schema of th
29335 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
29336 20 43 72 65 61 74 65 20 61 20 6d 69 72 72 6f 72   Create a mirror
29337 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 69 6e 20   schema.  ** in 
29338 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
29339 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 72  tabase..  */.  r
2933a 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28  c = execExecSql(
2933b 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45  db, .      "SELE
2933c 43 54 20 27 43 52 45 41 54 45 20 54 41 42 4c 45  CT 'CREATE TABLE
2933d 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
2933e 73 75 62 73 74 72 28 73 71 6c 2c 31 34 29 20 22  substr(sql,14) "
2933f 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73  .      "  FROM s
29340 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
29341 52 45 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20  RE type='table' 
29342 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74  AND name!='sqlit
29343 65 5f 73 65 71 75 65 6e 63 65 27 22 0a 20 20 20  e_sequence'".   
29344 20 20 20 22 20 20 20 41 4e 44 20 72 6f 6f 74 70     "   AND rootp
29345 61 67 65 3e 30 22 0a 20 20 29 3b 0a 20 20 69 66  age>0".  );.  if
29346 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
29347 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
29348 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65  cuum;.  rc = exe
29349 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20  cExecSql(db, .  
2934a 20 20 20 20 22 53 45 4c 45 43 54 20 27 43 52 45      "SELECT 'CRE
2934b 41 54 45 20 49 4e 44 45 58 20 76 61 63 75 75 6d  ATE INDEX vacuum
2934c 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28  _db.' || substr(
2934d 73 71 6c 2c 31 34 29 22 0a 20 20 20 20 20 20 22  sql,14)".      "
2934e 20 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61    FROM sqlite_ma
2934f 73 74 65 72 20 57 48 45 52 45 20 73 71 6c 20 4c  ster WHERE sql L
29350 49 4b 45 20 27 43 52 45 41 54 45 20 49 4e 44 45  IKE 'CREATE INDE
29351 58 20 25 27 20 22 29 3b 0a 20 20 69 66 28 20 72  X %' ");.  if( r
29352 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
29353 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
29354 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78  m;.  rc = execEx
29355 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20  ecSql(db, .     
29356 20 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45   "SELECT 'CREATE
29357 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 76 61   UNIQUE INDEX va
29358 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62  cuum_db.' || sub
29359 73 74 72 28 73 71 6c 2c 32 31 29 20 22 0a 20 20  str(sql,21) ".  
2935a 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69      "  FROM sqli
2935b 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20  te_master WHERE 
2935c 73 71 6c 20 4c 49 4b 45 20 27 43 52 45 41 54 45  sql LIKE 'CREATE
2935d 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 25 27   UNIQUE INDEX %'
2935e 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ");.  if( rc!=SQ
2935f 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
29360 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20  nd_of_vacuum;.. 
29361 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
29362 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74   the tables in t
29363 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
29364 2e 20 46 6f 72 20 65 61 63 68 2c 20 64 6f 0a 20  . For each, do. 
29365 20 2a 2a 20 61 6e 20 22 49 4e 53 45 52 54 20 49   ** an "INSERT I
29366 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 78 78  NTO vacuum_db.xx
29367 78 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  x SELECT * FROM 
29368 78 78 78 3b 22 20 74 6f 20 63 6f 70 79 0a 20 20  xxx;" to copy.  
29369 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ** the contents 
2936a 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  to the temporary
2936b 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a   database..  */.
2936c 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53    rc = execExecS
2936d 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53  ql(db, .      "S
2936e 45 4c 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e  ELECT 'INSERT IN
2936f 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c  TO vacuum_db.' |
29370 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0a  | quote(name) ".
29371 20 20 20 20 20 20 22 7c 7c 20 27 20 53 45 4c 45        "|| ' SELE
29372 43 54 20 2a 20 46 52 4f 4d 20 27 20 7c 7c 20 71  CT * FROM ' || q
29373 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b  uote(name) || ';
29374 27 22 0a 20 20 20 20 20 20 22 46 52 4f 4d 20 73  '".      "FROM s
29375 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 22 0a 20  qlite_master ". 
29376 20 20 20 20 20 22 57 48 45 52 45 20 74 79 70 65       "WHERE type
29377 20 3d 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e   = 'table' AND n
29378 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f 73 65 71  ame!='sqlite_seq
29379 75 65 6e 63 65 27 20 22 0a 20 20 20 20 20 20 22  uence' ".      "
2937a 20 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3e 30    AND rootpage>0
2937b 22 0a 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  "..  );.  if( rc
2937c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2937d 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2937e 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65  ;..  /* Copy ove
2937f 72 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 74  r the sequence t
29380 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  able.  */.  rc =
29381 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c   execExecSql(db,
29382 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20   .      "SELECT 
29383 27 44 45 4c 45 54 45 20 46 52 4f 4d 20 76 61 63  'DELETE FROM vac
29384 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74  uum_db.' || quot
29385 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22  e(name) || ';' "
29386 0a 20 20 20 20 20 20 22 46 52 4f 4d 20 76 61 63  .      "FROM vac
29387 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61  uum_db.sqlite_ma
29388 73 74 65 72 20 57 48 45 52 45 20 6e 61 6d 65 3d  ster WHERE name=
29389 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65  'sqlite_sequence
2938a 27 20 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72  ' ".  );.  if( r
2938b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
2938c 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2938d 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78  m;.  rc = execEx
2938e 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20  ecSql(db, .     
2938f 20 22 53 45 4c 45 43 54 20 27 49 4e 53 45 52 54   "SELECT 'INSERT
29390 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e   INTO vacuum_db.
29391 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
29392 20 22 0a 20 20 20 20 20 20 22 7c 7c 20 27 20 53   ".      "|| ' S
29393 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 27 20 7c  ELECT * FROM ' |
29394 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c  | quote(name) ||
29395 20 27 3b 27 20 22 0a 20 20 20 20 20 20 22 46 52   ';' ".      "FR
29396 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c  OM vacuum_db.sql
29397 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45  ite_master WHERE
29398 20 6e 61 6d 65 3d 3d 27 73 71 6c 69 74 65 5f 73   name=='sqlite_s
29399 65 71 75 65 6e 63 65 27 3b 22 0a 20 20 29 3b 0a  equence';".  );.
2939a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2939b 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2939c 66 5f 76 61 63 75 75 6d 3b 0a 0a 0a 20 20 2f 2a  f_vacuum;...  /*
2939d 20 43 6f 70 79 20 74 68 65 20 74 72 69 67 67 65   Copy the trigge
2939e 72 73 2c 20 76 69 65 77 73 2c 20 61 6e 64 20 76  rs, views, and v
2939f 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 66 72  irtual tables fr
293a0 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  om the main data
293a1 62 61 73 65 0a 20 20 2a 2a 20 6f 76 65 72 20 74  base.  ** over t
293a2 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  o the temporary 
293a3 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20  database.  None 
293a4 6f 66 20 74 68 65 73 65 20 6f 62 6a 65 63 74 73  of these objects
293a5 20 68 61 73 20 61 6e 79 0a 20 20 2a 2a 20 61 73   has any.  ** as
293a6 73 6f 63 69 61 74 65 64 20 73 74 6f 72 61 67 65  sociated storage
293a7 2c 20 73 6f 20 61 6c 6c 20 77 65 20 68 61 76 65  , so all we have
293a8 20 74 6f 20 64 6f 20 69 73 20 63 6f 70 79 20 74   to do is copy t
293a9 68 65 69 72 20 65 6e 74 72 69 65 73 0a 20 20 2a  heir entries.  *
293aa 2a 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 54  * from the SQLIT
293ab 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 0a  E_MASTER table..
293ac 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63    */.  rc = exec
293ad 53 71 6c 28 64 62 2c 0a 20 20 20 20 20 20 22 49  Sql(db,.      "I
293ae 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75  NSERT INTO vacuu
293af 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74  m_db.sqlite_mast
293b0 65 72 20 22 0a 20 20 20 20 20 20 22 20 20 53 45  er ".      "  SE
293b1 4c 45 43 54 20 74 79 70 65 2c 20 6e 61 6d 65 2c  LECT type, name,
293b2 20 74 62 6c 5f 6e 61 6d 65 2c 20 72 6f 6f 74 70   tbl_name, rootp
293b3 61 67 65 2c 20 73 71 6c 22 0a 20 20 20 20 20 20  age, sql".      
293b4 22 20 20 20 20 46 52 4f 4d 20 73 71 6c 69 74 65  "    FROM sqlite
293b5 5f 6d 61 73 74 65 72 22 0a 20 20 20 20 20 20 22  _master".      "
293b6 20 20 20 57 48 45 52 45 20 74 79 70 65 3d 27 76     WHERE type='v
293b7 69 65 77 27 20 4f 52 20 74 79 70 65 3d 27 74 72  iew' OR type='tr
293b8 69 67 67 65 72 27 22 0a 20 20 20 20 20 20 22 20  igger'".      " 
293b9 20 20 20 20 20 4f 52 20 28 74 79 70 65 3d 27 74       OR (type='t
293ba 61 62 6c 65 27 20 41 4e 44 20 72 6f 6f 74 70 61  able' AND rootpa
293bb 67 65 3d 30 29 22 0a 20 20 29 3b 0a 20 20 69 66  ge=0)".  );.  if
293bc 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f  ( rc ) goto end_
293bd 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a  of_vacuum;..  /*
293be 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
293bf 75 6e 6c 65 73 73 20 74 68 65 20 6d 61 69 6e 20  unless the main 
293c0 64 62 20 77 61 73 20 63 6f 6d 70 6c 65 74 65 6c  db was completel
293c1 79 20 65 6d 70 74 79 2c 20 74 68 65 72 65 20 69  y empty, there i
293c2 73 20 6e 6f 77 20 61 0a 20 20 2a 2a 20 74 72 61  s now a.  ** tra
293c3 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e  nsaction open on
293c4 20 74 68 65 20 76 61 63 75 75 6d 20 64 61 74 61   the vacuum data
293c5 62 61 73 65 2c 20 62 75 74 20 6e 6f 74 20 6f 6e  base, but not on
293c6 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
293c7 73 65 2e 0a 20 20 2a 2a 20 4f 70 65 6e 20 61 20  se..  ** Open a 
293c8 62 74 72 65 65 20 6c 65 76 65 6c 20 74 72 61 6e  btree level tran
293c9 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 6d  saction on the m
293ca 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 54 68  ain database. Th
293cb 69 73 20 61 6c 6c 6f 77 73 20 61 0a 20 20 2a 2a  is allows a.  **
293cc 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
293cd 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 29 2e  BtreeCopyFile().
293ce 20 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   The main databa
293cf 73 65 20 62 74 72 65 65 20 6c 65 76 65 6c 0a 20  se btree level. 
293d0 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
293d1 69 73 20 74 68 65 6e 20 63 6f 6d 6d 69 74 74 65  is then committe
293d2 64 2c 20 73 6f 20 74 68 65 20 53 51 4c 20 6c 65  d, so the SQL le
293d3 76 65 6c 20 6e 65 76 65 72 20 6b 6e 6f 77 73 20  vel never knows 
293d4 69 74 20 77 61 73 0a 20 20 2a 2a 20 6f 70 65 6e  it was.  ** open
293d5 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20  ed for writing. 
293d6 54 68 69 73 20 77 61 79 2c 20 74 68 65 20 53 51  This way, the SQ
293d7 4c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 75 73  L transaction us
293d8 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  ed to create the
293d9 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20  .  ** temporary 
293da 64 61 74 61 62 61 73 65 20 6e 65 76 65 72 20 6e  database never n
293db 65 65 64 73 20 74 6f 20 62 65 20 63 6f 6d 6d 69  eeds to be commi
293dc 74 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tted..  */.  if(
293dd 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
293de 7b 0a 20 20 20 20 75 33 32 20 6d 65 74 61 3b 0a  {.    u32 meta;.
293df 20 20 20 20 69 6e 74 20 69 3b 0a 0a 20 20 20 20      int i;..    
293e0 2f 2a 20 54 68 69 73 20 61 72 72 61 79 20 64 65  /* This array de
293e1 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20 6d  termines which m
293e2 65 74 61 20 6d 65 74 61 20 76 61 6c 75 65 73 20  eta meta values 
293e3 61 72 65 20 70 72 65 73 65 72 76 65 64 20 69 6e  are preserved in
293e4 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 61 63 75   the.    ** vacu
293e5 75 6d 2e 20 20 45 76 65 6e 20 65 6e 74 72 69 65  um.  Even entrie
293e6 73 20 61 72 65 20 74 68 65 20 6d 65 74 61 20 76  s are the meta v
293e7 61 6c 75 65 20 6e 75 6d 62 65 72 20 61 6e 64 20  alue number and 
293e8 6f 64 64 20 65 6e 74 72 69 65 73 0a 20 20 20 20  odd entries.    
293e9 2a 2a 20 61 72 65 20 61 6e 20 69 6e 63 72 65 6d  ** are an increm
293ea 65 6e 74 20 74 6f 20 61 70 70 6c 79 20 74 6f 20  ent to apply to 
293eb 74 68 65 20 6d 65 74 61 20 76 61 6c 75 65 20 61  the meta value a
293ec 66 74 65 72 20 74 68 65 20 76 61 63 75 75 6d 2e  fter the vacuum.
293ed 0a 20 20 20 20 2a 2a 20 54 68 65 20 69 6e 63 72  .    ** The incr
293ee 65 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f  ement is used to
293ef 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 63   increase the sc
293f0 68 65 6d 61 20 63 6f 6f 6b 69 65 20 73 6f 20 74  hema cookie so t
293f1 68 61 74 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a  hat other.    **
293f2 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20   connections to 
293f3 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
293f4 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 74 6f 20 72  e will know to r
293f5 65 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61  eread the schema
293f6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61  ..    */.    sta
293f7 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
293f8 65 64 20 63 68 61 72 20 61 43 6f 70 79 5b 5d 20  ed char aCopy[] 
293f9 3d 20 7b 0a 20 20 20 20 20 20 20 31 2c 20 31 2c  = {.       1, 1,
293fa 20 20 20 20 2f 2a 20 41 64 64 20 6f 6e 65 20 74      /* Add one t
293fb 6f 20 74 68 65 20 6f 6c 64 20 73 63 68 65 6d 61  o the old schema
293fc 20 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20   cookie */.     
293fd 20 20 33 2c 20 30 2c 20 20 20 20 2f 2a 20 50 72    3, 0,    /* Pr
293fe 65 73 65 72 76 65 20 74 68 65 20 64 65 66 61 75  eserve the defau
293ff 6c 74 20 70 61 67 65 20 63 61 63 68 65 20 73 69  lt page cache si
29400 7a 65 20 2a 2f 0a 20 20 20 20 20 20 20 35 2c 20  ze */.       5, 
29401 30 2c 20 20 20 20 2f 2a 20 50 72 65 73 65 72 76  0,    /* Preserv
29402 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 74 65  e the default te
29403 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  xt encoding */. 
29404 20 20 20 20 20 20 36 2c 20 30 2c 20 20 20 20 2f        6, 0,    /
29405 2a 20 50 72 65 73 65 72 76 65 20 74 68 65 20 75  * Preserve the u
29406 73 65 72 20 76 65 72 73 69 6f 6e 20 2a 2f 0a 20  ser version */. 
29407 20 20 20 7d 3b 0a 0a 20 20 20 20 61 73 73 65 72     };..    asser
29408 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42 74 72  t( 1==sqlite3Btr
29409 65 65 49 73 49 6e 54 72 61 6e 73 28 70 54 65 6d  eeIsInTrans(pTem
2940a 70 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  p) );.    assert
2940b 28 20 31 3d 3d 73 71 6c 69 74 65 33 42 74 72 65  ( 1==sqlite3Btre
2940c 65 49 73 49 6e 54 72 61 6e 73 28 70 4d 61 69 6e  eIsInTrans(pMain
2940d 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 70  ) );..    /* Cop
2940e 79 20 42 74 72 65 65 20 6d 65 74 61 20 76 61 6c  y Btree meta val
2940f 75 65 73 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  ues */.    for(i
29410 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
29411 61 43 6f 70 79 29 3b 20 69 2b 3d 32 29 7b 0a 20  aCopy); i+=2){. 
29412 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
29413 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 4d  3BtreeGetMeta(pM
29414 61 69 6e 2c 20 61 43 6f 70 79 5b 69 5d 2c 20 26  ain, aCopy[i], &
29415 6d 65 74 61 29 3b 0a 20 20 20 20 20 20 69 66 28  meta);.      if(
29416 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
29417 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
29418 75 75 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  uum;.      rc = 
29419 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61  sqlite3BtreeUpda
2941a 74 65 4d 65 74 61 28 70 54 65 6d 70 2c 20 61 43  teMeta(pTemp, aC
2941b 6f 70 79 5b 69 5d 2c 20 6d 65 74 61 2b 61 43 6f  opy[i], meta+aCo
2941c 70 79 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 20 20  py[i+1]);.      
2941d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2941e 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  K ) goto end_of_
2941f 76 61 63 75 75 6d 3b 0a 20 20 20 20 7d 0a 0a 20  vacuum;.    }.. 
29420 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
29421 74 72 65 65 43 6f 70 79 46 69 6c 65 28 70 4d 61  treeCopyFile(pMa
29422 69 6e 2c 20 70 54 65 6d 70 29 3b 0a 20 20 20 20  in, pTemp);.    
29423 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
29424 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  K ) goto end_of_
29425 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20 3d  vacuum;.    rc =
29426 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
29427 6d 69 74 28 70 54 65 6d 70 29 3b 0a 20 20 20 20  mit(pTemp);.    
29428 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
29429 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  K ) goto end_of_
2942a 76 61 63 75 75 6d 3b 0a 23 69 66 6e 64 65 66 20  vacuum;.#ifndef 
2942b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
2942c 56 41 43 55 55 4d 0a 20 20 20 20 73 71 6c 69 74  VACUUM.    sqlit
2942d 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61  e3BtreeSetAutoVa
2942e 63 75 75 6d 28 70 4d 61 69 6e 2c 20 73 71 6c 69  cuum(pMain, sqli
2942f 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56  te3BtreeGetAutoV
29430 61 63 75 75 6d 28 70 54 65 6d 70 29 29 3b 0a 23  acuum(pTemp));.#
29431 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73  endif.    rc = s
29432 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
29433 74 28 70 4d 61 69 6e 29 3b 0a 20 20 7d 0a 0a 20  t(pMain);.  }.. 
29434 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
29435 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  OK ){.    rc = s
29436 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61  qlite3BtreeSetPa
29437 67 65 53 69 7a 65 28 70 4d 61 69 6e 2c 20 73 71  geSize(pMain, sq
29438 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
29439 65 53 69 7a 65 28 70 54 65 6d 70 29 2c 20 6e 52  eSize(pTemp), nR
2943a 65 73 29 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6f 66  es);.  }..end_of
2943b 5f 76 61 63 75 75 6d 3a 0a 20 20 2f 2a 20 52 65  _vacuum:.  /* Re
2943c 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e  store the origin
2943d 61 6c 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e  al value of db->
2943e 66 6c 61 67 73 20 2a 2f 0a 20 20 64 62 2d 3e 66  flags */.  db->f
2943f 6c 61 67 73 20 3d 20 73 61 76 65 64 5f 66 6c 61  lags = saved_fla
29440 67 73 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67  gs;.  db->nChang
29441 65 20 3d 20 73 61 76 65 64 5f 6e 43 68 61 6e 67  e = saved_nChang
29442 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43  e;.  db->nTotalC
29443 68 61 6e 67 65 20 3d 20 73 61 76 65 64 5f 6e 54  hange = saved_nT
29444 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 0a 20 20 2f  otalChange;..  /
29445 2a 20 43 75 72 72 65 6e 74 6c 79 20 74 68 65 72  * Currently ther
29446 65 20 69 73 20 61 6e 20 53 51 4c 20 6c 65 76 65  e is an SQL leve
29447 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  l transaction op
29448 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d  en on the vacuum
29449 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  .  ** database. 
2944a 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c  No locks are hel
2944b 64 20 6f 6e 20 61 6e 79 20 6f 74 68 65 72 20 66  d on any other f
2944c 69 6c 65 73 20 28 73 69 6e 63 65 20 74 68 65 20  iles (since the 
2944d 6d 61 69 6e 20 66 69 6c 65 0a 20 20 2a 2a 20 77  main file.  ** w
2944e 61 73 20 63 6f 6d 6d 69 74 74 65 64 20 61 74 20  as committed at 
2944f 74 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c 29  the btree level)
29450 2e 20 53 6f 20 69 74 20 73 61 66 65 20 74 6f 20  . So it safe to 
29451 65 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74  end the transact
29452 69 6f 6e 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75  ion.  ** by manu
29453 61 6c 6c 79 20 73 65 74 74 69 6e 67 20 74 68 65  ally setting the
29454 20 61 75 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67   autoCommit flag
29455 20 74 6f 20 74 72 75 65 20 61 6e 64 20 64 65 74   to true and det
29456 61 63 68 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20  aching the.  ** 
29457 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e  vacuum database.
29458 20 54 68 65 20 76 61 63 75 75 6d 5f 64 62 20 6a   The vacuum_db j
29459 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64  ournal file is d
2945a 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20  eleted when the 
2945b 70 61 67 65 72 0a 20 20 2a 2a 20 69 73 20 63 6c  pager.  ** is cl
2945c 6f 73 65 64 20 62 79 20 74 68 65 20 44 45 54 41  osed by the DETA
2945d 43 48 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61  CH..  */.  db->a
2945e 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a  utoCommit = 1;..
2945f 20 20 69 66 28 20 70 44 62 20 29 7b 0a 20 20 20    if( pDb ){.   
29460 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
29461 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  se(pDb->pBt);.  
29462 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a    pDb->pBt = 0;.
29463 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
29464 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c   = 0;.  }..  sql
29465 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
29466 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
29467 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
29468 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
29469 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 26 26  E_OMIT_VACUUM &&
2946a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54   SQLITE_OMIT_ATT
2946b 41 43 48 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ACH */../*******
2946c 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
2946d 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  acuum.c ********
2946e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2946f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29470 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
29471 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
29472 6c 65 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a  le vtab.c ******
29473 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29474 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29475 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
29476 30 36 20 4a 75 6e 65 20 31 30 0a 2a 2a 0a 2a 2a  06 June 10.**.**
29477 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
29478 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
29479 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2947a 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2947b 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2947c 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2947d 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2947e 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2947f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
29480 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
29481 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
29482 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
29483 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
29484 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
29485 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
29486 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
29487 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
29488 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29489 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2948a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2948b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2948c 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
2948d 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2948e 63 6f 64 65 20 75 73 65 64 20 74 6f 20 68 65 6c  code used to hel
2948f 70 20 69 6d 70 6c 65 6d 65 6e 74 20 76 69 72 74  p implement virt
29490 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a  ual tables..**.*
29491 2a 20 24 49 64 3a 20 76 74 61 62 2e 63 2c 76 20  * $Id: vtab.c,v 
29492 31 2e 38 31 20 32 30 30 38 2f 31 32 2f 31 30 20  1.81 2008/12/10 
29493 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45 78 70  19:26:24 drh Exp
29494 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51   $.*/.#ifndef SQ
29495 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
29496 4c 54 41 42 4c 45 0a 0a 73 74 61 74 69 63 20 69  LTABLE..static i
29497 6e 74 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28  nt createModule(
29498 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
29499 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2949a 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69     /* Database i
2949b 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20 69  n which module i
2949c 73 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f 0a  s registered */.
2949d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
2949e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ame,            
2949f 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e    /* Name assign
294a0 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c  ed to this modul
294a1 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c  e */.  const sql
294a2 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
294a3 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64 65  dule,  /* The de
294a4 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20  finition of the 
294a5 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64  module */.  void
294a6 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20 20   *pAux,         
294a7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
294a8 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66  ontext pointer f
294a9 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e  or xCreate/xConn
294aa 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ect */.  void (*
294ab 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a  xDestroy)(void *
294ac 29 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75  )        /* Modu
294ad 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75  le destructor fu
294ae 6e 63 74 69 6f 6e 20 2a 2f 0a 29 20 7b 0a 20 20  nction */.) {.  
294af 69 6e 74 20 72 63 2c 20 6e 4e 61 6d 65 3b 0a 20  int rc, nName;. 
294b0 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 0a   Module *pMod;..
294b1 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
294b2 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
294b3 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  ;.  nName = sqli
294b4 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
294b5 65 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f  e);.  pMod = (Mo
294b6 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 44 62  dule *)sqlite3Db
294b7 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  MallocRaw(db, si
294b8 7a 65 6f 66 28 4d 6f 64 75 6c 65 29 20 2b 20 6e  zeof(Module) + n
294b9 4e 61 6d 65 20 2b 20 31 29 3b 0a 20 20 69 66 28  Name + 1);.  if(
294ba 20 70 4d 6f 64 20 29 7b 0a 20 20 20 20 4d 6f 64   pMod ){.    Mod
294bb 75 6c 65 20 2a 70 44 65 6c 3b 0a 20 20 20 20 63  ule *pDel;.    c
294bc 68 61 72 20 2a 7a 43 6f 70 79 20 3d 20 28 63 68  har *zCopy = (ch
294bd 61 72 20 2a 29 28 26 70 4d 6f 64 5b 31 5d 29 3b  ar *)(&pMod[1]);
294be 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70  .    memcpy(zCop
294bf 79 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b  y, zName, nName+
294c0 31 29 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 7a 4e  1);.    pMod->zN
294c1 61 6d 65 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20  ame = zCopy;.   
294c2 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 20 3d   pMod->pModule =
294c3 20 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 4d   pModule;.    pM
294c4 6f 64 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b  od->pAux = pAux;
294c5 0a 20 20 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74  .    pMod->xDest
294c6 72 6f 79 20 3d 20 78 44 65 73 74 72 6f 79 3b 0a  roy = xDestroy;.
294c7 20 20 20 20 70 44 65 6c 20 3d 20 28 4d 6f 64 75      pDel = (Modu
294c8 6c 65 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68  le *)sqlite3Hash
294c9 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 4d 6f 64  Insert(&db->aMod
294ca 75 6c 65 2c 20 7a 43 6f 70 79 2c 20 6e 4e 61 6d  ule, zCopy, nNam
294cb 65 2c 20 28 76 6f 69 64 2a 29 70 4d 6f 64 29 3b  e, (void*)pMod);
294cc 0a 20 20 20 20 69 66 28 20 70 44 65 6c 20 26 26  .    if( pDel &&
294cd 20 70 44 65 6c 2d 3e 78 44 65 73 74 72 6f 79 20   pDel->xDestroy 
294ce 29 7b 0a 20 20 20 20 20 20 70 44 65 6c 2d 3e 78  ){.      pDel->x
294cf 44 65 73 74 72 6f 79 28 70 44 65 6c 2d 3e 70 41  Destroy(pDel->pA
294d0 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ux);.    }.    s
294d1 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
294d2 20 70 44 65 6c 29 3b 0a 20 20 20 20 69 66 28 20   pDel);.    if( 
294d3 70 44 65 6c 3d 3d 70 4d 6f 64 20 29 7b 0a 20 20  pDel==pMod ){.  
294d4 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
294d5 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  iled = 1;.    }.
294d6 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
294d7 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
294d8 62 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69  b, 0);.  }else i
294d9 66 28 20 78 44 65 73 74 72 6f 79 20 29 7b 0a 20  f( xDestroy ){. 
294da 20 20 20 78 44 65 73 74 72 6f 79 28 70 41 75 78     xDestroy(pAux
294db 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71  );.  }.  rc = sq
294dc 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
294dd 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 73   SQLITE_OK);.  s
294de 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
294df 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
294e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
294e1 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41  /*.** External A
294e2 50 49 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  PI function used
294e3 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
294e4 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
294e5 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  odule..*/.SQLITE
294e6 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
294e7 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 0a  _create_module(.
294e8 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
294e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
294ea 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e    /* Database in
294eb 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20 69 73   which module is
294ec 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f 0a 20   registered */. 
294ed 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
294ee 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  me,             
294ef 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e 65   /* Name assigne
294f0 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65  d to this module
294f1 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   */.  const sqli
294f2 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
294f3 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66  ule,  /* The def
294f4 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d  inition of the m
294f5 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20  odule */.  void 
294f6 2a 70 41 75 78 20 20 20 20 20 20 20 20 20 20 20  *pAux           
294f7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
294f8 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f  ntext pointer fo
294f9 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65  r xCreate/xConne
294fa 63 74 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72  ct */.){.  retur
294fb 6e 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 64  n createModule(d
294fc 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c  b, zName, pModul
294fd 65 2c 20 70 41 75 78 2c 20 30 29 3b 0a 7d 0a 0a  e, pAux, 0);.}..
294fe 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41  /*.** External A
294ff 50 49 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  PI function used
29500 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
29501 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
29502 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  odule..*/.SQLITE
29503 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
29504 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76  _create_module_v
29505 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2(.  sqlite3 *db
29506 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
29507 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
29508 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65   in which module
29509 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 2a   is registered *
2950a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2950b 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20  zName,          
2950c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69      /* Name assi
2950d 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64  gned to this mod
2950e 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
2950f 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
29510 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20  Module,  /* The 
29511 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68  definition of th
29512 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f  e module */.  vo
29513 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20  id *pAux,       
29514 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29515 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   Context pointer
29516 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
29517 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20  nnect */.  void 
29518 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64  (*xDestroy)(void
29519 20 2a 29 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f   *)        /* Mo
2951a 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20  dule destructor 
2951b 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  function */.){. 
2951c 20 72 65 74 75 72 6e 20 63 72 65 61 74 65 4d 6f   return createMo
2951d 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  dule(db, zName, 
2951e 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78 2c 20 78  pModule, pAux, x
2951f 44 65 73 74 72 6f 79 29 3b 0a 7d 0a 0a 2f 2a 0a  Destroy);.}../*.
29520 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 76 69 72 74  ** Lock the virt
29521 75 61 6c 20 74 61 62 6c 65 20 73 6f 20 74 68 61  ual table so tha
29522 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64  t it cannot be d
29523 69 73 63 6f 6e 6e 65 63 74 65 64 2e 0a 2a 2a 20  isconnected..** 
29524 4c 6f 63 6b 73 20 6e 65 73 74 2e 20 20 45 76 65  Locks nest.  Eve
29525 72 79 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 68  ry lock should h
29526 61 76 65 20 61 20 63 6f 72 72 65 73 70 6f 6e 64  ave a correspond
29527 69 6e 67 20 75 6e 6c 6f 63 6b 2e 0a 2a 2a 20 49  ing unlock..** I
29528 66 20 61 6e 20 75 6e 6c 6f 63 6b 20 69 73 20 6f  f an unlock is o
29529 6d 69 74 74 65 64 2c 20 72 65 73 6f 75 72 63 65  mitted, resource
2952a 73 20 6c 65 61 6b 73 20 77 69 6c 6c 20 6f 63 63  s leaks will occ
2952b 75 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ur.  .**.** If a
2952c 20 64 69 73 63 6f 6e 6e 65 63 74 20 69 73 20 61   disconnect is a
2952d 74 74 65 6d 70 74 65 64 20 77 68 69 6c 65 20 61  ttempted while a
2952e 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
2952f 73 20 6c 6f 63 6b 65 64 2c 0a 2a 2a 20 74 68 65  s locked,.** the
29530 20 64 69 73 63 6f 6e 6e 65 63 74 20 69 73 20 64   disconnect is d
29531 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c  eferred until al
29532 6c 20 6c 6f 63 6b 73 20 68 61 76 65 20 62 65 65  l locks have bee
29533 6e 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51  n removed..*/.SQ
29534 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
29535 64 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63  d sqlite3VtabLoc
29536 6b 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  k(sqlite3_vtab *
29537 70 56 74 61 62 29 7b 0a 20 20 70 56 74 61 62 2d  pVtab){.  pVtab-
29538 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a  >nRef++;.}../*.*
29539 2a 20 55 6e 6c 6f 63 6b 20 61 20 76 69 72 74 75  * Unlock a virtu
2953a 61 6c 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20  al table.  When 
2953b 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73  the last lock is
2953c 20 72 65 6d 6f 76 65 64 2c 0a 2a 2a 20 64 69 73   removed,.** dis
2953d 63 6f 6e 6e 65 63 74 20 74 68 65 20 76 69 72 74  connect the virt
2953e 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ual table..*/.SQ
2953f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
29540 64 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c  d sqlite3VtabUnl
29541 6f 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ock(sqlite3 *db,
29542 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
29543 56 74 61 62 29 7b 0a 20 20 70 56 74 61 62 2d 3e  Vtab){.  pVtab->
29544 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74  nRef--;.  assert
29545 28 64 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20  (db);.  assert( 
29546 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
29547 63 6b 4f 6b 28 64 62 29 20 29 3b 0a 20 20 69 66  ckOk(db) );.  if
29548 28 20 70 56 74 61 62 2d 3e 6e 52 65 66 3d 3d 30  ( pVtab->nRef==0
29549 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
2954a 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41  magic==SQLITE_MA
2954b 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  GIC_BUSY ){.    
2954c 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2954d 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
2954e 20 20 20 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75      pVtab->pModu
2954f 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28  le->xDisconnect(
29550 70 56 74 61 62 29 3b 0a 20 20 20 20 20 20 28 76  pVtab);.      (v
29551 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
29552 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d 20 65  yOn(db);.    } e
29553 6c 73 65 20 7b 0a 20 20 20 20 20 20 70 56 74 61  lse {.      pVta
29554 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73  b->pModule->xDis
29555 63 6f 6e 6e 65 63 74 28 70 56 74 61 62 29 3b 0a  connect(pVtab);.
29556 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
29557 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20 61 6e 64  ** Clear any and
29558 20 61 6c 6c 20 76 69 72 74 75 61 6c 2d 74 61 62   all virtual-tab
29559 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  le information f
2955a 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 72 65  rom the Table re
2955b 63 6f 72 64 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  cord..** This ro
2955c 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
2955d 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6a 75   for example, ju
2955e 73 74 20 62 65 66 6f 72 65 20 64 65 6c 65 74 69  st before deleti
2955f 6e 67 20 74 68 65 20 54 61 62 6c 65 0a 2a 2a 20  ng the Table.** 
29560 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54  record..*/.SQLIT
29561 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
29562 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28  qlite3VtabClear(
29563 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 73 71 6c  Table *p){.  sql
29564 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
29565 20 3d 20 70 2d 3e 70 56 74 61 62 3b 0a 20 20 73   = p->pVtab;.  s
29566 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
29567 64 62 3b 0a 20 20 69 66 28 20 70 56 74 61 62 20  db;.  if( pVtab 
29568 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
29569 2d 3e 70 4d 6f 64 20 26 26 20 70 2d 3e 70 4d 6f  ->pMod && p->pMo
2956a 64 2d 3e 70 4d 6f 64 75 6c 65 20 29 3b 0a 20 20  d->pModule );.  
2956b 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
2956c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a  ock(db, pVtab);.
2956d 20 20 20 20 70 2d 3e 70 56 74 61 62 20 3d 20 30      p->pVtab = 0
2956e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61  ;.  }.  if( p->a
2956f 7a 4d 6f 64 75 6c 65 41 72 67 20 29 7b 0a 20 20  zModuleArg ){.  
29570 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
29571 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d 6f 64 75  (i=0; i<p->nModu
29572 6c 65 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20  leArg; i++){.   
29573 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
29574 28 64 62 2c 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65  (db, p->azModule
29575 41 72 67 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  Arg[i]);.    }. 
29576 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
29577 28 64 62 2c 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65  (db, p->azModule
29578 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  Arg);.  }.}../*.
29579 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 6d 6f 64  ** Add a new mod
2957a 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ule argument to 
2957b 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65  pTable->azModule
2957c 41 72 67 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 74  Arg[]..** The st
2957d 72 69 6e 67 20 69 73 20 6e 6f 74 20 63 6f 70 69  ring is not copi
2957e 65 64 20 2d 20 74 68 65 20 70 6f 69 6e 74 65 72  ed - the pointer
2957f 20 69 73 20 73 74 6f 72 65 64 2e 20 20 54 68 65   is stored.  The
29580 0a 2a 2a 20 73 74 72 69 6e 67 20 77 69 6c 6c 20  .** string will 
29581 62 65 20 66 72 65 65 64 20 61 75 74 6f 6d 61 74  be freed automat
29582 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20  ically when the 
29583 74 61 62 6c 65 20 69 73 0a 2a 2a 20 64 65 6c 65  table is.** dele
29584 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ted..*/.static v
29585 6f 69 64 20 61 64 64 4d 6f 64 75 6c 65 41 72 67  oid addModuleArg
29586 75 6d 65 6e 74 28 73 71 6c 69 74 65 33 20 2a 64  ument(sqlite3 *d
29587 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65  b, Table *pTable
29588 2c 20 63 68 61 72 20 2a 7a 41 72 67 29 7b 0a 20  , char *zArg){. 
29589 20 69 6e 74 20 69 20 3d 20 70 54 61 62 6c 65 2d   int i = pTable-
2958a 3e 6e 4d 6f 64 75 6c 65 41 72 67 2b 2b 3b 0a 20  >nModuleArg++;. 
2958b 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69   int nBytes = si
2958c 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 28 31 2b  zeof(char *)*(1+
2958d 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41  pTable->nModuleA
2958e 72 67 29 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a  rg);.  char **az
2958f 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 61 7a 4d  ModuleArg;.  azM
29590 6f 64 75 6c 65 41 72 67 20 3d 20 73 71 6c 69 74  oduleArg = sqlit
29591 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20  e3DbRealloc(db, 
29592 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65  pTable->azModule
29593 41 72 67 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20  Arg, nBytes);.  
29594 69 66 28 20 61 7a 4d 6f 64 75 6c 65 41 72 67 3d  if( azModuleArg=
29595 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b  =0 ){.    int j;
29596 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
29597 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73  i; j++){.      s
29598 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
29599 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c   pTable->azModul
2959a 65 41 72 67 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a  eArg[j]);.    }.
2959b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2959c 65 28 64 62 2c 20 7a 41 72 67 29 3b 0a 20 20 20  e(db, zArg);.   
2959d 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2959e 62 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64  b, pTable->azMod
2959f 75 6c 65 41 72 67 29 3b 0a 20 20 20 20 70 54 61  uleArg);.    pTa
295a0 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20  ble->nModuleArg 
295a1 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
295a2 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 5d    azModuleArg[i]
295a3 20 3d 20 7a 41 72 67 3b 0a 20 20 20 20 61 7a 4d   = zArg;.    azM
295a4 6f 64 75 6c 65 41 72 67 5b 69 2b 31 5d 20 3d 20  oduleArg[i+1] = 
295a5 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d  0;.  }.  pTable-
295a6 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 3d 20 61  >azModuleArg = a
295a7 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 7d 0a 0a 2f  zModuleArg;.}../
295a8 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20  *.** The parser 
295a9 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
295aa 6e 65 20 77 68 65 6e 20 69 74 20 66 69 72 73 74  ne when it first
295ab 20 73 65 65 73 20 61 20 43 52 45 41 54 45 20 56   sees a CREATE V
295ac 49 52 54 55 41 4c 20 54 41 42 4c 45 0a 2a 2a 20  IRTUAL TABLE.** 
295ad 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
295ae 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 68 61 73 20  module name has 
295af 62 65 65 6e 20 70 61 72 73 65 64 2c 20 62 75 74  been parsed, but
295b0 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 6c 69   the optional li
295b1 73 74 0a 2a 2a 20 6f 66 20 70 61 72 61 6d 65 74  st.** of paramet
295b2 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20  ers that follow 
295b3 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  the module name 
295b4 61 72 65 20 73 74 69 6c 6c 20 70 65 6e 64 69 6e  are still pendin
295b5 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
295b6 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
295b7 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 65 28  3VtabBeginParse(
295b8 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
295b9 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
295ba 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
295bb 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20   Token *pName1, 
295bc 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
295bd 66 20 6e 65 77 20 74 61 62 6c 65 2c 20 6f 72 20  f new table, or 
295be 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f  database name */
295bf 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32  .  Token *pName2
295c0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ,        /* Name
295c1 20 6f 66 20 6e 65 77 20 74 61 62 6c 65 20 6f 72   of new table or
295c2 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e   NULL */.  Token
295c3 20 2a 70 4d 6f 64 75 6c 65 4e 61 6d 65 20 20 20   *pModuleName   
295c4 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
295c5 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 65 20 76  module for the v
295c6 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
295c7 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  ){.  int iDb;   
295c8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
295c9 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20 74  e database the t
295ca 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 63 72  able is being cr
295cb 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 54 61  eated in */.  Ta
295cc 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20 20  ble *pTable;    
295cd 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 76      /* The new v
295ce 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
295cf 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
295d0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
295d1 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
295d2 2f 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  /..  if( pParse-
295d3 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  >db->flags & SQL
295d4 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 65 20  ITE_SharedCache 
295d5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
295d6 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
295d7 43 61 6e 6e 6f 74 20 75 73 65 20 76 69 72 74 75  Cannot use virtu
295d8 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 73 68 61  al tables in sha
295d9 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 22 29  red-cache mode")
295da 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
295db 7d 0a 0a 20 20 73 71 6c 69 74 65 33 53 74 61 72  }..  sqlite3Star
295dc 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  tTable(pParse, p
295dd 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 30  Name1, pName2, 0
295de 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 70 54  , 0, 1, 0);.  pT
295df 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 70  able = pParse->p
295e0 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20  NewTable;.  if( 
295e1 70 54 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 50 61  pTable==0 || pPa
295e2 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75  rse->nErr ) retu
295e3 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d  rn;.  assert( 0=
295e4 3d 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 20  =pTable->pIndex 
295e5 29 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73  );..  db = pPars
295e6 65 2d 3e 64 62 3b 0a 20 20 69 44 62 20 3d 20 73  e->db;.  iDb = s
295e7 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
295e8 64 65 78 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  dex(db, pTable->
295e9 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65  pSchema);.  asse
295ea 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 20  rt( iDb>=0 );.. 
295eb 20 70 54 61 62 6c 65 2d 3e 74 61 62 46 6c 61 67   pTable->tabFlag
295ec 73 20 7c 3d 20 54 46 5f 56 69 72 74 75 61 6c 3b  s |= TF_Virtual;
295ed 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75  .  pTable->nModu
295ee 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20 61 64 64  leArg = 0;.  add
295ef 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64  ModuleArgument(d
295f0 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74  b, pTable, sqlit
295f1 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
295f2 64 62 2c 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 29  db, pModuleName)
295f3 29 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72  );.  addModuleAr
295f4 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c  gument(db, pTabl
295f5 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72 44  e, sqlite3DbStrD
295f6 75 70 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 69  up(db, db->aDb[i
295f7 44 62 5d 2e 7a 4e 61 6d 65 29 29 3b 0a 20 20 61  Db].zName));.  a
295f8 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74  ddModuleArgument
295f9 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c  (db, pTable, sql
295fa 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
295fb 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 29   pTable->zName))
295fc 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d  ;.  pParse->sNam
295fd 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e 74 29  eToken.n = (int)
295fe 28 26 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e 7a  (&pModuleName->z
295ff 5b 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e 6e 5d  [pModuleName->n]
29600 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a 29 3b 0a 0a   - pName1->z);..
29601 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
29602 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
29603 4e 0a 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20  N.  /* Creating 
29604 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
29605 69 6e 76 6f 6b 65 73 20 74 68 65 20 61 75 74 68  invokes the auth
29606 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61  orization callba
29607 63 6b 20 74 77 69 63 65 2e 0a 20 20 2a 2a 20 54  ck twice..  ** T
29608 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74  he first invocat
29609 69 6f 6e 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70  ion, to obtain p
2960a 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 49 4e 53  ermission to INS
2960b 45 52 54 20 61 20 72 6f 77 20 69 6e 74 6f 20 74  ERT a row into t
2960c 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d  he.  ** sqlite_m
2960d 61 73 74 65 72 20 74 61 62 6c 65 2c 20 68 61 73  aster table, has
2960e 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d 61   already been ma
2960f 64 65 20 62 79 20 73 71 6c 69 74 65 33 53 74 61  de by sqlite3Sta
29610 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 2a 2a 20  rtTable()..  ** 
29611 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 2c  The second call,
29612 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d 69   to obtain permi
29613 73 73 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20  ssion to create 
29614 74 68 65 20 74 61 62 6c 65 2c 20 69 73 20 6d 61  the table, is ma
29615 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69  de now..  */.  i
29616 66 28 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64  f( pTable->azMod
29617 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 73 71  uleArg ){.    sq
29618 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
29619 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 43 52  Parse, SQLITE_CR
2961a 45 41 54 45 5f 56 54 41 42 4c 45 2c 20 70 54 61  EATE_VTABLE, pTa
2961b 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20  ble->zName, .   
2961c 20 20 20 20 20 20 20 20 20 70 54 61 62 6c 65 2d           pTable-
2961d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 2c  >azModuleArg[0],
2961e 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62   pParse->db->aDb
2961f 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
29620 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
29621 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74  * This routine t
29622 61 6b 65 73 20 74 68 65 20 6d 6f 64 75 6c 65 20  akes the module 
29623 61 72 67 75 6d 65 6e 74 20 74 68 61 74 20 68 61  argument that ha
29624 73 20 62 65 65 6e 20 61 63 63 75 6d 75 6c 61 74  s been accumulat
29625 69 6e 67 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65  ing.** in pParse
29626 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64 20 61 70 70  ->zArg[] and app
29627 65 6e 64 73 20 69 74 20 74 6f 20 74 68 65 20 6c  ends it to the l
29628 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ist of arguments
29629 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 69 72 74 75   on the.** virtu
2962a 61 6c 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74  al table current
2962b 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ly under constru
2962c 63 74 69 6f 6e 20 69 6e 20 70 50 61 72 73 65 2d  ction in pParse-
2962d 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  >pTable..*/.stat
2962e 69 63 20 76 6f 69 64 20 61 64 64 41 72 67 75 6d  ic void addArgum
2962f 65 6e 74 54 6f 56 74 61 62 28 50 61 72 73 65 20  entToVtab(Parse 
29630 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 66 28 20  *pParse){.  if( 
29631 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20 26  pParse->sArg.z &
29632 26 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  & pParse->pNewTa
29633 62 6c 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ble ){.    const
29634 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73   char *z = (cons
29635 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d 3e  t char*)pParse->
29636 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69 6e 74 20  sArg.z;.    int 
29637 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41 72 67  n = pParse->sArg
29638 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  .n;.    sqlite3 
29639 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2963a 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75 6c 65 41  ;.    addModuleA
2963b 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 50 61 72  rgument(db, pPar
2963c 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20 73  se->pNewTable, s
2963d 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
2963e 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20 20 7d 0a  db, z, n));.  }.
2963f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72  }../*.** The par
29640 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
29641 6f 75 74 69 6e 65 20 61 66 74 65 72 20 74 68 65  outine after the
29642 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
29643 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 0a  TABLE statement.
29644 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70  ** has been comp
29645 6c 65 74 65 6c 79 20 70 61 72 73 65 64 2e 0a 2a  letely parsed..*
29646 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
29647 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
29648 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61 72  bFinishParse(Par
29649 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
2964a 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54 61 62 6c  n *pEnd){.  Tabl
2964b 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
2964c 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69  /* The table bei
2964d 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20 2a  ng constructed *
2964e 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
2964f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
29650 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
29651 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d  on */.  char *zM
29652 6f 64 75 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54  odule;      /* T
29653 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 6f  he module name o
29654 66 20 74 68 65 20 74 61 62 6c 65 3a 20 55 53 49  f the table: USI
29655 4e 47 20 6d 6f 64 75 6c 65 6e 61 6d 65 20 2a 2f  NG modulename */
29656 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20  .  Module *pMod 
29657 3d 20 30 3b 0a 0a 20 20 61 64 64 41 72 67 75 6d  = 0;..  addArgum
29658 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73 65  entToVtab(pParse
29659 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41 72  );.  pParse->sAr
2965a 67 2e 7a 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c  g.z = 0;..  /* L
2965b 6f 6f 6b 75 70 20 74 68 65 20 6d 6f 64 75 6c 65  ookup the module
2965c 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 70 54 61 62   name. */.  pTab
2965d 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
2965e 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61 62  able;.  if( pTab
2965f 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
29660 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
29661 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 4d 6f  .  if( pTab->nMo
29662 64 75 6c 65 41 72 67 3c 31 20 29 20 72 65 74 75  duleArg<1 ) retu
29663 72 6e 3b 0a 20 20 7a 4d 6f 64 75 6c 65 20 3d 20  rn;.  zModule = 
29664 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  pTab->azModuleAr
29665 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20 28  g[0];.  pMod = (
29666 4d 6f 64 75 6c 65 2a 29 73 71 6c 69 74 65 33 48  Module*)sqlite3H
29667 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d 6f  ashFind(&db->aMo
29668 64 75 6c 65 2c 20 7a 4d 6f 64 75 6c 65 2c 0a 20  dule, zModule,. 
29669 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2966a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2966b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2966c 28 7a 4d 6f 64 75 6c 65 29 29 3b 0a 20 20 70 54  (zModule));.  pT
2966d 61 62 2d 3e 70 4d 6f 64 20 3d 20 70 4d 6f 64 3b  ab->pMod = pMod;
2966e 0a 20 20 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  .  .  /* If the 
2966f 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
29670 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69  ABLE statement i
29671 73 20 62 65 69 6e 67 20 65 6e 74 65 72 65 64 20  s being entered 
29672 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 66 69 72  for the.  ** fir
29673 73 74 20 74 69 6d 65 20 28 69 6e 20 6f 74 68 65  st time (in othe
29674 72 20 77 6f 72 64 73 20 69 66 20 74 68 65 20 76  r words if the v
29675 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20  irtual table is 
29676 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 0a 20  actually being. 
29677 20 2a 2a 20 63 72 65 61 74 65 64 20 6e 6f 77 20   ** created now 
29678 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74 20  instead of just 
29679 62 65 69 6e 67 20 72 65 61 64 20 6f 75 74 20 6f  being read out o
2967a 66 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 29  f sqlite_master)
2967b 20 74 68 65 6e 0a 20 20 2a 2a 20 64 6f 20 61 64   then.  ** do ad
2967c 64 69 74 69 6f 6e 61 6c 20 69 6e 69 74 69 61 6c  ditional initial
2967d 69 7a 61 74 69 6f 6e 20 77 6f 72 6b 20 61 6e 64  ization work and
2967e 20 73 74 6f 72 65 20 74 68 65 20 73 74 61 74 65   store the state
2967f 6d 65 6e 74 20 74 65 78 74 0a 20 20 2a 2a 20 69  ment text.  ** i
29680 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
29681 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  ter table..  */.
29682 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
29683 62 75 73 79 20 29 7b 0a 20 20 20 20 63 68 61 72  busy ){.    char
29684 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 63 68 61   *zStmt;.    cha
29685 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20 20 20 69  r *zWhere;.    i
29686 6e 74 20 69 44 62 3b 0a 20 20 20 20 56 64 62 65  nt iDb;.    Vdbe
29687 20 2a 76 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d   *v;..    /* Com
29688 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 74  pute the complet
29689 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52  e text of the CR
2968a 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
2968b 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  LE statement */.
2968c 20 20 20 20 69 66 28 20 70 45 6e 64 20 29 7b 0a      if( pEnd ){.
2968d 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4e        pParse->sN
2968e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e  ameToken.n = (in
2968f 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61  t)(pEnd->z - pPa
29690 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
29691 7a 29 20 2b 20 70 45 6e 64 2d 3e 6e 3b 0a 20 20  z) + pEnd->n;.  
29692 20 20 7d 0a 20 20 20 20 7a 53 74 6d 74 20 3d 20    }.    zStmt = 
29693 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
29694 62 2c 20 22 43 52 45 41 54 45 20 56 49 52 54 55  b, "CREATE VIRTU
29695 41 4c 20 54 41 42 4c 45 20 25 54 22 2c 20 26 70  AL TABLE %T", &p
29696 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
29697 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c  n);..    /* A sl
29698 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ot for the recor
29699 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  d has already be
2969a 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  en allocated in 
2969b 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49  the .    ** SQLI
2969c 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e  TE_MASTER table.
2969d 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74    We just need t
2969e 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c  o update that sl
2969f 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20  ot with all.    
296a0 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  ** the informati
296a1 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74  on we've collect
296a2 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20  ed.  .    **.   
296a3 20 2a 2a 20 54 68 65 20 56 4d 20 72 65 67 69 73   ** The VM regis
296a4 74 65 72 20 6e 75 6d 62 65 72 20 70 50 61 72 73  ter number pPars
296a5 65 2d 3e 72 65 67 52 6f 77 69 64 20 68 6f 6c 64  e->regRowid hold
296a6 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61  s the rowid of a
296a7 6e 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 69  n.    ** entry i
296a8 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
296a9 74 65 72 20 74 61 62 6c 65 20 74 68 74 20 77 61  ter table tht wa
296aa 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 68  s created for th
296ab 69 73 20 76 74 61 62 0a 20 20 20 20 2a 2a 20 62  is vtab.    ** b
296ac 79 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61  y sqlite3StartTa
296ad 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  ble()..    */.  
296ae 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
296af 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
296b0 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
296b1 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
296b2 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
296b3 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51        "UPDATE %Q
296b4 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22  .%s ".         "
296b5 53 45 54 20 74 79 70 65 3d 27 74 61 62 6c 65 27  SET type='table'
296b6 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e  , name=%Q, tbl_n
296b7 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65  ame=%Q, rootpage
296b8 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20  =0, sql=%Q ".   
296b9 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 64      "WHERE rowid
296ba 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d  =#%d",.      db-
296bb 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
296bc 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
296bd 62 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  b),.      pTab->
296be 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 54 61  zName,.      pTa
296bf 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  b->zName,.      
296c0 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61  zStmt,.      pPa
296c1 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20  rse->regRowid.  
296c2 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33    );.    sqlite3
296c3 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74  DbFree(db, zStmt
296c4 29 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74  );.    v = sqlit
296c5 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
296c6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
296c7 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
296c8 65 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 73 71  e, iDb);..    sq
296c9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
296ca 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c  v, OP_Expire, 0,
296cb 20 30 29 3b 0a 20 20 20 20 7a 57 68 65 72 65 20   0);.    zWhere 
296cc 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
296cd 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22  (db, "name='%q'"
296ce 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
296cf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
296d0 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73  ddOp4(v, OP_Pars
296d1 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 31 2c  eSchema, iDb, 1,
296d2 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44   0, zWhere, P4_D
296d3 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 73 71 6c  YNAMIC);.    sql
296d4 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
296d5 2c 20 4f 50 5f 56 43 72 65 61 74 65 2c 20 69 44  , OP_VCreate, iD
296d6 62 2c 20 30 2c 20 30 2c 20 0a 20 20 20 20 20 20  b, 0, 0, .      
296d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
296d8 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20     pTab->zName, 
296d9 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
296da 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20 2b 20 31  pTab->zName) + 1
296db 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
296dc 77 65 20 61 72 65 20 72 65 72 65 61 64 69 6e 67  we are rereading
296dd 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
296de 65 72 20 74 61 62 6c 65 20 63 72 65 61 74 65 20  er table create 
296df 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20  the in-memory.  
296e0 2a 2a 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65  ** record of the
296e1 20 74 61 62 6c 65 2e 20 49 66 20 74 68 65 20 6d   table. If the m
296e2 6f 64 75 6c 65 20 68 61 73 20 61 6c 72 65 61 64  odule has alread
296e3 79 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  y been registere
296e4 64 2c 0a 20 20 2a 2a 20 61 6c 73 6f 20 63 61 6c  d,.  ** also cal
296e5 6c 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 20 6d  l the xConnect m
296e6 65 74 68 6f 64 20 68 65 72 65 2e 0a 20 20 2a 2f  ethod here..  */
296e7 0a 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 54 61  .  else {.    Ta
296e8 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 53  ble *pOld;.    S
296e9 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
296ea 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
296eb 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
296ec 7a 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e  zName = pTab->zN
296ed 61 6d 65 3b 0a 20 20 20 20 69 6e 74 20 6e 4e 61  ame;.    int nNa
296ee 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  me = sqlite3Strl
296ef 65 6e 33 30 28 7a 4e 61 6d 65 29 20 2b 20 31 3b  en30(zName) + 1;
296f0 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69  .    pOld = sqli
296f1 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70  te3HashInsert(&p
296f2 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c  Schema->tblHash,
296f3 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70   zName, nName, p
296f4 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20 70 4f  Tab);.    if( pO
296f5 6c 64 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  ld ){.      db->
296f6 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
296f7 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
296f8 70 54 61 62 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f  pTab==pOld );  /
296f9 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61  * Malloc must ha
296fa 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65  ve failed inside
296fb 20 48 61 73 68 49 6e 73 65 72 74 28 29 20 2a 2f   HashInsert() */
296fc 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
296fd 20 20 20 7d 0a 20 20 20 20 70 53 63 68 65 6d 61     }.    pSchema
296fe 2d 3e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64  ->db = pParse->d
296ff 62 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70  b;.    pParse->p
29700 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20  NewTable = 0;.  
29701 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  }.}../*.** The p
29702 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
29703 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74   routine when it
29704 20 73 65 65 73 20 74 68 65 20 66 69 72 73 74 20   sees the first 
29705 74 6f 6b 65 6e 0a 2a 2a 20 6f 66 20 61 6e 20 61  token.** of an a
29706 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d  rgument to the m
29707 6f 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20  odule name in a 
29708 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
29709 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ABLE statement..
2970a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2970b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2970c 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 20  abArgInit(Parse 
2970d 2a 70 50 61 72 73 65 29 7b 0a 20 20 61 64 64 41  *pParse){.  addA
2970e 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 70 50  rgumentToVtab(pP
2970f 61 72 73 65 29 3b 0a 20 20 70 50 61 72 73 65 2d  arse);.  pParse-
29710 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0a 20 20 70  >sArg.z = 0;.  p
29711 50 61 72 73 65 2d 3e 73 41 72 67 2e 6e 20 3d 20  Parse->sArg.n = 
29712 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
29713 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
29714 73 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 65 61  s routine for ea
29715 63 68 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74  ch token after t
29716 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a  he first token.*
29717 2a 20 69 6e 20 61 6e 20 61 72 67 75 6d 65 6e 74  * in an argument
29718 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e   to the module n
29719 61 6d 65 20 69 6e 20 61 20 43 52 45 41 54 45 20  ame in a CREATE 
2971a 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74  VIRTUAL TABLE st
2971b 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
2971c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2971d 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78  sqlite3VtabArgEx
2971e 74 65 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72  tend(Parse *pPar
2971f 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 29 7b 0a 20  se, Token *p){. 
29720 20 54 6f 6b 65 6e 20 2a 70 41 72 67 20 3d 20 26   Token *pArg = &
29721 70 50 61 72 73 65 2d 3e 73 41 72 67 3b 0a 20 20  pParse->sArg;.  
29722 69 66 28 20 70 41 72 67 2d 3e 7a 3d 3d 30 20 29  if( pArg->z==0 )
29723 7b 0a 20 20 20 20 70 41 72 67 2d 3e 7a 20 3d 20  {.    pArg->z = 
29724 70 2d 3e 7a 3b 0a 20 20 20 20 70 41 72 67 2d 3e  p->z;.    pArg->
29725 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 65 6c 73  n = p->n;.  }els
29726 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70 41  e{.    assert(pA
29727 72 67 2d 3e 7a 20 3c 20 70 2d 3e 7a 29 3b 0a 20  rg->z < p->z);. 
29728 20 20 20 70 41 72 67 2d 3e 6e 20 3d 20 28 69 6e     pArg->n = (in
29729 74 29 28 26 70 2d 3e 7a 5b 70 2d 3e 6e 5d 20 2d  t)(&p->z[p->n] -
2972a 20 70 41 72 67 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d   pArg->z);.  }.}
2972b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61  ../*.** Invoke a
2972c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63   virtual table c
2972d 6f 6e 73 74 72 75 63 74 6f 72 20 28 65 69 74 68  onstructor (eith
2972e 65 72 20 78 43 72 65 61 74 65 20 6f 72 20 78 43  er xCreate or xC
2972f 6f 6e 6e 65 63 74 29 2e 20 54 68 65 0a 2a 2a 20  onnect). The.** 
29730 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66  pointer to the f
29731 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b  unction to invok
29732 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  e is passed as t
29733 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  he fourth parame
29734 74 65 72 0a 2a 2a 20 74 6f 20 74 68 69 73 20 70  ter.** to this p
29735 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61  rocedure..*/.sta
29736 74 69 63 20 69 6e 74 20 76 74 61 62 43 61 6c 6c  tic int vtabCall
29737 43 6f 6e 73 74 72 75 63 74 6f 72 28 0a 20 20 73  Constructor(.  s
29738 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 54  qlite3 *db, .  T
29739 61 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20 4d 6f  able *pTab,.  Mo
2973a 64 75 6c 65 20 2a 70 4d 6f 64 2c 0a 20 20 69 6e  dule *pMod,.  in
2973b 74 20 28 2a 78 43 6f 6e 73 74 72 75 63 74 29 28  t (*xConstruct)(
2973c 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69  sqlite3*,void*,i
2973d 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f  nt,const char*co
2973e 6e 73 74 2a 2c 73 71 6c 69 74 65 33 5f 76 74 61  nst*,sqlite3_vta
2973f 62 2a 2a 2c 63 68 61 72 2a 2a 29 2c 0a 20 20 63  b**,char**),.  c
29740 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20  har **pzErr.){. 
29741 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 72   int rc;.  int r
29742 63 32 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  c2;.  sqlite3_vt
29743 61 62 20 2a 70 56 74 61 62 20 3d 20 30 3b 0a 20  ab *pVtab = 0;. 
29744 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
29745 73 74 2a 61 7a 41 72 67 20 3d 20 28 63 6f 6e 73  st*azArg = (cons
29746 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 29 70  t char *const*)p
29747 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  Tab->azModuleArg
29748 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20 3d 20 70  ;.  int nArg = p
29749 54 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b  Tab->nModuleArg;
2974a 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20  .  char *zErr = 
2974b 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4d 6f 64 75  0;.  char *zModu
2974c 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  leName = sqlite3
2974d 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22  MPrintf(db, "%s"
2974e 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2974f 0a 20 20 69 66 28 20 21 7a 4d 6f 64 75 6c 65 4e  .  if( !zModuleN
29750 61 6d 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ame ){.    retur
29751 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
29752 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 21    }..  assert( !
29753 64 62 2d 3e 70 56 54 61 62 20 29 3b 0a 20 20 61  db->pVTab );.  a
29754 73 73 65 72 74 28 20 78 43 6f 6e 73 74 72 75 63  ssert( xConstruc
29755 74 20 29 3b 0a 0a 20 20 64 62 2d 3e 70 56 54 61  t );..  db->pVTa
29756 62 20 3d 20 70 54 61 62 3b 0a 20 20 72 63 20 3d  b = pTab;.  rc =
29757 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
29758 66 28 64 62 29 3b 0a 20 20 61 73 73 65 72 74 28  f(db);.  assert(
29759 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2975a 3b 0a 20 20 72 63 20 3d 20 78 43 6f 6e 73 74 72  ;.  rc = xConstr
2975b 75 63 74 28 64 62 2c 20 70 4d 6f 64 2d 3e 70 41  uct(db, pMod->pA
2975c 75 78 2c 20 6e 41 72 67 2c 20 61 7a 41 72 67 2c  ux, nArg, azArg,
2975d 20 26 70 56 74 61 62 2c 20 26 7a 45 72 72 29 3b   &pVtab, &zErr);
2975e 0a 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33  .  rc2 = sqlite3
2975f 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
29760 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
29761 4b 20 26 26 20 70 56 74 61 62 20 29 7b 0a 20 20  K && pVtab ){.  
29762 20 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65    pVtab->pModule
29763 20 3d 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65   = pMod->pModule
29764 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e 6e 52 65  ;.    pVtab->nRe
29765 66 20 3d 20 31 3b 0a 20 20 20 20 70 54 61 62 2d  f = 1;.    pTab-
29766 3e 70 56 74 61 62 20 3d 20 70 56 74 61 62 3b 0a  >pVtab = pVtab;.
29767 20 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54    }..  if( SQLIT
29768 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK!=rc ){.    
29769 69 66 28 20 7a 45 72 72 3d 3d 30 20 29 7b 0a 20  if( zErr==0 ){. 
2976a 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71       *pzErr = sq
2976b 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2976c 20 22 76 74 61 62 6c 65 20 63 6f 6e 73 74 72 75   "vtable constru
2976d 63 74 6f 72 20 66 61 69 6c 65 64 3a 20 25 73 22  ctor failed: %s"
2976e 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a  , zModuleName);.
2976f 20 20 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20      }else {.    
29770 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
29771 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
29772 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  s", zErr);.     
29773 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
29774 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a  b, zErr);.    }.
29775 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e    }else if( db->
29776 70 56 54 61 62 20 29 7b 0a 20 20 20 20 63 6f 6e  pVTab ){.    con
29777 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
29778 20 3d 20 22 76 74 61 62 6c 65 20 63 6f 6e 73 74   = "vtable const
29779 72 75 63 74 6f 72 20 64 69 64 20 6e 6f 74 20 64  ructor did not d
2977a 65 63 6c 61 72 65 20 73 63 68 65 6d 61 3a 20 25  eclare schema: %
2977b 73 22 3b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d  s";.    *pzErr =
2977c 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2977d 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54 61  db, zFormat, pTa
2977e 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72  b->zName);.    r
2977f 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
29780 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 72 63 3d  ;.  } .  if( rc=
29781 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
29782 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 7d 0a    rc = rc2;.  }.
29783 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30 3b    db->pVTab = 0;
29784 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
29785 28 64 62 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65  (db, zModuleName
29786 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 65 76 65 72  );..  /* If ever
29787 79 74 68 69 6e 67 20 77 65 6e 74 20 61 63 63 6f  ything went acco
29788 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2c 20 6c  rding to plan, l
29789 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
2978a 63 6f 6c 75 6d 6e 73 0a 20 20 2a 2a 20 6f 66 20  columns.  ** of 
2978b 74 68 65 20 74 61 62 6c 65 20 74 6f 20 73 65 65  the table to see
2978c 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 6d 20   if any of them 
2978d 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 6f 6b 65  contain the toke
2978e 6e 20 22 68 69 64 64 65 6e 22 2e 0a 20 20 2a 2a  n "hidden"..  **
2978f 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68 65 20   If so, set the 
29790 43 6f 6c 75 6d 6e 2e 69 73 48 69 64 64 65 6e 20  Column.isHidden 
29791 66 6c 61 67 20 61 6e 64 20 72 65 6d 6f 76 65 20  flag and remove 
29792 74 68 65 20 74 6f 6b 65 6e 20 66 72 6f 6d 0a 20  the token from. 
29793 20 2a 2a 20 74 68 65 20 74 79 70 65 20 73 74 72   ** the type str
29794 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ing..  */.  if( 
29795 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
29796 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20  .    int iCol;. 
29797 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
29798 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  Col<pTab->nCol; 
29799 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 63  iCol++){.      c
2979a 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 70 54 61  har *zType = pTa
2979b 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54  b->aCol[iCol].zT
2979c 79 70 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ype;.      int n
2979d 54 79 70 65 3b 0a 20 20 20 20 20 20 69 6e 74 20  Type;.      int 
2979e 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  i = 0;.      if(
2979f 20 21 7a 54 79 70 65 20 29 20 63 6f 6e 74 69 6e   !zType ) contin
297a0 75 65 3b 0a 20 20 20 20 20 20 6e 54 79 70 65 20  ue;.      nType 
297a1 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
297a2 30 28 7a 54 79 70 65 29 3b 0a 20 20 20 20 20 20  0(zType);.      
297a3 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
297a4 43 6d 70 28 22 68 69 64 64 65 6e 22 2c 20 7a 54  Cmp("hidden", zT
297a5 79 70 65 2c 20 36 29 20 7c 7c 20 28 7a 54 79 70  ype, 6) || (zTyp
297a6 65 5b 36 5d 20 26 26 20 7a 54 79 70 65 5b 36 5d  e[6] && zType[6]
297a7 21 3d 27 20 27 29 20 29 7b 0a 20 20 20 20 20 20  !=' ') ){.      
297a8 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 79    for(i=0; i<nTy
297a9 70 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pe; i++){.      
297aa 20 20 20 20 69 66 28 20 28 30 3d 3d 73 71 6c 69      if( (0==sqli
297ab 74 65 33 53 74 72 4e 49 43 6d 70 28 22 20 68 69  te3StrNICmp(" hi
297ac 64 64 65 6e 22 2c 20 26 7a 54 79 70 65 5b 69 5d  dden", &zType[i]
297ad 2c 20 37 29 29 0a 20 20 20 20 20 20 20 20 20 20  , 7)).          
297ae 20 26 26 20 28 7a 54 79 70 65 5b 69 2b 37 5d 3d   && (zType[i+7]=
297af 3d 27 5c 30 27 20 7c 7c 20 7a 54 79 70 65 5b 69  ='\0' || zType[i
297b0 2b 37 5d 3d 3d 27 20 27 29 0a 20 20 20 20 20 20  +7]==' ').      
297b1 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
297b2 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20     i++;.        
297b3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
297b4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
297b5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
297b6 66 28 20 69 3c 6e 54 79 70 65 20 29 7b 0a 20 20  f( i<nType ){.  
297b7 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
297b8 20 20 20 20 20 69 6e 74 20 6e 44 65 6c 20 3d 20       int nDel = 
297b9 36 20 2b 20 28 7a 54 79 70 65 5b 69 2b 36 5d 20  6 + (zType[i+6] 
297ba 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20  ? 1 : 0);.      
297bb 20 20 66 6f 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44    for(j=i; (j+nD
297bc 65 6c 29 3c 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29  el)<=nType; j++)
297bd 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70  {.          zTyp
297be 65 5b 6a 5d 20 3d 20 7a 54 79 70 65 5b 6a 2b 6e  e[j] = zType[j+n
297bf 44 65 6c 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Del];.        }.
297c0 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 79 70          if( zTyp
297c1 65 5b 69 5d 3d 3d 27 5c 30 27 20 26 26 20 69 3e  e[i]=='\0' && i>
297c2 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  0 ){.          a
297c3 73 73 65 72 74 28 7a 54 79 70 65 5b 69 2d 31 5d  ssert(zType[i-1]
297c4 3d 3d 27 20 27 29 3b 0a 20 20 20 20 20 20 20 20  ==' ');.        
297c5 20 20 7a 54 79 70 65 5b 69 2d 31 5d 20 3d 20 27    zType[i-1] = '
297c6 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  \0';.        }. 
297c7 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f         pTab->aCo
297c8 6c 5b 69 43 6f 6c 5d 2e 69 73 48 69 64 64 65 6e  l[iCol].isHidden
297c9 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
297ca 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
297cb 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
297cc 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  is function is i
297cd 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 70 61  nvoked by the pa
297ce 72 73 65 72 20 74 6f 20 63 61 6c 6c 20 74 68 65  rser to call the
297cf 20 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68   xConnect() meth
297d0 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72  od.** of the vir
297d1 74 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62 2e  tual table pTab.
297d2 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
297d3 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f  urs, an error co
297d4 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 0a  de is returned .
297d5 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ** and an error 
297d6 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 0a  left in pParse..
297d7 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20  **.** This call 
297d8 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 61  is a no-op if ta
297d9 62 6c 65 20 70 54 61 62 20 69 73 20 6e 6f 74 20  ble pTab is not 
297da 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
297db 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
297dc 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74  TE int sqlite3Vt
297dd 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61  abCallConnect(Pa
297de 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
297df 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 4d 6f 64  le *pTab){.  Mod
297e0 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 69 6e 74  ule *pMod;.  int
297e1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
297e2 0a 0a 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c  ..  if( !pTab ||
297e3 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
297e4 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d   & TF_Virtual)==
297e5 30 20 7c 7c 20 70 54 61 62 2d 3e 70 56 74 61 62  0 || pTab->pVtab
297e6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
297e7 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
297e8 20 70 4d 6f 64 20 3d 20 70 54 61 62 2d 3e 70 4d   pMod = pTab->pM
297e9 6f 64 3b 0a 20 20 69 66 28 20 21 70 4d 6f 64 20  od;.  if( !pMod 
297ea 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
297eb 72 20 2a 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61  r *zModule = pTa
297ec 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30  b->azModuleArg[0
297ed 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ];.    sqlite3Er
297ee 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
297ef 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20  no such module: 
297f0 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20  %s", zModule);. 
297f1 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
297f2 52 52 4f 52 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  RROR;.  } else {
297f3 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20  .    char *zErr 
297f4 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
297f5 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
297f6 62 3b 0a 20 20 20 20 72 63 20 3d 20 76 74 61 62  b;.    rc = vtab
297f7 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28  CallConstructor(
297f8 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20  db, pTab, pMod, 
297f9 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  pMod->pModule->x
297fa 43 6f 6e 6e 65 63 74 2c 20 26 7a 45 72 72 29 3b  Connect, &zErr);
297fb 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
297fc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
297fd 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
297fe 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 7a 45  pParse, "%s", zE
297ff 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  rr);.    }.    s
29800 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
29801 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 72   zErr);.  }..  r
29802 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
29803 2a 2a 20 41 64 64 20 74 68 65 20 76 69 72 74 75  ** Add the virtu
29804 61 6c 20 74 61 62 6c 65 20 70 56 74 61 62 20 74  al table pVtab t
29805 6f 20 74 68 65 20 61 72 72 61 79 20 73 71 6c 69  o the array sqli
29806 74 65 33 2e 61 56 54 72 61 6e 73 5b 5d 2e 0a 2a  te3.aVTrans[]..*
29807 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64  /.static int add
29808 54 6f 56 54 72 61 6e 73 28 73 71 6c 69 74 65 33  ToVTrans(sqlite3
29809 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f 76 74   *db, sqlite3_vt
2980a 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 63 6f  ab *pVtab){.  co
2980b 6e 73 74 20 69 6e 74 20 41 52 52 41 59 5f 49 4e  nst int ARRAY_IN
2980c 43 52 20 3d 20 35 3b 0a 0a 20 20 2f 2a 20 47 72  CR = 5;..  /* Gr
2980d 6f 77 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61  ow the sqlite3.a
2980e 56 54 72 61 6e 73 20 61 72 72 61 79 20 69 66 20  VTrans array if 
2980f 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 66  required */.  if
29810 28 20 28 64 62 2d 3e 6e 56 54 72 61 6e 73 25 41  ( (db->nVTrans%A
29811 52 52 41 59 5f 49 4e 43 52 29 3d 3d 30 20 29 7b  RRAY_INCR)==0 ){
29812 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61  .    sqlite3_vta
29813 62 20 2a 2a 61 56 54 72 61 6e 73 3b 0a 20 20 20  b **aVTrans;.   
29814 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69   int nBytes = si
29815 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61  zeof(sqlite3_vta
29816 62 20 2a 29 20 2a 20 28 64 62 2d 3e 6e 56 54 72  b *) * (db->nVTr
29817 61 6e 73 20 2b 20 41 52 52 41 59 5f 49 4e 43 52  ans + ARRAY_INCR
29818 29 3b 0a 20 20 20 20 61 56 54 72 61 6e 73 20 3d  );.    aVTrans =
29819 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
2981a 63 28 64 62 2c 20 28 76 6f 69 64 20 2a 29 64 62  c(db, (void *)db
2981b 2d 3e 61 56 54 72 61 6e 73 2c 20 6e 42 79 74 65  ->aVTrans, nByte
2981c 73 29 3b 0a 20 20 20 20 69 66 28 20 21 61 56 54  s);.    if( !aVT
2981d 72 61 6e 73 20 29 7b 0a 20 20 20 20 20 20 72 65  rans ){.      re
2981e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2981f 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d  M;.    }.    mem
29820 73 65 74 28 26 61 56 54 72 61 6e 73 5b 64 62 2d  set(&aVTrans[db-
29821 3e 6e 56 54 72 61 6e 73 5d 2c 20 30 2c 20 73 69  >nVTrans], 0, si
29822 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61  zeof(sqlite3_vta
29823 62 20 2a 29 2a 41 52 52 41 59 5f 49 4e 43 52 29  b *)*ARRAY_INCR)
29824 3b 0a 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e  ;.    db->aVTran
29825 73 20 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 7d  s = aVTrans;.  }
29826 0a 0a 20 20 2f 2a 20 41 64 64 20 70 56 74 61 62  ..  /* Add pVtab
29827 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73   to the end of s
29828 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 2a  qlite3.aVTrans *
29829 2f 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b  /.  db->aVTrans[
2982a 64 62 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d  db->nVTrans++] =
2982b 20 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65   pVtab;.  sqlite
2982c 33 56 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29  3VtabLock(pVtab)
2982d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2982e 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
2982f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
29830 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76  invoked by the v
29831 64 62 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  dbe to call the 
29832 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a  xCreate method.*
29833 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  * of the virtual
29834 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61   table named zTa
29835 62 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44  b in database iD
29836 62 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  b. .**.** If an 
29837 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70  error occurs, *p
29838 7a 45 72 72 20 69 73 20 73 65 74 20 74 6f 20 70  zErr is set to p
29839 6f 69 6e 74 20 61 6e 20 61 6e 20 45 6e 67 6c 69  oint an an Engli
2983a 73 68 20 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 64  sh language.** d
2983b 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
2983c 65 20 65 72 72 6f 72 20 61 6e 64 20 61 6e 20 53  e error and an S
2983d 51 4c 49 54 45 5f 58 58 58 20 65 72 72 6f 72 20  QLITE_XXX error 
2983e 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
2983f 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73  ..** In this cas
29840 65 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  e the caller mus
29841 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62  t call sqlite3Db
29842 46 72 65 65 28 64 62 2c 20 29 20 6f 6e 20 2a 70  Free(db, ) on *p
29843 7a 45 72 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  zErr..*/.SQLITE_
29844 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
29845 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74  te3VtabCallCreat
29846 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  e(sqlite3 *db, i
29847 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68  nt iDb, const ch
29848 61 72 20 2a 7a 54 61 62 2c 20 63 68 61 72 20 2a  ar *zTab, char *
29849 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20 72  *pzErr){.  int r
2984a 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
2984b 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
2984c 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20  Module *pMod;.  
2984d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64  const char *zMod
2984e 75 6c 65 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73  ule;..  pTab = s
2984f 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
29850 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e 61 44  db, zTab, db->aD
29851 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
29852 20 61 73 73 65 72 74 28 70 54 61 62 20 26 26 20   assert(pTab && 
29853 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20  (pTab->tabFlags 
29854 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30  & TF_Virtual)!=0
29855 20 26 26 20 21 70 54 61 62 2d 3e 70 56 74 61 62   && !pTab->pVtab
29856 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 70 54 61 62  );.  pMod = pTab
29857 2d 3e 70 4d 6f 64 3b 0a 20 20 7a 4d 6f 64 75 6c  ->pMod;.  zModul
29858 65 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75  e = pTab->azModu
29859 6c 65 41 72 67 5b 30 5d 3b 0a 0a 20 20 2f 2a 20  leArg[0];..  /* 
2985a 49 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 68 61  If the module ha
2985b 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  s been registere
2985c 64 20 61 6e 64 20 69 6e 63 6c 75 64 65 73 20 61  d and includes a
2985d 20 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2c 20   Create method, 
2985e 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 69 74 20  .  ** invoke it 
2985f 6e 6f 77 2e 20 49 66 20 74 68 65 20 6d 6f 64 75  now. If the modu
29860 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  le has not been 
29861 72 65 67 69 73 74 65 72 65 64 2c 20 72 65 74 75  registered, retu
29862 72 6e 20 61 6e 20 0a 20 20 2a 2a 20 65 72 72 6f  rn an .  ** erro
29863 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 64 6f  r. Otherwise, do
29864 20 6e 6f 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20   nothing..  */. 
29865 20 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a 20 20   if( !pMod ){.  
29866 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
29867 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e  e3MPrintf(db, "n
29868 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25  o such module: %
29869 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20  s", zModule);.  
2986a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
2986b 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ROR;.  }else{.  
2986c 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c 6c 43    rc = vtabCallC
2986d 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c 20 70  onstructor(db, p
2986e 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d  Tab, pMod, pMod-
2986f 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61 74  >pModule->xCreat
29870 65 2c 20 70 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a  e, pzErr);.  }..
29871 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
29872 5f 4f 4b 20 26 26 20 70 54 61 62 2d 3e 70 56 74  _OK && pTab->pVt
29873 61 62 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ab ){.      rc =
29874 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64 62 2c   addToVTrans(db,
29875 20 70 54 61 62 2d 3e 70 56 74 61 62 29 3b 0a 20   pTab->pVtab);. 
29876 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
29877 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
29878 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
29879 74 6f 20 73 65 74 20 74 68 65 20 73 63 68 65 6d  to set the schem
2987a 61 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74  a of a virtual t
2987b 61 62 6c 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c  able.  It is onl
2987c 79 0a 2a 2a 20 76 61 6c 69 64 20 74 6f 20 63 61  y.** valid to ca
2987d 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
2987e 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
2987f 20 78 43 72 65 61 74 65 28 29 20 6f 72 20 78 43   xCreate() or xC
29880 6f 6e 6e 65 63 74 28 29 20 6f 66 20 61 0a 2a 2a  onnect() of a.**
29881 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
29882 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  odule..*/.SQLITE
29883 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
29884 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73 71  _declare_vtab(sq
29885 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
29886 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65 54 61   char *zCreateTa
29887 62 6c 65 29 7b 0a 20 20 50 61 72 73 65 20 73 50  ble){.  Parse sP
29888 61 72 73 65 3b 0a 0a 20 20 69 6e 74 20 72 63 20  arse;..  int rc 
29889 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54  = SQLITE_OK;.  T
2988a 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 63 68  able *pTab;.  ch
2988b 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20  ar *zErr = 0;.. 
2988c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2988d 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2988e 0a 20 20 70 54 61 62 20 3d 20 64 62 2d 3e 70 56  .  pTab = db->pV
2988f 54 61 62 3b 0a 20 20 69 66 28 20 21 70 54 61 62  Tab;.  if( !pTab
29890 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
29891 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
29892 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 20  MISUSE, 0);.    
29893 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
29894 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
29895 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
29896 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
29897 61 73 73 65 72 74 28 28 70 54 61 62 2d 3e 74 61  assert((pTab->ta
29898 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
29899 75 61 6c 29 21 3d 30 20 26 26 20 70 54 61 62 2d  ual)!=0 && pTab-
2989a 3e 6e 43 6f 6c 3d 3d 30 20 26 26 20 70 54 61 62  >nCol==0 && pTab
2989b 2d 3e 61 43 6f 6c 3d 3d 30 29 3b 0a 0a 20 20 6d  ->aCol==0);..  m
2989c 65 6d 73 65 74 28 26 73 50 61 72 73 65 2c 20 30  emset(&sParse, 0
2989d 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29 29  , sizeof(Parse))
2989e 3b 0a 20 20 73 50 61 72 73 65 2e 64 65 63 6c 61  ;.  sParse.decla
2989f 72 65 56 74 61 62 20 3d 20 31 3b 0a 20 20 73 50  reVtab = 1;.  sP
298a0 61 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 0a 20  arse.db = db;.. 
298a1 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 4c 49   if( .      SQLI
298a2 54 45 5f 4f 4b 20 3d 3d 20 73 71 6c 69 74 65 33  TE_OK == sqlite3
298a3 52 75 6e 50 61 72 73 65 72 28 26 73 50 61 72 73  RunParser(&sPars
298a4 65 2c 20 7a 43 72 65 61 74 65 54 61 62 6c 65 2c  e, zCreateTable,
298a5 20 26 7a 45 72 72 29 20 26 26 20 0a 20 20 20 20   &zErr) && .    
298a6 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62    sParse.pNewTab
298a7 6c 65 20 26 26 20 0a 20 20 20 20 20 20 21 73 50  le && .      !sP
298a8 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e  arse.pNewTable->
298a9 70 53 65 6c 65 63 74 20 26 26 20 0a 20 20 20 20  pSelect && .    
298aa 20 20 28 73 50 61 72 73 65 2e 70 4e 65 77 54 61    (sParse.pNewTa
298ab 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  ble->tabFlags & 
298ac 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 0a 20  TF_Virtual)==0. 
298ad 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 61 43   ){.    pTab->aC
298ae 6f 6c 20 3d 20 73 50 61 72 73 65 2e 70 4e 65 77  ol = sParse.pNew
298af 54 61 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 20 20  Table->aCol;.   
298b0 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 73 50   pTab->nCol = sP
298b1 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e  arse.pNewTable->
298b2 6e 43 6f 6c 3b 0a 20 20 20 20 73 50 61 72 73 65  nCol;.    sParse
298b3 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c  .pNewTable->nCol
298b4 20 3d 20 30 3b 0a 20 20 20 20 73 50 61 72 73 65   = 0;.    sParse
298b5 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c  .pNewTable->aCol
298b6 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 56   = 0;.    db->pV
298b7 54 61 62 20 3d 20 30 3b 0a 20 20 7d 20 65 6c 73  Tab = 0;.  } els
298b8 65 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  e {.    sqlite3E
298b9 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
298ba 45 52 52 4f 52 2c 20 7a 45 72 72 29 3b 0a 20 20  ERROR, zErr);.  
298bb 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
298bc 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 72  db, zErr);.    r
298bd 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
298be 3b 0a 20 20 7d 0a 20 20 73 50 61 72 73 65 2e 64  ;.  }.  sParse.d
298bf 65 63 6c 61 72 65 56 74 61 62 20 3d 20 30 3b 0a  eclareVtab = 0;.
298c0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  .  sqlite3_final
298c1 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d  ize((sqlite3_stm
298c2 74 2a 29 73 50 61 72 73 65 2e 70 56 64 62 65 29  t*)sParse.pVdbe)
298c3 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74  ;.  sqlite3Delet
298c4 65 54 61 62 6c 65 28 73 50 61 72 73 65 2e 70 4e  eTable(sParse.pN
298c5 65 77 54 61 62 6c 65 29 3b 0a 20 20 73 50 61 72  ewTable);.  sPar
298c6 73 65 2e 70 4e 65 77 54 61 62 6c 65 20 3d 20 30  se.pNewTable = 0
298c7 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72 63  ;..  assert( (rc
298c8 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20  &0xff)==rc );.  
298c9 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
298ca 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73  xit(db, rc);.  s
298cb 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
298cc 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
298cd 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
298ce 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
298cf 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79  on is invoked by
298d0 20 74 68 65 20 76 64 62 65 20 74 6f 20 63 61 6c   the vdbe to cal
298d1 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d  l the xDestroy m
298d2 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20  ethod.** of the 
298d3 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61  virtual table na
298d4 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74 61  med zTab in data
298d5 62 61 73 65 20 69 44 62 2e 20 54 68 69 73 20 6f  base iDb. This o
298d6 63 63 75 72 73 0a 2a 2a 20 77 68 65 6e 20 61 20  ccurs.** when a 
298d7 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20 6d 65  DROP TABLE is me
298d8 6e 74 69 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  ntioned..**.** T
298d9 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f  his call is a no
298da 2d 6f 70 20 69 66 20 7a 54 61 62 20 69 73 20 6e  -op if zTab is n
298db 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  ot a virtual tab
298dc 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
298dd 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
298de 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79  3VtabCallDestroy
298df 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
298e0 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61  t iDb, const cha
298e1 72 20 2a 7a 54 61 62 29 0a 7b 0a 20 20 69 6e 74  r *zTab).{.  int
298e2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
298e3 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
298e4 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
298e5 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
298e6 54 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  Tab, db->aDb[iDb
298e7 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65  ].zName);.  asse
298e8 72 74 28 70 54 61 62 29 3b 0a 20 20 69 66 28 20  rt(pTab);.  if( 
298e9 70 54 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20  pTab->pVtab ){. 
298ea 20 20 20 69 6e 74 20 28 2a 78 44 65 73 74 72 6f     int (*xDestro
298eb 79 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  y)(sqlite3_vtab 
298ec 2a 70 56 54 61 62 29 20 3d 20 70 54 61 62 2d 3e  *pVTab) = pTab->
298ed 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  pMod->pModule->x
298ee 44 65 73 74 72 6f 79 3b 0a 20 20 20 20 72 63 20  Destroy;.    rc 
298ef 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  = sqlite3SafetyO
298f0 66 66 28 64 62 29 3b 0a 20 20 20 20 61 73 73 65  ff(db);.    asse
298f1 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
298f2 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 78 44 65  K );.    if( xDe
298f3 73 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 72  stroy ){.      r
298f4 63 20 3d 20 78 44 65 73 74 72 6f 79 28 70 54 61  c = xDestroy(pTa
298f5 62 2d 3e 70 56 74 61 62 29 3b 0a 20 20 20 20 7d  b->pVtab);.    }
298f6 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
298f7 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
298f8 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
298f9 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
298fa 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28  nt i;.      for(
298fb 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61  i=0; i<db->nVTra
298fc 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ns; i++){.      
298fd 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e    if( db->aVTran
298fe 73 5b 69 5d 3d 3d 70 54 61 62 2d 3e 70 56 74 61  s[i]==pTab->pVta
298ff 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64  b ){.          d
29900 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 20 3d 20  b->aVTrans[i] = 
29901 64 62 2d 3e 61 56 54 72 61 6e 73 5b 2d 2d 64 62  db->aVTrans[--db
29902 2d 3e 6e 56 54 72 61 6e 73 5d 3b 0a 20 20 20 20  ->nVTrans];.    
29903 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29904 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
29905 20 20 20 20 20 70 54 61 62 2d 3e 70 56 74 61 62       pTab->pVtab
29906 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
29907 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
29908 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
29909 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 65 69 74  tion invokes eit
2990a 68 65 72 20 74 68 65 20 78 52 6f 6c 6c 62 61 63  her the xRollbac
2990b 6b 20 6f 72 20 78 43 6f 6d 6d 69 74 20 6d 65 74  k or xCommit met
2990c 68 6f 64 0a 2a 2a 20 6f 66 20 65 61 63 68 20 6f  hod.** of each o
2990d 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2990e 62 6c 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69  bles in the sqli
2990f 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
29910 79 2e 20 54 68 65 20 6d 65 74 68 6f 64 0a 2a 2a  y. The method.**
29911 20 63 61 6c 6c 65 64 20 69 73 20 69 64 65 6e 74   called is ident
29912 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63  ified by the sec
29913 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 6f  ond argument, "o
29914 66 66 73 65 74 22 2c 20 77 68 69 63 68 20 69 73  ffset", which is
29915 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f  .** the offset o
29916 66 20 74 68 65 20 6d 65 74 68 6f 64 20 74 6f 20  f the method to 
29917 63 61 6c 6c 20 69 6e 20 74 68 65 20 73 71 6c 69  call in the sqli
29918 74 65 33 5f 6d 6f 64 75 6c 65 20 73 74 72 75 63  te3_module struc
29919 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ture..**.** The 
2991a 61 72 72 61 79 20 69 73 20 63 6c 65 61 72 65 64  array is cleared
2991b 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20   after invoking 
2991c 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 2e 20 0a  the callbacks. .
2991d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
2991e 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28 73 71 6c  allFinaliser(sql
2991f 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 66  ite3 *db, int of
29920 66 73 65 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  fset){.  int i;.
29921 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e    if( db->aVTran
29922 73 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  s ){.    for(i=0
29923 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 20  ; i<db->nVTrans 
29924 26 26 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69  && db->aVTrans[i
29925 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  ]; i++){.      s
29926 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
29927 61 62 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73  ab = db->aVTrans
29928 5b 69 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 28  [i];.      int (
29929 2a 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  *x)(sqlite3_vtab
2992a 20 2a 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 2a   *);.      x = *
2992b 28 69 6e 74 20 28 2a 2a 29 28 73 71 6c 69 74 65  (int (**)(sqlite
2992c 33 5f 76 74 61 62 20 2a 29 29 28 28 63 68 61 72  3_vtab *))((char
2992d 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   *)pVtab->pModul
2992e 65 20 2b 20 6f 66 66 73 65 74 29 3b 0a 20 20 20  e + offset);.   
2992f 20 20 20 69 66 28 20 78 20 29 20 78 28 70 56 74     if( x ) x(pVt
29930 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ab);.      sqlit
29931 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c  e3VtabUnlock(db,
29932 20 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20   pVtab);.    }. 
29933 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
29934 28 64 62 2c 20 64 62 2d 3e 61 56 54 72 61 6e 73  (db, db->aVTrans
29935 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 56 54 72 61  );.    db->nVTra
29936 6e 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e  ns = 0;.    db->
29937 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 7d  aVTrans = 0;.  }
29938 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65  .}../*.** Invoke
29939 20 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f   the xSync metho
2993a 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c  d of all virtual
2993b 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 73   tables in the s
2993c 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 0a 2a  qlite3.aVTrans.*
2993d 2a 20 61 72 72 61 79 2e 20 52 65 74 75 72 6e 20  * array. Return 
2993e 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66  the error code f
2993f 6f 72 20 74 68 65 20 66 69 72 73 74 20 65 72 72  or the first err
29940 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 2c 20  or that occurs, 
29941 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  or.** SQLITE_OK 
29942 69 66 20 61 6c 6c 20 78 53 79 6e 63 20 6f 70 65  if all xSync ope
29943 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63  rations are succ
29944 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 53 65  essful..**.** Se
29945 74 20 2a 70 7a 45 72 72 6d 73 67 20 74 6f 20 70  t *pzErrmsg to p
29946 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
29947 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
29948 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20 0a  released using .
29949 2a 2a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  ** sqlite3DbFree
2994a 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e  () containing an
2994b 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20   error message, 
2994c 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c 61  if one is availa
2994d 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
2994e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2994f 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69 74  e3VtabSync(sqlit
29950 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 70  e3 *db, char **p
29951 7a 45 72 72 6d 73 67 29 7b 0a 20 20 69 6e 74 20  zErrmsg){.  int 
29952 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  i;.  int rc = SQ
29953 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72  LITE_OK;.  int r
29954 63 73 61 66 65 74 79 3b 0a 20 20 73 71 6c 69 74  csafety;.  sqlit
29955 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 61 6e  e3_vtab **aVTran
29956 73 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 3b  s = db->aVTrans;
29957 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
29958 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
29959 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30   db->aVTrans = 0
2995a 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ;.  for(i=0; rc=
2995b 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
2995c 64 62 2d 3e 6e 56 54 72 61 6e 73 20 26 26 20 61  db->nVTrans && a
2995d 56 54 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b  VTrans[i]; i++){
2995e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61  .    sqlite3_vta
2995f 62 20 2a 70 56 74 61 62 20 3d 20 61 56 54 72 61  b *pVtab = aVTra
29960 6e 73 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 28  ns[i];.    int (
29961 2a 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  *x)(sqlite3_vtab
29962 20 2a 29 3b 0a 20 20 20 20 78 20 3d 20 70 56 74   *);.    x = pVt
29963 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 53 79  ab->pModule->xSy
29964 6e 63 3b 0a 20 20 20 20 69 66 28 20 78 20 29 7b  nc;.    if( x ){
29965 0a 20 20 20 20 20 20 72 63 20 3d 20 78 28 70 56  .      rc = x(pV
29966 74 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  tab);.      sqli
29967 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70  te3DbFree(db, *p
29968 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20  zErrmsg);.      
29969 2a 70 7a 45 72 72 6d 73 67 20 3d 20 70 56 74 61  *pzErrmsg = pVta
2996a 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20  b->zErrMsg;.    
2996b 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67    pVtab->zErrMsg
2996c 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
2996d 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20    db->aVTrans = 
2996e 61 56 54 72 61 6e 73 3b 0a 20 20 72 63 73 61 66  aVTrans;.  rcsaf
2996f 65 74 79 20 3d 20 73 71 6c 69 74 65 33 53 61 66  ety = sqlite3Saf
29970 65 74 79 4f 6e 28 64 62 29 3b 0a 0a 20 20 69 66  etyOn(db);..  if
29971 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
29972 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 63 73 61  ){.    rc = rcsa
29973 66 65 74 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75  fety;.  }.  retu
29974 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
29975 49 6e 76 6f 6b 65 20 74 68 65 20 78 52 6f 6c 6c  Invoke the xRoll
29976 62 61 63 6b 20 6d 65 74 68 6f 64 20 6f 66 20 61  back method of a
29977 6c 6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ll virtual table
29978 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c  s in the .** sql
29979 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72  ite3.aVTrans arr
2997a 61 79 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74  ay. Then clear t
2997b 68 65 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e  he array itself.
2997c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2997d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74  TE int sqlite3Vt
2997e 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74  abRollback(sqlit
2997f 65 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46  e3 *db){.  callF
29980 69 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f 66 66  inaliser(db, off
29981 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f  setof(sqlite3_mo
29982 64 75 6c 65 2c 78 52 6f 6c 6c 62 61 63 6b 29 29  dule,xRollback))
29983 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
29984 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  E_OK;.}../*.** I
29985 6e 76 6f 6b 65 20 74 68 65 20 78 43 6f 6d 6d 69  nvoke the xCommi
29986 74 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20  t method of all 
29987 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69  virtual tables i
29988 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65  n the .** sqlite
29989 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e  3.aVTrans array.
2998a 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20   Then clear the 
2998b 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f  array itself..*/
2998c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2998d 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43  int sqlite3VtabC
2998e 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64  ommit(sqlite3 *d
2998f 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69  b){.  callFinali
29990 73 65 72 28 64 62 2c 20 6f 66 66 73 65 74 6f 66  ser(db, offsetof
29991 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c  (sqlite3_module,
29992 78 43 6f 6d 6d 69 74 29 29 3b 0a 20 20 72 65 74  xCommit));.  ret
29993 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
29994 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76  ../*.** If the v
29995 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 56 74  irtual table pVt
29996 61 62 20 73 75 70 70 6f 72 74 73 20 74 68 65 20  ab supports the 
29997 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 65  transaction inte
29998 72 66 61 63 65 0a 2a 2a 20 28 78 42 65 67 69 6e  rface.** (xBegin
29999 2f 78 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d 6d  /xRollback/xComm
2999a 69 74 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c  it and optionall
2999b 79 20 78 53 79 6e 63 29 20 61 6e 64 20 61 20 74  y xSync) and a t
2999c 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  ransaction is.**
2999d 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6f   not currently o
2999e 70 65 6e 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20  pen, invoke the 
2999f 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 6e 6f  xBegin method no
299a0 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  w..**.** If the 
299a1 78 42 65 67 69 6e 20 63 61 6c 6c 20 69 73 20 73  xBegin call is s
299a2 75 63 63 65 73 73 66 75 6c 2c 20 70 6c 61 63 65  uccessful, place
299a3 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61   the sqlite3_vta
299a4 62 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e 20  b pointer.** in 
299a5 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72  the sqlite3.aVTr
299a6 61 6e 73 20 61 72 72 61 79 2e 0a 2a 2f 0a 53 51  ans array..*/.SQ
299a7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
299a8 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69   sqlite3VtabBegi
299a9 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73  n(sqlite3 *db, s
299aa 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
299ab 61 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ab){.  int rc = 
299ac 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e  SQLITE_OK;.  con
299ad 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
299ae 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f  e *pModule;..  /
299af 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20  * Special case: 
299b0 49 66 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 69  If db->aVTrans i
299b1 73 20 4e 55 4c 4c 20 61 6e 64 20 64 62 2d 3e 6e  s NULL and db->n
299b2 56 54 72 61 6e 73 20 69 73 20 67 72 65 61 74 65  VTrans is greate
299b3 72 0a 20 20 2a 2a 20 74 68 61 6e 20 7a 65 72 6f  r.  ** than zero
299b4 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
299b5 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61  tion is being ca
299b6 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
299b7 20 61 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20   a.  ** virtual 
299b8 6d 6f 64 75 6c 65 20 78 53 79 6e 63 28 29 20 63  module xSync() c
299b9 61 6c 6c 62 61 63 6b 2e 20 49 74 20 69 73 20 69  allback. It is i
299ba 6c 6c 65 67 61 6c 20 74 6f 20 77 72 69 74 65 20  llegal to write 
299bb 74 6f 20 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c  to .  ** virtual
299bc 20 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 69   module tables i
299bd 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 6f 20  n this case, so 
299be 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
299bf 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  CKED..  */.  if(
299c0 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79   sqlite3VtabInSy
299c1 6e 63 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65  nc(db) ){.    re
299c2 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
299c3 45 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70  ED;.  }.  if( !p
299c4 56 74 61 62 20 29 7b 0a 20 20 20 20 72 65 74 75  Vtab ){.    retu
299c5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
299c6 7d 20 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70  } .  pModule = p
299c7 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a  Vtab->pModule;..
299c8 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78    if( pModule->x
299c9 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74  Begin ){.    int
299ca 20 69 3b 0a 0a 0a 20 20 20 20 2f 2a 20 49 66 20   i;...    /* If 
299cb 70 56 74 61 62 20 69 73 20 61 6c 72 65 61 64 79  pVtab is already
299cc 20 69 6e 20 74 68 65 20 61 56 54 72 61 6e 73 20   in the aVTrans 
299cd 61 72 72 61 79 2c 20 72 65 74 75 72 6e 20 65 61  array, return ea
299ce 72 6c 79 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  rly */.    for(i
299cf 3d 30 3b 20 28 69 3c 64 62 2d 3e 6e 56 54 72 61  =0; (i<db->nVTra
299d0 6e 73 29 20 26 26 20 30 21 3d 64 62 2d 3e 61 56  ns) && 0!=db->aV
299d1 54 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  Trans[i]; i++){.
299d2 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 56        if( db->aV
299d3 54 72 61 6e 73 5b 69 5d 3d 3d 70 56 74 61 62 20  Trans[i]==pVtab 
299d4 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
299d5 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
299d6 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
299d7 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 42  /* Invoke the xB
299d8 65 67 69 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  egin method */. 
299d9 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d     rc = pModule-
299da 3e 78 42 65 67 69 6e 28 70 56 74 61 62 29 3b 0a  >xBegin(pVtab);.
299db 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
299dc 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
299dd 63 20 3d 20 61 64 64 54 6f 56 54 72 61 6e 73 28  c = addToVTrans(
299de 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20  db, pVtab);.    
299df 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
299e0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
299e1 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
299e2 28 70 44 65 66 29 20 69 73 20 61 20 66 75 6e 63  (pDef) is a func
299e3 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tion implementat
299e4 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63  ion.  The.** sec
299e5 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 70  ond parameter (p
299e6 45 78 70 72 29 20 69 73 20 74 68 65 20 66 69 72  Expr) is the fir
299e7 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
299e8 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  his function..**
299e9 20 49 66 20 70 45 78 70 72 20 69 73 20 61 20 63   If pExpr is a c
299ea 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75  olumn in a virtu
299eb 61 6c 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6c  al table, then l
299ec 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  et the virtual.*
299ed 2a 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  * table implemen
299ee 74 61 74 69 6f 6e 20 68 61 76 65 20 61 6e 20 6f  tation have an o
299ef 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 6f 76  pportunity to ov
299f0 65 72 6c 6f 61 64 20 74 68 65 20 66 75 6e 63 74  erload the funct
299f1 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
299f2 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
299f3 74 6f 20 61 6c 6c 6f 77 20 76 69 72 74 75 61 6c  to allow virtual
299f4 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
299f5 61 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 6f 76 65  ations to.** ove
299f6 72 6c 6f 61 64 20 4d 41 54 43 48 2c 20 4c 49 4b  rload MATCH, LIK
299f7 45 2c 20 47 4c 4f 42 2c 20 61 6e 64 20 52 45 47  E, GLOB, and REG
299f8 45 58 50 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  EXP operators..*
299f9 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 65 69 74 68  *.** Return eith
299fa 65 72 20 74 68 65 20 70 44 65 66 20 61 72 67 75  er the pDef argu
299fb 6d 65 6e 74 20 28 69 6e 64 69 63 61 74 69 6e 67  ment (indicating
299fc 20 6e 6f 20 63 68 61 6e 67 65 29 20 6f 72 20 61   no change) or a
299fd 20 0a 2a 2a 20 6e 65 77 20 46 75 6e 63 44 65 66   .** new FuncDef
299fe 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
299ff 69 73 20 6d 61 72 6b 65 64 20 61 73 20 65 70 68  is marked as eph
29a00 65 6d 65 72 61 6c 20 75 73 69 6e 67 20 74 68 65  emeral using the
29a01 0a 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  .** SQLITE_FUNC_
29a02 45 50 48 45 4d 20 66 6c 61 67 2e 0a 2a 2f 0a 53  EPHEM flag..*/.S
29a03 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75  QLITE_PRIVATE Fu
29a04 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74  ncDef *sqlite3Vt
29a05 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69  abOverloadFuncti
29a06 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
29a07 62 2c 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73  b,    /* Databas
29a08 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72  e connection for
29a09 20 72 65 70 6f 72 74 69 6e 67 20 6d 61 6c 6c 6f   reporting mallo
29a0a 63 20 70 72 6f 62 6c 65 6d 73 20 2a 2f 0a 20 20  c problems */.  
29a0b 46 75 6e 63 44 65 66 20 2a 70 44 65 66 2c 20 20  FuncDef *pDef,  
29a0c 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 70  /* Function to p
29a0d 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64  ossibly overload
29a0e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20   */.  int nArg, 
29a0f 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
29a10 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
29a11 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
29a12 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20    Expr *pExpr   
29a13 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d    /* First argum
29a14 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ent to the funct
29a15 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  ion */.){.  Tabl
29a16 65 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69 74  e *pTab;.  sqlit
29a17 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
29a18 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65    sqlite3_module
29a19 20 2a 70 4d 6f 64 3b 0a 20 20 76 6f 69 64 20 28   *pMod;.  void (
29a1a 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
29a1b 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
29a1c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 20 3d 20  ite3_value**) = 
29a1d 30 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20  0;.  void *pArg 
29a1e 3d 20 30 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a  = 0;.  FuncDef *
29a1f 70 4e 65 77 3b 0a 20 20 69 6e 74 20 72 63 20 3d  pNew;.  int rc =
29a20 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 77   0;.  char *zLow
29a21 65 72 4e 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e  erName;.  unsign
29a22 65 64 20 63 68 61 72 20 2a 7a 3b 0a 0a 0a 20 20  ed char *z;...  
29a23 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
29a24 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  the left operand
29a25 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   is a column in 
29a26 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
29a27 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d  */.  if( pExpr==
29a28 30 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b  0 ) return pDef;
29a29 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  .  if( pExpr->op
29a2a 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65  !=TK_COLUMN ) re
29a2b 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70 54 61  turn pDef;.  pTa
29a2c 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b  b = pExpr->pTab;
29a2d 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
29a2e 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20   return pDef;.  
29a2f 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c  if( (pTab->tabFl
29a30 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c  ags & TF_Virtual
29a31 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70 44  )==0 ) return pD
29a32 65 66 3b 0a 20 20 70 56 74 61 62 20 3d 20 70 54  ef;.  pVtab = pT
29a33 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 61 73 73  ab->pVtab;.  ass
29a34 65 72 74 28 20 70 56 74 61 62 21 3d 30 20 29 3b  ert( pVtab!=0 );
29a35 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62  .  assert( pVtab
29a36 2d 3e 70 4d 6f 64 75 6c 65 21 3d 30 20 29 3b 0a  ->pModule!=0 );.
29a37 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69 74 65    pMod = (sqlite
29a38 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62  3_module *)pVtab
29a39 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 66 28  ->pModule;.  if(
29a3a 20 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63   pMod->xFindFunc
29a3b 74 69 6f 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e  tion==0 ) return
29a3c 20 70 44 65 66 3b 0a 20 0a 20 20 2f 2a 20 43 61   pDef;. .  /* Ca
29a3d 6c 6c 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63  ll the xFindFunc
29a3e 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 6e 20 74  tion method on t
29a3f 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
29a40 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
29a41 20 20 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74    ** to see if t
29a42 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
29a43 6e 20 77 61 6e 74 73 20 74 6f 20 6f 76 65 72 6c  n wants to overl
29a44 6f 61 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f  oad this functio
29a45 6e 20 0a 20 20 2a 2f 0a 20 20 7a 4c 6f 77 65 72  n .  */.  zLower
29a46 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Name = sqlite3Db
29a47 53 74 72 44 75 70 28 64 62 2c 20 70 44 65 66 2d  StrDup(db, pDef-
29a48 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a  >zName);.  if( z
29a49 4c 6f 77 65 72 4e 61 6d 65 20 29 7b 0a 20 20 20  LowerName ){.   
29a4a 20 66 6f 72 28 7a 3d 28 75 6e 73 69 67 6e 65 64   for(z=(unsigned
29a4b 20 63 68 61 72 2a 29 7a 4c 6f 77 65 72 4e 61 6d   char*)zLowerNam
29a4c 65 3b 20 2a 7a 3b 20 7a 2b 2b 29 7b 0a 20 20 20  e; *z; z++){.   
29a4d 20 20 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 55     *z = sqlite3U
29a4e 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 5d 3b  pperToLower[*z];
29a4f 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
29a50 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74  pMod->xFindFunct
29a51 69 6f 6e 28 70 56 74 61 62 2c 20 6e 41 72 67 2c  ion(pVtab, nArg,
29a52 20 7a 4c 6f 77 65 72 4e 61 6d 65 2c 20 26 78 46   zLowerName, &xF
29a53 75 6e 63 2c 20 26 70 41 72 67 29 3b 0a 20 20 20  unc, &pArg);.   
29a54 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
29a55 62 2c 20 7a 4c 6f 77 65 72 4e 61 6d 65 29 3b 0a  b, zLowerName);.
29a56 20 20 20 20 69 66 28 20 70 56 74 61 62 2d 3e 7a      if( pVtab->z
29a57 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
29a58 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
29a59 20 72 63 2c 20 22 25 73 22 2c 20 70 56 74 61 62   rc, "%s", pVtab
29a5a 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
29a5b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
29a5c 64 62 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d  db, pVtab->zErrM
29a5d 73 67 29 3b 0a 20 20 20 20 20 20 70 56 74 61 62  sg);.      pVtab
29a5e 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
29a5f 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72     }.  }.  if( r
29a60 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  c==0 ){.    retu
29a61 72 6e 20 70 44 65 66 3b 0a 20 20 7d 0a 0a 20 20  rn pDef;.  }..  
29a62 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  /* Create a new 
29a63 65 70 68 65 6d 65 72 61 6c 20 66 75 6e 63 74 69  ephemeral functi
29a64 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 66 6f  on definition fo
29a65 72 20 74 68 65 20 6f 76 65 72 6c 6f 61 64 65 64  r the overloaded
29a66 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 2a  .  ** function *
29a67 2f 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  /.  pNew = sqlit
29a68 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
29a69 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  b, sizeof(*pNew)
29a6a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
29a6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20                + 
29a6c 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
29a6d 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 20 29 3b 0a  pDef->zName) );.
29a6e 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
29a6f 0a 20 20 20 20 72 65 74 75 72 6e 20 70 44 65 66  .    return pDef
29a70 3b 0a 20 20 7d 0a 20 20 2a 70 4e 65 77 20 3d 20  ;.  }.  *pNew = 
29a71 2a 70 44 65 66 3b 0a 20 20 70 4e 65 77 2d 3e 7a  *pDef;.  pNew->z
29a72 4e 61 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 26  Name = (char *)&
29a73 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70  pNew[1];.  memcp
29a74 79 28 70 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 70  y(pNew->zName, p
29a75 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69  Def->zName, sqli
29a76 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44 65 66  te3Strlen30(pDef
29a77 2d 3e 7a 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 70  ->zName)+1);.  p
29a78 4e 65 77 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75  New->xFunc = xFu
29a79 6e 63 3b 0a 20 20 70 4e 65 77 2d 3e 70 55 73 65  nc;.  pNew->pUse
29a7a 72 44 61 74 61 20 3d 20 70 41 72 67 3b 0a 20 20  rData = pArg;.  
29a7b 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 53  pNew->flags |= S
29a7c 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d  QLITE_FUNC_EPHEM
29a7d 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  ;.  return pNew;
29a7e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
29a7f 75 72 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ure virtual tabl
29a80 65 20 70 54 61 62 20 69 73 20 63 6f 6e 74 61 69  e pTab is contai
29a81 6e 65 64 20 69 6e 20 74 68 65 20 70 50 61 72 73  ned in the pPars
29a82 65 2d 3e 61 70 56 69 72 74 75 61 6c 4c 6f 63 6b  e->apVirtualLock
29a83 5b 5d 0a 2a 2a 20 61 72 72 61 79 20 73 6f 20 74  [].** array so t
29a84 68 61 74 20 61 6e 20 4f 50 5f 56 42 65 67 69 6e  hat an OP_VBegin
29a85 20 77 69 6c 6c 20 67 65 74 20 67 65 6e 65 72 61   will get genera
29a86 74 65 64 20 66 6f 72 20 69 74 2e 20 20 41 64 64  ted for it.  Add
29a87 20 70 54 61 62 20 74 6f 20 74 68 65 0a 2a 2a 20   pTab to the.** 
29a88 61 72 72 61 79 20 69 66 20 69 74 20 69 73 20 6d  array if it is m
29a89 69 73 73 69 6e 67 2e 20 20 49 66 20 70 54 61 62  issing.  If pTab
29a8a 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
29a8b 68 65 20 61 72 72 61 79 2c 20 74 68 69 73 20 72  he array, this r
29a8c 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e  outine.** is a n
29a8d 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  o-op..*/.SQLITE_
29a8e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
29a8f 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74  ite3VtabMakeWrit
29a90 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72  able(Parse *pPar
29a91 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29  se, Table *pTab)
29a92 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  {.  int i, n;.  
29a93 61 73 73 65 72 74 28 20 49 73 56 69 72 74 75 61  assert( IsVirtua
29a94 6c 28 70 54 61 62 29 20 29 3b 0a 20 20 66 6f 72  l(pTab) );.  for
29a95 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e  (i=0; i<pParse->
29a96 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b  nVtabLock; i++){
29a97 0a 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 70  .    if( pTab==p
29a98 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63  Parse->apVtabLoc
29a99 6b 5b 69 5d 20 29 20 72 65 74 75 72 6e 3b 0a 20  k[i] ) return;. 
29a9a 20 7d 0a 20 20 6e 20 3d 20 28 70 50 61 72 73 65   }.  n = (pParse
29a9b 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 31 29 2a 73  ->nVtabLock+1)*s
29a9c 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 70  izeof(pParse->ap
29a9d 56 74 61 62 4c 6f 63 6b 5b 30 5d 29 3b 0a 20 20  VtabLock[0]);.  
29a9e 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f  pParse->apVtabLo
29a9f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ck = sqlite3_rea
29aa0 6c 6c 6f 63 28 70 50 61 72 73 65 2d 3e 61 70 56  lloc(pParse->apV
29aa1 74 61 62 4c 6f 63 6b 2c 20 6e 29 3b 0a 20 20 69  tabLock, n);.  i
29aa2 66 28 20 70 50 61 72 73 65 2d 3e 61 70 56 74 61  f( pParse->apVta
29aa3 62 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 50 61  bLock ){.    pPa
29aa4 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b  rse->apVtabLock[
29aa5 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63  pParse->nVtabLoc
29aa6 6b 2b 2b 5d 20 3d 20 70 54 61 62 3b 0a 20 20 7d  k++] = pTab;.  }
29aa7 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 72 73 65  else{.    pParse
29aa8 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
29aa9 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23  ed = 1;.  }.}..#
29aaa 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
29aab 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
29aac 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
29aad 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 74 61  ***** End of vta
29aae 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  b.c ************
29aaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ab1 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
29ab2 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
29ab3 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   where.c *******
29ab4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ab5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ab6 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
29ab7 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
29ab8 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
29ab9 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
29aba 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
29abb 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
29abc 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
29abd 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
29abe 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
29abf 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
29ac0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
29ac1 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
29ac2 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
29ac3 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
29ac4 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
29ac5 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
29ac6 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
29ac7 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
29ac8 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
29ac9 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
29aca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29acb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29acc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29acd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
29ace 54 68 69 73 20 6d 6f 64 75 6c 65 20 63 6f 6e 74  This module cont
29acf 61 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61 74  ains C code that
29ad0 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20   generates VDBE 
29ad1 63 6f 64 65 20 75 73 65 64 20 74 6f 20 70 72 6f  code used to pro
29ad2 63 65 73 73 0a 2a 2a 20 74 68 65 20 57 48 45 52  cess.** the WHER
29ad3 45 20 63 6c 61 75 73 65 20 6f 66 20 53 51 4c 20  E clause of SQL 
29ad4 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 69  statements.  Thi
29ad5 73 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 73 70  s module is resp
29ad6 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 67  onsible for.** g
29ad7 65 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f  enerating the co
29ad8 64 65 20 74 68 61 74 20 6c 6f 6f 70 73 20 74 68  de that loops th
29ad9 72 6f 75 67 68 20 61 20 74 61 62 6c 65 20 6c 6f  rough a table lo
29ada 6f 6b 69 6e 67 20 66 6f 72 20 61 70 70 6c 69 63  oking for applic
29adb 61 62 6c 65 0a 2a 2a 20 72 6f 77 73 2e 20 20 49  able.** rows.  I
29adc 6e 64 69 63 65 73 20 61 72 65 20 73 65 6c 65 63  ndices are selec
29add 74 65 64 20 61 6e 64 20 75 73 65 64 20 74 6f 20  ted and used to 
29ade 73 70 65 65 64 20 74 68 65 20 73 65 61 72 63 68  speed the search
29adf 20 77 68 65 6e 20 64 6f 69 6e 67 0a 2a 2a 20 73   when doing.** s
29ae0 6f 20 69 73 20 61 70 70 6c 69 63 61 62 6c 65 2e  o is applicable.
29ae1 20 20 42 65 63 61 75 73 65 20 74 68 69 73 20 6d    Because this m
29ae2 6f 64 75 6c 65 20 69 73 20 72 65 73 70 6f 6e 73  odule is respons
29ae3 69 62 6c 65 20 66 6f 72 20 73 65 6c 65 63 74 69  ible for selecti
29ae4 6e 67 0a 2a 2a 20 69 6e 64 69 63 65 73 2c 20 79  ng.** indices, y
29ae5 6f 75 20 6d 69 67 68 74 20 61 6c 73 6f 20 74 68  ou might also th
29ae6 69 6e 6b 20 6f 66 20 74 68 69 73 20 6d 6f 64 75  ink of this modu
29ae7 6c 65 20 61 73 20 74 68 65 20 22 71 75 65 72 79  le as the "query
29ae8 20 6f 70 74 69 6d 69 7a 65 72 22 2e 0a 2a 2a 0a   optimizer"..**.
29ae9 2a 2a 20 24 49 64 3a 20 77 68 65 72 65 2e 63 2c  ** $Id: where.c,
29aea 76 20 31 2e 33 36 34 20 32 30 30 39 2f 30 31 2f  v 1.364 2009/01/
29aeb 31 34 20 30 30 3a 35 35 3a 31 30 20 64 72 68 20  14 00:55:10 drh 
29aec 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
29aed 54 72 61 63 65 20 6f 75 74 70 75 74 20 6d 61 63  Trace output mac
29aee 72 6f 73 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ros.*/.#if defin
29aef 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
29af0 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
29af1 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f  E_DEBUG).SQLITE_
29af2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
29af3 74 65 33 57 68 65 72 65 54 72 61 63 65 20 3d 20  te3WhereTrace = 
29af4 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 30 0a  0;.#endif.#if 0.
29af5 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 54 52  # define WHERETR
29af6 41 43 45 28 58 29 20 20 69 66 28 73 71 6c 69 74  ACE(X)  if(sqlit
29af7 65 33 57 68 65 72 65 54 72 61 63 65 29 20 73 71  e3WhereTrace) sq
29af8 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
29af9 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   X.#else.# defin
29afa 65 20 57 48 45 52 45 54 52 41 43 45 28 58 29 0a  e WHERETRACE(X).
29afb 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 77 61  #endif../* Forwa
29afc 72 64 20 72 65 66 65 72 65 6e 63 65 0a 2a 2f 0a  rd reference.*/.
29afd 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
29afe 68 65 72 65 43 6c 61 75 73 65 20 57 68 65 72 65  hereClause Where
29aff 43 6c 61 75 73 65 3b 0a 74 79 70 65 64 65 66 20  Clause;.typedef 
29b00 73 74 72 75 63 74 20 57 68 65 72 65 4d 61 73 6b  struct WhereMask
29b01 53 65 74 20 57 68 65 72 65 4d 61 73 6b 53 65 74  Set WhereMaskSet
29b02 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
29b03 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 57 68 65   WhereOrInfo Whe
29b04 72 65 4f 72 49 6e 66 6f 3b 0a 74 79 70 65 64 65  reOrInfo;.typede
29b05 66 20 73 74 72 75 63 74 20 57 68 65 72 65 41 6e  f struct WhereAn
29b06 64 49 6e 66 6f 20 57 68 65 72 65 41 6e 64 49 6e  dInfo WhereAndIn
29b07 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  fo;.typedef stru
29b08 63 74 20 57 68 65 72 65 43 6f 73 74 20 57 68 65  ct WhereCost Whe
29b09 72 65 43 6f 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54  reCost;../*.** T
29b0a 68 65 20 71 75 65 72 79 20 67 65 6e 65 72 61 74  he query generat
29b0b 6f 72 20 75 73 65 73 20 61 6e 20 61 72 72 61 79  or uses an array
29b0c 20 6f 66 20 69 6e 73 74 61 6e 63 65 73 20 6f 66   of instances of
29b0d 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
29b0e 74 6f 0a 2a 2a 20 68 65 6c 70 20 69 74 20 61 6e  to.** help it an
29b0f 61 6c 79 7a 65 20 74 68 65 20 73 75 62 65 78 70  alyze the subexp
29b10 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  ressions of the 
29b11 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45  WHERE clause.  E
29b12 61 63 68 20 57 48 45 52 45 0a 2a 2a 20 63 6c 61  ach WHERE.** cla
29b13 75 73 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  use subexpressio
29b14 6e 20 69 73 20 73 65 70 61 72 61 74 65 64 20 66  n is separated f
29b15 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 20 62  rom the others b
29b16 79 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 73 2e  y AND operators.
29b17 0a 2a 2a 20 28 4e 6f 74 65 3a 20 74 68 65 20 73  .** (Note: the s
29b18 61 6d 65 20 64 61 74 61 20 73 74 72 75 63 74 75  ame data structu
29b19 72 65 20 69 73 20 61 6c 73 6f 20 72 65 75 73 65  re is also reuse
29b1a 64 20 74 6f 20 68 6f 6c 64 20 61 20 67 72 6f 75  d to hold a grou
29b1b 70 20 6f 66 20 74 65 72 6d 73 0a 2a 2a 20 73 65  p of terms.** se
29b1c 70 61 72 61 74 65 64 20 62 79 20 4f 52 20 6f 70  parated by OR op
29b1d 65 72 61 74 6f 72 73 2e 20 20 42 75 74 20 61 74  erators.  But at
29b1e 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 2c 20   the top-level, 
29b1f 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 41 4e  everything is AN
29b20 44 0a 2a 2a 20 73 65 70 61 72 61 74 65 64 2e 29  D.** separated.)
29b21 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 57 68 65 72 65  .**.** All Where
29b22 54 65 72 6d 73 20 61 72 65 20 63 6f 6c 6c 65 63  Terms are collec
29b23 74 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c  ted into a singl
29b24 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74  e WhereClause st
29b25 72 75 63 74 75 72 65 2e 20 20 0a 2a 2a 20 54 68  ructure.  .** Th
29b26 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 64 65 6e  e following iden
29b27 74 69 74 79 20 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a  tity holds:.**.*
29b28 2a 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65  *        WhereTe
29b29 72 6d 2e 70 57 43 2d 3e 61 5b 57 68 65 72 65 54  rm.pWC->a[WhereT
29b2a 65 72 6d 2e 69 64 78 5d 20 3d 3d 20 57 68 65 72  erm.idx] == Wher
29b2b 65 54 65 72 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  eTerm.**.** When
29b2c 20 61 20 74 65 72 6d 20 69 73 20 6f 66 20 74 68   a term is of th
29b2d 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  e form:.**.**   
29b2e 20 20 20 20 20 20 20 20 20 20 20 58 20 3c 6f 70             X <op
29b2f 3e 20 3c 65 78 70 72 3e 0a 2a 2a 0a 2a 2a 20 77  > <expr>.**.** w
29b30 68 65 72 65 20 58 20 69 73 20 61 20 63 6f 6c 75  here X is a colu
29b31 6d 6e 20 6e 61 6d 65 20 61 6e 64 20 3c 6f 70 3e  mn name and <op>
29b32 20 69 73 20 6f 6e 65 20 6f 66 20 63 65 72 74 61   is one of certa
29b33 69 6e 20 6f 70 65 72 61 74 6f 72 73 2c 0a 2a 2a  in operators,.**
29b34 20 74 68 65 6e 20 57 68 65 72 65 54 65 72 6d 2e   then WhereTerm.
29b35 6c 65 66 74 43 75 72 73 6f 72 20 61 6e 64 20 57  leftCursor and W
29b36 68 65 72 65 54 65 72 6d 2e 75 2e 6c 65 66 74 43  hereTerm.u.leftC
29b37 6f 6c 75 6d 6e 20 72 65 63 6f 72 64 20 74 68 65  olumn record the
29b38 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  .** cursor numbe
29b39 72 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 75 6d  r and column num
29b3a 62 65 72 20 66 6f 72 20 58 2e 20 20 57 68 65 72  ber for X.  Wher
29b3b 65 54 65 72 6d 2e 65 4f 70 65 72 61 74 6f 72 20  eTerm.eOperator 
29b3c 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65 20 3c  records.** the <
29b3d 6f 70 3e 20 75 73 69 6e 67 20 61 20 62 69 74 6d  op> using a bitm
29b3e 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 64 65 66  ask encoding def
29b3f 69 6e 65 64 20 62 79 20 57 4f 5f 78 78 78 20 62  ined by WO_xxx b
29b40 65 6c 6f 77 2e 20 20 54 68 65 0a 2a 2a 20 75 73  elow.  The.** us
29b41 65 20 6f 66 20 61 20 62 69 74 6d 61 73 6b 20 65  e of a bitmask e
29b42 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20  ncoding for the 
29b43 6f 70 65 72 61 74 6f 72 20 61 6c 6c 6f 77 73 20  operator allows 
29b44 75 73 20 74 6f 20 73 65 61 72 63 68 0a 2a 2a 20  us to search.** 
29b45 71 75 69 63 6b 6c 79 20 66 6f 72 20 74 65 72 6d  quickly for term
29b46 73 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79  s that match any
29b47 20 6f 66 20 73 65 76 65 72 61 6c 20 64 69 66 66   of several diff
29b48 65 72 65 6e 74 20 6f 70 65 72 61 74 6f 72 73 2e  erent operators.
29b49 0a 2a 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65  .**.** A WhereTe
29b4a 72 6d 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65  rm might also be
29b4b 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75 62   two or more sub
29b4c 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20  terms connected 
29b4d 62 79 20 4f 52 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  by OR:.**.**    
29b4e 20 20 20 20 20 28 74 31 2e 58 20 3c 6f 70 3e 20       (t1.X <op> 
29b4f 3c 65 78 70 72 3e 29 20 4f 52 20 28 74 31 2e 59  <expr>) OR (t1.Y
29b50 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 29 20 4f 52   <op> <expr>) OR
29b51 20 2e 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74   .....**.** In t
29b52 68 69 73 20 73 65 63 6f 6e 64 20 63 61 73 65 2c  his second case,
29b53 20 77 74 46 6c 61 67 20 61 73 20 74 68 65 20 54   wtFlag as the T
29b54 45 52 4d 5f 4f 52 49 4e 46 4f 20 73 65 74 20 61  ERM_ORINFO set a
29b55 6e 64 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  nd eOperator==WO
29b56 5f 4f 52 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57  _OR.** and the W
29b57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e  hereTerm.u.pOrIn
29b58 66 6f 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20  fo field points 
29b59 74 6f 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66  to auxiliary inf
29b5a 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a  ormation that.**
29b5b 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 61 62   is collected ab
29b5c 6f 75 74 20 74 68 65 0a 2a 2a 0a 2a 2a 20 49 66  out the.**.** If
29b5d 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57   a term in the W
29b5e 48 45 52 45 20 63 6c 61 75 73 65 20 64 6f 65 73  HERE clause does
29b5f 20 6e 6f 74 20 6d 61 74 63 68 20 65 69 74 68 65   not match eithe
29b60 72 20 6f 66 20 74 68 65 20 74 77 6f 20 70 72 65  r of the two pre
29b61 76 69 6f 75 73 0a 2a 2a 20 63 61 74 65 67 6f 72  vious.** categor
29b62 69 65 73 2c 20 74 68 65 6e 20 65 4f 70 65 72 61  ies, then eOpera
29b63 74 6f 72 3d 3d 30 2e 20 20 54 68 65 20 57 68 65  tor==0.  The Whe
29b64 72 65 54 65 72 6d 2e 70 45 78 70 72 20 66 69 65  reTerm.pExpr fie
29b65 6c 64 20 69 73 20 73 74 69 6c 6c 20 73 65 74 0a  ld is still set.
29b66 2a 2a 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e  ** to the origin
29b67 61 6c 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  al subexpression
29b68 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 77 74 46   content and wtF
29b69 6c 61 67 73 20 69 73 20 73 65 74 20 75 70 20 61  lags is set up a
29b6a 70 70 72 6f 70 72 69 61 74 65 6c 79 0a 2a 2a 20  ppropriately.** 
29b6b 62 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 69 65  but no other fie
29b6c 6c 64 73 20 69 6e 20 74 68 65 20 57 68 65 72 65  lds in the Where
29b6d 54 65 72 6d 20 6f 62 6a 65 63 74 20 61 72 65 20  Term object are 
29b6e 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2a 0a 2a  meaningful..**.*
29b6f 2a 20 57 68 65 6e 20 65 4f 70 65 72 61 74 6f 72  * When eOperator
29b70 21 3d 30 2c 20 70 72 65 72 65 71 52 69 67 68 74  !=0, prereqRight
29b71 20 61 6e 64 20 70 72 65 72 65 71 41 6c 6c 20 72   and prereqAll r
29b72 65 63 6f 72 64 20 73 65 74 73 20 6f 66 20 63 75  ecord sets of cu
29b73 72 73 6f 72 20 6e 75 6d 62 65 72 73 2c 0a 2a 2a  rsor numbers,.**
29b74 20 62 75 74 20 74 68 65 79 20 64 6f 20 73 6f 20   but they do so 
29b75 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 41 20 73  indirectly.  A s
29b76 69 6e 67 6c 65 20 57 68 65 72 65 4d 61 73 6b 53  ingle WhereMaskS
29b77 65 74 20 73 74 72 75 63 74 75 72 65 20 74 72 61  et structure tra
29b78 6e 73 6c 61 74 65 73 0a 2a 2a 20 63 75 72 73 6f  nslates.** curso
29b79 72 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 62 69  r number into bi
29b7a 74 73 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73  ts and the trans
29b7b 6c 61 74 65 64 20 62 69 74 20 69 73 20 73 74 6f  lated bit is sto
29b7c 72 65 64 20 69 6e 20 74 68 65 20 70 72 65 72 65  red in the prere
29b7d 71 0a 2a 2a 20 66 69 65 6c 64 73 2e 20 20 54 68  q.** fields.  Th
29b7e 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73  e translation is
29b7f 20 75 73 65 64 20 69 6e 20 6f 72 64 65 72 20 74   used in order t
29b80 6f 20 6d 61 78 69 6d 69 7a 65 20 74 68 65 20 6e  o maximize the n
29b81 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 69 74 73  umber of.** bits
29b82 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 69   that will fit i
29b83 6e 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 54 68  n a Bitmask.  Th
29b84 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
29b85 6d 62 65 72 73 20 6d 69 67 68 74 20 62 65 0a 2a  mbers might be.*
29b86 2a 20 73 70 72 65 61 64 20 6f 75 74 20 6f 76 65  * spread out ove
29b87 72 20 74 68 65 20 6e 6f 6e 2d 6e 65 67 61 74 69  r the non-negati
29b88 76 65 20 69 6e 74 65 67 65 72 73 2e 20 20 46 6f  ve integers.  Fo
29b89 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 63  r example, the c
29b8a 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 73  ursor.** numbers
29b8b 20 6d 69 67 68 74 20 62 65 20 33 2c 20 38 2c 20   might be 3, 8, 
29b8c 39 2c 20 31 30 2c 20 32 30 2c 20 32 33 2c 20 34  9, 10, 20, 23, 4
29b8d 31 2c 20 61 6e 64 20 34 35 2e 20 20 54 68 65 20  1, and 45.  The 
29b8e 57 68 65 72 65 4d 61 73 6b 53 65 74 0a 2a 2a 20  WhereMaskSet.** 
29b8f 74 72 61 6e 73 6c 61 74 65 73 20 74 68 65 73 65  translates these
29b90 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 20 6e   sparse cursor n
29b91 75 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73  umbers into cons
29b92 65 63 75 74 69 76 65 20 69 6e 74 65 67 65 72 73  ecutive integers
29b93 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  .** beginning wi
29b94 74 68 20 30 20 69 6e 20 6f 72 64 65 72 20 74 6f  th 0 in order to
29b95 20 6d 61 6b 65 20 74 68 65 20 62 65 73 74 20 70   make the best p
29b96 6f 73 73 69 62 6c 65 20 75 73 65 20 6f 66 20 74  ossible use of t
29b97 68 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20  he available.** 
29b98 62 69 74 73 20 69 6e 20 74 68 65 20 42 69 74 6d  bits in the Bitm
29b99 61 73 6b 2e 20 20 53 6f 2c 20 69 6e 20 74 68 65  ask.  So, in the
29b9a 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20   example above, 
29b9b 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  the cursor numbe
29b9c 72 73 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d  rs.** would be m
29b9d 61 70 70 65 64 20 69 6e 74 6f 20 69 6e 74 65 67  apped into integ
29b9e 65 72 73 20 30 20 74 68 72 6f 75 67 68 20 37 2e  ers 0 through 7.
29b9f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  .**.** The numbe
29ba0 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20  r of terms in a 
29ba1 6a 6f 69 6e 20 69 73 20 6c 69 6d 69 74 65 64 20  join is limited 
29ba2 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  by the number of
29ba3 20 62 69 74 73 0a 2a 2a 20 69 6e 20 70 72 65 72   bits.** in prer
29ba4 65 71 52 69 67 68 74 20 61 6e 64 20 70 72 65 72  eqRight and prer
29ba5 65 71 41 6c 6c 2e 20 20 54 68 65 20 64 65 66 61  eqAll.  The defa
29ba6 75 6c 74 20 69 73 20 36 34 20 62 69 74 73 2c 20  ult is 64 bits, 
29ba7 68 65 6e 63 65 20 53 51 4c 69 74 65 0a 2a 2a 20  hence SQLite.** 
29ba8 69 73 20 6f 6e 6c 79 20 61 62 6c 65 20 74 6f 20  is only able to 
29ba9 70 72 6f 63 65 73 73 20 6a 6f 69 6e 73 20 77 69  process joins wi
29baa 74 68 20 36 34 20 6f 72 20 66 65 77 65 72 20 74  th 64 or fewer t
29bab 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  ables..*/.typede
29bac 66 20 73 74 72 75 63 74 20 57 68 65 72 65 54 65  f struct WhereTe
29bad 72 6d 20 57 68 65 72 65 54 65 72 6d 3b 0a 73 74  rm WhereTerm;.st
29bae 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20 7b  ruct WhereTerm {
29baf 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20  .  Expr *pExpr; 
29bb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
29bb1 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 75 62  inter to the sub
29bb2 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
29bb3 69 73 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a  is this term */.
29bb4 20 20 69 6e 74 20 69 50 61 72 65 6e 74 3b 20 20    int iParent;  
29bb5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73            /* Dis
29bb6 61 62 6c 65 20 70 57 43 2d 3e 61 5b 69 50 61 72  able pWC->a[iPar
29bb7 65 6e 74 5d 20 77 68 65 6e 20 74 68 69 73 20 74  ent] when this t
29bb8 65 72 6d 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a  erm disabled */.
29bb9 20 20 69 6e 74 20 6c 65 66 74 43 75 72 73 6f 72    int leftCursor
29bba 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  ;         /* Cur
29bbb 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 58 20  sor number of X 
29bbc 69 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72  in "X <op> <expr
29bbd 3e 22 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a  >" */.  union {.
29bbe 20 20 20 20 69 6e 74 20 6c 65 66 74 43 6f 6c 75      int leftColu
29bbf 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  mn;         /* C
29bc0 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 20  olumn number of 
29bc1 58 20 69 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78  X in "X <op> <ex
29bc2 70 72 3e 22 20 2a 2f 0a 20 20 20 20 57 68 65 72  pr>" */.    Wher
29bc3 65 4f 72 49 6e 66 6f 20 2a 70 4f 72 49 6e 66 6f  eOrInfo *pOrInfo
29bc4 3b 20 20 20 2f 2a 20 45 78 74 72 61 20 69 6e 66  ;   /* Extra inf
29bc5 6f 72 6d 61 74 69 6f 6e 20 69 66 20 65 4f 70 65  ormation if eOpe
29bc6 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 2a 2f 0a  rator==WO_OR */.
29bc7 20 20 20 20 57 68 65 72 65 41 6e 64 49 6e 66 6f      WhereAndInfo
29bc8 20 2a 70 41 6e 64 49 6e 66 6f 3b 20 2f 2a 20 45   *pAndInfo; /* E
29bc9 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  xtra information
29bca 20 69 66 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57   if eOperator==W
29bcb 4f 5f 41 4e 44 20 2a 2f 0a 20 20 7d 20 75 3b 0a  O_AND */.  } u;.
29bcc 20 20 75 31 36 20 65 4f 70 65 72 61 74 6f 72 3b    u16 eOperator;
29bcd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 57            /* A W
29bce 4f 5f 78 78 20 76 61 6c 75 65 20 64 65 73 63 72  O_xx value descr
29bcf 69 62 69 6e 67 20 3c 6f 70 3e 20 2a 2f 0a 20 20  ibing <op> */.  
29bd0 75 38 20 77 74 46 6c 61 67 73 3b 20 20 20 20 20  u8 wtFlags;     
29bd1 20 20 20 20 20 20 20 20 2f 2a 20 54 45 52 4d 5f          /* TERM_
29bd2 78 78 78 20 62 69 74 20 66 6c 61 67 73 2e 20 20  xxx bit flags.  
29bd3 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 75  See below */.  u
29bd4 38 20 6e 43 68 69 6c 64 3b 20 20 20 20 20 20 20  8 nChild;       
29bd5 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
29bd6 20 6f 66 20 63 68 69 6c 64 72 65 6e 20 74 68 61   of children tha
29bd7 74 20 6d 75 73 74 20 64 69 73 61 62 6c 65 20 75  t must disable u
29bd8 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  s */.  WhereClau
29bd9 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20 2f  se *pWC;       /
29bda 2a 20 54 68 65 20 63 6c 61 75 73 65 20 74 68 69  * The clause thi
29bdb 73 20 74 65 72 6d 20 69 73 20 70 61 72 74 20 6f  s term is part o
29bdc 66 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70  f */.  Bitmask p
29bdd 72 65 72 65 71 52 69 67 68 74 3b 20 20 20 20 2f  rereqRight;    /
29bde 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74 61 62  * Bitmask of tab
29bdf 6c 65 73 20 75 73 65 64 20 62 79 20 70 45 78 70  les used by pExp
29be0 72 2d 3e 70 52 69 67 68 74 20 2a 2f 0a 20 20 42  r->pRight */.  B
29be1 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41 6c 6c  itmask prereqAll
29be2 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73  ;      /* Bitmas
29be3 6b 20 6f 66 20 74 61 62 6c 65 73 20 72 65 66 65  k of tables refe
29be4 72 65 6e 63 65 64 20 62 79 20 70 45 78 70 72 20  renced by pExpr 
29be5 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  */.};../*.** All
29be6 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 57  owed values of W
29be7 68 65 72 65 54 65 72 6d 2e 77 74 46 6c 61 67 73  hereTerm.wtFlags
29be8 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d  .*/.#define TERM
29be9 5f 44 59 4e 41 4d 49 43 20 20 20 20 30 78 30 31  _DYNAMIC    0x01
29bea 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61     /* Need to ca
29beb 6c 6c 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  ll sqlite3ExprDe
29bec 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 29 20  lete(db, pExpr) 
29bed 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f  */.#define TERM_
29bee 56 49 52 54 55 41 4c 20 20 20 20 30 78 30 32 20  VIRTUAL    0x02 
29bef 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 74 68    /* Added by th
29bf0 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 44 6f  e optimizer.  Do
29bf1 20 6e 6f 74 20 63 6f 64 65 20 2a 2f 0a 23 64 65   not code */.#de
29bf2 66 69 6e 65 20 54 45 52 4d 5f 43 4f 44 45 44 20  fine TERM_CODED 
29bf3 20 20 20 20 20 30 78 30 34 20 20 20 2f 2a 20 54       0x04   /* T
29bf4 68 69 73 20 74 65 72 6d 20 69 73 20 61 6c 72 65  his term is alre
29bf5 61 64 79 20 63 6f 64 65 64 20 2a 2f 0a 23 64 65  ady coded */.#de
29bf6 66 69 6e 65 20 54 45 52 4d 5f 43 4f 50 49 45 44  fine TERM_COPIED
29bf7 20 20 20 20 20 30 78 30 38 20 20 20 2f 2a 20 48       0x08   /* H
29bf8 61 73 20 61 20 63 68 69 6c 64 20 2a 2f 0a 23 64  as a child */.#d
29bf9 65 66 69 6e 65 20 54 45 52 4d 5f 4f 52 49 4e 46  efine TERM_ORINF
29bfa 4f 20 20 20 20 20 30 78 31 30 20 20 20 2f 2a 20  O     0x10   /* 
29bfb 4e 65 65 64 20 74 6f 20 66 72 65 65 20 74 68 65  Need to free the
29bfc 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72   WhereTerm.u.pOr
29bfd 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 23  Info object */.#
29bfe 64 65 66 69 6e 65 20 54 45 52 4d 5f 41 4e 44 49  define TERM_ANDI
29bff 4e 46 4f 20 20 20 20 30 78 32 30 20 20 20 2f 2a  NFO    0x20   /*
29c00 20 4e 65 65 64 20 74 6f 20 66 72 65 65 20 74 68   Need to free th
29c01 65 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 41  e WhereTerm.u.pA
29c02 6e 64 49 6e 66 6f 20 6f 62 6a 20 2a 2f 0a 23 64  ndInfo obj */.#d
29c03 65 66 69 6e 65 20 54 45 52 4d 5f 4f 52 5f 4f 4b  efine TERM_OR_OK
29c04 20 20 20 20 20 20 30 78 34 30 20 20 20 2f 2a 20        0x40   /* 
29c05 55 73 65 64 20 64 75 72 69 6e 67 20 4f 52 2d 63  Used during OR-c
29c06 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67  lause processing
29c07 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e   */../*.** An in
29c08 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
29c09 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
29c0a 65 20 68 6f 6c 64 73 20 61 6c 6c 20 69 6e 66 6f  e holds all info
29c0b 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 0a  rmation about a.
29c0c 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  ** WHERE clause.
29c0d 20 20 4d 6f 73 74 6c 79 20 74 68 69 73 20 69 73    Mostly this is
29c0e 20 61 20 63 6f 6e 74 61 69 6e 65 72 20 66 6f 72   a container for
29c0f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 57 68 65   one or more Whe
29c10 72 65 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 72 75  reTerms..*/.stru
29c11 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 20 7b  ct WhereClause {
29c12 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
29c13 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
29c14 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
29c15 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b  t */.  WhereMask
29c16 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20  Set *pMaskSet;  
29c17 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20 74 61  /* Mapping of ta
29c18 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ble cursor numbe
29c19 72 73 20 74 6f 20 62 69 74 6d 61 73 6b 73 20 2a  rs to bitmasks *
29c1a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20  /.  u8 op;      
29c1b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29c1c 53 70 6c 69 74 20 6f 70 65 72 61 74 6f 72 2e 20  Split operator. 
29c1d 20 54 4b 5f 41 4e 44 20 6f 72 20 54 4b 5f 4f 52   TK_AND or TK_OR
29c1e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
29c1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
29c20 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
29c21 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6c 6f 74  s */.  int nSlot
29c22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
29c23 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
29c24 72 69 65 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20  ries in a[] */. 
29c25 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 20 20   WhereTerm *a;  
29c26 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 61 63            /* Eac
29c27 68 20 61 5b 5d 20 64 65 73 63 72 69 62 65 73 20  h a[] describes 
29c28 61 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48  a term of the WH
29c29 45 52 45 20 63 6c 75 61 73 65 20 2a 2f 0a 20 20  ERE cluase */.  
29c2a 57 68 65 72 65 54 65 72 6d 20 61 53 74 61 74 69  WhereTerm aStati
29c2b 63 5b 34 5d 3b 20 20 20 20 2f 2a 20 49 6e 69 74  c[4];    /* Init
29c2c 69 61 6c 20 73 74 61 74 69 63 20 73 70 61 63 65  ial static space
29c2d 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 7d 3b 0a 0a   for a[] */.};..
29c2e 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65 72  /*.** A WhereTer
29c2f 6d 20 77 69 74 68 20 65 4f 70 65 72 61 74 6f 72  m with eOperator
29c30 3d 3d 57 4f 5f 4f 52 20 68 61 73 20 69 74 73 20  ==WO_OR has its 
29c31 75 2e 70 4f 72 49 6e 66 6f 20 70 6f 69 6e 74 65  u.pOrInfo pointe
29c32 72 20 73 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79  r set to.** a dy
29c33 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
29c34 74 65 64 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  ted instance of 
29c35 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
29c36 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
29c37 63 74 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 7b  ct WhereOrInfo {
29c38 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 77  .  WhereClause w
29c39 63 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  c;          /* D
29c3a 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 69 6e 74  ecomposition int
29c3b 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20  o subterms */.  
29c3c 42 69 74 6d 61 73 6b 20 69 6e 64 65 78 61 62 6c  Bitmask indexabl
29c3d 65 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d  e;       /* Bitm
29c3e 61 73 6b 20 6f 66 20 61 6c 6c 20 69 6e 64 65 78  ask of all index
29c3f 61 62 6c 65 20 74 61 62 6c 65 73 20 69 6e 20 74  able tables in t
29c40 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a 7d 3b 0a  he clause */.};.
29c41 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65  ./*.** A WhereTe
29c42 72 6d 20 77 69 74 68 20 65 4f 70 65 72 61 74 6f  rm with eOperato
29c43 72 3d 3d 57 4f 5f 41 4e 44 20 68 61 73 20 69 74  r==WO_AND has it
29c44 73 20 75 2e 70 41 6e 64 49 6e 66 6f 20 70 6f 69  s u.pAndInfo poi
29c45 6e 74 65 72 20 73 65 74 20 74 6f 0a 2a 2a 20 61  nter set to.** a
29c46 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
29c47 6f 63 61 74 65 64 20 69 6e 73 74 61 6e 63 65 20  ocated instance 
29c48 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
29c49 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
29c4a 74 72 75 63 74 20 57 68 65 72 65 41 6e 64 49 6e  truct WhereAndIn
29c4b 66 6f 20 7b 0a 20 20 57 68 65 72 65 43 6c 61 75  fo {.  WhereClau
29c4c 73 65 20 77 63 3b 20 20 20 20 20 20 20 20 20 20  se wc;          
29c4d 2f 2a 20 54 68 65 20 73 75 62 65 78 70 72 65 73  /* The subexpres
29c4e 73 69 6f 6e 20 62 72 6f 6b 65 6e 20 6f 75 74 20  sion broken out 
29c4f 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
29c50 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
29c51 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
29c52 75 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20  ure keeps track 
29c53 6f 66 20 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20  of a mapping.** 
29c54 62 65 74 77 65 65 6e 20 56 44 42 45 20 63 75 72  between VDBE cur
29c55 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 6e 64 20  sor numbers and 
29c56 62 69 74 73 20 6f 66 20 74 68 65 20 62 69 74 6d  bits of the bitm
29c57 61 73 6b 73 20 69 6e 20 57 68 65 72 65 54 65 72  asks in WhereTer
29c58 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42  m..**.** The VDB
29c59 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  E cursor numbers
29c5a 20 61 72 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67   are small integ
29c5b 65 72 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  ers contained in
29c5c 20 0a 2a 2a 20 53 72 63 4c 69 73 74 5f 69 74 65   .** SrcList_ite
29c5d 6d 2e 69 43 75 72 73 6f 72 20 61 6e 64 20 45 78  m.iCursor and Ex
29c5e 70 72 2e 69 54 61 62 6c 65 20 66 69 65 6c 64 73  pr.iTable fields
29c5f 2e 20 20 46 6f 72 20 61 6e 79 20 67 69 76 65 6e  .  For any given
29c60 20 57 48 45 52 45 20 0a 2a 2a 20 63 6c 61 75 73   WHERE .** claus
29c61 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75  e, the cursor nu
29c62 6d 62 65 72 73 20 6d 69 67 68 74 20 6e 6f 74 20  mbers might not 
29c63 62 65 67 69 6e 20 77 69 74 68 20 30 20 61 6e 64  begin with 0 and
29c64 20 74 68 65 79 20 6d 69 67 68 74 0a 2a 2a 20 63   they might.** c
29c65 6f 6e 74 61 69 6e 20 67 61 70 73 20 69 6e 20 74  ontain gaps in t
29c66 68 65 20 6e 75 6d 62 65 72 69 6e 67 20 73 65 71  he numbering seq
29c67 75 65 6e 63 65 2e 20 20 42 75 74 20 77 65 20 77  uence.  But we w
29c68 61 6e 74 20 74 6f 20 6d 61 6b 65 20 6d 61 78 69  ant to make maxi
29c69 6d 75 6d 0a 2a 2a 20 75 73 65 20 6f 66 20 74 68  mum.** use of th
29c6a 65 20 62 69 74 73 20 69 6e 20 6f 75 72 20 62 69  e bits in our bi
29c6b 74 6d 61 73 6b 73 2e 20 20 54 68 69 73 20 73 74  tmasks.  This st
29c6c 72 75 63 74 75 72 65 20 70 72 6f 76 69 64 65 73  ructure provides
29c6d 20 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20 66 72   a mapping.** fr
29c6e 6f 6d 20 74 68 65 20 73 70 61 72 73 65 20 63 75  om the sparse cu
29c6f 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74  rsor numbers int
29c70 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20 69 6e  o consecutive in
29c71 74 65 67 65 72 73 20 62 65 67 69 6e 6e 69 6e 67  tegers beginning
29c72 0a 2a 2a 20 77 69 74 68 20 30 2e 0a 2a 2a 0a 2a  .** with 0..**.*
29c73 2a 20 49 66 20 57 68 65 72 65 4d 61 73 6b 53 65  * If WhereMaskSe
29c74 74 2e 69 78 5b 41 5d 3d 3d 42 20 69 74 20 6d 65  t.ix[A]==B it me
29c75 61 6e 73 20 74 68 61 74 20 54 68 65 20 41 2d 74  ans that The A-t
29c76 68 20 62 69 74 20 6f 66 20 61 20 42 69 74 6d 61  h bit of a Bitma
29c77 73 6b 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  sk.** correspond
29c78 73 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  s VDBE cursor nu
29c79 6d 62 65 72 20 42 2e 20 20 54 68 65 20 41 2d 74  mber B.  The A-t
29c7a 68 20 62 69 74 20 6f 66 20 61 20 62 69 74 6d 61  h bit of a bitma
29c7b 73 6b 20 69 73 20 31 3c 3c 41 2e 0a 2a 2a 0a 2a  sk is 1<<A..**.*
29c7c 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  * For example, i
29c7d 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
29c7e 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20 75 73  se expression us
29c7f 65 64 20 74 68 65 73 65 20 56 44 42 45 0a 2a 2a  ed these VDBE.**
29c80 20 63 75 72 73 6f 72 73 3a 20 20 34 2c 20 35 2c   cursors:  4, 5,
29c81 20 38 2c 20 32 39 2c 20 35 37 2c 20 37 33 2e 20   8, 29, 57, 73. 
29c82 20 54 68 65 6e 20 74 68 65 20 20 57 68 65 72 65   Then the  Where
29c83 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74 75 72  MaskSet structur
29c84 65 0a 2a 2a 20 77 6f 75 6c 64 20 6d 61 70 20 74  e.** would map t
29c85 68 6f 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  hose cursor numb
29c86 65 72 73 20 69 6e 74 6f 20 62 69 74 73 20 30 20  ers into bits 0 
29c87 74 68 72 6f 75 67 68 20 35 2e 0a 2a 2a 0a 2a 2a  through 5..**.**
29c88 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6d   Note that the m
29c89 61 70 70 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65  apping is not ne
29c8a 63 65 73 73 61 72 69 6c 79 20 6f 72 64 65 72 65  cessarily ordere
29c8b 64 2e 20 20 49 6e 20 74 68 65 20 65 78 61 6d 70  d.  In the examp
29c8c 6c 65 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 65  le.** above, the
29c8d 20 6d 61 70 70 69 6e 67 20 6d 69 67 68 74 20 67   mapping might g
29c8e 6f 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 34 2d  o like this:  4-
29c8f 3e 33 2c 20 35 2d 3e 31 2c 20 38 2d 3e 32 2c 20  >3, 5->1, 8->2, 
29c90 32 39 2d 3e 30 2c 0a 2a 2a 20 35 37 2d 3e 35 2c  29->0,.** 57->5,
29c91 20 37 33 2d 3e 34 2e 20 20 4f 72 20 6f 6e 65 20   73->4.  Or one 
29c92 6f 66 20 37 31 39 20 6f 74 68 65 72 20 63 6f 6d  of 719 other com
29c93 62 69 6e 61 74 69 6f 6e 73 20 6d 69 67 68 74 20  binations might 
29c94 62 65 20 75 73 65 64 2e 20 49 74 0a 2a 2a 20 64  be used. It.** d
29c95 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6d  oes not really m
29c96 61 74 74 65 72 2e 20 20 57 68 61 74 20 69 73 20  atter.  What is 
29c97 69 6d 70 6f 72 74 61 6e 74 20 69 73 20 74 68 61  important is tha
29c98 74 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 0a  t sparse cursor.
29c99 2a 2a 20 6e 75 6d 62 65 72 73 20 61 6c 6c 20 67  ** numbers all g
29c9a 65 74 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 62  et mapped into b
29c9b 69 74 20 6e 75 6d 62 65 72 73 20 74 68 61 74 20  it numbers that 
29c9c 62 65 67 69 6e 20 77 69 74 68 20 30 20 61 6e 64  begin with 0 and
29c9d 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 6f 20 67   contain.** no g
29c9e 61 70 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57  aps..*/.struct W
29c9f 68 65 72 65 4d 61 73 6b 53 65 74 20 7b 0a 20 20  hereMaskSet {.  
29ca0 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20  int n;          
29ca1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29ca2 20 4e 75 6d 62 65 72 20 6f 66 20 61 73 73 69 67   Number of assig
29ca3 6e 65 64 20 63 75 72 73 6f 72 20 76 61 6c 75 65  ned cursor value
29ca4 73 20 2a 2f 0a 20 20 69 6e 74 20 69 78 5b 42 4d  s */.  int ix[BM
29ca5 53 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  S];             
29ca6 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 61       /* Cursor a
29ca7 73 73 69 67 6e 65 64 20 74 6f 20 65 61 63 68 20  ssigned to each 
29ca8 62 69 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  bit */.};../*.**
29ca9 20 41 20 57 68 65 72 65 43 6f 73 74 20 6f 62 6a   A WhereCost obj
29caa 65 63 74 20 72 65 63 6f 72 64 73 20 61 20 6c 6f  ect records a lo
29cab 6f 6b 75 70 20 73 74 72 61 74 65 67 79 20 61 6e  okup strategy an
29cac 64 20 74 68 65 20 65 73 74 69 6d 61 74 65 64 0a  d the estimated.
29cad 2a 2a 20 63 6f 73 74 20 6f 66 20 70 75 72 73 75  ** cost of pursu
29cae 69 6e 67 20 74 68 61 74 20 73 74 72 61 74 65 67  ing that strateg
29caf 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  y..*/.struct Whe
29cb0 72 65 43 6f 73 74 20 7b 0a 20 20 57 68 65 72 65  reCost {.  Where
29cb1 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 2f 2a  Plan plan;    /*
29cb2 20 54 68 65 20 6c 6f 6f 6b 75 70 20 73 74 72 61   The lookup stra
29cb3 74 65 67 79 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  tegy */.  double
29cb4 20 72 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20   rCost;      /* 
29cb5 4f 76 65 72 61 6c 6c 20 63 6f 73 74 20 6f 66 20  Overall cost of 
29cb6 70 75 72 73 75 69 6e 67 20 74 68 69 73 20 73 65  pursuing this se
29cb7 61 72 63 68 20 73 74 72 61 74 65 67 79 20 2a 2f  arch strategy */
29cb8 0a 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77 3b 20  .  double nRow; 
29cb9 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74        /* Estimat
29cba 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74  ed number of out
29cbb 70 75 74 20 72 6f 77 73 20 2a 2f 0a 7d 3b 0a 0a  put rows */.};..
29cbc 2f 2a 0a 2a 2a 20 42 69 74 6d 61 73 6b 73 20 66  /*.** Bitmasks f
29cbd 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73  or the operators
29cbe 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72   that indices ar
29cbf 65 20 61 62 6c 65 20 74 6f 20 65 78 70 6c 6f 69  e able to exploi
29cc0 74 2e 20 20 41 6e 0a 2a 2a 20 4f 52 2d 65 64 20  t.  An.** OR-ed 
29cc1 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74  combination of t
29cc2 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20  hese values can 
29cc3 62 65 20 75 73 65 64 20 77 68 65 6e 20 73 65 61  be used when sea
29cc4 72 63 68 69 6e 67 20 66 6f 72 0a 2a 2a 20 74 65  rching for.** te
29cc5 72 6d 73 20 69 6e 20 74 68 65 20 77 68 65 72 65  rms in the where
29cc6 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 23 64 65 66   clause..*/.#def
29cc7 69 6e 65 20 57 4f 5f 49 4e 20 20 20 20 20 30 78  ine WO_IN     0x
29cc8 30 30 31 0a 23 64 65 66 69 6e 65 20 57 4f 5f 45  001.#define WO_E
29cc9 51 20 20 20 20 20 30 78 30 30 32 0a 23 64 65 66  Q     0x002.#def
29cca 69 6e 65 20 57 4f 5f 4c 54 20 20 20 20 20 28 57  ine WO_LT     (W
29ccb 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 54 2d 54 4b 5f  O_EQ<<(TK_LT-TK_
29ccc 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f  EQ)).#define WO_
29ccd 4c 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28  LE     (WO_EQ<<(
29cce 54 4b 5f 4c 45 2d 54 4b 5f 45 51 29 29 0a 23 64  TK_LE-TK_EQ)).#d
29ccf 65 66 69 6e 65 20 57 4f 5f 47 54 20 20 20 20 20  efine WO_GT     
29cd0 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 54 2d 54  (WO_EQ<<(TK_GT-T
29cd1 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57  K_EQ)).#define W
29cd2 4f 5f 47 45 20 20 20 20 20 28 57 4f 5f 45 51 3c  O_GE     (WO_EQ<
29cd3 3c 28 54 4b 5f 47 45 2d 54 4b 5f 45 51 29 29 0a  <(TK_GE-TK_EQ)).
29cd4 23 64 65 66 69 6e 65 20 57 4f 5f 4d 41 54 43 48  #define WO_MATCH
29cd5 20 20 30 78 30 34 30 0a 23 64 65 66 69 6e 65 20    0x040.#define 
29cd6 57 4f 5f 49 53 4e 55 4c 4c 20 30 78 30 38 30 0a  WO_ISNULL 0x080.
29cd7 23 64 65 66 69 6e 65 20 57 4f 5f 4f 52 20 20 20  #define WO_OR   
29cd8 20 20 30 78 31 30 30 20 20 20 20 20 20 20 2f 2a    0x100       /*
29cd9 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d   Two or more OR-
29cda 63 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d 73 20  connected terms 
29cdb 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 41 4e  */.#define WO_AN
29cdc 44 20 20 20 20 30 78 32 30 30 20 20 20 20 20 20  D    0x200      
29cdd 20 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20   /* Two or more 
29cde 41 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65  AND-connected te
29cdf 72 6d 73 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20  rms */..#define 
29ce0 57 4f 5f 41 4c 4c 20 20 20 20 30 78 66 66 66 20  WO_ALL    0xfff 
29ce1 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
29ce2 20 61 6c 6c 20 70 6f 73 73 69 62 6c 65 20 57 4f   all possible WO
29ce3 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 23 64 65  _* values */.#de
29ce4 66 69 6e 65 20 57 4f 5f 53 49 4e 47 4c 45 20 30  fine WO_SINGLE 0
29ce5 78 30 66 66 20 20 20 20 20 20 20 2f 2a 20 4d 61  x0ff       /* Ma
29ce6 73 6b 20 6f 66 20 61 6c 6c 20 6e 6f 6e 2d 63 6f  sk of all non-co
29ce7 6d 70 6f 75 6e 64 20 57 4f 5f 2a 20 76 61 6c 75  mpound WO_* valu
29ce8 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c  es */../*.** Val
29ce9 75 65 20 66 6f 72 20 77 73 46 6c 61 67 73 20 72  ue for wsFlags r
29cea 65 74 75 72 6e 65 64 20 62 79 20 62 65 73 74 49  eturned by bestI
29ceb 6e 64 65 78 28 29 20 61 6e 64 20 73 74 6f 72 65  ndex() and store
29cec 64 20 69 6e 0a 2a 2a 20 57 68 65 72 65 4c 65 76  d in.** WhereLev
29ced 65 6c 2e 77 73 46 6c 61 67 73 2e 20 20 54 68 65  el.wsFlags.  The
29cee 73 65 20 66 6c 61 67 73 20 64 65 74 65 72 6d 69  se flags determi
29cef 6e 65 20 77 68 69 63 68 20 73 65 61 72 63 68 0a  ne which search.
29cf0 2a 2a 20 73 74 72 61 74 65 67 69 65 73 20 61 72  ** strategies ar
29cf1 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a  e appropriate..*
29cf2 2a 0a 2a 2a 20 54 68 65 20 6c 65 61 73 74 20 73  *.** The least s
29cf3 69 67 6e 69 66 69 63 61 6e 74 20 31 32 20 62 69  ignificant 12 bi
29cf4 74 73 20 69 73 20 72 65 73 65 72 76 65 64 20 61  ts is reserved a
29cf5 73 20 61 20 6d 61 73 6b 20 66 6f 72 20 57 4f 5f  s a mask for WO_
29cf6 20 76 61 6c 75 65 73 20 61 62 6f 76 65 2e 0a 2a   values above..*
29cf7 2a 20 54 68 65 20 57 68 65 72 65 4c 65 76 65 6c  * The WhereLevel
29cf8 2e 77 73 46 6c 61 67 73 20 66 69 65 6c 64 20 69  .wsFlags field i
29cf9 73 20 75 73 75 61 6c 6c 79 20 73 65 74 20 74 6f  s usually set to
29cfa 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 7c 57 4f 5f   WO_IN|WO_EQ|WO_
29cfb 49 53 4e 55 4c 4c 2e 0a 2a 2a 20 42 75 74 20 69  ISNULL..** But i
29cfc 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74  f the table is t
29cfd 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f  he right table o
29cfe 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e 2c 20 57  f a left join, W
29cff 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67  hereLevel.wsFlag
29d00 73 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 57  s.** is set to W
29d01 4f 5f 49 4e 7c 57 4f 5f 45 51 2e 20 20 54 68 65  O_IN|WO_EQ.  The
29d02 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c   WhereLevel.wsFl
29d03 61 67 73 20 66 69 65 6c 64 20 63 61 6e 20 74 68  ags field can th
29d04 65 6e 20 62 65 20 75 73 65 64 20 61 73 0a 2a 2a  en be used as.**
29d05 20 74 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65   the "op" parame
29d06 74 65 72 20 74 6f 20 66 69 6e 64 54 65 72 6d 20  ter to findTerm 
29d07 77 68 65 6e 20 77 65 20 61 72 65 20 72 65 73 6f  when we are reso
29d08 6c 76 69 6e 67 20 65 71 75 61 6c 69 74 79 20 63  lving equality c
29d09 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a 20 49  onstraints..** I
29d0a 53 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74  SNULL constraint
29d0b 73 20 77 69 6c 6c 20 74 68 65 6e 20 6e 6f 74 20  s will then not 
29d0c 62 65 20 75 73 65 64 20 6f 6e 20 74 68 65 20 72  be used on the r
29d0d 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20  ight table of a 
29d0e 6c 65 66 74 0a 2a 2a 20 6a 6f 69 6e 2e 20 20 54  left.** join.  T
29d0f 69 63 6b 65 74 73 20 23 32 31 37 37 20 61 6e 64  ickets #2177 and
29d10 20 23 32 31 38 39 2e 0a 2a 2f 0a 23 64 65 66 69   #2189..*/.#defi
29d11 6e 65 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45  ne WHERE_ROWID_E
29d12 51 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30  Q     0x00001000
29d13 20 20 2f 2a 20 72 6f 77 69 64 3d 45 58 50 52 20    /* rowid=EXPR 
29d14 6f 72 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e  or rowid IN (...
29d15 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ) */.#define WHE
29d16 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 20  RE_ROWID_RANGE  
29d17 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 72  0x00002000  /* r
29d18 6f 77 69 64 3c 45 58 50 52 20 61 6e 64 2f 6f 72  owid<EXPR and/or
29d19 20 72 6f 77 69 64 3e 45 58 50 52 20 2a 2f 0a 23   rowid>EXPR */.#
29d1a 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c  define WHERE_COL
29d1b 55 4d 4e 5f 45 51 20 20 20 20 30 78 30 30 30 31  UMN_EQ    0x0001
29d1c 30 30 30 30 20 20 2f 2a 20 78 3d 45 58 50 52 20  0000  /* x=EXPR 
29d1d 6f 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f  or x IN (...) */
29d1e 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43  .#define WHERE_C
29d1f 4f 4c 55 4d 4e 5f 52 41 4e 47 45 20 30 78 30 30  OLUMN_RANGE 0x00
29d20 30 32 30 30 30 30 20 20 2f 2a 20 78 3c 45 58 50  020000  /* x<EXP
29d21 52 20 61 6e 64 2f 6f 72 20 78 3e 45 58 50 52 20  R and/or x>EXPR 
29d22 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
29d23 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 20 20 20 30 78  _COLUMN_IN    0x
29d24 30 30 30 34 30 30 30 30 20 20 2f 2a 20 78 20 49  00040000  /* x I
29d25 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69  N (...) */.#defi
29d26 6e 65 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44  ne WHERE_INDEXED
29d27 20 20 20 20 20 20 30 78 30 30 30 37 30 30 30 30        0x00070000
29d28 20 20 2f 2a 20 41 6e 79 74 68 69 6e 67 20 74 68    /* Anything th
29d29 61 74 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78  at uses an index
29d2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
29d2b 45 5f 49 4e 5f 41 42 4c 45 20 20 20 20 20 20 30  E_IN_ABLE      0
29d2c 78 30 30 30 37 31 30 30 30 20 20 2f 2a 20 41 62  x00071000  /* Ab
29d2d 6c 65 20 74 6f 20 73 75 70 70 6f 72 74 20 61 6e  le to support an
29d2e 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a   IN operator */.
29d2f 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 54 4f  #define WHERE_TO
29d30 50 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 30 31  P_LIMIT    0x001
29d31 30 30 30 30 30 20 20 2f 2a 20 78 3c 45 58 50 52  00000  /* x<EXPR
29d32 20 6f 72 20 78 3c 3d 45 58 50 52 20 63 6f 6e 73   or x<=EXPR cons
29d33 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  traint */.#defin
29d34 65 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49  e WHERE_BTM_LIMI
29d35 54 20 20 20 20 30 78 30 30 32 30 30 30 30 30 20  T    0x00200000 
29d36 20 2f 2a 20 78 3e 45 58 50 52 20 6f 72 20 78 3e   /* x>EXPR or x>
29d37 3d 45 58 50 52 20 63 6f 6e 73 74 72 61 69 6e 74  =EXPR constraint
29d38 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
29d39 45 5f 49 44 58 5f 4f 4e 4c 59 20 20 20 20 20 30  E_IDX_ONLY     0
29d3a 78 30 30 38 30 30 30 30 30 20 20 2f 2a 20 55 73  x00800000  /* Us
29d3b 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2d 20 6f  e index only - o
29d3c 6d 69 74 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65  mit table */.#de
29d3d 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52  fine WHERE_ORDER
29d3e 42 59 20 20 20 20 20 20 30 78 30 31 30 30 30 30  BY      0x010000
29d3f 30 30 20 20 2f 2a 20 4f 75 74 70 75 74 20 77 69  00  /* Output wi
29d40 6c 6c 20 61 70 70 65 61 72 20 69 6e 20 63 6f 72  ll appear in cor
29d41 72 65 63 74 20 6f 72 64 65 72 20 2a 2f 0a 23 64  rect order */.#d
29d42 65 66 69 6e 65 20 57 48 45 52 45 5f 52 45 56 45  efine WHERE_REVE
29d43 52 53 45 20 20 20 20 20 20 30 78 30 32 30 30 30  RSE      0x02000
29d44 30 30 30 20 20 2f 2a 20 53 63 61 6e 20 69 6e 20  000  /* Scan in 
29d45 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f  reverse order */
29d46 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 55  .#define WHERE_U
29d47 4e 49 51 55 45 20 20 20 20 20 20 20 30 78 30 34  NIQUE       0x04
29d48 30 30 30 30 30 30 20 20 2f 2a 20 53 65 6c 65 63  000000  /* Selec
29d49 74 73 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20  ts no more than 
29d4a 6f 6e 65 20 72 6f 77 20 2a 2f 0a 23 64 65 66 69  one row */.#defi
29d4b 6e 65 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c  ne WHERE_VIRTUAL
29d4c 54 41 42 4c 45 20 30 78 30 38 30 30 30 30 30 30  TABLE 0x08000000
29d4d 20 20 2f 2a 20 55 73 65 20 76 69 72 74 75 61 6c    /* Use virtual
29d4e 2d 74 61 62 6c 65 20 70 72 6f 63 65 73 73 69 6e  -table processin
29d4f 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  g */.#define WHE
29d50 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 20 20 20 20  RE_MULTI_OR     
29d51 30 78 31 30 30 30 30 30 30 30 20 20 2f 2a 20 4f  0x10000000  /* O
29d52 52 20 75 73 69 6e 67 20 6d 75 6c 74 69 70 6c 65  R using multiple
29d53 20 69 6e 64 69 63 65 73 20 2a 2f 0a 0a 2f 2a 0a   indices */../*.
29d54 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20  ** Initialize a 
29d55 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 57 68 65  preallocated Whe
29d56 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75  reClause structu
29d57 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  re..*/.static vo
29d58 69 64 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e  id whereClauseIn
29d59 69 74 28 0a 20 20 57 68 65 72 65 43 6c 61 75 73  it(.  WhereClaus
29d5a 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 2f  e *pWC,        /
29d5b 2a 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73  * The WhereClaus
29d5c 65 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69  e to be initiali
29d5d 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  zed */.  Parse *
29d5e 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
29d5f 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
29d60 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
29d61 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
29d62 6b 53 65 74 20 20 20 2f 2a 20 4d 61 70 70 69 6e  kSet   /* Mappin
29d63 67 20 66 72 6f 6d 20 74 61 62 6c 65 20 63 75 72  g from table cur
29d64 73 6f 72 20 6e 75 6d 62 65 72 73 20 74 6f 20 62  sor numbers to b
29d65 69 74 6d 61 73 6b 73 20 2a 2f 0a 29 7b 0a 20 20  itmasks */.){.  
29d66 70 57 43 2d 3e 70 50 61 72 73 65 20 3d 20 70 50  pWC->pParse = pP
29d67 61 72 73 65 3b 0a 20 20 70 57 43 2d 3e 70 4d 61  arse;.  pWC->pMa
29d68 73 6b 53 65 74 20 3d 20 70 4d 61 73 6b 53 65 74  skSet = pMaskSet
29d69 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72 6d 20 3d  ;.  pWC->nTerm =
29d6a 20 30 3b 0a 20 20 70 57 43 2d 3e 6e 53 6c 6f 74   0;.  pWC->nSlot
29d6b 20 3d 20 41 72 72 61 79 53 69 7a 65 28 70 57 43   = ArraySize(pWC
29d6c 2d 3e 61 53 74 61 74 69 63 29 3b 0a 20 20 70 57  ->aStatic);.  pW
29d6d 43 2d 3e 61 20 3d 20 70 57 43 2d 3e 61 53 74 61  C->a = pWC->aSta
29d6e 74 69 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61  tic;.}../* Forwa
29d6f 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a  rd reference */.
29d70 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72  static void wher
29d71 65 43 6c 61 75 73 65 43 6c 65 61 72 28 57 68 65  eClauseClear(Whe
29d72 72 65 43 6c 61 75 73 65 2a 29 3b 0a 0a 2f 2a 0a  reClause*);../*.
29d73 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c  ** Deallocate al
29d74 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  l memory associa
29d75 74 65 64 20 77 69 74 68 20 61 20 57 68 65 72 65  ted with a Where
29d76 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a  OrInfo object..*
29d77 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68  /.static void wh
29d78 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74 65 28  ereOrInfoDelete(
29d79 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65  sqlite3 *db, Whe
29d7a 72 65 4f 72 49 6e 66 6f 20 2a 70 29 7b 0a 20 20  reOrInfo *p){.  
29d7b 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72  whereClauseClear
29d7c 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c 69  (&p->wc);.  sqli
29d7d 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
29d7e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c  ;.}../*.** Deall
29d7f 6f 63 61 74 65 20 61 6c 6c 20 6d 65 6d 6f 72 79  ocate all memory
29d80 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
29d81 20 61 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20   a WhereAndInfo 
29d82 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69  object..*/.stati
29d83 63 20 76 6f 69 64 20 77 68 65 72 65 41 6e 64 49  c void whereAndI
29d84 6e 66 6f 44 65 6c 65 74 65 28 73 71 6c 69 74 65  nfoDelete(sqlite
29d85 33 20 2a 64 62 2c 20 57 68 65 72 65 41 6e 64 49  3 *db, WhereAndI
29d86 6e 66 6f 20 2a 70 29 7b 0a 20 20 77 68 65 72 65  nfo *p){.  where
29d87 43 6c 61 75 73 65 43 6c 65 61 72 28 26 70 2d 3e  ClauseClear(&p->
29d88 77 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  wc);.  sqlite3Db
29d89 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a  Free(db, p);.}..
29d8a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  /*.** Deallocate
29d8b 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20 73   a WhereClause s
29d8c 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 57  tructure.  The W
29d8d 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63  hereClause struc
29d8e 74 75 72 65 0a 2a 2a 20 69 74 73 65 6c 66 20 69  ture.** itself i
29d8f 73 20 6e 6f 74 20 66 72 65 65 64 2e 20 20 54 68  s not freed.  Th
29d90 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
29d91 65 20 69 6e 76 65 72 73 65 20 6f 66 20 77 68 65  e inverse of whe
29d92 72 65 43 6c 61 75 73 65 49 6e 69 74 28 29 2e 0a  reClauseInit()..
29d93 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
29d94 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28  hereClauseClear(
29d95 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
29d96 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 57 68  ){.  int i;.  Wh
29d97 65 72 65 54 65 72 6d 20 2a 61 3b 0a 20 20 73 71  ereTerm *a;.  sq
29d98 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 57 43 2d  lite3 *db = pWC-
29d99 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 66  >pParse->db;.  f
29d9a 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d 2d  or(i=pWC->nTerm-
29d9b 31 2c 20 61 3d 70 57 43 2d 3e 61 3b 20 69 3e 3d  1, a=pWC->a; i>=
29d9c 30 3b 20 69 2d 2d 2c 20 61 2b 2b 29 7b 0a 20 20  0; i--, a++){.  
29d9d 20 20 69 66 28 20 61 2d 3e 77 74 46 6c 61 67 73    if( a->wtFlags
29d9e 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20   & TERM_DYNAMIC 
29d9f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
29da0 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 61  ExprDelete(db, a
29da1 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a  ->pExpr);.    }.
29da2 20 20 20 20 69 66 28 20 61 2d 3e 77 74 46 6c 61      if( a->wtFla
29da3 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e 46 4f  gs & TERM_ORINFO
29da4 20 29 7b 0a 20 20 20 20 20 20 77 68 65 72 65 4f   ){.      whereO
29da5 72 49 6e 66 6f 44 65 6c 65 74 65 28 64 62 2c 20  rInfoDelete(db, 
29da6 61 2d 3e 75 2e 70 4f 72 49 6e 66 6f 29 3b 0a 20  a->u.pOrInfo);. 
29da7 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 2d 3e     }else if( a->
29da8 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 41  wtFlags & TERM_A
29da9 4e 44 49 4e 46 4f 20 29 7b 0a 20 20 20 20 20 20  NDINFO ){.      
29daa 77 68 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65  whereAndInfoDele
29dab 74 65 28 64 62 2c 20 61 2d 3e 75 2e 70 41 6e 64  te(db, a->u.pAnd
29dac 49 6e 66 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Info);.    }.  }
29dad 0a 20 20 69 66 28 20 70 57 43 2d 3e 61 21 3d 70  .  if( pWC->a!=p
29dae 57 43 2d 3e 61 53 74 61 74 69 63 20 29 7b 0a 20  WC->aStatic ){. 
29daf 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
29db0 28 64 62 2c 20 70 57 43 2d 3e 61 29 3b 0a 20 20  (db, pWC->a);.  
29db1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  }.}../*.** Add a
29db2 20 73 69 6e 67 6c 65 20 6e 65 77 20 57 68 65 72   single new Wher
29db3 65 54 65 72 6d 20 65 6e 74 72 79 20 74 6f 20 74  eTerm entry to t
29db4 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f  he WhereClause o
29db5 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a 20 54 68  bject pWC..** Th
29db6 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20  e new WhereTerm 
29db7 6f 62 6a 65 63 74 20 69 73 20 63 6f 6e 73 74 72  object is constr
29db8 75 63 74 65 64 20 66 72 6f 6d 20 45 78 70 72 20  ucted from Expr 
29db9 70 20 61 6e 64 20 77 69 74 68 20 77 74 46 6c 61  p and with wtFla
29dba 67 73 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78  gs..** The index
29dbb 20 69 6e 20 70 57 43 2d 3e 61 5b 5d 20 6f 66 20   in pWC->a[] of 
29dbc 74 68 65 20 6e 65 77 20 57 68 65 72 65 54 65 72  the new WhereTer
29dbd 6d 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  m is returned on
29dbe 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 30 20 69   success..** 0 i
29dbf 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  s returned if th
29dc0 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20  e new WhereTerm 
29dc1 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 64 64  could not be add
29dc2 65 64 20 64 75 65 20 74 6f 20 61 20 6d 65 6d 6f  ed due to a memo
29dc3 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  ry.** allocation
29dc4 20 65 72 72 6f 72 2e 20 20 54 68 65 20 6d 65 6d   error.  The mem
29dc5 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
29dc6 61 69 6c 75 72 65 20 77 69 6c 6c 20 62 65 20 72  ailure will be r
29dc7 65 63 6f 72 64 65 64 20 69 6e 0a 2a 2a 20 74 68  ecorded in.** th
29dc8 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  e db->mallocFail
29dc9 65 64 20 66 6c 61 67 20 73 6f 20 74 68 61 74 20  ed flag so that 
29dca 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 66 75 6e  higher-level fun
29dcb 63 74 69 6f 6e 73 20 63 61 6e 20 64 65 74 65 63  ctions can detec
29dcc 74 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  t it..**.** This
29dcd 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 69 6e   routine will in
29dce 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20  crease the size 
29dcf 6f 66 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20  of the pWC->a[] 
29dd0 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73 61  array as necessa
29dd1 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ry..**.** If the
29dd2 20 77 74 46 6c 61 67 73 20 61 72 67 75 6d 65 6e   wtFlags argumen
29dd3 74 20 69 6e 63 6c 75 64 65 73 20 54 45 52 4d 5f  t includes TERM_
29dd4 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 72 65  DYNAMIC, then re
29dd5 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a 2a 2a 20  sponsibility.** 
29dd6 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20  for freeing the 
29dd7 65 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73 20  expression p is 
29dd8 61 73 73 75 6d 65 64 20 62 79 20 74 68 65 20 57  assumed by the W
29dd9 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65 63  hereClause objec
29dda 74 20 70 57 43 2e 0a 2a 2a 20 54 68 69 73 20 69  t pWC..** This i
29ddb 73 20 74 72 75 65 20 65 76 65 6e 20 69 66 20 74  s true even if t
29ddc 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c  his routine fail
29ddd 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  s to allocate a 
29dde 6e 65 77 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a  new WhereTerm..*
29ddf 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20 54  *.** WARNING:  T
29de0 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68  his routine migh
29de1 74 20 72 65 61 6c 6c 6f 63 61 74 65 20 74 68 65  t reallocate the
29de2 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 73   space used to s
29de3 74 6f 72 65 0a 2a 2a 20 57 68 65 72 65 54 65 72  tore.** WhereTer
29de4 6d 73 2e 20 20 41 6c 6c 20 70 6f 69 6e 74 65 72  ms.  All pointer
29de5 73 20 74 6f 20 57 68 65 72 65 54 65 72 6d 73 20  s to WhereTerms 
29de6 73 68 6f 75 6c 64 20 62 65 20 69 6e 76 61 6c 69  should be invali
29de7 64 61 74 65 64 20 61 66 74 65 72 0a 2a 2a 20 63  dated after.** c
29de8 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
29de9 69 6e 65 2e 20 20 53 75 63 68 20 70 6f 69 6e 74  ine.  Such point
29dea 65 72 73 20 6d 61 79 20 62 65 20 72 65 69 6e 69  ers may be reini
29deb 74 69 61 6c 69 7a 65 64 20 62 79 20 72 65 66 65  tialized by refe
29dec 72 65 6e 63 69 6e 67 0a 2a 2a 20 74 68 65 20 70  rencing.** the p
29ded 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a  WC->a[] array..*
29dee 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 65  /.static int whe
29def 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 57  reClauseInsert(W
29df0 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
29df1 20 45 78 70 72 20 2a 70 2c 20 75 38 20 77 74 46   Expr *p, u8 wtF
29df2 6c 61 67 73 29 7b 0a 20 20 57 68 65 72 65 54 65  lags){.  WhereTe
29df3 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74  rm *pTerm;.  int
29df4 20 69 64 78 3b 0a 20 20 69 66 28 20 70 57 43 2d   idx;.  if( pWC-
29df5 3e 6e 54 65 72 6d 3e 3d 70 57 43 2d 3e 6e 53 6c  >nTerm>=pWC->nSl
29df6 6f 74 20 29 7b 0a 20 20 20 20 57 68 65 72 65 54  ot ){.    WhereT
29df7 65 72 6d 20 2a 70 4f 6c 64 20 3d 20 70 57 43 2d  erm *pOld = pWC-
29df8 3e 61 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  >a;.    sqlite3 
29df9 2a 64 62 20 3d 20 70 57 43 2d 3e 70 50 61 72 73  *db = pWC->pPars
29dfa 65 2d 3e 64 62 3b 0a 20 20 20 20 70 57 43 2d 3e  e->db;.    pWC->
29dfb 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  a = sqlite3DbMal
29dfc 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
29dfd 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43  f(pWC->a[0])*pWC
29dfe 2d 3e 6e 53 6c 6f 74 2a 32 20 29 3b 0a 20 20 20  ->nSlot*2 );.   
29dff 20 69 66 28 20 70 57 43 2d 3e 61 3d 3d 30 20 29   if( pWC->a==0 )
29e00 7b 0a 20 20 20 20 20 20 69 66 28 20 77 74 46 6c  {.      if( wtFl
29e01 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d  ags & TERM_DYNAM
29e02 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  IC ){.        sq
29e03 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
29e04 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a  db, p);.      }.
29e05 20 20 20 20 20 20 70 57 43 2d 3e 61 20 3d 20 70        pWC->a = p
29e06 4f 6c 64 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Old;.      retur
29e07 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  n 0;.    }.    m
29e08 65 6d 63 70 79 28 70 57 43 2d 3e 61 2c 20 70 4f  emcpy(pWC->a, pO
29e09 6c 64 2c 20 73 69 7a 65 6f 66 28 70 57 43 2d 3e  ld, sizeof(pWC->
29e0a 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e 54 65 72 6d  a[0])*pWC->nTerm
29e0b 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 21  );.    if( pOld!
29e0c 3d 70 57 43 2d 3e 61 53 74 61 74 69 63 20 29 7b  =pWC->aStatic ){
29e0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
29e0e 46 72 65 65 28 64 62 2c 20 70 4f 6c 64 29 3b 0a  Free(db, pOld);.
29e0f 20 20 20 20 7d 0a 20 20 20 20 70 57 43 2d 3e 6e      }.    pWC->n
29e10 53 6c 6f 74 20 3d 20 73 71 6c 69 74 65 33 44 62  Slot = sqlite3Db
29e11 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70  MallocSize(db, p
29e12 57 43 2d 3e 61 29 2f 73 69 7a 65 6f 66 28 70 57  WC->a)/sizeof(pW
29e13 43 2d 3e 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20  C->a[0]);.  }.  
29e14 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
29e15 69 64 78 20 3d 20 70 57 43 2d 3e 6e 54 65 72 6d  idx = pWC->nTerm
29e16 2b 2b 5d 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 45  ++];.  pTerm->pE
29e17 78 70 72 20 3d 20 70 3b 0a 20 20 70 54 65 72 6d  xpr = p;.  pTerm
29e18 2d 3e 77 74 46 6c 61 67 73 20 3d 20 77 74 46 6c  ->wtFlags = wtFl
29e19 61 67 73 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 57  ags;.  pTerm->pW
29e1a 43 20 3d 20 70 57 43 3b 0a 20 20 70 54 65 72 6d  C = pWC;.  pTerm
29e1b 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a  ->iParent = -1;.
29e1c 20 20 72 65 74 75 72 6e 20 69 64 78 3b 0a 7d 0a    return idx;.}.
29e1d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
29e1e 69 6e 65 20 69 64 65 6e 74 69 66 69 65 73 20 73  ine identifies s
29e1f 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  ubexpressions in
29e20 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
29e21 65 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20  e where.** each 
29e22 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73  subexpression is
29e23 20 73 65 70 61 72 61 74 65 64 20 62 79 20 74 68   separated by th
29e24 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 20 6f  e AND operator o
29e25 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20  r some other.** 
29e26 6f 70 65 72 61 74 6f 72 20 73 70 65 63 69 66 69  operator specifi
29e27 65 64 20 69 6e 20 74 68 65 20 6f 70 20 70 61 72  ed in the op par
29e28 61 6d 65 74 65 72 2e 20 20 54 68 65 20 57 68 65  ameter.  The Whe
29e29 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75  reClause structu
29e2a 72 65 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20  re.** is filled 
29e2b 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f  with pointers to
29e2c 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 2e   subexpressions.
29e2d 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a    For example:.*
29e2e 2a 0a 2a 2a 20 20 20 20 57 48 45 52 45 20 20 61  *.**    WHERE  a
29e2f 3d 3d 27 68 65 6c 6c 6f 27 20 41 4e 44 20 63 6f  =='hello' AND co
29e30 61 6c 65 73 63 65 28 62 2c 31 31 29 3c 31 30 20  alesce(b,11)<10 
29e31 41 4e 44 20 28 63 2b 31 32 21 3d 64 20 4f 52 20  AND (c+12!=d OR 
29e32 63 3d 3d 32 32 29 0a 2a 2a 20 20 20 20 20 20 20  c==22).**       
29e33 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20      \________/  
29e34 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f     \____________
29e35 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  ___/     \______
29e36 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20  __________/.**  
29e37 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 30            slot[0
29e38 5d 20 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f  ]            slo
29e39 74 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20 20  t[1]            
29e3a 20 20 20 73 6c 6f 74 5b 32 5d 0a 2a 2a 0a 2a 2a     slot[2].**.**
29e3b 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 57 48   The original WH
29e3c 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 70 45  ERE clause in pE
29e3d 78 70 72 20 69 73 20 75 6e 61 6c 74 65 72 65 64  xpr is unaltered
29e3e 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75 74  .  All this rout
29e3f 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 69 73 20 6d  ine.** does is m
29e40 61 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e 74 72 69  ake slot[] entri
29e41 65 73 20 70 6f 69 6e 74 20 74 6f 20 73 75 62 73  es point to subs
29e42 74 72 75 63 74 75 72 65 20 77 69 74 68 69 6e 20  tructure within 
29e43 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  pExpr..**.** In 
29e44 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e  the previous sen
29e45 74 65 6e 63 65 20 61 6e 64 20 69 6e 20 74 68 65  tence and in the
29e46 20 64 69 61 67 72 61 6d 2c 20 22 73 6c 6f 74 5b   diagram, "slot[
29e47 5d 22 20 72 65 66 65 72 73 20 74 6f 0a 2a 2a 20  ]" refers to.** 
29e48 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 2e  the WhereClause.
29e49 61 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 65 20  a[] array.  The 
29e4a 73 6c 6f 74 5b 5d 20 61 72 72 61 79 20 67 72 6f  slot[] array gro
29e4b 77 73 20 61 73 20 6e 65 65 64 65 64 20 74 6f 20  ws as needed to 
29e4c 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 6c 6c 20 74  contain.** all t
29e4d 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52  erms of the WHER
29e4e 45 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 61  E clause..*/.sta
29e4f 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 53 70  tic void whereSp
29e50 6c 69 74 28 57 68 65 72 65 43 6c 61 75 73 65 20  lit(WhereClause 
29e51 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 45 78 70  *pWC, Expr *pExp
29e52 72 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 70 57  r, int op){.  pW
29e53 43 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a  C->op = (u8)op;.
29e54 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29    if( pExpr==0 )
29e55 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
29e56 45 78 70 72 2d 3e 6f 70 21 3d 6f 70 20 29 7b 0a  Expr->op!=op ){.
29e57 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 65 49      whereClauseI
29e58 6e 73 65 72 74 28 70 57 43 2c 20 70 45 78 70 72  nsert(pWC, pExpr
29e59 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  , 0);.  }else{. 
29e5a 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 57     whereSplit(pW
29e5b 43 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  C, pExpr->pLeft,
29e5c 20 6f 70 29 3b 0a 20 20 20 20 77 68 65 72 65 53   op);.    whereS
29e5d 70 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72 2d  plit(pWC, pExpr-
29e5e 3e 70 52 69 67 68 74 2c 20 6f 70 29 3b 0a 20 20  >pRight, op);.  
29e5f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  }.}../*.** Initi
29e60 61 6c 69 7a 65 20 61 6e 20 65 78 70 72 65 73 73  alize an express
29e61 69 6f 6e 20 6d 61 73 6b 20 73 65 74 0a 2a 2f 0a  ion mask set.*/.
29e62 23 64 65 66 69 6e 65 20 69 6e 69 74 4d 61 73 6b  #define initMask
29e63 53 65 74 28 50 29 20 20 6d 65 6d 73 65 74 28 50  Set(P)  memset(P
29e64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 50 29 29  , 0, sizeof(*P))
29e65 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
29e66 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74  he bitmask for t
29e67 68 65 20 67 69 76 65 6e 20 63 75 72 73 6f 72 20  he given cursor 
29e68 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 20  number.  Return 
29e69 30 20 69 66 0a 2a 2a 20 69 43 75 72 73 6f 72 20  0 if.** iCursor 
29e6a 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 73 65  is not in the se
29e6b 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74  t..*/.static Bit
29e6c 6d 61 73 6b 20 67 65 74 4d 61 73 6b 28 57 68 65  mask getMask(Whe
29e6d 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  reMaskSet *pMask
29e6e 53 65 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72  Set, int iCursor
29e6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
29e70 72 28 69 3d 30 3b 20 69 3c 70 4d 61 73 6b 53 65  r(i=0; i<pMaskSe
29e71 74 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  t->n; i++){.    
29e72 69 66 28 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78  if( pMaskSet->ix
29e73 5b 69 5d 3d 3d 69 43 75 72 73 6f 72 20 29 7b 0a  [i]==iCursor ){.
29e74 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 28 42        return ((B
29e75 69 74 6d 61 73 6b 29 31 29 3c 3c 69 3b 0a 20 20  itmask)1)<<i;.  
29e76 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
29e77 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65   0;.}../*.** Cre
29e78 61 74 65 20 61 20 6e 65 77 20 6d 61 73 6b 20 66  ate a new mask f
29e79 6f 72 20 63 75 72 73 6f 72 20 69 43 75 72 73 6f  or cursor iCurso
29e7a 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69  r..**.** There i
29e7b 73 20 6f 6e 65 20 63 75 72 73 6f 72 20 70 65 72  s one cursor per
29e7c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52   table in the FR
29e7d 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20  OM clause.  The 
29e7e 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62  number of.** tab
29e7f 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  les in the FROM 
29e80 63 6c 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65  clause is limite
29e81 64 20 62 79 20 61 20 74 65 73 74 20 65 61 72 6c  d by a test earl
29e82 79 20 69 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69  y in the.** sqli
29e83 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20  te3WhereBegin() 
29e84 72 6f 75 74 69 6e 65 2e 20 20 53 6f 20 77 65 20  routine.  So we 
29e85 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 70 4d  know that the pM
29e86 61 73 6b 53 65 74 2d 3e 69 78 5b 5d 0a 2a 2a 20  askSet->ix[].** 
29e87 61 72 72 61 79 20 77 69 6c 6c 20 6e 65 76 65 72  array will never
29e88 20 6f 76 65 72 66 6c 6f 77 2e 0a 2a 2f 0a 73 74   overflow..*/.st
29e89 61 74 69 63 20 76 6f 69 64 20 63 72 65 61 74 65  atic void create
29e8a 4d 61 73 6b 28 57 68 65 72 65 4d 61 73 6b 53 65  Mask(WhereMaskSe
29e8b 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69 6e 74  t *pMaskSet, int
29e8c 20 69 43 75 72 73 6f 72 29 7b 0a 20 20 61 73 73   iCursor){.  ass
29e8d 65 72 74 28 20 70 4d 61 73 6b 53 65 74 2d 3e 6e  ert( pMaskSet->n
29e8e 20 3c 20 41 72 72 61 79 53 69 7a 65 28 70 4d 61   < ArraySize(pMa
29e8f 73 6b 53 65 74 2d 3e 69 78 29 20 29 3b 0a 20 20  skSet->ix) );.  
29e90 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 70 4d 61  pMaskSet->ix[pMa
29e91 73 6b 53 65 74 2d 3e 6e 2b 2b 5d 20 3d 20 69 43  skSet->n++] = iC
29e92 75 72 73 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ursor;.}../*.** 
29e93 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 6c  This routine wal
29e94 6b 73 20 28 72 65 63 75 72 73 69 76 65 6c 79 29  ks (recursively)
29e95 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
29e96 72 65 65 20 61 6e 64 20 67 65 6e 65 72 61 74 65  ree and generate
29e97 73 0a 2a 2a 20 61 20 62 69 74 6d 61 73 6b 20 69  s.** a bitmask i
29e98 6e 64 69 63 61 74 69 6e 67 20 77 68 69 63 68 20  ndicating which 
29e99 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 20  tables are used 
29e9a 69 6e 20 74 68 61 74 20 65 78 70 72 65 73 73 69  in that expressi
29e9b 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a  on.** tree..**.*
29e9c 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20 74  * In order for t
29e9d 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 77  his routine to w
29e9e 6f 72 6b 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67  ork, the calling
29e9f 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 68   function must h
29ea0 61 76 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c  ave.** previousl
29ea1 79 20 69 6e 76 6f 6b 65 64 20 73 71 6c 69 74 65  y invoked sqlite
29ea2 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
29ea3 73 28 29 20 6f 6e 20 74 68 65 20 65 78 70 72 65  s() on the expre
29ea4 73 73 69 6f 6e 2e 20 20 53 65 65 0a 2a 2a 20 74  ssion.  See.** t
29ea5 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  he header commen
29ea6 74 20 6f 6e 20 74 68 61 74 20 72 6f 75 74 69 6e  t on that routin
29ea7 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  e for additional
29ea8 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
29ea9 20 54 68 65 20 73 71 6c 69 74 65 33 52 65 73 6f   The sqlite3Reso
29eaa 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 72  lveExprNames() r
29eab 6f 75 74 69 6e 65 73 20 6c 6f 6f 6b 73 20 66 6f  outines looks fo
29eac 72 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61  r column names a
29ead 6e 64 0a 2a 2a 20 73 65 74 73 20 74 68 65 69 72  nd.** sets their
29eae 20 6f 70 63 6f 64 65 73 20 74 6f 20 54 4b 5f 43   opcodes to TK_C
29eaf 4f 4c 55 4d 4e 20 61 6e 64 20 74 68 65 69 72 20  OLUMN and their 
29eb0 45 78 70 72 2e 69 54 61 62 6c 65 20 66 69 65 6c  Expr.iTable fiel
29eb1 64 73 20 74 6f 0a 2a 2a 20 74 68 65 20 56 44 42  ds to.** the VDB
29eb2 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
29eb3 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54  of the table.  T
29eb4 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74  his routine just
29eb5 20 68 61 73 20 74 6f 0a 2a 2a 20 74 72 61 6e 73   has to.** trans
29eb6 6c 61 74 65 20 74 68 65 20 63 75 72 73 6f 72 20  late the cursor 
29eb7 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 62 69 74  numbers into bit
29eb8 6d 61 73 6b 20 76 61 6c 75 65 73 20 61 6e 64 20  mask values and 
29eb9 4f 52 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 62 69  OR all.** the bi
29eba 74 6d 61 73 6b 73 20 74 6f 67 65 74 68 65 72 2e  tmasks together.
29ebb 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61  .*/.static Bitma
29ebc 73 6b 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65  sk exprListTable
29ebd 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53  Usage(WhereMaskS
29ebe 65 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  et*, ExprList*);
29ebf 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20  .static Bitmask 
29ec0 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55  exprSelectTableU
29ec1 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65  sage(WhereMaskSe
29ec2 74 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 73 74  t*, Select*);.st
29ec3 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70  atic Bitmask exp
29ec4 72 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72  rTableUsage(Wher
29ec5 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  eMaskSet *pMaskS
29ec6 65 74 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20  et, Expr *p){.  
29ec7 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30  Bitmask mask = 0
29ec8 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
29ec9 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70  eturn 0;.  if( p
29eca 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
29ecb 29 7b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 67 65  ){.    mask = ge
29ecc 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
29ecd 70 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20  p->iTable);.    
29ece 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 20 20 7d  return mask;.  }
29ecf 0a 20 20 6d 61 73 6b 20 3d 20 65 78 70 72 54 61  .  mask = exprTa
29ed0 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
29ed1 74 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20  t, p->pRight);. 
29ed2 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62   mask |= exprTab
29ed3 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
29ed4 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 6d  , p->pLeft);.  m
29ed5 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54  ask |= exprListT
29ed6 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
29ed7 65 74 2c 20 70 2d 3e 70 4c 69 73 74 29 3b 0a 20  et, p->pList);. 
29ed8 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 53 65 6c   mask |= exprSel
29ed9 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  ectTableUsage(pM
29eda 61 73 6b 53 65 74 2c 20 70 2d 3e 70 53 65 6c 65  askSet, p->pSele
29edb 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 61  ct);.  return ma
29edc 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42 69 74  sk;.}.static Bit
29edd 6d 61 73 6b 20 65 78 70 72 4c 69 73 74 54 61 62  mask exprListTab
29ede 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d 61 73  leUsage(WhereMas
29edf 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
29ee0 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
29ee1 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 69 74  {.  int i;.  Bit
29ee2 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20  mask mask = 0;. 
29ee3 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20   if( pList ){.  
29ee4 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
29ee5 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
29ee6 0a 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  .      mask |= e
29ee7 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
29ee8 61 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61  askSet, pList->a
29ee9 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  [i].pExpr);.    
29eea 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d  }.  }.  return m
29eeb 61 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42 69  ask;.}.static Bi
29eec 74 6d 61 73 6b 20 65 78 70 72 53 65 6c 65 63 74  tmask exprSelect
29eed 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65  TableUsage(Where
29eee 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
29eef 74 2c 20 53 65 6c 65 63 74 20 2a 70 53 29 7b 0a  t, Select *pS){.
29ef0 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d    Bitmask mask =
29ef1 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 20   0;.  while( pS 
29ef2 29 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  ){.    mask |= e
29ef3 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
29ef4 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e  e(pMaskSet, pS->
29ef5 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 6d 61 73  pEList);.    mas
29ef6 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
29ef7 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
29ef8 2c 20 70 53 2d 3e 70 47 72 6f 75 70 42 79 29 3b  , pS->pGroupBy);
29ef9 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
29efa 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
29efb 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 4f  pMaskSet, pS->pO
29efc 72 64 65 72 42 79 29 3b 0a 20 20 20 20 6d 61 73  rderBy);.    mas
29efd 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73  k |= exprTableUs
29efe 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53  age(pMaskSet, pS
29eff 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 6d  ->pWhere);.    m
29f00 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65  ask |= exprTable
29f01 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
29f02 70 53 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20  pS->pHaving);.  
29f03 20 20 70 53 20 3d 20 70 53 2d 3e 70 50 72 69 6f    pS = pS->pPrio
29f04 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
29f05 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  mask;.}../*.** R
29f06 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
29f07 65 20 67 69 76 65 6e 20 6f 70 65 72 61 74 6f 72  e given operator
29f08 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 6f   is one of the o
29f09 70 65 72 61 74 6f 72 73 20 74 68 61 74 20 69 73  perators that is
29f0a 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20  .** allowed for 
29f0b 61 6e 20 69 6e 64 65 78 61 62 6c 65 20 57 48 45  an indexable WHE
29f0c 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 20  RE clause term. 
29f0d 20 54 68 65 20 61 6c 6c 6f 77 65 64 20 6f 70 65   The allowed ope
29f0e 72 61 74 6f 72 73 20 61 72 65 0a 2a 2a 20 22 3d  rators are.** "=
29f0f 22 2c 20 22 3c 22 2c 20 22 3e 22 2c 20 22 3c 3d  ", "<", ">", "<=
29f10 22 2c 20 22 3e 3d 22 2c 20 61 6e 64 20 22 49 4e  ", ">=", and "IN
29f11 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  "..*/.static int
29f12 20 61 6c 6c 6f 77 65 64 4f 70 28 69 6e 74 20 6f   allowedOp(int o
29f13 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 54 4b  p){.  assert( TK
29f14 5f 47 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f  _GT>TK_EQ && TK_
29f15 47 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73  GT<TK_GE );.  as
29f16 73 65 72 74 28 20 54 4b 5f 4c 54 3e 54 4b 5f 45  sert( TK_LT>TK_E
29f17 51 20 26 26 20 54 4b 5f 4c 54 3c 54 4b 5f 47 45  Q && TK_LT<TK_GE
29f18 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b   );.  assert( TK
29f19 5f 4c 45 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f  _LE>TK_EQ && TK_
29f1a 4c 45 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73  LE<TK_GE );.  as
29f1b 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f  sert( TK_GE==TK_
29f1c 45 51 2b 34 20 29 3b 0a 20 20 72 65 74 75 72 6e  EQ+4 );.  return
29f1d 20 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 28 6f   op==TK_IN || (o
29f1e 70 3e 3d 54 4b 5f 45 51 20 26 26 20 6f 70 3c 3d  p>=TK_EQ && op<=
29f1f 54 4b 5f 47 45 29 20 7c 7c 20 6f 70 3d 3d 54 4b  TK_GE) || op==TK
29f20 5f 49 53 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a  _ISNULL;.}../*.*
29f21 2a 20 53 77 61 70 20 74 77 6f 20 6f 62 6a 65 63  * Swap two objec
29f22 74 73 20 6f 66 20 74 79 70 65 20 54 59 50 45 2e  ts of type TYPE.
29f23 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 57 41 50  .*/.#define SWAP
29f24 28 54 59 50 45 2c 41 2c 42 29 20 7b 54 59 50 45  (TYPE,A,B) {TYPE
29f25 20 74 3d 41 3b 20 41 3d 42 3b 20 42 3d 74 3b 7d   t=A; A=B; B=t;}
29f26 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 75 74 65 20  ../*.** Commute 
29f27 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65  a comparison ope
29f28 72 61 74 6f 72 2e 20 20 45 78 70 72 65 73 73 69  rator.  Expressi
29f29 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ons of the form 
29f2a 22 58 20 6f 70 20 59 22 0a 2a 2a 20 61 72 65 20  "X op Y".** are 
29f2b 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 22  converted into "
29f2c 59 20 6f 70 20 58 22 2e 0a 2a 2a 0a 2a 2a 20 49  Y op X"..**.** I
29f2d 66 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  f a collation se
29f2e 71 75 65 6e 63 65 20 69 73 20 61 73 73 6f 63 69  quence is associ
29f2f 61 74 65 64 20 77 69 74 68 20 65 69 74 68 65 72  ated with either
29f30 20 74 68 65 20 6c 65 66 74 20 6f 72 20 72 69 67   the left or rig
29f31 68 74 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68  ht.** side of th
29f32 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 69 74  e comparison, it
29f33 20 72 65 6d 61 69 6e 73 20 61 73 73 6f 63 69 61   remains associa
29f34 74 65 64 20 77 69 74 68 20 74 68 65 20 73 61 6d  ted with the sam
29f35 65 20 73 69 64 65 20 61 66 74 65 72 0a 2a 2a 20  e side after.** 
29f36 74 68 65 20 63 6f 6d 6d 75 74 61 74 69 6f 6e 2e  the commutation.
29f37 20 53 6f 20 22 59 20 63 6f 6c 6c 61 74 65 20 4e   So "Y collate N
29f38 4f 43 41 53 45 20 6f 70 20 58 22 20 62 65 63 6f  OCASE op X" beco
29f39 6d 65 73 20 0a 2a 2a 20 22 58 20 63 6f 6c 6c 61  mes .** "X colla
29f3a 74 65 20 4e 4f 43 41 53 45 20 6f 70 20 59 22 2e  te NOCASE op Y".
29f3b 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
29f3c 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   any collation s
29f3d 65 71 75 65 6e 63 65 20 6f 6e 0a 2a 2a 20 74 68  equence on.** th
29f3e 65 20 6c 65 66 74 20 68 61 6e 64 20 73 69 64 65  e left hand side
29f3f 20 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e   of a comparison
29f40 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 63   overrides any c
29f41 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
29f42 65 20 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 74  e .** attached t
29f43 6f 20 74 68 65 20 72 69 67 68 74 2e 20 46 6f 72  o the right. For
29f44 20 74 68 65 20 73 61 6d 65 20 72 65 61 73 6f 6e   the same reason
29f45 20 74 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61   the EP_ExpColla
29f46 74 65 20 66 6c 61 67 0a 2a 2a 20 69 73 20 6e 6f  te flag.** is no
29f47 74 20 63 6f 6d 6d 75 74 65 64 2e 0a 2a 2f 0a 73  t commuted..*/.s
29f48 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 43  tatic void exprC
29f49 6f 6d 6d 75 74 65 28 50 61 72 73 65 20 2a 70 50  ommute(Parse *pP
29f4a 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
29f4b 72 29 7b 0a 20 20 75 31 36 20 65 78 70 52 69 67  r){.  u16 expRig
29f4c 68 74 20 3d 20 28 70 45 78 70 72 2d 3e 70 52 69  ht = (pExpr->pRi
29f4d 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  ght->flags & EP_
29f4e 45 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 75  ExpCollate);.  u
29f4f 31 36 20 65 78 70 4c 65 66 74 20 3d 20 28 70 45  16 expLeft = (pE
29f50 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67  xpr->pLeft->flag
29f51 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
29f52 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 6c  e);.  assert( al
29f53 6c 6f 77 65 64 4f 70 28 70 45 78 70 72 2d 3e 6f  lowedOp(pExpr->o
29f54 70 29 20 26 26 20 70 45 78 70 72 2d 3e 6f 70 21  p) && pExpr->op!
29f55 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 70 45 78 70  =TK_IN );.  pExp
29f56 72 2d 3e 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c  r->pRight->pColl
29f57 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
29f58 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
29f59 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20  xpr->pRight);.  
29f5a 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 70 43  pExpr->pLeft->pC
29f5b 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
29f5c 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
29f5d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a   pExpr->pLeft);.
29f5e 20 20 53 57 41 50 28 43 6f 6c 6c 53 65 71 2a 2c    SWAP(CollSeq*,
29f5f 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 70  pExpr->pRight->p
29f60 43 6f 6c 6c 2c 70 45 78 70 72 2d 3e 70 4c 65 66  Coll,pExpr->pLef
29f61 74 2d 3e 70 43 6f 6c 6c 29 3b 0a 20 20 70 45 78  t->pColl);.  pEx
29f62 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67  pr->pRight->flag
29f63 73 20 3d 20 28 70 45 78 70 72 2d 3e 70 52 69 67  s = (pExpr->pRig
29f64 68 74 2d 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f  ht->flags & ~EP_
29f65 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78  ExpCollate) | ex
29f66 70 4c 65 66 74 3b 0a 20 20 70 45 78 70 72 2d 3e  pLeft;.  pExpr->
29f67 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 3d 20 28  pLeft->flags = (
29f68 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c  pExpr->pLeft->fl
29f69 61 67 73 20 26 20 7e 45 50 5f 45 78 70 43 6f 6c  ags & ~EP_ExpCol
29f6a 6c 61 74 65 29 20 7c 20 65 78 70 52 69 67 68 74  late) | expRight
29f6b 3b 0a 20 20 53 57 41 50 28 45 78 70 72 2a 2c 70  ;.  SWAP(Expr*,p
29f6c 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 70 45 78  Expr->pRight,pEx
29f6d 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66  pr->pLeft);.  if
29f6e 28 20 70 45 78 70 72 2d 3e 6f 70 3e 3d 54 4b 5f  ( pExpr->op>=TK_
29f6f 47 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  GT ){.    assert
29f70 28 20 54 4b 5f 4c 54 3d 3d 54 4b 5f 47 54 2b 32  ( TK_LT==TK_GT+2
29f71 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
29f72 54 4b 5f 47 45 3d 3d 54 4b 5f 4c 45 2b 32 20 29  TK_GE==TK_LE+2 )
29f73 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b  ;.    assert( TK
29f74 5f 47 54 3e 54 4b 5f 45 51 20 29 3b 0a 20 20 20  _GT>TK_EQ );.   
29f75 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3c 54   assert( TK_GT<T
29f76 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 61 73 73 65  K_LE );.    asse
29f77 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3e 3d 54  rt( pExpr->op>=T
29f78 4b 5f 47 54 20 26 26 20 70 45 78 70 72 2d 3e 6f  K_GT && pExpr->o
29f79 70 3c 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20  p<=TK_GE );.    
29f7a 70 45 78 70 72 2d 3e 6f 70 20 3d 20 28 28 70 45  pExpr->op = ((pE
29f7b 78 70 72 2d 3e 6f 70 2d 54 4b 5f 47 54 29 5e 32  xpr->op-TK_GT)^2
29f7c 29 2b 54 4b 5f 47 54 3b 0a 20 20 7d 0a 7d 0a 0a  )+TK_GT;.  }.}..
29f7d 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20  /*.** Translate 
29f7e 66 72 6f 6d 20 54 4b 5f 78 78 20 6f 70 65 72 61  from TK_xx opera
29f7f 74 6f 72 20 74 6f 20 57 4f 5f 78 78 20 62 69 74  tor to WO_xx bit
29f80 6d 61 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mask..*/.static 
29f81 75 31 36 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b  u16 operatorMask
29f82 28 69 6e 74 20 6f 70 29 7b 0a 20 20 75 31 36 20  (int op){.  u16 
29f83 63 3b 0a 20 20 61 73 73 65 72 74 28 20 61 6c 6c  c;.  assert( all
29f84 6f 77 65 64 4f 70 28 6f 70 29 20 29 3b 0a 20 20  owedOp(op) );.  
29f85 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b  if( op==TK_IN ){
29f86 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49 4e 3b 0a  .    c = WO_IN;.
29f87 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d    }else if( op==
29f88 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20  TK_ISNULL ){.   
29f89 20 63 20 3d 20 57 4f 5f 49 53 4e 55 4c 4c 3b 0a   c = WO_ISNULL;.
29f8a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
29f8b 65 72 74 28 20 28 57 4f 5f 45 51 3c 3c 28 6f 70  ert( (WO_EQ<<(op
29f8c 2d 54 4b 5f 45 51 29 29 20 3c 20 30 78 37 66 66  -TK_EQ)) < 0x7ff
29f8d 66 20 29 3b 0a 20 20 20 20 63 20 3d 20 28 75 31  f );.    c = (u1
29f8e 36 29 28 57 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b  6)(WO_EQ<<(op-TK
29f8f 5f 45 51 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73  _EQ));.  }.  ass
29f90 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49 53 4e 55  ert( op!=TK_ISNU
29f91 4c 4c 20 7c 7c 20 63 3d 3d 57 4f 5f 49 53 4e 55  LL || c==WO_ISNU
29f92 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  LL );.  assert( 
29f93 6f 70 21 3d 54 4b 5f 49 4e 20 7c 7c 20 63 3d 3d  op!=TK_IN || c==
29f94 57 4f 5f 49 4e 20 29 3b 0a 20 20 61 73 73 65 72  WO_IN );.  asser
29f95 74 28 20 6f 70 21 3d 54 4b 5f 45 51 20 7c 7c 20  t( op!=TK_EQ || 
29f96 63 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 61 73  c==WO_EQ );.  as
29f97 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 4c 54 20  sert( op!=TK_LT 
29f98 7c 7c 20 63 3d 3d 57 4f 5f 4c 54 20 29 3b 0a 20  || c==WO_LT );. 
29f99 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
29f9a 4c 45 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 45 20 29  LE || c==WO_LE )
29f9b 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
29f9c 54 4b 5f 47 54 20 7c 7c 20 63 3d 3d 57 4f 5f 47  TK_GT || c==WO_G
29f9d 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
29f9e 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 63 3d 3d 57  p!=TK_GE || c==W
29f9f 4f 5f 47 45 20 29 3b 0a 20 20 72 65 74 75 72 6e  O_GE );.  return
29fa0 20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61   c;.}../*.** Sea
29fa1 72 63 68 20 66 6f 72 20 61 20 74 65 72 6d 20 69  rch for a term i
29fa2 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
29fa3 73 65 20 74 68 61 74 20 69 73 20 6f 66 20 74 68  se that is of th
29fa4 65 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 3c  e form "X <op> <
29fa5 65 78 70 72 3e 22 0a 2a 2a 20 77 68 65 72 65 20  expr>".** where 
29fa6 58 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65  X is a reference
29fa7 20 74 6f 20 74 68 65 20 69 43 6f 6c 75 6d 6e 20   to the iColumn 
29fa8 6f 66 20 74 61 62 6c 65 20 69 43 75 72 20 61 6e  of table iCur an
29fa9 64 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f 66  d <op> is one of
29faa 0a 2a 2a 20 74 68 65 20 57 4f 5f 78 78 20 6f 70  .** the WO_xx op
29fab 65 72 61 74 6f 72 20 63 6f 64 65 73 20 73 70 65  erator codes spe
29fac 63 69 66 69 65 64 20 62 79 20 74 68 65 20 6f 70  cified by the op
29fad 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 52   parameter..** R
29fae 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
29faf 74 6f 20 74 68 65 20 74 65 72 6d 2e 20 20 52 65  to the term.  Re
29fb0 74 75 72 6e 20 30 20 69 66 20 6e 6f 74 20 66 6f  turn 0 if not fo
29fb1 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57  und..*/.static W
29fb2 68 65 72 65 54 65 72 6d 20 2a 66 69 6e 64 54 65  hereTerm *findTe
29fb3 72 6d 28 0a 20 20 57 68 65 72 65 43 6c 61 75 73  rm(.  WhereClaus
29fb4 65 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a 20 54  e *pWC,     /* T
29fb5 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
29fb6 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 2a  to be searched *
29fb7 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  /.  int iCur,   
29fb8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
29fb9 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 4c 48  sor number of LH
29fba 53 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75  S */.  int iColu
29fbb 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  mn,          /* 
29fbc 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66  Column number of
29fbd 20 4c 48 53 20 2a 2f 0a 20 20 42 69 74 6d 61 73   LHS */.  Bitmas
29fbe 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20  k notReady,     
29fbf 2f 2a 20 52 48 53 20 6d 75 73 74 20 6e 6f 74 20  /* RHS must not 
29fc0 6f 76 65 72 6c 61 70 20 77 69 74 68 20 74 68 69  overlap with thi
29fc1 73 20 6d 61 73 6b 20 2a 2f 0a 20 20 75 33 32 20  s mask */.  u32 
29fc2 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
29fc3 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 57 4f 5f    /* Mask of WO_
29fc4 78 78 20 76 61 6c 75 65 73 20 64 65 73 63 72 69  xx values descri
29fc5 62 69 6e 67 20 6f 70 65 72 61 74 6f 72 20 2a 2f  bing operator */
29fc6 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 20  .  Index *pIdx  
29fc7 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 73 74           /* Must
29fc8 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 77   be compatible w
29fc9 69 74 68 20 74 68 69 73 20 69 6e 64 65 78 2c 20  ith this index, 
29fca 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 29  if not NULL */.)
29fcb 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  {.  WhereTerm *p
29fcc 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6b 3b 0a 20  Term;.  int k;. 
29fcd 20 61 73 73 65 72 74 28 20 69 43 75 72 3e 3d 30   assert( iCur>=0
29fce 20 29 3b 0a 20 20 6f 70 20 26 3d 20 57 4f 5f 41   );.  op &= WO_A
29fcf 4c 4c 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d  LL;.  for(pTerm=
29fd0 70 57 43 2d 3e 61 2c 20 6b 3d 70 57 43 2d 3e 6e  pWC->a, k=pWC->n
29fd1 54 65 72 6d 3b 20 6b 3b 20 6b 2d 2d 2c 20 70 54  Term; k; k--, pT
29fd2 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  erm++){.    if( 
29fd3 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
29fd4 72 3d 3d 69 43 75 72 0a 20 20 20 20 20 20 20 26  r==iCur.       &
29fd5 26 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  & (pTerm->prereq
29fd6 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61 64 79  Right & notReady
29fd7 29 3d 3d 30 0a 20 20 20 20 20 20 20 26 26 20 70  )==0.       && p
29fd8 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75  Term->u.leftColu
29fd9 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20  mn==iColumn.    
29fda 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 65 4f     && (pTerm->eO
29fdb 70 65 72 61 74 6f 72 20 26 20 6f 70 29 21 3d 30  perator & op)!=0
29fdc 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66  .    ){.      if
29fdd 28 20 70 49 64 78 20 26 26 20 70 54 65 72 6d 2d  ( pIdx && pTerm-
29fde 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 49  >eOperator!=WO_I
29fdf 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20  SNULL ){.       
29fe0 20 45 78 70 72 20 2a 70 58 20 3d 20 70 54 65 72   Expr *pX = pTer
29fe1 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m->pExpr;.      
29fe2 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
29fe3 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 69  ;.        char i
29fe4 64 78 61 66 66 3b 0a 20 20 20 20 20 20 20 20 69  dxaff;.        i
29fe5 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 50 61  nt j;.        Pa
29fe6 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57  rse *pParse = pW
29fe7 43 2d 3e 70 50 61 72 73 65 3b 0a 0a 20 20 20 20  C->pParse;..    
29fe8 20 20 20 20 69 64 78 61 66 66 20 3d 20 70 49 64      idxaff = pId
29fe9 78 2d 3e 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b  x->pTable->aCol[
29fea 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74  iColumn].affinit
29feb 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  y;.        if( !
29fec 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69  sqlite3IndexAffi
29fed 6e 69 74 79 4f 6b 28 70 58 2c 20 69 64 78 61 66  nityOk(pX, idxaf
29fee 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  f) ) continue;..
29fef 20 20 20 20 20 20 20 20 2f 2a 20 46 69 67 75 72          /* Figur
29ff0 65 20 6f 75 74 20 74 68 65 20 63 6f 6c 6c 61 74  e out the collat
29ff1 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 72 65 71  ion sequence req
29ff2 75 69 72 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e  uired from an in
29ff3 64 65 78 20 66 6f 72 0a 20 20 20 20 20 20 20 20  dex for.        
29ff4 2a 2a 20 69 74 20 74 6f 20 62 65 20 75 73 65 66  ** it to be usef
29ff5 75 6c 20 66 6f 72 20 6f 70 74 69 6d 69 73 69 6e  ul for optimisin
29ff6 67 20 65 78 70 72 65 73 73 69 6f 6e 20 70 58 2e  g expression pX.
29ff7 20 53 74 6f 72 65 20 74 68 69 73 0a 20 20 20 20   Store this.    
29ff8 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 69 6e 20      ** value in 
29ff9 76 61 72 69 61 62 6c 65 20 70 43 6f 6c 6c 2e 0a  variable pColl..
29ffa 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
29ffb 20 20 20 61 73 73 65 72 74 28 70 58 2d 3e 70 4c     assert(pX->pL
29ffc 65 66 74 29 3b 0a 20 20 20 20 20 20 20 20 70 43  eft);.        pC
29ffd 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 6e  oll = sqlite3Bin
29ffe 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
29fff 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c  q(pParse, pX->pL
2a000 65 66 74 2c 20 70 58 2d 3e 70 52 69 67 68 74 29  eft, pX->pRight)
2a001 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
2a002 28 70 43 6f 6c 6c 20 7c 7c 20 70 50 61 72 73 65  (pColl || pParse
2a003 2d 3e 6e 45 72 72 29 3b 0a 0a 20 20 20 20 20 20  ->nErr);..      
2a004 20 20 66 6f 72 28 6a 3d 30 3b 20 70 49 64 78 2d    for(j=0; pIdx-
2a005 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 21 3d 69 43  >aiColumn[j]!=iC
2a006 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  olumn; j++){.   
2a007 20 20 20 20 20 20 20 69 66 28 20 4e 45 56 45 52         if( NEVER
2a008 28 6a 3e 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  (j>=pIdx->nColum
2a009 6e 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  n) ) return 0;. 
2a00a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2a00b 20 69 66 28 20 70 43 6f 6c 6c 20 26 26 20 73 71   if( pColl && sq
2a00c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
2a00d 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 70 49 64 78 2d  ll->zName, pIdx-
2a00e 3e 61 7a 43 6f 6c 6c 5b 6a 5d 29 20 29 20 63 6f  >azColl[j]) ) co
2a00f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a  ntinue;.      }.
2a010 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 54 65        return pTe
2a011 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rm;.    }.  }.  
2a012 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20  return 0;.}../* 
2a013 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
2a014 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  e */.static void
2a015 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 53 72 63   exprAnalyze(Src
2a016 4c 69 73 74 2a 2c 20 57 68 65 72 65 43 6c 61 75  List*, WhereClau
2a017 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  se*, int);../*.*
2a018 2a 20 43 61 6c 6c 20 65 78 70 72 41 6e 61 6c 79  * Call exprAnaly
2a019 7a 65 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20  ze on all terms 
2a01a 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  in a WHERE claus
2a01b 65 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2f 0a 73 74  e.  .**.**.*/.st
2a01c 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e  atic void exprAn
2a01d 61 6c 79 7a 65 41 6c 6c 28 0a 20 20 53 72 63 4c  alyzeAll(.  SrcL
2a01e 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20  ist *pTabList,  
2a01f 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d       /* the FROM
2a020 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
2a021 72 65 43 6c 61 75 73 65 20 2a 70 57 43 20 20 20  reClause *pWC   
2a022 20 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45        /* the WHE
2a023 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20  RE clause to be 
2a024 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20  analyzed */.){. 
2a025 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
2a026 70 57 43 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e  pWC->nTerm-1; i>
2a027 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 65 78  =0; i--){.    ex
2a028 70 72 41 6e 61 6c 79 7a 65 28 70 54 61 62 4c 69  prAnalyze(pTabLi
2a029 73 74 2c 20 70 57 43 2c 20 69 29 3b 0a 20 20 7d  st, pWC, i);.  }
2a02a 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
2a02b 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54  TE_OMIT_LIKE_OPT
2a02c 49 4d 49 5a 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20  IMIZATION./*.** 
2a02d 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
2a02e 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73  the given expres
2a02f 73 69 6f 6e 20 69 73 20 61 20 4c 49 4b 45 20 6f  sion is a LIKE o
2a030 72 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20  r GLOB operator 
2a031 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 6f  that.** can be o
2a032 70 74 69 6d 69 7a 65 64 20 75 73 69 6e 67 20 69  ptimized using i
2a033 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
2a034 61 69 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 54  aints.  Return T
2a035 52 55 45 20 69 66 20 69 74 20 69 73 0a 2a 2a 20  RUE if it is.** 
2a036 73 6f 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20  so and false if 
2a037 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72  not..**.** In or
2a038 64 65 72 20 66 6f 72 20 74 68 65 20 6f 70 65 72  der for the oper
2a039 61 74 6f 72 20 74 6f 20 62 65 20 6f 70 74 69 6d  ator to be optim
2a03a 69 7a 69 62 6c 65 2c 20 74 68 65 20 52 48 53 20  izible, the RHS 
2a03b 6d 75 73 74 20 62 65 20 61 20 73 74 72 69 6e 67  must be a string
2a03c 0a 2a 2a 20 6c 69 74 65 72 61 6c 20 74 68 61 74  .** literal that
2a03d 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20   does not begin 
2a03e 77 69 74 68 20 61 20 77 69 6c 64 63 61 72 64 2e  with a wildcard.
2a03f 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74    .*/.static int
2a040 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 0a 20   isLikeOrGlob(. 
2a041 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2a042 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
2a043 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
2a044 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
2a045 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20  xpr *pExpr,     
2a046 20 2f 2a 20 54 65 73 74 20 74 68 69 73 20 65 78   /* Test this ex
2a047 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e  pression */.  in
2a048 74 20 2a 70 6e 50 61 74 74 65 72 6e 2c 20 20 20  t *pnPattern,   
2a049 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e  /* Number of non
2a04a 2d 77 69 6c 64 63 61 72 64 20 70 72 65 66 69 78  -wildcard prefix
2a04b 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20   characters */. 
2a04c 20 69 6e 74 20 2a 70 69 73 43 6f 6d 70 6c 65 74   int *pisComplet
2a04d 65 2c 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68  e, /* True if th
2a04e 65 20 6f 6e 6c 79 20 77 69 6c 64 63 61 72 64 20  e only wildcard 
2a04f 69 73 20 25 20 69 6e 20 74 68 65 20 6c 61 73 74  is % in the last
2a050 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20   character */.  
2a051 69 6e 74 20 2a 70 6e 6f 43 61 73 65 20 20 20 20  int *pnoCase    
2a052 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 70 70    /* True if upp
2a053 65 72 63 61 73 65 20 69 73 20 65 71 75 69 76 61  ercase is equiva
2a054 6c 65 6e 74 20 74 6f 20 6c 6f 77 65 72 63 61 73  lent to lowercas
2a055 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  e */.){.  const 
2a056 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
2a057 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f       /* String o
2a058 6e 20 52 48 53 20 6f 66 20 4c 49 4b 45 20 6f 70  n RHS of LIKE op
2a059 65 72 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70 72  erator */.  Expr
2a05a 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74   *pRight, *pLeft
2a05b 3b 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20  ;      /* Right 
2a05c 61 6e 64 20 6c 65 66 74 20 73 69 7a 65 20 6f 66  and left size of
2a05d 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a   LIKE operator *
2a05e 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  /.  ExprList *pL
2a05f 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ist;           /
2a060 2a 20 4c 69 73 74 20 6f 66 20 6f 70 65 72 61 6e  * List of operan
2a061 64 73 20 74 6f 20 74 68 65 20 4c 49 4b 45 20 6f  ds to the LIKE o
2a062 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  perator */.  int
2a063 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   c;             
2a064 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63          /* One c
2a065 68 61 72 61 63 74 65 72 20 69 6e 20 7a 5b 5d 20  haracter in z[] 
2a066 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  */.  int cnt;   
2a067 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a068 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e  /* Number of non
2a069 2d 77 69 6c 64 63 61 72 64 20 70 72 65 66 69 78  -wildcard prefix
2a06a 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20   characters */. 
2a06b 20 63 68 61 72 20 77 63 5b 33 5d 3b 20 20 20 20   char wc[3];    
2a06c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
2a06d 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65  ildcard characte
2a06e 72 73 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20  rs */.  CollSeq 
2a06f 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  *pColl;         
2a070 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20     /* Collating 
2a071 73 65 71 75 65 6e 63 65 20 66 6f 72 20 4c 48 53  sequence for LHS
2a072 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2a073 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2a074 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
2a075 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66  nection */..  if
2a076 28 20 21 73 71 6c 69 74 65 33 49 73 4c 69 6b 65  ( !sqlite3IsLike
2a077 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45 78  Function(db, pEx
2a078 70 72 2c 20 70 6e 6f 43 61 73 65 2c 20 77 63 29  pr, pnoCase, wc)
2a079 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2a07a 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c  ;.  }.#ifdef SQL
2a07b 49 54 45 5f 45 42 43 44 49 43 0a 20 20 69 66 28  ITE_EBCDIC.  if(
2a07c 20 2a 70 6e 6f 43 61 73 65 20 29 20 72 65 74 75   *pnoCase ) retu
2a07d 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70  rn 0;.#endif.  p
2a07e 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  List = pExpr->pL
2a07f 69 73 74 3b 0a 20 20 70 52 69 67 68 74 20 3d 20  ist;.  pRight = 
2a080 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  pList->a[0].pExp
2a081 72 3b 0a 20 20 69 66 28 20 70 52 69 67 68 74 2d  r;.  if( pRight-
2a082 3e 6f 70 21 3d 54 4b 5f 53 54 52 49 4e 47 20 29  >op!=TK_STRING )
2a083 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2a084 20 20 7d 0a 20 20 70 4c 65 66 74 20 3d 20 70 4c    }.  pLeft = pL
2a085 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b  ist->a[1].pExpr;
2a086 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70  .  if( pLeft->op
2a087 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20  !=TK_COLUMN ){. 
2a088 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2a089 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  pColl = sqlit
2a08a 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50  e3ExprCollSeq(pP
2a08b 61 72 73 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20  arse, pLeft);.  
2a08c 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 21 3d 30  assert( pColl!=0
2a08d 20 7c 7c 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75   || pLeft->iColu
2a08e 6d 6e 3d 3d 2d 31 20 29 3b 0a 20 20 69 66 28 20  mn==-1 );.  if( 
2a08f 70 43 6f 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  pColl==0 ){.    
2a090 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20  /* No collation 
2a091 69 73 20 64 65 66 69 6e 65 64 20 66 6f 72 20 74  is defined for t
2a092 68 65 20 52 4f 57 49 44 2e 20 20 55 73 65 20 74  he ROWID.  Use t
2a093 68 65 20 64 65 66 61 75 6c 74 2e 20 2a 2f 0a 20  he default. */. 
2a094 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70     pColl = db->p
2a095 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20  DfltColl;.  }.  
2a096 69 66 28 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65  if( (pColl->type
2a097 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49  !=SQLITE_COLL_BI
2a098 4e 41 52 59 20 7c 7c 20 2a 70 6e 6f 43 61 73 65  NARY || *pnoCase
2a099 29 20 26 26 0a 20 20 20 20 20 20 28 70 43 6f 6c  ) &&.      (pCol
2a09a 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54 45 5f  l->type!=SQLITE_
2a09b 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 7c 7c 20 21  COLL_NOCASE || !
2a09c 2a 70 6e 6f 43 61 73 65 29 20 29 7b 0a 20 20 20  *pnoCase) ){.   
2a09d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2a09e 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 45   sqlite3DequoteE
2a09f 78 70 72 28 64 62 2c 20 70 52 69 67 68 74 29 3b  xpr(db, pRight);
2a0a0 0a 20 20 7a 20 3d 20 28 63 68 61 72 20 2a 29 70  .  z = (char *)p
2a0a1 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a  Right->token.z;.
2a0a2 20 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28    cnt = 0;.  if(
2a0a3 20 7a 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28   z ){.    while(
2a0a4 20 28 63 3d 7a 5b 63 6e 74 5d 29 21 3d 30 20 26   (c=z[cnt])!=0 &
2a0a5 26 20 63 21 3d 77 63 5b 30 5d 20 26 26 20 63 21  & c!=wc[0] && c!
2a0a6 3d 77 63 5b 31 5d 20 26 26 20 63 21 3d 77 63 5b  =wc[1] && c!=wc[
2a0a7 32 5d 20 29 7b 20 63 6e 74 2b 2b 3b 20 7d 0a 20  2] ){ cnt++; }. 
2a0a8 20 7d 0a 20 20 69 66 28 20 63 6e 74 3d 3d 30 20   }.  if( cnt==0 
2a0a9 7c 7c 20 32 35 35 3d 3d 28 75 38 29 7a 5b 63 6e  || 255==(u8)z[cn
2a0aa 74 2d 31 5d 20 29 7b 0a 20 20 20 20 72 65 74 75  t-1] ){.    retu
2a0ab 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 69 73  rn 0;.  }.  *pis
2a0ac 43 6f 6d 70 6c 65 74 65 20 3d 20 7a 5b 63 6e 74  Complete = z[cnt
2a0ad 5d 3d 3d 77 63 5b 30 5d 20 26 26 20 7a 5b 63 6e  ]==wc[0] && z[cn
2a0ae 74 2b 31 5d 3d 3d 30 3b 0a 20 20 2a 70 6e 50 61  t+1]==0;.  *pnPa
2a0af 74 74 65 72 6e 20 3d 20 63 6e 74 3b 0a 20 20 72  ttern = cnt;.  r
2a0b0 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69  eturn 1;.}.#endi
2a0b1 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2a0b2 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49  _LIKE_OPTIMIZATI
2a0b3 4f 4e 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20  ON */...#ifndef 
2a0b4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2a0b5 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43  UALTABLE./*.** C
2a0b6 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
2a0b7 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
2a0b8 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ion is of the fo
2a0b9 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  rm.**.**        
2a0ba 20 63 6f 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78   column MATCH ex
2a0bb 70 72 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69  pr.**.** If it i
2a0bc 73 20 74 68 65 6e 20 72 65 74 75 72 6e 20 54 52  s then return TR
2a0bd 55 45 2e 20 20 49 66 20 6e 6f 74 2c 20 72 65 74  UE.  If not, ret
2a0be 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74  urn FALSE..*/.st
2a0bf 61 74 69 63 20 69 6e 74 20 69 73 4d 61 74 63 68  atic int isMatch
2a0c0 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20 45 78 70 72  OfColumn(.  Expr
2a0c1 20 2a 70 45 78 70 72 20 20 20 20 20 20 2f 2a 20   *pExpr      /* 
2a0c2 54 65 73 74 20 74 68 69 73 20 65 78 70 72 65 73  Test this expres
2a0c3 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  sion */.){.  Exp
2a0c4 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 0a 20  rList *pList;.. 
2a0c5 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
2a0c6 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 7b 0a 20  TK_FUNCTION ){. 
2a0c7 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2a0c8 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f  .  if( pExpr->to
2a0c9 6b 65 6e 2e 6e 21 3d 35 20 7c 7c 0a 20 20 20 20  ken.n!=5 ||.    
2a0ca 20 20 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43     sqlite3StrNIC
2a0cb 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  mp((const char*)
2a0cc 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22  pExpr->token.z,"
2a0cd 6d 61 74 63 68 22 2c 35 29 21 3d 30 20 29 7b 0a  match",5)!=0 ){.
2a0ce 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2a0cf 7d 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78 70  }.  pList = pExp
2a0d0 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20  r->pList;.  if( 
2a0d1 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 20  pList->nExpr!=2 
2a0d2 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2a0d3 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74  .  }.  if( pList
2a0d4 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70  ->a[1].pExpr->op
2a0d5 20 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b   != TK_COLUMN ){
2a0d6 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2a0d7 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d   }.  return 1;.}
2a0d8 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2a0d9 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2a0da 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66  BLE */../*.** If
2a0db 20 74 68 65 20 70 42 61 73 65 20 65 78 70 72 65   the pBase expre
2a0dc 73 73 69 6f 6e 20 6f 72 69 67 69 6e 61 74 65 64  ssion originated
2a0dd 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53   in the ON or US
2a0de 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a  ING clause of.**
2a0df 20 61 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 72   a join, then tr
2a0e0 61 6e 73 66 65 72 20 74 68 65 20 61 70 70 72 6f  ansfer the appro
2a0e1 70 72 69 61 74 65 20 6d 61 72 6b 69 6e 67 73 20  priate markings 
2a0e2 6f 76 65 72 20 74 6f 20 64 65 72 69 76 65 64 2e  over to derived.
2a0e3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2a0e4 74 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b  transferJoinMark
2a0e5 69 6e 67 73 28 45 78 70 72 20 2a 70 44 65 72 69  ings(Expr *pDeri
2a0e6 76 65 64 2c 20 45 78 70 72 20 2a 70 42 61 73 65  ved, Expr *pBase
2a0e7 29 7b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 66  ){.  pDerived->f
2a0e8 6c 61 67 73 20 7c 3d 20 70 42 61 73 65 2d 3e 66  lags |= pBase->f
2a0e9 6c 61 67 73 20 26 20 45 50 5f 46 72 6f 6d 4a 6f  lags & EP_FromJo
2a0ea 69 6e 3b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e  in;.  pDerived->
2a0eb 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20  iRightJoinTable 
2a0ec 3d 20 70 42 61 73 65 2d 3e 69 52 69 67 68 74 4a  = pBase->iRightJ
2a0ed 6f 69 6e 54 61 62 6c 65 3b 0a 7d 0a 0a 23 69 66  oinTable;.}..#if
2a0ee 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2a0ef 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
2a0f0 41 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69 6e  ATION) && !defin
2a0f1 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
2a0f2 55 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a 20 41  UBQUERY)./*.** A
2a0f3 6e 61 6c 79 7a 65 20 61 20 74 65 72 6d 20 74 68  nalyze a term th
2a0f4 61 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74  at consists of t
2a0f5 77 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d 63 6f  wo or more OR-co
2a0f6 6e 6e 65 63 74 65 64 0a 2a 2a 20 73 75 62 74 65  nnected.** subte
2a0f7 72 6d 73 2e 20 20 53 6f 20 69 6e 3a 0a 2a 2a 0a  rms.  So in:.**.
2a0f8 2a 2a 20 20 20 20 20 2e 2e 2e 20 57 48 45 52 45  **     ... WHERE
2a0f9 20 20 28 61 3d 35 29 20 41 4e 44 20 28 62 3d 37    (a=5) AND (b=7
2a0fa 20 4f 52 20 63 3d 39 20 4f 52 20 64 3d 31 33 29   OR c=9 OR d=13)
2a0fb 20 41 4e 44 20 28 64 3d 31 33 29 0a 2a 2a 20 20   AND (d=13).**  
2a0fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a0fd 20 20 20 20 20 20 20 20 5e 5e 5e 5e 5e 5e 5e 5e          ^^^^^^^^
2a0fe 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 0a 2a 2a 0a  ^^^^^^^^^^^^.**.
2a0ff 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2a100 61 6e 61 6c 79 7a 65 73 20 74 65 72 6d 73 20 73  analyzes terms s
2a101 75 63 68 20 61 73 20 74 68 65 20 6d 69 64 64 6c  uch as the middl
2a102 65 20 74 65 72 6d 20 69 6e 20 74 68 65 20 61 62  e term in the ab
2a103 6f 76 65 20 65 78 61 6d 70 6c 65 2e 0a 2a 2a 20  ove example..** 
2a104 41 20 57 68 65 72 65 4f 72 54 65 72 6d 20 6f 62  A WhereOrTerm ob
2a105 6a 65 63 74 20 69 73 20 63 6f 6d 70 75 74 65 64  ject is computed
2a106 20 61 6e 64 20 61 74 74 61 63 68 65 64 20 74 6f   and attached to
2a107 20 74 68 65 20 74 65 72 6d 20 75 6e 64 65 72 0a   the term under.
2a108 2a 2a 20 61 6e 61 6c 79 73 69 73 2c 20 72 65 67  ** analysis, reg
2a109 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6f  ardless of the o
2a10a 75 74 63 6f 6d 65 20 6f 66 20 74 68 65 20 61 6e  utcome of the an
2a10b 61 6c 79 73 69 73 2e 20 20 48 65 6e 63 65 3a 0a  alysis.  Hence:.
2a10c 2a 2a 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54  **.**     WhereT
2a10d 65 72 6d 2e 77 74 46 6c 61 67 73 20 20 20 7c 3d  erm.wtFlags   |=
2a10e 20 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 0a 2a 2a    TERM_ORINFO.**
2a10f 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 75       WhereTerm.u
2a110 2e 70 4f 72 49 6e 66 6f 20 20 3d 20 20 61 20 64  .pOrInfo  =  a d
2a111 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
2a112 61 74 65 64 20 57 68 65 72 65 4f 72 54 65 72 6d  ated WhereOrTerm
2a113 20 6f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68   object.**.** Th
2a114 65 20 74 65 72 6d 20 62 65 69 6e 67 20 61 6e 61  e term being ana
2a115 6c 79 7a 65 64 20 6d 75 73 74 20 68 61 76 65 20  lyzed must have 
2a116 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 66 20 4f  two or more of O
2a117 52 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62 74  R-connected subt
2a118 65 72 6d 73 2e 0a 2a 2a 20 41 20 73 69 6e 67 6c  erms..** A singl
2a119 65 20 73 75 62 74 65 72 6d 20 6d 69 67 68 74 20  e subterm might 
2a11a 62 65 20 61 20 73 65 74 20 6f 66 20 41 4e 44 2d  be a set of AND-
2a11b 63 6f 6e 6e 65 63 74 65 64 20 73 75 62 2d 73 75  connected sub-su
2a11c 62 74 65 72 6d 73 2e 0a 2a 2a 20 45 78 61 6d 70  bterms..** Examp
2a11d 6c 65 73 20 6f 66 20 74 65 72 6d 73 20 75 6e 64  les of terms und
2a11e 65 72 20 61 6e 61 6c 79 73 69 73 3a 0a 2a 2a 0a  er analysis:.**.
2a11f 2a 2a 20 20 20 20 20 28 41 29 20 20 20 20 20 74  **     (A)     t
2a120 31 2e 78 3d 74 32 2e 79 20 4f 52 20 74 31 2e 78  1.x=t2.y OR t1.x
2a121 3d 74 32 2e 7a 20 4f 52 20 74 31 2e 79 3d 31 35  =t2.z OR t1.y=15
2a122 20 4f 52 20 74 31 2e 7a 3d 74 33 2e 61 2b 35 0a   OR t1.z=t3.a+5.
2a123 2a 2a 20 20 20 20 20 28 42 29 20 20 20 20 20 78  **     (B)     x
2a124 3d 65 78 70 72 31 20 4f 52 20 65 78 70 72 32 3d  =expr1 OR expr2=
2a125 78 20 4f 52 20 78 3d 65 78 70 72 33 0a 2a 2a 20  x OR x=expr3.** 
2a126 20 20 20 20 28 43 29 20 20 20 20 20 74 31 2e 78      (C)     t1.x
2a127 3d 74 32 2e 79 20 4f 52 20 28 74 31 2e 78 3d 74  =t2.y OR (t1.x=t
2a128 32 2e 7a 20 41 4e 44 20 74 31 2e 79 3d 31 35 29  2.z AND t1.y=15)
2a129 0a 2a 2a 20 20 20 20 20 28 44 29 20 20 20 20 20  .**     (D)     
2a12a 78 3d 65 78 70 72 31 20 4f 52 20 28 79 3e 31 31  x=expr1 OR (y>11
2a12b 20 41 4e 44 20 79 3c 32 32 20 41 4e 44 20 7a 20   AND y<22 AND z 
2a12c 4c 49 4b 45 20 27 2a 68 65 6c 6c 6f 2a 27 29 0a  LIKE '*hello*').
2a12d 2a 2a 20 20 20 20 20 28 45 29 20 20 20 20 20 28  **     (E)     (
2a12e 70 2e 61 3d 31 20 41 4e 44 20 71 2e 62 3d 32 20  p.a=1 AND q.b=2 
2a12f 41 4e 44 20 72 2e 63 3d 33 29 20 4f 52 20 28 70  AND r.c=3) OR (p
2a130 2e 78 3d 34 20 41 4e 44 20 71 2e 79 3d 35 20 41  .x=4 AND q.y=5 A
2a131 4e 44 20 72 2e 7a 3d 36 29 0a 2a 2a 0a 2a 2a 20  ND r.z=6).**.** 
2a132 43 41 53 45 20 31 3a 0a 2a 2a 0a 2a 2a 20 49 66  CASE 1:.**.** If
2a133 20 61 6c 6c 20 73 75 62 74 65 72 6d 73 20 61 72   all subterms ar
2a134 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 54 2e  e of the form T.
2a135 43 3d 65 78 70 72 20 66 6f 72 20 73 6f 6d 65 20  C=expr for some 
2a136 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66  single column of
2a137 20 43 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74   C.** a single t
2a138 61 62 6c 65 20 54 20 28 61 73 20 73 68 6f 77 6e  able T (as shown
2a139 20 69 6e 20 65 78 61 6d 70 6c 65 20 42 20 61 62   in example B ab
2a13a 6f 76 65 29 20 74 68 65 6e 20 63 72 65 61 74 65  ove) then create
2a13b 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 0a 2a   a new virtual.*
2a13c 2a 20 74 65 72 6d 20 74 68 61 74 20 69 73 20 61  * term that is a
2a13d 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 49 4e 20  n equivalent IN 
2a13e 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 6e 20  expression.  In 
2a13f 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
2a140 74 68 65 20 74 65 72 6d 0a 2a 2a 20 62 65 69 6e  the term.** bein
2a141 67 20 61 6e 61 6c 79 7a 65 64 20 69 73 3a 0a 2a  g analyzed is:.*
2a142 2a 0a 2a 2a 20 20 20 20 20 20 78 20 3d 20 65 78  *.**      x = ex
2a143 70 72 31 20 20 4f 52 20 20 65 78 70 72 32 20 3d  pr1  OR  expr2 =
2a144 20 78 20 20 4f 52 20 20 78 20 3d 20 65 78 70 72   x  OR  x = expr
2a145 33 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 63 72 65  3.**.** then cre
2a146 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61  ate a new virtua
2a147 6c 20 74 65 72 6d 20 6c 69 6b 65 20 74 68 69 73  l term like this
2a148 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20 49  :.**.**      x I
2a149 4e 20 28 65 78 70 72 31 2c 65 78 70 72 32 2c 65  N (expr1,expr2,e
2a14a 78 70 72 33 29 0a 2a 2a 0a 2a 2a 20 43 41 53 45  xpr3).**.** CASE
2a14b 20 32 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6c 6c   2:.**.** If all
2a14c 20 73 75 62 74 65 72 6d 73 20 61 72 65 20 69 6e   subterms are in
2a14d 64 65 78 61 62 6c 65 20 62 79 20 61 20 73 69 6e  dexable by a sin
2a14e 67 6c 65 20 74 61 62 6c 65 20 54 2c 20 74 68 65  gle table T, the
2a14f 6e 20 73 65 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20  n set.**.**     
2a150 57 68 65 72 65 54 65 72 6d 2e 65 4f 70 65 72 61  WhereTerm.eOpera
2a151 74 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20  tor             
2a152 20 3d 20 20 57 4f 5f 4f 52 0a 2a 2a 20 20 20 20   =  WO_OR.**    
2a153 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72   WhereTerm.u.pOr
2a154 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20  Info->indexable 
2a155 20 7c 3d 20 20 74 68 65 20 63 75 72 73 6f 72 20   |=  the cursor 
2a156 6e 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65  number for table
2a157 20 54 0a 2a 2a 0a 2a 2a 20 41 20 73 75 62 74 65   T.**.** A subte
2a158 72 6d 20 69 73 20 22 69 6e 64 65 78 61 62 6c 65  rm is "indexable
2a159 22 20 69 66 20 69 74 20 69 73 20 6f 66 20 74 68  " if it is of th
2a15a 65 20 66 6f 72 6d 0a 2a 2a 20 22 54 2e 43 20 3c  e form.** "T.C <
2a15b 6f 70 3e 20 3c 65 78 70 72 3e 22 20 77 68 65 72  op> <expr>" wher
2a15c 65 20 43 20 69 73 20 61 6e 79 20 63 6f 6c 75 6d  e C is any colum
2a15d 6e 20 6f 66 20 74 61 62 6c 65 20 54 20 61 6e 64  n of table T and
2a15e 20 0a 2a 2a 20 3c 6f 70 3e 20 69 73 20 6f 6e 65   .** <op> is one
2a15f 20 6f 66 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3c   of "=", "<", "<
2a160 3d 22 2c 20 22 3e 22 2c 20 22 3e 3d 22 2c 20 22  =", ">", ">=", "
2a161 49 53 20 4e 55 4c 4c 22 2c 20 6f 72 20 22 49 4e  IS NULL", or "IN
2a162 22 2e 0a 2a 2a 20 41 20 73 75 62 74 65 72 6d 20  "..** A subterm 
2a163 69 73 20 61 6c 73 6f 20 69 6e 64 65 78 61 62 6c  is also indexabl
2a164 65 20 69 66 20 69 74 20 69 73 20 61 6e 20 41 4e  e if it is an AN
2a165 44 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65  D of two or more
2a166 0a 2a 2a 20 73 75 62 73 75 62 74 65 72 6d 73 20  .** subsubterms 
2a167 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20  at least one of 
2a168 77 68 69 63 68 20 69 73 20 69 6e 64 65 78 61 62  which is indexab
2a169 6c 65 2e 20 20 49 6e 64 65 78 61 62 6c 65 20 41  le.  Indexable A
2a16a 4e 44 20 0a 2a 2a 20 73 75 62 74 65 72 6d 73 20  ND .** subterms 
2a16b 68 61 76 65 20 74 68 65 69 72 20 65 4f 70 65 72  have their eOper
2a16c 61 74 6f 72 20 73 65 74 20 74 6f 20 57 4f 5f 41  ator set to WO_A
2a16d 4e 44 20 61 6e 64 20 74 68 65 79 20 68 61 76 65  ND and they have
2a16e 0a 2a 2a 20 75 2e 70 41 6e 64 49 6e 66 6f 20 73  .** u.pAndInfo s
2a16f 65 74 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61  et to a dynamica
2a170 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 57 68  lly allocated Wh
2a171 65 72 65 41 6e 64 54 65 72 6d 20 6f 62 6a 65 63  ereAndTerm objec
2a172 74 2e 0a 2a 2a 0a 2a 2a 20 46 72 6f 6d 20 61 6e  t..**.** From an
2a173 6f 74 68 65 72 20 70 6f 69 6e 74 20 6f 66 20 76  other point of v
2a174 69 65 77 2c 20 22 69 6e 64 65 78 61 62 6c 65 22  iew, "indexable"
2a175 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2a176 73 75 62 74 65 72 6d 20 63 6f 75 6c 64 0a 2a 2a  subterm could.**
2a177 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20   potentially be 
2a178 75 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64  used with an ind
2a179 65 78 20 69 66 20 61 6e 20 61 70 70 72 6f 70 72  ex if an appropr
2a17a 69 61 74 65 20 69 6e 64 65 78 20 65 78 69 73 74  iate index exist
2a17b 73 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 61 6c 79  s..** This analy
2a17c 73 69 73 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e  sis does not con
2a17d 73 69 64 65 72 20 77 68 65 74 68 65 72 20 6f 72  sider whether or
2a17e 20 6e 6f 74 20 74 68 65 20 69 6e 64 65 78 20 65   not the index e
2a17f 78 69 73 74 73 3b 20 74 68 61 74 0a 2a 2a 20 69  xists; that.** i
2a180 73 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 65 20  s something the 
2a181 62 65 73 74 49 6e 64 65 78 28 29 20 72 6f 75 74  bestIndex() rout
2a182 69 6e 65 20 77 69 6c 6c 20 64 65 74 65 72 6d 69  ine will determi
2a183 6e 65 2e 20 20 54 68 69 73 20 61 6e 61 6c 79 73  ne.  This analys
2a184 69 73 0a 2a 2a 20 6f 6e 6c 79 20 6c 6f 6f 6b 73  is.** only looks
2a185 20 61 74 20 77 68 65 74 68 65 72 20 73 75 62 74   at whether subt
2a186 65 72 6d 73 20 61 70 70 72 6f 70 72 69 61 74 65  erms appropriate
2a187 20 66 6f 72 20 69 6e 64 65 78 69 6e 67 20 65 78   for indexing ex
2a188 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65  ist..**.** All e
2a189 78 61 6d 70 6c 65 73 20 41 20 74 68 72 6f 75 67  xamples A throug
2a18a 68 20 45 20 61 62 6f 76 65 20 61 6c 6c 20 73 61  h E above all sa
2a18b 74 69 73 66 79 20 63 61 73 65 20 32 2e 20 20 42  tisfy case 2.  B
2a18c 75 74 20 69 66 20 61 20 74 65 72 6d 0a 2a 2a 20  ut if a term.** 
2a18d 61 6c 73 6f 20 73 74 61 74 69 73 66 69 65 73 20  also statisfies 
2a18e 63 61 73 65 20 31 20 28 73 75 63 68 20 61 73 20  case 1 (such as 
2a18f 42 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20  B) we know that 
2a190 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 77 69  the optimizer wi
2a191 6c 6c 0a 2a 2a 20 61 6c 77 61 79 73 20 70 72 65  ll.** always pre
2a192 66 65 72 20 63 61 73 65 20 31 2c 20 73 6f 20 69  fer case 1, so i
2a193 6e 20 74 68 61 74 20 63 61 73 65 20 77 65 20 70  n that case we p
2a194 72 65 74 65 6e 64 20 74 68 61 74 20 63 61 73 65  retend that case
2a195 20 32 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 61 74   2 is not.** sat
2a196 69 73 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74  isfied..**.** It
2a197 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 61   might be the ca
2a198 73 65 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65  se that multiple
2a199 20 74 61 62 6c 65 73 20 61 72 65 20 69 6e 64 65   tables are inde
2a19a 78 61 62 6c 65 2e 20 20 46 6f 72 20 65 78 61 6d  xable.  For exam
2a19b 70 6c 65 2c 0a 2a 2a 20 28 45 29 20 61 62 6f 76  ple,.** (E) abov
2a19c 65 20 69 73 20 69 6e 64 65 78 61 62 6c 65 20 6f  e is indexable o
2a19d 6e 20 74 61 62 6c 65 73 20 50 2c 20 51 2c 20 61  n tables P, Q, a
2a19e 6e 64 20 52 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d  nd R..**.** Term
2a19f 73 20 74 68 61 74 20 73 61 74 69 73 66 79 20 63  s that satisfy c
2a1a0 61 73 65 20 32 20 61 72 65 20 63 61 6e 64 69 64  ase 2 are candid
2a1a1 61 74 65 73 20 66 6f 72 20 6c 6f 6f 6b 75 70 20  ates for lookup 
2a1a2 62 79 20 75 73 69 6e 67 0a 2a 2a 20 73 65 70 61  by using.** sepa
2a1a3 72 61 74 65 20 69 6e 64 69 63 65 73 20 74 6f 20  rate indices to 
2a1a4 66 69 6e 64 20 72 6f 77 69 64 73 20 66 6f 72 20  find rowids for 
2a1a5 65 61 63 68 20 73 75 62 74 65 72 6d 20 61 6e 64  each subterm and
2a1a6 20 63 6f 6d 70 6f 73 69 6e 67 0a 2a 2a 20 74 68   composing.** th
2a1a7 65 20 75 6e 69 6f 6e 20 6f 66 20 61 6c 6c 20 72  e union of all r
2a1a8 6f 77 69 64 73 20 75 73 69 6e 67 20 61 20 52 6f  owids using a Ro
2a1a9 77 53 65 74 20 6f 62 6a 65 63 74 2e 20 20 54 68  wSet object.  Th
2a1aa 69 73 20 69 73 20 73 69 6d 69 6c 61 72 0a 2a 2a  is is similar.**
2a1ab 20 74 6f 20 22 62 69 74 6d 61 70 20 69 6e 64 69   to "bitmap indi
2a1ac 63 65 73 22 20 69 6e 20 6f 74 68 65 72 20 64 61  ces" in other da
2a1ad 74 61 62 61 73 65 20 65 6e 67 69 6e 65 73 2e 0a  tabase engines..
2a1ae 2a 2a 0a 2a 2a 20 4f 54 48 45 52 57 49 53 45 3a  **.** OTHERWISE:
2a1af 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65  .**.** If neithe
2a1b0 72 20 63 61 73 65 20 31 20 6e 6f 72 20 63 61 73  r case 1 nor cas
2a1b1 65 20 32 20 61 70 70 6c 79 2c 20 74 68 65 6e 20  e 2 apply, then 
2a1b2 6c 65 61 76 65 20 74 68 65 20 65 4f 70 65 72 61  leave the eOpera
2a1b3 74 6f 72 20 73 65 74 20 74 6f 0a 2a 2a 20 7a 65  tor set to.** ze
2a1b4 72 6f 2e 20 20 54 68 69 73 20 74 65 72 6d 20 69  ro.  This term i
2a1b5 73 20 6e 6f 74 20 75 73 65 66 75 6c 20 66 6f 72  s not useful for
2a1b6 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 73 74 61 74   search..*/.stat
2a1b7 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c  ic void exprAnal
2a1b8 79 7a 65 4f 72 54 65 72 6d 28 0a 20 20 53 72 63  yzeOrTerm(.  Src
2a1b9 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
2a1ba 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52         /* the FR
2a1bb 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57  OM clause */.  W
2a1bc 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
2a1bd 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
2a1be 63 6f 6d 70 6c 65 74 65 20 57 48 45 52 45 20 63  complete WHERE c
2a1bf 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
2a1c0 64 78 54 65 72 6d 20 20 20 20 20 20 20 20 20 20  dxTerm          
2a1c1 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
2a1c2 20 74 68 65 20 4f 52 2d 74 65 72 6d 20 74 6f 20   the OR-term to 
2a1c3 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29  be analyzed */.)
2a1c4 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
2a1c5 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b  e = pWC->pParse;
2a1c6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2a1c7 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2a1c8 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2a1c9 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20   pParse->db;    
2a1ca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2a1cb 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2a1cc 6e 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d  n */.  WhereTerm
2a1cd 20 2a 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e   *pTerm = &pWC->
2a1ce 61 5b 69 64 78 54 65 72 6d 5d 3b 20 20 20 20 2f  a[idxTerm];    /
2a1cf 2a 20 54 68 65 20 74 65 72 6d 20 74 6f 20 62 65  * The term to be
2a1d0 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 45   analyzed */.  E
2a1d1 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 54 65  xpr *pExpr = pTe
2a1d2 72 6d 2d 3e 70 45 78 70 72 3b 20 20 20 20 20 20  rm->pExpr;      
2a1d3 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
2a1d4 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20  pression of the 
2a1d5 74 65 72 6d 20 2a 2f 0a 20 20 57 68 65 72 65 4d  term */.  WhereM
2a1d6 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
2a1d7 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74   = pWC->pMaskSet
2a1d8 3b 20 2f 2a 20 54 61 62 6c 65 20 75 73 65 20 6d  ; /* Table use m
2a1d9 61 73 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  asks */.  int i;
2a1da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a1db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a1dc 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2a1dd 72 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  rs */.  WhereCla
2a1de 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 20 20 20  use *pOrWc;     
2a1df 20 20 2f 2a 20 42 72 65 61 6b 75 70 20 6f 66 20    /* Breakup of 
2a1e0 70 54 65 72 6d 20 69 6e 74 6f 20 73 75 62 74 65  pTerm into subte
2a1e1 72 6d 73 20 2a 2f 0a 20 20 57 68 65 72 65 54 65  rms */.  WhereTe
2a1e2 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 20 20 20 20  rm *pOrTerm;    
2a1e3 20 20 20 2f 2a 20 41 20 53 75 62 2d 74 65 72 6d     /* A Sub-term
2a1e4 20 77 69 74 68 69 6e 20 74 68 65 20 70 4f 72 57   within the pOrW
2a1e5 63 20 2a 2f 0a 20 20 57 68 65 72 65 4f 72 49 6e  c */.  WhereOrIn
2a1e6 66 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 20 20 20  fo *pOrInfo;    
2a1e7 20 2f 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69   /* Additional i
2a1e8 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 73 6f 63  nformation assoc
2a1e9 69 61 74 65 64 20 77 69 74 68 20 70 54 65 72 6d  iated with pTerm
2a1ea 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 63 68   */.  Bitmask ch
2a1eb 6e 67 54 6f 49 4e 3b 20 20 20 20 20 20 20 20 20  ngToIN;         
2a1ec 2f 2a 20 54 61 62 6c 65 73 20 74 68 61 74 20 6d  /* Tables that m
2a1ed 69 67 68 74 20 73 61 74 69 73 66 79 20 63 61 73  ight satisfy cas
2a1ee 65 20 31 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  e 1 */.  Bitmask
2a1ef 20 69 6e 64 65 78 61 62 6c 65 3b 20 20 20 20 20   indexable;     
2a1f0 20 20 20 2f 2a 20 54 61 62 6c 65 73 20 74 68 61     /* Tables tha
2a1f1 74 20 61 72 65 20 69 6e 64 65 78 61 62 6c 65 2c  t are indexable,
2a1f2 20 73 61 74 69 73 66 79 69 6e 67 20 63 61 73 65   satisfying case
2a1f3 20 32 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a   2 */..  /*.  **
2a1f4 20 42 72 65 61 6b 20 74 68 65 20 4f 52 20 63 6c   Break the OR cl
2a1f5 61 75 73 65 20 69 6e 74 6f 20 69 74 73 20 73 65  ause into its se
2a1f6 70 61 72 61 74 65 20 73 75 62 74 65 72 6d 73 2e  parate subterms.
2a1f7 20 20 54 68 65 20 73 75 62 74 65 72 6d 73 20 61    The subterms a
2a1f8 72 65 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69  re.  ** stored i
2a1f9 6e 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20  n a WhereClause 
2a1fa 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
2a1fb 6e 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20  ning within the 
2a1fc 57 68 65 72 65 4f 72 49 6e 66 6f 0a 20 20 2a 2a  WhereOrInfo.  **
2a1fd 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 73 20   object that is 
2a1fe 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20  attached to the 
2a1ff 6f 72 69 67 69 6e 61 6c 20 4f 52 20 63 6c 61 75  original OR clau
2a200 73 65 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20  se term..  */.  
2a201 61 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e  assert( (pTerm->
2a202 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f  wtFlags & (TERM_
2a203 44 59 4e 41 4d 49 43 7c 54 45 52 4d 5f 4f 52 49  DYNAMIC|TERM_ORI
2a204 4e 46 4f 7c 54 45 52 4d 5f 41 4e 44 49 4e 46 4f  NFO|TERM_ANDINFO
2a205 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ))==0 );.  asser
2a206 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  t( pExpr->op==TK
2a207 5f 4f 52 20 29 3b 0a 20 20 70 54 65 72 6d 2d 3e  _OR );.  pTerm->
2a208 75 2e 70 4f 72 49 6e 66 6f 20 3d 20 70 4f 72 49  u.pOrInfo = pOrI
2a209 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  nfo = sqlite3DbM
2a20a 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2a20b 7a 65 6f 66 28 2a 70 4f 72 49 6e 66 6f 29 29 3b  zeof(*pOrInfo));
2a20c 0a 20 20 69 66 28 20 70 4f 72 49 6e 66 6f 3d 3d  .  if( pOrInfo==
2a20d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 54  0 ) return;.  pT
2a20e 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20  erm->wtFlags |= 
2a20f 54 45 52 4d 5f 4f 52 49 4e 46 4f 3b 0a 20 20 70  TERM_ORINFO;.  p
2a210 4f 72 57 63 20 3d 20 26 70 4f 72 49 6e 66 6f 2d  OrWc = &pOrInfo-
2a211 3e 77 63 3b 0a 20 20 77 68 65 72 65 43 6c 61 75  >wc;.  whereClau
2a212 73 65 49 6e 69 74 28 70 4f 72 57 63 2c 20 70 57  seInit(pOrWc, pW
2a213 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61 73 6b  C->pParse, pMask
2a214 53 65 74 29 3b 0a 20 20 77 68 65 72 65 53 70 6c  Set);.  whereSpl
2a215 69 74 28 70 4f 72 57 63 2c 20 70 45 78 70 72 2c  it(pOrWc, pExpr,
2a216 20 54 4b 5f 4f 52 29 3b 0a 20 20 65 78 70 72 41   TK_OR);.  exprA
2a217 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20  nalyzeAll(pSrc, 
2a218 70 4f 72 57 63 29 3b 0a 20 20 69 66 28 20 64 62  pOrWc);.  if( db
2a219 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2a21a 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
2a21b 74 28 20 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 3e  t( pOrWc->nTerm>
2a21c 3d 32 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  =2 );..  /*.  **
2a21d 20 43 6f 6d 70 75 74 65 20 74 68 65 20 73 65 74   Compute the set
2a21e 20 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20   of tables that 
2a21f 6d 69 67 68 74 20 73 61 74 69 73 66 79 20 63 61  might satisfy ca
2a220 73 65 73 20 31 20 6f 72 20 32 2e 0a 20 20 2a 2f  ses 1 or 2..  */
2a221 0a 20 20 69 6e 64 65 78 61 62 6c 65 20 3d 20 63  .  indexable = c
2a222 68 6e 67 54 6f 49 4e 20 3d 20 7e 28 42 69 74 6d  hngToIN = ~(Bitm
2a223 61 73 6b 29 30 3b 0a 20 20 66 6f 72 28 69 3d 70  ask)0;.  for(i=p
2a224 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70  OrWc->nTerm-1, p
2a225 4f 72 54 65 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b  OrTerm=pOrWc->a;
2a226 20 69 3e 3d 30 20 26 26 20 69 6e 64 65 78 61 62   i>=0 && indexab
2a227 6c 65 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d  le; i--, pOrTerm
2a228 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 70 4f  ++){.    if( (pO
2a229 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  rTerm->eOperator
2a22a 20 26 20 57 4f 5f 53 49 4e 47 4c 45 29 3d 3d 30   & WO_SINGLE)==0
2a22b 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 41   ){.      WhereA
2a22c 6e 64 49 6e 66 6f 20 2a 70 41 6e 64 49 6e 66 6f  ndInfo *pAndInfo
2a22d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2a22e 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
2a22f 6f 72 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  or==0 );.      a
2a230 73 73 65 72 74 28 20 28 70 4f 72 54 65 72 6d 2d  ssert( (pOrTerm-
2a231 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d  >wtFlags & (TERM
2a232 5f 41 4e 44 49 4e 46 4f 7c 54 45 52 4d 5f 4f 52  _ANDINFO|TERM_OR
2a233 49 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20 20 20  INFO))==0 );.   
2a234 20 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20 30 3b     chngToIN = 0;
2a235 0a 20 20 20 20 20 20 70 41 6e 64 49 6e 66 6f 20  .      pAndInfo 
2a236 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2a237 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
2a238 2a 70 41 6e 64 49 6e 66 6f 29 29 3b 0a 20 20 20  *pAndInfo));.   
2a239 20 20 20 69 66 28 20 70 41 6e 64 49 6e 66 6f 20     if( pAndInfo 
2a23a 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  ){.        Where
2a23b 43 6c 61 75 73 65 20 2a 70 41 6e 64 57 43 3b 0a  Clause *pAndWC;.
2a23c 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72          WhereTer
2a23d 6d 20 2a 70 41 6e 64 54 65 72 6d 3b 0a 20 20 20  m *pAndTerm;.   
2a23e 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2a23f 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20 3d 20      Bitmask b = 
2a240 30 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65  0;.        pOrTe
2a241 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 20 3d  rm->u.pAndInfo =
2a242 20 70 41 6e 64 49 6e 66 6f 3b 0a 20 20 20 20 20   pAndInfo;.     
2a243 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c     pOrTerm->wtFl
2a244 61 67 73 20 7c 3d 20 54 45 52 4d 5f 41 4e 44 49  ags |= TERM_ANDI
2a245 4e 46 4f 3b 0a 20 20 20 20 20 20 20 20 70 4f 72  NFO;.        pOr
2a246 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2a247 3d 20 57 4f 5f 41 4e 44 3b 0a 20 20 20 20 20 20  = WO_AND;.      
2a248 20 20 70 41 6e 64 57 43 20 3d 20 26 70 41 6e 64    pAndWC = &pAnd
2a249 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20  Info->wc;.      
2a24a 20 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69    whereClauseIni
2a24b 74 28 70 41 6e 64 57 43 2c 20 70 57 43 2d 3e 70  t(pAndWC, pWC->p
2a24c 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74 29  Parse, pMaskSet)
2a24d 3b 0a 20 20 20 20 20 20 20 20 77 68 65 72 65 53  ;.        whereS
2a24e 70 6c 69 74 28 70 41 6e 64 57 43 2c 20 70 4f 72  plit(pAndWC, pOr
2a24f 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 54 4b 5f  Term->pExpr, TK_
2a250 41 4e 44 29 3b 0a 20 20 20 20 20 20 20 20 65 78  AND);.        ex
2a251 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72  prAnalyzeAll(pSr
2a252 63 2c 20 70 41 6e 64 57 43 29 3b 0a 20 20 20 20  c, pAndWC);.    
2a253 20 20 20 20 74 65 73 74 63 61 73 65 28 20 64 62      testcase( db
2a254 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2a255 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 64  ;.        if( !d
2a256 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2a257 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  ){.          for
2a258 28 6a 3d 30 2c 20 70 41 6e 64 54 65 72 6d 3d 70  (j=0, pAndTerm=p
2a259 41 6e 64 57 43 2d 3e 61 3b 20 6a 3c 70 41 6e 64  AndWC->a; j<pAnd
2a25a 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20  WC->nTerm; j++, 
2a25b 70 41 6e 64 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  pAndTerm++){.   
2a25c 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2a25d 20 70 41 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72   pAndTerm->pExpr
2a25e 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
2a25f 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 41  if( allowedOp(pA
2a260 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 6f  ndTerm->pExpr->o
2a261 70 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  p) ){.          
2a262 20 20 20 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b      b |= getMask
2a263 28 70 4d 61 73 6b 53 65 74 2c 20 70 41 6e 64 54  (pMaskSet, pAndT
2a264 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29  erm->leftCursor)
2a265 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
2a266 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2a267 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 6e      }.        in
2a268 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20  dexable &= b;.  
2a269 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20      }.    }else 
2a26a 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46  if( pOrTerm->wtF
2a26b 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 49  lags & TERM_COPI
2a26c 45 44 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53  ED ){.      /* S
2a26d 6b 69 70 20 74 68 69 73 20 74 65 72 6d 20 66 6f  kip this term fo
2a26e 72 20 6e 6f 77 2e 20 20 57 65 20 72 65 76 69 73  r now.  We revis
2a26f 69 74 20 69 74 20 77 68 65 6e 20 77 65 20 70 72  it it when we pr
2a270 6f 63 65 73 73 20 74 68 65 0a 20 20 20 20 20 20  ocess the.      
2a271 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
2a272 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 74 65   TERM_VIRTUAL te
2a273 72 6d 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b  rm */.    }else{
2a274 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62  .      Bitmask b
2a275 3b 0a 20 20 20 20 20 20 62 20 3d 20 67 65 74 4d  ;.      b = getM
2a276 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f  ask(pMaskSet, pO
2a277 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2a278 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f  r);.      if( pO
2a279 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2a27a 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 7b   TERM_VIRTUAL ){
2a27b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65  .        WhereTe
2a27c 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70 4f  rm *pOther = &pO
2a27d 72 57 63 2d 3e 61 5b 70 4f 72 54 65 72 6d 2d 3e  rWc->a[pOrTerm->
2a27e 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20 20 20  iParent];.      
2a27f 20 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b 28 70    b |= getMask(p
2a280 4d 61 73 6b 53 65 74 2c 20 70 4f 74 68 65 72 2d  MaskSet, pOther-
2a281 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b 0a 20 20  >leftCursor);.  
2a282 20 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 64 65      }.      inde
2a283 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20 20 20  xable &= b;.    
2a284 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65    if( pOrTerm->e
2a285 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 45 51 20  Operator!=WO_EQ 
2a286 29 7b 0a 20 20 20 20 20 20 20 20 63 68 6e 67 54  ){.        chngT
2a287 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  oIN = 0;.      }
2a288 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 68  else{.        ch
2a289 6e 67 54 6f 49 4e 20 26 3d 20 62 3b 0a 20 20 20  ngToIN &= b;.   
2a28a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
2a28b 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 63 6f 72 64    /*.  ** Record
2a28c 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c   the set of tabl
2a28d 65 73 20 74 68 61 74 20 73 61 74 69 73 66 79 20  es that satisfy 
2a28e 63 61 73 65 20 32 2e 20 20 54 68 65 20 73 65 74  case 2.  The set
2a28f 20 6d 69 67 68 74 20 62 65 0a 20 20 2a 2a 20 65   might be.  ** e
2a290 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 70 4f 72  mpty..  */.  pOr
2a291 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20  Info->indexable 
2a292 3d 20 69 6e 64 65 78 61 62 6c 65 3b 0a 20 20 70  = indexable;.  p
2a293 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2a294 3d 20 69 6e 64 65 78 61 62 6c 65 3d 3d 30 20 3f  = indexable==0 ?
2a295 20 30 20 3a 20 57 4f 5f 4f 52 3b 0a 0a 20 20 2f   0 : WO_OR;..  /
2a296 2a 0a 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e 20  *.  ** chngToIN 
2a297 68 6f 6c 64 73 20 61 20 73 65 74 20 6f 66 20 74  holds a set of t
2a298 61 62 6c 65 73 20 74 68 61 74 20 2a 6d 69 67 68  ables that *migh
2a299 74 2a 20 73 61 74 69 73 66 79 20 63 61 73 65 20  t* satisfy case 
2a29a 31 2e 20 20 42 75 74 0a 20 20 2a 2a 20 77 65 20  1.  But.  ** we 
2a29b 68 61 76 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20  have to do some 
2a29c 61 64 64 69 74 69 6f 6e 61 6c 20 63 68 65 63 6b  additional check
2a29d 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 63 61  ing to see if ca
2a29e 73 65 20 31 20 72 65 61 6c 6c 79 0a 20 20 2a 2a  se 1 really.  **
2a29f 20 69 73 20 73 61 74 69 73 66 69 65 64 2e 0a 20   is satisfied.. 
2a2a0 20 2a 2f 0a 20 20 69 66 28 20 63 68 6e 67 54 6f   */.  if( chngTo
2a2a1 49 4e 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 6b  IN ){.    int ok
2a2a2 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 20  ToChngToIN = 0; 
2a2a3 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
2a2a4 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f  he conversion to
2a2a5 20 49 4e 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a   IN is valid */.
2a2a6 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 20      int iColumn 
2a2a7 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 2f 2a  = -1;         /*
2a2a8 20 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 6f 6e   Column index on
2a2a9 20 6c 68 73 20 6f 66 20 49 4e 20 6f 70 65 72 61   lhs of IN opera
2a2aa 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  tor */.    int i
2a2ab 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20  Cursor;         
2a2ac 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75       /* Table cu
2a2ad 72 73 6f 72 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61  rsor common to a
2a2ae 6c 6c 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20  ll terms */.    
2a2af 69 6e 74 20 6a 20 3d 20 30 3b 20 20 20 20 20 20  int j = 0;      
2a2b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2a2b1 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20  p counter */..  
2a2b2 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20    /* Search for 
2a2b3 61 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  a table and colu
2a2b4 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72 73 20  mn that appears 
2a2b5 6f 6e 20 6f 6e 65 20 73 69 64 65 20 6f 72 20 74  on one side or t
2a2b6 68 65 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20  he.    ** other 
2a2b7 6f 66 20 74 68 65 20 3d 3d 20 6f 70 65 72 61 74  of the == operat
2a2b8 6f 72 20 69 6e 20 65 76 65 72 79 20 73 75 62 74  or in every subt
2a2b9 65 72 6d 2e 20 20 54 68 61 74 20 74 61 62 6c 65  erm.  That table
2a2ba 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 20 20 20 20   and column.    
2a2bb 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 72  ** will be recor
2a2bc 64 65 64 20 69 6e 20 69 43 75 72 73 6f 72 20 61  ded in iCursor a
2a2bd 6e 64 20 69 43 6f 6c 75 6d 6e 2e 20 20 54 68 65  nd iColumn.  The
2a2be 72 65 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20  re might not be 
2a2bf 61 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63 68 20  any.    ** such 
2a2c0 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
2a2c1 2e 20 20 53 65 74 20 6f 6b 54 6f 43 68 6e 67 54  .  Set okToChngT
2a2c2 6f 49 4e 20 69 66 20 61 6e 20 61 70 70 72 6f 70  oIN if an approp
2a2c3 72 69 61 74 65 20 74 61 62 6c 65 0a 20 20 20 20  riate table.    
2a2c4 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 69 73  ** and column is
2a2c5 20 66 6f 75 6e 64 20 62 75 74 20 6c 65 61 76 65   found but leave
2a2c6 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 66 61   okToChngToIN fa
2a2c7 6c 73 65 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64  lse if not found
2a2c8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
2a2c9 28 6a 3d 30 3b 20 6a 3c 32 20 26 26 20 21 6f 6b  (j=0; j<2 && !ok
2a2ca 54 6f 43 68 6e 67 54 6f 49 4e 3b 20 6a 2b 2b 29  ToChngToIN; j++)
2a2cb 7b 0a 20 20 20 20 20 20 70 4f 72 54 65 72 6d 20  {.      pOrTerm 
2a2cc 3d 20 70 4f 72 57 63 2d 3e 61 3b 0a 20 20 20 20  = pOrWc->a;.    
2a2cd 20 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e    for(i=pOrWc->n
2a2ce 54 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  Term-1; i>=0; i-
2a2cf 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20  -, pOrTerm++){. 
2a2d0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2a2d1 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  OrTerm->eOperato
2a2d2 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20  r==WO_EQ );.    
2a2d3 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46      pOrTerm->wtF
2a2d4 6c 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52  lags &= ~TERM_OR
2a2d5 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 69 66 28  _OK;.        if(
2a2d6 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75   pOrTerm->leftCu
2a2d7 72 73 6f 72 3d 3d 69 43 6f 6c 75 6d 6e 20 29 20  rsor==iColumn ) 
2a2d8 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2a2d9 20 20 69 66 28 20 28 63 68 6e 67 54 6f 49 4e 20    if( (chngToIN 
2a2da 26 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53  & getMask(pMaskS
2a2db 65 74 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66  et, pOrTerm->lef
2a2dc 74 43 75 72 73 6f 72 29 29 3d 3d 30 20 29 20 63  tCursor))==0 ) c
2a2dd 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
2a2de 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 4f 72 54 65   iColumn = pOrTe
2a2df 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e  rm->u.leftColumn
2a2e0 3b 0a 20 20 20 20 20 20 20 20 69 43 75 72 73 6f  ;.        iCurso
2a2e1 72 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66  r = pOrTerm->lef
2a2e2 74 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20  tCursor;.       
2a2e3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2a2e4 20 20 20 20 20 20 69 66 28 20 69 3c 30 20 29 7b        if( i<0 ){
2a2e5 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2a2e6 20 6a 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20   j==1 );.       
2a2e7 20 61 73 73 65 72 74 28 20 28 63 68 6e 67 54 6f   assert( (chngTo
2a2e8 49 4e 26 28 63 68 6e 67 54 6f 49 4e 2d 31 29 29  IN&(chngToIN-1))
2a2e9 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ==0 );.        a
2a2ea 73 73 65 72 74 28 20 63 68 6e 67 54 6f 49 4e 3d  ssert( chngToIN=
2a2eb 3d 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65  =getMask(pMaskSe
2a2ec 74 2c 20 69 43 6f 6c 75 6d 6e 29 20 29 3b 0a 20  t, iColumn) );. 
2a2ed 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a2ee 20 20 20 20 7d 0a 20 20 20 20 20 20 6f 6b 54 6f      }.      okTo
2a2ef 43 68 6e 67 54 6f 49 4e 20 3d 20 31 3b 0a 20 20  ChngToIN = 1;.  
2a2f0 20 20 20 20 66 6f 72 28 3b 20 69 3e 3d 30 20 26      for(; i>=0 &
2a2f1 26 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 3b 20  & okToChngToIN; 
2a2f2 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b  i--, pOrTerm++){
2a2f3 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2a2f4 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
2a2f5 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20  tor==WO_EQ );.  
2a2f6 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65 72        if( pOrTer
2a2f7 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 21 3d 69  m->leftCursor!=i
2a2f8 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20  Cursor ){.      
2a2f9 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46      pOrTerm->wtF
2a2fa 6c 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52  lags &= ~TERM_OR
2a2fb 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  _OK;.        }el
2a2fc 73 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e  se if( pOrTerm->
2a2fd 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 21 3d 69 43  u.leftColumn!=iC
2a2fe 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
2a2ff 20 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20     okToChngToIN 
2a300 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 0;.        }el
2a301 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  se{.          in
2a302 74 20 61 66 66 4c 65 66 74 2c 20 61 66 66 52 69  t affLeft, affRi
2a303 67 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20 2f  ght;.          /
2a304 2a 20 49 66 20 74 68 65 20 72 69 67 68 74 2d 68  * If the right-h
2a305 61 6e 64 20 73 69 64 65 20 69 73 20 61 6c 73 6f  and side is also
2a306 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20   a column, then 
2a307 74 68 65 20 61 66 66 69 6e 69 74 69 65 73 0a 20  the affinities. 
2a308 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 62           ** of b
2a309 6f 74 68 20 72 69 67 68 74 20 61 6e 64 20 6c 65  oth right and le
2a30a 66 74 20 73 69 64 65 73 20 6d 75 73 74 20 62 65  ft sides must be
2a30b 20 73 75 63 68 20 74 68 61 74 20 6e 6f 20 74 79   such that no ty
2a30c 70 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  pe.          ** 
2a30d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 72 65 20  conversions are 
2a30e 72 65 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20  required on the 
2a30f 72 69 67 68 74 2e 20 20 28 54 69 63 6b 65 74 20  right.  (Ticket 
2a310 23 32 32 34 39 29 0a 20 20 20 20 20 20 20 20 20  #2249).         
2a311 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 66   */.          af
2a312 66 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33  fRight = sqlite3
2a313 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4f 72  ExprAffinity(pOr
2a314 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69  Term->pExpr->pRi
2a315 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ght);.          
2a316 61 66 66 4c 65 66 74 20 3d 20 73 71 6c 69 74 65  affLeft = sqlite
2a317 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4f  3ExprAffinity(pO
2a318 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c  rTerm->pExpr->pL
2a319 65 66 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  eft);.          
2a31a 69 66 28 20 61 66 66 52 69 67 68 74 21 3d 30 20  if( affRight!=0 
2a31b 26 26 20 61 66 66 52 69 67 68 74 21 3d 61 66 66  && affRight!=aff
2a31c 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Left ){.        
2a31d 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e      okToChngToIN
2a31e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
2a31f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2a320 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c     pOrTerm->wtFl
2a321 61 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52 5f 4f  ags |= TERM_OR_O
2a322 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  K;.          }. 
2a323 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2a324 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
2a325 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 6f 6b  t this point, ok
2a326 54 6f 43 68 6e 67 54 6f 49 4e 20 69 73 20 74 72  ToChngToIN is tr
2a327 75 65 20 69 66 20 6f 72 69 67 69 6e 61 6c 20 70  ue if original p
2a328 54 65 72 6d 20 73 61 74 69 73 66 69 65 73 0a 20  Term satisfies. 
2a329 20 20 20 2a 2a 20 63 61 73 65 20 31 2e 20 20 49     ** case 1.  I
2a32a 6e 20 74 68 61 74 20 63 61 73 65 2c 20 63 6f 6e  n that case, con
2a32b 73 74 72 75 63 74 20 61 20 6e 65 77 20 76 69 72  struct a new vir
2a32c 74 75 61 6c 20 74 65 72 6d 20 74 68 61 74 20 69  tual term that i
2a32d 73 20 0a 20 20 20 20 2a 2a 20 70 54 65 72 6d 20  s .    ** pTerm 
2a32e 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61  converted into a
2a32f 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 2e 0a 20  n IN operator.. 
2a330 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 6b     */.    if( ok
2a331 54 6f 43 68 6e 67 54 6f 49 4e 20 29 7b 0a 20 20  ToChngToIN ){.  
2a332 20 20 20 20 45 78 70 72 20 2a 70 44 75 70 3b 20      Expr *pDup; 
2a333 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
2a334 74 72 61 6e 73 69 65 6e 74 20 64 75 70 6c 69 63  transient duplic
2a335 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  ate expression *
2a336 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74  /.      ExprList
2a337 20 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 2f   *pList = 0;   /
2a338 2a 20 54 68 65 20 52 48 53 20 6f 66 20 74 68 65  * The RHS of the
2a339 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a   IN operator */.
2a33a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66        Expr *pLef
2a33b 74 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  t = 0;       /* 
2a33c 54 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20 49  The LHS of the I
2a33d 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  N operator */.  
2a33e 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 20      Expr *pNew; 
2a33f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2a340 65 20 63 6f 6d 70 6c 65 74 65 20 49 4e 20 6f 70  e complete IN op
2a341 65 72 61 74 6f 72 20 2a 2f 0a 0a 20 20 20 20 20  erator */..     
2a342 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54   for(i=pOrWc->nT
2a343 65 72 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 70  erm-1, pOrTerm=p
2a344 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69  OrWc->a; i>=0; i
2a345 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a  --, pOrTerm++){.
2a346 20 20 20 20 20 20 20 20 69 66 28 20 28 70 4f 72          if( (pOr
2a347 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
2a348 54 45 52 4d 5f 4f 52 5f 4f 4b 29 3d 3d 30 20 29  TERM_OR_OK)==0 )
2a349 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2a34a 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65     assert( pOrTe
2a34b 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
2a34c 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 20 20  O_EQ );.        
2a34d 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d  assert( pOrTerm-
2a34e 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
2a34f 72 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 20 20  rsor );.        
2a350 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d  assert( pOrTerm-
2a351 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69  >u.leftColumn==i
2a352 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20  Column );.      
2a353 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
2a354 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 54  ExprDup(db, pOrT
2a355 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67  erm->pExpr->pRig
2a356 68 74 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 69  ht);.        pLi
2a357 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2a358 4c 69 73 74 41 70 70 65 6e 64 28 70 57 43 2d 3e  ListAppend(pWC->
2a359 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 70  pParse, pList, p
2a35a 44 75 70 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Dup, 0);.       
2a35b 20 70 4c 65 66 74 20 3d 20 70 4f 72 54 65 72 6d   pLeft = pOrTerm
2a35c 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a  ->pExpr->pLeft;.
2a35d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
2a35e 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30 20 29  sert( pLeft!=0 )
2a35f 3b 0a 20 20 20 20 20 20 70 44 75 70 20 3d 20 73  ;.      pDup = s
2a360 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2a361 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  , pLeft);.      
2a362 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78  pNew = sqlite3Ex
2a363 70 72 28 64 62 2c 20 54 4b 5f 49 4e 2c 20 70 44  pr(db, TK_IN, pD
2a364 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  up, 0, 0);.     
2a365 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
2a366 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b       int idxNew;
2a367 0a 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65  .        transfe
2a368 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 70 4e  rJoinMarkings(pN
2a369 65 77 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  ew, pExpr);.    
2a36a 20 20 20 20 70 4e 65 77 2d 3e 70 4c 69 73 74 20      pNew->pList 
2a36b 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20  = pList;.       
2a36c 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43   idxNew = whereC
2a36d 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
2a36e 20 70 4e 65 77 2c 20 54 45 52 4d 5f 56 49 52 54   pNew, TERM_VIRT
2a36f 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43  UAL|TERM_DYNAMIC
2a370 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
2a371 61 73 65 28 20 69 64 78 4e 65 77 3d 3d 30 20 29  ase( idxNew==0 )
2a372 3b 0a 20 20 20 20 20 20 20 20 65 78 70 72 41 6e  ;.        exprAn
2a373 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c  alyze(pSrc, pWC,
2a374 20 69 64 78 4e 65 77 29 3b 0a 20 20 20 20 20 20   idxNew);.      
2a375 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e    pTerm = &pWC->
2a376 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20  a[idxTerm];.    
2a377 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65      pWC->a[idxNe
2a378 77 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78  w].iParent = idx
2a379 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54  Term;.        pT
2a37a 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b  erm->nChild = 1;
2a37b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2a37c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2a37d 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
2a37e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a  pList);.      }.
2a37f 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70        pTerm->eOp
2a380 65 72 61 74 6f 72 20 3d 20 30 3b 20 20 2f 2a 20  erator = 0;  /* 
2a381 63 61 73 65 20 31 20 74 72 75 6d 70 73 20 63 61  case 1 trumps ca
2a382 73 65 20 32 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  se 2 */.    }.  
2a383 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  }.}.#endif /* !S
2a384 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
2a385 54 49 4d 49 5a 41 54 49 4f 4e 20 26 26 20 21 53  TIMIZATION && !S
2a386 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2a387 45 52 59 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  ERY */.../*.** T
2a388 68 65 20 69 6e 70 75 74 20 74 6f 20 74 68 69 73  he input to this
2a389 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 57   routine is an W
2a38a 68 65 72 65 54 65 72 6d 20 73 74 72 75 63 74 75  hereTerm structu
2a38b 72 65 20 77 69 74 68 20 6f 6e 6c 79 20 74 68 65  re with only the
2a38c 0a 2a 2a 20 22 70 45 78 70 72 22 20 66 69 65 6c  .** "pExpr" fiel
2a38d 64 20 66 69 6c 6c 65 64 20 69 6e 2e 20 20 54 68  d filled in.  Th
2a38e 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f  e job of this ro
2a38f 75 74 69 6e 65 20 69 73 20 74 6f 20 61 6e 61 6c  utine is to anal
2a390 79 7a 65 20 74 68 65 0a 2a 2a 20 73 75 62 65 78  yze the.** subex
2a391 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 6f 70  pression and pop
2a392 75 6c 61 74 65 20 61 6c 6c 20 74 68 65 20 6f 74  ulate all the ot
2a393 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20 74 68  her fields of th
2a394 65 20 57 68 65 72 65 54 65 72 6d 0a 2a 2a 20 73  e WhereTerm.** s
2a395 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
2a396 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
2a397 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  n is of the form
2a398 20 22 3c 65 78 70 72 3e 20 3c 6f 70 3e 20 58 22   "<expr> <op> X"
2a399 20 69 74 20 67 65 74 73 20 63 6f 6d 6d 75 74 65   it gets commute
2a39a 64 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 6e  d.** to the stan
2a39b 64 61 72 64 20 66 6f 72 6d 20 6f 66 20 22 58 20  dard form of "X 
2a39c 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 2e 0a 2a 2a  <op> <expr>"..**
2a39d 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
2a39e 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20  ssion is of the 
2a39f 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 59 22 20  form "X <op> Y" 
2a3a0 77 68 65 72 65 20 62 6f 74 68 20 58 20 61 6e 64  where both X and
2a3a1 20 59 20 61 72 65 0a 2a 2a 20 63 6f 6c 75 6d 6e   Y are.** column
2a3a2 73 2c 20 74 68 65 6e 20 74 68 65 20 6f 72 69 67  s, then the orig
2a3a3 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20  inal expression 
2a3a4 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64  is unchanged and
2a3a5 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 0a 2a   a new virtual.*
2a3a6 2a 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f  * term of the fo
2a3a7 72 6d 20 22 59 20 3c 6f 70 3e 20 58 22 20 69 73  rm "Y <op> X" is
2a3a8 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 57 48   added to the WH
2a3a9 45 52 45 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a  ERE clause and.*
2a3aa 2a 20 61 6e 61 6c 79 7a 65 64 20 73 65 70 61 72  * analyzed separ
2a3ab 61 74 65 6c 79 2e 20 20 54 68 65 20 6f 72 69 67  ately.  The orig
2a3ac 69 6e 61 6c 20 74 65 72 6d 20 69 73 20 6d 61 72  inal term is mar
2a3ad 6b 65 64 20 77 69 74 68 20 54 45 52 4d 5f 43 4f  ked with TERM_CO
2a3ae 50 49 45 44 0a 2a 2a 20 61 6e 64 20 74 68 65 20  PIED.** and the 
2a3af 6e 65 77 20 74 65 72 6d 20 69 73 20 6d 61 72 6b  new term is mark
2a3b0 65 64 20 77 69 74 68 20 54 45 52 4d 5f 44 59 4e  ed with TERM_DYN
2a3b1 41 4d 49 43 20 28 62 65 63 61 75 73 65 20 69 74  AMIC (because it
2a3b2 27 73 20 70 45 78 70 72 0a 2a 2a 20 6e 65 65 64  's pExpr.** need
2a3b3 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 77 69  s to be freed wi
2a3b4 74 68 20 74 68 65 20 57 68 65 72 65 43 6c 61 75  th the WhereClau
2a3b5 73 65 29 20 61 6e 64 20 54 45 52 4d 5f 56 49 52  se) and TERM_VIR
2a3b6 54 55 41 4c 20 28 62 65 63 61 75 73 65 20 69 74  TUAL (because it
2a3b7 0a 2a 2a 20 69 73 20 61 20 63 6f 6d 6d 75 74 65  .** is a commute
2a3b8 64 20 63 6f 70 79 20 6f 66 20 61 20 70 72 69 6f  d copy of a prio
2a3b9 72 20 74 65 72 6d 2e 29 20 20 54 68 65 20 6f 72  r term.)  The or
2a3ba 69 67 69 6e 61 6c 20 74 65 72 6d 20 68 61 73 20  iginal term has 
2a3bb 6e 43 68 69 6c 64 3d 31 0a 2a 2a 20 61 6e 64 20  nChild=1.** and 
2a3bc 74 68 65 20 63 6f 70 79 20 68 61 73 20 69 64 78  the copy has idx
2a3bd 50 61 72 65 6e 74 20 73 65 74 20 74 6f 20 74 68  Parent set to th
2a3be 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6f  e index of the o
2a3bf 72 69 67 69 6e 61 6c 20 74 65 72 6d 2e 0a 2a 2f  riginal term..*/
2a3c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70  .static void exp
2a3c1 72 41 6e 61 6c 79 7a 65 28 0a 20 20 53 72 63 4c  rAnalyze(.  SrcL
2a3c2 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
2a3c3 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f        /* the FRO
2a3c4 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68  M clause */.  Wh
2a3c5 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2a3c6 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 57          /* the W
2a3c7 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2a3c8 20 69 6e 74 20 69 64 78 54 65 72 6d 20 20 20 20   int idxTerm    
2a3c9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2a3ca 64 65 78 20 6f 66 20 74 68 65 20 74 65 72 6d 20  dex of the term 
2a3cb 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a  to be analyzed *
2a3cc 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d  /.){.  WhereTerm
2a3cd 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
2a3ce 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
2a3cf 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  erm to be analyz
2a3d0 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73  ed */.  WhereMas
2a3d1 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20  kSet *pMaskSet; 
2a3d2 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20           /* Set 
2a3d3 6f 66 20 74 61 62 6c 65 20 69 6e 64 65 78 20 6d  of table index m
2a3d4 61 73 6b 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a  asks */.  Expr *
2a3d5 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  pExpr;          
2a3d6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2a3d7 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
2a3d8 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20  be analyzed */. 
2a3d9 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 4c   Bitmask prereqL
2a3da 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  eft;            
2a3db 20 20 2f 2a 20 50 72 65 72 65 71 75 65 73 69 74    /* Prerequesit
2a3dc 65 73 20 6f 66 20 74 68 65 20 70 45 78 70 72 2d  es of the pExpr-
2a3dd 3e 70 4c 65 66 74 20 2a 2f 0a 20 20 42 69 74 6d  >pLeft */.  Bitm
2a3de 61 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20 20  ask prereqAll;  
2a3df 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a3e0 50 72 65 72 65 71 75 65 73 69 74 65 73 20 6f 66  Prerequesites of
2a3e1 20 70 45 78 70 72 20 2a 2f 0a 20 20 42 69 74 6d   pExpr */.  Bitm
2a3e2 61 73 6b 20 65 78 74 72 61 52 69 67 68 74 20 3d  ask extraRight =
2a3e3 20 30 3b 0a 20 20 69 6e 74 20 6e 50 61 74 74 65   0;.  int nPatte
2a3e4 72 6e 3b 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70  rn;.  int isComp
2a3e5 6c 65 74 65 3b 0a 20 20 69 6e 74 20 6e 6f 43 61  lete;.  int noCa
2a3e6 73 65 3b 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20  se;.  int op;   
2a3e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a3e8 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 2d 6c 65         /* Top-le
2a3e9 76 65 6c 20 6f 70 65 72 61 74 6f 72 2e 20 20 70  vel operator.  p
2a3ea 45 78 70 72 2d 3e 6f 70 20 2a 2f 0a 20 20 50 61  Expr->op */.  Pa
2a3eb 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57  rse *pParse = pW
2a3ec 43 2d 3e 70 50 61 72 73 65 3b 20 20 20 20 20 2f  C->pParse;     /
2a3ed 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
2a3ee 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  t */.  sqlite3 *
2a3ef 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2a3f0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2a3f1 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2a3f2 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  /..  if( db->mal
2a3f3 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2a3f4 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70   return;.  }.  p
2a3f5 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69  Term = &pWC->a[i
2a3f6 64 78 54 65 72 6d 5d 3b 0a 20 20 70 4d 61 73 6b  dxTerm];.  pMask
2a3f7 53 65 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b  Set = pWC->pMask
2a3f8 53 65 74 3b 0a 20 20 70 45 78 70 72 20 3d 20 70  Set;.  pExpr = p
2a3f9 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 70  Term->pExpr;.  p
2a3fa 72 65 72 65 71 4c 65 66 74 20 3d 20 65 78 70 72  rereqLeft = expr
2a3fb 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2a3fc 53 65 74 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  Set, pExpr->pLef
2a3fd 74 29 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72  t);.  op = pExpr
2a3fe 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  ->op;.  if( op==
2a3ff 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 61 73 73  TK_IN ){.    ass
2a400 65 72 74 28 20 70 45 78 70 72 2d 3e 70 52 69 67  ert( pExpr->pRig
2a401 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 70 54 65  ht==0 );.    pTe
2a402 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20  rm->prereqRight 
2a403 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55  = exprListTableU
2a404 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2a405 45 78 70 72 2d 3e 70 4c 69 73 74 29 0a 20 20 20  Expr->pList).   
2a406 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a407 20 20 20 20 20 20 20 7c 20 65 78 70 72 53 65 6c         | exprSel
2a408 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  ectTableUsage(pM
2a409 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 70  askSet, pExpr->p
2a40a 53 65 6c 65 63 74 29 3b 0a 20 20 7d 65 6c 73 65  Select);.  }else
2a40b 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55   if( op==TK_ISNU
2a40c 4c 4c 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d  LL ){.    pTerm-
2a40d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 30  >prereqRight = 0
2a40e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
2a40f 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  Term->prereqRigh
2a410 74 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  t = exprTableUsa
2a411 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78  ge(pMaskSet, pEx
2a412 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d  pr->pRight);.  }
2a413 0a 20 20 70 72 65 72 65 71 41 6c 6c 20 3d 20 65  .  prereqAll = e
2a414 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
2a415 61 73 6b 53 65 74 2c 20 70 45 78 70 72 29 3b 0a  askSet, pExpr);.
2a416 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
2a417 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
2a418 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20  FromJoin) ){.   
2a419 20 42 69 74 6d 61 73 6b 20 78 20 3d 20 67 65 74   Bitmask x = get
2a41a 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70  Mask(pMaskSet, p
2a41b 45 78 70 72 2d 3e 69 52 69 67 68 74 4a 6f 69 6e  Expr->iRightJoin
2a41c 54 61 62 6c 65 29 3b 0a 20 20 20 20 70 72 65 72  Table);.    prer
2a41d 65 71 41 6c 6c 20 7c 3d 20 78 3b 0a 20 20 20 20  eqAll |= x;.    
2a41e 65 78 74 72 61 52 69 67 68 74 20 3d 20 78 2d 31  extraRight = x-1
2a41f 3b 20 20 2f 2a 20 4f 4e 20 63 6c 61 75 73 65 20  ;  /* ON clause 
2a420 74 65 72 6d 73 20 6d 61 79 20 6e 6f 74 20 62 65  terms may not be
2a421 20 75 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e   used with an in
2a422 64 65 78 0a 20 20 20 20 20 20 20 20 20 20 20 20  dex.            
2a423 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e             ** on
2a424 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20 61   left table of a
2a425 20 4c 45 46 54 20 4a 4f 49 4e 2e 20 20 54 69 63   LEFT JOIN.  Tic
2a426 6b 65 74 20 23 33 30 31 35 20 2a 2f 0a 20 20 7d  ket #3015 */.  }
2a427 0a 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  .  pTerm->prereq
2a428 41 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b  All = prereqAll;
2a429 0a 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75  .  pTerm->leftCu
2a42a 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 70 54 65  rsor = -1;.  pTe
2a42b 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31  rm->iParent = -1
2a42c 3b 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  ;.  pTerm->eOper
2a42d 61 74 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 20  ator = 0;.  if( 
2a42e 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 26 26  allowedOp(op) &&
2a42f 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52   (pTerm->prereqR
2a430 69 67 68 74 20 26 20 70 72 65 72 65 71 4c 65 66  ight & prereqLef
2a431 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70  t)==0 ){.    Exp
2a432 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72  r *pLeft = pExpr
2a433 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 45 78 70  ->pLeft;.    Exp
2a434 72 20 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70  r *pRight = pExp
2a435 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 69  r->pRight;.    i
2a436 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  f( pLeft->op==TK
2a437 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20  _COLUMN ){.     
2a438 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73   pTerm->leftCurs
2a439 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62  or = pLeft->iTab
2a43a 6c 65 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  le;.      pTerm-
2a43b 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20  >u.leftColumn = 
2a43c 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  pLeft->iColumn;.
2a43d 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70        pTerm->eOp
2a43e 65 72 61 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f  erator = operato
2a43f 72 4d 61 73 6b 28 6f 70 29 3b 0a 20 20 20 20 7d  rMask(op);.    }
2a440 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 20  .    if( pRight 
2a441 26 26 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54  && pRight->op==T
2a442 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20  K_COLUMN ){.    
2a443 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4e 65    WhereTerm *pNe
2a444 77 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  w;.      Expr *p
2a445 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70  Dup;.      if( p
2a446 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
2a447 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  >=0 ){.        i
2a448 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20  nt idxNew;.     
2a449 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
2a44a 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 78  3ExprDup(db, pEx
2a44b 70 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  pr);.        if(
2a44c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2a44d 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  d ){.          s
2a44e 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2a44f 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20  (db, pDup);.    
2a450 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2a451 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2a452 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c  idxNew = whereCl
2a453 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
2a454 70 44 75 70 2c 20 54 45 52 4d 5f 56 49 52 54 55  pDup, TERM_VIRTU
2a455 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29  AL|TERM_DYNAMIC)
2a456 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 64  ;.        if( id
2a457 78 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  xNew==0 ) return
2a458 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d  ;.        pNew =
2a459 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d   &pWC->a[idxNew]
2a45a 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
2a45b 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72  iParent = idxTer
2a45c 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d  m;.        pTerm
2a45d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
2a45e 72 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  rm];.        pTe
2a45f 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a  rm->nChild = 1;.
2a460 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77          pTerm->w
2a461 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43  tFlags |= TERM_C
2a462 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 7d 65 6c  OPIED;.      }el
2a463 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44 75 70  se{.        pDup
2a464 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20   = pExpr;.      
2a465 20 20 70 4e 65 77 20 3d 20 70 54 65 72 6d 3b 0a    pNew = pTerm;.
2a466 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 78        }.      ex
2a467 70 72 43 6f 6d 6d 75 74 65 28 70 50 61 72 73 65  prCommute(pParse
2a468 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 70  , pDup);.      p
2a469 4c 65 66 74 20 3d 20 70 44 75 70 2d 3e 70 4c 65  Left = pDup->pLe
2a46a 66 74 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  ft;.      pNew->
2a46b 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65  leftCursor = pLe
2a46c 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20  ft->iTable;.    
2a46d 20 20 70 4e 65 77 2d 3e 75 2e 6c 65 66 74 43 6f    pNew->u.leftCo
2a46e 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43  lumn = pLeft->iC
2a46f 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65  olumn;.      pNe
2a470 77 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d  w->prereqRight =
2a471 20 70 72 65 72 65 71 4c 65 66 74 3b 0a 20 20 20   prereqLeft;.   
2a472 20 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 41     pNew->prereqA
2a473 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a  ll = prereqAll;.
2a474 20 20 20 20 20 20 70 4e 65 77 2d 3e 65 4f 70 65        pNew->eOpe
2a475 72 61 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72  rator = operator
2a476 4d 61 73 6b 28 70 44 75 70 2d 3e 6f 70 29 3b 0a  Mask(pDup->op);.
2a477 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 6e 64      }.  }..#ifnd
2a478 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
2a479 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54  ETWEEN_OPTIMIZAT
2a47a 49 4f 4e 0a 20 20 2f 2a 20 49 66 20 61 20 74 65  ION.  /* If a te
2a47b 72 6d 20 69 73 20 74 68 65 20 42 45 54 57 45 45  rm is the BETWEE
2a47c 4e 20 6f 70 65 72 61 74 6f 72 2c 20 63 72 65 61  N operator, crea
2a47d 74 65 20 74 77 6f 20 6e 65 77 20 76 69 72 74 75  te two new virtu
2a47e 61 6c 20 74 65 72 6d 73 0a 20 20 2a 2a 20 74 68  al terms.  ** th
2a47f 61 74 20 64 65 66 69 6e 65 20 74 68 65 20 72 61  at define the ra
2a480 6e 67 65 20 74 68 61 74 20 74 68 65 20 42 45 54  nge that the BET
2a481 57 45 45 4e 20 69 6d 70 6c 65 6d 65 6e 74 73 2e  WEEN implements.
2a482 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20    For example:. 
2a483 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 61 20   **.  **      a 
2a484 42 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63 0a  BETWEEN b AND c.
2a485 20 20 2a 2a 0a 20 20 2a 2a 20 69 73 20 63 6f 6e    **.  ** is con
2a486 76 65 72 74 65 64 20 69 6e 74 6f 3a 0a 20 20 2a  verted into:.  *
2a487 2a 0a 20 20 2a 2a 20 20 20 20 20 20 28 61 20 42  *.  **      (a B
2a488 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63 29 20  ETWEEN b AND c) 
2a489 41 4e 44 20 28 61 3e 3d 62 29 20 41 4e 44 20 28  AND (a>=b) AND (
2a48a 61 3c 3d 63 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20  a<=c).  **.  ** 
2a48b 54 68 65 20 74 77 6f 20 6e 65 77 20 74 65 72 6d  The two new term
2a48c 73 20 61 72 65 20 61 64 64 65 64 20 6f 6e 74 6f  s are added onto
2a48d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
2a48e 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65  WhereClause obje
2a48f 63 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 65 77  ct..  ** The new
2a490 20 74 65 72 6d 73 20 61 72 65 20 22 64 79 6e 61   terms are "dyna
2a491 6d 69 63 22 20 61 6e 64 20 61 72 65 20 63 68 69  mic" and are chi
2a492 6c 64 72 65 6e 20 6f 66 20 74 68 65 20 6f 72 69  ldren of the ori
2a493 67 69 6e 61 6c 20 42 45 54 57 45 45 4e 0a 20 20  ginal BETWEEN.  
2a494 2a 2a 20 74 65 72 6d 2e 20 20 54 68 61 74 20 6d  ** term.  That m
2a495 65 61 6e 73 20 74 68 61 74 20 69 66 20 74 68 65  eans that if the
2a496 20 42 45 54 57 45 45 4e 20 74 65 72 6d 20 69 73   BETWEEN term is
2a497 20 63 6f 64 65 64 2c 20 74 68 65 20 63 68 69 6c   coded, the chil
2a498 64 72 65 6e 20 61 72 65 0a 20 20 2a 2a 20 73 6b  dren are.  ** sk
2a499 69 70 70 65 64 2e 20 20 4f 72 2c 20 69 66 20 74  ipped.  Or, if t
2a49a 68 65 20 63 68 69 6c 64 72 65 6e 20 61 72 65 20  he children are 
2a49b 73 61 74 69 73 66 69 65 64 20 62 79 20 61 6e 20  satisfied by an 
2a49c 69 6e 64 65 78 2c 20 74 68 65 20 6f 72 69 67 69  index, the origi
2a49d 6e 61 6c 0a 20 20 2a 2a 20 42 45 54 57 45 45 4e  nal.  ** BETWEEN
2a49e 20 74 65 72 6d 20 69 73 20 73 6b 69 70 70 65 64   term is skipped
2a49f 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ..  */.  else if
2a4a0 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2a4a1 42 45 54 57 45 45 4e 20 26 26 20 70 57 43 2d 3e  BETWEEN && pWC->
2a4a2 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 7b 0a 20 20  op==TK_AND ){.  
2a4a3 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2a4a4 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  t = pExpr->pList
2a4a5 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
2a4a6 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
2a4a7 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b 5f 47 45 2c   ops[] = {TK_GE,
2a4a8 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20 20 61 73 73   TK_LE};.    ass
2a4a9 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b  ert( pList!=0 );
2a4aa 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69  .    assert( pLi
2a4ab 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 20 29 3b 0a  st->nExpr==2 );.
2a4ac 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32      for(i=0; i<2
2a4ad 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78  ; i++){.      Ex
2a4ae 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20 20  pr *pNewExpr;.  
2a4af 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a      int idxNew;.
2a4b0 20 20 20 20 20 20 70 4e 65 77 45 78 70 72 20 3d        pNewExpr =
2a4b1 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
2a4b2 20 6f 70 73 5b 69 5d 2c 20 73 71 6c 69 74 65 33   ops[i], sqlite3
2a4b3 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 78 70  ExprDup(db, pExp
2a4b4 72 2d 3e 70 4c 65 66 74 29 2c 0a 20 20 20 20 20  r->pLeft),.     
2a4b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a4b6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2a4b7 78 70 72 44 75 70 28 64 62 2c 20 70 4c 69 73 74  xprDup(db, pList
2a4b8 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 2c 20 30  ->a[i].pExpr), 0
2a4b9 29 3b 0a 20 20 20 20 20 20 69 64 78 4e 65 77 20  );.      idxNew 
2a4ba 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
2a4bb 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70  ert(pWC, pNewExp
2a4bc 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c  r, TERM_VIRTUAL|
2a4bd 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  TERM_DYNAMIC);. 
2a4be 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2a4bf 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20  dxNew==0 );.    
2a4c0 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53    exprAnalyze(pS
2a4c1 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 29  rc, pWC, idxNew)
2a4c2 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20  ;.      pTerm = 
2a4c3 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
2a4c4 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69  ;.      pWC->a[i
2a4c5 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20 3d  dxNew].iParent =
2a4c6 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 7d 0a   idxTerm;.    }.
2a4c7 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c      pTerm->nChil
2a4c8 64 20 3d 20 32 3b 0a 20 20 7d 0a 23 65 6e 64 69  d = 2;.  }.#endi
2a4c9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2a4ca 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a  _BETWEEN_OPTIMIZ
2a4cb 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 20 21 64  ATION */..#if !d
2a4cc 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2a4cd 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49  IT_OR_OPTIMIZATI
2a4ce 4f 4e 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  ON) && !defined(
2a4cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2a4d0 55 45 52 59 29 0a 20 20 2f 2a 20 41 6e 61 6c 79  UERY).  /* Analy
2a4d1 7a 65 20 61 20 74 65 72 6d 20 74 68 61 74 20 69  ze a term that i
2a4d2 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 74 77  s composed of tw
2a4d3 6f 20 6f 72 20 6d 6f 72 65 20 73 75 62 74 65 72  o or more subter
2a4d4 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a  ms connected by.
2a4d5 20 20 2a 2a 20 61 6e 20 4f 52 20 6f 70 65 72 61    ** an OR opera
2a4d6 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65  tor..  */.  else
2a4d7 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d   if( pExpr->op==
2a4d8 54 4b 5f 4f 52 20 29 7b 0a 20 20 20 20 61 73 73  TK_OR ){.    ass
2a4d9 65 72 74 28 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b  ert( pWC->op==TK
2a4da 5f 41 4e 44 20 29 3b 0a 20 20 20 20 65 78 70 72  _AND );.    expr
2a4db 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28 70 53  AnalyzeOrTerm(pS
2a4dc 72 63 2c 20 70 57 43 2c 20 69 64 78 54 65 72 6d  rc, pWC, idxTerm
2a4dd 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  );.  }.#endif /*
2a4de 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f   SQLITE_OMIT_OR_
2a4df 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a  OPTIMIZATION */.
2a4e0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2a4e1 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49  OMIT_LIKE_OPTIMI
2a4e2 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 41 64 64 20  ZATION.  /* Add 
2a4e3 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 72  constraints to r
2a4e4 65 64 75 63 65 20 74 68 65 20 73 65 61 72 63 68  educe the search
2a4e5 20 73 70 61 63 65 20 6f 6e 20 61 20 4c 49 4b 45   space on a LIKE
2a4e6 20 6f 72 20 47 4c 4f 42 0a 20 20 2a 2a 20 6f 70   or GLOB.  ** op
2a4e7 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 0a 20 20 2a  erator..  **.  *
2a4e8 2a 20 41 20 6c 69 6b 65 20 70 61 74 74 65 72 6e  * A like pattern
2a4e9 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 20   of the form "x 
2a4ea 4c 49 4b 45 20 27 61 62 63 25 27 22 20 69 73 20  LIKE 'abc%'" is 
2a4eb 63 68 61 6e 67 65 64 20 69 6e 74 6f 20 63 6f 6e  changed into con
2a4ec 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 0a 20 20  straints.  **.  
2a4ed 2a 2a 20 20 20 20 20 20 20 20 20 20 78 3e 3d 27  **          x>='
2a4ee 61 62 63 27 20 41 4e 44 20 78 3c 27 61 62 64 27  abc' AND x<'abd'
2a4ef 20 41 4e 44 20 78 20 4c 49 4b 45 20 27 61 62 63   AND x LIKE 'abc
2a4f0 25 27 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  %'.  **.  ** The
2a4f1 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20   last character 
2a4f2 6f 66 20 74 68 65 20 70 72 65 66 69 78 20 22 61  of the prefix "a
2a4f3 62 63 22 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  bc" is increment
2a4f4 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 0a 20  ed to form the. 
2a4f5 20 2a 2a 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20   ** termination 
2a4f6 63 6f 6e 64 69 74 69 6f 6e 20 22 61 62 64 22 2e  condition "abd".
2a4f7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 4c 69  .  */.  if( isLi
2a4f8 6b 65 4f 72 47 6c 6f 62 28 70 50 61 72 73 65 2c  keOrGlob(pParse,
2a4f9 20 70 45 78 70 72 2c 20 26 6e 50 61 74 74 65 72   pExpr, &nPatter
2a4fa 6e 2c 20 26 69 73 43 6f 6d 70 6c 65 74 65 2c 20  n, &isComplete, 
2a4fb 26 6e 6f 43 61 73 65 29 0a 20 20 20 20 20 20 20  &noCase).       
2a4fc 20 20 26 26 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b    && pWC->op==TK
2a4fd 5f 41 4e 44 20 29 7b 0a 20 20 20 20 45 78 70 72  _AND ){.    Expr
2a4fe 20 2a 70 4c 65 66 74 2c 20 2a 70 52 69 67 68 74   *pLeft, *pRight
2a4ff 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 53 74 72  ;.    Expr *pStr
2a500 31 2c 20 2a 70 53 74 72 32 3b 0a 20 20 20 20 45  1, *pStr2;.    E
2a501 78 70 72 20 2a 70 4e 65 77 45 78 70 72 31 2c 20  xpr *pNewExpr1, 
2a502 2a 70 4e 65 77 45 78 70 72 32 3b 0a 20 20 20 20  *pNewExpr2;.    
2a503 69 6e 74 20 69 64 78 4e 65 77 31 2c 20 69 64 78  int idxNew1, idx
2a504 4e 65 77 32 3b 0a 0a 20 20 20 20 70 4c 65 66 74  New2;..    pLeft
2a505 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d   = pExpr->pList-
2a506 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[1].pExpr;.   
2a507 20 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d   pRight = pExpr-
2a508 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  >pList->a[0].pEx
2a509 70 72 3b 0a 20 20 20 20 70 53 74 72 31 20 3d 20  pr;.    pStr1 = 
2a50a 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2a50b 72 73 65 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20  rse, TK_STRING, 
2a50c 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 0, 0);.    if
2a50d 28 20 70 53 74 72 31 20 29 7b 0a 20 20 20 20 20  ( pStr1 ){.     
2a50e 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70   sqlite3TokenCop
2a50f 79 28 64 62 2c 20 26 70 53 74 72 31 2d 3e 74 6f  y(db, &pStr1->to
2a510 6b 65 6e 2c 20 26 70 52 69 67 68 74 2d 3e 74 6f  ken, &pRight->to
2a511 6b 65 6e 29 3b 0a 20 20 20 20 20 20 70 53 74 72  ken);.      pStr
2a512 31 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d 20 6e 50 61  1->token.n = nPa
2a513 74 74 65 72 6e 3b 0a 20 20 20 20 20 20 70 53 74  ttern;.      pSt
2a514 72 31 2d 3e 66 6c 61 67 73 20 3d 20 45 50 5f 44  r1->flags = EP_D
2a515 65 71 75 6f 74 65 64 3b 0a 20 20 20 20 7d 0a 20  equoted;.    }. 
2a516 20 20 20 70 53 74 72 32 20 3d 20 73 71 6c 69 74     pStr2 = sqlit
2a517 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53  e3ExprDup(db, pS
2a518 74 72 31 29 3b 0a 20 20 20 20 69 66 28 20 21 64  tr1);.    if( !d
2a519 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2a51a 29 7b 0a 20 20 20 20 20 20 75 38 20 63 2c 20 2a  ){.      u8 c, *
2a51b 70 43 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  pC;.      assert
2a51c 28 20 70 53 74 72 32 2d 3e 74 6f 6b 65 6e 2e 64  ( pStr2->token.d
2a51d 79 6e 20 29 3b 0a 20 20 20 20 20 20 70 43 20 3d  yn );.      pC =
2a51e 20 28 75 38 2a 29 26 70 53 74 72 32 2d 3e 74 6f   (u8*)&pStr2->to
2a51f 6b 65 6e 2e 7a 5b 6e 50 61 74 74 65 72 6e 2d 31  ken.z[nPattern-1
2a520 5d 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 70 43  ];.      c = *pC
2a521 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61  ;.      if( noCa
2a522 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  se ){.        if
2a523 28 20 63 3d 3d 27 40 27 20 29 20 69 73 43 6f 6d  ( c=='@' ) isCom
2a524 70 6c 65 74 65 20 3d 20 30 3b 0a 20 20 20 20 20  plete = 0;.     
2a525 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 70     c = sqlite3Up
2a526 70 65 72 54 6f 4c 6f 77 65 72 5b 63 5d 3b 0a 20  perToLower[c];. 
2a527 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 43       }.      *pC
2a528 20 3d 20 63 20 2b 20 31 3b 0a 20 20 20 20 7d 0a   = c + 1;.    }.
2a529 20 20 20 20 70 4e 65 77 45 78 70 72 31 20 3d 20      pNewExpr1 = 
2a52a 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2a52b 72 73 65 2c 20 54 4b 5f 47 45 2c 20 73 71 6c 69  rse, TK_GE, sqli
2a52c 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 70 4c  te3ExprDup(db,pL
2a52d 65 66 74 29 2c 20 70 53 74 72 31 2c 20 30 29 3b  eft), pStr1, 0);
2a52e 0a 20 20 20 20 69 64 78 4e 65 77 31 20 3d 20 77  .    idxNew1 = w
2a52f 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74  hereClauseInsert
2a530 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 31 2c  (pWC, pNewExpr1,
2a531 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45   TERM_VIRTUAL|TE
2a532 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  RM_DYNAMIC);.   
2a533 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65   testcase( idxNe
2a534 77 31 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78 70  w1==0 );.    exp
2a535 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70  rAnalyze(pSrc, p
2a536 57 43 2c 20 69 64 78 4e 65 77 31 29 3b 0a 20 20  WC, idxNew1);.  
2a537 20 20 70 4e 65 77 45 78 70 72 32 20 3d 20 73 71    pNewExpr2 = sq
2a538 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2a539 65 2c 20 54 4b 5f 4c 54 2c 20 73 71 6c 69 74 65  e, TK_LT, sqlite
2a53a 33 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66  3ExprDup(db,pLef
2a53b 74 29 2c 20 70 53 74 72 32 2c 20 30 29 3b 0a 20  t), pStr2, 0);. 
2a53c 20 20 20 69 64 78 4e 65 77 32 20 3d 20 77 68 65     idxNew2 = whe
2a53d 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70  reClauseInsert(p
2a53e 57 43 2c 20 70 4e 65 77 45 78 70 72 32 2c 20 54  WC, pNewExpr2, T
2a53f 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d  ERM_VIRTUAL|TERM
2a540 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74  _DYNAMIC);.    t
2a541 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 32  estcase( idxNew2
2a542 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41  ==0 );.    exprA
2a543 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43  nalyze(pSrc, pWC
2a544 2c 20 69 64 78 4e 65 77 32 29 3b 0a 20 20 20 20  , idxNew2);.    
2a545 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
2a546 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 69 66  idxTerm];.    if
2a547 28 20 69 73 43 6f 6d 70 6c 65 74 65 20 29 7b 0a  ( isComplete ){.
2a548 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78        pWC->a[idx
2a549 4e 65 77 31 5d 2e 69 50 61 72 65 6e 74 20 3d 20  New1].iParent = 
2a54a 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70  idxTerm;.      p
2a54b 57 43 2d 3e 61 5b 69 64 78 4e 65 77 32 5d 2e 69  WC->a[idxNew2].i
2a54c 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d  Parent = idxTerm
2a54d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e  ;.      pTerm->n
2a54e 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20 20 20 7d  Child = 2;.    }
2a54f 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
2a550 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f  QLITE_OMIT_LIKE_
2a551 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a  OPTIMIZATION */.
2a552 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2a553 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2a554 45 0a 20 20 2f 2a 20 41 64 64 20 61 20 57 4f 5f  E.  /* Add a WO_
2a555 4d 41 54 43 48 20 61 75 78 69 6c 69 61 72 79 20  MATCH auxiliary 
2a556 74 65 72 6d 20 74 6f 20 74 68 65 20 63 6f 6e 73  term to the cons
2a557 74 72 61 69 6e 74 20 73 65 74 20 69 66 20 74 68  traint set if th
2a558 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 65  e.  ** current e
2a559 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20  xpression is of 
2a55a 74 68 65 20 66 6f 72 6d 3a 20 20 63 6f 6c 75 6d  the form:  colum
2a55b 6e 20 4d 41 54 43 48 20 65 78 70 72 2e 0a 20 20  n MATCH expr..  
2a55c 2a 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74  ** This informat
2a55d 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74  ion is used by t
2a55e 68 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65  he xBestIndex me
2a55f 74 68 6f 64 73 20 6f 66 0a 20 20 2a 2a 20 76 69  thods of.  ** vi
2a560 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 20 20 54  rtual tables.  T
2a561 68 65 20 6e 61 74 69 76 65 20 71 75 65 72 79 20  he native query 
2a562 6f 70 74 69 6d 69 7a 65 72 20 64 6f 65 73 20 6e  optimizer does n
2a563 6f 74 20 61 74 74 65 6d 70 74 0a 20 20 2a 2a 20  ot attempt.  ** 
2a564 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 77  to do anything w
2a565 69 74 68 20 4d 41 54 43 48 20 66 75 6e 63 74 69  ith MATCH functi
2a566 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ons..  */.  if( 
2a567 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e 28  isMatchOfColumn(
2a568 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e  pExpr) ){.    in
2a569 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 45 78  t idxNew;.    Ex
2a56a 70 72 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65  pr *pRight, *pLe
2a56b 66 74 3b 0a 20 20 20 20 57 68 65 72 65 54 65 72  ft;.    WhereTer
2a56c 6d 20 2a 70 4e 65 77 54 65 72 6d 3b 0a 20 20 20  m *pNewTerm;.   
2a56d 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 43   Bitmask prereqC
2a56e 6f 6c 75 6d 6e 2c 20 70 72 65 72 65 71 45 78 70  olumn, prereqExp
2a56f 72 3b 0a 0a 20 20 20 20 70 52 69 67 68 74 20 3d  r;..    pRight =
2a570 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61   pExpr->pList->a
2a571 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70  [0].pExpr;.    p
2a572 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Left = pExpr->pL
2a573 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b  ist->a[1].pExpr;
2a574 0a 20 20 20 20 70 72 65 72 65 71 45 78 70 72 20  .    prereqExpr 
2a575 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
2a576 28 70 4d 61 73 6b 53 65 74 2c 20 70 52 69 67 68  (pMaskSet, pRigh
2a577 74 29 3b 0a 20 20 20 20 70 72 65 72 65 71 43 6f  t);.    prereqCo
2a578 6c 75 6d 6e 20 3d 20 65 78 70 72 54 61 62 6c 65  lumn = exprTable
2a579 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2a57a 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20  pLeft);.    if( 
2a57b 28 70 72 65 72 65 71 45 78 70 72 20 26 20 70 72  (prereqExpr & pr
2a57c 65 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29  ereqColumn)==0 )
2a57d 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e  {.      Expr *pN
2a57e 65 77 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4e  ewExpr;.      pN
2a57f 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  ewExpr = sqlite3
2a580 45 78 70 72 28 64 62 2c 20 54 4b 5f 4d 41 54 43  Expr(db, TK_MATC
2a581 48 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70  H, 0, sqlite3Exp
2a582 72 44 75 70 28 64 62 2c 20 70 52 69 67 68 74 29  rDup(db, pRight)
2a583 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 64 78 4e  , 0);.      idxN
2a584 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65  ew = whereClause
2a585 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77  Insert(pWC, pNew
2a586 45 78 70 72 2c 20 54 45 52 4d 5f 56 49 52 54 55  Expr, TERM_VIRTU
2a587 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29  AL|TERM_DYNAMIC)
2a588 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2a589 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20  ( idxNew==0 );. 
2a58a 20 20 20 20 20 70 4e 65 77 54 65 72 6d 20 3d 20       pNewTerm = 
2a58b 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b  &pWC->a[idxNew];
2a58c 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d  .      pNewTerm-
2a58d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 70  >prereqRight = p
2a58e 72 65 72 65 71 45 78 70 72 3b 0a 20 20 20 20 20  rereqExpr;.     
2a58f 20 70 4e 65 77 54 65 72 6d 2d 3e 6c 65 66 74 43   pNewTerm->leftC
2a590 75 72 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69  ursor = pLeft->i
2a591 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 4e 65  Table;.      pNe
2a592 77 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c  wTerm->u.leftCol
2a593 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f  umn = pLeft->iCo
2a594 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65 77  lumn;.      pNew
2a595 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2a596 3d 20 57 4f 5f 4d 41 54 43 48 3b 0a 20 20 20 20  = WO_MATCH;.    
2a597 20 20 70 4e 65 77 54 65 72 6d 2d 3e 69 50 61 72    pNewTerm->iPar
2a598 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20  ent = idxTerm;. 
2a599 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57       pTerm = &pW
2a59a 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
2a59b 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69       pTerm->nChi
2a59c 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 54  ld = 1;.      pT
2a59d 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20  erm->wtFlags |= 
2a59e 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a 20 20 20  TERM_COPIED;.   
2a59f 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72 65     pNewTerm->pre
2a5a0 72 65 71 41 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e  reqAll = pTerm->
2a5a1 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20 7d  prereqAll;.    }
2a5a2 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
2a5a3 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2a5a4 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 20 20 2f 2a  ALTABLE */..  /*
2a5a5 20 50 72 65 76 65 6e 74 20 4f 4e 20 63 6c 61 75   Prevent ON clau
2a5a6 73 65 20 74 65 72 6d 73 20 6f 66 20 61 20 4c 45  se terms of a LE
2a5a7 46 54 20 4a 4f 49 4e 20 66 72 6f 6d 20 62 65 69  FT JOIN from bei
2a5a8 6e 67 20 75 73 65 64 20 74 6f 20 64 72 69 76 65  ng used to drive
2a5a9 0a 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 66  .  ** an index f
2a5aa 6f 72 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65  or tables to the
2a5ab 20 6c 65 66 74 20 6f 66 20 74 68 65 20 6a 6f 69   left of the joi
2a5ac 6e 2e 0a 20 20 2a 2f 0a 20 20 70 54 65 72 6d 2d  n..  */.  pTerm-
2a5ad 3e 70 72 65 72 65 71 52 69 67 68 74 20 7c 3d 20  >prereqRight |= 
2a5ae 65 78 74 72 61 52 69 67 68 74 3b 0a 7d 0a 0a 2f  extraRight;.}../
2a5af 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
2a5b0 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 65   if any of the e
2a5b1 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 70 4c  xpressions in pL
2a5b2 69 73 74 2d 3e 61 5b 69 46 69 72 73 74 2e 2e 2e  ist->a[iFirst...
2a5b3 5d 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 20 72  ] contain.** a r
2a5b4 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e 79 20  eference to any 
2a5b5 74 61 62 6c 65 20 6f 74 68 65 72 20 74 68 61 6e  table other than
2a5b6 20 74 68 65 20 69 42 61 73 65 20 74 61 62 6c 65   the iBase table
2a5b7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2a5b8 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54  referencesOtherT
2a5b9 61 62 6c 65 73 28 0a 20 20 45 78 70 72 4c 69 73  ables(.  ExprLis
2a5ba 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20  t *pList,       
2a5bb 20 20 20 2f 2a 20 53 65 61 72 63 68 20 65 78 70     /* Search exp
2a5bc 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 73 20  ressions in ths 
2a5bd 6c 69 73 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d  list */.  WhereM
2a5be 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
2a5bf 2c 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66  ,   /* Mapping f
2a5c0 72 6f 6d 20 74 61 62 6c 65 73 20 74 6f 20 62 69  rom tables to bi
2a5c1 74 6d 61 70 73 20 2a 2f 0a 20 20 69 6e 74 20 69  tmaps */.  int i
2a5c2 46 69 72 73 74 2c 20 20 20 20 20 20 20 20 20 20  First,          
2a5c3 20 20 20 20 20 2f 2a 20 42 65 20 73 65 61 72 63       /* Be searc
2a5c4 68 69 6e 67 20 77 69 74 68 20 74 68 65 20 69 46  hing with the iF
2a5c5 69 72 73 74 2d 74 68 20 65 78 70 72 65 73 73 69  irst-th expressi
2a5c6 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 42 61 73  on */.  int iBas
2a5c7 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
2a5c8 20 20 2f 2a 20 49 67 6e 6f 72 65 20 72 65 66 65    /* Ignore refe
2a5c9 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 74  rences to this t
2a5ca 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 42 69 74  able */.){.  Bit
2a5cb 6d 61 73 6b 20 61 6c 6c 6f 77 65 64 20 3d 20 7e  mask allowed = ~
2a5cc 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
2a5cd 2c 20 69 42 61 73 65 29 3b 0a 20 20 77 68 69 6c  , iBase);.  whil
2a5ce 65 28 20 69 46 69 72 73 74 3c 70 4c 69 73 74 2d  e( iFirst<pList-
2a5cf 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66  >nExpr ){.    if
2a5d0 28 20 28 65 78 70 72 54 61 62 6c 65 55 73 61 67  ( (exprTableUsag
2a5d1 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c 69 73  e(pMaskSet, pLis
2a5d2 74 2d 3e 61 5b 69 46 69 72 73 74 2b 2b 5d 2e 70  t->a[iFirst++].p
2a5d3 45 78 70 72 29 26 61 6c 6c 6f 77 65 64 29 21 3d  Expr)&allowed)!=
2a5d4 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
2a5d5 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
2a5d6 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f   return 0;.}.../
2a5d7 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2a5d8 65 20 64 65 63 69 64 65 73 20 69 66 20 70 49 64  e decides if pId
2a5d9 78 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  x can be used to
2a5da 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 44   satisfy the ORD
2a5db 45 52 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 2e  ER BY.** clause.
2a5dc 20 20 49 66 20 69 74 20 63 61 6e 2c 20 69 74 20    If it can, it 
2a5dd 72 65 74 75 72 6e 73 20 31 2e 20 20 49 66 20 70  returns 1.  If p
2a5de 49 64 78 20 63 61 6e 6e 6f 74 20 73 61 74 69 73  Idx cannot satis
2a5df 66 79 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20  fy the.** ORDER 
2a5e0 42 59 20 63 6c 61 75 73 65 2c 20 74 68 69 73 20  BY clause, this 
2a5e1 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
2a5e2 30 2e 0a 2a 2a 0a 2a 2a 20 70 4f 72 64 65 72 42  0..**.** pOrderB
2a5e3 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  y is an ORDER BY
2a5e4 20 63 6c 61 75 73 65 20 66 72 6f 6d 20 61 20 53   clause from a S
2a5e5 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
2a5e6 20 20 70 54 61 62 20 69 73 20 74 68 65 0a 2a 2a    pTab is the.**
2a5e7 20 6c 65 66 74 2d 6d 6f 73 74 20 74 61 62 6c 65   left-most table
2a5e8 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
2a5e9 75 73 65 20 6f 66 20 74 68 61 74 20 73 61 6d 65  use of that same
2a5ea 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2a5eb 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 74 61 62  t and.** the tab
2a5ec 6c 65 20 68 61 73 20 61 20 63 75 72 73 6f 72 20  le has a cursor 
2a5ed 6e 75 6d 62 65 72 20 6f 66 20 22 62 61 73 65 22  number of "base"
2a5ee 2e 20 20 70 49 64 78 20 69 73 20 61 6e 20 69 6e  .  pIdx is an in
2a5ef 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a 0a  dex on pTab..**.
2a5f0 2a 2a 20 6e 45 71 43 6f 6c 20 69 73 20 74 68 65  ** nEqCol is the
2a5f1 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
2a5f2 6e 73 20 6f 66 20 70 49 64 78 20 74 68 61 74 20  ns of pIdx that 
2a5f3 61 72 65 20 75 73 65 64 20 61 73 20 65 71 75 61  are used as equa
2a5f4 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73 74 72 61 69  lity.** constrai
2a5f5 6e 74 73 2e 20 20 41 6e 79 20 6f 66 20 74 68 65  nts.  Any of the
2a5f6 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62  se columns may b
2a5f7 65 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 74  e missing from t
2a5f8 68 65 20 4f 52 44 45 52 20 42 59 0a 2a 2a 20 63  he ORDER BY.** c
2a5f9 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20 6d 61  lause and the ma
2a5fa 74 63 68 20 63 61 6e 20 73 74 69 6c 6c 20 62 65  tch can still be
2a5fb 20 61 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a   a success..**.*
2a5fc 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74  * All terms of t
2a5fd 68 65 20 4f 52 44 45 52 20 42 59 20 74 68 61 74  he ORDER BY that
2a5fe 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20 74   match against t
2a5ff 68 65 20 69 6e 64 65 78 20 6d 75 73 74 20 62 65  he index must be
2a600 20 65 69 74 68 65 72 0a 2a 2a 20 41 53 43 20 6f   either.** ASC o
2a601 72 20 44 45 53 43 2e 20 20 28 54 65 72 6d 73 20  r DESC.  (Terms 
2a602 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
2a603 63 6c 61 75 73 65 20 70 61 73 74 20 74 68 65 20  clause past the 
2a604 65 6e 64 20 6f 66 20 61 20 55 4e 49 51 55 45 0a  end of a UNIQUE.
2a605 2a 2a 20 69 6e 64 65 78 20 64 6f 20 6e 6f 74 20  ** index do not 
2a606 6e 65 65 64 20 74 6f 20 73 61 74 69 73 66 79 20  need to satisfy 
2a607 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e  this constraint.
2a608 29 20 20 54 68 65 20 2a 70 62 52 65 76 20 76 61  )  The *pbRev va
2a609 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f  lue is.** set to
2a60a 20 31 20 69 66 20 74 68 65 20 4f 52 44 45 52 20   1 if the ORDER 
2a60b 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 6c 6c  BY clause is all
2a60c 20 44 45 53 43 20 61 6e 64 20 69 74 20 69 73 20   DESC and it is 
2a60d 73 65 74 20 74 6f 20 30 20 69 66 0a 2a 2a 20 74  set to 0 if.** t
2a60e 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2a60f 73 65 20 69 73 20 61 6c 6c 20 41 53 43 2e 0a 2a  se is all ASC..*
2a610 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 53  /.static int isS
2a611 6f 72 74 69 6e 67 49 6e 64 65 78 28 0a 20 20 50  ortingIndex(.  P
2a612 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2a613 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2a614 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57  g context */.  W
2a615 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
2a616 73 6b 53 65 74 2c 20 2f 2a 20 4d 61 70 70 69 6e  skSet, /* Mappin
2a617 67 20 66 72 6f 6d 20 74 61 62 6c 65 20 63 75 72  g from table cur
2a618 73 6f 72 20 6e 75 6d 62 65 72 73 20 74 6f 20 62  sor numbers to b
2a619 69 74 6d 61 70 73 20 2a 2f 0a 20 20 49 6e 64 65  itmaps */.  Inde
2a61a 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20  x *pIdx,        
2a61b 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78      /* The index
2a61c 20 77 65 20 61 72 65 20 74 65 73 74 69 6e 67 20   we are testing 
2a61d 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 2c 20 20  */.  int base,  
2a61e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a61f 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  Cursor number fo
2a620 72 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  r the table to b
2a621 65 20 73 6f 72 74 65 64 20 2a 2f 0a 20 20 45 78  e sorted */.  Ex
2a622 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
2a623 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44  ,     /* The ORD
2a624 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2a625 20 20 69 6e 74 20 6e 45 71 43 6f 6c 2c 20 20 20    int nEqCol,   
2a626 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2a627 62 65 72 20 6f 66 20 69 6e 64 65 78 20 63 6f 6c  ber of index col
2a628 75 6d 6e 73 20 77 69 74 68 20 3d 3d 20 63 6f 6e  umns with == con
2a629 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  straints */.  in
2a62a 74 20 2a 70 62 52 65 76 20 20 20 20 20 20 20 20  t *pbRev        
2a62b 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20        /* Set to 
2a62c 31 20 69 66 20 4f 52 44 45 52 20 42 59 20 69 73  1 if ORDER BY is
2a62d 20 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 69 6e   DESC */.){.  in
2a62e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
2a62f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a630 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
2a631 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65  /.  int sortOrde
2a632 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
2a633 20 20 20 20 2f 2a 20 58 4f 52 20 6f 66 20 69 6e      /* XOR of in
2a634 64 65 78 20 61 6e 64 20 4f 52 44 45 52 20 42 59  dex and ORDER BY
2a635 20 73 6f 72 74 20 64 69 72 65 63 74 69 6f 6e 20   sort direction 
2a636 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20  */.  int nTerm; 
2a637 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a638 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2a639 66 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73  f ORDER BY terms
2a63a 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70   */.  struct Exp
2a63b 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 65 72  rList_item *pTer
2a63c 6d 3b 20 20 20 20 2f 2a 20 41 20 74 65 72 6d 20  m;    /* A term 
2a63d 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
2a63e 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 71 6c 69  clause */.  sqli
2a63f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2a640 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  ->db;..  assert(
2a641 20 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b 0a   pOrderBy!=0 );.
2a642 20 20 6e 54 65 72 6d 20 3d 20 70 4f 72 64 65 72    nTerm = pOrder
2a643 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 73 73  By->nExpr;.  ass
2a644 65 72 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b 0a  ert( nTerm>0 );.
2a645 0a 20 20 2f 2a 20 4d 61 74 63 68 20 74 65 72 6d  .  /* Match term
2a646 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  s of the ORDER B
2a647 59 20 63 6c 61 75 73 65 20 61 67 61 69 6e 73 74  Y clause against
2a648 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a   columns of.  **
2a649 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 2a 2a   the index..  **
2a64a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  .  ** Note that 
2a64b 69 6e 64 69 63 65 73 20 68 61 76 65 20 70 49 64  indices have pId
2a64c 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 72 65 67 75 6c  x->nColumn regul
2a64d 61 72 20 63 6f 6c 75 6d 6e 73 20 70 6c 75 73 0a  ar columns plus.
2a64e 20 20 2a 2a 20 6f 6e 65 20 61 64 64 69 74 69 6f    ** one additio
2a64f 6e 61 6c 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61  nal column conta
2a650 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 69 64 2e  ining the rowid.
2a651 20 20 54 68 65 20 72 6f 77 69 64 20 63 6f 6c 75    The rowid colu
2a652 6d 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69  mn.  ** of the i
2a653 6e 64 65 78 20 69 73 20 61 6c 73 6f 20 61 6c 6c  ndex is also all
2a654 6f 77 65 64 20 74 6f 20 6d 61 74 63 68 20 61 67  owed to match ag
2a655 61 69 6e 73 74 20 74 68 65 20 4f 52 44 45 52 20  ainst the ORDER 
2a656 42 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e 0a  BY.  ** clause..
2a657 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a 3d 30    */.  for(i=j=0
2a658 2c 20 70 54 65 72 6d 3d 70 4f 72 64 65 72 42 79  , pTerm=pOrderBy
2a659 2d 3e 61 3b 20 6a 3c 6e 54 65 72 6d 20 26 26 20  ->a; j<nTerm && 
2a65a 69 3c 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  i<=pIdx->nColumn
2a65b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72  ; i++){.    Expr
2a65c 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 2f   *pExpr;       /
2a65d 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
2a65e 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2a65f 20 70 54 65 72 6d 20 2a 2f 0a 20 20 20 20 43 6f   pTerm */.    Co
2a660 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20  llSeq *pColl;   
2a661 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e   /* The collatin
2a662 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 70 45  g sequence of pE
2a663 78 70 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 74  xpr */.    int t
2a664 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 20 2f 2a  ermSortOrder; /*
2a665 20 53 6f 72 74 20 6f 72 64 65 72 20 66 6f 72 20   Sort order for 
2a666 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 20  this term */.   
2a667 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
2a668 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20      /* The i-th 
2a669 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e  column of the in
2a66a 64 65 78 2e 20 20 2d 31 20 66 6f 72 20 72 6f 77  dex.  -1 for row
2a66b 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53  id */.    int iS
2a66c 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 2f 2a 20  ortOrder;    /* 
2a66d 31 20 66 6f 72 20 44 45 53 43 2c 20 30 20 66 6f  1 for DESC, 0 fo
2a66e 72 20 41 53 43 20 6f 6e 20 74 68 65 20 69 2d 74  r ASC on the i-t
2a66f 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f 0a  h index term */.
2a670 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2a671 7a 43 6f 6c 6c 3b 20 2f 2a 20 4e 61 6d 65 20 6f  zColl; /* Name o
2a672 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  f the collating 
2a673 73 65 71 75 65 6e 63 65 20 66 6f 72 20 69 2d 74  sequence for i-t
2a674 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f 0a  h index term */.
2a675 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70 54 65  .    pExpr = pTe
2a676 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69  rm->pExpr;.    i
2a677 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  f( pExpr->op!=TK
2a678 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72  _COLUMN || pExpr
2a679 2d 3e 69 54 61 62 6c 65 21 3d 62 61 73 65 20 29  ->iTable!=base )
2a67a 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 6e 20 6e  {.      /* Can n
2a67b 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 20  ot use an index 
2a67c 73 6f 72 74 20 6f 6e 20 61 6e 79 74 68 69 6e 67  sort on anything
2a67d 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20 63   that is not a c
2a67e 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 0a 20 20 20  olumn in the.   
2a67f 20 20 20 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20     ** left-most 
2a680 74 61 62 6c 65 20 6f 66 20 74 68 65 20 46 52 4f  table of the FRO
2a681 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  M clause */.    
2a682 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2a683 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
2a684 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50  e3ExprCollSeq(pP
2a685 61 72 73 65 2c 20 70 45 78 70 72 29 3b 0a 20 20  arse, pExpr);.  
2a686 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a    if( !pColl ){.
2a687 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62        pColl = db
2a688 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20  ->pDfltColl;.   
2a689 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 70 49 64   }.    if( i<pId
2a68a 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  x->nColumn ){.  
2a68b 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 49      iColumn = pI
2a68c 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b  dx->aiColumn[i];
2a68d 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 75  .      if( iColu
2a68e 6d 6e 3d 3d 70 49 64 78 2d 3e 70 54 61 62 6c 65  mn==pIdx->pTable
2a68f 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
2a690 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b     iColumn = -1;
2a691 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2a692 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 49 64 78  SortOrder = pIdx
2a693 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b  ->aSortOrder[i];
2a694 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70  .      zColl = p
2a695 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a  Idx->azColl[i];.
2a696 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2a697 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20   iColumn = -1;. 
2a698 20 20 20 20 20 69 53 6f 72 74 4f 72 64 65 72 20       iSortOrder 
2a699 3d 20 30 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c  = 0;.      zColl
2a69a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b   = pColl->zName;
2a69b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
2a69c 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 69  Expr->iColumn!=i
2a69d 43 6f 6c 75 6d 6e 20 7c 7c 20 73 71 6c 69 74 65  Column || sqlite
2a69e 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e  3StrICmp(pColl->
2a69f 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b  zName, zColl) ){
2a6a0 0a 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 6a  .      /* Term j
2a6a1 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2a6a2 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74   clause does not
2a6a3 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e 20 69 20   match column i 
2a6a4 6f 66 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a  of the index */.
2a6a5 20 20 20 20 20 20 69 66 28 20 69 3c 6e 45 71 43        if( i<nEqC
2a6a6 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ol ){.        /*
2a6a7 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 6f 6c   If an index col
2a6a8 75 6d 6e 20 74 68 61 74 20 69 73 20 63 6f 6e 73  umn that is cons
2a6a9 74 72 61 69 6e 65 64 20 62 79 20 3d 3d 20 66 61  trained by == fa
2a6aa 69 6c 73 20 74 6f 20 6d 61 74 63 68 20 61 6e 0a  ils to match an.
2a6ab 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44 45 52          ** ORDER
2a6ac 20 42 59 20 74 65 72 6d 2c 20 74 68 61 74 20 69   BY term, that i
2a6ad 73 20 4f 4b 2e 20 20 4a 75 73 74 20 69 67 6e 6f  s OK.  Just igno
2a6ae 72 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 6f  re that column o
2a6af 66 20 74 68 65 20 69 6e 64 65 78 0a 20 20 20 20  f the index.    
2a6b0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63      */.        c
2a6b1 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d  ontinue;.      }
2a6b2 65 6c 73 65 20 69 66 28 20 69 3d 3d 70 49 64 78  else if( i==pIdx
2a6b3 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
2a6b4 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 63 6f       /* Index co
2a6b5 6c 75 6d 6e 20 69 20 69 73 20 74 68 65 20 72 6f  lumn i is the ro
2a6b6 77 69 64 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20  wid.  All other 
2a6b7 74 65 72 6d 73 20 6d 61 74 63 68 2e 20 2a 2f 0a  terms match. */.
2a6b8 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2a6b9 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2a6ba 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 69 6e 64      /* If an ind
2a6bb 65 78 20 63 6f 6c 75 6d 6e 20 66 61 69 6c 73 20  ex column fails 
2a6bc 74 6f 20 6d 61 74 63 68 20 61 6e 64 20 69 73 20  to match and is 
2a6bd 6e 6f 74 20 63 6f 6e 73 74 72 61 69 6e 65 64 20  not constrained 
2a6be 62 79 20 3d 3d 0a 20 20 20 20 20 20 20 20 2a 2a  by ==.        **
2a6bf 20 74 68 65 6e 20 74 68 65 20 69 6e 64 65 78 20   then the index 
2a6c0 63 61 6e 6e 6f 74 20 73 61 74 69 73 66 79 20 74  cannot satisfy t
2a6c1 68 65 20 4f 52 44 45 52 20 42 59 20 63 6f 6e 73  he ORDER BY cons
2a6c2 74 72 61 69 6e 74 2e 0a 20 20 20 20 20 20 20 20  traint..        
2a6c3 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
2a6c4 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
2a6c5 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
2a6c6 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 21  Idx->aSortOrder!
2a6c7 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
2a6c8 28 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64  ( pTerm->sortOrd
2a6c9 65 72 3d 3d 30 20 7c 7c 20 70 54 65 72 6d 2d 3e  er==0 || pTerm->
2a6ca 73 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29 3b 0a  sortOrder==1 );.
2a6cb 20 20 20 20 61 73 73 65 72 74 28 20 69 53 6f 72      assert( iSor
2a6cc 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20 69 53 6f  tOrder==0 || iSo
2a6cd 72 74 4f 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20  rtOrder==1 );.  
2a6ce 20 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 20    termSortOrder 
2a6cf 3d 20 69 53 6f 72 74 4f 72 64 65 72 20 5e 20 70  = iSortOrder ^ p
2a6d0 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b  Term->sortOrder;
2a6d1 0a 20 20 20 20 69 66 28 20 69 3e 6e 45 71 43 6f  .    if( i>nEqCo
2a6d2 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 74  l ){.      if( t
2a6d3 65 72 6d 53 6f 72 74 4f 72 64 65 72 21 3d 73 6f  ermSortOrder!=so
2a6d4 72 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20  rtOrder ){.     
2a6d5 20 20 20 2f 2a 20 49 6e 64 69 63 65 73 20 63 61     /* Indices ca
2a6d6 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 69  n only be used i
2a6d7 66 20 61 6c 6c 20 4f 52 44 45 52 20 42 59 20 74  f all ORDER BY t
2a6d8 65 72 6d 73 20 70 61 73 74 20 74 68 65 0a 20 20  erms past the.  
2a6d9 20 20 20 20 20 20 2a 2a 20 65 71 75 61 6c 69 74        ** equalit
2a6da 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72  y constraints ar
2a6db 65 20 61 6c 6c 20 65 69 74 68 65 72 20 44 45 53  e all either DES
2a6dc 43 20 6f 72 20 41 53 43 2e 20 2a 2f 0a 20 20 20  C or ASC. */.   
2a6dd 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
2a6de 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2a6df 7b 0a 20 20 20 20 20 20 73 6f 72 74 4f 72 64 65  {.      sortOrde
2a6e0 72 20 3d 20 74 65 72 6d 53 6f 72 74 4f 72 64 65  r = termSortOrde
2a6e1 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b  r;.    }.    j++
2a6e2 3b 0a 20 20 20 20 70 54 65 72 6d 2b 2b 3b 0a 20  ;.    pTerm++;. 
2a6e3 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30     if( iColumn<0
2a6e4 20 26 26 20 21 72 65 66 65 72 65 6e 63 65 73 4f   && !referencesO
2a6e5 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72 64 65  therTables(pOrde
2a6e6 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20 6a  rBy, pMaskSet, j
2a6e7 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20 20 20  , base) ){.     
2a6e8 20 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78   /* If the index
2a6e9 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65  ed column is the
2a6ea 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61 6e 64   primary key and
2a6eb 20 65 76 65 72 79 74 68 69 6e 67 20 6d 61 74 63   everything matc
2a6ec 68 65 73 0a 20 20 20 20 20 20 2a 2a 20 73 6f 20  hes.      ** so 
2a6ed 66 61 72 20 61 6e 64 20 6e 6f 6e 65 20 6f 66 20  far and none of 
2a6ee 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72  the ORDER BY ter
2a6ef 6d 73 20 74 6f 20 74 68 65 20 72 69 67 68 74 20  ms to the right 
2a6f0 72 65 66 65 72 65 6e 63 65 20 6f 74 68 65 72 0a  reference other.
2a6f1 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 20        ** tables 
2a6f2 69 6e 20 74 68 65 20 6a 6f 69 6e 2c 20 74 68 65  in the join, the
2a6f3 6e 20 77 65 20 61 72 65 20 61 73 73 75 72 65 64  n we are assured
2a6f4 20 74 68 61 74 20 74 68 65 20 69 6e 64 65 78 20   that the index 
2a6f5 63 61 6e 20 62 65 20 75 73 65 64 20 0a 20 20 20  can be used .   
2a6f6 20 20 20 2a 2a 20 74 6f 20 73 6f 72 74 20 62 65     ** to sort be
2a6f7 63 61 75 73 65 20 74 68 65 20 70 72 69 6d 61 72  cause the primar
2a6f8 79 20 6b 65 79 20 69 73 20 75 6e 69 71 75 65 20  y key is unique 
2a6f9 61 6e 64 20 73 6f 20 6e 6f 6e 65 20 6f 66 20 74  and so none of t
2a6fa 68 65 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a  he other.      *
2a6fb 2a 20 63 6f 6c 75 6d 6e 73 20 77 69 6c 6c 20 6d  * columns will m
2a6fc 61 6b 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e  ake any differen
2a6fd 63 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ce.      */.    
2a6fe 20 20 6a 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20    j = nTerm;.   
2a6ff 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 62 52 65 76   }.  }..  *pbRev
2a700 20 3d 20 73 6f 72 74 4f 72 64 65 72 21 3d 30 3b   = sortOrder!=0;
2a701 0a 20 20 69 66 28 20 6a 3e 3d 6e 54 65 72 6d 20  .  if( j>=nTerm 
2a702 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 74 65  ){.    /* All te
2a703 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
2a704 20 42 59 20 63 6c 61 75 73 65 20 61 72 65 20 63   BY clause are c
2a705 6f 76 65 72 65 64 20 62 79 20 74 68 69 73 20 69  overed by this i
2a706 6e 64 65 78 20 73 6f 0a 20 20 20 20 2a 2a 20 74  ndex so.    ** t
2a707 68 69 73 20 69 6e 64 65 78 20 63 61 6e 20 62 65  his index can be
2a708 20 75 73 65 64 20 66 6f 72 20 73 6f 72 74 69 6e   used for sortin
2a709 67 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  g. */.    return
2a70a 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49   1;.  }.  if( pI
2a70b 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
2a70c 4e 6f 6e 65 20 26 26 20 69 3d 3d 70 49 64 78 2d  None && i==pIdx-
2a70d 3e 6e 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 26  >nColumn.      &
2a70e 26 20 21 72 65 66 65 72 65 6e 63 65 73 4f 74 68  & !referencesOth
2a70f 65 72 54 61 62 6c 65 73 28 70 4f 72 64 65 72 42  erTables(pOrderB
2a710 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20 6a 2c 20  y, pMaskSet, j, 
2a711 62 61 73 65 29 20 29 7b 0a 20 20 20 20 2f 2a 20  base) ){.    /* 
2a712 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 69  All terms of thi
2a713 73 20 69 6e 64 65 78 20 6d 61 74 63 68 20 73 6f  s index match so
2a714 6d 65 20 70 72 65 66 69 78 20 6f 66 20 74 68 65  me prefix of the
2a715 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2a716 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20  .    ** and the 
2a717 69 6e 64 65 78 20 69 73 20 55 4e 49 51 55 45 20  index is UNIQUE 
2a718 61 6e 64 20 6e 6f 20 74 65 72 6d 73 20 6f 6e 20  and no terms on 
2a719 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20  the tail of the 
2a71a 4f 52 44 45 52 20 42 59 0a 20 20 20 20 2a 2a 20  ORDER BY.    ** 
2a71b 63 6c 61 75 73 65 20 72 65 66 65 72 65 6e 63 65  clause reference
2a71c 20 6f 74 68 65 72 20 74 61 62 6c 65 73 20 69 6e   other tables in
2a71d 20 61 20 6a 6f 69 6e 2e 20 20 49 66 20 74 68 69   a join.  If thi
2a71e 73 20 69 73 20 61 6c 6c 20 74 72 75 65 20 74 68  s is all true th
2a71f 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 72  en.    ** the or
2a720 64 65 72 20 62 79 20 63 6c 61 75 73 65 20 69 73  der by clause is
2a721 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 20 2a 2f   superfluous. */
2a722 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
2a723 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
2a724 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 61  ../*.** Check ta
2a725 62 6c 65 20 74 6f 20 73 65 65 20 69 66 20 74 68  ble to see if th
2a726 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2a727 65 20 69 6e 20 70 4f 72 64 65 72 42 79 20 63 61  e in pOrderBy ca
2a728 6e 20 62 65 20 73 61 74 69 73 66 69 65 64 0a 2a  n be satisfied.*
2a729 2a 20 62 79 20 73 6f 72 74 69 6e 67 20 69 6e 20  * by sorting in 
2a72a 6f 72 64 65 72 20 6f 66 20 52 4f 57 49 44 2e 20  order of ROWID. 
2a72b 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
2a72c 73 6f 20 61 6e 64 20 73 65 74 20 2a 70 62 52 65  so and set *pbRe
2a72d 76 20 74 6f 20 62 65 0a 2a 2a 20 74 72 75 65 20  v to be.** true 
2a72e 66 6f 72 20 72 65 76 65 72 73 65 20 52 4f 57 49  for reverse ROWI
2a72f 44 20 61 6e 64 20 66 61 6c 73 65 20 66 6f 72 20  D and false for 
2a730 66 6f 72 77 61 72 64 20 52 4f 57 49 44 20 6f 72  forward ROWID or
2a731 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  der..*/.static i
2a732 6e 74 20 73 6f 72 74 61 62 6c 65 42 79 52 6f 77  nt sortableByRow
2a733 69 64 28 0a 20 20 69 6e 74 20 62 61 73 65 2c 20  id(.  int base, 
2a734 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a735 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
2a736 6f 72 20 74 61 62 6c 65 20 74 6f 20 62 65 20 73  or table to be s
2a737 6f 72 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  orted */.  ExprL
2a738 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
2a739 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
2a73a 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57  BY clause */.  W
2a73b 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
2a73c 73 6b 53 65 74 2c 20 2f 2a 20 4d 61 70 70 69 6e  skSet, /* Mappin
2a73d 67 20 66 72 6f 6d 20 74 61 62 6c 65 20 63 75 72  g from table cur
2a73e 73 6f 72 73 20 74 6f 20 62 69 74 6d 61 70 73 20  sors to bitmaps 
2a73f 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52 65 76 20  */.  int *pbRev 
2a740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a741 53 65 74 20 74 6f 20 31 20 69 66 20 4f 52 44 45  Set to 1 if ORDE
2a742 52 20 42 59 20 69 73 20 44 45 53 43 20 2a 2f 0a  R BY is DESC */.
2a743 29 7b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 0a 20  ){.  Expr *p;.. 
2a744 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42   assert( pOrderB
2a745 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  y!=0 );.  assert
2a746 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  ( pOrderBy->nExp
2a747 72 3e 30 20 29 3b 0a 20 20 70 20 3d 20 70 4f 72  r>0 );.  p = pOr
2a748 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 70 45 78 70  derBy->a[0].pExp
2a749 72 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  r;.  if( p->op==
2a74a 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 2d 3e  TK_COLUMN && p->
2a74b 69 54 61 62 6c 65 3d 3d 62 61 73 65 20 26 26 20  iTable==base && 
2a74c 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 0a 20  p->iColumn==-1. 
2a74d 20 20 20 26 26 20 21 72 65 66 65 72 65 6e 63 65     && !reference
2a74e 73 4f 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72  sOtherTables(pOr
2a74f 64 65 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c  derBy, pMaskSet,
2a750 20 31 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20   1, base) ){.   
2a751 20 2a 70 62 52 65 76 20 3d 20 70 4f 72 64 65 72   *pbRev = pOrder
2a752 42 79 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64  By->a[0].sortOrd
2a753 65 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  er;.    return 1
2a754 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
2a755 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
2a756 72 65 20 61 20 63 72 75 64 65 20 65 73 74 69 6d  re a crude estim
2a757 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 67 61 72  ate of the logar
2a758 69 74 68 6d 20 6f 66 20 74 68 65 20 69 6e 70 75  ithm of the inpu
2a759 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 54 68 65 20  t value..** The 
2a75a 72 65 73 75 6c 74 73 20 6e 65 65 64 20 6e 6f 74  results need not
2a75b 20 62 65 20 65 78 61 63 74 2e 20 20 54 68 69 73   be exact.  This
2a75c 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f   is only used fo
2a75d 72 20 65 73 74 69 6d 61 74 69 6e 67 0a 2a 2a 20  r estimating.** 
2a75e 74 68 65 20 74 6f 74 61 6c 20 63 6f 73 74 20 6f  the total cost o
2a75f 66 20 70 65 72 66 6f 72 6d 69 6e 67 20 6f 70 65  f performing ope
2a760 72 61 74 69 6f 6e 73 20 77 69 74 68 20 4f 28 6c  rations with O(l
2a761 6f 67 4e 29 20 6f 72 20 4f 28 4e 6c 6f 67 4e 29  ogN) or O(NlogN)
2a762 0a 2a 2a 20 63 6f 6d 70 6c 65 78 69 74 79 2e 20  .** complexity. 
2a763 20 42 65 63 61 75 73 65 20 4e 20 69 73 20 6a 75   Because N is ju
2a764 73 74 20 61 20 67 75 65 73 73 2c 20 69 74 20 69  st a guess, it i
2a765 73 20 6e 6f 20 67 72 65 61 74 20 74 72 61 67 65  s no great trage
2a766 64 79 20 69 66 0a 2a 2a 20 6c 6f 67 4e 20 69 73  dy if.** logN is
2a767 20 61 20 6c 69 74 74 6c 65 20 6f 66 66 2e 0a 2a   a little off..*
2a768 2f 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65 20  /.static double 
2a769 65 73 74 4c 6f 67 28 64 6f 75 62 6c 65 20 4e 29  estLog(double N)
2a76a 7b 0a 20 20 64 6f 75 62 6c 65 20 6c 6f 67 4e 20  {.  double logN 
2a76b 3d 20 31 3b 0a 20 20 64 6f 75 62 6c 65 20 78 20  = 1;.  double x 
2a76c 3d 20 31 30 3b 0a 20 20 77 68 69 6c 65 28 20 4e  = 10;.  while( N
2a76d 3e 78 20 29 7b 0a 20 20 20 20 6c 6f 67 4e 20 2b  >x ){.    logN +
2a76e 3d 20 31 3b 0a 20 20 20 20 78 20 2a 3d 20 31 30  = 1;.    x *= 10
2a76f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6c  ;.  }.  return l
2a770 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77  ogN;.}../*.** Tw
2a771 6f 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 70  o routines for p
2a772 72 69 6e 74 69 6e 67 20 74 68 65 20 63 6f 6e 74  rinting the cont
2a773 65 6e 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  ent of an sqlite
2a774 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20  3_index_info.** 
2a775 73 74 72 75 63 74 75 72 65 2e 20 20 55 73 65 64  structure.  Used
2a776 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
2a777 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
2a778 20 20 49 66 20 6e 65 69 74 68 65 72 0a 2a 2a 20    If neither.** 
2a779 53 51 4c 49 54 45 5f 54 45 53 54 20 6f 72 20 53  SQLITE_TEST or S
2a77a 51 4c 49 54 45 5f 44 45 42 55 47 20 61 72 65 20  QLITE_DEBUG are 
2a77b 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68  defined, then th
2a77c 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  ese routines.** 
2a77d 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23  are no-ops..*/.#
2a77e 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
2a77f 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2a780 41 42 4c 45 29 20 26 26 20 64 65 66 69 6e 65 64  ABLE) && defined
2a781 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73  (SQLITE_DEBUG).s
2a782 74 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45  tatic void TRACE
2a783 5f 49 44 58 5f 49 4e 50 55 54 53 28 73 71 6c 69  _IDX_INPUTS(sqli
2a784 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2a785 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p){.  int i;.  i
2a786 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65  f( !sqlite3Where
2a787 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a  Trace ) return;.
2a788 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2a789 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
2a78a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2a78b 62 75 67 50 72 69 6e 74 66 28 22 20 20 63 6f 6e  bugPrintf("  con
2a78c 73 74 72 61 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c  straint[%d]: col
2a78d 3d 25 64 20 74 65 72 6d 69 64 3d 25 64 20 6f 70  =%d termid=%d op
2a78e 3d 25 64 20 75 73 61 62 6c 65 64 3d 25 64 5c 6e  =%d usabled=%d\n
2a78f 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20  ",.       i,.   
2a790 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69      p->aConstrai
2a791 6e 74 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20  nt[i].iColumn,. 
2a792 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72        p->aConstr
2a793 61 69 6e 74 5b 69 5d 2e 69 54 65 72 6d 4f 66 66  aint[i].iTermOff
2a794 73 65 74 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61  set,.       p->a
2a795 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 6f 70  Constraint[i].op
2a796 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
2a797 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c  straint[i].usabl
2a798 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  e);.  }.  for(i=
2a799 30 3b 20 69 3c 70 2d 3e 6e 4f 72 64 65 72 42 79  0; i<p->nOrderBy
2a79a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
2a79b 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2a79c 20 20 6f 72 64 65 72 62 79 5b 25 64 5d 3a 20 63    orderby[%d]: c
2a79d 6f 6c 3d 25 64 20 64 65 73 63 3d 25 64 5c 6e 22  ol=%d desc=%d\n"
2a79e 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20  ,.       i,.    
2a79f 20 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69     p->aOrderBy[i
2a7a0 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  ].iColumn,.     
2a7a1 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d    p->aOrderBy[i]
2a7a2 2e 64 65 73 63 29 3b 0a 20 20 7d 0a 7d 0a 73 74  .desc);.  }.}.st
2a7a3 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45 5f  atic void TRACE_
2a7a4 49 44 58 5f 4f 55 54 50 55 54 53 28 73 71 6c 69  IDX_OUTPUTS(sqli
2a7a5 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2a7a6 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p){.  int i;.  i
2a7a7 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65  f( !sqlite3Where
2a7a8 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a  Trace ) return;.
2a7a9 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2a7aa 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
2a7ab 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2a7ac 62 75 67 50 72 69 6e 74 66 28 22 20 20 75 73 61  bugPrintf("  usa
2a7ad 67 65 5b 25 64 5d 3a 20 61 72 67 76 49 64 78 3d  ge[%d]: argvIdx=
2a7ae 25 64 20 6f 6d 69 74 3d 25 64 5c 6e 22 2c 0a 20  %d omit=%d\n",. 
2a7af 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20        i,.       
2a7b0 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  p->aConstraintUs
2a7b1 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78  age[i].argvIndex
2a7b2 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
2a7b3 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
2a7b4 6f 6d 69 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  omit);.  }.  sql
2a7b5 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2a7b6 22 20 20 69 64 78 4e 75 6d 3d 25 64 5c 6e 22 2c  "  idxNum=%d\n",
2a7b7 20 70 2d 3e 69 64 78 4e 75 6d 29 3b 0a 20 20 73   p->idxNum);.  s
2a7b8 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2a7b9 66 28 22 20 20 69 64 78 53 74 72 3d 25 73 5c 6e  f("  idxStr=%s\n
2a7ba 22 2c 20 70 2d 3e 69 64 78 53 74 72 29 3b 0a 20  ", p->idxStr);. 
2a7bb 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2a7bc 6e 74 66 28 22 20 20 6f 72 64 65 72 42 79 43 6f  ntf("  orderByCo
2a7bd 6e 73 75 6d 65 64 3d 25 64 5c 6e 22 2c 20 70 2d  nsumed=%d\n", p-
2a7be 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64  >orderByConsumed
2a7bf 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75  );.  sqlite3Debu
2a7c0 67 50 72 69 6e 74 66 28 22 20 20 65 73 74 69 6d  gPrintf("  estim
2a7c1 61 74 65 64 43 6f 73 74 3d 25 67 5c 6e 22 2c 20  atedCost=%g\n", 
2a7c2 70 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  p->estimatedCost
2a7c3 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69  );.}.#else.#defi
2a7c4 6e 65 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50  ne TRACE_IDX_INP
2a7c5 55 54 53 28 41 29 0a 23 64 65 66 69 6e 65 20 54  UTS(A).#define T
2a7c6 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53  RACE_IDX_OUTPUTS
2a7c7 28 41 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  (A).#endif..#ifn
2a7c8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2a7c9 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a  VIRTUALTABLE./*.
2a7ca 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 62  ** Compute the b
2a7cb 65 73 74 20 69 6e 64 65 78 20 66 6f 72 20 61 20  est index for a 
2a7cc 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
2a7cd 2a 0a 2a 2a 20 54 68 65 20 62 65 73 74 20 69 6e  *.** The best in
2a7ce 64 65 78 20 69 73 20 63 6f 6d 70 75 74 65 64 20  dex is computed 
2a7cf 62 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65  by the xBestInde
2a7d0 78 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  x method of the 
2a7d1 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65  virtual.** table
2a7d2 20 6d 6f 64 75 6c 65 2e 20 20 54 68 69 73 20 72   module.  This r
2a7d3 6f 75 74 69 6e 65 20 69 73 20 72 65 61 6c 6c 79  outine is really
2a7d4 20 6a 75 73 74 20 61 20 77 72 61 70 70 65 72 20   just a wrapper 
2a7d5 74 68 61 74 20 73 65 74 73 20 75 70 0a 2a 2a 20  that sets up.** 
2a7d6 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
2a7d7 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2a7d8 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f   that is used to
2a7d9 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 77 69 74   communicate wit
2a7da 68 0a 2a 2a 20 78 42 65 73 74 49 6e 64 65 78 2e  h.** xBestIndex.
2a7db 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 20 6a 6f 69 6e  .**.** In a join
2a7dc 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  , this routine m
2a7dd 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64 20 6d  ight be called m
2a7de 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 66 6f  ultiple times fo
2a7df 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 76 69  r the.** same vi
2a7e0 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
2a7e1 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
2a7e2 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  info structure i
2a7e3 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64  s created.** and
2a7e4 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6f 6e 20   initialized on 
2a7e5 74 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61  the first invoca
2a7e6 74 69 6f 6e 20 61 6e 64 20 72 65 75 73 65 64 20  tion and reused 
2a7e7 6f 6e 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  on all subsequen
2a7e8 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73  t.** invocations
2a7e9 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69  .  The sqlite3_i
2a7ea 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
2a7eb 75 72 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64  ure is also used
2a7ec 20 77 68 65 6e 0a 2a 2a 20 63 6f 64 65 20 69 73   when.** code is
2a7ed 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 61 63   generated to ac
2a7ee 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c  cess the virtual
2a7ef 20 74 61 62 6c 65 2e 20 20 54 68 65 20 77 68 65   table.  The whe
2a7f0 72 65 49 6e 66 6f 44 65 6c 65 74 65 28 29 20 0a  reInfoDelete() .
2a7f1 2a 2a 20 72 6f 75 74 69 6e 65 20 74 61 6b 65 73  ** routine takes
2a7f2 20 63 61 72 65 20 6f 66 20 66 72 65 65 69 6e 67   care of freeing
2a7f3 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2a7f4 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2a7f5 65 20 61 66 74 65 72 0a 2a 2a 20 65 76 65 72 79  e after.** every
2a7f6 62 6f 64 79 20 68 61 73 20 66 69 6e 69 73 68 65  body has finishe
2a7f7 64 20 77 69 74 68 20 69 74 2e 0a 2a 2f 0a 73 74  d with it..*/.st
2a7f8 61 74 69 63 20 64 6f 75 62 6c 65 20 62 65 73 74  atic double best
2a7f9 56 69 72 74 75 61 6c 49 6e 64 65 78 28 0a 20 20  VirtualIndex(.  
2a7fa 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2a7fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a7fc 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
2a7fd 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
2a7fe 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
2a7ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2a800 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2a801 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2a802 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 20  t_item *pSrc,   
2a803 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
2a804 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61  ause term to sea
2a805 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  rch */.  Bitmask
2a806 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20   notReady,      
2a807 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
2a808 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  of cursors that 
2a809 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
2a80a 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
2a80b 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20  *pOrderBy,      
2a80c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 64        /* The ord
2a80d 65 72 20 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a  er by clause */.
2a80e 20 20 69 6e 74 20 6f 72 64 65 72 42 79 55 73 61    int orderByUsa
2a80f 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ble,            
2a810 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 63   /* True if we c
2a811 61 6e 20 70 6f 74 65 6e 74 69 61 6c 20 73 6f 72  an potential sor
2a812 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  t */.  sqlite3_i
2a813 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64  ndex_info **ppId
2a814 78 49 6e 66 6f 20 2f 2a 20 49 6e 64 65 78 20 69  xInfo /* Index i
2a815 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65  nformation passe
2a816 64 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20  d to xBestIndex 
2a817 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  */.){.  Table *p
2a818 54 61 62 20 3d 20 70 53 72 63 2d 3e 70 54 61 62  Tab = pSrc->pTab
2a819 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ;.  sqlite3_vtab
2a81a 20 2a 70 56 74 61 62 20 3d 20 70 54 61 62 2d 3e   *pVtab = pTab->
2a81b 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33  pVtab;.  sqlite3
2a81c 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64  _index_info *pId
2a81d 78 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20  xInfo;.  struct 
2a81e 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
2a81f 6e 73 74 72 61 69 6e 74 20 2a 70 49 64 78 43 6f  nstraint *pIdxCo
2a820 6e 73 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c  ns;.  struct sql
2a821 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72  ite3_index_order
2a822 62 79 20 2a 70 49 64 78 4f 72 64 65 72 42 79 3b  by *pIdxOrderBy;
2a823 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
2a824 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2a825 6e 74 5f 75 73 61 67 65 20 2a 70 55 73 61 67 65  nt_usage *pUsage
2a826 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  ;.  WhereTerm *p
2a827 54 65 72 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  Term;.  int i, j
2a828 3b 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79  ;.  int nOrderBy
2a829 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f  ;.  int rc;..  /
2a82a 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 33  * If the sqlite3
2a82b 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2a82c 63 74 75 72 65 20 68 61 73 20 6e 6f 74 20 62 65  cture has not be
2a82d 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 20  en previously.  
2a82e 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  ** allocated and
2a82f 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 66 6f 72   initialized for
2a830 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 74 61   this virtual ta
2a831 62 6c 65 2c 20 74 68 65 6e 20 61 6c 6c 6f 63 61  ble, then alloca
2a832 74 65 0a 20 20 2a 2a 20 61 6e 64 20 69 6e 69 74  te.  ** and init
2a833 69 61 6c 69 7a 65 20 69 74 20 6e 6f 77 0a 20 20  ialize it now.  
2a834 2a 2f 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d 20  */.  pIdxInfo = 
2a835 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20 20 69 66  *ppIdxInfo;.  if
2a836 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b  ( pIdxInfo==0 ){
2a837 0a 20 20 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a  .    int nTerm;.
2a838 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
2a839 22 52 65 63 6f 6d 70 75 74 69 6e 67 20 69 6e 64  "Recomputing ind
2a83a 65 78 20 69 6e 66 6f 20 66 6f 72 20 25 73 2e 2e  ex info for %s..
2a83b 2e 5c 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  .\n", pTab->zNam
2a83c 65 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 75  e));..    /* Cou
2a83d 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nt the number of
2a83e 20 70 6f 73 73 69 62 6c 65 20 57 48 45 52 45 20   possible WHERE 
2a83f 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e  clause constrain
2a840 74 73 20 72 65 66 65 72 72 69 6e 67 0a 20 20 20  ts referring.   
2a841 20 2a 2a 20 74 6f 20 74 68 69 73 20 76 69 72 74   ** to this virt
2a842 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  ual table */.   
2a843 20 66 6f 72 28 69 3d 6e 54 65 72 6d 3d 30 2c 20   for(i=nTerm=0, 
2a844 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c  pTerm=pWC->a; i<
2a845 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c  pWC->nTerm; i++,
2a846 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20   pTerm++){.     
2a847 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74   if( pTerm->left
2a848 43 75 72 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e  Cursor != pSrc->
2a849 69 43 75 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e  iCursor ) contin
2a84a 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ue;.      assert
2a84b 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  ( (pTerm->eOpera
2a84c 74 6f 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65  tor&(pTerm->eOpe
2a84d 72 61 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a  rator-1))==0 );.
2a84e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2a84f 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2a850 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 20  ==WO_IN );.     
2a851 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
2a852 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2a853 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20  ISNULL );.      
2a854 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
2a855 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e 7c 57 4f  ator & (WO_IN|WO
2a856 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f 6e 74 69  _ISNULL) ) conti
2a857 6e 75 65 3b 0a 20 20 20 20 20 20 6e 54 65 72 6d  nue;.      nTerm
2a858 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ++;.    }..    /
2a859 2a 20 49 66 20 74 68 65 20 4f 52 44 45 52 20 42  * If the ORDER B
2a85a 59 20 63 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e  Y clause contain
2a85b 73 20 6f 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20 69  s only columns i
2a85c 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 0a 20  n the current . 
2a85d 20 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 74 61     ** virtual ta
2a85e 62 6c 65 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74  ble then allocat
2a85f 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  e space for the 
2a860 61 4f 72 64 65 72 42 79 20 70 61 72 74 20 6f 66  aOrderBy part of
2a861 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 71 6c 69  .    ** the sqli
2a862 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
2a863 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f  tructure..    */
2a864 0a 20 20 20 20 6e 4f 72 64 65 72 42 79 20 3d 20  .    nOrderBy = 
2a865 30 3b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65  0;.    if( pOrde
2a866 72 42 79 20 29 7b 0a 20 20 20 20 20 20 66 6f 72  rBy ){.      for
2a867 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79  (i=0; i<pOrderBy
2a868 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
2a869 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78         Expr *pEx
2a86a 70 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  pr = pOrderBy->a
2a86b 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
2a86c 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70     if( pExpr->op
2a86d 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70  !=TK_COLUMN || p
2a86e 45 78 70 72 2d 3e 69 54 61 62 6c 65 21 3d 70 53  Expr->iTable!=pS
2a86f 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20 62 72  rc->iCursor ) br
2a870 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2a871 20 20 20 69 66 28 20 69 3d 3d 70 4f 72 64 65 72     if( i==pOrder
2a872 42 79 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  By->nExpr ){.   
2a873 20 20 20 20 20 6e 4f 72 64 65 72 42 79 20 3d 20       nOrderBy = 
2a874 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b  pOrderBy->nExpr;
2a875 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2a876 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20      /* Allocate 
2a877 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
2a878 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2a879 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 64 78  .    */.    pIdx
2a87a 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62  Info = sqlite3Db
2a87b 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73  MallocZero(pPars
2a87c 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  e->db, sizeof(*p
2a87d 49 64 78 49 6e 66 6f 29 0a 20 20 20 20 20 20 20  IdxInfo).       
2a87e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a87f 20 20 20 20 20 20 2b 20 28 73 69 7a 65 6f 66 28        + (sizeof(
2a880 2a 70 49 64 78 43 6f 6e 73 29 20 2b 20 73 69 7a  *pIdxCons) + siz
2a881 65 6f 66 28 2a 70 55 73 61 67 65 29 29 2a 6e 54  eof(*pUsage))*nT
2a882 65 72 6d 0a 20 20 20 20 20 20 20 20 20 20 20 20  erm.            
2a883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a884 20 2b 20 73 69 7a 65 6f 66 28 2a 70 49 64 78 4f   + sizeof(*pIdxO
2a885 72 64 65 72 42 79 29 2a 6e 4f 72 64 65 72 42 79  rderBy)*nOrderBy
2a886 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78   );.    if( pIdx
2a887 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Info==0 ){.     
2a888 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2a889 28 70 50 61 72 73 65 2c 20 22 6f 75 74 20 6f 66  (pParse, "out of
2a88a 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 20   memory");.     
2a88b 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a 20 20 20   return 0.0;.   
2a88c 20 7d 0a 20 20 20 20 2a 70 70 49 64 78 49 6e 66   }.    *ppIdxInf
2a88d 6f 20 3d 20 70 49 64 78 49 6e 66 6f 3b 0a 0a 20  o = pIdxInfo;.. 
2a88e 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65     /* Initialize
2a88f 20 74 68 65 20 73 74 72 75 63 74 75 72 65 2e 20   the structure. 
2a890 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   The sqlite3_ind
2a891 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2a892 65 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 2a  e contains.    *
2a893 2a 20 6d 61 6e 79 20 66 69 65 6c 64 73 20 74 68  * many fields th
2a894 61 74 20 61 72 65 20 64 65 63 6c 61 72 65 64 20  at are declared 
2a895 22 63 6f 6e 73 74 22 20 74 6f 20 70 72 65 76 65  "const" to preve
2a896 6e 74 20 78 42 65 73 74 49 6e 64 65 78 20 66 72  nt xBestIndex fr
2a897 6f 6d 0a 20 20 20 20 2a 2a 20 63 68 61 6e 67 69  om.    ** changi
2a898 6e 67 20 74 68 65 6d 2e 20 20 57 65 20 68 61 76  ng them.  We hav
2a899 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20 66 75 6e  e to do some fun
2a89a 6b 79 20 63 61 73 74 69 6e 67 20 69 6e 20 6f 72  ky casting in or
2a89b 64 65 72 20 74 6f 0a 20 20 20 20 2a 2a 20 69 6e  der to.    ** in
2a89c 69 74 69 61 6c 69 7a 65 20 74 68 6f 73 65 20 66  itialize those f
2a89d 69 65 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ields..    */.  
2a89e 20 20 70 49 64 78 43 6f 6e 73 20 3d 20 28 73 74    pIdxCons = (st
2a89f 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2a8a0 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 29 26  ex_constraint*)&
2a8a1 70 49 64 78 49 6e 66 6f 5b 31 5d 3b 0a 20 20 20  pIdxInfo[1];.   
2a8a2 20 70 49 64 78 4f 72 64 65 72 42 79 20 3d 20 28   pIdxOrderBy = (
2a8a3 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2a8a4 6e 64 65 78 5f 6f 72 64 65 72 62 79 2a 29 26 70  ndex_orderby*)&p
2a8a5 49 64 78 43 6f 6e 73 5b 6e 54 65 72 6d 5d 3b 0a  IdxCons[nTerm];.
2a8a6 20 20 20 20 70 55 73 61 67 65 20 3d 20 28 73 74      pUsage = (st
2a8a7 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2a8a8 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73  ex_constraint_us
2a8a9 61 67 65 2a 29 26 70 49 64 78 4f 72 64 65 72 42  age*)&pIdxOrderB
2a8aa 79 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20 20  y[nOrderBy];.   
2a8ab 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e 66   *(int*)&pIdxInf
2a8ac 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d  o->nConstraint =
2a8ad 20 6e 54 65 72 6d 3b 0a 20 20 20 20 2a 28 69 6e   nTerm;.    *(in
2a8ae 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f  t*)&pIdxInfo->nO
2a8af 72 64 65 72 42 79 20 3d 20 6e 4f 72 64 65 72 42  rderBy = nOrderB
2a8b0 79 3b 0a 20 20 20 20 2a 28 73 74 72 75 63 74 20  y;.    *(struct 
2a8b1 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
2a8b2 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78  nstraint**)&pIdx
2a8b3 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
2a8b4 74 20 3d 20 70 49 64 78 43 6f 6e 73 3b 0a 20 20  t = pIdxCons;.  
2a8b5 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74    *(struct sqlit
2a8b6 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79  e3_index_orderby
2a8b7 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 4f  **)&pIdxInfo->aO
2a8b8 72 64 65 72 42 79 20 3d 20 70 49 64 78 4f 72 64  rderBy = pIdxOrd
2a8b9 65 72 42 79 3b 0a 20 20 20 20 2a 28 73 74 72 75  erBy;.    *(stru
2a8ba 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
2a8bb 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67  _constraint_usag
2a8bc 65 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61  e**)&pIdxInfo->a
2a8bd 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 20  ConstraintUsage 
2a8be 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
2a8bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a8c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a8c2 20 20 20 20 20 20 20 70 55 73 61 67 65 3b 0a 0a         pUsage;..
2a8c3 20 20 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70      for(i=j=0, p
2a8c4 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70  Term=pWC->a; i<p
2a8c5 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20  WC->nTerm; i++, 
2a8c6 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pTerm++){.      
2a8c7 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43  if( pTerm->leftC
2a8c8 75 72 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e 69  ursor != pSrc->i
2a8c9 43 75 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e 75  Cursor ) continu
2a8ca 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
2a8cb 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74   (pTerm->eOperat
2a8cc 6f 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72  or&(pTerm->eOper
2a8cd 61 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a 20  ator-1))==0 );. 
2a8ce 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
2a8cf 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2a8d0 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 20 20  =WO_IN );.      
2a8d1 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
2a8d2 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49  >eOperator==WO_I
2a8d3 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 69  SNULL );.      i
2a8d4 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  f( pTerm->eOpera
2a8d5 74 6f 72 20 26 20 28 57 4f 5f 49 4e 7c 57 4f 5f  tor & (WO_IN|WO_
2a8d6 49 53 4e 55 4c 4c 29 20 29 20 63 6f 6e 74 69 6e  ISNULL) ) contin
2a8d7 75 65 3b 0a 20 20 20 20 20 20 70 49 64 78 43 6f  ue;.      pIdxCo
2a8d8 6e 73 5b 6a 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20  ns[j].iColumn = 
2a8d9 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c  pTerm->u.leftCol
2a8da 75 6d 6e 3b 0a 20 20 20 20 20 20 70 49 64 78 43  umn;.      pIdxC
2a8db 6f 6e 73 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66 73  ons[j].iTermOffs
2a8dc 65 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 70 49  et = i;.      pI
2a8dd 64 78 43 6f 6e 73 5b 6a 5d 2e 6f 70 20 3d 20 28  dxCons[j].op = (
2a8de 75 38 29 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  u8)pTerm->eOpera
2a8df 74 6f 72 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68  tor;.      /* Th
2a8e0 65 20 64 69 72 65 63 74 20 61 73 73 69 67 6e 6d  e direct assignm
2a8e1 65 6e 74 20 69 6e 20 74 68 65 20 70 72 65 76 69  ent in the previ
2a8e2 6f 75 73 20 6c 69 6e 65 20 69 73 20 70 6f 73 73  ous line is poss
2a8e3 69 62 6c 65 20 6f 6e 6c 79 20 62 65 63 61 75 73  ible only becaus
2a8e4 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 57  e.      ** the W
2a8e5 4f 5f 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4e  O_ and SQLITE_IN
2a8e6 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 20  DEX_CONSTRAINT_ 
2a8e7 63 6f 64 65 73 20 61 72 65 20 69 64 65 6e 74 69  codes are identi
2a8e8 63 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20  cal.  The.      
2a8e9 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73  ** following ass
2a8ea 65 72 74 73 20 76 65 72 69 66 79 20 74 68 69 73  erts verify this
2a8eb 20 66 61 63 74 2e 20 2a 2f 0a 20 20 20 20 20 20   fact. */.      
2a8ec 61 73 73 65 72 74 28 20 57 4f 5f 45 51 3d 3d 53  assert( WO_EQ==S
2a8ed 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
2a8ee 54 52 41 49 4e 54 5f 45 51 20 29 3b 0a 20 20 20  TRAINT_EQ );.   
2a8ef 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4c 54     assert( WO_LT
2a8f0 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
2a8f1 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 29 3b 0a  ONSTRAINT_LT );.
2a8f2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 57 4f        assert( WO
2a8f3 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  _LE==SQLITE_INDE
2a8f4 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20  X_CONSTRAINT_LE 
2a8f5 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2a8f6 20 57 4f 5f 47 54 3d 3d 53 51 4c 49 54 45 5f 49   WO_GT==SQLITE_I
2a8f7 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
2a8f8 47 54 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  GT );.      asse
2a8f9 72 74 28 20 57 4f 5f 47 45 3d 3d 53 51 4c 49 54  rt( WO_GE==SQLIT
2a8fa 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
2a8fb 4e 54 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 61  NT_GE );.      a
2a8fc 73 73 65 72 74 28 20 57 4f 5f 4d 41 54 43 48 3d  ssert( WO_MATCH=
2a8fd 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f  =SQLITE_INDEX_CO
2a8fe 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20 29  NSTRAINT_MATCH )
2a8ff 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2a900 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2a901 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c   & (WO_EQ|WO_LT|
2a902 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47  WO_LE|WO_GT|WO_G
2a903 45 7c 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0a 20  E|WO_MATCH) );. 
2a904 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 7d 0a       j++;.    }.
2a905 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
2a906 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20  OrderBy; i++){. 
2a907 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
2a908 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69   = pOrderBy->a[i
2a909 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 70  ].pExpr;.      p
2a90a 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43  IdxOrderBy[i].iC
2a90b 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69  olumn = pExpr->i
2a90c 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 49  Column;.      pI
2a90d 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65 73  dxOrderBy[i].des
2a90e 63 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  c = pOrderBy->a[
2a90f 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  i].sortOrder;.  
2a910 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74    }.  }..  /* At
2a911 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
2a912 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2a913 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68  nfo structure th
2a914 61 74 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e  at pIdxInfo poin
2a915 74 73 0a 20 20 2a 2a 20 74 6f 20 77 69 6c 6c 20  ts.  ** to will 
2a916 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61  have been initia
2a917 6c 69 7a 65 64 2c 20 65 69 74 68 65 72 20 64 75  lized, either du
2a918 72 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74  ring the current
2a919 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 72 0a 20   invocation or. 
2a91a 20 2a 2a 20 64 75 72 69 6e 67 20 73 6f 6d 65 20   ** during some 
2a91b 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e  prior invocation
2a91c 2e 20 20 4e 6f 77 20 77 65 20 6a 75 73 74 20 68  .  Now we just h
2a91d 61 76 65 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65  ave to customize
2a91e 20 74 68 65 0a 20 20 2a 2a 20 64 65 74 61 69 6c   the.  ** detail
2a91f 73 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f  s of pIdxInfo fo
2a920 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 6e  r the current in
2a921 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 73  vocation and pas
2a922 73 20 69 74 20 74 6f 0a 20 20 2a 2a 20 78 42 65  s it to.  ** xBe
2a923 73 74 49 6e 64 65 78 2e 0a 20 20 2a 2f 0a 0a 20  stIndex..  */.. 
2a924 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e   /* The module n
2a925 61 6d 65 20 6d 75 73 74 20 62 65 20 64 65 66 69  ame must be defi
2a926 6e 65 64 2e 20 41 6c 73 6f 2c 20 62 79 20 74 68  ned. Also, by th
2a927 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d  is point there m
2a928 75 73 74 0a 20 20 2a 2a 20 62 65 20 61 20 70 6f  ust.  ** be a po
2a929 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69  inter to an sqli
2a92a 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
2a92b 72 65 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20  re. Otherwise.  
2a92c 2a 2a 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  ** sqlite3ViewGe
2a92d 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 77  tColumnNames() w
2a92e 6f 75 6c 64 20 68 61 76 65 20 70 69 63 6b 65 64  ould have picked
2a92f 20 75 70 20 74 68 65 20 65 72 72 6f 72 2e 20 0a   up the error. .
2a930 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
2a931 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  Tab->azModuleArg
2a932 20 26 26 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75   && pTab->azModu
2a933 6c 65 41 72 67 5b 30 5d 20 29 3b 0a 20 20 61 73  leArg[0] );.  as
2a934 73 65 72 74 28 20 70 56 74 61 62 20 29 3b 0a 23  sert( pVtab );.#
2a935 69 66 20 30 0a 20 20 69 66 28 20 70 54 61 62 2d  if 0.  if( pTab-
2a936 3e 70 56 74 61 62 3d 3d 30 20 29 7b 0a 20 20 20  >pVtab==0 ){.   
2a937 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2a938 28 70 50 61 72 73 65 2c 20 22 75 6e 64 65 66 69  (pParse, "undefi
2a939 6e 65 64 20 6d 6f 64 75 6c 65 20 25 73 20 66 6f  ned module %s fo
2a93a 72 20 74 61 62 6c 65 20 25 73 22 2c 0a 20 20 20  r table %s",.   
2a93b 20 20 20 20 20 70 54 61 62 2d 3e 61 7a 4d 6f 64       pTab->azMod
2a93c 75 6c 65 41 72 67 5b 30 5d 2c 20 70 54 61 62 2d  uleArg[0], pTab-
2a93d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  >zName);.    ret
2a93e 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 23 65 6e  urn 0.0;.  }.#en
2a93f 64 69 66 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68  dif..  /* Set th
2a940 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e  e aConstraint[].
2a941 75 73 61 62 6c 65 20 66 69 65 6c 64 73 20 61 6e  usable fields an
2a942 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6c 6c  d initialize all
2a943 20 0a 20 20 2a 2a 20 6f 75 74 70 75 74 20 76 61   .  ** output va
2a944 72 69 61 62 6c 65 73 20 74 6f 20 7a 65 72 6f 2e  riables to zero.
2a945 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 43 6f 6e 73  .  **.  ** aCons
2a946 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20  traint[].usable 
2a947 69 73 20 74 72 75 65 20 66 6f 72 20 63 6f 6e 73  is true for cons
2a948 74 72 61 69 6e 74 73 20 77 68 65 72 65 20 74 68  traints where th
2a949 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 20 20 2a  e right-hand.  *
2a94a 2a 20 73 69 64 65 20 63 6f 6e 74 61 69 6e 73 20  * side contains 
2a94b 6f 6e 6c 79 20 72 65 66 65 72 65 6e 63 65 73 20  only references 
2a94c 74 6f 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65  to tables to the
2a94d 20 6c 65 66 74 20 6f 66 20 74 68 65 20 63 75 72   left of the cur
2a94e 72 65 6e 74 0a 20 20 2a 2a 20 74 61 62 6c 65 2e  rent.  ** table.
2a94f 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
2a950 2c 20 69 66 20 74 68 65 20 63 6f 6e 73 74 72 61  , if the constra
2a951 69 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f  int is of the fo
2a952 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  rm:.  **.  **   
2a953 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20 3d          column =
2a954 20 65 78 70 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20   expr.  **.  ** 
2a955 61 6e 64 20 77 65 20 61 72 65 20 65 76 61 6c 75  and we are evalu
2a956 61 74 69 6e 67 20 61 20 6a 6f 69 6e 2c 20 74 68  ating a join, th
2a957 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  en the constrain
2a958 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20 69 73 20 0a  t on column is .
2a959 20 20 2a 2a 20 6f 6e 6c 79 20 76 61 6c 69 64 20    ** only valid 
2a95a 69 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 72 65  if all tables re
2a95b 66 65 72 65 6e 63 65 64 20 69 6e 20 65 78 70 72  ferenced in expr
2a95c 20 6f 63 63 75 72 20 74 6f 20 74 68 65 20 6c 65   occur to the le
2a95d 66 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74  ft.  ** of the t
2a95e 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
2a95f 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2a 0a 20 20 2a  column..  **.  *
2a960 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e  * The aConstrain
2a961 74 73 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74 61  ts[] array conta
2a962 69 6e 73 20 65 6e 74 72 69 65 73 20 66 6f 72 20  ins entries for 
2a963 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  all constraints.
2a964 20 20 2a 2a 20 6f 6e 20 74 68 65 20 63 75 72 72    ** on the curr
2a965 65 6e 74 20 74 61 62 6c 65 2e 20 20 54 68 61 74  ent table.  That
2a966 20 77 61 79 20 77 65 20 6f 6e 6c 79 20 68 61 76   way we only hav
2a967 65 20 74 6f 20 63 6f 6d 70 75 74 65 20 69 74 20  e to compute it 
2a968 6f 6e 63 65 0a 20 20 2a 2a 20 65 76 65 6e 20 74  once.  ** even t
2a969 68 6f 75 67 68 20 77 65 20 6d 69 67 68 74 20 74  hough we might t
2a96a 72 79 20 74 6f 20 70 69 63 6b 20 74 68 65 20 62  ry to pick the b
2a96b 65 73 74 20 69 6e 64 65 78 20 6d 75 6c 74 69 70  est index multip
2a96c 6c 65 20 74 69 6d 65 73 2e 0a 20 20 2a 2a 20 46  le times..  ** F
2a96d 6f 72 20 65 61 63 68 20 61 74 74 65 6d 70 74 20  or each attempt 
2a96e 61 74 20 70 69 63 6b 69 6e 67 20 61 6e 20 69 6e  at picking an in
2a96f 64 65 78 2c 20 74 68 65 20 6f 72 64 65 72 20 6f  dex, the order o
2a970 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 0a  f tables in the.
2a971 20 20 2a 2a 20 6a 6f 69 6e 20 6d 69 67 68 74 20    ** join might 
2a972 62 65 20 64 69 66 66 65 72 65 6e 74 20 73 6f 20  be different so 
2a973 77 65 20 68 61 76 65 20 74 6f 20 72 65 63 6f 6d  we have to recom
2a974 70 75 74 65 20 74 68 65 20 75 73 61 62 6c 65 20  pute the usable 
2a975 66 6c 61 67 0a 20 20 2a 2a 20 65 61 63 68 20 74  flag.  ** each t
2a976 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78  ime..  */.  pIdx
2a977 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63 74 20  Cons = *(struct 
2a978 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
2a979 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78  nstraint**)&pIdx
2a97a 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
2a97b 74 3b 0a 20 20 70 55 73 61 67 65 20 3d 20 70 49  t;.  pUsage = pI
2a97c 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61  dxInfo->aConstra
2a97d 69 6e 74 55 73 61 67 65 3b 0a 20 20 66 6f 72 28  intUsage;.  for(
2a97e 69 3d 30 3b 20 69 3c 70 49 64 78 49 6e 66 6f 2d  i=0; i<pIdxInfo-
2a97f 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b  >nConstraint; i+
2a980 2b 2c 20 70 49 64 78 43 6f 6e 73 2b 2b 29 7b 0a  +, pIdxCons++){.
2a981 20 20 20 20 6a 20 3d 20 70 49 64 78 43 6f 6e 73      j = pIdxCons
2a982 2d 3e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20  ->iTermOffset;. 
2a983 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d     pTerm = &pWC-
2a984 3e 61 5b 6a 5d 3b 0a 20 20 20 20 70 49 64 78 43  >a[j];.    pIdxC
2a985 6f 6e 73 2d 3e 75 73 61 62 6c 65 20 3d 20 20 28  ons->usable =  (
2a986 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
2a987 68 74 20 26 20 6e 6f 74 52 65 61 64 79 29 3d 3d  ht & notReady)==
2a988 30 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 6d 65  0 ?1:0;.  }.  me
2a989 6d 73 65 74 28 70 55 73 61 67 65 2c 20 30 2c 20  mset(pUsage, 0, 
2a98a 73 69 7a 65 6f 66 28 70 55 73 61 67 65 5b 30 5d  sizeof(pUsage[0]
2a98b 29 2a 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e  )*pIdxInfo->nCon
2a98c 73 74 72 61 69 6e 74 29 3b 0a 20 20 69 66 28 20  straint);.  if( 
2a98d 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f  pIdxInfo->needTo
2a98e 46 72 65 65 49 64 78 53 74 72 20 29 7b 0a 20 20  FreeIdxStr ){.  
2a98f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
2a990 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 29  IdxInfo->idxStr)
2a991 3b 0a 20 20 7d 0a 20 20 70 49 64 78 49 6e 66 6f  ;.  }.  pIdxInfo
2a992 2d 3e 69 64 78 53 74 72 20 3d 20 30 3b 0a 20 20  ->idxStr = 0;.  
2a993 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d  pIdxInfo->idxNum
2a994 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f   = 0;.  pIdxInfo
2a995 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  ->needToFreeIdxS
2a996 74 72 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e  tr = 0;.  pIdxIn
2a997 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75  fo->orderByConsu
2a998 6d 65 64 20 3d 20 30 3b 0a 20 20 70 49 64 78 49  med = 0;.  pIdxI
2a999 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
2a99a 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f  st = SQLITE_BIG_
2a99b 44 42 4c 20 2f 20 32 2e 30 3b 0a 20 20 6e 4f 72  DBL / 2.0;.  nOr
2a99c 64 65 72 42 79 20 3d 20 70 49 64 78 49 6e 66 6f  derBy = pIdxInfo
2a99d 2d 3e 6e 4f 72 64 65 72 42 79 3b 0a 20 20 69 66  ->nOrderBy;.  if
2a99e 28 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64  ( pIdxInfo->nOrd
2a99f 65 72 42 79 20 26 26 20 21 6f 72 64 65 72 42 79  erBy && !orderBy
2a9a0 55 73 61 62 6c 65 20 29 7b 0a 20 20 20 20 2a 28  Usable ){.    *(
2a9a1 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  int*)&pIdxInfo->
2a9a2 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  nOrderBy = 0;.  
2a9a3 7d 0a 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  }..  (void)sqlit
2a9a4 65 33 53 61 66 65 74 79 4f 66 66 28 70 50 61 72  e3SafetyOff(pPar
2a9a5 73 65 2d 3e 64 62 29 3b 0a 20 20 57 48 45 52 45  se->db);.  WHERE
2a9a6 54 52 41 43 45 28 28 22 78 42 65 73 74 49 6e 64  TRACE(("xBestInd
2a9a7 65 78 20 66 6f 72 20 25 73 5c 6e 22 2c 20 70 54  ex for %s\n", pT
2a9a8 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 54  ab->zName));.  T
2a9a9 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28  RACE_IDX_INPUTS(
2a9aa 70 49 64 78 49 6e 66 6f 29 3b 0a 20 20 72 63 20  pIdxInfo);.  rc 
2a9ab 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  = pVtab->pModule
2a9ac 2d 3e 78 42 65 73 74 49 6e 64 65 78 28 70 56 74  ->xBestIndex(pVt
2a9ad 61 62 2c 20 70 49 64 78 49 6e 66 6f 29 3b 0a 20  ab, pIdxInfo);. 
2a9ae 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55   TRACE_IDX_OUTPU
2a9af 54 53 28 70 49 64 78 49 6e 66 6f 29 3b 0a 20 20  TS(pIdxInfo);.  
2a9b0 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
2a9b1 65 74 79 4f 6e 28 70 50 61 72 73 65 2d 3e 64 62  etyOn(pParse->db
2a9b2 29 3b 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51  );..  if( rc!=SQ
2a9b3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
2a9b4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
2a9b5 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 70 50 61  MEM ){.      pPa
2a9b6 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
2a9b7 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d  ailed = 1;.    }
2a9b8 65 6c 73 65 20 69 66 28 20 21 70 56 74 61 62 2d  else if( !pVtab-
2a9b9 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  >zErrMsg ){.    
2a9ba 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2a9bb 67 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20  g(pParse, "%s", 
2a9bc 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63  sqlite3ErrStr(rc
2a9bd 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ));.    }else{. 
2a9be 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2a9bf 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73  rMsg(pParse, "%s
2a9c0 22 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  ", pVtab->zErrMs
2a9c1 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  g);.    }.  }.  
2a9c2 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50  sqlite3DbFree(pP
2a9c3 61 72 73 65 2d 3e 64 62 2c 20 70 56 74 61 62 2d  arse->db, pVtab-
2a9c4 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 56 74  >zErrMsg);.  pVt
2a9c5 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
2a9c6 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
2a9c7 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72  IdxInfo->nConstr
2a9c8 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  aint; i++){.    
2a9c9 69 66 28 20 21 70 49 64 78 49 6e 66 6f 2d 3e 61  if( !pIdxInfo->a
2a9ca 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73  Constraint[i].us
2a9cb 61 62 6c 65 20 26 26 20 70 55 73 61 67 65 5b 69  able && pUsage[i
2a9cc 5d 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29 7b  ].argvIndex>0 ){
2a9cd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2a9ce 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
2a9cf 20 20 20 20 20 20 20 20 20 20 22 74 61 62 6c 65            "table
2a9d0 20 25 73 3a 20 78 42 65 73 74 49 6e 64 65 78 20   %s: xBestIndex 
2a9d1 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6e 76 61  returned an inva
2a9d2 6c 69 64 20 70 6c 61 6e 22 2c 20 70 54 61 62 2d  lid plan", pTab-
2a9d3 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72  >zName);.      r
2a9d4 65 74 75 72 6e 20 30 2e 30 3b 0a 20 20 20 20 7d  eturn 0.0;.    }
2a9d5 0a 20 20 7d 0a 0a 20 20 2a 28 69 6e 74 2a 29 26  .  }..  *(int*)&
2a9d6 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72  pIdxInfo->nOrder
2a9d7 42 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 20  By = nOrderBy;. 
2a9d8 20 72 65 74 75 72 6e 20 70 49 64 78 49 6e 66 6f   return pIdxInfo
2a9d9 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b  ->estimatedCost;
2a9da 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
2a9db 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2a9dc 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  TABLE */../*.** 
2a9dd 46 69 6e 64 20 74 68 65 20 71 75 65 72 79 20 70  Find the query p
2a9de 6c 61 6e 20 66 6f 72 20 61 63 63 65 73 73 69 6e  lan for accessin
2a9df 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74  g a particular t
2a9e0 61 62 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65  able.  Write the
2a9e1 0a 2a 2a 20 62 65 73 74 20 71 75 65 72 79 20 70  .** best query p
2a9e2 6c 61 6e 20 61 6e 64 20 69 74 73 20 63 6f 73 74  lan and its cost
2a9e3 20 69 6e 74 6f 20 74 68 65 20 57 68 65 72 65 43   into the WhereC
2a9e4 6f 73 74 20 6f 62 6a 65 63 74 20 73 75 70 70 6c  ost object suppl
2a9e5 69 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 6c 61  ied as the.** la
2a9e6 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  st parameter..**
2a9e7 0a 2a 2a 20 54 68 65 20 6c 6f 77 65 73 74 20 63  .** The lowest c
2a9e8 6f 73 74 20 70 6c 61 6e 20 77 69 6e 73 2e 20 20  ost plan wins.  
2a9e9 54 68 65 20 63 6f 73 74 20 69 73 20 61 6e 20 65  The cost is an e
2a9ea 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 61  stimate of the a
2a9eb 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20 43 50 55 20  mount of.** CPU 
2a9ec 61 6e 64 20 64 69 73 6b 20 49 2f 4f 20 6e 65 65  and disk I/O nee
2a9ed 64 20 74 6f 20 70 72 6f 63 65 73 73 20 74 68 65  d to process the
2a9ee 20 72 65 71 75 65 73 74 20 75 73 69 6e 67 20 74   request using t
2a9ef 68 65 20 73 65 6c 65 63 74 65 64 20 70 6c 61 6e  he selected plan
2a9f0 2e 0a 2a 2a 20 46 61 63 74 6f 72 73 20 74 68 61  ..** Factors tha
2a9f1 74 20 69 6e 66 6c 75 65 6e 63 65 20 63 6f 73 74  t influence cost
2a9f2 20 69 6e 63 6c 75 64 65 3a 0a 2a 2a 0a 2a 2a 20   include:.**.** 
2a9f3 20 20 20 2a 20 20 54 68 65 20 65 73 74 69 6d 61     *  The estima
2a9f4 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  ted number of ro
2a9f5 77 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ws that will be 
2a9f6 72 65 74 72 69 65 76 65 64 2e 20 20 28 54 68 65  retrieved.  (The
2a9f7 0a 2a 2a 20 20 20 20 20 20 20 66 65 77 65 72 20  .**       fewer 
2a9f8 74 68 65 20 62 65 74 74 65 72 2e 29 0a 2a 2a 0a  the better.).**.
2a9f9 2a 2a 20 20 20 20 2a 20 20 57 68 65 74 68 65 72  **    *  Whether
2a9fa 20 6f 72 20 6e 6f 74 20 73 6f 72 74 69 6e 67 20   or not sorting 
2a9fb 6d 75 73 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a  must occur..**.*
2a9fc 2a 20 20 20 20 2a 20 20 57 68 65 74 68 65 72 20  *    *  Whether 
2a9fd 6f 72 20 6e 6f 74 20 74 68 65 72 65 20 6d 75 73  or not there mus
2a9fe 74 20 62 65 20 73 65 70 61 72 61 74 65 20 6c 6f  t be separate lo
2a9ff 6f 6b 75 70 73 20 69 6e 20 74 68 65 0a 2a 2a 20  okups in the.** 
2aa00 20 20 20 20 20 20 69 6e 64 65 78 20 61 6e 64 20        index and 
2aa01 69 6e 20 74 68 65 20 6d 61 69 6e 20 74 61 62 6c  in the main tabl
2aa02 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  e..**.** If ther
2aa03 65 20 77 61 73 20 61 6e 20 49 4e 44 45 58 45 44  e was an INDEXED
2aa04 20 42 59 20 63 6c 61 75 73 65 20 61 74 74 61 63   BY clause attac
2aa05 68 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65  hed to the table
2aa06 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 2a   in the SELECT.*
2aa07 2a 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  * statement, the
2aa08 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
2aa09 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72 73 20 70  only considers p
2aa0a 6c 61 6e 73 20 75 73 69 6e 67 20 74 68 65 20 0a  lans using the .
2aa0b 2a 2a 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20  ** named index. 
2aa0c 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65  If one cannot be
2aa0d 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68 65   found, then the
2aa0e 20 72 65 74 75 72 6e 65 64 20 63 6f 73 74 20 69   returned cost i
2aa0f 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 49 47 5f  s.** SQLITE_BIG_
2aa10 44 42 4c 2e 20 49 66 20 61 20 70 6c 61 6e 20 63  DBL. If a plan c
2aa11 61 6e 20 62 65 20 66 6f 75 6e 64 20 74 68 61 74  an be found that
2aa12 20 75 73 65 73 20 74 68 65 20 6e 61 6d 65 64 20   uses the named 
2aa13 69 6e 64 65 78 2c 20 0a 2a 2a 20 74 68 65 6e 20  index, .** then 
2aa14 74 68 65 20 63 6f 73 74 20 69 73 20 63 61 6c 63  the cost is calc
2aa15 75 6c 61 74 65 64 20 69 6e 20 74 68 65 20 75 73  ulated in the us
2aa16 75 61 6c 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 49  ual way..**.** I
2aa17 66 20 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20  f a NOT INDEXED 
2aa18 63 6c 61 75 73 65 20 77 61 73 20 61 74 74 61 63  clause was attac
2aa19 68 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65  hed to the table
2aa1a 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 0a   in the SELECT .
2aa1b 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  ** statement, th
2aa1c 65 6e 20 6e 6f 20 69 6e 64 65 78 65 73 20 61 72  en no indexes ar
2aa1d 65 20 63 6f 6e 73 69 64 65 72 65 64 2e 20 48 6f  e considered. Ho
2aa1e 77 65 76 65 72 2c 20 74 68 65 20 73 65 6c 65 63  wever, the selec
2aa1f 74 65 64 20 0a 2a 2a 20 70 6c 61 6e 20 6d 61 79  ted .** plan may
2aa20 20 73 74 69 6c 6c 20 74 61 6b 65 20 61 64 76 61   still take adva
2aa21 6e 74 61 67 65 20 6f 66 20 74 68 65 20 74 61 62  ntage of the tab
2aa22 6c 65 73 20 62 75 69 6c 74 2d 69 6e 20 72 6f 77  les built-in row
2aa23 69 64 0a 2a 2a 20 69 6e 64 65 78 2e 0a 2a 2f 0a  id.** index..*/.
2aa24 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73 74  static void best
2aa25 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a  Index(.  Parse *
2aa26 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2aa27 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2aa28 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2aa29 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2aa2a 43 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  C,           /* 
2aa2b 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2aa2c 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2aa2d 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c  List_item *pSrc,
2aa2e 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
2aa2f 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61  ause term to sea
2aa30 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  rch */.  Bitmask
2aa31 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20   notReady,      
2aa32 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
2aa33 63 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65  cursors that are
2aa34 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a   not available *
2aa35 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
2aa36 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20 20  rderBy,         
2aa37 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
2aa38 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72  clause */.  Wher
2aa39 65 43 6f 73 74 20 2a 70 43 6f 73 74 20 20 20 20  eCost *pCost    
2aa3a 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73          /* Lowes
2aa3b 74 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61  t cost query pla
2aa3c 6e 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54  n */.){.  WhereT
2aa3d 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20  erm *pTerm;     
2aa3e 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c        /* A singl
2aa3f 65 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48  e term of the WH
2aa40 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2aa41 69 6e 74 20 69 43 75 72 20 3d 20 70 53 72 63 2d  int iCur = pSrc-
2aa42 3e 69 43 75 72 73 6f 72 3b 20 20 20 2f 2a 20 54  >iCursor;   /* T
2aa43 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68 65  he cursor of the
2aa44 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63   table to be acc
2aa45 65 73 73 65 64 20 2a 2f 0a 20 20 49 6e 64 65 78  essed */.  Index
2aa46 20 2a 70 50 72 6f 62 65 3b 20 20 20 20 20 20 20   *pProbe;       
2aa47 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64         /* An ind
2aa48 65 78 20 77 65 20 61 72 65 20 65 76 61 6c 75 61  ex we are evalua
2aa49 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ting */.  int re
2aa4a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
2aa4b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
2aa4c 20 73 63 61 6e 20 69 6e 20 72 65 76 65 72 73 65   scan in reverse
2aa4d 20 6f 72 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20   order */.  int 
2aa4e 77 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  wsFlags;        
2aa4f 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73          /* Flags
2aa50 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2aa51 20 70 50 72 6f 62 65 20 2a 2f 0a 20 20 69 6e 74   pProbe */.  int
2aa52 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20 20 20   nEq;           
2aa53 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2aa54 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63  er of == or IN c
2aa55 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
2aa56 69 6e 74 20 65 71 54 65 72 6d 4d 61 73 6b 3b 20  int eqTermMask; 
2aa57 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
2aa58 61 73 6b 20 6f 66 20 76 61 6c 69 64 20 65 71 75  ask of valid equ
2aa59 61 6c 69 74 79 20 6f 70 65 72 61 74 6f 72 73 20  ality operators 
2aa5a 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 63 6f 73 74  */.  double cost
2aa5b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2aa5c 20 2f 2a 20 43 6f 73 74 20 6f 66 20 75 73 69 6e   /* Cost of usin
2aa5d 67 20 70 50 72 6f 62 65 20 2a 2f 0a 20 20 64 6f  g pProbe */.  do
2aa5e 75 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20 20 20  uble nRow;      
2aa5f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 73 74            /* Est
2aa60 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66  imated number of
2aa61 20 72 6f 77 73 20 69 6e 20 72 65 73 75 6c 74 20   rows in result 
2aa62 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  set */.  int i; 
2aa63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa64 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2aa65 6e 74 65 72 20 2a 2f 0a 20 20 42 69 74 6d 61 73  nter */.  Bitmas
2aa66 6b 20 6d 61 73 6b 53 72 63 3b 20 20 20 20 20 20  k maskSrc;      
2aa67 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b        /* Bitmask
2aa68 20 66 6f 72 20 74 68 65 20 70 53 72 63 20 74 61   for the pSrc ta
2aa69 62 6c 65 20 2a 2f 0a 0a 20 20 57 48 45 52 45 54  ble */..  WHERET
2aa6a 52 41 43 45 28 28 22 62 65 73 74 49 6e 64 65 78  RACE(("bestIndex
2aa6b 3a 20 74 62 6c 3d 25 73 20 6e 6f 74 52 65 61 64  : tbl=%s notRead
2aa6c 79 3d 25 6c 6c 78 5c 6e 22 2c 20 70 53 72 63 2d  y=%llx\n", pSrc-
2aa6d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 6e 6f 74  >pTab->zName,not
2aa6e 52 65 61 64 79 29 29 3b 0a 20 20 70 50 72 6f 62  Ready));.  pProb
2aa6f 65 20 3d 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e  e = pSrc->pTab->
2aa70 70 49 6e 64 65 78 3b 0a 20 20 69 66 28 20 70 53  pIndex;.  if( pS
2aa71 72 63 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 29  rc->notIndexed )
2aa72 7b 0a 20 20 20 20 70 50 72 6f 62 65 20 3d 20 30  {.    pProbe = 0
2aa73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
2aa74 68 65 20 74 61 62 6c 65 20 68 61 73 20 6e 6f 20  he table has no 
2aa75 69 6e 64 69 63 65 73 20 61 6e 64 20 74 68 65 72  indices and ther
2aa76 65 20 61 72 65 20 6e 6f 20 74 65 72 6d 73 20 69  e are no terms i
2aa77 6e 20 74 68 65 20 77 68 65 72 65 0a 20 20 2a 2a  n the where.  **
2aa78 20 63 6c 61 75 73 65 20 74 68 61 74 20 72 65 66   clause that ref
2aa79 65 72 20 74 6f 20 74 68 65 20 52 4f 57 49 44 2c  er to the ROWID,
2aa7a 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e 65   then we will ne
2aa7b 76 65 72 20 62 65 20 61 62 6c 65 20 74 6f 20 64  ver be able to d
2aa7c 6f 0a 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67 20  o.  ** anything 
2aa7d 6f 74 68 65 72 20 74 68 61 6e 20 61 20 66 75 6c  other than a ful
2aa7e 6c 20 74 61 62 6c 65 20 73 63 61 6e 20 6f 6e 20  l table scan on 
2aa7f 74 68 69 73 20 74 61 62 6c 65 2e 20 20 57 65 20  this table.  We 
2aa80 6d 69 67 68 74 20 61 73 0a 20 20 2a 2a 20 77 65  might as.  ** we
2aa81 6c 6c 20 70 75 74 20 69 74 20 66 69 72 73 74 20  ll put it first 
2aa82 69 6e 20 74 68 65 20 6a 6f 69 6e 20 6f 72 64 65  in the join orde
2aa83 72 2e 20 20 54 68 61 74 20 77 61 79 2c 20 70 65  r.  That way, pe
2aa84 72 68 61 70 73 20 69 74 20 63 61 6e 20 62 65 0a  rhaps it can be.
2aa85 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20    ** referenced 
2aa86 62 79 20 6f 74 68 65 72 20 74 61 62 6c 65 73 20  by other tables 
2aa87 69 6e 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a  in the join..  *
2aa88 2f 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 73 74  /.  memset(pCost
2aa89 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f  , 0, sizeof(*pCo
2aa8a 73 74 29 29 3b 0a 20 20 69 66 28 20 70 50 72 6f  st));.  if( pPro
2aa8b 62 65 3d 3d 30 20 26 26 0a 20 20 20 20 20 66 69  be==0 &&.     fi
2aa8c 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
2aa8d 2c 20 2d 31 2c 20 30 2c 20 57 4f 5f 45 51 7c 57  , -1, 0, WO_EQ|W
2aa8e 4f 5f 49 4e 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c 45  O_IN|WO_LT|WO_LE
2aa8f 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 30 29 3d  |WO_GT|WO_GE,0)=
2aa90 3d 30 20 26 26 0a 20 20 20 20 20 28 70 4f 72 64  =0 &&.     (pOrd
2aa91 65 72 42 79 3d 3d 30 20 7c 7c 20 21 73 6f 72 74  erBy==0 || !sort
2aa92 61 62 6c 65 42 79 52 6f 77 69 64 28 69 43 75 72  ableByRowid(iCur
2aa93 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 57 43 2d  , pOrderBy, pWC-
2aa94 3e 70 4d 61 73 6b 53 65 74 2c 20 26 72 65 76 29  >pMaskSet, &rev)
2aa95 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
2aa96 0a 20 20 7d 0a 20 20 70 43 6f 73 74 2d 3e 72 43  .  }.  pCost->rC
2aa97 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47  ost = SQLITE_BIG
2aa98 5f 44 42 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  _DBL;..  /* Chec
2aa99 6b 20 66 6f 72 20 61 20 72 6f 77 69 64 3d 45 58  k for a rowid=EX
2aa9a 50 52 20 6f 72 20 72 6f 77 69 64 20 49 4e 20 28  PR or rowid IN (
2aa9b 2e 2e 2e 29 20 63 6f 6e 73 74 72 61 69 6e 74 73  ...) constraints
2aa9c 2e 20 49 66 20 74 68 65 72 65 20 77 61 73 0a 20  . If there was. 
2aa9d 20 2a 2a 20 61 6e 20 49 4e 44 45 58 45 44 20 42   ** an INDEXED B
2aa9e 59 20 63 6c 61 75 73 65 20 61 74 74 61 63 68 65  Y clause attache
2aa9f 64 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 2c  d to this table,
2aaa0 20 73 6b 69 70 20 74 68 69 73 20 73 74 65 70 2e   skip this step.
2aaa1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 53 72  .  */.  if( !pSr
2aaa2 63 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  c->pIndex ){.   
2aaa3 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72   pTerm = findTer
2aaa4 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c  m(pWC, iCur, -1,
2aaa5 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 45 51   notReady, WO_EQ
2aaa6 7c 57 4f 5f 49 4e 2c 20 30 29 3b 0a 20 20 20 20  |WO_IN, 0);.    
2aaa7 69 66 28 20 70 54 65 72 6d 20 29 7b 0a 20 20 20  if( pTerm ){.   
2aaa8 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a     Expr *pExpr;.
2aaa9 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61        pCost->pla
2aaaa 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57 48 45 52  n.wsFlags = WHER
2aaab 45 5f 52 4f 57 49 44 5f 45 51 3b 0a 20 20 20 20  E_ROWID_EQ;.    
2aaac 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70    if( pTerm->eOp
2aaad 65 72 61 74 6f 72 20 26 20 57 4f 5f 45 51 20 29  erator & WO_EQ )
2aaae 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77  {.        /* Row
2aaaf 69 64 3d 3d 20 69 73 20 61 6c 77 61 79 73 20 74  id== is always t
2aab0 68 65 20 62 65 73 74 20 70 69 63 6b 2e 20 20 4c  he best pick.  L
2aab1 6f 6f 6b 20 6e 6f 20 66 75 72 74 68 65 72 2e 20  ook no further. 
2aab2 20 42 65 63 61 75 73 65 20 6f 6e 6c 79 0a 20 20   Because only.  
2aab3 20 20 20 20 20 20 2a 2a 20 61 20 73 69 6e 67 6c        ** a singl
2aab4 65 20 72 6f 77 20 69 73 20 67 65 6e 65 72 61 74  e row is generat
2aab5 65 64 2c 20 6f 75 74 70 75 74 20 69 73 20 61 6c  ed, output is al
2aab6 77 61 79 73 20 69 6e 20 73 6f 72 74 65 64 20 6f  ways in sorted o
2aab7 72 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  rder */.        
2aab8 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c  pCost->plan.wsFl
2aab9 61 67 73 20 3d 20 57 48 45 52 45 5f 52 4f 57 49  ags = WHERE_ROWI
2aaba 44 5f 45 51 20 7c 20 57 48 45 52 45 5f 55 4e 49  D_EQ | WHERE_UNI
2aabb 51 55 45 3b 0a 20 20 20 20 20 20 20 20 70 43 6f  QUE;.        pCo
2aabc 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20 31  st->plan.nEq = 1
2aabd 3b 0a 20 20 20 20 20 20 20 20 57 48 45 52 45 54  ;.        WHERET
2aabe 52 41 43 45 28 28 22 2e 2e 2e 20 62 65 73 74 20  RACE(("... best 
2aabf 69 73 20 72 6f 77 69 64 5c 6e 22 29 29 3b 0a 20  is rowid\n"));. 
2aac0 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 72 43         pCost->rC
2aac1 6f 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ost = 0;.       
2aac2 20 70 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 31   pCost->nRow = 1
2aac3 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2aac4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
2aac5 28 20 28 70 45 78 70 72 20 3d 20 70 54 65 72 6d  ( (pExpr = pTerm
2aac6 2d 3e 70 45 78 70 72 29 2d 3e 70 4c 69 73 74 21  ->pExpr)->pList!
2aac7 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
2aac8 20 52 6f 77 69 64 20 49 4e 20 28 4c 49 53 54 29   Rowid IN (LIST)
2aac9 3a 20 63 6f 73 74 20 69 73 20 4e 6c 6f 67 4e 20  : cost is NlogN 
2aaca 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e  where N is the n
2aacb 75 6d 62 65 72 20 6f 66 20 6c 69 73 74 0a 20 20  umber of list.  
2aacc 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74        ** element
2aacd 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70  s.  */.        p
2aace 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 70 43  Cost->rCost = pC
2aacf 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 70 45 78 70  ost->nRow = pExp
2aad0 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  r->pList->nExpr;
2aad1 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  .        pCost->
2aad2 72 43 6f 73 74 20 2a 3d 20 65 73 74 4c 6f 67 28  rCost *= estLog(
2aad3 70 43 6f 73 74 2d 3e 72 43 6f 73 74 29 3b 0a 20  pCost->rCost);. 
2aad4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2aad5 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 49 4e 20      /* Rowid IN 
2aad6 28 53 45 4c 45 43 54 29 3a 20 63 6f 73 74 20 69  (SELECT): cost i
2aad7 73 20 4e 6c 6f 67 4e 20 77 68 65 72 65 20 4e 20  s NlogN where N 
2aad8 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2aad9 20 72 6f 77 73 0a 20 20 20 20 20 20 20 20 2a 2a   rows.        **
2aada 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f   in the result o
2aadb 66 20 74 68 65 20 69 6e 6e 65 72 20 73 65 6c 65  f the inner sele
2aadc 63 74 2e 20 20 57 65 20 68 61 76 65 20 6e 6f 20  ct.  We have no 
2aadd 77 61 79 20 74 6f 20 65 73 74 69 6d 61 74 65 0a  way to estimate.
2aade 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20          ** that 
2aadf 76 61 6c 75 65 20 73 6f 20 6d 61 6b 65 20 61 20  value so make a 
2aae0 77 69 6c 64 20 67 75 65 73 73 2e 20 2a 2f 0a 20  wild guess. */. 
2aae1 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 6e 52         pCost->nR
2aae2 6f 77 20 3d 20 31 30 30 3b 0a 20 20 20 20 20 20  ow = 100;.      
2aae3 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d    pCost->rCost =
2aae4 20 32 30 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   200;.      }.  
2aae5 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
2aae6 22 2e 2e 2e 20 72 6f 77 69 64 20 49 4e 20 63 6f  "... rowid IN co
2aae7 73 74 3a 20 25 2e 39 67 5c 6e 22 2c 20 70 43 6f  st: %.9g\n", pCo
2aae8 73 74 2d 3e 72 43 6f 73 74 29 29 3b 0a 20 20 20  st->rCost));.   
2aae9 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 45 73 74   }.  .    /* Est
2aaea 69 6d 61 74 65 20 74 68 65 20 63 6f 73 74 20 6f  imate the cost o
2aaeb 66 20 61 20 74 61 62 6c 65 20 73 63 61 6e 2e 20  f a table scan. 
2aaec 20 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e   If we do not kn
2aaed 6f 77 20 68 6f 77 20 6d 61 6e 79 0a 20 20 20 20  ow how many.    
2aaee 2a 2a 20 65 6e 74 72 69 65 73 20 61 72 65 20 69  ** entries are i
2aaef 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 75 73 65  n the table, use
2aaf0 20 31 20 6d 69 6c 6c 69 6f 6e 20 61 73 20 61 20   1 million as a 
2aaf1 67 75 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  guess..    */.  
2aaf2 20 20 63 6f 73 74 20 3d 20 70 50 72 6f 62 65 20    cost = pProbe 
2aaf3 3f 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45  ? pProbe->aiRowE
2aaf4 73 74 5b 30 5d 20 3a 20 31 30 30 30 30 30 30 3b  st[0] : 1000000;
2aaf5 0a 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28  .    WHERETRACE(
2aaf6 28 22 2e 2e 2e 20 74 61 62 6c 65 20 73 63 61 6e  ("... table scan
2aaf7 20 62 61 73 65 20 63 6f 73 74 3a 20 25 2e 39 67   base cost: %.9g
2aaf8 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20  \n", cost));.   
2aaf9 20 77 73 46 6c 61 67 73 20 3d 20 57 48 45 52 45   wsFlags = WHERE
2aafa 5f 52 4f 57 49 44 5f 52 41 4e 47 45 3b 0a 20 20  _ROWID_RANGE;.  
2aafb 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f  .    /* Check fo
2aafc 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e  r constraints on
2aafd 20 61 20 72 61 6e 67 65 20 6f 66 20 72 6f 77 69   a range of rowi
2aafe 64 73 20 69 6e 20 61 20 74 61 62 6c 65 20 73 63  ds in a table sc
2aaff 61 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  an..    */.    p
2ab00 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  Term = findTerm(
2ab01 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e  pWC, iCur, -1, n
2ab02 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57  otReady, WO_LT|W
2ab03 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45  O_LE|WO_GT|WO_GE
2ab04 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 54  , 0);.    if( pT
2ab05 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 69 66 28  erm ){.      if(
2ab06 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2ab07 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64  Cur, -1, notRead
2ab08 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20  y, WO_LT|WO_LE, 
2ab09 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 77 73  0) ){.        ws
2ab0a 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 54  Flags |= WHERE_T
2ab0b 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20  OP_LIMIT;.      
2ab0c 20 20 63 6f 73 74 20 2f 3d 20 33 3b 20 20 2f 2a    cost /= 3;  /*
2ab0d 20 47 75 65 73 73 20 74 68 61 74 20 72 6f 77 69   Guess that rowi
2ab0e 64 3c 45 58 50 52 20 65 6c 69 6d 69 6e 61 74 65  d<EXPR eliminate
2ab0f 73 20 74 77 6f 2d 74 68 69 72 64 73 20 6f 66 20  s two-thirds of 
2ab10 72 6f 77 73 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  rows */.      }.
2ab11 20 20 20 20 20 20 69 66 28 20 66 69 6e 64 54 65        if( findTe
2ab12 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31  rm(pWC, iCur, -1
2ab13 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47  , notReady, WO_G
2ab14 54 7c 57 4f 5f 47 45 2c 20 30 29 20 29 7b 0a 20  T|WO_GE, 0) ){. 
2ab15 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c         wsFlags |
2ab16 3d 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49  = WHERE_BTM_LIMI
2ab17 54 3b 0a 20 20 20 20 20 20 20 20 63 6f 73 74 20  T;.        cost 
2ab18 2f 3d 20 33 3b 20 20 2f 2a 20 47 75 65 73 73 20  /= 3;  /* Guess 
2ab19 74 68 61 74 20 72 6f 77 69 64 3e 45 58 50 52 20  that rowid>EXPR 
2ab1a 65 6c 69 6d 69 6e 61 74 65 73 20 74 77 6f 2d 74  eliminates two-t
2ab1b 68 69 72 64 73 20 6f 66 20 72 6f 77 73 20 2a 2f  hirds of rows */
2ab1c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 57  .      }.      W
2ab1d 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
2ab1e 72 6f 77 69 64 20 72 61 6e 67 65 20 72 65 64 75  rowid range redu
2ab1f 63 65 73 20 63 6f 73 74 20 74 6f 20 25 2e 39 67  ces cost to %.9g
2ab20 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20  \n", cost));.   
2ab21 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 73   }else{.      ws
2ab22 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 7d  Flags = 0;.    }
2ab23 0a 20 20 20 20 6e 52 6f 77 20 3d 20 63 6f 73 74  .    nRow = cost
2ab24 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74  ;.  .    /* If t
2ab25 68 65 20 74 61 62 6c 65 20 73 63 61 6e 20 64 6f  he table scan do
2ab26 65 73 20 6e 6f 74 20 73 61 74 69 73 66 79 20 74  es not satisfy t
2ab27 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2ab28 73 65 2c 20 69 6e 63 72 65 61 73 65 0a 20 20 20  se, increase.   
2ab29 20 2a 2a 20 74 68 65 20 63 6f 73 74 20 62 79 20   ** the cost by 
2ab2a 4e 6c 6f 67 4e 20 74 6f 20 63 6f 76 65 72 20 74  NlogN to cover t
2ab2b 68 65 20 65 78 70 65 6e 73 65 20 6f 66 20 73 6f  he expense of so
2ab2c 72 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66  rting. */.    if
2ab2d 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ( pOrderBy ){.  
2ab2e 20 20 20 20 69 66 28 20 73 6f 72 74 61 62 6c 65      if( sortable
2ab2f 42 79 52 6f 77 69 64 28 69 43 75 72 2c 20 70 4f  ByRowid(iCur, pO
2ab30 72 64 65 72 42 79 2c 20 70 57 43 2d 3e 70 4d 61  rderBy, pWC->pMa
2ab31 73 6b 53 65 74 2c 20 26 72 65 76 29 20 29 7b 0a  skSet, &rev) ){.
2ab32 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
2ab33 7c 3d 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  |= WHERE_ORDERBY
2ab34 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e  |WHERE_ROWID_RAN
2ab35 47 45 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  GE;.        if( 
2ab36 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20  rev ){.         
2ab37 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2ab38 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 20 20 20  E_REVERSE;.     
2ab39 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
2ab3a 7b 0a 20 20 20 20 20 20 20 20 63 6f 73 74 20 2b  {.        cost +
2ab3b 3d 20 63 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f  = cost*estLog(co
2ab3c 73 74 29 3b 0a 20 20 20 20 20 20 20 20 57 48 45  st);.        WHE
2ab3d 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 73 6f  RETRACE(("... so
2ab3e 72 74 69 6e 67 20 69 6e 63 72 65 61 73 65 73 20  rting increases 
2ab3f 63 6f 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c  cost to %.9g\n",
2ab40 20 63 6f 73 74 29 29 3b 0a 20 20 20 20 20 20 7d   cost));.      }
2ab41 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63  .    }.    if( c
2ab42 6f 73 74 3c 70 43 6f 73 74 2d 3e 72 43 6f 73 74  ost<pCost->rCost
2ab43 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d   ){.      pCost-
2ab44 3e 72 43 6f 73 74 20 3d 20 63 6f 73 74 3b 0a 20  >rCost = cost;. 
2ab45 20 20 20 20 20 70 43 6f 73 74 2d 3e 6e 52 6f 77       pCost->nRow
2ab46 20 3d 20 6e 52 6f 77 3b 0a 20 20 20 20 20 20 70   = nRow;.      p
2ab47 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  Cost->plan.wsFla
2ab48 67 73 20 3d 20 77 73 46 6c 61 67 73 3b 0a 20 20  gs = wsFlags;.  
2ab49 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66    }.  }..#ifndef
2ab4a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f   SQLITE_OMIT_OR_
2ab4b 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f  OPTIMIZATION.  /
2ab4c 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 6e 20  * Search for an 
2ab4d 4f 52 2d 63 6c 61 75 73 65 20 74 68 61 74 20 63  OR-clause that c
2ab4e 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6c 6f  an be used to lo
2ab4f 6f 6b 20 75 70 20 74 68 65 20 74 61 62 6c 65 2e  ok up the table.
2ab50 0a 20 20 2a 2f 0a 20 20 6d 61 73 6b 53 72 63 20  .  */.  maskSrc 
2ab51 3d 20 67 65 74 4d 61 73 6b 28 70 57 43 2d 3e 70  = getMask(pWC->p
2ab52 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 29 3b 0a  MaskSet, iCur);.
2ab53 20 20 66 6f 72 28 69 3d 30 2c 20 70 54 65 72 6d    for(i=0, pTerm
2ab54 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e  =pWC->a; i<pWC->
2ab55 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72  nTerm; i++, pTer
2ab56 6d 2b 2b 29 7b 0a 20 20 20 20 57 68 65 72 65 43  m++){.    WhereC
2ab57 6c 61 75 73 65 20 74 65 6d 70 57 43 3b 0a 20 20  lause tempWC;.  
2ab58 20 20 74 65 6d 70 57 43 20 3d 20 2a 70 57 43 3b    tempWC = *pWC;
2ab59 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e  .    if( pTerm->
2ab5a 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52  eOperator==WO_OR
2ab5b 20 0a 20 20 20 20 20 20 20 20 26 26 20 28 28 70   .        && ((p
2ab5c 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20  Term->prereqAll 
2ab5d 26 20 7e 6d 61 73 6b 53 72 63 29 20 26 20 6e 6f  & ~maskSrc) & no
2ab5e 74 52 65 61 64 79 29 3d 3d 30 0a 20 20 20 20 20  tReady)==0.     
2ab5f 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 75 2e     && (pTerm->u.
2ab60 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62  pOrInfo->indexab
2ab61 6c 65 20 26 20 6d 61 73 6b 53 72 63 29 21 3d 30  le & maskSrc)!=0
2ab62 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 43   ){.      WhereC
2ab63 6c 61 75 73 65 20 2a 70 4f 72 57 43 20 3d 20 26  lause *pOrWC = &
2ab64 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f  pTerm->u.pOrInfo
2ab65 2d 3e 77 63 3b 0a 20 20 20 20 20 20 57 68 65 72  ->wc;.      Wher
2ab66 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 0a  eTerm *pOrTerm;.
2ab67 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
2ab68 20 20 20 69 6e 74 20 73 6f 72 74 61 62 6c 65 20     int sortable 
2ab69 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c  = 0;.      doubl
2ab6a 65 20 72 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20  e rTotal = 0;.  
2ab6b 20 20 20 20 6e 52 6f 77 20 3d 20 30 3b 0a 20 20      nRow = 0;.  
2ab6c 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 4f 72      for(j=0, pOr
2ab6d 54 65 72 6d 3d 70 4f 72 57 43 2d 3e 61 3b 20 6a  Term=pOrWC->a; j
2ab6e 3c 70 4f 72 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a  <pOrWC->nTerm; j
2ab6f 2b 2b 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a  ++, pOrTerm++){.
2ab70 20 20 20 20 20 20 20 20 57 68 65 72 65 43 6f 73          WhereCos
2ab71 74 20 73 54 65 72 6d 43 6f 73 74 3b 0a 20 20 20  t sTermCost;.   
2ab72 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
2ab73 28 22 2e 2e 2e 20 4d 75 6c 74 69 2d 69 6e 64 65  ("... Multi-inde
2ab74 78 20 4f 52 20 74 65 73 74 69 6e 67 20 66 6f 72  x OR testing for
2ab75 20 74 65 72 6d 20 25 64 20 6f 66 20 25 64 2e 2e   term %d of %d..
2ab76 2e 2e 5c 6e 22 2c 20 6a 2c 69 29 29 3b 0a 20 20  ..\n", j,i));.  
2ab77 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65 72        if( pOrTer
2ab78 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
2ab79 5f 41 4e 44 20 29 7b 0a 20 20 20 20 20 20 20 20  _AND ){.        
2ab7a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2ab7b 41 6e 64 57 43 20 3d 20 26 70 4f 72 54 65 72 6d  AndWC = &pOrTerm
2ab7c 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 2d 3e 77 63  ->u.pAndInfo->wc
2ab7d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 65 73 74  ;.          best
2ab7e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 41  Index(pParse, pA
2ab7f 6e 64 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52  ndWC, pSrc, notR
2ab80 65 61 64 79 2c 20 30 2c 20 26 73 54 65 72 6d 43  eady, 0, &sTermC
2ab81 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  ost);.        }e
2ab82 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d  lse if( pOrTerm-
2ab83 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
2ab84 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74  r ){.          t
2ab85 65 6d 70 57 43 2e 61 20 3d 20 70 4f 72 54 65 72  empWC.a = pOrTer
2ab86 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d  m;.          tem
2ab87 70 57 43 2e 6e 54 65 72 6d 20 3d 20 31 3b 0a 20  pWC.nTerm = 1;. 
2ab88 20 20 20 20 20 20 20 20 20 62 65 73 74 49 6e 64           bestInd
2ab89 65 78 28 70 50 61 72 73 65 2c 20 26 74 65 6d 70  ex(pParse, &temp
2ab8a 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65 61  WC, pSrc, notRea
2ab8b 64 79 2c 20 30 2c 20 26 73 54 65 72 6d 43 6f 73  dy, 0, &sTermCos
2ab8c 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  t);.        }els
2ab8d 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  e{.          con
2ab8e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d  tinue;.        }
2ab8f 0a 20 20 20 20 20 20 20 20 72 54 6f 74 61 6c 20  .        rTotal 
2ab90 2b 3d 20 73 54 65 72 6d 43 6f 73 74 2e 72 43 6f  += sTermCost.rCo
2ab91 73 74 3b 0a 20 20 20 20 20 20 20 20 6e 52 6f 77  st;.        nRow
2ab92 20 2b 3d 20 73 54 65 72 6d 43 6f 73 74 2e 6e 52   += sTermCost.nR
2ab93 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ow;.        if( 
2ab94 72 54 6f 74 61 6c 3e 3d 70 43 6f 73 74 2d 3e 72  rTotal>=pCost->r
2ab95 43 6f 73 74 20 29 20 62 72 65 61 6b 3b 0a 20 20  Cost ) break;.  
2ab96 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2ab97 70 4f 72 64 65 72 42 79 21 3d 30 20 29 7b 0a 20  pOrderBy!=0 ){. 
2ab98 20 20 20 20 20 20 20 69 66 28 20 73 6f 72 74 61         if( sorta
2ab99 62 6c 65 42 79 52 6f 77 69 64 28 69 43 75 72 2c  bleByRowid(iCur,
2ab9a 20 70 4f 72 64 65 72 42 79 2c 20 70 57 43 2d 3e   pOrderBy, pWC->
2ab9b 70 4d 61 73 6b 53 65 74 2c 20 26 72 65 76 29 20  pMaskSet, &rev) 
2ab9c 26 26 20 21 72 65 76 20 29 7b 0a 20 20 20 20 20  && !rev ){.     
2ab9d 20 20 20 20 20 73 6f 72 74 61 62 6c 65 20 3d 20       sortable = 
2ab9e 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  1;.        }else
2ab9f 7b 0a 20 20 20 20 20 20 20 20 20 20 72 54 6f 74  {.          rTot
2aba0 61 6c 20 2b 3d 20 6e 52 6f 77 2a 65 73 74 4c 6f  al += nRow*estLo
2aba1 67 28 6e 52 6f 77 29 3b 0a 20 20 20 20 20 20 20  g(nRow);.       
2aba2 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
2aba3 2e 2e 2e 20 73 6f 72 74 69 6e 67 20 69 6e 63 72  ... sorting incr
2aba4 65 61 73 65 73 20 4f 52 20 63 6f 73 74 20 74 6f  eases OR cost to
2aba5 20 25 2e 39 67 5c 6e 22 2c 20 72 54 6f 74 61 6c   %.9g\n", rTotal
2aba6 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ));.        }.  
2aba7 20 20 20 20 7d 0a 20 20 20 20 20 20 57 48 45 52      }.      WHER
2aba8 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 6d 75 6c  ETRACE(("... mul
2aba9 74 69 2d 69 6e 64 65 78 20 4f 52 20 63 6f 73 74  ti-index OR cost
2abaa 3d 25 2e 39 67 20 6e 72 6f 77 3d 25 2e 39 67 5c  =%.9g nrow=%.9g\
2abab 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
2abac 20 20 20 20 20 20 72 54 6f 74 61 6c 2c 20 6e 52        rTotal, nR
2abad 6f 77 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ow));.      if( 
2abae 72 54 6f 74 61 6c 3c 70 43 6f 73 74 2d 3e 72 43  rTotal<pCost->rC
2abaf 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ost ){.        p
2abb0 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 72 54  Cost->rCost = rT
2abb1 6f 74 61 6c 3b 0a 20 20 20 20 20 20 20 20 70 43  otal;.        pC
2abb2 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77  ost->nRow = nRow
2abb3 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d  ;.        pCost-
2abb4 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20  >plan.wsFlags = 
2abb5 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 3b 0a  WHERE_MULTI_OR;.
2abb6 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70          pCost->p
2abb7 6c 61 6e 2e 75 2e 70 54 65 72 6d 20 3d 20 70 54  lan.u.pTerm = pT
2abb8 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 69 66 28  erm;.        if(
2abb9 20 73 6f 72 74 61 62 6c 65 20 29 7b 0a 20 20 20   sortable ){.   
2abba 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c         pCost->pl
2abbb 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57 48 45  an.wsFlags = WHE
2abbc 52 45 5f 4f 52 44 45 52 42 59 7c 57 48 45 52 45  RE_ORDERBY|WHERE
2abbd 5f 4d 55 4c 54 49 5f 4f 52 3b 0a 20 20 20 20 20  _MULTI_OR;.     
2abbe 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2abbf 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a   }.  }.#endif /*
2abc0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f   SQLITE_OMIT_OR_
2abc1 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a  OPTIMIZATION */.
2abc2 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 53 72  .  /* If the pSr
2abc3 63 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 72  c table is the r
2abc4 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20  ight table of a 
2abc5 4c 45 46 54 20 4a 4f 49 4e 20 74 68 65 6e 20 77  LEFT JOIN then w
2abc6 65 20 6d 61 79 20 6e 6f 74 0a 20 20 2a 2a 20 75  e may not.  ** u
2abc7 73 65 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 73  se an index to s
2abc8 61 74 69 73 66 79 20 49 53 20 4e 55 4c 4c 20 63  atisfy IS NULL c
2abc9 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 74 68  onstraints on th
2abca 61 74 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  at table.  This 
2abcb 69 73 0a 20 20 2a 2a 20 62 65 63 61 75 73 65 20  is.  ** because 
2abcc 63 6f 6c 75 6d 6e 73 20 6d 69 67 68 74 20 65 6e  columns might en
2abcd 64 20 75 70 20 62 65 69 6e 67 20 4e 55 4c 4c 20  d up being NULL 
2abce 69 66 20 74 68 65 20 74 61 62 6c 65 20 64 6f 65  if the table doe
2abcf 73 20 6e 6f 74 20 6d 61 74 63 68 20 2d 0a 20 20  s not match -.  
2abd0 2a 2a 20 61 20 63 69 72 63 75 6d 73 74 61 6e 63  ** a circumstanc
2abd1 65 20 77 68 69 63 68 20 74 68 65 20 69 6e 64 65  e which the inde
2abd2 78 20 63 61 6e 6e 6f 74 20 68 65 6c 70 20 75 73  x cannot help us
2abd3 20 64 69 73 63 6f 76 65 72 2e 20 20 54 69 63 6b   discover.  Tick
2abd4 65 74 20 23 32 31 37 37 2e 0a 20 20 2a 2f 0a 20  et #2177..  */. 
2abd5 20 69 66 28 20 28 70 53 72 63 2d 3e 6a 6f 69 6e   if( (pSrc->join
2abd6 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 29 21  type & JT_LEFT)!
2abd7 3d 30 20 29 7b 0a 20 20 20 20 65 71 54 65 72 6d  =0 ){.    eqTerm
2abd8 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f  Mask = WO_EQ|WO_
2abd9 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  IN;.  }else{.   
2abda 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f   eqTermMask = WO
2abdb 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e  _EQ|WO_IN|WO_ISN
2abdc 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c  ULL;.  }..  /* L
2abdd 6f 6f 6b 20 61 74 20 65 61 63 68 20 69 6e 64 65  ook at each inde
2abde 78 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  x..  */.  if( pS
2abdf 72 63 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20  rc->pIndex ){.  
2abe0 20 20 70 50 72 6f 62 65 20 3d 20 70 53 72 63 2d    pProbe = pSrc-
2abe1 3e 70 49 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 66  >pIndex;.  }.  f
2abe2 6f 72 28 3b 20 70 50 72 6f 62 65 3b 20 70 50 72  or(; pProbe; pPr
2abe3 6f 62 65 3d 28 70 53 72 63 2d 3e 70 49 6e 64 65  obe=(pSrc->pInde
2abe4 78 20 3f 20 30 20 3a 20 70 50 72 6f 62 65 2d 3e  x ? 0 : pProbe->
2abe5 70 4e 65 78 74 29 29 7b 0a 20 20 20 20 64 6f 75  pNext)){.    dou
2abe6 62 6c 65 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72  ble inMultiplier
2abe7 20 3d 20 31 3b 0a 0a 20 20 20 20 57 48 45 52 45   = 1;..    WHERE
2abe8 54 52 41 43 45 28 28 22 2e 2e 2e 20 69 6e 64 65  TRACE(("... inde
2abe9 78 20 25 73 3a 5c 6e 22 2c 20 70 50 72 6f 62 65  x %s:\n", pProbe
2abea 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20 20 20  ->zName));..    
2abeb 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d  /* Count the num
2abec 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
2abed 6e 20 74 68 65 20 69 6e 64 65 78 20 74 68 61 74  n the index that
2abee 20 61 72 65 20 73 61 74 69 73 66 69 65 64 0a 20   are satisfied. 
2abef 20 20 20 2a 2a 20 62 79 20 78 3d 45 58 50 52 20     ** by x=EXPR 
2abf0 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 72 20 78  constraints or x
2abf1 20 49 4e 20 28 2e 2e 2e 29 20 63 6f 6e 73 74 72   IN (...) constr
2abf2 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  aints..    */.  
2abf3 20 20 77 73 46 6c 61 67 73 20 3d 20 30 3b 0a 20    wsFlags = 0;. 
2abf4 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
2abf5 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  robe->nColumn; i
2abf6 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a  ++){.      int j
2abf7 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c   = pProbe->aiCol
2abf8 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 70 54  umn[i];.      pT
2abf9 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  erm = findTerm(p
2abfa 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74  WC, iCur, j, not
2abfb 52 65 61 64 79 2c 20 65 71 54 65 72 6d 4d 61 73  Ready, eqTermMas
2abfc 6b 2c 20 70 50 72 6f 62 65 29 3b 0a 20 20 20 20  k, pProbe);.    
2abfd 20 20 69 66 28 20 70 54 65 72 6d 3d 3d 30 20 29    if( pTerm==0 )
2abfe 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 77 73   break;.      ws
2abff 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43  Flags |= WHERE_C
2ac00 4f 4c 55 4d 4e 5f 45 51 3b 0a 20 20 20 20 20 20  OLUMN_EQ;.      
2ac01 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
2ac02 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 7b 0a  ator & WO_IN ){.
2ac03 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45          Expr *pE
2ac04 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  xpr = pTerm->pEx
2ac05 70 72 3b 0a 20 20 20 20 20 20 20 20 77 73 46 6c  pr;.        wsFl
2ac06 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c  ags |= WHERE_COL
2ac07 55 4d 4e 5f 49 4e 3b 0a 20 20 20 20 20 20 20 20  UMN_IN;.        
2ac08 69 66 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65  if( pExpr->pSele
2ac09 63 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ct!=0 ){.       
2ac0a 20 20 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20     inMultiplier 
2ac0b 2a 3d 20 32 35 3b 0a 20 20 20 20 20 20 20 20 7d  *= 25;.        }
2ac0c 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28  else if( ALWAYS(
2ac0d 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 20 29 7b  pExpr->pList) ){
2ac0e 0a 20 20 20 20 20 20 20 20 20 20 69 6e 4d 75 6c  .          inMul
2ac0f 74 69 70 6c 69 65 72 20 2a 3d 20 70 45 78 70 72  tiplier *= pExpr
2ac10 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 2b  ->pList->nExpr +
2ac11 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
2ac12 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2ac13 6e 52 6f 77 20 3d 20 70 50 72 6f 62 65 2d 3e 61  nRow = pProbe->a
2ac14 69 52 6f 77 45 73 74 5b 69 5d 20 2a 20 69 6e 4d  iRowEst[i] * inM
2ac15 75 6c 74 69 70 6c 69 65 72 3b 0a 20 20 20 20 63  ultiplier;.    c
2ac16 6f 73 74 20 3d 20 6e 52 6f 77 20 2a 20 65 73 74  ost = nRow * est
2ac17 4c 6f 67 28 69 6e 4d 75 6c 74 69 70 6c 69 65 72  Log(inMultiplier
2ac18 29 3b 0a 20 20 20 20 6e 45 71 20 3d 20 69 3b 0a  );.    nEq = i;.
2ac19 20 20 20 20 69 66 28 20 70 50 72 6f 62 65 2d 3e      if( pProbe->
2ac1a 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
2ac1b 20 26 26 20 28 77 73 46 6c 61 67 73 20 26 20 57   && (wsFlags & W
2ac1c 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 29 3d  HERE_COLUMN_IN)=
2ac1d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 6e  =0.         && n
2ac1e 45 71 3d 3d 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c  Eq==pProbe->nCol
2ac1f 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 77 73 46  umn ){.      wsF
2ac20 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 55 4e  lags |= WHERE_UN
2ac21 49 51 55 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20  IQUE;.    }.    
2ac22 57 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e  WHERETRACE(("...
2ac23 2e 2e 2e 20 6e 45 71 3d 25 64 20 69 6e 4d 75 6c  ... nEq=%d inMul
2ac24 74 3d 25 2e 39 67 20 63 6f 73 74 3d 25 2e 39 67  t=%.9g cost=%.9g
2ac25 5c 6e 22 2c 6e 45 71 2c 69 6e 4d 75 6c 74 69 70  \n",nEq,inMultip
2ac26 6c 69 65 72 2c 63 6f 73 74 29 29 3b 0a 0a 20 20  lier,cost));..  
2ac27 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 72 61    /* Look for ra
2ac28 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  nge constraints.
2ac29 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e      */.    if( n
2ac2a 45 71 3c 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75  Eq<pProbe->nColu
2ac2b 6d 6e 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  mn ){.      int 
2ac2c 6a 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f  j = pProbe->aiCo
2ac2d 6c 75 6d 6e 5b 6e 45 71 5d 3b 0a 20 20 20 20 20  lumn[nEq];.     
2ac2e 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72   pTerm = findTer
2ac2f 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20  m(pWC, iCur, j, 
2ac30 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c  notReady, WO_LT|
2ac31 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47  WO_LE|WO_GT|WO_G
2ac32 45 2c 20 70 50 72 6f 62 65 29 3b 0a 20 20 20 20  E, pProbe);.    
2ac33 20 20 69 66 28 20 70 54 65 72 6d 20 29 7b 0a 20    if( pTerm ){. 
2ac34 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c         wsFlags |
2ac35 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52  = WHERE_COLUMN_R
2ac36 41 4e 47 45 3b 0a 20 20 20 20 20 20 20 20 69 66  ANGE;.        if
2ac37 28 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  ( findTerm(pWC, 
2ac38 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64  iCur, j, notRead
2ac39 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20  y, WO_LT|WO_LE, 
2ac3a 70 50 72 6f 62 65 29 20 29 7b 0a 20 20 20 20 20  pProbe) ){.     
2ac3b 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20       wsFlags |= 
2ac3c 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b  WHERE_TOP_LIMIT;
2ac3d 0a 20 20 20 20 20 20 20 20 20 20 63 6f 73 74 20  .          cost 
2ac3e 2f 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20  /= 3;.          
2ac3f 6e 52 6f 77 20 2f 3d 20 33 3b 0a 20 20 20 20 20  nRow /= 3;.     
2ac40 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
2ac41 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2ac42 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79  Cur, j, notReady
2ac43 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 70  , WO_GT|WO_GE, p
2ac44 50 72 6f 62 65 29 20 29 7b 0a 20 20 20 20 20 20  Probe) ){.      
2ac45 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57      wsFlags |= W
2ac46 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a  HERE_BTM_LIMIT;.
2ac47 20 20 20 20 20 20 20 20 20 20 63 6f 73 74 20 2f            cost /
2ac48 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 6e  = 3;.          n
2ac49 52 6f 77 20 2f 3d 20 33 3b 0a 20 20 20 20 20 20  Row /= 3;.      
2ac4a 20 20 7d 0a 20 20 20 20 20 20 20 20 57 48 45 52    }.        WHER
2ac4b 45 54 52 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20  ETRACE(("...... 
2ac4c 72 61 6e 67 65 20 72 65 64 75 63 65 73 20 63 6f  range reduces co
2ac4d 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63  st to %.9g\n", c
2ac4e 6f 73 74 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ost));.      }. 
2ac4f 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 64     }..    /* Add
2ac50 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 20   the additional 
2ac51 63 6f 73 74 20 6f 66 20 73 6f 72 74 69 6e 67 20  cost of sorting 
2ac52 69 66 20 74 68 61 74 20 69 73 20 61 20 66 61 63  if that is a fac
2ac53 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  tor..    */.    
2ac54 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a  if( pOrderBy ){.
2ac55 20 20 20 20 20 20 69 66 28 20 28 77 73 46 6c 61        if( (wsFla
2ac56 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d  gs & WHERE_COLUM
2ac57 4e 5f 49 4e 29 3d 3d 30 20 26 26 0a 20 20 20 20  N_IN)==0 &&.    
2ac58 20 20 20 20 20 20 20 69 73 53 6f 72 74 69 6e 67         isSorting
2ac59 49 6e 64 65 78 28 70 50 61 72 73 65 2c 70 57 43  Index(pParse,pWC
2ac5a 2d 3e 70 4d 61 73 6b 53 65 74 2c 70 50 72 6f 62  ->pMaskSet,pProb
2ac5b 65 2c 69 43 75 72 2c 70 4f 72 64 65 72 42 79 2c  e,iCur,pOrderBy,
2ac5c 6e 45 71 2c 26 72 65 76 29 20 29 7b 0a 20 20 20  nEq,&rev) ){.   
2ac5d 20 20 20 20 20 69 66 28 20 77 73 46 6c 61 67 73       if( wsFlags
2ac5e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
2ac5f 20 77 73 46 6c 61 67 73 20 3d 20 57 48 45 52 45   wsFlags = WHERE
2ac60 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 3b 0a 20  _COLUMN_RANGE;. 
2ac61 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2ac62 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2ac63 45 5f 4f 52 44 45 52 42 59 3b 0a 20 20 20 20 20  E_ORDERBY;.     
2ac64 20 20 20 69 66 28 20 72 65 76 20 29 7b 0a 20 20     if( rev ){.  
2ac65 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
2ac66 7c 3d 20 57 48 45 52 45 5f 52 45 56 45 52 53 45  |= WHERE_REVERSE
2ac67 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2ac68 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2ac69 20 63 6f 73 74 20 2b 3d 20 63 6f 73 74 2a 65 73   cost += cost*es
2ac6a 74 4c 6f 67 28 63 6f 73 74 29 3b 0a 20 20 20 20  tLog(cost);.    
2ac6b 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
2ac6c 22 2e 2e 2e 2e 2e 2e 20 6f 72 64 65 72 62 79 20  "...... orderby 
2ac6d 69 6e 63 72 65 61 73 65 73 20 63 6f 73 74 20 74  increases cost t
2ac6e 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29  o %.9g\n", cost)
2ac6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2ac70 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74  ..    /* Check t
2ac71 6f 20 73 65 65 20 69 66 20 77 65 20 63 61 6e 20  o see if we can 
2ac72 67 65 74 20 61 77 61 79 20 77 69 74 68 20 75 73  get away with us
2ac73 69 6e 67 20 6a 75 73 74 20 74 68 65 20 69 6e 64  ing just the ind
2ac74 65 78 20 77 69 74 68 6f 75 74 0a 20 20 20 20 2a  ex without.    *
2ac75 2a 20 65 76 65 72 20 72 65 61 64 69 6e 67 20 74  * ever reading t
2ac76 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 74 68  he table.  If th
2ac77 61 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  at is the case, 
2ac78 74 68 65 6e 20 68 61 6c 76 65 20 74 68 65 0a 20  then halve the. 
2ac79 20 20 20 2a 2a 20 63 6f 73 74 20 6f 66 20 74 68     ** cost of th
2ac7a 69 73 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f  is index..    */
2ac7b 0a 20 20 20 20 69 66 28 20 77 73 46 6c 61 67 73  .    if( wsFlags
2ac7c 20 26 26 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65   && pSrc->colUse
2ac7d 64 20 3c 20 28 28 28 42 69 74 6d 61 73 6b 29 31  d < (((Bitmask)1
2ac7e 29 3c 3c 28 42 4d 53 2d 31 29 29 20 29 7b 0a 20  )<<(BMS-1)) ){. 
2ac7f 20 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 20 3d       Bitmask m =
2ac80 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65 64 3b 0a   pSrc->colUsed;.
2ac81 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
2ac82 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50     for(j=0; j<pP
2ac83 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a  robe->nColumn; j
2ac84 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ++){.        int
2ac85 20 78 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43   x = pProbe->aiC
2ac86 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 20 20  olumn[j];.      
2ac87 20 20 69 66 28 20 78 3c 42 4d 53 2d 31 20 29 7b    if( x<BMS-1 ){
2ac88 0a 20 20 20 20 20 20 20 20 20 20 6d 20 26 3d 20  .          m &= 
2ac89 7e 28 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c  ~(((Bitmask)1)<<
2ac8a 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  x);.        }.  
2ac8b 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2ac8c 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  m==0 ){.        
2ac8d 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45  wsFlags |= WHERE
2ac8e 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20 20 20 20  _IDX_ONLY;.     
2ac8f 20 20 20 63 6f 73 74 20 2f 3d 20 32 3b 0a 20 20     cost /= 2;.  
2ac90 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45        WHERETRACE
2ac91 28 28 22 2e 2e 2e 2e 2e 2e 20 69 64 78 2d 6f 6e  (("...... idx-on
2ac92 6c 79 20 72 65 64 75 63 65 73 20 63 6f 73 74 20  ly reduces cost 
2ac93 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74  to %.9g\n", cost
2ac94 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
2ac95 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  }..    /* If thi
2ac96 73 20 69 6e 64 65 78 20 68 61 73 20 61 63 68 69  s index has achi
2ac97 65 76 65 64 20 74 68 65 20 6c 6f 77 65 73 74 20  eved the lowest 
2ac98 63 6f 73 74 20 73 6f 20 66 61 72 2c 20 74 68 65  cost so far, the
2ac99 6e 20 75 73 65 20 69 74 2e 0a 20 20 20 20 2a 2f  n use it..    */
2ac9a 0a 20 20 20 20 69 66 28 20 77 73 46 6c 61 67 73  .    if( wsFlags
2ac9b 21 3d 30 20 26 26 20 63 6f 73 74 20 3c 20 70 43  !=0 && cost < pC
2ac9c 6f 73 74 2d 3e 72 43 6f 73 74 20 29 7b 0a 20 20  ost->rCost ){.  
2ac9d 20 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74      pCost->rCost
2ac9e 20 3d 20 63 6f 73 74 3b 0a 20 20 20 20 20 20 70   = cost;.      p
2ac9f 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f  Cost->nRow = nRo
2aca0 77 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  w;.      pCost->
2aca1 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 77  plan.wsFlags = w
2aca2 73 46 6c 61 67 73 3b 0a 20 20 20 20 20 20 70 43  sFlags;.      pC
2aca3 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20  ost->plan.nEq = 
2aca4 6e 45 71 3b 0a 20 20 20 20 20 20 61 73 73 65 72  nEq;.      asser
2aca5 74 28 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77  t( pCost->plan.w
2aca6 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
2aca7 4e 44 45 58 45 44 20 29 3b 0a 20 20 20 20 20 20  NDEXED );.      
2aca8 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49  pCost->plan.u.pI
2aca9 64 78 20 3d 20 70 50 72 6f 62 65 3b 0a 20 20 20  dx = pProbe;.   
2acaa 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 70   }.  }..  /* Rep
2acab 6f 72 74 20 74 68 65 20 62 65 73 74 20 72 65 73  ort the best res
2acac 75 6c 74 0a 20 20 2a 2f 0a 20 20 70 43 6f 73 74  ult.  */.  pCost
2acad 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c  ->plan.wsFlags |
2acae 3d 20 65 71 54 65 72 6d 4d 61 73 6b 3b 0a 20 20  = eqTermMask;.  
2acaf 57 48 45 52 45 54 52 41 43 45 28 28 22 62 65 73  WHERETRACE(("bes
2acb0 74 20 69 6e 64 65 78 20 69 73 20 25 73 2c 20 63  t index is %s, c
2acb1 6f 73 74 3d 25 2e 39 67 2c 20 6e 72 6f 77 3d 25  ost=%.9g, nrow=%
2acb2 2e 39 67 2c 20 77 73 46 6c 61 67 73 3d 25 78 2c  .9g, wsFlags=%x,
2acb3 20 6e 45 71 3d 25 64 5c 6e 22 2c 0a 20 20 20 20   nEq=%d\n",.    
2acb4 20 20 20 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e      (pCost->plan
2acb5 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2acb6 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 3f 0a 20  _INDEXED)!=0 ?. 
2acb7 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 73              pCos
2acb8 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e  t->plan.u.pIdx->
2acb9 7a 4e 61 6d 65 20 3a 20 22 28 6e 6f 6e 65 29 22  zName : "(none)"
2acba 2c 20 70 43 6f 73 74 2d 3e 6e 52 6f 77 2c 0a 20  , pCost->nRow,. 
2acbb 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 72 43         pCost->rC
2acbc 6f 73 74 2c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e  ost, pCost->plan
2acbd 2e 77 73 46 6c 61 67 73 2c 20 70 43 6f 73 74 2d  .wsFlags, pCost-
2acbe 3e 70 6c 61 6e 2e 6e 45 71 29 29 3b 0a 7d 0a 0a  >plan.nEq));.}..
2acbf 0a 2f 2a 0a 2a 2a 20 44 69 73 61 62 6c 65 20 61  ./*.** Disable a
2acc0 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45   term in the WHE
2acc1 52 45 20 63 6c 61 75 73 65 2e 20 20 45 78 63 65  RE clause.  Exce
2acc2 70 74 2c 20 64 6f 20 6e 6f 74 20 64 69 73 61 62  pt, do not disab
2acc3 6c 65 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20 69  le the term.** i
2acc4 66 20 69 74 20 63 6f 6e 74 72 6f 6c 73 20 61 20  f it controls a 
2acc5 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
2acc6 61 6e 64 20 69 74 20 64 69 64 20 6e 6f 74 20 6f  and it did not o
2acc7 72 69 67 69 6e 61 74 65 20 69 6e 20 74 68 65 20  riginate in the 
2acc8 4f 4e 0a 2a 2a 20 6f 72 20 55 53 49 4e 47 20 63  ON.** or USING c
2acc9 6c 61 75 73 65 20 6f 66 20 74 68 61 74 20 6a 6f  lause of that jo
2acca 69 6e 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 69 64  in..**.** Consid
2accb 65 72 20 74 68 65 20 74 65 72 6d 20 74 32 2e 7a  er the term t2.z
2accc 3d 27 6f 6b 27 20 69 6e 20 74 68 65 20 66 6f 6c  ='ok' in the fol
2accd 6c 6f 77 69 6e 67 20 71 75 65 72 69 65 73 3a 0a  lowing queries:.
2acce 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 53 45 4c  **.**   (1)  SEL
2accf 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c 45  ECT * FROM t1 LE
2acd0 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74 31  FT JOIN t2 ON t1
2acd1 2e 61 3d 74 32 2e 78 20 57 48 45 52 45 20 74 32  .a=t2.x WHERE t2
2acd2 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20 20 28 32 29  .z='ok'.**   (2)
2acd3 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2acd4 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20  t1 LEFT JOIN t2 
2acd5 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e 44  ON t1.a=t2.x AND
2acd6 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20 20   t2.z='ok'.**   
2acd7 28 33 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52  (3)  SELECT * FR
2acd8 4f 4d 20 74 31 2c 20 74 32 20 57 48 45 52 45 20  OM t1, t2 WHERE 
2acd9 74 31 2e 61 3d 74 32 2e 78 20 41 4e 44 20 74 32  t1.a=t2.x AND t2
2acda 2e 7a 3d 27 6f 6b 27 0a 2a 2a 0a 2a 2a 20 54 68  .z='ok'.**.** Th
2acdb 65 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 73 20 64  e t2.z='ok' is d
2acdc 69 73 61 62 6c 65 64 20 69 6e 20 74 68 65 20 69  isabled in the i
2acdd 6e 20 28 32 29 20 62 65 63 61 75 73 65 20 69 74  n (2) because it
2acde 20 6f 72 69 67 69 6e 61 74 65 73 0a 2a 2a 20 69   originates.** i
2acdf 6e 20 74 68 65 20 4f 4e 20 63 6c 61 75 73 65 2e  n the ON clause.
2ace0 20 20 54 68 65 20 74 65 72 6d 20 69 73 20 64 69    The term is di
2ace1 73 61 62 6c 65 64 20 69 6e 20 28 33 29 20 62 65  sabled in (3) be
2ace2 63 61 75 73 65 20 69 74 20 69 73 20 6e 6f 74 20  cause it is not 
2ace3 70 61 72 74 0a 2a 2a 20 6f 66 20 61 20 4c 45 46  part.** of a LEF
2ace4 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2e 20 20 49  T OUTER JOIN.  I
2ace5 6e 20 28 31 29 2c 20 74 68 65 20 74 65 72 6d 20  n (1), the term 
2ace6 69 73 20 6e 6f 74 20 64 69 73 61 62 6c 65 64 2e  is not disabled.
2ace7 0a 2a 2a 0a 2a 2a 20 44 69 73 61 62 6c 69 6e 67  .**.** Disabling
2ace8 20 61 20 74 65 72 6d 20 63 61 75 73 65 73 20 74   a term causes t
2ace9 68 61 74 20 74 65 72 6d 20 74 6f 20 6e 6f 74 20  hat term to not 
2acea 62 65 20 74 65 73 74 65 64 20 69 6e 20 74 68 65  be tested in the
2aceb 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a 20 6f   inner loop.** o
2acec 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 44 69 73  f the join.  Dis
2aced 61 62 6c 69 6e 67 20 69 73 20 61 6e 20 6f 70 74  abling is an opt
2acee 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 57 68 65 6e  imization.  When
2acef 20 74 65 72 6d 73 20 61 72 65 20 73 61 74 69 73   terms are satis
2acf0 66 69 65 64 0a 2a 2a 20 62 79 20 69 6e 64 69 63  fied.** by indic
2acf1 65 73 2c 20 77 65 20 64 69 73 61 62 6c 65 20 74  es, we disable t
2acf2 68 65 6d 20 74 6f 20 70 72 65 76 65 6e 74 20 72  hem to prevent r
2acf3 65 64 75 6e 64 61 6e 74 20 74 65 73 74 73 20 69  edundant tests i
2acf4 6e 20 74 68 65 20 69 6e 6e 65 72 0a 2a 2a 20 6c  n the inner.** l
2acf5 6f 6f 70 2e 20 20 57 65 20 77 6f 75 6c 64 20 67  oop.  We would g
2acf6 65 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 72  et the correct r
2acf7 65 73 75 6c 74 73 20 69 66 20 6e 6f 74 68 69 6e  esults if nothin
2acf8 67 20 77 65 72 65 20 65 76 65 72 20 64 69 73 61  g were ever disa
2acf9 62 6c 65 64 2c 0a 2a 2a 20 62 75 74 20 6a 6f 69  bled,.** but joi
2acfa 6e 73 20 6d 69 67 68 74 20 72 75 6e 20 61 20 6c  ns might run a l
2acfb 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20 20 54  ittle slower.  T
2acfc 68 65 20 74 72 69 63 6b 20 69 73 20 74 6f 20 64  he trick is to d
2acfd 69 73 61 62 6c 65 20 61 73 20 6d 75 63 68 0a 2a  isable as much.*
2acfe 2a 20 61 73 20 77 65 20 63 61 6e 20 77 69 74 68  * as we can with
2acff 6f 75 74 20 64 69 73 61 62 6c 69 6e 67 20 74 6f  out disabling to
2ad00 6f 20 6d 75 63 68 2e 20 20 49 66 20 77 65 20 64  o much.  If we d
2ad01 69 73 61 62 6c 65 64 20 69 6e 20 28 31 29 2c 20  isabled in (1), 
2ad02 77 65 27 64 20 67 65 74 0a 2a 2a 20 74 68 65 20  we'd get.** the 
2ad03 77 72 6f 6e 67 20 61 6e 73 77 65 72 2e 20 20 53  wrong answer.  S
2ad04 65 65 20 74 69 63 6b 65 74 20 23 38 31 33 2e 0a  ee ticket #813..
2ad05 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
2ad06 69 73 61 62 6c 65 54 65 72 6d 28 57 68 65 72 65  isableTerm(Where
2ad07 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 57  Level *pLevel, W
2ad08 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 29  hereTerm *pTerm)
2ad09 7b 0a 20 20 69 66 28 20 70 54 65 72 6d 0a 20 20  {.  if( pTerm.  
2ad0a 20 20 20 20 26 26 20 41 4c 57 41 59 53 28 28 70      && ALWAYS((p
2ad0b 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
2ad0c 54 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 29 0a  TERM_CODED)==0).
2ad0d 20 20 20 20 20 20 26 26 20 28 70 4c 65 76 65 6c        && (pLevel
2ad0e 2d 3e 69 4c 65 66 74 4a 6f 69 6e 3d 3d 30 20 7c  ->iLeftJoin==0 |
2ad0f 7c 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  | ExprHasPropert
2ad10 79 28 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20  y(pTerm->pExpr, 
2ad11 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 29 0a 20 20  EP_FromJoin)).  
2ad12 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 77 74  ){.    pTerm->wt
2ad13 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f  Flags |= TERM_CO
2ad14 44 45 44 3b 0a 20 20 20 20 69 66 28 20 70 54 65  DED;.    if( pTe
2ad15 72 6d 2d 3e 69 50 61 72 65 6e 74 3e 3d 30 20 29  rm->iParent>=0 )
2ad16 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  {.      WhereTer
2ad17 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70 54 65  m *pOther = &pTe
2ad18 72 6d 2d 3e 70 57 43 2d 3e 61 5b 70 54 65 72 6d  rm->pWC->a[pTerm
2ad19 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20  ->iParent];.    
2ad1a 20 20 69 66 28 20 28 2d 2d 70 4f 74 68 65 72 2d    if( (--pOther-
2ad1b 3e 6e 43 68 69 6c 64 29 3d 3d 30 20 29 7b 0a 20  >nChild)==0 ){. 
2ad1c 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65         disableTe
2ad1d 72 6d 28 70 4c 65 76 65 6c 2c 20 70 4f 74 68 65  rm(pLevel, pOthe
2ad1e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
2ad1f 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  }.  }.}../*.** A
2ad20 70 70 6c 79 20 74 68 65 20 61 66 66 69 6e 69 74  pply the affinit
2ad21 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ies associated w
2ad22 69 74 68 20 74 68 65 20 66 69 72 73 74 20 6e 20  ith the first n 
2ad23 63 6f 6c 75 6d 6e 73 20 6f 66 20 69 6e 64 65 78  columns of index
2ad24 0a 2a 2a 20 70 49 64 78 20 74 6f 20 74 68 65 20  .** pIdx to the 
2ad25 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 6e 20  values in the n 
2ad26 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69  registers starti
2ad27 6e 67 20 61 74 20 62 61 73 65 2e 0a 2a 2f 0a 73  ng at base..*/.s
2ad28 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 41  tatic void codeA
2ad29 70 70 6c 79 41 66 66 69 6e 69 74 79 28 50 61 72  pplyAffinity(Par
2ad2a 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
2ad2b 62 61 73 65 2c 20 69 6e 74 20 6e 2c 20 49 6e 64  base, int n, Ind
2ad2c 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69 66 28  ex *pIdx){.  if(
2ad2d 20 6e 3e 30 20 29 7b 0a 20 20 20 20 56 64 62 65   n>0 ){.    Vdbe
2ad2e 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2ad2f 64 62 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  dbe;.    assert(
2ad30 20 76 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c   v!=0 );.    sql
2ad31 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2ad32 2c 20 4f 50 5f 41 66 66 69 6e 69 74 79 2c 20 62  , OP_Affinity, b
2ad33 61 73 65 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c  ase, n);.    sql
2ad34 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
2ad35 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b 0a 20  yStr(v, pIdx);. 
2ad36 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2ad37 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
2ad38 65 28 70 50 61 72 73 65 2c 20 62 61 73 65 2c 20  e(pParse, base, 
2ad39 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a  n);.  }.}.../*.*
2ad3a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2ad3b 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 71 75  for a single equ
2ad3c 61 6c 69 74 79 20 74 65 72 6d 20 6f 66 20 74 68  ality term of th
2ad3d 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
2ad3e 20 41 6e 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20   An equality.** 
2ad3f 74 65 72 6d 20 63 61 6e 20 62 65 20 65 69 74 68  term can be eith
2ad40 65 72 20 58 3d 65 78 70 72 20 6f 72 20 58 20 49  er X=expr or X I
2ad41 4e 20 28 2e 2e 2e 29 2e 20 20 20 70 54 65 72 6d  N (...).   pTerm
2ad42 20 69 73 20 74 68 65 20 74 65 72 6d 20 74 6f 20   is the term to 
2ad43 62 65 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a 2a 2a  be .** coded..**
2ad44 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20  .** The current 
2ad45 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f  value for the co
2ad46 6e 73 74 72 61 69 6e 74 20 69 73 20 6c 65 66 74  nstraint is left
2ad47 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65   in register iRe
2ad48 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 63  g..**.** For a c
2ad49 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 74 68 65  onstraint of the
2ad4a 20 66 6f 72 6d 20 58 3d 65 78 70 72 2c 20 74 68   form X=expr, th
2ad4b 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
2ad4c 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69 74  evaluated and it
2ad4d 73 0a 2a 2a 20 72 65 73 75 6c 74 20 69 73 20 6c  s.** result is l
2ad4e 65 66 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b  eft on the stack
2ad4f 2e 20 20 46 6f 72 20 63 6f 6e 73 74 72 61 69 6e  .  For constrain
2ad50 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58  ts of the form X
2ad51 20 49 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69   IN (...).** thi
2ad52 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 75  s routine sets u
2ad53 70 20 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69  p a loop that wi
2ad54 6c 6c 20 69 74 65 72 61 74 65 20 6f 76 65 72 20  ll iterate over 
2ad55 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 58 2e  all values of X.
2ad56 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2ad57 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28  odeEqualityTerm(
2ad58 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2ad59 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61  ,      /* The pa
2ad5a 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2ad5b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
2ad5c 65 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20 74 65  erm,   /* The te
2ad5d 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  rm of the WHERE 
2ad5e 63 6c 61 75 73 65 20 74 6f 20 62 65 20 63 6f 64  clause to be cod
2ad5f 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76  ed */.  WhereLev
2ad60 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a 20 57  el *pLevel, /* W
2ad61 68 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  hen level of the
2ad62 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 77 65 20   FROM clause we 
2ad63 61 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e 20 2a  are working on *
2ad64 2f 0a 20 20 69 6e 74 20 69 54 61 72 67 65 74 20  /.  int iTarget 
2ad65 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d          /* Attem
2ad66 70 74 20 74 6f 20 6c 65 61 76 65 20 72 65 73 75  pt to leave resu
2ad67 6c 74 73 20 69 6e 20 74 68 69 73 20 72 65 67 69  lts in this regi
2ad68 73 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  ster */.){.  Exp
2ad69 72 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70  r *pX = pTerm->p
2ad6a 45 78 70 72 3b 0a 20 20 56 64 62 65 20 2a 76 20  Expr;.  Vdbe *v 
2ad6b 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2ad6c 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20  .  int iReg;    
2ad6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ad6e 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
2ad6f 67 20 72 65 73 75 6c 74 73 20 2a 2f 0a 0a 20 20  g results */..  
2ad70 61 73 73 65 72 74 28 20 69 54 61 72 67 65 74 3e  assert( iTarget>
2ad71 30 20 29 3b 0a 20 20 69 66 28 20 70 58 2d 3e 6f  0 );.  if( pX->o
2ad72 70 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20 20 20 20  p==TK_EQ ){.    
2ad73 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  iReg = sqlite3Ex
2ad74 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
2ad75 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c  rse, pX->pRight,
2ad76 20 69 54 61 72 67 65 74 29 3b 0a 20 20 7d 65 6c   iTarget);.  }el
2ad77 73 65 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54  se if( pX->op==T
2ad78 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  K_ISNULL ){.    
2ad79 69 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b 0a  iReg = iTarget;.
2ad7a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2ad7b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
2ad7c 2c 20 30 2c 20 69 52 65 67 29 3b 0a 23 69 66 6e  , 0, iReg);.#ifn
2ad7d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2ad7e 53 55 42 51 55 45 52 59 0a 20 20 7d 65 6c 73 65  SUBQUERY.  }else
2ad7f 7b 0a 20 20 20 20 69 6e 74 20 65 54 79 70 65 3b  {.    int eType;
2ad80 0a 20 20 20 20 69 6e 74 20 69 54 61 62 3b 0a 20  .    int iTab;. 
2ad81 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70     struct InLoop
2ad82 20 2a 70 49 6e 3b 0a 0a 20 20 20 20 61 73 73 65   *pIn;..    asse
2ad83 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49  rt( pX->op==TK_I
2ad84 4e 20 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20  N );.    iReg = 
2ad85 69 54 61 72 67 65 74 3b 0a 20 20 20 20 65 54 79  iTarget;.    eTy
2ad86 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  pe = sqlite3Find
2ad87 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  InIndex(pParse, 
2ad88 70 58 2c 20 30 29 3b 0a 20 20 20 20 69 54 61 62  pX, 0);.    iTab
2ad89 20 3d 20 70 58 2d 3e 69 54 61 62 6c 65 3b 0a 20   = pX->iTable;. 
2ad8a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2ad8b 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
2ad8c 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20 20  d, iTab, 0);.   
2ad8d 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2ad8e 20 22 25 2e 2a 73 22 2c 20 70 58 2d 3e 73 70 61   "%.*s", pX->spa
2ad8f 6e 2e 6e 2c 20 70 58 2d 3e 73 70 61 6e 2e 7a 29  n.n, pX->span.z)
2ad90 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2ad91 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2ad92 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 5f 41  ags & WHERE_IN_A
2ad93 42 4c 45 20 29 3b 0a 20 20 20 20 69 66 28 20 70  BLE );.    if( p
2ad94 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 3d  Level->u.in.nIn=
2ad95 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 76  =0 ){.      pLev
2ad96 65 6c 2d 3e 61 64 64 72 4e 78 74 20 3d 20 73 71  el->addrNxt = sq
2ad97 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2ad98 65 6c 28 76 29 3b 0a 20 20 20 20 7d 0a 20 20 20  el(v);.    }.   
2ad99 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49   pLevel->u.in.nI
2ad9a 6e 2b 2b 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  n++;.    pLevel-
2ad9b 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 20 3d 0a  >u.in.aInLoop =.
2ad9c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
2ad9d 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 50  ReallocOrFree(pP
2ad9e 61 72 73 65 2d 3e 64 62 2c 20 70 4c 65 76 65 6c  arse->db, pLevel
2ad9f 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 2c 0a  ->u.in.aInLoop,.
2ada0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ada1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
2ada2 7a 65 6f 66 28 70 4c 65 76 65 6c 2d 3e 75 2e 69  zeof(pLevel->u.i
2ada3 6e 2e 61 49 6e 4c 6f 6f 70 5b 30 5d 29 2a 70 4c  n.aInLoop[0])*pL
2ada4 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 29 3b  evel->u.in.nIn);
2ada5 0a 20 20 20 20 70 49 6e 20 3d 20 70 4c 65 76 65  .    pIn = pLeve
2ada6 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 3b  l->u.in.aInLoop;
2ada7 0a 20 20 20 20 69 66 28 20 70 49 6e 20 29 7b 0a  .    if( pIn ){.
2ada8 20 20 20 20 20 20 70 49 6e 20 2b 3d 20 70 4c 65        pIn += pLe
2ada9 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 20 2d 20  vel->u.in.nIn - 
2adaa 31 3b 0a 20 20 20 20 20 20 70 49 6e 2d 3e 69 43  1;.      pIn->iC
2adab 75 72 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 20  ur = iTab;.     
2adac 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49   if( eType==IN_I
2adad 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20  NDEX_ROWID ){.  
2adae 20 20 20 20 20 20 70 49 6e 2d 3e 61 64 64 72 49        pIn->addrI
2adaf 6e 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  nTop = sqlite3Vd
2adb0 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2adb1 6f 77 69 64 2c 20 69 54 61 62 2c 20 69 52 65 67  owid, iTab, iReg
2adb2 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2adb3 20 20 20 20 20 20 20 20 70 49 6e 2d 3e 61 64 64          pIn->add
2adb4 72 49 6e 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  rInTop = sqlite3
2adb5 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2adb6 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 30  _Column, iTab, 0
2adb7 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20 20 7d  , iReg);.      }
2adb8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2adb9 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
2adba 73 4e 75 6c 6c 2c 20 69 52 65 67 29 3b 0a 20 20  sNull, iReg);.  
2adbb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
2adbc 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 20  Level->u.in.nIn 
2adbd 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  = 0;.    }.#endi
2adbe 66 0a 20 20 7d 0a 20 20 64 69 73 61 62 6c 65 54  f.  }.  disableT
2adbf 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54 65 72  erm(pLevel, pTer
2adc0 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 52 65  m);.  return iRe
2adc1 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  g;.}../*.** Gene
2adc2 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
2adc3 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 61 6c 6c  ill evaluate all
2adc4 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74   == and IN const
2adc5 72 61 69 6e 74 73 20 66 6f 72 20 61 6e 0a 2a 2a  raints for an.**
2adc6 20 69 6e 64 65 78 2e 20 20 54 68 65 20 76 61 6c   index.  The val
2adc7 75 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f 6e 73  ues for all cons
2adc8 74 72 61 69 6e 74 73 20 61 72 65 20 6c 65 66 74  traints are left
2adc9 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a   on the stack..*
2adca 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65  *.** For example
2adcb 2c 20 63 6f 6e 73 69 64 65 72 20 74 61 62 6c 65  , consider table
2adcc 20 74 31 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29   t1(a,b,c,d,e,f)
2adcd 20 77 69 74 68 20 69 6e 64 65 78 20 69 31 28 61   with index i1(a
2adce 2c 62 2c 63 29 2e 0a 2a 2a 20 53 75 70 70 6f 73  ,b,c)..** Suppos
2adcf 65 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  e the WHERE clau
2add0 73 65 20 69 73 20 74 68 69 73 3a 20 20 61 3d 3d  se is this:  a==
2add1 35 20 41 4e 44 20 62 20 49 4e 20 28 31 2c 32 2c  5 AND b IN (1,2,
2add2 33 29 20 41 4e 44 20 63 3e 35 20 41 4e 44 20 63  3) AND c>5 AND c
2add3 3c 31 30 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78  <10.** The index
2add4 20 68 61 73 20 61 73 20 6d 61 6e 79 20 61 73 20   has as many as 
2add5 74 68 72 65 65 20 65 71 75 61 6c 69 74 79 20 63  three equality c
2add6 6f 6e 73 74 72 61 69 6e 74 73 2c 20 62 75 74 20  onstraints, but 
2add7 69 6e 20 74 68 69 73 0a 2a 2a 20 65 78 61 6d 70  in this.** examp
2add8 6c 65 2c 20 74 68 65 20 74 68 69 72 64 20 22 63  le, the third "c
2add9 22 20 76 61 6c 75 65 20 69 73 20 61 6e 20 69 6e  " value is an in
2adda 65 71 75 61 6c 69 74 79 2e 20 20 53 6f 20 6f 6e  equality.  So on
2addb 6c 79 20 74 77 6f 20 0a 2a 2a 20 63 6f 6e 73 74  ly two .** const
2addc 72 61 69 6e 74 73 20 61 72 65 20 63 6f 64 65 64  raints are coded
2addd 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
2adde 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 20 63 6f  will generate co
2addf 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 0a 2a  de to evaluate.*
2ade0 2a 20 61 3d 3d 35 20 61 6e 64 20 62 20 49 4e 20  * a==5 and b IN 
2ade1 28 31 2c 32 2c 33 29 2e 20 20 54 68 65 20 63 75  (1,2,3).  The cu
2ade2 72 72 65 6e 74 20 76 61 6c 75 65 73 20 66 6f 72  rrent values for
2ade3 20 61 20 61 6e 64 20 62 20 77 69 6c 6c 20 62 65   a and b will be
2ade4 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 63 6f   stored.** in co
2ade5 6e 73 65 63 75 74 69 76 65 20 72 65 67 69 73 74  nsecutive regist
2ade6 65 72 73 20 61 6e 64 20 74 68 65 20 69 6e 64 65  ers and the inde
2ade7 78 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 72  x of the first r
2ade8 65 67 69 73 74 65 72 20 69 73 20 72 65 74 75 72  egister is retur
2ade9 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ned..**.** In th
2adea 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 20  e example above 
2adeb 6e 45 71 3d 3d 32 2e 20 20 42 75 74 20 74 68 69  nEq==2.  But thi
2adec 73 20 73 75 62 72 6f 75 74 69 6e 65 20 77 6f 72  s subroutine wor
2aded 6b 73 20 66 6f 72 20 61 6e 79 20 76 61 6c 75 65  ks for any value
2adee 0a 2a 2a 20 6f 66 20 6e 45 71 20 69 6e 63 6c 75  .** of nEq inclu
2adef 64 69 6e 67 20 30 2e 20 20 49 66 20 6e 45 71 3d  ding 0.  If nEq=
2adf0 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  =0, this routine
2adf1 20 69 73 20 6e 65 61 72 6c 79 20 61 20 6e 6f 2d   is nearly a no-
2adf2 6f 70 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20  op..** The only 
2adf3 74 68 69 6e 67 20 69 74 20 64 6f 65 73 20 69 73  thing it does is
2adf4 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 70 4c   allocate the pL
2adf5 65 76 65 6c 2d 3e 69 4d 65 6d 20 6d 65 6d 6f 72  evel->iMem memor
2adf6 79 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68  y cell..**.** Th
2adf7 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79  is routine alway
2adf8 73 20 61 6c 6c 6f 63 61 74 65 73 20 61 74 20 6c  s allocates at l
2adf9 65 61 73 74 20 6f 6e 65 20 6d 65 6d 6f 72 79 20  east one memory 
2adfa 63 65 6c 6c 20 61 6e 64 20 72 65 74 75 72 6e 73  cell and returns
2adfb 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 6f 66  .** the index of
2adfc 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c   that memory cel
2adfd 6c 2e 20 54 68 65 20 63 6f 64 65 20 74 68 61 74  l. The code that
2adfe 0a 2a 2a 20 63 61 6c 6c 73 20 74 68 69 73 20 72  .** calls this r
2adff 6f 75 74 69 6e 65 20 77 69 6c 6c 20 75 73 65 20  outine will use 
2ae00 74 68 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  that memory cell
2ae01 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 74 65   to store the te
2ae02 72 6d 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6b 65 79  rmination.** key
2ae03 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6c 6f   value of the lo
2ae04 6f 70 2e 20 20 49 66 20 6f 6e 65 20 6f 72 20 6d  op.  If one or m
2ae05 6f 72 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 73  ore IN operators
2ae06 20 61 70 70 65 61 72 2c 20 74 68 65 6e 0a 2a 2a   appear, then.**
2ae07 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   this routine al
2ae08 6c 6f 63 61 74 65 73 20 61 6e 20 61 64 64 69 74  locates an addit
2ae09 69 6f 6e 61 6c 20 6e 45 71 20 6d 65 6d 6f 72 79  ional nEq memory
2ae0a 20 63 65 6c 6c 73 20 66 6f 72 20 69 6e 74 65 72   cells for inter
2ae0b 6e 61 6c 0a 2a 2a 20 75 73 65 2e 0a 2a 2f 0a 73  nal.** use..*/.s
2ae0c 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c  tatic int codeAl
2ae0d 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 0a  lEqualityTerms(.
2ae0e 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2ae0f 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
2ae10 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2ae11 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
2ae12 65 6c 2c 20 20 20 2f 2a 20 57 68 69 63 68 20 6e  el,   /* Which n
2ae13 65 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68  ested loop of th
2ae14 65 20 46 52 4f 4d 20 77 65 20 61 72 65 20 63 6f  e FROM we are co
2ae15 64 69 6e 67 20 2a 2f 0a 20 20 57 68 65 72 65 43  ding */.  WhereC
2ae16 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20  lause *pWC,     
2ae17 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2ae18 75 73 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  use */.  Bitmask
2ae19 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 2f   notReady,     /
2ae1a 2a 20 57 68 69 63 68 20 70 61 72 74 73 20 6f 66  * Which parts of
2ae1b 20 46 52 4f 4d 20 68 61 76 65 20 6e 6f 74 20 79   FROM have not y
2ae1c 65 74 20 62 65 65 6e 20 63 6f 64 65 64 20 2a 2f  et been coded */
2ae1d 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 52 65 67  .  int nExtraReg
2ae1e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2ae1f 65 72 20 6f 66 20 65 78 74 72 61 20 72 65 67 69  er of extra regi
2ae20 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74  sters to allocat
2ae21 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 45  e */.){.  int nE
2ae22 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  q = pLevel->plan
2ae23 2e 6e 45 71 3b 20 20 20 2f 2a 20 54 68 65 20 6e  .nEq;   /* The n
2ae24 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49  umber of == or I
2ae25 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f  N constraints to
2ae26 20 63 6f 64 65 20 2a 2f 0a 20 20 56 64 62 65 20   code */.  Vdbe 
2ae27 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2ae28 62 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  be;      /* The 
2ae29 76 6d 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  vm under constru
2ae2a 63 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e 64 65 78  ction */.  Index
2ae2b 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
2ae2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2ae2d 69 6e 64 65 78 20 62 65 69 6e 67 20 75 73 65 64  index being used
2ae2e 20 66 6f 72 20 74 68 69 73 20 6c 6f 6f 70 20 2a   for this loop *
2ae2f 2f 0a 20 20 69 6e 74 20 69 43 75 72 20 3d 20 70  /.  int iCur = p
2ae30 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 3b 20  Level->iTabCur; 
2ae31 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
2ae32 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
2ae33 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
2ae34 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
2ae35 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63 6f 6e 73  /* A single cons
2ae36 74 72 61 69 6e 74 20 74 65 72 6d 20 2a 2f 0a 20  traint term */. 
2ae37 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20   int j;         
2ae38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ae39 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
2ae3a 2f 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65 3b  /.  int regBase;
2ae3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae3c 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74    /* Base regist
2ae3d 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 67  er */.  int nReg
2ae3e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ae3f 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2ae40 6f 66 20 72 65 67 69 73 74 65 72 73 20 74 6f 20  of registers to 
2ae41 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 0a 20 20 2f  allocate */..  /
2ae42 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 73  * This module is
2ae43 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 6f 6e 20   only called on 
2ae44 71 75 65 72 79 20 70 6c 61 6e 73 20 74 68 61 74  query plans that
2ae45 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2e 20 2a   use an index. *
2ae46 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 76  /.  assert( pLev
2ae47 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2ae48 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44   & WHERE_INDEXED
2ae49 20 29 3b 0a 20 20 70 49 64 78 20 3d 20 70 4c 65   );.  pIdx = pLe
2ae4a 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78  vel->plan.u.pIdx
2ae4b 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  ;..  /* Figure o
2ae4c 75 74 20 68 6f 77 20 6d 61 6e 79 20 6d 65 6d 6f  ut how many memo
2ae4d 72 79 20 63 65 6c 6c 73 20 77 65 20 77 69 6c 6c  ry cells we will
2ae4e 20 6e 65 65 64 20 74 68 65 6e 20 61 6c 6c 6f 63   need then alloc
2ae4f 61 74 65 20 74 68 65 6d 2e 0a 20 20 2a 2f 0a 20  ate them..  */. 
2ae50 20 72 65 67 42 61 73 65 20 3d 20 70 50 61 72 73   regBase = pPars
2ae51 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 6e  e->nMem + 1;.  n
2ae52 52 65 67 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c  Reg = pLevel->pl
2ae53 61 6e 2e 6e 45 71 20 2b 20 6e 45 78 74 72 61 52  an.nEq + nExtraR
2ae54 65 67 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d  eg;.  pParse->nM
2ae55 65 6d 20 2b 3d 20 6e 52 65 67 3b 0a 0a 20 20 2f  em += nReg;..  /
2ae56 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20 65  * Evaluate the e
2ae57 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2ae58 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  nts.  */.  asser
2ae59 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  t( pIdx->nColumn
2ae5a 3e 3d 6e 45 71 20 29 3b 0a 20 20 66 6f 72 28 6a  >=nEq );.  for(j
2ae5b 3d 30 3b 20 6a 3c 6e 45 71 3b 20 6a 2b 2b 29 7b  =0; j<nEq; j++){
2ae5c 0a 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20 20  .    int r1;.   
2ae5d 20 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d 3e 61   int k = pIdx->a
2ae5e 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20  iColumn[j];.    
2ae5f 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d  pTerm = findTerm
2ae60 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e  (pWC, iCur, k, n
2ae61 6f 74 52 65 61 64 79 2c 20 70 4c 65 76 65 6c 2d  otReady, pLevel-
2ae62 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 2c 20 70  >plan.wsFlags, p
2ae63 49 64 78 29 3b 0a 20 20 20 20 69 66 28 20 4e 45  Idx);.    if( NE
2ae64 56 45 52 28 70 54 65 72 6d 3d 3d 30 29 20 29 20  VER(pTerm==0) ) 
2ae65 62 72 65 61 6b 3b 0a 20 20 20 20 61 73 73 65 72  break;.    asser
2ae66 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61  t( (pTerm->wtFla
2ae67 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29  gs & TERM_CODED)
2ae68 3d 3d 30 20 29 3b 0a 20 20 20 20 72 31 20 3d 20  ==0 );.    r1 = 
2ae69 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d  codeEqualityTerm
2ae6a 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c 20  (pParse, pTerm, 
2ae6b 70 4c 65 76 65 6c 2c 20 72 65 67 42 61 73 65 2b  pLevel, regBase+
2ae6c 6a 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21 3d  j);.    if( r1!=
2ae6d 72 65 67 42 61 73 65 2b 6a 20 29 7b 0a 20 20 20  regBase+j ){.   
2ae6e 20 20 20 69 66 28 20 6e 52 65 67 3d 3d 31 20 29     if( nReg==1 )
2ae6f 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2ae70 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2ae71 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 29  pParse, regBase)
2ae72 3b 0a 20 20 20 20 20 20 20 20 72 65 67 42 61 73  ;.        regBas
2ae73 65 20 3d 20 72 31 3b 0a 20 20 20 20 20 20 7d 65  e = r1;.      }e
2ae74 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
2ae75 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2ae76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 31 2c 20  , OP_SCopy, r1, 
2ae77 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20  regBase+j);.    
2ae78 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74 65    }.    }.    te
2ae79 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65  stcase( pTerm->e
2ae7a 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 53  Operator & WO_IS
2ae7b 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 74 65 73 74  NULL );.    test
2ae7c 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70  case( pTerm->eOp
2ae7d 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29  erator & WO_IN )
2ae7e 3b 0a 20 20 20 20 69 66 28 20 28 70 54 65 72 6d  ;.    if( (pTerm
2ae7f 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57  ->eOperator & (W
2ae80 4f 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49 4e 29 29  O_ISNULL|WO_IN))
2ae81 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
2ae82 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2ae83 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67  , OP_IsNull, reg
2ae84 42 61 73 65 2b 6a 2c 20 70 4c 65 76 65 6c 2d 3e  Base+j, pLevel->
2ae85 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20 7d 0a  addrBrk);.    }.
2ae86 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 67    }.  return reg
2ae87 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  Base;.}../*.** R
2ae88 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
2ae89 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 70 57  e WhereClause pW
2ae8a 43 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 74 65  C contains no te
2ae8b 72 6d 73 20 74 68 61 74 0a 2a 2a 20 61 72 65 20  rms that.** are 
2ae8c 6e 6f 74 20 76 69 72 74 75 61 6c 20 61 6e 64 20  not virtual and 
2ae8d 77 68 69 63 68 20 68 61 76 65 20 6e 6f 74 20 62  which have not b
2ae8e 65 65 6e 20 63 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a  een coded..**.**
2ae8f 20 54 6f 20 70 75 74 20 69 74 20 61 6e 6f 74 68   To put it anoth
2ae90 65 72 20 77 61 79 2c 20 72 65 74 75 72 6e 20 54  er way, return T
2ae91 52 55 45 20 69 66 20 6e 6f 20 61 64 64 69 74 69  RUE if no additi
2ae92 6f 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73  onal WHERE claus
2ae93 65 73 0a 2a 2a 20 74 65 73 74 73 20 61 72 65 20  es.** tests are 
2ae94 72 65 71 75 69 72 65 64 20 69 6e 20 6f 72 64 65  required in orde
2ae95 72 20 74 6f 20 65 73 74 61 62 6c 69 73 68 20 74  r to establish t
2ae96 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  hat the current 
2ae97 72 6f 77 0a 2a 2a 20 73 68 6f 75 6c 64 20 67 6f  row.** should go
2ae98 20 74 6f 20 6f 75 74 70 75 74 20 61 6e 64 20 72   to output and r
2ae99 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74  eturn FALSE if t
2ae9a 68 65 72 65 20 61 72 65 20 73 6f 6d 65 20 74 65  here are some te
2ae9b 72 6d 73 20 6f 66 0a 2a 2a 20 74 68 65 20 57 48  rms of.** the WH
2ae9c 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20  ERE clause that 
2ae9d 6e 65 65 64 20 74 6f 20 62 65 20 76 61 6c 69 64  need to be valid
2ae9e 61 74 65 64 20 62 65 66 6f 72 65 20 6f 75 74 70  ated before outp
2ae9f 75 74 69 6e 67 20 74 68 65 20 72 6f 77 2e 0a 2a  uting the row..*
2aea0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 65  /.static int whe
2aea1 72 65 52 6f 77 52 65 61 64 79 46 6f 72 4f 75 74  reRowReadyForOut
2aea2 70 75 74 28 57 68 65 72 65 43 6c 61 75 73 65 20  put(WhereClause 
2aea3 2a 70 57 43 29 7b 0a 20 20 57 68 65 72 65 54 65  *pWC){.  WhereTe
2aea4 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74  rm *pTerm;.  int
2aea5 20 6a 3b 0a 20 0a 20 20 66 6f 72 28 70 54 65 72   j;. .  for(pTer
2aea6 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 70 57 43 2d  m=pWC->a, j=pWC-
2aea7 3e 6e 54 65 72 6d 3b 20 6a 3e 30 3b 20 6a 2d 2d  >nTerm; j>0; j--
2aea8 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  , pTerm++){.    
2aea9 69 66 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c  if( (pTerm->wtFl
2aeaa 61 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54  ags & (TERM_VIRT
2aeab 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 29  UAL|TERM_CODED))
2aeac 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
2aead 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
2aeae 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
2aeaf 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 73  e code for the s
2aeb0 74 61 72 74 20 6f 66 20 74 68 65 20 69 4c 65 76  tart of the iLev
2aeb1 65 6c 2d 74 68 20 6c 6f 6f 70 20 69 6e 20 74 68  el-th loop in th
2aeb2 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a 2a  e WHERE clause.*
2aeb3 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
2aeb4 20 64 65 73 63 72 69 62 65 64 20 62 79 20 70 57   described by pW
2aeb5 49 6e 66 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Info..*/.static 
2aeb6 42 69 74 6d 61 73 6b 20 63 6f 64 65 4f 6e 65 4c  Bitmask codeOneL
2aeb7 6f 6f 70 53 74 61 72 74 28 0a 20 20 57 68 65 72  oopStart(.  Wher
2aeb8 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 2c 20 20  eInfo *pWInfo,  
2aeb9 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 69 6e 66   /* Complete inf
2aeba 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
2aebb 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2aebc 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c  */.  int iLevel,
2aebd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69            /* Whi
2aebe 63 68 20 6c 65 76 65 6c 20 6f 66 20 70 57 49 6e  ch level of pWIn
2aebf 66 6f 2d 3e 61 5b 5d 20 73 68 6f 75 6c 64 20 62  fo->a[] should b
2aec0 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20  e coded */.  u8 
2aec1 77 63 74 72 6c 46 6c 61 67 73 2c 20 20 20 20 20  wctrlFlags,     
2aec2 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20    /* One of the 
2aec3 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 64 65  WHERE_* flags de
2aec4 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49  fined in sqliteI
2aec5 6e 74 2e 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73  nt.h */.  Bitmas
2aec6 6b 20 6e 6f 74 52 65 61 64 79 20 20 20 20 20 2f  k notReady     /
2aec7 2a 20 57 68 69 63 68 20 74 61 62 6c 65 73 20 61  * Which tables a
2aec8 72 65 20 63 75 72 72 65 6e 74 6c 79 20 61 76 61  re currently ava
2aec9 69 6c 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69  ilable */.){.  i
2aeca 6e 74 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20  nt j, k;        
2aecb 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2aecc 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ters */.  int iC
2aecd 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ur;            /
2aece 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
2aecf 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  r for the table 
2aed0 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74  */.  int addrNxt
2aed1 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65  ;         /* Whe
2aed2 72 65 20 74 6f 20 6a 75 6d 70 20 74 6f 20 63 6f  re to jump to co
2aed3 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65 20  ntinue with the 
2aed4 6e 65 78 74 20 49 4e 20 63 61 73 65 20 2a 2f 0a  next IN case */.
2aed5 20 20 69 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b    int omitTable;
2aed6 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
2aed7 66 20 77 65 20 75 73 65 20 74 68 65 20 69 6e 64  f we use the ind
2aed8 65 78 20 6f 6e 6c 79 20 2a 2f 0a 20 20 69 6e 74  ex only */.  int
2aed9 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20   bRev;          
2aeda 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20    /* True if we 
2aedb 6e 65 65 64 20 74 6f 20 73 63 61 6e 20 69 6e 20  need to scan in 
2aedc 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f  reverse order */
2aedd 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70  .  WhereLevel *p
2aede 4c 65 76 65 6c 3b 20 20 2f 2a 20 54 68 65 20 77  Level;  /* The w
2aedf 68 65 72 65 20 6c 65 76 65 6c 20 74 6f 20 62 65  here level to be
2aee0 20 63 6f 64 65 64 20 2a 2f 0a 20 20 57 68 65 72   coded */.  Wher
2aee1 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20  eClause *pWC;   
2aee2 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f   /* Decompositio
2aee3 6e 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20  n of the entire 
2aee4 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2aee5 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
2aee6 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
2aee7 20 20 2f 2a 20 41 20 57 48 45 52 45 20 63 6c 61    /* A WHERE cla
2aee8 75 73 65 20 74 65 72 6d 20 2a 2f 0a 20 20 50 61  use term */.  Pa
2aee9 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20  rse *pParse;    
2aeea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2aeeb 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2aeec 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20   */.  Vdbe *v;  
2aeed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aeee 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65        /* The pre
2aeef 70 61 72 65 64 20 73 74 6d 74 20 75 6e 64 65 72  pared stmt under
2aef0 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a   constructions *
2aef1 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
2aef2 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65  st_item *pTabIte
2aef3 6d 3b 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75  m;  /* FROM clau
2aef4 73 65 20 74 65 72 6d 20 62 65 69 6e 67 20 63 6f  se term being co
2aef5 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ded */.  int add
2aef6 72 42 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20  rBrk;           
2aef7 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
2aef8 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f   here to break o
2aef9 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a  ut of the loop *
2aefa 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74  /.  int addrCont
2aefb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2aefc 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
2aefd 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
2aefe 68 20 6e 65 78 74 20 63 79 63 6c 65 20 2a 2f 0a  h next cycle */.
2aeff 20 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74 3b    int regRowSet;
2af00 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2af01 72 6f 77 69 64 73 20 74 6f 20 74 68 69 73 20 52  rowids to this R
2af02 6f 77 53 65 74 20 69 66 20 6e 6f 6e 2d 6e 65 67  owSet if non-neg
2af03 61 74 69 76 65 20 2a 2f 0a 20 20 69 6e 74 20 63  ative */.  int c
2af04 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 3b 20  odeRowSetEarly; 
2af05 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 64 65 78  /* True if index
2af06 20 66 75 6c 6c 79 20 63 6f 6e 73 74 72 61 69 6e   fully constrain
2af07 73 20 74 68 65 20 73 65 61 72 63 68 20 2a 2f 0a  s the search */.
2af08 20 20 0a 0a 20 20 70 50 61 72 73 65 20 3d 20 70    ..  pParse = p
2af09 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 0a 20  WInfo->pParse;. 
2af0a 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64   v = pParse->pVd
2af0b 62 65 3b 0a 20 20 70 57 43 20 3d 20 70 57 49 6e  be;.  pWC = pWIn
2af0c 66 6f 2d 3e 70 57 43 3b 0a 20 20 70 4c 65 76 65  fo->pWC;.  pLeve
2af0d 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69  l = &pWInfo->a[i
2af0e 4c 65 76 65 6c 5d 3b 0a 20 20 70 54 61 62 49 74  Level];.  pTabIt
2af0f 65 6d 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 70 54  em = &pWInfo->pT
2af10 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c  abList->a[pLevel
2af11 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 69 43 75 72  ->iFrom];.  iCur
2af12 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75   = pTabItem->iCu
2af13 72 73 6f 72 3b 0a 20 20 62 52 65 76 20 3d 20 28  rsor;.  bRev = (
2af14 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2af15 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 45 56  lags & WHERE_REV
2af16 45 52 53 45 29 21 3d 30 3b 0a 20 20 6f 6d 69 74  ERSE)!=0;.  omit
2af17 54 61 62 6c 65 20 3d 20 28 70 4c 65 76 65 6c 2d  Table = (pLevel-
2af18 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2af19 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 21  WHERE_IDX_ONLY)!
2af1a 3d 30 3b 0a 20 20 72 65 67 52 6f 77 53 65 74 20  =0;.  regRowSet 
2af1b 3d 20 70 57 49 6e 66 6f 2d 3e 72 65 67 52 6f 77  = pWInfo->regRow
2af1c 53 65 74 3b 0a 20 20 63 6f 64 65 52 6f 77 53 65  Set;.  codeRowSe
2af1d 74 45 61 72 6c 79 20 3d 20 30 3b 0a 0a 20 20 2f  tEarly = 0;..  /
2af1e 2a 20 43 72 65 61 74 65 20 6c 61 62 65 6c 73 20  * Create labels 
2af1f 66 6f 72 20 74 68 65 20 22 62 72 65 61 6b 22 20  for the "break" 
2af20 61 6e 64 20 22 63 6f 6e 74 69 6e 75 65 22 20 69  and "continue" i
2af21 6e 73 74 72 75 63 74 69 6f 6e 73 0a 20 20 2a 2a  nstructions.  **
2af22 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
2af23 20 6c 6f 6f 70 2e 20 20 4a 75 6d 70 20 74 6f 20   loop.  Jump to 
2af24 61 64 64 72 42 72 6b 20 74 6f 20 62 72 65 61 6b  addrBrk to break
2af25 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 0a   out of a loop..
2af26 20 20 2a 2a 20 4a 75 6d 70 20 74 6f 20 63 6f 6e    ** Jump to con
2af27 74 20 74 6f 20 67 6f 20 69 6d 6d 65 64 69 61 74  t to go immediat
2af28 65 6c 79 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ely to the next 
2af29 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
2af2a 0a 20 20 2a 2a 20 6c 6f 6f 70 2e 0a 20 20 2a 2a  .  ** loop..  **
2af2b 0a 20 20 2a 2a 20 57 68 65 6e 20 74 68 65 72 65  .  ** When there
2af2c 20 69 73 20 61 6e 20 49 4e 20 6f 70 65 72 61 74   is an IN operat
2af2d 6f 72 2c 20 77 65 20 61 6c 73 6f 20 68 61 76 65  or, we also have
2af2e 20 61 20 22 61 64 64 72 4e 78 74 22 20 6c 61 62   a "addrNxt" lab
2af2f 65 6c 20 74 68 61 74 0a 20 20 2a 2a 20 6d 65 61  el that.  ** mea
2af30 6e 73 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77  ns to continue w
2af31 69 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20  ith the next IN 
2af32 76 61 6c 75 65 20 63 6f 6d 62 69 6e 61 74 69 6f  value combinatio
2af33 6e 2e 20 20 57 68 65 6e 0a 20 20 2a 2a 20 74 68  n.  When.  ** th
2af34 65 72 65 20 61 72 65 20 6e 6f 20 49 4e 20 6f 70  ere are no IN op
2af35 65 72 61 74 6f 72 73 20 69 6e 20 74 68 65 20 63  erators in the c
2af36 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65 20  onstraints, the 
2af37 22 61 64 64 72 4e 78 74 22 20 6c 61 62 65 6c 0a  "addrNxt" label.
2af38 20 20 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65    ** is the same
2af39 20 61 73 20 22 61 64 64 72 42 72 6b 22 2e 0a 20   as "addrBrk".. 
2af3a 20 2a 2f 0a 20 20 61 64 64 72 42 72 6b 20 3d 20   */.  addrBrk = 
2af3b 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 20  pLevel->addrBrk 
2af3c 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78  = pLevel->addrNx
2af3d 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  t = sqlite3VdbeM
2af3e 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 61  akeLabel(v);.  a
2af3f 64 64 72 43 6f 6e 74 20 3d 20 70 4c 65 76 65 6c  ddrCont = pLevel
2af40 2d 3e 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c  ->addrCont = sql
2af41 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2af42 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  l(v);..  /* If t
2af43 68 69 73 20 69 73 20 74 68 65 20 72 69 67 68 74  his is the right
2af44 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54   table of a LEFT
2af45 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c   OUTER JOIN, all
2af46 6f 63 61 74 65 20 61 6e 64 0a 20 20 2a 2a 20 69  ocate and.  ** i
2af47 6e 69 74 69 61 6c 69 7a 65 20 61 20 6d 65 6d 6f  nitialize a memo
2af48 72 79 20 63 65 6c 6c 20 74 68 61 74 20 72 65 63  ry cell that rec
2af49 6f 72 64 73 20 69 66 20 74 68 69 73 20 74 61 62  ords if this tab
2af4a 6c 65 20 6d 61 74 63 68 65 73 20 61 6e 79 0a 20  le matches any. 
2af4b 20 2a 2a 20 72 6f 77 20 6f 66 20 74 68 65 20 6c   ** row of the l
2af4c 65 66 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65  eft table of the
2af4d 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66   join..  */.  if
2af4e 28 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 3e  ( pLevel->iFrom>
2af4f 30 20 26 26 20 28 70 54 61 62 49 74 65 6d 5b 30  0 && (pTabItem[0
2af50 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ].jointype & JT_
2af51 4c 45 46 54 29 21 3d 30 20 29 7b 0a 20 20 20 20  LEFT)!=0 ){.    
2af52 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
2af53 6e 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  n = ++pParse->nM
2af54 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  em;.    sqlite3V
2af55 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2af56 49 6e 74 65 67 65 72 2c 20 30 2c 20 70 4c 65 76  Integer, 0, pLev
2af57 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a  el->iLeftJoin);.
2af58 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2af59 28 76 2c 20 22 69 6e 69 74 20 4c 45 46 54 20 4a  (v, "init LEFT J
2af5a 4f 49 4e 20 6e 6f 2d 6d 61 74 63 68 20 66 6c 61  OIN no-match fla
2af5b 67 22 29 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  g"));.  }..#ifnd
2af5c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2af5d 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66  IRTUALTABLE.  if
2af5e 28 20 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  (  (pLevel->plan
2af5f 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2af60 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d  _VIRTUALTABLE)!=
2af61 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65  0 ){.    /* Case
2af62 20 30 3a 20 20 54 68 65 20 74 61 62 6c 65 20 69   0:  The table i
2af63 73 20 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  s a virtual-tabl
2af64 65 2e 20 20 55 73 65 20 74 68 65 20 56 46 69 6c  e.  Use the VFil
2af65 74 65 72 20 61 6e 64 20 56 4e 65 78 74 0a 20 20  ter and VNext.  
2af66 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f    **          to
2af67 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61   access the data
2af68 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
2af69 20 69 52 65 67 3b 20 20 20 2f 2a 20 50 33 20 56   iReg;   /* P3 V
2af6a 61 6c 75 65 20 66 6f 72 20 4f 50 5f 56 46 69 6c  alue for OP_VFil
2af6b 74 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ter */.    sqlit
2af6c 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
2af6d 56 74 61 62 49 64 78 20 3d 20 70 4c 65 76 65 6c  VtabIdx = pLevel
2af6e 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64  ->plan.u.pVtabId
2af6f 78 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73  x;.    int nCons
2af70 74 72 61 69 6e 74 20 3d 20 70 56 74 61 62 49 64  traint = pVtabId
2af71 78 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 0a  x->nConstraint;.
2af72 20 20 20 20 73 74 72 75 63 74 20 73 71 6c 69 74      struct sqlit
2af73 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2af74 69 6e 74 5f 75 73 61 67 65 20 2a 61 55 73 61 67  int_usage *aUsag
2af75 65 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20  e =.            
2af76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af78 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e 61 43      pVtabIdx->aC
2af79 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a  onstraintUsage;.
2af7a 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74      const struct
2af7b 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
2af7c 6f 6e 73 74 72 61 69 6e 74 20 2a 61 43 6f 6e 73  onstraint *aCons
2af7d 74 72 61 69 6e 74 20 3d 0a 20 20 20 20 20 20 20  traint =.       
2af7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af80 20 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64           pVtabId
2af81 78 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a  x->aConstraint;.
2af82 0a 20 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69  .    iReg = sqli
2af83 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
2af84 70 50 61 72 73 65 2c 20 6e 43 6f 6e 73 74 72 61  pParse, nConstra
2af85 69 6e 74 2b 32 29 3b 0a 20 20 20 20 70 50 61 72  int+2);.    pPar
2af86 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
2af87 63 68 65 2b 2b 3b 0a 20 20 20 20 66 6f 72 28 6a  che++;.    for(j
2af88 3d 31 3b 20 6a 3c 3d 6e 43 6f 6e 73 74 72 61 69  =1; j<=nConstrai
2af89 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  nt; j++){.      
2af8a 66 6f 72 28 6b 3d 30 3b 20 6b 3c 6e 43 6f 6e 73  for(k=0; k<nCons
2af8b 74 72 61 69 6e 74 3b 20 6b 2b 2b 29 7b 0a 20 20  traint; k++){.  
2af8c 20 20 20 20 20 20 69 66 28 20 61 55 73 61 67 65        if( aUsage
2af8d 5b 6b 5d 2e 61 72 67 76 49 6e 64 65 78 3d 3d 6a  [k].argvIndex==j
2af8e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
2af8f 74 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74  t iTerm = aConst
2af90 72 61 69 6e 74 5b 6b 5d 2e 69 54 65 72 6d 4f 66  raint[k].iTermOf
2af91 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20  fset;.          
2af92 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2af93 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 20  disableColCache 
2af94 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2af95 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
2af96 72 73 65 2c 20 70 57 43 2d 3e 61 5b 69 54 65 72  rse, pWC->a[iTer
2af97 6d 5d 2e 70 45 78 70 72 2d 3e 70 52 69 67 68 74  m].pExpr->pRight
2af98 2c 20 69 52 65 67 2b 6a 2b 31 29 3b 0a 20 20 20  , iReg+j+1);.   
2af99 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2af9a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2af9b 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 6e 43 6f        if( k==nCo
2af9c 6e 73 74 72 61 69 6e 74 20 29 20 62 72 65 61 6b  nstraint ) break
2af9d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
2af9e 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61  rt( pParse->disa
2af9f 62 6c 65 43 6f 6c 43 61 63 68 65 20 29 3b 0a 20  bleColCache );. 
2afa0 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62     pParse->disab
2afa1 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20  leColCache--;.  
2afa2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2afa3 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2afa4 72 2c 20 70 56 74 61 62 49 64 78 2d 3e 69 64 78  r, pVtabIdx->idx
2afa5 4e 75 6d 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  Num, iReg);.    
2afa6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2afa7 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2afa8 20 6a 2d 31 2c 20 69 52 65 67 2b 31 29 3b 0a 20   j-1, iReg+1);. 
2afa9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2afaa 64 4f 70 34 28 76 2c 20 4f 50 5f 56 46 69 6c 74  dOp4(v, OP_VFilt
2afab 65 72 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72  er, iCur, addrBr
2afac 6b 2c 20 69 52 65 67 2c 20 70 56 74 61 62 49 64  k, iReg, pVtabId
2afad 78 2d 3e 69 64 78 53 74 72 2c 0a 20 20 20 20 20  x->idxStr,.     
2afae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2afaf 20 70 56 74 61 62 49 64 78 2d 3e 6e 65 65 64 54   pVtabIdx->needT
2afb0 6f 46 72 65 65 49 64 78 53 74 72 20 3f 20 50 34  oFreeIdxStr ? P4
2afb1 5f 4d 50 52 49 4e 54 46 20 3a 20 50 34 5f 53 54  _MPRINTF : P4_ST
2afb2 41 54 49 43 29 3b 0a 20 20 20 20 70 56 74 61 62  ATIC);.    pVtab
2afb3 49 64 78 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  Idx->needToFreeI
2afb4 64 78 53 74 72 20 3d 20 30 3b 0a 20 20 20 20 66  dxStr = 0;.    f
2afb5 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f 6e 73 74  or(j=0; j<nConst
2afb6 72 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20  raint; j++){.   
2afb7 20 20 20 69 66 28 20 61 55 73 61 67 65 5b 6a 5d     if( aUsage[j]
2afb8 2e 6f 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20  .omit ){.       
2afb9 20 69 6e 74 20 69 54 65 72 6d 20 3d 20 61 43 6f   int iTerm = aCo
2afba 6e 73 74 72 61 69 6e 74 5b 6a 5d 2e 69 54 65 72  nstraint[j].iTer
2afbb 6d 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20  mOffset;.       
2afbc 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
2afbd 76 65 6c 2c 20 26 70 57 43 2d 3e 61 5b 69 54 65  vel, &pWC->a[iTe
2afbe 72 6d 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rm]);.      }.  
2afbf 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e    }.    pLevel->
2afc0 6f 70 20 3d 20 4f 50 5f 56 4e 65 78 74 3b 0a 20  op = OP_VNext;. 
2afc1 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20     pLevel->p1 = 
2afc2 69 43 75 72 3b 0a 20 20 20 20 70 4c 65 76 65 6c  iCur;.    pLevel
2afc3 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64  ->p2 = sqlite3Vd
2afc4 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2afc5 3b 0a 20 20 20 20 63 6f 64 65 52 6f 77 53 65 74  ;.    codeRowSet
2afc6 45 61 72 6c 79 20 3d 20 72 65 67 52 6f 77 53 65  Early = regRowSe
2afc7 74 3e 3d 30 20 3f 20 77 68 65 72 65 52 6f 77 52  t>=0 ? whereRowR
2afc8 65 61 64 79 46 6f 72 4f 75 74 70 75 74 28 70 57  eadyForOutput(pW
2afc9 43 29 20 3a 20 30 3b 0a 20 20 20 20 69 66 28 20  C) : 0;.    if( 
2afca 63 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20  codeRowSetEarly 
2afcb 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2afcc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2afcd 5f 56 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 69  _VRowid, iCur, i
2afce 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Reg);.      sqli
2afcf 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2afd0 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 2c 20 72   OP_RowSetAdd, r
2afd1 65 67 52 6f 77 53 65 74 2c 20 69 52 65 67 29 3b  egRowSet, iReg);
2afd2 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2afd3 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
2afd4 67 65 28 70 50 61 72 73 65 2c 20 69 52 65 67 2c  ge(pParse, iReg,
2afd5 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b   nConstraint+2);
2afd6 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
2afd7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
2afd8 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
2afd9 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c    if( pLevel->pl
2afda 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2afdb 52 45 5f 52 4f 57 49 44 5f 45 51 20 29 7b 0a 20  RE_ROWID_EQ ){. 
2afdc 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 57     /* Case 1:  W
2afdd 65 20 63 61 6e 20 64 69 72 65 63 74 6c 79 20 72  e can directly r
2afde 65 66 65 72 65 6e 63 65 20 61 20 73 69 6e 67 6c  eference a singl
2afdf 65 20 72 6f 77 20 75 73 69 6e 67 20 61 6e 0a 20  e row using an. 
2afe0 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 65     **          e
2afe1 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72 69 73  quality comparis
2afe2 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65 20 52  on against the R
2afe3 4f 57 49 44 20 66 69 65 6c 64 2e 20 20 4f 72 0a  OWID field.  Or.
2afe4 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
2afe5 77 65 20 72 65 66 65 72 65 6e 63 65 20 6d 75 6c  we reference mul
2afe6 74 69 70 6c 65 20 72 6f 77 73 20 75 73 69 6e 67  tiple rows using
2afe7 20 61 20 22 72 6f 77 69 64 20 49 4e 20 28 2e 2e   a "rowid IN (..
2afe8 2e 29 22 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  .)".    **      
2afe9 20 20 20 20 63 6f 6e 73 74 72 75 63 74 2e 0a 20      construct.. 
2afea 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 31     */.    int r1
2afeb 3b 0a 20 20 20 20 69 6e 74 20 72 74 6d 70 20 3d  ;.    int rtmp =
2afec 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2afed 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2afee 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d  pTerm = findTerm
2afef 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20  (pWC, iCur, -1, 
2aff0 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c  notReady, WO_EQ|
2aff1 57 4f 5f 49 4e 2c 20 30 29 3b 0a 20 20 20 20 61  WO_IN, 0);.    a
2aff2 73 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20  ssert( pTerm!=0 
2aff3 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2aff4 54 65 72 6d 2d 3e 70 45 78 70 72 21 3d 30 20 29  Term->pExpr!=0 )
2aff5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
2aff6 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d  erm->leftCursor=
2aff7 3d 69 43 75 72 20 29 3b 0a 20 20 20 20 61 73 73  =iCur );.    ass
2aff8 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d  ert( omitTable==
2aff9 30 20 29 3b 0a 20 20 20 20 72 31 20 3d 20 63 6f  0 );.    r1 = co
2affa 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 70  deEqualityTerm(p
2affb 50 61 72 73 65 2c 20 70 54 65 72 6d 2c 20 70 4c  Parse, pTerm, pL
2affc 65 76 65 6c 2c 20 72 74 6d 70 29 3b 0a 20 20 20  evel, rtmp);.   
2affd 20 61 64 64 72 4e 78 74 20 3d 20 70 4c 65 76 65   addrNxt = pLeve
2affe 6c 2d 3e 61 64 64 72 4e 78 74 3b 0a 20 20 20 20  l->addrNxt;.    
2afff 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b000 32 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e  2(v, OP_MustBeIn
2b001 74 2c 20 72 31 2c 20 61 64 64 72 4e 78 74 29 3b  t, r1, addrNxt);
2b002 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b003 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp3(v, OP_Not
2b004 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64  Exists, iCur, ad
2b005 64 72 4e 78 74 2c 20 72 31 29 3b 0a 20 20 20 20  drNxt, r1);.    
2b006 63 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20  codeRowSetEarly 
2b007 3d 20 28 70 57 43 2d 3e 6e 54 65 72 6d 3d 3d 31  = (pWC->nTerm==1
2b008 20 26 26 20 72 65 67 52 6f 77 53 65 74 3e 3d 30   && regRowSet>=0
2b009 29 20 3f 31 3a 30 3b 0a 20 20 20 20 69 66 28 20  ) ?1:0;.    if( 
2b00a 63 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20  codeRowSetEarly 
2b00b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2b00c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b00d 5f 52 6f 77 53 65 74 41 64 64 2c 20 72 65 67 52  _RowSetAdd, regR
2b00e 6f 77 53 65 74 2c 20 72 31 29 3b 0a 20 20 20 20  owSet, r1);.    
2b00f 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  }.    sqlite3Rel
2b010 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
2b011 73 65 2c 20 72 74 6d 70 29 3b 0a 20 20 20 20 56  se, rtmp);.    V
2b012 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2b013 70 6b 22 29 29 3b 0a 20 20 20 20 70 4c 65 76 65  pk"));.    pLeve
2b014 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b  l->op = OP_Noop;
2b015 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65  .  }else if( pLe
2b016 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2b017 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f  s & WHERE_ROWID_
2b018 52 41 4e 47 45 20 29 7b 0a 20 20 20 20 2f 2a 20  RANGE ){.    /* 
2b019 43 61 73 65 20 32 3a 20 20 57 65 20 68 61 76 65  Case 2:  We have
2b01a 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 20 63   an inequality c
2b01b 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73  omparison agains
2b01c 74 20 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c  t the ROWID fiel
2b01d 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  d..    */.    in
2b01e 74 20 74 65 73 74 4f 70 20 3d 20 4f 50 5f 4e 6f  t testOp = OP_No
2b01f 6f 70 3b 0a 20 20 20 20 69 6e 74 20 73 74 61 72  op;.    int star
2b020 74 3b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 45 6e  t;.    int memEn
2b021 64 56 61 6c 75 65 20 3d 20 30 3b 0a 20 20 20 20  dValue = 0;.    
2b022 57 68 65 72 65 54 65 72 6d 20 2a 70 53 74 61 72  WhereTerm *pStar
2b023 74 2c 20 2a 70 45 6e 64 3b 0a 0a 20 20 20 20 61  t, *pEnd;..    a
2b024 73 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65  ssert( omitTable
2b025 3d 3d 30 20 29 3b 0a 20 20 20 20 70 53 74 61 72  ==0 );.    pStar
2b026 74 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43  t = findTerm(pWC
2b027 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52  , iCur, -1, notR
2b028 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47  eady, WO_GT|WO_G
2b029 45 2c 20 30 29 3b 0a 20 20 20 20 70 45 6e 64 20  E, 0);.    pEnd 
2b02a 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
2b02b 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61  iCur, -1, notRea
2b02c 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c  dy, WO_LT|WO_LE,
2b02d 20 30 29 3b 0a 20 20 20 20 69 66 28 20 62 52 65   0);.    if( bRe
2b02e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 65 72 6d  v ){.      pTerm
2b02f 20 3d 20 70 53 74 61 72 74 3b 0a 20 20 20 20 20   = pStart;.     
2b030 20 70 53 74 61 72 74 20 3d 20 70 45 6e 64 3b 0a   pStart = pEnd;.
2b031 20 20 20 20 20 20 70 45 6e 64 20 3d 20 70 54 65        pEnd = pTe
2b032 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rm;.    }.    if
2b033 28 20 70 53 74 61 72 74 20 29 7b 0a 20 20 20 20  ( pStart ){.    
2b034 20 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20    Expr *pX;     
2b035 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
2b036 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 64  xpression that d
2b037 65 66 69 6e 65 73 20 74 68 65 20 73 74 61 72 74  efines the start
2b038 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20   bound */.      
2b039 69 6e 74 20 72 31 2c 20 72 54 65 6d 70 3b 20 20  int r1, rTemp;  
2b03a 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
2b03b 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74  rs for holding t
2b03c 68 65 20 73 74 61 72 74 20 62 6f 75 6e 64 61 72  he start boundar
2b03d 79 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 54  y */..      /* T
2b03e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e  he following con
2b03f 73 74 61 6e 74 20 6d 61 70 73 20 54 4b 5f 78 78  stant maps TK_xx
2b040 20 63 6f 64 65 73 20 69 6e 74 6f 20 63 6f 72 72   codes into corr
2b041 65 73 70 6f 6e 64 69 6e 67 20 0a 20 20 20 20 20  esponding .     
2b042 20 2a 2a 20 73 65 65 6b 20 6f 70 63 6f 64 65 73   ** seek opcodes
2b043 2e 20 20 49 74 20 64 65 70 65 6e 64 73 20 6f 6e  .  It depends on
2b044 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72   a particular or
2b045 64 65 72 69 6e 67 20 6f 66 20 54 4b 5f 78 78 0a  dering of TK_xx.
2b046 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63        */.      c
2b047 6f 6e 73 74 20 75 38 20 61 4d 6f 76 65 4f 70 5b  onst u8 aMoveOp[
2b048 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ] = {.          
2b049 20 2f 2a 20 54 4b 5f 47 54 20 2a 2f 20 20 4f 50   /* TK_GT */  OP
2b04a 5f 53 65 65 6b 47 74 2c 0a 20 20 20 20 20 20 20  _SeekGt,.       
2b04b 20 20 20 20 2f 2a 20 54 4b 5f 4c 45 20 2a 2f 20      /* TK_LE */ 
2b04c 20 4f 50 5f 53 65 65 6b 4c 65 2c 0a 20 20 20 20   OP_SeekLe,.    
2b04d 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 54 20         /* TK_LT 
2b04e 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c 74 2c 0a 20  */  OP_SeekLt,. 
2b04f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f            /* TK_
2b050 47 45 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 47 65  GE */  OP_SeekGe
2b051 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20  .      };.      
2b052 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 54  assert( TK_LE==T
2b053 4b 5f 47 54 2b 31 20 29 3b 20 20 20 20 20 20 2f  K_GT+1 );      /
2b054 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
2b055 6f 72 64 65 72 69 6e 67 2e 2e 20 2a 2f 0a 20 20  ordering.. */.  
2b056 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
2b057 54 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b 20 20 20  T==TK_GT+2 );   
2b058 20 20 20 2f 2a 20 20 2e 2e 2e 20 6f 66 20 74 68     /*  ... of th
2b059 65 20 54 4b 5f 78 78 20 76 61 6c 75 65 73 2e 2e  e TK_xx values..
2b05a 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  . */.      asser
2b05b 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 47 54 2b  t( TK_GE==TK_GT+
2b05c 33 20 29 3b 20 20 20 20 20 20 2f 2a 20 20 2e 2e  3 );      /*  ..
2b05d 2e 20 69 73 20 63 6f 72 72 65 63 63 74 2e 20 2a  . is correcct. *
2b05e 2f 0a 0a 20 20 20 20 20 20 70 58 20 3d 20 70 53  /..      pX = pS
2b05f 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a 20 20 20  tart->pExpr;.   
2b060 20 20 20 61 73 73 65 72 74 28 20 70 58 21 3d 30     assert( pX!=0
2b061 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
2b062 28 20 70 53 74 61 72 74 2d 3e 6c 65 66 74 43 75  ( pStart->leftCu
2b063 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a 20 20  rsor==iCur );.  
2b064 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
2b065 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
2b066 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c  rse, pX->pRight,
2b067 20 26 72 54 65 6d 70 29 3b 0a 20 20 20 20 20 20   &rTemp);.      
2b068 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b069 33 28 76 2c 20 61 4d 6f 76 65 4f 70 5b 70 58 2d  3(v, aMoveOp[pX-
2b06a 3e 6f 70 2d 54 4b 5f 47 54 5d 2c 20 69 43 75 72  >op-TK_GT], iCur
2b06b 2c 20 61 64 64 72 42 72 6b 2c 20 72 31 29 3b 0a  , addrBrk, r1);.
2b06c 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2b06d 74 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20  t((v, "pk"));.  
2b06e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2b06f 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
2b070 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 31  ge(pParse, r1, 1
2b071 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b072 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2b073 50 61 72 73 65 2c 20 72 54 65 6d 70 29 3b 0a 20  Parse, rTemp);. 
2b074 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d       disableTerm
2b075 28 70 4c 65 76 65 6c 2c 20 70 53 74 61 72 74 29  (pLevel, pStart)
2b076 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2b077 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b078 64 4f 70 32 28 76 2c 20 62 52 65 76 20 3f 20 4f  dOp2(v, bRev ? O
2b079 50 5f 4c 61 73 74 20 3a 20 4f 50 5f 52 65 77 69  P_Last : OP_Rewi
2b07a 6e 64 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72  nd, iCur, addrBr
2b07b 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  k);.    }.    if
2b07c 28 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20  ( pEnd ){.      
2b07d 45 78 70 72 20 2a 70 58 3b 0a 20 20 20 20 20 20  Expr *pX;.      
2b07e 70 58 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70 72  pX = pEnd->pExpr
2b07f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2b080 70 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  pX!=0 );.      a
2b081 73 73 65 72 74 28 20 70 45 6e 64 2d 3e 6c 65 66  ssert( pEnd->lef
2b082 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b  tCursor==iCur );
2b083 0a 20 20 20 20 20 20 6d 65 6d 45 6e 64 56 61 6c  .      memEndVal
2b084 75 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  ue = ++pParse->n
2b085 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Mem;.      sqlit
2b086 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
2b087 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 6d  e, pX->pRight, m
2b088 65 6d 45 6e 64 56 61 6c 75 65 29 3b 0a 20 20 20  emEndValue);.   
2b089 20 20 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54     if( pX->op==T
2b08a 4b 5f 4c 54 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d  K_LT || pX->op==
2b08b 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20 20 20 20  TK_GT ){.       
2b08c 20 74 65 73 74 4f 70 20 3d 20 62 52 65 76 20 3f   testOp = bRev ?
2b08d 20 4f 50 5f 4c 65 20 3a 20 4f 50 5f 47 65 3b 0a   OP_Le : OP_Ge;.
2b08e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2b08f 20 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52       testOp = bR
2b090 65 76 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f  ev ? OP_Lt : OP_
2b091 47 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  Gt;.      }.    
2b092 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c    disableTerm(pL
2b093 65 76 65 6c 2c 20 70 45 6e 64 29 3b 0a 20 20 20  evel, pEnd);.   
2b094 20 7d 0a 20 20 20 20 73 74 61 72 74 20 3d 20 73   }.    start = s
2b095 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2b096 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 70 4c  tAddr(v);.    pL
2b097 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76 20  evel->op = bRev 
2b098 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f 4e  ? OP_Prev : OP_N
2b099 65 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  ext;.    pLevel-
2b09a 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20  >p1 = iCur;.    
2b09b 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74 61  pLevel->p2 = sta
2b09c 72 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  rt;.    pLevel->
2b09d 70 35 20 3d 20 28 70 53 74 61 72 74 3d 3d 30 20  p5 = (pStart==0 
2b09e 26 26 20 70 45 6e 64 3d 3d 30 29 20 3f 31 3a 30  && pEnd==0) ?1:0
2b09f 3b 0a 20 20 20 20 63 6f 64 65 52 6f 77 53 65 74  ;.    codeRowSet
2b0a0 45 61 72 6c 79 20 3d 20 72 65 67 52 6f 77 53 65  Early = regRowSe
2b0a1 74 3e 3d 30 20 3f 20 77 68 65 72 65 52 6f 77 52  t>=0 ? whereRowR
2b0a2 65 61 64 79 46 6f 72 4f 75 74 70 75 74 28 70 57  eadyForOutput(pW
2b0a3 43 29 20 3a 20 30 3b 0a 20 20 20 20 69 66 28 20  C) : 0;.    if( 
2b0a4 63 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20  codeRowSetEarly 
2b0a5 7c 7c 20 74 65 73 74 4f 70 21 3d 4f 50 5f 4e 6f  || testOp!=OP_No
2b0a6 6f 70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  op ){.      int 
2b0a7 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
2b0a8 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
2b0a9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b0aa 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
2b0ab 77 69 64 2c 20 69 43 75 72 2c 20 72 31 29 3b 0a  wid, iCur, r1);.
2b0ac 20 20 20 20 20 20 69 66 28 20 74 65 73 74 4f 70        if( testOp
2b0ad 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20  !=OP_Noop ){.   
2b0ae 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b0af 41 64 64 4f 70 33 28 76 2c 20 74 65 73 74 4f 70  AddOp3(v, testOp
2b0b0 2c 20 6d 65 6d 45 6e 64 56 61 6c 75 65 2c 20 61  , memEndValue, a
2b0b1 64 64 72 42 72 6b 2c 20 72 31 29 3b 0a 20 20 20  ddrBrk, r1);.   
2b0b2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b0b3 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49  ChangeP5(v, SQLI
2b0b4 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c  TE_AFF_NUMERIC |
2b0b5 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
2b0b6 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  LL);.      }.   
2b0b7 20 20 20 69 66 28 20 63 6f 64 65 52 6f 77 53 65     if( codeRowSe
2b0b8 74 45 61 72 6c 79 20 29 7b 0a 20 20 20 20 20 20  tEarly ){.      
2b0b9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b0ba 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74  Op2(v, OP_RowSet
2b0bb 41 64 64 2c 20 72 65 67 52 6f 77 53 65 74 2c 20  Add, regRowSet, 
2b0bc 72 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  r1);.      }.   
2b0bd 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2b0be 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2b0bf 20 72 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   r1);.    }.  }e
2b0c0 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  lse if( pLevel->
2b0c1 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 28  plan.wsFlags & (
2b0c2 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e  WHERE_COLUMN_RAN
2b0c3 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  GE|WHERE_COLUMN_
2b0c4 45 51 29 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61  EQ) ){.    /* Ca
2b0c5 73 65 20 33 3a 20 41 20 73 63 61 6e 20 75 73 69  se 3: A scan usi
2b0c6 6e 67 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 20  ng an index..   
2b0c7 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
2b0c8 20 20 20 54 68 65 20 57 48 45 52 45 20 63 6c 61     The WHERE cla
2b0c9 75 73 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  use may contain 
2b0ca 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 65 71 75  zero or more equ
2b0cb 61 6c 69 74 79 20 0a 20 20 20 20 2a 2a 20 20 20  ality .    **   
2b0cc 20 20 20 20 20 20 74 65 72 6d 73 20 28 22 3d 3d        terms ("==
2b0cd 22 20 6f 72 20 22 49 4e 22 20 6f 70 65 72 61 74  " or "IN" operat
2b0ce 6f 72 73 29 20 74 68 61 74 20 72 65 66 65 72 20  ors) that refer 
2b0cf 74 6f 20 74 68 65 20 4e 0a 20 20 20 20 2a 2a 20  to the N.    ** 
2b0d0 20 20 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73          left-mos
2b0d1 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  t columns of the
2b0d2 20 69 6e 64 65 78 2e 20 49 74 20 6d 61 79 20 61   index. It may a
2b0d3 6c 73 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20  lso contain.    
2b0d4 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 65 71 75  **         inequ
2b0d5 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2b0d6 73 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c  s (>, <, >= or <
2b0d7 3d 29 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 65  =) on the indexe
2b0d8 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  d.    **        
2b0d9 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 6d 6d   column that imm
2b0da 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73  ediately follows
2b0db 20 74 68 65 20 4e 20 65 71 75 61 6c 69 74 69 65   the N equalitie
2b0dc 73 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 2a 2a 20  s. Only .    ** 
2b0dd 20 20 20 20 20 20 20 20 74 68 65 20 72 69 67 68          the righ
2b0de 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 63 61  t-most column ca
2b0df 6e 20 62 65 20 61 6e 20 69 6e 65 71 75 61 6c 69  n be an inequali
2b0e0 74 79 20 2d 20 74 68 65 20 72 65 73 74 20 6d 75  ty - the rest mu
2b0e1 73 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  st.    **       
2b0e2 20 20 75 73 65 20 74 68 65 20 22 3d 3d 22 20 61    use the "==" a
2b0e3 6e 64 20 22 49 4e 22 20 6f 70 65 72 61 74 6f 72  nd "IN" operator
2b0e4 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  s. For example, 
2b0e5 69 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20  if the .    **  
2b0e6 20 20 20 20 20 20 20 69 6e 64 65 78 20 69 73 20         index is 
2b0e7 6f 6e 20 28 78 2c 79 2c 7a 29 2c 20 74 68 65 6e  on (x,y,z), then
2b0e8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
2b0e9 6c 61 75 73 65 73 20 61 72 65 20 61 6c 6c 20 0a  lauses are all .
2b0ea 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6f      **         o
2b0eb 70 74 69 6d 69 7a 65 64 3a 0a 20 20 20 20 2a 2a  ptimized:.    **
2b0ec 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2b0ed 20 20 20 78 3d 35 0a 20 20 20 20 2a 2a 20 20 20     x=5.    **   
2b0ee 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44           x=5 AND
2b0ef 20 79 3d 31 30 0a 20 20 20 20 2a 2a 20 20 20 20   y=10.    **    
2b0f0 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
2b0f1 79 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20  y<10.    **     
2b0f2 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79         x=5 AND y
2b0f3 3e 35 20 41 4e 44 20 79 3c 31 30 0a 20 20 20 20  >5 AND y<10.    
2b0f4 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 3d  **            x=
2b0f5 35 20 41 4e 44 20 79 3d 35 20 41 4e 44 20 7a 3c  5 AND y=5 AND z<
2b0f6 3d 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  =10.    **.    *
2b0f7 2a 20 20 20 20 20 20 20 20 20 54 68 65 20 7a 3c  *         The z<
2b0f8 31 30 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66  10 term of the f
2b0f9 6f 6c 6c 6f 77 69 6e 67 20 63 61 6e 6e 6f 74 20  ollowing cannot 
2b0fa 62 65 20 75 73 65 64 2c 20 6f 6e 6c 79 0a 20 20  be used, only.  
2b0fb 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65    **         the
2b0fc 20 78 3d 35 20 74 65 72 6d 3a 0a 20 20 20 20 2a   x=5 term:.    *
2b0fd 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
2b0fe 20 20 20 20 78 3d 35 20 41 4e 44 20 7a 3c 31 30      x=5 AND z<10
2b0ff 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2b100 20 20 20 20 20 20 20 4e 20 6d 61 79 20 62 65 20         N may be 
2b101 7a 65 72 6f 20 69 66 20 74 68 65 72 65 20 61 72  zero if there ar
2b102 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  e inequality con
2b103 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2a  straints..    **
2b104 20 20 20 20 20 20 20 20 20 49 66 20 74 68 65 72           If ther
2b105 65 20 61 72 65 20 6e 6f 20 69 6e 65 71 75 61 6c  e are no inequal
2b106 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c  ity constraints,
2b107 20 74 68 65 6e 20 4e 20 69 73 20 61 74 0a 20 20   then N is at.  
2b108 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 65 61    **         lea
2b109 73 74 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20  st one..    **. 
2b10a 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68     **         Th
2b10b 69 73 20 63 61 73 65 20 69 73 20 61 6c 73 6f 20  is case is also 
2b10c 75 73 65 64 20 77 68 65 6e 20 74 68 65 72 65 20  used when there 
2b10d 61 72 65 20 6e 6f 20 57 48 45 52 45 20 63 6c 61  are no WHERE cla
2b10e 75 73 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  use.    **      
2b10f 20 20 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62     constraints b
2b110 75 74 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73  ut an index is s
2b111 65 6c 65 63 74 65 64 20 61 6e 79 77 61 79 2c 20  elected anyway, 
2b112 69 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20  in order.    ** 
2b113 20 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63 65          to force
2b114 20 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65   the output orde
2b115 72 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20  r to conform to 
2b116 61 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20 20 20  an ORDER BY..   
2b117 20 2a 2f 20 20 0a 20 20 20 20 69 6e 74 20 61 53   */  .    int aS
2b118 74 61 72 74 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20  tartOp[] = {.   
2b119 20 20 20 30 2c 0a 20 20 20 20 20 20 30 2c 0a 20     0,.      0,. 
2b11a 20 20 20 20 20 4f 50 5f 52 65 77 69 6e 64 2c 20       OP_Rewind, 
2b11b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 3a 20            /* 2: 
2b11c 28 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69  (!start_constrai
2b11d 6e 74 73 20 26 26 20 73 74 61 72 74 45 71 20 26  nts && startEq &
2b11e 26 20 20 21 62 52 65 76 29 20 2a 2f 0a 20 20 20  &  !bRev) */.   
2b11f 20 20 20 4f 50 5f 4c 61 73 74 2c 20 20 20 20 20     OP_Last,     
2b120 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 28 21          /* 3: (!
2b121 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74  start_constraint
2b122 73 20 26 26 20 73 74 61 72 74 45 71 20 26 26 20  s && startEq && 
2b123 20 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20    bRev) */.     
2b124 20 4f 50 5f 53 65 65 6b 47 74 2c 20 20 20 20 20   OP_SeekGt,     
2b125 20 20 20 20 20 20 2f 2a 20 34 3a 20 28 73 74 61        /* 4: (sta
2b126 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20  rt_constraints  
2b127 26 26 20 21 73 74 61 72 74 45 71 20 26 26 20 21  && !startEq && !
2b128 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f  bRev) */.      O
2b129 50 5f 53 65 65 6b 4c 74 2c 20 20 20 20 20 20 20  P_SeekLt,       
2b12a 20 20 20 20 2f 2a 20 35 3a 20 28 73 74 61 72 74      /* 5: (start
2b12b 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26  _constraints  &&
2b12c 20 21 73 74 61 72 74 45 71 20 26 26 20 20 62 52   !startEq &&  bR
2b12d 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f  ev) */.      OP_
2b12e 53 65 65 6b 47 65 2c 20 20 20 20 20 20 20 20 20  SeekGe,         
2b12f 20 20 2f 2a 20 36 3a 20 28 73 74 61 72 74 5f 63    /* 6: (start_c
2b130 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 20  onstraints  &&  
2b131 73 74 61 72 74 45 71 20 26 26 20 21 62 52 65 76  startEq && !bRev
2b132 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65  ) */.      OP_Se
2b133 65 6b 4c 65 20 20 20 20 20 20 20 20 20 20 20 20  ekLe            
2b134 2f 2a 20 37 3a 20 28 73 74 61 72 74 5f 63 6f 6e  /* 7: (start_con
2b135 73 74 72 61 69 6e 74 73 20 20 26 26 20 20 73 74  straints  &&  st
2b136 61 72 74 45 71 20 26 26 20 20 62 52 65 76 29 20  artEq &&  bRev) 
2b137 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e  */.    };.    in
2b138 74 20 61 45 6e 64 4f 70 5b 5d 20 3d 20 7b 0a 20  t aEndOp[] = {. 
2b139 20 20 20 20 20 4f 50 5f 4e 6f 6f 70 2c 20 20 20       OP_Noop,   
2b13a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 3a 20            /* 0: 
2b13b 28 21 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74  (!end_constraint
2b13c 73 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 49  s) */.      OP_I
2b13d 64 78 47 45 2c 20 20 20 20 20 20 20 20 20 20 20  dxGE,           
2b13e 20 2f 2a 20 31 3a 20 28 65 6e 64 5f 63 6f 6e 73   /* 1: (end_cons
2b13f 74 72 61 69 6e 74 73 20 26 26 20 21 62 52 65 76  traints && !bRev
2b140 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 49 64  ) */.      OP_Id
2b141 78 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20  xLT             
2b142 2f 2a 20 32 3a 20 28 65 6e 64 5f 63 6f 6e 73 74  /* 2: (end_const
2b143 72 61 69 6e 74 73 20 26 26 20 62 52 65 76 29 20  raints && bRev) 
2b144 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e  */.    };.    in
2b145 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e  t nEq = pLevel->
2b146 70 6c 61 6e 2e 6e 45 71 3b 0a 20 20 20 20 69 6e  plan.nEq;.    in
2b147 74 20 69 73 4d 69 6e 51 75 65 72 79 20 3d 20 30  t isMinQuery = 0
2b148 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ;          /* If
2b149 20 74 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69   this is an opti
2b14a 6d 69 7a 65 64 20 53 45 4c 45 43 54 20 6d 69 6e  mized SELECT min
2b14b 28 78 29 2e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74  (x).. */.    int
2b14c 20 72 65 67 42 61 73 65 3b 20 20 20 20 20 20 20   regBase;       
2b14d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73            /* Bas
2b14e 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69  e register holdi
2b14f 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 61  ng constraint va
2b150 6c 75 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20  lues */.    int 
2b151 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  r1;             
2b152 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
2b153 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20   register */.   
2b154 20 57 68 65 72 65 54 65 72 6d 20 2a 70 52 61 6e   WhereTerm *pRan
2b155 67 65 53 74 61 72 74 20 3d 20 30 3b 20 20 2f 2a  geStart = 0;  /*
2b156 20 49 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73   Inequality cons
2b157 74 72 61 69 6e 74 20 61 74 20 72 61 6e 67 65 20  traint at range 
2b158 73 74 61 72 74 20 2a 2f 0a 20 20 20 20 57 68 65  start */.    Whe
2b159 72 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 45 6e  reTerm *pRangeEn
2b15a 64 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 6e 65  d = 0;    /* Ine
2b15b 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2b15c 6e 74 20 61 74 20 72 61 6e 67 65 20 65 6e 64 20  nt at range end 
2b15d 2a 2f 0a 20 20 20 20 69 6e 74 20 73 74 61 72 74  */.    int start
2b15e 45 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Eq;             
2b15f 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72      /* True if r
2b160 61 6e 67 65 20 73 74 61 72 74 20 75 73 65 73 20  ange start uses 
2b161 3d 3d 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a  ==, >= or <= */.
2b162 20 20 20 20 69 6e 74 20 65 6e 64 45 71 3b 20 20      int endEq;  
2b163 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b164 20 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e 67   /* True if rang
2b165 65 20 65 6e 64 20 75 73 65 73 20 3d 3d 2c 20 3e  e end uses ==, >
2b166 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20 69  = or <= */.    i
2b167 6e 74 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61  nt start_constra
2b168 69 6e 74 73 3b 20 20 20 20 20 20 20 2f 2a 20 53  ints;       /* S
2b169 74 61 72 74 20 6f 66 20 72 61 6e 67 65 20 69 73  tart of range is
2b16a 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 2a 2f 0a   constrained */.
2b16b 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61      int nConstra
2b16c 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  int;            
2b16d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
2b16e 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 2a  nstraint terms *
2b16f 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  /.    Index *pId
2b170 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  x;         /* Th
2b171 65 20 69 6e 64 65 78 20 77 65 20 77 69 6c 6c 20  e index we will 
2b172 62 65 20 75 73 69 6e 67 20 2a 2f 0a 20 20 20 20  be using */.    
2b173 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20  int iIdxCur;    
2b174 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45       /* The VDBE
2b175 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20   cursor for the 
2b176 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74  index */.    int
2b177 20 6e 45 78 74 72 61 52 65 67 20 3d 20 30 3b 20   nExtraReg = 0; 
2b178 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
2b179 78 74 72 61 20 72 65 67 69 73 74 65 72 73 20 6e  xtra registers n
2b17a 65 65 64 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74  eeded */.    int
2b17b 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   op;            
2b17c 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e    /* Instruction
2b17d 20 6f 70 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20   opcode */..    
2b17e 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70  pIdx = pLevel->p
2b17f 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20 20 20  lan.u.pIdx;.    
2b180 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65 6c  iIdxCur = pLevel
2b181 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 6b  ->iIdxCur;.    k
2b182 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d   = pIdx->aiColum
2b183 6e 5b 6e 45 71 5d 3b 20 20 20 20 20 2f 2a 20 43  n[nEq];     /* C
2b184 6f 6c 75 6d 6e 20 66 6f 72 20 69 6e 65 71 75 61  olumn for inequa
2b185 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2b186 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74   */..    /* If t
2b187 68 69 73 20 6c 6f 6f 70 20 73 61 74 69 73 66 69  his loop satisfi
2b188 65 73 20 61 20 73 6f 72 74 20 6f 72 64 65 72 20  es a sort order 
2b189 28 70 4f 72 64 65 72 42 79 29 20 72 65 71 75 65  (pOrderBy) reque
2b18a 73 74 20 74 68 61 74 20 0a 20 20 20 20 2a 2a 20  st that .    ** 
2b18b 77 61 73 20 70 61 73 73 65 64 20 74 6f 20 74 68  was passed to th
2b18c 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69  is function to i
2b18d 6d 70 6c 65 6d 65 6e 74 20 61 20 22 53 45 4c 45  mplement a "SELE
2b18e 43 54 20 6d 69 6e 28 78 29 20 2e 2e 2e 22 20 0a  CT min(x) ..." .
2b18f 20 20 20 20 2a 2a 20 71 75 65 72 79 2c 20 74 68      ** query, th
2b190 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69  en the caller wi
2b191 6c 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74 68  ll only allow th
2b192 65 20 6c 6f 6f 70 20 74 6f 20 72 75 6e 20 66 6f  e loop to run fo
2b193 72 0a 20 20 20 20 2a 2a 20 61 20 73 69 6e 67 6c  r.    ** a singl
2b194 65 20 69 74 65 72 61 74 69 6f 6e 2e 20 54 68 69  e iteration. Thi
2b195 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  s means that the
2b196 20 66 69 72 73 74 20 72 6f 77 20 72 65 74 75 72   first row retur
2b197 6e 65 64 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c  ned.    ** shoul
2b198 64 20 6e 6f 74 20 68 61 76 65 20 61 20 4e 55 4c  d not have a NUL
2b199 4c 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  L value stored i
2b19a 6e 20 27 78 27 2e 20 49 66 20 63 6f 6c 75 6d 6e  n 'x'. If column
2b19b 20 27 78 27 20 69 73 0a 20 20 20 20 2a 2a 20 74   'x' is.    ** t
2b19c 68 65 20 66 69 72 73 74 20 6f 6e 65 20 61 66 74  he first one aft
2b19d 65 72 20 74 68 65 20 6e 45 71 20 65 71 75 61 6c  er the nEq equal
2b19e 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ity constraints 
2b19f 69 6e 20 74 68 65 20 69 6e 64 65 78 2c 0a 20 20  in the index,.  
2b1a0 20 20 2a 2a 20 74 68 69 73 20 72 65 71 75 69 72    ** this requir
2b1a1 65 73 20 73 6f 6d 65 20 73 70 65 63 69 61 6c 20  es some special 
2b1a2 68 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 2a 2f  handling..    */
2b1a3 0a 20 20 20 20 69 66 28 20 28 77 63 74 72 6c 46  .    if( (wctrlF
2b1a4 6c 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45 52  lags&WHERE_ORDER
2b1a5 42 59 5f 4d 49 4e 29 21 3d 30 0a 20 20 20 20 20  BY_MIN)!=0.     
2b1a6 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  && (pLevel->plan
2b1a7 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 4f  .wsFlags&WHERE_O
2b1a8 52 44 45 52 42 59 29 0a 20 20 20 20 20 26 26 20  RDERBY).     && 
2b1a9 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 6e  (pIdx->nColumn>n
2b1aa 45 71 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  Eq).    ){.     
2b1ab 20 2f 2a 20 61 73 73 65 72 74 28 20 70 4f 72 64   /* assert( pOrd
2b1ac 65 72 42 79 2d 3e 6e 45 78 70 72 3d 3d 31 20 29  erBy->nExpr==1 )
2b1ad 3b 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 61 73  ; */.      /* as
2b1ae 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e  sert( pOrderBy->
2b1af 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 69 43 6f 6c  a[0].pExpr->iCol
2b1b0 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c  umn==pIdx->aiCol
2b1b1 75 6d 6e 5b 6e 45 71 5d 20 29 3b 20 2a 2f 0a 20  umn[nEq] ); */. 
2b1b2 20 20 20 20 20 69 73 4d 69 6e 51 75 65 72 79 20       isMinQuery 
2b1b3 3d 20 31 3b 0a 20 20 20 20 20 20 6e 45 78 74 72  = 1;.      nExtr
2b1b4 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  aReg = 1;.    }.
2b1b5 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 6e 79  .    /* Find any
2b1b6 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73   inequality cons
2b1b7 74 72 61 69 6e 74 20 74 65 72 6d 73 20 66 6f 72  traint terms for
2b1b8 20 74 68 65 20 73 74 61 72 74 20 61 6e 64 20 65   the start and e
2b1b9 6e 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  nd .    ** of th
2b1ba 65 20 72 61 6e 67 65 2e 20 0a 20 20 20 20 2a 2f  e range. .    */
2b1bb 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
2b1bc 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2b1bd 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20  WHERE_TOP_LIMIT 
2b1be 29 7b 0a 20 20 20 20 20 20 70 52 61 6e 67 65 45  ){.      pRangeE
2b1bf 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  nd = findTerm(pW
2b1c0 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52  C, iCur, k, notR
2b1c1 65 61 64 79 2c 20 28 57 4f 5f 4c 54 7c 57 4f 5f  eady, (WO_LT|WO_
2b1c2 4c 45 29 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  LE), pIdx);.    
2b1c3 20 20 6e 45 78 74 72 61 52 65 67 20 3d 20 31 3b    nExtraReg = 1;
2b1c4 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
2b1c5 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2b1c6 61 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d 5f  ags & WHERE_BTM_
2b1c7 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 20 20 70  LIMIT ){.      p
2b1c8 52 61 6e 67 65 53 74 61 72 74 20 3d 20 66 69 6e  RangeStart = fin
2b1c9 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
2b1ca 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20 28 57   k, notReady, (W
2b1cb 4f 5f 47 54 7c 57 4f 5f 47 45 29 2c 20 70 49 64  O_GT|WO_GE), pId
2b1cc 78 29 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61  x);.      nExtra
2b1cd 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a  Reg = 1;.    }..
2b1ce 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
2b1cf 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65  code to evaluate
2b1d0 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 20   all constraint 
2b1d1 74 65 72 6d 73 20 75 73 69 6e 67 20 3d 3d 20 6f  terms using == o
2b1d2 72 20 49 4e 0a 20 20 20 20 2a 2a 20 61 6e 64 20  r IN.    ** and 
2b1d3 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 73  store the values
2b1d4 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73 20   of those terms 
2b1d5 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72  in an array of r
2b1d6 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a 20  egisters.    ** 
2b1d7 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 42  starting at regB
2b1d8 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
2b1d9 72 65 67 42 61 73 65 20 3d 20 63 6f 64 65 41 6c  regBase = codeAl
2b1da 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 70  lEqualityTerms(p
2b1db 50 61 72 73 65 2c 20 70 4c 65 76 65 6c 2c 20 70  Parse, pLevel, p
2b1dc 57 43 2c 20 6e 6f 74 52 65 61 64 79 2c 20 6e 45  WC, notReady, nE
2b1dd 78 74 72 61 52 65 67 29 3b 0a 20 20 20 20 61 64  xtraReg);.    ad
2b1de 64 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e  drNxt = pLevel->
2b1df 61 64 64 72 4e 78 74 3b 0a 0a 0a 20 20 20 20 2f  addrNxt;...    /
2b1e0 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e  * If we are doin
2b1e1 67 20 61 20 72 65 76 65 72 73 65 20 6f 72 64 65  g a reverse orde
2b1e2 72 20 73 63 61 6e 20 6f 6e 20 61 6e 20 61 73 63  r scan on an asc
2b1e3 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20 6f 72  ending index, or
2b1e4 0a 20 20 20 20 2a 2a 20 61 20 66 6f 72 77 61 72  .    ** a forwar
2b1e5 64 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e 20  d order scan on 
2b1e6 61 20 64 65 73 63 65 6e 64 69 6e 67 20 69 6e 64  a descending ind
2b1e7 65 78 2c 20 69 6e 74 65 72 63 68 61 6e 67 65 20  ex, interchange 
2b1e8 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 74 61 72  the .    ** star
2b1e9 74 20 61 6e 64 20 65 6e 64 20 74 65 72 6d 73 20  t and end terms 
2b1ea 28 70 52 61 6e 67 65 53 74 61 72 74 20 61 6e 64  (pRangeStart and
2b1eb 20 70 52 61 6e 67 65 45 6e 64 29 2e 0a 20 20 20   pRangeEnd)..   
2b1ec 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 52 65 76   */.    if( bRev
2b1ed 3d 3d 28 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72  ==(pIdx->aSortOr
2b1ee 64 65 72 5b 6e 45 71 5d 3d 3d 53 51 4c 49 54 45  der[nEq]==SQLITE
2b1ef 5f 53 4f 5f 41 53 43 29 20 29 7b 0a 20 20 20 20  _SO_ASC) ){.    
2b1f0 20 20 53 57 41 50 28 57 68 65 72 65 54 65 72 6d    SWAP(WhereTerm
2b1f1 20 2a 2c 20 70 52 61 6e 67 65 45 6e 64 2c 20 70   *, pRangeEnd, p
2b1f2 52 61 6e 67 65 53 74 61 72 74 29 3b 0a 20 20 20  RangeStart);.   
2b1f3 20 7d 0a 0a 20 20 20 20 74 65 73 74 63 61 73 65   }..    testcase
2b1f4 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26  ( pRangeStart &&
2b1f5 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f   pRangeStart->eO
2b1f6 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20  perator & WO_LE 
2b1f7 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2b1f8 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26 20   pRangeStart && 
2b1f9 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70  pRangeStart->eOp
2b1fa 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 20 29  erator & WO_GE )
2b1fb 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2b1fc 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52 61  pRangeEnd && pRa
2b1fd 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f  ngeEnd->eOperato
2b1fe 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a 20 20 20  r & WO_LE );.   
2b1ff 20 74 65 73 74 63 61 73 65 28 20 70 52 61 6e 67   testcase( pRang
2b200 65 45 6e 64 20 26 26 20 70 52 61 6e 67 65 45 6e  eEnd && pRangeEn
2b201 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57  d->eOperator & W
2b202 4f 5f 47 45 20 29 3b 0a 20 20 20 20 73 74 61 72  O_GE );.    star
2b203 74 45 71 20 3d 20 21 70 52 61 6e 67 65 53 74 61  tEq = !pRangeSta
2b204 72 74 20 7c 7c 20 70 52 61 6e 67 65 53 74 61 72  rt || pRangeStar
2b205 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  t->eOperator & (
2b206 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a 20 20  WO_LE|WO_GE);.  
2b207 20 20 65 6e 64 45 71 20 3d 20 20 20 21 70 52 61    endEq =   !pRa
2b208 6e 67 65 45 6e 64 20 7c 7c 20 70 52 61 6e 67 65  ngeEnd || pRange
2b209 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  End->eOperator &
2b20a 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a   (WO_LE|WO_GE);.
2b20b 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72      start_constr
2b20c 61 69 6e 74 73 20 3d 20 70 52 61 6e 67 65 53 74  aints = pRangeSt
2b20d 61 72 74 20 7c 7c 20 6e 45 71 3e 30 3b 0a 0a 20  art || nEq>0;.. 
2b20e 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 69     /* Seek the i
2b20f 6e 64 65 78 20 63 75 72 73 6f 72 20 74 6f 20 74  ndex cursor to t
2b210 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
2b211 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6e 43  range. */.    nC
2b212 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b  onstraint = nEq;
2b213 0a 20 20 20 20 69 66 28 20 70 52 61 6e 67 65 53  .    if( pRangeS
2b214 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 69 6e  tart ){.      in
2b215 74 20 64 63 63 20 3d 20 70 50 61 72 73 65 2d 3e  t dcc = pParse->
2b216 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3b  disableColCache;
2b217 0a 20 20 20 20 20 20 69 66 28 20 70 52 61 6e 67  .      if( pRang
2b218 65 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 20 20  eEnd ){.        
2b219 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
2b21a 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20  olCache++;.     
2b21b 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2b21c 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2b21d 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 70 45   pRangeStart->pE
2b21e 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 72 65 67  xpr->pRight, reg
2b21f 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20  Base+nEq);.     
2b220 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
2b221 43 6f 6c 43 61 63 68 65 20 3d 20 64 63 63 3b 0a  ColCache = dcc;.
2b222 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b223 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73  eAddOp2(v, OP_Is
2b224 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6e 45  Null, regBase+nE
2b225 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20 20 20  q, addrNxt);.   
2b226 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b     nConstraint++
2b227 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
2b228 69 73 4d 69 6e 51 75 65 72 79 20 29 7b 0a 20 20  isMinQuery ){.  
2b229 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b22a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
2b22b 2c 20 30 2c 20 72 65 67 42 61 73 65 2b 6e 45 71  , 0, regBase+nEq
2b22c 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74 72  );.      nConstr
2b22d 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 73 74  aint++;.      st
2b22e 61 72 74 45 71 20 3d 20 30 3b 0a 20 20 20 20 20  artEq = 0;.     
2b22f 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e   start_constrain
2b230 74 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ts = 1;.    }.  
2b231 20 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e    codeApplyAffin
2b232 69 74 79 28 70 50 61 72 73 65 2c 20 72 65 67 42  ity(pParse, regB
2b233 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74  ase, nConstraint
2b234 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 6f 70 20  , pIdx);.    op 
2b235 3d 20 61 53 74 61 72 74 4f 70 5b 28 73 74 61 72  = aStartOp[(star
2b236 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3c 3c 32  t_constraints<<2
2b237 29 20 2b 20 28 73 74 61 72 74 45 71 3c 3c 31 29  ) + (startEq<<1)
2b238 20 2b 20 62 52 65 76 5d 3b 0a 20 20 20 20 61 73   + bRev];.    as
2b239 73 65 72 74 28 20 6f 70 21 3d 30 20 29 3b 0a 20  sert( op!=0 );. 
2b23a 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2b23b 3d 4f 50 5f 52 65 77 69 6e 64 20 29 3b 0a 20 20  =OP_Rewind );.  
2b23c 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2b23d 4f 50 5f 4c 61 73 74 20 29 3b 0a 20 20 20 20 74  OP_Last );.    t
2b23e 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
2b23f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 74 65  SeekGt );.    te
2b240 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53  stcase( op==OP_S
2b241 65 65 6b 47 65 20 29 3b 0a 20 20 20 20 74 65 73  eekGe );.    tes
2b242 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65  tcase( op==OP_Se
2b243 65 6b 4c 65 20 29 3b 0a 20 20 20 20 74 65 73 74  ekLe );.    test
2b244 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65 65  case( op==OP_See
2b245 6b 4c 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  kLt );.    sqlit
2b246 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2b247 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 61 64 64  op, iIdxCur, add
2b248 72 4e 78 74 2c 20 72 65 67 42 61 73 65 2c 20 0a  rNxt, regBase, .
2b249 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b24a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 54        SQLITE_INT
2b24b 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73 74 72 61  _TO_PTR(nConstra
2b24c 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b  int), P4_INT32);
2b24d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68  ..    /* Load th
2b24e 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  e value for the 
2b24f 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  inequality const
2b250 72 61 69 6e 74 20 61 74 20 74 68 65 20 65 6e 64  raint at the end
2b251 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72   of the.    ** r
2b252 61 6e 67 65 20 28 69 66 20 61 6e 79 29 2e 0a 20  ange (if any).. 
2b253 20 20 20 2a 2f 0a 20 20 20 20 6e 43 6f 6e 73 74     */.    nConst
2b254 72 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20 20 20  raint = nEq;.   
2b255 20 69 66 28 20 70 52 61 6e 67 65 45 6e 64 20 29   if( pRangeEnd )
2b256 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2b257 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
2b258 70 52 61 6e 67 65 45 6e 64 2d 3e 70 45 78 70 72  pRangeEnd->pExpr
2b259 2d 3e 70 52 69 67 68 74 2c 20 72 65 67 42 61 73  ->pRight, regBas
2b25a 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 73 71  e+nEq);.      sq
2b25b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b25c 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65  v, OP_IsNull, re
2b25d 67 42 61 73 65 2b 6e 45 71 2c 20 61 64 64 72 4e  gBase+nEq, addrN
2b25e 78 74 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 41  xt);.      codeA
2b25f 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61  pplyAffinity(pPa
2b260 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 45  rse, regBase, nE
2b261 71 2b 31 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  q+1, pIdx);.    
2b262 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b    nConstraint++;
2b263 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
2b264 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 62  op of the loop b
2b265 6f 64 79 20 2a 2f 0a 20 20 20 20 70 4c 65 76 65  ody */.    pLeve
2b266 6c 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 33 56  l->p2 = sqlite3V
2b267 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2b268 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  );..    /* Check
2b269 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 63 75   if the index cu
2b26a 72 73 6f 72 20 69 73 20 70 61 73 74 20 74 68 65  rsor is past the
2b26b 20 65 6e 64 20 6f 66 20 74 68 65 20 72 61 6e 67   end of the rang
2b26c 65 2e 20 2a 2f 0a 20 20 20 20 6f 70 20 3d 20 61  e. */.    op = a
2b26d 45 6e 64 4f 70 5b 28 70 52 61 6e 67 65 45 6e 64  EndOp[(pRangeEnd
2b26e 20 7c 7c 20 6e 45 71 29 20 2a 20 28 31 20 2b 20   || nEq) * (1 + 
2b26f 62 52 65 76 29 5d 3b 0a 20 20 20 20 74 65 73 74  bRev)];.    test
2b270 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4e 6f 6f  case( op==OP_Noo
2b271 70 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  p );.    testcas
2b272 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 47 45 20  e( op==OP_IdxGE 
2b273 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2b274 20 6f 70 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 3b   op==OP_IdxLT );
2b275 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 4f 50 5f  .    if( op!=OP_
2b276 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73 71  Noop ){.      sq
2b277 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2b278 76 2c 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20  v, op, iIdxCur, 
2b279 61 64 64 72 4e 78 74 2c 20 72 65 67 42 61 73 65  addrNxt, regBase
2b27a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2b27b 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2b27c 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e  _INT_TO_PTR(nCon
2b27d 73 74 72 61 69 6e 74 29 2c 20 50 34 5f 49 4e 54  straint), P4_INT
2b27e 33 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  32);.      sqlit
2b27f 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
2b280 2c 20 65 6e 64 45 71 21 3d 62 52 65 76 20 3f 31  , endEq!=bRev ?1
2b281 3a 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  :0);.    }..    
2b282 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
2b283 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  inequality const
2b284 72 61 69 6e 74 73 2c 20 63 68 65 63 6b 20 74 68  raints, check th
2b285 61 74 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20  at the value.   
2b286 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65   ** of the table
2b287 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65   column that the
2b288 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 74   inequality cont
2b289 72 61 69 6e 73 20 69 73 20 6e 6f 74 20 4e 55 4c  rains is not NUL
2b28a 4c 2e 0a 20 20 20 20 2a 2a 20 49 66 20 69 74 20  L..    ** If it 
2b28b 69 73 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20  is, jump to the 
2b28c 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  next iteration o
2b28d 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 20 20  f the loop..    
2b28e 2a 2f 0a 20 20 20 20 72 31 20 3d 20 73 71 6c 69  */.    r1 = sqli
2b28f 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2b290 61 72 73 65 29 3b 0a 20 20 20 20 74 65 73 74 63  arse);.    testc
2b291 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  ase( pLevel->pla
2b292 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2b293 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 3b 0a 20  E_BTM_LIMIT );. 
2b294 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65     testcase( pLe
2b295 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2b296 73 20 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49  s & WHERE_TOP_LI
2b297 4d 49 54 20 29 3b 0a 20 20 20 20 69 66 28 20 70  MIT );.    if( p
2b298 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2b299 61 67 73 20 26 20 28 57 48 45 52 45 5f 42 54 4d  ags & (WHERE_BTM
2b29a 5f 4c 49 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50  _LIMIT|WHERE_TOP
2b29b 5f 4c 49 4d 49 54 29 20 29 7b 0a 20 20 20 20 20  _LIMIT) ){.     
2b29c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b29d 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
2b29e 20 69 49 64 78 43 75 72 2c 20 6e 45 71 2c 20 72   iIdxCur, nEq, r
2b29f 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2b2a0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b2a1 50 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c 20 61 64  P_IsNull, r1, ad
2b2a2 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 7d 0a 0a  drCont);.    }..
2b2a3 20 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20      /* Seek the 
2b2a4 74 61 62 6c 65 20 63 75 72 73 6f 72 2c 20 69 66  table cursor, if
2b2a5 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20   required */.   
2b2a6 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
2b2a7 76 65 6c 2c 20 70 52 61 6e 67 65 53 74 61 72 74  vel, pRangeStart
2b2a8 29 3b 0a 20 20 20 20 64 69 73 61 62 6c 65 54 65  );.    disableTe
2b2a9 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67  rm(pLevel, pRang
2b2aa 65 45 6e 64 29 3b 0a 20 20 20 20 63 6f 64 65 52  eEnd);.    codeR
2b2ab 6f 77 53 65 74 45 61 72 6c 79 20 3d 20 72 65 67  owSetEarly = reg
2b2ac 52 6f 77 53 65 74 3e 3d 30 20 3f 20 77 68 65 72  RowSet>=0 ? wher
2b2ad 65 52 6f 77 52 65 61 64 79 46 6f 72 4f 75 74 70  eRowReadyForOutp
2b2ae 75 74 28 70 57 43 29 20 3a 20 30 3b 0a 20 20 20  ut(pWC) : 0;.   
2b2af 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65 20   if( !omitTable 
2b2b0 7c 7c 20 63 6f 64 65 52 6f 77 53 65 74 45 61 72  || codeRowSetEar
2b2b1 6c 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ly ){.      sqli
2b2b2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b2b3 20 4f 50 5f 49 64 78 52 6f 77 69 64 2c 20 69 49   OP_IdxRowid, iI
2b2b4 64 78 43 75 72 2c 20 72 31 29 3b 0a 20 20 20 20  dxCur, r1);.    
2b2b5 20 20 69 66 28 20 63 6f 64 65 52 6f 77 53 65 74    if( codeRowSet
2b2b6 45 61 72 6c 79 20 29 7b 0a 20 20 20 20 20 20 20  Early ){.       
2b2b7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b2b8 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41  p2(v, OP_RowSetA
2b2b9 64 64 2c 20 72 65 67 52 6f 77 53 65 74 2c 20 72  dd, regRowSet, r
2b2ba 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  1);.      }else{
2b2bb 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b2bc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b2bd 5f 53 65 65 6b 2c 20 69 43 75 72 2c 20 72 31 29  _Seek, iCur, r1)
2b2be 3b 20 20 2f 2a 20 44 65 66 65 72 72 65 64 20 73  ;  /* Deferred s
2b2bf 65 65 6b 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  eek */.      }. 
2b2c0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2b2c1 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2b2c2 50 61 72 73 65 2c 20 72 31 29 3b 0a 0a 20 20 20  Parse, r1);..   
2b2c3 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20 69   /* Record the i
2b2c4 6e 73 74 72 75 63 74 69 6f 6e 20 75 73 65 64 20  nstruction used 
2b2c5 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65  to terminate the
2b2c6 20 6c 6f 6f 70 2e 20 44 69 73 61 62 6c 65 20 0a   loop. Disable .
2b2c7 20 20 20 20 2a 2a 20 57 48 45 52 45 20 63 6c 61      ** WHERE cla
2b2c8 75 73 65 20 74 65 72 6d 73 20 6d 61 64 65 20 72  use terms made r
2b2c9 65 64 75 6e 64 61 6e 74 20 62 79 20 74 68 65 20  edundant by the 
2b2ca 69 6e 64 65 78 20 72 61 6e 67 65 20 73 63 61 6e  index range scan
2b2cb 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c 65  ..    */.    pLe
2b2cc 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76 20 3f  vel->op = bRev ?
2b2cd 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f 4e 65   OP_Prev : OP_Ne
2b2ce 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  xt;.    pLevel->
2b2cf 70 31 20 3d 20 69 49 64 78 43 75 72 3b 0a 20 20  p1 = iIdxCur;.  
2b2d0 7d 65 6c 73 65 0a 0a 23 69 66 6e 64 65 66 20 53  }else..#ifndef S
2b2d1 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
2b2d2 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28  TIMIZATION.  if(
2b2d3 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2b2d4 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4d 55  Flags & WHERE_MU
2b2d5 4c 54 49 5f 4f 52 20 29 7b 0a 20 20 20 20 2f 2a  LTI_OR ){.    /*
2b2d6 20 43 61 73 65 20 34 3a 20 20 54 77 6f 20 6f 72   Case 4:  Two or
2b2d7 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 6c 79   more separately
2b2d8 20 69 6e 64 65 78 65 64 20 74 65 72 6d 73 20 63   indexed terms c
2b2d9 6f 6e 6e 65 63 74 65 64 20 62 79 20 4f 52 0a 20  onnected by OR. 
2b2da 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 61     **.    ** Exa
2b2db 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  mple:.    **.   
2b2dc 20 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42   **   CREATE TAB
2b2dd 4c 45 20 74 31 28 61 2c 62 2c 63 2c 64 29 3b 0a  LE t1(a,b,c,d);.
2b2de 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20      **   CREATE 
2b2df 49 4e 44 45 58 20 69 31 20 4f 4e 20 74 31 28 61  INDEX i1 ON t1(a
2b2e0 29 3b 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41  );.    **   CREA
2b2e1 54 45 20 49 4e 44 45 58 20 69 32 20 4f 4e 20 74  TE INDEX i2 ON t
2b2e2 31 28 62 29 3b 0a 20 20 20 20 2a 2a 20 20 20 43  1(b);.    **   C
2b2e3 52 45 41 54 45 20 49 4e 44 45 58 20 69 33 20 4f  REATE INDEX i3 O
2b2e4 4e 20 74 31 28 63 29 3b 0a 20 20 20 20 2a 2a 0a  N t1(c);.    **.
2b2e5 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20      **   SELECT 
2b2e6 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  * FROM t1 WHERE 
2b2e7 61 3d 35 20 4f 52 20 62 3d 37 20 4f 52 20 28 63  a=5 OR b=7 OR (c
2b2e8 3d 31 31 20 41 4e 44 20 64 3d 31 33 29 0a 20 20  =11 AND d=13).  
2b2e9 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 74    **.    ** In t
2b2ea 68 65 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 72  he example, ther
2b2eb 65 20 61 72 65 20 74 68 72 65 65 20 69 6e 64 65  e are three inde
2b2ec 78 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63  xed terms connec
2b2ed 74 65 64 20 62 79 20 4f 52 2e 0a 20 20 20 20 2a  ted by OR..    *
2b2ee 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65  * The top of the
2b2ef 20 6c 6f 6f 70 20 69 73 20 63 6f 6e 73 74 72 75   loop is constru
2b2f0 63 74 65 64 20 62 79 20 63 72 65 61 74 69 6e 67  cted by creating
2b2f1 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74   a RowSet object
2b2f2 0a 20 20 20 20 2a 2a 20 61 6e 64 20 70 6f 70 75  .    ** and popu
2b2f3 6c 61 74 69 6e 67 20 69 74 2e 20 20 54 68 65 6e  lating it.  Then
2b2f4 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 6c   looping over el
2b2f5 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 72 6f  ements of the ro
2b2f6 77 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  wset..    **.   
2b2f7 20 2a 2a 20 20 20 20 20 20 20 20 4e 75 6c 6c 20   **        Null 
2b2f8 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  1.    **        
2b2f9 23 20 66 69 6c 6c 20 52 6f 77 53 65 74 20 31 20  # fill RowSet 1 
2b2fa 77 69 74 68 20 65 6e 74 72 69 65 73 20 77 68 65  with entries whe
2b2fb 72 65 20 61 3d 35 20 75 73 69 6e 67 20 69 31 0a  re a=5 using i1.
2b2fc 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 23 20      **        # 
2b2fd 66 69 6c 6c 20 52 6f 77 73 65 74 20 31 20 77 69  fill Rowset 1 wi
2b2fe 74 68 20 65 6e 74 72 69 65 73 20 77 68 65 72 65  th entries where
2b2ff 20 62 3d 37 20 75 73 69 6e 67 20 69 32 0a 20 20   b=7 using i2.  
2b300 20 20 2a 2a 20 20 20 20 20 20 20 20 23 20 66 69    **        # fi
2b301 6c 6c 20 52 6f 77 73 65 74 20 31 20 77 69 74 68  ll Rowset 1 with
2b302 20 65 6e 74 72 69 65 73 20 77 68 65 72 65 20 63   entries where c
2b303 3d 31 31 20 61 6e 64 20 64 3d 31 33 20 69 33 20  =11 and d=13 i3 
2b304 61 6e 64 20 74 31 0a 20 20 20 20 2a 2a 20 20 20  and t1.    **   
2b305 20 20 41 3a 20 52 6f 77 53 65 74 52 65 61 64 20    A: RowSetRead 
2b306 31 2c 20 42 2c 20 32 0a 20 20 20 20 2a 2a 20 20  1, B, 2.    **  
2b307 20 20 20 20 20 20 53 65 65 6b 20 20 20 20 20 20        Seek      
2b308 20 69 2c 20 32 0a 20 20 20 20 2a 2a 0a 20 20 20   i, 2.    **.   
2b309 20 2a 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f   ** The bottom o
2b30a 66 20 74 68 65 20 6c 6f 6f 70 20 6c 6f 6f 6b 73  f the loop looks
2b30b 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 20 20   like this:.    
2b30c 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
2b30d 20 47 6f 74 6f 20 20 20 20 20 20 20 30 2c 20 41   Goto       0, A
2b30e 0a 20 20 20 20 2a 2a 20 20 20 20 20 42 3a 0a 20  .    **     B:. 
2b30f 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65     */.    int re
2b310 67 4f 72 52 6f 77 73 65 74 3b 20 20 20 20 20 20  gOrRowset;      
2b311 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
2b312 64 69 6e 67 20 74 68 65 20 52 6f 77 53 65 74 20  ding the RowSet 
2b313 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 69 6e  object */.    in
2b314 74 20 72 65 67 4e 65 78 74 52 6f 77 69 64 3b 20  t regNextRowid; 
2b315 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
2b316 20 68 6f 6c 64 69 6e 67 20 6e 65 78 74 20 72 6f   holding next ro
2b317 77 69 64 20 2a 2f 0a 20 20 20 20 57 68 65 72 65  wid */.    Where
2b318 43 6c 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20  Clause *pOrWc;  
2b319 20 20 2f 2a 20 54 68 65 20 4f 52 2d 63 6c 61 75    /* The OR-clau
2b31a 73 65 20 62 72 6f 6b 65 6e 20 6f 75 74 20 69 6e  se broken out in
2b31b 74 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20  to subterms */. 
2b31c 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f     WhereTerm *pO
2b31d 72 54 65 72 6d 3b 20 20 20 20 2f 2a 20 41 20 73  rTerm;    /* A s
2b31e 69 6e 67 6c 65 20 73 75 62 74 65 72 6d 20 77 69  ingle subterm wi
2b31f 74 68 69 6e 20 74 68 65 20 4f 52 2d 63 6c 61 75  thin the OR-clau
2b320 73 65 20 2a 2f 0a 20 20 20 20 53 72 63 4c 69 73  se */.    SrcLis
2b321 74 20 6f 6e 65 54 61 62 3b 20 20 20 20 20 20 20  t oneTab;       
2b322 20 2f 2a 20 53 68 6f 72 74 65 6e 65 64 20 74 61   /* Shortened ta
2b323 62 6c 65 20 6c 69 73 74 20 2a 2f 0a 20 20 20 0a  ble list */.   .
2b324 20 20 20 20 70 54 65 72 6d 20 3d 20 70 4c 65 76      pTerm = pLev
2b325 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 54 65 72 6d  el->plan.u.pTerm
2b326 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
2b327 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  erm!=0 );.    as
2b328 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 65 4f 70  sert( pTerm->eOp
2b329 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 29 3b  erator==WO_OR );
2b32a 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54  .    assert( (pT
2b32b 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
2b32c 45 52 4d 5f 4f 52 49 4e 46 4f 29 21 3d 30 20 29  ERM_ORINFO)!=0 )
2b32d 3b 0a 20 20 20 20 70 4f 72 57 63 20 3d 20 26 70  ;.    pOrWc = &p
2b32e 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d  Term->u.pOrInfo-
2b32f 3e 77 63 3b 0a 20 20 20 20 63 6f 64 65 52 6f 77  >wc;.    codeRow
2b330 53 65 74 45 61 72 6c 79 20 3d 20 28 72 65 67 52  SetEarly = (regR
2b331 6f 77 53 65 74 3e 3d 30 20 26 26 20 70 57 43 2d  owSet>=0 && pWC-
2b332 3e 6e 54 65 72 6d 3d 3d 31 29 20 3f 31 3a 30 3b  >nTerm==1) ?1:0;
2b333 0a 0a 20 20 20 20 69 66 28 20 63 6f 64 65 52 6f  ..    if( codeRo
2b334 77 53 65 74 45 61 72 6c 79 20 29 7b 0a 20 20 20  wSetEarly ){.   
2b335 20 20 20 72 65 67 4f 72 52 6f 77 73 65 74 20 3d     regOrRowset =
2b336 20 72 65 67 52 6f 77 53 65 74 3b 0a 20 20 20 20   regRowSet;.    
2b337 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 67  }else{.      reg
2b338 4f 72 52 6f 77 73 65 74 20 3d 20 73 71 6c 69 74  OrRowset = sqlit
2b339 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2b33a 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rse);.      sqli
2b33b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b33c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
2b33d 4f 72 52 6f 77 73 65 74 29 3b 0a 20 20 20 20 7d  OrRowset);.    }
2b33e 0a 20 20 20 20 6f 6e 65 54 61 62 2e 6e 53 72 63  .    oneTab.nSrc
2b33f 20 3d 20 31 3b 0a 20 20 20 20 6f 6e 65 54 61 62   = 1;.    oneTab
2b340 2e 6e 41 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 20  .nAlloc = 1;.   
2b341 20 6f 6e 65 54 61 62 2e 61 5b 30 5d 20 3d 20 2a   oneTab.a[0] = *
2b342 70 54 61 62 49 74 65 6d 3b 0a 20 20 20 20 66 6f  pTabItem;.    fo
2b343 72 28 6a 3d 30 2c 20 70 4f 72 54 65 72 6d 3d 70  r(j=0, pOrTerm=p
2b344 4f 72 57 63 2d 3e 61 3b 20 6a 3c 70 4f 72 57 63  OrWc->a; j<pOrWc
2b345 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70 4f  ->nTerm; j++, pO
2b346 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  rTerm++){.      
2b347 57 68 65 72 65 49 6e 66 6f 20 2a 70 53 75 62 57  WhereInfo *pSubW
2b348 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20  Info;.      if( 
2b349 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2b34a 73 6f 72 21 3d 69 43 75 72 20 26 26 20 70 4f 72  sor!=iCur && pOr
2b34b 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21  Term->eOperator!
2b34c 3d 57 4f 5f 41 4e 44 20 29 20 63 6f 6e 74 69 6e  =WO_AND ) contin
2b34d 75 65 3b 0a 20 20 20 20 20 20 70 53 75 62 57 49  ue;.      pSubWI
2b34e 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65  nfo = sqlite3Whe
2b34f 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20  reBegin(pParse, 
2b350 26 6f 6e 65 54 61 62 2c 20 70 4f 72 54 65 72 6d  &oneTab, pOrTerm
2b351 2d 3e 70 45 78 70 72 2c 20 30 2c 0a 20 20 20 20  ->pExpr, 0,.    
2b352 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b353 20 20 20 20 57 48 45 52 45 5f 46 49 4c 4c 5f 52      WHERE_FILL_R
2b354 4f 57 53 45 54 20 7c 20 57 48 45 52 45 5f 4f 4d  OWSET | WHERE_OM
2b355 49 54 5f 4f 50 45 4e 20 7c 20 57 48 45 52 45 5f  IT_OPEN | WHERE_
2b356 4f 4d 49 54 5f 43 4c 4f 53 45 2c 0a 20 20 20 20  OMIT_CLOSE,.    
2b357 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b358 20 20 20 20 72 65 67 4f 72 52 6f 77 73 65 74 29      regOrRowset)
2b359 3b 0a 20 20 20 20 20 20 69 66 28 20 70 53 75 62  ;.      if( pSub
2b35a 57 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20  WInfo ){.       
2b35b 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
2b35c 28 70 53 75 62 57 49 6e 66 6f 29 3b 0a 20 20 20  (pSubWInfo);.   
2b35d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
2b35e 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2b35f 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 43 6f  eLabel(v, addrCo
2b360 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 21 63 6f  nt);.    if( !co
2b361 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20 29 7b  deRowSetEarly ){
2b362 0a 20 20 20 20 20 20 72 65 67 4e 65 78 74 52 6f  .      regNextRo
2b363 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74  wid = sqlite3Get
2b364 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2b365 0a 20 20 20 20 20 20 61 64 64 72 43 6f 6e 74 20  .      addrCont 
2b366 3d 20 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  = .         sqli
2b367 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2b368 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20   OP_RowSetRead, 
2b369 72 65 67 4f 72 52 6f 77 73 65 74 2c 61 64 64 72  regOrRowset,addr
2b36a 42 72 6b 2c 72 65 67 4e 65 78 74 52 6f 77 69 64  Brk,regNextRowid
2b36b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b36c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b36d 5f 53 65 65 6b 2c 20 69 43 75 72 2c 20 72 65 67  _Seek, iCur, reg
2b36e 4e 65 78 74 52 6f 77 69 64 29 3b 0a 20 20 20 20  NextRowid);.    
2b36f 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2b370 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2b371 72 65 67 4e 65 78 74 52 6f 77 69 64 29 3b 0a 20  regNextRowid);. 
2b372 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 52       /* sqlite3R
2b373 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2b374 61 72 73 65 2c 20 72 65 67 4f 72 52 6f 77 73 65  arse, regOrRowse
2b375 74 29 3b 20 2f 2f 20 50 72 65 73 65 72 76 65 20  t); // Preserve 
2b376 74 68 65 20 52 6f 77 53 65 74 20 2a 2f 0a 20 20  the RowSet */.  
2b377 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d      pLevel->op =
2b378 20 4f 50 5f 47 6f 74 6f 3b 0a 20 20 20 20 20 20   OP_Goto;.      
2b379 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 61 64 64  pLevel->p2 = add
2b37a 72 43 6f 6e 74 3b 0a 20 20 20 20 7d 65 6c 73 65  rCont;.    }else
2b37b 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  {.      pLevel->
2b37c 6f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20  op = OP_Noop;.  
2b37d 20 20 7d 0a 20 20 20 20 64 69 73 61 62 6c 65 54    }.    disableT
2b37e 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54 65 72  erm(pLevel, pTer
2b37f 6d 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  m);.  }else.#end
2b380 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2b381 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  T_OR_OPTIMIZATIO
2b382 4e 20 2a 2f 0a 0a 20 20 7b 0a 20 20 20 20 2f 2a  N */..  {.    /*
2b383 20 43 61 73 65 20 35 3a 20 20 54 68 65 72 65 20   Case 5:  There 
2b384 69 73 20 6e 6f 20 75 73 61 62 6c 65 20 69 6e 64  is no usable ind
2b385 65 78 2e 20 20 57 65 20 6d 75 73 74 20 64 6f 20  ex.  We must do 
2b386 61 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a  a complete.    *
2b387 2a 20 20 20 20 20 20 20 20 20 20 73 63 61 6e 20  *          scan 
2b388 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 61  of the entire ta
2b389 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ble..    */.    
2b38a 61 73 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c  assert( omitTabl
2b38b 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  e==0 );.    asse
2b38c 72 74 28 20 62 52 65 76 3d 3d 30 20 29 3b 0a 20  rt( bRev==0 );. 
2b38d 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
2b38e 4f 50 5f 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65  OP_Next;.    pLe
2b38f 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a  vel->p1 = iCur;.
2b390 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d      pLevel->p2 =
2b391 20 31 20 2b 20 73 71 6c 69 74 65 33 56 64 62 65   1 + sqlite3Vdbe
2b392 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
2b393 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64 72 42  ind, iCur, addrB
2b394 72 6b 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  rk);.    pLevel-
2b395 3e 70 35 20 3d 20 53 51 4c 49 54 45 5f 53 54 4d  >p5 = SQLITE_STM
2b396 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e  TSTATUS_FULLSCAN
2b397 5f 53 54 45 50 3b 0a 20 20 20 20 63 6f 64 65 52  _STEP;.    codeR
2b398 6f 77 53 65 74 45 61 72 6c 79 20 3d 20 30 3b 0a  owSetEarly = 0;.
2b399 20 20 7d 0a 20 20 6e 6f 74 52 65 61 64 79 20 26    }.  notReady &
2b39a 3d 20 7e 67 65 74 4d 61 73 6b 28 70 57 43 2d 3e  = ~getMask(pWC->
2b39b 70 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 29 3b  pMaskSet, iCur);
2b39c 0a 0a 20 20 2f 2a 20 49 6e 73 65 72 74 20 63 6f  ..  /* Insert co
2b39d 64 65 20 74 6f 20 74 65 73 74 20 65 76 65 72 79  de to test every
2b39e 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 74   subexpression t
2b39f 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c  hat can be compl
2b3a0 65 74 65 6c 79 0a 20 20 2a 2a 20 63 6f 6d 70 75  etely.  ** compu
2b3a1 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 63 75  ted using the cu
2b3a2 72 72 65 6e 74 20 73 65 74 20 6f 66 20 74 61 62  rrent set of tab
2b3a3 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20  les..  */.  k = 
2b3a4 30 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70  0;.  for(pTerm=p
2b3a5 57 43 2d 3e 61 2c 20 6a 3d 70 57 43 2d 3e 6e 54  WC->a, j=pWC->nT
2b3a6 65 72 6d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70  erm; j>0; j--, p
2b3a7 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70  Term++){.    Exp
2b3a8 72 20 2a 70 45 3b 0a 20 20 20 20 74 65 73 74 63  r *pE;.    testc
2b3a9 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c  ase( pTerm->wtFl
2b3aa 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55  ags & TERM_VIRTU
2b3ab 41 4c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  AL );.    testca
2b3ac 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61  se( pTerm->wtFla
2b3ad 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 20  gs & TERM_CODED 
2b3ae 29 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  );.    if( pTerm
2b3af 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52  ->wtFlags & (TER
2b3b0 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43  M_VIRTUAL|TERM_C
2b3b1 4f 44 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65  ODED) ) continue
2b3b2 3b 0a 20 20 20 20 69 66 28 20 28 70 54 65 72 6d  ;.    if( (pTerm
2b3b3 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f  ->prereqAll & no
2b3b4 74 52 65 61 64 79 29 21 3d 30 20 29 20 63 6f 6e  tReady)!=0 ) con
2b3b5 74 69 6e 75 65 3b 0a 20 20 20 20 70 45 20 3d 20  tinue;.    pE = 
2b3b6 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
2b3b7 20 20 61 73 73 65 72 74 28 20 70 45 21 3d 30 20    assert( pE!=0 
2b3b8 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  );.    if( pLeve
2b3b9 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 26 26 20  l->iLeftJoin && 
2b3ba 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2b3bb 28 70 45 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e  (pE, EP_FromJoin
2b3bc 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69  ) ){.      conti
2b3bd 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  nue;.    }.    p
2b3be 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f  Parse->disableCo
2b3bf 6c 43 61 63 68 65 20 2b 3d 20 6b 3b 0a 20 20 20  lCache += k;.   
2b3c0 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
2b3c1 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 2c 20  lse(pParse, pE, 
2b3c2 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49 54 45  addrCont, SQLITE
2b3c3 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2b3c4 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c    pParse->disabl
2b3c5 65 43 6f 6c 43 61 63 68 65 20 2d 3d 20 6b 3b 0a  eColCache -= k;.
2b3c6 20 20 20 20 6b 20 3d 20 31 3b 0a 20 20 20 20 70      k = 1;.    p
2b3c7 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d  Term->wtFlags |=
2b3c8 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 7d   TERM_CODED;.  }
2b3c9 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 4c 45 46  ..  /* For a LEF
2b3ca 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 67 65  T OUTER JOIN, ge
2b3cb 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
2b3cc 20 77 69 6c 6c 20 72 65 63 6f 72 64 20 74 68 65   will record the
2b3cd 20 66 61 63 74 20 74 68 61 74 0a 20 20 2a 2a 20   fact that.  ** 
2b3ce 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f 77  at least one row
2b3cf 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61   of the right ta
2b3d0 62 6c 65 20 68 61 73 20 6d 61 74 63 68 65 64 20  ble has matched 
2b3d1 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 2e 20  the left table. 
2b3d2 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 65   .  */.  if( pLe
2b3d3 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29  vel->iLeftJoin )
2b3d4 7b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64  {.    pLevel->ad
2b3d5 64 72 46 69 72 73 74 20 3d 20 73 71 6c 69 74 65  drFirst = sqlite
2b3d6 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2b3d7 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  (v);.    sqlite3
2b3d8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b3d9 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 70 4c 65  _Integer, 1, pLe
2b3da 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b  vel->iLeftJoin);
2b3db 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
2b3dc 28 28 76 2c 20 22 72 65 63 6f 72 64 20 4c 45 46  ((v, "record LEF
2b3dd 54 20 4a 4f 49 4e 20 68 69 74 22 29 29 3b 0a 20  T JOIN hit"));. 
2b3de 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c     sqlite3ExprCl
2b3df 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70  earColumnCache(p
2b3e0 50 61 72 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69  Parse, pLevel->i
2b3e1 54 61 62 43 75 72 29 3b 0a 20 20 20 20 73 71 6c  TabCur);.    sql
2b3e2 69 74 65 33 45 78 70 72 43 6c 65 61 72 43 6f 6c  ite3ExprClearCol
2b3e3 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c  umnCache(pParse,
2b3e4 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
2b3e5 29 3b 0a 20 20 20 20 66 6f 72 28 70 54 65 72 6d  );.    for(pTerm
2b3e6 3d 70 57 43 2d 3e 61 2c 20 6a 3d 30 3b 20 6a 3c  =pWC->a, j=0; j<
2b3e7 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c  pWC->nTerm; j++,
2b3e8 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20   pTerm++){.     
2b3e9 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
2b3ea 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
2b3eb 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20  _VIRTUAL );.    
2b3ec 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
2b3ed 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  m->wtFlags & TER
2b3ee 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 20  M_CODED );.     
2b3ef 20 69 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c   if( pTerm->wtFl
2b3f0 61 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54  ags & (TERM_VIRT
2b3f1 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20  UAL|TERM_CODED) 
2b3f2 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2b3f3 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72    if( (pTerm->pr
2b3f4 65 72 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61  ereqAll & notRea
2b3f5 64 79 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75  dy)!=0 ) continu
2b3f6 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
2b3f7 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 20 29 3b   pTerm->pExpr );
2b3f8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2b3f9 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
2b3fa 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20  , pTerm->pExpr, 
2b3fb 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49 54 45  addrCont, SQLITE
2b3fc 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2b3fd 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61      pTerm->wtFla
2b3fe 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44  gs |= TERM_CODED
2b3ff 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
2b400 2a 0a 20 20 2a 2a 20 49 66 20 69 74 20 77 61 73  *.  ** If it was
2b401 20 72 65 71 75 65 73 74 65 64 20 74 6f 20 73 74   requested to st
2b402 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ore the results 
2b403 69 6e 20 61 20 72 6f 77 73 65 74 20 61 6e 64 20  in a rowset and 
2b404 74 68 61 74 20 68 61 73 0a 20 20 2a 2a 20 6e 6f  that has.  ** no
2b405 74 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64  t already been d
2b406 6f 2c 20 74 68 65 6e 20 64 6f 20 73 6f 20 6e 6f  o, then do so no
2b407 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65  w..  */.  if( re
2b408 67 52 6f 77 53 65 74 3e 3d 30 20 26 26 20 21 63  gRowSet>=0 && !c
2b409 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20 29  odeRowSetEarly )
2b40a 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73  {.    int r1 = s
2b40b 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2b40c 28 70 50 61 72 73 65 29 3b 0a 23 69 66 6e 64 65  (pParse);.#ifnde
2b40d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2b40e 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69  RTUALTABLE.    i
2b40f 66 28 20 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  f(  (pLevel->pla
2b410 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2b411 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21  E_VIRTUALTABLE)!
2b412 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
2b413 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b414 20 4f 50 5f 56 52 6f 77 69 64 2c 20 69 43 75 72   OP_VRowid, iCur
2b415 2c 20 72 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65  , r1);.    }else
2b416 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20  .#endif.    {.  
2b417 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b418 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
2b419 64 2c 20 69 43 75 72 2c 20 72 31 29 3b 0a 20 20  d, iCur, r1);.  
2b41a 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2b41b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b41c 52 6f 77 53 65 74 41 64 64 2c 20 72 65 67 52 6f  RowSetAdd, regRo
2b41d 77 53 65 74 2c 20 72 31 29 3b 0a 20 20 20 20 73  wSet, r1);.    s
2b41e 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2b41f 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
2b420 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
2b421 6e 6f 74 52 65 61 64 79 3b 0a 7d 0a 0a 23 69 66  notReady;.}..#if
2b422 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
2b423 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20  TEST)./*.** The 
2b424 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62  following variab
2b425 6c 65 20 68 6f 6c 64 73 20 61 20 74 65 78 74 20  le holds a text 
2b426 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 71  description of q
2b427 75 65 72 79 20 70 6c 61 6e 20 67 65 6e 65 72 61  uery plan genera
2b428 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 6f  ted.** by the mo
2b429 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74  st recent call t
2b42a 6f 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  o sqlite3WhereBe
2b42b 67 69 6e 28 29 2e 20 20 45 61 63 68 20 63 61 6c  gin().  Each cal
2b42c 6c 20 74 6f 20 57 68 65 72 65 42 65 67 69 6e 0a  l to WhereBegin.
2b42d 2a 2a 20 6f 76 65 72 77 72 69 74 65 73 20 74 68  ** overwrites th
2b42e 65 20 70 72 65 76 69 6f 75 73 2e 20 20 54 68 69  e previous.  Thi
2b42f 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  s information is
2b430 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
2b431 67 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c 79 73 69  g and.** analysi
2b432 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  s only..*/.SQLIT
2b433 45 5f 41 50 49 20 63 68 61 72 20 73 71 6c 69 74  E_API char sqlit
2b434 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 42 4d  e3_query_plan[BM
2b435 53 2a 32 2a 34 30 5d 3b 20 20 2f 2a 20 54 65 78  S*2*40];  /* Tex
2b436 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f  t of the join */
2b437 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 51 50 6c  .static int nQPl
2b438 61 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  an = 0;         
2b439 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 72 65       /* Next fre
2b43a 65 20 73 6c 6f 77 20 69 6e 20 5f 71 75 65 72 79  e slow in _query
2b43b 5f 70 6c 61 6e 5b 5d 20 2a 2f 0a 0a 23 65 6e 64  _plan[] */..#end
2b43c 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53  if /* SQLITE_TES
2b43d 54 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  T */.../*.** Fre
2b43e 65 20 61 20 57 68 65 72 65 49 6e 66 6f 20 73 74  e a WhereInfo st
2b43f 72 75 63 74 75 72 65 0a 2a 2f 0a 73 74 61 74 69  ructure.*/.stati
2b440 63 20 76 6f 69 64 20 77 68 65 72 65 49 6e 66 6f  c void whereInfo
2b441 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Free(sqlite3 *db
2b442 2c 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49  , WhereInfo *pWI
2b443 6e 66 6f 29 7b 0a 20 20 69 66 28 20 70 57 49 6e  nfo){.  if( pWIn
2b444 66 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  fo ){.    int i;
2b445 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2b446 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 20  pWInfo->nLevel; 
2b447 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
2b448 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2b449 70 49 6e 66 6f 20 3d 20 70 57 49 6e 66 6f 2d 3e  pInfo = pWInfo->
2b44a 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20  a[i].pIdxInfo;. 
2b44b 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 20 29       if( pInfo )
2b44c 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
2b44d 28 20 70 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46  ( pInfo->needToF
2b44e 72 65 65 49 64 78 53 74 72 3d 3d 30 20 7c 7c 20  reeIdxStr==0 || 
2b44f 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2b450 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
2b451 70 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65  pInfo->needToFre
2b452 65 49 64 78 53 74 72 20 29 7b 0a 20 20 20 20 20  eIdxStr ){.     
2b453 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
2b454 65 28 70 49 6e 66 6f 2d 3e 69 64 78 53 74 72 29  e(pInfo->idxStr)
2b455 3b 0a 09 7d 0a 20 20 20 20 20 20 20 20 73 71 6c  ;..}.        sql
2b456 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2b457 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Info);.      }. 
2b458 20 20 20 7d 0a 20 20 20 20 77 68 65 72 65 43 6c     }.    whereCl
2b459 61 75 73 65 43 6c 65 61 72 28 70 57 49 6e 66 6f  auseClear(pWInfo
2b45a 2d 3e 70 57 43 29 3b 0a 20 20 20 20 73 71 6c 69  ->pWC);.    sqli
2b45b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 57  te3DbFree(db, pW
2b45c 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f  Info);.  }.}.../
2b45d 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68  *.** Generate th
2b45e 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
2b45f 68 65 20 6c 6f 6f 70 20 75 73 65 64 20 66 6f 72  he loop used for
2b460 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72   WHERE clause pr
2b461 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 20 54 68 65  ocessing..** The
2b462 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
2b463 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
2b464 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72   opaque structur
2b465 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a  e that contains.
2b466 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e  ** information n
2b467 65 65 64 65 64 20 74 6f 20 74 65 72 6d 69 6e 61  eeded to termina
2b468 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20 20 4c 61  te the loop.  La
2b469 74 65 72 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67  ter, the calling
2b46a 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 68 6f 75   routine.** shou
2b46b 6c 64 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ld invoke sqlite
2b46c 33 57 68 65 72 65 45 6e 64 28 29 20 77 69 74 68  3WhereEnd() with
2b46d 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
2b46e 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  e of this functi
2b46f 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74  on.** in order t
2b470 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 57  o complete the W
2b471 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63  HERE clause proc
2b472 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66  essing..**.** If
2b473 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
2b474 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  , this routine r
2b475 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  eturns NULL..**.
2b476 2a 2a 20 54 68 65 20 62 61 73 69 63 20 69 64 65  ** The basic ide
2b477 61 20 69 73 20 74 6f 20 64 6f 20 61 20 6e 65 73  a is to do a nes
2b478 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65 20 6c 6f  ted loop, one lo
2b479 6f 70 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c  op for each tabl
2b47a 65 20 69 6e 0a 2a 2a 20 74 68 65 20 46 52 4f 4d  e in.** the FROM
2b47b 20 63 6c 61 75 73 65 20 6f 66 20 61 20 73 65 6c   clause of a sel
2b47c 65 63 74 2e 20 20 28 49 4e 53 45 52 54 20 61 6e  ect.  (INSERT an
2b47d 64 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  d UPDATE stateme
2b47e 6e 74 73 20 61 72 65 20 74 68 65 0a 2a 2a 20 73  nts are the.** s
2b47f 61 6d 65 20 61 73 20 61 20 53 45 4c 45 43 54 20  ame as a SELECT 
2b480 77 69 74 68 20 6f 6e 6c 79 20 61 20 73 69 6e 67  with only a sing
2b481 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  le table in the 
2b482 46 52 4f 4d 20 63 6c 61 75 73 65 2e 29 20 20 46  FROM clause.)  F
2b483 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69  or.** example, i
2b484 66 20 74 68 65 20 53 51 4c 20 69 73 20 74 68 69  f the SQL is thi
2b485 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53  s:.**.**       S
2b486 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c  ELECT * FROM t1,
2b487 20 74 32 2c 20 74 33 20 57 48 45 52 45 20 2e 2e   t2, t3 WHERE ..
2b488 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74 68  .;.**.** Then th
2b489 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
2b48a 20 69 73 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79   is conceptually
2b48b 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77   like the follow
2b48c 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ing:.**.**      
2b48d 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e 20  foreach row1 in 
2b48e 74 31 20 64 6f 20 20 20 20 20 20 20 5c 20 20 20  t1 do       \   
2b48f 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a   Code generated.
2b490 2a 2a 20 20 20 20 20 20 20 20 66 6f 72 65 61 63  **        foreac
2b491 68 20 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 20  h row2 in t2 do 
2b492 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69       |-- by sqli
2b493 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 0a  te3WhereBegin().
2b494 2a 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 65  **          fore
2b495 61 63 68 20 72 6f 77 33 20 69 6e 20 74 33 20 64  ach row3 in t3 d
2b496 6f 20 20 20 2f 0a 2a 2a 20 20 20 20 20 20 20 20  o   /.**        
2b497 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20      ....**      
2b498 20 20 20 20 65 6e 64 20 20 20 20 20 20 20 20 20      end         
2b499 20 20 20 20 20 20 20 20 20 20 20 20 5c 20 20 20              \   
2b49a 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a   Code generated.
2b49b 2a 2a 20 20 20 20 20 20 20 20 65 6e 64 20 20 20  **        end   
2b49c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b49d 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69       |-- by sqli
2b49e 74 65 33 57 68 65 72 65 45 6e 64 28 29 0a 2a 2a  te3WhereEnd().**
2b49f 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20        end       
2b4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4a1 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74    /.**.** Note t
2b4a2 68 61 74 20 74 68 65 20 6c 6f 6f 70 73 20 6d 69  hat the loops mi
2b4a3 67 68 74 20 6e 6f 74 20 62 65 20 6e 65 73 74 65  ght not be neste
2b4a4 64 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 69  d in the order i
2b4a5 6e 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20  n which they.** 
2b4a6 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 46 52  appear in the FR
2b4a7 4f 4d 20 63 6c 61 75 73 65 20 69 66 20 61 20 64  OM clause if a d
2b4a8 69 66 66 65 72 65 6e 74 20 6f 72 64 65 72 20 69  ifferent order i
2b4a9 73 20 62 65 74 74 65 72 20 61 62 6c 65 20 74 6f  s better able to
2b4aa 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20   make.** use of 
2b4ab 69 6e 64 69 63 65 73 2e 20 20 4e 6f 74 65 20 61  indices.  Note a
2b4ac 6c 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68  lso that when th
2b4ad 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 61 70  e IN operator ap
2b4ae 70 65 61 72 73 20 69 6e 0a 2a 2a 20 74 68 65 20  pears in.** the 
2b4af 57 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69 74  WHERE clause, it
2b4b0 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e   might result in
2b4b1 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e 65 73 74   additional nest
2b4b2 65 64 20 6c 6f 6f 70 73 20 66 6f 72 0a 2a 2a 20  ed loops for.** 
2b4b3 73 63 61 6e 6e 69 6e 67 20 74 68 72 6f 75 67 68  scanning through
2b4b4 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f 6e 20 74   all values on t
2b4b5 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
2b4b6 64 65 20 6f 66 20 74 68 65 20 49 4e 2e 0a 2a 2a  de of the IN..**
2b4b7 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 42 74  .** There are Bt
2b4b8 72 65 65 20 63 75 72 73 6f 72 73 20 61 73 73 6f  ree cursors asso
2b4b9 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68  ciated with each
2b4ba 20 74 61 62 6c 65 2e 20 20 74 31 20 75 73 65 73   table.  t1 uses
2b4bb 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65   cursor.** numbe
2b4bc 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d  r pTabList->a[0]
2b4bd 2e 69 43 75 72 73 6f 72 2e 20 20 74 32 20 75 73  .iCursor.  t2 us
2b4be 65 73 20 74 68 65 20 63 75 72 73 6f 72 20 70 54  es the cursor pT
2b4bf 61 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e 69 43 75  abList->a[1].iCu
2b4c0 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20  rsor..** And so 
2b4c1 66 6f 72 74 68 2e 20 20 54 68 69 73 20 72 6f 75  forth.  This rou
2b4c2 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63  tine generates c
2b4c3 6f 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 6f 73  ode to open thos
2b4c4 65 20 56 44 42 45 20 63 75 72 73 6f 72 73 0a 2a  e VDBE cursors.*
2b4c5 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 57 68 65  * and sqlite3Whe
2b4c6 72 65 45 6e 64 28 29 20 67 65 6e 65 72 61 74 65  reEnd() generate
2b4c7 73 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63 6c  s the code to cl
2b4c8 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20  ose them..**.** 
2b4c9 54 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 71  The code that sq
2b4ca 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
2b4cb 29 20 67 65 6e 65 72 61 74 65 73 20 6c 65 61 76  ) generates leav
2b4cc 65 73 20 74 68 65 20 63 75 72 73 6f 72 73 20 6e  es the cursors n
2b4cd 61 6d 65 64 0a 2a 2a 20 69 6e 20 70 54 61 62 4c  amed.** in pTabL
2b4ce 69 73 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  ist pointing at 
2b4cf 74 68 65 69 72 20 61 70 70 72 6f 70 72 69 61 74  their appropriat
2b4d0 65 20 65 6e 74 72 69 65 73 2e 20 20 54 68 65 20  e entries.  The 
2b4d1 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a 20 63 61  [...] code.** ca
2b4d2 6e 20 75 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20  n use OP_Column 
2b4d3 61 6e 64 20 4f 50 5f 52 6f 77 69 64 20 6f 70 63  and OP_Rowid opc
2b4d4 6f 64 65 73 20 6f 6e 20 74 68 65 73 65 20 63 75  odes on these cu
2b4d5 72 73 6f 72 73 20 74 6f 20 65 78 74 72 61 63 74  rsors to extract
2b4d6 0a 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68  .** data from th
2b4d7 65 20 76 61 72 69 6f 75 73 20 74 61 62 6c 65 73  e various tables
2b4d8 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a 2a   of the loop..**
2b4d9 0a 2a 2a 20 49 66 20 74 68 65 20 57 48 45 52 45  .** If the WHERE
2b4da 20 63 6c 61 75 73 65 20 69 73 20 65 6d 70 74 79   clause is empty
2b4db 2c 20 74 68 65 20 66 6f 72 65 61 63 68 20 6c 6f  , the foreach lo
2b4dc 6f 70 73 20 6d 75 73 74 20 65 61 63 68 20 73 63  ops must each sc
2b4dd 61 6e 20 74 68 65 69 72 0a 2a 2a 20 65 6e 74 69  an their.** enti
2b4de 72 65 20 74 61 62 6c 65 73 2e 20 20 54 68 75 73  re tables.  Thus
2b4df 20 61 20 74 68 72 65 65 2d 77 61 79 20 6a 6f 69   a three-way joi
2b4e0 6e 20 69 73 20 61 6e 20 4f 28 4e 5e 33 29 20 6f  n is an O(N^3) o
2b4e1 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69  peration.  But i
2b4e2 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 73 20  f.** the tables 
2b4e3 68 61 76 65 20 69 6e 64 69 63 65 73 20 61 6e 64  have indices and
2b4e4 20 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73   there are terms
2b4e5 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
2b4e6 61 75 73 65 20 74 68 61 74 0a 2a 2a 20 72 65 66  ause that.** ref
2b4e7 65 72 20 74 6f 20 74 68 6f 73 65 20 69 6e 64 69  er to those indi
2b4e8 63 65 73 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20  ces, a complete 
2b4e9 74 61 62 6c 65 20 73 63 61 6e 20 63 61 6e 20 62  table scan can b
2b4ea 65 20 61 76 6f 69 64 65 64 20 61 6e 64 20 74 68  e avoided and th
2b4eb 65 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 72  e.** code will r
2b4ec 75 6e 20 6d 75 63 68 20 66 61 73 74 65 72 2e 20  un much faster. 
2b4ed 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72   Most of the wor
2b4ee 6b 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  k of this routin
2b4ef 65 20 69 73 20 63 68 65 63 6b 69 6e 67 0a 2a 2a  e is checking.**
2b4f0 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 72 65   to see if there
2b4f1 20 61 72 65 20 69 6e 64 69 63 65 73 20 74 68 61   are indices tha
2b4f2 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  t can be used to
2b4f3 20 73 70 65 65 64 20 75 70 20 74 68 65 20 6c 6f   speed up the lo
2b4f4 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20  op..**.** Terms 
2b4f5 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
2b4f6 75 73 65 20 61 72 65 20 61 6c 73 6f 20 75 73 65  use are also use
2b4f7 64 20 74 6f 20 6c 69 6d 69 74 20 77 68 69 63 68  d to limit which
2b4f8 20 72 6f 77 73 20 61 63 74 75 61 6c 6c 79 0a 2a   rows actually.*
2b4f9 2a 20 6d 61 6b 65 20 69 74 20 74 6f 20 74 68 65  * make it to the
2b4fa 20 22 2e 2e 2e 22 20 69 6e 20 74 68 65 20 6d 69   "..." in the mi
2b4fb 64 64 6c 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70  ddle of the loop
2b4fc 2e 20 20 41 66 74 65 72 20 65 61 63 68 20 22 66  .  After each "f
2b4fd 6f 72 65 61 63 68 22 2c 0a 2a 2a 20 74 65 72 6d  oreach",.** term
2b4fe 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  s of the WHERE c
2b4ff 6c 61 75 73 65 20 74 68 61 74 20 75 73 65 20 6f  lause that use o
2b500 6e 6c 79 20 74 65 72 6d 73 20 69 6e 20 74 68 61  nly terms in tha
2b501 74 20 6c 6f 6f 70 20 61 6e 64 20 6f 75 74 65 72  t loop and outer
2b502 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72 65 20 65 76  .** loops are ev
2b503 61 6c 75 61 74 65 64 20 61 6e 64 20 69 66 20 66  aluated and if f
2b504 61 6c 73 65 20 61 20 6a 75 6d 70 20 69 73 20 6d  alse a jump is m
2b505 61 64 65 20 61 72 6f 75 6e 64 20 61 6c 6c 20 73  ade around all s
2b506 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 6e  ubsequent.** inn
2b507 65 72 20 6c 6f 6f 70 73 20 28 6f 72 20 61 72 6f  er loops (or aro
2b508 75 6e 64 20 74 68 65 20 22 2e 2e 2e 22 20 69 66  und the "..." if
2b509 20 74 68 65 20 74 65 73 74 20 6f 63 63 75 72 73   the test occurs
2b50a 20 77 69 74 68 69 6e 20 74 68 65 20 69 6e 6e 65   within the inne
2b50b 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f 6f 70 29  r-.** most loop)
2b50c 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20 4a 4f 49  .**.** OUTER JOI
2b50d 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f 75 74 65  NS.**.** An oute
2b50e 72 20 6a 6f 69 6e 20 6f 66 20 74 61 62 6c 65 73  r join of tables
2b50f 20 74 31 20 61 6e 64 20 74 32 20 69 73 20 63 6f   t1 and t2 is co
2b510 6e 63 65 70 74 61 6c 6c 79 20 63 6f 64 65 64 20  nceptally coded 
2b511 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
2b512 2a 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77  *    foreach row
2b513 31 20 69 6e 20 74 31 20 64 6f 0a 2a 2a 20 20 20  1 in t1 do.**   
2b514 20 20 20 66 6c 61 67 20 3d 20 30 0a 2a 2a 20 20     flag = 0.**  
2b515 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 32      foreach row2
2b516 20 69 6e 20 74 32 20 64 6f 0a 2a 2a 20 20 20 20   in t2 do.**    
2b517 20 20 20 20 73 74 61 72 74 3a 0a 2a 2a 20 20 20      start:.**   
2b518 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20         ....**   
2b519 20 20 20 20 20 20 20 66 6c 61 67 20 3d 20 31 0a         flag = 1.
2b51a 2a 2a 20 20 20 20 20 20 65 6e 64 0a 2a 2a 20 20  **      end.**  
2b51b 20 20 20 20 69 66 20 66 6c 61 67 3d 3d 30 20 74      if flag==0 t
2b51c 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 6d 6f  hen.**        mo
2b51d 76 65 20 74 68 65 20 72 6f 77 32 20 63 75 72 73  ve the row2 curs
2b51e 6f 72 20 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77  or to a null row
2b51f 0a 2a 2a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .**        goto 
2b520 73 74 61 72 74 0a 2a 2a 20 20 20 20 20 20 66 69  start.**      fi
2b521 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a 0a 2a 2a  .**    end.**.**
2b522 20 4f 52 44 45 52 20 42 59 20 43 4c 41 55 53 45   ORDER BY CLAUSE
2b523 20 50 52 4f 43 45 53 53 49 4e 47 0a 2a 2a 0a 2a   PROCESSING.**.*
2b524 2a 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20  * *ppOrderBy is 
2b525 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
2b526 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2b527 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
2b528 74 65 6d 65 6e 74 2c 0a 2a 2a 20 69 66 20 74 68  tement,.** if th
2b529 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 49 66 20  ere is one.  If 
2b52a 74 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45  there is no ORDE
2b52b 52 20 42 59 20 63 6c 61 75 73 65 20 6f 72 20 69  R BY clause or i
2b52c 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  f this routine.*
2b52d 2a 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d  * is called from
2b52e 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45   an UPDATE or DE
2b52f 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 2c 20  LETE statement, 
2b530 74 68 65 6e 20 70 70 4f 72 64 65 72 42 79 20 69  then ppOrderBy i
2b531 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66  s NULL..**.** If
2b532 20 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 62 65   an index can be
2b533 20 75 73 65 64 20 73 6f 20 74 68 61 74 20 74 68   used so that th
2b534 65 20 6e 61 74 75 72 61 6c 20 6f 75 74 70 75 74  e natural output
2b535 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 74 61   order of the ta
2b536 62 6c 65 0a 2a 2a 20 73 63 61 6e 20 69 73 20 63  ble.** scan is c
2b537 6f 72 72 65 63 74 20 66 6f 72 20 74 68 65 20 4f  orrect for the O
2b538 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20  RDER BY clause, 
2b539 74 68 65 6e 20 74 68 61 74 20 69 6e 64 65 78 20  then that index 
2b53a 69 73 20 75 73 65 64 20 61 6e 64 0a 2a 2a 20 2a  is used and.** *
2b53b 70 70 4f 72 64 65 72 42 79 20 69 73 20 73 65 74  ppOrderBy is set
2b53c 20 74 6f 20 4e 55 4c 4c 2e 20 20 54 68 69 73 20   to NULL.  This 
2b53d 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  is an optimizati
2b53e 6f 6e 20 74 68 61 74 20 70 72 65 76 65 6e 74 73  on that prevents
2b53f 20 61 6e 0a 2a 2a 20 75 6e 6e 65 63 65 73 73 61   an.** unnecessa
2b540 72 79 20 73 6f 72 74 20 6f 66 20 74 68 65 20 72  ry sort of the r
2b541 65 73 75 6c 74 20 73 65 74 20 69 66 20 61 6e 20  esult set if an 
2b542 69 6e 64 65 78 20 61 70 70 72 6f 70 72 69 61 74  index appropriat
2b543 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 4f 52 44  e for the.** ORD
2b544 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 6c 72  ER BY clause alr
2b545 65 61 64 79 20 65 78 69 73 74 73 2e 0a 2a 2a 0a  eady exists..**.
2b546 2a 2a 20 49 66 20 74 68 65 20 77 68 65 72 65 20  ** If the where 
2b547 63 6c 61 75 73 65 20 6c 6f 6f 70 73 20 63 61 6e  clause loops can
2b548 6e 6f 74 20 62 65 20 61 72 72 61 6e 67 65 64 20  not be arranged 
2b549 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20 63  to provide the c
2b54a 6f 72 72 65 63 74 0a 2a 2a 20 6f 75 74 70 75 74  orrect.** output
2b54b 20 6f 72 64 65 72 2c 20 74 68 65 6e 20 74 68 65   order, then the
2b54c 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 75   *ppOrderBy is u
2b54d 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c  nchanged..*/.SQL
2b54e 49 54 45 5f 50 52 49 56 41 54 45 20 57 68 65 72  ITE_PRIVATE Wher
2b54f 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57 68  eInfo *sqlite3Wh
2b550 65 72 65 42 65 67 69 6e 28 0a 20 20 50 61 72 73  ereBegin(.  Pars
2b551 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2b552 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
2b553 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
2b554 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20  List *pTabList, 
2b555 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20     /* A list of 
2b556 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 62 65  all tables to be
2b557 20 73 63 61 6e 6e 65 64 20 2a 2f 0a 20 20 45 78   scanned */.  Ex
2b558 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20  pr *pWhere,     
2b559 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
2b55a 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70   clause */.  Exp
2b55b 72 4c 69 73 74 20 2a 2a 70 70 4f 72 64 65 72 42  rList **ppOrderB
2b55c 79 2c 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42  y, /* An ORDER B
2b55d 59 20 63 6c 61 75 73 65 2c 20 6f 72 20 4e 55 4c  Y clause, or NUL
2b55e 4c 20 2a 2f 0a 20 20 75 38 20 77 63 74 72 6c 46  L */.  u8 wctrlF
2b55f 6c 61 67 73 2c 20 20 20 20 20 20 20 20 2f 2a 20  lags,        /* 
2b560 4f 6e 65 20 6f 66 20 74 68 65 20 57 48 45 52 45  One of the WHERE
2b561 5f 2a 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64  _* flags defined
2b562 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
2b563 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 53  */.  int regRowS
2b564 65 74 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  et         /* Re
2b565 67 69 73 74 65 72 20 68 6f 6c 64 20 52 6f 77 53  gister hold RowS
2b566 65 74 20 69 66 20 57 48 45 52 45 5f 46 49 4c 4c  et if WHERE_FILL
2b567 5f 52 4f 57 53 45 54 20 69 73 20 73 65 74 20 2a  _ROWSET is set *
2b568 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
2b569 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b56a 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2b56b 72 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f  r */.  WhereInfo
2b56c 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20   *pWInfo;       
2b56d 20 20 2f 2a 20 57 69 6c 6c 20 62 65 63 6f 6d 65    /* Will become
2b56e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
2b56f 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  e of this functi
2b570 6f 6e 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20  on */.  Vdbe *v 
2b571 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2b572 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2b573 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
2b574 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  e */.  Bitmask n
2b575 6f 74 52 65 61 64 79 3b 20 20 20 20 20 20 20 20  otReady;        
2b576 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 74 68 61    /* Cursors tha
2b577 74 20 61 72 65 20 6e 6f 74 20 79 65 74 20 70 6f  t are not yet po
2b578 73 69 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 57 68  sitioned */.  Wh
2b579 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2b57a 6b 53 65 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  kSet;    /* The 
2b57b 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20  expression mask 
2b57c 73 65 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  set */.  WhereCl
2b57d 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20  ause *pWC;      
2b57e 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f           /* Deco
2b57f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65  mposition of the
2b580 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2b581 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2b582 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d  t_item *pTabItem
2b583 3b 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 65  ;  /* A single e
2b584 6e 74 72 79 20 66 72 6f 6d 20 70 54 61 62 4c 69  ntry from pTabLi
2b585 73 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76  st */.  WhereLev
2b586 65 6c 20 2a 70 4c 65 76 65 6c 3b 20 20 20 20 20  el *pLevel;     
2b587 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e          /* A sin
2b588 67 6c 65 20 6c 65 76 65 6c 20 69 6e 20 74 68 65  gle level in the
2b589 20 70 57 49 6e 66 6f 20 6c 69 73 74 20 2a 2f 0a   pWInfo list */.
2b58a 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20    int iFrom;    
2b58b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b58c 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65    /* First unuse
2b58d 64 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6c  d FROM clause el
2b58e 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 61  ement */.  int a
2b58f 6e 64 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  ndFlags;        
2b590 20 20 20 20 20 20 2f 2a 20 41 4e 44 2d 65 64 20        /* AND-ed 
2b591 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 61  combination of a
2b592 6c 6c 20 70 57 43 2d 3e 61 5b 5d 2e 77 74 46 6c  ll pWC->a[].wtFl
2b593 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ags */.  sqlite3
2b594 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
2b595 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2b596 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
2b597 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2b598 42 79 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68  By = 0;..  /* Th
2b599 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c  e number of tabl
2b59a 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
2b59b 6c 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65 64  lause is limited
2b59c 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
2b59d 66 0a 20 20 2a 2a 20 62 69 74 73 20 69 6e 20 61  f.  ** bits in a
2b59e 20 42 69 74 6d 61 73 6b 20 0a 20 20 2a 2f 0a 20   Bitmask .  */. 
2b59f 20 69 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e   if( pTabList->n
2b5a0 53 72 63 3e 42 4d 53 20 29 7b 0a 20 20 20 20 73  Src>BMS ){.    s
2b5a1 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2b5a2 50 61 72 73 65 2c 20 22 61 74 20 6d 6f 73 74 20  Parse, "at most 
2b5a3 25 64 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a  %d tables in a j
2b5a4 6f 69 6e 22 2c 20 42 4d 53 29 3b 0a 20 20 20 20  oin", BMS);.    
2b5a5 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
2b5a6 20 69 66 28 20 70 70 4f 72 64 65 72 42 79 20 29   if( ppOrderBy )
2b5a7 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d  {.    pOrderBy =
2b5a8 20 2a 70 70 4f 72 64 65 72 42 79 3b 0a 20 20 7d   *ppOrderBy;.  }
2b5a9 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
2b5aa 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 74  and initialize t
2b5ab 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72  he WhereInfo str
2b5ac 75 63 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c  ucture that will
2b5ad 20 62 65 63 6f 6d 65 20 74 68 65 0a 20 20 2a 2a   become the.  **
2b5ae 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a 20   return value.. 
2b5af 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61 72 73   */.  db = pPars
2b5b0 65 2d 3e 64 62 3b 0a 20 20 70 57 49 6e 66 6f 20  e->db;.  pWInfo 
2b5b1 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2b5b2 63 5a 65 72 6f 28 64 62 2c 20 20 0a 20 20 20 20  cZero(db,  .    
2b5b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5b4 20 20 73 69 7a 65 6f 66 28 57 68 65 72 65 49 6e    sizeof(WhereIn
2b5b5 66 6f 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  fo).            
2b5b6 20 20 20 20 20 20 20 20 20 20 2b 20 28 70 54 61            + (pTa
2b5b7 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 29 2a 73  bList->nSrc-1)*s
2b5b8 69 7a 65 6f 66 28 57 68 65 72 65 4c 65 76 65 6c  izeof(WhereLevel
2b5b9 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
2b5ba 20 20 20 20 20 20 20 20 2b 20 73 69 7a 65 6f 66          + sizeof
2b5bb 28 57 68 65 72 65 43 6c 61 75 73 65 29 0a 20 20  (WhereClause).  
2b5bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5bd 20 20 20 20 2b 20 73 69 7a 65 6f 66 28 57 68 65      + sizeof(Whe
2b5be 72 65 4d 61 73 6b 53 65 74 29 0a 20 20 20 20 20  reMaskSet).     
2b5bf 20 20 20 20 20 20 29 3b 0a 20 20 69 66 28 20 64        );.  if( d
2b5c0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2b5c1 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72  ){.    goto wher
2b5c2 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d  eBeginError;.  }
2b5c3 0a 20 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65  .  pWInfo->nLeve
2b5c4 6c 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  l = pTabList->nS
2b5c5 72 63 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 50  rc;.  pWInfo->pP
2b5c6 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2b5c7 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73   pWInfo->pTabLis
2b5c8 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20  t = pTabList;.  
2b5c9 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 20 3d  pWInfo->iBreak =
2b5ca 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2b5cb 4c 61 62 65 6c 28 76 29 3b 0a 20 20 70 57 49 6e  Label(v);.  pWIn
2b5cc 66 6f 2d 3e 72 65 67 52 6f 77 53 65 74 20 3d 20  fo->regRowSet = 
2b5cd 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48  (wctrlFlags & WH
2b5ce 45 52 45 5f 46 49 4c 4c 5f 52 4f 57 53 45 54 29  ERE_FILL_ROWSET)
2b5cf 20 3f 20 72 65 67 52 6f 77 53 65 74 20 3a 20 2d   ? regRowSet : -
2b5d0 31 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 57 43  1;.  pWInfo->pWC
2b5d1 20 3d 20 70 57 43 20 3d 20 28 57 68 65 72 65 43   = pWC = (WhereC
2b5d2 6c 61 75 73 65 2a 29 26 70 57 49 6e 66 6f 2d 3e  lause*)&pWInfo->
2b5d3 61 5b 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c  a[pWInfo->nLevel
2b5d4 5d 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 77 63 74  ];.  pWInfo->wct
2b5d5 72 6c 46 6c 61 67 73 20 3d 20 77 63 74 72 6c 46  rlFlags = wctrlF
2b5d6 6c 61 67 73 3b 0a 20 20 70 4d 61 73 6b 53 65 74  lags;.  pMaskSet
2b5d7 20 3d 20 28 57 68 65 72 65 4d 61 73 6b 53 65 74   = (WhereMaskSet
2b5d8 2a 29 26 70 57 43 5b 31 5d 3b 0a 0a 20 20 2f 2a  *)&pWC[1];..  /*
2b5d9 20 53 70 6c 69 74 20 74 68 65 20 57 48 45 52 45   Split the WHERE
2b5da 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 73 65 70   clause into sep
2b5db 61 72 61 74 65 20 73 75 62 65 78 70 72 65 73 73  arate subexpress
2b5dc 69 6f 6e 73 20 77 68 65 72 65 20 65 61 63 68 0a  ions where each.
2b5dd 20 20 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69    ** subexpressi
2b5de 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65 64 20  on is separated 
2b5df 62 79 20 61 6e 20 41 4e 44 20 6f 70 65 72 61 74  by an AND operat
2b5e0 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 6e 69 74 4d  or..  */.  initM
2b5e1 61 73 6b 53 65 74 28 70 4d 61 73 6b 53 65 74 29  askSet(pMaskSet)
2b5e2 3b 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 49  ;.  whereClauseI
2b5e3 6e 69 74 28 70 57 43 2c 20 70 50 61 72 73 65 2c  nit(pWC, pParse,
2b5e4 20 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20 73 71   pMaskSet);.  sq
2b5e5 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e  lite3ExprCodeCon
2b5e6 73 74 61 6e 74 73 28 70 50 61 72 73 65 2c 20 70  stants(pParse, p
2b5e7 57 68 65 72 65 29 3b 0a 20 20 77 68 65 72 65 53  Where);.  whereS
2b5e8 70 6c 69 74 28 70 57 43 2c 20 70 57 68 65 72 65  plit(pWC, pWhere
2b5e9 2c 20 54 4b 5f 41 4e 44 29 3b 0a 20 20 20 20 0a  , TK_AND);.    .
2b5ea 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
2b5eb 65 3a 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  e: a WHERE claus
2b5ec 65 20 74 68 61 74 20 69 73 20 63 6f 6e 73 74 61  e that is consta
2b5ed 6e 74 2e 20 20 45 76 61 6c 75 61 74 65 20 74 68  nt.  Evaluate th
2b5ee 65 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f  e.  ** expressio
2b5ef 6e 20 61 6e 64 20 65 69 74 68 65 72 20 6a 75 6d  n and either jum
2b5f0 70 20 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74 68  p over all of th
2b5f1 65 20 63 6f 64 65 20 6f 72 20 66 61 6c 6c 20 74  e code or fall t
2b5f2 68 72 75 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  hru..  */.  if( 
2b5f3 70 57 68 65 72 65 20 26 26 20 28 70 54 61 62 4c  pWhere && (pTabL
2b5f4 69 73 74 2d 3e 6e 53 72 63 3d 3d 30 20 7c 7c 20  ist->nSrc==0 || 
2b5f5 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
2b5f6 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 57 68  stantNotJoin(pWh
2b5f7 65 72 65 29 29 20 29 7b 0a 20 20 20 20 73 71 6c  ere)) ){.    sql
2b5f8 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
2b5f9 70 50 61 72 73 65 2c 20 70 57 68 65 72 65 2c 20  pParse, pWhere, 
2b5fa 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 2c 20  pWInfo->iBreak, 
2b5fb 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
2b5fc 4c 29 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d  L);.    pWhere =
2b5fd 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73   0;.  }..  /* As
2b5fe 73 69 67 6e 20 61 20 62 69 74 20 66 72 6f 6d 20  sign a bit from 
2b5ff 74 68 65 20 62 69 74 6d 61 73 6b 20 74 6f 20 65  the bitmask to e
2b600 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65  very term in the
2b601 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 20 20   FROM clause..  
2b602 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 61 73 73  **.  ** When ass
2b603 69 67 6e 69 6e 67 20 62 69 74 6d 61 73 6b 20 76  igning bitmask v
2b604 61 6c 75 65 73 20 74 6f 20 46 52 4f 4d 20 63 6c  alues to FROM cl
2b605 61 75 73 65 20 63 75 72 73 6f 72 73 2c 20 69 74  ause cursors, it
2b606 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 74 68   must be.  ** th
2b607 65 20 63 61 73 65 20 74 68 61 74 20 69 66 20 58  e case that if X
2b608 20 69 73 20 74 68 65 20 62 69 74 6d 61 73 6b 20   is the bitmask 
2b609 66 6f 72 20 74 68 65 20 4e 2d 74 68 20 46 52 4f  for the N-th FRO
2b60a 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 68  M clause term th
2b60b 65 6e 0a 20 20 2a 2a 20 74 68 65 20 62 69 74 6d  en.  ** the bitm
2b60c 61 73 6b 20 66 6f 72 20 61 6c 6c 20 46 52 4f 4d  ask for all FROM
2b60d 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 74 6f   clause terms to
2b60e 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
2b60f 20 4e 2d 74 68 20 74 65 72 6d 0a 20 20 2a 2a 20   N-th term.  ** 
2b610 69 73 20 28 58 2d 31 29 2e 20 20 20 41 6e 20 65  is (X-1).   An e
2b611 78 70 72 65 73 73 69 6f 6e 20 66 72 6f 6d 20 74  xpression from t
2b612 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20  he ON clause of 
2b613 61 20 4c 45 46 54 20 4a 4f 49 4e 20 63 61 6e 20  a LEFT JOIN can 
2b614 75 73 65 0a 20 20 2a 2a 20 69 74 73 20 45 78 70  use.  ** its Exp
2b615 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  r.iRightJoinTabl
2b616 65 20 76 61 6c 75 65 20 74 6f 20 66 69 6e 64 20  e value to find 
2b617 74 68 65 20 62 69 74 6d 61 73 6b 20 6f 66 20 74  the bitmask of t
2b618 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 0a 20  he right table. 
2b619 20 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e   ** of the join.
2b61a 20 20 53 75 62 74 72 61 63 74 69 6e 67 20 6f 6e    Subtracting on
2b61b 65 20 66 72 6f 6d 20 74 68 65 20 72 69 67 68 74  e from the right
2b61c 20 74 61 62 6c 65 20 62 69 74 6d 61 73 6b 20 67   table bitmask g
2b61d 69 76 65 73 20 61 0a 20 20 2a 2a 20 62 69 74 6d  ives a.  ** bitm
2b61e 61 73 6b 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c  ask for all tabl
2b61f 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  es to the left o
2b620 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 4b 6e 6f  f the join.  Kno
2b621 77 69 6e 67 20 74 68 65 20 62 69 74 6d 61 73 6b  wing the bitmask
2b622 0a 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61  .  ** for all ta
2b623 62 6c 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74  bles to the left
2b624 20 6f 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e 20   of a left join 
2b625 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54  is important.  T
2b626 69 63 6b 65 74 20 23 33 30 31 35 2e 0a 20 20 2a  icket #3015..  *
2b627 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
2b628 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
2b629 2b 2b 29 7b 0a 20 20 20 20 63 72 65 61 74 65 4d  ++){.    createM
2b62a 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54  ask(pMaskSet, pT
2b62b 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75  abList->a[i].iCu
2b62c 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64  rsor);.  }.#ifnd
2b62d 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20  ef NDEBUG.  {.  
2b62e 20 20 42 69 74 6d 61 73 6b 20 74 6f 54 68 65 4c    Bitmask toTheL
2b62f 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  eft = 0;.    for
2b630 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74  (i=0; i<pTabList
2b631 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
2b632 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 20 3d 20      Bitmask m = 
2b633 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
2b634 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d  , pTabList->a[i]
2b635 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  .iCursor);.     
2b636 20 61 73 73 65 72 74 28 20 28 6d 2d 31 29 3d 3d   assert( (m-1)==
2b637 74 6f 54 68 65 4c 65 66 74 20 29 3b 0a 20 20 20  toTheLeft );.   
2b638 20 20 20 74 6f 54 68 65 4c 65 66 74 20 7c 3d 20     toTheLeft |= 
2b639 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  m;.    }.  }.#en
2b63a 64 69 66 0a 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a  dif..  /* Analyz
2b63b 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73 75 62  e all of the sub
2b63c 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 4e 6f  expressions.  No
2b63d 74 65 20 74 68 61 74 20 65 78 70 72 41 6e 61 6c  te that exprAnal
2b63e 79 7a 65 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a  yze() might.  **
2b63f 20 61 64 64 20 6e 65 77 20 76 69 72 74 75 61 6c   add new virtual
2b640 20 74 65 72 6d 73 20 6f 6e 74 6f 20 74 68 65 20   terms onto the 
2b641 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52 45  end of the WHERE
2b642 20 63 6c 61 75 73 65 2e 20 20 57 65 20 64 6f 20   clause.  We do 
2b643 6e 6f 74 0a 20 20 2a 2a 20 77 61 6e 74 20 74 6f  not.  ** want to
2b644 20 61 6e 61 6c 79 7a 65 20 74 68 65 73 65 20 76   analyze these v
2b645 69 72 74 75 61 6c 20 74 65 72 6d 73 2c 20 73 6f  irtual terms, so
2b646 20 73 74 61 72 74 20 61 6e 61 6c 79 7a 69 6e 67   start analyzing
2b647 20 61 74 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a   at the end.  **
2b648 20 61 6e 64 20 77 6f 72 6b 20 66 6f 72 77 61 72   and work forwar
2b649 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 61 64  d so that the ad
2b64a 64 65 64 20 76 69 72 74 75 61 6c 20 74 65 72 6d  ded virtual term
2b64b 73 20 61 72 65 20 6e 65 76 65 72 20 70 72 6f 63  s are never proc
2b64c 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 78  essed..  */.  ex
2b64d 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 54 61  prAnalyzeAll(pTa
2b64e 62 4c 69 73 74 2c 20 70 57 43 29 3b 0a 20 20 69  bList, pWC);.  i
2b64f 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2b650 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
2b651 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b  whereBeginError;
2b652 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 6f 73 65  .  }..  /* Chose
2b653 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20   the best index 
2b654 74 6f 20 75 73 65 20 66 6f 72 20 65 61 63 68 20  to use for each 
2b655 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f  table in the FRO
2b656 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20  M clause..  **. 
2b657 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 66 69   ** This loop fi
2b658 6c 6c 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  lls in the follo
2b659 77 69 6e 67 20 66 69 65 6c 64 73 3a 0a 20 20 2a  wing fields:.  *
2b65a 2a 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  *.  **   pWInfo-
2b65b 3e 61 5b 5d 2e 70 49 64 78 20 20 20 20 20 20 54  >a[].pIdx      T
2b65c 68 65 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20  he index to use 
2b65d 66 6f 72 20 74 68 69 73 20 6c 65 76 65 6c 20 6f  for this level o
2b65e 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 2a 2a  f the loop..  **
2b65f 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 77     pWInfo->a[].w
2b660 73 46 6c 61 67 73 20 20 20 57 48 45 52 45 5f 78  sFlags   WHERE_x
2b661 78 78 20 66 6c 61 67 73 20 61 73 73 6f 63 69 61  xx flags associa
2b662 74 65 64 20 77 69 74 68 20 70 49 64 78 0a 20 20  ted with pIdx.  
2b663 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d  **   pWInfo->a[]
2b664 2e 6e 45 71 20 20 20 20 20 20 20 54 68 65 20 6e  .nEq       The n
2b665 75 6d 62 65 72 20 6f 66 20 3d 3d 20 61 6e 64 20  umber of == and 
2b666 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20  IN constraints. 
2b667 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b   **   pWInfo->a[
2b668 5d 2e 69 46 72 6f 6d 20 20 20 20 20 57 68 69 63  ].iFrom     Whic
2b669 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52  h term of the FR
2b66a 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 62 65 69  OM clause is bei
2b66b 6e 67 20 63 6f 64 65 64 0a 20 20 2a 2a 20 20 20  ng coded.  **   
2b66c 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 54 61 62  pWInfo->a[].iTab
2b66d 43 75 72 20 20 20 54 68 65 20 56 44 42 45 20 63  Cur   The VDBE c
2b66e 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 64 61  ursor for the da
2b66f 74 61 62 61 73 65 20 74 61 62 6c 65 0a 20 20 2a  tabase table.  *
2b670 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e  *   pWInfo->a[].
2b671 69 49 64 78 43 75 72 20 20 20 54 68 65 20 56 44  iIdxCur   The VD
2b672 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68  BE cursor for th
2b673 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 20 20 70  e index.  **   p
2b674 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 54 65 72 6d  WInfo->a[].pTerm
2b675 20 20 20 20 20 57 68 65 6e 20 77 73 46 6c 61 67       When wsFlag
2b676 73 3d 3d 57 4f 5f 4f 52 2c 20 74 68 65 20 4f 52  s==WO_OR, the OR
2b677 2d 63 6c 61 75 73 65 20 74 65 72 6d 0a 20 20 2a  -clause term.  *
2b678 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70  *.  ** This loop
2b679 20 61 6c 73 6f 20 66 69 67 75 72 65 73 20 6f 75   also figures ou
2b67a 74 20 74 68 65 20 6e 65 73 74 69 6e 67 20 6f 72  t the nesting or
2b67b 64 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e  der of tables in
2b67c 20 74 68 65 20 46 52 4f 4d 0a 20 20 2a 2a 20 63   the FROM.  ** c
2b67d 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6e 6f  lause..  */.  no
2b67e 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d 61  tReady = ~(Bitma
2b67f 73 6b 29 30 3b 0a 20 20 70 54 61 62 49 74 65 6d  sk)0;.  pTabItem
2b680 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 3b 0a   = pTabList->a;.
2b681 20 20 70 4c 65 76 65 6c 20 3d 20 70 57 49 6e 66    pLevel = pWInf
2b682 6f 2d 3e 61 3b 0a 20 20 61 6e 64 46 6c 61 67 73  o->a;.  andFlags
2b683 20 3d 20 7e 30 3b 0a 20 20 57 48 45 52 45 54 52   = ~0;.  WHERETR
2b684 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69  ACE(("*** Optimi
2b685 7a 65 72 20 53 74 61 72 74 20 2a 2a 2a 5c 6e 22  zer Start ***\n"
2b686 29 29 3b 0a 20 20 66 6f 72 28 69 3d 69 46 72 6f  ));.  for(i=iFro
2b687 6d 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e  m=0, pLevel=pWIn
2b688 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73  fo->a; i<pTabLis
2b689 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c  t->nSrc; i++, pL
2b68a 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 57 68 65  evel++){.    Whe
2b68b 72 65 43 6f 73 74 20 62 65 73 74 50 6c 61 6e 3b  reCost bestPlan;
2b68c 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74           /* Most
2b68d 20 65 66 66 69 63 69 65 6e 74 20 70 6c 61 6e 20   efficient plan 
2b68e 73 65 65 6e 20 73 6f 20 66 61 72 20 2a 2f 0a 20  seen so far */. 
2b68f 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20     Index *pIdx; 
2b690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b691 2a 20 49 6e 64 65 78 20 66 6f 72 20 46 52 4f 4d  * Index for FROM
2b692 20 74 61 62 6c 65 20 61 74 20 70 54 61 62 49 74   table at pTabIt
2b693 65 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6a 3b  em */.    int j;
2b694 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b695 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
2b696 70 69 6e 67 20 6f 76 65 72 20 46 52 4f 4d 20 74  ping over FROM t
2b697 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74  ables */.    int
2b698 20 62 65 73 74 4a 20 3d 20 30 3b 20 20 20 20 20   bestJ = 0;     
2b699 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2b69a 76 61 6c 75 65 20 6f 66 20 6a 20 2a 2f 0a 20 20  value of j */.  
2b69b 20 20 42 69 74 6d 61 73 6b 20 6d 3b 20 20 20 20    Bitmask m;    
2b69c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b69d 20 42 69 74 6d 61 73 6b 20 76 61 6c 75 65 20 66   Bitmask value f
2b69e 6f 72 20 6a 20 6f 72 20 62 65 73 74 4a 20 2a 2f  or j or bestJ */
2b69f 0a 20 20 20 20 69 6e 74 20 6f 6e 63 65 20 3d 20  .    int once = 
2b6a0 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2b6a1 20 2f 2a 20 54 72 75 65 20 77 68 65 6e 20 66 69   /* True when fi
2b6a2 72 73 74 20 74 61 62 6c 65 20 69 73 20 73 65 65  rst table is see
2b6a3 6e 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74  n */..    memset
2b6a4 28 26 62 65 73 74 50 6c 61 6e 2c 20 30 2c 20 73  (&bestPlan, 0, s
2b6a5 69 7a 65 6f 66 28 62 65 73 74 50 6c 61 6e 29 29  izeof(bestPlan))
2b6a6 3b 0a 20 20 20 20 62 65 73 74 50 6c 61 6e 2e 72  ;.    bestPlan.r
2b6a7 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49  Cost = SQLITE_BI
2b6a8 47 5f 44 42 4c 3b 0a 20 20 20 20 66 6f 72 28 6a  G_DBL;.    for(j
2b6a9 3d 69 46 72 6f 6d 2c 20 70 54 61 62 49 74 65 6d  =iFrom, pTabItem
2b6aa 3d 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d  =&pTabList->a[j]
2b6ab 3b 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; j<pTabList->nS
2b6ac 72 63 3b 20 6a 2b 2b 2c 20 70 54 61 62 49 74 65  rc; j++, pTabIte
2b6ad 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  m++){.      int 
2b6ae 64 6f 4e 6f 74 52 65 6f 72 64 65 72 3b 20 20 2f  doNotReorder;  /
2b6af 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 74  * True if this t
2b6b0 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  able should not 
2b6b1 62 65 20 72 65 6f 72 64 65 72 65 64 20 2a 2f 0a  be reordered */.
2b6b2 20 20 20 20 20 20 57 68 65 72 65 43 6f 73 74 20        WhereCost 
2b6b3 73 43 6f 73 74 3b 20 20 20 2f 2a 20 43 6f 73 74  sCost;   /* Cost
2b6b4 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
2b6b5 6d 20 62 65 73 74 49 6e 64 65 78 28 29 20 2a 2f  m bestIndex() */
2b6b6 0a 0a 20 20 20 20 20 20 64 6f 4e 6f 74 52 65 6f  ..      doNotReo
2b6b7 72 64 65 72 20 3d 20 20 28 70 54 61 62 49 74 65  rder =  (pTabIte
2b6b8 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a  m->jointype & (J
2b6b9 54 5f 4c 45 46 54 7c 4a 54 5f 43 52 4f 53 53 29  T_LEFT|JT_CROSS)
2b6ba 29 21 3d 30 3b 0a 20 20 20 20 20 20 69 66 28 20  )!=0;.      if( 
2b6bb 6f 6e 63 65 20 26 26 20 64 6f 4e 6f 74 52 65 6f  once && doNotReo
2b6bc 72 64 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20  rder ) break;.  
2b6bd 20 20 20 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28      m = getMask(
2b6be 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 49 74  pMaskSet, pTabIt
2b6bf 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20  em->iCursor);.  
2b6c0 20 20 20 20 69 66 28 20 28 6d 20 26 20 6e 6f 74      if( (m & not
2b6c1 52 65 61 64 79 29 3d 3d 30 20 29 7b 0a 20 20 20  Ready)==0 ){.   
2b6c2 20 20 20 20 20 69 66 28 20 6a 3d 3d 69 46 72 6f       if( j==iFro
2b6c3 6d 20 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20 20 20  m ) iFrom++;.   
2b6c4 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2b6c5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
2b6c6 65 72 74 28 20 70 54 61 62 49 74 65 6d 2d 3e 70  ert( pTabItem->p
2b6c7 54 61 62 20 29 3b 0a 23 69 66 6e 64 65 66 20 53  Tab );.#ifndef S
2b6c8 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2b6c9 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 69 66  ALTABLE.      if
2b6ca 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
2b6cb 49 74 65 6d 2d 3e 70 54 61 62 29 20 29 7b 0a 20  Item->pTab) ){. 
2b6cc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
2b6cd 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62  ndex_info *pVtab
2b6ce 49 64 78 3b 20 2f 2a 20 43 75 72 72 65 6e 74 20  Idx; /* Current 
2b6cf 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20 2a 2f  virtual index */
2b6d0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b6d1 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70  _index_info **pp
2b6d2 49 64 78 49 6e 66 6f 20 3d 20 26 70 57 49 6e 66  IdxInfo = &pWInf
2b6d3 6f 2d 3e 61 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f  o->a[j].pIdxInfo
2b6d4 3b 0a 20 20 20 20 20 20 20 20 73 43 6f 73 74 2e  ;.        sCost.
2b6d5 72 43 6f 73 74 20 3d 20 62 65 73 74 56 69 72 74  rCost = bestVirt
2b6d6 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ualIndex(pParse,
2b6d7 20 70 57 43 2c 20 70 54 61 62 49 74 65 6d 2c 20   pWC, pTabItem, 
2b6d8 6e 6f 74 52 65 61 64 79 2c 0a 20 20 20 20 20 20  notReady,.      
2b6d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b6da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b6db 20 70 70 4f 72 64 65 72 42 79 20 3f 20 2a 70 70   ppOrderBy ? *pp
2b6dc 4f 72 64 65 72 42 79 20 3a 20 30 2c 20 69 3d 3d  OrderBy : 0, i==
2b6dd 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
2b6de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b6df 20 20 20 20 20 20 20 20 20 20 70 70 49 64 78 49            ppIdxI
2b6e0 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 73 43  nfo);.        sC
2b6e1 6f 73 74 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  ost.plan.wsFlags
2b6e2 20 3d 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c   = WHERE_VIRTUAL
2b6e3 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 20 20 73  TABLE;.        s
2b6e4 43 6f 73 74 2e 70 6c 61 6e 2e 75 2e 70 56 74 61  Cost.plan.u.pVta
2b6e5 62 49 64 78 20 3d 20 70 56 74 61 62 49 64 78 20  bIdx = pVtabIdx 
2b6e6 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20 20  = *ppIdxInfo;.  
2b6e7 20 20 20 20 20 20 69 66 28 20 70 56 74 61 62 49        if( pVtabI
2b6e8 64 78 20 26 26 20 70 56 74 61 62 49 64 78 2d 3e  dx && pVtabIdx->
2b6e9 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20  orderByConsumed 
2b6ea 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 43 6f  ){.          sCo
2b6eb 73 74 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  st.plan.wsFlags 
2b6ec 3d 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  = WHERE_VIRTUALT
2b6ed 41 42 4c 45 20 7c 20 57 48 45 52 45 5f 4f 52 44  ABLE | WHERE_ORD
2b6ee 45 52 42 59 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ERBY;.        }.
2b6ef 20 20 20 20 20 20 20 20 73 43 6f 73 74 2e 70 6c          sCost.pl
2b6f0 61 6e 2e 6e 45 71 20 3d 20 30 3b 0a 20 20 20 20  an.nEq = 0;.    
2b6f1 20 20 20 20 69 66 28 20 28 53 51 4c 49 54 45 5f      if( (SQLITE_
2b6f2 42 49 47 5f 44 42 4c 2f 32 2e 30 29 3c 73 43 6f  BIG_DBL/2.0)<sCo
2b6f3 73 74 2e 72 43 6f 73 74 20 29 7b 0a 20 20 20 20  st.rCost ){.    
2b6f4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 73        /* The cos
2b6f5 74 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  t is not allowed
2b6f6 20 74 6f 20 62 65 20 6c 61 72 67 65 72 20 74 68   to be larger th
2b6f7 61 6e 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42  an SQLITE_BIG_DB
2b6f8 4c 20 28 74 68 65 0a 20 20 20 20 20 20 20 20 20  L (the.         
2b6f9 20 2a 2a 20 69 6e 69 74 61 6c 20 76 61 6c 75 65   ** inital value
2b6fa 20 6f 66 20 6c 6f 77 65 73 74 43 6f 73 74 20 69   of lowestCost i
2b6fb 6e 20 74 68 69 73 20 6c 6f 6f 70 2e 20 49 66 20  n this loop. If 
2b6fc 69 74 20 69 73 2c 20 74 68 65 6e 0a 20 20 20 20  it is, then.    
2b6fd 20 20 20 20 20 20 2a 2a 20 74 68 65 20 28 63 6f        ** the (co
2b6fe 73 74 3c 6c 6f 77 65 73 74 43 6f 73 74 29 20 74  st<lowestCost) t
2b6ff 65 73 74 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e  est below will n
2b700 65 76 65 72 20 62 65 20 74 72 75 65 2e 0a 20 20  ever be true..  
2b701 20 20 20 20 20 20 20 20 2a 2f 20 0a 20 20 20 20          */ .    
2b702 20 20 20 20 20 20 73 43 6f 73 74 2e 72 43 6f 73        sCost.rCos
2b703 74 20 3d 20 28 53 51 4c 49 54 45 5f 42 49 47 5f  t = (SQLITE_BIG_
2b704 44 42 4c 2f 32 2e 30 29 3b 0a 20 20 20 20 20 20  DBL/2.0);.      
2b705 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20    }.      }else 
2b706 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a  .#endif.      {.
2b707 20 20 20 20 20 20 20 20 62 65 73 74 49 6e 64 65          bestInde
2b708 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70  x(pParse, pWC, p
2b709 54 61 62 49 74 65 6d 2c 20 6e 6f 74 52 65 61 64  TabItem, notRead
2b70a 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y,.             
2b70b 20 20 20 20 20 28 69 3d 3d 30 20 26 26 20 70 70       (i==0 && pp
2b70c 4f 72 64 65 72 42 79 29 20 3f 20 2a 70 70 4f 72  OrderBy) ? *ppOr
2b70d 64 65 72 42 79 20 3a 20 30 2c 20 26 73 43 6f 73  derBy : 0, &sCos
2b70e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
2b70f 20 20 69 66 28 20 6f 6e 63 65 3d 3d 30 20 7c 7c    if( once==0 ||
2b710 20 73 43 6f 73 74 2e 72 43 6f 73 74 3c 62 65 73   sCost.rCost<bes
2b711 74 50 6c 61 6e 2e 72 43 6f 73 74 20 29 7b 0a 20  tPlan.rCost ){. 
2b712 20 20 20 20 20 20 20 6f 6e 63 65 20 3d 20 31 3b         once = 1;
2b713 0a 20 20 20 20 20 20 20 20 62 65 73 74 50 6c 61  .        bestPla
2b714 6e 20 3d 20 73 43 6f 73 74 3b 0a 20 20 20 20 20  n = sCost;.     
2b715 20 20 20 62 65 73 74 4a 20 3d 20 6a 3b 0a 20 20     bestJ = j;.  
2b716 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2b717 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29 20 62  doNotReorder ) b
2b718 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2b719 61 73 73 65 72 74 28 20 6f 6e 63 65 20 29 3b 0a  assert( once );.
2b71a 20 20 20 20 61 73 73 65 72 74 28 20 6e 6f 74 52      assert( notR
2b71b 65 61 64 79 20 26 20 67 65 74 4d 61 73 6b 28 70  eady & getMask(p
2b71c 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73  MaskSet, pTabLis
2b71d 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75 72  t->a[bestJ].iCur
2b71e 73 6f 72 29 20 29 3b 0a 20 20 20 20 57 48 45 52  sor) );.    WHER
2b71f 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74  ETRACE(("*** Opt
2b720 69 6d 69 7a 65 72 20 73 65 6c 65 63 74 73 20 74  imizer selects t
2b721 61 62 6c 65 20 25 64 20 66 6f 72 20 6c 6f 6f 70  able %d for loop
2b722 20 25 64 5c 6e 22 2c 20 62 65 73 74 4a 2c 0a 20   %d\n", bestJ,. 
2b723 20 20 20 20 20 20 20 20 20 20 70 4c 65 76 65 6c            pLevel
2b724 2d 70 57 49 6e 66 6f 2d 3e 61 29 29 3b 0a 20 20  -pWInfo->a));.  
2b725 20 20 69 66 28 20 28 62 65 73 74 50 6c 61 6e 2e    if( (bestPlan.
2b726 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2b727 48 45 52 45 5f 4f 52 44 45 52 42 59 29 21 3d 30  HERE_ORDERBY)!=0
2b728 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 4f 72 64   ){.      *ppOrd
2b729 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  erBy = 0;.    }.
2b72a 20 20 20 20 61 6e 64 46 6c 61 67 73 20 26 3d 20      andFlags &= 
2b72b 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73  bestPlan.plan.ws
2b72c 46 6c 61 67 73 3b 0a 20 20 20 20 70 4c 65 76 65  Flags;.    pLeve
2b72d 6c 2d 3e 70 6c 61 6e 20 3d 20 62 65 73 74 50 6c  l->plan = bestPl
2b72e 61 6e 2e 70 6c 61 6e 3b 0a 20 20 20 20 69 66 28  an.plan;.    if(
2b72f 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77   bestPlan.plan.w
2b730 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
2b731 4e 44 45 58 45 44 20 29 7b 0a 20 20 20 20 20 20  NDEXED ){.      
2b732 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 20  pLevel->iIdxCur 
2b733 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2b734 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2b735 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43     pLevel->iIdxC
2b736 75 72 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  ur = -1;.    }. 
2b737 20 20 20 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e     notReady &= ~
2b738 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
2b739 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65  , pTabList->a[be
2b73a 73 74 4a 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20  stJ].iCursor);. 
2b73b 20 20 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d     pLevel->iFrom
2b73c 20 3d 20 62 65 73 74 4a 3b 0a 0a 20 20 20 20 2f   = bestJ;..    /
2b73d 2a 20 43 68 65 63 6b 20 74 68 61 74 20 69 66 20  * Check that if 
2b73e 74 68 65 20 74 61 62 6c 65 20 73 63 61 6e 6e 65  the table scanne
2b73f 64 20 62 79 20 74 68 69 73 20 6c 6f 6f 70 20 69  d by this loop i
2b740 74 65 72 61 74 69 6f 6e 20 68 61 64 20 61 6e 0a  teration had an.
2b741 20 20 20 20 2a 2a 20 49 4e 44 45 58 45 44 20 42      ** INDEXED B
2b742 59 20 63 6c 61 75 73 65 20 61 74 74 61 63 68 65  Y clause attache
2b743 64 20 74 6f 20 69 74 2c 20 74 68 61 74 20 74 68  d to it, that th
2b744 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 69 73  e named index is
2b745 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 73   being.    ** us
2b746 65 64 20 66 6f 72 20 74 68 65 20 73 63 61 6e 2e  ed for the scan.
2b747 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 71 75   If not, then qu
2b748 65 72 79 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  ery compilation 
2b749 68 61 73 20 66 61 69 6c 65 64 2e 0a 20 20 20 20  has failed..    
2b74a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  ** Return an err
2b74b 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  or..    */.    p
2b74c 49 64 78 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e  Idx = pTabList->
2b74d 61 5b 62 65 73 74 4a 5d 2e 70 49 6e 64 65 78 3b  a[bestJ].pIndex;
2b74e 0a 20 20 20 20 69 66 28 20 70 49 64 78 20 29 7b  .    if( pIdx ){
2b74f 0a 20 20 20 20 20 20 69 66 28 20 28 62 65 73 74  .      if( (best
2b750 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  Plan.plan.wsFlag
2b751 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45  s & WHERE_INDEXE
2b752 44 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  D)==0 ){.       
2b753 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2b754 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
2b755 20 75 73 65 20 69 6e 64 65 78 3a 20 25 73 22 2c   use index: %s",
2b756 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pIdx->zName);. 
2b757 20 20 20 20 20 20 20 67 6f 74 6f 20 77 68 65 72         goto wher
2b758 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 20  eBeginError;.   
2b759 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2b75a 20 20 2f 2a 20 49 66 20 61 6e 20 49 4e 44 45 58    /* If an INDEX
2b75b 45 44 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  ED BY clause is 
2b75c 75 73 65 64 2c 20 74 68 65 20 62 65 73 74 49 6e  used, the bestIn
2b75d 64 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  dex() function i
2b75e 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 75 61  s.        ** gua
2b75f 72 61 6e 74 65 65 64 20 74 6f 20 66 69 6e 64 20  ranteed to find 
2b760 74 68 65 20 69 6e 64 65 78 20 73 70 65 63 69 66  the index specif
2b761 69 65 64 20 69 6e 20 74 68 65 20 49 4e 44 45 58  ied in the INDEX
2b762 45 44 20 42 59 20 63 6c 61 75 73 65 0a 20 20 20  ED BY clause.   
2b763 20 20 20 20 20 2a 2a 20 69 66 20 69 74 20 66 69       ** if it fi
2b764 6e 64 20 61 6e 20 69 6e 64 65 78 20 61 74 20 61  nd an index at a
2b765 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  ll. */.        a
2b766 73 73 65 72 74 28 20 62 65 73 74 50 6c 61 6e 2e  ssert( bestPlan.
2b767 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 70 49 64  plan.u.pIdx==pId
2b768 78 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  x );.      }.   
2b769 20 7d 0a 20 20 7d 0a 20 20 57 48 45 52 45 54 52   }.  }.  WHERETR
2b76a 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69  ACE(("*** Optimi
2b76b 7a 65 72 20 46 69 6e 69 73 68 65 64 20 2a 2a 2a  zer Finished ***
2b76c 5c 6e 22 29 29 3b 0a 20 20 69 66 28 20 64 62 2d  \n"));.  if( db-
2b76d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2b76e 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42  .    goto whereB
2b76f 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a  eginError;.  }..
2b770 20 20 2f 2a 20 49 66 20 74 68 65 20 74 6f 74 61    /* If the tota
2b771 6c 20 71 75 65 72 79 20 6f 6e 6c 79 20 73 65 6c  l query only sel
2b772 65 63 74 73 20 61 20 73 69 6e 67 6c 65 20 72 6f  ects a single ro
2b773 77 2c 20 74 68 65 6e 20 74 68 65 20 4f 52 44 45  w, then the ORDE
2b774 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65  R BY.  ** clause
2b775 20 69 73 20 69 72 72 65 6c 65 76 61 6e 74 2e 0a   is irrelevant..
2b776 20 20 2a 2f 0a 20 20 69 66 28 20 28 61 6e 64 46    */.  if( (andF
2b777 6c 61 67 73 20 26 20 57 48 45 52 45 5f 55 4e 49  lags & WHERE_UNI
2b778 51 55 45 29 21 3d 30 20 26 26 20 70 70 4f 72 64  QUE)!=0 && ppOrd
2b779 65 72 42 79 20 29 7b 0a 20 20 20 20 2a 70 70 4f  erBy ){.    *ppO
2b77a 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d 0a  rderBy = 0;.  }.
2b77b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 6c  .  /* If the cal
2b77c 6c 65 72 20 69 73 20 61 6e 20 55 50 44 41 54 45  ler is an UPDATE
2b77d 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65   or DELETE state
2b77e 6d 65 6e 74 20 74 68 61 74 20 69 73 20 72 65 71  ment that is req
2b77f 75 65 73 74 69 6e 67 0a 20 20 2a 2a 20 74 6f 20  uesting.  ** to 
2b780 75 73 65 20 61 20 6f 6e 65 2d 70 61 73 73 20 61  use a one-pass a
2b781 6c 67 6f 72 69 74 68 6d 2c 20 64 65 74 65 72 6d  lgorithm, determ
2b782 69 6e 65 20 69 66 20 74 68 69 73 20 69 73 20 61  ine if this is a
2b783 70 70 72 6f 70 72 69 61 74 65 2e 0a 20 20 2a 2a  ppropriate..  **
2b784 20 54 68 65 20 6f 6e 65 2d 70 61 73 73 20 61 6c   The one-pass al
2b785 67 6f 72 69 74 68 6d 20 6f 6e 6c 79 20 77 6f 72  gorithm only wor
2b786 6b 73 20 69 66 20 74 68 65 20 57 48 45 52 45 20  ks if the WHERE 
2b787 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e  clause constrain
2b788 74 73 0a 20 20 2a 2a 20 74 68 65 20 73 74 61 74  ts.  ** the stat
2b789 65 6d 65 6e 74 20 74 6f 20 75 70 64 61 74 65 20  ement to update 
2b78a 61 20 73 69 6e 67 6c 65 20 72 6f 77 2e 0a 20 20  a single row..  
2b78b 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 77 63  */.  assert( (wc
2b78c 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
2b78d 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44  _ONEPASS_DESIRED
2b78e 29 3d 3d 30 20 7c 7c 20 70 57 49 6e 66 6f 2d 3e  )==0 || pWInfo->
2b78f 6e 4c 65 76 65 6c 3d 3d 31 20 29 3b 0a 20 20 69  nLevel==1 );.  i
2b790 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26  f( (wctrlFlags &
2b791 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44   WHERE_ONEPASS_D
2b792 45 53 49 52 45 44 29 21 3d 30 20 26 26 20 28 61  ESIRED)!=0 && (a
2b793 6e 64 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  ndFlags & WHERE_
2b794 55 4e 49 51 55 45 29 21 3d 30 20 29 7b 0a 20 20  UNIQUE)!=0 ){.  
2b795 20 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50    pWInfo->okOneP
2b796 61 73 73 20 3d 20 31 3b 0a 20 20 20 20 70 57 49  ass = 1;.    pWI
2b797 6e 66 6f 2d 3e 61 5b 30 5d 2e 70 6c 61 6e 2e 77  nfo->a[0].plan.w
2b798 73 46 6c 61 67 73 20 26 3d 20 7e 57 48 45 52 45  sFlags &= ~WHERE
2b799 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a  _IDX_ONLY;.  }..
2b79a 20 20 2f 2a 20 4f 70 65 6e 20 61 6c 6c 20 74 61    /* Open all ta
2b79b 62 6c 65 73 20 69 6e 20 74 68 65 20 70 54 61 62  bles in the pTab
2b79c 4c 69 73 74 20 61 6e 64 20 61 6e 79 20 69 6e 64  List and any ind
2b79d 69 63 65 73 20 73 65 6c 65 63 74 65 64 20 66 6f  ices selected fo
2b79e 72 0a 20 20 2a 2a 20 73 65 61 72 63 68 69 6e 67  r.  ** searching
2b79f 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a 20   those tables.. 
2b7a0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43 6f 64   */.  sqlite3Cod
2b7a1 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
2b7a2 61 72 73 65 2c 20 2d 31 29 3b 20 2f 2a 20 49 6e  arse, -1); /* In
2b7a3 73 65 72 74 20 74 68 65 20 63 6f 6f 6b 69 65 20  sert the cookie 
2b7a4 76 65 72 69 66 69 65 72 20 47 6f 74 6f 20 2a 2f  verifier Goto */
2b7a5 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76  .  for(i=0, pLev
2b7a6 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c  el=pWInfo->a; i<
2b7a7 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
2b7a8 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a  i++, pLevel++){.
2b7a9 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
2b7aa 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
2b7ab 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74   open */.    int
2b7ac 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a   iDb;         /*
2b7ad 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61   Index of databa
2b7ae 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61  se containing ta
2b7af 62 6c 65 2f 69 6e 64 65 78 20 2a 2f 0a 0a 23 69  ble/index */..#i
2b7b0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2b7b1 54 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 69 66  T_EXPLAIN.    if
2b7b2 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69  ( pParse->explai
2b7b3 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 63 68  n==2 ){.      ch
2b7b4 61 72 20 2a 7a 4d 73 67 3b 0a 20 20 20 20 20 20  ar *zMsg;.      
2b7b5 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2b7b6 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54  tem *pItem = &pT
2b7b7 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c  abList->a[pLevel
2b7b8 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 20 20  ->iFrom];.      
2b7b9 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50  zMsg = sqlite3MP
2b7ba 72 69 6e 74 66 28 64 62 2c 20 22 54 41 42 4c 45  rintf(db, "TABLE
2b7bb 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61   %s", pItem->zNa
2b7bc 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  me);.      if( p
2b7bd 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a  Item->zAlias ){.
2b7be 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73          zMsg = s
2b7bf 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
2b7c0 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 41 53 20  b, zMsg, "%s AS 
2b7c1 25 73 22 2c 20 7a 4d 73 67 2c 20 70 49 74 65 6d  %s", zMsg, pItem
2b7c2 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 20  ->zAlias);.     
2b7c3 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 70 4c   }.      if( (pL
2b7c4 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2b7c5 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58  gs & WHERE_INDEX
2b7c6 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ED)!=0 ){.      
2b7c7 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
2b7c8 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73  MAppendf(db, zMs
2b7c9 67 2c 20 22 25 73 20 57 49 54 48 20 49 4e 44 45  g, "%s WITH INDE
2b7ca 58 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20  X %s",.         
2b7cb 20 20 7a 4d 73 67 2c 20 70 4c 65 76 65 6c 2d 3e    zMsg, pLevel->
2b7cc 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61  plan.u.pIdx->zNa
2b7cd 6d 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  me);.      }else
2b7ce 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61   if( pLevel->pla
2b7cf 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2b7d0 45 5f 4d 55 4c 54 49 5f 4f 52 20 29 7b 0a 20 20  E_MULTI_OR ){.  
2b7d1 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c        zMsg = sql
2b7d2 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c  ite3MAppendf(db,
2b7d3 20 7a 4d 73 67 2c 20 22 25 73 20 56 49 41 20 4d   zMsg, "%s VIA M
2b7d4 55 4c 54 49 2d 49 4e 44 45 58 20 55 4e 49 4f 4e  ULTI-INDEX UNION
2b7d5 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20  ", zMsg);.      
2b7d6 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c  }else if( pLevel
2b7d7 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2b7d8 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51   (WHERE_ROWID_EQ
2b7d9 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e  |WHERE_ROWID_RAN
2b7da 47 45 29 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  GE) ){.        z
2b7db 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70  Msg = sqlite3MAp
2b7dc 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20  pendf(db, zMsg, 
2b7dd 22 25 73 20 55 53 49 4e 47 20 50 52 49 4d 41 52  "%s USING PRIMAR
2b7de 59 20 4b 45 59 22 2c 20 7a 4d 73 67 29 3b 0a 20  Y KEY", zMsg);. 
2b7df 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53       }.#ifndef S
2b7e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2b7e1 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 65 6c  ALTABLE.      el
2b7e2 73 65 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e  se if( (pLevel->
2b7e3 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2b7e4 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c  HERE_VIRTUALTABL
2b7e5 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  E)!=0 ){.       
2b7e6 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2b7e7 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 20 3d 20  nfo *pVtabIdx = 
2b7e8 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
2b7e9 56 74 61 62 49 64 78 3b 0a 20 20 20 20 20 20 20  VtabIdx;.       
2b7ea 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
2b7eb 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
2b7ec 2c 20 22 25 73 20 56 49 52 54 55 41 4c 20 54 41  , "%s VIRTUAL TA
2b7ed 42 4c 45 20 49 4e 44 45 58 20 25 64 3a 25 73 22  BLE INDEX %d:%s"
2b7ee 2c 20 7a 4d 73 67 2c 0a 20 20 20 20 20 20 20 20  , zMsg,.        
2b7ef 20 20 20 20 20 20 20 20 20 20 20 20 70 56 74 61              pVta
2b7f0 62 49 64 78 2d 3e 69 64 78 4e 75 6d 2c 20 70 56  bIdx->idxNum, pV
2b7f1 74 61 62 49 64 78 2d 3e 69 64 78 53 74 72 29 3b  tabIdx->idxStr);
2b7f2 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
2b7f3 20 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c        if( pLevel
2b7f4 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2b7f5 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 20 29   WHERE_ORDERBY )
2b7f6 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d  {.        zMsg =
2b7f7 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66   sqlite3MAppendf
2b7f8 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 4f  (db, zMsg, "%s O
2b7f9 52 44 45 52 20 42 59 22 2c 20 7a 4d 73 67 29 3b  RDER BY", zMsg);
2b7fa 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
2b7fb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2b7fc 28 76 2c 20 4f 50 5f 45 78 70 6c 61 69 6e 2c 20  (v, OP_Explain, 
2b7fd 69 2c 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  i, pLevel->iFrom
2b7fe 2c 20 30 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59  , 0, zMsg, P4_DY
2b7ff 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a 23 65  NAMIC);.    }.#e
2b800 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2b801 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20  MIT_EXPLAIN */. 
2b802 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70     pTabItem = &p
2b803 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65  TabList->a[pLeve
2b804 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 70  l->iFrom];.    p
2b805 54 61 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e  Tab = pTabItem->
2b806 70 54 61 62 3b 0a 20 20 20 20 69 44 62 20 3d 20  pTab;.    iDb = 
2b807 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2b808 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
2b809 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
2b80a 0a 20 20 20 20 69 66 28 20 28 70 54 61 62 2d 3e  .    if( (pTab->
2b80b 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70  tabFlags & TF_Ep
2b80c 68 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70  hemeral)!=0 || p
2b80d 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63  Tab->pSelect ) c
2b80e 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66  ontinue;.#ifndef
2b80f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2b810 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66  TUALTABLE.    if
2b811 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ( (pLevel->plan.
2b812 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2b813 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30  VIRTUALTABLE)!=0
2b814 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43   ){.      int iC
2b815 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69  ur = pTabItem->i
2b816 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71  Cursor;.      sq
2b817 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2b818 76 2c 20 4f 50 5f 56 4f 70 65 6e 2c 20 69 43 75  v, OP_VOpen, iCu
2b819 72 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20  r, 0, 0,.       
2b81a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b81b 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54   (const char*)pT
2b81c 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f 56 54  ab->pVtab, P4_VT
2b81d 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23  AB);.    }else.#
2b81e 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 28 70  endif.    if( (p
2b81f 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2b820 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f  ags & WHERE_IDX_
2b821 4f 4e 4c 59 29 3d 3d 30 0a 20 20 20 20 20 20 20  ONLY)==0.       
2b822 20 20 26 26 20 28 77 63 74 72 6c 46 6c 61 67 73    && (wctrlFlags
2b823 20 26 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50   & WHERE_OMIT_OP
2b824 45 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  EN)==0 ){.      
2b825 69 6e 74 20 6f 70 20 3d 20 70 57 49 6e 66 6f 2d  int op = pWInfo-
2b826 3e 6f 6b 4f 6e 65 50 61 73 73 20 3f 20 4f 50 5f  >okOnePass ? OP_
2b827 4f 70 65 6e 57 72 69 74 65 20 3a 20 4f 50 5f 4f  OpenWrite : OP_O
2b828 70 65 6e 52 65 61 64 3b 0a 20 20 20 20 20 20 73  penRead;.      s
2b829 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
2b82a 70 50 61 72 73 65 2c 20 70 54 61 62 49 74 65 6d  pParse, pTabItem
2b82b 2d 3e 69 43 75 72 73 6f 72 2c 20 69 44 62 2c 20  ->iCursor, iDb, 
2b82c 70 54 61 62 2c 20 6f 70 29 3b 0a 20 20 20 20 20  pTab, op);.     
2b82d 20 69 66 28 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b   if( !pWInfo->ok
2b82e 4f 6e 65 50 61 73 73 20 26 26 20 70 54 61 62 2d  OnePass && pTab-
2b82f 3e 6e 43 6f 6c 3c 42 4d 53 20 29 7b 0a 20 20 20  >nCol<BMS ){.   
2b830 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20 3d       Bitmask b =
2b831 20 70 54 61 62 49 74 65 6d 2d 3e 63 6f 6c 55 73   pTabItem->colUs
2b832 65 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ed;.        int 
2b833 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66  n = 0;.        f
2b834 6f 72 28 3b 20 62 3b 20 62 3d 62 3e 3e 31 2c 20  or(; b; b=b>>1, 
2b835 6e 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 73  n++){}.        s
2b836 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2b837 50 32 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62  P2(v, sqlite3Vdb
2b838 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d  eCurrentAddr(v)-
2b839 32 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 61  2, n);.        a
2b83a 73 73 65 72 74 28 20 6e 3c 3d 70 54 61 62 2d 3e  ssert( n<=pTab->
2b83b 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 7d 0a  nCol );.      }.
2b83c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2b83d 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
2b83e 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70  k(pParse, iDb, p
2b83f 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54  Tab->tnum, 0, pT
2b840 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
2b841 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 54  }.    pLevel->iT
2b842 61 62 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d  abCur = pTabItem
2b843 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 69  ->iCursor;.    i
2b844 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
2b845 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2b846 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a  _INDEXED)!=0 ){.
2b847 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 78        Index *pIx
2b848 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
2b849 75 2e 70 49 64 78 3b 0a 20 20 20 20 20 20 4b 65  u.pIdx;.      Ke
2b84a 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71  yInfo *pKey = sq
2b84b 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
2b84c 6f 28 70 50 61 72 73 65 2c 20 70 49 78 29 3b 0a  o(pParse, pIx);.
2b84d 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 43 75        int iIdxCu
2b84e 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  r = pLevel->iIdx
2b84f 43 75 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  Cur;.      asser
2b850 74 28 20 70 49 78 2d 3e 70 53 63 68 65 6d 61 3d  t( pIx->pSchema=
2b851 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
2b852 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2b853 69 49 64 78 43 75 72 3e 3d 30 20 29 3b 0a 20 20  iIdxCur>=0 );.  
2b854 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b855 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e  ddOp2(v, OP_SetN
2b856 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70 49  umColumns, 0, pI
2b857 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20  x->nColumn+1);. 
2b858 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b859 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65  AddOp4(v, OP_Ope
2b85a 6e 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20  nRead, iIdxCur, 
2b85b 70 49 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a  pIx->tnum, iDb,.
2b85c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b85d 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
2b85e 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Key, P4_KEYINFO_
2b85f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20  HANDOFF);.      
2b860 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2b861 22 25 73 22 2c 20 70 49 78 2d 3e 7a 4e 61 6d 65  "%s", pIx->zName
2b862 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ));.    }.    sq
2b863 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
2b864 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44  chema(pParse, iD
2b865 62 29 3b 0a 20 20 7d 0a 20 20 70 57 49 6e 66 6f  b);.  }.  pWInfo
2b866 2d 3e 69 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  ->iTop = sqlite3
2b867 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2b868 76 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  v);..  /* Genera
2b869 74 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64  te the code to d
2b86a 6f 20 74 68 65 20 73 65 61 72 63 68 2e 20 20 45  o the search.  E
2b86b 61 63 68 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  ach iteration of
2b86c 20 74 68 65 20 66 6f 72 0a 20 20 2a 2a 20 6c 6f   the for.  ** lo
2b86d 6f 70 20 62 65 6c 6f 77 20 67 65 6e 65 72 61 74  op below generat
2b86e 65 73 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69  es code for a si
2b86f 6e 67 6c 65 20 6e 65 73 74 65 64 20 6c 6f 6f 70  ngle nested loop
2b870 20 6f 66 20 74 68 65 20 56 4d 0a 20 20 2a 2a 20   of the VM.  ** 
2b871 70 72 6f 67 72 61 6d 2e 0a 20 20 2a 2f 0a 20 20  program..  */.  
2b872 6e 6f 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74  notReady = ~(Bit
2b873 6d 61 73 6b 29 30 3b 0a 20 20 66 6f 72 28 69 3d  mask)0;.  for(i=
2b874 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  0; i<pTabList->n
2b875 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e  Src; i++){.    n
2b876 6f 74 52 65 61 64 79 20 3d 20 63 6f 64 65 4f 6e  otReady = codeOn
2b877 65 4c 6f 6f 70 53 74 61 72 74 28 70 57 49 6e 66  eLoopStart(pWInf
2b878 6f 2c 20 69 2c 20 77 63 74 72 6c 46 6c 61 67 73  o, i, wctrlFlags
2b879 2c 20 6e 6f 74 52 65 61 64 79 29 3b 0a 20 20 20  , notReady);.   
2b87a 20 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e   pWInfo->iContin
2b87b 75 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b 69  ue = pWInfo->a[i
2b87c 5d 2e 61 64 64 72 43 6f 6e 74 3b 0a 20 20 7d 0a  ].addrCont;.  }.
2b87d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
2b87e 45 53 54 20 20 2f 2a 20 46 6f 72 20 74 65 73 74  EST  /* For test
2b87f 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
2b880 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20  g use only */.  
2b881 2f 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65  /* Record in the
2b882 20 71 75 65 72 79 20 70 6c 61 6e 20 69 6e 66 6f   query plan info
2b883 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
2b884 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 0a  e current table.
2b885 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64    ** and the ind
2b886 65 78 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  ex used to acces
2b887 73 20 69 74 20 28 69 66 20 61 6e 79 29 2e 20 20  s it (if any).  
2b888 49 66 20 74 68 65 20 74 61 62 6c 65 20 69 74 73  If the table its
2b889 65 6c 66 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20  elf.  ** is not 
2b88a 75 73 65 64 2c 20 69 74 73 20 6e 61 6d 65 20 69  used, its name i
2b88b 73 20 6a 75 73 74 20 27 7b 7d 27 2e 20 20 49 66  s just '{}'.  If
2b88c 20 6e 6f 20 69 6e 64 65 78 20 69 73 20 75 73 65   no index is use
2b88d 64 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78  d.  ** the index
2b88e 20 69 73 20 6c 69 73 74 65 64 20 61 73 20 22 7b   is listed as "{
2b88f 7d 22 2e 20 20 49 66 20 74 68 65 20 70 72 69 6d  }".  If the prim
2b890 61 72 79 20 6b 65 79 20 69 73 20 75 73 65 64 20  ary key is used 
2b891 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6e  the.  ** index n
2b892 61 6d 65 20 69 73 20 27 2a 27 2e 0a 20 20 2a 2f  ame is '*'..  */
2b893 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54  .  for(i=0; i<pT
2b894 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
2b895 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b  +){.    char *z;
2b896 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20  .    int n;.    
2b897 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f  pLevel = &pWInfo
2b898 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 54 61 62  ->a[i];.    pTab
2b899 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74  Item = &pTabList
2b89a 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f  ->a[pLevel->iFro
2b89b 6d 5d 3b 0a 20 20 20 20 7a 20 3d 20 70 54 61 62  m];.    z = pTab
2b89c 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20  Item->zAlias;.  
2b89d 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 7a 20 3d    if( z==0 ) z =
2b89e 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2d   pTabItem->pTab-
2b89f 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 20 3d 20  >zName;.    n = 
2b8a0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2b8a1 7a 29 3b 0a 20 20 20 20 69 66 28 20 6e 2b 6e 51  z);.    if( n+nQ
2b8a2 50 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71  Plan < sizeof(sq
2b8a3 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
2b8a4 29 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 69 66  )-10 ){.      if
2b8a5 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
2b8a6 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
2b8a7 44 58 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20  DX_ONLY ){.     
2b8a8 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
2b8a9 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
2b8aa 50 6c 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32 29 3b  Plan], "{}", 2);
2b8ab 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  .        nQPlan 
2b8ac 2b 3d 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  += 2;.      }els
2b8ad 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  e{.        memcp
2b8ae 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79  y(&sqlite3_query
2b8af 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 7a  _plan[nQPlan], z
2b8b0 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51  , n);.        nQ
2b8b1 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20  Plan += n;.     
2b8b2 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2b8b3 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
2b8b4 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20  an++] = ' ';.   
2b8b5 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28   }.    testcase(
2b8b6 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2b8b7 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f  Flags & WHERE_RO
2b8b8 57 49 44 5f 45 51 20 29 3b 0a 20 20 20 20 74 65  WID_EQ );.    te
2b8b9 73 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e  stcase( pLevel->
2b8ba 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2b8bb 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45  HERE_ROWID_RANGE
2b8bc 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76   );.    if( pLev
2b8bd 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2b8be 20 26 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f   & (WHERE_ROWID_
2b8bf 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52  EQ|WHERE_ROWID_R
2b8c0 41 4e 47 45 29 20 29 7b 0a 20 20 20 20 20 20 6d  ANGE) ){.      m
2b8c1 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71  emcpy(&sqlite3_q
2b8c2 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
2b8c3 5d 2c 20 22 2a 20 22 2c 20 32 29 3b 0a 20 20 20  ], "* ", 2);.   
2b8c4 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a     nQPlan += 2;.
2b8c5 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70      }else if( (p
2b8c6 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2b8c7 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45  ags & WHERE_INDE
2b8c8 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  XED)!=0 ){.     
2b8c9 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
2b8ca 65 6e 33 30 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  en30(pLevel->pla
2b8cb 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29  n.u.pIdx->zName)
2b8cc 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 2b 6e 51  ;.      if( n+nQ
2b8cd 50 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71  Plan < sizeof(sq
2b8ce 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
2b8cf 29 2d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  )-2 ){.        m
2b8d0 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71  emcpy(&sqlite3_q
2b8d1 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
2b8d2 5d 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ], pLevel->plan.
2b8d3 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e  u.pIdx->zName, n
2b8d4 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61  );.        nQPla
2b8d5 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20  n += n;.        
2b8d6 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
2b8d7 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27  an[nQPlan++] = '
2b8d8 20 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   ';.      }.    
2b8d9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
2b8da 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65  cpy(&sqlite3_que
2b8db 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c  ry_plan[nQPlan],
2b8dc 20 22 7b 7d 20 22 2c 20 33 29 3b 0a 20 20 20 20   "{} ", 3);.    
2b8dd 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 33 3b 0a 20    nQPlan += 3;. 
2b8de 20 20 20 7d 0a 20 20 7d 0a 20 20 77 68 69 6c 65     }.  }.  while
2b8df 28 20 6e 51 50 6c 61 6e 3e 30 20 26 26 20 73 71  ( nQPlan>0 && sq
2b8e0 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
2b8e1 5b 6e 51 50 6c 61 6e 2d 31 5d 3d 3d 27 20 27 20  [nQPlan-1]==' ' 
2b8e2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 71  ){.    sqlite3_q
2b8e3 75 65 72 79 5f 70 6c 61 6e 5b 2d 2d 6e 51 50 6c  uery_plan[--nQPl
2b8e4 61 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73  an] = 0;.  }.  s
2b8e5 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
2b8e6 6e 5b 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20  n[nQPlan] = 0;. 
2b8e7 20 6e 51 50 6c 61 6e 20 3d 20 30 3b 0a 23 65 6e   nQPlan = 0;.#en
2b8e8 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45  dif /* SQLITE_TE
2b8e9 53 54 20 2f 2f 20 54 65 73 74 69 6e 67 20 61 6e  ST // Testing an
2b8ea 64 20 64 65 62 75 67 67 69 6e 67 20 75 73 65 20  d debugging use 
2b8eb 6f 6e 6c 79 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65  only */..  /* Re
2b8ec 63 6f 72 64 20 74 68 65 20 63 6f 6e 74 69 6e 75  cord the continu
2b8ed 61 74 69 6f 6e 20 61 64 64 72 65 73 73 20 69 6e  ation address in
2b8ee 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73   the WhereInfo s
2b8ef 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 6e 0a  tructure.  Then.
2b8f0 20 20 2a 2a 20 63 6c 65 61 6e 20 75 70 20 61 6e    ** clean up an
2b8f1 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20  d return..  */. 
2b8f2 20 72 65 74 75 72 6e 20 70 57 49 6e 66 6f 3b 0a   return pWInfo;.
2b8f3 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20  .  /* Jump here 
2b8f4 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20  if malloc fails 
2b8f5 2a 2f 0a 77 68 65 72 65 42 65 67 69 6e 45 72 72  */.whereBeginErr
2b8f6 6f 72 3a 0a 20 20 77 68 65 72 65 49 6e 66 6f 46  or:.  whereInfoF
2b8f7 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b  ree(db, pWInfo);
2b8f8 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
2b8f9 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74  /*.** Generate t
2b8fa 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48  he end of the WH
2b8fb 45 52 45 20 6c 6f 6f 70 2e 20 20 53 65 65 20 63  ERE loop.  See c
2b8fc 6f 6d 6d 65 6e 74 73 20 6f 6e 20 0a 2a 2a 20 73  omments on .** s
2b8fd 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2b8fe 28 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  () for additiona
2b8ff 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
2b900 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2b901 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65   void sqlite3Whe
2b902 72 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 20  reEnd(WhereInfo 
2b903 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 50 61 72 73  *pWInfo){.  Pars
2b904 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 49 6e  e *pParse = pWIn
2b905 66 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20 56 64  fo->pParse;.  Vd
2b906 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2b907 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a  pVdbe;.  int i;.
2b908 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c    WhereLevel *pL
2b909 65 76 65 6c 3b 0a 20 20 53 72 63 4c 69 73 74 20  evel;.  SrcList 
2b90a 2a 70 54 61 62 4c 69 73 74 20 3d 20 70 57 49 6e  *pTabList = pWIn
2b90b 66 6f 2d 3e 70 54 61 62 4c 69 73 74 3b 0a 20 20  fo->pTabList;.  
2b90c 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2b90d 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20  arse->db;..  /* 
2b90e 47 65 6e 65 72 61 74 65 20 6c 6f 6f 70 20 74 65  Generate loop te
2b90f 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 2e 0a  rmination code..
2b910 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78    */.  sqlite3Ex
2b911 70 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 63  prClearColumnCac
2b912 68 65 28 70 50 61 72 73 65 2c 20 2d 31 29 3b 0a  he(pParse, -1);.
2b913 20 20 66 6f 72 28 69 3d 70 54 61 62 4c 69 73 74    for(i=pTabList
2b914 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 30 3b 20  ->nSrc-1; i>=0; 
2b915 69 2d 2d 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c  i--){.    pLevel
2b916 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d   = &pWInfo->a[i]
2b917 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2b918 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2b919 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e   pLevel->addrCon
2b91a 74 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76  t);.    if( pLev
2b91b 65 6c 2d 3e 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20  el->op!=OP_Noop 
2b91c 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2b91d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 70 4c  VdbeAddOp2(v, pL
2b91e 65 76 65 6c 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c  evel->op, pLevel
2b91f 2d 3e 70 31 2c 20 70 4c 65 76 65 6c 2d 3e 70 32  ->p1, pLevel->p2
2b920 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b921 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2b922 70 4c 65 76 65 6c 2d 3e 70 35 29 3b 0a 20 20 20  pLevel->p5);.   
2b923 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65   }.    if( pLeve
2b924 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2b925 26 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20  & WHERE_IN_ABLE 
2b926 26 26 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  && pLevel->u.in.
2b927 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73  nIn>0 ){.      s
2b928 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 2a 70 49  truct InLoop *pI
2b929 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  n;.      int j;.
2b92a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b92b 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2b92c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74   pLevel->addrNxt
2b92d 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70  );.      for(j=p
2b92e 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2c  Level->u.in.nIn,
2b92f 20 70 49 6e 3d 26 70 4c 65 76 65 6c 2d 3e 75 2e   pIn=&pLevel->u.
2b930 69 6e 2e 61 49 6e 4c 6f 6f 70 5b 6a 2d 31 5d 3b  in.aInLoop[j-1];
2b931 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70 49 6e 2d 2d   j>0; j--, pIn--
2b932 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2b933 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2b934 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70  , pIn->addrInTop
2b935 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  +1);.        sql
2b936 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b937 2c 20 4f 50 5f 4e 65 78 74 2c 20 70 49 6e 2d 3e  , OP_Next, pIn->
2b938 69 43 75 72 2c 20 70 49 6e 2d 3e 61 64 64 72 49  iCur, pIn->addrI
2b939 6e 54 6f 70 29 3b 0a 20 20 20 20 20 20 20 20 73  nTop);.        s
2b93a 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
2b93b 72 65 28 76 2c 20 70 49 6e 2d 3e 61 64 64 72 49  re(v, pIn->addrI
2b93c 6e 54 6f 70 2d 31 29 3b 0a 20 20 20 20 20 20 7d  nTop-1);.      }
2b93d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2b93e 46 72 65 65 28 64 62 2c 20 70 4c 65 76 65 6c 2d  Free(db, pLevel-
2b93f 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 29 3b 0a  >u.in.aInLoop);.
2b940 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2b941 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2b942 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64  l(v, pLevel->add
2b943 72 42 72 6b 29 3b 0a 20 20 20 20 69 66 28 20 70  rBrk);.    if( p
2b944 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
2b945 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64   ){.      int ad
2b946 64 72 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d  dr;.      addr =
2b947 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b948 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p1(v, OP_IfPos, 
2b949 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
2b94a 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n);.      sqlite
2b94b 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2b94c 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 54 61 62 4c  P_NullRow, pTabL
2b94d 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f  ist->a[i].iCurso
2b94e 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c  r);.      if( pL
2b94f 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3e 3d 30  evel->iIdxCur>=0
2b950 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2b951 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2b952 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 4c 65   OP_NullRow, pLe
2b953 76 65 6c 2d 3e 69 49 64 78 43 75 72 29 3b 0a 20  vel->iIdxCur);. 
2b954 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
2b955 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b956 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 4c  , OP_Goto, 0, pL
2b957 65 76 65 6c 2d 3e 61 64 64 72 46 69 72 73 74 29  evel->addrFirst)
2b958 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b959 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
2b95a 64 64 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ddr);.    }.  }.
2b95b 0a 20 20 2f 2a 20 54 68 65 20 22 62 72 65 61 6b  .  /* The "break
2b95c 22 20 70 6f 69 6e 74 20 69 73 20 68 65 72 65 2c  " point is here,
2b95d 20 6a 75 73 74 20 70 61 73 74 20 74 68 65 20 65   just past the e
2b95e 6e 64 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20  nd of the outer 
2b95f 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 53 65 74 20 69  loop..  ** Set i
2b960 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  t..  */.  sqlite
2b961 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2b962 6c 28 76 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72  l(v, pWInfo->iBr
2b963 65 61 6b 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73  eak);..  /* Clos
2b964 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 75 72  e all of the cur
2b965 73 6f 72 73 20 74 68 61 74 20 77 65 72 65 20 6f  sors that were o
2b966 70 65 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  pened by sqlite3
2b967 57 68 65 72 65 42 65 67 69 6e 2e 0a 20 20 2a 2f  WhereBegin..  */
2b968 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76  .  for(i=0, pLev
2b969 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c  el=pWInfo->a; i<
2b96a 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
2b96b 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a  i++, pLevel++){.
2b96c 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
2b96d 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65  st_item *pTabIte
2b96e 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61  m = &pTabList->a
2b96f 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b  [pLevel->iFrom];
2b970 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
2b971 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61   = pTabItem->pTa
2b972 62 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  b;.    assert( p
2b973 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 69 66  Tab!=0 );.    if
2b974 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  ( (pTab->tabFlag
2b975 73 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c  s & TF_Ephemeral
2b976 29 21 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70 53  )!=0 || pTab->pS
2b977 65 6c 65 63 74 20 29 20 63 6f 6e 74 69 6e 75 65  elect ) continue
2b978 3b 0a 20 20 20 20 69 66 28 20 28 70 57 49 6e 66  ;.    if( (pWInf
2b979 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20 26 20  o->wctrlFlags & 
2b97a 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45  WHERE_OMIT_CLOSE
2b97b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  )==0 ){.      if
2b97c 28 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65  ( !pWInfo->okOne
2b97d 50 61 73 73 20 26 26 20 28 70 4c 65 76 65 6c 2d  Pass && (pLevel-
2b97e 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2b97f 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d  WHERE_IDX_ONLY)=
2b980 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
2b981 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2b982 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 54 61  v, OP_Close, pTa
2b983 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b  bItem->iCursor);
2b984 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2b985 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
2b986 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2b987 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a  _INDEXED)!=0 ){.
2b988 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2b989 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2b98a 43 6c 6f 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69  Close, pLevel->i
2b98b 49 64 78 43 75 72 29 3b 0a 20 20 20 20 20 20 7d  IdxCur);.      }
2b98c 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
2b98d 66 20 74 68 69 73 20 73 63 61 6e 20 75 73 65 73  f this scan uses
2b98e 20 61 6e 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20   an index, make 
2b98f 63 6f 64 65 20 73 75 62 73 74 69 74 75 74 69 6f  code substitutio
2b990 6e 73 20 74 6f 20 72 65 61 64 20 64 61 74 61 0a  ns to read data.
2b991 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20      ** from the 
2b992 69 6e 64 65 78 20 69 6e 20 70 72 65 66 65 72 65  index in prefere
2b993 6e 63 65 20 74 6f 20 74 68 65 20 74 61 62 6c 65  nce to the table
2b994 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69  . Sometimes, thi
2b995 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74  s means.    ** t
2b996 68 65 20 74 61 62 6c 65 20 6e 65 65 64 20 6e 65  he table need ne
2b997 76 65 72 20 62 65 20 72 65 61 64 20 66 72 6f 6d  ver be read from
2b998 2e 20 54 68 69 73 20 69 73 20 61 20 70 65 72 66  . This is a perf
2b999 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20  ormance boost,. 
2b99a 20 20 20 2a 2a 20 61 73 20 74 68 65 20 76 64 62     ** as the vdb
2b99b 65 20 6c 65 76 65 6c 20 77 61 69 74 73 20 75 6e  e level waits un
2b99c 74 69 6c 20 74 68 65 20 74 61 62 6c 65 20 69 73  til the table is
2b99d 20 72 65 61 64 20 62 65 66 6f 72 65 20 61 63 74   read before act
2b99e 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65  ually.    ** see
2b99f 6b 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 63  king the table c
2b9a0 75 72 73 6f 72 20 74 6f 20 74 68 65 20 72 65 63  ursor to the rec
2b9a1 6f 72 64 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ord correspondin
2b9a2 67 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  g to the current
2b9a3 0a 20 20 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e  .    ** position
2b9a4 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 20   in the index.. 
2b9a5 20 20 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61     ** .    ** Ca
2b9a6 6c 6c 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20  lls to the code 
2b9a7 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 62 65 74  generator in bet
2b9a8 77 65 65 6e 20 73 71 6c 69 74 65 33 57 68 65 72  ween sqlite3Wher
2b9a9 65 42 65 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a  eBegin and.    *
2b9aa 2a 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e  * sqlite3WhereEn
2b9ab 64 20 77 69 6c 6c 20 68 61 76 65 20 63 72 65 61  d will have crea
2b9ac 74 65 64 20 63 6f 64 65 20 74 68 61 74 20 72 65  ted code that re
2b9ad 66 65 72 65 6e 63 65 73 20 74 68 65 20 74 61 62  ferences the tab
2b9ae 6c 65 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74  le.    ** direct
2b9af 6c 79 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73  ly.  This loop s
2b9b0 63 61 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f  cans all that co
2b9b1 64 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f  de looking for o
2b9b2 70 63 6f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68  pcodes.    ** th
2b9b3 61 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  at reference the
2b9b4 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65   table and conve
2b9b5 72 74 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70  rts them into op
2b9b6 63 6f 64 65 73 20 74 68 61 74 0a 20 20 20 20 2a  codes that.    *
2b9b7 2a 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20  * reference the 
2b9b8 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20  index..    */.  
2b9b9 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70    if( (pLevel->p
2b9ba 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2b9bb 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20  ERE_INDEXED)!=0 
2b9bc 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 2c 20  ){.      int k, 
2b9bd 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20 20 20 56  j, last;.      V
2b9be 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20  dbeOp *pOp;.    
2b9bf 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20    Index *pIdx = 
2b9c0 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
2b9c1 49 64 78 3b 0a 20 20 20 20 20 20 69 6e 74 20 75  Idx;.      int u
2b9c2 73 65 49 6e 64 65 78 4f 6e 6c 79 20 3d 20 70 4c  seIndexOnly = pL
2b9c3 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2b9c4 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f  gs & WHERE_IDX_O
2b9c5 4e 4c 59 3b 0a 0a 20 20 20 20 20 20 61 73 73 65  NLY;..      asse
2b9c6 72 74 28 20 70 49 64 78 21 3d 30 20 29 3b 0a 20  rt( pIdx!=0 );. 
2b9c7 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69 74       pOp = sqlit
2b9c8 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 70  e3VdbeGetOp(v, p
2b9c9 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20 20  WInfo->iTop);.  
2b9ca 20 20 20 20 6c 61 73 74 20 3d 20 73 71 6c 69 74      last = sqlit
2b9cb 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2b9cc 72 28 76 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  r(v);.      for(
2b9cd 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b 20  k=pWInfo->iTop; 
2b9ce 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f 70  k<last; k++, pOp
2b9cf 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
2b9d0 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65 76 65 6c   pOp->p1!=pLevel
2b9d1 2d 3e 69 54 61 62 43 75 72 20 29 20 63 6f 6e 74  ->iTabCur ) cont
2b9d2 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66  inue;.        if
2b9d3 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
2b9d4 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  P_Column ){.    
2b9d5 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
2b9d6 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pIdx->nColumn; 
2b9d7 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
2b9d8 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 70    if( pOp->p2==p
2b9d9 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
2b9da 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2b9db 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b 0a 20    pOp->p2 = j;. 
2b9dc 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 70               pOp
2b9dd 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e 69  ->p1 = pLevel->i
2b9de 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20 20 20  IdxCur;.        
2b9df 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b9e0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2b9e1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2b9e2 20 61 73 73 65 72 74 28 21 75 73 65 49 6e 64 65   assert(!useInde
2b9e3 78 4f 6e 6c 79 20 7c 7c 20 6a 3c 70 49 64 78 2d  xOnly || j<pIdx-
2b9e4 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20  >nColumn);.     
2b9e5 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70     }else if( pOp
2b9e6 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77  ->opcode==OP_Row
2b9e7 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  id ){.          
2b9e8 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c  pOp->p1 = pLevel
2b9e9 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20  ->iIdxCur;.     
2b9ea 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65       pOp->opcode
2b9eb 20 3d 20 4f 50 5f 49 64 78 52 6f 77 69 64 3b 0a   = OP_IdxRowid;.
2b9ec 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
2b9ed 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
2b9ee 50 5f 4e 75 6c 6c 52 6f 77 20 26 26 20 75 73 65  P_NullRow && use
2b9ef 49 6e 64 65 78 4f 6e 6c 79 20 29 7b 0a 20 20 20  IndexOnly ){.   
2b9f0 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f         pOp->opco
2b9f1 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20  de = OP_Noop;.  
2b9f2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2b9f3 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2b9f4 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70 0a 20 20  Final cleanup.  
2b9f5 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66 6f 46 72  */.  whereInfoFr
2b9f6 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a  ee(db, pWInfo);.
2b9f7 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a    return;.}../**
2b9f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2b9f9 20 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a   of where.c ****
2b9fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2b9fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2b9fe 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 63 20  in file parse.c 
2b9ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ba00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ba01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
2ba02 44 72 69 76 65 72 20 74 65 6d 70 6c 61 74 65 20  Driver template 
2ba03 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70 61  for the LEMON pa
2ba04 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a  rser generator..
2ba05 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2ba06 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2ba07 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2ba08 20 63 6f 64 65 2e 0a 2a 2f 0a 2f 2a 20 46 69 72   code..*/./* Fir
2ba09 73 74 20 6f 66 66 2c 20 63 6f 64 65 20 69 73 20  st off, code is 
2ba0a 69 6e 63 6c 75 64 65 64 20 74 68 61 74 20 66 6f  included that fo
2ba0b 6c 6c 6f 77 73 20 74 68 65 20 22 69 6e 63 6c 75  llows the "inclu
2ba0c 64 65 22 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a  de" declaration.
2ba0d 2a 2a 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20  ** in the input 
2ba0e 67 72 61 6d 6d 61 72 20 66 69 6c 65 2e 20 2a 2f  grammar file. */
2ba0f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  .../*.** An inst
2ba10 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
2ba11 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66  ucture holds inf
2ba12 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
2ba13 68 65 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c 61 75  he.** LIMIT clau
2ba14 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  se of a SELECT s
2ba15 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72  tatement..*/.str
2ba16 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 7b 0a 20  uct LimitVal {. 
2ba17 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b 20 20   Expr *pLimit;  
2ba18 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 65    /* The LIMIT e
2ba19 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c  xpression.  NULL
2ba1a 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
2ba1b 6c 69 6d 69 74 20 2a 2f 0a 20 20 45 78 70 72 20  limit */.  Expr 
2ba1c 2a 70 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 54  *pOffset;   /* T
2ba1d 68 65 20 4f 46 46 53 45 54 20 65 78 70 72 65 73  he OFFSET expres
2ba1e 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66 20 74  sion.  NULL if t
2ba1f 68 65 72 65 20 69 73 20 6e 6f 6e 65 20 2a 2f 0a  here is none */.
2ba20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
2ba21 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
2ba22 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20  ructure is used 
2ba23 74 6f 20 73 74 6f 72 65 20 74 68 65 20 4c 49 4b  to store the LIK
2ba24 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20 4e 4f 54 20  E,.** GLOB, NOT 
2ba25 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f 54 20 47 4c  LIKE, and NOT GL
2ba26 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2f  OB operators..*/
2ba27 0a 73 74 72 75 63 74 20 4c 69 6b 65 4f 70 20 7b  .struct LikeOp {
2ba28 0a 20 20 54 6f 6b 65 6e 20 65 4f 70 65 72 61 74  .  Token eOperat
2ba29 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b 65 22 20 6f  or;  /* "like" o
2ba2a 72 20 22 67 6c 6f 62 22 20 6f 72 20 22 72 65 67  r "glob" or "reg
2ba2b 65 78 70 22 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  exp" */.  int no
2ba2c 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  t;         /* Tr
2ba2d 75 65 20 69 66 20 74 68 65 20 4e 4f 54 20 6b 65  ue if the NOT ke
2ba2e 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74  yword is present
2ba2f 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
2ba30 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
2ba31 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
2ba32 74 75 72 65 20 64 65 73 63 72 69 62 65 73 20 74  ture describes t
2ba33 68 65 20 65 76 65 6e 74 20 6f 66 20 61 0a 2a 2a  he event of a.**
2ba34 20 54 52 49 47 47 45 52 2e 20 20 22 61 22 20 69   TRIGGER.  "a" i
2ba35 73 20 74 68 65 20 65 76 65 6e 74 20 74 79 70 65  s the event type
2ba36 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 50 44 41  , one of TK_UPDA
2ba37 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 0a 2a  TE, TK_INSERT,.*
2ba38 2a 20 54 4b 5f 44 45 4c 45 54 45 2c 20 6f 72 20  * TK_DELETE, or 
2ba39 54 4b 5f 49 4e 53 54 45 41 44 2e 20 20 49 66 20  TK_INSTEAD.  If 
2ba3a 74 68 65 20 65 76 65 6e 74 20 69 73 20 6f 66 20  the event is of 
2ba3b 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
2ba3c 20 20 20 20 55 50 44 41 54 45 20 4f 4e 20 28 61      UPDATE ON (a
2ba3d 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20 54 68 65 6e  ,b,c).**.** Then
2ba3e 20 74 68 65 20 22 62 22 20 49 64 4c 69 73 74 20   the "b" IdList 
2ba3f 72 65 63 6f 72 64 73 20 74 68 65 20 6c 69 73 74  records the list
2ba40 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f 0a 73 74 72   "a,b,c"..*/.str
2ba41 75 63 74 20 54 72 69 67 45 76 65 6e 74 20 7b 20  uct TrigEvent { 
2ba42 69 6e 74 20 61 3b 20 49 64 4c 69 73 74 20 2a 20  int a; IdList * 
2ba43 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  b; };../*.** An 
2ba44 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
2ba45 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
2ba46 20 74 68 65 20 41 54 54 41 43 48 20 6b 65 79 20   the ATTACH key 
2ba47 61 6e 64 20 74 68 65 20 6b 65 79 20 74 79 70 65  and the key type
2ba48 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 74 74 61  ..*/.struct Atta
2ba49 63 68 4b 65 79 20 7b 20 69 6e 74 20 74 79 70 65  chKey { int type
2ba4a 3b 20 20 54 6f 6b 65 6e 20 6b 65 79 3b 20 7d 3b  ;  Token key; };
2ba4b 0a 0a 2f 2a 20 4e 65 78 74 20 69 73 20 61 6c 6c  ../* Next is all
2ba4c 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 2c 20 69   token values, i
2ba4d 6e 20 61 20 66 6f 72 6d 20 73 75 69 74 61 62 6c  n a form suitabl
2ba4e 65 20 66 6f 72 20 75 73 65 20 62 79 20 6d 61 6b  e for use by mak
2ba4f 65 68 65 61 64 65 72 73 2e 0a 2a 2a 20 54 68 69  eheaders..** Thi
2ba50 73 20 73 65 63 74 69 6f 6e 20 77 69 6c 6c 20 62  s section will b
2ba51 65 20 6e 75 6c 6c 20 75 6e 6c 65 73 73 20 6c 65  e null unless le
2ba52 6d 6f 6e 20 69 73 20 72 75 6e 20 77 69 74 68 20  mon is run with 
2ba53 74 68 65 20 2d 6d 20 73 77 69 74 63 68 2e 0a 2a  the -m switch..*
2ba54 2f 0a 2f 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63  /./* .** These c
2ba55 6f 6e 73 74 61 6e 74 73 20 28 61 6c 6c 20 67 65  onstants (all ge
2ba56 6e 65 72 61 74 65 64 20 61 75 74 6f 6d 61 74 69  nerated automati
2ba57 63 61 6c 6c 79 20 62 79 20 74 68 65 20 70 61 72  cally by the par
2ba58 73 65 72 20 67 65 6e 65 72 61 74 6f 72 29 0a 2a  ser generator).*
2ba59 2a 20 73 70 65 63 69 66 79 20 74 68 65 20 76 61  * specify the va
2ba5a 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 74  rious kinds of t
2ba5b 6f 6b 65 6e 73 20 28 74 65 72 6d 69 6e 61 6c 73  okens (terminals
2ba5c 29 20 74 68 61 74 20 74 68 65 20 70 61 72 73 65  ) that the parse
2ba5d 72 0a 2a 2a 20 75 6e 64 65 72 73 74 61 6e 64 73  r.** understands
2ba5e 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 79  . .**.** Each sy
2ba5f 6d 62 6f 6c 20 68 65 72 65 20 69 73 20 61 20 74  mbol here is a t
2ba60 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 20 69  erminal symbol i
2ba61 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a  n the grammar..*
2ba62 2f 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  /./* Make sure t
2ba63 68 65 20 49 4e 54 45 52 46 41 43 45 20 6d 61 63  he INTERFACE mac
2ba64 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  ro is defined..*
2ba65 2f 0a 23 69 66 6e 64 65 66 20 49 4e 54 45 52 46  /.#ifndef INTERF
2ba66 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 54  ACE.# define INT
2ba67 45 52 46 41 43 45 20 31 0a 23 65 6e 64 69 66 0a  ERFACE 1.#endif.
2ba68 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 68 69 6e  /* The next thin
2ba69 67 20 69 6e 63 6c 75 64 65 64 20 69 73 20 73 65  g included is se
2ba6a 72 69 65 73 20 6f 66 20 64 65 66 69 6e 65 73 20  ries of defines 
2ba6b 77 68 69 63 68 20 63 6f 6e 74 72 6f 6c 0a 2a 2a  which control.**
2ba6c 20 76 61 72 69 6f 75 73 20 61 73 70 65 63 74 73   various aspects
2ba6d 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61 74 65   of the generate
2ba6e 64 20 70 61 72 73 65 72 2e 0a 2a 2a 20 20 20 20  d parser..**    
2ba6f 59 59 43 4f 44 45 54 59 50 45 20 20 20 20 20 20  YYCODETYPE      
2ba70 20 20 20 69 73 20 74 68 65 20 64 61 74 61 20 74     is the data t
2ba71 79 70 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f  ype used for sto
2ba72 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a  ring terminal.**
2ba73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba74 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65         and nonte
2ba75 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20  rminal numbers. 
2ba76 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22   "unsigned char"
2ba77 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
2ba78 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65               use
2ba79 64 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66  d if there are f
2ba7a 65 77 65 72 20 74 68 61 6e 20 32 35 30 20 74 65  ewer than 250 te
2ba7b 72 6d 69 6e 61 6c 73 0a 2a 2a 20 20 20 20 20 20  rminals.**      
2ba7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba7d 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c   and nonterminal
2ba7e 73 2e 20 20 22 69 6e 74 22 20 69 73 20 75 73 65  s.  "int" is use
2ba7f 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20  d otherwise..** 
2ba80 20 20 20 59 59 4e 4f 43 4f 44 45 20 20 20 20 20     YYNOCODE     
2ba81 20 20 20 20 20 20 69 73 20 61 20 6e 75 6d 62 65        is a numbe
2ba82 72 20 6f 66 20 74 79 70 65 20 59 59 43 4f 44 45  r of type YYCODE
2ba83 54 59 50 45 20 77 68 69 63 68 20 63 6f 72 72 65  TYPE which corre
2ba84 73 70 6f 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20  sponds.**       
2ba85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba86 74 6f 20 6e 6f 20 6c 65 67 61 6c 20 74 65 72 6d  to no legal term
2ba87 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69  inal or nontermi
2ba88 6e 61 6c 20 6e 75 6d 62 65 72 2e 20 20 54 68 69  nal number.  Thi
2ba89 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
2ba8a 20 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65             numbe
2ba8b 72 20 69 73 20 75 73 65 64 20 74 6f 20 66 69 6c  r is used to fil
2ba8c 6c 20 69 6e 20 65 6d 70 74 79 20 73 6c 6f 74 73  l in empty slots
2ba8d 20 6f 66 20 74 68 65 20 68 61 73 68 20 0a 2a 2a   of the hash .**
2ba8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba8f 20 20 20 20 20 20 20 74 61 62 6c 65 2e 0a 2a 2a         table..**
2ba90 20 20 20 20 59 59 46 41 4c 4c 42 41 43 4b 20 20      YYFALLBACK  
2ba91 20 20 20 20 20 20 20 49 66 20 64 65 66 69 6e 65         If define
2ba92 64 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65  d, this indicate
2ba93 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f  s that one or mo
2ba94 72 65 20 74 6f 6b 65 6e 73 0a 2a 2a 20 20 20 20  re tokens.**    
2ba95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba96 20 20 20 68 61 76 65 20 66 61 6c 6c 2d 62 61 63     have fall-bac
2ba97 6b 20 76 61 6c 75 65 73 20 77 68 69 63 68 20 73  k values which s
2ba98 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 66  hould be used if
2ba99 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
2ba9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 72                or
2ba9b 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20  iginal value of 
2ba9c 74 68 65 20 74 6f 6b 65 6e 20 77 69 6c 6c 20 6e  the token will n
2ba9d 6f 74 20 70 61 72 73 65 2e 0a 2a 2a 20 20 20 20  ot parse..**    
2ba9e 59 59 41 43 54 49 4f 4e 54 59 50 45 20 20 20 20  YYACTIONTYPE    
2ba9f 20 20 20 69 73 20 74 68 65 20 64 61 74 61 20 74     is the data t
2baa0 79 70 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f  ype used for sto
2baa1 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a  ring terminal.**
2baa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2baa3 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65         and nonte
2baa4 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20  rminal numbers. 
2baa5 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22   "unsigned char"
2baa6 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
2baa7 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65               use
2baa8 64 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66  d if there are f
2baa9 65 77 65 72 20 74 68 61 6e 20 32 35 30 20 72 75  ewer than 250 ru
2baaa 6c 65 73 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  les and.**      
2baab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2baac 20 73 74 61 74 65 73 20 63 6f 6d 62 69 6e 65 64   states combined
2baad 2e 20 20 22 69 6e 74 22 20 69 73 20 75 73 65 64  .  "int" is used
2baae 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20   otherwise..**  
2baaf 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54    sqlite3ParserT
2bab0 4f 4b 45 4e 54 59 50 45 20 20 20 20 20 69 73 20  OKENTYPE     is 
2bab1 74 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73  the data type us
2bab2 65 64 20 66 6f 72 20 6d 69 6e 6f 72 20 74 6f 6b  ed for minor tok
2bab3 65 6e 73 20 67 69 76 65 6e 20 0a 2a 2a 20 20 20  ens given .**   
2bab4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bab5 20 20 20 20 64 69 72 65 63 74 6c 79 20 74 6f 20      directly to 
2bab6 74 68 65 20 70 61 72 73 65 72 20 66 72 6f 6d 20  the parser from 
2bab7 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a  the tokenizer..*
2bab8 2a 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45  *    YYMINORTYPE
2bab9 20 20 20 20 20 20 20 20 69 73 20 74 68 65 20 64          is the d
2baba 61 74 61 20 74 79 70 65 20 75 73 65 64 20 66 6f  ata type used fo
2babb 72 20 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f 6b 65  r all minor toke
2babc 6e 73 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ns..**          
2babd 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 69               Thi
2babe 73 20 69 73 20 74 79 70 69 63 61 6c 6c 79 20 61  s is typically a
2babf 20 75 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79 20 74   union of many t
2bac0 79 70 65 73 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20  ypes, one of.** 
2bac1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bac2 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 73        which is s
2bac3 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45  qlite3ParserTOKE
2bac4 4e 54 59 50 45 2e 20 20 54 68 65 20 65 6e 74 72  NTYPE.  The entr
2bac5 79 20 69 6e 20 74 68 65 20 75 6e 69 6f 6e 0a 2a  y in the union.*
2bac6 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2bac7 20 20 20 20 20 20 20 20 66 6f 72 20 62 61 73 65          for base
2bac8 20 74 6f 6b 65 6e 73 20 69 73 20 63 61 6c 6c 65   tokens is calle
2bac9 64 20 22 79 79 30 22 2e 0a 2a 2a 20 20 20 20 59  d "yy0"..**    Y
2baca 59 53 54 41 43 4b 44 45 50 54 48 20 20 20 20 20  YSTACKDEPTH     
2bacb 20 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d    is the maximum
2bacc 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61   depth of the pa
2bacd 72 73 65 72 27 73 20 73 74 61 63 6b 2e 20 20 49  rser's stack.  I
2bace 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  f.**            
2bacf 20 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20             zero 
2bad0 74 68 65 20 73 74 61 63 6b 20 69 73 20 64 79 6e  the stack is dyn
2bad1 61 6d 69 63 61 6c 6c 79 20 73 69 7a 65 64 20 75  amically sized u
2bad2 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 0a 2a  sing realloc().*
2bad3 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73  *    sqlite3Pars
2bad4 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20  erARG_SDECL     
2bad5 41 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  A static variabl
2bad6 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f  e declaration fo
2bad7 72 20 74 68 65 20 25 65 78 74 72 61 5f 61 72 67  r the %extra_arg
2bad8 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69  ument.**    sqli
2bad9 74 65 33 50 61 72 73 65 72 41 52 47 5f 50 44 45  te3ParserARG_PDE
2bada 43 4c 20 20 20 20 20 41 20 70 61 72 61 6d 65 74  CL     A paramet
2badb 65 72 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 66  er declaration f
2badc 6f 72 20 74 68 65 20 25 65 78 74 72 61 5f 61 72  or the %extra_ar
2badd 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c  gument.**    sql
2bade 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54  ite3ParserARG_ST
2badf 4f 52 45 20 20 20 20 20 43 6f 64 65 20 74 6f 20  ORE     Code to 
2bae0 73 74 6f 72 65 20 25 65 78 74 72 61 5f 61 72 67  store %extra_arg
2bae1 75 6d 65 6e 74 20 69 6e 74 6f 20 79 79 70 50 61  ument into yypPa
2bae2 72 73 65 72 0a 2a 2a 20 20 20 20 73 71 6c 69 74  rser.**    sqlit
2bae3 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43  e3ParserARG_FETC
2bae4 48 20 20 20 20 20 43 6f 64 65 20 74 6f 20 65 78  H     Code to ex
2bae5 74 72 61 63 74 20 25 65 78 74 72 61 5f 61 72 67  tract %extra_arg
2bae6 75 6d 65 6e 74 20 66 72 6f 6d 20 79 79 70 50 61  ument from yypPa
2bae7 72 73 65 72 0a 2a 2a 20 20 20 20 59 59 4e 53 54  rser.**    YYNST
2bae8 41 54 45 20 20 20 20 20 20 20 20 20 20 20 74 68  ATE           th
2bae9 65 20 63 6f 6d 62 69 6e 65 64 20 6e 75 6d 62 65  e combined numbe
2baea 72 20 6f 66 20 73 74 61 74 65 73 2e 0a 2a 2a 20  r of states..** 
2baeb 20 20 20 59 59 4e 52 55 4c 45 20 20 20 20 20 20     YYNRULE      
2baec 20 20 20 20 20 20 74 68 65 20 6e 75 6d 62 65 72        the number
2baed 20 6f 66 20 72 75 6c 65 73 20 69 6e 20 74 68 65   of rules in the
2baee 20 67 72 61 6d 6d 61 72 0a 2a 2a 20 20 20 20 59   grammar.**    Y
2baef 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 20 20 20  YERRORSYMBOL    
2baf0 20 20 69 73 20 74 68 65 20 63 6f 64 65 20 6e 75    is the code nu
2baf1 6d 62 65 72 20 6f 66 20 74 68 65 20 65 72 72 6f  mber of the erro
2baf2 72 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20 6e 6f  r symbol.  If no
2baf3 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
2baf4 20 20 20 20 20 20 20 20 20 20 20 64 65 66 69 6e             defin
2baf5 65 64 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 20 65  ed, then do no e
2baf6 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e  rror processing.
2baf7 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59 43 4f  .*/.#define YYCO
2baf8 44 45 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20  DETYPE unsigned 
2baf9 63 68 61 72 0a 23 64 65 66 69 6e 65 20 59 59 4e  char.#define YYN
2bafa 4f 43 4f 44 45 20 32 35 31 0a 23 64 65 66 69 6e  OCODE 251.#defin
2bafb 65 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 75  e YYACTIONTYPE u
2bafc 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e  nsigned short in
2bafd 74 0a 23 64 65 66 69 6e 65 20 59 59 57 49 4c 44  t.#define YYWILD
2bafe 43 41 52 44 20 36 32 0a 23 64 65 66 69 6e 65 20  CARD 62.#define 
2baff 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b  sqlite3ParserTOK
2bb00 45 4e 54 59 50 45 20 54 6f 6b 65 6e 0a 74 79 70  ENTYPE Token.typ
2bb01 65 64 65 66 20 75 6e 69 6f 6e 20 7b 0a 20 20 69  edef union {.  i
2bb02 6e 74 20 79 79 69 6e 69 74 3b 0a 20 20 73 71 6c  nt yyinit;.  sql
2bb03 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54  ite3ParserTOKENT
2bb04 59 50 45 20 79 79 30 3b 0a 20 20 73 74 72 75 63  YPE yy0;.  struc
2bb05 74 20 4c 69 6d 69 74 56 61 6c 20 79 79 36 34 3b  t LimitVal yy64;
2bb06 0a 20 20 45 78 70 72 2a 20 79 79 31 32 32 3b 0a  .  Expr* yy122;.
2bb07 20 20 53 65 6c 65 63 74 2a 20 79 79 31 35 39 3b    Select* yy159;
2bb08 0a 20 20 49 64 4c 69 73 74 2a 20 79 79 31 38 30  .  IdList* yy180
2bb09 3b 0a 20 20 73 74 72 75 63 74 20 7b 69 6e 74 20  ;.  struct {int 
2bb0a 76 61 6c 75 65 3b 20 69 6e 74 20 6d 61 73 6b 3b  value; int mask;
2bb0b 7d 20 79 79 32 30 37 3b 0a 20 20 73 74 72 75 63  } yy207;.  struc
2bb0c 74 20 4c 69 6b 65 4f 70 20 79 79 33 31 38 3b 0a  t LikeOp yy318;.
2bb0d 20 20 54 72 69 67 67 65 72 53 74 65 70 2a 20 79    TriggerStep* y
2bb0e 79 33 32 37 3b 0a 20 20 53 72 63 4c 69 73 74 2a  y327;.  SrcList*
2bb0f 20 79 79 33 34 37 3b 0a 20 20 69 6e 74 20 79 79   yy347;.  int yy
2bb10 33 39 32 3b 0a 20 20 73 74 72 75 63 74 20 54 72  392;.  struct Tr
2bb11 69 67 45 76 65 6e 74 20 79 79 34 31 30 3b 0a 20  igEvent yy410;. 
2bb12 20 45 78 70 72 4c 69 73 74 2a 20 79 79 34 34 32   ExprList* yy442
2bb13 3b 0a 7d 20 59 59 4d 49 4e 4f 52 54 59 50 45 3b  ;.} YYMINORTYPE;
2bb14 0a 23 69 66 6e 64 65 66 20 59 59 53 54 41 43 4b  .#ifndef YYSTACK
2bb15 44 45 50 54 48 0a 23 64 65 66 69 6e 65 20 59 59  DEPTH.#define YY
2bb16 53 54 41 43 4b 44 45 50 54 48 20 31 30 30 0a 23  STACKDEPTH 100.#
2bb17 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71  endif.#define sq
2bb18 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
2bb19 44 45 43 4c 20 50 61 72 73 65 20 2a 70 50 61 72  DECL Parse *pPar
2bb1a 73 65 3b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  se;.#define sqli
2bb1b 74 65 33 50 61 72 73 65 72 41 52 47 5f 50 44 45  te3ParserARG_PDE
2bb1c 43 4c 20 2c 50 61 72 73 65 20 2a 70 50 61 72 73  CL ,Parse *pPars
2bb1d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2bb1e 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
2bb1f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d   Parse *pParse =
2bb20 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50 61 72   yypParser->pPar
2bb21 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
2bb22 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52  e3ParserARG_STOR
2bb23 45 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50 61  E yypParser->pPa
2bb24 72 73 65 20 3d 20 70 50 61 72 73 65 0a 23 64 65  rse = pParse.#de
2bb25 66 69 6e 65 20 59 59 4e 53 54 41 54 45 20 36 31  fine YYNSTATE 61
2bb26 30 0a 23 64 65 66 69 6e 65 20 59 59 4e 52 55 4c  0.#define YYNRUL
2bb27 45 20 33 31 39 0a 23 64 65 66 69 6e 65 20 59 59  E 319.#define YY
2bb28 46 41 4c 4c 42 41 43 4b 20 31 0a 23 64 65 66 69  FALLBACK 1.#defi
2bb29 6e 65 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 20  ne YY_NO_ACTION 
2bb2a 20 20 20 20 20 28 59 59 4e 53 54 41 54 45 2b 59       (YYNSTATE+Y
2bb2b 59 4e 52 55 4c 45 2b 32 29 0a 23 64 65 66 69 6e  YNRULE+2).#defin
2bb2c 65 20 59 59 5f 41 43 43 45 50 54 5f 41 43 54 49  e YY_ACCEPT_ACTI
2bb2d 4f 4e 20 20 28 59 59 4e 53 54 41 54 45 2b 59 59  ON  (YYNSTATE+YY
2bb2e 4e 52 55 4c 45 2b 31 29 0a 23 64 65 66 69 6e 65  NRULE+1).#define
2bb2f 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49 4f 4e   YY_ERROR_ACTION
2bb30 20 20 20 28 59 59 4e 53 54 41 54 45 2b 59 59 4e     (YYNSTATE+YYN
2bb31 52 55 4c 45 29 0a 0a 2f 2a 20 54 68 65 20 79 79  RULE)../* The yy
2bb32 7a 65 72 6f 6d 69 6e 6f 72 20 63 6f 6e 73 74 61  zerominor consta
2bb33 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 69 6e  nt is used to in
2bb34 69 74 69 61 6c 69 7a 65 20 69 6e 73 74 61 6e 63  itialize instanc
2bb35 65 73 20 6f 66 0a 2a 2a 20 59 59 4d 49 4e 4f 52  es of.** YYMINOR
2bb36 54 59 50 45 20 6f 62 6a 65 63 74 73 20 74 6f 20  TYPE objects to 
2bb37 7a 65 72 6f 2e 20 2a 2f 0a 73 74 61 74 69 63 20  zero. */.static 
2bb38 63 6f 6e 73 74 20 59 59 4d 49 4e 4f 52 54 59 50  const YYMINORTYP
2bb39 45 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 20 3d 20  E yyzerominor = 
2bb3a 7b 20 30 20 7d 3b 0a 0a 0a 2f 2a 20 4e 65 78 74  { 0 };.../* Next
2bb3b 20 61 72 65 20 74 68 65 20 74 61 62 6c 65 73 20   are the tables 
2bb3c 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
2bb3d 65 20 77 68 61 74 20 61 63 74 69 6f 6e 20 74 6f  e what action to
2bb3e 20 74 61 6b 65 20 62 61 73 65 64 20 6f 6e 20 74   take based on t
2bb3f 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 73 74  he.** current st
2bb40 61 74 65 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61  ate and lookahea
2bb41 64 20 74 6f 6b 65 6e 2e 20 20 54 68 65 73 65 20  d token.  These 
2bb42 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 20  tables are used 
2bb43 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20  to implement.** 
2bb44 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 74  functions that t
2bb45 61 6b 65 20 61 20 73 74 61 74 65 20 6e 75 6d 62  ake a state numb
2bb46 65 72 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64  er and lookahead
2bb47 20 76 61 6c 75 65 20 61 6e 64 20 72 65 74 75 72   value and retur
2bb48 6e 20 61 6e 0a 2a 2a 20 61 63 74 69 6f 6e 20 69  n an.** action i
2bb49 6e 74 65 67 65 72 2e 20 20 0a 2a 2a 0a 2a 2a 20  nteger.  .**.** 
2bb4a 53 75 70 70 6f 73 65 20 74 68 65 20 61 63 74 69  Suppose the acti
2bb4b 6f 6e 20 69 6e 74 65 67 65 72 20 69 73 20 4e 2e  on integer is N.
2bb4c 20 20 54 68 65 6e 20 74 68 65 20 61 63 74 69 6f    Then the actio
2bb4d 6e 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  n is determined 
2bb4e 61 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 0a 2a 2a  as.** follows.**
2bb4f 0a 2a 2a 20 20 20 30 20 3c 3d 20 4e 20 3c 20 59  .**   0 <= N < Y
2bb50 59 4e 53 54 41 54 45 20 20 20 20 20 20 20 20 20  YNSTATE         
2bb51 20 20 20 20 20 20 20 20 20 53 68 69 66 74 20 4e           Shift N
2bb52 2e 20 20 54 68 61 74 20 69 73 2c 20 70 75 73 68  .  That is, push
2bb53 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 0a 2a   the lookahead.*
2bb54 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2bb55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb56 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 6f 6e 74         token ont
2bb57 6f 20 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20  o the stack and 
2bb58 67 6f 74 6f 20 73 74 61 74 65 20 4e 2e 0a 2a 2a  goto state N..**
2bb59 0a 2a 2a 20 20 20 59 59 4e 53 54 41 54 45 20 3c  .**   YYNSTATE <
2bb5a 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54 45 2b 59  = N < YYNSTATE+Y
2bb5b 59 4e 52 55 4c 45 20 20 20 52 65 64 75 63 65 20  YNRULE   Reduce 
2bb5c 62 79 20 72 75 6c 65 20 4e 2d 59 59 4e 53 54 41  by rule N-YYNSTA
2bb5d 54 45 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d  TE..**.**   N ==
2bb5e 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c   YYNSTATE+YYNRUL
2bb5f 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 41  E              A
2bb60 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20 68 61   syntax error ha
2bb61 73 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a  s occurred..**.*
2bb62 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54  *   N == YYNSTAT
2bb63 45 2b 59 59 4e 52 55 4c 45 2b 31 20 20 20 20 20  E+YYNRULE+1     
2bb64 20 20 20 20 20 20 20 54 68 65 20 70 61 72 73 65         The parse
2bb65 72 20 61 63 63 65 70 74 73 20 69 74 73 20 69 6e  r accepts its in
2bb66 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d  put..**.**   N =
2bb67 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55  = YYNSTATE+YYNRU
2bb68 4c 45 2b 32 20 20 20 20 20 20 20 20 20 20 20 20  LE+2            
2bb69 4e 6f 20 73 75 63 68 20 61 63 74 69 6f 6e 2e 20  No such action. 
2bb6a 20 44 65 6e 6f 74 65 73 20 75 6e 75 73 65 64 0a   Denotes unused.
2bb6b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2bb6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb6d 20 20 20 20 20 20 20 20 73 6c 6f 74 73 20 69 6e          slots in
2bb6e 20 74 68 65 20 79 79 5f 61 63 74 69 6f 6e 5b 5d   the yy_action[]
2bb6f 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
2bb70 65 20 61 63 74 69 6f 6e 20 74 61 62 6c 65 20 69  e action table i
2bb71 73 20 63 6f 6e 73 74 72 75 63 74 65 64 20 61 73  s constructed as
2bb72 20 61 20 73 69 6e 67 6c 65 20 6c 61 72 67 65 20   a single large 
2bb73 74 61 62 6c 65 20 6e 61 6d 65 64 20 79 79 5f 61  table named yy_a
2bb74 63 74 69 6f 6e 5b 5d 2e 0a 2a 2a 20 47 69 76 65  ction[]..** Give
2bb75 6e 20 73 74 61 74 65 20 53 20 61 6e 64 20 6c 6f  n state S and lo
2bb76 6f 6b 61 68 65 61 64 20 58 2c 20 74 68 65 20 61  okahead X, the a
2bb77 63 74 69 6f 6e 20 69 73 20 63 6f 6d 70 75 74 65  ction is compute
2bb78 64 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  d as.**.**      
2bb79 79 79 5f 61 63 74 69 6f 6e 5b 20 79 79 5f 73 68  yy_action[ yy_sh
2bb7a 69 66 74 5f 6f 66 73 74 5b 53 5d 20 2b 20 58 20  ift_ofst[S] + X 
2bb7b 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69  ].**.** If the i
2bb7c 6e 64 65 78 20 76 61 6c 75 65 20 79 79 5f 73 68  ndex value yy_sh
2bb7d 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58 20 69 73  ift_ofst[S]+X is
2bb7e 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72   out of range or
2bb7f 20 69 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a   if the value.**
2bb80 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 79 79   yy_lookahead[yy
2bb81 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58  _shift_ofst[S]+X
2bb82 5d 20 69 73 20 6e 6f 74 20 65 71 75 61 6c 20 74  ] is not equal t
2bb83 6f 20 58 20 6f 72 20 69 66 20 79 79 5f 73 68 69  o X or if yy_shi
2bb84 66 74 5f 6f 66 73 74 5b 53 5d 0a 2a 2a 20 69 73  ft_ofst[S].** is
2bb85 20 65 71 75 61 6c 20 74 6f 20 59 59 5f 53 48 49   equal to YY_SHI
2bb86 46 54 5f 55 53 45 5f 44 46 4c 54 2c 20 69 74 20  FT_USE_DFLT, it 
2bb87 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 61  means that the a
2bb88 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 69 6e 20  ction is not in 
2bb89 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64  the table.** and
2bb8a 20 74 68 61 74 20 79 79 5f 64 65 66 61 75 6c 74   that yy_default
2bb8b 5b 53 5d 20 73 68 6f 75 6c 64 20 62 65 20 75 73  [S] should be us
2bb8c 65 64 20 69 6e 73 74 65 61 64 2e 20 20 0a 2a 2a  ed instead.  .**
2bb8d 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c 61 20  .** The formula 
2bb8e 61 62 6f 76 65 20 69 73 20 66 6f 72 20 63 6f 6d  above is for com
2bb8f 70 75 74 69 6e 67 20 74 68 65 20 61 63 74 69 6f  puting the actio
2bb90 6e 20 77 68 65 6e 20 74 68 65 20 6c 6f 6f 6b 61  n when the looka
2bb91 68 65 61 64 20 69 73 0a 2a 2a 20 61 20 74 65 72  head is.** a ter
2bb92 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 2e 20 20 49  minal symbol.  I
2bb93 66 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20  f the lookahead 
2bb94 69 73 20 61 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61  is a non-termina
2bb95 6c 20 28 61 73 20 6f 63 63 75 72 73 20 61 66 74  l (as occurs aft
2bb96 65 72 0a 2a 2a 20 61 20 72 65 64 75 63 65 20 61  er.** a reduce a
2bb97 63 74 69 6f 6e 29 20 74 68 65 6e 20 74 68 65 20  ction) then the 
2bb98 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d  yy_reduce_ofst[]
2bb99 20 61 72 72 61 79 20 69 73 20 75 73 65 64 20 69   array is used i
2bb9a 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 68  n place of.** th
2bb9b 65 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b  e yy_shift_ofst[
2bb9c 5d 20 61 72 72 61 79 20 61 6e 64 20 59 59 5f 52  ] array and YY_R
2bb9d 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20 69  EDUCE_USE_DFLT i
2bb9e 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20  s used in place 
2bb9f 6f 66 0a 2a 2a 20 59 59 5f 53 48 49 46 54 5f 55  of.** YY_SHIFT_U
2bba0 53 45 5f 44 46 4c 54 2e 0a 2a 2a 0a 2a 2a 20 54  SE_DFLT..**.** T
2bba1 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
2bba2 20 74 68 65 20 74 61 62 6c 65 73 20 67 65 6e 65   the tables gene
2bba3 72 61 74 65 64 20 69 6e 20 74 68 69 73 20 73 65  rated in this se
2bba4 63 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 79 79  ction:.**.**  yy
2bba5 5f 61 63 74 69 6f 6e 5b 5d 20 20 20 20 20 20 20  _action[]       
2bba6 20 41 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20   A single table 
2bba7 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 61  containing all a
2bba8 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f 6c  ctions..**  yy_l
2bba9 6f 6f 6b 61 68 65 61 64 5b 5d 20 20 20 20 20 41  ookahead[]     A
2bbaa 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   table containin
2bbab 67 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20  g the lookahead 
2bbac 66 6f 72 20 65 61 63 68 20 65 6e 74 72 79 20 69  for each entry i
2bbad 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  n.**            
2bbae 20 20 20 20 20 20 20 20 20 79 79 5f 61 63 74 69           yy_acti
2bbaf 6f 6e 2e 20 20 55 73 65 64 20 74 6f 20 64 65 74  on.  Used to det
2bbb0 65 63 74 20 68 61 73 68 20 63 6f 6c 6c 69 73 69  ect hash collisi
2bbb1 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f 73 68 69 66  ons..**  yy_shif
2bbb2 74 5f 6f 66 73 74 5b 5d 20 20 20 20 46 6f 72 20  t_ofst[]    For 
2bbb3 65 61 63 68 20 73 74 61 74 65 2c 20 74 68 65 20  each state, the 
2bbb4 6f 66 66 73 65 74 20 69 6e 74 6f 20 79 79 5f 61  offset into yy_a
2bbb5 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20 20 20  ction for.**    
2bbb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbb7 20 73 68 69 66 74 69 6e 67 20 74 65 72 6d 69 6e   shifting termin
2bbb8 61 6c 73 2e 0a 2a 2a 20 20 79 79 5f 72 65 64 75  als..**  yy_redu
2bbb9 63 65 5f 6f 66 73 74 5b 5d 20 20 20 46 6f 72 20  ce_ofst[]   For 
2bbba 65 61 63 68 20 73 74 61 74 65 2c 20 74 68 65 20  each state, the 
2bbbb 6f 66 66 73 65 74 20 69 6e 74 6f 20 79 79 5f 61  offset into yy_a
2bbbc 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20 20 20  ction for.**    
2bbbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbbe 20 73 68 69 66 74 69 6e 67 20 6e 6f 6e 2d 74 65   shifting non-te
2bbbf 72 6d 69 6e 61 6c 73 20 61 66 74 65 72 20 61 20  rminals after a 
2bbc0 72 65 64 75 63 65 2e 0a 2a 2a 20 20 79 79 5f 64  reduce..**  yy_d
2bbc1 65 66 61 75 6c 74 5b 5d 20 20 20 20 20 20 20 44  efault[]       D
2bbc2 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 66 6f  efault action fo
2bbc3 72 20 65 61 63 68 20 73 74 61 74 65 2e 0a 2a 2f  r each state..*/
2bbc4 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59  .static const YY
2bbc5 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f 61 63  ACTIONTYPE yy_ac
2bbc6 74 69 6f 6e 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20  tion[] = {. /*  
2bbc7 20 20 20 30 20 2a 2f 20 20 20 33 30 34 2c 20 20     0 */   304,  
2bbc8 39 33 30 2c 20 20 31 32 30 2c 20 20 36 30 39 2c  930,  120,  609,
2bbc9 20 20 20 20 31 2c 20 20 31 37 38 2c 20 20 32 31      1,  178,  21
2bbca 34 2c 20 20 34 33 36 2c 20 20 20 36 32 2c 20 20  4,  436,   62,  
2bbcb 20 36 32 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a   62,. /*    10 *
2bbcc 2f 20 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  /    62,   62,  
2bbcd 32 31 36 2c 20 20 20 36 34 2c 20 20 20 36 34 2c  216,   64,   64,
2bbce 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2bbcf 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 0a 20  5,   65,   66,. 
2bbd0 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 20 36  /*    20 */    6
2bbd1 36 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20  6,   66,   67,  
2bbd2 32 31 36 2c 20 20 34 30 36 2c 20 20 34 30 33 2c  216,  406,  403,
2bbd3 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 20 36    443,  449,   6
2bbd4 39 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 20  9,   64,. /*    
2bbd5 33 30 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36  30 */    64,   6
2bbd6 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  4,   64,   65,  
2bbd7 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c   65,   66,   66,
2bbd8 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31     66,   67,  21
2bbd9 36 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20  6,. /*    40 */ 
2bbda 20 20 34 36 39 2c 20 20 34 36 37 2c 20 20 33 33    469,  467,  33
2bbdb 36 2c 20 20 31 37 34 2c 20 20 20 36 31 2c 20 20  6,  174,   61,  
2bbdc 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bbdd 20 20 34 35 34 2c 20 20 34 35 30 2c 0a 20 2f 2a    454,  450,. /*
2bbde 20 20 20 20 35 30 20 2a 2f 20 20 20 34 35 30 2c      50 */   450,
2bbdf 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
2bbe0 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bbe1 20 36 32 2c 20 20 32 30 30 2c 20 20 20 36 34 2c   62,  200,   64,
2bbe2 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 20 36 30     64,. /*    60
2bbe3 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c   */    64,   64,
2bbe4 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
2bbe5 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bbe6 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bbe7 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20  . /*    70 */   
2bbe8 35 31 30 2c 20 20 33 31 32 2c 20 20 34 33 36 2c  510,  312,  436,
2bbe9 20 20 35 30 39 2c 20 20 34 33 38 2c 20 20 20 38    509,  438,   8
2bbea 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20  3,   64,   64,  
2bbeb 20 36 34 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20   64,   64,. /*  
2bbec 20 20 38 30 20 2a 2f 20 20 20 20 36 35 2c 20 20    80 */    65,  
2bbed 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c   65,   66,   66,
2bbee 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31     66,   67,  21
2bbef 36 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  6,   65,   65,  
2bbf0 20 36 36 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a   66,. /*    90 *
2bbf1 2f 20 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  /    66,   66,  
2bbf2 20 36 37 2c 20 20 32 31 36 2c 20 20 35 31 31 2c   67,  216,  511,
2bbf3 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 33 32    443,  449,  32
2bbf4 35 2c 20 20 34 30 38 2c 20 20 20 35 39 2c 0a 20  5,  408,   59,. 
2bbf5 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 34 36  /*   100 */   46
2bbf6 35 2c 20 20 32 31 38 2c 20 20 20 35 37 2c 20 20  5,  218,   57,  
2bbf7 32 31 33 2c 20 20 34 31 31 2c 20 20 34 39 36 2c  213,  411,  496,
2bbf8 20 20 34 32 38 2c 20 20 34 34 30 2c 20 20 34 34    428,  440,  44
2bbf9 30 2c 20 20 34 34 30 2c 0a 20 2f 2a 20 20 20 31  0,  440,. /*   1
2bbfa 31 30 20 2a 2f 20 20 20 32 30 36 2c 20 20 20 36  10 */   206,   6
2bbfb 37 2c 20 20 32 31 36 2c 20 20 20 36 31 2c 20 20  7,  216,   61,  
2bbfc 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bbfd 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bbfe 30 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20  0,. /*   120 */ 
2bbff 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
2bc00 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bc01 20 36 32 2c 20 20 35 35 32 2c 20 20 20 36 34 2c   62,  552,   64,
2bc02 20 20 20 36 34 2c 20 20 20 36 34 2c 0a 20 2f 2a     64,   64,. /*
2bc03 20 20 20 31 33 30 20 2a 2f 20 20 20 20 36 34 2c     130 */    64,
2bc04 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
2bc05 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bc06 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bc07 20 20 32 32 38 2c 0a 20 2f 2a 20 20 20 31 34 30    228,. /*   140
2bc08 20 2a 2f 20 20 20 31 38 36 2c 20 20 34 36 39 2c   */   186,  469,
2bc09 20 20 35 34 34 2c 20 20 33 31 32 2c 20 20 34 33    544,  312,  43
2bc0a 33 2c 20 20 31 37 30 2c 20 20 31 31 34 2c 20 20  3,  170,  114,  
2bc0b 32 35 36 2c 20 20 33 35 37 2c 20 20 32 36 31 2c  256,  357,  261,
2bc0c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20  . /*   150 */   
2bc0d 33 35 38 2c 20 20 31 38 31 2c 20 20 34 32 35 2c  358,  181,  425,
2bc0e 20 20 20 32 30 2c 20 20 34 32 36 2c 20 20 35 34     20,  426,  54
2bc0f 32 2c 20 20 31 35 33 2c 20 20 20 38 35 2c 20 20  2,  153,   85,  
2bc10 32 36 35 2c 20 20 34 36 35 2c 0a 20 2f 2a 20 20  265,  465,. /*  
2bc11 20 31 36 30 20 2a 2f 20 20 20 32 31 38 2c 20 20   160 */   218,  
2bc12 31 35 30 2c 20 20 31 35 31 2c 20 20 35 33 39 2c  150,  151,  539,
2bc13 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 20 39    443,  449,   9
2bc14 35 2c 20 20 33 31 31 2c 20 20 33 39 34 2c 20 20  5,  311,  394,  
2bc15 34 31 32 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a  412,. /*   170 *
2bc16 2f 20 20 20 34 31 33 2c 20 20 35 31 30 2c 20 20  /   413,  510,  
2bc17 32 37 36 2c 20 20 34 32 37 2c 20 20 34 33 36 2c  276,  427,  436,
2bc18 20 20 34 33 38 2c 20 20 31 35 32 2c 20 20 35 35    438,  152,  55
2bc19 33 2c 20 20 35 34 35 2c 20 20 35 38 39 2c 0a 20  3,  545,  589,. 
2bc1a 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 35 39  /*   180 */   59
2bc1b 30 2c 20 20 35 33 39 2c 20 20 20 36 31 2c 20 20  0,  539,   61,  
2bc1c 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bc1d 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bc1e 30 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20 31  0,   63,. /*   1
2bc1f 39 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20 36  90 */    63,   6
2bc20 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bc21 20 36 32 2c 20 20 34 30 32 2c 20 20 20 36 34 2c   62,  402,   64,
2bc22 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2bc23 34 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20  4,. /*   200 */ 
2bc24 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
2bc25 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bc26 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bc27 20 20 34 34 30 2c 20 20 34 34 30 2c 0a 20 2f 2a    440,  440,. /*
2bc28 20 20 20 32 31 30 20 2a 2f 20 20 20 34 34 30 2c     210 */   440,
2bc29 20 20 32 32 38 2c 20 20 31 30 39 2c 20 20 34 31    228,  109,  41
2bc2a 31 2c 20 20 33 39 39 2c 20 20 35 32 33 2c 20 20  1,  399,  523,  
2bc2b 35 39 33 2c 20 20 33 33 30 2c 20 20 31 31 34 2c  593,  330,  114,
2bc2c 20 20 32 35 36 2c 0a 20 2f 2a 20 20 20 32 32 30    256,. /*   220
2bc2d 20 2a 2f 20 20 20 33 35 37 2c 20 20 32 36 31 2c   */   357,  261,
2bc2e 20 20 33 35 38 2c 20 20 31 38 31 2c 20 20 31 38    358,  181,  18
2bc2f 37 2c 20 20 33 33 30 2c 20 20 34 38 35 2c 20 20  7,  330,  485,  
2bc30 33 35 39 2c 20 20 33 36 32 2c 20 20 33 36 33 2c  359,  362,  363,
2bc31 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20  . /*   230 */   
2bc32 32 36 35 2c 20 20 35 39 33 2c 20 20 32 34 31 2c  265,  593,  241,
2bc33 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 35 39    443,  449,  59
2bc34 32 2c 20 20 35 39 31 2c 20 20 32 34 38 2c 20 20  2,  591,  248,  
2bc35 33 36 34 2c 20 20 34 33 36 2c 0a 20 2f 2a 20 20  364,  436,. /*  
2bc36 20 32 34 30 20 2a 2f 20 20 20 34 33 32 2c 20 20   240 */   432,  
2bc37 20 33 35 2c 20 20 34 39 32 2c 20 20 20 36 36 2c   35,  492,   66,
2bc38 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20 36     66,   66,   6
2bc39 37 2c 20 20 32 31 36 2c 20 20 34 33 32 2c 20 20  7,  216,  432,  
2bc3a 20 34 32 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a   42,. /*   250 *
2bc3b 2f 20 20 20 35 39 32 2c 20 20 20 36 31 2c 20 20  /   592,   61,  
2bc3c 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bc3d 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bc3e 30 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 0a 20  0,   63,   63,. 
2bc3f 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 20 36  /*   260 */    6
2bc40 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bc41 20 36 32 2c 20 20 34 30 31 2c 20 20 20 36 34 2c   62,  401,   64,
2bc42 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2bc43 34 2c 20 20 20 36 35 2c 0a 20 2f 2a 20 20 20 32  4,   65,. /*   2
2bc44 37 30 20 2a 2f 20 20 20 20 36 35 2c 20 20 20 36  70 */    65,   6
2bc45 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bc46 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bc47 20 20 35 37 30 2c 20 20 34 31 32 2c 20 20 34 31    570,  412,  41
2bc48 33 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20  3,. /*   280 */ 
2bc49 20 20 31 38 37 2c 20 20 35 30 31 2c 20 20 33 34    187,  501,  34
2bc4a 34 2c 20 20 33 35 39 2c 20 20 33 36 32 2c 20 20  4,  359,  362,  
2bc4b 33 36 33 2c 20 20 32 31 35 2c 20 20 33 35 34 2c  363,  215,  354,
2bc4c 20 20 33 34 36 2c 20 20 32 32 31 2c 0a 20 2f 2a    346,  221,. /*
2bc4d 20 20 20 32 39 30 20 2a 2f 20 20 20 33 33 30 2c     290 */   330,
2bc4e 20 20 33 34 31 2c 20 20 33 33 30 2c 20 20 20 35    341,  330,   5
2bc4f 36 2c 20 20 33 36 34 2c 20 20 35 36 39 2c 20 20  6,  364,  569,  
2bc50 35 38 38 2c 20 20 32 31 37 2c 20 20 20 36 38 2c  588,  217,   68,
2bc51 20 20 31 35 36 2c 0a 20 2f 2a 20 20 20 33 30 30    156,. /*   300
2bc52 20 2a 2f 20 20 20 20 37 30 2c 20 20 31 35 35 2c   */    70,  155,
2bc53 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 20 36    443,  449,   6
2bc54 38 2c 20 20 31 38 37 2c 20 20 20 37 30 2c 20 20  8,  187,   70,  
2bc55 31 35 35 2c 20 20 33 35 39 2c 20 20 33 36 32 2c  155,  359,  362,
2bc56 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20  . /*   310 */   
2bc57 33 36 33 2c 20 20 33 39 37 2c 20 20 32 31 37 2c  363,  397,  217,
2bc58 20 20 34 33 32 2c 20 20 20 33 35 2c 20 20 34 33    432,   35,  43
2bc59 32 2c 20 20 20 33 36 2c 20 20 31 34 38 2c 20 20  2,   36,  148,  
2bc5a 35 36 39 2c 20 20 33 36 34 2c 0a 20 2f 2a 20 20  569,  364,. /*  
2bc5b 20 33 32 30 20 2a 2f 20 20 20 20 36 31 2c 20 20   320 */    61,  
2bc5c 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bc5d 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bc5e 30 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  0,   63,   63,  
2bc5f 20 36 32 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a   62,. /*   330 *
2bc60 2f 20 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  /    62,   62,  
2bc61 20 36 32 2c 20 20 34 33 33 2c 20 20 20 36 34 2c   62,  433,   64,
2bc62 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2bc63 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 0a 20  4,   65,   65,. 
2bc64 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 20 36  /*   340 */    6
2bc65 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bc66 20 36 37 2c 20 20 32 31 36 2c 20 20 33 38 37 2c   67,  216,  387,
2bc67 20 20 32 38 32 2c 20 20 32 38 31 2c 20 20 33 33    282,  281,  33
2bc68 30 2c 20 20 33 30 34 2c 0a 20 2f 2a 20 20 20 33  0,  304,. /*   3
2bc69 35 30 20 2a 2f 20 20 20 34 37 34 2c 20 20 20 36  50 */   474,   6
2bc6a 38 2c 20 20 34 38 30 2c 20 20 20 37 30 2c 20 20  8,  480,   70,  
2bc6b 31 35 35 2c 20 20 33 34 34 2c 20 20 32 31 34 2c  155,  344,  214,
2bc6c 20 20 31 35 34 2c 20 20 32 39 39 2c 20 20 33 33    154,  299,  33
2bc6d 30 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20  0,. /*   360 */ 
2bc6e 20 20 33 34 33 2c 20 20 34 36 37 2c 20 20 35 34    343,  467,  54
2bc6f 33 2c 20 20 31 37 34 2c 20 20 33 38 34 2c 20 20  3,  174,  384,  
2bc70 34 37 35 2c 20 20 32 35 37 2c 20 20 32 34 37 2c  475,  257,  247,
2bc71 20 20 33 38 37 2c 20 20 32 38 32 2c 0a 20 2f 2a    387,  282,. /*
2bc72 20 20 20 33 37 30 20 2a 2f 20 20 20 32 38 31 2c     370 */   281,
2bc73 20 20 34 33 32 2c 20 20 20 32 38 2c 20 20 34 31    432,   28,  41
2bc74 31 2c 20 20 31 36 30 2c 20 20 34 34 33 2c 20 20  1,  160,  443,  
2bc75 34 34 39 2c 20 20 32 35 38 2c 20 20 34 37 36 2c  449,  258,  476,
2bc76 20 20 32 31 34 2c 0a 20 2f 2a 20 20 20 33 38 30    214,. /*   380
2bc77 20 2a 2f 20 20 20 35 31 36 2c 20 20 34 39 36 2c   */   516,  496,
2bc78 20 20 34 33 32 2c 20 20 20 34 32 2c 20 20 31 39    432,   42,  19
2bc79 38 2c 20 20 34 39 32 2c 20 20 20 36 38 2c 20 20  8,  492,   68,  
2bc7a 31 36 32 2c 20 20 20 37 30 2c 20 20 31 35 35 2c  162,   70,  155,
2bc7b 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20  . /*   390 */   
2bc7c 35 31 37 2c 20 20 34 33 33 2c 20 20 20 37 38 2c  517,  433,   78,
2bc7d 20 20 20 36 31 2c 20 20 20 36 30 2c 20 20 33 30     61,   60,  30
2bc7e 39 2c 20 20 34 35 33 2c 20 20 34 35 34 2c 20 20  9,  453,  454,  
2bc7f 34 35 30 2c 20 20 34 35 30 2c 0a 20 2f 2a 20 20  450,  450,. /*  
2bc80 20 34 30 30 20 2a 2f 20 20 20 20 36 33 2c 20 20   400 */    63,  
2bc81 20 36 33 2c 20 20 20 36 32 2c 20 20 20 36 32 2c   63,   62,   62,
2bc82 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20 35 39     62,   62,  59
2bc83 35 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20  5,   64,   64,  
2bc84 20 36 34 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a   64,. /*   410 *
2bc85 2f 20 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  /    64,   65,  
2bc86 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c   65,   66,   66,
2bc87 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31     66,   67,  21
2bc88 36 2c 20 20 34 33 33 2c 20 20 33 36 37 2c 0a 20  6,  433,  367,. 
2bc89 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 33 34  /*   420 */   34
2bc8a 39 2c 20 20 34 33 33 2c 20 20 33 30 34 2c 20 20  9,  433,  304,  
2bc8b 32 32 30 2c 20 20 32 32 32 2c 20 20 35 34 34 2c  220,  222,  544,
2bc8c 20 20 35 30 35 2c 20 20 33 33 30 2c 20 20 34 36    505,  330,  46
2bc8d 35 2c 20 20 33 33 30 2c 0a 20 2f 2a 20 20 20 34  5,  330,. /*   4
2bc8e 33 30 20 2a 2f 20 20 20 32 33 30 2c 20 20 33 33  30 */   230,  33
2bc8f 30 2c 20 20 32 34 30 2c 20 20 31 36 33 2c 20 20  0,  240,  163,  
2bc90 31 36 31 2c 20 20 35 35 34 2c 20 20 20 32 30 2c  161,  554,   20,
2bc91 20 20 34 33 31 2c 20 20 34 31 32 2c 20 20 34 31    431,  412,  41
2bc92 33 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20  3,. /*   440 */ 
2bc93 20 20 20 20 32 2c 20 20 34 33 30 2c 20 20 33 38      2,  430,  38
2bc94 35 2c 20 20 33 37 35 2c 20 20 34 31 31 2c 20 20  5,  375,  411,  
2bc95 31 39 38 2c 20 20 31 38 32 2c 20 20 32 34 39 2c  198,  182,  249,
2bc96 20 20 34 34 33 2c 20 20 34 34 39 2c 0a 20 2f 2a    443,  449,. /*
2bc97 20 20 20 34 35 30 20 2a 2f 20 20 20 34 33 32 2c     450 */   432,
2bc98 20 20 20 33 35 2c 20 20 34 33 32 2c 20 20 20 35     35,  432,   5
2bc99 30 2c 20 20 34 33 32 2c 20 20 20 35 30 2c 20 20  0,  432,   50,  
2bc9a 33 31 30 2c 20 20 34 36 30 2c 20 20 34 36 31 2c  310,  460,  461,
2bc9b 20 20 20 31 37 2c 0a 20 2f 2a 20 20 20 34 36 30     17,. /*   460
2bc9c 20 2a 2f 20 20 20 32 30 37 2c 20 20 33 33 35 2c   */   207,  335,
2bc9d 20 20 34 36 30 2c 20 20 34 36 31 2c 20 20 33 38    460,  461,  38
2bc9e 38 2c 20 20 20 38 31 2c 20 20 20 36 31 2c 20 20  8,   81,   61,  
2bc9f 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bca0 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20  . /*   470 */   
2bca1 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35 30 2c  454,  450,  450,
2bca2 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
2bca3 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bca4 20 36 32 2c 20 20 34 33 33 2c 0a 20 2f 2a 20 20   62,  433,. /*  
2bca5 20 34 38 30 20 2a 2f 20 20 20 20 36 34 2c 20 20   480 */    64,  
2bca6 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34 2c   64,   64,   64,
2bca7 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
2bca8 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bca9 20 36 37 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a   67,. /*   490 *
2bcaa 2f 20 20 20 32 31 36 2c 20 20 33 30 34 2c 20 20  /   216,  304,  
2bcab 33 34 38 2c 20 20 35 30 34 2c 20 20 34 33 33 2c  348,  504,  433,
2bcac 20 20 35 30 38 2c 20 20 35 33 31 2c 20 20 34 38    508,  531,  48
2bcad 36 2c 20 20 33 32 30 2c 20 20 33 35 33 2c 0a 20  6,  320,  353,. 
2bcae 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 33 32  /*   500 */   32
2bcaf 31 2c 20 20 33 30 36 2c 20 20 34 35 37 2c 20 20  1,  306,  457,  
2bcb0 33 38 35 2c 20 20 20 32 33 2c 20 20 33 33 31 2c  385,   23,  331,
2bcb1 20 20 32 36 35 2c 20 20 34 37 30 2c 20 20 34 31    265,  470,  41
2bcb2 31 2c 20 20 34 31 32 2c 0a 20 2f 2a 20 20 20 35  1,  412,. /*   5
2bcb3 31 30 20 2a 2f 20 20 20 34 31 33 2c 20 20 34 34  10 */   413,  44
2bcb4 34 2c 20 20 34 34 35 2c 20 20 35 35 31 2c 20 20  4,  445,  551,  
2bcb5 35 32 36 2c 20 20 33 30 37 2c 20 20 35 33 32 2c  526,  307,  532,
2bcb6 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 32 31    443,  449,  21
2bcb7 37 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20  7,. /*   520 */ 
2bcb8 20 20 35 35 30 2c 20 20 34 39 36 2c 20 20 34 33    550,  496,  43
2bcb9 32 2c 20 20 20 20 33 2c 20 20 32 31 37 2c 20 20  2,    3,  217,  
2bcba 33 38 31 2c 20 20 36 30 37 2c 20 20 39 32 31 2c  381,  607,  921,
2bcbb 20 20 33 33 33 2c 20 20 39 32 31 2c 0a 20 2f 2a    333,  921,. /*
2bcbc 20 20 20 35 33 30 20 2a 2f 20 20 20 34 35 36 2c     530 */   456,
2bcbd 20 20 34 35 36 2c 20 20 34 34 37 2c 20 20 34 34    456,  447,  44
2bcbe 38 2c 20 20 32 37 36 2c 20 20 20 36 31 2c 20 20  8,  276,   61,  
2bcbf 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bcc0 20 20 34 35 34 2c 0a 20 2f 2a 20 20 20 35 34 30    454,. /*   540
2bcc1 20 2a 2f 20 20 20 34 35 30 2c 20 20 34 35 30 2c   */   450,  450,
2bcc2 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
2bcc3 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bcc4 20 36 32 2c 20 20 34 31 30 2c 20 20 20 36 34 2c   62,  410,   64,
2bcc5 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20  . /*   550 */   
2bcc6 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34 2c   64,   64,   64,
2bcc7 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
2bcc8 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bcc9 20 36 37 2c 20 20 32 31 36 2c 0a 20 2f 2a 20 20   67,  216,. /*  
2bcca 20 35 36 30 20 2a 2f 20 20 20 33 30 34 2c 20 20   560 */   304,  
2bccb 34 34 36 2c 20 20 36 30 37 2c 20 20 39 32 30 2c  446,  607,  920,
2bccc 20 20 35 32 35 2c 20 20 39 32 30 2c 20 20 36 30    525,  920,  60
2bccd 34 2c 20 20 32 36 34 2c 20 20 33 31 34 2c 20 20  4,  264,  314,  
2bcce 34 37 34 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a  474,. /*   570 *
2bccf 2f 20 20 20 34 31 31 2c 20 20 31 32 33 2c 20 20  /   411,  123,  
2bcd0 34 31 31 2c 20 20 34 31 32 2c 20 20 34 31 33 2c  411,  412,  413,
2bcd1 20 20 31 32 34 2c 20 20 32 37 37 2c 20 20 34 38    124,  277,  48
2bcd2 37 2c 20 20 32 33 34 2c 20 20 33 33 33 2c 0a 20  7,  234,  333,. 
2bcd3 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 34 31  /*   580 */   41
2bcd4 31 2c 20 20 34 35 36 2c 20 20 34 35 36 2c 20 20  1,  456,  456,  
2bcd5 33 31 39 2c 20 20 34 37 35 2c 20 20 34 31 31 2c  319,  475,  411,
2bcd6 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 33 33    443,  449,  33
2bcd7 33 2c 20 20 33 37 37 2c 0a 20 2f 2a 20 20 20 35  3,  377,. /*   5
2bcd8 39 30 20 2a 2f 20 20 20 34 35 36 2c 20 20 34 35  90 */   456,  45
2bcd9 36 2c 20 20 32 38 36 2c 20 20 33 33 33 2c 20 20  6,  286,  333,  
2bcda 33 38 30 2c 20 20 34 35 36 2c 20 20 34 35 36 2c  380,  456,  456,
2bcdb 20 20 34 37 36 2c 20 20 31 37 38 2c 20 20 33 34    476,  178,  34
2bcdc 30 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20  0,. /*   600 */ 
2bcdd 20 20 34 33 36 2c 20 20 34 32 30 2c 20 20 36 30    436,  420,  60
2bcde 34 2c 20 20 33 31 35 2c 20 20 20 36 31 2c 20 20  4,  315,   61,  
2bcdf 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bce0 20 20 34 35 34 2c 20 20 34 35 30 2c 0a 20 2f 2a    454,  450,. /*
2bce1 20 20 20 36 31 30 20 2a 2f 20 20 20 34 35 30 2c     610 */   450,
2bce2 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
2bce3 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bce4 20 36 32 2c 20 20 33 33 30 2c 20 20 20 36 34 2c   62,  330,   64,
2bce5 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 36 32 30     64,. /*   620
2bce6 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c   */    64,   64,
2bce7 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
2bce8 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bce9 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bcea 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f 20 20 20  . /*   630 */   
2bceb 32 38 39 2c 20 20 20 20 35 2c 20 20 32 38 37 2c  289,    5,  287,
2bcec 20 20 32 36 38 2c 20 20 34 36 36 2c 20 20 34 31    268,  466,  41
2bced 32 2c 20 20 34 31 33 2c 20 20 34 31 32 2c 20 20  2,  413,  412,  
2bcee 34 31 33 2c 20 20 33 39 36 2c 0a 20 2f 2a 20 20  413,  396,. /*  
2bcef 20 36 34 30 20 2a 2f 20 20 20 34 33 32 2c 20 20   640 */   432,  
2bcf0 20 32 39 2c 20 20 35 30 33 2c 20 20 33 33 30 2c   29,  503,  330,
2bcf1 20 20 31 35 39 2c 20 20 34 31 32 2c 20 20 34 31    159,  412,  41
2bcf2 33 2c 20 20 36 31 30 2c 20 20 34 30 36 2c 20 20  3,  610,  406,  
2bcf3 34 30 33 2c 0a 20 2f 2a 20 20 20 36 35 30 20 2a  403,. /*   650 *
2bcf4 2f 20 20 20 34 31 32 2c 20 20 34 31 33 2c 20 20  /   412,  413,  
2bcf5 34 31 34 2c 20 20 34 31 35 2c 20 20 34 31 36 2c  414,  415,  416,
2bcf6 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 33 33    443,  449,  33
2bcf7 33 2c 20 20 32 31 34 2c 20 20 34 35 36 2c 0a 20  3,  214,  456,. 
2bcf8 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20 20 34 35  /*   660 */   45
2bcf9 36 2c 20 20 34 38 38 2c 20 20 32 37 36 2c 20 20  6,  488,  276,  
2bcfa 34 38 39 2c 20 20 20 32 31 2c 20 20 34 33 36 2c  489,   21,  436,
2bcfb 20 20 34 33 32 2c 20 20 20 32 34 2c 20 20 34 33    432,   24,  43
2bcfc 36 2c 20 20 34 38 37 2c 0a 20 2f 2a 20 20 20 36  6,  487,. /*   6
2bcfd 37 30 20 2a 2f 20 20 20 35 31 34 2c 20 20 35 31  70 */   514,  51
2bcfe 35 2c 20 20 33 39 35 2c 20 20 20 36 31 2c 20 20  5,  395,   61,  
2bcff 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bd00 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bd01 30 2c 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20  0,. /*   680 */ 
2bd02 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
2bd03 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bd04 20 36 32 2c 20 20 33 33 30 2c 20 20 20 36 34 2c   62,  330,   64,
2bd05 20 20 20 36 34 2c 20 20 20 36 34 2c 0a 20 2f 2a     64,   64,. /*
2bd06 20 20 20 36 39 30 20 2a 2f 20 20 20 20 36 34 2c     690 */    64,
2bd07 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
2bd08 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bd09 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bd0a 20 20 35 36 30 2c 0a 20 2f 2a 20 20 20 37 30 30    560,. /*   700
2bd0b 20 2a 2f 20 20 20 33 37 34 2c 20 20 35 36 30 2c   */   374,  560,
2bd0c 20 20 33 35 32 2c 20 20 20 39 34 2c 20 20 35 37    352,   94,  57
2bd0d 38 2c 20 20 33 33 30 2c 20 20 35 36 37 2c 20 20  8,  330,  567,  
2bd0e 35 31 35 2c 20 20 33 33 30 2c 20 20 34 33 32 2c  515,  330,  432,
2bd0f 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20 20  . /*   710 */   
2bd10 20 33 33 2c 20 20 33 33 30 2c 20 20 32 38 38 2c   33,  330,  288,
2bd11 20 20 33 33 30 2c 20 20 35 36 32 2c 20 20 33 33    330,  562,  33
2bd12 30 2c 20 20 35 34 34 2c 20 20 33 33 30 2c 20 20  0,  544,  330,  
2bd13 35 36 31 2c 20 20 31 38 33 2c 0a 20 2f 2a 20 20  561,  183,. /*  
2bd14 20 37 32 30 20 2a 2f 20 20 20 31 38 34 2c 20 20   720 */   184,  
2bd15 31 38 35 2c 20 20 36 30 33 2c 20 20 33 30 33 2c  185,  603,  303,
2bd16 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 36 30    443,  449,  60
2bd17 30 2c 20 20 20 32 30 2c 20 20 34 33 32 2c 20 20  0,   20,  432,  
2bd18 20 35 34 2c 0a 20 2f 2a 20 20 20 37 33 30 20 2a   54,. /*   730 *
2bd19 2f 20 20 20 33 37 36 2c 20 20 34 33 32 2c 20 20  /   376,  432,  
2bd1a 20 35 33 2c 20 20 34 33 36 2c 20 20 34 33 32 2c   53,  436,  432,
2bd1b 20 20 20 39 39 2c 20 20 34 33 32 2c 20 20 20 39     99,  432,   9
2bd1c 37 2c 20 20 34 33 32 2c 20 20 31 30 32 2c 0a 20  7,  432,  102,. 
2bd1d 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20 20 34 33  /*   740 */   43
2bd1e 32 2c 20 20 31 30 33 2c 20 20 20 36 31 2c 20 20  2,  103,   61,  
2bd1f 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bd20 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bd21 30 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20 37  0,   63,. /*   7
2bd22 35 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20 36  50 */    63,   6
2bd23 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bd24 20 36 32 2c 20 20 33 33 30 2c 20 20 20 36 34 2c   62,  330,   64,
2bd25 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2bd26 34 2c 0a 20 2f 2a 20 20 20 37 36 30 20 2a 2f 20  4,. /*   760 */ 
2bd27 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
2bd28 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bd29 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bd2a 20 20 33 33 30 2c 20 20 34 30 35 2c 0a 20 2f 2a    330,  405,. /*
2bd2b 20 20 20 37 37 30 20 2a 2f 20 20 20 20 20 31 2c     770 */     1,
2bd2c 20 20 32 30 32 2c 20 20 33 33 30 2c 20 20 35 31    202,  330,  51
2bd2d 32 2c 20 20 33 33 30 2c 20 20 32 31 34 2c 20 20  2,  330,  214,  
2bd2e 33 33 30 2c 20 20 31 37 31 2c 20 20 34 33 32 2c  330,  171,  432,
2bd2f 20 20 31 30 38 2c 0a 20 2f 2a 20 20 20 37 38 30    108,. /*   780
2bd30 20 2a 2f 20 20 20 33 33 30 2c 20 20 34 32 31 2c   */   330,  421,
2bd31 20 20 34 32 39 2c 20 20 33 33 30 2c 20 20 34 38    429,  330,  48
2bd32 37 2c 20 20 33 34 32 2c 20 20 33 33 30 2c 20 20  7,  342,  330,  
2bd33 33 38 34 2c 20 20 20 31 39 2c 20 20 33 38 36 2c  384,   19,  386,
2bd34 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f 20 20 20  . /*   790 */   
2bd35 31 34 35 2c 20 20 34 33 32 2c 20 20 31 31 30 2c  145,  432,  110,
2bd36 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 34 33    443,  449,  43
2bd37 32 2c 20 20 20 31 36 2c 20 20 34 33 32 2c 20 20  2,   16,  432,  
2bd38 31 30 30 2c 20 20 34 33 32 2c 0a 20 2f 2a 20 20  100,  432,. /*  
2bd39 20 38 30 30 20 2a 2f 20 20 20 20 33 34 2c 20 20   800 */    34,  
2bd3a 33 35 31 2c 20 20 32 37 30 2c 20 20 34 33 32 2c  351,  270,  432,
2bd3b 20 20 20 39 38 2c 20 20 34 33 33 2c 20 20 34 33     98,  433,  43
2bd3c 32 2c 20 20 20 32 35 2c 20 20 32 37 36 2c 20 20  2,   25,  276,  
2bd3d 34 33 32 2c 0a 20 2f 2a 20 20 20 38 31 30 20 2a  432,. /*   810 *
2bd3e 2f 20 20 20 20 35 35 2c 20 20 20 36 31 2c 20 20  /    55,   61,  
2bd3f 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bd40 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bd41 30 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 0a 20  0,   63,   63,. 
2bd42 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20 20 20 36  /*   820 */    6
2bd43 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  2,   62,   62,  
2bd44 20 36 32 2c 20 20 33 33 30 2c 20 20 20 36 34 2c   62,  330,   64,
2bd45 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2bd46 34 2c 20 20 20 36 35 2c 0a 20 2f 2a 20 20 20 38  4,   65,. /*   8
2bd47 33 30 20 2a 2f 20 20 20 20 36 35 2c 20 20 20 36  30 */    65,   6
2bd48 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bd49 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bd4a 20 20 33 33 30 2c 20 20 33 32 33 2c 20 20 31 31    330,  323,  11
2bd4b 39 2c 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20  9,. /*   840 */ 
2bd4c 20 20 32 37 34 2c 20 20 33 33 30 2c 20 20 32 37    274,  330,  27
2bd4d 32 2c 20 20 33 33 30 2c 20 20 33 35 35 2c 20 20  2,  330,  355,  
2bd4e 33 33 30 2c 20 20 34 32 32 2c 20 20 34 33 32 2c  330,  422,  432,
2bd4f 20 20 31 31 31 2c 20 20 33 33 30 2c 0a 20 2f 2a    111,  330,. /*
2bd50 20 20 20 38 35 30 20 2a 2f 20 20 20 35 38 30 2c     850 */   580,
2bd51 20 20 31 35 39 2c 20 20 31 31 35 2c 20 20 32 33    159,  115,  23
2bd52 33 2c 20 20 33 33 30 2c 20 20 31 37 37 2c 20 20  3,  330,  177,  
2bd53 31 36 31 2c 20 20 34 33 39 2c 20 20 34 36 33 2c  161,  439,  463,
2bd54 20 20 34 36 33 2c 0a 20 2f 2a 20 20 20 38 36 30    463,. /*   860
2bd55 20 2a 2f 20 20 20 34 33 32 2c 20 20 31 31 32 2c   */   432,  112,
2bd56 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 34 33    443,  449,  43
2bd57 32 2c 20 20 31 31 33 2c 20 20 34 33 32 2c 20 20  2,  113,  432,  
2bd58 20 32 36 2c 20 20 34 33 32 2c 20 20 20 33 37 2c   26,  432,   37,
2bd59 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20 20  . /*   870 */   
2bd5a 36 34 39 2c 20 20 34 33 31 2c 20 20 34 33 32 2c  649,  431,  432,
2bd5b 20 20 20 33 38 2c 20 20 34 39 32 2c 20 20 34 33     38,  492,  43
2bd5c 30 2c 20 20 34 38 37 2c 20 20 34 33 32 2c 20 20  0,  487,  432,  
2bd5d 20 32 37 2c 20 20 32 36 34 2c 0a 20 2f 2a 20 20   27,  264,. /*  
2bd5e 20 38 38 30 20 2a 2f 20 20 20 20 36 31 2c 20 20   880 */    61,  
2bd5f 20 37 31 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   71,  309,  453,
2bd60 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bd61 30 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  0,   63,   63,  
2bd62 20 36 32 2c 0a 20 2f 2a 20 20 20 38 39 30 20 2a   62,. /*   890 *
2bd63 2f 20 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  /    62,   62,  
2bd64 20 36 32 2c 20 20 33 33 30 2c 20 20 20 36 34 2c   62,  330,   64,
2bd65 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2bd66 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 0a 20  4,   65,   65,. 
2bd67 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20 20 20 36  /*   900 */    6
2bd68 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  6,   66,   66,  
2bd69 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bd6a 20 20 33 33 30 2c 20 20 32 36 34 2c 20 20 32 36    330,  264,  26
2bd6b 34 2c 20 20 35 32 38 2c 0a 20 2f 2a 20 20 20 39  4,  528,. /*   9
2bd6c 31 30 20 2a 2f 20 20 20 33 33 30 2c 20 20 31 35  10 */   330,  15
2bd6d 37 2c 20 20 33 33 30 2c 20 20 32 35 32 2c 20 20  7,  330,  252,  
2bd6e 33 33 30 2c 20 20 32 32 39 2c 20 20 34 33 32 2c  330,  229,  432,
2bd6f 20 20 20 33 39 2c 20 20 33 33 30 2c 20 20 34 38     39,  330,  48
2bd70 32 2c 0a 20 2f 2a 20 20 20 39 32 30 20 2a 2f 20  2,. /*   920 */ 
2bd71 20 20 33 33 32 2c 20 20 34 37 38 2c 20 20 20 37    332,  478,   7
2bd72 37 2c 20 20 33 33 30 2c 20 20 20 37 39 2c 20 20  7,  330,   79,  
2bd73 33 33 30 2c 20 20 34 38 33 2c 20 20 35 32 30 2c  330,  483,  520,
2bd74 20 20 35 32 31 2c 20 20 34 33 32 2c 0a 20 2f 2a    521,  432,. /*
2bd75 20 20 20 39 33 30 20 2a 2f 20 20 20 20 34 30 2c     930 */    40,
2bd76 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 34 33    443,  449,  43
2bd77 32 2c 20 20 20 34 31 2c 20 20 34 33 32 2c 20 20  2,   41,  432,  
2bd78 20 34 33 2c 20 20 34 33 32 2c 20 20 20 34 34 2c   43,  432,   44,
2bd79 20 20 34 39 32 2c 0a 20 2f 2a 20 20 20 39 34 30    492,. /*   940
2bd7a 20 2a 2f 20 20 20 34 39 31 2c 20 20 34 33 32 2c   */   491,  432,
2bd7b 20 20 20 34 35 2c 20 20 33 31 36 2c 20 20 33 31     45,  316,  31
2bd7c 37 2c 20 20 34 33 33 2c 20 20 34 33 32 2c 20 20  7,  433,  432,  
2bd7d 20 33 30 2c 20 20 34 33 32 2c 20 20 20 33 31 2c   30,  432,   31,
2bd7e 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f 20 20 20  . /*   950 */   
2bd7f 20 36 30 2c 20 20 33 30 39 2c 20 20 34 35 33 2c   60,  309,  453,
2bd80 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bd81 30 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  0,   63,   63,  
2bd82 20 36 32 2c 20 20 20 36 32 2c 0a 20 2f 2a 20 20   62,   62,. /*  
2bd83 20 39 36 30 20 2a 2f 20 20 20 20 36 32 2c 20 20   960 */    62,  
2bd84 20 36 32 2c 20 20 33 33 30 2c 20 20 20 36 34 2c   62,  330,   64,
2bd85 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2bd86 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  4,   65,   65,  
2bd87 20 36 36 2c 0a 20 2f 2a 20 20 20 39 37 30 20 2a   66,. /*   970 *
2bd88 2f 20 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20  /    66,   66,  
2bd89 20 36 37 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   67,  216,  304,
2bd8a 20 20 33 33 30 2c 20 20 32 36 34 2c 20 20 35 36    330,  264,  56
2bd8b 34 2c 20 20 32 35 34 2c 20 20 33 33 30 2c 0a 20  4,  254,  330,. 
2bd8c 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20 20 34 35  /*   980 */   45
2bd8d 38 2c 20 20 33 33 30 2c 20 20 20 32 32 2c 20 20  8,  330,   22,  
2bd8e 33 33 30 2c 20 20 34 39 35 2c 20 20 34 33 32 2c  330,  495,  432,
2bd8f 20 20 20 34 36 2c 20 20 33 33 30 2c 20 20 34 39     46,  330,  49
2bd90 34 2c 20 20 35 33 35 2c 0a 20 2f 2a 20 20 20 39  4,  535,. /*   9
2bd91 39 30 20 2a 2f 20 20 20 31 37 39 2c 20 20 31 38  90 */   179,  18
2bd92 36 2c 20 20 33 33 30 2c 20 20 32 36 37 2c 20 20  6,  330,  267,  
2bd93 33 33 30 2c 20 20 31 38 36 2c 20 20 34 35 31 2c  330,  186,  451,
2bd94 20 20 34 39 37 2c 20 20 34 33 32 2c 20 20 20 34    497,  432,   4
2bd95 37 2c 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20  7,. /*  1000 */ 
2bd96 20 20 34 34 33 2c 20 20 34 34 39 2c 20 20 34 33    443,  449,  43
2bd97 32 2c 20 20 20 34 38 2c 20 20 34 33 32 2c 20 20  2,   48,  432,  
2bd98 20 34 39 2c 20 20 34 33 32 2c 20 20 20 33 32 2c   49,  432,   32,
2bd99 20 20 31 38 32 2c 20 20 32 36 32 2c 0a 20 2f 2a    182,  262,. /*
2bd9a 20 20 31 30 31 30 20 2a 2f 20 20 20 34 33 32 2c    1010 */   432,
2bd9b 20 20 20 31 30 2c 20 20 33 31 38 2c 20 20 32 37     10,  318,  27
2bd9c 36 2c 20 20 33 38 39 2c 20 20 34 33 32 2c 20 20  6,  389,  432,  
2bd9d 20 35 31 2c 20 20 34 33 32 2c 20 20 20 35 32 2c   51,  432,   52,
2bd9e 20 20 32 37 36 2c 0a 20 2f 2a 20 20 31 30 32 30    276,. /*  1020
2bd9f 20 2a 2f 20 20 20 33 30 39 2c 20 20 34 35 33 2c   */   309,  453,
2bda0 20 20 34 35 34 2c 20 20 34 35 30 2c 20 20 34 35    454,  450,  45
2bda1 30 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  0,   63,   63,  
2bda2 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c   62,   62,   62,
2bda3 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20 20  . /*  1030 */   
2bda4 20 36 32 2c 20 20 32 37 36 2c 20 20 20 36 34 2c   62,  276,   64,
2bda5 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2bda6 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  4,   65,   65,  
2bda7 20 36 36 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20   66,   66,. /*  
2bda8 31 30 34 30 20 2a 2f 20 20 20 20 36 36 2c 20 20  1040 */    66,  
2bda9 20 36 37 2c 20 20 32 31 36 2c 20 20 31 36 35 2c   67,  216,  165,
2bdaa 20 20 32 37 36 2c 20 20 32 37 36 2c 20 20 31 38    276,  276,  18
2bdab 39 2c 20 20 31 39 32 2c 20 20 32 33 35 2c 20 20  9,  192,  235,  
2bdac 32 33 36 2c 0a 20 2f 2a 20 20 31 30 35 30 20 2a  236,. /*  1050 *
2bdad 2f 20 20 20 32 33 37 2c 20 20 31 36 38 2c 20 20  /   237,  168,  
2bdae 32 33 39 2c 20 20 35 36 36 2c 20 20 31 30 35 2c  239,  566,  105,
2bdaf 20 20 35 38 31 2c 20 20 20 31 38 2c 20 20 35 33    581,   18,  53
2bdb0 30 2c 20 20 35 32 39 2c 20 20 20 37 33 2c 0a 20  0,  529,   73,. 
2bdb1 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20 20 33 33  /*  1060 */   33
2bdb2 37 2c 20 20 35 38 32 2c 20 20 20 20 34 2c 20 20  7,  582,    4,  
2bdb3 33 30 36 2c 20 20 36 30 35 2c 20 20 35 32 37 2c  306,  605,  527,
2bdb4 20 20 33 30 38 2c 20 20 32 31 31 2c 20 20 33 36    308,  211,  36
2bdb5 36 2c 20 20 32 39 34 2c 0a 20 2f 2a 20 20 31 30  6,  294,. /*  10
2bdb6 37 30 20 2a 2f 20 20 20 31 38 36 2c 20 20 32 36  70 */   186,  26
2bdb7 33 2c 20 20 35 33 33 2c 20 20 32 33 31 2c 20 20  3,  533,  231,  
2bdb8 33 33 34 2c 20 20 35 36 35 2c 20 20 32 39 35 2c  334,  565,  295,
2bdb9 20 20 31 38 36 2c 20 20 35 33 34 2c 20 20 35 34    186,  534,  54
2bdba 36 2c 0a 20 2f 2a 20 20 31 30 38 30 20 2a 2f 20  6,. /*  1080 */ 
2bdbb 20 20 34 33 33 2c 20 20 34 33 33 2c 20 20 35 37    433,  433,  57
2bdbc 33 2c 20 20 35 37 34 2c 20 20 31 37 39 2c 20 20  3,  574,  179,  
2bdbd 20 39 32 2c 20 20 32 33 32 2c 20 20 32 39 32 2c   92,  232,  292,
2bdbe 20 20 32 30 39 2c 20 20 32 36 39 2c 0a 20 2f 2a    209,  269,. /*
2bdbf 20 20 31 30 39 30 20 2a 2f 20 20 20 35 36 39 2c    1090 */   569,
2bdc0 20 20 33 33 39 2c 20 20 32 37 31 2c 20 20 38 35    339,  271,  85
2bdc1 33 2c 20 20 32 30 38 2c 20 20 32 37 33 2c 20 20  3,  208,  273,  
2bdc2 32 37 35 2c 20 20 32 31 30 2c 20 20 35 38 35 2c  275,  210,  585,
2bdc3 20 20 31 39 35 2c 0a 20 2f 2a 20 20 31 31 30 30    195,. /*  1100
2bdc4 20 2a 2f 20 20 20 20 39 32 2c 20 20 34 36 39 2c   */    92,  469,
2bdc5 20 20 33 37 31 2c 20 20 36 30 36 2c 20 20 36 30    371,  606,  60
2bdc6 32 2c 20 20 20 20 38 2c 20 20 33 30 32 2c 20 20  2,    8,  302,  
2bdc7 34 32 33 2c 20 20 32 38 30 2c 20 20 33 37 39 2c  423,  280,  379,
2bdc8 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f 20 20 20  . /*  1110 */   
2bdc9 33 38 32 2c 20 20 33 38 33 2c 20 20 31 34 37 2c  382,  383,  147,
2bdca 20 20 32 34 32 2c 20 20 32 38 33 2c 20 20 34 33    242,  283,  43
2bdcb 37 2c 20 20 34 36 32 2c 20 20 32 38 34 2c 20 20  7,  462,  284,  
2bdcc 32 38 35 2c 20 20 35 37 37 2c 0a 20 2f 2a 20 20  285,  577,. /*  
2bdcd 31 31 32 30 20 2a 2f 20 20 20 33 33 38 2c 20 20  1120 */   338,  
2bdce 20 37 36 2c 20 20 20 37 35 2c 20 20 35 38 37 2c   76,   75,  587,
2bdcf 20 20 32 39 33 2c 20 20 32 39 36 2c 20 20 32 39    293,  296,  29
2bdd0 37 2c 20 20 35 39 39 2c 20 20 34 38 31 2c 20 20  7,  599,  481,  
2bdd1 34 36 34 2c 0a 20 2f 2a 20 20 31 31 33 30 20 2a  464,. /*  1130 *
2bdd2 2f 20 20 20 20 37 34 2c 20 20 33 32 38 2c 20 20  /    74,  328,  
2bdd3 33 32 39 2c 20 20 32 35 30 2c 20 20 35 32 36 2c  329,  250,  526,
2bdd4 20 20 34 33 38 2c 20 20 35 37 32 2c 20 20 31 36    438,  572,  16
2bdd5 36 2c 20 20 32 39 30 2c 20 20 33 39 33 2c 0a 20  6,  290,  393,. 
2bdd6 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20 20 33 39  /*  1140 */   39
2bdd7 32 2c 20 20 32 39 31 2c 20 20 32 38 31 2c 20 20  2,  291,  281,  
2bdd8 34 30 39 2c 20 20 35 33 37 2c 20 20 35 38 34 2c  409,  537,  584,
2bdd9 20 20 33 30 35 2c 20 20 34 38 34 2c 20 20 32 35    305,  484,  25
2bdda 39 2c 20 20 35 34 30 2c 0a 20 2f 2a 20 20 31 31  9,  540,. /*  11
2bddb 35 30 20 2a 2f 20 20 20 34 31 37 2c 20 20 32 31  50 */   417,  21
2bddc 34 2c 20 20 34 31 38 2c 20 20 32 31 34 2c 20 20  4,  418,  214,  
2bddd 35 33 36 2c 20 20 33 32 36 2c 20 20 35 33 38 2c  536,  326,  538,
2bdde 20 20 34 31 39 2c 20 20 33 36 31 2c 20 20 31 36    419,  361,  16
2bddf 37 2c 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20  7,. /*  1160 */ 
2bde0 20 20 20 37 33 2c 20 20 33 33 37 2c 20 20 31 36     73,  337,  16
2bde1 39 2c 20 20 20 20 34 2c 20 20 20 20 37 2c 20 20  9,    4,    7,  
2bde2 33 32 37 2c 20 20 33 34 37 2c 20 20 33 30 38 2c  327,  347,  308,
2bde3 20 20 34 34 30 2c 20 20 34 34 30 2c 0a 20 2f 2a    440,  440,. /*
2bde4 20 20 31 31 37 30 20 2a 2f 20 20 20 34 34 30 2c    1170 */   440,
2bde5 20 20 34 34 31 2c 20 20 34 34 32 2c 20 20 20 31    441,  442,   1
2bde6 31 2c 20 20 20 38 35 2c 20 20 33 33 34 2c 20 20  1,   85,  334,  
2bde7 33 39 38 2c 20 20 20 38 34 2c 20 20 34 33 34 2c  398,   84,  434,
2bde8 20 20 33 34 35 2c 0a 20 2f 2a 20 20 31 31 38 30    345,. /*  1180
2bde9 20 2a 2f 20 20 20 32 34 33 2c 20 20 20 35 38 2c   */   243,   58,
2bdea 20 20 32 34 34 2c 20 20 20 37 33 2c 20 20 33 33    244,   73,  33
2bdeb 37 2c 20 20 20 38 30 2c 20 20 20 20 34 2c 20 20  7,   80,    4,  
2bdec 32 34 35 2c 20 20 34 33 35 2c 20 20 32 34 36 2c  245,  435,  246,
2bded 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20 20  . /*  1190 */   
2bdee 33 30 38 2c 20 20 31 37 36 2c 20 20 33 33 39 2c  308,  176,  339,
2bdef 20 20 34 37 39 2c 20 20 20 38 36 2c 20 20 31 32    479,   86,  12
2bdf0 31 2c 20 20 33 35 36 2c 20 20 33 35 30 2c 20 20  1,  356,  350,  
2bdf1 33 33 34 2c 20 20 34 39 33 2c 0a 20 2f 2a 20 20  334,  493,. /*  
2bdf2 31 32 30 30 20 2a 2f 20 20 20 32 35 31 2c 20 20  1200 */   251,  
2bdf3 32 35 33 2c 20 20 34 36 39 2c 20 20 34 39 39 2c  253,  469,  499,
2bdf4 20 20 32 35 35 2c 20 20 35 31 33 2c 20 20 35 30    255,  513,  50
2bdf5 30 2c 20 20 35 31 38 2c 20 20 33 31 33 2c 20 20  0,  518,  313,  
2bdf6 35 31 39 2c 0a 20 2f 2a 20 20 31 32 31 30 20 2a  519,. /*  1210 *
2bdf7 2f 20 20 20 32 36 30 2c 20 20 35 32 33 2c 20 20  /   260,  523,  
2bdf8 31 32 35 2c 20 20 35 32 32 2c 20 20 32 32 36 2c  125,  522,  226,
2bdf9 20 20 33 33 39 2c 20 20 32 31 39 2c 20 20 35 32    339,  219,  52
2bdfa 34 2c 20 20 33 36 38 2c 20 20 31 39 30 2c 0a 20  4,  368,  190,. 
2bdfb 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20 20 31 39  /*  1220 */   19
2bdfc 31 2c 20 20 33 30 30 2c 20 20 20 37 36 2c 20 20  1,  300,   76,  
2bdfd 20 37 35 2c 20 20 35 30 32 2c 20 20 34 36 39 2c   75,  502,  469,
2bdfe 20 20 32 32 35 2c 20 20 32 32 37 2c 20 20 35 34    225,  227,  54
2bdff 37 2c 20 20 35 34 31 2c 0a 20 2f 2a 20 20 31 32  7,  541,. /*  12
2be00 33 30 20 2a 2f 20 20 20 35 34 38 2c 20 20 20 37  30 */   548,   7
2be01 34 2c 20 20 33 32 38 2c 20 20 33 32 39 2c 20 20  4,  328,  329,  
2be02 33 30 31 2c 20 20 35 35 35 2c 20 20 34 33 38 2c  301,  555,  438,
2be03 20 20 35 34 39 2c 20 20 33 37 30 2c 20 20 31 39    549,  370,  19
2be04 33 2c 0a 20 2f 2a 20 20 31 32 34 30 20 2a 2f 20  3,. /*  1240 */ 
2be05 20 20 33 37 32 2c 20 20 31 39 34 2c 20 20 35 35    372,  194,  55
2be06 37 2c 20 20 20 38 39 2c 20 20 31 39 36 2c 20 20  7,   89,  196,  
2be07 20 37 36 2c 20 20 20 37 35 2c 20 20 32 37 38 2c   76,   75,  278,
2be08 20 20 33 37 38 2c 20 20 31 31 37 2c 0a 20 2f 2a    378,  117,. /*
2be09 20 20 31 32 35 30 20 2a 2f 20 20 20 35 35 38 2c    1250 */   558,
2be0a 20 20 35 36 38 2c 20 20 31 33 33 2c 20 20 33 39    568,  133,  39
2be0b 30 2c 20 20 20 37 34 2c 20 20 33 32 38 2c 20 20  0,   74,  328,  
2be0c 33 32 39 2c 20 20 31 39 39 2c 20 20 33 39 31 2c  329,  199,  391,
2be0d 20 20 34 33 38 2c 0a 20 2f 2a 20 20 31 32 36 30    438,. /*  1260
2be0e 20 2a 2f 20 20 20 33 32 32 2c 20 20 31 33 34 2c   */   322,  134,
2be0f 20 20 31 33 35 2c 20 20 31 33 36 2c 20 20 35 37    135,  136,  57
2be10 35 2c 20 20 31 34 33 2c 20 20 35 38 33 2c 20 20  5,  143,  583,  
2be11 35 39 36 2c 20 20 31 33 39 2c 20 20 34 34 30 2c  596,  139,  440,
2be12 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f 20 20 20  . /*  1270 */   
2be13 34 34 30 2c 20 20 34 34 30 2c 20 20 34 34 31 2c  440,  440,  441,
2be14 20 20 34 34 32 2c 20 20 20 31 31 2c 20 20 35 39    442,   11,  59
2be15 37 2c 20 20 35 39 38 2c 20 20 36 30 31 2c 20 20  7,  598,  601,  
2be16 31 33 37 2c 20 20 31 34 32 2c 0a 20 2f 2a 20 20  137,  142,. /*  
2be17 31 32 38 30 20 2a 2f 20 20 20 31 30 31 2c 20 20  1280 */   101,  
2be18 32 32 34 2c 20 20 31 30 34 2c 20 20 34 30 37 2c  224,  104,  407,
2be19 20 20 32 33 38 2c 20 20 34 32 34 2c 20 20 36 35    238,  424,  65
2be1a 30 2c 20 20 36 35 31 2c 20 20 20 39 33 2c 20 20  0,  651,   93,  
2be1b 31 37 32 2c 0a 20 2f 2a 20 20 31 32 39 30 20 2a  172,. /*  1290 *
2be1c 2f 20 20 20 20 39 36 2c 20 20 31 37 33 2c 20 20  /    96,  173,  
2be1d 34 34 30 2c 20 20 34 34 30 2c 20 20 34 34 30 2c  440,  440,  440,
2be1e 20 20 34 34 31 2c 20 20 34 34 32 2c 20 20 20 31    441,  442,   1
2be1f 31 2c 20 20 34 35 32 2c 20 20 34 35 35 2c 0a 20  1,  452,  455,. 
2be20 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20 20 20 37  /*  1300 */    7
2be21 32 2c 20 20 34 37 31 2c 20 20 34 35 39 2c 20 20  2,  471,  459,  
2be22 34 36 38 2c 20 20 34 37 32 2c 20 20 31 34 34 2c  468,  472,  144,
2be23 20 20 31 35 38 2c 20 20 20 20 36 2c 20 20 34 37    158,    6,  47
2be24 33 2c 20 20 34 39 30 2c 0a 20 2f 2a 20 20 31 33  3,  490,. /*  13
2be25 31 30 20 2a 2f 20 20 20 31 30 37 2c 20 20 31 37  10 */   107,  17
2be26 35 2c 20 20 34 37 37 2c 20 20 20 38 32 2c 20 20  5,  477,   82,  
2be27 20 31 33 2c 20 20 31 32 32 2c 20 20 20 31 32 2c   13,  122,   12,
2be28 20 20 31 38 30 2c 20 20 35 30 36 2c 20 20 31 31    180,  506,  11
2be29 38 2c 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20  8,. /*  1320 */ 
2be2a 20 20 34 39 38 2c 20 20 31 36 34 2c 20 20 35 30    498,  164,  50
2be2b 37 2c 20 20 33 32 34 2c 20 20 32 32 33 2c 20 20  7,  324,  223,  
2be2c 20 38 37 2c 20 20 31 32 36 2c 20 20 31 31 36 2c   87,  126,  116,
2be2d 20 20 32 36 36 2c 20 20 31 32 37 2c 0a 20 2f 2a    266,  127,. /*
2be2e 20 20 31 33 33 30 20 2a 2f 20 20 20 20 38 38 2c    1330 */    88,
2be2f 20 20 31 32 38 2c 20 20 31 38 38 2c 20 20 32 35    128,  188,  25
2be30 38 2c 20 20 33 36 30 2c 20 20 33 36 39 2c 20 20  8,  360,  369,  
2be31 31 34 36 2c 20 20 35 35 36 2c 20 20 31 32 39 2c  146,  556,  129,
2be32 20 20 33 37 33 2c 0a 20 2f 2a 20 20 31 33 34 30    373,. /*  1340
2be33 20 2a 2f 20 20 20 31 37 39 2c 20 20 33 36 35 2c   */   179,  365,
2be34 20 20 32 37 39 2c 20 20 31 39 37 2c 20 20 31 33    279,  197,  13
2be35 31 2c 20 20 31 33 30 2c 20 20 35 36 33 2c 20 20  1,  130,  563,  
2be36 20 20 39 2c 20 20 35 37 31 2c 20 20 31 33 32 2c    9,  571,  132,
2be37 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f 20 20 20  . /*  1350 */   
2be38 35 35 39 2c 20 20 32 30 31 2c 20 20 20 31 34 2c  559,  201,   14,
2be39 20 20 35 37 36 2c 20 20 32 30 33 2c 20 20 32 30    576,  203,  20
2be3a 34 2c 20 20 32 30 35 2c 20 20 35 37 39 2c 20 20  4,  205,  579,  
2be3b 31 34 30 2c 20 20 31 33 38 2c 0a 20 2f 2a 20 20  140,  138,. /*  
2be3c 31 33 36 30 20 2a 2f 20 20 20 31 34 31 2c 20 20  1360 */   141,  
2be3d 20 31 35 2c 20 20 35 38 36 2c 20 20 35 39 34 2c   15,  586,  594,
2be3e 20 20 32 31 32 2c 20 20 31 30 36 2c 20 20 34 30    212,  106,  40
2be3f 30 2c 20 20 32 39 38 2c 20 20 31 34 39 2c 20 20  0,  298,  149,  
2be40 34 30 34 2c 0a 20 2f 2a 20 20 31 33 37 30 20 2a  404,. /*  1370 *
2be41 2f 20 20 20 39 33 31 2c 20 20 36 30 38 2c 20 20  /   931,  608,  
2be42 20 39 30 2c 20 20 20 39 31 2c 0a 7d 3b 0a 73 74   90,   91,.};.st
2be43 61 74 69 63 20 63 6f 6e 73 74 20 59 59 43 4f 44  atic const YYCOD
2be44 45 54 59 50 45 20 79 79 5f 6c 6f 6f 6b 61 68 65  ETYPE yy_lookahe
2be45 61 64 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20  ad[] = {. /*    
2be46 20 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 31 34   0 */    19,  14
2be47 32 2c 20 20 31 34 33 2c 20 20 31 34 34 2c 20 20  2,  143,  144,  
2be48 31 34 35 2c 20 20 20 32 34 2c 20 20 31 31 33 2c  145,   24,  113,
2be49 20 20 20 32 36 2c 20 20 20 37 32 2c 20 20 20 37     26,   72,   7
2be4a 33 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20  3,. /*    10 */ 
2be4b 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 38     74,   75,   8
2be4c 37 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  7,   77,   78,  
2be4d 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c   79,   80,   81,
2be4e 20 20 20 38 32 2c 20 20 20 38 33 2c 0a 20 2f 2a     82,   83,. /*
2be4f 20 20 20 20 32 30 20 2a 2f 20 20 20 20 38 34 2c      20 */    84,
2be50 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
2be51 37 2c 20 20 20 20 31 2c 20 20 20 20 32 2c 20 20  7,    1,    2,  
2be52 20 34 35 2c 20 20 20 34 36 2c 20 20 20 37 36 2c   45,   46,   76,
2be53 20 20 20 37 37 2c 0a 20 2f 2a 20 20 20 20 33 30     77,. /*    30
2be54 20 2a 2f 20 20 20 20 37 38 2c 20 20 20 37 39 2c   */    78,   79,
2be55 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38     80,   81,   8
2be56 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
2be57 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
2be58 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20  . /*    40 */   
2be59 20 36 31 2c 20 20 31 36 35 2c 20 20 31 36 36 2c   61,  165,  166,
2be5a 20 20 31 36 37 2c 20 20 20 36 33 2c 20 20 20 36    167,   63,   6
2be5b 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2be5c 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20   67,   68,. /*  
2be5d 20 20 35 30 20 2a 2f 20 20 20 20 36 39 2c 20 20    50 */    69,  
2be5e 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2be5f 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2be60 35 2c 20 20 20 32 35 2c 20 20 20 37 37 2c 20 20  5,   25,   77,  
2be61 20 37 38 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a   78,. /*    60 *
2be62 2f 20 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  /    79,   80,  
2be63 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2be64 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2be65 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 0a 20  6,   87,   19,. 
2be66 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 20 39  /*    70 */    9
2be67 31 2c 20 20 20 31 39 2c 20 20 20 39 31 2c 20 20  1,   19,   91,  
2be68 31 37 33 2c 20 20 20 39 35 2c 20 20 20 32 35 2c  173,   95,   25,
2be69 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
2be6a 39 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 20  9,   80,. /*    
2be6b 38 30 20 2a 2f 20 20 20 20 38 31 2c 20 20 20 38  80 */    81,   8
2be6c 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
2be6d 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
2be6e 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2be6f 33 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20  3,. /*    90 */ 
2be70 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2be71 36 2c 20 20 20 38 37 2c 20 20 31 37 33 2c 20 20  6,   87,  173,  
2be72 20 34 35 2c 20 20 20 34 36 2c 20 20 31 34 36 2c   45,   46,  146,
2be73 20 20 31 34 37 2c 20 20 20 34 39 2c 0a 20 2f 2a    147,   49,. /*
2be74 20 20 20 31 30 30 20 2a 2f 20 20 20 20 38 31 2c     100 */    81,
2be75 20 20 20 38 32 2c 20 20 20 32 32 2c 20 20 31 35     82,   22,  15
2be76 32 2c 20 20 20 32 36 2c 20 20 31 35 30 2c 20 20  2,   26,  150,  
2be77 20 32 36 2c 20 20 31 32 38 2c 20 20 31 32 39 2c   26,  128,  129,
2be78 20 20 31 33 30 2c 0a 20 2f 2a 20 20 20 31 31 30    130,. /*   110
2be79 20 2a 2f 20 20 20 31 35 39 2c 20 20 20 38 36 2c   */   159,   86,
2be7a 20 20 20 38 37 2c 20 20 20 36 33 2c 20 20 20 36     87,   63,   6
2be7b 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2be7c 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2be7d 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20  . /*   120 */   
2be7e 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2be7f 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2be80 35 2c 20 20 31 38 35 2c 20 20 20 37 37 2c 20 20  5,  185,   77,  
2be81 20 37 38 2c 20 20 20 37 39 2c 0a 20 2f 2a 20 20   78,   79,. /*  
2be82 20 31 33 30 20 2a 2f 20 20 20 20 38 30 2c 20 20   130 */    80,  
2be83 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2be84 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2be85 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 20 20  6,   87,   19,  
2be86 20 38 37 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a   87,. /*   140 *
2be87 2f 20 20 20 20 32 35 2c 20 20 20 36 31 2c 20 20  /    25,   61,  
2be88 31 35 30 2c 20 20 20 31 39 2c 20 20 31 39 33 2c  150,   19,  193,
2be89 20 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39     93,   94,   9
2be8a 35 2c 20 20 20 39 36 2c 20 20 20 39 37 2c 0a 20  5,   96,   97,. 
2be8b 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 20 39  /*   150 */    9
2be8c 38 2c 20 20 20 39 39 2c 20 20 31 36 30 2c 20 20  8,   99,  160,  
2be8d 31 36 31 2c 20 20 31 37 31 2c 20 20 31 37 32 2c  161,  171,  172,
2be8e 20 20 20 32 35 2c 20 20 31 32 35 2c 20 20 31 30     25,  125,  10
2be8f 36 2c 20 20 20 38 31 2c 0a 20 2f 2a 20 20 20 31  6,   81,. /*   1
2be90 36 30 20 2a 2f 20 20 20 20 38 32 2c 20 20 20 38  60 */    82,   8
2be91 31 2c 20 20 20 38 32 2c 20 20 31 38 30 2c 20 20  1,   82,  180,  
2be92 20 34 35 2c 20 20 20 34 36 2c 20 20 20 34 37 2c   45,   46,   47,
2be93 20 20 32 31 32 2c 20 20 32 31 37 2c 20 20 20 39    212,  217,   9
2be94 31 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20  1,. /*   170 */ 
2be95 20 20 20 39 32 2c 20 20 20 39 31 2c 20 20 31 35     92,   91,  15
2be96 30 2c 20 20 31 37 32 2c 20 20 20 32 36 2c 20 20  0,  172,   26,  
2be97 20 39 35 2c 20 20 31 38 34 2c 20 20 31 38 35 2c   95,  184,  185,
2be98 20 20 31 38 35 2c 20 20 31 30 31 2c 0a 20 2f 2a    185,  101,. /*
2be99 20 20 20 31 38 30 20 2a 2f 20 20 20 31 30 32 2c     180 */   102,
2be9a 20 20 31 38 30 2c 20 20 20 36 33 2c 20 20 20 36    180,   63,   6
2be9b 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2be9c 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2be9d 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 31 39 30     70,. /*   190
2be9e 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37 32 2c   */    71,   72,
2be9f 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bea0 35 2c 20 20 32 34 34 2c 20 20 20 37 37 2c 20 20  5,  244,   77,  
2bea1 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
2bea2 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20  . /*   200 */   
2bea3 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2bea4 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bea5 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 20 20  6,   87,   19,  
2bea6 31 32 38 2c 20 20 31 32 39 2c 0a 20 2f 2a 20 20  128,  129,. /*  
2bea7 20 32 31 30 20 2a 2f 20 20 20 31 33 30 2c 20 20   210 */   130,  
2bea8 20 38 37 2c 20 20 20 32 34 2c 20 20 20 32 36 2c   87,   24,   26,
2bea9 20 20 31 39 32 2c 20 20 31 30 30 2c 20 20 31 35    192,  100,  15
2beaa 30 2c 20 20 31 35 30 2c 20 20 20 39 34 2c 20 20  0,  150,   94,  
2beab 20 39 35 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a   95,. /*   220 *
2beac 2f 20 20 20 20 39 36 2c 20 20 20 39 37 2c 20 20  /    96,   97,  
2bead 20 39 38 2c 20 20 20 39 39 2c 20 20 20 39 33 2c   98,   99,   93,
2beae 20 20 31 35 30 2c 20 20 20 32 35 2c 20 20 20 39    150,   25,   9
2beaf 36 2c 20 20 20 39 37 2c 20 20 20 39 38 2c 0a 20  6,   97,   98,. 
2beb0 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 31 30  /*   230 */   10
2beb1 36 2c 20 20 31 35 30 2c 20 20 31 39 34 2c 20 20  6,  150,  194,  
2beb2 20 34 35 2c 20 20 20 34 36 2c 20 20 31 36 39 2c   45,   46,  169,
2beb3 20 20 31 37 30 2c 20 20 31 35 30 2c 20 20 31 30    170,  150,  10
2beb4 37 2c 20 20 20 39 31 2c 0a 20 2f 2a 20 20 20 32  7,   91,. /*   2
2beb5 34 30 20 2a 2f 20 20 20 31 37 33 2c 20 20 31 37  40 */   173,  17
2beb6 34 2c 20 20 31 36 35 2c 20 20 20 38 33 2c 20 20  4,  165,   83,  
2beb7 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
2beb8 20 20 20 38 37 2c 20 20 31 37 33 2c 20 20 31 37     87,  173,  17
2beb9 34 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20  4,. /*   250 */ 
2beba 20 20 31 36 39 2c 20 20 20 36 33 2c 20 20 20 36    169,   63,   6
2bebb 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2bebc 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2bebd 20 20 20 37 30 2c 20 20 20 37 31 2c 0a 20 2f 2a     70,   71,. /*
2bebe 20 20 20 32 36 30 20 2a 2f 20 20 20 20 37 32 2c     260 */    72,
2bebf 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bec0 35 2c 20 20 32 34 32 2c 20 20 20 37 37 2c 20 20  5,  242,   77,  
2bec1 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
2bec2 20 20 20 38 31 2c 0a 20 2f 2a 20 20 20 32 37 30     81,. /*   270
2bec3 20 2a 2f 20 20 20 20 38 32 2c 20 20 20 38 33 2c   */    82,   83,
2bec4 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bec5 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 20 20  6,   87,   19,  
2bec6 20 31 31 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   11,   91,   92,
2bec7 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 20  . /*   280 */   
2bec8 20 39 33 2c 20 20 32 30 34 2c 20 20 32 31 35 2c   93,  204,  215,
2bec9 20 20 20 39 36 2c 20 20 20 39 37 2c 20 20 20 39     96,   97,   9
2beca 38 2c 20 20 31 39 36 2c 20 20 32 32 30 2c 20 20  8,  196,  220,  
2becb 32 31 33 2c 20 20 32 31 34 2c 0a 20 2f 2a 20 20  213,  214,. /*  
2becc 20 32 39 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   290 */   150,  
2becd 31 39 30 2c 20 20 31 35 30 2c 20 20 32 30 33 2c  190,  150,  203,
2bece 20 20 31 30 37 2c 20 20 20 35 32 2c 20 20 32 33    107,   52,  23
2becf 30 2c 20 20 32 33 31 2c 20 20 32 32 31 2c 20 20  0,  231,  221,  
2bed0 31 35 39 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a  159,. /*   300 *
2bed1 2f 20 20 20 32 32 33 2c 20 20 32 32 34 2c 20 20  /   223,  224,  
2bed2 20 34 35 2c 20 20 20 34 36 2c 20 20 32 32 31 2c   45,   46,  221,
2bed3 20 20 20 39 33 2c 20 20 32 32 33 2c 20 20 32 32     93,  223,  22
2bed4 34 2c 20 20 20 39 36 2c 20 20 20 39 37 2c 0a 20  4,   96,   97,. 
2bed5 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20 20 39  /*   310 */    9
2bed6 38 2c 20 20 32 33 30 2c 20 20 32 33 31 2c 20 20  8,  230,  231,  
2bed7 31 37 33 2c 20 20 31 37 34 2c 20 20 31 37 33 2c  173,  174,  173,
2bed8 20 20 31 37 34 2c 20 20 31 31 36 2c 20 20 20 35    174,  116,   5
2bed9 32 2c 20 20 31 30 37 2c 0a 20 2f 2a 20 20 20 33  2,  107,. /*   3
2beda 32 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20 36  20 */    63,   6
2bedb 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2bedc 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2bedd 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2bede 32 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20  2,. /*   330 */ 
2bedf 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bee0 35 2c 20 20 31 39 33 2c 20 20 20 37 37 2c 20 20  5,  193,   77,  
2bee1 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
2bee2 20 20 20 38 31 2c 20 20 20 38 32 2c 0a 20 2f 2a     81,   82,. /*
2bee3 20 20 20 33 34 30 20 2a 2f 20 20 20 20 38 33 2c     340 */    83,
2bee4 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bee5 36 2c 20 20 20 38 37 2c 20 20 31 30 32 2c 20 20  6,   87,  102,  
2bee6 31 30 33 2c 20 20 31 30 34 2c 20 20 31 35 30 2c  103,  104,  150,
2bee7 20 20 20 31 39 2c 0a 20 2f 2a 20 20 20 33 35 30     19,. /*   350
2bee8 20 2a 2f 20 20 20 20 31 32 2c 20 20 32 32 31 2c   */    12,  221,
2bee9 20 20 32 32 32 2c 20 20 32 32 33 2c 20 20 32 32    222,  223,  22
2beea 34 2c 20 20 32 31 35 2c 20 20 31 31 33 2c 20 20  4,  215,  113,  
2beeb 31 35 39 2c 20 20 31 36 32 2c 20 20 31 35 30 2c  159,  162,  150,
2beec 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 20  . /*   360 */   
2beed 32 32 30 2c 20 20 31 36 35 2c 20 20 31 36 36 2c  220,  165,  166,
2beee 20 20 31 36 37 2c 20 20 31 35 30 2c 20 20 20 32    167,  150,   2
2beef 37 2c 20 20 20 39 35 2c 20 20 32 32 35 2c 20 20  7,   95,  225,  
2bef0 31 30 32 2c 20 20 31 30 33 2c 0a 20 2f 2a 20 20  102,  103,. /*  
2bef1 20 33 37 30 20 2a 2f 20 20 20 31 30 34 2c 20 20   370 */   104,  
2bef2 31 37 33 2c 20 20 31 37 34 2c 20 20 20 32 36 2c  173,  174,   26,
2bef3 20 20 31 35 30 2c 20 20 20 34 35 2c 20 20 20 34    150,   45,   4
2bef4 36 2c 20 20 31 30 36 2c 20 20 20 34 30 2c 20 20  6,  106,   40,  
2bef5 31 31 33 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a  113,. /*   380 *
2bef6 2f 20 20 20 20 34 32 2c 20 20 31 35 30 2c 20 20  /    42,  150,  
2bef7 31 37 33 2c 20 20 31 37 34 2c 20 20 31 35 39 2c  173,  174,  159,
2bef8 20 20 31 36 35 2c 20 20 32 32 31 2c 20 20 31 35    165,  221,  15
2bef9 39 2c 20 20 32 32 33 2c 20 20 32 32 34 2c 0a 20  9,  223,  224,. 
2befa 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20 20 35  /*   390 */    5
2befb 32 2c 20 20 31 39 33 2c 20 20 31 33 35 2c 20 20  2,  193,  135,  
2befc 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c   63,   64,   65,
2befd 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36     66,   67,   6
2befe 38 2c 20 20 20 36 39 2c 0a 20 2f 2a 20 20 20 34  8,   69,. /*   4
2beff 30 30 20 2a 2f 20 20 20 20 37 30 2c 20 20 20 37  00 */    70,   7
2bf00 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
2bf01 20 37 34 2c 20 20 20 37 35 2c 20 20 32 34 31 2c   74,   75,  241,
2bf02 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
2bf03 39 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20  9,. /*   410 */ 
2bf04 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38     80,   81,   8
2bf05 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
2bf06 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
2bf07 20 20 31 39 33 2c 20 20 20 31 39 2c 0a 20 2f 2a    193,   19,. /*
2bf08 20 20 20 34 32 30 20 2a 2f 20 20 20 31 35 30 2c     420 */   150,
2bf09 20 20 31 39 33 2c 20 20 20 31 39 2c 20 20 32 31    193,   19,  21
2bf0a 34 2c 20 20 32 30 34 2c 20 20 31 35 30 2c 20 20  4,  204,  150,  
2bf0b 20 32 33 2c 20 20 31 35 30 2c 20 20 20 38 31 2c   23,  150,   81,
2bf0c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 34 33 30    150,. /*   430
2bf0d 20 2a 2f 20 20 20 32 31 36 2c 20 20 31 35 30 2c   */   216,  150,
2bf0e 20 20 31 35 37 2c 20 20 32 30 35 2c 20 20 32 30    157,  205,  20
2bf0f 36 2c 20 20 31 36 30 2c 20 20 31 36 31 2c 20 20  6,  160,  161,  
2bf10 31 31 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c  110,   91,   92,
2bf11 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20 20  . /*   440 */   
2bf12 20 32 32 2c 20 20 31 31 34 2c 20 20 32 31 37 2c   22,  114,  217,
2bf13 20 20 32 31 32 2c 20 20 20 32 36 2c 20 20 31 35    212,   26,  15
2bf14 39 2c 20 20 20 34 36 2c 20 20 31 35 30 2c 20 20  9,   46,  150,  
2bf15 20 34 35 2c 20 20 20 34 36 2c 0a 20 2f 2a 20 20   45,   46,. /*  
2bf16 20 34 35 30 20 2a 2f 20 20 20 31 37 33 2c 20 20   450 */   173,  
2bf17 31 37 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c  174,  173,  174,
2bf18 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20 31 36    173,  174,  16
2bf19 38 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  8,  169,  170,  
2bf1a 32 33 34 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a  234,. /*   460 *
2bf1b 2f 20 20 20 31 35 39 2c 20 20 31 36 38 2c 20 20  /   159,  168,  
2bf1c 31 36 39 2c 20 20 31 37 30 2c 20 20 32 33 39 2c  169,  170,  239,
2bf1d 20 20 31 33 35 2c 20 20 20 36 33 2c 20 20 20 36    135,   63,   6
2bf1e 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 0a 20  4,   65,   66,. 
2bf1f 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20 20 36  /*   470 */    6
2bf20 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  7,   68,   69,  
2bf21 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2bf22 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bf23 35 2c 20 20 31 39 33 2c 0a 20 2f 2a 20 20 20 34  5,  193,. /*   4
2bf24 38 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20 37  80 */    77,   7
2bf25 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  8,   79,   80,  
2bf26 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2bf27 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bf28 36 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20  6,. /*   490 */ 
2bf29 20 20 20 38 37 2c 20 20 20 31 39 2c 20 20 32 31     87,   19,  21
2bf2a 35 2c 20 20 20 32 33 2c 20 20 31 39 33 2c 20 20  5,   23,  193,  
2bf2b 20 32 33 2c 20 20 20 33 33 2c 20 20 32 30 37 2c   23,   33,  207,
2bf2c 20 20 32 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a    219,  150,. /*
2bf2d 20 20 20 35 30 30 20 2a 2f 20 20 20 32 31 39 2c     500 */   219,
2bf2e 20 20 31 30 31 2c 20 20 20 32 33 2c 20 20 32 31    101,   23,  21
2bf2f 37 2c 20 20 20 32 32 2c 20 20 31 35 30 2c 20 20  7,   22,  150,  
2bf30 31 30 36 2c 20 20 20 32 33 2c 20 20 20 32 36 2c  106,   23,   26,
2bf31 20 20 20 39 31 2c 0a 20 2f 2a 20 20 20 35 31 30     91,. /*   510
2bf32 20 2a 2f 20 20 20 20 39 32 2c 20 20 20 34 35 2c   */    92,   45,
2bf33 20 20 20 34 36 2c 20 20 31 38 30 2c 20 20 31 38     46,  180,  18
2bf34 31 2c 20 20 31 35 34 2c 20 20 20 35 33 2c 20 20  1,  154,   53,  
2bf35 20 34 35 2c 20 20 20 34 36 2c 20 20 32 33 31 2c   45,   46,  231,
2bf36 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20 20  . /*   520 */   
2bf37 31 38 37 2c 20 20 31 35 30 2c 20 20 31 37 33 2c  187,  150,  173,
2bf38 20 20 31 37 34 2c 20 20 32 33 31 2c 20 20 32 33    174,  231,  23
2bf39 39 2c 20 20 20 32 32 2c 20 20 20 32 33 2c 20 20  9,   22,   23,  
2bf3a 31 30 39 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20  109,   25,. /*  
2bf3b 20 35 33 30 20 2a 2f 20 20 20 31 31 31 2c 20 20   530 */   111,  
2bf3c 31 31 32 2c 20 20 20 36 36 2c 20 20 20 36 37 2c  112,   66,   67,
2bf3d 20 20 31 35 30 2c 20 20 20 36 33 2c 20 20 20 36    150,   63,   6
2bf3e 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2bf3f 20 36 37 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a   67,. /*   540 *
2bf40 2f 20 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  /    68,   69,  
2bf41 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2bf42 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bf43 35 2c 20 20 31 35 30 2c 20 20 20 37 37 2c 0a 20  5,  150,   77,. 
2bf44 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20 20 37  /*   550 */    7
2bf45 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  8,   79,   80,  
2bf46 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2bf47 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bf48 36 2c 20 20 20 38 37 2c 0a 20 2f 2a 20 20 20 35  6,   87,. /*   5
2bf49 36 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 39  60 */    19,   9
2bf4a 35 2c 20 20 20 32 32 2c 20 20 20 32 33 2c 20 20  5,   22,   23,  
2bf4b 20 32 33 2c 20 20 20 32 35 2c 20 20 20 36 32 2c   23,   25,   62,
2bf4c 20 20 31 35 30 2c 20 20 31 30 35 2c 20 20 20 31    150,  105,   1
2bf4d 32 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20  2,. /*   570 */ 
2bf4e 20 20 20 32 36 2c 20 20 20 32 33 2c 20 20 20 32     26,   23,   2
2bf4f 36 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  6,   91,   92,  
2bf50 20 32 33 2c 20 20 31 39 32 2c 20 20 20 32 35 2c   23,  192,   25,
2bf51 20 20 31 34 38 2c 20 20 31 30 39 2c 0a 20 2f 2a    148,  109,. /*
2bf52 20 20 20 35 38 30 20 2a 2f 20 20 20 20 32 36 2c     580 */    26,
2bf53 20 20 31 31 31 2c 20 20 31 31 32 2c 20 20 32 31    111,  112,  21
2bf54 32 2c 20 20 20 32 37 2c 20 20 20 32 36 2c 20 20  2,   27,   26,  
2bf55 20 34 35 2c 20 20 20 34 36 2c 20 20 31 30 39 2c   45,   46,  109,
2bf56 20 20 32 32 38 2c 0a 20 2f 2a 20 20 20 35 39 30    228,. /*   590
2bf57 20 2a 2f 20 20 20 31 31 31 2c 20 20 31 31 32 2c   */   111,  112,
2bf58 20 20 20 31 37 2c 20 20 31 30 39 2c 20 20 32 33     17,  109,  23
2bf59 33 2c 20 20 31 31 31 2c 20 20 31 31 32 2c 20 20  3,  111,  112,  
2bf5a 20 34 30 2c 20 20 20 32 34 2c 20 20 20 34 32 2c   40,   24,   42,
2bf5b 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20 20  . /*   600 */   
2bf5c 20 32 36 2c 20 20 31 35 30 2c 20 20 20 36 32 2c   26,  150,   62,
2bf5d 20 20 31 38 36 2c 20 20 20 36 33 2c 20 20 20 36    186,   63,   6
2bf5e 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2bf5f 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20   67,   68,. /*  
2bf60 20 36 31 30 20 2a 2f 20 20 20 20 36 39 2c 20 20   610 */    69,  
2bf61 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2bf62 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bf63 35 2c 20 20 31 35 30 2c 20 20 20 37 37 2c 20 20  5,  150,   77,  
2bf64 20 37 38 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a   78,. /*   620 *
2bf65 2f 20 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  /    79,   80,  
2bf66 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2bf67 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bf68 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 0a 20  6,   87,   19,. 
2bf69 2f 2a 20 20 20 36 33 30 20 2a 2f 20 20 20 20 35  /*   630 */    5
2bf6a 35 2c 20 20 31 39 35 2c 20 20 20 35 37 2c 20 20  5,  195,   57,  
2bf6b 20 32 33 2c 20 20 31 36 35 2c 20 20 20 39 31 2c   23,  165,   91,
2bf6c 20 20 20 39 32 2c 20 20 20 39 31 2c 20 20 20 39     92,   91,   9
2bf6d 32 2c 20 20 20 39 34 2c 0a 20 2f 2a 20 20 20 36  2,   94,. /*   6
2bf6e 34 30 20 2a 2f 20 20 20 31 37 33 2c 20 20 31 37  40 */   173,  17
2bf6f 34 2c 20 20 20 38 33 2c 20 20 31 35 30 2c 20 20  4,   83,  150,  
2bf70 20 39 32 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   92,   91,   92,
2bf71 20 20 20 20 30 2c 20 20 20 20 31 2c 20 20 20 20      0,    1,    
2bf72 32 2c 0a 20 2f 2a 20 20 20 36 35 30 20 2a 2f 20  2,. /*   650 */ 
2bf73 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 20     91,   92,    
2bf74 37 2c 20 20 20 20 38 2c 20 20 20 20 39 2c 20 20  7,    8,    9,  
2bf75 20 34 35 2c 20 20 20 34 36 2c 20 20 31 30 39 2c   45,   46,  109,
2bf76 20 20 31 31 33 2c 20 20 31 31 31 2c 0a 20 2f 2a    113,  111,. /*
2bf77 20 20 20 36 36 30 20 2a 2f 20 20 20 31 31 32 2c     660 */   112,
2bf78 20 20 31 31 37 2c 20 20 31 35 30 2c 20 20 31 31    117,  150,  11
2bf79 37 2c 20 20 20 32 32 2c 20 20 20 39 31 2c 20 20  7,   22,   91,  
2bf7a 31 37 33 2c 20 20 31 37 34 2c 20 20 20 32 36 2c  173,  174,   26,
2bf7b 20 20 31 31 37 2c 0a 20 2f 2a 20 20 20 36 37 30    117,. /*   670
2bf7c 20 2a 2f 20 20 20 31 38 39 2c 20 20 31 39 30 2c   */   189,  190,
2bf7d 20 20 31 32 37 2c 20 20 20 36 33 2c 20 20 20 36    127,   63,   6
2bf7e 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2bf7f 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2bf80 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20 20 20  . /*   680 */   
2bf81 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2bf82 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bf83 35 2c 20 20 31 35 30 2c 20 20 20 37 37 2c 20 20  5,  150,   77,  
2bf84 20 37 38 2c 20 20 20 37 39 2c 0a 20 2f 2a 20 20   78,   79,. /*  
2bf85 20 36 39 30 20 2a 2f 20 20 20 20 38 30 2c 20 20   690 */    80,  
2bf86 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2bf87 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bf88 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 20 20  6,   87,   19,  
2bf89 31 30 32 2c 0a 20 2f 2a 20 20 20 37 30 30 20 2a  102,. /*   700 *
2bf8a 2f 20 20 20 31 30 33 2c 20 20 31 30 34 2c 20 20  /   103,  104,  
2bf8b 20 31 39 2c 20 20 20 32 34 2c 20 20 31 39 32 2c   19,   24,  192,
2bf8c 20 20 31 35 30 2c 20 20 31 38 39 2c 20 20 31 39    150,  189,  19
2bf8d 30 2c 20 20 31 35 30 2c 20 20 31 37 33 2c 0a 20  0,  150,  173,. 
2bf8e 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20 20 31 37  /*   710 */   17
2bf8f 34 2c 20 20 31 35 30 2c 20 20 31 33 37 2c 20 20  4,  150,  137,  
2bf90 31 35 30 2c 20 20 20 32 38 2c 20 20 31 35 30 2c  150,   28,  150,
2bf91 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 33    150,  150,   3
2bf92 32 2c 20 20 31 30 32 2c 0a 20 2f 2a 20 20 20 37  2,  102,. /*   7
2bf93 32 30 20 2a 2f 20 20 20 31 30 33 2c 20 20 31 30  20 */   103,  10
2bf94 34 2c 20 20 32 34 37 2c 20 20 32 34 38 2c 20 20  4,  247,  248,  
2bf95 20 34 35 2c 20 20 20 34 36 2c 20 20 31 36 30 2c   45,   46,  160,
2bf96 20 20 31 36 31 2c 20 20 31 37 33 2c 20 20 31 37    161,  173,  17
2bf97 34 2c 0a 20 2f 2a 20 20 20 37 33 30 20 2a 2f 20  4,. /*   730 */ 
2bf98 20 20 20 34 34 2c 20 20 31 37 33 2c 20 20 31 37     44,  173,  17
2bf99 34 2c 20 20 20 39 31 2c 20 20 31 37 33 2c 20 20  4,   91,  173,  
2bf9a 31 37 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c  174,  173,  174,
2bf9b 20 20 31 37 33 2c 20 20 31 37 34 2c 0a 20 2f 2a    173,  174,. /*
2bf9c 20 20 20 37 34 30 20 2a 2f 20 20 20 31 37 33 2c     740 */   173,
2bf9d 20 20 31 37 34 2c 20 20 20 36 33 2c 20 20 20 36    174,   63,   6
2bf9e 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2bf9f 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2bfa0 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 37 35 30     70,. /*   750
2bfa1 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37 32 2c   */    71,   72,
2bfa2 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bfa3 35 2c 20 20 31 35 30 2c 20 20 20 37 37 2c 20 20  5,  150,   77,  
2bfa4 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
2bfa5 0a 20 2f 2a 20 20 20 37 36 30 20 2a 2f 20 20 20  . /*   760 */   
2bfa6 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2bfa7 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bfa8 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 20 20  6,   87,   19,  
2bfa9 31 35 30 2c 20 20 31 34 34 2c 0a 20 2f 2a 20 20  150,  144,. /*  
2bfaa 20 37 37 30 20 2a 2f 20 20 20 31 34 35 2c 20 20   770 */   145,  
2bfab 31 35 39 2c 20 20 31 35 30 2c 20 20 31 36 34 2c  159,  150,  164,
2bfac 20 20 31 35 30 2c 20 20 31 31 33 2c 20 20 31 35    150,  113,  15
2bfad 30 2c 20 20 20 32 32 2c 20 20 31 37 33 2c 20 20  0,   22,  173,  
2bfae 31 37 34 2c 0a 20 2f 2a 20 20 20 37 38 30 20 2a  174,. /*   780 *
2bfaf 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  /   150,  150,  
2bfb0 31 37 33 2c 20 20 31 35 30 2c 20 20 20 32 35 2c  173,  150,   25,
2bfb1 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35    150,  150,  15
2bfb2 30 2c 20 20 20 32 32 2c 20 20 31 32 37 2c 0a 20  0,   22,  127,. 
2bfb3 2f 2a 20 20 20 37 39 30 20 2a 2f 20 20 20 20 32  /*   790 */    2
2bfb4 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  4,  173,  174,  
2bfb5 20 34 35 2c 20 20 20 34 36 2c 20 20 31 37 33 2c   45,   46,  173,
2bfb6 20 20 31 37 34 2c 20 20 31 37 33 2c 20 20 31 37    174,  173,  17
2bfb7 34 2c 20 20 31 37 33 2c 0a 20 2f 2a 20 20 20 38  4,  173,. /*   8
2bfb8 30 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 31  00 */   174,  11
2bfb9 38 2c 20 20 20 31 37 2c 20 20 31 37 33 2c 20 20  8,   17,  173,  
2bfba 31 37 34 2c 20 20 31 39 33 2c 20 20 31 37 33 2c  174,  193,  173,
2bfbb 20 20 31 37 34 2c 20 20 31 35 30 2c 20 20 31 37    174,  150,  17
2bfbc 33 2c 0a 20 2f 2a 20 20 20 38 31 30 20 2a 2f 20  3,. /*   810 */ 
2bfbd 20 20 31 37 34 2c 20 20 20 36 33 2c 20 20 20 36    174,   63,   6
2bfbe 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2bfbf 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2bfc0 20 20 20 37 30 2c 20 20 20 37 31 2c 0a 20 2f 2a     70,   71,. /*
2bfc1 20 20 20 38 32 30 20 2a 2f 20 20 20 20 37 32 2c     820 */    72,
2bfc2 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bfc3 35 2c 20 20 31 35 30 2c 20 20 20 37 37 2c 20 20  5,  150,   77,  
2bfc4 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
2bfc5 20 20 20 38 31 2c 0a 20 2f 2a 20 20 20 38 33 30     81,. /*   830
2bfc6 20 2a 2f 20 20 20 20 38 32 2c 20 20 20 38 33 2c   */    82,   83,
2bfc7 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bfc8 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 20 20  6,   87,   19,  
2bfc9 31 35 30 2c 20 20 32 34 35 2c 20 20 32 34 36 2c  150,  245,  246,
2bfca 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20 20 20  . /*   840 */   
2bfcb 20 35 35 2c 20 20 31 35 30 2c 20 20 20 35 37 2c   55,  150,   57,
2bfcc 20 20 31 35 30 2c 20 20 20 38 33 2c 20 20 31 35    150,   83,  15
2bfcd 30 2c 20 20 31 35 30 2c 20 20 31 37 33 2c 20 20  0,  150,  173,  
2bfce 31 37 34 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  174,  150,. /*  
2bfcf 20 38 35 30 20 2a 2f 20 20 20 31 39 32 2c 20 20   850 */   192,  
2bfd0 20 39 32 2c 20 20 31 35 30 2c 20 20 32 31 36 2c   92,  150,  216,
2bfd1 20 20 31 35 30 2c 20 20 32 30 35 2c 20 20 32 30    150,  205,  20
2bfd2 36 2c 20 20 31 35 30 2c 20 20 31 32 38 2c 20 20  6,  150,  128,  
2bfd3 31 32 39 2c 0a 20 2f 2a 20 20 20 38 36 30 20 2a  129,. /*   860 *
2bfd4 2f 20 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  /   173,  174,  
2bfd5 20 34 35 2c 20 20 20 34 36 2c 20 20 31 37 33 2c   45,   46,  173,
2bfd6 20 20 31 37 34 2c 20 20 31 37 33 2c 20 20 31 37    174,  173,  17
2bfd7 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 0a 20  4,  173,  174,. 
2bfd8 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20 20 31 31  /*   870 */   11
2bfd9 35 2c 20 20 31 31 30 2c 20 20 31 37 33 2c 20 20  5,  110,  173,  
2bfda 31 37 34 2c 20 20 31 36 35 2c 20 20 31 31 34 2c  174,  165,  114,
2bfdb 20 20 31 31 37 2c 20 20 31 37 33 2c 20 20 31 37    117,  173,  17
2bfdc 34 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 38  4,  150,. /*   8
2bfdd 38 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20 36  80 */    63,   6
2bfde 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2bfdf 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2bfe0 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2bfe1 32 2c 0a 20 2f 2a 20 20 20 38 39 30 20 2a 2f 20  2,. /*   890 */ 
2bfe2 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2bfe3 35 2c 20 20 31 35 30 2c 20 20 20 37 37 2c 20 20  5,  150,   77,  
2bfe4 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
2bfe5 20 20 20 38 31 2c 20 20 20 38 32 2c 0a 20 2f 2a     81,   82,. /*
2bfe6 20 20 20 39 30 30 20 2a 2f 20 20 20 20 38 33 2c     900 */    83,
2bfe7 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2bfe8 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 20 20  6,   87,   19,  
2bfe9 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  150,  150,  150,
2bfea 20 20 31 38 32 2c 0a 20 2f 2a 20 20 20 39 31 30    182,. /*   910
2bfeb 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 35 39 2c   */   150,  159,
2bfec 20 20 31 35 30 2c 20 20 32 30 34 2c 20 20 31 35    150,  204,  15
2bfed 30 2c 20 20 31 38 36 2c 20 20 31 37 33 2c 20 20  0,  186,  173,  
2bfee 31 37 34 2c 20 20 31 35 30 2c 20 20 20 33 30 2c  174,  150,   30,
2bfef 0a 20 2f 2a 20 20 20 39 32 30 20 2a 2f 20 20 20  . /*   920 */   
2bff0 20 31 39 2c 20 20 31 35 30 2c 20 20 31 33 34 2c   19,  150,  134,
2bff1 20 20 31 35 30 2c 20 20 31 33 36 2c 20 20 31 35    150,  136,  15
2bff2 30 2c 20 20 20 33 37 2c 20 20 20 20 37 2c 20 20  0,   37,    7,  
2bff3 20 20 38 2c 20 20 31 37 33 2c 0a 20 2f 2a 20 20    8,  173,. /*  
2bff4 20 39 33 30 20 2a 2f 20 20 20 31 37 34 2c 20 20   930 */   174,  
2bff5 20 34 35 2c 20 20 20 34 36 2c 20 20 31 37 33 2c   45,   46,  173,
2bff6 20 20 31 37 34 2c 20 20 31 37 33 2c 20 20 31 37    174,  173,  17
2bff7 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  4,  173,  174,  
2bff8 31 36 35 2c 0a 20 2f 2a 20 20 20 39 34 30 20 2a  165,. /*   940 *
2bff9 2f 20 20 20 31 35 30 2c 20 20 31 37 33 2c 20 20  /   150,  173,  
2bffa 31 37 34 2c 20 20 31 38 36 2c 20 20 31 38 36 2c  174,  186,  186,
2bffb 20 20 31 39 33 2c 20 20 31 37 33 2c 20 20 31 37    193,  173,  17
2bffc 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 0a 20  4,  173,  174,. 
2bffd 2f 2a 20 20 20 39 35 30 20 2a 2f 20 20 20 20 36  /*   950 */    6
2bffe 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  4,   65,   66,  
2bfff 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2c000 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2c001 32 2c 20 20 20 37 33 2c 0a 20 2f 2a 20 20 20 39  2,   73,. /*   9
2c002 36 30 20 2a 2f 20 20 20 20 37 34 2c 20 20 20 37  60 */    74,   7
2c003 35 2c 20 20 31 35 30 2c 20 20 20 37 37 2c 20 20  5,  150,   77,  
2c004 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
2c005 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2c006 33 2c 0a 20 2f 2a 20 20 20 39 37 30 20 2a 2f 20  3,. /*   970 */ 
2c007 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2c008 36 2c 20 20 20 38 37 2c 20 20 20 31 39 2c 20 20  6,   87,   19,  
2c009 31 35 30 2c 20 20 31 35 30 2c 20 20 20 32 31 2c  150,  150,   21,
2c00a 20 20 32 30 34 2c 20 20 31 35 30 2c 0a 20 2f 2a    204,  150,. /*
2c00b 20 20 20 39 38 30 20 2a 2f 20 20 20 20 32 33 2c     980 */    23,
2c00c 20 20 31 35 30 2c 20 20 20 32 35 2c 20 20 31 35    150,   25,  15
2c00d 30 2c 20 20 31 35 30 2c 20 20 31 37 33 2c 20 20  0,  150,  173,  
2c00e 31 37 34 2c 20 20 31 35 30 2c 20 20 20 32 33 2c  174,  150,   23,
2c00f 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 39 39 30     23,. /*   990
2c010 20 2a 2f 20 20 20 20 32 35 2c 20 20 20 32 35 2c   */    25,   25,
2c011 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20 31 35    150,   23,  15
2c012 30 2c 20 20 20 32 35 2c 20 20 20 39 35 2c 20 20  0,   25,   95,  
2c013 31 35 30 2c 20 20 31 37 33 2c 20 20 31 37 34 2c  150,  173,  174,
2c014 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20 20 20  . /*  1000 */   
2c015 20 34 35 2c 20 20 20 34 36 2c 20 20 31 37 33 2c   45,   46,  173,
2c016 20 20 31 37 34 2c 20 20 31 37 33 2c 20 20 31 37    174,  173,  17
2c017 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  4,  173,  174,  
2c018 20 34 36 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20   46,  150,. /*  
2c019 31 30 31 30 20 2a 2f 20 20 20 31 37 33 2c 20 20  1010 */   173,  
2c01a 31 37 34 2c 20 20 31 38 36 2c 20 20 31 35 30 2c  174,  186,  150,
2c01b 20 20 20 35 38 2c 20 20 31 37 33 2c 20 20 31 37     58,  173,  17
2c01c 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  4,  173,  174,  
2c01d 31 35 30 2c 0a 20 2f 2a 20 20 31 30 32 30 20 2a  150,. /*  1020 *
2c01e 2f 20 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  /    65,   66,  
2c01f 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2c020 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2c021 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 0a 20  2,   73,   74,. 
2c022 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20 20 20 37  /*  1030 */    7
2c023 35 2c 20 20 31 35 30 2c 20 20 20 37 37 2c 20 20  5,  150,   77,  
2c024 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
2c025 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2c026 33 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 31 30  3,   84,. /*  10
2c027 34 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 20 38  40 */    85,   8
2c028 36 2c 20 20 20 38 37 2c 20 20 20 20 35 2c 20 20  6,   87,    5,  
2c029 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 39 2c  150,  150,  159,
2c02a 20 20 31 35 39 2c 20 20 20 31 30 2c 20 20 20 31    159,   10,   1
2c02b 31 2c 0a 20 2f 2a 20 20 31 30 35 30 20 2a 2f 20  1,. /*  1050 */ 
2c02c 20 20 20 31 32 2c 20 20 20 31 33 2c 20 20 20 31     12,   13,   1
2c02d 34 2c 20 20 20 39 37 2c 20 20 20 31 36 2c 20 20  4,   97,   16,  
2c02e 31 39 32 2c 20 20 20 32 32 2c 20 20 20 39 34 2c  192,   22,   94,
2c02f 20 20 20 39 35 2c 20 20 20 31 39 2c 0a 20 2f 2a     95,   19,. /*
2c030 20 20 31 30 36 30 20 2a 2f 20 20 20 20 32 30 2c    1060 */    20,
2c031 20 20 31 39 32 2c 20 20 20 32 32 2c 20 20 31 30    192,   22,  10
2c032 31 2c 20 20 20 32 33 2c 20 20 31 35 30 2c 20 20  1,   23,  150,  
2c033 20 32 36 2c 20 20 20 32 39 2c 20 20 20 32 33 2c   26,   29,   23,
2c034 20 20 20 33 31 2c 0a 20 2f 2a 20 20 31 30 37 30     31,. /*  1070
2c035 20 2a 2f 20 20 20 20 32 35 2c 20 20 31 35 30 2c   */    25,  150,
2c036 20 20 31 38 32 2c 20 20 31 39 32 2c 20 20 20 33    182,  192,   3
2c037 34 2c 20 20 20 32 33 2c 20 20 20 33 38 2c 20 20  4,   23,   38,  
2c038 20 32 35 2c 20 20 31 38 32 2c 20 20 31 35 30 2c   25,  182,  150,
2c039 0a 20 2f 2a 20 20 31 30 38 30 20 2a 2f 20 20 20  . /*  1080 */   
2c03a 31 39 33 2c 20 20 31 39 33 2c 20 20 20 32 33 2c  193,  193,   23,
2c03b 20 20 20 32 33 2c 20 20 20 32 35 2c 20 20 20 32     23,   25,   2
2c03c 35 2c 20 20 31 39 32 2c 20 20 31 39 32 2c 20 20  5,  192,  192,  
2c03d 20 35 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20   50,  150,. /*  
2c03e 31 30 39 30 20 2a 2f 20 20 20 20 35 32 2c 20 20  1090 */    52,  
2c03f 20 35 31 2c 20 20 31 35 30 2c 20 20 31 33 37 2c   51,  150,  137,
2c040 20 20 20 35 36 2c 20 20 31 35 30 2c 20 20 31 35     56,  150,  15
2c041 30 2c 20 20 20 35 39 2c 20 20 20 32 33 2c 20 20  0,   59,   23,  
2c042 32 33 35 2c 0a 20 2f 2a 20 20 31 31 30 30 20 2a  235,. /*  1100 *
2c043 2f 20 20 20 20 32 35 2c 20 20 20 36 31 2c 20 20  /    25,   61,  
2c044 32 33 36 2c 20 20 20 36 32 2c 20 20 20 32 33 2c  236,   62,   23,
2c045 20 20 20 37 31 2c 20 20 20 32 35 2c 20 20 31 35     71,   25,  15
2c046 33 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 0a 20  3,  150,  150,. 
2c047 2f 2a 20 20 31 31 31 30 20 2a 2f 20 20 20 31 35  /*  1110 */   15
2c048 30 2c 20 20 31 35 30 2c 20 20 31 39 35 2c 20 20  0,  150,  195,  
2c049 31 39 37 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  197,  150,  165,
2c04a 20 20 32 33 32 2c 20 20 31 35 30 2c 20 20 31 35    232,  150,  15
2c04b 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 31  0,  150,. /*  11
2c04c 32 30 20 2a 2f 20 20 20 32 32 37 2c 20 20 20 38  20 */   227,   8
2c04d 31 2c 20 20 20 38 32 2c 20 20 31 35 30 2c 20 20  1,   82,  150,  
2c04e 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  150,  150,  150,
2c04f 20 20 31 35 30 2c 20 20 31 37 36 2c 20 20 32 33    150,  176,  23
2c050 32 2c 0a 20 2f 2a 20 20 31 31 33 30 20 2a 2f 20  2,. /*  1130 */ 
2c051 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
2c052 32 2c 20 20 32 30 38 2c 20 20 31 38 31 2c 20 20  2,  208,  181,  
2c053 20 39 35 2c 20 20 31 39 38 2c 20 20 20 20 36 2c   95,  198,    6,
2c054 20 20 32 30 38 2c 20 20 32 30 38 2c 0a 20 2f 2a    208,  208,. /*
2c055 20 20 31 31 34 30 20 2a 2f 20 20 20 31 30 32 2c    1140 */   102,
2c056 20 20 31 30 33 2c 20 20 31 30 34 2c 20 20 31 34    103,  104,  14
2c057 39 2c 20 20 31 36 35 2c 20 20 31 39 38 2c 20 20  9,  165,  198,  
2c058 31 30 38 2c 20 20 31 37 36 2c 20 20 31 37 36 2c  108,  176,  176,
2c059 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 31 35 30    165,. /*  1150
2c05a 20 2a 2f 20 20 20 31 34 39 2c 20 20 31 31 33 2c   */   149,  113,
2c05b 20 20 31 34 39 2c 20 20 31 31 33 2c 20 20 31 37    149,  113,  17
2c05c 36 2c 20 20 31 34 39 2c 20 20 31 37 36 2c 20 20  6,  149,  176,  
2c05d 20 31 33 2c 20 20 31 37 37 2c 20 20 31 35 31 2c   13,  177,  151,
2c05e 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20 20 20  . /*  1160 */   
2c05f 20 31 39 2c 20 20 20 32 30 2c 20 20 31 35 31 2c   19,   20,  151,
2c060 20 20 20 32 32 2c 20 20 20 32 35 2c 20 20 31 35     22,   25,  15
2c061 38 2c 20 20 31 32 32 2c 20 20 20 32 36 2c 20 20  8,  122,   26,  
2c062 31 32 38 2c 20 20 31 32 39 2c 0a 20 2f 2a 20 20  128,  129,. /*  
2c063 31 31 37 30 20 2a 2f 20 20 20 31 33 30 2c 20 20  1170 */   130,  
2c064 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c  131,  132,  133,
2c065 20 20 31 32 35 2c 20 20 20 33 34 2c 20 20 31 33    125,   34,  13
2c066 38 2c 20 20 31 32 33 2c 20 20 31 39 33 2c 20 20  8,  123,  193,  
2c067 31 32 31 2c 0a 20 2f 2a 20 20 31 31 38 30 20 2a  121,. /*  1180 *
2c068 2f 20 20 20 31 39 38 2c 20 20 31 32 34 2c 20 20  /   198,  124,  
2c069 31 39 39 2c 20 20 20 31 39 2c 20 20 20 32 30 2c  199,   19,   20,
2c06a 20 20 31 33 34 2c 20 20 20 32 32 2c 20 20 32 30    134,   22,  20
2c06b 30 2c 20 20 32 30 32 2c 20 20 32 30 31 2c 0a 20  0,  202,  201,. 
2c06c 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20 20 20 32  /*  1190 */    2
2c06d 36 2c 20 20 31 31 35 2c 20 20 20 35 31 2c 20 20  6,  115,   51,  
2c06e 31 35 36 2c 20 20 31 30 31 2c 20 20 31 35 36 2c  156,  101,  156,
2c06f 20 20 31 30 31 2c 20 20 31 32 30 2c 20 20 20 33    101,  120,   3
2c070 34 2c 20 20 32 31 30 2c 0a 20 2f 2a 20 20 31 32  4,  210,. /*  12
2c071 30 30 20 2a 2f 20 20 20 32 30 39 2c 20 20 32 30  00 */   209,  20
2c072 39 2c 20 20 20 36 31 2c 20 20 32 31 30 2c 20 20  9,   61,  210,  
2c073 32 30 39 2c 20 20 31 37 35 2c 20 20 32 31 30 2c  209,  175,  210,
2c074 20 20 31 37 35 2c 20 20 20 34 33 2c 20 20 31 38    175,   43,  18
2c075 33 2c 0a 20 2f 2a 20 20 31 32 31 30 20 2a 2f 20  3,. /*  1210 */ 
2c076 20 20 31 37 35 2c 20 20 31 30 30 2c 20 20 20 32    175,  100,   2
2c077 32 2c 20 20 31 37 37 2c 20 20 20 38 37 2c 20 20  2,  177,   87,  
2c078 20 35 31 2c 20 20 32 32 36 2c 20 20 31 37 35 2c   51,  226,  175,
2c079 20 20 20 31 38 2c 20 20 31 35 35 2c 0a 20 2f 2a     18,  155,. /*
2c07a 20 20 31 32 32 30 20 2a 2f 20 20 20 31 35 35 2c    1220 */   155,
2c07b 20 20 31 37 38 2c 20 20 20 38 31 2c 20 20 20 38    178,   81,   8
2c07c 32 2c 20 20 20 38 33 2c 20 20 20 36 31 2c 20 20  2,   83,   61,  
2c07d 32 32 39 2c 20 20 32 32 39 2c 20 20 31 37 35 2c  229,  229,  175,
2c07e 20 20 31 38 33 2c 0a 20 2f 2a 20 20 31 32 33 30    183,. /*  1230
2c07f 20 2a 2f 20 20 20 31 37 35 2c 20 20 20 39 30 2c   */   175,   90,
2c080 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 31 37     91,   92,  17
2c081 38 2c 20 20 31 35 36 2c 20 20 20 39 35 2c 20 20  8,  156,   95,  
2c082 31 37 35 2c 20 20 31 35 36 2c 20 20 31 35 35 2c  175,  156,  155,
2c083 0a 20 2f 2a 20 20 31 32 34 30 20 2a 2f 20 20 20  . /*  1240 */   
2c084 20 34 31 2c 20 20 31 35 36 2c 20 20 31 35 36 2c   41,  156,  156,
2c085 20 20 31 33 34 2c 20 20 31 35 35 2c 20 20 20 38    134,  155,   8
2c086 31 2c 20 20 20 38 32 2c 20 20 32 33 37 2c 20 20  1,   82,  237,  
2c087 31 35 36 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20  156,   63,. /*  
2c088 31 32 35 30 20 2a 2f 20 20 20 32 33 38 2c 20 20  1250 */   238,  
2c089 31 38 38 2c 20 20 20 32 32 2c 20 20 31 35 36 2c  188,   22,  156,
2c08a 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
2c08b 32 2c 20 20 31 38 38 2c 20 20 20 31 38 2c 20 20  2,  188,   18,  
2c08c 20 39 35 2c 0a 20 2f 2a 20 20 31 32 36 30 20 2a   95,. /*  1260 *
2c08d 2f 20 20 20 31 35 36 2c 20 20 31 39 31 2c 20 20  /   156,  191,  
2c08e 31 39 31 2c 20 20 31 39 31 2c 20 20 31 39 38 2c  191,  191,  198,
2c08f 20 20 32 31 38 2c 20 20 31 39 38 2c 20 20 20 33    218,  198,   3
2c090 36 2c 20 20 31 38 38 2c 20 20 31 32 38 2c 0a 20  6,  188,  128,. 
2c091 2f 2a 20 20 31 32 37 30 20 2a 2f 20 20 20 31 32  /*  1270 */   12
2c092 39 2c 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20  9,  130,  131,  
2c093 31 33 32 2c 20 20 31 33 33 2c 20 20 31 35 36 2c  132,  133,  156,
2c094 20 20 31 35 36 2c 20 20 31 34 30 2c 20 20 31 39    156,  140,  19
2c095 31 2c 20 20 32 31 38 2c 0a 20 2f 2a 20 20 31 32  1,  218,. /*  12
2c096 38 30 20 2a 2f 20 20 20 31 36 33 2c 20 20 31 37  80 */   163,  17
2c097 39 2c 20 20 31 37 39 2c 20 20 20 20 31 2c 20 20  9,  179,    1,  
2c098 20 31 35 2c 20 20 20 32 33 2c 20 20 31 31 35 2c   15,   23,  115,
2c099 20 20 31 31 35 2c 20 20 32 34 30 2c 20 20 31 31    115,  240,  11
2c09a 35 2c 0a 20 2f 2a 20 20 31 32 39 30 20 2a 2f 20  5,. /*  1290 */ 
2c09b 20 20 32 34 30 2c 20 20 31 31 35 2c 20 20 31 32    240,  115,  12
2c09c 38 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20  8,  129,  130,  
2c09d 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c  131,  132,  133,
2c09e 20 20 20 39 35 2c 20 20 31 31 30 2c 0a 20 2f 2a     95,  110,. /*
2c09f 20 20 31 33 30 30 20 2a 2f 20 20 20 20 32 32 2c    1300 */    22,
2c0a0 20 20 20 31 31 2c 20 20 20 32 33 2c 20 20 20 32     11,   23,   2
2c0a1 33 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20  3,   23,   22,  
2c0a2 20 32 32 2c 20 20 31 31 39 2c 20 20 20 32 33 2c   22,  119,   23,
2c0a3 20 20 31 31 37 2c 0a 20 2f 2a 20 20 31 33 31 30    117,. /*  1310
2c0a4 20 2a 2f 20 20 20 32 34 33 2c 20 20 20 32 35 2c   */   243,   25,
2c0a5 20 20 20 32 33 2c 20 20 20 32 35 2c 20 20 31 31     23,   25,  11
2c0a6 39 2c 20 20 20 32 32 2c 20 20 20 32 35 2c 20 20  9,   22,   25,  
2c0a7 31 31 39 2c 20 20 20 32 33 2c 20 20 32 34 36 2c  119,   23,  246,
2c0a8 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20 20 20  . /*  1320 */   
2c0a9 31 31 38 2c 20 20 31 31 35 2c 20 20 20 32 33 2c  118,  115,   23,
2c0aa 20 20 32 34 39 2c 20 20 20 34 37 2c 20 20 20 32    249,   47,   2
2c0ab 32 2c 20 20 20 32 32 2c 20 20 20 33 35 2c 20 20  2,   22,   35,  
2c0ac 20 32 33 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20   23,   22,. /*  
2c0ad 31 33 33 30 20 2a 2f 20 20 20 20 32 32 2c 20 20  1330 */    22,  
2c0ae 20 32 32 2c 20 20 20 39 39 2c 20 20 31 30 36 2c   22,   99,  106,
2c0af 20 20 20 34 37 2c 20 20 20 31 39 2c 20 20 20 32     47,   19,   2
2c0b0 34 2c 20 20 20 32 30 2c 20 20 31 30 31 2c 20 20  4,   20,  101,  
2c0b1 20 33 39 2c 0a 20 2f 2a 20 20 31 33 34 30 20 2a   39,. /*  1340 *
2c0b2 2f 20 20 20 20 32 35 2c 20 20 20 34 37 2c 20 20  /    25,   47,  
2c0b3 31 33 37 2c 20 20 31 30 31 2c 20 20 20 32 32 2c  137,  101,   22,
2c0b4 20 20 20 34 38 2c 20 20 20 34 38 2c 20 20 20 20     48,   48,    
2c0b5 35 2c 20 20 20 20 31 2c 20 20 31 30 35 2c 0a 20  5,    1,  105,. 
2c0b6 2f 2a 20 20 31 33 35 30 20 2a 2f 20 20 20 20 35  /*  1350 */    5
2c0b7 34 2c 20 20 31 32 36 2c 20 20 20 32 32 2c 20 20  4,  126,   22,  
2c0b8 20 20 31 2c 20 20 31 31 36 2c 20 20 20 31 37 2c    1,  116,   17,
2c0b9 20 20 31 32 30 2c 20 20 20 32 30 2c 20 20 31 30    120,   20,  10
2c0ba 35 2c 20 20 31 31 36 2c 0a 20 2f 2a 20 20 31 33  5,  116,. /*  13
2c0bb 36 30 20 2a 2f 20 20 20 31 32 36 2c 20 20 20 32  60 */   126,   2
2c0bc 32 2c 20 20 31 32 37 2c 20 20 20 32 33 2c 20 20  2,  127,   23,  
2c0bd 20 31 35 2c 20 20 20 31 37 2c 20 20 20 36 30 2c   15,   17,   60,
2c0be 20 20 31 33 39 2c 20 20 20 32 32 2c 20 20 20 20    139,   22,    
2c0bf 33 2c 0a 20 2f 2a 20 20 31 33 37 30 20 2a 2f 20  3,. /*  1370 */ 
2c0c0 20 20 32 35 30 2c 20 20 20 20 34 2c 20 20 20 37    250,    4,   7
2c0c1 31 2c 20 20 20 37 31 2c 0a 7d 3b 0a 23 64 65 66  1,   71,.};.#def
2c0c2 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 55 53 45  ine YY_SHIFT_USE
2c0c3 5f 44 46 4c 54 20 28 2d 31 30 38 29 0a 23 64 65  _DFLT (-108).#de
2c0c4 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 4d 41  fine YY_SHIFT_MA
2c0c5 58 20 34 30 34 0a 73 74 61 74 69 63 20 63 6f 6e  X 404.static con
2c0c6 73 74 20 73 68 6f 72 74 20 79 79 5f 73 68 69 66  st short yy_shif
2c0c7 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f 2a  t_ofst[] = {. /*
2c0c8 20 20 20 20 20 30 20 2a 2f 20 20 20 20 32 33 2c       0 */    23,
2c0c9 20 31 30 33 38 2c 20 31 30 34 30 2c 20 20 2d 31   1038, 1040,  -1
2c0ca 39 2c 20 31 30 34 30 2c 20 31 31 36 34 2c 20 31  9, 1040, 1164, 1
2c0cb 31 36 34 2c 20 20 31 38 37 2c 20 20 20 37 38 2c  164,  187,   78,
2c0cc 20 20 32 34 33 2c 0a 20 2f 2a 20 20 20 20 31 30    243,. /*    10
2c0cd 20 2a 2f 20 20 20 31 31 39 2c 20 31 31 36 34 2c   */   119, 1164,
2c0ce 20 31 31 36 34 2c 20 31 31 36 34 2c 20 31 31 36   1164, 1164, 116
2c0cf 34 2c 20 31 31 36 34 2c 20 20 2d 34 38 2c 20 20  4, 1164,  -48,  
2c0d0 32 36 36 2c 20 20 33 34 37 2c 20 20 35 35 34 2c  266,  347,  554,
2c0d1 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20  . /*    20 */   
2c0d2 31 34 38 2c 20 20 20 31 39 2c 20 20 20 31 39 2c  148,   19,   19,
2c0d3 20 2d 31 30 37 2c 20 20 20 35 30 2c 20 20 31 38   -107,   50,  18
2c0d4 38 2c 20 20 32 35 37 2c 20 20 33 33 30 2c 20 20  8,  257,  330,  
2c0d5 34 30 33 2c 20 20 34 37 32 2c 0a 20 2f 2a 20 20  403,  472,. /*  
2c0d6 20 20 33 30 20 2a 2f 20 20 20 35 34 31 2c 20 20    30 */   541,  
2c0d7 36 31 30 2c 20 20 36 37 39 2c 20 20 37 34 38 2c  610,  679,  748,
2c0d8 20 20 38 31 37 2c 20 20 37 34 38 2c 20 20 37 34    817,  748,  74
2c0d9 38 2c 20 20 37 34 38 2c 20 20 37 34 38 2c 20 20  8,  748,  748,  
2c0da 37 34 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a  748,. /*    40 *
2c0db 2f 20 20 20 37 34 38 2c 20 20 37 34 38 2c 20 20  /   748,  748,  
2c0dc 37 34 38 2c 20 20 37 34 38 2c 20 20 37 34 38 2c  748,  748,  748,
2c0dd 20 20 37 34 38 2c 20 20 37 34 38 2c 20 20 37 34    748,  748,  74
2c0de 38 2c 20 20 37 34 38 2c 20 20 37 34 38 2c 0a 20  8,  748,  748,. 
2c0df 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 37 34  /*    50 */   74
2c0e0 38 2c 20 20 37 34 38 2c 20 20 37 34 38 2c 20 20  8,  748,  748,  
2c0e1 38 38 36 2c 20 20 39 35 35 2c 20 20 39 35 35 2c  886,  955,  955,
2c0e2 20 31 31 34 31 2c 20 31 31 36 34 2c 20 31 31 36   1141, 1164, 116
2c0e3 34 2c 20 31 31 36 34 2c 0a 20 2f 2a 20 20 20 20  4, 1164,. /*    
2c0e4 36 30 20 2a 2f 20 20 31 31 36 34 2c 20 31 31 36  60 */  1164, 116
2c0e5 34 2c 20 31 31 36 34 2c 20 31 31 36 34 2c 20 31  4, 1164, 1164, 1
2c0e6 31 36 34 2c 20 31 31 36 34 2c 20 31 31 36 34 2c  164, 1164, 1164,
2c0e7 20 31 31 36 34 2c 20 31 31 36 34 2c 20 31 31 36   1164, 1164, 116
2c0e8 34 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20  4,. /*    70 */ 
2c0e9 20 31 31 36 34 2c 20 31 31 36 34 2c 20 31 31 36   1164, 1164, 116
2c0ea 34 2c 20 31 31 36 34 2c 20 31 31 36 34 2c 20 31  4, 1164, 1164, 1
2c0eb 31 36 34 2c 20 31 31 36 34 2c 20 31 31 36 34 2c  164, 1164, 1164,
2c0ec 20 31 31 36 34 2c 20 31 31 36 34 2c 0a 20 2f 2a   1164, 1164,. /*
2c0ed 20 20 20 20 38 30 20 2a 2f 20 20 31 31 36 34 2c      80 */  1164,
2c0ee 20 31 31 36 34 2c 20 31 31 36 34 2c 20 31 31 36   1164, 1164, 116
2c0ef 34 2c 20 31 31 36 34 2c 20 31 31 36 34 2c 20 31  4, 1164, 1164, 1
2c0f0 31 36 34 2c 20 31 31 36 34 2c 20 31 31 36 34 2c  164, 1164, 1164,
2c0f1 20 31 31 36 34 2c 0a 20 2f 2a 20 20 20 20 39 30   1164,. /*    90
2c0f2 20 2a 2f 20 20 31 31 36 34 2c 20 31 31 36 34 2c   */  1164, 1164,
2c0f3 20 31 31 36 34 2c 20 31 31 36 34 2c 20 31 31 36   1164, 1164, 116
2c0f4 34 2c 20 31 31 36 34 2c 20 31 31 36 34 2c 20 20  4, 1164, 1164,  
2c0f5 2d 36 34 2c 20 20 2d 36 34 2c 20 20 20 2d 31 2c  -64,  -64,   -1,
2c0f6 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20  . /*   100 */   
2c0f7 20 2d 31 2c 20 20 20 35 32 2c 20 20 20 20 36 2c   -1,   52,    6,
2c0f8 20 20 31 36 30 2c 20 20 34 30 30 2c 20 20 39 35    160,  400,  95
2c0f9 36 2c 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20  6,  554,  554,  
2c0fa 20 32 35 2c 20 20 31 34 38 2c 0a 20 2f 2a 20 20   25,  148,. /*  
2c0fb 20 31 31 30 20 2a 2f 20 20 20 2d 37 35 2c 20 2d   110 */   -75, -
2c0fc 31 30 38 2c 20 2d 31 30 38 2c 20 2d 31 30 38 2c  108, -108, -108,
2c0fd 20 20 20 38 30 2c 20 20 31 32 34 2c 20 20 33 33     80,  124,  33
2c0fe 38 2c 20 20 33 33 38 2c 20 20 35 30 34 2c 20 20  8,  338,  504,  
2c0ff 35 34 30 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a  540,. /*   120 *
2c100 2f 20 20 20 36 34 37 2c 20 20 35 37 34 2c 20 20  /   647,  574,  
2c101 35 35 34 2c 20 20 35 37 34 2c 20 20 35 37 34 2c  554,  574,  574,
2c102 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20 35 35    554,  554,  55
2c103 34 2c 20 20 35 35 34 2c 20 20 35 35 34 2c 0a 20  4,  554,  554,. 
2c104 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 35 35  /*   130 */   55
2c105 34 2c 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20  4,  554,  554,  
2c106 35 35 34 2c 20 20 35 35 34 2c 20 20 35 35 34 2c  554,  554,  554,
2c107 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20 35 35    554,  554,  55
2c108 34 2c 20 20 35 35 34 2c 0a 20 2f 2a 20 20 20 31  4,  554,. /*   1
2c109 34 30 20 2a 2f 20 20 20 35 35 34 2c 20 20 35 35  40 */   554,  55
2c10a 34 2c 20 20 35 34 35 2c 20 20 36 36 32 2c 20 2d  4,  545,  662, -
2c10b 31 30 37 2c 20 2d 31 30 37 2c 20 2d 31 30 37 2c  107, -107, -107,
2c10c 20 2d 31 30 38 2c 20 2d 31 30 38 2c 20 2d 31 30   -108, -108, -10
2c10d 38 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20  8,. /*   150 */ 
2c10e 20 20 2d 32 31 2c 20 20 2d 32 31 2c 20 20 31 33    -21,  -21,  13
2c10f 31 2c 20 20 32 31 32 2c 20 20 34 37 30 2c 20 20  1,  212,  470,  
2c110 34 31 38 2c 20 20 34 37 39 2c 20 20 34 38 34 2c  418,  479,  484,
2c111 20 20 35 35 37 2c 20 20 35 34 34 2c 0a 20 2f 2a    557,  544,. /*
2c112 20 20 20 31 36 30 20 2a 2f 20 20 20 35 34 36 2c     160 */   546,
2c113 20 20 34 38 32 2c 20 20 35 34 38 2c 20 20 35 35    482,  548,  55
2c114 32 2c 20 20 35 35 39 2c 20 20 36 34 35 2c 20 20  2,  559,  645,  
2c115 35 35 34 2c 20 20 35 35 34 2c 20 20 35 35 34 2c  554,  554,  554,
2c116 20 20 35 35 34 2c 0a 20 2f 2a 20 20 20 31 37 30    554,. /*   170
2c117 20 2a 2f 20 20 20 35 35 34 2c 20 20 37 36 31 2c   */   554,  761,
2c118 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20 36 34    554,  554,  64
2c119 32 2c 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20  2,  554,  554,  
2c11a 37 35 39 2c 20 20 35 35 34 2c 20 20 35 35 34 2c  759,  554,  554,
2c11b 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20  . /*   180 */   
2c11c 35 35 34 2c 20 20 35 35 34 2c 20 20 35 35 34 2c  554,  554,  554,
2c11d 20 20 34 36 33 2c 20 20 34 36 33 2c 20 20 34 36    463,  463,  46
2c11e 33 2c 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20  3,  554,  554,  
2c11f 35 35 34 2c 20 20 34 31 39 2c 0a 20 2f 2a 20 20  554,  419,. /*  
2c120 20 31 39 30 20 2a 2f 20 20 20 35 35 34 2c 20 20   190 */   554,  
2c121 35 35 34 2c 20 20 34 31 39 2c 20 20 35 35 34 2c  554,  419,  554,
2c122 20 20 36 38 36 2c 20 20 35 39 37 2c 20 20 35 35    686,  597,  55
2c123 34 2c 20 20 35 35 34 2c 20 20 34 31 39 2c 20 20  4,  554,  419,  
2c124 35 35 34 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a  554,. /*   200 *
2c125 2f 20 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20  /   554,  554,  
2c126 34 31 39 2c 20 20 35 35 34 2c 20 20 35 35 34 2c  419,  554,  554,
2c127 20 20 35 35 34 2c 20 20 34 31 39 2c 20 20 34 31    554,  419,  41
2c128 39 2c 20 20 35 35 34 2c 20 20 35 35 34 2c 0a 20  9,  554,  554,. 
2c129 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 35 35  /*   210 */   55
2c12a 34 2c 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20  4,  554,  554,  
2c12b 37 36 36 2c 20 20 33 32 37 2c 20 20 32 30 31 2c  766,  327,  201,
2c12c 20 20 31 34 38 2c 20 20 37 33 30 2c 20 20 37 33    148,  730,  73
2c12d 30 2c 20 20 37 38 38 2c 0a 20 2f 2a 20 20 20 32  0,  788,. /*   2
2c12e 32 30 20 2a 2f 20 20 20 38 38 39 2c 20 20 38 38  20 */   889,  88
2c12f 39 2c 20 20 36 38 33 2c 20 20 38 38 39 2c 20 20  9,  683,  889,  
2c130 39 36 32 2c 20 20 38 38 39 2c 20 20 31 34 38 2c  962,  889,  148,
2c131 20 20 38 38 39 2c 20 20 31 34 38 2c 20 20 31 31    889,  148,  11
2c132 35 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20  5,. /*   230 */ 
2c133 20 20 20 33 32 2c 20 20 36 38 33 2c 20 20 36 38     32,  683,  68
2c134 33 2c 20 20 20 33 32 2c 20 31 31 33 31 2c 20 31  3,   32, 1131, 1
2c135 31 33 31 2c 20 31 31 33 31 2c 20 31 31 33 31 2c  131, 1131, 1131,
2c136 20 31 31 34 34 2c 20 31 31 34 34 2c 0a 20 2f 2a   1144, 1144,. /*
2c137 20 20 20 32 34 30 20 2a 2f 20 20 31 31 33 39 2c     240 */  1139,
2c138 20 2d 31 30 37 2c 20 31 30 34 39 2c 20 31 30 34   -107, 1049, 104
2c139 34 2c 20 31 30 35 34 2c 20 31 30 35 38 2c 20 31  4, 1054, 1058, 1
2c13a 30 35 37 2c 20 31 30 35 31 2c 20 31 30 37 36 2c  057, 1051, 1076,
2c13b 20 31 30 37 36 2c 0a 20 2f 2a 20 20 20 32 35 30   1076,. /*   250
2c13c 20 2a 2f 20 20 31 30 39 33 2c 20 31 30 37 37 2c   */  1093, 1077,
2c13d 20 31 30 39 33 2c 20 31 30 37 37 2c 20 31 30 39   1093, 1077, 109
2c13e 33 2c 20 31 30 37 37 2c 20 31 30 39 35 2c 20 31  3, 1077, 1095, 1
2c13f 30 39 35 2c 20 31 31 36 35 2c 20 31 30 39 35 2c  095, 1165, 1095,
2c140 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 31  . /*   260 */  1
2c141 31 31 31 2c 20 31 30 39 35 2c 20 31 31 39 30 2c  111, 1095, 1190,
2c142 20 31 31 32 37 2c 20 31 31 32 37 2c 20 31 31 36   1127, 1127, 116
2c143 35 2c 20 31 30 39 35 2c 20 31 30 39 35 2c 20 31  5, 1095, 1095, 1
2c144 30 39 35 2c 20 31 31 39 30 2c 0a 20 2f 2a 20 20  095, 1190,. /*  
2c145 20 32 37 30 20 2a 2f 20 20 31 32 30 30 2c 20 31   270 */  1200, 1
2c146 30 37 36 2c 20 31 32 30 30 2c 20 31 30 37 36 2c  076, 1200, 1076,
2c147 20 31 32 30 30 2c 20 31 30 37 36 2c 20 31 30 37   1200, 1076, 107
2c148 36 2c 20 31 31 39 39 2c 20 31 31 30 39 2c 20 31  6, 1199, 1109, 1
2c149 32 30 30 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a  200,. /*   280 *
2c14a 2f 20 20 31 30 37 36 2c 20 31 31 38 36 2c 20 31  /  1076, 1186, 1
2c14b 31 38 36 2c 20 31 32 33 30 2c 20 31 30 34 39 2c  186, 1230, 1049,
2c14c 20 31 30 37 36 2c 20 31 32 34 30 2c 20 31 32 34   1076, 1240, 124
2c14d 30 2c 20 31 32 34 30 2c 20 31 32 34 30 2c 0a 20  0, 1240, 1240,. 
2c14e 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31 30 34  /*   290 */  104
2c14f 39 2c 20 31 31 38 36 2c 20 31 32 33 30 2c 20 31  9, 1186, 1230, 1
2c150 30 37 36 2c 20 31 32 33 31 2c 20 31 32 33 31 2c  076, 1231, 1231,
2c151 20 31 30 37 36 2c 20 31 30 37 36 2c 20 31 31 33   1076, 1076, 113
2c152 37 2c 20 2d 31 30 38 2c 0a 20 2f 2a 20 20 20 33  7, -108,. /*   3
2c153 30 30 20 2a 2f 20 20 2d 31 30 38 2c 20 2d 31 30  00 */  -108, -10
2c154 38 2c 20 2d 31 30 38 2c 20 2d 31 30 38 2c 20 20  8, -108, -108,  
2c155 34 36 36 2c 20 20 35 37 35 2c 20 20 36 31 37 2c  466,  575,  617,
2c156 20 20 37 38 35 2c 20 20 37 35 35 2c 20 20 39 30    785,  755,  90
2c157 31 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20  1,. /*   310 */ 
2c158 20 20 39 35 37 2c 20 20 39 36 35 2c 20 20 32 37    957,  965,  27
2c159 31 2c 20 20 39 32 30 2c 20 20 39 36 33 2c 20 20  1,  920,  963,  
2c15a 39 36 36 2c 20 20 39 37 30 2c 20 31 30 34 35 2c  966,  970, 1045,
2c15b 20 31 30 35 32 2c 20 31 30 35 39 2c 0a 20 2f 2a   1052, 1059,. /*
2c15c 20 20 20 33 32 30 20 2a 2f 20 20 31 30 36 30 2c     320 */  1060,
2c15d 20 31 30 37 35 2c 20 31 30 33 34 2c 20 31 30 38   1075, 1034, 108
2c15e 31 2c 20 31 30 34 31 2c 20 31 32 38 32 2c 20 31  1, 1041, 1282, 1
2c15f 32 36 39 2c 20 31 32 36 32 2c 20 31 31 37 31 2c  269, 1262, 1171,
2c160 20 31 31 37 32 2c 0a 20 2f 2a 20 20 20 33 33 30   1172,. /*   330
2c161 20 2a 2f 20 20 31 31 37 34 2c 20 31 31 37 36 2c   */  1174, 1176,
2c162 20 31 32 30 33 2c 20 31 31 38 39 2c 20 31 32 37   1203, 1189, 127
2c163 38 2c 20 31 32 37 39 2c 20 31 32 38 30 2c 20 31  8, 1279, 1280, 1
2c164 32 38 33 2c 20 31 32 39 30 2c 20 31 32 38 34 2c  283, 1290, 1284,
2c165 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 31  . /*   340 */  1
2c166 32 38 31 2c 20 31 32 38 36 2c 20 31 32 38 35 2c  281, 1286, 1285,
2c167 20 31 32 38 39 2c 20 31 32 38 38 2c 20 31 31 38   1289, 1288, 118
2c168 38 2c 20 31 32 39 31 2c 20 31 31 39 35 2c 20 31  8, 1291, 1195, 1
2c169 32 38 38 2c 20 31 31 39 32 2c 0a 20 2f 2a 20 20  288, 1192,. /*  
2c16a 20 33 35 30 20 2a 2f 20 20 31 32 39 33 2c 20 31   350 */  1293, 1
2c16b 31 39 38 2c 20 31 32 30 32 2c 20 31 32 30 36 2c  198, 1202, 1206,
2c16c 20 31 32 39 35 2c 20 31 32 39 39 2c 20 31 32 39   1295, 1299, 129
2c16d 32 2c 20 31 32 37 37 2c 20 31 33 30 33 2c 20 31  2, 1277, 1303, 1
2c16e 32 38 37 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a  287,. /*   360 *
2c16f 2f 20 20 31 33 30 34 2c 20 31 33 30 35 2c 20 31  /  1304, 1305, 1
2c170 33 30 37 2c 20 31 33 30 38 2c 20 31 32 39 34 2c  307, 1308, 1294,
2c171 20 31 33 30 39 2c 20 31 32 33 33 2c 20 31 32 32   1309, 1233, 122
2c172 37 2c 20 31 33 31 36 2c 20 31 33 31 37 2c 0a 20  7, 1316, 1317,. 
2c173 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 31 33 31  /*   370 */  131
2c174 32 2c 20 31 32 33 37 2c 20 31 33 30 30 2c 20 31  2, 1237, 1300, 1
2c175 32 39 36 2c 20 31 32 39 37 2c 20 31 33 31 35 2c  296, 1297, 1315,
2c176 20 31 32 39 38 2c 20 31 32 30 35 2c 20 31 32 34   1298, 1205, 124
2c177 32 2c 20 31 33 32 32 2c 0a 20 2f 2a 20 20 20 33  2, 1322,. /*   3
2c178 38 30 20 2a 2f 20 20 31 33 34 32 2c 20 31 33 34  80 */  1342, 134
2c179 37 2c 20 31 32 34 34 2c 20 31 33 30 31 2c 20 31  7, 1244, 1301, 1
2c17a 33 30 32 2c 20 31 32 32 35 2c 20 31 33 33 30 2c  302, 1225, 1330,
2c17b 20 31 32 33 38 2c 20 31 33 35 32 2c 20 31 33 33   1238, 1352, 133
2c17c 38 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20  8,. /*   390 */ 
2c17d 20 31 32 33 36 2c 20 31 33 33 37 2c 20 31 32 34   1236, 1337, 124
2c17e 33 2c 20 31 32 35 33 2c 20 31 32 33 34 2c 20 31  3, 1253, 1234, 1
2c17f 33 33 39 2c 20 31 32 33 35 2c 20 31 33 34 30 2c  339, 1235, 1340,
2c180 20 31 33 34 38 2c 20 31 33 30 36 2c 0a 20 2f 2a   1348, 1306,. /*
2c181 20 20 20 34 30 30 20 2a 2f 20 20 31 33 34 39 2c     400 */  1349,
2c182 20 31 32 32 38 2c 20 31 33 34 36 2c 20 31 33 36   1228, 1346, 136
2c183 36 2c 20 31 33 36 37 2c 0a 7d 3b 0a 23 64 65 66  6, 1367,.};.#def
2c184 69 6e 65 20 59 59 5f 52 45 44 55 43 45 5f 55 53  ine YY_REDUCE_US
2c185 45 5f 44 46 4c 54 20 28 2d 31 34 32 29 0a 23 64  E_DFLT (-142).#d
2c186 65 66 69 6e 65 20 59 59 5f 52 45 44 55 43 45 5f  efine YY_REDUCE_
2c187 4d 41 58 20 33 30 33 0a 73 74 61 74 69 63 20 63  MAX 303.static c
2c188 6f 6e 73 74 20 73 68 6f 72 74 20 79 79 5f 72 65  onst short yy_re
2c189 64 75 63 65 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a  duce_ofst[] = {.
2c18a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 2d 31   /*     0 */  -1
2c18b 34 31 2c 20 20 2d 34 39 2c 20 20 31 34 30 2c 20  41,  -49,  140, 
2c18c 20 20 37 37 2c 20 20 31 39 38 2c 20 20 20 36 37    77,  198,   67
2c18d 2c 20 20 20 37 35 2c 20 20 20 2d 38 2c 20 20 20  ,   75,   -8,   
2c18e 36 36 2c 20 20 32 32 35 2c 0a 20 2f 2a 20 20 20  66,  225,. /*   
2c18f 20 31 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 31   10 */   165,  1
2c190 34 32 2c 20 20 32 30 39 2c 20 20 32 37 37 2c 20  42,  209,  277, 
2c191 20 32 37 39 2c 20 20 32 38 31 2c 20 20 31 33 30   279,  281,  130
2c192 2c 20 20 32 38 36 2c 20 20 20 38 31 2c 20 20 32  ,  286,   81,  2
2c193 37 35 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f  75,. /*    20 */
2c194 20 20 20 31 39 36 2c 20 20 32 38 38 2c 20 20 32     196,  288,  2
2c195 39 33 2c 20 20 32 32 38 2c 20 20 20 38 33 2c 20  93,  228,   83, 
2c196 20 20 38 33 2c 20 20 20 38 33 2c 20 20 20 38 33    83,   83,   83
2c197 2c 20 20 20 38 33 2c 20 20 20 38 33 2c 0a 20 2f  ,   83,   83,. /
2c198 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 20 38 33  *    30 */    83
2c199 2c 20 20 20 38 33 2c 20 20 20 38 33 2c 20 20 20  ,   83,   83,   
2c19a 38 33 2c 20 20 20 38 33 2c 20 20 20 38 33 2c 20  83,   83,   83, 
2c19b 20 20 38 33 2c 20 20 20 38 33 2c 20 20 20 38 33    83,   83,   83
2c19c 2c 20 20 20 38 33 2c 0a 20 2f 2a 20 20 20 20 34  ,   83,. /*    4
2c19d 30 20 2a 2f 20 20 20 20 38 33 2c 20 20 20 38 33  0 */    83,   83
2c19e 2c 20 20 20 38 33 2c 20 20 20 38 33 2c 20 20 20  ,   83,   83,   
2c19f 38 33 2c 20 20 20 38 33 2c 20 20 20 38 33 2c 20  83,   83,   83, 
2c1a0 20 20 38 33 2c 20 20 20 38 33 2c 20 20 20 38 33    83,   83,   83
2c1a1 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20  ,. /*    50 */  
2c1a2 20 20 38 33 2c 20 20 20 38 33 2c 20 20 20 38 33    83,   83,   83
2c1a3 2c 20 20 20 38 33 2c 20 20 20 38 33 2c 20 20 20  ,   83,   83,   
2c1a4 38 33 2c 20 20 33 34 39 2c 20 20 34 36 37 2c 20  83,  349,  467, 
2c1a5 20 34 39 33 2c 20 20 35 33 36 2c 0a 20 2f 2a 20   493,  536,. /* 
2c1a6 20 20 20 36 30 20 2a 2f 20 20 20 35 35 35 2c 20     60 */   555, 
2c1a7 20 35 35 38 2c 20 20 35 36 31 2c 20 20 35 36 33   558,  561,  563
2c1a8 2c 20 20 35 36 35 2c 20 20 35 36 37 2c 20 20 36  ,  565,  567,  6
2c1a9 30 35 2c 20 20 36 31 38 2c 20 20 36 32 32 2c 20  05,  618,  622, 
2c1aa 20 36 32 34 2c 0a 20 2f 2a 20 20 20 20 37 30 20   624,. /*    70 
2c1ab 2a 2f 20 20 20 36 32 36 2c 20 20 36 33 30 2c 20  */   626,  630, 
2c1ac 20 36 33 33 2c 20 20 36 33 36 2c 20 20 36 37 34   633,  636,  674
2c1ad 2c 20 20 36 38 37 2c 20 20 36 39 31 2c 20 20 36  ,  687,  691,  6
2c1ae 39 33 2c 20 20 36 39 35 2c 20 20 36 39 39 2c 0a  93,  695,  699,.
2c1af 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 37   /*    80 */   7
2c1b0 30 34 2c 20 20 37 34 33 2c 20 20 37 35 36 2c 20  04,  743,  756, 
2c1b1 20 37 36 30 2c 20 20 37 36 32 2c 20 20 37 36 34   760,  762,  764
2c1b2 2c 20 20 37 36 38 2c 20 20 37 37 33 2c 20 20 37  ,  768,  773,  7
2c1b3 37 35 2c 20 20 38 31 32 2c 0a 20 2f 2a 20 20 20  75,  812,. /*   
2c1b4 20 39 30 20 2a 2f 20 20 20 38 32 35 2c 20 20 38   90 */   825,  8
2c1b5 32 39 2c 20 20 38 33 31 2c 20 20 38 33 33 2c 20  29,  831,  833, 
2c1b6 20 38 33 37 2c 20 20 38 34 32 2c 20 20 38 34 34   837,  842,  844
2c1b7 2c 20 20 20 38 33 2c 20 20 20 38 33 2c 20 20 20  ,   83,   83,   
2c1b8 38 33 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f  83,. /*   100 */
2c1b9 20 20 20 20 38 33 2c 20 20 2d 31 37 2c 20 20 20      83,  -17,   
2c1ba 38 33 2c 20 20 20 38 33 2c 20 20 33 33 33 2c 20  83,   83,  333, 
2c1bb 20 33 36 31 2c 20 20 20 32 32 2c 20 20 35 36 36   361,   22,  566
2c1bc 2c 20 20 20 38 33 2c 20 2d 31 32 34 2c 0a 20 2f  ,   83, -124,. /
2c1bd 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 20 38 33  *   110 */    83
2c1be 2c 20 20 20 38 33 2c 20 20 20 38 33 2c 20 20 20  ,   83,   83,   
2c1bf 38 33 2c 20 20 36 30 39 2c 20 20 20 20 31 2c 20  83,  609,    1, 
2c1c0 20 34 38 31 2c 20 20 35 31 37 2c 20 20 34 37 35   481,  517,  475
2c1c1 2c 20 20 34 37 35 2c 0a 20 2f 2a 20 20 20 31 32  ,  475,. /*   12
2c1c2 30 20 2a 2f 20 20 20 36 32 35 2c 20 20 32 32 30  0 */   625,  220
2c1c3 2c 20 20 2d 34 35 2c 20 20 37 30 39 2c 20 20 37  ,  -45,  709,  7
2c1c4 37 34 2c 20 20 34 31 37 2c 20 20 37 32 39 2c 20  74,  417,  729, 
2c1c5 20 37 35 37 2c 20 20 37 35 38 2c 20 20 33 38 34   757,  758,  384
2c1c6 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20  ,. /*   130 */  
2c1c7 20 32 33 31 2c 20 20 38 32 36 2c 20 20 32 31 34   231,  826,  214
2c1c8 2c 20 20 33 37 31 2c 20 20 35 31 32 2c 20 20 36  ,  371,  512,  6
2c1c9 35 38 2c 20 20 38 36 33 2c 20 20 38 36 39 2c 20  58,  863,  869, 
2c1ca 20 38 38 31 2c 20 20 38 39 34 2c 0a 20 2f 2a 20   881,  894,. /* 
2c1cb 20 20 31 34 30 20 2a 2f 20 20 20 36 33 37 2c 20    140 */   637, 
2c1cc 20 38 39 35 2c 20 20 33 30 31 2c 20 20 36 31 32   895,  301,  612
2c1cd 2c 20 20 37 35 32 2c 20 20 38 38 37 2c 20 20 38  ,  752,  887,  8
2c1ce 38 38 2c 20 20 20 39 30 2c 20 20 36 35 30 2c 20  88,   90,  650, 
2c1cf 20 35 39 33 2c 0a 20 2f 2a 20 20 20 31 35 30 20   593,. /*   150 
2c1d0 2a 2f 20 20 2d 31 30 30 2c 20 20 2d 37 39 2c 20  */  -100,  -79, 
2c1d1 20 2d 35 39 2c 20 20 20 2d 37 2c 20 20 20 33 38   -59,   -7,   38
2c1d2 2c 20 20 20 38 37 2c 20 20 20 33 38 2c 20 20 20  ,   87,   38,   
2c1d3 33 38 2c 20 20 31 30 31 2c 20 20 32 32 34 2c 0a  38,  101,  224,.
2c1d4 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 32   /*   160 */   2
2c1d5 37 30 2c 20 20 32 39 37 2c 20 20 20 33 38 2c 20  70,  297,   38, 
2c1d6 20 32 39 30 2c 20 20 33 35 35 2c 20 20 34 33 30   290,  355,  430
2c1d7 2c 20 20 33 39 38 2c 20 20 34 35 31 2c 20 20 36  ,  398,  451,  6
2c1d8 33 31 2c 20 20 36 39 36 2c 0a 20 2f 2a 20 20 20  31,  696,. /*   
2c1d9 31 37 30 20 2a 2f 20 20 20 37 30 32 2c 20 20 34  170 */   702,  4
2c1da 33 36 2c 20 20 33 35 35 2c 20 20 37 30 37 2c 20  36,  355,  707, 
2c1db 20 34 36 39 2c 20 20 36 33 35 2c 20 20 37 37 31   469,  635,  771
2c1dc 2c 20 20 32 39 30 2c 20 20 37 39 30 2c 20 20 38  ,  290,  790,  8
2c1dd 33 34 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f  34,. /*   180 */
2c1de 20 20 20 38 34 37 2c 20 20 38 35 39 2c 20 20 39     847,  859,  9
2c1df 31 35 2c 20 20 37 32 37 2c 20 20 38 39 30 2c 20  15,  727,  890, 
2c1e0 20 38 39 36 2c 20 20 39 32 31 2c 20 20 39 32 39   896,  921,  929
2c1e1 2c 20 20 39 33 39 2c 20 20 20 33 38 2c 0a 20 2f  ,  939,   38,. /
2c1e2 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 39 34 32  *   190 */   942
2c1e3 2c 20 20 39 34 35 2c 20 20 20 33 38 2c 20 20 39  ,  945,   38,  9
2c1e4 34 36 2c 20 20 38 36 34 2c 20 20 38 36 36 2c 20  46,  864,  866, 
2c1e5 20 39 35 38 2c 20 20 39 35 39 2c 20 20 20 33 38   958,  959,   38
2c1e6 2c 20 20 39 36 30 2c 0a 20 2f 2a 20 20 20 32 30  ,  960,. /*   20
2c1e7 30 20 2a 2f 20 20 20 39 36 31 2c 20 20 39 36 34  0 */   961,  964
2c1e8 2c 20 20 20 33 38 2c 20 20 39 36 37 2c 20 20 39  ,   38,  967,  9
2c1e9 36 38 2c 20 20 39 36 39 2c 20 20 20 33 38 2c 20  68,  969,   38, 
2c1ea 20 20 33 38 2c 20 20 39 37 33 2c 20 20 39 37 34    38,  973,  974
2c1eb 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20  ,. /*   210 */  
2c1ec 20 39 37 35 2c 20 20 39 37 36 2c 20 20 39 37 37   975,  976,  977
2c1ed 2c 20 20 39 35 34 2c 20 20 39 31 37 2c 20 20 39  ,  954,  917,  9
2c1ee 31 36 2c 20 20 39 35 30 2c 20 20 38 38 34 2c 20  16,  950,  884, 
2c1ef 20 38 39 37 2c 20 20 38 39 33 2c 0a 20 2f 2a 20   897,  893,. /* 
2c1f0 20 20 32 32 30 20 2a 2f 20 20 20 39 35 32 2c 20    220 */   952, 
2c1f1 20 39 37 31 2c 20 20 39 32 35 2c 20 20 39 37 32   971,  925,  972
2c1f2 2c 20 20 39 35 33 2c 20 20 39 37 38 2c 20 20 39  ,  953,  978,  9
2c1f3 37 39 2c 20 20 39 38 30 2c 20 20 39 38 34 2c 20  79,  980,  984, 
2c1f4 20 39 38 31 2c 0a 20 2f 2a 20 20 20 32 33 30 20   981,. /*   230 
2c1f5 2a 2f 20 20 20 39 33 38 2c 20 20 39 33 30 2c 20  */   938,  930, 
2c1f6 20 39 33 31 2c 20 20 39 34 37 2c 20 20 39 39 34   931,  947,  994
2c1f7 2c 20 31 30 30 31 2c 20 31 30 30 33 2c 20 31 30  , 1001, 1003, 10
2c1f8 30 36 2c 20 31 30 30 38 2c 20 31 30 31 31 2c 0a  06, 1008, 1011,.
2c1f9 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 31 30   /*   240 */  10
2c1fa 30 37 2c 20 20 39 38 35 2c 20 20 39 38 32 2c 20  07,  985,  982, 
2c1fb 20 39 38 33 2c 20 20 39 38 37 2c 20 20 39 38 38   983,  987,  988
2c1fc 2c 20 20 39 38 36 2c 20 20 39 39 30 2c 20 31 30  ,  986,  990, 10
2c1fd 33 37 2c 20 31 30 33 39 2c 0a 20 2f 2a 20 20 20  37, 1039,. /*   
2c1fe 32 35 30 20 2a 2f 20 20 20 39 39 31 2c 20 20 39  250 */   991,  9
2c1ff 38 39 2c 20 20 39 39 32 2c 20 20 39 39 33 2c 20  89,  992,  993, 
2c200 20 39 39 35 2c 20 20 39 39 36 2c 20 31 30 33 30   995,  996, 1030
2c201 2c 20 31 30 33 32 2c 20 31 30 32 36 2c 20 31 30  , 1032, 1026, 10
2c202 33 35 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f  35,. /*   260 */
2c203 20 20 31 30 33 36 2c 20 31 30 34 32 2c 20 31 30    1036, 1042, 10
2c204 34 33 2c 20 20 39 39 37 2c 20 20 39 39 38 2c 20  43,  997,  998, 
2c205 31 30 34 36 2c 20 31 30 35 33 2c 20 31 30 35 35  1046, 1053, 1055
2c206 2c 20 31 30 36 32 2c 20 31 30 35 36 2c 0a 20 2f  , 1062, 1056,. /
2c207 2a 20 20 20 32 37 30 20 2a 2f 20 20 31 30 36 34  *   270 */  1064
2c208 2c 20 31 30 37 39 2c 20 31 30 36 35 2c 20 31 30  , 1079, 1065, 10
2c209 38 32 2c 20 31 30 38 34 2c 20 31 30 38 35 2c 20  82, 1084, 1085, 
2c20a 31 30 38 36 2c 20 31 30 31 30 2c 20 31 30 31 32  1086, 1010, 1012
2c20b 2c 20 31 30 38 39 2c 0a 20 2f 2a 20 20 20 32 38  , 1089,. /*   28
2c20c 30 20 2a 2f 20 20 31 30 39 32 2c 20 31 30 36 33  0 */  1092, 1063
2c20d 2c 20 31 30 36 39 2c 20 31 30 34 37 2c 20 31 30  , 1069, 1047, 10
2c20e 36 36 2c 20 31 30 39 37 2c 20 31 30 37 30 2c 20  66, 1097, 1070, 
2c20f 31 30 37 31 2c 20 31 30 37 32 2c 20 31 30 38 37  1071, 1072, 1087
2c210 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20  ,. /*   290 */  
2c211 31 30 36 38 2c 20 31 30 38 30 2c 20 31 30 36 31  1068, 1080, 1061
2c212 2c 20 31 31 30 34 2c 20 31 30 34 38 2c 20 31 30  , 1104, 1048, 10
2c213 35 30 2c 20 31 31 31 39 2c 20 31 31 32 30 2c 20  50, 1119, 1120, 
2c214 31 30 36 37 2c 20 31 31 31 37 2c 0a 20 2f 2a 20  1067, 1117,. /* 
2c215 20 20 33 30 30 20 2a 2f 20 20 31 31 30 32 2c 20    300 */  1102, 
2c216 31 31 30 33 2c 20 31 30 37 33 2c 20 31 30 37 34  1103, 1073, 1074
2c217 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73  ,.};.static cons
2c218 74 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 79  t YYACTIONTYPE y
2c219 79 5f 64 65 66 61 75 6c 74 5b 5d 20 3d 20 7b 0a  y_default[] = {.
2c21a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 36   /*     0 */   6
2c21b 31 35 2c 20 20 39 32 39 2c 20 20 38 34 38 2c 20  15,  929,  848, 
2c21c 20 37 33 36 2c 20 20 39 32 39 2c 20 20 38 34 38   736,  929,  848
2c21d 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 38  ,  929,  929,  8
2c21e 37 35 2c 20 20 39 32 39 2c 0a 20 2f 2a 20 20 20  75,  929,. /*   
2c21f 20 31 30 20 2a 2f 20 20 20 39 30 34 2c 20 20 38   10 */   904,  8
2c220 34 36 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  46,  929,  929, 
2c221 20 39 32 39 2c 20 20 39 32 39 2c 20 20 38 32 30   929,  929,  820
2c222 2c 20 20 39 32 39 2c 20 20 38 37 35 2c 20 20 39  ,  929,  875,  9
2c223 32 39 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f  29,. /*    20 */
2c224 20 20 20 36 35 32 2c 20 20 38 37 35 2c 20 20 38     652,  875,  8
2c225 37 35 2c 20 20 37 34 30 2c 20 20 37 37 31 2c 20  75,  740,  771, 
2c226 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c227 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 0a 20 2f  ,  929,  929,. /
2c228 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 39 32 39  *    30 */   929
2c229 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 37  ,  929,  929,  7
2c22a 37 32 2c 20 20 39 32 39 2c 20 20 38 35 30 2c 20  72,  929,  850, 
2c22b 20 38 34 35 2c 20 20 38 34 31 2c 20 20 38 34 33   845,  841,  843
2c22c 2c 20 20 38 34 32 2c 0a 20 2f 2a 20 20 20 20 34  ,  842,. /*    4
2c22d 30 20 2a 2f 20 20 20 38 34 39 2c 20 20 37 37 33  0 */   849,  773
2c22e 2c 20 20 37 36 32 2c 20 20 37 36 39 2c 20 20 37  ,  762,  769,  7
2c22f 37 36 2c 20 20 37 35 31 2c 20 20 38 38 38 2c 20  76,  751,  888, 
2c230 20 37 37 38 2c 20 20 37 37 39 2c 20 20 37 38 35   778,  779,  785
2c231 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20  ,. /*    50 */  
2c232 20 37 38 36 2c 20 20 39 30 35 2c 20 20 39 30 33   786,  905,  903
2c233 2c 20 20 38 30 38 2c 20 20 38 30 37 2c 20 20 38  ,  808,  807,  8
2c234 32 36 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  26,  929,  929, 
2c235 20 39 32 39 2c 20 20 39 32 39 2c 0a 20 2f 2a 20   929,  929,. /* 
2c236 20 20 20 36 30 20 2a 2f 20 20 20 39 32 39 2c 20     60 */   929, 
2c237 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c238 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c239 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c23a 20 39 32 39 2c 0a 20 2f 2a 20 20 20 20 37 30 20   929,. /*    70 
2c23b 2a 2f 20 20 20 39 32 39 2c 20 20 39 32 39 2c 20  */   929,  929, 
2c23c 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c23d 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c23e 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 0a  29,  929,  929,.
2c23f 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 39   /*    80 */   9
2c240 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c241 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c242 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c243 32 39 2c 20 20 39 32 39 2c 0a 20 2f 2a 20 20 20  29,  929,. /*   
2c244 20 39 30 20 2a 2f 20 20 20 39 32 39 2c 20 20 39   90 */   929,  9
2c245 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c246 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c247 2c 20 20 38 31 30 2c 20 20 38 33 32 2c 20 20 38  ,  810,  832,  8
2c248 30 39 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f  09,. /*   100 */
2c249 20 20 20 38 31 39 2c 20 20 36 34 35 2c 20 20 38     819,  645,  8
2c24a 31 31 2c 20 20 38 31 32 2c 20 20 37 30 35 2c 20  11,  812,  705, 
2c24b 20 36 34 30 2c 20 20 39 32 39 2c 20 20 39 32 39   640,  929,  929
2c24c 2c 20 20 38 31 33 2c 20 20 39 32 39 2c 0a 20 2f  ,  813,  929,. /
2c24d 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 38 31 34  *   110 */   814
2c24e 2c 20 20 38 32 37 2c 20 20 38 32 38 2c 20 20 38  ,  827,  828,  8
2c24f 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c250 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c251 2c 20 20 39 32 39 2c 0a 20 2f 2a 20 20 20 31 32  ,  929,. /*   12
2c252 30 20 2a 2f 20 20 20 36 31 35 2c 20 20 37 33 36  0 */   615,  736
2c253 2c 20 20 39 32 39 2c 20 20 37 33 36 2c 20 20 37  ,  929,  736,  7
2c254 33 36 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  36,  929,  929, 
2c255 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c256 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20  ,. /*   130 */  
2c257 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c258 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c259 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c25a 20 39 32 39 2c 20 20 39 32 39 2c 0a 20 2f 2a 20   929,  929,. /* 
2c25b 20 20 31 34 30 20 2a 2f 20 20 20 39 32 39 2c 20    140 */   929, 
2c25c 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c25d 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c25e 32 39 2c 20 20 37 33 30 2c 20 20 37 34 30 2c 20  29,  730,  740, 
2c25f 20 39 32 32 2c 0a 20 2f 2a 20 20 20 31 35 30 20   922,. /*   150 
2c260 2a 2f 20 20 20 39 32 39 2c 20 20 39 32 39 2c 20  */   929,  929, 
2c261 20 36 39 36 2c 20 20 39 32 39 2c 20 20 39 32 39   696,  929,  929
2c262 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c263 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 0a  29,  929,  929,.
2c264 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 39   /*   160 */   9
2c265 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c266 20 39 32 39 2c 20 20 39 32 39 2c 20 20 36 32 33   929,  929,  623
2c267 2c 20 20 36 32 31 2c 20 20 39 32 39 2c 20 20 39  ,  621,  929,  9
2c268 32 39 2c 20 20 39 32 39 2c 0a 20 2f 2a 20 20 20  29,  929,. /*   
2c269 31 37 30 20 2a 2f 20 20 20 39 32 39 2c 20 20 37  170 */   929,  7
2c26a 32 38 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  28,  929,  929, 
2c26b 20 36 35 34 2c 20 20 39 32 39 2c 20 20 39 32 39   654,  929,  929
2c26c 2c 20 20 37 33 38 2c 20 20 39 32 39 2c 20 20 39  ,  738,  929,  9
2c26d 32 39 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f  29,. /*   180 */
2c26e 20 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39     929,  929,  9
2c26f 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c270 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c271 2c 20 20 39 32 39 2c 20 20 36 34 32 2c 0a 20 2f  ,  929,  642,. /
2c272 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 39 32 39  *   190 */   929
2c273 2c 20 20 39 32 39 2c 20 20 37 31 37 2c 20 20 39  ,  929,  717,  9
2c274 32 39 2c 20 20 38 38 31 2c 20 20 39 32 39 2c 20  29,  881,  929, 
2c275 20 39 32 39 2c 20 20 39 32 39 2c 20 20 38 39 35   929,  929,  895
2c276 2c 20 20 39 32 39 2c 0a 20 2f 2a 20 20 20 32 30  ,  929,. /*   20
2c277 30 20 2a 2f 20 20 20 39 32 39 2c 20 20 39 32 39  0 */   929,  929
2c278 2c 20 20 38 39 33 2c 20 20 39 32 39 2c 20 20 39  ,  893,  929,  9
2c279 32 39 2c 20 20 39 32 39 2c 20 20 37 31 39 2c 20  29,  929,  719, 
2c27a 20 37 38 31 2c 20 20 38 36 31 2c 20 20 39 32 39   781,  861,  929
2c27b 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20  ,. /*   210 */  
2c27c 20 39 30 38 2c 20 20 39 31 30 2c 20 20 39 32 39   908,  910,  929
2c27d 2c 20 20 39 32 39 2c 20 20 37 32 38 2c 20 20 37  ,  929,  728,  7
2c27e 33 37 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  37,  929,  929, 
2c27f 20 39 32 39 2c 20 20 38 34 34 2c 0a 20 2f 2a 20   929,  844,. /* 
2c280 20 20 32 32 30 20 2a 2f 20 20 20 37 36 35 2c 20    220 */   765, 
2c281 20 37 36 35 2c 20 20 37 35 33 2c 20 20 37 36 35   765,  753,  765
2c282 2c 20 20 36 37 35 2c 20 20 37 36 35 2c 20 20 39  ,  675,  765,  9
2c283 32 39 2c 20 20 37 36 35 2c 20 20 39 32 39 2c 20  29,  765,  929, 
2c284 20 36 37 38 2c 0a 20 2f 2a 20 20 20 32 33 30 20   678,. /*   230 
2c285 2a 2f 20 20 20 37 37 35 2c 20 20 37 35 33 2c 20  */   775,  753, 
2c286 20 37 35 33 2c 20 20 37 37 35 2c 20 20 36 32 30   753,  775,  620
2c287 2c 20 20 36 32 30 2c 20 20 36 32 30 2c 20 20 36  ,  620,  620,  6
2c288 32 30 2c 20 20 36 33 31 2c 20 20 36 33 31 2c 0a  20,  631,  631,.
2c289 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 36   /*   240 */   6
2c28a 39 35 2c 20 20 39 32 39 2c 20 20 37 37 35 2c 20  95,  929,  775, 
2c28b 20 37 36 36 2c 20 20 37 36 38 2c 20 20 37 35 38   766,  768,  758
2c28c 2c 20 20 37 37 30 2c 20 20 39 32 39 2c 20 20 37  ,  770,  929,  7
2c28d 34 34 2c 20 20 37 34 34 2c 0a 20 2f 2a 20 20 20  44,  744,. /*   
2c28e 32 35 30 20 2a 2f 20 20 20 37 35 32 2c 20 20 37  250 */   752,  7
2c28f 35 37 2c 20 20 37 35 32 2c 20 20 37 35 37 2c 20  57,  752,  757, 
2c290 20 37 35 32 2c 20 20 37 35 37 2c 20 20 37 30 37   752,  757,  707
2c291 2c 20 20 37 30 37 2c 20 20 36 39 32 2c 20 20 37  ,  707,  692,  7
2c292 30 37 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f  07,. /*   260 */
2c293 20 20 20 36 37 38 2c 20 20 37 30 37 2c 20 20 38     678,  707,  8
2c294 35 34 2c 20 20 38 35 38 2c 20 20 38 35 38 2c 20  54,  858,  858, 
2c295 20 36 39 32 2c 20 20 37 30 37 2c 20 20 37 30 37   692,  707,  707
2c296 2c 20 20 37 30 37 2c 20 20 38 35 34 2c 0a 20 2f  ,  707,  854,. /
2c297 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 36 33 37  *   270 */   637
2c298 2c 20 20 37 34 34 2c 20 20 36 33 37 2c 20 20 37  ,  744,  637,  7
2c299 34 34 2c 20 20 36 33 37 2c 20 20 37 34 34 2c 20  44,  637,  744, 
2c29a 20 37 34 34 2c 20 20 38 38 35 2c 20 20 38 38 37   744,  885,  887
2c29b 2c 20 20 36 33 37 2c 0a 20 2f 2a 20 20 20 32 38  ,  637,. /*   28
2c29c 30 20 2a 2f 20 20 20 37 34 34 2c 20 20 37 30 39  0 */   744,  709
2c29d 2c 20 20 37 30 39 2c 20 20 37 38 37 2c 20 20 37  ,  709,  787,  7
2c29e 37 35 2c 20 20 37 34 34 2c 20 20 37 31 36 2c 20  75,  744,  716, 
2c29f 20 37 31 36 2c 20 20 37 31 36 2c 20 20 37 31 36   716,  716,  716
2c2a0 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20  ,. /*   290 */  
2c2a1 20 37 37 35 2c 20 20 37 30 39 2c 20 20 37 38 37   775,  709,  787
2c2a2 2c 20 20 37 34 34 2c 20 20 39 30 37 2c 20 20 39  ,  744,  907,  9
2c2a3 30 37 2c 20 20 37 34 34 2c 20 20 37 34 34 2c 20  07,  744,  744, 
2c2a4 20 39 31 35 2c 20 20 36 36 32 2c 0a 20 2f 2a 20   915,  662,. /* 
2c2a5 20 20 33 30 30 20 2a 2f 20 20 20 36 38 30 2c 20    300 */   680, 
2c2a6 20 36 38 30 2c 20 20 39 32 32 2c 20 20 39 32 37   680,  922,  927
2c2a7 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c2a8 32 39 2c 20 20 39 32 39 2c 20 20 37 39 34 2c 20  29,  929,  794, 
2c2a9 20 39 32 39 2c 0a 20 2f 2a 20 20 20 33 31 30 20   929,. /*   310 
2c2aa 2a 2f 20 20 20 39 32 39 2c 20 20 39 32 39 2c 20  */   929,  929, 
2c2ab 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c2ac 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c2ad 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 0a  29,  929,  929,.
2c2ae 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 39   /*   320 */   9
2c2af 32 39 2c 20 20 39 32 39 2c 20 20 38 36 38 2c 20  29,  929,  868, 
2c2b0 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c2b1 2c 20 20 36 32 39 2c 20 20 39 32 39 2c 20 20 37  ,  629,  929,  7
2c2b2 39 39 2c 20 20 37 39 35 2c 0a 20 2f 2a 20 20 20  99,  795,. /*   
2c2b3 33 33 30 20 2a 2f 20 20 20 39 32 39 2c 20 20 37  330 */   929,  7
2c2b4 39 36 2c 20 20 39 32 39 2c 20 20 37 32 32 2c 20  96,  929,  722, 
2c2b5 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c2b6 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c2b7 32 39 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f  29,. /*   340 */
2c2b8 20 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39     929,  929,  9
2c2b9 32 39 2c 20 20 39 32 39 2c 20 20 38 34 37 2c 20  29,  929,  847, 
2c2ba 20 39 32 39 2c 20 20 37 35 39 2c 20 20 39 32 39   929,  759,  929
2c2bb 2c 20 20 37 36 37 2c 20 20 39 32 39 2c 0a 20 2f  ,  767,  929,. /
2c2bc 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 39 32 39  *   350 */   929
2c2bd 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c2be 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c2bf 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c2c0 2c 20 20 39 32 39 2c 0a 20 2f 2a 20 20 20 33 36  ,  929,. /*   36
2c2c1 30 20 2a 2f 20 20 20 39 32 39 2c 20 20 39 32 39  0 */   929,  929
2c2c2 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c2c3 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c2c4 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c2c5 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20  ,. /*   370 */  
2c2c6 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c2c7 2c 20 20 39 32 39 2c 20 20 38 38 33 2c 20 20 38  ,  929,  883,  8
2c2c8 38 34 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  84,  929,  929, 
2c2c9 20 39 32 39 2c 20 20 39 32 39 2c 0a 20 2f 2a 20   929,  929,. /* 
2c2ca 20 20 33 38 30 20 2a 2f 20 20 20 39 32 39 2c 20    380 */   929, 
2c2cb 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c2cc 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c2cd 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20  29,  929,  929, 
2c2ce 20 39 32 39 2c 0a 20 2f 2a 20 20 20 33 39 30 20   929,. /*   390 
2c2cf 2a 2f 20 20 20 39 32 39 2c 20 20 39 32 39 2c 20  */   929,  929, 
2c2d0 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39 32 39   929,  929,  929
2c2d1 2c 20 20 39 32 39 2c 20 20 39 32 39 2c 20 20 39  ,  929,  929,  9
2c2d2 32 39 2c 20 20 39 32 39 2c 20 20 39 31 34 2c 0a  29,  929,  914,.
2c2d3 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 39   /*   400 */   9
2c2d4 32 39 2c 20 20 39 32 39 2c 20 20 39 31 37 2c 20  29,  929,  917, 
2c2d5 20 36 31 36 2c 20 20 39 32 39 2c 20 20 36 31 31   616,  929,  611
2c2d6 2c 20 20 36 31 33 2c 20 20 36 31 34 2c 20 20 36  ,  613,  614,  6
2c2d7 31 38 2c 20 20 36 31 39 2c 0a 20 2f 2a 20 20 20  18,  619,. /*   
2c2d8 34 31 30 20 2a 2f 20 20 20 36 32 32 2c 20 20 36  410 */   622,  6
2c2d9 34 39 2c 20 20 36 35 30 2c 20 20 36 35 31 2c 20  49,  650,  651, 
2c2da 20 36 32 34 2c 20 20 36 32 35 2c 20 20 36 32 36   624,  625,  626
2c2db 2c 20 20 36 32 37 2c 20 20 36 32 38 2c 20 20 36  ,  627,  628,  6
2c2dc 33 30 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f  30,. /*   420 */
2c2dd 20 20 20 36 33 34 2c 20 20 36 33 32 2c 20 20 36     634,  632,  6
2c2de 33 33 2c 20 20 36 33 35 2c 20 20 36 34 31 2c 20  33,  635,  641, 
2c2df 20 36 34 33 2c 20 20 36 36 31 2c 20 20 36 36 33   643,  661,  663
2c2e0 2c 20 20 36 34 37 2c 20 20 36 36 35 2c 0a 20 2f  ,  647,  665,. /
2c2e1 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 37 32 36  *   430 */   726
2c2e2 2c 20 20 37 32 37 2c 20 20 37 39 31 2c 20 20 37  ,  727,  791,  7
2c2e3 32 30 2c 20 20 37 32 31 2c 20 20 37 32 35 2c 20  20,  721,  725, 
2c2e4 20 36 34 38 2c 20 20 38 30 32 2c 20 20 37 39 33   648,  802,  793
2c2e5 2c 20 20 37 39 37 2c 0a 20 2f 2a 20 20 20 34 34  ,  797,. /*   44
2c2e6 30 20 2a 2f 20 20 20 37 39 38 2c 20 20 38 30 30  0 */   798,  800
2c2e7 2c 20 20 38 30 31 2c 20 20 38 31 35 2c 20 20 38  ,  801,  815,  8
2c2e8 31 36 2c 20 20 38 31 38 2c 20 20 38 32 34 2c 20  16,  818,  824, 
2c2e9 20 38 33 31 2c 20 20 38 33 34 2c 20 20 38 31 37   831,  834,  817
2c2ea 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20  ,. /*   450 */  
2c2eb 20 38 32 32 2c 20 20 38 32 33 2c 20 20 38 32 35   822,  823,  825
2c2ec 2c 20 20 38 33 30 2c 20 20 38 33 33 2c 20 20 37  ,  830,  833,  7
2c2ed 32 33 2c 20 20 37 32 34 2c 20 20 38 33 37 2c 20  23,  724,  837, 
2c2ee 20 36 35 35 2c 20 20 36 35 36 2c 0a 20 2f 2a 20   655,  656,. /* 
2c2ef 20 20 34 36 30 20 2a 2f 20 20 20 36 35 39 2c 20    460 */   659, 
2c2f0 20 36 36 30 2c 20 20 38 37 31 2c 20 20 38 37 33   660,  871,  873
2c2f1 2c 20 20 38 37 32 2c 20 20 38 37 34 2c 20 20 36  ,  872,  874,  6
2c2f2 35 38 2c 20 20 36 35 37 2c 20 20 38 30 33 2c 20  58,  657,  803, 
2c2f3 20 38 30 36 2c 0a 20 2f 2a 20 20 20 34 37 30 20   806,. /*   470 
2c2f4 2a 2f 20 20 20 38 33 39 2c 20 20 38 34 30 2c 20  */   839,  840, 
2c2f5 20 38 39 36 2c 20 20 38 39 37 2c 20 20 38 39 38   896,  897,  898
2c2f6 2c 20 20 38 39 39 2c 20 20 39 30 30 2c 20 20 38  ,  899,  900,  8
2c2f7 33 35 2c 20 20 37 34 35 2c 20 20 38 33 38 2c 0a  35,  745,  838,.
2c2f8 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 38   /*   480 */   8
2c2f9 32 31 2c 20 20 37 36 30 2c 20 20 37 36 33 2c 20  21,  760,  763, 
2c2fa 20 37 36 34 2c 20 20 37 36 31 2c 20 20 37 32 39   764,  761,  729
2c2fb 2c 20 20 37 33 39 2c 20 20 37 34 37 2c 20 20 37  ,  739,  747,  7
2c2fc 34 38 2c 20 20 37 34 39 2c 0a 20 2f 2a 20 20 20  48,  749,. /*   
2c2fd 34 39 30 20 2a 2f 20 20 20 37 35 30 2c 20 20 37  490 */   750,  7
2c2fe 33 34 2c 20 20 37 33 35 2c 20 20 37 34 31 2c 20  34,  735,  741, 
2c2ff 20 37 35 36 2c 20 20 37 38 39 2c 20 20 37 39 30   756,  789,  790
2c300 2c 20 20 37 35 34 2c 20 20 37 35 35 2c 20 20 37  ,  754,  755,  7
2c301 34 32 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f  42,. /*   500 */
2c302 20 20 20 37 34 33 2c 20 20 37 33 31 2c 20 20 37     743,  731,  7
2c303 33 32 2c 20 20 37 33 33 2c 20 20 38 33 36 2c 20  32,  733,  836, 
2c304 20 37 39 32 2c 20 20 38 30 34 2c 20 20 38 30 35   792,  804,  805
2c305 2c 20 20 36 36 36 2c 20 20 36 36 37 2c 0a 20 2f  ,  666,  667,. /
2c306 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 37 39 39  *   510 */   799
2c307 2c 20 20 36 36 38 2c 20 20 36 36 39 2c 20 20 36  ,  668,  669,  6
2c308 37 30 2c 20 20 37 30 38 2c 20 20 37 31 31 2c 20  70,  708,  711, 
2c309 20 37 31 32 2c 20 20 37 31 33 2c 20 20 36 37 31   712,  713,  671
2c30a 2c 20 20 36 39 30 2c 0a 20 2f 2a 20 20 20 35 32  ,  690,. /*   52
2c30b 30 20 2a 2f 20 20 20 36 39 33 2c 20 20 36 39 34  0 */   693,  694
2c30c 2c 20 20 36 37 32 2c 20 20 36 37 39 2c 20 20 36  ,  672,  679,  6
2c30d 37 33 2c 20 20 36 37 34 2c 20 20 36 38 31 2c 20  73,  674,  681, 
2c30e 20 36 38 32 2c 20 20 36 38 33 2c 20 20 36 38 36   682,  683,  686
2c30f 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20  ,. /*   530 */  
2c310 20 36 38 37 2c 20 20 36 38 38 2c 20 20 36 38 39   687,  688,  689
2c311 2c 20 20 36 38 34 2c 20 20 36 38 35 2c 20 20 38  ,  684,  685,  8
2c312 35 35 2c 20 20 38 35 36 2c 20 20 38 35 39 2c 20  55,  856,  859, 
2c313 20 38 35 37 2c 20 20 36 37 36 2c 0a 20 2f 2a 20   857,  676,. /* 
2c314 20 20 35 34 30 20 2a 2f 20 20 20 36 37 37 2c 20    540 */   677, 
2c315 20 36 39 31 2c 20 20 36 36 34 2c 20 20 36 35 33   691,  664,  653
2c316 2c 20 20 36 34 36 2c 20 20 36 39 37 2c 20 20 37  ,  646,  697,  7
2c317 30 30 2c 20 20 37 30 31 2c 20 20 37 30 32 2c 20  00,  701,  702, 
2c318 20 37 30 33 2c 0a 20 2f 2a 20 20 20 35 35 30 20   703,. /*   550 
2c319 2a 2f 20 20 20 37 30 34 2c 20 20 37 30 36 2c 20  */   704,  706, 
2c31a 20 36 39 38 2c 20 20 36 39 39 2c 20 20 36 34 34   698,  699,  644
2c31b 2c 20 20 36 33 36 2c 20 20 36 33 38 2c 20 20 37  ,  636,  638,  7
2c31c 34 36 2c 20 20 38 37 37 2c 20 20 38 38 36 2c 0a  46,  877,  886,.
2c31d 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 38   /*   560 */   8
2c31e 38 32 2c 20 20 38 37 38 2c 20 20 38 37 39 2c 20  82,  878,  879, 
2c31f 20 38 38 30 2c 20 20 36 33 39 2c 20 20 38 35 31   880,  639,  851
2c320 2c 20 20 38 35 32 2c 20 20 37 31 30 2c 20 20 37  ,  852,  710,  7
2c321 38 33 2c 20 20 37 38 34 2c 0a 20 2f 2a 20 20 20  83,  784,. /*   
2c322 35 37 30 20 2a 2f 20 20 20 38 37 36 2c 20 20 38  570 */   876,  8
2c323 38 39 2c 20 20 38 39 31 2c 20 20 37 38 38 2c 20  89,  891,  788, 
2c324 20 38 39 32 2c 20 20 38 39 34 2c 20 20 38 39 30   892,  894,  890
2c325 2c 20 20 39 31 39 2c 20 20 37 31 34 2c 20 20 37  ,  919,  714,  7
2c326 31 35 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f  15,. /*   580 */
2c327 20 20 20 37 31 38 2c 20 20 38 36 30 2c 20 20 39     718,  860,  9
2c328 30 31 2c 20 20 37 37 34 2c 20 20 37 37 37 2c 20  01,  774,  777, 
2c329 20 37 38 30 2c 20 20 37 38 32 2c 20 20 38 36 32   780,  782,  862
2c32a 2c 20 20 38 36 33 2c 20 20 38 36 34 2c 0a 20 2f  ,  863,  864,. /
2c32b 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 38 36 35  *   590 */   865
2c32c 2c 20 20 38 36 36 2c 20 20 38 36 39 2c 20 20 38  ,  866,  869,  8
2c32d 37 30 2c 20 20 38 36 37 2c 20 20 39 30 32 2c 20  70,  867,  902, 
2c32e 20 39 30 36 2c 20 20 39 30 39 2c 20 20 39 31 31   906,  909,  911
2c32f 2c 20 20 39 31 32 2c 0a 20 2f 2a 20 20 20 36 30  ,  912,. /*   60
2c330 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 36  0 */   913,  916
2c331 2c 20 20 39 31 38 2c 20 20 39 32 33 2c 20 20 39  ,  918,  923,  9
2c332 32 34 2c 20 20 39 32 35 2c 20 20 39 32 38 2c 20  24,  925,  928, 
2c333 20 39 32 36 2c 20 20 36 31 37 2c 20 20 36 31 32   926,  617,  612
2c334 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f  ,.};.#define YY_
2c335 53 5a 5f 41 43 54 54 41 42 20 28 69 6e 74 29 28  SZ_ACTTAB (int)(
2c336 73 69 7a 65 6f 66 28 79 79 5f 61 63 74 69 6f 6e  sizeof(yy_action
2c337 29 2f 73 69 7a 65 6f 66 28 79 79 5f 61 63 74 69  )/sizeof(yy_acti
2c338 6f 6e 5b 30 5d 29 29 0a 0a 2f 2a 20 54 68 65 20  on[0]))../* The 
2c339 6e 65 78 74 20 74 61 62 6c 65 20 6d 61 70 73 20  next table maps 
2c33a 74 6f 6b 65 6e 73 20 69 6e 74 6f 20 66 61 6c 6c  tokens into fall
2c33b 62 61 63 6b 20 74 6f 6b 65 6e 73 2e 20 20 49 66  back tokens.  If
2c33c 20 61 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20   a construct.** 
2c33d 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  like the followi
2c33e 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 20  ng:.** .**      
2c33f 25 66 61 6c 6c 62 61 63 6b 20 49 44 20 58 20 59  %fallback ID X Y
2c340 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61 70 70 65 61 72   Z..**.** appear
2c341 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72  s in the grammar
2c342 2c 20 74 68 65 6e 20 49 44 20 62 65 63 6f 6d 65  , then ID become
2c343 73 20 61 20 66 61 6c 6c 62 61 63 6b 20 74 6f 6b  s a fallback tok
2c344 65 6e 20 66 6f 72 20 58 2c 20 59 2c 0a 2a 2a 20  en for X, Y,.** 
2c345 61 6e 64 20 5a 2e 20 20 57 68 65 6e 65 76 65 72  and Z.  Whenever
2c346 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 6f 6b 65   one of the toke
2c347 6e 73 20 58 2c 20 59 2c 20 6f 72 20 5a 20 69 73  ns X, Y, or Z is
2c348 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20 70 61   input to the pa
2c349 72 73 65 72 0a 2a 2a 20 62 75 74 20 69 74 20 64  rser.** but it d
2c34a 6f 65 73 20 6e 6f 74 20 70 61 72 73 65 2c 20 74  oes not parse, t
2c34b 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20 74  he type of the t
2c34c 6f 6b 65 6e 20 69 73 20 63 68 61 6e 67 65 64 20  oken is changed 
2c34d 74 6f 20 49 44 20 61 6e 64 0a 2a 2a 20 74 68 65  to ID and.** the
2c34e 20 70 61 72 73 65 20 69 73 20 72 65 74 72 69 65   parse is retrie
2c34f 64 20 62 65 66 6f 72 65 20 61 6e 20 65 72 72 6f  d before an erro
2c350 72 20 69 73 20 74 68 72 6f 77 6e 2e 0a 2a 2f 0a  r is thrown..*/.
2c351 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41 43  #ifdef YYFALLBAC
2c352 4b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59  K.static const Y
2c353 59 43 4f 44 45 54 59 50 45 20 79 79 46 61 6c 6c  YCODETYPE yyFall
2c354 62 61 63 6b 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  back[] = {.    0
2c355 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 20 24  ,  /*          $
2c356 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c357 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2c358 53 45 4d 49 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  SEMI => nothing 
2c359 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2c35a 20 45 58 50 4c 41 49 4e 20 3d 3e 20 49 44 20 2a   EXPLAIN => ID *
2c35b 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2c35c 20 20 51 55 45 52 59 20 3d 3e 20 49 44 20 2a 2f    QUERY => ID */
2c35d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2c35e 20 20 50 4c 41 4e 20 3d 3e 20 49 44 20 2a 2f 0a    PLAN => ID */.
2c35f 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
2c360 42 45 47 49 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20  BEGIN => ID */. 
2c361 20 20 20 30 2c 20 20 2f 2a 20 54 52 41 4e 53 41     0,  /* TRANSA
2c362 43 54 49 4f 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67  CTION => nothing
2c363 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2c364 20 44 45 46 45 52 52 45 44 20 3d 3e 20 49 44 20   DEFERRED => ID 
2c365 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 49  */.   26,  /*  I
2c366 4d 4d 45 44 49 41 54 45 20 3d 3e 20 49 44 20 2a  MMEDIATE => ID *
2c367 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 45 58  /.   26,  /*  EX
2c368 43 4c 55 53 49 56 45 20 3d 3e 20 49 44 20 2a 2f  CLUSIVE => ID */
2c369 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
2c36a 43 4f 4d 4d 49 54 20 3d 3e 20 6e 6f 74 68 69 6e  COMMIT => nothin
2c36b 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  g */.   26,  /* 
2c36c 20 20 20 20 20 20 20 45 4e 44 20 3d 3e 20 49 44         END => ID
2c36d 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2c36e 20 52 4f 4c 4c 42 41 43 4b 20 3d 3e 20 6e 6f 74   ROLLBACK => not
2c36f 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2c370 2f 2a 20 20 53 41 56 45 50 4f 49 4e 54 20 3d 3e  /*  SAVEPOINT =>
2c371 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2c372 30 2c 20 20 2f 2a 20 20 20 20 52 45 4c 45 41 53  0,  /*    RELEAS
2c373 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  E => nothing */.
2c374 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2c375 20 20 20 54 4f 20 3d 3e 20 6e 6f 74 68 69 6e 67     TO => nothing
2c376 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2c377 20 20 20 43 52 45 41 54 45 20 3d 3e 20 6e 6f 74     CREATE => not
2c378 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2c379 2f 2a 20 20 20 20 20 20 54 41 42 4c 45 20 3d 3e  /*      TABLE =>
2c37a 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32   nothing */.   2
2c37b 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49  6,  /*         I
2c37c 46 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30  F => ID */.    0
2c37d 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 4e 4f 54  ,  /*        NOT
2c37e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c37f 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 45 58     0,  /*     EX
2c380 49 53 54 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ISTS => nothing 
2c381 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2c382 20 20 20 20 54 45 4d 50 20 3d 3e 20 49 44 20 2a      TEMP => ID *
2c383 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2c384 20 20 20 20 20 4c 50 20 3d 3e 20 6e 6f 74 68 69       LP => nothi
2c385 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2c386 20 20 20 20 20 20 20 20 20 52 50 20 3d 3e 20 6e           RP => n
2c387 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2c388 20 20 2f 2a 20 20 20 20 20 20 20 20 20 41 53 20    /*         AS 
2c389 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2c38a 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 43 4f    0,  /*      CO
2c38b 4d 4d 41 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  MMA => nothing *
2c38c 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2c38d 20 20 20 20 20 49 44 20 3d 3e 20 6e 6f 74 68 69       ID => nothi
2c38e 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ng */.   26,  /*
2c38f 20 20 20 20 20 20 41 42 4f 52 54 20 3d 3e 20 49        ABORT => I
2c390 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2c391 20 20 20 20 20 41 46 54 45 52 20 3d 3e 20 49 44       AFTER => ID
2c392 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2c393 20 20 41 4e 41 4c 59 5a 45 20 3d 3e 20 49 44 20    ANALYZE => ID 
2c394 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2c395 20 20 20 20 20 41 53 43 20 3d 3e 20 49 44 20 2a       ASC => ID *
2c396 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2c397 20 41 54 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f   ATTACH => ID */
2c398 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2c399 42 45 46 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a  BEFORE => ID */.
2c39a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 43 41     26,  /*    CA
2c39b 53 43 41 44 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  SCADE => ID */. 
2c39c 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
2c39d 43 41 53 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  CAST => ID */.  
2c39e 20 32 36 2c 20 20 2f 2a 20 20 20 43 4f 4e 46 4c   26,  /*   CONFL
2c39f 49 43 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ICT => ID */.   
2c3a0 32 36 2c 20 20 2f 2a 20 20 20 44 41 54 41 42 41  26,  /*   DATABA
2c3a1 53 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  SE => ID */.   2
2c3a2 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 44 45 53  6,  /*       DES
2c3a3 43 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  C => ID */.   26
2c3a4 2c 20 20 2f 2a 20 20 20 20 20 44 45 54 41 43 48  ,  /*     DETACH
2c3a5 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2c3a6 20 20 2f 2a 20 20 20 20 20 20 20 45 41 43 48 20    /*       EACH 
2c3a7 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2c3a8 20 2f 2a 20 20 20 20 20 20 20 46 41 49 4c 20 3d   /*       FAIL =
2c3a9 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2c3aa 2f 2a 20 20 20 20 20 20 20 20 46 4f 52 20 3d 3e  /*        FOR =>
2c3ab 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2c3ac 2a 20 20 20 20 20 49 47 4e 4f 52 45 20 3d 3e 20  *     IGNORE => 
2c3ad 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2c3ae 20 20 49 4e 49 54 49 41 4c 4c 59 20 3d 3e 20 49    INITIALLY => I
2c3af 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2c3b0 20 20 20 49 4e 53 54 45 41 44 20 3d 3e 20 49 44     INSTEAD => ID
2c3b1 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2c3b2 20 20 4c 49 4b 45 5f 4b 57 20 3d 3e 20 49 44 20    LIKE_KW => ID 
2c3b3 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2c3b4 20 20 20 4d 41 54 43 48 20 3d 3e 20 49 44 20 2a     MATCH => ID *
2c3b5 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2c3b6 20 20 20 20 4b 45 59 20 3d 3e 20 49 44 20 2a 2f      KEY => ID */
2c3b7 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2c3b8 20 20 20 20 4f 46 20 3d 3e 20 49 44 20 2a 2f 0a      OF => ID */.
2c3b9 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 4f     26,  /*     O
2c3ba 46 46 53 45 54 20 3d 3e 20 49 44 20 2a 2f 0a 20  FFSET => ID */. 
2c3bb 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 50 52    26,  /*     PR
2c3bc 41 47 4d 41 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  AGMA => ID */.  
2c3bd 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 52 41   26,  /*      RA
2c3be 49 53 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ISE => ID */.   
2c3bf 32 36 2c 20 20 2f 2a 20 20 20 20 52 45 50 4c 41  26,  /*    REPLA
2c3c0 43 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  CE => ID */.   2
2c3c1 36 2c 20 20 2f 2a 20 20 20 52 45 53 54 52 49 43  6,  /*   RESTRIC
2c3c2 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  T => ID */.   26
2c3c3 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 52 4f 57  ,  /*        ROW
2c3c4 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2c3c5 20 20 2f 2a 20 20 20 20 54 52 49 47 47 45 52 20    /*    TRIGGER 
2c3c6 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2c3c7 20 2f 2a 20 20 20 20 20 56 41 43 55 55 4d 20 3d   /*     VACUUM =
2c3c8 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2c3c9 2f 2a 20 20 20 20 20 20 20 56 49 45 57 20 3d 3e  /*       VIEW =>
2c3ca 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2c3cb 2a 20 20 20 20 56 49 52 54 55 41 4c 20 3d 3e 20  *    VIRTUAL => 
2c3cc 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2c3cd 20 20 20 20 52 45 49 4e 44 45 58 20 3d 3e 20 49      REINDEX => I
2c3ce 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2c3cf 20 20 20 20 52 45 4e 41 4d 45 20 3d 3e 20 49 44      RENAME => ID
2c3d0 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2c3d1 20 43 54 49 4d 45 5f 4b 57 20 3d 3e 20 49 44 20   CTIME_KW => ID 
2c3d2 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c3d3 20 20 20 20 20 41 4e 59 20 3d 3e 20 6e 6f 74 68       ANY => noth
2c3d4 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c3d5 2a 20 20 20 20 20 20 20 20 20 4f 52 20 3d 3e 20  *         OR => 
2c3d6 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c3d7 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 4e 44  ,  /*        AND
2c3d8 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c3d9 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2c3da 20 20 49 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    IS => nothing 
2c3db 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c3dc 20 42 45 54 57 45 45 4e 20 3d 3e 20 6e 6f 74 68   BETWEEN => noth
2c3dd 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c3de 2a 20 20 20 20 20 20 20 20 20 49 4e 20 3d 3e 20  *         IN => 
2c3df 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c3e0 2c 20 20 2f 2a 20 20 20 20 20 49 53 4e 55 4c 4c  ,  /*     ISNULL
2c3e1 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c3e2 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 4e 4f 54     0,  /*    NOT
2c3e3 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NULL => nothing 
2c3e4 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c3e5 20 20 20 20 20 20 4e 45 20 3d 3e 20 6e 6f 74 68        NE => noth
2c3e6 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c3e7 2a 20 20 20 20 20 20 20 20 20 45 51 20 3d 3e 20  *         EQ => 
2c3e8 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c3e9 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 47 54  ,  /*         GT
2c3ea 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c3eb 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2c3ec 20 20 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    LE => nothing 
2c3ed 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c3ee 20 20 20 20 20 20 4c 54 20 3d 3e 20 6e 6f 74 68        LT => noth
2c3ef 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c3f0 2a 20 20 20 20 20 20 20 20 20 47 45 20 3d 3e 20  *         GE => 
2c3f1 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c3f2 2c 20 20 2f 2a 20 20 20 20 20 45 53 43 41 50 45  ,  /*     ESCAPE
2c3f3 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c3f4 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 42 49     0,  /*     BI
2c3f5 54 41 4e 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  TAND => nothing 
2c3f6 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c3f7 20 20 20 42 49 54 4f 52 20 3d 3e 20 6e 6f 74 68     BITOR => noth
2c3f8 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c3f9 2a 20 20 20 20 20 4c 53 48 49 46 54 20 3d 3e 20  *     LSHIFT => 
2c3fa 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c3fb 2c 20 20 2f 2a 20 20 20 20 20 52 53 48 49 46 54  ,  /*     RSHIFT
2c3fc 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c3fd 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2c3fe 50 4c 55 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  PLUS => nothing 
2c3ff 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c400 20 20 20 4d 49 4e 55 53 20 3d 3e 20 6e 6f 74 68     MINUS => noth
2c401 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c402 2a 20 20 20 20 20 20 20 53 54 41 52 20 3d 3e 20  *       STAR => 
2c403 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c404 2c 20 20 2f 2a 20 20 20 20 20 20 53 4c 41 53 48  ,  /*      SLASH
2c405 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c406 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2c407 20 52 45 4d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   REM => nothing 
2c408 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c409 20 20 43 4f 4e 43 41 54 20 3d 3e 20 6e 6f 74 68    CONCAT => noth
2c40a 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c40b 2a 20 20 20 20 43 4f 4c 4c 41 54 45 20 3d 3e 20  *    COLLATE => 
2c40c 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c40d 2c 20 20 2f 2a 20 20 20 20 20 55 4d 49 4e 55 53  ,  /*     UMINUS
2c40e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c40f 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 55     0,  /*      U
2c410 50 4c 55 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  PLUS => nothing 
2c411 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c412 20 20 42 49 54 4e 4f 54 20 3d 3e 20 6e 6f 74 68    BITNOT => noth
2c413 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c414 2a 20 20 20 20 20 53 54 52 49 4e 47 20 3d 3e 20  *     STRING => 
2c415 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c416 2c 20 20 2f 2a 20 20 20 20 4a 4f 49 4e 5f 4b 57  ,  /*    JOIN_KW
2c417 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c418 20 20 20 30 2c 20 20 2f 2a 20 43 4f 4e 53 54 52     0,  /* CONSTR
2c419 41 49 4e 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  AINT => nothing 
2c41a 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c41b 20 44 45 46 41 55 4c 54 20 3d 3e 20 6e 6f 74 68   DEFAULT => noth
2c41c 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c41d 2a 20 20 20 20 20 20 20 4e 55 4c 4c 20 3d 3e 20  *       NULL => 
2c41e 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c41f 2c 20 20 2f 2a 20 20 20 20 50 52 49 4d 41 52 59  ,  /*    PRIMARY
2c420 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c421 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 55 4e     0,  /*     UN
2c422 49 51 55 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  IQUE => nothing 
2c423 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c424 20 20 20 43 48 45 43 4b 20 3d 3e 20 6e 6f 74 68     CHECK => noth
2c425 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c426 2a 20 52 45 46 45 52 45 4e 43 45 53 20 3d 3e 20  * REFERENCES => 
2c427 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c428 2c 20 20 2f 2a 20 20 20 41 55 54 4f 49 4e 43 52  ,  /*   AUTOINCR
2c429 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c42a 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2c42b 20 20 4f 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    ON => nothing 
2c42c 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c42d 20 20 44 45 4c 45 54 45 20 3d 3e 20 6e 6f 74 68    DELETE => noth
2c42e 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c42f 2a 20 20 20 20 20 55 50 44 41 54 45 20 3d 3e 20  *     UPDATE => 
2c430 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c431 2c 20 20 2f 2a 20 20 20 20 20 49 4e 53 45 52 54  ,  /*     INSERT
2c432 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c433 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2c434 20 53 45 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   SET => nothing 
2c435 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 44 45  */.    0,  /* DE
2c436 46 45 52 52 41 42 4c 45 20 3d 3e 20 6e 6f 74 68  FERRABLE => noth
2c437 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c438 2a 20 20 20 20 46 4f 52 45 49 47 4e 20 3d 3e 20  *    FOREIGN => 
2c439 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c43a 2c 20 20 2f 2a 20 20 20 20 20 20 20 44 52 4f 50  ,  /*       DROP
2c43b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c43c 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 55     0,  /*      U
2c43d 4e 49 4f 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NION => nothing 
2c43e 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c43f 20 20 20 20 20 41 4c 4c 20 3d 3e 20 6e 6f 74 68       ALL => noth
2c440 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c441 2a 20 20 20 20 20 45 58 43 45 50 54 20 3d 3e 20  *     EXCEPT => 
2c442 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c443 2c 20 20 2f 2a 20 20 49 4e 54 45 52 53 45 43 54  ,  /*  INTERSECT
2c444 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c445 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 53 45     0,  /*     SE
2c446 4c 45 43 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  LECT => nothing 
2c447 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c448 44 49 53 54 49 4e 43 54 20 3d 3e 20 6e 6f 74 68  DISTINCT => noth
2c449 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c44a 2a 20 20 20 20 20 20 20 20 44 4f 54 20 3d 3e 20  *        DOT => 
2c44b 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c44c 2c 20 20 2f 2a 20 20 20 20 20 20 20 46 52 4f 4d  ,  /*       FROM
2c44d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c44e 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2c44f 4a 4f 49 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  JOIN => nothing 
2c450 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c451 20 49 4e 44 45 58 45 44 20 3d 3e 20 6e 6f 74 68   INDEXED => noth
2c452 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c453 2a 20 20 20 20 20 20 20 20 20 42 59 20 3d 3e 20  *         BY => 
2c454 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c455 2c 20 20 2f 2a 20 20 20 20 20 20 55 53 49 4e 47  ,  /*      USING
2c456 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c457 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 4f     0,  /*      O
2c458 52 44 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  RDER => nothing 
2c459 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c45a 20 20 20 47 52 4f 55 50 20 3d 3e 20 6e 6f 74 68     GROUP => noth
2c45b 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c45c 2a 20 20 20 20 20 48 41 56 49 4e 47 20 3d 3e 20  *     HAVING => 
2c45d 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c45e 2c 20 20 2f 2a 20 20 20 20 20 20 4c 49 4d 49 54  ,  /*      LIMIT
2c45f 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c460 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 57     0,  /*      W
2c461 48 45 52 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  HERE => nothing 
2c462 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c463 20 20 20 20 49 4e 54 4f 20 3d 3e 20 6e 6f 74 68      INTO => noth
2c464 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c465 2a 20 20 20 20 20 56 41 4c 55 45 53 20 3d 3e 20  *     VALUES => 
2c466 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c467 2c 20 20 2f 2a 20 20 20 20 49 4e 54 45 47 45 52  ,  /*    INTEGER
2c468 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c469 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 46     0,  /*      F
2c46a 4c 4f 41 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  LOAT => nothing 
2c46b 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c46c 20 20 20 20 42 4c 4f 42 20 3d 3e 20 6e 6f 74 68      BLOB => noth
2c46d 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c46e 2a 20 20 20 52 45 47 49 53 54 45 52 20 3d 3e 20  *   REGISTER => 
2c46f 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c470 2c 20 20 2f 2a 20 20 20 56 41 52 49 41 42 4c 45  ,  /*   VARIABLE
2c471 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c472 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2c473 43 41 53 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  CASE => nothing 
2c474 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c475 20 20 20 20 57 48 45 4e 20 3d 3e 20 6e 6f 74 68      WHEN => noth
2c476 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c477 2a 20 20 20 20 20 20 20 54 48 45 4e 20 3d 3e 20  *       THEN => 
2c478 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c479 2c 20 20 2f 2a 20 20 20 20 20 20 20 45 4c 53 45  ,  /*       ELSE
2c47a 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2c47b 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 49     0,  /*      I
2c47c 4e 44 45 58 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NDEX => nothing 
2c47d 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2c47e 20 20 20 41 4c 54 45 52 20 3d 3e 20 6e 6f 74 68     ALTER => noth
2c47f 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2c480 2a 20 20 20 20 20 20 20 20 41 44 44 20 3d 3e 20  *        ADD => 
2c481 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2c482 2c 20 20 2f 2a 20 20 20 43 4f 4c 55 4d 4e 4b 57  ,  /*   COLUMNKW
2c483 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 7d   => nothing */.}
2c484 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 46 41  ;.#endif /* YYFA
2c485 4c 4c 42 41 43 4b 20 2a 2f 0a 0a 2f 2a 20 54 68  LLBACK */../* Th
2c486 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
2c487 63 74 75 72 65 20 72 65 70 72 65 73 65 6e 74 73  cture represents
2c488 20 61 20 73 69 6e 67 6c 65 20 65 6c 65 6d 65 6e   a single elemen
2c489 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 72 73  t of the.** pars
2c48a 65 72 27 73 20 73 74 61 63 6b 2e 20 20 49 6e 66  er's stack.  Inf
2c48b 6f 72 6d 61 74 69 6f 6e 20 73 74 6f 72 65 64 20  ormation stored 
2c48c 69 6e 63 6c 75 64 65 73 3a 0a 2a 2a 0a 2a 2a 20  includes:.**.** 
2c48d 20 20 2b 20 20 54 68 65 20 73 74 61 74 65 20 6e    +  The state n
2c48e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70 61  umber for the pa
2c48f 72 73 65 72 20 61 74 20 74 68 69 73 20 6c 65 76  rser at this lev
2c490 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  el of the stack.
2c491 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20  .**.**   +  The 
2c492 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b  value of the tok
2c493 65 6e 20 73 74 6f 72 65 64 20 61 74 20 74 68 69  en stored at thi
2c494 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 73  s level of the s
2c495 74 61 63 6b 2e 0a 2a 2a 20 20 20 20 20 20 28 49  tack..**      (I
2c496 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74  n other words, t
2c497 68 65 20 22 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e  he "major" token
2c498 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68  .).**.**   +  Th
2c499 65 20 73 65 6d 61 6e 74 69 63 20 76 61 6c 75 65  e semantic value
2c49a 20 73 74 6f 72 65 64 20 61 74 20 74 68 69 73 20   stored at this 
2c49b 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61  level of the sta
2c49c 63 6b 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20  ck.  This is.** 
2c49d 20 20 20 20 20 74 68 65 20 69 6e 66 6f 72 6d 61       the informa
2c49e 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65  tion used by the
2c49f 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73   action routines
2c4a0 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e   in the grammar.
2c4a1 0a 2a 2a 20 20 20 20 20 20 49 74 20 69 73 20 73  .**      It is s
2c4a2 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20  ometimes called 
2c4a3 74 68 65 20 22 6d 69 6e 6f 72 22 20 74 6f 6b 65  the "minor" toke
2c4a4 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 79 79 53  n..*/.struct yyS
2c4a5 74 61 63 6b 45 6e 74 72 79 20 7b 0a 20 20 59 59  tackEntry {.  YY
2c4a6 41 43 54 49 4f 4e 54 59 50 45 20 73 74 61 74 65  ACTIONTYPE state
2c4a7 6e 6f 3b 20 20 2f 2a 20 54 68 65 20 73 74 61 74  no;  /* The stat
2c4a8 65 2d 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 59 59  e-number */.  YY
2c4a9 43 4f 44 45 54 59 50 45 20 6d 61 6a 6f 72 3b 20  CODETYPE major; 
2c4aa 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f       /* The majo
2c4ab 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 20  r token value.  
2c4ac 54 68 69 73 20 69 73 20 74 68 65 20 63 6f 64 65  This is the code
2c4ad 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c4ae 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d            ** num
2c4af 62 65 72 20 66 6f 72 20 74 68 65 20 74 6f 6b 65  ber for the toke
2c4b0 6e 20 61 74 20 74 68 69 73 20 73 74 61 63 6b 20  n at this stack 
2c4b1 6c 65 76 65 6c 20 2a 2f 0a 20 20 59 59 4d 49 4e  level */.  YYMIN
2c4b2 4f 52 54 59 50 45 20 6d 69 6e 6f 72 3b 20 20 20  ORTYPE minor;   
2c4b3 20 20 2f 2a 20 54 68 65 20 75 73 65 72 2d 73 75    /* The user-su
2c4b4 70 70 6c 69 65 64 20 6d 69 6e 6f 72 20 74 6f 6b  pplied minor tok
2c4b5 65 6e 20 76 61 6c 75 65 2e 20 20 54 68 69 73 0a  en value.  This.
2c4b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4b7 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 74           ** is t
2c4b8 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
2c4b9 74 6f 6b 65 6e 20 20 2a 2f 0a 7d 3b 0a 74 79 70  token  */.};.typ
2c4ba 65 64 65 66 20 73 74 72 75 63 74 20 79 79 53 74  edef struct yySt
2c4bb 61 63 6b 45 6e 74 72 79 20 79 79 53 74 61 63 6b  ackEntry yyStack
2c4bc 45 6e 74 72 79 3b 0a 0a 2f 2a 20 54 68 65 20 73  Entry;../* The s
2c4bd 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 72 73  tate of the pars
2c4be 65 72 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79  er is completely
2c4bf 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 61 6e   contained in an
2c4c0 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20   instance of.** 
2c4c1 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
2c4c2 72 75 63 74 75 72 65 20 2a 2f 0a 73 74 72 75 63  ructure */.struc
2c4c3 74 20 79 79 50 61 72 73 65 72 20 7b 0a 20 20 69  t yyParser {.  i
2c4c4 6e 74 20 79 79 69 64 78 3b 20 20 20 20 20 20 20  nt yyidx;       
2c4c5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c4c6 49 6e 64 65 78 20 6f 66 20 74 6f 70 20 65 6c 65  Index of top ele
2c4c7 6d 65 6e 74 20 69 6e 20 73 74 61 63 6b 20 2a 2f  ment in stack */
2c4c8 0a 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d  .#ifdef YYTRACKM
2c4c9 41 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20 69  AXSTACKDEPTH.  i
2c4ca 6e 74 20 79 79 69 64 78 4d 61 78 3b 20 20 20 20  nt yyidxMax;    
2c4cb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c4cc 4d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  Maximum value of
2c4cd 20 79 79 69 64 78 20 2a 2f 0a 23 65 6e 64 69 66   yyidx */.#endif
2c4ce 0a 20 20 69 6e 74 20 79 79 65 72 72 63 6e 74 3b  .  int yyerrcnt;
2c4cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4d0 20 2f 2a 20 53 68 69 66 74 73 20 6c 65 66 74 20   /* Shifts left 
2c4d1 62 65 66 6f 72 65 20 6f 75 74 20 6f 66 20 74 68  before out of th
2c4d2 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c  e error */.  sql
2c4d3 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 44  ite3ParserARG_SD
2c4d4 45 43 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  ECL             
2c4d5 20 20 20 2f 2a 20 41 20 70 6c 61 63 65 20 74 6f     /* A place to
2c4d6 20 68 6f 6c 64 20 25 65 78 74 72 61 5f 61 72 67   hold %extra_arg
2c4d7 75 6d 65 6e 74 20 2a 2f 0a 23 69 66 20 59 59 53  ument */.#if YYS
2c4d8 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 69  TACKDEPTH<=0.  i
2c4d9 6e 74 20 79 79 73 74 6b 73 7a 3b 20 20 20 20 20  nt yystksz;     
2c4da 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c4db 43 75 72 72 65 6e 74 20 73 69 64 65 20 6f 66 20  Current side of 
2c4dc 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 79  the stack */.  y
2c4dd 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 73  yStackEntry *yys
2c4de 74 61 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  tack;        /* 
2c4df 54 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61  The parser's sta
2c4e0 63 6b 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 79 79  ck */.#else.  yy
2c4e1 53 74 61 63 6b 45 6e 74 72 79 20 79 79 73 74 61  StackEntry yysta
2c4e2 63 6b 5b 59 59 53 54 41 43 4b 44 45 50 54 48 5d  ck[YYSTACKDEPTH]
2c4e3 3b 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72  ;  /* The parser
2c4e4 27 73 20 73 74 61 63 6b 20 2a 2f 0a 23 65 6e 64  's stack */.#end
2c4e5 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  if.};.typedef st
2c4e6 72 75 63 74 20 79 79 50 61 72 73 65 72 20 79 79  ruct yyParser yy
2c4e7 50 61 72 73 65 72 3b 0a 0a 23 69 66 6e 64 65 66  Parser;..#ifndef
2c4e8 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 46   NDEBUG.static F
2c4e9 49 4c 45 20 2a 79 79 54 72 61 63 65 46 49 4c 45  ILE *yyTraceFILE
2c4ea 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 63 68 61   = 0;.static cha
2c4eb 72 20 2a 79 79 54 72 61 63 65 50 72 6f 6d 70 74  r *yyTracePrompt
2c4ec 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   = 0;.#endif /* 
2c4ed 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64  NDEBUG */..#ifnd
2c4ee 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 0a 2a 2a  ef NDEBUG./* .**
2c4ef 20 54 75 72 6e 20 70 61 72 73 65 72 20 74 72 61   Turn parser tra
2c4f0 63 69 6e 67 20 6f 6e 20 62 79 20 67 69 76 69 6e  cing on by givin
2c4f1 67 20 61 20 73 74 72 65 61 6d 20 74 6f 20 77 68  g a stream to wh
2c4f2 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68 65  ich to write the
2c4f3 20 74 72 61 63 65 0a 2a 2a 20 61 6e 64 20 61 20   trace.** and a 
2c4f4 70 72 6f 6d 70 74 20 74 6f 20 70 72 65 66 61 63  prompt to prefac
2c4f5 65 20 65 61 63 68 20 74 72 61 63 65 20 6d 65 73  e each trace mes
2c4f6 73 61 67 65 2e 20 20 54 72 61 63 69 6e 67 20 69  sage.  Tracing i
2c4f7 73 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2a 20  s turned off.** 
2c4f8 62 79 20 6d 61 6b 69 6e 67 20 65 69 74 68 65 72  by making either
2c4f9 20 61 72 67 75 6d 65 6e 74 20 4e 55 4c 4c 20 0a   argument NULL .
2c4fa 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a  **.** Inputs:.**
2c4fb 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20   <ul>.** <li> A 
2c4fc 46 49 4c 45 2a 20 74 6f 20 77 68 69 63 68 20 74  FILE* to which t
2c4fd 72 61 63 65 20 6f 75 74 70 75 74 20 73 68 6f 75  race output shou
2c4fe 6c 64 20 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a  ld be written..*
2c4ff 2a 20 20 20 20 20 20 49 66 20 4e 55 4c 4c 2c 20  *      If NULL, 
2c500 74 68 65 6e 20 74 72 61 63 69 6e 67 20 69 73 20  then tracing is 
2c501 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c  turned off..** <
2c502 6c 69 3e 20 41 20 70 72 65 66 69 78 20 73 74 72  li> A prefix str
2c503 69 6e 67 20 77 72 69 74 74 65 6e 20 61 74 20 74  ing written at t
2c504 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
2c505 65 76 65 72 79 0a 2a 2a 20 20 20 20 20 20 6c 69  every.**      li
2c506 6e 65 20 6f 66 20 74 72 61 63 65 20 6f 75 74 70  ne of trace outp
2c507 75 74 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 74 68  ut.  If NULL, th
2c508 65 6e 20 74 72 61 63 69 6e 67 20 69 73 0a 2a 2a  en tracing is.**
2c509 20 20 20 20 20 20 74 75 72 6e 65 64 20 6f 66 66        turned off
2c50a 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  ..** </ul>.**.**
2c50b 20 4f 75 74 70 75 74 73 3a 0a 2a 2a 20 4e 6f 6e   Outputs:.** Non
2c50c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2c50d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2c50e 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 4c  3ParserTrace(FIL
2c50f 45 20 2a 54 72 61 63 65 46 49 4c 45 2c 20 63 68  E *TraceFILE, ch
2c510 61 72 20 2a 7a 54 72 61 63 65 50 72 6f 6d 70 74  ar *zTracePrompt
2c511 29 7b 0a 20 20 79 79 54 72 61 63 65 46 49 4c 45  ){.  yyTraceFILE
2c512 20 3d 20 54 72 61 63 65 46 49 4c 45 3b 0a 20 20   = TraceFILE;.  
2c513 79 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20  yyTracePrompt = 
2c514 7a 54 72 61 63 65 50 72 6f 6d 70 74 3b 0a 20 20  zTracePrompt;.  
2c515 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 3d  if( yyTraceFILE=
2c516 3d 30 20 29 20 79 79 54 72 61 63 65 50 72 6f 6d  =0 ) yyTraceProm
2c517 70 74 20 3d 20 30 3b 0a 20 20 65 6c 73 65 20 69  pt = 0;.  else i
2c518 66 28 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74  f( yyTracePrompt
2c519 3d 3d 30 20 29 20 79 79 54 72 61 63 65 46 49 4c  ==0 ) yyTraceFIL
2c51a 45 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20  E = 0;.}.#endif 
2c51b 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69  /* NDEBUG */..#i
2c51c 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20  fndef NDEBUG./* 
2c51d 46 6f 72 20 74 72 61 63 69 6e 67 20 73 68 69 66  For tracing shif
2c51e 74 73 2c 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  ts, the names of
2c51f 20 61 6c 6c 20 74 65 72 6d 69 6e 61 6c 73 20 61   all terminals a
2c520 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73 0a  nd nonterminals.
2c521 2a 2a 20 61 72 65 20 72 65 71 75 69 72 65 64 2e  ** are required.
2c522 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
2c523 74 61 62 6c 65 20 73 75 70 70 6c 69 65 73 20 74  table supplies t
2c524 68 65 73 65 20 6e 61 6d 65 73 20 2a 2f 0a 73 74  hese names */.st
2c525 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2c526 2a 63 6f 6e 73 74 20 79 79 54 6f 6b 65 6e 4e 61  *const yyTokenNa
2c527 6d 65 5b 5d 20 3d 20 7b 20 0a 20 20 22 24 22 2c  me[] = { .  "$",
2c528 20 20 20 20 20 20 20 20 20 20 20 20 20 22 53 45               "SE
2c529 4d 49 22 2c 20 20 20 20 20 20 20 20 20 20 22 45  MI",          "E
2c52a 58 50 4c 41 49 4e 22 2c 20 20 20 20 20 20 20 22  XPLAIN",       "
2c52b 51 55 45 52 59 22 2c 20 20 20 20 20 20 20 0a 20  QUERY",       . 
2c52c 20 22 50 4c 41 4e 22 2c 20 20 20 20 20 20 20 20   "PLAN",        
2c52d 20 20 22 42 45 47 49 4e 22 2c 20 20 20 20 20 20    "BEGIN",      
2c52e 20 20 20 22 54 52 41 4e 53 41 43 54 49 4f 4e 22     "TRANSACTION"
2c52f 2c 20 20 20 22 44 45 46 45 52 52 45 44 22 2c 20  ,   "DEFERRED", 
2c530 20 20 20 0a 20 20 22 49 4d 4d 45 44 49 41 54 45     .  "IMMEDIATE
2c531 22 2c 20 20 20 20 20 22 45 58 43 4c 55 53 49 56  ",     "EXCLUSIV
2c532 45 22 2c 20 20 20 20 20 22 43 4f 4d 4d 49 54 22  E",     "COMMIT"
2c533 2c 20 20 20 20 20 20 20 20 22 45 4e 44 22 2c 20  ,        "END", 
2c534 20 20 20 20 20 20 20 20 0a 20 20 22 52 4f 4c 4c          .  "ROLL
2c535 42 41 43 4b 22 2c 20 20 20 20 20 20 22 53 41 56  BACK",      "SAV
2c536 45 50 4f 49 4e 54 22 2c 20 20 20 20 20 22 52 45  EPOINT",     "RE
2c537 4c 45 41 53 45 22 2c 20 20 20 20 20 20 20 22 54  LEASE",       "T
2c538 4f 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20  O",          .  
2c539 22 43 52 45 41 54 45 22 2c 20 20 20 20 20 20 20  "CREATE",       
2c53a 20 22 54 41 42 4c 45 22 2c 20 20 20 20 20 20 20   "TABLE",       
2c53b 20 20 22 49 46 22 2c 20 20 20 20 20 20 20 20 20    "IF",         
2c53c 20 20 20 22 4e 4f 54 22 2c 20 20 20 20 20 20 20     "NOT",       
2c53d 20 20 0a 20 20 22 45 58 49 53 54 53 22 2c 20 20    .  "EXISTS",  
2c53e 20 20 20 20 20 20 22 54 45 4d 50 22 2c 20 20 20        "TEMP",   
2c53f 20 20 20 20 20 20 20 22 4c 50 22 2c 20 20 20 20         "LP",    
2c540 20 20 20 20 20 20 20 20 22 52 50 22 2c 20 20 20          "RP",   
2c541 20 20 20 20 20 20 20 0a 20 20 22 41 53 22 2c 20         .  "AS", 
2c542 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4d 4d             "COMM
2c543 41 22 2c 20 20 20 20 20 20 20 20 20 22 49 44 22  A",         "ID"
2c544 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 41 42  ,            "AB
2c545 4f 52 54 22 2c 20 20 20 20 20 20 20 0a 20 20 22  ORT",       .  "
2c546 41 46 54 45 52 22 2c 20 20 20 20 20 20 20 20 20  AFTER",         
2c547 22 41 4e 41 4c 59 5a 45 22 2c 20 20 20 20 20 20  "ANALYZE",      
2c548 20 22 41 53 43 22 2c 20 20 20 20 20 20 20 20 20   "ASC",         
2c549 20 20 22 41 54 54 41 43 48 22 2c 20 20 20 20 20    "ATTACH",     
2c54a 20 0a 20 20 22 42 45 46 4f 52 45 22 2c 20 20 20   .  "BEFORE",   
2c54b 20 20 20 20 20 22 43 41 53 43 41 44 45 22 2c 20       "CASCADE", 
2c54c 20 20 20 20 20 20 22 43 41 53 54 22 2c 20 20 20        "CAST",   
2c54d 20 20 20 20 20 20 20 22 43 4f 4e 46 4c 49 43 54         "CONFLICT
2c54e 22 2c 20 20 20 20 0a 20 20 22 44 41 54 41 42 41  ",    .  "DATABA
2c54f 53 45 22 2c 20 20 20 20 20 20 22 44 45 53 43 22  SE",      "DESC"
2c550 2c 20 20 20 20 20 20 20 20 20 20 22 44 45 54 41  ,          "DETA
2c551 43 48 22 2c 20 20 20 20 20 20 20 20 22 45 41 43  CH",        "EAC
2c552 48 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 46  H",        .  "F
2c553 41 49 4c 22 2c 20 20 20 20 20 20 20 20 20 20 22  AIL",          "
2c554 46 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20  FOR",           
2c555 22 49 47 4e 4f 52 45 22 2c 20 20 20 20 20 20 20  "IGNORE",       
2c556 20 22 49 4e 49 54 49 41 4c 4c 59 22 2c 20 20 20   "INITIALLY",   
2c557 0a 20 20 22 49 4e 53 54 45 41 44 22 2c 20 20 20  .  "INSTEAD",   
2c558 20 20 20 20 22 4c 49 4b 45 5f 4b 57 22 2c 20 20      "LIKE_KW",  
2c559 20 20 20 20 20 22 4d 41 54 43 48 22 2c 20 20 20       "MATCH",   
2c55a 20 20 20 20 20 20 22 4b 45 59 22 2c 20 20 20 20        "KEY",    
2c55b 20 20 20 20 20 0a 20 20 22 4f 46 22 2c 20 20 20       .  "OF",   
2c55c 20 20 20 20 20 20 20 20 20 22 4f 46 46 53 45 54           "OFFSET
2c55d 22 2c 20 20 20 20 20 20 20 20 22 50 52 41 47 4d  ",        "PRAGM
2c55e 41 22 2c 20 20 20 20 20 20 20 20 22 52 41 49 53  A",        "RAIS
2c55f 45 22 2c 20 20 20 20 20 20 20 0a 20 20 22 52 45  E",       .  "RE
2c560 50 4c 41 43 45 22 2c 20 20 20 20 20 20 20 22 52  PLACE",       "R
2c561 45 53 54 52 49 43 54 22 2c 20 20 20 20 20 20 22  ESTRICT",      "
2c562 52 4f 57 22 2c 20 20 20 20 20 20 20 20 20 20 20  ROW",           
2c563 22 54 52 49 47 47 45 52 22 2c 20 20 20 20 20 0a  "TRIGGER",     .
2c564 20 20 22 56 41 43 55 55 4d 22 2c 20 20 20 20 20    "VACUUM",     
2c565 20 20 20 22 56 49 45 57 22 2c 20 20 20 20 20 20     "VIEW",      
2c566 20 20 20 20 22 56 49 52 54 55 41 4c 22 2c 20 20      "VIRTUAL",  
2c567 20 20 20 20 20 22 52 45 49 4e 44 45 58 22 2c 20       "REINDEX", 
2c568 20 20 20 20 0a 20 20 22 52 45 4e 41 4d 45 22 2c      .  "RENAME",
2c569 20 20 20 20 20 20 20 20 22 43 54 49 4d 45 5f 4b          "CTIME_K
2c56a 57 22 2c 20 20 20 20 20 20 22 41 4e 59 22 2c 20  W",      "ANY", 
2c56b 20 20 20 20 20 20 20 20 20 20 22 4f 52 22 2c 20            "OR", 
2c56c 20 20 20 20 20 20 20 20 20 0a 20 20 22 41 4e 44           .  "AND
2c56d 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 49 53  ",           "IS
2c56e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 42  ",            "B
2c56f 45 54 57 45 45 4e 22 2c 20 20 20 20 20 20 20 22  ETWEEN",       "
2c570 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20  IN",          . 
2c571 20 22 49 53 4e 55 4c 4c 22 2c 20 20 20 20 20 20   "ISNULL",      
2c572 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c 20 20 20 20    "NOTNULL",    
2c573 20 20 20 22 4e 45 22 2c 20 20 20 20 20 20 20 20     "NE",        
2c574 20 20 20 20 22 45 51 22 2c 20 20 20 20 20 20 20      "EQ",       
2c575 20 20 20 0a 20 20 22 47 54 22 2c 20 20 20 20 20     .  "GT",     
2c576 20 20 20 20 20 20 20 22 4c 45 22 2c 20 20 20 20         "LE",    
2c577 20 20 20 20 20 20 20 20 22 4c 54 22 2c 20 20 20          "LT",   
2c578 20 20 20 20 20 20 20 20 20 22 47 45 22 2c 20 20           "GE",  
2c579 20 20 20 20 20 20 20 20 0a 20 20 22 45 53 43 41          .  "ESCA
2c57a 50 45 22 2c 20 20 20 20 20 20 20 20 22 42 49 54  PE",        "BIT
2c57b 41 4e 44 22 2c 20 20 20 20 20 20 20 20 22 42 49  AND",        "BI
2c57c 54 4f 52 22 2c 20 20 20 20 20 20 20 20 20 22 4c  TOR",         "L
2c57d 53 48 49 46 54 22 2c 20 20 20 20 20 20 0a 20 20  SHIFT",      .  
2c57e 22 52 53 48 49 46 54 22 2c 20 20 20 20 20 20 20  "RSHIFT",       
2c57f 20 22 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20   "PLUS",        
2c580 20 20 22 4d 49 4e 55 53 22 2c 20 20 20 20 20 20    "MINUS",      
2c581 20 20 20 22 53 54 41 52 22 2c 20 20 20 20 20 20     "STAR",      
2c582 20 20 0a 20 20 22 53 4c 41 53 48 22 2c 20 20 20    .  "SLASH",   
2c583 20 20 20 20 20 20 22 52 45 4d 22 2c 20 20 20 20        "REM",    
2c584 20 20 20 20 20 20 20 22 43 4f 4e 43 41 54 22 2c         "CONCAT",
2c585 20 20 20 20 20 20 20 20 22 43 4f 4c 4c 41 54 45          "COLLATE
2c586 22 2c 20 20 20 20 20 0a 20 20 22 55 4d 49 4e 55  ",     .  "UMINU
2c587 53 22 2c 20 20 20 20 20 20 20 20 22 55 50 4c 55  S",        "UPLU
2c588 53 22 2c 20 20 20 20 20 20 20 20 20 22 42 49 54  S",         "BIT
2c589 4e 4f 54 22 2c 20 20 20 20 20 20 20 20 22 53 54  NOT",        "ST
2c58a 52 49 4e 47 22 2c 20 20 20 20 20 20 0a 20 20 22  RING",      .  "
2c58b 4a 4f 49 4e 5f 4b 57 22 2c 20 20 20 20 20 20 20  JOIN_KW",       
2c58c 22 43 4f 4e 53 54 52 41 49 4e 54 22 2c 20 20 20  "CONSTRAINT",   
2c58d 20 22 44 45 46 41 55 4c 54 22 2c 20 20 20 20 20   "DEFAULT",     
2c58e 20 20 22 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20    "NULL",       
2c58f 20 0a 20 20 22 50 52 49 4d 41 52 59 22 2c 20 20   .  "PRIMARY",  
2c590 20 20 20 20 20 22 55 4e 49 51 55 45 22 2c 20 20       "UNIQUE",  
2c591 20 20 20 20 20 20 22 43 48 45 43 4b 22 2c 20 20        "CHECK",  
2c592 20 20 20 20 20 20 20 22 52 45 46 45 52 45 4e 43         "REFERENC
2c593 45 53 22 2c 20 20 0a 20 20 22 41 55 54 4f 49 4e  ES",  .  "AUTOIN
2c594 43 52 22 2c 20 20 20 20 20 20 22 4f 4e 22 2c 20  CR",      "ON", 
2c595 20 20 20 20 20 20 20 20 20 20 20 22 44 45 4c 45             "DELE
2c596 54 45 22 2c 20 20 20 20 20 20 20 20 22 55 50 44  TE",        "UPD
2c597 41 54 45 22 2c 20 20 20 20 20 20 0a 20 20 22 49  ATE",      .  "I
2c598 4e 53 45 52 54 22 2c 20 20 20 20 20 20 20 20 22  NSERT",        "
2c599 53 45 54 22 2c 20 20 20 20 20 20 20 20 20 20 20  SET",           
2c59a 22 44 45 46 45 52 52 41 42 4c 45 22 2c 20 20 20  "DEFERRABLE",   
2c59b 20 22 46 4f 52 45 49 47 4e 22 2c 20 20 20 20 20   "FOREIGN",     
2c59c 0a 20 20 22 44 52 4f 50 22 2c 20 20 20 20 20 20  .  "DROP",      
2c59d 20 20 20 20 22 55 4e 49 4f 4e 22 2c 20 20 20 20      "UNION",    
2c59e 20 20 20 20 20 22 41 4c 4c 22 2c 20 20 20 20 20       "ALL",     
2c59f 20 20 20 20 20 20 22 45 58 43 45 50 54 22 2c 20        "EXCEPT", 
2c5a0 20 20 20 20 20 0a 20 20 22 49 4e 54 45 52 53 45       .  "INTERSE
2c5a1 43 54 22 2c 20 20 20 20 20 22 53 45 4c 45 43 54  CT",     "SELECT
2c5a2 22 2c 20 20 20 20 20 20 20 20 22 44 49 53 54 49  ",        "DISTI
2c5a3 4e 43 54 22 2c 20 20 20 20 20 20 22 44 4f 54 22  NCT",      "DOT"
2c5a4 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22 46 52  ,         .  "FR
2c5a5 4f 4d 22 2c 20 20 20 20 20 20 20 20 20 20 22 4a  OM",          "J
2c5a6 4f 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22  OIN",          "
2c5a7 49 4e 44 45 58 45 44 22 2c 20 20 20 20 20 20 20  INDEXED",       
2c5a8 22 42 59 22 2c 20 20 20 20 20 20 20 20 20 20 0a  "BY",          .
2c5a9 20 20 22 55 53 49 4e 47 22 2c 20 20 20 20 20 20    "USING",      
2c5aa 20 20 20 22 4f 52 44 45 52 22 2c 20 20 20 20 20     "ORDER",     
2c5ab 20 20 20 20 22 47 52 4f 55 50 22 2c 20 20 20 20      "GROUP",    
2c5ac 20 20 20 20 20 22 48 41 56 49 4e 47 22 2c 20 20       "HAVING",  
2c5ad 20 20 20 20 0a 20 20 22 4c 49 4d 49 54 22 2c 20      .  "LIMIT", 
2c5ae 20 20 20 20 20 20 20 20 22 57 48 45 52 45 22 2c          "WHERE",
2c5af 20 20 20 20 20 20 20 20 20 22 49 4e 54 4f 22 2c           "INTO",
2c5b0 20 20 20 20 20 20 20 20 20 20 22 56 41 4c 55 45            "VALUE
2c5b1 53 22 2c 20 20 20 20 20 20 0a 20 20 22 49 4e 54  S",      .  "INT
2c5b2 45 47 45 52 22 2c 20 20 20 20 20 20 20 22 46 4c  EGER",       "FL
2c5b3 4f 41 54 22 2c 20 20 20 20 20 20 20 20 20 22 42  OAT",         "B
2c5b4 4c 4f 42 22 2c 20 20 20 20 20 20 20 20 20 20 22  LOB",          "
2c5b5 52 45 47 49 53 54 45 52 22 2c 20 20 20 20 0a 20  REGISTER",    . 
2c5b6 20 22 56 41 52 49 41 42 4c 45 22 2c 20 20 20 20   "VARIABLE",    
2c5b7 20 20 22 43 41 53 45 22 2c 20 20 20 20 20 20 20    "CASE",       
2c5b8 20 20 20 22 57 48 45 4e 22 2c 20 20 20 20 20 20     "WHEN",      
2c5b9 20 20 20 20 22 54 48 45 4e 22 2c 20 20 20 20 20      "THEN",     
2c5ba 20 20 20 0a 20 20 22 45 4c 53 45 22 2c 20 20 20     .  "ELSE",   
2c5bb 20 20 20 20 20 20 20 22 49 4e 44 45 58 22 2c 20         "INDEX", 
2c5bc 20 20 20 20 20 20 20 20 22 41 4c 54 45 52 22 2c          "ALTER",
2c5bd 20 20 20 20 20 20 20 20 20 22 41 44 44 22 2c 20           "ADD", 
2c5be 20 20 20 20 20 20 20 20 0a 20 20 22 43 4f 4c 55          .  "COLU
2c5bf 4d 4e 4b 57 22 2c 20 20 20 20 20 20 22 65 72 72  MNKW",      "err
2c5c0 6f 72 22 2c 20 20 20 20 20 20 20 20 20 22 69 6e  or",         "in
2c5c1 70 75 74 22 2c 20 20 20 20 20 20 20 20 20 22 63  put",         "c
2c5c2 6d 64 6c 69 73 74 22 2c 20 20 20 20 20 0a 20 20  mdlist",     .  
2c5c3 22 65 63 6d 64 22 2c 20 20 20 20 20 20 20 20 20  "ecmd",         
2c5c4 20 22 65 78 70 6c 61 69 6e 22 2c 20 20 20 20 20   "explain",     
2c5c5 20 20 22 63 6d 64 78 22 2c 20 20 20 20 20 20 20    "cmdx",       
2c5c6 20 20 20 22 63 6d 64 22 2c 20 20 20 20 20 20 20     "cmd",       
2c5c7 20 20 0a 20 20 22 74 72 61 6e 73 74 79 70 65 22    .  "transtype"
2c5c8 2c 20 20 20 20 20 22 74 72 61 6e 73 5f 6f 70 74  ,     "trans_opt
2c5c9 22 2c 20 20 20 20 20 22 6e 6d 22 2c 20 20 20 20  ",     "nm",    
2c5ca 20 20 20 20 20 20 20 20 22 73 61 76 65 70 6f 69          "savepoi
2c5cb 6e 74 5f 6f 70 74 22 2c 0a 20 20 22 63 72 65 61  nt_opt",.  "crea
2c5cc 74 65 5f 74 61 62 6c 65 22 2c 20 20 22 63 72 65  te_table",  "cre
2c5cd 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 22 2c  ate_table_args",
2c5ce 20 20 22 74 65 6d 70 22 2c 20 20 20 20 20 20 20    "temp",       
2c5cf 20 20 20 22 69 66 6e 6f 74 65 78 69 73 74 73 22     "ifnotexists"
2c5d0 2c 20 0a 20 20 22 64 62 6e 6d 22 2c 20 20 20 20  , .  "dbnm",    
2c5d1 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 6c 69 73        "columnlis
2c5d2 74 22 2c 20 20 20 20 22 63 6f 6e 73 6c 69 73 74  t",    "conslist
2c5d3 5f 6f 70 74 22 2c 20 20 22 73 65 6c 65 63 74 22  _opt",  "select"
2c5d4 2c 20 20 20 20 20 20 0a 20 20 22 63 6f 6c 75 6d  ,      .  "colum
2c5d5 6e 22 2c 20 20 20 20 20 20 20 20 22 63 6f 6c 75  n",        "colu
2c5d6 6d 6e 69 64 22 2c 20 20 20 20 20 20 22 74 79 70  mnid",      "typ
2c5d7 65 22 2c 20 20 20 20 20 20 20 20 20 20 22 63 61  e",          "ca
2c5d8 72 67 6c 69 73 74 22 2c 20 20 20 20 0a 20 20 22  rglist",    .  "
2c5d9 69 64 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  id",            
2c5da 22 69 64 73 22 2c 20 20 20 20 20 20 20 20 20 20  "ids",          
2c5db 20 22 74 79 70 65 74 6f 6b 65 6e 22 2c 20 20 20   "typetoken",   
2c5dc 20 20 22 74 79 70 65 6e 61 6d 65 22 2c 20 20 20    "typename",   
2c5dd 20 0a 20 20 22 73 69 67 6e 65 64 22 2c 20 20 20   .  "signed",   
2c5de 20 20 20 20 20 22 70 6c 75 73 5f 6e 75 6d 22 2c       "plus_num",
2c5df 20 20 20 20 20 20 22 6d 69 6e 75 73 5f 6e 75 6d        "minus_num
2c5e0 22 2c 20 20 20 20 20 22 63 61 72 67 22 2c 20 20  ",     "carg",  
2c5e1 20 20 20 20 20 20 0a 20 20 22 63 63 6f 6e 73 22        .  "ccons"
2c5e2 2c 20 20 20 20 20 20 20 20 20 22 74 65 72 6d 22  ,         "term"
2c5e3 2c 20 20 20 20 20 20 20 20 20 20 22 65 78 70 72  ,          "expr
2c5e4 22 2c 20 20 20 20 20 20 20 20 20 20 22 6f 6e 63  ",          "onc
2c5e5 6f 6e 66 22 2c 20 20 20 20 20 20 0a 20 20 22 73  onf",      .  "s
2c5e6 6f 72 74 6f 72 64 65 72 22 2c 20 20 20 20 20 22  ortorder",     "
2c5e7 61 75 74 6f 69 6e 63 22 2c 20 20 20 20 20 20 20  autoinc",       
2c5e8 22 69 64 78 6c 69 73 74 5f 6f 70 74 22 2c 20 20  "idxlist_opt",  
2c5e9 20 22 72 65 66 61 72 67 73 22 2c 20 20 20 20 20   "refargs",     
2c5ea 0a 20 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61  .  "defer_subcla
2c5eb 75 73 65 22 2c 20 20 22 72 65 66 61 72 67 22 2c  use",  "refarg",
2c5ec 20 20 20 20 20 20 20 20 22 72 65 66 61 63 74 22          "refact"
2c5ed 2c 20 20 20 20 20 20 20 20 22 69 6e 69 74 5f 64  ,        "init_d
2c5ee 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
2c5ef 22 2c 0a 20 20 22 63 6f 6e 73 6c 69 73 74 22 2c  ",.  "conslist",
2c5f0 20 20 20 20 20 20 22 74 63 6f 6e 73 22 2c 20 20        "tcons",  
2c5f1 20 20 20 20 20 20 20 22 69 64 78 6c 69 73 74 22         "idxlist"
2c5f2 2c 20 20 20 20 20 20 20 22 64 65 66 65 72 5f 73  ,       "defer_s
2c5f3 75 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c 0a 20  ubclause_opt",. 
2c5f4 20 22 6f 72 63 6f 6e 66 22 2c 20 20 20 20 20 20   "orconf",      
2c5f5 20 20 22 72 65 73 6f 6c 76 65 74 79 70 65 22 2c    "resolvetype",
2c5f6 20 20 20 22 72 61 69 73 65 74 79 70 65 22 2c 20     "raisetype", 
2c5f7 20 20 20 20 22 69 66 65 78 69 73 74 73 22 2c 20      "ifexists", 
2c5f8 20 20 20 0a 20 20 22 66 75 6c 6c 6e 61 6d 65 22     .  "fullname"
2c5f9 2c 20 20 20 20 20 20 22 6f 6e 65 73 65 6c 65 63  ,      "oneselec
2c5fa 74 22 2c 20 20 20 20 20 22 6d 75 6c 74 69 73 65  t",     "multise
2c5fb 6c 65 63 74 5f 6f 70 22 2c 20 20 22 64 69 73 74  lect_op",  "dist
2c5fc 69 6e 63 74 22 2c 20 20 20 20 0a 20 20 22 73 65  inct",    .  "se
2c5fd 6c 63 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22 66  lcollist",    "f
2c5fe 72 6f 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22  rom",          "
2c5ff 77 68 65 72 65 5f 6f 70 74 22 2c 20 20 20 20 20  where_opt",     
2c600 22 67 72 6f 75 70 62 79 5f 6f 70 74 22 2c 20 0a  "groupby_opt", .
2c601 20 20 22 68 61 76 69 6e 67 5f 6f 70 74 22 2c 20    "having_opt", 
2c602 20 20 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 22     "orderby_opt"
2c603 2c 20 20 20 22 6c 69 6d 69 74 5f 6f 70 74 22 2c  ,   "limit_opt",
2c604 20 20 20 20 20 22 73 63 6c 70 22 2c 20 20 20 20       "sclp",    
2c605 20 20 20 20 0a 20 20 22 61 73 22 2c 20 20 20 20      .  "as",    
2c606 20 20 20 20 20 20 20 20 22 73 65 6c 74 61 62 6c          "seltabl
2c607 69 73 74 22 2c 20 20 20 20 22 73 74 6c 5f 70 72  ist",    "stl_pr
2c608 65 66 69 78 22 2c 20 20 20 20 22 6a 6f 69 6e 6f  efix",    "joino
2c609 70 22 2c 20 20 20 20 20 20 0a 20 20 22 69 6e 64  p",      .  "ind
2c60a 65 78 65 64 5f 6f 70 74 22 2c 20 20 20 22 6f 6e  exed_opt",   "on
2c60b 5f 6f 70 74 22 2c 20 20 20 20 20 20 20 20 22 75  _opt",        "u
2c60c 73 69 6e 67 5f 6f 70 74 22 2c 20 20 20 20 20 22  sing_opt",     "
2c60d 6a 6f 69 6e 6f 70 32 22 2c 20 20 20 20 20 0a 20  joinop2",     . 
2c60e 20 22 69 6e 73 63 6f 6c 6c 69 73 74 22 2c 20 20   "inscollist",  
2c60f 20 20 22 73 6f 72 74 6c 69 73 74 22 2c 20 20 20    "sortlist",   
2c610 20 20 20 22 73 6f 72 74 69 74 65 6d 22 2c 20 20     "sortitem",  
2c611 20 20 20 20 22 6e 65 78 70 72 6c 69 73 74 22 2c      "nexprlist",
2c612 20 20 20 0a 20 20 22 73 65 74 6c 69 73 74 22 2c     .  "setlist",
2c613 20 20 20 20 20 20 20 22 69 6e 73 65 72 74 5f 63         "insert_c
2c614 6d 64 22 2c 20 20 20 20 22 69 6e 73 63 6f 6c 6c  md",    "inscoll
2c615 69 73 74 5f 6f 70 74 22 2c 20 20 22 69 74 65 6d  ist_opt",  "item
2c616 6c 69 73 74 22 2c 20 20 20 20 0a 20 20 22 65 78  list",    .  "ex
2c617 70 72 6c 69 73 74 22 2c 20 20 20 20 20 20 22 6c  prlist",      "l
2c618 69 6b 65 6f 70 22 2c 20 20 20 20 20 20 20 20 22  ikeop",        "
2c619 65 73 63 61 70 65 22 2c 20 20 20 20 20 20 20 20  escape",        
2c61a 22 62 65 74 77 65 65 6e 5f 6f 70 22 2c 20 20 0a  "between_op",  .
2c61b 20 20 22 69 6e 5f 6f 70 22 2c 20 20 20 20 20 20    "in_op",      
2c61c 20 20 20 22 63 61 73 65 5f 6f 70 65 72 61 6e 64     "case_operand
2c61d 22 2c 20 20 22 63 61 73 65 5f 65 78 70 72 6c 69  ",  "case_exprli
2c61e 73 74 22 2c 20 20 22 63 61 73 65 5f 65 6c 73 65  st",  "case_else
2c61f 22 2c 20 20 20 0a 20 20 22 75 6e 69 71 75 65 66  ",   .  "uniquef
2c620 6c 61 67 22 2c 20 20 20 20 22 63 6f 6c 6c 61 74  lag",    "collat
2c621 65 22 2c 20 20 20 20 20 20 20 22 6e 6d 6e 75 6d  e",       "nmnum
2c622 22 2c 20 20 20 20 20 20 20 20 20 22 70 6c 75 73  ",         "plus
2c623 5f 6f 70 74 22 2c 20 20 20 20 0a 20 20 22 6e 75  _opt",    .  "nu
2c624 6d 62 65 72 22 2c 20 20 20 20 20 20 20 20 22 74  mber",        "t
2c625 72 69 67 67 65 72 5f 64 65 63 6c 22 2c 20 20 22  rigger_decl",  "
2c626 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
2c627 22 2c 20 20 22 74 72 69 67 67 65 72 5f 74 69 6d  ",  "trigger_tim
2c628 65 22 2c 0a 20 20 22 74 72 69 67 67 65 72 5f 65  e",.  "trigger_e
2c629 76 65 6e 74 22 2c 20 20 22 66 6f 72 65 61 63 68  vent",  "foreach
2c62a 5f 63 6c 61 75 73 65 22 2c 20 20 22 77 68 65 6e  _clause",  "when
2c62b 5f 63 6c 61 75 73 65 22 2c 20 20 20 22 74 72 69  _clause",   "tri
2c62c 67 67 65 72 5f 63 6d 64 22 2c 20 0a 20 20 22 64  gger_cmd", .  "d
2c62d 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 22 2c  atabase_kw_opt",
2c62e 20 20 22 6b 65 79 5f 6f 70 74 22 2c 20 20 20 20    "key_opt",    
2c62f 20 20 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66     "add_column_f
2c630 75 6c 6c 6e 61 6d 65 22 2c 20 20 22 6b 77 63 6f  ullname",  "kwco
2c631 6c 75 6d 6e 5f 6f 70 74 22 2c 0a 20 20 22 63 72  lumn_opt",.  "cr
2c632 65 61 74 65 5f 76 74 61 62 22 2c 20 20 20 22 76  eate_vtab",   "v
2c633 74 61 62 61 72 67 6c 69 73 74 22 2c 20 20 20 22  tabarglist",   "
2c634 76 74 61 62 61 72 67 22 2c 20 20 20 20 20 20 20  vtabarg",       
2c635 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 0a  "vtabargtoken",.
2c636 20 20 22 6c 70 22 2c 20 20 20 20 20 20 20 20 20    "lp",         
2c637 20 20 20 22 61 6e 79 6c 69 73 74 22 2c 20 20 20     "anylist",   
2c638 20 20 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20    .};.#endif /* 
2c639 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64  NDEBUG */..#ifnd
2c63a 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72  ef NDEBUG./* For
2c63b 20 74 72 61 63 69 6e 67 20 72 65 64 75 63 65 20   tracing reduce 
2c63c 61 63 74 69 6f 6e 73 2c 20 74 68 65 20 6e 61 6d  actions, the nam
2c63d 65 73 20 6f 66 20 61 6c 6c 20 72 75 6c 65 73 20  es of all rules 
2c63e 61 72 65 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f  are required..*/
2c63f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
2c640 61 72 20 2a 63 6f 6e 73 74 20 79 79 52 75 6c 65  ar *const yyRule
2c641 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20  Name[] = {. /*  
2c642 20 30 20 2a 2f 20 22 69 6e 70 75 74 20 3a 3a 3d   0 */ "input ::=
2c643 20 63 6d 64 6c 69 73 74 22 2c 0a 20 2f 2a 20 20   cmdlist",. /*  
2c644 20 31 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a   1 */ "cmdlist :
2c645 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63 6d 64 22  := cmdlist ecmd"
2c646 2c 0a 20 2f 2a 20 20 20 32 20 2a 2f 20 22 63 6d  ,. /*   2 */ "cm
2c647 64 6c 69 73 74 20 3a 3a 3d 20 65 63 6d 64 22 2c  dlist ::= ecmd",
2c648 0a 20 2f 2a 20 20 20 33 20 2a 2f 20 22 65 63 6d  . /*   3 */ "ecm
2c649 64 20 3a 3a 3d 20 53 45 4d 49 22 2c 0a 20 2f 2a  d ::= SEMI",. /*
2c64a 20 20 20 34 20 2a 2f 20 22 65 63 6d 64 20 3a 3a     4 */ "ecmd ::
2c64b 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64 78 20 53  = explain cmdx S
2c64c 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 35 20 2a 2f  EMI",. /*   5 */
2c64d 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 22 2c 0a   "explain ::=",.
2c64e 20 2f 2a 20 20 20 36 20 2a 2f 20 22 65 78 70 6c   /*   6 */ "expl
2c64f 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 22  ain ::= EXPLAIN"
2c650 2c 0a 20 2f 2a 20 20 20 37 20 2a 2f 20 22 65 78  ,. /*   7 */ "ex
2c651 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49  plain ::= EXPLAI
2c652 4e 20 51 55 45 52 59 20 50 4c 41 4e 22 2c 0a 20  N QUERY PLAN",. 
2c653 2f 2a 20 20 20 38 20 2a 2f 20 22 63 6d 64 78 20  /*   8 */ "cmdx 
2c654 3a 3a 3d 20 63 6d 64 22 2c 0a 20 2f 2a 20 20 20  ::= cmd",. /*   
2c655 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 42 45  9 */ "cmd ::= BE
2c656 47 49 4e 20 74 72 61 6e 73 74 79 70 65 20 74 72  GIN transtype tr
2c657 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31  ans_opt",. /*  1
2c658 30 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20  0 */ "trans_opt 
2c659 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 31 20 2a 2f  ::=",. /*  11 */
2c65a 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20   "trans_opt ::= 
2c65b 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c 0a 20 2f  TRANSACTION",. /
2c65c 2a 20 20 31 32 20 2a 2f 20 22 74 72 61 6e 73 5f  *  12 */ "trans_
2c65d 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54  opt ::= TRANSACT
2c65e 49 4f 4e 20 6e 6d 22 2c 0a 20 2f 2a 20 20 31 33  ION nm",. /*  13
2c65f 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a   */ "transtype :
2c660 3a 3d 22 2c 0a 20 2f 2a 20 20 31 34 20 2a 2f 20  :=",. /*  14 */ 
2c661 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44  "transtype ::= D
2c662 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20 31  EFERRED",. /*  1
2c663 35 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20  5 */ "transtype 
2c664 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 22 2c 0a  ::= IMMEDIATE",.
2c665 20 2f 2a 20 20 31 36 20 2a 2f 20 22 74 72 61 6e   /*  16 */ "tran
2c666 73 74 79 70 65 20 3a 3a 3d 20 45 58 43 4c 55 53  stype ::= EXCLUS
2c667 49 56 45 22 2c 0a 20 2f 2a 20 20 31 37 20 2a 2f  IVE",. /*  17 */
2c668 20 22 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54   "cmd ::= COMMIT
2c669 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a   trans_opt",. /*
2c66a 20 20 31 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d    18 */ "cmd ::=
2c66b 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 22 2c   END trans_opt",
2c66c 0a 20 2f 2a 20 20 31 39 20 2a 2f 20 22 63 6d 64  . /*  19 */ "cmd
2c66d 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72   ::= ROLLBACK tr
2c66e 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 32  ans_opt",. /*  2
2c66f 30 20 2a 2f 20 22 73 61 76 65 70 6f 69 6e 74 5f  0 */ "savepoint_
2c670 6f 70 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e  opt ::= SAVEPOIN
2c671 54 22 2c 0a 20 2f 2a 20 20 32 31 20 2a 2f 20 22  T",. /*  21 */ "
2c672 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a  savepoint_opt ::
2c673 3d 22 2c 0a 20 2f 2a 20 20 32 32 20 2a 2f 20 22  =",. /*  22 */ "
2c674 63 6d 64 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e  cmd ::= SAVEPOIN
2c675 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 33 20 2a  T nm",. /*  23 *
2c676 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 4c 45 41  / "cmd ::= RELEA
2c677 53 45 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74  SE savepoint_opt
2c678 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f   nm",. /*  24 */
2c679 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41   "cmd ::= ROLLBA
2c67a 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f 20  CK trans_opt TO 
2c67b 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d  savepoint_opt nm
2c67c 22 2c 0a 20 2f 2a 20 20 32 35 20 2a 2f 20 22 63  ",. /*  25 */ "c
2c67d 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61  md ::= create_ta
2c67e 62 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65  ble create_table
2c67f 5f 61 72 67 73 22 2c 0a 20 2f 2a 20 20 32 36 20  _args",. /*  26 
2c680 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65  */ "create_table
2c681 20 3a 3a 3d 20 43 52 45 41 54 45 20 74 65 6d 70   ::= CREATE temp
2c682 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73   TABLE ifnotexis
2c683 74 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a  ts nm dbnm",. /*
2c684 20 20 32 37 20 2a 2f 20 22 69 66 6e 6f 74 65 78    27 */ "ifnotex
2c685 69 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  ists ::=",. /*  
2c686 32 38 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73  28 */ "ifnotexis
2c687 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58  ts ::= IF NOT EX
2c688 49 53 54 53 22 2c 0a 20 2f 2a 20 20 32 39 20 2a  ISTS",. /*  29 *
2c689 2f 20 22 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50  / "temp ::= TEMP
2c68a 22 2c 0a 20 2f 2a 20 20 33 30 20 2a 2f 20 22 74  ",. /*  30 */ "t
2c68b 65 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 33  emp ::=",. /*  3
2c68c 31 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62  1 */ "create_tab
2c68d 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63  le_args ::= LP c
2c68e 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69  olumnlist consli
2c68f 73 74 5f 6f 70 74 20 52 50 22 2c 0a 20 2f 2a 20  st_opt RP",. /* 
2c690 20 33 32 20 2a 2f 20 22 63 72 65 61 74 65 5f 74   32 */ "create_t
2c691 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53  able_args ::= AS
2c692 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 20 33   select",. /*  3
2c693 33 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74  3 */ "columnlist
2c694 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20   ::= columnlist 
2c695 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22 2c 0a 20  COMMA column",. 
2c696 2f 2a 20 20 33 34 20 2a 2f 20 22 63 6f 6c 75 6d  /*  34 */ "colum
2c697 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e  nlist ::= column
2c698 22 2c 0a 20 2f 2a 20 20 33 35 20 2a 2f 20 22 63  ",. /*  35 */ "c
2c699 6f 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e  olumn ::= column
2c69a 69 64 20 74 79 70 65 20 63 61 72 67 6c 69 73 74  id type carglist
2c69b 22 2c 0a 20 2f 2a 20 20 33 36 20 2a 2f 20 22 63  ",. /*  36 */ "c
2c69c 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 22 2c  olumnid ::= nm",
2c69d 0a 20 2f 2a 20 20 33 37 20 2a 2f 20 22 69 64 20  . /*  37 */ "id 
2c69e 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20 33 38  ::= ID",. /*  38
2c69f 20 2a 2f 20 22 69 64 73 20 3a 3a 3d 20 49 44 7c   */ "ids ::= ID|
2c6a0 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 33 39  STRING",. /*  39
2c6a1 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 49 44 22 2c   */ "nm ::= ID",
2c6a2 0a 20 2f 2a 20 20 34 30 20 2a 2f 20 22 6e 6d 20  . /*  40 */ "nm 
2c6a3 3a 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a  ::= STRING",. /*
2c6a4 20 20 34 31 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20    41 */ "nm ::= 
2c6a5 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 20 34  JOIN_KW",. /*  4
2c6a6 32 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 22 2c  2 */ "type ::=",
2c6a7 0a 20 2f 2a 20 20 34 33 20 2a 2f 20 22 74 79 70  . /*  43 */ "typ
2c6a8 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 22  e ::= typetoken"
2c6a9 2c 0a 20 2f 2a 20 20 34 34 20 2a 2f 20 22 74 79  ,. /*  44 */ "ty
2c6aa 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65  petoken ::= type
2c6ab 6e 61 6d 65 22 2c 0a 20 2f 2a 20 20 34 35 20 2a  name",. /*  45 *
2c6ac 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d  / "typetoken ::=
2c6ad 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67   typename LP sig
2c6ae 6e 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 36  ned RP",. /*  46
2c6af 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a   */ "typetoken :
2c6b0 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73  := typename LP s
2c6b1 69 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67 6e  igned COMMA sign
2c6b2 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 37 20  ed RP",. /*  47 
2c6b3 2a 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d  */ "typename ::=
2c6b4 20 69 64 73 22 2c 0a 20 2f 2a 20 20 34 38 20 2a   ids",. /*  48 *
2c6b5 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  / "typename ::= 
2c6b6 74 79 70 65 6e 61 6d 65 20 69 64 73 22 2c 0a 20  typename ids",. 
2c6b7 2f 2a 20 20 34 39 20 2a 2f 20 22 73 69 67 6e 65  /*  49 */ "signe
2c6b8 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c  d ::= plus_num",
2c6b9 0a 20 2f 2a 20 20 35 30 20 2a 2f 20 22 73 69 67  . /*  50 */ "sig
2c6ba 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75  ned ::= minus_nu
2c6bb 6d 22 2c 0a 20 2f 2a 20 20 35 31 20 2a 2f 20 22  m",. /*  51 */ "
2c6bc 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72  carglist ::= car
2c6bd 67 6c 69 73 74 20 63 61 72 67 22 2c 0a 20 2f 2a  glist carg",. /*
2c6be 20 20 35 32 20 2a 2f 20 22 63 61 72 67 6c 69 73    52 */ "carglis
2c6bf 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 35 33 20  t ::=",. /*  53 
2c6c0 2a 2f 20 22 63 61 72 67 20 3a 3a 3d 20 43 4f 4e  */ "carg ::= CON
2c6c1 53 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73  STRAINT nm ccons
2c6c2 22 2c 0a 20 2f 2a 20 20 35 34 20 2a 2f 20 22 63  ",. /*  54 */ "c
2c6c3 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a  arg ::= ccons",.
2c6c4 20 2f 2a 20 20 35 35 20 2a 2f 20 22 63 63 6f 6e   /*  55 */ "ccon
2c6c5 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65  s ::= DEFAULT te
2c6c6 72 6d 22 2c 0a 20 2f 2a 20 20 35 36 20 2a 2f 20  rm",. /*  56 */ 
2c6c7 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55  "ccons ::= DEFAU
2c6c8 4c 54 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a  LT LP expr RP",.
2c6c9 20 2f 2a 20 20 35 37 20 2a 2f 20 22 63 63 6f 6e   /*  57 */ "ccon
2c6ca 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c  s ::= DEFAULT PL
2c6cb 55 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 35  US term",. /*  5
2c6cc 38 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  8 */ "ccons ::= 
2c6cd 44 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74 65  DEFAULT MINUS te
2c6ce 72 6d 22 2c 0a 20 2f 2a 20 20 35 39 20 2a 2f 20  rm",. /*  59 */ 
2c6cf 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55  "ccons ::= DEFAU
2c6d0 4c 54 20 69 64 22 2c 0a 20 2f 2a 20 20 36 30 20  LT id",. /*  60 
2c6d1 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55  */ "ccons ::= NU
2c6d2 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20  LL onconf",. /* 
2c6d3 20 36 31 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   61 */ "ccons ::
2c6d4 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e  = NOT NULL oncon
2c6d5 66 22 2c 0a 20 2f 2a 20 20 36 32 20 2a 2f 20 22  f",. /*  62 */ "
2c6d6 63 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52  ccons ::= PRIMAR
2c6d7 59 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20  Y KEY sortorder 
2c6d8 6f 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 22 2c  onconf autoinc",
2c6d9 0a 20 2f 2a 20 20 36 33 20 2a 2f 20 22 63 63 6f  . /*  63 */ "cco
2c6da 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e  ns ::= UNIQUE on
2c6db 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 34 20 2a  conf",. /*  64 *
2c6dc 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45  / "ccons ::= CHE
2c6dd 43 4b 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a  CK LP expr RP",.
2c6de 20 2f 2a 20 20 36 35 20 2a 2f 20 22 63 63 6f 6e   /*  65 */ "ccon
2c6df 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53  s ::= REFERENCES
2c6e0 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   nm idxlist_opt 
2c6e1 72 65 66 61 72 67 73 22 2c 0a 20 2f 2a 20 20 36  refargs",. /*  6
2c6e2 36 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  6 */ "ccons ::= 
2c6e3 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22  defer_subclause"
2c6e4 2c 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22 63 63  ,. /*  67 */ "cc
2c6e5 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20  ons ::= COLLATE 
2c6e6 69 64 73 22 2c 0a 20 2f 2a 20 20 36 38 20 2a 2f  ids",. /*  68 */
2c6e7 20 22 61 75 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a   "autoinc ::=",.
2c6e8 20 2f 2a 20 20 36 39 20 2a 2f 20 22 61 75 74 6f   /*  69 */ "auto
2c6e9 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52  inc ::= AUTOINCR
2c6ea 22 2c 0a 20 2f 2a 20 20 37 30 20 2a 2f 20 22 72  ",. /*  70 */ "r
2c6eb 65 66 61 72 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a  efargs ::=",. /*
2c6ec 20 20 37 31 20 2a 2f 20 22 72 65 66 61 72 67 73    71 */ "refargs
2c6ed 20 3a 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66   ::= refargs ref
2c6ee 61 72 67 22 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f  arg",. /*  72 */
2c6ef 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54   "refarg ::= MAT
2c6f0 43 48 20 6e 6d 22 2c 0a 20 2f 2a 20 20 37 33 20  CH nm",. /*  73 
2c6f1 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f  */ "refarg ::= O
2c6f2 4e 20 44 45 4c 45 54 45 20 72 65 66 61 63 74 22  N DELETE refact"
2c6f3 2c 0a 20 2f 2a 20 20 37 34 20 2a 2f 20 22 72 65  ,. /*  74 */ "re
2c6f4 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41  farg ::= ON UPDA
2c6f5 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20  TE refact",. /* 
2c6f6 20 37 35 20 2a 2f 20 22 72 65 66 61 72 67 20 3a   75 */ "refarg :
2c6f7 3a 3d 20 4f 4e 20 49 4e 53 45 52 54 20 72 65 66  := ON INSERT ref
2c6f8 61 63 74 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f  act",. /*  76 */
2c6f9 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54   "refact ::= SET
2c6fa 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 20 37 37 20   NULL",. /*  77 
2c6fb 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53  */ "refact ::= S
2c6fc 45 54 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a  ET DEFAULT",. /*
2c6fd 20 20 37 38 20 2a 2f 20 22 72 65 66 61 63 74 20    78 */ "refact 
2c6fe 3a 3a 3d 20 43 41 53 43 41 44 45 22 2c 0a 20 2f  ::= CASCADE",. /
2c6ff 2a 20 20 37 39 20 2a 2f 20 22 72 65 66 61 63 74  *  79 */ "refact
2c700 20 3a 3a 3d 20 52 45 53 54 52 49 43 54 22 2c 0a   ::= RESTRICT",.
2c701 20 2f 2a 20 20 38 30 20 2a 2f 20 22 64 65 66 65   /*  80 */ "defe
2c702 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20  r_subclause ::= 
2c703 4e 4f 54 20 44 45 46 45 52 52 41 42 4c 45 20 69  NOT DEFERRABLE i
2c704 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
2c705 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 38 31 20  d_opt",. /*  81 
2c706 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61  */ "defer_subcla
2c707 75 73 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42  use ::= DEFERRAB
2c708 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64  LE init_deferred
2c709 5f 70 72 65 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20  _pred_opt",. /* 
2c70a 20 38 32 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66   82 */ "init_def
2c70b 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a  erred_pred_opt :
2c70c 3a 3d 22 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f 20  :=",. /*  83 */ 
2c70d 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70  "init_deferred_p
2c70e 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54  red_opt ::= INIT
2c70f 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 22 2c  IALLY DEFERRED",
2c710 0a 20 2f 2a 20 20 38 34 20 2a 2f 20 22 69 6e 69  . /*  84 */ "ini
2c711 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
2c712 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c  opt ::= INITIALL
2c713 59 20 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f  Y IMMEDIATE",. /
2c714 2a 20 20 38 35 20 2a 2f 20 22 63 6f 6e 73 6c 69  *  85 */ "consli
2c715 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  st_opt ::=",. /*
2c716 20 20 38 36 20 2a 2f 20 22 63 6f 6e 73 6c 69 73    86 */ "conslis
2c717 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20  t_opt ::= COMMA 
2c718 63 6f 6e 73 6c 69 73 74 22 2c 0a 20 2f 2a 20 20  conslist",. /*  
2c719 38 37 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20  87 */ "conslist 
2c71a 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d  ::= conslist COM
2c71b 4d 41 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20  MA tcons",. /*  
2c71c 38 38 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20  88 */ "conslist 
2c71d 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f  ::= conslist tco
2c71e 6e 73 22 2c 0a 20 2f 2a 20 20 38 39 20 2a 2f 20  ns",. /*  89 */ 
2c71f 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63  "conslist ::= tc
2c720 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39 30 20 2a 2f  ons",. /*  90 */
2c721 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53   "tcons ::= CONS
2c722 54 52 41 49 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20  TRAINT nm",. /* 
2c723 20 39 31 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a   91 */ "tcons ::
2c724 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50  = PRIMARY KEY LP
2c725 20 69 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63   idxlist autoinc
2c726 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a   RP onconf",. /*
2c727 20 20 39 32 20 2a 2f 20 22 74 63 6f 6e 73 20 3a    92 */ "tcons :
2c728 3a 3d 20 55 4e 49 51 55 45 20 4c 50 20 69 64 78  := UNIQUE LP idx
2c729 6c 69 73 74 20 52 50 20 6f 6e 63 6f 6e 66 22 2c  list RP onconf",
2c72a 0a 20 2f 2a 20 20 39 33 20 2a 2f 20 22 74 63 6f  . /*  93 */ "tco
2c72b 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20  ns ::= CHECK LP 
2c72c 65 78 70 72 20 52 50 20 6f 6e 63 6f 6e 66 22 2c  expr RP onconf",
2c72d 0a 20 2f 2a 20 20 39 34 20 2a 2f 20 22 74 63 6f  . /*  94 */ "tco
2c72e 6e 73 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b  ns ::= FOREIGN K
2c72f 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50  EY LP idxlist RP
2c730 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69   REFERENCES nm i
2c731 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72  dxlist_opt refar
2c732 67 73 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  gs defer_subclau
2c733 73 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 39 35  se_opt",. /*  95
2c734 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c   */ "defer_subcl
2c735 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  ause_opt ::=",. 
2c736 2f 2a 20 20 39 36 20 2a 2f 20 22 64 65 66 65 72  /*  96 */ "defer
2c737 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a  _subclause_opt :
2c738 3a 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  := defer_subclau
2c739 73 65 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f 20  se",. /*  97 */ 
2c73a 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f  "onconf ::=",. /
2c73b 2a 20 20 39 38 20 2a 2f 20 22 6f 6e 63 6f 6e 66  *  98 */ "onconf
2c73c 20 3a 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54   ::= ON CONFLICT
2c73d 20 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20   resolvetype",. 
2c73e 2f 2a 20 20 39 39 20 2a 2f 20 22 6f 72 63 6f 6e  /*  99 */ "orcon
2c73f 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 30 20  f ::=",. /* 100 
2c740 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f  */ "orconf ::= O
2c741 52 20 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a  R resolvetype",.
2c742 20 2f 2a 20 31 30 31 20 2a 2f 20 22 72 65 73 6f   /* 101 */ "reso
2c743 6c 76 65 74 79 70 65 20 3a 3a 3d 20 72 61 69 73  lvetype ::= rais
2c744 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30 32 20  etype",. /* 102 
2c745 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20  */ "resolvetype 
2c746 3a 3a 3d 20 49 47 4e 4f 52 45 22 2c 0a 20 2f 2a  ::= IGNORE",. /*
2c747 20 31 30 33 20 2a 2f 20 22 72 65 73 6f 6c 76 65   103 */ "resolve
2c748 74 79 70 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45  type ::= REPLACE
2c749 22 2c 0a 20 2f 2a 20 31 30 34 20 2a 2f 20 22 63  ",. /* 104 */ "c
2c74a 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c  md ::= DROP TABL
2c74b 45 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  E ifexists fulln
2c74c 61 6d 65 22 2c 0a 20 2f 2a 20 31 30 35 20 2a 2f  ame",. /* 105 */
2c74d 20 22 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 49   "ifexists ::= I
2c74e 46 20 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20 31  F EXISTS",. /* 1
2c74f 30 36 20 2a 2f 20 22 69 66 65 78 69 73 74 73 20  06 */ "ifexists 
2c750 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 37 20 2a 2f  ::=",. /* 107 */
2c751 20 22 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45   "cmd ::= CREATE
2c752 20 74 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74   temp VIEW ifnot
2c753 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41  exists nm dbnm A
2c754 53 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31  S select",. /* 1
2c755 30 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44  08 */ "cmd ::= D
2c756 52 4f 50 20 56 49 45 57 20 69 66 65 78 69 73 74  ROP VIEW ifexist
2c757 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a  s fullname",. /*
2c758 20 31 30 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   109 */ "cmd ::=
2c759 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31   select",. /* 11
2c75a 30 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d  0 */ "select ::=
2c75b 20 6f 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a   oneselect",. /*
2c75c 20 31 31 31 20 2a 2f 20 22 73 65 6c 65 63 74 20   111 */ "select 
2c75d 3a 3a 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74 69  ::= select multi
2c75e 73 65 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c  select_op onesel
2c75f 65 63 74 22 2c 0a 20 2f 2a 20 31 31 32 20 2a 2f  ect",. /* 112 */
2c760 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70   "multiselect_op
2c761 20 3a 3a 3d 20 55 4e 49 4f 4e 22 2c 0a 20 2f 2a   ::= UNION",. /*
2c762 20 31 31 33 20 2a 2f 20 22 6d 75 6c 74 69 73 65   113 */ "multise
2c763 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f  lect_op ::= UNIO
2c764 4e 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 31 34 20  N ALL",. /* 114 
2c765 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f  */ "multiselect_
2c766 6f 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e  op ::= EXCEPT|IN
2c767 54 45 52 53 45 43 54 22 2c 0a 20 2f 2a 20 31 31  TERSECT",. /* 11
2c768 35 20 2a 2f 20 22 6f 6e 65 73 65 6c 65 63 74 20  5 */ "oneselect 
2c769 3a 3a 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69  ::= SELECT disti
2c76a 6e 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66  nct selcollist f
2c76b 72 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72  rom where_opt gr
2c76c 6f 75 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67  oupby_opt having
2c76d 5f 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74  _opt orderby_opt
2c76e 20 6c 69 6d 69 74 5f 6f 70 74 22 2c 0a 20 2f 2a   limit_opt",. /*
2c76f 20 31 31 36 20 2a 2f 20 22 64 69 73 74 69 6e 63   116 */ "distinc
2c770 74 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54 22 2c  t ::= DISTINCT",
2c771 0a 20 2f 2a 20 31 31 37 20 2a 2f 20 22 64 69 73  . /* 117 */ "dis
2c772 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0a  tinct ::= ALL",.
2c773 20 2f 2a 20 31 31 38 20 2a 2f 20 22 64 69 73 74   /* 118 */ "dist
2c774 69 6e 63 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  inct ::=",. /* 1
2c775 31 39 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 20  19 */ "sclp ::= 
2c776 73 65 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41  selcollist COMMA
2c777 22 2c 0a 20 2f 2a 20 31 32 30 20 2a 2f 20 22 73  ",. /* 120 */ "s
2c778 63 6c 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32  clp ::=",. /* 12
2c779 31 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74  1 */ "selcollist
2c77a 20 3a 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61   ::= sclp expr a
2c77b 73 22 2c 0a 20 2f 2a 20 31 32 32 20 2a 2f 20 22  s",. /* 122 */ "
2c77c 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73  selcollist ::= s
2c77d 63 6c 70 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31  clp STAR",. /* 1
2c77e 32 33 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73  23 */ "selcollis
2c77f 74 20 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f  t ::= sclp nm DO
2c780 54 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 32 34  T STAR",. /* 124
2c781 20 2a 2f 20 22 61 73 20 3a 3a 3d 20 41 53 20 6e   */ "as ::= AS n
2c782 6d 22 2c 0a 20 2f 2a 20 31 32 35 20 2a 2f 20 22  m",. /* 125 */ "
2c783 61 73 20 3a 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a  as ::= ids",. /*
2c784 20 31 32 36 20 2a 2f 20 22 61 73 20 3a 3a 3d 22   126 */ "as ::="
2c785 2c 0a 20 2f 2a 20 31 32 37 20 2a 2f 20 22 66 72  ,. /* 127 */ "fr
2c786 6f 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 38  om ::=",. /* 128
2c787 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d 20 46 52   */ "from ::= FR
2c788 4f 4d 20 73 65 6c 74 61 62 6c 69 73 74 22 2c 0a  OM seltablist",.
2c789 20 2f 2a 20 31 32 39 20 2a 2f 20 22 73 74 6c 5f   /* 129 */ "stl_
2c78a 70 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61  prefix ::= selta
2c78b 62 6c 69 73 74 20 6a 6f 69 6e 6f 70 22 2c 0a 20  blist joinop",. 
2c78c 2f 2a 20 31 33 30 20 2a 2f 20 22 73 74 6c 5f 70  /* 130 */ "stl_p
2c78d 72 65 66 69 78 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  refix ::=",. /* 
2c78e 31 33 31 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69  131 */ "seltabli
2c78f 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69  st ::= stl_prefi
2c790 78 20 6e 6d 20 64 62 6e 6d 20 61 73 20 69 6e 64  x nm dbnm as ind
2c791 65 78 65 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20  exed_opt on_opt 
2c792 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20  using_opt",. /* 
2c793 31 33 32 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69  132 */ "seltabli
2c794 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69  st ::= stl_prefi
2c795 78 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 61  x LP select RP a
2c796 73 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f  s on_opt using_o
2c797 70 74 22 2c 0a 20 2f 2a 20 31 33 33 20 2a 2f 20  pt",. /* 133 */ 
2c798 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20  "seltablist ::= 
2c799 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65  stl_prefix LP se
2c79a 6c 74 61 62 6c 69 73 74 20 52 50 20 61 73 20 6f  ltablist RP as o
2c79b 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22  n_opt using_opt"
2c79c 2c 0a 20 2f 2a 20 31 33 34 20 2a 2f 20 22 64 62  ,. /* 134 */ "db
2c79d 6e 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 35  nm ::=",. /* 135
2c79e 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 20 44 4f   */ "dbnm ::= DO
2c79f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 33 36 20 2a  T nm",. /* 136 *
2c7a0 2f 20 22 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20  / "fullname ::= 
2c7a1 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 31 33  nm dbnm",. /* 13
2c7a2 37 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d  7 */ "joinop ::=
2c7a3 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 22 2c 0a 20 2f   COMMA|JOIN",. /
2c7a4 2a 20 31 33 38 20 2a 2f 20 22 6a 6f 69 6e 6f 70  * 138 */ "joinop
2c7a5 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49   ::= JOIN_KW JOI
2c7a6 4e 22 2c 0a 20 2f 2a 20 31 33 39 20 2a 2f 20 22  N",. /* 139 */ "
2c7a7 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
2c7a8 4b 57 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a  KW nm JOIN",. /*
2c7a9 20 31 34 30 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20   140 */ "joinop 
2c7aa 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e  ::= JOIN_KW nm n
2c7ab 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 31  m JOIN",. /* 141
2c7ac 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 20   */ "on_opt ::= 
2c7ad 4f 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 34  ON expr",. /* 14
2c7ae 32 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d  2 */ "on_opt ::=
2c7af 22 2c 0a 20 2f 2a 20 31 34 33 20 2a 2f 20 22 69  ",. /* 143 */ "i
2c7b0 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c  ndexed_opt ::=",
2c7b1 0a 20 2f 2a 20 31 34 34 20 2a 2f 20 22 69 6e 64  . /* 144 */ "ind
2c7b2 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44  exed_opt ::= IND
2c7b3 45 58 45 44 20 42 59 20 6e 6d 22 2c 0a 20 2f 2a  EXED BY nm",. /*
2c7b4 20 31 34 35 20 2a 2f 20 22 69 6e 64 65 78 65 64   145 */ "indexed
2c7b5 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44  _opt ::= NOT IND
2c7b6 45 58 45 44 22 2c 0a 20 2f 2a 20 31 34 36 20 2a  EXED",. /* 146 *
2c7b7 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d  / "using_opt ::=
2c7b8 20 55 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c   USING LP inscol
2c7b9 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 34  list RP",. /* 14
2c7ba 37 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20  7 */ "using_opt 
2c7bb 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f  ::=",. /* 148 */
2c7bc 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a   "orderby_opt ::
2c7bd 3d 22 2c 0a 20 2f 2a 20 31 34 39 20 2a 2f 20 22  =",. /* 149 */ "
2c7be 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20  orderby_opt ::= 
2c7bf 4f 52 44 45 52 20 42 59 20 73 6f 72 74 6c 69 73  ORDER BY sortlis
2c7c0 74 22 2c 0a 20 2f 2a 20 31 35 30 20 2a 2f 20 22  t",. /* 150 */ "
2c7c1 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72  sortlist ::= sor
2c7c2 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72 74  tlist COMMA sort
2c7c3 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c  item sortorder",
2c7c4 0a 20 2f 2a 20 31 35 31 20 2a 2f 20 22 73 6f 72  . /* 151 */ "sor
2c7c5 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74  tlist ::= sortit
2c7c6 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20  em sortorder",. 
2c7c7 2f 2a 20 31 35 32 20 2a 2f 20 22 73 6f 72 74 69  /* 152 */ "sorti
2c7c8 74 65 6d 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20  tem ::= expr",. 
2c7c9 2f 2a 20 31 35 33 20 2a 2f 20 22 73 6f 72 74 6f  /* 153 */ "sorto
2c7ca 72 64 65 72 20 3a 3a 3d 20 41 53 43 22 2c 0a 20  rder ::= ASC",. 
2c7cb 2f 2a 20 31 35 34 20 2a 2f 20 22 73 6f 72 74 6f  /* 154 */ "sorto
2c7cc 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 22 2c 0a  rder ::= DESC",.
2c7cd 20 2f 2a 20 31 35 35 20 2a 2f 20 22 73 6f 72 74   /* 155 */ "sort
2c7ce 6f 72 64 65 72 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  order ::=",. /* 
2c7cf 31 35 36 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f  156 */ "groupby_
2c7d0 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35  opt ::=",. /* 15
2c7d1 37 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70  7 */ "groupby_op
2c7d2 74 20 3a 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e  t ::= GROUP BY n
2c7d3 65 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 31  exprlist",. /* 1
2c7d4 35 38 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70  58 */ "having_op
2c7d5 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 39 20  t ::=",. /* 159 
2c7d6 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a  */ "having_opt :
2c7d7 3a 3d 20 48 41 56 49 4e 47 20 65 78 70 72 22 2c  := HAVING expr",
2c7d8 0a 20 2f 2a 20 31 36 30 20 2a 2f 20 22 6c 69 6d  . /* 160 */ "lim
2c7d9 69 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  it_opt ::=",. /*
2c7da 20 31 36 31 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f   161 */ "limit_o
2c7db 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
2c7dc 72 22 2c 0a 20 2f 2a 20 31 36 32 20 2a 2f 20 22  r",. /* 162 */ "
2c7dd 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49  limit_opt ::= LI
2c7de 4d 49 54 20 65 78 70 72 20 4f 46 46 53 45 54 20  MIT expr OFFSET 
2c7df 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 33 20 2a  expr",. /* 163 *
2c7e0 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d  / "limit_opt ::=
2c7e1 20 4c 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d   LIMIT expr COMM
2c7e2 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 34  A expr",. /* 164
2c7e3 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 4c   */ "cmd ::= DEL
2c7e4 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d  ETE FROM fullnam
2c7e5 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 77 68  e indexed_opt wh
2c7e6 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 36  ere_opt",. /* 16
2c7e7 35 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20  5 */ "where_opt 
2c7e8 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 36 20 2a 2f  ::=",. /* 166 */
2c7e9 20 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20   "where_opt ::= 
2c7ea 57 48 45 52 45 20 65 78 70 72 22 2c 0a 20 2f 2a  WHERE expr",. /*
2c7eb 20 31 36 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   167 */ "cmd ::=
2c7ec 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 66   UPDATE orconf f
2c7ed 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f  ullname indexed_
2c7ee 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73 74 20  opt SET setlist 
2c7ef 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20  where_opt",. /* 
2c7f0 31 36 38 20 2a 2f 20 22 73 65 74 6c 69 73 74 20  168 */ "setlist 
2c7f1 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d  ::= setlist COMM
2c7f2 41 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20  A nm EQ expr",. 
2c7f3 2f 2a 20 31 36 39 20 2a 2f 20 22 73 65 74 6c 69  /* 169 */ "setli
2c7f4 73 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70  st ::= nm EQ exp
2c7f5 72 22 2c 0a 20 2f 2a 20 31 37 30 20 2a 2f 20 22  r",. /* 170 */ "
2c7f6 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
2c7f7 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
2c7f8 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
2c7f9 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69  VALUES LP itemli
2c7fa 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 37 31 20  st RP",. /* 171 
2c7fb 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  */ "cmd ::= inse
2c7fc 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
2c7fd 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
2c7fe 6f 70 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a  opt select",. /*
2c7ff 20 31 37 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   172 */ "cmd ::=
2c800 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
2c801 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c   fullname inscol
2c802 6c 69 73 74 5f 6f 70 74 20 44 45 46 41 55 4c 54  list_opt DEFAULT
2c803 20 56 41 4c 55 45 53 22 2c 0a 20 2f 2a 20 31 37   VALUES",. /* 17
2c804 33 20 2a 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64  3 */ "insert_cmd
2c805 20 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72 63 6f   ::= INSERT orco
2c806 6e 66 22 2c 0a 20 2f 2a 20 31 37 34 20 2a 2f 20  nf",. /* 174 */ 
2c807 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20  "insert_cmd ::= 
2c808 52 45 50 4c 41 43 45 22 2c 0a 20 2f 2a 20 31 37  REPLACE",. /* 17
2c809 35 20 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a  5 */ "itemlist :
2c80a 3a 3d 20 69 74 65 6d 6c 69 73 74 20 43 4f 4d 4d  := itemlist COMM
2c80b 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 36  A expr",. /* 176
2c80c 20 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a   */ "itemlist ::
2c80d 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 37  = expr",. /* 177
2c80e 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f   */ "inscollist_
2c80f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 37  opt ::=",. /* 17
2c810 38 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74  8 */ "inscollist
2c811 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63  _opt ::= LP insc
2c812 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  ollist RP",. /* 
2c813 31 37 39 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69  179 */ "inscolli
2c814 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73  st ::= inscollis
2c815 74 20 43 4f 4d 4d 41 20 6e 6d 22 2c 0a 20 2f 2a  t COMMA nm",. /*
2c816 20 31 38 30 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c   180 */ "inscoll
2c817 69 73 74 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a  ist ::= nm",. /*
2c818 20 31 38 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a   181 */ "expr ::
2c819 3d 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 31 38 32  = term",. /* 182
2c81a 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50   */ "expr ::= LP
2c81b 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 31   expr RP",. /* 1
2c81c 38 33 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20  83 */ "term ::= 
2c81d 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 31 38 34 20 2a  NULL",. /* 184 *
2c81e 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44 22 2c  / "expr ::= ID",
2c81f 0a 20 2f 2a 20 31 38 35 20 2a 2f 20 22 65 78 70  . /* 185 */ "exp
2c820 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a  r ::= JOIN_KW",.
2c821 20 2f 2a 20 31 38 36 20 2a 2f 20 22 65 78 70 72   /* 186 */ "expr
2c822 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c   ::= nm DOT nm",
2c823 0a 20 2f 2a 20 31 38 37 20 2a 2f 20 22 65 78 70  . /* 187 */ "exp
2c824 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20  r ::= nm DOT nm 
2c825 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 38  DOT nm",. /* 188
2c826 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 49 4e   */ "term ::= IN
2c827 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42  TEGER|FLOAT|BLOB
2c828 22 2c 0a 20 2f 2a 20 31 38 39 20 2a 2f 20 22 74  ",. /* 189 */ "t
2c829 65 72 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c  erm ::= STRING",
2c82a 0a 20 2f 2a 20 31 39 30 20 2a 2f 20 22 65 78 70  . /* 190 */ "exp
2c82b 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52 22 2c  r ::= REGISTER",
2c82c 0a 20 2f 2a 20 31 39 31 20 2a 2f 20 22 65 78 70  . /* 191 */ "exp
2c82d 72 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 22 2c  r ::= VARIABLE",
2c82e 0a 20 2f 2a 20 31 39 32 20 2a 2f 20 22 65 78 70  . /* 192 */ "exp
2c82f 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4c 4c 41  r ::= expr COLLA
2c830 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 31 39 33  TE ids",. /* 193
2c831 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 43 41   */ "expr ::= CA
2c832 53 54 20 4c 50 20 65 78 70 72 20 41 53 20 74 79  ST LP expr AS ty
2c833 70 65 74 6f 6b 65 6e 20 52 50 22 2c 0a 20 2f 2a  petoken RP",. /*
2c834 20 31 39 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   194 */ "expr ::
2c835 3d 20 49 44 20 4c 50 20 64 69 73 74 69 6e 63 74  = ID LP distinct
2c836 20 65 78 70 72 6c 69 73 74 20 52 50 22 2c 0a 20   exprlist RP",. 
2c837 2f 2a 20 31 39 35 20 2a 2f 20 22 65 78 70 72 20  /* 195 */ "expr 
2c838 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52 20 52  ::= ID LP STAR R
2c839 50 22 2c 0a 20 2f 2a 20 31 39 36 20 2a 2f 20 22  P",. /* 196 */ "
2c83a 74 65 72 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b  term ::= CTIME_K
2c83b 57 22 2c 0a 20 2f 2a 20 31 39 37 20 2a 2f 20 22  W",. /* 197 */ "
2c83c 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e  expr ::= expr AN
2c83d 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39 38  D expr",. /* 198
2c83e 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2c83f 70 72 20 4f 52 20 65 78 70 72 22 2c 0a 20 2f 2a  pr OR expr",. /*
2c840 20 31 39 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a   199 */ "expr ::
2c841 3d 20 65 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c  = expr LT|GT|GE|
2c842 4c 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30  LE expr",. /* 20
2c843 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  0 */ "expr ::= e
2c844 78 70 72 20 45 51 7c 4e 45 20 65 78 70 72 22 2c  xpr EQ|NE expr",
2c845 0a 20 2f 2a 20 32 30 31 20 2a 2f 20 22 65 78 70  . /* 201 */ "exp
2c846 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e  r ::= expr BITAN
2c847 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52  D|BITOR|LSHIFT|R
2c848 53 48 49 46 54 20 65 78 70 72 22 2c 0a 20 2f 2a  SHIFT expr",. /*
2c849 20 32 30 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a   202 */ "expr ::
2c84a 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d 49 4e 55  = expr PLUS|MINU
2c84b 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 33  S expr",. /* 203
2c84c 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2c84d 70 72 20 53 54 41 52 7c 53 4c 41 53 48 7c 52 45  pr STAR|SLASH|RE
2c84e 4d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 34  M expr",. /* 204
2c84f 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2c850 70 72 20 43 4f 4e 43 41 54 20 65 78 70 72 22 2c  pr CONCAT expr",
2c851 0a 20 2f 2a 20 32 30 35 20 2a 2f 20 22 6c 69 6b  . /* 205 */ "lik
2c852 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b 57 22  eop ::= LIKE_KW"
2c853 2c 0a 20 2f 2a 20 32 30 36 20 2a 2f 20 22 6c 69  ,. /* 206 */ "li
2c854 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b  keop ::= NOT LIK
2c855 45 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30 37 20 2a  E_KW",. /* 207 *
2c856 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41  / "likeop ::= MA
2c857 54 43 48 22 2c 0a 20 2f 2a 20 32 30 38 20 2a 2f  TCH",. /* 208 */
2c858 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54   "likeop ::= NOT
2c859 20 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32 30 39   MATCH",. /* 209
2c85a 20 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d 20   */ "escape ::= 
2c85b 45 53 43 41 50 45 20 65 78 70 72 22 2c 0a 20 2f  ESCAPE expr",. /
2c85c 2a 20 32 31 30 20 2a 2f 20 22 65 73 63 61 70 65  * 210 */ "escape
2c85d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 31 31 20 2a   ::=",. /* 211 *
2c85e 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
2c85f 20 6c 69 6b 65 6f 70 20 65 78 70 72 20 65 73 63   likeop expr esc
2c860 61 70 65 22 2c 0a 20 2f 2a 20 32 31 32 20 2a 2f  ape",. /* 212 */
2c861 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
2c862 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 22 2c  ISNULL|NOTNULL",
2c863 0a 20 2f 2a 20 32 31 33 20 2a 2f 20 22 65 78 70  . /* 213 */ "exp
2c864 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 55  r ::= expr IS NU
2c865 4c 4c 22 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20  LL",. /* 214 */ 
2c866 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4e  "expr ::= expr N
2c867 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31  OT NULL",. /* 21
2c868 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  5 */ "expr ::= e
2c869 78 70 72 20 49 53 20 4e 4f 54 20 4e 55 4c 4c 22  xpr IS NOT NULL"
2c86a 2c 0a 20 2f 2a 20 32 31 36 20 2a 2f 20 22 65 78  ,. /* 216 */ "ex
2c86b 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 22  pr ::= NOT expr"
2c86c 2c 0a 20 2f 2a 20 32 31 37 20 2a 2f 20 22 65 78  ,. /* 217 */ "ex
2c86d 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78  pr ::= BITNOT ex
2c86e 70 72 22 2c 0a 20 2f 2a 20 32 31 38 20 2a 2f 20  pr",. /* 218 */ 
2c86f 22 65 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20  "expr ::= MINUS 
2c870 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 39 20 2a  expr",. /* 219 *
2c871 2f 20 22 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53  / "expr ::= PLUS
2c872 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 30 20   expr",. /* 220 
2c873 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a  */ "between_op :
2c874 3a 3d 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a  := BETWEEN",. /*
2c875 20 32 32 31 20 2a 2f 20 22 62 65 74 77 65 65 6e   221 */ "between
2c876 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57  _op ::= NOT BETW
2c877 45 45 4e 22 2c 0a 20 2f 2a 20 32 32 32 20 2a 2f  EEN",. /* 222 */
2c878 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
2c879 62 65 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20  between_op expr 
2c87a 41 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  AND expr",. /* 2
2c87b 32 33 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d  23 */ "in_op ::=
2c87c 20 49 4e 22 2c 0a 20 2f 2a 20 32 32 34 20 2a 2f   IN",. /* 224 */
2c87d 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20   "in_op ::= NOT 
2c87e 49 4e 22 2c 0a 20 2f 2a 20 32 32 35 20 2a 2f 20  IN",. /* 225 */ 
2c87f 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69  "expr ::= expr i
2c880 6e 5f 6f 70 20 4c 50 20 65 78 70 72 6c 69 73 74  n_op LP exprlist
2c881 20 52 50 22 2c 0a 20 2f 2a 20 32 32 36 20 2a 2f   RP",. /* 226 */
2c882 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65   "expr ::= LP se
2c883 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32  lect RP",. /* 22
2c884 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  7 */ "expr ::= e
2c885 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c  xpr in_op LP sel
2c886 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32 38  ect RP",. /* 228
2c887 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2c888 70 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d  pr in_op nm dbnm
2c889 22 2c 0a 20 2f 2a 20 32 32 39 20 2a 2f 20 22 65  ",. /* 229 */ "e
2c88a 78 70 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c  xpr ::= EXISTS L
2c88b 50 20 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f  P select RP",. /
2c88c 2a 20 32 33 30 20 2a 2f 20 22 65 78 70 72 20 3a  * 230 */ "expr :
2c88d 3a 3d 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65  := CASE case_ope
2c88e 72 61 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69  rand case_exprli
2c88f 73 74 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44  st case_else END
2c890 22 2c 0a 20 2f 2a 20 32 33 31 20 2a 2f 20 22 63  ",. /* 231 */ "c
2c891 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
2c892 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57   case_exprlist W
2c893 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
2c894 70 72 22 2c 0a 20 2f 2a 20 32 33 32 20 2a 2f 20  pr",. /* 232 */ 
2c895 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a  "case_exprlist :
2c896 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48 45  := WHEN expr THE
2c897 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 33  N expr",. /* 233
2c898 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65 20 3a   */ "case_else :
2c899 3a 3d 20 45 4c 53 45 20 65 78 70 72 22 2c 0a 20  := ELSE expr",. 
2c89a 2f 2a 20 32 33 34 20 2a 2f 20 22 63 61 73 65 5f  /* 234 */ "case_
2c89b 65 6c 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  else ::=",. /* 2
2c89c 33 35 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72  35 */ "case_oper
2c89d 61 6e 64 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20  and ::= expr",. 
2c89e 2f 2a 20 32 33 36 20 2a 2f 20 22 63 61 73 65 5f  /* 236 */ "case_
2c89f 6f 70 65 72 61 6e 64 20 3a 3a 3d 22 2c 0a 20 2f  operand ::=",. /
2c8a0 2a 20 32 33 37 20 2a 2f 20 22 65 78 70 72 6c 69  * 237 */ "exprli
2c8a1 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74  st ::= nexprlist
2c8a2 22 2c 0a 20 2f 2a 20 32 33 38 20 2a 2f 20 22 65  ",. /* 238 */ "e
2c8a3 78 70 72 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f  xprlist ::=",. /
2c8a4 2a 20 32 33 39 20 2a 2f 20 22 6e 65 78 70 72 6c  * 239 */ "nexprl
2c8a5 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73  ist ::= nexprlis
2c8a6 74 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20  t COMMA expr",. 
2c8a7 2f 2a 20 32 34 30 20 2a 2f 20 22 6e 65 78 70 72  /* 240 */ "nexpr
2c8a8 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a  list ::= expr",.
2c8a9 20 2f 2a 20 32 34 31 20 2a 2f 20 22 63 6d 64 20   /* 241 */ "cmd 
2c8aa 3a 3a 3d 20 43 52 45 41 54 45 20 75 6e 69 71 75  ::= CREATE uniqu
2c8ab 65 66 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f  eflag INDEX ifno
2c8ac 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20  texists nm dbnm 
2c8ad 4f 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74  ON nm LP idxlist
2c8ae 20 52 50 22 2c 0a 20 2f 2a 20 32 34 32 20 2a 2f   RP",. /* 242 */
2c8af 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d   "uniqueflag ::=
2c8b0 20 55 4e 49 51 55 45 22 2c 0a 20 2f 2a 20 32 34   UNIQUE",. /* 24
2c8b1 33 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67  3 */ "uniqueflag
2c8b2 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 34 20 2a   ::=",. /* 244 *
2c8b3 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a  / "idxlist_opt :
2c8b4 3a 3d 22 2c 0a 20 2f 2a 20 32 34 35 20 2a 2f 20  :=",. /* 245 */ 
2c8b5 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  "idxlist_opt ::=
2c8b6 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c   LP idxlist RP",
2c8b7 0a 20 2f 2a 20 32 34 36 20 2a 2f 20 22 69 64 78  . /* 246 */ "idx
2c8b8 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74  list ::= idxlist
2c8b9 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74   COMMA nm collat
2c8ba 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f  e sortorder",. /
2c8bb 2a 20 32 34 37 20 2a 2f 20 22 69 64 78 6c 69 73  * 247 */ "idxlis
2c8bc 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65  t ::= nm collate
2c8bd 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a   sortorder",. /*
2c8be 20 32 34 38 20 2a 2f 20 22 63 6f 6c 6c 61 74 65   248 */ "collate
2c8bf 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 39 20 2a   ::=",. /* 249 *
2c8c0 2f 20 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43  / "collate ::= C
2c8c1 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a  OLLATE ids",. /*
2c8c2 20 32 35 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   250 */ "cmd ::=
2c8c3 20 44 52 4f 50 20 49 4e 44 45 58 20 69 66 65 78   DROP INDEX ifex
2c8c4 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a  ists fullname",.
2c8c5 20 2f 2a 20 32 35 31 20 2a 2f 20 22 63 6d 64 20   /* 251 */ "cmd 
2c8c6 3a 3a 3d 20 56 41 43 55 55 4d 22 2c 0a 20 2f 2a  ::= VACUUM",. /*
2c8c7 20 32 35 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   252 */ "cmd ::=
2c8c8 20 56 41 43 55 55 4d 20 6e 6d 22 2c 0a 20 2f 2a   VACUUM nm",. /*
2c8c9 20 32 35 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   253 */ "cmd ::=
2c8ca 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
2c8cb 45 51 20 6e 6d 6e 75 6d 22 2c 0a 20 2f 2a 20 32  EQ nmnum",. /* 2
2c8cc 35 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50  54 */ "cmd ::= P
2c8cd 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
2c8ce 20 4f 4e 22 2c 0a 20 2f 2a 20 32 35 35 20 2a 2f   ON",. /* 255 */
2c8cf 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41   "cmd ::= PRAGMA
2c8d0 20 6e 6d 20 64 62 6e 6d 20 45 51 20 44 45 4c 45   nm dbnm EQ DELE
2c8d1 54 45 22 2c 0a 20 2f 2a 20 32 35 36 20 2a 2f 20  TE",. /* 256 */ 
2c8d2 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20  "cmd ::= PRAGMA 
2c8d3 6e 6d 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73  nm dbnm EQ minus
2c8d4 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 32 35 37 20 2a  _num",. /* 257 *
2c8d5 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  / "cmd ::= PRAGM
2c8d6 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e  A nm dbnm LP nmn
2c8d7 75 6d 20 52 50 22 2c 0a 20 2f 2a 20 32 35 38 20  um RP",. /* 258 
2c8d8 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47  */ "cmd ::= PRAG
2c8d9 4d 41 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a  MA nm dbnm",. /*
2c8da 20 32 35 39 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a   259 */ "nmnum :
2c8db 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f  := plus_num",. /
2c8dc 2a 20 32 36 30 20 2a 2f 20 22 6e 6d 6e 75 6d 20  * 260 */ "nmnum 
2c8dd 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 36 31  ::= nm",. /* 261
2c8de 20 2a 2f 20 22 70 6c 75 73 5f 6e 75 6d 20 3a 3a   */ "plus_num ::
2c8df 3d 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65  = plus_opt numbe
2c8e0 72 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22  r",. /* 262 */ "
2c8e1 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d 49  minus_num ::= MI
2c8e2 4e 55 53 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a  NUS number",. /*
2c8e3 20 32 36 33 20 2a 2f 20 22 6e 75 6d 62 65 72 20   263 */ "number 
2c8e4 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41  ::= INTEGER|FLOA
2c8e5 54 22 2c 0a 20 2f 2a 20 32 36 34 20 2a 2f 20 22  T",. /* 264 */ "
2c8e6 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55  plus_opt ::= PLU
2c8e7 53 22 2c 0a 20 2f 2a 20 32 36 35 20 2a 2f 20 22  S",. /* 265 */ "
2c8e8 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  plus_opt ::=",. 
2c8e9 2f 2a 20 32 36 36 20 2a 2f 20 22 63 6d 64 20 3a  /* 266 */ "cmd :
2c8ea 3a 3d 20 43 52 45 41 54 45 20 74 72 69 67 67 65  := CREATE trigge
2c8eb 72 5f 64 65 63 6c 20 42 45 47 49 4e 20 74 72 69  r_decl BEGIN tri
2c8ec 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e  gger_cmd_list EN
2c8ed 44 22 2c 0a 20 2f 2a 20 32 36 37 20 2a 2f 20 22  D",. /* 267 */ "
2c8ee 74 72 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d  trigger_decl ::=
2c8ef 20 74 65 6d 70 20 54 52 49 47 47 45 52 20 69 66   temp TRIGGER if
2c8f0 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e  notexists nm dbn
2c8f1 6d 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 74  m trigger_time t
2c8f2 72 69 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20  rigger_event ON 
2c8f3 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68  fullname foreach
2c8f4 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c 61  _clause when_cla
2c8f5 75 73 65 22 2c 0a 20 2f 2a 20 32 36 38 20 2a 2f  use",. /* 268 */
2c8f6 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a   "trigger_time :
2c8f7 3a 3d 20 42 45 46 4f 52 45 22 2c 0a 20 2f 2a 20  := BEFORE",. /* 
2c8f8 32 36 39 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  269 */ "trigger_
2c8f9 74 69 6d 65 20 3a 3a 3d 20 41 46 54 45 52 22 2c  time ::= AFTER",
2c8fa 0a 20 2f 2a 20 32 37 30 20 2a 2f 20 22 74 72 69  . /* 270 */ "tri
2c8fb 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e  gger_time ::= IN
2c8fc 53 54 45 41 44 20 4f 46 22 2c 0a 20 2f 2a 20 32  STEAD OF",. /* 2
2c8fd 37 31 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74  71 */ "trigger_t
2c8fe 69 6d 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37  ime ::=",. /* 27
2c8ff 32 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76  2 */ "trigger_ev
2c900 65 6e 74 20 3a 3a 3d 20 44 45 4c 45 54 45 7c 49  ent ::= DELETE|I
2c901 4e 53 45 52 54 22 2c 0a 20 2f 2a 20 32 37 33 20  NSERT",. /* 273 
2c902 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e  */ "trigger_even
2c903 74 20 3a 3a 3d 20 55 50 44 41 54 45 22 2c 0a 20  t ::= UPDATE",. 
2c904 2f 2a 20 32 37 34 20 2a 2f 20 22 74 72 69 67 67  /* 274 */ "trigg
2c905 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44  er_event ::= UPD
2c906 41 54 45 20 4f 46 20 69 6e 73 63 6f 6c 6c 69 73  ATE OF inscollis
2c907 74 22 2c 0a 20 2f 2a 20 32 37 35 20 2a 2f 20 22  t",. /* 275 */ "
2c908 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a  foreach_clause :
2c909 3a 3d 22 2c 0a 20 2f 2a 20 32 37 36 20 2a 2f 20  :=",. /* 276 */ 
2c90a 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20  "foreach_clause 
2c90b 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20 52 4f 57  ::= FOR EACH ROW
2c90c 22 2c 0a 20 2f 2a 20 32 37 37 20 2a 2f 20 22 77  ",. /* 277 */ "w
2c90d 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 22 2c  hen_clause ::=",
2c90e 0a 20 2f 2a 20 32 37 38 20 2a 2f 20 22 77 68 65  . /* 278 */ "whe
2c90f 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45  n_clause ::= WHE
2c910 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 37 39  N expr",. /* 279
2c911 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64   */ "trigger_cmd
2c912 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65  _list ::= trigge
2c913 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72 69 67 67  r_cmd_list trigg
2c914 65 72 5f 63 6d 64 20 53 45 4d 49 22 2c 0a 20 2f  er_cmd SEMI",. /
2c915 2a 20 32 38 30 20 2a 2f 20 22 74 72 69 67 67 65  * 280 */ "trigge
2c916 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74  r_cmd_list ::= t
2c917 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 22  rigger_cmd SEMI"
2c918 2c 0a 20 2f 2a 20 32 38 31 20 2a 2f 20 22 74 72  ,. /* 281 */ "tr
2c919 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 55 50  igger_cmd ::= UP
2c91a 44 41 54 45 20 6f 72 63 6f 6e 66 20 6e 6d 20 53  DATE orconf nm S
2c91b 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65  ET setlist where
2c91c 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 38 32 20 2a  _opt",. /* 282 *
2c91d 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  / "trigger_cmd :
2c91e 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
2c91f 54 4f 20 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74  TO nm inscollist
2c920 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69  _opt VALUES LP i
2c921 74 65 6d 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  temlist RP",. /*
2c922 20 32 38 33 20 2a 2f 20 22 74 72 69 67 67 65 72   283 */ "trigger
2c923 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  _cmd ::= insert_
2c924 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63  cmd INTO nm insc
2c925 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63  ollist_opt selec
2c926 74 22 2c 0a 20 2f 2a 20 32 38 34 20 2a 2f 20 22  t",. /* 284 */ "
2c927 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
2c928 44 45 4c 45 54 45 20 46 52 4f 4d 20 6e 6d 20 77  DELETE FROM nm w
2c929 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32  here_opt",. /* 2
2c92a 38 35 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  85 */ "trigger_c
2c92b 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22 2c 0a  md ::= select",.
2c92c 20 2f 2a 20 32 38 36 20 2a 2f 20 22 65 78 70 72   /* 286 */ "expr
2c92d 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 49 47   ::= RAISE LP IG
2c92e 4e 4f 52 45 20 52 50 22 2c 0a 20 2f 2a 20 32 38  NORE RP",. /* 28
2c92f 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52  7 */ "expr ::= R
2c930 41 49 53 45 20 4c 50 20 72 61 69 73 65 74 79 70  AISE LP raisetyp
2c931 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50 22 2c 0a  e COMMA nm RP",.
2c932 20 2f 2a 20 32 38 38 20 2a 2f 20 22 72 61 69 73   /* 288 */ "rais
2c933 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41  etype ::= ROLLBA
2c934 43 4b 22 2c 0a 20 2f 2a 20 32 38 39 20 2a 2f 20  CK",. /* 289 */ 
2c935 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 41  "raisetype ::= A
2c936 42 4f 52 54 22 2c 0a 20 2f 2a 20 32 39 30 20 2a  BORT",. /* 290 *
2c937 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d  / "raisetype ::=
2c938 20 46 41 49 4c 22 2c 0a 20 2f 2a 20 32 39 31 20   FAIL",. /* 291 
2c939 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50  */ "cmd ::= DROP
2c93a 20 54 52 49 47 47 45 52 20 69 66 65 78 69 73 74   TRIGGER ifexist
2c93b 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a  s fullname",. /*
2c93c 20 32 39 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   292 */ "cmd ::=
2c93d 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65   ATTACH database
2c93e 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20  _kw_opt expr AS 
2c93f 65 78 70 72 20 6b 65 79 5f 6f 70 74 22 2c 0a 20  expr key_opt",. 
2c940 2f 2a 20 32 39 33 20 2a 2f 20 22 63 6d 64 20 3a  /* 293 */ "cmd :
2c941 3a 3d 20 44 45 54 41 43 48 20 64 61 74 61 62 61  := DETACH databa
2c942 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 22 2c  se_kw_opt expr",
2c943 0a 20 2f 2a 20 32 39 34 20 2a 2f 20 22 6b 65 79  . /* 294 */ "key
2c944 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  _opt ::=",. /* 2
2c945 39 35 20 2a 2f 20 22 6b 65 79 5f 6f 70 74 20 3a  95 */ "key_opt :
2c946 3a 3d 20 4b 45 59 20 65 78 70 72 22 2c 0a 20 2f  := KEY expr",. /
2c947 2a 20 32 39 36 20 2a 2f 20 22 64 61 74 61 62 61  * 296 */ "databa
2c948 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41  se_kw_opt ::= DA
2c949 54 41 42 41 53 45 22 2c 0a 20 2f 2a 20 32 39 37  TABASE",. /* 297
2c94a 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f 6b 77   */ "database_kw
2c94b 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  _opt ::=",. /* 2
2c94c 39 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52  98 */ "cmd ::= R
2c94d 45 49 4e 44 45 58 22 2c 0a 20 2f 2a 20 32 39 39  EINDEX",. /* 299
2c94e 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49   */ "cmd ::= REI
2c94f 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20  NDEX nm dbnm",. 
2c950 2f 2a 20 33 30 30 20 2a 2f 20 22 63 6d 64 20 3a  /* 300 */ "cmd :
2c951 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c 0a 20 2f 2a  := ANALYZE",. /*
2c952 20 33 30 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   301 */ "cmd ::=
2c953 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d   ANALYZE nm dbnm
2c954 22 2c 0a 20 2f 2a 20 33 30 32 20 2a 2f 20 22 63  ",. /* 302 */ "c
2c955 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42  md ::= ALTER TAB
2c956 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41  LE fullname RENA
2c957 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20 2f 2a 20 33  ME TO nm",. /* 3
2c958 30 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41  03 */ "cmd ::= A
2c959 4c 54 45 52 20 54 41 42 4c 45 20 61 64 64 5f 63  LTER TABLE add_c
2c95a 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 41  olumn_fullname A
2c95b 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20  DD kwcolumn_opt 
2c95c 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 33 30 34  column",. /* 304
2c95d 20 2a 2f 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f   */ "add_column_
2c95e 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c  fullname ::= ful
2c95f 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 33 30 35 20  lname",. /* 305 
2c960 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74  */ "kwcolumn_opt
2c961 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 36 20 2a   ::=",. /* 306 *
2c962 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20  / "kwcolumn_opt 
2c963 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 22 2c 0a 20  ::= COLUMNKW",. 
2c964 2f 2a 20 33 30 37 20 2a 2f 20 22 63 6d 64 20 3a  /* 307 */ "cmd :
2c965 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62 22 2c  := create_vtab",
2c966 0a 20 2f 2a 20 33 30 38 20 2a 2f 20 22 63 6d 64  . /* 308 */ "cmd
2c967 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62   ::= create_vtab
2c968 20 4c 50 20 76 74 61 62 61 72 67 6c 69 73 74 20   LP vtabarglist 
2c969 52 50 22 2c 0a 20 2f 2a 20 33 30 39 20 2a 2f 20  RP",. /* 309 */ 
2c96a 22 63 72 65 61 74 65 5f 76 74 61 62 20 3a 3a 3d  "create_vtab ::=
2c96b 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
2c96c 54 41 42 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53  TABLE nm dbnm US
2c96d 49 4e 47 20 6e 6d 22 2c 0a 20 2f 2a 20 33 31 30  ING nm",. /* 310
2c96e 20 2a 2f 20 22 76 74 61 62 61 72 67 6c 69 73 74   */ "vtabarglist
2c96f 20 3a 3a 3d 20 76 74 61 62 61 72 67 22 2c 0a 20   ::= vtabarg",. 
2c970 2f 2a 20 33 31 31 20 2a 2f 20 22 76 74 61 62 61  /* 311 */ "vtaba
2c971 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61  rglist ::= vtaba
2c972 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20 76 74 61  rglist COMMA vta
2c973 62 61 72 67 22 2c 0a 20 2f 2a 20 33 31 32 20 2a  barg",. /* 312 *
2c974 2f 20 22 76 74 61 62 61 72 67 20 3a 3a 3d 22 2c  / "vtabarg ::=",
2c975 0a 20 2f 2a 20 33 31 33 20 2a 2f 20 22 76 74 61  . /* 313 */ "vta
2c976 62 61 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67  barg ::= vtabarg
2c977 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 0a   vtabargtoken",.
2c978 20 2f 2a 20 33 31 34 20 2a 2f 20 22 76 74 61 62   /* 314 */ "vtab
2c979 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59  argtoken ::= ANY
2c97a 22 2c 0a 20 2f 2a 20 33 31 35 20 2a 2f 20 22 76  ",. /* 315 */ "v
2c97b 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20  tabargtoken ::= 
2c97c 6c 70 20 61 6e 79 6c 69 73 74 20 52 50 22 2c 0a  lp anylist RP",.
2c97d 20 2f 2a 20 33 31 36 20 2a 2f 20 22 6c 70 20 3a   /* 316 */ "lp :
2c97e 3a 3d 20 4c 50 22 2c 0a 20 2f 2a 20 33 31 37 20  := LP",. /* 317 
2c97f 2a 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 22  */ "anylist ::="
2c980 2c 0a 20 2f 2a 20 33 31 38 20 2a 2f 20 22 61 6e  ,. /* 318 */ "an
2c981 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73  ylist ::= anylis
2c982 74 20 41 4e 59 22 2c 0a 7d 3b 0a 23 65 6e 64 69  t ANY",.};.#endi
2c983 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a  f /* NDEBUG */..
2c984 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54  .#if YYSTACKDEPT
2c985 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74  H<=0./*.** Try t
2c986 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73  o increase the s
2c987 69 7a 65 20 6f 66 20 74 68 65 20 70 61 72 73 65  ize of the parse
2c988 72 20 73 74 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74  r stack..*/.stat
2c989 69 63 20 76 6f 69 64 20 79 79 47 72 6f 77 53 74  ic void yyGrowSt
2c98a 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a 70 29  ack(yyParser *p)
2c98b 7b 0a 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b  {.  int newSize;
2c98c 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  .  yyStackEntry 
2c98d 2a 70 4e 65 77 3b 0a 0a 20 20 6e 65 77 53 69 7a  *pNew;..  newSiz
2c98e 65 20 3d 20 70 2d 3e 79 79 73 74 6b 73 7a 2a 32  e = p->yystksz*2
2c98f 20 2b 20 31 30 30 3b 0a 20 20 70 4e 65 77 20 3d   + 100;.  pNew =
2c990 20 72 65 61 6c 6c 6f 63 28 70 2d 3e 79 79 73 74   realloc(p->yyst
2c991 61 63 6b 2c 20 6e 65 77 53 69 7a 65 2a 73 69 7a  ack, newSize*siz
2c992 65 6f 66 28 70 4e 65 77 5b 30 5d 29 29 3b 0a 20  eof(pNew[0]));. 
2c993 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
2c994 20 70 2d 3e 79 79 73 74 61 63 6b 20 3d 20 70 4e   p->yystack = pN
2c995 65 77 3b 0a 20 20 20 20 70 2d 3e 79 79 73 74 6b  ew;.    p->yystk
2c996 73 7a 20 3d 20 6e 65 77 53 69 7a 65 3b 0a 23 69  sz = newSize;.#i
2c997 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
2c998 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
2c999 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
2c99a 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
2c99b 73 53 74 61 63 6b 20 67 72 6f 77 73 20 74 6f 20  sStack grows to 
2c99c 25 64 20 65 6e 74 72 69 65 73 21 5c 6e 22 2c 0a  %d entries!\n",.
2c99d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79                yy
2c99e 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 70 2d 3e  TracePrompt, p->
2c99f 79 79 73 74 6b 73 7a 29 3b 0a 20 20 20 20 7d 0a  yystksz);.    }.
2c9a0 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 23 65 6e  #endif.  }.}.#en
2c9a1 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73  dif../* .** This
2c9a2 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61   function alloca
2c9a3 74 65 73 20 61 20 6e 65 77 20 70 61 72 73 65 72  tes a new parser
2c9a4 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 61 72  ..** The only ar
2c9a5 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e  gument is a poin
2c9a6 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
2c9a7 6e 20 77 68 69 63 68 20 77 6f 72 6b 73 20 6c 69  n which works li
2c9a8 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a  ke.** malloc..**
2c9a9 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 41  .** Inputs:.** A
2c9aa 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2c9ab 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f  function used to
2c9ac 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   allocate memory
2c9ad 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a  ..**.** Outputs:
2c9ae 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  .** A pointer to
2c9af 20 61 20 70 61 72 73 65 72 2e 20 20 54 68 69 73   a parser.  This
2c9b0 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64   pointer is used
2c9b1 20 69 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63   in subsequent c
2c9b2 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  alls.** to sqlit
2c9b3 65 33 50 61 72 73 65 72 20 61 6e 64 20 73 71 6c  e3Parser and sql
2c9b4 69 74 65 33 50 61 72 73 65 72 46 72 65 65 2e 0a  ite3ParserFree..
2c9b5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2c9b6 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50  E void *sqlite3P
2c9b7 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 20  arserAlloc(void 
2c9b8 2a 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 73  *(*mallocProc)(s
2c9b9 69 7a 65 5f 74 29 29 7b 0a 20 20 79 79 50 61 72  ize_t)){.  yyPar
2c9ba 73 65 72 20 2a 70 50 61 72 73 65 72 3b 0a 20 20  ser *pParser;.  
2c9bb 70 50 61 72 73 65 72 20 3d 20 28 79 79 50 61 72  pParser = (yyPar
2c9bc 73 65 72 2a 29 28 2a 6d 61 6c 6c 6f 63 50 72 6f  ser*)(*mallocPro
2c9bd 63 29 28 20 28 73 69 7a 65 5f 74 29 73 69 7a 65  c)( (size_t)size
2c9be 6f 66 28 79 79 50 61 72 73 65 72 29 20 29 3b 0a  of(yyParser) );.
2c9bf 20 20 69 66 28 20 70 50 61 72 73 65 72 20 29 7b    if( pParser ){
2c9c0 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79  .    pParser->yy
2c9c1 69 64 78 20 3d 20 2d 31 3b 0a 23 69 66 64 65 66  idx = -1;.#ifdef
2c9c2 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b   YYTRACKMAXSTACK
2c9c3 44 45 50 54 48 0a 20 20 20 20 70 50 61 72 73 65  DEPTH.    pParse
2c9c4 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20 30 3b  r->yyidxMax = 0;
2c9c5 0a 23 65 6e 64 69 66 0a 23 69 66 20 59 59 53 54  .#endif.#if YYST
2c9c6 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20  ACKDEPTH<=0.    
2c9c7 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
2c9c8 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 70 50 61   = NULL;.    pPa
2c9c9 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 3d 20  rser->yystksz = 
2c9ca 30 3b 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61  0;.    yyGrowSta
2c9cb 63 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 65 6e  ck(pParser);.#en
2c9cc 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  dif.  }.  return
2c9cd 20 70 50 61 72 73 65 72 3b 0a 7d 0a 0a 2f 2a 20   pParser;.}../* 
2c9ce 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75  The following fu
2c9cf 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74  nction deletes t
2c9d0 68 65 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61  he value associa
2c9d1 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 73 79  ted with a.** sy
2c9d2 6d 62 6f 6c 2e 20 20 54 68 65 20 73 79 6d 62 6f  mbol.  The symbo
2c9d3 6c 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  l can be either 
2c9d4 61 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f  a terminal or no
2c9d5 6e 74 65 72 6d 69 6e 61 6c 2e 0a 2a 2a 20 22 79  nterminal..** "y
2c9d6 79 6d 61 6a 6f 72 22 20 69 73 20 74 68 65 20 73  ymajor" is the s
2c9d7 79 6d 62 6f 6c 20 63 6f 64 65 2c 20 61 6e 64 20  ymbol code, and 
2c9d8 22 79 79 70 6d 69 6e 6f 72 22 20 69 73 20 61 20  "yypminor" is a 
2c9d9 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68  pointer to.** th
2c9da 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  e value..*/.stat
2c9db 69 63 20 76 6f 69 64 20 79 79 5f 64 65 73 74 72  ic void yy_destr
2c9dc 75 63 74 6f 72 28 0a 20 20 79 79 50 61 72 73 65  uctor(.  yyParse
2c9dd 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20  r *yypParser,   
2c9de 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
2c9df 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 79  /.  YYCODETYPE y
2c9e0 79 6d 61 6a 6f 72 2c 20 20 20 20 20 2f 2a 20 54  ymajor,     /* T
2c9e1 79 70 65 20 63 6f 64 65 20 66 6f 72 20 6f 62 6a  ype code for obj
2c9e2 65 63 74 20 74 6f 20 64 65 73 74 72 6f 79 20 2a  ect to destroy *
2c9e3 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20  /.  YYMINORTYPE 
2c9e4 2a 79 79 70 6d 69 6e 6f 72 20 20 20 2f 2a 20 54  *yypminor   /* T
2c9e5 68 65 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20  he object to be 
2c9e6 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 29 7b 0a  destroyed */.){.
2c9e7 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
2c9e8 52 47 5f 46 45 54 43 48 3b 0a 20 20 73 77 69 74  RG_FETCH;.  swit
2c9e9 63 68 28 20 79 79 6d 61 6a 6f 72 20 29 7b 0a 20  ch( yymajor ){. 
2c9ea 20 20 20 2f 2a 20 48 65 72 65 20 69 73 20 69 6e     /* Here is in
2c9eb 73 65 72 74 65 64 20 74 68 65 20 61 63 74 69 6f  serted the actio
2c9ec 6e 73 20 77 68 69 63 68 20 74 61 6b 65 20 70 6c  ns which take pl
2c9ed 61 63 65 20 77 68 65 6e 20 61 0a 20 20 20 20 2a  ace when a.    *
2c9ee 2a 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f  * terminal or no
2c9ef 6e 2d 74 65 72 6d 69 6e 61 6c 20 69 73 20 64 65  n-terminal is de
2c9f0 73 74 72 6f 79 65 64 2e 20 20 54 68 69 73 20 63  stroyed.  This c
2c9f1 61 6e 20 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a  an happen.    **
2c9f2 20 77 68 65 6e 20 74 68 65 20 73 79 6d 62 6f 6c   when the symbol
2c9f3 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20   is popped from 
2c9f4 74 68 65 20 73 74 61 63 6b 20 64 75 72 69 6e 67  the stack during
2c9f5 20 61 0a 20 20 20 20 2a 2a 20 72 65 64 75 63 65   a.    ** reduce
2c9f6 20 6f 72 20 64 75 72 69 6e 67 20 65 72 72 6f 72   or during error
2c9f7 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 72 20 77   processing or w
2c9f8 68 65 6e 20 61 20 70 61 72 73 65 72 20 69 73 20  hen a parser is 
2c9f9 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 65  .    ** being de
2c9fa 73 74 72 6f 79 65 64 20 62 65 66 6f 72 65 20 69  stroyed before i
2c9fb 74 20 69 73 20 66 69 6e 69 73 68 65 64 20 70 61  t is finished pa
2c9fc 72 73 69 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20  rsing..    **.  
2c9fd 20 20 2a 2a 20 4e 6f 74 65 3a 20 64 75 72 69 6e    ** Note: durin
2c9fe 67 20 61 20 72 65 64 75 63 65 2c 20 74 68 65 20  g a reduce, the 
2c9ff 6f 6e 6c 79 20 73 79 6d 62 6f 6c 73 20 64 65 73  only symbols des
2ca00 74 72 6f 79 65 64 20 61 72 65 20 74 68 6f 73 65  troyed are those
2ca01 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 20 61 70  .    ** which ap
2ca02 70 65 61 72 20 6f 6e 20 74 68 65 20 52 48 53 20  pear on the RHS 
2ca03 6f 66 20 74 68 65 20 72 75 6c 65 2c 20 62 75 74  of the rule, but
2ca04 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 75   which are not u
2ca05 73 65 64 0a 20 20 20 20 2a 2a 20 69 6e 73 69 64  sed.    ** insid
2ca06 65 20 74 68 65 20 43 20 63 6f 64 65 2e 0a 20 20  e the C code..  
2ca07 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 35    */.    case 15
2ca08 39 3a 20 2f 2a 20 73 65 6c 65 63 74 20 2a 2f 0a  9: /* select */.
2ca09 20 20 20 20 63 61 73 65 20 31 39 33 3a 20 2f 2a      case 193: /*
2ca0a 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a   oneselect */.{.
2ca0b 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
2ca0c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
2ca0d 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 35 39  (yypminor->yy159
2ca0e 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61  ));.}.      brea
2ca0f 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 37 33 3a  k;.    case 173:
2ca10 20 2f 2a 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20   /* term */.    
2ca11 63 61 73 65 20 31 37 34 3a 20 2f 2a 20 65 78 70  case 174: /* exp
2ca12 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39  r */.    case 19
2ca13 38 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20  8: /* where_opt 
2ca14 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 30 3a  */.    case 200:
2ca15 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 2a   /* having_opt *
2ca16 2f 0a 20 20 20 20 63 61 73 65 20 32 30 39 3a 20  /.    case 209: 
2ca17 2f 2a 20 6f 6e 5f 6f 70 74 20 2a 2f 0a 20 20 20  /* on_opt */.   
2ca18 20 63 61 73 65 20 32 31 34 3a 20 2f 2a 20 73 6f   case 214: /* so
2ca19 72 74 69 74 65 6d 20 2a 2f 0a 20 20 20 20 63 61  rtitem */.    ca
2ca1a 73 65 20 32 32 32 3a 20 2f 2a 20 65 73 63 61 70  se 222: /* escap
2ca1b 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32  e */.    case 22
2ca1c 35 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61  5: /* case_opera
2ca1d 6e 64 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  nd */.    case 2
2ca1e 32 37 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65  27: /* case_else
2ca1f 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 33 38   */.    case 238
2ca20 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65  : /* when_clause
2ca21 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 34 31   */.    case 241
2ca22 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a  : /* key_opt */.
2ca23 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c  {.sqlite3ExprDel
2ca24 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
2ca25 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 32 32  (yypminor->yy122
2ca26 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61  ));.}.      brea
2ca27 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 37 38 3a  k;.    case 178:
2ca28 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   /* idxlist_opt 
2ca29 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 38 36 3a  */.    case 186:
2ca2a 20 2f 2a 20 69 64 78 6c 69 73 74 20 2a 2f 0a 20   /* idxlist */. 
2ca2b 20 20 20 63 61 73 65 20 31 39 36 3a 20 2f 2a 20     case 196: /* 
2ca2c 73 65 6c 63 6f 6c 6c 69 73 74 20 2a 2f 0a 20 20  selcollist */.  
2ca2d 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a 20 67    case 199: /* g
2ca2e 72 6f 75 70 62 79 5f 6f 70 74 20 2a 2f 0a 20 20  roupby_opt */.  
2ca2f 20 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20 6f    case 201: /* o
2ca30 72 64 65 72 62 79 5f 6f 70 74 20 2a 2f 0a 20 20  rderby_opt */.  
2ca31 20 20 63 61 73 65 20 32 30 33 3a 20 2f 2a 20 73    case 203: /* s
2ca32 63 6c 70 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  clp */.    case 
2ca33 32 31 33 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74  213: /* sortlist
2ca34 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31 35   */.    case 215
2ca35 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 2a  : /* nexprlist *
2ca36 2f 0a 20 20 20 20 63 61 73 65 20 32 31 36 3a 20  /.    case 216: 
2ca37 2f 2a 20 73 65 74 6c 69 73 74 20 2a 2f 0a 20 20  /* setlist */.  
2ca38 20 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20 69    case 219: /* i
2ca39 74 65 6d 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63  temlist */.    c
2ca3a 61 73 65 20 32 32 30 3a 20 2f 2a 20 65 78 70 72  ase 220: /* expr
2ca3b 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
2ca3c 20 32 32 36 3a 20 2f 2a 20 63 61 73 65 5f 65 78   226: /* case_ex
2ca3d 70 72 6c 69 73 74 20 2a 2f 0a 7b 0a 73 71 6c 69  prlist */.{.sqli
2ca3e 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2ca3f 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79  e(pParse->db, (y
2ca40 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 34 32 29 29  ypminor->yy442))
2ca41 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.}.      break;
2ca42 0a 20 20 20 20 63 61 73 65 20 31 39 32 3a 20 2f  .    case 192: /
2ca43 2a 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 20 20  * fullname */.  
2ca44 20 20 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 66    case 197: /* f
2ca45 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  rom */.    case 
2ca46 32 30 35 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69  205: /* seltabli
2ca47 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  st */.    case 2
2ca48 30 36 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69  06: /* stl_prefi
2ca49 78 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 53 72  x */.{.sqlite3Sr
2ca4a 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  cListDelete(pPar
2ca4b 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f  se->db, (yypmino
2ca4c 72 2d 3e 79 79 33 34 37 29 29 3b 0a 7d 0a 20 20  r->yy347));.}.  
2ca4d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
2ca4e 61 73 65 20 32 31 30 3a 20 2f 2a 20 75 73 69 6e  ase 210: /* usin
2ca4f 67 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73  g_opt */.    cas
2ca50 65 20 32 31 32 3a 20 2f 2a 20 69 6e 73 63 6f 6c  e 212: /* inscol
2ca51 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
2ca52 20 32 31 38 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c   218: /* inscoll
2ca53 69 73 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c  ist_opt */.{.sql
2ca54 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
2ca55 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79  (pParse->db, (yy
2ca56 70 6d 69 6e 6f 72 2d 3e 79 79 31 38 30 29 29 3b  pminor->yy180));
2ca57 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .}.      break;.
2ca58 20 20 20 20 63 61 73 65 20 32 33 34 3a 20 2f 2a      case 234: /*
2ca59 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73   trigger_cmd_lis
2ca5a 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 33  t */.    case 23
2ca5b 39 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  9: /* trigger_cm
2ca5c 64 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 44 65  d */.{.sqlite3De
2ca5d 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28  leteTriggerStep(
2ca5e 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70  pParse->db, (yyp
2ca5f 6d 69 6e 6f 72 2d 3e 79 79 33 32 37 29 29 3b 0a  minor->yy327));.
2ca60 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
2ca61 20 20 20 63 61 73 65 20 32 33 36 3a 20 2f 2a 20     case 236: /* 
2ca62 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 2a 2f  trigger_event */
2ca63 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c 69 73 74  .{.sqlite3IdList
2ca64 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
2ca65 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  b, (yypminor->yy
2ca66 34 31 30 29 2e 62 29 3b 0a 7d 0a 20 20 20 20 20  410).b);.}.     
2ca67 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61   break;.    defa
2ca68 75 6c 74 3a 20 20 62 72 65 61 6b 3b 20 20 20 2f  ult:  break;   /
2ca69 2a 20 49 66 20 6e 6f 20 64 65 73 74 72 75 63 74  * If no destruct
2ca6a 6f 72 20 61 63 74 69 6f 6e 20 73 70 65 63 69 66  or action specif
2ca6b 69 65 64 3a 20 64 6f 20 6e 6f 74 68 69 6e 67 20  ied: do nothing 
2ca6c 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  */.  }.}../*.** 
2ca6d 50 6f 70 20 74 68 65 20 70 61 72 73 65 72 27 73  Pop the parser's
2ca6e 20 73 74 61 63 6b 20 6f 6e 63 65 2e 0a 2a 2a 0a   stack once..**.
2ca6f 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  ** If there is a
2ca70 20 64 65 73 74 72 75 63 74 6f 72 20 72 6f 75 74   destructor rout
2ca71 69 6e 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ine associated w
2ca72 69 74 68 20 74 68 65 20 74 6f 6b 65 6e 20 77 68  ith the token wh
2ca73 69 63 68 0a 2a 2a 20 69 73 20 70 6f 70 70 65 64  ich.** is popped
2ca74 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 2c   from the stack,
2ca75 20 74 68 65 6e 20 63 61 6c 6c 20 69 74 2e 0a 2a   then call it..*
2ca76 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
2ca77 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  major token numb
2ca78 65 72 20 66 6f 72 20 74 68 65 20 73 79 6d 62 6f  er for the symbo
2ca79 6c 20 70 6f 70 70 65 64 2e 0a 2a 2f 0a 73 74 61  l popped..*/.sta
2ca7a 74 69 63 20 69 6e 74 20 79 79 5f 70 6f 70 5f 70  tic int yy_pop_p
2ca7b 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 50 61  arser_stack(yyPa
2ca7c 72 73 65 72 20 2a 70 50 61 72 73 65 72 29 7b 0a  rser *pParser){.
2ca7d 20 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 6d    YYCODETYPE yym
2ca7e 61 6a 6f 72 3b 0a 20 20 79 79 53 74 61 63 6b 45  ajor;.  yyStackE
2ca7f 6e 74 72 79 20 2a 79 79 74 6f 73 20 3d 20 26 70  ntry *yytos = &p
2ca80 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
2ca81 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b  pParser->yyidx];
2ca82 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 72 2d  ..  if( pParser-
2ca83 3e 79 79 69 64 78 3c 30 20 29 20 72 65 74 75 72  >yyidx<0 ) retur
2ca84 6e 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  n 0;.#ifndef NDE
2ca85 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63  BUG.  if( yyTrac
2ca86 65 46 49 4c 45 20 26 26 20 70 50 61 72 73 65 72  eFILE && pParser
2ca87 2d 3e 79 79 69 64 78 3e 3d 30 20 29 7b 0a 20 20  ->yyidx>=0 ){.  
2ca88 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
2ca89 65 46 49 4c 45 2c 22 25 73 50 6f 70 70 69 6e 67  eFILE,"%sPopping
2ca8a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 79 79   %s\n",.      yy
2ca8b 54 72 61 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20  TracePrompt,.   
2ca8c 20 20 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79     yyTokenName[y
2ca8d 79 74 6f 73 2d 3e 6d 61 6a 6f 72 5d 29 3b 0a 20  ytos->major]);. 
2ca8e 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 6d 61   }.#endif.  yyma
2ca8f 6a 6f 72 20 3d 20 79 79 74 6f 73 2d 3e 6d 61 6a  jor = yytos->maj
2ca90 6f 72 3b 0a 20 20 79 79 5f 64 65 73 74 72 75 63  or;.  yy_destruc
2ca91 74 6f 72 28 70 50 61 72 73 65 72 2c 20 79 79 6d  tor(pParser, yym
2ca92 61 6a 6f 72 2c 20 26 79 79 74 6f 73 2d 3e 6d 69  ajor, &yytos->mi
2ca93 6e 6f 72 29 3b 0a 20 20 70 50 61 72 73 65 72 2d  nor);.  pParser-
2ca94 3e 79 79 69 64 78 2d 2d 3b 0a 20 20 72 65 74 75  >yyidx--;.  retu
2ca95 72 6e 20 79 79 6d 61 6a 6f 72 3b 0a 7d 0a 0a 2f  rn yymajor;.}../
2ca96 2a 20 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  * .** Deallocate
2ca97 20 61 6e 64 20 64 65 73 74 72 6f 79 20 61 20 70   and destroy a p
2ca98 61 72 73 65 72 2e 20 20 44 65 73 74 72 75 63 74  arser.  Destruct
2ca99 6f 72 73 20 61 72 65 20 61 6c 6c 20 63 61 6c 6c  ors are all call
2ca9a 65 64 20 66 6f 72 0a 2a 2a 20 61 6c 6c 20 73 74  ed for.** all st
2ca9b 61 63 6b 20 65 6c 65 6d 65 6e 74 73 20 62 65 66  ack elements bef
2ca9c 6f 72 65 20 73 68 75 74 74 69 6e 67 20 74 68 65  ore shutting the
2ca9d 20 70 61 72 73 65 72 20 64 6f 77 6e 2e 0a 2a 2a   parser down..**
2ca9e 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c  .** Inputs:.** <
2ca9f 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 41 20 70  ul>.** <li>  A p
2caa0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61  ointer to the pa
2caa1 72 73 65 72 2e 20 20 54 68 69 73 20 73 68 6f 75  rser.  This shou
2caa2 6c 64 20 62 65 20 61 20 70 6f 69 6e 74 65 72 0a  ld be a pointer.
2caa3 2a 2a 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65  **       obtaine
2caa4 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 50 61  d from sqlite3Pa
2caa5 72 73 65 72 41 6c 6c 6f 63 2e 0a 2a 2a 20 3c 6c  rserAlloc..** <l
2caa6 69 3e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  i>  A pointer to
2caa7 20 61 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64   a function used
2caa8 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f   to reclaim memo
2caa9 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20  ry obtained.**  
2caaa 20 20 20 20 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63       from malloc
2caab 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a 53 51  ..** </ul>.*/.SQ
2caac 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2caad 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46  d sqlite3ParserF
2caae 72 65 65 28 0a 20 20 76 6f 69 64 20 2a 70 2c 20  ree(.  void *p, 
2caaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cab0 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
2cab1 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a   to be deleted *
2cab2 2f 0a 20 20 76 6f 69 64 20 28 2a 66 72 65 65 50  /.  void (*freeP
2cab3 72 6f 63 29 28 76 6f 69 64 2a 29 20 20 20 20 20  roc)(void*)     
2cab4 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 75 73 65 64  /* Function used
2cab5 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f   to reclaim memo
2cab6 72 79 20 2a 2f 0a 29 7b 0a 20 20 79 79 50 61 72  ry */.){.  yyPar
2cab7 73 65 72 20 2a 70 50 61 72 73 65 72 20 3d 20 28  ser *pParser = (
2cab8 79 79 50 61 72 73 65 72 2a 29 70 3b 0a 20 20 69  yyParser*)p;.  i
2cab9 66 28 20 70 50 61 72 73 65 72 3d 3d 30 20 29 20  f( pParser==0 ) 
2caba 72 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28  return;.  while(
2cabb 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e   pParser->yyidx>
2cabc 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73  =0 ) yy_pop_pars
2cabd 65 72 5f 73 74 61 63 6b 28 70 50 61 72 73 65 72  er_stack(pParser
2cabe 29 3b 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45  );.#if YYSTACKDE
2cabf 50 54 48 3c 3d 30 0a 20 20 66 72 65 65 28 70 50  PTH<=0.  free(pP
2cac0 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 29 3b  arser->yystack);
2cac1 0a 23 65 6e 64 69 66 0a 20 20 28 2a 66 72 65 65  .#endif.  (*free
2cac2 50 72 6f 63 29 28 28 76 6f 69 64 2a 29 70 50 61  Proc)((void*)pPa
2cac3 72 73 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rser);.}../*.** 
2cac4 52 65 74 75 72 6e 20 74 68 65 20 70 65 61 6b 20  Return the peak 
2cac5 64 65 70 74 68 20 6f 66 20 74 68 65 20 73 74 61  depth of the sta
2cac6 63 6b 20 66 6f 72 20 61 20 70 61 72 73 65 72 2e  ck for a parser.
2cac7 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59 54 52 41  .*/.#ifdef YYTRA
2cac8 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a  CKMAXSTACKDEPTH.
2cac9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2caca 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  nt sqlite3Parser
2cacb 53 74 61 63 6b 50 65 61 6b 28 76 6f 69 64 20 2a  StackPeak(void *
2cacc 70 29 7b 0a 20 20 79 79 50 61 72 73 65 72 20 2a  p){.  yyParser *
2cacd 70 50 61 72 73 65 72 20 3d 20 28 79 79 50 61 72  pParser = (yyPar
2cace 73 65 72 2a 29 70 3b 0a 20 20 72 65 74 75 72 6e  ser*)p;.  return
2cacf 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d   pParser->yyidxM
2cad0 61 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ax;.}.#endif../*
2cad1 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70  .** Find the app
2cad2 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20  ropriate action 
2cad3 66 6f 72 20 61 20 70 61 72 73 65 72 20 67 69 76  for a parser giv
2cad4 65 6e 20 74 68 65 20 74 65 72 6d 69 6e 61 6c 0a  en the terminal.
2cad5 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f  ** look-ahead to
2cad6 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a  ken iLookAhead..
2cad7 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 6f  **.** If the loo
2cad8 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 73  k-ahead token is
2cad9 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65 6e 20   YYNOCODE, then 
2cada 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  check to see if 
2cadb 74 68 65 20 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  the action is.**
2cadc 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f 66 20   independent of 
2cadd 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20  the look-ahead. 
2cade 20 49 66 20 69 74 20 69 73 2c 20 72 65 74 75 72   If it is, retur
2cadf 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74  n the action, ot
2cae0 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74 75 72  herwise.** retur
2cae1 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a  n YY_NO_ACTION..
2cae2 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79  */.static int yy
2cae3 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61 63 74 69  _find_shift_acti
2cae4 6f 6e 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a  on(.  yyParser *
2cae5 70 50 61 72 73 65 72 2c 20 20 20 20 20 20 20 20  pParser,        
2cae6 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
2cae7 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 69 4c  .  YYCODETYPE iL
2cae8 6f 6f 6b 41 68 65 61 64 20 20 20 20 20 2f 2a 20  ookAhead     /* 
2cae9 54 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74  The look-ahead t
2caea 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oken */.){.  int
2caeb 20 69 3b 0a 20 20 69 6e 74 20 73 74 61 74 65 6e   i;.  int staten
2caec 6f 20 3d 20 70 50 61 72 73 65 72 2d 3e 79 79 73  o = pParser->yys
2caed 74 61 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79  tack[pParser->yy
2caee 69 64 78 5d 2e 73 74 61 74 65 6e 6f 3b 0a 20 0a  idx].stateno;. .
2caef 20 20 69 66 28 20 73 74 61 74 65 6e 6f 3e 59 59    if( stateno>YY
2caf0 5f 53 48 49 46 54 5f 4d 41 58 20 7c 7c 20 28 69  _SHIFT_MAX || (i
2caf1 20 3d 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74   = yy_shift_ofst
2caf2 5b 73 74 61 74 65 6e 6f 5d 29 3d 3d 59 59 5f 53  [stateno])==YY_S
2caf3 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 20 29 7b  HIFT_USE_DFLT ){
2caf4 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64  .    return yy_d
2caf5 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b  efault[stateno];
2caf6 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
2caf7 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43  LookAhead!=YYNOC
2caf8 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c  ODE );.  i += iL
2caf9 6f 6f 6b 41 68 65 61 64 3b 0a 20 20 69 66 28 20  ookAhead;.  if( 
2cafa 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f 53 5a 5f  i<0 || i>=YY_SZ_
2cafb 41 43 54 54 41 42 20 7c 7c 20 79 79 5f 6c 6f 6f  ACTTAB || yy_loo
2cafc 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b  kahead[i]!=iLook
2cafd 41 68 65 61 64 20 29 7b 0a 20 20 20 20 69 66 28  Ahead ){.    if(
2cafe 20 69 4c 6f 6f 6b 41 68 65 61 64 3e 30 20 29 7b   iLookAhead>0 ){
2caff 0a 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41  .#ifdef YYFALLBA
2cb00 43 4b 0a 20 20 20 20 20 20 59 59 43 4f 44 45 54  CK.      YYCODET
2cb01 59 50 45 20 69 46 61 6c 6c 62 61 63 6b 3b 20 20  YPE iFallback;  
2cb02 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c            /* Fal
2cb03 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20  lback token */. 
2cb04 20 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68       if( iLookAh
2cb05 65 61 64 3c 73 69 7a 65 6f 66 28 79 79 46 61 6c  ead<sizeof(yyFal
2cb06 6c 62 61 63 6b 29 2f 73 69 7a 65 6f 66 28 79 79  lback)/sizeof(yy
2cb07 46 61 6c 6c 62 61 63 6b 5b 30 5d 29 0a 20 20 20  Fallback[0]).   
2cb08 20 20 20 20 20 20 20 20 20 20 26 26 20 28 69 46            && (iF
2cb09 61 6c 6c 62 61 63 6b 20 3d 20 79 79 46 61 6c 6c  allback = yyFall
2cb0a 62 61 63 6b 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d  back[iLookAhead]
2cb0b 29 21 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20  )!=0 ){.#ifndef 
2cb0c 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 69  NDEBUG.        i
2cb0d 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
2cb0e 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69  {.          fpri
2cb0f 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
2cb10 20 22 25 73 46 41 4c 4c 42 41 43 4b 20 25 73 20   "%sFALLBACK %s 
2cb11 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  => %s\n",.      
2cb12 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72         yyTracePr
2cb13 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d  ompt, yyTokenNam
2cb14 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79  e[iLookAhead], y
2cb15 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c  yTokenName[iFall
2cb16 62 61 63 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20  back]);.        
2cb17 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
2cb18 20 72 65 74 75 72 6e 20 79 79 5f 66 69 6e 64 5f   return yy_find_
2cb19 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 70 50 61  shift_action(pPa
2cb1a 72 73 65 72 2c 20 69 46 61 6c 6c 62 61 63 6b 29  rser, iFallback)
2cb1b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
2cb1c 0a 23 69 66 64 65 66 20 59 59 57 49 4c 44 43 41  .#ifdef YYWILDCA
2cb1d 52 44 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  RD.      {.     
2cb1e 20 20 20 69 6e 74 20 6a 20 3d 20 69 20 2d 20 69     int j = i - i
2cb1f 4c 6f 6f 6b 41 68 65 61 64 20 2b 20 59 59 57 49  LookAhead + YYWI
2cb20 4c 44 43 41 52 44 3b 0a 20 20 20 20 20 20 20 20  LDCARD;.        
2cb21 69 66 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 59 59  if( j>=0 && j<YY
2cb22 5f 53 5a 5f 41 43 54 54 41 42 20 26 26 20 79 79  _SZ_ACTTAB && yy
2cb23 5f 6c 6f 6f 6b 61 68 65 61 64 5b 6a 5d 3d 3d 59  _lookahead[j]==Y
2cb24 59 57 49 4c 44 43 41 52 44 20 29 7b 0a 23 69 66  YWILDCARD ){.#if
2cb25 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
2cb26 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63        if( yyTrac
2cb27 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20  eFILE ){.       
2cb28 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
2cb29 72 61 63 65 46 49 4c 45 2c 20 22 25 73 57 49 4c  raceFILE, "%sWIL
2cb2a 44 43 41 52 44 20 25 73 20 3d 3e 20 25 73 5c 6e  DCARD %s => %s\n
2cb2b 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
2cb2c 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c    yyTracePrompt,
2cb2d 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f   yyTokenName[iLo
2cb2e 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65  okAhead], yyToke
2cb2f 6e 4e 61 6d 65 5b 59 59 57 49 4c 44 43 41 52 44  nName[YYWILDCARD
2cb30 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ]);.          }.
2cb31 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
2cb32 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65   */.          re
2cb33 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 6a  turn yy_action[j
2cb34 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ];.        }.   
2cb35 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 59     }.#endif /* Y
2cb36 59 57 49 4c 44 43 41 52 44 20 2a 2f 0a 20 20 20  YWILDCARD */.   
2cb37 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79   }.    return yy
2cb38 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f  _default[stateno
2cb39 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
2cb3a 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e  return yy_action
2cb3b 5b 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  [i];.  }.}../*.*
2cb3c 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70 72 6f  * Find the appro
2cb3d 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 66 6f  priate action fo
2cb3e 72 20 61 20 70 61 72 73 65 72 20 67 69 76 65 6e  r a parser given
2cb3f 20 74 68 65 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61   the non-termina
2cb40 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20  l.** look-ahead 
2cb41 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64  token iLookAhead
2cb42 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
2cb43 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20  ook-ahead token 
2cb44 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65  is YYNOCODE, the
2cb45 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  n check to see i
2cb46 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 0a  f the action is.
2cb47 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f  ** independent o
2cb48 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  f the look-ahead
2cb49 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72 65 74  .  If it is, ret
2cb4a 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20  urn the action, 
2cb4b 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74  otherwise.** ret
2cb4c 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e  urn YY_NO_ACTION
2cb4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2cb4e 79 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61  yy_find_reduce_a
2cb4f 63 74 69 6f 6e 28 0a 20 20 69 6e 74 20 73 74 61  ction(.  int sta
2cb50 74 65 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  teno,           
2cb51 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 74     /* Current st
2cb52 61 74 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ate number */.  
2cb53 59 59 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b  YYCODETYPE iLook
2cb54 41 68 65 61 64 20 20 20 20 20 2f 2a 20 54 68 65  Ahead     /* The
2cb55 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
2cb56 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  n */.){.  int i;
2cb57 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52 53  .#ifdef YYERRORS
2cb58 59 4d 42 4f 4c 0a 20 20 69 66 28 20 73 74 61 74  YMBOL.  if( stat
2cb59 65 6e 6f 3e 59 59 5f 52 45 44 55 43 45 5f 4d 41  eno>YY_REDUCE_MA
2cb5a 58 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  X ){.    return 
2cb5b 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65  yy_default[state
2cb5c 6e 6f 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  no];.  }.#else. 
2cb5d 20 61 73 73 65 72 74 28 20 73 74 61 74 65 6e 6f   assert( stateno
2cb5e 3c 3d 59 59 5f 52 45 44 55 43 45 5f 4d 41 58 20  <=YY_REDUCE_MAX 
2cb5f 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 20 3d 20  );.#endif.  i = 
2cb60 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 73  yy_reduce_ofst[s
2cb61 74 61 74 65 6e 6f 5d 3b 0a 20 20 61 73 73 65 72  tateno];.  asser
2cb62 74 28 20 69 21 3d 59 59 5f 52 45 44 55 43 45 5f  t( i!=YY_REDUCE_
2cb63 55 53 45 5f 44 46 4c 54 20 29 3b 0a 20 20 61 73  USE_DFLT );.  as
2cb64 73 65 72 74 28 20 69 4c 6f 6f 6b 41 68 65 61 64  sert( iLookAhead
2cb65 21 3d 59 59 4e 4f 43 4f 44 45 20 29 3b 0a 20 20  !=YYNOCODE );.  
2cb66 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b  i += iLookAhead;
2cb67 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52 53  .#ifdef YYERRORS
2cb68 59 4d 42 4f 4c 0a 20 20 69 66 28 20 69 3c 30 20  YMBOL.  if( i<0 
2cb69 7c 7c 20 69 3e 3d 59 59 5f 53 5a 5f 41 43 54 54  || i>=YY_SZ_ACTT
2cb6a 41 42 20 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65  AB || yy_lookahe
2cb6b 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61  ad[i]!=iLookAhea
2cb6c 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
2cb6d 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65  yy_default[state
2cb6e 6e 6f 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  no];.  }.#else. 
2cb6f 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
2cb70 20 69 3c 59 59 5f 53 5a 5f 41 43 54 54 41 42 20   i<YY_SZ_ACTTAB 
2cb71 29 3b 0a 20 20 61 73 73 65 72 74 28 20 79 79 5f  );.  assert( yy_
2cb72 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 3d 3d 69 4c  lookahead[i]==iL
2cb73 6f 6f 6b 41 68 65 61 64 20 29 3b 0a 23 65 6e 64  ookAhead );.#end
2cb74 69 66 0a 20 20 72 65 74 75 72 6e 20 79 79 5f 61  if.  return yy_a
2cb75 63 74 69 6f 6e 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a  ction[i];.}../*.
2cb76 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
2cb77 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2cb78 65 64 20 69 66 20 74 68 65 20 73 74 61 63 6b 20  ed if the stack 
2cb79 6f 76 65 72 66 6c 6f 77 73 2e 0a 2a 2f 0a 73 74  overflows..*/.st
2cb7a 61 74 69 63 20 76 6f 69 64 20 79 79 53 74 61 63  atic void yyStac
2cb7b 6b 4f 76 65 72 66 6c 6f 77 28 79 79 50 61 72 73  kOverflow(yyPars
2cb7c 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 59  er *yypParser, Y
2cb7d 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d  YMINORTYPE *yypM
2cb7e 69 6e 6f 72 29 7b 0a 20 20 20 73 71 6c 69 74 65  inor){.   sqlite
2cb7f 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
2cb80 3b 0a 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  ;.   yypParser->
2cb81 79 79 69 64 78 2d 2d 3b 0a 23 69 66 6e 64 65 66  yyidx--;.#ifndef
2cb82 20 4e 44 45 42 55 47 0a 20 20 20 69 66 28 20 79   NDEBUG.   if( y
2cb83 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
2cb84 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
2cb85 63 65 46 49 4c 45 2c 22 25 73 53 74 61 63 6b 20  ceFILE,"%sStack 
2cb86 4f 76 65 72 66 6c 6f 77 21 5c 6e 22 2c 79 79 54  Overflow!\n",yyT
2cb87 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20  racePrompt);.   
2cb88 7d 0a 23 65 6e 64 69 66 0a 20 20 20 77 68 69 6c  }.#endif.   whil
2cb89 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  e( yypParser->yy
2cb8a 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f  idx>=0 ) yy_pop_
2cb8b 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70  parser_stack(yyp
2cb8c 50 61 72 73 65 72 29 3b 0a 20 20 20 2f 2a 20 48  Parser);.   /* H
2cb8d 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65  ere code is inse
2cb8e 72 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20  rted which will 
2cb8f 65 78 65 63 75 74 65 20 69 66 20 74 68 65 20 70  execute if the p
2cb90 61 72 73 65 72 0a 20 20 20 2a 2a 20 73 74 61 63  arser.   ** stac
2cb91 6b 20 65 76 65 72 79 20 6f 76 65 72 66 6c 6f 77  k every overflow
2cb92 73 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50  s */..  UNUSED_P
2cb93 41 52 41 4d 45 54 45 52 28 79 79 70 4d 69 6e 6f  ARAMETER(yypMino
2cb94 72 29 3b 20 2f 2a 20 53 69 6c 65 6e 63 65 20 73  r); /* Silence s
2cb95 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72  ome compiler war
2cb96 6e 69 6e 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74  nings */.  sqlit
2cb97 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2cb98 65 2c 20 22 70 61 72 73 65 72 20 73 74 61 63 6b  e, "parser stack
2cb99 20 6f 76 65 72 66 6c 6f 77 22 29 3b 0a 20 20 70   overflow");.  p
2cb9a 50 61 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f  Parse->parseErro
2cb9b 72 20 3d 20 31 3b 0a 20 20 20 73 71 6c 69 74 65  r = 1;.   sqlite
2cb9c 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45  3ParserARG_STORE
2cb9d 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20 77 61  ; /* Suppress wa
2cb9e 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e 75 73  rning about unus
2cb9f 65 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  ed %extra_argume
2cba0 6e 74 20 76 61 72 20 2a 2f 0a 7d 0a 0a 2f 2a 0a  nt var */.}../*.
2cba1 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 73 68 69  ** Perform a shi
2cba2 66 74 20 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  ft action..*/.st
2cba3 61 74 69 63 20 76 6f 69 64 20 79 79 5f 73 68 69  atic void yy_shi
2cba4 66 74 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a  ft(.  yyParser *
2cba5 79 79 70 50 61 72 73 65 72 2c 20 20 20 20 20 20  yypParser,      
2cba6 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
2cba7 72 20 74 6f 20 62 65 20 73 68 69 66 74 65 64 20  r to be shifted 
2cba8 2a 2f 0a 20 20 69 6e 74 20 79 79 4e 65 77 53 74  */.  int yyNewSt
2cba9 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ate,            
2cbaa 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 73 74     /* The new st
2cbab 61 74 65 20 74 6f 20 73 68 69 66 74 20 69 6e 20  ate to shift in 
2cbac 2a 2f 0a 20 20 69 6e 74 20 79 79 4d 61 6a 6f 72  */.  int yyMajor
2cbad 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2cbae 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20     /* The major 
2cbaf 74 6f 6b 65 6e 20 74 6f 20 73 68 69 66 74 20 69  token to shift i
2cbb0 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59  n */.  YYMINORTY
2cbb1 50 45 20 2a 79 79 70 4d 69 6e 6f 72 20 20 20 20  PE *yypMinor    
2cbb2 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
2cbb3 74 6f 20 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b  to the minor tok
2cbb4 65 6e 20 74 6f 20 73 68 69 66 74 20 69 6e 20 2a  en to shift in *
2cbb5 2f 0a 29 7b 0a 20 20 79 79 53 74 61 63 6b 45 6e  /.){.  yyStackEn
2cbb6 74 72 79 20 2a 79 79 74 6f 73 3b 0a 20 20 79 79  try *yytos;.  yy
2cbb7 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2b 2b  pParser->yyidx++
2cbb8 3b 0a 23 69 66 64 65 66 20 59 59 54 52 41 43 4b  ;.#ifdef YYTRACK
2cbb9 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20  MAXSTACKDEPTH.  
2cbba 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
2cbbb 79 69 64 78 3e 79 79 70 50 61 72 73 65 72 2d 3e  yidx>yypParser->
2cbbc 79 79 69 64 78 4d 61 78 20 29 7b 0a 20 20 20 20  yyidxMax ){.    
2cbbd 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
2cbbe 4d 61 78 20 3d 20 79 79 70 50 61 72 73 65 72 2d  Max = yypParser-
2cbbf 3e 79 79 69 64 78 3b 0a 20 20 7d 0a 23 65 6e 64  >yyidx;.  }.#end
2cbc0 69 66 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45  if.#if YYSTACKDE
2cbc1 50 54 48 3e 30 20 0a 20 20 69 66 28 20 79 79 70  PTH>0 .  if( yyp
2cbc2 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 59  Parser->yyidx>=Y
2cbc3 59 53 54 41 43 4b 44 45 50 54 48 20 29 7b 0a 20  YSTACKDEPTH ){. 
2cbc4 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c     yyStackOverfl
2cbc5 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20 79 79  ow(yypParser, yy
2cbc6 70 4d 69 6e 6f 72 29 3b 0a 20 20 20 20 72 65 74  pMinor);.    ret
2cbc7 75 72 6e 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  urn;.  }.#else. 
2cbc8 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
2cbc9 79 79 69 64 78 3e 3d 79 79 70 50 61 72 73 65 72  yyidx>=yypParser
2cbca 2d 3e 79 79 73 74 6b 73 7a 20 29 7b 0a 20 20 20  ->yystksz ){.   
2cbcb 20 79 79 47 72 6f 77 53 74 61 63 6b 28 79 79 70   yyGrowStack(yyp
2cbcc 50 61 72 73 65 72 29 3b 0a 20 20 20 20 69 66 28  Parser);.    if(
2cbcd 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
2cbce 78 3e 3d 79 79 70 50 61 72 73 65 72 2d 3e 79 79  x>=yypParser->yy
2cbcf 73 74 6b 73 7a 20 29 7b 0a 20 20 20 20 20 20 79  stksz ){.      y
2cbd0 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79  yStackOverflow(y
2cbd1 79 70 50 61 72 73 65 72 2c 20 79 79 70 4d 69 6e  ypParser, yypMin
2cbd2 6f 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  or);.      retur
2cbd3 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  n;.    }.  }.#en
2cbd4 64 69 66 0a 20 20 79 79 74 6f 73 20 3d 20 26 79  dif.  yytos = &y
2cbd5 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63  ypParser->yystac
2cbd6 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  k[yypParser->yyi
2cbd7 64 78 5d 3b 0a 20 20 79 79 74 6f 73 2d 3e 73 74  dx];.  yytos->st
2cbd8 61 74 65 6e 6f 20 3d 20 28 59 59 41 43 54 49 4f  ateno = (YYACTIO
2cbd9 4e 54 59 50 45 29 79 79 4e 65 77 53 74 61 74 65  NTYPE)yyNewState
2cbda 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72  ;.  yytos->major
2cbdb 20 3d 20 28 59 59 43 4f 44 45 54 59 50 45 29 79   = (YYCODETYPE)y
2cbdc 79 4d 61 6a 6f 72 3b 0a 20 20 79 79 74 6f 73 2d  yMajor;.  yytos-
2cbdd 3e 6d 69 6e 6f 72 20 3d 20 2a 79 79 70 4d 69 6e  >minor = *yypMin
2cbde 6f 72 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  or;.#ifndef NDEB
2cbdf 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  UG.  if( yyTrace
2cbe0 46 49 4c 45 20 26 26 20 79 79 70 50 61 72 73 65  FILE && yypParse
2cbe1 72 2d 3e 79 79 69 64 78 3e 30 20 29 7b 0a 20 20  r->yyidx>0 ){.  
2cbe2 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 70 72    int i;.    fpr
2cbe3 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
2cbe4 2c 22 25 73 53 68 69 66 74 20 25 64 5c 6e 22 2c  ,"%sShift %d\n",
2cbe5 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79  yyTracePrompt,yy
2cbe6 4e 65 77 53 74 61 74 65 29 3b 0a 20 20 20 20 66  NewState);.    f
2cbe7 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
2cbe8 4c 45 2c 22 25 73 53 74 61 63 6b 3a 22 2c 79 79  LE,"%sStack:",yy
2cbe9 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20  TracePrompt);.  
2cbea 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 79 79    for(i=1; i<=yy
2cbeb 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 20  pParser->yyidx; 
2cbec 69 2b 2b 29 0a 20 20 20 20 20 20 66 70 72 69 6e  i++).      fprin
2cbed 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
2cbee 20 25 73 22 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65   %s",yyTokenName
2cbef 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74  [yypParser->yyst
2cbf0 61 63 6b 5b 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a  ack[i].major]);.
2cbf1 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
2cbf2 61 63 65 46 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20  aceFILE,"\n");. 
2cbf3 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20   }.#endif.}../* 
2cbf4 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61  The following ta
2cbf5 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66  ble contains inf
2cbf6 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65  ormation about e
2cbf7 76 65 72 79 20 72 75 6c 65 20 74 68 61 74 0a 2a  very rule that.*
2cbf8 2a 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67  * is used during
2cbf9 20 74 68 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a   the reduce..*/.
2cbfa 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72  static const str
2cbfb 75 63 74 20 7b 0a 20 20 59 59 43 4f 44 45 54 59  uct {.  YYCODETY
2cbfc 50 45 20 6c 68 73 3b 20 20 20 20 20 20 20 20 20  PE lhs;         
2cbfd 2f 2a 20 53 79 6d 62 6f 6c 20 6f 6e 20 74 68 65  /* Symbol on the
2cbfe 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
2cbff 6f 66 20 74 68 65 20 72 75 6c 65 20 2a 2f 0a 20  of the rule */. 
2cc00 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6e   unsigned char n
2cc01 72 68 73 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  rhs;     /* Numb
2cc02 65 72 20 6f 66 20 72 69 67 68 74 2d 68 61 6e 64  er of right-hand
2cc03 20 73 69 64 65 20 73 79 6d 62 6f 6c 73 20 69 6e   side symbols in
2cc04 20 74 68 65 20 72 75 6c 65 20 2a 2f 0a 7d 20 79   the rule */.} y
2cc05 79 52 75 6c 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a  yRuleInfo[] = {.
2cc06 20 20 7b 20 31 34 32 2c 20 31 20 7d 2c 0a 20 20    { 142, 1 },.  
2cc07 7b 20 31 34 33 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 143, 2 },.  { 
2cc08 31 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  143, 1 },.  { 14
2cc09 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 34 2c  4, 1 },.  { 144,
2cc0a 20 33 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 30   3 },.  { 145, 0
2cc0b 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d   },.  { 145, 1 }
2cc0c 2c 0a 20 20 7b 20 31 34 35 2c 20 33 20 7d 2c 0a  ,.  { 145, 3 },.
2cc0d 20 20 7b 20 31 34 36 2c 20 31 20 7d 2c 0a 20 20    { 146, 1 },.  
2cc0e 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 147, 3 },.  { 
2cc0f 31 34 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34  149, 0 },.  { 14
2cc10 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 39 2c  9, 1 },.  { 149,
2cc11 20 32 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 30   2 },.  { 148, 0
2cc12 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d   },.  { 148, 1 }
2cc13 2c 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a  ,.  { 148, 1 },.
2cc14 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20    { 148, 1 },.  
2cc15 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 147, 2 },.  { 
2cc16 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34  147, 2 },.  { 14
2cc17 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 31 2c  7, 2 },.  { 151,
2cc18 20 31 20 7d 2c 0a 20 20 7b 20 31 35 31 2c 20 30   1 },.  { 151, 0
2cc19 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d   },.  { 147, 2 }
2cc1a 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a  ,.  { 147, 3 },.
2cc1b 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20    { 147, 5 },.  
2cc1c 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 147, 2 },.  { 
2cc1d 31 35 32 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 35  152, 6 },.  { 15
2cc1e 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 35 2c  5, 0 },.  { 155,
2cc1f 20 33 20 7d 2c 0a 20 20 7b 20 31 35 34 2c 20 31   3 },.  { 154, 1
2cc20 20 7d 2c 0a 20 20 7b 20 31 35 34 2c 20 30 20 7d   },.  { 154, 0 }
2cc21 2c 0a 20 20 7b 20 31 35 33 2c 20 34 20 7d 2c 0a  ,.  { 153, 4 },.
2cc22 20 20 7b 20 31 35 33 2c 20 32 20 7d 2c 0a 20 20    { 153, 2 },.  
2cc23 7b 20 31 35 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 157, 3 },.  { 
2cc24 31 35 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  157, 1 },.  { 16
2cc25 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 36 31 2c  0, 3 },.  { 161,
2cc26 20 31 20 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 31   1 },.  { 164, 1
2cc27 20 7d 2c 0a 20 20 7b 20 31 36 35 2c 20 31 20 7d   },.  { 165, 1 }
2cc28 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a  ,.  { 150, 1 },.
2cc29 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a 20 20    { 150, 1 },.  
2cc2a 7b 20 31 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 150, 1 },.  { 
2cc2b 31 36 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 36  162, 0 },.  { 16
2cc2c 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 36 2c  2, 1 },.  { 166,
2cc2d 20 31 20 7d 2c 0a 20 20 7b 20 31 36 36 2c 20 34   1 },.  { 166, 4
2cc2e 20 7d 2c 0a 20 20 7b 20 31 36 36 2c 20 36 20 7d   },.  { 166, 6 }
2cc2f 2c 0a 20 20 7b 20 31 36 37 2c 20 31 20 7d 2c 0a  ,.  { 167, 1 },.
2cc30 20 20 7b 20 31 36 37 2c 20 32 20 7d 2c 0a 20 20    { 167, 2 },.  
2cc31 7b 20 31 36 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 168, 1 },.  { 
2cc32 31 36 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  168, 1 },.  { 16
2cc33 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 33 2c  3, 2 },.  { 163,
2cc34 20 30 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33   0 },.  { 171, 3
2cc35 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 31 20 7d   },.  { 171, 1 }
2cc36 2c 0a 20 20 7b 20 31 37 32 2c 20 32 20 7d 2c 0a  ,.  { 172, 2 },.
2cc37 20 20 7b 20 31 37 32 2c 20 34 20 7d 2c 0a 20 20    { 172, 4 },.  
2cc38 7b 20 31 37 32 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 172, 3 },.  { 
2cc39 31 37 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  172, 3 },.  { 17
2cc3a 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 32 2c  2, 2 },.  { 172,
2cc3b 20 32 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 33   2 },.  { 172, 3
2cc3c 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 35 20 7d   },.  { 172, 5 }
2cc3d 2c 0a 20 20 7b 20 31 37 32 2c 20 32 20 7d 2c 0a  ,.  { 172, 2 },.
2cc3e 20 20 7b 20 31 37 32 2c 20 34 20 7d 2c 0a 20 20    { 172, 4 },.  
2cc3f 7b 20 31 37 32 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 172, 4 },.  { 
2cc40 31 37 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  172, 1 },.  { 17
2cc41 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 37 2c  2, 2 },.  { 177,
2cc42 20 30 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 31   0 },.  { 177, 1
2cc43 20 7d 2c 0a 20 20 7b 20 31 37 39 2c 20 30 20 7d   },.  { 179, 0 }
2cc44 2c 0a 20 20 7b 20 31 37 39 2c 20 32 20 7d 2c 0a  ,.  { 179, 2 },.
2cc45 20 20 7b 20 31 38 31 2c 20 32 20 7d 2c 0a 20 20    { 181, 2 },.  
2cc46 7b 20 31 38 31 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 181, 3 },.  { 
2cc47 31 38 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38  181, 3 },.  { 18
2cc48 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 32 2c  1, 3 },.  { 182,
2cc49 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 32   2 },.  { 182, 2
2cc4a 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 31 20 7d   },.  { 182, 1 }
2cc4b 2c 0a 20 20 7b 20 31 38 32 2c 20 31 20 7d 2c 0a  ,.  { 182, 1 },.
2cc4c 20 20 7b 20 31 38 30 2c 20 33 20 7d 2c 0a 20 20    { 180, 3 },.  
2cc4d 7b 20 31 38 30 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 180, 2 },.  { 
2cc4e 31 38 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38  183, 0 },.  { 18
2cc4f 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 33 2c  3, 2 },.  { 183,
2cc50 20 32 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 30   2 },.  { 158, 0
2cc51 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 32 20 7d   },.  { 158, 2 }
2cc52 2c 0a 20 20 7b 20 31 38 34 2c 20 33 20 7d 2c 0a  ,.  { 184, 3 },.
2cc53 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20    { 184, 2 },.  
2cc54 7b 20 31 38 34 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 184, 1 },.  { 
2cc55 31 38 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38  185, 2 },.  { 18
2cc56 35 2c 20 37 20 7d 2c 0a 20 20 7b 20 31 38 35 2c  5, 7 },.  { 185,
2cc57 20 35 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 35   5 },.  { 185, 5
2cc58 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 31 30 20   },.  { 185, 10 
2cc59 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 30 20 7d 2c  },.  { 187, 0 },
2cc5a 0a 20 20 7b 20 31 38 37 2c 20 31 20 7d 2c 0a 20  .  { 187, 1 },. 
2cc5b 20 7b 20 31 37 35 2c 20 30 20 7d 2c 0a 20 20 7b   { 175, 0 },.  {
2cc5c 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
2cc5d 38 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 38  88, 0 },.  { 188
2cc5e 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 39 2c 20  , 2 },.  { 189, 
2cc5f 31 20 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 31 20  1 },.  { 189, 1 
2cc60 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 31 20 7d 2c  },.  { 189, 1 },
2cc61 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20  .  { 147, 4 },. 
2cc62 20 7b 20 31 39 31 2c 20 32 20 7d 2c 0a 20 20 7b   { 191, 2 },.  {
2cc63 20 31 39 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   191, 0 },.  { 1
2cc64 34 37 2c 20 38 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 8 },.  { 147
2cc65 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 4 },.  { 147, 
2cc66 31 20 7d 2c 0a 20 20 7b 20 31 35 39 2c 20 31 20  1 },.  { 159, 1 
2cc67 7d 2c 0a 20 20 7b 20 31 35 39 2c 20 33 20 7d 2c  },.  { 159, 3 },
2cc68 0a 20 20 7b 20 31 39 34 2c 20 31 20 7d 2c 0a 20  .  { 194, 1 },. 
2cc69 20 7b 20 31 39 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 194, 2 },.  {
2cc6a 20 31 39 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   194, 1 },.  { 1
2cc6b 39 33 2c 20 39 20 7d 2c 0a 20 20 7b 20 31 39 35  93, 9 },.  { 195
2cc6c 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20  , 1 },.  { 195, 
2cc6d 31 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 30 20  1 },.  { 195, 0 
2cc6e 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 32 20 7d 2c  },.  { 203, 2 },
2cc6f 0a 20 20 7b 20 32 30 33 2c 20 30 20 7d 2c 0a 20  .  { 203, 0 },. 
2cc70 20 7b 20 31 39 36 2c 20 33 20 7d 2c 0a 20 20 7b   { 196, 3 },.  {
2cc71 20 31 39 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   196, 2 },.  { 1
2cc72 39 36 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 30 34  96, 4 },.  { 204
2cc73 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20  , 2 },.  { 204, 
2cc74 31 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 30 20  1 },.  { 204, 0 
2cc75 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 30 20 7d 2c  },.  { 197, 0 },
2cc76 0a 20 20 7b 20 31 39 37 2c 20 32 20 7d 2c 0a 20  .  { 197, 2 },. 
2cc77 20 7b 20 32 30 36 2c 20 32 20 7d 2c 0a 20 20 7b   { 206, 2 },.  {
2cc78 20 32 30 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   206, 0 },.  { 2
2cc79 30 35 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 30 35  05, 7 },.  { 205
2cc7a 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20  , 7 },.  { 205, 
2cc7b 37 20 7d 2c 0a 20 20 7b 20 31 35 36 2c 20 30 20  7 },.  { 156, 0 
2cc7c 7d 2c 0a 20 20 7b 20 31 35 36 2c 20 32 20 7d 2c  },.  { 156, 2 },
2cc7d 0a 20 20 7b 20 31 39 32 2c 20 32 20 7d 2c 0a 20  .  { 192, 2 },. 
2cc7e 20 7b 20 32 30 37 2c 20 31 20 7d 2c 0a 20 20 7b   { 207, 1 },.  {
2cc7f 20 32 30 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   207, 2 },.  { 2
2cc80 30 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 37  07, 3 },.  { 207
2cc81 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20  , 4 },.  { 209, 
2cc82 32 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 30 20  2 },.  { 209, 0 
2cc83 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 30 20 7d 2c  },.  { 208, 0 },
2cc84 0a 20 20 7b 20 32 30 38 2c 20 33 20 7d 2c 0a 20  .  { 208, 3 },. 
2cc85 20 7b 20 32 30 38 2c 20 32 20 7d 2c 0a 20 20 7b   { 208, 2 },.  {
2cc86 20 32 31 30 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   210, 4 },.  { 2
2cc87 31 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 31  10, 0 },.  { 201
2cc88 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 31 2c 20  , 0 },.  { 201, 
2cc89 33 20 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 34 20  3 },.  { 213, 4 
2cc8a 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 32 20 7d 2c  },.  { 213, 2 },
2cc8b 0a 20 20 7b 20 32 31 34 2c 20 31 20 7d 2c 0a 20  .  { 214, 1 },. 
2cc8c 20 7b 20 31 37 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 176, 1 },.  {
2cc8d 20 31 37 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   176, 1 },.  { 1
2cc8e 37 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 39  76, 0 },.  { 199
2cc8f 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 39 2c 20  , 0 },.  { 199, 
2cc90 33 20 7d 2c 0a 20 20 7b 20 32 30 30 2c 20 30 20  3 },.  { 200, 0 
2cc91 7d 2c 0a 20 20 7b 20 32 30 30 2c 20 32 20 7d 2c  },.  { 200, 2 },
2cc92 0a 20 20 7b 20 32 30 32 2c 20 30 20 7d 2c 0a 20  .  { 202, 0 },. 
2cc93 20 7b 20 32 30 32 2c 20 32 20 7d 2c 0a 20 20 7b   { 202, 2 },.  {
2cc94 20 32 30 32 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   202, 4 },.  { 2
2cc95 30 32 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37  02, 4 },.  { 147
2cc96 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20  , 5 },.  { 198, 
2cc97 30 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20 32 20  0 },.  { 198, 2 
2cc98 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 37 20 7d 2c  },.  { 147, 7 },
2cc99 0a 20 20 7b 20 32 31 36 2c 20 35 20 7d 2c 0a 20  .  { 216, 5 },. 
2cc9a 20 7b 20 32 31 36 2c 20 33 20 7d 2c 0a 20 20 7b   { 216, 3 },.  {
2cc9b 20 31 34 37 2c 20 38 20 7d 2c 0a 20 20 7b 20 31   147, 8 },.  { 1
2cc9c 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 5 },.  { 147
2cc9d 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20  , 6 },.  { 217, 
2cc9e 32 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 31 20  2 },.  { 217, 1 
2cc9f 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 33 20 7d 2c  },.  { 219, 3 },
2cca0 0a 20 20 7b 20 32 31 39 2c 20 31 20 7d 2c 0a 20  .  { 219, 1 },. 
2cca1 20 7b 20 32 31 38 2c 20 30 20 7d 2c 0a 20 20 7b   { 218, 0 },.  {
2cca2 20 32 31 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   218, 3 },.  { 2
2cca3 31 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 32  12, 3 },.  { 212
2cca4 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 1 },.  { 174, 
2cca5 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20  1 },.  { 174, 3 
2cca6 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 31 20 7d 2c  },.  { 173, 1 },
2cca7 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20  .  { 174, 1 },. 
2cca8 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 174, 1 },.  {
2cca9 20 31 37 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   174, 3 },.  { 1
2ccaa 37 34 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 33  74, 5 },.  { 173
2ccab 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20  , 1 },.  { 173, 
2ccac 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20  1 },.  { 174, 1 
2ccad 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c  },.  { 174, 1 },
2ccae 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d 2c 0a 20  .  { 174, 3 },. 
2ccaf 20 7b 20 31 37 34 2c 20 36 20 7d 2c 0a 20 20 7b   { 174, 6 },.  {
2ccb0 20 31 37 34 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   174, 5 },.  { 1
2ccb1 37 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33  74, 4 },.  { 173
2ccb2 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 1 },.  { 174, 
2ccb3 33 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20  3 },.  { 174, 3 
2ccb4 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d 2c  },.  { 174, 3 },
2ccb5 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d 2c 0a 20  .  { 174, 3 },. 
2ccb6 20 7b 20 31 37 34 2c 20 33 20 7d 2c 0a 20 20 7b   { 174, 3 },.  {
2ccb7 20 31 37 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   174, 3 },.  { 1
2ccb8 37 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 34  74, 3 },.  { 174
2ccb9 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20  , 3 },.  { 221, 
2ccba 31 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 32 20  1 },.  { 221, 2 
2ccbb 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 31 20 7d 2c  },.  { 221, 1 },
2ccbc 0a 20 20 7b 20 32 32 31 2c 20 32 20 7d 2c 0a 20  .  { 221, 2 },. 
2ccbd 20 7b 20 32 32 32 2c 20 32 20 7d 2c 0a 20 20 7b   { 222, 2 },.  {
2ccbe 20 32 32 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   222, 0 },.  { 1
2ccbf 37 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 34  74, 4 },.  { 174
2ccc0 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 2 },.  { 174, 
2ccc1 33 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20  3 },.  { 174, 3 
2ccc2 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 34 20 7d 2c  },.  { 174, 4 },
2ccc3 0a 20 20 7b 20 31 37 34 2c 20 32 20 7d 2c 0a 20  .  { 174, 2 },. 
2ccc4 20 7b 20 31 37 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 174, 2 },.  {
2ccc5 20 31 37 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   174, 2 },.  { 1
2ccc6 37 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 33  74, 2 },.  { 223
2ccc7 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20  , 1 },.  { 223, 
2ccc8 32 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 35 20  2 },.  { 174, 5 
2ccc9 7d 2c 0a 20 20 7b 20 32 32 34 2c 20 31 20 7d 2c  },.  { 224, 1 },
2ccca 0a 20 20 7b 20 32 32 34 2c 20 32 20 7d 2c 0a 20  .  { 224, 2 },. 
2cccb 20 7b 20 31 37 34 2c 20 35 20 7d 2c 0a 20 20 7b   { 174, 5 },.  {
2cccc 20 31 37 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   174, 3 },.  { 1
2cccd 37 34 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 34  74, 5 },.  { 174
2ccce 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 4 },.  { 174, 
2cccf 34 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 35 20  4 },.  { 174, 5 
2ccd0 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 35 20 7d 2c  },.  { 226, 5 },
2ccd1 0a 20 20 7b 20 32 32 36 2c 20 34 20 7d 2c 0a 20  .  { 226, 4 },. 
2ccd2 20 7b 20 32 32 37 2c 20 32 20 7d 2c 0a 20 20 7b   { 227, 2 },.  {
2ccd3 20 32 32 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   227, 0 },.  { 2
2ccd4 32 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 35  25, 1 },.  { 225
2ccd5 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20  , 0 },.  { 220, 
2ccd6 31 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20 30 20  1 },.  { 220, 0 
2ccd7 7d 2c 0a 20 20 7b 20 32 31 35 2c 20 33 20 7d 2c  },.  { 215, 3 },
2ccd8 0a 20 20 7b 20 32 31 35 2c 20 31 20 7d 2c 0a 20  .  { 215, 1 },. 
2ccd9 20 7b 20 31 34 37 2c 20 31 31 20 7d 2c 0a 20 20   { 147, 11 },.  
2ccda 7b 20 32 32 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 228, 1 },.  { 
2ccdb 32 32 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37  228, 0 },.  { 17
2ccdc 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 38 2c  8, 0 },.  { 178,
2ccdd 20 33 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 35   3 },.  { 186, 5
2ccde 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 33 20 7d   },.  { 186, 3 }
2ccdf 2c 0a 20 20 7b 20 32 32 39 2c 20 30 20 7d 2c 0a  ,.  { 229, 0 },.
2cce0 20 20 7b 20 32 32 39 2c 20 32 20 7d 2c 0a 20 20    { 229, 2 },.  
2cce1 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 147, 4 },.  { 
2cce2 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  147, 1 },.  { 14
2cce3 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 2 },.  { 147,
2cce4 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35   5 },.  { 147, 5
2cce5 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d   },.  { 147, 5 }
2cce6 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a  ,.  { 147, 5 },.
2cce7 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20    { 147, 6 },.  
2cce8 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 147, 3 },.  { 
2cce9 32 33 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33  230, 1 },.  { 23
2ccea 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 39 2c  0, 1 },.  { 169,
2cceb 20 32 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 32   2 },.  { 170, 2
2ccec 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 31 20 7d   },.  { 232, 1 }
2cced 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a  ,.  { 231, 1 },.
2ccee 20 20 7b 20 32 33 31 2c 20 30 20 7d 2c 0a 20 20    { 231, 0 },.  
2ccef 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 147, 5 },.  { 
2ccf0 32 33 33 2c 20 31 31 20 7d 2c 0a 20 20 7b 20 32  233, 11 },.  { 2
2ccf1 33 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 35  35, 1 },.  { 235
2ccf2 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20  , 1 },.  { 235, 
2ccf3 32 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 30 20  2 },.  { 235, 0 
2ccf4 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 31 20 7d 2c  },.  { 236, 1 },
2ccf5 0a 20 20 7b 20 32 33 36 2c 20 31 20 7d 2c 0a 20  .  { 236, 1 },. 
2ccf6 20 7b 20 32 33 36 2c 20 33 20 7d 2c 0a 20 20 7b   { 236, 3 },.  {
2ccf7 20 32 33 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   237, 0 },.  { 2
2ccf8 33 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 38  37, 3 },.  { 238
2ccf9 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 38 2c 20  , 0 },.  { 238, 
2ccfa 32 20 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 33 20  2 },.  { 234, 3 
2ccfb 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 32 20 7d 2c  },.  { 234, 2 },
2ccfc 0a 20 20 7b 20 32 33 39 2c 20 36 20 7d 2c 0a 20  .  { 239, 6 },. 
2ccfd 20 7b 20 32 33 39 2c 20 38 20 7d 2c 0a 20 20 7b   { 239, 8 },.  {
2ccfe 20 32 33 39 2c 20 35 20 7d 2c 0a 20 20 7b 20 32   239, 5 },.  { 2
2ccff 33 39 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 33 39  39, 4 },.  { 239
2cd00 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 1 },.  { 174, 
2cd01 34 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 36 20  4 },.  { 174, 6 
2cd02 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c  },.  { 190, 1 },
2cd03 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20  .  { 190, 1 },. 
2cd04 20 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 190, 1 },.  {
2cd05 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   147, 4 },.  { 1
2cd06 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 6 },.  { 147
2cd07 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20  , 3 },.  { 241, 
2cd08 30 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 32 20  0 },.  { 241, 2 
2cd09 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 31 20 7d 2c  },.  { 240, 1 },
2cd0a 0a 20 20 7b 20 32 34 30 2c 20 30 20 7d 2c 0a 20  .  { 240, 0 },. 
2cd0b 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b   { 147, 1 },.  {
2cd0c 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   147, 3 },.  { 1
2cd0d 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 1 },.  { 147
2cd0e 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 3 },.  { 147, 
2cd0f 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20  6 },.  { 147, 6 
2cd10 7d 2c 0a 20 20 7b 20 32 34 32 2c 20 31 20 7d 2c  },.  { 242, 1 },
2cd11 0a 20 20 7b 20 32 34 33 2c 20 30 20 7d 2c 0a 20  .  { 243, 0 },. 
2cd12 20 7b 20 32 34 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 243, 1 },.  {
2cd13 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   147, 1 },.  { 1
2cd14 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 34 34  47, 4 },.  { 244
2cd15 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 34 35 2c 20  , 7 },.  { 245, 
2cd16 31 20 7d 2c 0a 20 20 7b 20 32 34 35 2c 20 33 20  1 },.  { 245, 3 
2cd17 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 30 20 7d 2c  },.  { 246, 0 },
2cd18 0a 20 20 7b 20 32 34 36 2c 20 32 20 7d 2c 0a 20  .  { 246, 2 },. 
2cd19 20 7b 20 32 34 37 2c 20 31 20 7d 2c 0a 20 20 7b   { 247, 1 },.  {
2cd1a 20 32 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   247, 3 },.  { 2
2cd1b 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 39  48, 1 },.  { 249
2cd1c 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34 39 2c 20  , 0 },.  { 249, 
2cd1d 32 20 7d 2c 0a 7d 3b 0a 0a 73 74 61 74 69 63 20  2 },.};..static 
2cd1e 76 6f 69 64 20 79 79 5f 61 63 63 65 70 74 28 79  void yy_accept(y
2cd1f 79 50 61 72 73 65 72 2a 29 3b 20 20 2f 2a 20 46  yParser*);  /* F
2cd20 6f 72 77 61 72 64 20 44 65 63 6c 61 72 61 74 69  orward Declarati
2cd21 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 65 72  on */../*.** Per
2cd22 66 6f 72 6d 20 61 20 72 65 64 75 63 65 20 61 63  form a reduce ac
2cd23 74 69 6f 6e 20 61 6e 64 20 74 68 65 20 73 68 69  tion and the shi
2cd24 66 74 20 74 68 61 74 20 6d 75 73 74 20 69 6d 6d  ft that must imm
2cd25 65 64 69 61 74 65 6c 79 0a 2a 2a 20 66 6f 6c 6c  ediately.** foll
2cd26 6f 77 20 74 68 65 20 72 65 64 75 63 65 2e 0a 2a  ow the reduce..*
2cd27 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  /.static void yy
2cd28 5f 72 65 64 75 63 65 28 0a 20 20 79 79 50 61 72  _reduce(.  yyPar
2cd29 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20  ser *yypParser, 
2cd2a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2cd2b 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20 79  arser */.  int y
2cd2c 79 72 75 6c 65 6e 6f 20 20 20 20 20 20 20 20 20  yruleno         
2cd2d 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2cd2e 72 20 6f 66 20 74 68 65 20 72 75 6c 65 20 62 79  r of the rule by
2cd2f 20 77 68 69 63 68 20 74 6f 20 72 65 64 75 63 65   which to reduce
2cd30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 79 79 67   */.){.  int yyg
2cd31 6f 74 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  oto;            
2cd32 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2cd33 6e 65 78 74 20 73 74 61 74 65 20 2a 2f 0a 20 20  next state */.  
2cd34 69 6e 74 20 79 79 61 63 74 3b 20 20 20 20 20 20  int yyact;      
2cd35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cd36 2f 2a 20 54 68 65 20 6e 65 78 74 20 61 63 74 69  /* The next acti
2cd37 6f 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54  on */.  YYMINORT
2cd38 59 50 45 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b  YPE yygotominor;
2cd39 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c          /* The L
2cd3a 48 53 20 6f 66 20 74 68 65 20 72 75 6c 65 20 72  HS of the rule r
2cd3b 65 64 75 63 65 64 20 2a 2f 0a 20 20 79 79 53 74  educed */.  yySt
2cd3c 61 63 6b 45 6e 74 72 79 20 2a 79 79 6d 73 70 3b  ackEntry *yymsp;
2cd3d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2cd3e 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 70 61  he top of the pa
2cd3f 72 73 65 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a  rser's stack */.
2cd40 20 20 69 6e 74 20 79 79 73 69 7a 65 3b 20 20 20    int yysize;   
2cd41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cd42 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 74 6f 20 70    /* Amount to p
2cd43 6f 70 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a  op the stack */.
2cd44 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
2cd45 52 47 5f 46 45 54 43 48 3b 0a 20 20 79 79 6d 73  RG_FETCH;.  yyms
2cd46 70 20 3d 20 26 79 79 70 50 61 72 73 65 72 2d 3e  p = &yypParser->
2cd47 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65  yystack[yypParse
2cd48 72 2d 3e 79 79 69 64 78 5d 3b 0a 23 69 66 6e 64  r->yyidx];.#ifnd
2cd49 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20  ef NDEBUG.  if( 
2cd4a 79 79 54 72 61 63 65 46 49 4c 45 20 26 26 20 79  yyTraceFILE && y
2cd4b 79 72 75 6c 65 6e 6f 3e 3d 30 20 0a 20 20 20 20  yruleno>=0 .    
2cd4c 20 20 20 20 26 26 20 79 79 72 75 6c 65 6e 6f 3c      && yyruleno<
2cd4d 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 79 79 52  (int)(sizeof(yyR
2cd4e 75 6c 65 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28  uleName)/sizeof(
2cd4f 79 79 52 75 6c 65 4e 61 6d 65 5b 30 5d 29 29 20  yyRuleName[0])) 
2cd50 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79  ){.    fprintf(y
2cd51 79 54 72 61 63 65 46 49 4c 45 2c 20 22 25 73 52  yTraceFILE, "%sR
2cd52 65 64 75 63 65 20 5b 25 73 5d 2e 5c 6e 22 2c 20  educe [%s].\n", 
2cd53 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 0a 20  yyTracePrompt,. 
2cd54 20 20 20 20 20 79 79 52 75 6c 65 4e 61 6d 65 5b       yyRuleName[
2cd55 79 79 72 75 6c 65 6e 6f 5d 29 3b 0a 20 20 7d 0a  yyruleno]);.  }.
2cd56 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
2cd57 20 2a 2f 0a 0a 20 20 2f 2a 20 53 69 6c 65 6e 63   */..  /* Silenc
2cd58 65 20 63 6f 6d 70 6c 61 69 6e 74 73 20 66 72 6f  e complaints fro
2cd59 6d 20 70 75 72 69 66 79 20 61 62 6f 75 74 20 79  m purify about y
2cd5a 79 67 6f 74 6f 6d 69 6e 6f 72 20 62 65 69 6e 67  ygotominor being
2cd5b 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 0a 20   uninitialized. 
2cd5c 20 2a 2a 20 69 6e 20 73 6f 6d 65 20 63 61 73 65   ** in some case
2cd5d 73 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 70  s when it is cop
2cd5e 69 65 64 20 69 6e 74 6f 20 74 68 65 20 73 74 61  ied into the sta
2cd5f 63 6b 20 61 66 74 65 72 20 74 68 65 20 66 6f 6c  ck after the fol
2cd60 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 77 69 74  lowing.  ** swit
2cd61 63 68 2e 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  ch.  yygotominor
2cd62 20 69 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65   is uninitialize
2cd63 64 20 77 68 65 6e 20 61 20 72 75 6c 65 20 72 65  d when a rule re
2cd64 64 75 63 65 73 20 74 68 61 74 20 64 6f 65 73 0a  duces that does.
2cd65 20 20 2a 2a 20 6e 6f 74 20 73 65 74 20 74 68 65    ** not set the
2cd66 20 76 61 6c 75 65 20 6f 66 20 69 74 73 20 6c 65   value of its le
2cd67 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6e 6f 6e  ft-hand side non
2cd68 74 65 72 6d 69 6e 61 6c 2e 20 20 4c 65 61 76 69  terminal.  Leavi
2cd69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75  ng the.  ** valu
2cd6a 65 20 6f 66 20 74 68 65 20 6e 6f 6e 74 65 72 6d  e of the nonterm
2cd6b 69 6e 61 6c 20 75 6e 69 6e 69 74 69 61 6c 69 7a  inal uninitializ
2cd6c 65 64 20 69 73 20 75 74 74 65 72 6c 79 20 68 61  ed is utterly ha
2cd6d 72 6d 6c 65 73 73 20 61 73 20 6c 6f 6e 67 0a 20  rmless as long. 
2cd6e 20 2a 2a 20 61 73 20 74 68 65 20 76 61 6c 75 65   ** as the value
2cd6f 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20   is never used. 
2cd70 20 53 6f 20 72 65 61 6c 6c 79 20 74 68 65 20 6f   So really the o
2cd71 6e 6c 79 20 74 68 69 6e 67 20 74 68 69 73 20 63  nly thing this c
2cd72 6f 64 65 0a 20 20 2a 2a 20 61 63 63 6f 6d 70 6c  ode.  ** accompl
2cd73 69 73 68 65 73 20 69 73 20 74 6f 20 71 75 69 65  ishes is to quie
2cd74 74 65 6e 20 70 75 72 69 66 79 2e 20 20 0a 20 20  ten purify.  .  
2cd75 2a 2a 0a 20 20 2a 2a 20 32 30 30 37 2d 30 31 2d  **.  ** 2007-01-
2cd76 31 36 3a 20 20 54 68 65 20 77 69 72 65 73 68 61  16:  The wiresha
2cd77 72 6b 20 70 72 6f 6a 65 63 74 20 28 77 77 77 2e  rk project (www.
2cd78 77 69 72 65 73 68 61 72 6b 2e 6f 72 67 29 20 72  wireshark.org) r
2cd79 65 70 6f 72 74 73 20 74 68 61 74 0a 20 20 2a 2a  eports that.  **
2cd7a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 63 6f   without this co
2cd7b 64 65 2c 20 74 68 65 69 72 20 70 61 72 73 65 72  de, their parser
2cd7c 20 73 65 67 66 61 75 6c 74 73 2e 20 20 49 27 6d   segfaults.  I'm
2cd7d 20 6e 6f 74 20 73 75 72 65 20 77 68 61 74 20 74   not sure what t
2cd7e 68 65 72 65 0a 20 20 2a 2a 20 70 61 72 73 65 72  here.  ** parser
2cd7f 20 69 73 20 64 6f 69 6e 67 20 74 6f 20 6d 61 6b   is doing to mak
2cd80 65 20 74 68 69 73 20 68 61 70 70 65 6e 2e 20 20  e this happen.  
2cd81 54 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f  This is the seco
2cd82 6e 64 20 62 75 67 20 72 65 70 6f 72 74 0a 20 20  nd bug report.  
2cd83 2a 2a 20 66 72 6f 6d 20 77 69 72 65 73 68 61 72  ** from wireshar
2cd84 6b 20 74 68 69 73 20 77 65 65 6b 2e 20 20 43 6c  k this week.  Cl
2cd85 65 61 72 6c 79 20 74 68 65 79 20 61 72 65 20 73  early they are s
2cd86 74 72 65 73 73 69 6e 67 20 4c 65 6d 6f 6e 20 69  tressing Lemon i
2cd87 6e 20 77 61 79 73 0a 20 20 2a 2a 20 74 68 61 74  n ways.  ** that
2cd88 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 65 6e   it has not been
2cd89 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 72 65   previously stre
2cd8a 73 73 65 64 2e 2e 2e 20 20 28 53 51 4c 69 74 65  ssed...  (SQLite
2cd8b 20 74 69 63 6b 65 74 20 23 32 31 37 32 29 0a 20   ticket #2172). 
2cd8c 20 2a 2f 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26   */.  /*memset(&
2cd8d 79 79 67 6f 74 6f 6d 69 6e 6f 72 2c 20 30 2c 20  yygotominor, 0, 
2cd8e 73 69 7a 65 6f 66 28 79 79 67 6f 74 6f 6d 69 6e  sizeof(yygotomin
2cd8f 6f 72 29 29 3b 2a 2f 0a 20 20 79 79 67 6f 74 6f  or));*/.  yygoto
2cd90 6d 69 6e 6f 72 20 3d 20 79 79 7a 65 72 6f 6d 69  minor = yyzeromi
2cd91 6e 6f 72 3b 0a 0a 0a 20 20 73 77 69 74 63 68 28  nor;...  switch(
2cd92 20 79 79 72 75 6c 65 6e 6f 20 29 7b 0a 20 20 2f   yyruleno ){.  /
2cd93 2a 20 42 65 67 69 6e 6e 69 6e 67 20 68 65 72 65  * Beginning here
2cd94 20 61 72 65 20 74 68 65 20 72 65 64 75 63 74 69   are the reducti
2cd95 6f 6e 20 63 61 73 65 73 2e 20 20 41 20 74 79 70  on cases.  A typ
2cd96 69 63 61 6c 20 65 78 61 6d 70 6c 65 0a 20 20 2a  ical example.  *
2cd97 2a 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 20  * follows:.  ** 
2cd98 20 20 63 61 73 65 20 30 3a 0a 20 20 2a 2a 20 20    case 0:.  **  
2cd99 23 6c 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c  #line <lineno> <
2cd9a 67 72 61 6d 6d 61 72 66 69 6c 65 3e 0a 20 20 2a  grammarfile>.  *
2cd9b 2a 20 20 20 20 20 7b 20 2e 2e 2e 20 7d 20 20 20  *     { ... }   
2cd9c 20 20 20 20 20 20 20 20 2f 2f 20 55 73 65 72 20          // User 
2cd9d 73 75 70 70 6c 69 65 64 20 63 6f 64 65 0a 20 20  supplied code.  
2cd9e 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69 6e 65 6e  **  #line <linen
2cd9f 6f 3e 20 3c 74 68 69 73 66 69 6c 65 3e 0a 20 20  o> <thisfile>.  
2cda0 2a 2a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  **     break;.  
2cda1 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 30 3a  */.      case 0:
2cda2 20 2f 2a 20 69 6e 70 75 74 20 3a 3a 3d 20 63 6d   /* input ::= cm
2cda3 64 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 63  dlist */.      c
2cda4 61 73 65 20 31 3a 20 2f 2a 20 63 6d 64 6c 69 73  ase 1: /* cmdlis
2cda5 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63  t ::= cmdlist ec
2cda6 6d 64 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  md */.      case
2cda7 20 32 3a 20 2f 2a 20 63 6d 64 6c 69 73 74 20 3a   2: /* cmdlist :
2cda8 3a 3d 20 65 63 6d 64 20 2a 2f 0a 20 20 20 20 20  := ecmd */.     
2cda9 20 63 61 73 65 20 33 3a 20 2f 2a 20 65 63 6d 64   case 3: /* ecmd
2cdaa 20 3a 3a 3d 20 53 45 4d 49 20 2a 2f 0a 20 20 20   ::= SEMI */.   
2cdab 20 20 20 63 61 73 65 20 34 3a 20 2f 2a 20 65 63     case 4: /* ec
2cdac 6d 64 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20 63  md ::= explain c
2cdad 6d 64 78 20 53 45 4d 49 20 2a 2f 0a 20 20 20 20  mdx SEMI */.    
2cdae 20 20 63 61 73 65 20 31 30 3a 20 2f 2a 20 74 72    case 10: /* tr
2cdaf 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20  ans_opt ::= */. 
2cdb0 20 20 20 20 20 63 61 73 65 20 31 31 3a 20 2f 2a       case 11: /*
2cdb1 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54   trans_opt ::= T
2cdb2 52 41 4e 53 41 43 54 49 4f 4e 20 2a 2f 0a 20 20  RANSACTION */.  
2cdb3 20 20 20 20 63 61 73 65 20 31 32 3a 20 2f 2a 20      case 12: /* 
2cdb4 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52  trans_opt ::= TR
2cdb5 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 20 2a 2f 0a  ANSACTION nm */.
2cdb6 20 20 20 20 20 20 63 61 73 65 20 32 30 3a 20 2f        case 20: /
2cdb7 2a 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20  * savepoint_opt 
2cdb8 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 2a 2f  ::= SAVEPOINT */
2cdb9 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 3a 20  .      case 21: 
2cdba 2f 2a 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74  /* savepoint_opt
2cdbb 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
2cdbc 73 65 20 32 35 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 25: /* cmd ::
2cdbd 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 63  = create_table c
2cdbe 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73  reate_table_args
2cdbf 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
2cdc0 33 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 6c 69 73 74  3: /* columnlist
2cdc1 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20   ::= columnlist 
2cdc2 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  COMMA column */.
2cdc3 20 20 20 20 20 20 63 61 73 65 20 33 34 3a 20 2f        case 34: /
2cdc4 2a 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d  * columnlist ::=
2cdc5 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20   column */.     
2cdc6 20 63 61 73 65 20 34 32 3a 20 2f 2a 20 74 79 70   case 42: /* typ
2cdc7 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  e ::= */.      c
2cdc8 61 73 65 20 34 39 3a 20 2f 2a 20 73 69 67 6e 65  ase 49: /* signe
2cdc9 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a  d ::= plus_num *
2cdca 2f 0a 20 20 20 20 20 20 63 61 73 65 20 35 30 3a  /.      case 50:
2cdcb 20 2f 2a 20 73 69 67 6e 65 64 20 3a 3a 3d 20 6d   /* signed ::= m
2cdcc 69 6e 75 73 5f 6e 75 6d 20 2a 2f 0a 20 20 20 20  inus_num */.    
2cdcd 20 20 63 61 73 65 20 35 31 3a 20 2f 2a 20 63 61    case 51: /* ca
2cdce 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c  rglist ::= cargl
2cdcf 69 73 74 20 63 61 72 67 20 2a 2f 0a 20 20 20 20  ist carg */.    
2cdd0 20 20 63 61 73 65 20 35 32 3a 20 2f 2a 20 63 61    case 52: /* ca
2cdd1 72 67 6c 69 73 74 20 3a 3a 3d 20 2a 2f 0a 20 20  rglist ::= */.  
2cdd2 20 20 20 20 63 61 73 65 20 35 33 3a 20 2f 2a 20      case 53: /* 
2cdd3 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41  carg ::= CONSTRA
2cdd4 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 20 2a 2f 0a  INT nm ccons */.
2cdd5 20 20 20 20 20 20 63 61 73 65 20 35 34 3a 20 2f        case 54: /
2cdd6 2a 20 63 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73  * carg ::= ccons
2cdd7 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 36   */.      case 6
2cdd8 30 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  0: /* ccons ::= 
2cdd9 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 20  NULL onconf */. 
2cdda 20 20 20 20 20 63 61 73 65 20 38 37 3a 20 2f 2a       case 87: /*
2cddb 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f   conslist ::= co
2cddc 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63 6f  nslist COMMA tco
2cddd 6e 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  ns */.      case
2cdde 20 38 38 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74   88: /* conslist
2cddf 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63   ::= conslist tc
2cde0 6f 6e 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ons */.      cas
2cde1 65 20 38 39 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73  e 89: /* conslis
2cde2 74 20 3a 3a 3d 20 74 63 6f 6e 73 20 2a 2f 0a 20  t ::= tcons */. 
2cde3 20 20 20 20 20 63 61 73 65 20 39 30 3a 20 2f 2a       case 90: /*
2cde4 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54   tcons ::= CONST
2cde5 52 41 49 4e 54 20 6e 6d 20 2a 2f 0a 20 20 20 20  RAINT nm */.    
2cde6 20 20 63 61 73 65 20 32 36 34 3a 20 2f 2a 20 70    case 264: /* p
2cde7 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55 53  lus_opt ::= PLUS
2cde8 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2cde9 36 35 3a 20 2f 2a 20 70 6c 75 73 5f 6f 70 74 20  65: /* plus_opt 
2cdea 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2cdeb 65 20 32 37 35 3a 20 2f 2a 20 66 6f 72 65 61 63  e 275: /* foreac
2cdec 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 0a  h_clause ::= */.
2cded 20 20 20 20 20 20 63 61 73 65 20 32 37 36 3a 20        case 276: 
2cdee 2f 2a 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73  /* foreach_claus
2cdef 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20 52  e ::= FOR EACH R
2cdf0 4f 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  OW */.      case
2cdf1 20 32 39 36 3a 20 2f 2a 20 64 61 74 61 62 61 73   296: /* databas
2cdf2 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54  e_kw_opt ::= DAT
2cdf3 41 42 41 53 45 20 2a 2f 0a 20 20 20 20 20 20 63  ABASE */.      c
2cdf4 61 73 65 20 32 39 37 3a 20 2f 2a 20 64 61 74 61  ase 297: /* data
2cdf5 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20  base_kw_opt ::= 
2cdf6 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  */.      case 30
2cdf7 35 3a 20 2f 2a 20 6b 77 63 6f 6c 75 6d 6e 5f 6f  5: /* kwcolumn_o
2cdf8 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  pt ::= */.      
2cdf9 63 61 73 65 20 33 30 36 3a 20 2f 2a 20 6b 77 63  case 306: /* kwc
2cdfa 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f  olumn_opt ::= CO
2cdfb 4c 55 4d 4e 4b 57 20 2a 2f 0a 20 20 20 20 20 20  LUMNKW */.      
2cdfc 63 61 73 65 20 33 31 30 3a 20 2f 2a 20 76 74 61  case 310: /* vta
2cdfd 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61  barglist ::= vta
2cdfe 62 61 72 67 20 2a 2f 0a 20 20 20 20 20 20 63 61  barg */.      ca
2cdff 73 65 20 33 31 31 3a 20 2f 2a 20 76 74 61 62 61  se 311: /* vtaba
2ce00 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61  rglist ::= vtaba
2ce01 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20 76 74 61  rglist COMMA vta
2ce02 62 61 72 67 20 2a 2f 0a 20 20 20 20 20 20 63 61  barg */.      ca
2ce03 73 65 20 33 31 33 3a 20 2f 2a 20 76 74 61 62 61  se 313: /* vtaba
2ce04 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67 20 76  rg ::= vtabarg v
2ce05 74 61 62 61 72 67 74 6f 6b 65 6e 20 2a 2f 0a 20  tabargtoken */. 
2ce06 20 20 20 20 20 63 61 73 65 20 33 31 37 3a 20 2f       case 317: /
2ce07 2a 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 2a 2f  * anylist ::= */
2ce08 0a 7b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  .{.}.        bre
2ce09 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35  ak;.      case 5
2ce0a 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d  : /* explain ::=
2ce0b 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65 67   */.{ sqlite3Beg
2ce0c 69 6e 50 61 72 73 65 28 70 50 61 72 73 65 2c 20  inParse(pParse, 
2ce0d 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  0); }.        br
2ce0e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ce0f 36 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a  6: /* explain ::
2ce10 3d 20 45 58 50 4c 41 49 4e 20 2a 2f 0a 7b 20 73  = EXPLAIN */.{ s
2ce11 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65  qlite3BeginParse
2ce12 28 70 50 61 72 73 65 2c 20 31 29 3b 20 7d 0a 20  (pParse, 1); }. 
2ce13 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ce14 20 20 20 20 63 61 73 65 20 37 3a 20 2f 2a 20 65      case 7: /* e
2ce15 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41  xplain ::= EXPLA
2ce16 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 2a 2f  IN QUERY PLAN */
2ce17 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50  .{ sqlite3BeginP
2ce18 61 72 73 65 28 70 50 61 72 73 65 2c 20 32 29 3b  arse(pParse, 2);
2ce19 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2ce1a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 3a 20  ;.      case 8: 
2ce1b 2f 2a 20 63 6d 64 78 20 3a 3a 3d 20 63 6d 64 20  /* cmdx ::= cmd 
2ce1c 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 46 69 6e 69  */.{ sqlite3Fini
2ce1d 73 68 43 6f 64 69 6e 67 28 70 50 61 72 73 65 29  shCoding(pParse)
2ce1e 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
2ce1f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 3a  k;.      case 9:
2ce20 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 42 45 47 49   /* cmd ::= BEGI
2ce21 4e 20 74 72 61 6e 73 74 79 70 65 20 74 72 61 6e  N transtype tran
2ce22 73 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65  s_opt */.{sqlite
2ce23 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f  3BeginTransactio
2ce24 6e 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  n(pParse, yymsp[
2ce25 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29  -1].minor.yy392)
2ce26 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2ce27 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 3a  ;.      case 13:
2ce28 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a   /* transtype ::
2ce29 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
2ce2a 72 2e 79 79 33 39 32 20 3d 20 54 4b 5f 44 45 46  r.yy392 = TK_DEF
2ce2b 45 52 52 45 44 3b 7d 0a 20 20 20 20 20 20 20 20  ERRED;}.        
2ce2c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2ce2d 65 20 31 34 3a 20 2f 2a 20 74 72 61 6e 73 74 79  e 14: /* transty
2ce2e 70 65 20 3a 3a 3d 20 44 45 46 45 52 52 45 44 20  pe ::= DEFERRED 
2ce2f 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  */.      case 15
2ce30 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a  : /* transtype :
2ce31 3a 3d 20 49 4d 4d 45 44 49 41 54 45 20 2a 2f 0a  := IMMEDIATE */.
2ce32 20 20 20 20 20 20 63 61 73 65 20 31 36 3a 20 2f        case 16: /
2ce33 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20  * transtype ::= 
2ce34 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 20  EXCLUSIVE */.   
2ce35 20 20 20 63 61 73 65 20 31 31 32 3a 20 2f 2a 20     case 112: /* 
2ce36 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
2ce37 3a 3d 20 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20 20  := UNION */.    
2ce38 20 20 63 61 73 65 20 31 31 34 3a 20 2f 2a 20 6d    case 114: /* m
2ce39 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a  ultiselect_op ::
2ce3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52 53 45  = EXCEPT|INTERSE
2ce3b 43 54 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  CT */.{yygotomin
2ce3c 6f 72 2e 79 79 33 39 32 20 3d 20 79 79 6d 73 70  or.yy392 = yymsp
2ce3d 5b 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20  [0].major;}.    
2ce3e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ce3f 20 63 61 73 65 20 31 37 3a 20 2f 2a 20 63 6d 64   case 17: /* cmd
2ce40 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e   ::= COMMIT tran
2ce41 73 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63  s_opt */.      c
2ce42 61 73 65 20 31 38 3a 20 2f 2a 20 63 6d 64 20 3a  ase 18: /* cmd :
2ce43 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74  := END trans_opt
2ce44 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 6f 6d 6d   */.{sqlite3Comm
2ce45 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  itTransaction(pP
2ce46 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20  arse);}.        
2ce47 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2ce48 65 20 31 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  e 19: /* cmd ::=
2ce49 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f   ROLLBACK trans_
2ce4a 6f 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52  opt */.{sqlite3R
2ce4b 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69  ollbackTransacti
2ce4c 6f 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20  on(pParse);}.   
2ce4d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ce4e 20 20 63 61 73 65 20 32 32 3a 20 2f 2a 20 63 6d    case 22: /* cm
2ce4f 64 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20  d ::= SAVEPOINT 
2ce50 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  nm */.{.  sqlite
2ce51 33 53 61 76 65 70 6f 69 6e 74 28 70 50 61 72 73  3Savepoint(pPars
2ce52 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f 42 45 47  e, SAVEPOINT_BEG
2ce53 49 4e 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  IN, &yymsp[0].mi
2ce54 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
2ce55 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ce56 20 63 61 73 65 20 32 33 3a 20 2f 2a 20 63 6d 64   case 23: /* cmd
2ce57 20 3a 3a 3d 20 52 45 4c 45 41 53 45 20 73 61 76   ::= RELEASE sav
2ce58 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f  epoint_opt nm */
2ce59 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61 76 65  .{.  sqlite3Save
2ce5a 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20 53 41  point(pParse, SA
2ce5b 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c  VEPOINT_RELEASE,
2ce5c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
2ce5d 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
2ce5e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2ce5f 73 65 20 32 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 24: /* cmd ::
2ce60 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73  = ROLLBACK trans
2ce61 5f 6f 70 74 20 54 4f 20 73 61 76 65 70 6f 69 6e  _opt TO savepoin
2ce62 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a 20 20  t_opt nm */.{.  
2ce63 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74  sqlite3Savepoint
2ce64 28 70 50 61 72 73 65 2c 20 53 41 56 45 50 4f 49  (pParse, SAVEPOI
2ce65 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 26 79 79  NT_ROLLBACK, &yy
2ce66 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2ce67 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2ce68 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2ce69 36 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62  6: /* create_tab
2ce6a 6c 65 20 3a 3a 3d 20 43 52 45 41 54 45 20 74 65  le ::= CREATE te
2ce6b 6d 70 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78  mp TABLE ifnotex
2ce6c 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a  ists nm dbnm */.
2ce6d 7b 0a 20 20 20 73 71 6c 69 74 65 33 53 74 61 72  {.   sqlite3Star
2ce6e 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 26 79  tTable(pParse,&y
2ce6f 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ce70 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
2ce71 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 34 5d  or.yy0,yymsp[-4]
2ce72 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 2c 30 2c 30  .minor.yy392,0,0
2ce73 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
2ce74 2e 79 79 33 39 32 29 3b 0a 7d 0a 20 20 20 20 20  .yy392);.}.     
2ce75 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ce76 63 61 73 65 20 32 37 3a 20 2f 2a 20 69 66 6e 6f  case 27: /* ifno
2ce77 74 65 78 69 73 74 73 20 3a 3a 3d 20 2a 2f 0a 20  texists ::= */. 
2ce78 20 20 20 20 20 63 61 73 65 20 33 30 3a 20 2f 2a       case 30: /*
2ce79 20 74 65 6d 70 20 3a 3a 3d 20 2a 2f 0a 20 20 20   temp ::= */.   
2ce7a 20 20 20 63 61 73 65 20 36 38 3a 20 2f 2a 20 61     case 68: /* a
2ce7b 75 74 6f 69 6e 63 20 3a 3a 3d 20 2a 2f 0a 20 20  utoinc ::= */.  
2ce7c 20 20 20 20 63 61 73 65 20 38 32 3a 20 2f 2a 20      case 82: /* 
2ce7d 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72  init_deferred_pr
2ce7e 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20  ed_opt ::= */.  
2ce7f 20 20 20 20 63 61 73 65 20 38 34 3a 20 2f 2a 20      case 84: /* 
2ce80 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72  init_deferred_pr
2ce81 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49  ed_opt ::= INITI
2ce82 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 2a  ALLY IMMEDIATE *
2ce83 2f 0a 20 20 20 20 20 20 63 61 73 65 20 39 35 3a  /.      case 95:
2ce84 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61   /* defer_subcla
2ce85 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20  use_opt ::= */. 
2ce86 20 20 20 20 20 63 61 73 65 20 31 30 36 3a 20 2f       case 106: /
2ce87 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 2a  * ifexists ::= *
2ce88 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 37  /.      case 117
2ce89 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a  : /* distinct ::
2ce8a 3d 20 41 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 63  = ALL */.      c
2ce8b 61 73 65 20 31 31 38 3a 20 2f 2a 20 64 69 73 74  ase 118: /* dist
2ce8c 69 6e 63 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  inct ::= */.    
2ce8d 20 20 63 61 73 65 20 32 32 30 3a 20 2f 2a 20 62    case 220: /* b
2ce8e 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45  etween_op ::= BE
2ce8f 54 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20 20 63  TWEEN */.      c
2ce90 61 73 65 20 32 32 33 3a 20 2f 2a 20 69 6e 5f 6f  ase 223: /* in_o
2ce91 70 20 3a 3a 3d 20 49 4e 20 2a 2f 0a 7b 79 79 67  p ::= IN */.{yyg
2ce92 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d  otominor.yy392 =
2ce93 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
2ce94 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2ce95 38 3a 20 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74  8: /* ifnotexist
2ce96 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58 49  s ::= IF NOT EXI
2ce97 53 54 53 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  STS */.      cas
2ce98 65 20 32 39 3a 20 2f 2a 20 74 65 6d 70 20 3a 3a  e 29: /* temp ::
2ce99 3d 20 54 45 4d 50 20 2a 2f 0a 20 20 20 20 20 20  = TEMP */.      
2ce9a 63 61 73 65 20 36 39 3a 20 2f 2a 20 61 75 74 6f  case 69: /* auto
2ce9b 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52  inc ::= AUTOINCR
2ce9c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 38   */.      case 8
2ce9d 33 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72  3: /* init_defer
2ce9e 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d  red_pred_opt ::=
2ce9f 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
2cea0 52 45 44 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  RED */.      cas
2cea1 65 20 31 30 35 3a 20 2f 2a 20 69 66 65 78 69 73  e 105: /* ifexis
2cea2 74 73 20 3a 3a 3d 20 49 46 20 45 58 49 53 54 53  ts ::= IF EXISTS
2cea3 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2cea4 31 36 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20  16: /* distinct 
2cea5 3a 3a 3d 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a  ::= DISTINCT */.
2cea6 20 20 20 20 20 20 63 61 73 65 20 32 32 31 3a 20        case 221: 
2cea7 2f 2a 20 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a  /* between_op ::
2cea8 3d 20 4e 4f 54 20 42 45 54 57 45 45 4e 20 2a 2f  = NOT BETWEEN */
2cea9 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 34 3a  .      case 224:
2ceaa 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f   /* in_op ::= NO
2ceab 54 20 49 4e 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  T IN */.{yygotom
2ceac 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20 31 3b 7d  inor.yy392 = 1;}
2cead 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ceae 20 20 20 20 20 20 63 61 73 65 20 33 31 3a 20 2f        case 31: /
2ceaf 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61  * create_table_a
2ceb0 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d  rgs ::= LP colum
2ceb1 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f 6f  nlist conslist_o
2ceb2 70 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c  pt RP */.{.  sql
2ceb3 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61  ite3EndTable(pPa
2ceb4 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  rse,&yymsp[-1].m
2ceb5 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2ceb6 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b  0].minor.yy0,0);
2ceb7 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2ceb8 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 32 3a  ;.      case 32:
2ceb9 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65   /* create_table
2ceba 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c  _args ::= AS sel
2cebb 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  ect */.{.  sqlit
2cebc 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73  e3EndTable(pPars
2cebd 65 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d  e,0,0,yymsp[0].m
2cebe 69 6e 6f 72 2e 79 79 31 35 39 29 3b 0a 20 20 73  inor.yy159);.  s
2cebf 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2cec0 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
2cec1 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2cec2 31 35 39 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  159);.}.        
2cec3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2cec4 65 20 33 35 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 20  e 35: /* column 
2cec5 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79 70  ::= columnid typ
2cec6 65 20 63 61 72 67 6c 69 73 74 20 2a 2f 0a 7b 0a  e carglist */.{.
2cec7 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2cec8 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e  0.z = yymsp[-2].
2cec9 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79  minor.yy0.z;.  y
2ceca 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e  ygotominor.yy0.n
2cecb 20 3d 20 28 69 6e 74 29 28 70 50 61 72 73 65 2d   = (int)(pParse-
2cecc 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79 79  >sLastToken.z-yy
2cecd 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2cece 30 2e 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e 73  0.z) + pParse->s
2cecf 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20  LastToken.n;.}. 
2ced0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ced1 20 20 20 20 63 61 73 65 20 33 36 3a 20 2f 2a 20      case 36: /* 
2ced2 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 20  columnid ::= nm 
2ced3 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 64  */.{.  sqlite3Ad
2ced4 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 26  dColumn(pParse,&
2ced5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ced6 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  y0);.  yygotomin
2ced7 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30  or.yy0 = yymsp[0
2ced8 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20  ].minor.yy0;.}. 
2ced9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ceda 20 20 20 20 63 61 73 65 20 33 37 3a 20 2f 2a 20      case 37: /* 
2cedb 69 64 20 3a 3a 3d 20 49 44 20 2a 2f 0a 20 20 20  id ::= ID */.   
2cedc 20 20 20 63 61 73 65 20 33 38 3a 20 2f 2a 20 69     case 38: /* i
2cedd 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47  ds ::= ID|STRING
2cede 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
2cedf 39 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 49 44 20  9: /* nm ::= ID 
2cee0 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 34 30  */.      case 40
2cee1 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 53 54 52 49  : /* nm ::= STRI
2cee2 4e 47 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  NG */.      case
2cee3 20 34 31 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 4a   41: /* nm ::= J
2cee4 4f 49 4e 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  OIN_KW */.      
2cee5 63 61 73 65 20 34 34 3a 20 2f 2a 20 74 79 70 65  case 44: /* type
2cee6 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61  token ::= typena
2cee7 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  me */.      case
2cee8 20 34 37 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65   47: /* typename
2cee9 20 3a 3a 3d 20 69 64 73 20 2a 2f 0a 20 20 20 20   ::= ids */.    
2ceea 20 20 63 61 73 65 20 31 32 34 3a 20 2f 2a 20 61    case 124: /* a
2ceeb 73 20 3a 3a 3d 20 41 53 20 6e 6d 20 2a 2f 0a 20  s ::= AS nm */. 
2ceec 20 20 20 20 20 63 61 73 65 20 31 32 35 3a 20 2f       case 125: /
2ceed 2a 20 61 73 20 3a 3a 3d 20 69 64 73 20 2a 2f 0a  * as ::= ids */.
2ceee 20 20 20 20 20 20 63 61 73 65 20 31 33 35 3a 20        case 135: 
2ceef 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20  /* dbnm ::= DOT 
2cef0 6e 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  nm */.      case
2cef1 20 31 34 34 3a 20 2f 2a 20 69 6e 64 65 78 65 64   144: /* indexed
2cef2 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44 45 58 45 44  _opt ::= INDEXED
2cef3 20 42 59 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20   BY nm */.      
2cef4 63 61 73 65 20 32 34 39 3a 20 2f 2a 20 63 6f 6c  case 249: /* col
2cef5 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45  late ::= COLLATE
2cef6 20 69 64 73 20 2a 2f 0a 20 20 20 20 20 20 63 61   ids */.      ca
2cef7 73 65 20 32 35 39 3a 20 2f 2a 20 6e 6d 6e 75 6d  se 259: /* nmnum
2cef8 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f   ::= plus_num */
2cef9 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 30 3a  .      case 260:
2cefa 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d   /* nmnum ::= nm
2cefb 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2cefc 36 31 3a 20 2f 2a 20 70 6c 75 73 5f 6e 75 6d 20  61: /* plus_num 
2cefd 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d  ::= plus_opt num
2cefe 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ber */.      cas
2ceff 65 20 32 36 32 3a 20 2f 2a 20 6d 69 6e 75 73 5f  e 262: /* minus_
2cf00 6e 75 6d 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75  num ::= MINUS nu
2cf01 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  mber */.      ca
2cf02 73 65 20 32 36 33 3a 20 2f 2a 20 6e 75 6d 62 65  se 263: /* numbe
2cf03 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c  r ::= INTEGER|FL
2cf04 4f 41 54 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  OAT */.{yygotomi
2cf05 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b  nor.yy0 = yymsp[
2cf06 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20  0].minor.yy0;}. 
2cf07 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2cf08 20 20 20 20 63 61 73 65 20 34 33 3a 20 2f 2a 20      case 43: /* 
2cf09 74 79 70 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b  type ::= typetok
2cf0a 65 6e 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  en */.{sqlite3Ad
2cf0b 64 43 6f 6c 75 6d 6e 54 79 70 65 28 70 50 61 72  dColumnType(pPar
2cf0c 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se,&yymsp[0].min
2cf0d 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
2cf0e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2cf0f 61 73 65 20 34 35 3a 20 2f 2a 20 74 79 70 65 74  ase 45: /* typet
2cf10 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d  oken ::= typenam
2cf11 65 20 4c 50 20 73 69 67 6e 65 64 20 52 50 20 2a  e LP signed RP *
2cf12 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2cf13 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b  r.yy0.z = yymsp[
2cf14 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -3].minor.yy0.z;
2cf15 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2cf16 79 30 2e 6e 20 3d 20 28 69 6e 74 29 28 26 79 79  y0.n = (int)(&yy
2cf17 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2cf18 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  .z[yymsp[0].mino
2cf19 72 2e 79 79 30 2e 6e 5d 20 2d 20 79 79 6d 73 70  r.yy0.n] - yymsp
2cf1a 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-3].minor.yy0.z
2cf1b 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2cf1c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34  ak;.      case 4
2cf1d 36 3a 20 2f 2a 20 74 79 70 65 74 6f 6b 65 6e 20  6: /* typetoken 
2cf1e 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20  ::= typename LP 
2cf1f 73 69 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67  signed COMMA sig
2cf20 6e 65 64 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79  ned RP */.{.  yy
2cf21 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20  gotominor.yy0.z 
2cf22 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  = yymsp[-5].mino
2cf23 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74  r.yy0.z;.  yygot
2cf24 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28  ominor.yy0.n = (
2cf25 69 6e 74 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d  int)(&yymsp[0].m
2cf26 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
2cf27 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
2cf28 20 2d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e   - yymsp[-5].min
2cf29 6f 72 2e 79 79 30 2e 7a 29 3b 0a 7d 0a 20 20 20  or.yy0.z);.}.   
2cf2a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2cf2b 20 20 63 61 73 65 20 34 38 3a 20 2f 2a 20 74 79    case 48: /* ty
2cf2c 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70 65 6e  pename ::= typen
2cf2d 61 6d 65 20 69 64 73 20 2a 2f 0a 7b 79 79 67 6f  ame ids */.{yygo
2cf2e 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 79 79  tominor.yy0.z=yy
2cf2f 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2cf30 30 2e 7a 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  0.z; yygotominor
2cf31 2e 79 79 30 2e 6e 3d 79 79 6d 73 70 5b 30 5d 2e  .yy0.n=yymsp[0].
2cf32 6d 69 6e 6f 72 2e 79 79 30 2e 6e 2b 28 69 6e 74  minor.yy0.n+(int
2cf33 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  )(yymsp[0].minor
2cf34 2e 79 79 30 2e 7a 2d 79 79 6d 73 70 5b 2d 31 5d  .yy0.z-yymsp[-1]
2cf35 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 7d 0a  .minor.yy0.z);}.
2cf36 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2cf37 20 20 20 20 20 63 61 73 65 20 35 35 3a 20 2f 2a       case 55: /*
2cf38 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55   ccons ::= DEFAU
2cf39 4c 54 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20  LT term */.     
2cf3a 20 63 61 73 65 20 35 37 3a 20 2f 2a 20 63 63 6f   case 57: /* cco
2cf3b 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50  ns ::= DEFAULT P
2cf3c 4c 55 53 20 74 65 72 6d 20 2a 2f 0a 7b 73 71 6c  LUS term */.{sql
2cf3d 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61  ite3AddDefaultVa
2cf3e 6c 75 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70  lue(pParse,yymsp
2cf3f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 29  [0].minor.yy122)
2cf40 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2cf41 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 36 3a  ;.      case 56:
2cf42 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45   /* ccons ::= DE
2cf43 46 41 55 4c 54 20 4c 50 20 65 78 70 72 20 52 50  FAULT LP expr RP
2cf44 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 44   */.{sqlite3AddD
2cf45 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72  efaultValue(pPar
2cf46 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  se,yymsp[-1].min
2cf47 6f 72 2e 79 79 31 32 32 29 3b 7d 0a 20 20 20 20  or.yy122);}.    
2cf48 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2cf49 20 63 61 73 65 20 35 38 3a 20 2f 2a 20 63 63 6f   case 58: /* cco
2cf4a 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4d  ns ::= DEFAULT M
2cf4b 49 4e 55 53 20 74 65 72 6d 20 2a 2f 0a 7b 0a 20  INUS term */.{. 
2cf4c 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74   Expr *p = sqlit
2cf4d 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2cf4e 54 4b 5f 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70  TK_UMINUS, yymsp
2cf4f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c  [0].minor.yy122,
2cf50 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
2cf51 33 45 78 70 72 53 70 61 6e 28 70 2c 26 79 79 6d  3ExprSpan(p,&yym
2cf52 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2cf53 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2cf54 2e 79 79 31 32 32 2d 3e 73 70 61 6e 29 3b 0a 20  .yy122->span);. 
2cf55 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75   sqlite3AddDefau
2cf56 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c 70  ltValue(pParse,p
2cf57 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2cf58 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35  ak;.      case 5
2cf59 39 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  9: /* ccons ::= 
2cf5a 44 45 46 41 55 4c 54 20 69 64 20 2a 2f 0a 7b 0a  DEFAULT id */.{.
2cf5b 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69    Expr *p = sqli
2cf5c 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2cf5d 20 54 4b 5f 53 54 52 49 4e 47 2c 20 30 2c 20 30   TK_STRING, 0, 0
2cf5e 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2cf5f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65  r.yy0);.  sqlite
2cf60 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65  3AddDefaultValue
2cf61 28 70 50 61 72 73 65 2c 70 29 3b 0a 7d 0a 20 20  (pParse,p);.}.  
2cf62 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2cf63 20 20 20 63 61 73 65 20 36 31 3a 20 2f 2a 20 63     case 61: /* c
2cf64 63 6f 6e 73 20 3a 3a 3d 20 4e 4f 54 20 4e 55 4c  cons ::= NOT NUL
2cf65 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c  L onconf */.{sql
2cf66 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 70  ite3AddNotNull(p
2cf67 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
2cf68 6d 69 6e 6f 72 2e 79 79 33 39 32 29 3b 7d 0a 20  minor.yy392);}. 
2cf69 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2cf6a 20 20 20 20 63 61 73 65 20 36 32 3a 20 2f 2a 20      case 62: /* 
2cf6b 63 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52  ccons ::= PRIMAR
2cf6c 59 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20  Y KEY sortorder 
2cf6d 6f 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 20 2a  onconf autoinc *
2cf6e 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 50 72 69  /.{sqlite3AddPri
2cf6f 6d 61 72 79 4b 65 79 28 70 50 61 72 73 65 2c 30  maryKey(pParse,0
2cf70 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2cf71 2e 79 79 33 39 32 2c 79 79 6d 73 70 5b 30 5d 2e  .yy392,yymsp[0].
2cf72 6d 69 6e 6f 72 2e 79 79 33 39 32 2c 79 79 6d 73  minor.yy392,yyms
2cf73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39  p[-2].minor.yy39
2cf74 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  2);}.        bre
2cf75 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
2cf76 33 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  3: /* ccons ::= 
2cf77 55 4e 49 51 55 45 20 6f 6e 63 6f 6e 66 20 2a 2f  UNIQUE onconf */
2cf78 0a 7b 73 71 6c 69 74 65 33 43 72 65 61 74 65 49  .{sqlite3CreateI
2cf79 6e 64 65 78 28 70 50 61 72 73 65 2c 30 2c 30 2c  ndex(pParse,0,0,
2cf7a 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0,0,yymsp[0].min
2cf7b 6f 72 2e 79 79 33 39 32 2c 30 2c 30 2c 30 2c 30  or.yy392,0,0,0,0
2cf7c 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2cf7d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 34  k;.      case 64
2cf7e 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 43  : /* ccons ::= C
2cf7f 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50 20  HECK LP expr RP 
2cf80 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 68  */.{sqlite3AddCh
2cf81 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74 28 70 50  eckConstraint(pP
2cf82 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  arse,yymsp[-1].m
2cf83 69 6e 6f 72 2e 79 79 31 32 32 29 3b 7d 0a 20 20  inor.yy122);}.  
2cf84 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2cf85 20 20 20 63 61 73 65 20 36 35 3a 20 2f 2a 20 63     case 65: /* c
2cf86 63 6f 6e 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e  cons ::= REFEREN
2cf87 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f  CES nm idxlist_o
2cf88 70 74 20 72 65 66 61 72 67 73 20 2a 2f 0a 7b 73  pt refargs */.{s
2cf89 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 65  qlite3CreateFore
2cf8a 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 30 2c  ignKey(pParse,0,
2cf8b 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
2cf8c 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  .yy0,yymsp[-1].m
2cf8d 69 6e 6f 72 2e 79 79 34 34 32 2c 79 79 6d 73 70  inor.yy442,yymsp
2cf8e 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29  [0].minor.yy392)
2cf8f 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2cf90 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 36 3a  ;.      case 66:
2cf91 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 64 65   /* ccons ::= de
2cf92 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f  fer_subclause */
2cf93 0a 7b 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f  .{sqlite3DeferFo
2cf94 72 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c  reignKey(pParse,
2cf95 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2cf96 79 33 39 32 29 3b 7d 0a 20 20 20 20 20 20 20 20  y392);}.        
2cf97 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2cf98 65 20 36 37 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 67: /* ccons :
2cf99 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a  := COLLATE ids *
2cf9a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  /.{sqlite3AddCol
2cf9b 6c 61 74 65 54 79 70 65 28 70 50 61 72 73 65 2c  lateType(pParse,
2cf9c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
2cf9d 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
2cf9e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2cf9f 65 20 37 30 3a 20 2f 2a 20 72 65 66 61 72 67 73  e 70: /* refargs
2cfa0 20 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f   ::= */.{ yygoto
2cfa1 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20 4f 45  minor.yy392 = OE
2cfa2 5f 52 65 73 74 72 69 63 74 20 2a 20 30 78 30 31  _Restrict * 0x01
2cfa3 30 31 30 31 3b 20 7d 0a 20 20 20 20 20 20 20 20  0101; }.        
2cfa4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2cfa5 65 20 37 31 3a 20 2f 2a 20 72 65 66 61 72 67 73  e 71: /* refargs
2cfa6 20 3a 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66   ::= refargs ref
2cfa7 61 72 67 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  arg */.{ yygotom
2cfa8 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20 28 79 79  inor.yy392 = (yy
2cfa9 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2cfaa 33 39 32 20 26 20 7e 79 79 6d 73 70 5b 30 5d 2e  392 & ~yymsp[0].
2cfab 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 6d 61 73 6b  minor.yy207.mask
2cfac 29 20 7c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  ) | yymsp[0].min
2cfad 6f 72 2e 79 79 32 30 37 2e 76 61 6c 75 65 3b 20  or.yy207.value; 
2cfae 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2cfaf 0a 20 20 20 20 20 20 63 61 73 65 20 37 32 3a 20  .      case 72: 
2cfb0 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41  /* refarg ::= MA
2cfb1 54 43 48 20 6e 6d 20 2a 2f 0a 7b 20 79 79 67 6f  TCH nm */.{ yygo
2cfb2 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 76 61  tominor.yy207.va
2cfb3 6c 75 65 20 3d 20 30 3b 20 20 20 20 20 79 79 67  lue = 0;     yyg
2cfb4 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 6d  otominor.yy207.m
2cfb5 61 73 6b 20 3d 20 30 78 30 30 30 30 30 30 3b 20  ask = 0x000000; 
2cfb6 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2cfb7 0a 20 20 20 20 20 20 63 61 73 65 20 37 33 3a 20  .      case 73: 
2cfb8 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e  /* refarg ::= ON
2cfb9 20 44 45 4c 45 54 45 20 72 65 66 61 63 74 20 2a   DELETE refact *
2cfba 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
2cfbb 79 79 32 30 37 2e 76 61 6c 75 65 20 3d 20 79 79  yy207.value = yy
2cfbc 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
2cfbd 39 32 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d 69  92;     yygotomi
2cfbe 6e 6f 72 2e 79 79 32 30 37 2e 6d 61 73 6b 20 3d  nor.yy207.mask =
2cfbf 20 30 78 30 30 30 30 66 66 3b 20 7d 0a 20 20 20   0x0000ff; }.   
2cfc0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2cfc1 20 20 63 61 73 65 20 37 34 3a 20 2f 2a 20 72 65    case 74: /* re
2cfc2 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41  farg ::= ON UPDA
2cfc3 54 45 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79  TE refact */.{ y
2cfc4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 37  ygotominor.yy207
2cfc5 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30  .value = yymsp[0
2cfc6 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 3c 3c 38  ].minor.yy392<<8
2cfc7 3b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;  yygotominor.y
2cfc8 79 32 30 37 2e 6d 61 73 6b 20 3d 20 30 78 30 30  y207.mask = 0x00
2cfc9 66 66 30 30 3b 20 7d 0a 20 20 20 20 20 20 20 20  ff00; }.        
2cfca 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2cfcb 65 20 37 35 3a 20 2f 2a 20 72 65 66 61 72 67 20  e 75: /* refarg 
2cfcc 3a 3a 3d 20 4f 4e 20 49 4e 53 45 52 54 20 72 65  ::= ON INSERT re
2cfcd 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  fact */.{ yygoto
2cfce 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 76 61 6c 75  minor.yy207.valu
2cfcf 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e = yymsp[0].min
2cfd0 6f 72 2e 79 79 33 39 32 3c 3c 31 36 3b 20 79 79  or.yy392<<16; yy
2cfd1 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e  gotominor.yy207.
2cfd2 6d 61 73 6b 20 3d 20 30 78 66 66 30 30 30 30 3b  mask = 0xff0000;
2cfd3 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2cfd4 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 36 3a  ;.      case 76:
2cfd5 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 53   /* refact ::= S
2cfd6 45 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 20 79 79 67  ET NULL */.{ yyg
2cfd7 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d  otominor.yy392 =
2cfd8 20 4f 45 5f 53 65 74 4e 75 6c 6c 3b 20 7d 0a 20   OE_SetNull; }. 
2cfd9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2cfda 20 20 20 20 63 61 73 65 20 37 37 3a 20 2f 2a 20      case 77: /* 
2cfdb 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 44  refact ::= SET D
2cfdc 45 46 41 55 4c 54 20 2a 2f 0a 7b 20 79 79 67 6f  EFAULT */.{ yygo
2cfdd 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20  tominor.yy392 = 
2cfde 4f 45 5f 53 65 74 44 66 6c 74 3b 20 7d 0a 20 20  OE_SetDflt; }.  
2cfdf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2cfe0 20 20 20 63 61 73 65 20 37 38 3a 20 2f 2a 20 72     case 78: /* r
2cfe1 65 66 61 63 74 20 3a 3a 3d 20 43 41 53 43 41 44  efact ::= CASCAD
2cfe2 45 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  E */.{ yygotomin
2cfe3 6f 72 2e 79 79 33 39 32 20 3d 20 4f 45 5f 43 61  or.yy392 = OE_Ca
2cfe4 73 63 61 64 65 3b 20 7d 0a 20 20 20 20 20 20 20  scade; }.       
2cfe5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2cfe6 73 65 20 37 39 3a 20 2f 2a 20 72 65 66 61 63 74  se 79: /* refact
2cfe7 20 3a 3a 3d 20 52 45 53 54 52 49 43 54 20 2a 2f   ::= RESTRICT */
2cfe8 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
2cfe9 79 33 39 32 20 3d 20 4f 45 5f 52 65 73 74 72 69  y392 = OE_Restri
2cfea 63 74 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ct; }.        br
2cfeb 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2cfec 38 30 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62  80: /* defer_sub
2cfed 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44  clause ::= NOT D
2cfee 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64  EFERRABLE init_d
2cfef 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
2cff0 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 38   */.      case 8
2cff1 31 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63  1: /* defer_subc
2cff2 6c 61 75 73 65 20 3a 3a 3d 20 44 45 46 45 52 52  lause ::= DEFERR
2cff3 41 42 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72  ABLE init_deferr
2cff4 65 64 5f 70 72 65 64 5f 6f 70 74 20 2a 2f 0a 20  ed_pred_opt */. 
2cff5 20 20 20 20 20 63 61 73 65 20 39 36 3a 20 2f 2a       case 96: /*
2cff6 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
2cff7 5f 6f 70 74 20 3a 3a 3d 20 64 65 66 65 72 5f 73  _opt ::= defer_s
2cff8 75 62 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  ubclause */.    
2cff9 20 20 63 61 73 65 20 39 38 3a 20 2f 2a 20 6f 6e    case 98: /* on
2cffa 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e 46  conf ::= ON CONF
2cffb 4c 49 43 54 20 72 65 73 6f 6c 76 65 74 79 70 65  LICT resolvetype
2cffc 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2cffd 30 30 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a  00: /* orconf ::
2cffe 3d 20 4f 52 20 72 65 73 6f 6c 76 65 74 79 70 65  = OR resolvetype
2cfff 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2d000 30 31 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79  01: /* resolvety
2d001 70 65 20 3a 3a 3d 20 72 61 69 73 65 74 79 70 65  pe ::= raisetype
2d002 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2d003 37 33 3a 20 2f 2a 20 69 6e 73 65 72 74 5f 63 6d  73: /* insert_cm
2d004 64 20 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72 63  d ::= INSERT orc
2d005 6f 6e 66 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  onf */.{yygotomi
2d006 6e 6f 72 2e 79 79 33 39 32 20 3d 20 79 79 6d 73  nor.yy392 = yyms
2d007 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32  p[0].minor.yy392
2d008 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2d009 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 35 3a  ;.      case 85:
2d00a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74   /* conslist_opt
2d00b 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
2d00c 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 20  inor.yy0.n = 0; 
2d00d 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
2d00e 7a 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  z = 0;}.        
2d00f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d010 65 20 38 36 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73  e 86: /* conslis
2d011 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20  t_opt ::= COMMA 
2d012 63 6f 6e 73 6c 69 73 74 20 2a 2f 0a 7b 79 79 67  conslist */.{yyg
2d013 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79  otominor.yy0 = y
2d014 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2d015 79 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  y0;}.        bre
2d016 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
2d017 31 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20  1: /* tcons ::= 
2d018 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69  PRIMARY KEY LP i
2d019 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52  dxlist autoinc R
2d01a 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c  P onconf */.{sql
2d01b 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
2d01c 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  y(pParse,yymsp[-
2d01d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 79  3].minor.yy442,y
2d01e 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2d01f 33 39 32 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  392,yymsp[-2].mi
2d020 6e 6f 72 2e 79 79 33 39 32 2c 30 29 3b 7d 0a 20  nor.yy392,0);}. 
2d021 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2d022 20 20 20 20 63 61 73 65 20 39 32 3a 20 2f 2a 20      case 92: /* 
2d023 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45  tcons ::= UNIQUE
2d024 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 6f   LP idxlist RP o
2d025 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65  nconf */.{sqlite
2d026 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61  3CreateIndex(pPa
2d027 72 73 65 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b  rse,0,0,0,yymsp[
2d028 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c  -2].minor.yy442,
2d029 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2d02a 79 33 39 32 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a  y392,0,0,0,0);}.
2d02b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d02c 20 20 20 20 20 63 61 73 65 20 39 33 3a 20 2f 2a       case 93: /*
2d02d 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b   tcons ::= CHECK
2d02e 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e 63 6f   LP expr RP onco
2d02f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  nf */.{sqlite3Ad
2d030 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74  dCheckConstraint
2d031 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32  (pParse,yymsp[-2
2d032 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 29 3b 7d  ].minor.yy122);}
2d033 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d034 20 20 20 20 20 20 63 61 73 65 20 39 34 3a 20 2f        case 94: /
2d035 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 46 4f 52 45  * tcons ::= FORE
2d036 49 47 4e 20 4b 45 59 20 4c 50 20 69 64 78 6c 69  IGN KEY LP idxli
2d037 73 74 20 52 50 20 52 45 46 45 52 45 4e 43 45 53  st RP REFERENCES
2d038 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   nm idxlist_opt 
2d039 72 65 66 61 72 67 73 20 64 65 66 65 72 5f 73 75  refargs defer_su
2d03a 62 63 6c 61 75 73 65 5f 6f 70 74 20 2a 2f 0a 7b  bclause_opt */.{
2d03b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61  .    sqlite3Crea
2d03c 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61  teForeignKey(pPa
2d03d 72 73 65 2c 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d  rse, yymsp[-6].m
2d03e 69 6e 6f 72 2e 79 79 34 34 32 2c 20 26 79 79 6d  inor.yy442, &yym
2d03f 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
2d040 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
2d041 72 2e 79 79 34 34 32 2c 20 79 79 6d 73 70 5b 2d  r.yy442, yymsp[-
2d042 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29 3b  1].minor.yy392);
2d043 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 66 65  .    sqlite3Defe
2d044 72 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61 72  rForeignKey(pPar
2d045 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se, yymsp[0].min
2d046 6f 72 2e 79 79 33 39 32 29 3b 0a 7d 0a 20 20 20  or.yy392);.}.   
2d047 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d048 20 20 63 61 73 65 20 39 37 3a 20 2f 2a 20 6f 6e    case 97: /* on
2d049 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  conf ::= */.    
2d04a 20 20 63 61 73 65 20 39 39 3a 20 2f 2a 20 6f 72    case 99: /* or
2d04b 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  conf ::= */.{yyg
2d04c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d  otominor.yy392 =
2d04d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 7d 0a 20 20   OE_Default;}.  
2d04e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d04f 20 20 20 63 61 73 65 20 31 30 32 3a 20 2f 2a 20     case 102: /* 
2d050 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20  resolvetype ::= 
2d051 49 47 4e 4f 52 45 20 2a 2f 0a 7b 79 79 67 6f 74  IGNORE */.{yygot
2d052 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20 4f  ominor.yy392 = O
2d053 45 5f 49 67 6e 6f 72 65 3b 7d 0a 20 20 20 20 20  E_Ignore;}.     
2d054 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2d055 63 61 73 65 20 31 30 33 3a 20 2f 2a 20 72 65 73  case 103: /* res
2d056 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 52 45 50  olvetype ::= REP
2d057 4c 41 43 45 20 2a 2f 0a 20 20 20 20 20 20 63 61  LACE */.      ca
2d058 73 65 20 31 37 34 3a 20 2f 2a 20 69 6e 73 65 72  se 174: /* inser
2d059 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41 43  t_cmd ::= REPLAC
2d05a 45 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  E */.{yygotomino
2d05b 72 2e 79 79 33 39 32 20 3d 20 4f 45 5f 52 65 70  r.yy392 = OE_Rep
2d05c 6c 61 63 65 3b 7d 0a 20 20 20 20 20 20 20 20 62  lace;}.        b
2d05d 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d05e 20 31 30 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   104: /* cmd ::=
2d05f 20 44 52 4f 50 20 54 41 42 4c 45 20 69 66 65 78   DROP TABLE ifex
2d060 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f  ists fullname */
2d061 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70  .{.  sqlite3Drop
2d062 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 79 79  Table(pParse, yy
2d063 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
2d064 34 37 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 31 5d  47, 0, yymsp[-1]
2d065 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29 3b 0a 7d  .minor.yy392);.}
2d066 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d067 20 20 20 20 20 20 63 61 73 65 20 31 30 37 3a 20        case 107: 
2d068 2f 2a 20 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54  /* cmd ::= CREAT
2d069 45 20 74 65 6d 70 20 56 49 45 57 20 69 66 6e 6f  E temp VIEW ifno
2d06a 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20  texists nm dbnm 
2d06b 41 53 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20  AS select */.{. 
2d06c 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 56 69   sqlite3CreateVi
2d06d 65 77 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  ew(pParse, &yyms
2d06e 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-7].minor.yy0,
2d06f 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f   &yymsp[-3].mino
2d070 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 32  r.yy0, &yymsp[-2
2d071 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d  ].minor.yy0, yym
2d072 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35  sp[0].minor.yy15
2d073 39 2c 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  9, yymsp[-6].min
2d074 6f 72 2e 79 79 33 39 32 2c 20 79 79 6d 73 70 5b  or.yy392, yymsp[
2d075 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29  -4].minor.yy392)
2d076 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2d077 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
2d078 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52  8: /* cmd ::= DR
2d079 4f 50 20 56 49 45 57 20 69 66 65 78 69 73 74 73  OP VIEW ifexists
2d07a 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20   fullname */.{. 
2d07b 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c   sqlite3DropTabl
2d07c 65 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  e(pParse, yymsp[
2d07d 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 20  0].minor.yy347, 
2d07e 31 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  1, yymsp[-1].min
2d07f 6f 72 2e 79 79 33 39 32 29 3b 0a 7d 0a 20 20 20  or.yy392);.}.   
2d080 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d081 20 20 63 61 73 65 20 31 30 39 3a 20 2f 2a 20 63    case 109: /* c
2d082 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 20 2a 2f  md ::= select */
2d083 0a 7b 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  .{.  SelectDest 
2d084 64 65 73 74 20 3d 20 7b 53 52 54 5f 4f 75 74 70  dest = {SRT_Outp
2d085 75 74 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b  ut, 0, 0, 0, 0};
2d086 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
2d087 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30  (pParse, yymsp[0
2d088 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 2c 20 26  ].minor.yy159, &
2d089 64 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  dest);.  sqlite3
2d08a 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61  SelectDelete(pPa
2d08b 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 30  rse->db, yymsp[0
2d08c 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 29 3b 0a  ].minor.yy159);.
2d08d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d08e 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 30 3a  .      case 110:
2d08f 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a 3d 20 6f   /* select ::= o
2d090 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 79 79 67  neselect */.{yyg
2d091 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 35 39 20 3d  otominor.yy159 =
2d092 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2d093 79 79 31 35 39 3b 7d 0a 20 20 20 20 20 20 20 20  yy159;}.        
2d094 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d095 65 20 31 31 31 3a 20 2f 2a 20 73 65 6c 65 63 74  e 111: /* select
2d096 20 3a 3a 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74   ::= select mult
2d097 69 73 65 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65  iselect_op onese
2d098 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 69 66 28 20  lect */.{.  if( 
2d099 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2d09a 79 31 35 39 20 29 7b 0a 20 20 20 20 79 79 6d 73  y159 ){.    yyms
2d09b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39  p[0].minor.yy159
2d09c 2d 3e 6f 70 20 3d 20 28 75 38 29 79 79 6d 73 70  ->op = (u8)yymsp
2d09d 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32  [-1].minor.yy392
2d09e 3b 0a 20 20 20 20 79 79 6d 73 70 5b 30 5d 2e 6d  ;.    yymsp[0].m
2d09f 69 6e 6f 72 2e 79 79 31 35 39 2d 3e 70 50 72 69  inor.yy159->pPri
2d0a0 6f 72 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  or = yymsp[-2].m
2d0a1 69 6e 6f 72 2e 79 79 31 35 39 3b 0a 20 20 7d 65  inor.yy159;.  }e
2d0a2 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
2d0a3 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61  SelectDelete(pPa
2d0a4 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d  rse->db, yymsp[-
2d0a5 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 29 3b  2].minor.yy159);
2d0a6 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .  }.  yygotomin
2d0a7 6f 72 2e 79 79 31 35 39 20 3d 20 79 79 6d 73 70  or.yy159 = yymsp
2d0a8 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 3b  [0].minor.yy159;
2d0a9 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2d0aa 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 33  ;.      case 113
2d0ab 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74  : /* multiselect
2d0ac 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c  _op ::= UNION AL
2d0ad 4c 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  L */.{yygotomino
2d0ae 72 2e 79 79 33 39 32 20 3d 20 54 4b 5f 41 4c 4c  r.yy392 = TK_ALL
2d0af 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2d0b0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 35  ;.      case 115
2d0b1 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20 3a  : /* oneselect :
2d0b2 3a 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e  := SELECT distin
2d0b3 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72  ct selcollist fr
2d0b4 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f  om where_opt gro
2d0b5 75 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f  upby_opt having_
2d0b6 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20  opt orderby_opt 
2d0b7 6c 69 6d 69 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 20  limit_opt */.{. 
2d0b8 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2d0b9 35 39 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  59 = sqlite3Sele
2d0ba 63 74 4e 65 77 28 70 50 61 72 73 65 2c 79 79 6d  ctNew(pParse,yym
2d0bb 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-6].minor.yy4
2d0bc 34 32 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  42,yymsp[-5].min
2d0bd 6f 72 2e 79 79 33 34 37 2c 79 79 6d 73 70 5b 2d  or.yy347,yymsp[-
2d0be 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 79  4].minor.yy122,y
2d0bf 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
2d0c0 79 34 34 32 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y442,yymsp[-2].m
2d0c1 69 6e 6f 72 2e 79 79 31 32 32 2c 79 79 6d 73 70  inor.yy122,yymsp
2d0c2 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32  [-1].minor.yy442
2d0c3 2c 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72  ,yymsp[-7].minor
2d0c4 2e 79 79 33 39 32 2c 79 79 6d 73 70 5b 30 5d 2e  .yy392,yymsp[0].
2d0c5 6d 69 6e 6f 72 2e 79 79 36 34 2e 70 4c 69 6d 69  minor.yy64.pLimi
2d0c6 74 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  t,yymsp[0].minor
2d0c7 2e 79 79 36 34 2e 70 4f 66 66 73 65 74 29 3b 0a  .yy64.pOffset);.
2d0c8 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d0c9 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 39 3a  .      case 119:
2d0ca 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d 20 73 65 6c   /* sclp ::= sel
2d0cb 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 2a 2f  collist COMMA */
2d0cc 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 35 3a  .      case 245:
2d0cd 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   /* idxlist_opt 
2d0ce 3a 3a 3d 20 4c 50 20 69 64 78 6c 69 73 74 20 52  ::= LP idxlist R
2d0cf 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  P */.{yygotomino
2d0d0 72 2e 79 79 34 34 32 20 3d 20 79 79 6d 73 70 5b  r.yy442 = yymsp[
2d0d1 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 3b  -1].minor.yy442;
2d0d2 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d0d3 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 30 3a  .      case 120:
2d0d4 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d 20 2a 2f 0a   /* sclp ::= */.
2d0d5 20 20 20 20 20 20 63 61 73 65 20 31 34 38 3a 20        case 148: 
2d0d6 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a  /* orderby_opt :
2d0d7 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2d0d8 20 31 35 36 3a 20 2f 2a 20 67 72 6f 75 70 62 79   156: /* groupby
2d0d9 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
2d0da 20 20 63 61 73 65 20 32 33 38 3a 20 2f 2a 20 65    case 238: /* e
2d0db 78 70 72 6c 69 73 74 20 3a 3a 3d 20 2a 2f 0a 20  xprlist ::= */. 
2d0dc 20 20 20 20 20 63 61 73 65 20 32 34 34 3a 20 2f       case 244: /
2d0dd 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a  * idxlist_opt ::
2d0de 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
2d0df 72 2e 79 79 34 34 32 20 3d 20 30 3b 7d 0a 20 20  r.yy442 = 0;}.  
2d0e0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d0e1 20 20 20 63 61 73 65 20 31 32 31 3a 20 2f 2a 20     case 121: /* 
2d0e2 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73  selcollist ::= s
2d0e3 63 6c 70 20 65 78 70 72 20 61 73 20 2a 2f 0a 7b  clp expr as */.{
2d0e4 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  .   yygotominor.
2d0e5 79 79 34 34 32 20 3d 20 73 71 6c 69 74 65 33 45  yy442 = sqlite3E
2d0e6 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2d0e7 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  arse,yymsp[-2].m
2d0e8 69 6e 6f 72 2e 79 79 34 34 32 2c 79 79 6d 73 70  inor.yy442,yymsp
2d0e9 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  [-1].minor.yy122
2d0ea 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
2d0eb 79 79 30 2e 6e 3f 26 79 79 6d 73 70 5b 30 5d 2e  yy0.n?&yymsp[0].
2d0ec 6d 69 6e 6f 72 2e 79 79 30 3a 30 29 3b 0a 7d 0a  minor.yy0:0);.}.
2d0ed 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d0ee 20 20 20 20 20 63 61 73 65 20 31 32 32 3a 20 2f       case 122: /
2d0ef 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d  * selcollist ::=
2d0f0 20 73 63 6c 70 20 53 54 41 52 20 2a 2f 0a 7b 0a   sclp STAR */.{.
2d0f1 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69    Expr *p = sqli
2d0f2 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2d0f3 20 54 4b 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 30   TK_ALL, 0, 0, 0
2d0f4 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
2d0f5 2e 79 79 34 34 32 20 3d 20 73 71 6c 69 74 65 33  .yy442 = sqlite3
2d0f6 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2d0f7 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d  Parse, yymsp[-1]
2d0f8 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 20 70 2c  .minor.yy442, p,
2d0f9 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62   0);.}.        b
2d0fa 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d0fb 20 31 32 33 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c   123: /* selcoll
2d0fc 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20  ist ::= sclp nm 
2d0fd 44 4f 54 20 53 54 41 52 20 2a 2f 0a 7b 0a 20 20  DOT STAR */.{.  
2d0fe 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 73  Expr *pRight = s
2d0ff 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2d100 73 65 2c 20 54 4b 5f 41 4c 4c 2c 20 30 2c 20 30  se, TK_ALL, 0, 0
2d101 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2d102 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a  r.yy0);.  Expr *
2d103 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 50  pLeft = sqlite3P
2d104 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2d105 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70  ID, 0, 0, &yymsp
2d106 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-2].minor.yy0);
2d107 0a 20 20 45 78 70 72 20 2a 70 44 6f 74 20 3d 20  .  Expr *pDot = 
2d108 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2d109 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65  rse, TK_DOT, pLe
2d10a 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a  ft, pRight, 0);.
2d10b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2d10c 34 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  442 = sqlite3Exp
2d10d 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2d10e 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  se,yymsp[-3].min
2d10f 6f 72 2e 79 79 34 34 32 2c 20 70 44 6f 74 2c 20  or.yy442, pDot, 
2d110 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2d111 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2d112 31 32 36 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 2a  126: /* as ::= *
2d113 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2d114 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20 20  y0.n = 0;}.     
2d115 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2d116 63 61 73 65 20 31 32 37 3a 20 2f 2a 20 66 72 6f  case 127: /* fro
2d117 6d 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  m ::= */.{yygoto
2d118 6d 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20 73 71  minor.yy347 = sq
2d119 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2d11a 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69  o(pParse->db, si
2d11b 7a 65 6f 66 28 2a 79 79 67 6f 74 6f 6d 69 6e 6f  zeof(*yygotomino
2d11c 72 2e 79 79 33 34 37 29 29 3b 7d 0a 20 20 20 20  r.yy347));}.    
2d11d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d11e 20 63 61 73 65 20 31 32 38 3a 20 2f 2a 20 66 72   case 128: /* fr
2d11f 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c 74  om ::= FROM selt
2d120 61 62 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 79 79  ablist */.{.  yy
2d121 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 37 20  gotominor.yy347 
2d122 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
2d123 2e 79 79 33 34 37 3b 0a 20 20 73 71 6c 69 74 65  .yy347;.  sqlite
2d124 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69  3SrcListShiftJoi
2d125 6e 54 79 70 65 28 79 79 67 6f 74 6f 6d 69 6e 6f  nType(yygotomino
2d126 72 2e 79 79 33 34 37 29 3b 0a 7d 0a 20 20 20 20  r.yy347);.}.    
2d127 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d128 20 63 61 73 65 20 31 32 39 3a 20 2f 2a 20 73 74   case 129: /* st
2d129 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c  l_prefix ::= sel
2d12a 74 61 62 6c 69 73 74 20 6a 6f 69 6e 6f 70 20 2a  tablist joinop *
2d12b 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e  /.{.   yygotomin
2d12c 6f 72 2e 79 79 33 34 37 20 3d 20 79 79 6d 73 70  or.yy347 = yymsp
2d12d 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37  [-1].minor.yy347
2d12e 3b 0a 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  ;.   if( yygotom
2d12f 69 6e 6f 72 2e 79 79 33 34 37 20 26 26 20 79 79  inor.yy347 && yy
2d130 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 37 2d  gotominor.yy347-
2d131 3e 6e 53 72 63 3e 30 20 29 20 79 79 67 6f 74 6f  >nSrc>0 ) yygoto
2d132 6d 69 6e 6f 72 2e 79 79 33 34 37 2d 3e 61 5b 79  minor.yy347->a[y
2d133 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 37  ygotominor.yy347
2d134 2d 3e 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e 74 79  ->nSrc-1].jointy
2d135 70 65 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30  pe = (u8)yymsp[0
2d136 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 3b 0a 7d  ].minor.yy392;.}
2d137 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d138 20 20 20 20 20 20 63 61 73 65 20 31 33 30 3a 20        case 130: 
2d139 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a  /* stl_prefix ::
2d13a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
2d13b 72 2e 79 79 33 34 37 20 3d 20 30 3b 7d 0a 20 20  r.yy347 = 0;}.  
2d13c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d13d 20 20 20 63 61 73 65 20 31 33 31 3a 20 2f 2a 20     case 131: /* 
2d13e 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73  seltablist ::= s
2d13f 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64 62 6e  tl_prefix nm dbn
2d140 6d 20 61 73 20 69 6e 64 65 78 65 64 5f 6f 70 74  m as indexed_opt
2d141 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70   on_opt using_op
2d142 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  t */.{.  yygotom
2d143 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20 73 71 6c  inor.yy347 = sql
2d144 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
2d145 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65  dFromTerm(pParse
2d146 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  ,yymsp[-6].minor
2d147 2e 79 79 33 34 37 2c 26 79 79 6d 73 70 5b 2d 35  .yy347,&yymsp[-5
2d148 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2d149 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
2d14a 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
2d14b 72 2e 79 79 30 2c 30 2c 79 79 6d 73 70 5b 2d 31  r.yy0,0,yymsp[-1
2d14c 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 79 79  ].minor.yy122,yy
2d14d 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
2d14e 38 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72  80);.  sqlite3Sr
2d14f 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 70  cListIndexedBy(p
2d150 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
2d151 6f 72 2e 79 79 33 34 37 2c 20 26 79 79 6d 73 70  or.yy347, &yymsp
2d152 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-2].minor.yy0);
2d153 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2d154 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 32  ;.      case 132
2d155 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20  : /* seltablist 
2d156 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c  ::= stl_prefix L
2d157 50 20 73 65 6c 65 63 74 20 52 50 20 61 73 20 6f  P select RP as o
2d158 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20  n_opt using_opt 
2d159 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  */.{.    yygotom
2d15a 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20 73 71 6c  inor.yy347 = sql
2d15b 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
2d15c 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65  dFromTerm(pParse
2d15d 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  ,yymsp[-6].minor
2d15e 2e 79 79 33 34 37 2c 30 2c 30 2c 26 79 79 6d 73  .yy347,0,0,&yyms
2d15f 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
2d160 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2d161 79 79 31 35 39 2c 79 79 6d 73 70 5b 2d 31 5d 2e  yy159,yymsp[-1].
2d162 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 79 79 6d 73  minor.yy122,yyms
2d163 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 30  p[0].minor.yy180
2d164 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62  );.  }.        b
2d165 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d166 20 31 33 33 3a 20 2f 2a 20 73 65 6c 74 61 62 6c   133: /* seltabl
2d167 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66  ist ::= stl_pref
2d168 69 78 20 4c 50 20 73 65 6c 74 61 62 6c 69 73 74  ix LP seltablist
2d169 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73   RP as on_opt us
2d16a 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20  ing_opt */.{.   
2d16b 20 69 66 28 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d   if( yymsp[-6].m
2d16c 69 6e 6f 72 2e 79 79 33 34 37 3d 3d 30 20 26 26  inor.yy347==0 &&
2d16d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
2d16e 2e 79 79 30 2e 6e 3d 3d 30 20 26 26 20 79 79 6d  .yy0.n==0 && yym
2d16f 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2d170 32 32 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b 30  22==0 && yymsp[0
2d171 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 30 3d 3d 30  ].minor.yy180==0
2d172 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f   ){.      yygoto
2d173 6d 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20 79 79  minor.yy347 = yy
2d174 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2d175 33 34 37 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  347;.    }else{.
2d176 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53        Select *pS
2d177 75 62 71 75 65 72 79 3b 0a 20 20 20 20 20 20 73  ubquery;.      s
2d178 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69  qlite3SrcListShi
2d179 66 74 4a 6f 69 6e 54 79 70 65 28 79 79 6d 73 70  ftJoinType(yymsp
2d17a 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37  [-4].minor.yy347
2d17b 29 3b 0a 20 20 20 20 20 20 70 53 75 62 71 75 65  );.      pSubque
2d17c 72 79 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ry = sqlite3Sele
2d17d 63 74 4e 65 77 28 70 50 61 72 73 65 2c 30 2c 79  ctNew(pParse,0,y
2d17e 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2d17f 79 33 34 37 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30  y347,0,0,0,0,0,0
2d180 2c 30 29 3b 0a 20 20 20 20 20 20 79 79 67 6f 74  ,0);.      yygot
2d181 6f 6d 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20 73  ominor.yy347 = s
2d182 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
2d183 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72  endFromTerm(pPar
2d184 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  se,yymsp[-6].min
2d185 6f 72 2e 79 79 33 34 37 2c 30 2c 30 2c 26 79 79  or.yy347,0,0,&yy
2d186 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2d187 30 2c 70 53 75 62 71 75 65 72 79 2c 79 79 6d 73  0,pSubquery,yyms
2d188 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32  p[-1].minor.yy12
2d189 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  2,yymsp[0].minor
2d18a 2e 79 79 31 38 30 29 3b 0a 20 20 20 20 7d 0a 20  .yy180);.    }. 
2d18b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2d18c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 34  ;.      case 134
2d18d 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 2a 2f  : /* dbnm ::= */
2d18e 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 33 3a  .      case 143:
2d18f 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20   /* indexed_opt 
2d190 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
2d191 6e 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79 67  nor.yy0.z=0; yyg
2d192 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 30  otominor.yy0.n=0
2d193 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2d194 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 36  ;.      case 136
2d195 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 3a 3a  : /* fullname ::
2d196 3d 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 79 79  = nm dbnm */.{yy
2d197 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 37 20  gotominor.yy347 
2d198 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
2d199 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64  Append(pParse->d
2d19a 62 2c 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  b,0,&yymsp[-1].m
2d19b 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2d19c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
2d19d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d19e 20 20 20 20 20 63 61 73 65 20 31 33 37 3a 20 2f       case 137: /
2d19f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d  * joinop ::= COM
2d1a0 4d 41 7c 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67  MA|JOIN */.{ yyg
2d1a1 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d  otominor.yy392 =
2d1a2 20 4a 54 5f 49 4e 4e 45 52 3b 20 7d 0a 20 20 20   JT_INNER; }.   
2d1a3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d1a4 20 20 63 61 73 65 20 31 33 38 3a 20 2f 2a 20 6a    case 138: /* j
2d1a5 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  oinop ::= JOIN_K
2d1a6 57 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f  W JOIN */.{ yygo
2d1a7 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20  tominor.yy392 = 
2d1a8 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28  sqlite3JoinType(
2d1a9 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31  pParse,&yymsp[-1
2d1aa 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29  ].minor.yy0,0,0)
2d1ab 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
2d1ac 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
2d1ad 39 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d  9: /* joinop ::=
2d1ae 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e   JOIN_KW nm JOIN
2d1af 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2d1b0 72 2e 79 79 33 39 32 20 3d 20 73 71 6c 69 74 65  r.yy392 = sqlite
2d1b1 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65  3JoinType(pParse
2d1b2 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
2d1b3 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d  r.yy0,&yymsp[-1]
2d1b4 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 20 7d  .minor.yy0,0); }
2d1b5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d1b6 20 20 20 20 20 20 63 61 73 65 20 31 34 30 3a 20        case 140: 
2d1b7 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f  /* joinop ::= JO
2d1b8 49 4e 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e  IN_KW nm nm JOIN
2d1b9 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2d1ba 72 2e 79 79 33 39 32 20 3d 20 73 71 6c 69 74 65  r.yy392 = sqlite
2d1bb 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65  3JoinType(pParse
2d1bc 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
2d1bd 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d  r.yy0,&yymsp[-2]
2d1be 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2d1bf 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
2d1c0 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
2d1c1 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
2d1c2 31 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d  1: /* on_opt ::=
2d1c3 20 4f 4e 20 65 78 70 72 20 2a 2f 0a 20 20 20 20   ON expr */.    
2d1c4 20 20 63 61 73 65 20 31 35 32 3a 20 2f 2a 20 73    case 152: /* s
2d1c5 6f 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72  ortitem ::= expr
2d1c6 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2d1c7 35 39 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70  59: /* having_op
2d1c8 74 20 3a 3a 3d 20 48 41 56 49 4e 47 20 65 78 70  t ::= HAVING exp
2d1c9 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2d1ca 31 36 36 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70  166: /* where_op
2d1cb 74 20 3a 3a 3d 20 57 48 45 52 45 20 65 78 70 72  t ::= WHERE expr
2d1cc 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2d1cd 38 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  81: /* expr ::= 
2d1ce 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61  term */.      ca
2d1cf 73 65 20 32 30 39 3a 20 2f 2a 20 65 73 63 61 70  se 209: /* escap
2d1d0 65 20 3a 3a 3d 20 45 53 43 41 50 45 20 65 78 70  e ::= ESCAPE exp
2d1d1 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2d1d2 32 33 33 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73  233: /* case_els
2d1d3 65 20 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72 20  e ::= ELSE expr 
2d1d4 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  */.      case 23
2d1d5 35 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61  5: /* case_opera
2d1d6 6e 64 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 0a 7b  nd ::= expr */.{
2d1d7 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32  yygotominor.yy12
2d1d8 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  2 = yymsp[0].min
2d1d9 6f 72 2e 79 79 31 32 32 3b 7d 0a 20 20 20 20 20  or.yy122;}.     
2d1da 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2d1db 63 61 73 65 20 31 34 32 3a 20 2f 2a 20 6f 6e 5f  case 142: /* on_
2d1dc 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20  opt ::= */.     
2d1dd 20 63 61 73 65 20 31 35 38 3a 20 2f 2a 20 68 61   case 158: /* ha
2d1de 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  ving_opt ::= */.
2d1df 20 20 20 20 20 20 63 61 73 65 20 31 36 35 3a 20        case 165: 
2d1e0 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d  /* where_opt ::=
2d1e1 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2d1e2 31 30 3a 20 2f 2a 20 65 73 63 61 70 65 20 3a 3a  10: /* escape ::
2d1e3 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2d1e4 32 33 34 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73  234: /* case_els
2d1e5 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  e ::= */.      c
2d1e6 61 73 65 20 32 33 36 3a 20 2f 2a 20 63 61 73 65  ase 236: /* case
2d1e7 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 2a 2f 0a  _operand ::= */.
2d1e8 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
2d1e9 32 32 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  22 = 0;}.       
2d1ea 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2d1eb 73 65 20 31 34 35 3a 20 2f 2a 20 69 6e 64 65 78  se 145: /* index
2d1ec 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20 49  ed_opt ::= NOT I
2d1ed 4e 44 45 58 45 44 20 2a 2f 0a 7b 79 79 67 6f 74  NDEXED */.{yygot
2d1ee 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20  ominor.yy0.z=0; 
2d1ef 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
2d1f0 6e 3d 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  n=1;}.        br
2d1f1 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2d1f2 31 34 36 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70  146: /* using_op
2d1f3 74 20 3a 3a 3d 20 55 53 49 4e 47 20 4c 50 20 69  t ::= USING LP i
2d1f4 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20 2a 2f 0a  nscollist RP */.
2d1f5 20 20 20 20 20 20 63 61 73 65 20 31 37 38 3a 20        case 178: 
2d1f6 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  /* inscollist_op
2d1f7 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c  t ::= LP inscoll
2d1f8 69 73 74 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74  ist RP */.{yygot
2d1f9 6f 6d 69 6e 6f 72 2e 79 79 31 38 30 20 3d 20 79  ominor.yy180 = y
2d1fa 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2d1fb 79 31 38 30 3b 7d 0a 20 20 20 20 20 20 20 20 62  y180;}.        b
2d1fc 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d1fd 20 31 34 37 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f   147: /* using_o
2d1fe 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  pt ::= */.      
2d1ff 63 61 73 65 20 31 37 37 3a 20 2f 2a 20 69 6e 73  case 177: /* ins
2d200 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20  collist_opt ::= 
2d201 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2d202 79 79 31 38 30 20 3d 20 30 3b 7d 0a 20 20 20 20  yy180 = 0;}.    
2d203 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d204 20 63 61 73 65 20 31 34 39 3a 20 2f 2a 20 6f 72   case 149: /* or
2d205 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52  derby_opt ::= OR
2d206 44 45 52 20 42 59 20 73 6f 72 74 6c 69 73 74 20  DER BY sortlist 
2d207 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  */.      case 15
2d208 37 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70  7: /* groupby_op
2d209 74 20 3a 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e  t ::= GROUP BY n
2d20a 65 78 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20  exprlist */.    
2d20b 20 20 63 61 73 65 20 32 33 37 3a 20 2f 2a 20 65    case 237: /* e
2d20c 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70  xprlist ::= nexp
2d20d 72 6c 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f  rlist */.{yygoto
2d20e 6d 69 6e 6f 72 2e 79 79 34 34 32 20 3d 20 79 79  minor.yy442 = yy
2d20f 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
2d210 34 32 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  42;}.        bre
2d211 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2d212 35 30 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20  50: /* sortlist 
2d213 3a 3a 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f 4d  ::= sortlist COM
2d214 4d 41 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74  MA sortitem sort
2d215 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 79 79 67  order */.{.  yyg
2d216 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 20 3d  otominor.yy442 =
2d217 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2d218 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
2d219 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2d21a 34 34 32 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  442,yymsp[-1].mi
2d21b 6e 6f 72 2e 79 79 31 32 32 2c 30 29 3b 0a 20 20  nor.yy122,0);.  
2d21c 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
2d21d 79 79 34 34 32 20 29 20 79 79 67 6f 74 6f 6d 69  yy442 ) yygotomi
2d21e 6e 6f 72 2e 79 79 34 34 32 2d 3e 61 5b 79 79 67  nor.yy442->a[yyg
2d21f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 2d 3e  otominor.yy442->
2d220 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64  nExpr-1].sortOrd
2d221 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30  er = (u8)yymsp[0
2d222 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 3b 0a 7d  ].minor.yy392;.}
2d223 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d224 20 20 20 20 20 20 63 61 73 65 20 31 35 31 3a 20        case 151: 
2d225 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20  /* sortlist ::= 
2d226 73 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64  sortitem sortord
2d227 65 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  er */.{.  yygoto
2d228 6d 69 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73 71  minor.yy442 = sq
2d229 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2d22a 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 79 79 6d  end(pParse,0,yym
2d22b 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2d22c 32 32 2c 30 29 3b 0a 20 20 69 66 28 20 79 79 67  22,0);.  if( yyg
2d22d 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 20 26  otominor.yy442 &
2d22e 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  & yygotominor.yy
2d22f 34 34 32 2d 3e 61 20 29 20 79 79 67 6f 74 6f 6d  442->a ) yygotom
2d230 69 6e 6f 72 2e 79 79 34 34 32 2d 3e 61 5b 30 5d  inor.yy442->a[0]
2d231 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  .sortOrder = (u8
2d232 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  )yymsp[0].minor.
2d233 79 79 33 39 32 3b 0a 7d 0a 20 20 20 20 20 20 20  yy392;.}.       
2d234 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2d235 73 65 20 31 35 33 3a 20 2f 2a 20 73 6f 72 74 6f  se 153: /* sorto
2d236 72 64 65 72 20 3a 3a 3d 20 41 53 43 20 2a 2f 0a  rder ::= ASC */.
2d237 20 20 20 20 20 20 63 61 73 65 20 31 35 35 3a 20        case 155: 
2d238 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d  /* sortorder ::=
2d239 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2d23a 2e 79 79 33 39 32 20 3d 20 53 51 4c 49 54 45 5f  .yy392 = SQLITE_
2d23b 53 4f 5f 41 53 43 3b 7d 0a 20 20 20 20 20 20 20  SO_ASC;}.       
2d23c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2d23d 73 65 20 31 35 34 3a 20 2f 2a 20 73 6f 72 74 6f  se 154: /* sorto
2d23e 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 20 2a 2f  rder ::= DESC */
2d23f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2d240 33 39 32 20 3d 20 53 51 4c 49 54 45 5f 53 4f 5f  392 = SQLITE_SO_
2d241 44 45 53 43 3b 7d 0a 20 20 20 20 20 20 20 20 62  DESC;}.        b
2d242 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d243 20 31 36 30 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   160: /* limit_o
2d244 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  pt ::= */.{yygot
2d245 6f 6d 69 6e 6f 72 2e 79 79 36 34 2e 70 4c 69 6d  ominor.yy64.pLim
2d246 69 74 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69  it = 0; yygotomi
2d247 6e 6f 72 2e 79 79 36 34 2e 70 4f 66 66 73 65 74  nor.yy64.pOffset
2d248 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
2d249 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d24a 20 31 36 31 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   161: /* limit_o
2d24b 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
2d24c 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  r */.{yygotomino
2d24d 72 2e 79 79 36 34 2e 70 4c 69 6d 69 74 20 3d 20  r.yy64.pLimit = 
2d24e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2d24f 79 31 32 32 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  y122; yygotomino
2d250 72 2e 79 79 36 34 2e 70 4f 66 66 73 65 74 20 3d  r.yy64.pOffset =
2d251 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
2d252 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2d253 36 32 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74  62: /* limit_opt
2d254 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20   ::= LIMIT expr 
2d255 4f 46 46 53 45 54 20 65 78 70 72 20 2a 2f 0a 7b  OFFSET expr */.{
2d256 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 34  yygotominor.yy64
2d257 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b  .pLimit = yymsp[
2d258 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 3b  -2].minor.yy122;
2d259 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36   yygotominor.yy6
2d25a 34 2e 70 4f 66 66 73 65 74 20 3d 20 79 79 6d 73  4.pOffset = yyms
2d25b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  p[0].minor.yy122
2d25c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2d25d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 33  ;.      case 163
2d25e 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a  : /* limit_opt :
2d25f 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 43 4f  := LIMIT expr CO
2d260 4d 4d 41 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67  MMA expr */.{yyg
2d261 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 34 2e 70 4f  otominor.yy64.pO
2d262 66 66 73 65 74 20 3d 20 79 79 6d 73 70 5b 2d 32  ffset = yymsp[-2
2d263 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 3b 20 79  ].minor.yy122; y
2d264 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 34 2e  ygotominor.yy64.
2d265 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 30  pLimit = yymsp[0
2d266 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 3b 7d 0a  ].minor.yy122;}.
2d267 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d268 20 20 20 20 20 63 61 73 65 20 31 36 34 3a 20 2f       case 164: /
2d269 2a 20 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45  * cmd ::= DELETE
2d26a 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20 69   FROM fullname i
2d26b 6e 64 65 78 65 64 5f 6f 70 74 20 77 68 65 72 65  ndexed_opt where
2d26c 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  _opt */.{.  sqli
2d26d 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65  te3SrcListIndexe
2d26e 64 42 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73  dBy(pParse, yyms
2d26f 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
2d270 37 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  7, &yymsp[-1].mi
2d271 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69  nor.yy0);.  sqli
2d272 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50  te3DeleteFrom(pP
2d273 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  arse,yymsp[-2].m
2d274 69 6e 6f 72 2e 79 79 33 34 37 2c 79 79 6d 73 70  inor.yy347,yymsp
2d275 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 29  [0].minor.yy122)
2d276 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2d277 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36  k;.      case 16
2d278 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 55 50  7: /* cmd ::= UP
2d279 44 41 54 45 20 6f 72 63 6f 6e 66 20 66 75 6c 6c  DATE orconf full
2d27a 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74  name indexed_opt
2d27b 20 53 45 54 20 73 65 74 6c 69 73 74 20 77 68 65   SET setlist whe
2d27c 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71  re_opt */.{.  sq
2d27d 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65  lite3SrcListInde
2d27e 78 65 64 42 79 28 70 50 61 72 73 65 2c 20 79 79  xedBy(pParse, yy
2d27f 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2d280 33 34 37 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e  347, &yymsp[-3].
2d281 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71  minor.yy0);.  sq
2d282 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65  lite3ExprListChe
2d283 63 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c  ckLength(pParse,
2d284 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2d285 79 79 34 34 32 2c 22 73 65 74 20 6c 69 73 74 22  yy442,"set list"
2d286 29 3b 20 0a 20 20 73 71 6c 69 74 65 33 55 70 64  ); .  sqlite3Upd
2d287 61 74 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70  ate(pParse,yymsp
2d288 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37  [-4].minor.yy347
2d289 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2d28a 2e 79 79 34 34 32 2c 79 79 6d 73 70 5b 30 5d 2e  .yy442,yymsp[0].
2d28b 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 79 79 6d 73  minor.yy122,yyms
2d28c 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39  p[-5].minor.yy39
2d28d 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  2);.}.        br
2d28e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2d28f 31 36 38 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20  168: /* setlist 
2d290 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d  ::= setlist COMM
2d291 41 20 6e 6d 20 45 51 20 65 78 70 72 20 2a 2f 0a  A nm EQ expr */.
2d292 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
2d293 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  42 = sqlite3Expr
2d294 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2d295 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  e,yymsp[-4].mino
2d296 72 2e 79 79 34 34 32 2c 79 79 6d 73 70 5b 30 5d  r.yy442,yymsp[0]
2d297 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 26 79 79  .minor.yy122,&yy
2d298 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2d299 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2d29a 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2d29b 36 39 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20 3a  69: /* setlist :
2d29c 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72 20 2a 2f  := nm EQ expr */
2d29d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2d29e 34 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  442 = sqlite3Exp
2d29f 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2d2a0 73 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  se,0,yymsp[0].mi
2d2a1 6e 6f 72 2e 79 79 31 32 32 2c 26 79 79 6d 73 70  nor.yy122,&yymsp
2d2a2 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-2].minor.yy0);
2d2a3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d2a4 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 30 3a  .      case 170:
2d2a5 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65   /* cmd ::= inse
2d2a6 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
2d2a7 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
2d2a8 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74  opt VALUES LP it
2d2a9 65 6d 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 73 71  emlist RP */.{sq
2d2aa 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72  lite3Insert(pPar
2d2ab 73 65 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  se, yymsp[-5].mi
2d2ac 6e 6f 72 2e 79 79 33 34 37 2c 20 79 79 6d 73 70  nor.yy347, yymsp
2d2ad 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32  [-1].minor.yy442
2d2ae 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  , 0, yymsp[-4].m
2d2af 69 6e 6f 72 2e 79 79 31 38 30 2c 20 79 79 6d 73  inor.yy180, yyms
2d2b0 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39  p[-7].minor.yy39
2d2b1 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  2);}.        bre
2d2b2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2d2b3 37 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69  71: /* cmd ::= i
2d2b4 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66  nsert_cmd INTO f
2d2b5 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69  ullname inscolli
2d2b6 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 20 2a 2f  st_opt select */
2d2b7 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28  .{sqlite3Insert(
2d2b8 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32  pParse, yymsp[-2
2d2b9 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 20 30  ].minor.yy347, 0
2d2ba 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2d2bb 2e 79 79 31 35 39 2c 20 79 79 6d 73 70 5b 2d 31  .yy159, yymsp[-1
2d2bc 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 30 2c 20 79  ].minor.yy180, y
2d2bd 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2d2be 79 33 39 32 29 3b 7d 0a 20 20 20 20 20 20 20 20  y392);}.        
2d2bf 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d2c0 65 20 31 37 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 172: /* cmd ::
2d2c1 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
2d2c2 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f  O fullname insco
2d2c3 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46 41 55 4c  llist_opt DEFAUL
2d2c4 54 20 56 41 4c 55 45 53 20 2a 2f 0a 7b 73 71 6c  T VALUES */.{sql
2d2c5 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73  ite3Insert(pPars
2d2c6 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  e, yymsp[-3].min
2d2c7 6f 72 2e 79 79 33 34 37 2c 20 30 2c 20 30 2c 20  or.yy347, 0, 0, 
2d2c8 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2d2c9 79 79 31 38 30 2c 20 79 79 6d 73 70 5b 2d 35 5d  yy180, yymsp[-5]
2d2ca 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29 3b 7d 0a  .minor.yy392);}.
2d2cb 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d2cc 20 20 20 20 20 63 61 73 65 20 31 37 35 3a 20 2f       case 175: /
2d2cd 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69  * itemlist ::= i
2d2ce 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78  temlist COMMA ex
2d2cf 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
2d2d0 20 32 33 39 3a 20 2f 2a 20 6e 65 78 70 72 6c 69   239: /* nexprli
2d2d1 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74  st ::= nexprlist
2d2d2 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a 7b   COMMA expr */.{
2d2d3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34  yygotominor.yy44
2d2d4 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  2 = sqlite3ExprL
2d2d5 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2d2d6 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
2d2d7 2e 79 79 34 34 32 2c 79 79 6d 73 70 5b 30 5d 2e  .yy442,yymsp[0].
2d2d8 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 30 29 3b 7d  minor.yy122,0);}
2d2d9 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d2da 20 20 20 20 20 20 63 61 73 65 20 31 37 36 3a 20        case 176: 
2d2db 2f 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20  /* itemlist ::= 
2d2dc 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
2d2dd 73 65 20 32 34 30 3a 20 2f 2a 20 6e 65 78 70 72  se 240: /* nexpr
2d2de 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a 2f  list ::= expr */
2d2df 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2d2e0 34 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  442 = sqlite3Exp
2d2e1 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2d2e2 73 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  se,0,yymsp[0].mi
2d2e3 6e 6f 72 2e 79 79 31 32 32 2c 30 29 3b 7d 0a 20  nor.yy122,0);}. 
2d2e4 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2d2e5 20 20 20 20 63 61 73 65 20 31 37 39 3a 20 2f 2a      case 179: /*
2d2e6 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20   inscollist ::= 
2d2e7 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41  inscollist COMMA
2d2e8 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69   nm */.{yygotomi
2d2e9 6e 6f 72 2e 79 79 31 38 30 20 3d 20 73 71 6c 69  nor.yy180 = sqli
2d2ea 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28  te3IdListAppend(
2d2eb 70 50 61 72 73 65 2d 3e 64 62 2c 79 79 6d 73 70  pParse->db,yymsp
2d2ec 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 30  [-2].minor.yy180
2d2ed 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2d2ee 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
2d2ef 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d2f0 65 20 31 38 30 3a 20 2f 2a 20 69 6e 73 63 6f 6c  e 180: /* inscol
2d2f1 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b  list ::= nm */.{
2d2f2 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38  yygotominor.yy18
2d2f3 30 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73  0 = sqlite3IdLis
2d2f4 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
2d2f5 64 62 2c 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  db,0,&yymsp[0].m
2d2f6 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
2d2f7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d2f8 20 63 61 73 65 20 31 38 32 3a 20 2f 2a 20 65 78   case 182: /* ex
2d2f9 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52  pr ::= LP expr R
2d2fa 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  P */.{yygotomino
2d2fb 72 2e 79 79 31 32 32 20 3d 20 79 79 6d 73 70 5b  r.yy122 = yymsp[
2d2fc 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 3b  -1].minor.yy122;
2d2fd 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
2d2fe 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  (yygotominor.yy1
2d2ff 32 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  22,&yymsp[-2].mi
2d300 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
2d301 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a  ].minor.yy0); }.
2d302 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d303 20 20 20 20 20 63 61 73 65 20 31 38 33 3a 20 2f       case 183: /
2d304 2a 20 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 20  * term ::= NULL 
2d305 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38  */.      case 18
2d306 38 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 49  8: /* term ::= I
2d307 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f  NTEGER|FLOAT|BLO
2d308 42 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  B */.      case 
2d309 31 38 39 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d  189: /* term ::=
2d30a 20 53 54 52 49 4e 47 20 2a 2f 0a 7b 79 79 67 6f   STRING */.{yygo
2d30b 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20  tominor.yy122 = 
2d30c 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2d30d 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 61  rse, yymsp[0].ma
2d30e 6a 6f 72 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  jor, 0, 0, &yyms
2d30f 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2d310 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d311 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 34 3a  .      case 184:
2d312 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20   /* expr ::= ID 
2d313 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38  */.      case 18
2d314 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4a  5: /* expr ::= J
2d315 4f 49 4e 5f 4b 57 20 2a 2f 0a 7b 79 79 67 6f 74  OIN_KW */.{yygot
2d316 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73  ominor.yy122 = s
2d317 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2d318 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
2d319 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
2d31a 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
2d31b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d31c 65 20 31 38 36 3a 20 2f 2a 20 65 78 70 72 20 3a  e 186: /* expr :
2d31d 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a  := nm DOT nm */.
2d31e 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 31 20  {.  Expr *temp1 
2d31f 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2d320 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
2d321 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d   0, &yymsp[-2].m
2d322 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70  inor.yy0);.  Exp
2d323 72 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69 74  r *temp2 = sqlit
2d324 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2d325 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
2d326 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2d327 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
2d328 2e 79 79 31 32 32 20 3d 20 73 71 6c 69 74 65 33  .yy122 = sqlite3
2d329 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2d32a 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65 6d  _DOT, temp1, tem
2d32b 70 32 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20  p2, 0);.}.      
2d32c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2d32d 61 73 65 20 31 38 37 3a 20 2f 2a 20 65 78 70 72  ase 187: /* expr
2d32e 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44   ::= nm DOT nm D
2d32f 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70  OT nm */.{.  Exp
2d330 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74  r *temp1 = sqlit
2d331 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2d332 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
2d333 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2d334 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  0);.  Expr *temp
2d335 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2d336 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
2d337 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
2d338 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
2d339 78 70 72 20 2a 74 65 6d 70 33 20 3d 20 73 71 6c  xpr *temp3 = sql
2d33a 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2d33b 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
2d33c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2d33d 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d  y0);.  Expr *tem
2d33e 70 34 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  p4 = sqlite3PExp
2d33f 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
2d340 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70 33 2c 20  , temp2, temp3, 
2d341 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
2d342 72 2e 79 79 31 32 32 20 3d 20 73 71 6c 69 74 65  r.yy122 = sqlite
2d343 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2d344 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65  K_DOT, temp1, te
2d345 6d 70 34 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20  mp4, 0);.}.     
2d346 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2d347 63 61 73 65 20 31 39 30 3a 20 2f 2a 20 65 78 70  case 190: /* exp
2d348 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52 20 2a  r ::= REGISTER *
2d349 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2d34a 79 31 32 32 20 3d 20 73 71 6c 69 74 65 33 52 65  y122 = sqlite3Re
2d34b 67 69 73 74 65 72 45 78 70 72 28 70 50 61 72 73  gisterExpr(pPars
2d34c 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e, &yymsp[0].min
2d34d 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
2d34e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2d34f 61 73 65 20 31 39 31 3a 20 2f 2a 20 65 78 70 72  ase 191: /* expr
2d350 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 20 2a 2f   ::= VARIABLE */
2d351 0a 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  .{.  Token *pTok
2d352 65 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  en = &yymsp[0].m
2d353 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 45 78 70 72  inor.yy0;.  Expr
2d354 20 2a 70 45 78 70 72 20 3d 20 79 79 67 6f 74 6f   *pExpr = yygoto
2d355 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73 71  minor.yy122 = sq
2d356 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2d357 65 2c 20 54 4b 5f 56 41 52 49 41 42 4c 45 2c 20  e, TK_VARIABLE, 
2d358 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20  0, 0, pToken);. 
2d359 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 69   sqlite3ExprAssi
2d35a 67 6e 56 61 72 4e 75 6d 62 65 72 28 70 50 61 72  gnVarNumber(pPar
2d35b 73 65 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 20 20  se, pExpr);.}.  
2d35c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d35d 20 20 20 63 61 73 65 20 31 39 32 3a 20 2f 2a 20     case 192: /* 
2d35e 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f  expr ::= expr CO
2d35f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b 0a 20  LLATE ids */.{. 
2d360 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2d361 32 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  22 = sqlite3Expr
2d362 53 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20  SetColl(pParse, 
2d363 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2d364 79 79 31 32 32 2c 20 26 79 79 6d 73 70 5b 30 5d  yy122, &yymsp[0]
2d365 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
2d366 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2d367 20 20 20 20 63 61 73 65 20 31 39 33 3a 20 2f 2a      case 193: /*
2d368 20 65 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c   expr ::= CAST L
2d369 50 20 65 78 70 72 20 41 53 20 74 79 70 65 74 6f  P expr AS typeto
2d36a 6b 65 6e 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79  ken RP */.{.  yy
2d36b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20  gotominor.yy122 
2d36c 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2d36d 50 61 72 73 65 2c 20 54 4b 5f 43 41 53 54 2c 20  Parse, TK_CAST, 
2d36e 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2d36f 79 79 31 32 32 2c 20 30 2c 20 26 79 79 6d 73 70  yy122, 0, &yymsp
2d370 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
2d371 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70  .  sqlite3ExprSp
2d372 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2d373 79 31 32 32 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e  y122,&yymsp[-5].
2d374 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2d375 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2d376 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d377 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 34 3a  .      case 194:
2d378 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20   /* expr ::= ID 
2d379 4c 50 20 64 69 73 74 69 6e 63 74 20 65 78 70 72  LP distinct expr
2d37a 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 69  list RP */.{.  i
2d37b 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  f( yymsp[-1].min
2d37c 6f 72 2e 79 79 34 34 32 20 26 26 20 79 79 6d 73  or.yy442 && yyms
2d37d 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34  p[-1].minor.yy44
2d37e 32 2d 3e 6e 45 78 70 72 3e 53 51 4c 49 54 45 5f  2->nExpr>SQLITE_
2d37f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MAX_FUNCTION_ARG
2d380 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2d381 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2d382 22 74 6f 6f 20 6d 61 6e 79 20 61 72 67 75 6d 65  "too many argume
2d383 6e 74 73 20 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  nts on function 
2d384 25 54 22 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e  %T", &yymsp[-4].
2d385 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a  minor.yy0);.  }.
2d386 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2d387 31 32 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  122 = sqlite3Exp
2d388 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65  rFunction(pParse
2d389 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2d38a 72 2e 79 79 34 34 32 2c 20 26 79 79 6d 73 70 5b  r.yy442, &yymsp[
2d38b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -4].minor.yy0);.
2d38c 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2d38d 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2d38e 31 32 32 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  122,&yymsp[-4].m
2d38f 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2d390 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
2d391 20 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d   if( yymsp[-2].m
2d392 69 6e 6f 72 2e 79 79 33 39 32 20 26 26 20 79 79  inor.yy392 && yy
2d393 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20  gotominor.yy122 
2d394 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ){.    yygotomin
2d395 6f 72 2e 79 79 31 32 32 2d 3e 66 6c 61 67 73 20  or.yy122->flags 
2d396 7c 3d 20 45 50 5f 44 69 73 74 69 6e 63 74 3b 0a  |= EP_Distinct;.
2d397 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72    }.}.        br
2d398 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2d399 31 39 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  195: /* expr ::=
2d39a 20 49 44 20 4c 50 20 53 54 41 52 20 52 50 20 2a   ID LP STAR RP *
2d39b 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2d39c 72 2e 79 79 31 32 32 20 3d 20 73 71 6c 69 74 65  r.yy122 = sqlite
2d39d 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50  3ExprFunction(pP
2d39e 61 72 73 65 2c 20 30 2c 20 26 79 79 6d 73 70 5b  arse, 0, &yymsp[
2d39f 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -3].minor.yy0);.
2d3a0 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2d3a1 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2d3a2 31 32 32 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  122,&yymsp[-3].m
2d3a3 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2d3a4 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
2d3a5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d3a6 20 20 20 20 20 20 63 61 73 65 20 31 39 36 3a 20        case 196: 
2d3a7 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 43 54 49 4d  /* term ::= CTIM
2d3a8 45 5f 4b 57 20 2a 2f 0a 7b 0a 20 20 2f 2a 20 54  E_KW */.{.  /* T
2d3a9 68 65 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 2c  he CURRENT_TIME,
2d3aa 20 43 55 52 52 45 4e 54 5f 44 41 54 45 2c 20 61   CURRENT_DATE, a
2d3ab 6e 64 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 53  nd CURRENT_TIMES
2d3ac 54 41 4d 50 20 76 61 6c 75 65 73 20 61 72 65 0a  TAMP values are.
2d3ad 20 20 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20    ** treated as 
2d3ae 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 72  functions that r
2d3af 65 74 75 72 6e 20 63 6f 6e 73 74 61 6e 74 73 20  eturn constants 
2d3b0 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.  yygotominor
2d3b1 2e 79 79 31 32 32 20 3d 20 73 71 6c 69 74 65 33  .yy122 = sqlite3
2d3b2 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61  ExprFunction(pPa
2d3b3 72 73 65 2c 20 30 2c 26 79 79 6d 73 70 5b 30 5d  rse, 0,&yymsp[0]
2d3b4 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69  .minor.yy0);.  i
2d3b5 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2d3b6 79 31 32 32 20 29 7b 0a 20 20 20 20 79 79 67 6f  y122 ){.    yygo
2d3b7 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 6f  tominor.yy122->o
2d3b8 70 20 3d 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e  p = TK_CONST_FUN
2d3b9 43 3b 20 20 0a 20 20 20 20 79 79 67 6f 74 6f 6d  C;  .    yygotom
2d3ba 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 73 70 61 6e  inor.yy122->span
2d3bb 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
2d3bc 72 2e 79 79 30 3b 0a 20 20 7d 0a 7d 0a 20 20 20  r.yy0;.  }.}.   
2d3bd 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d3be 20 20 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 65    case 197: /* e
2d3bf 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44  xpr ::= expr AND
2d3c0 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
2d3c1 61 73 65 20 31 39 38 3a 20 2f 2a 20 65 78 70 72  ase 198: /* expr
2d3c2 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70   ::= expr OR exp
2d3c3 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2d3c4 31 39 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  199: /* expr ::=
2d3c5 20 65 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c 4c   expr LT|GT|GE|L
2d3c6 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  E expr */.      
2d3c7 63 61 73 65 20 32 30 30 3a 20 2f 2a 20 65 78 70  case 200: /* exp
2d3c8 72 20 3a 3a 3d 20 65 78 70 72 20 45 51 7c 4e 45  r ::= expr EQ|NE
2d3c9 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
2d3ca 61 73 65 20 32 30 31 3a 20 2f 2a 20 65 78 70 72  ase 201: /* expr
2d3cb 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e 44   ::= expr BITAND
2d3cc 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53  |BITOR|LSHIFT|RS
2d3cd 48 49 46 54 20 65 78 70 72 20 2a 2f 0a 20 20 20  HIFT expr */.   
2d3ce 20 20 20 63 61 73 65 20 32 30 32 3a 20 2f 2a 20     case 202: /* 
2d3cf 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c  expr ::= expr PL
2d3d0 55 53 7c 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f  US|MINUS expr */
2d3d1 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 33 3a  .      case 203:
2d3d2 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
2d3d3 72 20 53 54 41 52 7c 53 4c 41 53 48 7c 52 45 4d  r STAR|SLASH|REM
2d3d4 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
2d3d5 61 73 65 20 32 30 34 3a 20 2f 2a 20 65 78 70 72  ase 204: /* expr
2d3d6 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41 54   ::= expr CONCAT
2d3d7 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f   expr */.{yygoto
2d3d8 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73 71  minor.yy122 = sq
2d3d9 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2d3da 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f  e,yymsp[-1].majo
2d3db 72 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  r,yymsp[-2].mino
2d3dc 72 2e 79 79 31 32 32 2c 79 79 6d 73 70 5b 30 5d  r.yy122,yymsp[0]
2d3dd 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 30 29 3b  .minor.yy122,0);
2d3de 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d3df 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 35 3a  .      case 205:
2d3e0 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c   /* likeop ::= L
2d3e1 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  IKE_KW */.      
2d3e2 63 61 73 65 20 32 30 37 3a 20 2f 2a 20 6c 69 6b  case 207: /* lik
2d3e3 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 20 2a 2f  eop ::= MATCH */
2d3e4 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2d3e5 33 31 38 2e 65 4f 70 65 72 61 74 6f 72 20 3d 20  318.eOperator = 
2d3e6 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2d3e7 79 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  y0; yygotominor.
2d3e8 79 79 33 31 38 2e 6e 6f 74 20 3d 20 30 3b 7d 0a  yy318.not = 0;}.
2d3e9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d3ea 20 20 20 20 20 63 61 73 65 20 32 30 36 3a 20 2f       case 206: /
2d3eb 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54  * likeop ::= NOT
2d3ec 20 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20   LIKE_KW */.    
2d3ed 20 20 63 61 73 65 20 32 30 38 3a 20 2f 2a 20 6c    case 208: /* l
2d3ee 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41  ikeop ::= NOT MA
2d3ef 54 43 48 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  TCH */.{yygotomi
2d3f0 6e 6f 72 2e 79 79 33 31 38 2e 65 4f 70 65 72 61  nor.yy318.eOpera
2d3f1 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  tor = yymsp[0].m
2d3f2 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f  inor.yy0; yygoto
2d3f3 6d 69 6e 6f 72 2e 79 79 33 31 38 2e 6e 6f 74 20  minor.yy318.not 
2d3f4 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 1;}.        br
2d3f5 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2d3f6 32 31 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  211: /* expr ::=
2d3f7 20 65 78 70 72 20 6c 69 6b 65 6f 70 20 65 78 70   expr likeop exp
2d3f8 72 20 65 73 63 61 70 65 20 2a 2f 0a 7b 0a 20 20  r escape */.{.  
2d3f9 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b  ExprList *pList;
2d3fa 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  .  pList = sqlit
2d3fb 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2d3fc 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70  (pParse,0, yymsp
2d3fd 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  [-1].minor.yy122
2d3fe 2c 20 30 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20  , 0);.  pList = 
2d3ff 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2d400 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69  ppend(pParse,pLi
2d401 73 74 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  st, yymsp[-3].mi
2d402 6e 6f 72 2e 79 79 31 32 32 2c 20 30 29 3b 0a 20  nor.yy122, 0);. 
2d403 20 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69   if( yymsp[0].mi
2d404 6e 6f 72 2e 79 79 31 32 32 20 29 7b 0a 20 20 20  nor.yy122 ){.   
2d405 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
2d406 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2d407 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79 6d  Parse,pList, yym
2d408 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32  sp[0].minor.yy12
2d409 32 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67  2, 0);.  }.  yyg
2d40a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d  otominor.yy122 =
2d40b 20 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63   sqlite3ExprFunc
2d40c 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 4c 69  tion(pParse, pLi
2d40d 73 74 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  st, &yymsp[-2].m
2d40e 69 6e 6f 72 2e 79 79 33 31 38 2e 65 4f 70 65 72  inor.yy318.eOper
2d40f 61 74 6f 72 29 3b 0a 20 20 69 66 28 20 79 79 6d  ator);.  if( yym
2d410 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
2d411 31 38 2e 6e 6f 74 20 29 20 79 79 67 6f 74 6f 6d  18.not ) yygotom
2d412 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73 71 6c  inor.yy122 = sql
2d413 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2d414 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f  , TK_NOT, yygoto
2d415 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 20 30 2c 20  minor.yy122, 0, 
2d416 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
2d417 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2d418 72 2e 79 79 31 32 32 2c 20 26 79 79 6d 73 70 5b  r.yy122, &yymsp[
2d419 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2d  -3].minor.yy122-
2d41a 3e 73 70 61 6e 2c 20 26 79 79 6d 73 70 5b 2d 31  >span, &yymsp[-1
2d41b 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 73  ].minor.yy122->s
2d41c 70 61 6e 29 3b 0a 20 20 69 66 28 20 79 79 67 6f  pan);.  if( yygo
2d41d 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 29 20  tominor.yy122 ) 
2d41e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32  yygotominor.yy12
2d41f 32 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49  2->flags |= EP_I
2d420 6e 66 69 78 46 75 6e 63 3b 0a 7d 0a 20 20 20 20  nfixFunc;.}.    
2d421 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d422 20 63 61 73 65 20 32 31 32 3a 20 2f 2a 20 65 78   case 212: /* ex
2d423 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e 55  pr ::= expr ISNU
2d424 4c 4c 7c 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a 7b 0a  LL|NOTNULL */.{.
2d425 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2d426 31 32 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78  122 = sqlite3PEx
2d427 70 72 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  pr(pParse, yymsp
2d428 5b 30 5d 2e 6d 61 6a 6f 72 2c 20 79 79 6d 73 70  [0].major, yymsp
2d429 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  [-1].minor.yy122
2d42a 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
2d42b 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2d42c 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 26 79 79  ominor.yy122,&yy
2d42d 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2d42e 31 32 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70  122->span,&yymsp
2d42f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2d430 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d431 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 33 3a  .      case 213:
2d432 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
2d433 72 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20  r IS NULL */.{. 
2d434 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2d435 32 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  22 = sqlite3PExp
2d436 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 53 4e  r(pParse, TK_ISN
2d437 55 4c 4c 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ULL, yymsp[-2].m
2d438 69 6e 6f 72 2e 79 79 31 32 32 2c 20 30 2c 20 30  inor.yy122, 0, 0
2d439 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2d43a 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2d43b 2e 79 79 31 32 32 2c 26 79 79 6d 73 70 5b 2d 32  .yy122,&yymsp[-2
2d43c 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 73  ].minor.yy122->s
2d43d 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  pan,&yymsp[0].mi
2d43e 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
2d43f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d440 20 63 61 73 65 20 32 31 34 3a 20 2f 2a 20 65 78   case 214: /* ex
2d441 70 72 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20  pr ::= expr NOT 
2d442 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  NULL */.{.  yygo
2d443 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20  tominor.yy122 = 
2d444 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2d445 72 73 65 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c  rse, TK_NOTNULL,
2d446 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
2d447 2e 79 79 31 32 32 2c 20 30 2c 20 30 29 3b 0a 20  .yy122, 0, 0);. 
2d448 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
2d449 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  (yygotominor.yy1
2d44a 32 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  22,&yymsp[-2].mi
2d44b 6e 6f 72 2e 79 79 31 32 32 2d 3e 73 70 61 6e 2c  nor.yy122->span,
2d44c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2d44d 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
2d44e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d44f 65 20 32 31 35 3a 20 2f 2a 20 65 78 70 72 20 3a  e 215: /* expr :
2d450 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f 54 20 4e  := expr IS NOT N
2d451 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  ULL */.{.  yygot
2d452 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73  ominor.yy122 = s
2d453 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2d454 73 65 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20  se, TK_NOTNULL, 
2d455 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2d456 79 79 31 32 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy122, 0, 0);.  
2d457 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
2d458 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32  yygotominor.yy12
2d459 32 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  2,&yymsp[-3].min
2d45a 6f 72 2e 79 79 31 32 32 2d 3e 73 70 61 6e 2c 26  or.yy122->span,&
2d45b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2d45c 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
2d45d 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d45e 20 32 31 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   216: /* expr ::
2d45f 3d 20 4e 4f 54 20 65 78 70 72 20 2a 2f 0a 20 20  = NOT expr */.  
2d460 20 20 20 20 63 61 73 65 20 32 31 37 3a 20 2f 2a      case 217: /*
2d461 20 65 78 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54   expr ::= BITNOT
2d462 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67   expr */.{.  yyg
2d463 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d  otominor.yy122 =
2d464 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2d465 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  arse, yymsp[-1].
2d466 6d 61 6a 6f 72 2c 20 79 79 6d 73 70 5b 30 5d 2e  major, yymsp[0].
2d467 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 20 30 2c 20  minor.yy122, 0, 
2d468 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
2d469 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2d46a 72 2e 79 79 31 32 32 2c 26 79 79 6d 73 70 5b 2d  r.yy122,&yymsp[-
2d46b 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  1].minor.yy0,&yy
2d46c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
2d46d 32 32 2d 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20 20  22->span);.}.   
2d46e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d46f 20 20 63 61 73 65 20 32 31 38 3a 20 2f 2a 20 65    case 218: /* e
2d470 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78  xpr ::= MINUS ex
2d471 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pr */.{.  yygoto
2d472 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73 71  minor.yy122 = sq
2d473 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2d474 65 2c 20 54 4b 5f 55 4d 49 4e 55 53 2c 20 79 79  e, TK_UMINUS, yy
2d475 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
2d476 32 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c  22, 0, 0);.  sql
2d477 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67  ite3ExprSpan(yyg
2d478 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 26  otominor.yy122,&
2d479 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2d47a 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
2d47b 6e 6f 72 2e 79 79 31 32 32 2d 3e 73 70 61 6e 29  nor.yy122->span)
2d47c 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2d47d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  k;.      case 21
2d47e 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 50  9: /* expr ::= P
2d47f 4c 55 53 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20  LUS expr */.{.  
2d480 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32  yygotominor.yy12
2d481 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2d482 28 70 50 61 72 73 65 2c 20 54 4b 5f 55 50 4c 55  (pParse, TK_UPLU
2d483 53 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  S, yymsp[0].mino
2d484 72 2e 79 79 31 32 32 2c 20 30 2c 20 30 29 3b 0a  r.yy122, 0, 0);.
2d485 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2d486 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2d487 31 32 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  122,&yymsp[-1].m
2d488 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2d489 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2d 3e  0].minor.yy122->
2d48a 73 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20  span);.}.       
2d48b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2d48c 73 65 20 32 32 32 3a 20 2f 2a 20 65 78 70 72 20  se 222: /* expr 
2d48d 3a 3a 3d 20 65 78 70 72 20 62 65 74 77 65 65 6e  ::= expr between
2d48e 5f 6f 70 20 65 78 70 72 20 41 4e 44 20 65 78 70  _op expr AND exp
2d48f 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c 69 73  r */.{.  ExprLis
2d490 74 20 2a 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  t *pList = sqlit
2d491 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2d492 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70  (pParse,0, yymsp
2d493 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  [-2].minor.yy122
2d494 2c 20 30 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20  , 0);.  pList = 
2d495 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2d496 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69  ppend(pParse,pLi
2d497 73 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  st, yymsp[0].min
2d498 6f 72 2e 79 79 31 32 32 2c 20 30 29 3b 0a 20 20  or.yy122, 0);.  
2d499 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32  yygotominor.yy12
2d49a 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2d49b 28 70 50 61 72 73 65 2c 20 54 4b 5f 42 45 54 57  (pParse, TK_BETW
2d49c 45 45 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  EEN, yymsp[-4].m
2d49d 69 6e 6f 72 2e 79 79 31 32 32 2c 20 30 2c 20 30  inor.yy122, 0, 0
2d49e 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  );.  if( yygotom
2d49f 69 6e 6f 72 2e 79 79 31 32 32 20 29 7b 0a 20 20  inor.yy122 ){.  
2d4a0 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2d4a1 31 32 32 2d 3e 70 4c 69 73 74 20 3d 20 70 4c 69  122->pList = pLi
2d4a2 73 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  st;.  }else{.   
2d4a3 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2d4a4 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
2d4a5 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 7d 20 0a  b, pList);.  } .
2d4a6 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e    if( yymsp[-3].
2d4a7 6d 69 6e 6f 72 2e 79 79 33 39 32 20 29 20 79 79  minor.yy392 ) yy
2d4a8 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20  gotominor.yy122 
2d4a9 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2d4aa 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79  Parse, TK_NOT, y
2d4ab 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32  ygotominor.yy122
2d4ac 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
2d4ad 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2d4ae 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 26 79 79  ominor.yy122,&yy
2d4af 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2d4b0 31 32 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70  122->span,&yymsp
2d4b1 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2d  [0].minor.yy122-
2d4b2 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20  >span);.}.      
2d4b3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2d4b4 61 73 65 20 32 32 35 3a 20 2f 2a 20 65 78 70 72  ase 225: /* expr
2d4b5 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20   ::= expr in_op 
2d4b6 4c 50 20 65 78 70 72 6c 69 73 74 20 52 50 20 2a  LP exprlist RP *
2d4b7 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  /.{.    yygotomi
2d4b8 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73 71 6c 69  nor.yy122 = sqli
2d4b9 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2d4ba 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34   TK_IN, yymsp[-4
2d4bb 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 20 30  ].minor.yy122, 0
2d4bc 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79  , 0);.    if( yy
2d4bd 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20  gotominor.yy122 
2d4be 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d  ){.      yygotom
2d4bf 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 70 4c 69 73  inor.yy122->pLis
2d4c0 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  t = yymsp[-1].mi
2d4c1 6e 6f 72 2e 79 79 34 34 32 3b 0a 20 20 20 20 20  nor.yy442;.     
2d4c2 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
2d4c3 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79  eight(pParse, yy
2d4c4 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 29  gotominor.yy122)
2d4c5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2d4c6 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2d4c7 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
2d4c8 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  >db, yymsp[-1].m
2d4c9 69 6e 6f 72 2e 79 79 34 34 32 29 3b 0a 20 20 20  inor.yy442);.   
2d4ca 20 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70   }.    if( yymsp
2d4cb 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32  [-3].minor.yy392
2d4cc 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
2d4cd 79 31 32 32 20 3d 20 73 71 6c 69 74 65 33 50 45  y122 = sqlite3PE
2d4ce 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
2d4cf 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  OT, yygotominor.
2d4d0 79 79 31 32 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy122, 0, 0);.  
2d4d1 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2d4d2 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2d4d3 31 32 32 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  122,&yymsp[-4].m
2d4d4 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 73 70 61 6e  inor.yy122->span
2d4d5 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2d4d6 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20  .yy0);.  }.     
2d4d7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2d4d8 63 61 73 65 20 32 32 36 3a 20 2f 2a 20 65 78 70  case 226: /* exp
2d4d9 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65 63 74 20  r ::= LP select 
2d4da 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f  RP */.{.    yygo
2d4db 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20  tominor.yy122 = 
2d4dc 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2d4dd 72 73 65 2c 20 54 4b 5f 53 45 4c 45 43 54 2c 20  rse, TK_SELECT, 
2d4de 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 0, 0);.    if
2d4df 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2d4e0 31 32 32 20 29 7b 0a 20 20 20 20 20 20 79 79 67  122 ){.      yyg
2d4e1 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 2d 3e  otominor.yy122->
2d4e2 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b  pSelect = yymsp[
2d4e3 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 3b  -1].minor.yy159;
2d4e4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2d4e5 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72  prSetHeight(pPar
2d4e6 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
2d4e7 79 79 31 32 32 29 3b 0a 20 20 20 20 7d 65 6c 73  yy122);.    }els
2d4e8 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
2d4e9 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61  SelectDelete(pPa
2d4ea 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d  rse->db, yymsp[-
2d4eb 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 29 3b  1].minor.yy159);
2d4ec 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2d4ed 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2d4ee 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 26 79 79  ominor.yy122,&yy
2d4ef 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2d4f0 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
2d4f1 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20  r.yy0);.  }.    
2d4f2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d4f3 20 63 61 73 65 20 32 32 37 3a 20 2f 2a 20 65 78   case 227: /* ex
2d4f4 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f  pr ::= expr in_o
2d4f5 70 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a  p LP select RP *
2d4f6 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  /.{.    yygotomi
2d4f7 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73 71 6c 69  nor.yy122 = sqli
2d4f8 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2d4f9 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34   TK_IN, yymsp[-4
2d4fa 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 20 30  ].minor.yy122, 0
2d4fb 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79  , 0);.    if( yy
2d4fc 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20  gotominor.yy122 
2d4fd 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d  ){.      yygotom
2d4fe 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 70 53 65 6c  inor.yy122->pSel
2d4ff 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  ect = yymsp[-1].
2d500 6d 69 6e 6f 72 2e 79 79 31 35 39 3b 0a 20 20 20  minor.yy159;.   
2d501 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65     sqlite3ExprSe
2d502 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  tHeight(pParse, 
2d503 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32  yygotominor.yy12
2d504 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  2);.    }else{. 
2d505 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
2d506 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  ctDelete(pParse-
2d507 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  >db, yymsp[-1].m
2d508 69 6e 6f 72 2e 79 79 31 35 39 29 3b 0a 20 20 20  inor.yy159);.   
2d509 20 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70   }.    if( yymsp
2d50a 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32  [-3].minor.yy392
2d50b 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
2d50c 79 31 32 32 20 3d 20 73 71 6c 69 74 65 33 50 45  y122 = sqlite3PE
2d50d 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
2d50e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  OT, yygotominor.
2d50f 79 79 31 32 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy122, 0, 0);.  
2d510 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2d511 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2d512 31 32 32 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  122,&yymsp[-4].m
2d513 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 73 70 61 6e  inor.yy122->span
2d514 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2d515 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20  .yy0);.  }.     
2d516 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2d517 63 61 73 65 20 32 32 38 3a 20 2f 2a 20 65 78 70  case 228: /* exp
2d518 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70  r ::= expr in_op
2d519 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20   nm dbnm */.{.  
2d51a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20    SrcList *pSrc 
2d51b 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
2d51c 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64  Append(pParse->d
2d51d 62 2c 20 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  b, 0,&yymsp[-1].
2d51e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2d51f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2d520 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
2d521 79 79 31 32 32 20 3d 20 73 71 6c 69 74 65 33 50  yy122 = sqlite3P
2d522 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2d523 49 4e 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  IN, yymsp[-3].mi
2d524 6e 6f 72 2e 79 79 31 32 32 2c 20 30 2c 20 30 29  nor.yy122, 0, 0)
2d525 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f  ;.    if( yygoto
2d526 6d 69 6e 6f 72 2e 79 79 31 32 32 20 29 7b 0a 20  minor.yy122 ){. 
2d527 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
2d528 2e 79 79 31 32 32 2d 3e 70 53 65 6c 65 63 74 20  .yy122->pSelect 
2d529 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e  = sqlite3SelectN
2d52a 65 77 28 70 50 61 72 73 65 2c 20 30 2c 70 53 72  ew(pParse, 0,pSr
2d52b 63 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 29  c,0,0,0,0,0,0,0)
2d52c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2d52d 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61  xprSetHeight(pPa
2d52e 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  rse, yygotominor
2d52f 2e 79 79 31 32 32 29 3b 0a 20 20 20 20 7d 65 6c  .yy122);.    }el
2d530 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2d531 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70  3SrcListDelete(p
2d532 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 29  Parse->db, pSrc)
2d533 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2d534 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2d535 79 79 33 39 32 20 29 20 79 79 67 6f 74 6f 6d 69  yy392 ) yygotomi
2d536 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73 71 6c 69  nor.yy122 = sqli
2d537 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2d538 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d   TK_NOT, yygotom
2d539 69 6e 6f 72 2e 79 79 31 32 32 2c 20 30 2c 20 30  inor.yy122, 0, 0
2d53a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
2d53b 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e  prSpan(yygotomin
2d53c 6f 72 2e 79 79 31 32 32 2c 26 79 79 6d 73 70 5b  or.yy122,&yymsp[
2d53d 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2d  -3].minor.yy122-
2d53e 3e 73 70 61 6e 2c 79 79 6d 73 70 5b 30 5d 2e 6d  >span,yymsp[0].m
2d53f 69 6e 6f 72 2e 79 79 30 2e 7a 3f 26 79 79 6d 73  inor.yy0.z?&yyms
2d540 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a 26  p[0].minor.yy0:&
2d541 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2d542 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20  yy0);.  }.      
2d543 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2d544 61 73 65 20 32 32 39 3a 20 2f 2a 20 65 78 70 72  ase 229: /* expr
2d545 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20 73   ::= EXISTS LP s
2d546 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  elect RP */.{.  
2d547 20 20 45 78 70 72 20 2a 70 20 3d 20 79 79 67 6f    Expr *p = yygo
2d548 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20  tominor.yy122 = 
2d549 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2d54a 72 73 65 2c 20 54 4b 5f 45 58 49 53 54 53 2c 20  rse, TK_EXISTS, 
2d54b 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 0, 0);.    if
2d54c 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ( p ){.      p->
2d54d 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b  pSelect = yymsp[
2d54e 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 3b  -1].minor.yy159;
2d54f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2d550 70 72 53 70 61 6e 28 70 2c 26 79 79 6d 73 70 5b  prSpan(p,&yymsp[
2d551 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -3].minor.yy0,&y
2d552 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2d553 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
2d554 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70  3ExprSetHeight(p
2d555 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
2d556 6f 72 2e 79 79 31 32 32 29 3b 0a 20 20 20 20 7d  or.yy122);.    }
2d557 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
2d558 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2d559 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
2d55a 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35  p[-1].minor.yy15
2d55b 39 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  9);.    }.  }.  
2d55c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d55d 20 20 20 63 61 73 65 20 32 33 30 3a 20 2f 2a 20     case 230: /* 
2d55e 65 78 70 72 20 3a 3a 3d 20 43 41 53 45 20 63 61  expr ::= CASE ca
2d55f 73 65 5f 6f 70 65 72 61 6e 64 20 63 61 73 65 5f  se_operand case_
2d560 65 78 70 72 6c 69 73 74 20 63 61 73 65 5f 65 6c  exprlist case_el
2d561 73 65 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20 79 79  se END */.{.  yy
2d562 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20  gotominor.yy122 
2d563 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2d564 50 61 72 73 65 2c 20 54 4b 5f 43 41 53 45 2c 20  Parse, TK_CASE, 
2d565 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2d566 79 79 31 32 32 2c 20 79 79 6d 73 70 5b 2d 31 5d  yy122, yymsp[-1]
2d567 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 20 30 29  .minor.yy122, 0)
2d568 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  ;.  if( yygotomi
2d569 6e 6f 72 2e 79 79 31 32 32 20 29 7b 0a 20 20 20  nor.yy122 ){.   
2d56a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2d56b 32 32 2d 3e 70 4c 69 73 74 20 3d 20 79 79 6d 73  22->pList = yyms
2d56c 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34  p[-2].minor.yy44
2d56d 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  2;.    sqlite3Ex
2d56e 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72  prSetHeight(pPar
2d56f 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
2d570 79 79 31 32 32 29 3b 0a 20 20 7d 65 6c 73 65 7b  yy122);.  }else{
2d571 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2d572 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
2d573 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 32 5d  e->db, yymsp[-2]
2d574 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 29 3b 0a 20  .minor.yy442);. 
2d575 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
2d576 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2d577 2e 79 79 31 32 32 2c 20 26 79 79 6d 73 70 5b 2d  .yy122, &yymsp[-
2d578 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  4].minor.yy0, &y
2d579 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2d57a 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2d57b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2d57c 32 33 31 3a 20 2f 2a 20 63 61 73 65 5f 65 78 70  231: /* case_exp
2d57d 72 6c 69 73 74 20 3a 3a 3d 20 63 61 73 65 5f 65  rlist ::= case_e
2d57e 78 70 72 6c 69 73 74 20 57 48 45 4e 20 65 78 70  xprlist WHEN exp
2d57f 72 20 54 48 45 4e 20 65 78 70 72 20 2a 2f 0a 7b  r THEN expr */.{
2d580 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2d581 79 34 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78  y442 = sqlite3Ex
2d582 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2d583 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  rse,yymsp[-4].mi
2d584 6e 6f 72 2e 79 79 34 34 32 2c 20 79 79 6d 73 70  nor.yy442, yymsp
2d585 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  [-2].minor.yy122
2d586 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  , 0);.  yygotomi
2d587 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73 71 6c 69  nor.yy442 = sqli
2d588 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2d589 64 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d  d(pParse,yygotom
2d58a 69 6e 6f 72 2e 79 79 34 34 32 2c 20 79 79 6d 73  inor.yy442, yyms
2d58b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  p[0].minor.yy122
2d58c 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  , 0);.}.        
2d58d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d58e 65 20 32 33 32 3a 20 2f 2a 20 63 61 73 65 5f 65  e 232: /* case_e
2d58f 78 70 72 6c 69 73 74 20 3a 3a 3d 20 57 48 45 4e  xprlist ::= WHEN
2d590 20 65 78 70 72 20 54 48 45 4e 20 65 78 70 72 20   expr THEN expr 
2d591 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2d592 6f 72 2e 79 79 34 34 32 20 3d 20 73 71 6c 69 74  or.yy442 = sqlit
2d593 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2d594 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70  (pParse,0, yymsp
2d595 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  [-2].minor.yy122
2d596 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  , 0);.  yygotomi
2d597 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73 71 6c 69  nor.yy442 = sqli
2d598 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2d599 64 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d  d(pParse,yygotom
2d59a 69 6e 6f 72 2e 79 79 34 34 32 2c 20 79 79 6d 73  inor.yy442, yyms
2d59b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  p[0].minor.yy122
2d59c 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  , 0);.}.        
2d59d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d59e 65 20 32 34 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 241: /* cmd ::
2d59f 3d 20 43 52 45 41 54 45 20 75 6e 69 71 75 65 66  = CREATE uniquef
2d5a0 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f 74 65  lag INDEX ifnote
2d5a1 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e  xists nm dbnm ON
2d5a2 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20 52   nm LP idxlist R
2d5a3 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  P */.{.  sqlite3
2d5a4 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72  CreateIndex(pPar
2d5a5 73 65 2c 20 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d  se, &yymsp[-6].m
2d5a6 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
2d5a7 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-5].minor.yy0, 
2d5a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2d5a9 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63        sqlite3Src
2d5aa 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2d5ab 65 2d 3e 64 62 2c 30 2c 26 79 79 6d 73 70 5b 2d  e->db,0,&yymsp[-
2d5ac 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 2c  3].minor.yy0,0),
2d5ad 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2d5ae 2e 79 79 34 34 32 2c 20 79 79 6d 73 70 5b 2d 39  .yy442, yymsp[-9
2d5af 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 2c 0a 20  ].minor.yy392,. 
2d5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d5b1 20 20 20 20 20 26 79 79 6d 73 70 5b 2d 31 30 5d       &yymsp[-10]
2d5b2 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
2d5b3 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
2d5b4 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 2c 20   SQLITE_SO_ASC, 
2d5b5 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e  yymsp[-7].minor.
2d5b6 79 79 33 39 32 29 3b 0a 7d 0a 20 20 20 20 20 20  yy392);.}.      
2d5b7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2d5b8 61 73 65 20 32 34 32 3a 20 2f 2a 20 75 6e 69 71  ase 242: /* uniq
2d5b9 75 65 66 6c 61 67 20 3a 3a 3d 20 55 4e 49 51 55  ueflag ::= UNIQU
2d5ba 45 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  E */.      case 
2d5bb 32 38 39 3a 20 2f 2a 20 72 61 69 73 65 74 79 70  289: /* raisetyp
2d5bc 65 20 3a 3a 3d 20 41 42 4f 52 54 20 2a 2f 0a 7b  e ::= ABORT */.{
2d5bd 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39  yygotominor.yy39
2d5be 32 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 7d 0a 20  2 = OE_Abort;}. 
2d5bf 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2d5c0 20 20 20 20 63 61 73 65 20 32 34 33 3a 20 2f 2a      case 243: /*
2d5c1 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20   uniqueflag ::= 
2d5c2 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2d5c3 79 79 33 39 32 20 3d 20 4f 45 5f 4e 6f 6e 65 3b  yy392 = OE_None;
2d5c4 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d5c5 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 36 3a  .      case 246:
2d5c6 20 2f 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20   /* idxlist ::= 
2d5c7 69 64 78 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d  idxlist COMMA nm
2d5c8 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64   collate sortord
2d5c9 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a  er */.{.  Expr *
2d5ca 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d  p = 0;.  if( yym
2d5cb 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2d5cc 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20  .n>0 ){.    p = 
2d5cd 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2d5ce 72 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20  rse, TK_COLUMN, 
2d5cf 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  0, 0, 0);.    sq
2d5d0 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c  lite3ExprSetColl
2d5d1 28 70 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d  (pParse, p, &yym
2d5d2 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2d5d3 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  );.  }.  yygotom
2d5d4 69 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73 71 6c  inor.yy442 = sql
2d5d5 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
2d5d6 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  nd(pParse,yymsp[
2d5d7 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c  -4].minor.yy442,
2d5d8 20 70 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d   p, &yymsp[-2].m
2d5d9 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c  inor.yy0);.  sql
2d5da 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63  ite3ExprListChec
2d5db 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20  kLength(pParse, 
2d5dc 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34  yygotominor.yy44
2d5dd 32 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20 20 69  2, "index");.  i
2d5de 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2d5df 79 34 34 32 20 29 20 79 79 67 6f 74 6f 6d 69 6e  y442 ) yygotomin
2d5e0 6f 72 2e 79 79 34 34 32 2d 3e 61 5b 79 79 67 6f  or.yy442->a[yygo
2d5e1 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 2d 3e 6e  tominor.yy442->n
2d5e2 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65  Expr-1].sortOrde
2d5e3 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d  r = (u8)yymsp[0]
2d5e4 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 3b 0a 7d 0a  .minor.yy392;.}.
2d5e5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d5e6 20 20 20 20 20 63 61 73 65 20 32 34 37 3a 20 2f       case 247: /
2d5e7 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20 6e 6d  * idxlist ::= nm
2d5e8 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64   collate sortord
2d5e9 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a  er */.{.  Expr *
2d5ea 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d  p = 0;.  if( yym
2d5eb 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2d5ec 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20  .n>0 ){.    p = 
2d5ed 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2d5ee 72 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20  rse, TK_COLUMN, 
2d5ef 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  0, 0, 0);.    sq
2d5f0 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c  lite3ExprSetColl
2d5f1 28 70 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d  (pParse, p, &yym
2d5f2 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2d5f3 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  );.  }.  yygotom
2d5f4 69 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73 71 6c  inor.yy442 = sql
2d5f5 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
2d5f6 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 70 2c 20  nd(pParse,0, p, 
2d5f7 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
2d5f8 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  .yy0);.  sqlite3
2d5f9 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e  ExprListCheckLen
2d5fa 67 74 68 28 70 50 61 72 73 65 2c 20 79 79 67 6f  gth(pParse, yygo
2d5fb 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 20 22  tominor.yy442, "
2d5fc 69 6e 64 65 78 22 29 3b 0a 20 20 69 66 28 20 79  index");.  if( y
2d5fd 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32  ygotominor.yy442
2d5fe 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
2d5ff 79 34 34 32 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69  y442->a[yygotomi
2d600 6e 6f 72 2e 79 79 34 34 32 2d 3e 6e 45 78 70 72  nor.yy442->nExpr
2d601 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20  -1].sortOrder = 
2d602 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  (u8)yymsp[0].min
2d603 6f 72 2e 79 79 33 39 32 3b 0a 7d 0a 20 20 20 20  or.yy392;.}.    
2d604 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d605 20 63 61 73 65 20 32 34 38 3a 20 2f 2a 20 63 6f   case 248: /* co
2d606 6c 6c 61 74 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79  llate ::= */.{yy
2d607 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20  gotominor.yy0.z 
2d608 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  = 0; yygotominor
2d609 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20  .yy0.n = 0;}.   
2d60a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d60b 20 20 63 61 73 65 20 32 35 30 3a 20 2f 2a 20 63    case 250: /* c
2d60c 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45  md ::= DROP INDE
2d60d 58 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  X ifexists fulln
2d60e 61 6d 65 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 44  ame */.{sqlite3D
2d60f 72 6f 70 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ropIndex(pParse,
2d610 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2d611 79 79 33 34 37 2c 20 79 79 6d 73 70 5b 2d 31 5d  yy347, yymsp[-1]
2d612 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29 3b 7d 0a  .minor.yy392);}.
2d613 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d614 20 20 20 20 20 63 61 73 65 20 32 35 31 3a 20 2f       case 251: /
2d615 2a 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d  * cmd ::= VACUUM
2d616 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2d617 35 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56  52: /* cmd ::= V
2d618 41 43 55 55 4d 20 6e 6d 20 2a 2f 0a 7b 73 71 6c  ACUUM nm */.{sql
2d619 69 74 65 33 56 61 63 75 75 6d 28 70 50 61 72 73  ite3Vacuum(pPars
2d61a 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  e);}.        bre
2d61b 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2d61c 35 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50  53: /* cmd ::= P
2d61d 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
2d61e 20 6e 6d 6e 75 6d 20 2a 2f 0a 20 20 20 20 20 20   nmnum */.      
2d61f 63 61 73 65 20 32 35 34 3a 20 2f 2a 20 63 6d 64  case 254: /* cmd
2d620 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
2d621 62 6e 6d 20 45 51 20 4f 4e 20 2a 2f 0a 20 20 20  bnm EQ ON */.   
2d622 20 20 20 63 61 73 65 20 32 35 35 3a 20 2f 2a 20     case 255: /* 
2d623 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
2d624 6d 20 64 62 6e 6d 20 45 51 20 44 45 4c 45 54 45  m dbnm EQ DELETE
2d625 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67   */.{sqlite3Prag
2d626 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  ma(pParse,&yymsp
2d627 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-3].minor.yy0,&
2d628 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2d629 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
2d62a 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20 20 20  nor.yy0,0);}.   
2d62b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d62c 20 20 63 61 73 65 20 32 35 36 3a 20 2f 2a 20 63    case 256: /* c
2d62d 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
2d62e 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73 5f 6e   dbnm EQ minus_n
2d62f 75 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  um */.{.  sqlite
2d630 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26  3Pragma(pParse,&
2d631 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2d632 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  yy0,&yymsp[-2].m
2d633 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2d634 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b  0].minor.yy0,1);
2d635 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2d636 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 37  ;.      case 257
2d637 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41  : /* cmd ::= PRA
2d638 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e  GMA nm dbnm LP n
2d639 6d 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73 71 6c 69  mnum RP */.{sqli
2d63a 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65  te3Pragma(pParse
2d63b 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  ,&yymsp[-4].mino
2d63c 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d  r.yy0,&yymsp[-3]
2d63d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2d63e 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
2d63f 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2d640 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2d641 35 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50  58: /* cmd ::= P
2d642 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 2a 2f  RAGMA nm dbnm */
2d643 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28  .{sqlite3Pragma(
2d644 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31  pParse,&yymsp[-1
2d645 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2d646 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
2d647 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  0,0);}.        b
2d648 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d649 20 32 36 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   266: /* cmd ::=
2d64a 20 43 52 45 41 54 45 20 74 72 69 67 67 65 72 5f   CREATE trigger_
2d64b 64 65 63 6c 20 42 45 47 49 4e 20 74 72 69 67 67  decl BEGIN trigg
2d64c 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e 44 20  er_cmd_list END 
2d64d 2a 2f 0a 7b 0a 20 20 54 6f 6b 65 6e 20 61 6c 6c  */.{.  Token all
2d64e 3b 0a 20 20 61 6c 6c 2e 7a 20 3d 20 79 79 6d 73  ;.  all.z = yyms
2d64f 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-3].minor.yy0.
2d650 7a 3b 0a 20 20 61 6c 6c 2e 6e 20 3d 20 28 69 6e  z;.  all.n = (in
2d651 74 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  t)(yymsp[0].mino
2d652 72 2e 79 79 30 2e 7a 20 2d 20 79 79 6d 73 70 5b  r.yy0.z - yymsp[
2d653 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29  -3].minor.yy0.z)
2d654 20 2b 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   + yymsp[0].mino
2d655 72 2e 79 79 30 2e 6e 3b 0a 20 20 73 71 6c 69 74  r.yy0.n;.  sqlit
2d656 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28  e3FinishTrigger(
2d657 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31  pParse, yymsp[-1
2d658 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37 2c 20 26  ].minor.yy327, &
2d659 61 6c 6c 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  all);.}.        
2d65a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d65b 65 20 32 36 37 3a 20 2f 2a 20 74 72 69 67 67 65  e 267: /* trigge
2d65c 72 5f 64 65 63 6c 20 3a 3a 3d 20 74 65 6d 70 20  r_decl ::= temp 
2d65d 54 52 49 47 47 45 52 20 69 66 6e 6f 74 65 78 69  TRIGGER ifnotexi
2d65e 73 74 73 20 6e 6d 20 64 62 6e 6d 20 74 72 69 67  sts nm dbnm trig
2d65f 67 65 72 5f 74 69 6d 65 20 74 72 69 67 67 65 72  ger_time trigger
2d660 5f 65 76 65 6e 74 20 4f 4e 20 66 75 6c 6c 6e 61  _event ON fullna
2d661 6d 65 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73  me foreach_claus
2d662 65 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 2a 2f  e when_clause */
2d663 0a 7b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  .{.  sqlite3Begi
2d664 6e 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  nTrigger(pParse,
2d665 20 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f   &yymsp[-7].mino
2d666 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 36  r.yy0, &yymsp[-6
2d667 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d  ].minor.yy0, yym
2d668 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-5].minor.yy3
2d669 39 32 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  92, yymsp[-4].mi
2d66a 6e 6f 72 2e 79 79 34 31 30 2e 61 2c 20 79 79 6d  nor.yy410.a, yym
2d66b 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-4].minor.yy4
2d66c 31 30 2e 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  10.b, yymsp[-2].
2d66d 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 20 79 79 6d  minor.yy347, yym
2d66e 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32  sp[0].minor.yy12
2d66f 32 2c 20 79 79 6d 73 70 5b 2d 31 30 5d 2e 6d 69  2, yymsp[-10].mi
2d670 6e 6f 72 2e 79 79 33 39 32 2c 20 79 79 6d 73 70  nor.yy392, yymsp
2d671 5b 2d 38 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32  [-8].minor.yy392
2d672 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
2d673 2e 79 79 30 20 3d 20 28 79 79 6d 73 70 5b 2d 36  .yy0 = (yymsp[-6
2d674 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 3d 30  ].minor.yy0.n==0
2d675 3f 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72  ?yymsp[-7].minor
2d676 2e 79 79 30 3a 79 79 6d 73 70 5b 2d 36 5d 2e 6d  .yy0:yymsp[-6].m
2d677 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
2d678 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d679 20 20 63 61 73 65 20 32 36 38 3a 20 2f 2a 20 74    case 268: /* t
2d67a 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20  rigger_time ::= 
2d67b 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20  BEFORE */.      
2d67c 63 61 73 65 20 32 37 31 3a 20 2f 2a 20 74 72 69  case 271: /* tri
2d67d 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 2a 2f  gger_time ::= */
2d67e 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
2d67f 79 33 39 32 20 3d 20 54 4b 5f 42 45 46 4f 52 45  y392 = TK_BEFORE
2d680 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
2d681 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  k;.      case 26
2d682 39 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69  9: /* trigger_ti
2d683 6d 65 20 3a 3a 3d 20 41 46 54 45 52 20 2a 2f 0a  me ::= AFTER */.
2d684 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
2d685 33 39 32 20 3d 20 54 4b 5f 41 46 54 45 52 3b 20  392 = TK_AFTER; 
2d686 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2d687 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 30  ;.      case 270
2d688 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d  : /* trigger_tim
2d689 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46  e ::= INSTEAD OF
2d68a 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2d68b 72 2e 79 79 33 39 32 20 3d 20 54 4b 5f 49 4e 53  r.yy392 = TK_INS
2d68c 54 45 41 44 3b 7d 0a 20 20 20 20 20 20 20 20 62  TEAD;}.        b
2d68d 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d68e 20 32 37 32 3a 20 2f 2a 20 74 72 69 67 67 65 72   272: /* trigger
2d68f 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45 54  _event ::= DELET
2d690 45 7c 49 4e 53 45 52 54 20 2a 2f 0a 20 20 20 20  E|INSERT */.    
2d691 20 20 63 61 73 65 20 32 37 33 3a 20 2f 2a 20 74    case 273: /* t
2d692 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
2d693 20 55 50 44 41 54 45 20 2a 2f 0a 7b 79 79 67 6f   UPDATE */.{yygo
2d694 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 61 20  tominor.yy410.a 
2d695 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72  = yymsp[0].major
2d696 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
2d697 34 31 30 2e 62 20 3d 20 30 3b 7d 0a 20 20 20 20  410.b = 0;}.    
2d698 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d699 20 63 61 73 65 20 32 37 34 3a 20 2f 2a 20 74 72   case 274: /* tr
2d69a 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20  igger_event ::= 
2d69b 55 50 44 41 54 45 20 4f 46 20 69 6e 73 63 6f 6c  UPDATE OF inscol
2d69c 6c 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  list */.{yygotom
2d69d 69 6e 6f 72 2e 79 79 34 31 30 2e 61 20 3d 20 54  inor.yy410.a = T
2d69e 4b 5f 55 50 44 41 54 45 3b 20 79 79 67 6f 74 6f  K_UPDATE; yygoto
2d69f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 62 20 3d 20  minor.yy410.b = 
2d6a0 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2d6a1 79 31 38 30 3b 7d 0a 20 20 20 20 20 20 20 20 62  y180;}.        b
2d6a2 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d6a3 20 32 37 37 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c   277: /* when_cl
2d6a4 61 75 73 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  ause ::= */.    
2d6a5 20 20 63 61 73 65 20 32 39 34 3a 20 2f 2a 20 6b    case 294: /* k
2d6a6 65 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 20  ey_opt ::= */.{ 
2d6a7 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32  yygotominor.yy12
2d6a8 32 20 3d 20 30 3b 20 7d 0a 20 20 20 20 20 20 20  2 = 0; }.       
2d6a9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2d6aa 73 65 20 32 37 38 3a 20 2f 2a 20 77 68 65 6e 5f  se 278: /* when_
2d6ab 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45 4e 20  clause ::= WHEN 
2d6ac 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
2d6ad 73 65 20 32 39 35 3a 20 2f 2a 20 6b 65 79 5f 6f  se 295: /* key_o
2d6ae 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 20  pt ::= KEY expr 
2d6af 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
2d6b0 2e 79 79 31 32 32 20 3d 20 79 79 6d 73 70 5b 30  .yy122 = yymsp[0
2d6b1 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 3b 20 7d  ].minor.yy122; }
2d6b2 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d6b3 20 20 20 20 20 20 63 61 73 65 20 32 37 39 3a 20        case 279: 
2d6b4 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  /* trigger_cmd_l
2d6b5 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f  ist ::= trigger_
2d6b6 63 6d 64 5f 6c 69 73 74 20 74 72 69 67 67 65 72  cmd_list trigger
2d6b7 5f 63 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b 0a 2f  _cmd SEMI */.{./
2d6b8 2a 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32  *.  if( yymsp[-2
2d6b9 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37 20 29 7b  ].minor.yy327 ){
2d6ba 0a 20 20 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  .    yymsp[-2].m
2d6bb 69 6e 6f 72 2e 79 79 33 32 37 2d 3e 70 4c 61 73  inor.yy327->pLas
2d6bc 74 2d 3e 70 4e 65 78 74 20 3d 20 79 79 6d 73 70  t->pNext = yymsp
2d6bd 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37  [-1].minor.yy327
2d6be 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 79  ;.  }else{.    y
2d6bf 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2d6c0 79 33 32 37 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  y327 = yymsp[-1]
2d6c1 2e 6d 69 6e 6f 72 2e 79 79 33 32 37 3b 0a 20 20  .minor.yy327;.  
2d6c2 7d 0a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 79  }.*/.  assert( y
2d6c3 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2d6c4 79 33 32 37 21 3d 30 20 29 3b 0a 20 20 79 79 6d  y327!=0 );.  yym
2d6c5 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
2d6c6 32 37 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74  27->pLast->pNext
2d6c7 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
2d6c8 6f 72 2e 79 79 33 32 37 3b 0a 20 20 79 79 6d 73  or.yy327;.  yyms
2d6c9 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-2].minor.yy32
2d6ca 37 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d 73 70  7->pLast = yymsp
2d6cb 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37  [-1].minor.yy327
2d6cc 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
2d6cd 79 79 33 32 37 20 3d 20 79 79 6d 73 70 5b 2d 32  yy327 = yymsp[-2
2d6ce 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37 3b 0a 7d  ].minor.yy327;.}
2d6cf 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d6d0 20 20 20 20 20 20 63 61 73 65 20 32 38 30 3a 20        case 280: 
2d6d1 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  /* trigger_cmd_l
2d6d2 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f  ist ::= trigger_
2d6d3 63 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b 20 0a 20  cmd SEMI */.{ . 
2d6d4 20 2f 2a 20 69 66 28 20 79 79 6d 73 70 5b 2d 31   /* if( yymsp[-1
2d6d5 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37 20 29 20  ].minor.yy327 ) 
2d6d6 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 79 79 6d  */.  assert( yym
2d6d7 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
2d6d8 32 37 21 3d 30 20 29 3b 0a 20 20 79 79 6d 73 70  27!=0 );.  yymsp
2d6d9 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37  [-1].minor.yy327
2d6da 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b  ->pLast = yymsp[
2d6db 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37 3b  -1].minor.yy327;
2d6dc 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2d6dd 79 33 32 37 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  y327 = yymsp[-1]
2d6de 2e 6d 69 6e 6f 72 2e 79 79 33 32 37 3b 0a 7d 0a  .minor.yy327;.}.
2d6df 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d6e0 20 20 20 20 20 63 61 73 65 20 32 38 31 3a 20 2f       case 281: /
2d6e1 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a  * trigger_cmd ::
2d6e2 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20  = UPDATE orconf 
2d6e3 6e 6d 20 53 45 54 20 73 65 74 6c 69 73 74 20 77  nm SET setlist w
2d6e4 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 20 79 79  here_opt */.{ yy
2d6e5 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 37 20  gotominor.yy327 
2d6e6 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
2d6e7 55 70 64 61 74 65 53 74 65 70 28 70 50 61 72 73  UpdateStep(pPars
2d6e8 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 33  e->db, &yymsp[-3
2d6e9 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d  ].minor.yy0, yym
2d6ea 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
2d6eb 34 32 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  42, yymsp[0].min
2d6ec 6f 72 2e 79 79 31 32 32 2c 20 79 79 6d 73 70 5b  or.yy122, yymsp[
2d6ed 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29  -4].minor.yy392)
2d6ee 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
2d6ef 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  k;.      case 28
2d6f0 32 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  2: /* trigger_cm
2d6f1 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
2d6f2 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63 6f 6c 6c   INTO nm inscoll
2d6f3 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c  ist_opt VALUES L
2d6f4 50 20 69 74 65 6d 6c 69 73 74 20 52 50 20 2a 2f  P itemlist RP */
2d6f5 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2d6f6 33 32 37 20 3d 20 73 71 6c 69 74 65 33 54 72 69  327 = sqlite3Tri
2d6f7 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 70  ggerInsertStep(p
2d6f8 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73  Parse->db, &yyms
2d6f9 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-5].minor.yy0,
2d6fa 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
2d6fb 2e 79 79 31 38 30 2c 20 79 79 6d 73 70 5b 2d 31  .yy180, yymsp[-1
2d6fc 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 20 30  ].minor.yy442, 0
2d6fd 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f  , yymsp[-7].mino
2d6fe 72 2e 79 79 33 39 32 29 3b 7d 0a 20 20 20 20 20  r.yy392);}.     
2d6ff 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2d700 63 61 73 65 20 32 38 33 3a 20 2f 2a 20 74 72 69  case 283: /* tri
2d701 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73  gger_cmd ::= ins
2d702 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20  ert_cmd INTO nm 
2d703 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
2d704 65 6c 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f  elect */.{yygoto
2d705 6d 69 6e 6f 72 2e 79 79 33 32 37 20 3d 20 73 71  minor.yy327 = sq
2d706 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 65  lite3TriggerInse
2d707 72 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64  rtStep(pParse->d
2d708 62 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  b, &yymsp[-2].mi
2d709 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d  nor.yy0, yymsp[-
2d70a 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 30 2c 20  1].minor.yy180, 
2d70b 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0, yymsp[0].mino
2d70c 72 2e 79 79 31 35 39 2c 20 79 79 6d 73 70 5b 2d  r.yy159, yymsp[-
2d70d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29 3b  4].minor.yy392);
2d70e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d70f 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 34 3a  .      case 284:
2d710 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20   /* trigger_cmd 
2d711 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
2d712 6e 6d 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a  nm where_opt */.
2d713 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
2d714 32 37 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  27 = sqlite3Trig
2d715 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 70 50  gerDeleteStep(pP
2d716 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70  arse->db, &yymsp
2d717 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-1].minor.yy0, 
2d718 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2d719 79 31 32 32 29 3b 7d 0a 20 20 20 20 20 20 20 20  y122);}.        
2d71a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d71b 65 20 32 38 35 3a 20 2f 2a 20 74 72 69 67 67 65  e 285: /* trigge
2d71c 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74  r_cmd ::= select
2d71d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2d71e 2e 79 79 33 32 37 20 3d 20 73 71 6c 69 74 65 33  .yy327 = sqlite3
2d71f 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74 65  TriggerSelectSte
2d720 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79  p(pParse->db, yy
2d721 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
2d722 35 39 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  59); }.        b
2d723 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d724 20 32 38 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   286: /* expr ::
2d725 3d 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52  = RAISE LP IGNOR
2d726 45 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  E RP */.{.  yygo
2d727 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20  tominor.yy122 = 
2d728 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2d729 72 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30  rse, TK_RAISE, 0
2d72a 2c 20 30 2c 20 30 29 3b 20 0a 20 20 69 66 28 20  , 0, 0); .  if( 
2d72b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32  yygotominor.yy12
2d72c 32 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  2 ){.    yygotom
2d72d 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 69 43 6f 6c  inor.yy122->iCol
2d72e 75 6d 6e 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b  umn = OE_Ignore;
2d72f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2d730 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2d731 2e 79 79 31 32 32 2c 20 26 79 79 6d 73 70 5b 2d  .yy122, &yymsp[-
2d732 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  3].minor.yy0, &y
2d733 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2d734 30 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20  0);.  }.}.      
2d735 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2d736 61 73 65 20 32 38 37 3a 20 2f 2a 20 65 78 70 72  ase 287: /* expr
2d737 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61   ::= RAISE LP ra
2d738 69 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d  isetype COMMA nm
2d739 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
2d73a 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20 73  ominor.yy122 = s
2d73b 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2d73c 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c  se, TK_RAISE, 0,
2d73d 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d   0, &yymsp[-1].m
2d73e 69 6e 6f 72 2e 79 79 30 29 3b 20 0a 20 20 69 66  inor.yy0); .  if
2d73f 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2d740 31 32 32 20 29 20 7b 0a 20 20 20 20 79 79 67 6f  122 ) {.    yygo
2d741 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 2d 3e 69  tominor.yy122->i
2d742 43 6f 6c 75 6d 6e 20 3d 20 79 79 6d 73 70 5b 2d  Column = yymsp[-
2d743 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 3b 0a  3].minor.yy392;.
2d744 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
2d745 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pan(yygotominor.
2d746 79 79 31 32 32 2c 20 26 79 79 6d 73 70 5b 2d 35  yy122, &yymsp[-5
2d747 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
2d748 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2d749 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20  );.  }.}.       
2d74a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2d74b 73 65 20 32 38 38 3a 20 2f 2a 20 72 61 69 73 65  se 288: /* raise
2d74c 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43  type ::= ROLLBAC
2d74d 4b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  K */.{yygotomino
2d74e 72 2e 79 79 33 39 32 20 3d 20 4f 45 5f 52 6f 6c  r.yy392 = OE_Rol
2d74f 6c 62 61 63 6b 3b 7d 0a 20 20 20 20 20 20 20 20  lback;}.        
2d750 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d751 65 20 32 39 30 3a 20 2f 2a 20 72 61 69 73 65 74  e 290: /* raiset
2d752 79 70 65 20 3a 3a 3d 20 46 41 49 4c 20 2a 2f 0a  ype ::= FAIL */.
2d753 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
2d754 39 32 20 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20  92 = OE_Fail;}. 
2d755 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2d756 20 20 20 20 63 61 73 65 20 32 39 31 3a 20 2f 2a      case 291: /*
2d757 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52   cmd ::= DROP TR
2d758 49 47 47 45 52 20 69 66 65 78 69 73 74 73 20 66  IGGER ifexists f
2d759 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73  ullname */.{.  s
2d75a 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65  qlite3DropTrigge
2d75b 72 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30  r(pParse,yymsp[0
2d75c 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 79 79  ].minor.yy347,yy
2d75d 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2d75e 33 39 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  392);.}.        
2d75f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d760 65 20 32 39 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 292: /* cmd ::
2d761 3d 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73  = ATTACH databas
2d762 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 41 53  e_kw_opt expr AS
2d763 20 65 78 70 72 20 6b 65 79 5f 6f 70 74 20 2a 2f   expr key_opt */
2d764 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 74 74 61  .{.  sqlite3Atta
2d765 63 68 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ch(pParse, yymsp
2d766 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32  [-3].minor.yy122
2d767 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2d768 72 2e 79 79 31 32 32 2c 20 79 79 6d 73 70 5b 30  r.yy122, yymsp[0
2d769 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 29 3b 0a  ].minor.yy122);.
2d76a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2d76b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 33 3a  .      case 293:
2d76c 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41   /* cmd ::= DETA
2d76d 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  CH database_kw_o
2d76e 70 74 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73  pt expr */.{.  s
2d76f 71 6c 69 74 65 33 44 65 74 61 63 68 28 70 50 61  qlite3Detach(pPa
2d770 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
2d771 6e 6f 72 2e 79 79 31 32 32 29 3b 0a 7d 0a 20 20  nor.yy122);.}.  
2d772 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d773 20 20 20 63 61 73 65 20 32 39 38 3a 20 2f 2a 20     case 298: /* 
2d774 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20  cmd ::= REINDEX 
2d775 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64  */.{sqlite3Reind
2d776 65 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29  ex(pParse, 0, 0)
2d777 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2d778 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 39  ;.      case 299
2d779 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49  : /* cmd ::= REI
2d77a 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a  NDEX nm dbnm */.
2d77b 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28  {sqlite3Reindex(
2d77c 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
2d77d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  1].minor.yy0, &y
2d77e 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2d77f 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2d780 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
2d781 30 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41  00: /* cmd ::= A
2d782 4e 41 4c 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69 74  NALYZE */.{sqlit
2d783 65 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65  e3Analyze(pParse
2d784 2c 20 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20  , 0, 0);}.      
2d785 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2d786 61 73 65 20 33 30 31 3a 20 2f 2a 20 63 6d 64 20  ase 301: /* cmd 
2d787 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64  ::= ANALYZE nm d
2d788 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  bnm */.{sqlite3A
2d789 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 26  nalyze(pParse, &
2d78a 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2d78b 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  yy0, &yymsp[0].m
2d78c 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
2d78d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d78e 20 63 61 73 65 20 33 30 32 3a 20 2f 2a 20 63 6d   case 302: /* cm
2d78f 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c  d ::= ALTER TABL
2d790 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d  E fullname RENAM
2d791 45 20 54 4f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73  E TO nm */.{.  s
2d792 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d  qlite3AlterRenam
2d793 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 79 79  eTable(pParse,yy
2d794 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2d795 33 34 37 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  347,&yymsp[0].mi
2d796 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
2d797 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d798 20 63 61 73 65 20 33 30 33 3a 20 2f 2a 20 63 6d   case 303: /* cm
2d799 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c  d ::= ALTER TABL
2d79a 45 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c  E add_column_ful
2d79b 6c 6e 61 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75  lname ADD kwcolu
2d79c 6d 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e 20 2a 2f  mn_opt column */
2d79d 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65  .{.  sqlite3Alte
2d79e 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e  rFinishAddColumn
2d79f 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
2d7a0 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
2d7a1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2d7a2 20 20 20 20 20 20 63 61 73 65 20 33 30 34 3a 20        case 304: 
2d7a3 2f 2a 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75  /* add_column_fu
2d7a4 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e  llname ::= fulln
2d7a5 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  ame */.{.  sqlit
2d7a6 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43  e3AlterBeginAddC
2d7a7 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 79 79  olumn(pParse, yy
2d7a8 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
2d7a9 34 37 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  47);.}.        b
2d7aa 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d7ab 20 33 30 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   307: /* cmd ::=
2d7ac 20 63 72 65 61 74 65 5f 76 74 61 62 20 2a 2f 0a   create_vtab */.
2d7ad 7b 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69  {sqlite3VtabFini
2d7ae 73 68 50 61 72 73 65 28 70 50 61 72 73 65 2c 30  shParse(pParse,0
2d7af 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2d7b0 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
2d7b1 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72  8: /* cmd ::= cr
2d7b2 65 61 74 65 5f 76 74 61 62 20 4c 50 20 76 74 61  eate_vtab LP vta
2d7b3 62 61 72 67 6c 69 73 74 20 52 50 20 2a 2f 0a 7b  barglist RP */.{
2d7b4 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73  sqlite3VtabFinis
2d7b5 68 50 61 72 73 65 28 70 50 61 72 73 65 2c 26 79  hParse(pParse,&y
2d7b6 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2d7b7 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2d7b8 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
2d7b9 30 39 3a 20 2f 2a 20 63 72 65 61 74 65 5f 76 74  09: /* create_vt
2d7ba 61 62 20 3a 3a 3d 20 43 52 45 41 54 45 20 56 49  ab ::= CREATE VI
2d7bb 52 54 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64  RTUAL TABLE nm d
2d7bc 62 6e 6d 20 55 53 49 4e 47 20 6e 6d 20 2a 2f 0a  bnm USING nm */.
2d7bd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61  {.    sqlite3Vta
2d7be 62 42 65 67 69 6e 50 61 72 73 65 28 70 50 61 72  bBeginParse(pPar
2d7bf 73 65 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  se, &yymsp[-3].m
2d7c0 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
2d7c1 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-2].minor.yy0, 
2d7c2 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2d7c3 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
2d7c4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2d7c5 65 20 33 31 32 3a 20 2f 2a 20 76 74 61 62 61 72  e 312: /* vtabar
2d7c6 67 20 3a 3a 3d 20 2a 2f 0a 7b 73 71 6c 69 74 65  g ::= */.{sqlite
2d7c7 33 56 74 61 62 41 72 67 49 6e 69 74 28 70 50 61  3VtabArgInit(pPa
2d7c8 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  rse);}.        b
2d7c9 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2d7ca 20 33 31 34 3a 20 2f 2a 20 76 74 61 62 61 72 67   314: /* vtabarg
2d7cb 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 20 2a 2f  token ::= ANY */
2d7cc 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 35 3a  .      case 315:
2d7cd 20 2f 2a 20 76 74 61 62 61 72 67 74 6f 6b 65 6e   /* vtabargtoken
2d7ce 20 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20   ::= lp anylist 
2d7cf 52 50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  RP */.      case
2d7d0 20 33 31 36 3a 20 2f 2a 20 6c 70 20 3a 3a 3d 20   316: /* lp ::= 
2d7d1 4c 50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  LP */.      case
2d7d2 20 33 31 38 3a 20 2f 2a 20 61 6e 79 6c 69 73 74   318: /* anylist
2d7d3 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59   ::= anylist ANY
2d7d4 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62   */.{sqlite3Vtab
2d7d5 41 72 67 45 78 74 65 6e 64 28 70 50 61 72 73 65  ArgExtend(pParse
2d7d6 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2d7d7 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
2d7d8 62 72 65 61 6b 3b 0a 20 20 7d 3b 0a 20 20 79 79  break;.  };.  yy
2d7d9 67 6f 74 6f 20 3d 20 79 79 52 75 6c 65 49 6e 66  goto = yyRuleInf
2d7da 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6c 68 73 3b  o[yyruleno].lhs;
2d7db 0a 20 20 79 79 73 69 7a 65 20 3d 20 79 79 52 75  .  yysize = yyRu
2d7dc 6c 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d  leInfo[yyruleno]
2d7dd 2e 6e 72 68 73 3b 0a 20 20 79 79 70 50 61 72 73  .nrhs;.  yypPars
2d7de 65 72 2d 3e 79 79 69 64 78 20 2d 3d 20 79 79 73  er->yyidx -= yys
2d7df 69 7a 65 3b 0a 20 20 79 79 61 63 74 20 3d 20 79  ize;.  yyact = y
2d7e0 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63  y_find_reduce_ac
2d7e1 74 69 6f 6e 28 79 79 6d 73 70 5b 2d 79 79 73 69  tion(yymsp[-yysi
2d7e2 7a 65 5d 2e 73 74 61 74 65 6e 6f 2c 28 59 59 43  ze].stateno,(YYC
2d7e3 4f 44 45 54 59 50 45 29 79 79 67 6f 74 6f 29 3b  ODETYPE)yygoto);
2d7e4 0a 20 20 69 66 28 20 79 79 61 63 74 20 3c 20 59  .  if( yyact < Y
2d7e5 59 4e 53 54 41 54 45 20 29 7b 0a 23 69 66 64 65  YNSTATE ){.#ifde
2d7e6 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20  f NDEBUG.    /* 
2d7e7 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 64 65  If we are not de
2d7e8 62 75 67 67 69 6e 67 20 61 6e 64 20 74 68 65 20  bugging and the 
2d7e9 72 65 64 75 63 65 20 61 63 74 69 6f 6e 20 70 6f  reduce action po
2d7ea 70 70 65 64 20 61 74 20 6c 65 61 73 74 0a 20 20  pped at least.  
2d7eb 20 20 2a 2a 20 6f 6e 65 20 65 6c 65 6d 65 6e 74    ** one element
2d7ec 20 6f 66 66 20 74 68 65 20 73 74 61 63 6b 2c 20   off the stack, 
2d7ed 74 68 65 6e 20 77 65 20 63 61 6e 20 70 75 73 68  then we can push
2d7ee 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74   the new element
2d7ef 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20 6f 6e 74   back.    ** ont
2d7f0 6f 20 74 68 65 20 73 74 61 63 6b 20 68 65 72 65  o the stack here
2d7f1 2c 20 61 6e 64 20 73 6b 69 70 20 74 68 65 20 73  , and skip the s
2d7f2 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 74 65  tack overflow te
2d7f3 73 74 20 69 6e 20 79 79 5f 73 68 69 66 74 28 29  st in yy_shift()
2d7f4 2e 0a 20 20 20 20 2a 2a 20 54 68 61 74 20 67 69  ..    ** That gi
2d7f5 76 65 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e  ves a significan
2d7f6 74 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65 6d  t speed improvem
2d7f7 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ent. */.    if( 
2d7f8 79 79 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  yysize ){.      
2d7f9 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
2d7fa 2b 2b 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 20  ++;.      yymsp 
2d7fb 2d 3d 20 79 79 73 69 7a 65 2d 31 3b 0a 20 20 20  -= yysize-1;.   
2d7fc 20 20 20 79 79 6d 73 70 2d 3e 73 74 61 74 65 6e     yymsp->staten
2d7fd 6f 20 3d 20 79 79 61 63 74 3b 0a 20 20 20 20 20  o = yyact;.     
2d7fe 20 79 79 6d 73 70 2d 3e 6d 61 6a 6f 72 20 3d 20   yymsp->major = 
2d7ff 79 79 67 6f 74 6f 3b 0a 20 20 20 20 20 20 79 79  yygoto;.      yy
2d800 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d 20 79 79 67  msp->minor = yyg
2d801 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 7d 65  otominor;.    }e
2d802 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b  lse.#endif.    {
2d803 0a 20 20 20 20 20 20 79 79 5f 73 68 69 66 74 28  .      yy_shift(
2d804 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74 2c  yypParser,yyact,
2d805 79 79 67 6f 74 6f 2c 26 79 79 67 6f 74 6f 6d 69  yygoto,&yygotomi
2d806 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  nor);.    }.  }e
2d807 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
2d808 20 79 79 61 63 74 20 3d 3d 20 59 59 4e 53 54 41   yyact == YYNSTA
2d809 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 2b 20 31  TE + YYNRULE + 1
2d80a 20 29 3b 0a 20 20 20 20 79 79 5f 61 63 63 65 70   );.    yy_accep
2d80b 74 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20  t(yypParser);.  
2d80c 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  }.}../*.** The f
2d80d 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65 78  ollowing code ex
2d80e 65 63 75 74 65 73 20 77 68 65 6e 20 74 68 65 20  ecutes when the 
2d80f 70 61 72 73 65 20 66 61 69 6c 73 0a 2a 2f 0a 73  parse fails.*/.s
2d810 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 70 61  tatic void yy_pa
2d811 72 73 65 5f 66 61 69 6c 65 64 28 0a 20 20 79 79  rse_failed(.  yy
2d812 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65  Parser *yypParse
2d813 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  r           /* T
2d814 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a  he parser */.){.
2d815 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
2d816 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65  RG_FETCH;.#ifnde
2d817 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79  f NDEBUG.  if( y
2d818 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
2d819 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
2d81a 65 46 49 4c 45 2c 22 25 73 46 61 69 6c 21 5c 6e  eFILE,"%sFail!\n
2d81b 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29  ",yyTracePrompt)
2d81c 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77  ;.  }.#endif.  w
2d81d 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d  hile( yypParser-
2d81e 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70  >yyidx>=0 ) yy_p
2d81f 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28  op_parser_stack(
2d820 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a  yypParser);.  /*
2d821 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e   Here code is in
2d822 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69 6c  serted which wil
2d823 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77 68  l be executed wh
2d824 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a 20  enever the.  ** 
2d825 70 61 72 73 65 72 20 66 61 69 6c 73 20 2a 2f 0a  parser fails */.
2d826 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
2d827 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70  RG_STORE; /* Sup
2d828 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62  press warning ab
2d829 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72  out unused %extr
2d82a 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61  a_argument varia
2d82b 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ble */.}../*.** 
2d82c 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
2d82d 64 65 20 65 78 65 63 75 74 65 73 20 77 68 65 6e  de executes when
2d82e 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20   a syntax error 
2d82f 66 69 72 73 74 20 6f 63 63 75 72 73 2e 0a 2a 2f  first occurs..*/
2d830 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f  .static void yy_
2d831 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 0a 20 20  syntax_error(.  
2d832 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72  yyParser *yypPar
2d833 73 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ser,           /
2d834 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
2d835 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20    int yymajor,  
2d836 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d837 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 79   /* The major ty
2d838 70 65 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  pe of the error 
2d839 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e  token */.  YYMIN
2d83a 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 20 20  ORTYPE yyminor  
2d83b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2d83c 20 6d 69 6e 6f 72 20 74 79 70 65 20 6f 66 20 74   minor type of t
2d83d 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 2a  he error token *
2d83e 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61  /.){.  sqlite3Pa
2d83f 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23  rserARG_FETCH;.#
2d840 64 65 66 69 6e 65 20 54 4f 4b 45 4e 20 28 79 79  define TOKEN (yy
2d841 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a 20 20 55 4e  minor.yy0)..  UN
2d842 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79  USED_PARAMETER(y
2d843 79 6d 61 6a 6f 72 29 3b 20 20 2f 2a 20 53 69 6c  ymajor);  /* Sil
2d844 65 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70 69 6c  ence some compil
2d845 65 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f 0a 20  er warnings */. 
2d846 20 61 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e 7a   assert( TOKEN.z
2d847 5b 30 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20 74  [0] );  /* The t
2d848 6f 6b 65 6e 69 7a 65 72 20 61 6c 77 61 79 73 20  okenizer always 
2d849 67 69 76 65 73 20 75 73 20 61 20 74 6f 6b 65 6e  gives us a token
2d84a 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72   */.  sqlite3Err
2d84b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
2d84c 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74  ear \"%T\": synt
2d84d 61 78 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b 45  ax error", &TOKE
2d84e 4e 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61  N);.  pParse->pa
2d84f 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20  rseError = 1;.  
2d850 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
2d851 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72  _STORE; /* Suppr
2d852 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75  ess warning abou
2d853 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f  t unused %extra_
2d854 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c  argument variabl
2d855 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  e */.}../*.** Th
2d856 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 65  e following is e
2d857 78 65 63 75 74 65 64 20 77 68 65 6e 20 74 68 65  xecuted when the
2d858 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73 0a   parser accepts.
2d859 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79  */.static void y
2d85a 79 5f 61 63 63 65 70 74 28 0a 20 20 79 79 50 61  y_accept(.  yyPa
2d85b 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 20  rser *yypParser 
2d85c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2d85d 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20   parser */.){.  
2d85e 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
2d85f 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65 66 20  _FETCH;.#ifndef 
2d860 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54  NDEBUG.  if( yyT
2d861 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
2d862 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
2d863 49 4c 45 2c 22 25 73 41 63 63 65 70 74 21 5c 6e  ILE,"%sAccept!\n
2d864 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29  ",yyTracePrompt)
2d865 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77  ;.  }.#endif.  w
2d866 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d  hile( yypParser-
2d867 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70  >yyidx>=0 ) yy_p
2d868 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28  op_parser_stack(
2d869 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a  yypParser);.  /*
2d86a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e   Here code is in
2d86b 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69 6c  serted which wil
2d86c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77 68  l be executed wh
2d86d 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a 20  enever the.  ** 
2d86e 70 61 72 73 65 72 20 61 63 63 65 70 74 73 20 2a  parser accepts *
2d86f 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
2d870 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53  rARG_STORE; /* S
2d871 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20  uppress warning 
2d872 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78  about unused %ex
2d873 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72  tra_argument var
2d874 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20 54  iable */.}../* T
2d875 68 65 20 6d 61 69 6e 20 70 61 72 73 65 72 20 70  he main parser p
2d876 72 6f 67 72 61 6d 2e 0a 2a 2a 20 54 68 65 20 66  rogram..** The f
2d877 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
2d878 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
2d879 73 74 72 75 63 74 75 72 65 20 6f 62 74 61 69 6e  structure obtain
2d87a 65 64 20 66 72 6f 6d 0a 2a 2a 20 22 73 71 6c 69  ed from.** "sqli
2d87b 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 22 20  te3ParserAlloc" 
2d87c 77 68 69 63 68 20 64 65 73 63 72 69 62 65 73 20  which describes 
2d87d 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
2d87e 65 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 2e  e of the parser.
2d87f 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61  .** The second a
2d880 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6d  rgument is the m
2d881 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65  ajor token numbe
2d882 72 2e 20 20 54 68 65 20 74 68 69 72 64 20 69 73  r.  The third is
2d883 0a 2a 2a 20 74 68 65 20 6d 69 6e 6f 72 20 74 6f  .** the minor to
2d884 6b 65 6e 2e 20 20 54 68 65 20 66 6f 75 72 74 68  ken.  The fourth
2d885 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65   optional argume
2d886 6e 74 20 69 73 20 77 68 61 74 65 76 65 72 20 74  nt is whatever t
2d887 68 65 0a 2a 2a 20 75 73 65 72 20 77 61 6e 74 73  he.** user wants
2d888 20 28 61 6e 64 20 73 70 65 63 69 66 69 65 64 20   (and specified 
2d889 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 29 20  in the grammar) 
2d88a 61 6e 64 20 69 73 20 61 76 61 69 6c 61 62 6c 65  and is available
2d88b 20 66 6f 72 0a 2a 2a 20 75 73 65 20 62 79 20 74   for.** use by t
2d88c 68 65 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e  he action routin
2d88d 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73  es..**.** Inputs
2d88e 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  :.** <ul>.** <li
2d88f 3e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  > A pointer to t
2d890 68 65 20 70 61 72 73 65 72 20 28 61 6e 20 6f 70  he parser (an op
2d891 61 71 75 65 20 73 74 72 75 63 74 75 72 65 2e 29  aque structure.)
2d892 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 61 6a  .** <li> The maj
2d893 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e  or token number.
2d894 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 69 6e  .** <li> The min
2d895 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e  or token number.
2d896 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 6f 70 74 69  .** <li> An opti
2d897 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 61  on argument of a
2d898 20 67 72 61 6d 6d 61 72 2d 73 70 65 63 69 66 69   grammar-specifi
2d899 65 64 20 74 79 70 65 2e 0a 2a 2a 20 3c 2f 75 6c  ed type..** </ul
2d89a 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a  >.**.** Outputs:
2d89b 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c  .** None..*/.SQL
2d89c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d89d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 0a   sqlite3Parser(.
2d89e 20 20 76 6f 69 64 20 2a 79 79 70 2c 20 20 20 20    void *yyp,    
2d89f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2d8a0 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
2d8a1 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20    int yymajor,  
2d8a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2d8a3 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65  * The major toke
2d8a4 6e 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f  n code number */
2d8a5 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
2d8a6 54 4f 4b 45 4e 54 59 50 45 20 79 79 6d 69 6e 6f  TOKENTYPE yymino
2d8a7 72 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76  r       /* The v
2d8a8 61 6c 75 65 20 66 6f 72 20 74 68 65 20 74 6f 6b  alue for the tok
2d8a9 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50  en */.  sqlite3P
2d8aa 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20 20  arserARG_PDECL  
2d8ab 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d8ac 4f 70 74 69 6f 6e 61 6c 20 25 65 78 74 72 61 5f  Optional %extra_
2d8ad 61 72 67 75 6d 65 6e 74 20 70 61 72 61 6d 65 74  argument paramet
2d8ae 65 72 20 2a 2f 0a 29 7b 0a 20 20 59 59 4d 49 4e  er */.){.  YYMIN
2d8af 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 75 6e  ORTYPE yyminorun
2d8b0 69 6f 6e 3b 0a 20 20 69 6e 74 20 79 79 61 63 74  ion;.  int yyact
2d8b1 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2d8b2 54 68 65 20 70 61 72 73 65 72 20 61 63 74 69 6f  The parser actio
2d8b3 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 79 79 65 6e  n. */.  int yyen
2d8b4 64 6f 66 69 6e 70 75 74 3b 20 20 20 20 20 2f 2a  dofinput;     /*
2d8b5 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20   True if we are 
2d8b6 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 69 6e  at the end of in
2d8b7 70 75 74 20 2a 2f 0a 23 69 66 64 65 66 20 59 59  put */.#ifdef YY
2d8b8 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 6e  ERRORSYMBOL.  in
2d8b9 74 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20 30  t yyerrorhit = 0
2d8ba 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 79  ;   /* True if y
2d8bb 79 6d 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f 6b  ymajor has invok
2d8bc 65 64 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 23  ed an error */.#
2d8bd 65 6e 64 69 66 0a 20 20 79 79 50 61 72 73 65 72  endif.  yyParser
2d8be 20 2a 79 79 70 50 61 72 73 65 72 3b 20 20 2f 2a   *yypParser;  /*
2d8bf 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 0a   The parser */..
2d8c0 20 20 2f 2a 20 28 72 65 29 69 6e 69 74 69 61 6c    /* (re)initial
2d8c1 69 7a 65 20 74 68 65 20 70 61 72 73 65 72 2c 20  ize the parser, 
2d8c2 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a  if necessary */.
2d8c3 20 20 79 79 70 50 61 72 73 65 72 20 3d 20 28 79    yypParser = (y
2d8c4 79 50 61 72 73 65 72 2a 29 79 79 70 3b 0a 20 20  yParser*)yyp;.  
2d8c5 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
2d8c6 79 69 64 78 3c 30 20 29 7b 0a 23 69 66 20 59 59  yidx<0 ){.#if YY
2d8c7 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20  STACKDEPTH<=0.  
2d8c8 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
2d8c9 3e 79 79 73 74 6b 73 7a 20 3c 3d 30 20 29 7b 0a  >yystksz <=0 ){.
2d8ca 20 20 20 20 20 20 2f 2a 6d 65 6d 73 65 74 28 26        /*memset(&
2d8cb 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30 2c  yyminorunion, 0,
2d8cc 20 73 69 7a 65 6f 66 28 79 79 6d 69 6e 6f 72 75   sizeof(yyminoru
2d8cd 6e 69 6f 6e 29 29 3b 2a 2f 0a 20 20 20 20 20 20  nion));*/.      
2d8ce 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 20 3d 20 79  yyminorunion = y
2d8cf 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20  yzerominor;.    
2d8d0 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f    yyStackOverflo
2d8d1 77 28 79 79 70 50 61 72 73 65 72 2c 20 26 79 79  w(yypParser, &yy
2d8d2 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
2d8d3 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
2d8d4 0a 23 65 6e 64 69 66 0a 20 20 20 20 79 79 70 50  .#endif.    yypP
2d8d5 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20 30  arser->yyidx = 0
2d8d6 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d  ;.    yypParser-
2d8d7 3e 79 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b 0a  >yyerrcnt = -1;.
2d8d8 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
2d8d9 79 73 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65 6e  ystack[0].staten
2d8da 6f 20 3d 20 30 3b 0a 20 20 20 20 79 79 70 50 61  o = 0;.    yypPa
2d8db 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30 5d  rser->yystack[0]
2d8dc 2e 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a  .major = 0;.  }.
2d8dd 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e 79    yyminorunion.y
2d8de 79 30 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20 20  y0 = yyminor;.  
2d8df 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20 28  yyendofinput = (
2d8e0 79 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20 73  yymajor==0);.  s
2d8e1 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
2d8e2 53 54 4f 52 45 3b 0a 0a 23 69 66 6e 64 65 66 20  STORE;..#ifndef 
2d8e3 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54  NDEBUG.  if( yyT
2d8e4 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
2d8e5 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
2d8e6 49 4c 45 2c 22 25 73 49 6e 70 75 74 20 25 73 5c  ILE,"%sInput %s\
2d8e7 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
2d8e8 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d  ,yyTokenName[yym
2d8e9 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64  ajor]);.  }.#end
2d8ea 69 66 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 79 79  if..  do{.    yy
2d8eb 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73 68  act = yy_find_sh
2d8ec 69 66 74 5f 61 63 74 69 6f 6e 28 79 79 70 50 61  ift_action(yypPa
2d8ed 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45  rser,(YYCODETYPE
2d8ee 29 79 79 6d 61 6a 6f 72 29 3b 0a 20 20 20 20 69  )yymajor);.    i
2d8ef 66 28 20 79 79 61 63 74 3c 59 59 4e 53 54 41 54  f( yyact<YYNSTAT
2d8f0 45 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  E ){.      asser
2d8f1 74 28 20 21 79 79 65 6e 64 6f 66 69 6e 70 75 74  t( !yyendofinput
2d8f2 20 29 3b 20 20 2f 2a 20 49 6d 70 6f 73 73 69 62   );  /* Impossib
2d8f3 6c 65 20 74 6f 20 73 68 69 66 74 20 74 68 65 20  le to shift the 
2d8f4 24 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20  $ token */.     
2d8f5 20 79 79 5f 73 68 69 66 74 28 79 79 70 50 61 72   yy_shift(yypPar
2d8f6 73 65 72 2c 79 79 61 63 74 2c 79 79 6d 61 6a 6f  ser,yyact,yymajo
2d8f7 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29  r,&yyminorunion)
2d8f8 3b 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65  ;.      yypParse
2d8f9 72 2d 3e 79 79 65 72 72 63 6e 74 2d 2d 3b 0a 20  r->yyerrcnt--;. 
2d8fa 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59       yymajor = Y
2d8fb 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 7d 65 6c  YNOCODE;.    }el
2d8fc 73 65 20 69 66 28 20 79 79 61 63 74 20 3c 20 59  se if( yyact < Y
2d8fd 59 4e 53 54 41 54 45 20 2b 20 59 59 4e 52 55 4c  YNSTATE + YYNRUL
2d8fe 45 20 29 7b 0a 20 20 20 20 20 20 79 79 5f 72 65  E ){.      yy_re
2d8ff 64 75 63 65 28 79 79 70 50 61 72 73 65 72 2c 79  duce(yypParser,y
2d900 79 61 63 74 2d 59 59 4e 53 54 41 54 45 29 3b 0a  yact-YYNSTATE);.
2d901 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2d902 20 61 73 73 65 72 74 28 20 79 79 61 63 74 20 3d   assert( yyact =
2d903 3d 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49 4f  = YY_ERROR_ACTIO
2d904 4e 20 29 3b 0a 23 69 66 64 65 66 20 59 59 45 52  N );.#ifdef YYER
2d905 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20 20  RORSYMBOL.      
2d906 69 6e 74 20 79 79 6d 78 3b 0a 23 65 6e 64 69 66  int yymx;.#endif
2d907 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2d908 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63        if( yyTrac
2d909 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20  eFILE ){.       
2d90a 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
2d90b 46 49 4c 45 2c 22 25 73 53 79 6e 74 61 78 20 45  FILE,"%sSyntax E
2d90c 72 72 6f 72 21 5c 6e 22 2c 79 79 54 72 61 63 65  rror!\n",yyTrace
2d90d 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 20 20 7d  Prompt);.      }
2d90e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 59  .#endif.#ifdef Y
2d90f 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20  YERRORSYMBOL.   
2d910 20 20 20 2f 2a 20 41 20 73 79 6e 74 61 78 20 65     /* A syntax e
2d911 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
2d912 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20  d..      ** The 
2d913 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 6e 20 65  response to an e
2d914 72 72 6f 72 20 64 65 70 65 6e 64 73 20 75 70 6f  rror depends upo
2d915 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  n whether or not
2d916 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 67 72   the.      ** gr
2d917 61 6d 6d 61 72 20 64 65 66 69 6e 65 73 20 61 6e  ammar defines an
2d918 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 22 45 52   error token "ER
2d919 52 4f 52 22 2e 20 20 0a 20 20 20 20 20 20 2a 2a  ROR".  .      **
2d91a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69  .      ** This i
2d91b 73 20 77 68 61 74 20 77 65 20 64 6f 20 69 66 20  s what we do if 
2d91c 74 68 65 20 67 72 61 6d 6d 61 72 20 64 6f 65 73  the grammar does
2d91d 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20   define ERROR:. 
2d91e 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
2d91f 20 20 2a 20 43 61 6c 6c 20 74 68 65 20 25 73 79    * Call the %sy
2d920 6e 74 61 78 5f 65 72 72 6f 72 20 66 75 6e 63 74  ntax_error funct
2d921 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ion..      **.  
2d922 20 20 20 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20      **  * Begin 
2d923 70 6f 70 70 69 6e 67 20 74 68 65 20 73 74 61 63  popping the stac
2d924 6b 20 75 6e 74 69 6c 20 77 65 20 65 6e 74 65 72  k until we enter
2d925 20 61 20 73 74 61 74 65 20 77 68 65 72 65 0a 20   a state where. 
2d926 20 20 20 20 20 2a 2a 20 20 20 20 69 74 20 69 73       **    it is
2d927 20 6c 65 67 61 6c 20 74 6f 20 73 68 69 66 74 20   legal to shift 
2d928 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c  the error symbol
2d929 2c 20 74 68 65 6e 20 73 68 69 66 74 0a 20 20 20  , then shift.   
2d92a 20 20 20 2a 2a 20 20 20 20 74 68 65 20 65 72 72     **    the err
2d92b 6f 72 20 73 79 6d 62 6f 6c 2e 0a 20 20 20 20 20  or symbol..     
2d92c 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20   **.      **  * 
2d92d 53 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f  Set the error co
2d92e 75 6e 74 20 74 6f 20 74 68 72 65 65 2e 0a 20 20  unt to three..  
2d92f 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
2d930 20 2a 20 42 65 67 69 6e 20 61 63 63 65 70 74 69   * Begin accepti
2d931 6e 67 20 61 6e 64 20 73 68 69 66 74 69 6e 67 20  ng and shifting 
2d932 6e 65 77 20 74 6f 6b 65 6e 73 2e 20 20 4e 6f 20  new tokens.  No 
2d933 6e 65 77 20 65 72 72 6f 72 0a 20 20 20 20 20 20  new error.      
2d934 2a 2a 20 20 20 20 70 72 6f 63 65 73 73 69 6e 67  **    processing
2d935 20 77 69 6c 6c 20 6f 63 63 75 72 20 75 6e 74 69   will occur unti
2d936 6c 20 74 68 72 65 65 20 74 6f 6b 65 6e 73 20 68  l three tokens h
2d937 61 76 65 20 62 65 65 6e 0a 20 20 20 20 20 20 2a  ave been.      *
2d938 2a 20 20 20 20 73 68 69 66 74 65 64 20 73 75 63  *    shifted suc
2d939 63 65 73 73 66 75 6c 6c 79 2e 0a 20 20 20 20 20  cessfully..     
2d93a 20 2a 2a 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20   **.      */.   
2d93b 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
2d93c 2d 3e 79 79 65 72 72 63 6e 74 3c 30 20 29 7b 0a  ->yyerrcnt<0 ){.
2d93d 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e 74 61          yy_synta
2d93e 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73 65  x_error(yypParse
2d93f 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f  r,yymajor,yymino
2d940 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d  runion);.      }
2d941 0a 20 20 20 20 20 20 79 79 6d 78 20 3d 20 79 79  .      yymx = yy
2d942 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
2d943 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64  [yypParser->yyid
2d944 78 5d 2e 6d 61 6a 6f 72 3b 0a 20 20 20 20 20 20  x].major;.      
2d945 69 66 28 20 79 79 6d 78 3d 3d 59 59 45 52 52 4f  if( yymx==YYERRO
2d946 52 53 59 4d 42 4f 4c 20 7c 7c 20 79 79 65 72 72  RSYMBOL || yyerr
2d947 6f 72 68 69 74 20 29 7b 0a 23 69 66 6e 64 65 66  orhit ){.#ifndef
2d948 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20 20   NDEBUG.        
2d949 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
2d94a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72  ){.          fpr
2d94b 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
2d94c 2c 22 25 73 44 69 73 63 61 72 64 20 69 6e 70 75  ,"%sDiscard inpu
2d94d 74 20 74 6f 6b 65 6e 20 25 73 5c 6e 22 2c 0a 20  t token %s\n",. 
2d94e 20 20 20 20 20 20 20 20 20 20 20 20 79 79 54 72              yyTr
2d94f 61 63 65 50 72 6f 6d 70 74 2c 79 79 54 6f 6b 65  acePrompt,yyToke
2d950 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29 3b  nName[yymajor]);
2d951 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  .        }.#endi
2d952 66 0a 20 20 20 20 20 20 20 20 79 79 5f 64 65 73  f.        yy_des
2d953 74 72 75 63 74 6f 72 28 79 79 70 50 61 72 73 65  tructor(yypParse
2d954 72 2c 20 28 59 59 43 4f 44 45 54 59 50 45 29 79  r, (YYCODETYPE)y
2d955 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75  ymajor,&yyminoru
2d956 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 20 20 79  nion);.        y
2d957 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44  ymajor = YYNOCOD
2d958 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  E;.      }else{.
2d959 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 0a           while(.
2d95a 20 20 20 20 20 20 20 20 20 20 79 79 70 50 61 72            yypPar
2d95b 73 65 72 2d 3e 79 79 69 64 78 20 3e 3d 20 30 20  ser->yyidx >= 0 
2d95c 26 26 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d  &&.          yym
2d95d 78 20 21 3d 20 59 59 45 52 52 4f 52 53 59 4d 42  x != YYERRORSYMB
2d95e 4f 4c 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  OL &&.          
2d95f 28 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64  (yyact = yy_find
2d960 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a  _reduce_action(.
2d961 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d962 20 20 20 20 20 20 20 20 79 79 70 50 61 72 73 65          yypParse
2d963 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61  r->yystack[yypPa
2d964 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61  rser->yyidx].sta
2d965 74 65 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20  teno,.          
2d966 20 20 20 20 20 20 20 20 20 20 20 20 20 20 59 59                YY
2d967 45 52 52 4f 52 53 59 4d 42 4f 4c 29 29 20 3e 3d  ERRORSYMBOL)) >=
2d968 20 59 59 4e 53 54 41 54 45 0a 20 20 20 20 20 20   YYNSTATE.      
2d969 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 79    ){.          y
2d96a 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61  y_pop_parser_sta
2d96b 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ck(yypParser);. 
2d96c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2d96d 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
2d96e 79 79 69 64 78 20 3c 20 30 20 7c 7c 20 79 79 6d  yyidx < 0 || yym
2d96f 61 6a 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ajor==0 ){.     
2d970 20 20 20 20 20 79 79 5f 64 65 73 74 72 75 63 74       yy_destruct
2d971 6f 72 28 79 79 70 50 61 72 73 65 72 2c 28 59 59  or(yypParser,(YY
2d972 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f 72  CODETYPE)yymajor
2d973 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b  ,&yyminorunion);
2d974 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 70 61  .          yy_pa
2d975 72 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61  rse_failed(yypPa
2d976 72 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  rser);.         
2d977 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43   yymajor = YYNOC
2d978 4f 44 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ODE;.        }el
2d979 73 65 20 69 66 28 20 79 79 6d 78 21 3d 59 59 45  se if( yymx!=YYE
2d97a 52 52 4f 52 53 59 4d 42 4f 4c 20 29 7b 0a 20 20  RRORSYMBOL ){.  
2d97b 20 20 20 20 20 20 20 20 59 59 4d 49 4e 4f 52 54          YYMINORT
2d97c 59 50 45 20 75 32 3b 0a 20 20 20 20 20 20 20 20  YPE u2;.        
2d97d 20 20 75 32 2e 59 59 45 52 52 53 59 4d 44 54 20    u2.YYERRSYMDT 
2d97e 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79  = 0;.          y
2d97f 79 5f 73 68 69 66 74 28 79 79 70 50 61 72 73 65  y_shift(yypParse
2d980 72 2c 79 79 61 63 74 2c 59 59 45 52 52 4f 52 53  r,yyact,YYERRORS
2d981 59 4d 42 4f 4c 2c 26 75 32 29 3b 0a 20 20 20 20  YMBOL,&u2);.    
2d982 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2d983 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
2d984 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20 20  yerrcnt = 3;.   
2d985 20 20 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20     yyerrorhit = 
2d986 31 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 59 59 45  1;.#else  /* YYE
2d987 52 52 4f 52 53 59 4d 42 4f 4c 20 69 73 20 6e 6f  RRORSYMBOL is no
2d988 74 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20 20 20  t defined */.   
2d989 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 77 68     /* This is wh
2d98a 61 74 20 77 65 20 64 6f 20 69 66 20 74 68 65 20  at we do if the 
2d98b 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 6e 6f 74  grammar does not
2d98c 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20   define ERROR:. 
2d98d 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
2d98e 20 20 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72    * Report an er
2d98f 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 61 6e 64  ror message, and
2d990 20 74 68 72 6f 77 20 61 77 61 79 20 74 68 65 20   throw away the 
2d991 69 6e 70 75 74 20 74 6f 6b 65 6e 2e 0a 20 20 20  input token..   
2d992 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
2d993 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 74  * If the input t
2d994 6f 6b 65 6e 20 69 73 20 24 2c 20 74 68 65 6e 20  oken is $, then 
2d995 66 61 69 6c 20 74 68 65 20 70 61 72 73 65 2e 0a  fail the parse..
2d996 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2d997 2a 20 41 73 20 62 65 66 6f 72 65 2c 20 73 75 62  * As before, sub
2d998 73 65 71 75 65 6e 74 20 65 72 72 6f 72 20 6d 65  sequent error me
2d999 73 73 61 67 65 73 20 61 72 65 20 73 75 70 70 72  ssages are suppr
2d99a 65 73 73 65 64 20 75 6e 74 69 6c 0a 20 20 20 20  essed until.    
2d99b 20 20 2a 2a 20 74 68 72 65 65 20 69 6e 70 75 74    ** three input
2d99c 20 74 6f 6b 65 6e 73 20 68 61 76 65 20 62 65 65   tokens have bee
2d99d 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 73  n successfully s
2d99e 68 69 66 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f  hifted..      */
2d99f 0a 20 20 20 20 20 20 69 66 28 20 79 79 70 50 61  .      if( yypPa
2d9a0 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c 3d  rser->yyerrcnt<=
2d9a1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f  0 ){.        yy_
2d9a2 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70  syntax_error(yyp
2d9a3 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79  Parser,yymajor,y
2d9a4 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
2d9a5 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 70 50      }.      yypP
2d9a6 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 20  arser->yyerrcnt 
2d9a7 3d 20 33 3b 0a 20 20 20 20 20 20 79 79 5f 64 65  = 3;.      yy_de
2d9a8 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72 73  structor(yypPars
2d9a9 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79  er,(YYCODETYPE)y
2d9aa 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75  ymajor,&yyminoru
2d9ab 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 69 66 28  nion);.      if(
2d9ac 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29 7b   yyendofinput ){
2d9ad 0a 20 20 20 20 20 20 20 20 79 79 5f 70 61 72 73  .        yy_pars
2d9ae 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72 73  e_failed(yypPars
2d9af 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
2d9b0 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e     yymajor = YYN
2d9b1 4f 43 4f 44 45 3b 0a 23 65 6e 64 69 66 0a 20 20  OCODE;.#endif.  
2d9b2 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 79 79    }.  }while( yy
2d9b3 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f 44 45 20  major!=YYNOCODE 
2d9b4 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  && yypParser->yy
2d9b5 69 64 78 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75  idx>=0 );.  retu
2d9b6 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  rn;.}../********
2d9b7 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61  ****** End of pa
2d9b8 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  rse.c **********
2d9b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d9ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d9bb 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2d9bc 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
2d9bd 65 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a  e tokenize.c ***
2d9be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d9bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d9c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
2d9c1 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
2d9c2 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2d9c3 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2d9c4 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2d9c5 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
2d9c6 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2d9c7 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2d9c8 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2d9c9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2d9ca 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2d9cb 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2d9cc 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
2d9cd 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
2d9ce 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
2d9cf 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
2d9d0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
2d9d1 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
2d9d2 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
2d9d3 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
2d9d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d9d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d9d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d9d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2d9d8 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f   An tokenizer fo
2d9d9 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73  r SQL.**.** This
2d9da 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43   file contains C
2d9db 20 63 6f 64 65 20 74 68 61 74 20 73 70 6c 69 74   code that split
2d9dc 73 20 61 6e 20 53 51 4c 20 69 6e 70 75 74 20 73  s an SQL input s
2d9dd 74 72 69 6e 67 20 75 70 20 69 6e 74 6f 0a 2a 2a  tring up into.**
2d9de 20 69 6e 64 69 76 69 64 75 61 6c 20 74 6f 6b 65   individual toke
2d9df 6e 73 20 61 6e 64 20 73 65 6e 64 73 20 74 68 6f  ns and sends tho
2d9e0 73 65 20 74 6f 6b 65 6e 73 20 6f 6e 65 2d 62 79  se tokens one-by
2d9e1 2d 6f 6e 65 20 6f 76 65 72 20 74 6f 20 74 68 65  -one over to the
2d9e2 0a 2a 2a 20 70 61 72 73 65 72 20 66 6f 72 20 61  .** parser for a
2d9e3 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 24  nalysis..**.** $
2d9e4 49 64 3a 20 74 6f 6b 65 6e 69 7a 65 2e 63 2c 76  Id: tokenize.c,v
2d9e5 20 31 2e 31 35 32 20 32 30 30 38 2f 30 39 2f 30   1.152 2008/09/0
2d9e6 31 20 31 35 3a 35 32 3a 31 31 20 64 72 68 20 45  1 15:52:11 drh E
2d9e7 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
2d9e8 68 65 20 63 68 61 72 4d 61 70 28 29 20 6d 61 63  he charMap() mac
2d9e9 72 6f 20 6d 61 70 73 20 61 6c 70 68 61 62 65 74  ro maps alphabet
2d9ea 69 63 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  ic characters in
2d9eb 74 6f 20 74 68 65 69 72 0a 2a 2a 20 6c 6f 77 65  to their.** lowe
2d9ec 72 2d 63 61 73 65 20 41 53 43 49 49 20 65 71 75  r-case ASCII equ
2d9ed 69 76 61 6c 65 6e 74 2e 20 20 4f 6e 20 41 53 43  ivalent.  On ASC
2d9ee 49 49 20 6d 61 63 68 69 6e 65 73 2c 20 74 68 69  II machines, thi
2d9ef 73 20 69 73 20 6a 75 73 74 0a 2a 2a 20 61 6e 20  s is just.** an 
2d9f0 75 70 70 65 72 2d 74 6f 2d 6c 6f 77 65 72 20 63  upper-to-lower c
2d9f1 61 73 65 20 6d 61 70 2e 20 20 4f 6e 20 45 42 43  ase map.  On EBC
2d9f2 44 49 43 20 6d 61 63 68 69 6e 65 73 20 77 65 20  DIC machines we 
2d9f3 61 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20 74 6f 20  also need.** to 
2d9f4 61 64 6a 75 73 74 20 74 68 65 20 65 6e 63 6f 64  adjust the encod
2d9f5 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c 70 68 61  ing.  Only alpha
2d9f6 62 65 74 69 63 20 63 68 61 72 61 63 74 65 72 73  betic characters
2d9f7 20 61 6e 64 20 75 6e 64 65 72 73 63 6f 72 65 73   and underscores
2d9f8 0a 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65 20 74  .** need to be t
2d9f9 72 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f 0a 23 69  ranslated..*/.#i
2d9fa 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49  fdef SQLITE_ASCI
2d9fb 49 0a 23 20 64 65 66 69 6e 65 20 63 68 61 72 4d  I.# define charM
2d9fc 61 70 28 58 29 20 73 71 6c 69 74 65 33 55 70 70  ap(X) sqlite3Upp
2d9fd 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67  erToLower[(unsig
2d9fe 6e 65 64 20 63 68 61 72 29 58 5d 0a 23 65 6e 64  ned char)X].#end
2d9ff 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
2da00 5f 45 42 43 44 49 43 0a 23 20 64 65 66 69 6e 65  _EBCDIC.# define
2da01 20 63 68 61 72 4d 61 70 28 58 29 20 65 62 63 64   charMap(X) ebcd
2da02 69 63 54 6f 41 73 63 69 69 5b 28 75 6e 73 69 67  icToAscii[(unsig
2da03 6e 65 64 20 63 68 61 72 29 58 5d 0a 63 6f 6e 73  ned char)X].cons
2da04 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
2da05 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b 5d 20  ebcdicToAscii[] 
2da06 3d 20 7b 0a 2f 2a 20 30 20 20 20 31 20 20 20 32  = {./* 0   1   2
2da07 20 20 20 33 20 20 20 34 20 20 20 35 20 20 20 36     3   4   5   6
2da08 20 20 20 37 20 20 20 38 20 20 20 39 20 20 20 41     7   8   9   A
2da09 20 20 20 42 20 20 20 43 20 20 20 44 20 20 20 45     B   C   D   E
2da0a 20 20 20 46 20 2a 2f 0a 20 20 20 30 2c 20 20 30     F */.   0,  0
2da0b 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da0c 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da0d 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da0e 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 30 78  ,  0,  0,  /* 0x
2da0f 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30   */.   0,  0,  0
2da10 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da11 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da12 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da13 2c 20 20 30 2c 20 20 2f 2a 20 31 78 20 2a 2f 0a  ,  0,  /* 1x */.
2da14 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30     0,  0,  0,  0
2da15 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da16 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da17 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da18 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 30  ,  /* 2x */.   0
2da19 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da1a 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da1b 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da1c 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
2da1d 2a 20 33 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30  * 3x */.   0,  0
2da1e 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da1f 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da20 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da21 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 34 78  ,  0,  0,  /* 4x
2da22 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30   */.   0,  0,  0
2da23 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da24 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da25 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da26 2c 20 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a  ,  0,  /* 5x */.
2da27 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30     0,  0,  0,  0
2da28 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da29 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da2a 2c 20 20 30 2c 20 39 35 2c 20 20 30 2c 20 20 30  ,  0, 95,  0,  0
2da2b 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 30  ,  /* 6x */.   0
2da2c 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da2d 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da2e 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da2f 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
2da30 2a 20 37 78 20 2a 2f 0a 20 20 20 30 2c 20 39 37  * 7x */.   0, 97
2da31 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31  , 98, 99,100,101
2da32 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35  ,102,103,104,105
2da33 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da34 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 38 78  ,  0,  0,  /* 8x
2da35 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30 37   */.   0,106,107
2da36 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31  ,108,109,110,111
2da37 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20 30  ,112,113,114,  0
2da38 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da39 2c 20 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a  ,  0,  /* 9x */.
2da3a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31 36     0,  0,115,116
2da3b 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30  ,117,118,119,120
2da3c 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20 30  ,121,122,  0,  0
2da3d 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da3e 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 30  ,  /* Ax */.   0
2da3f 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da40 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da41 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da42 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
2da43 2a 20 42 78 20 2a 2f 0a 20 20 20 30 2c 20 39 37  * Bx */.   0, 97
2da44 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31  , 98, 99,100,101
2da45 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35  ,102,103,104,105
2da46 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da47 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 43 78  ,  0,  0,  /* Cx
2da48 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30 37   */.   0,106,107
2da49 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31  ,108,109,110,111
2da4a 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20 30  ,112,113,114,  0
2da4b 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da4c 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a  ,  0,  /* Dx */.
2da4d 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31 36     0,  0,115,116
2da4e 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30  ,117,118,119,120
2da4f 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20 30  ,121,122,  0,  0
2da50 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da51 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 30  ,  /* Ex */.   0
2da52 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da53 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da54 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2da55 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
2da56 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69  * Fx */.};.#endi
2da57 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  f../*.** The sql
2da58 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 20  ite3KeywordCode 
2da59 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b 73 20 75  function looks u
2da5a 70 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20  p an identifier 
2da5b 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 0a  to determine if.
2da5c 2a 2a 20 69 74 20 69 73 20 61 20 6b 65 79 77 6f  ** it is a keywo
2da5d 72 64 2e 20 20 49 66 20 69 74 20 69 73 20 61 20  rd.  If it is a 
2da5e 6b 65 79 77 6f 72 64 2c 20 74 68 65 20 74 6f 6b  keyword, the tok
2da5f 65 6e 20 63 6f 64 65 20 6f 66 20 74 68 61 74 20  en code of that 
2da60 6b 65 79 77 6f 72 64 20 69 73 20 0a 2a 2a 20 72  keyword is .** r
2da61 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65  eturned.  If the
2da62 20 69 6e 70 75 74 20 69 73 20 6e 6f 74 20 61 20   input is not a 
2da63 6b 65 79 77 6f 72 64 2c 20 54 4b 5f 49 44 20 69  keyword, TK_ID i
2da64 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
2da65 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  * The implementa
2da66 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75  tion of this rou
2da67 74 69 6e 65 20 77 61 73 20 67 65 6e 65 72 61 74  tine was generat
2da68 65 64 20 62 79 20 61 20 70 72 6f 67 72 61 6d 2c  ed by a program,
2da69 0a 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73  .** mkkeywordhas
2da6a 68 2e 68 2c 20 6c 6f 63 61 74 65 64 20 69 6e 20  h.h, located in 
2da6b 74 68 65 20 74 6f 6f 6c 20 73 75 62 64 69 72 65  the tool subdire
2da6c 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64 69 73  ctory of the dis
2da6d 74 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a 20 54 68  tribution..** Th
2da6e 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20  e output of the 
2da6f 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63 20  mkkeywordhash.c 
2da70 70 72 6f 67 72 61 6d 20 69 73 20 77 72 69 74 74  program is writt
2da71 65 6e 20 69 6e 74 6f 20 61 20 66 69 6c 65 0a 2a  en into a file.*
2da72 2a 20 6e 61 6d 65 64 20 6b 65 79 77 6f 72 64 68  * named keywordh
2da73 61 73 68 2e 68 20 61 6e 64 20 74 68 65 6e 20 69  ash.h and then i
2da74 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 69  ncluded into thi
2da75 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 62 79  s source file by
2da76 0a 2a 2a 20 74 68 65 20 23 69 6e 63 6c 75 64 65  .** the #include
2da77 20 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a   below..*/./****
2da78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
2da79 64 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68  de keywordhash.h
2da7a 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
2da7b 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a  f tokenize.c ***
2da7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2da7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2da7e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64 68 61 73   file keywordhas
2da7f 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.h ************
2da80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2da81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2da82 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
2da83 61 69 6e 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  ains automatical
2da84 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  ly generated cod
2da85 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  e ******.**.** T
2da86 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
2da87 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 61 75  file has been au
2da88 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65  tomatically gene
2da89 72 61 74 65 64 20 62 79 0a 2a 2a 0a 2a 2a 20 20  rated by.**.**  
2da8a 20 20 20 24 48 65 61 64 65 72 3a 20 2f 73 71 6c     $Header: /sql
2da8b 69 74 65 2f 73 71 6c 69 74 65 2f 74 6f 6f 6c 2f  ite/sqlite/tool/
2da8c 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63 2c  mkkeywordhash.c,
2da8d 76 20 31 2e 33 36 20 32 30 30 38 2f 31 32 2f 33  v 1.36 2008/12/3
2da8e 31 20 32 31 3a 35 32 3a 34 31 20 64 72 68 20 45  1 21:52:41 drh E
2da8f 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  xp $.**.** The c
2da90 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
2da91 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75   implements a fu
2da92 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 65 74 65  nction that dete
2da93 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 0a 2a  rmines whether.*
2da94 2a 20 6f 72 20 6e 6f 74 20 61 20 67 69 76 65 6e  * or not a given
2da95 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 20 72   identifier is r
2da96 65 61 6c 6c 79 20 61 6e 20 53 51 4c 20 6b 65 79  eally an SQL key
2da97 77 6f 72 64 2e 20 20 54 68 65 20 73 61 6d 65 20  word.  The same 
2da98 74 68 69 6e 67 0a 2a 2a 20 6d 69 67 68 74 20 62  thing.** might b
2da99 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6d 6f  e implemented mo
2da9a 72 65 20 64 69 72 65 63 74 6c 79 20 75 73 69 6e  re directly usin
2da9b 67 20 61 20 68 61 6e 64 2d 77 72 69 74 74 65 6e  g a hand-written
2da9c 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2a 20   hash table..** 
2da9d 42 75 74 20 62 79 20 75 73 69 6e 67 20 74 68 69  But by using thi
2da9e 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
2da9f 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 2c 20  generated code, 
2daa0 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
2daa1 63 6f 64 65 0a 2a 2a 20 69 73 20 73 75 62 73 74  code.** is subst
2daa2 61 6e 74 69 61 6c 6c 79 20 72 65 64 75 63 65 64  antially reduced
2daa3 2e 20 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72  .  This is impor
2daa4 74 61 6e 74 20 66 6f 72 20 65 6d 62 65 64 64 65  tant for embedde
2daa5 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a  d applications.*
2daa6 2a 20 6f 6e 20 70 6c 61 74 66 6f 72 6d 73 20 77  * on platforms w
2daa7 69 74 68 20 6c 69 6d 69 74 65 64 20 6d 65 6d 6f  ith limited memo
2daa8 72 79 2e 0a 2a 2f 0a 2f 2a 20 48 61 73 68 20 73  ry..*/./* Hash s
2daa9 63 6f 72 65 3a 20 31 37 31 20 2a 2f 0a 73 74 61  core: 171 */.sta
2daaa 74 69 63 20 69 6e 74 20 6b 65 79 77 6f 72 64 43  tic int keywordC
2daab 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ode(const char *
2daac 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 2f 2a 20  z, int n){.  /* 
2daad 7a 54 65 78 74 5b 5d 20 65 6e 63 6f 64 65 73 20  zText[] encodes 
2daae 38 30 31 20 62 79 74 65 73 20 6f 66 20 6b 65 79  801 bytes of key
2daaf 77 6f 72 64 73 20 69 6e 20 35 34 31 20 62 79 74  words in 541 byt
2dab0 65 73 20 2a 2f 0a 20 20 2f 2a 20 20 20 52 45 49  es */.  /*   REI
2dab1 4e 44 45 58 45 44 45 53 43 41 50 45 41 43 48 45  NDEXEDESCAPEACHE
2dab2 43 4b 45 59 42 45 46 4f 52 45 49 47 4e 4f 52 45  CKEYBEFOREIGNORE
2dab3 47 45 58 50 4c 41 49 4e 53 54 45 41 44 44 41 54  GEXPLAINSTEADDAT
2dab4 41 42 41 53 45 4c 45 43 54 20 20 20 20 20 20 20  ABASELECT       
2dab5 2a 2f 0a 20 20 2f 2a 20 20 20 41 42 4c 45 46 54  */.  /*   ABLEFT
2dab6 48 45 4e 44 45 46 45 52 52 41 42 4c 45 4c 53 45  HENDEFERRABLELSE
2dab7 58 43 45 50 54 52 41 4e 53 41 43 54 49 4f 4e 41  XCEPTRANSACTIONA
2dab8 54 55 52 41 4c 54 45 52 41 49 53 45 58 43 4c 55  TURALTERAISEXCLU
2dab9 53 49 56 45 20 20 20 20 20 20 20 20 20 2a 2f 0a  SIVE         */.
2daba 20 20 2f 2a 20 20 20 58 49 53 54 53 41 56 45 50    /*   XISTSAVEP
2dabb 4f 49 4e 54 45 52 53 45 43 54 52 49 47 47 45 52  OINTERSECTRIGGER
2dabc 45 46 45 52 45 4e 43 45 53 43 4f 4e 53 54 52 41  EFERENCESCONSTRA
2dabd 49 4e 54 4f 46 46 53 45 54 45 4d 50 4f 52 41 52  INTOFFSETEMPORAR
2dabe 59 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f  Y         */.  /
2dabf 2a 20 20 20 55 4e 49 51 55 45 52 59 41 54 54 41  *   UNIQUERYATTA
2dac0 43 48 41 56 49 4e 47 52 4f 55 50 44 41 54 45 42  CHAVINGROUPDATEB
2dac1 45 47 49 4e 4e 45 52 45 4c 45 41 53 45 42 45 54  EGINNERELEASEBET
2dac2 57 45 45 4e 4f 54 4e 55 4c 4c 49 4b 45 20 20 20  WEENOTNULLIKE   
2dac3 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20         */.  /*  
2dac4 20 43 41 53 43 41 44 45 4c 45 54 45 43 41 53 45   CASCADELETECASE
2dac5 43 4f 4c 4c 41 54 45 43 52 45 41 54 45 43 55 52  COLLATECREATECUR
2dac6 52 45 4e 54 5f 44 41 54 45 44 45 54 41 43 48 49  RENT_DATEDETACHI
2dac7 4d 4d 45 44 49 41 54 45 4a 4f 49 4e 20 20 20 20  MMEDIATEJOIN    
2dac8 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 53 45      */.  /*   SE
2dac9 52 54 4d 41 54 43 48 50 4c 41 4e 41 4c 59 5a 45  RTMATCHPLANALYZE
2daca 50 52 41 47 4d 41 42 4f 52 54 56 41 4c 55 45 53  PRAGMABORTVALUES
2dacb 56 49 52 54 55 41 4c 49 4d 49 54 57 48 45 4e 57  VIRTUALIMITWHENW
2dacc 48 45 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20  HERENAME        
2dacd 20 2a 2f 0a 20 20 2f 2a 20 20 20 41 46 54 45 52   */.  /*   AFTER
2dace 45 50 4c 41 43 45 41 4e 44 45 46 41 55 4c 54 41  EPLACEANDEFAULTA
2dacf 55 54 4f 49 4e 43 52 45 4d 45 4e 54 43 41 53 54  UTOINCREMENTCAST
2dad0 43 4f 4c 55 4d 4e 43 4f 4d 4d 49 54 43 4f 4e 46  COLUMNCOMMITCONF
2dad1 4c 49 43 54 43 52 4f 53 53 20 20 20 20 20 2a 2f  LICTCROSS     */
2dad2 0a 20 20 2f 2a 20 20 20 43 55 52 52 45 4e 54 5f  .  /*   CURRENT_
2dad3 54 49 4d 45 53 54 41 4d 50 52 49 4d 41 52 59 44  TIMESTAMPRIMARYD
2dad4 45 46 45 52 52 45 44 49 53 54 49 4e 43 54 44 52  EFERREDISTINCTDR
2dad5 4f 50 46 41 49 4c 46 52 4f 4d 46 55 4c 4c 47 4c  OPFAILFROMFULLGL
2dad6 4f 42 59 49 46 20 20 20 20 20 20 2a 2f 0a 20 20  OBYIF      */.  
2dad7 2f 2a 20 20 20 49 53 4e 55 4c 4c 4f 52 44 45 52  /*   ISNULLORDER
2dad8 45 53 54 52 49 43 54 4f 55 54 45 52 49 47 48 54  ESTRICTOUTERIGHT
2dad9 52 4f 4c 4c 42 41 43 4b 52 4f 57 55 4e 49 4f 4e  ROLLBACKROWUNION
2dada 55 53 49 4e 47 56 41 43 55 55 4d 56 49 45 57 20  USINGVACUUMVIEW 
2dadb 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20          */.  /* 
2dadc 20 20 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20    INITIALLY     
2dadd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dade 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dadf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dae0 20 20 20 20 20 2a 2f 0a 20 20 73 74 61 74 69 63       */.  static
2dae1 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 54 65 78   const char zTex
2dae2 74 5b 35 34 30 5d 20 3d 20 7b 0a 20 20 20 20 27  t[540] = {.    '
2dae3 52 27 2c 27 45 27 2c 27 49 27 2c 27 4e 27 2c 27  R','E','I','N','
2dae4 44 27 2c 27 45 27 2c 27 58 27 2c 27 45 27 2c 27  D','E','X','E','
2dae5 44 27 2c 27 45 27 2c 27 53 27 2c 27 43 27 2c 27  D','E','S','C','
2dae6 41 27 2c 27 50 27 2c 27 45 27 2c 27 41 27 2c 27  A','P','E','A','
2dae7 43 27 2c 27 48 27 2c 0a 20 20 20 20 27 45 27 2c  C','H',.    'E',
2dae8 27 43 27 2c 27 4b 27 2c 27 45 27 2c 27 59 27 2c  'C','K','E','Y',
2dae9 27 42 27 2c 27 45 27 2c 27 46 27 2c 27 4f 27 2c  'B','E','F','O',
2daea 27 52 27 2c 27 45 27 2c 27 49 27 2c 27 47 27 2c  'R','E','I','G',
2daeb 27 4e 27 2c 27 4f 27 2c 27 52 27 2c 27 45 27 2c  'N','O','R','E',
2daec 27 47 27 2c 0a 20 20 20 20 27 45 27 2c 27 58 27  'G',.    'E','X'
2daed 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c 27 49 27  ,'P','L','A','I'
2daee 2c 27 4e 27 2c 27 53 27 2c 27 54 27 2c 27 45 27  ,'N','S','T','E'
2daef 2c 27 41 27 2c 27 44 27 2c 27 44 27 2c 27 41 27  ,'A','D','D','A'
2daf0 2c 27 54 27 2c 27 41 27 2c 27 42 27 2c 27 41 27  ,'T','A','B','A'
2daf1 2c 0a 20 20 20 20 27 53 27 2c 27 45 27 2c 27 4c  ,.    'S','E','L
2daf2 27 2c 27 45 27 2c 27 43 27 2c 27 54 27 2c 27 41  ','E','C','T','A
2daf3 27 2c 27 42 27 2c 27 4c 27 2c 27 45 27 2c 27 46  ','B','L','E','F
2daf4 27 2c 27 54 27 2c 27 48 27 2c 27 45 27 2c 27 4e  ','T','H','E','N
2daf5 27 2c 27 44 27 2c 27 45 27 2c 27 46 27 2c 0a 20  ','D','E','F',. 
2daf6 20 20 20 27 45 27 2c 27 52 27 2c 27 52 27 2c 27     'E','R','R','
2daf7 41 27 2c 27 42 27 2c 27 4c 27 2c 27 45 27 2c 27  A','B','L','E','
2daf8 4c 27 2c 27 53 27 2c 27 45 27 2c 27 58 27 2c 27  L','S','E','X','
2daf9 43 27 2c 27 45 27 2c 27 50 27 2c 27 54 27 2c 27  C','E','P','T','
2dafa 52 27 2c 27 41 27 2c 27 4e 27 2c 0a 20 20 20 20  R','A','N',.    
2dafb 27 53 27 2c 27 41 27 2c 27 43 27 2c 27 54 27 2c  'S','A','C','T',
2dafc 27 49 27 2c 27 4f 27 2c 27 4e 27 2c 27 41 27 2c  'I','O','N','A',
2dafd 27 54 27 2c 27 55 27 2c 27 52 27 2c 27 41 27 2c  'T','U','R','A',
2dafe 27 4c 27 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c  'L','T','E','R',
2daff 27 41 27 2c 27 49 27 2c 0a 20 20 20 20 27 53 27  'A','I',.    'S'
2db00 2c 27 45 27 2c 27 58 27 2c 27 43 27 2c 27 4c 27  ,'E','X','C','L'
2db01 2c 27 55 27 2c 27 53 27 2c 27 49 27 2c 27 56 27  ,'U','S','I','V'
2db02 2c 27 45 27 2c 27 58 27 2c 27 49 27 2c 27 53 27  ,'E','X','I','S'
2db03 2c 27 54 27 2c 27 53 27 2c 27 41 27 2c 27 56 27  ,'T','S','A','V'
2db04 2c 27 45 27 2c 0a 20 20 20 20 27 50 27 2c 27 4f  ,'E',.    'P','O
2db05 27 2c 27 49 27 2c 27 4e 27 2c 27 54 27 2c 27 45  ','I','N','T','E
2db06 27 2c 27 52 27 2c 27 53 27 2c 27 45 27 2c 27 43  ','R','S','E','C
2db07 27 2c 27 54 27 2c 27 52 27 2c 27 49 27 2c 27 47  ','T','R','I','G
2db08 27 2c 27 47 27 2c 27 45 27 2c 27 52 27 2c 27 45  ','G','E','R','E
2db09 27 2c 0a 20 20 20 20 27 46 27 2c 27 45 27 2c 27  ',.    'F','E','
2db0a 52 27 2c 27 45 27 2c 27 4e 27 2c 27 43 27 2c 27  R','E','N','C','
2db0b 45 27 2c 27 53 27 2c 27 43 27 2c 27 4f 27 2c 27  E','S','C','O','
2db0c 4e 27 2c 27 53 27 2c 27 54 27 2c 27 52 27 2c 27  N','S','T','R','
2db0d 41 27 2c 27 49 27 2c 27 4e 27 2c 27 54 27 2c 0a  A','I','N','T',.
2db0e 20 20 20 20 27 4f 27 2c 27 46 27 2c 27 46 27 2c      'O','F','F',
2db0f 27 53 27 2c 27 45 27 2c 27 54 27 2c 27 45 27 2c  'S','E','T','E',
2db10 27 4d 27 2c 27 50 27 2c 27 4f 27 2c 27 52 27 2c  'M','P','O','R',
2db11 27 41 27 2c 27 52 27 2c 27 59 27 2c 27 55 27 2c  'A','R','Y','U',
2db12 27 4e 27 2c 27 49 27 2c 27 51 27 2c 0a 20 20 20  'N','I','Q',.   
2db13 20 27 55 27 2c 27 45 27 2c 27 52 27 2c 27 59 27   'U','E','R','Y'
2db14 2c 27 41 27 2c 27 54 27 2c 27 54 27 2c 27 41 27  ,'A','T','T','A'
2db15 2c 27 43 27 2c 27 48 27 2c 27 41 27 2c 27 56 27  ,'C','H','A','V'
2db16 2c 27 49 27 2c 27 4e 27 2c 27 47 27 2c 27 52 27  ,'I','N','G','R'
2db17 2c 27 4f 27 2c 27 55 27 2c 0a 20 20 20 20 27 50  ,'O','U',.    'P
2db18 27 2c 27 44 27 2c 27 41 27 2c 27 54 27 2c 27 45  ','D','A','T','E
2db19 27 2c 27 42 27 2c 27 45 27 2c 27 47 27 2c 27 49  ','B','E','G','I
2db1a 27 2c 27 4e 27 2c 27 4e 27 2c 27 45 27 2c 27 52  ','N','N','E','R
2db1b 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27 41  ','E','L','E','A
2db1c 27 2c 27 53 27 2c 0a 20 20 20 20 27 45 27 2c 27  ','S',.    'E','
2db1d 42 27 2c 27 45 27 2c 27 54 27 2c 27 57 27 2c 27  B','E','T','W','
2db1e 45 27 2c 27 45 27 2c 27 4e 27 2c 27 4f 27 2c 27  E','E','N','O','
2db1f 54 27 2c 27 4e 27 2c 27 55 27 2c 27 4c 27 2c 27  T','N','U','L','
2db20 4c 27 2c 27 49 27 2c 27 4b 27 2c 27 45 27 2c 27  L','I','K','E','
2db21 43 27 2c 0a 20 20 20 20 27 41 27 2c 27 53 27 2c  C',.    'A','S',
2db22 27 43 27 2c 27 41 27 2c 27 44 27 2c 27 45 27 2c  'C','A','D','E',
2db23 27 4c 27 2c 27 45 27 2c 27 54 27 2c 27 45 27 2c  'L','E','T','E',
2db24 27 43 27 2c 27 41 27 2c 27 53 27 2c 27 45 27 2c  'C','A','S','E',
2db25 27 43 27 2c 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c  'C','O','L','L',
2db26 0a 20 20 20 20 27 41 27 2c 27 54 27 2c 27 45 27  .    'A','T','E'
2db27 2c 27 43 27 2c 27 52 27 2c 27 45 27 2c 27 41 27  ,'C','R','E','A'
2db28 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c 27 55 27  ,'T','E','C','U'
2db29 2c 27 52 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27  ,'R','R','E','N'
2db2a 2c 27 54 27 2c 27 5f 27 2c 27 44 27 2c 0a 20 20  ,'T','_','D',.  
2db2b 20 20 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 44    'A','T','E','D
2db2c 27 2c 27 45 27 2c 27 54 27 2c 27 41 27 2c 27 43  ','E','T','A','C
2db2d 27 2c 27 48 27 2c 27 49 27 2c 27 4d 27 2c 27 4d  ','H','I','M','M
2db2e 27 2c 27 45 27 2c 27 44 27 2c 27 49 27 2c 27 41  ','E','D','I','A
2db2f 27 2c 27 54 27 2c 27 45 27 2c 0a 20 20 20 20 27  ','T','E',.    '
2db30 4a 27 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27  J','O','I','N','
2db31 53 27 2c 27 45 27 2c 27 52 27 2c 27 54 27 2c 27  S','E','R','T','
2db32 4d 27 2c 27 41 27 2c 27 54 27 2c 27 43 27 2c 27  M','A','T','C','
2db33 48 27 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c 27  H','P','L','A','
2db34 4e 27 2c 27 41 27 2c 0a 20 20 20 20 27 4c 27 2c  N','A',.    'L',
2db35 27 59 27 2c 27 5a 27 2c 27 45 27 2c 27 50 27 2c  'Y','Z','E','P',
2db36 27 52 27 2c 27 41 27 2c 27 47 27 2c 27 4d 27 2c  'R','A','G','M',
2db37 27 41 27 2c 27 42 27 2c 27 4f 27 2c 27 52 27 2c  'A','B','O','R',
2db38 27 54 27 2c 27 56 27 2c 27 41 27 2c 27 4c 27 2c  'T','V','A','L',
2db39 27 55 27 2c 0a 20 20 20 20 27 45 27 2c 27 53 27  'U',.    'E','S'
2db3a 2c 27 56 27 2c 27 49 27 2c 27 52 27 2c 27 54 27  ,'V','I','R','T'
2db3b 2c 27 55 27 2c 27 41 27 2c 27 4c 27 2c 27 49 27  ,'U','A','L','I'
2db3c 2c 27 4d 27 2c 27 49 27 2c 27 54 27 2c 27 57 27  ,'M','I','T','W'
2db3d 2c 27 48 27 2c 27 45 27 2c 27 4e 27 2c 27 57 27  ,'H','E','N','W'
2db3e 2c 0a 20 20 20 20 27 48 27 2c 27 45 27 2c 27 52  ,.    'H','E','R
2db3f 27 2c 27 45 27 2c 27 4e 27 2c 27 41 27 2c 27 4d  ','E','N','A','M
2db40 27 2c 27 45 27 2c 27 41 27 2c 27 46 27 2c 27 54  ','E','A','F','T
2db41 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 50  ','E','R','E','P
2db42 27 2c 27 4c 27 2c 27 41 27 2c 27 43 27 2c 0a 20  ','L','A','C',. 
2db43 20 20 20 27 45 27 2c 27 41 27 2c 27 4e 27 2c 27     'E','A','N','
2db44 44 27 2c 27 45 27 2c 27 46 27 2c 27 41 27 2c 27  D','E','F','A','
2db45 55 27 2c 27 4c 27 2c 27 54 27 2c 27 41 27 2c 27  U','L','T','A','
2db46 55 27 2c 27 54 27 2c 27 4f 27 2c 27 49 27 2c 27  U','T','O','I','
2db47 4e 27 2c 27 43 27 2c 27 52 27 2c 0a 20 20 20 20  N','C','R',.    
2db48 27 45 27 2c 27 4d 27 2c 27 45 27 2c 27 4e 27 2c  'E','M','E','N',
2db49 27 54 27 2c 27 43 27 2c 27 41 27 2c 27 53 27 2c  'T','C','A','S',
2db4a 27 54 27 2c 27 43 27 2c 27 4f 27 2c 27 4c 27 2c  'T','C','O','L',
2db4b 27 55 27 2c 27 4d 27 2c 27 4e 27 2c 27 43 27 2c  'U','M','N','C',
2db4c 27 4f 27 2c 27 4d 27 2c 0a 20 20 20 20 27 4d 27  'O','M',.    'M'
2db4d 2c 27 49 27 2c 27 54 27 2c 27 43 27 2c 27 4f 27  ,'I','T','C','O'
2db4e 2c 27 4e 27 2c 27 46 27 2c 27 4c 27 2c 27 49 27  ,'N','F','L','I'
2db4f 2c 27 43 27 2c 27 54 27 2c 27 43 27 2c 27 52 27  ,'C','T','C','R'
2db50 2c 27 4f 27 2c 27 53 27 2c 27 53 27 2c 27 43 27  ,'O','S','S','C'
2db51 2c 27 55 27 2c 0a 20 20 20 20 27 52 27 2c 27 52  ,'U',.    'R','R
2db52 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27 5f  ','E','N','T','_
2db53 27 2c 27 54 27 2c 27 49 27 2c 27 4d 27 2c 27 45  ','T','I','M','E
2db54 27 2c 27 53 27 2c 27 54 27 2c 27 41 27 2c 27 4d  ','S','T','A','M
2db55 27 2c 27 50 27 2c 27 52 27 2c 27 49 27 2c 27 4d  ','P','R','I','M
2db56 27 2c 0a 20 20 20 20 27 41 27 2c 27 52 27 2c 27  ',.    'A','R','
2db57 59 27 2c 27 44 27 2c 27 45 27 2c 27 46 27 2c 27  Y','D','E','F','
2db58 45 27 2c 27 52 27 2c 27 52 27 2c 27 45 27 2c 27  E','R','R','E','
2db59 44 27 2c 27 49 27 2c 27 53 27 2c 27 54 27 2c 27  D','I','S','T','
2db5a 49 27 2c 27 4e 27 2c 27 43 27 2c 27 54 27 2c 0a  I','N','C','T',.
2db5b 20 20 20 20 27 44 27 2c 27 52 27 2c 27 4f 27 2c      'D','R','O',
2db5c 27 50 27 2c 27 46 27 2c 27 41 27 2c 27 49 27 2c  'P','F','A','I',
2db5d 27 4c 27 2c 27 46 27 2c 27 52 27 2c 27 4f 27 2c  'L','F','R','O',
2db5e 27 4d 27 2c 27 46 27 2c 27 55 27 2c 27 4c 27 2c  'M','F','U','L',
2db5f 27 4c 27 2c 27 47 27 2c 27 4c 27 2c 0a 20 20 20  'L','G','L',.   
2db60 20 27 4f 27 2c 27 42 27 2c 27 59 27 2c 27 49 27   'O','B','Y','I'
2db61 2c 27 46 27 2c 27 49 27 2c 27 53 27 2c 27 4e 27  ,'F','I','S','N'
2db62 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 4f 27  ,'U','L','L','O'
2db63 2c 27 52 27 2c 27 44 27 2c 27 45 27 2c 27 52 27  ,'R','D','E','R'
2db64 2c 27 45 27 2c 27 53 27 2c 0a 20 20 20 20 27 54  ,'E','S',.    'T
2db65 27 2c 27 52 27 2c 27 49 27 2c 27 43 27 2c 27 54  ','R','I','C','T
2db66 27 2c 27 4f 27 2c 27 55 27 2c 27 54 27 2c 27 45  ','O','U','T','E
2db67 27 2c 27 52 27 2c 27 49 27 2c 27 47 27 2c 27 48  ','R','I','G','H
2db68 27 2c 27 54 27 2c 27 52 27 2c 27 4f 27 2c 27 4c  ','T','R','O','L
2db69 27 2c 27 4c 27 2c 0a 20 20 20 20 27 42 27 2c 27  ','L',.    'B','
2db6a 41 27 2c 27 43 27 2c 27 4b 27 2c 27 52 27 2c 27  A','C','K','R','
2db6b 4f 27 2c 27 57 27 2c 27 55 27 2c 27 4e 27 2c 27  O','W','U','N','
2db6c 49 27 2c 27 4f 27 2c 27 4e 27 2c 27 55 27 2c 27  I','O','N','U','
2db6d 53 27 2c 27 49 27 2c 27 4e 27 2c 27 47 27 2c 27  S','I','N','G','
2db6e 56 27 2c 0a 20 20 20 20 27 41 27 2c 27 43 27 2c  V',.    'A','C',
2db6f 27 55 27 2c 27 55 27 2c 27 4d 27 2c 27 56 27 2c  'U','U','M','V',
2db70 27 49 27 2c 27 45 27 2c 27 57 27 2c 27 49 27 2c  'I','E','W','I',
2db71 27 4e 27 2c 27 49 27 2c 27 54 27 2c 27 49 27 2c  'N','I','T','I',
2db72 27 41 27 2c 27 4c 27 2c 27 4c 27 2c 27 59 27 2c  'A','L','L','Y',
2db73 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  .  };.  static c
2db74 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
2db75 61 72 20 61 48 61 73 68 5b 31 32 37 5d 20 3d 20  ar aHash[127] = 
2db76 7b 0a 20 20 20 20 20 20 37 30 2c 20 20 39 39 2c  {.      70,  99,
2db77 20 31 31 32 2c 20 20 36 38 2c 20 20 20 30 2c 20   112,  68,   0, 
2db78 20 34 33 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   43,   0,   0,  
2db79 37 36 2c 20 20 20 30 2c 20 20 37 31 2c 20 20 20  76,   0,  71,   
2db7a 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 34 31  0,   0,.      41
2db7b 2c 20 20 31 32 2c 20 20 37 32 2c 20 20 31 35 2c  ,  12,  72,  15,
2db7c 20 20 20 30 2c 20 31 31 31 2c 20 20 37 39 2c 20     0, 111,  79, 
2db7d 20 34 39 2c 20 31 30 36 2c 20 20 20 30 2c 20 20   49, 106,   0,  
2db7e 31 39 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20  19,   0,   0,.  
2db7f 20 20 20 31 31 36 2c 20 20 20 30 2c 20 31 31 34     116,   0, 114
2db80 2c 20 31 30 39 2c 20 20 20 30 2c 20 20 32 32 2c  , 109,   0,  22,
2db81 20 20 38 37 2c 20 20 20 30 2c 20 20 20 39 2c 20    87,   0,   9, 
2db82 20 20 30 2c 20 20 20 30 2c 20 20 36 34 2c 20 20    0,   0,  64,  
2db83 36 35 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 36  65,.       0,  6
2db84 33 2c 20 20 20 36 2c 20 20 20 30 2c 20 20 34 37  3,   6,   0,  47
2db85 2c 20 20 38 34 2c 20 20 39 36 2c 20 20 20 30 2c  ,  84,  96,   0,
2db86 20 31 31 33 2c 20 20 39 35 2c 20 20 20 30 2c 20   113,  95,   0, 
2db87 20 20 30 2c 20 20 34 34 2c 0a 20 20 20 20 20 20    0,  44,.      
2db88 20 30 2c 20 20 39 37 2c 20 20 32 34 2c 20 20 20   0,  97,  24,   
2db89 30 2c 20 20 31 37 2c 20 20 20 30 2c 20 31 31 37  0,  17,   0, 117
2db8a 2c 20 20 34 38 2c 20 20 32 33 2c 20 20 20 30 2c  ,  48,  23,   0,
2db8b 20 20 20 35 2c 20 31 30 34 2c 20 20 32 35 2c 0a     5, 104,  25,.
2db8c 20 20 20 20 20 20 39 30 2c 20 20 20 30 2c 20 20        90,   0,  
2db8d 20 30 2c 20 31 31 39 2c 20 31 30 30 2c 20 20 35   0, 119, 100,  5
2db8e 35 2c 20 31 31 38 2c 20 20 35 32 2c 20 20 20 37  5, 118,  52,   7
2db8f 2c 20 20 35 30 2c 20 20 20 30 2c 20 20 38 35 2c  ,  50,   0,  85,
2db90 20 20 20 30 2c 0a 20 20 20 20 20 20 39 34 2c 20     0,.      94, 
2db91 20 32 36 2c 20 20 20 30 2c 20 20 39 33 2c 20 20   26,   0,  93,  
2db92 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 38   0,   0,   0,  8
2db93 39 2c 20 20 38 36 2c 20 20 39 31 2c 20 20 38 32  9,  86,  91,  82
2db94 2c 20 31 30 33 2c 20 20 31 34 2c 0a 20 20 20 20  , 103,  14,.    
2db95 20 20 33 38 2c 20 31 30 32 2c 20 20 20 30 2c 20    38, 102,   0, 
2db96 20 37 35 2c 20 20 20 30 2c 20 20 31 38 2c 20 20   75,   0,  18,  
2db97 38 33 2c 20 31 30 35 2c 20 20 33 31 2c 20 20 20  83, 105,  31,   
2db98 30 2c 20 31 31 35 2c 20 20 37 34 2c 20 31 30 37  0, 115,  74, 107
2db99 2c 0a 20 20 20 20 20 20 35 37 2c 20 20 34 35 2c  ,.      57,  45,
2db9a 20 20 37 38 2c 20 20 20 30 2c 20 20 20 30 2c 20    78,   0,   0, 
2db9b 20 38 38 2c 20 20 33 39 2c 20 20 20 30 2c 20 31   88,  39,   0, 1
2db9c 31 30 2c 20 20 20 30 2c 20 20 33 35 2c 20 20 20  10,   0,  35,   
2db9d 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 32 38  0,   0,.      28
2db9e 2c 20 20 20 30 2c 20 20 38 30 2c 20 20 35 33 2c  ,   0,  80,  53,
2db9f 20 20 35 38 2c 20 20 20 30 2c 20 20 32 30 2c 20    58,   0,  20, 
2dba0 20 35 36 2c 20 20 20 30 2c 20 20 35 31 2c 0a 20   56,   0,  51,. 
2dba1 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   };.  static con
2dba2 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2dba3 20 61 4e 65 78 74 5b 31 31 39 5d 20 3d 20 7b 0a   aNext[119] = {.
2dba4 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20         0,   0,  
2dba5 20 30 2c 20 20 20 30 2c 20 20 20 34 2c 20 20 20   0,   0,   4,   
2dba6 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2dba7 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2dba8 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20     0,.       0, 
2dba9 20 20 32 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    2,   0,   0,  
2dbaa 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
2dbab 30 2c 20 20 31 33 2c 20 20 20 30 2c 20 20 20 30  0,  13,   0,   0
2dbac 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20  ,   0,   0,.    
2dbad 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2dbae 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2dbaf 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
2dbb0 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2dbb1 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c  ,.       0,   0,
2dbb2 20 20 20 30 2c 20 20 20 30 2c 20 20 33 32 2c 20     0,   0,  32, 
2dbb3 20 32 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   21,   0,   0,  
2dbb4 20 30 2c 20 20 34 32 2c 20 20 20 33 2c 20 20 34   0,  42,   3,  4
2dbb5 36 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30  6,   0,.       0
2dbb6 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 32 39 2c  ,   0,   0,  29,
2dbb7 20 20 20 30 2c 20 20 20 30 2c 20 20 33 37 2c 20     0,   0,  37, 
2dbb8 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2dbb9 20 31 2c 20 20 36 30 2c 20 20 20 30 2c 0a 20 20   1,  60,   0,.  
2dbba 20 20 20 20 20 30 2c 20 20 36 31 2c 20 20 20 30       0,  61,   0
2dbbb 2c 20 20 34 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,  40,   0,   0,
2dbbc 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2dbbd 20 20 30 2c 20 20 20 30 2c 20 20 35 39 2c 20 20    0,   0,  59,  
2dbbe 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20   0,.       0,   
2dbbf 30 2c 20 20 20 30 2c 20 20 33 30 2c 20 20 35 34  0,   0,  30,  54
2dbc0 2c 20 20 31 36 2c 20 20 33 33 2c 20 20 31 30 2c  ,  16,  33,  10,
2dbc1 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2dbc2 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20    0,   0,.      
2dbc3 20 30 2c 20 20 20 30 2c 20 20 31 31 2c 20 20 36   0,   0,  11,  6
2dbc4 36 2c 20 20 37 33 2c 20 20 20 30 2c 20 20 20 38  6,  73,   0,   8
2dbc5 2c 20 20 20 30 2c 20 20 39 38 2c 20 20 39 32 2c  ,   0,  98,  92,
2dbc6 20 20 20 30 2c 20 31 30 31 2c 20 20 20 30 2c 0a     0, 101,   0,.
2dbc7 20 20 20 20 20 20 38 31 2c 20 20 20 30 2c 20 20        81,   0,  
2dbc8 36 39 2c 20 20 20 30 2c 20 20 20 30 2c 20 31 30  69,   0,   0, 10
2dbc9 38 2c 20 20 32 37 2c 20 20 33 36 2c 20 20 36 37  8,  27,  36,  67
2dbca 2c 20 20 37 37 2c 20 20 20 30 2c 20 20 33 34 2c  ,  77,   0,  34,
2dbcb 20 20 36 32 2c 0a 20 20 20 20 20 20 20 30 2c 20    62,.       0, 
2dbcc 20 20 30 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74    0,.  };.  stat
2dbcd 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
2dbce 64 20 63 68 61 72 20 61 4c 65 6e 5b 31 31 39 5d  d char aLen[119]
2dbcf 20 3d 20 7b 0a 20 20 20 20 20 20 20 37 2c 20 20   = {.       7,  
2dbd0 20 37 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20   7,   5,   4,   
2dbd1 36 2c 20 20 20 34 2c 20 20 20 35 2c 20 20 20 33  6,   4,   5,   3
2dbd2 2c 20 20 20 36 2c 20 20 20 37 2c 20 20 20 33 2c  ,   6,   7,   3,
2dbd3 20 20 20 36 2c 20 20 20 36 2c 0a 20 20 20 20 20     6,   6,.     
2dbd4 20 20 37 2c 20 20 20 37 2c 20 20 20 33 2c 20 20    7,   7,   3,  
2dbd5 20 38 2c 20 20 20 32 2c 20 20 20 36 2c 20 20 20   8,   2,   6,   
2dbd6 35 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 33  5,   4,   4,   3
2dbd7 2c 20 20 31 30 2c 20 20 20 34 2c 20 20 20 36 2c  ,  10,   4,   6,
2dbd8 0a 20 20 20 20 20 20 31 31 2c 20 20 20 32 2c 20  .      11,   2, 
2dbd9 20 20 37 2c 20 20 20 35 2c 20 20 20 35 2c 20 20    7,   5,   5,  
2dbda 20 39 2c 20 20 20 36 2c 20 20 20 39 2c 20 20 20   9,   6,   9,   
2dbdb 39 2c 20 20 20 37 2c 20 20 31 30 2c 20 20 31 30  9,   7,  10,  10
2dbdc 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 36 2c  ,   4,.       6,
2dbdd 20 20 20 32 2c 20 20 20 33 2c 20 20 20 34 2c 20     2,   3,   4, 
2dbde 20 20 39 2c 20 20 20 32 2c 20 20 20 36 2c 20 20    9,   2,   6,  
2dbdf 20 35 2c 20 20 20 36 2c 20 20 20 36 2c 20 20 20   5,   6,   6,   
2dbe0 35 2c 20 20 20 36 2c 20 20 20 35 2c 0a 20 20 20  5,   6,   5,.   
2dbe1 20 20 20 20 35 2c 20 20 20 37 2c 20 20 20 37 2c      5,   7,   7,
2dbe2 20 20 20 37 2c 20 20 20 33 2c 20 20 20 34 2c 20     7,   3,   4, 
2dbe3 20 20 34 2c 20 20 20 37 2c 20 20 20 33 2c 20 20    4,   7,   3,  
2dbe4 20 36 2c 20 20 20 34 2c 20 20 20 37 2c 20 20 20   6,   4,   7,   
2dbe5 36 2c 0a 20 20 20 20 20 20 31 32 2c 20 20 20 36  6,.      12,   6
2dbe6 2c 20 20 20 39 2c 20 20 20 34 2c 20 20 20 36 2c  ,   9,   4,   6,
2dbe7 20 20 20 35 2c 20 20 20 34 2c 20 20 20 37 2c 20     5,   4,   7, 
2dbe8 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 20 20    6,   5,   6,  
2dbe9 20 37 2c 20 20 20 35 2c 0a 20 20 20 20 20 20 20   7,   5,.       
2dbea 34 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20 35  4,   5,   6,   5
2dbeb 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 37 2c  ,   7,   3,   7,
2dbec 20 20 31 33 2c 20 20 20 32 2c 20 20 20 32 2c 20    13,   2,   2, 
2dbed 20 20 34 2c 20 20 20 36 2c 20 20 20 36 2c 0a 20    4,   6,   6,. 
2dbee 20 20 20 20 20 20 38 2c 20 20 20 35 2c 20 20 31        8,   5,  1
2dbef 37 2c 20 20 31 32 2c 20 20 20 37 2c 20 20 20 38  7,  12,   7,   8
2dbf0 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 34 2c  ,   8,   2,   4,
2dbf1 20 20 20 34 2c 20 20 20 34 2c 20 20 20 34 2c 20     4,   4,   4, 
2dbf2 20 20 34 2c 0a 20 20 20 20 20 20 20 32 2c 20 20    4,.       2,  
2dbf3 20 32 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20   2,   6,   5,   
2dbf4 38 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20 38  8,   5,   5,   8
2dbf5 2c 20 20 20 33 2c 20 20 20 35 2c 20 20 20 35 2c  ,   3,   5,   5,
2dbf6 20 20 20 36 2c 20 20 20 34 2c 0a 20 20 20 20 20     6,   4,.     
2dbf7 20 20 39 2c 20 20 20 33 2c 0a 20 20 7d 3b 0a 20    9,   3,.  };. 
2dbf8 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
2dbf9 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74  signed short int
2dbfa 20 61 4f 66 66 73 65 74 5b 31 31 39 5d 20 3d 20   aOffset[119] = 
2dbfb 7b 0a 20 20 20 20 20 20 20 30 2c 20 20 20 32 2c  {.       0,   2,
2dbfc 20 20 20 32 2c 20 20 20 38 2c 20 20 20 39 2c 20     2,   8,   9, 
2dbfd 20 31 34 2c 20 20 31 36 2c 20 20 32 30 2c 20 20   14,  16,  20,  
2dbfe 32 33 2c 20 20 32 35 2c 20 20 32 35 2c 20 20 32  23,  25,  25,  2
2dbff 39 2c 20 20 33 33 2c 0a 20 20 20 20 20 20 33 36  9,  33,.      36
2dc00 2c 20 20 34 31 2c 20 20 34 36 2c 20 20 34 38 2c  ,  41,  46,  48,
2dc01 20 20 35 33 2c 20 20 35 34 2c 20 20 35 39 2c 20    53,  54,  59, 
2dc02 20 36 32 2c 20 20 36 35 2c 20 20 36 37 2c 20 20   62,  65,  67,  
2dc03 36 39 2c 20 20 37 38 2c 20 20 38 31 2c 0a 20 20  69,  78,  81,.  
2dc04 20 20 20 20 38 36 2c 20 20 39 35 2c 20 20 39 36      86,  95,  96
2dc05 2c 20 31 30 31 2c 20 31 30 35 2c 20 31 30 39 2c  , 101, 105, 109,
2dc06 20 31 31 37 2c 20 31 32 32 2c 20 31 32 38 2c 20   117, 122, 128, 
2dc07 31 33 36 2c 20 31 34 32 2c 20 31 35 32 2c 20 31  136, 142, 152, 1
2dc08 35 39 2c 0a 20 20 20 20 20 31 36 32 2c 20 31 36  59,.     162, 16
2dc09 32 2c 20 31 36 35 2c 20 31 36 37 2c 20 31 36 37  2, 165, 167, 167
2dc0a 2c 20 31 37 31 2c 20 31 37 36 2c 20 31 37 39 2c  , 171, 176, 179,
2dc0b 20 31 38 34 2c 20 31 38 39 2c 20 31 39 34 2c 20   184, 189, 194, 
2dc0c 31 39 37 2c 20 32 30 33 2c 0a 20 20 20 20 20 32  197, 203,.     2
2dc0d 30 36 2c 20 32 31 30 2c 20 32 31 37 2c 20 32 32  06, 210, 217, 22
2dc0e 33 2c 20 32 32 33 2c 20 32 32 36 2c 20 32 32 39  3, 223, 226, 229
2dc0f 2c 20 32 33 33 2c 20 32 33 34 2c 20 32 33 38 2c  , 233, 234, 238,
2dc10 20 32 34 34 2c 20 32 34 38 2c 20 32 35 35 2c 0a   244, 248, 255,.
2dc11 20 20 20 20 20 32 36 31 2c 20 32 37 33 2c 20 32       261, 273, 2
2dc12 37 39 2c 20 32 38 38 2c 20 32 39 30 2c 20 32 39  79, 288, 290, 29
2dc13 36 2c 20 33 30 31 2c 20 33 30 33 2c 20 33 31 30  6, 301, 303, 310
2dc14 2c 20 33 31 35 2c 20 33 32 30 2c 20 33 32 36 2c  , 315, 320, 326,
2dc15 20 33 33 32 2c 0a 20 20 20 20 20 33 33 37 2c 20   332,.     337, 
2dc16 33 34 31 2c 20 33 34 34 2c 20 33 35 30 2c 20 33  341, 344, 350, 3
2dc17 35 34 2c 20 33 36 31 2c 20 33 36 33 2c 20 33 37  54, 361, 363, 37
2dc18 30 2c 20 33 37 32 2c 20 33 37 34 2c 20 33 38 33  0, 372, 374, 383
2dc19 2c 20 33 38 37 2c 20 33 39 33 2c 0a 20 20 20 20  , 387, 393,.    
2dc1a 20 33 39 39 2c 20 34 30 37 2c 20 34 31 32 2c 20   399, 407, 412, 
2dc1b 34 31 32 2c 20 34 32 38 2c 20 34 33 35 2c 20 34  412, 428, 435, 4
2dc1c 34 32 2c 20 34 34 33 2c 20 34 35 30 2c 20 34 35  42, 443, 450, 45
2dc1d 34 2c 20 34 35 38 2c 20 34 36 32 2c 20 34 36 36  4, 458, 462, 466
2dc1e 2c 0a 20 20 20 20 20 34 36 39 2c 20 34 37 31 2c  ,.     469, 471,
2dc1f 20 34 37 33 2c 20 34 37 39 2c 20 34 38 33 2c 20   473, 479, 483, 
2dc20 34 39 31 2c 20 34 39 35 2c 20 35 30 30 2c 20 35  491, 495, 500, 5
2dc21 30 38 2c 20 35 31 31 2c 20 35 31 36 2c 20 35 32  08, 511, 516, 52
2dc22 31 2c 20 35 32 37 2c 0a 20 20 20 20 20 35 33 31  1, 527,.     531
2dc23 2c 20 35 33 36 2c 0a 20 20 7d 3b 0a 20 20 73 74  , 536,.  };.  st
2dc24 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
2dc25 6e 65 64 20 63 68 61 72 20 61 43 6f 64 65 5b 31  ned char aCode[1
2dc26 31 39 5d 20 3d 20 7b 0a 20 20 20 20 54 4b 5f 52  19] = {.    TK_R
2dc27 45 49 4e 44 45 58 2c 20 20 20 20 54 4b 5f 49 4e  EINDEX,    TK_IN
2dc28 44 45 58 45 44 2c 20 20 20 20 54 4b 5f 49 4e 44  DEXED,    TK_IND
2dc29 45 58 2c 20 20 20 20 20 20 54 4b 5f 44 45 53 43  EX,      TK_DESC
2dc2a 2c 20 20 20 20 20 20 20 54 4b 5f 45 53 43 41 50  ,       TK_ESCAP
2dc2b 45 2c 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 45  E,     .    TK_E
2dc2c 41 43 48 2c 20 20 20 20 20 20 20 54 4b 5f 43 48  ACH,       TK_CH
2dc2d 45 43 4b 2c 20 20 20 20 20 20 54 4b 5f 4b 45 59  ECK,      TK_KEY
2dc2e 2c 20 20 20 20 20 20 20 20 54 4b 5f 42 45 46 4f  ,        TK_BEFO
2dc2f 52 45 2c 20 20 20 20 20 54 4b 5f 46 4f 52 45 49  RE,     TK_FOREI
2dc30 47 4e 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f 46  GN,    .    TK_F
2dc31 4f 52 2c 20 20 20 20 20 20 20 20 54 4b 5f 49 47  OR,        TK_IG
2dc32 4e 4f 52 45 2c 20 20 20 20 20 54 4b 5f 4c 49 4b  NORE,     TK_LIK
2dc33 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 45 58 50 4c  E_KW,    TK_EXPL
2dc34 41 49 4e 2c 20 20 20 20 54 4b 5f 49 4e 53 54 45  AIN,    TK_INSTE
2dc35 41 44 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f 41  AD,    .    TK_A
2dc36 44 44 2c 20 20 20 20 20 20 20 20 54 4b 5f 44 41  DD,        TK_DA
2dc37 54 41 42 41 53 45 2c 20 20 20 54 4b 5f 41 53 2c  TABASE,   TK_AS,
2dc38 20 20 20 20 20 20 20 20 20 54 4b 5f 53 45 4c 45           TK_SELE
2dc39 43 54 2c 20 20 20 20 20 54 4b 5f 54 41 42 4c 45  CT,     TK_TABLE
2dc3a 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 4a  ,      .    TK_J
2dc3b 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 54 48  OIN_KW,    TK_TH
2dc3c 45 4e 2c 20 20 20 20 20 20 20 54 4b 5f 45 4e 44  EN,       TK_END
2dc3d 2c 20 20 20 20 20 20 20 20 54 4b 5f 44 45 46 45  ,        TK_DEFE
2dc3e 52 52 41 42 4c 45 2c 20 54 4b 5f 45 4c 53 45 2c  RRABLE, TK_ELSE,
2dc3f 20 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 45         .    TK_E
2dc40 58 43 45 50 54 2c 20 20 20 20 20 54 4b 5f 54 52  XCEPT,     TK_TR
2dc41 41 4e 53 41 43 54 49 4f 4e 2c 54 4b 5f 4f 4e 2c  ANSACTION,TK_ON,
2dc42 20 20 20 20 20 20 20 20 20 54 4b 5f 4a 4f 49 4e           TK_JOIN
2dc43 5f 4b 57 2c 20 20 20 20 54 4b 5f 41 4c 54 45 52  _KW,    TK_ALTER
2dc44 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 52  ,      .    TK_R
2dc45 41 49 53 45 2c 20 20 20 20 20 20 54 4b 5f 45 58  AISE,      TK_EX
2dc46 43 4c 55 53 49 56 45 2c 20 20 54 4b 5f 45 58 49  CLUSIVE,  TK_EXI
2dc47 53 54 53 2c 20 20 20 20 20 54 4b 5f 53 41 56 45  STS,     TK_SAVE
2dc48 50 4f 49 4e 54 2c 20 20 54 4b 5f 49 4e 54 45 52  POINT,  TK_INTER
2dc49 53 45 43 54 2c 20 20 0a 20 20 20 20 54 4b 5f 54  SECT,  .    TK_T
2dc4a 52 49 47 47 45 52 2c 20 20 20 20 54 4b 5f 52 45  RIGGER,    TK_RE
2dc4b 46 45 52 45 4e 43 45 53 2c 20 54 4b 5f 43 4f 4e  FERENCES, TK_CON
2dc4c 53 54 52 41 49 4e 54 2c 20 54 4b 5f 49 4e 54 4f  STRAINT, TK_INTO
2dc4d 2c 20 20 20 20 20 20 20 54 4b 5f 4f 46 46 53 45  ,       TK_OFFSE
2dc4e 54 2c 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 4f  T,     .    TK_O
2dc4f 46 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 53 45  F,         TK_SE
2dc50 54 2c 20 20 20 20 20 20 20 20 54 4b 5f 54 45 4d  T,        TK_TEM
2dc51 50 2c 20 20 20 20 20 20 20 54 4b 5f 54 45 4d 50  P,       TK_TEMP
2dc52 2c 20 20 20 20 20 20 20 54 4b 5f 4f 52 2c 20 20  ,       TK_OR,  
2dc53 20 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 55         .    TK_U
2dc54 4e 49 51 55 45 2c 20 20 20 20 20 54 4b 5f 51 55  NIQUE,     TK_QU
2dc55 45 52 59 2c 20 20 20 20 20 20 54 4b 5f 41 54 54  ERY,      TK_ATT
2dc56 41 43 48 2c 20 20 20 20 20 54 4b 5f 48 41 56 49  ACH,     TK_HAVI
2dc57 4e 47 2c 20 20 20 20 20 54 4b 5f 47 52 4f 55 50  NG,     TK_GROUP
2dc58 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 55  ,      .    TK_U
2dc59 50 44 41 54 45 2c 20 20 20 20 20 54 4b 5f 42 45  PDATE,     TK_BE
2dc5a 47 49 4e 2c 20 20 20 20 20 20 54 4b 5f 4a 4f 49  GIN,      TK_JOI
2dc5b 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 52 45 4c 45  N_KW,    TK_RELE
2dc5c 41 53 45 2c 20 20 20 20 54 4b 5f 42 45 54 57 45  ASE,    TK_BETWE
2dc5d 45 4e 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f 4e  EN,    .    TK_N
2dc5e 4f 54 4e 55 4c 4c 2c 20 20 20 20 54 4b 5f 4e 4f  OTNULL,    TK_NO
2dc5f 54 2c 20 20 20 20 20 20 20 20 54 4b 5f 4e 55 4c  T,        TK_NUL
2dc60 4c 2c 20 20 20 20 20 20 20 54 4b 5f 4c 49 4b 45  L,       TK_LIKE
2dc61 5f 4b 57 2c 20 20 20 20 54 4b 5f 43 41 53 43 41  _KW,    TK_CASCA
2dc62 44 45 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f 41  DE,    .    TK_A
2dc63 53 43 2c 20 20 20 20 20 20 20 20 54 4b 5f 44 45  SC,        TK_DE
2dc64 4c 45 54 45 2c 20 20 20 20 20 54 4b 5f 43 41 53  LETE,     TK_CAS
2dc65 45 2c 20 20 20 20 20 20 20 54 4b 5f 43 4f 4c 4c  E,       TK_COLL
2dc66 41 54 45 2c 20 20 20 20 54 4b 5f 43 52 45 41 54  ATE,    TK_CREAT
2dc67 45 2c 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 43  E,     .    TK_C
2dc68 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f 44 45  TIME_KW,   TK_DE
2dc69 54 41 43 48 2c 20 20 20 20 20 54 4b 5f 49 4d 4d  TACH,     TK_IMM
2dc6a 45 44 49 41 54 45 2c 20 20 54 4b 5f 4a 4f 49 4e  EDIATE,  TK_JOIN
2dc6b 2c 20 20 20 20 20 20 20 54 4b 5f 49 4e 53 45 52  ,       TK_INSER
2dc6c 54 2c 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 4d  T,     .    TK_M
2dc6d 41 54 43 48 2c 20 20 20 20 20 20 54 4b 5f 50 4c  ATCH,      TK_PL
2dc6e 41 4e 2c 20 20 20 20 20 20 20 54 4b 5f 41 4e 41  AN,       TK_ANA
2dc6f 4c 59 5a 45 2c 20 20 20 20 54 4b 5f 50 52 41 47  LYZE,    TK_PRAG
2dc70 4d 41 2c 20 20 20 20 20 54 4b 5f 41 42 4f 52 54  MA,     TK_ABORT
2dc71 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 56  ,      .    TK_V
2dc72 41 4c 55 45 53 2c 20 20 20 20 20 54 4b 5f 56 49  ALUES,     TK_VI
2dc73 52 54 55 41 4c 2c 20 20 20 20 54 4b 5f 4c 49 4d  RTUAL,    TK_LIM
2dc74 49 54 2c 20 20 20 20 20 20 54 4b 5f 57 48 45 4e  IT,      TK_WHEN
2dc75 2c 20 20 20 20 20 20 20 54 4b 5f 57 48 45 52 45  ,       TK_WHERE
2dc76 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 52  ,      .    TK_R
2dc77 45 4e 41 4d 45 2c 20 20 20 20 20 54 4b 5f 41 46  ENAME,     TK_AF
2dc78 54 45 52 2c 20 20 20 20 20 20 54 4b 5f 52 45 50  TER,      TK_REP
2dc79 4c 41 43 45 2c 20 20 20 20 54 4b 5f 41 4e 44 2c  LACE,    TK_AND,
2dc7a 20 20 20 20 20 20 20 20 54 4b 5f 44 45 46 41 55          TK_DEFAU
2dc7b 4c 54 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f 41  LT,    .    TK_A
2dc7c 55 54 4f 49 4e 43 52 2c 20 20 20 54 4b 5f 54 4f  UTOINCR,   TK_TO
2dc7d 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 49 4e 2c  ,         TK_IN,
2dc7e 20 20 20 20 20 20 20 20 20 54 4b 5f 43 41 53 54           TK_CAST
2dc7f 2c 20 20 20 20 20 20 20 54 4b 5f 43 4f 4c 55 4d  ,       TK_COLUM
2dc80 4e 4b 57 2c 20 20 20 0a 20 20 20 20 54 4b 5f 43  NKW,   .    TK_C
2dc81 4f 4d 4d 49 54 2c 20 20 20 20 20 54 4b 5f 43 4f  OMMIT,     TK_CO
2dc82 4e 46 4c 49 43 54 2c 20 20 20 54 4b 5f 4a 4f 49  NFLICT,   TK_JOI
2dc83 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 43 54 49 4d  N_KW,    TK_CTIM
2dc84 45 5f 4b 57 2c 20 20 20 54 4b 5f 43 54 49 4d 45  E_KW,   TK_CTIME
2dc85 5f 4b 57 2c 20 20 20 0a 20 20 20 20 54 4b 5f 50  _KW,   .    TK_P
2dc86 52 49 4d 41 52 59 2c 20 20 20 20 54 4b 5f 44 45  RIMARY,    TK_DE
2dc87 46 45 52 52 45 44 2c 20 20 20 54 4b 5f 44 49 53  FERRED,   TK_DIS
2dc88 54 49 4e 43 54 2c 20 20 20 54 4b 5f 49 53 2c 20  TINCT,   TK_IS, 
2dc89 20 20 20 20 20 20 20 20 54 4b 5f 44 52 4f 50 2c          TK_DROP,
2dc8a 20 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 46         .    TK_F
2dc8b 41 49 4c 2c 20 20 20 20 20 20 20 54 4b 5f 46 52  AIL,       TK_FR
2dc8c 4f 4d 2c 20 20 20 20 20 20 20 54 4b 5f 4a 4f 49  OM,       TK_JOI
2dc8d 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 4c 49 4b 45  N_KW,    TK_LIKE
2dc8e 5f 4b 57 2c 20 20 20 20 54 4b 5f 42 59 2c 20 20  _KW,    TK_BY,  
2dc8f 20 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 49         .    TK_I
2dc90 46 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 49 53  F,         TK_IS
2dc91 4e 55 4c 4c 2c 20 20 20 20 20 54 4b 5f 4f 52 44  NULL,     TK_ORD
2dc92 45 52 2c 20 20 20 20 20 20 54 4b 5f 52 45 53 54  ER,      TK_REST
2dc93 52 49 43 54 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f  RICT,   TK_JOIN_
2dc94 4b 57 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f 4a  KW,    .    TK_J
2dc95 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 52 4f  OIN_KW,    TK_RO
2dc96 4c 4c 42 41 43 4b 2c 20 20 20 54 4b 5f 52 4f 57  LLBACK,   TK_ROW
2dc97 2c 20 20 20 20 20 20 20 20 54 4b 5f 55 4e 49 4f  ,        TK_UNIO
2dc98 4e 2c 20 20 20 20 20 20 54 4b 5f 55 53 49 4e 47  N,      TK_USING
2dc99 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f 56  ,      .    TK_V
2dc9a 41 43 55 55 4d 2c 20 20 20 20 20 54 4b 5f 56 49  ACUUM,     TK_VI
2dc9b 45 57 2c 20 20 20 20 20 20 20 54 4b 5f 49 4e 49  EW,       TK_INI
2dc9c 54 49 41 4c 4c 59 2c 20 20 54 4b 5f 41 4c 4c 2c  TIALLY,  TK_ALL,
2dc9d 20 20 20 20 20 20 20 20 0a 20 20 7d 3b 0a 20 20          .  };.  
2dc9e 69 6e 74 20 68 2c 20 69 3b 0a 20 20 69 66 28 20  int h, i;.  if( 
2dc9f 6e 3c 32 20 29 20 72 65 74 75 72 6e 20 54 4b 5f  n<2 ) return TK_
2dca0 49 44 3b 0a 20 20 68 20 3d 20 28 28 63 68 61 72  ID;.  h = ((char
2dca1 4d 61 70 28 7a 5b 30 5d 29 2a 34 29 20 5e 0a 20  Map(z[0])*4) ^. 
2dca2 20 20 20 20 20 28 63 68 61 72 4d 61 70 28 7a 5b       (charMap(z[
2dca3 6e 2d 31 5d 29 2a 33 29 20 5e 0a 20 20 20 20 20  n-1])*3) ^.     
2dca4 20 6e 29 20 25 20 31 32 37 3b 0a 20 20 66 6f 72   n) % 127;.  for
2dca5 28 69 3d 28 28 69 6e 74 29 61 48 61 73 68 5b 68  (i=((int)aHash[h
2dca6 5d 29 2d 31 3b 20 69 3e 3d 30 3b 20 69 3d 28 28  ])-1; i>=0; i=((
2dca7 69 6e 74 29 61 4e 65 78 74 5b 69 5d 29 2d 31 29  int)aNext[i])-1)
2dca8 7b 0a 20 20 20 20 69 66 28 20 61 4c 65 6e 5b 69  {.    if( aLen[i
2dca9 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53  ]==n && sqlite3S
2dcaa 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 61  trNICmp(&zText[a
2dcab 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d  Offset[i]],z,n)=
2dcac 3d 30 20 29 7b 0a 20 20 20 20 20 20 74 65 73 74  =0 ){.      test
2dcad 63 61 73 65 28 20 69 3d 3d 30 20 29 3b 20 2f 2a  case( i==0 ); /*
2dcae 20 54 4b 5f 52 45 49 4e 44 45 58 20 2a 2f 0a 20   TK_REINDEX */. 
2dcaf 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dcb0 3d 3d 31 20 29 3b 20 2f 2a 20 54 4b 5f 49 4e 44  ==1 ); /* TK_IND
2dcb1 45 58 45 44 20 2a 2f 0a 20 20 20 20 20 20 74 65  EXED */.      te
2dcb2 73 74 63 61 73 65 28 20 69 3d 3d 32 20 29 3b 20  stcase( i==2 ); 
2dcb3 2f 2a 20 54 4b 5f 49 4e 44 45 58 20 2a 2f 0a 20  /* TK_INDEX */. 
2dcb4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dcb5 3d 3d 33 20 29 3b 20 2f 2a 20 54 4b 5f 44 45 53  ==3 ); /* TK_DES
2dcb6 43 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  C */.      testc
2dcb7 61 73 65 28 20 69 3d 3d 34 20 29 3b 20 2f 2a 20  ase( i==4 ); /* 
2dcb8 54 4b 5f 45 53 43 41 50 45 20 2a 2f 0a 20 20 20  TK_ESCAPE */.   
2dcb9 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2dcba 35 20 29 3b 20 2f 2a 20 54 4b 5f 45 41 43 48 20  5 ); /* TK_EACH 
2dcbb 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2dcbc 65 28 20 69 3d 3d 36 20 29 3b 20 2f 2a 20 54 4b  e( i==6 ); /* TK
2dcbd 5f 43 48 45 43 4b 20 2a 2f 0a 20 20 20 20 20 20  _CHECK */.      
2dcbe 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 20 29  testcase( i==7 )
2dcbf 3b 20 2f 2a 20 54 4b 5f 4b 45 59 20 2a 2f 0a 20  ; /* TK_KEY */. 
2dcc0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dcc1 3d 3d 38 20 29 3b 20 2f 2a 20 54 4b 5f 42 45 46  ==8 ); /* TK_BEF
2dcc2 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ORE */.      tes
2dcc3 74 63 61 73 65 28 20 69 3d 3d 39 20 29 3b 20 2f  tcase( i==9 ); /
2dcc4 2a 20 54 4b 5f 46 4f 52 45 49 47 4e 20 2a 2f 0a  * TK_FOREIGN */.
2dcc5 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2dcc6 69 3d 3d 31 30 20 29 3b 20 2f 2a 20 54 4b 5f 46  i==10 ); /* TK_F
2dcc7 4f 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  OR */.      test
2dcc8 63 61 73 65 28 20 69 3d 3d 31 31 20 29 3b 20 2f  case( i==11 ); /
2dcc9 2a 20 54 4b 5f 49 47 4e 4f 52 45 20 2a 2f 0a 20  * TK_IGNORE */. 
2dcca 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dccb 3d 3d 31 32 20 29 3b 20 2f 2a 20 54 4b 5f 4c 49  ==12 ); /* TK_LI
2dccc 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74  KE_KW */.      t
2dccd 65 73 74 63 61 73 65 28 20 69 3d 3d 31 33 20 29  estcase( i==13 )
2dcce 3b 20 2f 2a 20 54 4b 5f 45 58 50 4c 41 49 4e 20  ; /* TK_EXPLAIN 
2dccf 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2dcd0 65 28 20 69 3d 3d 31 34 20 29 3b 20 2f 2a 20 54  e( i==14 ); /* T
2dcd1 4b 5f 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20 20  K_INSTEAD */.   
2dcd2 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2dcd3 31 35 20 29 3b 20 2f 2a 20 54 4b 5f 41 44 44 20  15 ); /* TK_ADD 
2dcd4 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2dcd5 65 28 20 69 3d 3d 31 36 20 29 3b 20 2f 2a 20 54  e( i==16 ); /* T
2dcd6 4b 5f 44 41 54 41 42 41 53 45 20 2a 2f 0a 20 20  K_DATABASE */.  
2dcd7 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2dcd8 3d 31 37 20 29 3b 20 2f 2a 20 54 4b 5f 41 53 20  =17 ); /* TK_AS 
2dcd9 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2dcda 65 28 20 69 3d 3d 31 38 20 29 3b 20 2f 2a 20 54  e( i==18 ); /* T
2dcdb 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20  K_SELECT */.    
2dcdc 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
2dcdd 39 20 29 3b 20 2f 2a 20 54 4b 5f 54 41 42 4c 45  9 ); /* TK_TABLE
2dcde 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2dcdf 73 65 28 20 69 3d 3d 32 30 20 29 3b 20 2f 2a 20  se( i==20 ); /* 
2dce0 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 20 20  TK_JOIN_KW */.  
2dce1 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2dce2 3d 32 31 20 29 3b 20 2f 2a 20 54 4b 5f 54 48 45  =21 ); /* TK_THE
2dce3 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  N */.      testc
2dce4 61 73 65 28 20 69 3d 3d 32 32 20 29 3b 20 2f 2a  ase( i==22 ); /*
2dce5 20 54 4b 5f 45 4e 44 20 2a 2f 0a 20 20 20 20 20   TK_END */.     
2dce6 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 33   testcase( i==23
2dce7 20 29 3b 20 2f 2a 20 54 4b 5f 44 45 46 45 52 52   ); /* TK_DEFERR
2dce8 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  ABLE */.      te
2dce9 73 74 63 61 73 65 28 20 69 3d 3d 32 34 20 29 3b  stcase( i==24 );
2dcea 20 2f 2a 20 54 4b 5f 45 4c 53 45 20 2a 2f 0a 20   /* TK_ELSE */. 
2dceb 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dcec 3d 3d 32 35 20 29 3b 20 2f 2a 20 54 4b 5f 45 58  ==25 ); /* TK_EX
2dced 43 45 50 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  CEPT */.      te
2dcee 73 74 63 61 73 65 28 20 69 3d 3d 32 36 20 29 3b  stcase( i==26 );
2dcef 20 2f 2a 20 54 4b 5f 54 52 41 4e 53 41 43 54 49   /* TK_TRANSACTI
2dcf0 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ON */.      test
2dcf1 63 61 73 65 28 20 69 3d 3d 32 37 20 29 3b 20 2f  case( i==27 ); /
2dcf2 2a 20 54 4b 5f 4f 4e 20 2a 2f 0a 20 20 20 20 20  * TK_ON */.     
2dcf3 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 38   testcase( i==28
2dcf4 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b   ); /* TK_JOIN_K
2dcf5 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  W */.      testc
2dcf6 61 73 65 28 20 69 3d 3d 32 39 20 29 3b 20 2f 2a  ase( i==29 ); /*
2dcf7 20 54 4b 5f 41 4c 54 45 52 20 2a 2f 0a 20 20 20   TK_ALTER */.   
2dcf8 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2dcf9 33 30 20 29 3b 20 2f 2a 20 54 4b 5f 52 41 49 53  30 ); /* TK_RAIS
2dcfa 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
2dcfb 61 73 65 28 20 69 3d 3d 33 31 20 29 3b 20 2f 2a  ase( i==31 ); /*
2dcfc 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 20 2a 2f   TK_EXCLUSIVE */
2dcfd 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dcfe 20 69 3d 3d 33 32 20 29 3b 20 2f 2a 20 54 4b 5f   i==32 ); /* TK_
2dcff 45 58 49 53 54 53 20 2a 2f 0a 20 20 20 20 20 20  EXISTS */.      
2dd00 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 33 20  testcase( i==33 
2dd01 29 3b 20 2f 2a 20 54 4b 5f 53 41 56 45 50 4f 49  ); /* TK_SAVEPOI
2dd02 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  NT */.      test
2dd03 63 61 73 65 28 20 69 3d 3d 33 34 20 29 3b 20 2f  case( i==34 ); /
2dd04 2a 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 2a  * TK_INTERSECT *
2dd05 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2dd06 28 20 69 3d 3d 33 35 20 29 3b 20 2f 2a 20 54 4b  ( i==35 ); /* TK
2dd07 5f 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 20 20  _TRIGGER */.    
2dd08 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33    testcase( i==3
2dd09 36 20 29 3b 20 2f 2a 20 54 4b 5f 52 45 46 45 52  6 ); /* TK_REFER
2dd0a 45 4e 43 45 53 20 2a 2f 0a 20 20 20 20 20 20 74  ENCES */.      t
2dd0b 65 73 74 63 61 73 65 28 20 69 3d 3d 33 37 20 29  estcase( i==37 )
2dd0c 3b 20 2f 2a 20 54 4b 5f 43 4f 4e 53 54 52 41 49  ; /* TK_CONSTRAI
2dd0d 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  NT */.      test
2dd0e 63 61 73 65 28 20 69 3d 3d 33 38 20 29 3b 20 2f  case( i==38 ); /
2dd0f 2a 20 54 4b 5f 49 4e 54 4f 20 2a 2f 0a 20 20 20  * TK_INTO */.   
2dd10 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2dd11 33 39 20 29 3b 20 2f 2a 20 54 4b 5f 4f 46 46 53  39 ); /* TK_OFFS
2dd12 45 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ET */.      test
2dd13 63 61 73 65 28 20 69 3d 3d 34 30 20 29 3b 20 2f  case( i==40 ); /
2dd14 2a 20 54 4b 5f 4f 46 20 2a 2f 0a 20 20 20 20 20  * TK_OF */.     
2dd15 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 31   testcase( i==41
2dd16 20 29 3b 20 2f 2a 20 54 4b 5f 53 45 54 20 2a 2f   ); /* TK_SET */
2dd17 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dd18 20 69 3d 3d 34 32 20 29 3b 20 2f 2a 20 54 4b 5f   i==42 ); /* TK_
2dd19 54 45 4d 50 20 2a 2f 0a 20 20 20 20 20 20 74 65  TEMP */.      te
2dd1a 73 74 63 61 73 65 28 20 69 3d 3d 34 33 20 29 3b  stcase( i==43 );
2dd1b 20 2f 2a 20 54 4b 5f 54 45 4d 50 20 2a 2f 0a 20   /* TK_TEMP */. 
2dd1c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dd1d 3d 3d 34 34 20 29 3b 20 2f 2a 20 54 4b 5f 4f 52  ==44 ); /* TK_OR
2dd1e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2dd1f 73 65 28 20 69 3d 3d 34 35 20 29 3b 20 2f 2a 20  se( i==45 ); /* 
2dd20 54 4b 5f 55 4e 49 51 55 45 20 2a 2f 0a 20 20 20  TK_UNIQUE */.   
2dd21 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2dd22 34 36 20 29 3b 20 2f 2a 20 54 4b 5f 51 55 45 52  46 ); /* TK_QUER
2dd23 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  Y */.      testc
2dd24 61 73 65 28 20 69 3d 3d 34 37 20 29 3b 20 2f 2a  ase( i==47 ); /*
2dd25 20 54 4b 5f 41 54 54 41 43 48 20 2a 2f 0a 20 20   TK_ATTACH */.  
2dd26 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2dd27 3d 34 38 20 29 3b 20 2f 2a 20 54 4b 5f 48 41 56  =48 ); /* TK_HAV
2dd28 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ING */.      tes
2dd29 74 63 61 73 65 28 20 69 3d 3d 34 39 20 29 3b 20  tcase( i==49 ); 
2dd2a 2f 2a 20 54 4b 5f 47 52 4f 55 50 20 2a 2f 0a 20  /* TK_GROUP */. 
2dd2b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dd2c 3d 3d 35 30 20 29 3b 20 2f 2a 20 54 4b 5f 55 50  ==50 ); /* TK_UP
2dd2d 44 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  DATE */.      te
2dd2e 73 74 63 61 73 65 28 20 69 3d 3d 35 31 20 29 3b  stcase( i==51 );
2dd2f 20 2f 2a 20 54 4b 5f 42 45 47 49 4e 20 2a 2f 0a   /* TK_BEGIN */.
2dd30 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2dd31 69 3d 3d 35 32 20 29 3b 20 2f 2a 20 54 4b 5f 4a  i==52 ); /* TK_J
2dd32 4f 49 4e 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  OIN_KW */.      
2dd33 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 33 20  testcase( i==53 
2dd34 29 3b 20 2f 2a 20 54 4b 5f 52 45 4c 45 41 53 45  ); /* TK_RELEASE
2dd35 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2dd36 73 65 28 20 69 3d 3d 35 34 20 29 3b 20 2f 2a 20  se( i==54 ); /* 
2dd37 54 4b 5f 42 45 54 57 45 45 4e 20 2a 2f 0a 20 20  TK_BETWEEN */.  
2dd38 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2dd39 3d 35 35 20 29 3b 20 2f 2a 20 54 4b 5f 4e 4f 54  =55 ); /* TK_NOT
2dd3a 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65  NULL */.      te
2dd3b 73 74 63 61 73 65 28 20 69 3d 3d 35 36 20 29 3b  stcase( i==56 );
2dd3c 20 2f 2a 20 54 4b 5f 4e 4f 54 20 2a 2f 0a 20 20   /* TK_NOT */.  
2dd3d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2dd3e 3d 35 37 20 29 3b 20 2f 2a 20 54 4b 5f 4e 55 4c  =57 ); /* TK_NUL
2dd3f 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  L */.      testc
2dd40 61 73 65 28 20 69 3d 3d 35 38 20 29 3b 20 2f 2a  ase( i==58 ); /*
2dd41 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20   TK_LIKE_KW */. 
2dd42 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dd43 3d 3d 35 39 20 29 3b 20 2f 2a 20 54 4b 5f 43 41  ==59 ); /* TK_CA
2dd44 53 43 41 44 45 20 2a 2f 0a 20 20 20 20 20 20 74  SCADE */.      t
2dd45 65 73 74 63 61 73 65 28 20 69 3d 3d 36 30 20 29  estcase( i==60 )
2dd46 3b 20 2f 2a 20 54 4b 5f 41 53 43 20 2a 2f 0a 20  ; /* TK_ASC */. 
2dd47 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dd48 3d 3d 36 31 20 29 3b 20 2f 2a 20 54 4b 5f 44 45  ==61 ); /* TK_DE
2dd49 4c 45 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  LETE */.      te
2dd4a 73 74 63 61 73 65 28 20 69 3d 3d 36 32 20 29 3b  stcase( i==62 );
2dd4b 20 2f 2a 20 54 4b 5f 43 41 53 45 20 2a 2f 0a 20   /* TK_CASE */. 
2dd4c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dd4d 3d 3d 36 33 20 29 3b 20 2f 2a 20 54 4b 5f 43 4f  ==63 ); /* TK_CO
2dd4e 4c 4c 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74  LLATE */.      t
2dd4f 65 73 74 63 61 73 65 28 20 69 3d 3d 36 34 20 29  estcase( i==64 )
2dd50 3b 20 2f 2a 20 54 4b 5f 43 52 45 41 54 45 20 2a  ; /* TK_CREATE *
2dd51 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2dd52 28 20 69 3d 3d 36 35 20 29 3b 20 2f 2a 20 54 4b  ( i==65 ); /* TK
2dd53 5f 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 20 20 20  _CTIME_KW */.   
2dd54 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2dd55 36 36 20 29 3b 20 2f 2a 20 54 4b 5f 44 45 54 41  66 ); /* TK_DETA
2dd56 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  CH */.      test
2dd57 63 61 73 65 28 20 69 3d 3d 36 37 20 29 3b 20 2f  case( i==67 ); /
2dd58 2a 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20 2a  * TK_IMMEDIATE *
2dd59 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2dd5a 28 20 69 3d 3d 36 38 20 29 3b 20 2f 2a 20 54 4b  ( i==68 ); /* TK
2dd5b 5f 4a 4f 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74  _JOIN */.      t
2dd5c 65 73 74 63 61 73 65 28 20 69 3d 3d 36 39 20 29  estcase( i==69 )
2dd5d 3b 20 2f 2a 20 54 4b 5f 49 4e 53 45 52 54 20 2a  ; /* TK_INSERT *
2dd5e 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2dd5f 28 20 69 3d 3d 37 30 20 29 3b 20 2f 2a 20 54 4b  ( i==70 ); /* TK
2dd60 5f 4d 41 54 43 48 20 2a 2f 0a 20 20 20 20 20 20  _MATCH */.      
2dd61 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 31 20  testcase( i==71 
2dd62 29 3b 20 2f 2a 20 54 4b 5f 50 4c 41 4e 20 2a 2f  ); /* TK_PLAN */
2dd63 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dd64 20 69 3d 3d 37 32 20 29 3b 20 2f 2a 20 54 4b 5f   i==72 ); /* TK_
2dd65 41 4e 41 4c 59 5a 45 20 2a 2f 0a 20 20 20 20 20  ANALYZE */.     
2dd66 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 33   testcase( i==73
2dd67 20 29 3b 20 2f 2a 20 54 4b 5f 50 52 41 47 4d 41   ); /* TK_PRAGMA
2dd68 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2dd69 73 65 28 20 69 3d 3d 37 34 20 29 3b 20 2f 2a 20  se( i==74 ); /* 
2dd6a 54 4b 5f 41 42 4f 52 54 20 2a 2f 0a 20 20 20 20  TK_ABORT */.    
2dd6b 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
2dd6c 35 20 29 3b 20 2f 2a 20 54 4b 5f 56 41 4c 55 45  5 ); /* TK_VALUE
2dd6d 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  S */.      testc
2dd6e 61 73 65 28 20 69 3d 3d 37 36 20 29 3b 20 2f 2a  ase( i==76 ); /*
2dd6f 20 54 4b 5f 56 49 52 54 55 41 4c 20 2a 2f 0a 20   TK_VIRTUAL */. 
2dd70 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dd71 3d 3d 37 37 20 29 3b 20 2f 2a 20 54 4b 5f 4c 49  ==77 ); /* TK_LI
2dd72 4d 49 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  MIT */.      tes
2dd73 74 63 61 73 65 28 20 69 3d 3d 37 38 20 29 3b 20  tcase( i==78 ); 
2dd74 2f 2a 20 54 4b 5f 57 48 45 4e 20 2a 2f 0a 20 20  /* TK_WHEN */.  
2dd75 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2dd76 3d 37 39 20 29 3b 20 2f 2a 20 54 4b 5f 57 48 45  =79 ); /* TK_WHE
2dd77 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RE */.      test
2dd78 63 61 73 65 28 20 69 3d 3d 38 30 20 29 3b 20 2f  case( i==80 ); /
2dd79 2a 20 54 4b 5f 52 45 4e 41 4d 45 20 2a 2f 0a 20  * TK_RENAME */. 
2dd7a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2dd7b 3d 3d 38 31 20 29 3b 20 2f 2a 20 54 4b 5f 41 46  ==81 ); /* TK_AF
2dd7c 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  TER */.      tes
2dd7d 74 63 61 73 65 28 20 69 3d 3d 38 32 20 29 3b 20  tcase( i==82 ); 
2dd7e 2f 2a 20 54 4b 5f 52 45 50 4c 41 43 45 20 2a 2f  /* TK_REPLACE */
2dd7f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dd80 20 69 3d 3d 38 33 20 29 3b 20 2f 2a 20 54 4b 5f   i==83 ); /* TK_
2dd81 41 4e 44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  AND */.      tes
2dd82 74 63 61 73 65 28 20 69 3d 3d 38 34 20 29 3b 20  tcase( i==84 ); 
2dd83 2f 2a 20 54 4b 5f 44 45 46 41 55 4c 54 20 2a 2f  /* TK_DEFAULT */
2dd84 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dd85 20 69 3d 3d 38 35 20 29 3b 20 2f 2a 20 54 4b 5f   i==85 ); /* TK_
2dd86 41 55 54 4f 49 4e 43 52 20 2a 2f 0a 20 20 20 20  AUTOINCR */.    
2dd87 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38    testcase( i==8
2dd88 36 20 29 3b 20 2f 2a 20 54 4b 5f 54 4f 20 2a 2f  6 ); /* TK_TO */
2dd89 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dd8a 20 69 3d 3d 38 37 20 29 3b 20 2f 2a 20 54 4b 5f   i==87 ); /* TK_
2dd8b 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IN */.      test
2dd8c 63 61 73 65 28 20 69 3d 3d 38 38 20 29 3b 20 2f  case( i==88 ); /
2dd8d 2a 20 54 4b 5f 43 41 53 54 20 2a 2f 0a 20 20 20  * TK_CAST */.   
2dd8e 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2dd8f 38 39 20 29 3b 20 2f 2a 20 54 4b 5f 43 4f 4c 55  89 ); /* TK_COLU
2dd90 4d 4e 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74 65  MNKW */.      te
2dd91 73 74 63 61 73 65 28 20 69 3d 3d 39 30 20 29 3b  stcase( i==90 );
2dd92 20 2f 2a 20 54 4b 5f 43 4f 4d 4d 49 54 20 2a 2f   /* TK_COMMIT */
2dd93 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dd94 20 69 3d 3d 39 31 20 29 3b 20 2f 2a 20 54 4b 5f   i==91 ); /* TK_
2dd95 43 4f 4e 46 4c 49 43 54 20 2a 2f 0a 20 20 20 20  CONFLICT */.    
2dd96 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39    testcase( i==9
2dd97 32 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f  2 ); /* TK_JOIN_
2dd98 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  KW */.      test
2dd99 63 61 73 65 28 20 69 3d 3d 39 33 20 29 3b 20 2f  case( i==93 ); /
2dd9a 2a 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 2a 2f  * TK_CTIME_KW */
2dd9b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dd9c 20 69 3d 3d 39 34 20 29 3b 20 2f 2a 20 54 4b 5f   i==94 ); /* TK_
2dd9d 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 20 20 20 20  CTIME_KW */.    
2dd9e 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39    testcase( i==9
2dd9f 35 20 29 3b 20 2f 2a 20 54 4b 5f 50 52 49 4d 41  5 ); /* TK_PRIMA
2dda0 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RY */.      test
2dda1 63 61 73 65 28 20 69 3d 3d 39 36 20 29 3b 20 2f  case( i==96 ); /
2dda2 2a 20 54 4b 5f 44 45 46 45 52 52 45 44 20 2a 2f  * TK_DEFERRED */
2dda3 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dda4 20 69 3d 3d 39 37 20 29 3b 20 2f 2a 20 54 4b 5f   i==97 ); /* TK_
2dda5 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 20 20  DISTINCT */.    
2dda6 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39    testcase( i==9
2dda7 38 20 29 3b 20 2f 2a 20 54 4b 5f 49 53 20 2a 2f  8 ); /* TK_IS */
2dda8 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2dda9 20 69 3d 3d 39 39 20 29 3b 20 2f 2a 20 54 4b 5f   i==99 ); /* TK_
2ddaa 44 52 4f 50 20 2a 2f 0a 20 20 20 20 20 20 74 65  DROP */.      te
2ddab 73 74 63 61 73 65 28 20 69 3d 3d 31 30 30 20 29  stcase( i==100 )
2ddac 3b 20 2f 2a 20 54 4b 5f 46 41 49 4c 20 2a 2f 0a  ; /* TK_FAIL */.
2ddad 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ddae 69 3d 3d 31 30 31 20 29 3b 20 2f 2a 20 54 4b 5f  i==101 ); /* TK_
2ddaf 46 52 4f 4d 20 2a 2f 0a 20 20 20 20 20 20 74 65  FROM */.      te
2ddb0 73 74 63 61 73 65 28 20 69 3d 3d 31 30 32 20 29  stcase( i==102 )
2ddb1 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20  ; /* TK_JOIN_KW 
2ddb2 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2ddb3 65 28 20 69 3d 3d 31 30 33 20 29 3b 20 2f 2a 20  e( i==103 ); /* 
2ddb4 54 4b 5f 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20  TK_LIKE_KW */.  
2ddb5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2ddb6 3d 31 30 34 20 29 3b 20 2f 2a 20 54 4b 5f 42 59  =104 ); /* TK_BY
2ddb7 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2ddb8 73 65 28 20 69 3d 3d 31 30 35 20 29 3b 20 2f 2a  se( i==105 ); /*
2ddb9 20 54 4b 5f 49 46 20 2a 2f 0a 20 20 20 20 20 20   TK_IF */.      
2ddba 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 36  testcase( i==106
2ddbb 20 29 3b 20 2f 2a 20 54 4b 5f 49 53 4e 55 4c 4c   ); /* TK_ISNULL
2ddbc 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2ddbd 73 65 28 20 69 3d 3d 31 30 37 20 29 3b 20 2f 2a  se( i==107 ); /*
2ddbe 20 54 4b 5f 4f 52 44 45 52 20 2a 2f 0a 20 20 20   TK_ORDER */.   
2ddbf 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2ddc0 31 30 38 20 29 3b 20 2f 2a 20 54 4b 5f 52 45 53  108 ); /* TK_RES
2ddc1 54 52 49 43 54 20 2a 2f 0a 20 20 20 20 20 20 74  TRICT */.      t
2ddc2 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 39 20  estcase( i==109 
2ddc3 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b 57  ); /* TK_JOIN_KW
2ddc4 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2ddc5 73 65 28 20 69 3d 3d 31 31 30 20 29 3b 20 2f 2a  se( i==110 ); /*
2ddc6 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 20   TK_JOIN_KW */. 
2ddc7 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2ddc8 3d 3d 31 31 31 20 29 3b 20 2f 2a 20 54 4b 5f 52  ==111 ); /* TK_R
2ddc9 4f 4c 4c 42 41 43 4b 20 2a 2f 0a 20 20 20 20 20  OLLBACK */.     
2ddca 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31   testcase( i==11
2ddcb 32 20 29 3b 20 2f 2a 20 54 4b 5f 52 4f 57 20 2a  2 ); /* TK_ROW *
2ddcc 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2ddcd 28 20 69 3d 3d 31 31 33 20 29 3b 20 2f 2a 20 54  ( i==113 ); /* T
2ddce 4b 5f 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20 20 20  K_UNION */.     
2ddcf 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31   testcase( i==11
2ddd0 34 20 29 3b 20 2f 2a 20 54 4b 5f 55 53 49 4e 47  4 ); /* TK_USING
2ddd1 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2ddd2 73 65 28 20 69 3d 3d 31 31 35 20 29 3b 20 2f 2a  se( i==115 ); /*
2ddd3 20 54 4b 5f 56 41 43 55 55 4d 20 2a 2f 0a 20 20   TK_VACUUM */.  
2ddd4 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2ddd5 3d 31 31 36 20 29 3b 20 2f 2a 20 54 4b 5f 56 49  =116 ); /* TK_VI
2ddd6 45 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  EW */.      test
2ddd7 63 61 73 65 28 20 69 3d 3d 31 31 37 20 29 3b 20  case( i==117 ); 
2ddd8 2f 2a 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20  /* TK_INITIALLY 
2ddd9 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2ddda 65 28 20 69 3d 3d 31 31 38 20 29 3b 20 2f 2a 20  e( i==118 ); /* 
2dddb 54 4b 5f 41 4c 4c 20 2a 2f 0a 20 20 20 20 20 20  TK_ALL */.      
2dddc 72 65 74 75 72 6e 20 61 43 6f 64 65 5b 69 5d 3b  return aCode[i];
2dddd 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2ddde 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d 0a 53 51 4c  urn TK_ID;.}.SQL
2dddf 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2dde0 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f  sqlite3KeywordCo
2dde1 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  de(const unsigne
2dde2 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  d char *z, int n
2dde3 29 7b 0a 20 20 72 65 74 75 72 6e 20 6b 65 79 77  ){.  return keyw
2dde4 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a 29 7a  ordCode((char*)z
2dde5 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  , n);.}../******
2dde6 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2dde7 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 2a 2a  keywordhash.h **
2dde8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dde9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ddea 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2ddeb 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
2ddec 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
2dded 74 20 6f 66 66 20 69 6e 20 74 6f 6b 65 6e 69 7a  t off in tokeniz
2ddee 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
2ddef 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  *******/.../*.**
2ddf0 20 49 66 20 58 20 69 73 20 61 20 63 68 61 72 61   If X is a chara
2ddf1 63 74 65 72 20 74 68 61 74 20 63 61 6e 20 62 65  cter that can be
2ddf2 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64 65 6e   used in an iden
2ddf3 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a 20 49  tifier then.** I
2ddf4 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20 62 65  dChar(X) will be
2ddf5 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73   true.  Otherwis
2ddf6 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a  e it is false..*
2ddf7 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49 2c 20  *.** For ASCII, 
2ddf8 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 69  any character wi
2ddf9 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65  th the high-orde
2ddfa 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a 2a 20  r bit set is.** 
2ddfb 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20 69 64  allowed in an id
2ddfc 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72 20 37  entifier.  For 7
2ddfd 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 73 2c  -bit characters,
2ddfe 20 0a 2a 2a 20 73 71 6c 69 74 65 33 49 73 49 64   .** sqlite3IsId
2ddff 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62 65 20  Char[X] must be 
2de00 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 45 42 43  1..**.** For EBC
2de01 44 49 43 2c 20 74 68 65 20 72 75 6c 65 73 20 61  DIC, the rules a
2de02 72 65 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20  re more complex 
2de03 62 75 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  but have the sam
2de04 65 0a 2a 2a 20 65 6e 64 20 72 65 73 75 6c 74 2e  e.** end result.
2de05 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31  .**.** Ticket #1
2de06 30 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73 74  066.  the SQL st
2de07 61 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74 20  andard does not 
2de08 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68 65  allow '$' in the
2de09 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69 64  .** middle of id
2de0a 65 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20 6d  entfiers.  But m
2de0b 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65 6e  any SQL implemen
2de0c 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a 20  tations do. .** 
2de0d 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f  SQLite will allo
2de0e 77 20 27 24 27 20 69 6e 20 69 64 65 6e 74 69 66  w '$' in identif
2de0f 69 65 72 73 20 66 6f 72 20 63 6f 6d 70 61 74 69  iers for compati
2de10 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 74  bility..** But t
2de11 68 65 20 66 65 61 74 75 72 65 20 69 73 20 75 6e  he feature is un
2de12 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 23  documented..*/.#
2de13 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
2de14 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  II.SQLITE_PRIVAT
2de15 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c  E const char sql
2de16 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68 61  ite3IsAsciiIdCha
2de17 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31  r[] = {./* x0 x1
2de18 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36 20   x2 x3 x4 x5 x6 
2de19 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20 78  x7 x8 x9 xA xB x
2de1a 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20  C xD xE xF */.  
2de1b 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c    0, 0, 0, 0, 1,
2de1c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2de1d 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2de1e 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20  ,  /* 2x */.    
2de1f 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2de20 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
2de21 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2de22 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 3x */.    0,
2de23 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2de24 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2de25 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f  , 1, 1, 1, 1,  /
2de26 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31  * 4x */.    1, 1
2de27 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2de28 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
2de29 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20  0, 0, 0, 1,  /* 
2de2a 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  5x */.    0, 1, 
2de2b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2de2c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2de2d 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78   1, 1, 1,  /* 6x
2de2e 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
2de2f 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2de30 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
2de31 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a  , 0, 0,  /* 7x *
2de32 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43  /.};.#define IdC
2de33 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29 26  har(C)  (((c=C)&
2de34 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e 30  0x80)!=0 || (c>0
2de35 78 31 66 20 26 26 20 73 71 6c 69 74 65 33 49 73  x1f && sqlite3Is
2de36 41 73 63 69 69 49 64 43 68 61 72 5b 63 2d 30 78  AsciiIdChar[c-0x
2de37 32 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 69 66  20])).#endif.#if
2de38 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49  def SQLITE_EBCDI
2de39 43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  C.SQLITE_PRIVATE
2de3a 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69   const char sqli
2de3b 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68 61  te3IsEbcdicIdCha
2de3c 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31  r[] = {./* x0 x1
2de3d 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36 20   x2 x3 x4 x5 x6 
2de3e 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20 78  x7 x8 x9 xA xB x
2de3f 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20  C xD xE xF */.  
2de40 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c    0, 0, 1, 1, 1,
2de41 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2de42 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2de43 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20  ,  /* 4x */.    
2de44 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
2de45 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
2de46 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 0, 0, 0, 0, 
2de47 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 5x */.    0,
2de48 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2de49 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
2de4a 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 1, 0, 0,  /
2de4b 2a 20 36 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 6x */.    0, 1
2de4c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2de4d 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
2de4e 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
2de4f 37 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  7x */.    0, 1, 
2de50 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2de51 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c  , 1, 1, 0, 0, 1,
2de52 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 38 78   1, 1, 0,  /* 8x
2de53 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
2de54 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2de55 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20 30  1, 1, 0, 0, 1, 0
2de56 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 39 78 20 2a  , 1, 0,  /* 9x *
2de57 2f 0a 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 31  /.    1, 0, 1, 1
2de58 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2de59 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 1, 0, 1, 1, 
2de5a 31 2c 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a  1, 0,  /* Ax */.
2de5b 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
2de5c 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2de5d 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2de5e 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a 20 20   0,  /* Bx */.  
2de5f 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
2de60 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2de61 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
2de62 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20  ,  /* Cx */.    
2de63 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
2de64 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
2de65 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2de66 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 30 2c   /* Dx */.    0,
2de67 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2de68 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
2de69 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f  , 1, 1, 1, 1,  /
2de6a 2a 20 45 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31  * Ex */.    1, 1
2de6b 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2de6c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20   1, 1, 1, 0, 1, 
2de6d 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20  1, 1, 1, 0,  /* 
2de6e 46 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65  Fx */.};.#define
2de6f 20 49 64 43 68 61 72 28 43 29 20 20 28 28 28 63   IdChar(C)  (((c
2de70 3d 43 29 3e 3d 30 78 34 32 20 26 26 20 73 71 6c  =C)>=0x42 && sql
2de71 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68  ite3IsEbcdicIdCh
2de72 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e  ar[c-0x40])).#en
2de73 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  dif.../*.** Retu
2de74 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  rn the length of
2de75 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20   the token that 
2de76 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e 20  begins at z[0]. 
2de77 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f  .** Store the to
2de78 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f 6b  ken type in *tok
2de79 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20 72 65  enType before re
2de7a 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  turning..*/.SQLI
2de7b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2de7c 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63  qlite3GetToken(c
2de7d 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
2de7e 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65  ar *z, int *toke
2de7f 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 69 2c  nType){.  int i,
2de80 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20 2a 7a   c;.  switch( *z
2de81 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 20 27   ){.    case ' '
2de82 3a 20 63 61 73 65 20 27 5c 74 27 3a 20 63 61 73  : case '\t': cas
2de83 65 20 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c 66  e '\n': case '\f
2de84 27 3a 20 63 61 73 65 20 27 5c 72 27 3a 20 7b 0a  ': case '\r': {.
2de85 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69        for(i=1; i
2de86 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b  sspace(z[i]); i+
2de87 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65  +){}.      *toke
2de88 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45  nType = TK_SPACE
2de89 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
2de8a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2de8b 20 27 2d 27 3a 20 7b 0a 20 20 20 20 20 20 69 66   '-': {.      if
2de8c 28 20 7a 5b 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20  ( z[1]=='-' ){. 
2de8d 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32 3b 20         for(i=2; 
2de8e 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20 63  (c=z[i])!=0 && c
2de8f 21 3d 27 5c 6e 27 3b 20 69 2b 2b 29 7b 7d 0a 20  !='\n'; i++){}. 
2de90 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
2de91 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20  e = TK_SPACE;.  
2de92 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
2de93 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 74        }.      *t
2de94 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4d 49  okenType = TK_MI
2de95 4e 55 53 3b 0a 20 20 20 20 20 20 72 65 74 75 72  NUS;.      retur
2de96 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
2de97 61 73 65 20 27 28 27 3a 20 7b 0a 20 20 20 20 20  ase '(': {.     
2de98 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2de99 5f 4c 50 3b 0a 20 20 20 20 20 20 72 65 74 75 72  _LP;.      retur
2de9a 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
2de9b 61 73 65 20 27 29 27 3a 20 7b 0a 20 20 20 20 20  ase ')': {.     
2de9c 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2de9d 5f 52 50 3b 0a 20 20 20 20 20 20 72 65 74 75 72  _RP;.      retur
2de9e 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
2de9f 61 73 65 20 27 3b 27 3a 20 7b 0a 20 20 20 20 20  ase ';': {.     
2dea0 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2dea1 5f 53 45 4d 49 3b 0a 20 20 20 20 20 20 72 65 74  _SEMI;.      ret
2dea2 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
2dea3 20 63 61 73 65 20 27 2b 27 3a 20 7b 0a 20 20 20   case '+': {.   
2dea4 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2dea5 54 4b 5f 50 4c 55 53 3b 0a 20 20 20 20 20 20 72  TK_PLUS;.      r
2dea6 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
2dea7 20 20 20 63 61 73 65 20 27 2a 27 3a 20 7b 0a 20     case '*': {. 
2dea8 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2dea9 3d 20 54 4b 5f 53 54 41 52 3b 0a 20 20 20 20 20  = TK_STAR;.     
2deaa 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2deab 0a 20 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b  .    case '/': {
2deac 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21  .      if( z[1]!
2dead 3d 27 2a 27 20 7c 7c 20 7a 5b 32 5d 3d 3d 30 20  ='*' || z[2]==0 
2deae 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  ){.        *toke
2deaf 6e 54 79 70 65 20 3d 20 54 4b 5f 53 4c 41 53 48  nType = TK_SLASH
2deb0 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2deb1 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
2deb2 20 20 66 6f 72 28 69 3d 33 2c 20 63 3d 7a 5b 32    for(i=3, c=z[2
2deb3 5d 3b 20 28 63 21 3d 27 2a 27 20 7c 7c 20 7a 5b  ]; (c!='*' || z[
2deb4 69 5d 21 3d 27 2f 27 29 20 26 26 20 28 63 3d 7a  i]!='/') && (c=z
2deb5 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a  [i])!=0; i++){}.
2deb6 20 20 20 20 20 20 69 66 28 20 63 20 29 20 69 2b        if( c ) i+
2deb7 2b 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  +;.      *tokenT
2deb8 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a  ype = TK_SPACE;.
2deb9 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
2deba 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2debb 25 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  %': {.      *tok
2debc 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 45 4d 3b  enType = TK_REM;
2debd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2debe 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2debf 27 3d 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '=': {.      *to
2dec0 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 45 51 3b  kenType = TK_EQ;
2dec1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 20  .      return 1 
2dec2 2b 20 28 7a 5b 31 5d 3d 3d 27 3d 27 29 3b 0a 20  + (z[1]=='=');. 
2dec3 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3c     }.    case '<
2dec4 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28  ': {.      if( (
2dec5 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a  c=z[1])=='=' ){.
2dec6 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2dec7 70 65 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20  pe = TK_LE;.    
2dec8 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
2dec9 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
2deca 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='>' ){.        
2decb 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2decc 4e 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  NE;.        retu
2decd 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 2;.      }els
2dece 65 20 69 66 28 20 63 3d 3d 27 3c 27 20 29 7b 0a  e if( c=='<' ){.
2decf 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2ded0 70 65 20 3d 20 54 4b 5f 4c 53 48 49 46 54 3b 0a  pe = TK_LSHIFT;.
2ded1 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
2ded2 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2ded3 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
2ded4 65 20 3d 20 54 4b 5f 4c 54 3b 0a 20 20 20 20 20  e = TK_LT;.     
2ded5 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
2ded6 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63     }.    }.    c
2ded7 61 73 65 20 27 3e 27 3a 20 7b 0a 20 20 20 20 20  ase '>': {.     
2ded8 20 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27   if( (c=z[1])=='
2ded9 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  =' ){.        *t
2deda 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47 45  okenType = TK_GE
2dedb 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2dedc 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20   2;.      }else 
2dedd 69 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20 20  if( c=='>' ){.  
2dede 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2dedf 20 3d 20 54 4b 5f 52 53 48 49 46 54 3b 0a 20 20   = TK_RSHIFT;.  
2dee0 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
2dee1 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2dee2 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2dee3 3d 20 54 4b 5f 47 54 3b 0a 20 20 20 20 20 20 20  = TK_GT;.       
2dee4 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
2dee5 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73   }.    }.    cas
2dee6 65 20 27 21 27 3a 20 7b 0a 20 20 20 20 20 20 69  e '!': {.      i
2dee7 66 28 20 7a 5b 31 5d 21 3d 27 3d 27 20 29 7b 0a  f( z[1]!='=' ){.
2dee8 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2dee9 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
2deea 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2deeb 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  2;.      }else{.
2deec 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2deed 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20 20  pe = TK_NE;.    
2deee 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
2deef 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2def0 63 61 73 65 20 27 7c 27 3a 20 7b 0a 20 20 20 20  case '|': {.    
2def1 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 7c 27 20    if( z[1]!='|' 
2def2 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  ){.        *toke
2def3 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 4f 52  nType = TK_BITOR
2def4 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2def5 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   1;.      }else{
2def6 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
2def7 79 70 65 20 3d 20 54 4b 5f 43 4f 4e 43 41 54 3b  ype = TK_CONCAT;
2def8 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2def9 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  2;.      }.    }
2defa 0a 20 20 20 20 63 61 73 65 20 27 2c 27 3a 20 7b  .    case ',': {
2defb 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
2defc 65 20 3d 20 54 4b 5f 43 4f 4d 4d 41 3b 0a 20 20  e = TK_COMMA;.  
2defd 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2defe 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 26 27    }.    case '&'
2deff 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  : {.      *token
2df00 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 41 4e 44  Type = TK_BITAND
2df01 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
2df02 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2df03 20 27 7e 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74   '~': {.      *t
2df04 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49  okenType = TK_BI
2df05 54 4e 4f 54 3b 0a 20 20 20 20 20 20 72 65 74 75  TNOT;.      retu
2df06 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
2df07 63 61 73 65 20 27 60 27 3a 0a 20 20 20 20 63 61  case '`':.    ca
2df08 73 65 20 27 5c 27 27 3a 0a 20 20 20 20 63 61 73  se '\'':.    cas
2df09 65 20 27 22 27 3a 20 7b 0a 20 20 20 20 20 20 69  e '"': {.      i
2df0a 6e 74 20 64 65 6c 69 6d 20 3d 20 7a 5b 30 5d 3b  nt delim = z[0];
2df0b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  .      for(i=1; 
2df0c 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b  (c=z[i])!=0; i++
2df0d 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  ){.        if( c
2df0e 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20  ==delim ){.     
2df0f 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d       if( z[i+1]=
2df10 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20  =delim ){.      
2df11 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
2df12 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2df13 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2df14 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2df15 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2df16 20 20 20 69 66 28 20 63 3d 3d 27 5c 27 27 20 29     if( c=='\'' )
2df17 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
2df18 54 79 70 65 20 3d 20 54 4b 5f 53 54 52 49 4e 47  Type = TK_STRING
2df19 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2df1a 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73   i+1;.      }els
2df1b 65 20 69 66 28 20 63 21 3d 30 20 29 7b 0a 20 20  e if( c!=0 ){.  
2df1c 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2df1d 20 3d 20 54 4b 5f 49 44 3b 0a 20 20 20 20 20 20   = TK_ID;.      
2df1e 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20    return i+1;.  
2df1f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2df20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2df21 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20  TK_ILLEGAL;.    
2df22 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
2df23 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2df24 63 61 73 65 20 27 2e 27 3a 20 7b 0a 23 69 66 6e  case '.': {.#ifn
2df25 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2df26 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20  FLOATING_POINT. 
2df27 20 20 20 20 20 69 66 28 20 21 69 73 64 69 67 69       if( !isdigi
2df28 74 28 7a 5b 31 5d 29 20 29 0a 23 65 6e 64 69 66  t(z[1]) ).#endif
2df29 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
2df2a 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2df2b 5f 44 4f 54 3b 0a 20 20 20 20 20 20 20 20 72 65  _DOT;.        re
2df2c 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
2df2d 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
2df2e 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 69  next character i
2df2f 73 20 61 20 64 69 67 69 74 2c 20 74 68 69 73 20  s a digit, this 
2df30 69 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  is a floating po
2df31 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d  int.      ** num
2df32 62 65 72 20 74 68 61 74 20 62 65 67 69 6e 73 20  ber that begins 
2df33 77 69 74 68 20 22 2e 22 2e 20 20 46 61 6c 6c 20  with ".".  Fall 
2df34 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e 65  thru into the ne
2df35 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d  xt case */.    }
2df36 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 20 63  .    case '0': c
2df37 61 73 65 20 27 31 27 3a 20 63 61 73 65 20 27 32  ase '1': case '2
2df38 27 3a 20 63 61 73 65 20 27 33 27 3a 20 63 61 73  ': case '3': cas
2df39 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 20  e '4':.    case 
2df3a 27 35 27 3a 20 63 61 73 65 20 27 36 27 3a 20 63  '5': case '6': c
2df3b 61 73 65 20 27 37 27 3a 20 63 61 73 65 20 27 38  ase '7': case '8
2df3c 27 3a 20 63 61 73 65 20 27 39 27 3a 20 7b 0a 20  ': case '9': {. 
2df3d 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2df3e 3d 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20  = TK_INTEGER;.  
2df3f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 73 64      for(i=0; isd
2df40 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  igit(z[i]); i++)
2df41 7b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  {}.#ifndef SQLIT
2df42 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
2df43 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28 20  POINT.      if( 
2df44 7a 5b 69 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  z[i]=='.' ){.   
2df45 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
2df46 20 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74    while( isdigit
2df47 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d  (z[i]) ){ i++; }
2df48 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
2df49 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a  ype = TK_FLOAT;.
2df4a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2df4b 28 20 28 7a 5b 69 5d 3d 3d 27 65 27 20 7c 7c 20  ( (z[i]=='e' || 
2df4c 7a 5b 69 5d 3d 3d 27 45 27 29 20 26 26 0a 20 20  z[i]=='E') &&.  
2df4d 20 20 20 20 20 20 20 20 20 28 20 69 73 64 69 67           ( isdig
2df4e 69 74 28 7a 5b 69 2b 31 5d 29 20 0a 20 20 20 20  it(z[i+1]) .    
2df4f 20 20 20 20 20 20 20 20 7c 7c 20 28 28 7a 5b 69          || ((z[i
2df50 2b 31 5d 3d 3d 27 2b 27 20 7c 7c 20 7a 5b 69 2b  +1]=='+' || z[i+
2df51 31 5d 3d 3d 27 2d 27 29 20 26 26 20 69 73 64 69  1]=='-') && isdi
2df52 67 69 74 28 7a 5b 69 2b 32 5d 29 29 0a 20 20 20  git(z[i+2])).   
2df53 20 20 20 20 20 20 20 20 29 0a 20 20 20 20 20 20          ).      
2df54 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20  ){.        i += 
2df55 32 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  2;.        while
2df56 28 20 69 73 64 69 67 69 74 28 7a 5b 69 5d 29 20  ( isdigit(z[i]) 
2df57 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20  ){ i++; }.      
2df58 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2df59 4b 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d  K_FLOAT;.      }
2df5a 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 77 68  .#endif.      wh
2df5b 69 6c 65 28 20 49 64 43 68 61 72 28 7a 5b 69 5d  ile( IdChar(z[i]
2df5c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  ) ){.        *to
2df5d 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c  kenType = TK_ILL
2df5e 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 69 2b  EGAL;.        i+
2df5f 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  +;.      }.     
2df60 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d   return i;.    }
2df61 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20 7b  .    case '[': {
2df62 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 2c 20  .      for(i=1, 
2df63 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27 5d 27 20 26  c=z[0]; c!=']' &
2df64 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69  & (c=z[i])!=0; i
2df65 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b  ++){}.      *tok
2df66 65 6e 54 79 70 65 20 3d 20 63 3d 3d 27 5d 27 20  enType = c==']' 
2df67 3f 20 54 4b 5f 49 44 20 3a 20 54 4b 5f 49 4c 4c  ? TK_ID : TK_ILL
2df68 45 47 41 4c 3b 0a 20 20 20 20 20 20 72 65 74 75  EGAL;.      retu
2df69 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn i;.    }.    
2df6a 63 61 73 65 20 27 3f 27 3a 20 7b 0a 20 20 20 20  case '?': {.    
2df6b 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2df6c 4b 5f 56 41 52 49 41 42 4c 45 3b 0a 20 20 20 20  K_VARIABLE;.    
2df6d 20 20 66 6f 72 28 69 3d 31 3b 20 69 73 64 69 67    for(i=1; isdig
2df6e 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  it(z[i]); i++){}
2df6f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
2df70 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2df71 27 23 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72  '#': {.      for
2df72 28 69 3d 31 3b 20 69 73 64 69 67 69 74 28 7a 5b  (i=1; isdigit(z[
2df73 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20  i]); i++){}.    
2df74 20 20 69 66 28 20 69 3e 31 20 29 7b 0a 20 20 20    if( i>1 ){.   
2df75 20 20 20 20 20 2f 2a 20 50 61 72 61 6d 65 74 65       /* Paramete
2df76 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 23  rs of the form #
2df77 4e 4e 4e 20 28 77 68 65 72 65 20 4e 4e 4e 20 69  NNN (where NNN i
2df78 73 20 61 20 6e 75 6d 62 65 72 29 20 61 72 65 20  s a number) are 
2df79 75 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  used.        ** 
2df7a 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 73 71  internally by sq
2df7b 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2df7c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 2a 74  .  */.        *t
2df7d 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 45  okenType = TK_RE
2df7e 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 20 20  GISTER;.        
2df7f 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20  return i;.      
2df80 7d 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20  }.      /* Fall 
2df81 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74 68 65  through into the
2df82 20 6e 65 78 74 20 63 61 73 65 20 69 66 20 74 68   next case if th
2df83 65 20 27 23 27 20 69 73 20 6e 6f 74 20 66 6f 6c  e '#' is not fol
2df84 6c 6f 77 65 64 20 62 79 0a 20 20 20 20 20 20 2a  lowed by.      *
2df85 2a 20 61 20 64 69 67 69 74 2e 20 54 72 79 20 74  * a digit. Try t
2df86 6f 20 6d 61 74 63 68 20 23 41 41 41 41 20 77 68  o match #AAAA wh
2df87 65 72 65 20 41 41 41 41 20 69 73 20 61 20 70 61  ere AAAA is a pa
2df88 72 61 6d 65 74 65 72 20 6e 61 6d 65 2e 20 2a 2f  rameter name. */
2df89 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
2df8a 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56  QLITE_OMIT_TCL_V
2df8b 41 52 49 41 42 4c 45 0a 20 20 20 20 63 61 73 65  ARIABLE.    case
2df8c 20 27 24 27 3a 0a 23 65 6e 64 69 66 0a 20 20 20   '$':.#endif.   
2df8d 20 63 61 73 65 20 27 40 27 3a 20 20 2f 2a 20 46   case '@':  /* F
2df8e 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  or compatibility
2df8f 20 77 69 74 68 20 4d 53 20 53 51 4c 20 53 65 72   with MS SQL Ser
2df90 76 65 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ver */.    case 
2df91 27 3a 27 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ':': {.      int
2df92 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a 74   n = 0;.      *t
2df93 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56 41  okenType = TK_VA
2df94 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66 6f  RIABLE;.      fo
2df95 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=1; (c=z[i])!
2df96 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  =0; i++){.      
2df97 20 20 69 66 28 20 49 64 43 68 61 72 28 63 29 20    if( IdChar(c) 
2df98 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b  ){.          n++
2df99 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2df9a 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42  _OMIT_TCL_VARIAB
2df9b 4c 45 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  LE.        }else
2df9c 20 69 66 28 20 63 3d 3d 27 28 27 20 26 26 20 6e   if( c=='(' && n
2df9d 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >0 ){.          
2df9e 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  do{.            
2df9f 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  i++;.          }
2dfa0 77 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d 29 21  while( (c=z[i])!
2dfa1 3d 30 20 26 26 20 21 69 73 73 70 61 63 65 28 63  =0 && !isspace(c
2dfa2 29 20 26 26 20 63 21 3d 27 29 27 20 29 3b 0a 20  ) && c!=')' );. 
2dfa3 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d           if( c==
2dfa4 27 29 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ')' ){.         
2dfa5 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20     i++;.        
2dfa6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2dfa7 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2dfa8 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20  = TK_ILLEGAL;.  
2dfa9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2dfaa 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2dfab 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d     }else if( c==
2dfac 27 3a 27 20 26 26 20 7a 5b 69 2b 31 5d 3d 3d 27  ':' && z[i+1]=='
2dfad 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  :' ){.          
2dfae 69 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  i++;.#endif.    
2dfaf 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2dfb0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2dfb1 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2dfb2 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 2a      if( n==0 ) *
2dfb3 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
2dfb4 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 72 65  LLEGAL;.      re
2dfb5 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 23 69  turn i;.    }.#i
2dfb6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2dfb7 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20  T_BLOB_LITERAL. 
2dfb8 20 20 20 63 61 73 65 20 27 78 27 3a 20 63 61 73     case 'x': cas
2dfb9 65 20 27 58 27 3a 20 7b 0a 20 20 20 20 20 20 69  e 'X': {.      i
2dfba 66 28 20 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 7b  f( z[1]=='\'' ){
2dfbb 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
2dfbc 79 70 65 20 3d 20 54 4b 5f 42 4c 4f 42 3b 0a 20  ype = TK_BLOB;. 
2dfbd 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32 3b 20         for(i=2; 
2dfbe 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20 63  (c=z[i])!=0 && c
2dfbf 21 3d 27 5c 27 27 3b 20 69 2b 2b 29 7b 0a 20 20  !='\''; i++){.  
2dfc0 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 78          if( !isx
2dfc1 64 69 67 69 74 28 63 29 20 29 7b 0a 20 20 20 20  digit(c) ){.    
2dfc2 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2dfc3 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
2dfc4 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2dfc5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
2dfc6 66 28 20 69 25 32 20 7c 7c 20 21 63 20 29 20 2a  f( i%2 || !c ) *
2dfc7 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
2dfc8 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20  LLEGAL;.        
2dfc9 69 66 28 20 63 20 29 20 69 2b 2b 3b 0a 20 20 20  if( c ) i++;.   
2dfca 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
2dfcb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
2dfcc 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74  Otherwise fall t
2dfcd 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
2dfce 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d  xt case */.    }
2dfcf 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 65 66 61  .#endif.    defa
2dfd0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  ult: {.      if(
2dfd1 20 21 49 64 43 68 61 72 28 2a 7a 29 20 29 7b 0a   !IdChar(*z) ){.
2dfd2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2dfd3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
2dfd4 28 69 3d 31 3b 20 49 64 43 68 61 72 28 7a 5b 69  (i=1; IdChar(z[i
2dfd5 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20  ]); i++){}.     
2dfd6 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 6b 65   *tokenType = ke
2dfd7 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a  ywordCode((char*
2dfd8 29 7a 2c 20 69 29 3b 0a 20 20 20 20 20 20 72 65  )z, i);.      re
2dfd9 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
2dfda 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d  }.  *tokenType =
2dfdb 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 72   TK_ILLEGAL;.  r
2dfdc 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
2dfdd 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65 72  * Run the parser
2dfde 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 53 51   on the given SQ
2dfdf 4c 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 70  L string.  The p
2dfe0 61 72 73 65 72 20 73 74 72 75 63 74 75 72 65 20  arser structure 
2dfe1 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e 2e  is.** passed in.
2dfe2 20 20 41 6e 20 53 51 4c 49 54 45 5f 20 73 74 61    An SQLITE_ sta
2dfe3 74 75 73 20 63 6f 64 65 20 69 73 20 72 65 74 75  tus code is retu
2dfe4 72 6e 65 64 2e 20 20 49 66 20 61 6e 20 65 72 72  rned.  If an err
2dfe5 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68 65  or occurs.** the
2dfe6 6e 20 61 6e 20 61 6e 64 20 61 74 74 65 6d 70 74  n an and attempt
2dfe7 20 69 73 20 6d 61 64 65 20 74 6f 20 77 72 69 74   is made to writ
2dfe8 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
2dfe9 67 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f  ge into .** memo
2dfea 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
2dfeb 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
2dfec 29 20 61 6e 64 20 74 6f 20 6d 61 6b 65 20 2a 70  ) and to make *p
2dfed 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20 74 6f  zErrMsg point to
2dfee 20 74 68 61 74 0a 2a 2a 20 65 72 72 6f 72 20 6d   that.** error m
2dfef 65 73 73 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54  essage..*/.SQLIT
2dff0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2dff1 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 50  lite3RunParser(P
2dff2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
2dff3 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
2dff4 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29  char **pzErrMsg)
2dff5 7b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 30  {.  int nErr = 0
2dff6 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 76 6f 69  ;.  int i;.  voi
2dff7 64 20 2a 70 45 6e 67 69 6e 65 3b 0a 20 20 69 6e  d *pEngine;.  in
2dff8 74 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 69  t tokenType;.  i
2dff9 6e 74 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73  nt lastTokenPars
2dffa 65 64 20 3d 20 2d 31 3b 0a 20 20 73 71 6c 69 74  ed = -1;.  sqlit
2dffb 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2dffc 3e 64 62 3b 0a 20 20 69 6e 74 20 6d 78 53 71 6c  >db;.  int mxSql
2dffd 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74  Len = db->aLimit
2dffe 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51  [SQLITE_LIMIT_SQ
2dfff 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 0a 20 20 69 66  L_LENGTH];..  if
2e000 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  ( db->activeVdbe
2e001 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62  Cnt==0 ){.    db
2e002 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
2e003 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50  ed = 0;.  }.  pP
2e004 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
2e005 45 5f 4f 4b 3b 0a 20 20 70 50 61 72 73 65 2d 3e  E_OK;.  pParse->
2e006 7a 54 61 69 6c 20 3d 20 70 50 61 72 73 65 2d 3e  zTail = pParse->
2e007 7a 53 71 6c 20 3d 20 7a 53 71 6c 3b 0a 20 20 69  zSql = zSql;.  i
2e008 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
2e009 70 7a 45 72 72 4d 73 67 21 3d 30 20 29 3b 0a 20  pzErrMsg!=0 );. 
2e00a 20 70 45 6e 67 69 6e 65 20 3d 20 73 71 6c 69 74   pEngine = sqlit
2e00b 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 28 76  e3ParserAlloc((v
2e00c 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29  oid*(*)(size_t))
2e00d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 29 3b 0a  sqlite3Malloc);.
2e00e 20 20 69 66 28 20 70 45 6e 67 69 6e 65 3d 3d 30    if( pEngine==0
2e00f 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c   ){.    db->mall
2e010 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
2e011 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2e012 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73  NOMEM;.  }.  ass
2e013 65 72 74 28 20 70 50 61 72 73 65 2d 3e 73 4c 61  ert( pParse->sLa
2e014 73 74 54 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20 29  stToken.dyn==0 )
2e015 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
2e016 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30  se->pNewTable==0
2e017 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
2e018 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
2e019 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  r==0 );.  assert
2e01a 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3d 3d  ( pParse->nVar==
2e01b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2e01c 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3d  Parse->nVarExpr=
2e01d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2e01e 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
2e01f 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 61 73  Alloc==0 );.  as
2e020 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70  sert( pParse->ap
2e021 56 61 72 45 78 70 72 3d 3d 30 20 29 3b 0a 20 20  VarExpr==0 );.  
2e022 77 68 69 6c 65 28 20 21 64 62 2d 3e 6d 61 6c 6c  while( !db->mall
2e023 6f 63 46 61 69 6c 65 64 20 26 26 20 7a 53 71 6c  ocFailed && zSql
2e024 5b 69 5d 21 3d 30 20 29 7b 0a 20 20 20 20 61 73  [i]!=0 ){.    as
2e025 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20  sert( i>=0 );.  
2e026 20 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54    pParse->sLastT
2e027 6f 6b 65 6e 2e 7a 20 3d 20 28 75 38 2a 29 26 7a  oken.z = (u8*)&z
2e028 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65  Sql[i];.    asse
2e029 72 74 28 20 70 50 61 72 73 65 2d 3e 73 4c 61 73  rt( pParse->sLas
2e02a 74 54 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20 29 3b  tToken.dyn==0 );
2e02b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4c 61  .    pParse->sLa
2e02c 73 74 54 6f 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69  stToken.n = sqli
2e02d 74 65 33 47 65 74 54 6f 6b 65 6e 28 28 75 6e 73  te3GetToken((uns
2e02e 69 67 6e 65 64 20 63 68 61 72 2a 29 26 7a 53 71  igned char*)&zSq
2e02f 6c 5b 69 5d 2c 26 74 6f 6b 65 6e 54 79 70 65 29  l[i],&tokenType)
2e030 3b 0a 20 20 20 20 69 20 2b 3d 20 70 50 61 72 73  ;.    i += pPars
2e031 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b  e->sLastToken.n;
2e032 0a 20 20 20 20 69 66 28 20 69 3e 6d 78 53 71 6c  .    if( i>mxSql
2e033 4c 65 6e 20 29 7b 0a 20 20 20 20 20 20 70 50 61  Len ){.      pPa
2e034 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
2e035 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 20 20 62  _TOOBIG;.      b
2e036 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2e037 73 77 69 74 63 68 28 20 74 6f 6b 65 6e 54 79 70  switch( tokenTyp
2e038 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  e ){.      case 
2e039 54 4b 5f 53 50 41 43 45 3a 20 7b 0a 20 20 20 20  TK_SPACE: {.    
2e03a 20 20 20 20 69 66 28 20 64 62 2d 3e 75 31 2e 69      if( db->u1.i
2e03b 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 7b 0a  sInterrupted ){.
2e03c 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
2e03d 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e  ->rc = SQLITE_IN
2e03e 54 45 52 52 55 50 54 3b 0a 20 20 20 20 20 20 20  TERRUPT;.       
2e03f 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
2e040 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62  ing(pzErrMsg, db
2e041 2c 20 22 69 6e 74 65 72 72 75 70 74 22 29 3b 0a  , "interrupt");.
2e042 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61            goto a
2e043 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20  bort_parse;.    
2e044 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72      }.        br
2e045 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2e046 20 20 20 63 61 73 65 20 54 4b 5f 49 4c 4c 45 47     case TK_ILLEG
2e047 41 4c 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  AL: {.        sq
2e048 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2e049 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  *pzErrMsg);.    
2e04a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
2e04b 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2e04c 62 2c 20 22 75 6e 72 65 63 6f 67 6e 69 7a 65 64  b, "unrecognized
2e04d 20 74 6f 6b 65 6e 3a 20 5c 22 25 54 5c 22 22 2c   token: \"%T\"",
2e04e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e04f 20 20 20 20 20 20 20 20 20 26 70 50 61 72 73 65           &pParse
2e050 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 29 3b 0a 20  ->sLastToken);. 
2e051 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20         nErr++;. 
2e052 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
2e053 74 5f 70 61 72 73 65 3b 0a 20 20 20 20 20 20 7d  t_parse;.      }
2e054 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53  .      case TK_S
2e055 45 4d 49 3a 20 7b 0a 20 20 20 20 20 20 20 20 70  EMI: {.        p
2e056 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26  Parse->zTail = &
2e057 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20  zSql[i];.       
2e058 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e   /* Fall thru in
2e059 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  to the default c
2e05a 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ase */.      }. 
2e05b 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
2e05c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
2e05d 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 74  arser(pEngine, t
2e05e 6f 6b 65 6e 54 79 70 65 2c 20 70 50 61 72 73 65  okenType, pParse
2e05f 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50  ->sLastToken, pP
2e060 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 6c  arse);.        l
2e061 61 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d  astTokenParsed =
2e062 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 20 20   tokenType;.    
2e063 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
2e064 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2e065 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
2e066 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20  abort_parse;.   
2e067 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
2e068 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2e069 20 20 7d 0a 20 20 7d 0a 61 62 6f 72 74 5f 70 61    }.  }.abort_pa
2e06a 72 73 65 3a 0a 20 20 69 66 28 20 7a 53 71 6c 5b  rse:.  if( zSql[
2e06b 69 5d 3d 3d 30 20 26 26 20 6e 45 72 72 3d 3d 30  i]==0 && nErr==0
2e06c 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d   && pParse->rc==
2e06d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2e06e 20 69 66 28 20 6c 61 73 74 54 6f 6b 65 6e 50 61   if( lastTokenPa
2e06f 72 73 65 64 21 3d 54 4b 5f 53 45 4d 49 20 29 7b  rsed!=TK_SEMI ){
2e070 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61  .      sqlite3Pa
2e071 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 54 4b  rser(pEngine, TK
2e072 5f 53 45 4d 49 2c 20 70 50 61 72 73 65 2d 3e 73  _SEMI, pParse->s
2e073 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73  LastToken, pPars
2e074 65 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  e);.      pParse
2e075 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b  ->zTail = &zSql[
2e076 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  i];.    }.    sq
2e077 6c 69 74 65 33 50 61 72 73 65 72 28 70 45 6e 67  lite3Parser(pEng
2e078 69 6e 65 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e  ine, 0, pParse->
2e079 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72  sLastToken, pPar
2e07a 73 65 29 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20  se);.  }.#ifdef 
2e07b 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
2e07c 45 50 54 48 0a 20 20 73 71 6c 69 74 65 33 53 74  EPTH.  sqlite3St
2e07d 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
2e07e 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54 41  TATUS_PARSER_STA
2e07f 43 4b 2c 0a 20 20 20 20 20 20 73 71 6c 69 74 65  CK,.      sqlite
2e080 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b  3ParserStackPeak
2e081 28 70 45 6e 67 69 6e 65 29 0a 20 20 29 3b 0a 23  (pEngine).  );.#
2e082 65 6e 64 69 66 20 2f 2a 20 59 59 44 45 42 55 47  endif /* YYDEBUG
2e083 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
2e084 73 65 72 46 72 65 65 28 70 45 6e 67 69 6e 65 2c  serFree(pEngine,
2e085 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
2e086 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2e087 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 50  Failed ){.    pP
2e088 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
2e089 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69  E_NOMEM;.  }.  i
2e08a 66 28 20 70 50 61 72 73 65 2d 3e 72 63 21 3d 53  f( pParse->rc!=S
2e08b 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 72  QLITE_OK && pPar
2e08c 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44  se->rc!=SQLITE_D
2e08d 4f 4e 45 20 26 26 20 70 50 61 72 73 65 2d 3e 7a  ONE && pParse->z
2e08e 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a 20 20 20  ErrMsg==0 ){.   
2e08f 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
2e090 67 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  g(&pParse->zErrM
2e091 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
2e092 6c 69 74 65 33 45 72 72 53 74 72 28 70 50 61 72  lite3ErrStr(pPar
2e093 73 65 2d 3e 72 63 29 29 3b 0a 20 20 7d 0a 20 20  se->rc));.  }.  
2e094 69 66 28 20 70 50 61 72 73 65 2d 3e 7a 45 72 72  if( pParse->zErr
2e095 4d 73 67 20 29 7b 0a 20 20 20 20 69 66 28 20 2a  Msg ){.    if( *
2e096 70 7a 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a 20  pzErrMsg==0 ){. 
2e097 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
2e098 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
2e099 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2e09a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2e09b 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72  (db, pParse->zEr
2e09c 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rMsg);.    }.   
2e09d 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
2e09e 20 3d 20 30 3b 0a 20 20 20 20 6e 45 72 72 2b 2b   = 0;.    nErr++
2e09f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72  ;.  }.  if( pPar
2e0a0 73 65 2d 3e 70 56 64 62 65 20 26 26 20 70 50 61  se->pVdbe && pPa
2e0a1 72 73 65 2d 3e 6e 45 72 72 3e 30 20 26 26 20 70  rse->nErr>0 && p
2e0a2 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
2e0a3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2e0a4 64 62 65 44 65 6c 65 74 65 28 70 50 61 72 73 65  dbeDelete(pParse
2e0a5 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 70 50  ->pVdbe);.    pP
2e0a6 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 30 3b  arse->pVdbe = 0;
2e0a7 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
2e0a8 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
2e0a9 43 41 43 48 45 0a 20 20 69 66 28 20 70 50 61 72  CACHE.  if( pPar
2e0aa 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b  se->nested==0 ){
2e0ab 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2e0ac 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61  ee(db, pParse->a
2e0ad 54 61 62 6c 65 4c 6f 63 6b 29 3b 0a 20 20 20 20  TableLock);.    
2e0ae 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f  pParse->aTableLo
2e0af 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72  ck = 0;.    pPar
2e0b0 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20 3d  se->nTableLock =
2e0b1 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23   0;.  }.#endif.#
2e0b2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2e0b3 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2e0b4 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2e0b5 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 74  db, pParse->apVt
2e0b6 61 62 4c 6f 63 6b 29 3b 0a 23 65 6e 64 69 66 0a  abLock);.#endif.
2e0b7 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c 41  .  if( !IN_DECLA
2e0b8 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 2f  RE_VTAB ){.    /
2e0b9 2a 20 49 66 20 74 68 65 20 70 50 61 72 73 65 2d  * If the pParse-
2e0ba 3e 64 65 63 6c 61 72 65 56 74 61 62 20 66 6c 61  >declareVtab fla
2e0bb 67 20 69 73 20 73 65 74 2c 20 64 6f 20 6e 6f 74  g is set, do not
2e0bc 20 64 65 6c 65 74 65 20 61 6e 79 20 74 61 62 6c   delete any tabl
2e0bd 65 20 0a 20 20 20 20 2a 2a 20 73 74 72 75 63 74  e .    ** struct
2e0be 75 72 65 20 62 75 69 6c 74 20 75 70 20 69 6e 20  ure built up in 
2e0bf 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2e0c0 65 2e 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 63  e. The calling c
2e0c1 6f 64 65 20 28 73 65 65 20 76 74 61 62 2e 63 29  ode (see vtab.c)
2e0c2 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 74 61 6b  .    ** will tak
2e0c3 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  e responsibility
2e0c4 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65   for freeing the
2e0c5 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
2e0c6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
2e0c7 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
2e0c8 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2e0c9 65 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  e);.  }..  sqlit
2e0ca 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  e3DeleteTrigger(
2e0cb 64 62 2c 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  db, pParse->pNew
2e0cc 54 72 69 67 67 65 72 29 3b 0a 20 20 73 71 6c 69  Trigger);.  sqli
2e0cd 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50  te3DbFree(db, pP
2e0ce 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 29  arse->apVarExpr)
2e0cf 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2e0d0 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 41  e(db, pParse->aA
2e0d1 6c 69 61 73 29 3b 0a 20 20 77 68 69 6c 65 28 20  lias);.  while( 
2e0d2 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54  pParse->pZombieT
2e0d3 61 62 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20  ab ){.    Table 
2e0d4 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 5a 6f  *p = pParse->pZo
2e0d5 6d 62 69 65 54 61 62 3b 0a 20 20 20 20 70 50 61  mbieTab;.    pPa
2e0d6 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20  rse->pZombieTab 
2e0d7 3d 20 70 2d 3e 70 4e 65 78 74 5a 6f 6d 62 69 65  = p->pNextZombie
2e0d8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  ;.    sqlite3Del
2e0d9 65 74 65 54 61 62 6c 65 28 70 29 3b 0a 20 20 7d  eteTable(p);.  }
2e0da 0a 20 20 69 66 28 20 6e 45 72 72 3e 30 20 26 26  .  if( nErr>0 &&
2e0db 20 28 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51   (pParse->rc==SQ
2e0dc 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 72 73  LITE_OK || pPars
2e0dd 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  e->rc==SQLITE_DO
2e0de 4e 45 29 20 29 7b 0a 20 20 20 20 70 50 61 72 73  NE) ){.    pPars
2e0df 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45  e->rc = SQLITE_E
2e0e0 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75  RROR;.  }.  retu
2e0e1 72 6e 20 6e 45 72 72 3b 0a 7d 0a 0a 2f 2a 2a 2a  rn nErr;.}../***
2e0e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2e0e3 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  of tokenize.c **
2e0e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2e0e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2e0e8 6e 20 66 69 6c 65 20 63 6f 6d 70 6c 65 74 65 2e  n file complete.
2e0e9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2e0ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2e0ec 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
2e0ed 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
2e0ee 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
2e0ef 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
2e0f0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
2e0f1 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
2e0f2 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
2e0f3 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
2e0f4 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
2e0f5 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
2e0f6 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
2e0f7 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
2e0f8 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
2e0f9 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
2e0fa 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
2e0fb 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
2e0fc 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
2e0fd 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
2e0fe 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
2e0ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e101 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e102 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e103 2a 2a 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a  **.** An tokeniz
2e104 65 72 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a  er for SQL.**.**
2e105 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
2e106 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61 74 20  ins C code that 
2e107 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73  implements the s
2e108 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
2e109 29 20 41 50 49 2e 0a 2a 2a 20 54 68 69 73 20 63  ) API..** This c
2e10a 6f 64 65 20 75 73 65 64 20 74 6f 20 62 65 20 70  ode used to be p
2e10b 61 72 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  art of the token
2e10c 69 7a 65 72 2e 63 20 73 6f 75 72 63 65 20 66 69  izer.c source fi
2e10d 6c 65 2e 20 20 42 75 74 20 62 79 0a 2a 2a 20 73  le.  But by.** s
2e10e 65 70 61 72 61 74 69 6e 67 20 69 74 20 6f 75 74  eparating it out
2e10f 2c 20 74 68 65 20 63 6f 64 65 20 77 69 6c 6c 20  , the code will 
2e110 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  be automatically
2e111 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a 2a 2a   omitted from.**
2e112 20 73 74 61 74 69 63 20 6c 69 6e 6b 73 20 74 68   static links th
2e113 61 74 20 64 6f 20 6e 6f 74 20 75 73 65 20 69 74  at do not use it
2e114 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 6f 6d  ..**.** $Id: com
2e115 70 6c 65 74 65 2e 63 2c 76 20 31 2e 37 20 32 30  plete.c,v 1.7 20
2e116 30 38 2f 30 36 2f 31 33 20 31 38 3a 32 34 3a 32  08/06/13 18:24:2
2e117 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23  7 drh Exp $.*/.#
2e118 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2e119 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 0a 2f 2a 0a  IT_COMPLETE../*.
2e11a 2a 2a 20 54 68 69 73 20 69 73 20 64 65 66 69 6e  ** This is defin
2e11b 65 64 20 69 6e 20 74 6f 6b 65 6e 69 7a 65 2e 63  ed in tokenize.c
2e11c 2e 20 20 57 65 20 6a 75 73 74 20 68 61 76 65 20  .  We just have 
2e11d 74 6f 20 69 6d 70 6f 72 74 20 74 68 65 20 64 65  to import the de
2e11e 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66  finition..*/.#if
2e11f 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c  ndef SQLITE_AMAL
2e120 47 41 4d 41 54 49 4f 4e 0a 23 69 66 64 65 66 20  GAMATION.#ifdef 
2e121 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c  SQLITE_ASCII.SQL
2e122 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
2e123 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 49 73  t char sqlite3Is
2e124 41 73 63 69 69 49 64 43 68 61 72 5b 5d 3b 0a 23  AsciiIdChar[];.#
2e125 64 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29  define IdChar(C)
2e126 20 20 28 28 28 63 3d 43 29 26 30 78 38 30 29 21    (((c=C)&0x80)!
2e127 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66 20 26 26  =0 || (c>0x1f &&
2e128 20 73 71 6c 69 74 65 33 49 73 41 73 63 69 69 49   sqlite3IsAsciiI
2e129 64 43 68 61 72 5b 63 2d 30 78 32 30 5d 29 29 0a  dChar[c-0x20])).
2e12a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
2e12b 4c 49 54 45 5f 45 42 43 44 49 43 0a 53 51 4c 49  LITE_EBCDIC.SQLI
2e12c 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
2e12d 20 63 68 61 72 20 73 71 6c 69 74 65 33 49 73 45   char sqlite3IsE
2e12e 62 63 64 69 63 49 64 43 68 61 72 5b 5d 3b 0a 23  bcdicIdChar[];.#
2e12f 64 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29  define IdChar(C)
2e130 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34 32 20    (((c=C)>=0x42 
2e131 26 26 20 73 71 6c 69 74 65 33 49 73 45 62 63 64  && sqlite3IsEbcd
2e132 69 63 49 64 43 68 61 72 5b 63 2d 30 78 34 30 5d  icIdChar[c-0x40]
2e133 29 29 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  )).#endif.#endif
2e134 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47   /* SQLITE_AMALG
2e135 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 2f 2a 0a  AMATION */.../*.
2e136 2a 2a 20 54 6f 6b 65 6e 20 74 79 70 65 73 20 75  ** Token types u
2e137 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74  sed by the sqlit
2e138 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 72 6f  e3_complete() ro
2e139 75 74 69 6e 65 2e 20 20 53 65 65 20 74 68 65 20  utine.  See the 
2e13a 68 65 61 64 65 72 0a 2a 2a 20 63 6f 6d 6d 65 6e  header.** commen
2e13b 74 73 20 6f 6e 20 74 68 61 74 20 70 72 6f 63 65  ts on that proce
2e13c 64 75 72 65 20 66 6f 72 20 61 64 64 69 74 69 6f  dure for additio
2e13d 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
2e13e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 74 6b 53 45  .*/.#define tkSE
2e13f 4d 49 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20  MI    0.#define 
2e140 74 6b 57 53 20 20 20 20 20 20 31 0a 23 64 65 66  tkWS      1.#def
2e141 69 6e 65 20 74 6b 4f 54 48 45 52 20 20 20 32 0a  ine tkOTHER   2.
2e142 23 64 65 66 69 6e 65 20 74 6b 45 58 50 4c 41 49  #define tkEXPLAI
2e143 4e 20 33 0a 23 64 65 66 69 6e 65 20 74 6b 43 52  N 3.#define tkCR
2e144 45 41 54 45 20 20 34 0a 23 64 65 66 69 6e 65 20  EATE  4.#define 
2e145 74 6b 54 45 4d 50 20 20 20 20 35 0a 23 64 65 66  tkTEMP    5.#def
2e146 69 6e 65 20 74 6b 54 52 49 47 47 45 52 20 36 0a  ine tkTRIGGER 6.
2e147 23 64 65 66 69 6e 65 20 74 6b 45 4e 44 20 20 20  #define tkEND   
2e148 20 20 37 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72    7../*.** Retur
2e149 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69  n TRUE if the gi
2e14a 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67 20 65  ven SQL string e
2e14b 6e 64 73 20 69 6e 20 61 20 73 65 6d 69 63 6f 6c  nds in a semicol
2e14c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61  on..**.** Specia
2e14d 6c 20 68 61 6e 64 6c 69 6e 67 20 69 73 20 72 65  l handling is re
2e14e 71 75 69 72 65 20 66 6f 72 20 43 52 45 41 54 45  quire for CREATE
2e14f 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
2e150 6e 74 73 2e 0a 2a 2a 20 57 68 65 6e 65 76 65 72  nts..** Whenever
2e151 20 74 68 65 20 43 52 45 41 54 45 20 54 52 49 47   the CREATE TRIG
2e152 47 45 52 20 6b 65 79 77 6f 72 64 73 20 61 72 65  GER keywords are
2e153 20 73 65 65 6e 2c 20 74 68 65 20 73 74 61 74 65   seen, the state
2e154 6d 65 6e 74 0a 2a 2a 20 6d 75 73 74 20 65 6e 64  ment.** must end
2e155 20 77 69 74 68 20 22 3b 45 4e 44 3b 22 2e 0a 2a   with ";END;"..*
2e156 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d  *.** This implem
2e157 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20  entation uses a 
2e158 73 74 61 74 65 20 6d 61 63 68 69 6e 65 20 77 69  state machine wi
2e159 74 68 20 37 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  th 7 states:.**.
2e15a 2a 2a 20 20 20 28 30 29 20 53 54 41 52 54 20 20  **   (0) START  
2e15b 20 20 20 41 74 20 74 68 65 20 62 65 67 69 6e 6e     At the beginn
2e15c 69 6e 67 20 6f 72 20 65 6e 64 20 6f 66 20 61 6e  ing or end of an
2e15d 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
2e15e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   This routine.**
2e15f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e160 20 72 65 74 75 72 6e 73 20 31 20 69 66 20 69 74   returns 1 if it
2e161 20 65 6e 64 73 20 69 6e 20 74 68 65 20 53 54 41   ends in the STA
2e162 52 54 20 73 74 61 74 65 20 61 6e 64 20 30 20 69  RT state and 0 i
2e163 66 20 69 74 20 65 6e 64 73 0a 2a 2a 20 20 20 20  f it ends.**    
2e164 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 20               in 
2e165 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 2e  any other state.
2e166 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 4e 4f 52  .**.**   (1) NOR
2e167 4d 41 4c 20 20 20 20 57 65 20 61 72 65 20 69 6e  MAL    We are in
2e168 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
2e169 74 61 74 65 6d 65 6e 74 20 77 68 69 63 68 20 65  tatement which e
2e16a 6e 64 73 20 77 69 74 68 20 61 20 73 69 6e 67 6c  nds with a singl
2e16b 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
2e16c 20 20 20 20 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a       semicolon..
2e16d 2a 2a 0a 2a 2a 20 20 20 28 32 29 20 45 58 50 4c  **.**   (2) EXPL
2e16e 41 49 4e 20 20 20 54 68 65 20 6b 65 79 77 6f 72  AIN   The keywor
2e16f 64 20 45 58 50 4c 41 49 4e 20 68 61 73 20 62 65  d EXPLAIN has be
2e170 65 6e 20 73 65 65 6e 20 61 74 20 74 68 65 20 62  en seen at the b
2e171 65 67 69 6e 6e 69 6e 67 20 6f 66 20 0a 2a 2a 20  eginning of .** 
2e172 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e173 61 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  a statement..**.
2e174 2a 2a 20 20 20 28 33 29 20 43 52 45 41 54 45 20  **   (3) CREATE 
2e175 20 20 20 54 68 65 20 6b 65 79 77 6f 72 64 20 43     The keyword C
2e176 52 45 41 54 45 20 68 61 73 20 62 65 65 6e 20 73  REATE has been s
2e177 65 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e  een at the begin
2e178 6e 69 6e 67 20 6f 66 20 61 0a 2a 2a 20 20 20 20  ning of a.**    
2e179 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
2e17a 74 65 6d 65 6e 74 2c 20 70 6f 73 73 69 62 6c 79  tement, possibly
2e17b 20 70 72 65 63 65 65 64 65 64 20 62 79 20 45 58   preceeded by EX
2e17c 50 4c 41 49 4e 20 61 6e 64 2f 6f 72 20 66 6f 6c  PLAIN and/or fol
2e17d 6c 6f 77 65 64 20 62 79 0a 2a 2a 20 20 20 20 20  lowed by.**     
2e17e 20 20 20 20 20 20 20 20 20 20 20 20 54 45 4d 50              TEMP
2e17f 20 6f 72 20 54 45 4d 50 4f 52 41 52 59 0a 2a 2a   or TEMPORARY.**
2e180 0a 2a 2a 20 20 20 28 34 29 20 54 52 49 47 47 45  .**   (4) TRIGGE
2e181 52 20 20 20 57 65 20 61 72 65 20 69 6e 20 74 68  R   We are in th
2e182 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 74 72  e middle of a tr
2e183 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e  igger definition
2e184 20 74 68 61 74 20 6d 75 73 74 20 62 65 0a 2a 2a   that must be.**
2e185 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e186 20 65 6e 64 65 64 20 62 79 20 61 20 73 65 6d 69   ended by a semi
2e187 63 6f 6c 6f 6e 2c 20 74 68 65 20 6b 65 79 77 6f  colon, the keywo
2e188 72 64 20 45 4e 44 2c 20 61 6e 64 20 61 6e 6f 74  rd END, and anot
2e189 68 65 72 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a  her semicolon..*
2e18a 2a 0a 2a 2a 20 20 20 28 35 29 20 53 45 4d 49 20  *.**   (5) SEMI 
2e18b 20 20 20 20 20 57 65 27 76 65 20 73 65 65 6e 20       We've seen 
2e18c 74 68 65 20 66 69 72 73 74 20 73 65 6d 69 63 6f  the first semico
2e18d 6c 6f 6e 20 69 6e 20 74 68 65 20 22 3b 45 4e 44  lon in the ";END
2e18e 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73 20 61  ;" that occurs a
2e18f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
2e190 20 20 20 20 20 74 68 65 20 65 6e 64 20 6f 66 20       the end of 
2e191 61 20 74 72 69 67 67 65 72 20 64 65 66 69 6e 69  a trigger defini
2e192 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36  tion..**.**   (6
2e193 29 20 45 4e 44 20 20 20 20 20 20 20 57 65 27 76  ) END       We'v
2e194 65 20 73 65 65 6e 20 74 68 65 20 22 3b 45 4e 44  e seen the ";END
2e195 22 20 6f 66 20 74 68 65 20 22 3b 45 4e 44 3b 22  " of the ";END;"
2e196 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 20   that occurs at 
2e197 74 68 65 20 65 6e 64 0a 2a 2a 20 20 20 20 20 20  the end.**      
2e198 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 61 20             of a 
2e199 74 72 69 67 67 65 72 20 64 69 66 69 6e 69 74 69  trigger difiniti
2e19a 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 69  on..**.** Transi
2e19b 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 20 73 74  tions between st
2e19c 61 74 65 73 20 61 62 6f 76 65 20 61 72 65 20 64  ates above are d
2e19d 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 6f 6b  etermined by tok
2e19e 65 6e 73 20 65 78 74 72 61 63 74 65 64 0a 2a 2a  ens extracted.**
2e19f 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 2e   from the input.
2e1a0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
2e1a1 74 6f 6b 65 6e 73 20 61 72 65 20 73 69 67 6e 69  tokens are signi
2e1a2 66 69 63 61 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  ficant:.**.**   
2e1a3 28 30 29 20 74 6b 53 45 4d 49 20 20 20 20 20 20  (0) tkSEMI      
2e1a4 41 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 20  A semicolon..** 
2e1a5 20 20 28 31 29 20 74 6b 57 53 20 20 20 20 20 20    (1) tkWS      
2e1a6 20 20 57 68 69 74 65 73 70 61 63 65 0a 2a 2a 20    Whitespace.** 
2e1a7 20 20 28 32 29 20 74 6b 4f 54 48 45 52 20 20 20    (2) tkOTHER   
2e1a8 20 20 41 6e 79 20 6f 74 68 65 72 20 53 51 4c 20    Any other SQL 
2e1a9 74 6f 6b 65 6e 2e 0a 2a 2a 20 20 20 28 33 29 20  token..**   (3) 
2e1aa 74 6b 45 58 50 4c 41 49 4e 20 20 20 54 68 65 20  tkEXPLAIN   The 
2e1ab 22 65 78 70 6c 61 69 6e 22 20 6b 65 79 77 6f 72  "explain" keywor
2e1ac 64 2e 0a 2a 2a 20 20 20 28 34 29 20 74 6b 43 52  d..**   (4) tkCR
2e1ad 45 41 54 45 20 20 20 20 54 68 65 20 22 63 72 65  EATE    The "cre
2e1ae 61 74 65 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a  ate" keyword..**
2e1af 20 20 20 28 35 29 20 74 6b 54 45 4d 50 20 20 20     (5) tkTEMP   
2e1b0 20 20 20 54 68 65 20 22 74 65 6d 70 22 20 6f 72     The "temp" or
2e1b1 20 22 74 65 6d 70 6f 72 61 72 79 22 20 6b 65 79   "temporary" key
2e1b2 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 36 29 20 74  word..**   (6) t
2e1b3 6b 54 52 49 47 47 45 52 20 20 20 54 68 65 20 22  kTRIGGER   The "
2e1b4 74 72 69 67 67 65 72 22 20 6b 65 79 77 6f 72 64  trigger" keyword
2e1b5 2e 0a 2a 2a 20 20 20 28 37 29 20 74 6b 45 4e 44  ..**   (7) tkEND
2e1b6 20 20 20 20 20 20 20 54 68 65 20 22 65 6e 64 22         The "end"
2e1b7 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20   keyword..**.** 
2e1b8 57 68 69 74 65 73 70 61 63 65 20 6e 65 76 65 72  Whitespace never
2e1b9 20 63 61 75 73 65 73 20 61 20 73 74 61 74 65 20   causes a state 
2e1ba 74 72 61 6e 73 69 74 69 6f 6e 20 61 6e 64 20 69  transition and i
2e1bb 73 20 61 6c 77 61 79 73 20 69 67 6e 6f 72 65 64  s always ignored
2e1bc 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f  ..**.** If we co
2e1bd 6d 70 69 6c 65 20 77 69 74 68 20 53 51 4c 49 54  mpile with SQLIT
2e1be 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 2c 20  E_OMIT_TRIGGER, 
2e1bf 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 6d 70 75  all of the compu
2e1c0 74 61 74 69 6f 6e 20 6e 65 65 64 65 64 0a 2a 2a  tation needed.**
2e1c1 20 74 6f 20 72 65 63 6f 67 6e 69 7a 65 20 74 68   to recognize th
2e1c2 65 20 65 6e 64 20 6f 66 20 61 20 74 72 69 67 67  e end of a trigg
2e1c3 65 72 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65  er can be omitte
2e1c4 64 2e 20 20 41 6c 6c 20 77 65 20 68 61 76 65 20  d.  All we have 
2e1c5 74 6f 20 64 6f 0a 2a 2a 20 69 73 20 6c 6f 6f 6b  to do.** is look
2e1c6 20 66 6f 72 20 61 20 73 65 6d 69 63 6f 6c 6f 6e   for a semicolon
2e1c7 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72   that is not par
2e1c8 74 20 6f 66 20 61 6e 20 73 74 72 69 6e 67 20 6f  t of an string o
2e1c9 72 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a 53 51  r comment..*/.SQ
2e1ca 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2e1cb 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f  ite3_complete(co
2e1cc 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b  nst char *zSql){
2e1cd 0a 20 20 75 38 20 73 74 61 74 65 20 3d 20 30 3b  .  u8 state = 0;
2e1ce 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 74     /* Current st
2e1cf 61 74 65 2c 20 75 73 69 6e 67 20 6e 75 6d 62 65  ate, using numbe
2e1d0 72 73 20 64 65 66 69 6e 65 64 20 69 6e 20 68 65  rs defined in he
2e1d1 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 2a 2f 0a  ader comment */.
2e1d2 20 20 75 38 20 74 6f 6b 65 6e 3b 20 20 20 20 20    u8 token;     
2e1d3 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 74 68    /* Value of th
2e1d4 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a  e next token */.
2e1d5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2e1d6 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f  OMIT_TRIGGER.  /
2e1d7 2a 20 41 20 63 6f 6d 70 6c 65 78 20 73 74 61 74  * A complex stat
2e1d8 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 20 75 73  ement machine us
2e1d9 65 64 20 74 6f 20 64 65 74 65 63 74 20 74 68 65  ed to detect the
2e1da 20 65 6e 64 20 6f 66 20 61 20 43 52 45 41 54 45   end of a CREATE
2e1db 20 54 52 49 47 47 45 52 0a 20 20 2a 2a 20 73 74   TRIGGER.  ** st
2e1dc 61 74 65 6d 65 6e 74 2e 20 20 54 68 69 73 20 69  atement.  This i
2e1dd 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 63 61 73  s the normal cas
2e1de 65 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63  e..  */.  static
2e1df 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e 73 5b   const u8 trans[
2e1e0 37 5d 5b 38 5d 20 3d 20 7b 0a 20 20 20 20 20 20  7][8] = {.      
2e1e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e1e2 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20 20 20  * Token:        
2e1e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e1e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e1e5 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2e1e6 2f 2a 20 53 74 61 74 65 3a 20 20 20 20 20 20 20  /* State:       
2e1e7 2a 2a 20 20 53 45 4d 49 20 20 57 53 20 20 4f 54  **  SEMI  WS  OT
2e1e8 48 45 52 20 45 58 50 4c 41 49 4e 20 20 43 52 45  HER EXPLAIN  CRE
2e1e9 41 54 45 20 20 54 45 4d 50 20 20 54 52 49 47 47  ATE  TEMP  TRIGG
2e1ea 45 52 20 20 45 4e 44 20 20 2a 2f 0a 20 20 20 20  ER  END  */.    
2e1eb 20 2f 2a 20 30 20 20 20 53 54 41 52 54 3a 20 2a   /* 0   START: *
2e1ec 2f 20 7b 20 20 20 20 30 2c 20 20 30 2c 20 20 20  / {    0,  0,   
2e1ed 20 20 31 2c 20 20 20 20 20 20 32 2c 20 20 20 20    1,      2,    
2e1ee 20 20 33 2c 20 20 20 20 31 2c 20 20 20 20 20 20    3,    1,      
2e1ef 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20   1,   1,  },.   
2e1f0 20 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c 3a 20    /* 1  NORMAL: 
2e1f1 2a 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c 20 20  */ {    0,  1,  
2e1f2 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20     1,      1,   
2e1f3 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 20     1,    1,     
2e1f4 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20    1,   1,  },.  
2e1f5 20 20 20 2f 2a 20 32 20 45 58 50 4c 41 49 4e 3a     /* 2 EXPLAIN:
2e1f6 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 32 2c 20   */ {    0,  2, 
2e1f7 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20      1,      1,  
2e1f8 20 20 20 20 33 2c 20 20 20 20 31 2c 20 20 20 20      3,    1,    
2e1f9 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20     1,   1,  },. 
2e1fa 20 20 20 20 2f 2a 20 33 20 20 43 52 45 41 54 45      /* 3  CREATE
2e1fb 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 33 2c  : */ {    0,  3,
2e1fc 20 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20       1,      1, 
2e1fd 20 20 20 20 20 31 2c 20 20 20 20 33 2c 20 20 20       1,    3,   
2e1fe 20 20 20 20 34 2c 20 20 20 31 2c 20 20 7d 2c 0a      4,   1,  },.
2e1ff 20 20 20 20 20 2f 2a 20 34 20 54 52 49 47 47 45       /* 4 TRIGGE
2e200 52 3a 20 2a 2f 20 7b 20 20 20 20 35 2c 20 20 34  R: */ {    5,  4
2e201 2c 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c  ,     4,      4,
2e202 20 20 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20        4,    4,  
2e203 20 20 20 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c       4,   4,  },
2e204 0a 20 20 20 20 20 2f 2a 20 35 20 20 20 20 53 45  .     /* 5    SE
2e205 4d 49 3a 20 2a 2f 20 7b 20 20 20 20 35 2c 20 20  MI: */ {    5,  
2e206 35 2c 20 20 20 20 20 34 2c 20 20 20 20 20 20 34  5,     4,      4
2e207 2c 20 20 20 20 20 20 34 2c 20 20 20 20 34 2c 20  ,      4,    4, 
2e208 20 20 20 20 20 20 34 2c 20 20 20 36 2c 20 20 7d        4,   6,  }
2e209 2c 0a 20 20 20 20 20 2f 2a 20 36 20 20 20 20 20  ,.     /* 6     
2e20a 45 4e 44 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20  END: */ {    0, 
2e20b 20 36 2c 20 20 20 20 20 34 2c 20 20 20 20 20 20   6,     4,      
2e20c 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 34 2c  4,      4,    4,
2e20d 20 20 20 20 20 20 20 34 2c 20 20 20 34 2c 20 20         4,   4,  
2e20e 7d 2c 0a 20 20 7d 3b 0a 23 65 6c 73 65 0a 20 20  },.  };.#else.  
2e20f 2f 2a 20 49 66 20 74 72 69 67 67 65 72 73 20 61  /* If triggers a
2e210 72 65 20 6e 6f 74 20 73 75 70 70 6f 72 65 64 20  re not suppored 
2e211 62 79 20 74 68 69 73 20 63 6f 6d 70 69 6c 65 20  by this compile 
2e212 74 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65  then the stateme
2e213 6e 74 20 6d 61 63 68 69 6e 65 0a 20 20 2a 2a 20  nt machine.  ** 
2e214 75 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 74  used to detect t
2e215 68 65 20 65 6e 64 20 6f 66 20 61 20 73 74 61 74  he end of a stat
2e216 65 6d 65 6e 74 20 69 73 20 6d 75 63 68 20 73 69  ement is much si
2e217 6d 70 6c 69 65 72 0a 20 20 2a 2f 0a 20 20 73 74  mplier.  */.  st
2e218 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72  atic const u8 tr
2e219 61 6e 73 5b 32 5d 5b 33 5d 20 3d 20 7b 0a 20 20  ans[2][3] = {.  
2e21a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e21b 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20     /* Token:    
2e21c 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f         */.     /
2e21d 2a 20 53 74 61 74 65 3a 20 20 20 20 20 20 20 2a  * State:       *
2e21e 2a 20 20 53 45 4d 49 20 20 57 53 20 20 4f 54 48  *  SEMI  WS  OTH
2e21f 45 52 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 30 20  ER */.     /* 0 
2e220 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20    START: */ {   
2e221 20 30 2c 20 20 30 2c 20 20 20 20 20 31 2c 20 7d   0,  0,     1, }
2e222 2c 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52  ,.     /* 1  NOR
2e223 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20  MAL: */ {    0, 
2e224 20 31 2c 20 20 20 20 20 31 2c 20 7d 2c 0a 20 20   1,     1, },.  
2e225 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  };.#endif /* SQL
2e226 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2e227 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 2a 7a   */..  while( *z
2e228 53 71 6c 20 29 7b 0a 20 20 20 20 73 77 69 74 63  Sql ){.    switc
2e229 68 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20  h( *zSql ){.    
2e22a 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 20 20 2f    case ';': {  /
2e22b 2a 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 20 2a 2f  * A semicolon */
2e22c 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d  .        token =
2e22d 20 74 6b 53 45 4d 49 3b 0a 20 20 20 20 20 20 20   tkSEMI;.       
2e22e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2e22f 20 20 20 20 20 20 63 61 73 65 20 27 20 27 3a 0a        case ' ':.
2e230 20 20 20 20 20 20 63 61 73 65 20 27 5c 72 27 3a        case '\r':
2e231 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 74 27  .      case '\t'
2e232 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 6e  :.      case '\n
2e233 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c  ':.      case '\
2e234 66 27 3a 20 7b 20 20 2f 2a 20 57 68 69 74 65 20  f': {  /* White 
2e235 73 70 61 63 65 20 69 73 20 69 67 6e 6f 72 65 64  space is ignored
2e236 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f 6b 65   */.        toke
2e237 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20 20  n = tkWS;.      
2e238 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2e239 0a 20 20 20 20 20 20 63 61 73 65 20 27 2f 27 3a  .      case '/':
2e23a 20 7b 20 20 20 2f 2a 20 43 2d 73 74 79 6c 65 20   {   /* C-style 
2e23b 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  comments */.    
2e23c 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21      if( zSql[1]!
2e23d 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
2e23e 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
2e23f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  R;.          bre
2e240 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
2e241 20 20 20 20 20 20 7a 53 71 6c 20 2b 3d 20 32 3b        zSql += 2;
2e242 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
2e243 7a 53 71 6c 5b 30 5d 20 26 26 20 28 7a 53 71 6c  zSql[0] && (zSql
2e244 5b 30 5d 21 3d 27 2a 27 20 7c 7c 20 7a 53 71 6c  [0]!='*' || zSql
2e245 5b 31 5d 21 3d 27 2f 27 29 20 29 7b 20 7a 53 71  [1]!='/') ){ zSq
2e246 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69  l++; }.        i
2e247 66 28 20 7a 53 71 6c 5b 30 5d 3d 3d 30 20 29 20  f( zSql[0]==0 ) 
2e248 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2e249 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20    zSql++;.      
2e24a 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a    token = tkWS;.
2e24b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e24c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
2e24d 65 20 27 2d 27 3a 20 7b 20 20 20 2f 2a 20 53 51  e '-': {   /* SQ
2e24e 4c 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73  L-style comments
2e24f 20 66 72 6f 6d 20 22 2d 2d 22 20 74 6f 20 65 6e   from "--" to en
2e250 64 20 6f 66 20 6c 69 6e 65 20 2a 2f 0a 20 20 20  d of line */.   
2e251 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d       if( zSql[1]
2e252 21 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20  !='-' ){.       
2e253 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48     token = tkOTH
2e254 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  ER;.          br
2e255 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
2e256 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a         while( *z
2e257 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 27 5c  Sql && *zSql!='\
2e258 6e 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a  n' ){ zSql++; }.
2e259 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71          if( *zSq
2e25a 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 73 74  l==0 ) return st
2e25b 61 74 65 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20  ate==0;.        
2e25c 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20  token = tkWS;.  
2e25d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e25e 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
2e25f 27 5b 27 3a 20 7b 20 20 20 2f 2a 20 4d 69 63 72  '[': {   /* Micr
2e260 6f 73 6f 66 74 2d 73 74 79 6c 65 20 69 64 65 6e  osoft-style iden
2e261 74 69 66 69 65 72 73 20 69 6e 20 5b 2e 2e 2e 5d  tifiers in [...]
2e262 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 53 71 6c   */.        zSql
2e263 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ++;.        whil
2e264 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71  e( *zSql && *zSq
2e265 6c 21 3d 27 5d 27 20 29 7b 20 7a 53 71 6c 2b 2b  l!=']' ){ zSql++
2e266 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
2e267 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72  *zSql==0 ) retur
2e268 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b  n 0;.        tok
2e269 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
2e26a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e26b 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
2e26c 27 60 27 3a 20 20 20 20 20 2f 2a 20 47 72 61 76  '`':     /* Grav
2e26d 65 2d 61 63 63 65 6e 74 20 71 75 6f 74 65 64 20  e-accent quoted 
2e26e 73 79 6d 62 6f 6c 73 20 75 73 65 64 20 62 79 20  symbols used by 
2e26f 4d 79 53 51 4c 20 2a 2f 0a 20 20 20 20 20 20 63  MySQL */.      c
2e270 61 73 65 20 27 22 27 3a 20 20 20 20 20 2f 2a 20  ase '"':     /* 
2e271 73 69 6e 67 6c 65 2d 20 61 6e 64 20 64 6f 75 62  single- and doub
2e272 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le-quoted string
2e273 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  s */.      case 
2e274 27 5c 27 27 3a 20 7b 0a 20 20 20 20 20 20 20 20  '\'': {.        
2e275 69 6e 74 20 63 20 3d 20 2a 7a 53 71 6c 3b 0a 20  int c = *zSql;. 
2e276 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20         zSql++;. 
2e277 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a         while( *z
2e278 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 63 20  Sql && *zSql!=c 
2e279 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20  ){ zSql++; }.   
2e27a 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d       if( *zSql==
2e27b 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
2e27c 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
2e27d 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 62  OTHER;.        b
2e27e 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2e27f 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
2e280 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20         int c;.  
2e281 20 20 20 20 20 20 69 66 28 20 49 64 43 68 61 72        if( IdChar
2e282 28 28 75 38 29 2a 7a 53 71 6c 29 20 29 7b 0a 20  ((u8)*zSql) ){. 
2e283 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 77           /* Keyw
2e284 6f 72 64 73 20 61 6e 64 20 75 6e 71 75 6f 74 65  ords and unquote
2e285 64 20 69 64 65 6e 74 69 66 69 65 72 73 20 2a 2f  d identifiers */
2e286 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e  .          int n
2e287 49 64 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  Id;.          fo
2e288 72 28 6e 49 64 3d 31 3b 20 49 64 43 68 61 72 28  r(nId=1; IdChar(
2e289 7a 53 71 6c 5b 6e 49 64 5d 29 3b 20 6e 49 64 2b  zSql[nId]); nId+
2e28a 2b 29 7b 7d 0a 23 69 66 64 65 66 20 53 51 4c 49  +){}.#ifdef SQLI
2e28b 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
2e28c 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2e28d 3d 20 74 6b 4f 54 48 45 52 3b 0a 23 65 6c 73 65  = tkOTHER;.#else
2e28e 0a 20 20 20 20 20 20 20 20 20 20 73 77 69 74 63  .          switc
2e28f 68 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20  h( *zSql ){.    
2e290 20 20 20 20 20 20 20 20 63 61 73 65 20 27 63 27          case 'c'
2e291 3a 20 63 61 73 65 20 27 43 27 3a 20 7b 0a 20 20  : case 'C': {.  
2e292 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
2e293 6e 49 64 3d 3d 36 20 26 26 20 73 71 6c 69 74 65  nId==6 && sqlite
2e294 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20  3StrNICmp(zSql, 
2e295 22 63 72 65 61 74 65 22 2c 20 36 29 3d 3d 30 20  "create", 6)==0 
2e296 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2e297 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 43 52 45     token = tkCRE
2e298 41 54 45 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ATE;.           
2e299 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2e29a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2e29b 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
2e29c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2e29d 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e29e 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2e29f 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 27            case '
2e2a0 74 27 3a 20 63 61 73 65 20 27 54 27 3a 20 7b 0a  t': case 'T': {.
2e2a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
2e2a2 28 20 6e 49 64 3d 3d 37 20 26 26 20 73 71 6c 69  ( nId==7 && sqli
2e2a3 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c  te3StrNICmp(zSql
2e2a4 2c 20 22 74 72 69 67 67 65 72 22 2c 20 37 29 3d  , "trigger", 7)=
2e2a5 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2e2a6 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
2e2a7 54 52 49 47 47 45 52 3b 0a 20 20 20 20 20 20 20  TRIGGER;.       
2e2a8 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
2e2a9 20 6e 49 64 3d 3d 34 20 26 26 20 73 71 6c 69 74   nId==4 && sqlit
2e2aa 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c  e3StrNICmp(zSql,
2e2ab 20 22 74 65 6d 70 22 2c 20 34 29 3d 3d 30 20 29   "temp", 4)==0 )
2e2ac 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2e2ad 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50    token = tkTEMP
2e2ae 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2e2af 7d 65 6c 73 65 20 69 66 28 20 6e 49 64 3d 3d 39  }else if( nId==9
2e2b0 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
2e2b1 43 6d 70 28 7a 53 71 6c 2c 20 22 74 65 6d 70 6f  Cmp(zSql, "tempo
2e2b2 72 61 72 79 22 2c 20 39 29 3d 3d 30 20 29 7b 0a  rary", 9)==0 ){.
2e2b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e2b4 74 6f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a  token = tkTEMP;.
2e2b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65                }e
2e2b6 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
2e2b7 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
2e2b8 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  THER;.          
2e2b9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2e2ba 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e2bb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2e2bc 20 20 20 20 20 63 61 73 65 20 27 65 27 3a 20 20       case 'e':  
2e2bd 63 61 73 65 20 27 45 27 3a 20 7b 0a 20 20 20 20  case 'E': {.    
2e2be 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 49            if( nI
2e2bf 64 3d 3d 33 20 26 26 20 73 71 6c 69 74 65 33 53  d==3 && sqlite3S
2e2c0 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 65  trNICmp(zSql, "e
2e2c1 6e 64 22 2c 20 33 29 3d 3d 30 20 29 7b 0a 20 20  nd", 3)==0 ){.  
2e2c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
2e2c3 6b 65 6e 20 3d 20 74 6b 45 4e 44 3b 0a 20 20 20  ken = tkEND;.   
2e2c4 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
2e2c5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2e2c6 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 20  OMIT_EXPLAIN.   
2e2c7 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e             if( n
2e2c8 49 64 3d 3d 37 20 26 26 20 73 71 6c 69 74 65 33  Id==7 && sqlite3
2e2c9 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22  StrNICmp(zSql, "
2e2ca 65 78 70 6c 61 69 6e 22 2c 20 37 29 3d 3d 30 20  explain", 7)==0 
2e2cb 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2e2cc 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45 58 50     token = tkEXP
2e2cd 4c 41 49 4e 3b 0a 20 20 20 20 20 20 20 20 20 20  LAIN;.          
2e2ce 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
2e2cf 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  .              {
2e2d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e2d1 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
2e2d2 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2e2d3 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
2e2d4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
2e2d5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
2e2d6 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
2e2d7 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2e2d8 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
2e2d9 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e2da 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2e2db 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
2e2dc 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2e2dd 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 20 20 20  TRIGGER */.     
2e2de 20 20 20 20 20 7a 53 71 6c 20 2b 3d 20 6e 49 64       zSql += nId
2e2df 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  -1;.        }els
2e2e0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e{.          /* 
2e2e1 4f 70 65 72 61 74 6f 72 73 20 61 6e 64 20 73 70  Operators and sp
2e2e2 65 63 69 61 6c 20 73 79 6d 62 6f 6c 73 20 2a 2f  ecial symbols */
2e2e3 0a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  .          token
2e2e4 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20   = tkOTHER;.    
2e2e5 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72      }.        br
2e2e6 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2e2e7 20 7d 0a 20 20 20 20 73 74 61 74 65 20 3d 20 74   }.    state = t
2e2e8 72 61 6e 73 5b 73 74 61 74 65 5d 5b 74 6f 6b 65  rans[state][toke
2e2e9 6e 5d 3b 0a 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a  n];.    zSql++;.
2e2ea 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 74 61    }.  return sta
2e2eb 74 65 3d 3d 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65  te==0;.}..#ifnde
2e2ec 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
2e2ed 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  F16./*.** This r
2e2ee 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 73 61  outine is the sa
2e2ef 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74 65  me as the sqlite
2e2f0 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75  3_complete() rou
2e2f1 74 69 6e 65 20 64 65 73 63 72 69 62 65 64 0a 2a  tine described.*
2e2f2 2a 20 61 62 6f 76 65 2c 20 65 78 63 65 70 74 20  * above, except 
2e2f3 74 68 61 74 20 74 68 65 20 70 61 72 61 6d 65 74  that the paramet
2e2f4 65 72 20 69 73 20 72 65 71 75 69 72 65 64 20 74  er is required t
2e2f5 6f 20 62 65 20 55 54 46 2d 31 36 20 65 6e 63 6f  o be UTF-16 enco
2e2f6 64 65 64 2c 20 6e 6f 74 0a 2a 2a 20 55 54 46 2d  ded, not.** UTF-
2e2f7 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  8..*/.SQLITE_API
2e2f8 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d   int sqlite3_com
2e2f9 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20 76 6f  plete16(const vo
2e2fa 69 64 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c  id *zSql){.  sql
2e2fb 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
2e2fc 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
2e2fd 7a 53 71 6c 38 3b 0a 20 20 69 6e 74 20 72 63 20  zSql8;.  int rc 
2e2fe 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
2e2ff 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2e300 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
2e301 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69  rc = sqlite3_ini
2e302 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28  tialize();.  if(
2e303 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
2e304 0a 23 65 6e 64 69 66 0a 20 20 70 56 61 6c 20 3d  .#endif.  pVal =
2e305 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
2e306 28 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61  (0);.  sqlite3Va
2e307 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20  lueSetStr(pVal, 
2e308 2d 31 2c 20 7a 53 71 6c 2c 20 53 51 4c 49 54 45  -1, zSql, SQLITE
2e309 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51  _UTF16NATIVE, SQ
2e30a 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
2e30b 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65 33 56  zSql8 = sqlite3V
2e30c 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53  alueText(pVal, S
2e30d 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69  QLITE_UTF8);.  i
2e30e 66 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20 20 20  f( zSql8 ){.    
2e30f 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6d  rc = sqlite3_com
2e310 70 6c 65 74 65 28 7a 53 71 6c 38 29 3b 0a 20 20  plete(zSql8);.  
2e311 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
2e312 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2e313 7d 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65  }.  sqlite3Value
2e314 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 72 65  Free(pVal);.  re
2e315 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45  turn sqlite3ApiE
2e316 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 23 65  xit(0, rc);.}.#e
2e317 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2e318 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e  MIT_UTF16 */.#en
2e319 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2e31a 49 54 5f 43 4f 4d 50 4c 45 54 45 20 2a 2f 0a 0a  IT_COMPLETE */..
2e31b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2e31c 45 6e 64 20 6f 66 20 63 6f 6d 70 6c 65 74 65 2e  End of complete.
2e31d 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2e31e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e31f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2e320 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2e321 42 65 67 69 6e 20 66 69 6c 65 20 6d 61 69 6e 2e  Begin file main.
2e322 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2e323 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e324 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2e325 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
2e326 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
2e327 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2e328 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2e329 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2e32a 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2e32b 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2e32c 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2e32d 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2e32e 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2e32f 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2e330 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2e331 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2e332 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2e333 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2e334 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2e335 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2e336 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2e337 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2e338 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e339 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e33a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e33b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e33c 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e 20 66  ******.** Main f
2e33d 69 6c 65 20 66 6f 72 20 74 68 65 20 53 51 4c 69  ile for the SQLi
2e33e 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65  te library.  The
2e33f 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69   routines in thi
2e340 73 20 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d  s file.** implem
2e341 65 6e 74 20 74 68 65 20 70 72 6f 67 72 61 6d 6d  ent the programm
2e342 65 72 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  er interface to 
2e343 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 52 6f  the library.  Ro
2e344 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f 74 68  utines in.** oth
2e345 65 72 20 66 69 6c 65 73 20 61 72 65 20 66 6f 72  er files are for
2e346 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 62 79   internal use by
2e347 20 53 51 4c 69 74 65 20 61 6e 64 20 73 68 6f 75   SQLite and shou
2e348 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63  ld not be.** acc
2e349 65 73 73 65 64 20 62 79 20 75 73 65 72 73 20 6f  essed by users o
2e34a 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a  f the library..*
2e34b 2a 0a 2a 2a 20 24 49 64 3a 20 6d 61 69 6e 2e 63  *.** $Id: main.c
2e34c 2c 76 20 31 2e 35 32 31 20 32 30 30 39 2f 30 31  ,v 1.521 2009/01
2e34d 2f 31 30 20 31 36 3a 31 35 3a 32 32 20 64 72 68  /10 16:15:22 drh
2e34e 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65   Exp $.*/..#ifde
2e34f 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
2e350 46 54 53 33 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  FTS3./**********
2e351 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73  **** Include fts
2e352 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  3.h in the middl
2e353 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a  e of main.c ****
2e354 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e355 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2e356 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2e357 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fts3.h *********
2e358 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e359 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e35a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
2e35b 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65  Oct 10.**.** The
2e35c 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
2e35d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
2e35e 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
2e35f 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
2e360 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2e361 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2e362 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2e363 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2e364 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
2e365 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
2e366 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
2e367 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
2e368 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
2e369 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2e36a 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
2e36b 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
2e36c 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
2e36d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e36e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e36f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e371 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
2e372 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
2e373 65 20 69 73 20 75 73 65 64 20 62 79 20 70 72 6f  e is used by pro
2e374 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20  grams that want 
2e375 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  to link against 
2e376 74 68 65 0a 2a 2a 20 46 54 53 33 20 6c 69 62 72  the.** FTS3 libr
2e377 61 72 79 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65  ary.  All it doe
2e378 73 20 69 73 20 64 65 63 6c 61 72 65 20 74 68 65  s is declare the
2e379 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
2e37a 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  () interface..*/
2e37b 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22  ..#if 0.extern "
2e37c 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20  C" {.#endif  /* 
2e37d 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a  __cplusplus */..
2e37e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2e37f 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e  nt sqlite3Fts3In
2e380 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  it(sqlite3 *db);
2e381 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78  ..#if 0.}  /* ex
2e382 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64  tern "C" */.#end
2e383 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c  if  /* __cpluspl
2e384 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  us */../********
2e385 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
2e386 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s3.h ***********
2e387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e389 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2e38a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
2e38b 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
2e38c 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a  off in main.c **
2e38d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e38e 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  *****/.#endif.#i
2e38f 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
2e390 4c 45 5f 52 54 52 45 45 0a 2f 2a 2a 2a 2a 2a 2a  LE_RTREE./******
2e391 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
2e392 20 72 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20   rtree.h in the 
2e393 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63  middle of main.c
2e394 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2e395 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2e396 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2e397 69 6c 65 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a  ile rtree.h ****
2e398 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e399 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e39a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
2e39b 30 30 38 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a  008 May 26.**.**
2e39c 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2e39d 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2e39e 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2e39f 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2e3a0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2e3a1 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2e3a2 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2e3a3 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2e3a4 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2e3a5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2e3a6 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2e3a7 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2e3a8 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2e3a9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2e3aa 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2e3ab 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2e3ac 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2e3ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2e3b2 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
2e3b3 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 62 79   file is used by
2e3b4 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 77   programs that w
2e3b5 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69  ant to link agai
2e3b6 6e 73 74 20 74 68 65 0a 2a 2a 20 52 54 52 45 45  nst the.** RTREE
2e3b7 20 6c 69 62 72 61 72 79 2e 20 20 41 6c 6c 20 69   library.  All i
2e3b8 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61 72  t does is declar
2e3b9 65 20 74 68 65 20 73 71 6c 69 74 65 33 52 74 72  e the sqlite3Rtr
2e3ba 65 65 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61  eeInit() interfa
2e3bb 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78  ce..*/..#if 0.ex
2e3bc 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69  tern "C" {.#endi
2e3bd 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75  f  /* __cplusplu
2e3be 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49  s */..SQLITE_PRI
2e3bf 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2e3c0 52 74 72 65 65 49 6e 69 74 28 73 71 6c 69 74 65  RtreeInit(sqlite
2e3c1 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d  3 *db);..#if 0.}
2e3c2 20 20 2f 2a 20 65 78 74 65 72 6e 20 22 43 22 20    /* extern "C" 
2e3c3 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f  */.#endif  /* __
2e3c4 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a  cplusplus */../*
2e3c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
2e3c6 64 20 6f 66 20 72 74 72 65 65 2e 68 20 2a 2a 2a  d of rtree.h ***
2e3c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2e3ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
2e3cb 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
2e3cc 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61  e left off in ma
2e3cd 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
2e3ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65  ************/.#e
2e3cf 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
2e3d0 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 2f 2a  TE_ENABLE_ICU./*
2e3d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
2e3d2 63 6c 75 64 65 20 73 71 6c 69 74 65 69 63 75 2e  clude sqliteicu.
2e3d3 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
2e3d4 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a  of main.c ******
2e3d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2e3d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2e3d7 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 69  gin file sqlitei
2e3d8 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cu.h ***********
2e3d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2e3db 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 36 0a  .** 2008 May 26.
2e3dc 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2e3dd 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2e3de 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2e3df 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2e3e0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2e3e1 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2e3e2 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2e3e3 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2e3e4 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2e3e5 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2e3e6 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2e3e7 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2e3e8 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2e3e9 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2e3ea 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2e3eb 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2e3ec 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2e3ed 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2e3ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3f2 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68  ***.**.** This h
2e3f3 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 75 73  eader file is us
2e3f4 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74  ed by programs t
2e3f5 68 61 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b  hat want to link
2e3f6 20 61 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20   against the.** 
2e3f7 49 43 55 20 65 78 74 65 6e 73 69 6f 6e 2e 20 20  ICU extension.  
2e3f8 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73 20 64  All it does is d
2e3f9 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c 69 74  eclare the sqlit
2e3fa 65 33 49 63 75 49 6e 69 74 28 29 20 69 6e 74 65  e3IcuInit() inte
2e3fb 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30  rface..*/..#if 0
2e3fc 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65  .extern "C" {.#e
2e3fd 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73  ndif  /* __cplus
2e3fe 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f  plus */..SQLITE_
2e3ff 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2e400 74 65 33 49 63 75 49 6e 69 74 28 73 71 6c 69 74  te3IcuInit(sqlit
2e401 65 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a  e3 *db);..#if 0.
2e402 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20 22 43 22  }  /* extern "C"
2e403 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f   */.#endif  /* _
2e404 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 0a  _cplusplus */...
2e405 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2e406 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 69 63 75  End of sqliteicu
2e407 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
2e408 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e409 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2e40a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2e40b 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
2e40c 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
2e40d 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  main.c *********
2e40e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2e40f 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
2e410 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  e version of the
2e411 20 6c 69 62 72 61 72 79 0a 2a 2f 0a 23 69 66 6e   library.*/.#ifn
2e412 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47  def SQLITE_AMALG
2e413 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 41  AMATION.SQLITE_A
2e414 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  PI const char sq
2e415 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20  lite3_version[] 
2e416 3d 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  = SQLITE_VERSION
2e417 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ;.#endif.SQLITE_
2e418 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
2e419 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
2e41a 6f 6e 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  on(void){ return
2e41b 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e   sqlite3_version
2e41c 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ; }.SQLITE_API i
2e41d 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  nt sqlite3_libve
2e41e 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69  rsion_number(voi
2e41f 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
2e420 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
2e421 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ; }.SQLITE_API i
2e422 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  nt sqlite3_threa
2e423 64 73 61 66 65 28 76 6f 69 64 29 7b 20 72 65 74  dsafe(void){ ret
2e424 75 72 6e 20 53 51 4c 49 54 45 5f 54 48 52 45 41  urn SQLITE_THREA
2e425 44 53 41 46 45 3b 20 7d 0a 0a 23 69 66 20 21 64  DSAFE; }..#if !d
2e426 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2e427 49 54 5f 54 52 41 43 45 29 20 26 26 20 64 65 66  IT_TRACE) && def
2e428 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
2e429 4c 45 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a  LE_IOTRACE)./*.*
2e42a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * If the followi
2e42b 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e  ng function poin
2e42c 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  ter is not NULL 
2e42d 61 6e 64 20 69 66 0a 2a 2a 20 53 51 4c 49 54 45  and if.** SQLITE
2e42e 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20  _ENABLE_IOTRACE 
2e42f 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e  is enabled, then
2e430 20 6d 65 73 73 61 67 65 73 20 64 65 73 63 72 69   messages descri
2e431 62 69 6e 67 0a 2a 2a 20 49 2f 4f 20 61 63 74 69  bing.** I/O acti
2e432 76 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 75  ve are written u
2e433 73 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69  sing this functi
2e434 6f 6e 2e 20 20 54 68 65 73 65 20 6d 65 73 73 61  on.  These messa
2e435 67 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e  ges.** are inten
2e436 64 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e  ded for debuggin
2e437 67 20 61 63 74 69 76 69 74 79 20 6f 6e 6c 79 2e  g activity only.
2e438 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2e439 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65  TE void (*sqlite
2e43a 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 20  3IoTrace)(const 
2e43b 63 68 61 72 2a 2c 20 2e 2e 2e 29 20 3d 20 30 3b  char*, ...) = 0;
2e43c 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
2e43d 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2e43e 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
2e43f 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69  points to a stri
2e440 6e 67 20 77 68 69 63 68 20 69 73 20 74 68 65 0a  ng which is the.
2e441 2a 2a 20 6e 61 6d 65 20 6f 66 20 61 20 64 69 72  ** name of a dir
2e442 65 63 74 6f 72 79 2c 20 74 68 65 6e 20 74 68 61  ectory, then tha
2e443 74 20 64 69 72 65 63 74 6f 72 79 20 77 69 6c 6c  t directory will
2e444 20 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72   be used to stor
2e445 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66  e.** temporary f
2e446 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  iles..**.** See 
2e447 61 6c 73 6f 20 74 68 65 20 22 50 52 41 47 4d 41  also the "PRAGMA
2e448 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65   temp_store_dire
2e449 63 74 6f 72 79 22 20 53 51 4c 20 63 6f 6d 6d 61  ctory" SQL comma
2e44a 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  nd..*/.SQLITE_AP
2e44b 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
2e44c 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d  temp_directory =
2e44d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69   0;../*.** Initi
2e44e 61 6c 69 7a 65 20 53 51 4c 69 74 65 2e 20 20 0a  alize SQLite.  .
2e44f 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2e450 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  ne must be calle
2e451 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20  d to initialize 
2e452 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
2e453 61 74 69 6f 6e 2c 0a 2a 2a 20 56 46 53 2c 20 61  ation,.** VFS, a
2e454 6e 64 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  nd mutex subsyst
2e455 65 6d 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69  ems prior to doi
2e456 6e 67 20 61 6e 79 20 73 65 72 69 6f 75 73 20 77  ng any serious w
2e457 6f 72 6b 20 77 69 74 68 0a 2a 2a 20 53 51 4c 69  ork with.** SQLi
2e458 74 65 2e 20 20 42 75 74 20 61 73 20 6c 6f 6e 67  te.  But as long
2e459 20 61 73 20 79 6f 75 20 64 6f 20 6e 6f 74 20 63   as you do not c
2e45a 6f 6d 70 69 6c 65 20 77 69 74 68 20 53 51 4c 49  ompile with SQLI
2e45b 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
2e45c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
2e45d 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
2e45e 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79  automatically by
2e45f 20 6b 65 79 20 72 6f 75 74 69 6e 65 73 20 73 75   key routines su
2e460 63 68 20 61 73 0a 2a 2a 20 73 71 6c 69 74 65 33  ch as.** sqlite3
2e461 5f 6f 70 65 6e 28 29 2e 20 20 0a 2a 2a 0a 2a 2a  _open().  .**.**
2e462 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2e463 20 61 20 6e 6f 2d 6f 70 20 65 78 63 65 70 74 20   a no-op except 
2e464 6f 6e 20 69 74 73 20 76 65 72 79 20 66 69 72 73  on its very firs
2e465 74 20 63 61 6c 6c 20 66 6f 72 20 74 68 65 20 70  t call for the p
2e466 72 6f 63 65 73 73 2c 0a 2a 2a 20 6f 72 20 66 6f  rocess,.** or fo
2e467 72 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  r the first call
2e468 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f   after a call to
2e469 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
2e46a 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  n..**.** The fir
2e46b 73 74 20 74 68 72 65 61 64 20 74 6f 20 63 61 6c  st thread to cal
2e46c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  l this routine r
2e46d 75 6e 73 20 74 68 65 20 69 6e 69 74 69 61 6c 69  uns the initiali
2e46e 7a 61 74 69 6f 6e 20 74 6f 0a 2a 2a 20 63 6f 6d  zation to.** com
2e46f 70 6c 65 74 69 6f 6e 2e 20 20 49 66 20 73 75 62  pletion.  If sub
2e470 73 65 71 75 65 6e 74 20 74 68 72 65 61 64 73 20  sequent threads 
2e471 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
2e472 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72  e before the fir
2e473 73 74 0a 2a 2a 20 74 68 72 65 61 64 20 68 61 73  st.** thread has
2e474 20 66 69 6e 69 73 68 65 64 20 74 68 65 20 69 6e   finished the in
2e475 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f  itialization pro
2e476 63 65 73 73 2c 20 74 68 65 6e 20 74 68 65 20 73  cess, then the s
2e477 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 74 68 72  ubsequent.** thr
2e478 65 61 64 73 20 6d 75 73 74 20 62 6c 6f 63 6b 20  eads must block 
2e479 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 20  until the first 
2e47a 74 68 72 65 61 64 20 66 69 6e 69 73 68 65 73 20  thread finishes 
2e47b 77 69 74 68 20 74 68 65 20 69 6e 69 74 69 61 6c  with the initial
2e47c 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ization..**.** T
2e47d 68 65 20 66 69 72 73 74 20 74 68 72 65 61 64 20  he first thread 
2e47e 6d 69 67 68 74 20 63 61 6c 6c 20 74 68 69 73 20  might call this 
2e47f 72 6f 75 74 69 6e 65 20 72 65 63 75 72 73 69 76  routine recursiv
2e480 65 6c 79 2e 20 20 52 65 63 75 72 73 69 76 65 0a  ely.  Recursive.
2e481 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 74 68 69 73  ** calls to this
2e482 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20   routine should 
2e483 6e 6f 74 20 62 6c 6f 63 6b 2c 20 6f 66 20 63 6f  not block, of co
2e484 75 72 73 65 2e 20 20 4f 74 68 65 72 77 69 73 65  urse.  Otherwise
2e485 20 74 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 69   the.** initiali
2e486 7a 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 20 77  zation process w
2e487 6f 75 6c 64 20 6e 65 76 65 72 20 63 6f 6d 70 6c  ould never compl
2e488 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 74 20 58  ete..**.** Let X
2e489 20 62 65 20 74 68 65 20 66 69 72 73 74 20 74 68   be the first th
2e48a 72 65 61 64 20 74 6f 20 65 6e 74 65 72 20 74 68  read to enter th
2e48b 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 4c 65 74  is routine.  Let
2e48c 20 59 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72   Y be some other
2e48d 0a 2a 2a 20 74 68 72 65 61 64 2e 20 20 54 68 65  .** thread.  The
2e48e 6e 20 77 68 69 6c 65 20 74 68 65 20 69 6e 69 74  n while the init
2e48f 69 61 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ial invocation o
2e490 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 62  f this routine b
2e491 79 20 58 20 69 73 0a 2a 2a 20 69 6e 63 6f 6d 70  y X is.** incomp
2e492 6c 65 74 65 2c 20 69 74 20 69 73 20 72 65 71 75  lete, it is requ
2e493 69 72 65 64 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a  ired that:.**.**
2e494 20 20 20 20 2a 20 20 43 61 6c 6c 73 20 74 6f 20      *  Calls to 
2e495 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f  this routine fro
2e496 6d 20 59 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75  m Y must block u
2e497 6e 74 69 6c 20 74 68 65 20 6f 75 74 65 72 2d 6d  ntil the outer-m
2e498 6f 73 74 0a 2a 2a 20 20 20 20 20 20 20 63 61 6c  ost.**       cal
2e499 6c 20 62 79 20 58 20 63 6f 6d 70 6c 65 74 65 73  l by X completes
2e49a 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 52 65  ..**.**    *  Re
2e49b 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 74 6f  cursive calls to
2e49c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72   this routine fr
2e49d 6f 6d 20 74 68 72 65 61 64 20 58 20 72 65 74 75  om thread X retu
2e49e 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a  rn immediately.*
2e49f 2a 20 20 20 20 20 20 20 77 69 74 68 6f 75 74 20  *       without 
2e4a0 62 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  blocking..*/.SQL
2e4a1 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2e4a2 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76  te3_initialize(v
2e4a3 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  oid){.  sqlite3_
2e4a4 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 20  mutex *pMaster; 
2e4a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e4a6 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e       /* The main
2e4a7 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 2a 2f   static mutex */
2e4a8 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
2e4a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e4aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e4ab 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a  /* Result code *
2e4ac 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
2e4ad 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 72 63 20 3d  _OMIT_WSD.  rc =
2e4ae 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69   sqlite3_wsd_ini
2e4af 74 28 34 30 39 36 2c 20 32 34 29 3b 0a 20 20 69  t(4096, 24);.  i
2e4b0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2e4b1 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
2e4b2 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  c;.  }.#endif.. 
2e4b3 20 2f 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73   /* If SQLite is
2e4b4 20 61 6c 72 65 61 64 79 20 63 6f 6d 70 6c 65 74   already complet
2e4b5 65 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c  ely initialized,
2e4b6 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 0a   then this call.
2e4b7 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f    ** to sqlite3_
2e4b8 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73 68 6f  initialize() sho
2e4b9 75 6c 64 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20  uld be a no-op. 
2e4ba 20 42 75 74 20 74 68 65 20 69 6e 69 74 69 61 6c   But the initial
2e4bb 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 6d 75 73  ization.  ** mus
2e4bc 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 2e 20 20  t be complete.  
2e4bd 53 6f 20 69 73 49 6e 69 74 20 6d 75 73 74 20 6e  So isInit must n
2e4be 6f 74 20 62 65 20 73 65 74 20 75 6e 74 69 6c 20  ot be set until 
2e4bf 74 68 65 20 76 65 72 79 20 65 6e 64 0a 20 20 2a  the very end.  *
2e4c0 2a 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * of this routin
2e4c1 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  e..  */.  if( sq
2e4c2 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2e4c3 67 2e 69 73 49 6e 69 74 20 29 20 72 65 74 75 72  g.isInit ) retur
2e4c4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  n SQLITE_OK;..  
2e4c5 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
2e4c6 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
2e4c7 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   is initialized.
2e4c8 20 20 49 66 20 75 6e 61 62 6c 65 20 74 6f 20 0a    If unable to .
2e4c9 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20    ** initialize 
2e4ca 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
2e4cb 74 65 6d 2c 20 72 65 74 75 72 6e 20 65 61 72 6c  tem, return earl
2e4cc 79 20 77 69 74 68 20 74 68 65 20 65 72 72 6f 72  y with the error
2e4cd 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 73 79  ..  ** If the sy
2e4ce 73 74 65 6d 20 69 73 20 73 6f 20 73 69 63 6b 20  stem is so sick 
2e4cf 74 68 61 74 20 77 65 20 61 72 65 20 75 6e 61 62  that we are unab
2e4d0 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  le to allocate a
2e4d1 20 6d 75 74 65 78 2c 0a 20 20 2a 2a 20 74 68 65   mutex,.  ** the
2e4d2 72 65 20 69 73 20 6e 6f 74 20 6d 75 63 68 20 53  re is not much S
2e4d3 51 4c 69 74 65 20 69 73 20 67 6f 69 6e 67 20 74  QLite is going t
2e4d4 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 64 6f 2e  o be able to do.
2e4d5 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6d  .  **.  ** The m
2e4d6 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 20 6d  utex subsystem m
2e4d7 75 73 74 20 74 61 6b 65 20 63 61 72 65 20 6f 66  ust take care of
2e4d8 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 69 74 73   serializing its
2e4d9 20 6f 77 6e 0a 20 20 2a 2a 20 69 6e 69 74 69 61   own.  ** initia
2e4da 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  lization..  */. 
2e4db 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 75 74   rc = sqlite3Mut
2e4dc 65 78 49 6e 69 74 28 29 3b 0a 20 20 69 66 28 20  exInit();.  if( 
2e4dd 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
2e4de 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
2e4df 20 74 68 65 20 6d 61 6c 6c 6f 63 28 29 20 73 79   the malloc() sy
2e4e0 73 74 65 6d 20 61 6e 64 20 74 68 65 20 72 65 63  stem and the rec
2e4e1 75 72 73 69 76 65 20 70 49 6e 69 74 4d 75 74 65  ursive pInitMute
2e4e2 78 20 6d 75 74 65 78 2e 0a 20 20 2a 2a 20 54 68  x mutex..  ** Th
2e4e3 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  is operation is 
2e4e4 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65  protected by the
2e4e5 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d   STATIC_MASTER m
2e4e6 75 74 65 78 2e 20 20 4e 6f 74 65 20 74 68 61 74  utex.  Note that
2e4e7 0a 20 20 2a 2a 20 4d 75 74 65 78 41 6c 6c 6f 63  .  ** MutexAlloc
2e4e8 28 29 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72  () is called for
2e4e9 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 20   a static mutex 
2e4ea 70 72 69 6f 72 20 74 6f 20 69 6e 69 74 69 61 6c  prior to initial
2e4eb 69 7a 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 6d  izing the.  ** m
2e4ec 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 20  alloc subsystem 
2e4ed 2d 20 74 68 69 73 20 69 6d 70 6c 69 65 73 20 74  - this implies t
2e4ee 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  hat the allocati
2e4ef 6f 6e 20 6f 66 20 61 20 73 74 61 74 69 63 0a 20  on of a static. 
2e4f0 20 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 6e   ** mutex must n
2e4f1 6f 74 20 72 65 71 75 69 72 65 20 73 75 70 70 6f  ot require suppo
2e4f2 72 74 20 66 72 6f 6d 20 74 68 65 20 6d 61 6c 6c  rt from the mall
2e4f3 6f 63 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20  oc subsystem..  
2e4f4 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73  */.  pMaster = s
2e4f5 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
2e4f6 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
2e4f7 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20  ATIC_MASTER);.  
2e4f8 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
2e4f9 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ter(pMaster);.  
2e4fa 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62  if( !sqlite3Glob
2e4fb 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f  alConfig.isMallo
2e4fc 63 49 6e 69 74 20 29 7b 0a 20 20 20 20 72 63 20  cInit ){.    rc 
2e4fd 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49  = sqlite3MallocI
2e4fe 6e 69 74 28 29 3b 0a 20 20 7d 0a 20 20 69 66 28  nit();.  }.  if(
2e4ff 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2e500 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
2e501 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c  balConfig.isMall
2e502 6f 63 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20  ocInit = 1;.    
2e503 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62  if( !sqlite3Glob
2e504 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75  alConfig.pInitMu
2e505 74 65 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tex ){.      sql
2e506 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2e507 2e 70 49 6e 69 74 4d 75 74 65 78 20 3d 20 73 71  .pInitMutex = sq
2e508 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
2e509 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
2e50a 55 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 69  URSIVE);.      i
2e50b 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
2e50c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65  Config.bCoreMute
2e50d 78 20 26 26 20 21 73 71 6c 69 74 65 33 47 6c 6f  x && !sqlite3Glo
2e50e 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d  balConfig.pInitM
2e50f 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20  utex ){.        
2e510 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2e511 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  M;.      }.    }
2e512 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
2e513 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2e514 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2e515 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65  fig.nRefInitMute
2e516 78 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  x++;.  }.  sqlit
2e517 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
2e518 4d 61 73 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 49  Master);..  /* I
2e519 66 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74  f unable to init
2e51a 69 61 6c 69 7a 65 20 74 68 65 20 6d 61 6c 6c 6f  ialize the mallo
2e51b 63 20 73 75 62 73 79 73 74 65 6d 2c 20 74 68 65  c subsystem, the
2e51c 6e 20 72 65 74 75 72 6e 20 65 61 72 6c 79 2e 0a  n return early..
2e51d 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 6c 69    ** There is li
2e51e 74 74 6c 65 20 68 6f 70 65 20 6f 66 20 67 65 74  ttle hope of get
2e51f 74 69 6e 67 20 53 51 4c 69 74 65 20 74 6f 20 72  ting SQLite to r
2e520 75 6e 20 69 66 20 74 68 65 20 6d 61 6c 6c 6f 63  un if the malloc
2e521 0a 20 20 2a 2a 20 73 75 62 73 79 73 74 65 6d 20  .  ** subsystem 
2e522 63 61 6e 6e 6f 74 20 62 65 20 69 6e 69 74 69 61  cannot be initia
2e523 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  lized..  */.  if
2e524 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2e525 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
2e526 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 74  ;.  }..  /* Do t
2e527 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 69  he rest of the i
2e528 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 6e  nitialization un
2e529 64 65 72 20 74 68 65 20 72 65 63 75 72 73 69 76  der the recursiv
2e52a 65 20 6d 75 74 65 78 20 73 6f 0a 20 20 2a 2a 20  e mutex so.  ** 
2e52b 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20  that we will be 
2e52c 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 72  able to handle r
2e52d 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 69  ecursive calls i
2e52e 6e 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  nto.  ** sqlite3
2e52f 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20  _initialize().  
2e530 54 68 65 20 72 65 63 75 72 73 69 76 65 20 63 61  The recursive ca
2e531 6c 6c 73 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6d  lls normally com
2e532 65 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 73  e through.  ** s
2e533 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
2e534 20 77 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73   when it invokes
2e535 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67   sqlite3_vfs_reg
2e536 69 73 74 65 72 28 29 2c 20 62 75 74 20 6f 74 68  ister(), but oth
2e537 65 72 0a 20 20 2a 2a 20 72 65 63 75 72 73 69 76  er.  ** recursiv
2e538 65 20 63 61 6c 6c 73 20 6d 69 67 68 74 20 61 6c  e calls might al
2e539 73 6f 20 62 65 20 70 6f 73 73 69 62 6c 65 2e 0a  so be possible..
2e53a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d    */.  sqlite3_m
2e53b 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
2e53c 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
2e53d 49 6e 69 74 4d 75 74 65 78 29 3b 0a 20 20 69 66  InitMutex);.  if
2e53e 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
2e53f 6f 6e 66 69 67 2e 69 73 49 6e 69 74 3d 3d 30 20  onfig.isInit==0 
2e540 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  && sqlite3Global
2e541 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73  Config.inProgres
2e542 73 3d 3d 30 20 29 7b 0a 20 20 20 20 46 75 6e 63  s==0 ){.    Func
2e543 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d  DefHash *pHash =
2e544 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66   &GLOBAL(FuncDef
2e545 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f  Hash, sqlite3Glo
2e546 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20  balFunctions);. 
2e547 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2e548 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73  Config.inProgres
2e549 73 20 3d 20 31 3b 0a 20 20 20 20 6d 65 6d 73 65  s = 1;.    memse
2e54a 74 28 70 48 61 73 68 2c 20 30 2c 20 73 69 7a 65  t(pHash, 0, size
2e54b 6f 66 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  of(sqlite3Global
2e54c 46 75 6e 63 74 69 6f 6e 73 29 29 3b 0a 20 20 20  Functions));.   
2e54d 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
2e54e 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28  GlobalFunctions(
2e54f 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
2e550 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 3b 0a 20  te3_os_init();. 
2e551 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2e552 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
2e553 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65   = sqlite3Pcache
2e554 49 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20  Initialize();.  
2e555 20 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68      sqlite3PCach
2e556 65 42 75 66 66 65 72 53 65 74 75 70 28 20 73 71  eBufferSetup( sq
2e557 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2e558 67 2e 70 50 61 67 65 2c 20 0a 20 20 20 20 20 20  g.pPage, .      
2e559 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2e55a 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 2c 20  lConfig.szPage, 
2e55b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2e55c 66 69 67 2e 6e 50 61 67 65 29 3b 0a 20 20 20 20  fig.nPage);.    
2e55d 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  }.    sqlite3Glo
2e55e 62 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67  balConfig.inProg
2e55f 72 65 73 73 20 3d 20 30 3b 0a 20 20 20 20 73 71  ress = 0;.    sq
2e560 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2e561 67 2e 69 73 49 6e 69 74 20 3d 20 28 72 63 3d 3d  g.isInit = (rc==
2e562 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 31 20 3a 20  SQLITE_OK ? 1 : 
2e563 30 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  0);.  }.  sqlite
2e564 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71  3_mutex_leave(sq
2e565 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2e566 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a 0a  g.pInitMutex);..
2e567 20 20 2f 2a 20 47 6f 20 62 61 63 6b 20 75 6e 64    /* Go back und
2e568 65 72 20 74 68 65 20 73 74 61 74 69 63 20 6d 75  er the static mu
2e569 74 65 78 20 61 6e 64 20 63 6c 65 61 6e 20 75 70  tex and clean up
2e56a 20 74 68 65 20 72 65 63 75 72 73 69 76 65 0a 20   the recursive. 
2e56b 20 2a 2a 20 6d 75 74 65 78 20 74 6f 20 70 72 65   ** mutex to pre
2e56c 76 65 6e 74 20 61 20 72 65 73 6f 75 72 63 65 20  vent a resource 
2e56d 6c 65 61 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  leak..  */.  sql
2e56e 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
2e56f 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 73 71 6c  (pMaster);.  sql
2e570 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2e571 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 2d 2d  .nRefInitMutex--
2e572 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  ;.  if( sqlite3G
2e573 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66  lobalConfig.nRef
2e574 49 6e 69 74 4d 75 74 65 78 3c 3d 30 20 29 7b 0a  InitMutex<=0 ){.
2e575 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
2e576 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2e577 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 3d 3d 30  nRefInitMutex==0
2e578 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   );.    sqlite3_
2e579 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74  mutex_free(sqlit
2e57a 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
2e57b 49 6e 69 74 4d 75 74 65 78 29 3b 0a 20 20 20 20  InitMutex);.    
2e57c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2e57d 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20 3d  fig.pInitMutex =
2e57e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
2e57f 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d  3_mutex_leave(pM
2e580 61 73 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 54 68  aster);..  /* Th
2e581 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 6a  e following is j
2e582 75 73 74 20 61 20 73 61 6e 69 74 79 20 63 68 65  ust a sanity che
2e583 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
2e584 53 51 4c 69 74 65 20 68 61 73 0a 20 20 2a 2a 20  SQLite has.  ** 
2e585 62 65 65 6e 20 63 6f 6d 70 69 6c 65 64 20 63 6f  been compiled co
2e586 72 72 65 63 74 6c 79 2e 20 20 49 74 20 69 73 20  rrectly.  It is 
2e587 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 72 75 6e  important to run
2e588 20 74 68 69 73 20 63 6f 64 65 2c 20 62 75 74 0a   this code, but.
2e589 20 20 2a 2a 20 77 65 20 64 6f 6e 27 74 20 77 61    ** we don't wa
2e58a 6e 74 20 74 6f 20 72 75 6e 20 69 74 20 74 6f 6f  nt to run it too
2e58b 20 6f 66 74 65 6e 20 61 6e 64 20 73 6f 61 6b 20   often and soak 
2e58c 75 70 20 43 50 55 20 63 79 63 6c 65 73 20 66 6f  up CPU cycles fo
2e58d 72 20 6e 6f 0a 20 20 2a 2a 20 72 65 61 73 6f 6e  r no.  ** reason
2e58e 2e 20 20 53 6f 20 77 65 20 72 75 6e 20 69 74 20  .  So we run it 
2e58f 6f 6e 63 65 20 64 75 72 69 6e 67 20 69 6e 69 74  once during init
2e590 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f  ialization..  */
2e591 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2e592 20 20 2f 2a 20 54 68 69 73 20 73 65 63 74 69 6f    /* This sectio
2e593 6e 20 6f 66 20 63 6f 64 65 27 73 20 6f 6e 6c 79  n of code's only
2e594 20 22 6f 75 74 70 75 74 22 20 69 73 20 76 69 61   "output" is via
2e595 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
2e596 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 20 28 20  ents. */.  if ( 
2e597 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2e598 0a 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 28  .    u64 x = (((
2e599 75 36 34 29 31 29 3c 3c 36 33 29 2d 31 3b 0a 20  u64)1)<<63)-1;. 
2e59a 20 20 20 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20     double y;.   
2e59b 20 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28 78   assert(sizeof(x
2e59c 29 3d 3d 38 29 3b 0a 20 20 20 20 61 73 73 65 72  )==8);.    asser
2e59d 74 28 73 69 7a 65 6f 66 28 78 29 3d 3d 73 69 7a  t(sizeof(x)==siz
2e59e 65 6f 66 28 79 29 29 3b 0a 20 20 20 20 6d 65 6d  eof(y));.    mem
2e59f 63 70 79 28 26 79 2c 20 26 78 2c 20 38 29 3b 0a  cpy(&y, &x, 8);.
2e5a0 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
2e5a1 74 65 33 49 73 4e 61 4e 28 79 29 20 29 3b 0a 20  te3IsNaN(y) );. 
2e5a2 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74   }.#endif..  ret
2e5a3 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2e5a4 20 55 6e 64 6f 20 74 68 65 20 65 66 66 65 63 74   Undo the effect
2e5a5 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e 69  s of sqlite3_ini
2e5a6 74 69 61 6c 69 7a 65 28 29 2e 20 20 4d 75 73 74  tialize().  Must
2e5a7 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 77   not be called w
2e5a8 68 69 6c 65 0a 2a 2a 20 74 68 65 72 65 20 61 72  hile.** there ar
2e5a9 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 61  e outstanding da
2e5aa 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2e5ab 6e 73 20 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c  ns or memory all
2e5ac 6f 63 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 77  ocations or.** w
2e5ad 68 69 6c 65 20 61 6e 79 20 70 61 72 74 20 6f 66  hile any part of
2e5ae 20 53 51 4c 69 74 65 20 69 73 20 6f 74 68 65 72   SQLite is other
2e5af 77 69 73 65 20 69 6e 20 75 73 65 20 69 6e 20 61  wise in use in a
2e5b0 6e 79 20 74 68 72 65 61 64 2e 20 20 54 68 69 73  ny thread.  This
2e5b1 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 6e  .** routine is n
2e5b2 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20  ot threadsafe.  
2e5b3 4e 6f 74 20 62 79 20 61 20 6c 6f 6e 67 20 73 68  Not by a long sh
2e5b4 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ot..*/.SQLITE_AP
2e5b5 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 68  I int sqlite3_sh
2e5b6 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 20  utdown(void){.  
2e5b7 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2e5b8 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74  fig.isMallocInit
2e5b9 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50   = 0;.  sqlite3P
2e5ba 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 29 3b  cacheShutdown();
2e5bb 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
2e5bc 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69  obalConfig.isIni
2e5bd 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
2e5be 5f 6f 73 5f 65 6e 64 28 29 3b 0a 20 20 7d 0a 20  _os_end();.  }. 
2e5bf 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e   sqlite3MallocEn
2e5c0 64 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 4d 75  d();.  sqlite3Mu
2e5c1 74 65 78 45 6e 64 28 29 3b 0a 20 20 73 71 6c 69  texEnd();.  sqli
2e5c2 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2e5c3 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 72 65  isInit = 0;.  re
2e5c4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2e5c5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 41 50  }../*.** This AP
2e5c6 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c 69 63 61  I allows applica
2e5c7 74 69 6f 6e 73 20 74 6f 20 6d 6f 64 69 66 79 20  tions to modify 
2e5c8 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69  the global confi
2e5c9 67 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74  guration of.** t
2e5ca 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
2e5cb 79 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 0a 2a  y at run-time..*
2e5cc 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2e5cd 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65  e should only be
2e5ce 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   called when the
2e5cf 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61  re are no outsta
2e5d0 6e 64 69 6e 67 0a 2a 2a 20 64 61 74 61 62 61 73  nding.** databas
2e5d1 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 72  e connections or
2e5d2 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
2e5d3 6f 6e 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ons.  This routi
2e5d4 6e 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 74 68 72  ne is not.** thr
2e5d5 65 61 64 73 61 66 65 2e 20 20 46 61 69 6c 75 72  eadsafe.  Failur
2e5d6 65 20 74 6f 20 68 65 65 64 20 74 68 65 73 65 20  e to heed these 
2e5d7 77 61 72 6e 69 6e 67 73 20 63 61 6e 20 6c 65 61  warnings can lea
2e5d8 64 20 74 6f 20 75 6e 70 72 65 64 69 63 74 61 62  d to unpredictab
2e5d9 6c 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a  le.** behavior..
2e5da 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2e5db 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  t sqlite3_config
2e5dc 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20  (int op, ...){. 
2e5dd 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69   va_list ap;.  i
2e5de 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
2e5df 4b 3b 0a 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33  K;..  /* sqlite3
2e5e0 5f 63 6f 6e 66 69 67 28 29 20 73 68 61 6c 6c 20  _config() shall 
2e5e1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
2e5e2 53 55 53 45 20 69 66 20 69 74 20 69 73 20 69 6e  SUSE if it is in
2e5e3 76 6f 6b 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a  voked while.  **
2e5e4 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
2e5e5 61 72 79 20 69 73 20 69 6e 20 75 73 65 2e 20 2a  ary is in use. *
2e5e6 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  /.  if( sqlite3G
2e5e7 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e  lobalConfig.isIn
2e5e8 69 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  it ) return SQLI
2e5e9 54 45 5f 4d 49 53 55 53 45 3b 0a 0a 20 20 76 61  TE_MISUSE;..  va
2e5ea 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a  _start(ap, op);.
2e5eb 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
2e5ec 0a 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f  .    /* Mutex co
2e5ed 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
2e5ee 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 20 61 76 61  ons are only ava
2e5ef 69 6c 61 62 6c 65 20 69 6e 20 61 20 74 68 72 65  ilable in a thre
2e5f0 61 64 73 61 66 65 0a 20 20 20 20 2a 2a 20 63 6f  adsafe.    ** co
2e5f1 6d 70 69 6c 65 2e 20 0a 20 20 20 20 2a 2f 0a 23  mpile. .    */.#
2e5f2 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
2e5f3 53 41 46 45 0a 20 20 20 20 63 61 73 65 20 53 51  SAFE.    case SQ
2e5f4 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47  LITE_CONFIG_SING
2e5f5 4c 45 54 48 52 45 41 44 3a 20 7b 0a 20 20 20 20  LETHREAD: {.    
2e5f6 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 61 6c 6c    /* Disable all
2e5f7 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 20   mutexing */.   
2e5f8 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2e5f9 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65  Config.bCoreMute
2e5fa 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  x = 0;.      sql
2e5fb 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2e5fc 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20 30 3b  .bFullMutex = 0;
2e5fd 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2e5fe 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
2e5ff 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49  ITE_CONFIG_MULTI
2e600 54 48 52 45 41 44 3a 20 7b 0a 20 20 20 20 20 20  THREAD: {.      
2e601 2f 2a 20 44 69 73 61 62 6c 65 20 6d 75 74 65 78  /* Disable mutex
2e602 69 6e 67 20 6f 66 20 64 61 74 61 62 61 73 65 20  ing of database 
2e603 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 20  connections */. 
2e604 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6d       /* Enable m
2e605 75 74 65 78 69 6e 67 20 6f 66 20 63 6f 72 65 20  utexing of core 
2e606 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
2e607 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2e608 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
2e609 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20 20  reMutex = 1;.   
2e60a 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2e60b 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65  Config.bFullMute
2e60c 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65  x = 0;.      bre
2e60d 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2e60e 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
2e60f 5f 53 45 52 49 41 4c 49 5a 45 44 3a 20 7b 0a 20  _SERIALIZED: {. 
2e610 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20 61       /* Enable a
2e611 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20  ll mutexing */. 
2e612 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2e613 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75  alConfig.bCoreMu
2e614 74 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 73  tex = 1;.      s
2e615 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2e616 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20  ig.bFullMutex = 
2e617 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  1;.      break;.
2e618 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
2e619 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54  QLITE_CONFIG_MUT
2e61a 45 58 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53  EX: {.      /* S
2e61b 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
2e61c 61 74 69 76 65 20 6d 75 74 65 78 20 69 6d 70 6c  ative mutex impl
2e61d 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  ementation */.  
2e61e 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2e61f 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 20 3d 20  lConfig.mutex = 
2e620 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69  *va_arg(ap, sqli
2e621 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
2e622 73 2a 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  s*);.      break
2e623 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2e624 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47   SQLITE_CONFIG_G
2e625 45 54 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20 20  ETMUTEX: {.     
2e626 20 2f 2a 20 52 65 74 72 69 65 76 65 20 74 68 65   /* Retrieve the
2e627 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 20 69   current mutex i
2e628 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  mplementation */
2e629 0a 20 20 20 20 20 20 2a 76 61 5f 61 72 67 28 61  .      *va_arg(a
2e62a 70 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  p, sqlite3_mutex
2e62b 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c  _methods*) = sql
2e62c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2e62d 2e 6d 75 74 65 78 3b 0a 20 20 20 20 20 20 62 72  .mutex;.      br
2e62e 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2e62f 66 0a 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  f...    case SQL
2e630 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f  ITE_CONFIG_MALLO
2e631 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70  C: {.      /* Sp
2e632 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61  ecify an alterna
2e633 74 69 76 65 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c  tive malloc impl
2e634 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  ementation */.  
2e635 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2e636 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 76 61 5f  lConfig.m = *va_
2e637 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f  arg(ap, sqlite3_
2e638 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a 20  mem_methods*);. 
2e639 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e63a 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
2e63b 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c  E_CONFIG_GETMALL
2e63c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 52  OC: {.      /* R
2e63d 65 74 72 69 65 76 65 20 74 68 65 20 63 75 72 72  etrieve the curr
2e63e 65 6e 74 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70  ent malloc() imp
2e63f 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
2e640 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2e641 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
2e642 4d 61 6c 6c 6f 63 3d 3d 30 20 29 20 73 71 6c 69  Malloc==0 ) sqli
2e643 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74  te3MemSetDefault
2e644 28 29 3b 0a 20 20 20 20 20 20 2a 76 61 5f 61 72  ();.      *va_ar
2e645 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 65  g(ap, sqlite3_me
2e646 6d 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71  m_methods*) = sq
2e647 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2e648 67 2e 6d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  g.m;.      break
2e649 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2e64a 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
2e64b 45 4d 53 54 41 54 55 53 3a 20 7b 0a 20 20 20 20  EMSTATUS: {.    
2e64c 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64    /* Enable or d
2e64d 69 73 61 62 6c 65 20 74 68 65 20 6d 61 6c 6c 6f  isable the mallo
2e64e 63 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 63 74  c status collect
2e64f 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ion */.      sql
2e650 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2e651 2e 62 4d 65 6d 73 74 61 74 20 3d 20 76 61 5f 61  .bMemstat = va_a
2e652 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
2e653 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2e654 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e655 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3a 20  CONFIG_SCRATCH: 
2e656 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69 67  {.      /* Desig
2e657 6e 61 74 65 20 61 20 62 75 66 66 65 72 20 66 6f  nate a buffer fo
2e658 72 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  r scratch memory
2e659 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20   space */.      
2e65a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2e65b 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 76  fig.pScratch = v
2e65c 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29  a_arg(ap, void*)
2e65d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  ;.      sqlite3G
2e65e 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63  lobalConfig.szSc
2e65f 72 61 74 63 68 20 3d 20 76 61 5f 61 72 67 28 61  ratch = va_arg(a
2e660 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 73  p, int);.      s
2e661 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2e662 69 67 2e 6e 53 63 72 61 74 63 68 20 3d 20 76 61  ig.nScratch = va
2e663 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
2e664 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e665 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
2e666 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
2e667 48 45 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44  HE: {.      /* D
2e668 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65  esignate a buffe
2e669 72 20 66 6f 72 20 73 63 72 61 74 63 68 20 6d 65  r for scratch me
2e66a 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20  mory space */.  
2e66b 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2e66c 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20  lConfig.pPage = 
2e66d 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a  va_arg(ap, void*
2e66e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2e66f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
2e670 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  age = va_arg(ap,
2e671 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c   int);.      sql
2e672 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2e673 2e 6e 50 61 67 65 20 3d 20 76 61 5f 61 72 67 28  .nPage = va_arg(
2e674 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
2e675 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
2e676 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
2e677 4e 46 49 47 5f 50 43 41 43 48 45 3a 20 7b 0a 20  NFIG_PCACHE: {. 
2e678 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 79 20       /* Specify 
2e679 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d  an alternative m
2e67a 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61  alloc implementa
2e67b 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71  tion */.      sq
2e67c 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2e67d 67 2e 70 63 61 63 68 65 20 3d 20 2a 76 61 5f 61  g.pcache = *va_a
2e67e 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 70  rg(ap, sqlite3_p
2e67f 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29 3b  cache_methods*);
2e680 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2e681 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53 51    }..    case SQ
2e682 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50  LITE_CONFIG_GETP
2e683 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20 20 69  CACHE: {.      i
2e684 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
2e685 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49  Config.pcache.xI
2e686 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  nit==0 ){.      
2e687 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53    sqlite3PCacheS
2e688 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 20  etDefault();.   
2e689 20 20 20 7d 0a 20 20 20 20 20 20 2a 76 61 5f 61     }.      *va_a
2e68a 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 70  rg(ap, sqlite3_p
2e68b 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29 20  cache_methods*) 
2e68c 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
2e68d 6f 6e 66 69 67 2e 70 63 61 63 68 65 3b 0a 20 20  onfig.pcache;.  
2e68e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2e68f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
2e690 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
2e691 59 53 33 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  YS3) || defined(
2e692 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
2e693 4d 53 59 53 35 29 0a 20 20 20 20 63 61 73 65 20  MSYS5).    case 
2e694 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45  SQLITE_CONFIG_HE
2e695 41 50 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44  AP: {.      /* D
2e696 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65  esignate a buffe
2e697 72 20 66 6f 72 20 68 65 61 70 20 6d 65 6d 6f 72  r for heap memor
2e698 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20  y space */.     
2e699 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2e69a 6e 66 69 67 2e 70 48 65 61 70 20 3d 20 76 61 5f  nfig.pHeap = va_
2e69b 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a  arg(ap, void*);.
2e69c 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
2e69d 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 20  balConfig.nHeap 
2e69e 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
2e69f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2e6a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52  GlobalConfig.mnR
2e6a1 65 71 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  eq = va_arg(ap, 
2e6a2 69 6e 74 29 3b 0a 0a 20 20 20 20 20 20 69 66 28  int);..      if(
2e6a3 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2e6a4 6e 66 69 67 2e 70 48 65 61 70 3d 3d 30 20 29 7b  nfig.pHeap==0 ){
2e6a5 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74  .        /* If t
2e6a6 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65 72 20  he heap pointer 
2e6a7 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65  is NULL, then re
2e6a8 73 74 6f 72 65 20 74 68 65 20 6d 61 6c 6c 6f 63  store the malloc
2e6a9 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
2e6aa 20 20 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20          ** back 
2e6ab 74 6f 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73  to NULL pointers
2e6ac 20 74 6f 6f 2e 20 20 54 68 69 73 20 77 69 6c 6c   too.  This will
2e6ad 20 63 61 75 73 65 20 74 68 65 20 6d 61 6c 6c 6f   cause the mallo
2e6ae 63 20 74 6f 20 67 6f 0a 20 20 20 20 20 20 20 20  c to go.        
2e6af 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20 64  ** back to its d
2e6b0 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74  efault implement
2e6b1 61 74 69 6f 6e 20 77 68 65 6e 20 73 71 6c 69 74  ation when sqlit
2e6b2 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
2e6b3 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 75  is.        ** ru
2e6b4 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  n..        */.  
2e6b5 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 73 71        memset(&sq
2e6b6 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2e6b7 67 2e 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  g.m, 0, sizeof(s
2e6b8 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2e6b9 69 67 2e 6d 29 29 3b 0a 20 20 20 20 20 20 7d 65  ig.m));.      }e
2e6ba 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
2e6bb 54 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65 72  The heap pointer
2e6bc 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
2e6bd 65 6e 20 69 6e 73 74 61 6c 6c 20 6f 6e 65 20 6f  en install one o
2e6be 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  f the.        **
2e6bf 20 6d 65 6d 35 2e 63 2f 6d 65 6d 33 2e 63 20 6d   mem5.c/mem3.c m
2e6c0 65 74 68 6f 64 73 2e 20 49 66 20 6e 65 69 74 68  ethods. If neith
2e6c1 65 72 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  er ENABLE_MEMSYS
2e6c2 33 20 6e 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a  3 nor.        **
2e6c3 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20   ENABLE_MEMSYS5 
2e6c4 69 73 20 64 65 66 69 6e 65 64 2c 20 72 65 74 75  is defined, retu
2e6c5 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20  rn an error..   
2e6c6 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61       ** the defa
2e6c7 75 6c 74 20 63 61 73 65 20 61 6e 64 20 72 65 74  ult case and ret
2e6c8 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  urn an error..  
2e6c9 20 20 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20        */.#ifdef 
2e6ca 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
2e6cb 4d 53 59 53 33 0a 20 20 20 20 20 20 20 20 73 71  MSYS3.        sq
2e6cc 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2e6cd 67 2e 6d 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65  g.m = *sqlite3Me
2e6ce 6d 47 65 74 4d 65 6d 73 79 73 33 28 29 3b 0a 23  mGetMemsys3();.#
2e6cf 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
2e6d0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
2e6d1 53 35 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  S5.        sqlit
2e6d2 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
2e6d3 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65   = *sqlite3MemGe
2e6d4 74 4d 65 6d 73 79 73 35 28 29 3b 0a 23 65 6e 64  tMemsys5();.#end
2e6d5 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  if.      }.     
2e6d6 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
2e6d7 6e 64 69 66 0a 0a 20 20 20 20 63 61 73 65 20 53  ndif..    case S
2e6d8 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f  QLITE_CONFIG_LOO
2e6d9 4b 41 53 49 44 45 3a 20 7b 0a 20 20 20 20 20 20  KASIDE: {.      
2e6da 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2e6db 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 20  fig.szLookaside 
2e6dc 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
2e6dd 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2e6de 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f  GlobalConfig.nLo
2e6df 6f 6b 61 73 69 64 65 20 3d 20 76 61 5f 61 72 67  okaside = va_arg
2e6e0 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
2e6e1 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
2e6e2 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
2e6e3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2e6e4 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65  ERROR;.      bre
2e6e5 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
2e6e6 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65  va_end(ap);.  re
2e6e7 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2e6e8 2a 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 6f  * Set up the loo
2e6e9 6b 61 73 69 64 65 20 62 75 66 66 65 72 73 20 66  kaside buffers f
2e6ea 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  or a database co
2e6eb 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 20 52 65 74  nnection..** Ret
2e6ec 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  urn SQLITE_OK on
2e6ed 20 73 75 63 63 65 73 73 2e 20 20 0a 2a 2a 20 49   success.  .** I
2e6ee 66 20 6c 6f 6f 6b 61 73 69 64 65 20 69 73 20 61  f lookaside is a
2e6ef 6c 72 65 61 64 79 20 61 63 74 69 76 65 2c 20 72  lready active, r
2e6f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
2e6f1 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 20  Y..**.** The sz 
2e6f2 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
2e6f3 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
2e6f4 20 69 6e 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69   in each lookasi
2e6f5 64 65 20 73 6c 6f 74 2e 0a 2a 2a 20 54 68 65 20  de slot..** The 
2e6f6 63 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  cnt parameter is
2e6f7 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
2e6f8 6c 6f 74 73 2e 20 20 49 66 20 70 53 74 61 72 74  lots.  If pStart
2e6f9 20 69 73 20 4e 55 4c 4c 20 74 68 65 0a 2a 2a 20   is NULL the.** 
2e6fa 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 6c 6f  space for the lo
2e6fb 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 69  okaside memory i
2e6fc 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
2e6fd 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
2e6fe 2e 0a 2a 2a 20 49 66 20 70 53 74 61 72 74 20 69  ..** If pStart i
2e6ff 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20  s not NULL then 
2e700 69 74 20 69 73 20 73 7a 2a 63 6e 74 20 62 79 74  it is sz*cnt byt
2e701 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20  es of memory to 
2e702 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 6c  use for.** the l
2e703 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e  ookaside memory.
2e704 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2e705 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 73 71  etupLookaside(sq
2e706 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
2e707 2a 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69  *pBuf, int sz, i
2e708 6e 74 20 63 6e 74 29 7b 0a 20 20 76 6f 69 64 20  nt cnt){.  void 
2e709 2a 70 53 74 61 72 74 3b 0a 20 20 69 66 28 20 64  *pStart;.  if( d
2e70a 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75  b->lookaside.nOu
2e70b 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
2e70c 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
2e70d 0a 20 20 69 66 28 20 73 7a 3c 30 20 29 20 73 7a  .  if( sz<0 ) sz
2e70e 20 3d 20 30 3b 0a 20 20 69 66 28 20 63 6e 74 3c   = 0;.  if( cnt<
2e70f 30 20 29 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69  0 ) cnt = 0;.  i
2e710 66 28 20 70 42 75 66 3d 3d 30 20 29 7b 0a 20 20  f( pBuf==0 ){.  
2e711 20 20 73 7a 20 3d 20 28 73 7a 20 2b 20 37 29 26    sz = (sz + 7)&
2e712 7e 37 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  ~7;.    sqlite3B
2e713 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
2e714 28 29 3b 0a 20 20 20 20 70 53 74 61 72 74 20 3d  ();.    pStart =
2e715 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20   sqlite3Malloc( 
2e716 73 7a 2a 63 6e 74 20 29 3b 0a 20 20 20 20 73 71  sz*cnt );.    sq
2e717 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
2e718 6c 6c 6f 63 28 29 3b 0a 20 20 7d 65 6c 73 65 7b  lloc();.  }else{
2e719 0a 20 20 20 20 73 7a 20 3d 20 73 7a 26 7e 37 3b  .    sz = sz&~7;
2e71a 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20 70 42  .    pStart = pB
2e71b 75 66 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  uf;.  }.  if( db
2e71c 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c  ->lookaside.bMal
2e71d 6c 6f 63 65 64 20 29 7b 0a 20 20 20 20 73 71 6c  loced ){.    sql
2e71e 69 74 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f  ite3_free(db->lo
2e71f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74 29 3b  okaside.pStart);
2e720 0a 20 20 7d 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61  .  }.  db->looka
2e721 73 69 64 65 2e 70 53 74 61 72 74 20 3d 20 70 53  side.pStart = pS
2e722 74 61 72 74 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b  tart;.  db->look
2e723 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 30 3b  aside.pFree = 0;
2e724 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  .  db->lookaside
2e725 2e 73 7a 20 3d 20 28 75 31 36 29 73 7a 3b 0a 20  .sz = (u16)sz;. 
2e726 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
2e727 4d 61 6c 6c 6f 63 65 64 20 3d 20 70 42 75 66 3d  Malloced = pBuf=
2e728 3d 30 20 3f 31 3a 30 3b 0a 20 20 69 66 28 20 70  =0 ?1:0;.  if( p
2e729 53 74 61 72 74 20 29 7b 0a 20 20 20 20 69 6e 74  Start ){.    int
2e72a 20 69 3b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64   i;.    Lookasid
2e72b 65 53 6c 6f 74 20 2a 70 3b 0a 20 20 20 20 70 20  eSlot *p;.    p 
2e72c 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74  = (LookasideSlot
2e72d 2a 29 70 53 74 61 72 74 3b 0a 20 20 20 20 66 6f  *)pStart;.    fo
2e72e 72 28 69 3d 63 6e 74 2d 31 3b 20 69 3e 3d 30 3b  r(i=cnt-1; i>=0;
2e72f 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e   i--){.      p->
2e730 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b  pNext = db->look
2e731 61 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20  aside.pFree;.   
2e732 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
2e733 2e 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20  .pFree = p;.    
2e734 20 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65    p = (Lookaside
2e735 53 6c 6f 74 2a 29 26 28 28 75 38 2a 29 70 29 5b  Slot*)&((u8*)p)[
2e736 73 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  sz];.    }.    d
2e737 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e  b->lookaside.pEn
2e738 64 20 3d 20 70 3b 0a 20 20 20 20 64 62 2d 3e 6c  d = p;.    db->l
2e739 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
2e73a 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  d = 1;.  }else{.
2e73b 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
2e73c 65 2e 70 45 6e 64 20 3d 20 30 3b 0a 20 20 20 20  e.pEnd = 0;.    
2e73d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
2e73e 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20 7d 0a  nabled = 0;.  }.
2e73f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2e740 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  OK;.}../*.** Ret
2e741 75 72 6e 20 74 68 65 20 6d 75 74 65 78 20 61 73  urn the mutex as
2e742 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
2e743 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2e744 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ion..*/.SQLITE_A
2e745 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  PI sqlite3_mutex
2e746 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74   *sqlite3_db_mut
2e747 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ex(sqlite3 *db){
2e748 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6d 75  .  return db->mu
2e749 74 65 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  tex;.}../*.** Co
2e74a 6e 66 69 67 75 72 61 74 69 6f 6e 20 73 65 74 74  nfiguration sett
2e74b 69 6e 67 73 20 66 6f 72 20 61 6e 20 69 6e 64 69  ings for an indi
2e74c 76 69 64 75 61 6c 20 64 61 74 61 62 61 73 65 20  vidual database 
2e74d 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51  connection.*/.SQ
2e74e 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2e74f 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73  ite3_db_config(s
2e750 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2e751 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  op, ...){.  va_l
2e752 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 72 63  ist ap;.  int rc
2e753 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
2e754 20 6f 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20   op);.  switch( 
2e755 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  op ){.    case S
2e756 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c  QLITE_DBCONFIG_L
2e757 4f 4f 4b 41 53 49 44 45 3a 20 7b 0a 20 20 20 20  OOKASIDE: {.    
2e758 20 20 76 6f 69 64 20 2a 70 42 75 66 20 3d 20 76    void *pBuf = v
2e759 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29  a_arg(ap, void*)
2e75a 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 20 3d  ;.      int sz =
2e75b 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
2e75c 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20  ;.      int cnt 
2e75d 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
2e75e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65  );.      rc = se
2e75f 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c  tupLookaside(db,
2e760 20 70 42 75 66 2c 20 73 7a 2c 20 63 6e 74 29 3b   pBuf, sz, cnt);
2e761 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2e762 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
2e763 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51   {.      rc = SQ
2e764 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
2e765 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2e766 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b   }.  va_end(ap);
2e767 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2e768 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
2e769 72 75 65 20 69 66 20 74 68 65 20 62 75 66 66 65  rue if the buffe
2e76a 72 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 63 6f 6e 74  r z[0..n-1] cont
2e76b 61 69 6e 73 20 61 6c 6c 20 73 70 61 63 65 73 2e  ains all spaces.
2e76c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
2e76d 6c 6c 53 70 61 63 65 73 28 63 6f 6e 73 74 20 63  llSpaces(const c
2e76e 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a  har *z, int n){.
2e76f 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 26 26 20    while( n>0 && 
2e770 7a 5b 6e 2d 31 5d 3d 3d 27 20 27 20 29 7b 20 6e  z[n-1]==' ' ){ n
2e771 2d 2d 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  --; }.  return n
2e772 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ==0;.}../*.** Th
2e773 69 73 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  is is the defaul
2e774 74 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  t collating func
2e775 74 69 6f 6e 20 6e 61 6d 65 64 20 22 42 49 4e 41  tion named "BINA
2e776 52 59 22 20 77 68 69 63 68 20 69 73 20 61 6c 77  RY" which is alw
2e777 61 79 73 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65  ays.** available
2e778 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
2e779 61 64 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20  adFlag argument 
2e77a 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e  is not NULL then
2e77b 20 73 70 61 63 65 20 70 61 64 64 69 6e 67 20 61   space padding a
2e77c 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20  t the end.** of 
2e77d 73 74 72 69 6e 67 73 20 69 73 20 69 67 6e 6f 72  strings is ignor
2e77e 65 64 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d  ed.  This implem
2e77f 65 6e 74 73 20 74 68 65 20 52 54 52 49 4d 20 63  ents the RTRIM c
2e780 6f 6c 6c 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  ollation..*/.sta
2e781 74 69 63 20 69 6e 74 20 62 69 6e 43 6f 6c 6c 46  tic int binCollF
2e782 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a 70 61 64  unc(.  void *pad
2e783 46 6c 61 67 2c 0a 20 20 69 6e 74 20 6e 4b 65 79  Flag,.  int nKey
2e784 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  1, const void *p
2e785 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65 79  Key1,.  int nKey
2e786 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  2, const void *p
2e787 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72 63  Key2.){.  int rc
2e788 2c 20 6e 3b 0a 20 20 6e 20 3d 20 6e 4b 65 79 31  , n;.  n = nKey1
2e789 3c 6e 4b 65 79 32 20 3f 20 6e 4b 65 79 31 20 3a  <nKey2 ? nKey1 :
2e78a 20 6e 4b 65 79 32 3b 0a 20 20 72 63 20 3d 20 6d   nKey2;.  rc = m
2e78b 65 6d 63 6d 70 28 70 4b 65 79 31 2c 20 70 4b 65  emcmp(pKey1, pKe
2e78c 79 32 2c 20 6e 29 3b 0a 20 20 69 66 28 20 72 63  y2, n);.  if( rc
2e78d 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
2e78e 61 64 46 6c 61 67 0a 20 20 20 20 20 26 26 20 61  adFlag.     && a
2e78f 6c 6c 53 70 61 63 65 73 28 28 28 63 68 61 72 2a  llSpaces(((char*
2e790 29 70 4b 65 79 31 29 2b 6e 2c 20 6e 4b 65 79 31  )pKey1)+n, nKey1
2e791 2d 6e 29 0a 20 20 20 20 20 26 26 20 61 6c 6c 53  -n).     && allS
2e792 70 61 63 65 73 28 28 28 63 68 61 72 2a 29 70 4b  paces(((char*)pK
2e793 65 79 32 29 2b 6e 2c 20 6e 4b 65 79 32 2d 6e 29  ey2)+n, nKey2-n)
2e794 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a  .    ){.      /*
2e795 20 4c 65 61 76 65 20 72 63 20 75 6e 63 68 61 6e   Leave rc unchan
2e796 67 65 64 20 61 74 20 30 20 2a 2f 0a 20 20 20 20  ged at 0 */.    
2e797 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
2e798 3d 20 6e 4b 65 79 31 20 2d 20 6e 4b 65 79 32 3b  = nKey1 - nKey2;
2e799 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2e79a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2e79b 20 41 6e 6f 74 68 65 72 20 62 75 69 6c 74 2d 69   Another built-i
2e79c 6e 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  n collating sequ
2e79d 65 6e 63 65 3a 20 4e 4f 43 41 53 45 2e 20 0a 2a  ence: NOCASE. .*
2e79e 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6c 6c 61 74  *.** This collat
2e79f 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20  ing sequence is 
2e7a0 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75  intended to be u
2e7a1 73 65 64 20 66 6f 72 20 22 63 61 73 65 20 69 6e  sed for "case in
2e7a2 64 65 70 65 6e 64 61 6e 74 0a 2a 2a 20 63 6f 6d  dependant.** com
2e7a3 70 61 72 69 73 6f 6e 22 2e 20 53 51 4c 69 74 65  parison". SQLite
2e7a4 27 73 20 6b 6e 6f 77 6c 65 64 67 65 20 6f 66 20  's knowledge of 
2e7a5 75 70 70 65 72 20 61 6e 64 20 6c 6f 77 65 72 20  upper and lower 
2e7a6 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e 74 73  case equivalents
2e7a7 0a 2a 2a 20 65 78 74 65 6e 64 73 20 6f 6e 6c 79  .** extends only
2e7a8 20 74 6f 20 74 68 65 20 32 36 20 63 68 61 72 61   to the 26 chara
2e7a9 63 74 65 72 73 20 75 73 65 64 20 69 6e 20 74 68  cters used in th
2e7aa 65 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61  e English langua
2e7ab 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65  ge..**.** At the
2e7ac 20 6d 6f 6d 65 6e 74 20 74 68 65 72 65 20 69 73   moment there is
2e7ad 20 6f 6e 6c 79 20 61 20 55 54 46 2d 38 20 69 6d   only a UTF-8 im
2e7ae 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f  plementation..*/
2e7af 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 63 61  .static int noca
2e7b0 73 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e 63 28  seCollatingFunc(
2e7b1 0a 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  .  void *NotUsed
2e7b2 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63  ,.  int nKey1, c
2e7b3 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
2e7b4 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c 20 63  ,.  int nKey2, c
2e7b5 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
2e7b6 0a 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 73 71  .){.  int r = sq
2e7b7 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 0a 20  lite3StrNICmp(. 
2e7b8 20 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72       (const char
2e7b9 20 2a 29 70 4b 65 79 31 2c 20 28 63 6f 6e 73 74   *)pKey1, (const
2e7ba 20 63 68 61 72 20 2a 29 70 4b 65 79 32 2c 20 28   char *)pKey2, (
2e7bb 6e 4b 65 79 31 3c 6e 4b 65 79 32 29 3f 6e 4b 65  nKey1<nKey2)?nKe
2e7bc 79 31 3a 6e 4b 65 79 32 29 3b 0a 20 20 55 4e 55  y1:nKey2);.  UNU
2e7bd 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
2e7be 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 30 3d  tUsed);.  if( 0=
2e7bf 3d 72 20 29 7b 0a 20 20 20 20 72 20 3d 20 6e 4b  =r ){.    r = nK
2e7c0 65 79 31 2d 6e 4b 65 79 32 3b 0a 20 20 7d 0a 20  ey1-nKey2;.  }. 
2e7c1 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a   return r;.}../*
2e7c2 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 52  .** Return the R
2e7c3 4f 57 49 44 20 6f 66 20 74 68 65 20 6d 6f 73 74  OWID of the most
2e7c4 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 0a 2a   recent insert.*
2e7c5 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
2e7c6 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  ite_int64 sqlite
2e7c7 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
2e7c8 77 69 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29  wid(sqlite3 *db)
2e7c9 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c  {.  return db->l
2e7ca 61 73 74 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a  astRowid;.}../*.
2e7cb 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
2e7cc 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20  mber of changes 
2e7cd 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  in the most rece
2e7ce 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  nt call to sqlit
2e7cf 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2f 0a 53 51  e3_exec()..*/.SQ
2e7d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2e7d1 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c  ite3_changes(sql
2e7d2 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74  ite3 *db){.  ret
2e7d3 75 72 6e 20 64 62 2d 3e 6e 43 68 61 6e 67 65 3b  urn db->nChange;
2e7d4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2e7d5 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
2e7d6 68 61 6e 67 65 73 20 73 69 6e 63 65 20 74 68 65  hanges since the
2e7d7 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
2e7d8 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a   was opened..*/.
2e7d9 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2e7da 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
2e7db 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62  nges(sqlite3 *db
2e7dc 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e  ){.  return db->
2e7dd 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 7d 0a  nTotalChange;.}.
2e7de 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c  ./*.** Close all
2e7df 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73   open savepoints
2e7e0 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
2e7e1 6f 6e 6c 79 20 6d 61 6e 69 70 75 6c 61 74 65 73  only manipulates
2e7e2 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 0a 2a   fields of the.*
2e7e3 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * database handl
2e7e4 65 20 6f 62 6a 65 63 74 2c 20 69 74 20 64 6f 65  e object, it doe
2e7e5 73 20 6e 6f 74 20 63 6c 6f 73 65 20 61 6e 79 20  s not close any 
2e7e6 73 61 76 65 70 6f 69 6e 74 73 20 74 68 61 74 20  savepoints that 
2e7e7 6d 61 79 20 62 65 20 6f 70 65 6e 0a 2a 2a 20 61  may be open.** a
2e7e8 74 20 74 68 65 20 62 2d 74 72 65 65 2f 70 61 67  t the b-tree/pag
2e7e9 65 72 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 53 51 4c  er level..*/.SQL
2e7ea 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2e7eb 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76   sqlite3CloseSav
2e7ec 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20  epoints(sqlite3 
2e7ed 2a 64 62 29 7b 0a 20 20 77 68 69 6c 65 28 20 64  *db){.  while( d
2e7ee 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 7b  b->pSavepoint ){
2e7ef 0a 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a  .    Savepoint *
2e7f0 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65  pTmp = db->pSave
2e7f1 70 6f 69 6e 74 3b 0a 20 20 20 20 64 62 2d 3e 70  point;.    db->p
2e7f2 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 54 6d 70  Savepoint = pTmp
2e7f3 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c  ->pNext;.    sql
2e7f4 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2e7f5 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  Tmp);.  }.  db->
2e7f6 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a  nSavepoint = 0;.
2e7f7 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74    db->isTransact
2e7f8 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30  ionSavepoint = 0
2e7f9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
2e7fa 20 61 6e 20 65 78 69 73 74 69 6e 67 20 53 51 4c   an existing SQL
2e7fb 69 74 65 20 64 61 74 61 62 61 73 65 0a 2a 2f 0a  ite database.*/.
2e7fc 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2e7fd 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c  qlite3_close(sql
2e7fe 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 48 61 73  ite3 *db){.  Has
2e7ff 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 6e 74 20  hElem *i;.  int 
2e800 6a 3b 0a 0a 20 20 69 66 28 20 21 64 62 20 29 7b  j;..  if( !db ){
2e801 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2e802 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
2e803 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43   !sqlite3SafetyC
2e804 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29  heckSickOrOk(db)
2e805 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2e806 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
2e807 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
2e808 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
2e809 78 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  x);..#ifdef SQLI
2e80a 54 45 5f 53 53 45 0a 20 20 7b 0a 20 20 20 20 65  TE_SSE.  {.    e
2e80b 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74  xtern void sqlit
2e80c 65 33 53 73 65 43 6c 65 61 6e 75 70 28 73 71 6c  e3SseCleanup(sql
2e80d 69 74 65 33 2a 29 3b 0a 20 20 20 20 73 71 6c 69  ite3*);.    sqli
2e80e 74 65 33 53 73 65 43 6c 65 61 6e 75 70 28 64 62  te3SseCleanup(db
2e80f 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 0a 0a  );.  }.#endif ..
2e810 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
2e811 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
2e812 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20   0);..  /* If a 
2e813 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
2e814 70 65 6e 2c 20 74 68 65 20 52 65 73 65 74 49 6e  pen, the ResetIn
2e815 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 29 20 63  ternalSchema() c
2e816 61 6c 6c 20 61 62 6f 76 65 0a 20 20 2a 2a 20 77  all above.  ** w
2e817 69 6c 6c 20 6e 6f 74 20 68 61 76 65 20 63 61 6c  ill not have cal
2e818 6c 65 64 20 74 68 65 20 78 44 69 73 63 6f 6e 6e  led the xDisconn
2e819 65 63 74 28 29 20 6d 65 74 68 6f 64 20 6f 6e 20  ect() method on 
2e81a 61 6e 79 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a  any virtual.  **
2e81b 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64   tables in the d
2e81c 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20 61 72 72  b->aVTrans[] arr
2e81d 61 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ay. The followin
2e81e 67 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c  g sqlite3VtabRol
2e81f 6c 62 61 63 6b 28 29 0a 20 20 2a 2a 20 63 61 6c  lback().  ** cal
2e820 6c 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 20 57 65  l will do so. We
2e821 20 6e 65 65 64 20 74 6f 20 64 6f 20 74 68 69 73   need to do this
2e822 20 62 65 66 6f 72 65 20 74 68 65 20 63 68 65 63   before the chec
2e823 6b 20 66 6f 72 20 61 63 74 69 76 65 0a 20 20 2a  k for active.  *
2e824 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  * SQL statements
2e825 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 76   below, as the v
2e826 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  -table implement
2e827 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 73 74 6f  ation may be sto
2e828 72 69 6e 67 0a 20 20 2a 2a 20 73 6f 6d 65 20 70  ring.  ** some p
2e829 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2e82a 74 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20  ts internally.. 
2e82b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 74 61   */.  sqlite3Vta
2e82c 62 52 6f 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 0a  bRollback(db);..
2e82d 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
2e82e 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e  e any outstandin
2e82f 67 20 56 4d 73 2c 20 72 65 74 75 72 6e 20 53 51  g VMs, return SQ
2e830 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20  LITE_BUSY. */.  
2e831 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 7b  if( db->pVdbe ){
2e832 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2e833 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53  r(db, SQLITE_BUS
2e834 59 2c 20 0a 20 20 20 20 20 20 20 20 22 55 6e 61  Y, .        "Una
2e835 62 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64 75 65  ble to close due
2e836 20 74 6f 20 75 6e 66 69 6e 61 6c 69 73 65 64 20   to unfinalised 
2e837 73 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20  statements");.  
2e838 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2e839 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
2e83a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
2e83b 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
2e83c 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
2e83d 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72  afetyCheckSickOr
2e83e 4f 6b 28 64 62 29 20 29 3b 0a 0a 20 20 2f 2a 20  Ok(db) );..  /* 
2e83f 46 72 65 65 20 61 6e 79 20 6f 75 74 73 74 61 6e  Free any outstan
2e840 64 69 6e 67 20 53 61 76 65 70 6f 69 6e 74 20 73  ding Savepoint s
2e841 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 20 20  tructures. */.  
2e842 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65  sqlite3CloseSave
2e843 70 6f 69 6e 74 73 28 64 62 29 3b 0a 0a 20 20 66  points(db);..  f
2e844 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44  or(j=0; j<db->nD
2e845 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 73 74 72  b; j++){.    str
2e846 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64  uct Db *pDb = &d
2e847 62 2d 3e 61 44 62 5b 6a 5d 3b 0a 20 20 20 20 69  b->aDb[j];.    i
2e848 66 28 20 70 44 62 2d 3e 70 42 74 20 29 7b 0a 20  f( pDb->pBt ){. 
2e849 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
2e84a 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29  eClose(pDb->pBt)
2e84b 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 42 74  ;.      pDb->pBt
2e84c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
2e84d 6a 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  j!=1 ){.        
2e84e 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30  pDb->pSchema = 0
2e84f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2e850 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 73    }.  sqlite3Res
2e851 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2e852 28 64 62 2c 20 30 29 3b 0a 20 20 61 73 73 65 72  (db, 0);.  asser
2e853 74 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 29 3b  t( db->nDb<=2 );
2e854 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61  .  assert( db->a
2e855 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db==db->aDbStati
2e856 63 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20  c );.  for(j=0; 
2e857 6a 3c 41 72 72 61 79 53 69 7a 65 28 64 62 2d 3e  j<ArraySize(db->
2e858 61 46 75 6e 63 2e 61 29 3b 20 6a 2b 2b 29 7b 0a  aFunc.a); j++){.
2e859 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65      FuncDef *pNe
2e85a 78 74 2c 20 2a 70 48 61 73 68 2c 20 2a 70 3b 0a  xt, *pHash, *p;.
2e85b 20 20 20 20 66 6f 72 28 70 3d 64 62 2d 3e 61 46      for(p=db->aF
2e85c 75 6e 63 2e 61 5b 6a 5d 3b 20 70 3b 20 70 3d 70  unc.a[j]; p; p=p
2e85d 48 61 73 68 29 7b 0a 20 20 20 20 20 20 70 48 61  Hash){.      pHa
2e85e 73 68 20 3d 20 70 2d 3e 70 48 61 73 68 3b 0a 20  sh = p->pHash;. 
2e85f 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b       while( p ){
2e860 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20 3d  .        pNext =
2e861 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20   p->pNext;.     
2e862 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2e863 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20  (db, p);.       
2e864 20 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20 20 20   p = pNext;.    
2e865 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
2e866 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
2e867 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c  First(&db->aColl
2e868 53 65 71 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74  Seq); i; i=sqlit
2e869 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
2e86a 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
2e86b 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73  l = (CollSeq *)s
2e86c 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
2e86d 3b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ;.    /* Invoke 
2e86e 61 6e 79 20 64 65 73 74 72 75 63 74 6f 72 73 20  any destructors 
2e86f 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 63  registered for c
2e870 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2e871 65 20 75 73 65 72 20 64 61 74 61 2e 20 2a 2f 0a  e user data. */.
2e872 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33      for(j=0; j<3
2e873 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
2e874 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 20  ( pColl[j].xDel 
2e875 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  ){.        pColl
2e876 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c 6c 5b 6a  [j].xDel(pColl[j
2e877 5d 2e 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20  ].pUser);.      
2e878 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
2e879 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
2e87a 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  oll);.  }.  sqli
2e87b 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 64 62  te3HashClear(&db
2e87c 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66  ->aCollSeq);.#if
2e87d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2e87e 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
2e87f 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
2e880 46 69 72 73 74 28 26 64 62 2d 3e 61 4d 6f 64 75  First(&db->aModu
2e881 6c 65 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65  le); i; i=sqlite
2e882 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20  HashNext(i)){.  
2e883 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20 3d    Module *pMod =
2e884 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74   (Module *)sqlit
2e885 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20  eHashData(i);.  
2e886 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 44 65 73    if( pMod->xDes
2e887 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 4d  troy ){.      pM
2e888 6f 64 2d 3e 78 44 65 73 74 72 6f 79 28 70 4d 6f  od->xDestroy(pMo
2e889 64 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a  d->pAux);.    }.
2e88a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2e88b 65 28 64 62 2c 20 70 4d 6f 64 29 3b 0a 20 20 7d  e(db, pMod);.  }
2e88c 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  .  sqlite3HashCl
2e88d 65 61 72 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  ear(&db->aModule
2e88e 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c  );.#endif..  sql
2e88f 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
2e890 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 20 2f 2a 20  LITE_OK, 0); /* 
2e891 44 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20  Deallocates any 
2e892 63 61 63 68 65 64 20 65 72 72 6f 72 20 73 74 72  cached error str
2e893 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 66 28 20 64  ings. */.  if( d
2e894 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20 73  b->pErr ){.    s
2e895 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
2e896 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 7d 0a 20  db->pErr);.  }. 
2e897 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74   sqlite3CloseExt
2e898 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20  ensions(db);..  
2e899 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
2e89a 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a  TE_MAGIC_ERROR;.
2e89b 0a 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 2d 64  .  /* The temp-d
2e89c 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69  atabase schema i
2e89d 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 69 66 66  s allocated diff
2e89e 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65  erently from the
2e89f 20 6f 74 68 65 72 20 73 63 68 65 6d 61 0a 20 20   other schema.  
2e8a0 2a 2a 20 6f 62 6a 65 63 74 73 20 28 75 73 69 6e  ** objects (usin
2e8a1 67 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  g sqliteMalloc()
2e8a2 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74 65   directly, inste
2e8a3 61 64 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72  ad of sqlite3Btr
2e8a4 65 65 53 63 68 65 6d 61 28 29 29 2e 0a 20 20 2a  eeSchema())..  *
2e8a5 2a 20 53 6f 20 69 74 20 6e 65 65 64 73 20 74 6f  * So it needs to
2e8a6 20 62 65 20 66 72 65 65 64 20 68 65 72 65 2e 20   be freed here. 
2e8a7 54 6f 64 6f 3a 20 57 68 79 20 6e 6f 74 20 72 6f  Todo: Why not ro
2e8a8 6c 6c 20 74 68 65 20 74 65 6d 70 20 73 63 68 65  ll the temp sche
2e8a9 6d 61 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  ma into.  ** the
2e8aa 20 73 61 6d 65 20 73 71 6c 69 74 65 4d 61 6c 6c   same sqliteMall
2e8ab 6f 63 28 29 20 61 73 20 74 68 65 20 6f 6e 65 20  oc() as the one 
2e8ac 74 68 61 74 20 61 6c 6c 6f 63 61 74 65 73 20 74  that allocates t
2e8ad 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 2a  he database .  *
2e8ae 2a 20 73 74 72 75 63 74 75 72 65 3f 0a 20 20 2a  * structure?.  *
2e8af 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  /.  sqlite3DbFre
2e8b0 65 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 31 5d  e(db, db->aDb[1]
2e8b1 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c  .pSchema);.  sql
2e8b2 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2e8b3 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
2e8b4 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
2e8b5 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b 0a  E_MAGIC_CLOSED;.
2e8b6 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2e8b7 66 72 65 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  free(db->mutex);
2e8b8 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6c  .  assert( db->l
2e8b9 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3d 3d 30  ookaside.nOut==0
2e8ba 20 29 3b 20 20 2f 2a 20 46 61 69 6c 73 20 6f 6e   );  /* Fails on
2e8bb 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d   a lookaside mem
2e8bc 6f 72 79 20 6c 65 61 6b 20 2a 2f 0a 20 20 69 66  ory leak */.  if
2e8bd 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
2e8be 62 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a 20 20 20  bMalloced ){.   
2e8bf 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62   sqlite3_free(db
2e8c0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61  ->lookaside.pSta
2e8c1 72 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  rt);.  }.  sqlit
2e8c2 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20 20 72  e3_free(db);.  r
2e8c3 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2e8c4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61  .}../*.** Rollba
2e8c5 63 6b 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20  ck all database 
2e8c6 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  files..*/.SQLITE
2e8c7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2e8c8 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c  lite3RollbackAll
2e8c9 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2e8ca 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 6e   int i;.  int in
2e8cb 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 61 73 73  Trans = 0;.  ass
2e8cc 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
2e8cd 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
2e8ce 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  x) );.  sqlite3B
2e8cf 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
2e8d0 28 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ();.  for(i=0; i
2e8d1 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
2e8d2 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
2e8d3 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20 20  i].pBt ){.      
2e8d4 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  if( sqlite3Btree
2e8d5 49 73 49 6e 54 72 61 6e 73 28 64 62 2d 3e 61 44  IsInTrans(db->aD
2e8d6 62 5b 69 5d 2e 70 42 74 29 20 29 7b 0a 20 20 20  b[i].pBt) ){.   
2e8d7 20 20 20 20 20 69 6e 54 72 61 6e 73 20 3d 20 31       inTrans = 1
2e8d8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2e8d9 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
2e8da 62 61 63 6b 28 64 62 2d 3e 61 44 62 5b 69 5d 2e  back(db->aDb[i].
2e8db 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  pBt);.      db->
2e8dc 61 44 62 5b 69 5d 2e 69 6e 54 72 61 6e 73 20 3d  aDb[i].inTrans =
2e8dd 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
2e8de 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62  sqlite3VtabRollb
2e8df 61 63 6b 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  ack(db);.  sqlit
2e8e0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
2e8e1 63 28 29 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e  c();..  if( db->
2e8e2 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
2e8e3 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20  ernChanges ){.  
2e8e4 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50    sqlite3ExpireP
2e8e5 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
2e8e6 73 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74  s(db);.    sqlit
2e8e7 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
2e8e8 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
2e8e9 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 68  }..  /* If one h
2e8ea 61 73 20 62 65 65 6e 20 63 6f 6e 66 69 67 75 72  as been configur
2e8eb 65 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 72  ed, invoke the r
2e8ec 6f 6c 6c 62 61 63 6b 2d 68 6f 6f 6b 20 63 61 6c  ollback-hook cal
2e8ed 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 64  lback */.  if( d
2e8ee 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c  b->xRollbackCall
2e8ef 62 61 63 6b 20 26 26 20 28 69 6e 54 72 61 6e 73  back && (inTrans
2e8f0 20 7c 7c 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d   || !db->autoCom
2e8f1 6d 69 74 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e  mit) ){.    db->
2e8f2 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
2e8f3 6b 28 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41  k(db->pRollbackA
2e8f4 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  rg);.  }.}../*.*
2e8f5 2a 20 52 65 74 75 72 6e 20 61 20 73 74 61 74 69  * Return a stati
2e8f6 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65  c string that de
2e8f7 73 63 72 69 62 65 73 20 74 68 65 20 6b 69 6e 64  scribes the kind
2e8f8 20 6f 66 20 65 72 72 6f 72 20 73 70 65 63 69 66   of error specif
2e8f9 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72  ied in the.** ar
2e8fa 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
2e8fb 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
2e8fc 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72  char *sqlite3Err
2e8fd 53 74 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 63  Str(int rc){.  c
2e8fe 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20  onst char *z;.  
2e8ff 73 77 69 74 63 68 28 20 72 63 20 26 20 30 78 66  switch( rc & 0xf
2e900 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  f ){.    case SQ
2e901 4c 49 54 45 5f 52 4f 57 3a 0a 20 20 20 20 63 61  LITE_ROW:.    ca
2e902 73 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a  se SQLITE_DONE:.
2e903 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e904 4f 4b 3a 20 20 20 20 20 20 20 20 20 7a 20 3d 20  OK:         z = 
2e905 22 6e 6f 74 20 61 6e 20 65 72 72 6f 72 22 3b 20  "not an error"; 
2e906 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e907 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e908 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e909 45 52 52 4f 52 3a 20 20 20 20 20 20 7a 20 3d 20  ERROR:      z = 
2e90a 22 53 51 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72  "SQL logic error
2e90b 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61   or missing data
2e90c 62 61 73 65 22 3b 20 20 20 62 72 65 61 6b 3b 0a  base";   break;.
2e90d 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e90e 50 45 52 4d 3a 20 20 20 20 20 20 20 7a 20 3d 20  PERM:       z = 
2e90f 22 61 63 63 65 73 73 20 70 65 72 6d 69 73 73 69  "access permissi
2e910 6f 6e 20 64 65 6e 69 65 64 22 3b 20 20 20 20 20  on denied";     
2e911 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e912 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e913 41 42 4f 52 54 3a 20 20 20 20 20 20 7a 20 3d 20  ABORT:      z = 
2e914 22 63 61 6c 6c 62 61 63 6b 20 72 65 71 75 65 73  "callback reques
2e915 74 65 64 20 71 75 65 72 79 20 61 62 6f 72 74 22  ted query abort"
2e916 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;        break;.
2e917 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e918 42 55 53 59 3a 20 20 20 20 20 20 20 7a 20 3d 20  BUSY:       z = 
2e919 22 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63  "database is loc
2e91a 6b 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20  ked";           
2e91b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e91c 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e91d 4c 4f 43 4b 45 44 3a 20 20 20 20 20 7a 20 3d 20  LOCKED:     z = 
2e91e 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20  "database table 
2e91f 69 73 20 6c 6f 63 6b 65 64 22 3b 20 20 20 20 20  is locked";     
2e920 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e921 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e922 4e 4f 4d 45 4d 3a 20 20 20 20 20 20 7a 20 3d 20  NOMEM:      z = 
2e923 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 3b  "out of memory";
2e924 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e925 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e926 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e927 52 45 41 44 4f 4e 4c 59 3a 20 20 20 7a 20 3d 20  READONLY:   z = 
2e928 22 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74  "attempt to writ
2e929 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74  e a readonly dat
2e92a 61 62 61 73 65 22 3b 20 20 62 72 65 61 6b 3b 0a  abase";  break;.
2e92b 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e92c 49 4e 54 45 52 52 55 50 54 3a 20 20 7a 20 3d 20  INTERRUPT:  z = 
2e92d 22 69 6e 74 65 72 72 75 70 74 65 64 22 3b 20 20  "interrupted";  
2e92e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e92f 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e930 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e931 49 4f 45 52 52 3a 20 20 20 20 20 20 7a 20 3d 20  IOERR:      z = 
2e932 22 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 22  "disk I/O error"
2e933 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2e934 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e935 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e936 43 4f 52 52 55 50 54 3a 20 20 20 20 7a 20 3d 20  CORRUPT:    z = 
2e937 22 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69  "database disk i
2e938 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65  mage is malforme
2e939 64 22 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  d";      break;.
2e93a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e93b 46 55 4c 4c 3a 20 20 20 20 20 20 20 7a 20 3d 20  FULL:       z = 
2e93c 22 64 61 74 61 62 61 73 65 20 6f 72 20 64 69 73  "database or dis
2e93d 6b 20 69 73 20 66 75 6c 6c 22 3b 20 20 20 20 20  k is full";     
2e93e 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e93f 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e940 43 41 4e 54 4f 50 45 4e 3a 20 20 20 7a 20 3d 20  CANTOPEN:   z = 
2e941 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  "unable to open 
2e942 64 61 74 61 62 61 73 65 20 66 69 6c 65 22 3b 20  database file"; 
2e943 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e944 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e945 45 4d 50 54 59 3a 20 20 20 20 20 20 7a 20 3d 20  EMPTY:      z = 
2e946 22 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20  "table contains 
2e947 6e 6f 20 64 61 74 61 22 3b 20 20 20 20 20 20 20  no data";       
2e948 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e949 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e94a 53 43 48 45 4d 41 3a 20 20 20 20 20 7a 20 3d 20  SCHEMA:     z = 
2e94b 22 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61  "database schema
2e94c 20 68 61 73 20 63 68 61 6e 67 65 64 22 3b 20 20   has changed";  
2e94d 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e94e 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e94f 54 4f 4f 42 49 47 3a 20 20 20 20 20 7a 20 3d 20  TOOBIG:     z = 
2e950 22 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20  "String or BLOB 
2e951 65 78 63 65 65 64 65 64 20 73 69 7a 65 20 6c 69  exceeded size li
2e952 6d 69 74 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a  mit";    break;.
2e953 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e954 43 4f 4e 53 54 52 41 49 4e 54 3a 20 7a 20 3d 20  CONSTRAINT: z = 
2e955 22 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c  "constraint fail
2e956 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed";            
2e957 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e958 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e959 4d 49 53 4d 41 54 43 48 3a 20 20 20 7a 20 3d 20  MISMATCH:   z = 
2e95a 22 64 61 74 61 74 79 70 65 20 6d 69 73 6d 61 74  "datatype mismat
2e95b 63 68 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  ch";            
2e95c 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e95d 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e95e 4d 49 53 55 53 45 3a 20 20 20 20 20 7a 20 3d 20  MISUSE:     z = 
2e95f 22 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65  "library routine
2e960 20 63 61 6c 6c 65 64 20 6f 75 74 20 6f 66 20 73   called out of s
2e961 65 71 75 65 6e 63 65 22 3b 62 72 65 61 6b 3b 0a  equence";break;.
2e962 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e963 4e 4f 4c 46 53 3a 20 20 20 20 20 20 7a 20 3d 20  NOLFS:      z = 
2e964 22 6c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70  "large file supp
2e965 6f 72 74 20 69 73 20 64 69 73 61 62 6c 65 64 22  ort is disabled"
2e966 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;        break;.
2e967 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e968 41 55 54 48 3a 20 20 20 20 20 20 20 7a 20 3d 20  AUTH:       z = 
2e969 22 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64  "authorization d
2e96a 65 6e 69 65 64 22 3b 20 20 20 20 20 20 20 20 20  enied";         
2e96b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e96c 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e96d 46 4f 52 4d 41 54 3a 20 20 20 20 20 7a 20 3d 20  FORMAT:     z = 
2e96e 22 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62  "auxiliary datab
2e96f 61 73 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72  ase format error
2e970 22 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ";       break;.
2e971 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e972 52 41 4e 47 45 3a 20 20 20 20 20 20 7a 20 3d 20  RANGE:      z = 
2e973 22 62 69 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e 20  "bind or column 
2e974 69 6e 64 65 78 20 6f 75 74 20 6f 66 20 72 61 6e  index out of ran
2e975 67 65 22 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a  ge";     break;.
2e976 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2e977 4e 4f 54 41 44 42 3a 20 20 20 20 20 7a 20 3d 20  NOTADB:     z = 
2e978 22 66 69 6c 65 20 69 73 20 65 6e 63 72 79 70 74  "file is encrypt
2e979 65 64 20 6f 72 20 69 73 20 6e 6f 74 20 61 20 64  ed or is not a d
2e97a 61 74 61 62 61 73 65 22 3b 62 72 65 61 6b 3b 0a  atabase";break;.
2e97b 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20      default:    
2e97c 20 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20              z = 
2e97d 22 75 6e 6b 6e 6f 77 6e 20 65 72 72 6f 72 22 3b  "unknown error";
2e97e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e97f 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e980 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a    }.  return z;.
2e981 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2e982 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73  utine implements
2e983 20 61 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b   a busy callback
2e984 20 74 68 61 74 20 73 6c 65 65 70 73 20 61 6e 64   that sleeps and
2e985 20 74 72 69 65 73 0a 2a 2a 20 61 67 61 69 6e 20   tries.** again 
2e986 75 6e 74 69 6c 20 61 20 74 69 6d 65 6f 75 74 20  until a timeout 
2e987 76 61 6c 75 65 20 69 73 20 72 65 61 63 68 65 64  value is reached
2e988 2e 20 20 54 68 65 20 74 69 6d 65 6f 75 74 20 76  .  The timeout v
2e989 61 6c 75 65 20 69 73 0a 2a 2a 20 61 6e 20 69 6e  alue is.** an in
2e98a 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20  teger number of 
2e98b 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 70 61 73  milliseconds pas
2e98c 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 66 69  sed in as the fi
2e98d 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e  rst.** argument.
2e98e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2e98f 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73 79  qliteDefaultBusy
2e990 43 61 6c 6c 62 61 63 6b 28 0a 20 76 6f 69 64 20  Callback(. void 
2e991 2a 70 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  *ptr,           
2e992 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2e993 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 69  connection */. i
2e994 6e 74 20 63 6f 75 6e 74 20 20 20 20 20 20 20 20  nt count        
2e995 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2e996 72 20 6f 66 20 74 69 6d 65 73 20 74 61 62 6c 65  r of times table
2e997 20 68 61 73 20 62 65 65 6e 20 62 75 73 79 20 2a   has been busy *
2e998 2f 0a 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  /.){.#if SQLITE_
2e999 4f 53 5f 57 49 4e 20 7c 7c 20 28 64 65 66 69 6e  OS_WIN || (defin
2e99a 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20  ed(HAVE_USLEEP) 
2e99b 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50 29 0a  && HAVE_USLEEP).
2e99c 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2e99d 38 20 64 65 6c 61 79 73 5b 5d 20 3d 0a 20 20 20  8 delays[] =.   
2e99e 20 20 7b 20 31 2c 20 32 2c 20 35 2c 20 31 30 2c    { 1, 2, 5, 10,
2e99f 20 31 35 2c 20 32 30 2c 20 32 35 2c 20 32 35 2c   15, 20, 25, 25,
2e9a0 20 20 32 35 2c 20 20 35 30 2c 20 20 35 30 2c 20    25,  50,  50, 
2e9a1 31 30 30 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  100 };.  static 
2e9a2 63 6f 6e 73 74 20 75 38 20 74 6f 74 61 6c 73 5b  const u8 totals[
2e9a3 5d 20 3d 0a 20 20 20 20 20 7b 20 30 2c 20 31 2c  ] =.     { 0, 1,
2e9a4 20 33 2c 20 20 38 2c 20 31 38 2c 20 33 33 2c 20   3,  8, 18, 33, 
2e9a5 35 33 2c 20 37 38 2c 20 31 30 33 2c 20 31 32 38  53, 78, 103, 128
2e9a6 2c 20 31 37 38 2c 20 32 32 38 20 7d 3b 0a 23 20  , 178, 228 };.# 
2e9a7 64 65 66 69 6e 65 20 4e 44 45 4c 41 59 20 28 73  define NDELAY (s
2e9a8 69 7a 65 6f 66 28 64 65 6c 61 79 73 29 2f 73 69  izeof(delays)/si
2e9a9 7a 65 6f 66 28 64 65 6c 61 79 73 5b 30 5d 29 29  zeof(delays[0]))
2e9aa 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2e9ab 20 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72 3b   (sqlite3 *)ptr;
2e9ac 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d  .  int timeout =
2e9ad 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74   db->busyTimeout
2e9ae 3b 0a 20 20 69 6e 74 20 64 65 6c 61 79 2c 20 70  ;.  int delay, p
2e9af 72 69 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  rior;..  assert(
2e9b0 20 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 69   count>=0 );.  i
2e9b1 66 28 20 63 6f 75 6e 74 20 3c 20 4e 44 45 4c 41  f( count < NDELA
2e9b2 59 20 29 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d  Y ){.    delay =
2e9b3 20 64 65 6c 61 79 73 5b 63 6f 75 6e 74 5d 3b 0a   delays[count];.
2e9b4 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74 61      prior = tota
2e9b5 6c 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 7d 65 6c  ls[count];.  }el
2e9b6 73 65 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20  se{.    delay = 
2e9b7 64 65 6c 61 79 73 5b 4e 44 45 4c 41 59 2d 31 5d  delays[NDELAY-1]
2e9b8 3b 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f  ;.    prior = to
2e9b9 74 61 6c 73 5b 4e 44 45 4c 41 59 2d 31 5d 20 2b  tals[NDELAY-1] +
2e9ba 20 64 65 6c 61 79 2a 28 63 6f 75 6e 74 2d 28 4e   delay*(count-(N
2e9bb 44 45 4c 41 59 2d 31 29 29 3b 0a 20 20 7d 0a 20  DELAY-1));.  }. 
2e9bc 20 69 66 28 20 70 72 69 6f 72 20 2b 20 64 65 6c   if( prior + del
2e9bd 61 79 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a  ay > timeout ){.
2e9be 20 20 20 20 64 65 6c 61 79 20 3d 20 74 69 6d 65      delay = time
2e9bf 6f 75 74 20 2d 20 70 72 69 6f 72 3b 0a 20 20 20  out - prior;.   
2e9c0 20 69 66 28 20 64 65 6c 61 79 3c 3d 30 20 29 20   if( delay<=0 ) 
2e9c1 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2e9c2 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64  sqlite3OsSleep(d
2e9c3 62 2d 3e 70 56 66 73 2c 20 64 65 6c 61 79 2a 31  b->pVfs, delay*1
2e9c4 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 31  000);.  return 1
2e9c5 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65  ;.#else.  sqlite
2e9c6 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33  3 *db = (sqlite3
2e9c7 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74 69   *)ptr;.  int ti
2e9c8 6d 65 6f 75 74 20 3d 20 28 28 73 71 6c 69 74 65  meout = ((sqlite
2e9c9 33 20 2a 29 70 74 72 29 2d 3e 62 75 73 79 54 69  3 *)ptr)->busyTi
2e9ca 6d 65 6f 75 74 3b 0a 20 20 69 66 28 20 28 63 6f  meout;.  if( (co
2e9cb 75 6e 74 2b 31 29 2a 31 30 30 30 20 3e 20 74 69  unt+1)*1000 > ti
2e9cc 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20 72 65 74  meout ){.    ret
2e9cd 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  urn 0;.  }.  sql
2e9ce 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e  ite3OsSleep(db->
2e9cf 70 56 66 73 2c 20 31 30 30 30 30 30 30 29 3b 0a  pVfs, 1000000);.
2e9d0 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64    return 1;.#end
2e9d1 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  if.}../*.** Invo
2e9d2 6b 65 20 74 68 65 20 67 69 76 65 6e 20 62 75 73  ke the given bus
2e9d3 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a  y handler..**.**
2e9d4 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2e9d5 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20   called when an 
2e9d6 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 65 64  operation failed
2e9d7 20 77 69 74 68 20 61 20 6c 6f 63 6b 2e 0a 2a 2a   with a lock..**
2e9d8 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
2e9d9 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
2e9da 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 72  o, the lock is r
2e9db 65 74 72 69 65 64 2e 20 20 49 66 20 69 74 0a 2a  etried.  If it.*
2e9dc 2a 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68 65  * returns 0, the
2e9dd 20 6f 70 65 72 61 74 69 6f 6e 20 61 62 6f 72 74   operation abort
2e9de 73 20 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45  s with an SQLITE
2e9df 5f 42 55 53 59 20 65 72 72 6f 72 2e 0a 2a 2f 0a  _BUSY error..*/.
2e9e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2e9e1 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65  nt sqlite3Invoke
2e9e2 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79  BusyHandler(Busy
2e9e3 48 61 6e 64 6c 65 72 20 2a 70 29 7b 0a 20 20 69  Handler *p){.  i
2e9e4 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 4e 45 56  nt rc;.  if( NEV
2e9e5 45 52 28 70 3d 3d 30 29 20 7c 7c 20 70 2d 3e 78  ER(p==0) || p->x
2e9e6 46 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 42  Func==0 || p->nB
2e9e7 75 73 79 3c 30 20 29 20 72 65 74 75 72 6e 20 30  usy<0 ) return 0
2e9e8 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46 75 6e  ;.  rc = p->xFun
2e9e9 63 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e 6e 42  c(p->pArg, p->nB
2e9ea 75 73 79 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  usy);.  if( rc==
2e9eb 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73  0 ){.    p->nBus
2e9ec 79 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b  y = -1;.  }else{
2e9ed 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79 2b 2b 3b  .    p->nBusy++;
2e9ee 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
2e9ef 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ; .}../*.** This
2e9f0 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
2e9f1 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  e busy callback 
2e9f2 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61  for an Sqlite da
2e9f3 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a  tabase to the.**
2e9f4 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20   given callback 
2e9f5 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
2e9f6 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74  e given argument
2e9f7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2e9f8 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
2e9f9 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69  _handler(.  sqli
2e9fa 74 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28  te3 *db,.  int (
2e9fb 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 2c 69 6e  *xBusy)(void*,in
2e9fc 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  t),.  void *pArg
2e9fd 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  .){.  sqlite3_mu
2e9fe 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2e9ff 74 65 78 29 3b 0a 20 20 64 62 2d 3e 62 75 73 79  tex);.  db->busy
2ea00 48 61 6e 64 6c 65 72 2e 78 46 75 6e 63 20 3d 20  Handler.xFunc = 
2ea01 78 42 75 73 79 3b 0a 20 20 64 62 2d 3e 62 75 73  xBusy;.  db->bus
2ea02 79 48 61 6e 64 6c 65 72 2e 70 41 72 67 20 3d 20  yHandler.pArg = 
2ea03 70 41 72 67 3b 0a 20 20 64 62 2d 3e 62 75 73 79  pArg;.  db->busy
2ea04 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20  Handler.nBusy = 
2ea05 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  0;.  sqlite3_mut
2ea06 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2ea07 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
2ea08 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e  LITE_OK;.}..#ifn
2ea09 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2ea0a 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
2ea0b 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  K./*.** This rou
2ea0c 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 72  tine sets the pr
2ea0d 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
2ea0e 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61  for an Sqlite da
2ea0f 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a  tabase to the.**
2ea10 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20   given callback 
2ea11 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
2ea12 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74  e given argument
2ea13 2e 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63  . The progress c
2ea14 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 0a 2a 2a 20  allback will.** 
2ea15 62 65 20 69 6e 76 6f 6b 65 64 20 65 76 65 72 79  be invoked every
2ea16 20 6e 4f 70 73 20 6f 70 63 6f 64 65 73 2e 0a 2a   nOps opcodes..*
2ea17 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
2ea18 64 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65  d sqlite3_progre
2ea19 73 73 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71  ss_handler(.  sq
2ea1a 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 69 6e  lite3 *db, .  in
2ea1b 74 20 6e 4f 70 73 2c 0a 20 20 69 6e 74 20 28 2a  t nOps,.  int (*
2ea1c 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 2a  xProgress)(void*
2ea1d 29 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  ), .  void *pArg
2ea1e 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  .){.  sqlite3_mu
2ea1f 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2ea20 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 4f 70 73  tex);.  if( nOps
2ea21 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 78 50  >0 ){.    db->xP
2ea22 72 6f 67 72 65 73 73 20 3d 20 78 50 72 6f 67 72  rogress = xProgr
2ea23 65 73 73 3b 0a 20 20 20 20 64 62 2d 3e 6e 50 72  ess;.    db->nPr
2ea24 6f 67 72 65 73 73 4f 70 73 20 3d 20 6e 4f 70 73  ogressOps = nOps
2ea25 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f 67 72  ;.    db->pProgr
2ea26 65 73 73 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  essArg = pArg;. 
2ea27 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e   }else{.    db->
2ea28 78 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a 20  xProgress = 0;. 
2ea29 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73     db->nProgress
2ea2a 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  Ops = 0;.    db-
2ea2b 3e 70 50 72 6f 67 72 65 73 73 41 72 67 20 3d 20  >pProgressArg = 
2ea2c 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
2ea2d 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
2ea2e 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69  >mutex);.}.#endi
2ea2f 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  f.../*.** This r
2ea30 6f 75 74 69 6e 65 20 69 6e 73 74 61 6c 6c 73 20  outine installs 
2ea31 61 20 64 65 66 61 75 6c 74 20 62 75 73 79 20 68  a default busy h
2ea32 61 6e 64 6c 65 72 20 74 68 61 74 20 77 61 69 74  andler that wait
2ea33 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73 70 65  s for the.** spe
2ea34 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66  cified number of
2ea35 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 62 65   milliseconds be
2ea36 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 30  fore returning 0
2ea37 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2ea38 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
2ea39 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33  _timeout(sqlite3
2ea3a 20 2a 64 62 2c 20 69 6e 74 20 6d 73 29 7b 0a 20   *db, int ms){. 
2ea3b 20 69 66 28 20 6d 73 3e 30 20 29 7b 0a 20 20 20   if( ms>0 ){.   
2ea3c 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74   db->busyTimeout
2ea3d 20 3d 20 6d 73 3b 0a 20 20 20 20 73 71 6c 69 74   = ms;.    sqlit
2ea3e 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28  e3_busy_handler(
2ea3f 64 62 2c 20 73 71 6c 69 74 65 44 65 66 61 75 6c  db, sqliteDefaul
2ea40 74 42 75 73 79 43 61 6c 6c 62 61 63 6b 2c 20 28  tBusyCallback, (
2ea41 76 6f 69 64 2a 29 64 62 29 3b 0a 20 20 7d 65 6c  void*)db);.  }el
2ea42 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
2ea43 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c  busy_handler(db,
2ea44 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65   0, 0);.  }.  re
2ea45 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2ea46 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 75 73 65 20 61  }../*.** Cause a
2ea47 6e 79 20 70 65 6e 64 69 6e 67 20 6f 70 65 72 61  ny pending opera
2ea48 74 69 6f 6e 20 74 6f 20 73 74 6f 70 20 61 74 20  tion to stop at 
2ea49 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70  its earliest opp
2ea4a 6f 72 74 75 6e 69 74 79 2e 0a 2a 2f 0a 53 51 4c  ortunity..*/.SQL
2ea4b 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2ea4c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 73  ite3_interrupt(s
2ea4d 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64  qlite3 *db){.  d
2ea4e 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
2ea4f 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a  ted = 1;.}.../*.
2ea50 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2ea51 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 20   is exactly the 
2ea52 73 61 6d 65 20 61 73 20 73 71 6c 69 74 65 33 5f  same as sqlite3_
2ea53 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
2ea54 29 2c 20 65 78 63 65 70 74 0a 2a 2a 20 74 68 61  ), except.** tha
2ea55 74 20 69 74 20 69 73 20 64 65 73 69 67 6e 65 64  t it is designed
2ea56 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79   to be called by
2ea57 20 69 6e 74 65 72 6e 61 6c 20 63 6f 64 65 2e 20   internal code. 
2ea58 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 69  The difference i
2ea59 73 0a 2a 2a 20 74 68 61 74 20 69 66 20 61 20 6d  s.** that if a m
2ea5a 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 69 6e  alloc() fails in
2ea5b 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2ea5c 66 75 6e 63 74 69 6f 6e 28 29 2c 20 61 6e 20 65  function(), an e
2ea5d 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73 20  rror code.** is 
2ea5e 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
2ea5f 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c   mallocFailed fl
2ea60 61 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2f 0a  ag cleared. .*/.
2ea61 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2ea62 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  nt sqlite3Create
2ea63 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 20  Func(.  sqlite3 
2ea64 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  *db,.  const cha
2ea65 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  r *zFunctionName
2ea66 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20  ,.  int nArg,.  
2ea67 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69 64 20  int enc,.  void 
2ea68 2a 70 55 73 65 72 44 61 74 61 2c 0a 20 20 76 6f  *pUserData,.  vo
2ea69 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
2ea6a 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
2ea6b 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
2ea6c 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74  *),.  void (*xSt
2ea6d 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
2ea6e 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
2ea6f 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f  _value **),.  vo
2ea70 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
2ea71 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29  ite3_context*).)
2ea72 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a  {.  FuncDef *p;.
2ea73 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20    int nName;..  
2ea74 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
2ea75 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
2ea76 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 7a  utex) );.  if( z
2ea77 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 3d 3d 30 20  FunctionName==0 
2ea78 7c 7c 0a 20 20 20 20 20 20 28 78 46 75 6e 63 20  ||.      (xFunc 
2ea79 26 26 20 28 78 46 69 6e 61 6c 20 7c 7c 20 78 53  && (xFinal || xS
2ea7a 74 65 70 29 29 20 7c 7c 20 0a 20 20 20 20 20 20  tep)) || .      
2ea7b 28 21 78 46 75 6e 63 20 26 26 20 28 78 46 69 6e  (!xFunc && (xFin
2ea7c 61 6c 20 26 26 20 21 78 53 74 65 70 29 29 20 7c  al && !xStep)) |
2ea7d 7c 0a 20 20 20 20 20 20 28 21 78 46 75 6e 63 20  |.      (!xFunc 
2ea7e 26 26 20 28 21 78 46 69 6e 61 6c 20 26 26 20 78  && (!xFinal && x
2ea7f 53 74 65 70 29 29 20 7c 7c 0a 20 20 20 20 20 20  Step)) ||.      
2ea80 28 6e 41 72 67 3c 2d 31 20 7c 7c 20 6e 41 72 67  (nArg<-1 || nArg
2ea81 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43  >SQLITE_MAX_FUNC
2ea82 54 49 4f 4e 5f 41 52 47 29 20 7c 7c 0a 20 20 20  TION_ARG) ||.   
2ea83 20 20 20 28 32 35 35 3c 28 6e 4e 61 6d 65 20 3d     (255<(nName =
2ea84 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64   sqlite3Strlen(d
2ea85 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  b, zFunctionName
2ea86 29 29 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ))) ){.    sqlit
2ea87 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
2ea88 54 45 5f 45 52 52 4f 52 2c 20 22 62 61 64 20 70  TE_ERROR, "bad p
2ea89 61 72 61 6d 65 74 65 72 73 22 29 3b 0a 20 20 20  arameters");.   
2ea8a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2ea8b 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 0a 23 69 66  RROR;.  }.  .#if
2ea8c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2ea8d 5f 55 54 46 31 36 0a 20 20 2f 2a 20 49 66 20 53  _UTF16.  /* If S
2ea8e 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 73  QLITE_UTF16 is s
2ea8f 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20  pecified as the 
2ea90 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c 20 74  encoding type, t
2ea91 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a 20 20  ransform this.  
2ea92 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53 51 4c  ** to one of SQL
2ea93 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53  ITE_UTF16LE or S
2ea94 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 75 73  QLITE_UTF16BE us
2ea95 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c  ing the.  ** SQL
2ea96 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
2ea97 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f 55 54  macro. SQLITE_UT
2ea98 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65 64 20  F16 is not used 
2ea99 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2a  internally..  **
2ea9a 0a 20 20 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  .  ** If SQLITE_
2ea9b 41 4e 59 20 69 73 20 73 70 65 63 69 66 69 65 64  ANY is specified
2ea9c 2c 20 61 64 64 20 74 68 72 65 65 20 76 65 72 73  , add three vers
2ea9d 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 75 6e 63  ions of the func
2ea9e 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 65  tion.  ** to the
2ea9f 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 20 20 2a   hash table..  *
2eaa0 2f 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c  /.  if( enc==SQL
2eaa1 49 54 45 5f 55 54 46 31 36 20 29 7b 0a 20 20 20  ITE_UTF16 ){.   
2eaa2 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54   enc = SQLITE_UT
2eaa3 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 65 6c  F16NATIVE;.  }el
2eaa4 73 65 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49  se if( enc==SQLI
2eaa5 54 45 5f 41 4e 59 20 29 7b 0a 20 20 20 20 69 6e  TE_ANY ){.    in
2eaa6 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73  t rc;.    rc = s
2eaa7 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
2eaa8 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
2eaa9 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45  me, nArg, SQLITE
2eaaa 5f 55 54 46 38 2c 0a 20 20 20 20 20 20 20 20 20  _UTF8,.         
2eaab 70 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e 63  pUserData, xFunc
2eaac 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29  , xStep, xFinal)
2eaad 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
2eaae 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
2eaaf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65   rc = sqlite3Cre
2eab0 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e  ateFunc(db, zFun
2eab1 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c  ctionName, nArg,
2eab2 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c   SQLITE_UTF16LE,
2eab3 0a 20 20 20 20 20 20 20 20 20 20 70 55 73 65 72  .          pUser
2eab4 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78 53 74  Data, xFunc, xSt
2eab5 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20 20  ep, xFinal);.   
2eab6 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
2eab7 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2eab8 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
2eab9 20 7d 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c   }.    enc = SQL
2eaba 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 7d  ITE_UTF16BE;.  }
2eabb 0a 23 65 6c 73 65 0a 20 20 65 6e 63 20 3d 20 53  .#else.  enc = S
2eabc 51 4c 49 54 45 5f 55 54 46 38 3b 0a 23 65 6e 64  QLITE_UTF8;.#end
2eabd 69 66 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b  if.  .  /* Check
2eabe 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   if an existing 
2eabf 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e  function is bein
2eac0 67 20 6f 76 65 72 72 69 64 64 65 6e 20 6f 72 20  g overridden or 
2eac1 64 65 6c 65 74 65 64 2e 20 49 66 20 73 6f 2c 0a  deleted. If so,.
2eac2 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61    ** and there a
2eac3 72 65 20 61 63 74 69 76 65 20 56 4d 73 2c 20 74  re active VMs, t
2eac4 68 65 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54  hen return SQLIT
2eac5 45 5f 42 55 53 59 2e 20 49 66 20 61 20 66 75 6e  E_BUSY. If a fun
2eac6 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 62 65  ction.  ** is be
2eac7 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 2f 64  ing overridden/d
2eac8 65 6c 65 74 65 64 20 62 75 74 20 74 68 65 72 65  eleted but there
2eac9 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 56   are no active V
2eaca 4d 73 2c 20 61 6c 6c 6f 77 20 74 68 65 0a 20 20  Ms, allow the.  
2eacb 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  ** operation to 
2eacc 63 6f 6e 74 69 6e 75 65 20 62 75 74 20 69 6e 76  continue but inv
2eacd 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 63  alidate all prec
2eace 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e  ompiled statemen
2eacf 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 73  ts..  */.  p = s
2ead0 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
2ead1 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  on(db, zFunction
2ead2 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
2ead3 67 2c 20 28 75 38 29 65 6e 63 2c 20 30 29 3b 0a  g, (u8)enc, 0);.
2ead4 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 69 50    if( p && p->iP
2ead5 72 65 66 45 6e 63 3d 3d 65 6e 63 20 26 26 20 70  refEnc==enc && p
2ead6 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 29 7b 0a  ->nArg==nArg ){.
2ead7 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69      if( db->acti
2ead8 76 65 56 64 62 65 43 6e 74 20 29 7b 0a 20 20 20  veVdbeCnt ){.   
2ead9 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
2eada 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c  db, SQLITE_BUSY,
2eadb 20 0a 20 20 20 20 20 20 20 20 22 55 6e 61 62 6c   .        "Unabl
2eadc 65 20 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69  e to delete/modi
2eadd 66 79 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e  fy user-function
2eade 20 64 75 65 20 74 6f 20 61 63 74 69 76 65 20 73   due to active s
2eadf 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20  tatements");.   
2eae0 20 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e     assert( !db->
2eae1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
2eae2 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
2eae3 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65  ITE_BUSY;.    }e
2eae4 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
2eae5 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
2eae6 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
2eae7 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 20 3d      }.  }..  p =
2eae8 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
2eae9 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69  tion(db, zFuncti
2eaea 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e  onName, nName, n
2eaeb 41 72 67 2c 20 28 75 38 29 65 6e 63 2c 20 31 29  Arg, (u8)enc, 1)
2eaec 3b 0a 20 20 61 73 73 65 72 74 28 70 20 7c 7c 20  ;.  assert(p || 
2eaed 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2eaee 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  );.  if( !p ){. 
2eaef 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2eaf0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d  _NOMEM;.  }.  p-
2eaf1 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 70 2d  >flags = 0;.  p-
2eaf2 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b 0a  >xFunc = xFunc;.
2eaf3 20 20 70 2d 3e 78 53 74 65 70 20 3d 20 78 53 74    p->xStep = xSt
2eaf4 65 70 3b 0a 20 20 70 2d 3e 78 46 69 6e 61 6c 69  ep;.  p->xFinali
2eaf5 7a 65 20 3d 20 78 46 69 6e 61 6c 3b 0a 20 20 70  ze = xFinal;.  p
2eaf6 2d 3e 70 55 73 65 72 44 61 74 61 20 3d 20 70 55  ->pUserData = pU
2eaf7 73 65 72 44 61 74 61 3b 0a 20 20 70 2d 3e 6e 41  serData;.  p->nA
2eaf8 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b 0a  rg = (u16)nArg;.
2eaf9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2eafa 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  OK;.}../*.** Cre
2eafb 61 74 65 20 6e 65 77 20 75 73 65 72 20 66 75 6e  ate new user fun
2eafc 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ctions..*/.SQLIT
2eafd 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2eafe 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
2eaff 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
2eb00 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2eb01 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20  zFunctionName,. 
2eb02 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74   int nArg,.  int
2eb03 20 65 6e 63 2c 0a 20 20 76 6f 69 64 20 2a 70 2c   enc,.  void *p,
2eb04 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  .  void (*xFunc)
2eb05 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2eb06 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
2eb07 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20  lue **),.  void 
2eb08 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33  (*xStep)(sqlite3
2eb09 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
2eb0a 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c  lite3_value **),
2eb0b 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  .  void (*xFinal
2eb0c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
2eb0d 74 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  t*).){.  int rc;
2eb0e 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2eb0f 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2eb10 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
2eb11 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
2eb12 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e  zFunctionName, n
2eb13 41 72 67 2c 20 65 6e 63 2c 20 70 2c 20 78 46 75  Arg, enc, p, xFu
2eb14 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  nc, xStep, xFina
2eb15 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  l);.  rc = sqlit
2eb16 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
2eb17 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
2eb18 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2eb19 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
2eb1a 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
2eb1b 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
2eb1c 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2eb1d 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
2eb1e 63 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74  ction16(.  sqlit
2eb1f 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3 *db,.  const 
2eb20 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e  void *zFunctionN
2eb21 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c  ame,.  int nArg,
2eb22 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c  .  int eTextRep,
2eb23 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20 20 76 6f  .  void *p,.  vo
2eb24 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
2eb25 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
2eb26 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
2eb27 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65  ),.  void (*xSte
2eb28 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
2eb29 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
2eb2a 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64  value**),.  void
2eb2b 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74   (*xFinal)(sqlit
2eb2c 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a  e3_context*).){.
2eb2d 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72    int rc;.  char
2eb2e 20 2a 7a 46 75 6e 63 38 3b 0a 20 20 73 71 6c 69   *zFunc8;.  sqli
2eb2f 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2eb30 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
2eb31 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
2eb32 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a 46 75  cFailed );.  zFu
2eb33 6e 63 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66  nc8 = sqlite3Utf
2eb34 31 36 74 6f 38 28 64 62 2c 20 7a 46 75 6e 63 74  16to8(db, zFunct
2eb35 69 6f 6e 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20  ionName, -1);.  
2eb36 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  rc = sqlite3Crea
2eb37 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63  teFunc(db, zFunc
2eb38 38 2c 20 6e 41 72 67 2c 20 65 54 65 78 74 52 65  8, nArg, eTextRe
2eb39 70 2c 20 70 2c 20 78 46 75 6e 63 2c 20 78 53 74  p, p, xFunc, xSt
2eb3a 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20 73  ep, xFinal);.  s
2eb3b 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2eb3c 20 7a 46 75 6e 63 38 29 3b 0a 20 20 72 63 20 3d   zFunc8);.  rc =
2eb3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
2eb3e 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74  db, rc);.  sqlit
2eb3f 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2eb40 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
2eb41 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
2eb42 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 65  .../*.** Declare
2eb43 20 74 68 61 74 20 61 20 66 75 6e 63 74 69 6f 6e   that a function
2eb44 20 68 61 73 20 62 65 65 6e 20 6f 76 65 72 6c 6f   has been overlo
2eb45 61 64 65 64 20 62 79 20 61 20 76 69 72 74 75 61  aded by a virtua
2eb46 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  l table..**.** I
2eb47 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 61  f the function a
2eb48 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 73  lready exists as
2eb49 20 61 20 72 65 67 75 6c 61 72 20 67 6c 6f 62 61   a regular globa
2eb4a 6c 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e  l function, then
2eb4b 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
2eb4c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66   is a no-op.  If
2eb4d 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f   the function do
2eb4e 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 74 68  es not exist, th
2eb4f 65 6e 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e  en create.** a n
2eb50 65 77 20 6f 6e 65 20 74 68 61 74 20 61 6c 77 61  ew one that alwa
2eb51 79 73 20 74 68 72 6f 77 73 20 61 20 72 75 6e 2d  ys throws a run-
2eb52 74 69 6d 65 20 65 72 72 6f 72 2e 20 20 0a 2a 2a  time error.  .**
2eb53 0a 2a 2a 20 57 68 65 6e 20 76 69 72 74 75 61 6c  .** When virtual
2eb54 20 74 61 62 6c 65 73 20 69 6e 74 65 6e 64 20 74   tables intend t
2eb55 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 6f 76 65  o provide an ove
2eb56 72 6c 6f 61 64 65 64 20 66 75 6e 63 74 69 6f 6e  rloaded function
2eb57 2c 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75 6c 64  , they.** should
2eb58 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
2eb59 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ne to make sure 
2eb5a 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74  the global funct
2eb5b 69 6f 6e 20 65 78 69 73 74 73 2e 0a 2a 2a 20 41  ion exists..** A
2eb5c 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e   global function
2eb5d 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e 20 6f   must exist in o
2eb5e 72 64 65 72 20 66 6f 72 20 6e 61 6d 65 20 72 65  rder for name re
2eb5f 73 6f 6c 75 74 69 6f 6e 20 74 6f 20 77 6f 72 6b  solution to work
2eb60 0a 2a 2a 20 70 72 6f 70 65 72 6c 79 2e 0a 2a 2f  .** properly..*/
2eb61 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2eb62 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
2eb63 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c  _function(.  sql
2eb64 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73  ite3 *db,.  cons
2eb65 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20  t char *zName,. 
2eb66 20 69 6e 74 20 6e 41 72 67 0a 29 7b 0a 20 20 69   int nArg.){.  i
2eb67 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nt nName = sqlit
2eb68 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61  e3Strlen(db, zNa
2eb69 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  me);.  int rc;. 
2eb6a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2eb6b 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2eb6c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
2eb6d 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  ndFunction(db, z
2eb6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
2eb6f 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g, SQLITE_UTF8, 
2eb70 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  0)==0 ){.    sql
2eb71 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
2eb72 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  b, zName, nArg, 
2eb73 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20  SQLITE_UTF8,.   
2eb74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eb75 20 20 20 30 2c 20 73 71 6c 69 74 65 33 49 6e 76     0, sqlite3Inv
2eb76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 2c 20 30 2c  alidFunction, 0,
2eb77 20 30 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20   0);.  }.  rc = 
2eb78 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
2eb79 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20  b, SQLITE_OK);. 
2eb7a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2eb7b 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2eb7c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2eb7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2eb7e 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a  OMIT_TRACE./*.**
2eb7f 20 52 65 67 69 73 74 65 72 20 61 20 74 72 61 63   Register a trac
2eb80 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  e function.  The
2eb81 20 70 41 72 67 20 66 72 6f 6d 20 74 68 65 20 70   pArg from the p
2eb82 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74  reviously regist
2eb83 65 72 65 64 20 74 72 61 63 65 0a 2a 2a 20 69 73  ered trace.** is
2eb84 20 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a 2a 0a   returned.  .**.
2eb85 2a 2a 20 41 20 4e 55 4c 4c 20 74 72 61 63 65 20  ** A NULL trace 
2eb86 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e 73 20 74  function means t
2eb87 68 61 74 20 6e 6f 20 74 72 61 63 69 6e 67 20 69  hat no tracing i
2eb88 73 20 65 78 65 63 75 74 65 73 2e 20 20 41 20 6e  s executes.  A n
2eb89 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74 72 61 63 65  on-NULL.** trace
2eb8a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2eb8b 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74   a function that
2eb8c 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 20 74   is invoked at t
2eb8d 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68  he start of each
2eb8e 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  .** SQL statemen
2eb8f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
2eb90 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 74   void *sqlite3_t
2eb91 72 61 63 65 28 73 71 6c 69 74 65 33 20 2a 64 62  race(sqlite3 *db
2eb92 2c 20 76 6f 69 64 20 28 2a 78 54 72 61 63 65 29  , void (*xTrace)
2eb93 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
2eb94 72 2a 29 2c 20 76 6f 69 64 20 2a 70 41 72 67 29  r*), void *pArg)
2eb95 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a  {.  void *pOld;.
2eb96 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2eb97 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2eb98 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70  ;.  pOld = db->p
2eb99 54 72 61 63 65 41 72 67 3b 0a 20 20 64 62 2d 3e  TraceArg;.  db->
2eb9a 78 54 72 61 63 65 20 3d 20 78 54 72 61 63 65 3b  xTrace = xTrace;
2eb9b 0a 20 20 64 62 2d 3e 70 54 72 61 63 65 41 72 67  .  db->pTraceArg
2eb9c 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
2eb9d 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2eb9e 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
2eb9f 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 2f 2a 0a 2a  urn pOld;.}./*.*
2eba0 2a 20 52 65 67 69 73 74 65 72 20 61 20 70 72 6f  * Register a pro
2eba1 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20  file function.  
2eba2 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74 68  The pArg from th
2eba3 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  e previously reg
2eba4 69 73 74 65 72 65 64 20 0a 2a 2a 20 70 72 6f 66  istered .** prof
2eba5 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ile function is 
2eba6 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a 2a 0a 2a  returned.  .**.*
2eba7 2a 20 41 20 4e 55 4c 4c 20 70 72 6f 66 69 6c 65  * A NULL profile
2eba8 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e 73 20   function means 
2eba9 74 68 61 74 20 6e 6f 20 70 72 6f 66 69 6c 69 6e  that no profilin
2ebaa 67 20 69 73 20 65 78 65 63 75 74 65 73 2e 20 20  g is executes.  
2ebab 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 70 72  A non-NULL.** pr
2ebac 6f 66 69 6c 65 20 69 73 20 61 20 70 6f 69 6e 74  ofile is a point
2ebad 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
2ebae 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64   that is invoked
2ebaf 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   at the conclusi
2ebb0 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63 68 20 53 51  on of.** each SQ
2ebb1 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  L statement that
2ebb2 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49   is run..*/.SQLI
2ebb3 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
2ebb4 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 0a 20 20  ite3_profile(.  
2ebb5 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76  sqlite3 *db,.  v
2ebb6 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28  oid (*xProfile)(
2ebb7 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
2ebb8 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29  *,sqlite_uint64)
2ebb9 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29  ,.  void *pArg.)
2ebba 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a  {.  void *pOld;.
2ebbb 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2ebbc 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2ebbd 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70  ;.  pOld = db->p
2ebbe 50 72 6f 66 69 6c 65 41 72 67 3b 0a 20 20 64 62  ProfileArg;.  db
2ebbf 2d 3e 78 50 72 6f 66 69 6c 65 20 3d 20 78 50 72  ->xProfile = xPr
2ebc0 6f 66 69 6c 65 3b 0a 20 20 64 62 2d 3e 70 50 72  ofile;.  db->pPr
2ebc1 6f 66 69 6c 65 41 72 67 20 3d 20 70 41 72 67 3b  ofileArg = pArg;
2ebc2 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2ebc3 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
2ebc4 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64  );.  return pOld
2ebc5 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
2ebc6 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20  LITE_OMIT_TRACE 
2ebc7 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58 50 45 52 49 4d  */../*** EXPERIM
2ebc8 45 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  ENTAL ***.**.** 
2ebc9 52 65 67 69 73 74 65 72 20 61 20 66 75 6e 63 74  Register a funct
2ebca 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65  ion to be invoke
2ebcb 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63  d when a transac
2ebcc 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a  tion comments..*
2ebcd 2a 20 49 66 20 74 68 65 20 69 6e 76 6f 6b 65 64  * If the invoked
2ebce 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
2ebcf 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
2ebd0 20 74 68 65 20 63 6f 6d 6d 69 74 20 62 65 63 6f   the commit beco
2ebd1 6d 65 73 20 61 0a 2a 2a 20 72 6f 6c 6c 62 61 63  mes a.** rollbac
2ebd2 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  k..*/.SQLITE_API
2ebd3 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
2ebd4 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a 20 20 73 71  ommit_hook(.  sq
2ebd5 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2ebd6 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63          /* Attac
2ebd7 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68  h the hook to th
2ebd8 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  is database */. 
2ebd9 20 69 6e 74 20 28 2a 78 43 61 6c 6c 62 61 63 6b   int (*xCallback
2ebda 29 28 76 6f 69 64 2a 29 2c 20 20 2f 2a 20 46 75  )(void*),  /* Fu
2ebdb 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65  nction to invoke
2ebdc 20 6f 6e 20 65 61 63 68 20 63 6f 6d 6d 69 74 20   on each commit 
2ebdd 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20  */.  void *pArg 
2ebde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ebdf 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
2ebe0 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  e function */.){
2ebe1 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20  .  void *pOld;. 
2ebe2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2ebe3 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2ebe4 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70 43  .  pOld = db->pC
2ebe5 6f 6d 6d 69 74 41 72 67 3b 0a 20 20 64 62 2d 3e  ommitArg;.  db->
2ebe6 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20  xCommitCallback 
2ebe7 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64  = xCallback;.  d
2ebe8 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 20 3d 20  b->pCommitArg = 
2ebe9 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pArg;.  sqlite3_
2ebea 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2ebeb 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2ebec 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pOld;.}../*.** 
2ebed 52 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c 62  Register a callb
2ebee 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65  ack to be invoke
2ebef 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 72 6f  d each time a ro
2ebf0 77 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a  w is updated,.**
2ebf1 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c   inserted or del
2ebf2 65 74 65 64 20 75 73 69 6e 67 20 74 68 69 73 20  eted using this 
2ebf3 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2ebf4 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ion..*/.SQLITE_A
2ebf5 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
2ebf6 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20  _update_hook(.  
2ebf7 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2ebf8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74            /* Att
2ebf9 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20  ach the hook to 
2ebfa 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
2ebfb 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62  .  void (*xCallb
2ebfc 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  ack)(void*,int,c
2ebfd 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72  har const *,char
2ebfe 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 5f   const *,sqlite_
2ebff 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a  int64),.  void *
2ec00 70 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20  pArg            
2ec01 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
2ec02 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
2ec03 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52  */.){.  void *pR
2ec04 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  et;.  sqlite3_mu
2ec05 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2ec06 74 65 78 29 3b 0a 20 20 70 52 65 74 20 3d 20 64  tex);.  pRet = d
2ec07 62 2d 3e 70 55 70 64 61 74 65 41 72 67 3b 0a 20  b->pUpdateArg;. 
2ec08 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c   db->xUpdateCall
2ec09 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b  back = xCallback
2ec0a 3b 0a 20 20 64 62 2d 3e 70 55 70 64 61 74 65 41  ;.  db->pUpdateA
2ec0b 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c  rg = pArg;.  sql
2ec0c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2ec0d 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
2ec0e 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f  eturn pRet;.}../
2ec0f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
2ec10 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69  callback to be i
2ec11 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65  nvoked each time
2ec12 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
2ec13 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b  s rolled.** back
2ec14 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73   by this databas
2ec15 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
2ec16 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2ec17 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61   *sqlite3_rollba
2ec18 63 6b 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74  ck_hook(.  sqlit
2ec19 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2ec1a 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74       /* Attach t
2ec1b 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20  he hook to this 
2ec1c 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 76 6f  database */.  vo
2ec1d 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28  id (*xCallback)(
2ec1e 76 6f 69 64 2a 29 2c 20 2f 2a 20 43 61 6c 6c 62  void*), /* Callb
2ec1f 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ack function */.
2ec20 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20 20 20    void *pArg    
2ec21 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2ec22 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66  rgument to the f
2ec23 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
2ec24 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20 73 71  void *pRet;.  sq
2ec25 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
2ec26 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
2ec27 70 52 65 74 20 3d 20 64 62 2d 3e 70 52 6f 6c 6c  pRet = db->pRoll
2ec28 62 61 63 6b 41 72 67 3b 0a 20 20 64 62 2d 3e 78  backArg;.  db->x
2ec29 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b  RollbackCallback
2ec2a 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20   = xCallback;.  
2ec2b 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67  db->pRollbackArg
2ec2c 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
2ec2d 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2ec2e 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
2ec2f 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a  urn pRet;.}../*.
2ec30 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2ec31 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65  is called to cre
2ec32 61 74 65 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e  ate a connection
2ec33 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 42   to a database B
2ec34 54 72 65 65 0a 2a 2a 20 64 72 69 76 65 72 2e 20  Tree.** driver. 
2ec35 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73   If zFilename is
2ec36 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66   the name of a f
2ec37 69 6c 65 2c 20 74 68 65 6e 20 74 68 61 74 20 66  ile, then that f
2ec38 69 6c 65 20 69 73 0a 2a 2a 20 6f 70 65 6e 65 64  ile is.** opened
2ec39 20 61 6e 64 20 75 73 65 64 2e 20 20 49 66 20 7a   and used.  If z
2ec3a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20  Filename is the 
2ec3b 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d  magic name ":mem
2ec3c 6f 72 79 3a 22 20 74 68 65 6e 0a 2a 2a 20 74 68  ory:" then.** th
2ec3d 65 20 64 61 74 61 62 61 73 65 20 69 73 20 73 74  e database is st
2ec3e 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 28  ored in memory (
2ec3f 61 6e 64 20 69 73 20 74 68 75 73 20 66 6f 72 67  and is thus forg
2ec40 6f 74 74 65 6e 20 61 73 20 73 6f 6f 6e 20 61 73  otten as soon as
2ec41 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  .** the connecti
2ec42 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 29 20 20  on is closed.)  
2ec43 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  If zFilename is 
2ec44 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 61  NULL then the da
2ec45 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 61 20 22  tabase.** is a "
2ec46 76 69 72 74 75 61 6c 22 20 64 61 74 61 62 61 73  virtual" databas
2ec47 65 20 66 6f 72 20 74 72 61 6e 73 69 65 6e 74 20  e for transient 
2ec48 75 73 65 20 6f 6e 6c 79 20 61 6e 64 20 69 73 20  use only and is 
2ec49 64 65 6c 65 74 65 64 20 61 73 0a 2a 2a 20 73 6f  deleted as.** so
2ec4a 6f 6e 20 61 73 20 74 68 65 20 63 6f 6e 6e 65 63  on as the connec
2ec4b 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a  tion is closed..
2ec4c 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c 20  **.** A virtual 
2ec4d 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65 20  database can be 
2ec4e 65 69 74 68 65 72 20 61 20 64 69 73 6b 20 66 69  either a disk fi
2ec4f 6c 65 20 28 74 68 61 74 20 69 73 20 61 75 74 6f  le (that is auto
2ec50 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 64 65 6c  matically.** del
2ec51 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 66 69  eted when the fi
2ec52 6c 65 20 69 73 20 63 6c 6f 73 65 64 29 20 6f 72  le is closed) or
2ec53 20 69 74 20 61 6e 20 62 65 20 68 65 6c 64 20 65   it an be held e
2ec54 6e 74 69 72 65 6c 79 20 69 6e 20 6d 65 6d 6f 72  ntirely in memor
2ec55 79 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20  y,.** depending 
2ec56 6f 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  on the values of
2ec57 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 4d 50   the SQLITE_TEMP
2ec58 5f 53 54 4f 52 45 20 63 6f 6d 70 69 6c 65 2d 74  _STORE compile-t
2ec59 69 6d 65 20 6d 61 63 72 6f 20 61 6e 64 20 74 68  ime macro and th
2ec5a 65 0a 2a 2a 20 64 62 2d 3e 74 65 6d 70 5f 73 74  e.** db->temp_st
2ec5b 6f 72 65 20 76 61 72 69 61 62 6c 65 2c 20 61 63  ore variable, ac
2ec5c 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 66  cording to the f
2ec5d 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 3a 0a  ollowing chart:.
2ec5e 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 54  **.**   SQLITE_T
2ec5f 45 4d 50 5f 53 54 4f 52 45 20 20 20 20 20 64 62  EMP_STORE     db
2ec60 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 20 20 20  ->temp_store    
2ec61 20 4c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 65 6d   Location of tem
2ec62 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 0a  porary database.
2ec63 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
2ec64 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d  ------     -----
2ec65 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d  ---------     --
2ec66 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2ec67 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
2ec68 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
2ec69 20 20 20 20 20 20 20 20 61 6e 79 20 20 20 20 20          any     
2ec6a 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 0a             file.
2ec6b 2a 2a 20 20 20 31 20 20 20 20 20 20 20 20 20 20  **   1          
2ec6c 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20             1    
2ec6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
2ec6e 6c 65 0a 2a 2a 20 20 20 31 20 20 20 20 20 20 20  le.**   1       
2ec6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 20                2 
2ec70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec71 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 31 20 20   memory.**   1  
2ec72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec73 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
2ec74 20 20 20 20 20 20 66 69 6c 65 0a 2a 2a 20 20 20        file.**   
2ec75 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
2ec76 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20        1         
2ec77 20 20 20 20 20 20 20 20 20 66 69 6c 65 0a 2a 2a           file.**
2ec78 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20     2            
2ec79 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20           2      
2ec7a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f              memo
2ec7b 72 79 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20  ry.**   2       
2ec7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
2ec7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec7e 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 33 20 20   memory.**   3  
2ec7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec80 20 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20     any          
2ec81 20 20 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2f 0a        memory.*/.
2ec82 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2ec83 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
2ec84 61 63 74 6f 72 79 28 0a 20 20 63 6f 6e 73 74 20  actory(.  const 
2ec85 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2ec86 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 74 61      /* Main data
2ec87 62 61 73 65 20 77 68 65 6e 20 6f 70 65 6e 69 6e  base when openin
2ec88 67 20 61 75 78 20 6f 74 68 65 72 77 69 73 65 20  g aux otherwise 
2ec89 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  0 */.  const cha
2ec8a 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *zFilename,   
2ec8b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2ec8c 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  file containing 
2ec8d 74 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61  the BTree databa
2ec8e 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d 69 74  se */.  int omit
2ec8f 4a 6f 75 72 6e 61 6c 2c 20 20 20 20 20 20 20 20  Journal,        
2ec90 20 20 2f 2a 20 69 66 20 54 52 55 45 20 74 68 65    /* if TRUE the
2ec91 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c  n do not journal
2ec92 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
2ec93 69 6e 74 20 6e 43 61 63 68 65 2c 20 20 20 20 20  int nCache,     
2ec94 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77            /* How
2ec95 20 6d 61 6e 79 20 70 61 67 65 73 20 69 6e 20 74   many pages in t
2ec96 68 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f  he page cache */
2ec97 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 2c  .  int vfsFlags,
2ec98 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ec99 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  Flags passed thr
2ec9a 6f 75 67 68 20 74 6f 20 76 66 73 4f 70 65 6e 20  ough to vfsOpen 
2ec9b 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42  */.  Btree **ppB
2ec9c 74 72 65 65 20 20 20 20 20 20 20 20 20 20 20 2f  tree           /
2ec9d 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77  * Pointer to new
2ec9e 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72   Btree object wr
2ec9f 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 7b  itten here */.){
2eca0 0a 20 20 69 6e 74 20 62 74 46 6c 61 67 73 20 3d  .  int btFlags =
2eca1 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20   0;.  int rc;.  
2eca2 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2eca3 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
2eca4 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
2eca5 73 65 72 74 28 20 70 70 42 74 72 65 65 20 21 3d  sert( ppBtree !=
2eca6 20 30 29 3b 0a 20 20 69 66 28 20 6f 6d 69 74 4a   0);.  if( omitJ
2eca7 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 62 74  ournal ){.    bt
2eca8 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4f  Flags |= BTREE_O
2eca9 4d 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 7d  MIT_JOURNAL;.  }
2ecaa 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
2ecab 20 26 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64   & SQLITE_NoRead
2ecac 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 62 74 46 6c  lock ){.    btFl
2ecad 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4e 4f 5f  ags |= BTREE_NO_
2ecae 52 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20  READLOCK;.  }.  
2ecaf 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  if( zFilename==0
2ecb0 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54   ){.#if SQLITE_T
2ecb1 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a 20 20 20  EMP_STORE==0.   
2ecb2 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 2a   /* Do nothing *
2ecb3 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  /.#endif.#ifndef
2ecb4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d   SQLITE_OMIT_MEM
2ecb5 4f 52 59 44 42 0a 23 69 66 20 53 51 4c 49 54 45  ORYDB.#if SQLITE
2ecb6 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 0a 20  _TEMP_STORE==1. 
2ecb7 20 20 20 69 66 28 20 64 62 2d 3e 74 65 6d 70 5f     if( db->temp_
2ecb8 73 74 6f 72 65 3d 3d 32 20 29 20 7a 46 69 6c 65  store==2 ) zFile
2ecb9 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79 3a  name = ":memory:
2ecba 22 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  ";.#endif.#if SQ
2ecbb 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d  LITE_TEMP_STORE=
2ecbc 3d 32 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 74  =2.    if( db->t
2ecbd 65 6d 70 5f 73 74 6f 72 65 21 3d 31 20 29 20 7a  emp_store!=1 ) z
2ecbe 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d  Filename = ":mem
2ecbf 6f 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23 69  ory:";.#endif.#i
2ecc0 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  f SQLITE_TEMP_ST
2ecc1 4f 52 45 3d 3d 33 0a 20 20 20 20 7a 46 69 6c 65  ORE==3.    zFile
2ecc2 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79 3a  name = ":memory:
2ecc3 22 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  ";.#endif.#endif
2ecc4 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2ecc5 4d 45 4d 4f 52 59 44 42 20 2a 2f 0a 20 20 7d 0a  MEMORYDB */.  }.
2ecc6 0a 20 20 69 66 28 20 28 76 66 73 46 6c 61 67 73  .  if( (vfsFlags
2ecc7 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d   & SQLITE_OPEN_M
2ecc8 41 49 4e 5f 44 42 29 21 3d 30 20 26 26 20 28 7a  AIN_DB)!=0 && (z
2ecc9 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 2a  Filename==0 || *
2ecca 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 29 20 29 7b  zFilename==0) ){
2eccb 0a 20 20 20 20 76 66 73 46 6c 61 67 73 20 3d 20  .    vfsFlags = 
2eccc 28 76 66 73 46 6c 61 67 73 20 26 20 7e 53 51 4c  (vfsFlags & ~SQL
2eccd 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
2ecce 29 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ) | SQLITE_OPEN_
2eccf 54 45 4d 50 5f 44 42 3b 0a 20 20 7d 0a 20 20 72  TEMP_DB;.  }.  r
2ecd0 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
2ecd1 4f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20  Open(zFilename, 
2ecd2 28 73 71 6c 69 74 65 33 20 2a 29 64 62 2c 20 70  (sqlite3 *)db, p
2ecd3 70 42 74 72 65 65 2c 20 62 74 46 6c 61 67 73 2c  pBtree, btFlags,
2ecd4 20 76 66 73 46 6c 61 67 73 29 3b 0a 0a 20 20 2f   vfsFlags);..  /
2ecd5 2a 20 49 66 20 74 68 65 20 42 2d 54 72 65 65 20  * If the B-Tree 
2ecd6 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79  was successfully
2ecd7 20 6f 70 65 6e 65 64 2c 20 73 65 74 20 74 68 65   opened, set the
2ecd8 20 70 61 67 65 72 2d 63 61 63 68 65 20 73 69 7a   pager-cache siz
2ecd9 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 64 65  e to the.  ** de
2ecda 66 61 75 6c 74 20 76 61 6c 75 65 2e 20 45 78 63  fault value. Exc
2ecdb 65 70 74 2c 20 69 66 20 74 68 65 20 63 61 6c 6c  ept, if the call
2ecdc 20 74 6f 20 42 74 72 65 65 4f 70 65 6e 28 29 20   to BtreeOpen() 
2ecdd 72 65 74 75 72 6e 65 64 20 61 20 68 61 6e 64 6c  returned a handl
2ecde 65 0a 20 20 2a 2a 20 6f 70 65 6e 20 6f 6e 20 61  e.  ** open on a
2ecdf 6e 20 65 78 69 73 74 69 6e 67 20 73 68 61 72 65  n existing share
2ece0 64 20 70 61 67 65 72 2d 63 61 63 68 65 2c 20 64  d pager-cache, d
2ece1 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  o not change the
2ece2 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 20 20   pager-cache .  
2ece3 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20  ** size..  */.  
2ece4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2ece5 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42  K && 0==sqlite3B
2ece6 74 72 65 65 53 63 68 65 6d 61 28 2a 70 70 42 74  treeSchema(*ppBt
2ece7 72 65 65 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20  ree, 0, 0) ){.  
2ece8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
2ece9 74 43 61 63 68 65 53 69 7a 65 28 2a 70 70 42 74  tCacheSize(*ppBt
2ecea 72 65 65 2c 20 6e 43 61 63 68 65 29 3b 0a 20 20  ree, nCache);.  
2eceb 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
2ecec 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 55  ../*.** Return U
2eced 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 45 6e 67  TF-8 encoded Eng
2ecee 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 65 78  lish language ex
2ecef 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65  planation of the
2ecf0 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20   most recent.** 
2ecf1 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
2ecf2 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
2ecf3 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  *sqlite3_errmsg(
2ecf4 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2ecf5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20  const char *z;. 
2ecf6 20 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20 20   if( !db ){.    
2ecf7 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 72  return sqlite3Er
2ecf8 72 53 74 72 28 53 51 4c 49 54 45 5f 4e 4f 4d 45  rStr(SQLITE_NOME
2ecf9 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73  M);.  }.  if( !s
2ecfa 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
2ecfb 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b  kSickOrOk(db) ){
2ecfc 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
2ecfd 74 65 33 45 72 72 53 74 72 28 53 51 4c 49 54 45  te3ErrStr(SQLITE
2ecfe 5f 4d 49 53 55 53 45 29 3b 0a 20 20 7d 0a 20 20  _MISUSE);.  }.  
2ecff 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2ed00 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  iled ){.    retu
2ed01 72 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  rn sqlite3ErrStr
2ed02 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a  (SQLITE_NOMEM);.
2ed03 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
2ed04 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2ed05 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20  tex);.  assert( 
2ed06 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
2ed07 64 20 29 3b 0a 20 20 7a 20 3d 20 28 63 68 61 72  d );.  z = (char
2ed08 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
2ed09 74 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 0a  text(db->pErr);.
2ed0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
2ed0b 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
2ed0c 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20   if( z==0 ){.   
2ed0d 20 7a 20 3d 20 73 71 6c 69 74 65 33 45 72 72 53   z = sqlite3ErrS
2ed0e 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29 3b  tr(db->errCode);
2ed0f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
2ed10 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
2ed11 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
2ed12 7a 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  z;.}..#ifndef SQ
2ed13 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
2ed14 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 55 54 46  /*.** Return UTF
2ed15 2d 31 36 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c  -16 encoded Engl
2ed16 69 73 68 20 6c 61 6e 67 75 61 67 65 20 65 78 70  ish language exp
2ed17 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  lanation of the 
2ed18 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65  most recent.** e
2ed19 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rror..*/.SQLITE_
2ed1a 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
2ed1b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
2ed1c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2ed1d 20 2f 2a 20 42 65 63 61 75 73 65 20 61 6c 6c 20   /* Because all 
2ed1e 74 68 65 20 63 68 61 72 61 63 74 65 72 73 20 69  the characters i
2ed1f 6e 20 74 68 65 20 73 74 72 69 6e 67 20 61 72 65  n the string are
2ed20 20 69 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 0a   in the unicode.
2ed21 20 20 2a 2a 20 72 61 6e 67 65 20 30 78 30 30 2d    ** range 0x00-
2ed22 30 78 46 46 2c 20 69 66 20 77 65 20 70 61 64 20  0xFF, if we pad 
2ed23 74 68 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 73  the big-endian s
2ed24 74 72 69 6e 67 20 77 69 74 68 20 61 20 0a 20 20  tring with a .  
2ed25 2a 2a 20 7a 65 72 6f 20 62 79 74 65 2c 20 77 65  ** zero byte, we
2ed26 20 63 61 6e 20 6f 62 74 61 69 6e 20 74 68 65 20   can obtain the 
2ed27 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 73 74  little-endian st
2ed28 72 69 6e 67 20 77 69 74 68 0a 20 20 2a 2a 20 26  ring with.  ** &
2ed29 62 69 67 5f 65 6e 64 69 61 6e 5b 31 5d 2e 0a 20  big_endian[1].. 
2ed2a 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
2ed2b 73 74 20 63 68 61 72 20 6f 75 74 4f 66 4d 65 6d  st char outOfMem
2ed2c 42 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20  Be[] = {.    0, 
2ed2d 27 6f 27 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20  'o', 0, 'u', 0, 
2ed2e 27 74 27 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20  't', 0, ' ', .  
2ed2f 20 20 30 2c 20 27 6f 27 2c 20 30 2c 20 27 66 27    0, 'o', 0, 'f'
2ed30 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30  , 0, ' ', .    0
2ed31 2c 20 27 6d 27 2c 20 30 2c 20 27 65 27 2c 20 30  , 'm', 0, 'e', 0
2ed32 2c 20 27 6d 27 2c 20 30 2c 20 27 6f 27 2c 20 30  , 'm', 0, 'o', 0
2ed33 2c 20 27 72 27 2c 20 30 2c 20 27 79 27 2c 20 30  , 'r', 0, 'y', 0
2ed34 2c 20 30 2c 20 30 0a 20 20 7d 3b 0a 20 20 73 74  , 0, 0.  };.  st
2ed35 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2ed36 6d 69 73 75 73 65 42 65 20 5b 5d 20 3d 20 7b 0a  misuseBe [] = {.
2ed37 20 20 20 20 30 2c 20 27 6c 27 2c 20 30 2c 20 27      0, 'l', 0, '
2ed38 69 27 2c 20 30 2c 20 27 62 27 2c 20 30 2c 20 27  i', 0, 'b', 0, '
2ed39 72 27 2c 20 30 2c 20 27 61 27 2c 20 30 2c 20 27  r', 0, 'a', 0, '
2ed3a 72 27 2c 20 30 2c 20 27 79 27 2c 20 30 2c 20 27  r', 0, 'y', 0, '
2ed3b 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 72 27 2c   ', .    0, 'r',
2ed3c 20 30 2c 20 27 6f 27 2c 20 30 2c 20 27 75 27 2c   0, 'o', 0, 'u',
2ed3d 20 30 2c 20 27 74 27 2c 20 30 2c 20 27 69 27 2c   0, 't', 0, 'i',
2ed3e 20 30 2c 20 27 6e 27 2c 20 30 2c 20 27 65 27 2c   0, 'n', 0, 'e',
2ed3f 20 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c   0, ' ', .    0,
2ed40 20 27 63 27 2c 20 30 2c 20 27 61 27 2c 20 30 2c   'c', 0, 'a', 0,
2ed41 20 27 6c 27 2c 20 30 2c 20 27 6c 27 2c 20 30 2c   'l', 0, 'l', 0,
2ed42 20 27 65 27 2c 20 30 2c 20 27 64 27 2c 20 30 2c   'e', 0, 'd', 0,
2ed43 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 6f   ' ', .    0, 'o
2ed44 27 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20 27 74  ', 0, 'u', 0, 't
2ed45 27 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20 20 20  ', 0, ' ', .    
2ed46 30 2c 20 27 6f 27 2c 20 30 2c 20 27 66 27 2c 20  0, 'o', 0, 'f', 
2ed47 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20  0, ' ', .    0, 
2ed48 27 73 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20  's', 0, 'e', 0, 
2ed49 27 71 27 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20  'q', 0, 'u', 0, 
2ed4a 27 65 27 2c 20 30 2c 20 27 6e 27 2c 20 30 2c 20  'e', 0, 'n', 0, 
2ed4b 27 63 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20  'c', 0, 'e', 0, 
2ed4c 30 2c 20 30 0a 20 20 7d 3b 0a 0a 20 20 63 6f 6e  0, 0.  };..  con
2ed4d 73 74 20 76 6f 69 64 20 2a 7a 3b 0a 20 20 69 66  st void *z;.  if
2ed4e 28 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74  ( !db ){.    ret
2ed4f 75 72 6e 20 28 76 6f 69 64 20 2a 29 28 26 6f 75  urn (void *)(&ou
2ed50 74 4f 66 4d 65 6d 42 65 5b 53 51 4c 49 54 45 5f  tOfMemBe[SQLITE_
2ed51 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c  UTF16NATIVE==SQL
2ed52 49 54 45 5f 55 54 46 31 36 4c 45 3f 31 3a 30 5d  ITE_UTF16LE?1:0]
2ed53 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73 71  );.  }.  if( !sq
2ed54 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
2ed55 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a  SickOrOk(db) ){.
2ed56 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64      return (void
2ed57 20 2a 29 28 26 6d 69 73 75 73 65 42 65 5b 53 51   *)(&misuseBe[SQ
2ed58 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
2ed59 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
2ed5a 3f 31 3a 30 5d 29 3b 0a 20 20 7d 0a 20 20 73 71  ?1:0]);.  }.  sq
2ed5b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
2ed5c 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
2ed5d 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c  assert( !db->mal
2ed5e 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a  locFailed );.  z
2ed5f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2ed60 5f 74 65 78 74 31 36 28 64 62 2d 3e 70 45 72 72  _text16(db->pErr
2ed61 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b  );.  if( z==0 ){
2ed62 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
2ed63 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72  eSetStr(db->pErr
2ed64 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 45 72 72  , -1, sqlite3Err
2ed65 53 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29  Str(db->errCode)
2ed66 2c 0a 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  ,.         SQLIT
2ed67 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53  E_UTF8, SQLITE_S
2ed68 54 41 54 49 43 29 3b 0a 20 20 20 20 7a 20 3d 20  TATIC);.    z = 
2ed69 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2ed6a 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a  xt16(db->pErr);.
2ed6b 20 20 7d 0a 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f    }.  /* A mallo
2ed6c 63 28 29 20 6d 61 79 20 68 61 76 65 20 66 61 69  c() may have fai
2ed6d 6c 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 63  led within the c
2ed6e 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 76  all to sqlite3_v
2ed6f 61 6c 75 65 5f 74 65 78 74 31 36 28 29 0a 20 20  alue_text16().  
2ed70 2a 2a 20 61 62 6f 76 65 2e 20 49 66 20 74 68 69  ** above. If thi
2ed71 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  s is the case, t
2ed72 68 65 6e 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c  hen the db->mall
2ed73 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6e 65  ocFailed flag ne
2ed74 65 64 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 63  eds to.  ** be c
2ed75 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20 72 65  leared before re
2ed76 74 75 72 6e 69 6e 67 2e 20 44 6f 20 74 68 69 73  turning. Do this
2ed77 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74 65   directly, inste
2ed78 61 64 20 6f 66 20 76 69 61 0a 20 20 2a 2a 20 73  ad of via.  ** s
2ed79 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 29 2c  qlite3ApiExit(),
2ed7a 20 74 6f 20 61 76 6f 69 64 20 73 65 74 74 69 6e   to avoid settin
2ed7b 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  g the database h
2ed7c 61 6e 64 6c 65 20 65 72 72 6f 72 20 6d 65 73 73  andle error mess
2ed7d 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e  age..  */.  db->
2ed7e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30  mallocFailed = 0
2ed7f 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2ed80 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2ed81 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  x);.  return z;.
2ed82 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2ed83 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
2ed84 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
2ed85 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65  he most recent e
2ed86 72 72 6f 72 20 63 6f 64 65 20 67 65 6e 65 72 61  rror code genera
2ed87 74 65 64 20 62 79 20 61 6e 20 53 51 4c 69 74 65  ted by an SQLite
2ed88 20 72 6f 75 74 69 6e 65 2e 20 49 66 20 4e 55 4c   routine. If NUL
2ed89 4c 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 74  L is.** passed t
2ed8a 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  o this function,
2ed8b 20 77 65 20 61 73 73 75 6d 65 20 61 20 6d 61 6c   we assume a mal
2ed8c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72  loc() failed dur
2ed8d 69 6e 67 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ing sqlite3_open
2ed8e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
2ed8f 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72  I int sqlite3_er
2ed90 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64  rcode(sqlite3 *d
2ed91 62 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20  b){.  if( db && 
2ed92 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68  !sqlite3SafetyCh
2ed93 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20  eckSickOrOk(db) 
2ed94 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2ed95 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
2ed96 0a 20 20 69 66 28 20 21 64 62 20 7c 7c 20 64 62  .  if( !db || db
2ed97 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2ed98 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
2ed99 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
2ed9a 20 72 65 74 75 72 6e 20 64 62 2d 3e 65 72 72 43   return db->errC
2ed9b 6f 64 65 20 26 20 64 62 2d 3e 65 72 72 4d 61 73  ode & db->errMas
2ed9c 6b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  k;.}.SQLITE_API 
2ed9d 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65  int sqlite3_exte
2ed9e 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c  nded_errcode(sql
2ed9f 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28  ite3 *db){.  if(
2eda0 20 64 62 20 26 26 20 21 73 71 6c 69 74 65 33 53   db && !sqlite3S
2eda1 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72  afetyCheckSickOr
2eda2 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65  Ok(db) ){.    re
2eda3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
2eda4 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64  SE;.  }.  if( !d
2eda5 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  b || db->mallocF
2eda6 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74  ailed ){.    ret
2eda7 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2eda8 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 64  ;.  }.  return d
2eda9 62 2d 3e 65 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f  b->errCode;.}../
2edaa 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
2edab 77 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  w collating func
2edac 74 69 6f 6e 20 66 6f 72 20 64 61 74 61 62 61 73  tion for databas
2edad 65 20 22 64 62 22 2e 20 20 54 68 65 20 6e 61 6d  e "db".  The nam
2edae 65 20 69 73 20 7a 4e 61 6d 65 0a 2a 2a 20 61 6e  e is zName.** an
2edaf 64 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69  d the encoding i
2edb0 73 20 65 6e 63 2e 0a 2a 2f 0a 73 74 61 74 69 63  s enc..*/.static
2edb1 20 69 6e 74 20 63 72 65 61 74 65 43 6f 6c 6c 61   int createColla
2edb2 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a  tion(.  sqlite3*
2edb3 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68   db, .  const ch
2edb4 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e  ar *zName, .  in
2edb5 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20  t enc, .  void* 
2edb6 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f  pCtx,.  int(*xCo
2edb7 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74  mpare)(void*,int
2edb8 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
2edb9 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20  ,const void*),. 
2edba 20 76 6f 69 64 28 2a 78 44 65 6c 29 28 76 6f 69   void(*xDel)(voi
2edbb 64 2a 29 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  d*).){.  CollSeq
2edbc 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 65   *pColl;.  int e
2edbd 6e 63 32 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  nc2;.  int nName
2edbe 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73  ;.  .  assert( s
2edbf 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
2edc0 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
2edc1 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f  .  /* If SQLITE_
2edc2 55 54 46 31 36 20 69 73 20 73 70 65 63 69 66 69  UTF16 is specifi
2edc3 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f 64 69  ed as the encodi
2edc4 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73 66 6f  ng type, transfo
2edc5 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f 20  rm this.  ** to 
2edc6 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54  one of SQLITE_UT
2edc7 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f  F16LE or SQLITE_
2edc8 55 54 46 31 36 42 45 20 75 73 69 6e 67 20 74 68  UTF16BE using th
2edc9 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55 54  e.  ** SQLITE_UT
2edca 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72 6f 2e  F16NATIVE macro.
2edcb 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73   SQLITE_UTF16 is
2edcc 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e   not used intern
2edcd 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e 63  ally..  */.  enc
2edce 32 20 3d 20 65 6e 63 20 26 20 7e 53 51 4c 49 54  2 = enc & ~SQLIT
2edcf 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 3b  E_UTF16_ALIGNED;
2edd0 0a 20 20 69 66 28 20 65 6e 63 32 3d 3d 53 51 4c  .  if( enc2==SQL
2edd1 49 54 45 5f 55 54 46 31 36 20 29 7b 0a 20 20 20  ITE_UTF16 ){.   
2edd2 20 65 6e 63 32 20 3d 20 53 51 4c 49 54 45 5f 55   enc2 = SQLITE_U
2edd3 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 0a  TF16NATIVE;.  }.
2edd4 20 20 69 66 28 20 28 65 6e 63 32 26 7e 33 29 21    if( (enc2&~3)!
2edd5 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
2edd6 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
2edd7 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
2edd8 69 66 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20  if this call is 
2edd9 72 65 6d 6f 76 69 6e 67 20 6f 72 20 72 65 70 6c  removing or repl
2edda 61 63 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e  acing an existin
2eddb 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20 2a  g collation .  *
2eddc 2a 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20 73  * sequence. If s
2eddd 6f 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 65  o, and there are
2edde 20 61 63 74 69 76 65 20 56 4d 73 2c 20 72 65 74   active VMs, ret
2eddf 75 72 6e 20 62 75 73 79 2e 20 49 66 20 74 68 65  urn busy. If the
2ede0 72 65 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 20 61  re.  ** are no a
2ede1 63 74 69 76 65 20 56 4d 73 2c 20 69 6e 76 61 6c  ctive VMs, inval
2ede2 69 64 61 74 65 20 61 6e 79 20 70 72 65 2d 63 6f  idate any pre-co
2ede3 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74  mpiled statement
2ede4 73 2e 0a 20 20 2a 2f 0a 20 20 6e 4e 61 6d 65 20  s..  */.  nName 
2ede5 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28  = sqlite3Strlen(
2ede6 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 70 43  db, zName);.  pC
2ede7 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
2ede8 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 28 75 38  dCollSeq(db, (u8
2ede9 29 65 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  )enc2, zName, nN
2edea 61 6d 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ame, 0);.  if( p
2edeb 43 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78  Coll && pColl->x
2edec 43 6d 70 20 29 7b 0a 20 20 20 20 69 66 28 20 64  Cmp ){.    if( d
2eded 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
2edee 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2edef 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
2edf0 45 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20  E_BUSY, .       
2edf1 20 22 55 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65   "Unable to dele
2edf2 74 65 2f 6d 6f 64 69 66 79 20 63 6f 6c 6c 61 74  te/modify collat
2edf3 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 64 75 65  ion sequence due
2edf4 20 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65   to active state
2edf5 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 72  ments");.      r
2edf6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
2edf7 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  Y;.    }.    sql
2edf8 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
2edf9 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
2edfa 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63 6f 6c  ;..    /* If col
2edfb 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2edfc 70 43 6f 6c 6c 20 77 61 73 20 63 72 65 61 74 65  pColl was create
2edfd 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 61 20  d directly by a 
2edfe 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73  call to.    ** s
2edff 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2ee00 6c 6c 61 74 69 6f 6e 2c 20 61 6e 64 20 6e 6f 74  llation, and not
2ee01 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 79   generated by sy
2ee02 6e 74 68 43 6f 6c 6c 53 65 71 28 29 2c 0a 20 20  nthCollSeq(),.  
2ee03 20 20 2a 2a 20 74 68 65 6e 20 61 6e 79 20 63 6f    ** then any co
2ee04 70 69 65 73 20 6d 61 64 65 20 62 79 20 73 79 6e  pies made by syn
2ee05 74 68 43 6f 6c 6c 53 65 71 28 29 20 6e 65 65 64  thCollSeq() need
2ee06 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74   to be invalidat
2ee07 65 64 2e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c  ed..    ** Also,
2ee08 20 63 6f 6c 6c 61 74 69 6f 6e 20 64 65 73 74 72   collation destr
2ee09 75 63 74 6f 72 20 2d 20 43 6f 6c 6c 53 65 71 2e  uctor - CollSeq.
2ee0a 78 44 65 6c 28 29 20 2d 20 66 75 6e 63 74 69 6f  xDel() - functio
2ee0b 6e 20 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 2a  n may need.    *
2ee0c 2a 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 2e 0a  * to be called..
2ee0d 20 20 20 20 2a 2f 20 0a 20 20 20 20 69 66 28 20      */ .    if( 
2ee0e 28 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26 20 7e 53  (pColl->enc & ~S
2ee0f 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
2ee10 4e 45 44 29 3d 3d 65 6e 63 32 20 29 7b 0a 20 20  NED)==enc2 ){.  
2ee11 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f      CollSeq *aCo
2ee12 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  ll = sqlite3Hash
2ee13 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53  Find(&db->aCollS
2ee14 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  eq, zName, nName
2ee15 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  );.      int j;.
2ee16 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
2ee17 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  <3; j++){.      
2ee18 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 26    CollSeq *p = &
2ee19 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20  aColl[j];.      
2ee1a 20 20 69 66 28 20 70 2d 3e 65 6e 63 3d 3d 70 43    if( p->enc==pC
2ee1b 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20  oll->enc ){.    
2ee1c 20 20 20 20 20 20 69 66 28 20 70 2d 3e 78 44 65        if( p->xDe
2ee1d 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  l ){.           
2ee1e 20 70 2d 3e 78 44 65 6c 28 70 2d 3e 70 55 73 65   p->xDel(p->pUse
2ee1f 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  r);.          }.
2ee20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 78 43 6d            p->xCm
2ee21 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  p = 0;.        }
2ee22 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2ee23 20 7d 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71   }..  pColl = sq
2ee24 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
2ee25 28 64 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a  (db, (u8)enc2, z
2ee26 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 31 29 3b  Name, nName, 1);
2ee27 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a  .  if( pColl ){.
2ee28 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20      pColl->xCmp 
2ee29 3d 20 78 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20  = xCompare;.    
2ee2a 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 20 3d 20 70  pColl->pUser = p
2ee2b 43 74 78 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e  Ctx;.    pColl->
2ee2c 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20  xDel = xDel;.   
2ee2d 20 70 43 6f 6c 6c 2d 3e 65 6e 63 20 3d 20 28 75   pColl->enc = (u
2ee2e 38 29 28 65 6e 63 32 20 7c 20 28 65 6e 63 20 26  8)(enc2 | (enc &
2ee2f 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c   SQLITE_UTF16_AL
2ee30 49 47 4e 45 44 29 29 3b 0a 20 20 7d 0a 20 20 73  IGNED));.  }.  s
2ee31 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2ee32 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20  SQLITE_OK, 0);. 
2ee33 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2ee34 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  K;.}.../*.** Thi
2ee35 73 20 61 72 72 61 79 20 64 65 66 69 6e 65 73 20  s array defines 
2ee36 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64  hard upper bound
2ee37 73 20 6f 6e 20 6c 69 6d 69 74 20 76 61 6c 75 65  s on limit value
2ee38 73 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 69 74 69  s.  The.** initi
2ee39 61 6c 69 7a 65 72 20 6d 75 73 74 20 62 65 20 6b  alizer must be k
2ee3a 65 70 74 20 69 6e 20 73 79 6e 63 20 77 69 74 68  ept in sync with
2ee3b 20 74 68 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49   the SQLITE_LIMI
2ee3c 54 5f 2a 0a 2a 2a 20 23 64 65 66 69 6e 65 73 20  T_*.** #defines 
2ee3d 69 6e 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2f  in sqlite3.h..*/
2ee3e 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e  .static const in
2ee3f 74 20 61 48 61 72 64 4c 69 6d 69 74 5b 5d 20 3d  t aHardLimit[] =
2ee40 20 7b 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f   {.  SQLITE_MAX_
2ee41 4c 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54 45  LENGTH,.  SQLITE
2ee42 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 2c  _MAX_SQL_LENGTH,
2ee43 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  .  SQLITE_MAX_CO
2ee44 4c 55 4d 4e 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  LUMN,.  SQLITE_M
2ee45 41 58 5f 45 58 50 52 5f 44 45 50 54 48 2c 0a 20  AX_EXPR_DEPTH,. 
2ee46 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50   SQLITE_MAX_COMP
2ee47 4f 55 4e 44 5f 53 45 4c 45 43 54 2c 0a 20 20 53  OUND_SELECT,.  S
2ee48 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f  QLITE_MAX_VDBE_O
2ee49 50 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  P,.  SQLITE_MAX_
2ee4a 46 55 4e 43 54 49 4f 4e 5f 41 52 47 2c 0a 20 20  FUNCTION_ARG,.  
2ee4b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43  SQLITE_MAX_ATTAC
2ee4c 48 45 44 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  HED,.  SQLITE_MA
2ee4d 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  X_LIKE_PATTERN_L
2ee4e 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f  ENGTH,.  SQLITE_
2ee4f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MAX_VARIABLE_NUM
2ee50 42 45 52 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d  BER,.};../*.** M
2ee51 61 6b 65 20 73 75 72 65 20 74 68 65 20 68 61 72  ake sure the har
2ee52 64 20 6c 69 6d 69 74 73 20 61 72 65 20 73 65 74  d limits are set
2ee53 20 74 6f 20 72 65 61 73 6f 6e 61 62 6c 65 20 76   to reasonable v
2ee54 61 6c 75 65 73 0a 2a 2f 0a 23 69 66 20 53 51 4c  alues.*/.#if SQL
2ee55 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3c 31  ITE_MAX_LENGTH<1
2ee56 30 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  00.# error SQLIT
2ee57 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 6d 75 73  E_MAX_LENGTH mus
2ee58 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 30  t be at least 10
2ee59 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  0.#endif.#if SQL
2ee5a 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
2ee5b 54 48 3c 31 30 30 0a 23 20 65 72 72 6f 72 20 53  TH<100.# error S
2ee5c 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
2ee5d 4e 47 54 48 20 6d 75 73 74 20 62 65 20 61 74 20  NGTH must be at 
2ee5e 6c 65 61 73 74 20 31 30 30 0a 23 65 6e 64 69 66  least 100.#endif
2ee5f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
2ee60 53 51 4c 5f 4c 45 4e 47 54 48 3e 53 51 4c 49 54  SQL_LENGTH>SQLIT
2ee61 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20 65  E_MAX_LENGTH.# e
2ee62 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
2ee63 53 51 4c 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20  SQL_LENGTH must 
2ee64 6e 6f 74 20 62 65 20 67 72 65 61 74 65 72 20 74  not be greater t
2ee65 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  han SQLITE_MAX_L
2ee66 45 4e 47 54 48 0a 23 65 6e 64 69 66 0a 23 69 66  ENGTH.#endif.#if
2ee67 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50   SQLITE_MAX_COMP
2ee68 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 32 0a 23 20  OUND_SELECT<2.# 
2ee69 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58  error SQLITE_MAX
2ee6a 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
2ee6b 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73   must be at leas
2ee6c 74 20 32 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  t 2.#endif.#if S
2ee6d 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f  QLITE_MAX_VDBE_O
2ee6e 50 3c 34 30 0a 23 20 65 72 72 6f 72 20 53 51 4c  P<40.# error SQL
2ee6f 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20  ITE_MAX_VDBE_OP 
2ee70 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74  must be at least
2ee71 20 34 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53   40.#endif.#if S
2ee72 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49  QLITE_MAX_FUNCTI
2ee73 4f 4e 5f 41 52 47 3c 30 20 7c 7c 20 53 51 4c 49  ON_ARG<0 || SQLI
2ee74 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
2ee75 41 52 47 3e 31 30 30 30 0a 23 20 65 72 72 6f 72  ARG>1000.# error
2ee76 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43   SQLITE_MAX_FUNC
2ee77 54 49 4f 4e 5f 41 52 47 20 6d 75 73 74 20 62 65  TION_ARG must be
2ee78 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31   between 0 and 1
2ee79 30 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  000.#endif.#if S
2ee7a 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
2ee7b 45 44 3c 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4d  ED<0 || SQLITE_M
2ee7c 41 58 5f 41 54 54 41 43 48 45 44 3e 33 30 0a 23  AX_ATTACHED>30.#
2ee7d 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
2ee7e 58 5f 41 54 54 41 43 48 45 44 20 6d 75 73 74 20  X_ATTACHED must 
2ee7f 62 65 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64  be between 0 and
2ee80 20 33 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53   30.#endif.#if S
2ee81 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
2ee82 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 3c 31 0a  ATTERN_LENGTH<1.
2ee83 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
2ee84 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  AX_LIKE_PATTERN_
2ee85 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65 20 61  LENGTH must be a
2ee86 74 20 6c 65 61 73 74 20 31 0a 23 65 6e 64 69 66  t least 1.#endif
2ee87 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
2ee88 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c  VARIABLE_NUMBER<
2ee89 31 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  1.# error SQLITE
2ee8a 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55  _MAX_VARIABLE_NU
2ee8b 4d 42 45 52 20 6d 75 73 74 20 62 65 20 61 74 20  MBER must be at 
2ee8c 6c 65 61 73 74 20 31 0a 23 65 6e 64 69 66 0a 23  least 1.#endif.#
2ee8d 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  if SQLITE_MAX_CO
2ee8e 4c 55 4d 4e 3e 33 32 37 36 37 0a 23 20 65 72 72  LUMN>32767.# err
2ee8f 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  or SQLITE_MAX_CO
2ee90 4c 55 4d 4e 20 6d 75 73 74 20 6e 6f 74 20 65 78  LUMN must not ex
2ee91 63 65 65 64 20 33 32 37 36 37 0a 23 65 6e 64 69  ceed 32767.#endi
2ee92 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  f.../*.** Change
2ee93 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
2ee94 6c 69 6d 69 74 2e 20 20 52 65 70 6f 72 74 20 74  limit.  Report t
2ee95 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 2a 2a  he old value..**
2ee96 20 49 66 20 61 6e 20 69 6e 76 61 6c 69 64 20 6c   If an invalid l
2ee97 69 6d 69 74 20 69 6e 64 65 78 20 69 73 20 73 75  imit index is su
2ee98 70 70 6c 69 65 64 2c 20 72 65 70 6f 72 74 20 2d  pplied, report -
2ee99 31 2e 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68  1..** Make no ch
2ee9a 61 6e 67 65 73 20 62 75 74 20 73 74 69 6c 6c 20  anges but still 
2ee9b 72 65 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76  report the old v
2ee9c 61 6c 75 65 20 69 66 20 74 68 65 0a 2a 2a 20 6e  alue if the.** n
2ee9d 65 77 20 6c 69 6d 69 74 20 69 73 20 6e 65 67 61  ew limit is nega
2ee9e 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65  tive..**.** A ne
2ee9f 77 20 6c 6f 77 65 72 20 6c 69 6d 69 74 20 64 6f  w lower limit do
2eea0 65 73 20 6e 6f 74 20 73 68 72 69 6e 6b 20 65 78  es not shrink ex
2eea1 69 73 74 69 6e 67 20 63 6f 6e 73 74 72 75 63 74  isting construct
2eea2 73 2e 0a 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20  s..** It merely 
2eea3 70 72 65 76 65 6e 74 73 20 6e 65 77 20 63 6f 6e  prevents new con
2eea4 73 74 72 75 63 74 73 20 74 68 61 74 20 65 78 63  structs that exc
2eea5 65 65 64 20 74 68 65 20 6c 69 6d 69 74 0a 2a 2a  eed the limit.**
2eea6 20 66 72 6f 6d 20 66 6f 72 6d 69 6e 67 2e 0a 2a   from forming..*
2eea7 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2eea8 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73   sqlite3_limit(s
2eea9 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2eeaa 6c 69 6d 69 74 49 64 2c 20 69 6e 74 20 6e 65 77  limitId, int new
2eeab 4c 69 6d 69 74 29 7b 0a 20 20 69 6e 74 20 6f 6c  Limit){.  int ol
2eeac 64 4c 69 6d 69 74 3b 0a 20 20 69 66 28 20 6c 69  dLimit;.  if( li
2eead 6d 69 74 49 64 3c 30 20 7c 7c 20 6c 69 6d 69 74  mitId<0 || limit
2eeae 49 64 3e 3d 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d  Id>=SQLITE_N_LIM
2eeaf 49 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  IT ){.    return
2eeb0 20 2d 31 3b 0a 20 20 7d 0a 20 20 6f 6c 64 4c 69   -1;.  }.  oldLi
2eeb1 6d 69 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74  mit = db->aLimit
2eeb2 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20 20 69 66 28  [limitId];.  if(
2eeb3 20 6e 65 77 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a   newLimit>=0 ){.
2eeb4 20 20 20 20 69 66 28 20 6e 65 77 4c 69 6d 69 74      if( newLimit
2eeb5 3e 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69  >aHardLimit[limi
2eeb6 74 49 64 5d 20 29 7b 0a 20 20 20 20 20 20 6e 65  tId] ){.      ne
2eeb7 77 4c 69 6d 69 74 20 3d 20 61 48 61 72 64 4c 69  wLimit = aHardLi
2eeb8 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20 20  mit[limitId];.  
2eeb9 20 20 7d 0a 20 20 20 20 64 62 2d 3e 61 4c 69 6d    }.    db->aLim
2eeba 69 74 5b 6c 69 6d 69 74 49 64 5d 20 3d 20 6e 65  it[limitId] = ne
2eebb 77 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65  wLimit;.  }.  re
2eebc 74 75 72 6e 20 6f 6c 64 4c 69 6d 69 74 3b 0a 7d  turn oldLimit;.}
2eebd 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2eebe 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 77 6f  tine does the wo
2eebf 72 6b 20 6f 66 20 6f 70 65 6e 69 6e 67 20 61 20  rk of opening a 
2eec0 64 61 74 61 62 61 73 65 20 6f 6e 20 62 65 68 61  database on beha
2eec1 6c 66 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33  lf of.** sqlite3
2eec2 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69  _open() and sqli
2eec3 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 20 54 68  te3_open16(). Th
2eec4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e  e database filen
2eec5 61 6d 65 20 22 7a 46 69 6c 65 6e 61 6d 65 22 20  ame "zFilename" 
2eec6 20 0a 2a 2a 20 69 73 20 55 54 46 2d 38 20 65 6e   .** is UTF-8 en
2eec7 63 6f 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  coded..*/.static
2eec8 20 69 6e 74 20 6f 70 65 6e 44 61 74 61 62 61 73   int openDatabas
2eec9 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e(.  const char 
2eeca 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 2f 2a 20 44  *zFilename, /* D
2eecb 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
2eecc 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a   UTF-8 encoded *
2eecd 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70  /.  sqlite3 **pp
2eece 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  Db,        /* OU
2eecf 54 3a 20 52 65 74 75 72 6e 65 64 20 64 61 74 61  T: Returned data
2eed0 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
2eed1 20 75 6e 73 69 67 6e 65 64 20 66 6c 61 67 73 2c   unsigned flags,
2eed2 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61          /* Opera
2eed3 74 69 6f 6e 61 6c 20 66 6c 61 67 73 20 2a 2f 0a  tional flags */.
2eed4 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56    const char *zV
2eed5 66 73 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  fs       /* Name
2eed6 20 6f 66 20 74 68 65 20 56 46 53 20 74 6f 20 75   of the VFS to u
2eed7 73 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  se */.){.  sqlit
2eed8 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63  e3 *db;.  int rc
2eed9 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  ;.  CollSeq *pCo
2eeda 6c 6c 3b 0a 20 20 69 6e 74 20 69 73 54 68 72 65  ll;.  int isThre
2eedb 61 64 73 61 66 65 3b 0a 0a 23 69 66 6e 64 65 66  adsafe;..#ifndef
2eedc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2eedd 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71 6c  OINIT.  rc = sql
2eede 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
2eedf 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
2eee0 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a  turn rc;.#endif.
2eee1 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
2eee2 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
2eee3 4d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  Mutex==0 ){.    
2eee4 69 73 54 68 72 65 61 64 73 61 66 65 20 3d 20 30  isThreadsafe = 0
2eee5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c  ;.  }else if( fl
2eee6 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
2eee7 4e 5f 4e 4f 4d 55 54 45 58 20 29 7b 0a 20 20 20  N_NOMUTEX ){.   
2eee8 20 69 73 54 68 72 65 61 64 73 61 66 65 20 3d 20   isThreadsafe = 
2eee9 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  0;.  }else if( f
2eeea 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
2eeeb 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 29 7b 0a  EN_FULLMUTEX ){.
2eeec 20 20 20 20 69 73 54 68 72 65 61 64 73 61 66 65      isThreadsafe
2eeed 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
2eeee 20 20 20 69 73 54 68 72 65 61 64 73 61 66 65 20     isThreadsafe 
2eeef 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
2eef0 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78  onfig.bFullMutex
2eef1 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f  ;.  }..  /* Remo
2eef2 76 65 20 68 61 72 6d 66 75 6c 20 62 69 74 73 20  ve harmful bits 
2eef3 66 72 6f 6d 20 74 68 65 20 66 6c 61 67 73 20 70  from the flags p
2eef4 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 66 6c  arameter */.  fl
2eef5 61 67 73 20 26 3d 20 20 7e 28 20 53 51 4c 49 54  ags &=  ~( SQLIT
2eef6 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
2eef7 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 20 20 20  LOSE |.         
2eef8 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
2eef9 4e 5f 4d 41 49 4e 5f 44 42 20 7c 0a 20 20 20 20  N_MAIN_DB |.    
2eefa 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
2eefb 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 7c  E_OPEN_TEMP_DB |
2eefc 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2eefd 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41   SQLITE_OPEN_TRA
2eefe 4e 53 49 45 4e 54 5f 44 42 20 7c 20 0a 20 20 20  NSIENT_DB | .   
2eeff 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
2ef00 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
2ef01 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20  RNAL | .        
2ef02 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
2ef03 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20  EN_TEMP_JOURNAL 
2ef04 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  | .             
2ef05 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55    SQLITE_OPEN_SU
2ef06 42 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20  BJOURNAL | .    
2ef07 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
2ef08 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
2ef09 55 52 4e 41 4c 20 7c 0a 20 20 20 20 20 20 20 20  URNAL |.        
2ef0a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
2ef0b 45 4e 5f 4e 4f 4d 55 54 45 58 20 7c 0a 20 20 20  EN_NOMUTEX |.   
2ef0c 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
2ef0d 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45  TE_OPEN_FULLMUTE
2ef0e 58 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 29  X.             )
2ef0f 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
2ef10 20 74 68 65 20 73 71 6c 69 74 65 20 64 61 74 61   the sqlite data
2ef11 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
2ef12 64 62 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  db = sqlite3Mall
2ef13 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 73  ocZero( sizeof(s
2ef14 71 6c 69 74 65 33 29 20 29 3b 0a 20 20 69 66 28  qlite3) );.  if(
2ef15 20 64 62 3d 3d 30 20 29 20 67 6f 74 6f 20 6f 70   db==0 ) goto op
2ef16 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 69 66 28 20  endb_out;.  if( 
2ef17 69 73 54 68 72 65 61 64 73 61 66 65 20 29 7b 0a  isThreadsafe ){.
2ef18 20 20 20 20 64 62 2d 3e 6d 75 74 65 78 20 3d 20      db->mutex = 
2ef19 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
2ef1a 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  c(SQLITE_MUTEX_R
2ef1b 45 43 55 52 53 49 56 45 29 3b 0a 20 20 20 20 69  ECURSIVE);.    i
2ef1c 66 28 20 64 62 2d 3e 6d 75 74 65 78 3d 3d 30 20  f( db->mutex==0 
2ef1d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2ef1e 5f 66 72 65 65 28 64 62 29 3b 0a 20 20 20 20 20  _free(db);.     
2ef1f 20 64 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 67   db = 0;.      g
2ef20 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a  oto opendb_out;.
2ef21 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
2ef22 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2ef23 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62  db->mutex);.  db
2ef24 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30 78 66 66  ->errMask = 0xff
2ef25 3b 0a 20 20 64 62 2d 3e 70 72 69 6f 72 4e 65 77  ;.  db->priorNew
2ef26 52 6f 77 69 64 20 3d 20 30 3b 0a 20 20 64 62 2d  Rowid = 0;.  db-
2ef27 3e 6e 44 62 20 3d 20 32 3b 0a 20 20 64 62 2d 3e  >nDb = 2;.  db->
2ef28 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d  magic = SQLITE_M
2ef29 41 47 49 43 5f 42 55 53 59 3b 0a 20 20 64 62 2d  AGIC_BUSY;.  db-
2ef2a 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44 62 53 74  >aDb = db->aDbSt
2ef2b 61 74 69 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  atic;..  assert(
2ef2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 4c 69 6d   sizeof(db->aLim
2ef2d 69 74 29 3d 3d 73 69 7a 65 6f 66 28 61 48 61 72  it)==sizeof(aHar
2ef2e 64 4c 69 6d 69 74 29 20 29 3b 0a 20 20 6d 65 6d  dLimit) );.  mem
2ef2f 63 70 79 28 64 62 2d 3e 61 4c 69 6d 69 74 2c 20  cpy(db->aLimit, 
2ef30 61 48 61 72 64 4c 69 6d 69 74 2c 20 73 69 7a 65  aHardLimit, size
2ef31 6f 66 28 64 62 2d 3e 61 4c 69 6d 69 74 29 29 3b  of(db->aLimit));
2ef32 0a 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  .  db->autoCommi
2ef33 74 20 3d 20 31 3b 0a 20 20 64 62 2d 3e 6e 65 78  t = 1;.  db->nex
2ef34 74 41 75 74 6f 76 61 63 20 3d 20 2d 31 3b 0a 20  tAutovac = -1;. 
2ef35 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a   db->nextPagesiz
2ef36 65 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 66 6c 61  e = 0;.  db->fla
2ef37 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68 6f  gs |= SQLITE_Sho
2ef38 72 74 43 6f 6c 4e 61 6d 65 73 0a 23 69 66 20 53  rtColNames.#if S
2ef39 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49  QLITE_DEFAULT_FI
2ef3a 4c 45 5f 46 4f 52 4d 41 54 3c 34 0a 20 20 20 20  LE_FORMAT<4.    
2ef3b 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 53               | S
2ef3c 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65  QLITE_LegacyFile
2ef3d 46 6d 74 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  Fmt.#endif.#ifde
2ef3e 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
2ef3f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 20  LOAD_EXTENSION. 
2ef40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef41 7c 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74  | SQLITE_LoadExt
2ef42 65 6e 73 69 6f 6e 0a 23 65 6e 64 69 66 0a 20 20  ension.#endif.  
2ef43 20 20 20 20 3b 0a 20 20 73 71 6c 69 74 65 33 48      ;.  sqlite3H
2ef44 61 73 68 49 6e 69 74 28 26 64 62 2d 3e 61 43 6f  ashInit(&db->aCo
2ef45 6c 6c 53 65 71 2c 20 30 29 3b 0a 23 69 66 6e 64  llSeq, 0);.#ifnd
2ef46 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2ef47 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71  IRTUALTABLE.  sq
2ef48 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 64  lite3HashInit(&d
2ef49 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 30 29 3b 0a  b->aModule, 0);.
2ef4a 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 70 56  #endif..  db->pV
2ef4b 66 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73  fs = sqlite3_vfs
2ef4c 5f 66 69 6e 64 28 7a 56 66 73 29 3b 0a 20 20 69  _find(zVfs);.  i
2ef4d 66 28 20 21 64 62 2d 3e 70 56 66 73 20 29 7b 0a  f( !db->pVfs ){.
2ef4e 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2ef4f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74  ERROR;.    sqlit
2ef50 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
2ef51 22 6e 6f 20 73 75 63 68 20 76 66 73 3a 20 25 73  "no such vfs: %s
2ef52 22 2c 20 7a 56 66 73 29 3b 0a 20 20 20 20 67 6f  ", zVfs);.    go
2ef53 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20  to opendb_out;. 
2ef54 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65   }..  /* Add the
2ef55 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69   default collati
2ef56 6f 6e 20 73 65 71 75 65 6e 63 65 20 42 49 4e 41  on sequence BINA
2ef57 52 59 2e 20 42 49 4e 41 52 59 20 77 6f 72 6b 73  RY. BINARY works
2ef58 20 66 6f 72 20 62 6f 74 68 20 55 54 46 2d 38 0a   for both UTF-8.
2ef59 20 20 2a 2a 20 61 6e 64 20 55 54 46 2d 31 36 2c    ** and UTF-16,
2ef5a 20 73 6f 20 61 64 64 20 61 20 76 65 72 73 69 6f   so add a versio
2ef5b 6e 20 66 6f 72 20 65 61 63 68 20 74 6f 20 61 76  n for each to av
2ef5c 6f 69 64 20 61 6e 79 20 75 6e 6e 65 63 65 73 73  oid any unnecess
2ef5d 61 72 79 0a 20 20 2a 2a 20 63 6f 6e 76 65 72 73  ary.  ** convers
2ef5e 69 6f 6e 73 2e 20 54 68 65 20 6f 6e 6c 79 20 65  ions. The only e
2ef5f 72 72 6f 72 20 74 68 61 74 20 63 61 6e 20 6f 63  rror that can oc
2ef60 63 75 72 20 68 65 72 65 20 69 73 20 61 20 6d 61  cur here is a ma
2ef61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 2e 0a  lloc() failure..
2ef62 20 20 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c    */.  createCol
2ef63 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41  lation(db, "BINA
2ef64 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  RY", SQLITE_UTF8
2ef65 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63  , 0, binCollFunc
2ef66 2c 20 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f  , 0);.  createCo
2ef67 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e  llation(db, "BIN
2ef68 41 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46  ARY", SQLITE_UTF
2ef69 31 36 42 45 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c  16BE, 0, binColl
2ef6a 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61  Func, 0);.  crea
2ef6b 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20  teCollation(db, 
2ef6c 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45  "BINARY", SQLITE
2ef6d 5f 55 54 46 31 36 4c 45 2c 20 30 2c 20 62 69 6e  _UTF16LE, 0, bin
2ef6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20  CollFunc, 0);.  
2ef6f 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
2ef70 64 62 2c 20 22 52 54 52 49 4d 22 2c 20 53 51 4c  db, "RTRIM", SQL
2ef71 49 54 45 5f 55 54 46 38 2c 20 28 76 6f 69 64 2a  ITE_UTF8, (void*
2ef72 29 31 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c  )1, binCollFunc,
2ef73 20 30 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d   0);.  if( db->m
2ef74 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2ef75 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f     goto opendb_o
2ef76 75 74 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 44  ut;.  }.  db->pD
2ef77 66 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65  fltColl = sqlite
2ef78 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
2ef79 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22 42   SQLITE_UTF8, "B
2ef7a 49 4e 41 52 59 22 2c 20 36 2c 20 30 29 3b 0a 20  INARY", 6, 0);. 
2ef7b 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 44 66   assert( db->pDf
2ef7c 6c 74 43 6f 6c 6c 21 3d 30 20 29 3b 0a 0a 20 20  ltColl!=0 );..  
2ef7d 2f 2a 20 41 6c 73 6f 20 61 64 64 20 61 20 55 54  /* Also add a UT
2ef7e 46 2d 38 20 63 61 73 65 2d 69 6e 73 65 6e 73 69  F-8 case-insensi
2ef7f 74 69 76 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  tive collation s
2ef80 65 71 75 65 6e 63 65 2e 20 2a 2f 0a 20 20 63 72  equence. */.  cr
2ef81 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62  eateCollation(db
2ef82 2c 20 22 4e 4f 43 41 53 45 22 2c 20 53 51 4c 49  , "NOCASE", SQLI
2ef83 54 45 5f 55 54 46 38 2c 20 30 2c 20 6e 6f 63 61  TE_UTF8, 0, noca
2ef84 73 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e 63 2c  seCollatingFunc,
2ef85 20 30 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 66   0);..  /* Set f
2ef86 6c 61 67 73 20 6f 6e 20 74 68 65 20 62 75 69 6c  lags on the buil
2ef87 74 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67 20 73  t-in collating s
2ef88 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 64 62  equences */.  db
2ef89 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d 3e 74 79 70  ->pDfltColl->typ
2ef8a 65 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  e = SQLITE_COLL_
2ef8b 42 49 4e 41 52 59 3b 0a 20 20 70 43 6f 6c 6c 20  BINARY;.  pColl 
2ef8c 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c  = sqlite3FindCol
2ef8d 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45 5f  lSeq(db, SQLITE_
2ef8e 55 54 46 38 2c 20 22 4e 4f 43 41 53 45 22 2c 20  UTF8, "NOCASE", 
2ef8f 36 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 6f  6, 0);.  if( pCo
2ef90 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 2d  ll ){.    pColl-
2ef91 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 43  >type = SQLITE_C
2ef92 4f 4c 4c 5f 4e 4f 43 41 53 45 3b 0a 20 20 7d 0a  OLL_NOCASE;.  }.
2ef93 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 62  .  /* Open the b
2ef94 61 63 6b 65 6e 64 20 64 61 74 61 62 61 73 65 20  ackend database 
2ef95 64 72 69 76 65 72 20 2a 2f 0a 20 20 64 62 2d 3e  driver */.  db->
2ef96 6f 70 65 6e 46 6c 61 67 73 20 3d 20 66 6c 61 67  openFlags = flag
2ef97 73 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  s;.  rc = sqlite
2ef98 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62  3BtreeFactory(db
2ef99 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 30 2c 20  , zFilename, 0, 
2ef9a 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43  SQLITE_DEFAULT_C
2ef9b 41 43 48 45 5f 53 49 5a 45 2c 20 0a 20 20 20 20  ACHE_SIZE, .    
2ef9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef9d 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c 20 53         flags | S
2ef9e 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
2ef9f 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  DB,.            
2efa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
2efa1 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b  db->aDb[0].pBt);
2efa2 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2efa3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
2efa4 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
2efa5 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
2efa6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2efa7 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  M;.    }.    sql
2efa8 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
2efa9 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f  , 0);.    goto o
2efaa 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20  pendb_out;.  }. 
2efab 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68   db->aDb[0].pSch
2efac 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63 68  ema = sqlite3Sch
2efad 65 6d 61 47 65 74 28 64 62 2c 20 64 62 2d 3e 61  emaGet(db, db->a
2efae 44 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 64 62  Db[0].pBt);.  db
2efaf 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61  ->aDb[1].pSchema
2efb0 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2efb1 47 65 74 28 64 62 2c 20 30 29 3b 0a 0a 0a 20 20  Get(db, 0);...  
2efb2 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73  /* The default s
2efb3 61 66 65 74 79 5f 6c 65 76 65 6c 20 66 6f 72 20  afety_level for 
2efb4 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
2efb5 65 20 69 73 20 27 66 75 6c 6c 27 3b 20 66 6f 72  e is 'full'; for
2efb6 20 74 68 65 20 74 65 6d 70 0a 20 20 2a 2a 20 64   the temp.  ** d
2efb7 61 74 61 62 61 73 65 20 69 74 20 69 73 20 27 4e  atabase it is 'N
2efb8 4f 4e 45 27 2e 20 54 68 69 73 20 6d 61 74 63 68  ONE'. This match
2efb9 65 73 20 74 68 65 20 70 61 67 65 72 20 6c 61 79  es the pager lay
2efba 65 72 20 64 65 66 61 75 6c 74 73 2e 20 20 0a 20  er defaults.  . 
2efbb 20 2a 2f 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d   */.  db->aDb[0]
2efbc 2e 7a 4e 61 6d 65 20 3d 20 22 6d 61 69 6e 22 3b  .zName = "main";
2efbd 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 73 61  .  db->aDb[0].sa
2efbe 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b 0a  fety_level = 3;.
2efbf 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2efc0 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 64 62 2d  MIT_TEMPDB.  db-
2efc1 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20  >aDb[1].zName = 
2efc2 22 74 65 6d 70 22 3b 0a 20 20 64 62 2d 3e 61 44  "temp";.  db->aD
2efc3 62 5b 31 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65  b[1].safety_leve
2efc4 6c 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20  l = 1;.#endif.. 
2efc5 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
2efc6 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a  ITE_MAGIC_OPEN;.
2efc7 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2efc8 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
2efc9 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20  to opendb_out;. 
2efca 20 7d 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65   }..  /* Registe
2efcb 72 20 61 6c 6c 20 62 75 69 6c 74 2d 69 6e 20 66  r all built-in f
2efcc 75 6e 63 74 69 6f 6e 73 2c 20 62 75 74 20 64 6f  unctions, but do
2efcd 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
2efce 72 65 61 64 20 74 68 65 0a 20 20 2a 2a 20 64 61  read the.  ** da
2efcf 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 79 65  tabase schema ye
2efd0 74 2e 20 54 68 69 73 20 69 73 20 64 65 6c 61 79  t. This is delay
2efd1 65 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72  ed until the fir
2efd2 73 74 20 74 69 6d 65 20 74 68 65 20 64 61 74 61  st time the data
2efd3 62 61 73 65 0a 20 20 2a 2a 20 69 73 20 61 63 63  base.  ** is acc
2efd4 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71  essed..  */.  sq
2efd5 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
2efd6 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20  QLITE_OK, 0);.  
2efd7 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 42  sqlite3RegisterB
2efd8 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28  uiltinFunctions(
2efd9 64 62 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20  db);..  /* Load 
2efda 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73  automatic extens
2efdb 69 6f 6e 73 20 2d 20 65 78 74 65 6e 73 69 6f 6e  ions - extension
2efdc 73 20 74 68 61 74 20 68 61 76 65 20 62 65 65 6e  s that have been
2efdd 20 72 65 67 69 73 74 65 72 65 64 0a 20 20 2a 2a   registered.  **
2efde 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
2efdf 65 33 5f 61 75 74 6f 6d 61 74 69 63 5f 65 78 74  e3_automatic_ext
2efe0 65 6e 73 69 6f 6e 28 29 20 41 50 49 2e 0a 20 20  ension() API..  
2efe1 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  */.  (void)sqlit
2efe2 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73  e3AutoLoadExtens
2efe3 69 6f 6e 73 28 64 62 29 3b 0a 20 20 69 66 28 20  ions(db);.  if( 
2efe4 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
2efe5 64 62 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  db)!=SQLITE_OK )
2efe6 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64  {.    goto opend
2efe7 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 23 69 66 64  b_out;.  }..#ifd
2efe8 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2efe9 5f 46 54 53 31 0a 20 20 69 66 28 20 21 64 62 2d  _FTS1.  if( !db-
2efea 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2efeb 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20  .    extern int 
2efec 73 71 6c 69 74 65 33 46 74 73 31 49 6e 69 74 28  sqlite3Fts1Init(
2efed 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72  sqlite3*);.    r
2efee 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 31 49  c = sqlite3Fts1I
2efef 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e  nit(db);.  }.#en
2eff0 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
2eff1 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 32 0a 20  TE_ENABLE_FTS2. 
2eff2 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
2eff3 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51  Failed && rc==SQ
2eff4 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65  LITE_OK ){.    e
2eff5 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65  xtern int sqlite
2eff6 33 46 74 73 32 49 6e 69 74 28 73 71 6c 69 74 65  3Fts2Init(sqlite
2eff7 33 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  3*);.    rc = sq
2eff8 6c 69 74 65 33 46 74 73 32 49 6e 69 74 28 64 62  lite3Fts2Init(db
2eff9 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23  );.  }.#endif..#
2effa 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
2effb 42 4c 45 5f 46 54 53 33 0a 20 20 69 66 28 20 21  BLE_FTS3.  if( !
2effc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2effd 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
2effe 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  K ){.    rc = sq
2efff 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 64 62  lite3Fts3Init(db
2f000 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23  );.  }.#endif..#
2f001 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
2f002 42 4c 45 5f 49 43 55 0a 20 20 69 66 28 20 21 64  BLE_ICU.  if( !d
2f003 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2f004 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
2f005 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
2f006 69 74 65 33 49 63 75 49 6e 69 74 28 64 62 29 3b  ite3IcuInit(db);
2f007 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  .  }.#endif..#if
2f008 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
2f009 45 5f 52 54 52 45 45 0a 20 20 69 66 28 20 21 64  E_RTREE.  if( !d
2f00a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2f00b 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
2f00c 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
2f00d 74 65 33 52 74 72 65 65 49 6e 69 74 28 64 62 29  te3RtreeInit(db)
2f00e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
2f00f 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2f010 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 2d   rc, 0);..  /* -
2f011 44 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  DSQLITE_DEFAULT_
2f012 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 31 20 6d  LOCKING_MODE=1 m
2f013 61 6b 65 73 20 45 58 43 4c 55 53 49 56 45 20 74  akes EXCLUSIVE t
2f014 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69  he default locki
2f015 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 2d  ng.  ** mode.  -
2f016 44 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  DSQLITE_DEFAULT_
2f017 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 30 20 6d  LOCKING_MODE=0 m
2f018 61 6b 65 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64  ake NORMAL the d
2f019 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20  efault locking. 
2f01a 20 2a 2a 20 6d 6f 64 65 2e 20 20 44 6f 69 6e 67   ** mode.  Doing
2f01b 20 6e 6f 74 68 69 6e 67 20 61 74 20 61 6c 6c 20   nothing at all 
2f01c 61 6c 73 6f 20 6d 61 6b 65 73 20 4e 4f 52 4d 41  also makes NORMA
2f01d 4c 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 20  L the default.. 
2f01e 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
2f01f 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e  E_DEFAULT_LOCKIN
2f020 47 5f 4d 4f 44 45 0a 20 20 64 62 2d 3e 64 66 6c  G_MODE.  db->dfl
2f021 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20 53 51 4c 49  tLockMode = SQLI
2f022 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49  TE_DEFAULT_LOCKI
2f023 4e 47 5f 4d 4f 44 45 3b 0a 20 20 73 71 6c 69 74  NG_MODE;.  sqlit
2f024 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
2f025 64 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 50  de(sqlite3BtreeP
2f026 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 30 5d 2e  ager(db->aDb[0].
2f027 70 42 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20  pBt),.          
2f028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f029 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
2f02a 4f 43 4b 49 4e 47 5f 4d 4f 44 45 29 3b 0a 23 65  OCKING_MODE);.#e
2f02b 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 6e 61 62 6c  ndif..  /* Enabl
2f02c 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 2d  e the lookaside-
2f02d 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d  malloc subsystem
2f02e 20 2a 2f 0a 20 20 73 65 74 75 70 4c 6f 6f 6b 61   */.  setupLooka
2f02f 73 69 64 65 28 64 62 2c 20 30 2c 20 73 71 6c 69  side(db, 0, sqli
2f030 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2f031 73 7a 4c 6f 6f 6b 61 73 69 64 65 2c 0a 20 20 20  szLookaside,.   
2f032 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f033 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2f034 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b 61 73  alConfig.nLookas
2f035 69 64 65 29 3b 0a 0a 6f 70 65 6e 64 62 5f 6f 75  ide);..opendb_ou
2f036 74 3a 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20  t:.  if( db ){. 
2f037 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d     assert( db->m
2f038 75 74 65 78 21 3d 30 20 7c 7c 20 69 73 54 68 72  utex!=0 || isThr
2f039 65 61 64 73 61 66 65 3d 3d 30 20 7c 7c 20 73 71  eadsafe==0 || sq
2f03a 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2f03b 67 2e 62 46 75 6c 6c 4d 75 74 65 78 3d 3d 30 20  g.bFullMutex==0 
2f03c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
2f03d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
2f03e 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20  utex);.  }.  rc 
2f03f 3d 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  = sqlite3_errcod
2f040 65 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d  e(db);.  if( rc=
2f041 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
2f042 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f  .    sqlite3_clo
2f043 73 65 28 64 62 29 3b 0a 20 20 20 20 64 62 20 3d  se(db);.    db =
2f044 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
2f045 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2f046 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  .    db->magic =
2f047 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49   SQLITE_MAGIC_SI
2f048 43 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70 44 62 20  CK;.  }.  *ppDb 
2f049 3d 20 64 62 3b 0a 20 20 72 65 74 75 72 6e 20 73  = db;.  return s
2f04a 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 30 2c  qlite3ApiExit(0,
2f04b 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f   rc);.}../*.** O
2f04c 70 65 6e 20 61 20 6e 65 77 20 64 61 74 61 62 61  pen a new databa
2f04d 73 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  se handle..*/.SQ
2f04e 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2f04f 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e  ite3_open(.  con
2f050 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
2f051 6d 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a  me, .  sqlite3 *
2f052 2a 70 70 44 62 20 0a 29 7b 0a 20 20 72 65 74 75  *ppDb .){.  retu
2f053 72 6e 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28  rn openDatabase(
2f054 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c  zFilename, ppDb,
2f055 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f056 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
2f057 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53  EN_READWRITE | S
2f058 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
2f059 45 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  E, 0);.}.SQLITE_
2f05a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2f05b 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74  open_v2(.  const
2f05c 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c   char *filename,
2f05d 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66     /* Database f
2f05e 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20  ilename (UTF-8) 
2f05f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70  */.  sqlite3 **p
2f060 70 44 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  pDb,         /* 
2f061 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68  OUT: SQLite db h
2f062 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66  andle */.  int f
2f063 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
2f064 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20     /* Flags */. 
2f065 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66   const char *zVf
2f066 73 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  s        /* Name
2f067 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65 20 74   of VFS module t
2f068 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a 20 20 72 65  o use */.){.  re
2f069 74 75 72 6e 20 6f 70 65 6e 44 61 74 61 62 61 73  turn openDatabas
2f06a 65 28 66 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62  e(filename, ppDb
2f06b 2c 20 66 6c 61 67 73 2c 20 7a 56 66 73 29 3b 0a  , flags, zVfs);.
2f06c 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2f06d 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
2f06e 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61  ** Open a new da
2f06f 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a  tabase handle..*
2f070 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2f071 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
2f072 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
2f073 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c  Filename, .  sql
2f074 69 74 65 33 20 2a 2a 70 70 44 62 0a 29 7b 0a 20  ite3 **ppDb.){. 
2f075 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69   char const *zFi
2f076 6c 65 6e 61 6d 65 38 3b 20 20 20 2f 2a 20 7a 46  lename8;   /* zF
2f077 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 65 64 20  ilename encoded 
2f078 69 6e 20 55 54 46 2d 38 20 69 6e 73 74 65 61 64  in UTF-8 instead
2f079 20 6f 66 20 55 54 46 2d 31 36 20 2a 2f 0a 20 20   of UTF-16 */.  
2f07a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
2f07b 56 61 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  Val;.  int rc;..
2f07c 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 6e    assert( zFilen
2f07d 61 6d 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ame );.  assert(
2f07e 20 70 70 44 62 20 29 3b 0a 20 20 2a 70 70 44 62   ppDb );.  *ppDb
2f07f 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 0;.#ifndef SQ
2f080 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
2f081 49 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  IT.  rc = sqlite
2f082 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a  3_initialize();.
2f083 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
2f084 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70  n rc;.#endif.  p
2f085 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  Val = sqlite3Val
2f086 75 65 4e 65 77 28 30 29 3b 0a 20 20 73 71 6c 69  ueNew(0);.  sqli
2f087 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70  te3ValueSetStr(p
2f088 56 61 6c 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61  Val, -1, zFilena
2f089 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  me, SQLITE_UTF16
2f08a 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53  NATIVE, SQLITE_S
2f08b 54 41 54 49 43 29 3b 0a 20 20 7a 46 69 6c 65 6e  TATIC);.  zFilen
2f08c 61 6d 65 38 20 3d 20 73 71 6c 69 74 65 33 56 61  ame8 = sqlite3Va
2f08d 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51  lueText(pVal, SQ
2f08e 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66  LITE_UTF8);.  if
2f08f 28 20 7a 46 69 6c 65 6e 61 6d 65 38 20 29 7b 0a  ( zFilename8 ){.
2f090 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 61 74      rc = openDat
2f091 61 62 61 73 65 28 7a 46 69 6c 65 6e 61 6d 65 38  abase(zFilename8
2f092 2c 20 70 70 44 62 2c 0a 20 20 20 20 20 20 20 20  , ppDb,.        
2f093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
2f094 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
2f095 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  ITE | SQLITE_OPE
2f096 4e 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a 20 20  N_CREATE, 0);.  
2f097 20 20 61 73 73 65 72 74 28 20 2a 70 70 44 62 20    assert( *ppDb 
2f098 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  || rc==SQLITE_NO
2f099 4d 45 4d 20 29 3b 0a 20 20 20 20 69 66 28 20 72  MEM );.    if( r
2f09a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
2f09b 21 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 2a  !DbHasProperty(*
2f09c 70 70 44 62 2c 20 30 2c 20 44 42 5f 53 63 68 65  ppDb, 0, DB_Sche
2f09d 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20  maLoaded) ){.   
2f09e 20 20 20 45 4e 43 28 2a 70 70 44 62 29 20 3d 20     ENC(*ppDb) = 
2f09f 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
2f0a0 56 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  VE;.    }.  }els
2f0a1 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  e{.    rc = SQLI
2f0a2 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
2f0a3 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
2f0a4 28 70 56 61 6c 29 3b 0a 0a 20 20 72 65 74 75 72  (pVal);..  retur
2f0a5 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  n sqlite3ApiExit
2f0a6 28 30 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69  (0, rc);.}.#endi
2f0a7 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2f0a8 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _UTF16 */../*.**
2f0a9 20 52 65 67 69 73 74 65 72 20 61 20 6e 65 77 20   Register a new 
2f0aa 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2f0ab 63 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ce with the data
2f0ac 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a  base handle db..
2f0ad 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2f0ae 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
2f0af 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71  _collation(.  sq
2f0b0 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f  lite3* db, .  co
2f0b1 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
2f0b2 20 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20   .  int enc, .  
2f0b3 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e  void* pCtx,.  in
2f0b4 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69  t(*xCompare)(voi
2f0b5 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2f0b6 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2f0b7 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  d*).){.  int rc;
2f0b8 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2f0b9 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2f0ba 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  );.  assert( !db
2f0bb 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2f0bc 3b 0a 20 20 72 63 20 3d 20 63 72 65 61 74 65 43  ;.  rc = createC
2f0bd 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61  ollation(db, zNa
2f0be 6d 65 2c 20 65 6e 63 2c 20 70 43 74 78 2c 20 78  me, enc, pCtx, x
2f0bf 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20 72  Compare, 0);.  r
2f0c0 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
2f0c1 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
2f0c2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2f0c3 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2f0c4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2f0c5 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e  .** Register a n
2f0c6 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ew collation seq
2f0c7 75 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64  uence with the d
2f0c8 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64  atabase handle d
2f0c9 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  b..*/.SQLITE_API
2f0ca 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
2f0cb 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
2f0cc 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c  (.  sqlite3* db,
2f0cd 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
2f0ce 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e  zName, .  int en
2f0cf 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78  c, .  void* pCtx
2f0d0 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72  ,.  int(*xCompar
2f0d1 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  e)(void*,int,con
2f0d2 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
2f0d3 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69  st void*),.  voi
2f0d4 64 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a  d(*xDel)(void*).
2f0d5 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
2f0d6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2f0d7 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
2f0d8 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61   assert( !db->ma
2f0d9 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
2f0da 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61  rc = createColla
2f0db 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  tion(db, zName, 
2f0dc 65 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70  enc, pCtx, xComp
2f0dd 61 72 65 2c 20 78 44 65 6c 29 3b 0a 20 20 72 63  are, xDel);.  rc
2f0de 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
2f0df 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
2f0e0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2f0e1 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
2f0e2 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
2f0e3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f0e4 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67  _UTF16./*.** Reg
2f0e5 69 73 74 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c  ister a new coll
2f0e6 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77  ation sequence w
2f0e7 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65  ith the database
2f0e8 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53   handle db..*/.S
2f0e9 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2f0ea 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2f0eb 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69  lation16(.  sqli
2f0ec 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73  te3* db, .  cons
2f0ed 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20  t void *zName,. 
2f0ee 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69   int enc, .  voi
2f0ef 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a  d* pCtx,.  int(*
2f0f0 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
2f0f1 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
2f0f2 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
2f0f3 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
2f0f4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72  QLITE_OK;.  char
2f0f5 20 2a 7a 4e 61 6d 65 38 3b 0a 20 20 73 71 6c 69   *zName8;.  sqli
2f0f6 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2f0f7 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
2f0f8 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
2f0f9 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a 4e 61  cFailed );.  zNa
2f0fa 6d 65 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66  me8 = sqlite3Utf
2f0fb 31 36 74 6f 38 28 64 62 2c 20 7a 4e 61 6d 65 2c  16to8(db, zName,
2f0fc 20 2d 31 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d   -1);.  if( zNam
2f0fd 65 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63  e8 ){.    rc = c
2f0fe 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
2f0ff 62 2c 20 7a 4e 61 6d 65 38 2c 20 65 6e 63 2c 20  b, zName8, enc, 
2f100 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20  pCtx, xCompare, 
2f101 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  0);.    sqlite3D
2f102 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 38  bFree(db, zName8
2f103 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71  );.  }.  rc = sq
2f104 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
2f105 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f   rc);.  sqlite3_
2f106 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2f107 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2f108 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a   rc;.}.#endif /*
2f109 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
2f10a 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  16 */../*.** Reg
2f10b 69 73 74 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f  ister a collatio
2f10c 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f  n sequence facto
2f10d 72 79 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68  ry callback with
2f10e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
2f10f 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c  ndle.** db. Repl
2f110 61 63 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ace any previous
2f111 6c 79 20 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c  ly installed col
2f112 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2f113 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49  factory..*/.SQLI
2f114 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2f115 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
2f116 64 65 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ded(.  sqlite3 *
2f117 64 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70 43 6f  db, .  void *pCo
2f118 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a 20 20  llNeededArg, .  
2f119 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65  void(*xCollNeede
2f11a 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  d)(void*,sqlite3
2f11b 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63  *,int eTextRep,c
2f11c 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 7b 0a 20  onst char*).){. 
2f11d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2f11e 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2f11f 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64  .  db->xCollNeed
2f120 65 64 20 3d 20 78 43 6f 6c 6c 4e 65 65 64 65 64  ed = xCollNeeded
2f121 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  ;.  db->xCollNee
2f122 64 65 64 31 36 20 3d 20 30 3b 0a 20 20 64 62 2d  ded16 = 0;.  db-
2f123 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20  >pCollNeededArg 
2f124 3d 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67  = pCollNeededArg
2f125 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2f126 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2f127 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
2f128 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
2f129 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
2f12a 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  TF16./*.** Regis
2f12b 74 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20  ter a collation 
2f12c 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79  sequence factory
2f12d 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 74   callback with t
2f12e 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
2f12f 6c 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c 61 63  le.** db. Replac
2f130 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 6c 79  e any previously
2f131 20 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c 6c 61   installed colla
2f132 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 61  tion sequence fa
2f133 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ctory..*/.SQLITE
2f134 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2f135 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
2f136 64 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  d16(.  sqlite3 *
2f137 64 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70 43 6f  db, .  void *pCo
2f138 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a 20 20  llNeededArg, .  
2f139 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65  void(*xCollNeede
2f13a 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  d16)(void*,sqlit
2f13b 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
2f13c 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b  ,const void*).){
2f13d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2f13e 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2f13f 29 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65  );.  db->xCollNe
2f140 65 64 65 64 20 3d 20 30 3b 0a 20 20 64 62 2d 3e  eded = 0;.  db->
2f141 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d 20  xCollNeeded16 = 
2f142 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 3b 0a 20  xCollNeeded16;. 
2f143 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64   db->pCollNeeded
2f144 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65 64 65  Arg = pCollNeede
2f145 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  dArg;.  sqlite3_
2f146 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2f147 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2f148 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
2f149 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2f14a 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 23 69  MIT_UTF16 */..#i
2f14b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2f14c 54 5f 47 4c 4f 42 41 4c 52 45 43 4f 56 45 52 0a  T_GLOBALRECOVER.
2f14d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2f14e 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f  MIT_DEPRECATED./
2f14f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2f150 6f 6e 20 69 73 20 6e 6f 77 20 61 6e 20 61 6e 61  on is now an ana
2f151 63 68 72 6f 6e 69 73 6d 2e 20 49 74 20 75 73 65  chronism. It use
2f152 64 20 74 6f 20 62 65 20 75 73 65 64 20 74 6f 20  d to be used to 
2f153 72 65 63 6f 76 65 72 20 66 72 6f 6d 20 61 0a 2a  recover from a.*
2f154 2a 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75  * malloc() failu
2f155 72 65 2c 20 62 75 74 20 53 51 4c 69 74 65 20 6e  re, but SQLite n
2f156 6f 77 20 64 6f 65 73 20 74 68 69 73 20 61 75 74  ow does this aut
2f157 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2f 0a 53  omatically..*/.S
2f158 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2f159 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63  lite3_global_rec
2f15a 6f 76 65 72 28 76 6f 69 64 29 7b 0a 20 20 72 65  over(void){.  re
2f15b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2f15c 7d 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  }.#endif.#endif.
2f15d 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73  ./*.** Test to s
2f15e 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ee whether or no
2f15f 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  t the database c
2f160 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20  onnection is in 
2f161 61 75 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 6d 6f  autocommit.** mo
2f162 64 65 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45  de.  Return TRUE
2f163 20 69 66 20 69 74 20 69 73 20 61 6e 64 20 46 41   if it is and FA
2f164 4c 53 45 20 69 66 20 6e 6f 74 2e 20 20 41 75 74  LSE if not.  Aut
2f165 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
2f166 6f 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75 6c 74  on.** by default
2f167 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 69 73  .  Autocommit is
2f168 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 42   disabled by a B
2f169 45 47 49 4e 20 73 74 61 74 65 6d 65 6e 74 20 61  EGIN statement a
2f16a 6e 64 20 72 65 65 6e 61 62 6c 65 64 0a 2a 2a 20  nd reenabled.** 
2f16b 62 79 20 74 68 65 20 6e 65 78 74 20 43 4f 4d 4d  by the next COMM
2f16c 49 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2e 0a  IT or ROLLBACK..
2f16d 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54 48 49 53 20  **.******* THIS 
2f16e 49 53 20 41 4e 20 45 58 50 45 52 49 4d 45 4e 54  IS AN EXPERIMENT
2f16f 41 4c 20 41 50 49 20 41 4e 44 20 49 53 20 53 55  AL API AND IS SU
2f170 42 4a 45 43 54 20 54 4f 20 43 48 41 4e 47 45 20  BJECT TO CHANGE 
2f171 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  ******.*/.SQLITE
2f172 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2f173 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28  _get_autocommit(
2f174 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2f175 72 65 74 75 72 6e 20 64 62 2d 3e 61 75 74 6f 43  return db->autoC
2f176 6f 6d 6d 69 74 3b 0a 7d 0a 0a 23 69 66 64 65 66  ommit;.}..#ifdef
2f177 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
2f178 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
2f179 67 20 72 6f 75 74 69 6e 65 20 69 73 20 73 75 62  g routine is sub
2f17a 74 69 74 75 74 65 64 20 66 6f 72 20 63 6f 6e 73  tituted for cons
2f17b 74 61 6e 74 20 53 51 4c 49 54 45 5f 43 4f 52 52  tant SQLITE_CORR
2f17c 55 50 54 20 69 6e 0a 2a 2a 20 64 65 62 75 67 67  UPT in.** debugg
2f17d 69 6e 67 20 62 75 69 6c 64 73 2e 20 20 54 68 69  ing builds.  Thi
2f17e 73 20 70 72 6f 76 69 64 65 73 20 61 20 77 61 79  s provides a way
2f17f 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70   to set a breakp
2f180 6f 69 6e 74 20 66 6f 72 20 77 68 65 6e 0a 2a 2a  oint for when.**
2f181 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66   corruption is f
2f182 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a  irst detected..*
2f183 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2f184 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 72 72   int sqlite3Corr
2f185 75 70 74 28 76 6f 69 64 29 7b 0a 20 20 72 65 74  upt(void){.  ret
2f186 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
2f187 50 54 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  PT;.}.#endif..#i
2f188 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2f189 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a  T_DEPRECATED./*.
2f18a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 63 6f 6e  ** This is a con
2f18b 76 65 6e 69 65 6e 63 65 20 72 6f 75 74 69 6e 65  venience routine
2f18c 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65   that makes sure
2f18d 20 74 68 61 74 20 61 6c 6c 20 74 68 72 65 61 64   that all thread
2f18e 2d 73 70 65 63 69 66 69 63 0a 2a 2a 20 64 61 74  -specific.** dat
2f18f 61 20 66 6f 72 20 74 68 69 73 20 74 68 72 65 61  a for this threa
2f190 64 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c  d has been deall
2f191 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51  ocated..**.** SQ
2f192 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75  Lite no longer u
2f193 73 65 73 20 74 68 72 65 61 64 2d 73 70 65 63 69  ses thread-speci
2f194 66 69 63 20 64 61 74 61 20 73 6f 20 74 68 69 73  fic data so this
2f195 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 77 20   routine is now 
2f196 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 20 49 74 20  a.** no-op.  It 
2f197 69 73 20 72 65 74 61 69 6e 65 64 20 66 6f 72 20  is retained for 
2f198 68 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d 70 61  historical compa
2f199 74 69 62 69 6c 69 74 79 2e 0a 2a 2f 0a 53 51 4c  tibility..*/.SQL
2f19a 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2f19b 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61  ite3_thread_clea
2f19c 6e 75 70 28 76 6f 69 64 29 7b 0a 7d 0a 23 65 6e  nup(void){.}.#en
2f19d 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
2f19e 6e 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69  n meta informati
2f19f 6f 6e 20 61 62 6f 75 74 20 61 20 73 70 65 63 69  on about a speci
2f1a0 66 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20  fic column of a 
2f1a1 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a  database table..
2f1a2 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 69  ** See comment i
2f1a3 6e 20 73 71 6c 69 74 65 33 2e 68 20 28 73 71 6c  n sqlite3.h (sql
2f1a4 69 74 65 2e 68 2e 69 6e 29 20 66 6f 72 20 64 65  ite.h.in) for de
2f1a5 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  tails..*/.#ifdef
2f1a6 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
2f1a7 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 53  OLUMN_METADATA.S
2f1a8 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2f1a9 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75  lite3_table_colu
2f1aa 6d 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73  mn_metadata(.  s
2f1ab 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2f1ac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
2f1ad 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
2f1ae 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2f1af 2a 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20  *zDbName,       
2f1b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d   /* Database nam
2f1b1 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  e or NULL */.  c
2f1b2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c  onst char *zTabl
2f1b3 65 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61  eName,     /* Ta
2f1b4 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f  ble name */.  co
2f1b5 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d  nst char *zColum
2f1b6 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c  nName,    /* Col
2f1b7 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68  umn name */.  ch
2f1b8 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74  ar const **pzDat
2f1b9 61 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54  aType,    /* OUT
2f1ba 50 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61  PUT: Declared da
2f1bb 74 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61  ta type */.  cha
2f1bc 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c  r const **pzColl
2f1bd 53 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50  Seq,     /* OUTP
2f1be 55 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65  UT: Collation se
2f1bf 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20  quence name */. 
2f1c0 20 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20   int *pNotNull, 
2f1c1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f1c2 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20  OUTPUT: True if 
2f1c3 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61  NOT NULL constra
2f1c4 69 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20  int exists */.  
2f1c5 69 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79  int *pPrimaryKey
2f1c6 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ,           /* O
2f1c7 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63  UTPUT: True if c
2f1c8 6f 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b  olumn part of PK
2f1c9 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f   */.  int *pAuto
2f1ca 69 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20  inc             
2f1cb 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
2f1cc 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61  e if column is a
2f1cd 75 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f  uto-increment */
2f1ce 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
2f1cf 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20  char *zErrMsg = 
2f1d0 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  0;.  Table *pTab
2f1d1 20 3d 20 30 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a   = 0;.  Column *
2f1d2 70 43 6f 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20  pCol = 0;.  int 
2f1d3 69 43 6f 6c 3b 0a 0a 20 20 63 68 61 72 20 63 6f  iCol;..  char co
2f1d4 6e 73 74 20 2a 7a 44 61 74 61 54 79 70 65 20 3d  nst *zDataType =
2f1d5 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   0;.  char const
2f1d6 20 2a 7a 43 6f 6c 6c 53 65 71 20 3d 20 30 3b 0a   *zCollSeq = 0;.
2f1d7 20 20 69 6e 74 20 6e 6f 74 6e 75 6c 6c 20 3d 20    int notnull = 
2f1d8 30 3b 0a 20 20 69 6e 74 20 70 72 69 6d 61 72 79  0;.  int primary
2f1d9 6b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 61  key = 0;.  int a
2f1da 75 74 6f 69 6e 63 20 3d 20 30 3b 0a 0a 20 20 2f  utoinc = 0;..  /
2f1db 2a 20 45 6e 73 75 72 65 20 74 68 65 20 64 61 74  * Ensure the dat
2f1dc 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73  abase schema has
2f1dd 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a   been loaded */.
2f1de 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2f1df 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2f1e0 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65  ;.  (void)sqlite
2f1e1 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
2f1e2 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
2f1e3 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63 20  erAll(db);.  rc 
2f1e4 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62  = sqlite3Init(db
2f1e5 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73  , &zErrMsg);.  s
2f1e6 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
2f1e7 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 20 53  All(db);.  if( S
2f1e8 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a  QLITE_OK!=rc ){.
2f1e9 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
2f1ea 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f  ut;.  }..  /* Lo
2f1eb 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 69  cate the table i
2f1ec 6e 20 71 75 65 73 74 69 6f 6e 20 2a 2f 0a 20 20  n question */.  
2f1ed 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
2f1ee 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
2f1ef 6c 65 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29  leName, zDbName)
2f1f0 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c  ;.  if( !pTab ||
2f1f1 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
2f1f2 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a  {.    pTab = 0;.
2f1f3 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
2f1f4 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69  ut;.  }..  /* Fi
2f1f5 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f  nd the column fo
2f1f6 72 20 77 68 69 63 68 20 69 6e 66 6f 20 69 73 20  r which info is 
2f1f7 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 69  requested */.  i
2f1f8 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69  f( sqlite3IsRowi
2f1f9 64 28 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29  d(zColumnName) )
2f1fa 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61  {.    iCol = pTa
2f1fb 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 69 66  b->iPKey;.    if
2f1fc 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20  ( iCol>=0 ){.   
2f1fd 20 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d     pCol = &pTab-
2f1fe 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20  >aCol[iCol];.   
2f1ff 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
2f200 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c  for(iCol=0; iCol
2f201 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f  <pTab->nCol; iCo
2f202 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c  l++){.      pCol
2f203 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69   = &pTab->aCol[i
2f204 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  Col];.      if( 
2f205 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
2f206 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a  p(pCol->zName, z
2f207 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29 7b 0a 20  ColumnName) ){. 
2f208 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2f209 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2f20a 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e  if( iCol==pTab->
2f20b 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 70 54  nCol ){.      pT
2f20c 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f  ab = 0;.      go
2f20d 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  to error_out;.  
2f20e 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68    }.  }..  /* Th
2f20f 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
2f210 6b 20 73 74 6f 72 65 73 20 74 68 65 20 6d 65 74  k stores the met
2f211 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  a information th
2f212 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  at will be retur
2f213 6e 65 64 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20  ned.  ** to the 
2f214 63 61 6c 6c 65 72 20 69 6e 20 6c 6f 63 61 6c 20  caller in local 
2f215 76 61 72 69 61 62 6c 65 73 20 7a 44 61 74 61 54  variables zDataT
2f216 79 70 65 2c 20 7a 43 6f 6c 6c 53 65 71 2c 20 6e  ype, zCollSeq, n
2f217 6f 74 6e 75 6c 6c 2c 20 70 72 69 6d 61 72 79 6b  otnull, primaryk
2f218 65 79 0a 20 20 2a 2a 20 61 6e 64 20 61 75 74 6f  ey.  ** and auto
2f219 69 6e 63 2e 20 41 74 20 74 68 69 73 20 70 6f 69  inc. At this poi
2f21a 6e 74 20 74 68 65 72 65 20 61 72 65 20 74 77 6f  nt there are two
2f21b 20 70 6f 73 73 69 62 69 6c 69 74 69 65 73 3a 0a   possibilities:.
2f21c 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 20 31    ** .  **     1
2f21d 2e 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20  . The specified 
2f21e 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77 61 73 20  column name was 
2f21f 72 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f 72  rowid", "oid" or
2f220 20 22 5f 72 6f 77 69 64 5f 22 20 0a 20 20 2a 2a   "_rowid_" .  **
2f221 20 20 20 20 20 20 20 20 61 6e 64 20 74 68 65 72          and ther
2f222 65 20 69 73 20 6e 6f 20 65 78 70 6c 69 63 69 74  e is no explicit
2f223 6c 79 20 64 65 63 6c 61 72 65 64 20 49 50 4b 20  ly declared IPK 
2f224 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2a 0a 20 20  column. .  **.  
2f225 2a 2a 20 20 20 20 20 32 2e 20 54 68 65 20 74 61  **     2. The ta
2f226 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65  ble is not a vie
2f227 77 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e  w and the column
2f228 20 6e 61 6d 65 20 69 64 65 6e 74 69 66 69 65 64   name identified
2f229 20 61 6e 20 0a 20 20 2a 2a 20 20 20 20 20 20 20   an .  **       
2f22a 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c   explicitly decl
2f22b 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 43 6f 70  ared column. Cop
2f22c 79 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69  y meta informati
2f22d 6f 6e 20 66 72 6f 6d 20 2a 70 43 6f 6c 2e 0a 20  on from *pCol.. 
2f22e 20 2a 2f 20 0a 20 20 69 66 28 20 70 43 6f 6c 20   */ .  if( pCol 
2f22f 29 7b 0a 20 20 20 20 7a 44 61 74 61 54 79 70 65  ){.    zDataType
2f230 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a   = pCol->zType;.
2f231 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d 20 70      zCollSeq = p
2f232 43 6f 6c 2d 3e 7a 43 6f 6c 6c 3b 0a 20 20 20 20  Col->zColl;.    
2f233 6e 6f 74 6e 75 6c 6c 20 3d 20 70 43 6f 6c 2d 3e  notnull = pCol->
2f234 6e 6f 74 4e 75 6c 6c 21 3d 30 3b 0a 20 20 20 20  notNull!=0;.    
2f235 70 72 69 6d 61 72 79 6b 65 79 20 20 3d 20 70 43  primarykey  = pC
2f236 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 21 3d 30  ol->isPrimKey!=0
2f237 3b 0a 20 20 20 20 61 75 74 6f 69 6e 63 20 3d 20  ;.    autoinc = 
2f238 70 54 61 62 2d 3e 69 50 4b 65 79 3d 3d 69 43 6f  pTab->iPKey==iCo
2f239 6c 20 26 26 20 28 70 54 61 62 2d 3e 74 61 62 46  l && (pTab->tabF
2f23a 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e  lags & TF_Autoin
2f23b 63 72 65 6d 65 6e 74 29 21 3d 30 3b 0a 20 20 7d  crement)!=0;.  }
2f23c 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 54  else{.    zDataT
2f23d 79 70 65 20 3d 20 22 49 4e 54 45 47 45 52 22 3b  ype = "INTEGER";
2f23e 0a 20 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20  .    primarykey 
2f23f 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  = 1;.  }.  if( !
2f240 7a 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20 20  zCollSeq ){.    
2f241 7a 43 6f 6c 6c 53 65 71 20 3d 20 22 42 49 4e 41  zCollSeq = "BINA
2f242 52 59 22 3b 0a 20 20 7d 0a 0a 65 72 72 6f 72 5f  RY";.  }..error_
2f243 6f 75 74 3a 0a 20 20 28 76 6f 69 64 29 73 71 6c  out:.  (void)sql
2f244 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
2f245 29 3b 0a 0a 20 20 2f 2a 20 57 68 65 74 68 65 72  );..  /* Whether
2f246 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 61   the function ca
2f247 6c 6c 20 73 75 63 63 65 65 64 65 64 20 6f 72 20  ll succeeded or 
2f248 66 61 69 6c 65 64 2c 20 73 65 74 20 74 68 65 20  failed, set the 
2f249 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72  output parameter
2f24a 73 0a 20 20 2a 2a 20 74 6f 20 77 68 61 74 65 76  s.  ** to whatev
2f24b 65 72 20 74 68 65 69 72 20 6c 6f 63 61 6c 20 63  er their local c
2f24c 6f 75 6e 74 65 72 70 61 72 74 73 20 63 6f 6e 74  ounterparts cont
2f24d 61 69 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ain. If an error
2f24e 20 64 69 64 20 6f 63 63 75 72 2c 0a 20 20 2a 2a   did occur,.  **
2f24f 20 74 68 69 73 20 68 61 73 20 74 68 65 20 65 66   this has the ef
2f250 66 65 63 74 20 6f 66 20 7a 65 72 6f 69 6e 67 20  fect of zeroing 
2f251 61 6c 6c 20 6f 75 74 70 75 74 20 70 61 72 61 6d  all output param
2f252 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  eters..  */.  if
2f253 28 20 70 7a 44 61 74 61 54 79 70 65 20 29 20 2a  ( pzDataType ) *
2f254 70 7a 44 61 74 61 54 79 70 65 20 3d 20 7a 44 61  pzDataType = zDa
2f255 74 61 54 79 70 65 3b 0a 20 20 69 66 28 20 70 7a  taType;.  if( pz
2f256 43 6f 6c 6c 53 65 71 20 29 20 2a 70 7a 43 6f 6c  CollSeq ) *pzCol
2f257 6c 53 65 71 20 3d 20 7a 43 6f 6c 6c 53 65 71 3b  lSeq = zCollSeq;
2f258 0a 20 20 69 66 28 20 70 4e 6f 74 4e 75 6c 6c 20  .  if( pNotNull 
2f259 29 20 2a 70 4e 6f 74 4e 75 6c 6c 20 3d 20 6e 6f  ) *pNotNull = no
2f25a 74 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 70 50 72  tnull;.  if( pPr
2f25b 69 6d 61 72 79 4b 65 79 20 29 20 2a 70 50 72 69  imaryKey ) *pPri
2f25c 6d 61 72 79 4b 65 79 20 3d 20 70 72 69 6d 61 72  maryKey = primar
2f25d 79 6b 65 79 3b 0a 20 20 69 66 28 20 70 41 75 74  ykey;.  if( pAut
2f25e 6f 69 6e 63 20 29 20 2a 70 41 75 74 6f 69 6e 63  oinc ) *pAutoinc
2f25f 20 3d 20 61 75 74 6f 69 6e 63 3b 0a 0a 20 20 69   = autoinc;..  i
2f260 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63  f( SQLITE_OK==rc
2f261 20 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20   && !pTab ){.   
2f262 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2f263 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  b, zErrMsg);.   
2f264 20 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74   zErrMsg = sqlit
2f265 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e  e3MPrintf(db, "n
2f266 6f 20 73 75 63 68 20 74 61 62 6c 65 20 63 6f 6c  o such table col
2f267 75 6d 6e 3a 20 25 73 2e 25 73 22 2c 20 7a 54 61  umn: %s.%s", zTa
2f268 62 6c 65 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  bleName,.       
2f269 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b 0a 20   zColumnName);. 
2f26a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
2f26b 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  RROR;.  }.  sqli
2f26c 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c  te3Error(db, rc,
2f26d 20 28 7a 45 72 72 4d 73 67 3f 22 25 73 22 3a 30   (zErrMsg?"%s":0
2f26e 29 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73  ), zErrMsg);.  s
2f26f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2f270 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 63 20   zErrMsg);.  rc 
2f271 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
2f272 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
2f273 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
2f274 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
2f275 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
2f276 66 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66  f../*.** Sleep f
2f277 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c  or a little whil
2f278 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61  e.  Return the a
2f279 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c  mount of time sl
2f27a 65 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ept..*/.SQLITE_A
2f27b 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
2f27c 6c 65 65 70 28 69 6e 74 20 6d 73 29 7b 0a 20 20  leep(int ms){.  
2f27d 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
2f27e 73 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70  s;.  int rc;.  p
2f27f 56 66 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66  Vfs = sqlite3_vf
2f280 73 5f 66 69 6e 64 28 30 29 3b 0a 20 20 69 66 28  s_find(0);.  if(
2f281 20 70 56 66 73 3d 3d 30 20 29 20 72 65 74 75 72   pVfs==0 ) retur
2f282 6e 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20  n 0;..  /* This 
2f283 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 69  function works i
2f284 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2c 20  n milliseconds, 
2f285 62 75 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69  but the underlyi
2f286 6e 67 20 4f 73 53 6c 65 65 70 28 29 20 0a 20 20  ng OsSleep() .  
2f287 2a 2a 20 41 50 49 20 75 73 65 73 20 6d 69 63 72  ** API uses micr
2f288 6f 73 65 63 6f 6e 64 73 2e 20 48 65 6e 63 65 20  oseconds. Hence 
2f289 74 68 65 20 31 30 30 30 27 73 2e 0a 20 20 2a 2f  the 1000's..  */
2f28a 0a 20 20 72 63 20 3d 20 28 73 71 6c 69 74 65 33  .  rc = (sqlite3
2f28b 4f 73 53 6c 65 65 70 28 70 56 66 73 2c 20 31 30  OsSleep(pVfs, 10
2f28c 30 30 2a 6d 73 29 2f 31 30 30 30 29 3b 0a 20 20  00*ms)/1000);.  
2f28d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2f28e 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69  .** Enable or di
2f28f 73 61 62 6c 65 20 74 68 65 20 65 78 74 65 6e 64  sable the extend
2f290 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 2e  ed result codes.
2f291 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2f292 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  nt sqlite3_exten
2f293 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73  ded_result_codes
2f294 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
2f295 74 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69  t onoff){.  sqli
2f296 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2f297 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62  db->mutex);.  db
2f298 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 6f 6e 6f 66  ->errMask = onof
2f299 66 20 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a  f ? 0xffffffff :
2f29a 20 30 78 66 66 3b 0a 20 20 73 71 6c 69 74 65 33   0xff;.  sqlite3
2f29b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
2f29c 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
2f29d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
2f29e 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
2f29f 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65   xFileControl me
2f2a0 74 68 6f 64 20 6f 6e 20 61 20 70 61 72 74 69 63  thod on a partic
2f2a1 75 6c 61 72 20 64 61 74 61 62 61 73 65 2e 0a 2a  ular database..*
2f2a2 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2f2a3 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f   sqlite3_file_co
2f2a4 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 20 2a 64  ntrol(sqlite3 *d
2f2a5 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2f2a6 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20  DbName, int op, 
2f2a7 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 69  void *pArg){.  i
2f2a8 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45  nt rc = SQLITE_E
2f2a9 52 52 4f 52 3b 0a 20 20 69 6e 74 20 69 44 62 3b  RROR;.  int iDb;
2f2aa 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2f2ab 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2f2ac 29 3b 0a 20 20 69 66 28 20 7a 44 62 4e 61 6d 65  );.  if( zDbName
2f2ad 3d 3d 30 20 29 7b 0a 20 20 20 20 69 44 62 20 3d  ==0 ){.    iDb =
2f2ae 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
2f2af 20 66 6f 72 28 69 44 62 3d 30 3b 20 69 44 62 3c   for(iDb=0; iDb<
2f2b0 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b  db->nDb; iDb++){
2f2b1 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d  .      if( strcm
2f2b2 70 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  p(db->aDb[iDb].z
2f2b3 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29 3d 3d  Name, zDbName)==
2f2b4 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d  0 ) break;.    }
2f2b5 0a 20 20 7d 0a 20 20 69 66 28 20 69 44 62 3c 64  .  }.  if( iDb<d
2f2b6 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 42 74  b->nDb ){.    Bt
2f2b7 72 65 65 20 2a 70 42 74 72 65 65 20 3d 20 64 62  ree *pBtree = db
2f2b8 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a  ->aDb[iDb].pBt;.
2f2b9 20 20 20 20 69 66 28 20 70 42 74 72 65 65 20 29      if( pBtree )
2f2ba 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70  {.      Pager *p
2f2bb 50 61 67 65 72 3b 0a 20 20 20 20 20 20 73 71 6c  Pager;.      sql
2f2bc 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 0a 20  ite3_file *fd;. 
2f2bd 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
2f2be 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a  eEnter(pBtree);.
2f2bf 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73        pPager = s
2f2c0 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
2f2c1 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 20 20  (pBtree);.      
2f2c2 61 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d  assert( pPager!=
2f2c3 30 20 29 3b 0a 20 20 20 20 20 20 66 64 20 3d 20  0 );.      fd = 
2f2c4 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65  sqlite3PagerFile
2f2c5 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
2f2c6 61 73 73 65 72 74 28 20 66 64 21 3d 30 20 29 3b  assert( fd!=0 );
2f2c7 0a 20 20 20 20 20 20 69 66 28 20 66 64 2d 3e 70  .      if( fd->p
2f2c8 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20  Methods ){.     
2f2c9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
2f2ca 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 66 64 2c  sFileControl(fd,
2f2cb 20 6f 70 2c 20 70 41 72 67 29 3b 0a 20 20 20 20   op, pArg);.    
2f2cc 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2f2cd 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72  3BtreeLeave(pBtr
2f2ce 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ee);.    }.  }. 
2f2cf 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2f2d0 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2f2d1 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20 20 20  .  return rc;   
2f2d2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66  .}../*.** Interf
2f2d3 61 63 65 20 74 6f 20 74 68 65 20 74 65 73 74 69  ace to the testi
2f2d4 6e 67 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 53 51 4c  ng logic..*/.SQL
2f2d5 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2f2d6 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
2f2d7 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20  (int op, ...){. 
2f2d8 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 23 69 66   int rc = 0;.#if
2f2d9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f2da 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 20 20  _BUILTIN_TEST.  
2f2db 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 76 61  va_list ap;.  va
2f2dc 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a  _start(ap, op);.
2f2dd 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
2f2de 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 53  .    /*.    ** S
2f2df 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ave the current 
2f2e0 73 74 61 74 65 20 6f 66 20 74 68 65 20 50 52 4e  state of the PRN
2f2e1 47 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  G..    */.    ca
2f2e2 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  se SQLITE_TESTCT
2f2e3 52 4c 5f 50 52 4e 47 5f 53 41 56 45 3a 20 7b 0a  RL_PRNG_SAVE: {.
2f2e4 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e        sqlite3Prn
2f2e5 67 53 61 76 65 53 74 61 74 65 28 29 3b 0a 20 20  gSaveState();.  
2f2e6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2f2e7 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
2f2e8 52 65 73 74 6f 72 65 20 74 68 65 20 73 74 61 74  Restore the stat
2f2e9 65 20 6f 66 20 74 68 65 20 50 52 4e 47 20 74 6f  e of the PRNG to
2f2ea 20 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 20   the last state 
2f2eb 73 61 76 65 64 20 75 73 69 6e 67 0a 20 20 20 20  saved using.    
2f2ec 2a 2a 20 50 52 4e 47 5f 53 41 56 45 2e 20 20 49  ** PRNG_SAVE.  I
2f2ed 66 20 50 52 4e 47 5f 53 41 56 45 20 68 61 73 20  f PRNG_SAVE has 
2f2ee 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65  never before bee
2f2ef 6e 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 0a 20  n called, then. 
2f2f0 20 20 20 2a 2a 20 74 68 69 73 20 76 65 72 62 20     ** this verb 
2f2f1 61 63 74 73 20 6c 69 6b 65 20 50 52 4e 47 5f 52  acts like PRNG_R
2f2f2 45 53 45 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ESET..    */.   
2f2f3 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
2f2f4 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f  TCTRL_PRNG_RESTO
2f2f5 52 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  RE: {.      sqli
2f2f6 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53 74  te3PrngRestoreSt
2f2f7 61 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72 65  ate();.      bre
2f2f8 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ak;.    }..    /
2f2f9 2a 0a 20 20 20 20 2a 2a 20 52 65 73 65 74 20 74  *.    ** Reset t
2f2fa 68 65 20 50 52 4e 47 20 62 61 63 6b 20 74 6f 20  he PRNG back to 
2f2fb 69 74 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65  its uninitialize
2f2fc 64 20 73 74 61 74 65 2e 20 20 54 68 65 20 6e 65  d state.  The ne
2f2fd 78 74 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74  xt call.    ** t
2f2fe 6f 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d  o sqlite3_random
2f2ff 6e 65 73 73 28 29 20 77 69 6c 6c 20 72 65 73 65  ness() will rese
2f300 65 64 20 74 68 65 20 50 52 4e 47 20 75 73 69 6e  ed the PRNG usin
2f301 67 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 0a  g a single call.
2f302 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 78 52      ** to the xR
2f303 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64  andomness method
2f304 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
2f305 56 46 53 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  VFS..    */.    
2f306 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
2f307 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54 3a  CTRL_PRNG_RESET:
2f308 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
2f309 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 29  PrngResetState()
2f30a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2f30b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
2f30c 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73   **  sqlite3_tes
2f30d 74 5f 63 6f 6e 74 72 6f 6c 28 42 49 54 56 45 43  t_control(BITVEC
2f30e 5f 54 45 53 54 2c 20 73 69 7a 65 2c 20 70 72 6f  _TEST, size, pro
2f30f 67 72 61 6d 29 0a 20 20 20 20 2a 2a 0a 20 20 20  gram).    **.   
2f310 20 2a 2a 20 52 75 6e 20 61 20 74 65 73 74 20 61   ** Run a test a
2f311 67 61 69 6e 73 74 20 61 20 42 69 74 76 65 63 20  gainst a Bitvec 
2f312 6f 62 6a 65 63 74 20 6f 66 20 73 69 7a 65 2e 20  object of size. 
2f313 20 54 68 65 20 70 72 6f 67 72 61 6d 20 61 72 67   The program arg
2f314 75 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 69 73 20  ument.    ** is 
2f315 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74 65  an array of inte
2f316 67 65 72 73 20 74 68 61 74 20 64 65 66 69 6e 65  gers that define
2f317 73 20 74 68 65 20 74 65 73 74 2e 20 20 52 65 74  s the test.  Ret
2f318 75 72 6e 20 2d 31 20 6f 6e 20 61 0a 20 20 20 20  urn -1 on a.    
2f319 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** memory alloca
2f31a 74 69 6f 6e 20 65 72 72 6f 72 2c 20 30 20 6f 6e  tion error, 0 on
2f31b 20 73 75 63 63 65 73 73 2c 20 6f 72 20 6e 6f 6e   success, or non
2f31c 2d 7a 65 72 6f 20 66 6f 72 20 61 6e 20 65 72 72  -zero for an err
2f31d 6f 72 2e 0a 20 20 20 20 2a 2a 20 53 65 65 20 74  or..    ** See t
2f31e 68 65 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  he sqlite3Bitvec
2f31f 42 75 69 6c 74 69 6e 54 65 73 74 28 29 20 66 6f  BuiltinTest() fo
2f320 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
2f321 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  ormation..    */
2f322 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2f323 5f 54 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43  _TESTCTRL_BITVEC
2f324 5f 54 45 53 54 3a 20 7b 0a 20 20 20 20 20 20 69  _TEST: {.      i
2f325 6e 74 20 73 7a 20 3d 20 76 61 5f 61 72 67 28 61  nt sz = va_arg(a
2f326 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 69  p, int);.      i
2f327 6e 74 20 2a 61 50 72 6f 67 20 3d 20 76 61 5f 61  nt *aProg = va_a
2f328 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20  rg(ap, int*);.  
2f329 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2f32a 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73  BitvecBuiltinTes
2f32b 74 28 73 7a 2c 20 61 50 72 6f 67 29 3b 0a 20 20  t(sz, aProg);.  
2f32c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2f32d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
2f32e 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
2f32f 6e 74 72 6f 6c 28 42 45 4e 49 47 4e 5f 4d 41 4c  ntrol(BENIGN_MAL
2f330 4c 4f 43 5f 48 4f 4f 4b 53 2c 20 78 42 65 67 69  LOC_HOOKS, xBegi
2f331 6e 2c 20 78 45 6e 64 29 0a 20 20 20 20 2a 2a 0a  n, xEnd).    **.
2f332 20 20 20 20 2a 2a 20 52 65 67 69 73 74 65 72 20      ** Register 
2f333 68 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 74 6f  hooks to call to
2f334 20 69 6e 64 69 63 61 74 65 20 77 68 69 63 68 20   indicate which 
2f335 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
2f336 73 20 0a 20 20 20 20 2a 2a 20 61 72 65 20 62 65  s .    ** are be
2f337 6e 69 67 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  nign..    */.   
2f338 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
2f339 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c  TCTRL_BENIGN_MAL
2f33a 4c 4f 43 5f 48 4f 4f 4b 53 3a 20 7b 0a 20 20 20  LOC_HOOKS: {.   
2f33b 20 20 20 74 79 70 65 64 65 66 20 76 6f 69 64 20     typedef void 
2f33c 28 2a 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29  (*void_function)
2f33d 28 76 6f 69 64 29 3b 0a 20 20 20 20 20 20 76 6f  (void);.      vo
2f33e 69 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65 6e  id_function xBen
2f33f 69 67 6e 42 65 67 69 6e 3b 0a 20 20 20 20 20 20  ignBegin;.      
2f340 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42  void_function xB
2f341 65 6e 69 67 6e 45 6e 64 3b 0a 20 20 20 20 20 20  enignEnd;.      
2f342 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 76  xBenignBegin = v
2f343 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 5f 66  a_arg(ap, void_f
2f344 75 6e 63 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20  unction);.      
2f345 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 76 61 5f  xBenignEnd = va_
2f346 61 72 67 28 61 70 2c 20 76 6f 69 64 5f 66 75 6e  arg(ap, void_fun
2f347 63 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20 73 71  ction);.      sq
2f348 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f  lite3BenignMallo
2f349 63 48 6f 6f 6b 73 28 78 42 65 6e 69 67 6e 42 65  cHooks(xBenignBe
2f34a 67 69 6e 2c 20 78 42 65 6e 69 67 6e 45 6e 64 29  gin, xBenignEnd)
2f34b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2f34c 20 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e     }.  }.  va_en
2f34d 64 28 61 70 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  d(ap);.#endif /*
2f34e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
2f34f 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 20 20 72  LTIN_TEST */.  r
2f350 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a  eturn rc;.}../**
2f351 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2f352 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a   of main.c *****
2f353 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f354 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f355 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2f356 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2f357 69 6e 20 66 69 6c 65 20 66 74 73 33 2e 63 20 2a  in file fts3.c *
2f358 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f359 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f35a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2f35b 2a 2a 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a  ** 2006 Oct 10.*
2f35c 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2f35d 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2f35e 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2f35f 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
2f360 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2f361 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2f362 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2f363 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2f364 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2f365 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2f366 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
2f367 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
2f368 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
2f369 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
2f36a 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
2f36b 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
2f36c 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
2f36d 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
2f36e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f36f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f371 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f372 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  **.**.** This is
2f373 20 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c   an SQLite modul
2f374 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66  e implementing f
2f375 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 2e  ull-text search.
2f376 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63  .*/../*.** The c
2f377 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
2f378 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65   is only compile
2f379 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  d if:.**.**     
2f37a 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c  * The FTS3 modul
2f37b 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74  e is being built
2f37c 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e   as an extension
2f37d 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68  .**       (in wh
2f37e 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f  ich case SQLITE_
2f37f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69  CORE is not defi
2f380 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20  ned), or.**.**  
2f381 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f     * The FTS3 mo
2f382 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75  dule is being bu
2f383 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72  ilt into the cor
2f384 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51  e of.**       SQ
2f385 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63  Lite (in which c
2f386 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ase SQLITE_ENABL
2f387 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65  E_FTS3 is define
2f388 64 29 2e 0a 2a 2f 0a 0a 2f 2a 20 54 4f 44 4f 28  d)..*/../* TODO(
2f389 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20  shess) Consider 
2f38a 65 78 70 6f 72 74 69 6e 67 20 74 68 69 73 20 63  exporting this c
2f38b 6f 6d 6d 65 6e 74 20 74 6f 20 61 6e 20 48 54 4d  omment to an HTM
2f38c 4c 20 66 69 6c 65 20 6f 72 20 74 68 65 0a 2a 2a  L file or the.**
2f38d 20 77 69 6b 69 2e 0a 2a 2f 0a 2f 2a 20 54 68 65   wiki..*/./* The
2f38e 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78   full-text index
2f38f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20   is stored in a 
2f390 73 65 72 69 65 73 20 6f 66 20 62 2b 74 72 65 65  series of b+tree
2f391 20 28 2d 6c 69 6b 65 29 0a 2a 2a 20 73 74 72 75   (-like).** stru
2f392 63 74 75 72 65 73 20 63 61 6c 6c 65 64 20 73 65  ctures called se
2f393 67 6d 65 6e 74 73 20 77 68 69 63 68 20 6d 61 70  gments which map
2f394 20 74 65 72 6d 73 20 74 6f 20 64 6f 63 6c 69 73   terms to doclis
2f395 74 73 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72 75  ts.  The.** stru
2f396 63 74 75 72 65 73 20 61 72 65 20 6c 69 6b 65 20  ctures are like 
2f397 62 2b 74 72 65 65 73 20 69 6e 20 6c 61 79 6f 75  b+trees in layou
2f398 74 2c 20 62 75 74 20 61 72 65 20 63 6f 6e 73 74  t, but are const
2f399 72 75 63 74 65 64 20 66 72 6f 6d 20 74 68 65 0a  ructed from the.
2f39a 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 20 69 6e 20  ** bottom up in 
2f39b 6f 70 74 69 6d 61 6c 20 66 61 73 68 69 6f 6e 20  optimal fashion 
2f39c 61 6e 64 20 61 72 65 20 6e 6f 74 20 75 70 64 61  and are not upda
2f39d 74 61 62 6c 65 2e 20 20 53 69 6e 63 65 20 74 72  table.  Since tr
2f39e 65 65 73 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74  ees.** are built
2f39f 20 66 72 6f 6d 20 74 68 65 20 62 6f 74 74 6f 6d   from the bottom
2f3a0 20 75 70 2c 20 74 68 69 6e 67 73 20 77 69 6c 6c   up, things will
2f3a1 20 62 65 20 64 65 73 63 72 69 62 65 64 20 66 72   be described fr
2f3a2 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74 6f 6d  om the.** bottom
2f3a3 20 75 70 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20   up..**.**.**** 
2f3a4 56 61 72 69 6e 74 73 20 2a 2a 2a 2a 0a 2a 2a 20  Varints ****.** 
2f3a5 54 68 65 20 62 61 73 69 63 20 75 6e 69 74 20 6f  The basic unit o
2f3a6 66 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 20  f encoding is a 
2f3a7 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
2f3a8 69 6e 74 65 67 65 72 20 63 61 6c 6c 65 64 20 61  integer called a
2f3a9 0a 2a 2a 20 76 61 72 69 6e 74 2e 20 20 57 65 20  .** varint.  We 
2f3aa 65 6e 63 6f 64 65 20 76 61 72 69 61 62 6c 65 2d  encode variable-
2f3ab 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 20  length integers 
2f3ac 69 6e 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  in little-endian
2f3ad 20 6f 72 64 65 72 0a 2a 2a 20 75 73 69 6e 67 20   order.** using 
2f3ae 73 65 76 65 6e 20 62 69 74 73 20 2a 20 70 65 72  seven bits * per
2f3af 20 62 79 74 65 20 61 73 20 66 6f 6c 6c 6f 77 73   byte as follows
2f3b0 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20  :.**.** KEY:.** 
2f3b1 20 20 20 20 20 20 20 20 41 20 3d 20 30 78 78 78          A = 0xxx
2f3b2 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20 6f  xxxx    7 bits o
2f3b3 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66  f data and one f
2f3b4 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20  lag bit.**      
2f3b5 20 20 20 42 20 3d 20 31 78 78 78 78 78 78 78 20     B = 1xxxxxxx 
2f3b6 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 74     7 bits of dat
2f3b7 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62  a and one flag b
2f3b8 69 74 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73  it.**.**  7 bits
2f3b9 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20   - A.** 14 bits 
2f3ba 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20  - BA.** 21 bits 
2f3bb 2d 20 42 42 41 0a 2a 2a 20 61 6e 64 20 73 6f 20  - BBA.** and so 
2f3bc 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  on..**.** This i
2f3bd 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 68  s identical to h
2f3be 6f 77 20 73 71 6c 69 74 65 20 65 6e 63 6f 64 65  ow sqlite encode
2f3bf 73 20 76 61 72 69 6e 74 73 20 28 73 65 65 20 75  s varints (see u
2f3c0 74 69 6c 2e 63 29 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a  til.c)..**.**.**
2f3c1 2a 2a 20 44 6f 63 75 6d 65 6e 74 20 6c 69 73 74  ** Document list
2f3c2 73 20 2a 2a 2a 2a 0a 2a 2a 20 41 20 64 6f 63 6c  s ****.** A docl
2f3c3 69 73 74 20 28 64 6f 63 75 6d 65 6e 74 20 6c 69  ist (document li
2f3c4 73 74 29 20 68 6f 6c 64 73 20 61 20 64 6f 63 69  st) holds a doci
2f3c5 64 2d 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66  d-sorted list of
2f3c6 20 68 69 74 73 20 66 6f 72 20 61 0a 2a 2a 20 67   hits for a.** g
2f3c7 69 76 65 6e 20 74 65 72 6d 2e 20 20 44 6f 63 6c  iven term.  Docl
2f3c8 69 73 74 73 20 68 6f 6c 64 20 64 6f 63 69 64 73  ists hold docids
2f3c9 2c 20 61 6e 64 20 63 61 6e 20 6f 70 74 69 6f 6e  , and can option
2f3ca 61 6c 6c 79 20 61 73 73 6f 63 69 61 74 65 0a 2a  ally associate.*
2f3cb 2a 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e  * token position
2f3cc 73 20 61 6e 64 20 6f 66 66 73 65 74 73 20 77 69  s and offsets wi
2f3cd 74 68 20 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a  th docids..**.**
2f3ce 20 41 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f   A DL_POSITIONS_
2f3cf 4f 46 46 53 45 54 53 20 64 6f 63 6c 69 73 74 20  OFFSETS doclist 
2f3d0 69 73 20 73 74 6f 72 65 64 20 6c 69 6b 65 20 74  is stored like t
2f3d1 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 61 72 72 61 79  his:.**.** array
2f3d2 20 7b 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 64   {.**   varint d
2f3d3 6f 63 69 64 3b 0a 2a 2a 20 20 20 61 72 72 61 79  ocid;.**   array
2f3d4 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
2f3d5 20 20 28 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74    (position list
2f3d6 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 30 29 0a 2a   for column 0).*
2f3d7 2a 20 20 20 20 20 76 61 72 69 6e 74 20 70 6f 73  *     varint pos
2f3d8 69 74 69 6f 6e 3b 20 20 20 20 20 28 64 65 6c 74  ition;     (delt
2f3d9 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20  a from previous 
2f3da 70 6f 73 69 74 69 6f 6e 20 70 6c 75 73 20 50 4f  position plus PO
2f3db 53 5f 42 41 53 45 29 0a 2a 2a 20 20 20 20 20 76  S_BASE).**     v
2f3dc 61 72 69 6e 74 20 73 74 61 72 74 4f 66 66 73 65  arint startOffse
2f3dd 74 3b 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20  t;  (delta from 
2f3de 70 72 65 76 69 6f 75 73 20 73 74 61 72 74 4f 66  previous startOf
2f3df 66 73 65 74 29 0a 2a 2a 20 20 20 20 20 76 61 72  fset).**     var
2f3e0 69 6e 74 20 65 6e 64 4f 66 66 73 65 74 3b 20 20  int endOffset;  
2f3e1 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 73 74    (delta from st
2f3e2 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20 20  artOffset).**   
2f3e3 7d 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a  }.**   array {.*
2f3e4 2a 20 20 20 20 20 76 61 72 69 6e 74 20 50 4f 53  *     varint POS
2f3e5 5f 43 4f 4c 55 4d 4e 3b 20 20 20 28 6d 61 72 6b  _COLUMN;   (mark
2f3e6 73 20 73 74 61 72 74 20 6f 66 20 70 6f 73 69 74  s start of posit
2f3e7 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 6e 65 77  ion list for new
2f3e8 20 63 6f 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20 20   column).**     
2f3e9 76 61 72 69 6e 74 20 63 6f 6c 75 6d 6e 3b 20 20  varint column;  
2f3ea 20 20 20 20 20 28 69 6e 64 65 78 20 6f 66 20 6e       (index of n
2f3eb 65 77 20 63 6f 6c 75 6d 6e 29 0a 2a 2a 20 20 20  ew column).**   
2f3ec 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20    array {.**    
2f3ed 20 20 20 76 61 72 69 6e 74 20 70 6f 73 69 74 69     varint positi
2f3ee 6f 6e 3b 20 20 20 28 64 65 6c 74 61 20 66 72 6f  on;   (delta fro
2f3ef 6d 20 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74  m previous posit
2f3f0 69 6f 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53  ion plus POS_BAS
2f3f1 45 29 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69  E).**       vari
2f3f2 6e 74 20 73 74 61 72 74 4f 66 66 73 65 74 3b 28  nt startOffset;(
2f3f3 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69  delta from previ
2f3f4 6f 75 73 20 73 74 61 72 74 4f 66 66 73 65 74 29  ous startOffset)
2f3f5 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69 6e 74  .**       varint
2f3f6 20 65 6e 64 4f 66 66 73 65 74 3b 20 20 28 64 65   endOffset;  (de
2f3f7 6c 74 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66  lta from startOf
2f3f8 66 73 65 74 29 0a 2a 2a 20 20 20 20 20 7d 0a 2a  fset).**     }.*
2f3f9 2a 20 20 20 7d 0a 2a 2a 20 20 20 76 61 72 69 6e  *   }.**   varin
2f3fa 74 20 50 4f 53 5f 45 4e 44 3b 20 20 20 20 20 20  t POS_END;      
2f3fb 20 20 28 6d 61 72 6b 73 20 65 6e 64 20 6f 66 20    (marks end of 
2f3fc 70 6f 73 69 74 69 6f 6e 73 20 66 6f 72 20 74 68  positions for th
2f3fd 69 73 20 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20  is document..** 
2f3fe 7d 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 61 72  }.**.** Here, ar
2f3ff 72 61 79 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20  ray { X } means 
2f400 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63  zero or more occ
2f401 75 72 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61  urrences of X, a
2f402 64 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65  djacent in.** me
2f403 6d 6f 72 79 2e 20 20 41 20 22 70 6f 73 69 74 69  mory.  A "positi
2f404 6f 6e 22 20 69 73 20 61 6e 20 69 6e 64 65 78 20  on" is an index 
2f405 6f 66 20 61 20 74 6f 6b 65 6e 20 69 6e 20 74 68  of a token in th
2f406 65 20 74 6f 6b 65 6e 20 73 74 72 65 61 6d 0a 2a  e token stream.*
2f407 2a 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74  * generated by t
2f408 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2c 20 77 68  he tokenizer, wh
2f409 69 6c 65 20 61 6e 20 22 6f 66 66 73 65 74 22 20  ile an "offset" 
2f40a 69 73 20 61 20 62 79 74 65 20 6f 66 66 73 65 74  is a byte offset
2f40b 2c 0a 2a 2a 20 62 6f 74 68 20 62 61 73 65 64 20  ,.** both based 
2f40c 61 74 20 30 2e 20 20 4e 6f 74 65 20 74 68 61 74  at 0.  Note that
2f40d 20 50 4f 53 5f 45 4e 44 20 61 6e 64 20 50 4f 53   POS_END and POS
2f40e 5f 43 4f 4c 55 4d 4e 20 6f 63 63 75 72 20 69 6e  _COLUMN occur in
2f40f 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6c 6f 67   the.** same log
2f410 69 63 61 6c 20 70 6c 61 63 65 20 61 73 20 74 68  ical place as th
2f411 65 20 70 6f 73 69 74 69 6f 6e 20 65 6c 65 6d 65  e position eleme
2f412 6e 74 2c 20 61 6e 64 20 61 63 74 20 61 73 20 73  nt, and act as s
2f413 65 6e 74 69 6e 61 6c 73 0a 2a 2a 20 65 6e 64 69  entinals.** endi
2f414 6e 67 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c 69  ng a position li
2f415 73 74 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20  st array..**.** 
2f416 41 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 64  A DL_POSITIONS d
2f417 6f 63 6c 69 73 74 20 6f 6d 69 74 73 20 74 68 65  oclist omits the
2f418 20 73 74 61 72 74 4f 66 66 73 65 74 20 61 6e 64   startOffset and
2f419 20 65 6e 64 4f 66 66 73 65 74 0a 2a 2a 20 69 6e   endOffset.** in
2f41a 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 20 44 4c  formation.  A DL
2f41b 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20  _DOCIDS doclist 
2f41c 6f 6d 69 74 73 20 62 6f 74 68 20 74 68 65 20 70  omits both the p
2f41d 6f 73 69 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 6f  osition and.** o
2f41e 66 66 73 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f  ffset informatio
2f41f 6e 2c 20 62 65 63 6f 6d 69 6e 67 20 61 6e 20 61  n, becoming an a
2f420 72 72 61 79 20 6f 66 20 76 61 72 69 6e 74 2d 65  rray of varint-e
2f421 6e 63 6f 64 65 64 20 64 6f 63 69 64 73 2e 0a 2a  ncoded docids..*
2f422 2a 0a 2a 2a 20 4f 6e 2d 64 69 73 6b 20 64 61 74  *.** On-disk dat
2f423 61 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 74  a is stored as t
2f424 79 70 65 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  ype DL_DEFAULT, 
2f425 73 6f 20 77 65 20 64 6f 6e 27 74 20 73 65 72 69  so we don't seri
2f426 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 74 79 70  alize.** the typ
2f427 65 2e 20 20 44 75 65 20 74 6f 20 68 6f 77 20 64  e.  Due to how d
2f428 65 6c 65 74 69 6f 6e 20 69 73 20 69 6d 70 6c 65  eletion is imple
2f429 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20 73 65  mented in the se
2f42a 67 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 73 79  gmentation.** sy
2f42b 73 74 65 6d 2c 20 6f 6e 2d 64 69 73 6b 20 64 6f  stem, on-disk do
2f42c 63 6c 69 73 74 73 20 4d 55 53 54 20 73 74 6f 72  clists MUST stor
2f42d 65 20 61 74 20 6c 65 61 73 74 20 70 6f 73 69 74  e at least posit
2f42e 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a  ions..**.**.****
2f42f 20 53 65 67 6d 65 6e 74 20 6c 65 61 66 20 6e 6f   Segment leaf no
2f430 64 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d  des ****.** Segm
2f431 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 73  ent leaf nodes s
2f432 74 6f 72 65 20 74 65 72 6d 73 20 61 6e 64 20 64  tore terms and d
2f433 6f 63 6c 69 73 74 73 2c 20 6f 72 64 65 72 65 64  oclists, ordered
2f434 20 62 79 20 74 65 72 6d 2e 20 20 4c 65 61 66 0a   by term.  Leaf.
2f435 2a 2a 20 6e 6f 64 65 73 20 61 72 65 20 77 72 69  ** nodes are wri
2f436 74 74 65 6e 20 75 73 69 6e 67 20 4c 65 61 66 57  tten using LeafW
2f437 72 69 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20  riter, and read 
2f438 75 73 69 6e 67 20 4c 65 61 66 52 65 61 64 65 72  using LeafReader
2f439 20 28 74 6f 0a 2a 2a 20 69 74 65 72 61 74 65 20   (to.** iterate 
2f43a 74 68 72 6f 75 67 68 20 61 20 73 69 6e 67 6c 65  through a single
2f43b 20 6c 65 61 66 20 6e 6f 64 65 27 73 20 64 61 74   leaf node's dat
2f43c 61 29 20 61 6e 64 20 4c 65 61 76 65 73 52 65 61  a) and LeavesRea
2f43d 64 65 72 20 28 74 6f 0a 2a 2a 20 69 74 65 72 61  der (to.** itera
2f43e 74 65 20 74 68 72 6f 75 67 68 20 61 20 73 65 67  te through a seg
2f43f 6d 65 6e 74 27 73 20 65 6e 74 69 72 65 20 6c 65  ment's entire le
2f440 61 66 20 6c 61 79 65 72 29 2e 20 20 4c 65 61 66  af layer).  Leaf
2f441 20 6e 6f 64 65 73 20 68 61 76 65 0a 2a 2a 20 74   nodes have.** t
2f442 68 65 20 66 6f 72 6d 61 74 3a 0a 2a 2a 0a 2a 2a  he format:.**.**
2f443 20 76 61 72 69 6e 74 20 69 48 65 69 67 68 74 3b   varint iHeight;
2f444 20 20 20 20 20 20 20 20 20 20 20 20 20 28 68 65               (he
2f445 69 67 68 74 20 66 72 6f 6d 20 6c 65 61 66 20 6c  ight from leaf l
2f446 65 76 65 6c 2c 20 61 6c 77 61 79 73 20 30 29 0a  evel, always 0).
2f447 2a 2a 20 76 61 72 69 6e 74 20 6e 54 65 72 6d 3b  ** varint nTerm;
2f448 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2f449 6c 65 6e 67 74 68 20 6f 66 20 66 69 72 73 74 20  length of first 
2f44a 74 65 72 6d 29 0a 2a 2a 20 63 68 61 72 20 70 54  term).** char pT
2f44b 65 72 6d 5b 6e 54 65 72 6d 5d 3b 20 20 20 20 20  erm[nTerm];     
2f44c 20 20 20 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66       (content of
2f44d 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20   first term).** 
2f44e 76 61 72 69 6e 74 20 6e 44 6f 63 6c 69 73 74 3b  varint nDoclist;
2f44f 20 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e              (len
2f450 67 74 68 20 6f 66 20 74 65 72 6d 27 73 20 61 73  gth of term's as
2f451 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74  sociated doclist
2f452 29 0a 2a 2a 20 63 68 61 72 20 70 44 6f 63 6c 69  ).** char pDocli
2f453 73 74 5b 6e 44 6f 63 6c 69 73 74 5d 3b 20 20 20  st[nDoclist];   
2f454 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20 64 6f 63   (content of doc
2f455 6c 69 73 74 29 0a 2a 2a 20 61 72 72 61 79 20 7b  list).** array {
2f456 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2f457 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f458 28 66 75 72 74 68 65 72 20 74 65 72 6d 73 20 61  (further terms a
2f459 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64  re delta-encoded
2f45a 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 50  ).**   varint nP
2f45b 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  refix;          
2f45c 20 28 6c 65 6e 67 74 68 20 6f 66 20 70 72 65 66   (length of pref
2f45d 69 78 20 73 68 61 72 65 64 20 77 69 74 68 20 70  ix shared with p
2f45e 72 65 76 69 6f 75 73 20 74 65 72 6d 29 0a 2a 2a  revious term).**
2f45f 20 20 20 76 61 72 69 6e 74 20 6e 53 75 66 66 69     varint nSuffi
2f460 78 3b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65  x;           (le
2f461 6e 67 74 68 20 6f 66 20 75 6e 73 68 61 72 65 64  ngth of unshared
2f462 20 73 75 66 66 69 78 29 0a 2a 2a 20 20 20 63 68   suffix).**   ch
2f463 61 72 20 70 54 65 72 6d 53 75 66 66 69 78 5b 6e  ar pTermSuffix[n
2f464 53 75 66 66 69 78 5d 3b 28 75 6e 73 68 61 72 65  Suffix];(unshare
2f465 64 20 73 75 66 66 69 78 20 6f 66 20 6e 65 78 74  d suffix of next
2f466 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69   term).**   vari
2f467 6e 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20  nt nDoclist;    
2f468 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
2f469 20 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61 74   term's associat
2f46a 65 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 20  ed doclist).**  
2f46b 20 63 68 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e   char pDoclist[n
2f46c 44 6f 63 6c 69 73 74 5d 3b 20 20 28 63 6f 6e 74  Doclist];  (cont
2f46d 65 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29 0a  ent of doclist).
2f46e 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c  ** }.**.** Here,
2f46f 20 61 72 72 61 79 20 7b 20 58 20 7d 20 6d 65 61   array { X } mea
2f470 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  ns zero or more 
2f471 6f 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58  occurrences of X
2f472 2c 20 61 64 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a  , adjacent in.**
2f473 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4c   memory..**.** L
2f474 65 61 66 20 6e 6f 64 65 73 20 61 72 65 20 62 72  eaf nodes are br
2f475 6f 6b 65 6e 20 69 6e 74 6f 20 62 6c 6f 63 6b 73  oken into blocks
2f476 20 77 68 69 63 68 20 61 72 65 20 73 74 6f 72 65   which are store
2f477 64 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79 20 69  d contiguously i
2f478 6e 0a 2a 2a 20 74 68 65 20 25 5f 73 65 67 6d 65  n.** the %_segme
2f479 6e 74 73 20 74 61 62 6c 65 20 69 6e 20 73 6f 72  nts table in sor
2f47a 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 69 73  ted order.  This
2f47b 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e   means that when
2f47c 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 61   the end.** of a
2f47d 20 6e 6f 64 65 20 69 73 20 72 65 61 63 68 65 64   node is reached
2f47e 2c 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20  , the next term 
2f47f 69 73 20 69 6e 20 74 68 65 20 6e 6f 64 65 20 77  is in the node w
2f480 69 74 68 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20  ith the next.** 
2f481 67 72 65 61 74 65 72 20 6e 6f 64 65 20 69 64 2e  greater node id.
2f482 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20  .**.** New data 
2f483 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20  is spilled to a 
2f484 6e 65 77 20 6c 65 61 66 20 6e 6f 64 65 20 77 68  new leaf node wh
2f485 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e  en the current n
2f486 6f 64 65 0a 2a 2a 20 65 78 63 65 65 64 73 20 4c  ode.** exceeds L
2f487 45 41 46 5f 4d 41 58 20 62 79 74 65 73 20 28 64  EAF_MAX bytes (d
2f488 65 66 61 75 6c 74 20 32 30 34 38 29 2e 20 20 4e  efault 2048).  N
2f489 65 77 20 64 61 74 61 20 77 68 69 63 68 20 69 74  ew data which it
2f48a 73 65 6c 66 20 69 73 0a 2a 2a 20 6c 61 72 67 65  self is.** large
2f48b 72 20 74 68 61 6e 20 53 54 41 4e 44 41 4c 4f 4e  r than STANDALON
2f48c 45 5f 4d 49 4e 20 28 64 65 66 61 75 6c 74 20 31  E_MIN (default 1
2f48d 30 32 34 29 20 69 73 20 70 6c 61 63 65 64 20 69  024) is placed i
2f48e 6e 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a  n a standalone.*
2f48f 2a 20 6e 6f 64 65 20 28 61 20 6c 65 61 66 20 6e  * node (a leaf n
2f490 6f 64 65 20 77 69 74 68 20 61 20 73 69 6e 67 6c  ode with a singl
2f491 65 20 74 65 72 6d 20 61 6e 64 20 64 6f 63 6c 69  e term and docli
2f492 73 74 29 2e 20 20 54 68 65 20 67 6f 61 6c 20 6f  st).  The goal o
2f493 66 0a 2a 2a 20 74 68 65 73 65 20 73 65 74 74 69  f.** these setti
2f494 6e 67 73 20 69 73 20 74 6f 20 70 61 63 6b 20 74  ngs is to pack t
2f495 6f 67 65 74 68 65 72 20 67 72 6f 75 70 73 20 6f  ogether groups o
2f496 66 20 73 6d 61 6c 6c 20 64 6f 63 6c 69 73 74 73  f small doclists
2f497 20 77 68 69 6c 65 0a 2a 2a 20 6d 61 6b 69 6e 67   while.** making
2f498 20 69 74 20 65 66 66 69 63 69 65 6e 74 20 74 6f   it efficient to
2f499 20 64 69 72 65 63 74 6c 79 20 61 63 63 65 73 73   directly access
2f49a 20 6c 61 72 67 65 20 64 6f 63 6c 69 73 74 73 2e   large doclists.
2f49b 20 20 54 68 65 0a 2a 2a 20 61 73 73 75 6d 70 74    The.** assumpt
2f49c 69 6f 6e 20 69 73 20 74 68 61 74 20 6c 61 72 67  ion is that larg
2f49d 65 20 64 6f 63 6c 69 73 74 73 20 72 65 70 72 65  e doclists repre
2f49e 73 65 6e 74 20 74 65 72 6d 73 20 77 68 69 63 68  sent terms which
2f49f 20 61 72 65 20 6d 6f 72 65 0a 2a 2a 20 6c 69 6b   are more.** lik
2f4a0 65 6c 79 20 74 6f 20 62 65 20 71 75 65 72 79 20  ely to be query 
2f4a1 74 61 72 67 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  targets..**.** T
2f4a2 4f 44 4f 28 73 68 65 73 73 29 20 49 74 20 6d 61  ODO(shess) It ma
2f4a3 79 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72 20  y be useful for 
2f4a4 62 6c 6f 63 6b 69 6e 67 20 64 65 63 69 73 69 6f  blocking decisio
2f4a5 6e 73 20 74 6f 20 62 65 20 6d 6f 72 65 0a 2a 2a  ns to be more.**
2f4a6 20 64 79 6e 61 6d 69 63 2e 20 20 46 6f 72 20 69   dynamic.  For i
2f4a7 6e 73 74 61 6e 63 65 2c 20 69 74 20 6d 61 79 20  nstance, it may 
2f4a8 6d 61 6b 65 20 6d 6f 72 65 20 73 65 6e 73 65 20  make more sense 
2f4a9 74 6f 20 68 61 76 65 20 61 20 32 2e 35 6b 20 6c  to have a 2.5k l
2f4aa 65 61 66 0a 2a 2a 20 6e 6f 64 65 20 72 61 74 68  eaf.** node rath
2f4ab 65 72 20 74 68 61 6e 20 73 70 6c 69 74 74 69 6e  er than splittin
2f4ac 67 20 69 6e 74 6f 20 32 6b 20 61 6e 64 20 2e 35  g into 2k and .5
2f4ad 6b 20 6e 6f 64 65 73 2e 20 20 4d 79 20 69 6e 74  k nodes.  My int
2f4ae 75 69 74 69 6f 6e 20 69 73 0a 2a 2a 20 74 68 61  uition is.** tha
2f4af 74 20 74 68 69 73 20 6d 69 67 68 74 20 65 78 74  t this might ext
2f4b0 65 6e 64 20 74 68 72 6f 75 67 68 20 32 78 20 6f  end through 2x o
2f4b1 72 20 34 78 20 74 68 65 20 70 61 67 65 73 69 7a  r 4x the pagesiz
2f4b2 65 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65  e..**.**.**** Se
2f4b3 67 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72 20 6e  gment interior n
2f4b4 6f 64 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 65 67  odes ****.** Seg
2f4b5 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f  ment interior no
2f4b6 64 65 73 20 73 74 6f 72 65 20 62 6c 6f 63 6b 69  des store blocki
2f4b7 64 73 20 66 6f 72 20 73 75 62 74 72 65 65 20 6e  ds for subtree n
2f4b8 6f 64 65 73 20 61 6e 64 20 74 65 72 6d 73 0a 2a  odes and terms.*
2f4b9 2a 20 74 6f 20 64 65 73 63 72 69 62 65 20 77 68  * to describe wh
2f4ba 61 74 20 64 61 74 61 20 69 73 20 73 74 6f 72 65  at data is store
2f4bb 64 20 62 79 20 74 68 65 20 65 61 63 68 20 73 75  d by the each su
2f4bc 62 74 72 65 65 2e 20 20 49 6e 74 65 72 69 6f 72  btree.  Interior
2f4bd 0a 2a 2a 20 6e 6f 64 65 73 20 61 72 65 20 77 72  .** nodes are wr
2f4be 69 74 74 65 6e 20 75 73 69 6e 67 20 49 6e 74 65  itten using Inte
2f4bf 72 69 6f 72 57 72 69 74 65 72 2c 20 61 6e 64 20  riorWriter, and 
2f4c0 72 65 61 64 20 75 73 69 6e 67 0a 2a 2a 20 49 6e  read using.** In
2f4c1 74 65 72 69 6f 72 52 65 61 64 65 72 2e 20 20 49  teriorReader.  I
2f4c2 6e 74 65 72 69 6f 72 57 72 69 74 65 72 73 20 61  nteriorWriters a
2f4c3 72 65 20 63 72 65 61 74 65 64 20 61 73 20 6e 65  re created as ne
2f4c4 65 64 65 64 20 77 68 65 6e 0a 2a 2a 20 53 65 67  eded when.** Seg
2f4c5 6d 65 6e 74 57 72 69 74 65 72 20 63 72 65 61 74  mentWriter creat
2f4c6 65 73 20 6e 65 77 20 6c 65 61 66 20 6e 6f 64 65  es new leaf node
2f4c7 73 2c 20 6f 72 20 77 68 65 6e 20 61 6e 20 69 6e  s, or when an in
2f4c8 74 65 72 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 69  terior node.** i
2f4c9 74 73 65 6c 66 20 67 72 6f 77 73 20 74 6f 6f 20  tself grows too 
2f4ca 62 69 67 20 61 6e 64 20 6d 75 73 74 20 62 65 20  big and must be 
2f4cb 73 70 6c 69 74 2e 20 20 54 68 65 20 66 6f 72 6d  split.  The form
2f4cc 61 74 20 6f 66 20 69 6e 74 65 72 69 6f 72 0a 2a  at of interior.*
2f4cd 2a 20 6e 6f 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 76  * nodes:.**.** v
2f4ce 61 72 69 6e 74 20 69 48 65 69 67 68 74 3b 20 20  arint iHeight;  
2f4cf 20 20 20 20 20 20 20 20 20 28 68 65 69 67 68 74           (height
2f4d0 20 66 72 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c   from leaf level
2f4d1 2c 20 61 6c 77 61 79 73 20 3e 30 29 0a 2a 2a 20  , always >0).** 
2f4d2 76 61 72 69 6e 74 20 69 42 6c 6f 63 6b 69 64 3b  varint iBlockid;
2f4d3 20 20 20 20 20 20 20 20 20 20 28 62 6c 6f 63 6b            (block
2f4d4 20 69 64 20 6f 66 20 6e 6f 64 65 27 73 20 6c 65   id of node's le
2f4d5 66 74 6d 6f 73 74 20 73 75 62 74 72 65 65 29 0a  ftmost subtree).
2f4d6 2a 2a 20 6f 70 74 69 6f 6e 61 6c 20 7b 0a 2a 2a  ** optional {.**
2f4d7 20 20 20 76 61 72 69 6e 74 20 6e 54 65 72 6d 3b     varint nTerm;
2f4d8 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67             (leng
2f4d9 74 68 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  th of first term
2f4da 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 54 65 72  ).**   char pTer
2f4db 6d 5b 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20 28  m[nTerm];      (
2f4dc 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74  content of first
2f4dd 20 74 65 72 6d 29 0a 2a 2a 20 20 20 61 72 72 61   term).**   arra
2f4de 79 20 7b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  y {.**          
2f4df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4e0 20 20 20 20 20 20 28 66 75 72 74 68 65 72 20 74        (further t
2f4e1 65 72 6d 73 20 61 72 65 20 64 65 6c 74 61 2d 65  erms are delta-e
2f4e2 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20 20 20 76  ncoded).**     v
2f4e3 61 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20  arint nPrefix;  
2f4e4 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
2f4e5 68 20 6f 66 20 73 68 61 72 65 64 20 70 72 65 66  h of shared pref
2f4e6 69 78 20 77 69 74 68 20 70 72 65 76 69 6f 75 73  ix with previous
2f4e7 20 74 65 72 6d 29 0a 2a 2a 20 20 20 20 20 76 61   term).**     va
2f4e8 72 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20  rint nSuffix;   
2f4e9 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68           (length
2f4ea 20 6f 66 20 75 6e 73 68 61 72 65 64 20 73 75 66   of unshared suf
2f4eb 66 69 78 29 0a 2a 2a 20 20 20 20 20 63 68 61 72  fix).**     char
2f4ec 20 70 54 65 72 6d 53 75 66 66 69 78 5b 6e 53 75   pTermSuffix[nSu
2f4ed 66 66 69 78 5d 3b 20 28 75 6e 73 68 61 72 65 64  ffix]; (unshared
2f4ee 20 73 75 66 66 69 78 20 6f 66 20 6e 65 78 74 20   suffix of next 
2f4ef 74 65 72 6d 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20  term).**   }.** 
2f4f0 7d 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 6f 70  }.**.** Here, op
2f4f1 74 69 6f 6e 61 6c 20 7b 20 58 20 7d 20 6d 65 61  tional { X } mea
2f4f2 6e 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 65  ns an optional e
2f4f3 6c 65 6d 65 6e 74 2c 20 77 68 69 6c 65 20 61 72  lement, while ar
2f4f4 72 61 79 20 7b 20 58 20 7d 0a 2a 2a 20 6d 65 61  ray { X }.** mea
2f4f5 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  ns zero or more 
2f4f6 6f 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58  occurrences of X
2f4f7 2c 20 61 64 6a 61 63 65 6e 74 20 69 6e 20 6d 65  , adjacent in me
2f4f8 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69  mory..**.** An i
2f4f9 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 65 6e 63  nterior node enc
2f4fa 6f 64 65 73 20 6e 20 74 65 72 6d 73 20 73 65 70  odes n terms sep
2f4fb 61 72 61 74 69 6e 67 20 6e 2b 31 20 73 75 62 74  arating n+1 subt
2f4fc 72 65 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 75  rees.  The.** su
2f4fd 62 74 72 65 65 20 62 6c 6f 63 6b 73 20 61 72 65  btree blocks are
2f4fe 20 63 6f 6e 74 69 67 75 6f 75 73 2c 20 73 6f 20   contiguous, so 
2f4ff 6f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 73  only the first s
2f500 75 62 74 72 65 65 27 73 20 62 6c 6f 63 6b 69 64  ubtree's blockid
2f501 0a 2a 2a 20 69 73 20 65 6e 63 6f 64 65 64 2e 20  .** is encoded. 
2f502 20 54 68 65 20 73 75 62 74 72 65 65 20 61 74 20   The subtree at 
2f503 69 42 6c 6f 63 6b 69 64 20 77 69 6c 6c 20 63 6f  iBlockid will co
2f504 6e 74 61 69 6e 20 61 6c 6c 20 74 65 72 6d 73 20  ntain all terms 
2f505 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65  less.** than the
2f506 20 66 69 72 73 74 20 74 65 72 6d 20 65 6e 63 6f   first term enco
2f507 64 65 64 20 28 6f 72 20 61 6c 6c 20 74 65 72 6d  ded (or all term
2f508 73 20 69 66 20 6e 6f 20 74 65 72 6d 20 69 73 20  s if no term is 
2f509 65 6e 63 6f 64 65 64 29 2e 0a 2a 2a 20 4f 74 68  encoded)..** Oth
2f50a 65 72 77 69 73 65 2c 20 66 6f 72 20 74 65 72 6d  erwise, for term
2f50b 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
2f50c 72 20 65 71 75 61 6c 20 74 6f 20 70 54 65 72 6d  r equal to pTerm
2f50d 5b 69 5d 20 62 75 74 20 6c 65 73 73 0a 2a 2a 20  [i] but less.** 
2f50e 74 68 61 6e 20 70 54 65 72 6d 5b 69 2b 31 5d 2c  than pTerm[i+1],
2f50f 20 74 68 65 20 73 75 62 74 72 65 65 20 66 6f 72   the subtree for
2f510 20 74 68 61 74 20 74 65 72 6d 20 77 69 6c 6c 20   that term will 
2f511 62 65 20 72 6f 6f 74 65 64 20 61 74 0a 2a 2a 20  be rooted at.** 
2f512 69 42 6c 6f 63 6b 69 64 2b 69 2e 20 20 49 6e 74  iBlockid+i.  Int
2f513 65 72 69 6f 72 20 6e 6f 64 65 73 20 6f 6e 6c 79  erior nodes only
2f514 20 73 74 6f 72 65 20 65 6e 6f 75 67 68 20 74 65   store enough te
2f515 72 6d 20 64 61 74 61 20 74 6f 0a 2a 2a 20 64 69  rm data to.** di
2f516 73 74 69 6e 67 75 69 73 68 20 61 64 6a 61 63 65  stinguish adjace
2f517 6e 74 20 63 68 69 6c 64 72 65 6e 20 28 69 66 20  nt children (if 
2f518 74 68 65 20 72 69 67 68 74 6d 6f 73 74 20 74 65  the rightmost te
2f519 72 6d 20 6f 66 20 74 68 65 20 6c 65 66 74 0a 2a  rm of the left.*
2f51a 2a 20 63 68 69 6c 64 20 69 73 20 22 73 6f 6d 65  * child is "some
2f51b 74 68 69 6e 67 22 2c 20 61 6e 64 20 74 68 65 20  thing", and the 
2f51c 6c 65 66 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66  leftmost term of
2f51d 20 74 68 65 20 72 69 67 68 74 20 63 68 69 6c 64   the right child
2f51e 20 69 73 0a 2a 2a 20 22 77 69 63 6b 65 64 22 2c   is.** "wicked",
2f51f 20 6f 6e 6c 79 20 22 77 22 20 69 73 20 73 74 6f   only "w" is sto
2f520 72 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  red)..**.** New 
2f521 64 61 74 61 20 69 73 20 73 70 69 6c 6c 65 64 20  data is spilled 
2f522 74 6f 20 61 20 6e 65 77 20 69 6e 74 65 72 69 6f  to a new interio
2f523 72 20 6e 6f 64 65 20 61 74 20 74 68 65 20 73 61  r node at the sa
2f524 6d 65 20 68 65 69 67 68 74 20 77 68 65 6e 0a 2a  me height when.*
2f525 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f  * the current no
2f526 64 65 20 65 78 63 65 65 64 73 20 49 4e 54 45 52  de exceeds INTER
2f527 49 4f 52 5f 4d 41 58 20 62 79 74 65 73 20 28 64  IOR_MAX bytes (d
2f528 65 66 61 75 6c 74 20 32 30 34 38 29 2e 0a 2a 2a  efault 2048)..**
2f529 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45   INTERIOR_MIN_TE
2f52a 52 4d 53 20 28 64 65 66 61 75 6c 74 20 37 29 20  RMS (default 7) 
2f52b 6b 65 65 70 73 20 6c 61 72 67 65 20 74 65 72 6d  keeps large term
2f52c 73 20 66 72 6f 6d 20 6d 6f 6e 6f 70 6f 6c 69 7a  s from monopoliz
2f52d 69 6e 67 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20  ing.** interior 
2f52e 6e 6f 64 65 73 20 61 6e 64 20 6d 61 6b 69 6e 67  nodes and making
2f52f 20 74 68 65 20 74 72 65 65 20 74 6f 6f 20 73 6b   the tree too sk
2f530 69 6e 6e 79 2e 20 20 54 68 65 20 69 6e 74 65 72  inny.  The inter
2f531 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 61 74 20  ior nodes.** at 
2f532 61 20 67 69 76 65 6e 20 68 65 69 67 68 74 20 61  a given height a
2f533 72 65 20 6e 61 74 75 72 61 6c 6c 79 20 74 72 61  re naturally tra
2f534 63 6b 65 64 20 62 79 20 69 6e 74 65 72 69 6f 72  cked by interior
2f535 20 6e 6f 64 65 73 20 61 74 0a 2a 2a 20 68 65 69   nodes at.** hei
2f536 67 68 74 2b 31 2c 20 61 6e 64 20 73 6f 20 6f 6e  ght+1, and so on
2f537 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67  ..**.**.**** Seg
2f538 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 2a  ment directory *
2f539 2a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 67 6d 65  ***.** The segme
2f53a 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69 6e 20  nt directory in 
2f53b 74 61 62 6c 65 20 25 5f 73 65 67 64 69 72 20 73  table %_segdir s
2f53c 74 6f 72 65 73 20 6d 65 74 61 2d 69 6e 66 6f 72  tores meta-infor
2f53d 6d 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 6d 65  mation for.** me
2f53e 72 67 69 6e 67 20 61 6e 64 20 64 65 6c 65 74 69  rging and deleti
2f53f 6e 67 20 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64  ng segments, and
2f540 20 61 6c 73 6f 20 74 68 65 20 72 6f 6f 74 20 6e   also the root n
2f541 6f 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 65  ode of the.** se
2f542 67 6d 65 6e 74 27 73 20 74 72 65 65 2e 0a 2a 2a  gment's tree..**
2f543 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 6e 6f 64  .** The root nod
2f544 65 20 69 73 20 74 68 65 20 74 6f 70 20 6e 6f 64  e is the top nod
2f545 65 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74  e of the segment
2f546 27 73 20 74 72 65 65 20 61 66 74 65 72 20 65 6e  's tree after en
2f547 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 65 20 65 6e  coding.** the en
2f548 74 69 72 65 20 73 65 67 6d 65 6e 74 2c 20 72 65  tire segment, re
2f549 73 74 72 69 63 74 65 64 20 74 6f 20 52 4f 4f 54  stricted to ROOT
2f54a 5f 4d 41 58 20 62 79 74 65 73 20 28 64 65 66 61  _MAX bytes (defa
2f54b 75 6c 74 20 31 30 32 34 29 2e 0a 2a 2a 20 54 68  ult 1024)..** Th
2f54c 69 73 20 63 6f 75 6c 64 20 62 65 20 65 69 74 68  is could be eith
2f54d 65 72 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 6f  er a leaf node o
2f54e 72 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  r an interior no
2f54f 64 65 2e 20 20 49 66 20 74 68 65 20 74 6f 70 0a  de.  If the top.
2f550 2a 2a 20 6e 6f 64 65 20 72 65 71 75 69 72 65 73  ** node requires
2f551 20 6d 6f 72 65 20 74 68 61 6e 20 52 4f 4f 54 5f   more than ROOT_
2f552 4d 41 58 20 62 79 74 65 73 2c 20 69 74 20 69 73  MAX bytes, it is
2f553 20 66 6c 75 73 68 65 64 20 74 6f 20 25 5f 73 65   flushed to %_se
2f554 67 6d 65 6e 74 73 0a 2a 2a 20 61 6e 64 20 61 20  gments.** and a 
2f555 6e 65 77 20 72 6f 6f 74 20 69 6e 74 65 72 69 6f  new root interio
2f556 72 20 6e 6f 64 65 20 69 73 20 67 65 6e 65 72 61  r node is genera
2f557 74 65 64 20 28 77 68 69 63 68 20 73 68 6f 75 6c  ted (which shoul
2f558 64 20 61 6c 77 61 79 73 20 66 69 74 0a 2a 2a 20  d always fit.** 
2f559 77 69 74 68 69 6e 20 52 4f 4f 54 5f 4d 41 58 20  within ROOT_MAX 
2f55a 62 65 63 61 75 73 65 20 69 74 20 6f 6e 6c 79 20  because it only 
2f55b 6e 65 65 64 73 20 73 70 61 63 65 20 66 6f 72 20  needs space for 
2f55c 32 20 76 61 72 69 6e 74 73 2c 20 74 68 65 0a 2a  2 varints, the.*
2f55d 2a 20 68 65 69 67 68 74 20 61 6e 64 20 74 68 65  * height and the
2f55e 20 62 6c 6f 63 6b 69 64 20 6f 66 20 74 68 65 20   blockid of the 
2f55f 70 72 65 76 69 6f 75 73 20 72 6f 6f 74 29 2e 0a  previous root)..
2f560 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 74 61 2d 69  **.** The meta-i
2f561 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68  nformation in th
2f562 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74  e segment direct
2f563 6f 72 79 20 69 73 3a 0a 2a 2a 20 20 20 6c 65 76  ory is:.**   lev
2f564 65 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  el              
2f565 20 2d 20 73 65 67 6d 65 6e 74 20 6c 65 76 65 6c   - segment level
2f566 20 28 73 65 65 20 62 65 6c 6f 77 29 0a 2a 2a 20   (see below).** 
2f567 20 20 69 64 78 20 20 20 20 20 20 20 20 20 20 20    idx           
2f568 20 20 20 20 20 20 2d 20 69 6e 64 65 78 20 77 69        - index wi
2f569 74 68 69 6e 20 6c 65 76 65 6c 0a 2a 2a 20 20 20  thin level.**   
2f56a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f56b 20 20 20 20 2d 20 28 6c 65 76 65 6c 2c 69 64 78      - (level,idx
2f56c 20 75 6e 69 71 75 65 6c 79 20 69 64 65 6e 74 69   uniquely identi
2f56d 66 79 20 61 20 73 65 67 6d 65 6e 74 29 0a 2a 2a  fy a segment).**
2f56e 20 20 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20 20     start_block  
2f56f 20 20 20 20 20 20 20 2d 20 66 69 72 73 74 20 6c         - first l
2f570 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20 20 6c 65  eaf node.**   le
2f571 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 20  aves_end_block  
2f572 20 20 2d 20 6c 61 73 74 20 6c 65 61 66 20 6e 6f    - last leaf no
2f573 64 65 0a 2a 2a 20 20 20 65 6e 64 5f 62 6c 6f 63  de.**   end_bloc
2f574 6b 20 20 20 20 20 20 20 20 20 20 20 2d 20 6c 61  k           - la
2f575 73 74 20 62 6c 6f 63 6b 20 28 69 6e 63 6c 75 64  st block (includ
2f576 69 6e 67 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  ing interior nod
2f577 65 73 29 0a 2a 2a 20 20 20 72 6f 6f 74 20 20 20  es).**   root   
2f578 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 63               - c
2f579 6f 6e 74 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20  ontents of root 
2f57a 6e 6f 64 65 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  node.**.** If th
2f57b 65 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20 61  e root node is a
2f57c 20 6c 65 61 66 20 6e 6f 64 65 2c 20 74 68 65 6e   leaf node, then
2f57d 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 0a 2a 2a   start_block,.**
2f57e 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63   leaves_end_bloc
2f57f 6b 2c 20 61 6e 64 20 65 6e 64 5f 62 6c 6f 63 6b  k, and end_block
2f580 20 61 72 65 20 61 6c 6c 20 30 2e 0a 2a 2a 0a 2a   are all 0..**.*
2f581 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 6d  *.**** Segment m
2f582 65 72 67 69 6e 67 20 2a 2a 2a 2a 0a 2a 2a 20 54  erging ****.** T
2f583 6f 20 61 6d 6f 72 74 69 7a 65 20 75 70 64 61 74  o amortize updat
2f584 65 20 63 6f 73 74 73 2c 20 73 65 67 6d 65 6e 74  e costs, segment
2f585 73 20 61 72 65 20 67 72 6f 75 70 65 64 20 69 6e  s are grouped in
2f586 74 6f 20 6c 65 76 65 6c 73 20 61 6e 64 0a 2a 2a  to levels and.**
2f587 20 6d 65 72 67 65 64 20 69 6e 20 62 61 74 63 68   merged in batch
2f588 65 73 2e 20 20 45 61 63 68 20 69 6e 63 72 65 61  es.  Each increa
2f589 73 65 20 69 6e 20 6c 65 76 65 6c 20 72 65 70 72  se in level repr
2f58a 65 73 65 6e 74 73 20 65 78 70 6f 6e 65 6e 74 69  esents exponenti
2f58b 61 6c 6c 79 0a 2a 2a 20 6d 6f 72 65 20 64 6f 63  ally.** more doc
2f58c 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 65  uments..**.** Ne
2f58d 77 20 64 6f 63 75 6d 65 6e 74 73 20 28 61 63 74  w documents (act
2f58e 75 61 6c 6c 79 2c 20 64 6f 63 75 6d 65 6e 74 20  ually, document 
2f58f 75 70 64 61 74 65 73 29 20 61 72 65 20 74 6f 6b  updates) are tok
2f590 65 6e 69 7a 65 64 20 61 6e 64 0a 2a 2a 20 77 72  enized and.** wr
2f591 69 74 74 65 6e 20 69 6e 64 69 76 69 64 75 61 6c  itten individual
2f592 6c 79 20 28 75 73 69 6e 67 20 4c 65 61 66 57 72  ly (using LeafWr
2f593 69 74 65 72 29 20 74 6f 20 61 20 6c 65 76 65 6c  iter) to a level
2f594 20 30 20 73 65 67 6d 65 6e 74 2c 20 77 69 74 68   0 segment, with
2f595 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67  .** incrementing
2f596 20 69 64 78 2e 20 20 57 68 65 6e 20 69 64 78 20   idx.  When idx 
2f597 72 65 61 63 68 65 73 20 4d 45 52 47 45 5f 43 4f  reaches MERGE_CO
2f598 55 4e 54 20 28 64 65 66 61 75 6c 74 20 31 36 29  UNT (default 16)
2f599 2c 20 61 6c 6c 0a 2a 2a 20 6c 65 76 65 6c 20 30  , all.** level 0
2f59a 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 6d 65   segments are me
2f59b 72 67 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67  rged into a sing
2f59c 6c 65 20 6c 65 76 65 6c 20 31 20 73 65 67 6d 65  le level 1 segme
2f59d 6e 74 2e 20 20 4c 65 76 65 6c 20 31 0a 2a 2a 20  nt.  Level 1.** 
2f59e 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6c 69 6b  is populated lik
2f59f 65 20 6c 65 76 65 6c 20 30 2c 20 61 6e 64 20 65  e level 0, and e
2f5a0 76 65 6e 74 75 61 6c 6c 79 20 4d 45 52 47 45 5f  ventually MERGE_
2f5a1 43 4f 55 4e 54 20 6c 65 76 65 6c 20 31 0a 2a 2a  COUNT level 1.**
2f5a2 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 6d 65   segments are me
2f5a3 72 67 65 64 20 74 6f 20 61 20 73 69 6e 67 6c 65  rged to a single
2f5a4 20 6c 65 76 65 6c 20 32 20 73 65 67 6d 65 6e 74   level 2 segment
2f5a5 20 28 72 65 70 72 65 73 65 6e 74 69 6e 67 0a 2a   (representing.*
2f5a6 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 5e 32 20  * MERGE_COUNT^2 
2f5a7 75 70 64 61 74 65 73 29 2c 20 61 6e 64 20 73 6f  updates), and so
2f5a8 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 67   on..**.** A seg
2f5a9 6d 65 6e 74 20 6d 65 72 67 65 20 74 72 61 76 65  ment merge trave
2f5aa 72 73 65 73 20 61 6c 6c 20 73 65 67 6d 65 6e 74  rses all segment
2f5ab 73 20 61 74 20 61 20 67 69 76 65 6e 20 6c 65 76  s at a given lev
2f5ac 65 6c 20 69 6e 0a 2a 2a 20 70 61 72 61 6c 6c 65  el in.** paralle
2f5ad 6c 2c 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20  l, performing a 
2f5ae 73 74 72 61 69 67 68 74 66 6f 72 77 61 72 64 20  straightforward 
2f5af 73 6f 72 74 65 64 20 6d 65 72 67 65 2e 20 20 53  sorted merge.  S
2f5b0 69 6e 63 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20  ince segment.** 
2f5b1 6c 65 61 66 20 6e 6f 64 65 73 20 61 72 65 20 77  leaf nodes are w
2f5b2 72 69 74 74 65 6e 20 69 6e 20 74 6f 20 74 68 65  ritten in to the
2f5b3 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c   %_segments tabl
2f5b4 65 20 69 6e 20 6f 72 64 65 72 2c 20 74 68 69 73  e in order, this
2f5b5 0a 2a 2a 20 6d 65 72 67 65 20 74 72 61 76 65 72  .** merge traver
2f5b6 73 65 73 20 74 68 65 20 75 6e 64 65 72 6c 79 69  ses the underlyi
2f5b7 6e 67 20 73 71 6c 69 74 65 20 64 69 73 6b 20 73  ng sqlite disk s
2f5b8 74 72 75 63 74 75 72 65 73 20 65 66 66 69 63 69  tructures effici
2f5b9 65 6e 74 6c 79 2e 0a 2a 2a 20 41 66 74 65 72 20  ently..** After 
2f5ba 74 68 65 20 6d 65 72 67 65 2c 20 61 6c 6c 20 73  the merge, all s
2f5bb 65 67 6d 65 6e 74 20 62 6c 6f 63 6b 73 20 66 72  egment blocks fr
2f5bc 6f 6d 20 74 68 65 20 6d 65 72 67 65 64 20 6c 65  om the merged le
2f5bd 76 65 6c 20 61 72 65 0a 2a 2a 20 64 65 6c 65 74  vel are.** delet
2f5be 65 64 2e 0a 2a 2a 0a 2a 2a 20 4d 45 52 47 45 5f  ed..**.** MERGE_
2f5bf 43 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20 68  COUNT controls h
2f5c0 6f 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72 67  ow often we merg
2f5c1 65 20 73 65 67 6d 65 6e 74 73 2e 20 20 31 36 20  e segments.  16 
2f5c2 73 65 65 6d 73 20 74 6f 20 62 65 0a 2a 2a 20 73  seems to be.** s
2f5c3 6f 6d 65 77 68 61 74 20 6f 66 20 61 20 73 77 65  omewhat of a swe
2f5c4 65 74 20 73 70 6f 74 20 66 6f 72 20 69 6e 73 65  et spot for inse
2f5c5 72 74 69 6f 6e 20 70 65 72 66 6f 72 6d 61 6e 63  rtion performanc
2f5c6 65 2e 20 20 33 32 20 61 6e 64 20 36 34 20 73 68  e.  32 and 64 sh
2f5c7 6f 77 0a 2a 2a 20 76 65 72 79 20 73 69 6d 69 6c  ow.** very simil
2f5c8 61 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6e  ar performance n
2f5c9 75 6d 62 65 72 73 20 74 6f 20 31 36 20 6f 6e 20  umbers to 16 on 
2f5ca 69 6e 73 65 72 74 69 6f 6e 2c 20 74 68 6f 75 67  insertion, thoug
2f5cb 68 20 74 68 65 79 27 72 65 0a 2a 2a 20 61 20 74  h they're.** a t
2f5cc 69 6e 79 20 62 69 74 20 73 6c 6f 77 65 72 20 28  iny bit slower (
2f5cd 70 65 72 68 61 70 73 20 64 75 65 20 74 6f 20 6d  perhaps due to m
2f5ce 6f 72 65 20 6f 76 65 72 68 65 61 64 20 69 6e 20  ore overhead in 
2f5cf 6d 65 72 67 65 2d 74 69 6d 65 0a 2a 2a 20 73 6f  merge-time.** so
2f5d0 72 74 69 6e 67 29 2e 20 20 38 20 69 73 20 61 62  rting).  8 is ab
2f5d1 6f 75 74 20 32 30 25 20 73 6c 6f 77 65 72 20 74  out 20% slower t
2f5d2 68 61 6e 20 31 36 2c 20 34 20 61 62 6f 75 74 20  han 16, 4 about 
2f5d3 35 30 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 0a  50% slower than.
2f5d4 2a 2a 20 31 36 2c 20 32 20 61 62 6f 75 74 20 36  ** 16, 2 about 6
2f5d5 36 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 31  6% slower than 1
2f5d6 36 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 71 75 65 72  6..**.** At quer
2f5d7 79 20 74 69 6d 65 2c 20 68 69 67 68 20 4d 45 52  y time, high MER
2f5d8 47 45 5f 43 4f 55 4e 54 20 69 6e 63 72 65 61 73  GE_COUNT increas
2f5d9 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  es the number of
2f5da 20 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 77 68 69   segments.** whi
2f5db 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 73 63  ch need to be sc
2f5dc 61 6e 6e 65 64 20 61 6e 64 20 6d 65 72 67 65 64  anned and merged
2f5dd 2e 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c  .  For instance,
2f5de 20 77 69 74 68 20 31 30 30 6b 20 64 6f 63 73 0a   with 100k docs.
2f5df 2a 2a 20 69 6e 73 65 72 74 65 64 3a 0a 2a 2a 0a  ** inserted:.**.
2f5e0 2a 2a 20 20 20 20 4d 45 52 47 45 5f 43 4f 55 4e  **    MERGE_COUN
2f5e1 54 20 20 20 73 65 67 6d 65 6e 74 73 0a 2a 2a 20  T   segments.** 
2f5e2 20 20 20 20 20 20 31 36 20 20 20 20 20 20 20 20        16        
2f5e3 20 20 20 32 35 0a 2a 2a 20 20 20 20 20 20 20 20     25.**        
2f5e4 38 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 2a  8           12.*
2f5e5 2a 20 20 20 20 20 20 20 20 34 20 20 20 20 20 20  *        4      
2f5e6 20 20 20 20 20 31 30 0a 2a 2a 20 20 20 20 20 20       10.**      
2f5e7 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 36    2            6
2f5e8 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 70 70 65  .**.** This appe
2f5e9 61 72 73 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79  ars to have only
2f5ea 20 61 20 6d 6f 64 65 72 61 74 65 20 69 6d 70 61   a moderate impa
2f5eb 63 74 20 6f 6e 20 71 75 65 72 69 65 73 20 66 6f  ct on queries fo
2f5ec 72 20 76 65 72 79 0a 2a 2a 20 66 72 65 71 75 65  r very.** freque
2f5ed 6e 74 20 74 65 72 6d 73 20 28 77 68 69 63 68 20  nt terms (which 
2f5ee 61 72 65 20 73 6f 6d 65 77 68 61 74 20 64 6f 6d  are somewhat dom
2f5ef 69 6e 61 74 65 64 20 62 79 20 73 65 67 6d 65 6e  inated by segmen
2f5f0 74 20 6d 65 72 67 65 0a 2a 2a 20 63 6f 73 74 73  t merge.** costs
2f5f1 29 2c 20 61 6e 64 20 69 6e 66 72 65 71 75 65 6e  ), and infrequen
2f5f2 74 20 61 6e 64 20 6e 6f 6e 2d 65 78 69 73 74 65  t and non-existe
2f5f3 6e 74 20 74 65 72 6d 73 20 73 74 69 6c 6c 20 73  nt terms still s
2f5f4 65 65 6d 20 74 6f 20 62 65 20 66 61 73 74 0a 2a  eem to be fast.*
2f5f5 2a 20 65 76 65 6e 20 77 69 74 68 20 6d 61 6e 79  * even with many
2f5f6 20 73 65 67 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a   segments..**.**
2f5f7 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 61   TODO(shess) Tha
2f5f8 74 20 73 61 69 64 2c 20 69 74 20 77 6f 75 6c 64  t said, it would
2f5f9 20 62 65 20 6e 69 63 65 20 74 6f 20 68 61 76 65   be nice to have
2f5fa 20 61 20 62 65 74 74 65 72 20 71 75 65 72 79 2d   a better query-
2f5fb 73 69 64 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  side.** argument
2f5fc 20 66 6f 72 20 4d 45 52 47 45 5f 43 4f 55 4e 54   for MERGE_COUNT
2f5fd 20 6f 66 20 31 36 2e 20 20 41 6c 73 6f 2c 20 69   of 16.  Also, i
2f5fe 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2f 6c 69  t is possible/li
2f5ff 6b 65 6c 79 20 74 68 61 74 0a 2a 2a 20 6f 70 74  kely that.** opt
2f600 69 6d 69 7a 61 74 69 6f 6e 73 20 74 6f 20 74 68  imizations to th
2f601 69 6e 67 73 20 6c 69 6b 65 20 64 6f 63 6c 69 73  ings like doclis
2f602 74 20 6d 65 72 67 69 6e 67 20 77 69 6c 6c 20 73  t merging will s
2f603 77 69 6e 67 20 74 68 65 20 73 77 65 65 74 0a 2a  wing the sweet.*
2f604 2a 20 73 70 6f 74 20 61 72 6f 75 6e 64 2e 0a 2a  * spot around..*
2f605 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 48 61 6e  *.**.**.**** Han
2f606 64 6c 69 6e 67 20 6f 66 20 64 65 6c 65 74 69 6f  dling of deletio
2f607 6e 73 20 61 6e 64 20 75 70 64 61 74 65 73 20 2a  ns and updates *
2f608 2a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 77 65 27  ***.** Since we'
2f609 72 65 20 75 73 69 6e 67 20 61 20 73 65 67 6d 65  re using a segme
2f60a 6e 74 65 64 20 73 74 72 75 63 74 75 72 65 2c 20  nted structure, 
2f60b 77 69 74 68 20 6e 6f 20 64 6f 63 69 64 2d 6f 72  with no docid-or
2f60c 69 65 6e 74 65 64 0a 2a 2a 20 69 6e 64 65 78 20  iented.** index 
2f60d 69 6e 74 6f 20 74 68 65 20 74 65 72 6d 20 69 6e  into the term in
2f60e 64 65 78 2c 20 77 65 20 63 6c 65 61 72 6c 79 20  dex, we clearly 
2f60f 63 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 75 70  cannot simply up
2f610 64 61 74 65 20 74 68 65 20 74 65 72 6d 0a 2a 2a  date the term.**
2f611 20 69 6e 64 65 78 20 77 68 65 6e 20 61 20 64 6f   index when a do
2f612 63 75 6d 65 6e 74 20 69 73 20 64 65 6c 65 74 65  cument is delete
2f613 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 46  d or updated.  F
2f614 6f 72 20 64 65 6c 65 74 69 6f 6e 73 2c 20 77 65  or deletions, we
2f615 0a 2a 2a 20 77 72 69 74 65 20 61 6e 20 65 6d 70  .** write an emp
2f616 74 79 20 64 6f 63 6c 69 73 74 20 28 76 61 72 69  ty doclist (vari
2f617 6e 74 28 64 6f 63 69 64 29 20 76 61 72 69 6e 74  nt(docid) varint
2f618 28 50 4f 53 5f 45 4e 44 29 29 2c 20 66 6f 72 20  (POS_END)), for 
2f619 75 70 64 61 74 65 73 0a 2a 2a 20 77 65 20 73 69  updates.** we si
2f61a 6d 70 6c 79 20 77 72 69 74 65 20 74 68 65 20 6e  mply write the n
2f61b 65 77 20 64 6f 63 6c 69 73 74 2e 20 20 53 65 67  ew doclist.  Seg
2f61c 6d 65 6e 74 20 6d 65 72 67 65 73 20 6f 76 65 72  ment merges over
2f61d 77 72 69 74 65 20 6f 6c 64 65 72 0a 2a 2a 20 64  write older.** d
2f61e 61 74 61 20 66 6f 72 20 61 20 70 61 72 74 69 63  ata for a partic
2f61f 75 6c 61 72 20 64 6f 63 69 64 20 77 69 74 68 20  ular docid with 
2f620 6e 65 77 65 72 20 64 61 74 61 2c 20 73 6f 20 64  newer data, so d
2f621 65 6c 65 74 65 73 20 6f 72 20 75 70 64 61 74 65  eletes or update
2f622 73 0a 2a 2a 20 77 69 6c 6c 20 65 76 65 6e 74 75  s.** will eventu
2f623 61 6c 6c 79 20 6f 76 65 72 74 61 6b 65 20 74 68  ally overtake th
2f624 65 20 65 61 72 6c 69 65 72 20 64 61 74 61 20 61  e earlier data a
2f625 6e 64 20 6b 6e 6f 63 6b 20 69 74 20 6f 75 74 2e  nd knock it out.
2f626 20 20 54 68 65 0a 2a 2a 20 71 75 65 72 79 20 6c    The.** query l
2f627 6f 67 69 63 20 6c 69 6b 65 77 69 73 65 20 6d 65  ogic likewise me
2f628 72 67 65 73 20 64 6f 63 6c 69 73 74 73 20 73 6f  rges doclists so
2f629 20 74 68 61 74 20 6e 65 77 65 72 20 64 61 74 61   that newer data
2f62a 20 6b 6e 6f 63 6b 73 20 6f 75 74 0a 2a 2a 20 6f   knocks out.** o
2f62b 6c 64 65 72 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a  lder data..**.**
2f62c 20 54 4f 44 4f 28 73 68 65 73 73 29 20 50 72 6f   TODO(shess) Pro
2f62d 76 69 64 65 20 61 20 56 41 43 55 55 4d 20 74 79  vide a VACUUM ty
2f62e 70 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  pe operation to 
2f62f 63 6c 65 61 72 20 6f 75 74 20 61 6c 6c 0a 2a 2a  clear out all.**
2f630 20 64 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 64   deletions and d
2f631 75 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 54 68  uplications.  Th
2f632 69 73 20 77 6f 75 6c 64 20 62 61 73 69 63 61 6c  is would basical
2f633 6c 79 20 62 65 20 61 20 66 6f 72 63 65 64 20 6d  ly be a forced m
2f634 65 72 67 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 73  erge.** into a s
2f635 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74 2e 0a 2a  ingle segment..*
2f636 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
2f637 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
2f638 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
2f639 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69 66  NABLE_FTS3)..#if
2f63a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
2f63b 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 26 26 20  ENABLE_FTS3) && 
2f63c 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2f63d 43 4f 52 45 29 0a 23 20 64 65 66 69 6e 65 20 53  CORE).# define S
2f63e 51 4c 49 54 45 5f 43 4f 52 45 20 31 0a 23 65 6e  QLITE_CORE 1.#en
2f63f 64 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif.../*********
2f640 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74  ***** Include ft
2f641 73 33 5f 65 78 70 72 2e 68 20 69 6e 20 74 68 65  s3_expr.h in the
2f642 20 6d 69 64 64 6c 65 20 6f 66 20 66 74 73 33 2e   middle of fts3.
2f643 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2f644 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2f645 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2f646 20 66 74 73 33 5f 65 78 70 72 2e 68 20 2a 2a 2a   fts3_expr.h ***
2f647 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f648 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f649 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
2f64a 20 4e 6f 76 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68   Nov 28.**.** Th
2f64b 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2f64c 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2f64d 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2f64e 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2f64f 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2f650 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2f651 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2f652 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2f653 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2f654 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2f655 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2f656 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2f657 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2f658 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2f659 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2f65a 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2f65b 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2f65c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f65d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f65e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f65f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
2f661 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2f662 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73 33 5f  ** Include fts3_
2f663 74 6f 6b 65 6e 69 7a 65 72 2e 68 20 69 6e 20 74  tokenizer.h in t
2f664 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 66 74 73  he middle of fts
2f665 33 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a  3_expr.h *******
2f666 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2f667 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
2f668 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68 20 2a  s3_tokenizer.h *
2f669 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f66a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f66b 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75  */./*.** 2006 Ju
2f66c 6c 79 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ly 10.**.** The 
2f66d 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2f66e 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2f66f 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 0a  is source code..
2f670 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2f671 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f672 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f674 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
2f675 44 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  Defines the inte
2f676 72 66 61 63 65 20 74 6f 20 74 6f 6b 65 6e 69 7a  rface to tokeniz
2f677 65 72 73 20 75 73 65 64 20 62 79 20 66 75 6c 6c  ers used by full
2f678 74 65 78 74 2d 73 65 61 72 63 68 2e 20 20 54 68  text-search.  Th
2f679 65 72 65 0a 2a 2a 20 61 72 65 20 74 68 72 65 65  ere.** are three
2f67a 20 62 61 73 69 63 20 63 6f 6d 70 6f 6e 65 6e 74   basic component
2f67b 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33  s:.**.** sqlite3
2f67c 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
2f67d 65 20 69 73 20 61 20 73 69 6e 67 6c 65 74 6f 6e  e is a singleton
2f67e 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 74 6f   defining the to
2f67f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6e 74 65 72  kenizer.** inter
2f680 66 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20  face functions. 
2f681 20 54 68 69 73 20 69 73 20 65 73 73 65 6e 74 69   This is essenti
2f682 61 6c 6c 79 20 74 68 65 20 63 6c 61 73 73 20 73  ally the class s
2f683 74 72 75 63 74 75 72 65 20 66 6f 72 0a 2a 2a 20  tructure for.** 
2f684 74 6f 6b 65 6e 69 7a 65 72 73 2e 0a 2a 2a 0a 2a  tokenizers..**.*
2f685 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  * sqlite3_tokeni
2f686 7a 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 64  zer is used to d
2f687 65 66 69 6e 65 20 61 20 70 61 72 74 69 63 75 6c  efine a particul
2f688 61 72 20 74 6f 6b 65 6e 69 7a 65 72 2c 20 70 65  ar tokenizer, pe
2f689 72 68 61 70 73 0a 2a 2a 20 69 6e 63 6c 75 64 69  rhaps.** includi
2f68a 6e 67 20 63 75 73 74 6f 6d 69 7a 61 74 69 6f 6e  ng customization
2f68b 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 64 65 66   information def
2f68c 69 6e 65 64 20 61 74 20 63 72 65 61 74 69 6f 6e  ined at creation
2f68d 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c   time..**.** sql
2f68e 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
2f68f 75 72 73 6f 72 20 69 73 20 67 65 6e 65 72 61 74  ursor is generat
2f690 65 64 20 62 79 20 61 20 74 6f 6b 65 6e 69 7a 65  ed by a tokenize
2f691 72 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a  r to generate.**
2f692 20 74 6f 6b 65 6e 73 20 66 72 6f 6d 20 61 20 70   tokens from a p
2f693 61 72 74 69 63 75 6c 61 72 20 69 6e 70 75 74 2e  articular input.
2f694 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 46 54 53  .*/.#ifndef _FTS
2f695 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 0a 23  3_TOKENIZER_H_.#
2f696 64 65 66 69 6e 65 20 5f 46 54 53 33 5f 54 4f 4b  define _FTS3_TOK
2f697 45 4e 49 5a 45 52 5f 48 5f 0a 0a 2f 2a 20 54 4f  ENIZER_H_../* TO
2f698 44 4f 28 73 68 65 73 73 29 20 4f 6e 6c 79 20 75  DO(shess) Only u
2f699 73 65 64 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f  sed for SQLITE_O
2f69a 4b 20 61 6e 64 20 53 51 4c 49 54 45 5f 44 4f 4e  K and SQLITE_DON
2f69b 45 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a  E at this time..
2f69c 2a 2a 20 49 66 20 74 6f 6b 65 6e 69 7a 65 72 73  ** If tokenizers
2f69d 20 61 72 65 20 74 6f 20 62 65 20 61 6c 6c 6f 77   are to be allow
2f69e 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74  ed to call sqlit
2f69f 65 33 5f 2a 28 29 20 66 75 6e 63 74 69 6f 6e 73  e3_*() functions
2f6a0 2c 20 74 68 65 6e 0a 2a 2a 20 77 65 20 77 69 6c  , then.** we wil
2f6a1 6c 20 6e 65 65 64 20 61 20 77 61 79 20 74 6f 20  l need a way to 
2f6a2 72 65 67 69 73 74 65 72 20 74 68 65 20 41 50 49  register the API
2f6a3 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a   consistently..*
2f6a4 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75  /../*.** Structu
2f6a5 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  res used by the 
2f6a6 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65 72 66  tokenizer interf
2f6a7 61 63 65 2e 20 57 68 65 6e 20 61 20 6e 65 77 20  ace. When a new 
2f6a8 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d 70  tokenizer.** imp
2f6a9 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 72  lementation is r
2f6aa 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 63  egistered, the c
2f6ab 61 6c 6c 65 72 20 70 72 6f 76 69 64 65 73 20 61  aller provides a
2f6ac 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61   pointer to.** a
2f6ad 6e 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  n sqlite3_tokeni
2f6ae 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 74 61  zer_module conta
2f6af 69 6e 69 6e 67 20 70 6f 69 6e 74 65 72 73 20 74  ining pointers t
2f6b0 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a  o the callback.*
2f6b1 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  * functions that
2f6b2 20 6d 61 6b 65 20 75 70 20 61 6e 20 69 6d 70 6c   make up an impl
2f6b3 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  ementation..**.*
2f6b4 2a 20 57 68 65 6e 20 61 6e 20 66 74 73 33 20 74  * When an fts3 t
2f6b5 61 62 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c  able is created,
2f6b6 20 69 74 20 70 61 73 73 65 73 20 61 6e 79 20 61   it passes any a
2f6b7 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20  rguments passed 
2f6b8 74 6f 0a 2a 2a 20 74 68 65 20 74 6f 6b 65 6e 69  to.** the tokeni
2f6b9 7a 65 72 20 63 6c 61 75 73 65 20 6f 66 20 74 68  zer clause of th
2f6ba 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  e CREATE VIRTUAL
2f6bb 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
2f6bc 20 74 6f 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74   to the.** sqlit
2f6bd 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
2f6be 75 6c 65 2e 78 43 72 65 61 74 65 28 29 20 66 75  ule.xCreate() fu
2f6bf 6e 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 65  nction of the re
2f6c0 71 75 65 73 74 65 64 20 74 6f 6b 65 6e 69 7a 65  quested tokenize
2f6c1 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  r.** implementat
2f6c2 69 6f 6e 2e 20 54 68 65 20 78 43 72 65 61 74 65  ion. The xCreate
2f6c3 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74  () function in t
2f6c4 75 72 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 0a  urn returns an .
2f6c5 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
2f6c6 69 7a 65 72 20 73 74 72 75 63 74 75 72 65 20 72  izer structure r
2f6c7 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
2f6c8 73 70 65 63 69 66 69 63 20 74 6f 6b 65 6e 69 7a  specific tokeniz
2f6c9 65 72 20 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64  er to.** be used
2f6ca 20 66 6f 72 20 74 68 65 20 66 74 73 33 20 74 61   for the fts3 ta
2f6cb 62 6c 65 20 28 63 75 73 74 6f 6d 69 7a 65 64 20  ble (customized 
2f6cc 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  by the tokenizer
2f6cd 20 63 6c 61 75 73 65 20 61 72 67 75 6d 65 6e 74   clause argument
2f6ce 73 29 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 74 6f 6b  s)..**.** To tok
2f6cf 65 6e 69 7a 65 20 61 6e 20 69 6e 70 75 74 20 62  enize an input b
2f6d0 75 66 66 65 72 2c 20 74 68 65 20 73 71 6c 69 74  uffer, the sqlit
2f6d1 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
2f6d2 75 6c 65 2e 78 4f 70 65 6e 28 29 0a 2a 2a 20 6d  ule.xOpen().** m
2f6d3 65 74 68 6f 64 20 69 73 20 63 61 6c 6c 65 64 2e  ethod is called.
2f6d4 20 49 74 20 72 65 74 75 72 6e 73 20 61 6e 20 73   It returns an s
2f6d5 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2f6d6 5f 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 0a 2a  _cursor object.*
2f6d7 2a 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73  * that may be us
2f6d8 65 64 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 61  ed to tokenize a
2f6d9 20 73 70 65 63 69 66 69 63 20 69 6e 70 75 74 20   specific input 
2f6da 62 75 66 66 65 72 20 62 61 73 65 64 20 6f 6e 0a  buffer based on.
2f6db 2a 2a 20 74 68 65 20 74 6f 6b 65 6e 69 7a 61 74  ** the tokenizat
2f6dc 69 6f 6e 20 72 75 6c 65 73 20 73 75 70 70 6c 69  ion rules suppli
2f6dd 65 64 20 62 79 20 61 20 73 70 65 63 69 66 69 63  ed by a specific
2f6de 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2f6df 65 72 0a 2a 2a 20 6f 62 6a 65 63 74 2e 0a 2a 2f  er.** object..*/
2f6e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2f6e1 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
2f6e2 72 5f 6d 6f 64 75 6c 65 20 73 71 6c 69 74 65 33  r_module sqlite3
2f6e3 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
2f6e4 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
2f6e5 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
2f6e6 7a 65 72 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  zer sqlite3_toke
2f6e7 6e 69 7a 65 72 3b 0a 74 79 70 65 64 65 66 20 73  nizer;.typedef s
2f6e8 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f  truct sqlite3_to
2f6e9 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 73  kenizer_cursor s
2f6ea 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2f6eb 5f 63 75 72 73 6f 72 3b 0a 0a 73 74 72 75 63 74  _cursor;..struct
2f6ec 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2f6ed 65 72 5f 6d 6f 64 75 6c 65 20 7b 0a 0a 20 20 2f  er_module {..  /
2f6ee 2a 0a 20 20 2a 2a 20 53 74 72 75 63 74 75 72 65  *.  ** Structure
2f6ef 20 76 65 72 73 69 6f 6e 2e 20 53 68 6f 75 6c 64   version. Should
2f6f0 20 61 6c 77 61 79 73 20 62 65 20 73 65 74 20 74   always be set t
2f6f1 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  o 0..  */.  int 
2f6f2 69 56 65 72 73 69 6f 6e 3b 0a 0a 20 20 2f 2a 0a  iVersion;..  /*.
2f6f3 20 20 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65    ** Create a ne
2f6f4 77 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 54 68 65  w tokenizer. The
2f6f5 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 61   values in the a
2f6f6 72 67 76 5b 5d 20 61 72 72 61 79 20 61 72 65 20  rgv[] array are 
2f6f7 74 68 65 0a 20 20 2a 2a 20 61 72 67 75 6d 65 6e  the.  ** argumen
2f6f8 74 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  ts passed to the
2f6f9 20 22 74 6f 6b 65 6e 69 7a 65 72 22 20 63 6c 61   "tokenizer" cla
2f6fa 75 73 65 20 6f 66 20 74 68 65 20 43 52 45 41 54  use of the CREAT
2f6fb 45 20 56 49 52 54 55 41 4c 0a 20 20 2a 2a 20 54  E VIRTUAL.  ** T
2f6fc 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 74  ABLE statement t
2f6fd 68 61 74 20 63 72 65 61 74 65 64 20 74 68 65 20  hat created the 
2f6fe 66 74 73 33 20 74 61 62 6c 65 2e 20 46 6f 72 20  fts3 table. For 
2f6ff 65 78 61 6d 70 6c 65 2c 20 69 66 0a 20 20 2a 2a  example, if.  **
2f700 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53   the following S
2f701 51 4c 20 69 73 20 65 78 65 63 75 74 65 64 3a 0a  QL is executed:.
2f702 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 43 52 45 41    **.  **   CREA
2f703 54 45 20 2e 2e 20 55 53 49 4e 47 20 66 74 73 33  TE .. USING fts3
2f704 28 20 2e 2e 2e 20 2c 20 74 6f 6b 65 6e 69 7a 65  ( ... , tokenize
2f705 72 20 3c 74 6f 6b 65 6e 69 7a 65 72 2d 6e 61 6d  r <tokenizer-nam
2f706 65 3e 20 61 72 67 31 20 61 72 67 32 29 0a 20 20  e> arg1 arg2).  
2f707 2a 2a 0a 20 20 2a 2a 20 74 68 65 6e 20 61 72 67  **.  ** then arg
2f708 63 20 69 73 20 73 65 74 20 74 6f 20 32 2c 20 61  c is set to 2, a
2f709 6e 64 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72  nd the argv[] ar
2f70a 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69  ray contains poi
2f70b 6e 74 65 72 73 0a 20 20 2a 2a 20 74 6f 20 74 68  nters.  ** to th
2f70c 65 20 73 74 72 69 6e 67 73 20 22 61 72 67 31 22  e strings "arg1"
2f70d 20 61 6e 64 20 22 61 72 67 32 22 2e 0a 20 20 2a   and "arg2"..  *
2f70e 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6d 65 74 68  *.  ** This meth
2f70f 6f 64 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e  od should return
2f710 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f   either SQLITE_O
2f711 4b 20 28 30 29 2c 20 6f 72 20 61 6e 20 53 51 4c  K (0), or an SQL
2f712 69 74 65 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20  ite error .  ** 
2f713 63 6f 64 65 2e 20 49 66 20 53 51 4c 49 54 45 5f  code. If SQLITE_
2f714 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  OK is returned, 
2f715 74 68 65 6e 20 2a 70 70 54 6f 6b 65 6e 69 7a 65  then *ppTokenize
2f716 72 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 0a  r should be set.
2f717 20 20 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 61 74    ** to point at
2f718 20 74 68 65 20 6e 65 77 6c 79 20 63 72 65 61 74   the newly creat
2f719 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72  ed tokenizer str
2f71a 75 63 74 75 72 65 2e 20 54 68 65 20 67 65 6e 65  ucture. The gene
2f71b 72 69 63 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  ric.  ** sqlite3
2f71c 5f 74 6f 6b 65 6e 69 7a 65 72 2e 70 4d 6f 64 75  _tokenizer.pModu
2f71d 6c 65 20 76 61 72 69 61 62 6c 65 20 73 68 6f 75  le variable shou
2f71e 6c 64 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61  ld not be initia
2f71f 6c 69 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68  lised by.  ** th
2f720 69 73 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 65  is callback. The
2f721 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 64 6f 20   caller will do 
2f722 73 6f 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  so..  */.  int (
2f723 2a 78 43 72 65 61 74 65 29 28 0a 20 20 20 20 69  *xCreate)(.    i
2f724 6e 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20  nt argc,        
2f725 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f726 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72     /* Size of ar
2f727 67 76 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20  gv array */.    
2f728 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
2f729 74 2a 61 72 67 76 2c 20 20 20 20 20 20 20 20 20  t*argv,         
2f72a 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
2f72b 20 61 72 67 75 6d 65 6e 74 20 73 74 72 69 6e 67   argument string
2f72c 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  s */.    sqlite3
2f72d 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54  _tokenizer **ppT
2f72e 6f 6b 65 6e 69 7a 65 72 20 20 20 20 20 2f 2a 20  okenizer     /* 
2f72f 4f 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b  OUT: Created tok
2f730 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 29 3b 0a 0a  enizer */.  );..
2f731 20 20 2f 2a 0a 20 20 2a 2a 20 44 65 73 74 72 6f    /*.  ** Destro
2f732 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f  y an existing to
2f733 6b 65 6e 69 7a 65 72 2e 20 54 68 65 20 66 74 73  kenizer. The fts
2f734 33 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74  3 module calls t
2f735 68 69 73 20 6d 65 74 68 6f 64 0a 20 20 2a 2a 20  his method.  ** 
2f736 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72  exactly once for
2f737 20 65 61 63 68 20 73 75 63 63 65 73 73 66 75 6c   each successful
2f738 20 63 61 6c 6c 20 74 6f 20 78 43 72 65 61 74 65   call to xCreate
2f739 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  ()..  */.  int (
2f73a 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74  *xDestroy)(sqlit
2f73b 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
2f73c 6f 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20 20 2f 2a  okenizer);..  /*
2f73d 0a 20 20 2a 2a 20 43 72 65 61 74 65 20 61 20 74  .  ** Create a t
2f73e 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20  okenizer cursor 
2f73f 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e 20 69  to tokenize an i
2f740 6e 70 75 74 20 62 75 66 66 65 72 2e 20 54 68 65  nput buffer. The
2f741 20 63 61 6c 6c 65 72 0a 20 20 2a 2a 20 69 73 20   caller.  ** is 
2f742 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
2f743 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68  ensuring that th
2f744 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 72  e input buffer r
2f745 65 6d 61 69 6e 73 20 76 61 6c 69 64 0a 20 20 2a  emains valid.  *
2f746 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 75 72 73  * until the curs
2f747 6f 72 20 69 73 20 63 6c 6f 73 65 64 20 28 75 73  or is closed (us
2f748 69 6e 67 20 74 68 65 20 78 43 6c 6f 73 65 28 29  ing the xClose()
2f749 20 6d 65 74 68 6f 64 29 2e 20 0a 20 20 2a 2f 0a   method). .  */.
2f74a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 0a    int (*xOpen)(.
2f74b 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65      sqlite3_toke
2f74c 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
2f74d 72 2c 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  r,       /* Toke
2f74e 6e 69 7a 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  nizer object */.
2f74f 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2f750 70 49 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74  pInput, int nByt
2f751 65 73 2c 20 20 20 20 20 20 2f 2a 20 49 6e 70 75  es,      /* Inpu
2f752 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20  t buffer */.    
2f753 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
2f754 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72  r_cursor **ppCur
2f755 73 6f 72 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65  sor  /* OUT: Cre
2f756 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 63  ated tokenizer c
2f757 75 72 73 6f 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20  ursor */.  );.. 
2f758 20 2f 2a 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79   /*.  ** Destroy
2f759 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f 6b   an existing tok
2f75a 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 2e 20 54  enizer cursor. T
2f75b 68 65 20 66 74 73 33 20 6d 6f 64 75 6c 65 20 63  he fts3 module c
2f75c 61 6c 6c 73 20 74 68 69 73 20 0a 20 20 2a 2a 20  alls this .  ** 
2f75d 6d 65 74 68 6f 64 20 65 78 61 63 74 6c 79 20 6f  method exactly o
2f75e 6e 63 65 20 66 6f 72 20 65 61 63 68 20 73 75 63  nce for each suc
2f75f 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20  cessful call to 
2f760 78 4f 70 65 6e 28 29 2e 0a 20 20 2a 2f 0a 20 20  xOpen()..  */.  
2f761 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73 71  int (*xClose)(sq
2f762 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
2f763 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  cursor *pCursor)
2f764 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74  ;..  /*.  ** Ret
2f765 72 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 74  rieve the next t
2f766 6f 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 74 6f  oken from the to
2f767 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 70  kenizer cursor p
2f768 43 75 72 73 6f 72 2e 20 54 68 69 73 0a 20 20 2a  Cursor. This.  *
2f769 2a 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20  * method should 
2f76a 65 69 74 68 65 72 20 72 65 74 75 72 6e 20 53 51  either return SQ
2f76b 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 73 65 74 20  LITE_OK and set 
2f76c 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68  the values of th
2f76d 65 0a 20 20 2a 2a 20 22 4f 55 54 22 20 76 61 72  e.  ** "OUT" var
2f76e 69 61 62 6c 65 73 20 69 64 65 6e 74 69 66 69 65  iables identifie
2f76f 64 20 62 65 6c 6f 77 2c 20 6f 72 20 53 51 4c 49  d below, or SQLI
2f770 54 45 5f 44 4f 4e 45 20 74 6f 20 69 6e 64 69 63  TE_DONE to indic
2f771 61 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 74 68  ate that.  ** th
2f772 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66  e end of the buf
2f773 66 65 72 20 68 61 73 20 62 65 65 6e 20 72 65 61  fer has been rea
2f774 63 68 65 64 2c 20 6f 72 20 61 6e 20 53 51 4c 69  ched, or an SQLi
2f775 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20  te error code.. 
2f776 20 2a 2a 0a 20 20 2a 2a 20 2a 70 70 54 6f 6b 65   **.  ** *ppToke
2f777 6e 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20  n should be set 
2f778 74 6f 20 70 6f 69 6e 74 20 61 74 20 61 20 62 75  to point at a bu
2f779 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
2f77a 74 68 65 20 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c  the .  ** normal
2f77b 69 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  ized version of 
2f77c 74 68 65 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20  the token (i.e. 
2f77d 61 66 74 65 72 20 61 6e 79 20 63 61 73 65 2d 66  after any case-f
2f77e 6f 6c 64 69 6e 67 20 61 6e 64 2f 6f 72 0a 20 20  olding and/or.  
2f77f 2a 2a 20 73 74 65 6d 6d 69 6e 67 20 68 61 73 20  ** stemming has 
2f780 62 65 65 6e 20 70 65 72 66 6f 72 6d 65 64 29 2e  been performed).
2f781 20 2a 70 6e 42 79 74 65 73 20 73 68 6f 75 6c 64   *pnBytes should
2f782 20 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 6c   be set to the l
2f783 65 6e 67 74 68 0a 20 20 2a 2a 20 6f 66 20 74 68  ength.  ** of th
2f784 69 73 20 62 75 66 66 65 72 20 69 6e 20 62 79 74  is buffer in byt
2f785 65 73 2e 20 54 68 65 20 69 6e 70 75 74 20 74 65  es. The input te
2f786 78 74 20 74 68 61 74 20 67 65 6e 65 72 61 74 65  xt that generate
2f787 64 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 0a 20  d the token is. 
2f788 20 2a 2a 20 69 64 65 6e 74 69 66 69 65 64 20 62   ** identified b
2f789 79 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65  y the byte offse
2f78a 74 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a  ts returned in *
2f78b 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 61 6e  piStartOffset an
2f78c 64 0a 20 20 2a 2a 20 2a 70 69 45 6e 64 4f 66 66  d.  ** *piEndOff
2f78d 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  set..  **.  ** T
2f78e 68 65 20 62 75 66 66 65 72 20 2a 70 70 54 6f 6b  he buffer *ppTok
2f78f 65 6e 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  en is set to poi
2f790 6e 74 20 61 74 20 69 73 20 6d 61 6e 61 67 65 64  nt at is managed
2f791 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65   by the tokenize
2f792 72 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  r.  ** implement
2f793 61 74 69 6f 6e 2e 20 49 74 20 69 73 20 6f 6e 6c  ation. It is onl
2f794 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65  y required to be
2f795 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65   valid until the
2f796 20 6e 65 78 74 20 63 61 6c 6c 0a 20 20 2a 2a 20   next call.  ** 
2f797 74 6f 20 78 4e 65 78 74 28 29 20 6f 72 20 78 43  to xNext() or xC
2f798 6c 6f 73 65 28 29 2e 20 0a 20 20 2a 2f 0a 20 20  lose(). .  */.  
2f799 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 63  /* TODO(shess) c
2f79a 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74  urrent implement
2f79b 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 70  ation requires p
2f79c 49 6e 70 75 74 20 74 6f 20 62 65 0a 20 20 2a 2a  Input to be.  **
2f79d 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 2e   nul-terminated.
2f79e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 65 69    This should ei
2f79f 74 68 65 72 20 62 65 20 66 69 78 65 64 2c 20 6f  ther be fixed, o
2f7a0 72 20 70 49 6e 70 75 74 2f 6e 42 79 74 65 73 0a  r pInput/nBytes.
2f7a1 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63    ** should be c
2f7a2 6f 6e 76 65 72 74 65 64 20 74 6f 20 7a 49 6e 70  onverted to zInp
2f7a3 75 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  ut..  */.  int (
2f7a4 2a 78 4e 65 78 74 29 28 0a 20 20 20 20 73 71 6c  *xNext)(.    sql
2f7a5 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
2f7a6 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20  ursor *pCursor, 
2f7a7 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 63    /* Tokenizer c
2f7a8 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 63 6f 6e  ursor */.    con
2f7a9 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f 6b 65  st char **ppToke
2f7aa 6e 2c 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c  n, int *pnBytes,
2f7ab 20 20 2f 2a 20 4f 55 54 3a 20 4e 6f 72 6d 61 6c    /* OUT: Normal
2f7ac 69 7a 65 64 20 74 65 78 74 20 66 6f 72 20 74 6f  ized text for to
2f7ad 6b 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a  ken */.    int *
2f7ae 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20  piStartOffset,  
2f7af 2f 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66 66  /* OUT: Byte off
2f7b0 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 69 6e 20  set of token in 
2f7b1 69 6e 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a  input buffer */.
2f7b2 20 20 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66      int *piEndOf
2f7b3 66 73 65 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  fset,    /* OUT:
2f7b4 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20   Byte offset of 
2f7b5 65 6e 64 20 6f 66 20 74 6f 6b 65 6e 20 69 6e 20  end of token in 
2f7b6 69 6e 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a  input buffer */.
2f7b7 20 20 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74      int *piPosit
2f7b8 69 6f 6e 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  ion      /* OUT:
2f7b9 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e   Number of token
2f7ba 73 20 72 65 74 75 72 6e 65 64 20 62 65 66 6f 72  s returned befor
2f7bb 65 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20  e this one */.  
2f7bc 29 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 73 71  );.};..struct sq
2f7bd 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
2f7be 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  {.  const sqlite
2f7bf 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
2f7c0 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f 2a  le *pModule;  /*
2f7c1 20 54 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20   The module for 
2f7c2 74 68 69 73 20 74 6f 6b 65 6e 69 7a 65 72 20 2a  this tokenizer *
2f7c3 2f 0a 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72  /.  /* Tokenizer
2f7c4 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
2f7c5 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
2f7c6 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
2f7c7 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 73 74 72  ields */.};..str
2f7c8 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  uct sqlite3_toke
2f7c9 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20  nizer_cursor {. 
2f7ca 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2f7cb 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20  er *pTokenizer; 
2f7cc 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a        /* Tokeniz
2f7cd 65 72 20 66 6f 72 20 74 68 69 73 20 63 75 72 73  er for this curs
2f7ce 6f 72 2e 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b 65  or. */.  /* Toke
2f7cf 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61  nizer implementa
2f7d0 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63  tions will typic
2f7d1 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f  ally add additio
2f7d2 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b  nal fields */.};
2f7d3 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 46 54 53  ..#endif /* _FTS
2f7d4 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 20 2a  3_TOKENIZER_H_ *
2f7d5 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2f7d6 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 74  ** End of fts3_t
2f7d7 6f 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a  okenizer.h *****
2f7d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f7d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f7da 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2f7db 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
2f7dc 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
2f7dd 69 6e 20 66 74 73 33 5f 65 78 70 72 2e 68 20 2a  in fts3_expr.h *
2f7de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f7df 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  */../*.** The fo
2f7e0 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 69 62 65  llowing describe
2f7e1 73 20 74 68 65 20 73 79 6e 74 61 78 20 73 75 70  s the syntax sup
2f7e2 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 66 74  ported by the ft
2f7e3 73 33 20 4d 41 54 43 48 0a 2a 2a 20 6f 70 65 72  s3 MATCH.** oper
2f7e4 61 74 6f 72 20 69 6e 20 61 20 73 69 6d 69 6c 61  ator in a simila
2f7e5 72 20 66 6f 72 6d 61 74 20 74 6f 20 74 68 61 74  r format to that
2f7e6 20 75 73 65 64 20 62 79 20 74 68 65 20 6c 65 6d   used by the lem
2f7e7 6f 6e 20 70 61 72 73 65 72 0a 2a 2a 20 67 65 6e  on parser.** gen
2f7e8 65 72 61 74 6f 72 2e 20 54 68 69 73 20 6d 6f 64  erator. This mod
2f7e9 75 6c 65 20 64 6f 65 73 20 6e 6f 74 20 75 73 65  ule does not use
2f7ea 20 61 63 74 75 61 6c 6c 79 20 6c 65 6d 6f 6e 2c   actually lemon,
2f7eb 20 69 74 20 75 73 65 73 20 61 0a 2a 2a 20 63 75   it uses a.** cu
2f7ec 73 74 6f 6d 20 70 61 72 73 65 72 2e 0a 2a 2a 0a  stom parser..**.
2f7ed 2a 2a 20 20 20 71 75 65 72 79 20 3a 3a 3d 20 61  **   query ::= a
2f7ee 6e 64 65 78 70 72 20 28 4f 52 20 61 6e 64 65 78  ndexpr (OR andex
2f7ef 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 6e  pr)*..**.**   an
2f7f0 64 65 78 70 72 20 3a 3a 3d 20 6e 6f 74 65 78 70  dexpr ::= notexp
2f7f1 72 20 28 41 4e 44 3f 20 6e 6f 74 65 78 70 72 29  r (AND? notexpr)
2f7f2 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 6e 6f 74 65 78  *..**.**   notex
2f7f3 70 72 20 3a 3a 3d 20 6e 65 61 72 65 78 70 72 20  pr ::= nearexpr 
2f7f4 28 4e 4f 54 20 6e 65 61 72 65 78 70 72 7c 2d 54  (NOT nearexpr|-T
2f7f5 4f 4b 45 4e 29 2a 2e 0a 2a 2a 20 20 20 6e 6f 74  OKEN)*..**   not
2f7f6 65 78 70 72 20 3a 3a 3d 20 4c 50 20 71 75 65 72  expr ::= LP quer
2f7f7 79 20 52 50 2e 0a 2a 2a 0a 2a 2a 20 20 20 6e 65  y RP..**.**   ne
2f7f8 61 72 65 78 70 72 20 3a 3a 3d 20 70 68 72 61 73  arexpr ::= phras
2f7f9 65 20 28 4e 45 41 52 20 64 69 73 74 61 6e 63 65  e (NEAR distance
2f7fa 5f 6f 70 74 20 6e 65 61 72 65 78 70 72 29 2a 2e  _opt nearexpr)*.
2f7fb 0a 2a 2a 0a 2a 2a 20 20 20 64 69 73 74 61 6e 63  .**.**   distanc
2f7fc 65 5f 6f 70 74 20 3a 3a 3d 20 2e 0a 2a 2a 20 20  e_opt ::= ..**  
2f7fd 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a 3a   distance_opt ::
2f7fe 3d 20 2f 20 49 4e 54 45 47 45 52 2e 0a 2a 2a 0a  = / INTEGER..**.
2f7ff 2a 2a 20 20 20 70 68 72 61 73 65 20 3a 3a 3d 20  **   phrase ::= 
2f800 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20 70 68 72 61  TOKEN..**   phra
2f801 73 65 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 3a 54 4f  se ::= COLUMN:TO
2f802 4b 45 4e 2e 0a 2a 2a 20 20 20 70 68 72 61 73 65  KEN..**   phrase
2f803 20 3a 3a 3d 20 22 54 4f 4b 45 4e 20 54 4f 4b 45   ::= "TOKEN TOKE
2f804 4e 20 54 4f 4b 45 4e 2e 2e 2e 22 2e 0a 2a 2f 0a  N TOKEN..."..*/.
2f805 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2f806 46 74 73 33 45 78 70 72 20 46 74 73 33 45 78 70  Fts3Expr Fts3Exp
2f807 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
2f808 74 20 46 74 73 33 50 68 72 61 73 65 20 46 74 73  t Fts3Phrase Fts
2f809 33 50 68 72 61 73 65 3b 0a 0a 2f 2a 0a 2a 2a 20  3Phrase;../*.** 
2f80a 41 20 22 70 68 72 61 73 65 22 20 69 73 20 61 20  A "phrase" is a 
2f80b 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20  sequence of one 
2f80c 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 20 74  or more tokens t
2f80d 68 61 74 20 6d 75 73 74 20 6d 61 74 63 68 20 69  hat must match i
2f80e 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 2e 20 20  n.** sequence.  
2f80f 41 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 20 69  A single token i
2f810 73 20 74 68 65 20 62 61 73 65 20 63 61 73 65 20  s the base case 
2f811 61 6e 64 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d  and the most com
2f812 6d 6f 6e 20 63 61 73 65 2e 0a 2a 2a 20 46 6f 72  mon case..** For
2f813 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74   a sequence of t
2f814 6f 6b 65 6e 73 20 63 6f 6e 74 61 69 6e 65 64 20  okens contained 
2f815 69 6e 20 22 2e 2e 2e 22 2c 20 6e 54 6f 6b 65 6e  in "...", nToken
2f816 20 77 69 6c 6c 20 62 65 20 74 68 65 20 6e 75 6d   will be the num
2f817 62 65 72 0a 2a 2a 20 6f 66 20 74 6f 6b 65 6e 73  ber.** of tokens
2f818 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 2e 0a   in the string..
2f819 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33 50 68  */.struct Fts3Ph
2f81a 72 61 73 65 20 7b 0a 20 20 69 6e 74 20 6e 54 6f  rase {.  int nTo
2f81b 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ken;          /*
2f81c 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e   Number of token
2f81d 73 20 69 6e 20 74 68 65 20 70 68 72 61 73 65 20  s in the phrase 
2f81e 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  */.  int iColumn
2f81f 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  ;         /* Ind
2f820 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 74 68 69  ex of column thi
2f821 73 20 70 68 72 61 73 65 20 6d 75 73 74 20 6d 61  s phrase must ma
2f822 74 63 68 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4e  tch */.  int isN
2f823 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ot;           /*
2f824 20 50 68 72 61 73 65 20 70 72 65 66 69 78 65 64   Phrase prefixed
2f825 20 62 79 20 75 6e 61 72 79 20 6e 6f 74 20 28 2d   by unary not (-
2f826 29 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  ) operator */.  
2f827 73 74 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b  struct PhraseTok
2f828 65 6e 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  en {.    char *z
2f829 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2f82a 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 74 6f  * Text of the to
2f82b 6b 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ken */.    int n
2f82c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2f82d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
2f82e 74 65 73 20 69 6e 20 62 75 66 66 65 72 20 70 6f  tes in buffer po
2f82f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 20 2a 2f  inted to by z */
2f830 0a 20 20 20 20 69 6e 74 20 69 73 50 72 65 66 69  .    int isPrefi
2f831 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  x;         /* Tr
2f832 75 65 20 69 66 20 74 6f 6b 65 6e 20 65 6e 64 73  ue if token ends
2f833 20 69 6e 20 77 69 74 68 20 61 20 22 2a 22 20 63   in with a "*" c
2f834 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 7d 20  haracter */.  } 
2f835 61 54 6f 6b 65 6e 5b 31 5d 3b 20 20 20 20 20 20  aToken[1];      
2f836 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20     /* One entry 
2f837 66 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 69  for each token i
2f838 6e 20 74 68 65 20 70 68 72 61 73 65 20 2a 2f 0a  n the phrase */.
2f839 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 65 65  };../*.** A tree
2f83a 20 6f 66 20 74 68 65 73 65 20 6f 62 6a 65 63 74   of these object
2f83b 73 20 66 6f 72 6d 73 20 74 68 65 20 52 48 53 20  s forms the RHS 
2f83c 6f 66 20 61 20 4d 41 54 43 48 20 6f 70 65 72 61  of a MATCH opera
2f83d 74 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46  tor..*/.struct F
2f83e 74 73 33 45 78 70 72 20 7b 0a 20 20 69 6e 74 20  ts3Expr {.  int 
2f83f 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20  eType;          
2f840 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
2f841 20 74 68 65 20 46 54 53 51 55 45 52 59 5f 58 58   the FTSQUERY_XX
2f842 58 20 76 61 6c 75 65 73 20 64 65 66 69 6e 65 64  X values defined
2f843 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20   below */.  int 
2f844 6e 4e 65 61 72 3b 20 20 20 20 20 20 20 20 20 20  nNear;          
2f845 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20         /* Valid 
2f846 69 66 20 65 54 79 70 65 3d 3d 46 54 53 51 55 45  if eType==FTSQUE
2f847 52 59 5f 4e 45 41 52 20 2a 2f 0a 20 20 46 74 73  RY_NEAR */.  Fts
2f848 33 45 78 70 72 20 2a 70 50 61 72 65 6e 74 3b 20  3Expr *pParent; 
2f849 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 72 65          /* pPare
2f84a 6e 74 2d 3e 70 4c 65 66 74 3d 3d 74 68 69 73 20  nt->pLeft==this 
2f84b 6f 72 20 70 50 61 72 65 6e 74 2d 3e 70 52 69 67  or pParent->pRig
2f84c 68 74 3d 3d 74 68 69 73 20 2a 2f 0a 20 20 46 74  ht==this */.  Ft
2f84d 73 33 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 20  s3Expr *pLeft;  
2f84e 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74           /* Left
2f84f 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 46 74   operand */.  Ft
2f850 73 33 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20  s3Expr *pRight; 
2f851 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68           /* Righ
2f852 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 46  t operand */.  F
2f853 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61  ts3Phrase *pPhra
2f854 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c  se;       /* Val
2f855 69 64 20 69 66 20 65 54 79 70 65 3d 3d 46 54 53  id if eType==FTS
2f856 51 55 45 52 59 5f 50 48 52 41 53 45 20 2a 2f 0a  QUERY_PHRASE */.
2f857 7d 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  };..SQLITE_PRIVA
2f858 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
2f859 73 33 45 78 70 72 50 61 72 73 65 28 73 71 6c 69  s3ExprParse(sqli
2f85a 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2c  te3_tokenizer *,
2f85b 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74 2c 20 69   char **, int, i
2f85c 6e 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  nt, .           
2f85d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
2f85e 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c  nst char *, int,
2f85f 20 46 74 73 33 45 78 70 72 20 2a 2a 29 3b 0a 53   Fts3Expr **);.S
2f860 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2f861 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  id sqlite3Fts3Ex
2f862 70 72 46 72 65 65 28 46 74 73 33 45 78 70 72 20  prFree(Fts3Expr 
2f863 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6e 64 69  *);../*.** Candi
2f864 64 61 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20  date values for 
2f865 46 74 73 33 51 75 65 72 79 2e 65 54 79 70 65 2e  Fts3Query.eType.
2f866 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f   Note that the o
2f867 72 64 65 72 20 6f 66 20 74 68 65 20 66 69 72 73  rder of the firs
2f868 74 0a 2a 2a 20 66 6f 75 72 20 76 61 6c 75 65 73  t.** four values
2f869 20 69 73 20 69 6e 20 6f 72 64 65 72 20 6f 66 20   is in order of 
2f86a 70 72 65 63 65 64 65 6e 63 65 20 77 68 65 6e 20  precedence when 
2f86b 70 61 72 73 69 6e 67 20 65 78 70 72 65 73 73 69  parsing expressi
2f86c 6f 6e 73 2e 20 46 6f 72 20 0a 2a 2a 20 65 78 61  ons. For .** exa
2f86d 6d 70 6c 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  mple, the follow
2f86e 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61 20  ing:.**.**   "a 
2f86f 4f 52 20 62 20 41 4e 44 20 63 20 4e 4f 54 20 64  OR b AND c NOT d
2f870 20 4e 45 41 52 20 65 22 0a 2a 2a 0a 2a 2a 20 69   NEAR e".**.** i
2f871 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a  s equivalent to:
2f872 0a 2a 2a 0a 2a 2a 20 20 20 22 61 20 4f 52 20 28  .**.**   "a OR (
2f873 62 20 41 4e 44 20 28 63 20 4e 4f 54 20 28 64 20  b AND (c NOT (d 
2f874 4e 45 41 52 20 65 29 29 29 22 0a 2a 2f 0a 23 64  NEAR e)))".*/.#d
2f875 65 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4e  efine FTSQUERY_N
2f876 45 41 52 20 20 20 31 0a 23 64 65 66 69 6e 65 20  EAR   1.#define 
2f877 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 20 20 20  FTSQUERY_NOT    
2f878 32 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45  2.#define FTSQUE
2f879 52 59 5f 41 4e 44 20 20 20 20 33 0a 23 64 65 66  RY_AND    3.#def
2f87a 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4f 52 20  ine FTSQUERY_OR 
2f87b 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 46 54      4.#define FT
2f87c 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 35 0a  SQUERY_PHRASE 5.
2f87d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
2f87e 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EST.SQLITE_PRIVA
2f87f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
2f880 74 73 33 45 78 70 72 49 6e 69 74 54 65 73 74 49  ts3ExprInitTestI
2f881 6e 74 65 72 66 61 63 65 28 73 71 6c 69 74 65 33  nterface(sqlite3
2f882 20 2a 64 62 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f   *db);.#endif../
2f883 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
2f884 6e 64 20 6f 66 20 66 74 73 33 5f 65 78 70 72 2e  nd of fts3_expr.
2f885 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
2f886 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f887 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2f888 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
2f889 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
2f88a 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 66  we left off in f
2f88b 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ts3.c **********
2f88c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2f88d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
2f88e 6e 63 6c 75 64 65 20 66 74 73 33 5f 68 61 73 68  nclude fts3_hash
2f88f 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
2f890 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a   of fts3.c *****
2f891 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2f892 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
2f893 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 68  egin file fts3_h
2f894 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.h **********
2f895 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f896 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2f897 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
2f898 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 22.**.** The
2f899 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
2f89a 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
2f89b 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
2f89c 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
2f89d 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2f89e 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2f89f 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2f8a0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2f8a1 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
2f8a2 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
2f8a3 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
2f8a4 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
2f8a5 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
2f8a6 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2f8a7 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
2f8a8 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
2f8a9 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
2f8aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8ae 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  *****.** This is
2f8af 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65   the header file
2f8b0 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69 63   for the generic
2f8b1 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c   hash-table impl
2f8b2 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65  emenation.** use
2f8b3 64 20 69 6e 20 53 51 4c 69 74 65 2e 20 20 57 65  d in SQLite.  We
2f8b4 27 76 65 20 6d 6f 64 69 66 69 65 64 20 69 74 20  've modified it 
2f8b5 73 6c 69 67 68 74 6c 79 20 74 6f 20 73 65 72 76  slightly to serv
2f8b6 65 20 61 73 20 61 20 73 74 61 6e 64 61 6c 6f 6e  e as a standalon
2f8b7 65 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20  e.** hash table 
2f8b8 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66  implementation f
2f8b9 6f 72 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74  or the full-text
2f8ba 20 69 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c 65   indexing module
2f8bb 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ..**.*/.#ifndef 
2f8bc 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a 23 64  _FTS3_HASH_H_.#d
2f8bd 65 66 69 6e 65 20 5f 46 54 53 33 5f 48 41 53 48  efine _FTS3_HASH
2f8be 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  _H_../* Forward 
2f8bf 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20  declarations of 
2f8c0 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74  structures. */.t
2f8c1 79 70 65 64 65 66 20 73 74 72 75 63 74 20 66 74  ypedef struct ft
2f8c2 73 33 48 61 73 68 20 66 74 73 33 48 61 73 68 3b  s3Hash fts3Hash;
2f8c3 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2f8c4 66 74 73 33 48 61 73 68 45 6c 65 6d 20 66 74 73  fts3HashElem fts
2f8c5 33 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41  3HashElem;../* A
2f8c6 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74   complete hash t
2f8c7 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61  able is an insta
2f8c8 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
2f8c9 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
2f8ca 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73  ** The internals
2f8cb 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
2f8cc 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  re are intended 
2f8cd 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20  to be opaque -- 
2f8ce 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73  client.** code s
2f8cf 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70  hould not attemp
2f8d0 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d  t to access or m
2f8d1 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c 64 73  odify the fields
2f8d2 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
2f8d3 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20  re.** directly. 
2f8d4 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 74 72   Change this str
2f8d5 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75  ucture only by u
2f8d6 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65  sing the routine
2f8d7 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65  s below..** Howe
2f8d8 76 65 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68 65  ver, many of the
2f8d9 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e   "procedures" an
2f8da 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f  d "functions" fo
2f8db 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a  r modifying and.
2f8dc 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 69  ** accessing thi
2f8dd 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
2f8de 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73  really macros, s
2f8df 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c  o we can't reall
2f8e0 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73  y make.** this s
2f8e1 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e  tructure opaque.
2f8e2 0a 2a 2f 0a 73 74 72 75 63 74 20 66 74 73 33 48  .*/.struct fts3H
2f8e3 61 73 68 20 7b 0a 20 20 63 68 61 72 20 6b 65 79  ash {.  char key
2f8e4 43 6c 61 73 73 3b 20 20 20 20 20 20 20 20 20 20  Class;          
2f8e5 2f 2a 20 48 41 53 48 5f 49 4e 54 2c 20 5f 50 4f  /* HASH_INT, _PO
2f8e6 49 4e 54 45 52 2c 20 5f 53 54 52 49 4e 47 2c 20  INTER, _STRING, 
2f8e7 5f 42 49 4e 41 52 59 20 2a 2f 0a 20 20 63 68 61  _BINARY */.  cha
2f8e8 72 20 63 6f 70 79 4b 65 79 3b 20 20 20 20 20 20  r copyKey;      
2f8e9 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2f8ea 63 6f 70 79 20 6f 66 20 6b 65 79 20 6d 61 64 65  copy of key made
2f8eb 20 6f 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20   on insert */.  
2f8ec 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
2f8ed 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2f8ee 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
2f8ef 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
2f8f0 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 66 69  fts3HashElem *fi
2f8f1 72 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 66  rst;    /* The f
2f8f2 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
2f8f3 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 69  the array */.  i
2f8f4 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20  nt htsize;      
2f8f5 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2f8f6 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74   of buckets in t
2f8f7 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f  he hash table */
2f8f8 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68  .  struct _fts3h
2f8f9 74 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 74 68  t {        /* th
2f8fa 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a  e hash table */.
2f8fb 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20      int count;  
2f8fc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f8fd 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
2f8fe 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68  s with this hash
2f8ff 20 2a 2f 0a 20 20 20 20 66 74 73 33 48 61 73 68   */.    fts3Hash
2f900 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20  Elem *chain;    
2f901 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66   /* Pointer to f
2f902 69 72 73 74 20 65 6e 74 72 79 20 77 69 74 68 20  irst entry with 
2f903 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d  this hash */.  }
2f904 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63   *ht;.};../* Eac
2f905 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65  h element in the
2f906 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61   hash table is a
2f907 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
2f908 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20  e following .** 
2f909 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c 6c 20  structure.  All 
2f90a 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 74 6f  elements are sto
2f90b 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  red on a single 
2f90c 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69  doubly-linked li
2f90d 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c  st..**.** Again,
2f90e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
2f90f 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62  is intended to b
2f910 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74  e opaque, but it
2f911 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a   can't really.**
2f912 20 62 65 20 6f 70 61 71 75 65 20 62 65 63 61 75   be opaque becau
2f913 73 65 20 69 74 20 69 73 20 75 73 65 64 20 62 79  se it is used by
2f914 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75   macros..*/.stru
2f915 63 74 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20  ct fts3HashElem 
2f916 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d  {.  fts3HashElem
2f917 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 2f   *next, *prev; /
2f918 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69  * Next and previ
2f919 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  ous elements in 
2f91a 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76  the table */.  v
2f91b 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20  oid *data;      
2f91c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2f91d 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
2f91e 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a  h this element *
2f91f 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20  /.  void *pKey; 
2f920 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f  int nKey;      /
2f921 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64  * Key associated
2f922 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65   with this eleme
2f923 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
2f924 54 68 65 72 65 20 61 72 65 20 32 20 64 69 66 66  There are 2 diff
2f925 65 72 65 6e 74 20 6d 6f 64 65 73 20 6f 66 20 6f  erent modes of o
2f926 70 65 72 61 74 69 6f 6e 20 66 6f 72 20 61 20 68  peration for a h
2f927 61 73 68 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a  ash table:.**.**
2f928 20 20 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52     FTS3_HASH_STR
2f929 49 4e 47 20 20 20 20 20 20 20 20 70 4b 65 79 20  ING        pKey 
2f92a 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69  points to a stri
2f92b 6e 67 20 74 68 61 74 20 69 73 20 6e 4b 65 79 20  ng that is nKey 
2f92c 62 79 74 65 73 20 6c 6f 6e 67 0a 2a 2a 20 20 20  bytes long.**   
2f92d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f92e 20 20 20 20 20 20 20 20 28 69 6e 63 6c 75 64 69          (includi
2f92f 6e 67 20 74 68 65 20 6e 75 6c 6c 2d 74 65 72 6d  ng the null-term
2f930 69 6e 61 74 6f 72 2c 20 69 66 20 61 6e 79 29 2e  inator, if any).
2f931 20 20 43 61 73 65 0a 2a 2a 20 20 20 20 20 20 20    Case.**       
2f932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f933 20 20 20 20 69 73 20 72 65 73 70 65 63 74 65 64      is respected
2f934 20 69 6e 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e   in comparisons.
2f935 0a 2a 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41  .**.**   FTS3_HA
2f936 53 48 5f 42 49 4e 41 52 59 20 20 20 20 20 20 20  SH_BINARY       
2f937 20 70 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20   pKey points to 
2f938 62 69 6e 61 72 79 20 64 61 74 61 20 6e 4b 65 79  binary data nKey
2f939 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 0a 2a 2a   bytes long. .**
2f93a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f93b 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 6d             memcm
2f93c 70 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 63  p() is used to c
2f93d 6f 6d 70 61 72 65 20 6b 65 79 73 2e 0a 2a 2a 0a  ompare keys..**.
2f93e 2a 2a 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65  ** A copy of the
2f93f 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66 20   key is made if 
2f940 74 68 65 20 63 6f 70 79 4b 65 79 20 70 61 72 61  the copyKey para
2f941 6d 65 74 65 72 20 74 6f 20 66 74 73 33 48 61 73  meter to fts3Has
2f942 68 49 6e 69 74 20 69 73 20 31 2e 20 20 0a 2a 2f  hInit is 1.  .*/
2f943 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 48 41  .#define FTS3_HA
2f944 53 48 5f 53 54 52 49 4e 47 20 20 20 20 31 0a 23  SH_STRING    1.#
2f945 64 65 66 69 6e 65 20 46 54 53 33 5f 48 41 53 48  define FTS3_HASH
2f946 5f 42 49 4e 41 52 59 20 20 20 20 32 0a 0a 2f 2a  _BINARY    2../*
2f947 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69  .** Access routi
2f948 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c  nes.  To delete,
2f949 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70   insert a NULL p
2f94a 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
2f94b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2f94c 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
2f94d 69 74 28 66 74 73 33 48 61 73 68 2a 2c 20 69 6e  it(fts3Hash*, in
2f94e 74 20 6b 65 79 74 79 70 65 2c 20 69 6e 74 20 63  t keytype, int c
2f94f 6f 70 79 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f  opyKey);.SQLITE_
2f950 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
2f951 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73  lite3Fts3HashIns
2f952 65 72 74 28 66 74 73 33 48 61 73 68 2a 2c 20 63  ert(fts3Hash*, c
2f953 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
2f954 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20   int nKey, void 
2f955 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f  *pData);.SQLITE_
2f956 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
2f957 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e  lite3Fts3HashFin
2f958 64 28 63 6f 6e 73 74 20 66 74 73 33 48 61 73 68  d(const fts3Hash
2f959 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  *, const void *p
2f95a 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a  Key, int nKey);.
2f95b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2f95c 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 48  oid sqlite3Fts3H
2f95d 61 73 68 43 6c 65 61 72 28 66 74 73 33 48 61 73  ashClear(fts3Has
2f95e 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 68 6f 72  h*);../*.** Shor
2f95f 74 68 61 6e 64 20 66 6f 72 20 74 68 65 20 66 75  thand for the fu
2f960 6e 63 74 69 6f 6e 73 20 61 62 6f 76 65 0a 2a 2f  nctions above.*/
2f961 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73  .#define fts3Has
2f962 68 49 6e 69 74 20 20 20 73 71 6c 69 74 65 33 46  hInit   sqlite3F
2f963 74 73 33 48 61 73 68 49 6e 69 74 0a 23 64 65 66  ts3HashInit.#def
2f964 69 6e 65 20 66 74 73 33 48 61 73 68 49 6e 73 65  ine fts3HashInse
2f965 72 74 20 73 71 6c 69 74 65 33 46 74 73 33 48 61  rt sqlite3Fts3Ha
2f966 73 68 49 6e 73 65 72 74 0a 23 64 65 66 69 6e 65  shInsert.#define
2f967 20 66 74 73 33 48 61 73 68 46 69 6e 64 20 20 20   fts3HashFind   
2f968 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46  sqlite3Fts3HashF
2f969 69 6e 64 0a 23 64 65 66 69 6e 65 20 66 74 73 33  ind.#define fts3
2f96a 48 61 73 68 43 6c 65 61 72 20 20 73 71 6c 69 74  HashClear  sqlit
2f96b 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 0a  e3Fts3HashClear.
2f96c 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
2f96d 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61  r looping over a
2f96e 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61  ll elements of a
2f96f 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68   hash table.  Th
2f970 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69  e idiom is.** li
2f971 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
2f972 20 66 74 73 33 48 61 73 68 20 68 3b 0a 2a 2a 20   fts3Hash h;.** 
2f973 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    fts3HashElem *
2f974 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20  p;.**   ....**  
2f975 20 66 6f 72 28 70 3d 66 74 73 33 48 61 73 68 46   for(p=fts3HashF
2f976 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d 66  irst(&h); p; p=f
2f977 74 73 33 48 61 73 68 4e 65 78 74 28 70 29 29 7b  ts3HashNext(p)){
2f978 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72 75  .**     SomeStru
2f979 63 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20 66  cture *pData = f
2f97a 74 73 33 48 61 73 68 44 61 74 61 28 70 29 3b 0a  ts3HashData(p);.
2f97b 2a 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d  **     // do som
2f97c 65 74 68 69 6e 67 20 77 69 74 68 20 70 44 61 74  ething with pDat
2f97d 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66  a.**   }.*/.#def
2f97e 69 6e 65 20 66 74 73 33 48 61 73 68 46 69 72 73  ine fts3HashFirs
2f97f 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72 73  t(H)  ((H)->firs
2f980 74 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  t).#define fts3H
2f981 61 73 68 4e 65 78 74 28 45 29 20 20 20 28 28 45  ashNext(E)   ((E
2f982 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e 65  )->next).#define
2f983 20 66 74 73 33 48 61 73 68 44 61 74 61 28 45 29   fts3HashData(E)
2f984 20 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 23     ((E)->data).#
2f985 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 4b  define fts3HashK
2f986 65 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e 70  ey(E)    ((E)->p
2f987 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 66 74 73  Key).#define fts
2f988 33 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20  3HashKeysize(E) 
2f989 28 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a  ((E)->nKey)../*.
2f98a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  ** Number of ent
2f98b 72 69 65 73 20 69 6e 20 61 20 68 61 73 68 20 74  ries in a hash t
2f98c 61 62 6c 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  able.*/.#define 
2f98d 66 74 73 33 48 61 73 68 43 6f 75 6e 74 28 48 29  fts3HashCount(H)
2f98e 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a 0a    ((H)->count)..
2f98f 23 65 6e 64 69 66 20 2f 2a 20 5f 46 54 53 33 5f  #endif /* _FTS3_
2f990 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  HASH_H_ */../***
2f991 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2f992 6f 66 20 66 74 73 33 5f 68 61 73 68 2e 68 20 2a  of fts3_hash.h *
2f993 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f994 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f995 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2f996 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
2f997 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
2f998 6c 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73 33  left off in fts3
2f999 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2f99a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 6e  **********/.#ifn
2f99b 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  def SQLITE_CORE 
2f99c 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
2f99d 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69 66  ION_INIT1.#endif
2f99e 0a 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .../* TODO(shess
2f99f 29 20 4d 41 4e 2c 20 74 68 69 73 20 74 68 69 6e  ) MAN, this thin
2f9a0 67 20 6e 65 65 64 73 20 73 6f 6d 65 20 72 65 66  g needs some ref
2f9a1 61 63 74 6f 72 69 6e 67 2e 20 20 41 74 20 6d 69  actoring.  At mi
2f9a2 6e 69 6d 75 6d 2c 20 69 74 0a 2a 2a 20 77 6f 75  nimum, it.** wou
2f9a3 6c 64 20 62 65 20 6e 69 63 65 20 74 6f 20 6f 72  ld be nice to or
2f9a4 64 65 72 20 74 68 65 20 66 69 6c 65 20 62 65 74  der the file bet
2f9a5 74 65 72 2c 20 70 65 72 68 61 70 73 20 73 6f 6d  ter, perhaps som
2f9a6 65 74 68 69 6e 67 20 61 6c 6f 6e 67 20 74 68 65  ething along the
2f9a7 0a 2a 2a 20 6c 69 6e 65 73 20 6f 66 3a 0a 2a 2a  .** lines of:.**
2f9a8 0a 2a 2a 20 20 2d 20 75 74 69 6c 69 74 79 20 66  .**  - utility f
2f9a9 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74  unctions.**  - t
2f9aa 61 62 6c 65 20 73 65 74 75 70 20 66 75 6e 63 74  able setup funct
2f9ab 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65  ions.**  - table
2f9ac 20 75 70 64 61 74 65 20 66 75 6e 63 74 69 6f 6e   update function
2f9ad 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20 71 75  s.**  - table qu
2f9ae 65 72 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a  ery functions.**
2f9af 0a 2a 2a 20 50 75 74 20 74 68 65 20 71 75 65 72  .** Put the quer
2f9b0 79 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 73 74  y functions last
2f9b1 20 62 65 63 61 75 73 65 20 74 68 65 79 27 72 65   because they're
2f9b2 20 6c 69 6b 65 6c 79 20 74 6f 20 72 65 66 65 72   likely to refer
2f9b3 65 6e 63 65 0a 2a 2a 20 74 79 70 65 64 65 66 73  ence.** typedefs
2f9b4 20 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 66 72   or functions fr
2f9b5 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 75 70 64  om the table upd
2f9b6 61 74 65 20 73 65 63 74 69 6f 6e 2e 0a 2a 2f 0a  ate section..*/.
2f9b7 0a 23 69 66 20 30 0a 23 20 64 65 66 69 6e 65 20  .#if 0.# define 
2f9b8 46 54 53 54 52 41 43 45 28 41 29 20 20 70 72 69  FTSTRACE(A)  pri
2f9b9 6e 74 66 20 41 3b 20 66 66 6c 75 73 68 28 73 74  ntf A; fflush(st
2f9ba 64 6f 75 74 29 0a 23 65 6c 73 65 0a 23 20 64 65  dout).#else.# de
2f9bb 66 69 6e 65 20 46 54 53 54 52 41 43 45 28 41 29  fine FTSTRACE(A)
2f9bc 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 74 20 69  .#endif../* It i
2f9bd 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 63 61  s not safe to ca
2f9be 6c 6c 20 69 73 73 70 61 63 65 28 29 2c 20 74 6f  ll isspace(), to
2f9bf 6c 6f 77 65 72 28 29 2c 20 6f 72 20 69 73 61 6c  lower(), or isal
2f9c0 6e 75 6d 28 29 20 6f 6e 0a 2a 2a 20 68 69 2d 62  num() on.** hi-b
2f9c1 69 74 2d 73 65 74 20 63 68 61 72 61 63 74 65 72  it-set character
2f9c2 73 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  s.  This is the 
2f9c3 73 61 6d 65 20 73 6f 6c 75 74 69 6f 6e 20 75 73  same solution us
2f9c4 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 74 6f 6b  ed in the.** tok
2f9c5 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 2f 2a 20 54 4f  enizer..*/./* TO
2f9c6 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 73 6e  DO(shess) The sn
2f9c7 69 70 70 65 74 2d 67 65 6e 65 72 61 74 69 6f 6e  ippet-generation
2f9c8 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 20   code should be 
2f9c9 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 74 6f 6b  using the.** tok
2f9ca 65 6e 69 7a 65 72 2d 67 65 6e 65 72 61 74 65 64  enizer-generated
2f9cb 20 74 6f 6b 65 6e 73 20 72 61 74 68 65 72 20 74   tokens rather t
2f9cc 68 61 6e 20 64 6f 69 6e 67 20 69 74 73 20 6f 77  han doing its ow
2f9cd 6e 20 6c 6f 63 61 6c 0a 2a 2a 20 74 6f 6b 65 6e  n local.** token
2f9ce 69 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 54  ization..*/./* T
2f9cf 4f 44 4f 28 73 68 65 73 73 29 20 49 73 20 5f 5f  ODO(shess) Is __
2f9d0 69 73 61 73 63 69 69 28 29 20 61 20 70 6f 72 74  isascii() a port
2f9d1 61 62 6c 65 20 76 65 72 73 69 6f 6e 20 6f 66 20  able version of 
2f9d2 28 63 26 30 78 38 30 29 3d 3d 30 3f 20 2a 2f 0a  (c&0x80)==0? */.
2f9d3 73 74 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f  static int safe_
2f9d4 69 73 73 70 61 63 65 28 63 68 61 72 20 63 29 7b  isspace(char c){
2f9d5 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38  .  return (c&0x8
2f9d6 30 29 3d 3d 30 20 3f 20 69 73 73 70 61 63 65 28  0)==0 ? isspace(
2f9d7 63 29 20 3a 20 30 3b 0a 7d 0a 73 74 61 74 69 63  c) : 0;.}.static
2f9d8 20 69 6e 74 20 73 61 66 65 5f 74 6f 6c 6f 77 65   int safe_tolowe
2f9d9 72 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74  r(char c){.  ret
2f9da 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20  urn (c&0x80)==0 
2f9db 3f 20 74 6f 6c 6f 77 65 72 28 63 29 20 3a 20 63  ? tolower(c) : c
2f9dc 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  ;.}.static int s
2f9dd 61 66 65 5f 69 73 61 6c 6e 75 6d 28 63 68 61 72  afe_isalnum(char
2f9de 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63   c){.  return (c
2f9df 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 61 6c  &0x80)==0 ? isal
2f9e0 6e 75 6d 28 63 29 20 3a 20 30 3b 0a 7d 0a 0a 74  num(c) : 0;.}..t
2f9e1 79 70 65 64 65 66 20 65 6e 75 6d 20 44 6f 63 4c  ypedef enum DocL
2f9e2 69 73 74 54 79 70 65 20 7b 0a 20 20 44 4c 5f 44  istType {.  DL_D
2f9e3 4f 43 49 44 53 2c 20 20 20 20 20 20 20 20 20 20  OCIDS,          
2f9e4 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 6f 6e      /* docids on
2f9e5 6c 79 20 2a 2f 0a 20 20 44 4c 5f 50 4f 53 49 54  ly */.  DL_POSIT
2f9e6 49 4f 4e 53 2c 20 20 20 20 20 20 20 20 20 20 20  IONS,           
2f9e7 2f 2a 20 64 6f 63 69 64 73 20 2b 20 70 6f 73 69  /* docids + posi
2f9e8 74 69 6f 6e 73 20 2a 2f 0a 20 20 44 4c 5f 50 4f  tions */.  DL_PO
2f9e9 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
2f9ea 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20 70     /* docids + p
2f9eb 6f 73 69 74 69 6f 6e 73 20 2b 20 6f 66 66 73 65  ositions + offse
2f9ec 74 73 20 2a 2f 0a 7d 20 44 6f 63 4c 69 73 74 54  ts */.} DocListT
2f9ed 79 70 65 3b 0a 0a 2f 2a 0a 2a 2a 20 42 79 20 64  ype;../*.** By d
2f9ee 65 66 61 75 6c 74 2c 20 6f 6e 6c 79 20 70 6f 73  efault, only pos
2f9ef 69 74 69 6f 6e 73 20 61 6e 64 20 6e 6f 74 20 6f  itions and not o
2f9f0 66 66 73 65 74 73 20 61 72 65 20 73 74 6f 72 65  ffsets are store
2f9f1 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74  d in the doclist
2f9f2 73 2e 0a 2a 2a 20 54 6f 20 63 68 61 6e 67 65 20  s..** To change 
2f9f3 74 68 69 73 20 73 6f 20 74 68 61 74 20 6f 66 66  this so that off
2f9f4 73 65 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  sets are stored 
2f9f5 74 6f 6f 2c 20 63 6f 6d 70 69 6c 65 20 77 69 74  too, compile wit
2f9f6 68 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  h.**.**         
2f9f7 20 2d 44 44 4c 5f 44 45 46 41 55 4c 54 3d 44 4c   -DDL_DEFAULT=DL
2f9f8 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45  _POSITIONS_OFFSE
2f9f9 54 53 0a 2a 2a 0a 2a 2a 20 49 66 20 44 4c 5f 44  TS.**.** If DL_D
2f9fa 45 46 41 55 4c 54 20 69 73 20 73 65 74 20 74 6f  EFAULT is set to
2f9fb 20 44 4c 5f 44 4f 43 49 44 53 2c 20 79 6f 75 72   DL_DOCIDS, your
2f9fc 20 74 61 62 6c 65 20 63 61 6e 20 6f 6e 6c 79 20   table can only 
2f9fd 62 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 69  be inserted.** i
2f9fe 6e 74 6f 20 28 6e 6f 20 64 65 6c 65 74 65 73 20  nto (no deletes 
2f9ff 6f 72 20 75 70 64 61 74 65 73 29 2e 0a 2a 2f 0a  or updates)..*/.
2fa00 23 69 66 6e 64 65 66 20 44 4c 5f 44 45 46 41 55  #ifndef DL_DEFAU
2fa01 4c 54 0a 23 20 64 65 66 69 6e 65 20 44 4c 5f 44  LT.# define DL_D
2fa02 45 46 41 55 4c 54 20 44 4c 5f 50 4f 53 49 54 49  EFAULT DL_POSITI
2fa03 4f 4e 53 0a 23 65 6e 64 69 66 0a 0a 65 6e 75 6d  ONS.#endif..enum
2fa04 20 7b 0a 20 20 50 4f 53 5f 45 4e 44 20 3d 20 30   {.  POS_END = 0
2fa05 2c 20 20 20 20 20 20 20 20 2f 2a 20 65 6e 64 20  ,        /* end 
2fa06 6f 66 20 74 68 69 73 20 70 6f 73 69 74 69 6f 6e  of this position
2fa07 20 6c 69 73 74 20 2a 2f 0a 20 20 50 4f 53 5f 43   list */.  POS_C
2fa08 4f 4c 55 4d 4e 2c 20 20 20 20 20 20 20 20 20 2f  OLUMN,         /
2fa09 2a 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 65  * followed by ne
2fa0a 77 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  w column number 
2fa0b 2a 2f 0a 20 20 50 4f 53 5f 42 41 53 45 0a 7d 3b  */.  POS_BASE.};
2fa0c 0a 0a 2f 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54  ../* MERGE_COUNT
2fa0d 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f 66   controls how of
2fa0e 74 65 6e 20 77 65 20 6d 65 72 67 65 20 73 65 67  ten we merge seg
2fa0f 6d 65 6e 74 73 20 28 73 65 65 20 63 6f 6d 6d 65  ments (see comme
2fa10 6e 74 20 61 74 0a 2a 2a 20 74 6f 70 20 6f 66 20  nt at.** top of 
2fa11 66 69 6c 65 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  file)..*/.#defin
2fa12 65 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 31 36  e MERGE_COUNT 16
2fa13 0a 0a 2f 2a 20 75 74 69 6c 69 74 79 20 66 75 6e  ../* utility fun
2fa14 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f 2a 20 43 4c  ctions */../* CL
2fa15 45 41 52 28 29 20 61 6e 64 20 53 43 52 41 4d 42  EAR() and SCRAMB
2fa16 4c 45 28 29 20 61 62 73 74 72 61 63 74 20 6d 65  LE() abstract me
2fa17 6d 73 65 74 28 29 20 6f 6e 20 61 20 70 6f 69 6e  mset() on a poin
2fa18 74 65 72 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a  ter to a single.
2fa19 2a 2a 20 72 65 63 6f 72 64 20 74 6f 20 70 72 65  ** record to pre
2fa1a 76 65 6e 74 20 65 72 72 6f 72 73 20 6f 66 20 74  vent errors of t
2fa1b 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 6d  he form:.**.** m
2fa1c 79 5f 66 75 6e 63 74 69 6f 6e 28 53 6f 6d 65 54  y_function(SomeT
2fa1d 79 70 65 20 2a 62 29 7b 0a 2a 2a 20 20 20 6d 65  ype *b){.**   me
2fa1e 6d 73 65 74 28 62 2c 20 27 5c 30 27 2c 20 73 69  mset(b, '\0', si
2fa1f 7a 65 6f 66 28 62 29 29 3b 20 20 2f 2f 20 73 69  zeof(b));  // si
2fa20 7a 65 6f 66 28 62 29 21 3d 73 69 7a 65 6f 66 28  zeof(b)!=sizeof(
2fa21 2a 62 29 0a 2a 2a 20 7d 0a 2a 2f 0a 2f 2a 20 54  *b).** }.*/./* T
2fa22 4f 44 4f 28 73 68 65 73 73 29 20 4f 62 76 69 6f  ODO(shess) Obvio
2fa23 75 73 20 63 61 6e 64 69 64 61 74 65 73 20 66 6f  us candidates fo
2fa24 72 20 61 20 68 65 61 64 65 72 20 66 69 6c 65 2e  r a header file.
2fa25 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4c 45 41   */.#define CLEA
2fa26 52 28 62 29 20 6d 65 6d 73 65 74 28 62 2c 20 27  R(b) memset(b, '
2fa27 5c 30 27 2c 20 73 69 7a 65 6f 66 28 2a 28 62 29  \0', sizeof(*(b)
2fa28 29 29 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  ))..#ifndef NDEB
2fa29 55 47 0a 23 20 20 64 65 66 69 6e 65 20 53 43 52  UG.#  define SCR
2fa2a 41 4d 42 4c 45 28 62 29 20 6d 65 6d 73 65 74 28  AMBLE(b) memset(
2fa2b 62 2c 20 30 78 35 35 2c 20 73 69 7a 65 6f 66 28  b, 0x55, sizeof(
2fa2c 2a 28 62 29 29 29 0a 23 65 6c 73 65 0a 23 20 20  *(b))).#else.#  
2fa2d 64 65 66 69 6e 65 20 53 43 52 41 4d 42 4c 45 28  define SCRAMBLE(
2fa2e 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 57 65  b).#endif../* We
2fa2f 20 6d 61 79 20 6e 65 65 64 20 75 70 20 74 6f 20   may need up to 
2fa30 56 41 52 49 4e 54 5f 4d 41 58 20 62 79 74 65 73  VARINT_MAX bytes
2fa31 20 74 6f 20 73 74 6f 72 65 20 61 6e 20 65 6e 63   to store an enc
2fa32 6f 64 65 64 20 36 34 2d 62 69 74 20 69 6e 74 65  oded 64-bit inte
2fa33 67 65 72 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ger. */.#define 
2fa34 56 41 52 49 4e 54 5f 4d 41 58 20 31 30 0a 0a 2f  VARINT_MAX 10../
2fa35 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69 74  * Write a 64-bit
2fa36 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68   variable-length
2fa37 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d 6f   integer to memo
2fa38 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ry starting at p
2fa39 5b 30 5d 2e 0a 20 2a 20 54 68 65 20 6c 65 6e 67  [0].. * The leng
2fa3a 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74 74  th of data writt
2fa3b 65 6e 20 77 69 6c 6c 20 62 65 20 62 65 74 77 65  en will be betwe
2fa3c 65 6e 20 31 20 61 6e 64 20 56 41 52 49 4e 54 5f  en 1 and VARINT_
2fa3d 4d 41 58 20 62 79 74 65 73 2e 0a 20 2a 20 54 68  MAX bytes.. * Th
2fa3e 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
2fa3f 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74  s written is ret
2fa40 75 72 6e 65 64 2e 20 2a 2f 0a 73 74 61 74 69 63  urned. */.static
2fa41 20 69 6e 74 20 66 74 73 33 50 75 74 56 61 72 69   int fts3PutVari
2fa42 6e 74 28 63 68 61 72 20 2a 70 2c 20 73 71 6c 69  nt(char *p, sqli
2fa43 74 65 5f 69 6e 74 36 34 20 76 29 7b 0a 20 20 75  te_int64 v){.  u
2fa44 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71 20  nsigned char *q 
2fa45 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
2fa46 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74 65 5f   *) p;.  sqlite_
2fa47 75 69 6e 74 36 34 20 76 75 20 3d 20 76 3b 0a 20  uint64 vu = v;. 
2fa48 20 64 6f 7b 0a 20 20 20 20 2a 71 2b 2b 20 3d 20   do{.    *q++ = 
2fa49 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 20  (unsigned char) 
2fa4a 28 28 76 75 20 26 20 30 78 37 66 29 20 7c 20 30  ((vu & 0x7f) | 0
2fa4b 78 38 30 29 3b 0a 20 20 20 20 76 75 20 3e 3e 3d  x80);.    vu >>=
2fa4c 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 75   7;.  }while( vu
2fa4d 21 3d 30 20 29 3b 0a 20 20 71 5b 2d 31 5d 20 26  !=0 );.  q[-1] &
2fa4e 3d 20 30 78 37 66 3b 20 20 2f 2a 20 74 75 72 6e  = 0x7f;  /* turn
2fa4f 20 6f 66 66 20 68 69 67 68 20 62 69 74 20 69 6e   off high bit in
2fa50 20 66 69 6e 61 6c 20 62 79 74 65 20 2a 2f 0a 20   final byte */. 
2fa51 20 61 73 73 65 72 74 28 20 71 20 2d 20 28 75 6e   assert( q - (un
2fa52 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 20  signed char *)p 
2fa53 3c 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20 29 3b  <= VARINT_MAX );
2fa54 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 20  .  return (int) 
2fa55 28 71 20 2d 20 28 75 6e 73 69 67 6e 65 64 20 63  (q - (unsigned c
2fa56 68 61 72 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20  har *)p);.}../* 
2fa57 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 76 61  Read a 64-bit va
2fa58 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
2fa59 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72  teger from memor
2fa5a 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b  y starting at p[
2fa5b 30 5d 2e 0a 20 2a 20 52 65 74 75 72 6e 20 74 68  0].. * Return th
2fa5c 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
2fa5d 73 20 72 65 61 64 2c 20 6f 72 20 30 20 6f 6e 20  s read, or 0 on 
2fa5e 65 72 72 6f 72 2e 0a 20 2a 20 54 68 65 20 76 61  error.. * The va
2fa5f 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  lue is stored in
2fa60 20 2a 76 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69   *v. */.static i
2fa61 6e 74 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  nt fts3GetVarint
2fa62 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c 20  (const char *p, 
2fa63 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 76 29  sqlite_int64 *v)
2fa64 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
2fa65 65 64 20 63 68 61 72 20 2a 71 20 3d 20 28 63 6f  ed char *q = (co
2fa66 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2fa67 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74 65  r *) p;.  sqlite
2fa68 5f 75 69 6e 74 36 34 20 78 20 3d 20 30 2c 20 79  _uint64 x = 0, y
2fa69 20 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 28   = 1;.  while( (
2fa6a 2a 71 20 26 20 30 78 38 30 29 20 3d 3d 20 30 78  *q & 0x80) == 0x
2fa6b 38 30 20 29 7b 0a 20 20 20 20 78 20 2b 3d 20 79  80 ){.    x += y
2fa6c 20 2a 20 28 2a 71 2b 2b 20 26 20 30 78 37 66 29   * (*q++ & 0x7f)
2fa6d 3b 0a 20 20 20 20 79 20 3c 3c 3d 20 37 3b 0a 20  ;.    y <<= 7;. 
2fa6e 20 20 20 69 66 28 20 71 20 2d 20 28 75 6e 73 69     if( q - (unsi
2fa6f 67 6e 65 64 20 63 68 61 72 20 2a 29 70 20 3e 3d  gned char *)p >=
2fa70 20 56 41 52 49 4e 54 5f 4d 41 58 20 29 7b 20 20   VARINT_MAX ){  
2fa71 2f 2a 20 62 61 64 20 64 61 74 61 20 2a 2f 0a 20  /* bad data */. 
2fa72 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29       assert( 0 )
2fa73 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  ;.      return 0
2fa74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 78 20  ;.    }.  }.  x 
2fa75 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 29 3b 0a 20  += y * (*q++);. 
2fa76 20 2a 76 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e   *v = (sqlite_in
2fa77 74 36 34 29 20 78 3b 0a 20 20 72 65 74 75 72 6e  t64) x;.  return
2fa78 20 28 69 6e 74 29 20 28 71 20 2d 20 28 75 6e 73   (int) (q - (uns
2fa79 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 3b  igned char *)p);
2fa7a 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .}..static int f
2fa7b 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 63  ts3GetVarint32(c
2fa7c 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c 20 69 6e  onst char *p, in
2fa7d 74 20 2a 70 69 29 7b 0a 20 73 71 6c 69 74 65 5f  t *pi){. sqlite_
2fa7e 69 6e 74 36 34 20 69 3b 0a 20 69 6e 74 20 72 65  int64 i;. int re
2fa7f 74 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  t = fts3GetVarin
2fa80 74 28 70 2c 20 26 69 29 3b 0a 20 2a 70 69 20 3d  t(p, &i);. *pi =
2fa81 20 28 69 6e 74 29 20 69 3b 0a 20 61 73 73 65 72   (int) i;. asser
2fa82 74 28 20 2a 70 69 3d 3d 69 20 29 3b 0a 20 72 65  t( *pi==i );. re
2fa83 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 2a  turn ret;.}../**
2fa84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2f 0a 2f 2a 20 44 61 74 61 42 75 66 66 65 72  */./* DataBuffer
2fa89 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c   is used to coll
2fa8a 65 63 74 20 64 61 74 61 20 69 6e 74 6f 20 61 20  ect data into a 
2fa8b 62 75 66 66 65 72 20 69 6e 20 70 69 65 63 65 6d  buffer in piecem
2fa8c 65 61 6c 0a 2a 2a 20 66 61 73 68 69 6f 6e 2e 20  eal.** fashion. 
2fa8d 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74   It implements t
2fa8e 68 65 20 75 73 75 61 6c 20 64 69 73 74 69 6e 63  he usual distinc
2fa8f 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 61 6d 6f  tion between amo
2fa90 75 6e 74 20 6f 66 0a 2a 2a 20 64 61 74 61 20 63  unt of.** data c
2fa91 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20  urrently stored 
2fa92 28 6e 44 61 74 61 29 20 61 6e 64 20 62 75 66 66  (nData) and buff
2fa93 65 72 20 63 61 70 61 63 69 74 79 20 28 6e 43 61  er capacity (nCa
2fa94 70 61 63 69 74 79 29 2e 0a 2a 2a 0a 2a 2a 20 64  pacity)..**.** d
2fa95 61 74 61 42 75 66 66 65 72 49 6e 69 74 20 2d 20  ataBufferInit - 
2fa96 63 72 65 61 74 65 20 61 20 62 75 66 66 65 72 20  create a buffer 
2fa97 77 69 74 68 20 67 69 76 65 6e 20 69 6e 69 74 69  with given initi
2fa98 61 6c 20 63 61 70 61 63 69 74 79 2e 0a 2a 2a 20  al capacity..** 
2fa99 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 20  dataBufferReset 
2fa9a 2d 20 66 6f 72 67 65 74 20 62 75 66 66 65 72 27  - forget buffer'
2fa9b 73 20 64 61 74 61 2c 20 72 65 74 61 69 6e 69 6e  s data, retainin
2fa9c 67 20 63 61 70 61 63 69 74 79 2e 0a 2a 2a 20 64  g capacity..** d
2fa9d 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
2fa9e 20 2d 20 66 72 65 65 20 62 75 66 66 65 72 27 73   - free buffer's
2fa9f 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42 75   data..** dataBu
2faa0 66 66 65 72 53 77 61 70 20 2d 20 73 77 61 70 20  fferSwap - swap 
2faa1 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 77 6f 20  contents of two 
2faa2 62 75 66 66 65 72 73 2e 0a 2a 2a 20 64 61 74 61  buffers..** data
2faa3 42 75 66 66 65 72 45 78 70 61 6e 64 20 2d 20 65  BufferExpand - e
2faa4 78 70 61 6e 64 20 63 61 70 61 63 69 74 79 20 77  xpand capacity w
2faa5 69 74 68 6f 75 74 20 61 64 64 69 6e 67 20 64 61  ithout adding da
2faa6 74 61 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65  ta..** dataBuffe
2faa7 72 41 70 70 65 6e 64 20 2d 20 61 70 70 65 6e 64  rAppend - append
2faa8 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42 75   data..** dataBu
2faa9 66 66 65 72 41 70 70 65 6e 64 32 20 2d 20 61 70  fferAppend2 - ap
2faaa 70 65 6e 64 20 74 77 6f 20 70 69 65 63 65 73 20  pend two pieces 
2faab 6f 66 20 64 61 74 61 20 61 74 20 6f 6e 63 65 2e  of data at once.
2faac 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 52 65  .** dataBufferRe
2faad 70 6c 61 63 65 20 2d 20 72 65 70 6c 61 63 65 20  place - replace 
2faae 62 75 66 66 65 72 27 73 20 64 61 74 61 2e 0a 2a  buffer's data..*
2faaf 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
2fab0 20 44 61 74 61 42 75 66 66 65 72 20 7b 0a 20 20   DataBuffer {.  
2fab1 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20  char *pData;    
2fab2 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
2fab3 20 74 6f 20 6d 61 6c 6c 6f 63 27 65 64 20 62 75   to malloc'ed bu
2fab4 66 66 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ffer. */.  int n
2fab5 43 61 70 61 63 69 74 79 3b 20 20 20 20 20 20 20  Capacity;       
2fab6 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 44 61 74   /* Size of pDat
2fab7 61 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69  a buffer. */.  i
2fab8 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20  nt nData;       
2fab9 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 64       /* End of d
2faba 61 74 61 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20  ata loaded into 
2fabb 70 44 61 74 61 2e 20 2a 2f 0a 7d 20 44 61 74 61  pData. */.} Data
2fabc 42 75 66 66 65 72 3b 0a 0a 73 74 61 74 69 63 20  Buffer;..static 
2fabd 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72 49  void dataBufferI
2fabe 6e 69 74 28 44 61 74 61 42 75 66 66 65 72 20 2a  nit(DataBuffer *
2fabf 70 42 75 66 66 65 72 2c 20 69 6e 74 20 6e 43 61  pBuffer, int nCa
2fac0 70 61 63 69 74 79 29 7b 0a 20 20 61 73 73 65 72  pacity){.  asser
2fac1 74 28 20 6e 43 61 70 61 63 69 74 79 3e 3d 30 20  t( nCapacity>=0 
2fac2 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e 44  );.  pBuffer->nD
2fac3 61 74 61 20 3d 20 30 3b 0a 20 20 70 42 75 66 66  ata = 0;.  pBuff
2fac4 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20 3d 20  er->nCapacity = 
2fac5 6e 43 61 70 61 63 69 74 79 3b 0a 20 20 70 42 75  nCapacity;.  pBu
2fac6 66 66 65 72 2d 3e 70 44 61 74 61 20 3d 20 6e 43  ffer->pData = nC
2fac7 61 70 61 63 69 74 79 3d 3d 30 20 3f 20 4e 55 4c  apacity==0 ? NUL
2fac8 4c 20 3a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  L : sqlite3_mall
2fac9 6f 63 28 6e 43 61 70 61 63 69 74 79 29 3b 0a 7d  oc(nCapacity);.}
2faca 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
2facb 61 42 75 66 66 65 72 52 65 73 65 74 28 44 61 74  aBufferReset(Dat
2facc 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
2facd 29 7b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e 44  ){.  pBuffer->nD
2face 61 74 61 20 3d 20 30 3b 0a 7d 0a 73 74 61 74 69  ata = 0;.}.stati
2facf 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
2fad0 72 44 65 73 74 72 6f 79 28 44 61 74 61 42 75 66  rDestroy(DataBuf
2fad1 66 65 72 20 2a 70 42 75 66 66 65 72 29 7b 0a 20  fer *pBuffer){. 
2fad2 20 69 66 28 20 70 42 75 66 66 65 72 2d 3e 70 44   if( pBuffer->pD
2fad3 61 74 61 21 3d 4e 55 4c 4c 20 29 20 73 71 6c 69  ata!=NULL ) sqli
2fad4 74 65 33 5f 66 72 65 65 28 70 42 75 66 66 65 72  te3_free(pBuffer
2fad5 2d 3e 70 44 61 74 61 29 3b 0a 20 20 53 43 52 41  ->pData);.  SCRA
2fad6 4d 42 4c 45 28 70 42 75 66 66 65 72 29 3b 0a 7d  MBLE(pBuffer);.}
2fad7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
2fad8 61 42 75 66 66 65 72 53 77 61 70 28 44 61 74 61  aBufferSwap(Data
2fad9 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 31  Buffer *pBuffer1
2fada 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 42  , DataBuffer *pB
2fadb 75 66 66 65 72 32 29 7b 0a 20 20 44 61 74 61 42  uffer2){.  DataB
2fadc 75 66 66 65 72 20 74 6d 70 20 3d 20 2a 70 42 75  uffer tmp = *pBu
2fadd 66 66 65 72 31 3b 0a 20 20 2a 70 42 75 66 66 65  ffer1;.  *pBuffe
2fade 72 31 20 3d 20 2a 70 42 75 66 66 65 72 32 3b 0a  r1 = *pBuffer2;.
2fadf 20 20 2a 70 42 75 66 66 65 72 32 20 3d 20 74 6d    *pBuffer2 = tm
2fae0 70 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  p;.}.static void
2fae1 20 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e   dataBufferExpan
2fae2 64 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42  d(DataBuffer *pB
2fae3 75 66 66 65 72 2c 20 69 6e 74 20 6e 41 64 64 43  uffer, int nAddC
2fae4 61 70 61 63 69 74 79 29 7b 0a 20 20 61 73 73 65  apacity){.  asse
2fae5 72 74 28 20 6e 41 64 64 43 61 70 61 63 69 74 79  rt( nAddCapacity
2fae6 3e 30 20 29 3b 0a 20 20 2f 2a 20 54 4f 44 4f 28  >0 );.  /* TODO(
2fae7 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20  shess) Consider 
2fae8 65 78 70 61 6e 64 69 6e 67 20 6d 6f 72 65 20 61  expanding more a
2fae9 67 67 72 65 73 73 69 76 65 6c 79 2e 20 20 4e 6f  ggressively.  No
2faea 74 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a  te that the.  **
2faeb 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 61 6c 6c   underlying mall
2faec 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  oc implementatio
2faed 6e 20 6d 61 79 20 74 61 6b 65 20 63 61 72 65 20  n may take care 
2faee 6f 66 20 73 75 63 68 20 74 68 69 6e 67 73 20 66  of such things f
2faef 6f 72 0a 20 20 2a 2a 20 75 73 20 61 6c 72 65 61  or.  ** us alrea
2faf0 64 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  dy..  */.  if( p
2faf1 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e 41  Buffer->nData+nA
2faf2 64 64 43 61 70 61 63 69 74 79 3e 70 42 75 66 66  ddCapacity>pBuff
2faf3 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20 29 7b  er->nCapacity ){
2faf4 0a 20 20 20 20 70 42 75 66 66 65 72 2d 3e 6e 43  .    pBuffer->nC
2faf5 61 70 61 63 69 74 79 20 3d 20 70 42 75 66 66 65  apacity = pBuffe
2faf6 72 2d 3e 6e 44 61 74 61 2b 6e 41 64 64 43 61 70  r->nData+nAddCap
2faf7 61 63 69 74 79 3b 0a 20 20 20 20 70 42 75 66 66  acity;.    pBuff
2faf8 65 72 2d 3e 70 44 61 74 61 20 3d 20 73 71 6c 69  er->pData = sqli
2faf9 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 42 75 66  te3_realloc(pBuf
2fafa 66 65 72 2d 3e 70 44 61 74 61 2c 20 70 42 75 66  fer->pData, pBuf
2fafb 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 29 3b  fer->nCapacity);
2fafc 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
2fafd 69 64 20 64 61 74 61 42 75 66 66 65 72 41 70 70  id dataBufferApp
2fafe 65 6e 64 28 44 61 74 61 42 75 66 66 65 72 20 2a  end(DataBuffer *
2faff 70 42 75 66 66 65 72 2c 0a 20 20 20 20 20 20 20  pBuffer,.       
2fb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb01 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2fb02 20 2a 70 53 6f 75 72 63 65 2c 20 69 6e 74 20 6e   *pSource, int n
2fb03 53 6f 75 72 63 65 29 7b 0a 20 20 61 73 73 65 72  Source){.  asser
2fb04 74 28 20 6e 53 6f 75 72 63 65 3e 30 20 26 26 20  t( nSource>0 && 
2fb05 70 53 6f 75 72 63 65 21 3d 4e 55 4c 4c 20 29 3b  pSource!=NULL );
2fb06 0a 20 20 64 61 74 61 42 75 66 66 65 72 45 78 70  .  dataBufferExp
2fb07 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e 53 6f  and(pBuffer, nSo
2fb08 75 72 63 65 29 3b 0a 20 20 6d 65 6d 63 70 79 28  urce);.  memcpy(
2fb09 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 2b 70  pBuffer->pData+p
2fb0a 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2c 20 70  Buffer->nData, p
2fb0b 53 6f 75 72 63 65 2c 20 6e 53 6f 75 72 63 65 29  Source, nSource)
2fb0c 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e 44 61  ;.  pBuffer->nDa
2fb0d 74 61 20 2b 3d 20 6e 53 6f 75 72 63 65 3b 0a 7d  ta += nSource;.}
2fb0e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
2fb0f 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 44  aBufferAppend2(D
2fb10 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66  ataBuffer *pBuff
2fb11 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
2fb12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb13 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53    const char *pS
2fb14 6f 75 72 63 65 31 2c 20 69 6e 74 20 6e 53 6f 75  ource1, int nSou
2fb15 72 63 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20  rce1,.          
2fb16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb17 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2fb18 70 53 6f 75 72 63 65 32 2c 20 69 6e 74 20 6e 53  pSource2, int nS
2fb19 6f 75 72 63 65 32 29 7b 0a 20 20 61 73 73 65 72  ource2){.  asser
2fb1a 74 28 20 6e 53 6f 75 72 63 65 31 3e 30 20 26 26  t( nSource1>0 &&
2fb1b 20 70 53 6f 75 72 63 65 31 21 3d 4e 55 4c 4c 20   pSource1!=NULL 
2fb1c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 53 6f  );.  assert( nSo
2fb1d 75 72 63 65 32 3e 30 20 26 26 20 70 53 6f 75 72  urce2>0 && pSour
2fb1e 63 65 32 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 64  ce2!=NULL );.  d
2fb1f 61 74 61 42 75 66 66 65 72 45 78 70 61 6e 64 28  ataBufferExpand(
2fb20 70 42 75 66 66 65 72 2c 20 6e 53 6f 75 72 63 65  pBuffer, nSource
2fb21 31 2b 6e 53 6f 75 72 63 65 32 29 3b 0a 20 20 6d  1+nSource2);.  m
2fb22 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70  emcpy(pBuffer->p
2fb23 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44  Data+pBuffer->nD
2fb24 61 74 61 2c 20 70 53 6f 75 72 63 65 31 2c 20 6e  ata, pSource1, n
2fb25 53 6f 75 72 63 65 31 29 3b 0a 20 20 6d 65 6d 63  Source1);.  memc
2fb26 70 79 28 70 42 75 66 66 65 72 2d 3e 70 44 61 74  py(pBuffer->pDat
2fb27 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61  a+pBuffer->nData
2fb28 2b 6e 53 6f 75 72 63 65 31 2c 20 70 53 6f 75 72  +nSource1, pSour
2fb29 63 65 32 2c 20 6e 53 6f 75 72 63 65 32 29 3b 0a  ce2, nSource2);.
2fb2a 20 20 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61    pBuffer->nData
2fb2b 20 2b 3d 20 6e 53 6f 75 72 63 65 31 2b 6e 53 6f   += nSource1+nSo
2fb2c 75 72 63 65 32 3b 0a 7d 0a 73 74 61 74 69 63 20  urce2;.}.static 
2fb2d 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72 52  void dataBufferR
2fb2e 65 70 6c 61 63 65 28 44 61 74 61 42 75 66 66 65  eplace(DataBuffe
2fb2f 72 20 2a 70 42 75 66 66 65 72 2c 0a 20 20 20 20  r *pBuffer,.    
2fb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb31 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
2fb32 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c 20 69  char *pSource, i
2fb33 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20 20 64  nt nSource){.  d
2fb34 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28 70  ataBufferReset(p
2fb35 42 75 66 66 65 72 29 3b 0a 20 20 64 61 74 61 42  Buffer);.  dataB
2fb36 75 66 66 65 72 41 70 70 65 6e 64 28 70 42 75 66  ufferAppend(pBuf
2fb37 66 65 72 2c 20 70 53 6f 75 72 63 65 2c 20 6e 53  fer, pSource, nS
2fb38 6f 75 72 63 65 29 3b 0a 7d 0a 0a 2f 2a 20 53 74  ource);.}../* St
2fb39 72 69 6e 67 42 75 66 66 65 72 20 69 73 20 61 20  ringBuffer is a 
2fb3a 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20  null-terminated 
2fb3b 76 65 72 73 69 6f 6e 20 6f 66 20 44 61 74 61 42  version of DataB
2fb3c 75 66 66 65 72 2e 20 2a 2f 0a 74 79 70 65 64 65  uffer. */.typede
2fb3d 66 20 73 74 72 75 63 74 20 53 74 72 69 6e 67 42  f struct StringB
2fb3e 75 66 66 65 72 20 7b 0a 20 20 44 61 74 61 42 75  uffer {.  DataBu
2fb3f 66 66 65 72 20 62 3b 20 20 20 20 20 20 20 20 20  ffer b;         
2fb40 20 20 20 2f 2a 20 49 6e 63 6c 75 64 65 73 20 6e     /* Includes n
2fb41 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20  ull terminator. 
2fb42 2a 2f 0a 7d 20 53 74 72 69 6e 67 42 75 66 66 65  */.} StringBuffe
2fb43 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
2fb44 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65 72  initStringBuffer
2fb45 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73  (StringBuffer *s
2fb46 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72  b){.  dataBuffer
2fb47 49 6e 69 74 28 26 73 62 2d 3e 62 2c 20 31 30 30  Init(&sb->b, 100
2fb48 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 52  );.  dataBufferR
2fb49 65 70 6c 61 63 65 28 26 73 62 2d 3e 62 2c 20 22  eplace(&sb->b, "
2fb4a 22 2c 20 31 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ", 1);.}.static 
2fb4b 69 6e 74 20 73 74 72 69 6e 67 42 75 66 66 65 72  int stringBuffer
2fb4c 4c 65 6e 67 74 68 28 53 74 72 69 6e 67 42 75 66  Length(StringBuf
2fb4d 66 65 72 20 2a 73 62 29 7b 0a 20 20 72 65 74 75  fer *sb){.  retu
2fb4e 72 6e 20 73 62 2d 3e 62 2e 6e 44 61 74 61 2d 31  rn sb->b.nData-1
2fb4f 3b 0a 7d 0a 73 74 61 74 69 63 20 63 68 61 72 20  ;.}.static char 
2fb50 2a 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74  *stringBufferDat
2fb51 61 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a  a(StringBuffer *
2fb52 73 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 62  sb){.  return sb
2fb53 2d 3e 62 2e 70 44 61 74 61 3b 0a 7d 0a 73 74 61  ->b.pData;.}.sta
2fb54 74 69 63 20 76 6f 69 64 20 73 74 72 69 6e 67 42  tic void stringB
2fb55 75 66 66 65 72 44 65 73 74 72 6f 79 28 53 74 72  ufferDestroy(Str
2fb56 69 6e 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a  ingBuffer *sb){.
2fb57 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
2fb58 72 6f 79 28 26 73 62 2d 3e 62 29 3b 0a 7d 0a 0a  roy(&sb->b);.}..
2fb59 73 74 61 74 69 63 20 76 6f 69 64 20 6e 61 70 70  static void napp
2fb5a 65 6e 64 28 53 74 72 69 6e 67 42 75 66 66 65 72  end(StringBuffer
2fb5b 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *sb, const char
2fb5c 20 2a 7a 46 72 6f 6d 2c 20 69 6e 74 20 6e 46 72   *zFrom, int nFr
2fb5d 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  om){.  assert( s
2fb5e 62 2d 3e 62 2e 6e 44 61 74 61 3e 30 20 29 3b 0a  b->b.nData>0 );.
2fb5f 20 20 69 66 28 20 6e 46 72 6f 6d 3e 30 20 29 7b    if( nFrom>0 ){
2fb60 0a 20 20 20 20 73 62 2d 3e 62 2e 6e 44 61 74 61  .    sb->b.nData
2fb61 2d 2d 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  --;.    dataBuff
2fb62 65 72 41 70 70 65 6e 64 32 28 26 73 62 2d 3e 62  erAppend2(&sb->b
2fb63 2c 20 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d 2c 20  , zFrom, nFrom, 
2fb64 22 22 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 73 74  "", 1);.  }.}.st
2fb65 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64  atic void append
2fb66 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73  (StringBuffer *s
2fb67 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2fb68 46 72 6f 6d 29 7b 0a 20 20 6e 61 70 70 65 6e 64  From){.  nappend
2fb69 28 73 62 2c 20 7a 46 72 6f 6d 2c 20 73 74 72 6c  (sb, zFrom, strl
2fb6a 65 6e 28 7a 46 72 6f 6d 29 29 3b 0a 7d 0a 0a 2f  en(zFrom));.}../
2fb6b 2a 20 41 70 70 65 6e 64 20 61 20 6c 69 73 74 20  * Append a list 
2fb6c 6f 66 20 73 74 72 69 6e 67 73 20 73 65 70 61 72  of strings separ
2fb6d 61 74 65 64 20 62 79 20 63 6f 6d 6d 61 73 2e 20  ated by commas. 
2fb6e 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
2fb6f 70 70 65 6e 64 4c 69 73 74 28 53 74 72 69 6e 67  ppendList(String
2fb70 42 75 66 66 65 72 20 2a 73 62 2c 20 69 6e 74 20  Buffer *sb, int 
2fb71 6e 53 74 72 69 6e 67 2c 20 63 68 61 72 20 2a 2a  nString, char **
2fb72 61 7a 53 74 72 69 6e 67 29 7b 0a 20 20 69 6e 74  azString){.  int
2fb73 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
2fb74 3c 6e 53 74 72 69 6e 67 3b 20 2b 2b 69 29 7b 0a  <nString; ++i){.
2fb75 20 20 20 20 69 66 28 20 69 3e 30 20 29 20 61 70      if( i>0 ) ap
2fb76 70 65 6e 64 28 73 62 2c 20 22 2c 20 22 29 3b 0a  pend(sb, ", ");.
2fb77 20 20 20 20 61 70 70 65 6e 64 28 73 62 2c 20 61      append(sb, a
2fb78 7a 53 74 72 69 6e 67 5b 69 5d 29 3b 0a 20 20 7d  zString[i]);.  }
2fb79 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 65  .}..static int e
2fb7a 6e 64 73 49 6e 57 68 69 74 65 53 70 61 63 65 28  ndsInWhiteSpace(
2fb7b 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 70 29  StringBuffer *p)
2fb7c 7b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e  {.  return strin
2fb7d 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28 70 29  gBufferLength(p)
2fb7e 3e 30 20 26 26 0a 20 20 20 20 73 61 66 65 5f 69  >0 &&.    safe_i
2fb7f 73 73 70 61 63 65 28 73 74 72 69 6e 67 42 75 66  sspace(stringBuf
2fb80 66 65 72 44 61 74 61 28 70 29 5b 73 74 72 69 6e  ferData(p)[strin
2fb81 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28 70 29  gBufferLength(p)
2fb82 2d 31 5d 29 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 74  -1]);.}../* If t
2fb83 68 65 20 53 74 72 69 6e 67 42 75 66 66 65 72 20  he StringBuffer 
2fb84 65 6e 64 73 20 69 6e 20 73 6f 6d 65 74 68 69 6e  ends in somethin
2fb85 67 20 6f 74 68 65 72 20 74 68 61 6e 20 77 68 69  g other than whi
2fb86 74 65 20 73 70 61 63 65 2c 20 61 64 64 20 61 0a  te space, add a.
2fb87 2a 2a 20 73 69 6e 67 6c 65 20 73 70 61 63 65 20  ** single space 
2fb88 63 68 61 72 61 63 74 65 72 20 74 6f 20 74 68 65  character to the
2fb89 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   end..*/.static 
2fb8a 76 6f 69 64 20 61 70 70 65 6e 64 57 68 69 74 65  void appendWhite
2fb8b 53 70 61 63 65 28 53 74 72 69 6e 67 42 75 66 66  Space(StringBuff
2fb8c 65 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 73 74  er *p){.  if( st
2fb8d 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68  ringBufferLength
2fb8e 28 70 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  (p)==0 ) return;
2fb8f 0a 20 20 69 66 28 20 21 65 6e 64 73 49 6e 57 68  .  if( !endsInWh
2fb90 69 74 65 53 70 61 63 65 28 70 29 20 29 20 61 70  iteSpace(p) ) ap
2fb91 70 65 6e 64 28 70 2c 20 22 20 22 29 3b 0a 7d 0a  pend(p, " ");.}.
2fb92 0a 2f 2a 20 52 65 6d 6f 76 65 20 77 68 69 74 65  ./* Remove white
2fb93 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20   space from the 
2fb94 65 6e 64 20 6f 66 20 74 68 65 20 53 74 72 69 6e  end of the Strin
2fb95 67 42 75 66 66 65 72 20 2a 2f 0a 73 74 61 74 69  gBuffer */.stati
2fb96 63 20 76 6f 69 64 20 74 72 69 6d 57 68 69 74 65  c void trimWhite
2fb97 53 70 61 63 65 28 53 74 72 69 6e 67 42 75 66 66  Space(StringBuff
2fb98 65 72 20 2a 70 29 7b 0a 20 20 77 68 69 6c 65 28  er *p){.  while(
2fb99 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70 61 63   endsInWhiteSpac
2fb9a 65 28 70 29 20 29 7b 0a 20 20 20 20 70 2d 3e 62  e(p) ){.    p->b
2fb9b 2e 70 44 61 74 61 5b 2d 2d 70 2d 3e 62 2e 6e 44  .pData[--p->b.nD
2fb9c 61 74 61 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20  ata-1] = '\0';. 
2fb9d 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
2fb9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fba1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44  **********/./* D
2fba2 4c 52 65 61 64 65 72 20 69 73 20 75 73 65 64 20  LReader is used 
2fba3 74 6f 20 72 65 61 64 20 64 6f 63 75 6d 65 6e 74  to read document
2fba4 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61   elements from a
2fba5 20 64 6f 63 6c 69 73 74 2e 20 20 54 68 65 0a 2a   doclist.  The.*
2fba6 2a 20 63 75 72 72 65 6e 74 20 64 6f 63 69 64 20  * current docid 
2fba7 69 73 20 63 61 63 68 65 64 2c 20 73 6f 20 64 6c  is cached, so dl
2fba8 72 44 6f 63 69 64 28 29 20 69 73 20 66 61 73 74  rDocid() is fast
2fba9 2e 20 20 44 4c 52 65 61 64 65 72 20 64 6f 65 73  .  DLReader does
2fbaa 20 6e 6f 74 0a 2a 2a 20 6f 77 6e 20 74 68 65 20   not.** own the 
2fbab 64 6f 63 6c 69 73 74 20 62 75 66 66 65 72 2e 0a  doclist buffer..
2fbac 2a 2a 0a 2a 2a 20 64 6c 72 41 74 45 6e 64 20 2d  **.** dlrAtEnd -
2fbad 20 74 72 75 65 20 69 66 20 74 68 65 72 65 27 73   true if there's
2fbae 20 6e 6f 20 6d 6f 72 65 20 64 61 74 61 20 74 6f   no more data to
2fbaf 20 72 65 61 64 2e 0a 2a 2a 20 64 6c 72 44 6f 63   read..** dlrDoc
2fbb0 69 64 20 2d 20 64 6f 63 69 64 20 6f 66 20 63 75  id - docid of cu
2fbb1 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74 2e 0a  rrent document..
2fbb2 2a 2a 20 64 6c 72 44 6f 63 44 61 74 61 20 2d 20  ** dlrDocData - 
2fbb3 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66 6f 72  doclist data for
2fbb4 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e   current documen
2fbb5 74 20 28 69 6e 63 6c 75 64 69 6e 67 20 64 6f 63  t (including doc
2fbb6 69 64 29 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44 61  id)..** dlrDocDa
2fbb7 74 61 42 79 74 65 73 20 2d 20 6c 65 6e 67 74 68  taBytes - length
2fbb8 20 6f 66 20 73 61 6d 65 2e 0a 2a 2a 20 64 6c 72   of same..** dlr
2fbb9 41 6c 6c 44 61 74 61 42 79 74 65 73 20 2d 20 6c  AllDataBytes - l
2fbba 65 6e 67 74 68 20 6f 66 20 61 6c 6c 20 72 65 6d  ength of all rem
2fbbb 61 69 6e 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 20  aining data..** 
2fbbc 64 6c 72 50 6f 73 44 61 74 61 20 2d 20 70 6f 73  dlrPosData - pos
2fbbd 69 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 63  ition data for c
2fbbe 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74 2e  urrent document.
2fbbf 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61 4c 65  .** dlrPosDataLe
2fbc0 6e 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 70 6f  n - length of po
2fbc1 73 20 64 61 74 61 20 66 6f 72 20 63 75 72 72 65  s data for curre
2fbc2 6e 74 20 64 6f 63 75 6d 65 6e 74 20 28 69 6e 63  nt document (inc
2fbc3 6c 20 50 4f 53 5f 45 4e 44 29 2e 0a 2a 2a 20 64  l POS_END)..** d
2fbc4 6c 72 53 74 65 70 20 2d 20 73 74 65 70 20 74 6f  lrStep - step to
2fbc5 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e   current documen
2fbc6 74 2e 0a 2a 2a 20 64 6c 72 49 6e 69 74 20 2d 20  t..** dlrInit - 
2fbc7 69 6e 69 74 69 61 6c 20 66 6f 72 20 64 6f 63 6c  initial for docl
2fbc8 69 73 74 20 6f 66 20 67 69 76 65 6e 20 74 79 70  ist of given typ
2fbc9 65 20 61 67 61 69 6e 73 74 20 67 69 76 65 6e 20  e against given 
2fbca 64 61 74 61 2e 0a 2a 2a 20 64 6c 72 44 65 73 74  data..** dlrDest
2fbcb 72 6f 79 20 2d 20 63 6c 65 61 6e 20 75 70 2e 0a  roy - clean up..
2fbcc 2a 2a 0a 2a 2a 20 45 78 70 65 63 74 65 64 20 75  **.** Expected u
2fbcd 73 61 67 65 20 69 73 20 73 6f 6d 65 74 68 69 6e  sage is somethin
2fbce 67 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  g like:.**.**   
2fbcf 44 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 3b  DLReader reader;
2fbd0 0a 2a 2a 20 20 20 64 6c 72 49 6e 69 74 28 26 72  .**   dlrInit(&r
2fbd1 65 61 64 65 72 2c 20 70 44 61 74 61 2c 20 6e 44  eader, pData, nD
2fbd2 61 74 61 29 3b 0a 2a 2a 20 20 20 77 68 69 6c 65  ata);.**   while
2fbd3 28 20 21 64 6c 72 41 74 45 6e 64 28 26 72 65 61  ( !dlrAtEnd(&rea
2fbd4 64 65 72 29 20 29 7b 0a 2a 2a 20 20 20 20 20 2f  der) ){.**     /
2fbd5 2f 20 63 61 6c 6c 73 20 74 6f 20 64 6c 72 44 6f  / calls to dlrDo
2fbd6 63 69 64 28 29 20 61 6e 64 20 6b 69 6e 2e 0a 2a  cid() and kin..*
2fbd7 2a 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72  *     dlrStep(&r
2fbd8 65 61 64 65 72 29 3b 0a 2a 2a 20 20 20 7d 0a 2a  eader);.**   }.*
2fbd9 2a 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  *   dlrDestroy(&
2fbda 72 65 61 64 65 72 29 3b 0a 2a 2f 0a 74 79 70 65  reader);.*/.type
2fbdb 64 65 66 20 73 74 72 75 63 74 20 44 4c 52 65 61  def struct DLRea
2fbdc 64 65 72 20 7b 0a 20 20 44 6f 63 4c 69 73 74 54  der {.  DocListT
2fbdd 79 70 65 20 69 54 79 70 65 3b 0a 20 20 63 6f 6e  ype iType;.  con
2fbde 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 3b 0a  st char *pData;.
2fbdf 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20 20    int nData;..  
2fbe0 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
2fbe1 63 69 64 3b 0a 20 20 69 6e 74 20 6e 45 6c 65 6d  cid;.  int nElem
2fbe2 65 6e 74 3b 0a 7d 20 44 4c 52 65 61 64 65 72 3b  ent;.} DLReader;
2fbe3 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72  ..static int dlr
2fbe4 41 74 45 6e 64 28 44 4c 52 65 61 64 65 72 20 2a  AtEnd(DLReader *
2fbe5 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
2fbe6 72 74 28 20 70 52 65 61 64 65 72 2d 3e 6e 44 61  rt( pReader->nDa
2fbe7 74 61 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  ta>=0 );.  retur
2fbe8 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61  n pReader->nData
2fbe9 3d 3d 30 3b 0a 7d 0a 73 74 61 74 69 63 20 73 71  ==0;.}.static sq
2fbea 6c 69 74 65 5f 69 6e 74 36 34 20 64 6c 72 44 6f  lite_int64 dlrDo
2fbeb 63 69 64 28 44 4c 52 65 61 64 65 72 20 2a 70 52  cid(DLReader *pR
2fbec 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
2fbed 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !dlrAtEnd(pRea
2fbee 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
2fbef 20 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64   pReader->iDocid
2fbf0 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.}.static const
2fbf1 20 63 68 61 72 20 2a 64 6c 72 44 6f 63 44 61 74   char *dlrDocDat
2fbf2 61 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61  a(DLReader *pRea
2fbf3 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
2fbf4 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !dlrAtEnd(pReade
2fbf5 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
2fbf6 52 65 61 64 65 72 2d 3e 70 44 61 74 61 3b 0a 7d  Reader->pData;.}
2fbf7 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72 44  .static int dlrD
2fbf8 6f 63 44 61 74 61 42 79 74 65 73 28 44 4c 52 65  ocDataBytes(DLRe
2fbf9 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
2fbfa 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41 74    assert( !dlrAt
2fbfb 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
2fbfc 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
2fbfd 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a 7d 0a 73 74  ->nElement;.}.st
2fbfe 61 74 69 63 20 69 6e 74 20 64 6c 72 41 6c 6c 44  atic int dlrAllD
2fbff 61 74 61 42 79 74 65 73 28 44 4c 52 65 61 64 65  ataBytes(DLReade
2fc00 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61  r *pReader){.  a
2fc01 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64  ssert( !dlrAtEnd
2fc02 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72  (pReader) );.  r
2fc03 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e  eturn pReader->n
2fc04 44 61 74 61 3b 0a 7d 0a 2f 2a 20 54 4f 44 4f 28  Data;.}./* TODO(
2fc05 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20  shess) Consider 
2fc06 61 64 64 69 6e 67 20 61 20 66 69 65 6c 64 20 74  adding a field t
2fc07 6f 20 74 72 61 63 6b 20 69 44 6f 63 69 64 20 76  o track iDocid v
2fc08 61 72 69 6e 74 20 6c 65 6e 67 74 68 0a 2a 2a 20  arint length.** 
2fc09 74 6f 20 6d 61 6b 65 20 74 68 65 73 65 20 74 77  to make these tw
2fc0a 6f 20 66 75 6e 63 74 69 6f 6e 73 20 66 61 73 74  o functions fast
2fc0b 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20  er.  This might 
2fc0c 6d 61 74 74 65 72 20 28 61 20 74 69 6e 79 20 62  matter (a tiny b
2fc0d 69 74 29 0a 2a 2a 20 66 6f 72 20 71 75 65 72 69  it).** for queri
2fc0e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  es..*/.static co
2fc0f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72 50 6f 73  nst char *dlrPos
2fc10 44 61 74 61 28 44 4c 52 65 61 64 65 72 20 2a 70  Data(DLReader *p
2fc11 52 65 61 64 65 72 29 7b 0a 20 20 73 71 6c 69 74  Reader){.  sqlit
2fc12 65 5f 69 6e 74 36 34 20 69 44 75 6d 6d 79 3b 0a  e_int64 iDummy;.
2fc13 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33 47 65    int n = fts3Ge
2fc14 74 56 61 72 69 6e 74 28 70 52 65 61 64 65 72 2d  tVarint(pReader-
2fc15 3e 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29  >pData, &iDummy)
2fc16 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72  ;.  assert( !dlr
2fc17 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29  AtEnd(pReader) )
2fc18 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64  ;.  return pRead
2fc19 65 72 2d 3e 70 44 61 74 61 2b 6e 3b 0a 7d 0a 73  er->pData+n;.}.s
2fc1a 74 61 74 69 63 20 69 6e 74 20 64 6c 72 50 6f 73  tatic int dlrPos
2fc1b 44 61 74 61 4c 65 6e 28 44 4c 52 65 61 64 65 72  DataLen(DLReader
2fc1c 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 73 71   *pReader){.  sq
2fc1d 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75 6d 6d  lite_int64 iDumm
2fc1e 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74 73  y;.  int n = fts
2fc1f 33 47 65 74 56 61 72 69 6e 74 28 70 52 65 61 64  3GetVarint(pRead
2fc20 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 44 75 6d  er->pData, &iDum
2fc21 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  my);.  assert( !
2fc22 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72  dlrAtEnd(pReader
2fc23 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  ) );.  return pR
2fc24 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 2d  eader->nElement-
2fc25 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  n;.}.static void
2fc26 20 64 6c 72 53 74 65 70 28 44 4c 52 65 61 64 65   dlrStep(DLReade
2fc27 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61  r *pReader){.  a
2fc28 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64  ssert( !dlrAtEnd
2fc29 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20 20  (pReader) );..  
2fc2a 2f 2a 20 53 6b 69 70 20 70 61 73 74 20 63 75 72  /* Skip past cur
2fc2b 72 65 6e 74 20 64 6f 63 6c 69 73 74 20 65 6c 65  rent doclist ele
2fc2c 6d 65 6e 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72  ment. */.  asser
2fc2d 74 28 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65  t( pReader->nEle
2fc2e 6d 65 6e 74 3c 3d 70 52 65 61 64 65 72 2d 3e 6e  ment<=pReader->n
2fc2f 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64 65  Data );.  pReade
2fc30 72 2d 3e 70 44 61 74 61 20 2b 3d 20 70 52 65 61  r->pData += pRea
2fc31 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a 20  der->nElement;. 
2fc32 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
2fc33 2d 3d 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65  -= pReader->nEle
2fc34 6d 65 6e 74 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  ment;..  /* If t
2fc35 68 65 72 65 20 69 73 20 6d 6f 72 65 20 64 61 74  here is more dat
2fc36 61 2c 20 72 65 61 64 20 74 68 65 20 6e 65 78 74  a, read the next
2fc37 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74   doclist element
2fc38 2e 20 2a 2f 0a 20 20 69 66 28 20 70 52 65 61 64  . */.  if( pRead
2fc39 65 72 2d 3e 6e 44 61 74 61 21 3d 30 20 29 7b 0a  er->nData!=0 ){.
2fc3a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
2fc3b 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a 20 20   iDocidDelta;.  
2fc3c 20 20 69 6e 74 20 69 44 75 6d 6d 79 2c 20 6e 20    int iDummy, n 
2fc3d 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28  = fts3GetVarint(
2fc3e 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
2fc3f 26 69 44 6f 63 69 64 44 65 6c 74 61 29 3b 0a 20  &iDocidDelta);. 
2fc40 20 20 20 70 52 65 61 64 65 72 2d 3e 69 44 6f 63     pReader->iDoc
2fc41 69 64 20 2b 3d 20 69 44 6f 63 69 64 44 65 6c 74  id += iDocidDelt
2fc42 61 3b 0a 20 20 20 20 69 66 28 20 70 52 65 61 64  a;.    if( pRead
2fc43 65 72 2d 3e 69 54 79 70 65 3e 3d 44 4c 5f 50 4f  er->iType>=DL_PO
2fc44 53 49 54 49 4f 4e 53 20 29 7b 0a 20 20 20 20 20  SITIONS ){.     
2fc45 20 61 73 73 65 72 74 28 20 6e 3c 70 52 65 61 64   assert( n<pRead
2fc46 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20  er->nData );.   
2fc47 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20     while( 1 ){. 
2fc48 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73 33         n += fts3
2fc49 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
2fc4a 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69  der->pData+n, &i
2fc4b 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20  Dummy);.        
2fc4c 61 73 73 65 72 74 28 20 6e 3c 3d 70 52 65 61 64  assert( n<=pRead
2fc4d 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20  er->nData );.   
2fc4e 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79 3d       if( iDummy=
2fc4f 3d 50 4f 53 5f 45 4e 44 20 29 20 62 72 65 61 6b  =POS_END ) break
2fc50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 44  ;.        if( iD
2fc51 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f 4c 55 4d 4e  ummy==POS_COLUMN
2fc52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20   ){.          n 
2fc53 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
2fc54 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74  32(pReader->pDat
2fc55 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  a+n, &iDummy);. 
2fc56 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2fc57 20 6e 3c 70 52 65 61 64 65 72 2d 3e 6e 44 61 74   n<pReader->nDat
2fc58 61 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  a );.        }el
2fc59 73 65 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e  se if( pReader->
2fc5a 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49  iType==DL_POSITI
2fc5b 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20  ONS_OFFSETS ){. 
2fc5c 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74           n += ft
2fc5d 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
2fc5e 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20  eader->pData+n, 
2fc5f 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  &iDummy);.      
2fc60 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
2fc61 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
2fc62 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d  ->pData+n, &iDum
2fc63 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  my);.          a
2fc64 73 73 65 72 74 28 20 6e 3c 70 52 65 61 64 65 72  ssert( n<pReader
2fc65 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20  ->nData );.     
2fc66 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2fc67 20 7d 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e   }.    pReader->
2fc68 6e 45 6c 65 6d 65 6e 74 20 3d 20 6e 3b 0a 20 20  nElement = n;.  
2fc69 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
2fc6a 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3c 3d 70 52 65  r->nElement<=pRe
2fc6b 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20  ader->nData );. 
2fc6c 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
2fc6d 20 64 6c 72 49 6e 69 74 28 44 4c 52 65 61 64 65   dlrInit(DLReade
2fc6e 72 20 2a 70 52 65 61 64 65 72 2c 20 44 6f 63 4c  r *pReader, DocL
2fc6f 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 0a 20  istType iType,. 
2fc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc71 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
2fc72 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 29  Data, int nData)
2fc73 7b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74  {.  assert( pDat
2fc74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e 44 61 74 61  a!=NULL && nData
2fc75 21 3d 30 20 29 3b 0a 20 20 70 52 65 61 64 65 72  !=0 );.  pReader
2fc76 2d 3e 69 54 79 70 65 20 3d 20 69 54 79 70 65 3b  ->iType = iType;
2fc77 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  .  pReader->pDat
2fc78 61 20 3d 20 70 44 61 74 61 3b 0a 20 20 70 52 65  a = pData;.  pRe
2fc79 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44  ader->nData = nD
2fc7a 61 74 61 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  ata;.  pReader->
2fc7b 6e 45 6c 65 6d 65 6e 74 20 3d 20 30 3b 0a 20 20  nElement = 0;.  
2fc7c 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64 20  pReader->iDocid 
2fc7d 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20  = 0;..  /* Load 
2fc7e 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e  the first elemen
2fc7f 74 27 73 20 64 61 74 61 2e 20 20 54 68 65 72 65  t's data.  There
2fc80 20 6d 75 73 74 20 62 65 20 61 20 66 69 72 73 74   must be a first
2fc81 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 64   element. */.  d
2fc82 6c 72 53 74 65 70 28 70 52 65 61 64 65 72 29 3b  lrStep(pReader);
2fc83 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
2fc84 6c 72 44 65 73 74 72 6f 79 28 44 4c 52 65 61 64  lrDestroy(DLRead
2fc85 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2fc86 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72  SCRAMBLE(pReader
2fc87 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44  );.}..#ifndef ND
2fc88 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79 20 74  EBUG./* Verify t
2fc89 68 61 74 20 74 68 65 20 64 6f 63 6c 69 73 74 20  hat the doclist 
2fc8a 63 61 6e 20 62 65 20 76 61 6c 69 64 6c 79 20 64  can be validly d
2fc8b 65 63 6f 64 65 64 2e 20 20 41 6c 73 6f 20 72 65  ecoded.  Also re
2fc8c 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6c 61 73  turns the.** las
2fc8d 74 20 64 6f 63 69 64 20 66 6f 75 6e 64 20 62 65  t docid found be
2fc8e 63 61 75 73 65 20 69 74 20 69 73 20 63 6f 6e 76  cause it is conv
2fc8f 65 6e 69 65 6e 74 20 69 6e 20 6f 74 68 65 72 20  enient in other 
2fc90 61 73 73 65 72 74 69 6f 6e 73 20 66 6f 72 0a 2a  assertions for.*
2fc91 2a 20 44 4c 57 72 69 74 65 72 2e 0a 2a 2f 0a 73  * DLWriter..*/.s
2fc92 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69  tatic void docLi
2fc93 73 74 56 61 6c 69 64 61 74 65 28 44 6f 63 4c 69  stValidate(DocLi
2fc94 73 74 54 79 70 65 20 69 54 79 70 65 2c 20 63 6f  stType iType, co
2fc95 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
2fc96 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20   int nData,.    
2fc97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc98 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
2fc99 6e 74 36 34 20 2a 70 4c 61 73 74 44 6f 63 69 64  nt64 *pLastDocid
2fc9a 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  ){.  sqlite_int6
2fc9b 34 20 69 50 72 65 76 44 6f 63 69 64 20 3d 20 30  4 iPrevDocid = 0
2fc9c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74  ;.  assert( nDat
2fc9d 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  a>0 );.  assert(
2fc9e 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 61   pData!=0 );.  a
2fc9f 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e 44 61  ssert( pData+nDa
2fca0 74 61 3e 70 44 61 74 61 20 29 3b 0a 20 20 77 68  ta>pData );.  wh
2fca1 69 6c 65 28 20 6e 44 61 74 61 21 3d 30 20 29 7b  ile( nData!=0 ){
2fca2 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
2fca3 34 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a 20  4 iDocidDelta;. 
2fca4 20 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33 47     int n = fts3G
2fca5 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c 20  etVarint(pData, 
2fca6 26 69 44 6f 63 69 64 44 65 6c 74 61 29 3b 0a 20  &iDocidDelta);. 
2fca7 20 20 20 69 50 72 65 76 44 6f 63 69 64 20 2b 3d     iPrevDocid +=
2fca8 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a 20 20   iDocidDelta;.  
2fca9 20 20 69 66 28 20 69 54 79 70 65 3e 44 4c 5f 44    if( iType>DL_D
2fcaa 4f 43 49 44 53 20 29 7b 0a 20 20 20 20 20 20 69  OCIDS ){.      i
2fcab 6e 74 20 69 44 75 6d 6d 79 3b 0a 20 20 20 20 20  nt iDummy;.     
2fcac 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20   while( 1 ){.   
2fcad 20 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65       n += fts3Ge
2fcae 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2b  tVarint32(pData+
2fcaf 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20  n, &iDummy);.   
2fcb0 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79 3d       if( iDummy=
2fcb1 3d 50 4f 53 5f 45 4e 44 20 29 20 62 72 65 61 6b  =POS_END ) break
2fcb2 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 44  ;.        if( iD
2fcb3 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f 4c 55 4d 4e  ummy==POS_COLUMN
2fcb4 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20   ){.          n 
2fcb5 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
2fcb6 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75  32(pData+n, &iDu
2fcb7 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  mmy);.        }e
2fcb8 6c 73 65 20 69 66 28 20 69 54 79 70 65 3e 44 4c  lse if( iType>DL
2fcb9 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20 20  _POSITIONS ){.  
2fcba 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73          n += fts
2fcbb 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61  3GetVarint32(pDa
2fcbc 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  ta+n, &iDummy);.
2fcbd 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66            n += f
2fcbe 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
2fcbf 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29  Data+n, &iDummy)
2fcc0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2fcc1 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 6e      assert( n<=n
2fcc2 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 7d 0a  Data );.      }.
2fcc3 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
2fcc4 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20  ( n<=nData );.  
2fcc5 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20    pData += n;.  
2fcc6 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 20 20    nData -= n;.  
2fcc7 7d 0a 20 20 69 66 28 20 70 4c 61 73 74 44 6f 63  }.  if( pLastDoc
2fcc8 69 64 20 29 20 2a 70 4c 61 73 74 44 6f 63 69 64  id ) *pLastDocid
2fcc9 20 3d 20 69 50 72 65 76 44 6f 63 69 64 3b 0a 7d   = iPrevDocid;.}
2fcca 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f  .#define ASSERT_
2fccb 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 69 2c  VALID_DOCLIST(i,
2fccc 20 70 2c 20 6e 2c 20 6f 29 20 64 6f 63 4c 69 73   p, n, o) docLis
2fccd 74 56 61 6c 69 64 61 74 65 28 69 2c 20 70 2c 20  tValidate(i, p, 
2fcce 6e 2c 20 6f 29 0a 23 65 6c 73 65 0a 23 64 65 66  n, o).#else.#def
2fccf 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44  ine ASSERT_VALID
2fcd0 5f 44 4f 43 4c 49 53 54 28 69 2c 20 70 2c 20 6e  _DOCLIST(i, p, n
2fcd1 2c 20 6f 29 20 61 73 73 65 72 74 28 20 31 20 29  , o) assert( 1 )
2fcd2 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
2fcd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fcd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fcd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fcd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2fcd7 2a 20 44 4c 57 72 69 74 65 72 20 69 73 20 75 73  * DLWriter is us
2fcd8 65 64 20 74 6f 20 77 72 69 74 65 20 64 6f 63 6c  ed to write docl
2fcd9 69 73 74 20 64 61 74 61 20 74 6f 20 61 20 44 61  ist data to a Da
2fcda 74 61 42 75 66 66 65 72 2e 20 20 44 4c 57 72 69  taBuffer.  DLWri
2fcdb 74 65 72 0a 2a 2a 20 61 6c 77 61 79 73 20 61 70  ter.** always ap
2fcdc 70 65 6e 64 73 20 74 6f 20 74 68 65 20 62 75 66  pends to the buf
2fcdd 66 65 72 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74  fer and does not
2fcde 20 6f 77 6e 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 64   own it..**.** d
2fcdf 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 69 61 6c  lwInit - initial
2fce0 69 7a 65 20 74 6f 20 77 72 69 74 65 20 61 20 67  ize to write a g
2fce1 69 76 65 6e 20 74 79 70 65 20 64 6f 63 6c 69 73  iven type doclis
2fce2 74 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a  tto a buffer..**
2fce3 20 64 6c 77 44 65 73 74 72 6f 79 20 2d 20 63 6c   dlwDestroy - cl
2fce4 65 61 72 20 74 68 65 20 77 72 69 74 65 72 27 73  ear the writer's
2fce5 20 6d 65 6d 6f 72 79 2e 20 20 44 6f 65 73 20 6e   memory.  Does n
2fce6 6f 74 20 66 72 65 65 20 62 75 66 66 65 72 2e 0a  ot free buffer..
2fce7 2a 2a 20 64 6c 77 41 70 70 65 6e 64 20 2d 20 61  ** dlwAppend - a
2fce8 70 70 65 6e 64 20 72 61 77 20 64 6f 63 6c 69 73  ppend raw doclis
2fce9 74 20 64 61 74 61 20 74 6f 20 62 75 66 66 65 72  t data to buffer
2fcea 2e 0a 2a 2a 20 64 6c 77 43 6f 70 79 20 2d 20 63  ..** dlwCopy - c
2fceb 6f 70 79 20 6e 65 78 74 20 64 6f 63 6c 69 73 74  opy next doclist
2fcec 20 66 72 6f 6d 20 72 65 61 64 65 72 20 74 6f 20   from reader to 
2fced 77 72 69 74 65 72 2e 0a 2a 2a 20 64 6c 77 41 64  writer..** dlwAd
2fcee 64 20 2d 20 63 6f 6e 73 74 72 75 63 74 20 64 6f  d - construct do
2fcef 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74 20 61 6e  clist element an
2fcf0 64 20 61 70 70 65 6e 64 20 74 6f 20 62 75 66 66  d append to buff
2fcf1 65 72 2e 0a 2a 2a 20 20 20 20 4f 6e 6c 79 20 61  er..**    Only a
2fcf2 70 70 6c 79 20 64 6c 77 41 64 64 28 29 20 74 6f  pply dlwAdd() to
2fcf3 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69   DL_DOCIDS docli
2fcf4 73 74 73 20 28 65 6c 73 65 20 75 73 65 20 50 4c  sts (else use PL
2fcf5 57 72 69 74 65 72 29 2e 0a 2a 2f 0a 74 79 70 65  Writer)..*/.type
2fcf6 64 65 66 20 73 74 72 75 63 74 20 44 4c 57 72 69  def struct DLWri
2fcf7 74 65 72 20 7b 0a 20 20 44 6f 63 4c 69 73 74 54  ter {.  DocListT
2fcf8 79 70 65 20 69 54 79 70 65 3b 0a 20 20 44 61 74  ype iType;.  Dat
2fcf9 61 42 75 66 66 65 72 20 2a 62 3b 0a 20 20 73 71  aBuffer *b;.  sq
2fcfa 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72 65 76  lite_int64 iPrev
2fcfb 44 6f 63 69 64 3b 0a 23 69 66 6e 64 65 66 20 4e  Docid;.#ifndef N
2fcfc 44 45 42 55 47 0a 20 20 69 6e 74 20 68 61 73 5f  DEBUG.  int has_
2fcfd 69 50 72 65 76 44 6f 63 69 64 3b 0a 23 65 6e 64  iPrevDocid;.#end
2fcfe 69 66 0a 7d 20 44 4c 57 72 69 74 65 72 3b 0a 0a  if.} DLWriter;..
2fcff 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 77 49  static void dlwI
2fd00 6e 69 74 28 44 4c 57 72 69 74 65 72 20 2a 70 57  nit(DLWriter *pW
2fd01 72 69 74 65 72 2c 20 44 6f 63 4c 69 73 74 54 79  riter, DocListTy
2fd02 70 65 20 69 54 79 70 65 2c 20 44 61 74 61 42 75  pe iType, DataBu
2fd03 66 66 65 72 20 2a 62 29 7b 0a 20 20 70 57 72 69  ffer *b){.  pWri
2fd04 74 65 72 2d 3e 62 20 3d 20 62 3b 0a 20 20 70 57  ter->b = b;.  pW
2fd05 72 69 74 65 72 2d 3e 69 54 79 70 65 20 3d 20 69  riter->iType = i
2fd06 54 79 70 65 3b 0a 20 20 70 57 72 69 74 65 72 2d  Type;.  pWriter-
2fd07 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20 30 3b  >iPrevDocid = 0;
2fd08 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2fd09 20 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 69    pWriter->has_i
2fd0a 50 72 65 76 44 6f 63 69 64 20 3d 20 30 3b 0a 23  PrevDocid = 0;.#
2fd0b 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69 63 20 76  endif.}.static v
2fd0c 6f 69 64 20 64 6c 77 44 65 73 74 72 6f 79 28 44  oid dlwDestroy(D
2fd0d 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
2fd0e 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 57  ){.  SCRAMBLE(pW
2fd0f 72 69 74 65 72 29 3b 0a 7d 0a 2f 2a 20 69 46 69  riter);.}./* iFi
2fd10 72 73 74 44 6f 63 69 64 20 69 73 20 74 68 65 20  rstDocid is the 
2fd11 66 69 72 73 74 20 64 6f 63 69 64 20 69 6e 20 74  first docid in t
2fd12 68 65 20 64 6f 63 6c 69 73 74 20 69 6e 20 70 44  he doclist in pD
2fd13 61 74 61 2e 20 20 49 74 20 69 73 0a 2a 2a 20 6e  ata.  It is.** n
2fd14 65 65 64 65 64 20 62 65 63 61 75 73 65 20 70 44  eeded because pD
2fd15 61 74 61 20 6d 61 79 20 70 6f 69 6e 74 20 77 69  ata may point wi
2fd16 74 68 69 6e 20 61 20 6c 61 72 67 65 72 20 64 6f  thin a larger do
2fd17 63 6c 69 73 74 2c 20 69 6e 20 77 68 69 63 68 0a  clist, in which.
2fd18 2a 2a 20 63 61 73 65 20 74 68 65 20 66 69 72 73  ** case the firs
2fd19 74 20 69 74 65 6d 20 77 6f 75 6c 64 20 62 65 20  t item would be 
2fd1a 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 0a 2a  delta-encoded..*
2fd1b 2a 0a 2a 2a 20 69 4c 61 73 74 44 6f 63 69 64 20  *.** iLastDocid 
2fd1c 69 73 20 74 68 65 20 66 69 6e 61 6c 20 64 6f 63  is the final doc
2fd1d 69 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73  id in the doclis
2fd1e 74 20 69 6e 20 70 44 61 74 61 2e 20 20 49 74 20  t in pData.  It 
2fd1f 69 73 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20  is.** needed to 
2fd20 63 72 65 61 74 65 20 74 68 65 20 6e 65 77 20 69  create the new i
2fd21 50 72 65 76 44 6f 63 69 64 20 66 6f 72 20 66 75  PrevDocid for fu
2fd22 74 75 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64  ture delta-encod
2fd23 69 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 64  ing.  The.** cod
2fd24 65 20 63 6f 75 6c 64 20 64 65 63 6f 64 65 20 74  e could decode t
2fd25 68 65 20 70 61 73 73 65 64 20 64 6f 63 6c 69 73  he passed doclis
2fd26 74 20 74 6f 20 72 65 63 72 65 61 74 65 20 69 4c  t to recreate iL
2fd27 61 73 74 44 6f 63 69 64 2c 20 62 75 74 0a 2a 2a  astDocid, but.**
2fd28 20 74 68 65 20 6f 6e 6c 79 20 63 75 72 72 65 6e   the only curren
2fd29 74 20 75 73 65 72 20 28 64 6f 63 4c 69 73 74 4d  t user (docListM
2fd2a 65 72 67 65 29 20 61 6c 72 65 61 64 79 20 68 61  erge) already ha
2fd2b 73 20 64 65 63 6f 64 65 64 20 74 68 69 73 0a 2a  s decoded this.*
2fd2c 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  * information..*
2fd2d 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
2fd2e 20 54 68 69 73 20 68 61 73 20 62 65 63 6f 6d 65   This has become
2fd2f 20 6a 75 73 74 20 61 20 68 65 6c 70 65 72 20 66   just a helper f
2fd30 6f 72 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 2e  or docListMerge.
2fd31 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61 20 72  .** Consider a r
2fd32 65 66 61 63 74 6f 72 20 74 6f 20 6d 61 6b 65 20  efactor to make 
2fd33 74 68 69 73 20 63 6c 65 61 6e 65 72 2e 0a 2a 2f  this cleaner..*/
2fd34 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 77  .static void dlw
2fd35 41 70 70 65 6e 64 28 44 4c 57 72 69 74 65 72 20  Append(DLWriter 
2fd36 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  *pWriter,.      
2fd37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fd38 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
2fd39 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
2fd3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fd3b 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
2fd3c 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20 73 71   iFirstDocid, sq
2fd3d 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 61 73 74  lite_int64 iLast
2fd3e 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  Docid){.  sqlite
2fd3f 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 20 3d 20  _int64 iDocid = 
2fd40 30 3b 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49  0;.  char c[VARI
2fd41 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e  NT_MAX];.  int n
2fd42 46 69 72 73 74 4f 6c 64 2c 20 6e 46 69 72 73 74  FirstOld, nFirst
2fd43 4e 65 77 3b 20 20 20 20 20 2f 2a 20 4f 6c 64 20  New;     /* Old 
2fd44 61 6e 64 20 6e 65 77 20 76 61 72 69 6e 74 20 6c  and new varint l
2fd45 65 6e 20 6f 66 20 66 69 72 73 74 20 64 6f 63 69  en of first doci
2fd46 64 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44  d. */.#ifndef ND
2fd47 45 42 55 47 0a 20 20 73 71 6c 69 74 65 5f 69 6e  EBUG.  sqlite_in
2fd48 74 36 34 20 69 4c 61 73 74 44 6f 63 69 64 44 65  t64 iLastDocidDe
2fd49 6c 74 61 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  lta;.#endif..  /
2fd4a 2a 20 52 65 63 6f 64 65 20 74 68 65 20 69 6e 69  * Recode the ini
2fd4b 74 69 61 6c 20 64 6f 63 69 64 20 61 73 20 64 65  tial docid as de
2fd4c 6c 74 61 20 66 72 6f 6d 20 69 50 72 65 76 44 6f  lta from iPrevDo
2fd4d 63 69 64 2e 20 2a 2f 0a 20 20 6e 46 69 72 73 74  cid. */.  nFirst
2fd4e 4f 6c 64 20 3d 20 66 74 73 33 47 65 74 56 61 72  Old = fts3GetVar
2fd4f 69 6e 74 28 70 44 61 74 61 2c 20 26 69 44 6f 63  int(pData, &iDoc
2fd50 69 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  id);.  assert( n
2fd51 46 69 72 73 74 4f 6c 64 3c 6e 44 61 74 61 20 7c  FirstOld<nData |
2fd52 7c 20 28 6e 46 69 72 73 74 4f 6c 64 3d 3d 6e 44  | (nFirstOld==nD
2fd53 61 74 61 20 26 26 20 70 57 72 69 74 65 72 2d 3e  ata && pWriter->
2fd54 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44 53  iType==DL_DOCIDS
2fd55 29 20 29 3b 0a 20 20 6e 46 69 72 73 74 4e 65 77  ) );.  nFirstNew
2fd56 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
2fd57 28 63 2c 20 69 46 69 72 73 74 44 6f 63 69 64 2d  (c, iFirstDocid-
2fd58 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f  pWriter->iPrevDo
2fd59 63 69 64 29 3b 0a 0a 20 20 2f 2a 20 56 65 72 69  cid);..  /* Veri
2fd5a 66 79 20 74 68 61 74 20 74 68 65 20 69 6e 63 6f  fy that the inco
2fd5b 6d 69 6e 67 20 64 6f 63 6c 69 73 74 20 69 73 20  ming doclist is 
2fd5c 76 61 6c 69 64 20 41 4e 44 20 74 68 61 74 20 69  valid AND that i
2fd5d 74 20 65 6e 64 73 20 77 69 74 68 0a 20 20 2a 2a  t ends with.  **
2fd5e 20 74 68 65 20 65 78 70 65 63 74 65 64 20 64 6f   the expected do
2fd5f 63 69 64 2e 20 20 54 68 69 73 20 69 73 20 65 73  cid.  This is es
2fd60 73 65 6e 74 69 61 6c 20 62 65 63 61 75 73 65 20  sential because 
2fd61 77 65 27 6c 6c 20 74 72 75 73 74 20 74 68 69 73  we'll trust this
2fd62 0a 20 20 2a 2a 20 64 6f 63 69 64 20 69 6e 20 66  .  ** docid in f
2fd63 75 74 75 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f  uture delta-enco
2fd64 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 41 53 53  ding..  */.  ASS
2fd65 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53  ERT_VALID_DOCLIS
2fd66 54 28 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65  T(pWriter->iType
2fd67 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , pData, nData, 
2fd68 26 69 4c 61 73 74 44 6f 63 69 64 44 65 6c 74 61  &iLastDocidDelta
2fd69 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 61  );.  assert( iLa
2fd6a 73 74 44 6f 63 69 64 3d 3d 69 46 69 72 73 74 44  stDocid==iFirstD
2fd6b 6f 63 69 64 2d 69 44 6f 63 69 64 2b 69 4c 61 73  ocid-iDocid+iLas
2fd6c 74 44 6f 63 69 64 44 65 6c 74 61 20 29 3b 0a 0a  tDocidDelta );..
2fd6d 20 20 2f 2a 20 41 70 70 65 6e 64 20 72 65 63 6f    /* Append reco
2fd6e 64 65 64 20 69 6e 69 74 69 61 6c 20 64 6f 63 69  ded initial doci
2fd6f 64 20 61 6e 64 20 65 76 65 72 79 74 68 69 6e 67  d and everything
2fd70 20 65 6c 73 65 2e 20 20 52 65 73 74 20 6f 66 20   else.  Rest of 
2fd71 64 6f 63 69 64 73 0a 20 20 2a 2a 20 73 68 6f 75  docids.  ** shou
2fd72 6c 64 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c  ld have been del
2fd73 74 61 2d 65 6e 63 6f 64 65 64 20 66 72 6f 6d 20  ta-encoded from 
2fd74 70 72 65 76 69 6f 75 73 20 69 6e 69 74 69 61 6c  previous initial
2fd75 20 64 6f 63 69 64 2e 0a 20 20 2a 2f 0a 20 20 69   docid..  */.  i
2fd76 66 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e 44 61  f( nFirstOld<nDa
2fd77 74 61 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75  ta ){.    dataBu
2fd78 66 66 65 72 41 70 70 65 6e 64 32 28 70 57 72 69  fferAppend2(pWri
2fd79 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 46 69 72 73  ter->b, c, nFirs
2fd7a 74 4e 65 77 2c 0a 20 20 20 20 20 20 20 20 20 20  tNew,.          
2fd7b 20 20 20 20 20 20 20 20 20 20 20 20 70 44 61 74              pDat
2fd7c 61 2b 6e 46 69 72 73 74 4f 6c 64 2c 20 6e 44 61  a+nFirstOld, nDa
2fd7d 74 61 2d 6e 46 69 72 73 74 4f 6c 64 29 3b 0a 20  ta-nFirstOld);. 
2fd7e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 61 74 61   }else{.    data
2fd7f 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72  BufferAppend(pWr
2fd80 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 46 69 72  iter->b, c, nFir
2fd81 73 74 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 70 57  stNew);.  }.  pW
2fd82 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
2fd83 64 20 3d 20 69 4c 61 73 74 44 6f 63 69 64 3b 0a  d = iLastDocid;.
2fd84 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c  }.static void dl
2fd85 77 43 6f 70 79 28 44 4c 57 72 69 74 65 72 20 2a  wCopy(DLWriter *
2fd86 70 57 72 69 74 65 72 2c 20 44 4c 52 65 61 64 65  pWriter, DLReade
2fd87 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 64  r *pReader){.  d
2fd88 6c 77 41 70 70 65 6e 64 28 70 57 72 69 74 65 72  lwAppend(pWriter
2fd89 2c 20 64 6c 72 44 6f 63 44 61 74 61 28 70 52 65  , dlrDocData(pRe
2fd8a 61 64 65 72 29 2c 20 64 6c 72 44 6f 63 44 61 74  ader), dlrDocDat
2fd8b 61 42 79 74 65 73 28 70 52 65 61 64 65 72 29 2c  aBytes(pReader),
2fd8c 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 6c 72  .            dlr
2fd8d 44 6f 63 69 64 28 70 52 65 61 64 65 72 29 2c 20  Docid(pReader), 
2fd8e 64 6c 72 44 6f 63 69 64 28 70 52 65 61 64 65 72  dlrDocid(pReader
2fd8f 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  ));.}.static voi
2fd90 64 20 64 6c 77 41 64 64 28 44 4c 57 72 69 74 65  d dlwAdd(DLWrite
2fd91 72 20 2a 70 57 72 69 74 65 72 2c 20 73 71 6c 69  r *pWriter, sqli
2fd92 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29  te_int64 iDocid)
2fd93 7b 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e  {.  char c[VARIN
2fd94 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 20  T_MAX];.  int n 
2fd95 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
2fd96 63 2c 20 69 44 6f 63 69 64 2d 70 57 72 69 74 65  c, iDocid-pWrite
2fd97 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 29 3b 0a  r->iPrevDocid);.
2fd98 0a 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d 75 73  .  /* Docids mus
2fd99 74 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20 20 61  t ascend. */.  a
2fd9a 73 73 65 72 74 28 20 21 70 57 72 69 74 65 72 2d  ssert( !pWriter-
2fd9b 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20  >has_iPrevDocid 
2fd9c 7c 7c 20 69 44 6f 63 69 64 3e 70 57 72 69 74 65  || iDocid>pWrite
2fd9d 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 29 3b  r->iPrevDocid );
2fd9e 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
2fd9f 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f  er->iType==DL_DO
2fda0 43 49 44 53 20 29 3b 0a 0a 20 20 64 61 74 61 42  CIDS );..  dataB
2fda1 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72 69  ufferAppend(pWri
2fda2 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20  ter->b, c, n);. 
2fda3 20 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44   pWriter->iPrevD
2fda4 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 23  ocid = iDocid;.#
2fda5 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
2fda6 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 69 50 72  pWriter->has_iPr
2fda7 65 76 44 6f 63 69 64 20 3d 20 31 3b 0a 23 65 6e  evDocid = 1;.#en
2fda8 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  dif.}../********
2fda9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fdaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fdab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fdac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
2fdad 50 4c 52 65 61 64 65 72 20 69 73 20 75 73 65 64  PLReader is used
2fdae 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 66 72   to read data fr
2fdaf 6f 6d 20 61 20 64 6f 63 75 6d 65 6e 74 27 73 20  om a document's 
2fdb0 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2e 20 20  position list.  
2fdb1 41 73 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72  As.** the caller
2fdb2 20 73 74 65 70 73 20 74 68 72 6f 75 67 68 20 74   steps through t
2fdb3 68 65 20 6c 69 73 74 2c 20 64 61 74 61 20 69 73  he list, data is
2fdb4 20 63 61 63 68 65 64 20 73 6f 20 74 68 61 74 20   cached so that 
2fdb5 76 61 72 69 6e 74 73 0a 2a 2a 20 6f 6e 6c 79 20  varints.** only 
2fdb6 6e 65 65 64 20 74 6f 20 62 65 20 64 65 63 6f 64  need to be decod
2fdb7 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 70  ed once..**.** p
2fdb8 6c 72 49 6e 69 74 2c 20 70 6c 72 44 65 73 74 72  lrInit, plrDestr
2fdb9 6f 79 20 2d 20 63 72 65 61 74 65 2f 64 65 73 74  oy - create/dest
2fdba 72 6f 79 20 61 20 72 65 61 64 65 72 2e 0a 2a 2a  roy a reader..**
2fdbb 20 70 6c 72 43 6f 6c 75 6d 6e 2c 20 70 6c 72 50   plrColumn, plrP
2fdbc 6f 73 69 74 69 6f 6e 2c 20 70 6c 72 53 74 61 72  osition, plrStar
2fdbd 74 4f 66 66 73 65 74 2c 20 70 6c 72 45 6e 64 4f  tOffset, plrEndO
2fdbe 66 66 73 65 74 20 2d 20 61 63 63 65 73 73 6f 72  ffset - accessor
2fdbf 73 0a 2a 2a 20 70 6c 72 41 74 45 6e 64 20 2d 20  s.** plrAtEnd - 
2fdc0 61 74 20 65 6e 64 20 6f 66 20 73 74 72 65 61 6d  at end of stream
2fdc1 2c 20 6f 6e 6c 79 20 63 61 6c 6c 20 70 6c 72 44  , only call plrD
2fdc2 65 73 74 72 6f 79 20 6f 6e 63 65 20 74 72 75 65  estroy once true
2fdc3 2e 0a 2a 2a 20 70 6c 72 53 74 65 70 20 2d 20 73  ..** plrStep - s
2fdc4 74 65 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20  tep to the next 
2fdc5 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 74 79 70 65  element..*/.type
2fdc6 64 65 66 20 73 74 72 75 63 74 20 50 4c 52 65 61  def struct PLRea
2fdc7 64 65 72 20 7b 0a 20 20 2f 2a 20 54 68 65 73 65  der {.  /* These
2fdc8 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 6e 65   refer to the ne
2fdc9 78 74 20 70 6f 73 69 74 69 6f 6e 27 73 20 64 61  xt position's da
2fdca 74 61 2e 20 20 6e 44 61 74 61 20 77 69 6c 6c 20  ta.  nData will 
2fdcb 72 65 61 63 68 20 30 20 77 68 65 6e 0a 20 20 2a  reach 0 when.  *
2fdcc 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 6c 61  * reading the la
2fdcd 73 74 20 70 6f 73 69 74 69 6f 6e 2c 20 73 6f 20  st position, so 
2fdce 70 6c 72 53 74 65 70 28 29 20 73 69 67 6e 61 6c  plrStep() signal
2fdcf 73 20 45 4f 46 20 62 79 20 73 65 74 74 69 6e 67  s EOF by setting
2fdd0 0a 20 20 2a 2a 20 70 44 61 74 61 20 74 6f 20 4e  .  ** pData to N
2fdd1 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 63 6f 6e 73  ULL..  */.  cons
2fdd2 74 20 63 68 61 72 20 2a 70 44 61 74 61 3b 0a 20  t char *pData;. 
2fdd3 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20 20 44   int nData;..  D
2fdd4 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65  ocListType iType
2fdd5 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b  ;.  int iColumn;
2fdd6 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
2fdd7 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 72 65 61 64  last column read
2fdd8 20 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73 69 74   */.  int iPosit
2fdd9 69 6f 6e 3b 20 20 20 20 20 20 20 2f 2a 20 74 68  ion;       /* th
2fdda 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 20  e last position 
2fddb 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 53  read */.  int iS
2fddc 74 61 72 74 4f 66 66 73 65 74 3b 20 20 20 20 2f  tartOffset;    /
2fddd 2a 20 74 68 65 20 6c 61 73 74 20 73 74 61 72 74  * the last start
2fdde 20 6f 66 66 73 65 74 20 72 65 61 64 20 2a 2f 0a   offset read */.
2fddf 20 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65 74    int iEndOffset
2fde0 3b 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6c 61  ;      /* the la
2fde1 73 74 20 65 6e 64 20 6f 66 66 73 65 74 20 72 65  st end offset re
2fde2 61 64 20 2a 2f 0a 7d 20 50 4c 52 65 61 64 65 72  ad */.} PLReader
2fde3 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c  ;..static int pl
2fde4 72 41 74 45 6e 64 28 50 4c 52 65 61 64 65 72 20  rAtEnd(PLReader 
2fde5 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
2fde6 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44 61  urn pReader->pDa
2fde7 74 61 3d 3d 4e 55 4c 4c 3b 0a 7d 0a 73 74 61 74  ta==NULL;.}.stat
2fde8 69 63 20 69 6e 74 20 70 6c 72 43 6f 6c 75 6d 6e  ic int plrColumn
2fde9 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64  (PLReader *pRead
2fdea 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
2fdeb 70 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72  plrAtEnd(pReader
2fdec 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  ) );.  return pR
2fded 65 61 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  eader->iColumn;.
2fdee 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
2fdef 50 6f 73 69 74 69 6f 6e 28 50 4c 52 65 61 64 65  Position(PLReade
2fdf0 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61  r *pReader){.  a
2fdf1 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64  ssert( !plrAtEnd
2fdf2 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72  (pReader) );.  r
2fdf3 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69  eturn pReader->i
2fdf4 50 6f 73 69 74 69 6f 6e 3b 0a 7d 0a 73 74 61 74  Position;.}.stat
2fdf5 69 63 20 69 6e 74 20 70 6c 72 53 74 61 72 74 4f  ic int plrStartO
2fdf6 66 66 73 65 74 28 50 4c 52 65 61 64 65 72 20 2a  ffset(PLReader *
2fdf7 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
2fdf8 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52  rt( !plrAtEnd(pR
2fdf9 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
2fdfa 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61  rn pReader->iSta
2fdfb 72 74 4f 66 66 73 65 74 3b 0a 7d 0a 73 74 61 74  rtOffset;.}.stat
2fdfc 69 63 20 69 6e 74 20 70 6c 72 45 6e 64 4f 66 66  ic int plrEndOff
2fdfd 73 65 74 28 50 4c 52 65 61 64 65 72 20 2a 70 52  set(PLReader *pR
2fdfe 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
2fdff 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !plrAtEnd(pRea
2fe00 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
2fe01 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f 66   pReader->iEndOf
2fe02 66 73 65 74 3b 0a 7d 0a 73 74 61 74 69 63 20 76  fset;.}.static v
2fe03 6f 69 64 20 70 6c 72 53 74 65 70 28 50 4c 52 65  oid plrStep(PLRe
2fe04 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
2fe05 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 0a 20 20 61    int i, n;..  a
2fe06 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64  ssert( !plrAtEnd
2fe07 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20 20  (pReader) );..  
2fe08 69 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44 61  if( pReader->nDa
2fe09 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 70 52 65  ta==0 ){.    pRe
2fe0a 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 4e 55  ader->pData = NU
2fe0b 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  LL;.    return;.
2fe0c 20 20 7d 0a 0a 20 20 6e 20 3d 20 66 74 73 33 47    }..  n = fts3G
2fe0d 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
2fe0e 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 29 3b 0a  er->pData, &i);.
2fe0f 20 20 69 66 28 20 69 3d 3d 50 4f 53 5f 43 4f 4c    if( i==POS_COL
2fe10 55 4d 4e 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20  UMN ){.    n += 
2fe11 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
2fe12 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
2fe13 2c 20 26 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c  , &pReader->iCol
2fe14 75 6d 6e 29 3b 0a 20 20 20 20 70 52 65 61 64 65  umn);.    pReade
2fe15 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20 30  r->iPosition = 0
2fe16 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69  ;.    pReader->i
2fe17 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 30 3b  StartOffset = 0;
2fe18 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65  .    n += fts3Ge
2fe19 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
2fe1a 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b  r->pData+n, &i);
2fe1b 0a 20 20 7d 0a 20 20 2f 2a 20 53 68 6f 75 6c 64  .  }.  /* Should
2fe1c 20 6e 65 76 65 72 20 73 65 65 20 61 64 6a 61 63   never see adjac
2fe1d 65 6e 74 20 63 6f 6c 75 6d 6e 20 63 68 61 6e 67  ent column chang
2fe1e 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  es. */.  assert(
2fe1f 20 69 21 3d 50 4f 53 5f 43 4f 4c 55 4d 4e 20 29   i!=POS_COLUMN )
2fe20 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 50 4f 53 5f  ;..  if( i==POS_
2fe21 45 4e 44 20 29 7b 0a 20 20 20 20 70 52 65 61 64  END ){.    pRead
2fe22 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20  er->nData = 0;. 
2fe23 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74     pReader->pDat
2fe24 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65  a = NULL;.    re
2fe25 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 52 65  turn;.  }..  pRe
2fe26 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20  ader->iPosition 
2fe27 2b 3d 20 69 2d 50 4f 53 5f 42 41 53 45 3b 0a 20  += i-POS_BASE;. 
2fe28 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 69 54   if( pReader->iT
2fe29 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e  ype==DL_POSITION
2fe2a 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20 20  S_OFFSETS ){.   
2fe2b 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72   n += fts3GetVar
2fe2c 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
2fe2d 44 61 74 61 2b 6e 2c 20 26 69 29 3b 0a 20 20 20  Data+n, &i);.   
2fe2e 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72 74   pReader->iStart
2fe2f 4f 66 66 73 65 74 20 2b 3d 20 69 3b 0a 20 20 20  Offset += i;.   
2fe30 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72   n += fts3GetVar
2fe31 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
2fe32 44 61 74 61 2b 6e 2c 20 26 69 29 3b 0a 20 20 20  Data+n, &i);.   
2fe33 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f 66   pReader->iEndOf
2fe34 66 73 65 74 20 3d 20 70 52 65 61 64 65 72 2d 3e  fset = pReader->
2fe35 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69 3b 0a  iStartOffset+i;.
2fe36 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3c    }.  assert( n<
2fe37 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
2fe38 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44  );.  pReader->pD
2fe39 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 70 52 65 61  ata += n;.  pRea
2fe3a 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 3b  der->nData -= n;
2fe3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
2fe3c 70 6c 72 49 6e 69 74 28 50 4c 52 65 61 64 65 72  plrInit(PLReader
2fe3d 20 2a 70 52 65 61 64 65 72 2c 20 44 4c 52 65 61   *pReader, DLRea
2fe3e 64 65 72 20 2a 70 44 4c 52 65 61 64 65 72 29 7b  der *pDLReader){
2fe3f 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  .  pReader->pDat
2fe40 61 20 3d 20 64 6c 72 50 6f 73 44 61 74 61 28 70  a = dlrPosData(p
2fe41 44 4c 52 65 61 64 65 72 29 3b 0a 20 20 70 52 65  DLReader);.  pRe
2fe42 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 64 6c  ader->nData = dl
2fe43 72 50 6f 73 44 61 74 61 4c 65 6e 28 70 44 4c 52  rPosDataLen(pDLR
2fe44 65 61 64 65 72 29 3b 0a 20 20 70 52 65 61 64 65  eader);.  pReade
2fe45 72 2d 3e 69 54 79 70 65 20 3d 20 70 44 4c 52 65  r->iType = pDLRe
2fe46 61 64 65 72 2d 3e 69 54 79 70 65 3b 0a 20 20 70  ader->iType;.  p
2fe47 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20  Reader->iColumn 
2fe48 3d 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  = 0;.  pReader->
2fe49 69 50 6f 73 69 74 69 6f 6e 20 3d 20 30 3b 0a 20  iPosition = 0;. 
2fe4a 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72 74   pReader->iStart
2fe4b 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 52  Offset = 0;.  pR
2fe4c 65 61 64 65 72 2d 3e 69 45 6e 64 4f 66 66 73 65  eader->iEndOffse
2fe4d 74 20 3d 20 30 3b 0a 20 20 70 6c 72 53 74 65 70  t = 0;.  plrStep
2fe4e 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61  (pReader);.}.sta
2fe4f 74 69 63 20 76 6f 69 64 20 70 6c 72 44 65 73 74  tic void plrDest
2fe50 72 6f 79 28 50 4c 52 65 61 64 65 72 20 2a 70 52  roy(PLReader *pR
2fe51 65 61 64 65 72 29 7b 0a 20 20 53 43 52 41 4d 42  eader){.  SCRAMB
2fe52 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a  LE(pReader);.}..
2fe53 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
2fe54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fe55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fe56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fe57 2a 2a 2a 2a 2f 0a 2f 2a 20 50 4c 57 72 69 74 65  ****/./* PLWrite
2fe58 72 20 69 73 20 75 73 65 64 20 69 6e 20 63 6f 6e  r is used in con
2fe59 73 74 72 75 63 74 69 6e 67 20 61 20 64 6f 63 75  structing a docu
2fe5a 6d 65 6e 74 27 73 20 70 6f 73 69 74 69 6f 6e 20  ment's position 
2fe5b 6c 69 73 74 2e 20 20 41 73 20 61 0a 2a 2a 20 63  list.  As a.** c
2fe5c 6f 6e 76 65 6e 69 65 6e 63 65 2c 20 69 66 20 69  onvenience, if i
2fe5d 54 79 70 65 20 69 73 20 44 4c 5f 44 4f 43 49 44  Type is DL_DOCID
2fe5e 53 2c 20 50 4c 57 72 69 74 65 72 20 62 65 63 6f  S, PLWriter beco
2fe5f 6d 65 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20  mes a no-op..** 
2fe60 50 4c 57 72 69 74 65 72 20 77 72 69 74 65 73 20  PLWriter writes 
2fe61 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  to the associate
2fe62 64 20 44 4c 57 72 69 74 65 72 27 73 20 62 75 66  d DLWriter's buf
2fe63 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 6c 77 49 6e  fer..**.** plwIn
2fe64 69 74 20 2d 20 69 6e 69 74 20 66 6f 72 20 77 72  it - init for wr
2fe65 69 74 69 6e 67 20 61 20 64 6f 63 75 6d 65 6e 74  iting a document
2fe66 27 73 20 70 6f 73 6c 69 73 74 2e 0a 2a 2a 20 70  's poslist..** p
2fe67 6c 77 44 65 73 74 72 6f 79 20 2d 20 63 6c 65 61  lwDestroy - clea
2fe68 72 20 61 20 77 72 69 74 65 72 2e 0a 2a 2a 20 70  r a writer..** p
2fe69 6c 77 41 64 64 20 2d 20 61 70 70 65 6e 64 20 70  lwAdd - append p
2fe6a 6f 73 69 74 69 6f 6e 20 61 6e 64 20 6f 66 66 73  osition and offs
2fe6b 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  et information..
2fe6c 2a 2a 20 70 6c 77 43 6f 70 79 20 2d 20 63 6f 70  ** plwCopy - cop
2fe6d 79 20 6e 65 78 74 20 70 6f 73 69 74 69 6f 6e 27  y next position'
2fe6e 73 20 64 61 74 61 20 66 72 6f 6d 20 72 65 61 64  s data from read
2fe6f 65 72 20 74 6f 20 77 72 69 74 65 72 2e 0a 2a 2a  er to writer..**
2fe70 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 20 2d 20   plwTerminate - 
2fe71 61 64 64 20 61 6e 79 20 6e 65 63 65 73 73 61 72  add any necessar
2fe72 79 20 64 6f 63 6c 69 73 74 20 74 65 72 6d 69 6e  y doclist termin
2fe73 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c  ator..**.** Call
2fe74 69 6e 67 20 70 6c 77 41 64 64 28 29 20 61 66 74  ing plwAdd() aft
2fe75 65 72 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28  er plwTerminate(
2fe76 29 20 6d 61 79 20 72 65 73 75 6c 74 20 69 6e 20  ) may result in 
2fe77 61 20 63 6f 72 72 75 70 74 0a 2a 2a 20 64 6f 63  a corrupt.** doc
2fe78 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  list..*/./* TODO
2fe79 28 73 68 65 73 73 29 20 55 6e 74 69 6c 20 77 65  (shess) Until we
2fe7a 27 76 65 20 77 72 69 74 74 65 6e 20 74 68 65 20  've written the 
2fe7b 73 65 63 6f 6e 64 20 69 74 65 6d 2c 20 77 65 20  second item, we 
2fe7c 63 61 6e 20 63 61 63 68 65 20 74 68 65 0a 2a 2a  can cache the.**
2fe7d 20 66 69 72 73 74 20 69 74 65 6d 27 73 20 69 6e   first item's in
2fe7e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 54 68 65 6e  formation.  Then
2fe7f 20 77 65 27 64 20 68 61 76 65 20 74 68 72 65 65   we'd have three
2fe80 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 2d   states:.**.** -
2fe81 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 69 74   initialized wit
2fe82 68 20 64 6f 63 69 64 2c 20 6e 6f 20 70 6f 73 69  h docid, no posi
2fe83 74 69 6f 6e 73 2e 0a 2a 2a 20 2d 20 64 6f 63 69  tions..** - doci
2fe84 64 20 61 6e 64 20 6f 6e 65 20 70 6f 73 69 74 69  d and one positi
2fe85 6f 6e 2e 0a 2a 2a 20 2d 20 64 6f 63 69 64 20 61  on..** - docid a
2fe86 6e 64 20 6d 75 6c 74 69 70 6c 65 20 70 6f 73 69  nd multiple posi
2fe87 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c  tions..**.** Onl
2fe88 79 20 74 68 65 20 6c 61 73 74 20 73 74 61 74 65  y the last state
2fe89 20 6e 65 65 64 73 20 74 6f 20 61 63 74 75 61 6c   needs to actual
2fe8a 6c 79 20 77 72 69 74 65 20 74 6f 20 64 6c 77 2d  ly write to dlw-
2fe8b 3e 62 2c 20 77 68 69 63 68 20 77 6f 75 6c 64 0a  >b, which would.
2fe8c 2a 2a 20 62 65 20 61 6e 20 69 6d 70 72 6f 76 65  ** be an improve
2fe8d 6d 65 6e 74 20 69 6e 20 74 68 65 20 44 4c 43 6f  ment in the DLCo
2fe8e 6c 6c 65 63 74 6f 72 20 63 61 73 65 2e 0a 2a 2f  llector case..*/
2fe8f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2fe90 50 4c 57 72 69 74 65 72 20 7b 0a 20 20 44 4c 57  PLWriter {.  DLW
2fe91 72 69 74 65 72 20 2a 64 6c 77 3b 0a 0a 20 20 69  riter *dlw;..  i
2fe92 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 2f  nt iColumn;    /
2fe93 2a 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  * the last colum
2fe94 6e 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69  n written */.  i
2fe95 6e 74 20 69 50 6f 73 3b 20 20 20 20 20 20 20 2f  nt iPos;       /
2fe96 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73 69 74  * the last posit
2fe97 69 6f 6e 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20  ion written */. 
2fe98 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20   int iOffset;   
2fe99 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 73 74 61   /* the last sta
2fe9a 72 74 20 6f 66 66 73 65 74 20 77 72 69 74 74 65  rt offset writte
2fe9b 6e 20 2a 2f 0a 7d 20 50 4c 57 72 69 74 65 72 3b  n */.} PLWriter;
2fe9c 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  ../* TODO(shess)
2fe9d 20 49 6e 20 74 68 65 20 63 61 73 65 20 77 68 65   In the case whe
2fe9e 72 65 20 74 68 65 20 70 61 72 65 6e 74 20 69 73  re the parent is
2fe9f 20 72 65 61 64 69 6e 67 20 74 68 65 73 65 20 76   reading these v
2fea0 61 6c 75 65 73 0a 2a 2a 20 66 72 6f 6d 20 61 20  alues.** from a 
2fea1 50 4c 52 65 61 64 65 72 2c 20 77 65 20 63 6f 75  PLReader, we cou
2fea2 6c 64 20 6f 70 74 69 6d 69 7a 65 20 74 6f 20 61  ld optimize to a
2fea3 20 63 6f 70 79 20 69 66 20 74 68 61 74 20 50 4c   copy if that PL
2fea4 52 65 61 64 65 72 20 68 61 73 0a 2a 2a 20 74 68  Reader has.** th
2fea5 65 20 73 61 6d 65 20 74 79 70 65 20 61 73 20 70  e same type as p
2fea6 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  Writer..*/.stati
2fea7 63 20 76 6f 69 64 20 70 6c 77 41 64 64 28 50 4c  c void plwAdd(PL
2fea8 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
2fea9 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69 6e   int iColumn, in
2feaa 74 20 69 50 6f 73 2c 0a 20 20 20 20 20 20 20 20  t iPos,.        
2feab 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69             int i
2feac 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 6e 74  StartOffset, int
2fead 20 69 45 6e 64 4f 66 66 73 65 74 29 7b 0a 20 20   iEndOffset){.  
2feae 2f 2a 20 57 6f 72 73 74 2d 63 61 73 65 20 73 70  /* Worst-case sp
2feaf 61 63 65 20 66 6f 72 20 50 4f 53 5f 43 4f 4c 55  ace for POS_COLU
2feb0 4d 4e 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f  MN, iColumn, iPo
2feb1 73 44 65 6c 74 61 2c 0a 20 20 2a 2a 20 69 53 74  sDelta,.  ** iSt
2feb2 61 72 74 4f 66 66 73 65 74 44 65 6c 74 61 2c 20  artOffsetDelta, 
2feb3 61 6e 64 20 69 45 6e 64 4f 66 66 73 65 74 44 65  and iEndOffsetDe
2feb4 6c 74 61 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72  lta..  */.  char
2feb5 20 63 5b 35 2a 56 41 52 49 4e 54 5f 4d 41 58 5d   c[5*VARINT_MAX]
2feb6 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 0a  ;.  int n = 0;..
2feb7 20 20 2f 2a 20 42 61 6e 20 70 6c 77 41 64 64 28    /* Ban plwAdd(
2feb8 29 20 61 66 74 65 72 20 70 6c 77 54 65 72 6d 69  ) after plwTermi
2feb9 6e 61 74 65 28 29 2e 20 2a 2f 0a 20 20 61 73 73  nate(). */.  ass
2feba 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 69 50  ert( pWriter->iP
2febb 6f 73 21 3d 2d 31 20 29 3b 0a 0a 20 20 69 66 28  os!=-1 );..  if(
2febc 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69   pWriter->dlw->i
2febd 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44 53 20  Type==DL_DOCIDS 
2febe 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28  ) return;..  if(
2febf 20 69 43 6f 6c 75 6d 6e 21 3d 70 57 72 69 74 65   iColumn!=pWrite
2fec0 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  r->iColumn ){.  
2fec1 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61    n += fts3PutVa
2fec2 72 69 6e 74 28 63 2b 6e 2c 20 50 4f 53 5f 43 4f  rint(c+n, POS_CO
2fec3 4c 55 4d 4e 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  LUMN);.    n += 
2fec4 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
2fec5 6e 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  n, iColumn);.   
2fec6 20 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d   pWriter->iColum
2fec7 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  n = iColumn;.   
2fec8 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d   pWriter->iPos =
2fec9 20 30 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d   0;.    pWriter-
2feca 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  >iOffset = 0;.  
2fecb 7d 0a 20 20 61 73 73 65 72 74 28 20 69 50 6f 73  }.  assert( iPos
2fecc 3e 3d 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20  >=pWriter->iPos 
2fecd 29 3b 0a 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  );.  n += fts3Pu
2fece 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 50 4f 53  tVarint(c+n, POS
2fecf 5f 42 41 53 45 2b 28 69 50 6f 73 2d 70 57 72 69  _BASE+(iPos-pWri
2fed0 74 65 72 2d 3e 69 50 6f 73 29 29 3b 0a 20 20 70  ter->iPos));.  p
2fed1 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d 20 69  Writer->iPos = i
2fed2 50 6f 73 3b 0a 20 20 69 66 28 20 70 57 72 69 74  Pos;.  if( pWrit
2fed3 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3d 3d  er->dlw->iType==
2fed4 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46  DL_POSITIONS_OFF
2fed5 53 45 54 53 20 29 7b 0a 20 20 20 20 61 73 73 65  SETS ){.    asse
2fed6 72 74 28 20 69 53 74 61 72 74 4f 66 66 73 65 74  rt( iStartOffset
2fed7 3e 3d 70 57 72 69 74 65 72 2d 3e 69 4f 66 66 73  >=pWriter->iOffs
2fed8 65 74 20 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66  et );.    n += f
2fed9 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e  ts3PutVarint(c+n
2feda 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74 2d 70  , iStartOffset-p
2fedb 57 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 29  Writer->iOffset)
2fedc 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69  ;.    pWriter->i
2fedd 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72 74 4f  Offset = iStartO
2fede 66 66 73 65 74 3b 0a 20 20 20 20 61 73 73 65 72  ffset;.    asser
2fedf 74 28 20 69 45 6e 64 4f 66 66 73 65 74 3e 3d 69  t( iEndOffset>=i
2fee0 53 74 61 72 74 4f 66 66 73 65 74 20 29 3b 0a 20  StartOffset );. 
2fee1 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56     n += fts3PutV
2fee2 61 72 69 6e 74 28 63 2b 6e 2c 20 69 45 6e 64 4f  arint(c+n, iEndO
2fee3 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66 73  ffset-iStartOffs
2fee4 65 74 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42  et);.  }.  dataB
2fee5 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72 69  ufferAppend(pWri
2fee6 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20 63 2c 20  ter->dlw->b, c, 
2fee7 6e 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  n);.}.static voi
2fee8 64 20 70 6c 77 43 6f 70 79 28 50 4c 57 72 69 74  d plwCopy(PLWrit
2fee9 65 72 20 2a 70 57 72 69 74 65 72 2c 20 50 4c 52  er *pWriter, PLR
2feea 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2feeb 0a 20 20 70 6c 77 41 64 64 28 70 57 72 69 74 65  .  plwAdd(pWrite
2feec 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 65  r, plrColumn(pRe
2feed 61 64 65 72 29 2c 20 70 6c 72 50 6f 73 69 74 69  ader), plrPositi
2feee 6f 6e 28 70 52 65 61 64 65 72 29 2c 0a 20 20 20  on(pReader),.   
2feef 20 20 20 20 20 20 70 6c 72 53 74 61 72 74 4f 66        plrStartOf
2fef0 66 73 65 74 28 70 52 65 61 64 65 72 29 2c 20 70  fset(pReader), p
2fef1 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 52 65 61  lrEndOffset(pRea
2fef2 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20  der));.}.static 
2fef3 76 6f 69 64 20 70 6c 77 49 6e 69 74 28 50 4c 57  void plwInit(PLW
2fef4 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20  riter *pWriter, 
2fef5 44 4c 57 72 69 74 65 72 20 2a 64 6c 77 2c 20 73  DLWriter *dlw, s
2fef6 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
2fef7 69 64 29 7b 0a 20 20 63 68 61 72 20 63 5b 56 41  id){.  char c[VA
2fef8 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74  RINT_MAX];.  int
2fef9 20 6e 3b 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e   n;..  pWriter->
2fefa 64 6c 77 20 3d 20 64 6c 77 3b 0a 0a 20 20 2f 2a  dlw = dlw;..  /*
2fefb 20 44 6f 63 69 64 73 20 6d 75 73 74 20 61 73 63   Docids must asc
2fefc 65 6e 64 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  end. */.  assert
2fefd 28 20 21 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d  ( !pWriter->dlw-
2fefe 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20  >has_iPrevDocid 
2feff 7c 7c 20 69 44 6f 63 69 64 3e 70 57 72 69 74 65  || iDocid>pWrite
2ff00 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76 44 6f 63  r->dlw->iPrevDoc
2ff01 69 64 20 29 3b 0a 20 20 6e 20 3d 20 66 74 73 33  id );.  n = fts3
2ff02 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69 44 6f  PutVarint(c, iDo
2ff03 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 64 6c 77  cid-pWriter->dlw
2ff04 2d 3e 69 50 72 65 76 44 6f 63 69 64 29 3b 0a 20  ->iPrevDocid);. 
2ff05 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
2ff06 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e  d(pWriter->dlw->
2ff07 62 2c 20 63 2c 20 6e 29 3b 0a 20 20 70 57 72 69  b, c, n);.  pWri
2ff08 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76 44  ter->dlw->iPrevD
2ff09 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 23  ocid = iDocid;.#
2ff0a 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
2ff0b 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 68 61  pWriter->dlw->ha
2ff0c 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20 31  s_iPrevDocid = 1
2ff0d 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 70 57 72 69  ;.#endif..  pWri
2ff0e 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 30  ter->iColumn = 0
2ff0f 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f  ;.  pWriter->iPo
2ff10 73 20 3d 20 30 3b 0a 20 20 70 57 72 69 74 65 72  s = 0;.  pWriter
2ff11 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 7d  ->iOffset = 0;.}
2ff12 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
2ff13 53 68 6f 75 6c 64 20 70 6c 77 44 65 73 74 72 6f  Should plwDestro
2ff14 79 28 29 20 61 6c 73 6f 20 74 65 72 6d 69 6e 61  y() also termina
2ff15 74 65 20 74 68 65 20 64 6f 63 6c 69 73 74 3f 20  te the doclist? 
2ff16 20 42 75 74 0a 2a 2a 20 74 68 65 6e 20 70 6c 77   But.** then plw
2ff17 44 65 73 74 72 6f 79 28 29 20 77 6f 75 6c 64 20  Destroy() would 
2ff18 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 6a 75 73  no longer be jus
2ff19 74 20 61 20 64 65 73 74 72 75 63 74 6f 72 2c 20  t a destructor, 
2ff1a 69 74 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f  it would.** also
2ff1b 20 62 65 20 64 6f 69 6e 67 20 77 6f 72 6b 2c 20   be doing work, 
2ff1c 77 68 69 63 68 20 69 73 6e 27 74 20 63 6f 6e 73  which isn't cons
2ff1d 69 73 74 65 6e 74 20 77 69 74 68 20 74 68 65 20  istent with the 
2ff1e 6f 76 65 72 61 6c 6c 20 69 64 69 6f 6d 2e 0a 2a  overall idiom..*
2ff1f 2a 20 41 6e 6f 74 68 65 72 20 6f 70 74 69 6f 6e  * Another option
2ff20 20 77 6f 75 6c 64 20 62 65 20 66 6f 72 20 70 6c   would be for pl
2ff21 77 41 64 64 28 29 20 74 6f 20 61 6c 77 61 79 73  wAdd() to always
2ff22 20 61 70 70 65 6e 64 20 61 6e 79 20 6e 65 63 65   append any nece
2ff23 73 73 61 72 79 0a 2a 2a 20 74 65 72 6d 69 6e 61  ssary.** termina
2ff24 74 6f 72 2c 20 73 6f 20 74 68 61 74 20 74 68 65  tor, so that the
2ff25 20 6f 75 74 70 75 74 20 69 73 20 61 6c 77 61 79   output is alway
2ff26 73 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20  s correct.  But 
2ff27 74 68 61 74 20 77 6f 75 6c 64 0a 2a 2a 20 61 64  that would.** ad
2ff28 64 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 77 6f  d incremental wo
2ff29 72 6b 20 74 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e  rk to the common
2ff2a 20 63 61 73 65 20 77 69 74 68 20 74 68 65 20 6f   case with the o
2ff2b 6e 6c 79 20 62 65 6e 65 66 69 74 20 62 65 69 6e  nly benefit bein
2ff2c 67 0a 2a 2a 20 41 50 49 20 65 6c 65 67 61 6e 63  g.** API eleganc
2ff2d 65 2e 20 20 50 75 6e 74 20 66 6f 72 20 6e 6f 77  e.  Punt for now
2ff2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2ff2f 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 50 4c   plwTerminate(PL
2ff30 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29  Writer *pWriter)
2ff31 7b 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  {.  if( pWriter-
2ff32 3e 64 6c 77 2d 3e 69 54 79 70 65 3e 44 4c 5f 44  >dlw->iType>DL_D
2ff33 4f 43 49 44 53 20 29 7b 0a 20 20 20 20 63 68 61  OCIDS ){.    cha
2ff34 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b  r c[VARINT_MAX];
2ff35 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 66 74 73  .    int n = fts
2ff36 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 50 4f  3PutVarint(c, PO
2ff37 53 5f 45 4e 44 29 3b 0a 20 20 20 20 64 61 74 61  S_END);.    data
2ff38 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72  BufferAppend(pWr
2ff39 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20 63 2c  iter->dlw->b, c,
2ff3a 20 6e 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66   n);.  }.#ifndef
2ff3b 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 4d 61 72   NDEBUG.  /* Mar
2ff3c 6b 20 61 73 20 74 65 72 6d 69 6e 61 74 65 64 20  k as terminated 
2ff3d 66 6f 72 20 61 73 73 65 72 74 20 69 6e 20 70 6c  for assert in pl
2ff3e 77 41 64 64 28 29 2e 20 2a 2f 0a 20 20 70 57 72  wAdd(). */.  pWr
2ff3f 69 74 65 72 2d 3e 69 50 6f 73 20 3d 20 2d 31 3b  iter->iPos = -1;
2ff40 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69 63  .#endif.}.static
2ff41 20 76 6f 69 64 20 70 6c 77 44 65 73 74 72 6f 79   void plwDestroy
2ff42 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74  (PLWriter *pWrit
2ff43 65 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28  er){.  SCRAMBLE(
2ff44 70 57 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 2a  pWriter);.}../**
2ff45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ff46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ff47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ff48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ff49 2a 2f 0a 2f 2a 20 44 4c 43 6f 6c 6c 65 63 74 6f  */./* DLCollecto
2ff4a 72 20 77 72 61 70 73 20 50 4c 57 72 69 74 65 72  r wraps PLWriter
2ff4b 20 61 6e 64 20 44 4c 57 72 69 74 65 72 20 74 6f   and DLWriter to
2ff4c 20 70 72 6f 76 69 64 65 20 61 0a 2a 2a 20 64 79   provide a.** dy
2ff4d 6e 61 6d 69 63 61 6c 6c 79 2d 61 6c 6c 6f 63 61  namically-alloca
2ff4e 74 65 64 20 64 6f 63 6c 69 73 74 20 61 72 65 61  ted doclist area
2ff4f 20 74 6f 20 75 73 65 20 64 75 72 69 6e 67 20 74   to use during t
2ff50 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a  okenization..**.
2ff51 2a 2a 20 64 6c 63 4e 65 77 20 2d 20 6d 61 6c 6c  ** dlcNew - mall
2ff52 6f 63 20 75 70 20 61 6e 64 20 69 6e 69 74 69 61  oc up and initia
2ff53 6c 69 7a 65 20 61 20 63 6f 6c 6c 65 63 74 6f 72  lize a collector
2ff54 2e 0a 2a 2a 20 64 6c 63 44 65 6c 65 74 65 20 2d  ..** dlcDelete -
2ff55 20 64 65 73 74 72 6f 79 20 61 20 63 6f 6c 6c 65   destroy a colle
2ff56 63 74 6f 72 20 61 6e 64 20 61 6c 6c 20 63 6f 6e  ctor and all con
2ff57 74 61 69 6e 65 64 20 69 74 65 6d 73 2e 0a 2a 2a  tained items..**
2ff58 20 64 6c 63 41 64 64 50 6f 73 20 2d 20 61 70 70   dlcAddPos - app
2ff59 65 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61 6e 64  end position and
2ff5a 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61 74   offset informat
2ff5b 69 6f 6e 2e 0a 2a 2a 20 64 6c 63 41 64 64 44 6f  ion..** dlcAddDo
2ff5c 63 6c 69 73 74 20 2d 20 61 64 64 20 74 68 65 20  clist - add the 
2ff5d 63 6f 6c 6c 65 63 74 65 64 20 64 6f 63 6c 69 73  collected doclis
2ff5e 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 62  t to the given b
2ff5f 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 63 4e 65 78  uffer..** dlcNex
2ff60 74 20 2d 20 74 65 72 6d 69 6e 61 74 65 20 74 68  t - terminate th
2ff61 65 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65  e current docume
2ff62 6e 74 20 61 6e 64 20 6f 70 65 6e 20 61 6e 6f 74  nt and open anot
2ff63 68 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  her..*/.typedef 
2ff64 73 74 72 75 63 74 20 44 4c 43 6f 6c 6c 65 63 74  struct DLCollect
2ff65 6f 72 20 7b 0a 20 20 44 61 74 61 42 75 66 66 65  or {.  DataBuffe
2ff66 72 20 62 3b 0a 20 20 44 4c 57 72 69 74 65 72 20  r b;.  DLWriter 
2ff67 64 6c 77 3b 0a 20 20 50 4c 57 72 69 74 65 72 20  dlw;.  PLWriter 
2ff68 70 6c 77 3b 0a 7d 20 44 4c 43 6f 6c 6c 65 63 74  plw;.} DLCollect
2ff69 6f 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  or;../* TODO(she
2ff6a 73 73 29 20 54 68 69 73 20 63 6f 75 6c 64 20 61  ss) This could a
2ff6b 6c 73 6f 20 62 65 20 64 6f 6e 65 20 62 79 20 63  lso be done by c
2ff6c 61 6c 6c 69 6e 67 20 70 6c 77 54 65 72 6d 69 6e  alling plwTermin
2ff6d 61 74 65 28 29 20 61 6e 64 0a 2a 2a 20 64 61 74  ate() and.** dat
2ff6e 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 29 2e  aBufferAppend().
2ff6f 20 20 49 20 74 72 69 65 64 20 74 68 61 74 2c 20    I tried that, 
2ff70 65 78 70 65 63 74 69 6e 67 20 6e 6f 6d 69 6e 61  expecting nomina
2ff71 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a  l performance.**
2ff72 20 64 69 66 66 65 72 65 6e 63 65 73 2c 20 62 75   differences, bu
2ff73 74 20 69 74 20 73 65 65 6d 65 64 20 74 6f 20 70  t it seemed to p
2ff74 72 65 74 74 79 20 72 65 6c 69 61 62 6c 79 20 62  retty reliably b
2ff75 65 20 77 6f 72 74 68 20 31 25 20 74 6f 20 63 6f  e worth 1% to co
2ff76 64 65 0a 2a 2a 20 69 74 20 74 68 69 73 20 77 61  de.** it this wa
2ff77 79 2e 20 20 49 20 73 75 73 70 65 63 74 20 69 74  y.  I suspect it
2ff78 20 69 73 20 74 68 65 20 69 6e 63 72 65 6d 65 6e   is the incremen
2ff79 74 61 6c 20 6d 61 6c 6c 6f 63 20 6f 76 65 72 68  tal malloc overh
2ff7a 65 61 64 20 28 73 6f 6d 65 0a 2a 2a 20 70 65 72  ead (some.** per
2ff7b 63 65 6e 74 61 67 65 20 6f 66 20 74 68 65 20 70  centage of the p
2ff7c 6c 77 54 65 72 6d 69 6e 61 74 65 28 29 20 63 61  lwTerminate() ca
2ff7d 6c 6c 73 20 77 69 6c 6c 20 63 61 75 73 65 20 61  lls will cause a
2ff7e 20 72 65 61 6c 6c 6f 63 29 2c 20 73 6f 0a 2a 2a   realloc), so.**
2ff7f 20 74 68 69 73 20 6d 69 67 68 74 20 62 65 20 77   this might be w
2ff80 6f 72 74 68 20 72 65 76 69 73 69 74 69 6e 67 20  orth revisiting 
2ff81 69 66 20 74 68 65 20 44 61 74 61 42 75 66 66 65  if the DataBuffe
2ff82 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  r implementation
2ff83 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 0a 2a 2f 0a  .** changes..*/.
2ff84 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 63 41  static void dlcA
2ff85 64 64 44 6f 63 6c 69 73 74 28 44 4c 43 6f 6c 6c  ddDoclist(DLColl
2ff86 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74 6f  ector *pCollecto
2ff87 72 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a 62  r, DataBuffer *b
2ff88 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 65 63  ){.  if( pCollec
2ff89 74 6f 72 2d 3e 64 6c 77 2e 69 54 79 70 65 3e 44  tor->dlw.iType>D
2ff8a 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20 20  L_DOCIDS ){.    
2ff8b 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
2ff8c 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20  X];.    int n = 
2ff8d 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c  fts3PutVarint(c,
2ff8e 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20 20 20 64   POS_END);.    d
2ff8f 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32  ataBufferAppend2
2ff90 28 62 2c 20 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  (b, pCollector->
2ff91 62 2e 70 44 61 74 61 2c 20 70 43 6f 6c 6c 65 63  b.pData, pCollec
2ff92 74 6f 72 2d 3e 62 2e 6e 44 61 74 61 2c 20 63 2c  tor->b.nData, c,
2ff93 20 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20   n);.  }else{.  
2ff94 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
2ff95 6e 64 28 62 2c 20 70 43 6f 6c 6c 65 63 74 6f 72  nd(b, pCollector
2ff96 2d 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f 6c 6c  ->b.pData, pColl
2ff97 65 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61 29 3b  ector->b.nData);
2ff98 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
2ff99 69 64 20 64 6c 63 4e 65 78 74 28 44 4c 43 6f 6c  id dlcNext(DLCol
2ff9a 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74  lector *pCollect
2ff9b 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  or, sqlite_int64
2ff9c 20 69 44 6f 63 69 64 29 7b 0a 20 20 70 6c 77 54   iDocid){.  plwT
2ff9d 65 72 6d 69 6e 61 74 65 28 26 70 43 6f 6c 6c 65  erminate(&pColle
2ff9e 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 70 6c  ctor->plw);.  pl
2ff9f 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65  wDestroy(&pColle
2ffa0 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 70 6c  ctor->plw);.  pl
2ffa1 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f  wInit(&pCollecto
2ffa2 72 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c 65 63  r->plw, &pCollec
2ffa3 74 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63 69 64  tor->dlw, iDocid
2ffa4 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
2ffa5 20 64 6c 63 41 64 64 50 6f 73 28 44 4c 43 6f 6c   dlcAddPos(DLCol
2ffa6 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74  lector *pCollect
2ffa7 6f 72 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c  or, int iColumn,
2ffa8 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20 20 20 20   int iPos,.     
2ffa9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ffaa 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65   int iStartOffse
2ffab 74 2c 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65  t, int iEndOffse
2ffac 74 29 7b 0a 20 20 70 6c 77 41 64 64 28 26 70 43  t){.  plwAdd(&pC
2ffad 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 69  ollector->plw, i
2ffae 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73 2c 20 69 53  Column, iPos, iS
2ffaf 74 61 72 74 4f 66 66 73 65 74 2c 20 69 45 6e 64  tartOffset, iEnd
2ffb0 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a 73 74 61 74  Offset);.}..stat
2ffb1 69 63 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a  ic DLCollector *
2ffb2 64 6c 63 4e 65 77 28 73 71 6c 69 74 65 5f 69 6e  dlcNew(sqlite_in
2ffb3 74 36 34 20 69 44 6f 63 69 64 2c 20 44 6f 63 4c  t64 iDocid, DocL
2ffb4 69 73 74 54 79 70 65 20 69 54 79 70 65 29 7b 0a  istType iType){.
2ffb5 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70    DLCollector *p
2ffb6 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 73 71 6c 69  Collector = sqli
2ffb7 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
2ffb8 66 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 29 29 3b  f(DLCollector));
2ffb9 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  .  dataBufferIni
2ffba 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62  t(&pCollector->b
2ffbb 2c 20 30 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28  , 0);.  dlwInit(
2ffbc 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77  &pCollector->dlw
2ffbd 2c 20 69 54 79 70 65 2c 20 26 70 43 6f 6c 6c 65  , iType, &pColle
2ffbe 63 74 6f 72 2d 3e 62 29 3b 0a 20 20 70 6c 77 49  ctor->b);.  plwI
2ffbf 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d  nit(&pCollector-
2ffc0 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c 65 63 74 6f  >plw, &pCollecto
2ffc1 72 2d 3e 64 6c 77 2c 20 69 44 6f 63 69 64 29 3b  r->dlw, iDocid);
2ffc2 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 65  .  return pColle
2ffc3 63 74 6f 72 3b 0a 7d 0a 73 74 61 74 69 63 20 76  ctor;.}.static v
2ffc4 6f 69 64 20 64 6c 63 44 65 6c 65 74 65 28 44 4c  oid dlcDelete(DL
2ffc5 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c  Collector *pColl
2ffc6 65 63 74 6f 72 29 7b 0a 20 20 70 6c 77 44 65 73  ector){.  plwDes
2ffc7 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74 6f 72  troy(&pCollector
2ffc8 2d 3e 70 6c 77 29 3b 0a 20 20 64 6c 77 44 65 73  ->plw);.  dlwDes
2ffc9 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74 6f 72  troy(&pCollector
2ffca 2d 3e 64 6c 77 29 3b 0a 20 20 64 61 74 61 42 75  ->dlw);.  dataBu
2ffcb 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 43 6f  fferDestroy(&pCo
2ffcc 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a 20 20 53  llector->b);.  S
2ffcd 43 52 41 4d 42 4c 45 28 70 43 6f 6c 6c 65 63 74  CRAMBLE(pCollect
2ffce 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  or);.  sqlite3_f
2ffcf 72 65 65 28 70 43 6f 6c 6c 65 63 74 6f 72 29 3b  ree(pCollector);
2ffd0 0a 7d 0a 0a 0a 2f 2a 20 43 6f 70 79 20 74 68 65  .}.../* Copy the
2ffd1 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 6f 66   doclist data of
2ffd2 20 69 54 79 70 65 20 69 6e 20 70 44 61 74 61 2f   iType in pData/
2ffd3 6e 44 61 74 61 20 69 6e 74 6f 20 2a 6f 75 74 2c  nData into *out,
2ffd4 20 74 72 69 6d 6d 69 6e 67 0a 2a 2a 20 75 6e 6e   trimming.** unn
2ffd5 65 63 65 73 73 61 72 79 20 64 61 74 61 20 61 73  ecessary data as
2ffd6 20 77 65 20 67 6f 2e 20 20 4f 6e 6c 79 20 63 6f   we go.  Only co
2ffd7 6c 75 6d 6e 73 20 6d 61 74 63 68 69 6e 67 20 69  lumns matching i
2ffd8 43 6f 6c 75 6d 6e 20 61 72 65 0a 2a 2a 20 63 6f  Column are.** co
2ffd9 70 69 65 64 2c 20 61 6c 6c 20 63 6f 6c 75 6d 6e  pied, all column
2ffda 73 20 63 6f 70 69 65 64 20 69 66 20 69 43 6f 6c  s copied if iCol
2ffdb 75 6d 6e 20 69 73 20 2d 31 2e 20 20 45 6c 65 6d  umn is -1.  Elem
2ffdc 65 6e 74 73 20 77 69 74 68 20 6e 6f 0a 2a 2a 20  ents with no.** 
2ffdd 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 73  matching columns
2ffde 20 61 72 65 20 64 72 6f 70 70 65 64 2e 20 20 54   are dropped.  T
2ffdf 68 65 20 6f 75 74 70 75 74 20 69 73 20 61 6e 20  he output is an 
2ffe0 69 4f 75 74 54 79 70 65 20 64 6f 63 6c 69 73 74  iOutType doclist
2ffe1 2e 0a 2a 2f 0a 2f 2a 20 4e 4f 54 45 28 73 68 65  ..*/./* NOTE(she
2ffe2 73 73 29 20 54 68 69 73 20 63 6f 64 65 20 69 73  ss) This code is
2ffe3 20 6f 6e 6c 79 20 76 61 6c 69 64 20 61 66 74 65   only valid afte
2ffe4 72 20 61 6c 6c 20 64 6f 63 6c 69 73 74 73 20 61  r all doclists a
2ffe5 72 65 20 6d 65 72 67 65 64 2e 0a 2a 2a 20 49 66  re merged..** If
2ffe6 20 74 68 69 73 20 69 73 20 72 75 6e 20 62 65 66   this is run bef
2ffe7 6f 72 65 20 6d 65 72 67 65 73 2c 20 74 68 65 6e  ore merges, then
2ffe8 20 64 6f 63 6c 69 73 74 20 69 74 65 6d 73 20 77   doclist items w
2ffe9 68 69 63 68 20 72 65 70 72 65 73 65 6e 74 0a 2a  hich represent.*
2ffea 2a 20 64 65 6c 65 74 69 6f 6e 20 77 69 6c 6c 20  * deletion will 
2ffeb 62 65 20 74 72 69 6d 6d 65 64 2c 20 61 6e 64 20  be trimmed, and 
2ffec 77 69 6c 6c 20 74 68 75 73 20 6e 6f 74 20 65 66  will thus not ef
2ffed 66 65 63 74 20 61 20 64 65 6c 65 74 69 6f 6e 0a  fect a deletion.
2ffee 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 6d 65  ** during the me
2ffef 72 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  rge..*/.static v
2fff0 6f 69 64 20 64 6f 63 4c 69 73 74 54 72 69 6d 28  oid docListTrim(
2fff1 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
2fff2 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  e, const char *p
2fff3 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
2fff4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2fff5 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f           int iCo
2fff6 6c 75 6d 6e 2c 20 44 6f 63 4c 69 73 74 54 79 70  lumn, DocListTyp
2fff7 65 20 69 4f 75 74 54 79 70 65 2c 20 44 61 74 61  e iOutType, Data
2fff8 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20  Buffer *out){.  
2fff9 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64 65  DLReader dlReade
2fffa 72 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 64 6c  r;.  DLWriter dl
2fffb 57 72 69 74 65 72 3b 0a 0a 20 20 61 73 73 65 72  Writer;..  asser
2fffc 74 28 20 69 4f 75 74 54 79 70 65 3c 3d 69 54 79  t( iOutType<=iTy
2fffd 70 65 20 29 3b 0a 0a 20 20 64 6c 72 49 6e 69 74  pe );..  dlrInit
2fffe 28 26 64 6c 52 65 61 64 65 72 2c 20 69 54 79 70  (&dlReader, iTyp
2ffff 65 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29  e, pData, nData)
30000 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26 64 6c 57  ;.  dlwInit(&dlW
30001 72 69 74 65 72 2c 20 69 4f 75 74 54 79 70 65 2c  riter, iOutType,
30002 20 6f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28   out);..  while(
30003 20 21 64 6c 72 41 74 45 6e 64 28 26 64 6c 52 65   !dlrAtEnd(&dlRe
30004 61 64 65 72 29 20 29 7b 0a 20 20 20 20 50 4c 52  ader) ){.    PLR
30005 65 61 64 65 72 20 70 6c 52 65 61 64 65 72 3b 0a  eader plReader;.
30006 20 20 20 20 50 4c 57 72 69 74 65 72 20 70 6c 57      PLWriter plW
30007 72 69 74 65 72 3b 0a 20 20 20 20 69 6e 74 20 6d  riter;.    int m
30008 61 74 63 68 20 3d 20 30 3b 0a 0a 20 20 20 20 70  atch = 0;..    p
30009 6c 72 49 6e 69 74 28 26 70 6c 52 65 61 64 65 72  lrInit(&plReader
3000a 2c 20 26 64 6c 52 65 61 64 65 72 29 3b 0a 0a 20  , &dlReader);.. 
3000b 20 20 20 77 68 69 6c 65 28 20 21 70 6c 72 41 74     while( !plrAt
3000c 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29 20 29  End(&plReader) )
3000d 7b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  {.      if( iCol
3000e 75 6d 6e 3d 3d 2d 31 20 7c 7c 20 70 6c 72 43 6f  umn==-1 || plrCo
3000f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 3d  lumn(&plReader)=
30010 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  =iColumn ){.    
30011 20 20 20 20 69 66 28 20 21 6d 61 74 63 68 20 29      if( !match )
30012 7b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 49  {.          plwI
30013 6e 69 74 28 26 70 6c 57 72 69 74 65 72 2c 20 26  nit(&plWriter, &
30014 64 6c 57 72 69 74 65 72 2c 20 64 6c 72 44 6f 63  dlWriter, dlrDoc
30015 69 64 28 26 64 6c 52 65 61 64 65 72 29 29 3b 0a  id(&dlReader));.
30016 20 20 20 20 20 20 20 20 20 20 6d 61 74 63 68 20            match 
30017 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
30018 20 20 20 20 20 20 20 70 6c 77 41 64 64 28 26 70         plwAdd(&p
30019 6c 57 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75  lWriter, plrColu
3001a 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 2c 20 70  mn(&plReader), p
3001b 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52 65  lrPosition(&plRe
3001c 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20 20  ader),.         
3001d 20 20 20 20 20 20 70 6c 72 53 74 61 72 74 4f 66        plrStartOf
3001e 66 73 65 74 28 26 70 6c 52 65 61 64 65 72 29 2c  fset(&plReader),
3001f 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 26 70   plrEndOffset(&p
30020 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20 20  lReader));.     
30021 20 7d 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70   }.      plrStep
30022 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 20 20 20  (&plReader);.   
30023 20 7d 0a 20 20 20 20 69 66 28 20 6d 61 74 63 68   }.    if( match
30024 20 29 7b 0a 20 20 20 20 20 20 70 6c 77 54 65 72   ){.      plwTer
30025 6d 69 6e 61 74 65 28 26 70 6c 57 72 69 74 65 72  minate(&plWriter
30026 29 3b 0a 20 20 20 20 20 20 70 6c 77 44 65 73 74  );.      plwDest
30027 72 6f 79 28 26 70 6c 57 72 69 74 65 72 29 3b 0a  roy(&plWriter);.
30028 20 20 20 20 7d 0a 0a 20 20 20 20 70 6c 72 44 65      }..    plrDe
30029 73 74 72 6f 79 28 26 70 6c 52 65 61 64 65 72 29  stroy(&plReader)
3002a 3b 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26 64  ;.    dlrStep(&d
3002b 6c 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20  lReader);.  }.  
3002c 64 6c 77 44 65 73 74 72 6f 79 28 26 64 6c 57 72  dlwDestroy(&dlWr
3002d 69 74 65 72 29 3b 0a 20 20 64 6c 72 44 65 73 74  iter);.  dlrDest
3002e 72 6f 79 28 26 64 6c 52 65 61 64 65 72 29 3b 0a  roy(&dlReader);.
3002f 7d 0a 0a 2f 2a 20 55 73 65 64 20 62 79 20 64 6f  }../* Used by do
30030 63 4c 69 73 74 4d 65 72 67 65 28 29 20 74 6f 20  cListMerge() to 
30031 6b 65 65 70 20 64 6f 63 6c 69 73 74 73 20 69 6e  keep doclists in
30032 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6f   the ascending o
30033 72 64 65 72 20 62 79 0a 2a 2a 20 64 6f 63 69 64  rder by.** docid
30034 2c 20 74 68 65 6e 20 61 73 63 65 6e 64 69 6e 67  , then ascending
30035 20 6f 72 64 65 72 20 62 79 20 61 67 65 20 28 73   order by age (s
30036 6f 20 74 68 65 20 6e 65 77 65 73 74 20 63 6f 6d  o the newest com
30037 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 74 79  es first)..*/.ty
30038 70 65 64 65 66 20 73 74 72 75 63 74 20 4f 72 64  pedef struct Ord
30039 65 72 65 64 44 4c 52 65 61 64 65 72 20 7b 0a 20  eredDLReader {. 
3003a 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64   DLReader *pRead
3003b 65 72 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73  er;..  /* TODO(s
3003c 68 65 73 73 29 20 49 66 20 77 65 20 61 73 73 75  hess) If we assu
3003d 6d 65 20 74 68 61 74 20 64 6f 63 4c 69 73 74 4d  me that docListM
3003e 65 72 67 65 20 70 52 65 61 64 65 72 73 20 69 73  erge pReaders is
3003f 20 6f 72 64 65 72 65 64 20 62 79 0a 20 20 2a 2a   ordered by.  **
30040 20 61 67 65 20 28 77 68 69 63 68 20 77 65 20 64   age (which we d
30041 6f 29 2c 20 74 68 65 6e 20 77 65 20 63 6f 75 6c  o), then we coul
30042 64 20 75 73 65 20 70 52 65 61 64 65 72 20 63 6f  d use pReader co
30043 6d 70 61 72 69 73 6f 6e 73 20 74 6f 20 62 72 65  mparisons to bre
30044 61 6b 0a 20 20 2a 2a 20 74 69 65 73 2e 0a 20 20  ak.  ** ties..  
30045 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 0a 7d 20  */.  int idx;.} 
30046 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 3b  OrderedDLReader;
30047 0a 0a 2f 2a 20 4f 72 64 65 72 20 65 6f 66 20 74  ../* Order eof t
30048 6f 20 65 6e 64 2c 20 74 68 65 6e 20 62 79 20 64  o end, then by d
30049 6f 63 69 64 20 61 73 63 2c 20 69 64 78 20 64 65  ocid asc, idx de
3004a 73 63 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  sc. */.static in
3004b 74 20 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65  t orderedDLReade
3004c 72 43 6d 70 28 4f 72 64 65 72 65 64 44 4c 52 65  rCmp(OrderedDLRe
3004d 61 64 65 72 20 2a 72 31 2c 20 4f 72 64 65 72 65  ader *r1, Ordere
3004e 64 44 4c 52 65 61 64 65 72 20 2a 72 32 29 7b 0a  dDLReader *r2){.
3004f 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 72    if( dlrAtEnd(r
30050 31 2d 3e 70 52 65 61 64 65 72 29 20 29 7b 0a 20  1->pReader) ){. 
30051 20 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28     if( dlrAtEnd(
30052 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20 72  r2->pReader) ) r
30053 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 42 6f 74  eturn 0;  /* Bot
30054 68 20 61 74 45 6e 64 28 29 2e 20 2a 2f 0a 20 20  h atEnd(). */.  
30055 20 20 72 65 74 75 72 6e 20 31 3b 20 20 20 20 20    return 1;     
30056 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30057 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79           /* Only
30058 20 72 31 20 61 74 45 6e 64 28 29 2e 20 2a 2f 0a   r1 atEnd(). */.
30059 20 20 7d 0a 20 20 69 66 28 20 64 6c 72 41 74 45    }.  if( dlrAtE
3005a 6e 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20  nd(r2->pReader) 
3005b 29 20 72 65 74 75 72 6e 20 2d 31 3b 20 20 20 2f  ) return -1;   /
3005c 2a 20 4f 6e 6c 79 20 72 32 20 61 74 45 6e 64 28  * Only r2 atEnd(
3005d 29 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 64 6c 72  ). */..  if( dlr
3005e 44 6f 63 69 64 28 72 31 2d 3e 70 52 65 61 64 65  Docid(r1->pReade
3005f 72 29 3c 64 6c 72 44 6f 63 69 64 28 72 32 2d 3e  r)<dlrDocid(r2->
30060 70 52 65 61 64 65 72 29 20 29 20 72 65 74 75 72  pReader) ) retur
30061 6e 20 2d 31 3b 0a 20 20 69 66 28 20 64 6c 72 44  n -1;.  if( dlrD
30062 6f 63 69 64 28 72 31 2d 3e 70 52 65 61 64 65 72  ocid(r1->pReader
30063 29 3e 64 6c 72 44 6f 63 69 64 28 72 32 2d 3e 70  )>dlrDocid(r2->p
30064 52 65 61 64 65 72 29 20 29 20 72 65 74 75 72 6e  Reader) ) return
30065 20 31 3b 0a 0a 20 20 2f 2a 20 44 65 73 63 65 6e   1;..  /* Descen
30066 64 69 6e 67 20 6f 6e 20 69 64 78 2e 20 2a 2f 0a  ding on idx. */.
30067 20 20 72 65 74 75 72 6e 20 72 32 2d 3e 69 64 78    return r2->idx
30068 2d 72 31 2d 3e 69 64 78 3b 0a 7d 0a 0a 2f 2a 20  -r1->idx;.}../* 
30069 42 75 62 62 6c 65 20 70 5b 30 5d 20 74 6f 20 61  Bubble p[0] to a
3006a 70 70 72 6f 70 72 69 61 74 65 20 70 6c 61 63 65  ppropriate place
3006b 20 69 6e 20 70 5b 31 2e 2e 6e 2d 31 5d 2e 20 20   in p[1..n-1].  
3006c 41 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20  Assumes that.** 
3006d 70 5b 31 2e 2e 6e 2d 31 5d 20 69 73 20 61 6c 72  p[1..n-1] is alr
3006e 65 61 64 79 20 73 6f 72 74 65 64 2e 0a 2a 2f 0a  eady sorted..*/.
3006f 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49  /* TODO(shess) I
30070 73 20 74 68 69 73 20 66 72 65 71 75 65 6e 74 20  s this frequent 
30071 65 6e 6f 75 67 68 20 74 6f 20 77 61 72 72 61 6e  enough to warran
30072 74 20 61 20 62 69 6e 61 72 79 20 73 65 61 72 63  t a binary searc
30073 68 3f 0a 2a 2a 20 42 65 66 6f 72 65 20 69 6d 70  h?.** Before imp
30074 6c 65 6d 65 6e 74 69 6e 67 20 74 68 61 74 2c 20  lementing that, 
30075 69 6e 73 74 72 75 6d 65 6e 74 20 74 68 65 20 63  instrument the c
30076 6f 64 65 20 74 6f 20 63 68 65 63 6b 2e 20 20 49  ode to check.  I
30077 6e 20 6d 6f 73 74 0a 2a 2a 20 63 75 72 72 65 6e  n most.** curren
30078 74 20 75 73 61 67 65 2c 20 49 20 65 78 70 65 63  t usage, I expec
30079 74 20 74 68 61 74 20 70 5b 30 5d 20 77 69 6c 6c  t that p[0] will
3007a 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 70 5b   be less than p[
3007b 31 5d 20 61 20 76 65 72 79 0a 2a 2a 20 68 69 67  1] a very.** hig
3007c 68 20 70 72 6f 70 6f 72 74 69 6f 6e 20 6f 66 20  h proportion of 
3007d 74 68 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61  the time..*/.sta
3007e 74 69 63 20 76 6f 69 64 20 6f 72 64 65 72 65 64  tic void ordered
3007f 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72 28  DLReaderReorder(
30080 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 20  OrderedDLReader 
30081 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 77 68  *p, int n){.  wh
30082 69 6c 65 28 20 6e 3e 31 20 26 26 20 6f 72 64 65  ile( n>1 && orde
30083 72 65 64 44 4c 52 65 61 64 65 72 43 6d 70 28 70  redDLReaderCmp(p
30084 2c 20 70 2b 31 29 3e 30 20 29 7b 0a 20 20 20 20  , p+1)>0 ){.    
30085 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 20  OrderedDLReader 
30086 74 6d 70 20 3d 20 70 5b 30 5d 3b 0a 20 20 20 20  tmp = p[0];.    
30087 70 5b 30 5d 20 3d 20 70 5b 31 5d 3b 0a 20 20 20  p[0] = p[1];.   
30088 20 70 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20 20 20   p[1] = tmp;.   
30089 20 6e 2d 2d 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20   n--;.    p++;. 
3008a 20 7d 0a 7d 0a 0a 2f 2a 20 47 69 76 65 6e 20 61   }.}../* Given a
3008b 6e 20 61 72 72 61 79 20 6f 66 20 64 6f 63 6c 69  n array of docli
3008c 73 74 20 72 65 61 64 65 72 73 2c 20 6d 65 72 67  st readers, merg
3008d 65 20 74 68 65 69 72 20 64 6f 63 6c 69 73 74 20  e their doclist 
3008e 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e 74 6f  elements.** into
3008f 20 6f 75 74 20 69 6e 20 73 6f 72 74 65 64 20 6f   out in sorted o
30090 72 64 65 72 20 28 62 79 20 64 6f 63 69 64 29 2c  rder (by docid),
30091 20 64 72 6f 70 70 69 6e 67 20 65 6c 65 6d 65 6e   dropping elemen
30092 74 73 20 66 72 6f 6d 20 6f 6c 64 65 72 0a 2a 2a  ts from older.**
30093 20 72 65 61 64 65 72 73 20 77 68 65 6e 20 74 68   readers when th
30094 65 72 65 20 69 73 20 61 20 64 75 70 6c 69 63 61  ere is a duplica
30095 74 65 20 64 6f 63 69 64 2e 20 20 70 52 65 61 64  te docid.  pRead
30096 65 72 73 20 69 73 20 61 73 73 75 6d 65 64 20 74  ers is assumed t
30097 6f 20 62 65 0a 2a 2a 20 6f 72 64 65 72 65 64 20  o be.** ordered 
30098 62 79 20 61 67 65 2c 20 6f 6c 64 65 73 74 20 66  by age, oldest f
30099 69 72 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  irst..*/./* TODO
3009a 28 73 68 65 73 73 29 20 6e 52 65 61 64 65 72 73  (shess) nReaders
3009b 20 6d 75 73 74 20 62 65 20 3c 3d 20 4d 45 52 47   must be <= MERG
3009c 45 5f 43 4f 55 4e 54 2e 20 20 54 68 69 73 20 73  E_COUNT.  This s
3009d 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 0a 2a  hould probably.*
3009e 2a 20 62 65 20 66 69 78 65 64 2e 0a 2a 2f 0a 73  * be fixed..*/.s
3009f 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69  tatic void docLi
300a0 73 74 4d 65 72 67 65 28 44 61 74 61 42 75 66 66  stMerge(DataBuff
300a1 65 72 20 2a 6f 75 74 2c 0a 20 20 20 20 20 20 20  er *out,.       
300a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
300a3 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61    DLReader *pRea
300a4 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64 65  ders, int nReade
300a5 72 73 29 7b 0a 20 20 4f 72 64 65 72 65 64 44 4c  rs){.  OrderedDL
300a6 52 65 61 64 65 72 20 72 65 61 64 65 72 73 5b 4d  Reader readers[M
300a7 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 44  ERGE_COUNT];.  D
300a8 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
300a9 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 63 6f    int i, n;.  co
300aa 6e 73 74 20 63 68 61 72 20 2a 70 53 74 61 72 74  nst char *pStart
300ab 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 53 74 61   = 0;.  int nSta
300ac 72 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  rt = 0;.  sqlite
300ad 5f 69 6e 74 36 34 20 69 46 69 72 73 74 44 6f 63  _int64 iFirstDoc
300ae 69 64 20 3d 20 30 2c 20 69 4c 61 73 74 44 6f 63  id = 0, iLastDoc
300af 69 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  id = 0;..  asser
300b0 74 28 20 6e 52 65 61 64 65 72 73 3e 30 20 29 3b  t( nReaders>0 );
300b1 0a 20 20 69 66 28 20 6e 52 65 61 64 65 72 73 3d  .  if( nReaders=
300b2 3d 31 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75  =1 ){.    dataBu
300b3 66 66 65 72 41 70 70 65 6e 64 28 6f 75 74 2c 20  fferAppend(out, 
300b4 64 6c 72 44 6f 63 44 61 74 61 28 70 52 65 61 64  dlrDocData(pRead
300b5 65 72 73 29 2c 20 64 6c 72 41 6c 6c 44 61 74 61  ers), dlrAllData
300b6 42 79 74 65 73 28 70 52 65 61 64 65 72 73 29 29  Bytes(pReaders))
300b7 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
300b8 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65  }..  assert( nRe
300b9 61 64 65 72 73 3c 3d 4d 45 52 47 45 5f 43 4f 55  aders<=MERGE_COU
300ba 4e 54 20 29 3b 0a 20 20 6e 20 3d 20 30 3b 0a 20  NT );.  n = 0;. 
300bb 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 61   for(i=0; i<nRea
300bc 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ders; i++){.    
300bd 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72 73  assert( pReaders
300be 5b 69 5d 2e 69 54 79 70 65 3d 3d 70 52 65 61 64  [i].iType==pRead
300bf 65 72 73 5b 30 5d 2e 69 54 79 70 65 20 29 3b 0a  ers[0].iType );.
300c0 20 20 20 20 72 65 61 64 65 72 73 5b 69 5d 2e 70      readers[i].p
300c1 52 65 61 64 65 72 20 3d 20 70 52 65 61 64 65 72  Reader = pReader
300c2 73 2b 69 3b 0a 20 20 20 20 72 65 61 64 65 72 73  s+i;.    readers
300c3 5b 69 5d 2e 69 64 78 20 3d 20 69 3b 0a 20 20 20  [i].idx = i;.   
300c4 20 6e 20 2b 3d 20 64 6c 72 41 6c 6c 44 61 74 61   n += dlrAllData
300c5 42 79 74 65 73 28 26 70 52 65 61 64 65 72 73 5b  Bytes(&pReaders[
300c6 69 5d 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 43 6f  i]);.  }.  /* Co
300c7 6e 73 65 72 76 61 74 69 76 65 6c 79 20 73 69 7a  nservatively siz
300c8 65 20 6f 75 74 70 75 74 20 74 6f 20 73 75 6d 20  e output to sum 
300c9 6f 66 20 69 6e 70 75 74 73 2e 20 20 4f 75 74 70  of inputs.  Outp
300ca 75 74 20 73 68 6f 75 6c 64 20 65 6e 64 0a 20 20  ut should end.  
300cb 2a 2a 20 75 70 20 73 74 72 69 63 74 6c 79 20 73  ** up strictly s
300cc 6d 61 6c 6c 65 72 20 74 68 61 6e 20 69 6e 70 75  maller than inpu
300cd 74 2e 0a 20 20 2a 2f 0a 20 20 64 61 74 61 42 75  t..  */.  dataBu
300ce 66 66 65 72 45 78 70 61 6e 64 28 6f 75 74 2c 20  fferExpand(out, 
300cf 6e 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68  n);..  /* Get th
300d0 65 20 72 65 61 64 65 72 73 20 69 6e 74 6f 20 73  e readers into s
300d1 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 2a 2f 0a  orted order. */.
300d2 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29    while( i-->0 )
300d3 7b 0a 20 20 20 20 6f 72 64 65 72 65 64 44 4c 52  {.    orderedDLR
300d4 65 61 64 65 72 52 65 6f 72 64 65 72 28 72 65 61  eaderReorder(rea
300d5 64 65 72 73 2b 69 2c 20 6e 52 65 61 64 65 72 73  ders+i, nReaders
300d6 2d 69 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c 77 49  -i);.  }..  dlwI
300d7 6e 69 74 28 26 77 72 69 74 65 72 2c 20 70 52 65  nit(&writer, pRe
300d8 61 64 65 72 73 5b 30 5d 2e 69 54 79 70 65 2c 20  aders[0].iType, 
300d9 6f 75 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 21  out);.  while( !
300da 64 6c 72 41 74 45 6e 64 28 72 65 61 64 65 72 73  dlrAtEnd(readers
300db 5b 30 5d 2e 70 52 65 61 64 65 72 29 20 29 7b 0a  [0].pReader) ){.
300dc 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
300dd 20 69 44 6f 63 69 64 20 3d 20 64 6c 72 44 6f 63   iDocid = dlrDoc
300de 69 64 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52  id(readers[0].pR
300df 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20  eader);..    /* 
300e0 49 66 20 74 68 69 73 20 69 73 20 61 20 63 6f 6e  If this is a con
300e1 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 65  tinuation of the
300e2 20 63 75 72 72 65 6e 74 20 62 75 66 66 65 72 20   current buffer 
300e3 74 6f 20 63 6f 70 79 2c 20 65 78 74 65 6e 64 0a  to copy, extend.
300e4 20 20 20 20 2a 2a 20 74 68 61 74 20 62 75 66 66      ** that buff
300e5 65 72 2e 20 20 6d 65 6d 63 70 79 28 29 20 73 65  er.  memcpy() se
300e6 65 6d 73 20 74 6f 20 62 65 20 6d 6f 72 65 20 65  ems to be more e
300e7 66 66 69 63 69 65 6e 74 20 69 66 20 69 74 20 68  fficient if it h
300e8 61 73 20 61 0a 20 20 20 20 2a 2a 20 6c 6f 74 73  as a.    ** lots
300e9 20 6f 66 20 64 61 74 61 20 74 6f 20 63 6f 70 79   of data to copy
300ea 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
300eb 20 64 6c 72 44 6f 63 44 61 74 61 28 72 65 61 64   dlrDocData(read
300ec 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 3d  ers[0].pReader)=
300ed 3d 70 53 74 61 72 74 2b 6e 53 74 61 72 74 20 29  =pStart+nStart )
300ee 7b 0a 20 20 20 20 20 20 6e 53 74 61 72 74 20 2b  {.      nStart +
300ef 3d 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65  = dlrDocDataByte
300f0 73 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65  s(readers[0].pRe
300f1 61 64 65 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ader);.    }else
300f2 7b 0a 20 20 20 20 20 20 69 66 28 20 70 53 74 61  {.      if( pSta
300f3 72 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  rt!=0 ){.       
300f4 20 64 6c 77 41 70 70 65 6e 64 28 26 77 72 69 74   dlwAppend(&writ
300f5 65 72 2c 20 70 53 74 61 72 74 2c 20 6e 53 74 61  er, pStart, nSta
300f6 72 74 2c 20 69 46 69 72 73 74 44 6f 63 69 64 2c  rt, iFirstDocid,
300f7 20 69 4c 61 73 74 44 6f 63 69 64 29 3b 0a 20 20   iLastDocid);.  
300f8 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53 74 61      }.      pSta
300f9 72 74 20 3d 20 64 6c 72 44 6f 63 44 61 74 61 28  rt = dlrDocData(
300fa 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64  readers[0].pRead
300fb 65 72 29 3b 0a 20 20 20 20 20 20 6e 53 74 61 72  er);.      nStar
300fc 74 20 3d 20 64 6c 72 44 6f 63 44 61 74 61 42 79  t = dlrDocDataBy
300fd 74 65 73 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  tes(readers[0].p
300fe 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 69  Reader);.      i
300ff 46 69 72 73 74 44 6f 63 69 64 20 3d 20 69 44 6f  FirstDocid = iDo
30100 63 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  cid;.    }.    i
30101 4c 61 73 74 44 6f 63 69 64 20 3d 20 69 44 6f 63  LastDocid = iDoc
30102 69 64 3b 0a 20 20 20 20 64 6c 72 53 74 65 70 28  id;.    dlrStep(
30103 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64  readers[0].pRead
30104 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f  er);..    /* Dro
30105 70 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 6c 64  p all of the old
30106 65 72 20 65 6c 65 6d 65 6e 74 73 20 77 69 74 68  er elements with
30107 20 74 68 65 20 73 61 6d 65 20 64 6f 63 69 64 2e   the same docid.
30108 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   */.    for(i=1;
30109 20 69 3c 6e 52 65 61 64 65 72 73 20 26 26 0a 20   i<nReaders &&. 
3010a 20 20 20 20 20 20 20 20 20 20 20 20 21 64 6c 72              !dlr
3010b 41 74 45 6e 64 28 72 65 61 64 65 72 73 5b 69 5d  AtEnd(readers[i]
3010c 2e 70 52 65 61 64 65 72 29 20 26 26 0a 20 20 20  .pReader) &&.   
3010d 20 20 20 20 20 20 20 20 20 20 64 6c 72 44 6f 63            dlrDoc
3010e 69 64 28 72 65 61 64 65 72 73 5b 69 5d 2e 70 52  id(readers[i].pR
3010f 65 61 64 65 72 29 3d 3d 69 44 6f 63 69 64 3b 20  eader)==iDocid; 
30110 69 2b 2b 29 7b 0a 20 20 20 20 20 20 64 6c 72 53  i++){.      dlrS
30111 74 65 70 28 72 65 61 64 65 72 73 5b 69 5d 2e 70  tep(readers[i].p
30112 52 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 0a  Reader);.    }..
30113 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 72      /* Get the r
30114 65 61 64 65 72 73 20 62 61 63 6b 20 69 6e 74 6f  eaders back into
30115 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 77   order. */.    w
30116 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29 7b 0a 20  hile( i-->0 ){. 
30117 20 20 20 20 20 6f 72 64 65 72 65 64 44 4c 52 65       orderedDLRe
30118 61 64 65 72 52 65 6f 72 64 65 72 28 72 65 61 64  aderReorder(read
30119 65 72 73 2b 69 2c 20 6e 52 65 61 64 65 72 73 2d  ers+i, nReaders-
3011a 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  i);.    }.  }.. 
3011b 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20 61 6e   /* Copy over an
3011c 79 20 72 65 6d 61 69 6e 69 6e 67 20 65 6c 65 6d  y remaining elem
3011d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 28 20 6e  ents. */.  if( n
3011e 53 74 61 72 74 3e 30 20 29 20 64 6c 77 41 70 70  Start>0 ) dlwApp
3011f 65 6e 64 28 26 77 72 69 74 65 72 2c 20 70 53 74  end(&writer, pSt
30120 61 72 74 2c 20 6e 53 74 61 72 74 2c 20 69 46 69  art, nStart, iFi
30121 72 73 74 44 6f 63 69 64 2c 20 69 4c 61 73 74 44  rstDocid, iLastD
30122 6f 63 69 64 29 3b 0a 20 20 64 6c 77 44 65 73 74  ocid);.  dlwDest
30123 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a  roy(&writer);.}.
30124 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  ./* Helper funct
30125 69 6f 6e 20 66 6f 72 20 70 6f 73 4c 69 73 74 55  ion for posListU
30126 6e 69 6f 6e 28 29 2e 20 20 43 6f 6d 70 61 72 65  nion().  Compare
30127 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 6f  s the current po
30128 73 69 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65  sition.** betwee
30129 6e 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74  n left and right
3012a 2c 20 72 65 74 75 72 6e 69 6e 67 20 61 73 20 73  , returning as s
3012b 74 61 6e 64 61 72 64 20 43 20 69 64 69 6f 6d 20  tandard C idiom 
3012c 6f 66 20 3c 30 20 69 66 0a 2a 2a 20 6c 65 66 74  of <0 if.** left
3012d 3c 72 69 67 68 74 2c 20 3e 30 20 69 66 20 6c 65  <right, >0 if le
3012e 66 74 3e 72 69 67 68 74 2c 20 61 6e 64 20 30 20  ft>right, and 0 
3012f 69 66 20 6c 65 66 74 3d 3d 72 69 67 68 74 2e 20  if left==right. 
30130 20 22 45 6e 64 22 20 61 6c 77 61 79 73 0a 2a 2a   "End" always.**
30131 20 63 6f 6d 70 61 72 65 73 20 67 72 65 61 74 65   compares greate
30132 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
30133 20 70 6f 73 4c 69 73 74 43 6d 70 28 50 4c 52 65   posListCmp(PLRe
30134 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 50 4c 52  ader *pLeft, PLR
30135 65 61 64 65 72 20 2a 70 52 69 67 68 74 29 7b 0a  eader *pRight){.
30136 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d    assert( pLeft-
30137 3e 69 54 79 70 65 3d 3d 70 52 69 67 68 74 2d 3e  >iType==pRight->
30138 69 54 79 70 65 20 29 3b 0a 20 20 69 66 28 20 70  iType );.  if( p
30139 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f  Left->iType==DL_
3013a 44 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e 20  DOCIDS ) return 
3013b 30 3b 0a 0a 20 20 69 66 28 20 70 6c 72 41 74 45  0;..  if( plrAtE
3013c 6e 64 28 70 4c 65 66 74 29 20 29 20 72 65 74 75  nd(pLeft) ) retu
3013d 72 6e 20 70 6c 72 41 74 45 6e 64 28 70 52 69 67  rn plrAtEnd(pRig
3013e 68 74 29 20 3f 20 30 20 3a 20 31 3b 0a 20 20 69  ht) ? 0 : 1;.  i
3013f 66 28 20 70 6c 72 41 74 45 6e 64 28 70 52 69 67  f( plrAtEnd(pRig
30140 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  ht) ) return -1;
30141 0a 0a 20 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d  ..  if( plrColum
30142 6e 28 70 4c 65 66 74 29 3c 70 6c 72 43 6f 6c 75  n(pLeft)<plrColu
30143 6d 6e 28 70 52 69 67 68 74 29 20 29 20 72 65 74  mn(pRight) ) ret
30144 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 70 6c  urn -1;.  if( pl
30145 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3e 70  rColumn(pLeft)>p
30146 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29  lrColumn(pRight)
30147 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20   ) return 1;..  
30148 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28  if( plrPosition(
30149 70 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69 74 69  pLeft)<plrPositi
3014a 6f 6e 28 70 52 69 67 68 74 29 20 29 20 72 65 74  on(pRight) ) ret
3014b 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 70 6c  urn -1;.  if( pl
3014c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66 74 29  rPosition(pLeft)
3014d 3e 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52 69  >plrPosition(pRi
3014e 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b  ght) ) return 1;
3014f 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 69 54  .  if( pLeft->iT
30150 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e  ype==DL_POSITION
30151 53 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  S ) return 0;.. 
30152 20 69 66 28 20 70 6c 72 53 74 61 72 74 4f 66 66   if( plrStartOff
30153 73 65 74 28 70 4c 65 66 74 29 3c 70 6c 72 53 74  set(pLeft)<plrSt
30154 61 72 74 4f 66 66 73 65 74 28 70 52 69 67 68 74  artOffset(pRight
30155 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  ) ) return -1;. 
30156 20 69 66 28 20 70 6c 72 53 74 61 72 74 4f 66 66   if( plrStartOff
30157 73 65 74 28 70 4c 65 66 74 29 3e 70 6c 72 53 74  set(pLeft)>plrSt
30158 61 72 74 4f 66 66 73 65 74 28 70 52 69 67 68 74  artOffset(pRight
30159 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20  ) ) return 1;.. 
3015a 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66 73 65   if( plrEndOffse
3015b 74 28 70 4c 65 66 74 29 3c 70 6c 72 45 6e 64 4f  t(pLeft)<plrEndO
3015c 66 66 73 65 74 28 70 52 69 67 68 74 29 20 29 20  ffset(pRight) ) 
3015d 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28  return -1;.  if(
3015e 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 4c   plrEndOffset(pL
3015f 65 66 74 29 3e 70 6c 72 45 6e 64 4f 66 66 73 65  eft)>plrEndOffse
30160 74 28 70 52 69 67 68 74 29 20 29 20 72 65 74 75  t(pRight) ) retu
30161 72 6e 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20  rn 1;..  return 
30162 30 3b 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65 20 74  0;.}../* Write t
30163 68 65 20 75 6e 69 6f 6e 20 6f 66 20 70 6f 73 69  he union of posi
30164 74 69 6f 6e 20 6c 69 73 74 73 20 69 6e 20 70 4c  tion lists in pL
30165 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 20 74  eft and pRight t
30166 6f 20 70 4f 75 74 2e 0a 2a 2a 20 22 55 6e 69 6f  o pOut..** "Unio
30167 6e 22 20 69 6e 20 74 68 69 73 20 63 61 73 65 20  n" in this case 
30168 6d 65 61 6e 69 6e 67 20 22 41 6c 6c 20 75 6e 69  meaning "All uni
30169 71 75 65 20 70 6f 73 69 74 69 6f 6e 20 74 75 70  que position tup
3016a 6c 65 73 22 2e 20 20 53 68 6f 75 6c 64 0a 2a 2a  les".  Should.**
3016b 20 77 6f 72 6b 20 77 69 74 68 20 61 6e 79 20 64   work with any d
3016c 6f 63 6c 69 73 74 20 74 79 70 65 2c 20 74 68 6f  oclist type, tho
3016d 75 67 68 20 62 6f 74 68 20 69 6e 70 75 74 73 20  ugh both inputs 
3016e 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a 2a  and the output.*
3016f 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20  * should be the 
30170 73 61 6d 65 20 74 79 70 65 2e 0a 2a 2f 0a 73 74  same type..*/.st
30171 61 74 69 63 20 76 6f 69 64 20 70 6f 73 4c 69 73  atic void posLis
30172 74 55 6e 69 6f 6e 28 44 4c 52 65 61 64 65 72 20  tUnion(DLReader 
30173 2a 70 4c 65 66 74 2c 20 44 4c 52 65 61 64 65 72  *pLeft, DLReader
30174 20 2a 70 52 69 67 68 74 2c 20 44 4c 57 72 69 74   *pRight, DLWrit
30175 65 72 20 2a 70 4f 75 74 29 7b 0a 20 20 50 4c 52  er *pOut){.  PLR
30176 65 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68  eader left, righ
30177 74 3b 0a 20 20 50 4c 57 72 69 74 65 72 20 77 72  t;.  PLWriter wr
30178 69 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  iter;..  assert(
30179 20 64 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29   dlrDocid(pLeft)
3017a 3d 3d 64 6c 72 44 6f 63 69 64 28 70 52 69 67 68  ==dlrDocid(pRigh
3017b 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  t) );.  assert( 
3017c 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 52  pLeft->iType==pR
3017d 69 67 68 74 2d 3e 69 54 79 70 65 20 29 3b 0a 20  ight->iType );. 
3017e 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e   assert( pLeft->
3017f 69 54 79 70 65 3d 3d 70 4f 75 74 2d 3e 69 54 79  iType==pOut->iTy
30180 70 65 20 29 3b 0a 0a 20 20 70 6c 72 49 6e 69 74  pe );..  plrInit
30181 28 26 6c 65 66 74 2c 20 70 4c 65 66 74 29 3b 0a  (&left, pLeft);.
30182 20 20 70 6c 72 49 6e 69 74 28 26 72 69 67 68 74    plrInit(&right
30183 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 6c 77  , pRight);.  plw
30184 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 70 4f  Init(&writer, pO
30185 75 74 2c 20 64 6c 72 44 6f 63 69 64 28 70 4c 65  ut, dlrDocid(pLe
30186 66 74 29 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  ft));..  while( 
30187 21 70 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !plrAtEnd(&left)
30188 20 7c 7c 20 21 70 6c 72 41 74 45 6e 64 28 26 72   || !plrAtEnd(&r
30189 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 6e 74  ight) ){.    int
3018a 20 63 20 3d 20 70 6f 73 4c 69 73 74 43 6d 70 28   c = posListCmp(
3018b 26 6c 65 66 74 2c 20 26 72 69 67 68 74 29 3b 0a  &left, &right);.
3018c 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20      if( c<0 ){. 
3018d 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72       plwCopy(&wr
3018e 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20  iter, &left);.  
3018f 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c 65 66      plrStep(&lef
30190 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  t);.    }else if
30191 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 70  ( c>0 ){.      p
30192 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
30193 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 70  &right);.      p
30194 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
30195 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
30196 20 70 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72   plwCopy(&writer
30197 2c 20 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20  , &left);.      
30198 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a  plrStep(&left);.
30199 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72        plrStep(&r
3019a 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ight);.    }.  }
3019b 0a 0a 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65  ..  plwTerminate
3019c 28 26 77 72 69 74 65 72 29 3b 0a 20 20 70 6c 77  (&writer);.  plw
3019d 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
3019e 3b 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26  ;.  plrDestroy(&
3019f 6c 65 66 74 29 3b 0a 20 20 70 6c 72 44 65 73 74  left);.  plrDest
301a0 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 7d 0a 0a  roy(&right);.}..
301a1 2f 2a 20 57 72 69 74 65 20 74 68 65 20 75 6e 69  /* Write the uni
301a2 6f 6e 20 6f 66 20 64 6f 63 6c 69 73 74 73 20 69  on of doclists i
301a3 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  n pLeft and pRig
301a4 68 74 20 74 6f 20 70 4f 75 74 2e 20 20 46 6f 72  ht to pOut.  For
301a5 0a 2a 2a 20 64 6f 63 69 64 73 20 69 6e 20 63 6f  .** docids in co
301a6 6d 6d 6f 6e 20 62 65 74 77 65 65 6e 20 74 68 65  mmon between the
301a7 20 69 6e 70 75 74 73 2c 20 74 68 65 20 75 6e 69   inputs, the uni
301a8 6f 6e 20 6f 66 20 74 68 65 20 70 6f 73 69 74 69  on of the positi
301a9 6f 6e 0a 2a 2a 20 6c 69 73 74 73 20 69 73 20 77  on.** lists is w
301aa 72 69 74 74 65 6e 2e 20 20 49 6e 70 75 74 73 20  ritten.  Inputs 
301ab 61 6e 64 20 6f 75 74 70 75 74 73 20 61 72 65 20  and outputs are 
301ac 61 6c 77 61 79 73 20 74 79 70 65 20 44 4c 5f 44  always type DL_D
301ad 45 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69  EFAULT..*/.stati
301ae 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 55 6e  c void docListUn
301af 69 6f 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  ion(.  const cha
301b0 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c  r *pLeft, int nL
301b1 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  eft,.  const cha
301b2 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e  r *pRight, int n
301b3 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75 66  Right,.  DataBuf
301b4 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20 2f  fer *pOut      /
301b5 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d 62  * Write the comb
301b6 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65 72  ined doclist her
301b7 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64  e */.){.  DLRead
301b8 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a  er left, right;.
301b9 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74 65    DLWriter write
301ba 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d  r;..  if( nLeft=
301bb 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 52  =0 ){.    if( nR
301bc 69 67 68 74 21 3d 30 29 20 64 61 74 61 42 75 66  ight!=0) dataBuf
301bd 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20  ferAppend(pOut, 
301be 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
301bf 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
301c0 0a 20 20 69 66 28 20 6e 52 69 67 68 74 3d 3d 30  .  if( nRight==0
301c1 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66   ){.    dataBuff
301c2 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70  erAppend(pOut, p
301c3 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20  Left, nLeft);.  
301c4 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
301c5 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20   dlrInit(&left, 
301c6 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 4c 65 66  DL_DEFAULT, pLef
301c7 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72  t, nLeft);.  dlr
301c8 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f  Init(&right, DL_
301c9 44 45 46 41 55 4c 54 2c 20 70 52 69 67 68 74 2c  DEFAULT, pRight,
301ca 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77 49   nRight);.  dlwI
301cb 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c 5f  nit(&writer, DL_
301cc 44 45 46 41 55 4c 54 2c 20 70 4f 75 74 29 3b 0a  DEFAULT, pOut);.
301cd 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74  .  while( !dlrAt
301ce 45 6e 64 28 26 6c 65 66 74 29 20 7c 7c 20 21 64  End(&left) || !d
301cf 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
301d0 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74  ){.    if( dlrAt
301d1 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  End(&right) ){. 
301d2 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77 72       dlwCopy(&wr
301d3 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20  iter, &left);.  
301d4 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66      dlrStep(&lef
301d5 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  t);.    }else if
301d6 28 20 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74  ( dlrAtEnd(&left
301d7 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f  ) ){.      dlwCo
301d8 70 79 28 26 77 72 69 74 65 72 2c 20 26 72 69 67  py(&writer, &rig
301d9 68 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  ht);.      dlrSt
301da 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
301db 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63  }else if( dlrDoc
301dc 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63  id(&left)<dlrDoc
301dd 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20  id(&right) ){.  
301de 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77 72 69      dlwCopy(&wri
301df 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20 20  ter, &left);.   
301e0 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74     dlrStep(&left
301e1 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
301e2 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
301e3 3e 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74  >dlrDocid(&right
301e4 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f  ) ){.      dlwCo
301e5 70 79 28 26 77 72 69 74 65 72 2c 20 26 72 69 67  py(&writer, &rig
301e6 68 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  ht);.      dlrSt
301e7 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
301e8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 6f 73  }else{.      pos
301e9 4c 69 73 74 55 6e 69 6f 6e 28 26 6c 65 66 74 2c  ListUnion(&left,
301ea 20 26 72 69 67 68 74 2c 20 26 77 72 69 74 65 72   &right, &writer
301eb 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
301ec 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64  (&left);.      d
301ed 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
301ee 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72      }.  }..  dlr
301ef 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a  Destroy(&left);.
301f0 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69    dlrDestroy(&ri
301f1 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72  ght);.  dlwDestr
301f2 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a  oy(&writer);.}..
301f3 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /* .** This func
301f4 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20  tion is used as 
301f5 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70 6c  part of the impl
301f6 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 68  ementation of ph
301f7 72 61 73 65 20 61 6e 64 0a 2a 2a 20 4e 45 41 52  rase and.** NEAR
301f8 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a   matching..**.**
301f9 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68   pLeft and pRigh
301fa 74 20 61 72 65 20 44 4c 52 65 61 64 65 72 73 20  t are DLReaders 
301fb 70 6f 73 69 74 69 6f 6e 65 64 20 74 6f 20 74 68  positioned to th
301fc 65 20 73 61 6d 65 20 64 6f 63 69 64 20 69 6e 0a  e same docid in.
301fd 2a 2a 20 6c 69 73 74 73 20 6f 66 20 74 79 70 65  ** lists of type
301fe 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 2e 20 54 68   DL_POSITION. Th
301ff 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74  is function writ
30200 65 73 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74  es an entry to t
30201 68 65 0a 2a 2a 20 44 4c 57 72 69 74 65 72 20 70  he.** DLWriter p
30202 4f 75 74 20 66 6f 72 20 65 61 63 68 20 70 6f 73  Out for each pos
30203 69 74 69 6f 6e 20 69 6e 20 70 52 69 67 68 74 20  ition in pRight 
30204 74 68 61 74 20 69 73 20 6c 65 73 73 20 74 68 61  that is less tha
30205 6e 0a 2a 2a 20 28 6e 4e 65 61 72 2b 31 29 20 67  n.** (nNear+1) g
30206 72 65 61 74 65 72 20 28 62 75 74 20 6e 6f 74 20  reater (but not 
30207 65 71 75 61 6c 20 74 6f 20 6f 72 20 73 6d 61 6c  equal to or smal
30208 6c 65 72 29 20 74 68 61 6e 20 61 20 70 6f 73 69  ler) than a posi
30209 74 69 6f 6e 20 0a 2a 2a 20 69 6e 20 70 4c 65 66  tion .** in pLef
3020a 74 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  t. For example, 
3020b 69 66 20 6e 4e 65 61 72 20 69 73 20 30 2c 20 61  if nNear is 0, a
3020c 6e 64 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73  nd the positions
3020d 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 62 79   contained.** by
3020e 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68   pLeft and pRigh
3020f 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  t are:.**.**    
30210 70 4c 65 66 74 3a 20 20 35 20 31 30 20 31 35 20  pLeft:  5 10 15 
30211 32 30 0a 2a 2a 20 20 20 20 70 52 69 67 68 74 3a  20.**    pRight:
30212 20 36 20 20 39 20 31 37 20 32 31 0a 2a 2a 0a 2a   6  9 17 21.**.*
30213 2a 20 74 68 65 6e 20 74 68 65 20 64 6f 63 69 64  * then the docid
30214 20 69 73 20 61 64 64 65 64 20 74 6f 20 70 4f 75   is added to pOu
30215 74 2e 20 49 66 20 70 4f 75 74 20 69 73 20 6f 66  t. If pOut is of
30216 20 74 79 70 65 20 44 4c 5f 50 4f 53 49 54 49 4f   type DL_POSITIO
30217 4e 53 2c 0a 2a 2a 20 74 68 65 6e 20 61 20 70 6f  NS,.** then a po
30218 73 69 74 69 6f 6e 69 64 73 20 22 36 22 20 61 6e  sitionids "6" an
30219 64 20 22 32 31 22 20 61 72 65 20 61 6c 73 6f 20  d "21" are also 
3021a 61 64 64 65 64 20 74 6f 20 70 4f 75 74 2e 0a 2a  added to pOut..*
3021b 2a 0a 2a 2a 20 49 66 20 62 6f 6f 6c 65 61 6e 20  *.** If boolean 
3021c 61 72 67 75 6d 65 6e 74 20 69 73 53 61 76 65 4c  argument isSaveL
3021d 65 66 74 20 69 73 20 74 72 75 65 2c 20 74 68 65  eft is true, the
3021e 6e 20 70 6f 73 69 74 69 6f 6e 69 64 73 20 61 72  n positionids ar
3021f 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72 6f 6d  e copied.** from
30220 20 70 4c 65 66 74 20 69 6e 73 74 65 61 64 20 6f   pLeft instead o
30221 66 20 70 52 69 67 68 74 2e 20 49 6e 20 74 68 65  f pRight. In the
30222 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20   example above, 
30223 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 22 35  the positions "5
30224 22 0a 2a 2a 20 61 6e 64 20 22 32 30 22 20 77 6f  ".** and "20" wo
30225 75 6c 64 20 62 65 20 61 64 64 65 64 20 69 6e 73  uld be added ins
30226 74 65 61 64 20 6f 66 20 22 36 22 20 61 6e 64 20  tead of "6" and 
30227 22 32 31 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  "21"..*/.static 
30228 76 6f 69 64 20 70 6f 73 4c 69 73 74 50 68 72 61  void posListPhra
30229 73 65 4d 65 72 67 65 28 0a 20 20 44 4c 52 65 61  seMerge(.  DLRea
3022a 64 65 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20 44  der *pLeft, .  D
3022b 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68 74 2c  LReader *pRight,
3022c 0a 20 20 69 6e 74 20 6e 4e 65 61 72 2c 0a 20 20  .  int nNear,.  
3022d 69 6e 74 20 69 73 53 61 76 65 4c 65 66 74 2c 0a  int isSaveLeft,.
3022e 20 20 44 4c 57 72 69 74 65 72 20 2a 70 4f 75 74    DLWriter *pOut
3022f 0a 29 7b 0a 20 20 50 4c 52 65 61 64 65 72 20 6c  .){.  PLReader l
30230 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 50 4c  eft, right;.  PL
30231 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 20  Writer writer;. 
30232 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a   int match = 0;.
30233 0a 20 20 61 73 73 65 72 74 28 20 64 6c 72 44 6f  .  assert( dlrDo
30234 63 69 64 28 70 4c 65 66 74 29 3d 3d 64 6c 72 44  cid(pLeft)==dlrD
30235 6f 63 69 64 28 70 52 69 67 68 74 29 20 29 3b 0a  ocid(pRight) );.
30236 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e    assert( pOut->
30237 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49 54 49  iType!=DL_POSITI
30238 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b 0a 0a  ONS_OFFSETS );..
30239 20 20 70 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c    plrInit(&left,
3023a 20 70 4c 65 66 74 29 3b 0a 20 20 70 6c 72 49 6e   pLeft);.  plrIn
3023b 69 74 28 26 72 69 67 68 74 2c 20 70 52 69 67 68  it(&right, pRigh
3023c 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 70  t);..  while( !p
3023d 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 26  lrAtEnd(&left) &
3023e 26 20 21 70 6c 72 41 74 45 6e 64 28 26 72 69 67  & !plrAtEnd(&rig
3023f 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ht) ){.    if( p
30240 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 3c  lrColumn(&left)<
30241 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74  plrColumn(&right
30242 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74  ) ){.      plrSt
30243 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d  ep(&left);.    }
30244 65 6c 73 65 20 69 66 28 20 70 6c 72 43 6f 6c 75  else if( plrColu
30245 6d 6e 28 26 6c 65 66 74 29 3e 70 6c 72 43 6f 6c  mn(&left)>plrCol
30246 75 6d 6e 28 26 72 69 67 68 74 29 20 29 7b 0a 20  umn(&right) ){. 
30247 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72 69       plrStep(&ri
30248 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ght);.    }else 
30249 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28  if( plrPosition(
3024a 26 6c 65 66 74 29 3e 3d 70 6c 72 50 6f 73 69 74  &left)>=plrPosit
3024b 69 6f 6e 28 26 72 69 67 68 74 29 20 29 7b 0a 20  ion(&right) ){. 
3024c 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72 69       plrStep(&ri
3024d 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ght);.    }else{
3024e 0a 20 20 20 20 20 20 69 66 28 20 28 70 6c 72 50  .      if( (plrP
3024f 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74 29 2d  osition(&right)-
30250 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65 66  plrPosition(&lef
30251 74 29 29 3c 3d 28 6e 4e 65 61 72 2b 31 29 20 29  t))<=(nNear+1) )
30252 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6d  {.        if( !m
30253 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20  atch ){.        
30254 20 20 70 6c 77 49 6e 69 74 28 26 77 72 69 74 65    plwInit(&write
30255 72 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f 63 69  r, pOut, dlrDoci
30256 64 28 70 4c 65 66 74 29 29 3b 0a 20 20 20 20 20  d(pLeft));.     
30257 20 20 20 20 20 6d 61 74 63 68 20 3d 20 31 3b 0a       match = 1;.
30258 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
30259 20 20 69 66 28 20 21 69 73 53 61 76 65 4c 65 66    if( !isSaveLef
3025a 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  t ){.          p
3025b 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20 70  lwAdd(&writer, p
3025c 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74 29  lrColumn(&right)
3025d 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 72  , plrPosition(&r
3025e 69 67 68 74 29 2c 20 30 2c 20 30 29 3b 0a 20 20  ight), 0, 0);.  
3025f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
30260 20 20 20 20 20 20 20 70 6c 77 41 64 64 28 26 77         plwAdd(&w
30261 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e  riter, plrColumn
30262 28 26 6c 65 66 74 29 2c 20 70 6c 72 50 6f 73 69  (&left), plrPosi
30263 74 69 6f 6e 28 26 6c 65 66 74 29 2c 20 30 2c 20  tion(&left), 0, 
30264 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0);.        }.  
30265 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72        plrStep(&r
30266 69 67 68 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ight);.      }el
30267 73 65 7b 0a 20 20 20 20 20 20 20 20 70 6c 72 53  se{.        plrS
30268 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  tep(&left);.    
30269 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
3026a 20 69 66 28 20 6d 61 74 63 68 20 29 7b 0a 20 20   if( match ){.  
3026b 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26    plwTerminate(&
3026c 77 72 69 74 65 72 29 3b 0a 20 20 20 20 70 6c 77  writer);.    plw
3026d 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
3026e 3b 0a 20 20 7d 0a 0a 20 20 70 6c 72 44 65 73 74  ;.  }..  plrDest
3026f 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 70 6c  roy(&left);.  pl
30270 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29  rDestroy(&right)
30271 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61  ;.}../*.** Compa
30272 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 70 6f  re the values po
30273 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20  inted to by the 
30274 50 4c 52 65 61 64 65 72 73 20 70 61 73 73 65 64  PLReaders passed
30275 20 61 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 0a   as arguments. .
30276 2a 2a 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20  ** Return -1 if 
30277 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65  the value pointe
30278 64 20 74 6f 20 62 79 20 70 4c 65 66 74 20 69 73  d to by pLeft is
30279 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 73   considered less
3027a 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 76 61 6c   than.** the val
3027b 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ue pointed to by
3027c 20 70 52 69 67 68 74 2c 20 2b 31 20 69 66 20 69   pRight, +1 if i
3027d 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  t is considered 
3027e 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20  greater.** than 
3027f 69 74 2c 20 6f 72 20 30 20 69 66 20 69 74 20 69  it, or 0 if it i
30280 73 20 65 71 75 61 6c 2e 20 69 2e 65 2e 0a 2a 2a  s equal. i.e..**
30281 0a 2a 2a 20 20 20 20 20 28 2a 70 4c 65 66 74 20  .**     (*pLeft 
30282 2d 20 2a 70 52 69 67 68 74 29 0a 2a 2a 0a 2a 2a  - *pRight).**.**
30283 20 41 20 50 4c 52 65 61 64 65 72 20 74 68 61 74   A PLReader that
30284 20 69 73 20 69 6e 20 74 68 65 20 45 4f 46 20 63   is in the EOF c
30285 6f 6e 64 69 74 69 6f 6e 20 69 73 20 63 6f 6e 73  ondition is cons
30286 69 64 65 72 65 64 20 67 72 65 61 74 65 72 20 74  idered greater t
30287 68 61 6e 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 72  han.** any other
30288 2e 20 49 66 20 6e 65 69 74 68 65 72 20 61 72 67  . If neither arg
30289 75 6d 65 6e 74 20 69 73 20 69 6e 20 45 4f 46 20  ument is in EOF 
3028a 73 74 61 74 65 2c 20 74 68 65 20 72 65 74 75 72  state, the retur
3028b 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 70 6c  n value of.** pl
3028c 72 43 6f 6c 75 6d 6e 28 29 20 69 73 20 75 73 65  rColumn() is use
3028d 64 2e 20 49 66 20 74 68 65 20 70 6c 72 43 6f 6c  d. If the plrCol
3028e 75 6d 6e 28 29 20 76 61 6c 75 65 73 20 61 72 65  umn() values are
3028f 20 65 71 75 61 6c 2c 20 74 68 65 0a 2a 2a 20 63   equal, the.** c
30290 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6f 6e 20  omparison is on 
30291 74 68 65 20 62 61 73 69 73 20 6f 66 20 70 6c 72  the basis of plr
30292 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73  Position()..*/.s
30293 74 61 74 69 63 20 69 6e 74 20 70 6c 72 43 6f 6d  tatic int plrCom
30294 70 61 72 65 28 50 4c 52 65 61 64 65 72 20 2a 70  pare(PLReader *p
30295 4c 65 66 74 2c 20 50 4c 52 65 61 64 65 72 20 2a  Left, PLReader *
30296 70 52 69 67 68 74 29 7b 0a 20 20 61 73 73 65 72  pRight){.  asser
30297 74 28 21 70 6c 72 41 74 45 6e 64 28 70 4c 65 66  t(!plrAtEnd(pLef
30298 74 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e 64 28  t) || !plrAtEnd(
30299 70 52 69 67 68 74 29 29 3b 0a 0a 20 20 69 66 28  pRight));..  if(
3029a 20 70 6c 72 41 74 45 6e 64 28 70 52 69 67 68 74   plrAtEnd(pRight
3029b 29 20 7c 7c 20 70 6c 72 41 74 45 6e 64 28 70 4c  ) || plrAtEnd(pL
3029c 65 66 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  eft) ){.    retu
3029d 72 6e 20 28 70 6c 72 41 74 45 6e 64 28 70 52 69  rn (plrAtEnd(pRi
3029e 67 68 74 29 20 3f 20 2d 31 20 3a 20 31 29 3b 0a  ght) ? -1 : 1);.
3029f 20 20 7d 0a 20 20 69 66 28 20 70 6c 72 43 6f 6c    }.  if( plrCol
302a0 75 6d 6e 28 70 4c 65 66 74 29 21 3d 70 6c 72 43  umn(pLeft)!=plrC
302a1 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29 20 29 7b  olumn(pRight) ){
302a2 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28 70 6c  .    return ((pl
302a3 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3c 70  rColumn(pLeft)<p
302a4 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29  lrColumn(pRight)
302a5 29 20 3f 20 2d 31 20 3a 20 31 29 3b 0a 20 20 7d  ) ? -1 : 1);.  }
302a6 0a 20 20 69 66 28 20 70 6c 72 50 6f 73 69 74 69  .  if( plrPositi
302a7 6f 6e 28 70 4c 65 66 74 29 21 3d 70 6c 72 50 6f  on(pLeft)!=plrPo
302a8 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29 20 29  sition(pRight) )
302a9 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28 70  {.    return ((p
302aa 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66 74  lrPosition(pLeft
302ab 29 3c 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52  )<plrPosition(pR
302ac 69 67 68 74 29 29 20 3f 20 2d 31 20 3a 20 31 29  ight)) ? -1 : 1)
302ad 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
302ae 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20  ;.}../* We have 
302af 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 77 69 74  two doclists wit
302b0 68 20 70 6f 73 69 74 69 6f 6e 73 3a 20 20 70 4c  h positions:  pL
302b1 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e 20  eft and pRight. 
302b2 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20  Depending.** on 
302b3 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
302b4 20 6e 4e 65 61 72 20 70 61 72 61 6d 65 74 65 72   nNear parameter
302b5 2c 20 70 65 72 66 6f 72 6d 20 65 69 74 68 65 72  , perform either
302b6 20 61 20 70 68 72 61 73 65 0a 2a 2a 20 69 6e 74   a phrase.** int
302b7 65 72 73 65 63 74 69 6f 6e 20 28 69 66 20 6e 4e  ersection (if nN
302b8 65 61 72 3d 3d 30 29 20 6f 72 20 61 20 4e 45 41  ear==0) or a NEA
302b9 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 28  R intersection (
302ba 69 66 20 6e 4e 65 61 72 3e 30 29 0a 2a 2a 20 61  if nNear>0).** a
302bb 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 73  nd write the res
302bc 75 6c 74 73 20 69 6e 74 6f 20 70 4f 75 74 2e 0a  ults into pOut..
302bd 2a 2a 0a 2a 2a 20 41 20 70 68 72 61 73 65 20 69  **.** A phrase i
302be 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d 65 61 6e  ntersection mean
302bf 73 20 74 68 61 74 20 74 77 6f 20 64 6f 63 75 6d  s that two docum
302c0 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74 63 68 0a  ents only match.
302c1 2a 2a 20 69 66 20 70 4c 65 66 74 2e 69 50 6f 73  ** if pLeft.iPos
302c2 2b 31 3d 3d 70 52 69 67 68 74 2e 69 50 6f 73 2e  +1==pRight.iPos.
302c3 0a 2a 2a 0a 2a 2a 20 41 20 4e 45 41 52 20 69 6e  .**.** A NEAR in
302c4 74 65 72 73 65 63 74 69 6f 6e 20 6d 65 61 6e 73  tersection means
302c5 20 74 68 61 74 20 74 77 6f 20 64 6f 63 75 6d 65   that two docume
302c6 6e 74 73 20 6f 6e 6c 79 20 6d 61 74 63 68 20 69  nts only match i
302c7 66 20 0a 2a 2a 20 28 61 62 73 28 70 4c 65 66 74  f .** (abs(pLeft
302c8 2e 69 50 6f 73 2d 70 52 69 67 68 74 2e 69 50 6f  .iPos-pRight.iPo
302c9 73 29 3c 6e 4e 65 61 72 29 2e 0a 2a 2a 0a 2a 2a  s)<nNear)..**.**
302ca 20 49 66 20 61 20 4e 45 41 52 20 69 6e 74 65 72   If a NEAR inter
302cb 73 65 63 74 69 6f 6e 20 69 73 20 72 65 71 75 65  section is reque
302cc 73 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6e  sted, then the n
302cd 50 68 72 61 73 65 20 61 72 67 75 6d 65 6e 74 20  Phrase argument 
302ce 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 70 61 73  should.** be pas
302cf 73 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  sed the number o
302d0 66 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20  f tokens in the 
302d1 74 77 6f 20 6f 70 65 72 61 6e 64 73 20 74 6f 20  two operands to 
302d2 74 68 65 20 4e 45 41 52 20 6f 70 65 72 61 74 6f  the NEAR operato
302d3 72 0a 2a 2a 20 63 6f 6d 62 69 6e 65 64 2e 20 46  r.** combined. F
302d4 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a  or example:.**.*
302d5 2a 20 20 20 20 20 20 20 51 75 65 72 79 20 73 79  *       Query sy
302d6 6e 74 61 78 20 20 20 20 20 20 20 20 20 20 20 20  ntax            
302d7 20 20 20 6e 50 68 72 61 73 65 0a 2a 2a 20 20 20     nPhrase.**   
302d8 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
302d9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
302da 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20  -------.**      
302db 20 22 41 20 42 20 43 22 20 4e 45 41 52 20 22 44   "A B C" NEAR "D
302dc 20 45 22 20 20 20 20 20 20 20 20 20 35 0a 2a 2a   E"         5.**
302dd 20 20 20 20 20 20 20 41 20 4e 45 41 52 20 42 20         A NEAR B 
302de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
302df 20 20 32 0a 2a 2a 0a 2a 2a 20 69 54 79 70 65 20    2.**.** iType 
302e0 63 6f 6e 74 72 6f 6c 73 20 74 68 65 20 74 79 70  controls the typ
302e1 65 20 6f 66 20 64 61 74 61 20 77 72 69 74 74 65  e of data writte
302e2 6e 20 74 6f 20 70 4f 75 74 2e 20 20 49 66 20 69  n to pOut.  If i
302e3 54 79 70 65 20 69 73 0a 2a 2a 20 44 4c 5f 50 4f  Type is.** DL_PO
302e4 53 49 54 49 4f 4e 53 2c 20 74 68 65 20 70 6f 73  SITIONS, the pos
302e5 69 74 69 6f 6e 73 20 61 72 65 20 74 68 6f 73 65  itions are those
302e6 20 66 72 6f 6d 20 70 52 69 67 68 74 2e 0a 2a 2f   from pRight..*/
302e7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63  .static void doc
302e8 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  ListPhraseMerge(
302e9 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
302ea 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c  Left, int nLeft,
302eb 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
302ec 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68  Right, int nRigh
302ed 74 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72 2c 20  t,.  int nNear, 
302ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20             /* 0 
302ef 66 6f 72 20 61 20 70 68 72 61 73 65 20 6d 65 72  for a phrase mer
302f0 67 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72  ge, non-zero for
302f1 20 61 20 4e 45 41 52 20 6d 65 72 67 65 20 2a 2f   a NEAR merge */
302f2 0a 20 20 69 6e 74 20 6e 50 68 72 61 73 65 2c 20  .  int nPhrase, 
302f3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
302f4 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
302f5 6c 65 66 74 2b 72 69 67 68 74 20 6f 70 65 72 61  left+right opera
302f6 6e 64 73 20 74 6f 20 4e 45 41 52 20 2a 2f 0a 20  nds to NEAR */. 
302f7 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79   DocListType iTy
302f8 70 65 2c 20 20 20 20 2f 2a 20 54 79 70 65 20 6f  pe,    /* Type o
302f9 66 20 64 6f 63 6c 69 73 74 20 74 6f 20 77 72 69  f doclist to wri
302fa 74 65 20 74 6f 20 70 4f 75 74 20 2a 2f 0a 20 20  te to pOut */.  
302fb 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75 74  DataBuffer *pOut
302fc 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
302fd 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c  he combined docl
302fe 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ist here */.){. 
302ff 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20   DLReader left, 
30300 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74 65  right;.  DLWrite
30301 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66 28  r writer;..  if(
30302 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c 20 6e 52 69   nLeft==0 || nRi
30303 67 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ght==0 ) return;
30304 0a 0a 20 20 61 73 73 65 72 74 28 20 69 54 79 70  ..  assert( iTyp
30305 65 21 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f  e!=DL_POSITIONS_
30306 4f 46 46 53 45 54 53 20 29 3b 0a 0a 20 20 64 6c  OFFSETS );..  dl
30307 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c 5f  rInit(&left, DL_
30308 50 4f 53 49 54 49 4f 4e 53 2c 20 70 4c 65 66 74  POSITIONS, pLeft
30309 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72 49  , nLeft);.  dlrI
3030a 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f 50  nit(&right, DL_P
3030b 4f 53 49 54 49 4f 4e 53 2c 20 70 52 69 67 68 74  OSITIONS, pRight
3030c 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77  , nRight);.  dlw
3030d 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 69 54  Init(&writer, iT
3030e 79 70 65 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77  ype, pOut);..  w
3030f 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28  hile( !dlrAtEnd(
30310 26 6c 65 66 74 29 20 26 26 20 21 64 6c 72 41 74  &left) && !dlrAt
30311 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  End(&right) ){. 
30312 20 20 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28     if( dlrDocid(
30313 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64 28  &left)<dlrDocid(
30314 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
30315 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b   dlrStep(&left);
30316 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64  .    }else if( d
30317 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 3c  lrDocid(&right)<
30318 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 20  dlrDocid(&left) 
30319 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  ){.      dlrStep
3031a 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
3031b 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  lse{.      if( n
3031c 4e 65 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Near==0 ){.     
3031d 20 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73 65     posListPhrase
3031e 4d 65 72 67 65 28 26 6c 65 66 74 2c 20 26 72 69  Merge(&left, &ri
3031f 67 68 74 2c 20 30 2c 20 30 2c 20 26 77 72 69 74  ght, 0, 0, &writ
30320 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  er);.      }else
30321 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
30322 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 77 68  s case occurs wh
30323 65 6e 20 74 77 6f 20 74 65 72 6d 73 20 28 73 69  en two terms (si
30324 6d 70 6c 65 20 74 65 72 6d 73 20 6f 72 20 70 68  mple terms or ph
30325 72 61 73 65 73 29 20 61 72 65 0a 20 20 20 20 20  rases) are.     
30326 20 20 20 20 2a 20 63 6f 6e 6e 65 63 74 65 64 20      * connected 
30327 62 79 20 61 20 4e 45 41 52 20 6f 70 65 72 61 74  by a NEAR operat
30328 6f 72 2c 20 73 70 61 6e 20 28 6e 4e 65 61 72 2b  or, span (nNear+
30329 31 29 2e 20 69 2e 65 2e 0a 20 20 20 20 20 20 20  1). i.e..       
3032a 20 20 2a 0a 20 20 20 20 20 20 20 20 20 2a 20 20    *.         *  
3032b 20 20 20 27 22 74 65 72 72 69 62 6c 65 20 63 6f     '"terrible co
3032c 6d 70 61 6e 79 22 20 4e 45 41 52 20 77 69 64 67  mpany" NEAR widg
3032d 65 74 27 0a 20 20 20 20 20 20 20 20 20 2a 2f 0a  et'.         */.
3032e 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66          DataBuff
3032f 65 72 20 6f 6e 65 20 3d 20 7b 30 2c 20 30 2c 20  er one = {0, 0, 
30330 30 7d 3b 0a 20 20 20 20 20 20 20 20 44 61 74 61  0};.        Data
30331 42 75 66 66 65 72 20 74 77 6f 20 3d 20 7b 30 2c  Buffer two = {0,
30332 20 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20 20 20   0, 0};..       
30333 20 44 4c 57 72 69 74 65 72 20 64 6c 77 72 69 74   DLWriter dlwrit
30334 65 72 32 3b 0a 20 20 20 20 20 20 20 20 44 4c 52  er2;.        DLR
30335 65 61 64 65 72 20 64 72 31 20 3d 20 7b 30 2c 20  eader dr1 = {0, 
30336 30 2c 20 30 2c 20 30 2c 20 30 7d 3b 20 0a 20 20  0, 0, 0, 0}; .  
30337 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20 64        DLReader d
30338 72 32 20 3d 20 7b 30 2c 20 30 2c 20 30 2c 20 30  r2 = {0, 0, 0, 0
30339 2c 20 30 7d 3b 0a 0a 20 20 20 20 20 20 20 20 64  , 0};..        d
3033a 6c 77 49 6e 69 74 28 26 64 6c 77 72 69 74 65 72  lwInit(&dlwriter
3033b 32 2c 20 69 54 79 70 65 2c 20 26 6f 6e 65 29 3b  2, iType, &one);
3033c 0a 20 20 20 20 20 20 20 20 70 6f 73 4c 69 73 74  .        posList
3033d 50 68 72 61 73 65 4d 65 72 67 65 28 26 72 69 67  PhraseMerge(&rig
3033e 68 74 2c 20 26 6c 65 66 74 2c 20 6e 4e 65 61 72  ht, &left, nNear
3033f 2d 33 2b 6e 50 68 72 61 73 65 2c 20 31 2c 20 26  -3+nPhrase, 1, &
30340 64 6c 77 72 69 74 65 72 32 29 3b 0a 20 20 20 20  dlwriter2);.    
30341 20 20 20 20 64 6c 77 49 6e 69 74 28 26 64 6c 77      dlwInit(&dlw
30342 72 69 74 65 72 32 2c 20 69 54 79 70 65 2c 20 26  riter2, iType, &
30343 74 77 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 6f  two);.        po
30344 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65  sListPhraseMerge
30345 28 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20  (&left, &right, 
30346 6e 4e 65 61 72 2d 31 2c 20 30 2c 20 26 64 6c 77  nNear-1, 0, &dlw
30347 72 69 74 65 72 32 29 3b 0a 0a 20 20 20 20 20 20  riter2);..      
30348 20 20 69 66 28 20 6f 6e 65 2e 6e 44 61 74 61 29    if( one.nData)
30349 20 64 6c 72 49 6e 69 74 28 26 64 72 31 2c 20 69   dlrInit(&dr1, i
3034a 54 79 70 65 2c 20 6f 6e 65 2e 70 44 61 74 61 2c  Type, one.pData,
3034b 20 6f 6e 65 2e 6e 44 61 74 61 29 3b 0a 20 20 20   one.nData);.   
3034c 20 20 20 20 20 69 66 28 20 74 77 6f 2e 6e 44 61       if( two.nDa
3034d 74 61 29 20 64 6c 72 49 6e 69 74 28 26 64 72 32  ta) dlrInit(&dr2
3034e 2c 20 69 54 79 70 65 2c 20 74 77 6f 2e 70 44 61  , iType, two.pDa
3034f 74 61 2c 20 74 77 6f 2e 6e 44 61 74 61 29 3b 0a  ta, two.nData);.
30350 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 64 6c  .        if( !dl
30351 72 41 74 45 6e 64 28 26 64 72 31 29 20 7c 7c 20  rAtEnd(&dr1) || 
30352 21 64 6c 72 41 74 45 6e 64 28 26 64 72 32 29 20  !dlrAtEnd(&dr2) 
30353 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 4c 52  ){.          PLR
30354 65 61 64 65 72 20 70 72 31 20 3d 20 7b 30 7d 3b  eader pr1 = {0};
30355 0a 20 20 20 20 20 20 20 20 20 20 50 4c 52 65 61  .          PLRea
30356 64 65 72 20 70 72 32 20 3d 20 7b 30 7d 3b 0a 0a  der pr2 = {0};..
30357 20 20 20 20 20 20 20 20 20 20 50 4c 57 72 69 74            PLWrit
30358 65 72 20 70 6c 77 72 69 74 65 72 3b 0a 20 20 20  er plwriter;.   
30359 20 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28 26         plwInit(&
3035a 70 6c 77 72 69 74 65 72 2c 20 26 77 72 69 74 65  plwriter, &write
3035b 72 2c 20 64 6c 72 44 6f 63 69 64 28 64 6c 72 41  r, dlrDocid(dlrA
3035c 74 45 6e 64 28 26 64 72 31 29 3f 26 64 72 32 3a  tEnd(&dr1)?&dr2:
3035d 26 64 72 31 29 29 3b 0a 0a 20 20 20 20 20 20 20  &dr1));..       
3035e 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44 61 74 61     if( one.nData
3035f 20 29 20 70 6c 72 49 6e 69 74 28 26 70 72 31 2c   ) plrInit(&pr1,
30360 20 26 64 72 31 29 3b 0a 20 20 20 20 20 20 20 20   &dr1);.        
30361 20 20 69 66 28 20 74 77 6f 2e 6e 44 61 74 61 20    if( two.nData 
30362 29 20 70 6c 72 49 6e 69 74 28 26 70 72 32 2c 20  ) plrInit(&pr2, 
30363 26 64 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20  &dr2);.         
30364 20 77 68 69 6c 65 28 20 21 70 6c 72 41 74 45 6e   while( !plrAtEn
30365 64 28 26 70 72 31 29 20 7c 7c 20 21 70 6c 72 41  d(&pr1) || !plrA
30366 74 45 6e 64 28 26 70 72 32 29 20 29 7b 0a 20 20  tEnd(&pr2) ){.  
30367 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 43            int iC
30368 6f 6d 70 61 72 65 20 3d 20 70 6c 72 43 6f 6d 70  ompare = plrComp
30369 61 72 65 28 26 70 72 31 2c 20 26 70 72 32 29 3b  are(&pr1, &pr2);
3036a 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 77 69  .            swi
3036b 74 63 68 28 20 69 43 6f 6d 70 61 72 65 20 29 7b  tch( iCompare ){
3036c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63  .              c
3036d 61 73 65 20 2d 31 3a 0a 20 20 20 20 20 20 20 20  ase -1:.        
3036e 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28          plwCopy(
3036f 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72 31 29  &plwriter, &pr1)
30370 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
30371 20 20 70 6c 72 53 74 65 70 28 26 70 72 31 29 3b    plrStep(&pr1);
30372 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
30373 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
30374 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a 20 20        case 1:.  
30375 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
30376 77 43 6f 70 79 28 26 70 6c 77 72 69 74 65 72 2c  wCopy(&plwriter,
30377 20 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20 20   &pr2);.        
30378 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28          plrStep(
30379 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20  &pr2);.         
3037a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3037b 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
3037c 20 30 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20   0:.            
3037d 20 20 20 20 70 6c 77 43 6f 70 79 28 26 70 6c 77      plwCopy(&plw
3037e 72 69 74 65 72 2c 20 26 70 72 31 29 3b 0a 20 20  riter, &pr1);.  
3037f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
30380 72 53 74 65 70 28 26 70 72 31 29 3b 0a 20 20 20  rStep(&pr1);.   
30381 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72               plr
30382 53 74 65 70 28 26 70 72 32 29 3b 0a 20 20 20 20  Step(&pr2);.    
30383 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
30384 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  k;.            }
30385 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
30386 20 20 20 20 20 20 20 70 6c 77 54 65 72 6d 69 6e         plwTermin
30387 61 74 65 28 26 70 6c 77 72 69 74 65 72 29 3b 0a  ate(&plwriter);.
30388 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
30389 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
3038a 72 6f 79 28 26 6f 6e 65 29 3b 0a 20 20 20 20 20  roy(&one);.     
3038b 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
3038c 74 72 6f 79 28 26 74 77 6f 29 3b 0a 20 20 20 20  troy(&two);.    
3038d 20 20 7d 0a 20 20 20 20 20 20 64 6c 72 53 74 65    }.      dlrSte
3038e 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20  p(&left);.      
3038f 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  dlrStep(&right);
30390 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c  .    }.  }..  dl
30391 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b  rDestroy(&left);
30392 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
30393 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74  ight);.  dlwDest
30394 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a  roy(&writer);.}.
30395 0a 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f 20  ./* We have two 
30396 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73  DL_DOCIDS doclis
30397 74 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70  ts:  pLeft and p
30398 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20  Right..** Write 
30399 74 68 65 20 69 6e 74 65 72 73 65 63 74 69 6f 6e  the intersection
3039a 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 64 6f   of these two do
3039b 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 4f 75 74  clists into pOut
3039c 20 61 73 20 61 0a 2a 2a 20 44 4c 5f 44 4f 43 49   as a.** DL_DOCI
3039d 44 53 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 73  DS doclist..*/.s
3039e 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69  tatic void docLi
3039f 73 74 41 6e 64 4d 65 72 67 65 28 0a 20 20 63 6f  stAndMerge(.  co
303a0 6e 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74 2c  nst char *pLeft,
303a1 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f   int nLeft,.  co
303a2 6e 73 74 20 63 68 61 72 20 2a 70 52 69 67 68 74  nst char *pRight
303a3 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20  , int nRight,.  
303a4 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75 74  DataBuffer *pOut
303a5 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
303a6 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c  he combined docl
303a7 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ist here */.){. 
303a8 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20   DLReader left, 
303a9 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74 65  right;.  DLWrite
303aa 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66 28  r writer;..  if(
303ab 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c 20 6e 52 69   nLeft==0 || nRi
303ac 67 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ght==0 ) return;
303ad 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66  ..  dlrInit(&lef
303ae 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4c  t, DL_DOCIDS, pL
303af 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64  eft, nLeft);.  d
303b0 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44  lrInit(&right, D
303b1 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69 67 68 74  L_DOCIDS, pRight
303b2 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77  , nRight);.  dlw
303b3 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c  Init(&writer, DL
303b4 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74 29 3b 0a  _DOCIDS, pOut);.
303b5 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74  .  while( !dlrAt
303b6 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20 21 64  End(&left) && !d
303b7 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
303b8 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 44 6f  ){.    if( dlrDo
303b9 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f  cid(&left)<dlrDo
303ba 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  cid(&right) ){. 
303bb 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65       dlrStep(&le
303bc 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
303bd 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67  f( dlrDocid(&rig
303be 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65  ht)<dlrDocid(&le
303bf 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72  ft) ){.      dlr
303c0 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
303c1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
303c2 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20 64  lwAdd(&writer, d
303c3 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29 3b  lrDocid(&left));
303c4 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
303c5 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 6c 72  left);.      dlr
303c6 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
303c7 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65    }.  }..  dlrDe
303c8 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
303c9 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68  dlrDestroy(&righ
303ca 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79  t);.  dlwDestroy
303cb 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a  (&writer);.}../*
303cc 20 57 65 20 68 61 76 65 20 74 77 6f 20 44 4c 5f   We have two DL_
303cd 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 73 3a  DOCIDS doclists:
303ce 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67    pLeft and pRig
303cf 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  ht..** Write the
303d0 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 73 65 20   union of these 
303d1 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 69 6e 74  two doclists int
303d2 6f 20 70 4f 75 74 20 61 73 20 61 0a 2a 2a 20 44  o pOut as a.** D
303d3 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74  L_DOCIDS doclist
303d4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
303d5 20 64 6f 63 4c 69 73 74 4f 72 4d 65 72 67 65 28   docListOrMerge(
303d6 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
303d7 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c  Left, int nLeft,
303d8 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
303d9 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68  Right, int nRigh
303da 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  t,.  DataBuffer 
303db 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72  *pOut      /* Wr
303dc 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64  ite the combined
303dd 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f   doclist here */
303de 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c  .){.  DLReader l
303df 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c  eft, right;.  DL
303e0 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a  Writer writer;..
303e1 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 29    if( nLeft==0 )
303e2 7b 0a 20 20 20 20 69 66 28 20 6e 52 69 67 68 74  {.    if( nRight
303e3 21 3d 30 20 29 20 64 61 74 61 42 75 66 66 65 72  !=0 ) dataBuffer
303e4 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70 52 69  Append(pOut, pRi
303e5 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20  ght, nRight);.  
303e6 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
303e7 69 66 28 20 6e 52 69 67 68 74 3d 3d 30 20 29 7b  if( nRight==0 ){
303e8 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
303e9 70 70 65 6e 64 28 70 4f 75 74 2c 20 70 4c 65 66  ppend(pOut, pLef
303ea 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 20 20 72  t, nLeft);.    r
303eb 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 64 6c  eturn;.  }..  dl
303ec 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c 5f  rInit(&left, DL_
303ed 44 4f 43 49 44 53 2c 20 70 4c 65 66 74 2c 20 6e  DOCIDS, pLeft, n
303ee 4c 65 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69 74  Left);.  dlrInit
303ef 28 26 72 69 67 68 74 2c 20 44 4c 5f 44 4f 43 49  (&right, DL_DOCI
303f0 44 53 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67  DS, pRight, nRig
303f1 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26  ht);.  dlwInit(&
303f2 77 72 69 74 65 72 2c 20 44 4c 5f 44 4f 43 49 44  writer, DL_DOCID
303f3 53 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68 69  S, pOut);..  whi
303f4 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26 6c  le( !dlrAtEnd(&l
303f5 65 66 74 29 20 7c 7c 20 21 64 6c 72 41 74 45 6e  eft) || !dlrAtEn
303f6 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
303f7 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26 72   if( dlrAtEnd(&r
303f8 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64  ight) ){.      d
303f9 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20 64  lwAdd(&writer, d
303fa 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29 3b  lrDocid(&left));
303fb 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
303fc 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  left);.    }else
303fd 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26 6c   if( dlrAtEnd(&l
303fe 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c  eft) ){.      dl
303ff 77 41 64 64 28 26 77 72 69 74 65 72 2c 20 64 6c  wAdd(&writer, dl
30400 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 29 3b  rDocid(&right));
30401 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
30402 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  right);.    }els
30403 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26  e if( dlrDocid(&
30404 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64 28 26  left)<dlrDocid(&
30405 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  right) ){.      
30406 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20  dlwAdd(&writer, 
30407 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29  dlrDocid(&left))
30408 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
30409 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73  &left);.    }els
3040a 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26  e if( dlrDocid(&
3040b 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28  right)<dlrDocid(
3040c 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20  &left) ){.      
3040d 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20  dlwAdd(&writer, 
3040e 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
3040f 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
30410 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
30411 6c 73 65 7b 0a 20 20 20 20 20 20 64 6c 77 41 64  lse{.      dlwAd
30412 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f  d(&writer, dlrDo
30413 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20  cid(&left));.   
30414 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74     dlrStep(&left
30415 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
30416 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a  (&right);.    }.
30417 20 20 7d 0a 0a 20 20 64 6c 72 44 65 73 74 72 6f    }..  dlrDestro
30418 79 28 26 6c 65 66 74 29 3b 0a 20 20 64 6c 72 44  y(&left);.  dlrD
30419 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a  estroy(&right);.
3041a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26 77 72    dlwDestroy(&wr
3041b 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 57 65 20  iter);.}../* We 
3041c 68 61 76 65 20 74 77 6f 20 44 4c 5f 44 4f 43 49  have two DL_DOCI
3041d 44 53 20 64 6f 63 6c 69 73 74 73 3a 20 20 70 4c  DS doclists:  pL
3041e 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e 0a  eft and pRight..
3041f 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 70 4f  ** Write into pO
30420 75 74 20 61 73 20 44 4c 5f 44 4f 43 49 44 53 20  ut as DL_DOCIDS 
30421 64 6f 63 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69  doclist containi
30422 6e 67 20 61 6c 6c 20 64 6f 63 75 6d 65 6e 74 73  ng all documents
30423 20 74 68 61 74 0a 2a 2a 20 6f 63 63 75 72 20 69   that.** occur i
30424 6e 20 70 4c 65 66 74 20 62 75 74 20 6e 6f 74 20  n pLeft but not 
30425 69 6e 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 73 74  in pRight..*/.st
30426 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73  atic void docLis
30427 74 45 78 63 65 70 74 4d 65 72 67 65 28 0a 20 20  tExceptMerge(.  
30428 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c 65 66  const char *pLef
30429 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20  t, int nLeft,.  
3042a 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 69 67  const char *pRig
3042b 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a  ht, int nRight,.
3042c 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f    DataBuffer *pO
3042d 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  ut      /* Write
3042e 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f   the combined do
3042f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b  clist here */.){
30430 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74  .  DLReader left
30431 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69  , right;.  DLWri
30432 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69  ter writer;..  i
30433 66 28 20 6e 4c 65 66 74 3d 3d 30 20 29 20 72 65  f( nLeft==0 ) re
30434 74 75 72 6e 3b 0a 20 20 69 66 28 20 6e 52 69 67  turn;.  if( nRig
30435 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74  ht==0 ){.    dat
30436 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f  aBufferAppend(pO
30437 75 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74  ut, pLeft, nLeft
30438 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
30439 20 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c   }..  dlrInit(&l
3043a 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20  eft, DL_DOCIDS, 
3043b 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
3043c 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c   dlrInit(&right,
3043d 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69 67   DL_DOCIDS, pRig
3043e 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64  ht, nRight);.  d
3043f 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20  lwInit(&writer, 
30440 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74 29  DL_DOCIDS, pOut)
30441 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72  ;..  while( !dlr
30442 41 74 45 6e 64 28 26 6c 65 66 74 29 20 29 7b 0a  AtEnd(&left) ){.
30443 20 20 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41      while( !dlrA
30444 74 45 6e 64 28 26 72 69 67 68 74 29 20 26 26 20  tEnd(&right) && 
30445 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
30446 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29  <dlrDocid(&left)
30447 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74 65   ){.      dlrSte
30448 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
30449 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74 45 6e  .    if( dlrAtEn
3044a 64 28 26 72 69 67 68 74 29 20 7c 7c 20 64 6c 72  d(&right) || dlr
3044b 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72  Docid(&left)<dlr
3044c 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b  Docid(&right) ){
3044d 0a 20 20 20 20 20 20 64 6c 77 41 64 64 28 26 77  .      dlwAdd(&w
3044e 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28  riter, dlrDocid(
3044f 26 6c 65 66 74 29 29 3b 0a 20 20 20 20 7d 0a 20  &left));.    }. 
30450 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74     dlrStep(&left
30451 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65 73  );.  }..  dlrDes
30452 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 64  troy(&left);.  d
30453 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74  lrDestroy(&right
30454 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28  );.  dlwDestroy(
30455 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 73 74 61  &writer);.}..sta
30456 74 69 63 20 63 68 61 72 20 2a 73 74 72 69 6e 67  tic char *string
30457 5f 64 75 70 5f 6e 28 63 6f 6e 73 74 20 63 68 61  _dup_n(const cha
30458 72 20 2a 73 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *s, int n){.  
30459 63 68 61 72 20 2a 73 74 72 20 3d 20 73 71 6c 69  char *str = sqli
3045a 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 20 2b 20 31  te3_malloc(n + 1
3045b 29 3b 0a 20 20 6d 65 6d 63 70 79 28 73 74 72 2c  );.  memcpy(str,
3045c 20 73 2c 20 6e 29 3b 0a 20 20 73 74 72 5b 6e 5d   s, n);.  str[n]
3045d 20 3d 20 27 5c 30 27 3b 0a 20 20 72 65 74 75 72   = '\0';.  retur
3045e 6e 20 73 74 72 3b 0a 7d 0a 0a 2f 2a 20 44 75 70  n str;.}../* Dup
3045f 6c 69 63 61 74 65 20 61 20 73 74 72 69 6e 67 3b  licate a string;
30460 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   the caller must
30461 20 66 72 65 65 28 29 20 74 68 65 20 72 65 74 75   free() the retu
30462 72 6e 65 64 20 73 74 72 69 6e 67 2e 0a 20 2a 20  rned string.. * 
30463 28 57 65 20 64 6f 6e 27 74 20 75 73 65 20 73 74  (We don't use st
30464 72 64 75 70 28 29 20 73 69 6e 63 65 20 69 74 20  rdup() since it 
30465 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74  is not part of t
30466 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  he standard C li
30467 62 72 61 72 79 20 61 6e 64 0a 20 2a 20 6d 61 79  brary and. * may
30468 20 6e 6f 74 20 62 65 20 61 76 61 69 6c 61 62 6c   not be availabl
30469 65 20 65 76 65 72 79 77 68 65 72 65 2e 29 20 2a  e everywhere.) *
3046a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 73  /.static char *s
3046b 74 72 69 6e 67 5f 64 75 70 28 63 6f 6e 73 74 20  tring_dup(const 
3046c 63 68 61 72 20 2a 73 29 7b 0a 20 20 72 65 74 75  char *s){.  retu
3046d 72 6e 20 73 74 72 69 6e 67 5f 64 75 70 5f 6e 28  rn string_dup_n(
3046e 73 2c 20 73 74 72 6c 65 6e 28 73 29 29 3b 0a 7d  s, strlen(s));.}
3046f 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20 61 20 73 74  ../* Format a st
30470 72 69 6e 67 2c 20 72 65 70 6c 61 63 69 6e 67 20  ring, replacing 
30471 65 61 63 68 20 6f 63 63 75 72 72 65 6e 63 65 20  each occurrence 
30472 6f 66 20 74 68 65 20 25 20 63 68 61 72 61 63 74  of the % charact
30473 65 72 20 77 69 74 68 0a 20 2a 20 7a 44 62 2e 7a  er with. * zDb.z
30474 4e 61 6d 65 2e 20 20 54 68 69 73 20 6d 61 79 20  Name.  This may 
30475 62 65 20 6d 6f 72 65 20 63 6f 6e 76 65 6e 69 65  be more convenie
30476 6e 74 20 74 68 61 6e 20 73 71 6c 69 74 65 5f 6d  nt than sqlite_m
30477 70 72 69 6e 74 66 28 29 0a 20 2a 20 77 68 65 6e  printf(). * when
30478 20 6f 6e 65 20 73 74 72 69 6e 67 20 69 73 20 75   one string is u
30479 73 65 64 20 72 65 70 65 61 74 65 64 6c 79 20 69  sed repeatedly i
3047a 6e 20 61 20 66 6f 72 6d 61 74 20 73 74 72 69 6e  n a format strin
3047b 67 2e 0a 20 2a 20 54 68 65 20 63 61 6c 6c 65 72  g.. * The caller
3047c 20 6d 75 73 74 20 66 72 65 65 28 29 20 74 68 65   must free() the
3047d 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67   returned string
3047e 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  . */.static char
3047f 20 2a 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74 28   *string_format(
30480 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
30481 6d 61 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  mat,.           
30482 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30483 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c  const char *zDb,
30484 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
30485 6d 65 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  me){.  const cha
30486 72 20 2a 70 3b 0a 20 20 73 69 7a 65 5f 74 20 6c  r *p;.  size_t l
30487 65 6e 20 3d 20 30 3b 0a 20 20 73 69 7a 65 5f 74  en = 0;.  size_t
30488 20 6e 44 62 20 3d 20 73 74 72 6c 65 6e 28 7a 44   nDb = strlen(zD
30489 62 29 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 4e 61  b);.  size_t nNa
3048a 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d  me = strlen(zNam
3048b 65 29 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 46 75  e);.  size_t nFu
3048c 6c 6c 54 61 62 6c 65 4e 61 6d 65 20 3d 20 6e 44  llTableName = nD
3048d 62 2b 31 2b 6e 4e 61 6d 65 3b 0a 20 20 63 68 61  b+1+nName;.  cha
3048e 72 20 2a 72 65 73 75 6c 74 3b 0a 20 20 63 68 61  r *result;.  cha
3048f 72 20 2a 72 3b 0a 0a 20 20 2f 2a 20 66 69 72 73  r *r;..  /* firs
30490 74 20 63 6f 6d 70 75 74 65 20 6c 65 6e 67 74 68  t compute length
30491 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 66 6f 72   needed */.  for
30492 28 70 20 3d 20 7a 46 6f 72 6d 61 74 20 3b 20 2a  (p = zFormat ; *
30493 70 20 3b 20 2b 2b 70 29 7b 0a 20 20 20 20 6c 65  p ; ++p){.    le
30494 6e 20 2b 3d 20 28 2a 70 3d 3d 27 25 27 20 3f 20  n += (*p=='%' ? 
30495 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d 65 20 3a  nFullTableName :
30496 20 31 29 3b 0a 20 20 7d 0a 20 20 6c 65 6e 20 2b   1);.  }.  len +
30497 3d 20 31 3b 20 20 2f 2a 20 66 6f 72 20 6e 75 6c  = 1;  /* for nul
30498 6c 20 74 65 72 6d 69 6e 61 74 6f 72 20 2a 2f 0a  l terminator */.
30499 0a 20 20 72 20 3d 20 72 65 73 75 6c 74 20 3d 20  .  r = result = 
3049a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6c  sqlite3_malloc(l
3049b 65 6e 29 3b 0a 20 20 66 6f 72 28 70 20 3d 20 7a  en);.  for(p = z
3049c 46 6f 72 6d 61 74 3b 20 2a 70 3b 20 2b 2b 70 29  Format; *p; ++p)
3049d 7b 0a 20 20 20 20 69 66 28 20 2a 70 3d 3d 27 25  {.    if( *p=='%
3049e 27 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  ' ){.      memcp
3049f 79 28 72 2c 20 7a 44 62 2c 20 6e 44 62 29 3b 0a  y(r, zDb, nDb);.
304a0 20 20 20 20 20 20 72 20 2b 3d 20 6e 44 62 3b 0a        r += nDb;.
304a1 20 20 20 20 20 20 2a 72 2b 2b 20 3d 20 27 2e 27        *r++ = '.'
304a2 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 72  ;.      memcpy(r
304a3 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
304a4 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e 4e 61 6d  .      r += nNam
304a5 65 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  e;.    } else {.
304a6 20 20 20 20 20 20 2a 72 2b 2b 20 3d 20 2a 70 3b        *r++ = *p;
304a7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 72 2b  .    }.  }.  *r+
304a8 2b 20 3d 20 27 5c 30 27 3b 0a 20 20 61 73 73 65  + = '\0';.  asse
304a9 72 74 28 20 72 20 3d 3d 20 72 65 73 75 6c 74 20  rt( r == result 
304aa 2b 20 6c 65 6e 20 29 3b 0a 20 20 72 65 74 75 72  + len );.  retur
304ab 6e 20 72 65 73 75 6c 74 3b 0a 7d 0a 0a 73 74 61  n result;.}..sta
304ac 74 69 63 20 69 6e 74 20 73 71 6c 5f 65 78 65 63  tic int sql_exec
304ad 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
304ae 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 63  nst char *zDb, c
304af 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
304b0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
304b1 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
304b2 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a 20 20 63 68   *zFormat){.  ch
304b3 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64 20 3d 20 73  ar *zCommand = s
304b4 74 72 69 6e 67 5f 66 6f 72 6d 61 74 28 7a 46 6f  tring_format(zFo
304b5 72 6d 61 74 2c 20 7a 44 62 2c 20 7a 4e 61 6d 65  rmat, zDb, zName
304b6 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 46  );.  int rc;.  F
304b7 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 73  TSTRACE(("FTS3 s
304b8 71 6c 3a 20 25 73 5c 6e 22 2c 20 7a 43 6f 6d 6d  ql: %s\n", zComm
304b9 61 6e 64 29 29 3b 0a 20 20 72 63 20 3d 20 73 71  and));.  rc = sq
304ba 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a  lite3_exec(db, z
304bb 43 6f 6d 6d 61 6e 64 2c 20 4e 55 4c 4c 2c 20 30  Command, NULL, 0
304bc 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73 71 6c 69 74  , NULL);.  sqlit
304bd 65 33 5f 66 72 65 65 28 7a 43 6f 6d 6d 61 6e 64  e3_free(zCommand
304be 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
304bf 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  }..static int sq
304c0 6c 5f 70 72 65 70 61 72 65 28 73 71 6c 69 74 65  l_prepare(sqlite
304c1 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
304c2 72 20 2a 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68  r *zDb, const ch
304c3 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  ar *zName,.     
304c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304c5 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
304c6 2a 70 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63  *ppStmt, const c
304c7 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a 20  har *zFormat){. 
304c8 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64 20   char *zCommand 
304c9 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74 28  = string_format(
304ca 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c 20 7a 4e  zFormat, zDb, zN
304cb 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  ame);.  int rc;.
304cc 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
304cd 33 20 70 72 65 70 61 72 65 3a 20 25 73 5c 6e 22  3 prepare: %s\n"
304ce 2c 20 7a 43 6f 6d 6d 61 6e 64 29 29 3b 0a 20 20  , zCommand));.  
304cf 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
304d0 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 43 6f 6d  pare_v2(db, zCom
304d1 6d 61 6e 64 2c 20 2d 31 2c 20 70 70 53 74 6d 74  mand, -1, ppStmt
304d2 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73 71 6c 69 74  , NULL);.  sqlit
304d3 65 33 5f 66 72 65 65 28 7a 43 6f 6d 6d 61 6e 64  e3_free(zCommand
304d4 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
304d5 7d 0a 0a 2f 2a 20 65 6e 64 20 75 74 69 6c 69 74  }../* end utilit
304d6 79 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a  y functions */..
304d7 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72  /* Forward refer
304d8 65 6e 63 65 20 2a 2f 0a 74 79 70 65 64 65 66 20  ence */.typedef 
304d9 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78 74 5f  struct fulltext_
304da 76 74 61 62 20 66 75 6c 6c 74 65 78 74 5f 76 74  vtab fulltext_vt
304db 61 62 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  ab;../*.** An in
304dc 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
304dd 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
304de 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66  e keeps track of
304df 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 6d 61   generated.** ma
304e0 74 63 68 69 6e 67 2d 77 6f 72 64 20 6f 66 66 73  tching-word offs
304e1 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  et information a
304e2 6e 64 20 73 6e 69 70 70 65 74 73 2e 0a 2a 2f 0a  nd snippets..*/.
304e3 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
304e4 6e 69 70 70 65 74 20 7b 0a 20 20 69 6e 74 20 6e  nippet {.  int n
304e5 4d 61 74 63 68 3b 20 20 20 20 20 2f 2a 20 54 6f  Match;     /* To
304e6 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61  tal number of ma
304e7 74 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  tches */.  int n
304e8 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 53 70  Alloc;     /* Sp
304e9 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ace allocated fo
304ea 72 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20 20  r aMatch[] */.  
304eb 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61  struct snippetMa
304ec 74 63 68 20 7b 20 2f 2a 20 4f 6e 65 20 65 6e 74  tch { /* One ent
304ed 72 79 20 66 6f 72 20 65 61 63 68 20 6d 61 74 63  ry for each matc
304ee 68 69 6e 67 20 74 65 72 6d 20 2a 2f 0a 20 20 20  hing term */.   
304ef 20 63 68 61 72 20 73 6e 53 74 61 74 75 73 3b 20   char snStatus; 
304f0 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20        /* Status 
304f1 66 6c 61 67 20 66 6f 72 20 75 73 65 20 77 68 69  flag for use whi
304f2 6c 65 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20  le constructing 
304f3 73 6e 69 70 70 65 74 73 20 2a 2f 0a 20 20 20 20  snippets */.    
304f4 73 68 6f 72 74 20 69 6e 74 20 69 43 6f 6c 3b 20  short int iCol; 
304f5 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75       /* The colu
304f6 6d 6e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  mn that contains
304f7 20 74 68 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20   the match */.  
304f8 20 20 73 68 6f 72 74 20 69 6e 74 20 69 54 65 72    short int iTer
304f9 6d 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e  m;     /* The in
304fa 64 65 78 20 69 6e 20 51 75 65 72 79 2e 70 54 65  dex in Query.pTe
304fb 72 6d 73 5b 5d 20 6f 66 20 74 68 65 20 6d 61 74  rms[] of the mat
304fc 63 68 69 6e 67 20 74 65 72 6d 20 2a 2f 0a 20 20  ching term */.  
304fd 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20 20    int iToken;   
304fe 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
304ff 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74 63 68  dex of the match
30500 69 6e 67 20 64 6f 63 75 6d 65 6e 74 20 74 6f 6b  ing document tok
30501 65 6e 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20  en */.    short 
30502 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 2f  int nByte;     /
30503 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
30504 73 20 69 6e 20 74 68 65 20 74 65 72 6d 20 2a 2f  s in the term */
30505 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 3b  .    int iStart;
30506 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
30507 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 66   offset to the f
30508 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 6f  irst character o
30509 66 20 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20 20  f the term */.  
3050a 7d 20 2a 61 4d 61 74 63 68 3b 20 20 20 20 20 20  } *aMatch;      
3050b 2f 2a 20 50 6f 69 6e 74 73 20 74 6f 20 73 70 61  /* Points to spa
3050c 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ce obtained from
3050d 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61   malloc */.  cha
3050e 72 20 2a 7a 4f 66 66 73 65 74 3b 20 20 2f 2a 20  r *zOffset;  /* 
3050f 54 65 78 74 20 72 65 6e 64 65 72 69 6e 67 20 6f  Text rendering o
30510 66 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20 20  f aMatch[] */.  
30511 69 6e 74 20 6e 4f 66 66 73 65 74 3b 20 20 20 20  int nOffset;    
30512 2f 2a 20 73 74 72 6c 65 6e 28 7a 4f 66 66 73 65  /* strlen(zOffse
30513 74 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53  t) */.  char *zS
30514 6e 69 70 70 65 74 3b 20 2f 2a 20 53 6e 69 70 70  nippet; /* Snipp
30515 65 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  et text */.  int
30516 20 6e 53 6e 69 70 70 65 74 3b 20 20 20 2f 2a 20   nSnippet;   /* 
30517 73 74 72 6c 65 6e 28 7a 53 6e 69 70 70 65 74 29  strlen(zSnippet)
30518 20 2a 2f 0a 7d 20 53 6e 69 70 70 65 74 3b 0a 0a   */.} Snippet;..
30519 0a 74 79 70 65 64 65 66 20 65 6e 75 6d 20 51 75  .typedef enum Qu
3051a 65 72 79 54 79 70 65 20 7b 0a 20 20 51 55 45 52  eryType {.  QUER
3051b 59 5f 47 45 4e 45 52 49 43 2c 20 20 20 2f 2a 20  Y_GENERIC,   /* 
3051c 74 61 62 6c 65 20 73 63 61 6e 20 2a 2f 0a 20 20  table scan */.  
3051d 51 55 45 52 59 5f 44 4f 43 49 44 2c 20 20 20 20  QUERY_DOCID,    
3051e 20 2f 2a 20 6c 6f 6f 6b 75 70 20 62 79 20 64 6f   /* lookup by do
3051f 63 69 64 20 2a 2f 0a 20 20 51 55 45 52 59 5f 46  cid */.  QUERY_F
30520 55 4c 4c 54 45 58 54 20 20 20 2f 2a 20 51 55 45  ULLTEXT   /* QUE
30521 52 59 5f 46 55 4c 4c 54 45 58 54 20 2b 20 5b 69  RY_FULLTEXT + [i
30522 5d 20 69 73 20 61 20 66 75 6c 6c 2d 74 65 78 74  ] is a full-text
30523 20 73 65 61 72 63 68 20 66 6f 72 20 63 6f 6c 75   search for colu
30524 6d 6e 20 69 2a 2f 0a 7d 20 51 75 65 72 79 54 79  mn i*/.} QueryTy
30525 70 65 3b 0a 0a 74 79 70 65 64 65 66 20 65 6e 75  pe;..typedef enu
30526 6d 20 66 75 6c 6c 74 65 78 74 5f 73 74 61 74 65  m fulltext_state
30527 6d 65 6e 74 20 7b 0a 20 20 43 4f 4e 54 45 4e 54  ment {.  CONTENT
30528 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c 0a 20 20  _INSERT_STMT,.  
30529 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f 53  CONTENT_SELECT_S
3052a 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 55  TMT,.  CONTENT_U
3052b 50 44 41 54 45 5f 53 54 4d 54 2c 0a 20 20 43 4f  PDATE_STMT,.  CO
3052c 4e 54 45 4e 54 5f 44 45 4c 45 54 45 5f 53 54 4d  NTENT_DELETE_STM
3052d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 45 58 49  T,.  CONTENT_EXI
3052e 53 54 53 5f 53 54 4d 54 2c 0a 0a 20 20 42 4c 4f  STS_STMT,..  BLO
3052f 43 4b 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c 0a  CK_INSERT_STMT,.
30530 20 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 5f 53    BLOCK_SELECT_S
30531 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 44 45 4c  TMT,.  BLOCK_DEL
30532 45 54 45 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f 43  ETE_STMT,.  BLOC
30533 4b 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d  K_DELETE_ALL_STM
30534 54 2c 0a 0a 20 20 53 45 47 44 49 52 5f 4d 41 58  T,..  SEGDIR_MAX
30535 5f 49 4e 44 45 58 5f 53 54 4d 54 2c 0a 20 20 53  _INDEX_STMT,.  S
30536 45 47 44 49 52 5f 53 45 54 5f 53 54 4d 54 2c 0a  EGDIR_SET_STMT,.
30537 20 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f    SEGDIR_SELECT_
30538 4c 45 56 45 4c 5f 53 54 4d 54 2c 0a 20 20 53 45  LEVEL_STMT,.  SE
30539 47 44 49 52 5f 53 50 41 4e 5f 53 54 4d 54 2c 0a  GDIR_SPAN_STMT,.
3053a 20 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f    SEGDIR_DELETE_
3053b 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53  STMT,.  SEGDIR_S
3053c 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54 5f 53 54  ELECT_SEGMENT_ST
3053d 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 45 4c  MT,.  SEGDIR_SEL
3053e 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 20 20  ECT_ALL_STMT,.  
3053f 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f 41 4c  SEGDIR_DELETE_AL
30540 4c 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52  L_STMT,.  SEGDIR
30541 5f 43 4f 55 4e 54 5f 53 54 4d 54 2c 0a 0a 20 20  _COUNT_STMT,..  
30542 4d 41 58 5f 53 54 4d 54 20 20 20 20 20 20 20 20  MAX_STMT        
30543 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30544 41 6c 77 61 79 73 20 61 74 20 65 6e 64 21 20 2a  Always at end! *
30545 2f 0a 7d 20 66 75 6c 6c 74 65 78 74 5f 73 74 61  /.} fulltext_sta
30546 74 65 6d 65 6e 74 3b 0a 0a 2f 2a 20 54 68 65 73  tement;../* Thes
30547 65 20 6d 75 73 74 20 65 78 61 63 74 6c 79 20 6d  e must exactly m
30548 61 74 63 68 20 74 68 65 20 65 6e 75 6d 20 61 62  atch the enum ab
30549 6f 76 65 2e 20 2a 2f 0a 2f 2a 20 54 4f 44 4f 28  ove. */./* TODO(
3054a 73 68 65 73 73 29 3a 20 49 73 20 74 68 65 72 65  shess): Is there
3054b 20 73 6f 6d 65 20 72 69 73 6b 20 74 68 61 74 20   some risk that 
3054c 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c  a statement will
3054d 20 62 65 20 75 73 65 64 20 69 6e 20 74 77 6f 0a   be used in two.
3054e 2a 2a 20 63 75 72 73 6f 72 73 20 61 74 20 6f 6e  ** cursors at on
3054f 63 65 2c 20 65 2e 67 2e 20 20 69 66 20 61 20 71  ce, e.g.  if a q
30550 75 65 72 79 20 6a 6f 69 6e 73 20 61 20 76 69 72  uery joins a vir
30551 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20 69 74  tual table to it
30552 73 65 6c 66 3f 0a 2a 2a 20 49 66 20 73 6f 20 70  self?.** If so p
30553 65 72 68 61 70 73 20 77 65 20 73 68 6f 75 6c 64  erhaps we should
30554 20 6d 6f 76 65 20 73 6f 6d 65 20 6f 66 20 74 68   move some of th
30555 65 73 65 20 74 6f 20 74 68 65 20 63 75 72 73 6f  ese to the curso
30556 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61  r object..*/.sta
30557 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
30558 63 6f 6e 73 74 20 66 75 6c 6c 74 65 78 74 5f 7a  const fulltext_z
30559 53 74 61 74 65 6d 65 6e 74 5b 4d 41 58 5f 53 54  Statement[MAX_ST
3055a 4d 54 5d 20 3d 20 7b 0a 20 20 2f 2a 20 43 4f 4e  MT] = {.  /* CON
3055b 54 45 4e 54 5f 49 4e 53 45 52 54 20 2a 2f 20 4e  TENT_INSERT */ N
3055c 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61 74  ULL,  /* generat
3055d 65 64 20 69 6e 20 63 6f 6e 74 65 6e 74 49 6e 73  ed in contentIns
3055e 65 72 74 53 74 61 74 65 6d 65 6e 74 28 29 20 2a  ertStatement() *
3055f 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 53  /.  /* CONTENT_S
30560 45 4c 45 43 54 20 2a 2f 20 4e 55 4c 4c 2c 20 20  ELECT */ NULL,  
30561 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  /* generated in 
30562 63 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74 61  contentSelectSta
30563 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a  tement() */.  /*
30564 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45 20   CONTENT_UPDATE 
30565 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e  */ NULL,  /* gen
30566 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74 65 6e  erated in conten
30567 74 55 70 64 61 74 65 53 74 61 74 65 6d 65 6e 74  tUpdateStatement
30568 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45  () */.  /* CONTE
30569 4e 54 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64 65  NT_DELETE */ "de
3056a 6c 65 74 65 20 66 72 6f 6d 20 25 5f 63 6f 6e 74  lete from %_cont
3056b 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64 20  ent where docid 
3056c 3d 20 3f 22 2c 0a 20 20 2f 2a 20 43 4f 4e 54 45  = ?",.  /* CONTE
3056d 4e 54 5f 45 58 49 53 54 53 20 2a 2f 20 22 73 65  NT_EXISTS */ "se
3056e 6c 65 63 74 20 64 6f 63 69 64 20 66 72 6f 6d 20  lect docid from 
3056f 25 5f 63 6f 6e 74 65 6e 74 20 6c 69 6d 69 74 20  %_content limit 
30570 31 22 2c 0a 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f  1",..  /* BLOCK_
30571 49 4e 53 45 52 54 20 2a 2f 0a 20 20 22 69 6e 73  INSERT */.  "ins
30572 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67 6d 65  ert into %_segme
30573 6e 74 73 20 28 62 6c 6f 63 6b 69 64 2c 20 62 6c  nts (blockid, bl
30574 6f 63 6b 29 20 76 61 6c 75 65 73 20 28 6e 75 6c  ock) values (nul
30575 6c 2c 20 3f 29 22 2c 0a 20 20 2f 2a 20 42 4c 4f  l, ?)",.  /* BLO
30576 43 4b 5f 53 45 4c 45 43 54 20 2a 2f 20 22 73 65  CK_SELECT */ "se
30577 6c 65 63 74 20 62 6c 6f 63 6b 20 66 72 6f 6d 20  lect block from 
30578 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65 72 65  %_segments where
30579 20 62 6c 6f 63 6b 69 64 20 3d 20 3f 22 2c 0a 20   blockid = ?",. 
3057a 20 2f 2a 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45   /* BLOCK_DELETE
3057b 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d   */ "delete from
3057c 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65 72   %_segments wher
3057d 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77 65 65  e blockid betwee
3057e 6e 20 3f 20 61 6e 64 20 3f 22 2c 0a 20 20 2f 2a  n ? and ?",.  /*
3057f 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41 4c   BLOCK_DELETE_AL
30580 4c 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f  L */ "delete fro
30581 6d 20 25 5f 73 65 67 6d 65 6e 74 73 22 2c 0a 0a  m %_segments",..
30582 20 20 2f 2a 20 53 45 47 44 49 52 5f 4d 41 58 5f    /* SEGDIR_MAX_
30583 49 4e 44 45 58 20 2a 2f 20 22 73 65 6c 65 63 74  INDEX */ "select
30584 20 6d 61 78 28 69 64 78 29 20 66 72 6f 6d 20 25   max(idx) from %
30585 5f 73 65 67 64 69 72 20 77 68 65 72 65 20 6c 65  _segdir where le
30586 76 65 6c 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 53  vel = ?",.  /* S
30587 45 47 44 49 52 5f 53 45 54 20 2a 2f 20 22 69 6e  EGDIR_SET */ "in
30588 73 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67 64  sert into %_segd
30589 69 72 20 76 61 6c 75 65 73 20 28 3f 2c 20 3f 2c  ir values (?, ?,
3058a 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 29 22 2c 0a 20   ?, ?, ?, ?)",. 
3058b 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 4c 45 43   /* SEGDIR_SELEC
3058c 54 5f 4c 45 56 45 4c 20 2a 2f 0a 20 20 22 73 65  T_LEVEL */.  "se
3058d 6c 65 63 74 20 73 74 61 72 74 5f 62 6c 6f 63 6b  lect start_block
3058e 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f  , leaves_end_blo
3058f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d 20 25 5f  ck, root from %_
30590 73 65 67 64 69 72 20 22 0a 20 20 22 20 77 68 65  segdir ".  " whe
30591 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20 6f 72 64  re level = ? ord
30592 65 72 20 62 79 20 69 64 78 22 2c 0a 20 20 2f 2a  er by idx",.  /*
30593 20 53 45 47 44 49 52 5f 53 50 41 4e 20 2a 2f 0a   SEGDIR_SPAN */.
30594 20 20 22 73 65 6c 65 63 74 20 6d 69 6e 28 73 74    "select min(st
30595 61 72 74 5f 62 6c 6f 63 6b 29 2c 20 6d 61 78 28  art_block), max(
30596 65 6e 64 5f 62 6c 6f 63 6b 29 20 66 72 6f 6d 20  end_block) from 
30597 25 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20 77  %_segdir ".  " w
30598 68 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20 61  here level = ? a
30599 6e 64 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20 3c  nd start_block <
3059a 3e 20 30 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49  > 0",.  /* SEGDI
3059b 52 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64 65 6c  R_DELETE */ "del
3059c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 64 69  ete from %_segdi
3059d 72 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d 20  r where level = 
3059e 3f 22 2c 0a 0a 20 20 2f 2a 20 4e 4f 54 45 28 73  ?",..  /* NOTE(s
3059f 68 65 73 73 29 3a 20 54 68 65 20 66 69 72 73 74  hess): The first
305a0 20 74 68 72 65 65 20 72 65 73 75 6c 74 73 20 6f   three results o
305a1 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
305a2 74 77 6f 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  two.  ** stateme
305a3 6e 74 73 20 6d 75 73 74 20 6d 61 74 63 68 2e 0a  nts must match..
305a4 20 20 2a 2f 0a 20 20 2f 2a 20 53 45 47 44 49 52    */.  /* SEGDIR
305a5 5f 53 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54 20  _SELECT_SEGMENT 
305a6 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 73 74 61  */.  "select sta
305a7 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73  rt_block, leaves
305a8 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74  _end_block, root
305a9 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22   from %_segdir "
305aa 0a 20 20 22 20 77 68 65 72 65 20 6c 65 76 65 6c  .  " where level
305ab 20 3d 20 3f 20 61 6e 64 20 69 64 78 20 3d 20 3f   = ? and idx = ?
305ac 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53  ",.  /* SEGDIR_S
305ad 45 4c 45 43 54 5f 41 4c 4c 20 2a 2f 0a 20 20 22  ELECT_ALL */.  "
305ae 73 65 6c 65 63 74 20 73 74 61 72 74 5f 62 6c 6f  select start_blo
305af 63 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62  ck, leaves_end_b
305b0 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d 20  lock, root from 
305b1 25 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20 6f  %_segdir ".  " o
305b2 72 64 65 72 20 62 79 20 6c 65 76 65 6c 20 64 65  rder by level de
305b3 73 63 2c 20 69 64 78 20 61 73 63 22 2c 0a 20 20  sc, idx asc",.  
305b4 2f 2a 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45  /* SEGDIR_DELETE
305b5 5f 41 4c 4c 20 2a 2f 20 22 64 65 6c 65 74 65 20  _ALL */ "delete 
305b6 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 22 2c 0a  from %_segdir",.
305b7 20 20 2f 2a 20 53 45 47 44 49 52 5f 43 4f 55 4e    /* SEGDIR_COUN
305b8 54 20 2a 2f 20 22 73 65 6c 65 63 74 20 63 6f 75  T */ "select cou
305b9 6e 74 28 2a 29 2c 20 69 66 6e 75 6c 6c 28 6d 61  nt(*), ifnull(ma
305ba 78 28 6c 65 76 65 6c 29 2c 30 29 20 66 72 6f 6d  x(level),0) from
305bb 20 25 5f 73 65 67 64 69 72 22 2c 0a 7d 3b 0a 0a   %_segdir",.};..
305bc 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 6e 65 63 74 69  /*.** A connecti
305bd 6f 6e 20 74 6f 20 61 20 66 75 6c 6c 74 65 78 74  on to a fulltext
305be 20 69 6e 64 65 78 20 69 73 20 61 6e 20 69 6e 73   index is an ins
305bf 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
305c0 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74  lowing.** struct
305c1 75 72 65 2e 20 20 54 68 65 20 78 43 72 65 61 74  ure.  The xCreat
305c2 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20 6d  e and xConnect m
305c3 65 74 68 6f 64 73 20 63 72 65 61 74 65 20 61 6e  ethods create an
305c4 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20   instance.** of 
305c5 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61  this structure a
305c6 6e 64 20 78 44 65 73 74 72 6f 79 20 61 6e 64 20  nd xDestroy and 
305c7 78 44 69 73 63 6f 6e 6e 65 63 74 20 66 72 65 65  xDisconnect free
305c8 20 74 68 61 74 20 69 6e 73 74 61 6e 63 65 2e 0a   that instance..
305c9 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 6d 65 74  ** All other met
305ca 68 6f 64 73 20 72 65 63 65 69 76 65 20 61 20 70  hods receive a p
305cb 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 74  ointer to the st
305cc 72 75 63 74 75 72 65 20 61 73 20 6f 6e 65 20 6f  ructure as one o
305cd 66 20 74 68 65 69 72 0a 2a 2a 20 61 72 67 75 6d  f their.** argum
305ce 65 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ents..*/.struct 
305cf 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 7b 0a  fulltext_vtab {.
305d0 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 62    sqlite3_vtab b
305d1 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ase;            
305d2 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73     /* Base class
305d3 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
305d4 63 6f 72 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  core */.  sqlite
305d5 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
305d6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
305d7 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
305d8 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
305d9 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20   char *zDb;     
305da 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c              /* l
305db 6f 67 69 63 61 6c 20 64 61 74 61 62 61 73 65 20  ogical database 
305dc 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  name */.  const 
305dd 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
305de 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 69             /* vi
305df 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65  rtual table name
305e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d   */.  int nColum
305e1 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
305e2 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72         /* number
305e3 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 76   of columns in v
305e4 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
305e5 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 75 6d    char **azColum
305e6 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
305e7 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d     /* column nam
305e8 65 73 2e 20 20 6d 61 6c 6c 6f 63 65 64 20 2a 2f  es.  malloced */
305e9 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6e 74  .  char **azCont
305ea 65 6e 74 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  entColumn;      
305eb 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 61      /* column na
305ec 6d 65 73 20 69 6e 20 63 6f 6e 74 65 6e 74 20 74  mes in content t
305ed 61 62 6c 65 3b 20 6d 61 6c 6c 6f 63 65 64 20 2a  able; malloced *
305ee 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
305ef 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
305f0 72 3b 20 20 20 2f 2a 20 74 6f 6b 65 6e 69 7a 65  r;   /* tokenize
305f1 72 20 66 6f 72 20 69 6e 73 65 72 74 73 20 61 6e  r for inserts an
305f2 64 20 71 75 65 72 69 65 73 20 2a 2f 0a 0a 20 20  d queries */..  
305f3 2f 2a 20 50 72 65 63 6f 6d 70 69 6c 65 64 20 73  /* Precompiled s
305f4 74 61 74 65 6d 65 6e 74 73 20 77 68 69 63 68 20  tatements which 
305f5 77 65 20 6b 65 65 70 20 61 73 20 6c 6f 6e 67 20  we keep as long 
305f6 61 73 20 74 68 65 20 74 61 62 6c 65 20 69 73 0a  as the table is.
305f7 20 20 2a 2a 20 6f 70 65 6e 2e 0a 20 20 2a 2f 0a    ** open..  */.
305f8 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
305f9 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65  pFulltextStateme
305fa 6e 74 73 5b 4d 41 58 5f 53 54 4d 54 5d 3b 0a 0a  nts[MAX_STMT];..
305fb 20 20 2f 2a 20 50 72 65 63 6f 6d 70 69 6c 65 64    /* Precompiled
305fc 20 73 74 61 74 65 6d 65 6e 74 73 20 75 73 65 64   statements used
305fd 20 66 6f 72 20 73 65 67 6d 65 6e 74 20 6d 65 72   for segment mer
305fe 67 65 73 2e 20 20 57 65 20 72 75 6e 20 61 0a 20  ges.  We run a. 
305ff 20 2a 2a 20 73 65 70 61 72 61 74 65 20 73 65 6c   ** separate sel
30600 65 63 74 20 61 63 72 6f 73 73 20 74 68 65 20 6c  ect across the l
30601 65 61 66 20 6c 65 76 65 6c 20 6f 66 20 65 61 63  eaf level of eac
30602 68 20 74 72 65 65 20 62 65 69 6e 67 20 6d 65 72  h tree being mer
30603 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ged..  */.  sqli
30604 74 65 33 5f 73 74 6d 74 20 2a 70 4c 65 61 66 53  te3_stmt *pLeafS
30605 65 6c 65 63 74 53 74 6d 74 73 5b 4d 45 52 47 45  electStmts[MERGE
30606 5f 43 4f 55 4e 54 5d 3b 0a 20 20 2f 2a 20 54 68  _COUNT];.  /* Th
30607 65 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64  e statement used
30608 20 74 6f 20 70 72 65 70 61 72 65 20 70 4c 65 61   to prepare pLea
30609 66 53 65 6c 65 63 74 53 74 6d 74 73 2e 20 2a 2f  fSelectStmts. */
3060a 0a 23 64 65 66 69 6e 65 20 4c 45 41 46 5f 53 45  .#define LEAF_SE
3060b 4c 45 43 54 20 5c 0a 20 20 22 73 65 6c 65 63 74  LECT \.  "select
3060c 20 62 6c 6f 63 6b 20 66 72 6f 6d 20 25 5f 73 65   block from %_se
3060d 67 6d 65 6e 74 73 20 77 68 65 72 65 20 62 6c 6f  gments where blo
3060e 63 6b 69 64 20 62 65 74 77 65 65 6e 20 3f 20 61  ckid between ? a
3060f 6e 64 20 3f 20 6f 72 64 65 72 20 62 79 20 62 6c  nd ? order by bl
30610 6f 63 6b 69 64 22 0a 0a 20 20 2f 2a 20 54 68 65  ockid"..  /* The
30611 73 65 20 62 75 66 66 65 72 20 70 65 6e 64 69 6e  se buffer pendin
30612 67 20 69 6e 64 65 78 20 75 70 64 61 74 65 73 20  g index updates 
30613 64 75 72 69 6e 67 20 74 72 61 6e 73 61 63 74 69  during transacti
30614 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 50 65 6e 64 69  ons..  ** nPendi
30615 6e 67 44 61 74 61 20 65 73 74 69 6d 61 74 65 73  ngData estimates
30616 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 69 7a 65   the memory size
30617 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67 20   of the pending 
30618 64 61 74 61 2e 20 20 49 74 0a 20 20 2a 2a 20 64  data.  It.  ** d
30619 6f 65 73 6e 27 74 20 69 6e 63 6c 75 64 65 20 74  oesn't include t
3061a 68 65 20 68 61 73 68 2d 62 75 63 6b 65 74 20 6f  he hash-bucket o
3061b 76 65 72 68 65 61 64 2c 20 6e 6f 72 20 61 6e 79  verhead, nor any
3061c 20 6d 61 6c 6c 6f 63 0a 20 20 2a 2a 20 6f 76 65   malloc.  ** ove
3061d 72 68 65 61 64 2e 20 20 57 68 65 6e 20 6e 50 65  rhead.  When nPe
3061e 6e 64 69 6e 67 44 61 74 61 20 65 78 63 65 65 64  ndingData exceed
3061f 73 20 6b 50 65 6e 64 69 6e 67 54 68 72 65 73 68  s kPendingThresh
30620 6f 6c 64 2c 20 74 68 65 0a 20 20 2a 2a 20 62 75  old, the.  ** bu
30621 66 66 65 72 20 69 73 20 66 6c 75 73 68 65 64 20  ffer is flushed 
30622 65 76 65 6e 20 62 65 66 6f 72 65 20 74 68 65 20  even before the 
30623 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6c 6f 73  transaction clos
30624 65 73 2e 0a 20 20 2a 2a 20 70 65 6e 64 69 6e 67  es..  ** pending
30625 54 65 72 6d 73 20 73 74 6f 72 65 73 20 74 68 65  Terms stores the
30626 20 64 61 74 61 2c 20 61 6e 64 20 69 73 20 6f 6e   data, and is on
30627 6c 79 20 76 61 6c 69 64 20 77 68 65 6e 20 6e 50  ly valid when nP
30628 65 6e 64 69 6e 67 44 61 74 61 0a 20 20 2a 2a 20  endingData.  ** 
30629 69 73 20 3e 3d 30 20 28 6e 50 65 6e 64 69 6e 67  is >=0 (nPending
3062a 44 61 74 61 3c 30 20 6d 65 61 6e 73 20 70 65 6e  Data<0 means pen
3062b 64 69 6e 67 54 65 72 6d 73 20 68 61 73 20 6e 6f  dingTerms has no
3062c 74 20 62 65 65 6e 0a 20 20 2a 2a 20 69 6e 69 74  t been.  ** init
3062d 69 61 6c 69 7a 65 64 29 2e 20 20 69 50 72 65 76  ialized).  iPrev
3062e 44 6f 63 69 64 20 69 73 20 74 68 65 20 6c 61 73  Docid is the las
3062f 74 20 64 6f 63 69 64 20 77 72 69 74 74 65 6e 2c  t docid written,
30630 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 0a 20 20   used to make.  
30631 2a 2a 20 63 65 72 74 61 69 6e 20 77 65 27 72 65  ** certain we're
30632 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 20 73 6f   inserting in so
30633 72 74 65 64 20 6f 72 64 65 72 2e 0a 20 20 2a 2f  rted order..  */
30634 0a 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e 67 44  .  int nPendingD
30635 61 74 61 3b 0a 23 64 65 66 69 6e 65 20 6b 50 65  ata;.#define kPe
30636 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c 64 20 28  ndingThreshold (
30637 31 2a 31 30 32 34 2a 31 30 32 34 29 0a 20 20 73  1*1024*1024).  s
30638 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72 65  qlite_int64 iPre
30639 76 44 6f 63 69 64 3b 0a 20 20 66 74 73 33 48 61  vDocid;.  fts3Ha
3063a 73 68 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 3b  sh pendingTerms;
3063b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  .};../*.** When 
3063c 74 68 65 20 63 6f 72 65 20 77 61 6e 74 73 20 74  the core wants t
3063d 6f 20 64 6f 20 61 20 71 75 65 72 79 2c 20 69 74  o do a query, it
3063e 20 63 72 65 61 74 65 20 61 20 63 75 72 73 6f 72   create a cursor
3063f 20 75 73 69 6e 67 20 61 0a 2a 2a 20 63 61 6c 6c   using a.** call
30640 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 69 73   to xOpen.  This
30641 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6e   structure is an
30642 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 61 20 63   instance of a c
30643 75 72 73 6f 72 2e 20 20 49 74 0a 2a 2a 20 69 73  ursor.  It.** is
30644 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 78 43   destroyed by xC
30645 6c 6f 73 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  lose..*/.typedef
30646 20 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78 74   struct fulltext
30647 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69  _cursor {.  sqli
30648 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
30649 62 61 73 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  base;        /* 
3064a 42 61 73 65 20 63 6c 61 73 73 20 75 73 65 64 20  Base class used 
3064b 62 79 20 53 51 4c 69 74 65 20 63 6f 72 65 20 2a  by SQLite core *
3064c 2f 0a 20 20 51 75 65 72 79 54 79 70 65 20 69 43  /.  QueryType iC
3064d 75 72 73 6f 72 54 79 70 65 3b 20 20 20 20 20 20  ursorType;      
3064e 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
3064f 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
30650 66 6f 2e 69 64 78 4e 75 6d 20 2a 2f 0a 20 20 73  fo.idxNum */.  s
30651 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
30652 6d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mt;             
30653 2f 2a 20 50 72 65 70 61 72 65 64 20 73 74 61 74  /* Prepared stat
30654 65 6d 65 6e 74 20 69 6e 20 75 73 65 20 62 79 20  ement in use by 
30655 74 68 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  the cursor */.  
30656 69 6e 74 20 65 6f 66 3b 20 20 20 20 20 20 20 20  int eof;        
30657 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30658 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74 20 45   /* True if at E
30659 6e 64 20 4f 66 20 52 65 73 75 6c 74 73 20 2a 2f  nd Of Results */
3065a 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 45 78  .  Fts3Expr *pEx
3065b 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pr;             
3065c 20 20 20 20 2f 2a 20 50 61 72 73 65 64 20 4d 41      /* Parsed MA
3065d 54 43 48 20 71 75 65 72 79 20 73 74 72 69 6e 67  TCH query string
3065e 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 73 6e   */.  Snippet sn
3065f 69 70 70 65 74 3b 20 20 20 20 20 20 20 20 20 20  ippet;          
30660 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 64         /* Cached
30661 20 73 6e 69 70 70 65 74 20 66 6f 72 20 74 68 65   snippet for the
30662 20 63 75 72 72 65 6e 74 20 72 6f 77 20 2a 2f 0a   current row */.
30663 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
30664 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30665 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 62 65 69     /* Column bei
30666 6e 67 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20  ng searched */. 
30667 20 44 61 74 61 42 75 66 66 65 72 20 72 65 73 75   DataBuffer resu
30668 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  lt;             
30669 20 20 2f 2a 20 44 6f 63 6c 69 73 74 20 72 65 73    /* Doclist res
3066a 75 6c 74 73 20 66 72 6f 6d 20 66 75 6c 6c 74 65  ults from fullte
3066b 78 74 51 75 65 72 79 20 2a 2f 0a 20 20 44 4c 52  xtQuery */.  DLR
3066c 65 61 64 65 72 20 72 65 61 64 65 72 3b 20 20 20  eader reader;   
3066d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3066e 20 52 65 73 75 6c 74 20 72 65 61 64 65 72 20 69   Result reader i
3066f 66 20 72 65 73 75 6c 74 20 6e 6f 74 20 65 6d 70  f result not emp
30670 74 79 20 2a 2f 0a 7d 20 66 75 6c 6c 74 65 78 74  ty */.} fulltext
30671 5f 63 75 72 73 6f 72 3b 0a 0a 73 74 61 74 69 63  _cursor;..static
30672 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
30673 63 75 72 73 6f 72 5f 76 74 61 62 28 66 75 6c 6c  cursor_vtab(full
30674 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 29 7b  text_cursor *c){
30675 0a 20 20 72 65 74 75 72 6e 20 28 66 75 6c 6c 74  .  return (fullt
30676 65 78 74 5f 76 74 61 62 20 2a 29 20 63 2d 3e 62  ext_vtab *) c->b
30677 61 73 65 2e 70 56 74 61 62 3b 0a 7d 0a 0a 73 74  ase.pVtab;.}..st
30678 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
30679 65 33 5f 6d 6f 64 75 6c 65 20 66 74 73 33 4d 6f  e3_module fts3Mo
3067a 64 75 6c 65 3b 20 20 20 2f 2a 20 66 6f 72 77 61  dule;   /* forwa
3067b 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a  rd declaration *
3067c 2f 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 64  /../* Return a d
3067d 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65 72  ynamically gener
3067e 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6f  ated statement o
3067f 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20 20  f the form. *   
30680 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 63 6f  insert into %_co
30681 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c 20 2e 2e  ntent (docid, ..
30682 2e 29 20 76 61 6c 75 65 73 20 28 3f 2c 20 2e 2e  .) values (?, ..
30683 2e 29 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f  .). */.static co
30684 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65 6e  nst char *conten
30685 74 49 6e 73 65 72 74 53 74 61 74 65 6d 65 6e 74  tInsertStatement
30686 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
30687 76 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66 66  v){.  StringBuff
30688 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 69 3b 0a  er sb;.  int i;.
30689 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66  .  initStringBuf
3068a 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70 70 65  fer(&sb);.  appe
3068b 6e 64 28 26 73 62 2c 20 22 69 6e 73 65 72 74 20  nd(&sb, "insert 
3068c 69 6e 74 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 28  into %_content (
3068d 64 6f 63 69 64 2c 20 22 29 3b 0a 20 20 61 70 70  docid, ");.  app
3068e 65 6e 64 4c 69 73 74 28 26 73 62 2c 20 76 2d 3e  endList(&sb, v->
3068f 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43 6f  nColumn, v->azCo
30690 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ntentColumn);.  
30691 61 70 70 65 6e 64 28 26 73 62 2c 20 22 29 20 76  append(&sb, ") v
30692 61 6c 75 65 73 20 28 3f 22 29 3b 0a 20 20 66 6f  alues (?");.  fo
30693 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c  r(i=0; i<v->nCol
30694 75 6d 6e 3b 20 2b 2b 69 29 0a 20 20 20 20 61 70  umn; ++i).    ap
30695 70 65 6e 64 28 26 73 62 2c 20 22 2c 20 3f 22 29  pend(&sb, ", ?")
30696 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20  ;.  append(&sb, 
30697 22 29 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  ")");.  return s
30698 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61 28  tringBufferData(
30699 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75  &sb);.}../* Retu
3069a 72 6e 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  rn a dynamically
3069b 20 67 65 6e 65 72 61 74 65 64 20 73 74 61 74 65   generated state
3069c 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d  ment of the form
3069d 0a 20 2a 20 20 20 73 65 6c 65 63 74 20 3c 63 6f  . *   select <co
3069e 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 73 3e 20 66  ntent columns> f
3069f 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68  rom %_content wh
306a0 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 0a 20 2a  ere docid = ?. *
306a1 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
306a2 68 61 72 20 2a 63 6f 6e 74 65 6e 74 53 65 6c 65  har *contentSele
306a3 63 74 53 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c  ctStatement(full
306a4 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20  text_vtab *v){. 
306a5 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62   StringBuffer sb
306a6 3b 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75  ;.  initStringBu
306a7 66 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70 70  ffer(&sb);.  app
306a8 65 6e 64 28 26 73 62 2c 20 22 53 45 4c 45 43 54  end(&sb, "SELECT
306a9 20 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69 73   ");.  appendLis
306aa 74 28 26 73 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d  t(&sb, v->nColum
306ab 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  n, v->azContentC
306ac 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70 65 6e 64  olumn);.  append
306ad 28 26 73 62 2c 20 22 20 46 52 4f 4d 20 25 5f 63  (&sb, " FROM %_c
306ae 6f 6e 74 65 6e 74 20 57 48 45 52 45 20 64 6f 63  ontent WHERE doc
306af 69 64 20 3d 20 3f 22 29 3b 0a 20 20 72 65 74 75  id = ?");.  retu
306b0 72 6e 20 73 74 72 69 6e 67 42 75 66 66 65 72 44  rn stringBufferD
306b1 61 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 20  ata(&sb);.}../* 
306b2 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d 69 63  Return a dynamic
306b3 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 73  ally generated s
306b4 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 65 20  tatement of the 
306b5 66 6f 72 6d 0a 20 2a 20 20 20 75 70 64 61 74 65  form. *   update
306b6 20 25 5f 63 6f 6e 74 65 6e 74 20 73 65 74 20 5b   %_content set [
306b7 63 6f 6c 5f 30 5d 20 3d 20 3f 2c 20 5b 63 6f 6c  col_0] = ?, [col
306b8 5f 31 5d 20 3d 20 3f 2c 20 2e 2e 2e 0a 20 2a 20  _1] = ?, .... * 
306b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
306ba 20 20 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d     where docid =
306bb 20 3f 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f   ?. */.static co
306bc 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65 6e  nst char *conten
306bd 74 55 70 64 61 74 65 53 74 61 74 65 6d 65 6e 74  tUpdateStatement
306be 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
306bf 76 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66 66  v){.  StringBuff
306c0 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 69 3b 0a  er sb;.  int i;.
306c1 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66  .  initStringBuf
306c2 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70 70 65  fer(&sb);.  appe
306c3 6e 64 28 26 73 62 2c 20 22 75 70 64 61 74 65 20  nd(&sb, "update 
306c4 25 5f 63 6f 6e 74 65 6e 74 20 73 65 74 20 22 29  %_content set ")
306c5 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76  ;.  for(i=0; i<v
306c6 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 20  ->nColumn; ++i) 
306c7 7b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b  {.    if( i>0 ){
306c8 0a 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73  .      append(&s
306c9 62 2c 20 22 2c 20 22 29 3b 0a 20 20 20 20 7d 0a  b, ", ");.    }.
306ca 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20      append(&sb, 
306cb 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  v->azContentColu
306cc 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20 61 70 70 65  mn[i]);.    appe
306cd 6e 64 28 26 73 62 2c 20 22 20 3d 20 3f 22 29 3b  nd(&sb, " = ?");
306ce 0a 20 20 7d 0a 20 20 61 70 70 65 6e 64 28 26 73  .  }.  append(&s
306cf 62 2c 20 22 20 77 68 65 72 65 20 64 6f 63 69 64  b, " where docid
306d0 20 3d 20 3f 22 29 3b 0a 20 20 72 65 74 75 72 6e   = ?");.  return
306d1 20 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74   stringBufferDat
306d2 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 20 50 75  a(&sb);.}../* Pu
306d3 74 73 20 61 20 66 72 65 73 68 6c 79 2d 70 72 65  ts a freshly-pre
306d4 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
306d5 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 69 53  determined by iS
306d6 74 6d 74 20 69 6e 20 2a 70 70 53 74 6d 74 2e 0a  tmt in *ppStmt..
306d7 2a 2a 20 49 66 20 74 68 65 20 69 6e 64 69 63 61  ** If the indica
306d8 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 68 61  ted statement ha
306d9 73 20 6e 65 76 65 72 20 62 65 65 6e 20 70 72 65  s never been pre
306da 70 61 72 65 64 2c 20 69 74 20 69 73 20 70 72 65  pared, it is pre
306db 70 61 72 65 64 0a 2a 2a 20 61 6e 64 20 63 61 63  pared.** and cac
306dc 68 65 64 2c 20 6f 74 68 65 72 77 69 73 65 20 74  hed, otherwise t
306dd 68 65 20 63 61 63 68 65 64 20 76 65 72 73 69 6f  he cached versio
306de 6e 20 69 73 20 72 65 73 65 74 2e 0a 2a 2f 0a 73  n is reset..*/.s
306df 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 67 65  tatic int sql_ge
306e0 74 5f 73 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c  t_statement(full
306e1 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 66 75  text_vtab *v, fu
306e2 6c 6c 74 65 78 74 5f 73 74 61 74 65 6d 65 6e 74  lltext_statement
306e3 20 69 53 74 6d 74 2c 0a 20 20 20 20 20 20 20 20   iStmt,.        
306e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
306e5 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d       sqlite3_stm
306e6 74 20 2a 2a 70 70 53 74 6d 74 29 7b 0a 20 20 61  t **ppStmt){.  a
306e7 73 73 65 72 74 28 20 69 53 74 6d 74 3c 4d 41 58  ssert( iStmt<MAX
306e8 5f 53 54 4d 54 20 29 3b 0a 20 20 69 66 28 20 76  _STMT );.  if( v
306e9 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65  ->pFulltextState
306ea 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 3d 3d 4e 55  ments[iStmt]==NU
306eb 4c 4c 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  LL ){.    const 
306ec 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20  char *zStmt;.   
306ed 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73 77 69   int rc;.    swi
306ee 74 63 68 28 20 69 53 74 6d 74 20 29 7b 0a 20 20  tch( iStmt ){.  
306ef 20 20 20 20 63 61 73 65 20 43 4f 4e 54 45 4e 54      case CONTENT
306f0 5f 49 4e 53 45 52 54 5f 53 54 4d 54 3a 0a 20 20  _INSERT_STMT:.  
306f1 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 6f        zStmt = co
306f2 6e 74 65 6e 74 49 6e 73 65 72 74 53 74 61 74 65  ntentInsertState
306f3 6d 65 6e 74 28 76 29 3b 20 62 72 65 61 6b 3b 0a  ment(v); break;.
306f4 20 20 20 20 20 20 63 61 73 65 20 43 4f 4e 54 45        case CONTE
306f5 4e 54 5f 53 45 4c 45 43 54 5f 53 54 4d 54 3a 0a  NT_SELECT_STMT:.
306f6 20 20 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20          zStmt = 
306f7 63 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74 61  contentSelectSta
306f8 74 65 6d 65 6e 74 28 76 29 3b 20 62 72 65 61 6b  tement(v); break
306f9 3b 0a 20 20 20 20 20 20 63 61 73 65 20 43 4f 4e  ;.      case CON
306fa 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d 54  TENT_UPDATE_STMT
306fb 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d 74 20  :.        zStmt 
306fc 3d 20 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53  = contentUpdateS
306fd 74 61 74 65 6d 65 6e 74 28 76 29 3b 20 62 72 65  tatement(v); bre
306fe 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c  ak;.      defaul
306ff 74 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d 74  t:.        zStmt
30700 20 3d 20 66 75 6c 6c 74 65 78 74 5f 7a 53 74 61   = fulltext_zSta
30701 74 65 6d 65 6e 74 5b 69 53 74 6d 74 5d 3b 0a 20  tement[iStmt];. 
30702 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
30703 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62 2c  l_prepare(v->db,
30704 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d   v->zDb, v->zNam
30705 65 2c 20 26 76 2d 3e 70 46 75 6c 6c 74 65 78 74  e, &v->pFulltext
30706 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74  Statements[iStmt
30707 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
30708 20 20 20 20 20 20 20 20 20 20 20 20 7a 53 74 6d              zStm
30709 74 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 74 6d  t);.    if( zStm
3070a 74 20 21 3d 20 66 75 6c 6c 74 65 78 74 5f 7a 53  t != fulltext_zS
3070b 74 61 74 65 6d 65 6e 74 5b 69 53 74 6d 74 5d 29  tatement[iStmt])
3070c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28 76   sqlite3_free((v
3070d 6f 69 64 20 2a 29 20 7a 53 74 6d 74 29 3b 0a 20  oid *) zStmt);. 
3070e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
3070f 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
30710 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
30711 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
30712 33 5f 72 65 73 65 74 28 76 2d 3e 70 46 75 6c 6c  3_reset(v->pFull
30713 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69  textStatements[i
30714 53 74 6d 74 5d 29 3b 0a 20 20 20 20 69 66 28 20  Stmt]);.    if( 
30715 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
30716 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
30717 20 20 2a 70 70 53 74 6d 74 20 3d 20 76 2d 3e 70    *ppStmt = v->p
30718 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e  FulltextStatemen
30719 74 73 5b 69 53 74 6d 74 5d 3b 0a 20 20 72 65 74  ts[iStmt];.  ret
3071a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
3071b 0a 0a 2f 2a 20 4c 69 6b 65 20 73 71 6c 69 74 65  ../* Like sqlite
3071c 33 5f 73 74 65 70 28 29 2c 20 62 75 74 20 63 6f  3_step(), but co
3071d 6e 76 65 72 74 20 53 51 4c 49 54 45 5f 44 4f 4e  nvert SQLITE_DON
3071e 45 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61  E to SQLITE_OK a
3071f 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57  nd.** SQLITE_ROW
30720 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   to SQLITE_ERROR
30721 2e 20 20 55 73 65 66 75 6c 20 66 6f 72 20 73 74  .  Useful for st
30722 61 74 65 6d 65 6e 74 73 20 6c 69 6b 65 20 55 50  atements like UP
30723 44 41 54 45 2c 0a 2a 2a 20 77 68 65 72 65 20 77  DATE,.** where w
30724 65 20 65 78 70 65 63 74 20 6e 6f 20 72 65 73 75  e expect no resu
30725 6c 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lts..*/.static i
30726 6e 74 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74  nt sql_single_st
30727 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ep(sqlite3_stmt 
30728 2a 73 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  *s){.  int rc = 
30729 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
3072a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53  .  return (rc==S
3072b 51 4c 49 54 45 5f 44 4f 4e 45 29 20 3f 20 53 51  QLITE_DONE) ? SQ
3072c 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 3b 0a 7d 0a  LITE_OK : rc;.}.
3072d 0a 2f 2a 20 4c 69 6b 65 20 73 71 6c 5f 67 65 74  ./* Like sql_get
3072e 5f 73 74 61 74 65 6d 65 6e 74 28 29 2c 20 62 75  _statement(), bu
3072f 74 20 66 6f 72 20 73 70 65 63 69 61 6c 20 72 65  t for special re
30730 70 6c 69 63 61 74 65 64 20 4c 45 41 46 5f 53 45  plicated LEAF_SE
30731 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  LECT.** statemen
30732 74 73 2e 20 20 69 64 78 20 2d 31 20 69 73 20 61  ts.  idx -1 is a
30733 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 66 6f   special case fo
30734 72 20 61 6e 20 75 6e 63 61 63 68 65 64 20 76 65  r an uncached ve
30735 72 73 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20  rsion of.** the 
30736 73 74 61 74 65 6d 65 6e 74 20 28 75 73 65 64 20  statement (used 
30737 69 6e 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 20  in the optimize 
30738 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2e  implementation).
30739 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
3073a 73 29 20 57 72 69 74 65 20 76 65 72 73 69 6f 6e  s) Write version
3073b 20 66 6f 72 20 67 65 6e 65 72 69 63 20 73 74 61   for generic sta
3073c 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 68 65 6e  tements and then
3073d 20 73 68 61 72 65 0a 2a 2a 20 74 68 61 74 20 62   share.** that b
3073e 65 74 77 65 65 6e 20 74 68 65 20 63 61 63 68 65  etween the cache
3073f 64 2d 73 74 61 74 65 6d 65 6e 74 20 66 75 6e 63  d-statement func
30740 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tions..*/.static
30741 20 69 6e 74 20 73 71 6c 5f 67 65 74 5f 6c 65 61   int sql_get_lea
30742 66 5f 73 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c  f_statement(full
30743 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e  text_vtab *v, in
30744 74 20 69 64 78 2c 0a 20 20 20 20 20 20 20 20 20  t idx,.         
30745 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30746 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
30747 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 29 7b  _stmt **ppStmt){
30748 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d  .  assert( idx>=
30749 2d 31 20 26 26 20 69 64 78 3c 4d 45 52 47 45 5f  -1 && idx<MERGE_
3074a 43 4f 55 4e 54 20 29 3b 0a 20 20 69 66 28 20 69  COUNT );.  if( i
3074b 64 78 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 72 65  dx==-1 ){.    re
3074c 74 75 72 6e 20 73 71 6c 5f 70 72 65 70 61 72 65  turn sql_prepare
3074d 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20  (v->db, v->zDb, 
3074e 76 2d 3e 7a 4e 61 6d 65 2c 20 70 70 53 74 6d 74  v->zName, ppStmt
3074f 2c 20 4c 45 41 46 5f 53 45 4c 45 43 54 29 3b 0a  , LEAF_SELECT);.
30750 20 20 7d 65 6c 73 65 20 69 66 28 20 76 2d 3e 70    }else if( v->p
30751 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b  LeafSelectStmts[
30752 69 64 78 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20  idx]==NULL ){.  
30753 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 70    int rc = sql_p
30754 72 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d  repare(v->db, v-
30755 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20  >zDb, v->zName, 
30756 26 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53  &v->pLeafSelectS
30757 74 6d 74 73 5b 69 64 78 5d 2c 0a 20 20 20 20 20  tmts[idx],.     
30758 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30759 20 20 20 20 4c 45 41 46 5f 53 45 4c 45 43 54 29      LEAF_SELECT)
3075a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
3075b 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3075c 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20   rc;.  }else{.  
3075d 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
3075e 65 33 5f 72 65 73 65 74 28 76 2d 3e 70 4c 65 61  e3_reset(v->pLea
3075f 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78  fSelectStmts[idx
30760 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  ]);.    if( rc!=
30761 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
30762 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70  rn rc;.  }..  *p
30763 70 53 74 6d 74 20 3d 20 76 2d 3e 70 4c 65 61 66  pStmt = v->pLeaf
30764 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78 5d  SelectStmts[idx]
30765 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
30766 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73 65  E_OK;.}../* inse
30767 72 74 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65 6e  rt into %_conten
30768 74 20 28 64 6f 63 69 64 2c 20 2e 2e 2e 29 20 76  t (docid, ...) v
30769 61 6c 75 65 73 20 28 5b 64 6f 63 69 64 5d 2c 20  alues ([docid], 
3076a 5b 70 56 61 6c 75 65 73 5d 29 0a 2a 2a 20 49 66  [pValues]).** If
3076b 20 74 68 65 20 64 6f 63 69 64 20 63 6f 6e 74 61   the docid conta
3076c 69 6e 73 20 53 51 4c 20 4e 55 4c 4c 2c 20 74 68  ins SQL NULL, th
3076d 65 6e 20 61 20 75 6e 69 71 75 65 20 64 6f 63 69  en a unique doci
3076e 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 67 65 6e  d will be.** gen
3076f 65 72 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  erated..*/.stati
30770 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 69 6e  c int content_in
30771 73 65 72 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  sert(fulltext_vt
30772 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f 76  ab *v, sqlite3_v
30773 61 6c 75 65 20 2a 64 6f 63 69 64 2c 0a 20 20 20  alue *docid,.   
30774 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30775 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76         sqlite3_v
30776 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 29 7b  alue **pValues){
30777 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
30778 2a 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  *s;.  int i;.  i
30779 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
3077a 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e  statement(v, CON
3077b 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54 4d 54  TENT_INSERT_STMT
3077c 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
3077d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3077e 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
3077f 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c  sqlite3_bind_val
30780 75 65 28 73 2c 20 31 2c 20 64 6f 63 69 64 29 3b  ue(s, 1, docid);
30781 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
30782 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
30783 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
30784 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29  v->nColumn; ++i)
30785 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
30786 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 2c  e3_bind_value(s,
30787 20 32 2b 69 2c 20 70 56 61 6c 75 65 73 5b 69 5d   2+i, pValues[i]
30788 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
30789 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3078a 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  n rc;.  }..  ret
3078b 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73  urn sql_single_s
3078c 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 75 70  tep(s);.}../* up
3078d 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74 20 73  date %_content s
3078e 65 74 20 63 6f 6c 30 20 3d 20 70 56 61 6c 75 65  et col0 = pValue
3078f 73 5b 30 5d 2c 20 63 6f 6c 31 20 3d 20 70 56 61  s[0], col1 = pVa
30790 6c 75 65 73 5b 31 5d 2c 20 2e 2e 2e 0a 20 2a 20  lues[1], .... * 
30791 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30792 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20 5b   where docid = [
30793 69 44 6f 63 69 64 5d 20 2a 2f 0a 73 74 61 74 69  iDocid] */.stati
30794 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 75 70  c int content_up
30795 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f 76 74  date(fulltext_vt
30796 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f 76  ab *v, sqlite3_v
30797 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 2c 0a  alue **pValues,.
30798 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30799 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3079a 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a  _int64 iDocid){.
3079b 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
3079c 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  s;.  int i;.  in
3079d 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73  t rc = sql_get_s
3079e 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e 54  tatement(v, CONT
3079f 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d 54 2c  ENT_UPDATE_STMT,
307a0 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d   &s);.  if( rc!=
307a1 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
307a2 72 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69 3d  rn rc;..  for(i=
307a3 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b  0; i<v->nColumn;
307a4 20 2b 2b 69 29 7b 0a 20 20 20 20 72 63 20 3d 20   ++i){.    rc = 
307a5 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c  sqlite3_bind_val
307a6 75 65 28 73 2c 20 31 2b 69 2c 20 70 56 61 6c 75  ue(s, 1+i, pValu
307a7 65 73 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20  es[i]);.    if( 
307a8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
307a9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
307aa 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
307ab 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2b 76  ind_int64(s, 1+v
307ac 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69 44 6f 63 69  ->nColumn, iDoci
307ad 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
307ae 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
307af 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73   rc;..  return s
307b0 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
307b1 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
307b2 64 20 66 72 65 65 53 74 72 69 6e 67 41 72 72 61  d freeStringArra
307b3 79 28 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20 63  y(int nString, c
307b4 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 53 74 72  onst char **pStr
307b5 69 6e 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 0a  ing){.  int i;..
307b6 20 20 66 6f 72 20 28 69 3d 30 20 3b 20 69 20 3c    for (i=0 ; i <
307b7 20 6e 53 74 72 69 6e 67 20 3b 20 2b 2b 69 29 20   nString ; ++i) 
307b8 7b 0a 20 20 20 20 69 66 28 20 70 53 74 72 69 6e  {.    if( pStrin
307b9 67 5b 69 5d 21 3d 4e 55 4c 4c 20 29 20 73 71 6c  g[i]!=NULL ) sql
307ba 69 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20  ite3_free((void 
307bb 2a 29 20 70 53 74 72 69 6e 67 5b 69 5d 29 3b 0a  *) pString[i]);.
307bc 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
307bd 65 65 28 28 76 6f 69 64 20 2a 29 20 70 53 74 72  ee((void *) pStr
307be 69 6e 67 29 3b 0a 7d 0a 0a 2f 2a 20 73 65 6c 65  ing);.}../* sele
307bf 63 74 20 2a 20 66 72 6f 6d 20 25 5f 63 6f 6e 74  ct * from %_cont
307c0 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64 20  ent where docid 
307c1 3d 20 5b 69 44 6f 63 69 64 5d 0a 20 2a 20 54 68  = [iDocid]. * Th
307c2 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 64 65  e caller must de
307c3 6c 65 74 65 20 74 68 65 20 72 65 74 75 72 6e 65  lete the returne
307c4 64 20 61 72 72 61 79 20 61 6e 64 20 61 6c 6c 20  d array and all 
307c5 73 74 72 69 6e 67 73 20 69 6e 20 69 74 2e 0a 20  strings in it.. 
307c6 2a 20 6e 75 6c 6c 20 66 69 65 6c 64 73 20 77 69  * null fields wi
307c7 6c 6c 20 62 65 20 4e 55 4c 4c 20 69 6e 20 74 68  ll be NULL in th
307c8 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61 79  e returned array
307c9 2e 0a 20 2a 0a 20 2a 20 54 4f 44 4f 3a 20 50 65  .. *. * TODO: Pe
307ca 72 68 61 70 73 20 77 65 20 73 68 6f 75 6c 64 20  rhaps we should 
307cb 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 2f 6c  return pointer/l
307cc 65 6e 67 74 68 20 73 74 72 69 6e 67 73 20 68 65  ength strings he
307cd 72 65 20 66 6f 72 20 63 6f 6e 73 69 73 74 65 6e  re for consisten
307ce 63 79 0a 20 2a 20 77 69 74 68 20 6f 74 68 65 72  cy. * with other
307cf 20 63 6f 64 65 20 77 68 69 63 68 20 75 73 65 73   code which uses
307d0 20 70 6f 69 6e 74 65 72 2f 6c 65 6e 67 74 68 2e   pointer/length.
307d1 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63   */.static int c
307d2 6f 6e 74 65 6e 74 5f 73 65 6c 65 63 74 28 66 75  ontent_select(fu
307d3 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
307d4 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
307d5 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cid,.           
307d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
307d7 6f 6e 73 74 20 63 68 61 72 20 2a 2a 2a 70 56 61  onst char ***pVa
307d8 6c 75 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33  lues){.  sqlite3
307d9 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 63 6f 6e 73  _stmt *s;.  cons
307da 74 20 63 68 61 72 20 2a 2a 76 61 6c 75 65 73 3b  t char **values;
307db 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
307dc 72 63 3b 0a 0a 20 20 2a 70 56 61 6c 75 65 73 20  rc;..  *pValues 
307dd 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 72 63 20 3d 20  = NULL;..  rc = 
307de 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
307df 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 53 45 4c  t(v, CONTENT_SEL
307e0 45 43 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ECT_STMT, &s);. 
307e1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
307e2 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
307e3 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
307e4 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c  bind_int64(s, 1,
307e5 20 69 44 6f 63 69 64 29 3b 0a 20 20 69 66 28 20   iDocid);.  if( 
307e6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
307e7 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
307e8 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
307e9 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
307ea 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
307eb 6e 20 72 63 3b 0a 0a 20 20 76 61 6c 75 65 73 20  n rc;..  values 
307ec 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  = (const char **
307ed 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ) sqlite3_malloc
307ee 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 73 69  (v->nColumn * si
307ef 7a 65 6f 66 28 63 6f 6e 73 74 20 63 68 61 72 20  zeof(const char 
307f0 2a 29 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  *));.  for(i=0; 
307f1 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b  i<v->nColumn; ++
307f2 69 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  i){.    if( sqli
307f3 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
307f4 73 2c 20 69 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  s, i)==SQLITE_NU
307f5 4c 4c 20 29 7b 0a 20 20 20 20 20 20 76 61 6c 75  LL ){.      valu
307f6 65 73 5b 69 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20  es[i] = NULL;.  
307f7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76    }else{.      v
307f8 61 6c 75 65 73 5b 69 5d 20 3d 20 73 74 72 69 6e  alues[i] = strin
307f9 67 5f 64 75 70 28 28 63 68 61 72 2a 29 73 71 6c  g_dup((char*)sql
307fa 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
307fb 28 73 2c 20 69 29 29 3b 0a 20 20 20 20 7d 0a 20  (s, i));.    }. 
307fc 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65   }..  /* We expe
307fd 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e  ct only one row.
307fe 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74    We must execut
307ff 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65  e another sqlite
30800 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f  3_step().   * to
30801 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74   complete the it
30802 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69  eration; otherwi
30803 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c  se the table wil
30804 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e  l remain locked.
30805 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
30806 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
30807 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
30808 45 20 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65  E ){.    *pValue
30809 73 20 3d 20 76 61 6c 75 65 73 3b 0a 20 20 20 20  s = values;.    
3080a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3080b 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 53 74 72  ;.  }..  freeStr
3080c 69 6e 67 41 72 72 61 79 28 76 2d 3e 6e 43 6f 6c  ingArray(v->nCol
3080d 75 6d 6e 2c 20 76 61 6c 75 65 73 29 3b 0a 20 20  umn, values);.  
3080e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
3080f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 63   delete from %_c
30810 6f 6e 74 65 6e 74 20 77 68 65 72 65 20 64 6f 63  ontent where doc
30811 69 64 20 3d 20 5b 69 44 6f 63 69 64 20 5d 20 2a  id = [iDocid ] *
30812 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e  /.static int con
30813 74 65 6e 74 5f 64 65 6c 65 74 65 28 66 75 6c 6c  tent_delete(full
30814 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
30815 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
30816 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  d){.  sqlite3_st
30817 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20  mt *s;.  int rc 
30818 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
30819 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 44  ent(v, CONTENT_D
3081a 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73 29 3b  ELETE_STMT, &s);
3081b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3081c 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
3081d 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
3081e 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20  3_bind_int64(s, 
3081f 31 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 69 66  1, iDocid);.  if
30820 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
30821 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
30822 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c  return sql_singl
30823 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a  e_step(s);.}../*
30824 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   Returns SQLITE_
30825 52 4f 57 20 69 66 20 61 6e 79 20 72 6f 77 73 20  ROW if any rows 
30826 65 78 69 73 74 20 69 6e 20 25 5f 63 6f 6e 74 65  exist in %_conte
30827 6e 74 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  nt, SQLITE_DONE 
30828 69 66 0a 2a 2a 20 6e 6f 20 72 6f 77 73 20 65 78  if.** no rows ex
30829 69 73 74 2c 20 61 6e 64 20 61 6e 79 20 65 72 72  ist, and any err
3082a 6f 72 20 69 6e 20 63 61 73 65 20 6f 66 20 66 61  or in case of fa
3082b 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ilure..*/.static
3082c 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 65 78 69   int content_exi
3082d 73 74 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  sts(fulltext_vta
3082e 62 20 2a 76 29 7b 0a 20 20 73 71 6c 69 74 65 33  b *v){.  sqlite3
3082f 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
30830 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
30831 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e  tement(v, CONTEN
30832 54 5f 45 58 49 53 54 53 5f 53 54 4d 54 2c 20 26  T_EXISTS_STMT, &
30833 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
30834 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
30835 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
30836 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
30837 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
30838 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OW ) return rc;.
30839 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74 20  .  /* We expect 
3083a 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57  only one row.  W
3083b 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20 61  e must execute a
3083c 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f 73  nother sqlite3_s
3083d 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20 63 6f  tep().   * to co
3083e 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72 61  mplete the itera
3083f 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65 20  tion; otherwise 
30840 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20 72  the table will r
30841 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f  emain locked. */
30842 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
30843 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72  step(s);.  if( r
30844 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
30845 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52   return SQLITE_R
30846 4f 57 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  OW;.  if( rc==SQ
30847 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
30848 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
30849 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3084a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 25  /* insert into %
3084b 5f 73 65 67 6d 65 6e 74 73 20 76 61 6c 75 65 73  _segments values
3084c 20 28 5b 70 44 61 74 61 5d 29 0a 2a 2a 20 20 20   ([pData]).**   
3084d 72 65 74 75 72 6e 73 20 61 73 73 69 67 6e 65 64  returns assigned
3084e 20 62 6c 6f 63 6b 69 64 20 69 6e 20 2a 70 69 42   blockid in *piB
3084f 6c 6f 63 6b 69 64 0a 2a 2f 0a 73 74 61 74 69 63  lockid.*/.static
30850 20 69 6e 74 20 62 6c 6f 63 6b 5f 69 6e 73 65 72   int block_inser
30851 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
30852 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *v, const char *
30853 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
30854 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
30855 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
30856 5f 69 6e 74 36 34 20 2a 70 69 42 6c 6f 63 6b 69  _int64 *piBlocki
30857 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  d){.  sqlite3_st
30858 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20  mt *s;.  int rc 
30859 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
3085a 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 49 4e 53  ent(v, BLOCK_INS
3085b 45 52 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ERT_STMT, &s);. 
3085c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3085d 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
3085e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3085f 62 69 6e 64 5f 62 6c 6f 62 28 73 2c 20 31 2c 20  bind_blob(s, 1, 
30860 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 53 51  pData, nData, SQ
30861 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
30862 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
30863 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
30864 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
30865 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63  tep(s);.  if( rc
30866 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  ==SQLITE_ROW ) r
30867 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
30868 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  OR;.  if( rc!=SQ
30869 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75  LITE_DONE ) retu
3086a 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 62 6c 6f  rn rc;..  /* blo
3086b 63 6b 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20 61  ckid column is a
3086c 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f 77 69  n alias for rowi
3086d 64 2e 20 2a 2f 0a 20 20 2a 70 69 42 6c 6f 63 6b  d. */.  *piBlock
3086e 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73  id = sqlite3_las
3086f 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 76  t_insert_rowid(v
30870 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75 72 6e 20  ->db);.  return 
30871 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
30872 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73   delete from %_s
30873 65 67 6d 65 6e 74 73 0a 2a 2a 20 20 20 77 68 65  egments.**   whe
30874 72 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77 65  re blockid betwe
30875 65 6e 20 5b 69 53 74 61 72 74 42 6c 6f 63 6b 69  en [iStartBlocki
30876 64 5d 20 61 6e 64 20 5b 69 45 6e 64 42 6c 6f 63  d] and [iEndBloc
30877 6b 69 64 5d 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74  kid].**.** Delet
30878 65 73 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20  es the range of 
30879 62 6c 6f 63 6b 73 2c 20 69 6e 63 6c 75 73 69 76  blocks, inclusiv
3087a 65 2c 20 75 73 65 64 20 74 6f 20 64 65 6c 65 74  e, used to delet
3087b 65 20 74 68 65 20 62 6c 6f 63 6b 73 0a 2a 2a 20  e the blocks.** 
3087c 77 68 69 63 68 20 66 6f 72 6d 20 61 20 73 65 67  which form a seg
3087d 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ment..*/.static 
3087e 69 6e 74 20 62 6c 6f 63 6b 5f 64 65 6c 65 74 65  int block_delete
3087f 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
30880 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
30881 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
30882 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c  e_int64 iStartBl
30883 6f 63 6b 69 64 2c 20 73 71 6c 69 74 65 5f 69 6e  ockid, sqlite_in
30884 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29  t64 iEndBlockid)
30885 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
30886 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20   *s;.  int rc = 
30887 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
30888 74 28 76 2c 20 42 4c 4f 43 4b 5f 44 45 4c 45 54  t(v, BLOCK_DELET
30889 45 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  E_STMT, &s);.  i
3088a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3088b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
3088c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
3088d 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69  nd_int64(s, 1, i
3088e 53 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20  StartBlockid);. 
3088f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
30890 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
30891 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
30892 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 32 2c  bind_int64(s, 2,
30893 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20   iEndBlockid);. 
30894 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
30895 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
30896 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69  .  return sql_si
30897 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a  ngle_step(s);.}.
30898 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53 51 4c 49  ./* Returns SQLI
30899 54 45 5f 52 4f 57 20 77 69 74 68 20 2a 70 69 64  TE_ROW with *pid
3089a 78 20 73 65 74 20 74 6f 20 74 68 65 20 6d 61 78  x set to the max
3089b 69 6d 75 6d 20 73 65 67 6d 65 6e 74 20 69 64 78  imum segment idx
3089c 20 66 6f 75 6e 64 0a 2a 2a 20 61 74 20 69 4c 65   found.** at iLe
3089d 76 65 6c 2e 20 20 52 65 74 75 72 6e 73 20 53 51  vel.  Returns SQ
3089e 4c 49 54 45 5f 44 4f 4e 45 20 69 66 20 74 68 65  LITE_DONE if the
3089f 72 65 20 61 72 65 20 6e 6f 20 73 65 67 6d 65 6e  re are no segmen
308a0 74 73 20 61 74 0a 2a 2a 20 69 4c 65 76 65 6c 2e  ts at.** iLevel.
308a1 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75    Otherwise retu
308a2 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  rns an error..*/
308a3 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67 64  .static int segd
308a4 69 72 5f 6d 61 78 5f 69 6e 64 65 78 28 66 75 6c  ir_max_index(ful
308a5 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
308a6 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 2a  nt iLevel, int *
308a7 70 69 64 78 29 7b 0a 20 20 73 71 6c 69 74 65 33  pidx){.  sqlite3
308a8 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
308a9 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
308aa 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
308ab 5f 4d 41 58 5f 49 4e 44 45 58 5f 53 54 4d 54 2c  _MAX_INDEX_STMT,
308ac 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d   &s);.  if( rc!=
308ad 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
308ae 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
308af 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
308b0 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20  s, 1, iLevel);. 
308b1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
308b2 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
308b3 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
308b4 73 74 65 70 28 73 29 3b 0a 20 20 2f 2a 20 53 68  step(s);.  /* Sh
308b5 6f 75 6c 64 20 61 6c 77 61 79 73 20 67 65 74 20  ould always get 
308b6 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f 77  at least one row
308b7 20 64 75 65 20 74 6f 20 68 6f 77 20 6d 61 78 28   due to how max(
308b8 29 20 77 6f 72 6b 73 2e 20 2a 2f 0a 20 20 69 66  ) works. */.  if
308b9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
308ba 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  E ) return SQLIT
308bb 45 5f 44 4f 4e 45 3b 0a 20 20 69 66 28 20 72 63  E_DONE;.  if( rc
308bc 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  !=SQLITE_ROW ) r
308bd 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
308be 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74 20  NULL means that 
308bf 74 68 65 72 65 20 77 65 72 65 20 6e 6f 20 69 6e  there were no in
308c0 70 75 74 73 20 74 6f 20 6d 61 78 28 29 2e 20 2a  puts to max(). *
308c1 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e  /.  if( SQLITE_N
308c2 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 63 6f 6c  ULL==sqlite3_col
308c3 75 6d 6e 5f 74 79 70 65 28 73 2c 20 30 29 20 29  umn_type(s, 0) )
308c4 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
308c5 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 20 20  e3_step(s);.    
308c6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
308c7 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  OW ) return SQLI
308c8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65  TE_ERROR;.    re
308c9 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
308ca 2a 70 69 64 78 20 3d 20 73 71 6c 69 74 65 33 5f  *pidx = sqlite3_
308cb 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 30 29  column_int(s, 0)
308cc 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63  ;..  /* We expec
308cd 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20  t only one row. 
308ce 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65   We must execute
308cf 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33   another sqlite3
308d0 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20  _step().   * to 
308d1 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65  complete the ite
308d2 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73  ration; otherwis
308d3 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c  e the table will
308d4 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20   remain locked. 
308d5 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
308d6 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28  3_step(s);.  if(
308d7 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc==SQLITE_ROW 
308d8 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
308d9 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21  ERROR;.  if( rc!
308da 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72  =SQLITE_DONE ) r
308db 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 65 74 75  eturn rc;.  retu
308dc 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 7d  rn SQLITE_ROW;.}
308dd 0a 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f  ../* insert into
308de 20 25 5f 73 65 67 64 69 72 20 76 61 6c 75 65 73   %_segdir values
308df 20 28 0a 2a 2a 20 20 20 5b 69 4c 65 76 65 6c 5d   (.**   [iLevel]
308e0 2c 20 5b 69 64 78 5d 2c 0a 2a 2a 20 20 20 5b 69  , [idx],.**   [i
308e1 53 74 61 72 74 42 6c 6f 63 6b 69 64 5d 2c 20 5b  StartBlockid], [
308e2 69 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69  iLeavesEndBlocki
308e3 64 5d 2c 20 5b 69 45 6e 64 42 6c 6f 63 6b 69 64  d], [iEndBlockid
308e4 5d 2c 0a 2a 2a 20 20 20 5b 70 52 6f 6f 74 44 61  ],.**   [pRootDa
308e5 74 61 5d 0a 2a 2a 20 29 0a 2a 2f 0a 73 74 61 74  ta].** ).*/.stat
308e6 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 73 65  ic int segdir_se
308e7 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
308e8 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20  *v, int iLevel, 
308e9 69 6e 74 20 69 64 78 2c 0a 20 20 20 20 20 20 20  int idx,.       
308ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
308eb 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61  qlite_int64 iSta
308ec 72 74 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20  rtBlockid,.     
308ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
308ee 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c   sqlite_int64 iL
308ef 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69 64 2c  eavesEndBlockid,
308f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
308f1 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
308f2 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c  t64 iEndBlockid,
308f3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
308f4 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
308f5 72 20 2a 70 52 6f 6f 74 44 61 74 61 2c 20 69 6e  r *pRootData, in
308f6 74 20 6e 52 6f 6f 74 44 61 74 61 29 7b 0a 20 20  t nRootData){.  
308f7 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
308f8 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f  .  int rc = sql_
308f9 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
308fa 20 53 45 47 44 49 52 5f 53 45 54 5f 53 54 4d 54   SEGDIR_SET_STMT
308fb 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
308fc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
308fd 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
308fe 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
308ff 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a  (s, 1, iLevel);.
30900 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
30901 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
30902 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
30903 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c 20  _bind_int(s, 2, 
30904 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  idx);.  if( rc!=
30905 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
30906 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
30907 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
30908 34 28 73 2c 20 33 2c 20 69 53 74 61 72 74 42 6c  4(s, 3, iStartBl
30909 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63  ockid);.  if( rc
3090a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
3090b 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
3090c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
3090d 74 36 34 28 73 2c 20 34 2c 20 69 4c 65 61 76 65  t64(s, 4, iLeave
3090e 73 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  sEndBlockid);.  
3090f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
30910 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
30911 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
30912 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 35 2c 20  ind_int64(s, 5, 
30913 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  iEndBlockid);.  
30914 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
30915 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
30916 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
30917 69 6e 64 5f 62 6c 6f 62 28 73 2c 20 36 2c 20 70  ind_blob(s, 6, p
30918 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44  RootData, nRootD
30919 61 74 61 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ata, SQLITE_STAT
3091a 49 43 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  IC);.  if( rc!=S
3091b 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3091c 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20  n rc;..  return 
3091d 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28  sql_single_step(
3091e 73 29 3b 0a 7d 0a 0a 2f 2a 20 51 75 65 72 69 65  s);.}../* Querie
3091f 73 20 25 5f 73 65 67 64 69 72 20 66 6f 72 20 74  s %_segdir for t
30920 68 65 20 62 6c 6f 63 6b 20 73 70 61 6e 20 6f 66  he block span of
30921 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 69 6e   the segments in
30922 20 6c 65 76 65 6c 0a 2a 2a 20 69 4c 65 76 65 6c   level.** iLevel
30923 2e 20 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54  .  Returns SQLIT
30924 45 5f 44 4f 4e 45 20 69 66 20 74 68 65 72 65 20  E_DONE if there 
30925 61 72 65 20 6e 6f 20 62 6c 6f 63 6b 73 20 66 6f  are no blocks fo
30926 72 20 69 4c 65 76 65 6c 2c 0a 2a 2a 20 53 51 4c  r iLevel,.** SQL
30927 49 54 45 5f 52 4f 57 20 69 66 20 74 68 65 72 65  ITE_ROW if there
30928 20 61 72 65 20 62 6c 6f 63 6b 73 2c 20 65 6c 73   are blocks, els
30929 65 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73  e an error..*/.s
3092a 74 61 74 69 63 20 69 6e 74 20 73 65 67 64 69 72  tatic int segdir
3092b 5f 73 70 61 6e 28 66 75 6c 6c 74 65 78 74 5f 76  _span(fulltext_v
3092c 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76  tab *v, int iLev
3092d 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  el,.            
3092e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3092f 65 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72 74  e_int64 *piStart
30930 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20  Blockid,.       
30931 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30932 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
30933 45 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73  EndBlockid){.  s
30934 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
30935 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
30936 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
30937 53 45 47 44 49 52 5f 53 50 41 4e 5f 53 54 4d 54  SEGDIR_SPAN_STMT
30938 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
30939 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3093a 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
3093b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
3093c 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a  (s, 1, iLevel);.
3093d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3093e 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
3093f 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
30940 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
30941 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
30942 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
30943 44 4f 4e 45 3b 20 20 2f 2a 20 53 68 6f 75 6c 64  DONE;  /* Should
30944 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 2a 2f   never happen */
30945 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
30946 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72  E_ROW ) return r
30947 63 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 68 61  c;..  /* This ha
30948 70 70 65 6e 73 20 69 66 20 61 6c 6c 20 73 65 67  ppens if all seg
30949 6d 65 6e 74 73 20 61 74 20 74 68 69 73 20 6c 65  ments at this le
3094a 76 65 6c 20 61 72 65 20 65 6e 74 69 72 65 6c 79  vel are entirely
3094b 20 69 6e 6c 69 6e 65 2e 20 2a 2f 0a 20 20 69 66   inline. */.  if
3094c 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73  ( SQLITE_NULL==s
3094d 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
3094e 70 65 28 73 2c 20 30 29 20 29 7b 0a 20 20 20 20  pe(s, 0) ){.    
3094f 2f 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c  /* We expect onl
30950 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d  y one row.  We m
30951 75 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74  ust execute anot
30952 68 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70  her sqlite3_step
30953 28 29 0a 20 20 20 20 20 2a 20 74 6f 20 63 6f 6d  ().     * to com
30954 70 6c 65 74 65 20 74 68 65 20 69 74 65 72 61 74  plete the iterat
30955 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65 20 74  ion; otherwise t
30956 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20 72 65  he table will re
30957 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a  main locked. */.
30958 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 73 71      int rc2 = sq
30959 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
3095a 20 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49     if( rc2==SQLI
3095b 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
3095c 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
3095d 20 20 72 65 74 75 72 6e 20 72 63 32 3b 0a 20 20    return rc2;.  
3095e 7d 0a 0a 20 20 2a 70 69 53 74 61 72 74 42 6c 6f  }..  *piStartBlo
3095f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63  ckid = sqlite3_c
30960 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 30  olumn_int64(s, 0
30961 29 3b 0a 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b  );.  *piEndBlock
30962 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  id = sqlite3_col
30963 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b  umn_int64(s, 1);
30964 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74  ..  /* We expect
30965 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20   only one row.  
30966 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20  We must execute 
30967 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f  another sqlite3_
30968 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20 63  step().   * to c
30969 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72  omplete the iter
3096a 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65  ation; otherwise
3096b 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20   the table will 
3096c 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a  remain locked. *
3096d 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
3096e 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
3096f 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
30970 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
30971 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d  RROR;.  if( rc!=
30972 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65  SQLITE_DONE ) re
30973 74 75 72 6e 20 72 63 3b 0a 20 20 72 65 74 75 72  turn rc;.  retur
30974 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 7d 0a  n SQLITE_ROW;.}.
30975 0a 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 73  ./* Delete the s
30976 65 67 6d 65 6e 74 20 62 6c 6f 63 6b 73 20 61 6e  egment blocks an
30977 64 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74  d segment direct
30978 6f 72 79 20 72 65 63 6f 72 64 73 20 66 6f 72 20  ory records for 
30979 61 6c 6c 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20  all.** segments 
3097a 61 74 20 69 4c 65 76 65 6c 2e 0a 2a 2f 0a 73 74  at iLevel..*/.st
3097b 61 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f  atic int segdir_
3097c 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f  delete(fulltext_
3097d 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65  vtab *v, int iLe
3097e 76 65 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  vel){.  sqlite3_
3097f 73 74 6d 74 20 2a 73 3b 0a 20 20 73 71 6c 69 74  stmt *s;.  sqlit
30980 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c  e_int64 iStartBl
30981 6f 63 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b  ockid, iEndBlock
30982 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  id;.  int rc = s
30983 65 67 64 69 72 5f 73 70 61 6e 28 76 2c 20 69 4c  egdir_span(v, iL
30984 65 76 65 6c 2c 20 26 69 53 74 61 72 74 42 6c 6f  evel, &iStartBlo
30985 63 6b 69 64 2c 20 26 69 45 6e 64 42 6c 6f 63 6b  ckid, &iEndBlock
30986 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
30987 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 63 21  QLITE_ROW && rc!
30988 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72  =SQLITE_DONE ) r
30989 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 66 28  eturn rc;..  if(
3098a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc==SQLITE_ROW 
3098b 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f 63  ){.    rc = bloc
3098c 6b 5f 64 65 6c 65 74 65 28 76 2c 20 69 53 74 61  k_delete(v, iSta
3098d 72 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e 64 42  rtBlockid, iEndB
3098e 6c 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28  lockid);.    if(
3098f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
30990 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
30991 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65  .  /* Delete the
30992 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
30993 72 79 20 69 74 73 65 6c 66 2e 20 2a 2f 0a 20 20  ry itself. */.  
30994 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
30995 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
30996 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73  _DELETE_STMT, &s
30997 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
30998 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
30999 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
3099a 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
3099b 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20  , 1, iLevel);.  
3099c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3099d 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
3099e 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e    return sql_sin
3099f 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a  gle_step(s);.}..
309a0 2f 2a 20 44 65 6c 65 74 65 20 65 6e 74 69 72 65  /* Delete entire
309a1 20 66 74 73 20 69 6e 64 65 78 2c 20 53 51 4c 49   fts index, SQLI
309a2 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
309a3 2c 20 72 65 6c 65 76 61 6e 74 20 65 72 72 6f 72  , relevant error
309a4 20 6f 6e 0a 2a 2a 20 66 61 69 6c 75 72 65 2e 0a   on.** failure..
309a5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
309a6 67 64 69 72 5f 64 65 6c 65 74 65 5f 61 6c 6c 28  gdir_delete_all(
309a7 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
309a8 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
309a9 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t *s;.  int rc =
309aa 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
309ab 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 44 45 4c  nt(v, SEGDIR_DEL
309ac 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73  ETE_ALL_STMT, &s
309ad 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
309ae 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
309af 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f  rc;..  rc = sql_
309b0 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a  single_step(s);.
309b1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
309b2 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
309b3 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74  ..  rc = sql_get
309b4 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c  _statement(v, BL
309b5 4f 43 4b 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53  OCK_DELETE_ALL_S
309b6 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
309b7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
309b8 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65  return rc;..  re
309b9 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f  turn sql_single_
309ba 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 52  step(s);.}../* R
309bb 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
309bc 20 77 69 74 68 20 2a 70 6e 53 65 67 6d 65 6e 74   with *pnSegment
309bd 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d  s set to the num
309be 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
309bf 6e 0a 2a 2a 20 25 5f 73 65 67 64 69 72 20 61 6e  n.** %_segdir an
309c0 64 20 2a 70 69 4d 61 78 4c 65 76 65 6c 20 73 65  d *piMaxLevel se
309c1 74 20 74 6f 20 74 68 65 20 68 69 67 68 65 73 74  t to the highest
309c2 20 6c 65 76 65 6c 20 77 68 69 63 68 20 68 61 73   level which has
309c3 20 61 0a 2a 2a 20 73 65 67 6d 65 6e 74 2e 20 20   a.** segment.  
309c4 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e  Otherwise return
309c5 73 20 74 68 65 20 53 51 4c 69 74 65 20 65 72 72  s the SQLite err
309c6 6f 72 20 77 68 69 63 68 20 63 61 75 73 65 64 20  or which caused 
309c7 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  failure..*/.stat
309c8 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 63 6f  ic int segdir_co
309c9 75 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  unt(fulltext_vta
309ca 62 20 2a 76 2c 20 69 6e 74 20 2a 70 6e 53 65 67  b *v, int *pnSeg
309cb 6d 65 6e 74 73 2c 20 69 6e 74 20 2a 70 69 4d 61  ments, int *piMa
309cc 78 4c 65 76 65 6c 29 7b 0a 20 20 73 71 6c 69 74  xLevel){.  sqlit
309cd 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e  e3_stmt *s;.  in
309ce 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73  t rc = sql_get_s
309cf 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44  tatement(v, SEGD
309d0 49 52 5f 43 4f 55 4e 54 5f 53 54 4d 54 2c 20 26  IR_COUNT_STMT, &
309d1 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
309d2 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
309d3 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
309d4 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
309d5 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20  /* TODO(shess): 
309d6 54 68 69 73 20 63 61 73 65 20 73 68 6f 75 6c 64  This case should
309d7 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65   not be possible
309d8 3f 20 20 53 68 6f 75 6c 64 20 73 74 72 6f 6e 67  ?  Should strong
309d9 65 72 0a 20 20 2a 2a 20 6d 65 61 73 75 72 65 73  er.  ** measures
309da 20 62 65 20 74 61 6b 65 6e 20 69 66 20 69 74 20   be taken if it 
309db 68 61 70 70 65 6e 73 3f 0a 20 20 2a 2f 0a 20 20  happens?.  */.  
309dc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
309dd 4f 4e 45 20 29 7b 0a 20 20 20 20 2a 70 6e 53 65  ONE ){.    *pnSe
309de 67 6d 65 6e 74 73 20 3d 20 30 3b 0a 20 20 20 20  gments = 0;.    
309df 2a 70 69 4d 61 78 4c 65 76 65 6c 20 3d 20 30 3b  *piMaxLevel = 0;
309e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
309e1 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
309e2 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc!=SQLITE_ROW 
309e3 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
309e4 2a 70 6e 53 65 67 6d 65 6e 74 73 20 3d 20 73 71  *pnSegments = sq
309e5 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
309e6 28 73 2c 20 30 29 3b 0a 20 20 2a 70 69 4d 61 78  (s, 0);.  *piMax
309e7 4c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 5f  Level = sqlite3_
309e8 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 31 29  column_int(s, 1)
309e9 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63  ;..  /* We expec
309ea 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20  t only one row. 
309eb 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65   We must execute
309ec 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33   another sqlite3
309ed 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20  _step().   * to 
309ee 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65  complete the ite
309ef 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73  ration; otherwis
309f0 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c  e the table will
309f1 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20   remain locked. 
309f2 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
309f3 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28  3_step(s);.  if(
309f4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
309f5 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
309f6 5f 4f 4b 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  _OK;.  if( rc==S
309f7 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
309f8 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
309f9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
309fa 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
309fb 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d  clearPendingTerm
309fc 73 28 29 20 69 73 20 66 61 72 20 64 6f 77 6e 20  s() is far down 
309fd 74 68 65 20 66 69 6c 65 20 62 65 63 61 75 73 65  the file because
309fe 0a 2a 2a 20 77 72 69 74 65 5a 65 72 6f 53 65 67  .** writeZeroSeg
309ff 6d 65 6e 74 28 29 20 69 73 20 66 61 72 20 64 6f  ment() is far do
30a00 77 6e 20 74 68 65 20 66 69 6c 65 20 62 65 63 61  wn the file beca
30a01 75 73 65 20 4c 65 61 66 57 72 69 74 65 72 20 69  use LeafWriter i
30a02 73 20 66 61 72 0a 2a 2a 20 64 6f 77 6e 20 74 68  s far.** down th
30a03 65 20 66 69 6c 65 2e 20 20 43 6f 6e 73 69 64 65  e file.  Conside
30a04 72 20 72 65 66 61 63 74 6f 72 69 6e 67 20 74 68  r refactoring th
30a05 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 74  e code to move t
30a06 68 65 20 6e 6f 6e 2d 76 74 61 62 0a 2a 2a 20 63  he non-vtab.** c
30a07 6f 64 65 20 61 62 6f 76 65 20 74 68 65 20 76 74  ode above the vt
30a08 61 62 20 63 6f 64 65 20 73 6f 20 74 68 61 74 20  ab code so that 
30a09 77 65 20 64 6f 6e 27 74 20 6e 65 65 64 20 74 68  we don't need th
30a0a 69 73 20 66 6f 72 77 61 72 64 0a 2a 2a 20 72 65  is forward.** re
30a0b 66 65 72 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74  ference..*/.stat
30a0c 69 63 20 69 6e 74 20 63 6c 65 61 72 50 65 6e 64  ic int clearPend
30a0d 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78  ingTerms(fulltex
30a0e 74 5f 76 74 61 62 20 2a 76 29 3b 0a 0a 2f 2a 0a  t_vtab *v);../*.
30a0f 2a 2a 20 46 72 65 65 20 74 68 65 20 6d 65 6d 6f  ** Free the memo
30a10 72 79 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 61  ry used to conta
30a11 69 6e 20 61 20 66 75 6c 6c 74 65 78 74 5f 76 74  in a fulltext_vt
30a12 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ab structure..*/
30a13 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 75 6c  .static void ful
30a14 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74 72  ltext_vtab_destr
30a15 6f 79 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  oy(fulltext_vtab
30a16 20 2a 76 29 7b 0a 20 20 69 6e 74 20 69 53 74 6d   *v){.  int iStm
30a17 74 2c 20 69 3b 0a 0a 20 20 46 54 53 54 52 41 43  t, i;..  FTSTRAC
30a18 45 28 28 22 46 54 53 33 20 44 65 73 74 72 6f 79  E(("FTS3 Destroy
30a19 20 25 70 5c 6e 22 2c 20 76 29 29 3b 0a 20 20 66   %p\n", v));.  f
30a1a 6f 72 28 20 69 53 74 6d 74 3d 30 3b 20 69 53 74  or( iStmt=0; iSt
30a1b 6d 74 3c 4d 41 58 5f 53 54 4d 54 3b 20 69 53 74  mt<MAX_STMT; iSt
30a1c 6d 74 2b 2b 20 29 7b 0a 20 20 20 20 69 66 28 20  mt++ ){.    if( 
30a1d 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74  v->pFulltextStat
30a1e 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 21 3d 4e  ements[iStmt]!=N
30a1f 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ULL ){.      sql
30a20 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 76 2d  ite3_finalize(v-
30a21 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
30a22 65 6e 74 73 5b 69 53 74 6d 74 5d 29 3b 0a 20 20  ents[iStmt]);.  
30a23 20 20 20 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74      v->pFulltext
30a24 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74  Statements[iStmt
30a25 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a  ] = NULL;.    }.
30a26 20 20 7d 0a 0a 20 20 66 6f 72 28 20 69 3d 30 3b    }..  for( i=0;
30a27 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20   i<MERGE_COUNT; 
30a28 69 2b 2b 20 29 7b 0a 20 20 20 20 69 66 28 20 76  i++ ){.    if( v
30a29 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d  ->pLeafSelectStm
30a2a 74 73 5b 69 5d 21 3d 4e 55 4c 4c 20 29 7b 0a 20  ts[i]!=NULL ){. 
30a2b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e       sqlite3_fin
30a2c 61 6c 69 7a 65 28 76 2d 3e 70 4c 65 61 66 53 65  alize(v->pLeafSe
30a2d 6c 65 63 74 53 74 6d 74 73 5b 69 5d 29 3b 0a 20  lectStmts[i]);. 
30a2e 20 20 20 20 20 76 2d 3e 70 4c 65 61 66 53 65 6c       v->pLeafSel
30a2f 65 63 74 53 74 6d 74 73 5b 69 5d 20 3d 20 4e 55  ectStmts[i] = NU
30a30 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  LL;.    }.  }.. 
30a31 20 69 66 28 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a   if( v->pTokeniz
30a32 65 72 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  er!=NULL ){.    
30a33 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70  v->pTokenizer->p
30a34 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f 79  Module->xDestroy
30a35 28 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b  (v->pTokenizer);
30a36 0a 20 20 20 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a  .    v->pTokeniz
30a37 65 72 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a  er = NULL;.  }..
30a38 20 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65    clearPendingTe
30a39 72 6d 73 28 76 29 3b 0a 0a 20 20 73 71 6c 69 74  rms(v);..  sqlit
30a3a 65 33 5f 66 72 65 65 28 76 2d 3e 61 7a 43 6f 6c  e3_free(v->azCol
30a3b 75 6d 6e 29 3b 0a 20 20 66 6f 72 28 69 20 3d 20  umn);.  for(i = 
30a3c 30 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d  0; i < v->nColum
30a3d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20 73 71  n; ++i) {.    sq
30a3e 6c 69 74 65 33 5f 66 72 65 65 28 76 2d 3e 61 7a  lite3_free(v->az
30a3f 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d  ContentColumn[i]
30a40 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
30a41 5f 66 72 65 65 28 76 2d 3e 61 7a 43 6f 6e 74 65  _free(v->azConte
30a42 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c  ntColumn);.  sql
30a43 69 74 65 33 5f 66 72 65 65 28 76 29 3b 0a 7d 0a  ite3_free(v);.}.
30a44 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79 70  ./*.** Token typ
30a45 65 73 20 66 6f 72 20 70 61 72 73 69 6e 67 20 74  es for parsing t
30a46 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  he arguments to 
30a47 78 43 6f 6e 6e 65 63 74 20 6f 72 20 78 43 72 65  xConnect or xCre
30a48 61 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ate..*/.#define 
30a49 54 4f 4b 45 4e 5f 45 4f 46 20 20 20 20 20 20 20  TOKEN_EOF       
30a4a 20 20 30 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66    0    /* End of
30a4b 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65   file */.#define
30a4c 20 54 4f 4b 45 4e 5f 53 50 41 43 45 20 20 20 20   TOKEN_SPACE    
30a4d 20 20 20 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b     1    /* Any k
30a4e 69 6e 64 20 6f 66 20 77 68 69 74 65 73 70 61 63  ind of whitespac
30a4f 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b  e */.#define TOK
30a50 45 4e 5f 49 44 20 20 20 20 20 20 20 20 20 20 32  EN_ID          2
30a51 20 20 20 20 2f 2a 20 41 6e 20 69 64 65 6e 74 69      /* An identi
30a52 66 69 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  fier */.#define 
30a53 54 4f 4b 45 4e 5f 53 54 52 49 4e 47 20 20 20 20  TOKEN_STRING    
30a54 20 20 33 20 20 20 20 2f 2a 20 41 20 73 74 72 69    3    /* A stri
30a55 6e 67 20 6c 69 74 65 72 61 6c 20 2a 2f 0a 23 64  ng literal */.#d
30a56 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 50 55 4e 43  efine TOKEN_PUNC
30a57 54 20 20 20 20 20 20 20 34 20 20 20 20 2f 2a 20  T       4    /* 
30a58 41 20 73 69 6e 67 6c 65 20 70 75 6e 63 74 75 61  A single punctua
30a59 74 69 6f 6e 20 63 68 61 72 61 63 74 65 72 20 2a  tion character *
30a5a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20 69 73  /../*.** If X is
30a5b 20 61 20 63 68 61 72 61 63 74 65 72 20 74 68 61   a character tha
30a5c 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e  t can be used in
30a5d 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74   an identifier t
30a5e 68 65 6e 0a 2a 2a 20 66 74 73 49 64 43 68 61 72  hen.** ftsIdChar
30a5f 28 58 29 20 77 69 6c 6c 20 62 65 20 74 72 75 65  (X) will be true
30a60 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 20  .  Otherwise it 
30a61 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20  is false..**.** 
30a62 46 6f 72 20 41 53 43 49 49 2c 20 61 6e 79 20 63  For ASCII, any c
30a63 68 61 72 61 63 74 65 72 20 77 69 74 68 20 74 68  haracter with th
30a64 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69 74  e high-order bit
30a65 20 73 65 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77   set is.** allow
30a66 65 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69 66  ed in an identif
30a67 69 65 72 2e 20 20 46 6f 72 20 37 2d 62 69 74 20  ier.  For 7-bit 
30a68 63 68 61 72 61 63 74 65 72 73 2c 20 0a 2a 2a 20  characters, .** 
30a69 69 73 46 74 73 49 64 43 68 61 72 5b 58 5d 20 6d  isFtsIdChar[X] m
30a6a 75 73 74 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a 20  ust be 1..**.** 
30a6b 54 69 63 6b 65 74 20 23 31 30 36 36 2e 20 20 74  Ticket #1066.  t
30a6c 68 65 20 53 51 4c 20 73 74 61 6e 64 61 72 64 20  he SQL standard 
30a6d 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 27  does not allow '
30a6e 24 27 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 69 64  $' in the.** mid
30a6f 64 6c 65 20 6f 66 20 69 64 65 6e 74 66 69 65 72  dle of identfier
30a70 73 2e 20 20 42 75 74 20 6d 61 6e 79 20 53 51 4c  s.  But many SQL
30a71 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
30a72 20 64 6f 2e 20 0a 2a 2a 20 53 51 4c 69 74 65 20   do. .** SQLite 
30a73 77 69 6c 6c 20 61 6c 6c 6f 77 20 27 24 27 20 69  will allow '$' i
30a74 6e 20 69 64 65 6e 74 69 66 69 65 72 73 20 66 6f  n identifiers fo
30a75 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e  r compatibility.
30a76 0a 2a 2a 20 42 75 74 20 74 68 65 20 66 65 61 74  .** But the feat
30a77 75 72 65 20 69 73 20 75 6e 64 6f 63 75 6d 65 6e  ure is undocumen
30a78 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ted..*/.static c
30a79 6f 6e 73 74 20 63 68 61 72 20 69 73 46 74 73 49  onst char isFtsI
30a7a 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78  dChar[] = {./* x
30a7b 30 20 78 31 20 78 32 20 78 33 20 78 34 20 78 35  0 x1 x2 x3 x4 x5
30a7c 20 78 36 20 78 37 20 78 38 20 78 39 20 78 41 20   x6 x7 x8 x9 xA 
30a7d 78 42 20 78 43 20 78 44 20 78 45 20 78 46 20 2a  xB xC xD xE xF *
30a7e 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  /.    0, 0, 0, 0
30a7f 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
30a80 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
30a81 30 2c 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a  0, 0,  /* 2x */.
30a82 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20      1, 1, 1, 1, 
30a83 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
30a84 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
30a85 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20   0,  /* 3x */.  
30a86 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
30a87 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
30a88 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
30a89 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20  ,  /* 4x */.    
30a8a 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
30a8b 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
30a8c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20   0, 0, 0, 0, 1, 
30a8d 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 5x */.    0,
30a8e 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
30a8f 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
30a90 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f  , 1, 1, 1, 1,  /
30a91 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31  * 6x */.    1, 1
30a92 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
30a93 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
30a94 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
30a95 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65  7x */.};.#define
30a96 20 66 74 73 49 64 43 68 61 72 28 43 29 20 20 28   ftsIdChar(C)  (
30a97 28 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30 20  ((c=C)&0x80)!=0 
30a98 7c 7c 20 28 63 3e 30 78 31 66 20 26 26 20 69 73  || (c>0x1f && is
30a99 46 74 73 49 64 43 68 61 72 5b 63 2d 30 78 32 30  FtsIdChar[c-0x20
30a9a 5d 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ])).../*.** Retu
30a9b 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  rn the length of
30a9c 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20   the token that 
30a9d 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e 20  begins at z[0]. 
30a9e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f  .** Store the to
30a9f 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f 6b  ken type in *tok
30aa0 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20 72 65  enType before re
30aa1 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  turning..*/.stat
30aa2 69 63 20 69 6e 74 20 66 74 73 47 65 74 54 6f 6b  ic int ftsGetTok
30aa3 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  en(const char *z
30aa4 2c 20 69 6e 74 20 2a 74 6f 6b 65 6e 54 79 70 65  , int *tokenType
30aa5 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20  ){.  int i, c;. 
30aa6 20 73 77 69 74 63 68 28 20 2a 7a 20 29 7b 0a 20   switch( *z ){. 
30aa7 20 20 20 63 61 73 65 20 30 3a 20 7b 0a 20 20 20     case 0: {.   
30aa8 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
30aa9 54 4f 4b 45 4e 5f 45 4f 46 3b 0a 20 20 20 20 20  TOKEN_EOF;.     
30aaa 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
30aab 0a 20 20 20 20 63 61 73 65 20 27 20 27 3a 20 63  .    case ' ': c
30aac 61 73 65 20 27 5c 74 27 3a 20 63 61 73 65 20 27  ase '\t': case '
30aad 5c 6e 27 3a 20 63 61 73 65 20 27 5c 66 27 3a 20  \n': case '\f': 
30aae 63 61 73 65 20 27 5c 72 27 3a 20 7b 0a 20 20 20  case '\r': {.   
30aaf 20 20 20 66 6f 72 28 69 3d 31 3b 20 73 61 66 65     for(i=1; safe
30ab0 5f 69 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20  _isspace(z[i]); 
30ab1 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f  i++){}.      *to
30ab2 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f  kenType = TOKEN_
30ab3 53 50 41 43 45 3b 0a 20 20 20 20 20 20 72 65 74  SPACE;.      ret
30ab4 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20  urn i;.    }.   
30ab5 20 63 61 73 65 20 27 60 27 3a 0a 20 20 20 20 63   case '`':.    c
30ab6 61 73 65 20 27 5c 27 27 3a 0a 20 20 20 20 63 61  ase '\'':.    ca
30ab7 73 65 20 27 22 27 3a 20 7b 0a 20 20 20 20 20 20  se '"': {.      
30ab8 69 6e 74 20 64 65 6c 69 6d 20 3d 20 7a 5b 30 5d  int delim = z[0]
30ab9 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  ;.      for(i=1;
30aba 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b   (c=z[i])!=0; i+
30abb 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
30abc 63 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20  c==delim ){.    
30abd 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d        if( z[i+1]
30abe 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20  ==delim ){.     
30abf 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20         i++;.    
30ac0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
30ac1 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
30ac2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
30ac3 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
30ac4 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
30ac5 20 54 4f 4b 45 4e 5f 53 54 52 49 4e 47 3b 0a 20   TOKEN_STRING;. 
30ac6 20 20 20 20 20 72 65 74 75 72 6e 20 69 20 2b 20       return i + 
30ac7 28 63 21 3d 30 29 3b 0a 20 20 20 20 7d 0a 20 20  (c!=0);.    }.  
30ac8 20 20 63 61 73 65 20 27 5b 27 3a 20 7b 0a 20 20    case '[': {.  
30ac9 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 63 3d 7a      for(i=1, c=z
30aca 5b 30 5d 3b 20 63 21 3d 27 5d 27 20 26 26 20 28  [0]; c!=']' && (
30acb 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
30acc 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
30acd 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 49 44 3b 0a  ype = TOKEN_ID;.
30ace 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
30acf 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
30ad0 74 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 21  t: {.      if( !
30ad1 66 74 73 49 64 43 68 61 72 28 2a 7a 29 20 29 7b  ftsIdChar(*z) ){
30ad2 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30ad3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
30ad4 72 28 69 3d 31 3b 20 66 74 73 49 64 43 68 61 72  r(i=1; ftsIdChar
30ad5 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
30ad6 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
30ad7 3d 20 54 4f 4b 45 4e 5f 49 44 3b 0a 20 20 20 20  = TOKEN_ID;.    
30ad8 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
30ad9 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79  }.  }.  *tokenTy
30ada 70 65 20 3d 20 54 4f 4b 45 4e 5f 50 55 4e 43 54  pe = TOKEN_PUNCT
30adb 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ;.  return 1;.}.
30adc 0a 2f 2a 0a 2a 2a 20 41 20 74 6f 6b 65 6e 20 65  ./*.** A token e
30add 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 61 20  xtracted from a 
30ade 73 74 72 69 6e 67 20 69 73 20 61 6e 20 69 6e 73  string is an ins
30adf 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
30ae0 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74  lowing.** struct
30ae1 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ure..*/.typedef 
30ae2 73 74 72 75 63 74 20 46 74 73 54 6f 6b 65 6e 20  struct FtsToken 
30ae3 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
30ae4 7a 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  z;       /* Poin
30ae5 74 65 72 20 74 6f 20 74 6f 6b 65 6e 20 74 65 78  ter to token tex
30ae6 74 2e 20 20 4e 6f 74 20 27 5c 30 30 30 27 20 74  t.  Not '\000' t
30ae7 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 20 20 73  erminated */.  s
30ae8 68 6f 72 74 20 69 6e 74 20 6e 3b 20 20 20 20 20  hort int n;     
30ae9 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
30aea 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20   the token text 
30aeb 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 7d 20 46  in bytes. */.} F
30aec 74 73 54 6f 6b 65 6e 3b 0a 0a 2f 2a 0a 2a 2a 20  tsToken;../*.** 
30aed 47 69 76 65 6e 20 61 20 69 6e 70 75 74 20 73 74  Given a input st
30aee 72 69 6e 67 20 28 77 68 69 63 68 20 69 73 20 72  ring (which is r
30aef 65 61 6c 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65  eally one of the
30af0 20 61 72 67 76 5b 5d 20 70 61 72 61 6d 65 74 65   argv[] paramete
30af1 72 73 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e 74  rs.** passed int
30af2 6f 20 78 43 6f 6e 6e 65 63 74 20 6f 72 20 78 43  o xConnect or xC
30af3 72 65 61 74 65 29 20 73 70 6c 69 74 20 74 68 65  reate) split the
30af4 20 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f 20   string up into 
30af5 74 6f 6b 65 6e 73 2e 0a 2a 2a 20 52 65 74 75 72  tokens..** Retur
30af6 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f  n an array of po
30af7 69 6e 74 65 72 73 20 74 6f 20 27 5c 30 30 30 27  inters to '\000'
30af8 20 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69   terminated stri
30af9 6e 67 73 2c 20 6f 6e 65 20 73 74 72 69 6e 67 0a  ngs, one string.
30afa 2a 2a 20 66 6f 72 20 65 61 63 68 20 6e 6f 6e 2d  ** for each non-
30afb 77 68 69 74 65 73 70 61 63 65 20 74 6f 6b 65 6e  whitespace token
30afc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
30afd 72 6e 65 64 20 61 72 72 61 79 20 69 73 20 74 65  rned array is te
30afe 72 6d 69 6e 61 74 65 64 20 62 79 20 61 20 73 69  rminated by a si
30aff 6e 67 6c 65 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ngle NULL pointe
30b00 72 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74  r..**.** Space t
30b01 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72  o hold the retur
30b02 6e 65 64 20 61 72 72 61 79 20 69 73 20 6f 62 74  ned array is obt
30b03 61 69 6e 65 64 20 66 72 6f 6d 20 61 20 73 69 6e  ained from a sin
30b04 67 6c 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 61 6e  gle.** malloc an
30b05 64 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65  d should be free
30b06 64 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65  d by passing the
30b07 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 74 6f   return value to
30b08 20 66 72 65 65 28 29 2e 0a 2a 2a 20 54 68 65 20   free()..** The 
30b09 69 6e 64 69 76 69 64 75 61 6c 20 73 74 72 69 6e  individual strin
30b0a 67 73 20 77 69 74 68 69 6e 20 74 68 65 20 74 6f  gs within the to
30b0b 6b 65 6e 20 6c 69 73 74 20 61 72 65 20 61 6c 6c  ken list are all
30b0c 20 61 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68   a part of.** th
30b0d 65 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79 20  e single memory 
30b0e 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 77  allocation and w
30b0f 69 6c 6c 20 61 6c 6c 20 62 65 20 66 72 65 65 64  ill all be freed
30b10 20 61 74 20 6f 6e 63 65 2e 0a 2a 2f 0a 73 74 61   at once..*/.sta
30b11 74 69 63 20 63 68 61 72 20 2a 2a 74 6f 6b 65 6e  tic char **token
30b12 69 7a 65 53 74 72 69 6e 67 28 63 6f 6e 73 74 20  izeString(const 
30b13 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 6e  char *z, int *pn
30b14 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 6e 54  Token){.  int nT
30b15 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 46 74 73 54  oken = 0;.  FtsT
30b16 6f 6b 65 6e 20 2a 61 54 6f 6b 65 6e 20 3d 20 73  oken *aToken = s
30b17 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73  qlite3_malloc( s
30b18 74 72 6c 65 6e 28 7a 29 20 2a 20 73 69 7a 65 6f  trlen(z) * sizeo
30b19 66 28 61 54 6f 6b 65 6e 5b 30 5d 29 20 29 3b 0a  f(aToken[0]) );.
30b1a 20 20 69 6e 74 20 6e 20 3d 20 31 3b 0a 20 20 69    int n = 1;.  i
30b1b 6e 74 20 65 2c 20 69 3b 0a 20 20 69 6e 74 20 74  nt e, i;.  int t
30b1c 6f 74 61 6c 53 69 7a 65 20 3d 20 30 3b 0a 20 20  otalSize = 0;.  
30b1d 63 68 61 72 20 2a 2a 61 7a 54 6f 6b 65 6e 3b 0a  char **azToken;.
30b1e 20 20 63 68 61 72 20 2a 7a 43 6f 70 79 3b 0a 20    char *zCopy;. 
30b1f 20 77 68 69 6c 65 28 20 6e 3e 30 20 29 7b 0a 20   while( n>0 ){. 
30b20 20 20 20 6e 20 3d 20 66 74 73 47 65 74 54 6f 6b     n = ftsGetTok
30b21 65 6e 28 7a 2c 20 26 65 29 3b 0a 20 20 20 20 69  en(z, &e);.    i
30b22 66 28 20 65 21 3d 54 4f 4b 45 4e 5f 53 50 41 43  f( e!=TOKEN_SPAC
30b23 45 20 29 7b 0a 20 20 20 20 20 20 61 54 6f 6b 65  E ){.      aToke
30b24 6e 5b 6e 54 6f 6b 65 6e 5d 2e 7a 20 3d 20 7a 3b  n[nToken].z = z;
30b25 0a 20 20 20 20 20 20 61 54 6f 6b 65 6e 5b 6e 54  .      aToken[nT
30b26 6f 6b 65 6e 5d 2e 6e 20 3d 20 6e 3b 0a 20 20 20  oken].n = n;.   
30b27 20 20 20 6e 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20     nToken++;.   
30b28 20 20 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d 20     totalSize += 
30b29 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  n+1;.    }.    z
30b2a 20 2b 3d 20 6e 3b 0a 20 20 7d 0a 20 20 61 7a 54   += n;.  }.  azT
30b2b 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 2a 29 73  oken = (char**)s
30b2c 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e  qlite3_malloc( n
30b2d 54 6f 6b 65 6e 2a 73 69 7a 65 6f 66 28 63 68 61  Token*sizeof(cha
30b2e 72 2a 29 20 2b 20 74 6f 74 61 6c 53 69 7a 65 20  r*) + totalSize 
30b2f 29 3b 0a 20 20 7a 43 6f 70 79 20 3d 20 28 63 68  );.  zCopy = (ch
30b30 61 72 2a 29 26 61 7a 54 6f 6b 65 6e 5b 6e 54 6f  ar*)&azToken[nTo
30b31 6b 65 6e 5d 3b 0a 20 20 6e 54 6f 6b 65 6e 2d 2d  ken];.  nToken--
30b32 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
30b33 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  Token; i++){.   
30b34 20 61 7a 54 6f 6b 65 6e 5b 69 5d 20 3d 20 7a 43   azToken[i] = zC
30b35 6f 70 79 3b 0a 20 20 20 20 6e 20 3d 20 61 54 6f  opy;.    n = aTo
30b36 6b 65 6e 5b 69 5d 2e 6e 3b 0a 20 20 20 20 6d 65  ken[i].n;.    me
30b37 6d 63 70 79 28 7a 43 6f 70 79 2c 20 61 54 6f 6b  mcpy(zCopy, aTok
30b38 65 6e 5b 69 5d 2e 7a 2c 20 6e 29 3b 0a 20 20 20  en[i].z, n);.   
30b39 20 7a 43 6f 70 79 5b 6e 5d 20 3d 20 30 3b 0a 20   zCopy[n] = 0;. 
30b3a 20 20 20 7a 43 6f 70 79 20 2b 3d 20 6e 2b 31 3b     zCopy += n+1;
30b3b 0a 20 20 7d 0a 20 20 61 7a 54 6f 6b 65 6e 5b 6e  .  }.  azToken[n
30b3c 54 6f 6b 65 6e 5d 20 3d 20 30 3b 0a 20 20 73 71  Token] = 0;.  sq
30b3d 6c 69 74 65 33 5f 66 72 65 65 28 61 54 6f 6b 65  lite3_free(aToke
30b3e 6e 29 3b 0a 20 20 2a 70 6e 54 6f 6b 65 6e 20 3d  n);.  *pnToken =
30b3f 20 6e 54 6f 6b 65 6e 3b 0a 20 20 72 65 74 75 72   nToken;.  retur
30b40 6e 20 61 7a 54 6f 6b 65 6e 3b 0a 7d 0a 0a 2f 2a  n azToken;.}../*
30b41 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 53  .** Convert an S
30b42 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 20  QL-style quoted 
30b43 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f  string into a no
30b44 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20 72  rmal string by r
30b45 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71  emoving.** the q
30b46 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73 2e  uote characters.
30b47 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e    The conversion
30b48 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63   is done in-plac
30b49 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e  e.  If the.** in
30b4a 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 67  put does not beg
30b4b 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 65 20  in with a quote 
30b4c 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20  character, then 
30b4d 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
30b4e 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
30b4f 2a 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a  * Examples:.**.*
30b50 2a 20 20 20 20 20 22 61 62 63 22 20 20 20 62 65  *     "abc"   be
30b51 63 6f 6d 65 73 20 20 20 61 62 63 0a 2a 2a 20 20  comes   abc.**  
30b52 20 20 20 27 78 79 7a 27 20 20 20 62 65 63 6f 6d     'xyz'   becom
30b53 65 73 20 20 20 78 79 7a 0a 2a 2a 20 20 20 20 20  es   xyz.**     
30b54 5b 70 71 72 5d 20 20 20 62 65 63 6f 6d 65 73 20  [pqr]   becomes 
30b55 20 20 70 71 72 0a 2a 2a 20 20 20 20 20 60 6d 6e    pqr.**     `mn
30b56 6f 60 20 20 20 62 65 63 6f 6d 65 73 20 20 20 6d  o`   becomes   m
30b57 6e 6f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  no.*/.static voi
30b58 64 20 64 65 71 75 6f 74 65 53 74 72 69 6e 67 28  d dequoteString(
30b59 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  char *z){.  int 
30b5a 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20  quote;.  int i, 
30b5b 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20  j;.  if( z==0 ) 
30b5c 72 65 74 75 72 6e 3b 0a 20 20 71 75 6f 74 65 20  return;.  quote 
30b5d 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68  = z[0];.  switch
30b5e 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63  ( quote ){.    c
30b5f 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b  ase '\'':  break
30b60 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a 20  ;.    case '"': 
30b61 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
30b62 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20  e '`':   break; 
30b63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
30b64 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70  * For MySQL comp
30b65 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20  atibility */.   
30b66 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f   case '[':   quo
30b67 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b  te = ']';  break
30b68 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c  ;  /* For MS Sql
30b69 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69  Server compatibi
30b6a 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61  lity */.    defa
30b6b 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ult:    return;.
30b6c 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a    }.  for(i=1, j
30b6d 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  =0; z[i]; i++){.
30b6e 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75      if( z[i]==qu
30b6f 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ote ){.      if(
30b70 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29   z[i+1]==quote )
30b71 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d  {.        z[j++]
30b72 20 3d 20 71 75 6f 74 65 3b 0a 20 20 20 20 20 20   = quote;.      
30b73 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c    i++;.      }el
30b74 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b  se{.        z[j+
30b75 2b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  +] = 0;.        
30b76 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
30b77 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
30b78 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20  z[j++] = z[i];. 
30b79 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
30b7a 2a 20 54 68 65 20 69 6e 70 75 74 20 61 7a 49 6e  * The input azIn
30b7b 20 69 73 20 61 20 4e 55 4c 4c 2d 74 65 72 6d 69   is a NULL-termi
30b7c 6e 61 74 65 64 20 6c 69 73 74 20 6f 66 20 74 6f  nated list of to
30b7d 6b 65 6e 73 2e 20 20 52 65 6d 6f 76 65 20 74 68  kens.  Remove th
30b7e 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e  e first.** token
30b7f 20 61 6e 64 20 61 6c 6c 20 70 75 6e 63 74 75 61   and all punctua
30b80 74 69 6f 6e 20 74 6f 6b 65 6e 73 2e 20 20 52 65  tion tokens.  Re
30b81 6d 6f 76 65 20 74 68 65 20 71 75 6f 74 65 73 20  move the quotes 
30b82 66 72 6f 6d 0a 2a 2a 20 61 72 6f 75 6e 64 20 73  from.** around s
30b83 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 74 6f  tring literal to
30b84 6b 65 6e 73 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d  kens..**.** Exam
30b85 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 69  ple:.**.**     i
30b86 6e 70 75 74 3a 20 20 20 20 20 20 74 6f 6b 65 6e  nput:      token
30b87 69 7a 65 20 63 68 69 6e 65 73 65 20 28 20 27 73  ize chinese ( 's
30b88 69 6d 70 6c 69 66 65 64 27 20 2c 20 27 6d 69 78  implifed' , 'mix
30b89 65 64 27 20 29 0a 2a 2a 20 20 20 20 20 6f 75 74  ed' ).**     out
30b8a 70 75 74 3a 20 20 20 20 20 63 68 69 6e 65 73 65  put:     chinese
30b8b 20 73 69 6d 70 6c 69 66 65 64 20 6d 69 78 65 64   simplifed mixed
30b8c 0a 2a 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 65  .**.** Another e
30b8d 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  xample:.**.**   
30b8e 20 20 69 6e 70 75 74 3a 20 20 20 20 20 20 64 65    input:      de
30b8f 6c 69 6d 69 74 65 72 73 20 28 20 27 5b 27 20 2c  limiters ( '[' ,
30b90 20 27 5d 27 20 2c 20 27 2e 2e 2e 27 20 29 0a 2a   ']' , '...' ).*
30b91 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20  *     output:   
30b92 20 20 5b 20 5d 20 2e 2e 2e 0a 2a 2f 0a 73 74 61    [ ] ....*/.sta
30b93 74 69 63 20 76 6f 69 64 20 74 6f 6b 65 6e 4c 69  tic void tokenLi
30b94 73 74 54 6f 49 64 4c 69 73 74 28 63 68 61 72 20  stToIdList(char 
30b95 2a 2a 61 7a 49 6e 29 7b 0a 20 20 69 6e 74 20 69  **azIn){.  int i
30b96 2c 20 6a 3b 0a 20 20 69 66 28 20 61 7a 49 6e 20  , j;.  if( azIn 
30b97 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  ){.    for(i=0, 
30b98 6a 3d 2d 31 3b 20 61 7a 49 6e 5b 69 5d 3b 20 69  j=-1; azIn[i]; i
30b99 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ++){.      if( s
30b9a 61 66 65 5f 69 73 61 6c 6e 75 6d 28 61 7a 49 6e  afe_isalnum(azIn
30b9b 5b 69 5d 5b 30 5d 29 20 7c 7c 20 61 7a 49 6e 5b  [i][0]) || azIn[
30b9c 69 5d 5b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20  i][1] ){.       
30b9d 20 64 65 71 75 6f 74 65 53 74 72 69 6e 67 28 61   dequoteString(a
30b9e 7a 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20  zIn[i]);.       
30b9f 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20   if( j>=0 ){.   
30ba0 20 20 20 20 20 20 20 61 7a 49 6e 5b 6a 5d 20 3d         azIn[j] =
30ba1 20 61 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 20 20   azIn[i];.      
30ba2 20 20 7d 0a 20 20 20 20 20 20 20 20 6a 2b 2b 3b    }.        j++;
30ba3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
30ba4 20 20 20 61 7a 49 6e 5b 6a 5d 20 3d 20 30 3b 0a     azIn[j] = 0;.
30ba5 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69    }.}.../*.** Fi
30ba6 6e 64 20 74 68 65 20 66 69 72 73 74 20 61 6c 70  nd the first alp
30ba7 68 61 6e 75 6d 65 72 69 63 20 74 6f 6b 65 6e 20  hanumeric token 
30ba8 69 6e 20 74 68 65 20 73 74 72 69 6e 67 20 7a 49  in the string zI
30ba9 6e 2e 20 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61  n.  Null-termina
30baa 74 65 0a 2a 2a 20 74 68 69 73 20 74 6f 6b 65 6e  te.** this token
30bab 2e 20 20 52 65 6d 6f 76 65 20 61 6e 79 20 71 75  .  Remove any qu
30bac 6f 74 61 74 69 6f 6e 20 6d 61 72 6b 73 2e 20 20  otation marks.  
30bad 41 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  And return a poi
30bae 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 72  nter to.** the r
30baf 65 73 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  esult..*/.static
30bb0 20 63 68 61 72 20 2a 66 69 72 73 74 54 6f 6b 65   char *firstToke
30bb1 6e 28 63 68 61 72 20 2a 7a 49 6e 2c 20 63 68 61  n(char *zIn, cha
30bb2 72 20 2a 2a 70 7a 54 61 69 6c 29 7b 0a 20 20 69  r **pzTail){.  i
30bb3 6e 74 20 6e 2c 20 74 74 79 70 65 3b 0a 20 20 77  nt n, ttype;.  w
30bb4 68 69 6c 65 28 31 29 7b 0a 20 20 20 20 6e 20 3d  hile(1){.    n =
30bb5 20 66 74 73 47 65 74 54 6f 6b 65 6e 28 7a 49 6e   ftsGetToken(zIn
30bb6 2c 20 26 74 74 79 70 65 29 3b 0a 20 20 20 20 69  , &ttype);.    i
30bb7 66 28 20 74 74 79 70 65 3d 3d 54 4f 4b 45 4e 5f  f( ttype==TOKEN_
30bb8 53 50 41 43 45 20 29 7b 0a 20 20 20 20 20 20 7a  SPACE ){.      z
30bb9 49 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 7d 65 6c  In += n;.    }el
30bba 73 65 20 69 66 28 20 74 74 79 70 65 3d 3d 54 4f  se if( ttype==TO
30bbb 4b 45 4e 5f 45 4f 46 20 29 7b 0a 20 20 20 20 20  KEN_EOF ){.     
30bbc 20 2a 70 7a 54 61 69 6c 20 3d 20 7a 49 6e 3b 0a   *pzTail = zIn;.
30bbd 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
30bbe 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
30bbf 20 7a 49 6e 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20   zIn[n] = 0;.   
30bc0 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 26 7a 49     *pzTail = &zI
30bc1 6e 5b 31 5d 3b 0a 20 20 20 20 20 20 64 65 71 75  n[1];.      dequ
30bc2 6f 74 65 53 74 72 69 6e 67 28 7a 49 6e 29 3b 0a  oteString(zIn);.
30bc3 20 20 20 20 20 20 72 65 74 75 72 6e 20 7a 49 6e        return zIn
30bc4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  ;.    }.  }.  /*
30bc5 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 7d 0a 0a  NOTREACHED*/.}..
30bc6 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  /* Return true i
30bc7 66 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20  f....**.**   *  
30bc8 73 20 62 65 67 69 6e 73 20 77 69 74 68 20 74 68  s begins with th
30bc9 65 20 73 74 72 69 6e 67 20 74 2c 20 69 67 6e 6f  e string t, igno
30bca 72 69 6e 67 20 63 61 73 65 0a 2a 2a 20 20 20 2a  ring case.**   *
30bcb 20 20 73 20 69 73 20 6c 6f 6e 67 65 72 20 74 68    s is longer th
30bcc 61 6e 20 74 0a 2a 2a 20 20 20 2a 20 20 54 68 65  an t.**   *  The
30bcd 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
30bce 20 6f 66 20 73 20 62 65 79 6f 6e 64 20 74 20 69   of s beyond t i
30bcf 73 20 6e 6f 74 20 61 20 61 6c 70 68 61 6e 75 6d  s not a alphanum
30bd0 65 72 69 63 0a 2a 2a 20 0a 2a 2a 20 49 67 6e 6f  eric.** .** Igno
30bd1 72 65 20 6c 65 61 64 69 6e 67 20 73 70 61 63 65  re leading space
30bd2 20 69 6e 20 2a 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f   in *s..**.** To
30bd3 20 70 75 74 20 69 74 20 61 6e 6f 74 68 65 72 20   put it another 
30bd4 77 61 79 2c 20 72 65 74 75 72 6e 20 74 72 75 65  way, return true
30bd5 20 69 66 20 74 68 65 20 66 69 72 73 74 20 74 6f   if the first to
30bd6 6b 65 6e 20 6f 66 0a 2a 2a 20 73 5b 5d 20 69 73  ken of.** s[] is
30bd7 20 74 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   t[]..*/.static 
30bd8 69 6e 74 20 73 74 61 72 74 73 57 69 74 68 28 63  int startsWith(c
30bd9 6f 6e 73 74 20 63 68 61 72 20 2a 73 2c 20 63 6f  onst char *s, co
30bda 6e 73 74 20 63 68 61 72 20 2a 74 29 7b 0a 20 20  nst char *t){.  
30bdb 77 68 69 6c 65 28 20 73 61 66 65 5f 69 73 73 70  while( safe_issp
30bdc 61 63 65 28 2a 73 29 20 29 7b 20 73 2b 2b 3b 20  ace(*s) ){ s++; 
30bdd 7d 0a 20 20 77 68 69 6c 65 28 20 2a 74 20 29 7b  }.  while( *t ){
30bde 0a 20 20 20 20 69 66 28 20 73 61 66 65 5f 74 6f  .    if( safe_to
30bdf 6c 6f 77 65 72 28 2a 73 2b 2b 29 21 3d 73 61 66  lower(*s++)!=saf
30be0 65 5f 74 6f 6c 6f 77 65 72 28 2a 74 2b 2b 29 20  e_tolower(*t++) 
30be1 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a  ) return 0;.  }.
30be2 20 20 72 65 74 75 72 6e 20 2a 73 21 3d 27 5f 27    return *s!='_'
30be3 20 26 26 20 21 73 61 66 65 5f 69 73 61 6c 6e 75   && !safe_isalnu
30be4 6d 28 2a 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  m(*s);.}../*.** 
30be5 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
30be6 68 69 73 20 73 74 72 75 63 74 75 72 65 20 64 65  his structure de
30be7 66 69 6e 65 73 20 74 68 65 20 22 73 70 65 63 22  fines the "spec"
30be8 20 6f 66 20 61 0a 2a 2a 20 66 75 6c 6c 20 74 65   of a.** full te
30be9 78 74 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20  xt index.  This 
30bea 73 74 72 75 63 74 75 72 65 20 69 73 20 70 6f 70  structure is pop
30beb 75 6c 61 74 65 64 20 62 79 20 70 61 72 73 65 53  ulated by parseS
30bec 70 65 63 0a 2a 2a 20 61 6e 64 20 75 73 65 20 62  pec.** and use b
30bed 79 20 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65 63  y fulltextConnec
30bee 74 20 61 6e 64 20 66 75 6c 6c 74 65 78 74 43 72  t and fulltextCr
30bef 65 61 74 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  eate..*/.typedef
30bf0 20 73 74 72 75 63 74 20 54 61 62 6c 65 53 70 65   struct TableSpe
30bf1 63 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  c {.  const char
30bf2 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 2f   *zDb;         /
30bf3 2a 20 4c 6f 67 69 63 61 6c 20 64 61 74 61 62 61  * Logical databa
30bf4 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e  se name */.  con
30bf5 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  st char *zName; 
30bf6 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
30bf7 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69   the full-text i
30bf8 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  ndex */.  int nC
30bf9 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
30bfa 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
30bfb 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e  columns to be in
30bfc 64 65 78 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  dexed */.  char 
30bfd 2a 2a 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  **azColumn;     
30bfe 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20      /* Original 
30bff 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73  names of columns
30c00 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a   to be indexed *
30c01 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6e  /.  char **azCon
30c02 74 65 6e 74 43 6f 6c 75 6d 6e 3b 20 20 2f 2a 20  tentColumn;  /* 
30c03 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72  Column names for
30c04 20 25 5f 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20   %_content */.  
30c05 63 68 61 72 20 2a 2a 61 7a 54 6f 6b 65 6e 69 7a  char **azTokeniz
30c06 65 72 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  er;      /* Name
30c07 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72 20 61 6e   of tokenizer an
30c08 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73 20  d its arguments 
30c09 2a 2f 0a 7d 20 54 61 62 6c 65 53 70 65 63 3b 0a  */.} TableSpec;.
30c0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d 20 61  ./*.** Reclaim a
30c0b 6c 6c 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  ll of the memory
30c0c 20 75 73 65 64 20 62 79 20 61 20 54 61 62 6c 65   used by a Table
30c0d 53 70 65 63 0a 2a 2f 0a 73 74 61 74 69 63 20 76  Spec.*/.static v
30c0e 6f 69 64 20 63 6c 65 61 72 54 61 62 6c 65 53 70  oid clearTableSp
30c0f 65 63 28 54 61 62 6c 65 53 70 65 63 20 2a 70 29  ec(TableSpec *p)
30c10 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   {.  sqlite3_fre
30c11 65 28 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 29 3b 0a  e(p->azColumn);.
30c12 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
30c13 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
30c14 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  n);.  sqlite3_fr
30c15 65 65 28 70 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65  ee(p->azTokenize
30c16 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 61 72 73 65 20  r);.}../* Parse 
30c17 61 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  a CREATE VIRTUAL
30c18 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
30c19 2c 20 77 68 69 63 68 20 6c 6f 6f 6b 73 20 6c 69  , which looks li
30c1a 6b 65 20 74 68 69 73 3a 0a 20 2a 0a 20 2a 20 43  ke this:. *. * C
30c1b 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
30c1c 42 4c 45 20 65 6d 61 69 6c 0a 20 2a 20 20 20 20  BLE email. *    
30c1d 20 20 20 20 55 53 49 4e 47 20 66 74 73 33 28 73      USING fts3(s
30c1e 75 62 6a 65 63 74 2c 20 62 6f 64 79 2c 20 74 6f  ubject, body, to
30c1f 6b 65 6e 69 7a 65 20 6d 79 74 6f 6b 65 6e 69 7a  kenize mytokeniz
30c20 65 72 28 6d 79 61 72 67 29 29 0a 20 2a 0a 20 2a  er(myarg)). *. *
30c21 20 57 65 20 72 65 74 75 72 6e 20 70 61 72 73 65   We return parse
30c22 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  d information in
30c23 20 61 20 54 61 62 6c 65 53 70 65 63 20 73 74 72   a TableSpec str
30c24 75 63 74 75 72 65 2e 0a 20 2a 20 0a 20 2a 2f 0a  ucture.. * . */.
30c25 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65  static int parse
30c26 53 70 65 63 28 54 61 62 6c 65 53 70 65 63 20 2a  Spec(TableSpec *
30c27 70 53 70 65 63 2c 20 69 6e 74 20 61 72 67 63 2c  pSpec, int argc,
30c28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
30c29 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20  st*argv,.       
30c2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 68                ch
30c2b 61 72 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e  ar**pzErr){.  in
30c2c 74 20 69 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a  t i, n;.  char *
30c2d 7a 2c 20 2a 7a 44 75 6d 6d 79 3b 0a 20 20 63 68  z, *zDummy;.  ch
30c2e 61 72 20 2a 2a 61 7a 41 72 67 3b 0a 20 20 63 6f  ar **azArg;.  co
30c2f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e  nst char *zToken
30c30 69 7a 65 72 20 3d 20 30 3b 20 20 20 20 2f 2a 20  izer = 0;    /* 
30c31 61 72 67 76 5b 5d 20 65 6e 74 72 79 20 64 65 73  argv[] entry des
30c32 63 72 69 62 69 6e 67 20 74 68 65 20 74 6f 6b 65  cribing the toke
30c33 6e 69 7a 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65  nizer */..  asse
30c34 72 74 28 20 61 72 67 63 3e 3d 33 20 29 3b 0a 20  rt( argc>=3 );. 
30c35 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 74 65   /* Current inte
30c36 72 66 61 63 65 3a 0a 20 20 2a 2a 20 61 72 67 76  rface:.  ** argv
30c37 5b 30 5d 20 2d 20 6d 6f 64 75 6c 65 20 6e 61 6d  [0] - module nam
30c38 65 0a 20 20 2a 2a 20 61 72 67 76 5b 31 5d 20 2d  e.  ** argv[1] -
30c39 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 20   database name. 
30c3a 20 2a 2a 20 61 72 67 76 5b 32 5d 20 2d 20 74 61   ** argv[2] - ta
30c3b 62 6c 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 72  ble name.  ** ar
30c3c 67 76 5b 33 2e 2e 5d 20 2d 20 63 6f 6c 75 6d 6e  gv[3..] - column
30c3d 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 66 6f  s, optionally fo
30c3e 6c 6c 6f 77 65 64 20 62 79 20 74 6f 6b 65 6e 69  llowed by tokeni
30c3f 7a 65 72 20 73 70 65 63 69 66 69 63 61 74 69 6f  zer specificatio
30c40 6e 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  n.  **          
30c41 20 20 20 61 6e 64 20 73 6e 69 70 70 65 74 20 64     and snippet d
30c42 65 6c 69 6d 69 74 65 72 73 20 73 70 65 63 69 66  elimiters specif
30c43 69 63 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a 20  ication..  */.. 
30c44 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20   /* Make a copy 
30c45 6f 66 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  of the complete 
30c46 61 72 67 76 5b 5d 5b 5d 20 61 72 72 61 79 20 69  argv[][] array i
30c47 6e 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63  n a single alloc
30c48 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20  ation..  ** The 
30c49 61 72 67 76 5b 5d 5b 5d 20 61 72 72 61 79 20 69  argv[][] array i
30c4a 73 20 72 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20  s read-only and 
30c4b 74 72 61 6e 73 69 65 6e 74 2e 20 20 57 65 20 63  transient.  We c
30c4c 61 6e 20 77 72 69 74 65 20 74 6f 20 74 68 65 0a  an write to the.
30c4d 20 20 2a 2a 20 63 6f 70 79 20 69 6e 20 6f 72 64    ** copy in ord
30c4e 65 72 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 69  er to modify thi
30c4f 6e 67 73 20 61 6e 64 20 74 68 65 20 63 6f 70 79  ngs and the copy
30c50 20 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e 0a   is persistent..
30c51 20 20 2a 2f 0a 20 20 43 4c 45 41 52 28 70 53 70    */.  CLEAR(pSp
30c52 65 63 29 3b 0a 20 20 66 6f 72 28 69 3d 6e 3d 30  ec);.  for(i=n=0
30c53 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a  ; i<argc; i++){.
30c54 20 20 20 20 6e 20 2b 3d 20 73 74 72 6c 65 6e 28      n += strlen(
30c55 61 72 67 76 5b 69 5d 29 20 2b 20 31 3b 0a 20 20  argv[i]) + 1;.  
30c56 7d 0a 20 20 61 7a 41 72 67 20 3d 20 73 71 6c 69  }.  azArg = sqli
30c57 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
30c58 6f 66 28 63 68 61 72 2a 29 2a 61 72 67 63 20 2b  of(char*)*argc +
30c59 20 6e 20 29 3b 0a 20 20 69 66 28 20 61 7a 41 72   n );.  if( azAr
30c5a 67 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  g==0 ){.    retu
30c5b 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
30c5c 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72  .  }.  z = (char
30c5d 2a 29 26 61 7a 41 72 67 5b 61 72 67 63 5d 3b 0a  *)&azArg[argc];.
30c5e 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61 72 67    for(i=0; i<arg
30c5f 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 7a 41  c; i++){.    azA
30c60 72 67 5b 69 5d 20 3d 20 7a 3b 0a 20 20 20 20 73  rg[i] = z;.    s
30c61 74 72 63 70 79 28 7a 2c 20 61 72 67 76 5b 69 5d  trcpy(z, argv[i]
30c62 29 3b 0a 20 20 20 20 7a 20 2b 3d 20 73 74 72 6c  );.    z += strl
30c63 65 6e 28 7a 29 2b 31 3b 0a 20 20 7d 0a 0a 20 20  en(z)+1;.  }..  
30c64 2f 2a 20 49 64 65 6e 74 69 66 79 20 74 68 65 20  /* Identify the 
30c65 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64  column names and
30c66 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 61   the tokenizer a
30c67 6e 64 20 64 65 6c 69 6d 69 74 65 72 20 61 72 67  nd delimiter arg
30c68 75 6d 65 6e 74 73 0a 20 20 2a 2a 20 69 6e 20 74  uments.  ** in t
30c69 68 65 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72 61  he argv[][] arra
30c6a 79 2e 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63 2d  y..  */.  pSpec-
30c6b 3e 7a 44 62 20 3d 20 61 7a 41 72 67 5b 31 5d 3b  >zDb = azArg[1];
30c6c 0a 20 20 70 53 70 65 63 2d 3e 7a 4e 61 6d 65 20  .  pSpec->zName 
30c6d 3d 20 61 7a 41 72 67 5b 32 5d 3b 0a 20 20 70 53  = azArg[2];.  pS
30c6e 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 30  pec->nColumn = 0
30c6f 3b 0a 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6c  ;.  pSpec->azCol
30c70 75 6d 6e 20 3d 20 61 7a 41 72 67 3b 0a 20 20 7a  umn = azArg;.  z
30c71 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 22 74 6f 6b  Tokenizer = "tok
30c72 65 6e 69 7a 65 20 73 69 6d 70 6c 65 22 3b 0a 20  enize simple";. 
30c73 20 66 6f 72 28 69 3d 33 3b 20 69 3c 61 72 67 63   for(i=3; i<argc
30c74 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 69 66 28 20  ; ++i){.    if( 
30c75 73 74 61 72 74 73 57 69 74 68 28 61 7a 41 72 67  startsWith(azArg
30c76 5b 69 5d 2c 22 74 6f 6b 65 6e 69 7a 65 22 29 20  [i],"tokenize") 
30c77 29 7b 0a 20 20 20 20 20 20 7a 54 6f 6b 65 6e 69  ){.      zTokeni
30c78 7a 65 72 20 3d 20 61 7a 41 72 67 5b 69 5d 3b 0a  zer = azArg[i];.
30c79 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
30c7a 20 7a 20 3d 20 61 7a 41 72 67 5b 70 53 70 65 63   z = azArg[pSpec
30c7b 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 66 69 72  ->nColumn] = fir
30c7c 73 74 54 6f 6b 65 6e 28 61 7a 41 72 67 5b 69 5d  stToken(azArg[i]
30c7d 2c 20 26 7a 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &zDummy);.    
30c7e 20 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e    pSpec->nColumn
30c7f 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ++;.    }.  }.  
30c80 69 66 28 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75  if( pSpec->nColu
30c81 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 7a 41  mn==0 ){.    azA
30c82 72 67 5b 30 5d 20 3d 20 22 63 6f 6e 74 65 6e 74  rg[0] = "content
30c83 22 3b 0a 20 20 20 20 70 53 70 65 63 2d 3e 6e 43  ";.    pSpec->nC
30c84 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 20 20 7d 0a 0a  olumn = 1;.  }..
30c85 20 20 2f 2a 0a 20 20 2a 2a 20 43 6f 6e 73 74 72    /*.  ** Constr
30c86 75 63 74 20 74 68 65 20 6c 69 73 74 20 6f 66 20  uct the list of 
30c87 63 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 20 6e  content column n
30c88 61 6d 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ames..  **.  ** 
30c89 45 61 63 68 20 63 6f 6e 74 65 6e 74 20 63 6f 6c  Each content col
30c8a 75 6d 6e 20 6e 61 6d 65 20 77 69 6c 6c 20 62 65  umn name will be
30c8b 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 63 4e 4e   of the form cNN
30c8c 41 41 41 41 0a 20 20 2a 2a 20 77 68 65 72 65 20  AAAA.  ** where 
30c8d 4e 4e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e  NN is the column
30c8e 20 6e 75 6d 62 65 72 20 61 6e 64 20 41 41 41 41   number and AAAA
30c8f 20 69 73 20 74 68 65 20 73 61 6e 69 74 69 7a 65   is the sanitize
30c90 64 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 61  d.  ** column na
30c91 6d 65 2e 20 20 22 73 61 6e 69 74 69 7a 65 64 22  me.  "sanitized"
30c92 20 6d 65 61 6e 73 20 74 68 61 74 20 73 70 65 63   means that spec
30c93 69 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 61  ial characters a
30c94 72 65 0a 20 20 2a 2a 20 63 6f 6e 76 65 72 74 65  re.  ** converte
30c95 64 20 74 6f 20 22 5f 22 2e 20 20 54 68 65 20 63  d to "_".  The c
30c96 4e 4e 20 70 72 65 66 69 78 20 67 75 61 72 61 6e  NN prefix guaran
30c97 74 65 65 73 20 74 68 61 74 20 61 6c 6c 20 63 6f  tees that all co
30c98 6c 75 6d 6e 0a 20 20 2a 2a 20 6e 61 6d 65 73 20  lumn.  ** names 
30c99 61 72 65 20 75 6e 69 71 75 65 2e 0a 20 20 2a 2a  are unique..  **
30c9a 0a 20 20 2a 2a 20 54 68 65 20 41 41 41 41 20 73  .  ** The AAAA s
30c9b 75 66 66 69 78 20 69 73 20 6e 6f 74 20 73 74 72  uffix is not str
30c9c 69 63 74 6c 79 20 6e 65 63 65 73 73 61 72 79 2e  ictly necessary.
30c9d 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64    It is included
30c9e 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f  .  ** for the co
30c9f 6e 76 65 6e 69 65 6e 63 65 20 6f 66 20 70 65 6f  nvenience of peo
30ca0 70 6c 65 20 77 68 6f 20 6d 69 67 68 74 20 65 78  ple who might ex
30ca1 61 6d 69 6e 65 20 74 68 65 20 67 65 6e 65 72 61  amine the genera
30ca2 74 65 64 0a 20 20 2a 2a 20 25 5f 63 6f 6e 74 65  ted.  ** %_conte
30ca3 6e 74 20 74 61 62 6c 65 20 61 6e 64 20 77 6f 6e  nt table and won
30ca4 64 65 72 20 77 68 61 74 20 74 68 65 20 63 6f 6c  der what the col
30ca5 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 66 6f  umns are used fo
30ca6 72 2e 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63 2d  r..  */.  pSpec-
30ca7 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  >azContentColumn
30ca8 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
30ca9 63 28 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d  c( pSpec->nColum
30caa 6e 20 2a 20 73 69 7a 65 6f 66 28 63 68 61 72 20  n * sizeof(char 
30cab 2a 29 20 29 3b 0a 20 20 69 66 28 20 70 53 70 65  *) );.  if( pSpe
30cac 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  c->azContentColu
30cad 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6c 65  mn==0 ){.    cle
30cae 61 72 54 61 62 6c 65 53 70 65 63 28 70 53 70 65  arTableSpec(pSpe
30caf 63 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  c);.    return S
30cb0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
30cb1 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53  .  for(i=0; i<pS
30cb2 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  pec->nColumn; i+
30cb3 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 70 3b  +){.    char *p;
30cb4 0a 20 20 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f  .    pSpec->azCo
30cb5 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 20 3d  ntentColumn[i] =
30cb6 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
30cb7 28 22 63 25 64 25 73 22 2c 20 69 2c 20 61 7a 41  ("c%d%s", i, azA
30cb8 72 67 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 20  rg[i]);.    for 
30cb9 28 70 20 3d 20 70 53 70 65 63 2d 3e 61 7a 43 6f  (p = pSpec->azCo
30cba 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 3b 20  ntentColumn[i]; 
30cbb 2a 70 20 3b 20 2b 2b 70 29 20 7b 0a 20 20 20 20  *p ; ++p) {.    
30cbc 20 20 69 66 28 20 21 73 61 66 65 5f 69 73 61 6c    if( !safe_isal
30cbd 6e 75 6d 28 2a 70 29 20 29 20 2a 70 20 3d 20 27  num(*p) ) *p = '
30cbe 5f 27 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  _';.    }.  }.. 
30cbf 20 2f 2a 0a 20 20 2a 2a 20 50 61 72 73 65 20 74   /*.  ** Parse t
30cc0 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 73 70 65  he tokenizer spe
30cc1 63 69 66 69 63 61 74 69 6f 6e 20 73 74 72 69 6e  cification strin
30cc2 67 2e 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63 2d  g..  */.  pSpec-
30cc3 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 74  >azTokenizer = t
30cc4 6f 6b 65 6e 69 7a 65 53 74 72 69 6e 67 28 7a 54  okenizeString(zT
30cc5 6f 6b 65 6e 69 7a 65 72 2c 20 26 6e 29 3b 0a 20  okenizer, &n);. 
30cc6 20 74 6f 6b 65 6e 4c 69 73 74 54 6f 49 64 4c 69   tokenListToIdLi
30cc7 73 74 28 70 53 70 65 63 2d 3e 61 7a 54 6f 6b 65  st(pSpec->azToke
30cc8 6e 69 7a 65 72 29 3b 0a 0a 20 20 72 65 74 75 72  nizer);..  retur
30cc9 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
30cca 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
30ccb 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
30ccc 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 65 73  atement that des
30ccd 63 72 69 62 65 73 20 74 68 65 20 73 63 68 65 6d  cribes the schem
30cce 61 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74  a of.** the virt
30ccf 75 61 6c 20 74 61 62 6c 65 2e 20 20 52 65 74 75  ual table.  Retu
30cd0 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
30cd1 74 68 69 73 20 73 63 68 65 6d 61 20 73 74 72 69  this schema stri
30cd2 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20  ng..**.** Space 
30cd3 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
30cd4 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
30cd5 28 29 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65  () and should be
30cd6 20 66 72 65 65 64 0a 2a 2a 20 75 73 69 6e 67 20   freed.** using 
30cd7 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 0a  sqlite3_free()..
30cd8 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
30cd9 66 75 6c 6c 74 65 78 74 53 63 68 65 6d 61 28 0a  fulltextSchema(.
30cda 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20    int nColumn,  
30cdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30cdc 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
30cdd 75 6d 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  umns */.  const 
30cde 63 68 61 72 20 2a 63 6f 6e 73 74 2a 20 61 7a 43  char *const* azC
30cdf 6f 6c 75 6d 6e 2c 20 20 2f 2a 20 4c 69 73 74 20  olumn,  /* List 
30ce0 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
30ce1 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
30ce2 6c 65 4e 61 6d 65 20 20 20 20 20 20 20 20 2f 2a  leName        /*
30ce3 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   Name of the tab
30ce4 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  le */.){.  int i
30ce5 3b 0a 20 20 63 68 61 72 20 2a 7a 53 63 68 65 6d  ;.  char *zSchem
30ce6 61 2c 20 2a 7a 4e 65 78 74 3b 0a 20 20 63 6f 6e  a, *zNext;.  con
30ce7 73 74 20 63 68 61 72 20 2a 7a 53 65 70 20 3d 20  st char *zSep = 
30ce8 22 28 22 3b 0a 20 20 7a 53 63 68 65 6d 61 20 3d  "(";.  zSchema =
30ce9 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
30cea 28 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 78  ("CREATE TABLE x
30ceb 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ");.  for(i=0; i
30cec 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  <nColumn; i++){.
30ced 20 20 20 20 7a 4e 65 78 74 20 3d 20 73 71 6c 69      zNext = sqli
30cee 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 25  te3_mprintf("%s%
30cef 73 25 51 22 2c 20 7a 53 63 68 65 6d 61 2c 20 7a  s%Q", zSchema, z
30cf0 53 65 70 2c 20 61 7a 43 6f 6c 75 6d 6e 5b 69 5d  Sep, azColumn[i]
30cf1 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
30cf2 72 65 65 28 7a 53 63 68 65 6d 61 29 3b 0a 20 20  ree(zSchema);.  
30cf3 20 20 7a 53 63 68 65 6d 61 20 3d 20 7a 4e 65 78    zSchema = zNex
30cf4 74 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 2c  t;.    zSep = ",
30cf5 22 3b 0a 20 20 7d 0a 20 20 7a 4e 65 78 74 20 3d  ";.  }.  zNext =
30cf6 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
30cf7 28 22 25 73 2c 25 51 20 48 49 44 44 45 4e 22 2c  ("%s,%Q HIDDEN",
30cf8 20 7a 53 63 68 65 6d 61 2c 20 7a 54 61 62 6c 65   zSchema, zTable
30cf9 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
30cfa 5f 66 72 65 65 28 7a 53 63 68 65 6d 61 29 3b 0a  _free(zSchema);.
30cfb 20 20 7a 53 63 68 65 6d 61 20 3d 20 7a 4e 65 78    zSchema = zNex
30cfc 74 3b 0a 20 20 7a 4e 65 78 74 20 3d 20 73 71 6c  t;.  zNext = sql
30cfd 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
30cfe 2c 64 6f 63 69 64 20 48 49 44 44 45 4e 29 22 2c  ,docid HIDDEN)",
30cff 20 7a 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c   zSchema);.  sql
30d00 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68 65 6d  ite3_free(zSchem
30d01 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65  a);.  return zNe
30d02 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 69  xt;.}../*.** Bui
30d03 6c 64 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33  ld a new sqlite3
30d04 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 20  _vtab structure 
30d05 74 68 61 74 20 77 69 6c 6c 20 64 65 73 63 72 69  that will descri
30d06 62 65 20 74 68 65 0a 2a 2a 20 66 75 6c 6c 74 65  be the.** fullte
30d07 78 74 20 69 6e 64 65 78 20 64 65 66 69 6e 65 64  xt index defined
30d08 20 62 79 20 73 70 65 63 2e 0a 2a 2f 0a 73 74 61   by spec..*/.sta
30d09 74 69 63 20 69 6e 74 20 63 6f 6e 73 74 72 75 63  tic int construc
30d0a 74 56 74 61 62 28 0a 20 20 73 71 6c 69 74 65 33  tVtab(.  sqlite3
30d0b 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
30d0c 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 69 74 65     /* The SQLite
30d0d 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
30d0e 74 69 6f 6e 20 2a 2f 0a 20 20 66 74 73 33 48 61  tion */.  fts3Ha
30d0f 73 68 20 2a 70 48 61 73 68 2c 20 20 20 20 20 20  sh *pHash,      
30d10 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c      /* Hash tabl
30d11 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f 6b  e containing tok
30d12 65 6e 69 7a 65 72 73 20 2a 2f 0a 20 20 54 61 62  enizers */.  Tab
30d13 6c 65 53 70 65 63 20 2a 73 70 65 63 2c 20 20 20  leSpec *spec,   
30d14 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 64         /* Parsed
30d15 20 73 70 65 63 20 69 6e 66 6f 72 6d 61 74 69 6f   spec informatio
30d16 6e 20 66 72 6f 6d 20 70 61 72 73 65 53 70 65 63  n from parseSpec
30d17 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  () */.  sqlite3_
30d18 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 20  vtab **ppVTab,  
30d19 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72    /* Write the r
30d1a 65 73 75 6c 74 69 6e 67 20 76 74 61 62 20 73 74  esulting vtab st
30d1b 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a  ructure here */.
30d1c 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 20 20    char **pzErr  
30d1d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
30d1e 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d  rite any error m
30d1f 65 73 73 61 67 65 20 68 65 72 65 20 2a 2f 0a 29  essage here */.)
30d20 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  {.  int rc;.  in
30d21 74 20 6e 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  t n;.  fulltext_
30d22 76 74 61 62 20 2a 76 20 3d 20 30 3b 0a 20 20 63  vtab *v = 0;.  c
30d23 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
30d24 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 6d  enizer_module *m
30d25 20 3d 20 4e 55 4c 4c 3b 0a 20 20 63 68 61 72 20   = NULL;.  char 
30d26 2a 73 63 68 65 6d 61 3b 0a 0a 20 20 63 68 61 72  *schema;..  char
30d27 20 63 6f 6e 73 74 20 2a 7a 54 6f 6b 3b 20 20 20   const *zTok;   
30d28 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
30d29 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 75 73   tokenizer to us
30d2a 65 20 66 6f 72 20 74 68 69 73 20 66 74 73 20 74  e for this fts t
30d2b 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  able */.  int nT
30d2c 6f 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ok;             
30d2d 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
30d2e 20 7a 54 6f 6b 2c 20 69 6e 63 6c 75 64 69 6e 67   zTok, including
30d2f 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 6f 72 20   nul terminator 
30d30 2a 2f 0a 0a 20 20 76 20 3d 20 28 66 75 6c 6c 74  */..  v = (fullt
30d31 65 78 74 5f 76 74 61 62 20 2a 29 20 73 71 6c 69  ext_vtab *) sqli
30d32 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
30d33 66 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 29  f(fulltext_vtab)
30d34 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
30d35 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
30d36 4d 45 4d 3b 0a 20 20 43 4c 45 41 52 28 76 29 3b  MEM;.  CLEAR(v);
30d37 0a 20 20 2f 2a 20 73 71 6c 69 74 65 20 77 69 6c  .  /* sqlite wil
30d38 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 76 2d 3e  l initialize v->
30d39 62 61 73 65 20 2a 2f 0a 20 20 76 2d 3e 64 62 20  base */.  v->db 
30d3a 3d 20 64 62 3b 0a 20 20 76 2d 3e 7a 44 62 20 3d  = db;.  v->zDb =
30d3b 20 73 70 65 63 2d 3e 7a 44 62 3b 20 20 20 20 20   spec->zDb;     
30d3c 20 20 2f 2a 20 46 72 65 65 64 20 77 68 65 6e 20    /* Freed when 
30d3d 61 7a 43 6f 6c 75 6d 6e 20 69 73 20 66 72 65 65  azColumn is free
30d3e 64 20 2a 2f 0a 20 20 76 2d 3e 7a 4e 61 6d 65 20  d */.  v->zName 
30d3f 3d 20 73 70 65 63 2d 3e 7a 4e 61 6d 65 3b 20 20  = spec->zName;  
30d40 20 2f 2a 20 46 72 65 65 64 20 77 68 65 6e 20 61   /* Freed when a
30d41 7a 43 6f 6c 75 6d 6e 20 69 73 20 66 72 65 65 64  zColumn is freed
30d42 20 2a 2f 0a 20 20 76 2d 3e 6e 43 6f 6c 75 6d 6e   */.  v->nColumn
30d43 20 3d 20 73 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e   = spec->nColumn
30d44 3b 0a 20 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74  ;.  v->azContent
30d45 43 6f 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e 61  Column = spec->a
30d46 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b 0a  zContentColumn;.
30d47 20 20 73 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e    spec->azConten
30d48 74 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 76  tColumn = 0;.  v
30d49 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 73 70 65  ->azColumn = spe
30d4a 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 3b 0a 20 20 73  c->azColumn;.  s
30d4b 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20  pec->azColumn = 
30d4c 30 3b 0a 0a 20 20 69 66 28 20 73 70 65 63 2d 3e  0;..  if( spec->
30d4d 61 7a 54 6f 6b 65 6e 69 7a 65 72 3d 3d 30 20 29  azTokenizer==0 )
30d4e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
30d4f 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a  ITE_NOMEM;.  }..
30d50 20 20 7a 54 6f 6b 20 3d 20 73 70 65 63 2d 3e 61    zTok = spec->a
30d51 7a 54 6f 6b 65 6e 69 7a 65 72 5b 30 5d 3b 20 0a  zTokenizer[0]; .
30d52 20 20 69 66 28 20 21 7a 54 6f 6b 20 29 7b 0a 20    if( !zTok ){. 
30d53 20 20 20 7a 54 6f 6b 20 3d 20 22 73 69 6d 70 6c     zTok = "simpl
30d54 65 22 3b 0a 20 20 7d 0a 20 20 6e 54 6f 6b 20 3d  e";.  }.  nTok =
30d55 20 73 74 72 6c 65 6e 28 7a 54 6f 6b 29 2b 31 3b   strlen(zTok)+1;
30d56 0a 0a 20 20 6d 20 3d 20 28 73 71 6c 69 74 65 33  ..  m = (sqlite3
30d57 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
30d58 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33 48  e *)sqlite3Fts3H
30d59 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a  ashFind(pHash, z
30d5a 54 6f 6b 2c 20 6e 54 6f 6b 29 3b 0a 20 20 69 66  Tok, nTok);.  if
30d5b 28 20 21 6d 20 29 7b 0a 20 20 20 20 2a 70 7a 45  ( !m ){.    *pzE
30d5c 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  rr = sqlite3_mpr
30d5d 69 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f  intf("unknown to
30d5e 6b 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20 73 70  kenizer: %s", sp
30d5f 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b  ec->azTokenizer[
30d60 30 5d 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  0]);.    rc = SQ
30d61 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
30d62 67 6f 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 0a 20  goto err;.  }.. 
30d63 20 66 6f 72 28 6e 3d 30 3b 20 73 70 65 63 2d 3e   for(n=0; spec->
30d64 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b 6e 5d 3b 20  azTokenizer[n]; 
30d65 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 6e 20 29  n++){}.  if( n )
30d66 7b 0a 20 20 20 20 72 63 20 3d 20 6d 2d 3e 78 43  {.    rc = m->xC
30d67 72 65 61 74 65 28 6e 2d 31 2c 20 28 63 6f 6e 73  reate(n-1, (cons
30d68 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a 29 26 73  t char*const*)&s
30d69 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72  pec->azTokenizer
30d6a 5b 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  [1],.           
30d6b 20 20 20 20 20 20 20 20 20 26 76 2d 3e 70 54 6f           &v->pTo
30d6c 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 7d 65 6c 73  kenizer);.  }els
30d6d 65 7b 0a 20 20 20 20 72 63 20 3d 20 6d 2d 3e 78  e{.    rc = m->x
30d6e 43 72 65 61 74 65 28 30 2c 20 30 2c 20 26 76 2d  Create(0, 0, &v-
30d6f 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20  >pTokenizer);.  
30d70 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  }.  if( rc!=SQLI
30d71 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
30d72 3b 0a 20 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65  ;.  v->pTokenize
30d73 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 6d 3b 0a  r->pModule = m;.
30d74 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 76 65 72 69  .  /* TODO: veri
30d75 66 79 20 74 68 65 20 65 78 69 73 74 65 6e 63 65  fy the existence
30d76 20 6f 66 20 62 61 63 6b 69 6e 67 20 74 61 62 6c   of backing tabl
30d77 65 73 20 66 6f 6f 5f 63 6f 6e 74 65 6e 74 2c 20  es foo_content, 
30d78 66 6f 6f 5f 74 65 72 6d 20 2a 2f 0a 0a 20 20 73  foo_term */..  s
30d79 63 68 65 6d 61 20 3d 20 66 75 6c 6c 74 65 78 74  chema = fulltext
30d7a 53 63 68 65 6d 61 28 76 2d 3e 6e 43 6f 6c 75 6d  Schema(v->nColum
30d7b 6e 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 63  n, (const char*c
30d7c 6f 6e 73 74 2a 29 76 2d 3e 61 7a 43 6f 6c 75 6d  onst*)v->azColum
30d7d 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n,.             
30d7e 20 20 20 20 20 20 20 20 20 20 20 20 20 73 70 65               spe
30d7f 63 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 72 63 20  c->zName);.  rc 
30d80 3d 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  = sqlite3_declar
30d81 65 5f 76 74 61 62 28 64 62 2c 20 73 63 68 65 6d  e_vtab(db, schem
30d82 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  a);.  sqlite3_fr
30d83 65 65 28 73 63 68 65 6d 61 29 3b 0a 20 20 69 66  ee(schema);.  if
30d84 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
30d85 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 6d  ) goto err;..  m
30d86 65 6d 73 65 74 28 76 2d 3e 70 46 75 6c 6c 74 65  emset(v->pFullte
30d87 78 74 53 74 61 74 65 6d 65 6e 74 73 2c 20 30 2c  xtStatements, 0,
30d88 20 73 69 7a 65 6f 66 28 76 2d 3e 70 46 75 6c 6c   sizeof(v->pFull
30d89 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 29 29  textStatements))
30d8a 3b 0a 0a 20 20 2f 2a 20 49 6e 64 69 63 61 74 65  ;..  /* Indicate
30d8b 20 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72   that the buffer
30d8c 20 69 73 20 6e 6f 74 20 6c 69 76 65 2e 20 2a 2f   is not live. */
30d8d 0a 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  .  v->nPendingDa
30d8e 74 61 20 3d 20 2d 31 3b 0a 0a 20 20 2a 70 70 56  ta = -1;..  *ppV
30d8f 54 61 62 20 3d 20 26 76 2d 3e 62 61 73 65 3b 0a  Tab = &v->base;.
30d90 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
30d91 33 20 43 6f 6e 6e 65 63 74 20 25 70 5c 6e 22 2c  3 Connect %p\n",
30d92 20 76 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20   v));..  return 
30d93 72 63 3b 0a 0a 65 72 72 3a 0a 20 20 66 75 6c 6c  rc;..err:.  full
30d94 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74 72 6f  text_vtab_destro
30d95 79 28 76 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  y(v);.  return r
30d96 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  c;.}..static int
30d97 20 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65 63 74   fulltextConnect
30d98 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
30d99 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20  .  void *pAux,. 
30d9a 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
30d9b 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
30d9c 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  v,.  sqlite3_vta
30d9d 62 20 2a 2a 70 70 56 54 61 62 2c 0a 20 20 63 68  b **ppVTab,.  ch
30d9e 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20  ar **pzErr.){.  
30d9f 54 61 62 6c 65 53 70 65 63 20 73 70 65 63 3b 0a  TableSpec spec;.
30da0 20 20 69 6e 74 20 72 63 20 3d 20 70 61 72 73 65    int rc = parse
30da1 53 70 65 63 28 26 73 70 65 63 2c 20 61 72 67 63  Spec(&spec, argc
30da2 2c 20 61 72 67 76 2c 20 70 7a 45 72 72 29 3b 0a  , argv, pzErr);.
30da3 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
30da4 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
30da5 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 73 74 72 75  ..  rc = constru
30da6 63 74 56 74 61 62 28 64 62 2c 20 28 66 74 73 33  ctVtab(db, (fts3
30da7 48 61 73 68 20 2a 29 70 41 75 78 2c 20 26 73 70  Hash *)pAux, &sp
30da8 65 63 2c 20 70 70 56 54 61 62 2c 20 70 7a 45 72  ec, ppVTab, pzEr
30da9 72 29 3b 0a 20 20 63 6c 65 61 72 54 61 62 6c 65  r);.  clearTable
30daa 53 70 65 63 28 26 73 70 65 63 29 3b 0a 20 20 72  Spec(&spec);.  r
30dab 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
30dac 54 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61  The %_content ta
30dad 62 6c 65 20 68 6f 6c 64 73 20 74 68 65 20 74 65  ble holds the te
30dae 78 74 20 6f 66 20 65 61 63 68 20 64 6f 63 75 6d  xt of each docum
30daf 65 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 74 68 65  ent, with.** the
30db0 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e 20 65 78   docid column ex
30db1 70 6f 73 65 64 20 61 73 20 74 68 65 20 53 51 4c  posed as the SQL
30db2 69 74 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68  ite rowid for th
30db3 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 54  e table..*/./* T
30db4 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
30db5 63 6f 6d 6d 65 6e 74 20 6e 65 65 64 73 20 65 6c  comment needs el
30db6 61 62 6f 72 61 74 69 6f 6e 20 74 6f 20 6d 61 74  aboration to mat
30db7 63 68 20 74 68 65 20 75 70 64 61 74 65 64 0a 2a  ch the updated.*
30db8 2a 20 63 6f 64 65 2e 20 20 57 6f 72 6b 20 69 74  * code.  Work it
30db9 20 69 6e 74 6f 20 74 68 65 20 74 6f 70 2d 6f 66   into the top-of
30dba 2d 66 69 6c 65 20 63 6f 6d 6d 65 6e 74 20 61 74  -file comment at
30dbb 20 74 68 61 74 20 74 69 6d 65 2e 0a 2a 2f 0a 73   that time..*/.s
30dbc 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
30dbd 78 74 43 72 65 61 74 65 28 73 71 6c 69 74 65 33  xtCreate(sqlite3
30dbe 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 41 75 78   *db, void *pAux
30dbf 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
30dc0 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
30dc1 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
30dc2 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 0a   * const *argv,.
30dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30dc4 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
30dc5 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c  3_vtab **ppVTab,
30dc6 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b 0a   char **pzErr){.
30dc7 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 61 62 6c    int rc;.  Tabl
30dc8 65 53 70 65 63 20 73 70 65 63 3b 0a 20 20 53 74  eSpec spec;.  St
30dc9 72 69 6e 67 42 75 66 66 65 72 20 73 63 68 65 6d  ringBuffer schem
30dca 61 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  a;.  FTSTRACE(("
30dcb 46 54 53 33 20 43 72 65 61 74 65 5c 6e 22 29 29  FTS3 Create\n"))
30dcc 3b 0a 0a 20 20 72 63 20 3d 20 70 61 72 73 65 53  ;..  rc = parseS
30dcd 70 65 63 28 26 73 70 65 63 2c 20 61 72 67 63 2c  pec(&spec, argc,
30dce 20 61 72 67 76 2c 20 70 7a 45 72 72 29 3b 0a 20   argv, pzErr);. 
30dcf 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
30dd0 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
30dd1 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66  .  initStringBuf
30dd2 66 65 72 28 26 73 63 68 65 6d 61 29 3b 0a 20 20  fer(&schema);.  
30dd3 61 70 70 65 6e 64 28 26 73 63 68 65 6d 61 2c 20  append(&schema, 
30dd4 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25 5f  "CREATE TABLE %_
30dd5 63 6f 6e 74 65 6e 74 28 22 29 3b 0a 20 20 61 70  content(");.  ap
30dd6 70 65 6e 64 28 26 73 63 68 65 6d 61 2c 20 22 20  pend(&schema, " 
30dd7 20 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50   docid INTEGER P
30dd8 52 49 4d 41 52 59 20 4b 45 59 2c 22 29 3b 0a 20  RIMARY KEY,");. 
30dd9 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73 63 68   appendList(&sch
30dda 65 6d 61 2c 20 73 70 65 63 2e 6e 43 6f 6c 75 6d  ema, spec.nColum
30ddb 6e 2c 20 73 70 65 63 2e 61 7a 43 6f 6e 74 65 6e  n, spec.azConten
30ddc 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70 65  tColumn);.  appe
30ddd 6e 64 28 26 73 63 68 65 6d 61 2c 20 22 29 22 29  nd(&schema, ")")
30dde 3b 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78 65  ;.  rc = sql_exe
30ddf 63 28 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20  c(db, spec.zDb, 
30de0 73 70 65 63 2e 7a 4e 61 6d 65 2c 20 73 74 72 69  spec.zName, stri
30de1 6e 67 42 75 66 66 65 72 44 61 74 61 28 26 73 63  ngBufferData(&sc
30de2 68 65 6d 61 29 29 3b 0a 20 20 73 74 72 69 6e 67  hema));.  string
30de3 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 73  BufferDestroy(&s
30de4 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 72 63  chema);.  if( rc
30de5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
30de6 74 6f 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20  to out;..  rc = 
30de7 73 71 6c 5f 65 78 65 63 28 64 62 2c 20 73 70 65  sql_exec(db, spe
30de8 63 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61 6d  c.zDb, spec.zNam
30de9 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
30dea 20 20 20 22 63 72 65 61 74 65 20 74 61 62 6c 65     "create table
30deb 20 25 5f 73 65 67 6d 65 6e 74 73 28 22 0a 20 20   %_segments(".  
30dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20                " 
30ded 20 62 6c 6f 63 6b 69 64 20 49 4e 54 45 47 45 52   blockid INTEGER
30dee 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 22 0a 20   PRIMARY KEY,". 
30def 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
30df0 20 20 62 6c 6f 63 6b 20 62 6c 6f 62 22 0a 20 20    block blob".  
30df1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 29                ")
30df2 3b 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ;".             
30df3 20 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d     );.  if( rc!=
30df4 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
30df5 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71   out;..  rc = sq
30df6 6c 5f 65 78 65 63 28 64 62 2c 20 73 70 65 63 2e  l_exec(db, spec.
30df7 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61 6d 65 2c  zDb, spec.zName,
30df8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
30df9 20 22 63 72 65 61 74 65 20 74 61 62 6c 65 20 25   "create table %
30dfa 5f 73 65 67 64 69 72 28 22 0a 20 20 20 20 20 20  _segdir(".      
30dfb 20 20 20 20 20 20 20 20 20 20 22 20 20 6c 65 76            "  lev
30dfc 65 6c 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20  el integer,".   
30dfd 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
30dfe 69 64 78 20 69 6e 74 65 67 65 72 2c 22 0a 20 20  idx integer,".  
30dff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20                " 
30e00 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20 69 6e 74   start_block int
30e01 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20  eger,".         
30e02 20 20 20 20 20 20 20 22 20 20 6c 65 61 76 65 73         "  leaves
30e03 5f 65 6e 64 5f 62 6c 6f 63 6b 20 69 6e 74 65 67  _end_block integ
30e04 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20  er,".           
30e05 20 20 20 20 20 22 20 20 65 6e 64 5f 62 6c 6f 63       "  end_bloc
30e06 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20 20  k integer,".    
30e07 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20 72              "  r
30e08 6f 6f 74 20 62 6c 6f 62 2c 22 0a 20 20 20 20 20  oot blob,".     
30e09 20 20 20 20 20 20 20 20 20 20 20 22 20 20 70 72             "  pr
30e0a 69 6d 61 72 79 20 6b 65 79 28 6c 65 76 65 6c 2c  imary key(level,
30e0b 20 69 64 78 29 22 0a 20 20 20 20 20 20 20 20 20   idx)".         
30e0c 20 20 20 20 20 20 20 22 29 3b 22 29 3b 0a 20 20         ");");.  
30e0d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
30e0e 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20  K ) goto out;.. 
30e0f 20 72 63 20 3d 20 63 6f 6e 73 74 72 75 63 74 56   rc = constructV
30e10 74 61 62 28 64 62 2c 20 28 66 74 73 33 48 61 73  tab(db, (fts3Has
30e11 68 20 2a 29 70 41 75 78 2c 20 26 73 70 65 63 2c  h *)pAux, &spec,
30e12 20 70 70 56 54 61 62 2c 20 70 7a 45 72 72 29 3b   ppVTab, pzErr);
30e13 0a 0a 6f 75 74 3a 0a 20 20 63 6c 65 61 72 54 61  ..out:.  clearTa
30e14 62 6c 65 53 70 65 63 28 26 73 70 65 63 29 3b 0a  bleSpec(&spec);.
30e15 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
30e16 2f 2a 20 44 65 63 69 64 65 20 68 6f 77 20 74 6f  /* Decide how to
30e17 20 68 61 6e 64 6c 65 20 61 6e 20 53 51 4c 20 71   handle an SQL q
30e18 75 65 72 79 2e 20 2a 2f 0a 73 74 61 74 69 63 20  uery. */.static 
30e19 69 6e 74 20 66 75 6c 6c 74 65 78 74 42 65 73 74  int fulltextBest
30e1a 49 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 76 74  Index(sqlite3_vt
30e1b 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74  ab *pVTab, sqlit
30e1c 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
30e1d 49 6e 66 6f 29 7b 0a 20 20 66 75 6c 6c 74 65 78  Info){.  fulltex
30e1e 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c  t_vtab *v = (ful
30e1f 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54  ltext_vtab *)pVT
30e20 61 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46  ab;.  int i;.  F
30e21 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 42  TSTRACE(("FTS3 B
30e22 65 73 74 49 6e 64 65 78 5c 6e 22 29 29 3b 0a 0a  estIndex\n"));..
30e23 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e    for(i=0; i<pIn
30e24 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  fo->nConstraint;
30e25 20 2b 2b 69 29 7b 0a 20 20 20 20 63 6f 6e 73 74   ++i){.    const
30e26 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
30e27 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
30e28 20 2a 70 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20   *pConstraint;. 
30e29 20 20 20 70 43 6f 6e 73 74 72 61 69 6e 74 20 3d     pConstraint =
30e2a 20 26 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72   &pInfo->aConstr
30e2b 61 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28  aint[i];.    if(
30e2c 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 75 73   pConstraint->us
30e2d 61 62 6c 65 20 29 20 7b 0a 20 20 20 20 20 20 69  able ) {.      i
30e2e 66 28 20 28 70 43 6f 6e 73 74 72 61 69 6e 74 2d  f( (pConstraint-
30e2f 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c 20  >iColumn==-1 || 
30e30 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f  pConstraint->iCo
30e31 6c 75 6d 6e 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e  lumn==v->nColumn
30e32 2b 31 29 20 26 26 0a 20 20 20 20 20 20 20 20 20  +1) &&.         
30e33 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 6f 70   pConstraint->op
30e34 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
30e35 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29 7b 0a  ONSTRAINT_EQ ){.
30e36 20 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69          pInfo->i
30e37 64 78 4e 75 6d 20 3d 20 51 55 45 52 59 5f 44 4f  dxNum = QUERY_DO
30e38 43 49 44 3b 20 20 20 20 20 20 2f 2a 20 6c 6f 6f  CID;      /* loo
30e39 6b 75 70 20 62 79 20 64 6f 63 69 64 20 2a 2f 0a  kup by docid */.
30e3a 20 20 20 20 20 20 20 20 46 54 53 54 52 41 43 45          FTSTRACE
30e3b 28 28 22 46 54 53 33 20 51 55 45 52 59 5f 44 4f  (("FTS3 QUERY_DO
30e3c 43 49 44 5c 6e 22 29 29 3b 0a 20 20 20 20 20 20  CID\n"));.      
30e3d 7d 20 65 6c 73 65 20 69 66 28 20 70 43 6f 6e 73  } else if( pCons
30e3e 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3e  traint->iColumn>
30e3f 3d 30 20 26 26 20 70 43 6f 6e 73 74 72 61 69 6e  =0 && pConstrain
30e40 74 2d 3e 69 43 6f 6c 75 6d 6e 3c 3d 76 2d 3e 6e  t->iColumn<=v->n
30e41 43 6f 6c 75 6d 6e 20 26 26 0a 20 20 20 20 20 20  Column &&.      
30e42 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6e 73             pCons
30e43 74 72 61 69 6e 74 2d 3e 6f 70 3d 3d 53 51 4c 49  traint->op==SQLI
30e44 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
30e45 49 4e 54 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20  INT_MATCH ){.   
30e46 20 20 20 20 20 2f 2a 20 66 75 6c 6c 2d 74 65 78       /* full-tex
30e47 74 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20  t search */.    
30e48 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75      pInfo->idxNu
30e49 6d 20 3d 20 51 55 45 52 59 5f 46 55 4c 4c 54 45  m = QUERY_FULLTE
30e4a 58 54 20 2b 20 70 43 6f 6e 73 74 72 61 69 6e 74  XT + pConstraint
30e4b 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
30e4c 20 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54     FTSTRACE(("FT
30e4d 53 33 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58  S3 QUERY_FULLTEX
30e4e 54 20 25 64 5c 6e 22 2c 20 70 43 6f 6e 73 74 72  T %d\n", pConstr
30e4f 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 29 29 3b  aint->iColumn));
30e50 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 63 6f  .      } else co
30e51 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 70  ntinue;..      p
30e52 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
30e53 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e  tUsage[i].argvIn
30e54 64 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  dex = 1;.      p
30e55 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
30e56 74 55 73 61 67 65 5b 69 5d 2e 6f 6d 69 74 20 3d  tUsage[i].omit =
30e57 20 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 6e   1;..      /* An
30e58 20 61 72 62 69 74 72 61 72 79 20 76 61 6c 75 65   arbitrary value
30e59 20 66 6f 72 20 6e 6f 77 2e 0a 20 20 20 20 20 20   for now..      
30e5a 20 2a 20 54 4f 44 4f 3a 20 50 65 72 68 61 70 73   * TODO: Perhaps
30e5b 20 64 6f 63 69 64 20 6d 61 74 63 68 65 73 20 73   docid matches s
30e5c 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64 65  hould be conside
30e5d 72 65 64 20 63 68 65 61 70 65 72 20 74 68 61 6e  red cheaper than
30e5e 0a 20 20 20 20 20 20 20 2a 20 66 75 6c 6c 2d 74  .       * full-t
30e5f 65 78 74 20 73 65 61 72 63 68 65 73 2e 20 2a 2f  ext searches. */
30e60 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 65 73  .      pInfo->es
30e61 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 31 2e  timatedCost = 1.
30e62 30 3b 20 20 20 0a 0a 20 20 20 20 20 20 72 65 74  0;   ..      ret
30e63 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
30e64 20 20 20 7d 0a 20 20 7d 0a 20 20 70 49 6e 66 6f     }.  }.  pInfo
30e65 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55 45 52 59  ->idxNum = QUERY
30e66 5f 47 45 4e 45 52 49 43 3b 0a 20 20 72 65 74 75  _GENERIC;.  retu
30e67 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
30e68 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
30e69 74 65 78 74 44 69 73 63 6f 6e 6e 65 63 74 28 73  textDisconnect(s
30e6a 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54  qlite3_vtab *pVT
30e6b 61 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45 28  ab){.  FTSTRACE(
30e6c 28 22 46 54 53 33 20 44 69 73 63 6f 6e 6e 65 63  ("FTS3 Disconnec
30e6d 74 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 29 29  t %p\n", pVTab))
30e6e 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  ;.  fulltext_vta
30e6f 62 5f 64 65 73 74 72 6f 79 28 28 66 75 6c 6c 74  b_destroy((fullt
30e70 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61 62  ext_vtab *)pVTab
30e71 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
30e72 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63  TE_OK;.}..static
30e73 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 44 65 73   int fulltextDes
30e74 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 76 74 61  troy(sqlite3_vta
30e75 62 20 2a 70 56 54 61 62 29 7b 0a 20 20 66 75 6c  b *pVTab){.  ful
30e76 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20  ltext_vtab *v = 
30e77 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
30e78 29 70 56 54 61 62 3b 0a 20 20 69 6e 74 20 72 63  )pVTab;.  int rc
30e79 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  ;..  FTSTRACE(("
30e7a 46 54 53 33 20 44 65 73 74 72 6f 79 20 25 70 5c  FTS3 Destroy %p\
30e7b 6e 22 2c 20 70 56 54 61 62 29 29 3b 0a 20 20 72  n", pVTab));.  r
30e7c 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 76 2d 3e  c = sql_exec(v->
30e7d 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a  db, v->zDb, v->z
30e7e 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
30e7f 20 20 20 20 20 20 22 64 72 6f 70 20 74 61 62 6c        "drop tabl
30e80 65 20 69 66 20 65 78 69 73 74 73 20 25 5f 63 6f  e if exists %_co
30e81 6e 74 65 6e 74 3b 22 0a 20 20 20 20 20 20 20 20  ntent;".        
30e82 20 20 20 20 20 20 20 20 22 64 72 6f 70 20 74 61          "drop ta
30e83 62 6c 65 20 69 66 20 65 78 69 73 74 73 20 25 5f  ble if exists %_
30e84 73 65 67 6d 65 6e 74 73 3b 22 0a 20 20 20 20 20  segments;".     
30e85 20 20 20 20 20 20 20 20 20 20 20 22 64 72 6f 70             "drop
30e86 20 74 61 62 6c 65 20 69 66 20 65 78 69 73 74 73   table if exists
30e87 20 25 5f 73 65 67 64 69 72 3b 22 0a 20 20 20 20   %_segdir;".    
30e88 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20              );. 
30e89 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
30e8a 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
30e8b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
30e8c 5f 64 65 73 74 72 6f 79 28 28 66 75 6c 6c 74 65  _destroy((fullte
30e8d 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61 62 29  xt_vtab *)pVTab)
30e8e 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
30e8f 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  E_OK;.}..static 
30e90 69 6e 74 20 66 75 6c 6c 74 65 78 74 4f 70 65 6e  int fulltextOpen
30e91 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
30e92 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74  VTab, sqlite3_vt
30e93 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75  ab_cursor **ppCu
30e94 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78  rsor){.  fulltex
30e95 74 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20  t_cursor *c;..  
30e96 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75  c = (fulltext_cu
30e97 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  rsor *) sqlite3_
30e98 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66 75  malloc(sizeof(fu
30e99 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 29 29 3b  lltext_cursor));
30e9a 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20  .  if( c ){.    
30e9b 6d 65 6d 73 65 74 28 63 2c 20 30 2c 20 73 69 7a  memset(c, 0, siz
30e9c 65 6f 66 28 66 75 6c 6c 74 65 78 74 5f 63 75 72  eof(fulltext_cur
30e9d 73 6f 72 29 29 3b 0a 20 20 20 20 2f 2a 20 73 71  sor));.    /* sq
30e9e 6c 69 74 65 20 77 69 6c 6c 20 69 6e 69 74 69 61  lite will initia
30e9f 6c 69 7a 65 20 63 2d 3e 62 61 73 65 20 2a 2f 0a  lize c->base */.
30ea0 20 20 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20      *ppCursor = 
30ea1 26 63 2d 3e 62 61 73 65 3b 0a 20 20 20 20 46 54  &c->base;.    FT
30ea2 53 54 52 41 43 45 28 28 22 46 54 53 33 20 4f 70  STRACE(("FTS3 Op
30ea3 65 6e 20 25 70 3a 20 25 70 5c 6e 22 2c 20 70 56  en %p: %p\n", pV
30ea4 54 61 62 2c 20 63 29 29 3b 0a 20 20 20 20 72 65  Tab, c));.    re
30ea5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
30ea6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
30ea7 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
30ea8 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 46 72 65 65  ;.  }.}../* Free
30ea9 20 61 6c 6c 20 6f 66 20 74 68 65 20 64 79 6e 61   all of the dyna
30eaa 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
30eab 64 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79  d memory held by
30eac 20 74 68 65 0a 2a 2a 20 53 6e 69 70 70 65 74 0a   the.** Snippet.
30ead 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
30eae 6e 69 70 70 65 74 43 6c 65 61 72 28 53 6e 69 70  nippetClear(Snip
30eaf 70 65 74 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  pet *p){.  sqlit
30eb0 65 33 5f 66 72 65 65 28 70 2d 3e 61 4d 61 74 63  e3_free(p->aMatc
30eb1 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  h);.  sqlite3_fr
30eb2 65 65 28 70 2d 3e 7a 4f 66 66 73 65 74 29 3b 0a  ee(p->zOffset);.
30eb3 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
30eb4 2d 3e 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20 43  ->zSnippet);.  C
30eb5 4c 45 41 52 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  LEAR(p);.}../*.*
30eb6 2a 20 41 70 70 65 6e 64 20 61 20 73 69 6e 67 6c  * Append a singl
30eb7 65 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70  e entry to the p
30eb8 2d 3e 61 4d 61 74 63 68 5b 5d 20 6c 6f 67 2e 0a  ->aMatch[] log..
30eb9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
30eba 6e 69 70 70 65 74 41 70 70 65 6e 64 4d 61 74 63  nippetAppendMatc
30ebb 68 28 0a 20 20 53 6e 69 70 70 65 74 20 2a 70 2c  h(.  Snippet *p,
30ebc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
30ebd 2a 20 41 70 70 65 6e 64 20 74 68 65 20 65 6e 74  * Append the ent
30ebe 72 79 20 74 6f 20 74 68 69 73 20 73 6e 69 70 70  ry to this snipp
30ebf 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  et */.  int iCol
30ec0 2c 20 69 6e 74 20 69 54 65 72 6d 2c 20 20 20 20  , int iTerm,    
30ec1 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20    /* The column 
30ec2 61 6e 64 20 71 75 65 72 79 20 74 65 72 6d 20 2a  and query term *
30ec3 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 2c 20  /.  int iToken, 
30ec4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30ec5 20 4d 61 74 63 68 69 6e 67 20 74 6f 6b 65 6e 20   Matching token 
30ec6 69 6e 20 64 6f 63 75 6d 65 6e 74 20 2a 2f 0a 20  in document */. 
30ec7 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 6e 74   int iStart, int
30ec8 20 6e 42 79 74 65 20 20 20 20 20 2f 2a 20 4f 66   nByte     /* Of
30ec9 66 73 65 74 20 61 6e 64 20 73 69 7a 65 20 6f 66  fset and size of
30eca 20 74 68 65 20 6d 61 74 63 68 20 2a 2f 0a 29 7b   the match */.){
30ecb 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
30ecc 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20  ct snippetMatch 
30ecd 2a 70 4d 61 74 63 68 3b 0a 20 20 69 66 28 20 70  *pMatch;.  if( p
30ece 2d 3e 6e 4d 61 74 63 68 2b 31 3e 3d 70 2d 3e 6e  ->nMatch+1>=p->n
30ecf 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 2d 3e  Alloc ){.    p->
30ed0 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c 6c  nAlloc = p->nAll
30ed1 6f 63 2a 32 20 2b 20 31 30 3b 0a 20 20 20 20 70  oc*2 + 10;.    p
30ed2 2d 3e 61 4d 61 74 63 68 20 3d 20 73 71 6c 69 74  ->aMatch = sqlit
30ed3 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2d 3e 61 4d  e3_realloc(p->aM
30ed4 61 74 63 68 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 2a  atch, p->nAlloc*
30ed5 73 69 7a 65 6f 66 28 70 2d 3e 61 4d 61 74 63 68  sizeof(p->aMatch
30ed6 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20  [0]) );.    if( 
30ed7 70 2d 3e 61 4d 61 74 63 68 3d 3d 30 20 29 7b 0a  p->aMatch==0 ){.
30ed8 20 20 20 20 20 20 70 2d 3e 6e 4d 61 74 63 68 20        p->nMatch 
30ed9 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 41  = 0;.      p->nA
30eda 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20  lloc = 0;.      
30edb 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
30edc 7d 0a 20 20 69 20 3d 20 70 2d 3e 6e 4d 61 74 63  }.  i = p->nMatc
30edd 68 2b 2b 3b 0a 20 20 70 4d 61 74 63 68 20 3d 20  h++;.  pMatch = 
30ede 26 70 2d 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a 20  &p->aMatch[i];. 
30edf 20 70 4d 61 74 63 68 2d 3e 69 43 6f 6c 20 3d 20   pMatch->iCol = 
30ee0 69 43 6f 6c 3b 0a 20 20 70 4d 61 74 63 68 2d 3e  iCol;.  pMatch->
30ee1 69 54 65 72 6d 20 3d 20 69 54 65 72 6d 3b 0a 20  iTerm = iTerm;. 
30ee2 20 70 4d 61 74 63 68 2d 3e 69 54 6f 6b 65 6e 20   pMatch->iToken 
30ee3 3d 20 69 54 6f 6b 65 6e 3b 0a 20 20 70 4d 61 74  = iToken;.  pMat
30ee4 63 68 2d 3e 69 53 74 61 72 74 20 3d 20 69 53 74  ch->iStart = iSt
30ee5 61 72 74 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 6e  art;.  pMatch->n
30ee6 42 79 74 65 20 3d 20 6e 42 79 74 65 3b 0a 7d 0a  Byte = nByte;.}.
30ee7 0a 2f 2a 0a 2a 2a 20 53 69 7a 69 6e 67 20 69 6e  ./*.** Sizing in
30ee8 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68  formation for th
30ee9 65 20 63 69 72 63 75 6c 61 72 20 62 75 66 66 65  e circular buffe
30eea 72 20 75 73 65 64 20 69 6e 20 73 6e 69 70 70 65  r used in snippe
30eeb 74 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e  tOffsetsOfColumn
30eec 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54  ().*/.#define FT
30eed 53 33 5f 52 4f 54 4f 52 5f 53 5a 20 20 20 28 33  S3_ROTOR_SZ   (3
30eee 32 29 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f  2).#define FTS3_
30eef 52 4f 54 4f 52 5f 4d 41 53 4b 20 28 46 54 53 33  ROTOR_MASK (FTS3
30ef0 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29 0a 0a 2f 2a  _ROTOR_SZ-1)../*
30ef1 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20  .** Function to 
30ef2 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
30ef3 74 68 65 20 74 6f 6b 65 6e 73 20 6f 66 20 61 20  the tokens of a 
30ef4 63 6f 6d 70 69 6c 65 64 20 65 78 70 72 65 73 73  compiled express
30ef5 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70  ion..**.** Excep
30ef6 74 2c 20 73 6b 69 70 20 61 6c 6c 20 74 6f 6b 65  t, skip all toke
30ef7 6e 73 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2d  ns on the right-
30ef8 68 61 6e 64 20 73 69 64 65 20 6f 66 20 61 20 4e  hand side of a N
30ef9 4f 54 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20  OT operator..** 
30efa 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
30efb 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 6f   used to find to
30efc 6b 65 6e 73 20 61 73 20 70 61 72 74 20 6f 66 20  kens as part of 
30efd 73 6e 69 70 70 65 74 20 61 6e 64 20 6f 66 66 73  snippet and offs
30efe 65 74 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6f 6e  et.** generation
30eff 20 61 6e 64 20 77 65 20 64 6f 20 6e 74 20 77 61   and we do nt wa
30f00 6e 74 20 73 6e 69 70 70 65 74 73 20 61 6e 64 20  nt snippets and 
30f01 6f 66 66 73 65 74 73 20 74 6f 20 72 65 70 6f 72  offsets to repor
30f02 74 20 6d 61 74 63 68 65 73 0a 2a 2a 20 66 6f 72  t matches.** for
30f03 20 74 6f 6b 65 6e 73 20 6f 6e 20 74 68 65 20 52   tokens on the R
30f04 48 53 20 6f 66 20 61 20 4e 4f 54 2e 0a 2a 2f 0a  HS of a NOT..*/.
30f05 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 4e  static int fts3N
30f06 65 78 74 45 78 70 72 54 6f 6b 65 6e 28 46 74 73  extExprToken(Fts
30f07 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20  3Expr **ppExpr, 
30f08 69 6e 74 20 2a 70 69 54 6f 6b 65 6e 29 7b 0a 20  int *piToken){. 
30f09 20 46 74 73 33 45 78 70 72 20 2a 70 20 3d 20 2a   Fts3Expr *p = *
30f0a 70 70 45 78 70 72 3b 0a 20 20 69 6e 74 20 69 54  ppExpr;.  int iT
30f0b 6f 6b 65 6e 20 3d 20 2a 70 69 54 6f 6b 65 6e 3b  oken = *piToken;
30f0c 0a 20 20 69 66 28 20 69 54 6f 6b 65 6e 3c 30 20  .  if( iToken<0 
30f0d 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69  ){.    /* In thi
30f0e 73 20 63 61 73 65 20 74 68 65 20 65 78 70 72 65  s case the expre
30f0f 73 73 69 6f 6e 20 70 20 69 73 20 74 68 65 20 72  ssion p is the r
30f10 6f 6f 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73  oot of an expres
30f11 73 69 6f 6e 20 74 72 65 65 2e 0a 20 20 20 20 2a  sion tree..    *
30f12 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20 66 69  * Move to the fi
30f13 72 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  rst token in the
30f14 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
30f15 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69  ..    */.    whi
30f16 6c 65 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b 0a  le( p->pLeft ){.
30f17 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65        p = p->pLe
30f18 66 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 54  ft;.    }.    iT
30f19 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  oken = 0;.  }els
30f1a 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70 20  e{.    assert(p 
30f1b 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53  && p->eType==FTS
30f1c 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b 0a  QUERY_PHRASE );.
30f1d 20 20 20 20 69 66 28 20 69 54 6f 6b 65 6e 3c 28      if( iToken<(
30f1e 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  p->pPhrase->nTok
30f1f 65 6e 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69  en-1) ){.      i
30f20 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 7d 65 6c  Token++;.    }el
30f21 73 65 7b 0a 20 20 20 20 20 20 69 54 6f 6b 65 6e  se{.      iToken
30f22 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c   = 0;.      whil
30f23 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20 26 26  e( p->pParent &&
30f24 20 70 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 4c 65   p->pParent->pLe
30f25 66 74 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 20  ft!=p ){.       
30f26 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 61 72   assert( p->pPar
30f27 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70 20 29  ent->pRight==p )
30f28 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d  ;.        p = p-
30f29 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20  >pParent;.      
30f2a 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70  }.      p = p->p
30f2b 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 69 66  Parent;.      if
30f2c 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ( p ){.        a
30f2d 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68 74  ssert( p->pRight
30f2e 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70  !=0 );.        p
30f2f 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20   = p->pRight;.  
30f30 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e        while( p->
30f31 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  pLeft ){.       
30f32 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b     p = p->pLeft;
30f33 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
30f34 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
30f35 2a 70 70 45 78 70 72 20 3d 20 70 3b 0a 20 20 2a  *ppExpr = p;.  *
30f36 70 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65 6e  piToken = iToken
30f37 3b 0a 20 20 72 65 74 75 72 6e 20 70 3f 31 3a 30  ;.  return p?1:0
30f38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
30f39 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 65 78  n TRUE if the ex
30f3a 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 70 45  pression node pE
30f3b 78 70 72 20 69 73 20 6c 6f 63 61 74 65 64 20 62  xpr is located b
30f3c 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 52 48  eneath the.** RH
30f3d 53 20 6f 66 20 61 20 4e 4f 54 20 6f 70 65 72 61  S of a NOT opera
30f3e 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tor..*/.static i
30f3f 6e 74 20 66 74 73 33 45 78 70 72 42 65 6e 65 61  nt fts3ExprBenea
30f40 74 68 4e 6f 74 28 46 74 73 33 45 78 70 72 20 2a  thNot(Fts3Expr *
30f41 70 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20 2a  p){.  Fts3Expr *
30f42 70 50 61 72 65 6e 74 3b 0a 20 20 77 68 69 6c 65  pParent;.  while
30f43 28 20 70 20 29 7b 0a 20 20 20 20 70 50 61 72 65  ( p ){.    pPare
30f44 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b  nt = p->pParent;
30f45 0a 20 20 20 20 69 66 28 20 70 50 61 72 65 6e 74  .    if( pParent
30f46 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 65 54 79   && pParent->eTy
30f47 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 4f 54  pe==FTSQUERY_NOT
30f48 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70 52 69   && pParent->pRi
30f49 67 68 74 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20  ght==p ){.      
30f4a 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
30f4b 20 20 20 20 70 20 3d 20 70 50 61 72 65 6e 74 3b      p = pParent;
30f4c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
30f4d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 65 6e  .}../*.** Add en
30f4e 74 72 69 65 73 20 74 6f 20 70 53 6e 69 70 70 65  tries to pSnippe
30f4f 74 2d 3e 61 4d 61 74 63 68 5b 5d 20 66 6f 72 20  t->aMatch[] for 
30f50 65 76 65 72 79 20 6d 61 74 63 68 20 74 68 61 74  every match that
30f51 20 6f 63 63 75 72 73 20 61 67 61 69 6e 73 74 0a   occurs against.
30f52 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 7a 44 6f 63  ** document zDoc
30f53 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 77 68 69 63  [0..nDoc-1] whic
30f54 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 63  h is stored in c
30f55 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 2a  olumn iColumn..*
30f56 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e  /.static void sn
30f57 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66 43 6f  ippetOffsetsOfCo
30f58 6c 75 6d 6e 28 0a 20 20 66 75 6c 6c 74 65 78 74  lumn(.  fulltext
30f59 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20  _cursor *pCur,  
30f5a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75         /* The fu
30f5b 6c 6c 74 65 73 74 20 73 65 61 72 63 68 20 63 75  lltest search cu
30f5c 72 73 6f 72 20 2a 2f 0a 20 20 53 6e 69 70 70 65  rsor */.  Snippe
30f5d 74 20 2a 70 53 6e 69 70 70 65 74 2c 20 20 20 20  t *pSnippet,    
30f5e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
30f5f 53 6e 69 70 70 65 74 20 6f 62 6a 65 63 74 20 74  Snippet object t
30f60 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 2a  o be filled in *
30f61 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c  /.  int iColumn,
30f62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f63 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66     /* Index of f
30f64 75 6c 6c 74 65 78 74 20 74 61 62 6c 65 20 63 6f  ulltext table co
30f65 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  lumn */.  const 
30f66 63 68 61 72 20 2a 7a 44 6f 63 2c 20 20 20 20 20  char *zDoc,     
30f67 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74           /* Text
30f68 20 6f 66 20 74 68 65 20 66 75 6c 6c 74 65 78 74   of the fulltext
30f69 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f   table column */
30f6a 0a 20 20 69 6e 74 20 6e 44 6f 63 20 20 20 20 20  .  int nDoc     
30f6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f6c 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a    /* Length of z
30f6d 44 6f 63 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  Doc in bytes */.
30f6e 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  ){.  const sqlit
30f6f 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
30f70 75 6c 65 20 2a 70 54 4d 6f 64 75 6c 65 3b 20 20  ule *pTModule;  
30f71 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72  /* The tokenizer
30f72 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71 6c   module */.  sql
30f73 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
30f74 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20  pTokenizer;     
30f75 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
30f76 70 65 63 69 66 69 63 20 74 6f 6b 65 6e 69 7a 65  pecific tokenize
30f77 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74  r */.  sqlite3_t
30f78 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
30f79 2a 70 54 43 75 72 73 6f 72 3b 20 20 20 20 20 20  *pTCursor;      
30f7a 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 63    /* Tokenizer c
30f7b 75 72 73 6f 72 20 2a 2f 0a 20 20 66 75 6c 6c 74  ursor */.  fullt
30f7c 65 78 74 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  ext_vtab *pVtab;
30f7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f7e 2f 2a 20 54 68 65 20 66 75 6c 6c 20 74 65 78 74  /* The full text
30f7f 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
30f80 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  nColumn;        
30f81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f82 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
30f83 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e 64  lumns in the ind
30f84 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a  ex */.  int i, j
30f85 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
30f86 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30f87 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f  Loop counters */
30f88 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
30f89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f8a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
30f8b 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 6e 73 69  n code */.  unsi
30f8c 67 6e 65 64 20 69 6e 74 20 6d 61 74 63 68 2c 20  gned int match, 
30f8d 70 72 65 76 4d 61 74 63 68 3b 20 20 20 20 20 20  prevMatch;      
30f8e 20 2f 2a 20 50 68 72 61 73 65 20 73 65 61 72 63   /* Phrase searc
30f8f 68 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 20 20  h bitmasks */.  
30f90 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b  const char *zTok
30f91 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  en;             
30f92 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 74 6f 6b       /* Next tok
30f93 65 6e 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65  en from the toke
30f94 6e 69 7a 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  nizer */.  int n
30f95 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
30f96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f97 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 54 6f 6b 65  /* Size of zToke
30f98 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65 67 69  n */.  int iBegi
30f99 6e 2c 20 69 45 6e 64 2c 20 69 50 6f 73 3b 20 20  n, iEnd, iPos;  
30f9a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
30f9b 66 66 73 65 74 73 20 6f 66 20 62 65 67 69 6e 6e  ffsets of beginn
30f9c 69 6e 67 20 61 6e 64 20 65 6e 64 20 2a 2f 0a 0a  ing and end */..
30f9d 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
30f9e 6e 67 20 76 61 72 69 61 62 6c 65 73 20 6b 65 65  ng variables kee
30f9f 70 20 61 20 63 69 72 63 75 6c 61 72 20 62 75 66  p a circular buf
30fa0 66 65 72 20 6f 66 20 74 68 65 20 6c 61 73 74 0a  fer of the last.
30fa1 20 20 2a 2a 20 66 65 77 20 74 6f 6b 65 6e 73 20    ** few tokens 
30fa2 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
30fa3 74 20 69 52 6f 74 6f 72 20 3d 20 30 3b 20 20 20  t iRotor = 0;   
30fa4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
30fa5 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 74 6f  ex of current to
30fa6 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f  ken */.  int iRo
30fa7 74 6f 72 42 65 67 69 6e 5b 46 54 53 33 5f 52 4f  torBegin[FTS3_RO
30fa8 54 4f 52 5f 53 5a 5d 3b 20 20 20 20 20 20 2f 2a  TOR_SZ];      /*
30fa9 20 42 65 67 69 6e 6e 69 6e 67 20 6f 66 66 73 65   Beginning offse
30faa 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  t of token */.  
30fab 69 6e 74 20 69 52 6f 74 6f 72 4c 65 6e 5b 46 54  int iRotorLen[FT
30fac 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b 20 20 20  S3_ROTOR_SZ];   
30fad 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
30fae 66 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 70 56  f token */..  pV
30faf 74 61 62 20 3d 20 63 75 72 73 6f 72 5f 76 74 61  tab = cursor_vta
30fb0 62 28 70 43 75 72 29 3b 0a 20 20 6e 43 6f 6c 75  b(pCur);.  nColu
30fb1 6d 6e 20 3d 20 70 56 74 61 62 2d 3e 6e 43 6f 6c  mn = pVtab->nCol
30fb2 75 6d 6e 3b 0a 20 20 70 54 6f 6b 65 6e 69 7a 65  umn;.  pTokenize
30fb3 72 20 3d 20 70 56 74 61 62 2d 3e 70 54 6f 6b 65  r = pVtab->pToke
30fb4 6e 69 7a 65 72 3b 0a 20 20 70 54 4d 6f 64 75 6c  nizer;.  pTModul
30fb5 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e  e = pTokenizer->
30fb6 70 4d 6f 64 75 6c 65 3b 0a 20 20 72 63 20 3d 20  pModule;.  rc = 
30fb7 70 54 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28  pTModule->xOpen(
30fb8 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 44 6f 63  pTokenizer, zDoc
30fb9 2c 20 6e 44 6f 63 2c 20 26 70 54 43 75 72 73 6f  , nDoc, &pTCurso
30fba 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  r);.  if( rc ) r
30fbb 65 74 75 72 6e 3b 0a 20 20 70 54 43 75 72 73 6f  eturn;.  pTCurso
30fbc 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  r->pTokenizer = 
30fbd 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20 70  pTokenizer;..  p
30fbe 72 65 76 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20  revMatch = 0;.  
30fbf 77 68 69 6c 65 28 20 21 70 54 4d 6f 64 75 6c 65  while( !pTModule
30fc0 2d 3e 78 4e 65 78 74 28 70 54 43 75 72 73 6f 72  ->xNext(pTCursor
30fc1 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b  , &zToken, &nTok
30fc2 65 6e 2c 20 26 69 42 65 67 69 6e 2c 20 26 69 45  en, &iBegin, &iE
30fc3 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20 20  nd, &iPos) ){.  
30fc4 20 20 46 74 73 33 45 78 70 72 20 2a 70 49 74 65    Fts3Expr *pIte
30fc5 72 20 3d 20 70 43 75 72 2d 3e 70 45 78 70 72 3b  r = pCur->pExpr;
30fc6 0a 20 20 20 20 69 6e 74 20 69 49 74 65 72 20 3d  .    int iIter =
30fc7 20 2d 31 3b 0a 20 20 20 20 69 52 6f 74 6f 72 42   -1;.    iRotorB
30fc8 65 67 69 6e 5b 69 52 6f 74 6f 72 26 46 54 53 33  egin[iRotor&FTS3
30fc9 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20 69  _ROTOR_MASK] = i
30fca 42 65 67 69 6e 3b 0a 20 20 20 20 69 52 6f 74 6f  Begin;.    iRoto
30fcb 72 4c 65 6e 5b 69 52 6f 74 6f 72 26 46 54 53 33  rLen[iRotor&FTS3
30fcc 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20 69  _ROTOR_MASK] = i
30fcd 45 6e 64 2d 69 42 65 67 69 6e 3b 0a 20 20 20 20  End-iBegin;.    
30fce 6d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 66  match = 0;.    f
30fcf 6f 72 28 69 3d 30 3b 20 69 3c 28 46 54 53 33 5f  or(i=0; i<(FTS3_
30fd0 52 4f 54 4f 52 5f 53 5a 2d 31 29 20 26 26 20 66  ROTOR_SZ-1) && f
30fd1 74 73 33 4e 65 78 74 45 78 70 72 54 6f 6b 65 6e  ts3NextExprToken
30fd2 28 26 70 49 74 65 72 2c 20 26 69 49 74 65 72 29  (&pIter, &iIter)
30fd3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e  ; i++){.      in
30fd4 74 20 6e 50 68 72 61 73 65 3b 20 20 20 20 20 20  t nPhrase;      
30fd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30fd6 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e   Number of token
30fd7 73 20 69 6e 20 63 75 72 72 65 6e 74 20 70 68 72  s in current phr
30fd8 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 73 74 72  ase */.      str
30fd9 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e 20  uct PhraseToken 
30fda 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20 2f 2a 20  *pToken;     /* 
30fdb 43 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  Current token */
30fdc 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b  .      int iCol;
30fdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30fde 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
30fdf 20 69 6e 64 65 78 20 2a 2f 0a 0a 20 20 20 20 20   index */..     
30fe0 20 69 66 28 20 66 74 73 33 45 78 70 72 42 65 6e   if( fts3ExprBen
30fe1 65 61 74 68 4e 6f 74 28 70 49 74 65 72 29 20 29  eathNot(pIter) )
30fe2 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
30fe3 20 6e 50 68 72 61 73 65 20 3d 20 70 49 74 65 72   nPhrase = pIter
30fe4 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  ->pPhrase->nToke
30fe5 6e 3b 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e 20  n;.      pToken 
30fe6 3d 20 26 70 49 74 65 72 2d 3e 70 50 68 72 61 73  = &pIter->pPhras
30fe7 65 2d 3e 61 54 6f 6b 65 6e 5b 69 49 74 65 72 5d  e->aToken[iIter]
30fe8 3b 0a 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 70  ;.      iCol = p
30fe9 49 74 65 72 2d 3e 70 50 68 72 61 73 65 2d 3e 69  Iter->pPhrase->i
30fea 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66  Column;.      if
30feb 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f  ( iCol>=0 && iCo
30fec 6c 3c 6e 43 6f 6c 75 6d 6e 20 26 26 20 69 43 6f  l<nColumn && iCo
30fed 6c 21 3d 69 43 6f 6c 75 6d 6e 20 29 20 63 6f 6e  l!=iColumn ) con
30fee 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28  tinue;.      if(
30fef 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 6e 54 6f 6b 65   pToken->n>nToke
30ff0 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  n ) continue;.  
30ff1 20 20 20 20 69 66 28 20 21 70 54 6f 6b 65 6e 2d      if( !pToken-
30ff2 3e 69 73 50 72 65 66 69 78 20 26 26 20 70 54 6f  >isPrefix && pTo
30ff3 6b 65 6e 2d 3e 6e 3c 6e 54 6f 6b 65 6e 20 29 20  ken->n<nToken ) 
30ff4 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
30ff5 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e  assert( pToken->
30ff6 6e 3c 3d 6e 54 6f 6b 65 6e 20 29 3b 0a 20 20 20  n<=nToken );.   
30ff7 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 54     if( memcmp(pT
30ff8 6f 6b 65 6e 2d 3e 7a 2c 20 7a 54 6f 6b 65 6e 2c  oken->z, zToken,
30ff9 20 70 54 6f 6b 65 6e 2d 3e 6e 29 20 29 20 63 6f   pToken->n) ) co
30ffa 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66  ntinue;.      if
30ffb 28 20 69 49 74 65 72 3e 30 20 26 26 20 28 70 72  ( iIter>0 && (pr
30ffc 65 76 4d 61 74 63 68 20 26 20 28 31 3c 3c 69 29  evMatch & (1<<i)
30ffd 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )==0 ) continue;
30ffe 0a 20 20 20 20 20 20 6d 61 74 63 68 20 7c 3d 20  .      match |= 
30fff 31 3c 3c 69 3b 0a 20 20 20 20 20 20 69 66 28 20  1<<i;.      if( 
31000 69 3d 3d 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53  i==(FTS3_ROTOR_S
31001 5a 2d 32 29 20 7c 7c 20 6e 50 68 72 61 73 65 3d  Z-2) || nPhrase=
31002 3d 69 49 74 65 72 2b 31 20 29 7b 0a 20 20 20 20  =iIter+1 ){.    
31003 20 20 20 20 66 6f 72 28 6a 3d 6e 50 68 72 61 73      for(j=nPhras
31004 65 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 29 7b  e-1; j>=0; j--){
31005 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b  .          int k
31006 20 3d 20 28 69 52 6f 74 6f 72 2d 6a 29 20 26 20   = (iRotor-j) & 
31007 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 3b  FTS3_ROTOR_MASK;
31008 0a 20 20 20 20 20 20 20 20 20 20 73 6e 69 70 70  .          snipp
31009 65 74 41 70 70 65 6e 64 4d 61 74 63 68 28 70 53  etAppendMatch(pS
3100a 6e 69 70 70 65 74 2c 20 69 43 6f 6c 75 6d 6e 2c  nippet, iColumn,
3100b 20 69 2d 6a 2c 20 69 50 6f 73 2d 6a 2c 0a 20 20   i-j, iPos-j,.  
3100c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 52                iR
3100d 6f 74 6f 72 42 65 67 69 6e 5b 6b 5d 2c 20 69 52  otorBegin[k], iR
3100e 6f 74 6f 72 4c 65 6e 5b 6b 5d 29 3b 0a 20 20 20  otorLen[k]);.   
3100f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
31010 20 20 20 7d 0a 20 20 20 20 70 72 65 76 4d 61 74     }.    prevMat
31011 63 68 20 3d 20 6d 61 74 63 68 3c 3c 31 3b 0a 20  ch = match<<1;. 
31012 20 20 20 69 52 6f 74 6f 72 2b 2b 3b 0a 20 20 7d     iRotor++;.  }
31013 0a 20 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 43 6c  .  pTModule->xCl
31014 6f 73 65 28 70 54 43 75 72 73 6f 72 29 3b 20 20  ose(pTCursor);  
31015 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
31016 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68   entries from th
31017 65 20 70 53 6e 69 70 70 65 74 20 73 74 72 75 63  e pSnippet struc
31018 74 75 72 65 20 74 6f 20 61 63 63 6f 75 6e 74 20  ture to account 
31019 66 6f 72 20 74 68 65 20 4e 45 41 52 0a 2a 2a 20  for the NEAR.** 
3101a 6f 70 65 72 61 74 6f 72 2e 20 57 68 65 6e 20 74  operator. When t
3101b 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 70  his is called, p
3101c 53 6e 69 70 70 65 74 20 63 6f 6e 74 61 69 6e 73  Snippet contains
3101d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 74 6f 6b   the list of tok
3101e 65 6e 20 0a 2a 2a 20 6f 66 66 73 65 74 73 20 70  en .** offsets p
3101f 72 6f 64 75 63 65 64 20 62 79 20 74 72 65 61 74  roduced by treat
31020 69 6e 67 20 61 6c 6c 20 4e 45 41 52 20 6f 70 65  ing all NEAR ope
31021 72 61 74 6f 72 73 20 61 73 20 41 4e 44 20 6f 70  rators as AND op
31022 65 72 61 74 6f 72 73 2e 0a 2a 2a 20 54 68 69 73  erators..** This
31023 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65   function remove
31024 73 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74 68  s any entries th
31025 61 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  at should not be
31026 20 70 72 65 73 65 6e 74 20 61 66 74 65 72 0a 2a   present after.*
31027 2a 20 61 63 63 6f 75 6e 74 69 6e 67 20 66 6f 72  * accounting for
31028 20 74 68 65 20 4e 45 41 52 20 72 65 73 74 72 69   the NEAR restri
31029 63 74 69 6f 6e 2e 20 46 6f 72 20 65 78 61 6d 70  ction. For examp
3102a 6c 65 2c 20 69 66 20 74 68 65 20 71 75 65 72 69  le, if the queri
3102b 65 64 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 69  ed.** document i
3102c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 41 20  s:.**.**     "A 
3102d 42 20 43 20 44 20 45 20 41 22 0a 2a 2a 0a 2a 2a  B C D E A".**.**
3102e 20 61 6e 64 20 74 68 65 20 71 75 65 72 79 20 69   and the query i
3102f 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 41 20  s:.** .**     A 
31030 4e 45 41 52 2f 30 20 45 0a 2a 2a 0a 2a 2a 20 74  NEAR/0 E.**.** t
31031 68 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75  hen when this fu
31032 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
31033 20 74 68 65 20 53 6e 69 70 70 65 74 20 63 6f 6e   the Snippet con
31034 74 61 69 6e 73 20 74 6f 6b 65 6e 20 6f 66 66 73  tains token offs
31035 65 74 73 0a 2a 2a 20 30 2c 20 34 20 61 6e 64 20  ets.** 0, 4 and 
31036 35 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  5. This function
31037 20 72 65 6d 6f 76 65 73 20 74 68 65 20 22 30 22   removes the "0"
31038 20 65 6e 74 72 79 20 28 62 65 63 61 75 73 65 20   entry (because 
31039 74 68 65 20 66 69 72 73 74 20 41 0a 2a 2a 20 69  the first A.** i
3103a 73 20 6e 6f 74 20 6e 65 61 72 20 65 6e 6f 75 67  s not near enoug
3103b 68 20 74 6f 20 61 6e 20 45 29 2e 0a 2a 2a 0a 2a  h to an E)..**.*
3103c 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63  * When this func
3103d 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20  tion is called, 
3103e 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65  the value pointe
3103f 64 20 74 6f 20 62 79 20 70 61 72 61 6d 65 74 65  d to by paramete
31040 72 20 70 69 4c 65 66 74 20 69 73 0a 2a 2a 20 74  r piLeft is.** t
31041 68 65 20 69 6e 74 65 67 65 72 20 69 64 20 6f 66  he integer id of
31042 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 74   the left-most t
31043 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70 72  oken in the expr
31044 65 73 73 69 6f 6e 20 74 72 65 65 20 68 65 61 64  ession tree head
31045 65 64 20 62 79 0a 2a 2a 20 70 45 78 70 72 2e 20  ed by.** pExpr. 
31046 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e  This function in
31047 63 72 65 6d 65 6e 74 73 20 2a 70 69 4c 65 66 74  crements *piLeft
31048 20 62 79 20 74 68 65 20 74 6f 74 61 6c 20 6e 75   by the total nu
31049 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 0a 2a  mber of tokens.*
3104a 2a 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73  * in the express
3104b 69 6f 6e 20 74 72 65 65 20 68 65 61 64 65 64 20  ion tree headed 
3104c 62 79 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20  by pExpr..**.** 
3104d 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79 20  Return 1 if any 
3104e 74 72 69 6d 6d 69 6e 67 20 6f 63 63 75 72 73 2e  trimming occurs.
3104f 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e 6f    Return 0 if no
31050 20 74 72 69 6d 6d 69 6e 67 20 69 73 20 72 65 71   trimming is req
31051 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  uired..*/.static
31052 20 69 6e 74 20 74 72 69 6d 53 6e 69 70 70 65 74   int trimSnippet
31053 4f 66 66 73 65 74 73 28 0a 20 20 46 74 73 33 45  Offsets(.  Fts3E
31054 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20  xpr *pExpr,     
31055 20 2f 2a 20 54 68 65 20 73 65 61 72 63 68 20 65   /* The search e
31056 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53  xpression */.  S
31057 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65 74  nippet *pSnippet
31058 2c 20 20 20 20 2f 2a 20 54 68 65 20 73 65 74 20  ,    /* The set 
31059 6f 66 20 73 6e 69 70 70 65 74 20 6f 66 66 73 65  of snippet offse
3105a 74 73 20 74 6f 20 62 65 20 74 72 69 6d 6d 65 64  ts to be trimmed
3105b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 4c 65 66   */.  int *piLef
3105c 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  t           /* I
3105d 6e 64 65 78 20 6f 66 20 6c 65 66 74 2d 6d 6f 73  ndex of left-mos
3105e 74 20 74 6f 6b 65 6e 20 69 6e 20 70 45 78 70 72  t token in pExpr
3105f 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45 78   */.){.  if( pEx
31060 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 74 72  pr ){.    if( tr
31061 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73  imSnippetOffsets
31062 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70  (pExpr->pLeft, p
31063 53 6e 69 70 70 65 74 2c 20 70 69 4c 65 66 74 29  Snippet, piLeft)
31064 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
31065 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73   1;.    }..    s
31066 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65 54  witch( pExpr->eT
31067 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73  ype ){.      cas
31068 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  e FTSQUERY_PHRAS
31069 45 3a 0a 20 20 20 20 20 20 20 20 2a 70 69 4c 65  E:.        *piLe
3106a 66 74 20 2b 3d 20 70 45 78 70 72 2d 3e 70 50 68  ft += pExpr->pPh
3106b 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20  rase->nToken;.  
3106c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3106d 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
3106e 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20 20  _NEAR: {.       
3106f 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61   /* The right-ha
31070 6e 64 2d 73 69 64 65 20 6f 66 20 61 20 4e 45 41  nd-side of a NEA
31071 52 20 6f 70 65 72 61 74 6f 72 20 69 73 20 61 6c  R operator is al
31072 77 61 79 73 20 61 20 70 68 72 61 73 65 2e 20 54  ways a phrase. T
31073 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 65  he.        ** le
31074 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 69 73 20  ft-hand-side is 
31075 65 69 74 68 65 72 20 61 20 70 68 72 61 73 65 20  either a phrase 
31076 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  or an expression
31077 20 74 72 65 65 20 74 68 61 74 20 69 73 20 0a 20   tree that is . 
31078 20 20 20 20 20 20 20 2a 2a 20 69 74 73 65 6c 66         ** itself
31079 20 68 65 61 64 65 64 20 62 79 20 61 20 4e 45 41   headed by a NEA
3107a 52 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 65 20  R operator. The 
3107b 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 69 74 69 61  following initia
3107c 6c 69 7a 61 74 69 6f 6e 73 0a 20 20 20 20 20 20  lizations.      
3107d 20 20 2a 2a 20 73 65 74 20 6c 6f 63 61 6c 20 76    ** set local v
3107e 61 72 69 61 62 6c 65 20 69 4c 65 66 74 20 74 6f  ariable iLeft to
3107f 20 74 68 65 20 74 6f 6b 65 6e 20 6e 75 6d 62 65   the token numbe
31080 72 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f  r of the left-mo
31081 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f  st.        ** to
31082 6b 65 6e 20 69 6e 20 74 68 65 20 72 69 67 68 74  ken in the right
31083 2d 68 61 6e 64 20 70 68 72 61 73 65 2c 20 61 6e  -hand phrase, an
31084 64 20 69 52 69 67 68 74 20 74 6f 20 74 68 65 20  d iRight to the 
31085 72 69 67 68 74 20 6d 6f 73 74 0a 20 20 20 20 20  right most.     
31086 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74     ** token in t
31087 68 65 20 73 61 6d 65 20 70 68 72 61 73 65 2e 20  he same phrase. 
31088 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
31089 77 65 20 68 61 64 3a 0a 20 20 20 20 20 20 20 20  we had:.        
3108a 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  **.        **   
3108b 20 20 3c 63 6f 6c 3e 20 4d 41 54 43 48 20 27 22    <col> MATCH '"
3108c 61 62 63 20 64 65 66 22 20 4e 45 41 52 2f 32 20  abc def" NEAR/2 
3108d 22 67 68 69 20 6a 6b 6c 22 27 0a 20 20 20 20 20  "ghi jkl"'.     
3108e 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
3108f 20 74 68 65 6e 20 69 4c 65 66 74 20 77 69 6c 6c   then iLeft will
31090 20 62 65 20 73 65 74 20 74 6f 20 32 20 28 74 6f   be set to 2 (to
31091 6b 65 6e 20 6e 75 6d 62 65 72 20 6f 66 20 67 68  ken number of gh
31092 69 29 20 61 6e 64 20 6e 54 6f 6b 65 6e 20 77 69  i) and nToken wi
31093 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65  ll.        ** be
31094 20 73 65 74 20 74 6f 20 34 2e 0a 20 20 20 20 20   set to 4..     
31095 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 74     */.        Ft
31096 73 33 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20  s3Expr *pLeft = 
31097 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
31098 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
31099 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
3109a 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
3109b 69 6e 74 20 69 4c 65 66 74 20 3d 20 2a 70 69 4c  int iLeft = *piL
3109c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  eft;.        int
3109d 20 6e 4e 65 61 72 20 3d 20 70 45 78 70 72 2d 3e   nNear = pExpr->
3109e 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20 20 20 69  nNear;.        i
3109f 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 70 52 69 67  nt nToken = pRig
310a0 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  ht->pPhrase->nTo
310a1 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  ken;.        int
310a2 20 6a 6a 2c 20 69 69 3b 0a 20 20 20 20 20 20 20   jj, ii;.       
310a3 20 69 66 28 20 70 4c 65 66 74 2d 3e 65 54 79 70   if( pLeft->eTyp
310a4 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
310a5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4c   ){.          pL
310a6 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 52 69  eft = pLeft->pRi
310a7 67 68 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ght;.        }. 
310a8 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
310a9 52 69 67 68 74 2d 3e 65 54 79 70 65 3d 3d 46 54  Right->eType==FT
310aa 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b  SQUERY_PHRASE );
310ab 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
310ac 20 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46   pLeft->eType==F
310ad 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
310ae 3b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e  ;.        nToken
310af 20 2b 3d 20 70 4c 65 66 74 2d 3e 70 50 68 72 61   += pLeft->pPhra
310b0 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 0a 20 20 20  se->nToken;..   
310b1 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69       for(ii=0; i
310b2 69 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74  i<pSnippet->nMat
310b3 63 68 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  ch; ii++){.     
310b4 20 20 20 20 20 73 74 72 75 63 74 20 73 6e 69 70       struct snip
310b5 70 65 74 4d 61 74 63 68 20 2a 70 20 3d 20 26 70  petMatch *p = &p
310b6 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b  Snippet->aMatch[
310b7 69 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ii];.          i
310b8 66 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 69 4c 65  f( p->iTerm==iLe
310b9 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
310ba 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30 3b 0a    int isOk = 0;.
310bb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
310bc 6e 69 70 70 65 74 20 69 69 20 69 73 20 61 6e 20  nippet ii is an 
310bd 6f 63 63 75 72 65 6e 63 65 20 6f 66 20 71 75 65  occurence of que
310be 72 79 20 74 65 72 6d 20 69 4c 65 66 74 20 69 6e  ry term iLeft in
310bf 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e 0a 20   the document.. 
310c0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 74             ** It
310c1 20 6f 63 63 75 72 73 20 61 74 20 70 6f 73 69 74   occurs at posit
310c2 69 6f 6e 20 28 70 2d 3e 69 54 6f 6b 65 6e 29 20  ion (p->iToken) 
310c3 6f 66 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e  of the document.
310c4 20 57 65 20 6e 6f 77 0a 20 20 20 20 20 20 20 20   We now.        
310c5 20 20 20 20 2a 2a 20 73 65 61 72 63 68 20 66 6f      ** search fo
310c6 72 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  r an instance of
310c7 20 74 6f 6b 65 6e 20 28 69 4c 65 66 74 2d 31 29   token (iLeft-1)
310c8 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74 68   somewhere in th
310c9 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  e .            *
310ca 2a 20 72 61 6e 67 65 20 28 70 2d 3e 69 54 6f 6b  * range (p->iTok
310cb 65 6e 20 2d 20 6e 4e 65 61 72 29 2e 2e 2e 28 70  en - nNear)...(p
310cc 2d 3e 69 54 6f 6b 65 6e 20 2b 20 6e 4e 65 61 72  ->iToken + nNear
310cd 20 2b 20 6e 54 6f 6b 65 6e 29 20 77 69 74 68 69   + nToken) withi
310ce 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  n .            *
310cf 2a 20 74 68 65 20 73 65 74 20 6f 66 20 73 6e 69  * the set of sni
310d0 70 70 65 74 4d 61 74 63 68 20 73 74 72 75 63 74  ppetMatch struct
310d1 75 72 65 73 2e 20 49 66 20 6f 6e 65 20 69 73 20  ures. If one is 
310d2 66 6f 75 6e 64 2c 20 70 72 6f 63 65 65 64 2e 20  found, proceed. 
310d3 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
310d4 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65  If one cannot be
310d5 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 72 65 6d   found, then rem
310d6 6f 76 65 20 73 6e 69 70 70 65 74 73 20 69 69 2e  ove snippets ii.
310d7 2e 28 69 69 2b 4e 2d 31 29 20 0a 20 20 20 20 20  .(ii+N-1) .     
310d8 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74         ** from t
310d9 68 65 20 6d 61 74 63 68 69 6e 67 20 73 6e 69 70  he matching snip
310da 70 65 74 73 2c 20 77 68 65 72 65 20 4e 20 69 73  pets, where N is
310db 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
310dc 6f 6b 65 6e 73 20 0a 20 20 20 20 20 20 20 20 20  okens .         
310dd 20 20 20 2a 2a 20 69 6e 20 70 68 72 61 73 65 20     ** in phrase 
310de 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2e  pRight->pPhrase.
310df 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  .            */.
310e0 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28              for(
310e1 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20 26 26  jj=0; isOk==0 &&
310e2 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d   jj<pSnippet->nM
310e3 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20  atch; jj++){.   
310e4 20 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63             struc
310e5 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a  t snippetMatch *
310e6 70 32 20 3d 20 26 70 53 6e 69 70 70 65 74 2d 3e  p2 = &pSnippet->
310e7 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20 20 20 20  aMatch[jj];.    
310e8 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 32            if( p2
310e9 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65 66 74 2d  ->iTerm==(iLeft-
310ea 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  1) ){.          
310eb 20 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54        if( p2->iT
310ec 6f 6b 65 6e 3e 3d 28 70 2d 3e 69 54 6f 6b 65 6e  oken>=(p->iToken
310ed 2d 6e 4e 65 61 72 2d 31 29 20 0a 20 20 20 20 20  -nNear-1) .     
310ee 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70              && p
310ef 32 2d 3e 69 54 6f 6b 65 6e 3c 28 70 2d 3e 69 54  2->iToken<(p->iT
310f0 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 6e 54 6f 6b 65  oken+nNear+nToke
310f1 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  n) .            
310f2 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
310f3 20 20 20 20 20 20 20 20 20 69 73 4f 6b 20 3d 20           isOk = 
310f4 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
310f5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
310f6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
310f7 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69   }.            i
310f8 66 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20 20  f( !isOk ){.    
310f9 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 6b            int kk
310fa 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
310fb 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 52 69  for(kk=0; kk<pRi
310fc 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  ght->pPhrase->nT
310fd 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20  oken; kk++){.   
310fe 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 6e               pSn
310ff 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6b 6b  ippet->aMatch[kk
31100 2b 69 69 5d 2e 69 54 65 72 6d 20 3d 20 2d 32 3b  +ii].iTerm = -2;
31101 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
31102 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
31103 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20  eturn 1;.       
31104 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
31105 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
31106 20 70 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65 66   p->iTerm==(iLef
31107 74 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  t-1) ){.        
31108 20 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30      int isOk = 0
31109 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f  ;.            fo
3110a 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20  r(jj=0; isOk==0 
3110b 26 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e  && jj<pSnippet->
3110c 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20  nMatch; jj++){. 
3110d 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72               str
3110e 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68  uct snippetMatch
3110f 20 2a 70 32 20 3d 20 26 70 53 6e 69 70 70 65 74   *p2 = &pSnippet
31110 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20 20  ->aMatch[jj];.  
31111 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
31112 70 32 2d 3e 69 54 65 72 6d 3d 3d 69 4c 65 66 74  p2->iTerm==iLeft
31113 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
31114 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54 6f 6b      if( p2->iTok
31115 65 6e 3c 3d 28 70 2d 3e 69 54 6f 6b 65 6e 2b 6e  en<=(p->iToken+n
31116 4e 65 61 72 2b 31 29 20 0a 20 20 20 20 20 20 20  Near+1) .       
31117 20 20 20 20 20 20 20 20 20 20 26 26 20 70 32 2d            && p2-
31118 3e 69 54 6f 6b 65 6e 3e 28 70 2d 3e 69 54 6f 6b  >iToken>(p->iTok
31119 65 6e 2d 6e 4e 65 61 72 2d 6e 54 6f 6b 65 6e 29  en-nNear-nToken)
3111a 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
3111b 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20    ){.           
3111c 20 20 20 20 20 20 20 69 73 4f 6b 20 3d 20 31 3b         isOk = 1;
3111d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3111e 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
3111f 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d   }.            }
31120 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
31121 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20 20 20 20   !isOk ){.      
31122 20 20 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b 0a          int kk;.
31123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f                fo
31124 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 4c 65 66 74  r(kk=0; kk<pLeft
31125 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  ->pPhrase->nToke
31126 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20 20  n; kk++){.      
31127 20 20 20 20 20 20 20 20 20 20 70 53 6e 69 70 70            pSnipp
31128 65 74 2d 3e 61 4d 61 74 63 68 5b 69 69 2d 6b 6b  et->aMatch[ii-kk
31129 5d 2e 69 54 65 72 6d 20 3d 20 2d 32 3b 0a 20 20  ].iTerm = -2;.  
3112a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
3112b 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
3112c 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20  rn 1;.          
3112d 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
3112e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3112f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
31130 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
31131 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65  trimSnippetOffse
31132 74 73 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74  ts(pExpr->pRight
31133 2c 20 70 53 6e 69 70 70 65 74 2c 20 70 69 4c 65  , pSnippet, piLe
31134 66 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ft) ){.      ret
31135 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
31136 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
31137 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 6c  /*.** Compute al
31138 6c 20 6f 66 66 73 65 74 73 20 66 6f 72 20 74 68  l offsets for th
31139 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
3113a 20 74 68 65 20 71 75 65 72 79 2e 20 20 0a 2a 2a   the query.  .**
3113b 20 49 66 20 74 68 65 20 6f 66 66 73 65 74 73 20   If the offsets 
3113c 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
3113d 6e 20 63 6f 6d 70 75 74 65 64 2c 20 74 68 69 73  n computed, this
3113e 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
3113f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  -op..*/.static v
31140 6f 69 64 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66  oid snippetAllOf
31141 66 73 65 74 73 28 66 75 6c 6c 74 65 78 74 5f 63  fsets(fulltext_c
31142 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 6e 74  ursor *p){.  int
31143 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20   nColumn;.  int 
31144 69 43 6f 6c 75 6d 6e 2c 20 69 3b 0a 20 20 69 6e  iColumn, i;.  in
31145 74 20 69 46 69 72 73 74 2c 20 69 4c 61 73 74 3b  t iFirst, iLast;
31146 0a 20 20 69 6e 74 20 69 54 65 72 6d 20 3d 20 30  .  int iTerm = 0
31147 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  ;.  fulltext_vta
31148 62 20 2a 70 46 74 73 20 3d 20 63 75 72 73 6f 72  b *pFts = cursor
31149 5f 76 74 61 62 28 70 29 3b 0a 0a 20 20 69 66 28  _vtab(p);..  if(
3114a 20 70 2d 3e 73 6e 69 70 70 65 74 2e 6e 4d 61 74   p->snippet.nMat
3114b 63 68 20 7c 7c 20 70 2d 3e 70 45 78 70 72 3d 3d  ch || p->pExpr==
3114c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  0 ){.    return;
3114d 0a 20 20 7d 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d  .  }.  nColumn =
3114e 20 70 46 74 73 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a   pFts->nColumn;.
3114f 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 28 70 2d 3e    iColumn = (p->
31150 69 43 75 72 73 6f 72 54 79 70 65 20 2d 20 51 55  iCursorType - QU
31151 45 52 59 5f 46 55 4c 4c 54 45 58 54 29 3b 0a 20  ERY_FULLTEXT);. 
31152 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 7c   if( iColumn<0 |
31153 7c 20 69 43 6f 6c 75 6d 6e 3e 3d 6e 43 6f 6c 75  | iColumn>=nColu
31154 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f  mn ){.    /* Loo
31155 6b 20 66 6f 72 20 6d 61 74 63 68 65 73 20 6f 76  k for matches ov
31156 65 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6f  er all columns o
31157 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20  f the full-text 
31158 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 46 69  index */.    iFi
31159 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 69 4c 61  rst = 0;.    iLa
3115a 73 74 20 3d 20 6e 43 6f 6c 75 6d 6e 2d 31 3b 0a  st = nColumn-1;.
3115b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
3115c 4c 6f 6f 6b 20 66 6f 72 20 6d 61 74 63 68 65 73  Look for matches
3115d 20 69 6e 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d   in the iColumn-
3115e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
3115f 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2a 2f 0a 20   index only */. 
31160 20 20 20 69 46 69 72 73 74 20 3d 20 69 43 6f 6c     iFirst = iCol
31161 75 6d 6e 3b 0a 20 20 20 20 69 4c 61 73 74 20 3d  umn;.    iLast =
31162 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20   iColumn;.  }.  
31163 66 6f 72 28 69 3d 69 46 69 72 73 74 3b 20 69 3c  for(i=iFirst; i<
31164 3d 69 4c 61 73 74 3b 20 69 2b 2b 29 7b 0a 20 20  =iLast; i++){.  
31165 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
31166 6f 63 3b 0a 20 20 20 20 69 6e 74 20 6e 44 6f 63  oc;.    int nDoc
31167 3b 0a 20 20 20 20 7a 44 6f 63 20 3d 20 28 63 6f  ;.    zDoc = (co
31168 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65  nst char*)sqlite
31169 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 2d  3_column_text(p-
3116a 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20 20  >pStmt, i+1);.  
3116b 20 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33    nDoc = sqlite3
3116c 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 2d  _column_bytes(p-
3116d 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20 20  >pStmt, i+1);.  
3116e 20 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73    snippetOffsets
3116f 4f 66 43 6f 6c 75 6d 6e 28 70 2c 20 26 70 2d 3e  OfColumn(p, &p->
31170 73 6e 69 70 70 65 74 2c 20 69 2c 20 7a 44 6f 63  snippet, i, zDoc
31171 2c 20 6e 44 6f 63 29 3b 0a 20 20 7d 0a 0a 20 20  , nDoc);.  }..  
31172 77 68 69 6c 65 28 20 74 72 69 6d 53 6e 69 70 70  while( trimSnipp
31173 65 74 4f 66 66 73 65 74 73 28 70 2d 3e 70 45 78  etOffsets(p->pEx
31174 70 72 2c 20 26 70 2d 3e 73 6e 69 70 70 65 74 2c  pr, &p->snippet,
31175 20 26 69 54 65 72 6d 29 20 29 7b 0a 20 20 20 20   &iTerm) ){.    
31176 69 54 65 72 6d 20 3d 20 30 3b 0a 20 20 7d 0a 7d  iTerm = 0;.  }.}
31177 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
31178 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
31179 69 6e 20 74 68 65 20 61 4d 61 74 63 68 5b 5d 20  in the aMatch[] 
3117a 61 72 72 61 79 20 6f 66 20 74 68 65 20 73 6e 69  array of the sni
3117b 70 70 65 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  ppet.** into the
3117c 20 73 74 72 69 6e 67 20 7a 4f 66 66 73 65 74 5b   string zOffset[
3117d 30 2e 2e 6e 4f 66 66 73 65 74 2d 31 5d 2e 20 54  0..nOffset-1]. T
3117e 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 75 73  his string is us
3117f 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 74  ed as.** the ret
31180 75 72 6e 20 6f 66 20 74 68 65 20 53 51 4c 20 6f  urn of the SQL o
31181 66 66 73 65 74 73 28 29 20 66 75 6e 63 74 69 6f  ffsets() functio
31182 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
31183 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 54  d snippetOffsetT
31184 65 78 74 28 53 6e 69 70 70 65 74 20 2a 70 29 7b  ext(Snippet *p){
31185 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
31186 63 6e 74 20 3d 20 30 3b 0a 20 20 53 74 72 69 6e  cnt = 0;.  Strin
31187 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 63 68  gBuffer sb;.  ch
31188 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a 20 20  ar zBuf[200];.  
31189 69 66 28 20 70 2d 3e 7a 4f 66 66 73 65 74 20 29  if( p->zOffset )
3118a 20 72 65 74 75 72 6e 3b 0a 20 20 69 6e 69 74 53   return;.  initS
3118b 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 62 29  tringBuffer(&sb)
3118c 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
3118d 2d 3e 6e 4d 61 74 63 68 3b 20 69 2b 2b 29 7b 0a  ->nMatch; i++){.
3118e 20 20 20 20 73 74 72 75 63 74 20 73 6e 69 70 70      struct snipp
3118f 65 74 4d 61 74 63 68 20 2a 70 4d 61 74 63 68 20  etMatch *pMatch 
31190 3d 20 26 70 2d 3e 61 4d 61 74 63 68 5b 69 5d 3b  = &p->aMatch[i];
31191 0a 20 20 20 20 69 66 28 20 70 4d 61 74 63 68 2d  .    if( pMatch-
31192 3e 69 54 65 72 6d 3e 3d 30 20 29 7b 0a 20 20 20  >iTerm>=0 ){.   
31193 20 20 20 2f 2a 20 49 66 20 73 6e 69 70 70 65 74     /* If snippet
31194 4d 61 74 63 68 2e 69 54 65 72 6d 20 69 73 20 6c  Match.iTerm is l
31195 65 73 73 20 74 68 61 6e 20 30 2c 20 74 68 65 6e  ess than 0, then
31196 20 74 68 65 20 6d 61 74 63 68 20 77 61 73 20 0a   the match was .
31197 20 20 20 20 20 20 2a 2a 20 64 69 73 63 61 72 64        ** discard
31198 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 70 72  ed as part of pr
31199 6f 63 65 73 73 69 6e 67 20 74 68 65 20 4e 45 41  ocessing the NEA
3119a 52 20 6f 70 65 72 61 74 6f 72 20 28 73 65 65 20  R operator (see 
3119b 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72  the .      ** tr
3119c 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73  imSnippetOffsets
3119d 46 6f 72 4e 65 61 72 28 29 20 66 75 6e 63 74 69  ForNear() functi
3119e 6f 6e 20 66 6f 72 20 64 65 74 61 69 6c 73 29 2e  on for details).
3119f 20 49 67 6e 6f 72 65 20 0a 20 20 20 20 20 20 2a   Ignore .      *
311a0 2a 20 69 74 20 69 6e 20 74 68 69 73 20 63 61 73  * it in this cas
311a1 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  e.      */.     
311a2 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a   zBuf[0] = ' ';.
311a3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
311a4 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42  printf(sizeof(zB
311a5 75 66 29 2d 31 2c 20 26 7a 42 75 66 5b 63 6e 74  uf)-1, &zBuf[cnt
311a6 3e 30 5d 2c 20 22 25 64 20 25 64 20 25 64 20 25  >0], "%d %d %d %
311a7 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 4d  d",.          pM
311a8 61 74 63 68 2d 3e 69 43 6f 6c 2c 20 70 4d 61 74  atch->iCol, pMat
311a9 63 68 2d 3e 69 54 65 72 6d 2c 20 70 4d 61 74 63  ch->iTerm, pMatc
311aa 68 2d 3e 69 53 74 61 72 74 2c 20 70 4d 61 74 63  h->iStart, pMatc
311ab 68 2d 3e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  h->nByte);.     
311ac 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 42 75   append(&sb, zBu
311ad 66 29 3b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b  f);.      cnt++;
311ae 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e  .    }.  }.  p->
311af 7a 4f 66 66 73 65 74 20 3d 20 73 74 72 69 6e 67  zOffset = string
311b0 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b  BufferData(&sb);
311b1 0a 20 20 70 2d 3e 6e 4f 66 66 73 65 74 20 3d 20  .  p->nOffset = 
311b2 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67  stringBufferLeng
311b3 74 68 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  th(&sb);.}../*.*
311b4 2a 20 7a 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31  * zDoc[0..nDoc-1
311b5 5d 20 69 73 20 70 68 72 61 73 65 20 6f 66 20 74  ] is phrase of t
311b6 65 78 74 2e 20 20 61 4d 61 74 63 68 5b 30 2e 2e  ext.  aMatch[0..
311b7 6e 4d 61 74 63 68 2d 31 5d 20 61 72 65 20 61 20  nMatch-1] are a 
311b8 73 65 74 0a 2a 2a 20 6f 66 20 6d 61 74 63 68 69  set.** of matchi
311b9 6e 67 20 77 6f 72 64 73 20 73 6f 6d 65 20 6f 66  ng words some of
311ba 20 77 68 69 63 68 20 6d 69 67 68 74 20 62 65 20   which might be 
311bb 69 6e 20 7a 44 6f 63 2e 20 20 7a 44 6f 63 20 69  in zDoc.  zDoc i
311bc 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62  s column.** numb
311bd 65 72 20 69 43 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 69  er iCol..**.** i
311be 42 72 65 61 6b 20 69 73 20 73 75 67 67 65 73 74  Break is suggest
311bf 65 64 20 73 70 6f 74 20 69 6e 20 7a 44 6f 63 20  ed spot in zDoc 
311c0 77 68 65 72 65 20 77 65 20 63 6f 75 6c 64 20 62  where we could b
311c1 65 67 69 6e 20 6f 72 20 65 6e 64 20 61 6e 0a 2a  egin or end an.*
311c2 2a 20 65 78 63 65 72 70 74 2e 20 20 52 65 74 75  * excerpt.  Retu
311c3 72 6e 20 61 20 76 61 6c 75 65 20 73 69 6d 69 6c  rn a value simil
311c4 61 72 20 74 6f 20 69 42 72 65 61 6b 20 62 75 74  ar to iBreak but
311c5 20 70 6f 73 73 69 62 6c 79 20 61 64 6a 75 73 74   possibly adjust
311c6 65 64 0a 2a 2a 20 74 6f 20 62 65 20 61 20 6c 69  ed.** to be a li
311c7 74 74 6c 65 20 6c 65 66 74 20 6f 72 20 72 69 67  ttle left or rig
311c8 68 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 62  ht so that the b
311c9 72 65 61 6b 20 70 6f 69 6e 74 20 69 73 20 62 65  reak point is be
311ca 74 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tter..*/.static 
311cb 69 6e 74 20 77 6f 72 64 42 6f 75 6e 64 61 72 79  int wordBoundary
311cc 28 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 2c 20  (.  int iBreak, 
311cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
311ce 20 20 2f 2a 20 54 68 65 20 73 75 67 67 65 73 74    /* The suggest
311cf 65 64 20 62 72 65 61 6b 20 70 6f 69 6e 74 20 2a  ed break point *
311d0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
311d1 7a 44 6f 63 2c 20 20 20 20 20 20 20 20 20 20 20  zDoc,           
311d2 20 20 2f 2a 20 44 6f 63 75 6d 65 6e 74 20 74 65    /* Document te
311d3 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f 63  xt */.  int nDoc
311d4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
311d5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
311d6 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 44 6f 63  of bytes in zDoc
311d7 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73  [] */.  struct s
311d8 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 61 4d 61  nippetMatch *aMa
311d9 74 63 68 2c 20 20 2f 2a 20 4d 61 74 63 68 69 6e  tch,  /* Matchin
311da 67 20 77 6f 72 64 73 20 2a 2f 0a 20 20 69 6e 74  g words */.  int
311db 20 6e 4d 61 74 63 68 2c 20 20 20 20 20 20 20 20   nMatch,        
311dc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
311dd 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
311de 69 6e 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20  in aMatch[] */. 
311df 20 69 6e 74 20 69 43 6f 6c 20 20 20 20 20 20 20   int iCol       
311e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
311e1 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d  * The column num
311e2 62 65 72 20 66 6f 72 20 7a 44 6f 63 5b 5d 20 2a  ber for zDoc[] *
311e3 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
311e4 69 66 28 20 69 42 72 65 61 6b 3c 3d 31 30 20 29  if( iBreak<=10 )
311e5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
311e6 20 20 7d 0a 20 20 69 66 28 20 69 42 72 65 61 6b    }.  if( iBreak
311e7 3e 3d 6e 44 6f 63 2d 31 30 20 29 7b 0a 20 20 20  >=nDoc-10 ){.   
311e8 20 72 65 74 75 72 6e 20 6e 44 6f 63 3b 0a 20 20   return nDoc;.  
311e9 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  }.  for(i=0; i<n
311ea 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b  Match && aMatch[
311eb 69 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 3b 20 69 2b  i].iCol<iCol; i+
311ec 2b 29 7b 7d 0a 20 20 77 68 69 6c 65 28 20 69 3c  +){}.  while( i<
311ed 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68  nMatch && aMatch
311ee 5b 69 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74 63  [i].iStart+aMatc
311ef 68 5b 69 5d 2e 6e 42 79 74 65 3c 69 42 72 65 61  h[i].nByte<iBrea
311f0 6b 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 69 66  k ){ i++; }.  if
311f1 28 20 69 3c 6e 4d 61 74 63 68 20 29 7b 0a 20 20  ( i<nMatch ){.  
311f2 20 20 69 66 28 20 61 4d 61 74 63 68 5b 69 5d 2e    if( aMatch[i].
311f3 69 53 74 61 72 74 3c 69 42 72 65 61 6b 2b 31 30  iStart<iBreak+10
311f4 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
311f5 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72   aMatch[i].iStar
311f6 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
311f7 20 69 3e 30 20 26 26 20 61 4d 61 74 63 68 5b 69   i>0 && aMatch[i
311f8 2d 31 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74 63  -1].iStart+aMatc
311f9 68 5b 69 2d 31 5d 2e 6e 42 79 74 65 3e 3d 69 42  h[i-1].nByte>=iB
311fa 72 65 61 6b 20 29 7b 0a 20 20 20 20 20 20 72 65  reak ){.      re
311fb 74 75 72 6e 20 61 4d 61 74 63 68 5b 69 2d 31 5d  turn aMatch[i-1]
311fc 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a 20  .iStart;.    }. 
311fd 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c   }.  for(i=1; i<
311fe 3d 31 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  =10; i++){.    i
311ff 66 28 20 73 61 66 65 5f 69 73 73 70 61 63 65 28  f( safe_isspace(
31200 7a 44 6f 63 5b 69 42 72 65 61 6b 2d 69 5d 29 20  zDoc[iBreak-i]) 
31201 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
31202 69 42 72 65 61 6b 20 2d 20 69 20 2b 20 31 3b 0a  iBreak - i + 1;.
31203 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 61      }.    if( sa
31204 66 65 5f 69 73 73 70 61 63 65 28 7a 44 6f 63 5b  fe_isspace(zDoc[
31205 69 42 72 65 61 6b 2b 69 5d 29 20 29 7b 0a 20 20  iBreak+i]) ){.  
31206 20 20 20 20 72 65 74 75 72 6e 20 69 42 72 65 61      return iBrea
31207 6b 20 2b 20 69 20 2b 20 31 3b 0a 20 20 20 20 7d  k + i + 1;.    }
31208 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 42  .  }.  return iB
31209 72 65 61 6b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a  reak;.}..../*.**
3120a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   Allowed values 
3120b 66 6f 72 20 53 6e 69 70 70 65 74 2e 61 4d 61 74  for Snippet.aMat
3120c 63 68 5b 5d 2e 73 6e 53 74 61 74 75 73 0a 2a 2f  ch[].snStatus.*/
3120d 0a 23 64 65 66 69 6e 65 20 53 4e 49 50 50 45 54  .#define SNIPPET
3120e 5f 49 47 4e 4f 52 45 20 20 30 20 20 20 2f 2a 20  _IGNORE  0   /* 
3120f 49 74 20 69 73 20 6f 6b 20 74 6f 20 6f 6d 69 74  It is ok to omit
31210 20 74 68 69 73 20 6d 61 74 63 68 20 66 72 6f 6d   this match from
31211 20 74 68 65 20 73 6e 69 70 70 65 74 20 2a 2f 0a   the snippet */.
31212 23 64 65 66 69 6e 65 20 53 4e 49 50 50 45 54 5f  #define SNIPPET_
31213 44 45 53 49 52 45 44 20 31 20 20 20 2f 2a 20 57  DESIRED 1   /* W
31214 65 20 77 61 6e 74 20 74 6f 20 69 6e 63 6c 75 64  e want to includ
31215 65 20 74 68 69 73 20 6d 61 74 63 68 20 69 6e 20  e this match in 
31216 74 68 65 20 73 6e 69 70 70 65 74 20 2a 2f 0a 0a  the snippet */..
31217 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74  /*.** Generate t
31218 68 65 20 74 65 78 74 20 6f 66 20 61 20 73 6e 69  he text of a sni
31219 70 70 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ppet..*/.static 
3121a 76 6f 69 64 20 73 6e 69 70 70 65 74 54 65 78 74  void snippetText
3121b 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  (.  fulltext_cur
3121c 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 20  sor *pCursor,   
3121d 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 77 65  /* The cursor we
3121e 20 6e 65 65 64 20 74 68 65 20 73 6e 69 70 70 65   need the snippe
3121f 74 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  t for */.  const
31220 20 63 68 61 72 20 2a 7a 53 74 61 72 74 4d 61 72   char *zStartMar
31221 6b 2c 20 20 20 20 20 2f 2a 20 4d 61 72 6b 75 70  k,     /* Markup
31222 20 74 6f 20 61 70 70 65 61 72 20 62 65 66 6f 72   to appear befor
31223 65 20 65 61 63 68 20 6d 61 74 63 68 20 2a 2f 0a  e each match */.
31224 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
31225 6e 64 4d 61 72 6b 2c 20 20 20 20 20 20 20 2f 2a  ndMark,       /*
31226 20 4d 61 72 6b 75 70 20 74 6f 20 61 70 70 65 61   Markup to appea
31227 72 20 61 66 74 65 72 20 65 61 63 68 20 6d 61 74  r after each mat
31228 63 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ch */.  const ch
31229 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20 20 20  ar *zEllipsis   
3122a 20 20 20 20 2f 2a 20 45 6c 6c 69 70 73 69 73 20      /* Ellipsis 
3122b 6d 61 72 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  mark */.){.  int
3122c 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75 63 74 20   i, j;.  struct 
3122d 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 61 4d  snippetMatch *aM
3122e 61 74 63 68 3b 0a 20 20 69 6e 74 20 6e 4d 61 74  atch;.  int nMat
3122f 63 68 3b 0a 20 20 69 6e 74 20 6e 44 65 73 69 72  ch;.  int nDesir
31230 65 64 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66  ed;.  StringBuff
31231 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 74 61 69  er sb;.  int tai
31232 6c 43 6f 6c 3b 0a 20 20 69 6e 74 20 74 61 69 6c  lCol;.  int tail
31233 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 69 43  Offset;.  int iC
31234 6f 6c 3b 0a 20 20 69 6e 74 20 6e 44 6f 63 3b 0a  ol;.  int nDoc;.
31235 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
31236 6f 63 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74  oc;.  int iStart
31237 2c 20 69 45 6e 64 3b 0a 20 20 69 6e 74 20 74 61  , iEnd;.  int ta
31238 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b 0a  ilEllipsis = 0;.
31239 20 20 69 6e 74 20 69 4d 61 74 63 68 3b 0a 20 20    int iMatch;.  
3123a 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
3123b 28 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65  (pCursor->snippe
3123c 74 2e 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20 70  t.zSnippet);.  p
3123d 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
3123e 7a 53 6e 69 70 70 65 74 20 3d 20 30 3b 0a 20 20  zSnippet = 0;.  
3123f 61 4d 61 74 63 68 20 3d 20 70 43 75 72 73 6f 72  aMatch = pCursor
31240 2d 3e 73 6e 69 70 70 65 74 2e 61 4d 61 74 63 68  ->snippet.aMatch
31241 3b 0a 20 20 6e 4d 61 74 63 68 20 3d 20 70 43 75  ;.  nMatch = pCu
31242 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e 4d  rsor->snippet.nM
31243 61 74 63 68 3b 0a 20 20 69 6e 69 74 53 74 72 69  atch;.  initStri
31244 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a 0a  ngBuffer(&sb);..
31245 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61    for(i=0; i<nMa
31246 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61  tch; i++){.    a
31247 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75  Match[i].snStatu
31248 73 20 3d 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f  s = SNIPPET_IGNO
31249 52 45 3b 0a 20 20 7d 0a 20 20 6e 44 65 73 69 72  RE;.  }.  nDesir
3124a 65 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  ed = 0;.  for(i=
3124b 30 3b 20 69 3c 46 54 53 33 5f 52 4f 54 4f 52 5f  0; i<FTS3_ROTOR_
3124c 53 5a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f  SZ; i++){.    fo
3124d 72 28 6a 3d 30 3b 20 6a 3c 6e 4d 61 74 63 68 3b  r(j=0; j<nMatch;
3124e 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   j++){.      if(
3124f 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72 6d   aMatch[j].iTerm
31250 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==i ){.        a
31251 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75  Match[j].snStatu
31252 73 20 3d 20 53 4e 49 50 50 45 54 5f 44 45 53 49  s = SNIPPET_DESI
31253 52 45 44 3b 0a 20 20 20 20 20 20 20 20 6e 44 65  RED;.        nDe
31254 73 69 72 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20  sired++;.       
31255 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
31256 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 4d 61      }.  }..  iMa
31257 74 63 68 20 3d 20 30 3b 0a 20 20 74 61 69 6c 43  tch = 0;.  tailC
31258 6f 6c 20 3d 20 2d 31 3b 0a 20 20 74 61 69 6c 4f  ol = -1;.  tailO
31259 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72  ffset = 0;.  for
3125a 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20 26  (i=0; i<nMatch &
3125b 26 20 6e 44 65 73 69 72 65 64 3e 30 3b 20 69 2b  & nDesired>0; i+
3125c 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74  +){.    if( aMat
3125d 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73 21 3d  ch[i].snStatus!=
3125e 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44 20  SNIPPET_DESIRED 
3125f 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
31260 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20  nDesired--;.    
31261 69 43 6f 6c 20 3d 20 61 4d 61 74 63 68 5b 69 5d  iCol = aMatch[i]
31262 2e 69 43 6f 6c 3b 0a 20 20 20 20 7a 44 6f 63 20  .iCol;.    zDoc 
31263 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73  = (const char*)s
31264 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
31265 78 74 28 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d  xt(pCursor->pStm
31266 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20  t, iCol+1);.    
31267 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63  nDoc = sqlite3_c
31268 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43 75 72  olumn_bytes(pCur
31269 73 6f 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c  sor->pStmt, iCol
3126a 2b 31 29 3b 0a 20 20 20 20 69 53 74 61 72 74 20  +1);.    iStart 
3126b 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  = aMatch[i].iSta
3126c 72 74 20 2d 20 34 30 3b 0a 20 20 20 20 69 53 74  rt - 40;.    iSt
3126d 61 72 74 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61  art = wordBounda
3126e 72 79 28 69 53 74 61 72 74 2c 20 7a 44 6f 63 2c  ry(iStart, zDoc,
3126f 20 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e   nDoc, aMatch, n
31270 4d 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20  Match, iCol);.  
31271 20 20 69 66 28 20 69 53 74 61 72 74 3c 3d 31 30    if( iStart<=10
31272 20 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74   ){.      iStart
31273 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
31274 69 66 28 20 69 43 6f 6c 3d 3d 74 61 69 6c 43 6f  if( iCol==tailCo
31275 6c 20 26 26 20 69 53 74 61 72 74 3c 3d 74 61 69  l && iStart<=tai
31276 6c 4f 66 66 73 65 74 2b 32 30 20 29 7b 0a 20 20  lOffset+20 ){.  
31277 20 20 20 20 69 53 74 61 72 74 20 3d 20 74 61 69      iStart = tai
31278 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20  lOffset;.    }. 
31279 20 20 20 69 66 28 20 28 69 43 6f 6c 21 3d 74 61     if( (iCol!=ta
3127a 69 6c 43 6f 6c 20 26 26 20 74 61 69 6c 43 6f 6c  ilCol && tailCol
3127b 3e 3d 30 29 20 7c 7c 20 69 53 74 61 72 74 21 3d  >=0) || iStart!=
3127c 74 61 69 6c 4f 66 66 73 65 74 20 29 7b 0a 20 20  tailOffset ){.  
3127d 20 20 20 20 74 72 69 6d 57 68 69 74 65 53 70 61      trimWhiteSpa
3127e 63 65 28 26 73 62 29 3b 0a 20 20 20 20 20 20 61  ce(&sb);.      a
3127f 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65 28  ppendWhiteSpace(
31280 26 73 62 29 3b 0a 20 20 20 20 20 20 61 70 70 65  &sb);.      appe
31281 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69  nd(&sb, zEllipsi
31282 73 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e 64  s);.      append
31283 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b  WhiteSpace(&sb);
31284 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 20  .    }.    iEnd 
31285 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  = aMatch[i].iSta
31286 72 74 20 2b 20 61 4d 61 74 63 68 5b 69 5d 2e 6e  rt + aMatch[i].n
31287 42 79 74 65 20 2b 20 34 30 3b 0a 20 20 20 20 69  Byte + 40;.    i
31288 45 6e 64 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61  End = wordBounda
31289 72 79 28 69 45 6e 64 2c 20 7a 44 6f 63 2c 20 6e  ry(iEnd, zDoc, n
3128a 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d 61  Doc, aMatch, nMa
3128b 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20  tch, iCol);.    
3128c 69 66 28 20 69 45 6e 64 3e 3d 6e 44 6f 63 2d 31  if( iEnd>=nDoc-1
3128d 30 20 29 7b 0a 20 20 20 20 20 20 69 45 6e 64 20  0 ){.      iEnd 
3128e 3d 20 6e 44 6f 63 3b 0a 20 20 20 20 20 20 74 61  = nDoc;.      ta
3128f 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b 0a  ilEllipsis = 0;.
31290 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
31291 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20   tailEllipsis = 
31292 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69  1;.    }.    whi
31293 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63  le( iMatch<nMatc
31294 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74  h && aMatch[iMat
31295 63 68 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 20 29 7b  ch].iCol<iCol ){
31296 20 69 4d 61 74 63 68 2b 2b 3b 20 7d 0a 20 20 20   iMatch++; }.   
31297 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69   while( iStart<i
31298 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 77 68 69  End ){.      whi
31299 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63  le( iMatch<nMatc
3129a 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74  h && aMatch[iMat
3129b 63 68 5d 2e 69 53 74 61 72 74 3c 69 53 74 61 72  ch].iStart<iStar
3129c 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26  t.             &
3129d 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d  & aMatch[iMatch]
3129e 2e 69 43 6f 6c 3c 3d 69 43 6f 6c 20 29 7b 0a 20  .iCol<=iCol ){. 
3129f 20 20 20 20 20 20 20 69 4d 61 74 63 68 2b 2b 3b         iMatch++;
312a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
312a1 66 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68  f( iMatch<nMatch
312a2 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63   && aMatch[iMatc
312a3 68 5d 2e 69 53 74 61 72 74 3c 69 45 6e 64 0a 20  h].iStart<iEnd. 
312a4 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 61              && a
312a5 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 43  Match[iMatch].iC
312a6 6f 6c 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20  ol==iCol ){.    
312a7 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c      nappend(&sb,
312a8 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20   &zDoc[iStart], 
312a9 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69  aMatch[iMatch].i
312aa 53 74 61 72 74 20 2d 20 69 53 74 61 72 74 29 3b  Start - iStart);
312ab 0a 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20  .        iStart 
312ac 3d 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d  = aMatch[iMatch]
312ad 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20 20  .iStart;.       
312ae 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 53 74   append(&sb, zSt
312af 61 72 74 4d 61 72 6b 29 3b 0a 20 20 20 20 20 20  artMark);.      
312b0 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c 20 26    nappend(&sb, &
312b1 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20 61 4d  zDoc[iStart], aM
312b2 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 6e 42 79  atch[iMatch].nBy
312b3 74 65 29 3b 0a 20 20 20 20 20 20 20 20 61 70 70  te);.        app
312b4 65 6e 64 28 26 73 62 2c 20 7a 45 6e 64 4d 61 72  end(&sb, zEndMar
312b5 6b 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61  k);.        iSta
312b6 72 74 20 2b 3d 20 61 4d 61 74 63 68 5b 69 4d 61  rt += aMatch[iMa
312b7 74 63 68 5d 2e 6e 42 79 74 65 3b 0a 20 20 20 20  tch].nByte;.    
312b8 20 20 20 20 66 6f 72 28 6a 3d 69 4d 61 74 63 68      for(j=iMatch
312b9 2b 31 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a 2b  +1; j<nMatch; j+
312ba 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
312bb 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72  ( aMatch[j].iTer
312bc 6d 3d 3d 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  m==aMatch[iMatch
312bd 5d 2e 69 54 65 72 6d 0a 20 20 20 20 20 20 20 20  ].iTerm.        
312be 20 20 20 20 20 20 26 26 20 61 4d 61 74 63 68 5b        && aMatch[
312bf 6a 5d 2e 73 6e 53 74 61 74 75 73 3d 3d 53 4e 49  j].snStatus==SNI
312c0 50 50 45 54 5f 44 45 53 49 52 45 44 20 29 7b 0a  PPET_DESIRED ){.
312c1 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65 73              nDes
312c2 69 72 65 64 2d 2d 3b 0a 20 20 20 20 20 20 20 20  ired--;.        
312c3 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e      aMatch[j].sn
312c4 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45 54  Status = SNIPPET
312c5 5f 49 47 4e 4f 52 45 3b 0a 20 20 20 20 20 20 20  _IGNORE;.       
312c6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
312c7 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
312c8 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c      nappend(&sb,
312c9 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20   &zDoc[iStart], 
312ca 69 45 6e 64 20 2d 20 69 53 74 61 72 74 29 3b 0a  iEnd - iStart);.
312cb 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d          iStart =
312cc 20 69 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20   iEnd;.      }. 
312cd 20 20 20 7d 0a 20 20 20 20 74 61 69 6c 43 6f 6c     }.    tailCol
312ce 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 74 61 69   = iCol;.    tai
312cf 6c 4f 66 66 73 65 74 20 3d 20 69 45 6e 64 3b 0a  lOffset = iEnd;.
312d0 20 20 7d 0a 20 20 74 72 69 6d 57 68 69 74 65 53    }.  trimWhiteS
312d1 70 61 63 65 28 26 73 62 29 3b 0a 20 20 69 66 28  pace(&sb);.  if(
312d2 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 29 7b   tailEllipsis ){
312d3 0a 20 20 20 20 61 70 70 65 6e 64 57 68 69 74 65  .    appendWhite
312d4 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20  Space(&sb);.    
312d5 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45 6c 6c  append(&sb, zEll
312d6 69 70 73 69 73 29 3b 0a 20 20 7d 0a 20 20 70 43  ipsis);.  }.  pC
312d7 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a  ursor->snippet.z
312d8 53 6e 69 70 70 65 74 20 3d 20 73 74 72 69 6e 67  Snippet = string
312d9 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b  BufferData(&sb);
312da 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70  .  pCursor->snip
312db 70 65 74 2e 6e 53 6e 69 70 70 65 74 20 3d 20 73  pet.nSnippet = s
312dc 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
312dd 68 28 26 73 62 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  h(&sb);.}.../*.*
312de 2a 20 43 6c 6f 73 65 20 74 68 65 20 63 75 72 73  * Close the curs
312df 6f 72 2e 20 20 46 6f 72 20 61 64 64 69 74 69 6f  or.  For additio
312e0 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  nal information 
312e1 73 65 65 20 74 68 65 20 64 6f 63 75 6d 65 6e 74  see the document
312e2 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 68 65 20  ation.** on the 
312e3 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 6f 66  xClose method of
312e4 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
312e5 6c 65 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  le interface..*/
312e6 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
312e7 74 65 78 74 43 6c 6f 73 65 28 73 71 6c 69 74 65  textClose(sqlite
312e8 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
312e9 43 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c 74  Cursor){.  fullt
312ea 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  ext_cursor *c = 
312eb 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72  (fulltext_cursor
312ec 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 46   *) pCursor;.  F
312ed 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 43  TSTRACE(("FTS3 C
312ee 6c 6f 73 65 20 25 70 5c 6e 22 2c 20 63 29 29 3b  lose %p\n", c));
312ef 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  .  sqlite3_final
312f0 69 7a 65 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20  ize(c->pStmt);. 
312f1 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
312f2 46 72 65 65 28 63 2d 3e 70 45 78 70 72 29 3b 0a  Free(c->pExpr);.
312f3 20 20 73 6e 69 70 70 65 74 43 6c 65 61 72 28 26    snippetClear(&
312f4 63 2d 3e 73 6e 69 70 70 65 74 29 3b 0a 20 20 69  c->snippet);.  i
312f5 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61  f( c->result.nDa
312f6 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 64 6c 72  ta!=0 ){.    dlr
312f7 44 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 61 64  Destroy(&c->read
312f8 65 72 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42  er);.  }.  dataB
312f9 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 63 2d  ufferDestroy(&c-
312fa 3e 72 65 73 75 6c 74 29 3b 0a 20 20 73 71 6c 69  >result);.  sqli
312fb 74 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20 72  te3_free(c);.  r
312fc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
312fd 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .}..static int f
312fe 75 6c 6c 74 65 78 74 4e 65 78 74 28 73 71 6c 69  ulltextNext(sqli
312ff 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
31300 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c  *pCursor){.  ful
31301 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20  ltext_cursor *c 
31302 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  = (fulltext_curs
31303 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20  or *) pCursor;. 
31304 20 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54   int rc;..  FTST
31305 52 41 43 45 28 28 22 46 54 53 33 20 4e 65 78 74  RACE(("FTS3 Next
31306 20 25 70 5c 6e 22 2c 20 70 43 75 72 73 6f 72 29   %p\n", pCursor)
31307 29 3b 0a 20 20 73 6e 69 70 70 65 74 43 6c 65 61  );.  snippetClea
31308 72 28 26 63 2d 3e 73 6e 69 70 70 65 74 29 3b 0a  r(&c->snippet);.
31309 20 20 69 66 28 20 63 2d 3e 69 43 75 72 73 6f 72    if( c->iCursor
3130a 54 79 70 65 20 3c 20 51 55 45 52 59 5f 46 55 4c  Type < QUERY_FUL
3130b 4c 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20  LTEXT ){.    /* 
3130c 54 4f 44 4f 28 73 68 65 73 73 29 20 48 61 6e 64  TODO(shess) Hand
3130d 6c 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  le SQLITE_SCHEMA
3130e 20 41 4e 44 20 53 51 4c 49 54 45 5f 42 55 53 59   AND SQLITE_BUSY
3130f 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71  . */.    rc = sq
31310 6c 69 74 65 33 5f 73 74 65 70 28 63 2d 3e 70 53  lite3_step(c->pS
31311 74 6d 74 29 3b 0a 20 20 20 20 73 77 69 74 63 68  tmt);.    switch
31312 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 63 61  ( rc ){.      ca
31313 73 65 20 53 51 4c 49 54 45 5f 52 4f 57 3a 0a 20  se SQLITE_ROW:. 
31314 20 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20         c->eof = 
31315 30 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  0;.        retur
31316 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
31317 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44     case SQLITE_D
31318 4f 4e 45 3a 0a 20 20 20 20 20 20 20 20 63 2d 3e  ONE:.        c->
31319 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  eof = 1;.       
3131a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3131b 4b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74  K;.      default
3131c 3a 0a 20 20 20 20 20 20 20 20 63 2d 3e 65 6f 66  :.        c->eof
3131d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65   = 1;.        re
3131e 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
3131f 20 7d 20 65 6c 73 65 20 7b 20 20 2f 2a 20 66 75   } else {  /* fu
31320 6c 6c 2d 74 65 78 74 20 71 75 65 72 79 20 2a 2f  ll-text query */
31321 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
31322 33 5f 72 65 73 65 74 28 63 2d 3e 70 53 74 6d 74  3_reset(c->pStmt
31323 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
31324 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
31325 6e 20 72 63 3b 0a 0a 20 20 20 20 69 66 28 20 63  n rc;..    if( c
31326 2d 3e 72 65 73 75 6c 74 2e 6e 44 61 74 61 3d 3d  ->result.nData==
31327 30 20 7c 7c 20 64 6c 72 41 74 45 6e 64 28 26 63  0 || dlrAtEnd(&c
31328 2d 3e 72 65 61 64 65 72 29 20 29 7b 0a 20 20 20  ->reader) ){.   
31329 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20     c->eof = 1;. 
3132a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
3132b 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  TE_OK;.    }.   
3132c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
3132d 6e 64 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74 6d  nd_int64(c->pStm
3132e 74 2c 20 31 2c 20 64 6c 72 44 6f 63 69 64 28 26  t, 1, dlrDocid(&
3132f 63 2d 3e 72 65 61 64 65 72 29 29 3b 0a 20 20 20  c->reader));.   
31330 20 64 6c 72 53 74 65 70 28 26 63 2d 3e 72 65 61   dlrStep(&c->rea
31331 64 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  der);.    if( rc
31332 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
31333 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 2f 2a 20  turn rc;.    /* 
31334 54 4f 44 4f 28 73 68 65 73 73 29 20 48 61 6e 64  TODO(shess) Hand
31335 6c 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  le SQLITE_SCHEMA
31336 20 41 4e 44 20 53 51 4c 49 54 45 5f 42 55 53 59   AND SQLITE_BUSY
31337 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71  . */.    rc = sq
31338 6c 69 74 65 33 5f 73 74 65 70 28 63 2d 3e 70 53  lite3_step(c->pS
31339 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  tmt);.    if( rc
3133a 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 20  ==SQLITE_ROW ){ 
3133b 20 20 2f 2a 20 74 68 65 20 63 61 73 65 20 77 65    /* the case we
3133c 20 65 78 70 65 63 74 20 2a 2f 0a 20 20 20 20 20   expect */.     
3133d 20 63 2d 3e 65 6f 66 20 3d 20 30 3b 0a 20 20 20   c->eof = 0;.   
3133e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3133f 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  _OK;.    }.    /
31340 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  * an error occur
31341 72 65 64 3b 20 61 62 6f 72 74 20 2a 2f 0a 20 20  red; abort */.  
31342 20 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51 4c    return rc==SQL
31343 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 54  ITE_DONE ? SQLIT
31344 45 5f 45 52 52 4f 52 20 3a 20 72 63 3b 0a 20 20  E_ERROR : rc;.  
31345 7d 0a 7d 0a 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  }.}.../* TODO(sh
31346 65 73 73 29 20 49 66 20 77 65 20 70 75 73 68 65  ess) If we pushe
31347 64 20 4c 65 61 66 52 65 61 64 65 72 20 74 6f 20  d LeafReader to 
31348 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 66  the top of the f
31349 69 6c 65 2c 20 6f 72 20 74 6f 0a 2a 2a 20 61 6e  ile, or to.** an
3134a 6f 74 68 65 72 20 66 69 6c 65 2c 20 74 65 72 6d  other file, term
3134b 5f 73 65 6c 65 63 74 28 29 20 63 6f 75 6c 64 20  _select() could 
3134c 62 65 20 70 75 73 68 65 64 20 61 62 6f 76 65 0a  be pushed above.
3134d 2a 2a 20 64 6f 63 4c 69 73 74 4f 66 54 65 72 6d  ** docListOfTerm
3134e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ()..*/.static in
3134f 74 20 74 65 72 6d 53 65 6c 65 63 74 28 66 75 6c  t termSelect(ful
31350 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
31351 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20  nt iColumn,.    
31352 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31353 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
31354 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
31355 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
31356 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31357 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20      DocListType 
31358 69 54 79 70 65 2c 20 44 61 74 61 42 75 66 66 65  iType, DataBuffe
31359 72 20 2a 6f 75 74 29 3b 0a 0a 2f 2a 20 0a 2a 2a  r *out);../* .**
3135a 20 52 65 74 75 72 6e 20 61 20 44 6f 63 4c 69 73   Return a DocLis
3135b 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  t corresponding 
3135c 74 6f 20 74 68 65 20 70 68 72 61 73 65 20 2a 70  to the phrase *p
3135d 50 68 72 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  Phrase..**.** Th
3135e 65 20 72 65 73 75 6c 74 69 6e 67 20 44 4c 5f 44  e resulting DL_D
3135f 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20 69 73  OCIDS doclist is
31360 20 73 74 6f 72 65 64 20 69 6e 20 70 52 65 73 75   stored in pResu
31361 6c 74 2c 20 77 68 69 63 68 20 69 73 0a 2a 2a 20  lt, which is.** 
31362 6f 76 65 72 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a  overwritten..*/.
31363 73 74 61 74 69 63 20 69 6e 74 20 64 6f 63 4c 69  static int docLi
31364 73 74 4f 66 50 68 72 61 73 65 28 0a 20 20 66 75  stOfPhrase(.  fu
31365 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 54 61  lltext_vtab *pTa
31366 62 2c 20 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c  b,   /* The full
31367 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20   text index */. 
31368 20 46 74 73 33 50 68 72 61 73 65 20 2a 70 50 68   Fts3Phrase *pPh
31369 72 61 73 65 2c 20 20 20 2f 2a 20 50 68 72 61 73  rase,   /* Phras
3136a 65 20 74 6f 20 72 65 74 75 72 6e 20 61 20 64 6f  e to return a do
3136b 63 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64  clist correspond
3136c 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 44 6f 63 4c  ing to */.  DocL
3136d 69 73 74 54 79 70 65 20 65 4c 69 73 74 54 79 70  istType eListTyp
3136e 65 2c 20 2f 2a 20 45 69 74 68 65 72 20 44 4c 5f  e, /* Either DL_
3136f 44 4f 43 49 44 53 20 6f 72 20 44 4c 5f 50 4f 53  DOCIDS or DL_POS
31370 49 54 49 4f 4e 53 20 2a 2f 0a 20 20 44 61 74 61  ITIONS */.  Data
31371 42 75 66 66 65 72 20 2a 70 52 65 73 75 6c 74 20  Buffer *pResult 
31372 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
31373 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29  result here */.)
31374 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e  {.  int ii;.  in
31375 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
31376 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70  ;.  int iCol = p
31377 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 3b  Phrase->iColumn;
31378 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 65  .  DocListType e
31379 54 79 70 65 20 3d 20 65 4c 69 73 74 54 79 70 65  Type = eListType
3137a 3b 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70  ;.  assert( eTyp
3137b 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20  e==DL_POSITIONS 
3137c 7c 7c 20 65 54 79 70 65 3d 3d 44 4c 5f 44 4f 43  || eType==DL_DOC
3137d 49 44 53 20 29 3b 0a 20 20 69 66 28 20 70 50 68  IDS );.  if( pPh
3137e 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3e 31 20 29  rase->nToken>1 )
3137f 7b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 44 4c  {.    eType = DL
31380 5f 50 4f 53 49 54 49 4f 4e 53 3b 0a 20 20 7d 0a  _POSITIONS;.  }.
31381 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20  .  /* This code 
31382 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20  should never be 
31383 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66 66  called with buff
31384 65 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a 2f  ered updates. */
31385 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d  .  assert( pTab-
31386 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20  >nPendingData<0 
31387 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
31388 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
31389 20 69 69 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f   ii<pPhrase->nTo
3138a 6b 65 6e 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ken; ii++){.    
3138b 44 61 74 61 42 75 66 66 65 72 20 74 6d 70 3b 0a  DataBuffer tmp;.
3138c 20 20 20 20 73 74 72 75 63 74 20 50 68 72 61 73      struct Phras
3138d 65 54 6f 6b 65 6e 20 2a 70 20 3d 20 26 70 50 68  eToken *p = &pPh
3138e 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d  rase->aToken[ii]
3138f 3b 0a 20 20 20 20 72 63 20 3d 20 74 65 72 6d 53  ;.    rc = termS
31390 65 6c 65 63 74 28 70 54 61 62 2c 20 69 43 6f 6c  elect(pTab, iCol
31391 2c 20 70 2d 3e 7a 2c 20 70 2d 3e 6e 2c 20 70 2d  , p->z, p->n, p-
31392 3e 69 73 50 72 65 66 69 78 2c 20 65 54 79 70 65  >isPrefix, eType
31393 2c 20 26 74 6d 70 29 3b 0a 20 20 20 20 69 66 28  , &tmp);.    if(
31394 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
31395 7b 0a 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d  {.      if( ii==
31396 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 52  0 ){.        *pR
31397 65 73 75 6c 74 20 3d 20 74 6d 70 3b 0a 20 20 20  esult = tmp;.   
31398 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
31399 20 20 44 61 74 61 42 75 66 66 65 72 20 72 65 73    DataBuffer res
3139a 20 3d 20 2a 70 52 65 73 75 6c 74 3b 0a 20 20 20   = *pResult;.   
3139b 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49       dataBufferI
3139c 6e 69 74 28 70 52 65 73 75 6c 74 2c 20 30 29 3b  nit(pResult, 0);
3139d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 69 3d  .        if( ii=
3139e 3d 28 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  =(pPhrase->nToke
3139f 6e 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  n-1) ){.        
313a0 20 20 65 54 79 70 65 20 3d 20 65 4c 69 73 74 54    eType = eListT
313a1 79 70 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ype;.        }. 
313a2 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 50 68         docListPh
313a3 72 61 73 65 4d 65 72 67 65 28 0a 20 20 20 20 20  raseMerge(.     
313a4 20 20 20 20 20 72 65 73 2e 70 44 61 74 61 2c 20       res.pData, 
313a5 72 65 73 2e 6e 44 61 74 61 2c 20 74 6d 70 2e 70  res.nData, tmp.p
313a6 44 61 74 61 2c 20 74 6d 70 2e 6e 44 61 74 61 2c  Data, tmp.nData,
313a7 20 30 2c 20 30 2c 20 65 54 79 70 65 2c 20 70 52   0, 0, eType, pR
313a8 65 73 75 6c 74 0a 20 20 20 20 20 20 20 20 29 3b  esult.        );
313a9 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
313aa 66 65 72 44 65 73 74 72 6f 79 28 26 72 65 73 29  ferDestroy(&res)
313ab 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75  ;.        dataBu
313ac 66 66 65 72 44 65 73 74 72 6f 79 28 26 74 6d 70  fferDestroy(&tmp
313ad 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
313ae 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
313af 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 76 61 6c  c;.}../*.** Eval
313b0 75 61 74 65 20 74 68 65 20 66 75 6c 6c 2d 74 65  uate the full-te
313b1 78 74 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  xt expression pE
313b2 78 70 72 20 61 67 61 69 6e 73 74 20 66 74 73 33  xpr against fts3
313b3 20 74 61 62 6c 65 20 70 54 61 62 2e 20 57 72 69   table pTab. Wri
313b4 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  te.** the result
313b5 73 20 69 6e 74 6f 20 70 52 65 73 2e 0a 2a 2f 0a  s into pRes..*/.
313b6 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c 46  static int evalF
313b7 74 73 33 45 78 70 72 28 0a 20 20 66 75 6c 6c 74  ts3Expr(.  fullt
313b8 65 78 74 5f 76 74 61 62 20 2a 70 54 61 62 2c 20  ext_vtab *pTab, 
313b9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 74 73            /* Fts
313ba 33 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20  3 Virtual table 
313bb 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 46 74 73 33  object */.  Fts3
313bc 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20  Expr *pExpr,    
313bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
313be 72 73 65 64 20 66 74 73 33 20 65 78 70 72 65 73  rsed fts3 expres
313bf 73 69 6f 6e 20 2a 2f 0a 20 20 44 61 74 61 42 75  sion */.  DataBu
313c0 66 66 65 72 20 2a 70 52 65 73 20 20 20 20 20 20  ffer *pRes      
313c1 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
313c2 20 57 72 69 74 65 20 72 65 73 75 6c 74 73 20 6f   Write results o
313c3 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
313c4 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
313c5 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
313c6 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ;..  /* Initiali
313c7 7a 65 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  ze the output bu
313c8 66 66 65 72 2e 20 49 66 20 74 68 69 73 20 69 73  ffer. If this is
313c9 20 61 6e 20 65 6d 70 74 79 20 71 75 65 72 79 20   an empty query 
313ca 28 70 45 78 70 72 3d 3d 30 29 2c 20 0a 20 20 2a  (pExpr==0), .  *
313cb 2a 20 74 68 69 73 20 69 73 20 61 6c 6c 20 74 68  * this is all th
313cc 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 64  at needs to be d
313cd 6f 6e 65 2e 20 45 6d 70 74 79 20 71 75 65 72 69  one. Empty queri
313ce 65 73 20 70 72 6f 64 75 63 65 20 65 6d 70 74 79  es produce empty
313cf 20 0a 20 20 2a 2a 20 72 65 73 75 6c 74 20 73 65   .  ** result se
313d0 74 73 2e 0a 20 20 2a 2f 0a 20 20 64 61 74 61 42  ts..  */.  dataB
313d1 75 66 66 65 72 49 6e 69 74 28 70 52 65 73 2c 20  ufferInit(pRes, 
313d2 30 29 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72  0);..  if( pExpr
313d3 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70   ){.    if( pExp
313d4 72 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  r->eType==FTSQUE
313d5 52 59 5f 50 48 52 41 53 45 20 29 7b 0a 20 20 20  RY_PHRASE ){.   
313d6 20 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 65     DocListType e
313d7 54 79 70 65 20 3d 20 44 4c 5f 44 4f 43 49 44 53  Type = DL_DOCIDS
313d8 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  ;.      if( pExp
313d9 72 2d 3e 70 50 61 72 65 6e 74 20 26 26 20 70 45  r->pParent && pE
313da 78 70 72 2d 3e 70 50 61 72 65 6e 74 2d 3e 65 54  xpr->pParent->eT
313db 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45  ype==FTSQUERY_NE
313dc 41 52 20 29 7b 0a 20 20 20 20 20 20 20 20 65 54  AR ){.        eT
313dd 79 70 65 20 3d 20 44 4c 5f 50 4f 53 49 54 49 4f  ype = DL_POSITIO
313de 4e 53 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  NS;.      }.    
313df 20 20 72 63 20 3d 20 64 6f 63 4c 69 73 74 4f 66    rc = docListOf
313e0 50 68 72 61 73 65 28 70 54 61 62 2c 20 70 45 78  Phrase(pTab, pEx
313e1 70 72 2d 3e 70 50 68 72 61 73 65 2c 20 65 54 79  pr->pPhrase, eTy
313e2 70 65 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d  pe, pRes);.    }
313e3 65 6c 73 65 7b 0a 20 20 20 20 20 20 44 61 74 61  else{.      Data
313e4 42 75 66 66 65 72 20 6c 68 73 3b 0a 20 20 20 20  Buffer lhs;.    
313e5 20 20 44 61 74 61 42 75 66 66 65 72 20 72 68 73    DataBuffer rhs
313e6 3b 0a 0a 20 20 20 20 20 20 64 61 74 61 42 75 66  ;..      dataBuf
313e7 66 65 72 49 6e 69 74 28 26 72 68 73 2c 20 30 29  ferInit(&rhs, 0)
313e8 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49  ;.      if( SQLI
313e9 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 65 76 61  TE_OK==(rc = eva
313ea 6c 46 74 73 33 45 78 70 72 28 70 54 61 62 2c 20  lFts3Expr(pTab, 
313eb 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 6c  pExpr->pLeft, &l
313ec 68 73 29 29 20 0a 20 20 20 20 20 20 20 26 26 20  hs)) .       && 
313ed 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
313ee 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 70 54   evalFts3Expr(pT
313ef 61 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ab, pExpr->pRigh
313f0 74 2c 20 26 72 68 73 29 29 20 0a 20 20 20 20 20  t, &rhs)) .     
313f1 20 29 7b 0a 20 20 20 20 20 20 20 20 73 77 69 74   ){.        swit
313f2 63 68 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65  ch( pExpr->eType
313f3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 61   ){.          ca
313f4 73 65 20 46 54 53 51 55 45 52 59 5f 4e 45 41 52  se FTSQUERY_NEAR
313f5 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
313f6 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20  int nToken;.    
313f7 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72          Fts3Expr
313f8 20 2a 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20   *pLeft;.       
313f9 20 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70 65       DocListType
313fa 20 65 54 79 70 65 20 3d 20 44 4c 5f 44 4f 43 49   eType = DL_DOCI
313fb 44 53 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  DS;.            
313fc 69 66 28 20 70 45 78 70 72 2d 3e 70 50 61 72 65  if( pExpr->pPare
313fd 6e 74 20 26 26 20 70 45 78 70 72 2d 3e 70 50 61  nt && pExpr->pPa
313fe 72 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53  rent->eType==FTS
313ff 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20  QUERY_NEAR ){.  
31400 20 20 20 20 20 20 20 20 20 20 20 20 65 54 79 70              eTyp
31401 65 20 3d 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  e = DL_POSITIONS
31402 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
31403 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 65 66              pLef
31404 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  t = pExpr->pLeft
31405 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 77 68  ;.            wh
31406 69 6c 65 28 20 70 4c 65 66 74 2d 3e 65 54 79 70  ile( pLeft->eTyp
31407 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
31408 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 20   ){ .           
31409 20 20 20 70 4c 65 66 74 3d 70 4c 65 66 74 2d 3e     pLeft=pLeft->
3140a 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
3140b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
3140c 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
3140d 3e 70 52 69 67 68 74 2d 3e 65 54 79 70 65 3d 3d  >pRight->eType==
3140e 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
3140f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  );.            a
31410 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 65 54  ssert( pLeft->eT
31411 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
31412 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20  RASE );.        
31413 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 70 4c 65      nToken = pLe
31414 66 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  ft->pPhrase->nTo
31415 6b 65 6e 20 2b 20 70 45 78 70 72 2d 3e 70 52 69  ken + pExpr->pRi
31416 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  ght->pPhrase->nT
31417 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 20 20  oken;.          
31418 20 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65 4d    docListPhraseM
31419 65 72 67 65 28 6c 68 73 2e 70 44 61 74 61 2c 20  erge(lhs.pData, 
3141a 6c 68 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70  lhs.nData, rhs.p
3141b 44 61 74 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c  Data, rhs.nData,
3141c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
3141d 20 20 70 45 78 70 72 2d 3e 6e 4e 65 61 72 2b 31    pExpr->nNear+1
3141e 2c 20 6e 54 6f 6b 65 6e 2c 20 65 54 79 70 65 2c  , nToken, eType,
3141f 20 70 52 65 73 0a 20 20 20 20 20 20 20 20 20 20   pRes.          
31420 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20    );.           
31421 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
31422 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 61    }.          ca
31423 73 65 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 3a  se FTSQUERY_NOT:
31424 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64   {.            d
31425 6f 63 4c 69 73 74 45 78 63 65 70 74 4d 65 72 67  ocListExceptMerg
31426 65 28 6c 68 73 2e 70 44 61 74 61 2c 20 6c 68 73  e(lhs.pData, lhs
31427 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70 44 61 74  .nData, rhs.pDat
31428 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c 70 52 65  a, rhs.nData,pRe
31429 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  s);.            
3142a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
3142b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 61 73   }.          cas
3142c 65 20 46 54 53 51 55 45 52 59 5f 41 4e 44 3a 20  e FTSQUERY_AND: 
3142d 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 6f  {.            do
3142e 63 4c 69 73 74 41 6e 64 4d 65 72 67 65 28 6c 68  cListAndMerge(lh
3142f 73 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44 61  s.pData, lhs.nDa
31430 74 61 2c 20 72 68 73 2e 70 44 61 74 61 2c 20 72  ta, rhs.pData, r
31431 68 73 2e 6e 44 61 74 61 2c 20 70 52 65 73 29 3b  hs.nData, pRes);
31432 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
31433 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
31434 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 46            case F
31435 54 53 51 55 45 52 59 5f 4f 52 3a 20 7b 0a 20 20  TSQUERY_OR: {.  
31436 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73            docLis
31437 74 4f 72 4d 65 72 67 65 28 6c 68 73 2e 70 44 61  tOrMerge(lhs.pDa
31438 74 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72  ta, lhs.nData, r
31439 68 73 2e 70 44 61 74 61 2c 20 72 68 73 2e 6e 44  hs.pData, rhs.nD
3143a 61 74 61 2c 20 70 52 65 73 29 3b 0a 20 20 20 20  ata, pRes);.    
3143b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3143c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3143d 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
3143e 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
3143f 74 72 6f 79 28 26 6c 68 73 29 3b 0a 20 20 20 20  troy(&lhs);.    
31440 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
31441 72 6f 79 28 26 72 68 73 29 3b 0a 20 20 20 20 7d  roy(&rhs);.    }
31442 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
31443 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  c;.}../* TODO(sh
31444 65 73 73 29 20 52 65 66 61 63 74 6f 72 20 74 68  ess) Refactor th
31445 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65  e code to remove
31446 20 74 68 69 73 20 66 6f 72 77 61 72 64 20 64 65   this forward de
31447 63 6c 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  cl. */.static in
31448 74 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  t flushPendingTe
31449 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rms(fulltext_vta
3144a 62 20 2a 76 29 3b 0a 0a 2f 2a 20 50 65 72 66 6f  b *v);../* Perfo
3144b 72 6d 20 61 20 66 75 6c 6c 2d 74 65 78 74 20 71  rm a full-text q
3144c 75 65 72 79 20 75 73 69 6e 67 20 74 68 65 20 73  uery using the s
3144d 65 61 72 63 68 20 65 78 70 72 65 73 73 69 6f 6e  earch expression
3144e 20 69 6e 0a 2a 2a 20 7a 49 6e 70 75 74 5b 30 2e   in.** zInput[0.
3144f 2e 6e 49 6e 70 75 74 2d 31 5d 2e 20 20 52 65 74  .nInput-1].  Ret
31450 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 6d 61  urn a list of ma
31451 74 63 68 69 6e 67 20 64 6f 63 75 6d 65 6e 74 73  tching documents
31452 0a 2a 2a 20 69 6e 20 70 52 65 73 75 6c 74 2e 0a  .** in pResult..
31453 2a 2a 0a 2a 2a 20 51 75 65 72 69 65 73 20 6d 75  **.** Queries mu
31454 73 74 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e 20  st match column 
31455 69 43 6f 6c 75 6d 6e 2e 20 20 4f 72 20 69 66 20  iColumn.  Or if 
31456 69 43 6f 6c 75 6d 6e 3e 3d 6e 43 6f 6c 75 6d 6e  iColumn>=nColumn
31457 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 61 6c 6c  .** they are all
31458 6f 77 65 64 20 74 6f 20 6d 61 74 63 68 20 61 67  owed to match ag
31459 61 69 6e 73 74 20 61 6e 79 20 63 6f 6c 75 6d 6e  ainst any column
3145a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3145b 66 75 6c 6c 74 65 78 74 51 75 65 72 79 28 0a 20  fulltextQuery(. 
3145c 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
3145d 76 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66  v,      /* The f
3145e 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a  ull text index *
3145f 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c  /.  int iColumn,
31460 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
31461 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 69 73  tch against this
31462 20 63 6f 6c 75 6d 6e 20 62 79 20 64 65 66 61 75   column by defau
31463 6c 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  lt */.  const ch
31464 61 72 20 2a 7a 49 6e 70 75 74 2c 20 20 20 20 2f  ar *zInput,    /
31465 2a 20 54 68 65 20 71 75 65 72 79 20 73 74 72 69  * The query stri
31466 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70  ng */.  int nInp
31467 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ut,            /
31468 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
31469 73 20 69 6e 20 7a 49 6e 70 75 74 5b 5d 20 2a 2f  s in zInput[] */
3146a 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70  .  DataBuffer *p
3146b 52 65 73 75 6c 74 2c 20 20 20 2f 2a 20 57 72 69  Result,   /* Wri
3146c 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 64 6f  te the result do
3146d 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 20 20  clist here */.  
3146e 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
3146f 72 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20  r        /* Put 
31470 70 61 72 73 65 64 20 71 75 65 72 79 20 73 74 72  parsed query str
31471 69 6e 67 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ing here */.){. 
31472 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54   int rc;..  /* T
31473 4f 44 4f 28 73 68 65 73 73 29 20 49 6e 73 74 65  ODO(shess) Inste
31474 61 64 20 6f 66 20 66 6c 75 73 68 69 6e 67 20 70  ad of flushing p
31475 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 77 65 20  endingTerms, we 
31476 63 6f 75 6c 64 20 71 75 65 72 79 20 66 6f 72 0a  could query for.
31477 20 20 2a 2a 20 74 68 65 20 72 65 6c 65 76 61 6e    ** the relevan
31478 74 20 74 65 72 6d 20 61 6e 64 20 6d 65 72 67 65  t term and merge
31479 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e 74   the doclist int
3147a 6f 20 77 68 61 74 20 77 65 20 72 65 63 65 69 76  o what we receiv
3147b 65 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20  e from.  ** the 
3147c 64 61 74 61 62 61 73 65 2e 20 20 57 61 69 74 20  database.  Wait 
3147d 61 6e 64 20 73 65 65 20 69 66 20 74 68 69 73 20  and see if this 
3147e 69 73 20 61 20 63 6f 6d 6d 6f 6e 20 69 73 73 75  is a common issu
3147f 65 2c 20 66 69 72 73 74 2e 0a 20 20 2a 2a 0a 20  e, first..  **. 
31480 20 2a 2a 20 41 20 67 6f 6f 64 20 72 65 61 73 6f   ** A good reaso
31481 6e 20 6e 6f 74 20 74 6f 20 66 6c 75 73 68 20 69  n not to flush i
31482 73 20 74 6f 20 6e 6f 74 20 67 65 6e 65 72 61 74  s to not generat
31483 65 20 75 70 64 61 74 65 2d 72 65 6c 61 74 65 64  e update-related
31484 0a 20 20 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  .  ** error code
31485 73 20 66 72 6f 6d 20 68 65 72 65 2e 0a 20 20 2a  s from here..  *
31486 2f 0a 0a 20 20 2f 2a 20 46 6c 75 73 68 20 61 6e  /..  /* Flush an
31487 79 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  y buffered updat
31488 65 73 20 62 65 66 6f 72 65 20 65 78 65 63 75 74  es before execut
31489 69 6e 67 20 74 68 65 20 71 75 65 72 79 2e 20 2a  ing the query. *
3148a 2f 0a 20 20 72 63 20 3d 20 66 6c 75 73 68 50 65  /.  rc = flushPe
3148b 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20  ndingTerms(v);. 
3148c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3148d 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
3148e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50   rc;.  }..  /* P
3148f 61 72 73 65 20 74 68 65 20 71 75 65 72 79 20 70  arse the query p
31490 61 73 73 65 64 20 74 6f 20 74 68 65 20 4d 41 54  assed to the MAT
31491 43 48 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a  CH operator. */.
31492 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74    rc = sqlite3Ft
31493 73 33 45 78 70 72 50 61 72 73 65 28 76 2d 3e 70  s3ExprParse(v->p
31494 54 6f 6b 65 6e 69 7a 65 72 2c 20 0a 20 20 20 20  Tokenizer, .    
31495 20 20 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 2c 20 76    v->azColumn, v
31496 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69 43 6f 6c 75  ->nColumn, iColu
31497 6d 6e 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70  mn, zInput, nInp
31498 75 74 2c 20 70 70 45 78 70 72 0a 20 20 29 3b 0a  ut, ppExpr.  );.
31499 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3149a 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
3149b 74 28 20 30 3d 3d 28 2a 70 70 45 78 70 72 29 20  t( 0==(*ppExpr) 
3149c 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
3149d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
3149e 65 76 61 6c 46 74 73 33 45 78 70 72 28 76 2c 20  evalFts3Expr(v, 
3149f 2a 70 70 45 78 70 72 2c 20 70 52 65 73 75 6c 74  *ppExpr, pResult
314a0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
314a1 20 69 73 20 74 68 65 20 78 46 69 6c 74 65 72 20   is the xFilter 
314a2 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 74 68  interface for th
314a3 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
314a4 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 76 69 72    See.** the vir
314a5 74 75 61 6c 20 74 61 62 6c 65 20 78 46 69 6c 74  tual table xFilt
314a6 65 72 20 6d 65 74 68 6f 64 20 64 6f 63 75 6d 65  er method docume
314a7 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69  ntation for addi
314a8 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d  tional.** inform
314a9 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ation..**.** If 
314aa 69 64 78 4e 75 6d 3d 3d 51 55 45 52 59 5f 47 45  idxNum==QUERY_GE
314ab 4e 45 52 49 43 20 74 68 65 6e 20 64 6f 20 61 20  NERIC then do a 
314ac 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 20  full table scan 
314ad 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 25  against.** the %
314ae 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 2e 0a  _content table..
314af 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d  **.** If idxNum=
314b0 3d 51 55 45 52 59 5f 44 4f 43 49 44 20 74 68 65  =QUERY_DOCID the
314b1 6e 20 64 6f 20 61 20 64 6f 63 69 64 20 6c 6f 6f  n do a docid loo
314b2 6b 75 70 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  kup for a single
314b3 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 74 68 65   entry.** in the
314b4 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65   %_content table
314b5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75  ..**.** If idxNu
314b6 6d 3e 3d 51 55 45 52 59 5f 46 55 4c 4c 54 45 58  m>=QUERY_FULLTEX
314b7 54 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 66  T then use the f
314b8 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 2e 20  ull text index. 
314b9 20 54 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6f   The.** column o
314ba 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 20  n the left-hand 
314bb 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43  side of the MATC
314bc 48 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63 6f  H operator is co
314bd 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20 69  lumn.** number i
314be 64 78 4e 75 6d 2d 51 55 45 52 59 5f 46 55 4c 4c  dxNum-QUERY_FULL
314bf 54 45 58 54 2c 20 30 20 69 6e 64 65 78 65 64 2e  TEXT, 0 indexed.
314c0 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 68 65    argv[0] is the
314c1 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73   right-hand.** s
314c2 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43 48  ide of the MATCH
314c3 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 2f 2a   operator..*/./*
314c4 20 54 4f 44 4f 28 73 68 65 73 73 29 20 55 70 67   TODO(shess) Upg
314c5 72 61 64 65 20 74 68 65 20 63 75 72 73 6f 72 20  rade the cursor 
314c6 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61  initialization a
314c7 6e 64 20 64 65 73 74 72 75 63 74 69 6f 6e 20 74  nd destruction t
314c8 6f 0a 2a 2a 20 61 63 63 6f 75 6e 74 20 66 6f 72  o.** account for
314c9 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72 28   fulltextFilter(
314ca 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 6d  ) being called m
314cb 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 6f 6e  ultiple times on
314cc 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 75 72   the.** same cur
314cd 73 6f 72 2e 20 20 54 68 65 20 63 75 72 72 65 6e  sor.  The curren
314ce 74 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 76 65  t solution is ve
314cf 72 79 20 66 72 61 67 69 6c 65 2e 20 20 41 70 70  ry fragile.  App
314d0 6c 79 20 66 69 78 20 74 6f 0a 2a 2a 20 66 74 73  ly fix to.** fts
314d1 33 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  3 as appropriate
314d2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
314d3 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72 28 0a  fulltextFilter(.
314d4 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63    sqlite3_vtab_c
314d5 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20  ursor *pCursor, 
314d6 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f      /* The curso
314d7 72 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20  r used for this 
314d8 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69  query */.  int i
314d9 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20 63 68 61  dxNum, const cha
314da 72 20 2a 69 64 78 53 74 72 2c 20 20 20 2f 2a 20  r *idxStr,   /* 
314db 57 68 69 63 68 20 69 6e 64 65 78 69 6e 67 20 73  Which indexing s
314dc 63 68 65 6d 65 20 74 6f 20 75 73 65 20 2a 2f 0a  cheme to use */.
314dd 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69    int argc, sqli
314de 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
314df 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73      /* Arguments
314e0 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 69 6e   for the indexin
314e1 67 20 73 63 68 65 6d 65 20 2a 2f 0a 29 7b 0a 20  g scheme */.){. 
314e2 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
314e3 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *c = (fulltext_
314e4 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
314e5 72 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  r;.  fulltext_vt
314e6 61 62 20 2a 76 20 3d 20 63 75 72 73 6f 72 5f 76  ab *v = cursor_v
314e7 74 61 62 28 63 29 3b 0a 20 20 69 6e 74 20 72 63  tab(c);.  int rc
314e8 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  ;..  FTSTRACE(("
314e9 46 54 53 33 20 46 69 6c 74 65 72 20 25 70 5c 6e  FTS3 Filter %p\n
314ea 22 2c 70 43 75 72 73 6f 72 29 29 3b 0a 0a 20 20  ",pCursor));..  
314eb 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  /* If the cursor
314ec 20 68 61 73 20 61 20 73 74 61 74 65 6d 65 6e 74   has a statement
314ed 20 74 68 61 74 20 77 61 73 20 6e 6f 74 20 70 72   that was not pr
314ee 65 70 61 72 65 64 20 61 63 63 6f 72 64 69 6e 67  epared according
314ef 20 74 6f 0a 20 20 2a 2a 20 69 64 78 4e 75 6d 2c   to.  ** idxNum,
314f0 20 63 6c 65 61 72 20 69 74 2e 20 20 49 20 62 65   clear it.  I be
314f1 6c 69 65 76 65 20 61 6c 6c 20 63 61 6c 6c 73 20  lieve all calls 
314f2 74 6f 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65  to fulltextFilte
314f3 72 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 67 69  r with a.  ** gi
314f4 76 65 6e 20 63 75 72 73 6f 72 20 77 69 6c 6c 20  ven cursor will 
314f5 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 69 64  have the same id
314f6 78 4e 75 6d 20 2c 20 62 75 74 20 69 6e 20 74 68  xNum , but in th
314f7 69 73 20 63 61 73 65 20 69 74 27 73 0a 20 20 2a  is case it's.  *
314f8 2a 20 65 61 73 79 20 74 6f 20 62 65 20 73 61 66  * easy to be saf
314f9 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 2d  e..  */.  if( c-
314fa 3e 70 53 74 6d 74 20 26 26 20 63 2d 3e 69 43 75  >pStmt && c->iCu
314fb 72 73 6f 72 54 79 70 65 21 3d 69 64 78 4e 75 6d  rsorType!=idxNum
314fc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
314fd 66 69 6e 61 6c 69 7a 65 28 63 2d 3e 70 53 74 6d  finalize(c->pStm
314fe 74 29 3b 0a 20 20 20 20 63 2d 3e 70 53 74 6d 74  t);.    c->pStmt
314ff 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20   = NULL;.  }..  
31500 2f 2a 20 47 65 74 20 61 20 66 72 65 73 68 20 73  /* Get a fresh s
31501 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72  tatement appropr
31502 69 61 74 65 20 74 6f 20 69 64 78 4e 75 6d 2e 20  iate to idxNum. 
31503 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  */.  /* TODO(she
31504 73 73 29 3a 20 41 64 64 20 61 20 70 72 65 70 61  ss): Add a prepa
31505 72 65 64 2d 73 74 61 74 65 6d 65 6e 74 20 63 61  red-statement ca
31506 63 68 65 20 69 6e 20 74 68 65 20 76 74 20 73 74  che in the vt st
31507 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68  ructure..  ** Th
31508 65 20 63 61 63 68 65 20 6d 75 73 74 20 68 61 6e  e cache must han
31509 64 6c 65 20 6d 75 6c 74 69 70 6c 65 20 6f 70 65  dle multiple ope
3150a 6e 20 63 75 72 73 6f 72 73 2e 20 20 45 61 73 69  n cursors.  Easi
3150b 65 72 20 74 6f 20 63 61 63 68 65 20 74 68 65 0a  er to cache the.
3150c 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 76    ** statement v
3150d 61 72 69 61 6e 74 73 20 61 74 20 74 68 65 20 76  ariants at the v
3150e 74 20 74 6f 20 72 65 64 75 63 65 20 6d 61 6c 6c  t to reduce mall
3150f 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20  oc/realloc/free 
31510 68 65 72 65 2e 0a 20 20 2a 2a 20 4f 72 20 77 65  here..  ** Or we
31511 20 63 6f 75 6c 64 20 68 61 76 65 20 61 20 53 74   could have a St
31512 72 69 6e 67 42 75 66 66 65 72 20 76 61 72 69 61  ringBuffer varia
31513 6e 74 20 77 68 69 63 68 20 61 6c 6c 6f 77 65 64  nt which allowed
31514 20 73 74 61 63 6b 0a 20 20 2a 2a 20 63 6f 6e 73   stack.  ** cons
31515 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 73 6d 61  truction for sma
31516 6c 6c 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a  ll values..  */.
31517 20 20 69 66 28 20 21 63 2d 3e 70 53 74 6d 74 20    if( !c->pStmt 
31518 29 7b 0a 20 20 20 20 53 74 72 69 6e 67 42 75 66  ){.    StringBuf
31519 66 65 72 20 73 62 3b 0a 20 20 20 20 69 6e 69 74  fer sb;.    init
3151a 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 62  StringBuffer(&sb
3151b 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73  );.    append(&s
3151c 62 2c 20 22 53 45 4c 45 43 54 20 64 6f 63 69 64  b, "SELECT docid
3151d 2c 20 22 29 3b 0a 20 20 20 20 61 70 70 65 6e 64  , ");.    append
3151e 4c 69 73 74 28 26 73 62 2c 20 76 2d 3e 6e 43 6f  List(&sb, v->nCo
3151f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65  lumn, v->azConte
31520 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 61  ntColumn);.    a
31521 70 70 65 6e 64 28 26 73 62 2c 20 22 20 46 52 4f  ppend(&sb, " FRO
31522 4d 20 25 5f 63 6f 6e 74 65 6e 74 22 29 3b 0a 20  M %_content");. 
31523 20 20 20 69 66 28 20 69 64 78 4e 75 6d 21 3d 51     if( idxNum!=Q
31524 55 45 52 59 5f 47 45 4e 45 52 49 43 20 29 20 61  UERY_GENERIC ) a
31525 70 70 65 6e 64 28 26 73 62 2c 20 22 20 57 48 45  ppend(&sb, " WHE
31526 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a  RE docid = ?");.
31527 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 70 72 65      rc = sql_pre
31528 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a  pare(v->db, v->z
31529 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20 26 63  Db, v->zName, &c
3152a 2d 3e 70 53 74 6d 74 2c 0a 20 20 20 20 20 20 20  ->pStmt,.       
3152b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
3152c 72 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26  ringBufferData(&
3152d 73 62 29 29 3b 0a 20 20 20 20 73 74 72 69 6e 67  sb));.    string
3152e 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 73  BufferDestroy(&s
3152f 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  b);.    if( rc!=
31530 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
31531 72 6e 20 72 63 3b 0a 20 20 20 20 63 2d 3e 69 43  rn rc;.    c->iC
31532 75 72 73 6f 72 54 79 70 65 20 3d 20 69 64 78 4e  ursorType = idxN
31533 75 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  um;.  }else{.   
31534 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 63   sqlite3_reset(c
31535 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 61 73  ->pStmt);.    as
31536 73 65 72 74 28 20 63 2d 3e 69 43 75 72 73 6f 72  sert( c->iCursor
31537 54 79 70 65 3d 3d 69 64 78 4e 75 6d 20 29 3b 0a  Type==idxNum );.
31538 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 69    }..  switch( i
31539 64 78 4e 75 6d 20 29 7b 0a 20 20 20 20 63 61 73  dxNum ){.    cas
3153a 65 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43 3a  e QUERY_GENERIC:
3153b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20  .      break;.. 
3153c 20 20 20 63 61 73 65 20 51 55 45 52 59 5f 44 4f     case QUERY_DO
3153d 43 49 44 3a 0a 20 20 20 20 20 20 72 63 20 3d 20  CID:.      rc = 
3153e 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
3153f 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 31 2c 20  64(c->pStmt, 1, 
31540 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
31541 74 36 34 28 61 72 67 76 5b 30 5d 29 29 3b 0a 20  t64(argv[0]));. 
31542 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
31543 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
31544 72 63 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  rc;.      break;
31545 0a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  ..    default:  
31546 20 2f 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73 65   /* full-text se
31547 61 72 63 68 20 2a 2f 0a 20 20 20 20 7b 0a 20 20  arch */.    {.  
31548 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 69      int iCol = i
31549 64 78 4e 75 6d 2d 51 55 45 52 59 5f 46 55 4c 4c  dxNum-QUERY_FULL
3154a 54 45 58 54 3b 0a 20 20 20 20 20 20 63 6f 6e 73  TEXT;.      cons
3154b 74 20 63 68 61 72 20 2a 7a 51 75 65 72 79 20 3d  t char *zQuery =
3154c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
3154d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3154e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20  t(argv[0]);.    
3154f 20 20 61 73 73 65 72 74 28 20 69 64 78 4e 75 6d    assert( idxNum
31550 3c 3d 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54  <=QUERY_FULLTEXT
31551 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  +v->nColumn);.  
31552 20 20 20 20 61 73 73 65 72 74 28 20 61 72 67 63      assert( argc
31553 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ==1 );.      if(
31554 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61 74 61   c->result.nData
31555 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  !=0 ){.        /
31556 2a 20 54 68 69 73 20 63 61 73 65 20 68 61 70 70  * This case happ
31557 65 6e 73 20 69 66 20 74 68 65 20 73 61 6d 65 20  ens if the same 
31558 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 72  cursor is used r
31559 65 70 65 61 74 65 64 6c 79 2e 20 2a 2f 0a 20 20  epeatedly. */.  
3155a 20 20 20 20 20 20 64 6c 72 44 65 73 74 72 6f 79        dlrDestroy
3155b 28 26 63 2d 3e 72 65 61 64 65 72 29 3b 0a 20 20  (&c->reader);.  
3155c 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
3155d 52 65 73 65 74 28 26 63 2d 3e 72 65 73 75 6c 74  Reset(&c->result
3155e 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
3155f 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
31560 65 72 49 6e 69 74 28 26 63 2d 3e 72 65 73 75 6c  erInit(&c->resul
31561 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  t, 0);.      }. 
31562 20 20 20 20 20 72 63 20 3d 20 66 75 6c 6c 74 65       rc = fullte
31563 78 74 51 75 65 72 79 28 76 2c 20 69 43 6f 6c 2c  xtQuery(v, iCol,
31564 20 7a 51 75 65 72 79 2c 20 2d 31 2c 20 26 63 2d   zQuery, -1, &c-
31565 3e 72 65 73 75 6c 74 2c 20 26 63 2d 3e 70 45 78  >result, &c->pEx
31566 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  pr);.      if( r
31567 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
31568 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
31569 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44  if( c->result.nD
3156a 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ata!=0 ){.      
3156b 20 20 64 6c 72 49 6e 69 74 28 26 63 2d 3e 72 65    dlrInit(&c->re
3156c 61 64 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c  ader, DL_DOCIDS,
3156d 20 63 2d 3e 72 65 73 75 6c 74 2e 70 44 61 74 61   c->result.pData
3156e 2c 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61 74  , c->result.nDat
3156f 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  a);.      }.    
31570 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
31571 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 66 75 6c   }..  return ful
31572 6c 74 65 78 74 4e 65 78 74 28 70 43 75 72 73 6f  ltextNext(pCurso
31573 72 29 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 69  r);.}../* This i
31574 73 20 74 68 65 20 78 45 6f 66 20 6d 65 74 68 6f  s the xEof metho
31575 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  d of the virtual
31576 20 74 61 62 6c 65 2e 20 20 54 68 65 20 53 51 4c   table.  The SQL
31577 69 74 65 20 63 6f 72 65 0a 2a 2a 20 63 61 6c 6c  ite core.** call
31578 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74  s this routine t
31579 6f 20 66 69 6e 64 20 6f 75 74 20 69 66 20 69 74  o find out if it
3157a 20 68 61 73 20 72 65 61 63 68 65 64 20 74 68 65   has reached the
3157b 20 65 6e 64 20 6f 66 0a 2a 2a 20 61 20 71 75 65   end of.** a que
3157c 72 79 27 73 20 72 65 73 75 6c 74 73 20 73 65 74  ry's results set
3157d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3157e 66 75 6c 6c 74 65 78 74 45 6f 66 28 73 71 6c 69  fulltextEof(sqli
3157f 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
31580 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c  *pCursor){.  ful
31581 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20  ltext_cursor *c 
31582 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  = (fulltext_curs
31583 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20  or *) pCursor;. 
31584 20 72 65 74 75 72 6e 20 63 2d 3e 65 6f 66 3b 0a   return c->eof;.
31585 7d 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20 74 68  }../* This is th
31586 65 20 78 43 6f 6c 75 6d 6e 20 6d 65 74 68 6f 64  e xColumn method
31587 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
31588 74 61 62 6c 65 2e 20 20 54 68 65 20 53 51 4c 69  table.  The SQLi
31589 74 65 0a 2a 2a 20 63 6f 72 65 20 63 61 6c 6c 73  te.** core calls
3158a 20 74 68 69 73 20 6d 65 74 68 6f 64 20 64 75 72   this method dur
3158b 69 6e 67 20 61 20 71 75 65 72 79 20 77 68 65 6e  ing a query when
3158c 20 69 74 20 6e 65 65 64 73 20 74 68 65 20 76 61   it needs the va
3158d 6c 75 65 0a 2a 2a 20 6f 66 20 61 20 63 6f 6c 75  lue.** of a colu
3158e 6d 6e 20 66 72 6f 6d 20 74 68 65 20 76 69 72 74  mn from the virt
3158f 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ual table.  This
31590 20 6d 65 74 68 6f 64 20 6e 65 65 64 73 20 74 6f   method needs to
31591 20 75 73 65 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74   use.** one of t
31592 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
31593 74 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 74  t_*() routines t
31594 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 71 75  o store the requ
31595 65 73 74 65 64 0a 2a 2a 20 76 61 6c 75 65 20 62  ested.** value b
31596 61 63 6b 20 69 6e 20 74 68 65 20 70 43 6f 6e 74  ack in the pCont
31597 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ext..*/.static i
31598 6e 74 20 66 75 6c 6c 74 65 78 74 43 6f 6c 75 6d  nt fulltextColum
31599 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  n(sqlite3_vtab_c
3159a 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 0a  ursor *pCursor,.
3159b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3159c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3159d 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  3_context *pCont
3159e 65 78 74 2c 20 69 6e 74 20 69 64 78 43 6f 6c 29  ext, int idxCol)
3159f 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  {.  fulltext_cur
315a0 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65  sor *c = (fullte
315a1 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75  xt_cursor *) pCu
315a2 72 73 6f 72 3b 0a 20 20 66 75 6c 6c 74 65 78 74  rsor;.  fulltext
315a3 5f 76 74 61 62 20 2a 76 20 3d 20 63 75 72 73 6f  _vtab *v = curso
315a4 72 5f 76 74 61 62 28 63 29 3b 0a 0a 20 20 69 66  r_vtab(c);..  if
315a5 28 20 69 64 78 43 6f 6c 3c 76 2d 3e 6e 43 6f 6c  ( idxCol<v->nCol
315a6 75 6d 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  umn ){.    sqlit
315a7 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d  e3_value *pVal =
315a8 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
315a9 76 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c 20  value(c->pStmt, 
315aa 69 64 78 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 73  idxCol+1);.    s
315ab 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
315ac 6c 75 65 28 70 43 6f 6e 74 65 78 74 2c 20 70 56  lue(pContext, pV
315ad 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  al);.  }else if(
315ae 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f 6c   idxCol==v->nCol
315af 75 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  umn ){.    /* Th
315b0 65 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 20 77  e extra column w
315b1 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 74 68 65  hose name is the
315b2 20 73 61 6d 65 20 61 73 20 74 68 65 20 74 61 62   same as the tab
315b3 6c 65 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75 72  le..    ** Retur
315b4 6e 20 61 20 62 6c 6f 62 20 77 68 69 63 68 20 69  n a blob which i
315b5 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
315b6 68 65 20 63 75 72 73 6f 72 0a 20 20 20 20 2a 2f  he cursor.    */
315b7 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
315b8 75 6c 74 5f 62 6c 6f 62 28 70 43 6f 6e 74 65 78  ult_blob(pContex
315b9 74 2c 20 26 63 2c 20 73 69 7a 65 6f 66 28 63 29  t, &c, sizeof(c)
315ba 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
315bb 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  NT);.  }else if(
315bc 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f 6c   idxCol==v->nCol
315bd 75 6d 6e 2b 31 20 29 7b 0a 20 20 20 20 2f 2a 20  umn+1 ){.    /* 
315be 54 68 65 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e  The docid column
315bf 2c 20 77 68 69 63 68 20 69 73 20 61 6e 20 61 6c  , which is an al
315c0 69 61 73 20 66 6f 72 20 72 6f 77 69 64 2e 20 2a  ias for rowid. *
315c1 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  /.    sqlite3_va
315c2 6c 75 65 20 2a 70 56 61 6c 20 3d 20 73 71 6c 69  lue *pVal = sqli
315c3 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65  te3_column_value
315c4 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20  (c->pStmt, 0);. 
315c5 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
315c6 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65 78 74  t_value(pContext
315c7 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 0a 20 20 72  , pVal);.  }.  r
315c8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
315c9 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20 74  .}../* This is t
315ca 68 65 20 78 52 6f 77 69 64 20 6d 65 74 68 6f 64  he xRowid method
315cb 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  .  The SQLite co
315cc 72 65 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  re calls this ro
315cd 75 74 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 74 72  utine to.** retr
315ce 69 65 76 65 20 74 68 65 20 72 6f 77 69 64 20 66  ieve the rowid f
315cf 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
315d0 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ow of the result
315d1 20 73 65 74 2e 20 20 66 74 73 33 0a 2a 2a 20 65   set.  fts3.** e
315d2 78 70 6f 73 65 73 20 25 5f 63 6f 6e 74 65 6e 74  xposes %_content
315d3 2e 64 6f 63 69 64 20 61 73 20 74 68 65 20 72 6f  .docid as the ro
315d4 77 69 64 20 66 6f 72 20 74 68 65 20 76 69 72 74  wid for the virt
315d5 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 0a  ual table.  The.
315d6 2a 2a 20 72 6f 77 69 64 20 73 68 6f 75 6c 64 20  ** rowid should 
315d7 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70  be written to *p
315d8 52 6f 77 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  Rowid..*/.static
315d9 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 52 6f 77   int fulltextRow
315da 69 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  id(sqlite3_vtab_
315db 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
315dc 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
315dd 52 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c 74 65  Rowid){.  fullte
315de 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  xt_cursor *c = (
315df 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
315e0 2a 29 20 70 43 75 72 73 6f 72 3b 0a 0a 20 20 2a  *) pCursor;..  *
315e1 70 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  pRowid = sqlite3
315e2 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 63 2d  _column_int64(c-
315e3 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 72 65  >pStmt, 0);.  re
315e4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
315e5 7d 0a 0a 2f 2a 20 41 64 64 20 61 6c 6c 20 74 65  }../* Add all te
315e6 72 6d 73 20 69 6e 20 5b 7a 54 65 78 74 5d 20 74  rms in [zText] t
315e7 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 74  o pendingTerms t
315e8 61 62 6c 65 2e 20 20 49 66 20 5b 69 43 6f 6c 75  able.  If [iColu
315e9 6d 6e 5d 20 3e 20 30 2c 0a 2a 2a 20 77 65 20 61  mn] > 0,.** we a
315ea 6c 73 6f 20 73 74 6f 72 65 20 70 6f 73 69 74 69  lso store positi
315eb 6f 6e 73 20 61 6e 64 20 6f 66 66 73 65 74 73 20  ons and offsets 
315ec 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
315ed 65 20 75 73 69 6e 67 20 74 68 61 74 0a 2a 2a 20  e using that.** 
315ee 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a  column number..*
315ef 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 75 69  /.static int bui
315f0 6c 64 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ldTerms(fulltext
315f1 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65  _vtab *v, sqlite
315f2 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a 20  _int64 iDocid,. 
315f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
315f4 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
315f5 2a 7a 54 65 78 74 2c 20 69 6e 74 20 69 43 6f 6c  *zText, int iCol
315f6 75 6d 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  umn){.  sqlite3_
315f7 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
315f8 6e 69 7a 65 72 20 3d 20 76 2d 3e 70 54 6f 6b 65  nizer = v->pToke
315f9 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33  nizer;.  sqlite3
315fa 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
315fb 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 63 6f  r *pCursor;.  co
315fc 6e 73 74 20 63 68 61 72 20 2a 70 54 6f 6b 65 6e  nst char *pToken
315fd 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 42 79  ;.  int nTokenBy
315fe 74 65 73 3b 0a 20 20 69 6e 74 20 69 53 74 61 72  tes;.  int iStar
315ff 74 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f 66 66  tOffset, iEndOff
31600 73 65 74 2c 20 69 50 6f 73 69 74 69 6f 6e 3b 0a  set, iPosition;.
31601 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20    int rc;..  rc 
31602 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d  = pTokenizer->pM
31603 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f  odule->xOpen(pTo
31604 6b 65 6e 69 7a 65 72 2c 20 7a 54 65 78 74 2c 20  kenizer, zText, 
31605 2d 31 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a 20  -1, &pCursor);. 
31606 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
31607 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
31608 0a 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b  .  pCursor->pTok
31609 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69  enizer = pTokeni
3160a 7a 65 72 3b 0a 20 20 77 68 69 6c 65 28 20 53 51  zer;.  while( SQ
3160b 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 3d 70 54 6f  LITE_OK==(rc=pTo
3160c 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
3160d 2d 3e 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c  ->xNext(pCursor,
3160e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3160f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31611 20 20 20 20 26 70 54 6f 6b 65 6e 2c 20 26 6e 54      &pToken, &nT
31612 6f 6b 65 6e 42 79 74 65 73 2c 0a 20 20 20 20 20  okenBytes,.     
31613 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31614 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31615 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69                &i
31616 53 74 61 72 74 4f 66 66 73 65 74 2c 20 26 69 45  StartOffset, &iE
31617 6e 64 4f 66 66 73 65 74 2c 0a 20 20 20 20 20 20  ndOffset,.      
31618 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31619 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3161a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69 50               &iP
3161b 6f 73 69 74 69 6f 6e 29 29 20 29 7b 0a 20 20 20  osition)) ){.   
3161c 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 3b   DLCollector *p;
3161d 0a 20 20 20 20 69 6e 74 20 6e 44 61 74 61 3b 20  .    int nData; 
3161e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3161f 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f 63    /* Size of doc
31620 6c 69 73 74 20 62 65 66 6f 72 65 20 6f 75 72 20  list before our 
31621 75 70 64 61 74 65 2e 20 2a 2f 0a 0a 20 20 20 20  update. */..    
31622 2f 2a 20 50 6f 73 69 74 69 6f 6e 73 20 63 61 6e  /* Positions can
31623 27 74 20 62 65 20 6e 65 67 61 74 69 76 65 3b 20  't be negative; 
31624 77 65 20 75 73 65 20 2d 31 20 61 73 20 61 20 74  we use -1 as a t
31625 65 72 6d 69 6e 61 74 6f 72 0a 20 20 20 20 20 2a  erminator.     *
31626 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 20 54 6f   internally.  To
31627 6b 65 6e 20 63 61 6e 27 74 20 62 65 20 4e 55 4c  ken can't be NUL
31628 4c 20 6f 72 20 65 6d 70 74 79 2e 20 2a 2f 0a 20  L or empty. */. 
31629 20 20 20 69 66 28 20 69 50 6f 73 69 74 69 6f 6e     if( iPosition
3162a 3c 30 20 7c 7c 20 70 54 6f 6b 65 6e 20 3d 3d 20  <0 || pToken == 
3162b 4e 55 4c 4c 20 7c 7c 20 6e 54 6f 6b 65 6e 42 79  NULL || nTokenBy
3162c 74 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  tes == 0 ){.    
3162d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
3162e 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ROR;.      break
3162f 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 20 3d  ;.    }..    p =
31630 20 66 74 73 33 48 61 73 68 46 69 6e 64 28 26 76   fts3HashFind(&v
31631 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20  ->pendingTerms, 
31632 70 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 42 79  pToken, nTokenBy
31633 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 70 3d  tes);.    if( p=
31634 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 6e  =NULL ){.      n
31635 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 20 20  Data = 0;.      
31636 70 20 3d 20 64 6c 63 4e 65 77 28 69 44 6f 63 69  p = dlcNew(iDoci
31637 64 2c 20 44 4c 5f 44 45 46 41 55 4c 54 29 3b 0a  d, DL_DEFAULT);.
31638 20 20 20 20 20 20 66 74 73 33 48 61 73 68 49 6e        fts3HashIn
31639 73 65 72 74 28 26 76 2d 3e 70 65 6e 64 69 6e 67  sert(&v->pending
3163a 54 65 72 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20 6e  Terms, pToken, n
3163b 54 6f 6b 65 6e 42 79 74 65 73 2c 20 70 29 3b 0a  TokenBytes, p);.
3163c 0a 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 68 65  .      /* Overhe
3163d 61 64 20 66 6f 72 20 6f 75 72 20 68 61 73 68 20  ad for our hash 
3163e 74 61 62 6c 65 20 65 6e 74 72 79 2c 20 74 68 65  table entry, the
3163f 20 6b 65 79 2c 20 61 6e 64 20 74 68 65 20 76 61   key, and the va
31640 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 20 20 76 2d  lue. */.      v-
31641 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 2b 3d  >nPendingData +=
31642 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 66   sizeof(struct f
31643 74 73 33 48 61 73 68 45 6c 65 6d 29 2b 73 69 7a  ts3HashElem)+siz
31644 65 6f 66 28 2a 70 29 2b 6e 54 6f 6b 65 6e 42 79  eof(*p)+nTokenBy
31645 74 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  tes;.    }else{.
31646 20 20 20 20 20 20 6e 44 61 74 61 20 3d 20 70 2d        nData = p-
31647 3e 62 2e 6e 44 61 74 61 3b 0a 20 20 20 20 20 20  >b.nData;.      
31648 69 66 28 20 70 2d 3e 64 6c 77 2e 69 50 72 65 76  if( p->dlw.iPrev
31649 44 6f 63 69 64 21 3d 69 44 6f 63 69 64 20 29 20  Docid!=iDocid ) 
3164a 64 6c 63 4e 65 78 74 28 70 2c 20 69 44 6f 63 69  dlcNext(p, iDoci
3164b 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  d);.    }.    if
3164c 28 20 69 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a  ( iColumn>=0 ){.
3164d 20 20 20 20 20 20 64 6c 63 41 64 64 50 6f 73 28        dlcAddPos(
3164e 70 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73  p, iColumn, iPos
3164f 69 74 69 6f 6e 2c 20 69 53 74 61 72 74 4f 66 66  ition, iStartOff
31650 73 65 74 2c 20 69 45 6e 64 4f 66 66 73 65 74 29  set, iEndOffset)
31651 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
31652 41 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61 20  Accumulate data 
31653 61 64 64 65 64 20 62 79 20 64 6c 63 4e 65 77 20  added by dlcNew 
31654 6f 72 20 64 6c 63 4e 65 78 74 2c 20 61 6e 64 20  or dlcNext, and 
31655 64 6c 63 41 64 64 50 6f 73 2e 20 2a 2f 0a 20 20  dlcAddPos. */.  
31656 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74    v->nPendingDat
31657 61 20 2b 3d 20 70 2d 3e 62 2e 6e 44 61 74 61 2d  a += p->b.nData-
31658 6e 44 61 74 61 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  nData;.  }..  /*
31659 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 68 65   TODO(shess) Che
3165a 63 6b 20 72 65 74 75 72 6e 3f 20 20 53 68 6f 75  ck return?  Shou
3165b 6c 64 20 74 68 69 73 20 62 65 20 61 62 6c 65 20  ld this be able 
3165c 74 6f 20 63 61 75 73 65 20 65 72 72 6f 72 73 20  to cause errors 
3165d 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 70 6f 69  at.  ** this poi
3165e 6e 74 3f 20 20 41 63 74 75 61 6c 6c 79 2c 20 73  nt?  Actually, s
3165f 61 6d 65 20 71 75 65 73 74 69 6f 6e 20 61 62 6f  ame question abo
31660 75 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  ut sqlite3_final
31661 69 7a 65 28 29 2c 0a 20 20 2a 2a 20 74 68 6f 75  ize(),.  ** thou
31662 67 68 20 6f 6e 65 20 63 6f 75 6c 64 20 61 72 67  gh one could arg
31663 75 65 20 74 68 61 74 20 66 61 69 6c 75 72 65 20  ue that failure 
31664 74 68 65 72 65 20 6d 65 61 6e 73 20 74 68 61 74  there means that
31665 20 74 68 65 20 64 61 74 61 20 69 73 0a 20 20 2a   the data is.  *
31666 2a 20 6e 6f 74 20 64 75 72 61 62 6c 65 2e 20 20  * not durable.  
31667 2a 70 6f 6e 64 65 72 2a 0a 20 20 2a 2f 0a 20 20  *ponder*.  */.  
31668 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64  pTokenizer->pMod
31669 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75 72  ule->xClose(pCur
3166a 73 6f 72 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  sor);.  if( SQLI
3166b 54 45 5f 44 4f 4e 45 20 3d 3d 20 72 63 20 29 20  TE_DONE == rc ) 
3166c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3166d 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
3166e 0a 0a 2f 2a 20 41 64 64 20 64 6f 63 6c 69 73 74  ../* Add doclist
3166f 73 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20  s for all terms 
31670 69 6e 20 5b 70 56 61 6c 75 65 73 5d 20 74 6f 20  in [pValues] to 
31671 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 74 61 62  pendingTerms tab
31672 6c 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le. */.static in
31673 74 20 69 6e 73 65 72 74 54 65 72 6d 73 28 66 75  t insertTerms(fu
31674 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
31675 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
31676 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cid,.           
31677 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
31678 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61 6c  te3_value **pVal
31679 75 65 73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ues){.  int i;. 
3167a 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 20 3c 20   for(i = 0; i < 
3167b 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 3b 20 2b 2b 69  v->nColumn ; ++i
3167c 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65  ){.    char *zTe
3167d 78 74 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69  xt = (char*)sqli
3167e 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70  te3_value_text(p
3167f 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20  Values[i]);.    
31680 69 6e 74 20 72 63 20 3d 20 62 75 69 6c 64 54 65  int rc = buildTe
31681 72 6d 73 28 76 2c 20 69 44 6f 63 69 64 2c 20 7a  rms(v, iDocid, z
31682 54 65 78 74 2c 20 69 29 3b 0a 20 20 20 20 69 66  Text, i);.    if
31683 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
31684 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
31685 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
31686 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 65  _OK;.}../* Add e
31687 6d 70 74 79 20 64 6f 63 6c 69 73 74 73 20 66 6f  mpty doclists fo
31688 72 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 74  r all terms in t
31689 68 65 20 67 69 76 65 6e 20 72 6f 77 27 73 20 63  he given row's c
3168a 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 70 65 6e  ontent to.** pen
3168b 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74  dingTerms..*/.st
3168c 61 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65 54  atic int deleteT
3168d 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74  erms(fulltext_vt
3168e 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e  ab *v, sqlite_in
3168f 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 63  t64 iDocid){.  c
31690 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 56 61 6c  onst char **pVal
31691 75 65 73 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63  ues;.  int i, rc
31692 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  ;..  /* TODO(she
31693 73 73 29 20 53 68 6f 75 6c 64 20 77 65 20 61 6c  ss) Should we al
31694 6c 6f 77 20 73 75 63 68 20 74 61 62 6c 65 73 20  low such tables 
31695 61 74 20 61 6c 6c 3f 20 2a 2f 0a 20 20 69 66 28  at all? */.  if(
31696 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f   DL_DEFAULT==DL_
31697 44 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e 20  DOCIDS ) return 
31698 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20  SQLITE_ERROR;.. 
31699 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 73 65   rc = content_se
3169a 6c 65 63 74 28 76 2c 20 69 44 6f 63 69 64 2c 20  lect(v, iDocid, 
3169b 26 70 56 61 6c 75 65 73 29 3b 0a 20 20 69 66 28  &pValues);.  if(
3169c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3169d 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 66   return rc;..  f
3169e 6f 72 28 69 20 3d 20 30 20 3b 20 69 20 3c 20 76  or(i = 0 ; i < v
3169f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 20  ->nColumn; ++i) 
316a0 7b 0a 20 20 20 20 72 63 20 3d 20 62 75 69 6c 64  {.    rc = build
316a1 54 65 72 6d 73 28 76 2c 20 69 44 6f 63 69 64 2c  Terms(v, iDocid,
316a2 20 70 56 61 6c 75 65 73 5b 69 5d 2c 20 2d 31 29   pValues[i], -1)
316a3 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
316a4 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b  LITE_OK ) break;
316a5 0a 20 20 7d 0a 0a 20 20 66 72 65 65 53 74 72 69  .  }..  freeStri
316a6 6e 67 41 72 72 61 79 28 76 2d 3e 6e 43 6f 6c 75  ngArray(v->nColu
316a7 6d 6e 2c 20 70 56 61 6c 75 65 73 29 3b 0a 20 20  mn, pValues);.  
316a8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
316a9 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ;.}../* TODO(she
316aa 73 73 29 20 52 65 66 61 63 74 6f 72 20 74 68 65  ss) Refactor the
316ab 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20   code to remove 
316ac 74 68 69 73 20 66 6f 72 77 61 72 64 20 64 65 63  this forward dec
316ad 6c 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l. */.static int
316ae 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d   initPendingTerm
316af 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
316b0 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
316b1 20 69 44 6f 63 69 64 29 3b 0a 0a 2f 2a 20 49 6e   iDocid);../* In
316b2 73 65 72 74 20 61 20 72 6f 77 20 69 6e 74 6f 20  sert a row into 
316b3 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61  the %_content ta
316b4 62 6c 65 3b 20 73 65 74 20 2a 70 69 44 6f 63 69  ble; set *piDoci
316b5 64 20 74 6f 20 62 65 20 74 68 65 20 49 44 20 6f  d to be the ID o
316b6 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 72 6f 77  f the.** new row
316b7 2e 20 20 41 64 64 20 64 6f 63 6c 69 73 74 73 20  .  Add doclists 
316b8 66 6f 72 20 74 65 72 6d 73 20 74 6f 20 70 65 6e  for terms to pen
316b9 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74  dingTerms..*/.st
316ba 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f 69  atic int index_i
316bb 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74 5f 76  nsert(fulltext_v
316bc 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f  tab *v, sqlite3_
316bd 76 61 6c 75 65 20 2a 70 52 65 71 75 65 73 74 44  value *pRequestD
316be 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ocid,.          
316bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
316c0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56  lite3_value **pV
316c1 61 6c 75 65 73 2c 20 73 71 6c 69 74 65 5f 69 6e  alues, sqlite_in
316c2 74 36 34 20 2a 70 69 44 6f 63 69 64 29 7b 0a 20  t64 *piDocid){. 
316c3 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d   int rc;..  rc =
316c4 20 63 6f 6e 74 65 6e 74 5f 69 6e 73 65 72 74 28   content_insert(
316c5 76 2c 20 70 52 65 71 75 65 73 74 44 6f 63 69 64  v, pRequestDocid
316c6 2c 20 70 56 61 6c 75 65 73 29 3b 20 20 2f 2a 20  , pValues);  /* 
316c7 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20 49  execute an SQL I
316c8 4e 53 45 52 54 20 2a 2f 0a 20 20 69 66 28 20 72  NSERT */.  if( r
316c9 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
316ca 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
316cb 64 6f 63 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20  docid column is 
316cc 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f 77  an alias for row
316cd 69 64 2e 20 2a 2f 0a 20 20 2a 70 69 44 6f 63 69  id. */.  *piDoci
316ce 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  d = sqlite3_last
316cf 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 76 2d  _insert_rowid(v-
316d0 3e 64 62 29 3b 0a 20 20 72 63 20 3d 20 69 6e 69  >db);.  rc = ini
316d1 74 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 2c  tPendingTerms(v,
316d2 20 2a 70 69 44 6f 63 69 64 29 3b 0a 20 20 69 66   *piDocid);.  if
316d3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
316d4 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
316d5 72 65 74 75 72 6e 20 69 6e 73 65 72 74 54 65 72  return insertTer
316d6 6d 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 2c 20  ms(v, *piDocid, 
316d7 70 56 61 6c 75 65 73 29 3b 0a 7d 0a 0a 2f 2a 20  pValues);.}../* 
316d8 44 65 6c 65 74 65 20 61 20 72 6f 77 20 66 72 6f  Delete a row fro
316d9 6d 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20  m the %_content 
316da 74 61 62 6c 65 3b 20 61 64 64 20 65 6d 70 74 79  table; add empty
316db 20 64 6f 63 6c 69 73 74 73 20 66 6f 72 20 74 65   doclists for te
316dc 72 6d 73 0a 2a 2a 20 74 6f 20 70 65 6e 64 69 6e  rms.** to pendin
316dd 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69  gTerms..*/.stati
316de 63 20 69 6e 74 20 69 6e 64 65 78 5f 64 65 6c 65  c int index_dele
316df 74 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  te(fulltext_vtab
316e0 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36   *v, sqlite_int6
316e1 34 20 69 52 6f 77 29 7b 0a 20 20 69 6e 74 20 72  4 iRow){.  int r
316e2 63 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54  c = initPendingT
316e3 65 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20  erms(v, iRow);. 
316e4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
316e5 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
316e6 0a 20 20 72 63 20 3d 20 64 65 6c 65 74 65 54 65  .  rc = deleteTe
316e7 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20  rms(v, iRow);.  
316e8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
316e9 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
316ea 20 20 72 65 74 75 72 6e 20 63 6f 6e 74 65 6e 74    return content
316eb 5f 64 65 6c 65 74 65 28 76 2c 20 69 52 6f 77 29  _delete(v, iRow)
316ec 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61 6e  ;  /* execute an
316ed 20 53 51 4c 20 44 45 4c 45 54 45 20 2a 2f 0a 7d   SQL DELETE */.}
316ee 0a 0a 2f 2a 20 55 70 64 61 74 65 20 61 20 72 6f  ../* Update a ro
316ef 77 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65  w in the %_conte
316f0 6e 74 20 74 61 62 6c 65 3b 20 61 64 64 20 64 65  nt table; add de
316f1 6c 65 74 65 20 64 6f 63 6c 69 73 74 73 20 74 6f  lete doclists to
316f2 0a 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73  .** pendingTerms
316f3 20 66 6f 72 20 6f 6c 64 20 74 65 72 6d 73 20 6e   for old terms n
316f4 6f 74 20 69 6e 20 74 68 65 20 6e 65 77 20 64 61  ot in the new da
316f5 74 61 2c 20 61 64 64 20 69 6e 73 65 72 74 20 64  ta, add insert d
316f6 6f 63 6c 69 73 74 73 0a 2a 2a 20 74 6f 20 70 65  oclists.** to pe
316f7 6e 64 69 6e 67 54 65 72 6d 73 20 66 6f 72 20 74  ndingTerms for t
316f8 65 72 6d 73 20 69 6e 20 74 68 65 20 6e 65 77 20  erms in the new 
316f9 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  data..*/.static 
316fa 69 6e 74 20 69 6e 64 65 78 5f 75 70 64 61 74 65  int index_update
316fb 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
316fc 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  v, sqlite_int64 
316fd 69 52 6f 77 2c 0a 20 20 20 20 20 20 20 20 20 20  iRow,.          
316fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
316ff 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56  lite3_value **pV
31700 61 6c 75 65 73 29 7b 0a 20 20 69 6e 74 20 72 63  alues){.  int rc
31701 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65   = initPendingTe
31702 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20  rms(v, iRow);.  
31703 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
31704 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
31705 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e    /* Generate an
31706 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 20 66   empty doclist f
31707 6f 72 20 65 61 63 68 20 74 65 72 6d 20 74 68 61  or each term tha
31708 74 20 70 72 65 76 69 6f 75 73 6c 79 20 61 70 70  t previously app
31709 65 61 72 65 64 20 69 6e 20 74 68 69 73 0a 20 20  eared in this.  
3170a 20 2a 20 72 6f 77 2e 20 2a 2f 0a 20 20 72 63 20   * row. */.  rc 
3170b 3d 20 64 65 6c 65 74 65 54 65 72 6d 73 28 76 2c  = deleteTerms(v,
3170c 20 69 52 6f 77 29 3b 0a 20 20 69 66 28 20 72 63   iRow);.  if( rc
3170d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
3170e 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
3170f 20 63 6f 6e 74 65 6e 74 5f 75 70 64 61 74 65 28   content_update(
31710 76 2c 20 70 56 61 6c 75 65 73 2c 20 69 52 6f 77  v, pValues, iRow
31711 29 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61  );  /* execute a
31712 6e 20 53 51 4c 20 55 50 44 41 54 45 20 2a 2f 0a  n SQL UPDATE */.
31713 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
31714 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
31715 0a 0a 20 20 2f 2a 20 4e 6f 77 20 61 64 64 20 70  ..  /* Now add p
31716 6f 73 69 74 69 6f 6e 73 20 66 6f 72 20 74 65 72  ositions for ter
31717 6d 73 20 77 68 69 63 68 20 61 70 70 65 61 72 20  ms which appear 
31718 69 6e 20 74 68 65 20 75 70 64 61 74 65 64 20 72  in the updated r
31719 6f 77 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ow. */.  return 
3171a 69 6e 73 65 72 74 54 65 72 6d 73 28 76 2c 20 69  insertTerms(v, i
3171b 52 6f 77 2c 20 70 56 61 6c 75 65 73 29 3b 0a 7d  Row, pValues);.}
3171c 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
3171d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3171e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3171f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31720 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e 74 65 72  ******/./* Inter
31721 69 6f 72 57 72 69 74 65 72 20 69 73 20 75 73 65  iorWriter is use
31722 64 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 65 72  d to collect ter
31723 6d 73 20 61 6e 64 20 62 6c 6f 63 6b 20 72 65 66  ms and block ref
31724 65 72 65 6e 63 65 73 20 69 6e 74 6f 0a 2a 2a 20  erences into.** 
31725 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 69  interior nodes i
31726 6e 20 25 5f 73 65 67 6d 65 6e 74 73 2e 20 20 53  n %_segments.  S
31727 65 65 20 63 6f 6d 6d 65 6e 74 61 72 79 20 61 74  ee commentary at
31728 20 74 6f 70 20 6f 66 20 66 69 6c 65 20 66 6f 72   top of file for
31729 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2f 0a 0a  .** format..*/..
3172a 2f 2a 20 48 6f 77 20 6c 61 72 67 65 20 69 6e 74  /* How large int
3172b 65 72 69 6f 72 20 6e 6f 64 65 73 20 63 61 6e 20  erior nodes can 
3172c 67 72 6f 77 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  grow. */.#define
3172d 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 32 30   INTERIOR_MAX 20
3172e 34 38 0a 0a 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6e  48../* Minimum n
3172f 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 70  umber of terms p
31730 65 72 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  er interior node
31731 20 28 65 78 63 65 70 74 20 74 68 65 20 72 6f 6f   (except the roo
31732 74 29 2e 20 54 68 69 73 0a 2a 2a 20 70 72 65 76  t). This.** prev
31733 65 6e 74 73 20 6c 61 72 67 65 20 74 65 72 6d 73  ents large terms
31734 20 66 72 6f 6d 20 6d 61 6b 69 6e 67 20 74 68 65   from making the
31735 20 74 72 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79   tree too skinny
31736 20 2d 20 6d 75 73 74 20 62 65 20 3e 30 0a 2a 2a   - must be >0.**
31737 20 73 6f 20 74 68 61 74 20 74 68 65 20 74 72 65   so that the tre
31738 65 20 61 6c 77 61 79 73 20 6d 61 6b 65 73 20 70  e always makes p
31739 72 6f 67 72 65 73 73 2e 20 20 4e 6f 74 65 20 74  rogress.  Note t
3173a 68 61 74 20 74 68 65 20 6d 69 6e 20 74 72 65 65  hat the min tree
3173b 0a 2a 2a 20 66 61 6e 6f 75 74 20 77 69 6c 6c 20  .** fanout will 
3173c 62 65 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f  be INTERIOR_MIN_
3173d 54 45 52 4d 53 2b 31 2e 0a 2a 2f 0a 23 64 65 66  TERMS+1..*/.#def
3173e 69 6e 65 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e  ine INTERIOR_MIN
3173f 5f 54 45 52 4d 53 20 37 0a 23 69 66 20 49 4e 54  _TERMS 7.#if INT
31740 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 3c  ERIOR_MIN_TERMS<
31741 31 0a 23 20 65 72 72 6f 72 20 49 4e 54 45 52 49  1.# error INTERI
31742 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 6d 75 73  OR_MIN_TERMS mus
31743 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61  t be greater tha
31744 6e 20 30 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  n 0..#endif../* 
31745 52 4f 4f 54 5f 4d 41 58 20 63 6f 6e 74 72 6f 6c  ROOT_MAX control
31746 73 20 68 6f 77 20 6d 75 63 68 20 64 61 74 61 20  s how much data 
31747 69 73 20 73 74 6f 72 65 64 20 69 6e 6c 69 6e 65  is stored inline
31748 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 0a   in the segment.
31749 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2f  ** directory..*/
3174a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
3174b 50 75 73 68 20 52 4f 4f 54 5f 4d 41 58 20 64 6f  Push ROOT_MAX do
3174c 77 6e 20 74 6f 20 77 68 6f 65 76 65 72 20 69 73  wn to whoever is
3174d 20 77 72 69 74 69 6e 67 20 74 68 69 6e 67 73 2e   writing things.
3174e 20 20 49 74 27 73 0a 2a 2a 20 6f 6e 6c 79 20 68    It's.** only h
3174f 65 72 65 20 73 6f 20 74 68 61 74 20 69 6e 74 65  ere so that inte
31750 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e  riorWriterRootIn
31751 66 6f 28 29 20 61 6e 64 20 6c 65 61 66 57 72 69  fo() and leafWri
31752 74 65 72 52 6f 6f 74 49 6e 66 6f 28 29 0a 2a 2a  terRootInfo().**
31753 20 63 61 6e 20 62 6f 74 68 20 73 65 65 20 69 74   can both see it
31754 2c 20 62 75 74 20 69 66 20 74 68 65 20 63 61 6c  , but if the cal
31755 6c 65 72 20 70 61 73 73 65 64 20 69 74 20 69 6e  ler passed it in
31756 2c 20 77 65 20 77 6f 75 6c 64 6e 27 74 20 65 76  , we wouldn't ev
31757 65 6e 0a 2a 2a 20 6e 65 65 64 20 61 20 64 65 66  en.** need a def
31758 69 6e 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ine..*/.#define 
31759 52 4f 4f 54 5f 4d 41 58 20 31 30 32 34 0a 23 69  ROOT_MAX 1024.#i
3175a 66 20 52 4f 4f 54 5f 4d 41 58 3c 56 41 52 49 4e  f ROOT_MAX<VARIN
3175b 54 5f 4d 41 58 2a 32 0a 23 20 65 72 72 6f 72 20  T_MAX*2.# error 
3175c 52 4f 4f 54 5f 4d 41 58 20 6d 75 73 74 20 68 61  ROOT_MAX must ha
3175d 76 65 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20  ve enough space 
3175e 66 6f 72 20 61 20 68 65 61 64 65 72 2e 0a 23 65  for a header..#e
3175f 6e 64 69 66 0a 0a 2f 2a 20 49 6e 74 65 72 69 6f  ndif../* Interio
31760 72 42 6c 6f 63 6b 20 73 74 6f 72 65 73 20 61 20  rBlock stores a 
31761 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66 20 69  linked-list of i
31762 6e 74 65 72 69 6f 72 20 62 6c 6f 63 6b 73 20 77  nterior blocks w
31763 68 69 6c 65 20 61 20 6c 6f 77 65 72 0a 2a 2a 20  hile a lower.** 
31764 6c 61 79 65 72 20 69 73 20 62 65 69 6e 67 20 63  layer is being c
31765 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2f 0a 74  onstructed..*/.t
31766 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e  ypedef struct In
31767 74 65 72 69 6f 72 42 6c 6f 63 6b 20 7b 0a 20 20  teriorBlock {.  
31768 44 61 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b  DataBuffer term;
31769 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
3176a 66 74 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20 62  ftmost term in b
3176b 6c 6f 63 6b 27 73 20 73 75 62 74 72 65 65 2e 20  lock's subtree. 
3176c 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  */.  DataBuffer 
3176d 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  data;           
3176e 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 64 20 64  /* Accumulated d
3176f 61 74 61 20 66 6f 72 20 74 68 65 20 62 6c 6f 63  ata for the bloc
31770 6b 2e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 49  k. */.  struct I
31771 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 6e 65  nteriorBlock *ne
31772 78 74 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 42 6c  xt;.} InteriorBl
31773 6f 63 6b 3b 0a 0a 73 74 61 74 69 63 20 49 6e 74  ock;..static Int
31774 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 69 6e 74 65  eriorBlock *inte
31775 72 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 69 6e 74  riorBlockNew(int
31776 20 69 48 65 69 67 68 74 2c 20 73 71 6c 69 74 65   iHeight, sqlite
31777 5f 69 6e 74 36 34 20 69 43 68 69 6c 64 42 6c 6f  _int64 iChildBlo
31778 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ck,.            
31779 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3177a 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
3177b 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
3177c 74 20 6e 54 65 72 6d 29 7b 0a 20 20 49 6e 74 65  t nTerm){.  Inte
3177d 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63 6b  riorBlock *block
3177e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
3177f 63 28 73 69 7a 65 6f 66 28 49 6e 74 65 72 69 6f  c(sizeof(Interio
31780 72 42 6c 6f 63 6b 29 29 3b 0a 20 20 63 68 61 72  rBlock));.  char
31781 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41   c[VARINT_MAX+VA
31782 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74  RINT_MAX];.  int
31783 20 6e 3b 0a 0a 20 20 69 66 28 20 62 6c 6f 63 6b   n;..  if( block
31784 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 62   ){.    memset(b
31785 6c 6f 63 6b 2c 20 30 2c 20 73 69 7a 65 6f 66 28  lock, 0, sizeof(
31786 2a 62 6c 6f 63 6b 29 29 3b 0a 20 20 20 20 64 61  *block));.    da
31787 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 62 6c  taBufferInit(&bl
31788 6f 63 6b 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 20  ock->term, 0);. 
31789 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70     dataBufferRep
3178a 6c 61 63 65 28 26 62 6c 6f 63 6b 2d 3e 74 65 72  lace(&block->ter
3178b 6d 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29  m, pTerm, nTerm)
3178c 3b 0a 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50  ;..    n = fts3P
3178d 75 74 56 61 72 69 6e 74 28 63 2c 20 69 48 65 69  utVarint(c, iHei
3178e 67 68 74 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66  ght);.    n += f
3178f 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e  ts3PutVarint(c+n
31790 2c 20 69 43 68 69 6c 64 42 6c 6f 63 6b 29 3b 0a  , iChildBlock);.
31791 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e      dataBufferIn
31792 69 74 28 26 62 6c 6f 63 6b 2d 3e 64 61 74 61 2c  it(&block->data,
31793 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 29 3b 0a   INTERIOR_MAX);.
31794 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
31795 70 6c 61 63 65 28 26 62 6c 6f 63 6b 2d 3e 64 61  place(&block->da
31796 74 61 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a 20  ta, c, n);.  }. 
31797 20 72 65 74 75 72 6e 20 62 6c 6f 63 6b 3b 0a 7d   return block;.}
31798 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
31799 0a 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20  ./* Verify that 
3179a 74 68 65 20 64 61 74 61 20 69 73 20 72 65 61 64  the data is read
3179b 61 62 6c 65 20 61 73 20 61 6e 20 69 6e 74 65 72  able as an inter
3179c 69 6f 72 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61  ior node. */.sta
3179d 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f  tic void interio
3179e 72 42 6c 6f 63 6b 56 61 6c 69 64 61 74 65 28 49  rBlockValidate(I
3179f 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 70 42  nteriorBlock *pB
317a0 6c 6f 63 6b 29 7b 0a 20 20 63 6f 6e 73 74 20 63  lock){.  const c
317a1 68 61 72 20 2a 70 44 61 74 61 20 3d 20 70 42 6c  har *pData = pBl
317a2 6f 63 6b 2d 3e 64 61 74 61 2e 70 44 61 74 61 3b  ock->data.pData;
317a3 0a 20 20 69 6e 74 20 6e 44 61 74 61 20 3d 20 70  .  int nData = p
317a4 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74  Block->data.nDat
317a5 61 3b 0a 20 20 69 6e 74 20 6e 2c 20 69 44 75 6d  a;.  int n, iDum
317a6 6d 79 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  my;.  sqlite_int
317a7 36 34 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20  64 iBlockid;..  
317a8 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20  assert( nData>0 
317a9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  );.  assert( pDa
317aa 74 61 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ta!=0 );.  asser
317ab 74 28 20 70 44 61 74 61 2b 6e 44 61 74 61 3e 70  t( pData+nData>p
317ac 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 4d 75  Data );..  /* Mu
317ad 73 74 20 6c 65 61 64 20 77 69 74 68 20 68 65 69  st lead with hei
317ae 67 68 74 20 6f 66 20 6e 6f 64 65 20 61 73 20 61  ght of node as a
317af 20 76 61 72 69 6e 74 28 6e 29 2c 20 6e 3e 30 20   varint(n), n>0 
317b0 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
317b1 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20  Varint32(pData, 
317b2 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65  &iDummy);.  asse
317b3 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73  rt( n>0 );.  ass
317b4 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
317b5 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44 61  .  assert( n<nDa
317b6 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d  ta );.  pData +=
317b7 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e   n;.  nData -= n
317b8 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 63 6f 6e  ;..  /* Must con
317b9 74 61 69 6e 20 69 42 6c 6f 63 6b 69 64 2e 20 2a  tain iBlockid. *
317ba 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56  /.  n = fts3GetV
317bb 61 72 69 6e 74 28 70 44 61 74 61 2c 20 26 69 42  arint(pData, &iB
317bc 6c 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65 72  lockid);.  asser
317bd 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65  t( n>0 );.  asse
317be 72 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a  rt( n<=nData );.
317bf 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20    pData += n;.  
317c0 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20 2f  nData -= n;..  /
317c1 2a 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74  * Zero or more t
317c2 65 72 6d 73 20 6f 66 20 70 6f 73 69 74 69 76 65  erms of positive
317c3 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 66 28   length */.  if(
317c4 20 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20   nData!=0 ){.   
317c5 20 2f 2a 20 46 69 72 73 74 20 74 65 72 6d 20 69   /* First term i
317c6 73 20 6e 6f 74 20 64 65 6c 74 61 2d 65 6e 63 6f  s not delta-enco
317c7 64 65 64 2e 20 2a 2f 0a 20 20 20 20 6e 20 3d 20  ded. */.    n = 
317c8 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
317c9 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b  pData, &iDummy);
317ca 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30  .    assert( n>0
317cb 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
317cc 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20  iDummy>0 );.    
317cd 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79  assert( n+iDummy
317ce 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  >0);.    assert(
317cf 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61   n+iDummy<=nData
317d0 20 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d   );.    pData +=
317d1 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 6e   n+iDummy;.    n
317d2 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79  Data -= n+iDummy
317d3 3b 0a 0a 20 20 20 20 2f 2a 20 46 6f 6c 6c 6f 77  ;..    /* Follow
317d4 69 6e 67 20 74 65 72 6d 73 20 64 65 6c 74 61 2d  ing terms delta-
317d5 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 20 20  encoded. */.    
317d6 77 68 69 6c 65 28 20 6e 44 61 74 61 21 3d 30 20  while( nData!=0 
317d7 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67  ){.      /* Leng
317d8 74 68 20 6f 66 20 73 68 61 72 65 64 20 70 72 65  th of shared pre
317d9 66 69 78 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 20  fix. */.      n 
317da 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
317db 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79  2(pData, &iDummy
317dc 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
317dd 20 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73   n>0 );.      as
317de 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 3d 30 20  sert( iDummy>=0 
317df 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
317e0 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20 20   n<nData );.    
317e1 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20    pData += n;.  
317e2 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a      nData -= n;.
317e3 0a 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68  .      /* Length
317e4 20 61 6e 64 20 64 61 74 61 20 6f 66 20 64 69 73   and data of dis
317e5 74 69 6e 63 74 20 73 75 66 66 69 78 2e 20 2a 2f  tinct suffix. */
317e6 0a 20 20 20 20 20 20 6e 20 3d 20 66 74 73 33 47  .      n = fts3G
317e7 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
317e8 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
317e9 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b    assert( n>0 );
317ea 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
317eb 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 20  Dummy>0 );.     
317ec 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d   assert( n+iDumm
317ed 79 3e 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65  y>0);.      asse
317ee 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44  rt( n+iDummy<=nD
317ef 61 74 61 20 29 3b 0a 20 20 20 20 20 20 70 44 61  ata );.      pDa
317f0 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a  ta += n+iDummy;.
317f1 20 20 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e        nData -= n
317f2 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 7d 0a 20  +iDummy;.    }. 
317f3 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53 53   }.}.#define ASS
317f4 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
317f5 4f 52 5f 42 4c 4f 43 4b 28 78 29 20 69 6e 74 65  OR_BLOCK(x) inte
317f6 72 69 6f 72 42 6c 6f 63 6b 56 61 6c 69 64 61 74  riorBlockValidat
317f7 65 28 78 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  e(x).#else.#defi
317f8 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
317f9 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 78  INTERIOR_BLOCK(x
317fa 29 20 61 73 73 65 72 74 28 20 31 20 29 0a 23 65  ) assert( 1 ).#e
317fb 6e 64 69 66 0a 0a 74 79 70 65 64 65 66 20 73 74  ndif..typedef st
317fc 72 75 63 74 20 49 6e 74 65 72 69 6f 72 57 72 69  ruct InteriorWri
317fd 74 65 72 20 7b 0a 20 20 69 6e 74 20 69 48 65 69  ter {.  int iHei
317fe 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ght;            
317ff 20 20 20 20 20 20 20 2f 2a 20 66 72 6f 6d 20 30         /* from 0
31800 20 61 74 20 6c 65 61 76 65 73 2e 20 2a 2f 0a 20   at leaves. */. 
31801 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a   InteriorBlock *
31802 66 69 72 73 74 2c 20 2a 6c 61 73 74 3b 0a 20 20  first, *last;.  
31803 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72 57  struct InteriorW
31804 72 69 74 65 72 20 2a 70 61 72 65 6e 74 57 72 69  riter *parentWri
31805 74 65 72 3b 0a 0a 20 20 44 61 74 61 42 75 66 66  ter;..  DataBuff
31806 65 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20  er term;        
31807 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 74         /* Last t
31808 65 72 6d 20 77 72 69 74 74 65 6e 20 74 6f 20 62  erm written to b
31809 6c 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a 2f 0a  lock "last". */.
3180a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
3180b 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f 63  OpeningChildBloc
3180c 6b 3b 20 2f 2a 20 46 69 72 73 74 20 63 68 69 6c  k; /* First chil
3180d 64 20 62 6c 6f 63 6b 20 69 6e 20 62 6c 6f 63 6b  d block in block
3180e 20 22 6c 61 73 74 22 2e 20 2a 2f 0a 23 69 66 6e   "last". */.#ifn
3180f 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 71 6c  def NDEBUG.  sql
31810 69 74 65 5f 69 6e 74 36 34 20 69 4c 61 73 74 43  ite_int64 iLastC
31811 68 69 6c 64 42 6c 6f 63 6b 3b 20 20 2f 2a 20 66  hildBlock;  /* f
31812 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 63  or consistency c
31813 68 65 63 6b 73 2e 20 2a 2f 0a 23 65 6e 64 69 66  hecks. */.#endif
31814 0a 7d 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65  .} InteriorWrite
31815 72 3b 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  r;../* Initializ
31816 65 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  e an interior no
31817 64 65 20 77 68 65 72 65 20 70 54 65 72 6d 5b 6e  de where pTerm[n
31818 54 65 72 6d 5d 20 6d 61 72 6b 73 20 74 68 65 20  Term] marks the 
31819 6c 65 66 74 6d 6f 73 74 0a 2a 2a 20 74 65 72 6d  leftmost.** term
3181a 20 69 6e 20 74 68 65 20 74 72 65 65 2e 20 20 69   in the tree.  i
3181b 43 68 69 6c 64 42 6c 6f 63 6b 20 69 73 20 74 68  ChildBlock is th
3181c 65 20 6c 65 66 74 6d 6f 73 74 20 63 68 69 6c 64  e leftmost child
3181d 20 62 6c 6f 63 6b 20 61 74 20 74 68 65 0a 2a 2a   block at the.**
3181e 20 6e 65 78 74 20 6c 65 76 65 6c 20 64 6f 77 6e   next level down
3181f 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74   the tree..*/.st
31820 61 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69  atic void interi
31821 6f 72 57 72 69 74 65 72 49 6e 69 74 28 69 6e 74  orWriterInit(int
31822 20 69 48 65 69 67 68 74 2c 20 63 6f 6e 73 74 20   iHeight, const 
31823 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
31824 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20   nTerm,.        
31825 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31826 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
31827 74 36 34 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c  t64 iChildBlock,
31828 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
31829 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3182a 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a  InteriorWriter *
3182b 70 57 72 69 74 65 72 29 7b 0a 20 20 49 6e 74 65  pWriter){.  Inte
3182c 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63 6b  riorBlock *block
3182d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 48 65 69  ;.  assert( iHei
3182e 67 68 74 3e 30 20 29 3b 0a 20 20 43 4c 45 41 52  ght>0 );.  CLEAR
3182f 28 70 57 72 69 74 65 72 29 3b 0a 0a 20 20 70 57  (pWriter);..  pW
31830 72 69 74 65 72 2d 3e 69 48 65 69 67 68 74 20 3d  riter->iHeight =
31831 20 69 48 65 69 67 68 74 3b 0a 20 20 70 57 72 69   iHeight;.  pWri
31832 74 65 72 2d 3e 69 4f 70 65 6e 69 6e 67 43 68 69  ter->iOpeningChi
31833 6c 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64  ldBlock = iChild
31834 42 6c 6f 63 6b 3b 0a 23 69 66 6e 64 65 66 20 4e  Block;.#ifndef N
31835 44 45 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d  DEBUG.  pWriter-
31836 3e 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b  >iLastChildBlock
31837 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a   = iChildBlock;.
31838 23 65 6e 64 69 66 0a 20 20 62 6c 6f 63 6b 20 3d  #endif.  block =
31839 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65   interiorBlockNe
3183a 77 28 69 48 65 69 67 68 74 2c 20 69 43 68 69 6c  w(iHeight, iChil
3183b 64 42 6c 6f 63 6b 2c 20 70 54 65 72 6d 2c 20 6e  dBlock, pTerm, n
3183c 54 65 72 6d 29 3b 0a 20 20 70 57 72 69 74 65 72  Term);.  pWriter
3183d 2d 3e 6c 61 73 74 20 3d 20 70 57 72 69 74 65 72  ->last = pWriter
3183e 2d 3e 66 69 72 73 74 20 3d 20 62 6c 6f 63 6b 3b  ->first = block;
3183f 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  .  ASSERT_VALID_
31840 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70  INTERIOR_BLOCK(p
31841 57 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 20  Writer->last);. 
31842 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
31843 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20  &pWriter->term, 
31844 30 29 3b 0a 7d 0a 0a 2f 2a 20 41 70 70 65 6e 64  0);.}../* Append
31845 20 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20   the child node 
31846 72 6f 6f 74 65 64 20 61 74 20 69 43 68 69 6c 64  rooted at iChild
31847 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e 74  Block to the int
31848 65 72 69 6f 72 20 6e 6f 64 65 2c 0a 2a 2a 20 77  erior node,.** w
31849 69 74 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d  ith pTerm[nTerm]
3184a 20 61 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74   as the leftmost
3184b 20 74 65 72 6d 20 69 6e 20 69 43 68 69 6c 64 42   term in iChildB
3184c 6c 6f 63 6b 27 73 20 73 75 62 74 72 65 65 2e 0a  lock's subtree..
3184d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
3184e 6e 74 65 72 69 6f 72 57 72 69 74 65 72 41 70 70  nteriorWriterApp
3184f 65 6e 64 28 49 6e 74 65 72 69 6f 72 57 72 69 74  end(InteriorWrit
31850 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20  er *pWriter,.   
31851 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31852 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
31853 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c  nst char *pTerm,
31854 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20   int nTerm,.    
31855 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31856 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
31857 69 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64  ite_int64 iChild
31858 42 6c 6f 63 6b 29 7b 0a 20 20 63 68 61 72 20 63  Block){.  char c
31859 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49  [VARINT_MAX+VARI
3185a 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e  NT_MAX];.  int n
3185b 2c 20 6e 50 72 65 66 69 78 20 3d 20 30 3b 0a 0a  , nPrefix = 0;..
3185c 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49    ASSERT_VALID_I
3185d 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70 57  NTERIOR_BLOCK(pW
3185e 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 0a 20  riter->last);.. 
3185f 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 65   /* The first te
31860 72 6d 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  rm written into 
31861 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  an interior node
31862 20 69 73 20 61 63 74 75 61 6c 6c 79 0a 20 20 2a   is actually.  *
31863 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
31864 68 20 74 68 65 20 73 65 63 6f 6e 64 20 63 68 69  h the second chi
31865 6c 64 20 61 64 64 65 64 20 28 74 68 65 20 66 69  ld added (the fi
31866 72 73 74 20 63 68 69 6c 64 20 77 61 73 20 61 64  rst child was ad
31867 64 65 64 0a 20 20 2a 2a 20 69 6e 20 69 6e 74 65  ded.  ** in inte
31868 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74 2c 20  riorWriterInit, 
31869 6f 72 20 69 6e 20 74 68 65 20 69 66 20 63 6c 61  or in the if cla
3186a 75 73 65 20 61 74 20 74 68 65 20 62 6f 74 74 6f  use at the botto
3186b 6d 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20 66  m of this.  ** f
3186c 75 6e 63 74 69 6f 6e 29 2e 20 20 54 68 61 74 20  unction).  That 
3186d 74 65 72 6d 20 67 65 74 73 20 65 6e 63 6f 64 65  term gets encode
3186e 64 20 73 74 72 61 69 67 68 74 20 75 70 2c 20 77  d straight up, w
3186f 69 74 68 20 6e 50 72 65 66 69 78 20 6c 65 66 74  ith nPrefix left
31870 0a 20 20 2a 2a 20 61 74 20 30 2e 0a 20 20 2a 2f  .  ** at 0..  */
31871 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
31872 74 65 72 6d 2e 6e 44 61 74 61 3d 3d 30 20 29 7b  term.nData==0 ){
31873 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75 74  .    n = fts3Put
31874 56 61 72 69 6e 74 28 63 2c 20 6e 54 65 72 6d 29  Varint(c, nTerm)
31875 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77  ;.  }else{.    w
31876 68 69 6c 65 28 20 6e 50 72 65 66 69 78 3c 70 57  hile( nPrefix<pW
31877 72 69 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74  riter->term.nDat
31878 61 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  a &&.           
31879 70 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 3d 3d  pTerm[nPrefix]==
3187a 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 70 44  pWriter->term.pD
3187b 61 74 61 5b 6e 50 72 65 66 69 78 5d 20 29 7b 0a  ata[nPrefix] ){.
3187c 20 20 20 20 20 20 6e 50 72 65 66 69 78 2b 2b 3b        nPrefix++;
3187d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 20 3d 20  .    }..    n = 
3187e 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c  fts3PutVarint(c,
3187f 20 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e   nPrefix);.    n
31880 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e   += fts3PutVarin
31881 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d 6e 50 72  t(c+n, nTerm-nPr
31882 65 66 69 78 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e  efix);.  }..#ifn
31883 64 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57 72  def NDEBUG.  pWr
31884 69 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c 64  iter->iLastChild
31885 42 6c 6f 63 6b 2b 2b 3b 0a 23 65 6e 64 69 66 0a  Block++;.#endif.
31886 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74 65    assert( pWrite
31887 72 2d 3e 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f  r->iLastChildBlo
31888 63 6b 3d 3d 69 43 68 69 6c 64 42 6c 6f 63 6b 20  ck==iChildBlock 
31889 29 3b 0a 0a 20 20 2f 2a 20 4f 76 65 72 66 6c 6f  );..  /* Overflo
3188a 77 20 74 6f 20 61 20 6e 65 77 20 62 6c 6f 63 6b  w to a new block
3188b 20 69 66 20 74 68 65 20 6e 65 77 20 74 65 72 6d   if the new term
3188c 20 6d 61 6b 65 73 20 74 68 65 20 63 75 72 72 65   makes the curre
3188d 6e 74 20 62 6c 6f 63 6b 0a 20 20 2a 2a 20 74 6f  nt block.  ** to
3188e 6f 20 62 69 67 2c 20 61 6e 64 20 74 68 65 20 63  o big, and the c
3188f 75 72 72 65 6e 74 20 62 6c 6f 63 6b 20 61 6c 72  urrent block alr
31890 65 61 64 79 20 68 61 73 20 65 6e 6f 75 67 68 20  eady has enough 
31891 74 65 72 6d 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  terms..  */.  if
31892 28 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 2d  ( pWriter->last-
31893 3e 64 61 74 61 2e 6e 44 61 74 61 2b 6e 2b 6e 54  >data.nData+n+nT
31894 65 72 6d 2d 6e 50 72 65 66 69 78 3e 49 4e 54 45  erm-nPrefix>INTE
31895 52 49 4f 52 5f 4d 41 58 20 26 26 0a 20 20 20 20  RIOR_MAX &&.    
31896 20 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2d 70 57    iChildBlock-pW
31897 72 69 74 65 72 2d 3e 69 4f 70 65 6e 69 6e 67 43  riter->iOpeningC
31898 68 69 6c 64 42 6c 6f 63 6b 3e 49 4e 54 45 52 49  hildBlock>INTERI
31899 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 29 7b 0a  OR_MIN_TERMS ){.
3189a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 6c 61 73      pWriter->las
3189b 74 2d 3e 6e 65 78 74 20 3d 20 69 6e 74 65 72 69  t->next = interi
3189c 6f 72 42 6c 6f 63 6b 4e 65 77 28 70 57 72 69 74  orBlockNew(pWrit
3189d 65 72 2d 3e 69 48 65 69 67 68 74 2c 20 69 43 68  er->iHeight, iCh
3189e 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20  ildBlock,.      
3189f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
318a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
318a1 20 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72       pTerm, nTer
318a2 6d 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d  m);.    pWriter-
318a3 3e 6c 61 73 74 20 3d 20 70 57 72 69 74 65 72 2d  >last = pWriter-
318a4 3e 6c 61 73 74 2d 3e 6e 65 78 74 3b 0a 20 20 20  >last->next;.   
318a5 20 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e 69   pWriter->iOpeni
318a6 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20 69  ngChildBlock = i
318a7 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a 20 20 20 20  ChildBlock;.    
318a8 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
318a9 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29 3b  &pWriter->term);
318aa 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 61  .  }else{.    da
318ab 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
318ac 26 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 2d 3e  &pWriter->last->
318ad 64 61 74 61 2c 20 63 2c 20 6e 2c 0a 20 20 20 20  data, c, n,.    
318ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
318af 20 20 70 54 65 72 6d 2b 6e 50 72 65 66 69 78 2c    pTerm+nPrefix,
318b0 20 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 29 3b   nTerm-nPrefix);
318b1 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52  .    dataBufferR
318b2 65 70 6c 61 63 65 28 26 70 57 72 69 74 65 72 2d  eplace(&pWriter-
318b3 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 54  >term, pTerm, nT
318b4 65 72 6d 29 3b 0a 20 20 7d 0a 20 20 41 53 53 45  erm);.  }.  ASSE
318b5 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f  RT_VALID_INTERIO
318b6 52 5f 42 4c 4f 43 4b 28 70 57 72 69 74 65 72 2d  R_BLOCK(pWriter-
318b7 3e 6c 61 73 74 29 3b 0a 7d 0a 0a 2f 2a 20 46 72  >last);.}../* Fr
318b8 65 65 20 74 68 65 20 73 70 61 63 65 20 75 73 65  ee the space use
318b9 64 20 62 79 20 70 57 72 69 74 65 72 2c 20 69 6e  d by pWriter, in
318ba 63 6c 75 64 69 6e 67 20 74 68 65 20 6c 69 6e 6b  cluding the link
318bb 65 64 2d 6c 69 73 74 20 6f 66 0a 2a 2a 20 49 6e  ed-list of.** In
318bc 74 65 72 69 6f 72 42 6c 6f 63 6b 73 2c 20 61 6e  teriorBlocks, an
318bd 64 20 70 61 72 65 6e 74 57 72 69 74 65 72 2c 20  d parentWriter, 
318be 69 66 20 70 72 65 73 65 6e 74 2e 0a 2a 2f 0a 73  if present..*/.s
318bf 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72 69  tatic int interi
318c0 6f 72 57 72 69 74 65 72 44 65 73 74 72 6f 79 28  orWriterDestroy(
318c1 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a  InteriorWriter *
318c2 70 57 72 69 74 65 72 29 7b 0a 20 20 49 6e 74 65  pWriter){.  Inte
318c3 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63 6b  riorBlock *block
318c4 20 3d 20 70 57 72 69 74 65 72 2d 3e 66 69 72 73   = pWriter->firs
318c5 74 3b 0a 0a 20 20 77 68 69 6c 65 28 20 62 6c 6f  t;..  while( blo
318c6 63 6b 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ck!=NULL ){.    
318c7 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62  InteriorBlock *b
318c8 20 3d 20 62 6c 6f 63 6b 3b 0a 20 20 20 20 62 6c   = block;.    bl
318c9 6f 63 6b 20 3d 20 62 6c 6f 63 6b 2d 3e 6e 65 78  ock = block->nex
318ca 74 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  t;.    dataBuffe
318cb 72 44 65 73 74 72 6f 79 28 26 62 2d 3e 74 65 72  rDestroy(&b->ter
318cc 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  m);.    dataBuff
318cd 65 72 44 65 73 74 72 6f 79 28 26 62 2d 3e 64 61  erDestroy(&b->da
318ce 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ta);.    sqlite3
318cf 5f 66 72 65 65 28 62 29 3b 0a 20 20 7d 0a 20 20  _free(b);.  }.  
318d0 69 66 28 20 70 57 72 69 74 65 72 2d 3e 70 61 72  if( pWriter->par
318d1 65 6e 74 57 72 69 74 65 72 21 3d 4e 55 4c 4c 20  entWriter!=NULL 
318d2 29 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72 57  ){.    interiorW
318d3 72 69 74 65 72 44 65 73 74 72 6f 79 28 70 57 72  riterDestroy(pWr
318d4 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74  iter->parentWrit
318d5 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  er);.    sqlite3
318d6 5f 66 72 65 65 28 70 57 72 69 74 65 72 2d 3e 70  _free(pWriter->p
318d7 61 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 20 20  arentWriter);.  
318d8 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  }.  dataBufferDe
318d9 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e  stroy(&pWriter->
318da 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d 42 4c  term);.  SCRAMBL
318db 45 28 70 57 72 69 74 65 72 29 3b 0a 20 20 72 65  E(pWriter);.  re
318dc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
318dd 7d 0a 0a 2f 2a 20 49 66 20 70 57 72 69 74 65 72  }../* If pWriter
318de 20 63 61 6e 20 66 69 74 20 65 6e 74 69 72 65 6c   can fit entirel
318df 79 20 69 6e 20 52 4f 4f 54 5f 4d 41 58 2c 20 72  y in ROOT_MAX, r
318e0 65 74 75 72 6e 20 69 74 20 61 73 20 74 68 65 20  eturn it as the 
318e1 72 6f 6f 74 20 69 6e 66 6f 0a 2a 2a 20 64 69 72  root info.** dir
318e2 65 63 74 6c 79 2c 20 6c 65 61 76 69 6e 67 20 2a  ectly, leaving *
318e3 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 75 6e 63  piEndBlockid unc
318e4 68 61 6e 67 65 64 2e 20 20 4f 74 68 65 72 77 69  hanged.  Otherwi
318e5 73 65 2c 20 66 6c 75 73 68 0a 2a 2a 20 70 57 72  se, flush.** pWr
318e6 69 74 65 72 20 74 6f 20 25 5f 73 65 67 6d 65 6e  iter to %_segmen
318e7 74 73 2c 20 62 75 69 6c 64 69 6e 67 20 61 20 6e  ts, building a n
318e8 65 77 20 6c 61 79 65 72 20 6f 66 20 69 6e 74 65  ew layer of inte
318e9 72 69 6f 72 20 6e 6f 64 65 73 2c 20 61 6e 64 0a  rior nodes, and.
318ea 2a 2a 20 72 65 63 75 72 73 69 76 65 6c 79 20 61  ** recursively a
318eb 73 6b 20 66 6f 72 20 74 68 65 69 72 20 72 6f 6f  sk for their roo
318ec 74 20 69 6e 74 6f 2e 0a 2a 2f 0a 73 74 61 74 69  t into..*/.stati
318ed 63 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 57 72  c int interiorWr
318ee 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 66 75 6c  iterRootInfo(ful
318ef 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 49  ltext_vtab *v, I
318f0 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70  nteriorWriter *p
318f1 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20  Writer,.        
318f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
318f3 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
318f4 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e 74  *ppRootInfo, int
318f5 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20 20   *pnRootInfo,.  
318f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
318f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
318f8 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
318f9 45 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 49  EndBlockid){.  I
318fa 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c  nteriorBlock *bl
318fb 6f 63 6b 20 3d 20 70 57 72 69 74 65 72 2d 3e 66  ock = pWriter->f
318fc 69 72 73 74 3b 0a 20 20 73 71 6c 69 74 65 5f 69  irst;.  sqlite_i
318fd 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 20 3d 20  nt64 iBlockid = 
318fe 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  0;.  int rc;..  
318ff 2f 2a 20 49 66 20 77 65 20 63 61 6e 20 66 69 74  /* If we can fit
31900 20 74 68 65 20 73 65 67 6d 65 6e 74 20 69 6e 6c   the segment inl
31901 69 6e 65 20 2a 2f 0a 20 20 69 66 28 20 62 6c 6f  ine */.  if( blo
31902 63 6b 3d 3d 70 57 72 69 74 65 72 2d 3e 6c 61 73  ck==pWriter->las
31903 74 20 26 26 20 62 6c 6f 63 6b 2d 3e 64 61 74 61  t && block->data
31904 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d 41 58 20  .nData<ROOT_MAX 
31905 29 7b 0a 20 20 20 20 2a 70 70 52 6f 6f 74 49 6e  ){.    *ppRootIn
31906 66 6f 20 3d 20 62 6c 6f 63 6b 2d 3e 64 61 74 61  fo = block->data
31907 2e 70 44 61 74 61 3b 0a 20 20 20 20 2a 70 6e 52  .pData;.    *pnR
31908 6f 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b 2d  ootInfo = block-
31909 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20 20 20  >data.nData;.   
3190a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3190b 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6c 75  K;.  }..  /* Flu
3190c 73 68 20 74 68 65 20 66 69 72 73 74 20 62 6c 6f  sh the first blo
3190d 63 6b 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73  ck to %_segments
3190e 2c 20 61 6e 64 20 63 72 65 61 74 65 20 61 20 6e  , and create a n
3190f 65 77 20 6c 65 76 65 6c 20 6f 66 0a 20 20 2a 2a  ew level of.  **
31910 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 0a   interior node..
31911 20 20 2a 2f 0a 20 20 41 53 53 45 52 54 5f 56 41    */.  ASSERT_VA
31912 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f  LID_INTERIOR_BLO
31913 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20 20 72 63 20  CK(block);.  rc 
31914 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 76  = block_insert(v
31915 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44  , block->data.pD
31916 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61  ata, block->data
31917 2e 6e 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69  .nData, &iBlocki
31918 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
31919 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3191a 20 72 63 3b 0a 20 20 2a 70 69 45 6e 64 42 6c 6f   rc;.  *piEndBlo
3191b 63 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64 3b  ckid = iBlockid;
3191c 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e 70 61 72  ..  pWriter->par
3191d 65 6e 74 57 72 69 74 65 72 20 3d 20 73 71 6c 69  entWriter = sqli
3191e 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
3191f 66 28 2a 70 57 72 69 74 65 72 2d 3e 70 61 72 65  f(*pWriter->pare
31920 6e 74 57 72 69 74 65 72 29 29 3b 0a 20 20 69 6e  ntWriter));.  in
31921 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74  teriorWriterInit
31922 28 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68  (pWriter->iHeigh
31923 74 2b 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20  t+1,.           
31924 20 20 20 20 20 20 20 20 20 20 62 6c 6f 63 6b 2d            block-
31925 3e 74 65 72 6d 2e 70 44 61 74 61 2c 20 62 6c 6f  >term.pData, blo
31926 63 6b 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 2c 0a  ck->term.nData,.
31927 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31928 20 20 20 20 20 69 42 6c 6f 63 6b 69 64 2c 20 70       iBlockid, p
31929 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72  Writer->parentWr
3192a 69 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 46 6c 75  iter);..  /* Flu
3192b 73 68 20 61 64 64 69 74 69 6f 6e 61 6c 20 62 6c  sh additional bl
3192c 6f 63 6b 73 20 61 6e 64 20 61 70 70 65 6e 64 20  ocks and append 
3192d 74 6f 20 74 68 65 20 68 69 67 68 65 72 20 69 6e  to the higher in
3192e 74 65 72 69 6f 72 0a 20 20 2a 2a 20 6e 6f 64 65  terior.  ** node
3192f 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 62 6c 6f  ..  */.  for(blo
31930 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65 78 74 3b 20  ck=block->next; 
31931 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 3b 20 62 6c 6f  block!=NULL; blo
31932 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65 78 74 29 7b  ck=block->next){
31933 0a 20 20 20 20 41 53 53 45 52 54 5f 56 41 4c 49  .    ASSERT_VALI
31934 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b  D_INTERIOR_BLOCK
31935 28 62 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20  (block);.    rc 
31936 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 76  = block_insert(v
31937 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44  , block->data.pD
31938 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61  ata, block->data
31939 2e 6e 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69  .nData, &iBlocki
3193a 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  d);.    if( rc!=
3193b 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
3193c 72 6e 20 72 63 3b 0a 20 20 20 20 2a 70 69 45 6e  rn rc;.    *piEn
3193d 64 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63  dBlockid = iBloc
3193e 6b 69 64 3b 0a 0a 20 20 20 20 69 6e 74 65 72 69  kid;..    interi
3193f 6f 72 57 72 69 74 65 72 41 70 70 65 6e 64 28 70  orWriterAppend(p
31940 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72  Writer->parentWr
31941 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  iter,.          
31942 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
31943 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74 61  lock->term.pData
31944 2c 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e 44  , block->term.nD
31945 61 74 61 2c 20 69 42 6c 6f 63 6b 69 64 29 3b 0a  ata, iBlockid);.
31946 20 20 7d 0a 0a 20 20 2f 2a 20 50 61 72 65 6e 74    }..  /* Parent
31947 20 6e 6f 64 65 20 67 65 74 73 20 74 68 65 20 63   node gets the c
31948 68 61 6e 63 65 20 74 6f 20 62 65 20 74 68 65 20  hance to be the 
31949 72 6f 6f 74 2e 20 2a 2f 0a 20 20 72 65 74 75 72  root. */.  retur
3194a 6e 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  n interiorWriter
3194b 52 6f 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72 69  RootInfo(v, pWri
3194c 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65  ter->parentWrite
3194d 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
3194e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3194f 20 20 20 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 70     ppRootInfo, p
31950 6e 52 6f 6f 74 49 6e 66 6f 2c 20 70 69 45 6e 64  nRootInfo, piEnd
31951 42 6c 6f 63 6b 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a  Blockid);.}../**
31952 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31953 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31954 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31955 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
31956 2f 2a 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65  /* InteriorReade
31957 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61  r is used to rea
31958 64 20 6f 66 66 20 74 68 65 20 64 61 74 61 20 66  d off the data f
31959 72 6f 6d 20 61 6e 20 69 6e 74 65 72 69 6f 72 20  rom an interior 
3195a 6e 6f 64 65 0a 2a 2a 20 28 73 65 65 20 63 6f 6d  node.** (see com
3195b 6d 65 6e 74 20 61 74 20 74 6f 70 20 6f 66 20 66  ment at top of f
3195c 69 6c 65 20 66 6f 72 20 74 68 65 20 66 6f 72 6d  ile for the form
3195d 61 74 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  at)..*/.typedef 
3195e 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72 52  struct InteriorR
3195f 65 61 64 65 72 20 7b 0a 20 20 63 6f 6e 73 74 20  eader {.  const 
31960 63 68 61 72 20 2a 70 44 61 74 61 3b 0a 20 20 69  char *pData;.  i
31961 6e 74 20 6e 44 61 74 61 3b 0a 0a 20 20 44 61 74  nt nData;..  Dat
31962 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20  aBuffer term;   
31963 20 20 20 20 20 20 20 2f 2a 20 70 72 65 76 69 6f         /* previo
31964 75 73 20 74 65 72 6d 2c 20 66 6f 72 20 64 65 63  us term, for dec
31965 6f 64 69 6e 67 20 74 65 72 6d 20 64 65 6c 74 61  oding term delta
31966 2e 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 5f 69  . */..  sqlite_i
31967 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 3b 0a 7d  nt64 iBlockid;.}
31968 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 3b   InteriorReader;
31969 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  ..static void in
3196a 74 65 72 69 6f 72 52 65 61 64 65 72 44 65 73 74  teriorReaderDest
3196b 72 6f 79 28 49 6e 74 65 72 69 6f 72 52 65 61 64  roy(InteriorRead
3196c 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
3196d 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
3196e 79 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  y(&pReader->term
3196f 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52  );.  SCRAMBLE(pR
31970 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 54 4f  eader);.}../* TO
31971 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 61 73  DO(shess) The as
31972 73 65 72 74 69 6f 6e 73 20 61 72 65 20 67 72 65  sertions are gre
31973 61 74 2c 20 62 75 74 20 77 68 61 74 20 69 66 20  at, but what if 
31974 77 65 27 72 65 20 69 6e 20 4e 44 45 42 55 47 0a  we're in NDEBUG.
31975 2a 2a 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 20  ** and the blob 
31976 69 73 20 65 6d 70 74 79 20 6f 72 20 6f 74 68 65  is empty or othe
31977 72 77 69 73 65 20 63 6f 6e 74 61 69 6e 73 20 73  rwise contains s
31978 75 73 70 65 63 74 20 64 61 74 61 3f 0a 2a 2f 0a  uspect data?.*/.
31979 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74 65  static void inte
3197a 72 69 6f 72 52 65 61 64 65 72 49 6e 69 74 28 63  riorReaderInit(c
3197b 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
3197c 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20  , int nData,.   
3197d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3197e 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65              Inte
3197f 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61  riorReader *pRea
31980 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e  der){.  int n, n
31981 54 65 72 6d 3b 0a 0a 20 20 2f 2a 20 52 65 71 75  Term;..  /* Requ
31982 69 72 65 20 61 74 20 6c 65 61 73 74 20 74 68 65  ire at least the
31983 20 6c 65 61 64 69 6e 67 20 66 6c 61 67 20 62 79   leading flag by
31984 74 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  te */.  assert( 
31985 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73  nData>0 );.  ass
31986 65 72 74 28 20 70 44 61 74 61 5b 30 5d 21 3d 27  ert( pData[0]!='
31987 5c 30 27 20 29 3b 0a 0a 20 20 43 4c 45 41 52 28  \0' );..  CLEAR(
31988 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20  pReader);..  /* 
31989 44 65 63 6f 64 65 20 74 68 65 20 62 61 73 65 20  Decode the base 
3198a 62 6c 6f 63 6b 69 64 2c 20 61 6e 64 20 73 65 74  blockid, and set
3198b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74   the cursor to t
3198c 68 65 20 66 69 72 73 74 20 74 65 72 6d 2e 20 2a  he first term. *
3198d 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56  /.  n = fts3GetV
3198e 61 72 69 6e 74 28 70 44 61 74 61 2b 31 2c 20 26  arint(pData+1, &
3198f 70 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b 69  pReader->iBlocki
31990 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 31 2b  d);.  assert( 1+
31991 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 70 52  n<=nData );.  pR
31992 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70  eader->pData = p
31993 44 61 74 61 2b 31 2b 6e 3b 0a 20 20 70 52 65 61  Data+1+n;.  pRea
31994 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44 61  der->nData = nDa
31995 74 61 2d 28 31 2b 6e 29 3b 0a 0a 20 20 2f 2a 20  ta-(1+n);..  /* 
31996 41 20 73 69 6e 67 6c 65 2d 63 68 69 6c 64 20 69  A single-child i
31997 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 28 73 75  nterior node (su
31998 63 68 20 61 73 20 77 68 65 6e 20 61 20 6c 65 61  ch as when a lea
31999 66 20 6e 6f 64 65 20 77 61 73 20 74 6f 6f 0a 20  f node was too. 
3199a 20 2a 2a 20 6c 61 72 67 65 20 66 6f 72 20 74 68   ** large for th
3199b 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74  e segment direct
3199c 6f 72 79 29 20 77 6f 6e 27 74 20 68 61 76 65 20  ory) won't have 
3199d 61 6e 79 20 74 65 72 6d 73 2e 0a 20 20 2a 2a 20  any terms..  ** 
3199e 4f 74 68 65 72 77 69 73 65 2c 20 64 65 63 6f 64  Otherwise, decod
3199f 65 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  e the first term
319a0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65  ..  */.  if( pRe
319a1 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 20 29  ader->nData==0 )
319a2 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  {.    dataBuffer
319a3 49 6e 69 74 28 26 70 52 65 61 64 65 72 2d 3e 74  Init(&pReader->t
319a4 65 72 6d 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  erm, 0);.  }else
319a5 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65  {.    n = fts3Ge
319a6 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
319a7 72 2d 3e 70 44 61 74 61 2c 20 26 6e 54 65 72 6d  r->pData, &nTerm
319a8 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  );.    dataBuffe
319a9 72 49 6e 69 74 28 26 70 52 65 61 64 65 72 2d 3e  rInit(&pReader->
319aa 74 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  term, nTerm);.  
319ab 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c    dataBufferRepl
319ac 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 74 65  ace(&pReader->te
319ad 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70 44 61  rm, pReader->pDa
319ae 74 61 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  ta+n, nTerm);.  
319af 20 20 61 73 73 65 72 74 28 20 6e 2b 6e 54 65 72    assert( n+nTer
319b0 6d 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  m<=pReader->nDat
319b1 61 20 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72  a );.    pReader
319b2 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 54 65  ->pData += n+nTe
319b3 72 6d 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d  rm;.    pReader-
319b4 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 54 65 72  >nData -= n+nTer
319b5 6d 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63  m;.  }.}..static
319b6 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 52 65 61   int interiorRea
319b7 64 65 72 41 74 45 6e 64 28 49 6e 74 65 72 69 6f  derAtEnd(Interio
319b8 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  rReader *pReader
319b9 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  ){.  return pRea
319ba 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3d  der->term.nData=
319bb 3d 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73 71  =0;.}..static sq
319bc 6c 69 74 65 5f 69 6e 74 36 34 20 69 6e 74 65 72  lite_int64 inter
319bd 69 6f 72 52 65 61 64 65 72 43 75 72 72 65 6e 74  iorReaderCurrent
319be 42 6c 6f 63 6b 69 64 28 49 6e 74 65 72 69 6f 72  Blockid(Interior
319bf 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
319c0 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64  {.  return pRead
319c1 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 3b 0a 7d 0a  er->iBlockid;.}.
319c2 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65  .static int inte
319c3 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d 42 79  riorReaderTermBy
319c4 74 65 73 28 49 6e 74 65 72 69 6f 72 52 65 61 64  tes(InteriorRead
319c5 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
319c6 61 73 73 65 72 74 28 20 21 69 6e 74 65 72 69 6f  assert( !interio
319c7 72 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65  rReaderAtEnd(pRe
319c8 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
319c9 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
319ca 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20  nData;.}.static 
319cb 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 74 65  const char *inte
319cc 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d 28 49  riorReaderTerm(I
319cd 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
319ce 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
319cf 74 28 20 21 69 6e 74 65 72 69 6f 72 52 65 61 64  t( !interiorRead
319d0 65 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29  erAtEnd(pReader)
319d1 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
319d2 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61  ader->term.pData
319d3 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 66 6f 72  ;.}../* Step for
319d4 77 61 72 64 20 74 6f 20 74 68 65 20 6e 65 78 74  ward to the next
319d5 20 74 65 72 6d 20 69 6e 20 74 68 65 20 6e 6f 64   term in the nod
319d6 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e. */.static voi
319d7 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  d interiorReader
319d8 53 74 65 70 28 49 6e 74 65 72 69 6f 72 52 65 61  Step(InteriorRea
319d9 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
319da 20 61 73 73 65 72 74 28 20 21 69 6e 74 65 72 69   assert( !interi
319db 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 70 52  orReaderAtEnd(pR
319dc 65 61 64 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20  eader) );..  /* 
319dd 49 66 20 74 68 65 20 6c 61 73 74 20 74 65 72 6d  If the last term
319de 20 68 61 73 20 62 65 65 6e 20 72 65 61 64 2c 20   has been read, 
319df 73 69 67 6e 61 6c 20 65 6f 66 2c 20 65 6c 73 65  signal eof, else
319e0 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 0a 20   construct the. 
319e1 20 2a 2a 20 6e 65 78 74 20 74 65 72 6d 2e 0a 20   ** next term.. 
319e2 20 2a 2f 0a 20 20 69 66 28 20 70 52 65 61 64 65   */.  if( pReade
319e3 72 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20  r->nData==0 ){. 
319e4 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73     dataBufferRes
319e5 65 74 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72  et(&pReader->ter
319e6 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  m);.  }else{.   
319e7 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 2c   int n, nPrefix,
319e8 20 6e 53 75 66 66 69 78 3b 0a 0a 20 20 20 20 6e   nSuffix;..    n
319e9 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
319ea 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74  32(pReader->pDat
319eb 61 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a 20 20  a, &nPrefix);.  
319ec 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
319ed 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
319ee 70 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66 66 69  pData+n, &nSuffi
319ef 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 72 75 6e  x);..    /* Trun
319f0 63 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  cate the current
319f1 20 74 65 72 6d 20 61 6e 64 20 61 70 70 65 6e 64   term and append
319f2 20 73 75 66 66 69 78 20 64 61 74 61 2e 20 2a 2f   suffix data. */
319f3 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 74 65  .    pReader->te
319f4 72 6d 2e 6e 44 61 74 61 20 3d 20 6e 50 72 65 66  rm.nData = nPref
319f5 69 78 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  ix;.    dataBuff
319f6 65 72 41 70 70 65 6e 64 28 26 70 52 65 61 64 65  erAppend(&pReade
319f7 72 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65 72  r->term, pReader
319f8 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e 53 75 66 66  ->pData+n, nSuff
319f9 69 78 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  ix);..    assert
319fa 28 20 6e 2b 6e 53 75 66 66 69 78 3c 3d 70 52 65  ( n+nSuffix<=pRe
319fb 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20  ader->nData );. 
319fc 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74     pReader->pDat
319fd 61 20 2b 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a  a += n+nSuffix;.
319fe 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61      pReader->nDa
319ff 74 61 20 2d 3d 20 6e 2b 6e 53 75 66 66 69 78 3b  ta -= n+nSuffix;
31a00 0a 20 20 7d 0a 20 20 70 52 65 61 64 65 72 2d 3e  .  }.  pReader->
31a01 69 42 6c 6f 63 6b 69 64 2b 2b 3b 0a 7d 0a 0a 2f  iBlockid++;.}../
31a02 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 63 75  * Compare the cu
31a03 72 72 65 6e 74 20 74 65 72 6d 20 74 6f 20 70 54  rrent term to pT
31a04 65 72 6d 5b 6e 54 65 72 6d 5d 2c 20 72 65 74 75  erm[nTerm], retu
31a05 72 6e 69 6e 67 20 73 74 72 63 6d 70 2d 73 74 79  rning strcmp-sty
31a06 6c 65 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 20  le.** results.  
31a07 49 66 20 69 73 50 72 65 66 69 78 2c 20 65 71 75  If isPrefix, equ
31a08 61 6c 69 74 79 20 6d 65 61 6e 73 20 65 71 75 61  ality means equa
31a09 6c 20 74 68 72 6f 75 67 68 20 6e 54 65 72 6d 20  l through nTerm 
31a0a 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  bytes..*/.static
31a0b 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 52 65 61   int interiorRea
31a0c 64 65 72 54 65 72 6d 43 6d 70 28 49 6e 74 65 72  derTermCmp(Inter
31a0d 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64  iorReader *pRead
31a0e 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
31a0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31a10 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
31a11 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
31a12 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 29  m, int isPrefix)
31a13 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
31a14 70 52 65 61 64 65 72 54 65 72 6d 20 3d 20 69 6e  pReaderTerm = in
31a15 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d  teriorReaderTerm
31a16 28 70 52 65 61 64 65 72 29 3b 0a 20 20 69 6e 74  (pReader);.  int
31a17 20 6e 52 65 61 64 65 72 54 65 72 6d 20 3d 20 69   nReaderTerm = i
31a18 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72  nteriorReaderTer
31a19 6d 42 79 74 65 73 28 70 52 65 61 64 65 72 29 3b  mBytes(pReader);
31a1a 0a 20 20 69 6e 74 20 63 2c 20 6e 20 3d 20 6e 52  .  int c, n = nR
31a1b 65 61 64 65 72 54 65 72 6d 3c 6e 54 65 72 6d 20  eaderTerm<nTerm 
31a1c 3f 20 6e 52 65 61 64 65 72 54 65 72 6d 20 3a 20  ? nReaderTerm : 
31a1d 6e 54 65 72 6d 3b 0a 0a 20 20 69 66 28 20 6e 3d  nTerm;..  if( n=
31a1e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 52  =0 ){.    if( nR
31a1f 65 61 64 65 72 54 65 72 6d 3e 30 20 29 20 72 65  eaderTerm>0 ) re
31a20 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 69 66 28  turn -1;.    if(
31a21 20 6e 54 65 72 6d 3e 30 20 29 20 72 65 74 75 72   nTerm>0 ) retur
31a22 6e 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  n 1;.    return 
31a23 30 3b 0a 20 20 7d 0a 0a 20 20 63 20 3d 20 6d 65  0;.  }..  c = me
31a24 6d 63 6d 70 28 70 52 65 61 64 65 72 54 65 72 6d  mcmp(pReaderTerm
31a25 2c 20 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20 69  , pTerm, n);.  i
31a26 66 28 20 63 21 3d 30 20 29 20 72 65 74 75 72 6e  f( c!=0 ) return
31a27 20 63 3b 0a 20 20 69 66 28 20 69 73 50 72 65 66   c;.  if( isPref
31a28 69 78 20 26 26 20 6e 3d 3d 6e 54 65 72 6d 20 29  ix && n==nTerm )
31a29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74   return 0;.  ret
31a2a 75 72 6e 20 6e 52 65 61 64 65 72 54 65 72 6d 20  urn nReaderTerm 
31a2b 2d 20 6e 54 65 72 6d 3b 0a 7d 0a 0a 2f 2a 2a 2a  - nTerm;.}../***
31a2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31a2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31a2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31a2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
31a30 2a 20 4c 65 61 66 57 72 69 74 65 72 20 69 73 20  * LeafWriter is 
31a31 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74 20  used to collect 
31a32 74 65 72 6d 73 20 61 6e 64 20 61 73 73 6f 63 69  terms and associ
31a33 61 74 65 64 20 64 6f 63 6c 69 73 74 20 64 61 74  ated doclist dat
31a34 61 0a 2a 2a 20 69 6e 74 6f 20 6c 65 61 66 20 62  a.** into leaf b
31a35 6c 6f 63 6b 73 20 69 6e 20 25 5f 73 65 67 6d 65  locks in %_segme
31a36 6e 74 73 20 28 73 65 65 20 74 6f 70 20 6f 66 20  nts (see top of 
31a37 66 69 6c 65 20 66 6f 72 20 66 6f 72 6d 61 74 20  file for format 
31a38 69 6e 66 6f 29 2e 0a 2a 2a 20 45 78 70 65 63 74  info)..** Expect
31a39 65 64 20 75 73 61 67 65 20 69 73 3a 0a 2a 2a 0a  ed usage is:.**.
31a3a 2a 2a 20 4c 65 61 66 57 72 69 74 65 72 20 77 72  ** LeafWriter wr
31a3b 69 74 65 72 3b 0a 2a 2a 20 6c 65 61 66 57 72 69  iter;.** leafWri
31a3c 74 65 72 49 6e 69 74 28 30 2c 20 30 2c 20 26 77  terInit(0, 0, &w
31a3d 72 69 74 65 72 29 3b 0a 2a 2a 20 77 68 69 6c 65  riter);.** while
31a3e 28 20 73 6f 72 74 65 64 5f 74 65 72 6d 73 5f 6c  ( sorted_terms_l
31a3f 65 66 74 5f 74 6f 5f 70 72 6f 63 65 73 73 20 29  eft_to_process )
31a40 7b 0a 2a 2a 20 20 20 2f 2f 20 64 61 74 61 20 69  {.**   // data i
31a41 73 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66  s doclist data f
31a42 6f 72 20 74 68 61 74 20 74 65 72 6d 2e 0a 2a 2a  or that term..**
31a43 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74     rc = leafWrit
31a44 65 72 53 74 65 70 28 76 2c 20 26 77 72 69 74 65  erStep(v, &write
31a45 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  r, pTerm, nTerm,
31a46 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a   pData, nData);.
31a47 2a 2a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  **   if( rc!=SQL
31a48 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
31a49 72 3b 0a 2a 2a 20 7d 0a 2a 2a 20 72 63 20 3d 20  r;.** }.** rc = 
31a4a 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69  leafWriterFinali
31a4b 7a 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b 0a  ze(v, &writer);.
31a4c 2a 2a 65 72 72 3a 0a 2a 2a 20 6c 65 61 66 57 72  **err:.** leafWr
31a4d 69 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69  iterDestroy(&wri
31a4e 74 65 72 29 3b 0a 2a 2a 20 72 65 74 75 72 6e 20  ter);.** return 
31a4f 72 63 3b 0a 2a 2a 0a 2a 2a 20 6c 65 61 66 57 72  rc;.**.** leafWr
31a50 69 74 65 72 53 74 65 70 28 29 20 6d 61 79 20 77  iterStep() may w
31a51 72 69 74 65 20 61 20 63 6f 6c 6c 65 63 74 65 64  rite a collected
31a52 20 6c 65 61 66 20 6f 75 74 20 74 6f 20 25 5f 73   leaf out to %_s
31a53 65 67 6d 65 6e 74 73 2e 0a 2a 2a 20 6c 65 61 66  egments..** leaf
31a54 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 29  WriterFinalize()
31a55 20 66 69 6e 69 73 68 65 73 20 77 72 69 74 69 6e   finishes writin
31a56 67 20 61 6e 79 20 62 75 66 66 65 72 65 64 20 64  g any buffered d
31a57 61 74 61 20 61 6e 64 20 73 74 6f 72 65 73 0a 2a  ata and stores.*
31a58 2a 20 61 20 72 6f 6f 74 20 6e 6f 64 65 20 69 6e  * a root node in
31a59 20 25 5f 73 65 67 64 69 72 2e 20 20 6c 65 61 66   %_segdir.  leaf
31a5a 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 29 20  WriterDestroy() 
31a5b 66 72 65 65 73 20 61 6c 6c 20 62 75 66 66 65 72  frees all buffer
31a5c 73 20 61 6e 64 0a 2a 2a 20 49 6e 74 65 72 69 6f  s and.** Interio
31a5d 72 57 72 69 74 65 72 73 20 61 6c 6c 6f 63 61 74  rWriters allocat
31a5e 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 77 72  ed as part of wr
31a5f 69 74 69 6e 67 20 74 68 69 73 20 73 65 67 6d 65  iting this segme
31a60 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73  nt..**.** TODO(s
31a61 68 65 73 73 29 20 44 6f 63 75 6d 65 6e 74 20 6c  hess) Document l
31a62 65 61 66 57 72 69 74 65 72 53 74 65 70 4d 65 72  eafWriterStepMer
31a63 67 65 28 29 2e 0a 2a 2f 0a 0a 2f 2a 20 50 75 74  ge()..*/../* Put
31a64 20 74 65 72 6d 73 20 77 69 74 68 20 64 61 74 61   terms with data
31a65 20 74 68 69 73 20 62 69 67 20 69 6e 20 74 68 65   this big in the
31a66 69 72 20 6f 77 6e 20 62 6c 6f 63 6b 2e 20 2a 2f  ir own block. */
31a67 0a 23 64 65 66 69 6e 65 20 53 54 41 4e 44 41 4c  .#define STANDAL
31a68 4f 4e 45 5f 4d 49 4e 20 31 30 32 34 0a 0a 2f 2a  ONE_MIN 1024../*
31a69 20 4b 65 65 70 20 6c 65 61 66 20 62 6c 6f 63 6b   Keep leaf block
31a6a 73 20 62 65 6c 6f 77 20 74 68 69 73 20 73 69 7a  s below this siz
31a6b 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 45  e. */.#define LE
31a6c 41 46 5f 4d 41 58 20 32 30 34 38 0a 0a 74 79 70  AF_MAX 2048..typ
31a6d 65 64 65 66 20 73 74 72 75 63 74 20 4c 65 61 66  edef struct Leaf
31a6e 57 72 69 74 65 72 20 7b 0a 20 20 69 6e 74 20 69  Writer {.  int i
31a6f 4c 65 76 65 6c 3b 0a 20 20 69 6e 74 20 69 64 78  Level;.  int idx
31a70 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
31a71 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3b 20   iStartBlockid; 
31a72 20 20 20 20 2f 2a 20 6e 65 65 64 65 64 20 74 6f      /* needed to
31a73 20 63 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74   create the root
31a74 20 69 6e 66 6f 20 2a 2f 0a 20 20 73 71 6c 69 74   info */.  sqlit
31a75 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63  e_int64 iEndBloc
31a76 6b 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 77 68  kid;       /* wh
31a77 65 6e 20 77 65 27 72 65 20 64 6f 6e 65 20 77 72  en we're done wr
31a78 69 74 69 6e 67 2e 20 2a 2f 0a 0a 20 20 44 61 74  iting. */..  Dat
31a79 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20  aBuffer term;   
31a7a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31a7b 70 72 65 76 69 6f 75 73 20 65 6e 63 6f 64 65 64  previous encoded
31a7c 20 74 65 72 6d 20 2a 2f 0a 20 20 44 61 74 61 42   term */.  DataB
31a7d 75 66 66 65 72 20 64 61 74 61 3b 20 20 20 20 20  uffer data;     
31a7e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65 6e             /* en
31a7f 63 6f 64 69 6e 67 20 62 75 66 66 65 72 20 2a 2f  coding buffer */
31a80 0a 0a 20 20 2f 2a 20 62 79 74 65 73 20 6f 66 20  ..  /* bytes of 
31a81 66 69 72 73 74 20 74 65 72 6d 20 69 6e 20 74 68  first term in th
31a82 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 20 77  e current node w
31a83 68 69 63 68 20 64 69 73 74 69 6e 67 75 69 73 68  hich distinguish
31a84 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 65 72  es that.  ** ter
31a85 6d 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20  m from the last 
31a86 74 65 72 6d 20 6f 66 20 74 68 65 20 70 72 65 76  term of the prev
31a87 69 6f 75 73 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a  ious node..  */.
31a88 20 20 69 6e 74 20 6e 54 65 72 6d 44 69 73 74 69    int nTermDisti
31a89 6e 63 74 3b 0a 0a 20 20 49 6e 74 65 72 69 6f 72  nct;..  Interior
31a8a 57 72 69 74 65 72 20 70 61 72 65 6e 74 57 72 69  Writer parentWri
31a8b 74 65 72 3b 20 20 20 20 2f 2a 20 69 66 20 77 65  ter;    /* if we
31a8c 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69   overflow */.  i
31a8d 6e 74 20 68 61 73 5f 70 61 72 65 6e 74 3b 0a 7d  nt has_parent;.}
31a8e 20 4c 65 61 66 57 72 69 74 65 72 3b 0a 0a 73 74   LeafWriter;..st
31a8f 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66 57 72  atic void leafWr
31a90 69 74 65 72 49 6e 69 74 28 69 6e 74 20 69 4c 65  iterInit(int iLe
31a91 76 65 6c 2c 20 69 6e 74 20 69 64 78 2c 20 4c 65  vel, int idx, Le
31a92 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  afWriter *pWrite
31a93 72 29 7b 0a 20 20 43 4c 45 41 52 28 70 57 72 69  r){.  CLEAR(pWri
31a94 74 65 72 29 3b 0a 20 20 70 57 72 69 74 65 72 2d  ter);.  pWriter-
31a95 3e 69 4c 65 76 65 6c 20 3d 20 69 4c 65 76 65 6c  >iLevel = iLevel
31a96 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 64 78  ;.  pWriter->idx
31a97 20 3d 20 69 64 78 3b 0a 0a 20 20 64 61 74 61 42   = idx;..  dataB
31a98 75 66 66 65 72 49 6e 69 74 28 26 70 57 72 69 74  ufferInit(&pWrit
31a99 65 72 2d 3e 74 65 72 6d 2c 20 33 32 29 3b 0a 0a  er->term, 32);..
31a9a 20 20 2f 2a 20 53 74 61 72 74 20 6f 75 74 20 77    /* Start out w
31a9b 69 74 68 20 61 20 72 65 61 73 6f 6e 61 62 6c 79  ith a reasonably
31a9c 20 73 69 7a 65 64 20 62 6c 6f 63 6b 2c 20 74 68   sized block, th
31a9d 6f 75 67 68 20 69 74 20 63 61 6e 20 67 72 6f 77  ough it can grow
31a9e 2e 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66 65  . */.  dataBuffe
31a9f 72 49 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e  rInit(&pWriter->
31aa0 64 61 74 61 2c 20 4c 45 41 46 5f 4d 41 58 29 3b  data, LEAF_MAX);
31aa1 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  .}..#ifndef NDEB
31aa2 55 47 0a 2f 2a 20 56 65 72 69 66 79 20 74 68 61  UG./* Verify tha
31aa3 74 20 74 68 65 20 64 61 74 61 20 69 73 20 72 65  t the data is re
31aa4 61 64 61 62 6c 65 20 61 73 20 61 20 6c 65 61 66  adable as a leaf
31aa5 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74 69 63   node. */.static
31aa6 20 76 6f 69 64 20 6c 65 61 66 4e 6f 64 65 56 61   void leafNodeVa
31aa7 6c 69 64 61 74 65 28 63 6f 6e 73 74 20 63 68 61  lidate(const cha
31aa8 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
31aa9 61 74 61 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 69  ata){.  int n, i
31aaa 44 75 6d 6d 79 3b 0a 0a 20 20 69 66 28 20 6e 44  Dummy;..  if( nD
31aab 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ata==0 ) return;
31aac 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61  .  assert( nData
31aad 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
31aae 70 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 61 73  pData!=0 );.  as
31aaf 73 65 72 74 28 20 70 44 61 74 61 2b 6e 44 61 74  sert( pData+nDat
31ab0 61 3e 70 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a  a>pData );..  /*
31ab1 20 4d 75 73 74 20 6c 65 61 64 20 77 69 74 68 20   Must lead with 
31ab2 61 20 76 61 72 69 6e 74 28 30 29 20 2a 2f 0a 20  a varint(0) */. 
31ab3 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
31ab4 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
31ab5 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
31ab6 69 44 75 6d 6d 79 3d 3d 30 20 29 3b 0a 20 20 61  iDummy==0 );.  a
31ab7 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
31ab8 61 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20  assert( n<nData 
31ab9 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b  );.  pData += n;
31aba 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a  .  nData -= n;..
31abb 20 20 2f 2a 20 4c 65 61 64 69 6e 67 20 74 65 72    /* Leading ter
31abc 6d 20 6c 65 6e 67 74 68 20 61 6e 64 20 64 61 74  m length and dat
31abd 61 20 6d 75 73 74 20 66 69 74 20 69 6e 20 62 75  a must fit in bu
31abe 66 66 65 72 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66  ffer. */.  n = f
31abf 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
31ac0 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
31ac1 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b    assert( n>0 );
31ac2 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d  .  assert( iDumm
31ac3 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  y>0 );.  assert(
31ac4 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20   n+iDummy>0 );. 
31ac5 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d   assert( n+iDumm
31ac6 79 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61  y<nData );.  pDa
31ac7 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a  ta += n+iDummy;.
31ac8 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75    nData -= n+iDu
31ac9 6d 6d 79 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64 69  mmy;..  /* Leadi
31aca 6e 67 20 74 65 72 6d 27 73 20 64 6f 63 6c 69 73  ng term's doclis
31acb 74 20 6c 65 6e 67 74 68 20 61 6e 64 20 64 61 74  t length and dat
31acc 61 20 6d 75 73 74 20 66 69 74 2e 20 2a 2f 0a 20  a must fit. */. 
31acd 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
31ace 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
31acf 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
31ad0 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  n>0 );.  assert(
31ad1 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61   iDummy>0 );.  a
31ad2 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e  ssert( n+iDummy>
31ad3 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
31ad4 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61 20 29  +iDummy<=nData )
31ad5 3b 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44  ;.  ASSERT_VALID
31ad6 5f 44 4f 43 4c 49 53 54 28 44 4c 5f 44 45 46 41  _DOCLIST(DL_DEFA
31ad7 55 4c 54 2c 20 70 44 61 74 61 2b 6e 2c 20 69 44  ULT, pData+n, iD
31ad8 75 6d 6d 79 2c 20 4e 55 4c 4c 29 3b 0a 20 20 70  ummy, NULL);.  p
31ad9 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79  Data += n+iDummy
31ada 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69  ;.  nData -= n+i
31adb 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 20 56 65 72  Dummy;..  /* Ver
31adc 69 66 79 20 74 68 61 74 20 74 72 61 69 6c 69 6e  ify that trailin
31add 67 20 74 65 72 6d 73 20 61 6e 64 20 64 6f 63 6c  g terms and docl
31ade 69 73 74 73 20 61 6c 73 6f 20 61 72 65 20 72 65  ists also are re
31adf 61 64 61 62 6c 65 2e 20 2a 2f 0a 20 20 77 68 69  adable. */.  whi
31ae0 6c 65 28 20 6e 44 61 74 61 21 3d 30 20 29 7b 0a  le( nData!=0 ){.
31ae1 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56      n = fts3GetV
31ae2 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26  arint32(pData, &
31ae3 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73  iDummy);.    ass
31ae4 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20  ert( n>0 );.    
31ae5 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 3d  assert( iDummy>=
31ae6 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
31ae7 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20 20   n<nData );.    
31ae8 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 20 20  pData += n;.    
31ae9 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 20 20 20 20  nData -= n;.    
31aea 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
31aeb 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d  t32(pData, &iDum
31aec 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  my);.    assert(
31aed 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65   n>0 );.    asse
31aee 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  rt( iDummy>0 );.
31aef 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44      assert( n+iD
31af0 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61 73  ummy>0 );.    as
31af1 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 6e  sert( n+iDummy<n
31af2 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44 61 74  Data );.    pDat
31af3 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a += n+iDummy;. 
31af4 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44     nData -= n+iD
31af5 75 6d 6d 79 3b 0a 0a 20 20 20 20 6e 20 3d 20 66  ummy;..    n = f
31af6 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
31af7 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
31af8 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20      assert( n>0 
31af9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
31afa 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61  Dummy>0 );.    a
31afb 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e  ssert( n+iDummy>
31afc 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
31afd 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61   n+iDummy<=nData
31afe 20 29 3b 0a 20 20 20 20 41 53 53 45 52 54 5f 56   );.    ASSERT_V
31aff 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44 4c 5f  ALID_DOCLIST(DL_
31b00 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2b 6e  DEFAULT, pData+n
31b01 2c 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c 29 3b  , iDummy, NULL);
31b02 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b  .    pData += n+
31b03 69 44 75 6d 6d 79 3b 0a 20 20 20 20 6e 44 61 74  iDummy;.    nDat
31b04 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a -= n+iDummy;. 
31b05 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53 53   }.}.#define ASS
31b06 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e  ERT_VALID_LEAF_N
31b07 4f 44 45 28 70 2c 20 6e 29 20 6c 65 61 66 4e 6f  ODE(p, n) leafNo
31b08 64 65 56 61 6c 69 64 61 74 65 28 70 2c 20 6e 29  deValidate(p, n)
31b09 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 41  .#else.#define A
31b0a 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46  SSERT_VALID_LEAF
31b0b 5f 4e 4f 44 45 28 70 2c 20 6e 29 20 61 73 73 65  _NODE(p, n) asse
31b0c 72 74 28 20 31 20 29 0a 23 65 6e 64 69 66 0a 0a  rt( 1 ).#endif..
31b0d 2f 2a 20 46 6c 75 73 68 20 74 68 65 20 63 75 72  /* Flush the cur
31b0e 72 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 20 74  rent leaf node t
31b0f 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e  o %_segments, an
31b10 64 20 61 64 64 69 6e 67 20 74 68 65 20 72 65 73  d adding the res
31b11 75 6c 74 69 6e 67 0a 2a 2a 20 62 6c 6f 63 6b 69  ulting.** blocki
31b12 64 20 61 6e 64 20 74 68 65 20 73 74 61 72 74 69  d and the starti
31b13 6e 67 20 74 65 72 6d 20 74 6f 20 74 68 65 20 69  ng term to the i
31b14 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 77 68 69  nterior node whi
31b15 63 68 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 61  ch will.** conta
31b16 69 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  in it..*/.static
31b17 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 49   int leafWriterI
31b18 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 66 75 6c  nternalFlush(ful
31b19 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c  ltext_vtab *v, L
31b1a 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74  eafWriter *pWrit
31b1b 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
31b1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31b1d 20 20 20 20 20 20 20 69 6e 74 20 69 44 61 74 61         int iData
31b1e 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20  , int nData){.  
31b1f 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c  sqlite_int64 iBl
31b20 6f 63 6b 69 64 20 3d 20 30 3b 0a 20 20 63 6f 6e  ockid = 0;.  con
31b21 73 74 20 63 68 61 72 20 2a 70 53 74 61 72 74 69  st char *pStarti
31b22 6e 67 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e 53  ngTerm;.  int nS
31b23 74 61 72 74 69 6e 67 54 65 72 6d 2c 20 72 63 2c  tartingTerm, rc,
31b24 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 68   n;..  /* Must h
31b25 61 76 65 20 74 68 65 20 6c 65 61 64 69 6e 67 20  ave the leading 
31b26 76 61 72 69 6e 74 28 30 29 20 66 6c 61 67 2c 20  varint(0) flag, 
31b27 70 6c 75 73 20 61 74 20 6c 65 61 73 74 20 73 6f  plus at least so
31b28 6d 65 0a 20 20 2a 2a 20 76 61 6c 69 64 2d 6c 6f  me.  ** valid-lo
31b29 6f 6b 69 6e 67 20 64 61 74 61 2e 0a 20 20 2a 2f  oking data..  */
31b2a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61  .  assert( nData
31b2b 3e 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >2 );.  assert( 
31b2c 69 44 61 74 61 3e 3d 30 20 29 3b 0a 20 20 61 73  iData>=0 );.  as
31b2d 73 65 72 74 28 20 69 44 61 74 61 2b 6e 44 61 74  sert( iData+nDat
31b2e 61 3c 3d 70 57 72 69 74 65 72 2d 3e 64 61 74 61  a<=pWriter->data
31b2f 2e 6e 44 61 74 61 20 29 3b 0a 20 20 41 53 53 45  .nData );.  ASSE
31b30 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f  RT_VALID_LEAF_NO
31b31 44 45 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  DE(pWriter->data
31b32 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e 44  .pData+iData, nD
31b33 61 74 61 29 3b 0a 0a 20 20 72 63 20 3d 20 62 6c  ata);..  rc = bl
31b34 6f 63 6b 5f 69 6e 73 65 72 74 28 76 2c 20 70 57  ock_insert(v, pW
31b35 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
31b36 61 2b 69 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  a+iData, nData, 
31b37 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66  &iBlockid);.  if
31b38 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
31b39 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61  ) return rc;.  a
31b3a 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64 21  ssert( iBlockid!
31b3b 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 63 6f  =0 );..  /* Reco
31b3c 6e 73 74 72 75 63 74 20 74 68 65 20 66 69 72 73  nstruct the firs
31b3d 74 20 74 65 72 6d 20 69 6e 20 74 68 65 20 6c 65  t term in the le
31b3e 61 66 20 66 6f 72 20 70 75 72 70 6f 73 65 73 20  af for purposes 
31b3f 6f 66 20 62 75 69 6c 64 69 6e 67 0a 20 20 2a 2a  of building.  **
31b40 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f   the interior no
31b41 64 65 2e 0a 20 20 2a 2f 0a 20 20 6e 20 3d 20 66  de..  */.  n = f
31b42 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
31b43 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
31b44 74 61 2b 69 44 61 74 61 2b 31 2c 20 26 6e 53 74  ta+iData+1, &nSt
31b45 61 72 74 69 6e 67 54 65 72 6d 29 3b 0a 20 20 70  artingTerm);.  p
31b46 53 74 61 72 74 69 6e 67 54 65 72 6d 20 3d 20 70  StartingTerm = p
31b47 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
31b48 74 61 2b 69 44 61 74 61 2b 31 2b 6e 3b 0a 20 20  ta+iData+1+n;.  
31b49 61 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d  assert( pWriter-
31b4a 3e 64 61 74 61 2e 6e 44 61 74 61 3e 69 44 61 74  >data.nData>iDat
31b4b 61 2b 31 2b 6e 2b 6e 53 74 61 72 74 69 6e 67 54  a+1+n+nStartingT
31b4c 65 72 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28  erm );.  assert(
31b4d 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44   pWriter->nTermD
31b4e 69 73 74 69 6e 63 74 3e 30 20 29 3b 0a 20 20 61  istinct>0 );.  a
31b4f 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
31b50 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3c 3d 6e  nTermDistinct<=n
31b51 53 74 61 72 74 69 6e 67 54 65 72 6d 20 29 3b 0a  StartingTerm );.
31b52 20 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 20    nStartingTerm 
31b53 3d 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d  = pWriter->nTerm
31b54 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 69 66 28  Distinct;..  if(
31b55 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61   pWriter->has_pa
31b56 72 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 65  rent ){.    inte
31b57 72 69 6f 72 57 72 69 74 65 72 41 70 70 65 6e 64  riorWriterAppend
31b58 28 26 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e  (&pWriter->paren
31b59 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  tWriter,.       
31b5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31b5b 20 20 70 53 74 61 72 74 69 6e 67 54 65 72 6d 2c    pStartingTerm,
31b5c 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 2c 20   nStartingTerm, 
31b5d 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 7d 65 6c  iBlockid);.  }el
31b5e 73 65 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72  se{.    interior
31b5f 57 72 69 74 65 72 49 6e 69 74 28 31 2c 20 70 53  WriterInit(1, pS
31b60 74 61 72 74 69 6e 67 54 65 72 6d 2c 20 6e 53 74  tartingTerm, nSt
31b61 61 72 74 69 6e 67 54 65 72 6d 2c 20 69 42 6c 6f  artingTerm, iBlo
31b62 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ckid,.          
31b63 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 57               &pW
31b64 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
31b65 74 65 72 29 3b 0a 20 20 20 20 70 57 72 69 74 65  ter);.    pWrite
31b66 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 3d 20  r->has_parent = 
31b67 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 61  1;.  }..  /* Tra
31b68 63 6b 20 74 68 65 20 73 70 61 6e 20 6f 66 20 74  ck the span of t
31b69 68 69 73 20 73 65 67 6d 65 6e 74 27 73 20 6c 65  his segment's le
31b6a 61 66 20 6e 6f 64 65 73 2e 20 2a 2f 0a 20 20 69  af nodes. */.  i
31b6b 66 28 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64  f( pWriter->iEnd
31b6c 42 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b 0a 20 20  Blockid==0 ){.  
31b6d 20 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42    pWriter->iEndB
31b6e 6c 6f 63 6b 69 64 20 3d 20 70 57 72 69 74 65 72  lockid = pWriter
31b6f 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20  ->iStartBlockid 
31b70 3d 20 69 42 6c 6f 63 6b 69 64 3b 0a 20 20 7d 65  = iBlockid;.  }e
31b71 6c 73 65 7b 0a 20 20 20 20 70 57 72 69 74 65 72  lse{.    pWriter
31b72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 2b 2b 3b  ->iEndBlockid++;
31b73 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 42 6c  .    assert( iBl
31b74 6f 63 6b 69 64 3d 3d 70 57 72 69 74 65 72 2d 3e  ockid==pWriter->
31b75 69 45 6e 64 42 6c 6f 63 6b 69 64 20 29 3b 0a 20  iEndBlockid );. 
31b76 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
31b77 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63  ITE_OK;.}.static
31b78 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 46   int leafWriterF
31b79 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f 76 74  lush(fulltext_vt
31b7a 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65  ab *v, LeafWrite
31b7b 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69  r *pWriter){.  i
31b7c 6e 74 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74  nt rc = leafWrit
31b7d 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28  erInternalFlush(
31b7e 76 2c 20 70 57 72 69 74 65 72 2c 20 30 2c 20 70  v, pWriter, 0, p
31b7f 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
31b80 74 61 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ta);.  if( rc!=S
31b81 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
31b82 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 52 65 2d 69  n rc;..  /* Re-i
31b83 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 75  nitialize the ou
31b84 74 70 75 74 20 62 75 66 66 65 72 2e 20 2a 2f 0a  tput buffer. */.
31b85 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65    dataBufferRese
31b86 74 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61  t(&pWriter->data
31b87 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  );..  return SQL
31b88 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 46 65  ITE_OK;.}../* Fe
31b89 74 63 68 20 74 68 65 20 72 6f 6f 74 20 69 6e 66  tch the root inf
31b8a 6f 20 66 6f 72 20 74 68 65 20 73 65 67 6d 65 6e  o for the segmen
31b8b 74 2e 20 20 49 66 20 74 68 65 20 65 6e 74 69 72  t.  If the entir
31b8c 65 20 6c 65 61 66 20 66 69 74 73 0a 2a 2a 20 77  e leaf fits.** w
31b8d 69 74 68 69 6e 20 52 4f 4f 54 5f 4d 41 58 2c 20  ithin ROOT_MAX, 
31b8e 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65 20  then it will be 
31b8f 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c  returned directl
31b90 79 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74 0a  y, otherwise it.
31b91 2a 2a 20 77 69 6c 6c 20 62 65 20 66 6c 75 73 68  ** will be flush
31b92 65 64 20 61 6e 64 20 74 68 65 20 72 6f 6f 74 20  ed and the root 
31b93 69 6e 66 6f 20 77 69 6c 6c 20 62 65 20 72 65 74  info will be ret
31b94 75 72 6e 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a  urned from the.*
31b95 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e  * interior node.
31b96 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20    *piEndBlockid 
31b97 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 62 6c  is set to the bl
31b98 6f 63 6b 69 64 20 6f 66 20 74 68 65 20 6c 61 73  ockid of the las
31b99 74 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6f 72  t.** interior or
31b9a 20 6c 65 61 66 20 6e 6f 64 65 20 77 72 69 74 74   leaf node writt
31b9b 65 6e 20 74 6f 20 64 69 73 6b 20 28 30 20 69 66  en to disk (0 if
31b9c 20 6e 6f 6e 65 20 61 72 65 20 77 72 69 74 74 65   none are writte
31b9d 6e 20 61 74 0a 2a 2a 20 61 6c 6c 29 2e 0a 2a 2f  n at.** all)..*/
31b9e 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66  .static int leaf
31b9f 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 66  WriterRootInfo(f
31ba0 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
31ba1 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72   LeafWriter *pWr
31ba2 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  iter,.          
31ba3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ba4 20 20 20 20 63 68 61 72 20 2a 2a 70 70 52 6f 6f      char **ppRoo
31ba5 74 49 6e 66 6f 2c 20 69 6e 74 20 2a 70 6e 52 6f  tInfo, int *pnRo
31ba6 6f 74 49 6e 66 6f 2c 0a 20 20 20 20 20 20 20 20  otInfo,.        
31ba7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ba8 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
31ba9 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  64 *piEndBlockid
31baa 29 7b 0a 20 20 2f 2a 20 77 65 20 63 61 6e 20 66  ){.  /* we can f
31bab 69 74 20 74 68 65 20 73 65 67 6d 65 6e 74 20 65  it the segment e
31bac 6e 74 69 72 65 6c 79 20 69 6e 6c 69 6e 65 20 2a  ntirely inline *
31bad 2f 0a 20 20 69 66 28 20 21 70 57 72 69 74 65 72  /.  if( !pWriter
31bae 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 26 26 20  ->has_parent && 
31baf 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
31bb0 61 74 61 3c 52 4f 4f 54 5f 4d 41 58 20 29 7b 0a  ata<ROOT_MAX ){.
31bb1 20 20 20 20 2a 70 70 52 6f 6f 74 49 6e 66 6f 20      *ppRootInfo 
31bb2 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  = pWriter->data.
31bb3 70 44 61 74 61 3b 0a 20 20 20 20 2a 70 6e 52 6f  pData;.    *pnRo
31bb4 6f 74 49 6e 66 6f 20 3d 20 70 57 72 69 74 65 72  otInfo = pWriter
31bb5 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20 20  ->data.nData;.  
31bb6 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20    *piEndBlockid 
31bb7 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
31bb8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
31bb9 20 20 2f 2a 20 46 6c 75 73 68 20 72 65 6d 61 69    /* Flush remai
31bba 6e 69 6e 67 20 6c 65 61 66 20 64 61 74 61 2e 20  ning leaf data. 
31bbb 2a 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72  */.  if( pWriter
31bbc 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e 30 20 29  ->data.nData>0 )
31bbd 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 6c  {.    int rc = l
31bbe 65 61 66 57 72 69 74 65 72 46 6c 75 73 68 28 76  eafWriterFlush(v
31bbf 2c 20 70 57 72 69 74 65 72 29 3b 0a 20 20 20 20  , pWriter);.    
31bc0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
31bc1 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
31bc2 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 6d 75 73 74   }..  /* We must
31bc3 20 68 61 76 65 20 66 6c 75 73 68 65 64 20 61 20   have flushed a 
31bc4 6c 65 61 66 20 61 74 20 73 6f 6d 65 20 70 6f 69  leaf at some poi
31bc5 6e 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  nt. */.  assert(
31bc6 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61   pWriter->has_pa
31bc7 72 65 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20 54 65  rent );..  /* Te
31bc8 6e 61 74 69 76 65 6c 79 20 73 65 74 20 74 68 65  natively set the
31bc9 20 65 6e 64 20 6c 65 61 66 20 62 6c 6f 63 6b 69   end leaf blocki
31bca 64 20 61 73 20 74 68 65 20 65 6e 64 20 62 6c 6f  d as the end blo
31bcb 63 6b 69 64 2e 20 20 49 66 20 74 68 65 0a 20 20  ckid.  If the.  
31bcc 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ** interior node
31bcd 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64   can be returned
31bce 20 69 6e 6c 69 6e 65 2c 20 74 68 69 73 20 77 69   inline, this wi
31bcf 6c 6c 20 62 65 20 74 68 65 20 66 69 6e 61 6c 0a  ll be the final.
31bd0 20 20 2a 2a 20 62 6c 6f 63 6b 69 64 2c 20 6f 74    ** blockid, ot
31bd1 68 65 72 77 69 73 65 20 69 74 20 77 69 6c 6c 20  herwise it will 
31bd2 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 62  be overwritten b
31bd3 79 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 57  y.  ** interiorW
31bd4 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 29 2e  riterRootInfo().
31bd5 0a 20 20 2a 2f 0a 20 20 2a 70 69 45 6e 64 42 6c  .  */.  *piEndBl
31bd6 6f 63 6b 69 64 20 3d 20 70 57 72 69 74 65 72 2d  ockid = pWriter-
31bd7 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a 0a 20  >iEndBlockid;.. 
31bd8 20 72 65 74 75 72 6e 20 69 6e 74 65 72 69 6f 72   return interior
31bd9 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76  WriterRootInfo(v
31bda 2c 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72 65  , &pWriter->pare
31bdb 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  ntWriter,.      
31bdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31bdd 20 20 20 20 20 20 20 20 20 20 70 70 52 6f 6f 74            ppRoot
31bde 49 6e 66 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66 6f  Info, pnRootInfo
31bdf 2c 20 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b  , piEndBlockid);
31be0 0a 7d 0a 0a 2f 2a 20 43 6f 6c 6c 65 63 74 20 74  .}../* Collect t
31be1 68 65 20 72 6f 6f 74 49 6e 66 6f 20 64 61 74 61  he rootInfo data
31be2 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e   and store it in
31be3 74 6f 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64  to the segment d
31be4 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 20 54 68 69  irectory..** Thi
31be5 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74  s has the effect
31be6 20 6f 66 20 66 6c 75 73 68 69 6e 67 20 74 68 65   of flushing the
31be7 20 73 65 67 6d 65 6e 74 27 73 20 6c 65 61 66 20   segment's leaf 
31be8 64 61 74 61 20 74 6f 0a 2a 2a 20 25 5f 73 65 67  data to.** %_seg
31be9 6d 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f 20  ments, and also 
31bea 66 6c 75 73 68 69 6e 67 20 61 6e 79 20 69 6e 74  flushing any int
31beb 65 72 69 6f 72 20 6e 6f 64 65 73 20 74 6f 20 25  erior nodes to %
31bec 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74  _segments..*/.st
31bed 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69  atic int leafWri
31bee 74 65 72 46 69 6e 61 6c 69 7a 65 28 66 75 6c 6c  terFinalize(full
31bef 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65  text_vtab *v, Le
31bf0 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  afWriter *pWrite
31bf1 72 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  r){.  sqlite_int
31bf2 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a  64 iEndBlockid;.
31bf3 20 20 63 68 61 72 20 2a 70 52 6f 6f 74 49 6e 66    char *pRootInf
31bf4 6f 3b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 52 6f  o;.  int rc, nRo
31bf5 6f 74 49 6e 66 6f 3b 0a 0a 20 20 72 63 20 3d 20  otInfo;..  rc = 
31bf6 6c 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49 6e  leafWriterRootIn
31bf7 66 6f 28 76 2c 20 70 57 72 69 74 65 72 2c 20 26  fo(v, pWriter, &
31bf8 70 52 6f 6f 74 49 6e 66 6f 2c 20 26 6e 52 6f 6f  pRootInfo, &nRoo
31bf9 74 49 6e 66 6f 2c 20 26 69 45 6e 64 42 6c 6f 63  tInfo, &iEndBloc
31bfa 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  kid);.  if( rc!=
31bfb 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
31bfc 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e  rn rc;..  /* Don
31bfd 27 74 20 62 6f 74 68 65 72 20 73 74 6f 72 69 6e  't bother storin
31bfe 67 20 61 6e 20 65 6e 74 69 72 65 6c 79 20 65 6d  g an entirely em
31bff 70 74 79 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a  pty segment. */.
31c00 20 20 69 66 28 20 69 45 6e 64 42 6c 6f 63 6b 69    if( iEndBlocki
31c01 64 3d 3d 30 20 26 26 20 6e 52 6f 6f 74 49 6e 66  d==0 && nRootInf
31c02 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  o==0 ) return SQ
31c03 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72 65 74 75  LITE_OK;..  retu
31c04 72 6e 20 73 65 67 64 69 72 5f 73 65 74 28 76 2c  rn segdir_set(v,
31c05 20 70 57 72 69 74 65 72 2d 3e 69 4c 65 76 65 6c   pWriter->iLevel
31c06 2c 20 70 57 72 69 74 65 72 2d 3e 69 64 78 2c 0a  , pWriter->idx,.
31c07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c08 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 53 74      pWriter->iSt
31c09 61 72 74 42 6c 6f 63 6b 69 64 2c 20 70 57 72 69  artBlockid, pWri
31c0a 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64  ter->iEndBlockid
31c0b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
31c0c 20 20 20 20 20 20 69 45 6e 64 42 6c 6f 63 6b 69        iEndBlocki
31c0d 64 2c 20 70 52 6f 6f 74 49 6e 66 6f 2c 20 6e 52  d, pRootInfo, nR
31c0e 6f 6f 74 49 6e 66 6f 29 3b 0a 7d 0a 0a 73 74 61  ootInfo);.}..sta
31c0f 74 69 63 20 76 6f 69 64 20 6c 65 61 66 57 72 69  tic void leafWri
31c10 74 65 72 44 65 73 74 72 6f 79 28 4c 65 61 66 57  terDestroy(LeafW
31c11 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b  riter *pWriter){
31c12 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
31c13 68 61 73 5f 70 61 72 65 6e 74 20 29 20 69 6e 74  has_parent ) int
31c14 65 72 69 6f 72 57 72 69 74 65 72 44 65 73 74 72  eriorWriterDestr
31c15 6f 79 28 26 70 57 72 69 74 65 72 2d 3e 70 61 72  oy(&pWriter->par
31c16 65 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 64 61  entWriter);.  da
31c17 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
31c18 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29 3b  &pWriter->term);
31c19 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
31c1a 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e 64  troy(&pWriter->d
31c1b 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20 45 6e 63 6f  ata);.}../* Enco
31c1c 64 65 20 61 20 74 65 72 6d 20 69 6e 74 6f 20 74  de a term into t
31c1d 68 65 20 6c 65 61 66 57 72 69 74 65 72 2c 20 64  he leafWriter, d
31c1e 65 6c 74 61 2d 65 6e 63 6f 64 69 6e 67 20 61 73  elta-encoding as
31c1f 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2a   appropriate..**
31c20 20 52 65 74 75 72 6e 73 20 74 68 65 20 6c 65 6e   Returns the len
31c21 67 74 68 20 6f 66 20 74 68 65 20 6e 65 77 20 74  gth of the new t
31c22 65 72 6d 20 77 68 69 63 68 20 64 69 73 74 69 6e  erm which distin
31c23 67 75 69 73 68 65 73 20 69 74 20 66 72 6f 6d 20  guishes it from 
31c24 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20  the.** previous 
31c25 74 65 72 6d 2c 20 77 68 69 63 68 20 63 61 6e 20  term, which can 
31c26 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 20 6e  be used to set n
31c27 54 65 72 6d 44 69 73 74 69 6e 63 74 20 77 68 65  TermDistinct whe
31c28 6e 20 61 20 6e 6f 64 65 0a 2a 2a 20 62 6f 75 6e  n a node.** boun
31c29 64 61 72 79 20 69 73 20 63 72 6f 73 73 65 64 2e  dary is crossed.
31c2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
31c2b 65 61 66 57 72 69 74 65 72 45 6e 63 6f 64 65 54  eafWriterEncodeT
31c2c 65 72 6d 28 4c 65 61 66 57 72 69 74 65 72 20 2a  erm(LeafWriter *
31c2d 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  pWriter,.       
31c2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c2f 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
31c30 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
31c31 6e 54 65 72 6d 29 7b 0a 20 20 63 68 61 72 20 63  nTerm){.  char c
31c32 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49  [VARINT_MAX+VARI
31c33 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e  NT_MAX];.  int n
31c34 2c 20 6e 50 72 65 66 69 78 20 3d 20 30 3b 0a 0a  , nPrefix = 0;..
31c35 20 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 3e    assert( nTerm>
31c36 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 50  0 );.  while( nP
31c37 72 65 66 69 78 3c 70 57 72 69 74 65 72 2d 3e 74  refix<pWriter->t
31c38 65 72 6d 2e 6e 44 61 74 61 20 26 26 0a 20 20 20  erm.nData &&.   
31c39 20 20 20 20 20 20 70 54 65 72 6d 5b 6e 50 72 65        pTerm[nPre
31c3a 66 69 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e 74  fix]==pWriter->t
31c3b 65 72 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66 69  erm.pData[nPrefi
31c3c 78 5d 20 29 7b 0a 20 20 20 20 6e 50 72 65 66 69  x] ){.    nPrefi
31c3d 78 2b 2b 3b 0a 20 20 20 20 2f 2a 20 46 61 69 6c  x++;.    /* Fail
31c3e 69 6e 67 20 74 68 69 73 20 69 6d 70 6c 69 65 73  ing this implies
31c3f 20 74 68 61 74 20 74 68 65 20 74 65 72 6d 73 20   that the terms 
31c40 77 65 72 65 6e 27 74 20 69 6e 20 6f 72 64 65 72  weren't in order
31c41 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
31c42 20 6e 50 72 65 66 69 78 3c 6e 54 65 72 6d 20 29   nPrefix<nTerm )
31c43 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 57 72  ;.  }..  if( pWr
31c44 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
31c45 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 45 6e  ==0 ){.    /* En
31c46 63 6f 64 65 20 74 68 65 20 6e 6f 64 65 20 68 65  code the node he
31c47 61 64 65 72 20 61 6e 64 20 6c 65 61 64 69 6e 67  ader and leading
31c48 20 74 65 72 6d 20 61 73 3a 0a 20 20 20 20 2a 2a   term as:.    **
31c49 20 20 76 61 72 69 6e 74 28 30 29 0a 20 20 20 20    varint(0).    
31c4a 2a 2a 20 20 76 61 72 69 6e 74 28 6e 54 65 72 6d  **  varint(nTerm
31c4b 29 0a 20 20 20 20 2a 2a 20 20 63 68 61 72 20 70  ).    **  char p
31c4c 54 65 72 6d 5b 6e 54 65 72 6d 5d 0a 20 20 20 20  Term[nTerm].    
31c4d 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50  */.    n = fts3P
31c4e 75 74 56 61 72 69 6e 74 28 63 2c 20 27 5c 30 27  utVarint(c, '\0'
31c4f 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  );.    n += fts3
31c50 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e  PutVarint(c+n, n
31c51 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74 61 42  Term);.    dataB
31c52 75 66 66 65 72 41 70 70 65 6e 64 32 28 26 70 57  ufferAppend2(&pW
31c53 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c 20  riter->data, c, 
31c54 6e 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29  n, pTerm, nTerm)
31c55 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
31c56 2a 20 44 65 6c 74 61 2d 65 6e 63 6f 64 65 20 74  * Delta-encode t
31c57 68 65 20 74 65 72 6d 20 61 73 3a 0a 20 20 20 20  he term as:.    
31c58 2a 2a 20 20 76 61 72 69 6e 74 28 6e 50 72 65 66  **  varint(nPref
31c59 69 78 29 0a 20 20 20 20 2a 2a 20 20 76 61 72 69  ix).    **  vari
31c5a 6e 74 28 6e 53 75 66 66 69 78 29 0a 20 20 20 20  nt(nSuffix).    
31c5b 2a 2a 20 20 63 68 61 72 20 70 54 65 72 6d 53 75  **  char pTermSu
31c5c 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 0a 20 20  ffix[nSuffix].  
31c5d 20 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73    */.    n = fts
31c5e 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 50  3PutVarint(c, nP
31c5f 72 65 66 69 78 29 3b 0a 20 20 20 20 6e 20 2b 3d  refix);.    n +=
31c60 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
31c61 2b 6e 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66 69  +n, nTerm-nPrefi
31c62 78 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  x);.    dataBuff
31c63 65 72 41 70 70 65 6e 64 32 28 26 70 57 72 69 74  erAppend2(&pWrit
31c64 65 72 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 2c 20  er->data, c, n, 
31c65 70 54 65 72 6d 2b 6e 50 72 65 66 69 78 2c 20 6e  pTerm+nPrefix, n
31c66 54 65 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a 20  Term-nPrefix);. 
31c67 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 52   }.  dataBufferR
31c68 65 70 6c 61 63 65 28 26 70 57 72 69 74 65 72 2d  eplace(&pWriter-
31c69 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 54  >term, pTerm, nT
31c6a 65 72 6d 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  erm);..  return 
31c6b 6e 50 72 65 66 69 78 2b 31 3b 0a 7d 0a 0a 2f 2a  nPrefix+1;.}../*
31c6c 20 55 73 65 64 20 74 6f 20 61 76 6f 69 64 20 61   Used to avoid a
31c6d 20 6d 65 6d 6d 6f 76 65 20 77 68 65 6e 20 61 20   memmove when a 
31c6e 6c 61 72 67 65 20 61 6d 6f 75 6e 74 20 6f 66 20  large amount of 
31c6f 64 6f 63 6c 69 73 74 20 64 61 74 61 20 69 73 20  doclist data is 
31c70 69 6e 0a 2a 2a 20 74 68 65 20 62 75 66 66 65 72  in.** the buffer
31c71 2e 20 20 54 68 69 73 20 63 6f 6e 73 74 72 75 63  .  This construc
31c72 74 73 20 61 20 6e 6f 64 65 20 61 6e 64 20 74 65  ts a node and te
31c73 72 6d 20 68 65 61 64 65 72 20 62 65 66 6f 72 65  rm header before
31c74 0a 2a 2a 20 69 44 6f 63 6c 69 73 74 44 61 74 61  .** iDoclistData
31c75 20 61 6e 64 20 66 6c 75 73 68 65 73 20 74 68 65   and flushes the
31c76 20 72 65 73 75 6c 74 69 6e 67 20 63 6f 6d 70 6c   resulting compl
31c77 65 74 65 20 6e 6f 64 65 20 75 73 69 6e 67 0a 2a  ete node using.*
31c78 2a 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65  * leafWriterInte
31c79 72 6e 61 6c 46 6c 75 73 68 28 29 2e 0a 2a 2f 0a  rnalFlush()..*/.
31c7a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
31c7b 72 69 74 65 72 49 6e 6c 69 6e 65 46 6c 75 73 68  riterInlineFlush
31c7c 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
31c7d 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70  v, LeafWriter *p
31c7e 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20  Writer,.        
31c7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c80 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
31c81 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
31c82 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20  nTerm,.         
31c83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c84 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 6f 63          int iDoc
31c85 6c 69 73 74 44 61 74 61 29 7b 0a 20 20 63 68 61  listData){.  cha
31c86 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56  r c[VARINT_MAX+V
31c87 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e  ARINT_MAX];.  in
31c88 74 20 69 44 61 74 61 2c 20 6e 20 3d 20 66 74 73  t iData, n = fts
31c89 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 30 29  3PutVarint(c, 0)
31c8a 3b 0a 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74  ;.  n += fts3Put
31c8b 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72  Varint(c+n, nTer
31c8c 6d 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20  m);..  /* There 
31c8d 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
31c8e 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 68 65   room for the he
31c8f 61 64 65 72 2e 20 20 45 76 65 6e 20 69 66 20 70  ader.  Even if p
31c90 54 65 72 6d 20 73 68 61 72 65 64 0a 20 20 2a 2a  Term shared.  **
31c91 20 61 20 73 75 62 73 74 61 6e 74 69 61 6c 20 70   a substantial p
31c92 72 65 66 69 78 20 77 69 74 68 20 74 68 65 20 70  refix with the p
31c93 72 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 74 68  revious term, th
31c94 65 20 65 6e 74 69 72 65 20 70 72 65 66 69 78 0a  e entire prefix.
31c95 20 20 2a 2a 20 63 6f 75 6c 64 20 62 65 20 63 6f    ** could be co
31c96 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 65  nstructed from e
31c97 61 72 6c 69 65 72 20 64 61 74 61 20 69 6e 20 74  arlier data in t
31c98 68 65 20 64 6f 63 6c 69 73 74 2c 20 73 6f 20 74  he doclist, so t
31c99 68 65 72 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  here.  ** should
31c9a 20 62 65 20 72 6f 6f 6d 2e 0a 20 20 2a 2f 0a 20   be room..  */. 
31c9b 20 61 73 73 65 72 74 28 20 69 44 6f 63 6c 69 73   assert( iDoclis
31c9c 74 44 61 74 61 3e 3d 6e 2b 6e 54 65 72 6d 20 29  tData>=n+nTerm )
31c9d 3b 0a 0a 20 20 69 44 61 74 61 20 3d 20 69 44 6f  ;..  iData = iDo
31c9e 63 6c 69 73 74 44 61 74 61 2d 28 6e 2b 6e 54 65  clistData-(n+nTe
31c9f 72 6d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 57  rm);.  memcpy(pW
31ca0 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
31ca1 61 2b 69 44 61 74 61 2c 20 63 2c 20 6e 29 3b 0a  a+iData, c, n);.
31ca2 20 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72    memcpy(pWriter
31ca3 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61  ->data.pData+iDa
31ca4 74 61 2b 6e 2c 20 70 54 65 72 6d 2c 20 6e 54 65  ta+n, pTerm, nTe
31ca5 72 6d 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6c  rm);..  return l
31ca6 65 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61  eafWriterInterna
31ca7 6c 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65  lFlush(v, pWrite
31ca8 72 2c 20 69 44 61 74 61 2c 20 70 57 72 69 74 65  r, iData, pWrite
31ca9 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 44  r->data.nData-iD
31caa 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20 50 75 73 68  ata);.}../* Push
31cab 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 61 6c   pTerm[nTerm] al
31cac 6f 6e 67 20 77 69 74 68 20 74 68 65 20 64 6f 63  ong with the doc
31cad 6c 69 73 74 20 64 61 74 61 20 74 6f 20 74 68 65  list data to the
31cae 20 6c 65 61 66 20 6c 61 79 65 72 20 6f 66 0a 2a   leaf layer of.*
31caf 2a 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f  * %_segments..*/
31cb0 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66  .static int leaf
31cb1 57 72 69 74 65 72 53 74 65 70 4d 65 72 67 65 28  WriterStepMerge(
31cb2 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
31cb3 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
31cb4 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
31cb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31cb6 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
31cb7 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
31cb8 72 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  rm,.            
31cb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31cba 20 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65     DLReader *pRe
31cbb 61 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64  aders, int nRead
31cbc 65 72 73 29 7b 0a 20 20 63 68 61 72 20 63 5b 56  ers){.  char c[V
31cbd 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e 54  ARINT_MAX+VARINT
31cbe 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 69 54 65  _MAX];.  int iTe
31cbf 72 6d 44 61 74 61 20 3d 20 70 57 72 69 74 65 72  rmData = pWriter
31cc0 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 69 44  ->data.nData, iD
31cc1 6f 63 6c 69 73 74 44 61 74 61 3b 0a 20 20 69 6e  oclistData;.  in
31cc2 74 20 69 2c 20 6e 44 61 74 61 2c 20 6e 2c 20 6e  t i, nData, n, n
31cc3 41 63 74 75 61 6c 44 61 74 61 2c 20 6e 41 63 74  ActualData, nAct
31cc4 75 61 6c 2c 20 72 63 2c 20 6e 54 65 72 6d 44 69  ual, rc, nTermDi
31cc5 73 74 69 6e 63 74 3b 0a 0a 20 20 41 53 53 45 52  stinct;..  ASSER
31cc6 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44  T_VALID_LEAF_NOD
31cc7 45 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  E(pWriter->data.
31cc8 70 44 61 74 61 2c 20 70 57 72 69 74 65 72 2d 3e  pData, pWriter->
31cc9 64 61 74 61 2e 6e 44 61 74 61 29 3b 0a 20 20 6e  data.nData);.  n
31cca 54 65 72 6d 44 69 73 74 69 6e 63 74 20 3d 20 6c  TermDistinct = l
31ccb 65 61 66 57 72 69 74 65 72 45 6e 63 6f 64 65 54  eafWriterEncodeT
31ccc 65 72 6d 28 70 57 72 69 74 65 72 2c 20 70 54 65  erm(pWriter, pTe
31ccd 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f  rm, nTerm);..  /
31cce 2a 20 52 65 6d 65 6d 62 65 72 20 6e 54 65 72 6d  * Remember nTerm
31ccf 44 69 73 74 69 6e 63 74 20 69 66 20 6f 70 65 6e  Distinct if open
31cd0 69 6e 67 20 61 20 6e 65 77 20 6e 6f 64 65 2e 20  ing a new node. 
31cd1 2a 2f 0a 20 20 69 66 28 20 69 54 65 72 6d 44 61  */.  if( iTermDa
31cd2 74 61 3d 3d 30 20 29 20 70 57 72 69 74 65 72 2d  ta==0 ) pWriter-
31cd3 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 3d  >nTermDistinct =
31cd4 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a   nTermDistinct;.
31cd5 0a 20 20 69 44 6f 63 6c 69 73 74 44 61 74 61 20  .  iDoclistData 
31cd6 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  = pWriter->data.
31cd7 6e 44 61 74 61 3b 0a 0a 20 20 2f 2a 20 45 73 74  nData;..  /* Est
31cd8 69 6d 61 74 65 20 74 68 65 20 6c 65 6e 67 74 68  imate the length
31cd9 20 6f 66 20 74 68 65 20 6d 65 72 67 65 64 20 64   of the merged d
31cda 6f 63 6c 69 73 74 20 73 6f 20 77 65 20 63 61 6e  oclist so we can
31cdb 20 6c 65 61 76 65 20 73 70 61 63 65 0a 20 20 2a   leave space.  *
31cdc 2a 20 74 6f 20 65 6e 63 6f 64 65 20 69 74 2e 0a  * to encode it..
31cdd 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20    */.  for(i=0, 
31cde 6e 44 61 74 61 3d 30 3b 20 69 3c 6e 52 65 61 64  nData=0; i<nRead
31cdf 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e  ers; i++){.    n
31ce0 44 61 74 61 20 2b 3d 20 64 6c 72 41 6c 6c 44 61  Data += dlrAllDa
31ce1 74 61 42 79 74 65 73 28 26 70 52 65 61 64 65 72  taBytes(&pReader
31ce2 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d  s[i]);.  }.  n =
31ce3 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
31ce4 2c 20 6e 44 61 74 61 29 3b 0a 20 20 64 61 74 61  , nData);.  data
31ce5 42 75 66 66 65 72 41 70 70 65 6e 64 28 26 70 57  BufferAppend(&pW
31ce6 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c 20  riter->data, c, 
31ce7 6e 29 3b 0a 0a 20 20 64 6f 63 4c 69 73 74 4d 65  n);..  docListMe
31ce8 72 67 65 28 26 70 57 72 69 74 65 72 2d 3e 64 61  rge(&pWriter->da
31ce9 74 61 2c 20 70 52 65 61 64 65 72 73 2c 20 6e 52  ta, pReaders, nR
31cea 65 61 64 65 72 73 29 3b 0a 20 20 41 53 53 45 52  eaders);.  ASSER
31ceb 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28  T_VALID_DOCLIST(
31cec 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20  DL_DEFAULT,.    
31ced 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31cee 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61     pWriter->data
31cef 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
31cf0 61 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20 20  ata+n,.         
31cf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
31cf2 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
31cf3 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61 2d 6e  a-iDoclistData-n
31cf4 2c 20 4e 55 4c 4c 29 3b 0a 0a 20 20 2f 2a 20 54  , NULL);..  /* T
31cf5 68 65 20 61 63 74 75 61 6c 20 61 6d 6f 75 6e 74  he actual amount
31cf6 20 6f 66 20 64 6f 63 6c 69 73 74 20 64 61 74 61   of doclist data
31cf7 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 63   at this point c
31cf8 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c 65 72 0a  ould be smaller.
31cf9 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 20 6c 65    ** than the le
31cfa 6e 67 74 68 20 77 65 20 65 6e 63 6f 64 65 64 2e  ngth we encoded.
31cfb 20 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20    Additionally, 
31cfc 74 68 65 20 73 70 61 63 65 20 72 65 71 75 69 72  the space requir
31cfd 65 64 20 74 6f 0a 20 20 2a 2a 20 65 6e 63 6f 64  ed to.  ** encod
31cfe 65 20 74 68 69 73 20 6c 65 6e 67 74 68 20 63 6f  e this length co
31cff 75 6c 64 20 62 65 20 73 6d 61 6c 6c 65 72 2e 20  uld be smaller. 
31d00 20 46 6f 72 20 73 6d 61 6c 6c 20 64 6f 63 6c 69   For small docli
31d01 73 74 73 2c 20 74 68 69 73 20 69 73 0a 20 20 2a  sts, this is.  *
31d02 2a 20 6e 6f 74 20 61 20 62 69 67 20 64 65 61 6c  * not a big deal
31d03 2c 20 77 65 20 63 61 6e 20 6a 75 73 74 20 75 73  , we can just us
31d04 65 20 6d 65 6d 6d 6f 76 65 28 29 20 74 6f 20 61  e memmove() to a
31d05 64 6a 75 73 74 20 74 68 69 6e 67 73 2e 0a 20 20  djust things..  
31d06 2a 2f 0a 20 20 6e 41 63 74 75 61 6c 44 61 74 61  */.  nActualData
31d07 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61   = pWriter->data
31d08 2e 6e 44 61 74 61 2d 28 69 44 6f 63 6c 69 73 74  .nData-(iDoclist
31d09 44 61 74 61 2b 6e 29 3b 0a 20 20 6e 41 63 74 75  Data+n);.  nActu
31d0a 61 6c 20 3d 20 66 74 73 33 50 75 74 56 61 72 69  al = fts3PutVari
31d0b 6e 74 28 63 2c 20 6e 41 63 74 75 61 6c 44 61 74  nt(c, nActualDat
31d0c 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 41  a);.  assert( nA
31d0d 63 74 75 61 6c 44 61 74 61 3c 3d 6e 44 61 74 61  ctualData<=nData
31d0e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 41   );.  assert( nA
31d0f 63 74 75 61 6c 3c 3d 6e 20 29 3b 0a 0a 20 20 2f  ctual<=n );..  /
31d10 2a 20 49 66 20 74 68 65 20 6e 65 77 20 64 6f 63  * If the new doc
31d11 6c 69 73 74 20 69 73 20 62 69 67 20 65 6e 6f 75  list is big enou
31d12 67 68 20 66 6f 72 20 66 6f 72 63 65 20 61 20 73  gh for force a s
31d13 74 61 6e 64 61 6c 6f 6e 65 20 6c 65 61 66 0a 20  tandalone leaf. 
31d14 20 2a 2a 20 6e 6f 64 65 2c 20 77 65 20 63 61 6e   ** node, we can
31d15 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6c 75   immediately flu
31d16 73 68 20 69 74 20 69 6e 6c 69 6e 65 20 77 69 74  sh it inline wit
31d17 68 6f 75 74 20 64 6f 69 6e 67 20 74 68 65 0a 20  hout doing the. 
31d18 20 2a 2a 20 6d 65 6d 6d 6f 76 65 28 29 2e 0a 20   ** memmove().. 
31d19 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68   */.  /* TODO(sh
31d1a 65 73 73 29 20 54 68 69 73 20 74 65 73 74 20 6d  ess) This test m
31d1b 61 74 63 68 65 73 20 6c 65 61 66 57 72 69 74 65  atches leafWrite
31d1c 72 53 74 65 70 28 29 2c 20 77 68 69 63 68 20 64  rStep(), which d
31d1d 6f 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 74 65  oes this.  ** te
31d1e 73 74 20 62 65 66 6f 72 65 20 69 74 20 6b 6e 6f  st before it kno
31d1f 77 73 20 74 68 65 20 63 6f 73 74 20 74 6f 20 76  ws the cost to v
31d20 61 72 69 6e 74 2d 65 6e 63 6f 64 65 20 74 68 65  arint-encode the
31d21 20 74 65 72 6d 20 61 6e 64 0a 20 20 2a 2a 20 64   term and.  ** d
31d22 6f 63 6c 69 73 74 20 6c 65 6e 67 74 68 73 2e 20  oclist lengths. 
31d23 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 2c 20   At some point, 
31d24 63 68 61 6e 67 65 20 74 6f 0a 20 20 2a 2a 20 70  change to.  ** p
31d25 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
31d26 74 61 2d 69 54 65 72 6d 44 61 74 61 3e 53 54 41  ta-iTermData>STA
31d27 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 2e 0a 20 20 2a  NDALONE_MIN..  *
31d28 2f 0a 20 20 69 66 28 20 6e 54 65 72 6d 2b 6e 41  /.  if( nTerm+nA
31d29 63 74 75 61 6c 44 61 74 61 3e 53 54 41 4e 44 41  ctualData>STANDA
31d2a 4c 4f 4e 45 5f 4d 49 4e 20 29 7b 0a 20 20 20 20  LONE_MIN ){.    
31d2b 2f 2a 20 50 75 73 68 20 6c 65 61 66 20 6e 6f 64  /* Push leaf nod
31d2c 65 20 66 72 6f 6d 20 62 65 66 6f 72 65 20 74 68  e from before th
31d2d 69 73 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 20 20  is term. */.    
31d2e 69 66 28 20 69 54 65 72 6d 44 61 74 61 3e 30 20  if( iTermData>0 
31d2f 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65  ){.      rc = le
31d30 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c  afWriterInternal
31d31 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72  Flush(v, pWriter
31d32 2c 20 30 2c 20 69 54 65 72 6d 44 61 74 61 29 3b  , 0, iTermData);
31d33 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
31d34 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
31d35 6e 20 72 63 3b 0a 0a 20 20 20 20 20 20 70 57 72  n rc;..      pWr
31d36 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69  iter->nTermDisti
31d37 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74 69  nct = nTermDisti
31d38 6e 63 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  nct;.    }..    
31d39 2f 2a 20 46 69 78 20 74 68 65 20 65 6e 63 6f 64  /* Fix the encod
31d3a 65 64 20 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74  ed doclist lengt
31d3b 68 2e 20 2a 2f 0a 20 20 20 20 69 44 6f 63 6c 69  h. */.    iDocli
31d3c 73 74 44 61 74 61 20 2b 3d 20 6e 20 2d 20 6e 41  stData += n - nA
31d3d 63 74 75 61 6c 3b 0a 20 20 20 20 6d 65 6d 63 70  ctual;.    memcp
31d3e 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  y(pWriter->data.
31d3f 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61  pData+iDoclistDa
31d40 74 61 2c 20 63 2c 20 6e 41 63 74 75 61 6c 29 3b  ta, c, nActual);
31d41 0a 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 74 68  ..    /* Push th
31d42 65 20 73 74 61 6e 64 61 6c 6f 6e 65 20 6c 65 61  e standalone lea
31d43 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 72  f node. */.    r
31d44 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e  c = leafWriterIn
31d45 6c 69 6e 65 46 6c 75 73 68 28 76 2c 20 70 57 72  lineFlush(v, pWr
31d46 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65  iter, pTerm, nTe
31d47 72 6d 2c 20 69 44 6f 63 6c 69 73 74 44 61 74 61  rm, iDoclistData
31d48 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
31d49 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
31d4a 6e 20 72 63 3b 0a 0a 20 20 20 20 2f 2a 20 4c 65  n rc;..    /* Le
31d4b 61 76 65 20 74 68 65 20 6e 6f 64 65 20 65 6d 70  ave the node emp
31d4c 74 79 2e 20 2a 2f 0a 20 20 20 20 64 61 74 61 42  ty. */.    dataB
31d4d 75 66 66 65 72 52 65 73 65 74 28 26 70 57 72 69  ufferReset(&pWri
31d4e 74 65 72 2d 3e 64 61 74 61 29 3b 0a 0a 20 20 20  ter->data);..   
31d4f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
31d50 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
31d51 69 6e 74 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61  int, we know tha
31d52 74 20 74 68 65 20 64 6f 63 6c 69 73 74 20 77 61  t the doclist wa
31d53 73 20 73 6d 61 6c 6c 2c 20 73 6f 20 64 6f 20 74  s small, so do t
31d54 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76 65 20  he.  ** memmove 
31d55 69 66 20 69 6e 64 69 63 61 74 65 64 2e 0a 20 20  if indicated..  
31d56 2a 2f 0a 20 20 69 66 28 20 6e 41 63 74 75 61 6c  */.  if( nActual
31d57 3c 6e 20 29 7b 0a 20 20 20 20 6d 65 6d 6d 6f 76  <n ){.    memmov
31d58 65 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  e(pWriter->data.
31d59 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61  pData+iDoclistDa
31d5a 74 61 2b 6e 41 63 74 75 61 6c 2c 0a 20 20 20 20  ta+nActual,.    
31d5b 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d          pWriter-
31d5c 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
31d5d 6c 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20 20  listData+n,.    
31d5e 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d          pWriter-
31d5f 3e 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44 6f  >data.nData-(iDo
31d60 63 6c 69 73 74 44 61 74 61 2b 6e 29 29 3b 0a 20  clistData+n));. 
31d61 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61     pWriter->data
31d62 2e 6e 44 61 74 61 20 2d 3d 20 6e 2d 6e 41 63 74  .nData -= n-nAct
31d63 75 61 6c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  ual;.  }..  /* R
31d64 65 70 6c 61 63 65 20 77 72 69 74 74 65 6e 20 6c  eplace written l
31d65 65 6e 67 74 68 20 77 69 74 68 20 61 63 74 75 61  ength with actua
31d66 6c 20 6c 65 6e 67 74 68 2e 20 2a 2f 0a 20 20 6d  l length. */.  m
31d67 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64  emcpy(pWriter->d
31d68 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69  ata.pData+iDocli
31d69 73 74 44 61 74 61 2c 20 63 2c 20 6e 41 63 74 75  stData, c, nActu
31d6a 61 6c 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  al);..  /* If th
31d6b 65 20 6e 6f 64 65 20 69 73 20 74 6f 6f 20 6c 61  e node is too la
31d6c 72 67 65 2c 20 62 72 65 61 6b 20 74 68 69 6e 67  rge, break thing
31d6d 73 20 75 70 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f  s up. */.  /* TO
31d6e 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 74  DO(shess) This t
31d6f 65 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61 66  est matches leaf
31d70 57 72 69 74 65 72 53 74 65 70 28 29 2c 20 77 68  WriterStep(), wh
31d71 69 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20 20  ich does this.  
31d72 2a 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20 69  ** test before i
31d73 74 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73 74  t knows the cost
31d74 20 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64   to varint-encod
31d75 65 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a 20  e the term and. 
31d76 20 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e 67   ** doclist leng
31d77 74 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70 6f  ths.  At some po
31d78 69 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a 20  int, change to. 
31d79 20 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61 74   ** pWriter->dat
31d7a 61 2e 6e 44 61 74 61 3e 4c 45 41 46 5f 4d 41 58  a.nData>LEAF_MAX
31d7b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 54 65  ..  */.  if( iTe
31d7c 72 6d 44 61 74 61 2b 6e 54 65 72 6d 2b 6e 41 63  rmData+nTerm+nAc
31d7d 74 75 61 6c 44 61 74 61 3e 4c 45 41 46 5f 4d 41  tualData>LEAF_MA
31d7e 58 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6c 75 73  X ){.    /* Flus
31d7f 68 20 6f 75 74 20 74 68 65 20 6c 65 61 64 69 6e  h out the leadin
31d80 67 20 64 61 74 61 20 61 73 20 61 20 6e 6f 64 65  g data as a node
31d81 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6c 65 61   */.    rc = lea
31d82 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46  fWriterInternalF
31d83 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 2c  lush(v, pWriter,
31d84 20 30 2c 20 69 54 65 72 6d 44 61 74 61 29 3b 0a   0, iTermData);.
31d85 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
31d86 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
31d87 63 3b 0a 0a 20 20 20 20 70 57 72 69 74 65 72 2d  c;..    pWriter-
31d88 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 3d  >nTermDistinct =
31d89 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a   nTermDistinct;.
31d8a 0a 20 20 20 20 2f 2a 20 52 65 62 75 69 6c 64 20  .    /* Rebuild 
31d8b 68 65 61 64 65 72 20 75 73 69 6e 67 20 74 68 65  header using the
31d8c 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 2a 2f   current term */
31d8d 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75 74  .    n = fts3Put
31d8e 56 61 72 69 6e 74 28 70 57 72 69 74 65 72 2d 3e  Varint(pWriter->
31d8f 64 61 74 61 2e 70 44 61 74 61 2c 20 30 29 3b 0a  data.pData, 0);.
31d90 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74      n += fts3Put
31d91 56 61 72 69 6e 74 28 70 57 72 69 74 65 72 2d 3e  Varint(pWriter->
31d92 64 61 74 61 2e 70 44 61 74 61 2b 6e 2c 20 6e 54  data.pData+n, nT
31d93 65 72 6d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  erm);.    memcpy
31d94 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
31d95 44 61 74 61 2b 6e 2c 20 70 54 65 72 6d 2c 20 6e  Data+n, pTerm, n
31d96 54 65 72 6d 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  Term);.    n += 
31d97 6e 54 65 72 6d 3b 0a 0a 20 20 20 20 2f 2a 20 54  nTerm;..    /* T
31d98 68 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77 61  here should alwa
31d99 79 73 20 62 65 20 72 6f 6f 6d 2c 20 62 65 63 61  ys be room, beca
31d9a 75 73 65 20 74 68 65 20 70 72 65 76 69 6f 75 73  use the previous
31d9b 20 65 6e 63 6f 64 69 6e 67 0a 20 20 20 20 2a 2a   encoding.    **
31d9c 20 69 6e 63 6c 75 64 65 64 20 61 6c 6c 20 64 61   included all da
31d9d 74 61 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20  ta necessary to 
31d9e 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 74 65  construct the te
31d9f 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  rm..    */.    a
31da0 73 73 65 72 74 28 20 6e 3c 69 44 6f 63 6c 69 73  ssert( n<iDoclis
31da1 74 44 61 74 61 20 29 3b 0a 20 20 20 20 2f 2a 20  tData );.    /* 
31da2 53 6f 20 6c 6f 6e 67 20 61 73 20 53 54 41 4e 44  So long as STAND
31da3 41 4c 4f 4e 45 5f 4d 49 4e 20 69 73 20 68 61 6c  ALONE_MIN is hal
31da4 66 20 6f 72 20 6c 65 73 73 20 6f 66 20 4c 45 41  f or less of LEA
31da5 46 5f 4d 41 58 2c 20 74 68 65 0a 20 20 20 20 2a  F_MAX, the.    *
31da6 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 63  * following memc
31da7 70 79 28 29 20 69 73 20 73 61 66 65 20 28 61 73  py() is safe (as
31da8 20 6f 70 70 6f 73 65 64 20 74 6f 20 6e 65 65 64   opposed to need
31da9 69 6e 67 20 61 20 6d 65 6d 6d 6f 76 65 29 2e 0a  ing a memmove)..
31daa 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
31dab 74 28 20 32 2a 53 54 41 4e 44 41 4c 4f 4e 45 5f  t( 2*STANDALONE_
31dac 4d 49 4e 3c 3d 4c 45 41 46 5f 4d 41 58 20 29 3b  MIN<=LEAF_MAX );
31dad 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 70  .    assert( n+p
31dae 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
31daf 74 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61 3c  ta-iDoclistData<
31db0 69 44 6f 63 6c 69 73 74 44 61 74 61 20 29 3b 0a  iDoclistData );.
31db1 20 20 20 20 6d 65 6d 63 70 79 28 70 57 72 69 74      memcpy(pWrit
31db2 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 6e  er->data.pData+n
31db3 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 57 72  ,.           pWr
31db4 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
31db5 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 0a 20  +iDoclistData,. 
31db6 20 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65            pWrite
31db7 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 44  r->data.nData-iD
31db8 6f 63 6c 69 73 74 44 61 74 61 29 3b 0a 20 20 20  oclistData);.   
31db9 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e   pWriter->data.n
31dba 44 61 74 61 20 2d 3d 20 69 44 6f 63 6c 69 73 74  Data -= iDoclist
31dbb 44 61 74 61 2d 6e 3b 0a 20 20 7d 0a 20 20 41 53  Data-n;.  }.  AS
31dbc 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f  SERT_VALID_LEAF_
31dbd 4e 4f 44 45 28 70 57 72 69 74 65 72 2d 3e 64 61  NODE(pWriter->da
31dbe 74 61 2e 70 44 61 74 61 2c 20 70 57 72 69 74 65  ta.pData, pWrite
31dbf 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b 0a  r->data.nData);.
31dc0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
31dc1 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 50 75 73 68 20  _OK;.}../* Push 
31dc2 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 61 6c 6f  pTerm[nTerm] alo
31dc3 6e 67 20 77 69 74 68 20 74 68 65 20 64 6f 63 6c  ng with the docl
31dc4 69 73 74 20 64 61 74 61 20 74 6f 20 74 68 65 20  ist data to the 
31dc5 6c 65 61 66 20 6c 61 79 65 72 20 6f 66 0a 2a 2a  leaf layer of.**
31dc6 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a   %_segments..*/.
31dc7 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 52  /* TODO(shess) R
31dc8 65 76 69 73 65 20 77 72 69 74 65 5a 65 72 6f 53  evise writeZeroS
31dc9 65 67 6d 65 6e 74 28 29 20 73 6f 20 74 68 61 74  egment() so that
31dca 20 64 6f 63 6c 69 73 74 73 20 61 72 65 0a 2a 2a   doclists are.**
31dcb 20 63 6f 6e 73 74 72 75 63 74 65 64 20 64 69 72   constructed dir
31dcc 65 63 74 6c 79 20 69 6e 20 70 57 72 69 74 65 72  ectly in pWriter
31dcd 2d 3e 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69  ->data..*/.stati
31dce 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72  c int leafWriter
31dcf 53 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76 74  Step(fulltext_vt
31dd0 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65  ab *v, LeafWrite
31dd1 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
31dd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31dd3 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
31dd4 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
31dd5 72 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  rm,.            
31dd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
31dd7 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
31dd8 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 69   int nData){.  i
31dd9 6e 74 20 72 63 3b 0a 20 20 44 4c 52 65 61 64 65  nt rc;.  DLReade
31dda 72 20 72 65 61 64 65 72 3b 0a 0a 20 20 64 6c 72  r reader;..  dlr
31ddb 49 6e 69 74 28 26 72 65 61 64 65 72 2c 20 44 4c  Init(&reader, DL
31ddc 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2c  _DEFAULT, pData,
31ddd 20 6e 44 61 74 61 29 3b 0a 20 20 72 63 20 3d 20   nData);.  rc = 
31dde 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 4d 65  leafWriterStepMe
31ddf 72 67 65 28 76 2c 20 70 57 72 69 74 65 72 2c 20  rge(v, pWriter, 
31de0 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 26 72  pTerm, nTerm, &r
31de1 65 61 64 65 72 2c 20 31 29 3b 0a 20 20 64 6c 72  eader, 1);.  dlr
31de2 44 65 73 74 72 6f 79 28 26 72 65 61 64 65 72 29  Destroy(&reader)
31de3 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  ;..  return rc;.
31de4 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.../***********
31de5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31de6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31de7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31de8 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 66 52 65  *****/./* LeafRe
31de9 61 64 65 72 20 69 73 20 75 73 65 64 20 74 6f 20  ader is used to 
31dea 69 74 65 72 61 74 65 20 6f 76 65 72 20 61 6e 20  iterate over an 
31deb 69 6e 64 69 76 69 64 75 61 6c 20 6c 65 61 66 20  individual leaf 
31dec 6e 6f 64 65 2e 20 2a 2f 0a 74 79 70 65 64 65 66  node. */.typedef
31ded 20 73 74 72 75 63 74 20 4c 65 61 66 52 65 61 64   struct LeafRead
31dee 65 72 20 7b 0a 20 20 44 61 74 61 42 75 66 66 65  er {.  DataBuffe
31def 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20 20  r term;         
31df0 20 2f 2a 20 63 6f 70 79 20 6f 66 20 63 75 72 72   /* copy of curr
31df1 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 0a 20 20  ent term. */..  
31df2 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
31df3 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 64 61 74  a;        /* dat
31df4 61 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74 65  a for current te
31df5 72 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61  rm. */.  int nDa
31df6 74 61 3b 0a 7d 20 4c 65 61 66 52 65 61 64 65 72  ta;.} LeafReader
31df7 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  ;..static void l
31df8 65 61 66 52 65 61 64 65 72 44 65 73 74 72 6f 79  eafReaderDestroy
31df9 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65  (LeafReader *pRe
31dfa 61 64 65 72 29 7b 0a 20 20 64 61 74 61 42 75 66  ader){.  dataBuf
31dfb 66 65 72 44 65 73 74 72 6f 79 28 26 70 52 65 61  ferDestroy(&pRea
31dfc 64 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53 43  der->term);.  SC
31dfd 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29 3b  RAMBLE(pReader);
31dfe 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .}..static int l
31dff 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28 4c  eafReaderAtEnd(L
31e00 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61 64  eafReader *pRead
31e01 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52  er){.  return pR
31e02 65 61 64 65 72 2d 3e 6e 44 61 74 61 3c 3d 30 3b  eader->nData<=0;
31e03 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68  .}../* Access th
31e04 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20  e current term. 
31e05 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65  */.static int le
31e06 61 66 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  afReaderTermByte
31e07 73 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  s(LeafReader *pR
31e08 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  eader){.  return
31e09 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e   pReader->term.n
31e0a 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20 63  Data;.}.static c
31e0b 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65 61 66 52  onst char *leafR
31e0c 65 61 64 65 72 54 65 72 6d 28 4c 65 61 66 52 65  eaderTerm(LeafRe
31e0d 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
31e0e 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
31e0f 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20  r->term.nData>0 
31e10 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
31e11 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 3b  der->term.pData;
31e12 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68  .}../* Access th
31e13 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66  e doclist data f
31e14 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  or the current t
31e15 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  erm. */.static i
31e16 6e 74 20 6c 65 61 66 52 65 61 64 65 72 44 61 74  nt leafReaderDat
31e17 61 42 79 74 65 73 28 4c 65 61 66 52 65 61 64 65  aBytes(LeafReade
31e18 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69  r *pReader){.  i
31e19 6e 74 20 6e 44 61 74 61 3b 0a 20 20 61 73 73 65  nt nData;.  asse
31e1a 72 74 28 20 70 52 65 61 64 65 72 2d 3e 74 65 72  rt( pReader->ter
31e1b 6d 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 66  m.nData>0 );.  f
31e1c 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
31e1d 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
31e1e 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75 72 6e  nData);.  return
31e1f 20 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63   nData;.}.static
31e20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65 61   const char *lea
31e21 66 52 65 61 64 65 72 44 61 74 61 28 4c 65 61 66  fReaderData(Leaf
31e22 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
31e23 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e 44 61 74 61  {.  int n, nData
31e24 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 61  ;.  assert( pRea
31e25 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e  der->term.nData>
31e26 30 20 29 3b 0a 20 20 6e 20 3d 20 66 74 73 33 47  0 );.  n = fts3G
31e27 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
31e28 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 44 61 74  er->pData, &nDat
31e29 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  a);.  return pRe
31e2a 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b 0a 7d  ader->pData+n;.}
31e2b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  ..static void le
31e2c 61 66 52 65 61 64 65 72 49 6e 69 74 28 63 6f 6e  afReaderInit(con
31e2d 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
31e2e 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
31e2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e30 20 20 20 20 20 20 4c 65 61 66 52 65 61 64 65 72        LeafReader
31e31 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e   *pReader){.  in
31e32 74 20 6e 54 65 72 6d 2c 20 6e 3b 0a 0a 20 20 61  t nTerm, n;..  a
31e33 73 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29  ssert( nData>0 )
31e34 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74  ;.  assert( pDat
31e35 61 5b 30 5d 3d 3d 27 5c 30 27 20 29 3b 0a 0a 20  a[0]=='\0' );.. 
31e36 20 43 4c 45 41 52 28 70 52 65 61 64 65 72 29 3b   CLEAR(pReader);
31e37 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20  ..  /* Read the 
31e38 66 69 72 73 74 20 74 65 72 6d 2c 20 73 6b 69 70  first term, skip
31e39 70 69 6e 67 20 74 68 65 20 68 65 61 64 65 72 20  ping the header 
31e3a 62 79 74 65 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66  byte. */.  n = f
31e3b 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
31e3c 44 61 74 61 2b 31 2c 20 26 6e 54 65 72 6d 29 3b  Data+1, &nTerm);
31e3d 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  .  dataBufferIni
31e3e 74 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  t(&pReader->term
31e3f 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 64 61 74 61  , nTerm);.  data
31e40 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70  BufferReplace(&p
31e41 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70 44  Reader->term, pD
31e42 61 74 61 2b 31 2b 6e 2c 20 6e 54 65 72 6d 29 3b  ata+1+n, nTerm);
31e43 0a 0a 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 20  ..  /* Position 
31e44 61 66 74 65 72 20 74 68 65 20 66 69 72 73 74 20  after the first 
31e45 74 65 72 6d 2e 20 2a 2f 0a 20 20 61 73 73 65 72  term. */.  asser
31e46 74 28 20 31 2b 6e 2b 6e 54 65 72 6d 3c 6e 44 61  t( 1+n+nTerm<nDa
31e47 74 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72 2d  ta );.  pReader-
31e48 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 2b 31  >pData = pData+1
31e49 2b 6e 2b 6e 54 65 72 6d 3b 0a 20 20 70 52 65 61  +n+nTerm;.  pRea
31e4a 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44 61  der->nData = nDa
31e4b 74 61 2d 31 2d 6e 2d 6e 54 65 72 6d 3b 0a 7d 0a  ta-1-n-nTerm;.}.
31e4c 0a 2f 2a 20 53 74 65 70 20 74 68 65 20 72 65 61  ./* Step the rea
31e4d 64 65 72 20 66 6f 72 77 61 72 64 20 74 6f 20 74  der forward to t
31e4e 68 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a 2f  he next term. */
31e4f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61  .static void lea
31e50 66 52 65 61 64 65 72 53 74 65 70 28 4c 65 61 66  fReaderStep(Leaf
31e51 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
31e52 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e 44 61 74 61  {.  int n, nData
31e53 2c 20 6e 50 72 65 66 69 78 2c 20 6e 53 75 66 66  , nPrefix, nSuff
31e54 69 78 3b 0a 20 20 61 73 73 65 72 74 28 20 21 6c  ix;.  assert( !l
31e55 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28 70  eafReaderAtEnd(p
31e56 52 65 61 64 65 72 29 20 29 3b 0a 0a 20 20 2f 2a  Reader) );..  /*
31e57 20 53 6b 69 70 20 70 72 65 76 69 6f 75 73 20 65   Skip previous e
31e58 6e 74 72 79 27 73 20 64 61 74 61 20 62 6c 6f 63  ntry's data bloc
31e59 6b 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33  k. */.  n = fts3
31e5a 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
31e5b 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 44 61  der->pData, &nDa
31e5c 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  ta);.  assert( n
31e5d 2b 6e 44 61 74 61 3c 3d 70 52 65 61 64 65 72 2d  +nData<=pReader-
31e5e 3e 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61  >nData );.  pRea
31e5f 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b  der->pData += n+
31e60 6e 44 61 74 61 3b 0a 20 20 70 52 65 61 64 65 72  nData;.  pReader
31e61 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 44 61  ->nData -= n+nDa
31e62 74 61 3b 0a 0a 20 20 69 66 28 20 21 6c 65 61 66  ta;..  if( !leaf
31e63 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61  ReaderAtEnd(pRea
31e64 64 65 72 29 20 29 7b 0a 20 20 20 20 2f 2a 20 43  der) ){.    /* C
31e65 6f 6e 73 74 72 75 63 74 20 74 68 65 20 6e 65 77  onstruct the new
31e66 20 74 65 72 6d 20 75 73 69 6e 67 20 61 20 70 72   term using a pr
31e67 65 66 69 78 20 66 72 6f 6d 20 74 68 65 20 6f 6c  efix from the ol
31e68 64 20 74 65 72 6d 20 70 6c 75 73 20 61 0a 20 20  d term plus a.  
31e69 20 20 2a 2a 20 73 75 66 66 69 78 20 66 72 6f 6d    ** suffix from
31e6a 20 74 68 65 20 6c 65 61 66 20 64 61 74 61 2e 0a   the leaf data..
31e6b 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66      */.    n = f
31e6c 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
31e6d 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
31e6e 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e 20  nPrefix);.    n 
31e6f 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
31e70 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74  32(pReader->pDat
31e71 61 2b 6e 2c 20 26 6e 53 75 66 66 69 78 29 3b 0a  a+n, &nSuffix);.
31e72 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 6e 53      assert( n+nS
31e73 75 66 66 69 78 3c 70 52 65 61 64 65 72 2d 3e 6e  uffix<pReader->n
31e74 44 61 74 61 20 29 3b 0a 20 20 20 20 70 52 65 61  Data );.    pRea
31e75 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20  der->term.nData 
31e76 3d 20 6e 50 72 65 66 69 78 3b 0a 20 20 20 20 64  = nPrefix;.    d
31e77 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
31e78 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20  &pReader->term, 
31e79 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
31e7a 2c 20 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20  , nSuffix);..   
31e7b 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20   pReader->pData 
31e7c 2b 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a 20 20  += n+nSuffix;.  
31e7d 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
31e7e 20 2d 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a 20   -= n+nSuffix;. 
31e7f 20 7d 0a 7d 0a 0a 2f 2a 20 73 74 72 63 6d 70 2d   }.}../* strcmp-
31e80 73 74 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e  style comparison
31e81 20 6f 66 20 70 52 65 61 64 65 72 27 73 20 63 75   of pReader's cu
31e82 72 72 65 6e 74 20 74 65 72 6d 20 61 67 61 69 6e  rrent term again
31e83 73 74 20 70 54 65 72 6d 2e 0a 2a 2a 20 49 66 20  st pTerm..** If 
31e84 69 73 50 72 65 66 69 78 2c 20 65 71 75 61 6c 69  isPrefix, equali
31e85 74 79 20 6d 65 61 6e 73 20 65 71 75 61 6c 20 74  ty means equal t
31e86 68 72 6f 75 67 68 20 6e 54 65 72 6d 20 62 79 74  hrough nTerm byt
31e87 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
31e88 74 20 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d  t leafReaderTerm
31e89 43 6d 70 28 4c 65 61 66 52 65 61 64 65 72 20 2a  Cmp(LeafReader *
31e8a 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20  pReader,.       
31e8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e8c 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
31e8d 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
31e8e 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78  rm, int isPrefix
31e8f 29 7b 0a 20 20 69 6e 74 20 63 2c 20 6e 20 3d 20  ){.  int c, n = 
31e90 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
31e91 61 74 61 3c 6e 54 65 72 6d 20 3f 20 70 52 65 61  ata<nTerm ? pRea
31e92 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20  der->term.nData 
31e93 3a 20 6e 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e  : nTerm;.  if( n
31e94 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
31e95 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61  Reader->term.nDa
31e96 74 61 3e 30 20 29 20 72 65 74 75 72 6e 20 2d 31  ta>0 ) return -1
31e97 3b 0a 20 20 20 20 69 66 28 6e 54 65 72 6d 3e 30  ;.    if(nTerm>0
31e98 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
31e99 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
31e9a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 70 52 65    c = memcmp(pRe
31e9b 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61  ader->term.pData
31e9c 2c 20 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20 69  , pTerm, n);.  i
31e9d 66 28 20 63 21 3d 30 20 29 20 72 65 74 75 72 6e  f( c!=0 ) return
31e9e 20 63 3b 0a 20 20 69 66 28 20 69 73 50 72 65 66   c;.  if( isPref
31e9f 69 78 20 26 26 20 6e 3d 3d 6e 54 65 72 6d 20 29  ix && n==nTerm )
31ea0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74   return 0;.  ret
31ea1 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72  urn pReader->ter
31ea2 6d 2e 6e 44 61 74 61 20 2d 20 6e 54 65 72 6d 3b  m.nData - nTerm;
31ea3 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}.../**********
31ea4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31ea5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31ea6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31ea7 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 76 65  ******/./* Leave
31ea8 73 52 65 61 64 65 72 20 77 72 61 70 73 20 4c 65  sReader wraps Le
31ea9 61 66 52 65 61 64 65 72 20 74 6f 20 61 6c 6c 6f  afReader to allo
31eaa 77 20 69 74 65 72 61 74 69 6e 67 20 6f 76 65 72  w iterating over
31eab 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 6c   the entire.** l
31eac 65 61 66 20 6c 61 79 65 72 20 6f 66 20 74 68 65  eaf layer of the
31ead 20 74 72 65 65 2e 0a 2a 2f 0a 74 79 70 65 64 65   tree..*/.typede
31eae 66 20 73 74 72 75 63 74 20 4c 65 61 76 65 73 52  f struct LeavesR
31eaf 65 61 64 65 72 20 7b 0a 20 20 69 6e 74 20 69 64  eader {.  int id
31eb0 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
31eb1 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 77 69 74      /* Index wit
31eb2 68 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 2e  hin the segment.
31eb3 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73   */..  sqlite3_s
31eb4 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20  tmt *pStmt;     
31eb5 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 77 65   /* Statement we
31eb6 27 72 65 20 73 74 72 65 61 6d 69 6e 67 20 6c 65  're streaming le
31eb7 61 76 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20  aves from. */.  
31eb8 69 6e 74 20 65 6f 66 3b 20 20 20 20 20 20 20 20  int eof;        
31eb9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27            /* we'
31eba 76 65 20 73 65 65 6e 20 53 51 4c 49 54 45 5f 44  ve seen SQLITE_D
31ebb 4f 4e 45 20 66 72 6f 6d 20 70 53 74 6d 74 2e 20  ONE from pStmt. 
31ebc 2a 2f 0a 0a 20 20 4c 65 61 66 52 65 61 64 65 72  */..  LeafReader
31ebd 20 6c 65 61 66 52 65 61 64 65 72 3b 20 20 20 20   leafReader;    
31ebe 2f 2a 20 72 65 61 64 65 72 20 66 6f 72 20 74 68  /* reader for th
31ebf 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 2e 20  e current leaf. 
31ec0 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  */.  DataBuffer 
31ec1 72 6f 6f 74 44 61 74 61 3b 20 20 20 20 20 20 2f  rootData;      /
31ec2 2a 20 72 6f 6f 74 20 64 61 74 61 20 66 6f 72 20  * root data for 
31ec3 69 6e 6c 69 6e 65 2e 20 2a 2f 0a 7d 20 4c 65 61  inline. */.} Lea
31ec4 76 65 73 52 65 61 64 65 72 3b 0a 0a 2f 2a 20 41  vesReader;../* A
31ec5 63 63 65 73 73 20 74 68 65 20 63 75 72 72 65 6e  ccess the curren
31ec6 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69  t term. */.stati
31ec7 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
31ec8 65 72 54 65 72 6d 42 79 74 65 73 28 4c 65 61 76  erTermBytes(Leav
31ec9 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
31eca 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70  r){.  assert( !p
31ecb 52 65 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20  Reader->eof );. 
31ecc 20 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61 64   return leafRead
31ecd 65 72 54 65 72 6d 42 79 74 65 73 28 26 70 52 65  erTermBytes(&pRe
31ece 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72  ader->leafReader
31ecf 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73  );.}.static cons
31ed0 74 20 63 68 61 72 20 2a 6c 65 61 76 65 73 52 65  t char *leavesRe
31ed1 61 64 65 72 54 65 72 6d 28 4c 65 61 76 65 73 52  aderTerm(LeavesR
31ed2 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
31ed3 0a 20 20 61 73 73 65 72 74 28 20 21 70 52 65 61  .  assert( !pRea
31ed4 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65  der->eof );.  re
31ed5 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 54  turn leafReaderT
31ed6 65 72 6d 28 26 70 52 65 61 64 65 72 2d 3e 6c 65  erm(&pReader->le
31ed7 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a  afReader);.}../*
31ed8 20 41 63 63 65 73 73 20 74 68 65 20 64 6f 63 6c   Access the docl
31ed9 69 73 74 20 64 61 74 61 20 66 6f 72 20 74 68 65  ist data for the
31eda 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a   current term. *
31edb 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
31edc 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
31edd 65 73 28 4c 65 61 76 65 73 52 65 61 64 65 72 20  es(LeavesReader 
31ede 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
31edf 65 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e 65  ert( !pReader->e
31ee0 6f 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c  of );.  return l
31ee1 65 61 66 52 65 61 64 65 72 44 61 74 61 42 79 74  eafReaderDataByt
31ee2 65 73 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61  es(&pReader->lea
31ee3 66 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74  fReader);.}.stat
31ee4 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c  ic const char *l
31ee5 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
31ee6 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
31ee7 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
31ee8 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66 20  ( !pReader->eof 
31ee9 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66  );.  return leaf
31eea 52 65 61 64 65 72 44 61 74 61 28 26 70 52 65 61  ReaderData(&pRea
31eeb 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29  der->leafReader)
31eec 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
31eed 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e  leavesReaderAtEn
31eee 64 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  d(LeavesReader *
31eef 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75  pReader){.  retu
31ef0 72 6e 20 70 52 65 61 64 65 72 2d 3e 65 6f 66 3b  rn pReader->eof;
31ef1 0a 7d 0a 0a 2f 2a 20 6c 6f 61 64 53 65 67 6d 65  .}../* loadSegme
31ef2 6e 74 4c 65 61 76 65 73 28 29 20 6d 61 79 20 6e  ntLeaves() may n
31ef3 6f 74 20 72 65 61 64 20 61 6c 6c 20 74 68 65 20  ot read all the 
31ef4 77 61 79 20 74 6f 20 53 51 4c 49 54 45 5f 44 4f  way to SQLITE_DO
31ef5 4e 45 2c 20 74 68 75 73 0a 2a 2a 20 6c 65 61 76  NE, thus.** leav
31ef6 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ing the statemen
31ef7 74 20 68 61 6e 64 6c 65 20 6f 70 65 6e 2c 20 77  t handle open, w
31ef8 68 69 63 68 20 6c 6f 63 6b 73 20 74 68 65 20 74  hich locks the t
31ef9 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  able..*/./* TODO
31efa 28 73 68 65 73 73 29 20 54 68 69 73 20 22 73 6f  (shess) This "so
31efb 6c 75 74 69 6f 6e 22 20 69 73 20 6e 6f 74 20 73  lution" is not s
31efc 61 74 69 73 66 61 63 74 6f 72 79 2e 20 20 52 65  atisfactory.  Re
31efd 61 6c 6c 79 2c 20 74 68 65 72 65 0a 2a 2a 20 73  ally, there.** s
31efe 68 6f 75 6c 64 20 62 65 20 63 68 65 63 6b 2d 69  hould be check-i
31eff 6e 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61  n function for a
31f00 6c 6c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e  ll statement han
31f01 64 6c 65 73 20 77 68 69 63 68 0a 2a 2a 20 61 72  dles which.** ar
31f02 72 61 6e 67 65 73 20 74 6f 20 63 61 6c 6c 20 73  ranges to call s
31f03 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2e 20  qlite3_reset(). 
31f04 20 54 68 69 73 20 6d 6f 73 74 20 6c 69 6b 65 6c   This most likel
31f05 79 20 77 69 6c 6c 20 72 65 71 75 69 72 65 0a 2a  y will require.*
31f06 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 74  * modification t
31f07 6f 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 20 61  o control flow a
31f08 6c 6c 20 6f 76 65 72 20 74 68 65 20 70 6c 61 63  ll over the plac
31f09 65 2c 20 74 68 6f 75 67 68 2c 20 73 6f 20 66 6f  e, though, so fo
31f0a 72 20 6e 6f 77 0a 2a 2a 20 6a 75 73 74 20 70 75  r now.** just pu
31f0b 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  nt..**.** Note t
31f0c 68 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  he the current s
31f0d 79 73 74 65 6d 20 61 73 73 75 6d 65 73 20 74 68  ystem assumes th
31f0e 61 74 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65  at segment merge
31f0f 73 20 77 69 6c 6c 20 72 75 6e 20 74 6f 0a 2a 2a  s will run to.**
31f10 20 63 6f 6d 70 6c 65 74 69 6f 6e 2c 20 77 68 69   completion, whi
31f11 63 68 20 69 73 20 77 68 79 20 74 68 69 73 20 70  ch is why this p
31f12 61 72 74 69 63 75 6c 61 72 20 70 72 6f 62 61 62  articular probab
31f13 6c 79 20 68 61 73 6e 27 74 20 61 72 69 73 65 6e  ly hasn't arisen
31f14 20 69 6e 0a 2a 2a 20 74 68 69 73 20 63 61 73 65   in.** this case
31f15 2e 20 20 50 72 6f 62 61 62 6c 79 20 61 20 62 72  .  Probably a br
31f16 69 74 74 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e  ittle assumption
31f17 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31f18 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 73 65  leavesReaderRese
31f19 74 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  t(LeavesReader *
31f1a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75  pReader){.  retu
31f1b 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  rn sqlite3_reset
31f1c 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 29  (pReader->pStmt)
31f1d 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
31f1e 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73   leavesReaderDes
31f1f 74 72 6f 79 28 4c 65 61 76 65 73 52 65 61 64 65  troy(LeavesReade
31f20 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 2f  r *pReader){.  /
31f21 2a 20 49 66 20 69 64 78 20 69 73 20 2d 31 2c 20  * If idx is -1, 
31f22 74 68 61 74 20 6d 65 61 6e 73 20 77 65 27 72 65  that means we're
31f23 20 75 73 69 6e 67 20 61 20 6e 6f 6e 2d 63 61 63   using a non-cac
31f24 68 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  hed statement.  
31f25 2a 2a 20 68 61 6e 64 6c 65 20 69 6e 20 74 68 65  ** handle in the
31f26 20 6f 70 74 69 6d 69 7a 65 28 29 20 63 61 73 65   optimize() case
31f27 2c 20 73 6f 20 77 65 20 6e 65 65 64 20 74 6f 20  , so we need to 
31f28 72 65 6c 65 61 73 65 20 69 74 2e 0a 20 20 2a 2f  release it..  */
31f29 0a 20 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e  .  if( pReader->
31f2a 70 53 74 6d 74 21 3d 4e 55 4c 4c 20 26 26 20 70  pStmt!=NULL && p
31f2b 52 65 61 64 65 72 2d 3e 69 64 78 3d 3d 2d 31 20  Reader->idx==-1 
31f2c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
31f2d 69 6e 61 6c 69 7a 65 28 70 52 65 61 64 65 72 2d  inalize(pReader-
31f2e 3e 70 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20 6c  >pStmt);.  }.  l
31f2f 65 61 66 52 65 61 64 65 72 44 65 73 74 72 6f 79  eafReaderDestroy
31f30 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52  (&pReader->leafR
31f31 65 61 64 65 72 29 3b 0a 20 20 64 61 74 61 42 75  eader);.  dataBu
31f32 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 52 65  fferDestroy(&pRe
31f33 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 29 3b  ader->rootData);
31f34 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65 61  .  SCRAMBLE(pRea
31f35 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69 74  der);.}../* Init
31f36 69 61 6c 69 7a 65 20 70 52 65 61 64 65 72 20 77  ialize pReader w
31f37 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 72 6f  ith the given ro
31f38 6f 74 20 64 61 74 61 20 28 69 66 20 69 53 74 61  ot data (if iSta
31f39 72 74 42 6c 6f 63 6b 69 64 3d 3d 30 0a 2a 2a 20  rtBlockid==0.** 
31f3a 74 68 65 20 6c 65 61 66 20 64 61 74 61 20 77 61  the leaf data wa
31f3b 73 20 65 6e 74 69 72 65 6c 79 20 63 6f 6e 74 61  s entirely conta
31f3c 69 6e 65 64 20 69 6e 20 74 68 65 20 72 6f 6f 74  ined in the root
31f3d 29 2c 20 6f 72 20 66 72 6f 6d 20 74 68 65 0a 2a  ), or from the.*
31f3e 2a 20 73 74 72 65 61 6d 20 6f 66 20 62 6c 6f 63  * stream of bloc
31f3f 6b 73 20 62 65 74 77 65 65 6e 20 69 53 74 61 72  ks between iStar
31f40 74 42 6c 6f 63 6b 69 64 20 61 6e 64 20 69 45 6e  tBlockid and iEn
31f41 64 42 6c 6f 63 6b 69 64 2c 20 69 6e 63 6c 75 73  dBlockid, inclus
31f42 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ive..*/.static i
31f43 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 49  nt leavesReaderI
31f44 6e 69 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  nit(fulltext_vta
31f45 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20 20  b *v,.          
31f46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f47 20 20 69 6e 74 20 69 64 78 2c 0a 20 20 20 20 20    int idx,.     
31f48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f49 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
31f4a 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69  t64 iStartBlocki
31f4b 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
31f4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
31f4d 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64  qlite_int64 iEnd
31f4e 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20  Blockid,.       
31f4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f50 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
31f51 2a 70 52 6f 6f 74 44 61 74 61 2c 20 69 6e 74 20  *pRootData, int 
31f52 6e 52 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20 20  nRootData,.     
31f53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f54 20 20 20 20 20 20 20 4c 65 61 76 65 73 52 65 61         LeavesRea
31f55 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
31f56 20 43 4c 45 41 52 28 70 52 65 61 64 65 72 29 3b   CLEAR(pReader);
31f57 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 64 78 20  .  pReader->idx 
31f58 3d 20 69 64 78 3b 0a 0a 20 20 64 61 74 61 42 75  = idx;..  dataBu
31f59 66 66 65 72 49 6e 69 74 28 26 70 52 65 61 64 65  fferInit(&pReade
31f5a 72 2d 3e 72 6f 6f 74 44 61 74 61 2c 20 30 29 3b  r->rootData, 0);
31f5b 0a 20 20 69 66 28 20 69 53 74 61 72 74 42 6c 6f  .  if( iStartBlo
31f5c 63 6b 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  ckid==0 ){.    /
31f5d 2a 20 45 6e 74 69 72 65 20 6c 65 61 66 20 6c 65  * Entire leaf le
31f5e 76 65 6c 20 66 69 74 20 69 6e 20 72 6f 6f 74 20  vel fit in root 
31f5f 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 64 61 74  data. */.    dat
31f60 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26  aBufferReplace(&
31f61 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74  pReader->rootDat
31f62 61 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52  a, pRootData, nR
31f63 6f 6f 74 44 61 74 61 29 3b 0a 20 20 20 20 6c 65  ootData);.    le
31f64 61 66 52 65 61 64 65 72 49 6e 69 74 28 70 52 65  afReaderInit(pRe
31f65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 70  ader->rootData.p
31f66 44 61 74 61 2c 20 70 52 65 61 64 65 72 2d 3e 72  Data, pReader->r
31f67 6f 6f 74 44 61 74 61 2e 6e 44 61 74 61 2c 0a 20  ootData.nData,. 
31f68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f69 20 20 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66    &pReader->leaf
31f6a 52 65 61 64 65 72 29 3b 0a 20 20 7d 65 6c 73 65  Reader);.  }else
31f6b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74  {.    sqlite3_st
31f6c 6d 74 20 2a 73 3b 0a 20 20 20 20 69 6e 74 20 72  mt *s;.    int r
31f6d 63 20 3d 20 73 71 6c 5f 67 65 74 5f 6c 65 61 66  c = sql_get_leaf
31f6e 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 69 64  _statement(v, id
31f6f 78 2c 20 26 73 29 3b 0a 20 20 20 20 69 66 28 20  x, &s);.    if( 
31f70 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
31f71 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
31f72 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
31f73 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69 53  d_int64(s, 1, iS
31f74 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  tartBlockid);.  
31f75 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
31f76 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
31f77 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
31f78 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
31f79 20 32 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29   2, iEndBlockid)
31f7a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
31f7b 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
31f7c 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73   rc;..    rc = s
31f7d 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
31f7e 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
31f7f 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  TE_DONE ){.     
31f80 20 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d 20   pReader->eof = 
31f81 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  1;.      return 
31f82 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
31f83 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
31f84 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e  ITE_ROW ) return
31f85 20 72 63 3b 0a 0a 20 20 20 20 70 52 65 61 64 65   rc;..    pReade
31f86 72 2d 3e 70 53 74 6d 74 20 3d 20 73 3b 0a 20 20  r->pStmt = s;.  
31f87 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69 74    leafReaderInit
31f88 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  (sqlite3_column_
31f89 62 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70 53  blob(pReader->pS
31f8a 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20 20  tmt, 0),.       
31f8b 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
31f8c 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
31f8d 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c  (pReader->pStmt,
31f8e 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0),.           
31f8f 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72          &pReader
31f90 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20  ->leafReader);. 
31f91 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
31f92 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 53 74 65  TE_OK;.}../* Ste
31f93 70 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 65  p the current le
31f94 61 66 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68  af forward to th
31f95 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 20 49 66  e next term.  If
31f96 20 77 65 20 72 65 61 63 68 20 74 68 65 0a 2a 2a   we reach the.**
31f97 20 65 6e 64 20 6f 66 20 74 68 65 20 63 75 72 72   end of the curr
31f98 65 6e 74 20 6c 65 61 66 2c 20 73 74 65 70 20 66  ent leaf, step f
31f99 6f 72 77 61 72 64 20 74 6f 20 74 68 65 20 6e 65  orward to the ne
31f9a 78 74 20 6c 65 61 66 20 62 6c 6f 63 6b 2e 0a 2a  xt leaf block..*
31f9b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
31f9c 76 65 73 52 65 61 64 65 72 53 74 65 70 28 66 75  vesReaderStep(fu
31f9d 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
31f9e 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
31f9f 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
31fa0 28 20 21 6c 65 61 76 65 73 52 65 61 64 65 72 41  ( !leavesReaderA
31fa1 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
31fa2 0a 20 20 6c 65 61 66 52 65 61 64 65 72 53 74 65  .  leafReaderSte
31fa3 70 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66  p(&pReader->leaf
31fa4 52 65 61 64 65 72 29 3b 0a 0a 20 20 69 66 28 20  Reader);..  if( 
31fa5 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28  leafReaderAtEnd(
31fa6 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65  &pReader->leafRe
31fa7 61 64 65 72 29 20 29 7b 0a 20 20 20 20 69 6e 74  ader) ){.    int
31fa8 20 72 63 3b 0a 20 20 20 20 69 66 28 20 70 52 65   rc;.    if( pRe
31fa9 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 70  ader->rootData.p
31faa 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 70 52  Data ){.      pR
31fab 65 61 64 65 72 2d 3e 65 6f 66 20 3d 20 31 3b 0a  eader->eof = 1;.
31fac 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
31fad 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
31fae 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
31faf 74 65 70 28 70 52 65 61 64 65 72 2d 3e 70 53 74  tep(pReader->pSt
31fb0 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  mt);.    if( rc!
31fb1 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
31fb2 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65 6f       pReader->eo
31fb3 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74  f = 1;.      ret
31fb4 75 72 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  urn rc==SQLITE_D
31fb5 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20  ONE ? SQLITE_OK 
31fb6 3a 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  : rc;.    }.    
31fb7 6c 65 61 66 52 65 61 64 65 72 44 65 73 74 72 6f  leafReaderDestro
31fb8 79 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66  y(&pReader->leaf
31fb9 52 65 61 64 65 72 29 3b 0a 20 20 20 20 6c 65 61  Reader);.    lea
31fba 66 52 65 61 64 65 72 49 6e 69 74 28 73 71 6c 69  fReaderInit(sqli
31fbb 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
31fbc 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20  pReader->pStmt, 
31fbd 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0),.            
31fbe 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63         sqlite3_c
31fbf 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 52 65 61  olumn_bytes(pRea
31fc0 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a  der->pStmt, 0),.
31fc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31fc2 20 20 20 26 70 52 65 61 64 65 72 2d 3e 6c 65 61     &pReader->lea
31fc3 66 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20  fReader);.  }.  
31fc4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
31fc5 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 4c 65  ;.}../* Order Le
31fc6 61 76 65 73 52 65 61 64 65 72 73 20 62 79 20 74  avesReaders by t
31fc7 68 65 69 72 20 74 65 72 6d 2c 20 69 67 6e 6f 72  heir term, ignor
31fc8 69 6e 67 20 69 64 78 2e 20 20 52 65 61 64 65 72  ing idx.  Reader
31fc9 73 20 61 74 20 65 6f 66 0a 2a 2a 20 61 6c 77 61  s at eof.** alwa
31fca 79 73 20 73 6f 72 74 20 74 6f 20 74 68 65 20 65  ys sort to the e
31fcb 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nd..*/.static in
31fcc 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  t leavesReaderTe
31fcd 72 6d 43 6d 70 28 4c 65 61 76 65 73 52 65 61 64  rmCmp(LeavesRead
31fce 65 72 20 2a 6c 72 31 2c 20 4c 65 61 76 65 73 52  er *lr1, LeavesR
31fcf 65 61 64 65 72 20 2a 6c 72 32 29 7b 0a 20 20 69  eader *lr2){.  i
31fd0 66 28 20 6c 65 61 76 65 73 52 65 61 64 65 72 41  f( leavesReaderA
31fd1 74 45 6e 64 28 6c 72 31 29 20 29 7b 0a 20 20 20  tEnd(lr1) ){.   
31fd2 20 69 66 28 20 6c 65 61 76 65 73 52 65 61 64 65   if( leavesReade
31fd3 72 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72 65  rAtEnd(lr2) ) re
31fd4 74 75 72 6e 20 30 3b 0a 20 20 20 20 72 65 74 75  turn 0;.    retu
31fd5 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 1;.  }.  if( 
31fd6 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e  leavesReaderAtEn
31fd7 64 28 6c 72 32 29 20 29 20 72 65 74 75 72 6e 20  d(lr2) ) return 
31fd8 2d 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6c 65  -1;..  return le
31fd9 61 66 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  afReaderTermCmp(
31fda 26 6c 72 31 2d 3e 6c 65 61 66 52 65 61 64 65 72  &lr1->leafReader
31fdb 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
31fdc 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 65 61               lea
31fdd 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 6c 72  vesReaderTerm(lr
31fde 32 29 2c 20 6c 65 61 76 65 73 52 65 61 64 65 72  2), leavesReader
31fdf 54 65 72 6d 42 79 74 65 73 28 6c 72 32 29 2c 0a  TermBytes(lr2),.
31fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31fe1 20 20 20 20 20 20 20 20 20 20 20 30 29 3b 0a 7d             0);.}
31fe2 0a 0a 2f 2a 20 53 69 6d 69 6c 61 72 20 74 6f 20  ../* Similar to 
31fe3 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
31fe4 43 6d 70 28 29 2c 20 77 69 74 68 20 61 64 64 69  Cmp(), with addi
31fe5 74 69 6f 6e 61 6c 20 6f 72 64 65 72 69 6e 67 20  tional ordering 
31fe6 62 79 20 69 64 78 0a 2a 2a 20 73 6f 20 74 68 61  by idx.** so tha
31fe7 74 20 6f 6c 64 65 72 20 73 65 67 6d 65 6e 74 73  t older segments
31fe8 20 73 6f 72 74 20 62 65 66 6f 72 65 20 6e 65 77   sort before new
31fe9 65 72 20 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a  er segments..*/.
31fea 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65  static int leave
31feb 73 52 65 61 64 65 72 43 6d 70 28 4c 65 61 76 65  sReaderCmp(Leave
31fec 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c 65  sReader *lr1, Le
31fed 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32 29  avesReader *lr2)
31fee 7b 0a 20 20 69 6e 74 20 63 20 3d 20 6c 65 61 76  {.  int c = leav
31fef 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  esReaderTermCmp(
31ff0 6c 72 31 2c 20 6c 72 32 29 3b 0a 20 20 69 66 28  lr1, lr2);.  if(
31ff1 20 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20 63   c!=0 ) return c
31ff2 3b 0a 20 20 72 65 74 75 72 6e 20 6c 72 31 2d 3e  ;.  return lr1->
31ff3 69 64 78 2d 6c 72 32 2d 3e 69 64 78 3b 0a 7d 0a  idx-lr2->idx;.}.
31ff4 0a 2f 2a 20 41 73 73 75 6d 65 20 74 68 61 74 20  ./* Assume that 
31ff5 70 4c 72 5b 31 5d 2e 2e 70 4c 72 5b 6e 4c 72 5d  pLr[1]..pLr[nLr]
31ff6 20 61 72 65 20 73 6f 72 74 65 64 2e 20 20 42 75   are sorted.  Bu
31ff7 62 62 6c 65 20 70 4c 72 5b 30 5d 20 69 6e 74 6f  bble pLr[0] into
31ff8 20 69 74 73 0a 2a 2a 20 73 6f 72 74 65 64 20 70   its.** sorted p
31ff9 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  osition..*/.stat
31ffa 69 63 20 76 6f 69 64 20 6c 65 61 76 65 73 52 65  ic void leavesRe
31ffb 61 64 65 72 52 65 6f 72 64 65 72 28 4c 65 61 76  aderReorder(Leav
31ffc 65 73 52 65 61 64 65 72 20 2a 70 4c 72 2c 20 69  esReader *pLr, i
31ffd 6e 74 20 6e 4c 72 29 7b 0a 20 20 77 68 69 6c 65  nt nLr){.  while
31ffe 28 20 6e 4c 72 3e 31 20 26 26 20 6c 65 61 76 65  ( nLr>1 && leave
31fff 73 52 65 61 64 65 72 43 6d 70 28 70 4c 72 2c 20  sReaderCmp(pLr, 
32000 70 4c 72 2b 31 29 3e 30 20 29 7b 0a 20 20 20 20  pLr+1)>0 ){.    
32001 4c 65 61 76 65 73 52 65 61 64 65 72 20 74 6d 70  LeavesReader tmp
32002 20 3d 20 70 4c 72 5b 30 5d 3b 0a 20 20 20 20 70   = pLr[0];.    p
32003 4c 72 5b 30 5d 20 3d 20 70 4c 72 5b 31 5d 3b 0a  Lr[0] = pLr[1];.
32004 20 20 20 20 70 4c 72 5b 31 5d 20 3d 20 74 6d 70      pLr[1] = tmp
32005 3b 0a 20 20 20 20 6e 4c 72 2d 2d 3b 0a 20 20 20  ;.    nLr--;.   
32006 20 70 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f   pLr++;.  }.}../
32007 2a 20 49 6e 69 74 69 61 6c 69 7a 65 73 20 70 52  * Initializes pR
32008 65 61 64 65 72 73 20 77 69 74 68 20 74 68 65 20  eaders with the 
32009 73 65 67 6d 65 6e 74 73 20 66 72 6f 6d 20 6c 65  segments from le
3200a 76 65 6c 20 69 4c 65 76 65 6c 2c 20 72 65 74 75  vel iLevel, retu
3200b 72 6e 69 6e 67 0a 2a 2a 20 74 68 65 20 6e 75 6d  rning.** the num
3200c 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74 73 20  ber of segments 
3200d 69 6e 20 2a 70 69 52 65 61 64 65 72 73 2e 20 20  in *piReaders.  
3200e 4c 65 61 76 65 73 20 70 52 65 61 64 65 72 73 20  Leaves pReaders 
3200f 69 6e 20 73 6f 72 74 65 64 0a 2a 2a 20 6f 72 64  in sorted.** ord
32010 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
32011 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 73 49  t leavesReadersI
32012 6e 69 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  nit(fulltext_vta
32013 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c  b *v, int iLevel
32014 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32015 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c                 L
32016 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65  eavesReader *pRe
32017 61 64 65 72 73 2c 20 69 6e 74 20 2a 70 69 52 65  aders, int *piRe
32018 61 64 65 72 73 29 7b 0a 20 20 73 71 6c 69 74 65  aders){.  sqlite
32019 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
3201a 20 69 2c 20 72 63 20 3d 20 73 71 6c 5f 67 65 74   i, rc = sql_get
3201b 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45  _statement(v, SE
3201c 47 44 49 52 5f 53 45 4c 45 43 54 5f 4c 45 56 45  GDIR_SELECT_LEVE
3201d 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  L_STMT, &s);.  i
3201e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3201f 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
32020 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
32021 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65  nd_int(s, 1, iLe
32022 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  vel);.  if( rc!=
32023 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
32024 72 6e 20 72 63 3b 0a 0a 20 20 69 20 3d 20 30 3b  rn rc;..  i = 0;
32025 0a 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20  .  while( (rc = 
32026 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 29  sqlite3_step(s))
32027 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
32028 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
32029 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74 65   iStart = sqlite
3202a 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73  3_column_int64(s
3202b 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
3202c 5f 69 6e 74 36 34 20 69 45 6e 64 20 3d 20 73 71  _int64 iEnd = sq
3202d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
3202e 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20 63 6f  64(s, 1);.    co
3202f 6e 73 74 20 63 68 61 72 20 2a 70 52 6f 6f 74 44  nst char *pRootD
32030 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ata = sqlite3_co
32031 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32 29 3b  lumn_blob(s, 2);
32032 0a 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 44 61  .    int nRootDa
32033 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ta = sqlite3_col
32034 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29 3b  umn_bytes(s, 2);
32035 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c  ..    assert( i<
32036 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 20  MERGE_COUNT );. 
32037 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65     rc = leavesRe
32038 61 64 65 72 49 6e 69 74 28 76 2c 20 69 2c 20 69  aderInit(v, i, i
32039 53 74 61 72 74 2c 20 69 45 6e 64 2c 20 70 52 6f  Start, iEnd, pRo
3203a 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74  otData, nRootDat
3203b 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
3203c 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 52               &pR
3203d 65 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 20 20  eaders[i]);.    
3203e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3203f 4b 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20  K ) break;..    
32040 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  i++;.  }.  if( r
32041 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
32042 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d  {.    while( i--
32043 3e 30 20 29 7b 0a 20 20 20 20 20 20 6c 65 61 76  >0 ){.      leav
32044 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  esReaderDestroy(
32045 26 70 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a 20  &pReaders[i]);. 
32046 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
32047 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 52 65  rc;.  }..  *piRe
32048 61 64 65 72 73 20 3d 20 69 3b 0a 0a 20 20 2f 2a  aders = i;..  /*
32049 20 4c 65 61 76 65 20 6f 75 72 20 72 65 73 75 6c   Leave our resul
3204a 74 73 20 73 6f 72 74 65 64 20 62 79 20 74 65 72  ts sorted by ter
3204b 6d 2c 20 74 68 65 6e 20 61 67 65 2e 20 2a 2f 0a  m, then age. */.
3204c 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 29 7b 0a    while( i-- ){.
3204d 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72      leavesReader
3204e 52 65 6f 72 64 65 72 28 70 52 65 61 64 65 72 73  Reorder(pReaders
3204f 2b 69 2c 20 2a 70 69 52 65 61 64 65 72 73 2d 69  +i, *piReaders-i
32050 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
32051 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
32052 20 4d 65 72 67 65 20 64 6f 63 6c 69 73 74 73 20   Merge doclists 
32053 66 72 6f 6d 20 70 52 65 61 64 65 72 73 5b 6e 52  from pReaders[nR
32054 65 61 64 65 72 73 5d 20 69 6e 74 6f 20 61 20 73  eaders] into a s
32055 69 6e 67 6c 65 20 64 6f 63 6c 69 73 74 2c 20 77  ingle doclist, w
32056 68 69 63 68 0a 2a 2a 20 69 73 20 77 72 69 74 74  hich.** is writt
32057 65 6e 20 74 6f 20 70 57 72 69 74 65 72 2e 20 20  en to pWriter.  
32058 41 73 73 75 6d 65 73 20 70 52 65 61 64 65 72 73  Assumes pReaders
32059 20 69 73 20 6f 72 64 65 72 65 64 20 6f 6c 64 65   is ordered olde
3205a 73 74 20 74 6f 0a 2a 2a 20 6e 65 77 65 73 74 2e  st to.** newest.
3205b 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
3205c 73 29 20 43 6f 6e 73 69 64 65 72 20 70 75 74 74  s) Consider putt
3205d 69 6e 67 20 74 68 69 73 20 69 6e 6c 69 6e 65 20  ing this inline 
3205e 69 6e 20 73 65 67 6d 65 6e 74 4d 65 72 67 65 28  in segmentMerge(
3205f 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ). */.static int
32060 20 6c 65 61 76 65 73 52 65 61 64 65 72 73 4d 65   leavesReadersMe
32061 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rge(fulltext_vta
32062 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20 20  b *v,.          
32063 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32064 20 20 20 20 4c 65 61 76 65 73 52 65 61 64 65 72      LeavesReader
32065 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20   *pReaders, int 
32066 6e 52 65 61 64 65 72 73 2c 0a 20 20 20 20 20 20  nReaders,.      
32067 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32068 20 20 20 20 20 20 20 20 4c 65 61 66 57 72 69 74          LeafWrit
32069 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
3206a 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64 65  DLReader dlReade
3206b 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b  rs[MERGE_COUNT];
3206c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
3206d 54 65 72 6d 20 3d 20 6c 65 61 76 65 73 52 65 61  Term = leavesRea
3206e 64 65 72 54 65 72 6d 28 70 52 65 61 64 65 72 73  derTerm(pReaders
3206f 29 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 54 65 72  );.  int i, nTer
32070 6d 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72  m = leavesReader
32071 54 65 72 6d 42 79 74 65 73 28 70 52 65 61 64 65  TermBytes(pReade
32072 72 73 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  rs);..  assert( 
32073 6e 52 65 61 64 65 72 73 3c 3d 4d 45 52 47 45 5f  nReaders<=MERGE_
32074 43 4f 55 4e 54 20 29 3b 0a 0a 20 20 66 6f 72 28  COUNT );..  for(
32075 69 3d 30 3b 20 69 3c 6e 52 65 61 64 65 72 73 3b  i=0; i<nReaders;
32076 20 69 2b 2b 29 7b 0a 20 20 20 20 64 6c 72 49 6e   i++){.    dlrIn
32077 69 74 28 26 64 6c 52 65 61 64 65 72 73 5b 69 5d  it(&dlReaders[i]
32078 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20  , DL_DEFAULT,.  
32079 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73            leaves
3207a 52 65 61 64 65 72 44 61 74 61 28 70 52 65 61 64  ReaderData(pRead
3207b 65 72 73 2b 69 29 2c 0a 20 20 20 20 20 20 20 20  ers+i),.        
3207c 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72      leavesReader
3207d 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64 65  DataBytes(pReade
3207e 72 73 2b 69 29 29 3b 0a 20 20 7d 0a 0a 20 20 72  rs+i));.  }..  r
3207f 65 74 75 72 6e 20 6c 65 61 66 57 72 69 74 65 72  eturn leafWriter
32080 53 74 65 70 4d 65 72 67 65 28 76 2c 20 70 57 72  StepMerge(v, pWr
32081 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65  iter, pTerm, nTe
32082 72 6d 2c 20 64 6c 52 65 61 64 65 72 73 2c 20 6e  rm, dlReaders, n
32083 52 65 61 64 65 72 73 29 3b 0a 7d 0a 0a 2f 2a 20  Readers);.}../* 
32084 46 6f 72 77 61 72 64 20 72 65 66 20 64 75 65 20  Forward ref due 
32085 74 6f 20 6d 75 74 75 61 6c 20 72 65 63 75 72 73  to mutual recurs
32086 69 6f 6e 20 77 69 74 68 20 73 65 67 64 69 72 4e  ion with segdirN
32087 65 78 74 49 6e 64 65 78 28 29 2e 20 2a 2f 0a 73  extIndex(). */.s
32088 74 61 74 69 63 20 69 6e 74 20 73 65 67 6d 65 6e  tatic int segmen
32089 74 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74 5f  tMerge(fulltext_
3208a 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65  vtab *v, int iLe
3208b 76 65 6c 29 3b 0a 0a 2f 2a 20 50 75 74 20 74 68  vel);../* Put th
3208c 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c 65  e next available
3208d 20 69 6e 64 65 78 20 61 74 20 69 4c 65 76 65 6c   index at iLevel
3208e 20 69 6e 74 6f 20 2a 70 69 64 78 2e 20 20 49 66   into *pidx.  If
3208f 20 69 4c 65 76 65 6c 0a 2a 2a 20 61 6c 72 65 61   iLevel.** alrea
32090 64 79 20 68 61 73 20 4d 45 52 47 45 5f 43 4f 55  dy has MERGE_COU
32091 4e 54 20 73 65 67 6d 65 6e 74 73 2c 20 74 68 65  NT segments, the
32092 79 20 61 72 65 20 6d 65 72 67 65 64 20 74 6f 20  y are merged to 
32093 61 20 68 69 67 68 65 72 0a 2a 2a 20 6c 65 76 65  a higher.** leve
32094 6c 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 2e 0a  l to make room..
32095 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
32096 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 66 75  gdirNextIndex(fu
32097 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
32098 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20  int iLevel, int 
32099 2a 70 69 64 78 29 7b 0a 20 20 69 6e 74 20 72 63  *pidx){.  int rc
3209a 20 3d 20 73 65 67 64 69 72 5f 6d 61 78 5f 69 6e   = segdir_max_in
3209b 64 65 78 28 76 2c 20 69 4c 65 76 65 6c 2c 20 70  dex(v, iLevel, p
3209c 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  idx);.  if( rc==
3209d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 20 20  SQLITE_DONE ){  
3209e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
3209f 6f 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c  o segments at iL
320a0 65 76 65 6c 2e 20 2a 2f 0a 20 20 20 20 2a 70 69  evel. */.    *pi
320a1 64 78 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  dx = 0;.  }else 
320a2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
320a3 4f 57 20 29 7b 0a 20 20 20 20 69 66 28 20 2a 70  OW ){.    if( *p
320a4 69 64 78 3d 3d 28 4d 45 52 47 45 5f 43 4f 55 4e  idx==(MERGE_COUN
320a5 54 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72 63  T-1) ){.      rc
320a6 20 3d 20 73 65 67 6d 65 6e 74 4d 65 72 67 65 28   = segmentMerge(
320a7 76 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 20 20  v, iLevel);.    
320a8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
320a9 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
320aa 0a 20 20 20 20 20 20 2a 70 69 64 78 20 3d 20 30  .      *pidx = 0
320ab 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
320ac 20 20 20 28 2a 70 69 64 78 29 2b 2b 3b 0a 20 20     (*pidx)++;.  
320ad 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
320ae 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
320af 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
320b0 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 65 72 67 65 20  OK;.}../* Merge 
320b1 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d  MERGE_COUNT segm
320b2 65 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 20 69  ents at iLevel i
320b3 6e 74 6f 20 61 20 6e 65 77 20 73 65 67 6d 65 6e  nto a new segmen
320b4 74 20 61 74 0a 2a 2a 20 69 4c 65 76 65 6c 2b 31  t at.** iLevel+1
320b5 2e 20 20 49 66 20 69 4c 65 76 65 6c 2b 31 20 69  .  If iLevel+1 i
320b6 73 20 61 6c 72 65 61 64 79 20 66 75 6c 6c 20 6f  s already full o
320b7 66 20 73 65 67 6d 65 6e 74 73 2c 20 74 68 6f 73  f segments, thos
320b8 65 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 6d 65 72  e will be.** mer
320b9 67 65 64 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d  ged to make room
320ba 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
320bb 73 65 67 6d 65 6e 74 4d 65 72 67 65 28 66 75 6c  segmentMerge(ful
320bc 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
320bd 6e 74 20 69 4c 65 76 65 6c 29 7b 0a 20 20 4c 65  nt iLevel){.  Le
320be 61 66 57 72 69 74 65 72 20 77 72 69 74 65 72 3b  afWriter writer;
320bf 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65 72 20  .  LeavesReader 
320c0 6c 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d  lrs[MERGE_COUNT]
320c1 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c 20 69  ;.  int i, rc, i
320c2 64 78 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44 65  dx = 0;..  /* De
320c3 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65 78 74  termine the next
320c4 20 61 76 61 69 6c 61 62 6c 65 20 73 65 67 6d 65   available segme
320c5 6e 74 20 69 6e 64 65 78 20 61 74 20 74 68 65 20  nt index at the 
320c6 6e 65 78 74 20 6c 65 76 65 6c 2c 0a 20 20 2a 2a  next level,.  **
320c7 20 6d 65 72 67 69 6e 67 20 61 73 20 6e 65 63 65   merging as nece
320c8 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 72 63  ssary..  */.  rc
320c9 20 3d 20 73 65 67 64 69 72 4e 65 78 74 49 6e 64   = segdirNextInd
320ca 65 78 28 76 2c 20 69 4c 65 76 65 6c 2b 31 2c 20  ex(v, iLevel+1, 
320cb 26 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21  &idx);.  if( rc!
320cc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
320cd 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 4f  urn rc;..  /* TO
320ce 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 61  DO(shess) This a
320cf 73 73 75 6d 65 73 20 74 68 61 74 20 77 65 27 6c  ssumes that we'l
320d0 6c 20 61 6c 77 61 79 73 20 73 65 65 20 65 78 61  l always see exa
320d1 63 74 6c 79 0a 20 20 2a 2a 20 4d 45 52 47 45 5f  ctly.  ** MERGE_
320d2 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 20 74  COUNT segments t
320d3 6f 20 6d 65 72 67 65 20 61 74 20 61 20 67 69 76  o merge at a giv
320d4 65 6e 20 6c 65 76 65 6c 2e 20 20 54 68 61 74 20  en level.  That 
320d5 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 62 72 6f  will be.  ** bro
320d6 6b 65 6e 20 69 66 20 77 65 20 61 6c 6c 6f 77 20  ken if we allow 
320d7 74 68 65 20 64 65 76 65 6c 6f 70 65 72 20 74 6f  the developer to
320d8 20 72 65 71 75 65 73 74 20 70 72 65 65 6d 70 74   request preempt
320d9 69 76 65 20 6f 72 0a 20 20 2a 2a 20 64 65 66 65  ive or.  ** defe
320da 72 72 65 64 20 6d 65 72 67 69 6e 67 2e 0a 20 20  rred merging..  
320db 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6c 72 73  */.  memset(&lrs
320dc 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28 6c  , '\0', sizeof(l
320dd 72 73 29 29 3b 0a 20 20 72 63 20 3d 20 6c 65 61  rs));.  rc = lea
320de 76 65 73 52 65 61 64 65 72 73 49 6e 69 74 28 76  vesReadersInit(v
320df 2c 20 69 4c 65 76 65 6c 2c 20 6c 72 73 2c 20 26  , iLevel, lrs, &
320e0 69 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  i);.  if( rc!=SQ
320e1 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
320e2 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69   rc;.  assert( i
320e3 3d 3d 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b  ==MERGE_COUNT );
320e4 0a 0a 20 20 6c 65 61 66 57 72 69 74 65 72 49 6e  ..  leafWriterIn
320e5 69 74 28 69 4c 65 76 65 6c 2b 31 2c 20 69 64 78  it(iLevel+1, idx
320e6 2c 20 26 77 72 69 74 65 72 29 3b 0a 0a 20 20 2f  , &writer);..  /
320e7 2a 20 53 69 6e 63 65 20 6c 65 61 76 65 73 52 65  * Since leavesRe
320e8 61 64 65 72 52 65 6f 72 64 65 72 28 29 20 70 75  aderReorder() pu
320e9 73 68 65 73 20 72 65 61 64 65 72 73 20 61 74 20  shes readers at 
320ea 65 6f 66 20 74 6f 20 74 68 65 20 65 6e 64 2c 0a  eof to the end,.
320eb 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 66 69    ** when the fi
320ec 72 73 74 20 72 65 61 64 65 72 20 69 73 20 65 6d  rst reader is em
320ed 70 74 79 2c 20 61 6c 6c 20 77 69 6c 6c 20 62 65  pty, all will be
320ee 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 77   empty..  */.  w
320ef 68 69 6c 65 28 20 21 6c 65 61 76 65 73 52 65 61  hile( !leavesRea
320f0 64 65 72 41 74 45 6e 64 28 6c 72 73 29 20 29 7b  derAtEnd(lrs) ){
320f1 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  .    /* Figure o
320f2 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 61 64  ut how many read
320f3 65 72 73 20 73 68 61 72 65 20 74 68 65 69 72 20  ers share their 
320f4 6e 65 78 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20  next term. */.  
320f5 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4d 45 52    for(i=1; i<MER
320f6 47 45 5f 43 4f 55 4e 54 20 26 26 20 21 6c 65 61  GE_COUNT && !lea
320f7 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c  vesReaderAtEnd(l
320f8 72 73 2b 69 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  rs+i); i++){.   
320f9 20 20 20 69 66 28 20 30 21 3d 6c 65 61 76 65 73     if( 0!=leaves
320fa 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 6c 72  ReaderTermCmp(lr
320fb 73 2c 20 6c 72 73 2b 69 29 20 29 20 62 72 65 61  s, lrs+i) ) brea
320fc 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63  k;.    }..    rc
320fd 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 73   = leavesReaders
320fe 4d 65 72 67 65 28 76 2c 20 6c 72 73 2c 20 69 2c  Merge(v, lrs, i,
320ff 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 69   &writer);.    i
32100 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
32101 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20   ) goto err;..  
32102 20 20 2f 2a 20 53 74 65 70 20 66 6f 72 77 61 72    /* Step forwar
32103 64 20 74 68 6f 73 65 20 74 68 61 74 20 77 65 72  d those that wer
32104 65 20 6d 65 72 67 65 64 2e 20 2a 2f 0a 20 20 20  e merged. */.   
32105 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29 7b   while( i-->0 ){
32106 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65 61 76  .      rc = leav
32107 65 73 52 65 61 64 65 72 53 74 65 70 28 76 2c 20  esReaderStep(v, 
32108 6c 72 73 2b 69 29 3b 0a 20 20 20 20 20 20 69 66  lrs+i);.      if
32109 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3210a 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20  ) goto err;..   
3210b 20 20 20 2f 2a 20 52 65 6f 72 64 65 72 20 62 79     /* Reorder by
3210c 20 74 65 72 6d 2c 20 74 68 65 6e 20 62 79 20 61   term, then by a
3210d 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 6c 65 61  ge. */.      lea
3210e 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72  vesReaderReorder
3210f 28 6c 72 73 2b 69 2c 20 4d 45 52 47 45 5f 43 4f  (lrs+i, MERGE_CO
32110 55 4e 54 2d 69 29 3b 0a 20 20 20 20 7d 0a 20 20  UNT-i);.    }.  
32111 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  }..  for(i=0; i<
32112 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20 69 2b 2b  MERGE_COUNT; i++
32113 29 7b 0a 20 20 20 20 6c 65 61 76 65 73 52 65 61  ){.    leavesRea
32114 64 65 72 44 65 73 74 72 6f 79 28 26 6c 72 73 5b  derDestroy(&lrs[
32115 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d  i]);.  }..  rc =
32116 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c   leafWriterFinal
32117 69 7a 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b  ize(v, &writer);
32118 0a 20 20 6c 65 61 66 57 72 69 74 65 72 44 65 73  .  leafWriterDes
32119 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 20  troy(&writer);. 
3211a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3211b 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
3211c 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65  .  /* Delete the
3211d 20 6d 65 72 67 65 64 20 73 65 67 6d 65 6e 74 20   merged segment 
3211e 64 61 74 61 2e 20 2a 2f 0a 20 20 72 65 74 75 72  data. */.  retur
3211f 6e 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65 28  n segdir_delete(
32120 76 2c 20 69 4c 65 76 65 6c 29 3b 0a 0a 20 65 72  v, iLevel);.. er
32121 72 3a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  r:.  for(i=0; i<
32122 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20 69 2b 2b  MERGE_COUNT; i++
32123 29 7b 0a 20 20 20 20 6c 65 61 76 65 73 52 65 61  ){.    leavesRea
32124 64 65 72 44 65 73 74 72 6f 79 28 26 6c 72 73 5b  derDestroy(&lrs[
32125 69 5d 29 3b 0a 20 20 7d 0a 20 20 6c 65 61 66 57  i]);.  }.  leafW
32126 72 69 74 65 72 44 65 73 74 72 6f 79 28 26 77 72  riterDestroy(&wr
32127 69 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  iter);.  return 
32128 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 75 6d 75  rc;.}../* Accumu
32129 6c 61 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f  late the union o
3212a 66 20 2a 61 63 63 20 61 6e 64 20 2a 70 44 61 74  f *acc and *pDat
3212b 61 20 69 6e 74 6f 20 2a 61 63 63 2e 20 2a 2f 0a  a into *acc. */.
3212c 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c  static void docL
3212d 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e 69  istAccumulateUni
3212e 6f 6e 28 44 61 74 61 42 75 66 66 65 72 20 2a 61  on(DataBuffer *a
3212f 63 63 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  cc,.            
32130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32131 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
32132 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
32133 61 74 61 29 20 7b 0a 20 20 44 61 74 61 42 75 66  ata) {.  DataBuf
32134 66 65 72 20 74 6d 70 20 3d 20 2a 61 63 63 3b 0a  fer tmp = *acc;.
32135 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
32136 28 61 63 63 2c 20 74 6d 70 2e 6e 44 61 74 61 2b  (acc, tmp.nData+
32137 6e 44 61 74 61 29 3b 0a 20 20 64 6f 63 4c 69 73  nData);.  docLis
32138 74 55 6e 69 6f 6e 28 74 6d 70 2e 70 44 61 74 61  tUnion(tmp.pData
32139 2c 20 74 6d 70 2e 6e 44 61 74 61 2c 20 70 44 61  , tmp.nData, pDa
3213a 74 61 2c 20 6e 44 61 74 61 2c 20 61 63 63 29 3b  ta, nData, acc);
3213b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
3213c 74 72 6f 79 28 26 74 6d 70 29 3b 0a 7d 0a 0a 2f  troy(&tmp);.}../
3213d 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49 74  * TODO(shess) It
3213e 20 6d 69 67 68 74 20 62 65 20 69 6e 74 65 72 65   might be intere
3213f 73 74 69 6e 67 20 74 6f 20 65 78 70 6c 6f 72 65  sting to explore
32140 20 64 69 66 66 65 72 65 6e 74 20 6d 65 72 67 65   different merge
32141 0a 2a 2a 20 73 74 72 61 74 65 67 69 65 73 2c 20  .** strategies, 
32142 68 65 72 65 2e 20 20 46 6f 72 20 69 6e 73 74 61  here.  For insta
32143 6e 63 65 2c 20 73 69 6e 63 65 20 74 68 69 73 20  nce, since this 
32144 69 73 20 61 20 73 6f 72 74 65 64 20 6d 65 72 67  is a sorted merg
32145 65 2c 20 77 65 0a 2a 2a 20 63 6f 75 6c 64 20 65  e, we.** could e
32146 61 73 69 6c 79 20 6d 65 72 67 65 20 6d 61 6e 79  asily merge many
32147 20 64 6f 63 6c 69 73 74 73 20 69 6e 20 70 61 72   doclists in par
32148 61 6c 6c 65 6c 2e 20 20 57 69 74 68 20 73 6f 6d  allel.  With som
32149 65 0a 2a 2a 20 63 6f 6d 70 72 65 68 65 6e 73 69  e.** comprehensi
3214a 6f 6e 20 6f 66 20 74 68 65 20 73 74 6f 72 61 67  on of the storag
3214b 65 20 66 6f 72 6d 61 74 2c 20 77 65 20 63 6f 75  e format, we cou
3214c 6c 64 20 6d 65 72 67 65 20 61 6c 6c 20 6f 66 20  ld merge all of 
3214d 74 68 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 20  the.** doclists 
3214e 77 69 74 68 69 6e 20 61 20 6c 65 61 66 20 6e 6f  within a leaf no
3214f 64 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d  de directly from
32150 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 27 73   the leaf node's
32151 20 73 74 6f 72 61 67 65 2e 0a 2a 2a 20 49 74 20   storage..** It 
32152 6d 61 79 20 62 65 20 77 6f 72 74 68 77 68 69 6c  may be worthwhil
32153 65 20 74 6f 20 6d 65 72 67 65 20 73 6d 61 6c 6c  e to merge small
32154 65 72 20 64 6f 63 6c 69 73 74 73 20 62 65 66 6f  er doclists befo
32155 72 65 20 6c 61 72 67 65 72 0a 2a 2a 20 64 6f 63  re larger.** doc
32156 6c 69 73 74 73 2c 20 73 69 6e 63 65 20 74 68 65  lists, since the
32157 79 20 63 61 6e 20 62 65 20 74 72 61 76 65 72 73  y can be travers
32158 65 64 20 6d 6f 72 65 20 71 75 69 63 6b 6c 79 20  ed more quickly 
32159 2d 20 62 75 74 20 74 68 65 0a 2a 2a 20 72 65 73  - but the.** res
3215a 75 6c 74 73 20 6d 61 79 20 68 61 76 65 20 6c 65  ults may have le
3215b 73 73 20 6f 76 65 72 6c 61 70 2c 20 6d 61 6b 69  ss overlap, maki
3215c 6e 67 20 74 68 65 6d 20 6d 6f 72 65 20 65 78 70  ng them more exp
3215d 65 6e 73 69 76 65 20 69 6e 20 61 0a 2a 2a 20 64  ensive in a.** d
3215e 69 66 66 65 72 65 6e 74 20 77 61 79 2e 0a 2a 2f  ifferent way..*/
3215f 0a 0a 2f 2a 20 53 63 61 6e 20 70 52 65 61 64 65  ../* Scan pReade
32160 72 20 66 6f 72 20 70 54 65 72 6d 2f 6e 54 65 72  r for pTerm/nTer
32161 6d 2c 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65  m, and merge the
32162 20 74 65 72 6d 27 73 20 64 6f 63 6c 69 73 74 20   term's doclist 
32163 6f 76 65 72 0a 2a 2a 20 2a 6f 75 74 20 28 61 6e  over.** *out (an
32164 79 20 64 6f 63 6c 69 73 74 73 20 77 69 74 68 20  y doclists with 
32165 64 75 70 6c 69 63 61 74 65 20 64 6f 63 69 64 73  duplicate docids
32166 20 6f 76 65 72 77 72 69 74 65 20 74 68 6f 73 65   overwrite those
32167 20 69 6e 20 2a 6f 75 74 29 2e 0a 2a 2a 20 49 6e   in *out)..** In
32168 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20  ternal function 
32169 66 6f 72 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c  for loadSegmentL
3216a 65 61 66 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  eaf()..*/.static
3216b 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74   int loadSegment
3216c 4c 65 61 76 65 73 49 6e 74 28 66 75 6c 6c 74 65  LeavesInt(fullte
3216d 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 76  xt_vtab *v, Leav
3216e 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
3216f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
32170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32171 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
32172 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c  Term, int nTerm,
32173 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20   int isPrefix,. 
32174 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32175 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44                 D
32176 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b  ataBuffer *out){
32177 0a 20 20 2f 2a 20 64 6f 63 6c 69 73 74 20 64 61  .  /* doclist da
32178 74 61 20 69 73 20 61 63 63 75 6d 75 6c 61 74 65  ta is accumulate
32179 64 20 69 6e 74 6f 20 70 42 75 66 66 65 72 73 20  d into pBuffers 
3217a 73 69 6d 69 6c 61 72 20 74 6f 20 68 6f 77 20 6f  similar to how o
3217b 6e 65 20 64 6f 65 73 0a 20 20 2a 2a 20 69 6e 63  ne does.  ** inc
3217c 72 65 6d 65 6e 74 20 69 6e 20 62 69 6e 61 72 79  rement in binary
3217d 20 61 72 69 74 68 6d 65 74 69 63 2e 20 20 49 66   arithmetic.  If
3217e 20 69 6e 64 65 78 20 30 20 69 73 20 65 6d 70 74   index 0 is empt
3217f 79 2c 20 74 68 65 20 64 61 74 61 20 69 73 0a 20  y, the data is. 
32180 20 2a 2a 20 73 74 6f 72 65 64 20 74 68 65 72 65   ** stored there
32181 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 64  .  If there is d
32182 61 74 61 20 74 68 65 72 65 2c 20 69 74 20 69 73  ata there, it is
32183 20 6d 65 72 67 65 64 20 61 6e 64 20 74 68 65 0a   merged and the.
32184 20 20 2a 2a 20 72 65 73 75 6c 74 73 20 63 61 72    ** results car
32185 72 69 65 64 20 69 6e 74 6f 20 70 6f 73 69 74 69  ried into positi
32186 6f 6e 20 31 2c 20 77 69 74 68 20 66 75 72 74 68  on 1, with furth
32187 65 72 20 6d 65 72 67 65 2d 61 6e 64 2d 63 61 72  er merge-and-car
32188 72 79 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 61 6e  ry.  ** until an
32189 20 65 6d 70 74 79 20 70 6f 73 69 74 69 6f 6e 20   empty position 
3218a 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20  is found..  */. 
3218b 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75   DataBuffer *pBu
3218c 66 66 65 72 73 20 3d 20 4e 55 4c 4c 3b 0a 20 20  ffers = NULL;.  
3218d 69 6e 74 20 6e 42 75 66 66 65 72 73 20 3d 20 30  int nBuffers = 0
3218e 2c 20 6e 4d 61 78 42 75 66 66 65 72 73 20 3d 20  , nMaxBuffers = 
3218f 30 2c 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  0, rc;..  assert
32190 28 20 6e 54 65 72 6d 3e 30 20 29 3b 0a 0a 20 20  ( nTerm>0 );..  
32191 66 6f 72 28 72 63 3d 53 51 4c 49 54 45 5f 4f 4b  for(rc=SQLITE_OK
32192 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
32193 26 26 20 21 6c 65 61 76 65 73 52 65 61 64 65 72  && !leavesReader
32194 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 3b 0a  AtEnd(pReader);.
32195 20 20 20 20 20 20 72 63 3d 6c 65 61 76 65 73 52        rc=leavesR
32196 65 61 64 65 72 53 74 65 70 28 76 2c 20 70 52 65  eaderStep(v, pRe
32197 61 64 65 72 29 29 7b 0a 20 20 20 20 2f 2a 20 54  ader)){.    /* T
32198 4f 44 4f 28 73 68 65 73 73 29 20 52 65 61 6c 6c  ODO(shess) Reall
32199 79 20 77 61 6e 74 20 6c 65 61 76 65 73 52 65 61  y want leavesRea
3219a 64 65 72 54 65 72 6d 43 6d 70 28 29 2c 20 62 75  derTermCmp(), bu
3219b 74 20 74 68 61 74 20 6e 61 6d 65 20 69 73 0a 20  t that name is. 
3219c 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 74 61     ** already ta
3219d 6b 65 6e 20 74 6f 20 63 6f 6d 70 61 72 65 20 74  ken to compare t
3219e 68 65 20 74 65 72 6d 73 20 6f 66 20 74 77 6f 20  he terms of two 
3219f 4c 65 61 76 65 73 52 65 61 64 65 72 73 2e 20 20  LeavesReaders.  
321a0 54 68 69 6e 6b 0a 20 20 20 20 2a 2a 20 6f 6e 20  Think.    ** on 
321a1 61 20 62 65 74 74 65 72 20 6e 61 6d 65 2e 20 20  a better name.  
321a2 5b 4d 65 61 6e 77 68 69 6c 65 2c 20 62 72 65 61  [Meanwhile, brea
321a3 6b 20 65 6e 63 61 70 73 75 6c 61 74 69 6f 6e 20  k encapsulation 
321a4 72 61 74 68 65 72 20 74 68 61 6e 0a 20 20 20 20  rather than.    
321a5 2a 2a 20 75 73 65 20 61 20 63 6f 6e 66 75 73 69  ** use a confusi
321a6 6e 67 20 6e 61 6d 65 2e 5d 0a 20 20 20 20 2a 2f  ng name.].    */
321a7 0a 20 20 20 20 69 6e 74 20 63 20 3d 20 6c 65 61  .    int c = lea
321a8 66 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26  fReaderTermCmp(&
321a9 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61  pReader->leafRea
321aa 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72  der, pTerm, nTer
321ab 6d 2c 20 69 73 50 72 65 66 69 78 29 3b 0a 20 20  m, isPrefix);.  
321ac 20 20 69 66 28 20 63 3e 30 20 29 20 62 72 65 61    if( c>0 ) brea
321ad 6b 3b 20 20 20 20 20 20 2f 2a 20 50 61 73 74 20  k;      /* Past 
321ae 61 6e 79 20 70 6f 73 73 69 62 6c 65 20 6d 61 74  any possible mat
321af 63 68 65 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ches. */.    if(
321b0 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63   c==0 ){.      c
321b1 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
321b2 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 44   = leavesReaderD
321b3 61 74 61 28 70 52 65 61 64 65 72 29 3b 0a 20 20  ata(pReader);.  
321b4 20 20 20 20 69 6e 74 20 69 42 75 66 66 65 72 2c      int iBuffer,
321b5 20 6e 44 61 74 61 20 3d 20 6c 65 61 76 65 73 52   nData = leavesR
321b6 65 61 64 65 72 44 61 74 61 42 79 74 65 73 28 70  eaderDataBytes(p
321b7 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 20 20  Reader);..      
321b8 2f 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72 73  /* Find the firs
321b9 74 20 65 6d 70 74 79 20 62 75 66 66 65 72 2e 20  t empty buffer. 
321ba 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 69 42 75  */.      for(iBu
321bb 66 66 65 72 3d 30 3b 20 69 42 75 66 66 65 72 3c  ffer=0; iBuffer<
321bc 6e 42 75 66 66 65 72 73 3b 20 2b 2b 69 42 75 66  nBuffers; ++iBuf
321bd 66 65 72 29 7b 0a 20 20 20 20 20 20 20 20 69 66  fer){.        if
321be 28 20 30 3d 3d 70 42 75 66 66 65 72 73 5b 69 42  ( 0==pBuffers[iB
321bf 75 66 66 65 72 5d 2e 6e 44 61 74 61 20 29 20 62  uffer].nData ) b
321c0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  reak;.      }.. 
321c1 20 20 20 20 20 2f 2a 20 4f 75 74 20 6f 66 20 62       /* Out of b
321c2 75 66 66 65 72 73 2c 20 61 64 64 20 61 6e 20 65  uffers, add an e
321c3 6d 70 74 79 20 6f 6e 65 2e 20 2a 2f 0a 20 20 20  mpty one. */.   
321c4 20 20 20 69 66 28 20 69 42 75 66 66 65 72 3d 3d     if( iBuffer==
321c5 6e 42 75 66 66 65 72 73 20 29 7b 0a 20 20 20 20  nBuffers ){.    
321c6 20 20 20 20 69 66 28 20 6e 42 75 66 66 65 72 73      if( nBuffers
321c7 3d 3d 6e 4d 61 78 42 75 66 66 65 72 73 20 29 7b  ==nMaxBuffers ){
321c8 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42  .          DataB
321c9 75 66 66 65 72 20 2a 70 3b 0a 20 20 20 20 20 20  uffer *p;.      
321ca 20 20 20 20 6e 4d 61 78 42 75 66 66 65 72 73 20      nMaxBuffers 
321cb 2b 3d 20 32 30 3b 0a 0a 20 20 20 20 20 20 20 20  += 20;..        
321cc 20 20 2f 2a 20 4d 61 6e 75 61 6c 20 72 65 61 6c    /* Manual real
321cd 6c 6f 63 20 73 6f 20 77 65 20 63 61 6e 20 68 61  loc so we can ha
321ce 6e 64 6c 65 20 4e 55 4c 4c 20 61 70 70 72 6f 70  ndle NULL approp
321cf 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20 20  riately. */.    
321d0 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65        p = sqlite
321d1 33 5f 6d 61 6c 6c 6f 63 28 6e 4d 61 78 42 75 66  3_malloc(nMaxBuf
321d2 66 65 72 73 2a 73 69 7a 65 6f 66 28 2a 70 42 75  fers*sizeof(*pBu
321d3 66 66 65 72 73 29 29 3b 0a 20 20 20 20 20 20 20  ffers));.       
321d4 20 20 20 69 66 28 20 70 3d 3d 4e 55 4c 4c 20 29     if( p==NULL )
321d5 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
321d6 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
321d7 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
321d8 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
321d9 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e  .          if( n
321da 42 75 66 66 65 72 73 3e 30 20 29 7b 0a 20 20 20  Buffers>0 ){.   
321db 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
321dc 70 42 75 66 66 65 72 73 21 3d 4e 55 4c 4c 29 3b  pBuffers!=NULL);
321dd 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d  .            mem
321de 63 70 79 28 70 2c 20 70 42 75 66 66 65 72 73 2c  cpy(p, pBuffers,
321df 20 6e 42 75 66 66 65 72 73 2a 73 69 7a 65 6f 66   nBuffers*sizeof
321e0 28 2a 70 42 75 66 66 65 72 73 29 29 3b 0a 20 20  (*pBuffers));.  
321e1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
321e2 33 5f 66 72 65 65 28 70 42 75 66 66 65 72 73 29  3_free(pBuffers)
321e3 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
321e4 20 20 20 20 20 20 20 20 70 42 75 66 66 65 72 73          pBuffers
321e5 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = p;.        }.
321e6 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
321e7 65 72 49 6e 69 74 28 26 28 70 42 75 66 66 65 72  erInit(&(pBuffer
321e8 73 5b 6e 42 75 66 66 65 72 73 5d 29 2c 20 30 29  s[nBuffers]), 0)
321e9 3b 0a 20 20 20 20 20 20 20 20 6e 42 75 66 66 65  ;.        nBuffe
321ea 72 73 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  rs++;.      }.. 
321eb 20 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20       /* At this 
321ec 70 6f 69 6e 74 2c 20 6d 75 73 74 20 68 61 76 65  point, must have
321ed 20 61 6e 20 65 6d 70 74 79 20 61 74 20 69 42 75   an empty at iBu
321ee 66 66 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 61  ffer. */.      a
321ef 73 73 65 72 74 28 69 42 75 66 66 65 72 3c 6e 42  ssert(iBuffer<nB
321f0 75 66 66 65 72 73 20 26 26 20 70 42 75 66 66 65  uffers && pBuffe
321f1 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61 74  rs[iBuffer].nDat
321f2 61 3d 3d 30 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  a==0);..      /*
321f3 20 49 66 20 65 6d 70 74 79 20 77 61 73 20 66 69   If empty was fi
321f4 72 73 74 20 62 75 66 66 65 72 2c 20 6e 6f 20 6e  rst buffer, no n
321f5 65 65 64 20 66 6f 72 20 6d 65 72 67 65 20 6c 6f  eed for merge lo
321f6 67 69 63 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  gic. */.      if
321f7 28 20 69 42 75 66 66 65 72 3d 3d 30 20 29 7b 0a  ( iBuffer==0 ){.
321f8 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
321f9 65 72 52 65 70 6c 61 63 65 28 26 28 70 42 75 66  erReplace(&(pBuf
321fa 66 65 72 73 5b 30 5d 29 2c 20 70 44 61 74 61 2c  fers[0]), pData,
321fb 20 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d   nData);.      }
321fc 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
321fd 20 70 41 63 63 20 69 73 20 74 68 65 20 65 6d 70   pAcc is the emp
321fe 74 79 20 62 75 66 66 65 72 20 74 68 65 20 6d 65  ty buffer the me
321ff 72 67 65 64 20 64 61 74 61 20 77 69 6c 6c 20 65  rged data will e
32200 6e 64 20 75 70 20 69 6e 2e 20 2a 2f 0a 20 20 20  nd up in. */.   
32201 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
32202 2a 70 41 63 63 20 3d 20 26 28 70 42 75 66 66 65  *pAcc = &(pBuffe
32203 72 73 5b 69 42 75 66 66 65 72 5d 29 3b 0a 20 20  rs[iBuffer]);.  
32204 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
32205 20 2a 70 20 3d 20 26 28 70 42 75 66 66 65 72 73   *p = &(pBuffers
32206 5b 30 5d 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f  [0]);..        /
32207 2a 20 48 61 6e 64 6c 65 20 70 6f 73 69 74 69 6f  * Handle positio
32208 6e 20 30 20 73 70 65 63 69 61 6c 6c 79 20 74 6f  n 0 specially to
32209 20 61 76 6f 69 64 20 6e 65 65 64 20 74 6f 20 70   avoid need to p
3220a 72 69 6d 65 20 70 41 63 63 0a 20 20 20 20 20 20  rime pAcc.      
3220b 20 20 2a 2a 20 77 69 74 68 20 70 44 61 74 61 2f    ** with pData/
3220c 6e 44 61 74 61 2e 0a 20 20 20 20 20 20 20 20 2a  nData..        *
3220d 2f 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75  /.        dataBu
3220e 66 66 65 72 53 77 61 70 28 70 2c 20 70 41 63 63  fferSwap(p, pAcc
3220f 29 3b 0a 20 20 20 20 20 20 20 20 64 6f 63 4c 69  );.        docLi
32210 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e 69 6f  stAccumulateUnio
32211 6e 28 70 41 63 63 2c 20 70 44 61 74 61 2c 20 6e  n(pAcc, pData, n
32212 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 20 20  Data);..        
32213 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65  /* Accumulate re
32214 6d 61 69 6e 69 6e 67 20 64 6f 63 6c 69 73 74 73  maining doclists
32215 20 69 6e 74 6f 20 70 41 63 63 2e 20 2a 2f 0a 20   into pAcc. */. 
32216 20 20 20 20 20 20 20 66 6f 72 28 2b 2b 70 3b 20         for(++p; 
32217 70 3c 70 41 63 63 3b 20 2b 2b 70 29 7b 0a 20 20  p<pAcc; ++p){.  
32218 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 41          docListA
32219 63 63 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 70  ccumulateUnion(p
3221a 41 63 63 2c 20 70 2d 3e 70 44 61 74 61 2c 20 70  Acc, p->pData, p
3221b 2d 3e 6e 44 61 74 61 29 3b 0a 0a 20 20 20 20 20  ->nData);..     
3221c 20 20 20 20 20 2f 2a 20 64 61 74 61 42 75 66 66       /* dataBuff
3221d 65 72 52 65 73 65 74 28 29 20 63 6f 75 6c 64 20  erReset() could 
3221e 61 6c 6c 6f 77 20 61 20 6c 61 72 67 65 20 64 6f  allow a large do
3221f 63 6c 69 73 74 20 74 6f 20 62 6c 6f 77 20 75 70  clist to blow up
32220 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 75  .          ** ou
32221 72 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65  r memory require
32222 6d 65 6e 74 73 2e 0a 20 20 20 20 20 20 20 20 20  ments..         
32223 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66   */.          if
32224 28 20 70 2d 3e 6e 43 61 70 61 63 69 74 79 3c 31  ( p->nCapacity<1
32225 30 32 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20  024 ){.         
32226 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73     dataBufferRes
32227 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20  et(p);.         
32228 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
32229 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
3222a 73 74 72 6f 79 28 70 29 3b 0a 20 20 20 20 20 20  stroy(p);.      
3222b 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
3222c 49 6e 69 74 28 70 2c 20 30 29 3b 0a 20 20 20 20  Init(p, 0);.    
3222d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3222e 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
3222f 20 20 7d 0a 0a 20 20 2f 2a 20 55 6e 69 6f 6e 20    }..  /* Union 
32230 61 6c 6c 20 74 68 65 20 64 6f 63 6c 69 73 74 73  all the doclists
32231 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 2a   together into *
32232 6f 75 74 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44  out. */.  /* TOD
32233 4f 28 73 68 65 73 73 29 20 57 68 61 74 20 69 66  O(shess) What if
32234 20 2a 6f 75 74 20 69 73 20 62 69 67 3f 20 20 53   *out is big?  S
32235 69 67 68 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63  igh. */.  if( rc
32236 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e  ==SQLITE_OK && n
32237 42 75 66 66 65 72 73 3e 30 20 29 7b 0a 20 20 20  Buffers>0 ){.   
32238 20 69 6e 74 20 69 42 75 66 66 65 72 3b 0a 20 20   int iBuffer;.  
32239 20 20 66 6f 72 28 69 42 75 66 66 65 72 3d 30 3b    for(iBuffer=0;
3223a 20 69 42 75 66 66 65 72 3c 6e 42 75 66 66 65 72   iBuffer<nBuffer
3223b 73 3b 20 2b 2b 69 42 75 66 66 65 72 29 7b 0a 20  s; ++iBuffer){. 
3223c 20 20 20 20 20 69 66 28 20 70 42 75 66 66 65 72       if( pBuffer
3223d 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61 74 61  s[iBuffer].nData
3223e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  >0 ){.        if
3223f 28 20 6f 75 74 2d 3e 6e 44 61 74 61 3d 3d 30 20  ( out->nData==0 
32240 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 61 74  ){.          dat
32241 61 42 75 66 66 65 72 53 77 61 70 28 6f 75 74 2c  aBufferSwap(out,
32242 20 26 28 70 42 75 66 66 65 72 73 5b 69 42 75 66   &(pBuffers[iBuf
32243 66 65 72 5d 29 29 3b 0a 20 20 20 20 20 20 20 20  fer]));.        
32244 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
32245 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61   docListAccumula
32246 74 65 55 6e 69 6f 6e 28 6f 75 74 2c 20 70 42 75  teUnion(out, pBu
32247 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 70  ffers[iBuffer].p
32248 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
32249 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3224a 20 20 20 20 20 20 20 70 42 75 66 66 65 72 73 5b         pBuffers[
3224b 69 42 75 66 66 65 72 5d 2e 6e 44 61 74 61 29 3b  iBuffer].nData);
3224c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3224d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
3224e 77 68 69 6c 65 28 20 6e 42 75 66 66 65 72 73 2d  while( nBuffers-
3224f 2d 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  - ){.    dataBuf
32250 66 65 72 44 65 73 74 72 6f 79 28 26 28 70 42 75  ferDestroy(&(pBu
32251 66 66 65 72 73 5b 6e 42 75 66 66 65 72 73 5d 29  ffers[nBuffers])
32252 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 75  );.  }.  if( pBu
32253 66 66 65 72 73 21 3d 4e 55 4c 4c 20 29 20 73 71  ffers!=NULL ) sq
32254 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75 66 66  lite3_free(pBuff
32255 65 72 73 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ers);..  return 
32256 72 63 3b 0a 7d 0a 0a 2f 2a 20 43 61 6c 6c 20 6c  rc;.}../* Call l
32257 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
32258 49 6e 74 28 29 20 77 69 74 68 20 70 44 61 74 61  Int() with pData
32259 2f 6e 44 61 74 61 20 61 73 20 69 6e 70 75 74 2e  /nData as input.
3225a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c   */.static int l
3225b 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 66 28 66  oadSegmentLeaf(f
3225c 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
3225d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
3225e 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20  ta, int nData,. 
3225f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32260 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
32261 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
32262 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72   nTerm, int isPr
32263 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20  efix,.          
32264 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32265 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74   DataBuffer *out
32266 29 7b 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65  ){.  LeavesReade
32267 72 20 72 65 61 64 65 72 3b 0a 20 20 69 6e 74 20  r reader;.  int 
32268 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e  rc;..  assert( n
32269 44 61 74 61 3e 31 20 29 3b 0a 20 20 61 73 73 65  Data>1 );.  asse
3226a 72 74 28 20 2a 70 44 61 74 61 3d 3d 27 5c 30 27  rt( *pData=='\0'
3226b 20 29 3b 0a 20 20 72 63 20 3d 20 6c 65 61 76 65   );.  rc = leave
3226c 73 52 65 61 64 65 72 49 6e 69 74 28 76 2c 20 30  sReaderInit(v, 0
3226d 2c 20 30 2c 20 30 2c 20 70 44 61 74 61 2c 20 6e  , 0, 0, pData, n
3226e 44 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b 0a  Data, &reader);.
3226f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32270 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
32271 0a 0a 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67  ..  rc = loadSeg
32272 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 76 2c  mentLeavesInt(v,
32273 20 26 72 65 61 64 65 72 2c 20 70 54 65 72 6d 2c   &reader, pTerm,
32274 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78   nTerm, isPrefix
32275 2c 20 6f 75 74 29 3b 0a 20 20 6c 65 61 76 65 73  , out);.  leaves
32276 52 65 61 64 65 72 52 65 73 65 74 28 26 72 65 61  ReaderReset(&rea
32277 64 65 72 29 3b 0a 20 20 6c 65 61 76 65 73 52 65  der);.  leavesRe
32278 61 64 65 72 44 65 73 74 72 6f 79 28 26 72 65 61  aderDestroy(&rea
32279 64 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  der);.  return r
3227a 63 3b 0a 7d 0a 0a 2f 2a 20 43 61 6c 6c 20 6c 6f  c;.}../* Call lo
3227b 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 49  adSegmentLeavesI
3227c 6e 74 28 29 20 77 69 74 68 20 74 68 65 20 6c 65  nt() with the le
3227d 61 66 20 6e 6f 64 65 73 20 66 72 6f 6d 20 69 53  af nodes from iS
3227e 74 61 72 74 4c 65 61 66 20 74 6f 0a 2a 2a 20 69  tartLeaf to.** i
3227f 45 6e 64 4c 65 61 66 20 28 69 6e 63 6c 75 73 69  EndLeaf (inclusi
32280 76 65 29 20 61 73 20 69 6e 70 75 74 2c 20 61 6e  ve) as input, an
32281 64 20 6d 65 72 67 65 20 74 68 65 20 72 65 73 75  d merge the resu
32282 6c 74 69 6e 67 20 64 6f 63 6c 69 73 74 20 69 6e  lting doclist in
32283 74 6f 0a 2a 2a 20 6f 75 74 2e 0a 2a 2f 0a 73 74  to.** out..*/.st
32284 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67  atic int loadSeg
32285 6d 65 6e 74 4c 65 61 76 65 73 28 66 75 6c 6c 74  mentLeaves(fullt
32286 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20  ext_vtab *v,.   
32287 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32288 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32289 5f 69 6e 74 36 34 20 69 53 74 61 72 74 4c 65 61  _int64 iStartLea
3228a 66 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  f, sqlite_int64 
3228b 69 45 6e 64 4c 65 61 66 2c 0a 20 20 20 20 20 20  iEndLeaf,.      
3228c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3228d 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
3228e 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
3228f 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
32290 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
32291 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32292 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29  DataBuffer *out)
32293 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4c 65  {.  int rc;.  Le
32294 61 76 65 73 52 65 61 64 65 72 20 72 65 61 64 65  avesReader reade
32295 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 53  r;..  assert( iS
32296 74 61 72 74 4c 65 61 66 3c 3d 69 45 6e 64 4c 65  tartLeaf<=iEndLe
32297 61 66 20 29 3b 0a 20 20 72 63 20 3d 20 6c 65 61  af );.  rc = lea
32298 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 76 2c  vesReaderInit(v,
32299 20 30 2c 20 69 53 74 61 72 74 4c 65 61 66 2c 20   0, iStartLeaf, 
3229a 69 45 6e 64 4c 65 61 66 2c 20 4e 55 4c 4c 2c 20  iEndLeaf, NULL, 
3229b 30 2c 20 26 72 65 61 64 65 72 29 3b 0a 20 20 69  0, &reader);.  i
3229c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3229d 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
3229e 20 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e   rc = loadSegmen
3229f 74 4c 65 61 76 65 73 49 6e 74 28 76 2c 20 26 72  tLeavesInt(v, &r
322a0 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  eader, pTerm, nT
322a1 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 6f  erm, isPrefix, o
322a2 75 74 29 3b 0a 20 20 6c 65 61 76 65 73 52 65 61  ut);.  leavesRea
322a3 64 65 72 52 65 73 65 74 28 26 72 65 61 64 65 72  derReset(&reader
322a4 29 3b 0a 20 20 6c 65 61 76 65 73 52 65 61 64 65  );.  leavesReade
322a5 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65 72  rDestroy(&reader
322a6 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
322a7 7d 0a 0a 2f 2a 20 54 61 6b 69 6e 67 20 70 44 61  }../* Taking pDa
322a8 74 61 2f 6e 44 61 74 61 20 61 73 20 61 6e 20 69  ta/nData as an i
322a9 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c 20 66 69  nterior node, fi
322aa 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63 65 20  nd the sequence 
322ab 6f 66 20 63 68 69 6c 64 0a 2a 2a 20 6e 6f 64 65  of child.** node
322ac 73 20 77 68 69 63 68 20 63 6f 75 6c 64 20 69 6e  s which could in
322ad 63 6c 75 64 65 20 70 54 65 72 6d 2f 6e 54 65 72  clude pTerm/nTer
322ae 6d 2f 69 73 50 72 65 66 69 78 2e 20 20 4e 6f 74  m/isPrefix.  Not
322af 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 69 6e  e that the.** in
322b0 74 65 72 69 6f 72 20 6e 6f 64 65 20 74 65 72 6d  terior node term
322b1 73 20 6c 6f 67 69 63 61 6c 6c 79 20 63 6f 6d 65  s logically come
322b2 20 62 65 74 77 65 65 6e 20 74 68 65 20 62 6c 6f   between the blo
322b3 63 6b 73 2c 20 73 6f 20 74 68 65 72 65 20 69 73  cks, so there is
322b4 0a 2a 2a 20 6f 6e 65 20 6d 6f 72 65 20 62 6c 6f  .** one more blo
322b5 63 6b 69 64 20 74 68 61 6e 20 74 68 65 72 65 20  ckid than there 
322b6 61 72 65 20 74 65 72 6d 73 20 28 74 68 61 74 20  are terms (that 
322b7 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74  block contains t
322b8 65 72 6d 73 20 3e 3d 0a 2a 2a 20 74 68 65 20 6c  erms >=.** the l
322b9 61 73 74 20 69 6e 74 65 72 69 6f 72 2d 6e 6f 64  ast interior-nod
322ba 65 20 74 65 72 6d 29 2e 0a 2a 2f 0a 2f 2a 20 54  e term)..*/./* T
322bb 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 63  ODO(shess) The c
322bc 61 6c 6c 69 6e 67 20 63 6f 64 65 20 6d 61 79 20  alling code may 
322bd 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 61  already know tha
322be 74 20 74 68 65 20 65 6e 64 20 63 68 69 6c 64 20  t the end child 
322bf 69 73 0a 2a 2a 20 6e 6f 74 20 77 6f 72 74 68 20  is.** not worth 
322c0 63 61 6c 63 75 6c 61 74 69 6e 67 2c 20 62 65 63  calculating, bec
322c1 61 75 73 65 20 74 68 65 20 65 6e 64 20 6d 61 79  ause the end may
322c2 20 62 65 20 69 6e 20 61 20 6c 61 74 65 72 20 73   be in a later s
322c3 69 62 6c 69 6e 67 0a 2a 2a 20 6e 6f 64 65 2e 20  ibling.** node. 
322c4 20 43 6f 6e 73 69 64 65 72 20 77 68 65 74 68 65   Consider whethe
322c5 72 20 62 72 65 61 6b 69 6e 67 20 73 79 6d 6d 65  r breaking symme
322c6 74 72 79 20 69 73 20 77 6f 72 74 68 77 68 69 6c  try is worthwhil
322c7 65 2e 20 20 49 20 73 75 73 70 65 63 74 0a 2a 2a  e.  I suspect.**
322c8 20 69 74 20 69 73 20 6e 6f 74 20 77 6f 72 74 68   it is not worth
322c9 77 68 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  while..*/.static
322ca 20 76 6f 69 64 20 67 65 74 43 68 69 6c 64 72 65   void getChildre
322cb 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 63 6f 6e 73  nContaining(cons
322cc 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69  t char *pData, i
322cd 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20  nt nData,.      
322ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
322cf 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
322d0 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
322d1 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
322d2 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20  Prefix,.        
322d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
322d4 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
322d5 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72 74 43  _int64 *piStartC
322d6 68 69 6c 64 2c 0a 20 20 20 20 20 20 20 20 20 20  hild,.          
322d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
322d8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
322d9 6e 74 36 34 20 2a 70 69 45 6e 64 43 68 69 6c 64  nt64 *piEndChild
322da 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 52 65 61  ){.  InteriorRea
322db 64 65 72 20 72 65 61 64 65 72 3b 0a 0a 20 20 61  der reader;..  a
322dc 73 73 65 72 74 28 20 6e 44 61 74 61 3e 31 20 29  ssert( nData>1 )
322dd 3b 0a 20 20 61 73 73 65 72 74 28 20 2a 70 44 61  ;.  assert( *pDa
322de 74 61 21 3d 27 5c 30 27 20 29 3b 0a 20 20 69 6e  ta!='\0' );.  in
322df 74 65 72 69 6f 72 52 65 61 64 65 72 49 6e 69 74  teriorReaderInit
322e0 28 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 26  (pData, nData, &
322e1 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 53  reader);..  /* S
322e2 63 61 6e 20 66 6f 72 20 74 68 65 20 66 69 72 73  can for the firs
322e3 74 20 63 68 69 6c 64 20 77 68 69 63 68 20 63 6f  t child which co
322e4 75 6c 64 20 63 6f 6e 74 61 69 6e 20 70 54 65 72  uld contain pTer
322e5 6d 2f 6e 54 65 72 6d 2e 20 2a 2f 0a 20 20 77 68  m/nTerm. */.  wh
322e6 69 6c 65 28 20 21 69 6e 74 65 72 69 6f 72 52 65  ile( !interiorRe
322e7 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64 65  aderAtEnd(&reade
322e8 72 29 20 29 7b 0a 20 20 20 20 69 66 28 20 69 6e  r) ){.    if( in
322e9 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d  teriorReaderTerm
322ea 43 6d 70 28 26 72 65 61 64 65 72 2c 20 70 54 65  Cmp(&reader, pTe
322eb 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 29 3e 30 20  rm, nTerm, 0)>0 
322ec 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 6e 74  ) break;.    int
322ed 65 72 69 6f 72 52 65 61 64 65 72 53 74 65 70 28  eriorReaderStep(
322ee 26 72 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20  &reader);.  }.  
322ef 2a 70 69 53 74 61 72 74 43 68 69 6c 64 20 3d 20  *piStartChild = 
322f0 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43 75  interiorReaderCu
322f1 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 26 72 65  rrentBlockid(&re
322f2 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 4b 65 65  ader);..  /* Kee
322f3 70 20 73 63 61 6e 6e 69 6e 67 20 74 6f 20 66 69  p scanning to fi
322f4 6e 64 20 61 20 74 65 72 6d 20 67 72 65 61 74 65  nd a term greate
322f5 72 20 74 68 61 6e 20 6f 75 72 20 74 65 72 6d 2c  r than our term,
322f6 20 75 73 69 6e 67 20 70 72 65 66 69 78 0a 20 20   using prefix.  
322f7 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 66  ** comparison if
322f8 20 69 6e 64 69 63 61 74 65 64 2e 20 20 49 66 20   indicated.  If 
322f9 69 73 50 72 65 66 69 78 20 69 73 20 66 61 6c 73  isPrefix is fals
322fa 65 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20  e, this will be 
322fb 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 62 6c  the.  ** same bl
322fc 6f 63 6b 69 64 20 61 73 20 74 68 65 20 73 74 61  ockid as the sta
322fd 72 74 69 6e 67 20 62 6c 6f 63 6b 2e 0a 20 20 2a  rting block..  *
322fe 2f 0a 20 20 77 68 69 6c 65 28 20 21 69 6e 74 65  /.  while( !inte
322ff 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28  riorReaderAtEnd(
32300 26 72 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  &reader) ){.    
32301 69 66 28 20 69 6e 74 65 72 69 6f 72 52 65 61 64  if( interiorRead
32302 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64 65  erTermCmp(&reade
32303 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  r, pTerm, nTerm,
32304 20 69 73 50 72 65 66 69 78 29 3e 30 20 29 20 62   isPrefix)>0 ) b
32305 72 65 61 6b 3b 0a 20 20 20 20 69 6e 74 65 72 69  reak;.    interi
32306 6f 72 52 65 61 64 65 72 53 74 65 70 28 26 72 65  orReaderStep(&re
32307 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20 2a 70 69  ader);.  }.  *pi
32308 45 6e 64 43 68 69 6c 64 20 3d 20 69 6e 74 65 72  EndChild = inter
32309 69 6f 72 52 65 61 64 65 72 43 75 72 72 65 6e 74  iorReaderCurrent
3230a 42 6c 6f 63 6b 69 64 28 26 72 65 61 64 65 72 29  Blockid(&reader)
3230b 3b 0a 0a 20 20 69 6e 74 65 72 69 6f 72 52 65 61  ;..  interiorRea
3230c 64 65 72 44 65 73 74 72 6f 79 28 26 72 65 61 64  derDestroy(&read
3230d 65 72 29 3b 0a 0a 20 20 2f 2a 20 43 68 69 6c 64  er);..  /* Child
3230e 72 65 6e 20 6d 75 73 74 20 61 73 63 65 6e 64 2c  ren must ascend,
3230f 20 61 6e 64 20 69 66 20 21 70 72 65 66 69 78 2c   and if !prefix,
32310 20 62 6f 74 68 20 6d 75 73 74 20 62 65 20 74 68   both must be th
32311 65 20 73 61 6d 65 2e 20 2a 2f 0a 20 20 61 73 73  e same. */.  ass
32312 65 72 74 28 20 2a 70 69 45 6e 64 43 68 69 6c 64  ert( *piEndChild
32313 3e 3d 2a 70 69 53 74 61 72 74 43 68 69 6c 64 20  >=*piStartChild 
32314 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 50  );.  assert( isP
32315 72 65 66 69 78 20 7c 7c 20 2a 70 69 53 74 61 72  refix || *piStar
32316 74 43 68 69 6c 64 3d 3d 2a 70 69 45 6e 64 43 68  tChild==*piEndCh
32317 69 6c 64 20 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 61  ild );.}../* Rea
32318 64 20 62 6c 6f 63 6b 20 61 74 20 69 42 6c 6f 63  d block at iBloc
32319 6b 69 64 20 61 6e 64 20 70 61 73 73 20 69 74 20  kid and pass it 
3231a 77 69 74 68 20 6f 74 68 65 72 20 70 61 72 61 6d  with other param
3231b 73 20 74 6f 0a 2a 2a 20 67 65 74 43 68 69 6c 64  s to.** getChild
3231c 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 29 2e  renContaining().
3231d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
3231e 6f 61 64 41 6e 64 47 65 74 43 68 69 6c 64 72 65  oadAndGetChildre
3231f 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 0a 20 20 66  nContaining(.  f
32320 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
32321 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
32322 69 42 6c 6f 63 6b 69 64 2c 0a 20 20 63 6f 6e 73  iBlockid,.  cons
32323 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
32324 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
32325 50 72 65 66 69 78 2c 0a 20 20 73 71 6c 69 74 65  Prefix,.  sqlite
32326 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72 74 43  _int64 *piStartC
32327 68 69 6c 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74  hild, sqlite_int
32328 36 34 20 2a 70 69 45 6e 64 43 68 69 6c 64 0a 29  64 *piEndChild.)
32329 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
3232a 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 6e   *s = NULL;.  in
3232b 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
3232c 20 69 42 6c 6f 63 6b 69 64 21 3d 30 20 29 3b 0a   iBlockid!=0 );.
3232d 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 21    assert( pTerm!
3232e 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72  =NULL );.  asser
3232f 74 28 20 6e 54 65 72 6d 21 3d 30 20 29 3b 20 20  t( nTerm!=0 );  
32330 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68        /* TODO(sh
32331 65 73 73 29 20 57 68 79 20 6e 6f 74 20 61 6c 6c  ess) Why not all
32332 6f 77 20 74 68 69 73 3f 20 2a 2f 0a 20 20 61 73  ow this? */.  as
32333 73 65 72 74 28 20 70 69 53 74 61 72 74 43 68 69  sert( piStartChi
32334 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73  ld!=NULL );.  as
32335 73 65 72 74 28 20 70 69 45 6e 64 43 68 69 6c 64  sert( piEndChild
32336 21 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 72 63 20  !=NULL );..  rc 
32337 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
32338 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 53 45 4c  ent(v, BLOCK_SEL
32339 45 43 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ECT_STMT, &s);. 
3233a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3233b 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
3233c 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3233d 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c  bind_int64(s, 1,
3233e 20 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66   iBlockid);.  if
3233f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
32340 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
32341 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
32342 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  p(s);.  if( rc==
32343 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65  SQLITE_DONE ) re
32344 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
32345 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  R;.  if( rc!=SQL
32346 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e  ITE_ROW ) return
32347 20 72 63 3b 0a 0a 20 20 67 65 74 43 68 69 6c 64   rc;..  getChild
32348 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 73 71  renContaining(sq
32349 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
3234a 62 28 73 2c 20 30 29 2c 20 73 71 6c 69 74 65 33  b(s, 0), sqlite3
3234b 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c  _column_bytes(s,
3234c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0),.           
3234d 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 65               pTe
3234e 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
3234f 66 69 78 2c 20 70 69 53 74 61 72 74 43 68 69 6c  fix, piStartChil
32350 64 2c 20 70 69 45 6e 64 43 68 69 6c 64 29 3b 0a  d, piEndChild);.
32351 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74 20  .  /* We expect 
32352 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57  only one row.  W
32353 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20 61  e must execute a
32354 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f 73  nother sqlite3_s
32355 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20 63 6f  tep().   * to co
32356 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72 61  mplete the itera
32357 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65 20  tion; otherwise 
32358 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20 72  the table will r
32359 65 6d 61 69 6e 0a 20 20 20 2a 20 6c 6f 63 6b 65  emain.   * locke
3235a 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  d. */.  rc = sql
3235b 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
3235c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
3235d 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  OW ) return SQLI
3235e 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20  TE_ERROR;.  if( 
3235f 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
32360 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
32361 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
32362 3b 0a 7d 0a 0a 2f 2a 20 54 72 61 76 65 72 73 65  ;.}../* Traverse
32363 20 74 68 65 20 74 72 65 65 20 72 65 70 72 65 73   the tree repres
32364 65 6e 74 65 64 20 62 79 20 70 44 61 74 61 5b 6e  ented by pData[n
32365 44 61 74 61 5d 20 6c 6f 6f 6b 69 6e 67 20 66 6f  Data] looking fo
32366 72 0a 2a 2a 20 70 54 65 72 6d 5b 6e 54 65 72 6d  r.** pTerm[nTerm
32367 5d 2c 20 70 6c 61 63 69 6e 67 20 69 74 73 20 64  ], placing its d
32368 6f 63 6c 69 73 74 20 69 6e 74 6f 20 2a 6f 75 74  oclist into *out
32369 2e 20 20 54 68 69 73 20 69 73 20 69 6e 74 65 72  .  This is inter
3236a 6e 61 6c 20 74 6f 0a 2a 2a 20 6c 6f 61 64 53 65  nal to.** loadSe
3236b 67 6d 65 6e 74 28 29 20 74 6f 20 6d 61 6b 65 20  gment() to make 
3236c 65 72 72 6f 72 2d 68 61 6e 64 6c 69 6e 67 20 63  error-handling c
3236d 6c 65 61 6e 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  leaner..*/.stati
3236e 63 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e  c int loadSegmen
3236f 74 49 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  tInt(fulltext_vt
32370 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61  ab *v, const cha
32371 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
32372 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
32373 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
32374 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 65 61  qlite_int64 iLea
32375 76 65 73 45 6e 64 2c 0a 20 20 20 20 20 20 20 20  vesEnd,.        
32376 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32377 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
32378 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
32379 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
3237a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3237b 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66          DataBuff
3237c 65 72 20 2a 6f 75 74 29 7b 0a 20 20 2f 2a 20 53  er *out){.  /* S
3237d 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 72  pecial case wher
3237e 65 20 72 6f 6f 74 20 69 73 20 61 20 6c 65 61 66  e root is a leaf
3237f 2e 20 2a 2f 0a 20 20 69 66 28 20 2a 70 44 61 74  . */.  if( *pDat
32380 61 3d 3d 27 5c 30 27 20 29 7b 0a 20 20 20 20 72  a=='\0' ){.    r
32381 65 74 75 72 6e 20 6c 6f 61 64 53 65 67 6d 65 6e  eturn loadSegmen
32382 74 4c 65 61 66 28 76 2c 20 70 44 61 74 61 2c 20  tLeaf(v, pData, 
32383 6e 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e 54  nData, pTerm, nT
32384 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 6f  erm, isPrefix, o
32385 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ut);.  }else{.  
32386 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73 71    int rc;.    sq
32387 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72  lite_int64 iStar
32388 74 43 68 69 6c 64 2c 20 69 45 6e 64 43 68 69 6c  tChild, iEndChil
32389 64 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65  d;..    /* Proce
3238a 73 73 20 70 44 61 74 61 20 61 73 20 61 6e 20 69  ss pData as an i
3238b 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c 20 74 68  nterior node, th
3238c 65 6e 20 6c 6f 6f 70 20 64 6f 77 6e 20 74 68 65  en loop down the
3238d 20 74 72 65 65 0a 20 20 20 20 2a 2a 20 75 6e 74   tree.    ** unt
3238e 69 6c 20 77 65 20 66 69 6e 64 20 74 68 65 20 73  il we find the s
3238f 65 74 20 6f 66 20 6c 65 61 66 20 6e 6f 64 65 73  et of leaf nodes
32390 20 74 6f 20 73 63 61 6e 20 66 6f 72 20 74 68 65   to scan for the
32391 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20   term..    */.  
32392 20 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e    getChildrenCon
32393 74 61 69 6e 69 6e 67 28 70 44 61 74 61 2c 20 6e  taining(pData, n
32394 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e 54 65  Data, pTerm, nTe
32395 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 0a 20 20  rm, isPrefix,.  
32396 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32397 20 20 20 20 20 20 20 20 26 69 53 74 61 72 74 43          &iStartC
32398 68 69 6c 64 2c 20 26 69 45 6e 64 43 68 69 6c 64  hild, &iEndChild
32399 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53  );.    while( iS
3239a 74 61 72 74 43 68 69 6c 64 3e 69 4c 65 61 76 65  tartChild>iLeave
3239b 73 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 73 71  sEnd ){.      sq
3239c 6c 69 74 65 5f 69 6e 74 36 34 20 69 4e 65 78 74  lite_int64 iNext
3239d 53 74 61 72 74 2c 20 69 4e 65 78 74 45 6e 64 3b  Start, iNextEnd;
3239e 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 61 64  .      rc = load
3239f 41 6e 64 47 65 74 43 68 69 6c 64 72 65 6e 43 6f  AndGetChildrenCo
323a0 6e 74 61 69 6e 69 6e 67 28 76 2c 20 69 53 74 61  ntaining(v, iSta
323a1 72 74 43 68 69 6c 64 2c 20 70 54 65 72 6d 2c 20  rtChild, pTerm, 
323a2 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c  nTerm, isPrefix,
323a3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
323a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
323a5 20 20 20 20 20 20 20 20 20 26 69 4e 65 78 74 53           &iNextS
323a6 74 61 72 74 2c 20 26 69 4e 65 78 74 45 6e 64 29  tart, &iNextEnd)
323a7 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
323a8 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
323a9 72 6e 20 72 63 3b 0a 0a 20 20 20 20 20 20 2f 2a  rn rc;..      /*
323aa 20 49 66 20 77 65 27 76 65 20 62 72 61 6e 63 68   If we've branch
323ab 65 64 2c 20 66 6f 6c 6c 6f 77 20 74 68 65 20 65  ed, follow the e
323ac 6e 64 20 62 72 61 6e 63 68 2c 20 74 6f 6f 2e 20  nd branch, too. 
323ad 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 53 74  */.      if( iSt
323ae 61 72 74 43 68 69 6c 64 21 3d 69 45 6e 64 43 68  artChild!=iEndCh
323af 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ild ){.        s
323b0 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75 6d  qlite_int64 iDum
323b1 6d 79 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  my;.        rc =
323b2 20 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c 64   loadAndGetChild
323b3 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 76 2c  renContaining(v,
323b4 20 69 45 6e 64 43 68 69 6c 64 2c 20 70 54 65 72   iEndChild, pTer
323b5 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66  m, nTerm, isPref
323b6 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
323b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
323b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69                &i
323b9 44 75 6d 6d 79 2c 20 26 69 4e 65 78 74 45 6e 64  Dummy, &iNextEnd
323ba 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
323bb 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
323bc 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
323bd 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  }..      assert(
323be 20 69 4e 65 78 74 53 74 61 72 74 3c 3d 69 4e 65   iNextStart<=iNe
323bf 78 74 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 69  xtEnd );.      i
323c0 53 74 61 72 74 43 68 69 6c 64 20 3d 20 69 4e 65  StartChild = iNe
323c1 78 74 53 74 61 72 74 3b 0a 20 20 20 20 20 20 69  xtStart;.      i
323c2 45 6e 64 43 68 69 6c 64 20 3d 20 69 4e 65 78 74  EndChild = iNext
323c3 45 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  End;.    }.    a
323c4 73 73 65 72 74 28 20 69 53 74 61 72 74 43 68 69  ssert( iStartChi
323c5 6c 64 3c 3d 69 4c 65 61 76 65 73 45 6e 64 20 29  ld<=iLeavesEnd )
323c6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 45  ;.    assert( iE
323c7 6e 64 43 68 69 6c 64 3c 3d 69 4c 65 61 76 65 73  ndChild<=iLeaves
323c8 45 6e 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53  End );..    /* S
323c9 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  can through the 
323ca 6c 65 61 66 20 73 65 67 6d 65 6e 74 73 20 66 6f  leaf segments fo
323cb 72 20 64 6f 63 6c 69 73 74 73 2e 20 2a 2f 0a 20  r doclists. */. 
323cc 20 20 20 72 65 74 75 72 6e 20 6c 6f 61 64 53 65     return loadSe
323cd 67 6d 65 6e 74 4c 65 61 76 65 73 28 76 2c 20 69  gmentLeaves(v, i
323ce 53 74 61 72 74 43 68 69 6c 64 2c 20 69 45 6e 64  StartChild, iEnd
323cf 43 68 69 6c 64 2c 0a 20 20 20 20 20 20 20 20 20  Child,.         
323d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
323d1 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d      pTerm, nTerm
323d2 2c 20 69 73 50 72 65 66 69 78 2c 20 6f 75 74 29  , isPrefix, out)
323d3 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 43 61 6c 6c  ;.  }.}../* Call
323d4 20 6c 6f 61 64 53 65 67 6d 65 6e 74 49 6e 74 28   loadSegmentInt(
323d5 29 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 68 65  ) to collect the
323d6 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 70 54 65   doclist for pTe
323d7 72 6d 2f 6e 54 65 72 6d 2c 20 74 68 65 6e 0a 2a  rm/nTerm, then.*
323d8 2a 20 6d 65 72 67 65 20 69 74 73 20 64 6f 63 6c  * merge its docl
323d9 69 73 74 20 6f 76 65 72 20 2a 6f 75 74 20 28 61  ist over *out (a
323da 6e 79 20 64 75 70 6c 69 63 61 74 65 20 64 6f 63  ny duplicate doc
323db 6c 69 73 74 73 20 72 65 61 64 20 66 72 6f 6d 20  lists read from 
323dc 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 72  the.** segment r
323dd 6f 6f 74 65 64 20 61 74 20 70 44 61 74 61 20 77  ooted at pData w
323de 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68  ill overwrite th
323df 6f 73 65 20 69 6e 20 2a 6f 75 74 29 2e 0a 2a 2f  ose in *out)..*/
323e0 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
323e1 43 6f 6e 73 69 64 65 72 20 63 68 61 6e 67 69 6e  Consider changin
323e2 67 20 74 68 69 73 20 74 6f 20 64 65 74 65 72 6d  g this to determ
323e3 69 6e 65 20 74 68 65 20 64 65 70 74 68 20 6f 66  ine the depth of
323e4 20 74 68 65 0a 2a 2a 20 6c 65 61 76 65 73 20 75   the.** leaves u
323e5 73 69 6e 67 20 65 69 74 68 65 72 20 74 68 65 20  sing either the 
323e6 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72 73  first characters
323e7 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   of interior nod
323e8 65 73 20 28 77 68 65 6e 0a 2a 2a 20 3d 3d 31 2c  es (when.** ==1,
323e9 20 77 65 27 72 65 20 6f 6e 65 20 6c 65 76 65 6c   we're one level
323ea 20 61 62 6f 76 65 20 74 68 65 20 6c 65 61 76 65   above the leave
323eb 73 29 2c 20 6f 72 20 74 68 65 20 66 69 72 73 74  s), or the first
323ec 20 63 68 61 72 61 63 74 65 72 20 6f 66 0a 2a 2a   character of.**
323ed 20 74 68 65 20 72 6f 6f 74 20 28 77 68 69 63 68   the root (which
323ee 20 77 69 6c 6c 20 64 65 73 63 72 69 62 65 20 74   will describe t
323ef 68 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65  he height of the
323f0 20 74 72 65 65 20 64 69 72 65 63 74 6c 79 29 2e   tree directly).
323f1 0a 2a 2a 20 45 69 74 68 65 72 20 66 65 65 6c 73  .** Either feels
323f2 20 73 6f 6d 65 77 68 61 74 20 74 72 69 63 6b 79   somewhat tricky
323f3 20 74 6f 20 6d 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f   to me..*/./* TO
323f4 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 63 75  DO(shess) The cu
323f5 72 72 65 6e 74 20 6d 65 72 67 65 20 69 73 20 6c  rrent merge is l
323f6 69 6b 65 6c 79 20 74 6f 20 62 65 20 73 6c 6f 77  ikely to be slow
323f7 20 66 6f 72 20 6c 61 72 67 65 0a 2a 2a 20 64 6f   for large.** do
323f8 63 6c 69 73 74 73 20 28 74 68 6f 75 67 68 20 69  clists (though i
323f9 74 20 73 68 6f 75 6c 64 20 70 72 6f 63 65 73 73  t should process
323fa 20 66 72 6f 6d 20 6e 65 77 65 73 74 2f 73 6d 61   from newest/sma
323fb 6c 6c 65 73 74 20 74 6f 0a 2a 2a 20 6f 6c 64 65  llest to.** olde
323fc 73 74 2f 6c 61 72 67 65 73 74 2c 20 73 6f 20 69  st/largest, so i
323fd 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 61  t may not be tha
323fe 74 20 62 61 64 29 2e 20 20 49 74 20 6d 69 67 68  t bad).  It migh
323ff 74 20 62 65 20 75 73 65 66 75 6c 20 74 6f 0a 2a  t be useful to.*
32400 2a 20 6d 6f 64 69 66 79 20 74 68 69 6e 67 73 20  * modify things 
32401 74 6f 20 61 6c 6c 6f 77 20 66 6f 72 20 4e 2d 77  to allow for N-w
32402 61 79 20 6d 65 72 67 69 6e 67 2e 20 20 54 68 69  ay merging.  Thi
32403 73 20 63 6f 75 6c 64 20 65 69 74 68 65 72 20 62  s could either b
32404 65 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 73 65  e.** within a se
32405 67 6d 65 6e 74 2c 20 77 69 74 68 20 70 61 69 72  gment, with pair
32406 77 69 73 65 20 6d 65 72 67 65 73 20 61 63 72 6f  wise merges acro
32407 73 73 20 73 65 67 6d 65 6e 74 73 2c 20 6f 72 20  ss segments, or 
32408 61 63 72 6f 73 73 0a 2a 2a 20 61 6c 6c 20 73 65  across.** all se
32409 67 6d 65 6e 74 73 20 61 74 20 6f 6e 63 65 2e 0a  gments at once..
3240a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f  */.static int lo
3240b 61 64 53 65 67 6d 65 6e 74 28 66 75 6c 6c 74 65  adSegment(fullte
3240c 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73  xt_vtab *v, cons
3240d 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69  t char *pData, i
3240e 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20  nt nData,.      
3240f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32410 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c   sqlite_int64 iL
32411 65 61 76 65 73 45 6e 64 2c 0a 20 20 20 20 20 20  eavesEnd,.      
32412 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32413 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
32414 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69  rm, int nTerm, i
32415 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20  nt isPrefix,.   
32416 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32417 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20 2a      DataBuffer *
32418 6f 75 74 29 7b 0a 20 20 44 61 74 61 42 75 66 66  out){.  DataBuff
32419 65 72 20 72 65 73 75 6c 74 3b 0a 20 20 69 6e 74  er result;.  int
3241a 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
3241b 6e 44 61 74 61 3e 31 20 29 3b 0a 0a 20 20 2f 2a  nData>1 );..  /*
3241c 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f 75 6c   This code shoul
3241d 64 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c 65  d never be calle
3241e 64 20 77 69 74 68 20 62 75 66 66 65 72 65 64 20  d with buffered 
3241f 75 70 64 61 74 65 73 2e 20 2a 2f 0a 20 20 61 73  updates. */.  as
32420 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69 6e  sert( v->nPendin
32421 67 44 61 74 61 3c 30 20 29 3b 0a 0a 20 20 64 61  gData<0 );..  da
32422 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 72 65  taBufferInit(&re
32423 73 75 6c 74 2c 20 30 29 3b 0a 20 20 72 63 20 3d  sult, 0);.  rc =
32424 20 6c 6f 61 64 53 65 67 6d 65 6e 74 49 6e 74 28   loadSegmentInt(
32425 76 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  v, pData, nData,
32426 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20 20   iLeavesEnd,.   
32427 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32428 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c     pTerm, nTerm,
32429 20 69 73 50 72 65 66 69 78 2c 20 26 72 65 73 75   isPrefix, &resu
3242a 6c 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  lt);.  if( rc==S
3242b 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 75  QLITE_OK && resu
3242c 6c 74 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20  lt.nData>0 ){.  
3242d 20 20 69 66 28 20 6f 75 74 2d 3e 6e 44 61 74 61    if( out->nData
3242e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 44 61 74  ==0 ){.      Dat
3242f 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20 2a 6f  aBuffer tmp = *o
32430 75 74 3b 0a 20 20 20 20 20 20 2a 6f 75 74 20 3d  ut;.      *out =
32431 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 20 20 72   result;.      r
32432 65 73 75 6c 74 20 3d 20 74 6d 70 3b 0a 20 20 20  esult = tmp;.   
32433 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 44 61   }else{.      Da
32434 74 61 42 75 66 66 65 72 20 6d 65 72 67 65 64 3b  taBuffer merged;
32435 0a 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20  .      DLReader 
32436 72 65 61 64 65 72 73 5b 32 5d 3b 0a 0a 20 20 20  readers[2];..   
32437 20 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61 64     dlrInit(&read
32438 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46 41 55  ers[0], DL_DEFAU
32439 4c 54 2c 20 6f 75 74 2d 3e 70 44 61 74 61 2c 20  LT, out->pData, 
3243a 6f 75 74 2d 3e 6e 44 61 74 61 29 3b 0a 20 20 20  out->nData);.   
3243b 20 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61 64     dlrInit(&read
3243c 65 72 73 5b 31 5d 2c 20 44 4c 5f 44 45 46 41 55  ers[1], DL_DEFAU
3243d 4c 54 2c 20 72 65 73 75 6c 74 2e 70 44 61 74 61  LT, result.pData
3243e 2c 20 72 65 73 75 6c 74 2e 6e 44 61 74 61 29 3b  , result.nData);
3243f 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65  .      dataBuffe
32440 72 49 6e 69 74 28 26 6d 65 72 67 65 64 2c 20 6f  rInit(&merged, o
32441 75 74 2d 3e 6e 44 61 74 61 2b 72 65 73 75 6c 74  ut->nData+result
32442 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 64  .nData);.      d
32443 6f 63 4c 69 73 74 4d 65 72 67 65 28 26 6d 65 72  ocListMerge(&mer
32444 67 65 64 2c 20 72 65 61 64 65 72 73 2c 20 32 29  ged, readers, 2)
32445 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  ;.      dataBuff
32446 65 72 44 65 73 74 72 6f 79 28 6f 75 74 29 3b 0a  erDestroy(out);.
32447 20 20 20 20 20 20 2a 6f 75 74 20 3d 20 6d 65 72        *out = mer
32448 67 65 64 3b 0a 20 20 20 20 20 20 64 6c 72 44 65  ged;.      dlrDe
32449 73 74 72 6f 79 28 26 72 65 61 64 65 72 73 5b 30  stroy(&readers[0
3244a 5d 29 3b 0a 20 20 20 20 20 20 64 6c 72 44 65 73  ]);.      dlrDes
3244b 74 72 6f 79 28 26 72 65 61 64 65 72 73 5b 31 5d  troy(&readers[1]
3244c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64  );.    }.  }.  d
3244d 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
3244e 28 26 72 65 73 75 6c 74 29 3b 0a 20 20 72 65 74  (&result);.  ret
3244f 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 53 63  urn rc;.}../* Sc
32450 61 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  an the database 
32451 61 6e 64 20 6d 65 72 67 65 20 74 6f 67 65 74 68  and merge togeth
32452 65 72 20 74 68 65 20 70 6f 73 74 69 6e 67 20 6c  er the posting l
32453 69 73 74 73 20 66 6f 72 20 74 68 65 20 74 65 72  ists for the ter
32454 6d 0a 2a 2a 20 69 6e 74 6f 20 2a 6f 75 74 2e 0a  m.** into *out..
32455 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65  */.static int te
32456 72 6d 53 65 6c 65 63 74 28 0a 20 20 66 75 6c 6c  rmSelect(.  full
32457 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 0a 20  text_vtab *v, . 
32458 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20 20   int iColumn,.  
32459 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
3245a 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20 20  m, int nTerm,   
3245b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 72            /* Ter
3245c 6d 20 74 6f 20 71 75 65 72 79 20 66 6f 72 20 2a  m to query for *
3245d 2f 0a 20 20 69 6e 74 20 69 73 50 72 65 66 69 78  /.  int isPrefix
3245e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3245f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32460 20 54 72 75 65 20 66 6f 72 20 61 20 70 72 65 66   True for a pref
32461 69 78 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 44  ix search */.  D
32462 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65  ocListType iType
32463 2c 20 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  , .  DataBuffer 
32464 2a 6f 75 74 20 20 20 20 20 20 20 20 20 20 20 20  *out            
32465 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32466 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74 73 20  * Write results 
32467 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 61 74  here */.){.  Dat
32468 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 3b  aBuffer doclist;
32469 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
3246a 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  *s;.  int rc = s
3246b 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
3246c 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
3246d 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b  T_ALL_STMT, &s);
3246e 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3246f 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
32470 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64  ;..  /* This cod
32471 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  e should never b
32472 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75  e called with bu
32473 66 66 65 72 65 64 20 75 70 64 61 74 65 73 2e 20  ffered updates. 
32474 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e  */.  assert( v->
32475 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29  nPendingData<0 )
32476 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  ;..  dataBufferI
32477 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 30 29  nit(&doclist, 0)
32478 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ;.  dataBufferIn
32479 69 74 28 6f 75 74 2c 20 30 29 3b 0a 0a 20 20 2f  it(out, 0);..  /
3247a 2a 20 54 72 61 76 65 72 73 65 20 74 68 65 20 73  * Traverse the s
3247b 65 67 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6c 64  egments from old
3247c 65 73 74 20 74 6f 20 6e 65 77 65 73 74 20 73 6f  est to newest so
3247d 20 74 68 61 74 20 6e 65 77 65 72 20 64 6f 63 6c   that newer docl
3247e 69 73 74 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74  ist.  ** element
3247f 73 20 66 6f 72 20 67 69 76 65 6e 20 64 6f 63 69  s for given doci
32480 64 73 20 6f 76 65 72 77 72 69 74 65 20 6f 6c 64  ds overwrite old
32481 65 72 20 65 6c 65 6d 65 6e 74 73 2e 0a 20 20 2a  er elements..  *
32482 2f 0a 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d  /.  while( (rc =
32483 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
32484 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
32485 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
32486 2a 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33  *pData = sqlite3
32487 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20  _column_blob(s, 
32488 32 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e  2);.    const in
32489 74 20 6e 44 61 74 61 20 3d 20 73 71 6c 69 74 65  t nData = sqlite
3248a 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73  3_column_bytes(s
3248b 2c 20 32 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20  , 2);.    const 
3248c 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 65  sqlite_int64 iLe
3248d 61 76 65 73 45 6e 64 20 3d 20 73 71 6c 69 74 65  avesEnd = sqlite
3248e 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73  3_column_int64(s
3248f 2c 20 31 29 3b 0a 20 20 20 20 72 63 20 3d 20 6c  , 1);.    rc = l
32490 6f 61 64 53 65 67 6d 65 6e 74 28 76 2c 20 70 44  oadSegment(v, pD
32491 61 74 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65 61  ata, nData, iLea
32492 76 65 73 45 6e 64 2c 20 70 54 65 72 6d 2c 20 6e  vesEnd, pTerm, n
32493 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 0a  Term, isPrefix,.
32494 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32495 20 20 20 20 20 26 64 6f 63 6c 69 73 74 29 3b 0a       &doclist);.
32496 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
32497 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
32498 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
32499 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
3249a 20 20 20 69 66 28 20 64 6f 63 6c 69 73 74 2e 6e     if( doclist.n
3249b 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20  Data!=0 ){.     
3249c 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
3249d 54 68 65 20 6f 6c 64 20 74 65 72 6d 5f 73 65 6c  The old term_sel
3249e 65 63 74 5f 61 6c 6c 28 29 20 63 6f 64 65 20 61  ect_all() code a
3249f 70 70 6c 69 65 64 20 74 68 65 20 63 6f 6c 75 6d  pplied the colum
324a0 6e 0a 20 20 20 20 20 20 2a 2a 20 72 65 73 74 72  n.      ** restr
324a1 69 63 74 20 61 73 20 77 65 20 6d 65 72 67 65 64  ict as we merged
324a2 20 73 65 67 6d 65 6e 74 73 2c 20 6c 65 61 64 69   segments, leadi
324a3 6e 67 20 74 6f 20 73 6d 61 6c 6c 65 72 20 62 75  ng to smaller bu
324a4 66 66 65 72 73 2e 0a 20 20 20 20 20 20 2a 2a 20  ffers..      ** 
324a5 54 68 69 73 20 69 73 20 70 72 6f 62 61 62 6c 79  This is probably
324a6 20 77 6f 72 74 68 77 68 69 6c 65 20 74 6f 20 62   worthwhile to b
324a7 72 69 6e 67 20 62 61 63 6b 2c 20 6f 6e 63 65 20  ring back, once 
324a8 74 68 65 20 6e 65 77 20 73 74 6f 72 61 67 65 0a  the new storage.
324a9 20 20 20 20 20 20 2a 2a 20 73 79 73 74 65 6d 20        ** system 
324aa 69 73 20 63 68 65 63 6b 65 64 20 69 6e 2e 0a 20  is checked in.. 
324ab 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
324ac 28 20 69 43 6f 6c 75 6d 6e 3d 3d 76 2d 3e 6e 43  ( iColumn==v->nC
324ad 6f 6c 75 6d 6e 29 20 69 43 6f 6c 75 6d 6e 20 3d  olumn) iColumn =
324ae 20 2d 31 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69   -1;.      docLi
324af 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41 55 4c  stTrim(DL_DEFAUL
324b0 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61  T, doclist.pData
324b1 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 2c  , doclist.nData,
324b2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
324b3 20 20 20 69 43 6f 6c 75 6d 6e 2c 20 69 54 79 70     iColumn, iTyp
324b4 65 2c 20 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20  e, out);.    }. 
324b5 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
324b6 4b 3b 0a 20 20 7d 0a 0a 20 65 72 72 3a 0a 20 20  K;.  }.. err:.  
324b7 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
324b8 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 72  y(&doclist);.  r
324b9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a  eturn rc;.}../**
324ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
324bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
324bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
324bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
324be 2f 2a 20 55 73 65 64 20 74 6f 20 68 6f 6c 64 20  /* Used to hold 
324bf 68 61 73 68 74 61 62 6c 65 20 64 61 74 61 20 66  hashtable data f
324c0 6f 72 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a 74  or sorting. */.t
324c1 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 65  ypedef struct Te
324c2 72 6d 44 61 74 61 20 7b 0a 20 20 63 6f 6e 73 74  rmData {.  const
324c3 20 63 68 61 72 20 2a 70 54 65 72 6d 3b 0a 20 20   char *pTerm;.  
324c4 69 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 44 4c 43  int nTerm;.  DLC
324c5 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65  ollector *pColle
324c6 63 74 6f 72 3b 0a 7d 20 54 65 72 6d 44 61 74 61  ctor;.} TermData
324c7 3b 0a 0a 2f 2a 20 4f 72 64 65 72 73 20 54 65 72  ;../* Orders Ter
324c8 6d 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69  mData elements i
324c9 6e 20 73 74 72 63 6d 70 20 66 61 73 68 69 6f 6e  n strcmp fashion
324ca 20 28 20 3c 30 20 66 6f 72 20 6c 65 73 73 2d 74   ( <0 for less-t
324cb 68 61 6e 2c 20 30 0a 2a 2a 20 66 6f 72 20 65 71  han, 0.** for eq
324cc 75 61 6c 2c 20 3e 30 20 66 6f 72 20 67 72 65 61  ual, >0 for grea
324cd 74 65 72 2d 74 68 61 6e 29 2e 0a 2a 2f 0a 73 74  ter-than)..*/.st
324ce 61 74 69 63 20 69 6e 74 20 74 65 72 6d 44 61 74  atic int termDat
324cf 61 43 6d 70 28 63 6f 6e 73 74 20 76 6f 69 64 20  aCmp(const void 
324d0 2a 61 76 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  *av, const void 
324d1 2a 62 76 29 7b 0a 20 20 63 6f 6e 73 74 20 54 65  *bv){.  const Te
324d2 72 6d 44 61 74 61 20 2a 61 20 3d 20 28 63 6f 6e  rmData *a = (con
324d3 73 74 20 54 65 72 6d 44 61 74 61 20 2a 29 61 76  st TermData *)av
324d4 3b 0a 20 20 63 6f 6e 73 74 20 54 65 72 6d 44 61  ;.  const TermDa
324d5 74 61 20 2a 62 20 3d 20 28 63 6f 6e 73 74 20 54  ta *b = (const T
324d6 65 72 6d 44 61 74 61 20 2a 29 62 76 3b 0a 20 20  ermData *)bv;.  
324d7 69 6e 74 20 6e 20 3d 20 61 2d 3e 6e 54 65 72 6d  int n = a->nTerm
324d8 3c 62 2d 3e 6e 54 65 72 6d 20 3f 20 61 2d 3e 6e  <b->nTerm ? a->n
324d9 54 65 72 6d 20 3a 20 62 2d 3e 6e 54 65 72 6d 3b  Term : b->nTerm;
324da 0a 20 20 69 6e 74 20 63 20 3d 20 6d 65 6d 63 6d  .  int c = memcm
324db 70 28 61 2d 3e 70 54 65 72 6d 2c 20 62 2d 3e 70  p(a->pTerm, b->p
324dc 54 65 72 6d 2c 20 6e 29 3b 0a 20 20 69 66 28 20  Term, n);.  if( 
324dd 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20 63 3b  c!=0 ) return c;
324de 0a 20 20 72 65 74 75 72 6e 20 61 2d 3e 6e 54 65  .  return a->nTe
324df 72 6d 2d 62 2d 3e 6e 54 65 72 6d 3b 0a 7d 0a 0a  rm-b->nTerm;.}..
324e0 2f 2a 20 4f 72 64 65 72 20 70 54 65 72 6d 73 20  /* Order pTerms 
324e1 64 61 74 61 20 62 79 20 74 65 72 6d 2c 20 74 68  data by term, th
324e2 65 6e 20 77 72 69 74 65 20 61 20 6e 65 77 20 6c  en write a new l
324e3 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74 20 75  evel 0 segment u
324e4 73 69 6e 67 0a 2a 2a 20 4c 65 61 66 57 72 69 74  sing.** LeafWrit
324e5 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
324e6 74 20 77 72 69 74 65 5a 65 72 6f 53 65 67 6d 65  t writeZeroSegme
324e7 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  nt(fulltext_vtab
324e8 20 2a 76 2c 20 66 74 73 33 48 61 73 68 20 2a 70   *v, fts3Hash *p
324e9 54 65 72 6d 73 29 7b 0a 20 20 66 74 73 33 48 61  Terms){.  fts3Ha
324ea 73 68 45 6c 65 6d 20 2a 65 3b 0a 20 20 69 6e 74  shElem *e;.  int
324eb 20 69 64 78 2c 20 72 63 2c 20 69 2c 20 6e 3b 0a   idx, rc, i, n;.
324ec 20 20 54 65 72 6d 44 61 74 61 20 2a 70 44 61 74    TermData *pDat
324ed 61 3b 0a 20 20 4c 65 61 66 57 72 69 74 65 72 20  a;.  LeafWriter 
324ee 77 72 69 74 65 72 3b 0a 20 20 44 61 74 61 42 75  writer;.  DataBu
324ef 66 66 65 72 20 64 6c 3b 0a 0a 20 20 2f 2a 20 44  ffer dl;..  /* D
324f0 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65 78  etermine the nex
324f1 74 20 69 6e 64 65 78 20 61 74 20 6c 65 76 65 6c  t index at level
324f2 20 30 2c 20 6d 65 72 67 69 6e 67 20 61 73 20 6e   0, merging as n
324f3 65 63 65 73 73 61 72 79 2e 20 2a 2f 0a 20 20 72  ecessary. */.  r
324f4 63 20 3d 20 73 65 67 64 69 72 4e 65 78 74 49 6e  c = segdirNextIn
324f5 64 65 78 28 76 2c 20 30 2c 20 26 69 64 78 29 3b  dex(v, 0, &idx);
324f6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
324f7 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
324f8 3b 0a 0a 20 20 6e 20 3d 20 66 74 73 33 48 61 73  ;..  n = fts3Has
324f9 68 43 6f 75 6e 74 28 70 54 65 72 6d 73 29 3b 0a  hCount(pTerms);.
324fa 20 20 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65    pData = sqlite
324fb 33 5f 6d 61 6c 6c 6f 63 28 6e 2a 73 69 7a 65 6f  3_malloc(n*sizeo
324fc 66 28 54 65 72 6d 44 61 74 61 29 29 3b 0a 0a 20  f(TermData));.. 
324fd 20 66 6f 72 28 69 20 3d 20 30 2c 20 65 20 3d 20   for(i = 0, e = 
324fe 66 74 73 33 48 61 73 68 46 69 72 73 74 28 70 54  fts3HashFirst(pT
324ff 65 72 6d 73 29 3b 20 65 3b 20 69 2b 2b 2c 20 65  erms); e; i++, e
32500 20 3d 20 66 74 73 33 48 61 73 68 4e 65 78 74 28   = fts3HashNext(
32501 65 29 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  e)){.    assert(
32502 20 69 3c 6e 20 29 3b 0a 20 20 20 20 70 44 61 74   i<n );.    pDat
32503 61 5b 69 5d 2e 70 54 65 72 6d 20 3d 20 66 74 73  a[i].pTerm = fts
32504 33 48 61 73 68 4b 65 79 28 65 29 3b 0a 20 20 20  3HashKey(e);.   
32505 20 70 44 61 74 61 5b 69 5d 2e 6e 54 65 72 6d 20   pData[i].nTerm 
32506 3d 20 66 74 73 33 48 61 73 68 4b 65 79 73 69 7a  = fts3HashKeysiz
32507 65 28 65 29 3b 0a 20 20 20 20 70 44 61 74 61 5b  e(e);.    pData[
32508 69 5d 2e 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20  i].pCollector = 
32509 66 74 73 33 48 61 73 68 44 61 74 61 28 65 29 3b  fts3HashData(e);
3250a 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
3250b 3d 3d 6e 20 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44  ==n );..  /* TOD
3250c 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64 20  O(shess) Should 
3250d 77 65 20 61 6c 6c 6f 77 20 75 73 65 72 2d 64 65  we allow user-de
3250e 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  fined collation 
3250f 73 65 71 75 65 6e 63 65 73 2c 0a 20 20 2a 2a 20  sequences,.  ** 
32510 68 65 72 65 3f 20 20 49 20 74 68 69 6e 6b 20 77  here?  I think w
32511 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 68 61 74  e only need that
32512 20 6f 6e 63 65 20 77 65 20 73 75 70 70 6f 72 74   once we support
32513 20 70 72 65 66 69 78 20 73 65 61 72 63 68 65 73   prefix searches
32514 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 3e 31  ..  */.  if( n>1
32515 20 29 20 71 73 6f 72 74 28 70 44 61 74 61 2c 20   ) qsort(pData, 
32516 6e 2c 20 73 69 7a 65 6f 66 28 2a 70 44 61 74 61  n, sizeof(*pData
32517 29 2c 20 74 65 72 6d 44 61 74 61 43 6d 70 29 3b  ), termDataCmp);
32518 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  ..  /* TODO(shes
32519 73 29 20 52 65 66 61 63 74 6f 72 20 73 6f 20 74  s) Refactor so t
3251a 68 61 74 20 77 65 20 63 61 6e 20 77 72 69 74 65  hat we can write
3251b 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65   directly to the
3251c 20 73 65 67 6d 65 6e 74 0a 20 20 2a 2a 20 44 61   segment.  ** Da
3251d 74 61 42 75 66 66 65 72 2c 20 61 73 20 68 61 70  taBuffer, as hap
3251e 70 65 6e 73 20 66 6f 72 20 73 65 67 6d 65 6e 74  pens for segment
3251f 20 6d 65 72 67 65 73 2e 0a 20 20 2a 2f 0a 20 20   merges..  */.  
32520 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 30  leafWriterInit(0
32521 2c 20 69 64 78 2c 20 26 77 72 69 74 65 72 29 3b  , idx, &writer);
32522 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  .  dataBufferIni
32523 74 28 26 64 6c 2c 20 30 29 3b 0a 20 20 66 6f 72  t(&dl, 0);.  for
32524 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b  (i=0; i<n; i++){
32525 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52  .    dataBufferR
32526 65 73 65 74 28 26 64 6c 29 3b 0a 20 20 20 20 64  eset(&dl);.    d
32527 6c 63 41 64 64 44 6f 63 6c 69 73 74 28 70 44 61  lcAddDoclist(pDa
32528 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65 63 74 6f 72  ta[i].pCollector
32529 2c 20 26 64 6c 29 3b 0a 20 20 20 20 72 63 20 3d  , &dl);.    rc =
3252a 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28   leafWriterStep(
3252b 76 2c 20 26 77 72 69 74 65 72 2c 0a 20 20 20 20  v, &writer,.    
3252c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3252d 20 20 20 20 70 44 61 74 61 5b 69 5d 2e 70 54 65      pData[i].pTe
3252e 72 6d 2c 20 70 44 61 74 61 5b 69 5d 2e 6e 54 65  rm, pData[i].nTe
3252f 72 6d 2c 20 64 6c 2e 70 44 61 74 61 2c 20 64 6c  rm, dl.pData, dl
32530 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28  .nData);.    if(
32531 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32532 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 20   goto err;.  }. 
32533 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
32534 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69  Finalize(v, &wri
32535 74 65 72 29 3b 0a 0a 20 65 72 72 3a 0a 20 20 64  ter);.. err:.  d
32536 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
32537 28 26 64 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33  (&dl);.  sqlite3
32538 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a 20 20  _free(pData);.  
32539 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f  leafWriterDestro
3253a 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 72 65  y(&writer);.  re
3253b 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 49  turn rc;.}../* I
3253c 66 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 68  f pendingTerms h
3253d 61 73 20 64 61 74 61 2c 20 66 72 65 65 20 69 74  as data, free it
3253e 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
3253f 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d  clearPendingTerm
32540 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
32541 2a 76 29 7b 0a 20 20 69 66 28 20 76 2d 3e 6e 50  *v){.  if( v->nP
32542 65 6e 64 69 6e 67 44 61 74 61 3e 3d 30 20 29 7b  endingData>=0 ){
32543 0a 20 20 20 20 66 74 73 33 48 61 73 68 45 6c 65  .    fts3HashEle
32544 6d 20 2a 65 3b 0a 20 20 20 20 66 6f 72 28 65 3d  m *e;.    for(e=
32545 66 74 73 33 48 61 73 68 46 69 72 73 74 28 26 76  fts3HashFirst(&v
32546 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29 3b  ->pendingTerms);
32547 20 65 3b 20 65 3d 66 74 73 33 48 61 73 68 4e 65   e; e=fts3HashNe
32548 78 74 28 65 29 29 7b 0a 20 20 20 20 20 20 64 6c  xt(e)){.      dl
32549 63 44 65 6c 65 74 65 28 66 74 73 33 48 61 73 68  cDelete(fts3Hash
3254a 44 61 74 61 28 65 29 29 3b 0a 20 20 20 20 7d 0a  Data(e));.    }.
3254b 20 20 20 20 66 74 73 33 48 61 73 68 43 6c 65 61      fts3HashClea
3254c 72 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72  r(&v->pendingTer
3254d 6d 73 29 3b 0a 20 20 20 20 76 2d 3e 6e 50 65 6e  ms);.    v->nPen
3254e 64 69 6e 67 44 61 74 61 20 3d 20 2d 31 3b 0a 20  dingData = -1;. 
3254f 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
32550 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20  TE_OK;.}../* If 
32551 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 68 61 73  pendingTerms has
32552 20 64 61 74 61 2c 20 66 6c 75 73 68 20 69 74 20   data, flush it 
32553 74 6f 20 61 20 6c 65 76 65 6c 2d 7a 65 72 6f 20  to a level-zero 
32554 73 65 67 6d 65 6e 74 2c 20 61 6e 64 0a 2a 2a 20  segment, and.** 
32555 66 72 65 65 20 69 74 2e 0a 2a 2f 0a 73 74 61 74  free it..*/.stat
32556 69 63 20 69 6e 74 20 66 6c 75 73 68 50 65 6e 64  ic int flushPend
32557 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78  ingTerms(fulltex
32558 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69 66  t_vtab *v){.  if
32559 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74  ( v->nPendingDat
3255a 61 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  a>=0 ){.    int 
3255b 72 63 20 3d 20 77 72 69 74 65 5a 65 72 6f 53 65  rc = writeZeroSe
3255c 67 6d 65 6e 74 28 76 2c 20 26 76 2d 3e 70 65 6e  gment(v, &v->pen
3255d 64 69 6e 67 54 65 72 6d 73 29 3b 0a 20 20 20 20  dingTerms);.    
3255e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3255f 4b 20 29 20 63 6c 65 61 72 50 65 6e 64 69 6e 67  K ) clearPending
32560 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20 72 65  Terms(v);.    re
32561 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 72  turn rc;.  }.  r
32562 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
32563 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69 6e  .}../* If pendin
32564 67 54 65 72 6d 73 20 69 73 20 22 74 6f 6f 20 62  gTerms is "too b
32565 69 67 22 2c 20 6f 72 20 64 6f 63 69 64 20 69 73  ig", or docid is
32566 20 6f 75 74 20 6f 66 20 6f 72 64 65 72 2c 20 66   out of order, f
32567 6c 75 73 68 20 69 74 2e 0a 2a 2a 20 52 65 67 61  lush it..** Rega
32568 72 64 6c 65 73 73 2c 20 62 65 20 63 65 72 74 61  rdless, be certa
32569 69 6e 20 74 68 61 74 20 70 65 6e 64 69 6e 67 54  in that pendingT
3256a 65 72 6d 73 20 69 73 20 69 6e 69 74 69 61 6c 69  erms is initiali
3256b 7a 65 64 20 66 6f 72 20 75 73 65 2e 0a 2a 2f 0a  zed for use..*/.
3256c 73 74 61 74 69 63 20 69 6e 74 20 69 6e 69 74 50  static int initP
3256d 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c  endingTerms(full
3256e 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
3256f 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
32570 64 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68  d){.  /* TODO(sh
32571 65 73 73 29 20 45 78 70 6c 6f 72 65 20 77 68 65  ess) Explore whe
32572 74 68 65 72 20 70 61 72 74 69 61 6c 6c 79 20 66  ther partially f
32573 6c 75 73 68 69 6e 67 20 74 68 65 20 62 75 66 66  lushing the buff
32574 65 72 20 6f 6e 0a 20 20 2a 2a 20 66 6f 72 63 65  er on.  ** force
32575 64 2d 66 6c 75 73 68 20 77 6f 75 6c 64 20 70 72  d-flush would pr
32576 6f 76 69 64 65 20 62 65 74 74 65 72 20 70 65 72  ovide better per
32577 66 6f 72 6d 61 6e 63 65 2e 20 20 49 20 73 75 73  formance.  I sus
32578 70 65 63 74 20 74 68 61 74 20 69 66 0a 20 20 2a  pect that if.  *
32579 2a 20 77 65 20 6f 72 64 65 72 65 64 20 74 68 65  * we ordered the
3257a 20 64 6f 63 6c 69 73 74 73 20 62 79 20 73 69 7a   doclists by siz
3257b 65 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74 68  e and flushed th
3257c 65 20 6c 61 72 67 65 73 74 20 75 6e 74 69 6c 20  e largest until 
3257d 74 68 65 0a 20 20 2a 2a 20 62 75 66 66 65 72 20  the.  ** buffer 
3257e 77 61 73 20 68 61 6c 66 20 65 6d 70 74 79 2c 20  was half empty, 
3257f 74 68 61 74 20 77 6f 75 6c 64 20 6c 65 74 20 74  that would let t
32580 68 65 20 6c 65 73 73 20 66 72 65 71 75 65 6e 74  he less frequent
32581 20 74 65 72 6d 73 0a 20 20 2a 2a 20 67 65 6e 65   terms.  ** gene
32582 72 61 74 65 20 6c 6f 6e 67 65 72 20 64 6f 63 6c  rate longer docl
32583 69 73 74 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ists..  */.  if(
32584 20 69 44 6f 63 69 64 3c 3d 76 2d 3e 69 50 72 65   iDocid<=v->iPre
32585 76 44 6f 63 69 64 20 7c 7c 20 76 2d 3e 6e 50 65  vDocid || v->nPe
32586 6e 64 69 6e 67 44 61 74 61 3e 6b 50 65 6e 64 69  ndingData>kPendi
32587 6e 67 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20  ngThreshold ){. 
32588 20 20 20 69 6e 74 20 72 63 20 3d 20 66 6c 75 73     int rc = flus
32589 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29  hPendingTerms(v)
3258a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
3258b 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3258c 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76   rc;.  }.  if( v
3258d 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30  ->nPendingData<0
3258e 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73 68   ){.    fts3Hash
3258f 49 6e 69 74 28 26 76 2d 3e 70 65 6e 64 69 6e 67  Init(&v->pending
32590 54 65 72 6d 73 2c 20 46 54 53 33 5f 48 41 53 48  Terms, FTS3_HASH
32591 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20 20  _STRING, 1);.   
32592 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
32593 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 76 2d 3e 69   = 0;.  }.  v->i
32594 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f 63  PrevDocid = iDoc
32595 69 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  id;.  return SQL
32596 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68  ITE_OK;.}../* Th
32597 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  is function impl
32598 65 6d 65 6e 74 73 20 74 68 65 20 78 55 70 64 61  ements the xUpda
32599 74 65 20 63 61 6c 6c 62 61 63 6b 3b 20 69 74 20  te callback; it 
3259a 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c  is the top-level
3259b 20 65 6e 74 72 79 0a 20 2a 20 70 6f 69 6e 74 20   entry. * point 
3259c 66 6f 72 20 69 6e 73 65 72 74 69 6e 67 2c 20 64  for inserting, d
3259d 65 6c 65 74 69 6e 67 20 6f 72 20 75 70 64 61 74  eleting or updat
3259e 69 6e 67 20 61 20 72 6f 77 20 69 6e 20 61 20 66  ing a row in a f
3259f 75 6c 6c 2d 74 65 78 74 20 74 61 62 6c 65 2e 20  ull-text table. 
325a0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  */.static int fu
325a1 6c 6c 74 65 78 74 55 70 64 61 74 65 28 73 71 6c  lltextUpdate(sql
325a2 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
325a3 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69  , int nArg, sqli
325a4 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 70 41 72  te3_value **ppAr
325a5 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  g,.             
325a6 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
325a7 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77 69  ite_int64 *pRowi
325a8 64 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  d){.  fulltext_v
325a9 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65  tab *v = (fullte
325aa 78 74 5f 76 74 61 62 20 2a 29 20 70 56 74 61 62  xt_vtab *) pVtab
325ab 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 46  ;.  int rc;..  F
325ac 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 55  TSTRACE(("FTS3 U
325ad 70 64 61 74 65 20 25 70 5c 6e 22 2c 20 70 56 74  pdate %p\n", pVt
325ae 61 62 29 29 3b 0a 0a 20 20 69 66 28 20 6e 41 72  ab));..  if( nAr
325af 67 3c 32 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  g<2 ){.    rc = 
325b0 69 6e 64 65 78 5f 64 65 6c 65 74 65 28 76 2c 20  index_delete(v, 
325b1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
325b2 74 36 34 28 70 70 41 72 67 5b 30 5d 29 29 3b 0a  t64(ppArg[0]));.
325b3 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
325b4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f  TE_OK ){.      /
325b5 2a 20 49 66 20 77 65 20 6a 75 73 74 20 64 65 6c  * If we just del
325b6 65 74 65 64 20 74 68 65 20 6c 61 73 74 20 72 6f  eted the last ro
325b7 77 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  w in the table, 
325b8 63 6c 65 61 72 20 6f 75 74 20 74 68 65 0a 20 20  clear out the.  
325b9 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 64 61 74      ** index dat
325ba 61 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  a..      */.    
325bb 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 65    rc = content_e
325bc 78 69 73 74 73 28 76 29 3b 0a 20 20 20 20 20 20  xists(v);.      
325bd 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
325be 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OW ){.        rc
325bf 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
325c0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63      }else if( rc
325c1 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
325c2 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 65 61  .        /* Clea
325c3 72 20 74 68 65 20 70 65 6e 64 69 6e 67 20 74 65  r the pending te
325c4 72 6d 73 20 73 6f 20 77 65 20 64 6f 6e 27 74 20  rms so we don't 
325c5 66 6c 75 73 68 20 61 20 75 73 65 6c 65 73 73 20  flush a useless 
325c6 6c 65 76 65 6c 2d 30 0a 20 20 20 20 20 20 20 20  level-0.        
325c7 2a 2a 20 73 65 67 6d 65 6e 74 20 77 68 65 6e 20  ** segment when 
325c8 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
325c9 63 6c 6f 73 65 73 2e 0a 20 20 20 20 20 20 20 20  closes..        
325ca 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  */.        rc = 
325cb 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d  clearPendingTerm
325cc 73 28 76 29 3b 0a 20 20 20 20 20 20 20 20 69 66  s(v);.        if
325cd 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
325ce 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
325cf 3d 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65 5f  = segdir_delete_
325d0 61 6c 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20  all(v);.        
325d1 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
325d2 20 20 7d 20 65 6c 73 65 20 69 66 28 20 73 71 6c    } else if( sql
325d3 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
325d4 70 70 41 72 67 5b 30 5d 29 20 21 3d 20 53 51 4c  ppArg[0]) != SQL
325d5 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
325d6 2f 2a 20 41 6e 20 75 70 64 61 74 65 3a 0a 20 20  /* An update:.  
325d7 20 20 20 2a 20 70 70 41 72 67 5b 30 5d 20 3d 20     * ppArg[0] = 
325d8 6f 6c 64 20 72 6f 77 69 64 0a 20 20 20 20 20 2a  old rowid.     *
325d9 20 70 70 41 72 67 5b 31 5d 20 3d 20 6e 65 77 20   ppArg[1] = new 
325da 72 6f 77 69 64 0a 20 20 20 20 20 2a 20 70 70 41  rowid.     * ppA
325db 72 67 5b 32 2e 2e 32 2b 76 2d 3e 6e 43 6f 6c 75  rg[2..2+v->nColu
325dc 6d 6e 2d 31 5d 20 3d 20 76 61 6c 75 65 73 0a 20  mn-1] = values. 
325dd 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d      * ppArg[2+v-
325de 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 76 61 6c 75  >nColumn] = valu
325df 65 20 66 6f 72 20 6d 61 67 69 63 20 63 6f 6c 75  e for magic colu
325e0 6d 6e 20 28 77 65 20 69 67 6e 6f 72 65 20 74 68  mn (we ignore th
325e1 69 73 29 0a 20 20 20 20 20 2a 20 70 70 41 72 67  is).     * ppArg
325e2 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d  [2+v->nColumn+1]
325e3 20 3d 20 76 61 6c 75 65 20 66 6f 72 20 64 6f 63   = value for doc
325e4 69 64 0a 20 20 20 20 20 2a 2f 0a 20 20 20 20 73  id.     */.    s
325e5 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 69  qlite_int64 rowi
325e6 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
325e7 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b 30 5d  e_int64(ppArg[0]
325e8 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
325e9 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 70  e3_value_type(pp
325ea 41 72 67 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54  Arg[1]) != SQLIT
325eb 45 5f 49 4e 54 45 47 45 52 20 7c 7c 0a 20 20 20  E_INTEGER ||.   
325ec 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
325ed 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b 31  ue_int64(ppArg[1
325ee 5d 29 20 21 3d 20 72 6f 77 69 64 20 29 7b 0a 20  ]) != rowid ){. 
325ef 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
325f0 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 77 65 20 64  _ERROR;  /* we d
325f1 6f 6e 27 74 20 61 6c 6c 6f 77 20 63 68 61 6e 67  on't allow chang
325f2 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 2a 2f  ing the rowid */
325f3 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73  .    }else if( s
325f4 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
325f5 65 28 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f  e(ppArg[2+v->nCo
325f6 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20 53 51 4c 49  lumn+1]) != SQLI
325f7 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c 0a 20 20  TE_INTEGER ||.  
325f8 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
325f9 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
325fa 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75  ppArg[2+v->nColu
325fb 6d 6e 2b 31 5d 29 20 21 3d 20 72 6f 77 69 64 20  mn+1]) != rowid 
325fc 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
325fd 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20  LITE_ERROR;  /* 
325fe 77 65 20 64 6f 6e 27 74 20 61 6c 6c 6f 77 20 63  we don't allow c
325ff 68 61 6e 67 69 6e 67 20 74 68 65 20 64 6f 63 69  hanging the doci
32600 64 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  d */.    }else{.
32601 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41        assert( nA
32602 72 67 3d 3d 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e  rg==2+v->nColumn
32603 2b 32 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  +2);.      rc = 
32604 69 6e 64 65 78 5f 75 70 64 61 74 65 28 76 2c 20  index_update(v, 
32605 72 6f 77 69 64 2c 20 26 70 70 41 72 67 5b 32 5d  rowid, &ppArg[2]
32606 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73  );.    }.  } els
32607 65 20 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 69 6e  e {.    /* An in
32608 73 65 72 74 3a 0a 20 20 20 20 20 2a 20 70 70 41  sert:.     * ppA
32609 72 67 5b 31 5d 20 3d 20 72 65 71 75 65 73 74 65  rg[1] = requeste
3260a 64 20 72 6f 77 69 64 0a 20 20 20 20 20 2a 20 70  d rowid.     * p
3260b 70 41 72 67 5b 32 2e 2e 32 2b 76 2d 3e 6e 43 6f  pArg[2..2+v->nCo
3260c 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61 6c 75 65 73  lumn-1] = values
3260d 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2b  .     * ppArg[2+
3260e 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 76 61  v->nColumn] = va
3260f 6c 75 65 20 66 6f 72 20 6d 61 67 69 63 20 63 6f  lue for magic co
32610 6c 75 6d 6e 20 28 77 65 20 69 67 6e 6f 72 65 20  lumn (we ignore 
32611 74 68 69 73 29 0a 20 20 20 20 20 2a 20 70 70 41  this).     * ppA
32612 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b  rg[2+v->nColumn+
32613 31 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20 64  1] = value for d
32614 6f 63 69 64 0a 20 20 20 20 20 2a 2f 0a 20 20 20  ocid.     */.   
32615 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
32616 70 52 65 71 75 65 73 74 44 6f 63 69 64 20 3d 20  pRequestDocid = 
32617 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75  ppArg[2+v->nColu
32618 6d 6e 2b 31 5d 3b 0a 20 20 20 20 61 73 73 65 72  mn+1];.    asser
32619 74 28 20 6e 41 72 67 3d 3d 32 2b 76 2d 3e 6e 43  t( nArg==2+v->nC
3261a 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 69 66  olumn+2);.    if
3261b 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 21 3d  ( SQLITE_NULL !=
3261c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
3261d 79 70 65 28 70 52 65 71 75 65 73 74 44 6f 63 69  ype(pRequestDoci
3261e 64 29 20 26 26 0a 20 20 20 20 20 20 20 20 53 51  d) &&.        SQ
3261f 4c 49 54 45 5f 4e 55 4c 4c 20 21 3d 20 73 71 6c  LITE_NULL != sql
32620 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
32621 70 70 41 72 67 5b 31 5d 29 20 29 7b 0a 20 20 20  ppArg[1]) ){.   
32622 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
32623 29 20 43 6f 6e 73 69 64 65 72 20 61 6c 6c 6f 77  ) Consider allow
32624 69 6e 67 20 74 68 69 73 20 74 6f 20 77 6f 72 6b  ing this to work
32625 20 69 66 20 74 68 65 20 76 61 6c 75 65 73 20 61   if the values a
32626 72 65 0a 20 20 20 20 20 20 2a 2a 20 69 64 65 6e  re.      ** iden
32627 74 69 63 61 6c 2e 20 20 49 27 6d 20 69 6e 63 6c  tical.  I'm incl
32628 69 6e 65 64 20 74 6f 20 64 69 73 63 6f 75 72 61  ined to discoura
32629 67 65 20 74 68 61 74 20 75 73 61 67 65 2c 20 74  ge that usage, t
3262a 68 6f 75 67 68 2c 0a 20 20 20 20 20 20 2a 2a 20  hough,.      ** 
3262b 67 69 76 65 6e 20 74 68 61 74 20 62 6f 74 68 20  given that both 
3262c 72 6f 77 69 64 20 61 6e 64 20 64 6f 63 69 64 20  rowid and docid 
3262d 61 72 65 20 73 70 65 63 69 61 6c 20 63 6f 6c 75  are special colu
3262e 6d 6e 73 2e 20 20 42 65 74 74 65 72 0a 20 20 20  mns.  Better.   
3262f 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 74     ** would be t
32630 6f 20 64 65 66 69 6e 65 20 6f 6e 65 20 6f 72 20  o define one or 
32631 74 68 65 20 6f 74 68 65 72 20 61 73 20 74 68 65  the other as the
32632 20 64 65 66 61 75 6c 74 20 77 69 6e 6e 65 72 2c   default winner,
32633 0a 20 20 20 20 20 20 2a 2a 20 62 75 74 20 73 68  .      ** but sh
32634 6f 75 6c 64 20 69 74 20 62 65 20 66 74 73 33 2d  ould it be fts3-
32635 63 65 6e 74 72 69 63 20 28 64 6f 63 69 64 29 20  centric (docid) 
32636 6f 72 20 53 51 4c 69 74 65 2d 63 65 6e 74 72 69  or SQLite-centri
32637 63 0a 20 20 20 20 20 20 2a 2a 20 28 72 6f 77 69  c.      ** (rowi
32638 64 29 3f 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  d)?.      */.   
32639 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
3263a 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  RROR;.    }else{
3263b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
3263c 45 5f 4e 55 4c 4c 20 3d 3d 20 73 71 6c 69 74 65  E_NULL == sqlite
3263d 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 52 65  3_value_type(pRe
3263e 71 75 65 73 74 44 6f 63 69 64 29 20 29 7b 0a 20  questDocid) ){. 
3263f 20 20 20 20 20 20 20 70 52 65 71 75 65 73 74 44         pRequestD
32640 6f 63 69 64 20 3d 20 70 70 41 72 67 5b 31 5d 3b  ocid = ppArg[1];
32641 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
32642 63 20 3d 20 69 6e 64 65 78 5f 69 6e 73 65 72 74  c = index_insert
32643 28 76 2c 20 70 52 65 71 75 65 73 74 44 6f 63 69  (v, pRequestDoci
32644 64 2c 20 26 70 70 41 72 67 5b 32 5d 2c 20 70 52  d, &ppArg[2], pR
32645 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  owid);.    }.  }
32646 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
32647 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  ..static int ful
32648 6c 74 65 78 74 53 79 6e 63 28 73 71 6c 69 74 65  ltextSync(sqlite
32649 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
3264a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
3264b 33 20 78 53 79 6e 63 28 29 5c 6e 22 29 29 3b 0a  3 xSync()\n"));.
3264c 20 20 72 65 74 75 72 6e 20 66 6c 75 73 68 50 65    return flushPe
3264d 6e 64 69 6e 67 54 65 72 6d 73 28 28 66 75 6c 6c  ndingTerms((full
3264e 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56 74 61  text_vtab *)pVta
3264f 62 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  b);.}..static in
32650 74 20 66 75 6c 6c 74 65 78 74 42 65 67 69 6e 28  t fulltextBegin(
32651 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
32652 74 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  tab){.  fulltext
32653 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c  _vtab *v = (full
32654 74 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56 74  text_vtab *) pVt
32655 61 62 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28  ab;.  FTSTRACE((
32656 22 46 54 53 33 20 78 42 65 67 69 6e 28 29 5c 6e  "FTS3 xBegin()\n
32657 22 29 29 3b 0a 0a 20 20 2f 2a 20 41 6e 79 20 62  "));..  /* Any b
32658 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73 20  uffered updates 
32659 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e  should have been
3265a 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65 20   cleared by the 
3265b 70 72 65 76 69 6f 75 73 0a 20 20 2a 2a 20 74 72  previous.  ** tr
3265c 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  ansaction..  */.
3265d 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e 50 65    assert( v->nPe
3265e 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a 20  ndingData<0 );. 
3265f 20 72 65 74 75 72 6e 20 63 6c 65 61 72 50 65 6e   return clearPen
32660 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 7d 0a  dingTerms(v);.}.
32661 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
32662 74 65 78 74 43 6f 6d 6d 69 74 28 73 71 6c 69 74  textCommit(sqlit
32663 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b  e3_vtab *pVtab){
32664 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
32665 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *v = (fulltext_
32666 76 74 61 62 20 2a 29 20 70 56 74 61 62 3b 0a 20  vtab *) pVtab;. 
32667 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33   FTSTRACE(("FTS3
32668 20 78 43 6f 6d 6d 69 74 28 29 5c 6e 22 29 29 3b   xCommit()\n"));
32669 0a 0a 20 20 2f 2a 20 42 75 66 66 65 72 65 64 20  ..  /* Buffered 
3266a 75 70 64 61 74 65 73 20 73 68 6f 75 6c 64 20 68  updates should h
3266b 61 76 65 20 62 65 65 6e 20 63 6c 65 61 72 65 64  ave been cleared
3266c 20 62 79 20 66 75 6c 6c 74 65 78 74 53 79 6e 63   by fulltextSync
3266d 28 29 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  (). */.  assert(
3266e 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
3266f 3c 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 63  <0 );.  return c
32670 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73  learPendingTerms
32671 28 76 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  (v);.}..static i
32672 6e 74 20 66 75 6c 6c 74 65 78 74 52 6f 6c 6c 62  nt fulltextRollb
32673 61 63 6b 28 73 71 6c 69 74 65 33 5f 76 74 61 62  ack(sqlite3_vtab
32674 20 2a 70 56 74 61 62 29 7b 0a 20 20 46 54 53 54   *pVtab){.  FTST
32675 52 41 43 45 28 28 22 46 54 53 33 20 78 52 6f 6c  RACE(("FTS3 xRol
32676 6c 62 61 63 6b 28 29 5c 6e 22 29 29 3b 0a 20 20  lback()\n"));.  
32677 72 65 74 75 72 6e 20 63 6c 65 61 72 50 65 6e 64  return clearPend
32678 69 6e 67 54 65 72 6d 73 28 28 66 75 6c 6c 74 65  ingTerms((fullte
32679 78 74 5f 76 74 61 62 20 2a 29 70 56 74 61 62 29  xt_vtab *)pVtab)
3267a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
3267b 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
3267c 20 73 6e 69 70 70 65 74 28 29 20 66 75 6e 63 74   snippet() funct
3267d 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a 2a 2f 0a  ion for FTS3.*/.
3267e 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70  static void snip
3267f 70 65 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  petFunc(.  sqlit
32680 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  e3_context *pCon
32681 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
32682 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
32683 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 75  e **argv.){.  fu
32684 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
32685 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61 72  Cursor;.  if( ar
32686 67 63 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20  gc<1 ) return;. 
32687 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
32688 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
32689 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c  !=SQLITE_BLOB ||
3268a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76  .      sqlite3_v
3268b 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
3268c 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72  0])!=sizeof(pCur
3268d 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  sor) ){.    sqli
3268e 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
3268f 28 70 43 6f 6e 74 65 78 74 2c 20 22 69 6c 6c 65  (pContext, "ille
32690 67 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d 65  gal first argume
32691 6e 74 20 74 6f 20 68 74 6d 6c 5f 73 6e 69 70 70  nt to html_snipp
32692 65 74 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65  et",-1);.  }else
32693 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
32694 20 2a 7a 53 74 61 72 74 20 3d 20 22 3c 62 3e 22   *zStart = "<b>"
32695 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
32696 20 2a 7a 45 6e 64 20 3d 20 22 3c 2f 62 3e 22 3b   *zEnd = "</b>";
32697 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
32698 2a 7a 45 6c 6c 69 70 73 69 73 20 3d 20 22 3c 62  *zEllipsis = "<b
32699 3e 2e 2e 2e 3c 2f 62 3e 22 3b 0a 20 20 20 20 6d  >...</b>";.    m
3269a 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c 20  emcpy(&pCursor, 
3269b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
3269c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a  ob(argv[0]), siz
3269d 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a 20  eof(pCursor));. 
3269e 20 20 20 69 66 28 20 61 72 67 63 3e 3d 32 20 29     if( argc>=2 )
3269f 7b 0a 20 20 20 20 20 20 7a 53 74 61 72 74 20 3d  {.      zStart =
326a0 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
326a1 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
326a2 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20  (argv[1]);.     
326a3 20 69 66 28 20 61 72 67 63 3e 3d 33 20 29 7b 0a   if( argc>=3 ){.
326a4 20 20 20 20 20 20 20 20 7a 45 6e 64 20 3d 20 28          zEnd = (
326a5 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69  const char*)sqli
326a6 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
326a7 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20  rgv[2]);.       
326a8 20 69 66 28 20 61 72 67 63 3e 3d 34 20 29 7b 0a   if( argc>=4 ){.
326a9 20 20 20 20 20 20 20 20 20 20 7a 45 6c 6c 69 70            zEllip
326aa 73 69 73 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  sis = (const cha
326ab 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r*)sqlite3_value
326ac 5f 74 65 78 74 28 61 72 67 76 5b 33 5d 29 3b 0a  _text(argv[3]);.
326ad 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
326ae 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 6e 69 70  }.    }.    snip
326af 70 65 74 41 6c 6c 4f 66 66 73 65 74 73 28 70 43  petAllOffsets(pC
326b0 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 6e 69 70  ursor);.    snip
326b1 70 65 74 54 65 78 74 28 70 43 75 72 73 6f 72 2c  petText(pCursor,
326b2 20 7a 53 74 61 72 74 2c 20 7a 45 6e 64 2c 20 7a   zStart, zEnd, z
326b3 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20 20 20 73  Ellipsis);.    s
326b4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
326b5 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 70 43 75  xt(pContext, pCu
326b6 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 53  rsor->snippet.zS
326b7 6e 69 70 70 65 74 2c 0a 20 20 20 20 20 20 20 20  nippet,.        
326b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
326b9 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74  pCursor->snippet
326ba 2e 6e 53 6e 69 70 70 65 74 2c 20 53 51 4c 49 54  .nSnippet, SQLIT
326bb 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d  E_STATIC);.  }.}
326bc 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
326bd 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 66  tation of the of
326be 66 73 65 74 73 28 29 20 66 75 6e 63 74 69 6f 6e  fsets() function
326bf 20 66 6f 72 20 46 54 53 33 0a 2a 2f 0a 73 74 61   for FTS3.*/.sta
326c0 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74  tic void snippet
326c1 4f 66 66 73 65 74 73 46 75 6e 63 28 0a 20 20 73  OffsetsFunc(.  s
326c2 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
326c3 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  pContext,.  int 
326c4 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
326c5 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
326c6 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f    fulltext_curso
326c7 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 66  r *pCursor;.  if
326c8 28 20 61 72 67 63 3c 31 20 29 20 72 65 74 75 72  ( argc<1 ) retur
326c9 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  n;.  if( sqlite3
326ca 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
326cb 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  [0])!=SQLITE_BLO
326cc 42 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69 74  B ||.      sqlit
326cd 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
326ce 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28  rgv[0])!=sizeof(
326cf 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20  pCursor) ){.    
326d0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
326d1 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
326d2 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72  illegal first ar
326d3 67 75 6d 65 6e 74 20 74 6f 20 6f 66 66 73 65 74  gument to offset
326d4 73 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  s",-1);.  }else{
326d5 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75  .    memcpy(&pCu
326d6 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61  rsor, sqlite3_va
326d7 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
326d8 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  ), sizeof(pCurso
326d9 72 29 29 3b 0a 20 20 20 20 73 6e 69 70 70 65 74  r));.    snippet
326da 41 6c 6c 4f 66 66 73 65 74 73 28 70 43 75 72 73  AllOffsets(pCurs
326db 6f 72 29 3b 0a 20 20 20 20 73 6e 69 70 70 65 74  or);.    snippet
326dc 4f 66 66 73 65 74 54 65 78 74 28 26 70 43 75 72  OffsetText(&pCur
326dd 73 6f 72 2d 3e 73 6e 69 70 70 65 74 29 3b 0a 20  sor->snippet);. 
326de 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
326df 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74 2c  t_text(pContext,
326e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
326e1 20 20 20 20 20 20 20 20 20 70 43 75 72 73 6f 72           pCursor
326e2 2d 3e 73 6e 69 70 70 65 74 2e 7a 4f 66 66 73 65  ->snippet.zOffse
326e3 74 2c 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70  t, pCursor->snip
326e4 70 65 74 2e 6e 4f 66 66 73 65 74 2c 0a 20 20 20  pet.nOffset,.   
326e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
326e6 20 20 20 20 20 53 51 4c 49 54 45 5f 53 54 41 54       SQLITE_STAT
326e7 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 4f  IC);.  }.}../* O
326e8 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 69  ptLeavesReader i
326e9 73 20 6e 65 61 72 6c 79 20 69 64 65 6e 74 69 63  s nearly identic
326ea 61 6c 20 74 6f 20 4c 65 61 76 65 73 52 65 61 64  al to LeavesRead
326eb 65 72 2c 20 65 78 63 65 70 74 20 74 68 61 74 0a  er, except that.
326ec 2a 2a 20 77 68 65 72 65 20 4c 65 61 76 65 73 52  ** where LeavesR
326ed 65 61 64 65 72 20 69 73 20 67 65 61 72 65 64 20  eader is geared 
326ee 74 6f 77 61 72 64 73 20 74 68 65 20 6d 65 72 67  towards the merg
326ef 69 6e 67 20 6f 66 20 63 6f 6d 70 6c 65 74 65 0a  ing of complete.
326f0 2a 2a 20 73 65 67 6d 65 6e 74 20 6c 65 76 65 6c  ** segment level
326f1 73 20 28 77 69 74 68 20 65 78 61 63 74 6c 79 20  s (with exactly 
326f2 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d  MERGE_COUNT segm
326f3 65 6e 74 73 29 2c 20 4f 70 74 4c 65 61 76 65 73  ents), OptLeaves
326f4 52 65 61 64 65 72 0a 2a 2a 20 69 73 20 67 65 61  Reader.** is gea
326f5 72 65 64 20 74 6f 77 61 72 64 73 20 69 6d 70 6c  red towards impl
326f6 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
326f7 65 20 6f 70 74 69 6d 69 7a 65 28 29 20 66 75 6e  e optimize() fun
326f8 63 74 69 6f 6e 2c 20 61 6e 64 0a 2a 2a 20 63 61  ction, and.** ca
326f9 6e 20 6d 65 72 67 65 20 61 6c 6c 20 73 65 67 6d  n merge all segm
326fa 65 6e 74 73 20 73 69 6d 75 6c 74 61 6e 65 6f 75  ents simultaneou
326fb 73 6c 79 2e 20 20 54 68 69 73 20 76 65 72 73 69  sly.  This versi
326fc 6f 6e 20 6d 61 79 20 62 65 0a 2a 2a 20 73 6f 6d  on may be.** som
326fd 65 77 68 61 74 20 6c 65 73 73 20 65 66 66 69 63  ewhat less effic
326fe 69 65 6e 74 20 74 68 61 6e 20 4c 65 61 76 65 73  ient than Leaves
326ff 52 65 61 64 65 72 20 62 65 63 61 75 73 65 20 69  Reader because i
32700 74 20 6d 65 72 67 65 73 20 69 6e 74 6f 20 61 6e  t merges into an
32701 0a 2a 2a 20 61 63 63 75 6d 75 6c 61 74 6f 72 20  .** accumulator 
32702 72 61 74 68 65 72 20 74 68 61 6e 20 64 6f 69 6e  rather than doin
32703 67 20 61 6e 20 4e 2d 77 61 79 20 6d 65 72 67 65  g an N-way merge
32704 2c 20 62 75 74 20 73 69 6e 63 65 20 73 65 67 6d  , but since segm
32705 65 6e 74 0a 2a 2a 20 73 69 7a 65 20 67 72 6f 77  ent.** size grow
32706 73 20 65 78 70 6f 6e 65 6e 74 69 61 6c 6c 79 20  s exponentially 
32707 28 73 6f 20 73 65 67 6d 65 6e 74 20 63 6f 75 6e  (so segment coun
32708 74 20 6c 6f 67 72 69 74 68 6d 69 63 61 6c 6c 79  t logrithmically
32709 29 20 74 68 69 73 20 69 73 0a 2a 2a 20 70 72 6f  ) this is.** pro
3270a 62 61 62 6c 79 20 6e 6f 74 20 61 6e 20 69 6d 6d  bably not an imm
3270b 65 64 69 61 74 65 20 70 72 6f 62 6c 65 6d 2e 0a  ediate problem..
3270c 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  */./* TODO(shess
3270d 29 3a 20 50 72 6f 76 65 20 74 68 61 74 20 61 73  ): Prove that as
3270e 73 65 72 74 69 6f 6e 2c 20 6f 72 20 65 78 74 65  sertion, or exte
3270f 6e 64 20 74 68 65 20 6d 65 72 67 65 20 63 6f 64  nd the merge cod
32710 65 20 74 6f 0a 2a 2a 20 6d 65 72 67 65 20 74 72  e to.** merge tr
32711 65 65 20 66 61 73 68 69 6f 6e 20 28 6c 69 6b 65  ee fashion (like
32712 20 74 68 65 20 70 72 65 66 69 78 2d 73 65 61 72   the prefix-sear
32713 63 68 69 6e 67 20 63 6f 64 65 20 64 6f 65 73 29  ching code does)
32714 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
32715 73 73 29 3a 20 4f 70 74 4c 65 61 76 65 73 52 65  ss): OptLeavesRe
32716 61 64 65 72 20 61 6e 64 20 4c 65 61 76 65 73 52  ader and LeavesR
32717 65 61 64 65 72 20 63 6f 75 6c 64 20 70 72 6f 62  eader could prob
32718 61 62 6c 79 20 62 65 0a 2a 2a 20 6d 65 72 67 65  ably be.** merge
32719 64 20 77 69 74 68 20 6c 69 74 74 6c 65 20 6f 72  d with little or
3271a 20 6e 6f 20 6c 6f 73 73 20 6f 66 20 70 65 72 66   no loss of perf
3271b 6f 72 6d 61 6e 63 65 20 66 6f 72 20 4c 65 61 76  ormance for Leav
3271c 65 73 52 65 61 64 65 72 2e 20 20 54 68 65 0a 2a  esReader.  The.*
3271d 2a 20 6d 65 72 67 65 64 20 63 6f 64 65 20 77 6f  * merged code wo
3271e 75 6c 64 20 6e 65 65 64 20 74 6f 20 68 61 6e 64  uld need to hand
3271f 6c 65 20 3e 4d 45 52 47 45 5f 43 4f 55 4e 54 20  le >MERGE_COUNT 
32720 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20 77 6f  segments, and wo
32721 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 6e 65 65 64  uld.** also need
32722 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 6f   to be able to o
32723 70 74 69 6f 6e 61 6c 6c 79 20 6f 70 74 69 6d 69  ptionally optimi
32724 7a 65 20 61 77 61 79 20 64 65 6c 65 74 65 73 2e  ze away deletes.
32725 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
32726 63 74 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64  ct OptLeavesRead
32727 65 72 20 7b 0a 20 20 2f 2a 20 53 65 67 6d 65 6e  er {.  /* Segmen
32728 74 20 6e 75 6d 62 65 72 2c 20 74 6f 20 6f 72 64  t number, to ord
32729 65 72 20 72 65 61 64 65 72 73 20 62 79 20 61 67  er readers by ag
3272a 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65 67 6d  e. */.  int segm
3272b 65 6e 74 3b 0a 20 20 4c 65 61 76 65 73 52 65 61  ent;.  LeavesRea
3272c 64 65 72 20 72 65 61 64 65 72 3b 0a 7d 20 4f 70  der reader;.} Op
3272d 74 4c 65 61 76 65 73 52 65 61 64 65 72 3b 0a 0a  tLeavesReader;..
3272e 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c 65  static int optLe
3272f 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28  avesReaderAtEnd(
32730 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
32731 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
32732 75 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72  urn leavesReader
32733 41 74 45 6e 64 28 26 70 52 65 61 64 65 72 2d 3e  AtEnd(&pReader->
32734 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69  reader);.}.stati
32735 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52  c int optLeavesR
32736 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 4f  eaderTermBytes(O
32737 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
32738 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75  pReader){.  retu
32739 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 54  rn leavesReaderT
3273a 65 72 6d 42 79 74 65 73 28 26 70 52 65 61 64 65  ermBytes(&pReade
3273b 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74  r->reader);.}.st
3273c 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
3273d 2a 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  *optLeavesReader
3273e 44 61 74 61 28 4f 70 74 4c 65 61 76 65 73 52 65  Data(OptLeavesRe
3273f 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
32740 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52    return leavesR
32741 65 61 64 65 72 44 61 74 61 28 26 70 52 65 61 64  eaderData(&pRead
32742 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73  er->reader);.}.s
32743 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c 65 61  tatic int optLea
32744 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
32745 65 73 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64  es(OptLeavesRead
32746 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
32747 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65 61  return leavesRea
32748 64 65 72 44 61 74 61 42 79 74 65 73 28 26 70 52  derDataBytes(&pR
32749 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a  eader->reader);.
3274a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  }.static const c
3274b 68 61 72 20 2a 6f 70 74 4c 65 61 76 65 73 52 65  har *optLeavesRe
3274c 61 64 65 72 54 65 72 6d 28 4f 70 74 4c 65 61 76  aderTerm(OptLeav
3274d 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
3274e 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  r){.  return lea
3274f 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 26 70  vesReaderTerm(&p
32750 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b  Reader->reader);
32751 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70  .}.static int op
32752 74 4c 65 61 76 65 73 52 65 61 64 65 72 53 74 65  tLeavesReaderSte
32753 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  p(fulltext_vtab 
32754 2a 76 2c 20 4f 70 74 4c 65 61 76 65 73 52 65 61  *v, OptLeavesRea
32755 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
32756 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65   return leavesRe
32757 61 64 65 72 53 74 65 70 28 76 2c 20 26 70 52 65  aderStep(v, &pRe
32758 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d  ader->reader);.}
32759 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c  .static int optL
3275a 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 43  eavesReaderTermC
3275b 6d 70 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64  mp(OptLeavesRead
3275c 65 72 20 2a 6c 72 31 2c 20 4f 70 74 4c 65 61 76  er *lr1, OptLeav
3275d 65 73 52 65 61 64 65 72 20 2a 6c 72 32 29 7b 0a  esReader *lr2){.
3275e 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52    return leavesR
3275f 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26 6c 72  eaderTermCmp(&lr
32760 31 2d 3e 72 65 61 64 65 72 2c 20 26 6c 72 32 2d  1->reader, &lr2-
32761 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 2f 2a 20 4f  >reader);.}./* O
32762 72 64 65 72 20 62 79 20 74 65 72 6d 20 61 73 63  rder by term asc
32763 65 6e 64 69 6e 67 2c 20 73 65 67 6d 65 6e 74 20  ending, segment 
32764 61 73 63 65 6e 64 69 6e 67 20 28 6f 6c 64 65 73  ascending (oldes
32765 74 20 74 6f 20 6e 65 77 65 73 74 29 2c 20 77 69  t to newest), wi
32766 74 68 0a 2a 2a 20 65 78 68 61 75 73 74 65 64 20  th.** exhausted 
32767 72 65 61 64 65 72 73 20 74 6f 20 74 68 65 20 65  readers to the e
32768 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nd..*/.static in
32769 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  t optLeavesReade
3276a 72 43 6d 70 28 4f 70 74 4c 65 61 76 65 73 52 65  rCmp(OptLeavesRe
3276b 61 64 65 72 20 2a 6c 72 31 2c 20 4f 70 74 4c 65  ader *lr1, OptLe
3276c 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32 29  avesReader *lr2)
3276d 7b 0a 20 20 69 6e 74 20 63 20 3d 20 6f 70 74 4c  {.  int c = optL
3276e 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 43  eavesReaderTermC
3276f 6d 70 28 6c 72 31 2c 20 6c 72 32 29 3b 0a 20 20  mp(lr1, lr2);.  
32770 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75 72  if( c!=0 ) retur
32771 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20 6c 72  n c;.  return lr
32772 31 2d 3e 73 65 67 6d 65 6e 74 2d 6c 72 32 2d 3e  1->segment-lr2->
32773 73 65 67 6d 65 6e 74 3b 0a 7d 0a 2f 2a 20 42 75  segment;.}./* Bu
32774 62 62 6c 65 20 70 4c 72 5b 30 5d 20 74 6f 20 61  bble pLr[0] to a
32775 70 70 72 6f 70 72 69 61 74 65 20 70 6c 61 63 65  ppropriate place
32776 20 69 6e 20 70 4c 72 5b 31 2e 2e 6e 4c 72 2d 31   in pLr[1..nLr-1
32777 5d 2e 20 20 41 73 73 75 6d 65 73 20 74 68 61 74  ].  Assumes that
32778 0a 2a 2a 20 70 4c 72 5b 31 2e 2e 6e 4c 72 2d 31  .** pLr[1..nLr-1
32779 5d 20 69 73 20 61 6c 72 65 61 64 79 20 73 6f 72  ] is already sor
3277a 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ted..*/.static v
3277b 6f 69 64 20 6f 70 74 4c 65 61 76 65 73 52 65 61  oid optLeavesRea
3277c 64 65 72 52 65 6f 72 64 65 72 28 4f 70 74 4c 65  derReorder(OptLe
3277d 61 76 65 73 52 65 61 64 65 72 20 2a 70 4c 72 2c  avesReader *pLr,
3277e 20 69 6e 74 20 6e 4c 72 29 7b 0a 20 20 77 68 69   int nLr){.  whi
3277f 6c 65 28 20 6e 4c 72 3e 31 20 26 26 20 6f 70 74  le( nLr>1 && opt
32780 4c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70 28  LeavesReaderCmp(
32781 70 4c 72 2c 20 70 4c 72 2b 31 29 3e 30 20 29 7b  pLr, pLr+1)>0 ){
32782 0a 20 20 20 20 4f 70 74 4c 65 61 76 65 73 52 65  .    OptLeavesRe
32783 61 64 65 72 20 74 6d 70 20 3d 20 70 4c 72 5b 30  ader tmp = pLr[0
32784 5d 3b 0a 20 20 20 20 70 4c 72 5b 30 5d 20 3d 20  ];.    pLr[0] = 
32785 70 4c 72 5b 31 5d 3b 0a 20 20 20 20 70 4c 72 5b  pLr[1];.    pLr[
32786 31 5d 20 3d 20 74 6d 70 3b 0a 20 20 20 20 6e 4c  1] = tmp;.    nL
32787 72 2d 2d 3b 0a 20 20 20 20 70 4c 72 2b 2b 3b 0a  r--;.    pLr++;.
32788 20 20 7d 0a 7d 0a 0a 2f 2a 20 6f 70 74 69 6d 69    }.}../* optimi
32789 7a 65 28 29 20 68 65 6c 70 65 72 20 66 75 6e 63  ze() helper func
3278a 74 69 6f 6e 2e 20 20 50 75 74 20 74 68 65 20 72  tion.  Put the r
3278b 65 61 64 65 72 73 20 69 6e 20 6f 72 64 65 72 20  eaders in order 
3278c 61 6e 64 20 69 74 65 72 61 74 65 0a 2a 2a 20 74  and iterate.** t
3278d 68 72 6f 75 67 68 20 74 68 65 6d 2c 20 6d 65 72  hrough them, mer
3278e 67 69 6e 67 20 64 6f 63 6c 69 73 74 73 20 66 6f  ging doclists fo
3278f 72 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 73  r matching terms
32790 20 69 6e 74 6f 20 70 57 72 69 74 65 72 2e 0a 2a   into pWriter..*
32791 2a 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45  * Returns SQLITE
32792 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c 20  _OK on success, 
32793 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20 65 72  or the SQLite er
32794 72 6f 72 20 63 6f 64 65 20 77 68 69 63 68 0a 2a  ror code which.*
32795 2a 20 70 72 65 76 65 6e 74 65 64 20 73 75 63 63  * prevented succ
32796 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ess..*/.static i
32797 6e 74 20 6f 70 74 69 6d 69 7a 65 49 6e 74 65 72  nt optimizeInter
32798 6e 61 6c 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  nal(fulltext_vta
32799 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20 20  b *v,.          
3279a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3279b 20 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65    OptLeavesReade
3279c 72 20 2a 72 65 61 64 65 72 73 2c 20 69 6e 74 20  r *readers, int 
3279d 6e 52 65 61 64 65 72 73 2c 0a 20 20 20 20 20 20  nReaders,.      
3279e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3279f 20 20 20 20 20 20 4c 65 61 66 57 72 69 74 65 72        LeafWriter
327a0 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69 6e   *pWriter){.  in
327a1 74 20 69 2c 20 72 63 20 3d 20 53 51 4c 49 54 45  t i, rc = SQLITE
327a2 5f 4f 4b 3b 0a 20 20 44 61 74 61 42 75 66 66 65  _OK;.  DataBuffe
327a3 72 20 64 6f 63 6c 69 73 74 2c 20 6d 65 72 67 65  r doclist, merge
327a4 64 2c 20 74 6d 70 3b 0a 0a 20 20 2f 2a 20 4f 72  d, tmp;..  /* Or
327a5 64 65 72 20 74 68 65 20 72 65 61 64 65 72 73 2e  der the readers.
327a6 20 2a 2f 0a 20 20 69 20 3d 20 6e 52 65 61 64 65   */.  i = nReade
327a7 72 73 3b 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d  rs;.  while( i--
327a8 20 3e 20 30 20 29 7b 0a 20 20 20 20 6f 70 74 4c   > 0 ){.    optL
327a9 65 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64  eavesReaderReord
327aa 65 72 28 26 72 65 61 64 65 72 73 5b 69 5d 2c 20  er(&readers[i], 
327ab 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20 7d  nReaders-i);.  }
327ac 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ..  dataBufferIn
327ad 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 4c 45 41  it(&doclist, LEA
327ae 46 5f 4d 41 58 29 3b 0a 20 20 64 61 74 61 42 75  F_MAX);.  dataBu
327af 66 66 65 72 49 6e 69 74 28 26 6d 65 72 67 65 64  fferInit(&merged
327b0 2c 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 0a 20 20  , LEAF_MAX);..  
327b1 2f 2a 20 45 78 68 61 75 73 74 65 64 20 72 65 61  /* Exhausted rea
327b2 64 65 72 73 20 62 75 62 62 6c 65 20 74 6f 20 74  ders bubble to t
327b3 68 65 20 65 6e 64 2c 20 73 6f 20 77 68 65 6e 20  he end, so when 
327b4 74 68 65 20 66 69 72 73 74 20 72 65 61 64 65 72  the first reader
327b5 20 69 73 0a 20 20 2a 2a 20 61 74 20 65 6f 66 2c   is.  ** at eof,
327b6 20 61 6c 6c 20 61 72 65 20 61 74 20 65 6f 66 2e   all are at eof.
327b7 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21  .  */.  while( !
327b8 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 41  optLeavesReaderA
327b9 74 45 6e 64 28 26 72 65 61 64 65 72 73 5b 30 5d  tEnd(&readers[0]
327ba 29 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 46 69 67  ) ){..    /* Fig
327bb 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
327bc 20 72 65 61 64 65 72 73 20 73 68 61 72 65 20 74   readers share t
327bd 68 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a 2f  he next term. */
327be 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  .    for(i=1; i<
327bf 6e 52 65 61 64 65 72 73 20 26 26 20 21 6f 70 74  nReaders && !opt
327c0 4c 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e  LeavesReaderAtEn
327c1 64 28 26 72 65 61 64 65 72 73 5b 69 5d 29 3b 20  d(&readers[i]); 
327c2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
327c3 30 21 3d 6f 70 74 4c 65 61 76 65 73 52 65 61 64  0!=optLeavesRead
327c4 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64 65  erTermCmp(&reade
327c5 72 73 5b 30 5d 2c 20 26 72 65 61 64 65 72 73 5b  rs[0], &readers[
327c6 69 5d 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  i]) ) break;.   
327c7 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 70 65 63 69   }..    /* Speci
327c8 61 6c 2d 63 61 73 65 20 66 6f 72 20 6e 6f 20 6d  al-case for no m
327c9 65 72 67 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28  erge. */.    if(
327ca 20 69 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 2f   i==1 ){.      /
327cb 2a 20 54 72 69 6d 20 64 65 6c 65 74 69 6f 6e 73  * Trim deletions
327cc 20 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c 69 73   from the doclis
327cd 74 2e 20 2a 2f 0a 20 20 20 20 20 20 64 61 74 61  t. */.      data
327ce 42 75 66 66 65 72 52 65 73 65 74 28 26 6d 65 72  BufferReset(&mer
327cf 67 65 64 29 3b 0a 20 20 20 20 20 20 64 6f 63 4c  ged);.      docL
327d0 69 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41 55  istTrim(DL_DEFAU
327d1 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LT,.            
327d2 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52        optLeavesR
327d3 65 61 64 65 72 44 61 74 61 28 26 72 65 61 64 65  eaderData(&reade
327d4 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20  rs[0]),.        
327d5 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61            optLea
327d6 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
327d7 65 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29 2c  es(&readers[0]),
327d8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
327d9 20 20 20 2d 31 2c 20 44 4c 5f 44 45 46 41 55 4c     -1, DL_DEFAUL
327da 54 2c 20 26 6d 65 72 67 65 64 29 3b 0a 20 20 20  T, &merged);.   
327db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 44 4c   }else{.      DL
327dc 52 65 61 64 65 72 20 64 6c 52 65 61 64 65 72 73  Reader dlReaders
327dd 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20  [MERGE_COUNT];. 
327de 20 20 20 20 20 69 6e 74 20 69 52 65 61 64 65 72       int iReader
327df 2c 20 6e 52 65 61 64 65 72 73 3b 0a 0a 20 20 20  , nReaders;..   
327e0 20 20 20 2f 2a 20 50 72 69 6d 65 20 74 68 65 20     /* Prime the 
327e1 70 69 70 65 6c 69 6e 65 20 77 69 74 68 20 74 68  pipeline with th
327e2 65 20 66 69 72 73 74 20 72 65 61 64 65 72 27 73  e first reader's
327e3 20 64 6f 63 6c 69 73 74 2e 20 20 41 66 74 65 72   doclist.  After
327e4 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 65 20 70 61  .      ** one pa
327e5 73 73 20 69 6e 64 65 78 20 30 20 77 69 6c 6c 20  ss index 0 will 
327e6 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 61 63  reference the ac
327e7 63 75 6d 75 6c 61 74 65 64 20 64 6f 63 6c 69 73  cumulated doclis
327e8 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  t..      */.    
327e9 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65 61    dlrInit(&dlRea
327ea 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46 41  ders[0], DL_DEFA
327eb 55 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ULT,.           
327ec 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64     optLeavesRead
327ed 65 72 44 61 74 61 28 26 72 65 61 64 65 72 73 5b  erData(&readers[
327ee 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0]),.           
327ef 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64     optLeavesRead
327f0 65 72 44 61 74 61 42 79 74 65 73 28 26 72 65 61  erDataBytes(&rea
327f1 64 65 72 73 5b 30 5d 29 29 3b 0a 20 20 20 20 20  ders[0]));.     
327f2 20 69 52 65 61 64 65 72 20 3d 20 31 3b 0a 0a 20   iReader = 1;.. 
327f3 20 20 20 20 20 61 73 73 65 72 74 28 20 69 52 65       assert( iRe
327f4 61 64 65 72 3c 69 20 29 3b 20 20 2f 2a 20 4d 75  ader<i );  /* Mu
327f5 73 74 20 65 78 65 63 75 74 65 20 74 68 65 20 6c  st execute the l
327f6 6f 6f 70 20 61 74 20 6c 65 61 73 74 20 6f 6e 63  oop at least onc
327f7 65 2e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c  e. */.      whil
327f8 65 28 20 69 52 65 61 64 65 72 3c 69 20 29 7b 0a  e( iReader<i ){.
327f9 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 72 67 65          /* Merge
327fa 20 31 36 20 69 6e 70 75 74 73 20 70 65 72 20 70   16 inputs per p
327fb 61 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ass. */.        
327fc 66 6f 72 28 20 6e 52 65 61 64 65 72 73 3d 31 3b  for( nReaders=1;
327fd 20 69 52 65 61 64 65 72 3c 69 20 26 26 20 6e 52   iReader<i && nR
327fe 65 61 64 65 72 73 3c 4d 45 52 47 45 5f 43 4f 55  eaders<MERGE_COU
327ff 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  NT;.            
32800 20 69 52 65 61 64 65 72 2b 2b 2c 20 6e 52 65 61   iReader++, nRea
32801 64 65 72 73 2b 2b 20 29 7b 0a 20 20 20 20 20 20  ders++ ){.      
32802 20 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52      dlrInit(&dlR
32803 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72 73 5d  eaders[nReaders]
32804 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20  , DL_DEFAULT,.  
32805 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32806 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
32807 61 74 61 28 26 72 65 61 64 65 72 73 5b 69 52 65  ata(&readers[iRe
32808 61 64 65 72 5d 29 2c 0a 20 20 20 20 20 20 20 20  ader]),.        
32809 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61            optLea
3280a 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
3280b 65 73 28 26 72 65 61 64 65 72 73 5b 69 52 65 61  es(&readers[iRea
3280c 64 65 72 5d 29 29 3b 0a 20 20 20 20 20 20 20 20  der]));.        
3280d 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 65  }..        /* Me
3280e 72 67 65 20 64 6f 63 6c 69 73 74 73 20 61 6e 64  rge doclists and
3280f 20 73 77 61 70 20 72 65 73 75 6c 74 20 69 6e 74   swap result int
32810 6f 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e 20 2a  o accumulator. *
32811 2f 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75  /.        dataBu
32812 66 66 65 72 52 65 73 65 74 28 26 6d 65 72 67 65  fferReset(&merge
32813 64 29 3b 0a 20 20 20 20 20 20 20 20 64 6f 63 4c  d);.        docL
32814 69 73 74 4d 65 72 67 65 28 26 6d 65 72 67 65 64  istMerge(&merged
32815 2c 20 64 6c 52 65 61 64 65 72 73 2c 20 6e 52 65  , dlReaders, nRe
32816 61 64 65 72 73 29 3b 0a 20 20 20 20 20 20 20 20  aders);.        
32817 74 6d 70 20 3d 20 6d 65 72 67 65 64 3b 0a 20 20  tmp = merged;.  
32818 20 20 20 20 20 20 6d 65 72 67 65 64 20 3d 20 64        merged = d
32819 6f 63 6c 69 73 74 3b 0a 20 20 20 20 20 20 20 20  oclist;.        
3281a 64 6f 63 6c 69 73 74 20 3d 20 74 6d 70 3b 0a 0a  doclist = tmp;..
3281b 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e          while( n
3281c 52 65 61 64 65 72 73 2d 2d 20 3e 20 30 20 29 7b  Readers-- > 0 ){
3281d 0a 20 20 20 20 20 20 20 20 20 20 64 6c 72 44 65  .          dlrDe
3281e 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65 72 73  stroy(&dlReaders
3281f 5b 6e 52 65 61 64 65 72 73 5d 29 3b 0a 20 20 20  [nReaders]);.   
32820 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
32821 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 64 20 64  /* Accumulated d
32822 6f 63 6c 69 73 74 20 74 6f 20 72 65 61 64 65 72  oclist to reader
32823 20 30 20 66 6f 72 20 6e 65 78 74 20 70 61 73 73   0 for next pass
32824 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 6c 72  . */.        dlr
32825 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72 73 5b  Init(&dlReaders[
32826 30 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  0], DL_DEFAULT, 
32827 64 6f 63 6c 69 73 74 2e 70 44 61 74 61 2c 20 64  doclist.pData, d
32828 6f 63 6c 69 73 74 2e 6e 44 61 74 61 29 3b 0a 20  oclist.nData);. 
32829 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
3282a 20 44 65 73 74 72 6f 79 20 72 65 61 64 65 72 20   Destroy reader 
3282b 74 68 61 74 20 77 61 73 20 6c 65 66 74 20 69 6e  that was left in
3282c 20 74 68 65 20 70 69 70 65 6c 69 6e 65 2e 20 2a   the pipeline. *
3282d 2f 0a 20 20 20 20 20 20 64 6c 72 44 65 73 74 72  /.      dlrDestr
3282e 6f 79 28 26 64 6c 52 65 61 64 65 72 73 5b 30 5d  oy(&dlReaders[0]
3282f 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 72 69  );..      /* Tri
32830 6d 20 64 65 6c 65 74 69 6f 6e 73 20 66 72 6f 6d  m deletions from
32831 20 74 68 65 20 64 6f 63 6c 69 73 74 2e 20 2a 2f   the doclist. */
32832 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65  .      dataBuffe
32833 72 52 65 73 65 74 28 26 6d 65 72 67 65 64 29 3b  rReset(&merged);
32834 0a 20 20 20 20 20 20 64 6f 63 4c 69 73 74 54 72  .      docListTr
32835 69 6d 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 64  im(DL_DEFAULT, d
32836 6f 63 6c 69 73 74 2e 70 44 61 74 61 2c 20 64 6f  oclist.pData, do
32837 63 6c 69 73 74 2e 6e 44 61 74 61 2c 0a 20 20 20  clist.nData,.   
32838 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
32839 31 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 26  1, DL_DEFAULT, &
3283a 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 7d 0a 0a  merged);.    }..
3283b 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 61 73 73      /* Only pass
3283c 20 64 6f 63 6c 69 73 74 73 20 77 69 74 68 20 68   doclists with h
3283d 69 74 73 20 28 73 6b 69 70 20 69 66 20 61 6c 6c  its (skip if all
3283e 20 68 69 74 73 20 64 65 6c 65 74 65 64 29 2e 20   hits deleted). 
3283f 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 72 67 65  */.    if( merge
32840 64 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20  d.nData>0 ){.   
32841 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74     rc = leafWrit
32842 65 72 53 74 65 70 28 76 2c 20 70 57 72 69 74 65  erStep(v, pWrite
32843 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
32844 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 74               opt
32845 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  LeavesReaderTerm
32846 28 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a 20  (&readers[0]),. 
32847 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32848 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76           optLeav
32849 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  esReaderTermByte
3284a 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a  s(&readers[0]),.
3284b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3284c 20 20 20 20 20 20 20 20 20 20 6d 65 72 67 65 64            merged
3284d 2e 70 44 61 74 61 2c 20 6d 65 72 67 65 64 2e 6e  .pData, merged.n
3284e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 69 66 28  Data);.      if(
3284f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32850 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 20 20 7d   goto err;.    }
32851 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20 6d 65  ..    /* Step me
32852 72 67 65 64 20 72 65 61 64 65 72 73 20 74 6f 20  rged readers to 
32853 6e 65 78 74 20 74 65 72 6d 20 61 6e 64 20 72 65  next term and re
32854 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 77 68  order. */.    wh
32855 69 6c 65 28 20 69 2d 2d 20 3e 20 30 20 29 7b 0a  ile( i-- > 0 ){.
32856 20 20 20 20 20 20 72 63 20 3d 20 6f 70 74 4c 65        rc = optLe
32857 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76  avesReaderStep(v
32858 2c 20 26 72 65 61 64 65 72 73 5b 69 5d 29 3b 0a  , &readers[i]);.
32859 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
3285a 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
3285b 72 72 3b 0a 0a 20 20 20 20 20 20 6f 70 74 4c 65  rr;..      optLe
3285c 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
3285d 72 28 26 72 65 61 64 65 72 73 5b 69 5d 2c 20 6e  r(&readers[i], n
3285e 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20 20 20  Readers-i);.    
3285f 7d 0a 20 20 7d 0a 0a 20 65 72 72 3a 0a 20 20 64  }.  }.. err:.  d
32860 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
32861 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 64 61  (&doclist);.  da
32862 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
32863 26 6d 65 72 67 65 64 29 3b 0a 20 20 72 65 74 75  &merged);.  retu
32864 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70  rn rc;.}../* Imp
32865 6c 65 6d 65 6e 74 20 6f 70 74 69 6d 69 7a 65 28  lement optimize(
32866 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46  ) function for F
32867 54 53 33 2e 20 20 6f 70 74 69 6d 69 7a 65 28 74  TS3.  optimize(t
32868 29 20 6d 65 72 67 65 73 20 61 6c 6c 0a 2a 2a 20  ) merges all.** 
32869 73 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  segments in the 
3286a 66 74 73 20 69 6e 64 65 78 20 69 6e 74 6f 20 61  fts index into a
3286b 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74 2e   single segment.
3286c 20 20 27 74 27 20 69 73 20 74 68 65 20 6d 61 67    't' is the mag
3286d 69 63 0a 2a 2a 20 74 61 62 6c 65 2d 6e 61 6d 65  ic.** table-name
3286e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61  d column..*/.sta
3286f 74 69 63 20 76 6f 69 64 20 6f 70 74 69 6d 69 7a  tic void optimiz
32870 65 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f  eFunc(sqlite3_co
32871 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c  ntext *pContext,
32872 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32873 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 72            int ar
32874 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
32875 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 66 75 6c  e **argv){.  ful
32876 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70 43  ltext_cursor *pC
32877 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61 72 67  ursor;.  if( arg
32878 63 3e 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  c>1 ){.    sqlit
32879 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
3287a 70 43 6f 6e 74 65 78 74 2c 20 22 65 78 63 65 73  pContext, "exces
3287b 73 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 6f  s arguments to o
3287c 70 74 69 6d 69 7a 65 28 29 22 2c 2d 31 29 3b 0a  ptimize()",-1);.
3287d 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
3287e 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
3287f 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f  rgv[0])!=SQLITE_
32880 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20 20  BLOB ||.        
32881 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
32882 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
32883 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72  !=sizeof(pCursor
32884 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
32885 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43  _result_error(pC
32886 6f 6e 74 65 78 74 2c 20 22 69 6c 6c 65 67 61 6c  ontext, "illegal
32887 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
32888 74 6f 20 6f 70 74 69 6d 69 7a 65 22 2c 2d 31 29  to optimize",-1)
32889 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66  ;.  }else{.    f
3288a 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 3b  ulltext_vtab *v;
3288b 0a 20 20 20 20 69 6e 74 20 69 2c 20 72 63 2c 20  .    int i, rc, 
3288c 69 4d 61 78 4c 65 76 65 6c 3b 0a 20 20 20 20 4f  iMaxLevel;.    O
3288d 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
3288e 72 65 61 64 65 72 73 3b 0a 20 20 20 20 69 6e 74  readers;.    int
3288f 20 6e 52 65 61 64 65 72 73 3b 0a 20 20 20 20 4c   nReaders;.    L
32890 65 61 66 57 72 69 74 65 72 20 77 72 69 74 65 72  eafWriter writer
32891 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74  ;.    sqlite3_st
32892 6d 74 20 2a 73 3b 0a 0a 20 20 20 20 6d 65 6d 63  mt *s;..    memc
32893 70 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c  py(&pCursor, sql
32894 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
32895 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66  argv[0]), sizeof
32896 28 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20  (pCursor));.    
32897 76 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28  v = cursor_vtab(
32898 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20 2f  pCursor);..    /
32899 2a 20 46 6c 75 73 68 20 61 6e 79 20 62 75 66 66  * Flush any buff
3289a 65 72 65 64 20 75 70 64 61 74 65 73 20 62 65 66  ered updates bef
3289b 6f 72 65 20 6f 70 74 69 6d 69 7a 69 6e 67 2e 20  ore optimizing. 
3289c 2a 2f 0a 20 20 20 20 72 63 20 3d 20 66 6c 75 73  */.    rc = flus
3289d 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29  hPendingTerms(v)
3289e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
3289f 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
328a0 72 72 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 65  rr;..    rc = se
328a1 67 64 69 72 5f 63 6f 75 6e 74 28 76 2c 20 26 6e  gdir_count(v, &n
328a2 52 65 61 64 65 72 73 2c 20 26 69 4d 61 78 4c 65  Readers, &iMaxLe
328a3 76 65 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63  vel);.    if( rc
328a4 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
328a5 74 6f 20 65 72 72 3b 0a 20 20 20 20 69 66 28 20  to err;.    if( 
328a6 6e 52 65 61 64 65 72 73 3d 3d 30 20 7c 7c 20 6e  nReaders==0 || n
328a7 52 65 61 64 65 72 73 3d 3d 31 20 29 7b 0a 20 20  Readers==1 ){.  
328a8 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
328a9 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74  lt_text(pContext
328aa 2c 20 22 49 6e 64 65 78 20 61 6c 72 65 61 64 79  , "Index already
328ab 20 6f 70 74 69 6d 61 6c 22 2c 20 2d 31 2c 0a 20   optimal", -1,. 
328ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328ad 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
328ae 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 72  STATIC);.      r
328af 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20  eturn;.    }..  
328b0 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73    rc = sql_get_s
328b1 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44  tatement(v, SEGD
328b2 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54  IR_SELECT_ALL_ST
328b3 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 69 66 28  MT, &s);.    if(
328b4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
328b5 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20   goto err;..    
328b6 72 65 61 64 65 72 73 20 3d 20 73 71 6c 69 74 65  readers = sqlite
328b7 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65 61 64 65 72  3_malloc(nReader
328b8 73 2a 73 69 7a 65 6f 66 28 72 65 61 64 65 72 73  s*sizeof(readers
328b9 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 72  [0]));.    if( r
328ba 65 61 64 65 72 73 3d 3d 4e 55 4c 4c 20 29 20 67  eaders==NULL ) g
328bb 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 2f 2a  oto err;..    /*
328bc 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 72 65   Note that there
328bd 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 65   will already be
328be 20 61 20 73 65 67 6d 65 6e 74 20 61 74 20 74 68   a segment at th
328bf 69 73 20 70 6f 73 69 74 69 6f 6e 0a 20 20 20 20  is position.    
328c0 2a 2a 20 75 6e 74 69 6c 20 77 65 20 63 61 6c 6c  ** until we call
328c1 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65 28 29   segdir_delete()
328c2 20 6f 6e 20 69 4d 61 78 4c 65 76 65 6c 2e 0a 20   on iMaxLevel.. 
328c3 20 20 20 2a 2f 0a 20 20 20 20 6c 65 61 66 57 72     */.    leafWr
328c4 69 74 65 72 49 6e 69 74 28 69 4d 61 78 4c 65 76  iterInit(iMaxLev
328c5 65 6c 2c 20 30 2c 20 26 77 72 69 74 65 72 29 3b  el, 0, &writer);
328c6 0a 0a 20 20 20 20 69 20 3d 20 30 3b 0a 20 20 20  ..    i = 0;.   
328c7 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73 71   while( (rc = sq
328c8 6c 69 74 65 33 5f 73 74 65 70 28 73 29 29 3d 3d  lite3_step(s))==
328c9 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
328ca 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
328cb 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74 65   iStart = sqlite
328cc 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73  3_column_int64(s
328cd 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
328ce 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 20 3d 20  te_int64 iEnd = 
328cf 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
328d0 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20  nt64(s, 1);.    
328d1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
328d2 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65  ootData = sqlite
328d3 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c  3_column_blob(s,
328d4 20 32 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e   2);.      int n
328d5 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74  RootData = sqlit
328d6 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
328d7 73 2c 20 32 29 3b 0a 0a 20 20 20 20 20 20 61 73  s, 2);..      as
328d8 73 65 72 74 28 20 69 3c 6e 52 65 61 64 65 72 73  sert( i<nReaders
328d9 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c   );.      rc = l
328da 65 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28  eavesReaderInit(
328db 76 2c 20 2d 31 2c 20 69 53 74 61 72 74 2c 20 69  v, -1, iStart, i
328dc 45 6e 64 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20  End, pRootData, 
328dd 6e 52 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20 20  nRootData,.     
328de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328df 20 20 20 20 20 20 20 26 72 65 61 64 65 72 73 5b         &readers[
328e0 69 5d 2e 72 65 61 64 65 72 29 3b 0a 20 20 20 20  i].reader);.    
328e1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
328e2 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20  _OK ) break;..  
328e3 20 20 20 20 72 65 61 64 65 72 73 5b 69 5d 2e 73      readers[i].s
328e4 65 67 6d 65 6e 74 20 3d 20 69 3b 0a 20 20 20 20  egment = i;.    
328e5 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20    i++;.    }..  
328e6 20 20 2f 2a 20 49 66 20 77 65 20 6d 61 6e 61 67    /* If we manag
328e7 65 64 20 74 6f 20 73 75 63 63 65 73 66 75 6c 6c  ed to succesfull
328e8 79 20 72 65 61 64 20 74 68 65 6d 20 61 6c 6c 2c  y read them all,
328e9 20 6f 70 74 69 6d 69 7a 65 20 74 68 65 6d 2e 20   optimize them. 
328ea 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
328eb 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
328ec 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e      assert( i==n
328ed 52 65 61 64 65 72 73 20 29 3b 0a 20 20 20 20 20  Readers );.     
328ee 20 72 63 20 3d 20 6f 70 74 69 6d 69 7a 65 49 6e   rc = optimizeIn
328ef 74 65 72 6e 61 6c 28 76 2c 20 72 65 61 64 65 72  ternal(v, reader
328f0 73 2c 20 6e 52 65 61 64 65 72 73 2c 20 26 77 72  s, nReaders, &wr
328f1 69 74 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  iter);.    }..  
328f2 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e 20 30    while( i-- > 0
328f3 20 29 7b 0a 20 20 20 20 20 20 6c 65 61 76 65 73   ){.      leaves
328f4 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26 72  ReaderDestroy(&r
328f5 65 61 64 65 72 73 5b 69 5d 2e 72 65 61 64 65 72  eaders[i].reader
328f6 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
328f7 69 74 65 33 5f 66 72 65 65 28 72 65 61 64 65 72  ite3_free(reader
328f8 73 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77  s);..    /* If w
328f9 65 27 76 65 20 73 75 63 63 65 73 73 66 75 6c 6c  e've successfull
328fa 79 20 67 6f 74 74 65 6e 20 74 6f 20 68 65 72 65  y gotten to here
328fb 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6f 6c 64  , delete the old
328fc 20 73 65 67 6d 65 6e 74 73 0a 20 20 20 20 2a 2a   segments.    **
328fd 20 61 6e 64 20 66 6c 75 73 68 20 74 68 65 20 69   and flush the i
328fe 6e 74 65 72 69 6f 72 20 73 74 72 75 63 74 75 72  nterior structur
328ff 65 20 6f 66 20 74 68 65 20 6e 65 77 20 73 65 67  e of the new seg
32900 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ment..    */.   
32901 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
32902 4f 4b 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  OK ){.      for(
32903 20 69 3d 30 3b 20 69 3c 3d 69 4d 61 78 4c 65 76   i=0; i<=iMaxLev
32904 65 6c 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20  el; i++ ){.     
32905 20 20 20 72 63 20 3d 20 73 65 67 64 69 72 5f 64     rc = segdir_d
32906 65 6c 65 74 65 28 76 2c 20 69 29 3b 0a 20 20 20  elete(v, i);.   
32907 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
32908 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a  ITE_OK ) break;.
32909 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
3290a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3290b 20 29 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74   ) rc = leafWrit
3290c 65 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77  erFinalize(v, &w
3290d 72 69 74 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20  riter);.    }.. 
3290e 20 20 20 6c 65 61 66 57 72 69 74 65 72 44 65 73     leafWriterDes
3290f 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 0a  troy(&writer);..
32910 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
32911 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
32912 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ;..    sqlite3_r
32913 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74  esult_text(pCont
32914 65 78 74 2c 20 22 49 6e 64 65 78 20 6f 70 74 69  ext, "Index opti
32915 6d 69 7a 65 64 22 2c 20 2d 31 2c 20 53 51 4c 49  mized", -1, SQLI
32916 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
32917 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20 2f 2a 20  return;..    /* 
32918 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 45 72 72  TODO(shess): Err
32919 6f 72 2d 68 61 6e 64 6c 69 6e 67 20 6e 65 65 64  or-handling need
3291a 73 20 74 6f 20 62 65 20 69 6d 70 72 6f 76 65 64  s to be improved
3291b 20 61 6c 6f 6e 67 20 74 68 65 0a 20 20 20 20 2a   along the.    *
3291c 2a 20 6c 69 6e 65 73 20 6f 66 20 74 68 65 20 64  * lines of the d
3291d 75 6d 70 5f 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  ump_ functions..
3291e 20 20 20 20 2a 2f 0a 20 65 72 72 3a 0a 20 20 20      */. err:.   
3291f 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 62 75   {.      char bu
32920 66 5b 35 31 32 5d 3b 0a 20 20 20 20 20 20 73 71  f[512];.      sq
32921 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
32922 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66 2c  izeof(buf), buf,
32923 20 22 45 72 72 6f 72 20 69 6e 20 6f 70 74 69 6d   "Error in optim
32924 69 7a 65 3a 20 25 73 22 2c 0a 20 20 20 20 20 20  ize: %s",.      
32925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32926 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
32927 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
32928 64 62 5f 68 61 6e 64 6c 65 28 70 43 6f 6e 74 65  db_handle(pConte
32929 78 74 29 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  xt)));.      sql
3292a 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
3292b 72 28 70 43 6f 6e 74 65 78 74 2c 20 62 75 66 2c  r(pContext, buf,
3292c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   -1);.    }.  }.
3292d 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
3292e 5f 54 45 53 54 0a 2f 2a 20 47 65 6e 65 72 61 74  _TEST./* Generat
3292f 65 20 61 6e 20 65 72 72 6f 72 20 6f 66 20 74 68  e an error of th
32930 65 20 66 6f 72 6d 20 22 3c 70 72 65 66 69 78 3e  e form "<prefix>
32931 3a 20 3c 6d 73 67 3e 22 2e 20 20 49 66 20 6d 73  : <msg>".  If ms
32932 67 20 69 73 20 4e 55 4c 4c 2c 0a 2a 2a 20 70 75  g is NULL,.** pu
32933 6c 6c 20 74 68 65 20 65 72 72 6f 72 20 66 72 6f  ll the error fro
32934 6d 20 74 68 65 20 63 6f 6e 74 65 78 74 27 73 20  m the context's 
32935 64 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74  db handle..*/.st
32936 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61  atic void genera
32937 74 65 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  teError(sqlite3_
32938 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  context *pContex
32939 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
3293a 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
3293b 73 74 20 63 68 61 72 20 2a 70 72 65 66 69 78 2c  st char *prefix,
3293c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6d 73 67   const char *msg
3293d 29 7b 0a 20 20 63 68 61 72 20 62 75 66 5b 35 31  ){.  char buf[51
3293e 32 5d 3b 0a 20 20 69 66 28 20 6d 73 67 3d 3d 4e  2];.  if( msg==N
3293f 55 4c 4c 20 29 20 6d 73 67 20 3d 20 73 71 6c 69  ULL ) msg = sqli
32940 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c 69 74  te3_errmsg(sqlit
32941 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
32942 6e 64 6c 65 28 70 43 6f 6e 74 65 78 74 29 29 3b  ndle(pContext));
32943 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
32944 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29 2c  ntf(sizeof(buf),
32945 20 62 75 66 2c 20 22 25 73 3a 20 25 73 22 2c 20   buf, "%s: %s", 
32946 70 72 65 66 69 78 2c 20 6d 73 67 29 3b 0a 20 20  prefix, msg);.  
32947 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
32948 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 62  rror(pContext, b
32949 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 20 48  uf, -1);.}../* H
3294a 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74  elper function t
3294b 6f 20 63 6f 6c 6c 65 63 74 20 74 68 65 20 73 65  o collect the se
3294c 74 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 68  t of terms in th
3294d 65 20 73 65 67 6d 65 6e 74 20 69 6e 74 6f 0a 2a  e segment into.*
3294e 2a 20 70 54 65 72 6d 73 2e 20 20 54 68 65 20 73  * pTerms.  The s
3294f 65 67 6d 65 6e 74 20 69 73 20 64 65 66 69 6e 65  egment is define
32950 64 20 62 79 20 74 68 65 20 6c 65 61 66 20 6e 6f  d by the leaf no
32951 64 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 69  des between.** i
32952 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 61 6e 64  StartBlockid and
32953 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20 69 6e   iEndBlockid, in
32954 63 6c 75 73 69 76 65 2c 20 6f 72 20 62 79 20 74  clusive, or by t
32955 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a  he contents of.*
32956 2a 20 70 52 6f 6f 74 44 61 74 61 20 69 66 20 69  * pRootData if i
32957 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 69 73 20  StartBlockid is 
32958 30 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65  0 (in which case
32959 20 74 68 65 20 65 6e 74 69 72 65 20 73 65 67 6d   the entire segm
3295a 65 6e 74 0a 2a 2a 20 66 69 74 20 69 6e 20 61 20  ent.** fit in a 
3295b 6c 65 61 66 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  leaf)..*/.static
3295c 20 69 6e 74 20 63 6f 6c 6c 65 63 74 53 65 67 6d   int collectSegm
3295d 65 6e 74 54 65 72 6d 73 28 66 75 6c 6c 74 65 78  entTerms(fulltex
3295e 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
3295f 65 33 5f 73 74 6d 74 20 2a 73 2c 0a 20 20 20 20  e3_stmt *s,.    
32960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32961 20 20 20 20 20 20 20 20 20 20 20 66 74 73 33 48             fts3H
32962 61 73 68 20 2a 70 54 65 72 6d 73 29 7b 0a 20 20  ash *pTerms){.  
32963 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69 6e 74  const sqlite_int
32964 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  64 iStartBlockid
32965 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
32966 6e 5f 69 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20  n_int64(s, 0);. 
32967 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69 6e   const sqlite_in
32968 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 20  t64 iEndBlockid 
32969 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
3296a 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20  _int64(s, 1);.  
3296b 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 6f 6f  const char *pRoo
3296c 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f  tData = sqlite3_
3296d 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32  column_blob(s, 2
3296e 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e  );.  const int n
3296f 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74  RootData = sqlit
32970 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
32971 73 2c 20 32 29 3b 0a 20 20 4c 65 61 76 65 73 52  s, 2);.  LeavesR
32972 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a 20 20  eader reader;.  
32973 69 6e 74 20 72 63 20 3d 20 6c 65 61 76 65 73 52  int rc = leavesR
32974 65 61 64 65 72 49 6e 69 74 28 76 2c 20 30 2c 20  eaderInit(v, 0, 
32975 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 69  iStartBlockid, i
32976 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  EndBlockid,.    
32977 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32978 20 20 20 20 20 20 20 20 70 52 6f 6f 74 44 61 74          pRootDat
32979 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 20 26 72  a, nRootData, &r
3297a 65 61 64 65 72 29 3b 0a 20 20 69 66 28 20 72 63  eader);.  if( rc
3297b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
3297c 74 75 72 6e 20 72 63 3b 0a 0a 20 20 77 68 69 6c  turn rc;..  whil
3297d 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
3297e 20 26 26 20 21 6c 65 61 76 65 73 52 65 61 64 65   && !leavesReade
3297f 72 41 74 45 6e 64 28 26 72 65 61 64 65 72 29 20  rAtEnd(&reader) 
32980 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
32981 72 20 2a 70 54 65 72 6d 20 3d 20 6c 65 61 76 65  r *pTerm = leave
32982 73 52 65 61 64 65 72 54 65 72 6d 28 26 72 65 61  sReaderTerm(&rea
32983 64 65 72 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20  der);.    const 
32984 69 6e 74 20 6e 54 65 72 6d 20 3d 20 6c 65 61 76  int nTerm = leav
32985 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  esReaderTermByte
32986 73 28 26 72 65 61 64 65 72 29 3b 0a 20 20 20 20  s(&reader);.    
32987 76 6f 69 64 20 2a 6f 6c 64 56 61 6c 75 65 20 3d  void *oldValue =
32988 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
32989 46 69 6e 64 28 70 54 65 72 6d 73 2c 20 70 54 65  Find(pTerms, pTe
3298a 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20  rm, nTerm);.    
3298b 76 6f 69 64 20 2a 6e 65 77 56 61 6c 75 65 20 3d  void *newValue =
3298c 20 28 76 6f 69 64 20 2a 29 28 28 63 68 61 72 20   (void *)((char 
3298d 2a 29 6f 6c 64 56 61 6c 75 65 2b 31 29 3b 0a 0a  *)oldValue+1);..
3298e 20 20 20 20 2f 2a 20 46 72 6f 6d 20 74 68 65 20      /* From the 
3298f 63 6f 6d 6d 65 6e 74 20 62 65 66 6f 72 65 20 73  comment before s
32990 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
32991 73 65 72 74 20 69 6e 20 66 74 73 33 5f 68 61 73  sert in fts3_has
32992 68 2e 63 2c 0a 20 20 20 20 2a 2a 20 74 68 65 20  h.c,.    ** the 
32993 64 61 74 61 20 76 61 6c 75 65 20 70 61 73 73 65  data value passe
32994 64 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  d is returned in
32995 20 63 61 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 20   case of malloc 
32996 66 61 69 6c 75 72 65 2e 0a 20 20 20 20 2a 2f 0a  failure..    */.
32997 20 20 20 20 69 66 28 20 6e 65 77 56 61 6c 75 65      if( newValue
32998 3d 3d 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  ==sqlite3Fts3Has
32999 68 49 6e 73 65 72 74 28 70 54 65 72 6d 73 2c 20  hInsert(pTerms, 
3299a 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 6e 65  pTerm, nTerm, ne
3299b 77 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20  wValue) ){.     
3299c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
3299d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  EM;.    }else{. 
3299e 20 20 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73       rc = leaves
3299f 52 65 61 64 65 72 53 74 65 70 28 76 2c 20 26 72  ReaderStep(v, &r
329a0 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  eader);.    }.  
329a1 7d 0a 0a 20 20 6c 65 61 76 65 73 52 65 61 64 65  }..  leavesReade
329a2 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65 72  rDestroy(&reader
329a3 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
329a4 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e  }../* Helper fun
329a5 63 74 69 6f 6e 20 74 6f 20 62 75 69 6c 64 20 74  ction to build t
329a6 68 65 20 72 65 73 75 6c 74 20 73 74 72 69 6e 67  he result string
329a7 20 66 6f 72 20 64 75 6d 70 5f 74 65 72 6d 73 28   for dump_terms(
329a8 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ). */.static int
329a9 20 67 65 6e 65 72 61 74 65 54 65 72 6d 73 52 65   generateTermsRe
329aa 73 75 6c 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  sult(sqlite3_con
329ab 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20  text *pContext, 
329ac 66 74 73 33 48 61 73 68 20 2a 70 54 65 72 6d 73  fts3Hash *pTerms
329ad 29 7b 0a 20 20 69 6e 74 20 69 54 65 72 6d 2c 20  ){.  int iTerm, 
329ae 6e 54 65 72 6d 73 2c 20 6e 52 65 73 75 6c 74 42  nTerms, nResultB
329af 79 74 65 73 2c 20 69 42 79 74 65 3b 0a 20 20 63  ytes, iByte;.  c
329b0 68 61 72 20 2a 72 65 73 75 6c 74 3b 0a 20 20 54  har *result;.  T
329b1 65 72 6d 44 61 74 61 20 2a 70 44 61 74 61 3b 0a  ermData *pData;.
329b2 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    fts3HashElem *
329b3 65 3b 0a 0a 20 20 2f 2a 20 49 74 65 72 61 74 65  e;..  /* Iterate
329b4 20 70 54 65 72 6d 73 20 74 6f 20 67 65 6e 65 72   pTerms to gener
329b5 61 74 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20  ate an array of 
329b6 74 65 72 6d 73 20 69 6e 20 70 44 61 74 61 20 66  terms in pData f
329b7 6f 72 0a 20 20 2a 2a 20 73 6f 72 74 69 6e 67 2e  or.  ** sorting.
329b8 0a 20 20 2a 2f 0a 20 20 6e 54 65 72 6d 73 20 3d  .  */.  nTerms =
329b9 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74 28 70   fts3HashCount(p
329ba 54 65 72 6d 73 29 3b 0a 20 20 61 73 73 65 72 74  Terms);.  assert
329bb 28 20 6e 54 65 72 6d 73 3e 30 20 29 3b 0a 20 20  ( nTerms>0 );.  
329bc 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f  pData = sqlite3_
329bd 6d 61 6c 6c 6f 63 28 6e 54 65 72 6d 73 2a 73 69  malloc(nTerms*si
329be 7a 65 6f 66 28 54 65 72 6d 44 61 74 61 29 29 3b  zeof(TermData));
329bf 0a 20 20 69 66 28 20 70 44 61 74 61 3d 3d 4e 55  .  if( pData==NU
329c0 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  LL ) return SQLI
329c1 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 6e 52 65  TE_NOMEM;..  nRe
329c2 73 75 6c 74 42 79 74 65 73 20 3d 20 30 3b 0a 20  sultBytes = 0;. 
329c3 20 66 6f 72 28 69 54 65 72 6d 20 3d 20 30 2c 20   for(iTerm = 0, 
329c4 65 20 3d 20 66 74 73 33 48 61 73 68 46 69 72 73  e = fts3HashFirs
329c5 74 28 70 54 65 72 6d 73 29 3b 20 65 3b 20 69 54  t(pTerms); e; iT
329c6 65 72 6d 2b 2b 2c 20 65 20 3d 20 66 74 73 33 48  erm++, e = fts3H
329c7 61 73 68 4e 65 78 74 28 65 29 29 7b 0a 20 20 20  ashNext(e)){.   
329c8 20 6e 52 65 73 75 6c 74 42 79 74 65 73 20 2b 3d   nResultBytes +=
329c9 20 66 74 73 33 48 61 73 68 4b 65 79 73 69 7a 65   fts3HashKeysize
329ca 28 65 29 2b 31 3b 20 20 20 2f 2a 20 54 65 72 6d  (e)+1;   /* Term
329cb 20 70 6c 75 73 20 74 72 61 69 6c 69 6e 67 20 73   plus trailing s
329cc 70 61 63 65 20 2a 2f 0a 20 20 20 20 61 73 73 65  pace */.    asse
329cd 72 74 28 20 69 54 65 72 6d 3c 6e 54 65 72 6d 73  rt( iTerm<nTerms
329ce 20 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 54   );.    pData[iT
329cf 65 72 6d 5d 2e 70 54 65 72 6d 20 3d 20 66 74 73  erm].pTerm = fts
329d0 33 48 61 73 68 4b 65 79 28 65 29 3b 0a 20 20 20  3HashKey(e);.   
329d1 20 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e 6e 54   pData[iTerm].nT
329d2 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68 4b 65  erm = fts3HashKe
329d3 79 73 69 7a 65 28 65 29 3b 0a 20 20 20 20 70 44  ysize(e);.    pD
329d4 61 74 61 5b 69 54 65 72 6d 5d 2e 70 43 6f 6c 6c  ata[iTerm].pColl
329d5 65 63 74 6f 72 20 3d 20 66 74 73 33 48 61 73 68  ector = fts3Hash
329d6 44 61 74 61 28 65 29 3b 20 20 2f 2a 20 75 6e 75  Data(e);  /* unu
329d7 73 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73  sed */.  }.  ass
329d8 65 72 74 28 20 69 54 65 72 6d 3d 3d 6e 54 65 72  ert( iTerm==nTer
329d9 6d 73 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  ms );..  assert(
329da 20 6e 52 65 73 75 6c 74 42 79 74 65 73 3e 30 20   nResultBytes>0 
329db 29 3b 20 20 20 2f 2a 20 6e 54 65 72 6d 73 3e 30  );   /* nTerms>0
329dc 2c 20 6e 52 65 73 75 6c 74 73 42 79 74 65 73 20  , nResultsBytes 
329dd 6d 75 73 74 20 62 65 2c 20 74 6f 6f 2e 20 2a 2f  must be, too. */
329de 0a 20 20 72 65 73 75 6c 74 20 3d 20 73 71 6c 69  .  result = sqli
329df 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65 73 75  te3_malloc(nResu
329e0 6c 74 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20  ltBytes);.  if( 
329e1 72 65 73 75 6c 74 3d 3d 4e 55 4c 4c 20 29 7b 0a  result==NULL ){.
329e2 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
329e3 28 70 44 61 74 61 29 3b 0a 20 20 20 20 72 65 74  (pData);.    ret
329e4 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
329e5 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 54 65  ;.  }..  if( nTe
329e6 72 6d 73 3e 31 20 29 20 71 73 6f 72 74 28 70 44  rms>1 ) qsort(pD
329e7 61 74 61 2c 20 6e 54 65 72 6d 73 2c 20 73 69 7a  ata, nTerms, siz
329e8 65 6f 66 28 2a 70 44 61 74 61 29 2c 20 74 65 72  eof(*pData), ter
329e9 6d 44 61 74 61 43 6d 70 29 3b 0a 0a 20 20 2f 2a  mDataCmp);..  /*
329ea 20 52 65 61 64 20 74 68 65 20 74 65 72 6d 73 20   Read the terms 
329eb 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 75 69 6c  in order to buil
329ec 64 20 74 68 65 20 72 65 73 75 6c 74 2e 20 2a 2f  d the result. */
329ed 0a 20 20 69 42 79 74 65 20 3d 20 30 3b 0a 20 20  .  iByte = 0;.  
329ee 66 6f 72 28 69 54 65 72 6d 3d 30 3b 20 69 54 65  for(iTerm=0; iTe
329ef 72 6d 3c 6e 54 65 72 6d 73 3b 20 2b 2b 69 54 65  rm<nTerms; ++iTe
329f0 72 6d 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  rm){.    memcpy(
329f1 72 65 73 75 6c 74 2b 69 42 79 74 65 2c 20 70 44  result+iByte, pD
329f2 61 74 61 5b 69 54 65 72 6d 5d 2e 70 54 65 72 6d  ata[iTerm].pTerm
329f3 2c 20 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e 6e  , pData[iTerm].n
329f4 54 65 72 6d 29 3b 0a 20 20 20 20 69 42 79 74 65  Term);.    iByte
329f5 20 2b 3d 20 70 44 61 74 61 5b 69 54 65 72 6d 5d   += pData[iTerm]
329f6 2e 6e 54 65 72 6d 3b 0a 20 20 20 20 72 65 73 75  .nTerm;.    resu
329f7 6c 74 5b 69 42 79 74 65 2b 2b 5d 20 3d 20 27 20  lt[iByte++] = ' 
329f8 27 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  ';.  }.  assert(
329f9 20 69 42 79 74 65 3d 3d 6e 52 65 73 75 6c 74 42   iByte==nResultB
329fa 79 74 65 73 20 29 3b 0a 20 20 61 73 73 65 72 74  ytes );.  assert
329fb 28 20 72 65 73 75 6c 74 5b 6e 52 65 73 75 6c 74  ( result[nResult
329fc 42 79 74 65 73 2d 31 5d 3d 3d 27 20 27 20 29 3b  Bytes-1]==' ' );
329fd 0a 20 20 72 65 73 75 6c 74 5b 6e 52 65 73 75 6c  .  result[nResul
329fe 74 42 79 74 65 73 2d 31 5d 20 3d 20 27 5c 30 27  tBytes-1] = '\0'
329ff 3b 0a 0a 20 20 2f 2a 20 50 61 73 73 65 73 20 61  ;..  /* Passes a
32a00 77 61 79 20 6f 77 6e 65 72 73 68 69 70 20 6f 66  way ownership of
32a01 20 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 20 73 71   result. */.  sq
32a02 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
32a03 74 28 70 43 6f 6e 74 65 78 74 2c 20 72 65 73 75  t(pContext, resu
32a04 6c 74 2c 20 6e 52 65 73 75 6c 74 42 79 74 65 73  lt, nResultBytes
32a05 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65  -1, sqlite3_free
32a06 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
32a07 65 28 70 44 61 74 61 29 3b 0a 20 20 72 65 74 75  e(pData);.  retu
32a08 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
32a09 0a 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 73 20 64  ./* Implements d
32a0a 75 6d 70 5f 74 65 72 6d 73 28 29 20 66 6f 72 20  ump_terms() for 
32a0b 75 73 65 20 69 6e 20 69 6e 73 70 65 63 74 69 6e  use in inspectin
32a0c 67 20 74 68 65 20 66 74 73 33 20 69 6e 64 65 78  g the fts3 index
32a0d 20 66 72 6f 6d 0a 2a 2a 20 74 65 73 74 73 2e 20   from.** tests. 
32a0e 20 54 45 58 54 20 72 65 73 75 6c 74 20 63 6f 6e   TEXT result con
32a0f 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72 64 65  taining the orde
32a10 72 65 64 20 6c 69 73 74 20 6f 66 20 74 65 72 6d  red list of term
32a11 73 20 6a 6f 69 6e 65 64 20 62 79 0a 2a 2a 20 73  s joined by.** s
32a12 70 61 63 65 73 2e 20 20 64 75 6d 70 5f 74 65 72  paces.  dump_ter
32a13 6d 73 28 74 2c 20 6c 65 76 65 6c 2c 20 69 64 78  ms(t, level, idx
32a14 29 20 64 75 6d 70 73 20 74 68 65 20 74 65 72 6d  ) dumps the term
32a15 73 20 66 6f 72 20 74 68 65 20 73 65 67 6d 65 6e  s for the segmen
32a16 74 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62  t.** specified b
32a17 79 20 6c 65 76 65 6c 2c 20 69 64 78 20 28 69 6e  y level, idx (in
32a18 20 25 5f 73 65 67 64 69 72 29 2c 20 77 68 69 6c   %_segdir), whil
32a19 65 20 64 75 6d 70 5f 74 65 72 6d 73 28 74 29 20  e dump_terms(t) 
32a1a 64 75 6d 70 73 0a 2a 2a 20 61 6c 6c 20 74 65 72  dumps.** all ter
32a1b 6d 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e  ms in the index.
32a1c 20 20 49 6e 20 62 6f 74 68 20 63 61 73 65 73 20    In both cases 
32a1d 74 20 69 73 20 74 68 65 20 66 74 73 20 74 61 62  t is the fts tab
32a1e 6c 65 27 73 20 6d 61 67 69 63 0a 2a 2a 20 74 61  le's magic.** ta
32a1f 62 6c 65 2d 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e  ble-named column
32a20 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
32a21 20 64 75 6d 70 54 65 72 6d 73 46 75 6e 63 28 0a   dumpTermsFunc(.
32a22 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
32a23 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69  t *pContext,.  i
32a24 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
32a25 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
32a26 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
32a27 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69  or *pCursor;.  i
32a28 66 28 20 61 72 67 63 21 3d 33 20 26 26 20 61 72  f( argc!=3 && ar
32a29 67 63 21 3d 31 20 29 7b 0a 20 20 20 20 67 65 6e  gc!=1 ){.    gen
32a2a 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74  erateError(pCont
32a2b 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73  ext, "dump_terms
32a2c 22 2c 20 22 69 6e 63 6f 72 72 65 63 74 20 61 72  ", "incorrect ar
32a2d 67 75 6d 65 6e 74 73 22 29 3b 0a 20 20 7d 65 6c  guments");.  }el
32a2e 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  se if( sqlite3_v
32a2f 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
32a30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20  ])!=SQLITE_BLOB 
32a31 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ||.            s
32a32 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
32a33 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a  es(argv[0])!=siz
32a34 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a  eof(pCursor) ){.
32a35 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
32a36 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
32a37 70 5f 74 65 72 6d 73 22 2c 20 22 69 6c 6c 65 67  p_terms", "illeg
32a38 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  al first argumen
32a39 74 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  t");.  }else{.  
32a3a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
32a3b 2a 76 3b 0a 20 20 20 20 66 74 73 33 48 61 73 68  *v;.    fts3Hash
32a3c 20 74 65 72 6d 73 3b 0a 20 20 20 20 73 71 6c 69   terms;.    sqli
32a3d 74 65 33 5f 73 74 6d 74 20 2a 73 20 3d 20 4e 55  te3_stmt *s = NU
32a3e 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a  LL;.    int rc;.
32a3f 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75  .    memcpy(&pCu
32a40 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61  rsor, sqlite3_va
32a41 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
32a42 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  ), sizeof(pCurso
32a43 72 29 29 3b 0a 20 20 20 20 76 20 3d 20 63 75 72  r));.    v = cur
32a44 73 6f 72 5f 76 74 61 62 28 70 43 75 72 73 6f 72  sor_vtab(pCursor
32a45 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61  );..    /* If pa
32a46 73 73 65 64 20 6f 6e 6c 79 20 74 68 65 20 63 75  ssed only the cu
32a47 72 73 6f 72 20 63 6f 6c 75 6d 6e 2c 20 67 65 74  rsor column, get
32a48 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 2e 20 20   all segments.  
32a49 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20 2a 2a  Otherwise.    **
32a4a 20 67 65 74 20 74 68 65 20 73 65 67 6d 65 6e 74   get the segment
32a4b 20 64 65 73 63 72 69 62 65 64 20 62 79 20 74 68   described by th
32a4c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20  e following two 
32a4d 61 72 67 75 6d 65 6e 74 73 2e 0a 20 20 20 20 2a  arguments..    *
32a4e 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63 3d 3d  /.    if( argc==
32a4f 31 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  1 ){.      rc = 
32a50 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
32a51 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45  t(v, SEGDIR_SELE
32a52 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29  CT_ALL_STMT, &s)
32a53 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
32a54 20 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f     rc = sql_get_
32a55 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47  statement(v, SEG
32a56 44 49 52 5f 53 45 4c 45 43 54 5f 53 45 47 4d 45  DIR_SELECT_SEGME
32a57 4e 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  NT_STMT, &s);.  
32a58 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
32a59 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
32a5a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
32a5b 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 73 71 6c  nd_int(s, 1, sql
32a5c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
32a5d 72 67 76 5b 31 5d 29 29 3b 0a 20 20 20 20 20 20  rgv[1]));.      
32a5e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
32a5f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
32a60 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
32a61 6e 64 5f 69 6e 74 28 73 2c 20 32 2c 20 73 71 6c  nd_int(s, 2, sql
32a62 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
32a63 72 67 76 5b 32 5d 29 29 3b 0a 20 20 20 20 20 20  rgv[2]));.      
32a64 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
32a65 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  }..    if( rc!=S
32a66 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
32a67 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28    generateError(
32a68 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f  pContext, "dump_
32a69 74 65 72 6d 73 22 2c 20 4e 55 4c 4c 29 3b 0a 20  terms", NULL);. 
32a6a 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
32a6b 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6c 6c 65   }..    /* Colle
32a6c 63 74 20 74 68 65 20 74 65 72 6d 73 20 66 6f 72  ct the terms for
32a6d 20 65 61 63 68 20 73 65 67 6d 65 6e 74 2e 20 2a   each segment. *
32a6e 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  /.    sqlite3Fts
32a6f 33 48 61 73 68 49 6e 69 74 28 26 74 65 72 6d 73  3HashInit(&terms
32a70 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49  , FTS3_HASH_STRI
32a71 4e 47 2c 20 31 29 3b 0a 20 20 20 20 77 68 69 6c  NG, 1);.    whil
32a72 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  e( (rc = sqlite3
32a73 5f 73 74 65 70 28 73 29 29 3d 3d 53 51 4c 49 54  _step(s))==SQLIT
32a74 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 72  E_ROW ){.      r
32a75 63 20 3d 20 63 6f 6c 6c 65 63 74 53 65 67 6d 65  c = collectSegme
32a76 6e 74 54 65 72 6d 73 28 76 2c 20 73 2c 20 26 74  ntTerms(v, s, &t
32a77 65 72 6d 73 29 3b 0a 20 20 20 20 20 20 69 66 28  erms);.      if(
32a78 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32a79 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
32a7a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
32a7b 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  E_DONE ){.      
32a7c 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 29  sqlite3_reset(s)
32a7d 3b 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74 65  ;.      generate
32a7e 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
32a7f 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 4e 55  "dump_terms", NU
32a80 4c 4c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  LL);.    }else{.
32a81 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
32a82 6e 54 65 72 6d 73 20 3d 20 66 74 73 33 48 61 73  nTerms = fts3Has
32a83 68 43 6f 75 6e 74 28 26 74 65 72 6d 73 29 3b 0a  hCount(&terms);.
32a84 20 20 20 20 20 20 69 66 28 20 6e 54 65 72 6d 73        if( nTerms
32a85 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  >0 ){.        rc
32a86 20 3d 20 67 65 6e 65 72 61 74 65 54 65 72 6d 73   = generateTerms
32a87 52 65 73 75 6c 74 28 70 43 6f 6e 74 65 78 74 2c  Result(pContext,
32a88 20 26 74 65 72 6d 73 29 3b 0a 20 20 20 20 20 20   &terms);.      
32a89 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
32a8a 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
32a8b 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
32a8c 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
32a8d 70 5f 74 65 72 6d 73 22 2c 20 22 6f 75 74 20 6f  p_terms", "out o
32a8e 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20  f memory");.    
32a8f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
32a90 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d       assert( rc=
32a91 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
32a92 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
32a93 6c 73 65 20 69 66 28 20 61 72 67 63 3d 3d 33 20  lse if( argc==3 
32a94 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
32a95 65 20 73 70 65 63 69 66 69 63 20 73 65 67 6d 65  e specific segme
32a96 6e 74 20 61 73 6b 65 64 20 66 6f 72 20 63 6f 75  nt asked for cou
32a97 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e  ld not be found.
32a98 20 2a 2f 0a 20 20 20 20 20 20 20 20 67 65 6e 65   */.        gene
32a99 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
32a9a 78 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22  xt, "dump_terms"
32a9b 2c 20 22 73 65 67 6d 65 6e 74 20 6e 6f 74 20 66  , "segment not f
32a9c 6f 75 6e 64 22 29 3b 0a 20 20 20 20 20 20 7d 65  ound");.      }e
32a9d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
32a9e 4e 6f 20 73 65 67 6d 65 6e 74 73 20 66 6f 75 6e  No segments foun
32a9f 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 2f 2a  d. */.        /*
32aa0 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 49 74   TODO(shess): It
32aa1 20 73 68 6f 75 6c 64 20 62 65 20 69 6d 70 6f 73   should be impos
32aa2 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68 20 74  sible to reach t
32aa3 68 69 73 2e 20 20 54 68 69 73 0a 20 20 20 20 20  his.  This.     
32aa4 20 20 20 2a 2a 20 63 61 73 65 20 63 61 6e 20 6f     ** case can o
32aa5 6e 6c 79 20 68 61 70 70 65 6e 20 66 6f 72 20 61  nly happen for a
32aa6 6e 20 65 6d 70 74 79 20 74 61 62 6c 65 2c 20 69  n empty table, i
32aa7 6e 20 77 68 69 63 68 20 63 61 73 65 0a 20 20 20  n which case.   
32aa8 20 20 20 20 20 2a 2a 20 53 51 4c 69 74 65 20 68       ** SQLite h
32aa9 61 73 20 6e 6f 20 72 6f 77 73 20 74 6f 20 63 61  as no rows to ca
32aaa 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
32aab 20 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a   on..        */.
32aac 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32aad 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 70 43 6f 6e  result_null(pCon
32aae 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  text);.      }. 
32aaf 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
32ab0 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 26 74  Fts3HashClear(&t
32ab1 65 72 6d 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  erms);.  }.}../*
32ab2 20 45 78 70 61 6e 64 20 74 68 65 20 44 4c 5f 44   Expand the DL_D
32ab3 45 46 41 55 4c 54 20 64 6f 63 6c 69 73 74 20 69  EFAULT doclist i
32ab4 6e 20 70 44 61 74 61 20 69 6e 74 6f 20 61 20 74  n pData into a t
32ab5 65 78 74 20 72 65 73 75 6c 74 20 69 6e 0a 2a 2a  ext result in.**
32ab6 20 70 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 73 74   pContext..*/.st
32ab7 61 74 69 63 20 76 6f 69 64 20 63 72 65 61 74 65  atic void create
32ab8 44 6f 63 6c 69 73 74 52 65 73 75 6c 74 28 73 71  DoclistResult(sq
32ab9 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
32aba 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20  Context,.       
32abb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32abc 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
32abd 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
32abe 6e 44 61 74 61 29 7b 0a 20 20 44 61 74 61 42 75  nData){.  DataBu
32abf 66 66 65 72 20 64 75 6d 70 3b 0a 20 20 44 4c 52  ffer dump;.  DLR
32ac0 65 61 64 65 72 20 64 6c 52 65 61 64 65 72 3b 0a  eader dlReader;.
32ac1 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
32ac2 21 3d 4e 55 4c 4c 20 26 26 20 6e 44 61 74 61 3e  !=NULL && nData>
32ac3 30 20 29 3b 0a 0a 20 20 64 61 74 61 42 75 66 66  0 );..  dataBuff
32ac4 65 72 49 6e 69 74 28 26 64 75 6d 70 2c 20 30 29  erInit(&dump, 0)
32ac5 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52  ;.  dlrInit(&dlR
32ac6 65 61 64 65 72 2c 20 44 4c 5f 44 45 46 41 55 4c  eader, DL_DEFAUL
32ac7 54 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29  T, pData, nData)
32ac8 3b 0a 20 20 66 6f 72 28 20 3b 20 21 64 6c 72 41  ;.  for( ; !dlrA
32ac9 74 45 6e 64 28 26 64 6c 52 65 61 64 65 72 29 3b  tEnd(&dlReader);
32aca 20 64 6c 72 53 74 65 70 28 26 64 6c 52 65 61 64   dlrStep(&dlRead
32acb 65 72 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20  er) ){.    char 
32acc 62 75 66 5b 32 35 36 5d 3b 0a 20 20 20 20 50 4c  buf[256];.    PL
32acd 52 65 61 64 65 72 20 70 6c 52 65 61 64 65 72 3b  Reader plReader;
32ace 0a 0a 20 20 20 20 70 6c 72 49 6e 69 74 28 26 70  ..    plrInit(&p
32acf 6c 52 65 61 64 65 72 2c 20 26 64 6c 52 65 61 64  lReader, &dlRead
32ad0 65 72 29 3b 0a 20 20 20 20 69 66 28 20 44 4c 5f  er);.    if( DL_
32ad1 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 44 4f 43 49  DEFAULT==DL_DOCI
32ad2 44 53 20 7c 7c 20 70 6c 72 41 74 45 6e 64 28 26  DS || plrAtEnd(&
32ad3 70 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20  plReader) ){.   
32ad4 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
32ad5 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29 2c  ntf(sizeof(buf),
32ad6 20 62 75 66 2c 20 22 5b 25 6c 6c 64 5d 20 22 2c   buf, "[%lld] ",
32ad7 20 64 6c 72 44 6f 63 69 64 28 26 64 6c 52 65 61   dlrDocid(&dlRea
32ad8 64 65 72 29 29 3b 0a 20 20 20 20 20 20 64 61 74  der));.      dat
32ad9 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26 64  aBufferAppend(&d
32ada 75 6d 70 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e  ump, buf, strlen
32adb 28 62 75 66 29 29 3b 0a 20 20 20 20 7d 65 6c 73  (buf));.    }els
32adc 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f  e{.      int iCo
32add 6c 75 6d 6e 20 3d 20 70 6c 72 43 6f 6c 75 6d 6e  lumn = plrColumn
32ade 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 0a 20 20  (&plReader);..  
32adf 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
32ae0 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29  intf(sizeof(buf)
32ae1 2c 20 62 75 66 2c 20 22 5b 25 6c 6c 64 20 25 64  , buf, "[%lld %d
32ae2 5b 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  [",.            
32ae3 20 20 20 20 20 20 20 20 20 20 20 64 6c 72 44 6f             dlrDo
32ae4 63 69 64 28 26 64 6c 52 65 61 64 65 72 29 2c 20  cid(&dlReader), 
32ae5 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20  iColumn);.      
32ae6 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
32ae7 28 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72  (&dump, buf, str
32ae8 6c 65 6e 28 62 75 66 29 29 3b 0a 0a 20 20 20 20  len(buf));..    
32ae9 20 20 66 6f 72 28 20 3b 20 21 70 6c 72 41 74 45    for( ; !plrAtE
32aea 6e 64 28 26 70 6c 52 65 61 64 65 72 29 3b 20 70  nd(&plReader); p
32aeb 6c 72 53 74 65 70 28 26 70 6c 52 65 61 64 65 72  lrStep(&plReader
32aec 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ) ){.        if(
32aed 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65   plrColumn(&plRe
32aee 61 64 65 72 29 21 3d 69 43 6f 6c 75 6d 6e 20 29  ader)!=iColumn )
32aef 7b 0a 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c  {.          iCol
32af0 75 6d 6e 20 3d 20 70 6c 72 43 6f 6c 75 6d 6e 28  umn = plrColumn(
32af1 26 70 6c 52 65 61 64 65 72 29 3b 0a 20 20 20 20  &plReader);.    
32af2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
32af3 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75  printf(sizeof(bu
32af4 66 29 2c 20 62 75 66 2c 20 22 5d 20 25 64 5b 22  f), buf, "] %d["
32af5 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , iColumn);.    
32af6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 75        assert( du
32af7 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20  mp.nData>0 );.  
32af8 20 20 20 20 20 20 20 20 64 75 6d 70 2e 6e 44 61          dump.nDa
32af9 74 61 2d 2d 3b 20 20 20 20 20 20 20 20 20 20 20  ta--;           
32afa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 76 65            /* Ove
32afb 72 77 72 69 74 65 20 74 72 61 69 6c 69 6e 67 20  rwrite trailing 
32afc 73 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20 20 20  space. */.      
32afd 20 20 20 20 61 73 73 65 72 74 28 20 64 75 6d 70      assert( dump
32afe 2e 70 44 61 74 61 5b 64 75 6d 70 2e 6e 44 61 74  .pData[dump.nDat
32aff 61 5d 3d 3d 27 20 27 29 3b 0a 20 20 20 20 20 20  a]==' ');.      
32b00 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
32b01 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c  pend(&dump, buf,
32b02 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 20   strlen(buf));. 
32b03 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32b04 20 69 66 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d   if( DL_DEFAULT=
32b05 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46  =DL_POSITIONS_OF
32b06 46 53 45 54 53 20 29 7b 0a 20 20 20 20 20 20 20  FSETS ){.       
32b07 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
32b08 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29 2c  ntf(sizeof(buf),
32b09 20 62 75 66 2c 20 22 25 64 2c 25 64 2c 25 64 20   buf, "%d,%d,%d 
32b0a 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
32b0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
32b0c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52 65 61  rPosition(&plRea
32b0d 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20 20 20  der),.          
32b0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b0f 20 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28   plrStartOffset(
32b10 26 70 6c 52 65 61 64 65 72 29 2c 20 70 6c 72 45  &plReader), plrE
32b11 6e 64 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64  ndOffset(&plRead
32b12 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  er));.        }e
32b13 6c 73 65 20 69 66 28 20 44 4c 5f 44 45 46 41 55  lse if( DL_DEFAU
32b14 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  LT==DL_POSITIONS
32b15 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
32b16 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
32b17 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66 2c  izeof(buf), buf,
32b18 20 22 25 64 20 22 2c 20 70 6c 72 50 6f 73 69 74   "%d ", plrPosit
32b19 69 6f 6e 28 26 70 6c 52 65 61 64 65 72 29 29 3b  ion(&plReader));
32b1a 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
32b1b 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
32b1c 28 20 4e 55 4c 4c 3d 3d 22 55 6e 68 61 6e 64 6c  ( NULL=="Unhandl
32b1d 65 64 20 44 4c 5f 44 45 46 41 55 4c 54 20 76 61  ed DL_DEFAULT va
32b1e 6c 75 65 22 29 3b 0a 20 20 20 20 20 20 20 20 7d  lue");.        }
32b1f 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
32b20 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c  ferAppend(&dump,
32b21 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62 75 66   buf, strlen(buf
32b22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
32b23 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26 70 6c    plrDestroy(&pl
32b24 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 20 20  Reader);..      
32b25 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e 44 61  assert( dump.nDa
32b26 74 61 3e 30 20 29 3b 0a 20 20 20 20 20 20 64 75  ta>0 );.      du
32b27 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20 20  mp.nData--;     
32b28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b29 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74 72 61  /* Overwrite tra
32b2a 69 6c 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f 0a  iling space. */.
32b2b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 75        assert( du
32b2c 6d 70 2e 70 44 61 74 61 5b 64 75 6d 70 2e 6e 44  mp.pData[dump.nD
32b2d 61 74 61 5d 3d 3d 27 20 27 29 3b 0a 20 20 20 20  ata]==' ');.    
32b2e 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
32b2f 6e 64 28 26 64 75 6d 70 2c 20 22 5d 5d 20 22 2c  nd(&dump, "]] ",
32b30 20 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   3);.    }.  }. 
32b31 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52   dlrDestroy(&dlR
32b32 65 61 64 65 72 29 3b 0a 0a 20 20 61 73 73 65 72  eader);..  asser
32b33 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20  t( dump.nData>0 
32b34 29 3b 0a 20 20 64 75 6d 70 2e 6e 44 61 74 61 2d  );.  dump.nData-
32b35 2d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  -;              
32b36 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 77 72         /* Overwr
32b37 69 74 65 20 74 72 61 69 6c 69 6e 67 20 73 70 61  ite trailing spa
32b38 63 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ce. */.  assert(
32b39 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75 6d 70   dump.pData[dump
32b3a 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b 0a 20  .nData]==' ');. 
32b3b 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75 6d 70   dump.pData[dump
32b3c 2e 6e 44 61 74 61 5d 20 3d 20 27 5c 30 27 3b 0a  .nData] = '\0';.
32b3d 20 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e    assert( dump.n
32b3e 44 61 74 61 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20  Data>0 );..  /* 
32b3f 50 61 73 73 65 73 20 6f 77 6e 65 72 73 68 69 70  Passes ownership
32b40 20 6f 66 20 64 75 6d 70 27 73 20 62 75 66 66 65   of dump's buffe
32b41 72 20 74 6f 20 70 43 6f 6e 74 65 78 74 2e 20 2a  r to pContext. *
32b42 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  /.  sqlite3_resu
32b43 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74  lt_text(pContext
32b44 2c 20 64 75 6d 70 2e 70 44 61 74 61 2c 20 64 75  , dump.pData, du
32b45 6d 70 2e 6e 44 61 74 61 2c 20 73 71 6c 69 74 65  mp.nData, sqlite
32b46 33 5f 66 72 65 65 29 3b 0a 20 20 64 75 6d 70 2e  3_free);.  dump.
32b47 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20  pData = NULL;.  
32b48 64 75 6d 70 2e 6e 44 61 74 61 20 3d 20 64 75 6d  dump.nData = dum
32b49 70 2e 6e 43 61 70 61 63 69 74 79 20 3d 20 30 3b  p.nCapacity = 0;
32b4a 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74  .}../* Implement
32b4b 73 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74 28 29  s dump_doclist()
32b4c 20 66 6f 72 20 75 73 65 20 69 6e 20 69 6e 73 70   for use in insp
32b4d 65 63 74 69 6e 67 20 74 68 65 20 66 74 73 33 20  ecting the fts3 
32b4e 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 65  index from.** te
32b4f 73 74 73 2e 20 20 54 45 58 54 20 72 65 73 75 6c  sts.  TEXT resul
32b50 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 73  t containing a s
32b51 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
32b52 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 64  tion of the.** d
32b53 6f 63 6c 69 73 74 20 66 6f 72 20 74 68 65 20 69  oclist for the i
32b54 6e 64 69 63 61 74 65 64 20 74 65 72 6d 2e 20 20  ndicated term.  
32b55 64 75 6d 70 5f 64 6f 63 6c 69 73 74 28 74 2c 20  dump_doclist(t, 
32b56 74 65 72 6d 2c 20 6c 65 76 65 6c 2c 20 69 64 78  term, level, idx
32b57 29 0a 2a 2a 20 64 75 6d 70 73 20 74 68 65 20 64  ).** dumps the d
32b58 6f 63 6c 69 73 74 20 66 6f 72 20 74 65 72 6d 20  oclist for term 
32b59 66 72 6f 6d 20 74 68 65 20 73 65 67 6d 65 6e 74  from the segment
32b5a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 6c 65   specified by le
32b5b 76 65 6c 2c 20 69 64 78 0a 2a 2a 20 28 69 6e 20  vel, idx.** (in 
32b5c 25 5f 73 65 67 64 69 72 29 2c 20 77 68 69 6c 65  %_segdir), while
32b5d 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74 28 74 2c   dump_doclist(t,
32b5e 20 74 65 72 6d 29 20 64 75 6d 70 73 20 74 68 65   term) dumps the
32b5f 20 6c 6f 67 69 63 61 6c 0a 2a 2a 20 64 6f 63 6c   logical.** docl
32b60 69 73 74 20 66 6f 72 20 74 68 65 20 74 65 72 6d  ist for the term
32b61 20 61 63 72 6f 73 73 20 61 6c 6c 20 73 65 67 6d   across all segm
32b62 65 6e 74 73 2e 20 20 54 68 65 20 70 65 72 2d 73  ents.  The per-s
32b63 65 67 6d 65 6e 74 20 64 6f 63 6c 69 73 74 0a 2a  egment doclist.*
32b64 2a 20 63 61 6e 20 63 6f 6e 74 61 69 6e 20 64 65  * can contain de
32b65 6c 65 74 69 6f 6e 73 2c 20 77 68 69 6c 65 20 74  letions, while t
32b66 68 65 20 66 75 6c 6c 2d 69 6e 64 65 78 20 64 6f  he full-index do
32b67 63 6c 69 73 74 20 77 69 6c 6c 20 6e 6f 74 0a 2a  clist will not.*
32b68 2a 20 28 64 65 6c 65 74 69 6f 6e 73 20 61 72 65  * (deletions are
32b69 20 6f 6d 69 74 74 65 64 29 2e 0a 2a 2a 0a 2a 2a   omitted)..**.**
32b6a 20 52 65 73 75 6c 74 20 66 6f 72 6d 61 74 73 20   Result formats 
32b6b 64 69 66 66 65 72 20 77 69 74 68 20 74 68 65 20  differ with the 
32b6c 73 65 74 74 69 6e 67 20 6f 66 20 44 4c 5f 44 45  setting of DL_DE
32b6d 46 41 55 4c 54 53 2e 20 20 45 78 61 6d 70 6c 65  FAULTS.  Example
32b6e 73 3a 0a 2a 2a 0a 2a 2a 20 44 4c 5f 44 4f 43 49  s:.**.** DL_DOCI
32b6f 44 53 3a 20 5b 31 5d 20 5b 33 5d 20 5b 37 5d 0a  DS: [1] [3] [7].
32b70 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 3a  ** DL_POSITIONS:
32b71 20 5b 31 20 30 5b 30 20 34 5d 20 31 5b 31 37 5d   [1 0[0 4] 1[17]
32b72 5d 20 5b 33 20 31 5b 35 5d 5d 0a 2a 2a 20 44 4c  ] [3 1[5]].** DL
32b73 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45  _POSITIONS_OFFSE
32b74 54 53 3a 20 5b 31 20 30 5b 30 2c 30 2c 33 20 34  TS: [1 0[0,0,3 4
32b75 2c 32 33 2c 32 36 5d 20 31 5b 31 37 2c 31 30 32  ,23,26] 1[17,102
32b76 2c 31 30 35 5d 5d 20 5b 33 20 31 5b 35 2c 32 30  ,105]] [3 1[5,20
32b77 2c 32 33 5d 5d 0a 2a 2a 0a 2a 2a 20 49 6e 20 65  ,23]].**.** In e
32b78 61 63 68 20 63 61 73 65 20 74 68 65 20 6e 75 6d  ach case the num
32b79 62 65 72 20 61 66 74 65 72 20 74 68 65 20 6f 75  ber after the ou
32b7a 74 65 72 20 27 5b 27 20 69 73 20 74 68 65 20 64  ter '[' is the d
32b7b 6f 63 69 64 2e 20 20 49 6e 20 74 68 65 0a 2a 2a  ocid.  In the.**
32b7c 20 6c 61 74 74 65 72 20 74 77 6f 20 63 61 73 65   latter two case
32b7d 73 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 62 65  s, the number be
32b7e 66 6f 72 65 20 74 68 65 20 69 6e 6e 65 72 20 27  fore the inner '
32b7f 5b 27 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e  [' is the column
32b80 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77  .** associated w
32b81 69 74 68 20 74 68 65 20 76 61 6c 75 65 73 20 77  ith the values w
32b82 69 74 68 69 6e 2e 20 20 46 6f 72 20 44 4c 5f 50  ithin.  For DL_P
32b83 4f 53 49 54 49 4f 4e 53 20 74 68 65 20 6e 75 6d  OSITIONS the num
32b84 62 65 72 73 0a 2a 2a 20 77 69 74 68 69 6e 20 61  bers.** within a
32b85 72 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73  re the positions
32b86 2c 20 66 6f 72 20 44 4c 5f 50 4f 53 49 54 49 4f  , for DL_POSITIO
32b87 4e 53 5f 4f 46 46 53 45 54 53 20 74 68 65 79 20  NS_OFFSETS they 
32b88 61 72 65 20 74 68 65 0a 2a 2a 20 70 6f 73 69 74  are the.** posit
32b89 69 6f 6e 2c 20 74 68 65 20 73 74 61 72 74 20 6f  ion, the start o
32b8a 66 66 73 65 74 2c 20 61 6e 64 20 74 68 65 20 65  ffset, and the e
32b8b 6e 64 20 6f 66 66 73 65 74 2e 0a 2a 2f 0a 73 74  nd offset..*/.st
32b8c 61 74 69 63 20 76 6f 69 64 20 64 75 6d 70 44 6f  atic void dumpDo
32b8d 63 6c 69 73 74 46 75 6e 63 28 0a 20 20 73 71 6c  clistFunc(.  sql
32b8e 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
32b8f 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
32b90 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
32b91 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 75  e **argv.){.  fu
32b92 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
32b93 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61 72  Cursor;.  if( ar
32b94 67 63 21 3d 32 20 26 26 20 61 72 67 63 21 3d 34  gc!=2 && argc!=4
32b95 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65   ){.    generate
32b96 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
32b97 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20  "dump_doclist", 
32b98 22 69 6e 63 6f 72 72 65 63 74 20 61 72 67 75 6d  "incorrect argum
32b99 65 6e 74 73 22 29 3b 0a 20 20 7d 65 6c 73 65 20  ents");.  }else 
32b9a 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
32b9b 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21  e_type(argv[0])!
32b9c 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a  =SQLITE_BLOB ||.
32b9d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
32b9e 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
32b9f 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66  argv[0])!=sizeof
32ba0 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20  (pCursor) ){.   
32ba1 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70   generateError(p
32ba2 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64  Context, "dump_d
32ba3 6f 63 6c 69 73 74 22 2c 20 22 69 6c 6c 65 67 61  oclist", "illega
32ba4 6c 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  l first argument
32ba5 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ");.  }else if( 
32ba6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
32ba7 78 74 28 61 72 67 76 5b 31 5d 29 3d 3d 4e 55 4c  xt(argv[1])==NUL
32ba8 4c 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20  L ||.           
32ba9 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
32baa 65 78 74 28 61 72 67 76 5b 31 5d 29 5b 30 5d 3d  ext(argv[1])[0]=
32bab 3d 27 5c 30 27 20 29 7b 0a 20 20 20 20 67 65 6e  ='\0' ){.    gen
32bac 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74  erateError(pCont
32bad 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69  ext, "dump_docli
32bae 73 74 22 2c 20 22 65 6d 70 74 79 20 73 65 63 6f  st", "empty seco
32baf 6e 64 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20  nd argument");. 
32bb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73   }else{.    cons
32bb1 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20 3d 20  t char *pTerm = 
32bb2 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
32bb3 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
32bb4 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 63  (argv[1]);.    c
32bb5 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d 20 3d  onst int nTerm =
32bb6 20 73 74 72 6c 65 6e 28 70 54 65 72 6d 29 3b 0a   strlen(pTerm);.
32bb7 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61      fulltext_vta
32bb8 62 20 2a 76 3b 0a 20 20 20 20 69 6e 74 20 72 63  b *v;.    int rc
32bb9 3b 0a 20 20 20 20 44 61 74 61 42 75 66 66 65 72  ;.    DataBuffer
32bba 20 64 6f 63 6c 69 73 74 3b 0a 0a 20 20 20 20 6d   doclist;..    m
32bbb 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c 20  emcpy(&pCursor, 
32bbc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
32bbd 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a  ob(argv[0]), siz
32bbe 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a 20  eof(pCursor));. 
32bbf 20 20 20 76 20 3d 20 63 75 72 73 6f 72 5f 76 74     v = cursor_vt
32bc0 61 62 28 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20  ab(pCursor);..  
32bc1 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
32bc2 28 26 64 6f 63 6c 69 73 74 2c 20 30 29 3b 0a 0a  (&doclist, 0);..
32bc3 20 20 20 20 2f 2a 20 74 65 72 6d 53 65 6c 65 63      /* termSelec
32bc4 74 28 29 20 79 69 65 6c 64 73 20 74 68 65 20 73  t() yields the s
32bc5 61 6d 65 20 6c 6f 67 69 63 61 6c 20 64 6f 63 6c  ame logical docl
32bc6 69 73 74 20 74 68 61 74 20 71 75 65 72 69 65 73  ist that queries
32bc7 20 61 72 65 0a 20 20 20 20 2a 2a 20 72 75 6e 20   are.    ** run 
32bc8 61 67 61 69 6e 73 74 2e 0a 20 20 20 20 2a 2f 0a  against..    */.
32bc9 20 20 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20      if( argc==2 
32bca 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 74 65  ){.      rc = te
32bcb 72 6d 53 65 6c 65 63 74 28 76 2c 20 76 2d 3e 6e  rmSelect(v, v->n
32bcc 43 6f 6c 75 6d 6e 2c 20 70 54 65 72 6d 2c 20 6e  Column, pTerm, n
32bcd 54 65 72 6d 2c 20 30 2c 20 44 4c 5f 44 45 46 41  Term, 0, DL_DEFA
32bce 55 4c 54 2c 20 26 64 6f 63 6c 69 73 74 29 3b 0a  ULT, &doclist);.
32bcf 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
32bd0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
32bd1 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 20 20 20 20   = NULL;..      
32bd2 2f 2a 20 47 65 74 20 6f 75 72 20 73 70 65 63 69  /* Get our speci
32bd3 66 69 63 20 73 65 67 6d 65 6e 74 27 73 20 69 6e  fic segment's in
32bd4 66 6f 72 6d 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20  formation. */.  
32bd5 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74      rc = sql_get
32bd6 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45  _statement(v, SE
32bd7 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45 47 4d  GDIR_SELECT_SEGM
32bd8 45 4e 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ENT_STMT, &s);. 
32bd9 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
32bda 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
32bdb 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
32bdc 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 73 71  ind_int(s, 1, sq
32bdd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
32bde 61 72 67 76 5b 32 5d 29 29 3b 0a 20 20 20 20 20  argv[2]));.     
32bdf 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
32be0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
32be1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
32be2 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c 20 73 71  ind_int(s, 2, sq
32be3 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
32be4 61 72 67 76 5b 33 5d 29 29 3b 0a 20 20 20 20 20  argv[3]));.     
32be5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
32be6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
32be7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
32be8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
32be9 65 70 28 73 29 3b 0a 0a 20 20 20 20 20 20 20 20  ep(s);..        
32bea 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
32beb 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ONE ){.         
32bec 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
32bed 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20  oy(&doclist);.  
32bee 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74 65          generate
32bef 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
32bf0 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20  "dump_doclist", 
32bf1 22 73 65 67 6d 65 6e 74 20 6e 6f 74 20 66 6f 75  "segment not fou
32bf2 6e 64 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20  nd");.          
32bf3 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20  return;.        
32bf4 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  }..        /* Fo
32bf5 75 6e 64 20 61 20 73 65 67 6d 65 6e 74 2c 20 6c  und a segment, l
32bf6 6f 61 64 20 69 74 20 69 6e 74 6f 20 64 6f 63 6c  oad it into docl
32bf7 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ist. */.        
32bf8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
32bf9 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OW ){.          
32bfa 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69 6e 74  const sqlite_int
32bfb 36 34 20 69 4c 65 61 76 65 73 45 6e 64 20 3d 20  64 iLeavesEnd = 
32bfc 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
32bfd 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20  nt64(s, 1);.    
32bfe 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
32bff 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65   *pData = sqlite
32c00 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c  3_column_blob(s,
32c01 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63   2);.          c
32c02 6f 6e 73 74 20 69 6e 74 20 6e 44 61 74 61 20 3d  onst int nData =
32c03 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
32c04 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20  bytes(s, 2);..  
32c05 20 20 20 20 20 20 20 20 2f 2a 20 6c 6f 61 64 53          /* loadS
32c06 65 67 6d 65 6e 74 28 29 20 69 73 20 75 73 65 64  egment() is used
32c07 20 62 79 20 74 65 72 6d 53 65 6c 65 63 74 28 29   by termSelect()
32c08 20 74 6f 20 6c 6f 61 64 20 65 61 63 68 0a 20 20   to load each.  
32c09 20 20 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65          ** segme
32c0a 6e 74 27 73 20 64 61 74 61 2e 0a 20 20 20 20 20  nt's data..     
32c0b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
32c0c 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65    rc = loadSegme
32c0d 6e 74 28 76 2c 20 70 44 61 74 61 2c 20 6e 44 61  nt(v, pData, nDa
32c0e 74 61 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c 20  ta, iLeavesEnd, 
32c0f 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 2c  pTerm, nTerm, 0,
32c10 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32c11 20 20 20 20 20 20 20 20 20 20 20 20 26 64 6f 63              &doc
32c12 6c 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 20  list);.         
32c13 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
32c14 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
32c15 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
32c16 74 65 70 28 73 29 3b 0a 0a 20 20 20 20 20 20 20  tep(s);..       
32c17 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20 6e       /* Should n
32c18 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74 68 61  ot have more tha
32c19 6e 20 6f 6e 65 20 6d 61 74 63 68 69 6e 67 20 73  n one matching s
32c1a 65 67 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20  egment. */.     
32c1b 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
32c1c 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
32c1d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
32c1e 74 65 33 5f 72 65 73 65 74 28 73 29 3b 0a 20 20  te3_reset(s);.  
32c1f 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
32c20 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 64  BufferDestroy(&d
32c21 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  oclist);.       
32c22 20 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45         generateE
32c23 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
32c24 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22  dump_doclist", "
32c25 69 6e 76 61 6c 69 64 20 73 65 67 64 69 72 22 29  invalid segdir")
32c26 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
32c27 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20  return;.        
32c28 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
32c29 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
32c2a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
32c2b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
32c2c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
32c2d 65 73 65 74 28 73 29 3b 0a 20 20 20 20 7d 0a 0a  eset(s);.    }..
32c2e 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
32c2f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
32c30 66 28 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61  f( doclist.nData
32c31 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 72  >0 ){.        cr
32c32 65 61 74 65 44 6f 63 6c 69 73 74 52 65 73 75 6c  eateDoclistResul
32c33 74 28 70 43 6f 6e 74 65 78 74 2c 20 64 6f 63 6c  t(pContext, docl
32c34 69 73 74 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69  ist.pData, docli
32c35 73 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20  st.nData);.     
32c36 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
32c37 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20  /* TODO(shess): 
32c38 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20  This can happen 
32c39 69 66 20 74 68 65 20 74 65 72 6d 20 69 73 20 6e  if the term is n
32c3a 6f 74 20 70 72 65 73 65 6e 74 2c 20 6f 72 0a 20  ot present, or. 
32c3b 20 20 20 20 20 20 20 2a 2a 20 69 66 20 61 6c 6c         ** if all
32c3c 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68   instances of th
32c3d 65 20 74 65 72 6d 20 68 61 76 65 20 62 65 65 6e  e term have been
32c3e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68 69   deleted and thi
32c3f 73 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  s is.        ** 
32c40 61 6e 20 61 6c 6c 2d 69 6e 64 65 78 20 64 75 6d  an all-index dum
32c41 70 2e 20 20 49 74 20 6d 61 79 20 62 65 20 69 6e  p.  It may be in
32c42 74 65 72 65 73 74 69 6e 67 20 74 6f 20 64 69 73  teresting to dis
32c43 74 69 6e 67 75 69 73 68 0a 20 20 20 20 20 20 20  tinguish.       
32c44 20 2a 2a 20 74 68 65 73 65 20 63 61 73 65 73 2e   ** these cases.
32c45 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
32c46 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
32c47 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74  lt_text(pContext
32c48 2c 20 22 22 2c 20 30 2c 20 53 51 4c 49 54 45 5f  , "", 0, SQLITE_
32c49 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 7d  STATIC);.      }
32c4a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72  .    }else if( r
32c4b 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
32c4c 29 7b 0a 20 20 20 20 20 20 2f 2a 20 48 61 6e 64  ){.      /* Hand
32c4d 6c 65 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79  le out-of-memory
32c4e 20 63 61 73 65 73 20 73 70 65 63 69 61 6c 6c 79   cases specially
32c4f 20 62 65 63 61 75 73 65 20 69 66 20 74 68 65 79   because if they
32c50 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 67 65   are.      ** ge
32c51 6e 65 72 61 74 65 64 20 69 6e 20 66 74 73 33 20  nerated in fts3 
32c52 63 6f 64 65 20 74 68 65 79 20 6d 61 79 20 6e 6f  code they may no
32c53 74 20 62 65 20 72 65 66 6c 65 63 74 65 64 20 69  t be reflected i
32c54 6e 20 74 68 65 20 64 62 0a 20 20 20 20 20 20 2a  n the db.      *
32c55 2a 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 20 20  * handle..      
32c56 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f  */.      /* TODO
32c57 28 73 68 65 73 73 29 3a 20 48 61 6e 64 6c 65 20  (shess): Handle 
32c58 74 68 69 73 20 6d 6f 72 65 20 63 6f 6d 70 72 65  this more compre
32c59 68 65 6e 73 69 76 65 6c 79 2e 0a 20 20 20 20 20  hensively..     
32c5a 20 2a 2a 20 73 71 6c 69 74 65 33 45 72 72 53 74   ** sqlite3ErrSt
32c5b 72 28 29 20 68 61 73 20 77 68 61 74 20 49 20 6e  r() has what I n
32c5c 65 65 64 2c 20 62 75 74 20 69 73 20 69 6e 74 65  eed, but is inte
32c5d 72 6e 61 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  rnal..      */. 
32c5e 20 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72       generateErr
32c5f 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75  or(pContext, "du
32c60 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22 6f 75  mp_doclist", "ou
32c61 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20  t of memory");. 
32c62 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
32c63 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43  generateError(pC
32c64 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f  ontext, "dump_do
32c65 63 6c 69 73 74 22 2c 20 4e 55 4c 4c 29 3b 0a 20  clist", NULL);. 
32c66 20 20 20 7d 0a 0a 20 20 20 20 64 61 74 61 42 75     }..    dataBu
32c67 66 66 65 72 44 65 73 74 72 6f 79 28 26 64 6f 63  fferDestroy(&doc
32c68 6c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  list);.  }.}.#en
32c69 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
32c6a 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e  routine implemen
32c6b 74 73 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63  ts the xFindFunc
32c6c 74 69 6f 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20  tion method for 
32c6d 74 68 65 20 46 54 53 33 0a 2a 2a 20 76 69 72 74  the FTS3.** virt
32c6e 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ual table..*/.st
32c6f 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
32c70 74 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 0a 20  tFindFunction(. 
32c71 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
32c72 56 74 61 62 2c 0a 20 20 69 6e 74 20 6e 41 72 67  Vtab,.  int nArg
32c73 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
32c74 7a 4e 61 6d 65 2c 0a 20 20 76 6f 69 64 20 28 2a  zName,.  void (*
32c75 2a 70 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33  *pxFunc)(sqlite3
32c76 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
32c77 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a  lite3_value**),.
32c78 20 20 76 6f 69 64 20 2a 2a 70 70 41 72 67 0a 29    void **ppArg.)
32c79 7b 0a 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a  {.  if( strcmp(z
32c7a 4e 61 6d 65 2c 22 73 6e 69 70 70 65 74 22 29 3d  Name,"snippet")=
32c7b 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78 46 75 6e  =0 ){.    *pxFun
32c7c 63 20 3d 20 73 6e 69 70 70 65 74 46 75 6e 63 3b  c = snippetFunc;
32c7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
32c7e 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d   }else if( strcm
32c7f 70 28 7a 4e 61 6d 65 2c 22 6f 66 66 73 65 74 73  p(zName,"offsets
32c80 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78  ")==0 ){.    *px
32c81 46 75 6e 63 20 3d 20 73 6e 69 70 70 65 74 4f 66  Func = snippetOf
32c82 66 73 65 74 73 46 75 6e 63 3b 0a 20 20 20 20 72  fsetsFunc;.    r
32c83 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65  eturn 1;.  }else
32c84 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d   if( strcmp(zNam
32c85 65 2c 22 6f 70 74 69 6d 69 7a 65 22 29 3d 3d 30  e,"optimize")==0
32c86 20 29 7b 0a 20 20 20 20 2a 70 78 46 75 6e 63 20   ){.    *pxFunc 
32c87 3d 20 6f 70 74 69 6d 69 7a 65 46 75 6e 63 3b 0a  = optimizeFunc;.
32c88 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 69      return 1;.#i
32c89 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
32c8a 0a 20 20 20 20 2f 2a 20 4e 4f 54 45 28 73 68 65  .    /* NOTE(she
32c8b 73 73 29 3a 20 54 68 65 73 65 20 66 75 6e 63 74  ss): These funct
32c8c 69 6f 6e 73 20 61 72 65 20 70 72 65 73 65 6e 74  ions are present
32c8d 20 6f 6e 6c 79 20 66 6f 72 20 74 65 73 74 69 6e   only for testin
32c8e 67 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f 73 65  g.    ** purpose
32c8f 73 2e 20 20 4e 6f 20 70 61 72 74 69 63 75 6c 61  s.  No particula
32c90 72 20 65 66 66 6f 72 74 20 69 73 20 6d 61 64 65  r effort is made
32c91 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 74 68 65   to optimize the
32c92 69 72 0a 20 20 20 20 2a 2a 20 65 78 65 63 75 74  ir.    ** execut
32c93 69 6f 6e 20 6f 72 20 68 6f 77 20 74 68 65 79 20  ion or how they 
32c94 62 75 69 6c 64 20 74 68 65 69 72 20 72 65 73 75  build their resu
32c95 6c 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 7d 65  lts..    */.  }e
32c96 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a  lse if( strcmp(z
32c97 4e 61 6d 65 2c 22 64 75 6d 70 5f 74 65 72 6d 73  Name,"dump_terms
32c98 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ")==0 ){.    /* 
32c99 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
32c9a 22 46 6f 75 6e 64 20 64 75 6d 70 5f 74 65 72 6d  "Found dump_term
32c9b 73 5c 6e 22 29 3b 20 2a 2f 0a 20 20 20 20 2a 70  s\n"); */.    *p
32c9c 78 46 75 6e 63 20 3d 20 64 75 6d 70 54 65 72 6d  xFunc = dumpTerm
32c9d 73 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72  sFunc;.    retur
32c9e 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 1;.  }else if(
32c9f 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 64   strcmp(zName,"d
32ca0 75 6d 70 5f 64 6f 63 6c 69 73 74 22 29 3d 3d 30  ump_doclist")==0
32ca1 20 29 7b 0a 20 20 20 20 2f 2a 20 66 70 72 69 6e   ){.    /* fprin
32ca2 74 66 28 73 74 64 65 72 72 2c 20 22 46 6f 75 6e  tf(stderr, "Foun
32ca3 64 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74 5c 6e  d dump_doclist\n
32ca4 22 29 3b 20 2a 2f 0a 20 20 20 20 2a 70 78 46 75  "); */.    *pxFu
32ca5 6e 63 20 3d 20 64 75 6d 70 44 6f 63 6c 69 73 74  nc = dumpDoclist
32ca6 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72 6e  Func;.    return
32ca7 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20   1;.#endif.  }. 
32ca8 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
32ca9 0a 2a 2a 20 52 65 6e 61 6d 65 20 61 6e 20 66 74  .** Rename an ft
32caa 73 33 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  s3 table..*/.sta
32cab 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
32cac 52 65 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65  Rename(.  sqlite
32cad 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 0a 20  3_vtab *pVtab,. 
32cae 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
32caf 6d 65 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  me.){.  fulltext
32cb0 5f 76 74 61 62 20 2a 70 20 3d 20 28 66 75 6c 6c  _vtab *p = (full
32cb1 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56 74 61  text_vtab *)pVta
32cb2 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  b;.  int rc = SQ
32cb3 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 63 68  LITE_NOMEM;.  ch
32cb4 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74  ar *zSql = sqlit
32cb5 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20  e3_mprintf(.    
32cb6 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e  "ALTER TABLE %Q.
32cb7 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 20 52 45  '%q_content'  RE
32cb8 4e 41 4d 45 20 54 4f 20 27 25 71 5f 63 6f 6e 74  NAME TO '%q_cont
32cb9 65 6e 74 27 3b 22 0a 20 20 20 20 22 41 4c 54 45  ent';".    "ALTE
32cba 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 73  R TABLE %Q.'%q_s
32cbb 65 67 6d 65 6e 74 73 27 20 52 45 4e 41 4d 45 20  egments' RENAME 
32cbc 54 4f 20 27 25 71 5f 73 65 67 6d 65 6e 74 73 27  TO '%q_segments'
32cbd 3b 22 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41  ;".    "ALTER TA
32cbe 42 4c 45 20 25 51 2e 27 25 71 5f 73 65 67 64 69  BLE %Q.'%q_segdi
32cbf 72 27 20 20 20 52 45 4e 41 4d 45 20 54 4f 20 27  r'   RENAME TO '
32cc0 25 71 5f 73 65 67 64 69 72 27 3b 22 0a 20 20 20  %q_segdir';".   
32cc1 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e   , p->zDb, p->zN
32cc2 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a 20 20 20 20  ame, zName .    
32cc3 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61  , p->zDb, p->zNa
32cc4 6d 65 2c 20 7a 4e 61 6d 65 20 0a 20 20 20 20 2c  me, zName .    ,
32cc5 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d   p->zDb, p->zNam
32cc6 65 2c 20 7a 4e 61 6d 65 0a 20 20 29 3b 0a 20 20  e, zName.  );.  
32cc7 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20  if( zSql ){.    
32cc8 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
32cc9 63 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30  c(p->db, zSql, 0
32cca 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
32ccb 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b  ite3_free(zSql);
32ccc 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
32ccd 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73  ;.}..static cons
32cce 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
32ccf 20 66 74 73 33 4d 6f 64 75 6c 65 20 3d 20 7b 0a   fts3Module = {.
32cd0 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 20 20    /* iVersion   
32cd1 20 20 20 2a 2f 20 30 2c 0a 20 20 2f 2a 20 78 43     */ 0,.  /* xC
32cd2 72 65 61 74 65 20 20 20 20 20 20 20 2a 2f 20 66  reate       */ f
32cd3 75 6c 6c 74 65 78 74 43 72 65 61 74 65 2c 0a 20  ulltextCreate,. 
32cd4 20 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 20 20 20   /* xConnect    
32cd5 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6f 6e    */ fulltextCon
32cd6 6e 65 63 74 2c 0a 20 20 2f 2a 20 78 42 65 73 74  nect,.  /* xBest
32cd7 49 6e 64 65 78 20 20 20 20 2a 2f 20 66 75 6c 6c  Index    */ full
32cd8 74 65 78 74 42 65 73 74 49 6e 64 65 78 2c 0a 20  textBestIndex,. 
32cd9 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65 63 74 20   /* xDisconnect 
32cda 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 44 69 73    */ fulltextDis
32cdb 63 6f 6e 6e 65 63 74 2c 0a 20 20 2f 2a 20 78 44  connect,.  /* xD
32cdc 65 73 74 72 6f 79 20 20 20 20 20 20 2a 2f 20 66  estroy      */ f
32cdd 75 6c 6c 74 65 78 74 44 65 73 74 72 6f 79 2c 0a  ulltextDestroy,.
32cde 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20 20 20    /* xOpen      
32cdf 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 4f 70     */ fulltextOp
32ce0 65 6e 2c 0a 20 20 2f 2a 20 78 43 6c 6f 73 65 20  en,.  /* xClose 
32ce1 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65         */ fullte
32ce2 78 74 43 6c 6f 73 65 2c 0a 20 20 2f 2a 20 78 46  xtClose,.  /* xF
32ce3 69 6c 74 65 72 20 20 20 20 20 20 20 2a 2f 20 66  ilter       */ f
32ce4 75 6c 6c 74 65 78 74 46 69 6c 74 65 72 2c 0a 20  ulltextFilter,. 
32ce5 20 2f 2a 20 78 4e 65 78 74 20 20 20 20 20 20 20   /* xNext       
32ce6 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 4e 65 78    */ fulltextNex
32ce7 74 2c 0a 20 20 2f 2a 20 78 45 6f 66 20 20 20 20  t,.  /* xEof    
32ce8 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
32ce9 74 45 6f 66 2c 0a 20 20 2f 2a 20 78 43 6f 6c 75  tEof,.  /* xColu
32cea 6d 6e 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c  mn       */ full
32ceb 74 65 78 74 43 6f 6c 75 6d 6e 2c 0a 20 20 2f 2a  textColumn,.  /*
32cec 20 78 52 6f 77 69 64 20 20 20 20 20 20 20 20 2a   xRowid        *
32ced 2f 20 66 75 6c 6c 74 65 78 74 52 6f 77 69 64 2c  / fulltextRowid,
32cee 0a 20 20 2f 2a 20 78 55 70 64 61 74 65 20 20 20  .  /* xUpdate   
32cef 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 55      */ fulltextU
32cf0 70 64 61 74 65 2c 0a 20 20 2f 2a 20 78 42 65 67  pdate,.  /* xBeg
32cf1 69 6e 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  in        */ ful
32cf2 6c 74 65 78 74 42 65 67 69 6e 2c 0a 20 20 2f 2a  ltextBegin,.  /*
32cf3 20 78 53 79 6e 63 20 20 20 20 20 20 20 20 20 2a   xSync         *
32cf4 2f 20 66 75 6c 6c 74 65 78 74 53 79 6e 63 2c 0a  / fulltextSync,.
32cf5 20 20 2f 2a 20 78 43 6f 6d 6d 69 74 20 20 20 20    /* xCommit    
32cf6 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6f     */ fulltextCo
32cf7 6d 6d 69 74 2c 0a 20 20 2f 2a 20 78 52 6f 6c 6c  mmit,.  /* xRoll
32cf8 62 61 63 6b 20 20 20 20 20 2a 2f 20 66 75 6c 6c  back     */ full
32cf9 74 65 78 74 52 6f 6c 6c 62 61 63 6b 2c 0a 20 20  textRollback,.  
32cfa 2f 2a 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e  /* xFindFunction
32cfb 20 2a 2f 20 66 75 6c 6c 74 65 78 74 46 69 6e 64   */ fulltextFind
32cfc 46 75 6e 63 74 69 6f 6e 2c 0a 20 20 2f 2a 20 78  Function,.  /* x
32cfd 52 65 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20  Rename */       
32cfe 66 75 6c 6c 74 65 78 74 52 65 6e 61 6d 65 2c 0a  fulltextRename,.
32cff 7d 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  };..static void 
32d00 68 61 73 68 44 65 73 74 72 6f 79 28 76 6f 69 64  hashDestroy(void
32d01 20 2a 70 29 7b 0a 20 20 66 74 73 33 48 61 73 68   *p){.  fts3Hash
32d02 20 2a 70 48 61 73 68 20 3d 20 28 66 74 73 33 48   *pHash = (fts3H
32d03 61 73 68 20 2a 29 70 3b 0a 20 20 73 71 6c 69 74  ash *)p;.  sqlit
32d04 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28  e3Fts3HashClear(
32d05 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65  pHash);.  sqlite
32d06 33 5f 66 72 65 65 28 70 48 61 73 68 29 3b 0a 7d  3_free(pHash);.}
32d07 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 74 73 33  ../*.** The fts3
32d08 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69   built-in tokeni
32d09 7a 65 72 73 20 2d 20 22 73 69 6d 70 6c 65 22 20  zers - "simple" 
32d0a 61 6e 64 20 22 70 6f 72 74 65 72 22 20 2d 20 61  and "porter" - a
32d0b 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a  re implemented.*
32d0c 2a 20 69 6e 20 66 69 6c 65 73 20 66 74 73 33 5f  * in files fts3_
32d0d 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 61 6e 64  tokenizer1.c and
32d0e 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63 20 72   fts3_porter.c r
32d0f 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68 65  espectively. The
32d10 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 77   following.** tw
32d11 6f 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72  o forward declar
32d12 61 74 69 6f 6e 73 20 61 72 65 20 66 6f 72 20 66  ations are for f
32d13 75 6e 63 74 69 6f 6e 73 20 64 65 63 6c 61 72 65  unctions declare
32d14 64 20 69 6e 20 74 68 65 73 65 20 66 69 6c 65 73  d in these files
32d15 0a 2a 2a 20 75 73 65 64 20 74 6f 20 72 65 74 72  .** used to retr
32d16 69 65 76 65 20 74 68 65 20 72 65 73 70 65 63 74  ieve the respect
32d17 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ive implementati
32d18 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69  ons..**.** Calli
32d19 6e 67 20 73 71 6c 69 74 65 33 46 74 73 33 53 69  ng sqlite3Fts3Si
32d1a 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
32d1b 75 6c 65 28 29 20 73 65 74 73 20 74 68 65 20 76  ule() sets the v
32d1c 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20  alue pointed.** 
32d1d 74 6f 20 62 79 20 74 68 65 20 61 72 67 75 6d 65  to by the argume
32d1e 6e 74 20 74 6f 20 70 6f 69 6e 74 20 61 20 74 68  nt to point a th
32d1f 65 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b 65 6e  e "simple" token
32d20 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74  izer implementat
32d21 69 6f 6e 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ion..** Function
32d22 20 2e 2e 2e 50 6f 72 74 65 72 54 6f 6b 65 6e 69   ...PorterTokeni
32d23 7a 65 72 4d 6f 64 75 6c 65 28 29 20 73 65 74 73  zerModule() sets
32d24 20 2a 70 4d 6f 64 75 6c 65 20 74 6f 20 70 6f 69   *pModule to poi
32d25 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 72  nt to the.** por
32d26 74 65 72 20 74 6f 6b 65 6e 69 7a 65 72 2f 73 74  ter tokenizer/st
32d27 65 6d 6d 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61  emmer implementa
32d28 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
32d29 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
32d2a 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f  ite3Fts3SimpleTo
32d2b 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71  kenizerModule(sq
32d2c 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
32d2d 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
32d2e 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c 49 54 45 5f  Module);.SQLITE_
32d2f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
32d30 69 74 65 33 46 74 73 33 50 6f 72 74 65 72 54 6f  ite3Fts3PorterTo
32d31 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71  kenizerModule(sq
32d32 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
32d33 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
32d34 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c 49 54 45 5f  Module);.SQLITE_
32d35 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
32d36 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b 65 6e  ite3Fts3IcuToken
32d37 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69 74  izerModule(sqlit
32d38 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
32d39 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64  ule const**ppMod
32d3a 75 6c 65 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  ule);..SQLITE_PR
32d3b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
32d3c 33 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62  3Fts3InitHashTab
32d3d 6c 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 66 74  le(sqlite3 *, ft
32d3e 73 33 48 61 73 68 20 2a 2c 20 63 6f 6e 73 74 20  s3Hash *, const 
32d3f 63 68 61 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  char *);../*.** 
32d40 49 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20 66  Initialise the f
32d41 74 73 33 20 65 78 74 65 6e 73 69 6f 6e 2e 20 49  ts3 extension. I
32d42 66 20 74 68 69 73 20 65 78 74 65 6e 73 69 6f 6e  f this extension
32d43 20 69 73 20 62 75 69 6c 74 20 61 73 20 70 61 72   is built as par
32d44 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69  t.** of the sqli
32d45 74 65 20 6c 69 62 72 61 72 79 2c 20 74 68 65 6e  te library, then
32d46 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
32d47 73 20 63 61 6c 6c 65 64 20 64 69 72 65 63 74 6c  s called directl
32d48 79 20 62 79 0a 2a 2a 20 53 51 4c 69 74 65 2e 20  y by.** SQLite. 
32d49 49 66 20 66 74 73 33 20 69 73 20 62 75 69 6c 74  If fts3 is built
32d4a 20 61 73 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c   as a dynamicall
32d4b 79 20 6c 6f 61 64 61 62 6c 65 20 65 78 74 65 6e  y loadable exten
32d4c 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 66 75  sion, this.** fu
32d4d 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
32d4e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f   by the sqlite3_
32d4f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 29  extension_init()
32d50 20 65 6e 74 72 79 20 70 6f 69 6e 74 2e 0a 2a 2f   entry point..*/
32d51 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
32d52 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49  int sqlite3Fts3I
32d53 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  nit(sqlite3 *db)
32d54 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
32d55 49 54 45 5f 4f 4b 3b 0a 20 20 66 74 73 33 48 61  ITE_OK;.  fts3Ha
32d56 73 68 20 2a 70 48 61 73 68 20 3d 20 30 3b 0a 20  sh *pHash = 0;. 
32d57 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
32d58 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
32d59 2a 70 53 69 6d 70 6c 65 20 3d 20 30 3b 0a 20 20  *pSimple = 0;.  
32d5a 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
32d5b 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
32d5c 70 50 6f 72 74 65 72 20 3d 20 30 3b 0a 20 20 63  pPorter = 0;.  c
32d5d 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
32d5e 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
32d5f 49 63 75 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69  Icu = 0;..  sqli
32d60 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b  te3Fts3SimpleTok
32d61 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 53  enizerModule(&pS
32d62 69 6d 70 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65  imple);.  sqlite
32d63 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e  3Fts3PorterToken
32d64 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 50 6f 72  izerModule(&pPor
32d65 74 65 72 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  ter);.#ifdef SQL
32d66 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 20  ITE_ENABLE_ICU. 
32d67 20 73 71 6c 69 74 65 33 46 74 73 33 49 63 75 54   sqlite3Fts3IcuT
32d68 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26  okenizerModule(&
32d69 70 49 63 75 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  pIcu);.#endif.. 
32d6a 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64   /* Allocate and
32d6b 20 69 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20   initialise the 
32d6c 68 61 73 68 2d 74 61 62 6c 65 20 75 73 65 64 20  hash-table used 
32d6d 74 6f 20 73 74 6f 72 65 20 74 6f 6b 65 6e 69 7a  to store tokeniz
32d6e 65 72 73 2e 20 2a 2f 0a 20 20 70 48 61 73 68 20  ers. */.  pHash 
32d6f 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
32d70 28 73 69 7a 65 6f 66 28 66 74 73 33 48 61 73 68  (sizeof(fts3Hash
32d71 29 29 3b 0a 20 20 69 66 28 20 21 70 48 61 73 68  ));.  if( !pHash
32d72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
32d73 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c  ITE_NOMEM;.  }el
32d74 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46  se{.    sqlite3F
32d75 74 73 33 48 61 73 68 49 6e 69 74 28 70 48 61 73  ts3HashInit(pHas
32d76 68 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52  h, FTS3_HASH_STR
32d77 49 4e 47 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20  ING, 1);.  }..  
32d78 2f 2a 20 4c 6f 61 64 20 74 68 65 20 62 75 69 6c  /* Load the buil
32d79 74 2d 69 6e 20 74 6f 6b 65 6e 69 7a 65 72 73 20  t-in tokenizers 
32d7a 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  into the hash ta
32d7b 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  ble */.  if( rc=
32d7c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
32d7d 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 74 73    if( sqlite3Fts
32d7e 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 73  3HashInsert(pHas
32d7f 68 2c 20 22 73 69 6d 70 6c 65 22 2c 20 37 2c 20  h, "simple", 7, 
32d80 28 76 6f 69 64 20 2a 29 70 53 69 6d 70 6c 65 29  (void *)pSimple)
32d81 0a 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33  .     || sqlite3
32d82 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70  Fts3HashInsert(p
32d83 48 61 73 68 2c 20 22 70 6f 72 74 65 72 22 2c 20  Hash, "porter", 
32d84 37 2c 20 28 76 6f 69 64 20 2a 29 70 50 6f 72 74  7, (void *)pPort
32d85 65 72 29 20 0a 20 20 20 20 20 7c 7c 20 28 70 49  er) .     || (pI
32d86 63 75 20 26 26 20 73 71 6c 69 74 65 33 46 74 73  cu && sqlite3Fts
32d87 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 73  3HashInsert(pHas
32d88 68 2c 20 22 69 63 75 22 2c 20 34 2c 20 28 76 6f  h, "icu", 4, (vo
32d89 69 64 20 2a 29 70 49 63 75 29 29 0a 20 20 20 20  id *)pIcu)).    
32d8a 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
32d8b 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
32d8c 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51  }.  }..#ifdef SQ
32d8d 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69  LITE_TEST.  sqli
32d8e 74 65 33 46 74 73 33 45 78 70 72 49 6e 69 74 54  te3Fts3ExprInitT
32d8f 65 73 74 49 6e 74 65 72 66 61 63 65 28 64 62 29  estInterface(db)
32d90 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 43  ;.#endif..  /* C
32d91 72 65 61 74 65 20 74 68 65 20 76 69 72 74 75 61  reate the virtua
32d92 6c 20 74 61 62 6c 65 20 77 72 61 70 70 65 72 20  l table wrapper 
32d93 61 72 6f 75 6e 64 20 74 68 65 20 68 61 73 68 2d  around the hash-
32d94 74 61 62 6c 65 20 61 6e 64 20 6f 76 65 72 6c 6f  table and overlo
32d95 61 64 20 0a 20 20 2a 2a 20 74 68 65 20 74 77 6f  ad .  ** the two
32d96 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
32d97 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 73 75  s. If this is su
32d98 63 63 65 73 73 66 75 6c 2c 20 72 65 67 69 73 74  ccessful, regist
32d99 65 72 20 74 68 65 0a 20 20 2a 2a 20 6d 6f 64 75  er the.  ** modu
32d9a 6c 65 20 77 69 74 68 20 73 71 6c 69 74 65 2e 0a  le with sqlite..
32d9b 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54    */.  if( SQLIT
32d9c 45 5f 4f 4b 3d 3d 72 63 20 0a 20 20 20 26 26 20  E_OK==rc .   && 
32d9d 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
32d9e 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
32d9f 48 61 73 68 54 61 62 6c 65 28 64 62 2c 20 70 48  HashTable(db, pH
32da0 61 73 68 2c 20 22 66 74 73 33 5f 74 6f 6b 65 6e  ash, "fts3_token
32da1 69 7a 65 72 22 29 29 0a 20 20 20 26 26 20 53 51  izer")).   && SQ
32da2 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
32da3 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
32da4 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 73 6e  function(db, "sn
32da5 69 70 70 65 74 22 2c 20 2d 31 29 29 0a 20 20 20  ippet", -1)).   
32da6 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
32da7 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  c = sqlite3_over
32da8 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62  load_function(db
32da9 2c 20 22 6f 66 66 73 65 74 73 22 2c 20 2d 31 29  , "offsets", -1)
32daa 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f  ).   && SQLITE_O
32dab 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
32dac 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
32dad 6f 6e 28 64 62 2c 20 22 6f 70 74 69 6d 69 7a 65  on(db, "optimize
32dae 22 2c 20 2d 31 29 29 0a 23 69 66 64 65 66 20 53  ", -1)).#ifdef S
32daf 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 26 26  QLITE_TEST.   &&
32db0 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
32db1 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  = sqlite3_overlo
32db2 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  ad_function(db, 
32db3 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 2d 31  "dump_terms", -1
32db4 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f  )).   && SQLITE_
32db5 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
32db6 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
32db7 69 6f 6e 28 64 62 2c 20 22 64 75 6d 70 5f 64 6f  ion(db, "dump_do
32db8 63 6c 69 73 74 22 2c 20 2d 31 29 29 0a 23 65 6e  clist", -1)).#en
32db9 64 69 66 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  dif.  ){.    ret
32dba 75 72 6e 20 73 71 6c 69 74 65 33 5f 63 72 65 61  urn sqlite3_crea
32dbb 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a 20 20  te_module_v2(.  
32dbc 20 20 20 20 20 20 64 62 2c 20 22 66 74 73 33 22        db, "fts3"
32dbd 2c 20 26 66 74 73 33 4d 6f 64 75 6c 65 2c 20 28  , &fts3Module, (
32dbe 76 6f 69 64 20 2a 29 70 48 61 73 68 2c 20 68 61  void *)pHash, ha
32dbf 73 68 44 65 73 74 72 6f 79 0a 20 20 20 20 29 3b  shDestroy.    );
32dc0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72  .  }..  /* An er
32dc1 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64 2e  ror has occured.
32dc2 20 44 65 6c 65 74 65 20 74 68 65 20 68 61 73 68   Delete the hash
32dc3 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74 75 72   table and retur
32dc4 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  n the error code
32dc5 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72  . */.  assert( r
32dc6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c!=SQLITE_OK );.
32dc7 20 20 69 66 28 20 70 48 61 73 68 20 29 7b 0a 20    if( pHash ){. 
32dc8 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61     sqlite3Fts3Ha
32dc9 73 68 43 6c 65 61 72 28 70 48 61 73 68 29 3b 0a  shClear(pHash);.
32dca 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
32dcb 28 70 48 61 73 68 29 3b 0a 20 20 7d 0a 20 20 72  (pHash);.  }.  r
32dcc 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
32dcd 20 21 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51   !SQLITE_CORE.SQ
32dce 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
32dcf 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
32dd0 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit(.  sqlite3 *
32dd1 64 62 2c 20 0a 20 20 63 68 61 72 20 2a 2a 70 7a  db, .  char **pz
32dd2 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20  ErrMsg,.  const 
32dd3 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
32dd4 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20  ines *pApi.){.  
32dd5 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e  SQLITE_EXTENSION
32dd6 5f 49 4e 49 54 32 28 70 41 70 69 29 0a 20 20 72  _INIT2(pApi).  r
32dd7 65 74 75 72 6e 20 73 71 6c 69 74 65 33 46 74 73  eturn sqlite3Fts
32dd8 33 49 6e 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e  3Init(db);.}.#en
32dd9 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
32dda 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
32ddb 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
32ddc 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
32ddd 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  S3) */../*******
32dde 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
32ddf 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ts3.c **********
32de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32de1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32de2 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
32de3 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
32de4 6c 65 20 66 74 73 33 5f 65 78 70 72 2e 63 20 2a  le fts3_expr.c *
32de5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32de6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32de7 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
32de8 30 38 20 4e 6f 76 20 32 38 0a 2a 2a 0a 2a 2a 20  08 Nov 28.**.** 
32de9 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
32dea 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
32deb 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
32dec 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
32ded 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
32dee 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
32def 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
32df0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
32df1 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
32df2 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
32df3 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
32df4 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
32df5 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
32df6 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
32df7 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
32df8 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
32df9 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
32dfa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32dfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32dfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32dfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32dfe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
32dff 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20  .** This module 
32e00 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68  contains code th
32e01 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  at implements a 
32e02 70 61 72 73 65 72 20 66 6f 72 20 66 74 73 33 20  parser for fts3 
32e03 71 75 65 72 79 20 73 74 72 69 6e 67 73 0a 2a 2a  query strings.**
32e04 20 28 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64   (the right-hand
32e05 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
32e06 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 29   MATCH operator)
32e07 2e 20 42 65 63 61 75 73 65 20 74 68 65 20 73 75  . Because the su
32e08 70 70 6f 72 74 65 64 20 0a 2a 2a 20 73 79 6e 74  pported .** synt
32e09 61 78 20 69 73 20 72 65 6c 61 74 69 76 65 6c 79  ax is relatively
32e0a 20 73 69 6d 70 6c 65 2c 20 74 68 65 20 77 68 6f   simple, the who
32e0b 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 2f 70 61 72  le tokenizer/par
32e0c 73 65 72 20 73 79 73 74 65 6d 20 69 73 0a 2a 2a  ser system is.**
32e0d 20 68 61 6e 64 2d 63 6f 64 65 64 2e 20 54 68 65   hand-coded. The
32e0e 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63   public interfac
32e0f 65 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65  e to this module
32e10 20 69 73 20 64 65 63 6c 61 72 65 64 20 69 6e 20   is declared in 
32e11 73 6f 75 72 63 65 0a 2a 2a 20 63 6f 64 65 20 66  source.** code f
32e12 69 6c 65 20 22 66 74 73 33 5f 65 78 70 72 2e 68  ile "fts3_expr.h
32e13 22 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  "..*/.#if !defin
32e14 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20  ed(SQLITE_CORE) 
32e15 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
32e16 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a  E_ENABLE_FTS3)..
32e17 2f 2a 0a 2a 2a 20 42 79 20 64 65 66 61 75 6c 74  /*.** By default
32e18 2c 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 70 61  , this module pa
32e19 72 73 65 73 20 74 68 65 20 6c 65 67 61 63 79 20  rses the legacy 
32e1a 73 79 6e 74 61 78 20 74 68 61 74 20 68 61 73 20  syntax that has 
32e1b 62 65 65 6e 20 0a 2a 2a 20 74 72 61 64 69 74 69  been .** traditi
32e1c 6f 6e 61 6c 6c 79 20 75 73 65 64 20 62 79 20 66  onally used by f
32e1d 74 73 33 2e 20 4f 72 2c 20 69 66 20 53 51 4c 49  ts3. Or, if SQLI
32e1e 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50  TE_ENABLE_FTS3_P
32e1f 41 52 45 4e 54 48 45 53 49 53 0a 2a 2a 20 69 73  ARENTHESIS.** is
32e20 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 69   defined, then i
32e21 74 20 75 73 65 73 20 74 68 65 20 6e 65 77 20 73  t uses the new s
32e22 79 6e 74 61 78 2e 20 54 68 65 20 64 69 66 66 65  yntax. The diffe
32e23 72 65 6e 63 65 73 20 62 65 74 77 65 65 6e 0a 2a  rences between.*
32e24 2a 20 74 68 65 20 6e 65 77 20 61 6e 64 20 74 68  * the new and th
32e25 65 20 6f 6c 64 20 73 79 6e 74 61 78 65 73 20 61  e old syntaxes a
32e26 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 61 29 20 54 68  re:.**.**  a) Th
32e27 65 20 6e 65 77 20 73 79 6e 74 61 78 20 73 75 70  e new syntax sup
32e28 70 6f 72 74 73 20 70 61 72 65 6e 74 68 65 73 69  ports parenthesi
32e29 73 2e 20 54 68 65 20 6f 6c 64 20 64 6f 65 73 20  s. The old does 
32e2a 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 20 62 29 20 54  not..**.**  b) T
32e2b 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20 73 75  he new syntax su
32e2c 70 70 6f 72 74 73 20 74 68 65 20 41 4e 44 20 61  pports the AND a
32e2d 6e 64 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 73  nd NOT operators
32e2e 2e 20 54 68 65 20 6f 6c 64 20 64 6f 65 73 20 6e  . The old does n
32e2f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 20 63 29 20 54 68  ot..**.**  c) Th
32e30 65 20 6f 6c 64 20 73 79 6e 74 61 78 20 73 75 70  e old syntax sup
32e31 70 6f 72 74 73 20 74 68 65 20 22 2d 22 20 74 6f  ports the "-" to
32e32 6b 65 6e 20 71 75 61 6c 69 66 69 65 72 2e 20 54  ken qualifier. T
32e33 68 69 73 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 20  his is not .**  
32e34 20 20 20 73 75 70 70 6f 72 74 65 64 20 62 79 20     supported by 
32e35 74 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20 28  the new syntax (
32e36 69 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 62  it is replaced b
32e37 79 20 74 68 65 20 4e 4f 54 20 6f 70 65 72 61 74  y the NOT operat
32e38 6f 72 29 2e 0a 2a 2a 0a 2a 2a 20 20 64 29 20 57  or)..**.**  d) W
32e39 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20 6f 6c  hen using the ol
32e3a 64 20 73 79 6e 74 61 78 2c 20 74 68 65 20 4f 52  d syntax, the OR
32e3b 20 6f 70 65 72 61 74 6f 72 20 68 61 73 20 61 20   operator has a 
32e3c 67 72 65 61 74 65 72 20 70 72 65 63 65 64 65 6e  greater preceden
32e3d 63 65 0a 2a 2a 20 20 20 20 20 74 68 61 6e 20 61  ce.**     than a
32e3e 6e 20 69 6d 70 6c 69 63 69 74 20 41 4e 44 2e 20  n implicit AND. 
32e3f 57 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20 6e  When using the n
32e40 65 77 2c 20 62 6f 74 68 20 69 6d 70 6c 69 63 69  ew, both implici
32e41 74 79 20 61 6e 64 20 65 78 70 6c 69 63 69 74 0a  ty and explicit.
32e42 2a 2a 20 20 20 20 20 41 4e 44 20 6f 70 65 72 61  **     AND opera
32e43 74 6f 72 73 20 68 61 76 65 20 61 20 68 69 67 68  tors have a high
32e44 65 72 20 70 72 65 63 65 64 65 6e 63 65 20 74 68  er precedence th
32e45 61 6e 20 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  an OR..**.** If 
32e46 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51  compiled with SQ
32e47 4c 49 54 45 5f 54 45 53 54 20 64 65 66 69 6e 65  LITE_TEST define
32e48 64 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 6f 64  d, then this mod
32e49 75 6c 65 20 65 78 70 6f 72 74 73 20 74 68 65 0a  ule exports the.
32e4a 2a 2a 20 73 79 6d 62 6f 6c 20 22 69 6e 74 20 73  ** symbol "int s
32e4b 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62  qlite3_fts3_enab
32e4c 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 22 2e  le_parentheses".
32e4d 20 53 65 74 74 69 6e 67 20 74 68 69 73 20 76 61   Setting this va
32e4e 72 69 61 62 6c 65 0a 2a 2a 20 74 6f 20 7a 65 72  riable.** to zer
32e4f 6f 20 63 61 75 73 65 73 20 74 68 65 20 6d 6f 64  o causes the mod
32e50 75 6c 65 20 74 6f 20 75 73 65 20 74 68 65 20 6f  ule to use the o
32e51 6c 64 20 73 79 6e 74 61 78 2e 20 49 66 20 69 74  ld syntax. If it
32e52 20 69 73 20 73 65 74 20 74 6f 20 0a 2a 2a 20 6e   is set to .** n
32e53 6f 6e 2d 7a 65 72 6f 20 74 68 65 20 6e 65 77 20  on-zero the new 
32e54 73 79 6e 74 61 78 20 69 73 20 61 63 74 69 76 61  syntax is activa
32e55 74 65 64 2e 20 54 68 69 73 20 69 73 20 73 6f 20  ted. This is so 
32e56 62 6f 74 68 20 73 79 6e 74 61 78 65 73 20 63 61  both syntaxes ca
32e57 6e 0a 2a 2a 20 62 65 20 74 65 73 74 65 64 20 75  n.** be tested u
32e58 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 62 75  sing a single bu
32e59 69 6c 64 20 6f 66 20 74 65 73 74 66 69 78 74 75  ild of testfixtu
32e5a 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  re..*/.#ifdef SQ
32e5b 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
32e5c 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
32e5d 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
32e5e 65 6e 74 68 65 73 65 73 20 3d 20 30 3b 0a 23 65  entheses = 0;.#e
32e5f 6c 73 65 0a 23 20 69 66 64 65 66 20 53 51 4c 49  lse.# ifdef SQLI
32e60 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50  TE_ENABLE_FTS3_P
32e61 41 52 45 4e 54 48 45 53 49 53 20 0a 23 20 20 64  ARENTHESIS .#  d
32e62 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 74  efine sqlite3_ft
32e63 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74  s3_enable_parent
32e64 68 65 73 65 73 20 31 0a 23 20 65 6c 73 65 0a 23  heses 1.# else.#
32e65 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
32e66 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
32e67 65 6e 74 68 65 73 65 73 20 30 0a 23 20 65 6e 64  entheses 0.# end
32e68 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  if.#endif../*.**
32e69 20 44 65 66 61 75 6c 74 20 73 70 61 6e 20 66 6f   Default span fo
32e6a 72 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 73  r NEAR operators
32e6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
32e6c 49 54 45 5f 46 54 53 33 5f 44 45 46 41 55 4c 54  ITE_FTS3_DEFAULT
32e6d 5f 4e 45 41 52 5f 50 41 52 41 4d 20 31 30 0a 0a  _NEAR_PARAM 10..
32e6e 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
32e6f 50 61 72 73 65 43 6f 6e 74 65 78 74 20 50 61 72  ParseContext Par
32e70 73 65 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63  seContext;.struc
32e71 74 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20 7b  t ParseContext {
32e72 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
32e73 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
32e74 3b 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69  ;      /* Tokeni
32e75 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  zer module */.  
32e76 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 61 7a 43  const char **azC
32e77 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
32e78 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
32e79 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72  column names for
32e7a 20 66 74 73 33 20 74 61 62 6c 65 20 2a 2f 0a 20   fts3 table */. 
32e7b 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20   int nCol;      
32e7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e7d 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
32e7e 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 7a 43  f entries in azC
32e7f 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 44  ol[] */.  int iD
32e80 65 66 61 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20  efaultCol;      
32e81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32e82 20 44 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20   Default column 
32e83 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20 20 73 71  to query */.  sq
32e84 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
32e85 43 74 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ctx;            
32e86 20 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f 72    /* Write error
32e87 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 2a 2f   message here */
32e88 0a 20 20 69 6e 74 20 6e 4e 65 73 74 3b 20 20 20  .  int nNest;   
32e89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e8a 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
32e8b 20 6f 66 20 6e 65 73 74 65 64 20 62 72 61 63 6b   of nested brack
32e8c 65 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ets */.};../*.**
32e8d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
32e8e 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20  s equivalent to 
32e8f 74 68 65 20 73 74 61 6e 64 61 72 64 20 69 73 73  the standard iss
32e90 70 61 63 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e  pace() function.
32e91 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61 6e   .**.** The stan
32e92 64 61 72 64 20 69 73 73 70 61 63 65 28 29 20 63  dard isspace() c
32e93 61 6e 20 62 65 20 61 77 6b 77 61 72 64 20 74 6f  an be awkward to
32e94 20 75 73 65 20 73 61 66 65 6c 79 2c 20 62 65 63   use safely, bec
32e95 61 75 73 65 20 61 6c 74 68 6f 75 67 68 20 69 74  ause although it
32e96 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74  .** is defined t
32e97 6f 20 61 63 63 65 70 74 20 61 6e 20 61 72 67 75  o accept an argu
32e98 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e 74  ment of type int
32e99 2c 20 69 74 73 20 62 65 68 61 76 69 6f 75 72 20  , its behaviour 
32e9a 77 68 65 6e 20 70 61 73 73 65 64 0a 2a 2a 20 61  when passed.** a
32e9b 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 66  n integer that f
32e9c 61 6c 6c 73 20 6f 75 74 73 69 64 65 20 6f 66 20  alls outside of 
32e9d 74 68 65 20 72 61 6e 67 65 20 6f 66 20 74 68 65  the range of the
32e9e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74   unsigned char t
32e9f 79 70 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69  ype.** is undefi
32ea0 6e 65 64 20 28 61 6e 64 20 73 6f 6d 65 74 69 6d  ned (and sometim
32ea1 65 73 2c 20 22 75 6e 64 65 66 69 6e 65 64 22 20  es, "undefined" 
32ea2 6d 65 61 6e 73 20 73 65 67 66 61 75 6c 74 29 2e  means segfault).
32ea3 20 54 68 69 73 20 77 72 61 70 70 65 72 0a 2a 2a   This wrapper.**
32ea4 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 61   is defined to a
32ea5 63 63 65 70 74 20 61 6e 20 61 72 67 75 6d 65 6e  ccept an argumen
32ea6 74 20 6f 66 20 74 79 70 65 20 63 68 61 72 2c 20  t of type char, 
32ea7 61 6e 64 20 61 6c 77 61 79 73 20 72 65 74 75 72  and always retur
32ea8 6e 73 20 30 20 66 6f 72 0a 2a 2a 20 61 6e 79 20  ns 0 for.** any 
32ea9 76 61 6c 75 65 73 20 74 68 61 74 20 66 61 6c 6c  values that fall
32eaa 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20   outside of the 
32eab 72 61 6e 67 65 20 6f 66 20 74 68 65 20 75 6e 73  range of the uns
32eac 69 67 6e 65 64 20 63 68 61 72 20 74 79 70 65 20  igned char type 
32ead 28 69 2e 65 2e 0a 2a 2a 20 6e 65 67 61 74 69 76  (i.e..** negativ
32eae 65 20 76 61 6c 75 65 73 29 2e 0a 2a 2f 0a 73 74  e values)..*/.st
32eaf 61 74 69 63 20 69 6e 74 20 66 74 73 33 69 73 73  atic int fts3iss
32eb0 70 61 63 65 28 63 68 61 72 20 63 29 7b 0a 20 20  pace(char c){.  
32eb1 72 65 74 75 72 6e 20 28 63 26 30 78 38 30 29 3d  return (c&0x80)=
32eb2 3d 30 20 3f 20 69 73 73 70 61 63 65 28 63 29 20  =0 ? isspace(c) 
32eb3 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78  : 0;.}../*.** Ex
32eb4 74 72 61 63 74 20 74 68 65 20 6e 65 78 74 20 74  tract the next t
32eb5 6f 6b 65 6e 20 66 72 6f 6d 20 62 75 66 66 65 72  oken from buffer
32eb6 20 7a 20 28 6c 65 6e 67 74 68 20 6e 29 20 75 73   z (length n) us
32eb7 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65  ing the tokenize
32eb8 72 0a 2a 2a 20 61 6e 64 20 6f 74 68 65 72 20 69  r.** and other i
32eb9 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 63 6f 6c 75  nformation (colu
32eba 6d 6e 20 6e 61 6d 65 73 20 65 74 63 2e 29 20 69  mn names etc.) i
32ebb 6e 20 70 50 61 72 73 65 2e 20 43 72 65 61 74 65  n pParse. Create
32ebc 20 61 6e 20 46 74 73 33 45 78 70 72 0a 2a 2a 20   an Fts3Expr.** 
32ebd 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 79 70  structure of typ
32ebe 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  e FTSQUERY_PHRAS
32ebf 45 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70  E containing a p
32ec0 68 72 61 73 65 20 63 6f 6e 73 69 73 74 69 6e 67  hrase consisting
32ec1 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 69 6e 67   of this.** sing
32ec2 6c 65 20 74 6f 6b 65 6e 20 61 6e 64 20 73 65 74  le token and set
32ec3 20 2a 70 70 45 78 70 72 20 74 6f 20 70 6f 69 6e   *ppExpr to poin
32ec4 74 20 74 6f 20 69 74 2e 20 49 66 20 74 68 65 20  t to it. If the 
32ec5 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66 66 65  end of the buffe
32ec6 72 20 69 73 0a 2a 2a 20 72 65 61 63 68 65 64 20  r is.** reached 
32ec7 62 65 66 6f 72 65 20 61 20 74 6f 6b 65 6e 20 69  before a token i
32ec8 73 20 66 6f 75 6e 64 2c 20 73 65 74 20 2a 70 70  s found, set *pp
32ec9 45 78 70 72 20 74 6f 20 7a 65 72 6f 2e 20 49 74  Expr to zero. It
32eca 20 69 73 20 74 68 65 0a 2a 2a 20 72 65 73 70 6f   is the.** respo
32ecb 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65  nsibility of the
32ecc 20 63 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74   caller to event
32ecd 75 61 6c 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65  ually deallocate
32ece 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a   the allocated .
32ecf 2a 2a 20 46 74 73 33 45 78 70 72 20 73 74 72 75  ** Fts3Expr stru
32ed0 63 74 75 72 65 20 28 69 66 20 61 6e 79 29 20 62  cture (if any) b
32ed1 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20  y passing it to 
32ed2 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 0a  sqlite3_free()..
32ed3 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
32ed4 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73  ITE_OK if succes
32ed5 73 66 75 6c 2c 20 6f 72 20 53 51 4c 49 54 45 5f  sful, or SQLITE_
32ed6 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65 6d 6f 72  NOMEM if a memor
32ed7 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  y allocation.** 
32ed8 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  fails..*/.static
32ed9 20 69 6e 74 20 67 65 74 4e 65 78 74 54 6f 6b 65   int getNextToke
32eda 6e 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  n(.  ParseContex
32edb 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  t *pParse,      
32edc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32edd 66 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65  fts3 query parse
32ede 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
32edf 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  t iCol,         
32ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ee1 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66        /* Value f
32ee2 6f 72 20 46 74 73 33 50 68 72 61 73 65 2e 69 43  or Fts3Phrase.iC
32ee3 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  olumn */.  const
32ee4 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
32ee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ee6 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69     /* Input stri
32ee7 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72  ng */.  Fts3Expr
32ee8 20 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20   **ppExpr,      
32ee9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32eea 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65 73 73 69  /* OUT: expressi
32eeb 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  on */.  int *pnC
32eec 6f 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20 20  onsumed         
32eed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32eee 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f  /* OUT: Number o
32eef 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64  f bytes consumed
32ef0 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
32ef1 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
32ef2 65 6e 69 7a 65 72 20 3d 20 70 50 61 72 73 65 2d  enizer = pParse-
32ef3 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73  >pTokenizer;.  s
32ef4 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
32ef5 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70  _module const *p
32ef6 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69  Module = pTokeni
32ef7 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  zer->pModule;.  
32ef8 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
32ef9 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
32efa 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 46  or *pCursor;.  F
32efb 74 73 33 45 78 70 72 20 2a 70 52 65 74 20 3d 20  ts3Expr *pRet = 
32efc 30 3b 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 75 6d  0;.  int nConsum
32efd 65 64 20 3d 20 30 3b 0a 0a 20 20 72 63 20 3d 20  ed = 0;..  rc = 
32efe 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70  pModule->xOpen(p
32eff 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 2c 20 6e 2c  Tokenizer, z, n,
32f00 20 26 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66   &pCursor);.  if
32f01 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
32f02 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
32f03 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 69  r *zToken;.    i
32f04 6e 74 20 6e 54 6f 6b 65 6e 2c 20 69 53 74 61 72  nt nToken, iStar
32f05 74 2c 20 69 45 6e 64 2c 20 69 50 6f 73 69 74 69  t, iEnd, iPositi
32f06 6f 6e 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74  on;.    int nByt
32f07 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
32f08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f09 20 2f 2a 20 74 6f 74 61 6c 20 73 70 61 63 65 20   /* total space 
32f0a 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 0a  to allocate */..
32f0b 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f      pCursor->pTo
32f0c 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e  kenizer = pToken
32f0d 69 7a 65 72 3b 0a 20 20 20 20 72 63 20 3d 20 70  izer;.    rc = p
32f0e 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43  Module->xNext(pC
32f0f 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20  ursor, &zToken, 
32f10 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74 61 72 74  &nToken, &iStart
32f11 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 69 74  , &iEnd, &iPosit
32f12 69 6f 6e 29 3b 0a 0a 20 20 20 20 69 66 28 20 72  ion);..    if( r
32f13 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
32f14 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69        nByte = si
32f15 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 20 2b  zeof(Fts3Expr) +
32f16 20 73 69 7a 65 6f 66 28 46 74 73 33 50 68 72 61   sizeof(Fts3Phra
32f17 73 65 29 20 2b 20 6e 54 6f 6b 65 6e 3b 0a 20 20  se) + nToken;.  
32f18 20 20 20 20 70 52 65 74 20 3d 20 28 46 74 73 33      pRet = (Fts3
32f19 45 78 70 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d  Expr *)sqlite3_m
32f1a 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  alloc(nByte);.  
32f1b 20 20 20 20 69 66 28 20 21 70 52 65 74 20 29 7b      if( !pRet ){
32f1c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
32f1d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
32f1e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
32f1f 20 6d 65 6d 73 65 74 28 70 52 65 74 2c 20 30 2c   memset(pRet, 0,
32f20 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20   nByte);.       
32f21 20 70 52 65 74 2d 3e 65 54 79 70 65 20 3d 20 46   pRet->eType = F
32f22 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 3b 0a  TSQUERY_PHRASE;.
32f23 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50          pRet->pP
32f24 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68 72  hrase = (Fts3Phr
32f25 61 73 65 20 2a 29 26 70 52 65 74 5b 31 5d 3b 0a  ase *)&pRet[1];.
32f26 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50          pRet->pP
32f27 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20  hrase->nToken = 
32f28 31 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  1;.        pRet-
32f29 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d  >pPhrase->iColum
32f2a 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20  n = iCol;.      
32f2b 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d    pRet->pPhrase-
32f2c 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 6e 20 3d 20 6e  >aToken[0].n = n
32f2d 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70  Token;.        p
32f2e 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54  Ret->pPhrase->aT
32f2f 6f 6b 65 6e 5b 30 5d 2e 7a 20 3d 20 28 63 68 61  oken[0].z = (cha
32f30 72 20 2a 29 26 70 52 65 74 2d 3e 70 50 68 72 61  r *)&pRet->pPhra
32f31 73 65 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d  se[1];.        m
32f32 65 6d 63 70 79 28 70 52 65 74 2d 3e 70 50 68 72  emcpy(pRet->pPhr
32f33 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a  ase->aToken[0].z
32f34 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  , zToken, nToken
32f35 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
32f36 69 45 6e 64 3c 6e 20 26 26 20 7a 5b 69 45 6e 64  iEnd<n && z[iEnd
32f37 5d 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20  ]=='*' ){.      
32f38 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73      pRet->pPhras
32f39 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 69 73 50  e->aToken[0].isP
32f3a 72 65 66 69 78 20 3d 20 31 3b 0a 20 20 20 20 20  refix = 1;.     
32f3b 20 20 20 20 20 69 45 6e 64 2b 2b 3b 0a 20 20 20       iEnd++;.   
32f3c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
32f3d 66 28 20 21 73 71 6c 69 74 65 33 5f 66 74 73 33  f( !sqlite3_fts3
32f3e 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65  _enable_parenthe
32f3f 73 65 73 20 26 26 20 69 53 74 61 72 74 3e 30 20  ses && iStart>0 
32f40 26 26 20 7a 5b 69 53 74 61 72 74 2d 31 5d 3d 3d  && z[iStart-1]==
32f41 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '-' ){.         
32f42 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e   pRet->pPhrase->
32f43 69 73 4e 6f 74 20 3d 20 31 3b 0a 20 20 20 20 20  isNot = 1;.     
32f44 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
32f45 20 7d 0a 20 20 20 20 6e 43 6f 6e 73 75 6d 65 64   }.    nConsumed
32f46 20 3d 20 69 45 6e 64 3b 0a 0a 20 20 20 20 70 4d   = iEnd;..    pM
32f47 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43  odule->xClose(pC
32f48 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 0a 20  ursor);.  }.  . 
32f49 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 6e   *pnConsumed = n
32f4a 43 6f 6e 73 75 6d 65 64 3b 0a 20 20 2a 70 70 45  Consumed;.  *ppE
32f4b 78 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 72 65  xpr = pRet;.  re
32f4c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
32f4d 2a 2a 20 45 6e 6c 61 72 67 65 20 61 20 6d 65 6d  ** Enlarge a mem
32f4e 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  ory allocation. 
32f4f 20 49 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65   If an out-of-me
32f50 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
32f51 6f 63 63 75 72 73 2c 0a 2a 2a 20 74 68 65 6e 20  occurs,.** then 
32f52 66 72 65 65 20 74 68 65 20 6f 6c 64 20 61 6c 6c  free the old all
32f53 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64  ocation..*/.void
32f54 20 2a 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46   *fts3ReallocOrF
32f55 72 65 65 28 76 6f 69 64 20 2a 70 4f 72 69 67 2c  ree(void *pOrig,
32f56 20 69 6e 74 20 6e 4e 65 77 29 7b 0a 20 20 76 6f   int nNew){.  vo
32f57 69 64 20 2a 70 52 65 74 20 3d 20 73 71 6c 69 74  id *pRet = sqlit
32f58 65 33 5f 72 65 61 6c 6c 6f 63 28 70 4f 72 69 67  e3_realloc(pOrig
32f59 2c 20 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20 21  , nNew);.  if( !
32f5a 70 52 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  pRet ){.    sqli
32f5b 74 65 33 5f 66 72 65 65 28 70 4f 72 69 67 29 3b  te3_free(pOrig);
32f5c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 52  .  }.  return pR
32f5d 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 66  et;.}../*.** Buf
32f5e 66 65 72 20 7a 49 6e 70 75 74 2c 20 6c 65 6e 67  fer zInput, leng
32f5f 74 68 20 6e 49 6e 70 75 74 2c 20 63 6f 6e 74 61  th nInput, conta
32f60 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ins the contents
32f61 20 6f 66 20 61 20 71 75 6f 74 65 64 20 73 74 72   of a quoted str
32f62 69 6e 67 0a 2a 2a 20 74 68 61 74 20 61 70 70 65  ing.** that appe
32f63 61 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  ared as part of 
32f64 61 6e 20 66 74 73 33 20 71 75 65 72 79 20 65 78  an fts3 query ex
32f65 70 72 65 73 73 69 6f 6e 2e 20 4e 65 69 74 68 65  pression. Neithe
32f66 72 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65  r quote characte
32f67 72 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64  r.** is included
32f68 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 2e 20   in the buffer. 
32f69 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74  This function at
32f6a 74 65 6d 70 74 73 20 74 6f 20 74 6f 6b 65 6e 69  tempts to tokeni
32f6b 7a 65 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a  ze the entire.**
32f6c 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 61 6e   input buffer an
32f6d 64 20 63 72 65 61 74 65 20 61 6e 20 46 74 73 33  d create an Fts3
32f6e 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20 6f  Expr structure o
32f6f 66 20 74 79 70 65 20 46 54 53 51 55 45 52 59 5f  f type FTSQUERY_
32f70 50 48 52 41 53 45 20 0a 2a 2a 20 63 6f 6e 74 61  PHRASE .** conta
32f71 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74  ining the result
32f72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63  s..**.** If succ
32f73 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f  essful, SQLITE_O
32f74 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  K is returned an
32f75 64 20 2a 70 70 45 78 70 72 20 73 65 74 20 74 6f  d *ppExpr set to
32f76 20 70 6f 69 6e 74 20 61 74 20 74 68 65 0a 2a 2a   point at the.**
32f77 20 61 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 45   allocated Fts3E
32f78 78 70 72 20 73 74 72 75 63 74 75 72 65 2e 20 4f  xpr structure. O
32f79 74 68 65 72 77 69 73 65 2c 20 65 69 74 68 65 72  therwise, either
32f7a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28 6f   SQLITE_NOMEM (o
32f7b 75 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ut of memory.** 
32f7c 65 72 72 6f 72 29 20 6f 72 20 53 51 4c 49 54 45  error) or SQLITE
32f7d 5f 45 52 52 4f 52 20 28 74 6f 6b 65 6e 69 7a 61  _ERROR (tokeniza
32f7e 74 69 6f 6e 20 65 72 72 6f 72 29 20 69 73 20 72  tion error) is r
32f7f 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45  eturned and *ppE
32f80 78 70 72 20 73 65 74 0a 2a 2a 20 74 6f 20 30 2e  xpr set.** to 0.
32f81 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
32f82 65 74 4e 65 78 74 53 74 72 69 6e 67 28 0a 20 20  etNextString(.  
32f83 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50  ParseContext *pP
32f84 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
32f85 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20          /* fts3 
32f86 71 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74  query parse cont
32f87 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
32f88 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e 74  har *zInput, int
32f89 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20   nInput,        
32f8a 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67   /* Input string
32f8b 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
32f8c 2a 70 70 45 78 70 72 20 20 20 20 20 20 20 20 20  *ppExpr         
32f8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32f8e 20 4f 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e   OUT: expression
32f8f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
32f90 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
32f91 65 6e 69 7a 65 72 20 3d 20 70 50 61 72 73 65 2d  enizer = pParse-
32f92 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73  >pTokenizer;.  s
32f93 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
32f94 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70  _module const *p
32f95 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69  Module = pTokeni
32f96 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  zer->pModule;.  
32f97 69 6e 74 20 72 63 3b 0a 20 20 46 74 73 33 45 78  int rc;.  Fts3Ex
32f98 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20 73 71 6c  pr *p = 0;.  sql
32f99 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
32f9a 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 20 3d  ursor *pCursor =
32f9b 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 6d   0;.  char *zTem
32f9c 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 54 65  p = 0;.  int nTe
32f9d 6d 70 20 3d 20 30 3b 0a 0a 20 20 72 63 20 3d 20  mp = 0;..  rc = 
32f9e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70  pModule->xOpen(p
32f9f 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 49 6e 70 75  Tokenizer, zInpu
32fa0 74 2c 20 6e 49 6e 70 75 74 2c 20 26 70 43 75 72  t, nInput, &pCur
32fa1 73 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  sor);.  if( rc==
32fa2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
32fa3 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 70 43 75   int ii;.    pCu
32fa4 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  rsor->pTokenizer
32fa5 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20   = pTokenizer;. 
32fa6 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d     for(ii=0; rc=
32fa7 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b  =SQLITE_OK; ii++
32fa8 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
32fa9 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 20  har *zToken;.   
32faa 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 69     int nToken, i
32fab 42 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69 50 6f  Begin, iEnd, iPo
32fac 73 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 4d  s;.      rc = pM
32fad 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75  odule->xNext(pCu
32fae 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26  rsor, &zToken, &
32faf 6e 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c  nToken, &iBegin,
32fb0 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a   &iEnd, &iPos);.
32fb1 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
32fb2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
32fb3 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73     int nByte = s
32fb4 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 20  izeof(Fts3Expr) 
32fb5 2b 20 73 69 7a 65 6f 66 28 46 74 73 33 50 68 72  + sizeof(Fts3Phr
32fb6 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 70 20  ase);.        p 
32fb7 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46  = fts3ReallocOrF
32fb8 72 65 65 28 70 2c 20 6e 42 79 74 65 2b 69 69 2a  ree(p, nByte+ii*
32fb9 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 50 68  sizeof(struct Ph
32fba 72 61 73 65 54 6f 6b 65 6e 29 29 3b 0a 20 20 20  raseToken));.   
32fbb 20 20 20 20 20 7a 54 65 6d 70 20 3d 20 66 74 73       zTemp = fts
32fbc 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 7a  3ReallocOrFree(z
32fbd 54 65 6d 70 2c 20 6e 54 65 6d 70 20 2b 20 6e 54  Temp, nTemp + nT
32fbe 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 69  oken);.        i
32fbf 66 28 20 21 70 20 7c 7c 20 21 7a 54 65 6d 70 20  f( !p || !zTemp 
32fc0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
32fc1 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20  o no_mem;.      
32fc2 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
32fc3 69 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ii==0 ){.       
32fc4 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20     memset(p, 0, 
32fc5 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20  nByte);.        
32fc6 20 20 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28    p->pPhrase = (
32fc7 46 74 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b  Fts3Phrase *)&p[
32fc8 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d  1];.          p-
32fc9 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55 45 52  >eType = FTSQUER
32fca 59 5f 50 48 52 41 53 45 3b 0a 20 20 20 20 20 20  Y_PHRASE;.      
32fcb 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e      p->pPhrase->
32fcc 69 43 6f 6c 75 6d 6e 20 3d 20 70 50 61 72 73 65  iColumn = pParse
32fcd 2d 3e 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20  ->iDefaultCol;. 
32fce 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32fcf 20 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46   p->pPhrase = (F
32fd0 74 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31  ts3Phrase *)&p[1
32fd1 5d 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50  ];.        p->pP
32fd2 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20  hrase->nToken = 
32fd3 69 69 2b 31 3b 0a 20 20 20 20 20 20 20 20 70 2d  ii+1;.        p-
32fd4 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e  >pPhrase->aToken
32fd5 5b 69 69 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b  [ii].n = nToken;
32fd6 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
32fd7 26 7a 54 65 6d 70 5b 6e 54 65 6d 70 5d 2c 20 7a  &zTemp[nTemp], z
32fd8 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a  Token, nToken);.
32fd9 20 20 20 20 20 20 20 20 6e 54 65 6d 70 20 2b 3d          nTemp +=
32fda 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20   nToken;.       
32fdb 20 69 66 28 20 69 45 6e 64 3c 6e 49 6e 70 75 74   if( iEnd<nInput
32fdc 20 26 26 20 7a 49 6e 70 75 74 5b 69 45 6e 64 5d   && zInput[iEnd]
32fdd 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20  =='*' ){.       
32fde 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61     p->pPhrase->a
32fdf 54 6f 6b 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66  Token[ii].isPref
32fe0 69 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ix = 1;.        
32fe1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
32fe2 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f   p->pPhrase->aTo
32fe3 6b 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69 78  ken[ii].isPrefix
32fe4 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
32fe5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
32fe6 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f     pModule->xClo
32fe7 73 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20  se(pCursor);.   
32fe8 20 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20   pCursor = 0;.  
32fe9 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
32fea 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
32feb 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 63 68 61 72  int jj;.    char
32fec 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20   *zNew;.    int 
32fed 6e 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e  nNew = 0;.    in
32fee 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66  t nByte = sizeof
32fef 28 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a  (Fts3Expr) + siz
32ff0 65 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 3b  eof(Fts3Phrase);
32ff1 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 28 70  .    nByte += (p
32ff2 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  ->pPhrase->nToke
32ff3 6e 2d 31 29 20 2a 20 73 69 7a 65 6f 66 28 73 74  n-1) * sizeof(st
32ff4 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e  ruct PhraseToken
32ff5 29 3b 0a 20 20 20 20 70 20 3d 20 66 74 73 33 52  );.    p = fts3R
32ff6 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2c 20  eallocOrFree(p, 
32ff7 6e 42 79 74 65 20 2b 20 6e 54 65 6d 70 29 3b 0a  nByte + nTemp);.
32ff8 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20      if( !p ){.  
32ff9 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
32ffa 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50  .    }.    p->pP
32ffb 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68 72  hrase = (Fts3Phr
32ffc 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  ase *)&p[1];.   
32ffd 20 7a 4e 65 77 20 3d 20 26 28 28 28 63 68 61 72   zNew = &(((char
32ffe 20 2a 29 70 29 5b 6e 42 79 74 65 5d 29 3b 0a 20   *)p)[nByte]);. 
32fff 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20     memcpy(zNew, 
33000 7a 54 65 6d 70 2c 20 6e 54 65 6d 70 29 3b 0a 20  zTemp, nTemp);. 
33001 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c     for(jj=0; jj<
33002 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  p->pPhrase->nTok
33003 65 6e 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20  en; jj++){.     
33004 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f   p->pPhrase->aTo
33005 6b 65 6e 5b 6a 6a 5d 2e 7a 20 3d 20 26 7a 4e 65  ken[jj].z = &zNe
33006 77 5b 6e 4e 65 77 5d 3b 0a 20 20 20 20 20 20 6e  w[nNew];.      n
33007 4e 65 77 20 2b 3d 20 70 2d 3e 70 50 68 72 61 73  New += p->pPhras
33008 65 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e 6e 3b  e->aToken[jj].n;
33009 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
3300a 65 33 5f 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a  e3_free(zTemp);.
3300b 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
3300c 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 45 78  OK;.  }..  *ppEx
3300d 70 72 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e  pr = p;.  return
3300e 20 72 63 3b 0a 6e 6f 5f 6d 65 6d 3a 0a 0a 20 20   rc;.no_mem:..  
3300f 69 66 28 20 70 43 75 72 73 6f 72 20 29 7b 0a 20  if( pCursor ){. 
33010 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f     pModule->xClo
33011 73 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  se(pCursor);.  }
33012 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
33013 7a 54 65 6d 70 29 3b 0a 20 20 73 71 6c 69 74 65  zTemp);.  sqlite
33014 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 2a 70 70  3_free(p);.  *pp
33015 45 78 70 72 20 3d 20 30 3b 0a 20 20 72 65 74 75  Expr = 0;.  retu
33016 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
33017 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  .}../*.** Functi
33018 6f 6e 20 67 65 74 4e 65 78 74 4e 6f 64 65 28 29  on getNextNode()
33019 2c 20 77 68 69 63 68 20 69 73 20 63 61 6c 6c 65  , which is calle
3301a 64 20 62 79 20 66 74 73 33 45 78 70 72 50 61 72  d by fts3ExprPar
3301b 73 65 28 29 2c 20 6d 61 79 20 69 74 73 65 6c 66  se(), may itself
3301c 0a 2a 2a 20 63 61 6c 6c 20 66 74 73 33 45 78 70  .** call fts3Exp
3301d 72 50 61 72 73 65 28 29 2e 20 53 6f 20 74 68 69  rParse(). So thi
3301e 73 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72  s forward declar
3301f 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65  ation is require
33020 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
33021 20 66 74 73 33 45 78 70 72 50 61 72 73 65 28 50   fts3ExprParse(P
33022 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 2c 20 63  arseContext *, c
33023 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
33024 2c 20 46 74 73 33 45 78 70 72 20 2a 2a 2c 20 69  , Fts3Expr **, i
33025 6e 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  nt *);../*.** Th
33026 65 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  e output variabl
33027 65 20 2a 70 70 45 78 70 72 20 69 73 20 70 6f 70  e *ppExpr is pop
33028 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e 20 61  ulated with an a
33029 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 45 78 70  llocated Fts3Exp
3302a 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c  r .** structure,
3302b 20 6f 72 20 73 65 74 20 74 6f 20 30 20 69 66 20   or set to 0 if 
3302c 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
3302d 6e 70 75 74 20 62 75 66 66 65 72 20 69 73 20 72  nput buffer is r
3302e 65 61 63 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  eached..**.** Re
3302f 74 75 72 6e 73 20 61 6e 20 53 51 4c 69 74 65 20  turns an SQLite 
33030 65 72 72 6f 72 20 63 6f 64 65 2e 20 53 51 4c 49  error code. SQLI
33031 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68  TE_OK if everyth
33032 69 6e 67 20 77 6f 72 6b 73 2c 20 53 51 4c 49 54  ing works, SQLIT
33033 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 66 20 61 20  E_NOMEM.** if a 
33034 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f  malloc failure o
33035 63 63 75 72 73 2c 20 6f 72 20 53 51 4c 49 54 45  ccurs, or SQLITE
33036 5f 45 52 52 4f 52 20 69 66 20 61 20 70 61 72 73  _ERROR if a pars
33037 65 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  e error is encou
33038 6e 74 65 72 65 64 2e 0a 2a 2a 20 49 66 20 53 51  ntered..** If SQ
33039 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65  LITE_ERROR is re
3303a 74 75 72 6e 65 64 2c 20 70 43 6f 6e 74 65 78 74  turned, pContext
3303b 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69   is populated wi
3303c 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
3303d 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  age..*/.static i
3303e 6e 74 20 67 65 74 4e 65 78 74 4e 6f 64 65 28 0a  nt getNextNode(.
3303f 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a    ParseContext *
33040 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
33041 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73            /* fts
33042 33 20 71 75 65 72 79 20 70 61 72 73 65 20 63 6f  3 query parse co
33043 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ntext */.  const
33044 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
33045 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33046 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69     /* Input stri
33047 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72  ng */.  Fts3Expr
33048 20 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20   **ppExpr,      
33049 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3304a 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65 73 73 69  /* OUT: expressi
3304b 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  on */.  int *pnC
3304c 6f 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20 20  onsumed         
3304d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3304e 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f  /* OUT: Number o
3304f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64  f bytes consumed
33050 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20   */.){.  static 
33051 63 6f 6e 73 74 20 73 74 72 75 63 74 20 46 74 73  const struct Fts
33052 33 4b 65 79 77 6f 72 64 20 7b 0a 20 20 20 20 63  3Keyword {.    c
33053 68 61 72 20 7a 5b 34 5d 3b 20 20 20 20 20 20 20  har z[4];       
33054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33055 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64 20       /* Keyword 
33056 74 65 78 74 20 2a 2f 0a 20 20 20 20 75 6e 73 69  text */.    unsi
33057 67 6e 65 64 20 63 68 61 72 20 6e 3b 20 20 20 20  gned char n;    
33058 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33059 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74    /* Length of t
3305a 68 65 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 20 20  he keyword */.  
3305b 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
3305c 70 61 72 65 6e 4f 6e 6c 79 3b 20 20 20 20 20 20  parenOnly;      
3305d 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20          /* Only 
3305e 76 61 6c 69 64 20 69 6e 20 70 61 72 65 6e 20 6d  valid in paren m
3305f 6f 64 65 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67  ode */.    unsig
33060 6e 65 64 20 63 68 61 72 20 65 54 79 70 65 3b 20  ned char eType; 
33061 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33062 20 2f 2a 20 4b 65 79 77 6f 72 64 20 63 6f 64 65   /* Keyword code
33063 20 2a 2f 0a 20 20 7d 20 61 4b 65 79 77 6f 72 64   */.  } aKeyword
33064 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 4f 52  [] = {.    { "OR
33065 22 20 2c 20 20 32 2c 20 30 2c 20 46 54 53 51 55  " ,  2, 0, FTSQU
33066 45 52 59 5f 4f 52 20 20 20 7d 2c 0a 20 20 20 20  ERY_OR   },.    
33067 7b 20 22 41 4e 44 22 2c 20 20 33 2c 20 31 2c 20  { "AND",  3, 1, 
33068 46 54 53 51 55 45 52 59 5f 41 4e 44 20 20 7d 2c  FTSQUERY_AND  },
33069 0a 20 20 20 20 7b 20 22 4e 4f 54 22 2c 20 20 33  .    { "NOT",  3
3306a 2c 20 31 2c 20 46 54 53 51 55 45 52 59 5f 4e 4f  , 1, FTSQUERY_NO
3306b 54 20 20 7d 2c 0a 20 20 20 20 7b 20 22 4e 45 41  T  },.    { "NEA
3306c 52 22 2c 20 34 2c 20 30 2c 20 46 54 53 51 55 45  R", 4, 0, FTSQUE
3306d 52 59 5f 4e 45 41 52 20 7d 0a 20 20 7d 3b 0a 20  RY_NEAR }.  };. 
3306e 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 69   int ii;.  int i
3306f 43 6f 6c 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 4c  Col;.  int iColL
33070 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  en;.  int rc;.  
33071 46 74 73 33 45 78 70 72 20 2a 70 52 65 74 20 3d  Fts3Expr *pRet =
33072 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61   0;..  const cha
33073 72 20 2a 7a 49 6e 70 75 74 20 3d 20 7a 3b 0a 20  r *zInput = z;. 
33074 20 69 6e 74 20 6e 49 6e 70 75 74 20 3d 20 6e 3b   int nInput = n;
33075 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72  ..  /* Skip over
33076 20 61 6e 79 20 77 68 69 74 65 73 70 61 63 65 20   any whitespace 
33077 62 65 66 6f 72 65 20 63 68 65 63 6b 69 6e 67 20  before checking 
33078 66 6f 72 20 61 20 6b 65 79 77 6f 72 64 2c 20 61  for a keyword, a
33079 6e 20 6f 70 65 6e 20 6f 72 0a 20 20 2a 2a 20 63  n open or.  ** c
3307a 6c 6f 73 65 20 62 72 61 63 6b 65 74 2c 20 6f 72  lose bracket, or
3307b 20 61 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67   a quoted string
3307c 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  . .  */.  while(
3307d 20 6e 49 6e 70 75 74 3e 30 20 26 26 20 66 74 73   nInput>0 && fts
3307e 33 69 73 73 70 61 63 65 28 2a 7a 49 6e 70 75 74  3isspace(*zInput
3307f 29 20 29 7b 0a 20 20 20 20 6e 49 6e 70 75 74 2d  ) ){.    nInput-
33080 2d 3b 0a 20 20 20 20 7a 49 6e 70 75 74 2b 2b 3b  -;.    zInput++;
33081 0a 20 20 7d 0a 20 20 69 66 28 20 6e 49 6e 70 75  .  }.  if( nInpu
33082 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  t==0 ){.    retu
33083 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
33084 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66    }..  /* See if
33085 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20   we are dealing 
33086 77 69 74 68 20 61 20 6b 65 79 77 6f 72 64 2e 20  with a keyword. 
33087 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  */.  for(ii=0; i
33088 69 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 61  i<(int)(sizeof(a
33089 4b 65 79 77 6f 72 64 29 2f 73 69 7a 65 6f 66 28  Keyword)/sizeof(
3308a 73 74 72 75 63 74 20 46 74 73 33 4b 65 79 77 6f  struct Fts3Keywo
3308b 72 64 29 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  rd)); ii++){.   
3308c 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 46 74   const struct Ft
3308d 73 33 4b 65 79 77 6f 72 64 20 2a 70 4b 65 79 20  s3Keyword *pKey 
3308e 3d 20 26 61 4b 65 79 77 6f 72 64 5b 69 69 5d 3b  = &aKeyword[ii];
3308f 0a 0a 20 20 20 20 69 66 28 20 28 70 4b 65 79 2d  ..    if( (pKey-
33090 3e 70 61 72 65 6e 4f 6e 6c 79 20 26 20 7e 73 71  >parenOnly & ~sq
33091 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
33092 65 5f 70 61 72 65 6e 74 68 65 73 65 73 29 21 3d  e_parentheses)!=
33093 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69  0 ){.      conti
33094 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  nue;.    }..    
33095 69 66 28 20 6e 49 6e 70 75 74 3e 3d 70 4b 65 79  if( nInput>=pKey
33096 2d 3e 6e 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70  ->n && 0==memcmp
33097 28 7a 49 6e 70 75 74 2c 20 70 4b 65 79 2d 3e 7a  (zInput, pKey->z
33098 2c 20 70 4b 65 79 2d 3e 6e 29 20 29 7b 0a 20 20  , pKey->n) ){.  
33099 20 20 20 20 69 6e 74 20 6e 4e 65 61 72 20 3d 20      int nNear = 
3309a 53 51 4c 49 54 45 5f 46 54 53 33 5f 44 45 46 41  SQLITE_FTS3_DEFA
3309b 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d 3b 0a  ULT_NEAR_PARAM;.
3309c 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 20 3d        int nKey =
3309d 20 70 4b 65 79 2d 3e 6e 3b 0a 20 20 20 20 20 20   pKey->n;.      
3309e 63 68 61 72 20 63 4e 65 78 74 3b 0a 0a 20 20 20  char cNext;..   
3309f 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
330a0 20 61 20 22 4e 45 41 52 22 20 6b 65 79 77 6f 72   a "NEAR" keywor
330a1 64 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 6e 20  d, check for an 
330a2 65 78 70 6c 69 63 69 74 20 6e 65 61 72 6e 65 73  explicit nearnes
330a3 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  s. */.      if( 
330a4 70 4b 65 79 2d 3e 65 54 79 70 65 3d 3d 46 54 53  pKey->eType==FTS
330a5 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20  QUERY_NEAR ){.  
330a6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 4b        assert( nK
330a7 65 79 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 20  ey==4 );.       
330a8 20 69 66 28 20 7a 49 6e 70 75 74 5b 34 5d 3d 3d   if( zInput[4]==
330a9 27 2f 27 20 26 26 20 7a 49 6e 70 75 74 5b 35 5d  '/' && zInput[5]
330aa 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70 75 74 5b  >='0' && zInput[
330ab 35 5d 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20  5]<='9' ){.     
330ac 20 20 20 20 20 6e 4e 65 61 72 20 3d 20 30 3b 0a       nNear = 0;.
330ad 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e 4b            for(nK
330ae 65 79 3d 35 3b 20 7a 49 6e 70 75 74 5b 6e 4b 65  ey=5; zInput[nKe
330af 79 5d 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70 75  y]>='0' && zInpu
330b0 74 5b 6e 4b 65 79 5d 3c 3d 27 39 27 3b 20 6e 4b  t[nKey]<='9'; nK
330b1 65 79 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  ey++){.         
330b2 20 20 20 6e 4e 65 61 72 20 3d 20 6e 4e 65 61 72     nNear = nNear
330b3 20 2a 20 31 30 20 2b 20 28 7a 49 6e 70 75 74 5b   * 10 + (zInput[
330b4 6e 4b 65 79 5d 20 2d 20 27 30 27 29 3b 0a 20 20  nKey] - '0');.  
330b5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
330b6 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
330b7 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f     /* At this po
330b8 69 6e 74 20 74 68 69 73 20 69 73 20 70 72 6f 62  int this is prob
330b9 61 62 6c 79 20 61 20 6b 65 79 77 6f 72 64 2e 20  ably a keyword. 
330ba 42 75 74 20 66 6f 72 20 74 68 61 74 20 74 6f 20  But for that to 
330bb 62 65 20 74 72 75 65 2c 0a 20 20 20 20 20 20 2a  be true,.      *
330bc 2a 20 74 68 65 20 6e 65 78 74 20 62 79 74 65 20  * the next byte 
330bd 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 65 69 74  must contain eit
330be 68 65 72 20 77 68 69 74 65 73 70 61 63 65 2c 20  her whitespace, 
330bf 61 6e 20 6f 70 65 6e 20 6f 72 20 63 6c 6f 73 65  an open or close
330c0 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 65 6e 74  .      ** parent
330c1 68 65 73 69 73 2c 20 61 20 71 75 6f 74 65 20 63  hesis, a quote c
330c2 68 61 72 61 63 74 65 72 2c 20 6f 72 20 45 4f 46  haracter, or EOF
330c3 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  . .      */.    
330c4 20 20 63 4e 65 78 74 20 3d 20 7a 49 6e 70 75 74    cNext = zInput
330c5 5b 6e 4b 65 79 5d 3b 0a 20 20 20 20 20 20 69 66  [nKey];.      if
330c6 28 20 66 74 73 33 69 73 73 70 61 63 65 28 63 4e  ( fts3isspace(cN
330c7 65 78 74 29 20 0a 20 20 20 20 20 20 20 7c 7c 20  ext) .       || 
330c8 63 4e 65 78 74 3d 3d 27 22 27 20 7c 7c 20 63 4e  cNext=='"' || cN
330c9 65 78 74 3d 3d 27 28 27 20 7c 7c 20 63 4e 65 78  ext=='(' || cNex
330ca 74 3d 3d 27 29 27 20 7c 7c 20 63 4e 65 78 74 3d  t==')' || cNext=
330cb 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  =0.      ){.    
330cc 20 20 20 20 70 52 65 74 20 3d 20 28 46 74 73 33      pRet = (Fts3
330cd 45 78 70 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d  Expr *)sqlite3_m
330ce 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73  alloc(sizeof(Fts
330cf 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20  3Expr));.       
330d0 20 6d 65 6d 73 65 74 28 70 52 65 74 2c 20 30 2c   memset(pRet, 0,
330d1 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72   sizeof(Fts3Expr
330d2 29 29 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74  ));.        pRet
330d3 2d 3e 65 54 79 70 65 20 3d 20 70 4b 65 79 2d 3e  ->eType = pKey->
330d4 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 70  eType;.        p
330d5 52 65 74 2d 3e 6e 4e 65 61 72 20 3d 20 6e 4e 65  Ret->nNear = nNe
330d6 61 72 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 45  ar;.        *ppE
330d7 78 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 20 20  xpr = pRet;.    
330d8 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20      *pnConsumed 
330d9 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b  = (zInput - z) +
330da 20 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 72   nKey;.        r
330db 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
330dc 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
330dd 2f 2a 20 54 75 72 6e 73 20 6f 75 74 20 74 68 61  /* Turns out tha
330de 74 20 77 61 73 6e 27 74 20 61 20 6b 65 79 77 6f  t wasn't a keywo
330df 72 64 20 61 66 74 65 72 20 61 6c 6c 2e 20 54 68  rd after all. Th
330e0 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 74 68  is happens if th
330e1 65 0a 20 20 20 20 20 20 2a 2a 20 75 73 65 72 20  e.      ** user 
330e2 68 61 73 20 73 75 70 70 6c 69 65 64 20 61 20 74  has supplied a t
330e3 6f 6b 65 6e 20 73 75 63 68 20 61 73 20 22 4f 52  oken such as "OR
330e4 61 63 6c 65 22 2e 20 43 6f 6e 74 69 6e 75 65 2e  acle". Continue.
330e5 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 7d 0a  .      */.    }.
330e6 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
330e7 66 6f 72 20 61 6e 20 6f 70 65 6e 20 62 72 61 63  for an open brac
330e8 6b 65 74 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71  ket. */.  if( sq
330e9 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
330ea 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 29 7b  e_parentheses ){
330eb 0a 20 20 20 20 69 66 28 20 2a 7a 49 6e 70 75 74  .    if( *zInput
330ec 3d 3d 27 28 27 20 29 7b 0a 20 20 20 20 20 20 69  =='(' ){.      i
330ed 6e 74 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a 20 20  nt nConsumed;.  
330ee 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
330ef 20 20 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2b    pParse->nNest+
330f0 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74  +;.      rc = ft
330f1 73 33 45 78 70 72 50 61 72 73 65 28 70 50 61 72  s3ExprParse(pPar
330f2 73 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d 2c 20  se, &zInput[1], 
330f3 6e 49 6e 70 75 74 2d 31 2c 20 70 70 45 78 70 72  nInput-1, ppExpr
330f4 2c 20 26 6e 43 6f 6e 73 75 6d 65 64 29 3b 0a 20  , &nConsumed);. 
330f5 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
330f6 49 54 45 5f 4f 4b 20 26 26 20 21 2a 70 70 45 78  ITE_OK && !*ppEx
330f7 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  pr ){.        rc
330f8 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a   = SQLITE_DONE;.
330f9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70        }.      *p
330fa 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e  nConsumed = (zIn
330fb 70 75 74 20 2d 20 7a 29 20 2b 20 31 20 2b 20 6e  put - z) + 1 + n
330fc 43 6f 6e 73 75 6d 65 64 3b 0a 20 20 20 20 20 20  Consumed;.      
330fd 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
330fe 0a 20 20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  .  .    /* Check
330ff 20 66 6f 72 20 61 20 63 6c 6f 73 65 20 62 72 61   for a close bra
33100 63 6b 65 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28  cket. */.    if(
33101 20 2a 7a 49 6e 70 75 74 3d 3d 27 29 27 20 29 7b   *zInput==')' ){
33102 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
33103 4e 65 73 74 2d 2d 3b 0a 20 20 20 20 20 20 2a 70  Nest--;.      *p
33104 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e  nConsumed = (zIn
33105 70 75 74 20 2d 20 7a 29 20 2b 20 31 3b 0a 20 20  put - z) + 1;.  
33106 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
33107 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20  E_DONE;.    }.  
33108 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 77  }..  /* See if w
33109 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
3310a 74 68 20 61 20 71 75 6f 74 65 64 20 70 68 72 61  th a quoted phra
3310b 73 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 74  se. If this is t
3310c 68 65 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20  he case, then.  
3310d 2a 2a 20 73 65 61 72 63 68 20 66 6f 72 20 74 68  ** search for th
3310e 65 20 63 6c 6f 73 69 6e 67 20 71 75 6f 74 65 20  e closing quote 
3310f 61 6e 64 20 70 61 73 73 20 74 68 65 20 77 68 6f  and pass the who
33110 6c 65 20 73 74 72 69 6e 67 20 74 6f 20 67 65 74  le string to get
33111 4e 65 78 74 53 74 72 69 6e 67 28 29 0a 20 20 2a  NextString().  *
33112 2a 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  * for processing
33113 2e 20 54 68 69 73 20 69 73 20 65 61 73 79 20 74  . This is easy t
33114 6f 20 64 6f 2c 20 61 73 20 66 74 73 33 20 68 61  o do, as fts3 ha
33115 73 20 6e 6f 20 73 79 6e 74 61 78 20 66 6f 72 20  s no syntax for 
33116 65 73 63 61 70 69 6e 67 0a 20 20 2a 2a 20 61 20  escaping.  ** a 
33117 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 20  quote character 
33118 65 6d 62 65 64 64 65 64 20 69 6e 20 61 20 73 74  embedded in a st
33119 72 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ring..  */.  if(
3311a 20 2a 7a 49 6e 70 75 74 3d 3d 27 22 27 20 29 7b   *zInput=='"' ){
3311b 0a 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20 69  .    for(ii=1; i
3311c 69 3c 6e 49 6e 70 75 74 20 26 26 20 7a 49 6e 70  i<nInput && zInp
3311d 75 74 5b 69 69 5d 21 3d 27 22 27 3b 20 69 69 2b  ut[ii]!='"'; ii+
3311e 2b 29 3b 0a 20 20 20 20 2a 70 6e 43 6f 6e 73 75  +);.    *pnConsu
3311f 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d 20  med = (zInput - 
33120 7a 29 20 2b 20 69 69 20 2b 20 31 3b 0a 20 20 20  z) + ii + 1;.   
33121 20 69 66 28 20 69 69 3d 3d 6e 49 6e 70 75 74 20   if( ii==nInput 
33122 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
33123 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
33124 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 67    }.    return g
33125 65 74 4e 65 78 74 53 74 72 69 6e 67 28 70 50 61  etNextString(pPa
33126 72 73 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d 2c  rse, &zInput[1],
33127 20 69 69 2d 31 2c 20 70 70 45 78 70 72 29 3b 0a   ii-1, ppExpr);.
33128 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f    }...  /* If co
33129 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74  ntrol flows to t
3312a 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 69 73 20  his point, this 
3312b 6d 75 73 74 20 62 65 20 61 20 72 65 67 75 6c 61  must be a regula
3312c 72 20 74 6f 6b 65 6e 2c 20 6f 72 20 0a 20 20 2a  r token, or .  *
3312d 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * the end of the
3312e 20 69 6e 70 75 74 2e 20 52 65 61 64 20 61 20 72   input. Read a r
3312f 65 67 75 6c 61 72 20 74 6f 6b 65 6e 20 75 73 69  egular token usi
33130 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74  ng the sqlite3_t
33131 6f 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6e  okenizer.  ** in
33132 74 65 72 66 61 63 65 2e 20 42 65 66 6f 72 65 20  terface. Before 
33133 64 6f 69 6e 67 20 73 6f 2c 20 66 69 67 75 72 65  doing so, figure
33134 20 6f 75 74 20 69 66 20 74 68 65 72 65 20 69 73   out if there is
33135 20 61 6e 20 65 78 70 6c 69 63 69 74 0a 20 20 2a   an explicit.  *
33136 2a 20 63 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69  * column specifi
33137 65 72 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e  er for the token
33138 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44  . .  **.  ** TOD
33139 4f 3a 20 53 74 72 61 6e 67 65 6c 79 2c 20 69 74  O: Strangely, it
3313a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
3313b 20 74 6f 20 61 73 73 6f 63 69 61 74 65 20 61 20   to associate a 
3313c 63 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69 65 72  column specifier
3313d 0a 20 20 2a 2a 20 77 69 74 68 20 61 20 71 75 6f  .  ** with a quo
3313e 74 65 64 20 70 68 72 61 73 65 2c 20 6f 6e 6c 79  ted phrase, only
3313f 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74   with a single t
33140 6f 6b 65 6e 2e 20 4e 6f 74 20 73 75 72 65 20 69  oken. Not sure i
33141 66 20 74 68 69 73 20 77 61 73 0a 20 20 2a 2a 20  f this was.  ** 
33142 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  an implementatio
33143 6e 20 61 72 74 69 66 61 63 74 20 6f 72 20 61 6e  n artifact or an
33144 20 69 6e 74 65 6e 74 69 6f 6e 61 6c 20 64 65 63   intentional dec
33145 69 73 69 6f 6e 20 77 68 65 6e 20 66 74 73 33 20  ision when fts3 
33146 77 61 73 0a 20 20 2a 2a 20 66 69 72 73 74 20 69  was.  ** first i
33147 6d 70 6c 65 6d 65 6e 74 65 64 2e 20 57 68 69 63  mplemented. Whic
33148 68 65 76 65 72 20 69 74 20 77 61 73 2c 20 74 68  hever it was, th
33149 69 73 20 6d 6f 64 75 6c 65 20 64 75 70 6c 69 63  is module duplic
3314a 61 74 65 73 20 74 68 65 20 0a 20 20 2a 2a 20 6c  ates the .  ** l
3314b 69 6d 69 74 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  imitation..  */.
3314c 20 20 69 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d    iCol = pParse-
3314d 3e 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20  >iDefaultCol;.  
3314e 69 43 6f 6c 4c 65 6e 20 3d 20 30 3b 0a 20 20 66  iColLen = 0;.  f
3314f 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 72  or(ii=0; ii<pPar
33150 73 65 2d 3e 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b  se->nCol; ii++){
33151 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
33152 2a 7a 53 74 72 20 3d 20 70 50 61 72 73 65 2d 3e  *zStr = pParse->
33153 61 7a 43 6f 6c 5b 69 69 5d 3b 0a 20 20 20 20 69  azCol[ii];.    i
33154 6e 74 20 6e 53 74 72 20 3d 20 73 74 72 6c 65 6e  nt nStr = strlen
33155 28 7a 53 74 72 29 3b 0a 20 20 20 20 69 66 28 20  (zStr);.    if( 
33156 6e 49 6e 70 75 74 3e 6e 53 74 72 20 26 26 20 7a  nInput>nStr && z
33157 49 6e 70 75 74 5b 6e 53 74 72 5d 3d 3d 27 3a 27  Input[nStr]==':'
33158 20 26 26 20 6d 65 6d 63 6d 70 28 7a 53 74 72 2c   && memcmp(zStr,
33159 20 7a 49 6e 70 75 74 2c 20 6e 53 74 72 29 3d 3d   zInput, nStr)==
3315a 30 20 29 7b 0a 20 20 20 20 20 20 69 43 6f 6c 20  0 ){.      iCol 
3315b 3d 20 69 69 3b 0a 20 20 20 20 20 20 69 43 6f 6c  = ii;.      iCol
3315c 4c 65 6e 20 3d 20 28 28 7a 49 6e 70 75 74 20 2d  Len = ((zInput -
3315d 20 7a 29 20 2b 20 6e 53 74 72 20 2b 20 31 29 3b   z) + nStr + 1);
3315e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3315f 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67    }.  }.  rc = g
33160 65 74 4e 65 78 74 54 6f 6b 65 6e 28 70 50 61 72  etNextToken(pPar
33161 73 65 2c 20 69 43 6f 6c 2c 20 26 7a 5b 69 43 6f  se, iCol, &z[iCo
33162 6c 4c 65 6e 5d 2c 20 6e 2d 69 43 6f 6c 4c 65 6e  lLen], n-iColLen
33163 2c 20 70 70 45 78 70 72 2c 20 70 6e 43 6f 6e 73  , ppExpr, pnCons
33164 75 6d 65 64 29 3b 0a 20 20 2a 70 6e 43 6f 6e 73  umed);.  *pnCons
33165 75 6d 65 64 20 2b 3d 20 69 43 6f 6c 4c 65 6e 3b  umed += iColLen;
33166 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
33167 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  ./*.** The argum
33168 65 6e 74 20 69 73 20 61 6e 20 46 74 73 33 45 78  ent is an Fts3Ex
33169 70 72 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  pr structure for
3316a 20 61 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74   a binary operat
3316b 6f 72 20 28 61 6e 79 20 74 79 70 65 0a 2a 2a 20  or (any type.** 
3316c 65 78 63 65 70 74 20 61 6e 20 46 54 53 51 55 45  except an FTSQUE
3316d 52 59 5f 50 48 52 41 53 45 29 2e 20 52 65 74 75  RY_PHRASE). Retu
3316e 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 76 61  rn an integer va
3316f 6c 75 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67  lue representing
33170 20 74 68 65 0a 2a 2a 20 70 72 65 63 65 64 65 6e   the.** preceden
33171 63 65 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  ce of the operat
33172 6f 72 2e 20 4c 6f 77 65 72 20 76 61 6c 75 65 73  or. Lower values
33173 20 68 61 76 65 20 61 20 68 69 67 68 65 72 20 70   have a higher p
33174 72 65 63 65 64 65 6e 63 65 20 28 69 2e 65 2e 0a  recedence (i.e..
33175 2a 2a 20 67 72 6f 75 70 20 6d 6f 72 65 20 74 69  ** group more ti
33176 67 68 74 6c 79 29 2e 20 46 6f 72 20 65 78 61 6d  ghtly). For exam
33177 70 6c 65 2c 20 69 6e 20 74 68 65 20 43 20 6c 61  ple, in the C la
33178 6e 67 75 61 67 65 2c 20 74 68 65 20 3d 3d 20 6f  nguage, the == o
33179 70 65 72 61 74 6f 72 0a 2a 2a 20 67 72 6f 75 70  perator.** group
3317a 73 20 6d 6f 72 65 20 74 69 67 68 74 6c 79 20 74  s more tightly t
3317b 68 61 6e 20 7c 7c 2c 20 61 6e 64 20 77 6f 75 6c  han ||, and woul
3317c 64 20 74 68 65 72 65 66 6f 72 65 20 68 61 76 65  d therefore have
3317d 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65 64   a higher preced
3317e 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  ence..**.** When
3317f 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 20 66   using the new f
33180 74 73 33 20 71 75 65 72 79 20 73 79 6e 74 61 78  ts3 query syntax
33181 20 28 77 68 65 6e 20 53 51 4c 49 54 45 5f 45 4e   (when SQLITE_EN
33182 41 42 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e 54  ABLE_FTS3_PARENT
33183 48 45 53 49 53 0a 2a 2a 20 69 73 20 64 65 66 69  HESIS.** is defi
33184 6e 65 64 29 2c 20 74 68 65 20 6f 72 64 65 72 20  ned), the order 
33185 6f 66 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73  of the operators
33186 20 69 6e 20 70 72 65 63 65 64 65 6e 63 65 20 66   in precedence f
33187 72 6f 6d 20 68 69 67 68 65 73 74 20 74 6f 0a 2a  rom highest to.*
33188 2a 20 6c 6f 77 65 73 74 20 69 73 3a 0a 2a 2a 0a  * lowest is:.**.
33189 2a 2a 20 20 20 4e 45 41 52 0a 2a 2a 20 20 20 4e  **   NEAR.**   N
3318a 4f 54 0a 2a 2a 20 20 20 41 4e 44 20 28 69 6e 63  OT.**   AND (inc
3318b 6c 75 64 69 6e 67 20 69 6d 70 6c 69 63 69 74 20  luding implicit 
3318c 41 4e 44 73 29 0a 2a 2a 20 20 20 4f 52 0a 2a 2a  ANDs).**   OR.**
3318d 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68  .** Note that wh
3318e 65 6e 20 75 73 69 6e 67 20 74 68 65 20 6f 6c 64  en using the old
3318f 20 71 75 65 72 79 20 73 79 6e 74 61 78 2c 20 74   query syntax, t
33190 68 65 20 4f 52 20 6f 70 65 72 61 74 6f 72 20 68  he OR operator h
33191 61 73 20 61 20 68 69 67 68 65 72 0a 2a 2a 20 70  as a higher.** p
33192 72 65 63 65 64 65 6e 63 65 20 74 68 61 6e 20 74  recedence than t
33193 68 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e  he AND operator.
33194 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
33195 70 50 72 65 63 65 64 65 6e 63 65 28 46 74 73 33  pPrecedence(Fts3
33196 45 78 70 72 20 2a 70 29 7b 0a 20 20 61 73 73 65  Expr *p){.  asse
33197 72 74 28 20 70 2d 3e 65 54 79 70 65 21 3d 46 54  rt( p->eType!=FT
33198 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b  SQUERY_PHRASE );
33199 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 66  .  if( sqlite3_f
3319a 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
3319b 74 68 65 73 65 73 20 29 7b 0a 20 20 20 20 72 65  theses ){.    re
3319c 74 75 72 6e 20 70 2d 3e 65 54 79 70 65 3b 0a 20  turn p->eType;. 
3319d 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 54   }else if( p->eT
3319e 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45  ype==FTSQUERY_NE
3319f 41 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  AR ){.    return
331a0 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   1;.  }else if( 
331a1 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
331a2 52 59 5f 4f 52 20 29 7b 0a 20 20 20 20 72 65 74  RY_OR ){.    ret
331a3 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 2;.  }.  ass
331a4 65 72 74 28 20 70 2d 3e 65 54 79 70 65 3d 3d 46  ert( p->eType==F
331a5 54 53 51 55 45 52 59 5f 41 4e 44 20 29 3b 0a 20  TSQUERY_AND );. 
331a6 20 72 65 74 75 72 6e 20 33 3b 0a 7d 0a 0a 2f 2a   return 3;.}../*
331a7 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 70 48  .** Argument ppH
331a8 65 61 64 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  ead contains a p
331a9 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 75  ointer to the cu
331aa 72 72 65 6e 74 20 68 65 61 64 20 6f 66 20 61 20  rrent head of a 
331ab 71 75 65 72 79 20 0a 2a 2a 20 65 78 70 72 65 73  query .** expres
331ac 73 69 6f 6e 20 74 72 65 65 20 62 65 69 6e 67 20  sion tree being 
331ad 70 61 72 73 65 64 2e 20 70 50 72 65 76 20 69 73  parsed. pPrev is
331ae 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
331af 6e 6f 64 65 20 6d 6f 73 74 20 72 65 63 65 6e 74  node most recent
331b0 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69  ly.** inserted i
331b1 6e 74 6f 20 74 68 65 20 74 72 65 65 2e 20 54 68  nto the tree. Th
331b2 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 73  is function adds
331b3 20 70 4e 65 77 2c 20 77 68 69 63 68 20 69 73 20   pNew, which is 
331b4 61 6c 77 61 79 73 20 61 20 62 69 6e 61 72 79 0a  always a binary.
331b5 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6e 6f 64 65  ** operator node
331b6 2c 20 69 6e 74 6f 20 74 68 65 20 65 78 70 72 65  , into the expre
331b7 73 73 69 6f 6e 20 74 72 65 65 20 62 61 73 65 64  ssion tree based
331b8 20 6f 6e 20 74 68 65 20 72 65 6c 61 74 69 76 65   on the relative
331b9 20 70 72 65 63 65 64 65 6e 63 65 0a 2a 2a 20 6f   precedence.** o
331ba 66 20 70 4e 65 77 20 61 6e 64 20 74 68 65 20 65  f pNew and the e
331bb 78 69 73 74 69 6e 67 20 6e 6f 64 65 73 20 6f 66  xisting nodes of
331bc 20 74 68 65 20 74 72 65 65 2e 20 54 68 69 73 20   the tree. This 
331bd 6d 61 79 20 72 65 73 75 6c 74 20 69 6e 20 74 68  may result in th
331be 65 20 68 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65  e head.** of the
331bf 20 74 72 65 65 20 63 68 61 6e 67 69 6e 67 2c 20   tree changing, 
331c0 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 2a 70  in which case *p
331c1 70 48 65 61 64 20 69 73 20 73 65 74 20 74 6f 20  pHead is set to 
331c2 74 68 65 20 6e 65 77 20 72 6f 6f 74 20 6e 6f 64  the new root nod
331c3 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
331c4 64 20 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70  d insertBinaryOp
331c5 65 72 61 74 6f 72 28 0a 20 20 46 74 73 33 45 78  erator(.  Fts3Ex
331c6 70 72 20 2a 2a 70 70 48 65 61 64 2c 20 20 20 20  pr **ppHead,    
331c7 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
331c8 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f   the root node o
331c9 66 20 61 20 74 72 65 65 20 2a 2f 0a 20 20 46 74  f a tree */.  Ft
331ca 73 33 45 78 70 72 20 2a 70 50 72 65 76 2c 20 20  s3Expr *pPrev,  
331cb 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 6d         /* Node m
331cc 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73  ost recently ins
331cd 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74  erted into the t
331ce 72 65 65 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  ree */.  Fts3Exp
331cf 72 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20  r *pNew         
331d0 20 20 2f 2a 20 4e 65 77 20 62 69 6e 61 72 79 20    /* New binary 
331d1 6e 6f 64 65 20 74 6f 20 69 6e 73 65 72 74 20 69  node to insert i
331d2 6e 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74  nto expression t
331d3 72 65 65 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33  ree */.){.  Fts3
331d4 45 78 70 72 20 2a 70 53 70 6c 69 74 20 3d 20 70  Expr *pSplit = p
331d5 50 72 65 76 3b 0a 20 20 77 68 69 6c 65 28 20 70  Prev;.  while( p
331d6 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 20 26  Split->pParent &
331d7 26 20 6f 70 50 72 65 63 65 64 65 6e 63 65 28 70  & opPrecedence(p
331d8 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 29 3c  Split->pParent)<
331d9 3d 6f 70 50 72 65 63 65 64 65 6e 63 65 28 70 4e  =opPrecedence(pN
331da 65 77 29 20 29 7b 0a 20 20 20 20 70 53 70 6c 69  ew) ){.    pSpli
331db 74 20 3d 20 70 53 70 6c 69 74 2d 3e 70 50 61 72  t = pSplit->pPar
331dc 65 6e 74 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  ent;.  }..  if( 
331dd 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 20  pSplit->pParent 
331de 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
331df 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 2d 3e  Split->pParent->
331e0 70 52 69 67 68 74 3d 3d 70 53 70 6c 69 74 20 29  pRight==pSplit )
331e1 3b 0a 20 20 20 20 70 53 70 6c 69 74 2d 3e 70 50  ;.    pSplit->pP
331e2 61 72 65 6e 74 2d 3e 70 52 69 67 68 74 20 3d 20  arent->pRight = 
331e3 70 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77 2d 3e  pNew;.    pNew->
331e4 70 50 61 72 65 6e 74 20 3d 20 70 53 70 6c 69 74  pParent = pSplit
331e5 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 7d 65 6c  ->pParent;.  }el
331e6 73 65 7b 0a 20 20 20 20 2a 70 70 48 65 61 64 20  se{.    *ppHead 
331e7 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 4e  = pNew;.  }.  pN
331e8 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70 53 70 6c  ew->pLeft = pSpl
331e9 69 74 3b 0a 20 20 70 53 70 6c 69 74 2d 3e 70 50  it;.  pSplit->pP
331ea 61 72 65 6e 74 20 3d 20 70 4e 65 77 3b 0a 7d 0a  arent = pNew;.}.
331eb 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 68 65  ./*.** Parse the
331ec 20 66 74 73 33 20 71 75 65 72 79 20 65 78 70 72   fts3 query expr
331ed 65 73 73 69 6f 6e 20 66 6f 75 6e 64 20 69 6e 20  ession found in 
331ee 62 75 66 66 65 72 20 7a 2c 20 6c 65 6e 67 74 68  buffer z, length
331ef 20 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f   n. This functio
331f0 6e 0a 2a 2a 20 72 65 74 75 72 6e 73 20 65 69 74  n.** returns eit
331f1 68 65 72 20 77 68 65 6e 20 74 68 65 20 65 6e 64  her when the end
331f2 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 69   of the buffer i
331f3 73 20 72 65 61 63 68 65 64 20 6f 72 20 61 6e 20  s reached or an 
331f4 75 6e 6d 61 74 63 68 65 64 20 0a 2a 2a 20 63 6c  unmatched .** cl
331f5 6f 73 69 6e 67 20 62 72 61 63 6b 65 74 20 2d 20  osing bracket - 
331f6 27 29 27 20 2d 20 69 73 20 65 6e 63 6f 75 6e 74  ')' - is encount
331f7 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  ered..**.** If s
331f8 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54  uccessful, SQLIT
331f9 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
331fa 2c 20 2a 70 70 45 78 70 72 20 69 73 20 73 65 74  , *ppExpr is set
331fb 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
331fc 0a 2a 2a 20 70 61 72 73 65 64 20 66 6f 72 6d 20  .** parsed form 
331fd 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
331fe 6e 20 61 6e 64 20 2a 70 6e 43 6f 6e 73 75 6d 65  n and *pnConsume
331ff 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  d is set to the 
33200 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74  number of.** byt
33201 65 73 20 72 65 61 64 20 66 72 6f 6d 20 62 75 66  es read from buf
33202 66 65 72 20 7a 2e 20 4f 74 68 65 72 77 69 73 65  fer z. Otherwise
33203 2c 20 2a 70 70 45 78 70 72 20 69 73 20 73 65 74  , *ppExpr is set
33204 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45   to 0 and SQLITE
33205 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 6f 75 74 20 6f  _NOMEM.** (out o
33206 66 20 6d 65 6d 6f 72 79 20 65 72 72 6f 72 29 20  f memory error) 
33207 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  or SQLITE_ERROR 
33208 28 70 61 72 73 65 20 65 72 72 6f 72 29 20 69 73  (parse error) is
33209 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
3320a 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 78 70  atic int fts3Exp
3320b 72 50 61 72 73 65 28 0a 20 20 50 61 72 73 65 43  rParse(.  ParseC
3320c 6f 6e 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20  ontext *pParse, 
3320d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3320e 20 20 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20    /* fts3 query 
3320f 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  parse context */
33210 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
33211 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20  , int n,        
33212 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
33213 78 74 20 6f 66 20 4d 41 54 43 48 20 71 75 65 72  xt of MATCH quer
33214 79 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20  y */.  Fts3Expr 
33215 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20 20  **ppExpr,       
33216 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33217 2a 20 4f 55 54 3a 20 50 61 72 73 65 64 20 71 75  * OUT: Parsed qu
33218 65 72 79 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ery structure */
33219 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d  .  int *pnConsum
3321a 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ed              
3321b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
3321c 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
3321d 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29  es consumed */.)
3321e 7b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 52  {.  Fts3Expr *pR
3321f 65 74 20 3d 20 30 3b 0a 20 20 46 74 73 33 45 78  et = 0;.  Fts3Ex
33220 70 72 20 2a 70 50 72 65 76 20 3d 20 30 3b 0a 20  pr *pPrev = 0;. 
33221 20 46 74 73 33 45 78 70 72 20 2a 70 4e 6f 74 42   Fts3Expr *pNotB
33222 72 61 6e 63 68 20 3d 20 30 3b 20 20 20 20 20 20  ranch = 0;      
33223 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79           /* Only
33224 20 75 73 65 64 20 69 6e 20 6c 65 67 61 63 79 20   used in legacy 
33225 70 61 72 73 65 20 6d 6f 64 65 20 2a 2f 0a 20 20  parse mode */.  
33226 69 6e 74 20 6e 49 6e 20 3d 20 6e 3b 0a 20 20 63  int nIn = n;.  c
33227 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 20 3d  onst char *zIn =
33228 20 7a 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   z;.  int rc = S
33229 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
3322a 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
3322b 3d 20 31 3b 0a 0a 20 20 77 68 69 6c 65 28 20 72  = 1;..  while( r
3322c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3322d 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 20      Fts3Expr *p 
3322e 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79  = 0;.    int nBy
3322f 74 65 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  te = 0;.    rc =
33230 20 67 65 74 4e 65 78 74 4e 6f 64 65 28 70 50 61   getNextNode(pPa
33231 72 73 65 2c 20 7a 49 6e 2c 20 6e 49 6e 2c 20 26  rse, zIn, nIn, &
33232 70 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20  p, &nByte);.    
33233 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
33234 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  K ){.      int i
33235 73 50 68 72 61 73 65 3b 0a 0a 20 20 20 20 20 20  sPhrase;..      
33236 69 66 28 20 21 73 71 6c 69 74 65 33 5f 66 74 73  if( !sqlite3_fts
33237 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68  3_enable_parenth
33238 65 73 65 73 20 0a 20 20 20 20 20 20 20 26 26 20  eses .       && 
33239 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
3323a 52 59 5f 50 48 52 41 53 45 20 26 26 20 70 2d 3e  RY_PHRASE && p->
3323b 70 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74 20 0a  pPhrase->isNot .
3323c 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
3323d 20 2f 2a 20 43 72 65 61 74 65 20 61 6e 20 69 6d   /* Create an im
3323e 70 6c 69 63 69 74 20 4e 4f 54 20 6f 70 65 72 61  plicit NOT opera
3323f 74 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tor. */.        
33240 46 74 73 33 45 78 70 72 20 2a 70 4e 6f 74 20 3d  Fts3Expr *pNot =
33241 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
33242 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29  sizeof(Fts3Expr)
33243 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  );.        if( !
33244 70 4e 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20  pNot ){.        
33245 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
33246 72 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  rFree(p);.      
33247 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
33248 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
33249 20 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f   goto exprparse_
3324a 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  out;.        }. 
3324b 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 4e         memset(pN
3324c 6f 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74  ot, 0, sizeof(Ft
3324d 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20  s3Expr));.      
3324e 20 20 70 4e 6f 74 2d 3e 65 54 79 70 65 20 3d 20    pNot->eType = 
3324f 46 54 53 51 55 45 52 59 5f 4e 4f 54 3b 0a 20 20  FTSQUERY_NOT;.  
33250 20 20 20 20 20 20 70 4e 6f 74 2d 3e 70 52 69 67        pNot->pRig
33251 68 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  ht = p;.        
33252 69 66 28 20 70 4e 6f 74 42 72 61 6e 63 68 20 29  if( pNotBranch )
33253 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 6f 74  {.          pNot
33254 42 72 61 6e 63 68 2d 3e 70 4c 65 66 74 20 3d 20  Branch->pLeft = 
33255 70 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 6f  p;.          pNo
33256 74 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e 6f 74  t->pRight = pNot
33257 42 72 61 6e 63 68 3b 0a 20 20 20 20 20 20 20 20  Branch;.        
33258 7d 0a 20 20 20 20 20 20 20 20 70 4e 6f 74 42 72  }.        pNotBr
33259 61 6e 63 68 20 3d 20 70 4e 6f 74 3b 0a 20 20 20  anch = pNot;.   
3325a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3325b 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 70 2d    int eType = p-
3325c 3e 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20  >eType;.        
3325d 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 46  assert( eType!=F
3325e 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 7c  TSQUERY_PHRASE |
3325f 7c 20 21 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69  | !p->pPhrase->i
33260 73 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20  sNot );.        
33261 69 73 50 68 72 61 73 65 20 3d 20 28 65 54 79 70  isPhrase = (eTyp
33262 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e==FTSQUERY_PHRA
33263 53 45 20 7c 7c 20 70 2d 3e 70 4c 65 66 74 29 3b  SE || p->pLeft);
33264 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ..        /* The
33265 20 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65   isRequirePhrase
33266 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
33267 20 74 6f 20 74 72 75 65 20 69 66 20 61 20 70 68   to true if a ph
33268 72 61 73 65 20 6f 72 0a 20 20 20 20 20 20 20 20  rase or.        
33269 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ** an expression
3326a 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 70 61   contained in pa
3326b 72 65 6e 74 68 65 73 69 73 20 69 73 20 72 65 71  renthesis is req
3326c 75 69 72 65 64 2e 20 49 66 20 61 0a 20 20 20 20  uired. If a.    
3326d 20 20 20 20 2a 2a 20 62 69 6e 61 72 79 20 6f 70      ** binary op
3326e 65 72 61 74 6f 72 20 28 41 4e 44 2c 20 4f 52 2c  erator (AND, OR,
3326f 20 4e 4f 54 20 6f 72 20 4e 45 41 52 29 20 69 73   NOT or NEAR) is
33270 20 65 6e 63 6f 75 6e 74 65 64 20 77 68 65 6e 0a   encounted when.
33271 20 20 20 20 20 20 20 20 2a 2a 20 69 73 52 65 71          ** isReq
33272 75 69 72 65 50 68 72 61 73 65 20 69 73 20 73 65  uirePhrase is se
33273 74 2c 20 74 68 69 73 20 69 73 20 61 20 73 79 6e  t, this is a syn
33274 74 61 78 20 65 72 72 6f 72 2e 0a 20 20 20 20 20  tax error..     
33275 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66     */.        if
33276 28 20 21 69 73 50 68 72 61 73 65 20 26 26 20 69  ( !isPhrase && i
33277 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20 29  sRequirePhrase )
33278 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
33279 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28  te3Fts3ExprFree(
3327a 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63  p);.          rc
3327b 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
3327c 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
3327d 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20  exprparse_out;. 
3327e 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20         }.  .    
3327f 20 20 20 20 69 66 28 20 69 73 50 68 72 61 73 65      if( isPhrase
33280 20 26 26 20 21 69 73 52 65 71 75 69 72 65 50 68   && !isRequirePh
33281 72 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20  rase ){.        
33282 20 20 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 69    /* Insert an i
33283 6d 70 6c 69 63 69 74 20 41 4e 44 20 6f 70 65 72  mplicit AND oper
33284 61 74 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20  ator. */.       
33285 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 41 6e     Fts3Expr *pAn
33286 64 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  d;.          ass
33287 65 72 74 28 20 70 52 65 74 20 26 26 20 70 50 72  ert( pRet && pPr
33288 65 76 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ev );.          
33289 70 41 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d  pAnd = sqlite3_m
3328a 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73  alloc(sizeof(Fts
3328b 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20  3Expr));.       
3328c 20 20 20 69 66 28 20 21 70 41 6e 64 20 29 7b 0a     if( !pAnd ){.
3328d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
3328e 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28  te3Fts3ExprFree(
3328f 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  p);.            
33290 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
33291 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  M;.            g
33292 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f 75  oto exprparse_ou
33293 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  t;.          }. 
33294 20 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28           memset(
33295 70 41 6e 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pAnd, 0, sizeof(
33296 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20  Fts3Expr));.    
33297 20 20 20 20 20 20 70 41 6e 64 2d 3e 65 54 79 70        pAnd->eTyp
33298 65 20 3d 20 46 54 53 51 55 45 52 59 5f 41 4e 44  e = FTSQUERY_AND
33299 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 73 65  ;.          inse
3329a 72 74 42 69 6e 61 72 79 4f 70 65 72 61 74 6f 72  rtBinaryOperator
3329b 28 26 70 52 65 74 2c 20 70 50 72 65 76 2c 20 70  (&pRet, pPrev, p
3329c 41 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  And);.          
3329d 70 50 72 65 76 20 3d 20 70 41 6e 64 3b 0a 20 20  pPrev = pAnd;.  
3329e 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
3329f 20 2f 2a 20 54 68 69 73 20 74 65 73 74 20 63 61   /* This test ca
332a0 74 63 68 65 73 20 61 74 74 65 6d 70 74 73 20 74  tches attempts t
332a1 6f 20 6d 61 6b 65 20 65 69 74 68 65 72 20 6f 70  o make either op
332a2 65 72 61 6e 64 20 6f 66 20 61 20 4e 45 41 52 0a  erand of a NEAR.
332a3 20 20 20 20 20 20 20 20 2a 2a 20 6f 70 65 72 61          ** opera
332a4 74 6f 72 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74  tor something ot
332a5 68 65 72 20 74 68 61 6e 20 61 20 70 68 72 61 73  her than a phras
332a6 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  e. For example, 
332a7 65 69 74 68 65 72 20 6f 66 0a 20 20 20 20 20 20  either of.      
332a8 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69    ** the followi
332a9 6e 67 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  ng:.        **. 
332aa 20 20 20 20 20 20 20 2a 2a 20 20 20 20 28 62 72         **    (br
332ab 61 63 6b 65 74 65 64 20 65 78 70 72 65 73 73 69  acketed expressi
332ac 6f 6e 29 20 4e 45 41 52 20 70 68 72 61 73 65 0a  on) NEAR phrase.
332ad 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 70 68          **    ph
332ae 72 61 73 65 20 4e 45 41 52 20 28 62 72 61 63 6b  rase NEAR (brack
332af 65 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 29  eted expression)
332b0 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
332b1 20 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 6e      ** Return an
332b2 20 65 72 72 6f 72 20 69 6e 20 65 69 74 68 65 72   error in either
332b3 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a   case..        *
332b4 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  /.        if( pP
332b5 72 65 76 20 26 26 20 28 0a 20 20 20 20 20 20 20  rev && (.       
332b6 20 20 20 20 20 28 65 54 79 70 65 3d 3d 46 54 53       (eType==FTS
332b7 51 55 45 52 59 5f 4e 45 41 52 20 26 26 20 21 69  QUERY_NEAR && !i
332b8 73 50 68 72 61 73 65 20 26 26 20 70 50 72 65 76  sPhrase && pPrev
332b9 2d 3e 65 54 79 70 65 21 3d 46 54 53 51 55 45 52  ->eType!=FTSQUER
332ba 59 5f 50 48 52 41 53 45 29 0a 20 20 20 20 20 20  Y_PHRASE).      
332bb 20 20 20 7c 7c 20 28 65 54 79 70 65 21 3d 46 54     || (eType!=FT
332bc 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 26 26  SQUERY_PHRASE &&
332bd 20 69 73 50 68 72 61 73 65 20 26 26 20 70 50 72   isPhrase && pPr
332be 65 76 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  ev->eType==FTSQU
332bf 45 52 59 5f 4e 45 41 52 29 0a 20 20 20 20 20 20  ERY_NEAR).      
332c0 20 20 29 29 7b 0a 20 20 20 20 20 20 20 20 20 20    )){.          
332c1 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
332c2 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20  ree(p);.        
332c3 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
332c4 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 67  ROR;.          g
332c5 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f 75  oto exprparse_ou
332c6 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0a  t;.        }.  .
332c7 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50 68          if( isPh
332c8 72 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20  rase ){.        
332c9 20 20 69 66 28 20 70 52 65 74 20 29 7b 0a 20 20    if( pRet ){.  
332ca 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
332cb 28 20 70 50 72 65 76 20 26 26 20 70 50 72 65 76  ( pPrev && pPrev
332cc 2d 3e 70 4c 65 66 74 20 26 26 20 70 50 72 65 76  ->pLeft && pPrev
332cd 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20  ->pRight==0 );. 
332ce 20 20 20 20 20 20 20 20 20 20 20 70 50 72 65 76             pPrev
332cf 2d 3e 70 52 69 67 68 74 20 3d 20 70 3b 0a 20 20  ->pRight = p;.  
332d0 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 61            p->pPa
332d1 72 65 6e 74 20 3d 20 70 50 72 65 76 3b 0a 20 20  rent = pPrev;.  
332d2 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
332d3 20 20 20 20 20 20 20 20 20 20 20 70 52 65 74 20             pRet 
332d4 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = p;.          }
332d5 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
332d6 20 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74            insert
332d7 42 69 6e 61 72 79 4f 70 65 72 61 74 6f 72 28 26  BinaryOperator(&
332d8 70 52 65 74 2c 20 70 50 72 65 76 2c 20 70 29 3b  pRet, pPrev, p);
332d9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
332da 20 20 20 69 73 52 65 71 75 69 72 65 50 68 72 61     isRequirePhra
332db 73 65 20 3d 20 21 69 73 50 68 72 61 73 65 3b 0a  se = !isPhrase;.
332dc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
332dd 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b  sert( nByte>0 );
332de 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
332df 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
332e0 20 7c 7c 20 28 6e 42 79 74 65 3e 30 20 26 26 20   || (nByte>0 && 
332e1 6e 42 79 74 65 3c 3d 6e 49 6e 29 20 29 3b 0a 20  nByte<=nIn) );. 
332e2 20 20 20 6e 49 6e 20 2d 3d 20 6e 42 79 74 65 3b     nIn -= nByte;
332e3 0a 20 20 20 20 7a 49 6e 20 2b 3d 20 6e 42 79 74  .    zIn += nByt
332e4 65 3b 0a 20 20 20 20 70 50 72 65 76 20 3d 20 70  e;.    pPrev = p
332e5 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  ;.  }..  if( rc=
332e6 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26 20  =SQLITE_DONE && 
332e7 70 52 65 74 20 26 26 20 69 73 52 65 71 75 69 72  pRet && isRequir
332e8 65 50 68 72 61 73 65 20 29 7b 0a 20 20 20 20 72  ePhrase ){.    r
332e9 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
332ea 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  ;.  }..  if( rc=
332eb 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
332ec 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
332ed 4f 4b 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c  OK;.    if( !sql
332ee 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65  ite3_fts3_enable
332ef 5f 70 61 72 65 6e 74 68 65 73 65 73 20 26 26 20  _parentheses && 
332f0 70 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0a 20 20  pNotBranch ){.  
332f1 20 20 20 20 69 66 28 20 21 70 52 65 74 20 29 7b      if( !pRet ){
332f2 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
332f3 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
332f4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
332f5 20 70 4e 6f 74 42 72 61 6e 63 68 2d 3e 70 4c 65   pNotBranch->pLe
332f6 66 74 20 3d 20 70 52 65 74 3b 0a 20 20 20 20 20  ft = pRet;.     
332f7 20 20 20 70 52 65 74 20 3d 20 70 4e 6f 74 42 72     pRet = pNotBr
332f8 61 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20  anch;.      }.  
332f9 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 6e 43 6f 6e    }.  }.  *pnCon
332fa 73 75 6d 65 64 20 3d 20 6e 20 2d 20 6e 49 6e 3b  sumed = n - nIn;
332fb 0a 0a 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3a  ..exprparse_out:
332fc 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
332fd 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
332fe 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28  te3Fts3ExprFree(
332ff 70 52 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  pRet);.    sqlit
33300 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
33301 4e 6f 74 42 72 61 6e 63 68 29 3b 0a 20 20 20 20  NotBranch);.    
33302 70 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pRet = 0;.  }.  
33303 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74 3b 0a  *ppExpr = pRet;.
33304 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
33305 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73  /*.** Parameters
33306 20 7a 20 61 6e 64 20 6e 20 63 6f 6e 74 61 69 6e   z and n contain
33307 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
33308 64 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 62 75  d length of a bu
33309 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 0a  ffer containing.
3330a 2a 2a 20 61 6e 20 66 74 73 33 20 71 75 65 72 79  ** an fts3 query
3330b 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 72 65 73   expression, res
3330c 70 65 63 74 69 76 65 6c 79 2e 20 54 68 69 73 20  pectively. This 
3330d 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74  function attempt
3330e 73 20 74 6f 20 70 61 72 73 65 20 74 68 65 0a 2a  s to parse the.*
3330f 2a 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  * query expressi
33310 6f 6e 20 61 6e 64 20 63 72 65 61 74 65 20 61 20  on and create a 
33311 74 72 65 65 20 6f 66 20 46 74 73 33 45 78 70 72  tree of Fts3Expr
33312 20 73 74 72 75 63 74 75 72 65 73 20 72 65 70 72   structures repr
33313 65 73 65 6e 74 69 6e 67 20 74 68 65 0a 2a 2a 20  esenting the.** 
33314 70 61 72 73 65 64 20 65 78 70 72 65 73 73 69 6f  parsed expressio
33315 6e 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  n. If successful
33316 2c 20 2a 70 70 45 78 70 72 20 69 73 20 73 65 74  , *ppExpr is set
33317 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
33318 20 68 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20   head.** of the 
33319 70 61 72 73 65 64 20 65 78 70 72 65 73 73 69 6f  parsed expressio
3331a 6e 20 74 72 65 65 20 61 6e 64 20 53 51 4c 49 54  n tree and SQLIT
3331b 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
3331c 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a  . If an error.**
3331d 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20   occurs, either 
3331e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28 6f 75  SQLITE_NOMEM (ou
3331f 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f  t-of-memory erro
33320 72 29 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52  r) or SQLITE_ERR
33321 4f 52 20 28 70 61 72 73 65 0a 2a 2a 20 65 72 72  OR (parse.** err
33322 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65 64 20  or) is returned 
33323 61 6e 64 20 2a 70 70 45 78 70 72 20 69 73 20 73  and *ppExpr is s
33324 65 74 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  et to 0..**.** I
33325 66 20 70 61 72 61 6d 65 74 65 72 20 6e 20 69 73  f parameter n is
33326 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
33327 65 72 2c 20 74 68 65 6e 20 7a 20 69 73 20 61 73  er, then z is as
33328 73 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20 74  sumed to point t
33329 6f 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  o a.** nul-termi
3332a 6e 61 74 65 64 20 73 74 72 69 6e 67 20 61 6e 64  nated string and
3332b 20 74 68 65 20 6c 65 6e 67 74 68 20 69 73 20 64   the length is d
3332c 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20  etermined using 
3332d 73 74 72 6c 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20  strlen()..**.** 
3332e 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
3332f 74 65 72 2c 20 70 54 6f 6b 65 6e 69 7a 65 72 2c  ter, pTokenizer,
33330 20 69 73 20 70 61 73 73 65 64 20 74 68 65 20 66   is passed the f
33331 74 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f  ts3 tokenizer mo
33332 64 75 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 20 74  dule to.** use t
33333 6f 20 6e 6f 72 6d 61 6c 69 7a 65 20 71 75 65 72  o normalize quer
33334 79 20 74 6f 6b 65 6e 73 20 77 68 69 6c 65 20 70  y tokens while p
33335 61 72 73 69 6e 67 20 74 68 65 20 65 78 70 72 65  arsing the expre
33336 73 73 69 6f 6e 2e 20 54 68 65 20 61 7a 43 6f 6c  ssion. The azCol
33337 5b 5d 0a 2a 2a 20 61 72 72 61 79 2c 20 77 68 69  [].** array, whi
33338 63 68 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  ch is assumed to
33339 20 63 6f 6e 74 61 69 6e 20 6e 43 6f 6c 20 65 6e   contain nCol en
3333a 74 72 69 65 73 2c 20 73 68 6f 75 6c 64 20 63 6f  tries, should co
3333b 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 0a  ntain the names.
3333c 2a 2a 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d  ** of each colum
3333d 6e 20 69 6e 20 74 68 65 20 74 61 72 67 65 74 20  n in the target 
3333e 66 74 73 33 20 74 61 62 6c 65 2c 20 69 6e 20 6f  fts3 table, in o
3333f 72 64 65 72 20 66 72 6f 6d 20 6c 65 66 74 20 74  rder from left t
33340 6f 20 72 69 67 68 74 2e 20 0a 2a 2a 20 43 6f 6c  o right. .** Col
33341 75 6d 6e 20 6e 61 6d 65 73 20 6d 75 73 74 20 62  umn names must b
33342 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  e nul-terminated
33343 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20   strings..**.** 
33344 54 68 65 20 69 44 65 66 61 75 6c 74 43 6f 6c 20  The iDefaultCol 
33345 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64  parameter should
33346 20 62 65 20 70 61 73 73 65 64 20 74 68 65 20 69   be passed the i
33347 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c  ndex of the tabl
33348 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 68 61 74  e column.** that
33349 20 61 70 70 65 61 72 73 20 6f 6e 20 74 68 65 20   appears on the 
3334a 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f  left-hand-side o
3334b 66 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72  f the MATCH oper
3334c 61 74 6f 72 20 28 74 68 65 20 64 65 66 61 75 6c  ator (the defaul
3334d 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20 6d  t.** column to m
3334e 61 74 63 68 20 61 67 61 69 6e 73 74 20 66 6f 72  atch against for
3334f 20 74 6f 6b 65 6e 73 20 66 6f 72 20 77 68 69 63   tokens for whic
33350 68 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  h a column name 
33351 69 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c  is not explicitl
33352 79 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 61  y.** specified a
33353 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 71 75  s part of the qu
33354 65 72 79 20 73 74 72 69 6e 67 29 2c 20 6f 72 20  ery string), or 
33355 2d 31 20 69 66 20 74 6f 6b 65 6e 73 20 6d 61 79  -1 if tokens may
33356 20 62 79 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d   by default.** m
33357 61 74 63 68 20 61 6e 79 20 74 61 62 6c 65 20 63  atch any table c
33358 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  olumn..*/.SQLITE
33359 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3335a 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73  ite3Fts3ExprPars
3335b 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  e(.  sqlite3_tok
3335c 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
3335d 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  er,      /* Toke
3335e 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a  nizer module */.
3335f 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 2c 20    char **azCol, 
33360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33361 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f        /* Array o
33362 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66  f column names f
33363 6f 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a 2f  or fts3 table */
33364 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 20 20 20  .  int nCol,    
33365 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33366 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
33367 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
33368 7a 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  zCol[] */.  int 
33369 69 44 65 66 61 75 6c 74 43 6f 6c 2c 20 20 20 20  iDefaultCol,    
3336a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3336b 2f 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c 75 6d  /* Default colum
3336c 6e 20 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20 20  n to query */.  
3336d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
3336e 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20  nt n,           
3336f 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 4d      /* Text of M
33370 41 54 43 48 20 71 75 65 72 79 20 2a 2f 0a 20 20  ATCH query */.  
33371 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
33372 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
33373 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72 73      /* OUT: Pars
33374 65 64 20 71 75 65 72 79 20 73 74 72 75 63 74 75  ed query structu
33375 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e  re */.){.  int n
33376 50 61 72 73 65 64 3b 0a 20 20 69 6e 74 20 72 63  Parsed;.  int rc
33377 3b 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74  ;.  ParseContext
33378 20 73 50 61 72 73 65 3b 0a 20 20 73 50 61 72 73   sParse;.  sPars
33379 65 2e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70  e.pTokenizer = p
3337a 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 50 61  Tokenizer;.  sPa
3337b 72 73 65 2e 61 7a 43 6f 6c 20 3d 20 28 63 6f 6e  rse.azCol = (con
3337c 73 74 20 63 68 61 72 20 2a 2a 29 61 7a 43 6f 6c  st char **)azCol
3337d 3b 0a 20 20 73 50 61 72 73 65 2e 6e 43 6f 6c 20  ;.  sParse.nCol 
3337e 3d 20 6e 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65  = nCol;.  sParse
3337f 2e 69 44 65 66 61 75 6c 74 43 6f 6c 20 3d 20 69  .iDefaultCol = i
33380 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 73 50  DefaultCol;.  sP
33381 61 72 73 65 2e 6e 4e 65 73 74 20 3d 20 30 3b 0a  arse.nNest = 0;.
33382 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20    if( z==0 ){.  
33383 20 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a 20    *ppExpr = 0;. 
33384 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
33385 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  _OK;.  }.  if( n
33386 3c 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74  <0 ){.    n = st
33387 72 6c 65 6e 28 7a 29 3b 0a 20 20 7d 0a 20 20 72  rlen(z);.  }.  r
33388 63 20 3d 20 66 74 73 33 45 78 70 72 50 61 72 73  c = fts3ExprPars
33389 65 28 26 73 50 61 72 73 65 2c 20 7a 2c 20 6e 2c  e(&sParse, z, n,
3338a 20 70 70 45 78 70 72 2c 20 26 6e 50 61 72 73 65   ppExpr, &nParse
3338b 64 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  d);..  /* Check 
3338c 66 6f 72 20 6d 69 73 6d 61 74 63 68 65 64 20 70  for mismatched p
3338d 61 72 65 6e 74 68 65 73 69 73 20 2a 2f 0a 20 20  arenthesis */.  
3338e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3338f 4b 20 26 26 20 73 50 61 72 73 65 2e 6e 4e 65 73  K && sParse.nNes
33390 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  t ){.    rc = SQ
33391 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
33392 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
33393 72 65 65 28 2a 70 70 45 78 70 72 29 3b 0a 20 20  ree(*ppExpr);.  
33394 20 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a 20    *ppExpr = 0;. 
33395 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
33396 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
33397 20 70 61 72 73 65 64 20 66 74 73 33 20 71 75 65   parsed fts3 que
33398 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6c  ry expression al
33399 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74  located by sqlit
3339a 65 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28  e3Fts3ExprParse(
3339b 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
3339c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
3339d 33 46 74 73 33 45 78 70 72 46 72 65 65 28 46 74  3Fts3ExprFree(Ft
3339e 73 33 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66  s3Expr *p){.  if
3339f 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
333a0 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
333a1 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71  ->pLeft);.    sq
333a2 6c 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65  lite3Fts3ExprFre
333a3 65 28 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20  e(p->pRight);.  
333a4 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
333a5 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  );.  }.}../*****
333a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333aa 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
333ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333af 2a 2a 2a 2a 2a 0a 2a 2a 20 45 76 65 72 79 74 68  *****.** Everyth
333b0 69 6e 67 20 61 66 74 65 72 20 74 68 69 73 20 70  ing after this p
333b1 6f 69 6e 74 20 69 73 20 6a 75 73 74 20 74 65 73  oint is just tes
333b2 74 20 63 6f 64 65 2e 0a 2a 2f 0a 0a 23 69 66 64  t code..*/..#ifd
333b3 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 0a  ef SQLITE_TEST..
333b4 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20  ./*.** Function 
333b5 74 6f 20 71 75 65 72 79 20 74 68 65 20 68 61 73  to query the has
333b6 68 2d 74 61 62 6c 65 20 6f 66 20 74 6f 6b 65 6e  h-table of token
333b7 69 7a 65 72 73 20 28 73 65 65 20 52 45 41 44 4d  izers (see READM
333b8 45 2e 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 0a 2a  E.tokenizers)..*
333b9 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 71 75 65  /.static int que
333ba 72 79 54 65 73 74 54 6f 6b 65 6e 69 7a 65 72 28  ryTestTokenizer(
333bb 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
333bc 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
333bd 4e 61 6d 65 2c 20 20 0a 20 20 63 6f 6e 73 74 20  Name,  .  const 
333be 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
333bf 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0a 29 7b  r_module **pp.){
333c0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
333c1 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
333c2 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  ;.  const char z
333c3 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20  Sql[] = "SELECT 
333c4 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f  fts3_tokenizer(?
333c5 29 22 3b 0a 0a 20 20 2a 70 70 20 3d 20 30 3b 0a  )";..  *pp = 0;.
333c6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70    rc = sqlite3_p
333c7 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53  repare_v2(db, zS
333c8 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20  ql, -1, &pStmt, 
333c9 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  0);.  if( rc!=SQ
333ca 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
333cb 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
333cc 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
333cd 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61  xt(pStmt, 1, zNa
333ce 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53  me, -1, SQLITE_S
333cf 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20 53 51  TATIC);.  if( SQ
333d0 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65  LITE_ROW==sqlite
333d1 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b  3_step(pStmt) ){
333d2 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
333d3 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74  _column_type(pSt
333d4 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 42  mt, 0)==SQLITE_B
333d5 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  LOB ){.      mem
333d6 63 70 79 28 70 70 2c 20 73 71 6c 69 74 65 33 5f  cpy(pp, sqlite3_
333d7 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d  column_blob(pStm
333d8 74 2c 20 30 29 2c 20 73 69 7a 65 6f 66 28 2a 70  t, 0), sizeof(*p
333d9 70 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  p));.    }.  }..
333da 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
333db 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
333dc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
333dd 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 61 72 74  function is part
333de 20 6f 66 20 74 68 65 20 74 65 73 74 20 69 6e 74   of the test int
333df 65 72 66 61 63 65 20 66 6f 72 20 74 68 65 20 71  erface for the q
333e0 75 65 72 79 20 70 61 72 73 65 72 2e 20 49 74 0a  uery parser. It.
333e1 2a 2a 20 77 72 69 74 65 73 20 61 20 74 65 78 74  ** writes a text
333e2 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
333e3 6f 66 20 74 68 65 20 71 75 65 72 79 20 65 78 70  of the query exp
333e4 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 6e  ression pExpr in
333e5 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72  to the.** buffer
333e6 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61   pointed to by a
333e7 72 67 75 6d 65 6e 74 20 7a 42 75 66 2e 20 49 74  rgument zBuf. It
333e8 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
333e9 20 7a 42 75 66 20 69 73 20 6c 61 72 67 65 20 0a   zBuf is large .
333ea 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f  ** enough to sto
333eb 72 65 20 74 68 65 20 72 65 71 75 69 72 65 64 20  re the required 
333ec 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74  text representat
333ed 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
333ee 6f 69 64 20 65 78 70 72 54 6f 53 74 72 69 6e 67  oid exprToString
333ef 28 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72  (Fts3Expr *pExpr
333f0 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
333f1 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
333f2 65 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  eType ){.    cas
333f3 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  e FTSQUERY_PHRAS
333f4 45 3a 20 7b 0a 20 20 20 20 20 20 46 74 73 33 50  E: {.      Fts3P
333f5 68 72 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d  hrase *pPhrase =
333f6 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 3b   pExpr->pPhrase;
333f7 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
333f8 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69      zBuf += spri
333f9 6e 74 66 28 7a 42 75 66 2c 20 22 50 48 52 41 53  ntf(zBuf, "PHRAS
333fa 45 20 25 64 20 25 64 22 2c 20 70 50 68 72 61 73  E %d %d", pPhras
333fb 65 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 50 68 72  e->iColumn, pPhr
333fc 61 73 65 2d 3e 69 73 4e 6f 74 29 3b 0a 20 20 20  ase->isNot);.   
333fd 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
333fe 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69  hrase->nToken; i
333ff 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  ++){.        zBu
33400 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
33401 66 2c 22 20 25 2e 2a 73 22 2c 70 50 68 72 61 73  f," %.*s",pPhras
33402 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 6e 2c 70  e->aToken[i].n,p
33403 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69  Phrase->aToken[i
33404 5d 2e 7a 29 3b 0a 20 20 20 20 20 20 20 20 7a 42  ].z);.        zB
33405 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
33406 75 66 2c 22 25 73 22 2c 20 28 70 50 68 72 61 73  uf,"%s", (pPhras
33407 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 69 73 50  e->aToken[i].isP
33408 72 65 66 69 78 3f 22 2b 22 3a 22 22 29 29 3b 0a  refix?"+":""));.
33409 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
3340a 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  turn;.    }..   
3340b 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e   case FTSQUERY_N
3340c 45 41 52 3a 0a 20 20 20 20 20 20 7a 42 75 66 20  EAR:.      zBuf 
3340d 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c  += sprintf(zBuf,
3340e 20 22 4e 45 41 52 2f 25 64 20 22 2c 20 70 45 78   "NEAR/%d ", pEx
3340f 70 72 2d 3e 6e 4e 65 61 72 29 3b 0a 20 20 20 20  pr->nNear);.    
33410 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
33411 65 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 3a 0a  e FTSQUERY_NOT:.
33412 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70        zBuf += sp
33413 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 4e 4f 54  rintf(zBuf, "NOT
33414 20 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   ");.      break
33415 3b 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55  ;.    case FTSQU
33416 45 52 59 5f 41 4e 44 3a 0a 20 20 20 20 20 20 7a  ERY_AND:.      z
33417 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a  Buf += sprintf(z
33418 42 75 66 2c 20 22 41 4e 44 20 22 29 3b 0a 20 20  Buf, "AND ");.  
33419 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
3341a 61 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52 3a  ase FTSQUERY_OR:
3341b 0a 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73  .      zBuf += s
3341c 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 4f 52  printf(zBuf, "OR
3341d 20 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   ");.      break
3341e 3b 0a 20 20 7d 0a 0a 20 20 7a 42 75 66 20 2b 3d  ;.  }..  zBuf +=
3341f 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
33420 7b 22 29 3b 0a 20 20 65 78 70 72 54 6f 53 74 72  {");.  exprToStr
33421 69 6e 67 28 70 45 78 70 72 2d 3e 70 4c 65 66 74  ing(pExpr->pLeft
33422 2c 20 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20  , zBuf);.  zBuf 
33423 2b 3d 20 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b  += strlen(zBuf);
33424 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e  .  zBuf += sprin
33425 74 66 28 7a 42 75 66 2c 20 22 7d 20 22 29 3b 0a  tf(zBuf, "} ");.
33426 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e  .  zBuf += sprin
33427 74 66 28 7a 42 75 66 2c 20 22 7b 22 29 3b 0a 20  tf(zBuf, "{");. 
33428 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45   exprToString(pE
33429 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 7a 42 75  xpr->pRight, zBu
3342a 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 74  f);.  zBuf += st
3342b 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 7a 42  rlen(zBuf);.  zB
3342c 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
3342d 75 66 2c 20 22 7d 22 29 3b 0a 7d 0a 0a 2f 2a 0a  uf, "}");.}../*.
3342e 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69  ** This is the i
3342f 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
33430 20 61 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75   a scalar SQL fu
33431 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74  nction used to t
33432 65 73 74 20 74 68 65 20 0a 2a 2a 20 65 78 70 72  est the .** expr
33433 65 73 73 69 6f 6e 20 70 61 72 73 65 72 2e 20 49  ession parser. I
33434 74 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c  t should be call
33435 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ed as follows:.*
33436 2a 0a 2a 2a 20 20 20 66 74 73 33 5f 65 78 70 72  *.**   fts3_expr
33437 74 65 73 74 28 3c 74 6f 6b 65 6e 69 7a 65 72 3e  test(<tokenizer>
33438 2c 20 3c 65 78 70 72 3e 2c 20 3c 63 6f 6c 75 6d  , <expr>, <colum
33439 6e 20 31 3e 2c 20 2e 2e 2e 29 3b 0a 2a 2a 0a 2a  n 1>, ...);.**.*
3343a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
3343b 6d 65 6e 74 2c 20 3c 74 6f 6b 65 6e 69 7a 65 72  ment, <tokenizer
3343c 3e 2c 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  >, is the name o
3343d 66 20 74 68 65 20 66 74 73 33 20 74 6f 6b 65 6e  f the fts3 token
3343e 69 7a 65 72 20 75 73 65 64 0a 2a 2a 20 74 6f 20  izer used.** to 
3343f 70 61 72 73 65 20 74 68 65 20 71 75 65 72 79 20  parse the query 
33440 65 78 70 72 65 73 73 69 6f 6e 20 28 73 65 65 20  expression (see 
33441 52 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65 72  README.tokenizer
33442 73 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61  s). The second a
33443 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68  rgument.** is th
33444 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  e query expressi
33445 6f 6e 20 74 6f 20 70 61 72 73 65 2e 20 45 61 63  on to parse. Eac
33446 68 20 73 75 62 73 65 71 75 65 6e 74 20 61 72 67  h subsequent arg
33447 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 61 6d  ument is the nam
33448 65 0a 2a 2a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  e.** of a column
33449 20 6f 66 20 74 68 65 20 66 74 73 33 20 74 61 62   of the fts3 tab
3344a 6c 65 20 74 68 61 74 20 74 68 65 20 71 75 65 72  le that the quer
3344b 79 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79  y expression may
3344c 20 72 65 66 65 72 20 74 6f 2e 0a 2a 2a 20 46 6f   refer to..** Fo
3344d 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  r example:.**.**
3344e 20 20 20 53 45 4c 45 43 54 20 66 74 73 33 5f 65     SELECT fts3_e
3344f 78 70 72 74 65 73 74 28 27 73 69 6d 70 6c 65 27  xprtest('simple'
33450 2c 20 27 42 69 6c 6c 20 63 6f 6c 32 3a 42 6c 6f  , 'Bill col2:Blo
33451 67 67 73 27 2c 20 27 63 6f 6c 31 27 2c 20 27 63  ggs', 'col1', 'c
33452 6f 6c 32 27 29 3b 0a 2a 2f 0a 73 74 61 74 69 63  ol2');.*/.static
33453 20 76 6f 69 64 20 66 74 73 33 45 78 70 72 54 65   void fts3ExprTe
33454 73 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  st(.  sqlite3_co
33455 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
33456 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
33457 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
33458 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  gv.){.  sqlite3_
33459 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
3345a 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20   const *pModule 
3345b 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  = 0;.  sqlite3_t
3345c 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
3345d 69 7a 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20  izer = 0;.  int 
3345e 72 63 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43  rc;.  char **azC
3345f 6f 6c 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20  ol = 0;.  const 
33460 63 68 61 72 20 2a 7a 45 78 70 72 3b 0a 20 20 69  char *zExpr;.  i
33461 6e 74 20 6e 45 78 70 72 3b 0a 20 20 69 6e 74 20  nt nExpr;.  int 
33462 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  nCol;.  int ii;.
33463 20 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70    Fts3Expr *pExp
33464 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r;.  sqlite3 *db
33465 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
33466 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
33467 74 65 78 74 29 3b 0a 0a 20 20 69 66 28 20 61 72  text);..  if( ar
33468 67 63 3c 33 20 29 7b 0a 20 20 20 20 73 71 6c 69  gc<3 ){.    sqli
33469 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
3346a 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20  (context, .     
3346b 20 20 20 22 55 73 61 67 65 3a 20 66 74 73 33 5f     "Usage: fts3_
3346c 65 78 70 72 74 65 73 74 28 74 6f 6b 65 6e 69 7a  exprtest(tokeniz
3346d 65 72 2c 20 65 78 70 72 2c 20 63 6f 6c 31 2c 20  er, expr, col1, 
3346e 2e 2e 2e 22 2c 20 2d 31 0a 20 20 20 20 29 3b 0a  ...", -1.    );.
3346f 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
33470 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54 65 73  .  rc = queryTes
33471 74 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 0a 20  tTokenizer(db,. 
33472 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33473 20 20 20 20 20 20 20 20 20 28 63 6f 6e 73 74 20           (const 
33474 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
33475 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
33476 5d 29 2c 20 26 70 4d 6f 64 75 6c 65 29 3b 0a 20  ]), &pModule);. 
33477 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
33478 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c  NOMEM ){.    sql
33479 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
3347a 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29  r_nomem(context)
3347b 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74  ;.    goto exprt
3347c 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65  est_out;.  }else
3347d 20 69 66 28 20 21 70 4d 6f 64 75 6c 65 20 29 7b   if( !pModule ){
3347e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3347f 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
33480 74 2c 20 22 4e 6f 20 73 75 63 68 20 74 6f 6b 65  t, "No such toke
33481 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 22 2c 20 2d  nizer module", -
33482 31 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70  1);.    goto exp
33483 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  rtest_out;.  }..
33484 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
33485 78 43 72 65 61 74 65 28 30 2c 20 30 2c 20 26 70  xCreate(0, 0, &p
33486 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 61 73  Tokenizer);.  as
33487 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
33488 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51  _NOMEM || rc==SQ
33489 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28  LITE_OK );.  if(
3348a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
3348b 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  M ){.    sqlite3
3348c 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
3348d 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  mem(context);.  
3348e 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f    goto exprtest_
3348f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65  out;.  }.  pToke
33490 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d  nizer->pModule =
33491 20 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 7a 45 78   pModule;..  zEx
33492 70 72 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  pr = (const char
33493 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
33494 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
33495 20 20 6e 45 78 70 72 20 3d 20 73 71 6c 69 74 65    nExpr = sqlite
33496 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
33497 67 76 5b 31 5d 29 3b 0a 20 20 6e 43 6f 6c 20 3d  gv[1]);.  nCol =
33498 20 61 72 67 63 2d 32 3b 0a 20 20 61 7a 43 6f 6c   argc-2;.  azCol
33499 20 3d 20 28 63 68 61 72 20 2a 2a 29 73 71 6c 69   = (char **)sqli
3349a 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 6f 6c 2a  te3_malloc(nCol*
3349b 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 29 3b  sizeof(char *));
3349c 0a 20 20 69 66 28 20 21 61 7a 43 6f 6c 20 29 7b  .  if( !azCol ){
3349d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3349e 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
3349f 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f  context);.    go
334a0 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b  to exprtest_out;
334a1 0a 20 20 7d 0a 20 20 66 6f 72 28 69 69 3d 30 3b  .  }.  for(ii=0;
334a2 20 69 69 3c 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b   ii<nCol; ii++){
334a3 0a 20 20 20 20 61 7a 43 6f 6c 5b 69 69 5d 20 3d  .    azCol[ii] =
334a4 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
334a5 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
334a6 5b 69 69 2b 32 5d 29 3b 0a 20 20 7d 0a 0a 20 20  [ii+2]);.  }..  
334a7 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
334a8 45 78 70 72 50 61 72 73 65 28 0a 20 20 20 20 20  ExprParse(.     
334a9 20 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 61 7a 43   pTokenizer, azC
334aa 6f 6c 2c 20 6e 43 6f 6c 2c 20 6e 43 6f 6c 2c 20  ol, nCol, nCol, 
334ab 7a 45 78 70 72 2c 20 6e 45 78 70 72 2c 20 26 70  zExpr, nExpr, &p
334ac 45 78 70 72 0a 20 20 29 3b 0a 20 20 69 66 28 20  Expr.  );.  if( 
334ad 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
334ae 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
334af 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
334b0 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
334b1 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f   goto exprtest_o
334b2 75 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ut;.  }else if( 
334b3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
334b4 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 34  .    char zBuf[4
334b5 30 39 36 5d 3b 0a 20 20 20 20 65 78 70 72 54 6f  096];.    exprTo
334b6 53 74 72 69 6e 67 28 70 45 78 70 72 2c 20 7a 42  String(pExpr, zB
334b7 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  uf);.    sqlite3
334b8 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
334b9 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20  text, zBuf, -1, 
334ba 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
334bb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  );.    sqlite3Ft
334bc 73 33 45 78 70 72 46 72 65 65 28 70 45 78 70 72  s3ExprFree(pExpr
334bd 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
334be 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
334bf 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 45  rror(context, "E
334c0 72 72 6f 72 20 70 61 72 73 69 6e 67 20 65 78 70  rror parsing exp
334c1 72 65 73 73 69 6f 6e 22 2c 20 2d 31 29 3b 0a 20  ression", -1);. 
334c2 20 7d 0a 0a 65 78 70 72 74 65 73 74 5f 6f 75 74   }..exprtest_out
334c3 3a 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 20  :.  if( pModule 
334c4 26 26 20 70 54 6f 6b 65 6e 69 7a 65 72 20 29 7b  && pTokenizer ){
334c5 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  .    rc = pModul
334c6 65 2d 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b  e->xDestroy(pTok
334c7 65 6e 69 7a 65 72 29 3b 0a 20 20 7d 0a 20 20 73  enizer);.  }.  s
334c8 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 6f  qlite3_free(azCo
334c9 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  l);.}../*.** Reg
334ca 69 73 74 65 72 20 74 68 65 20 71 75 65 72 79 20  ister the query 
334cb 65 78 70 72 65 73 73 69 6f 6e 20 70 61 72 73 65  expression parse
334cc 72 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20  r test function 
334cd 66 74 73 33 5f 65 78 70 72 74 65 73 74 28 29 20  fts3_exprtest() 
334ce 0a 2a 2a 20 77 69 74 68 20 64 61 74 61 62 61 73  .** with databas
334cf 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e  e connection db.
334d0 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
334d1 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
334d2 46 74 73 33 45 78 70 72 49 6e 69 74 54 65 73 74  Fts3ExprInitTest
334d3 49 6e 74 65 72 66 61 63 65 28 73 71 6c 69 74 65  Interface(sqlite
334d4 33 2a 20 64 62 29 7b 0a 20 20 73 71 6c 69 74 65  3* db){.  sqlite
334d5 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
334d6 6e 28 0a 20 20 20 20 20 20 64 62 2c 20 22 66 74  n(.      db, "ft
334d7 73 33 5f 65 78 70 72 74 65 73 74 22 2c 20 2d 31  s3_exprtest", -1
334d8 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
334d9 2c 20 66 74 73 33 45 78 70 72 54 65 73 74 2c 20  , fts3ExprTest, 
334da 30 2c 20 30 0a 20 20 29 3b 0a 7d 0a 0a 23 65 6e  0, 0.  );.}..#en
334db 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  dif.#endif /* !d
334dc 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
334dd 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
334de 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
334df 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  3) */../********
334e0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
334e1 73 33 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a  s3_expr.c ******
334e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334e4 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
334e5 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
334e6 65 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a  e fts3_hash.c **
334e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334e9 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
334ea 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a  1 September 22.*
334eb 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
334ec 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
334ed 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
334ee 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
334ef 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
334f0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
334f1 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
334f2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
334f3 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
334f4 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
334f5 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
334f6 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
334f7 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
334f8 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
334f9 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
334fa 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
334fb 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
334fc 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
334fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
33501 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70   This is the imp
33502 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67  lementation of g
33503 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c  eneric hash-tabl
33504 65 73 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  es used in SQLit
33505 65 2e 0a 2a 2a 20 57 65 27 76 65 20 6d 6f 64 69  e..** We've modi
33506 66 69 65 64 20 69 74 20 73 6c 69 67 68 74 6c 79  fied it slightly
33507 20 74 6f 20 73 65 72 76 65 20 61 73 20 61 20 73   to serve as a s
33508 74 61 6e 64 61 6c 6f 6e 65 20 68 61 73 68 20 74  tandalone hash t
33509 61 62 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  able.** implemen
3350a 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66  tation for the f
3350b 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 69 6e  ull-text indexin
3350c 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 0a 2f 2a  g module..*/../*
3350d 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
3350e 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
3350f 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a  y compiled if:.*
33510 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
33511 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
33512 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20  ing built as an 
33513 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20  extension.**    
33514 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73     (in which cas
33515 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73  e SQLITE_CORE is
33516 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f   not defined), o
33517 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  r.**.**     * Th
33518 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
33519 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74   being built int
3351a 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a  o the core of.**
3351b 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69         SQLite (i
3351c 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
3351d 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
3351e 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a  is defined)..*/.
3351f 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
33520 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
33521 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
33522 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 2f 2a 0a 2a  LE_FTS3)..../*.*
33523 2a 20 4d 61 6c 6c 6f 63 20 61 6e 64 20 46 72 65  * Malloc and Fre
33524 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73  e functions.*/.s
33525 74 61 74 69 63 20 76 6f 69 64 20 2a 66 74 73 33  tatic void *fts3
33526 48 61 73 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e  HashMalloc(int n
33527 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73  ){.  void *p = s
33528 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 29  qlite3_malloc(n)
33529 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
3352a 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29   memset(p, 0, n)
3352b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
3352c 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
3352d 66 74 73 33 48 61 73 68 46 72 65 65 28 76 6f 69  fts3HashFree(voi
3352e 64 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  d *p){.  sqlite3
3352f 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 20  _free(p);.}../* 
33530 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79  Turn bulk memory
33531 20 69 6e 74 6f 20 61 20 68 61 73 68 20 74 61 62   into a hash tab
33532 6c 65 20 6f 62 6a 65 63 74 20 62 79 20 69 6e 69  le object by ini
33533 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a  tializing the.**
33534 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 48   fields of the H
33535 61 73 68 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ash structure..*
33536 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73 20 61  *.** "pNew" is a
33537 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
33538 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20  hash table that 
33539 69 73 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c  is to be initial
3353a 69 7a 65 64 2e 0a 2a 2a 20 6b 65 79 43 6c 61 73  ized..** keyClas
3353b 73 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20  s is one of the 
3353c 63 6f 6e 73 74 61 6e 74 73 20 0a 2a 2a 20 46 54  constants .** FT
3353d 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 6f  S3_HASH_BINARY o
3353e 72 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49  r FTS3_HASH_STRI
3353f 4e 47 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f  NG.  The value o
33540 66 20 6b 65 79 43 6c 61 73 73 20 0a 2a 2a 20 64  f keyClass .** d
33541 65 74 65 72 6d 69 6e 65 73 20 77 68 61 74 20 6b  etermines what k
33542 69 6e 64 20 6f 66 20 6b 65 79 20 74 68 65 20 68  ind of key the h
33543 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c 20 75  ash table will u
33544 73 65 2e 20 20 22 63 6f 70 79 4b 65 79 22 20 69  se.  "copyKey" i
33545 73 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65  s.** true if the
33546 20 68 61 73 68 20 74 61 62 6c 65 20 73 68 6f 75   hash table shou
33547 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20  ld make its own 
33548 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20  private copy of 
33549 6b 65 79 73 20 61 6e 64 0a 2a 2a 20 66 61 6c 73  keys and.** fals
3354a 65 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6a  e if it should j
3354b 75 73 74 20 75 73 65 20 74 68 65 20 73 75 70 70  ust use the supp
3354c 6c 69 65 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  lied pointer..*/
3354d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3354e 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
3354f 48 61 73 68 49 6e 69 74 28 66 74 73 33 48 61 73  HashInit(fts3Has
33550 68 20 2a 70 4e 65 77 2c 20 69 6e 74 20 6b 65 79  h *pNew, int key
33551 43 6c 61 73 73 2c 20 69 6e 74 20 63 6f 70 79 4b  Class, int copyK
33552 65 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  ey){.  assert( p
33553 4e 65 77 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  New!=0 );.  asse
33554 72 74 28 20 6b 65 79 43 6c 61 73 73 3e 3d 46 54  rt( keyClass>=FT
33555 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 26  S3_HASH_STRING &
33556 26 20 6b 65 79 43 6c 61 73 73 3c 3d 46 54 53 33  & keyClass<=FTS3
33557 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a  _HASH_BINARY );.
33558 20 20 70 4e 65 77 2d 3e 6b 65 79 43 6c 61 73 73    pNew->keyClass
33559 20 3d 20 6b 65 79 43 6c 61 73 73 3b 0a 20 20 70   = keyClass;.  p
3355a 4e 65 77 2d 3e 63 6f 70 79 4b 65 79 20 3d 20 63  New->copyKey = c
3355b 6f 70 79 4b 65 79 3b 0a 20 20 70 4e 65 77 2d 3e  opyKey;.  pNew->
3355c 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65  first = 0;.  pNe
3355d 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20  w->count = 0;.  
3355e 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30  pNew->htsize = 0
3355f 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30  ;.  pNew->ht = 0
33560 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61  ;.}../* Remove a
33561 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20  ll entries from 
33562 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52  a hash table.  R
33563 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72  eclaim all memor
33564 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20  y..** Call this 
33565 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74  routine to delet
33566 65 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f  e a hash table o
33567 72 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73  r to reset a has
33568 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68  h table.** to th
33569 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a  e empty state..*
3356a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
3356b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
3356c 33 48 61 73 68 43 6c 65 61 72 28 66 74 73 33 48  3HashClear(fts3H
3356d 61 73 68 20 2a 70 48 29 7b 0a 20 20 66 74 73 33  ash *pH){.  fts3
3356e 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
3356f 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
33570 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20  ooping over all 
33571 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
33572 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65  table */..  asse
33573 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65  rt( pH!=0 );.  e
33574 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b  lem = pH->first;
33575 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30  .  pH->first = 0
33576 3b 0a 20 20 66 74 73 33 48 61 73 68 46 72 65 65  ;.  fts3HashFree
33577 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e  (pH->ht);.  pH->
33578 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74  ht = 0;.  pH->ht
33579 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c  size = 0;.  whil
3357a 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 66  e( elem ){.    f
3357b 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78  ts3HashElem *nex
3357c 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e  t_elem = elem->n
3357d 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d  ext;.    if( pH-
3357e 3e 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d  >copyKey && elem
3357f 2d 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  ->pKey ){.      
33580 66 74 73 33 48 61 73 68 46 72 65 65 28 65 6c 65  fts3HashFree(ele
33581 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 7d 0a  m->pKey);.    }.
33582 20 20 20 20 66 74 73 33 48 61 73 68 46 72 65 65      fts3HashFree
33583 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d  (elem);.    elem
33584 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20   = next_elem;.  
33585 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20  }.  pH->count = 
33586 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68  0;.}../*.** Hash
33587 20 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20   and comparison 
33588 66 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e 20 74  functions when t
33589 68 65 20 6d 6f 64 65 20 69 73 20 46 54 53 33 5f  he mode is FTS3_
3358a 48 41 53 48 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73  HASH_STRING.*/.s
3358b 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 74  tatic int fts3St
3358c 72 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69 64  rHash(const void
3358d 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
3358e 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
3358f 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  *z = (const char
33590 20 2a 29 70 4b 65 79 3b 0a 20 20 69 6e 74 20 68   *)pKey;.  int h
33591 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 4b 65 79   = 0;.  if( nKey
33592 3c 3d 30 20 29 20 6e 4b 65 79 20 3d 20 28 69 6e  <=0 ) nKey = (in
33593 74 29 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20  t) strlen(z);.  
33594 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20  while( nKey > 0 
33595 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c   ){.    h = (h<<
33596 33 29 20 5e 20 68 20 5e 20 2a 7a 2b 2b 3b 0a 20  3) ^ h ^ *z++;. 
33597 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20     nKey--;.  }. 
33598 20 72 65 74 75 72 6e 20 68 20 26 20 30 78 37 66   return h & 0x7f
33599 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69 63  ffffff;.}.static
3359a 20 69 6e 74 20 66 74 73 33 53 74 72 43 6f 6d 70   int fts3StrComp
3359b 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  are(const void *
3359c 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63  pKey1, int n1, c
3359d 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
3359e 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28  , int n2){.  if(
3359f 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e   n1!=n2 ) return
335a0 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72   1;.  return str
335a1 6e 63 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 72  ncmp((const char
335a2 2a 29 70 4b 65 79 31 2c 28 63 6f 6e 73 74 20 63  *)pKey1,(const c
335a3 68 61 72 2a 29 70 4b 65 79 32 2c 6e 31 29 3b 0a  har*)pKey2,n1);.
335a4 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e  }../*.** Hash an
335a5 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  d comparison fun
335a6 63 74 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20  ctions when the 
335a7 6d 6f 64 65 20 69 73 20 46 54 53 33 5f 48 41 53  mode is FTS3_HAS
335a8 48 5f 42 49 4e 41 52 59 0a 2a 2f 0a 73 74 61 74  H_BINARY.*/.stat
335a9 69 63 20 69 6e 74 20 66 74 73 33 42 69 6e 48 61  ic int fts3BinHa
335aa 73 68 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  sh(const void *p
335ab 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a  Key, int nKey){.
335ac 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 63    int h = 0;.  c
335ad 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28  onst char *z = (
335ae 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65  const char *)pKe
335af 79 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79  y;.  while( nKey
335b0 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 68 20  -- > 0 ){.    h 
335b1 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 2a  = (h<<3) ^ h ^ *
335b2 28 7a 2b 2b 29 3b 0a 20 20 7d 0a 20 20 72 65 74  (z++);.  }.  ret
335b3 75 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66  urn h & 0x7fffff
335b4 66 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ff;.}.static int
335b5 20 66 74 73 33 42 69 6e 43 6f 6d 70 61 72 65 28   fts3BinCompare(
335b6 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
335b7 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74  1, int n1, const
335b8 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e   void *pKey2, in
335b9 74 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21  t n2){.  if( n1!
335ba 3d 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  =n2 ) return 1;.
335bb 20 20 72 65 74 75 72 6e 20 6d 65 6d 63 6d 70 28    return memcmp(
335bc 70 4b 65 79 31 2c 70 4b 65 79 32 2c 6e 31 29 3b  pKey1,pKey2,n1);
335bd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
335be 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
335bf 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 68 61  e appropriate ha
335c0 73 68 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65  sh function give
335c1 6e 20 74 68 65 20 6b 65 79 20 63 6c 61 73 73 2e  n the key class.
335c2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 73 79 6e  .**.** The C syn
335c3 74 61 78 20 69 6e 20 74 68 69 73 20 66 75 6e 63  tax in this func
335c4 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20  tion definition 
335c5 6d 61 79 20 62 65 20 75 6e 66 61 6d 69 6c 61 72  may be unfamilar
335c6 20 74 6f 20 73 6f 6d 65 20 0a 2a 2a 20 70 72 6f   to some .** pro
335c7 67 72 61 6d 6d 65 72 73 2c 20 73 6f 20 77 65 20  grammers, so we 
335c8 70 72 6f 76 69 64 65 20 74 68 65 20 66 6f 6c 6c  provide the foll
335c9 6f 77 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c  owing additional
335ca 20 65 78 70 6c 61 6e 61 74 69 6f 6e 3a 0a 2a 2a   explanation:.**
335cb 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  .** The name of 
335cc 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
335cd 22 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e  "ftsHashFunction
335ce 22 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e  ".  The function
335cf 20 74 61 6b 65 73 20 61 0a 2a 2a 20 73 69 6e 67   takes a.** sing
335d0 6c 65 20 70 61 72 61 6d 65 74 65 72 20 22 6b 65  le parameter "ke
335d1 79 43 6c 61 73 73 22 2e 20 20 54 68 65 20 72 65  yClass".  The re
335d2 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 74  turn value of ft
335d3 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 29 0a  sHashFunction().
335d4 2a 2a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ** is a pointer 
335d5 74 6f 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74  to another funct
335d6 69 6f 6e 2e 20 20 53 70 65 63 69 66 69 63 61 6c  ion.  Specifical
335d7 6c 79 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76  ly, the return v
335d8 61 6c 75 65 0a 2a 2a 20 6f 66 20 66 74 73 48 61  alue.** of ftsHa
335d9 73 68 46 75 6e 63 74 69 6f 6e 28 29 20 69 73 20  shFunction() is 
335da 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
335db 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 74 61 6b  unction that tak
335dc 65 73 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72  es two parameter
335dd 73 0a 2a 2a 20 77 69 74 68 20 74 79 70 65 73 20  s.** with types 
335de 22 63 6f 6e 73 74 20 76 6f 69 64 2a 22 20 61 6e  "const void*" an
335df 64 20 22 69 6e 74 22 20 61 6e 64 20 72 65 74 75  d "int" and retu
335e0 72 6e 73 20 61 6e 20 22 69 6e 74 22 2e 0a 2a 2f  rns an "int"..*/
335e1 0a 73 74 61 74 69 63 20 69 6e 74 20 28 2a 66 74  .static int (*ft
335e2 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 69 6e  sHashFunction(in
335e3 74 20 6b 65 79 43 6c 61 73 73 29 29 28 63 6f 6e  t keyClass))(con
335e4 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20  st void*,int){. 
335e5 20 69 66 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46   if( keyClass==F
335e6 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20  TS3_HASH_STRING 
335e7 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 66  ){.    return &f
335e8 74 73 33 53 74 72 48 61 73 68 3b 0a 20 20 7d 65  ts3StrHash;.  }e
335e9 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
335ea 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f   keyClass==FTS3_
335eb 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20  HASH_BINARY );. 
335ec 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 42     return &fts3B
335ed 69 6e 48 61 73 68 3b 0a 20 20 7d 0a 7d 0a 0a 2f  inHash;.  }.}../
335ee 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
335ef 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70  inter to the app
335f0 72 6f 70 72 69 61 74 65 20 68 61 73 68 20 66 75  ropriate hash fu
335f1 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65  nction given the
335f2 20 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a   key class..**.*
335f3 2a 20 46 6f 72 20 68 65 6c 70 20 69 6e 20 69 6e  * For help in in
335f4 74 65 72 70 72 65 74 65 64 20 74 68 65 20 6f 62  terpreted the ob
335f5 73 63 75 72 65 20 43 20 63 6f 64 65 20 69 6e 20  scure C code in 
335f6 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  the function def
335f7 69 6e 69 74 69 6f 6e 2c 0a 2a 2a 20 73 65 65 20  inition,.** see 
335f8 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65  the header comme
335f9 6e 74 20 6f 6e 20 74 68 65 20 70 72 65 76 69 6f  nt on the previo
335fa 75 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  us function..*/.
335fb 73 74 61 74 69 63 20 69 6e 74 20 28 2a 66 74 73  static int (*fts
335fc 43 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e 28  CompareFunction(
335fd 69 6e 74 20 6b 65 79 43 6c 61 73 73 29 29 28 63  int keyClass))(c
335fe 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
335ff 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b  onst void*,int){
33600 0a 20 20 69 66 28 20 6b 65 79 43 6c 61 73 73 3d  .  if( keyClass=
33601 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e  =FTS3_HASH_STRIN
33602 47 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  G ){.    return 
33603 26 66 74 73 33 53 74 72 43 6f 6d 70 61 72 65 3b  &fts3StrCompare;
33604 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
33605 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d 3d  sert( keyClass==
33606 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59  FTS3_HASH_BINARY
33607 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 26   );.    return &
33608 66 74 73 33 42 69 6e 43 6f 6d 70 61 72 65 3b 0a  fts3BinCompare;.
33609 20 20 7d 0a 7d 0a 0a 2f 2a 20 4c 69 6e 6b 20 61    }.}../* Link a
3360a 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74  n element into t
3360b 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f  he hash table.*/
3360c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
3360d 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65 6d 65  3HashInsertEleme
3360e 6e 74 28 0a 20 20 66 74 73 33 48 61 73 68 20 2a  nt(.  fts3Hash *
3360f 70 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  pH,            /
33610 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68  * The complete h
33611 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73  ash table */.  s
33612 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70  truct _fts3ht *p
33613 45 6e 74 72 79 2c 20 20 2f 2a 20 54 68 65 20 65  Entry,  /* The e
33614 6e 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20  ntry into which 
33615 70 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64  pNew is inserted
33616 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c   */.  fts3HashEl
33617 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20 2f  em *pNew       /
33618 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f  * The element to
33619 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
3361a 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  ){.  fts3HashEle
3361b 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 20 2f 2a  m *pHead;     /*
3361c 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 61   First element a
3361d 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72 79  lready in pEntry
3361e 20 2a 2f 0a 20 20 70 48 65 61 64 20 3d 20 70 45   */.  pHead = pE
3361f 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 69  ntry->chain;.  i
33620 66 28 20 70 48 65 61 64 20 29 7b 0a 20 20 20 20  f( pHead ){.    
33621 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 65  pNew->next = pHe
33622 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72  ad;.    pNew->pr
33623 65 76 20 3d 20 70 48 65 61 64 2d 3e 70 72 65 76  ev = pHead->prev
33624 3b 0a 20 20 20 20 69 66 28 20 70 48 65 61 64 2d  ;.    if( pHead-
33625 3e 70 72 65 76 20 29 7b 20 70 48 65 61 64 2d 3e  >prev ){ pHead->
33626 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 70 4e 65  prev->next = pNe
33627 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65 20 20 20  w; }.    else   
33628 20 20 20 20 20 20 20 20 20 20 7b 20 70 48 2d 3e            { pH->
33629 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 20 7d 0a  first = pNew; }.
3362a 20 20 20 20 70 48 65 61 64 2d 3e 70 72 65 76 20      pHead->prev 
3362b 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b  = pNew;.  }else{
3362c 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20  .    pNew->next 
3362d 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20  = pH->first;.   
3362e 20 69 66 28 20 70 48 2d 3e 66 69 72 73 74 20 29   if( pH->first )
3362f 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70 72 65  { pH->first->pre
33630 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20  v = pNew; }.    
33631 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 30 3b 0a  pNew->prev = 0;.
33632 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20      pH->first = 
33633 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  pNew;.  }.  pEnt
33634 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 70  ry->count++;.  p
33635 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70  Entry->chain = p
33636 4e 65 77 3b 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69  New;.}.../* Resi
33637 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  ze the hash tabl
33638 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e  e so that it can
33639 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22  tains "new_size"
3363a 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 20 22 6e 65   buckets..** "ne
3363b 77 5f 73 69 7a 65 22 20 6d 75 73 74 20 62 65 20  w_size" must be 
3363c 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 20 54  a power of 2.  T
3363d 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69  he hash table mi
3363e 67 68 74 20 66 61 69 6c 20 0a 2a 2a 20 74 6f 20  ght fail .** to 
3363f 72 65 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65  resize if sqlite
33640 4d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a  Malloc() fails..
33641 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
33642 74 73 33 52 65 68 61 73 68 28 66 74 73 33 48 61  ts3Rehash(fts3Ha
33643 73 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65 77 5f  sh *pH, int new_
33644 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74 20  size){.  struct 
33645 5f 66 74 73 33 68 74 20 2a 6e 65 77 5f 68 74 3b  _fts3ht *new_ht;
33646 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
33647 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20   new hash table 
33648 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  */.  fts3HashEle
33649 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65  m *elem, *next_e
3364a 6c 65 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f  lem;  /* For loo
3364b 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69  ping over existi
3364c 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20  ng elements */. 
3364d 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f   int (*xHash)(co
3364e 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
3364f 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75    /* The hash fu
33650 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73  nction */..  ass
33651 65 72 74 28 20 28 6e 65 77 5f 73 69 7a 65 20 26  ert( (new_size &
33652 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 29 3d 3d   (new_size-1))==
33653 30 20 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20  0 );.  new_ht = 
33654 28 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20  (struct _fts3ht 
33655 2a 29 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63  *)fts3HashMalloc
33656 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f  ( new_size*sizeo
33657 66 28 73 74 72 75 63 74 20 5f 66 74 73 33 68 74  f(struct _fts3ht
33658 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 68  ) );.  if( new_h
33659 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
3365a 20 66 74 73 33 48 61 73 68 46 72 65 65 28 70 48   fts3HashFree(pH
3365b 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20  ->ht);.  pH->ht 
3365c 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e  = new_ht;.  pH->
3365d 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a  htsize = new_siz
3365e 65 3b 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73  e;.  xHash = fts
3365f 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d  HashFunction(pH-
33660 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 66 6f  >keyClass);.  fo
33661 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74  r(elem=pH->first
33662 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65  , pH->first=0; e
33663 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74  lem; elem = next
33664 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 69 6e 74 20  _elem){.    int 
33665 68 20 3d 20 28 2a 78 48 61 73 68 29 28 65 6c 65  h = (*xHash)(ele
33666 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e  m->pKey, elem->n
33667 4b 65 79 29 20 26 20 28 6e 65 77 5f 73 69 7a 65  Key) & (new_size
33668 2d 31 29 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c  -1);.    next_el
33669 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  em = elem->next;
3366a 0a 20 20 20 20 66 74 73 33 48 61 73 68 49 6e 73  .    fts3HashIns
3366b 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26  ertElement(pH, &
3366c 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d 29  new_ht[h], elem)
3366d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 54 68 69 73  ;.  }.}../* This
3366e 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69   function (for i
3366f 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79  nternal use only
33670 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65  ) locates an ele
33671 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61  ment in an.** ha
33672 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61  sh table that ma
33673 74 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20  tches the given 
33674 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66  key.  The hash f
33675 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a  or this key has.
33676 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ** already been 
33677 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20  computed and is 
33678 70 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74  passed as the 4t
33679 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a  h parameter..*/.
3367a 73 74 61 74 69 63 20 66 74 73 33 48 61 73 68 45  static fts3HashE
3367b 6c 65 6d 20 2a 66 74 73 33 46 69 6e 64 45 6c 65  lem *fts3FindEle
3367c 6d 65 6e 74 42 79 48 61 73 68 28 0a 20 20 63 6f  mentByHash(.  co
3367d 6e 73 74 20 66 74 73 33 48 61 73 68 20 2a 70 48  nst fts3Hash *pH
3367e 2c 20 2f 2a 20 54 68 65 20 70 48 20 74 6f 20 62  , /* The pH to b
3367f 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20  e searched */.  
33680 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
33681 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 77  ,   /* The key w
33682 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 20  e are searching 
33683 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65  for */.  int nKe
33684 79 2c 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20  y,.  int h      
33685 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
33686 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65  hash for this ke
33687 79 2e 20 2a 2f 0a 29 7b 0a 20 20 66 74 73 33 48  y. */.){.  fts3H
33688 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
33689 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
3368a 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74  d to loop thru t
3368b 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20  he element list 
3368c 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20  */.  int count; 
3368d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3368e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3368f 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74 20 74   elements left t
33690 6f 20 74 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20  o test */.  int 
33691 28 2a 78 43 6f 6d 70 61 72 65 29 28 63 6f 6e 73  (*xCompare)(cons
33692 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
33693 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f  t void*,int);  /
33694 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  * comparison fun
33695 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20  ction */..  if( 
33696 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 73 74  pH->ht ){.    st
33697 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70 45  ruct _fts3ht *pE
33698 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68  ntry = &pH->ht[h
33699 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 45  ];.    elem = pE
3369a 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20  ntry->chain;.   
3369b 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d   count = pEntry-
3369c 3e 63 6f 75 6e 74 3b 0a 20 20 20 20 78 43 6f 6d  >count;.    xCom
3369d 70 61 72 65 20 3d 20 66 74 73 43 6f 6d 70 61 72  pare = ftsCompar
3369e 65 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65  eFunction(pH->ke
3369f 79 43 6c 61 73 73 29 3b 0a 20 20 20 20 77 68 69  yClass);.    whi
336a0 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 65  le( count-- && e
336a1 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 69 66 28  lem ){.      if(
336a2 20 28 2a 78 43 6f 6d 70 61 72 65 29 28 65 6c 65   (*xCompare)(ele
336a3 6d 2d 3e 70 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b  m->pKey,elem->nK
336a4 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30  ey,pKey,nKey)==0
336a5 20 29 7b 20 0a 20 20 20 20 20 20 20 20 72 65 74   ){ .        ret
336a6 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 20 20  urn elem;.      
336a7 7d 0a 20 20 20 20 20 20 65 6c 65 6d 20 3d 20 65  }.      elem = e
336a8 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d  lem->next;.    }
336a9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
336aa 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20  .}../* Remove a 
336ab 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f  single entry fro
336ac 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
336ad 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72   given a pointer
336ae 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d   to that.** elem
336af 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 6f  ent and a hash o
336b0 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 20  n the element's 
336b1 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  key..*/.static v
336b2 6f 69 64 20 66 74 73 33 52 65 6d 6f 76 65 45 6c  oid fts3RemoveEl
336b3 65 6d 65 6e 74 42 79 48 61 73 68 28 0a 20 20 66  ementByHash(.  f
336b4 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20 20 20  ts3Hash *pH,    
336b5 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63       /* The pH c
336b6 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22  ontaining "elem"
336b7 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c   */.  fts3HashEl
336b8 65 6d 2a 20 65 6c 65 6d 2c 20 20 20 2f 2a 20 54  em* elem,   /* T
336b9 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65  he element to be
336ba 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
336bb 65 20 70 48 20 2a 2f 0a 20 20 69 6e 74 20 68 20  e pH */.  int h 
336bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336bd 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66 6f  /* Hash value fo
336be 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f  r the element */
336bf 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 66 74  .){.  struct _ft
336c0 73 33 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20  s3ht *pEntry;.  
336c1 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29  if( elem->prev )
336c2 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76  {.    elem->prev
336c3 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e  ->next = elem->n
336c4 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20  ext; .  }else{. 
336c5 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65     pH->first = e
336c6 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20  lem->next;.  }. 
336c7 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20   if( elem->next 
336c8 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78  ){.    elem->nex
336c9 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e  t->prev = elem->
336ca 70 72 65 76 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  prev;.  }.  pEnt
336cb 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b  ry = &pH->ht[h];
336cc 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63  .  if( pEntry->c
336cd 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20  hain==elem ){.  
336ce 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20    pEntry->chain 
336cf 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20  = elem->next;.  
336d0 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  }.  pEntry->coun
336d1 74 2d 2d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72  t--;.  if( pEntr
336d2 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20  y->count<=0 ){. 
336d3 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e     pEntry->chain
336d4 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
336d5 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20 65  pH->copyKey && e
336d6 6c 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a 20 20 20  lem->pKey ){.   
336d7 20 66 74 73 33 48 61 73 68 46 72 65 65 28 65 6c   fts3HashFree(el
336d8 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d 0a 20  em->pKey);.  }. 
336d9 20 66 74 73 33 48 61 73 68 46 72 65 65 28 20 65   fts3HashFree( e
336da 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75  lem );.  pH->cou
336db 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e  nt--;.  if( pH->
336dc 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20  count<=0 ){.    
336dd 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69 72 73  assert( pH->firs
336de 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t==0 );.    asse
336df 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30  rt( pH->count==0
336e0 20 29 3b 0a 20 20 20 20 66 74 73 33 48 61 73 68   );.    fts3Hash
336e1 43 6c 65 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d  Clear(pH);.  }.}
336e2 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  ../* Attempt to 
336e3 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e  locate an elemen
336e4 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61  t of the hash ta
336e5 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b 65  ble pH with a ke
336e6 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65  y.** that matche
336e7 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65  s pKey,nKey.  Re
336e8 74 75 72 6e 20 74 68 65 20 64 61 74 61 20 66 6f  turn the data fo
336e9 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69  r this element i
336ea 66 20 69 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64  f it is.** found
336eb 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65  , or NULL if the
336ec 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a  re is no match..
336ed 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
336ee 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 46  E void *sqlite3F
336ef 74 73 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73  ts3HashFind(cons
336f0 74 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20  t fts3Hash *pH, 
336f1 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
336f2 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69  , int nKey){.  i
336f3 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20  nt h;           
336f4 20 20 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20        /* A hash 
336f5 6f 6e 20 6b 65 79 20 2a 2f 0a 20 20 66 74 73 33  on key */.  fts3
336f6 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
336f7 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e     /* The elemen
336f8 74 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b  t that matches k
336f9 65 79 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48  ey */.  int (*xH
336fa 61 73 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  ash)(const void*
336fb 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 68  ,int);  /* The h
336fc 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ash function */.
336fd 0a 20 20 69 66 28 20 70 48 3d 3d 30 20 7c 7c 20  .  if( pH==0 || 
336fe 70 48 2d 3e 68 74 3d 3d 30 20 29 20 72 65 74 75  pH->ht==0 ) retu
336ff 72 6e 20 30 3b 0a 20 20 78 48 61 73 68 20 3d 20  rn 0;.  xHash = 
33700 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28  ftsHashFunction(
33701 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20  pH->keyClass);. 
33702 20 61 73 73 65 72 74 28 20 78 48 61 73 68 21 3d   assert( xHash!=
33703 30 20 29 3b 0a 20 20 68 20 3d 20 28 2a 78 48 61  0 );.  h = (*xHa
33704 73 68 29 28 70 4b 65 79 2c 6e 4b 65 79 29 3b 0a  sh)(pKey,nKey);.
33705 20 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e 68    assert( (pH->h
33706 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74 73  tsize & (pH->hts
33707 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  ize-1))==0 );.  
33708 65 6c 65 6d 20 3d 20 66 74 73 33 46 69 6e 64 45  elem = fts3FindE
33709 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 70 48 2c  lementByHash(pH,
3370a 70 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 26 20 28  pKey,nKey, h & (
3370b 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3b 0a  pH->htsize-1));.
3370c 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20    return elem ? 
3370d 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a  elem->data : 0;.
3370e 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20  }../* Insert an 
3370f 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65  element into the
33710 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20   hash table pH. 
33711 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79   The key is pKey
33712 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65  ,nKey.** and the
33713 20 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e   data is "data".
33714 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65  .**.** If no ele
33715 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68  ment exists with
33716 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c   a matching key,
33717 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65   then a new.** e
33718 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65  lement is create
33719 64 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 68  d.  A copy of th
3371a 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66  e key is made if
3371b 20 74 68 65 20 63 6f 70 79 4b 65 79 0a 2a 2a 20   the copyKey.** 
3371c 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e 55  flag is set.  NU
3371d 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  LL is returned..
3371e 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72  **.** If another
3371f 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79   element already
33720 20 65 78 69 73 74 73 20 77 69 74 68 20 74 68 65   exists with the
33721 20 73 61 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20   same key, then 
33722 74 68 65 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20  the.** new data 
33723 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64  replaces the old
33724 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 6f 6c   data and the ol
33725 64 20 64 61 74 61 20 69 73 20 72 65 74 75 72 6e  d data is return
33726 65 64 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69  ed..** The key i
33727 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 69 6e 20  s not copied in 
33728 74 68 69 73 20 69 6e 73 74 61 6e 63 65 2e 20 20  this instance.  
33729 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  If a malloc fail
3372a 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e  s, then.** the n
3372b 65 77 20 64 61 74 61 20 69 73 20 72 65 74 75 72  ew data is retur
3372c 6e 65 64 20 61 6e 64 20 74 68 65 20 68 61 73 68  ned and the hash
3372d 20 74 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e   table is unchan
3372e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ged..**.** If th
3372f 65 20 22 64 61 74 61 22 20 70 61 72 61 6d 65 74  e "data" paramet
33730 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  er to this funct
33731 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  ion is NULL, the
33732 6e 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74  n the.** element
33733 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
33734 6f 20 22 6b 65 79 22 20 69 73 20 72 65 6d 6f 76  o "key" is remov
33735 65 64 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  ed from the hash
33736 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
33737 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
33738 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
33739 6e 73 65 72 74 28 0a 20 20 66 74 73 33 48 61 73  nsert(.  fts3Has
3373a 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 2f 2a  h *pH,        /*
3373b 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   The hash table 
3373c 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a  to insert into *
3373d 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
3373e 70 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20  pKey,    /* The 
3373f 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65  key */.  int nKe
33740 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  y,            /*
33741 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
33742 20 69 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20   in the key */. 
33743 20 76 6f 69 64 20 2a 64 61 74 61 20 20 20 20 20   void *data     
33744 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
33745 61 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 68 72  a */.){.  int hr
33746 61 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  aw;             
33747 20 20 20 20 2f 2a 20 52 61 77 20 68 61 73 68 20      /* Raw hash 
33748 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79  value of the key
33749 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20   */.  int h;    
3374a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3374b 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66 20 74  /* the hash of t
3374c 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61  he key modulo ha
3374d 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f  sh table size */
3374e 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20  .  fts3HashElem 
3374f 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20  *elem;       /* 
33750 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72  Used to loop thr
33751 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69  u the element li
33752 73 74 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68  st */.  fts3Hash
33753 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20  Elem *new_elem; 
33754 20 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74    /* New element
33755 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 48   added to the pH
33756 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61 73   */.  int (*xHas
33757 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  h)(const void*,i
33758 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 68 61 73  nt);  /* The has
33759 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20  h function */.. 
3375a 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29   assert( pH!=0 )
3375b 3b 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73 48  ;.  xHash = ftsH
3375c 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e  ashFunction(pH->
3375d 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 61 73 73  keyClass);.  ass
3375e 65 72 74 28 20 78 48 61 73 68 21 3d 30 20 29 3b  ert( xHash!=0 );
3375f 0a 20 20 68 72 61 77 20 3d 20 28 2a 78 48 61 73  .  hraw = (*xHas
33760 68 29 28 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a  h)(pKey, nKey);.
33761 20 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e 68    assert( (pH->h
33762 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74 73  tsize & (pH->hts
33763 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  ize-1))==0 );.  
33764 68 20 3d 20 68 72 61 77 20 26 20 28 70 48 2d 3e  h = hraw & (pH->
33765 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20 65 6c 65  htsize-1);.  ele
33766 6d 20 3d 20 66 74 73 33 46 69 6e 64 45 6c 65 6d  m = fts3FindElem
33767 65 6e 74 42 79 48 61 73 68 28 70 48 2c 70 4b 65  entByHash(pH,pKe
33768 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 69 66 28  y,nKey,h);.  if(
33769 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 76 6f 69   elem ){.    voi
3376a 64 20 2a 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c  d *old_data = el
3376b 65 6d 2d 3e 64 61 74 61 3b 0a 20 20 20 20 69 66  em->data;.    if
3376c 28 20 64 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  ( data==0 ){.   
3376d 20 20 20 66 74 73 33 52 65 6d 6f 76 65 45 6c 65     fts3RemoveEle
3376e 6d 65 6e 74 42 79 48 61 73 68 28 70 48 2c 65 6c  mentByHash(pH,el
3376f 65 6d 2c 68 29 3b 0a 20 20 20 20 7d 65 6c 73 65  em,h);.    }else
33770 7b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61  {.      elem->da
33771 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 20 20 7d  ta = data;.    }
33772 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f  .    return old_
33773 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20  data;.  }.  if( 
33774 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e  data==0 ) return
33775 20 30 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74   0;.  if( pH->ht
33776 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 66  size==0 ){.    f
33777 74 73 33 52 65 68 61 73 68 28 70 48 2c 38 29 3b  ts3Rehash(pH,8);
33778 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 68 74 73  .    if( pH->hts
33779 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ize==0 ){.      
3377a 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20  pH->count = 0;. 
3377b 20 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61       return data
3377c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6e 65  ;.    }.  }.  ne
3377d 77 5f 65 6c 65 6d 20 3d 20 28 66 74 73 33 48 61  w_elem = (fts3Ha
3377e 73 68 45 6c 65 6d 2a 29 66 74 73 33 48 61 73 68  shElem*)fts3Hash
3377f 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 66  Malloc( sizeof(f
33780 74 73 33 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a  ts3HashElem) );.
33781 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d    if( new_elem==
33782 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b  0 ) return data;
33783 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b  .  if( pH->copyK
33784 65 79 20 26 26 20 70 4b 65 79 21 3d 30 20 29 7b  ey && pKey!=0 ){
33785 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70  .    new_elem->p
33786 4b 65 79 20 3d 20 66 74 73 33 48 61 73 68 4d 61  Key = fts3HashMa
33787 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20 20  lloc( nKey );.  
33788 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d 3e    if( new_elem->
33789 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pKey==0 ){.     
3378a 20 66 74 73 33 48 61 73 68 46 72 65 65 28 6e 65   fts3HashFree(ne
3378b 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 72  w_elem);.      r
3378c 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20  eturn data;.    
3378d 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 28 76 6f  }.    memcpy((vo
3378e 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b  id*)new_elem->pK
3378f 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b  ey, pKey, nKey);
33790 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65  .  }else{.    ne
33791 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28  w_elem->pKey = (
33792 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d 0a  void*)pKey;.  }.
33793 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79    new_elem->nKey
33794 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e 63   = nKey;.  pH->c
33795 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48  ount++;.  if( pH
33796 2d 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68 74  ->count > pH->ht
33797 73 69 7a 65 20 29 7b 0a 20 20 20 20 66 74 73 33  size ){.    fts3
33798 52 65 68 61 73 68 28 70 48 2c 70 48 2d 3e 68 74  Rehash(pH,pH->ht
33799 73 69 7a 65 2a 32 29 3b 0a 20 20 7d 0a 20 20 61  size*2);.  }.  a
3379a 73 73 65 72 74 28 20 70 48 2d 3e 68 74 73 69 7a  ssert( pH->htsiz
3379b 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  e>0 );.  assert(
3379c 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28   (pH->htsize & (
3379d 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d  pH->htsize-1))==
3379e 30 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77 20  0 );.  h = hraw 
3379f 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29  & (pH->htsize-1)
337a0 3b 0a 20 20 66 74 73 33 48 61 73 68 49 6e 73 65  ;.  fts3HashInse
337a1 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70  rtElement(pH, &p
337a2 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c  H->ht[h], new_el
337a3 65 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d  em);.  new_elem-
337a4 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20  >data = data;.  
337a5 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 65 6e  return 0;.}..#en
337a6 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
337a7 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
337a8 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
337a9 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
337aa 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
337ab 45 6e 64 20 6f 66 20 66 74 73 33 5f 68 61 73 68  End of fts3_hash
337ac 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
337ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
337af 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
337b0 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
337b1 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  porter.c *******
337b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
337b4 2f 2a 0a 2a 2a 20 32 30 30 36 20 53 65 70 74 65  /*.** 2006 Septe
337b5 6d 62 65 72 20 33 30 0a 2a 2a 0a 2a 2a 20 54 68  mber 30.**.** Th
337b6 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
337b7 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
337b8 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
337b9 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
337ba 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
337bb 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
337bc 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
337bd 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
337be 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
337bf 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
337c0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
337c1 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
337c2 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
337c3 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
337c4 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
337c5 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
337c6 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
337c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337cb 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  ******.** Implem
337cc 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
337cd 66 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63 68  full-text-search
337ce 20 74 6f 6b 65 6e 69 7a 65 72 20 74 68 61 74 20   tokenizer that 
337cf 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 20  implements.** a 
337d0 50 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 2e 0a  Porter stemmer..
337d1 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
337d2 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
337d3 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  is only compiled
337d4 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a   if:.**.**     *
337d5 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
337d6 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
337d7 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a  as an extension.
337d8 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68 69  **       (in whi
337d9 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43  ch case SQLITE_C
337da 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  ORE is not defin
337db 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20  ed), or.**.**   
337dc 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64    * The FTS3 mod
337dd 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69  ule is being bui
337de 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65  lt into the core
337df 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c   of.**       SQL
337e0 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63 61  ite (in which ca
337e1 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  se SQLITE_ENABLE
337e2 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65 64  _FTS3 is defined
337e3 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  )..*/.#if !defin
337e4 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20  ed(SQLITE_CORE) 
337e5 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
337e6 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a  E_ENABLE_FTS3)..
337e7 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64  .../*.** Class d
337e8 65 72 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69  erived from sqli
337e9 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f  te3_tokenizer.*/
337ea 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
337eb 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
337ec 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b   {.  sqlite3_tok
337ed 65 6e 69 7a 65 72 20 62 61 73 65 3b 20 20 20 20  enizer base;    
337ee 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73 20    /* Base class 
337ef 2a 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f 6b 65  */.} porter_toke
337f0 6e 69 7a 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6c  nizer;../*.** Cl
337f1 61 73 73 20 64 65 72 69 76 65 64 20 66 72 6f 6d  ass derived from
337f2 20 73 71 6c 69 74 33 5f 74 6f 6b 65 6e 69 7a 65   sqlit3_tokenize
337f3 72 5f 63 75 72 73 6f 72 0a 2a 2f 0a 74 79 70 65  r_cursor.*/.type
337f4 64 65 66 20 73 74 72 75 63 74 20 70 6f 72 74 65  def struct porte
337f5 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  r_tokenizer_curs
337f6 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
337f7 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
337f8 62 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68  base;.  const ch
337f9 61 72 20 2a 7a 49 6e 70 75 74 3b 20 20 20 20 20  ar *zInput;     
337fa 20 20 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65       /* input we
337fb 20 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20   are tokenizing 
337fc 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b  */.  int nInput;
337fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
337fe 20 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65    /* size of the
337ff 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20   input */.  int 
33800 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  iOffset;        
33801 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72           /* curr
33802 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  ent position in 
33803 7a 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  zInput */.  int 
33804 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  iToken;         
33805 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65           /* inde
33806 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20  x of next token 
33807 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a  to be returned *
33808 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e  /.  char *zToken
33809 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3380a 20 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f 72 20   /* storage for 
3380b 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  current token */
3380c 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 61 74 65  .  int nAllocate
3380d 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
3380e 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  /* space allocat
3380f 65 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66  ed to zToken buf
33810 66 65 72 20 2a 2f 0a 7d 20 70 6f 72 74 65 72 5f  fer */.} porter_
33811 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
33812 3b 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  ;.../* Forward d
33813 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74  eclaration */.st
33814 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
33815 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
33816 75 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69  ule porterTokeni
33817 7a 65 72 4d 6f 64 75 6c 65 3b 0a 0a 0a 2f 2a 0a  zerModule;.../*.
33818 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
33819 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e  tokenizer instan
3381a 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
3381b 74 20 70 6f 72 74 65 72 43 72 65 61 74 65 28 0a  t porterCreate(.
3381c 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
3381d 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
3381e 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argv,.  sqlite3_
3381f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f  tokenizer **ppTo
33820 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 70 6f 72  kenizer.){.  por
33821 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74  ter_tokenizer *t
33822 3b 0a 20 20 74 20 3d 20 28 70 6f 72 74 65 72 5f  ;.  t = (porter_
33823 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71 6c  tokenizer *) sql
33824 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
33825 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66 28 20 74  of(*t));.  if( t
33826 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20  ==NULL ) return 
33827 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
33828 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20 73 69 7a  memset(t, 0, siz
33829 65 6f 66 28 2a 74 29 29 3b 0a 20 20 2a 70 70 54  eof(*t));.  *ppT
3382a 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d 3e 62  okenizer = &t->b
3382b 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ase;.  return SQ
3382c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
3382d 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b 65  * Destroy a toke
3382e 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20  nizer.*/.static 
3382f 69 6e 74 20 70 6f 72 74 65 72 44 65 73 74 72 6f  int porterDestro
33830 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  y(sqlite3_tokeni
33831 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29  zer *pTokenizer)
33832 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  {.  sqlite3_free
33833 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20  (pTokenizer);.  
33834 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
33835 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
33836 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65  re to begin toke
33837 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75  nizing a particu
33838 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65  lar string.  The
33839 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67   input.** string
3383a 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64   to be tokenized
3383b 20 69 73 20 7a 49 6e 70 75 74 5b 30 2e 2e 6e 49   is zInput[0..nI
3383c 6e 70 75 74 2d 31 5d 2e 20 20 41 20 63 75 72 73  nput-1].  A curs
3383d 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e  or.** used to in
3383e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65  crementally toke
3383f 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67  nize this string
33840 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
33841 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a  .** *ppCursor..*
33842 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72  /.static int por
33843 74 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  terOpen(.  sqlit
33844 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
33845 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20  okenizer,       
33846 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a    /* The tokeniz
33847 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  er */.  const ch
33848 61 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e 74 20  ar *zInput, int 
33849 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 2f  nInput,        /
3384a 2a 20 53 74 72 69 6e 67 20 74 6f 20 62 65 20 74  * String to be t
3384b 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20 73 71  okenized */.  sq
3384c 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3384d 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f  cursor **ppCurso
3384e 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b  r    /* OUT: Tok
3384f 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
33850 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f   */.){.  porter_
33851 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
33852 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 70 6f 72   *c;..  c = (por
33853 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ter_tokenizer_cu
33854 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  rsor *) sqlite3_
33855 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 63  malloc(sizeof(*c
33856 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e 55 4c  ));.  if( c==NUL
33857 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
33858 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 7a  E_NOMEM;..  c->z
33859 49 6e 70 75 74 20 3d 20 7a 49 6e 70 75 74 3b 0a  Input = zInput;.
3385a 20 20 69 66 28 20 7a 49 6e 70 75 74 3d 3d 30 20    if( zInput==0 
3385b 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75 74  ){.    c->nInput
3385c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
3385d 28 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a 20 20  ( nInput<0 ){.  
3385e 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 28 69    c->nInput = (i
3385f 6e 74 29 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74  nt)strlen(zInput
33860 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
33861 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 6e 49 6e 70  c->nInput = nInp
33862 75 74 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f 66  ut;.  }.  c->iOf
33863 66 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20 20  fset = 0;       
33864 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61            /* sta
33865 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 74  rt tokenizing at
33866 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 2a   the beginning *
33867 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20  /.  c->iToken = 
33868 30 3b 0a 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20 3d  0;.  c->zToken =
33869 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 20 20   NULL;          
3386a 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61 63 65       /* no space
3386b 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65 74 2e   allocated, yet.
3386c 20 2a 2f 0a 20 20 63 2d 3e 6e 41 6c 6c 6f 63 61   */.  c->nAlloca
3386d 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70 70 43  ted = 0;..  *ppC
3386e 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65  ursor = &c->base
3386f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
33870 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
33871 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74  lose a tokenizat
33872 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65 76 69  ion cursor previ
33873 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62 79 20  ously opened by 
33874 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 70 6f 72  a call to.** por
33875 74 65 72 4f 70 65 6e 28 29 20 61 62 6f 76 65 2e  terOpen() above.
33876 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
33877 6f 72 74 65 72 43 6c 6f 73 65 28 73 71 6c 69 74  orterClose(sqlit
33878 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
33879 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
3387a 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65   porter_tokenize
3387b 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 70  r_cursor *c = (p
3387c 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
3387d 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
3387e 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  r;.  sqlite3_fre
3387f 65 28 63 2d 3e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  e(c->zToken);.  
33880 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 29 3b  sqlite3_free(c);
33881 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
33882 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 56 6f 77  _OK;.}./*.** Vow
33883 65 6c 20 6f 72 20 63 6f 6e 73 6f 6e 61 6e 74 0a  el or consonant.
33884 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
33885 63 68 61 72 20 63 54 79 70 65 5b 5d 20 3d 20 7b  char cType[] = {
33886 0a 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20  .   0, 1, 1, 1, 
33887 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  0, 1, 1, 1, 0, 1
33888 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
33889 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3388a 30 2c 0a 20 20 20 31 2c 20 31 2c 20 31 2c 20 32  0,.   1, 1, 1, 2
3388b 2c 20 31 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69 73  , 1.};../*.** is
3388c 43 6f 6e 73 6f 6e 61 6e 74 28 29 20 61 6e 64 20  Consonant() and 
3388d 69 73 56 6f 77 65 6c 28 29 20 64 65 74 65 72 6d  isVowel() determ
3388e 69 6e 65 20 69 66 20 74 68 65 69 72 20 66 69 72  ine if their fir
3388f 73 74 20 63 68 61 72 61 63 74 65 72 20 69 6e 0a  st character in.
33890 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 74 68  ** the string th
33891 65 79 20 70 6f 69 6e 74 20 74 6f 20 69 73 20 61  ey point to is a
33892 20 63 6f 6e 73 6f 6e 61 6e 74 20 6f 72 20 61 20   consonant or a 
33893 76 6f 77 65 6c 2c 20 61 63 63 6f 72 64 69 6e 67  vowel, according
33894 0a 2a 2a 20 74 6f 20 50 6f 72 74 65 72 20 72 75  .** to Porter ru
33895 6c 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 63 6f  ls.  .**.** A co
33896 6e 73 6f 6e 61 74 65 20 69 73 20 61 6e 79 20 6c  nsonate is any l
33897 65 74 74 65 72 20 6f 74 68 65 72 20 74 68 61 6e  etter other than
33898 20 27 61 27 2c 20 27 65 27 2c 20 27 69 27 2c 20   'a', 'e', 'i', 
33899 27 6f 27 2c 20 6f 72 20 27 75 27 2e 0a 2a 2a 20  'o', or 'u'..** 
3389a 27 59 27 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61  'Y' is a consona
3389b 6e 74 20 75 6e 6c 65 73 73 20 69 74 20 66 6f 6c  nt unless it fol
3389c 6c 6f 77 73 20 61 6e 6f 74 68 65 72 20 63 6f 6e  lows another con
3389d 73 6f 6e 61 6e 74 2c 0a 2a 2a 20 69 6e 20 77 68  sonant,.** in wh
3389e 69 63 68 20 63 61 73 65 20 69 74 20 69 73 20 61  ich case it is a
3389f 20 76 6f 77 65 6c 2e 0a 2a 2a 0a 2a 2a 20 49 6e   vowel..**.** In
338a0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 2c 20   these routine, 
338a1 74 68 65 20 6c 65 74 74 65 72 73 20 61 72 65 20  the letters are 
338a2 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72  in reverse order
338a3 2e 20 20 53 6f 20 74 68 65 20 27 79 27 20 72 75  .  So the 'y' ru
338a4 6c 65 0a 2a 2a 20 69 73 20 74 68 61 74 20 27 79  le.** is that 'y
338a5 27 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74  ' is a consonant
338a6 20 75 6e 6c 65 73 73 20 69 74 20 69 73 20 66 6f   unless it is fo
338a7 6c 6c 6f 77 65 64 20 62 79 20 61 6e 6f 74 68 65  llowed by anothe
338a8 72 0a 2a 2a 20 63 6f 6e 73 6f 6e 65 6e 74 2e 0a  r.** consonent..
338a9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  */.static int is
338aa 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72  Vowel(const char
338ab 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 69  *);.static int i
338ac 73 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73 74  sConsonant(const
338ad 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74   char *z){.  int
338ae 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d 20 2a   j;.  char x = *
338af 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20 29 20  z;.  if( x==0 ) 
338b0 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65  return 0;.  asse
338b1 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20 78 3c  rt( x>='a' && x<
338b2 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20 63 54  ='z' );.  j = cT
338b3 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20 69 66  ype[x-'a'];.  if
338b4 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e 20 6a  ( j<2 ) return j
338b5 3b 0a 20 20 72 65 74 75 72 6e 20 7a 5b 31 5d 3d  ;.  return z[1]=
338b6 3d 30 20 7c 7c 20 69 73 56 6f 77 65 6c 28 7a 20  =0 || isVowel(z 
338b7 2b 20 31 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69  + 1);.}.static i
338b8 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f 6e 73 74  nt isVowel(const
338b9 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74   char *z){.  int
338ba 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d 20 2a   j;.  char x = *
338bb 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20 29 20  z;.  if( x==0 ) 
338bc 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65  return 0;.  asse
338bd 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20 78 3c  rt( x>='a' && x<
338be 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20 63 54  ='z' );.  j = cT
338bf 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20 69 66  ype[x-'a'];.  if
338c0 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e 20 31  ( j<2 ) return 1
338c1 2d 6a 3b 0a 20 20 72 65 74 75 72 6e 20 69 73 43  -j;.  return isC
338c2 6f 6e 73 6f 6e 61 6e 74 28 7a 20 2b 20 31 29 3b  onsonant(z + 1);
338c3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 61 6e  .}../*.** Let an
338c4 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e  y sequence of on
338c5 65 20 6f 72 20 6d 6f 72 65 20 76 6f 77 65 6c 73  e or more vowels
338c6 20 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 20   be represented 
338c7 62 79 20 56 20 61 6e 64 20 6c 65 74 0a 2a 2a 20  by V and let.** 
338c8 43 20 62 65 20 73 65 71 75 65 6e 63 65 20 6f 66  C be sequence of
338c9 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 6f 6e   one or more con
338ca 73 6f 6e 61 6e 74 73 2e 20 20 54 68 65 6e 20 65  sonants.  Then e
338cb 76 65 72 79 20 77 6f 72 64 20 63 61 6e 20 62 65  very word can be
338cc 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64 20  .** represented 
338cd 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  as:.**.**       
338ce 20 20 20 20 5b 43 5d 20 28 56 43 29 7b 6d 7d 20      [C] (VC){m} 
338cf 5b 56 5d 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 6f  [V].**.** In pro
338d0 73 65 3a 20 20 41 20 77 6f 72 64 20 69 73 20 61  se:  A word is a
338d1 6e 20 6f 70 74 69 6f 6e 61 6c 20 63 6f 6e 73 6f  n optional conso
338d2 6e 61 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79  nant followed by
338d3 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 76 6f 77 65   zero or.** vowe
338d4 6c 2d 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72  l-consonant pair
338d5 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e  s followed by an
338d6 20 6f 70 74 69 6f 6e 61 6c 20 76 6f 77 65 6c 2e   optional vowel.
338d7 20 20 22 6d 22 20 69 73 20 74 68 65 0a 2a 2a 20    "m" is the.** 
338d8 6e 75 6d 62 65 72 20 6f 66 20 76 6f 77 65 6c 20  number of vowel 
338d9 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72 73 2e  consonant pairs.
338da 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63    This routine c
338db 6f 6d 70 75 74 65 73 20 74 68 65 20 76 61 6c 75  omputes the valu
338dc 65 0a 2a 2a 20 6f 66 20 6d 20 66 6f 72 20 74 68  e.** of m for th
338dd 65 20 66 69 72 73 74 20 69 20 62 79 74 65 73 20  e first i bytes 
338de 6f 66 20 61 20 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a  of a word..**.**
338df 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
338e0 74 68 65 20 6d 2d 76 61 6c 75 65 20 66 6f 72 20  the m-value for 
338e1 7a 20 69 73 20 31 20 6f 72 20 6d 6f 72 65 2e 20  z is 1 or more. 
338e2 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
338e3 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20  .** return true 
338e4 69 66 20 7a 20 63 6f 6e 74 61 69 6e 73 20 61 74  if z contains at
338e5 20 6c 65 61 73 74 20 6f 6e 65 20 76 6f 77 65 6c   least one vowel
338e6 20 74 68 61 74 20 69 73 20 66 6f 6c 6c 6f 77 65   that is followe
338e7 64 0a 2a 2a 20 62 79 20 61 20 63 6f 6e 73 6f 6e  d.** by a conson
338e8 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ant..**.** In th
338e9 69 73 20 72 6f 75 74 69 6e 65 20 7a 5b 5d 20 69  is routine z[] i
338ea 73 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  s in reverse ord
338eb 65 72 2e 20 20 53 6f 20 77 65 20 61 72 65 20 72  er.  So we are r
338ec 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a  eally looking.**
338ed 20 66 6f 72 20 61 6e 20 69 6e 73 74 61 6e 63 65   for an instance
338ee 20 6f 66 20 6f 66 20 61 20 63 6f 6e 73 6f 6e 61   of of a consona
338ef 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  nt followed by a
338f0 20 76 6f 77 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69   vowel..*/.stati
338f1 63 20 69 6e 74 20 6d 5f 67 74 5f 30 28 63 6f 6e  c int m_gt_0(con
338f2 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77  st char *z){.  w
338f3 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
338f4 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
338f5 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
338f6 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  0;.  while( isCo
338f7 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
338f8 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
338f9 21 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20  !=0;.}../* Like 
338fa 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63 65 70  mgt0 above excep
338fb 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67  t we are looking
338fc 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f 66 20   for a value of 
338fd 6d 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 65 78  m which is.** ex
338fe 61 63 74 6c 79 20 31 0a 2a 2f 0a 73 74 61 74 69  actly 1.*/.stati
338ff 63 20 69 6e 74 20 6d 5f 65 71 5f 31 28 63 6f 6e  c int m_eq_1(con
33900 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77  st char *z){.  w
33901 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
33902 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
33903 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
33904 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  0;.  while( isCo
33905 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
33906 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30  +; }.  if( *z==0
33907 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77   ) return 0;.  w
33908 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
33909 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
3390a 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
3390b 31 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  1;.  while( isCo
3390c 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
3390d 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
3390e 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20  ==0;.}../* Like 
3390f 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63 65 70  mgt0 above excep
33910 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67  t we are looking
33911 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f 66 20   for a value of 
33912 6d 3e 31 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f  m>1 instead.** o
33913 72 20 6d 3e 30 0a 2a 2f 0a 73 74 61 74 69 63 20  r m>0.*/.static 
33914 69 6e 74 20 6d 5f 67 74 5f 31 28 63 6f 6e 73 74  int m_gt_1(const
33915 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69   char *z){.  whi
33916 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20 29  le( isVowel(z) )
33917 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a  { z++; }.  if( *
33918 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  z==0 ) return 0;
33919 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73  .  while( isCons
3391a 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b  onant(z) ){ z++;
3391b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29   }.  if( *z==0 )
3391c 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69   return 0;.  whi
3391d 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20 29  le( isVowel(z) )
3391e 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a  { z++; }.  if( *
3391f 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  z==0 ) return 0;
33920 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73  .  while( isCons
33921 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b  onant(z) ){ z++;
33922 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d   }.  return *z!=
33923 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  0;.}../*.** Retu
33924 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 72 65  rn TRUE if there
33925 20 69 73 20 61 20 76 6f 77 65 6c 20 61 6e 79 77   is a vowel anyw
33926 68 65 72 65 20 77 69 74 68 69 6e 20 7a 5b 30 2e  here within z[0.
33927 2e 6e 2d 31 5d 0a 2a 2f 0a 73 74 61 74 69 63 20  .n-1].*/.static 
33928 69 6e 74 20 68 61 73 56 6f 77 65 6c 28 63 6f 6e  int hasVowel(con
33929 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77  st char *z){.  w
3392a 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e  hile( isConsonan
3392b 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20  t(z) ){ z++; }. 
3392c 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d   return *z!=0;.}
3392d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
3392e 52 55 45 20 69 66 20 74 68 65 20 77 6f 72 64 20  RUE if the word 
3392f 65 6e 64 73 20 69 6e 20 61 20 64 6f 75 62 6c 65  ends in a double
33930 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a   consonant..**.*
33931 2a 20 54 68 65 20 74 65 78 74 20 69 73 20 72 65  * The text is re
33932 76 65 72 73 65 64 20 68 65 72 65 2e 20 53 6f 20  versed here. So 
33933 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f  we are really lo
33934 6f 6b 69 6e 67 20 61 74 0a 2a 2a 20 74 68 65 20  oking at.** the 
33935 66 69 72 73 74 20 74 77 6f 20 63 68 61 72 61 63  first two charac
33936 74 65 72 73 20 6f 66 20 7a 5b 5d 2e 0a 2a 2f 0a  ters of z[]..*/.
33937 73 74 61 74 69 63 20 69 6e 74 20 64 6f 75 62 6c  static int doubl
33938 65 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73 74  eConsonant(const
33939 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74   char *z){.  ret
3393a 75 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  urn isConsonant(
3393b 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 7a 5b 31 5d  z) && z[0]==z[1]
3393c 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28   && isConsonant(
3393d 7a 2b 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  z+1);.}../*.** R
3393e 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
3393f 65 20 77 6f 72 64 20 65 6e 64 73 20 77 69 74 68  e word ends with
33940 20 74 68 72 65 65 20 6c 65 74 74 65 72 73 20 77   three letters w
33941 68 69 63 68 0a 2a 2a 20 61 72 65 20 63 6f 6e 73  hich.** are cons
33942 6f 6e 61 6e 74 2d 76 6f 77 65 6c 2d 63 6f 6e 73  onant-vowel-cons
33943 6f 6e 65 6e 74 20 61 6e 64 20 77 68 65 72 65 20  onent and where 
33944 74 68 65 20 66 69 6e 61 6c 20 63 6f 6e 73 6f 6e  the final conson
33945 61 6e 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 27 77  ant.** is not 'w
33946 27 2c 20 27 78 27 2c 20 6f 72 20 27 79 27 2e 0a  ', 'x', or 'y'..
33947 2a 2a 0a 2a 2a 20 54 68 65 20 77 6f 72 64 20 69  **.** The word i
33948 73 20 72 65 76 65 72 73 65 64 20 68 65 72 65 2e  s reversed here.
33949 20 20 53 6f 20 77 65 20 61 72 65 20 72 65 61 6c    So we are real
3394a 6c 79 20 63 68 65 63 6b 69 6e 67 20 74 68 65 0a  ly checking the.
3394b 2a 2a 20 66 69 72 73 74 20 74 68 72 65 65 20 6c  ** first three l
3394c 65 74 74 65 72 73 20 61 6e 64 20 74 68 65 20 66  etters and the f
3394d 69 72 73 74 20 6f 6e 65 20 63 61 6e 6e 6f 74 20  irst one cannot 
3394e 62 65 20 69 6e 20 5b 77 78 79 5d 2e 0a 2a 2f 0a  be in [wxy]..*/.
3394f 73 74 61 74 69 63 20 69 6e 74 20 73 74 61 72 5f  static int star_
33950 6f 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  oh(const char *z
33951 29 7b 0a 20 20 72 65 74 75 72 6e 0a 20 20 20 20  ){.  return.    
33952 7a 5b 30 5d 21 3d 30 20 26 26 20 69 73 43 6f 6e  z[0]!=0 && isCon
33953 73 6f 6e 61 6e 74 28 7a 29 20 26 26 0a 20 20 20  sonant(z) &&.   
33954 20 7a 5b 30 5d 21 3d 27 77 27 20 26 26 20 7a 5b   z[0]!='w' && z[
33955 30 5d 21 3d 27 78 27 20 26 26 20 7a 5b 30 5d 21  0]!='x' && z[0]!
33956 3d 27 79 27 20 26 26 0a 20 20 20 20 7a 5b 31 5d  ='y' &&.    z[1]
33957 21 3d 30 20 26 26 20 69 73 56 6f 77 65 6c 28 7a  !=0 && isVowel(z
33958 2b 31 29 20 26 26 0a 20 20 20 20 7a 5b 32 5d 21  +1) &&.    z[2]!
33959 3d 30 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e  =0 && isConsonan
3395a 74 28 7a 2b 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  t(z+2);.}../*.**
3395b 20 49 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64   If the word end
3395c 73 20 77 69 74 68 20 7a 46 72 6f 6d 20 61 6e 64  s with zFrom and
3395d 20 78 43 6f 6e 64 28 29 20 69 73 20 74 72 75 65   xCond() is true
3395e 20 66 6f 72 20 74 68 65 20 73 74 65 6d 0a 2a 2a   for the stem.**
3395f 20 6f 66 20 74 68 65 20 77 6f 72 64 20 74 68 61   of the word tha
33960 74 20 70 72 65 63 65 65 64 73 20 74 68 65 20 7a  t preceeds the z
33961 46 72 6f 6d 20 65 6e 64 69 6e 67 2c 20 74 68 65  From ending, the
33962 6e 20 63 68 61 6e 67 65 20 74 68 65 20 0a 2a 2a  n change the .**
33963 20 65 6e 64 69 6e 67 20 74 6f 20 7a 54 6f 2e 0a   ending to zTo..
33964 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20  **.** The input 
33965 77 6f 72 64 20 2a 70 7a 20 61 6e 64 20 7a 46 72  word *pz and zFr
33966 6f 6d 20 61 72 65 20 62 6f 74 68 20 69 6e 20 72  om are both in r
33967 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20 7a  everse order.  z
33968 54 6f 0a 2a 2a 20 69 73 20 69 6e 20 6e 6f 72 6d  To.** is in norm
33969 61 6c 20 6f 72 64 65 72 2e 20 0a 2a 2a 0a 2a 2a  al order. .**.**
3396a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
3396b 7a 46 72 6f 6d 20 6d 61 74 63 68 65 73 2e 20 20  zFrom matches.  
3396c 52 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20  Return FALSE if 
3396d 7a 46 72 6f 6d 20 64 6f 65 73 20 6e 6f 74 0a 2a  zFrom does not.*
3396e 2a 20 6d 61 74 63 68 2e 20 20 4e 6f 74 20 74 68  * match.  Not th
3396f 61 74 20 54 52 55 45 20 69 73 20 72 65 74 75 72  at TRUE is retur
33970 6e 65 64 20 65 76 65 6e 20 69 66 20 78 43 6f 6e  ned even if xCon
33971 64 28 29 20 66 61 69 6c 73 20 61 6e 64 0a 2a 2a  d() fails and.**
33972 20 6e 6f 20 73 75 62 73 74 69 74 75 74 69 6f 6e   no substitution
33973 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74   occurs..*/.stat
33974 69 63 20 69 6e 74 20 73 74 65 6d 28 0a 20 20 63  ic int stem(.  c
33975 68 61 72 20 2a 2a 70 7a 2c 20 20 20 20 20 20 20  har **pz,       
33976 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 6f 72        /* The wor
33977 64 20 62 65 69 6e 67 20 73 74 65 6d 6d 65 64 20  d being stemmed 
33978 28 52 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20  (Reversed) */.  
33979 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 72 6f  const char *zFro
3397a 6d 2c 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  m,     /* If the
3397b 20 65 6e 64 69 6e 67 20 6d 61 74 63 68 65 73 20   ending matches 
3397c 74 68 69 73 2e 2e 2e 20 28 52 65 76 65 72 73 65  this... (Reverse
3397d 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  d) */.  const ch
3397e 61 72 20 2a 7a 54 6f 2c 20 20 20 20 20 20 20 2f  ar *zTo,       /
3397f 2a 20 2e 2e 2e 20 63 68 61 6e 67 65 20 74 68 65  * ... change the
33980 20 65 6e 64 69 6e 67 20 74 6f 20 74 68 69 73 20   ending to this 
33981 28 6e 6f 74 20 72 65 76 65 72 73 65 64 29 20 2a  (not reversed) *
33982 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 64 29  /.  int (*xCond)
33983 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20 20 20  (const char*)   
33984 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 74 68 61  /* Condition tha
33985 74 20 6d 75 73 74 20 62 65 20 74 72 75 65 20 2a  t must be true *
33986 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 20 3d  /.){.  char *z =
33987 20 2a 70 7a 3b 0a 20 20 77 68 69 6c 65 28 20 2a   *pz;.  while( *
33988 7a 46 72 6f 6d 20 26 26 20 2a 7a 46 72 6f 6d 3d  zFrom && *zFrom=
33989 3d 2a 7a 20 29 7b 20 7a 2b 2b 3b 20 7a 46 72 6f  =*z ){ z++; zFro
3398a 6d 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 46  m++; }.  if( *zF
3398b 72 6f 6d 21 3d 30 20 29 20 72 65 74 75 72 6e 20  rom!=0 ) return 
3398c 30 3b 0a 20 20 69 66 28 20 78 43 6f 6e 64 20 26  0;.  if( xCond &
3398d 26 20 21 78 43 6f 6e 64 28 7a 29 20 29 20 72 65  & !xCond(z) ) re
3398e 74 75 72 6e 20 31 3b 0a 20 20 77 68 69 6c 65 28  turn 1;.  while(
3398f 20 2a 7a 54 6f 20 29 7b 0a 20 20 20 20 2a 28 2d   *zTo ){.    *(-
33990 2d 7a 29 20 3d 20 2a 28 7a 54 6f 2b 2b 29 3b 0a  -z) = *(zTo++);.
33991 20 20 7d 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 20    }.  *pz = z;. 
33992 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
33993 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
33994 66 61 6c 6c 62 61 63 6b 20 73 74 65 6d 6d 65 72  fallback stemmer
33995 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 70   used when the p
33996 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 20 69 73  orter stemmer is
33997 0a 2a 2a 20 69 6e 61 70 70 72 6f 70 72 69 61 74  .** inappropriat
33998 65 2e 20 20 54 68 65 20 69 6e 70 75 74 20 77 6f  e.  The input wo
33999 72 64 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74  rd is copied int
3399a 6f 20 74 68 65 20 6f 75 74 70 75 74 20 77 69 74  o the output wit
3399b 68 0a 2a 2a 20 55 53 2d 41 53 43 49 49 20 63 61  h.** US-ASCII ca
3399c 73 65 20 66 6f 6c 64 69 6e 67 2e 20 20 49 66 20  se folding.  If 
3399d 74 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 69  the input word i
3399e 73 20 74 6f 6f 20 6c 6f 6e 67 20 28 6d 6f 72 65  s too long (more
3399f 0a 2a 2a 20 74 68 61 6e 20 32 30 20 62 79 74 65  .** than 20 byte
339a0 73 20 69 66 20 69 74 20 63 6f 6e 74 61 69 6e 73  s if it contains
339a1 20 6e 6f 20 64 69 67 69 74 73 20 6f 72 20 6d 6f   no digits or mo
339a2 72 65 20 74 68 61 6e 20 36 20 62 79 74 65 73 20  re than 6 bytes 
339a3 69 66 0a 2a 2a 20 69 74 20 63 6f 6e 74 61 69 6e  if.** it contain
339a4 73 20 64 69 67 69 74 73 29 20 74 68 65 6e 20 77  s digits) then w
339a5 6f 72 64 20 69 73 20 74 72 75 6e 63 61 74 65 64  ord is truncated
339a6 20 74 6f 20 32 30 20 6f 72 20 36 20 62 79 74 65   to 20 or 6 byte
339a7 73 0a 2a 2a 20 62 79 20 74 61 6b 69 6e 67 20 31  s.** by taking 1
339a8 30 20 6f 72 20 33 20 62 79 74 65 73 20 66 72 6f  0 or 3 bytes fro
339a9 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
339aa 61 6e 64 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74  and end..*/.stat
339ab 69 63 20 76 6f 69 64 20 63 6f 70 79 5f 73 74 65  ic void copy_ste
339ac 6d 6d 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20  mmer(const char 
339ad 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c 20 63  *zIn, int nIn, c
339ae 68 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74 20 2a  har *zOut, int *
339af 70 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20 69 2c  pnOut){.  int i,
339b0 20 6d 78 2c 20 6a 3b 0a 20 20 69 6e 74 20 68 61   mx, j;.  int ha
339b1 73 44 69 67 69 74 20 3d 20 30 3b 0a 20 20 66 6f  sDigit = 0;.  fo
339b2 72 28 69 3d 30 3b 20 69 3c 6e 49 6e 3b 20 69 2b  r(i=0; i<nIn; i+
339b3 2b 29 7b 0a 20 20 20 20 69 6e 74 20 63 20 3d 20  +){.    int c = 
339b4 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20  zIn[i];.    if( 
339b5 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27 5a 27  c>='A' && c<='Z'
339b6 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69   ){.      zOut[i
339b7 5d 20 3d 20 63 20 2d 20 27 41 27 20 2b 20 27 61  ] = c - 'A' + 'a
339b8 27 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ';.    }else{.  
339b9 20 20 20 20 69 66 28 20 63 3e 3d 27 30 27 20 26      if( c>='0' &
339ba 26 20 63 3c 3d 27 39 27 20 29 20 68 61 73 44 69  & c<='9' ) hasDi
339bb 67 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a  git = 1;.      z
339bc 4f 75 74 5b 69 5d 20 3d 20 63 3b 0a 20 20 20 20  Out[i] = c;.    
339bd 7d 0a 20 20 7d 0a 20 20 6d 78 20 3d 20 68 61 73  }.  }.  mx = has
339be 44 69 67 69 74 20 3f 20 33 20 3a 20 31 30 3b 0a  Digit ? 3 : 10;.
339bf 20 20 69 66 28 20 6e 49 6e 3e 6d 78 2a 32 20 29    if( nIn>mx*2 )
339c0 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 6d 78 2c 20  {.    for(j=mx, 
339c1 69 3d 6e 49 6e 2d 6d 78 3b 20 69 3c 6e 49 6e 3b  i=nIn-mx; i<nIn;
339c2 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20   i++, j++){.    
339c3 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 7a 4f 75 74    zOut[j] = zOut
339c4 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  [i];.    }.    i
339c5 20 3d 20 6a 3b 0a 20 20 7d 0a 20 20 7a 4f 75 74   = j;.  }.  zOut
339c6 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 6e 4f 75  [i] = 0;.  *pnOu
339c7 74 20 3d 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  t = i;.}.../*.**
339c8 20 53 74 65 6d 20 74 68 65 20 69 6e 70 75 74 20   Stem the input 
339c9 77 6f 72 64 20 7a 49 6e 5b 30 2e 2e 6e 49 6e 2d  word zIn[0..nIn-
339ca 31 5d 2e 20 20 53 74 6f 72 65 20 74 68 65 20 6f  1].  Store the o
339cb 75 74 70 75 74 20 69 6e 20 7a 4f 75 74 2e 0a 2a  utput in zOut..*
339cc 2a 20 7a 4f 75 74 20 69 73 20 61 74 20 6c 65 61  * zOut is at lea
339cd 73 74 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f  st big enough to
339ce 20 68 6f 6c 64 20 6e 49 6e 20 62 79 74 65 73 2e   hold nIn bytes.
339cf 20 20 57 72 69 74 65 20 74 68 65 20 61 63 74 75    Write the actu
339d0 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68  al.** size of th
339d1 65 20 6f 75 74 70 75 74 20 77 6f 72 64 20 28 65  e output word (e
339d2 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 20  xclusive of the 
339d3 27 5c 30 27 20 74 65 72 6d 69 6e 61 74 6f 72 29  '\0' terminator)
339d4 20 69 6e 74 6f 20 2a 70 6e 4f 75 74 2e 0a 2a 2a   into *pnOut..**
339d5 0a 2a 2a 20 41 6e 79 20 75 70 70 65 72 2d 63 61  .** Any upper-ca
339d6 73 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  se characters in
339d7 20 74 68 65 20 55 53 2d 41 53 43 49 49 20 63 68   the US-ASCII ch
339d8 61 72 61 63 74 65 72 20 73 65 74 20 28 5b 41 2d  aracter set ([A-
339d9 5a 5d 29 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65  Z]).** are conve
339da 72 74 65 64 20 74 6f 20 6c 6f 77 65 72 20 63 61  rted to lower ca
339db 73 65 2e 20 20 55 70 70 65 72 2d 63 61 73 65 20  se.  Upper-case 
339dc 55 54 46 20 63 68 61 72 61 63 74 65 72 73 20 61  UTF characters a
339dd 72 65 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e  re.** unchanged.
339de 0a 2a 2a 0a 2a 2a 20 57 6f 72 64 73 20 74 68 61  .**.** Words tha
339df 74 20 61 72 65 20 6c 6f 6e 67 65 72 20 74 68 61  t are longer tha
339e0 6e 20 61 62 6f 75 74 20 32 30 20 62 79 74 65 73  n about 20 bytes
339e1 20 61 72 65 20 73 74 65 6d 6d 65 64 20 62 79 20   are stemmed by 
339e2 72 65 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 20 66  retaining.** a f
339e3 65 77 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68  ew bytes from th
339e4 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20  e beginning and 
339e5 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 77  the end of the w
339e6 6f 72 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  ord.  If the.** 
339e7 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 64 69  word contains di
339e8 67 69 74 73 2c 20 33 20 62 79 74 65 73 20 61 72  gits, 3 bytes ar
339e9 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65  e taken from the
339ea 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 0a 2a   beginning and.*
339eb 2a 20 33 20 62 79 74 65 73 20 66 72 6f 6d 20 74  * 3 bytes from t
339ec 68 65 20 65 6e 64 2e 20 20 46 6f 72 20 6c 6f 6e  he end.  For lon
339ed 67 20 77 6f 72 64 73 20 77 69 74 68 6f 75 74 20  g words without 
339ee 64 69 67 69 74 73 2c 20 31 30 20 62 79 74 65 73  digits, 10 bytes
339ef 0a 2a 2a 20 61 72 65 20 74 61 6b 65 6e 20 66 72  .** are taken fr
339f0 6f 6d 20 65 61 63 68 20 65 6e 64 2e 20 20 55 53  om each end.  US
339f1 2d 41 53 43 49 49 20 63 61 73 65 20 66 6f 6c 64  -ASCII case fold
339f2 69 6e 67 20 73 74 69 6c 6c 20 61 70 70 6c 69 65  ing still applie
339f3 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65  s..** .** If the
339f4 20 69 6e 70 75 74 20 77 6f 72 64 20 63 6f 6e 74   input word cont
339f5 61 69 6e 73 20 6e 6f 74 20 64 69 67 69 74 73 20  ains not digits 
339f6 62 75 74 20 64 6f 65 73 20 63 68 61 72 61 63 74  but does charact
339f7 65 72 73 20 6e 6f 74 20 0a 2a 2a 20 69 6e 20 5b  ers not .** in [
339f8 61 2d 7a 41 2d 5a 5d 20 74 68 65 6e 20 6e 6f 20  a-zA-Z] then no 
339f9 73 74 65 6d 6d 69 6e 67 20 69 73 20 61 74 74 65  stemming is atte
339fa 6d 70 74 65 64 20 61 6e 64 20 74 68 69 73 20 72  mpted and this r
339fb 6f 75 74 69 6e 65 20 6a 75 73 74 20 0a 2a 2a 20  outine just .** 
339fc 63 6f 70 69 65 73 20 74 68 65 20 69 6e 70 75 74  copies the input
339fd 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 20   into the input 
339fe 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20  into the output 
339ff 77 69 74 68 20 55 53 2d 41 53 43 49 49 0a 2a 2a  with US-ASCII.**
33a00 20 63 61 73 65 20 66 6f 6c 64 69 6e 67 2e 0a 2a   case folding..*
33a01 2a 0a 2a 2a 20 53 74 65 6d 6d 69 6e 67 20 6e 65  *.** Stemming ne
33a02 76 65 72 20 69 6e 63 72 65 61 73 65 73 20 74 68  ver increases th
33a03 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
33a04 77 6f 72 64 2e 20 20 53 6f 20 74 68 65 72 65 20  word.  So there 
33a05 69 73 0a 2a 2a 20 6e 6f 20 63 68 61 6e 63 65 20  is.** no chance 
33a06 6f 66 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 74  of overflowing t
33a07 68 65 20 7a 4f 75 74 20 62 75 66 66 65 72 2e 0a  he zOut buffer..
33a08 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
33a09 6f 72 74 65 72 5f 73 74 65 6d 6d 65 72 28 63 6f  orter_stemmer(co
33a0a 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69  nst char *zIn, i
33a0b 6e 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f  nt nIn, char *zO
33a0c 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b  ut, int *pnOut){
33a0d 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 63 3b 0a  .  int i, j, c;.
33a0e 20 20 63 68 61 72 20 7a 52 65 76 65 72 73 65 5b    char zReverse[
33a0f 32 38 5d 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20  28];.  char *z, 
33a10 2a 7a 32 3b 0a 20 20 69 66 28 20 6e 49 6e 3c 33  *z2;.  if( nIn<3
33a11 20 7c 7c 20 6e 49 6e 3e 3d 73 69 7a 65 6f 66 28   || nIn>=sizeof(
33a12 7a 52 65 76 65 72 73 65 29 2d 37 20 29 7b 0a 20  zReverse)-7 ){. 
33a13 20 20 20 2f 2a 20 54 68 65 20 77 6f 72 64 20 69     /* The word i
33a14 73 20 74 6f 6f 20 62 69 67 20 6f 72 20 74 6f 6f  s too big or too
33a15 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65 20 70   small for the p
33a16 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 2e 0a 20  orter stemmer.. 
33a17 20 20 20 2a 2a 20 46 61 6c 6c 62 61 63 6b 20 74     ** Fallback t
33a18 6f 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d 6d  o the copy stemm
33a19 65 72 20 2a 2f 0a 20 20 20 20 63 6f 70 79 5f 73  er */.    copy_s
33a1a 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c  temmer(zIn, nIn,
33a1b 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a 20   zOut, pnOut);. 
33a1c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
33a1d 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 73 69 7a 65   for(i=0, j=size
33a1e 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 36 3b 20  of(zReverse)-6; 
33a1f 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2d 2d 29  i<nIn; i++, j--)
33a20 7b 0a 20 20 20 20 63 20 3d 20 7a 49 6e 5b 69 5d  {.    c = zIn[i]
33a21 3b 0a 20 20 20 20 69 66 28 20 63 3e 3d 27 41 27  ;.    if( c>='A'
33a22 20 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a 20 20   && c<='Z' ){.  
33a23 20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a 5d 20      zReverse[j] 
33a24 3d 20 63 20 2b 20 27 61 27 20 2d 20 27 41 27 3b  = c + 'a' - 'A';
33a25 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63  .    }else if( c
33a26 3e 3d 27 61 27 20 26 26 20 63 3c 3d 27 7a 27 20  >='a' && c<='z' 
33a27 29 7b 0a 20 20 20 20 20 20 7a 52 65 76 65 72 73  ){.      zRevers
33a28 65 5b 6a 5d 20 3d 20 63 3b 0a 20 20 20 20 7d 65  e[j] = c;.    }e
33a29 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  lse{.      /* Th
33a2a 65 20 75 73 65 20 6f 66 20 61 20 63 68 61 72 61  e use of a chara
33a2b 63 74 65 72 20 6e 6f 74 20 69 6e 20 5b 61 2d 7a  cter not in [a-z
33a2c 41 2d 5a 5d 20 6d 65 61 6e 73 20 74 68 61 74 20  A-Z] means that 
33a2d 77 65 20 66 61 6c 6c 62 61 63 6b 0a 20 20 20 20  we fallback.    
33a2e 20 20 2a 2a 20 74 6f 20 74 68 65 20 63 6f 70 79    ** to the copy
33a2f 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20 20 20   stemmer */.    
33a30 20 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 7a    copy_stemmer(z
33a31 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20 70  In, nIn, zOut, p
33a32 6e 4f 75 74 29 3b 0a 20 20 20 20 20 20 72 65 74  nOut);.      ret
33a33 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
33a34 20 6d 65 6d 73 65 74 28 26 7a 52 65 76 65 72 73   memset(&zRevers
33a35 65 5b 73 69 7a 65 6f 66 28 7a 52 65 76 65 72 73  e[sizeof(zRevers
33a36 65 29 2d 35 5d 2c 20 30 2c 20 35 29 3b 0a 20 20  e)-5], 0, 5);.  
33a37 7a 20 3d 20 26 7a 52 65 76 65 72 73 65 5b 6a 2b  z = &zReverse[j+
33a38 31 5d 3b 0a 0a 0a 20 20 2f 2a 20 53 74 65 70 20  1];...  /* Step 
33a39 31 61 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d  1a */.  if( z[0]
33a3a 3d 3d 27 73 27 20 29 7b 0a 20 20 20 20 69 66 28  =='s' ){.    if(
33a3b 0a 20 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20  .     !stem(&z, 
33a3c 22 73 65 73 73 22 2c 20 22 73 73 22 2c 20 30 29  "sess", "ss", 0)
33a3d 20 26 26 0a 20 20 20 20 20 21 73 74 65 6d 28 26   &&.     !stem(&
33a3e 7a 2c 20 22 73 65 69 22 2c 20 22 69 22 2c 20 30  z, "sei", "i", 0
33a3f 29 20 20 26 26 0a 20 20 20 20 20 21 73 74 65 6d  )  &&.     !stem
33a40 28 26 7a 2c 20 22 73 73 22 2c 20 22 73 73 22 2c  (&z, "ss", "ss",
33a41 20 30 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20   0).    ){.     
33a42 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   z++;.    }.  }.
33a43 0a 20 20 2f 2a 20 53 74 65 70 20 31 62 20 2a 2f  .  /* Step 1b */
33a44 20 20 0a 20 20 7a 32 20 3d 20 7a 3b 0a 20 20 69    .  z2 = z;.  i
33a45 66 28 20 73 74 65 6d 28 26 7a 2c 20 22 64 65 65  f( stem(&z, "dee
33a46 22 2c 20 22 65 65 22 2c 20 6d 5f 67 74 5f 30 29  ", "ee", m_gt_0)
33a47 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f   ){.    /* Do no
33a48 74 68 69 6e 67 2e 20 20 54 68 65 20 77 6f 72 6b  thing.  The work
33a49 20 77 61 73 20 61 6c 6c 20 69 6e 20 74 68 65 20   was all in the 
33a4a 74 65 73 74 20 2a 2f 0a 20 20 7d 65 6c 73 65 20  test */.  }else 
33a4b 69 66 28 20 0a 20 20 20 20 20 28 73 74 65 6d 28  if( .     (stem(
33a4c 26 7a 2c 20 22 67 6e 69 22 2c 20 22 22 2c 20 68  &z, "gni", "", h
33a4d 61 73 56 6f 77 65 6c 29 20 7c 7c 20 73 74 65 6d  asVowel) || stem
33a4e 28 26 7a 2c 20 22 64 65 22 2c 20 22 22 2c 20 68  (&z, "de", "", h
33a4f 61 73 56 6f 77 65 6c 29 29 0a 20 20 20 20 20 20  asVowel)).      
33a50 26 26 20 7a 21 3d 7a 32 0a 20 20 29 7b 0a 20 20  && z!=z2.  ){.  
33a51 20 20 20 69 66 28 20 73 74 65 6d 28 26 7a 2c 20     if( stem(&z, 
33a52 22 74 61 22 2c 20 22 61 74 65 22 2c 20 30 29 20  "ta", "ate", 0) 
33a53 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
33a54 28 26 7a 2c 20 22 6c 62 22 2c 20 22 62 6c 65 22  (&z, "lb", "ble"
33a55 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20 20 20  , 0) ||.        
33a56 20 73 74 65 6d 28 26 7a 2c 20 22 7a 69 22 2c 20   stem(&z, "zi", 
33a57 22 69 7a 65 22 2c 20 30 29 20 29 7b 0a 20 20 20  "ize", 0) ){.   
33a58 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e      /* Do nothin
33a59 67 2e 20 20 54 68 65 20 77 6f 72 6b 20 77 61 73  g.  The work was
33a5a 20 61 6c 6c 20 69 6e 20 74 68 65 20 74 65 73 74   all in the test
33a5b 20 2a 2f 0a 20 20 20 20 20 7d 65 6c 73 65 20 69   */.     }else i
33a5c 66 28 20 64 6f 75 62 6c 65 43 6f 6e 73 6f 6e 61  f( doubleConsona
33a5d 6e 74 28 7a 29 20 26 26 20 28 2a 7a 21 3d 27 6c  nt(z) && (*z!='l
33a5e 27 20 26 26 20 2a 7a 21 3d 27 73 27 20 26 26 20  ' && *z!='s' && 
33a5f 2a 7a 21 3d 27 7a 27 29 20 29 7b 0a 20 20 20 20  *z!='z') ){.    
33a60 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 20 7d 65 6c     z++;.     }el
33a61 73 65 20 69 66 28 20 6d 5f 65 71 5f 31 28 7a 29  se if( m_eq_1(z)
33a62 20 26 26 20 73 74 61 72 5f 6f 68 28 7a 29 20 29   && star_oh(z) )
33a63 7b 0a 20 20 20 20 20 20 20 2a 28 2d 2d 7a 29 20  {.       *(--z) 
33a64 3d 20 27 65 27 3b 0a 20 20 20 20 20 7d 0a 20 20  = 'e';.     }.  
33a65 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 31 63 20  }..  /* Step 1c 
33a66 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27  */.  if( z[0]=='
33a67 79 27 20 26 26 20 68 61 73 56 6f 77 65 6c 28 7a  y' && hasVowel(z
33a68 2b 31 29 20 29 7b 0a 20 20 20 20 7a 5b 30 5d 20  +1) ){.    z[0] 
33a69 3d 20 27 69 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  = 'i';.  }..  /*
33a6a 20 53 74 65 70 20 32 20 2a 2f 0a 20 20 73 77 69   Step 2 */.  swi
33a6b 74 63 68 28 20 7a 5b 31 5d 20 29 7b 0a 20 20 20  tch( z[1] ){.   
33a6c 63 61 73 65 20 27 61 27 3a 0a 20 20 20 20 20 73  case 'a':.     s
33a6d 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69 74 61  tem(&z, "lanoita
33a6e 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30  ", "ate", m_gt_0
33a6f 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
33a70 7a 2c 20 22 6c 61 6e 6f 69 74 22 2c 20 22 74 69  z, "lanoit", "ti
33a71 6f 6e 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20  on", m_gt_0);.  
33a72 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
33a73 65 20 27 63 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 'c':.     stem
33a74 28 26 7a 2c 20 22 69 63 6e 65 22 2c 20 22 65 6e  (&z, "icne", "en
33a75 63 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ce", m_gt_0) ||.
33a76 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
33a77 63 6e 61 22 2c 20 22 61 6e 63 65 22 2c 20 6d 5f  cna", "ance", m_
33a78 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
33a79 6b 3b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a  k;.   case 'e':.
33a7a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 72       stem(&z, "r
33a7b 65 7a 69 22 2c 20 22 69 7a 65 22 2c 20 6d 5f 67  ezi", "ize", m_g
33a7c 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
33a7d 3b 0a 20 20 20 63 61 73 65 20 27 67 27 3a 0a 20  ;.   case 'g':. 
33a7e 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 67      stem(&z, "ig
33a7f 6f 6c 22 2c 20 22 6c 6f 67 22 2c 20 6d 5f 67 74  ol", "log", m_gt
33a80 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
33a81 0a 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20  .   case 'l':.  
33a82 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 62     stem(&z, "ilb
33a83 22 2c 20 22 62 6c 65 22 2c 20 6d 5f 67 74 5f 30  ", "ble", m_gt_0
33a84 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
33a85 7a 2c 20 22 69 6c 6c 61 22 2c 20 22 61 6c 22 2c  z, "illa", "al",
33a86 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
33a87 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 74 6e 65   stem(&z, "iltne
33a88 22 2c 20 22 65 6e 74 22 2c 20 6d 5f 67 74 5f 30  ", "ent", m_gt_0
33a89 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
33a8a 7a 2c 20 22 69 6c 65 22 2c 20 22 65 22 2c 20 6d  z, "ile", "e", m
33a8b 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73  _gt_0) ||.     s
33a8c 74 65 6d 28 26 7a 2c 20 22 69 6c 73 75 6f 22 2c  tem(&z, "ilsuo",
33a8d 20 22 6f 75 73 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ous", m_gt_0);
33a8e 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
33a8f 63 61 73 65 20 27 6f 27 3a 0a 20 20 20 20 20 73  case 'o':.     s
33a90 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61 7a 69  tem(&z, "noitazi
33a91 22 2c 20 22 69 7a 65 22 2c 20 6d 5f 67 74 5f 30  ", "ize", m_gt_0
33a92 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
33a93 7a 2c 20 22 6e 6f 69 74 61 22 2c 20 22 61 74 65  z, "noita", "ate
33a94 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
33a95 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 72 6f 74     stem(&z, "rot
33a96 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f  a", "ate", m_gt_
33a97 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
33a98 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20     case 's':.   
33a99 20 20 73 74 65 6d 28 26 7a 2c 20 22 6d 73 69 6c    stem(&z, "msil
33a9a 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30  a", "al", m_gt_0
33a9b 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
33a9c 7a 2c 20 22 73 73 65 6e 65 76 69 22 2c 20 22 69  z, "ssenevi", "i
33a9d 76 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ve", m_gt_0) ||.
33a9e 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
33a9f 73 65 6e 6c 75 66 22 2c 20 22 66 75 6c 22 2c 20  senluf", "ful", 
33aa0 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
33aa1 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 73 75  stem(&z, "ssensu
33aa2 6f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f 67 74 5f  o", "ous", m_gt_
33aa3 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
33aa4 20 20 20 63 61 73 65 20 27 74 27 3a 0a 20 20 20     case 't':.   
33aa5 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 6c    stem(&z, "itil
33aa6 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30  a", "al", m_gt_0
33aa7 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
33aa8 7a 2c 20 22 69 74 69 76 69 22 2c 20 22 69 76 65  z, "itivi", "ive
33aa9 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
33aaa 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69     stem(&z, "iti
33aab 6c 69 62 22 2c 20 22 62 6c 65 22 2c 20 6d 5f 67  lib", "ble", m_g
33aac 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
33aad 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  ;.  }..  /* Step
33aae 20 33 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20   3 */.  switch( 
33aaf 7a 5b 30 5d 20 29 7b 0a 20 20 20 63 61 73 65 20  z[0] ){.   case 
33ab0 27 65 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'e':.     stem(&
33ab1 7a 2c 20 22 65 74 61 63 69 22 2c 20 22 69 63 22  z, "etaci", "ic"
33ab2 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
33ab3 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 76 69 74    stem(&z, "evit
33ab4 61 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29 20  a", "", m_gt_0) 
33ab5 20 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26    ||.     stem(&
33ab6 7a 2c 20 22 65 7a 69 6c 61 22 2c 20 22 61 6c 22  z, "ezila", "al"
33ab7 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20  , m_gt_0);.     
33ab8 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
33ab9 69 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  i':.     stem(&z
33aba 2c 20 22 69 74 69 63 69 22 2c 20 22 69 63 22 2c  , "itici", "ic",
33abb 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
33abc 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6c  reak;.   case 'l
33abd 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
33abe 20 22 6c 61 63 69 22 2c 20 22 69 63 22 2c 20 6d   "laci", "ic", m
33abf 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73  _gt_0) ||.     s
33ac0 74 65 6d 28 26 7a 2c 20 22 6c 75 66 22 2c 20 22  tem(&z, "luf", "
33ac1 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
33ac2 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
33ac3 27 73 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  's':.     stem(&
33ac4 7a 2c 20 22 73 73 65 6e 22 2c 20 22 22 2c 20 6d  z, "ssen", "", m
33ac5 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65  _gt_0);.     bre
33ac6 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  ak;.  }..  /* St
33ac7 65 70 20 34 20 2a 2f 0a 20 20 73 77 69 74 63 68  ep 4 */.  switch
33ac8 28 20 7a 5b 31 5d 20 29 7b 0a 20 20 20 63 61 73  ( z[1] ){.   cas
33ac9 65 20 27 61 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'a':.     if( 
33aca 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 6d 5f 67  z[0]=='l' && m_g
33acb 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20  t_1(z+2) ){.    
33acc 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20     z += 2;.     
33acd 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  }.     break;.  
33ace 20 63 61 73 65 20 27 63 27 3a 0a 20 20 20 20 20   case 'c':.     
33acf 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26  if( z[0]=='e' &&
33ad0 20 7a 5b 32 5d 3d 3d 27 6e 27 20 26 26 20 28 7a   z[2]=='n' && (z
33ad1 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d  [3]=='a' || z[3]
33ad2 3d 3d 27 65 27 29 20 20 26 26 20 6d 5f 67 74 5f  =='e')  && m_gt_
33ad3 31 28 7a 2b 34 29 20 20 29 7b 0a 20 20 20 20 20  1(z+4)  ){.     
33ad4 20 20 7a 20 2b 3d 20 34 3b 0a 20 20 20 20 20 7d    z += 4;.     }
33ad5 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
33ad6 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20 69  case 'e':.     i
33ad7 66 28 20 7a 5b 30 5d 3d 3d 27 72 27 20 26 26 20  f( z[0]=='r' && 
33ad8 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20  m_gt_1(z+2) ){. 
33ad9 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20        z += 2;.  
33ada 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b     }.     break;
33adb 0a 20 20 20 63 61 73 65 20 27 69 27 3a 0a 20 20  .   case 'i':.  
33adc 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 63 27     if( z[0]=='c'
33add 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20   && m_gt_1(z+2) 
33ade 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32  ){.       z += 2
33adf 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  ;.     }.     br
33ae0 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6c 27  eak;.   case 'l'
33ae1 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
33ae2 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 62  ='e' && z[2]=='b
33ae3 27 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61 27 20  ' && (z[3]=='a' 
33ae4 7c 7c 20 7a 5b 33 5d 3d 3d 27 69 27 29 20 26 26  || z[3]=='i') &&
33ae5 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 29 7b 0a   m_gt_1(z+4) ){.
33ae6 20 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b 0a 20         z += 4;. 
33ae7 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
33ae8 3b 0a 20 20 20 63 61 73 65 20 27 6e 27 3a 0a 20  ;.   case 'n':. 
33ae9 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 74      if( z[0]=='t
33aea 27 20 29 7b 0a 20 20 20 20 20 20 20 69 66 28 20  ' ){.       if( 
33aeb 7a 5b 32 5d 3d 3d 27 61 27 20 29 7b 0a 20 20 20  z[2]=='a' ){.   
33aec 20 20 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31        if( m_gt_1
33aed 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+3) ){.       
33aee 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20      z += 3;.    
33aef 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 7d 65       }.       }e
33af0 6c 73 65 20 69 66 28 20 7a 5b 32 5d 3d 3d 27 65  lse if( z[2]=='e
33af1 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 73 74  ' ){.         st
33af2 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 65 22 2c 20  em(&z, "tneme", 
33af3 22 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20  "", m_gt_1) ||. 
33af4 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c          stem(&z,
33af5 20 22 74 6e 65 6d 22 2c 20 22 22 2c 20 6d 5f 67   "tnem", "", m_g
33af6 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20 20 20  t_1) ||.        
33af7 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65 22 2c   stem(&z, "tne",
33af8 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20   "", m_gt_1);.  
33af9 20 20 20 20 20 7d 0a 20 20 20 20 20 7d 0a 20 20       }.     }.  
33afa 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
33afb 65 20 27 6f 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'o':.     if( 
33afc 7a 5b 30 5d 3d 3d 27 75 27 20 29 7b 0a 20 20 20  z[0]=='u' ){.   
33afd 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a      if( m_gt_1(z
33afe 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  +2) ){.         
33aff 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 7d  z += 2;.       }
33b00 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  .     }else if( 
33b01 7a 5b 33 5d 3d 3d 27 73 27 20 7c 7c 20 7a 5b 33  z[3]=='s' || z[3
33b02 5d 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20 20 20  ]=='t' ){.      
33b03 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 22 2c   stem(&z, "noi",
33b04 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20   "", m_gt_1);.  
33b05 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b     }.     break;
33b06 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20  .   case 's':.  
33b07 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 6d 27     if( z[0]=='m'
33b08 20 26 26 20 7a 5b 32 5d 3d 3d 27 69 27 20 26 26   && z[2]=='i' &&
33b09 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a   m_gt_1(z+3) ){.
33b0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20         z += 3;. 
33b0b 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
33b0c 3b 0a 20 20 20 63 61 73 65 20 27 74 27 3a 0a 20  ;.   case 't':. 
33b0d 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 74      stem(&z, "et
33b0e 61 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20  a", "", m_gt_1) 
33b0f 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ||.     stem(&z,
33b10 20 22 69 74 69 22 2c 20 22 22 2c 20 6d 5f 67 74   "iti", "", m_gt
33b11 5f 31 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _1);.     break;
33b12 0a 20 20 20 63 61 73 65 20 27 75 27 3a 0a 20 20  .   case 'u':.  
33b13 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 73 27     if( z[0]=='s'
33b14 20 26 26 20 7a 5b 32 5d 3d 3d 27 6f 27 20 26 26   && z[2]=='o' &&
33b15 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a   m_gt_1(z+3) ){.
33b16 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20         z += 3;. 
33b17 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
33b18 3b 0a 20 20 20 63 61 73 65 20 27 76 27 3a 0a 20  ;.   case 'v':. 
33b19 20 20 63 61 73 65 20 27 7a 27 3a 0a 20 20 20 20    case 'z':.    
33b1a 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26   if( z[0]=='e' &
33b1b 26 20 7a 5b 32 5d 3d 3d 27 69 27 20 26 26 20 6d  & z[2]=='i' && m
33b1c 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20  _gt_1(z+3) ){.  
33b1d 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20       z += 3;.   
33b1e 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
33b1f 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35    }..  /* Step 5
33b20 61 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d 3d  a */.  if( z[0]=
33b21 3d 27 65 27 20 29 7b 0a 20 20 20 20 69 66 28 20  ='e' ){.    if( 
33b22 6d 5f 67 74 5f 31 28 7a 2b 31 29 20 29 7b 0a 20  m_gt_1(z+1) ){. 
33b23 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65       z++;.    }e
33b24 6c 73 65 20 69 66 28 20 6d 5f 65 71 5f 31 28 7a  lse if( m_eq_1(z
33b25 2b 31 29 20 26 26 20 21 73 74 61 72 5f 6f 68 28  +1) && !star_oh(
33b26 7a 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 7a 2b  z+1) ){.      z+
33b27 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  +;.    }.  }..  
33b28 2f 2a 20 53 74 65 70 20 35 62 20 2a 2f 0a 20 20  /* Step 5b */.  
33b29 69 66 28 20 6d 5f 67 74 5f 31 28 7a 29 20 26 26  if( m_gt_1(z) &&
33b2a 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 7a 5b   z[0]=='l' && z[
33b2b 31 5d 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 7a  1]=='l' ){.    z
33b2c 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 7a 5b  ++;.  }..  /* z[
33b2d 5d 20 69 73 20 6e 6f 77 20 74 68 65 20 73 74 65  ] is now the ste
33b2e 6d 6d 65 64 20 77 6f 72 64 20 69 6e 20 72 65 76  mmed word in rev
33b2f 65 72 73 65 20 6f 72 64 65 72 2e 20 20 46 6c 69  erse order.  Fli
33b30 70 20 69 74 20 62 61 63 6b 0a 20 20 2a 2a 20 61  p it back.  ** a
33b31 72 6f 75 6e 64 20 69 6e 74 6f 20 66 6f 72 77 61  round into forwa
33b32 72 64 20 6f 72 64 65 72 20 61 6e 64 20 72 65 74  rd order and ret
33b33 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 2a 70 6e 4f  urn..  */.  *pnO
33b34 75 74 20 3d 20 69 20 3d 20 73 74 72 6c 65 6e 28  ut = i = strlen(
33b35 7a 29 3b 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20  z);.  zOut[i] = 
33b36 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 20 29  0;.  while( *z )
33b37 7b 0a 20 20 20 20 7a 4f 75 74 5b 2d 2d 69 5d 20  {.    zOut[--i] 
33b38 3d 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a 7d 0a  = *(z++);.  }.}.
33b39 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 72  ./*.** Character
33b3a 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 61  s that can be pa
33b3b 72 74 20 6f 66 20 61 20 74 6f 6b 65 6e 2e 20 20  rt of a token.  
33b3c 57 65 20 61 73 73 75 6d 65 20 61 6e 79 20 63 68  We assume any ch
33b3d 61 72 61 63 74 65 72 0a 2a 2a 20 77 68 6f 73 65  aracter.** whose
33b3e 20 76 61 6c 75 65 20 69 73 20 67 72 65 61 74 65   value is greate
33b3f 72 20 74 68 61 6e 20 30 78 38 30 20 28 61 6e 79  r than 0x80 (any
33b40 20 55 54 46 20 63 68 61 72 61 63 74 65 72 29 20   UTF character) 
33b41 63 61 6e 20 62 65 0a 2a 2a 20 70 61 72 74 20 6f  can be.** part o
33b42 66 20 61 20 74 6f 6b 65 6e 2e 20 20 49 6e 20 6f  f a token.  In o
33b43 74 68 65 72 20 77 6f 72 64 73 2c 20 64 65 6c 69  ther words, deli
33b44 6d 69 74 65 72 73 20 61 6c 6c 20 6d 75 73 74 20  miters all must 
33b45 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 20 6f  have.** values o
33b46 66 20 30 78 37 66 20 6f 72 20 6c 6f 77 65 72 2e  f 0x7f or lower.
33b47 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
33b48 20 63 68 61 72 20 70 6f 72 74 65 72 49 64 43 68   char porterIdCh
33b49 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78  ar[] = {./* x0 x
33b4a 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36  1 x2 x3 x4 x5 x6
33b4b 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20   x7 x8 x9 xA xB 
33b4c 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20  xC xD xE xF */. 
33b4d 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31     1, 1, 1, 1, 1
33b4e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
33b4f 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
33b50 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20  0,  /* 3x */.   
33b51 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
33b52 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
33b53 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
33b54 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31    /* 4x */.    1
33b55 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
33b56 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
33b57 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20  0, 0, 0, 0, 1,  
33b58 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* 5x */.    0, 
33b59 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
33b5a 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
33b5b 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a   1, 1, 1, 1,  /*
33b5c 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c   6x */.    1, 1,
33b5d 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
33b5e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
33b5f 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37  , 0, 0, 0,  /* 7
33b60 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20  x */.};.#define 
33b61 69 73 44 65 6c 69 6d 28 43 29 20 28 28 28 63 68  isDelim(C) (((ch
33b62 3d 43 29 26 30 78 38 30 29 3d 3d 30 20 26 26 20  =C)&0x80)==0 && 
33b63 28 63 68 3c 30 78 33 30 20 7c 7c 20 21 70 6f 72  (ch<0x30 || !por
33b64 74 65 72 49 64 43 68 61 72 5b 63 68 2d 30 78 33  terIdChar[ch-0x3
33b65 30 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  0]))../*.** Extr
33b66 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b  act the next tok
33b67 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69  en from a tokeni
33b68 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 20 20  zation cursor.  
33b69 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 0a  The cursor must.
33b6a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65  ** have been ope
33b6b 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63  ned by a prior c
33b6c 61 6c 6c 20 74 6f 20 70 6f 72 74 65 72 4f 70 65  all to porterOpe
33b6d 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  n()..*/.static i
33b6e 6e 74 20 70 6f 72 74 65 72 4e 65 78 74 28 0a 20  nt porterNext(. 
33b6f 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
33b70 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
33b71 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72  or,  /* Cursor r
33b72 65 74 75 72 6e 65 64 20 62 79 20 70 6f 72 74 65  eturned by porte
33b73 72 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  rOpen */.  const
33b74 20 63 68 61 72 20 2a 2a 70 7a 54 6f 6b 65 6e 2c   char **pzToken,
33b75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33b76 2a 20 4f 55 54 3a 20 2a 70 7a 54 6f 6b 65 6e 20  * OUT: *pzToken 
33b77 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78  is the token tex
33b78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79  t */.  int *pnBy
33b79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
33b7a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
33b7b 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
33b7c 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20  es in token */. 
33b7d 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
33b7e 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
33b7f 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61       /* OUT: Sta
33b80 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  rting offset of 
33b81 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
33b82 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20  piEndOffset,    
33b83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33b84 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66  * OUT: Ending of
33b85 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
33b86 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69  .  int *piPositi
33b87 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
33b88 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50         /* OUT: P
33b89 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20  osition integer 
33b8a 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  of token */.){. 
33b8b 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65   porter_tokenize
33b8c 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 70  r_cursor *c = (p
33b8d 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
33b8e 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
33b8f 72 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  r;.  const char 
33b90 2a 7a 20 3d 20 63 2d 3e 7a 49 6e 70 75 74 3b 0a  *z = c->zInput;.
33b91 0a 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66  .  while( c->iOf
33b92 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 29  fset<c->nInput )
33b93 7b 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74  {.    int iStart
33b94 4f 66 66 73 65 74 2c 20 63 68 3b 0a 0a 20 20 20  Offset, ch;..   
33b95 20 2f 2a 20 53 63 61 6e 20 70 61 73 74 20 64 65   /* Scan past de
33b96 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74 65  limiter characte
33b97 72 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  rs */.    while(
33b98 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e   c->iOffset<c->n
33b99 49 6e 70 75 74 20 26 26 20 69 73 44 65 6c 69 6d  Input && isDelim
33b9a 28 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20  (z[c->iOffset]) 
33b9b 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66 66  ){.      c->iOff
33b9c 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  set++;.    }..  
33b9d 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64    /* Count non-d
33b9e 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74  elimiter charact
33b9f 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53 74 61  ers. */.    iSta
33ba0 72 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f  rtOffset = c->iO
33ba1 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65  ffset;.    while
33ba2 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e  ( c->iOffset<c->
33ba3 6e 49 6e 70 75 74 20 26 26 20 21 69 73 44 65 6c  nInput && !isDel
33ba4 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d  im(z[c->iOffset]
33ba5 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f  ) ){.      c->iO
33ba6 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  ffset++;.    }..
33ba7 20 20 20 20 69 66 28 20 63 2d 3e 69 4f 66 66 73      if( c->iOffs
33ba8 65 74 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20  et>iStartOffset 
33ba9 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d  ){.      int n =
33baa 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53 74 61   c->iOffset-iSta
33bab 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  rtOffset;.      
33bac 69 66 28 20 6e 3e 63 2d 3e 6e 41 6c 6c 6f 63 61  if( n>c->nAlloca
33bad 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ted ){.        c
33bae 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e  ->nAllocated = n
33baf 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e  +20;.        c->
33bb0 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33  zToken = sqlite3
33bb1 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 7a 54 6f 6b  _realloc(c->zTok
33bb2 65 6e 2c 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65  en, c->nAllocate
33bb3 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
33bb4 63 2d 3e 7a 54 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20  c->zToken==NULL 
33bb5 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
33bb6 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
33bb7 20 20 20 20 20 70 6f 72 74 65 72 5f 73 74 65 6d       porter_stem
33bb8 6d 65 72 28 26 7a 5b 69 53 74 61 72 74 4f 66 66  mer(&z[iStartOff
33bb9 73 65 74 5d 2c 20 6e 2c 20 63 2d 3e 7a 54 6f 6b  set], n, c->zTok
33bba 65 6e 2c 20 70 6e 42 79 74 65 73 29 3b 0a 20 20  en, pnBytes);.  
33bbb 20 20 20 20 2a 70 7a 54 6f 6b 65 6e 20 3d 20 63      *pzToken = c
33bbc 2d 3e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  ->zToken;.      
33bbd 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d  *piStartOffset =
33bbe 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20   iStartOffset;. 
33bbf 20 20 20 20 20 2a 70 69 45 6e 64 4f 66 66 73 65       *piEndOffse
33bc0 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a  t = c->iOffset;.
33bc1 20 20 20 20 20 20 2a 70 69 50 6f 73 69 74 69 6f        *piPositio
33bc2 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b  n = c->iToken++;
33bc3 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
33bc4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
33bc5 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
33bc6 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a  TE_DONE;.}../*.*
33bc7 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f 75  * The set of rou
33bc8 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c 65  tines that imple
33bc9 6d 65 6e 74 20 74 68 65 20 70 6f 72 74 65 72 2d  ment the porter-
33bca 73 74 65 6d 6d 65 72 20 74 6f 6b 65 6e 69 7a 65  stemmer tokenize
33bcb 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  r.*/.static cons
33bcc 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
33bcd 7a 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72 74 65  zer_module porte
33bce 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  rTokenizerModule
33bcf 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 70 6f 72 74   = {.  0,.  port
33bd0 65 72 43 72 65 61 74 65 2c 0a 20 20 70 6f 72 74  erCreate,.  port
33bd1 65 72 44 65 73 74 72 6f 79 2c 0a 20 20 70 6f 72  erDestroy,.  por
33bd2 74 65 72 4f 70 65 6e 2c 0a 20 20 70 6f 72 74 65  terOpen,.  porte
33bd3 72 43 6c 6f 73 65 2c 0a 20 20 70 6f 72 74 65 72  rClose,.  porter
33bd4 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  Next,.};../*.** 
33bd5 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
33bd6 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65 72 2e  orter tokenizer.
33bd7 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
33bd8 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a 2a  er to the new.**
33bd9 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20 2a 70   tokenizer in *p
33bda 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51 4c 49 54  pModule.*/.SQLIT
33bdb 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
33bdc 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65 72  qlite3Fts3Porter
33bdd 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
33bde 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
33bdf 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
33be0 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20  t**ppModule.){. 
33be1 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 70 6f   *ppModule = &po
33be2 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  rterTokenizerMod
33be3 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  ule;.}..#endif /
33be4 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
33be5 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
33be6 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
33be7 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
33be8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
33be9 66 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63 20  f fts3_porter.c 
33bea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33beb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33bec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
33bed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
33bee 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e   file fts3_token
33bef 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  izer.c *********
33bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33bf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
33bf2 20 32 30 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a   2007 June 22.**
33bf3 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
33bf4 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
33bf5 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
33bf6 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
33bf7 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
33bf8 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
33bf9 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
33bfa 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
33bfb 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
33bfc 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
33bfd 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
33bfe 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
33bff 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
33c00 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
33c01 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
33c02 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
33c03 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
33c04 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
33c05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33c06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33c07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33c08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33c09 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  *.**.** This is 
33c0a 70 61 72 74 20 6f 66 20 61 6e 20 53 51 4c 69 74  part of an SQLit
33c0b 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  e module impleme
33c0c 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78 74 20  nting full-text 
33c0d 73 65 61 72 63 68 2e 0a 2a 2a 20 54 68 69 73 20  search..** This 
33c0e 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20  particular file 
33c0f 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 67  implements the g
33c10 65 6e 65 72 69 63 20 74 6f 6b 65 6e 69 7a 65 72  eneric tokenizer
33c11 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a   interface..*/..
33c12 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  /*.** The code i
33c13 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
33c14 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a  nly compiled if:
33c15 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
33c16 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
33c17 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
33c18 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20  n extension.**  
33c19 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63       (in which c
33c1a 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ase SQLITE_CORE 
33c1b 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
33c1c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20   or.**.**     * 
33c1d 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
33c1e 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69  is being built i
33c1f 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a  nto the core of.
33c20 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20  **       SQLite 
33c21 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
33c22 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
33c23 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a  3 is defined)..*
33c24 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
33c25 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
33c26 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
33c27 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69 66 6e  ABLE_FTS3)..#ifn
33c28 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a  def SQLITE_CORE.
33c29 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
33c2a 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69 66 0a  ON_INIT1.#endif.
33c2b 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
33c2c 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51  tation of the SQ
33c2d 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  L scalar functio
33c2e 6e 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  n for accessing 
33c2f 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 0a  the underlying .
33c30 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20 54  ** hash table. T
33c31 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
33c32 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20 66 6f   be called as fo
33c33 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53  llows:.**.**   S
33c34 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d  ELECT <function-
33c35 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e  name>(<key-name>
33c36 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c  );.**   SELECT <
33c37 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c  function-name>(<
33c38 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e  key-name>, <poin
33c39 74 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77 68 65  ter>);.**.** whe
33c3a 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d  re <function-nam
33c3b 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 70  e> is the name p
33c3c 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
33c3d 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ond argument.** 
33c3e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 46 74  to the sqlite3Ft
33c3f 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28  s3InitHashTable(
33c40 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e 67 2e  ) function (e.g.
33c41 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72   'fts3_tokenizer
33c42 27 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ')..**.** If the
33c43 20 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67 75 6d   <pointer> argum
33c44 65 6e 74 20 69 73 20 73 70 65 63 69 66 69 65 64  ent is specified
33c45 2c 20 69 74 20 6d 75 73 74 20 62 65 20 61 20 62  , it must be a b
33c46 6c 6f 62 20 76 61 6c 75 65 0a 2a 2a 20 63 6f 6e  lob value.** con
33c47 74 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  taining a pointe
33c48 72 20 74 6f 20 62 65 20 73 74 6f 72 65 64 20 61  r to be stored a
33c49 73 20 74 68 65 20 68 61 73 68 20 64 61 74 61 20  s the hash data 
33c4a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
33c4b 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 20 3c   to the string <
33c4c 6b 65 79 2d 6e 61 6d 65 3e 2e 20 49 66 20 3c 70  key-name>. If <p
33c4d 6f 69 6e 74 65 72 3e 20 69 73 20 6e 6f 74 20 73  ointer> is not s
33c4e 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 0a 2a  pecified, then.*
33c4f 2a 20 74 68 65 20 73 74 72 69 6e 67 20 3c 6b 65  * the string <ke
33c50 79 2d 6e 61 6d 65 3e 20 6d 75 73 74 20 61 6c 72  y-name> must alr
33c51 65 61 64 79 20 65 78 69 73 74 20 69 6e 20 74 68  eady exist in th
33c52 65 20 68 61 73 20 74 61 62 6c 65 2e 20 4f 74 68  e has table. Oth
33c53 65 72 77 69 73 65 2c 0a 2a 2a 20 61 6e 20 65 72  erwise,.** an er
33c54 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ror is returned.
33c55 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f  .**.** Whether o
33c56 72 20 6e 6f 74 20 74 68 65 20 3c 70 6f 69 6e 74  r not the <point
33c57 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20  er> argument is 
33c58 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 20 76  specified, the v
33c59 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a  alue returned.**
33c5a 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e 74 61   is a blob conta
33c5b 69 6e 69 6e 67 20 74 68 65 20 70 6f 69 6e 74 65  ining the pointe
33c5c 72 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 20  r stored as the 
33c5d 68 61 73 68 20 64 61 74 61 20 63 6f 72 72 65 73  hash data corres
33c5e 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 73 74  ponding.** to st
33c5f 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20  ring <key-name> 
33c60 28 61 66 74 65 72 20 74 68 65 20 68 61 73 68 2d  (after the hash-
33c61 74 61 62 6c 65 20 69 73 20 75 70 64 61 74 65 64  table is updated
33c62 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29  , if applicable)
33c63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
33c64 20 73 63 61 6c 61 72 46 75 6e 63 28 0a 20 20 73   scalarFunc(.  s
33c65 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
33c66 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
33c67 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
33c68 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
33c69 20 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68   fts3Hash *pHash
33c6a 3b 0a 20 20 76 6f 69 64 20 2a 70 50 74 72 20 3d  ;.  void *pPtr =
33c6b 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   0;.  const unsi
33c6c 67 6e 65 64 20 63 68 61 72 20 2a 7a 4e 61 6d 65  gned char *zName
33c6d 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a  ;.  int nName;..
33c6e 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
33c6f 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a  1 || argc==2 );.
33c70 0a 20 20 70 48 61 73 68 20 3d 20 28 66 74 73 33  .  pHash = (fts3
33c71 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f 75  Hash *)sqlite3_u
33c72 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
33c73 29 3b 0a 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71  );..  zName = sq
33c74 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
33c75 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 4e 61  (argv[0]);.  nNa
33c76 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  me = sqlite3_val
33c77 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
33c78 29 2b 31 3b 0a 0a 20 20 69 66 28 20 61 72 67 63  )+1;..  if( argc
33c79 3d 3d 32 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  ==2 ){.    void 
33c7a 2a 70 4f 6c 64 3b 0a 20 20 20 20 69 6e 74 20 6e  *pOld;.    int n
33c7b 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
33c7c 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29 3b  _bytes(argv[1]);
33c7d 0a 20 20 20 20 69 66 28 20 6e 21 3d 73 69 7a 65  .    if( n!=size
33c7e 6f 66 28 70 50 74 72 29 20 29 7b 0a 20 20 20 20  of(pPtr) ){.    
33c7f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
33c80 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
33c81 22 61 72 67 75 6d 65 6e 74 20 74 79 70 65 20 6d  "argument type m
33c82 69 73 6d 61 74 63 68 22 2c 20 2d 31 29 3b 0a 20  ismatch", -1);. 
33c83 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
33c84 20 7d 0a 20 20 20 20 70 50 74 72 20 3d 20 2a 28   }.    pPtr = *(
33c85 76 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65 33 5f  void **)sqlite3_
33c86 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
33c87 31 5d 29 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20  1]);.    pOld = 
33c88 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
33c89 6e 73 65 72 74 28 70 48 61 73 68 2c 20 28 76 6f  nsert(pHash, (vo
33c8a 69 64 20 2a 29 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  id *)zName, nNam
33c8b 65 2c 20 70 50 74 72 29 3b 0a 20 20 20 20 69 66  e, pPtr);.    if
33c8c 28 20 70 4f 6c 64 3d 3d 70 50 74 72 20 29 7b 0a  ( pOld==pPtr ){.
33c8d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
33c8e 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
33c8f 78 74 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  xt, "out of memo
33c90 72 79 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  ry", -1);.      
33c91 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
33c92 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 74 72 20  }else{.    pPtr 
33c93 3d 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  = sqlite3Fts3Has
33c94 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a 4e 61  hFind(pHash, zNa
33c95 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
33c96 69 66 28 20 21 70 50 74 72 20 29 7b 0a 20 20 20  if( !pPtr ){.   
33c97 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20     char *zErr = 
33c98 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
33c99 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a  "unknown tokeniz
33c9a 65 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  er: %s", zName);
33c9b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
33c9c 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
33c9d 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a  ext, zErr, -1);.
33c9e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
33c9f 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  ee(zErr);.      
33ca0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
33ca1 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  }..  sqlite3_res
33ca2 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74  ult_blob(context
33ca3 2c 20 28 76 6f 69 64 20 2a 29 26 70 50 74 72 2c  , (void *)&pPtr,
33ca4 20 73 69 7a 65 6f 66 28 70 50 74 72 29 2c 20 53   sizeof(pPtr), S
33ca5 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
33ca6 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
33ca7 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20  TE_TEST.../*.** 
33ca8 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
33ca9 66 20 61 20 73 70 65 63 69 61 6c 20 53 51 4c 20  f a special SQL 
33caa 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
33cab 66 6f 72 20 74 65 73 74 69 6e 67 20 74 6f 6b 65  for testing toke
33cac 6e 69 7a 65 72 73 20 0a 2a 2a 20 64 65 73 69 67  nizers .** desig
33cad 6e 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 69  ned to be used i
33cae 6e 20 63 6f 6e 63 65 72 74 20 77 69 74 68 20 74  n concert with t
33caf 68 65 20 54 63 6c 20 74 65 73 74 69 6e 67 20 66  he Tcl testing f
33cb0 72 61 6d 65 77 6f 72 6b 2e 20 54 68 69 73 0a 2a  ramework. This.*
33cb1 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  * function must 
33cb2 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74  be called with t
33cb3 77 6f 20 61 72 67 75 6d 65 6e 74 73 3a 0a 2a 2a  wo arguments:.**
33cb4 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75  .**   SELECT <fu
33cb5 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65  nction-name>(<ke
33cb6 79 2d 6e 61 6d 65 3e 2c 20 3c 69 6e 70 75 74 2d  y-name>, <input-
33cb7 73 74 72 69 6e 67 3e 29 3b 0a 2a 2a 20 20 20 53  string>);.**   S
33cb8 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d  ELECT <function-
33cb9 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e  name>(<key-name>
33cba 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a 2a 2a  , <pointer>);.**
33cbb 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e 63 74  .** where <funct
33cbc 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65  ion-name> is the
33cbd 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61 73 20   name passed as 
33cbe 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
33cbf 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71  ent.** to the sq
33cc0 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73  lite3Fts3InitHas
33cc1 68 54 61 62 6c 65 28 29 20 66 75 6e 63 74 69 6f  hTable() functio
33cc2 6e 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74 6f  n (e.g. 'fts3_to
33cc3 6b 65 6e 69 7a 65 72 27 29 0a 2a 2a 20 63 6f 6e  kenizer').** con
33cc4 63 61 74 65 6e 61 74 65 64 20 77 69 74 68 20 74  catenated with t
33cc5 68 65 20 73 74 72 69 6e 67 20 27 5f 74 65 73 74  he string '_test
33cc6 27 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74 6f  ' (e.g. 'fts3_to
33cc7 6b 65 6e 69 7a 65 72 5f 74 65 73 74 27 29 2e 0a  kenizer_test')..
33cc8 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
33cc9 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69   value is a stri
33cca 6e 67 20 74 68 61 74 20 6d 61 79 20 62 65 20 69  ng that may be i
33ccb 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20  nterpreted as a 
33ccc 54 63 6c 0a 2a 2a 20 6c 69 73 74 2e 20 46 6f 72  Tcl.** list. For
33ccd 20 65 61 63 68 20 74 6f 6b 65 6e 20 69 6e 20 74   each token in t
33cce 68 65 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e 67  he <input-string
33ccf 3e 2c 20 74 68 72 65 65 20 65 6c 65 6d 65 6e 74  >, three element
33cd0 73 20 61 72 65 0a 2a 2a 20 61 64 64 65 64 20 74  s are.** added t
33cd1 6f 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 6c  o the returned l
33cd2 69 73 74 2e 20 54 68 65 20 66 69 72 73 74 20 69  ist. The first i
33cd3 73 20 74 68 65 20 74 6f 6b 65 6e 20 70 6f 73 69  s the token posi
33cd4 74 69 6f 6e 2c 20 74 68 65 20 0a 2a 2a 20 73 65  tion, the .** se
33cd5 63 6f 6e 64 20 69 73 20 74 68 65 20 74 6f 6b 65  cond is the toke
33cd6 6e 20 74 65 78 74 20 28 66 6f 6c 64 65 64 2c 20  n text (folded, 
33cd7 73 74 65 6d 6d 65 64 2c 20 65 74 63 2e 29 20 61  stemmed, etc.) a
33cd8 6e 64 20 74 68 65 20 74 68 69 72 64 20 69 73 20  nd the third is 
33cd9 74 68 65 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67  the.** substring
33cda 20 6f 66 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e   of <input-strin
33cdb 67 3e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  g> associated wi
33cdc 74 68 20 74 68 65 20 74 6f 6b 65 6e 2e 20 46 6f  th the token. Fo
33cdd 72 20 65 78 61 6d 70 6c 65 2c 20 0a 2a 2a 20 75  r example, .** u
33cde 73 69 6e 67 20 74 68 65 20 62 75 69 6c 74 2d 69  sing the built-i
33cdf 6e 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b 65 6e  n "simple" token
33ce0 69 7a 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  izer:.**.**   SE
33ce1 4c 45 43 54 20 66 74 73 5f 74 6f 6b 65 6e 69 7a  LECT fts_tokeniz
33ce2 65 72 5f 74 65 73 74 28 27 73 69 6d 70 6c 65 27  er_test('simple'
33ce3 2c 20 27 49 20 64 6f 6e 27 74 20 73 65 65 20 68  , 'I don't see h
33ce4 6f 77 27 29 3b 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c  ow');.**.** will
33ce5 20 72 65 74 75 72 6e 20 74 68 65 20 73 74 72 69   return the stri
33ce6 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 7b 30 20  ng:.**.**   "{0 
33ce7 69 20 49 20 31 20 64 6f 6e 74 20 64 6f 6e 27 74  i I 1 dont don't
33ce8 20 32 20 73 65 65 20 73 65 65 20 33 20 68 6f 77   2 see see 3 how
33ce9 20 68 6f 77 7d 22 0a 2a 2a 20 20 20 0a 2a 2f 0a   how}".**   .*/.
33cea 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74  static void test
33ceb 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
33cec 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
33ced 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
33cee 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
33cef 61 72 67 76 0a 29 7b 0a 20 20 66 74 73 33 48 61  argv.){.  fts3Ha
33cf0 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 73 71 6c  sh *pHash;.  sql
33cf1 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
33cf2 6f 64 75 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69  odule *p;.  sqli
33cf3 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
33cf4 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 0a 20  Tokenizer = 0;. 
33cf5 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
33cf6 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 73 72 20  er_cursor *pCsr 
33cf7 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68  = 0;..  const ch
33cf8 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20  ar *zErr = 0;.. 
33cf9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
33cfa 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b  me;.  int nName;
33cfb 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
33cfc 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e 49 6e  Input;.  int nIn
33cfd 70 75 74 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68  put;..  const ch
33cfe 61 72 20 2a 7a 41 72 67 20 3d 20 30 3b 0a 0a 20  ar *zArg = 0;.. 
33cff 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f   const char *zTo
33d00 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65  ken;.  int nToke
33d01 6e 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74 3b  n;.  int iStart;
33d02 0a 20 20 69 6e 74 20 69 45 6e 64 3b 0a 20 20 69  .  int iEnd;.  i
33d03 6e 74 20 69 50 6f 73 3b 0a 0a 20 20 54 63 6c 5f  nt iPos;..  Tcl_
33d04 4f 62 6a 20 2a 70 52 65 74 3b 0a 0a 20 20 61 73  Obj *pRet;..  as
33d05 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20 7c 7c  sert( argc==2 ||
33d06 20 61 72 67 63 3d 3d 33 20 29 3b 0a 0a 20 20 6e   argc==3 );..  n
33d07 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  Name = sqlite3_v
33d08 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
33d09 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28  0]);.  zName = (
33d0a 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
33d0b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
33d0c 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 49 6e 70  argv[0]);.  nInp
33d0d 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ut = sqlite3_val
33d0e 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 61 72  ue_bytes(argv[ar
33d0f 67 63 2d 31 5d 29 3b 0a 20 20 7a 49 6e 70 75 74  gc-1]);.  zInput
33d10 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
33d11 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
33d12 65 78 74 28 61 72 67 76 5b 61 72 67 63 2d 31 5d  ext(argv[argc-1]
33d13 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  );..  if( argc==
33d14 33 20 29 7b 0a 20 20 20 20 7a 41 72 67 20 3d 20  3 ){.    zArg = 
33d15 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
33d16 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
33d17 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 0a 0a  (argv[1]);.  }..
33d18 20 20 70 48 61 73 68 20 3d 20 28 66 74 73 33 48    pHash = (fts3H
33d19 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f 75 73  ash *)sqlite3_us
33d1a 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
33d1b 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 33  ;.  p = (sqlite3
33d1c 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
33d1d 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33 48  e *)sqlite3Fts3H
33d1e 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a  ashFind(pHash, z
33d1f 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a  Name, nName+1);.
33d20 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20  .  if( !p ){.   
33d21 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 73 71   char *zErr = sq
33d22 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 75  lite3_mprintf("u
33d23 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72  nknown tokenizer
33d24 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  : %s", zName);. 
33d25 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
33d26 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
33d27 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20   zErr, -1);.    
33d28 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
33d29 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  r);.    return;.
33d2a 20 20 7d 0a 0a 20 20 70 52 65 74 20 3d 20 54 63    }..  pRet = Tc
33d2b 6c 5f 4e 65 77 4f 62 6a 28 29 3b 0a 20 20 54 63  l_NewObj();.  Tc
33d2c 6c 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74 28 70  l_IncrRefCount(p
33d2d 52 65 74 29 3b 0a 0a 20 20 69 66 28 20 53 51 4c  Ret);..  if( SQL
33d2e 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 72 65 61  ITE_OK!=p->xCrea
33d2f 74 65 28 7a 41 72 67 20 3f 20 31 20 3a 20 30 2c  te(zArg ? 1 : 0,
33d30 20 26 7a 41 72 67 2c 20 26 70 54 6f 6b 65 6e 69   &zArg, &pTokeni
33d31 7a 65 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72  zer) ){.    zErr
33d32 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 43 72   = "error in xCr
33d33 65 61 74 65 28 29 22 3b 0a 20 20 20 20 67 6f 74  eate()";.    got
33d34 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20  o finish;.  }.  
33d35 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64  pTokenizer->pMod
33d36 75 6c 65 20 3d 20 70 3b 0a 20 20 69 66 28 20 53  ule = p;.  if( S
33d37 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 4f 70  QLITE_OK!=p->xOp
33d38 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a  en(pTokenizer, z
33d39 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 26  Input, nInput, &
33d3a 70 43 73 72 29 20 29 7b 0a 20 20 20 20 7a 45 72  pCsr) ){.    zEr
33d3b 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 4f  r = "error in xO
33d3c 70 65 6e 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f  pen()";.    goto
33d3d 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20 70   finish;.  }.  p
33d3e 43 73 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  Csr->pTokenizer 
33d3f 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 20  = pTokenizer;.. 
33d40 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 4f   while( SQLITE_O
33d41 4b 3d 3d 70 2d 3e 78 4e 65 78 74 28 70 43 73 72  K==p->xNext(pCsr
33d42 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b  , &zToken, &nTok
33d43 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26 69 45  en, &iStart, &iE
33d44 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20 20  nd, &iPos) ){.  
33d45 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70    Tcl_ListObjApp
33d46 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52  endElement(0, pR
33d47 65 74 2c 20 54 63 6c 5f 4e 65 77 49 6e 74 4f 62  et, Tcl_NewIntOb
33d48 6a 28 69 50 6f 73 29 29 3b 0a 20 20 20 20 54 63  j(iPos));.    Tc
33d49 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45  l_ListObjAppendE
33d4a 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c 20  lement(0, pRet, 
33d4b 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62 6a  Tcl_NewStringObj
33d4c 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29  (zToken, nToken)
33d4d 29 3b 0a 20 20 20 20 7a 54 6f 6b 65 6e 20 3d 20  );.    zToken = 
33d4e 26 7a 49 6e 70 75 74 5b 69 53 74 61 72 74 5d 3b  &zInput[iStart];
33d4f 0a 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 69 45  .    nToken = iE
33d50 6e 64 2d 69 53 74 61 72 74 3b 0a 20 20 20 20 54  nd-iStart;.    T
33d51 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64  cl_ListObjAppend
33d52 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c  Element(0, pRet,
33d53 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62   Tcl_NewStringOb
33d54 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  j(zToken, nToken
33d55 29 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53  ));.  }..  if( S
33d56 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 6c  QLITE_OK!=p->xCl
33d57 6f 73 65 28 70 43 73 72 29 20 29 7b 0a 20 20 20  ose(pCsr) ){.   
33d58 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69   zErr = "error i
33d59 6e 20 78 43 6c 6f 73 65 28 29 22 3b 0a 20 20 20  n xClose()";.   
33d5a 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20   goto finish;.  
33d5b 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  }.  if( SQLITE_O
33d5c 4b 21 3d 70 2d 3e 78 44 65 73 74 72 6f 79 28 70  K!=p->xDestroy(p
33d5d 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a 20 20  Tokenizer) ){.  
33d5e 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20    zErr = "error 
33d5f 69 6e 20 78 44 65 73 74 72 6f 79 28 29 22 3b 0a  in xDestroy()";.
33d60 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b      goto finish;
33d61 0a 20 20 7d 0a 0a 66 69 6e 69 73 68 3a 0a 20 20  .  }..finish:.  
33d62 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20 20  if( zErr ){.    
33d63 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
33d64 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
33d65 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 65 6c 73 65  rr, -1);.  }else
33d66 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
33d67 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
33d68 74 2c 20 54 63 6c 5f 47 65 74 53 74 72 69 6e 67  t, Tcl_GetString
33d69 28 70 52 65 74 29 2c 20 2d 31 2c 20 53 51 4c 49  (pRet), -1, SQLI
33d6a 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
33d6b 20 7d 0a 20 20 54 63 6c 5f 44 65 63 72 52 65 66   }.  Tcl_DecrRef
33d6c 43 6f 75 6e 74 28 70 52 65 74 29 3b 0a 7d 0a 0a  Count(pRet);.}..
33d6d 73 74 61 74 69 63 0a 69 6e 74 20 72 65 67 69 73  static.int regis
33d6e 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20  terTokenizer(.  
33d6f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
33d70 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
33d71 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
33d72 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
33d73 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  p.){.  int rc;. 
33d74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
33d75 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68  Stmt;.  const ch
33d76 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c  ar zSql[] = "SEL
33d77 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ECT fts3_tokeniz
33d78 65 72 28 3f 2c 20 3f 29 22 3b 0a 0a 20 20 72 63  er(?, ?)";..  rc
33d79 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61   = sqlite3_prepa
33d7a 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20  re_v2(db, zSql, 
33d7b 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a  -1, &pStmt, 0);.
33d7c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
33d7d 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
33d7e 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  n rc;.  }..  sql
33d7f 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70  ite3_bind_text(p
33d80 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20  Stmt, 1, zName, 
33d81 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  -1, SQLITE_STATI
33d82 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  C);.  sqlite3_bi
33d83 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 32  nd_blob(pStmt, 2
33d84 2c 20 26 70 2c 20 73 69 7a 65 6f 66 28 70 29 2c  , &p, sizeof(p),
33d85 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
33d86 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
33d87 70 53 74 6d 74 29 3b 0a 0a 20 20 72 65 74 75 72  pStmt);..  retur
33d88 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  n sqlite3_finali
33d89 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73 74  ze(pStmt);.}..st
33d8a 61 74 69 63 0a 69 6e 74 20 71 75 65 72 79 54 6f  atic.int queryTo
33d8b 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  kenizer(.  sqlit
33d8c 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20  e3 *db, .  char 
33d8d 2a 7a 4e 61 6d 65 2c 20 20 0a 20 20 63 6f 6e 73  *zName,  .  cons
33d8e 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
33d8f 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0a  zer_module **pp.
33d90 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
33d91 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
33d92 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  mt;.  const char
33d93 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43   zSql[] = "SELEC
33d94 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  T fts3_tokenizer
33d95 28 3f 29 22 3b 0a 0a 20 20 2a 70 70 20 3d 20 30  (?)";..  *pp = 0
33d96 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
33d97 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
33d98 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
33d99 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
33d9a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
33d9b 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
33d9c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
33d9d 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a  text(pStmt, 1, z
33d9e 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45  Name, -1, SQLITE
33d9f 5f 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20  _STATIC);.  if( 
33da0 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69  SQLITE_ROW==sqli
33da1 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20  te3_step(pStmt) 
33da2 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
33da3 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70  e3_column_type(p
33da4 53 74 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45  Stmt, 0)==SQLITE
33da5 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 6d  _BLOB ){.      m
33da6 65 6d 63 70 79 28 70 70 2c 20 73 71 6c 69 74 65  emcpy(pp, sqlite
33da7 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53  3_column_blob(pS
33da8 74 6d 74 2c 20 30 29 2c 20 73 69 7a 65 6f 66 28  tmt, 0), sizeof(
33da9 2a 70 70 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  *pp));.    }.  }
33daa 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
33dab 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
33dac 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  t);.}..SQLITE_PR
33dad 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
33dae 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
33daf 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69  nizerModule(sqli
33db0 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
33db1 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f  dule const**ppMo
33db2 64 75 6c 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6d  dule);../*.** Im
33db3 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
33db4 74 68 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74  the scalar funct
33db5 69 6f 6e 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ion fts3_tokeniz
33db6 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74  er_internal_test
33db7 28 29 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ()..** This func
33db8 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72  tion is used for
33db9 20 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2c 20 69   testing only, i
33dba 74 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65  t is not include
33dbb 64 20 69 6e 20 74 68 65 0a 2a 2a 20 62 75 69 6c  d in the.** buil
33dbc 64 20 75 6e 6c 65 73 73 20 53 51 4c 49 54 45 5f  d unless SQLITE_
33dbd 54 45 53 54 20 69 73 20 64 65 66 69 6e 65 64 2e  TEST is defined.
33dbe 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f  .**.** The purpo
33dbf 73 65 20 6f 66 20 74 68 69 73 20 69 73 20 74 6f  se of this is to
33dc0 20 74 65 73 74 20 74 68 61 74 20 74 68 65 20 66   test that the f
33dc1 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 29 20  ts3_tokenizer() 
33dc2 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20  function.** can 
33dc3 62 65 20 75 73 65 64 20 61 73 20 64 65 73 69 67  be used as desig
33dc4 6e 65 64 20 62 79 20 74 68 65 20 43 2d 63 6f 64  ned by the C-cod
33dc5 65 20 69 6e 20 74 68 65 20 71 75 65 72 79 54 6f  e in the queryTo
33dc6 6b 65 6e 69 7a 65 72 20 61 6e 64 0a 2a 2a 20 72  kenizer and.** r
33dc7 65 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72  egisterTokenizer
33dc8 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 61 62 6f  () functions abo
33dc9 76 65 2e 20 54 68 65 73 65 20 74 77 6f 20 66 75  ve. These two fu
33dca 6e 63 74 69 6f 6e 73 20 61 72 65 20 72 65 70 65  nctions are repe
33dcb 61 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 52  ated.** in the R
33dcc 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65 72 20  EADME.tokenizer 
33dcd 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70  file as an examp
33dce 6c 65 2c 20 73 6f 20 69 74 20 69 73 20 69 6d 70  le, so it is imp
33dcf 6f 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 65 73  ortant to.** tes
33dd0 74 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 6f  t them..**.** To
33dd1 20 72 75 6e 20 74 68 65 20 74 65 73 74 73 2c 20   run the tests, 
33dd2 65 76 61 6c 75 61 74 65 20 74 68 65 20 66 74 73  evaluate the fts
33dd3 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65  3_tokenizer_inte
33dd4 72 6e 61 6c 5f 74 65 73 74 28 29 20 73 63 61 6c  rnal_test() scal
33dd5 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77  ar.** function w
33dd6 69 74 68 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73  ith no arguments
33dd7 2e 20 41 6e 20 61 73 73 65 72 74 28 29 20 77 69  . An assert() wi
33dd8 6c 6c 20 66 61 69 6c 20 69 66 20 61 20 70 72 6f  ll fail if a pro
33dd9 62 6c 65 6d 20 69 73 0a 2a 2a 20 64 65 74 65 63  blem is.** detec
33dda 74 65 64 2e 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a  ted. i.e.:.**.**
33ddb 20 20 20 20 20 53 45 4c 45 43 54 20 66 74 73 33       SELECT fts3
33ddc 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72  _tokenizer_inter
33ddd 6e 61 6c 5f 74 65 73 74 28 29 3b 0a 2a 2a 0a 2a  nal_test();.**.*
33dde 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
33ddf 74 54 65 73 74 46 75 6e 63 28 0a 20 20 73 71 6c  tTestFunc(.  sql
33de0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
33de1 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
33de2 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
33de3 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
33de4 6e 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74 20 73  nt rc;.  const s
33de5 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
33de6 5f 6d 6f 64 75 6c 65 20 2a 70 31 3b 0a 20 20 63  _module *p1;.  c
33de7 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
33de8 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
33de9 32 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2;.  sqlite3 *db
33dea 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 73 71   = (sqlite3 *)sq
33deb 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
33dec 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 2f 2a 20  context);..  /* 
33ded 54 65 73 74 20 74 68 65 20 71 75 65 72 79 20 66  Test the query f
33dee 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  unction */.  sql
33def 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f  ite3Fts3SimpleTo
33df0 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70  kenizerModule(&p
33df1 31 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79  1);.  rc = query
33df2 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 73  Tokenizer(db, "s
33df3 69 6d 70 6c 65 22 2c 20 26 70 32 29 3b 0a 20 20  imple", &p2);.  
33df4 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
33df5 54 45 5f 4f 4b 20 29 3b 0a 20 20 61 73 73 65 72  TE_OK );.  asser
33df6 74 28 20 70 31 3d 3d 70 32 20 29 3b 0a 20 20 72  t( p1==p2 );.  r
33df7 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  c = queryTokeniz
33df8 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f  er(db, "nosuchto
33df9 6b 65 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0a  kenizer", &p2);.
33dfa 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
33dfb 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 20 20  LITE_ERROR );.  
33dfc 61 73 73 65 72 74 28 20 70 32 3d 3d 30 20 29 3b  assert( p2==0 );
33dfd 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 73 74  .  assert( 0==st
33dfe 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 65 72 72  rcmp(sqlite3_err
33dff 6d 73 67 28 64 62 29 2c 20 22 75 6e 6b 6e 6f 77  msg(db), "unknow
33e00 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 6e 6f 73  n tokenizer: nos
33e01 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 29 20 29  uchtokenizer") )
33e02 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74 68 65  ;..  /* Test the
33e03 20 73 74 6f 72 61 67 65 20 66 75 6e 63 74 69 6f   storage functio
33e04 6e 20 2a 2f 0a 20 20 72 63 20 3d 20 72 65 67 69  n */.  rc = regi
33e05 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 64 62  sterTokenizer(db
33e06 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a  , "nosuchtokeniz
33e07 65 72 22 2c 20 70 31 29 3b 0a 20 20 61 73 73 65  er", p1);.  asse
33e08 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
33e09 4b 20 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72  K );.  rc = quer
33e0a 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22  yTokenizer(db, "
33e0b 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22  nosuchtokenizer"
33e0c 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74  , &p2);.  assert
33e0d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
33e0e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 32 3d  );.  assert( p2=
33e0f 3d 70 31 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65  =p1 );..  sqlite
33e10 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
33e11 6e 74 65 78 74 2c 20 22 6f 6b 22 2c 20 2d 31 2c  ntext, "ok", -1,
33e12 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
33e13 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  .}..#endif../*.*
33e14 2a 20 53 65 74 20 75 70 20 53 51 4c 20 6f 62 6a  * Set up SQL obj
33e15 65 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65  ects in database
33e16 20 64 62 20 75 73 65 64 20 74 6f 20 61 63 63 65   db used to acce
33e17 73 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ss the contents 
33e18 6f 66 0a 2a 2a 20 74 68 65 20 68 61 73 68 20 74  of.** the hash t
33e19 61 62 6c 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  able pointed to 
33e1a 62 79 20 61 72 67 75 6d 65 6e 74 20 70 48 61 73  by argument pHas
33e1b 68 2e 20 54 68 65 20 68 61 73 68 20 74 61 62 6c  h. The hash tabl
33e1c 65 20 6d 75 73 74 0a 2a 2a 20 62 65 65 6e 20 69  e must.** been i
33e1d 6e 69 74 69 61 6c 69 73 65 64 20 74 6f 20 75 73  nitialised to us
33e1e 65 20 73 74 72 69 6e 67 20 6b 65 79 73 2c 20 61  e string keys, a
33e1f 6e 64 20 74 6f 20 74 61 6b 65 20 61 20 70 72 69  nd to take a pri
33e20 76 61 74 65 20 63 6f 70 79 20 0a 2a 2a 20 6f 66  vate copy .** of
33e21 20 74 68 65 20 6b 65 79 20 77 68 65 6e 20 61 20   the key when a 
33e22 76 61 6c 75 65 20 69 73 20 69 6e 73 65 72 74 65  value is inserte
33e23 64 2e 20 69 2e 65 2e 20 62 79 20 61 20 63 61 6c  d. i.e. by a cal
33e24 6c 20 73 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a  l similar to:.**
33e25 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 46 74  .**    sqlite3Ft
33e26 73 33 48 61 73 68 49 6e 69 74 28 70 48 61 73 68  s3HashInit(pHash
33e27 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49  , FTS3_HASH_STRI
33e28 4e 47 2c 20 31 29 3b 0a 2a 2a 0a 2a 2a 20 54 68  NG, 1);.**.** Th
33e29 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 73  is function adds
33e2a 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69   a scalar functi
33e2b 6f 6e 20 28 73 65 65 20 68 65 61 64 65 72 20 63  on (see header c
33e2c 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 2a 2a 20  omment above.** 
33e2d 73 63 61 6c 61 72 46 75 6e 63 28 29 20 69 6e 20  scalarFunc() in 
33e2e 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65  this file for de
33e2f 74 61 69 6c 73 29 20 61 6e 64 2c 20 69 66 20 45  tails) and, if E
33e30 4e 41 42 4c 45 5f 54 41 42 4c 45 20 69 73 0a 2a  NABLE_TABLE is.*
33e31 2a 20 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d  * defined at com
33e32 70 69 6c 61 74 69 6f 6e 20 74 69 6d 65 2c 20 61  pilation time, a
33e33 20 74 65 6d 70 6f 72 61 72 79 20 76 69 72 74 75   temporary virtu
33e34 61 6c 20 74 61 62 6c 65 20 28 73 65 65 20 68 65  al table (see he
33e35 61 64 65 72 20 0a 2a 2a 20 63 6f 6d 6d 65 6e 74  ader .** comment
33e36 20 61 62 6f 76 65 20 73 74 72 75 63 74 20 48 61   above struct Ha
33e37 73 68 54 61 62 6c 65 56 74 61 62 29 20 74 6f 20  shTableVtab) to 
33e38 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
33e39 65 6d 61 2e 20 42 6f 74 68 20 0a 2a 2a 20 70 72  ema. Both .** pr
33e3a 6f 76 69 64 65 20 72 65 61 64 2f 77 72 69 74 65  ovide read/write
33e3b 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 63   access to the c
33e3c 6f 6e 74 65 6e 74 73 20 6f 66 20 2a 70 48 61 73  ontents of *pHas
33e3d 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69  h..**.** The thi
33e3e 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  rd argument to t
33e3f 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 7a 4e  his function, zN
33e40 61 6d 65 2c 20 69 73 20 75 73 65 64 20 61 73 20  ame, is used as 
33e41 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 62  the name.** of b
33e42 6f 74 68 20 74 68 65 20 73 63 61 6c 61 72 20 61  oth the scalar a
33e43 6e 64 2c 20 69 66 20 63 72 65 61 74 65 64 2c 20  nd, if created, 
33e44 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
33e45 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
33e46 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
33e47 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c  Fts3InitHashTabl
33e48 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
33e49 2c 20 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  , .  fts3Hash *p
33e4a 48 61 73 68 2c 20 0a 20 20 63 6f 6e 73 74 20 63  Hash, .  const c
33e4b 68 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20  har *zName.){.  
33e4c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
33e4d 4f 4b 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20  OK;.  void *p = 
33e4e 28 76 6f 69 64 20 2a 29 70 48 61 73 68 3b 0a 20  (void *)pHash;. 
33e4f 20 63 6f 6e 73 74 20 69 6e 74 20 61 6e 79 20 3d   const int any =
33e50 20 53 51 4c 49 54 45 5f 41 4e 59 3b 0a 20 20 63   SQLITE_ANY;.  c
33e51 68 61 72 20 2a 7a 54 65 73 74 20 3d 20 30 3b 0a  har *zTest = 0;.
33e52 20 20 63 68 61 72 20 2a 7a 54 65 73 74 32 20 3d    char *zTest2 =
33e53 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   0;..#ifdef SQLI
33e54 54 45 5f 54 45 53 54 0a 20 20 76 6f 69 64 20 2a  TE_TEST.  void *
33e55 70 64 62 20 3d 20 28 76 6f 69 64 20 2a 29 64 62  pdb = (void *)db
33e56 3b 0a 20 20 7a 54 65 73 74 20 3d 20 73 71 6c 69  ;.  zTest = sqli
33e57 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f  te3_mprintf("%s_
33e58 74 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  test", zName);. 
33e59 20 7a 54 65 73 74 32 20 3d 20 73 71 6c 69 74 65   zTest2 = sqlite
33e5a 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f 69 6e  3_mprintf("%s_in
33e5b 74 65 72 6e 61 6c 5f 74 65 73 74 22 2c 20 7a 4e  ternal_test", zN
33e5c 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 54 65  ame);.  if( !zTe
33e5d 73 74 20 7c 7c 20 21 7a 54 65 73 74 32 20 29 7b  st || !zTest2 ){
33e5e 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
33e5f 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64  _NOMEM;.  }.#end
33e60 69 66 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51  if..  if( rc!=SQ
33e61 4c 49 54 45 5f 4f 4b 0a 20 20 20 7c 7c 20 28 72  LITE_OK.   || (r
33e62 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61  c = sqlite3_crea
33e63 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  te_function(db, 
33e64 7a 4e 61 6d 65 2c 20 31 2c 20 61 6e 79 2c 20 70  zName, 1, any, p
33e65 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c 20 30 2c  , scalarFunc, 0,
33e66 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20 3d   0)).   || (rc =
33e67 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
33e68 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61  function(db, zNa
33e69 6d 65 2c 20 32 2c 20 61 6e 79 2c 20 70 2c 20 73  me, 2, any, p, s
33e6a 63 61 6c 61 72 46 75 6e 63 2c 20 30 2c 20 30 29  calarFunc, 0, 0)
33e6b 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ).#ifdef SQLITE_
33e6c 54 45 53 54 0a 20 20 20 7c 7c 20 28 72 63 20 3d  TEST.   || (rc =
33e6d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
33e6e 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65  function(db, zTe
33e6f 73 74 2c 20 32 2c 20 61 6e 79 2c 20 70 2c 20 74  st, 2, any, p, t
33e70 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a  estFunc, 0, 0)).
33e71 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
33e72 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
33e73 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c 20 33  ion(db, zTest, 3
33e74 2c 20 61 6e 79 2c 20 70 2c 20 74 65 73 74 46 75  , any, p, testFu
33e75 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c  nc, 0, 0)).   ||
33e76 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63   (rc = sqlite3_c
33e77 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
33e78 62 2c 20 7a 54 65 73 74 32 2c 20 30 2c 20 61 6e  b, zTest2, 0, an
33e79 79 2c 20 70 64 62 2c 20 69 6e 74 54 65 73 74 46  y, pdb, intTestF
33e7a 75 6e 63 2c 20 30 2c 20 30 29 29 0a 23 65 6e 64  unc, 0, 0)).#end
33e7b 69 66 0a 20 20 29 3b 0a 0a 20 20 73 71 6c 69 74  if.  );..  sqlit
33e7c 65 33 5f 66 72 65 65 28 7a 54 65 73 74 29 3b 0a  e3_free(zTest);.
33e7d 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
33e7e 54 65 73 74 32 29 3b 0a 20 20 72 65 74 75 72 6e  Test2);.  return
33e7f 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f   rc;.}..#endif /
33e80 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
33e81 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
33e82 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
33e83 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
33e84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
33e85 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  f fts3_tokenizer
33e86 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
33e87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
33e89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
33e8a 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e   file fts3_token
33e8b 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  izer1.c ********
33e8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
33e8e 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a   2006 Oct 10.**.
33e8f 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
33e90 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
33e91 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
33e92 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
33e93 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
33e94 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
33e95 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
33e96 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
33e97 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
33e98 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
33e99 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
33e9a 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
33e9b 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
33e9c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
33e9d 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
33e9e 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
33e9f 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
33ea0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
33ea1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33ea2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33ea3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33ea4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33ea5 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  .**.** Implement
33ea6 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 22 73 69  ation of the "si
33ea7 6d 70 6c 65 22 20 66 75 6c 6c 2d 74 65 78 74 2d  mple" full-text-
33ea8 73 65 61 72 63 68 20 74 6f 6b 65 6e 69 7a 65 72  search tokenizer
33ea9 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ..*/../*.** The 
33eaa 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
33eab 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c  e is only compil
33eac 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ed if:.**.**    
33ead 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
33eae 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
33eaf 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f  t as an extensio
33eb0 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77  n.**       (in w
33eb1 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
33eb2 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66  _CORE is not def
33eb3 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  ined), or.**.** 
33eb4 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
33eb5 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
33eb6 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f  uilt into the co
33eb7 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53  re of.**       S
33eb8 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20  QLite (in which 
33eb9 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  case SQLITE_ENAB
33eba 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e  LE_FTS3 is defin
33ebb 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ed)..*/.#if !def
33ebc 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
33ebd 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
33ebe 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
33ebf 0a 0a 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72  .....typedef str
33ec0 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  uct simple_token
33ec1 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33  izer {.  sqlite3
33ec2 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65 3b  _tokenizer base;
33ec3 0a 20 20 63 68 61 72 20 64 65 6c 69 6d 5b 31 32  .  char delim[12
33ec4 38 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  8];             
33ec5 2f 2a 20 66 6c 61 67 20 41 53 43 49 49 20 64 65  /* flag ASCII de
33ec6 6c 69 6d 69 74 65 72 73 20 2a 2f 0a 7d 20 73 69  limiters */.} si
33ec7 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a  mple_tokenizer;.
33ec8 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
33ec9 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
33eca 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69  _cursor {.  sqli
33ecb 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
33ecc 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 63 6f 6e  rsor base;.  con
33ecd 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75 74 3b  st char *pInput;
33ece 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 70            /* inp
33ecf 75 74 20 77 65 20 61 72 65 20 74 6f 6b 65 6e 69  ut we are tokeni
33ed0 7a 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  zing */.  int nB
33ed1 79 74 65 73 3b 20 20 20 20 20 20 20 20 20 20 20  ytes;           
33ed2 20 20 20 20 20 20 20 2f 2a 20 73 69 7a 65 20 6f         /* size o
33ed3 66 20 74 68 65 20 69 6e 70 75 74 20 2a 2f 0a 20  f the input */. 
33ed4 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20   int iOffset;   
33ed5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33ed6 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f   current positio
33ed7 6e 20 69 6e 20 70 49 6e 70 75 74 20 2a 2f 0a 20  n in pInput */. 
33ed8 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20 20 20   int iToken;    
33ed9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33eda 20 69 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 74   index of next t
33edb 6f 6b 65 6e 20 74 6f 20 62 65 20 72 65 74 75 72  oken to be retur
33edc 6e 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70  ned */.  char *p
33edd 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
33ede 20 20 20 20 20 20 2f 2a 20 73 74 6f 72 61 67 65        /* storage
33edf 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74 6f 6b   for current tok
33ee0 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b  en */.  int nTok
33ee1 65 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20 20  enAllocated;    
33ee2 20 20 20 20 20 2f 2a 20 73 70 61 63 65 20 61 6c       /* space al
33ee3 6c 6f 63 61 74 65 64 20 74 6f 20 7a 54 6f 6b 65  located to zToke
33ee4 6e 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20 73 69  n buffer */.} si
33ee5 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  mple_tokenizer_c
33ee6 75 72 73 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f 72 77  ursor;.../* Forw
33ee7 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  ard declaration 
33ee8 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
33ee9 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
33eea 72 5f 6d 6f 64 75 6c 65 20 73 69 6d 70 6c 65 54  r_module simpleT
33eeb 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a  okenizerModule;.
33eec 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70  .static int simp
33eed 6c 65 44 65 6c 69 6d 28 73 69 6d 70 6c 65 5f 74  leDelim(simple_t
33eee 6f 6b 65 6e 69 7a 65 72 20 2a 74 2c 20 75 6e 73  okenizer *t, uns
33eef 69 67 6e 65 64 20 63 68 61 72 20 63 29 7b 0a 20  igned char c){. 
33ef0 20 72 65 74 75 72 6e 20 63 3c 30 78 38 30 20 26   return c<0x80 &
33ef1 26 20 74 2d 3e 64 65 6c 69 6d 5b 63 5d 3b 0a 7d  & t->delim[c];.}
33ef2 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
33ef3 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69   new tokenizer i
33ef4 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74  nstance..*/.stat
33ef5 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 43 72 65  ic int simpleCre
33ef6 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63 2c  ate(.  int argc,
33ef7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f   const char * co
33ef8 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20 73 71 6c  nst *argv,.  sql
33ef9 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
33efa 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b 0a  *ppTokenizer.){.
33efb 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a    simple_tokeniz
33efc 65 72 20 2a 74 3b 0a 0a 20 20 74 20 3d 20 28 73  er *t;..  t = (s
33efd 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
33efe 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
33eff 63 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20  c(sizeof(*t));. 
33f00 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29 20 72   if( t==NULL ) r
33f01 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
33f02 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74 2c 20  EM;.  memset(t, 
33f03 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a  0, sizeof(*t));.
33f04 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .  /* TODO(shess
33f05 29 20 44 65 6c 69 6d 69 74 65 72 73 20 6e 65 65  ) Delimiters nee
33f06 64 20 74 6f 20 72 65 6d 61 69 6e 20 74 68 65 20  d to remain the 
33f07 73 61 6d 65 20 66 72 6f 6d 20 72 75 6e 20 74 6f  same from run to
33f08 20 72 75 6e 2c 0a 20 20 2a 2a 20 65 6c 73 65 20   run,.  ** else 
33f09 77 65 20 6e 65 65 64 20 74 6f 20 72 65 69 6e 64  we need to reind
33f0a 65 78 2e 20 20 4f 6e 65 20 73 6f 6c 75 74 69 6f  ex.  One solutio
33f0b 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 65 74  n would be a met
33f0c 61 2d 74 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20  a-table to.  ** 
33f0d 74 72 61 63 6b 20 73 75 63 68 20 69 6e 66 6f 72  track such infor
33f0e 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 64 61  mation in the da
33f0f 74 61 62 61 73 65 2c 20 74 68 65 6e 20 77 65 27  tabase, then we'
33f10 64 20 6f 6e 6c 79 20 77 61 6e 74 20 74 68 69 73  d only want this
33f11 0a 20 20 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  .  ** informatio
33f12 6e 20 6f 6e 20 74 68 65 20 69 6e 69 74 69 61 6c  n on the initial
33f13 20 63 72 65 61 74 65 2e 0a 20 20 2a 2f 0a 20 20   create..  */.  
33f14 69 66 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 20  if( argc>1 ){.  
33f15 20 20 69 6e 74 20 69 2c 20 6e 20 3d 20 73 74 72    int i, n = str
33f16 6c 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  len(argv[1]);.  
33f17 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20    for(i=0; i<n; 
33f18 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  i++){.      unsi
33f19 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d 20 61  gned char ch = a
33f1a 72 67 76 5b 31 5d 5b 69 5d 3b 0a 20 20 20 20 20  rgv[1][i];.     
33f1b 20 2f 2a 20 57 65 20 65 78 70 6c 69 63 69 74 6c   /* We explicitl
33f1c 79 20 64 6f 6e 27 74 20 73 75 70 70 6f 72 74 20  y don't support 
33f1d 55 54 46 2d 38 20 64 65 6c 69 6d 69 74 65 72 73  UTF-8 delimiters
33f1e 20 66 6f 72 20 6e 6f 77 2e 20 2a 2f 0a 20 20 20   for now. */.   
33f1f 20 20 20 69 66 28 20 63 68 3e 3d 30 78 38 30 20     if( ch>=0x80 
33f20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
33f21 65 33 5f 66 72 65 65 28 74 29 3b 0a 20 20 20 20  e3_free(t);.    
33f22 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
33f23 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d  E_ERROR;.      }
33f24 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b  .      t->delim[
33f25 63 68 5d 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ch] = 1;.    }. 
33f26 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a   } else {.    /*
33f27 20 4d 61 72 6b 20 6e 6f 6e 2d 61 6c 70 68 61 6e   Mark non-alphan
33f28 75 6d 65 72 69 63 20 41 53 43 49 49 20 63 68 61  umeric ASCII cha
33f29 72 61 63 74 65 72 73 20 61 73 20 64 65 6c 69 6d  racters as delim
33f2a 69 74 65 72 73 20 2a 2f 0a 20 20 20 20 69 6e 74  iters */.    int
33f2b 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   i;.    for(i=1;
33f2c 20 69 3c 30 78 38 30 3b 20 69 2b 2b 29 7b 0a 20   i<0x80; i++){. 
33f2d 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b 69 5d       t->delim[i]
33f2e 20 3d 20 21 69 73 61 6c 6e 75 6d 28 69 29 3b 0a   = !isalnum(i);.
33f2f 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70      }.  }..  *pp
33f30 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d 3e  Tokenizer = &t->
33f31 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53  base;.  return S
33f32 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
33f33 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b  ** Destroy a tok
33f34 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63  enizer.*/.static
33f35 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 73 74 72   int simpleDestr
33f36 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  oy(sqlite3_token
33f37 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
33f38 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ){.  sqlite3_fre
33f39 65 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20  e(pTokenizer);. 
33f3a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
33f3b 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70  K;.}../*.** Prep
33f3c 61 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b  are to begin tok
33f3d 65 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63  enizing a partic
33f3e 75 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68  ular string.  Th
33f3f 65 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e  e input.** strin
33f40 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65  g to be tokenize
33f41 64 20 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e  d is pInput[0..n
33f42 42 79 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72  Bytes-1].  A cur
33f43 73 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69  sor.** used to i
33f44 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b  ncrementally tok
33f45 65 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e  enize this strin
33f46 67 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  g is returned in
33f47 20 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a   .** *ppCursor..
33f48 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69  */.static int si
33f49 6d 70 6c 65 4f 70 65 6e 28 0a 20 20 73 71 6c 69  mpleOpen(.  sqli
33f4a 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
33f4b 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20  Tokenizer,      
33f4c 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69     /* The tokeni
33f4d 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  zer */.  const c
33f4e 68 61 72 20 2a 70 49 6e 70 75 74 2c 20 69 6e 74  har *pInput, int
33f4f 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
33f50 2f 2a 20 53 74 72 69 6e 67 20 74 6f 20 62 65 20  /* String to be 
33f51 74 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20 73  tokenized */.  s
33f52 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
33f53 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
33f54 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f  or    /* OUT: To
33f55 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
33f56 72 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c 65  r */.){.  simple
33f57 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
33f58 72 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 73 69  r *c;..  c = (si
33f59 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  mple_tokenizer_c
33f5a 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33  ursor *) sqlite3
33f5b 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a  _malloc(sizeof(*
33f5c 63 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e 55  c));.  if( c==NU
33f5d 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  LL ) return SQLI
33f5e 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e  TE_NOMEM;..  c->
33f5f 70 49 6e 70 75 74 20 3d 20 70 49 6e 70 75 74 3b  pInput = pInput;
33f60 0a 20 20 69 66 28 20 70 49 6e 70 75 74 3d 3d 30  .  if( pInput==0
33f61 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74 65   ){.    c->nByte
33f62 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  s = 0;.  }else i
33f63 66 28 20 6e 42 79 74 65 73 3c 30 20 29 7b 0a 20  f( nBytes<0 ){. 
33f64 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 28     c->nBytes = (
33f65 69 6e 74 29 73 74 72 6c 65 6e 28 70 49 6e 70 75  int)strlen(pInpu
33f66 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
33f67 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 6e 42 79   c->nBytes = nBy
33f68 74 65 73 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f  tes;.  }.  c->iO
33f69 66 66 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20  ffset = 0;      
33f6a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74             /* st
33f6b 61 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61  art tokenizing a
33f6c 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
33f6d 2a 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d  */.  c->iToken =
33f6e 20 30 3b 0a 20 20 63 2d 3e 70 54 6f 6b 65 6e 20   0;.  c->pToken 
33f6f 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 20  = NULL;         
33f70 20 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61 63        /* no spac
33f71 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65 74  e allocated, yet
33f72 2e 20 2a 2f 0a 20 20 63 2d 3e 6e 54 6f 6b 65 6e  . */.  c->nToken
33f73 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b 0a 0a  Allocated = 0;..
33f74 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 26 63    *ppCursor = &c
33f75 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e  ->base;.  return
33f76 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
33f77 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b  *.** Close a tok
33f78 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
33f79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
33f7a 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ed by a call to.
33f7b 2a 2a 20 73 69 6d 70 6c 65 4f 70 65 6e 28 29 20  ** simpleOpen() 
33f7c 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  above..*/.static
33f7d 20 69 6e 74 20 73 69 6d 70 6c 65 43 6c 6f 73 65   int simpleClose
33f7e 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
33f7f 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
33f80 6f 72 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f  or){.  simple_to
33f81 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
33f82 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65  c = (simple_toke
33f83 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20  nizer_cursor *) 
33f84 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74  pCursor;.  sqlit
33f85 65 33 5f 66 72 65 65 28 63 2d 3e 70 54 6f 6b 65  e3_free(c->pToke
33f86 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  n);.  sqlite3_fr
33f87 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(c);.  return 
33f88 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
33f89 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
33f8a 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  next token from 
33f8b 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  a tokenization c
33f8c 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73  ursor.  The curs
33f8d 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20  or must.** have 
33f8e 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61  been opened by a
33f8f 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73   prior call to s
33f90 69 6d 70 6c 65 4f 70 65 6e 28 29 2e 0a 2a 2f 0a  impleOpen()..*/.
33f91 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c  static int simpl
33f92 65 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33  eNext(.  sqlite3
33f93 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
33f94 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20  r *pCursor,  /* 
33f95 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20  Cursor returned 
33f96 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f  by simpleOpen */
33f97 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
33f98 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20  ppToken,        
33f99 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a         /* OUT: *
33f9a 70 70 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74  ppToken is the t
33f9b 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69  oken text */.  i
33f9c 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20  nt *pnBytes,    
33f9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f9e 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
33f9f 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f  r of bytes in to
33fa0 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
33fa1 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20  StartOffset,    
33fa2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33fa3 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66  OUT: Starting of
33fa4 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
33fa5 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66  .  int *piEndOff
33fa6 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
33fa7 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
33fa8 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  nding offset of 
33fa9 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
33faa 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20  piPosition      
33fab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33fac 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20  * OUT: Position 
33fad 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e  integer of token
33fae 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c 65 5f   */.){.  simple_
33faf 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
33fb0 20 2a 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f   *c = (simple_to
33fb1 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
33fb2 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 69 6d  ) pCursor;.  sim
33fb3 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74  ple_tokenizer *t
33fb4 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e   = (simple_token
33fb5 69 7a 65 72 20 2a 29 20 70 43 75 72 73 6f 72 2d  izer *) pCursor-
33fb6 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 75  >pTokenizer;.  u
33fb7 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 20  nsigned char *p 
33fb8 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
33fb9 20 2a 29 63 2d 3e 70 49 6e 70 75 74 3b 0a 0a 20   *)c->pInput;.. 
33fba 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73   while( c->iOffs
33fbb 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 29 7b 0a  et<c->nBytes ){.
33fbc 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f 66      int iStartOf
33fbd 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 63  fset;..    /* Sc
33fbe 61 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74 65  an past delimite
33fbf 72 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a  r characters */.
33fc0 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f      while( c->iO
33fc1 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20  ffset<c->nBytes 
33fc2 26 26 20 73 69 6d 70 6c 65 44 65 6c 69 6d 28 74  && simpleDelim(t
33fc3 2c 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29  , p[c->iOffset])
33fc4 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66   ){.      c->iOf
33fc5 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20  fset++;.    }.. 
33fc6 20 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e 2d     /* Count non-
33fc7 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63  delimiter charac
33fc8 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53 74  ters. */.    iSt
33fc9 61 72 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69  artOffset = c->i
33fca 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c  Offset;.    whil
33fcb 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
33fcc 3e 6e 42 79 74 65 73 20 26 26 20 21 73 69 6d 70  >nBytes && !simp
33fcd 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e  leDelim(t, p[c->
33fce 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20  iOffset]) ){.   
33fcf 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b     c->iOffset++;
33fd0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
33fd1 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74 61 72  c->iOffset>iStar
33fd2 74 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20  tOffset ){.     
33fd3 20 69 6e 74 20 69 2c 20 6e 20 3d 20 63 2d 3e 69   int i, n = c->i
33fd4 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66  Offset-iStartOff
33fd5 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  set;.      if( n
33fd6 3e 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61  >c->nTokenAlloca
33fd7 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ted ){.        c
33fd8 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65  ->nTokenAllocate
33fd9 64 20 3d 20 6e 2b 32 30 3b 0a 20 20 20 20 20 20  d = n+20;.      
33fda 20 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20 73 71    c->pToken = sq
33fdb 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 63 2d  lite3_realloc(c-
33fdc 3e 70 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 54 6f 6b  >pToken, c->nTok
33fdd 65 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20 20  enAllocated);.  
33fde 20 20 20 20 20 20 69 66 28 20 63 2d 3e 70 54 6f        if( c->pTo
33fdf 6b 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ken==NULL ) retu
33fe0 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
33fe1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
33fe2 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b  or(i=0; i<n; i++
33fe3 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 4f  ){.        /* TO
33fe4 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 6e  DO(shess) This n
33fe5 65 65 64 73 20 65 78 70 61 6e 73 69 6f 6e 20 74  eeds expansion t
33fe6 6f 20 68 61 6e 64 6c 65 20 55 54 46 2d 38 0a 20  o handle UTF-8. 
33fe7 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 2d 69         ** case-i
33fe8 6e 73 65 6e 73 69 74 69 76 69 74 79 2e 0a 20 20  nsensitivity..  
33fe9 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
33fea 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
33feb 68 20 3d 20 70 5b 69 53 74 61 72 74 4f 66 66 73  h = p[iStartOffs
33fec 65 74 2b 69 5d 3b 0a 20 20 20 20 20 20 20 20 63  et+i];.        c
33fed 2d 3e 70 54 6f 6b 65 6e 5b 69 5d 20 3d 20 63 68  ->pToken[i] = ch
33fee 3c 30 78 38 30 20 3f 20 74 6f 6c 6f 77 65 72 28  <0x80 ? tolower(
33fef 63 68 29 20 3a 20 63 68 3b 0a 20 20 20 20 20 20  ch) : ch;.      
33ff0 7d 0a 20 20 20 20 20 20 2a 70 70 54 6f 6b 65 6e  }.      *ppToken
33ff1 20 3d 20 63 2d 3e 70 54 6f 6b 65 6e 3b 0a 20 20   = c->pToken;.  
33ff2 20 20 20 20 2a 70 6e 42 79 74 65 73 20 3d 20 6e      *pnBytes = n
33ff3 3b 0a 20 20 20 20 20 20 2a 70 69 53 74 61 72 74  ;.      *piStart
33ff4 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72 74 4f  Offset = iStartO
33ff5 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69  ffset;.      *pi
33ff6 45 6e 64 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69  EndOffset = c->i
33ff7 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70  Offset;.      *p
33ff8 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63 2d 3e 69  iPosition = c->i
33ff9 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 20 20 20 20  Token++;..      
33ffa 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
33ffb 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
33ffc 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
33ffd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
33ffe 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74  et of routines t
33fff 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  hat implement th
34000 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a  e simple tokeniz
34001 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  er.*/.static con
34002 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
34003 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73 69 6d 70  izer_module simp
34004 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
34005 65 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 73 69 6d  e = {.  0,.  sim
34006 70 6c 65 43 72 65 61 74 65 2c 0a 20 20 73 69 6d  pleCreate,.  sim
34007 70 6c 65 44 65 73 74 72 6f 79 2c 0a 20 20 73 69  pleDestroy,.  si
34008 6d 70 6c 65 4f 70 65 6e 2c 0a 20 20 73 69 6d 70  mpleOpen,.  simp
34009 6c 65 43 6c 6f 73 65 2c 0a 20 20 73 69 6d 70 6c  leClose,.  simpl
3400a 65 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  eNext,.};../*.**
3400b 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
3400c 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72  simple tokenizer
3400d 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
3400e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a  ter to the new.*
3400f 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20 2a  * tokenizer in *
34010 70 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51 4c 49  ppModule.*/.SQLI
34011 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
34012 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c  sqlite3Fts3Simpl
34013 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  eTokenizerModule
34014 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
34015 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
34016 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a  st**ppModule.){.
34017 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 73    *ppModule = &s
34018 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
34019 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  dule;.}..#endif 
3401a 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
3401b 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
3401c 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
3401d 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a  E_FTS3) */../***
3401e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
3401f 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  of fts3_tokenize
34020 72 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r1.c ***********
34021 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34022 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
34023 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
34024 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 63 20 2a  n file rtree.c *
34025 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34027 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
34028 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
34029 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
3402a 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
3402b 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
3402c 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
3402d 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
3402e 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
3402f 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
34030 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
34031 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
34032 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
34033 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
34034 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
34035 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
34036 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
34037 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
34038 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
34039 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
3403a 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
3403b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3403c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3403d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3403e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3403f 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
34040 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f  contains code fo
34041 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  r implementation
34042 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  s of the r-tree 
34043 61 6e 64 20 72 2a 2d 74 72 65 65 0a 2a 2a 20 61  and r*-tree.** a
34044 6c 67 6f 72 69 74 68 6d 73 20 70 61 63 6b 61 67  lgorithms packag
34045 65 64 20 61 73 20 61 6e 20 53 51 4c 69 74 65 20  ed as an SQLite 
34046 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
34047 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  dule..**.** $Id:
34048 20 72 74 72 65 65 2e 63 2c 76 20 31 2e 31 32 20   rtree.c,v 1.12 
34049 32 30 30 38 2f 31 32 2f 32 32 20 31 35 3a 30 34  2008/12/22 15:04
3404a 3a 33 32 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :32 danielk1977 
3404b 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64  Exp $.*/..#if !d
3404c 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
3404d 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
3404e 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52  QLITE_ENABLE_RTR
3404f 45 45 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EE)../*.** This 
34050 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e  file contains an
34051 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
34052 6f 66 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64  of a couple of d
34053 69 66 66 65 72 65 6e 74 20 76 61 72 69 61 6e 74  ifferent variant
34054 73 0a 2a 2a 20 6f 66 20 74 68 65 20 72 2d 74 72  s.** of the r-tr
34055 65 65 20 61 6c 67 6f 72 69 74 68 6d 2e 20 53 65  ee algorithm. Se
34056 65 20 74 68 65 20 52 45 41 44 4d 45 20 66 69 6c  e the README fil
34057 65 20 66 6f 72 20 66 75 72 74 68 65 72 20 64 65  e for further de
34058 74 61 69 6c 73 2e 20 54 68 65 20 0a 2a 2a 20 73  tails. The .** s
34059 61 6d 65 20 64 61 74 61 2d 73 74 72 75 63 74 75  ame data-structu
3405a 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 61  re is used for a
3405b 6c 6c 2c 20 62 75 74 20 74 68 65 20 61 6c 67 6f  ll, but the algo
3405c 72 69 74 68 6d 73 20 66 6f 72 20 69 6e 73 65 72  rithms for inser
3405d 74 20 61 6e 64 0a 2a 2a 20 64 65 6c 65 74 65 20  t and.** delete 
3405e 6f 70 65 72 61 74 69 6f 6e 73 20 76 61 72 79 2e  operations vary.
3405f 20 54 68 65 20 76 61 72 69 61 6e 74 73 20 75 73   The variants us
34060 65 64 20 61 72 65 20 73 65 6c 65 63 74 65 64 20  ed are selected 
34061 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  at compile time 
34062 0a 2a 2a 20 62 79 20 64 65 66 69 6e 69 6e 67 20  .** by defining 
34063 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79  the following sy
34064 6d 62 6f 6c 73 3a 0a 2a 2f 0a 0a 2f 2a 20 45 69  mbols:.*/../* Ei
34065 74 68 65 72 2c 20 62 6f 74 68 20 6f 72 20 6e 6f  ther, both or no
34066 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
34067 69 6e 67 20 6d 61 79 20 62 65 20 73 65 74 20 74  ing may be set t
34068 6f 20 61 63 74 69 76 61 74 65 20 0a 2a 2a 20 72  o activate .** r
34069 2a 74 72 65 65 20 76 61 72 69 61 6e 74 20 61 6c  *tree variant al
3406a 67 6f 72 69 74 68 6d 73 2e 0a 2a 2f 0a 23 64 65  gorithms..*/.#de
3406b 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54  fine VARIANT_RST
3406c 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42  ARTREE_CHOOSESUB
3406d 54 52 45 45 20 30 0a 23 64 65 66 69 6e 65 20 56  TREE 0.#define V
3406e 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
3406f 5f 52 45 49 4e 53 45 52 54 20 20 20 20 20 20 31  _REINSERT      1
34070 0a 0a 2f 2a 20 0a 2a 2a 20 45 78 61 63 74 6c 79  ../* .** Exactly
34071 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
34072 6f 77 69 6e 67 20 6d 75 73 74 20 62 65 20 73 65  owing must be se
34073 74 20 74 6f 20 31 2e 0a 2a 2f 0a 23 64 65 66 69  t to 1..*/.#defi
34074 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  ne VARIANT_GUTTM
34075 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c  AN_QUADRATIC_SPL
34076 49 54 20 30 0a 23 64 65 66 69 6e 65 20 56 41 52  IT 0.#define VAR
34077 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
34078 45 41 52 5f 53 50 4c 49 54 20 20 20 20 30 0a 23  EAR_SPLIT    0.#
34079 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52  define VARIANT_R
3407a 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54 20 20  STARTREE_SPLIT  
3407b 20 20 20 20 20 20 20 31 0a 0a 23 64 65 66 69 6e         1..#defin
3407c 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  e VARIANT_GUTTMA
3407d 4e 5f 53 50 4c 49 54 20 5c 0a 20 20 20 20 20 20  N_SPLIT \.      
3407e 20 20 28 56 41 52 49 41 4e 54 5f 47 55 54 54 4d    (VARIANT_GUTTM
3407f 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 7c  AN_LINEAR_SPLIT|
34080 7c 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e  |VARIANT_GUTTMAN
34081 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54  _QUADRATIC_SPLIT
34082 29 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  )..#if VARIANT_G
34083 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
34084 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e 65  _SPLIT.  #define
34085 20 50 69 63 6b 4e 65 78 74 20 51 75 61 64 72 61   PickNext Quadra
34086 74 69 63 50 69 63 6b 4e 65 78 74 0a 20 20 23 64  ticPickNext.  #d
34087 65 66 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20  efine PickSeeds 
34088 51 75 61 64 72 61 74 69 63 50 69 63 6b 53 65 65  QuadraticPickSee
34089 64 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73  ds.  #define Ass
3408a 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f  ignCells splitNo
3408b 64 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66  deGuttman.#endif
3408c 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54  .#if VARIANT_GUT
3408d 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49  TMAN_LINEAR_SPLI
3408e 54 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  T.  #define Pick
3408f 4e 65 78 74 20 4c 69 6e 65 61 72 50 69 63 6b 4e  Next LinearPickN
34090 65 78 74 0a 20 20 23 64 65 66 69 6e 65 20 50 69  ext.  #define Pi
34091 63 6b 53 65 65 64 73 20 4c 69 6e 65 61 72 50 69  ckSeeds LinearPi
34092 63 6b 53 65 65 64 73 0a 20 20 23 64 65 66 69 6e  ckSeeds.  #defin
34093 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70  e AssignCells sp
34094 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0a 23  litNodeGuttman.#
34095 65 6e 64 69 66 0a 23 69 66 20 56 41 52 49 41 4e  endif.#if VARIAN
34096 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49  T_RSTARTREE_SPLI
34097 54 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69  T.  #define Assi
34098 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64  gnCells splitNod
34099 65 53 74 61 72 74 72 65 65 0a 23 65 6e 64 69 66  eStartree.#endif
3409a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
3409b 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f  E_CORE.  SQLITE_
3409c 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a  EXTENSION_INIT1.
3409d 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 0a 23  #else.#endif...#
3409e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  ifndef SQLITE_AM
3409f 41 4c 47 41 4d 41 54 49 4f 4e 0a 74 79 70 65 64  ALGAMATION.typed
340a0 65 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  ef sqlite3_int64
340a1 20 69 36 34 3b 0a 74 79 70 65 64 65 66 20 75 6e   i64;.typedef un
340a2 73 69 67 6e 65 64 20 63 68 61 72 20 75 38 3b 0a  signed char u8;.
340a3 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
340a4 20 69 6e 74 20 75 33 32 3b 0a 23 65 6e 64 69 66   int u32;.#endif
340a5 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
340a6 20 52 74 72 65 65 20 52 74 72 65 65 3b 0a 74 79   Rtree Rtree;.ty
340a7 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72  pedef struct Rtr
340a8 65 65 43 75 72 73 6f 72 20 52 74 72 65 65 43 75  eeCursor RtreeCu
340a9 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74  rsor;.typedef st
340aa 72 75 63 74 20 52 74 72 65 65 4e 6f 64 65 20 52  ruct RtreeNode R
340ab 74 72 65 65 4e 6f 64 65 3b 0a 74 79 70 65 64 65  treeNode;.typede
340ac 66 20 73 74 72 75 63 74 20 52 74 72 65 65 43 65  f struct RtreeCe
340ad 6c 6c 20 52 74 72 65 65 43 65 6c 6c 3b 0a 74 79  ll RtreeCell;.ty
340ae 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72  pedef struct Rtr
340af 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 52 74 72  eeConstraint Rtr
340b0 65 65 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 74 79  eeConstraint;.ty
340b1 70 65 64 65 66 20 75 6e 69 6f 6e 20 52 74 72 65  pedef union Rtre
340b2 65 43 6f 6f 72 64 20 52 74 72 65 65 43 6f 6f 72  eCoord RtreeCoor
340b3 64 3b 0a 0a 2f 2a 20 54 68 65 20 72 74 72 65 65  d;../* The rtree
340b4 20 6d 61 79 20 68 61 76 65 20 62 65 74 77 65 65   may have betwee
340b5 6e 20 31 20 61 6e 64 20 52 54 52 45 45 5f 4d 41  n 1 and RTREE_MA
340b6 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 64 69 6d  X_DIMENSIONS dim
340b7 65 6e 73 69 6f 6e 73 2e 20 2a 2f 0a 23 64 65 66  ensions. */.#def
340b8 69 6e 65 20 52 54 52 45 45 5f 4d 41 58 5f 44 49  ine RTREE_MAX_DI
340b9 4d 45 4e 53 49 4f 4e 53 20 35 0a 0a 2f 2a 20 53  MENSIONS 5../* S
340ba 69 7a 65 20 6f 66 20 68 61 73 68 20 74 61 62 6c  ize of hash tabl
340bb 65 20 52 74 72 65 65 2e 61 48 61 73 68 2e 20 54  e Rtree.aHash. T
340bc 68 69 73 20 68 61 73 68 20 74 61 62 6c 65 20 69  his hash table i
340bd 73 20 6e 6f 74 20 65 78 70 65 63 74 65 64 20 74  s not expected t
340be 6f 0a 2a 2a 20 65 76 65 72 20 63 6f 6e 74 61 69  o.** ever contai
340bf 6e 20 76 65 72 79 20 6d 61 6e 79 20 65 6e 74 72  n very many entr
340c0 69 65 73 2c 20 73 6f 20 61 20 66 69 78 65 64 20  ies, so a fixed 
340c1 6e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74  number of bucket
340c2 73 20 69 73 20 0a 2a 2a 20 75 73 65 64 2e 0a 2a  s is .** used..*
340c3 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48 53 49  /.#define HASHSI
340c4 5a 45 20 31 32 38 0a 0a 2f 2a 20 0a 2a 2a 20 41  ZE 128../* .** A
340c5 6e 20 72 74 72 65 65 20 76 69 72 74 75 61 6c 2d  n rtree virtual-
340c6 74 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  table object..*/
340c7 0a 73 74 72 75 63 74 20 52 74 72 65 65 20 7b 0a  .struct Rtree {.
340c8 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 62    sqlite3_vtab b
340c9 61 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ase;.  sqlite3 *
340ca 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
340cb 20 20 20 2f 2a 20 48 6f 73 74 20 64 61 74 61 62     /* Host datab
340cc 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
340cd 2f 0a 20 20 69 6e 74 20 69 4e 6f 64 65 53 69 7a  /.  int iNodeSiz
340ce 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
340cf 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65 73  /* Size in bytes
340d0 20 6f 66 20 65 61 63 68 20 6e 6f 64 65 20 69 6e   of each node in
340d1 20 74 68 65 20 6e 6f 64 65 20 74 61 62 6c 65 20   the node table 
340d2 2a 2f 0a 20 20 69 6e 74 20 6e 44 69 6d 3b 20 20  */.  int nDim;  
340d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
340d4 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 69   /* Number of di
340d5 6d 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  mensions */.  in
340d6 74 20 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b  t nBytesPerCell;
340d7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
340d8 65 73 20 63 6f 6e 73 75 6d 65 64 20 70 65 72 20  es consumed per 
340d9 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 44  cell */.  int iD
340da 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  epth;           
340db 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
340dc 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 72 2d   depth of the r-
340dd 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
340de 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  /.  char *zDb;  
340df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
340e0 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
340e1 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72  ase containing r
340e2 2d 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20  -tree table */. 
340e3 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
340e4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
340e5 4e 61 6d 65 20 6f 66 20 72 2d 74 72 65 65 20 74  Name of r-tree t
340e6 61 62 6c 65 20 2a 2f 20 0a 20 20 52 74 72 65 65  able */ .  Rtree
340e7 4e 6f 64 65 20 2a 61 48 61 73 68 5b 48 41 53 48  Node *aHash[HASH
340e8 53 49 5a 45 5d 3b 20 2f 2a 20 48 61 73 68 20 74  SIZE]; /* Hash t
340e9 61 62 6c 65 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72  able of in-memor
340ea 79 20 6e 6f 64 65 73 2e 20 2a 2f 20 0a 20 20 69  y nodes. */ .  i
340eb 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20  nt nBusy;       
340ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
340ed 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
340ee 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 73 74  users of this st
340ef 72 75 63 74 75 72 65 20 2a 2f 0a 0a 20 20 2f 2a  ructure */..  /*
340f0 20 4c 69 73 74 20 6f 66 20 6e 6f 64 65 73 20 72   List of nodes r
340f1 65 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 20  emoved during a 
340f2 43 6f 6e 64 65 6e 73 65 54 72 65 65 20 6f 70 65  CondenseTree ope
340f3 72 61 74 69 6f 6e 2e 20 4c 69 73 74 20 69 73 0a  ration. List is.
340f4 20 20 2a 2a 20 6c 69 6e 6b 65 64 20 74 6f 67 65    ** linked toge
340f5 74 68 65 72 20 76 69 61 20 74 68 65 20 70 6f 69  ther via the poi
340f6 6e 74 65 72 20 6e 6f 72 6d 61 6c 6c 79 20 75 73  nter normally us
340f7 65 64 20 66 6f 72 20 68 61 73 68 20 63 68 61 69  ed for hash chai
340f8 6e 73 20 2d 0a 20 20 2a 2a 20 52 74 72 65 65 4e  ns -.  ** RtreeN
340f9 6f 64 65 2e 70 4e 65 78 74 2e 20 52 74 72 65 65  ode.pNext. Rtree
340fa 4e 6f 64 65 2e 69 4e 6f 64 65 20 73 74 6f 72 65  Node.iNode store
340fb 73 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 74  s the depth of t
340fc 68 65 20 73 75 62 2d 74 72 65 65 20 0a 20 20 2a  he sub-tree .  *
340fd 2a 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20  * headed by the 
340fe 6e 6f 64 65 20 28 6c 65 61 66 20 6e 6f 64 65 73  node (leaf nodes
340ff 20 68 61 76 65 20 52 74 72 65 65 4e 6f 64 65 2e   have RtreeNode.
34100 69 4e 6f 64 65 3d 3d 30 29 2e 0a 20 20 2a 2f 0a  iNode==0)..  */.
34101 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 44 65    RtreeNode *pDe
34102 6c 65 74 65 64 3b 0a 20 20 69 6e 74 20 69 52 65  leted;.  int iRe
34103 69 6e 73 65 72 74 48 65 69 67 68 74 3b 20 20 20  insertHeight;   
34104 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f       /* Height o
34105 66 20 73 75 62 2d 74 72 65 65 73 20 52 65 69 6e  f sub-trees Rein
34106 73 65 72 74 28 29 20 68 61 73 20 72 75 6e 20 6f  sert() has run o
34107 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 53 74 61 74 65  n */..  /* State
34108 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72  ments to read/wr
34109 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63  ite/delete a rec
3410a 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 6e 6f 64  ord from xxx_nod
3410b 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  e */.  sqlite3_s
3410c 74 6d 74 20 2a 70 52 65 61 64 4e 6f 64 65 3b 0a  tmt *pReadNode;.
3410d 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
3410e 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20 73 71  pWriteNode;.  sq
3410f 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c  lite3_stmt *pDel
34110 65 74 65 4e 6f 64 65 3b 0a 0a 20 20 2f 2a 20 53  eteNode;..  /* S
34111 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61  tatements to rea
34112 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61  d/write/delete a
34113 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78   record from xxx
34114 5f 72 6f 77 69 64 20 2a 2f 0a 20 20 73 71 6c 69  _rowid */.  sqli
34115 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64 52  te3_stmt *pReadR
34116 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  owid;.  sqlite3_
34117 73 74 6d 74 20 2a 70 57 72 69 74 65 52 6f 77 69  stmt *pWriteRowi
34118 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  d;.  sqlite3_stm
34119 74 20 2a 70 44 65 6c 65 74 65 52 6f 77 69 64 3b  t *pDeleteRowid;
3411a 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74  ..  /* Statement
3411b 73 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f  s to read/write/
3411c 64 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20  delete a record 
3411d 66 72 6f 6d 20 78 78 78 5f 70 61 72 65 6e 74 20  from xxx_parent 
3411e 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
3411f 74 20 2a 70 52 65 61 64 50 61 72 65 6e 74 3b 0a  t *pReadParent;.
34120 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
34121 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20 20  pWriteParent;.  
34122 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44  sqlite3_stmt *pD
34123 65 6c 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20 20  eleteParent;..  
34124 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65 3b 0a  int eCoordType;.
34125 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65 20  };../* Possible 
34126 76 61 6c 75 65 73 20 66 6f 72 20 65 43 6f 6f 72  values for eCoor
34127 64 54 79 70 65 3a 20 2a 2f 0a 23 64 65 66 69 6e  dType: */.#defin
34128 65 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45  e RTREE_COORD_RE
34129 41 4c 33 32 20 30 0a 23 64 65 66 69 6e 65 20 52  AL32 0.#define R
3412a 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32  TREE_COORD_INT32
3412b 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d    1../*.** The m
3412c 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  inimum number of
3412d 20 63 65 6c 6c 73 20 61 6c 6c 6f 77 65 64 20 66   cells allowed f
3412e 6f 72 20 61 20 6e 6f 64 65 20 69 73 20 61 20 74  or a node is a t
3412f 68 69 72 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20  hird of the .** 
34130 6d 61 78 69 6d 75 6d 2e 20 49 6e 20 47 75 74 6d  maximum. In Gutm
34131 61 6e 27 73 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a  an's notation:.*
34132 2a 0a 2a 2a 20 20 20 20 20 6d 20 3d 20 4d 2f 33  *.**     m = M/3
34133 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 52 2a 2d  .**.** If an R*-
34134 74 72 65 65 20 22 52 65 69 6e 73 65 72 74 22 20  tree "Reinsert" 
34135 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71  operation is req
34136 75 69 72 65 64 2c 20 74 68 65 20 73 61 6d 65 20  uired, the same 
34137 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c  number of.** cel
34138 6c 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66  ls are removed f
34139 72 6f 6d 20 74 68 65 20 6f 76 65 72 66 75 6c 6c  rom the overfull
3413a 20 6e 6f 64 65 20 61 6e 64 20 72 65 69 6e 73 65   node and reinse
3413b 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72  rted into the tr
3413c 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ee..*/.#define R
3413d 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 29  TREE_MINCELLS(p)
3413e 20 28 28 28 28 70 29 2d 3e 69 4e 6f 64 65 53 69   ((((p)->iNodeSi
3413f 7a 65 2d 34 29 2f 28 70 29 2d 3e 6e 42 79 74 65  ze-4)/(p)->nByte
34140 73 50 65 72 43 65 6c 6c 29 2f 33 29 0a 23 64 65  sPerCell)/3).#de
34141 66 69 6e 65 20 52 54 52 45 45 5f 52 45 49 4e 53  fine RTREE_REINS
34142 45 52 54 28 70 29 20 52 54 52 45 45 5f 4d 49 4e  ERT(p) RTREE_MIN
34143 43 45 4c 4c 53 28 70 29 0a 23 64 65 66 69 6e 65  CELLS(p).#define
34144 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 20   RTREE_MAXCELLS 
34145 35 31 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74  51../* .** An rt
34146 72 65 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63  ree cursor objec
34147 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72  t..*/.struct Rtr
34148 65 65 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  eeCursor {.  sql
34149 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
3414a 20 62 61 73 65 3b 0a 20 20 52 74 72 65 65 4e 6f   base;.  RtreeNo
3414b 64 65 20 2a 70 4e 6f 64 65 3b 20 20 20 20 20 20  de *pNode;      
3414c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f             /* No
3414d 64 65 20 63 75 72 73 6f 72 20 69 73 20 63 75 72  de cursor is cur
3414e 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
3414f 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c  at */.  int iCel
34150 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
34151 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
34152 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 63 65  ex of current ce
34153 6c 6c 20 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a 20  ll in pNode */. 
34154 20 69 6e 74 20 69 53 74 72 61 74 65 67 79 3b 20   int iStrategy; 
34155 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34156 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 64     /* Copy of id
34157 78 4e 75 6d 20 73 65 61 72 63 68 20 70 61 72 61  xNum search para
34158 6d 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  meter */.  int n
34159 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20  Constraint;     
3415a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3415b 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
3415c 73 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74  s in aConstraint
3415d 20 2a 2f 0a 20 20 52 74 72 65 65 43 6f 6e 73 74   */.  RtreeConst
3415e 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69  raint *aConstrai
3415f 6e 74 3b 20 20 20 20 20 2f 2a 20 53 65 61 72 63  nt;     /* Searc
34160 68 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a  h constraints. *
34161 2f 0a 7d 3b 0a 0a 75 6e 69 6f 6e 20 52 74 72 65  /.};..union Rtre
34162 65 43 6f 6f 72 64 20 7b 0a 20 20 66 6c 6f 61 74  eCoord {.  float
34163 20 66 3b 0a 20 20 69 6e 74 20 69 3b 0a 7d 3b 0a   f;.  int i;.};.
34164 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  ./*.** The argum
34165 65 6e 74 20 69 73 20 61 6e 20 52 74 72 65 65 43  ent is an RtreeC
34166 6f 6f 72 64 2e 20 52 65 74 75 72 6e 20 74 68 65  oord. Return the
34167 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 77 69   value stored wi
34168 74 68 69 6e 20 74 68 65 20 52 74 72 65 65 43 6f  thin the RtreeCo
34169 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 74 65 64  ord.** formatted
3416a 20 61 73 20 61 20 64 6f 75 62 6c 65 2e 20 54 68   as a double. Th
3416b 69 73 20 6d 61 63 72 6f 20 61 73 73 75 6d 65 73  is macro assumes
3416c 20 74 68 61 74 20 6c 6f 63 61 6c 20 76 61 72 69   that local vari
3416d 61 62 6c 65 20 70 52 74 72 65 65 20 70 6f 69 6e  able pRtree poin
3416e 74 73 0a 2a 2a 20 74 6f 20 74 68 65 20 52 74 72  ts.** to the Rtr
3416f 65 65 20 73 74 72 75 63 74 75 72 65 20 61 73 73  ee structure ass
34170 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
34171 20 52 74 72 65 65 43 6f 6f 72 64 2e 0a 2a 2f 0a   RtreeCoord..*/.
34172 23 64 65 66 69 6e 65 20 44 43 4f 4f 52 44 28 63  #define DCOORD(c
34173 6f 6f 72 64 29 20 28 20 20 20 20 20 20 20 20 20  oord) (         
34174 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34175 20 20 5c 0a 20 20 28 70 52 74 72 65 65 2d 3e 65    \.  (pRtree->e
34176 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45  CoordType==RTREE
34177 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 29 20 3f  _COORD_REAL32) ?
34178 20 20 20 20 20 20 5c 0a 20 20 20 20 28 28 64 6f        \.    ((do
34179 75 62 6c 65 29 63 6f 6f 72 64 2e 66 29 20 3a 20  uble)coord.f) : 
3417a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3417b 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
3417c 28 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64 2e 69  ((double)coord.i
3417d 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
3417e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
3417f 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 65 61 72 63  )../*.** A searc
34180 68 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f  h constraint..*/
34181 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e  .struct RtreeCon
34182 73 74 72 61 69 6e 74 20 7b 0a 20 20 69 6e 74 20  straint {.  int 
34183 69 43 6f 6f 72 64 3b 20 20 20 20 20 20 20 20 20  iCoord;         
34184 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34185 20 49 6e 64 65 78 20 6f 66 20 63 6f 6e 73 74 72   Index of constr
34186 61 69 6e 65 64 20 63 6f 6f 72 64 69 6e 61 74 65  ained coordinate
34187 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20   */.  int op;   
34188 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34189 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74          /* Const
3418a 72 61 69 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f  raining operatio
3418b 6e 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 56  n */.  double rV
3418c 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20 20 20  alue;           
3418d 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73           /* Cons
3418e 74 72 61 69 6e 74 20 76 61 6c 75 65 2e 20 2a 2f  traint value. */
3418f 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65  .};../* Possible
34190 20 76 61 6c 75 65 73 20 66 6f 72 20 52 74 72 65   values for Rtre
34191 65 43 6f 6e 73 74 72 61 69 6e 74 2e 6f 70 20 2a  eConstraint.op *
34192 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
34193 45 51 20 30 78 34 31 0a 23 64 65 66 69 6e 65 20  EQ 0x41.#define 
34194 52 54 52 45 45 5f 4c 45 20 30 78 34 32 0a 23 64  RTREE_LE 0x42.#d
34195 65 66 69 6e 65 20 52 54 52 45 45 5f 4c 54 20 30  efine RTREE_LT 0
34196 78 34 33 0a 23 64 65 66 69 6e 65 20 52 54 52 45  x43.#define RTRE
34197 45 5f 47 45 20 30 78 34 34 0a 23 64 65 66 69 6e  E_GE 0x44.#defin
34198 65 20 52 54 52 45 45 5f 47 54 20 30 78 34 35 0a  e RTREE_GT 0x45.
34199 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65  ./* .** An rtree
3419a 20 73 74 72 75 63 74 75 72 65 20 6e 6f 64 65 2e   structure node.
3419b 0a 2a 2a 0a 2a 2a 20 44 61 74 61 20 66 6f 72 6d  .**.** Data form
3419c 61 74 20 28 52 74 72 65 65 4e 6f 64 65 2e 7a 44  at (RtreeNode.zD
3419d 61 74 61 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e  ata):.**.**   1.
3419e 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20   If the node is 
3419f 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 28 6e  the root node (n
341a0 6f 64 65 20 31 29 2c 20 74 68 65 6e 20 74 68 65  ode 1), then the
341a1 20 66 69 72 73 74 20 32 20 62 79 74 65 73 0a 2a   first 2 bytes.*
341a2 2a 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 6f  *      of the no
341a3 64 65 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74  de contain the t
341a4 72 65 65 20 64 65 70 74 68 20 61 73 20 61 20 62  ree depth as a b
341a5 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
341a6 72 2e 0a 2a 2a 20 20 20 20 20 20 46 6f 72 20 6e  r..**      For n
341a7 6f 6e 2d 72 6f 6f 74 20 6e 6f 64 65 73 2c 20 74  on-root nodes, t
341a8 68 65 20 66 69 72 73 74 20 32 20 62 79 74 65 73  he first 2 bytes
341a9 20 61 72 65 20 6c 65 66 74 20 75 6e 75 73 65 64   are left unused
341aa 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65  ..**.**   2. The
341ab 20 6e 65 78 74 20 32 20 62 79 74 65 73 20 63 6f   next 2 bytes co
341ac 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72  ntain the number
341ad 20 6f 66 20 65 6e 74 72 69 65 73 20 63 75 72 72   of entries curr
341ae 65 6e 74 6c 79 20 0a 2a 2a 20 20 20 20 20 20 73  ently .**      s
341af 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 6f 64  tored in the nod
341b0 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 54 68  e..**.**   3. Th
341b1 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74  e remainder of t
341b2 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 73  he node contains
341b3 20 74 68 65 20 6e 6f 64 65 20 65 6e 74 72 69 65   the node entrie
341b4 73 2e 20 45 61 63 68 20 65 6e 74 72 79 0a 2a 2a  s. Each entry.**
341b5 20 20 20 20 20 20 63 6f 6e 73 69 73 74 73 20 6f        consists o
341b6 66 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 79 74  f a single 8-byt
341b7 65 20 69 6e 74 65 67 65 72 20 66 6f 6c 6c 6f 77  e integer follow
341b8 65 64 20 62 79 20 61 6e 20 65 76 65 6e 20 6e 75  ed by an even nu
341b9 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 6f 66 20  mber.**      of 
341ba 34 2d 62 79 74 65 20 63 6f 6f 72 64 69 6e 61 74  4-byte coordinat
341bb 65 73 2e 20 46 6f 72 20 6c 65 61 66 20 6e 6f 64  es. For leaf nod
341bc 65 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 69  es the integer i
341bd 73 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 20  s the rowid.**  
341be 20 20 20 20 6f 66 20 61 20 72 65 63 6f 72 64 2e      of a record.
341bf 20 46 6f 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   For internal no
341c0 64 65 73 20 69 74 20 69 73 20 74 68 65 20 6e 6f  des it is the no
341c1 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 0a 2a  de number of a.*
341c2 2a 20 20 20 20 20 20 63 68 69 6c 64 20 70 61 67  *      child pag
341c3 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72  e..*/.struct Rtr
341c4 65 65 4e 6f 64 65 20 7b 0a 20 20 52 74 72 65 65  eeNode {.  Rtree
341c5 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b 20 20  Node *pParent;  
341c6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
341c7 50 61 72 65 6e 74 20 6e 6f 64 65 20 2a 2f 0a 20  Parent node */. 
341c8 20 69 36 34 20 69 4e 6f 64 65 3b 0a 20 20 69 6e   i64 iNode;.  in
341c9 74 20 6e 52 65 66 3b 0a 20 20 69 6e 74 20 69 73  t nRef;.  int is
341ca 44 69 72 74 79 3b 0a 20 20 75 38 20 2a 7a 44 61  Dirty;.  u8 *zDa
341cb 74 61 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ta;.  RtreeNode 
341cc 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
341cd 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
341ce 6e 6f 64 65 20 69 6e 20 74 68 69 73 20 68 61 73  node in this has
341cf 68 20 63 68 61 69 6e 20 2a 2f 0a 7d 3b 0a 23 64  h chain */.};.#d
341d0 65 66 69 6e 65 20 4e 43 45 4c 4c 28 70 4e 6f 64  efine NCELL(pNod
341d1 65 29 20 72 65 61 64 49 6e 74 31 36 28 26 28 70  e) readInt16(&(p
341d2 4e 6f 64 65 29 2d 3e 7a 44 61 74 61 5b 32 5d 29  Node)->zData[2])
341d3 0a 0a 2f 2a 20 0a 2a 2a 20 53 74 72 75 63 74 75  ../* .** Structu
341d4 72 65 20 74 6f 20 73 74 6f 72 65 20 61 20 64 65  re to store a de
341d5 73 65 72 69 61 6c 69 7a 65 64 20 72 74 72 65 65  serialized rtree
341d6 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74 72 75   record..*/.stru
341d7 63 74 20 52 74 72 65 65 43 65 6c 6c 20 7b 0a 20  ct RtreeCell {. 
341d8 20 69 36 34 20 69 52 6f 77 69 64 3b 0a 20 20 52   i64 iRowid;.  R
341d9 74 72 65 65 43 6f 6f 72 64 20 61 43 6f 6f 72 64  treeCoord aCoord
341da 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  [RTREE_MAX_DIMEN
341db 53 49 4f 4e 53 2a 32 5d 3b 0a 7d 3b 0a 0a 23 69  SIONS*2];.};..#i
341dc 66 6e 64 65 66 20 4d 41 58 0a 23 20 64 65 66 69  fndef MAX.# defi
341dd 6e 65 20 4d 41 58 28 78 2c 79 29 20 28 28 78 29  ne MAX(x,y) ((x)
341de 20 3c 20 28 79 29 20 3f 20 28 79 29 20 3a 20 28   < (y) ? (y) : (
341df 78 29 29 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  x)).#endif.#ifnd
341e0 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20  ef MIN.# define 
341e1 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 20 3e 20  MIN(x,y) ((x) > 
341e2 28 79 29 20 3f 20 28 79 29 20 3a 20 28 78 29 29  (y) ? (y) : (x))
341e3 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46  .#endif../*.** F
341e4 75 6e 63 74 69 6f 6e 73 20 74 6f 20 64 65 73 65  unctions to dese
341e5 72 69 61 6c 69 7a 65 20 61 20 31 36 20 62 69 74  rialize a 16 bit
341e6 20 69 6e 74 65 67 65 72 2c 20 33 32 20 62 69 74   integer, 32 bit
341e7 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64   real number and
341e8 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e 74 65 67  .** 64 bit integ
341e9 65 72 2e 20 54 68 65 20 64 65 73 65 72 69 61 6c  er. The deserial
341ea 69 7a 65 64 20 76 61 6c 75 65 20 69 73 20 72 65  ized value is re
341eb 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
341ec 63 20 69 6e 74 20 72 65 61 64 49 6e 74 31 36 28  c int readInt16(
341ed 75 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  u8 *p){.  return
341ee 20 28 70 5b 30 5d 3c 3c 38 29 20 2b 20 70 5b 31   (p[0]<<8) + p[1
341ef 5d 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  ];.}.static void
341f0 20 72 65 61 64 43 6f 6f 72 64 28 75 38 20 2a 70   readCoord(u8 *p
341f1 2c 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43  , RtreeCoord *pC
341f2 6f 6f 72 64 29 7b 0a 20 20 75 33 32 20 69 20 3d  oord){.  u32 i =
341f3 20 28 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b   (.    (((u32)p[
341f4 30 5d 29 20 3c 3c 20 32 34 29 20 2b 20 0a 20 20  0]) << 24) + .  
341f5 20 20 28 28 28 75 33 32 29 70 5b 31 5d 29 20 3c    (((u32)p[1]) <
341f6 3c 20 31 36 29 20 2b 20 0a 20 20 20 20 28 28 28  < 16) + .    (((
341f7 75 33 32 29 70 5b 32 5d 29 20 3c 3c 20 20 38 29  u32)p[2]) <<  8)
341f8 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
341f9 5b 33 5d 29 20 3c 3c 20 20 30 29 0a 20 20 29 3b  [3]) <<  0).  );
341fa 0a 20 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f 72  .  *(u32 *)pCoor
341fb 64 20 3d 20 69 3b 0a 7d 0a 73 74 61 74 69 63 20  d = i;.}.static 
341fc 69 36 34 20 72 65 61 64 49 6e 74 36 34 28 75 38  i64 readInt64(u8
341fd 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28   *p){.  return (
341fe 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 30 5d  .    (((i64)p[0]
341ff 29 20 3c 3c 20 35 36 29 20 2b 20 0a 20 20 20 20  ) << 56) + .    
34200 28 28 28 69 36 34 29 70 5b 31 5d 29 20 3c 3c 20  (((i64)p[1]) << 
34201 34 38 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  48) + .    (((i6
34202 34 29 70 5b 32 5d 29 20 3c 3c 20 34 30 29 20 2b  4)p[2]) << 40) +
34203 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 33   .    (((i64)p[3
34204 5d 29 20 3c 3c 20 33 32 29 20 2b 20 0a 20 20 20  ]) << 32) + .   
34205 20 28 28 28 69 36 34 29 70 5b 34 5d 29 20 3c 3c   (((i64)p[4]) <<
34206 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28 69   24) + .    (((i
34207 36 34 29 70 5b 35 5d 29 20 3c 3c 20 31 36 29 20  64)p[5]) << 16) 
34208 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
34209 36 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20 20  6]) <<  8) + .  
3420a 20 20 28 28 28 69 36 34 29 70 5b 37 5d 29 20 3c    (((i64)p[7]) <
3420b 3c 20 20 30 29 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a  <  0).  );.}../*
3420c 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74 6f  .** Functions to
3420d 20 73 65 72 69 61 6c 69 7a 65 20 61 20 31 36 20   serialize a 16 
3420e 62 69 74 20 69 6e 74 65 67 65 72 2c 20 33 32 20  bit integer, 32 
3420f 62 69 74 20 72 65 61 6c 20 6e 75 6d 62 65 72 20  bit real number 
34210 61 6e 64 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e  and.** 64 bit in
34211 74 65 67 65 72 2e 20 54 68 65 20 76 61 6c 75 65  teger. The value
34212 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
34213 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
34214 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 74   written.** to t
34215 68 65 20 61 72 67 75 6d 65 6e 74 20 62 75 66 66  he argument buff
34216 65 72 20 28 61 6c 77 61 79 73 20 32 2c 20 34 20  er (always 2, 4 
34217 61 6e 64 20 38 20 72 65 73 70 65 63 74 69 76 65  and 8 respective
34218 6c 79 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ly)..*/.static i
34219 6e 74 20 77 72 69 74 65 49 6e 74 31 36 28 75 38  nt writeInt16(u8
3421a 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 70   *p, int i){.  p
3421b 5b 30 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [0] = (i>> 8)&0x
3421c 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 69 3e  FF;.  p[1] = (i>
3421d 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
3421e 75 72 6e 20 32 3b 0a 7d 0a 73 74 61 74 69 63 20  urn 2;.}.static 
3421f 69 6e 74 20 77 72 69 74 65 43 6f 6f 72 64 28 75  int writeCoord(u
34220 38 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f 72 64  8 *p, RtreeCoord
34221 20 2a 70 43 6f 6f 72 64 29 7b 0a 20 20 75 33 32   *pCoord){.  u32
34222 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   i;.  assert( si
34223 7a 65 6f 66 28 52 74 72 65 65 43 6f 6f 72 64 29  zeof(RtreeCoord)
34224 3d 3d 34 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==4 );.  assert(
34225 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20   sizeof(u32)==4 
34226 29 3b 0a 20 20 69 20 3d 20 2a 28 75 33 32 20 2a  );.  i = *(u32 *
34227 29 70 43 6f 6f 72 64 3b 0a 20 20 70 5b 30 5d 20  )pCoord;.  p[0] 
34228 3d 20 28 69 3e 3e 32 34 29 26 30 78 46 46 3b 0a  = (i>>24)&0xFF;.
34229 20 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 31 36 29    p[1] = (i>>16)
3422a 26 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20  &0xFF;.  p[2] = 
3422b 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20 20  (i>> 8)&0xFF;.  
3422c 70 5b 33 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30  p[3] = (i>> 0)&0
3422d 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 34 3b  xFF;.  return 4;
3422e 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  .}.static int wr
3422f 69 74 65 49 6e 74 36 34 28 75 38 20 2a 70 2c 20  iteInt64(u8 *p, 
34230 69 36 34 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d  i64 i){.  p[0] =
34231 20 28 69 3e 3e 35 36 29 26 30 78 46 46 3b 0a 20   (i>>56)&0xFF;. 
34232 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 34 38 29 26   p[1] = (i>>48)&
34233 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20 28  0xFF;.  p[2] = (
34234 69 3e 3e 34 30 29 26 30 78 46 46 3b 0a 20 20 70  i>>40)&0xFF;.  p
34235 5b 33 5d 20 3d 20 28 69 3e 3e 33 32 29 26 30 78  [3] = (i>>32)&0x
34236 46 46 3b 0a 20 20 70 5b 34 5d 20 3d 20 28 69 3e  FF;.  p[4] = (i>
34237 3e 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 35  >24)&0xFF;.  p[5
34238 5d 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46  ] = (i>>16)&0xFF
34239 3b 0a 20 20 70 5b 36 5d 20 3d 20 28 69 3e 3e 20  ;.  p[6] = (i>> 
3423a 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 37 5d 20  8)&0xFF;.  p[7] 
3423b 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a  = (i>> 0)&0xFF;.
3423c 20 20 72 65 74 75 72 6e 20 38 3b 0a 7d 0a 0a 2f    return 8;.}../
3423d 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74  *.** Increment t
3423e 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
3423f 6e 74 20 6f 66 20 6e 6f 64 65 20 70 2e 0a 2a 2f  nt of node p..*/
34240 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64  .static void nod
34241 65 52 65 66 65 72 65 6e 63 65 28 52 74 72 65 65  eReference(Rtree
34242 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  Node *p){.  if( 
34243 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 52 65 66  p ){.    p->nRef
34244 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ++;.  }.}../*.**
34245 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6e 74 65   Clear the conte
34246 6e 74 20 6f 66 20 6e 6f 64 65 20 70 20 28 73 65  nt of node p (se
34247 74 20 61 6c 6c 20 62 79 74 65 73 20 74 6f 20 30  t all bytes to 0
34248 78 30 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  x00)..*/.static 
34249 76 6f 69 64 20 6e 6f 64 65 5a 65 72 6f 28 52 74  void nodeZero(Rt
3424a 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3424b 65 65 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66  eeNode *p){.  if
3424c 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
3424d 74 28 26 70 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20  t(&p->zData[2], 
3424e 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  0, pRtree->iNode
3424f 53 69 7a 65 2d 32 29 3b 0a 20 20 20 20 70 2d 3e  Size-2);.    p->
34250 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 7d  isDirty = 1;.  }
34251 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
34252 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 69 4e  a node number iN
34253 6f 64 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20  ode, return the 
34254 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6b 65  corresponding ke
34255 79 20 74 6f 20 75 73 65 0a 2a 2a 20 69 6e 20 74  y to use.** in t
34256 68 65 20 52 74 72 65 65 2e 61 48 61 73 68 20 74  he Rtree.aHash t
34257 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
34258 69 6e 74 20 6e 6f 64 65 48 61 73 68 28 69 36 34  int nodeHash(i64
34259 20 69 4e 6f 64 65 29 7b 0a 20 20 72 65 74 75 72   iNode){.  retur
3425a 6e 20 28 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e  n (.    (iNode>>
3425b 35 36 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 38  56) ^ (iNode>>48
3425c 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 30 29 20  ) ^ (iNode>>40) 
3425d 5e 20 28 69 4e 6f 64 65 3e 3e 33 32 29 20 5e 20  ^ (iNode>>32) ^ 
3425e 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e 32 34 29  .    (iNode>>24)
3425f 20 5e 20 28 69 4e 6f 64 65 3e 3e 31 36 29 20 5e   ^ (iNode>>16) ^
34260 20 28 69 4e 6f 64 65 3e 3e 20 38 29 20 5e 20 28   (iNode>> 8) ^ (
34261 69 4e 6f 64 65 3e 3e 20 30 29 0a 20 20 29 20 25  iNode>> 0).  ) %
34262 20 48 41 53 48 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a   HASHSIZE;.}../*
34263 0a 2a 2a 20 53 65 61 72 63 68 20 74 68 65 20 6e  .** Search the n
34264 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 20 66  ode hash table f
34265 6f 72 20 6e 6f 64 65 20 69 4e 6f 64 65 2e 20 49  or node iNode. I
34266 66 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20  f found, return 
34267 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
34268 69 74 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72  it. Otherwise, r
34269 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74  eturn 0..*/.stat
3426a 69 63 20 52 74 72 65 65 4e 6f 64 65 20 2a 6e 6f  ic RtreeNode *no
3426b 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 52 74 72  deHashLookup(Rtr
3426c 65 65 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20  ee *pRtree, i64 
3426d 69 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e  iNode){.  RtreeN
3426e 6f 64 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  ode *p;.  assert
3426f 28 20 69 4e 6f 64 65 21 3d 30 20 29 3b 0a 20 20  ( iNode!=0 );.  
34270 66 6f 72 28 70 3d 70 52 74 72 65 65 2d 3e 61 48  for(p=pRtree->aH
34271 61 73 68 5b 6e 6f 64 65 48 61 73 68 28 69 4e 6f  ash[nodeHash(iNo
34272 64 65 29 5d 3b 20 70 20 26 26 20 70 2d 3e 69 4e  de)]; p && p->iN
34273 6f 64 65 21 3d 69 4e 6f 64 65 3b 20 70 3d 70 2d  ode!=iNode; p=p-
34274 3e 70 4e 65 78 74 29 3b 0a 20 20 72 65 74 75 72  >pNext);.  retur
34275 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  n p;.}../*.** Ad
34276 64 20 6e 6f 64 65 20 70 4e 6f 64 65 20 74 6f 20  d node pNode to 
34277 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61  the node hash ta
34278 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
34279 6f 69 64 20 6e 6f 64 65 48 61 73 68 49 6e 73 65  oid nodeHashInse
3427a 72 74 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  rt(Rtree *pRtree
3427b 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
3427c 64 65 29 7b 0a 20 20 69 66 28 20 70 4e 6f 64 65  de){.  if( pNode
3427d 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 48 61 73   ){.    int iHas
3427e 68 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  h;.    assert( p
3427f 4e 6f 64 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29  Node->pNext==0 )
34280 3b 0a 20 20 20 20 69 48 61 73 68 20 3d 20 6e 6f  ;.    iHash = no
34281 64 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e  deHash(pNode->iN
34282 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  ode);.    pNode-
34283 3e 70 4e 65 78 74 20 3d 20 70 52 74 72 65 65 2d  >pNext = pRtree-
34284 3e 61 48 61 73 68 5b 69 48 61 73 68 5d 3b 0a 20  >aHash[iHash];. 
34285 20 20 20 70 52 74 72 65 65 2d 3e 61 48 61 73 68     pRtree->aHash
34286 5b 69 48 61 73 68 5d 20 3d 20 70 4e 6f 64 65 3b  [iHash] = pNode;
34287 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
34288 6d 6f 76 65 20 6e 6f 64 65 20 70 4e 6f 64 65 20  move node pNode 
34289 66 72 6f 6d 20 74 68 65 20 6e 6f 64 65 20 68 61  from the node ha
3428a 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  sh table..*/.sta
3428b 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 48 61 73  tic void nodeHas
3428c 68 44 65 6c 65 74 65 28 52 74 72 65 65 20 2a 70  hDelete(Rtree *p
3428d 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
3428e 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65   *pNode){.  Rtre
3428f 65 4e 6f 64 65 20 2a 2a 70 70 3b 0a 20 20 69 66  eNode **pp;.  if
34290 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21 3d  ( pNode->iNode!=
34291 30 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 26 70  0 ){.    pp = &p
34292 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64  Rtree->aHash[nod
34293 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e 6f  eHash(pNode->iNo
34294 64 65 29 5d 3b 0a 20 20 20 20 66 6f 72 28 20 3b  de)];.    for( ;
34295 20 28 2a 70 70 29 21 3d 70 4e 6f 64 65 3b 20 70   (*pp)!=pNode; p
34296 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78  p = &(*pp)->pNex
34297 74 29 7b 20 61 73 73 65 72 74 28 2a 70 70 29 3b  t){ assert(*pp);
34298 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 70 4e 6f   }.    *pp = pNo
34299 64 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70  de->pNext;.    p
3429a 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  Node->pNext = 0;
3429b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  .  }.}../*.** Al
3429c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72  locate and retur
3429d 6e 20 6e 65 77 20 72 2d 74 72 65 65 20 6e 6f 64  n new r-tree nod
3429e 65 2e 20 49 6e 69 74 69 61 6c 6c 79 2c 20 28 52  e. Initially, (R
3429f 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d 3d  treeNode.iNode==
342a0 30 29 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  0),.** indicatin
342a1 67 20 74 68 61 74 20 6e 6f 64 65 20 68 61 73 20  g that node has 
342a2 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 61 73 73  not yet been ass
342a3 69 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75 6d  igned a node num
342a4 62 65 72 2e 20 49 74 20 69 73 0a 2a 2a 20 61 73  ber. It is.** as
342a5 73 69 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75  signed a node nu
342a6 6d 62 65 72 20 77 68 65 6e 20 6e 6f 64 65 57 72  mber when nodeWr
342a7 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ite() is called 
342a8 74 6f 20 77 72 69 74 65 20 74 68 65 0a 2a 2a 20  to write the.** 
342a9 6e 6f 64 65 20 63 6f 6e 74 65 6e 74 73 20 6f 75  node contents ou
342aa 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
342ab 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74 72  e..*/.static Rtr
342ac 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 4e 65 77 28  eeNode *nodeNew(
342ad 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
342ae 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e  treeNode *pParen
342af 74 2c 20 69 6e 74 20 7a 65 72 6f 29 7b 0a 20 20  t, int zero){.  
342b0 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
342b1 3b 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74 72  ;.  pNode = (Rtr
342b2 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33  eeNode *)sqlite3
342b3 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
342b4 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74 72  treeNode) + pRtr
342b5 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a  ee->iNodeSize);.
342b6 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
342b7 20 20 20 6d 65 6d 73 65 74 28 70 4e 6f 64 65 2c     memset(pNode,
342b8 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
342b9 4e 6f 64 65 29 20 2b 20 28 7a 65 72 6f 3f 70 52  Node) + (zero?pR
342ba 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 3a  tree->iNodeSize:
342bb 30 29 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  0));.    pNode->
342bc 7a 44 61 74 61 20 3d 20 28 75 38 20 2a 29 26 70  zData = (u8 *)&p
342bd 4e 6f 64 65 5b 31 5d 3b 0a 20 20 20 20 70 4e 6f  Node[1];.    pNo
342be 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  de->nRef = 1;.  
342bf 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74    pNode->pParent
342c0 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20   = pParent;.    
342c1 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
342c2 20 31 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66 65   1;.    nodeRefe
342c3 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
342c4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 6f    }.  return pNo
342c5 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74  de;.}../*.** Obt
342c6 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20  ain a reference 
342c7 74 6f 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f 64  to an r-tree nod
342c8 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
342c9 0a 6e 6f 64 65 41 63 71 75 69 72 65 28 0a 20 20  .nodeAcquire(.  
342ca 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20  Rtree *pRtree,  
342cb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 2d             /* R-
342cc 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
342cd 2f 0a 20 20 69 36 34 20 69 4e 6f 64 65 2c 20 20  /.  i64 iNode,  
342ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
342cf 2a 20 4e 6f 64 65 20 6e 75 6d 62 65 72 20 74 6f  * Node number to
342d0 20 6c 6f 61 64 20 2a 2f 0a 20 20 52 74 72 65 65   load */.  Rtree
342d1 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 20  Node *pParent,  
342d2 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20        /* Either 
342d3 74 68 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 20  the parent node 
342d4 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 52 74 72  or NULL */.  Rtr
342d5 65 65 4e 6f 64 65 20 2a 2a 70 70 4e 6f 64 65 20  eeNode **ppNode 
342d6 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
342d7 41 63 71 75 69 72 65 64 20 6e 6f 64 65 20 2a 2f  Acquired node */
342d8 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
342d9 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
342da 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ;..  /* Check if
342db 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6e   the requested n
342dc 6f 64 65 20 69 73 20 61 6c 72 65 61 64 79 20 69  ode is already i
342dd 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  n the hash table
342de 2e 20 49 66 20 73 6f 2c 0a 20 20 2a 2a 20 69 6e  . If so,.  ** in
342df 63 72 65 61 73 65 20 69 74 73 20 72 65 66 65 72  crease its refer
342e0 65 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72  ence count and r
342e1 65 74 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20  eturn it..  */. 
342e2 20 69 66 28 20 28 70 4e 6f 64 65 20 3d 20 6e 6f   if( (pNode = no
342e3 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74  deHashLookup(pRt
342e4 72 65 65 2c 20 69 4e 6f 64 65 29 29 20 29 7b 0a  ree, iNode)) ){.
342e5 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
342e6 72 65 6e 74 20 7c 7c 20 21 70 4e 6f 64 65 2d 3e  rent || !pNode->
342e7 70 50 61 72 65 6e 74 20 7c 7c 20 70 4e 6f 64 65  pParent || pNode
342e8 2d 3e 70 50 61 72 65 6e 74 3d 3d 70 50 61 72 65  ->pParent==pPare
342e9 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  nt );.    if( pP
342ea 61 72 65 6e 74 20 26 26 20 21 70 4e 6f 64 65 2d  arent && !pNode-
342eb 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  >pParent ){.    
342ec 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
342ed 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20  pParent);.      
342ee 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d  pNode->pParent =
342ef 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 7d 0a   pParent;.    }.
342f0 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b      pNode->nRef+
342f1 2b 3b 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d  +;.    *ppNode =
342f2 20 70 4e 6f 64 65 3b 0a 20 20 20 20 72 65 74 75   pNode;.    retu
342f3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
342f4 7d 0a 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74  }..  pNode = (Rt
342f5 72 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65  reeNode *)sqlite
342f6 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
342f7 52 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74  RtreeNode) + pRt
342f8 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b  ree->iNodeSize);
342f9 0a 20 20 69 66 28 20 21 70 4e 6f 64 65 20 29 7b  .  if( !pNode ){
342fa 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d 20 30  .    *ppNode = 0
342fb 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
342fc 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
342fd 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20   pNode->pParent 
342fe 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 70 4e 6f  = pParent;.  pNo
342ff 64 65 2d 3e 7a 44 61 74 61 20 3d 20 28 75 38 20  de->zData = (u8 
34300 2a 29 26 70 4e 6f 64 65 5b 31 5d 3b 0a 20 20 70  *)&pNode[1];.  p
34301 4e 6f 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  Node->nRef = 1;.
34302 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d    pNode->iNode =
34303 20 69 4e 6f 64 65 3b 0a 20 20 70 4e 6f 64 65 2d   iNode;.  pNode-
34304 3e 69 73 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  >isDirty = 0;.  
34305 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30  pNode->pNext = 0
34306 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ;..  sqlite3_bin
34307 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e  d_int64(pRtree->
34308 70 52 65 61 64 4e 6f 64 65 2c 20 31 2c 20 69 4e  pReadNode, 1, iN
34309 6f 64 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  ode);.  rc = sql
3430a 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
3430b 2d 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20  ->pReadNode);.  
3430c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
3430d 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  OW ){.    const 
3430e 75 38 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69  u8 *zBlob = sqli
3430f 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
34310 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
34311 65 2c 20 30 29 3b 0a 20 20 20 20 6d 65 6d 63 70  e, 0);.    memcp
34312 79 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20  y(pNode->zData, 
34313 7a 42 6c 6f 62 2c 20 70 52 74 72 65 65 2d 3e 69  zBlob, pRtree->i
34314 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 20 20 6e  NodeSize);.    n
34315 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61  odeReference(pPa
34316 72 65 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rent);.  }else{.
34317 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
34318 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f  (pNode);.    pNo
34319 64 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2a  de = 0;.  }..  *
3431a 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b 0a  ppNode = pNode;.
3431b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
3431c 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
3431d 61 64 4e 6f 64 65 29 3b 0a 0a 20 20 69 66 28 20  adNode);..  if( 
3431e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
3431f 20 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20   iNode==1 ){.   
34320 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20   pRtree->iDepth 
34321 3d 20 72 65 61 64 49 6e 74 31 36 28 70 4e 6f 64  = readInt16(pNod
34322 65 2d 3e 7a 44 61 74 61 29 3b 0a 20 20 7d 0a 0a  e->zData);.  }..
34323 20 20 61 73 73 65 72 74 28 20 28 72 63 3d 3d 53    assert( (rc==S
34324 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4e 6f 64  QLITE_OK && pNod
34325 65 29 20 7c 7c 20 28 70 4e 6f 64 65 3d 3d 30 20  e) || (pNode==0 
34326 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  && rc!=SQLITE_OK
34327 29 20 29 3b 0a 20 20 6e 6f 64 65 48 61 73 68 49  ) );.  nodeHashI
34328 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
34329 6f 64 65 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ode);..  return 
3432a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 76 65  rc;.}../*.** Ove
3432b 72 77 72 69 74 65 20 63 65 6c 6c 20 69 43 65 6c  rwrite cell iCel
3432c 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 20  l of node pNode 
3432d 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74  with the content
3432e 73 20 6f 66 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73  s of pCell..*/.s
3432f 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 4f  tatic void nodeO
34330 76 65 72 77 72 69 74 65 43 65 6c 6c 28 0a 20 20  verwriteCell(.  
34331 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
34332 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
34333 64 65 2c 20 20 0a 20 20 52 74 72 65 65 43 65 6c  de,  .  RtreeCel
34334 6c 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *pCell, .  int
34335 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20   iCell.){.  int 
34336 69 69 3b 0a 20 20 75 38 20 2a 70 20 3d 20 26 70  ii;.  u8 *p = &p
34337 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b 20  Node->zData[4 + 
34338 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
34339 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 3b 0a 20 20  rCell*iCell];.  
3433a 70 20 2b 3d 20 77 72 69 74 65 49 6e 74 36 34 28  p += writeInt64(
3433b 70 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  p, pCell->iRowid
3433c 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
3433d 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  i<(pRtree->nDim*
3433e 32 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 70  2); ii++){.    p
3433f 20 2b 3d 20 77 72 69 74 65 43 6f 6f 72 64 28 70   += writeCoord(p
34340 2c 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64  , &pCell->aCoord
34341 5b 69 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 4e 6f  [ii]);.  }.  pNo
34342 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
34343 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
34344 20 63 65 6c 6c 20 74 68 65 20 63 65 6c 6c 20 77   cell the cell w
34345 69 74 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20  ith index iCell 
34346 66 72 6f 6d 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  from node pNode.
34347 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
34348 6e 6f 64 65 44 65 6c 65 74 65 43 65 6c 6c 28 52  nodeDeleteCell(R
34349 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
3434a 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20  reeNode *pNode, 
3434b 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38  int iCell){.  u8
3434c 20 2a 70 44 73 74 20 3d 20 26 70 4e 6f 64 65 2d   *pDst = &pNode-
3434d 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65  >zData[4 + pRtre
3434e 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
3434f 2a 69 43 65 6c 6c 5d 3b 0a 20 20 75 38 20 2a 70  *iCell];.  u8 *p
34350 53 72 63 20 3d 20 26 70 44 73 74 5b 70 52 74 72  Src = &pDst[pRtr
34351 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
34352 6c 5d 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20  l];.  int nByte 
34353 3d 20 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20  = (NCELL(pNode) 
34354 2d 20 69 43 65 6c 6c 20 2d 20 31 29 20 2a 20 70  - iCell - 1) * p
34355 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
34356 43 65 6c 6c 3b 0a 20 20 6d 65 6d 6d 6f 76 65 28  Cell;.  memmove(
34357 70 44 73 74 2c 20 70 53 72 63 2c 20 6e 42 79 74  pDst, pSrc, nByt
34358 65 29 3b 0a 20 20 77 72 69 74 65 49 6e 74 31 36  e);.  writeInt16
34359 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 32  (&pNode->zData[2
3435a 5d 2c 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2d  ], NCELL(pNode)-
3435b 31 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44  1);.  pNode->isD
3435c 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  irty = 1;.}../*.
3435d 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63 6f  ** Insert the co
3435e 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70  ntents of cell p
3435f 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20 70  Cell into node p
34360 4e 6f 64 65 2e 20 49 66 20 74 68 65 20 69 6e 73  Node. If the ins
34361 65 72 74 0a 2a 2a 20 69 73 20 73 75 63 63 65 73  ert.** is succes
34362 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c  sful, return SQL
34363 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  ITE_OK..**.** If
34364 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 65 6e   there is not en
34365 6f 75 67 68 20 66 72 65 65 20 73 70 61 63 65 20  ough free space 
34366 69 6e 20 70 4e 6f 64 65 2c 20 72 65 74 75 72 6e  in pNode, return
34367 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a 2f   SQLITE_FULL..*/
34368 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
34369 49 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 52 74  InsertCell(.  Rt
3436a 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
3436b 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
3436c 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  , .  RtreeCell *
3436d 70 43 65 6c 6c 20 0a 29 7b 0a 20 20 69 6e 74 20  pCell .){.  int 
3436e 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  nCell;          
3436f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
34370 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 63  rent number of c
34371 65 6c 6c 73 20 69 6e 20 70 4e 6f 64 65 20 2a 2f  ells in pNode */
34372 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 3b  .  int nMaxCell;
34373 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34374 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62   /* Maximum numb
34375 65 72 20 6f 66 20 63 65 6c 6c 73 20 66 6f 72 20  er of cells for 
34376 70 4e 6f 64 65 20 2a 2f 0a 0a 20 20 6e 4d 61 78  pNode */..  nMax
34377 43 65 6c 6c 20 3d 20 28 70 52 74 72 65 65 2d 3e  Cell = (pRtree->
34378 69 4e 6f 64 65 53 69 7a 65 2d 34 29 2f 70 52 74  iNodeSize-4)/pRt
34379 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65  ree->nBytesPerCe
3437a 6c 6c 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43  ll;.  nCell = NC
3437b 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 0a 20 20 61  ELL(pNode);..  a
3437c 73 73 65 72 74 28 6e 43 65 6c 6c 3c 3d 6e 4d 61  ssert(nCell<=nMa
3437d 78 43 65 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 6e  xCell);..  if( n
3437e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 20 29 7b  Cell<nMaxCell ){
3437f 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69  .    nodeOverwri
34380 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
34381 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 6e 43 65  Node, pCell, nCe
34382 6c 6c 29 3b 0a 20 20 20 20 77 72 69 74 65 49 6e  ll);.    writeIn
34383 74 31 36 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  t16(&pNode->zDat
34384 61 5b 32 5d 2c 20 6e 43 65 6c 6c 2b 31 29 3b 0a  a[2], nCell+1);.
34385 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72      pNode->isDir
34386 74 79 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72  ty = 1;.  }..  r
34387 65 74 75 72 6e 20 28 6e 43 65 6c 6c 3d 3d 6e 4d  eturn (nCell==nM
34388 61 78 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  axCell);.}../*.*
34389 2a 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73  * If the node is
3438a 20 64 69 72 74 79 2c 20 77 72 69 74 65 20 69 74   dirty, write it
3438b 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
3438c 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
3438d 69 6e 74 0a 6e 6f 64 65 57 72 69 74 65 28 52 74  int.nodeWrite(Rt
3438e 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3438f 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
34390 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
34391 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64  E_OK;.  if( pNod
34392 65 2d 3e 69 73 44 69 72 74 79 20 29 7b 0a 20 20  e->isDirty ){.  
34393 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
34394 70 20 3d 20 70 52 74 72 65 65 2d 3e 70 57 72 69  p = pRtree->pWri
34395 74 65 4e 6f 64 65 3b 0a 20 20 20 20 69 66 28 20  teNode;.    if( 
34396 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 29 7b 0a  pNode->iNode ){.
34397 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
34398 6e 64 5f 69 6e 74 36 34 28 70 2c 20 31 2c 20 70  nd_int64(p, 1, p
34399 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
3439a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
3439b 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
3439c 28 70 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20  (p, 1);.    }.  
3439d 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62    sqlite3_bind_b
3439e 6c 6f 62 28 70 2c 20 32 2c 20 70 4e 6f 64 65 2d  lob(p, 2, pNode-
3439f 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e  >zData, pRtree->
343a0 69 4e 6f 64 65 53 69 7a 65 2c 20 53 51 4c 49 54  iNodeSize, SQLIT
343a1 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
343a2 71 6c 69 74 65 33 5f 73 74 65 70 28 70 29 3b 0a  qlite3_step(p);.
343a3 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72      pNode->isDir
343a4 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  ty = 0;.    rc =
343a5 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
343a6 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65  );.    if( pNode
343a7 2d 3e 69 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63  ->iNode==0 && rc
343a8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
343a9 20 20 20 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64       pNode->iNod
343aa 65 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  e = sqlite3_last
343ab 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 52  _insert_rowid(pR
343ac 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20  tree->db);.     
343ad 20 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28   nodeHashInsert(
343ae 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
343af 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
343b0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
343b1 52 65 6c 65 61 73 65 20 61 20 72 65 66 65 72 65  Release a refere
343b2 6e 63 65 20 74 6f 20 61 20 6e 6f 64 65 2e 20 49  nce to a node. I
343b3 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69  f the node is di
343b4 72 74 79 20 61 6e 64 20 74 68 65 20 72 65 66 65  rty and the refe
343b5 72 65 6e 63 65 0a 2a 2a 20 63 6f 75 6e 74 20 64  rence.** count d
343b6 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2c 20 74 68  rops to zero, th
343b7 65 20 6e 6f 64 65 20 64 61 74 61 20 69 73 20 77  e node data is w
343b8 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
343b9 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
343ba 63 20 69 6e 74 0a 6e 6f 64 65 52 65 6c 65 61 73  c int.nodeReleas
343bb 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
343bc 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
343bd 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
343be 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
343bf 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 61 73 73  pNode ){.    ass
343c0 65 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  ert( pNode->nRef
343c1 3e 30 20 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  >0 );.    pNode-
343c2 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28  >nRef--;.    if(
343c3 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 30 20   pNode->nRef==0 
343c4 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 6f  ){.      if( pNo
343c5 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a  de->iNode==1 ){.
343c6 20 20 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e          pRtree->
343c7 69 44 65 70 74 68 20 3d 20 2d 31 3b 0a 20 20 20  iDepth = -1;.   
343c8 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
343c9 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b  Node->pParent ){
343ca 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  .        rc = no
343cb 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
343cc 2c 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  , pNode->pParent
343cd 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
343ce 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
343cf 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
343d0 20 3d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74   = nodeWrite(pRt
343d1 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20  ree, pNode);.   
343d2 20 20 20 7d 0a 20 20 20 20 20 20 6e 6f 64 65 48     }.      nodeH
343d3 61 73 68 44 65 6c 65 74 65 28 70 52 74 72 65 65  ashDelete(pRtree
343d4 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  , pNode);.      
343d5 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f  sqlite3_free(pNo
343d6 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  de);.    }.  }. 
343d7 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
343d8 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
343d9 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  64-bit integer v
343da 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64 20  alue associated 
343db 77 69 74 68 20 63 65 6c 6c 20 69 43 65 6c 6c 20  with cell iCell 
343dc 6f 66 0a 2a 2a 20 6e 6f 64 65 20 70 4e 6f 64 65  of.** node pNode
343dd 2e 20 49 66 20 70 4e 6f 64 65 20 69 73 20 61 20  . If pNode is a 
343de 6c 65 61 66 20 6e 6f 64 65 2c 20 74 68 69 73 20  leaf node, this 
343df 69 73 20 61 20 72 6f 77 69 64 2e 20 49 66 20 69  is a rowid. If i
343e0 74 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 72  t is.** an inter
343e1 6e 61 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 74  nal node, then t
343e2 68 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65  he 64-bit intege
343e3 72 20 69 73 20 61 20 63 68 69 6c 64 20 70 61 67  r is a child pag
343e4 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
343e5 74 69 63 20 69 36 34 20 6e 6f 64 65 47 65 74 52  tic i64 nodeGetR
343e6 6f 77 69 64 28 0a 20 20 52 74 72 65 65 20 2a 70  owid(.  Rtree *p
343e7 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e  Rtree, .  RtreeN
343e8 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69  ode *pNode, .  i
343e9 6e 74 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 61 73  nt iCell.){.  as
343ea 73 65 72 74 28 20 69 43 65 6c 6c 3c 4e 43 45 4c  sert( iCell<NCEL
343eb 4c 28 70 4e 6f 64 65 29 20 29 3b 0a 20 20 72 65  L(pNode) );.  re
343ec 74 75 72 6e 20 72 65 61 64 49 6e 74 36 34 28 26  turn readInt64(&
343ed 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b  pNode->zData[4 +
343ee 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50   pRtree->nBytesP
343ef 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 29 3b 0a  erCell*iCell]);.
343f0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
343f1 63 6f 6f 72 64 69 6e 61 74 65 20 69 43 6f 6f 72  coordinate iCoor
343f2 64 20 66 72 6f 6d 20 63 65 6c 6c 20 69 43 65 6c  d from cell iCel
343f3 6c 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l in node pNode.
343f4 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
343f5 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 0a 20 20  nodeGetCoord(.  
343f6 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
343f7 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
343f8 64 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c  de, .  int iCell
343f9 2c 0a 20 20 69 6e 74 20 69 43 6f 6f 72 64 2c 0a  ,.  int iCoord,.
343fa 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43    RtreeCoord *pC
343fb 6f 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 2f  oord           /
343fc 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65  * Space to write
343fd 20 72 65 73 75 6c 74 20 74 6f 20 2a 2f 0a 29 7b   result to */.){
343fe 0a 20 20 72 65 61 64 43 6f 6f 72 64 28 26 70 4e  .  readCoord(&pN
343ff 6f 64 65 2d 3e 7a 44 61 74 61 5b 31 32 20 2b 20  ode->zData[12 + 
34400 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
34401 72 43 65 6c 6c 2a 69 43 65 6c 6c 20 2b 20 34 2a  rCell*iCell + 4*
34402 69 43 6f 6f 72 64 5d 2c 20 70 43 6f 6f 72 64 29  iCoord], pCoord)
34403 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72  ;.}../*.** Deser
34404 69 61 6c 69 7a 65 20 63 65 6c 6c 20 69 43 65 6c  ialize cell iCel
34405 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l of node pNode.
34406 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 73 74   Populate the st
34407 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 0a  ructure pointed.
34408 2a 2a 20 74 6f 20 62 79 20 70 43 65 6c 6c 20 77  ** to by pCell w
34409 69 74 68 20 74 68 65 20 72 65 73 75 6c 74 73 2e  ith the results.
3440a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3440b 6e 6f 64 65 47 65 74 43 65 6c 6c 28 0a 20 20 52  nodeGetCell(.  R
3440c 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
3440d 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
3440e 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c  e, .  int iCell,
3440f 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
34410 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b  ell.){.  int ii;
34411 0a 20 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  .  pCell->iRowid
34412 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28   = nodeGetRowid(
34413 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
34414 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d  Cell);.  for(ii=
34415 30 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44  0; ii<pRtree->nD
34416 69 6d 2a 32 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  im*2; ii++){.   
34417 20 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70 52   nodeGetCoord(pR
34418 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65  tree, pNode, iCe
34419 6c 6c 2c 20 69 69 2c 20 26 70 43 65 6c 6c 2d 3e  ll, ii, &pCell->
3441a 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a 20 20 7d  aCoord[ii]);.  }
3441b 0a 7d 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  .}.../* Forward 
3441c 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20  declaration for 
3441d 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  the function tha
3441e 74 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20  t does the work 
3441f 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61  of.** the virtua
34420 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
34421 43 72 65 61 74 65 28 29 20 61 6e 64 20 78 43 6f  Create() and xCo
34422 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 73 2e  nnect() methods.
34423 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
34424 74 72 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69  treeInit(.  sqli
34425 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69  te3 *, void *, i
34426 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  nt, const char *
34427 63 6f 6e 73 74 2a 2c 20 73 71 6c 69 74 65 33 5f  const*, sqlite3_
34428 76 74 61 62 20 2a 2a 2c 20 63 68 61 72 20 2a 2a  vtab **, char **
34429 2c 20 69 6e 74 0a 29 3b 0a 0a 2f 2a 20 0a 2a 2a  , int.);../* .**
3442a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
3442b 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72 65  able module xCre
3442c 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ate method..*/.s
3442d 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 43  tatic int rtreeC
3442e 72 65 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33  reate(.  sqlite3
3442f 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41   *db,.  void *pA
34430 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  ux,.  int argc, 
34431 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
34432 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65  t*argv,.  sqlite
34433 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
34434 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a  .  char **pzErr.
34435 29 7b 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65  ){.  return rtre
34436 65 49 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20  eInit(db, pAux, 
34437 61 72 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74  argc, argv, ppVt
34438 61 62 2c 20 70 7a 45 72 72 2c 20 31 29 3b 0a 7d  ab, pzErr, 1);.}
34439 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
3443a 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
3443b 75 6c 65 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74  ule xConnect met
3443c 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
3443d 6e 74 20 72 74 72 65 65 43 6f 6e 6e 65 63 74 28  nt rtreeConnect(
3443e 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
3443f 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20    void *pAux,.  
34440 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20  int argc, const 
34441 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76  char *const*argv
34442 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ,.  sqlite3_vtab
34443 20 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68 61   **ppVtab,.  cha
34444 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 72  r **pzErr.){.  r
34445 65 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74 28  eturn rtreeInit(
34446 64 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c 20  db, pAux, argc, 
34447 61 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70 7a  argv, ppVtab, pz
34448 45 72 72 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Err, 0);.}../*.*
34449 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  * Increment the 
3444a 72 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65  r-tree reference
3444b 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69   count..*/.stati
3444c 63 20 76 6f 69 64 20 72 74 72 65 65 52 65 66 65  c void rtreeRefe
3444d 72 65 6e 63 65 28 52 74 72 65 65 20 2a 70 52 74  rence(Rtree *pRt
3444e 72 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d 3e  ree){.  pRtree->
3444f 6e 42 75 73 79 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a  nBusy++;.}../*.*
34450 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
34451 72 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65  r-tree reference
34452 20 63 6f 75 6e 74 2e 20 57 68 65 6e 20 74 68 65   count. When the
34453 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
34454 20 72 65 61 63 68 65 73 0a 2a 2a 20 7a 65 72 6f   reaches.** zero
34455 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 69   the structure i
34456 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74  s deleted..*/.st
34457 61 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 52  atic void rtreeR
34458 65 6c 65 61 73 65 28 52 74 72 65 65 20 2a 70 52  elease(Rtree *pR
34459 74 72 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d  tree){.  pRtree-
3445a 3e 6e 42 75 73 79 2d 2d 3b 0a 20 20 69 66 28 20  >nBusy--;.  if( 
3445b 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 3d 3d 30  pRtree->nBusy==0
3445c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
3445d 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d  finalize(pRtree-
3445e 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20 20  >pReadNode);.   
3445f 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
34460 65 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  e(pRtree->pWrite
34461 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Node);.    sqlit
34462 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
34463 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29  ee->pDeleteNode)
34464 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
34465 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
34466 52 65 61 64 52 6f 77 69 64 29 3b 0a 20 20 20 20  ReadRowid);.    
34467 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
34468 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
34469 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
3446a 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
3446b 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
3446c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
3446d 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
3446e 70 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20 20  pReadParent);.  
3446f 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
34470 7a 65 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  ze(pRtree->pWrit
34471 65 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71  eParent);.    sq
34472 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
34473 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
34474 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  rent);.    sqlit
34475 65 33 5f 66 72 65 65 28 70 52 74 72 65 65 29 3b  e3_free(pRtree);
34476 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  .  }.}../* .** R
34477 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
34478 6c 65 20 6d 6f 64 75 6c 65 20 78 44 69 73 63 6f  le module xDisco
34479 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f  nnect method..*/
3447a 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
3447b 65 44 69 73 63 6f 6e 6e 65 63 74 28 73 71 6c 69  eDisconnect(sqli
3447c 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
3447d 7b 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65  {.  rtreeRelease
3447e 28 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 29  ((Rtree *)pVtab)
3447f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
34480 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  E_OK;.}../* .** 
34481 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61  Rtree virtual ta
34482 62 6c 65 20 6d 6f 64 75 6c 65 20 78 44 65 73 74  ble module xDest
34483 72 6f 79 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  roy method..*/.s
34484 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 44  tatic int rtreeD
34485 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 76  estroy(sqlite3_v
34486 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 52  tab *pVtab){.  R
34487 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
34488 52 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20  Rtree *)pVtab;. 
34489 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20   int rc;.  char 
3448a 2a 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74  *zCreate = sqlit
3448b 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20  e3_mprintf(.    
3448c 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27  "DROP TABLE '%q'
3448d 2e 27 25 71 5f 6e 6f 64 65 27 3b 22 0a 20 20 20  .'%q_node';".   
3448e 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71   "DROP TABLE '%q
3448f 27 2e 27 25 71 5f 72 6f 77 69 64 27 3b 22 0a 20  '.'%q_rowid';". 
34490 20 20 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27     "DROP TABLE '
34491 25 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 3b  %q'.'%q_parent';
34492 22 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a  ",.    pRtree->z
34493 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d  Db, pRtree->zNam
34494 65 2c 20 0a 20 20 20 20 70 52 74 72 65 65 2d 3e  e, .    pRtree->
34495 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
34496 6d 65 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e  me,.    pRtree->
34497 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
34498 6d 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 7a  me.  );.  if( !z
34499 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 72 63  Create ){.    rc
3449a 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
3449b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
3449c 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
3449d 70 52 74 72 65 65 2d 3e 64 62 2c 20 7a 43 72 65  pRtree->db, zCre
3449e 61 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ate, 0, 0, 0);. 
3449f 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
344a0 7a 43 72 65 61 74 65 29 3b 0a 20 20 7d 0a 20 20  zCreate);.  }.  
344a1 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
344a2 4b 20 29 7b 0a 20 20 20 20 72 74 72 65 65 52 65  K ){.    rtreeRe
344a3 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20  lease(pRtree);. 
344a4 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
344a5 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  .}../* .** Rtree
344a6 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
344a7 6f 64 75 6c 65 20 78 4f 70 65 6e 20 6d 65 74 68  odule xOpen meth
344a8 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
344a9 74 20 72 74 72 65 65 4f 70 65 6e 28 73 71 6c 69  t rtreeOpen(sqli
344aa 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
344ab 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
344ac 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29  rsor **ppCursor)
344ad 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
344ae 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 52 74 72  ITE_NOMEM;.  Rtr
344af 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a  eeCursor *pCsr;.
344b0 0a 20 20 70 43 73 72 20 3d 20 28 52 74 72 65 65  .  pCsr = (Rtree
344b1 43 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33  Cursor *)sqlite3
344b2 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
344b3 74 72 65 65 43 75 72 73 6f 72 29 29 3b 0a 20 20  treeCursor));.  
344b4 69 66 28 20 70 43 73 72 20 29 7b 0a 20 20 20 20  if( pCsr ){.    
344b5 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20  memset(pCsr, 0, 
344b6 73 69 7a 65 6f 66 28 52 74 72 65 65 43 75 72 73  sizeof(RtreeCurs
344b7 6f 72 29 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e  or));.    pCsr->
344b8 62 61 73 65 2e 70 56 74 61 62 20 3d 20 70 56 54  base.pVtab = pVT
344b9 61 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ab;.    rc = SQL
344ba 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 2a 70  ITE_OK;.  }.  *p
344bb 70 43 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74  pCursor = (sqlit
344bc 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
344bd 29 70 43 73 72 3b 0a 0a 20 20 72 65 74 75 72 6e  )pCsr;..  return
344be 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52   rc;.}../* .** R
344bf 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
344c0 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6c 6f 73 65  le module xClose
344c1 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
344c2 69 63 20 69 6e 74 20 72 74 72 65 65 43 6c 6f 73  ic int rtreeClos
344c3 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  e(sqlite3_vtab_c
344c4 75 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52  ursor *cur){.  R
344c5 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
344c6 52 74 72 65 65 20 2a 29 28 63 75 72 2d 3e 70 56  Rtree *)(cur->pV
344c7 74 61 62 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  tab);.  int rc;.
344c8 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70    RtreeCursor *p
344c9 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73  Csr = (RtreeCurs
344ca 6f 72 20 2a 29 63 75 72 3b 0a 20 20 73 71 6c 69  or *)cur;.  sqli
344cb 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61  te3_free(pCsr->a
344cc 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 72  Constraint);.  r
344cd 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
344ce 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e  pRtree, pCsr->pN
344cf 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ode);.  sqlite3_
344d0 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 72 65  free(pCsr);.  re
344d1 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
344d2 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
344d3 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 45 6f  table module xEo
344d4 66 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20  f method..**.** 
344d5 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
344d6 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f  if the cursor do
344d7 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  es not currently
344d8 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69   point to a vali
344d9 64 20 0a 2a 2a 20 72 65 63 6f 72 64 20 28 69 2e  d .** record (i.
344da 65 20 69 66 20 74 68 65 20 73 63 61 6e 20 68 61  e if the scan ha
344db 73 20 66 69 6e 69 73 68 65 64 29 2c 20 6f 72 20  s finished), or 
344dc 7a 65 72 6f 20 6f 74 68 65 72 77 69 73 65 2e 0a  zero otherwise..
344dd 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
344de 72 65 65 45 6f 66 28 73 71 6c 69 74 65 33 5f 76  reeEof(sqlite3_v
344df 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72 29  tab_cursor *cur)
344e0 7b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20  {.  RtreeCursor 
344e1 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75  *pCsr = (RtreeCu
344e2 72 73 6f 72 20 2a 29 63 75 72 3b 0a 20 20 72 65  rsor *)cur;.  re
344e3 74 75 72 6e 20 28 70 43 73 72 2d 3e 70 4e 6f 64  turn (pCsr->pNod
344e4 65 3d 3d 30 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  e==0);.}../* .**
344e5 20 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20   Cursor pCursor 
344e6 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73  currently points
344e7 20 74 6f 20 61 20 63 65 6c 6c 20 69 6e 20 61 20   to a cell in a 
344e8 6e 6f 6e 2d 6c 65 61 66 20 70 61 67 65 2e 0a 2a  non-leaf page..*
344e9 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
344ea 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68 65   the sub-tree he
344eb 61 64 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c  aded by the cell
344ec 20 69 73 20 66 69 6c 74 65 72 65 64 0a 2a 2a 20   is filtered.** 
344ed 28 65 78 63 6c 75 64 65 64 29 20 62 79 20 74 68  (excluded) by th
344ee 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e  e constraints in
344ef 20 74 68 65 20 70 43 75 72 73 6f 72 2d 3e 61 43   the pCursor->aC
344f0 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 0a 2a 2a 20  onstraint[] .** 
344f1 61 72 72 61 79 2c 20 6f 72 20 66 61 6c 73 65 20  array, or false 
344f2 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
344f3 61 74 69 63 20 69 6e 74 20 74 65 73 74 52 74 72  atic int testRtr
344f4 65 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52  eeCell(Rtree *pR
344f5 74 72 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f  tree, RtreeCurso
344f6 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52  r *pCursor){.  R
344f7 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
344f8 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 62   int ii;.  int b
344f9 52 65 73 20 3d 20 30 3b 0a 0a 20 20 6e 6f 64 65  Res = 0;..  node
344fa 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
344fb 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
344fc 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20  pCursor->iCell, 
344fd 26 63 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69  &cell);.  for(ii
344fe 3d 30 3b 20 62 52 65 73 3d 3d 30 20 26 26 20 69  =0; bRes==0 && i
344ff 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43 6f 6e 73  i<pCursor->nCons
34500 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  traint; ii++){. 
34501 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69     RtreeConstrai
34502 6e 74 20 2a 70 20 3d 20 26 70 43 75 72 73 6f 72  nt *p = &pCursor
34503 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69  ->aConstraint[ii
34504 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 65  ];.    double ce
34505 6c 6c 5f 6d 69 6e 20 3d 20 44 43 4f 4f 52 44 28  ll_min = DCOORD(
34506 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e  cell.aCoord[(p->
34507 69 43 6f 6f 72 64 3e 3e 31 29 2a 32 5d 29 3b 0a  iCoord>>1)*2]);.
34508 20 20 20 20 64 6f 75 62 6c 65 20 63 65 6c 6c 5f      double cell_
34509 6d 61 78 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c  max = DCOORD(cel
3450a 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f  l.aCoord[(p->iCo
3450b 6f 72 64 3e 3e 31 29 2a 32 2b 31 5d 29 3b 0a 0a  ord>>1)*2+1]);..
3450c 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 6f 70      assert(p->op
3450d 3d 3d 52 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d  ==RTREE_LE || p-
3450e 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c  >op==RTREE_LT ||
3450f 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45   p->op==RTREE_GE
34510 20 0a 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e   .        || p->
34511 6f 70 3d 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20  op==RTREE_GT || 
34512 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 45 51 0a  p->op==RTREE_EQ.
34513 20 20 20 20 29 3b 0a 0a 20 20 20 20 73 77 69 74      );..    swit
34514 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20  ch( p->op ){.   
34515 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 4c 45     case RTREE_LE
34516 3a 20 63 61 73 65 20 52 54 52 45 45 5f 4c 54 3a  : case RTREE_LT:
34517 20 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75   bRes = p->rValu
34518 65 3c 63 65 6c 6c 5f 6d 69 6e 3b 20 62 72 65 61  e<cell_min; brea
34519 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54  k;.      case RT
3451a 52 45 45 5f 47 45 3a 20 63 61 73 65 20 52 54 52  REE_GE: case RTR
3451b 45 45 5f 47 54 3a 20 62 52 65 73 20 3d 20 70 2d  EE_GT: bRes = p-
3451c 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78  >rValue>cell_max
3451d 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63  ; break;.      c
3451e 61 73 65 20 52 54 52 45 45 5f 45 51 3a 20 0a 20  ase RTREE_EQ: . 
3451f 20 20 20 20 20 20 20 62 52 65 73 20 3d 20 28 70         bRes = (p
34520 2d 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61  ->rValue>cell_ma
34521 78 20 7c 7c 20 70 2d 3e 72 56 61 6c 75 65 3c 63  x || p->rValue<c
34522 65 6c 6c 5f 6d 69 6e 29 3b 0a 20 20 20 20 20 20  ell_min);.      
34523 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
34524 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 62 52 65   }..  return bRe
34525 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74  s;.}../* .** Ret
34526 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
34527 63 65 6c 6c 20 74 68 61 74 20 63 75 72 73 6f 72  cell that cursor
34528 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
34529 6c 79 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20  ly points to.** 
3452a 77 6f 75 6c 64 20 62 65 20 66 69 6c 74 65 72 65  would be filtere
3452b 64 20 28 65 78 63 6c 75 64 65 64 29 20 62 79 20  d (excluded) by 
3452c 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  the constraints 
3452d 69 6e 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 73  in the .** pCurs
3452e 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  or->aConstraint[
3452f 5d 20 61 72 72 61 79 2c 20 6f 72 20 66 61 6c 73  ] array, or fals
34530 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a  e otherwise..**.
34531 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
34532 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
34533 65 20 63 65 6c 6c 20 69 73 20 70 61 72 74 20 6f  e cell is part o
34534 66 20 61 20 6c 65 61 66 20 6e 6f 64 65 2e 0a 2a  f a leaf node..*
34535 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 73  /.static int tes
34536 74 52 74 72 65 65 45 6e 74 72 79 28 52 74 72 65  tRtreeEntry(Rtre
34537 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
34538 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  Cursor *pCursor)
34539 7b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65  {.  RtreeCell ce
3453a 6c 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20  ll;.  int ii;.. 
3453b 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
3453c 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e  ree, pCursor->pN
3453d 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e 69 43  ode, pCursor->iC
3453e 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 66  ell, &cell);.  f
3453f 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72  or(ii=0; ii<pCur
34540 73 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  sor->nConstraint
34541 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
34542 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20  eeConstraint *p 
34543 3d 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e  = &pCursor->aCon
34544 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20  straint[ii];.   
34545 20 64 6f 75 62 6c 65 20 63 6f 6f 72 64 20 3d 20   double coord = 
34546 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f  DCOORD(cell.aCoo
34547 72 64 5b 70 2d 3e 69 43 6f 6f 72 64 5d 29 3b 0a  rd[p->iCoord]);.
34548 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
34549 20 61 73 73 65 72 74 28 70 2d 3e 6f 70 3d 3d 52   assert(p->op==R
3454a 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70  TREE_LE || p->op
3454b 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c 20 70 2d  ==RTREE_LT || p-
3454c 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45 20 0a 20  >op==RTREE_GE . 
3454d 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d         || p->op=
3454e 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20 70 2d 3e  =RTREE_GT || p->
3454f 6f 70 3d 3d 52 54 52 45 45 5f 45 51 0a 20 20 20  op==RTREE_EQ.   
34550 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20   );.    switch( 
34551 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63  p->op ){.      c
34552 61 73 65 20 52 54 52 45 45 5f 4c 45 3a 20 72 65  ase RTREE_LE: re
34553 73 20 3d 20 28 63 6f 6f 72 64 3c 3d 70 2d 3e 72  s = (coord<=p->r
34554 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a 20  Value); break;. 
34555 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
34556 4c 54 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64  LT: res = (coord
34557 3c 70 2d 3e 72 56 61 6c 75 65 29 3b 20 20 62 72  <p->rValue);  br
34558 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
34559 52 54 52 45 45 5f 47 45 3a 20 72 65 73 20 3d 20  RTREE_GE: res = 
3455a 28 63 6f 6f 72 64 3e 3d 70 2d 3e 72 56 61 6c 75  (coord>=p->rValu
3455b 65 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  e); break;.     
3455c 20 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a 20   case RTREE_GT: 
3455d 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e 70 2d 3e  res = (coord>p->
3455e 72 56 61 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b  rValue);  break;
3455f 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45  .      case RTRE
34560 45 5f 45 51 3a 20 72 65 73 20 3d 20 28 63 6f 6f  E_EQ: res = (coo
34561 72 64 3d 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20  rd==p->rValue); 
34562 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
34563 20 20 69 66 28 20 21 72 65 73 20 29 20 72 65 74    if( !res ) ret
34564 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65  urn 1;.  }..  re
34565 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
34566 20 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20   Cursor pCursor 
34567 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73  currently points
34568 20 61 74 20 61 20 6e 6f 64 65 20 74 68 61 74 20   at a node that 
34569 68 65 61 64 73 20 61 20 73 75 62 2d 74 72 65 65  heads a sub-tree
3456a 20 6f 66 0a 2a 2a 20 68 65 69 67 68 74 20 69 48   of.** height iH
3456b 65 69 67 68 74 20 28 69 66 20 69 48 65 69 67 68  eight (if iHeigh
3456c 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 6e  t==0, then the n
3456d 6f 64 65 20 69 73 20 61 20 6c 65 61 66 29 2e 20  ode is a leaf). 
3456e 44 65 73 63 65 6e 64 0a 2a 2a 20 74 6f 20 70 6f  Descend.** to po
3456f 69 6e 74 20 74 6f 20 74 68 65 20 6c 65 66 74 2d  int to the left-
34570 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 20 74 68 65  most cell of the
34571 20 73 75 62 2d 74 72 65 65 20 74 68 61 74 20 6d   sub-tree that m
34572 61 74 63 68 65 73 20 74 68 65 20 0a 2a 2a 20 63  atches the .** c
34573 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e 73 74 72  onfigured constr
34574 61 69 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  aints..*/.static
34575 20 69 6e 74 20 64 65 73 63 65 6e 64 54 6f 43 65   int descendToCe
34576 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ll(.  Rtree *pRt
34577 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43 75 72  ree, .  RtreeCur
34578 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 0a 20  sor *pCursor, . 
34579 20 69 6e 74 20 69 48 65 69 67 68 74 2c 0a 20 20   int iHeight,.  
3457a 69 6e 74 20 2a 70 45 6f 66 20 20 20 20 20 20 20  int *pEof       
3457b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
3457c 3a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66  : Set to true if
3457d 20 63 61 6e 6e 6f 74 20 64 65 73 63 65 6e 64 20   cannot descend 
3457e 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 73 45 6f  */.){.  int isEo
3457f 66 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  f;.  int rc;.  i
34580 6e 74 20 69 69 3b 0a 20 20 52 74 72 65 65 4e 6f  nt ii;.  RtreeNo
34581 64 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20 73 71  de *pChild;.  sq
34582 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
34583 69 64 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65  id;..  RtreeNode
34584 20 2a 70 53 61 76 65 64 4e 6f 64 65 20 3d 20 70   *pSavedNode = p
34585 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 3b 0a 20  Cursor->pNode;. 
34586 20 69 6e 74 20 69 53 61 76 65 64 43 65 6c 6c 20   int iSavedCell 
34587 3d 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c  = pCursor->iCell
34588 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 48 65  ;..  assert( iHe
34589 69 67 68 74 3e 3d 30 20 29 3b 0a 0a 20 20 69 66  ight>=0 );..  if
3458a 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a  ( iHeight==0 ){.
3458b 20 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73 74      isEof = test
3458c 52 74 72 65 65 45 6e 74 72 79 28 70 52 74 72 65  RtreeEntry(pRtre
3458d 65 2c 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  e, pCursor);.  }
3458e 65 6c 73 65 7b 0a 20 20 20 20 69 73 45 6f 66 20  else{.    isEof 
3458f 3d 20 74 65 73 74 52 74 72 65 65 43 65 6c 6c 28  = testRtreeCell(
34590 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 29  pRtree, pCursor)
34591 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 45 6f  ;.  }.  if( isEo
34592 66 20 7c 7c 20 69 48 65 69 67 68 74 3d 3d 30 20  f || iHeight==0 
34593 29 7b 0a 20 20 20 20 2a 70 45 6f 66 20 3d 20 69  ){.    *pEof = i
34594 73 45 6f 66 3b 0a 20 20 20 20 72 65 74 75 72 6e  sEof;.    return
34595 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
34596 0a 20 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  .  iRowid = node
34597 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
34598 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c   pCursor->pNode,
34599 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 29   pCursor->iCell)
3459a 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71  ;.  rc = nodeAcq
3459b 75 69 72 65 28 70 52 74 72 65 65 2c 20 69 52 6f  uire(pRtree, iRo
3459c 77 69 64 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e  wid, pCursor->pN
3459d 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a 20  ode, &pChild);. 
3459e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3459f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
345a0 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 6e 6f 64 65   rc;.  }..  node
345a1 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
345a2 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 29 3b  pCursor->pNode);
345a3 0a 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64  .  pCursor->pNod
345a4 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 69 73  e = pChild;.  is
345a5 45 6f 66 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69  Eof = 1;.  for(i
345a6 69 3d 30 3b 20 69 73 45 6f 66 20 26 26 20 69 69  i=0; isEof && ii
345a7 3c 4e 43 45 4c 4c 28 70 43 68 69 6c 64 29 3b 20  <NCELL(pChild); 
345a8 69 69 2b 2b 29 7b 0a 20 20 20 20 70 43 75 72 73  ii++){.    pCurs
345a9 6f 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 69 3b 0a  or->iCell = ii;.
345aa 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64      rc = descend
345ab 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  ToCell(pRtree, p
345ac 43 75 72 73 6f 72 2c 20 69 48 65 69 67 68 74 2d  Cursor, iHeight-
345ad 31 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20  1, &isEof);.    
345ae 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
345af 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
345b0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
345b1 0a 20 20 69 66 28 20 69 73 45 6f 66 20 29 7b 0a  .  if( isEof ){.
345b2 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
345b3 73 6f 72 2d 3e 70 4e 6f 64 65 3d 3d 70 43 68 69  sor->pNode==pChi
345b4 6c 64 20 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ld );.    nodeRe
345b5 66 65 72 65 6e 63 65 28 70 53 61 76 65 64 4e 6f  ference(pSavedNo
345b6 64 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 6c  de);.    nodeRel
345b7 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68  ease(pRtree, pCh
345b8 69 6c 64 29 3b 0a 20 20 20 20 70 43 75 72 73 6f  ild);.    pCurso
345b9 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 53 61 76 65  r->pNode = pSave
345ba 64 4e 6f 64 65 3b 0a 20 20 20 20 70 43 75 72 73  dNode;.    pCurs
345bb 6f 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 53 61 76  or->iCell = iSav
345bc 65 64 43 65 6c 6c 3b 0a 20 20 7d 0a 0a 20 20 2a  edCell;.  }..  *
345bd 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20 20  pEof = isEof;.  
345be 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
345bf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 65 20 6f  ;.}../*.** One o
345c0 66 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 6e  f the cells in n
345c1 6f 64 65 20 70 4e 6f 64 65 20 69 73 20 67 75 61  ode pNode is gua
345c2 72 61 6e 74 65 65 64 20 74 6f 20 68 61 76 65 20  ranteed to have 
345c3 61 20 36 34 2d 62 69 74 20 0a 2a 2a 20 69 6e 74  a 64-bit .** int
345c4 65 67 65 72 20 76 61 6c 75 65 20 65 71 75 61 6c  eger value equal
345c5 20 74 6f 20 69 52 6f 77 69 64 2e 20 52 65 74 75   to iRowid. Retu
345c6 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
345c7 74 68 69 73 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74  this cell..*/.st
345c8 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 52 6f 77  atic int nodeRow
345c9 69 64 49 6e 64 65 78 28 52 74 72 65 65 20 2a 70  idIndex(Rtree *p
345ca 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
345cb 20 2a 70 4e 6f 64 65 2c 20 69 36 34 20 69 52 6f   *pNode, i64 iRo
345cc 77 69 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  wid){.  int ii;.
345cd 20 20 66 6f 72 28 69 69 3d 30 3b 20 6e 6f 64 65    for(ii=0; node
345ce 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
345cf 20 70 4e 6f 64 65 2c 20 69 69 29 21 3d 69 52 6f   pNode, ii)!=iRo
345d0 77 69 64 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  wid; ii++){.    
345d1 61 73 73 65 72 74 28 20 69 69 3c 28 4e 43 45 4c  assert( ii<(NCEL
345d2 4c 28 70 4e 6f 64 65 29 2d 31 29 20 29 3b 0a 20  L(pNode)-1) );. 
345d3 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 69 3b 0a   }.  return ii;.
345d4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
345d5 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
345d6 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e 67   cell containing
345d7 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 6e 6f   a pointer to no
345d8 64 65 20 70 4e 6f 64 65 0a 2a 2a 20 69 6e 20 69  de pNode.** in i
345d9 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 70 4e  ts parent. If pN
345da 6f 64 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20  ode is the root 
345db 6e 6f 64 65 2c 20 72 65 74 75 72 6e 20 2d 31 2e  node, return -1.
345dc 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
345dd 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 52  odeParentIndex(R
345de 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
345df 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b  reeNode *pNode){
345e0 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50  .  RtreeNode *pP
345e1 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70  arent = pNode->p
345e2 50 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70 50  Parent;.  if( pP
345e3 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 72 65 74  arent ){.    ret
345e4 75 72 6e 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64  urn nodeRowidInd
345e5 65 78 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ex(pRtree, pPare
345e6 6e 74 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  nt, pNode->iNode
345e7 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
345e8 2d 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  -1;.}../* .** Rt
345e9 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
345ea 65 20 6d 6f 64 75 6c 65 20 78 4e 65 78 74 20 6d  e module xNext m
345eb 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
345ec 20 69 6e 74 20 72 74 72 65 65 4e 65 78 74 28 73   int rtreeNext(s
345ed 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
345ee 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 29  or *pVtabCursor)
345ef 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  {.  Rtree *pRtre
345f0 65 20 3d 20 28 52 74 72 65 65 20 2a 29 28 70 56  e = (Rtree *)(pV
345f1 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
345f2 29 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72  );.  RtreeCursor
345f3 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43   *pCsr = (RtreeC
345f4 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75 72  ursor *)pVtabCur
345f5 73 6f 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  sor;.  int rc = 
345f6 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
345f7 28 20 70 43 73 72 2d 3e 69 53 74 72 61 74 65 67  ( pCsr->iStrateg
345f8 79 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54  y==1 ){.    /* T
345f9 68 69 73 20 22 73 63 61 6e 22 20 69 73 20 61 20  his "scan" is a 
345fa 64 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20 62 79  direct lookup by
345fb 20 72 6f 77 69 64 2e 20 54 68 65 72 65 20 69 73   rowid. There is
345fc 20 6e 6f 20 6e 65 78 74 20 65 6e 74 72 79 2e 20   no next entry. 
345fd 2a 2f 0a 20 20 20 20 6e 6f 64 65 52 65 6c 65 61  */.    nodeRelea
345fe 73 65 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d  se(pRtree, pCsr-
345ff 3e 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 43 73  >pNode);.    pCs
34600 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20  r->pNode = 0;.  
34601 7d 0a 0a 20 20 65 6c 73 65 20 69 66 28 20 70 43  }..  else if( pC
34602 73 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20 20 20  sr->pNode ){.   
34603 20 2f 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20   /* Move to the 
34604 6e 65 78 74 20 65 6e 74 72 79 20 74 68 61 74 20  next entry that 
34605 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f 6e 66  matches the conf
34606 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  igured constrain
34607 74 73 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ts. */.    int i
34608 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20  Height = 0;.    
34609 77 68 69 6c 65 28 20 70 43 73 72 2d 3e 70 4e 6f  while( pCsr->pNo
3460a 64 65 20 29 7b 0a 20 20 20 20 20 20 52 74 72 65  de ){.      Rtre
3460b 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 20 3d 20 70  eNode *pNode = p
3460c 43 73 72 2d 3e 70 4e 6f 64 65 3b 0a 20 20 20 20  Csr->pNode;.    
3460d 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
3460e 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ELL(pNode);.    
3460f 20 20 66 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c    for(pCsr->iCel
34610 6c 2b 2b 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c  l++; pCsr->iCell
34611 3c 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43  <nCell; pCsr->iC
34612 65 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ell++){.        
34613 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20 20 20 20  int isEof;.     
34614 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64 54     rc = descendT
34615 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  oCell(pRtree, pC
34616 73 72 2c 20 69 48 65 69 67 68 74 2c 20 26 69 73  sr, iHeight, &is
34617 45 6f 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Eof);.        if
34618 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
34619 7c 7c 20 21 69 73 45 6f 66 20 29 7b 0a 20 20 20  || !isEof ){.   
3461a 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
3461b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
3461c 20 20 7d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e    }.      pCsr->
3461d 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 2d 3e 70  pNode = pNode->p
3461e 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 70 43  Parent;.      pC
3461f 73 72 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  sr->iCell = node
34620 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72  ParentIndex(pRtr
34621 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ee, pNode);.    
34622 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
34623 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20  pCsr->pNode);.  
34624 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
34625 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
34626 20 20 20 20 20 20 69 48 65 69 67 68 74 2b 2b 3b        iHeight++;
34627 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
34628 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a  turn rc;.}../* .
34629 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c  ** Rtree virtual
3462a 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 52   table module xR
3462b 6f 77 69 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  owid method..*/.
3462c 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
3462d 52 6f 77 69 64 28 73 71 6c 69 74 65 33 5f 76 74  Rowid(sqlite3_vt
3462e 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
3462f 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69  Cursor, sqlite_i
34630 6e 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20  nt64 *pRowid){. 
34631 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d   Rtree *pRtree =
34632 20 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 43   (Rtree *)pVtabC
34633 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20  ursor->pVtab;.  
34634 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73  RtreeCursor *pCs
34635 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72  r = (RtreeCursor
34636 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a   *)pVtabCursor;.
34637 0a 20 20 61 73 73 65 72 74 28 70 43 73 72 2d 3e  .  assert(pCsr->
34638 70 4e 6f 64 65 29 3b 0a 20 20 2a 70 52 6f 77 69  pNode);.  *pRowi
34639 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64  d = nodeGetRowid
3463a 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70  (pRtree, pCsr->p
3463b 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c  Node, pCsr->iCel
3463c 6c 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  l);..  return SQ
3463d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a  LITE_OK;.}../* .
3463e 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c  ** Rtree virtual
3463f 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43   table module xC
34640 6f 6c 75 6d 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f  olumn method..*/
34641 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
34642 65 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f  eColumn(sqlite3_
34643 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72  vtab_cursor *cur
34644 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  , sqlite3_contex
34645 74 20 2a 63 74 78 2c 20 69 6e 74 20 69 29 7b 0a  t *ctx, int i){.
34646 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20    Rtree *pRtree 
34647 3d 20 28 52 74 72 65 65 20 2a 29 63 75 72 2d 3e  = (Rtree *)cur->
34648 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75  pVtab;.  RtreeCu
34649 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74  rsor *pCsr = (Rt
3464a 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72 3b  reeCursor *)cur;
3464b 0a 0a 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a  ..  if( i==0 ){.
3464c 20 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d      i64 iRowid =
3464d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52   nodeGetRowid(pR
3464e 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64  tree, pCsr->pNod
3464f 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b  e, pCsr->iCell);
34650 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
34651 75 6c 74 5f 69 6e 74 36 34 28 63 74 78 2c 20 69  ult_int64(ctx, i
34652 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b  Rowid);.  }else{
34653 0a 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20  .    RtreeCoord 
34654 63 3b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 6f  c;.    nodeGetCo
34655 6f 72 64 28 70 52 74 72 65 65 2c 20 70 43 73 72  ord(pRtree, pCsr
34656 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69  ->pNode, pCsr->i
34657 43 65 6c 6c 2c 20 69 2d 31 2c 20 26 63 29 3b 0a  Cell, i-1, &c);.
34658 20 20 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e      if( pRtree->
34659 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
3465a 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29  E_COORD_REAL32 )
3465b 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
3465c 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 74  result_double(ct
3465d 78 2c 20 63 2e 66 29 3b 0a 20 20 20 20 7d 65 6c  x, c.f);.    }el
3465e 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
3465f 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64  ( pRtree->eCoord
34660 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52  Type==RTREE_COOR
34661 44 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20  D_INT32 );.     
34662 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
34663 69 6e 74 28 63 74 78 2c 20 63 2e 69 29 3b 0a 20  int(ctx, c.i);. 
34664 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
34665 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
34666 0a 2f 2a 20 0a 2a 2a 20 55 73 65 20 6e 6f 64 65  ./* .** Use node
34667 41 63 71 75 69 72 65 28 29 20 74 6f 20 6f 62 74  Acquire() to obt
34668 61 69 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64  ain the leaf nod
34669 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
3466a 20 72 65 63 6f 72 64 20 77 69 74 68 20 0a 2a 2a   record with .**
3466b 20 72 6f 77 69 64 20 69 52 6f 77 69 64 2e 20 49   rowid iRowid. I
3466c 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65  f successful, se
3466d 74 20 2a 70 70 4c 65 61 66 20 74 6f 20 70 6f 69  t *ppLeaf to poi
3466e 6e 74 20 74 6f 20 74 68 65 20 6e 6f 64 65 20 61  nt to the node a
3466f 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c  nd.** return SQL
34670 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72 65  ITE_OK. If there
34671 20 69 73 20 6e 6f 20 73 75 63 68 20 72 65 63 6f   is no such reco
34672 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  rd in the table,
34673 20 73 65 74 0a 2a 2a 20 2a 70 70 4c 65 61 66 20   set.** *ppLeaf 
34674 74 6f 20 30 20 61 6e 64 20 72 65 74 75 72 6e 20  to 0 and return 
34675 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e  SQLITE_OK. If an
34676 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 73   error occurs, s
34677 65 74 20 2a 70 70 4c 65 61 66 0a 2a 2a 20 74 6f  et *ppLeaf.** to
34678 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
34679 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
3467a 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
3467b 20 69 6e 74 20 66 69 6e 64 4c 65 61 66 4e 6f 64   int findLeafNod
3467c 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
3467d 20 69 36 34 20 69 52 6f 77 69 64 2c 20 52 74 72   i64 iRowid, Rtr
3467e 65 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66 29  eeNode **ppLeaf)
3467f 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 2a 70  {.  int rc;.  *p
34680 70 4c 65 61 66 20 3d 20 30 3b 0a 20 20 73 71 6c  pLeaf = 0;.  sql
34681 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
34682 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
34683 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a  id, 1, iRowid);.
34684 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74    if( sqlite3_st
34685 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ep(pRtree->pRead
34686 52 6f 77 69 64 29 3d 3d 53 51 4c 49 54 45 5f 52  Rowid)==SQLITE_R
34687 4f 57 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4e  OW ){.    i64 iN
34688 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ode = sqlite3_co
34689 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72 65  lumn_int64(pRtre
3468a 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 30  e->pReadRowid, 0
3468b 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  );.    rc = node
3468c 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20  Acquire(pRtree, 
3468d 69 4e 6f 64 65 2c 20 30 2c 20 70 70 4c 65 61 66  iNode, 0, ppLeaf
3468e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
3468f 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
34690 61 64 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73  adRowid);.  }els
34691 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
34692 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
34693 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a 20  ->pReadRowid);. 
34694 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
34695 7d 0a 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  }.../* .** Rtree
34696 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
34697 6f 64 75 6c 65 20 78 46 69 6c 74 65 72 20 6d 65  odule xFilter me
34698 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
34699 69 6e 74 20 72 74 72 65 65 46 69 6c 74 65 72 28  int rtreeFilter(
3469a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
3469b 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
3469c 73 6f 72 2c 20 0a 20 20 69 6e 74 20 69 64 78 4e  sor, .  int idxN
3469d 75 6d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  um, const char *
3469e 69 64 78 53 74 72 2c 0a 20 20 69 6e 74 20 61 72  idxStr,.  int ar
3469f 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
346a0 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 52 74  e **argv.){.  Rt
346a1 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
346a2 74 72 65 65 20 2a 29 70 56 74 61 62 43 75 72 73  tree *)pVtabCurs
346a3 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 52 74 72  or->pVtab;.  Rtr
346a4 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
346a5 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
346a6 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 0a 20 20  pVtabCursor;..  
346a7 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f 74  RtreeNode *pRoot
346a8 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a   = 0;.  int ii;.
346a9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
346aa 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65  E_OK;..  rtreeRe
346ab 66 65 72 65 6e 63 65 28 70 52 74 72 65 65 29 3b  ference(pRtree);
346ac 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
346ad 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  (pCsr->aConstrai
346ae 6e 74 29 3b 0a 20 20 70 43 73 72 2d 3e 61 43 6f  nt);.  pCsr->aCo
346af 6e 73 74 72 61 69 6e 74 20 3d 20 30 3b 0a 20 20  nstraint = 0;.  
346b0 70 43 73 72 2d 3e 69 53 74 72 61 74 65 67 79 20  pCsr->iStrategy 
346b1 3d 20 69 64 78 4e 75 6d 3b 0a 0a 20 20 69 66 28  = idxNum;..  if(
346b2 20 69 64 78 4e 75 6d 3d 3d 31 20 29 7b 0a 20 20   idxNum==1 ){.  
346b3 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
346b4 65 20 2d 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f  e - lookup by ro
346b5 77 69 64 2e 20 2a 2f 0a 20 20 20 20 52 74 72 65  wid. */.    Rtre
346b6 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b 20 20 20  eNode *pLeaf;   
346b7 20 20 20 20 20 2f 2a 20 4c 65 61 66 20 6f 6e 20       /* Leaf on 
346b8 77 68 69 63 68 20 74 68 65 20 72 65 71 75 69 72  which the requir
346b9 65 64 20 63 65 6c 6c 20 72 65 73 69 64 65 73 20  ed cell resides 
346ba 2a 2f 0a 20 20 20 20 69 36 34 20 69 52 6f 77 69  */.    i64 iRowi
346bb 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
346bc 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29  e_int64(argv[0])
346bd 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
346be 65 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  eafNode(pRtree, 
346bf 69 52 6f 77 69 64 2c 20 26 70 4c 65 61 66 29 3b  iRowid, &pLeaf);
346c0 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65  .    pCsr->pNode
346c1 20 3d 20 70 4c 65 61 66 3b 20 0a 20 20 20 20 69   = pLeaf; .    i
346c2 66 28 20 70 4c 65 61 66 20 26 26 20 72 63 3d 3d  f( pLeaf && rc==
346c3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
346c4 20 20 20 70 43 73 72 2d 3e 69 43 65 6c 6c 20 3d     pCsr->iCell =
346c5 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28   nodeRowidIndex(
346c6 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 69  pRtree, pLeaf, i
346c7 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  Rowid);.    }.  
346c8 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 6f  }else{.    /* No
346c9 72 6d 61 6c 20 63 61 73 65 20 2d 20 72 2d 74 72  rmal case - r-tr
346ca 65 65 20 73 63 61 6e 2e 20 53 65 74 20 75 70 20  ee scan. Set up 
346cb 74 68 65 20 52 74 72 65 65 43 75 72 73 6f 72 2e  the RtreeCursor.
346cc 61 43 6f 6e 73 74 72 61 69 6e 74 20 61 72 72 61  aConstraint arra
346cd 79 20 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74  y .    ** with t
346ce 68 65 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f  he configured co
346cf 6e 73 74 72 61 69 6e 74 73 2e 20 0a 20 20 20 20  nstraints. .    
346d0 2a 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63 3e  */.    if( argc>
346d1 30 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72 2d  0 ){.      pCsr-
346d2 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 73  >aConstraint = s
346d3 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
346d4 7a 65 6f 66 28 52 74 72 65 65 43 6f 6e 73 74 72  zeof(RtreeConstr
346d5 61 69 6e 74 29 2a 61 72 67 63 29 3b 0a 20 20 20  aint)*argc);.   
346d6 20 20 20 70 43 73 72 2d 3e 6e 43 6f 6e 73 74 72     pCsr->nConstr
346d7 61 69 6e 74 20 3d 20 61 72 67 63 3b 0a 20 20 20  aint = argc;.   
346d8 20 20 20 69 66 28 20 21 70 43 73 72 2d 3e 61 43     if( !pCsr->aC
346d9 6f 6e 73 74 72 61 69 6e 74 20 29 7b 0a 20 20 20  onstraint ){.   
346da 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
346db 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65  _NOMEM;.      }e
346dc 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
346dd 65 72 74 28 20 28 69 64 78 53 74 72 3d 3d 30 20  ert( (idxStr==0 
346de 26 26 20 61 72 67 63 3d 3d 30 29 20 7c 7c 20 73  && argc==0) || s
346df 74 72 6c 65 6e 28 69 64 78 53 74 72 29 3d 3d 61  trlen(idxStr)==a
346e0 72 67 63 2a 32 20 29 3b 0a 20 20 20 20 20 20 20  rgc*2 );.       
346e1 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 61 72   for(ii=0; ii<ar
346e2 67 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  gc; ii++){.     
346e3 20 20 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72       RtreeConstr
346e4 61 69 6e 74 20 2a 70 20 3d 20 26 70 43 73 72 2d  aint *p = &pCsr-
346e5 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d  >aConstraint[ii]
346e6 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6f  ;.          p->o
346e7 70 20 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 5d  p = idxStr[ii*2]
346e8 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69  ;.          p->i
346e9 43 6f 6f 72 64 20 3d 20 69 64 78 53 74 72 5b 69  Coord = idxStr[i
346ea 69 2a 32 2b 31 5d 2d 27 61 27 3b 0a 20 20 20 20  i*2+1]-'a';.    
346eb 20 20 20 20 20 20 70 2d 3e 72 56 61 6c 75 65 20        p->rValue 
346ec 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
346ed 64 6f 75 62 6c 65 28 61 72 67 76 5b 69 69 5d 29  double(argv[ii])
346ee 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
346ef 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
346f0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
346f1 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72  OK ){.      pCsr
346f2 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20  ->pNode = 0;.   
346f3 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75     rc = nodeAcqu
346f4 69 72 65 28 70 52 74 72 65 65 2c 20 31 2c 20 30  ire(pRtree, 1, 0
346f5 2c 20 26 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d  , &pRoot);.    }
346f6 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
346f7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
346f8 69 6e 74 20 69 73 45 6f 66 20 3d 20 31 3b 0a 20  int isEof = 1;. 
346f9 20 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d       int nCell =
346fa 20 4e 43 45 4c 4c 28 70 52 6f 6f 74 29 3b 0a 20   NCELL(pRoot);. 
346fb 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
346fc 20 3d 20 70 52 6f 6f 74 3b 0a 20 20 20 20 20 20   = pRoot;.      
346fd 66 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c 6c 3d  for(pCsr->iCell=
346fe 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
346ff 20 26 26 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c   && pCsr->iCell<
34700 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43 65  nCell; pCsr->iCe
34701 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61  ll++){.        a
34702 73 73 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f  ssert( pCsr->pNo
34703 64 65 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20  de==pRoot );.   
34704 20 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e       rc = descen
34705 64 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  dToCell(pRtree, 
34706 70 43 73 72 2c 20 70 52 74 72 65 65 2d 3e 69 44  pCsr, pRtree->iD
34707 65 70 74 68 2c 20 26 69 73 45 6f 66 29 3b 0a 20  epth, &isEof);. 
34708 20 20 20 20 20 20 20 69 66 28 20 21 69 73 45 6f         if( !isEo
34709 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  f ){.          b
3470a 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
3470b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
3470c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3470d 26 26 20 69 73 45 6f 66 20 29 7b 0a 20 20 20 20  && isEof ){.    
3470e 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73 72      assert( pCsr
3470f 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f 6f 74 20 29  ->pNode==pRoot )
34710 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 52 65  ;.        nodeRe
34711 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52  lease(pRtree, pR
34712 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 70 43  oot);.        pC
34713 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20  sr->pNode = 0;. 
34714 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
34715 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
34716 4f 4b 20 7c 7c 20 21 70 43 73 72 2d 3e 70 4e 6f  OK || !pCsr->pNo
34717 64 65 20 7c 7c 20 70 43 73 72 2d 3e 69 43 65 6c  de || pCsr->iCel
34718 6c 3c 4e 43 45 4c 4c 28 70 43 73 72 2d 3e 70 4e  l<NCELL(pCsr->pN
34719 6f 64 65 29 20 29 3b 0a 20 20 20 20 7d 0a 20 20  ode) );.    }.  
3471a 7d 0a 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73  }..  rtreeReleas
3471b 65 28 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74  e(pRtree);.  ret
3471c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
3471d 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
3471e 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 42 65 73  able module xBes
3471f 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 2e 20 54  tIndex method. T
34720 68 65 72 65 20 61 72 65 20 74 68 72 65 65 0a 2a  here are three.*
34721 2a 20 74 61 62 6c 65 20 73 63 61 6e 20 73 74 72  * table scan str
34722 61 74 65 67 69 65 73 20 74 6f 20 63 68 6f 6f 73  ategies to choos
34723 65 20 66 72 6f 6d 20 28 69 6e 20 6f 72 64 65 72  e from (in order
34724 20 66 72 6f 6d 20 6d 6f 73 74 20 74 6f 20 0a 2a   from most to .*
34725 2a 20 6c 65 61 73 74 20 64 65 73 69 72 61 62 6c  * least desirabl
34726 65 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 64 78 4e  e):.**.**   idxN
34727 75 6d 20 20 20 20 20 69 64 78 53 74 72 20 20 20  um     idxStr   
34728 20 20 20 20 20 53 74 72 61 74 65 67 79 0a 2a 2a       Strategy.**
34729 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
3472a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3472b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3472c 2d 2d 2d 0a 2a 2a 20 20 20 20 20 31 20 20 20 20  ---.**     1    
3472d 20 20 20 20 55 6e 75 73 65 64 20 20 20 20 20 20      Unused      
3472e 20 20 44 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20    Direct lookup 
3472f 62 79 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20  by rowid..**    
34730 20 32 20 20 20 20 20 20 20 20 53 65 65 20 62 65   2        See be
34731 6c 6f 77 20 20 20 20 20 52 2d 74 72 65 65 20 71  low     R-tree q
34732 75 65 72 79 2e 0a 2a 2a 20 20 20 20 20 33 20 20  uery..**     3  
34733 20 20 20 20 20 20 55 6e 75 73 65 64 20 20 20 20        Unused    
34734 20 20 20 20 46 75 6c 6c 20 74 61 62 6c 65 20 73      Full table s
34735 63 61 6e 2e 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  can..**   ------
34736 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
34737 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
34738 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a  ----------.**.**
34739 20 49 66 20 73 74 72 61 74 65 67 79 20 31 20 6f   If strategy 1 o
3473a 72 20 33 20 69 73 20 75 73 65 64 2c 20 74 68 65  r 3 is used, the
3473b 6e 20 69 64 78 53 74 72 20 69 73 20 6e 6f 74 20  n idxStr is not 
3473c 6d 65 61 6e 69 6e 67 66 75 6c 2e 20 49 66 20 73  meaningful. If s
3473d 74 72 61 74 65 67 79 0a 2a 2a 20 32 20 69 73 20  trategy.** 2 is 
3473e 75 73 65 64 2c 20 69 64 78 53 74 72 20 69 73 20  used, idxStr is 
3473f 66 6f 72 6d 61 74 74 65 64 20 74 6f 20 63 6f 6e  formatted to con
34740 74 61 69 6e 20 32 20 62 79 74 65 73 20 66 6f 72  tain 2 bytes for
34741 20 65 61 63 68 20 0a 2a 2a 20 63 6f 6e 73 74 72   each .** constr
34742 61 69 6e 74 20 75 73 65 64 2e 20 54 68 65 20 66  aint used. The f
34743 69 72 73 74 20 74 77 6f 20 62 79 74 65 73 20 6f  irst two bytes o
34744 66 20 69 64 78 53 74 72 20 63 6f 72 72 65 73 70  f idxStr corresp
34745 6f 6e 64 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63  ond to .** the c
34746 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 73 71 6c  onstraint in sql
34747 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e  ite3_index_info.
34748 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
34749 5b 5d 20 77 69 74 68 0a 2a 2a 20 28 61 72 67 76  [] with.** (argv
3474a 49 6e 64 65 78 3d 3d 31 29 20 65 74 63 2e 0a 2a  Index==1) etc..*
3474b 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6f  *.** The first o
3474c 66 20 65 61 63 68 20 70 61 69 72 20 6f 66 20 62  f each pair of b
3474d 79 74 65 73 20 69 6e 20 69 64 78 53 74 72 20 69  ytes in idxStr i
3474e 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 63 6f  dentifies the co
3474f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 6f 70 65 72  nstraint.** oper
34750 61 74 6f 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ator as follows:
34751 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 65 72 61 74 6f  .**.**   Operato
34752 72 20 20 20 20 42 79 74 65 20 56 61 6c 75 65 0a  r    Byte Value.
34753 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
34754 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
34755 20 20 20 20 3d 20 20 20 20 20 20 20 20 30 78 34      =        0x4
34756 31 20 28 27 41 27 29 0a 2a 2a 20 20 20 20 20 3c  1 ('A').**     <
34757 3d 20 20 20 20 20 20 20 20 30 78 34 32 20 28 27  =        0x42 ('
34758 42 27 29 0a 2a 2a 20 20 20 20 20 20 3c 20 20 20  B').**      <   
34759 20 20 20 20 20 30 78 34 33 20 28 27 43 27 29 0a       0x43 ('C').
3475a 2a 2a 20 20 20 20 20 3e 3d 20 20 20 20 20 20 20  **     >=       
3475b 20 30 78 34 34 20 28 27 44 27 29 0a 2a 2a 20 20   0x44 ('D').**  
3475c 20 20 20 20 3e 20 20 20 20 20 20 20 20 30 78 34      >        0x4
3475d 35 20 28 27 45 27 29 0a 2a 2a 20 20 20 2d 2d 2d  5 ('E').**   ---
3475e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3475f 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  ---.**.** The se
34760 63 6f 6e 64 20 6f 66 20 65 61 63 68 20 70 61 69  cond of each pai
34761 72 20 6f 66 20 62 79 74 65 73 20 69 64 65 6e 74  r of bytes ident
34762 69 66 69 65 73 20 74 68 65 20 63 6f 6f 72 64 69  ifies the coordi
34763 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74  nate column.** t
34764 6f 20 77 68 69 63 68 20 74 68 65 20 63 6f 6e 73  o which the cons
34765 74 72 61 69 6e 74 20 61 70 70 6c 69 65 73 2e 20  traint applies. 
34766 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6f  The leftmost coo
34767 72 64 69 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a  rdinate column.*
34768 2a 20 69 73 20 27 61 27 2c 20 74 68 65 20 73 65  * is 'a', the se
34769 63 6f 6e 64 20 66 72 6f 6d 20 74 68 65 20 6c 65  cond from the le
3476a 66 74 20 27 62 27 20 65 74 63 2e 0a 2a 2f 0a 73  ft 'b' etc..*/.s
3476b 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 42  tatic int rtreeB
3476c 65 73 74 49 6e 64 65 78 28 73 71 6c 69 74 65 33  estIndex(sqlite3
3476d 5f 76 74 61 62 20 2a 74 61 62 2c 20 73 71 6c 69  _vtab *tab, sqli
3476e 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
3476f 70 49 64 78 49 6e 66 6f 29 7b 0a 20 20 69 6e 74  pIdxInfo){.  int
34770 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
34771 0a 20 20 69 6e 74 20 69 69 2c 20 63 43 6f 6c 3b  .  int ii, cCol;
34772 0a 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30  ..  int iIdx = 0
34773 3b 0a 20 20 63 68 61 72 20 7a 49 64 78 53 74 72  ;.  char zIdxStr
34774 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  [RTREE_MAX_DIMEN
34775 53 49 4f 4e 53 2a 38 2b 31 5d 3b 0a 20 20 6d 65  SIONS*8+1];.  me
34776 6d 73 65 74 28 7a 49 64 78 53 74 72 2c 20 30 2c  mset(zIdxStr, 0,
34777 20 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72 29   sizeof(zIdxStr)
34778 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 49  );..  assert( pI
34779 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 3d 3d  dxInfo->idxStr==
3477a 30 20 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  0 );.  for(ii=0;
3477b 20 69 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43   ii<pIdxInfo->nC
3477c 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29  onstraint; ii++)
3477d 7b 0a 20 20 20 20 73 74 72 75 63 74 20 73 71 6c  {.    struct sql
3477e 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
3477f 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 49 64 78  raint *p = &pIdx
34780 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
34781 74 5b 69 69 5d 3b 0a 0a 20 20 20 20 69 66 28 20  t[ii];..    if( 
34782 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e  p->usable && p->
34783 69 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 70 2d  iColumn==0 && p-
34784 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  >op==SQLITE_INDE
34785 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20  X_CONSTRAINT_EQ 
34786 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 68  ){.      /* We h
34787 61 76 65 20 61 6e 20 65 71 75 61 6c 69 74 79 20  ave an equality 
34788 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68  constraint on th
34789 65 20 72 6f 77 69 64 2e 20 55 73 65 20 73 74 72  e rowid. Use str
3478a 61 74 65 67 79 20 31 2e 20 2a 2f 0a 20 20 20 20  ategy 1. */.    
3478b 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
3478c 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 69 69 3b  for(jj=0; jj<ii;
3478d 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   jj++){.        
3478e 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
3478f 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 61  raintUsage[jj].a
34790 72 67 76 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20  rgvIndex = 0;.  
34791 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
34792 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
34793 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 30 3b 0a 20  [jj].omit = 0;. 
34794 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 64       }.      pId
34795 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  xInfo->idxNum = 
34796 31 3b 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66  1;.      pIdxInf
34797 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
34798 61 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e 64 65  age[ii].argvInde
34799 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 49 64  x = 1;.      pId
3479a 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
3479b 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d 69 74  ntUsage[jj].omit
3479c 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   = 1;..      /* 
3479d 54 68 69 73 20 73 74 72 61 74 65 67 79 20 69 6e  This strategy in
3479e 76 6f 6c 76 65 73 20 61 20 74 77 6f 20 72 6f 77  volves a two row
3479f 69 64 20 6c 6f 6f 6b 75 70 73 20 6f 6e 20 61 6e  id lookups on an
347a0 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72   B-Tree structur
347a1 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  es.      ** and 
347a2 74 68 65 6e 20 61 20 6c 69 6e 65 61 72 20 73 65  then a linear se
347a3 61 72 63 68 20 6f 66 20 61 6e 20 52 2d 54 72 65  arch of an R-Tre
347a4 65 20 6e 6f 64 65 2e 20 54 68 69 73 20 73 68 6f  e node. This sho
347a5 75 6c 64 20 62 65 20 0a 20 20 20 20 20 20 2a 2a  uld be .      **
347a6 20 63 6f 6e 73 69 64 65 72 65 64 20 61 6c 6d 6f   considered almo
347a7 73 74 20 61 73 20 71 75 69 63 6b 20 61 73 20 61  st as quick as a
347a8 20 64 69 72 65 63 74 20 72 6f 77 69 64 20 6c 6f   direct rowid lo
347a9 6f 6b 75 70 20 28 66 6f 72 20 77 68 69 63 68 20  okup (for which 
347aa 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  .      ** sqlite
347ab 20 75 73 65 73 20 61 6e 20 69 6e 74 65 72 6e 61   uses an interna
347ac 6c 20 63 6f 73 74 20 6f 66 20 30 2e 30 29 2e 0a  l cost of 0.0)..
347ad 20 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20        */ .      
347ae 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61  pIdxInfo->estima
347af 74 65 64 43 6f 73 74 20 3d 20 31 30 2e 30 3b 0a  tedCost = 10.0;.
347b0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
347b1 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20  ITE_OK;.    }.. 
347b2 20 20 20 69 66 28 20 70 2d 3e 75 73 61 62 6c 65     if( p->usable
347b3 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3e 30   && p->iColumn>0
347b4 20 29 7b 0a 20 20 20 20 20 20 75 38 20 6f 70 20   ){.      u8 op 
347b5 3d 20 30 3b 0a 20 20 20 20 20 20 73 77 69 74 63  = 0;.      switc
347b6 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  h( p->op ){.    
347b7 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
347b8 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
347b9 5f 45 51 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f  _EQ: op = RTREE_
347ba 45 51 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  EQ; break;.     
347bb 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
347bc 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
347bd 47 54 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 47  GT: op = RTREE_G
347be 54 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  T; break;.      
347bf 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
347c0 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c  DEX_CONSTRAINT_L
347c1 45 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 45  E: op = RTREE_LE
347c2 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
347c3 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
347c4 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
347c5 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 54 3b  : op = RTREE_LT;
347c6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
347c7 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  case SQLITE_INDE
347c8 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 3a  X_CONSTRAINT_GE:
347c9 20 6f 70 20 3d 20 52 54 52 45 45 5f 47 45 3b 20   op = RTREE_GE; 
347ca 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
347cb 20 20 20 20 20 69 66 28 20 6f 70 20 29 7b 0a 20       if( op ){. 
347cc 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73         /* Make s
347cd 75 72 65 20 74 68 69 73 20 70 61 72 74 69 63 75  ure this particu
347ce 6c 61 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 68  lar constraint h
347cf 61 73 20 6e 6f 74 20 62 65 65 6e 20 75 73 65 64  as not been used
347d0 20 62 65 66 6f 72 65 2e 0a 20 20 20 20 20 20 20   before..       
347d1 20 2a 2a 20 49 66 20 69 74 20 68 61 73 20 62 65   ** If it has be
347d2 65 6e 20 75 73 65 64 20 62 65 66 6f 72 65 2c 20  en used before, 
347d3 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 20 20 20  ignore it..     
347d4 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
347d5 20 41 20 3c 3d 20 6f 72 20 3c 20 63 61 6e 20 62   A <= or < can b
347d6 65 20 75 73 65 64 20 69 66 20 74 68 65 72 65 20  e used if there 
347d7 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 20 6f 72  is a prior >= or
347d8 20 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41   >..        ** A
347d9 20 3e 3d 20 6f 72 20 3e 20 63 61 6e 20 62 65 20   >= or > can be 
347da 75 73 65 64 20 69 66 20 74 68 65 72 65 20 69 73  used if there is
347db 20 61 20 70 72 69 6f 72 20 3c 20 6f 72 20 3c 3d   a prior < or <=
347dc 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3c  ..        ** A <
347dd 3d 20 6f 72 20 3c 20 69 73 20 64 69 73 71 75 61  = or < is disqua
347de 6c 69 66 69 65 64 20 69 66 20 74 68 65 72 65 20  lified if there 
347df 69 73 20 61 20 70 72 69 6f 72 20 3c 3d 2c 20 3c  is a prior <=, <
347e0 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20  , or ==..       
347e1 20 2a 2a 20 41 20 3e 3d 20 6f 72 20 3e 20 69 73   ** A >= or > is
347e2 20 64 69 73 71 75 61 6c 69 66 69 65 64 20 69 66   disqualified if
347e3 20 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f   there is a prio
347e4 72 20 3e 3d 2c 20 3e 2c 20 6f 72 20 3d 3d 2e 0a  r >=, >, or ==..
347e5 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3d 3d 20          ** A == 
347e6 69 73 20 64 69 73 71 75 61 6c 69 66 65 64 20 69  is disqualifed i
347e7 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 70  f there is any p
347e8 72 69 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 2e  rior constraint.
347e9 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
347ea 20 20 20 20 69 6e 74 20 6a 2c 20 6f 70 6d 73 6b      int j, opmsk
347eb 3b 0a 20 20 20 20 20 20 20 20 73 74 61 74 69 63  ;.        static
347ec 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
347ed 63 68 61 72 20 63 6f 6d 70 61 74 69 62 6c 65 5b  char compatible[
347ee 5d 20 3d 20 7b 20 30 2c 20 30 2c 20 31 2c 20 31  ] = { 0, 0, 1, 1
347ef 2c 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20 20 20  , 2, 2 };.      
347f0 20 20 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74    assert( compat
347f1 69 62 6c 65 5b 52 54 52 45 45 5f 45 51 20 26 20  ible[RTREE_EQ & 
347f2 37 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  7]==0 );.       
347f3 20 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69   assert( compati
347f4 62 6c 65 5b 52 54 52 45 45 5f 4c 54 20 26 20 37  ble[RTREE_LT & 7
347f5 5d 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20  ]==1 );.        
347f6 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62  assert( compatib
347f7 6c 65 5b 52 54 52 45 45 5f 4c 45 20 26 20 37 5d  le[RTREE_LE & 7]
347f8 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ==1 );.        a
347f9 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c  ssert( compatibl
347fa 65 5b 52 54 52 45 45 5f 47 54 20 26 20 37 5d 3d  e[RTREE_GT & 7]=
347fb 3d 32 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =2 );.        as
347fc 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65  sert( compatible
347fd 5b 52 54 52 45 45 5f 47 45 20 26 20 37 5d 3d 3d  [RTREE_GE & 7]==
347fe 32 20 29 3b 0a 20 20 20 20 20 20 20 20 63 43 6f  2 );.        cCo
347ff 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 2d  l = p->iColumn -
34800 20 31 20 2b 20 27 61 27 3b 0a 20 20 20 20 20 20   1 + 'a';.      
34801 20 20 6f 70 6d 73 6b 20 3d 20 63 6f 6d 70 61 74    opmsk = compat
34802 69 62 6c 65 5b 6f 70 20 26 20 37 5d 3b 0a 20 20  ible[op & 7];.  
34803 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
34804 3c 69 49 64 78 3b 20 6a 2b 3d 32 29 7b 0a 20 20  <iIdx; j+=2){.  
34805 20 20 20 20 20 20 20 20 69 66 28 20 7a 49 64 78          if( zIdx
34806 53 74 72 5b 6a 2b 31 5d 3d 3d 63 43 6f 6c 20 26  Str[j+1]==cCol &
34807 26 20 28 63 6f 6d 70 61 74 69 62 6c 65 5b 7a 49  & (compatible[zI
34808 64 78 53 74 72 5b 6a 5d 20 26 20 37 5d 20 26 20  dxStr[j] & 7] & 
34809 6f 70 6d 73 6b 29 21 3d 30 20 29 7b 0a 20 20 20  opmsk)!=0 ){.   
3480a 20 20 20 20 20 20 20 20 20 6f 70 20 3d 20 30 3b           op = 0;
3480b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
3480c 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
3480d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3480e 7d 0a 20 20 20 20 20 20 69 66 28 20 6f 70 20 29  }.      if( op )
3480f 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
34810 28 20 69 49 64 78 3c 73 69 7a 65 6f 66 28 7a 49  ( iIdx<sizeof(zI
34811 64 78 53 74 72 29 2d 31 20 29 3b 0a 20 20 20 20  dxStr)-1 );.    
34812 20 20 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78      zIdxStr[iIdx
34813 2b 2b 5d 20 3d 20 6f 70 3b 0a 20 20 20 20 20 20  ++] = op;.      
34814 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78 2b 2b    zIdxStr[iIdx++
34815 5d 20 3d 20 63 43 6f 6c 3b 0a 20 20 20 20 20 20  ] = cCol;.      
34816 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e    pIdxInfo->aCon
34817 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d  straintUsage[ii]
34818 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 28 69 49  .argvIndex = (iI
34819 64 78 2f 32 29 3b 0a 20 20 20 20 20 20 20 20 70  dx/2);.        p
3481a 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
3481b 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e 6f 6d  aintUsage[ii].om
3481c 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  it = 1;.      }.
3481d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 49 64      }.  }..  pId
3481e 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  xInfo->idxNum = 
3481f 32 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e  2;.  pIdxInfo->n
34820 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20  eedToFreeIdxStr 
34821 3d 20 31 3b 0a 20 20 69 66 28 20 69 49 64 78 3e  = 1;.  if( iIdx>
34822 30 20 26 26 20 30 3d 3d 28 70 49 64 78 49 6e 66  0 && 0==(pIdxInf
34823 6f 2d 3e 69 64 78 53 74 72 20 3d 20 73 71 6c 69  o->idxStr = sqli
34824 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22  te3_mprintf("%s"
34825 2c 20 7a 49 64 78 53 74 72 29 29 20 29 7b 0a 20  , zIdxStr)) ){. 
34826 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
34827 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73  _NOMEM;.  }.  as
34828 73 65 72 74 28 20 69 49 64 78 3e 3d 30 20 29 3b  sert( iIdx>=0 );
34829 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  .  pIdxInfo->est
3482a 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 28 32 30  imatedCost = (20
3482b 30 30 30 30 30 2e 30 20 2f 20 28 64 6f 75 62 6c  00000.0 / (doubl
3482c 65 29 28 69 49 64 78 20 2b 20 31 29 29 3b 0a 20  e)(iIdx + 1));. 
3482d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
3482e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
3482f 4e 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 76 6f  N-dimensional vo
34830 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 65 6c 6c  lumn of the cell
34831 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 2e 0a 2a   stored in *p..*
34832 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63  /.static float c
34833 65 6c 6c 41 72 65 61 28 52 74 72 65 65 20 2a 70  ellArea(Rtree *p
34834 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
34835 20 2a 70 29 7b 0a 20 20 66 6c 6f 61 74 20 61 72   *p){.  float ar
34836 65 61 20 3d 20 31 2e 30 3b 0a 20 20 69 6e 74 20  ea = 1.0;.  int 
34837 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ii;.  for(ii=0; 
34838 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
34839 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
3483a 20 61 72 65 61 20 3d 20 61 72 65 61 20 2a 20 28   area = area * (
3483b 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
3483c 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f 4f 52 44  [ii+1]) - DCOORD
3483d 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 29  (p->aCoord[ii]))
3483e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61  ;.  }.  return a
3483f 72 65 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rea;.}../*.** Re
34840 74 75 72 6e 20 74 68 65 20 6d 61 72 67 69 6e 20  turn the margin 
34841 6c 65 6e 67 74 68 20 6f 66 20 63 65 6c 6c 20 70  length of cell p
34842 2e 20 54 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e  . The margin len
34843 67 74 68 20 69 73 20 74 68 65 20 73 75 6d 0a 2a  gth is the sum.*
34844 2a 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 73  * of the objects
34845 20 73 69 7a 65 20 69 6e 20 65 61 63 68 20 64 69   size in each di
34846 6d 65 6e 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  mension..*/.stat
34847 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4d 61 72  ic float cellMar
34848 67 69 6e 28 52 74 72 65 65 20 2a 70 52 74 72 65  gin(Rtree *pRtre
34849 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 29  e, RtreeCell *p)
3484a 7b 0a 20 20 66 6c 6f 61 74 20 6d 61 72 67 69 6e  {.  float margin
3484b 20 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 20 69 69   = 0.0;.  int ii
3484c 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
3484d 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
3484e 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 6d  ); ii+=2){.    m
3484f 61 72 67 69 6e 20 2b 3d 20 28 44 43 4f 4f 52 44  argin += (DCOORD
34850 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  (p->aCoord[ii+1]
34851 29 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61 43  ) - DCOORD(p->aC
34852 6f 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a  oord[ii]));.  }.
34853 20 20 72 65 74 75 72 6e 20 6d 61 72 67 69 6e 3b    return margin;
34854 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 6f 72 65 20  .}../*.** Store 
34855 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 63 65 6c  the union of cel
34856 6c 73 20 70 31 20 61 6e 64 20 70 32 20 69 6e 20  ls p1 and p2 in 
34857 70 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  p1..*/.static vo
34858 69 64 20 63 65 6c 6c 55 6e 69 6f 6e 28 52 74 72  id cellUnion(Rtr
34859 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
3485a 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65  eCell *p1, Rtree
3485b 43 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74  Cell *p2){.  int
3485c 20 69 69 3b 0a 20 20 69 66 28 20 70 52 74 72 65   ii;.  if( pRtre
3485d 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52  e->eCoordType==R
3485e 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33  TREE_COORD_REAL3
3485f 32 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d  2 ){.    for(ii=
34860 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e  0; ii<(pRtree->n
34861 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a  Dim*2); ii+=2){.
34862 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64        p1->aCoord
34863 5b 69 69 5d 2e 66 20 3d 20 4d 49 4e 28 70 31 2d  [ii].f = MIN(p1-
34864 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 2c 20 70  >aCoord[ii].f, p
34865 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 29  2->aCoord[ii].f)
34866 3b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f  ;.      p1->aCoo
34867 72 64 5b 69 69 2b 31 5d 2e 66 20 3d 20 4d 41 58  rd[ii+1].f = MAX
34868 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31  (p1->aCoord[ii+1
34869 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b  ].f, p2->aCoord[
3486a 69 69 2b 31 5d 2e 66 29 3b 0a 20 20 20 20 7d 0a  ii+1].f);.    }.
3486b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72    }else{.    for
3486c 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
3486d 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
3486e 32 29 7b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43  2){.      p1->aC
3486f 6f 6f 72 64 5b 69 69 5d 2e 69 20 3d 20 4d 49 4e  oord[ii].i = MIN
34870 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e  (p1->aCoord[ii].
34871 69 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69  i, p2->aCoord[ii
34872 5d 2e 69 29 3b 0a 20 20 20 20 20 20 70 31 2d 3e  ].i);.      p1->
34873 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20 3d  aCoord[ii+1].i =
34874 20 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72 64 5b   MAX(p1->aCoord[
34875 69 69 2b 31 5d 2e 69 2c 20 70 32 2d 3e 61 43 6f  ii+1].i, p2->aCo
34876 6f 72 64 5b 69 69 2b 31 5d 2e 69 29 3b 0a 20 20  ord[ii+1].i);.  
34877 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
34878 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
34879 74 68 65 20 61 72 65 61 20 63 6f 76 65 72 65 64  the area covered
3487a 20 62 79 20 70 32 20 69 73 20 61 20 73 75 62 73   by p2 is a subs
3487b 65 74 20 6f 66 20 74 68 65 20 61 72 65 61 20 63  et of the area c
3487c 6f 76 65 72 65 64 0a 2a 2a 20 62 79 20 70 31 2e  overed.** by p1.
3487d 20 46 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65   False otherwise
3487e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3487f 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 52 74 72  cellContains(Rtr
34880 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
34881 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65  eCell *p1, Rtree
34882 43 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74  Cell *p2){.  int
34883 20 69 69 3b 0a 20 20 69 6e 74 20 69 73 49 6e 74   ii;.  int isInt
34884 20 3d 20 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f   = (pRtree->eCoo
34885 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
34886 4f 52 44 5f 49 4e 54 33 32 29 3b 0a 20 20 66 6f  ORD_INT32);.  fo
34887 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72  r(ii=0; ii<(pRtr
34888 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b  ee->nDim*2); ii+
34889 3d 32 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f  =2){.    RtreeCo
3488a 6f 72 64 20 2a 61 31 20 3d 20 26 70 31 2d 3e 61  ord *a1 = &p1->a
3488b 43 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20 52  Coord[ii];.    R
3488c 74 72 65 65 43 6f 6f 72 64 20 2a 61 32 20 3d 20  treeCoord *a2 = 
3488d 26 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 3b  &p2->aCoord[ii];
3488e 0a 20 20 20 20 69 66 28 20 28 21 69 73 49 6e 74  .    if( (!isInt
3488f 20 26 26 20 28 61 32 5b 30 5d 2e 66 3c 61 31 5b   && (a2[0].f<a1[
34890 30 5d 2e 66 20 7c 7c 20 61 32 5b 31 5d 2e 66 3e  0].f || a2[1].f>
34891 61 31 5b 31 5d 2e 66 29 29 20 0a 20 20 20 20 20  a1[1].f)) .     
34892 7c 7c 20 28 20 69 73 49 6e 74 20 26 26 20 28 61  || ( isInt && (a
34893 32 5b 30 5d 2e 69 3c 61 31 5b 30 5d 2e 69 20 7c  2[0].i<a1[0].i |
34894 7c 20 61 32 5b 31 5d 2e 69 3e 61 31 5b 31 5d 2e  | a2[1].i>a1[1].
34895 69 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  i)) .    ){.    
34896 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
34897 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
34898 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
34899 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 63 65 6c  n the amount cel
3489a 6c 20 70 20 77 6f 75 6c 64 20 67 72 6f 77 20 62  l p would grow b
3489b 79 20 69 66 20 69 74 20 77 65 72 65 20 75 6e 69  y if it were uni
3489c 6f 6e 65 64 20 77 69 74 68 20 70 43 65 6c 6c 2e  oned with pCell.
3489d 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74  .*/.static float
3489e 20 63 65 6c 6c 47 72 6f 77 74 68 28 52 74 72 65   cellGrowth(Rtre
3489f 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
348a0 43 65 6c 6c 20 2a 70 2c 20 52 74 72 65 65 43 65  Cell *p, RtreeCe
348a1 6c 6c 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 66 6c  ll *pCell){.  fl
348a2 6f 61 74 20 61 72 65 61 3b 0a 20 20 52 74 72 65  oat area;.  Rtre
348a3 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 6d 65  eCell cell;.  me
348a4 6d 63 70 79 28 26 63 65 6c 6c 2c 20 70 2c 20 73  mcpy(&cell, p, s
348a5 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
348a6 29 3b 0a 20 20 61 72 65 61 20 3d 20 63 65 6c 6c  );.  area = cell
348a7 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65  Area(pRtree, &ce
348a8 6c 6c 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e  ll);.  cellUnion
348a9 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20  (pRtree, &cell, 
348aa 70 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  pCell);.  return
348ab 20 28 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65   (cellArea(pRtre
348ac 65 2c 20 26 63 65 6c 6c 29 2d 61 72 65 61 29 3b  e, &cell)-area);
348ad 0a 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  .}..#if VARIANT_
348ae 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
348af 53 55 42 54 52 45 45 20 7c 7c 20 56 41 52 49 41  SUBTREE || VARIA
348b0 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c  NT_RSTARTREE_SPL
348b1 49 54 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20  IT.static float 
348b2 63 65 6c 6c 4f 76 65 72 6c 61 70 28 0a 20 20 52  cellOverlap(.  R
348b3 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
348b4 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a   RtreeCell *p, .
348b5 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
348b6 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
348b7 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c 75 64  , .  int iExclud
348b8 65 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  e.){.  int ii;. 
348b9 20 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d   float overlap =
348ba 20 30 2e 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30   0.0;.  for(ii=0
348bb 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  ; ii<nCell; ii++
348bc 29 7b 0a 20 20 20 20 69 66 28 20 69 69 21 3d 69  ){.    if( ii!=i
348bd 45 78 63 6c 75 64 65 20 29 7b 0a 20 20 20 20 20  Exclude ){.     
348be 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 66   int jj;.      f
348bf 6c 6f 61 74 20 6f 20 3d 20 31 2e 30 3b 0a 20 20  loat o = 1.0;.  
348c0 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
348c1 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
348c2 29 3b 20 6a 6a 2b 3d 32 29 7b 0a 20 20 20 20 20  ); jj+=2){.     
348c3 20 20 20 64 6f 75 62 6c 65 20 78 31 3b 0a 20 20     double x1;.  
348c4 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 32 3b        double x2;
348c5 0a 0a 20 20 20 20 20 20 20 20 78 31 20 3d 20 4d  ..        x1 = M
348c6 41 58 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  AX(DCOORD(p->aCo
348c7 6f 72 64 5b 6a 6a 5d 29 2c 20 44 43 4f 4f 52 44  ord[jj]), DCOORD
348c8 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72  (aCell[ii].aCoor
348c9 64 5b 6a 6a 5d 29 29 3b 0a 20 20 20 20 20 20 20  d[jj]));.       
348ca 20 78 32 20 3d 20 4d 49 4e 28 44 43 4f 4f 52 44   x2 = MIN(DCOORD
348cb 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d  (p->aCoord[jj+1]
348cc 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b  ), DCOORD(aCell[
348cd 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d  ii].aCoord[jj+1]
348ce 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  ));..        if(
348cf 20 78 32 3c 78 31 20 29 7b 0a 20 20 20 20 20 20   x2<x1 ){.      
348d0 20 20 20 20 6f 20 3d 20 30 2e 30 3b 0a 20 20 20      o = 0.0;.   
348d1 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
348d2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
348d3 20 20 20 20 20 20 20 6f 20 3d 20 6f 20 2a 20 28         o = o * (
348d4 78 32 2d 78 31 29 3b 0a 20 20 20 20 20 20 20 20  x2-x1);.        
348d5 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
348d6 6f 76 65 72 6c 61 70 20 2b 3d 20 6f 3b 0a 20 20  overlap += o;.  
348d7 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
348d8 20 6f 76 65 72 6c 61 70 3b 0a 7d 0a 23 65 6e 64   overlap;.}.#end
348d9 69 66 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  if..#if VARIANT_
348da 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
348db 53 55 42 54 52 45 45 0a 73 74 61 74 69 63 20 66  SUBTREE.static f
348dc 6c 6f 61 74 20 63 65 6c 6c 4f 76 65 72 6c 61 70  loat cellOverlap
348dd 45 6e 6c 61 72 67 65 6d 65 6e 74 28 0a 20 20 52  Enlargement(.  R
348de 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
348df 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a   RtreeCell *p, .
348e0 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 49 6e    RtreeCell *pIn
348e1 73 65 72 74 2c 20 0a 20 20 52 74 72 65 65 43 65  sert, .  RtreeCe
348e2 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *aCell, .  in
348e3 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20  t nCell, .  int 
348e4 69 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20 66 6c  iExclude.){.  fl
348e5 6f 61 74 20 62 65 66 6f 72 65 3b 0a 20 20 66 6c  oat before;.  fl
348e6 6f 61 74 20 61 66 74 65 72 3b 0a 20 20 62 65 66  oat after;.  bef
348e7 6f 72 65 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61  ore = cellOverla
348e8 70 28 70 52 74 72 65 65 2c 20 70 2c 20 61 43 65  p(pRtree, p, aCe
348e9 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69 45 78 63 6c  ll, nCell, iExcl
348ea 75 64 65 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f  ude);.  cellUnio
348eb 6e 28 70 52 74 72 65 65 2c 20 70 2c 20 70 49 6e  n(pRtree, p, pIn
348ec 73 65 72 74 29 3b 0a 20 20 61 66 74 65 72 20 3d  sert);.  after =
348ed 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74   cellOverlap(pRt
348ee 72 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e  ree, p, aCell, n
348ef 43 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b  Cell, iExclude);
348f0 0a 20 20 72 65 74 75 72 6e 20 61 66 74 65 72 2d  .  return after-
348f1 62 65 66 6f 72 65 3b 0a 7d 0a 23 65 6e 64 69 66  before;.}.#endif
348f2 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
348f3 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
348f4 73 20 74 68 65 20 43 68 6f 6f 73 65 4c 65 61 66  s the ChooseLeaf
348f5 20 61 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20   algorithm from 
348f6 47 75 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2a 20 43  Gutman[84]..** C
348f7 68 6f 6f 73 65 53 75 62 54 72 65 65 20 69 6e 20  hooseSubTree in 
348f8 72 2a 74 72 65 65 20 74 65 72 6d 69 6e 6f 6c 6f  r*tree terminolo
348f9 67 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  gy..*/.static in
348fa 74 20 43 68 6f 6f 73 65 4c 65 61 66 28 0a 20 20  t ChooseLeaf(.  
348fb 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20  Rtree *pRtree,  
348fc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
348fd 52 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20  Rtree table */. 
348fe 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
348ff 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l,            /*
34900 20 43 65 6c 6c 20 74 6f 20 69 6e 73 65 72 74 20   Cell to insert 
34901 69 6e 74 6f 20 72 74 72 65 65 20 2a 2f 0a 20 20  into rtree */.  
34902 69 6e 74 20 69 48 65 69 67 68 74 2c 20 20 20 20  int iHeight,    
34903 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34904 48 65 69 67 68 74 20 6f 66 20 73 75 62 2d 74 72  Height of sub-tr
34905 65 65 20 72 6f 6f 74 65 64 20 61 74 20 70 43 65  ee rooted at pCe
34906 6c 6c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  ll */.  RtreeNod
34907 65 20 2a 2a 70 70 4c 65 61 66 20 20 20 20 20 20  e **ppLeaf      
34908 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 6c       /* OUT: Sel
34909 65 63 74 65 64 20 6c 65 61 66 20 70 61 67 65 20  ected leaf page 
3490a 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
3490b 20 20 69 6e 74 20 69 69 3b 0a 20 20 52 74 72 65    int ii;.  Rtre
3490c 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20  eNode *pNode;.  
3490d 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
3490e 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26  (pRtree, 1, 0, &
3490f 70 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69  pNode);..  for(i
34910 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
34911 4f 4b 20 26 26 20 69 69 3c 28 70 52 74 72 65 65  OK && ii<(pRtree
34912 2d 3e 69 44 65 70 74 68 2d 69 48 65 69 67 68 74  ->iDepth-iHeight
34913 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  ); ii++){.    in
34914 74 20 69 43 65 6c 6c 3b 0a 20 20 20 20 73 71 6c  t iCell;.    sql
34915 69 74 65 33 5f 69 6e 74 36 34 20 69 42 65 73 74  ite3_int64 iBest
34916 3b 0a 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69  ;..    float fMi
34917 6e 47 72 6f 77 74 68 3b 0a 20 20 20 20 66 6c 6f  nGrowth;.    flo
34918 61 74 20 66 4d 69 6e 41 72 65 61 3b 0a 20 20 20  at fMinArea;.   
34919 20 66 6c 6f 61 74 20 66 4d 69 6e 4f 76 65 72 6c   float fMinOverl
3491a 61 70 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 43 65  ap;..    int nCe
3491b 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
3491c 29 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  );.    RtreeCell
3491d 20 63 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65   cell;.    Rtree
3491e 4e 6f 64 65 20 2a 70 43 68 69 6c 64 3b 0a 0a 20  Node *pChild;.. 
3491f 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43     RtreeCell *aC
34920 65 6c 6c 20 3d 20 30 3b 0a 0a 23 69 66 20 56 41  ell = 0;..#if VA
34921 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
34922 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 20 20  CHOOSESUBTREE.  
34923 20 20 69 66 28 20 69 69 3d 3d 28 70 52 74 72 65    if( ii==(pRtre
34924 65 2d 3e 69 44 65 70 74 68 2d 31 29 20 29 7b 0a  e->iDepth-1) ){.
34925 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20        int jj;.  
34926 20 20 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c 69      aCell = sqli
34927 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
34928 66 28 52 74 72 65 65 43 65 6c 6c 29 2a 6e 43 65  f(RtreeCell)*nCe
34929 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ll);.      if( !
3492a 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  aCell ){.       
3492b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
3492c 45 4d 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65  EM;.        node
3492d 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
3492e 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20  pNode);.        
3492f 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20  pNode = 0;.     
34930 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
34931 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6a     }.      for(j
34932 6a 3d 30 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a  j=0; jj<nCell; j
34933 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 6e 6f  j++){.        no
34934 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65  deGetCell(pRtree
34935 2c 20 70 4e 6f 64 65 2c 20 6a 6a 2c 20 26 61 43  , pNode, jj, &aC
34936 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20  ell[jj]);.      
34937 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  }.    }.#endif..
34938 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68      /* Select th
34939 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 77 68 69  e child node whi
3493a 63 68 20 77 69 6c 6c 20 62 65 20 65 6e 6c 61 72  ch will be enlar
3493b 67 65 64 20 74 68 65 20 6c 65 61 73 74 20 69 66  ged the least if
3493c 20 70 43 65 6c 6c 0a 20 20 20 20 2a 2a 20 69 73   pCell.    ** is
3493d 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69   inserted into i
3493e 74 2e 20 52 65 73 6f 6c 76 65 20 74 69 65 73 20  t. Resolve ties 
3493f 62 79 20 63 68 6f 6f 73 69 6e 67 20 74 68 65 20  by choosing the 
34940 65 6e 74 72 79 20 77 69 74 68 0a 20 20 20 20 2a  entry with.    *
34941 2a 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 61  * the smallest a
34942 72 65 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  rea..    */.    
34943 66 6f 72 28 69 43 65 6c 6c 3d 30 3b 20 69 43 65  for(iCell=0; iCe
34944 6c 6c 3c 6e 43 65 6c 6c 3b 20 69 43 65 6c 6c 2b  ll<nCell; iCell+
34945 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  +){.      float 
34946 67 72 6f 77 74 68 3b 0a 20 20 20 20 20 20 66 6c  growth;.      fl
34947 6f 61 74 20 61 72 65 61 3b 0a 20 20 20 20 20 20  oat area;.      
34948 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d 20  float overlap = 
34949 30 2e 30 3b 0a 20 20 20 20 20 20 6e 6f 64 65 47  0.0;.      nodeG
3494a 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
3494b 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 26 63 65  Node, iCell, &ce
3494c 6c 6c 29 3b 0a 20 20 20 20 20 20 67 72 6f 77 74  ll);.      growt
3494d 68 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70  h = cellGrowth(p
3494e 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43  Rtree, &cell, pC
3494f 65 6c 6c 29 3b 0a 20 20 20 20 20 20 61 72 65 61  ell);.      area
34950 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72   = cellArea(pRtr
34951 65 65 2c 20 26 63 65 6c 6c 29 3b 0a 23 69 66 20  ee, &cell);.#if 
34952 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
34953 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a  E_CHOOSESUBTREE.
34954 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d 28 70        if( ii==(p
34955 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 31 29  Rtree->iDepth-1)
34956 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 76 65 72   ){.        over
34957 6c 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61  lap = cellOverla
34958 70 45 6e 6c 61 72 67 65 6d 65 6e 74 28 70 52 74  pEnlargement(pRt
34959 72 65 65 2c 26 63 65 6c 6c 2c 70 43 65 6c 6c 2c  ree,&cell,pCell,
3495a 61 43 65 6c 6c 2c 6e 43 65 6c 6c 2c 69 43 65 6c  aCell,nCell,iCel
3495b 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  l);.      }.#end
3495c 69 66 0a 20 20 20 20 20 20 69 66 28 20 28 69 43  if.      if( (iC
3495d 65 6c 6c 3d 3d 30 29 20 0a 20 20 20 20 20 20 20  ell==0) .       
3495e 7c 7c 20 28 6f 76 65 72 6c 61 70 3c 66 4d 69 6e  || (overlap<fMin
3495f 4f 76 65 72 6c 61 70 29 20 0a 20 20 20 20 20 20  Overlap) .      
34960 20 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 4d   || (overlap==fM
34961 69 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72 6f  inOverlap && gro
34962 77 74 68 3c 66 4d 69 6e 47 72 6f 77 74 68 29 0a  wth<fMinGrowth).
34963 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c         || (overl
34964 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c 61 70 20  ap==fMinOverlap 
34965 26 26 20 67 72 6f 77 74 68 3d 3d 66 4d 69 6e 47  && growth==fMinG
34966 72 6f 77 74 68 20 26 26 20 61 72 65 61 3c 66 4d  rowth && area<fM
34967 69 6e 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b  inArea).      ){
34968 0a 20 20 20 20 20 20 20 20 66 4d 69 6e 4f 76 65  .        fMinOve
34969 72 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b 0a  rlap = overlap;.
3496a 20 20 20 20 20 20 20 20 66 4d 69 6e 47 72 6f 77          fMinGrow
3496b 74 68 20 3d 20 67 72 6f 77 74 68 3b 0a 20 20 20  th = growth;.   
3496c 20 20 20 20 20 66 4d 69 6e 41 72 65 61 20 3d 20       fMinArea = 
3496d 61 72 65 61 3b 0a 20 20 20 20 20 20 20 20 69 42  area;.        iB
3496e 65 73 74 20 3d 20 63 65 6c 6c 2e 69 52 6f 77 69  est = cell.iRowi
3496f 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  d;.      }.    }
34970 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
34971 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20 20 20 72  ee(aCell);.    r
34972 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
34973 70 52 74 72 65 65 2c 20 69 42 65 73 74 2c 20 70  pRtree, iBest, p
34974 4e 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a  Node, &pChild);.
34975 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
34976 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
34977 20 20 20 20 70 4e 6f 64 65 20 3d 20 70 43 68 69      pNode = pChi
34978 6c 64 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4c 65  ld;.  }..  *ppLe
34979 61 66 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 65  af = pNode;.  re
3497a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
3497b 2a 20 41 20 63 65 6c 6c 20 77 69 74 68 20 74 68  * A cell with th
3497c 65 20 73 61 6d 65 20 63 6f 6e 74 65 6e 74 20 61  e same content a
3497d 73 20 70 43 65 6c 6c 20 68 61 73 20 6a 75 73 74  s pCell has just
3497e 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64 20 69   been inserted i
3497f 6e 74 6f 0a 2a 2a 20 74 68 65 20 6e 6f 64 65 20  nto.** the node 
34980 70 4e 6f 64 65 2e 20 54 68 69 73 20 66 75 6e 63  pNode. This func
34981 74 69 6f 6e 20 75 70 64 61 74 65 73 20 74 68 65  tion updates the
34982 20 62 6f 75 6e 64 69 6e 67 20 62 6f 78 20 63 65   bounding box ce
34983 6c 6c 73 20 69 6e 0a 2a 2a 20 61 6c 6c 20 61 6e  lls in.** all an
34984 63 65 73 74 6f 72 20 65 6c 65 6d 65 6e 74 73 2e  cestor elements.
34985 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
34986 41 64 6a 75 73 74 54 72 65 65 28 0a 20 20 52 74  AdjustTree(.  Rt
34987 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20 20 20  ree *pRtree,    
34988 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34989 2f 2a 20 52 74 72 65 65 20 74 61 62 6c 65 20 2a  /* Rtree table *
3498a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  /.  RtreeNode *p
3498b 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20  Node,           
3498c 20 20 20 20 20 20 2f 2a 20 41 64 6a 75 73 74 20        /* Adjust 
3498d 61 6e 63 65 73 74 72 79 20 6f 66 20 74 68 69 73  ancestry of this
3498e 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 52 74 72 65   node. */.  Rtre
3498f 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 20 20 20  eCell *pCell    
34990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34991 20 54 68 69 73 20 63 65 6c 6c 20 77 61 73 20 6a   This cell was j
34992 75 73 74 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a  ust inserted */.
34993 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ){.  RtreeNode *
34994 70 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 77 68 69  p = pNode;.  whi
34995 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20 29  le( p->pParent )
34996 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  {.    RtreeCell 
34997 63 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65 4e  cell;.    RtreeN
34998 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  ode *pParent = p
34999 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 69  ->pParent;.    i
3499a 6e 74 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 50  nt iCell = nodeP
3499b 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65  arentIndex(pRtre
3499c 65 2c 20 70 29 3b 0a 0a 20 20 20 20 6e 6f 64 65  e, p);..    node
3499d 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
3499e 70 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20  pParent, iCell, 
3499f 26 63 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20  &cell);.    if( 
349a0 21 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 70 52  !cellContains(pR
349a1 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65  tree, &cell, pCe
349a2 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 65 6c  ll) ){.      cel
349a3 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
349a4 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  cell, pCell);.  
349a5 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74      nodeOverwrit
349a6 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50  eCell(pRtree, pP
349a7 61 72 65 6e 74 2c 20 26 63 65 6c 6c 2c 20 69 43  arent, &cell, iC
349a8 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 0a 20 20  ell);.    }. .  
349a9 20 20 70 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20    p = pParent;. 
349aa 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74   }.}../*.** Writ
349ab 65 20 6d 61 70 70 69 6e 67 20 28 69 52 6f 77 69  e mapping (iRowi
349ac 64 2d 3e 69 4e 6f 64 65 29 20 74 6f 20 74 68 65  d->iNode) to the
349ad 20 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74   <rtree>_rowid t
349ae 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
349af 69 6e 74 20 72 6f 77 69 64 57 72 69 74 65 28 52  int rowidWrite(R
349b0 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71  tree *pRtree, sq
349b1 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
349b2 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  id, sqlite3_int6
349b3 34 20 69 4e 6f 64 65 29 7b 0a 20 20 73 71 6c 69  4 iNode){.  sqli
349b4 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
349b5 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
349b6 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a  id, 1, iRowid);.
349b7 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
349b8 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72  nt64(pRtree->pWr
349b9 69 74 65 52 6f 77 69 64 2c 20 32 2c 20 69 4e 6f  iteRowid, 2, iNo
349ba 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  de);.  sqlite3_s
349bb 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69  tep(pRtree->pWri
349bc 74 65 52 6f 77 69 64 29 3b 0a 20 20 72 65 74 75  teRowid);.  retu
349bd 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  rn sqlite3_reset
349be 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
349bf 6f 77 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  owid);.}../*.** 
349c0 57 72 69 74 65 20 6d 61 70 70 69 6e 67 20 28 69  Write mapping (i
349c1 4e 6f 64 65 2d 3e 69 50 61 72 29 20 74 6f 20 74  Node->iPar) to t
349c2 68 65 20 3c 72 74 72 65 65 3e 5f 70 61 72 65 6e  he <rtree>_paren
349c3 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  t table..*/.stat
349c4 69 63 20 69 6e 74 20 70 61 72 65 6e 74 57 72 69  ic int parentWri
349c5 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  te(Rtree *pRtree
349c6 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
349c7 69 4e 6f 64 65 2c 20 73 71 6c 69 74 65 33 5f 69  iNode, sqlite3_i
349c8 6e 74 36 34 20 69 50 61 72 29 7b 0a 20 20 73 71  nt64 iPar){.  sq
349c9 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
349ca 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  (pRtree->pWriteP
349cb 61 72 65 6e 74 2c 20 31 2c 20 69 4e 6f 64 65 29  arent, 1, iNode)
349cc 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ;.  sqlite3_bind
349cd 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
349ce 57 72 69 74 65 50 61 72 65 6e 74 2c 20 32 2c 20  WriteParent, 2, 
349cf 69 50 61 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  iPar);.  sqlite3
349d0 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57  _step(pRtree->pW
349d1 72 69 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 72  riteParent);.  r
349d2 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65  eturn sqlite3_re
349d3 73 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69  set(pRtree->pWri
349d4 74 65 50 61 72 65 6e 74 29 3b 0a 7d 0a 0a 73 74  teParent);.}..st
349d5 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e  atic int rtreeIn
349d6 73 65 72 74 43 65 6c 6c 28 52 74 72 65 65 20 2a  sertCell(Rtree *
349d7 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20 52  , RtreeNode *, R
349d8 74 72 65 65 43 65 6c 6c 20 2a 2c 20 69 6e 74 29  treeCell *, int)
349d9 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  ;..#if VARIANT_G
349da 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50  UTTMAN_LINEAR_SP
349db 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  LIT./*.** Implem
349dc 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
349dd 6c 69 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f  linear variant o
349de 66 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29  f the PickNext()
349df 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a   function from.*
349e0 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a  * Guttman[84]..*
349e1 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 43 65  /.static RtreeCe
349e2 6c 6c 20 2a 4c 69 6e 65 61 72 50 69 63 6b 4e 65  ll *LinearPickNe
349e3 78 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  xt(.  Rtree *pRt
349e4 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ree,.  RtreeCell
349e5 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *aCell, .  int 
349e6 6e 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65 65 43  nCell, .  RtreeC
349e7 65 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c 20 0a  ell *pLeftBox, .
349e8 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 52 69    RtreeCell *pRi
349e9 67 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20 2a 61  ghtBox,.  int *a
349ea 69 55 73 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69  iUsed.){.  int i
349eb 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 61  i;.  for(ii=0; a
349ec 69 55 73 65 64 5b 69 69 5d 3b 20 69 69 2b 2b 29  iUsed[ii]; ii++)
349ed 3b 0a 20 20 61 69 55 73 65 64 5b 69 69 5d 20 3d  ;.  aiUsed[ii] =
349ee 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43   1;.  return &aC
349ef 65 6c 6c 5b 69 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  ell[ii];.}../*.*
349f0 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
349f1 20 6f 66 20 74 68 65 20 6c 69 6e 65 61 72 20 76   of the linear v
349f2 61 72 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69  ariant of the Pi
349f3 63 6b 53 65 65 64 73 28 29 20 66 75 6e 63 74 69  ckSeeds() functi
349f4 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d  on from.** Guttm
349f5 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69  an[84]..*/.stati
349f6 63 20 76 6f 69 64 20 4c 69 6e 65 61 72 50 69 63  c void LinearPic
349f7 6b 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20  kSeeds(.  Rtree 
349f8 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
349f9 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
349fa 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
349fb 74 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a  t *piLeftSeed, .
349fc 20 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65    int *piRightSe
349fd 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ed.){.  int i;. 
349fe 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20 3d   int iLeftSeed =
349ff 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67 68 74   0;.  int iRight
34a00 53 65 65 64 20 3d 20 31 3b 0a 20 20 66 6c 6f 61  Seed = 1;.  floa
34a01 74 20 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e 65 72  t maxNormalInner
34a02 57 69 64 74 68 20 3d 20 30 2e 30 3b 0a 0a 20 20  Width = 0.0;..  
34a03 2f 2a 20 50 69 63 6b 20 74 77 6f 20 22 73 65 65  /* Pick two "see
34a04 64 22 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68  d" cells from th
34a05 65 20 61 72 72 61 79 20 6f 66 20 63 65 6c 6c 73  e array of cells
34a06 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20  . The algorithm 
34a07 75 73 65 64 0a 20 20 2a 2a 20 68 65 72 65 20 69  used.  ** here i
34a08 73 20 74 68 65 20 4c 69 6e 65 61 72 50 69 63 6b  s the LinearPick
34a09 53 65 65 64 73 20 61 6c 67 6f 72 69 74 68 6d 20  Seeds algorithm 
34a0a 66 72 6f 6d 20 47 75 74 6d 61 6e 5b 31 39 38 34  from Gutman[1984
34a0b 5d 2e 20 54 68 65 20 0a 20 20 2a 2a 20 69 6e 64  ]. The .  ** ind
34a0c 69 63 65 73 20 6f 66 20 74 68 65 20 74 77 6f 20  ices of the two 
34a0d 73 65 65 64 20 63 65 6c 6c 73 20 69 6e 20 74 68  seed cells in th
34a0e 65 20 61 72 72 61 79 20 61 72 65 20 73 74 6f 72  e array are stor
34a0f 65 64 20 69 6e 20 6c 6f 63 61 6c 0a 20 20 2a 2a  ed in local.  **
34a10 20 76 61 72 69 61 62 6c 65 73 20 69 4c 65 66 74   variables iLeft
34a11 53 65 65 6b 20 61 6e 64 20 69 52 69 67 68 74 53  Seek and iRightS
34a12 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  eed..  */.  for(
34a13 69 3d 30 3b 20 69 3c 70 52 74 72 65 65 2d 3e 6e  i=0; i<pRtree->n
34a14 44 69 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66  Dim; i++){.    f
34a15 6c 6f 61 74 20 78 31 20 3d 20 61 43 65 6c 6c 5b  loat x1 = aCell[
34a16 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d 3b 0a  0].aCoord[i*2];.
34a17 20 20 20 20 66 6c 6f 61 74 20 78 32 20 3d 20 61      float x2 = a
34a18 43 65 6c 6c 5b 30 5d 2e 61 43 6f 6f 72 64 5b 69  Cell[0].aCoord[i
34a19 2a 32 2b 31 5d 3b 0a 20 20 20 20 66 6c 6f 61 74  *2+1];.    float
34a1a 20 78 33 20 3d 20 78 31 3b 0a 20 20 20 20 66 6c   x3 = x1;.    fl
34a1b 6f 61 74 20 78 34 20 3d 20 78 32 3b 0a 20 20 20  oat x4 = x2;.   
34a1c 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20 69 6e   int jj;..    in
34a1d 74 20 69 43 65 6c 6c 4c 65 66 74 20 3d 20 30 3b  t iCellLeft = 0;
34a1e 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 52 69  .    int iCellRi
34a1f 67 68 74 20 3d 20 30 3b 0a 0a 20 20 20 20 66 6f  ght = 0;..    fo
34a20 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 43 65 6c 6c  r(jj=1; jj<nCell
34a21 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66  ; jj++){.      f
34a22 6c 6f 61 74 20 6c 65 66 74 20 3d 20 61 43 65 6c  loat left = aCel
34a23 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32  l[jj].aCoord[i*2
34a24 5d 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72  ];.      float r
34a25 69 67 68 74 20 3d 20 61 43 65 6c 6c 5b 6a 6a 5d  ight = aCell[jj]
34a26 2e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d 3b 0a  .aCoord[i*2+1];.
34a27 0a 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3c  .      if( left<
34a28 78 31 20 29 20 78 31 20 3d 20 6c 65 66 74 3b 0a  x1 ) x1 = left;.
34a29 20 20 20 20 20 20 69 66 28 20 72 69 67 68 74 3e        if( right>
34a2a 78 34 20 29 20 78 34 20 3d 20 72 69 67 68 74 3b  x4 ) x4 = right;
34a2b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3e  .      if( left>
34a2c 78 33 20 29 7b 0a 20 20 20 20 20 20 20 20 78 33  x3 ){.        x3
34a2d 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20 20 20   = left;.       
34a2e 20 69 43 65 6c 6c 52 69 67 68 74 20 3d 20 6a 6a   iCellRight = jj
34a2f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
34a30 69 66 28 20 72 69 67 68 74 3c 78 32 20 29 7b 0a  if( right<x2 ){.
34a31 20 20 20 20 20 20 20 20 78 32 20 3d 20 72 69 67          x2 = rig
34a32 68 74 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c  ht;.        iCel
34a33 6c 4c 65 66 74 20 3d 20 6a 6a 3b 0a 20 20 20 20  lLeft = jj;.    
34a34 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69    }.    }..    i
34a35 66 28 20 78 34 21 3d 78 31 20 29 7b 0a 20 20 20  f( x4!=x1 ){.   
34a36 20 20 20 66 6c 6f 61 74 20 6e 6f 72 6d 61 6c 77     float normalw
34a37 69 64 74 68 20 3d 20 28 78 33 20 2d 20 78 32 29  idth = (x3 - x2)
34a38 20 2f 20 28 78 34 20 2d 20 78 31 29 3b 0a 20 20   / (x4 - x1);.  
34a39 20 20 20 20 69 66 28 20 6e 6f 72 6d 61 6c 77 69      if( normalwi
34a3a 64 74 68 3e 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e  dth>maxNormalInn
34a3b 65 72 57 69 64 74 68 20 29 7b 0a 20 20 20 20 20  erWidth ){.     
34a3c 20 20 20 69 4c 65 66 74 53 65 65 64 20 3d 20 69     iLeftSeed = i
34a3d 43 65 6c 6c 4c 65 66 74 3b 0a 20 20 20 20 20 20  CellLeft;.      
34a3e 20 20 69 52 69 67 68 74 53 65 65 64 20 3d 20 69    iRightSeed = i
34a3f 43 65 6c 6c 52 69 67 68 74 3b 0a 20 20 20 20 20  CellRight;.     
34a40 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
34a41 2a 70 69 4c 65 66 74 53 65 65 64 20 3d 20 69 4c  *piLeftSeed = iL
34a42 65 66 74 53 65 65 64 3b 0a 20 20 2a 70 69 52 69  eftSeed;.  *piRi
34a43 67 68 74 53 65 65 64 20 3d 20 69 52 69 67 68 74  ghtSeed = iRight
34a44 53 65 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Seed;.}.#endif /
34a45 2a 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  * VARIANT_GUTTMA
34a46 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 20 2a  N_LINEAR_SPLIT *
34a47 2f 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  /..#if VARIANT_G
34a48 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
34a49 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70  _SPLIT./*.** Imp
34a4a 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
34a4b 68 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72  he quadratic var
34a4c 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b  iant of the Pick
34a4d 4e 65 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  Next() function 
34a4e 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
34a4f 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52  84]..*/.static R
34a50 74 72 65 65 43 65 6c 6c 20 2a 51 75 61 64 72 61  treeCell *Quadra
34a51 74 69 63 50 69 63 6b 4e 65 78 74 28 0a 20 20 52  ticPickNext(.  R
34a52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20  tree *pRtree,.  
34a53 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
34a54 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20  , .  int nCell, 
34a55 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c  .  RtreeCell *pL
34a56 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72 65 65  eftBox, .  Rtree
34a57 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c  Cell *pRightBox,
34a58 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64 0a 29  .  int *aiUsed.)
34a59 7b 0a 20 20 23 64 65 66 69 6e 65 20 46 41 42 53  {.  #define FABS
34a5a 28 61 29 20 28 28 61 29 3c 30 2e 30 3f 2d 31 2e  (a) ((a)<0.0?-1.
34a5b 30 2a 28 61 29 3a 28 61 29 29 0a 0a 20 20 69 6e  0*(a):(a))..  in
34a5c 74 20 69 53 65 6c 65 63 74 20 3d 20 2d 31 3b 0a  t iSelect = -1;.
34a5d 20 20 66 6c 6f 61 74 20 66 44 69 66 66 3b 0a 20    float fDiff;. 
34a5e 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
34a5f 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
34a60 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 69  i++){.    if( ai
34a61 55 73 65 64 5b 69 69 5d 3d 3d 30 20 29 7b 0a 20  Used[ii]==0 ){. 
34a62 20 20 20 20 20 66 6c 6f 61 74 20 6c 65 66 74 20       float left 
34a63 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74  = cellGrowth(pRt
34a64 72 65 65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26  ree, pLeftBox, &
34a65 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20  aCell[ii]);.    
34a66 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
34a67 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65  cellGrowth(pRtre
34a68 65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26 61 43  e, pLeftBox, &aC
34a69 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20  ell[ii]);.      
34a6a 66 6c 6f 61 74 20 64 69 66 66 20 3d 20 46 41 42  float diff = FAB
34a6b 53 28 72 69 67 68 74 2d 6c 65 66 74 29 3b 0a 20  S(right-left);. 
34a6c 20 20 20 20 20 69 66 28 20 69 53 65 6c 65 63 74       if( iSelect
34a6d 3c 30 20 7c 7c 20 64 69 66 66 3e 66 44 69 66 66  <0 || diff>fDiff
34a6e 20 29 7b 0a 20 20 20 20 20 20 20 20 66 44 69 66   ){.        fDif
34a6f 66 20 3d 20 64 69 66 66 3b 0a 20 20 20 20 20 20  f = diff;.      
34a70 20 20 69 53 65 6c 65 63 74 20 3d 20 69 69 3b 0a    iSelect = ii;.
34a71 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
34a72 7d 0a 20 20 61 69 55 73 65 64 5b 69 53 65 6c 65  }.  aiUsed[iSele
34a73 63 74 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75 72  ct] = 1;.  retur
34a74 6e 20 26 61 43 65 6c 6c 5b 69 53 65 6c 65 63 74  n &aCell[iSelect
34a75 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  ];.}../*.** Impl
34a76 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
34a77 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72 69  e quadratic vari
34a78 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b 53  ant of the PickS
34a79 65 65 64 73 28 29 20 66 75 6e 63 74 69 6f 6e 20  eeds() function 
34a7a 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
34a7b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  84]..*/.static v
34a7c 6f 69 64 20 51 75 61 64 72 61 74 69 63 50 69 63  oid QuadraticPic
34a7d 6b 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20  kSeeds(.  Rtree 
34a7e 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
34a7f 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
34a80 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
34a81 74 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a  t *piLeftSeed, .
34a82 20 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65    int *piRightSe
34a83 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  ed.){.  int ii;.
34a84 20 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 69 6e 74    int jj;..  int
34a85 20 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a   iLeftSeed = 0;.
34a86 20 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64    int iRightSeed
34a87 20 3d 20 31 3b 0a 20 20 66 6c 6f 61 74 20 66 57   = 1;.  float fW
34a88 61 73 74 65 20 3d 20 30 2e 30 3b 0a 0a 20 20 66  aste = 0.0;..  f
34a89 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c  or(ii=0; ii<nCel
34a8a 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66 6f  l; ii++){.    fo
34a8b 72 28 6a 6a 3d 69 69 2b 31 3b 20 6a 6a 3c 6e 43  r(jj=ii+1; jj<nC
34a8c 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  ell; jj++){.    
34a8d 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
34a8e 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c  cellArea(pRtree,
34a8f 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20   &aCell[jj]);.  
34a90 20 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74 68      float growth
34a91 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52   = cellGrowth(pR
34a92 74 72 65 65 2c 20 26 61 43 65 6c 6c 5b 69 69 5d  tree, &aCell[ii]
34a93 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20  , &aCell[jj]);. 
34a94 20 20 20 20 20 66 6c 6f 61 74 20 77 61 73 74 65       float waste
34a95 20 3d 20 67 72 6f 77 74 68 20 2d 20 72 69 67 68   = growth - righ
34a96 74 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 77 61  t;..      if( wa
34a97 73 74 65 3e 66 57 61 73 74 65 20 29 7b 0a 20 20  ste>fWaste ){.  
34a98 20 20 20 20 20 20 69 4c 65 66 74 53 65 65 64 20        iLeftSeed 
34a99 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 69 52  = ii;.        iR
34a9a 69 67 68 74 53 65 65 64 20 3d 20 6a 6a 3b 0a 20  ightSeed = jj;. 
34a9b 20 20 20 20 20 20 20 66 57 61 73 74 65 20 3d 20         fWaste = 
34a9c 77 61 73 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20  waste;.      }. 
34a9d 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c     }.  }..  *piL
34a9e 65 66 74 53 65 65 64 20 3d 20 69 4c 65 66 74 53  eftSeed = iLeftS
34a9f 65 65 64 3b 0a 20 20 2a 70 69 52 69 67 68 74 53  eed;.  *piRightS
34aa0 65 65 64 20 3d 20 69 52 69 67 68 74 53 65 65 64  eed = iRightSeed
34aa1 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41  ;.}.#endif /* VA
34aa2 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55  RIANT_GUTTMAN_QU
34aa3 41 44 52 41 54 49 43 5f 53 50 4c 49 54 20 2a 2f  ADRATIC_SPLIT */
34aa4 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
34aa5 73 20 61 49 64 78 2c 20 61 44 69 73 74 61 6e 63  s aIdx, aDistanc
34aa6 65 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c 6c  e and aSpare all
34aa7 20 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79 73   point to arrays
34aa8 20 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64 78   of size.** nIdx
34aa9 2e 20 54 68 65 20 61 49 64 78 20 61 72 72 61 79  . The aIdx array
34aaa 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 65   contains the se
34aab 74 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66 72  t of integers fr
34aac 6f 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49 64  om 0 to .** (nId
34aad 78 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74 69  x-1) in no parti
34aae 63 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68 69  cular order. Thi
34aaf 73 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74 73  s function sorts
34ab0 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 69   the values.** i
34ab1 6e 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e 67  n aIdx according
34ab2 20 74 6f 20 74 68 65 20 69 6e 64 65 78 65 64 20   to the indexed 
34ab3 76 61 6c 75 65 73 20 69 6e 20 61 44 69 73 74 61  values in aDista
34ab4 6e 63 65 2e 20 46 6f 72 0a 2a 2a 20 65 78 61 6d  nce. For.** exam
34ab5 70 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68  ple, assuming th
34ab6 65 20 69 6e 70 75 74 73 3a 0a 2a 2a 0a 2a 2a 20  e inputs:.**.** 
34ab7 20 20 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20    aIdx      = { 
34ab8 30 2c 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33  0,   1,   2,   3
34ab9 20 7d 0a 2a 2a 20 20 20 61 44 69 73 74 61 6e 63   }.**   aDistanc
34aba 65 20 3d 20 7b 20 35 2e 30 2c 20 32 2e 30 2c 20  e = { 5.0, 2.0, 
34abb 37 2e 30 2c 20 36 2e 30 20 7d 0a 2a 2a 0a 2a 2a  7.0, 6.0 }.**.**
34abc 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   this function s
34abd 65 74 73 20 74 68 65 20 61 49 64 78 20 61 72 72  ets the aIdx arr
34abe 61 79 20 74 6f 20 63 6f 6e 74 61 69 6e 3a 0a 2a  ay to contain:.*
34abf 2a 0a 2a 2a 20 20 20 61 49 64 78 20 20 20 20 20  *.**   aIdx     
34ac0 20 3d 20 7b 20 30 2c 20 20 20 31 2c 20 20 20 32   = { 0,   1,   2
34ac1 2c 20 20 20 33 20 7d 0a 2a 2a 0a 2a 2a 20 54 68  ,   3 }.**.** Th
34ac2 65 20 61 53 70 61 72 65 20 61 72 72 61 79 20 69  e aSpare array i
34ac3 73 20 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72  s used as tempor
34ac4 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63  ary working spac
34ac5 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74  e by the.** sort
34ac6 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a  ing algorithm..*
34ac7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 53 6f  /.static void So
34ac8 72 74 42 79 44 69 73 74 61 6e 63 65 28 0a 20 20  rtByDistance(.  
34ac9 69 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20 69 6e  int *aIdx, .  in
34aca 74 20 6e 49 64 78 2c 20 0a 20 20 66 6c 6f 61 74  t nIdx, .  float
34acb 20 2a 61 44 69 73 74 61 6e 63 65 2c 20 0a 20 20   *aDistance, .  
34acc 69 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20  int *aSpare.){. 
34acd 20 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a 20   if( nIdx>1 ){. 
34ace 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30     int iLeft = 0
34acf 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74  ;.    int iRight
34ad0 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e   = 0;..    int n
34ad1 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20  Left = nIdx/2;. 
34ad2 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20     int nRight = 
34ad3 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20  nIdx-nLeft;.    
34ad4 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64  int *aLeft = aId
34ad5 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67  x;.    int *aRig
34ad6 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74  ht = &aIdx[nLeft
34ad7 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69  ];..    SortByDi
34ad8 73 74 61 6e 63 65 28 61 4c 65 66 74 2c 20 6e 4c  stance(aLeft, nL
34ad9 65 66 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  eft, aDistance, 
34ada 61 53 70 61 72 65 29 3b 0a 20 20 20 20 53 6f 72  aSpare);.    Sor
34adb 74 42 79 44 69 73 74 61 6e 63 65 28 61 52 69 67  tByDistance(aRig
34adc 68 74 2c 20 6e 52 69 67 68 74 2c 20 61 44 69 73  ht, nRight, aDis
34add 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a  tance, aSpare);.
34ade 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 53 70 61  .    memcpy(aSpa
34adf 72 65 2c 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f  re, aLeft, sizeo
34ae0 66 28 69 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20  f(int)*nLeft);. 
34ae1 20 20 20 61 4c 65 66 74 20 3d 20 61 53 70 61 72     aLeft = aSpar
34ae2 65 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69  e;..    while( i
34ae3 4c 65 66 74 3c 6e 4c 65 66 74 20 7c 7c 20 69 52  Left<nLeft || iR
34ae4 69 67 68 74 3c 6e 52 69 67 68 74 20 29 7b 0a 20  ight<nRight ){. 
34ae5 20 20 20 20 20 69 66 28 20 69 4c 65 66 74 3d 3d       if( iLeft==
34ae6 6e 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  nLeft ){.       
34ae7 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
34ae8 68 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69  ht] = aRight[iRi
34ae9 67 68 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52  ght];.        iR
34aea 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65  ight++;.      }e
34aeb 6c 73 65 20 69 66 28 20 69 52 69 67 68 74 3d 3d  lse if( iRight==
34aec 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  nRight ){.      
34aed 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69    aIdx[iLeft+iRi
34aee 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65  ght] = aLeft[iLe
34aef 66 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65  ft];.        iLe
34af0 66 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ft++;.      }els
34af1 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  e{.        float
34af2 20 66 4c 65 66 74 20 3d 20 61 44 69 73 74 61 6e   fLeft = aDistan
34af3 63 65 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d  ce[aLeft[iLeft]]
34af4 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20  ;.        float 
34af5 66 52 69 67 68 74 20 3d 20 61 44 69 73 74 61 6e  fRight = aDistan
34af6 63 65 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74  ce[aRight[iRight
34af7 5d 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]];.        if( 
34af8 66 4c 65 66 74 3c 66 52 69 67 68 74 20 29 7b 0a  fLeft<fRight ){.
34af9 20 20 20 20 20 20 20 20 20 20 61 49 64 78 5b 69            aIdx[i
34afa 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
34afb 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20 20  Left[iLeft];.   
34afc 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a         iLeft++;.
34afd 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
34afe 20 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c           aIdx[iL
34aff 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52  eft+iRight] = aR
34b00 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20  ight[iRight];.  
34b01 20 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b          iRight++
34b02 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
34b03 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30    }.    }..#if 0
34b04 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
34b05 61 74 20 74 68 65 20 73 6f 72 74 20 77 6f 72 6b  at the sort work
34b06 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20  ed */.    {.    
34b07 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
34b08 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 49 64  for(jj=1; jj<nId
34b09 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  x; jj++){.      
34b0a 20 20 66 6c 6f 61 74 20 6c 65 66 74 20 3d 20 61    float left = a
34b0b 44 69 73 74 61 6e 63 65 5b 61 49 64 78 5b 6a 6a  Distance[aIdx[jj
34b0c 2d 31 5d 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  -1]];.        fl
34b0d 6f 61 74 20 72 69 67 68 74 20 3d 20 61 44 69 73  oat right = aDis
34b0e 74 61 6e 63 65 5b 61 49 64 78 5b 6a 6a 5d 5d 3b  tance[aIdx[jj]];
34b0f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
34b10 20 6c 65 66 74 3c 3d 72 69 67 68 74 20 29 3b 0a   left<=right );.
34b11 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
34b12 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ndif.  }.}../*.*
34b13 2a 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78  * Arguments aIdx
34b14 2c 20 61 43 65 6c 6c 20 61 6e 64 20 61 53 70 61  , aCell and aSpa
34b15 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20  re all point to 
34b16 61 72 72 61 79 73 20 6f 66 20 73 69 7a 65 0a 2a  arrays of size.*
34b17 2a 20 6e 49 64 78 2e 20 54 68 65 20 61 49 64 78  * nIdx. The aIdx
34b18 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
34b19 74 68 65 20 73 65 74 20 6f 66 20 69 6e 74 65 67  the set of integ
34b1a 65 72 73 20 66 72 6f 6d 20 30 20 74 6f 20 0a 2a  ers from 0 to .*
34b1b 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e 20 6e 6f  * (nIdx-1) in no
34b1c 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65   particular orde
34b1d 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
34b1e 20 73 6f 72 74 73 20 74 68 65 20 76 61 6c 75 65   sorts the value
34b1f 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20 61 63 63  s.** in aIdx acc
34b20 6f 72 64 69 6e 67 20 74 6f 20 64 69 6d 65 6e 73  ording to dimens
34b21 69 6f 6e 20 69 44 69 6d 20 6f 66 20 74 68 65 20  ion iDim of the 
34b22 63 65 6c 6c 73 20 69 6e 20 61 43 65 6c 6c 2e 20  cells in aCell. 
34b23 54 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 76  The.** minimum v
34b24 61 6c 75 65 20 6f 66 20 64 69 6d 65 6e 73 69 6f  alue of dimensio
34b25 6e 20 69 44 69 6d 20 69 73 20 63 6f 6e 73 69 64  n iDim is consid
34b26 65 72 65 64 20 66 69 72 73 74 2c 20 74 68 65 0a  ered first, the.
34b27 2a 2a 20 6d 61 78 69 6d 75 6d 20 75 73 65 64 20  ** maximum used 
34b28 74 6f 20 62 72 65 61 6b 20 74 69 65 73 2e 0a 2a  to break ties..*
34b29 2a 0a 2a 2a 20 54 68 65 20 61 53 70 61 72 65 20  *.** The aSpare 
34b2a 61 72 72 61 79 20 69 73 20 75 73 65 64 20 61 73  array is used as
34b2b 20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69   temporary worki
34b2c 6e 67 20 73 70 61 63 65 20 62 79 20 74 68 65 0a  ng space by the.
34b2d 2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67 6f 72  ** sorting algor
34b2e 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ithm..*/.static 
34b2f 76 6f 69 64 20 53 6f 72 74 42 79 44 69 6d 65 6e  void SortByDimen
34b30 73 69 6f 6e 28 0a 20 20 52 74 72 65 65 20 2a 70  sion(.  Rtree *p
34b31 52 74 72 65 65 2c 0a 20 20 69 6e 74 20 2a 61 49  Rtree,.  int *aI
34b32 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c  dx, .  int nIdx,
34b33 20 0a 20 20 69 6e 74 20 69 44 69 6d 2c 20 0a 20   .  int iDim, . 
34b34 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
34b35 6c 2c 20 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  l, .  int *aSpar
34b36 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49 64 78 3e  e.){.  if( nIdx>
34b37 31 20 29 7b 0a 0a 20 20 20 20 69 6e 74 20 69 4c  1 ){..    int iL
34b38 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  eft = 0;.    int
34b39 20 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20   iRight = 0;..  
34b3a 20 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20 6e 49    int nLeft = nI
34b3b 64 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20 6e 52  dx/2;.    int nR
34b3c 69 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c 65 66  ight = nIdx-nLef
34b3d 74 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c 65 66  t;.    int *aLef
34b3e 74 20 3d 20 61 49 64 78 3b 0a 20 20 20 20 69 6e  t = aIdx;.    in
34b3f 74 20 2a 61 52 69 67 68 74 20 3d 20 26 61 49 64  t *aRight = &aId
34b40 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20 20 53  x[nLeft];..    S
34b41 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70  ortByDimension(p
34b42 52 74 72 65 65 2c 20 61 4c 65 66 74 2c 20 6e 4c  Rtree, aLeft, nL
34b43 65 66 74 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c  eft, iDim, aCell
34b44 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 20 20 53  , aSpare);.    S
34b45 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70  ortByDimension(p
34b46 52 74 72 65 65 2c 20 61 52 69 67 68 74 2c 20 6e  Rtree, aRight, n
34b47 52 69 67 68 74 2c 20 69 44 69 6d 2c 20 61 43 65  Right, iDim, aCe
34b48 6c 6c 2c 20 61 53 70 61 72 65 29 3b 0a 0a 20 20  ll, aSpare);..  
34b49 20 20 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c    memcpy(aSpare,
34b4a 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69   aLeft, sizeof(i
34b4b 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20 20 20 20  nt)*nLeft);.    
34b4c 61 4c 65 66 74 20 3d 20 61 53 70 61 72 65 3b 0a  aLeft = aSpare;.
34b4d 20 20 20 20 77 68 69 6c 65 28 20 69 4c 65 66 74      while( iLeft
34b4e 3c 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67 68 74  <nLeft || iRight
34b4f 3c 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  <nRight ){.     
34b50 20 64 6f 75 62 6c 65 20 78 6c 65 66 74 31 20 3d   double xleft1 =
34b51 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 4c   DCOORD(aCell[aL
34b52 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43 6f 6f  eft[iLeft]].aCoo
34b53 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20  rd[iDim*2]);.   
34b54 20 20 20 64 6f 75 62 6c 65 20 78 6c 65 66 74 32     double xleft2
34b55 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
34b56 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43  aLeft[iLeft]].aC
34b57 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b  oord[iDim*2+1]);
34b58 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72  .      double xr
34b59 69 67 68 74 31 20 3d 20 44 43 4f 4f 52 44 28 61  ight1 = DCOORD(a
34b5a 43 65 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67  Cell[aRight[iRig
34b5b 68 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ht]].aCoord[iDim
34b5c 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75 62  *2]);.      doub
34b5d 6c 65 20 78 72 69 67 68 74 32 20 3d 20 44 43 4f  le xright2 = DCO
34b5e 4f 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68 74  ORD(aCell[aRight
34b5f 5b 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72 64  [iRight]].aCoord
34b60 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20  [iDim*2+1]);.   
34b61 20 20 20 69 66 28 20 28 69 4c 65 66 74 21 3d 6e     if( (iLeft!=n
34b62 4c 65 66 74 29 20 26 26 20 28 28 69 52 69 67 68  Left) && ((iRigh
34b63 74 3d 3d 6e 52 69 67 68 74 29 0a 20 20 20 20 20  t==nRight).     
34b64 20 20 7c 7c 20 28 78 6c 65 66 74 31 3c 78 72 69    || (xleft1<xri
34b65 67 68 74 31 29 0a 20 20 20 20 20 20 20 7c 7c 20  ght1).       || 
34b66 28 78 6c 65 66 74 31 3d 3d 78 72 69 67 68 74 31  (xleft1==xright1
34b67 20 26 26 20 78 6c 65 66 74 32 3c 78 72 69 67 68   && xleft2<xrigh
34b68 74 32 29 0a 20 20 20 20 20 20 29 29 7b 0a 20 20  t2).      )){.  
34b69 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74        aIdx[iLeft
34b6a 2b 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74  +iRight] = aLeft
34b6b 5b 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20 20 20  [iLeft];.       
34b6c 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20 20   iLeft++;.      
34b6d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
34b6e 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
34b6f 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68  ] = aRight[iRigh
34b70 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52 69 67  t];.        iRig
34b71 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ht++;.      }.  
34b72 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f    }..#if 0.    /
34b73 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
34b74 20 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f 0a   sort worked */.
34b75 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20      {.      int 
34b76 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a  jj;.      for(jj
34b77 3d 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b  =1; jj<nIdx; jj+
34b78 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61  +){.        floa
34b79 74 20 78 6c 65 66 74 31 20 3d 20 61 43 65 6c 6c  t xleft1 = aCell
34b7a 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f  [aIdx[jj-1]].aCo
34b7b 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20 20  ord[iDim*2];.   
34b7c 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65 66 74       float xleft
34b7d 32 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a  2 = aCell[aIdx[j
34b7e 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  j-1]].aCoord[iDi
34b7f 6d 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20  m*2+1];.        
34b80 66 6c 6f 61 74 20 78 72 69 67 68 74 31 20 3d 20  float xright1 = 
34b81 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d 2e  aCell[aIdx[jj]].
34b82 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a  aCoord[iDim*2];.
34b83 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78 72          float xr
34b84 69 67 68 74 32 20 3d 20 61 43 65 6c 6c 5b 61 49  ight2 = aCell[aI
34b85 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b 69  dx[jj]].aCoord[i
34b86 44 69 6d 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20  Dim*2+1];.      
34b87 20 20 61 73 73 65 72 74 28 20 78 6c 65 66 74 31    assert( xleft1
34b88 3c 3d 78 72 69 67 68 74 31 20 26 26 20 28 78 6c  <=xright1 && (xl
34b89 65 66 74 31 3c 78 72 69 67 68 74 31 20 7c 7c 20  eft1<xright1 || 
34b8a 78 6c 65 66 74 32 3c 3d 78 72 69 67 68 74 32 29  xleft2<=xright2)
34b8b 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
34b8c 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a  }.#endif.  }.}..
34b8d 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41  #if VARIANT_RSTA
34b8e 52 54 52 45 45 5f 53 50 4c 49 54 0a 2f 2a 0a 2a  RTREE_SPLIT./*.*
34b8f 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
34b90 20 6f 66 20 74 68 65 20 52 2a 2d 74 72 65 65 20   of the R*-tree 
34b91 76 61 72 69 61 6e 74 20 6f 66 20 53 70 6c 69 74  variant of Split
34b92 4e 6f 64 65 20 66 72 6f 6d 20 42 65 63 6b 6d 61  Node from Beckma
34b93 6e 5b 31 39 39 30 5d 2e 0a 2a 2f 0a 73 74 61 74  n[1990]..*/.stat
34b94 69 63 20 69 6e 74 20 73 70 6c 69 74 4e 6f 64 65  ic int splitNode
34b95 53 74 61 72 74 72 65 65 28 0a 20 20 52 74 72 65  Startree(.  Rtre
34b96 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
34b97 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0a 20  eeCell *aCell,. 
34b98 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20 52 74   int nCell,.  Rt
34b99 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 2c 0a  reeNode *pLeft,.
34b9a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 69    RtreeNode *pRi
34b9b 67 68 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ght,.  RtreeCell
34b9c 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20 20 52   *pBboxLeft,.  R
34b9d 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 52  treeCell *pBboxR
34b9e 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 2a  ight.){.  int **
34b9f 61 61 53 6f 72 74 65 64 3b 0a 20 20 69 6e 74 20  aaSorted;.  int 
34ba0 2a 61 53 70 61 72 65 3b 0a 20 20 69 6e 74 20 69  *aSpare;.  int i
34ba1 69 3b 0a 0a 20 20 69 6e 74 20 69 42 65 73 74 44  i;..  int iBestD
34ba2 69 6d 3b 0a 20 20 69 6e 74 20 69 42 65 73 74 53  im;.  int iBestS
34ba3 70 6c 69 74 3b 0a 20 20 66 6c 6f 61 74 20 66 42  plit;.  float fB
34ba4 65 73 74 4d 61 72 67 69 6e 3b 0a 0a 20 20 69 6e  estMargin;..  in
34ba5 74 20 6e 42 79 74 65 20 3d 20 28 70 52 74 72 65  t nByte = (pRtre
34ba6 65 2d 3e 6e 44 69 6d 2b 31 29 2a 28 73 69 7a 65  e->nDim+1)*(size
34ba7 6f 66 28 69 6e 74 2a 29 2b 6e 43 65 6c 6c 2a 73  of(int*)+nCell*s
34ba8 69 7a 65 6f 66 28 69 6e 74 29 29 3b 0a 0a 20 20  izeof(int));..  
34ba9 61 61 53 6f 72 74 65 64 20 3d 20 28 69 6e 74 20  aaSorted = (int 
34baa 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  **)sqlite3_mallo
34bab 63 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20  c(nByte);.  if( 
34bac 21 61 61 53 6f 72 74 65 64 20 29 7b 0a 20 20 20  !aaSorted ){.   
34bad 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
34bae 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61 53 70  OMEM;.  }..  aSp
34baf 61 72 65 20 3d 20 26 28 28 69 6e 74 20 2a 29 26  are = &((int *)&
34bb0 61 61 53 6f 72 74 65 64 5b 70 52 74 72 65 65 2d  aaSorted[pRtree-
34bb1 3e 6e 44 69 6d 5d 29 5b 70 52 74 72 65 65 2d 3e  >nDim])[pRtree->
34bb2 6e 44 69 6d 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 6d  nDim*nCell];.  m
34bb3 65 6d 73 65 74 28 61 61 53 6f 72 74 65 64 2c 20  emset(aaSorted, 
34bb4 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 66 6f 72  0, nByte);.  for
34bb5 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72 65 65  (ii=0; ii<pRtree
34bb6 2d 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29 7b 0a 20  ->nDim; ii++){. 
34bb7 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 61     int jj;.    a
34bb8 61 53 6f 72 74 65 64 5b 69 69 5d 20 3d 20 26 28  aSorted[ii] = &(
34bb9 28 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65 64  (int *)&aaSorted
34bba 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29 5b  [pRtree->nDim])[
34bbb 69 69 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 20 20 66  ii*nCell];.    f
34bbc 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e 43 65 6c  or(jj=0; jj<nCel
34bbd 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  l; jj++){.      
34bbe 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6a 6a 5d  aaSorted[ii][jj]
34bbf 20 3d 20 6a 6a 3b 0a 20 20 20 20 7d 0a 20 20 20   = jj;.    }.   
34bc0 20 53 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e   SortByDimension
34bc1 28 70 52 74 72 65 65 2c 20 61 61 53 6f 72 74 65  (pRtree, aaSorte
34bc2 64 5b 69 69 5d 2c 20 6e 43 65 6c 6c 2c 20 69 69  d[ii], nCell, ii
34bc3 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65 29  , aCell, aSpare)
34bc4 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 69 3d  ;.  }..  for(ii=
34bc5 30 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44  0; ii<pRtree->nD
34bc6 69 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66  im; ii++){.    f
34bc7 6c 6f 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e  loat margin = 0.
34bc8 30 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 42 65  0;.    float fBe
34bc9 73 74 4f 76 65 72 6c 61 70 3b 0a 20 20 20 20 66  stOverlap;.    f
34bca 6c 6f 61 74 20 66 42 65 73 74 41 72 65 61 3b 0a  loat fBestArea;.
34bcb 20 20 20 20 69 6e 74 20 69 42 65 73 74 4c 65 66      int iBestLef
34bcc 74 3b 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66 74  t;.    int nLeft
34bcd 3b 0a 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20  ;..    for(.    
34bce 20 20 6e 4c 65 66 74 3d 52 54 52 45 45 5f 4d 49    nLeft=RTREE_MI
34bcf 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 3b 20  NCELLS(pRtree); 
34bd0 0a 20 20 20 20 20 20 6e 4c 65 66 74 3c 3d 28 6e  .      nLeft<=(n
34bd1 43 65 6c 6c 2d 52 54 52 45 45 5f 4d 49 4e 43 45  Cell-RTREE_MINCE
34bd2 4c 4c 53 28 70 52 74 72 65 65 29 29 3b 20 0a 20  LLS(pRtree)); . 
34bd3 20 20 20 20 20 6e 4c 65 66 74 2b 2b 0a 20 20 20       nLeft++.   
34bd4 20 29 7b 0a 20 20 20 20 20 20 52 74 72 65 65 43   ){.      RtreeC
34bd5 65 6c 6c 20 6c 65 66 74 3b 0a 20 20 20 20 20 20  ell left;.      
34bd6 52 74 72 65 65 43 65 6c 6c 20 72 69 67 68 74 3b  RtreeCell right;
34bd7 0a 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b 0a 20  .      int kk;. 
34bd8 20 20 20 20 20 66 6c 6f 61 74 20 6f 76 65 72 6c       float overl
34bd9 61 70 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  ap;.      float 
34bda 61 72 65 61 3b 0a 0a 20 20 20 20 20 20 6d 65 6d  area;..      mem
34bdb 63 70 79 28 26 6c 65 66 74 2c 20 26 61 43 65 6c  cpy(&left, &aCel
34bdc 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 30  l[aaSorted[ii][0
34bdd 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ]], sizeof(Rtree
34bde 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20 20 6d 65  Cell));.      me
34bdf 6d 63 70 79 28 26 72 69 67 68 74 2c 20 26 61 43  mcpy(&right, &aC
34be0 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d  ell[aaSorted[ii]
34be1 5b 6e 43 65 6c 6c 2d 31 5d 5d 2c 20 73 69 7a 65  [nCell-1]], size
34be2 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a  of(RtreeCell));.
34be3 20 20 20 20 20 20 66 6f 72 28 6b 6b 3d 31 3b 20        for(kk=1; 
34be4 6b 6b 3c 28 6e 43 65 6c 6c 2d 31 29 3b 20 6b 6b  kk<(nCell-1); kk
34be5 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
34be6 20 6b 6b 3c 6e 4c 65 66 74 20 29 7b 0a 20 20 20   kk<nLeft ){.   
34be7 20 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e         cellUnion
34be8 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74 2c 20  (pRtree, &left, 
34be9 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
34bea 69 69 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20  ii][kk]]);.     
34beb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
34bec 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
34bed 74 72 65 65 2c 20 26 72 69 67 68 74 2c 20 26 61  tree, &right, &a
34bee 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69  Cell[aaSorted[ii
34bef 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20 20 20  ][kk]]);.       
34bf0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
34bf1 20 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c 4d   margin += cellM
34bf2 61 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26 6c  argin(pRtree, &l
34bf3 65 66 74 29 3b 0a 20 20 20 20 20 20 6d 61 72 67  eft);.      marg
34bf4 69 6e 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e  in += cellMargin
34bf5 28 70 52 74 72 65 65 2c 20 26 72 69 67 68 74 29  (pRtree, &right)
34bf6 3b 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20  ;.      overlap 
34bf7 3d 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52  = cellOverlap(pR
34bf8 74 72 65 65 2c 20 26 6c 65 66 74 2c 20 26 72 69  tree, &left, &ri
34bf9 67 68 74 2c 20 31 2c 20 2d 31 29 3b 0a 20 20 20  ght, 1, -1);.   
34bfa 20 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41 72     area = cellAr
34bfb 65 61 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74  ea(pRtree, &left
34bfc 29 20 2b 20 63 65 6c 6c 41 72 65 61 28 70 52 74  ) + cellArea(pRt
34bfd 72 65 65 2c 20 26 72 69 67 68 74 29 3b 0a 20 20  ree, &right);.  
34bfe 20 20 20 20 69 66 28 20 28 6e 4c 65 66 74 3d 3d      if( (nLeft==
34bff 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
34c00 52 74 72 65 65 29 29 0a 20 20 20 20 20 20 20 7c  Rtree)).       |
34c01 7c 20 28 6f 76 65 72 6c 61 70 3c 66 42 65 73 74  | (overlap<fBest
34c02 4f 76 65 72 6c 61 70 29 0a 20 20 20 20 20 20 20  Overlap).       
34c03 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 42 65  || (overlap==fBe
34c04 73 74 4f 76 65 72 6c 61 70 20 26 26 20 61 72 65  stOverlap && are
34c05 61 3c 66 42 65 73 74 41 72 65 61 29 0a 20 20 20  a<fBestArea).   
34c06 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 42     ){.        iB
34c07 65 73 74 4c 65 66 74 20 3d 20 6e 4c 65 66 74 3b  estLeft = nLeft;
34c08 0a 20 20 20 20 20 20 20 20 66 42 65 73 74 4f 76  .        fBestOv
34c09 65 72 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b  erlap = overlap;
34c0a 0a 20 20 20 20 20 20 20 20 66 42 65 73 74 41 72  .        fBestAr
34c0b 65 61 20 3d 20 61 72 65 61 3b 0a 20 20 20 20 20  ea = area;.     
34c0c 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
34c0d 28 20 69 69 3d 3d 30 20 7c 7c 20 6d 61 72 67 69  ( ii==0 || margi
34c0e 6e 3c 66 42 65 73 74 4d 61 72 67 69 6e 20 29 7b  n<fBestMargin ){
34c0f 0a 20 20 20 20 20 20 69 42 65 73 74 44 69 6d 20  .      iBestDim 
34c10 3d 20 69 69 3b 0a 20 20 20 20 20 20 66 42 65 73  = ii;.      fBes
34c11 74 4d 61 72 67 69 6e 20 3d 20 6d 61 72 67 69 6e  tMargin = margin
34c12 3b 0a 20 20 20 20 20 20 69 42 65 73 74 53 70 6c  ;.      iBestSpl
34c13 69 74 20 3d 20 69 42 65 73 74 4c 65 66 74 3b 0a  it = iBestLeft;.
34c14 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6d 65 6d      }.  }..  mem
34c15 63 70 79 28 70 42 62 6f 78 4c 65 66 74 2c 20 26  cpy(pBboxLeft, &
34c16 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69  aCell[aaSorted[i
34c17 42 65 73 74 44 69 6d 5d 5b 30 5d 5d 2c 20 73 69  BestDim][0]], si
34c18 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
34c19 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78  ;.  memcpy(pBbox
34c1a 52 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61  Right, &aCell[aa
34c1b 53 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d  Sorted[iBestDim]
34c1c 5b 69 42 65 73 74 53 70 6c 69 74 5d 5d 2c 20 73  [iBestSplit]], s
34c1d 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
34c1e 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
34c1f 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a  i<nCell; ii++){.
34c20 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
34c21 54 61 72 67 65 74 20 3d 20 28 69 69 3c 69 42 65  Target = (ii<iBe
34c22 73 74 53 70 6c 69 74 29 3f 70 4c 65 66 74 3a 70  stSplit)?pLeft:p
34c23 52 69 67 68 74 3b 0a 20 20 20 20 52 74 72 65 65  Right;.    Rtree
34c24 43 65 6c 6c 20 2a 70 42 62 6f 78 20 3d 20 28 69  Cell *pBbox = (i
34c25 69 3c 69 42 65 73 74 53 70 6c 69 74 29 3f 70 42  i<iBestSplit)?pB
34c26 62 6f 78 4c 65 66 74 3a 70 42 62 6f 78 52 69 67  boxLeft:pBboxRig
34c27 68 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c  ht;.    RtreeCel
34c28 6c 20 2a 70 43 65 6c 6c 20 3d 20 26 61 43 65 6c  l *pCell = &aCel
34c29 6c 5b 61 61 53 6f 72 74 65 64 5b 69 42 65 73 74  l[aaSorted[iBest
34c2a 44 69 6d 5d 5b 69 69 5d 5d 3b 0a 20 20 20 20 6e  Dim][ii]];.    n
34c2b 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
34c2c 74 72 65 65 2c 20 70 54 61 72 67 65 74 2c 20 70  tree, pTarget, p
34c2d 43 65 6c 6c 29 3b 0a 20 20 20 20 63 65 6c 6c 55  Cell);.    cellU
34c2e 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62  nion(pRtree, pBb
34c2f 6f 78 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 7d 0a  ox, pCell);.  }.
34c30 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
34c31 61 61 53 6f 72 74 65 64 29 3b 0a 20 20 72 65 74  aaSorted);.  ret
34c32 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
34c33 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 56 41 52  .#endif..#if VAR
34c34 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c  IANT_GUTTMAN_SPL
34c35 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  IT./*.** Impleme
34c36 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72  ntation of the r
34c37 65 67 75 6c 61 72 20 52 2d 74 72 65 65 20 53 70  egular R-tree Sp
34c38 6c 69 74 4e 6f 64 65 20 66 72 6f 6d 20 47 75 74  litNode from Gut
34c39 74 6d 61 6e 5b 31 39 38 34 5d 2e 0a 2a 2f 0a 73  tman[1984]..*/.s
34c3a 74 61 74 69 63 20 69 6e 74 20 73 70 6c 69 74 4e  tatic int splitN
34c3b 6f 64 65 47 75 74 74 6d 61 6e 28 0a 20 20 52 74  odeGuttman(.  Rt
34c3c 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52  ree *pRtree,.  R
34c3d 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c  treeCell *aCell,
34c3e 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20  .  int nCell,.  
34c3f 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74  RtreeNode *pLeft
34c40 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ,.  RtreeNode *p
34c41 52 69 67 68 74 2c 0a 20 20 52 74 72 65 65 43 65  Right,.  RtreeCe
34c42 6c 6c 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20  ll *pBboxLeft,. 
34c43 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f   RtreeCell *pBbo
34c44 78 52 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20  xRight.){.  int 
34c45 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20  iLeftSeed = 0;. 
34c46 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64 20   int iRightSeed 
34c47 3d 20 31 3b 0a 20 20 69 6e 74 20 2a 61 69 55 73  = 1;.  int *aiUs
34c48 65 64 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ed;.  int i;..  
34c49 61 69 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33  aiUsed = sqlite3
34c4a 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 69  _malloc(sizeof(i
34c4b 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20 6d 65  nt)*nCell);.  me
34c4c 6d 73 65 74 28 61 69 55 73 65 64 2c 20 30 2c 20  mset(aiUsed, 0, 
34c4d 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65 6c  sizeof(int)*nCel
34c4e 6c 29 3b 0a 0a 20 20 50 69 63 6b 53 65 65 64 73  l);..  PickSeeds
34c4f 28 70 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20  (pRtree, aCell, 
34c50 6e 43 65 6c 6c 2c 20 26 69 4c 65 66 74 53 65 65  nCell, &iLeftSee
34c51 64 2c 20 26 69 52 69 67 68 74 53 65 65 64 29 3b  d, &iRightSeed);
34c52 0a 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78  ..  memcpy(pBbox
34c53 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65  Left, &aCell[iLe
34c54 66 74 53 65 65 64 5d 2c 20 73 69 7a 65 6f 66 28  ftSeed], sizeof(
34c55 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d  RtreeCell));.  m
34c56 65 6d 63 70 79 28 70 42 62 6f 78 52 69 67 68 74  emcpy(pBboxRight
34c57 2c 20 26 61 43 65 6c 6c 5b 69 52 69 67 68 74 53  , &aCell[iRightS
34c58 65 65 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72  eed], sizeof(Rtr
34c59 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6e 6f 64 65  eeCell));.  node
34c5a 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65  InsertCell(pRtre
34c5b 65 2c 20 70 4c 65 66 74 2c 20 26 61 43 65 6c 6c  e, pLeft, &aCell
34c5c 5b 69 4c 65 66 74 53 65 65 64 5d 29 3b 0a 20 20  [iLeftSeed]);.  
34c5d 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  nodeInsertCell(p
34c5e 52 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20 26  Rtree, pRight, &
34c5f 61 43 65 6c 6c 5b 69 52 69 67 68 74 53 65 65 64  aCell[iRightSeed
34c60 5d 29 3b 0a 20 20 61 69 55 73 65 64 5b 69 4c 65  ]);.  aiUsed[iLe
34c61 66 74 53 65 65 64 5d 20 3d 20 31 3b 0a 20 20 61  ftSeed] = 1;.  a
34c62 69 55 73 65 64 5b 69 52 69 67 68 74 53 65 65 64  iUsed[iRightSeed
34c63 5d 20 3d 20 31 3b 0a 0a 20 20 66 6f 72 28 69 3d  ] = 1;..  for(i=
34c64 6e 43 65 6c 6c 2d 32 3b 20 69 3e 30 3b 20 69 2d  nCell-2; i>0; i-
34c65 2d 29 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c  -){.    RtreeCel
34c66 6c 20 2a 70 4e 65 78 74 3b 0a 20 20 20 20 70 4e  l *pNext;.    pN
34c67 65 78 74 20 3d 20 50 69 63 6b 4e 65 78 74 28 70  ext = PickNext(p
34c68 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43  Rtree, aCell, nC
34c69 65 6c 6c 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20  ell, pBboxLeft, 
34c6a 70 42 62 6f 78 52 69 67 68 74 2c 20 61 69 55 73  pBboxRight, aiUs
34c6b 65 64 29 3b 0a 20 20 20 20 66 6c 6f 61 74 20 64  ed);.    float d
34c6c 69 66 66 20 3d 20 20 0a 20 20 20 20 20 20 63 65  iff =  .      ce
34c6d 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c  llGrowth(pRtree,
34c6e 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78   pBboxLeft, pNex
34c6f 74 29 20 2d 20 0a 20 20 20 20 20 20 63 65 6c 6c  t) - .      cell
34c70 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70  Growth(pRtree, p
34c71 42 62 6f 78 52 69 67 68 74 2c 20 70 4e 65 78 74  BboxRight, pNext
34c72 29 0a 20 20 20 20 3b 0a 20 20 20 20 69 66 28 20  ).    ;.    if( 
34c73 28 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28  (RTREE_MINCELLS(
34c74 70 52 74 72 65 65 29 2d 4e 43 45 4c 4c 28 70 52  pRtree)-NCELL(pR
34c75 69 67 68 74 29 3d 3d 69 29 0a 20 20 20 20 20 7c  ight)==i).     |
34c76 7c 20 28 64 69 66 66 3e 30 2e 30 20 26 26 20 28  | (diff>0.0 && (
34c77 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
34c78 52 74 72 65 65 29 2d 4e 43 45 4c 4c 28 70 4c 65  Rtree)-NCELL(pLe
34c79 66 74 29 21 3d 69 29 29 0a 20 20 20 20 29 7b 0a  ft)!=i)).    ){.
34c7a 20 20 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74        nodeInsert
34c7b 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69  Cell(pRtree, pRi
34c7c 67 68 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20  ght, pNext);.   
34c7d 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74     cellUnion(pRt
34c7e 72 65 65 2c 20 70 42 62 6f 78 52 69 67 68 74 2c  ree, pBboxRight,
34c7f 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c   pNext);.    }el
34c80 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64 65 49 6e  se{.      nodeIn
34c81 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
34c82 20 70 4c 65 66 74 2c 20 70 4e 65 78 74 29 3b 0a   pLeft, pNext);.
34c83 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28        cellUnion(
34c84 70 52 74 72 65 65 2c 20 70 42 62 6f 78 4c 65 66  pRtree, pBboxLef
34c85 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 7d  t, pNext);.    }
34c86 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
34c87 66 72 65 65 28 61 69 55 73 65 64 29 3b 0a 20 20  free(aiUsed);.  
34c88 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
34c89 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74  ;.}.#endif..stat
34c8a 69 63 20 69 6e 74 20 75 70 64 61 74 65 4d 61 70  ic int updateMap
34c8b 70 69 6e 67 28 0a 20 20 52 74 72 65 65 20 2a 70  ping(.  Rtree *p
34c8c 52 74 72 65 65 2c 20 0a 20 20 69 36 34 20 69 52  Rtree, .  i64 iR
34c8d 6f 77 69 64 2c 20 0a 20 20 52 74 72 65 65 4e 6f  owid, .  RtreeNo
34c8e 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e  de *pNode, .  in
34c8f 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69  t iHeight.){.  i
34c90 6e 74 20 28 2a 78 53 65 74 4d 61 70 70 69 6e 67  nt (*xSetMapping
34c91 29 28 52 74 72 65 65 20 2a 2c 20 73 71 6c 69 74  )(Rtree *, sqlit
34c92 65 33 5f 69 6e 74 36 34 2c 20 73 71 6c 69 74 65  e3_int64, sqlite
34c93 33 5f 69 6e 74 36 34 29 3b 0a 20 20 78 53 65 74  3_int64);.  xSet
34c94 4d 61 70 70 69 6e 67 20 3d 20 28 28 69 48 65 69  Mapping = ((iHei
34c95 67 68 74 3d 3d 30 29 3f 72 6f 77 69 64 57 72 69  ght==0)?rowidWri
34c96 74 65 3a 70 61 72 65 6e 74 57 72 69 74 65 29 3b  te:parentWrite);
34c97 0a 20 20 69 66 28 20 69 48 65 69 67 68 74 3e 30  .  if( iHeight>0
34c98 20 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64   ){.    RtreeNod
34c99 65 20 2a 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65  e *pChild = node
34c9a 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65  HashLookup(pRtre
34c9b 65 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 20 20  e, iRowid);.    
34c9c 69 66 28 20 70 43 68 69 6c 64 20 29 7b 0a 20 20  if( pChild ){.  
34c9d 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
34c9e 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2d 3e  pRtree, pChild->
34c9f 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20  pParent);.      
34ca0 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 4e  nodeReference(pN
34ca1 6f 64 65 29 3b 0a 20 20 20 20 20 20 70 43 68 69  ode);.      pChi
34ca2 6c 64 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e  ld->pParent = pN
34ca3 6f 64 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ode;.    }.  }. 
34ca4 20 72 65 74 75 72 6e 20 78 53 65 74 4d 61 70 70   return xSetMapp
34ca5 69 6e 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77  ing(pRtree, iRow
34ca6 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  id, pNode->iNode
34ca7 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  );.}..static int
34ca8 20 53 70 6c 69 74 4e 6f 64 65 28 0a 20 20 52 74   SplitNode(.  Rt
34ca9 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52  ree *pRtree,.  R
34caa 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
34cab 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
34cac 65 6c 6c 2c 0a 20 20 69 6e 74 20 69 48 65 69 67  ell,.  int iHeig
34cad 68 74 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ht.){.  int i;. 
34cae 20 69 6e 74 20 6e 65 77 43 65 6c 6c 49 73 52 69   int newCellIsRi
34caf 67 68 74 20 3d 20 30 3b 0a 0a 20 20 69 6e 74 20  ght = 0;..  int 
34cb0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
34cb1 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
34cb2 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 52 74  ELL(pNode);.  Rt
34cb3 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a  reeCell *aCell;.
34cb4 20 20 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 0a    int *aiUsed;..
34cb5 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65    RtreeNode *pLe
34cb6 66 74 20 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e  ft = 0;.  RtreeN
34cb7 6f 64 65 20 2a 70 52 69 67 68 74 20 3d 20 30 3b  ode *pRight = 0;
34cb8 0a 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 6c 65  ..  RtreeCell le
34cb9 66 74 62 62 6f 78 3b 0a 20 20 52 74 72 65 65 43  ftbbox;.  RtreeC
34cba 65 6c 6c 20 72 69 67 68 74 62 62 6f 78 3b 0a 0a  ell rightbbox;..
34cbb 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e    /* Allocate an
34cbc 20 61 72 72 61 79 20 61 6e 64 20 70 6f 70 75 6c   array and popul
34cbd 61 74 65 20 69 74 20 77 69 74 68 20 61 20 63 6f  ate it with a co
34cbe 70 79 20 6f 66 20 70 43 65 6c 6c 20 61 6e 64 20  py of pCell and 
34cbf 0a 20 20 2a 2a 20 61 6c 6c 20 63 65 6c 6c 73 20  .  ** all cells 
34cc0 66 72 6f 6d 20 6e 6f 64 65 20 70 4c 65 66 74 2e  from node pLeft.
34cc1 20 54 68 65 6e 20 7a 65 72 6f 20 74 68 65 20 6f   Then zero the o
34cc2 72 69 67 69 6e 61 6c 20 6e 6f 64 65 2e 0a 20 20  riginal node..  
34cc3 2a 2f 0a 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c  */.  aCell = sql
34cc4 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 28 73 69 7a  ite3_malloc((siz
34cc5 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 2b 73  eof(RtreeCell)+s
34cc6 69 7a 65 6f 66 28 69 6e 74 29 29 2a 28 6e 43 65  izeof(int))*(nCe
34cc7 6c 6c 2b 31 29 29 3b 0a 20 20 69 66 28 20 21 61  ll+1));.  if( !a
34cc8 43 65 6c 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  Cell ){.    rc =
34cc9 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
34cca 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64     goto splitnod
34ccb 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 69 55  e_out;.  }.  aiU
34ccc 73 65 64 20 3d 20 28 69 6e 74 20 2a 29 26 61 43  sed = (int *)&aC
34ccd 65 6c 6c 5b 6e 43 65 6c 6c 2b 31 5d 3b 0a 20 20  ell[nCell+1];.  
34cce 6d 65 6d 73 65 74 28 61 69 55 73 65 64 2c 20 30  memset(aiUsed, 0
34ccf 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e  , sizeof(int)*(n
34cd0 43 65 6c 6c 2b 31 29 29 3b 0a 20 20 66 6f 72 28  Cell+1));.  for(
34cd1 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b  i=0; i<nCell; i+
34cd2 2b 29 7b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43  +){.    nodeGetC
34cd3 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ell(pRtree, pNod
34cd4 65 2c 20 69 2c 20 26 61 43 65 6c 6c 5b 69 5d 29  e, i, &aCell[i])
34cd5 3b 0a 20 20 7d 0a 20 20 6e 6f 64 65 5a 65 72 6f  ;.  }.  nodeZero
34cd6 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
34cd7 0a 20 20 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c  .  memcpy(&aCell
34cd8 5b 6e 43 65 6c 6c 5d 2c 20 70 43 65 6c 6c 2c 20  [nCell], pCell, 
34cd9 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c  sizeof(RtreeCell
34cda 29 29 3b 0a 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 0a  ));.  nCell++;..
34cdb 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
34cdc 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 70 52 69  de==1 ){.    pRi
34cdd 67 68 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52  ght = nodeNew(pR
34cde 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 31 29 3b  tree, pNode, 1);
34cdf 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 6e 6f 64  .    pLeft = nod
34ce0 65 4e 65 77 28 70 52 74 72 65 65 2c 20 70 4e 6f  eNew(pRtree, pNo
34ce1 64 65 2c 20 31 29 3b 0a 20 20 20 20 70 52 74 72  de, 1);.    pRtr
34ce2 65 65 2d 3e 69 44 65 70 74 68 2b 2b 3b 0a 20 20  ee->iDepth++;.  
34ce3 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79    pNode->isDirty
34ce4 20 3d 20 31 3b 0a 20 20 20 20 77 72 69 74 65 49   = 1;.    writeI
34ce5 6e 74 31 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74  nt16(pNode->zDat
34ce6 61 2c 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74  a, pRtree->iDept
34ce7 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  h);.  }else{.   
34ce8 20 70 4c 65 66 74 20 3d 20 70 4e 6f 64 65 3b 0a   pLeft = pNode;.
34ce9 20 20 20 20 70 52 69 67 68 74 20 3d 20 6e 6f 64      pRight = nod
34cea 65 4e 65 77 28 70 52 74 72 65 65 2c 20 70 4c 65  eNew(pRtree, pLe
34ceb 66 74 2d 3e 70 50 61 72 65 6e 74 2c 20 31 29 3b  ft->pParent, 1);
34cec 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e  .    nodeReferen
34ced 63 65 28 70 4c 65 66 74 29 3b 0a 20 20 7d 0a 0a  ce(pLeft);.  }..
34cee 20 20 69 66 28 20 21 70 4c 65 66 74 20 7c 7c 20    if( !pLeft || 
34cef 21 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 72  !pRight ){.    r
34cf0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
34cf1 3b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74  ;.    goto split
34cf2 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  node_out;.  }.. 
34cf3 20 6d 65 6d 73 65 74 28 70 4c 65 66 74 2d 3e 7a   memset(pLeft->z
34cf4 44 61 74 61 2c 20 30 2c 20 70 52 74 72 65 65 2d  Data, 0, pRtree-
34cf5 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 6d  >iNodeSize);.  m
34cf6 65 6d 73 65 74 28 70 52 69 67 68 74 2d 3e 7a 44  emset(pRight->zD
34cf7 61 74 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e  ata, 0, pRtree->
34cf8 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 0a 20 20 72  iNodeSize);..  r
34cf9 63 20 3d 20 41 73 73 69 67 6e 43 65 6c 6c 73 28  c = AssignCells(
34cfa 70 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e  pRtree, aCell, n
34cfb 43 65 6c 6c 2c 20 70 4c 65 66 74 2c 20 70 52 69  Cell, pLeft, pRi
34cfc 67 68 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20  ght, &leftbbox, 
34cfd 26 72 69 67 68 74 62 62 6f 78 29 3b 0a 20 20 69  &rightbbox);.  i
34cfe 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
34cff 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c   ){.    goto spl
34d00 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a  itnode_out;.  }.
34d01 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 62 6f 74  .  /* Ensure bot
34d02 68 20 63 68 69 6c 64 20 6e 6f 64 65 73 20 68 61  h child nodes ha
34d03 76 65 20 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20  ve node numbers 
34d04 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 65 6d  assigned to them
34d05 2e 20 2a 2f 0a 20 20 69 66 28 20 28 30 3d 3d 70  . */.  if( (0==p
34d06 52 69 67 68 74 2d 3e 69 4e 6f 64 65 20 26 26 20  Right->iNode && 
34d07 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
34d08 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74 72 65   nodeWrite(pRtre
34d09 65 2c 20 70 52 69 67 68 74 29 29 29 0a 20 20 20  e, pRight))).   
34d0a 7c 7c 20 28 30 3d 3d 70 4c 65 66 74 2d 3e 69 4e  || (0==pLeft->iN
34d0b 6f 64 65 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b  ode && SQLITE_OK
34d0c 21 3d 28 72 63 20 3d 20 6e 6f 64 65 57 72 69 74  !=(rc = nodeWrit
34d0d 65 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29  e(pRtree, pLeft)
34d0e 29 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f  )).  ){.    goto
34d0f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
34d10 20 20 7d 0a 0a 20 20 72 69 67 68 74 62 62 6f 78    }..  rightbbox
34d11 2e 69 52 6f 77 69 64 20 3d 20 70 52 69 67 68 74  .iRowid = pRight
34d12 2d 3e 69 4e 6f 64 65 3b 0a 20 20 6c 65 66 74 62  ->iNode;.  leftb
34d13 62 6f 78 2e 69 52 6f 77 69 64 20 3d 20 70 4c 65  box.iRowid = pLe
34d14 66 74 2d 3e 69 4e 6f 64 65 3b 0a 0a 20 20 69 66  ft->iNode;..  if
34d15 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d  ( pNode->iNode==
34d16 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 74  1 ){.    rc = rt
34d17 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  reeInsertCell(pR
34d18 74 72 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61  tree, pLeft->pPa
34d19 72 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c  rent, &leftbbox,
34d1a 20 69 48 65 69 67 68 74 2b 31 29 3b 0a 20 20 20   iHeight+1);.   
34d1b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
34d1c 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  OK ){.      goto
34d1d 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
34d1e 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
34d1f 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50     RtreeNode *pP
34d20 61 72 65 6e 74 20 3d 20 70 4c 65 66 74 2d 3e 70  arent = pLeft->p
34d21 50 61 72 65 6e 74 3b 0a 20 20 20 20 69 6e 74 20  Parent;.    int 
34d22 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65  iCell = nodePare
34d23 6e 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20  ntIndex(pRtree, 
34d24 70 4c 65 66 74 29 3b 0a 20 20 20 20 6e 6f 64 65  pLeft);.    node
34d25 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52  OverwriteCell(pR
34d26 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26  tree, pParent, &
34d27 6c 65 66 74 62 62 6f 78 2c 20 69 43 65 6c 6c 29  leftbbox, iCell)
34d28 3b 0a 20 20 20 20 41 64 6a 75 73 74 54 72 65 65  ;.    AdjustTree
34d29 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74  (pRtree, pParent
34d2a 2c 20 26 6c 65 66 74 62 62 6f 78 29 3b 0a 20 20  , &leftbbox);.  
34d2b 7d 0a 20 20 69 66 28 20 28 72 63 20 3d 20 72 74  }.  if( (rc = rt
34d2c 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  reeInsertCell(pR
34d2d 74 72 65 65 2c 20 70 52 69 67 68 74 2d 3e 70 50  tree, pRight->pP
34d2e 61 72 65 6e 74 2c 20 26 72 69 67 68 74 62 62 6f  arent, &rightbbo
34d2f 78 2c 20 69 48 65 69 67 68 74 2b 31 29 29 20 29  x, iHeight+1)) )
34d30 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74  {.    goto split
34d31 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  node_out;.  }.. 
34d32 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45 4c   for(i=0; i<NCEL
34d33 4c 28 70 52 69 67 68 74 29 3b 20 69 2b 2b 29 7b  L(pRight); i++){
34d34 0a 20 20 20 20 69 36 34 20 69 52 6f 77 69 64 20  .    i64 iRowid 
34d35 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  = nodeGetRowid(p
34d36 52 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20 69  Rtree, pRight, i
34d37 29 3b 0a 20 20 20 20 72 63 20 3d 20 75 70 64 61  );.    rc = upda
34d38 74 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65  teMapping(pRtree
34d39 2c 20 69 52 6f 77 69 64 2c 20 70 52 69 67 68 74  , iRowid, pRight
34d3a 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20  , iHeight);.    
34d3b 69 66 28 20 69 52 6f 77 69 64 3d 3d 70 43 65 6c  if( iRowid==pCel
34d3c 6c 2d 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20  l->iRowid ){.   
34d3d 20 20 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68     newCellIsRigh
34d3e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  t = 1;.    }.   
34d3f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
34d40 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  OK ){.      goto
34d41 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
34d42 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
34d43 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20  pNode->iNode==1 
34d44 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
34d45 69 3c 4e 43 45 4c 4c 28 70 4c 65 66 74 29 3b 20  i<NCELL(pLeft); 
34d46 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 36 34 20  i++){.      i64 
34d47 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74  iRowid = nodeGet
34d48 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 4c  Rowid(pRtree, pL
34d49 65 66 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 72  eft, i);.      r
34d4a 63 20 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e  c = updateMappin
34d4b 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64  g(pRtree, iRowid
34d4c 2c 20 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74  , pLeft, iHeight
34d4d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
34d4e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
34d4f 20 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74        goto split
34d50 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20  node_out;.      
34d51 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  }.    }.  }else 
34d52 69 66 28 20 6e 65 77 43 65 6c 6c 49 73 52 69 67  if( newCellIsRig
34d53 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  ht==0 ){.    rc 
34d54 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28  = updateMapping(
34d55 70 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69  pRtree, pCell->i
34d56 52 6f 77 69 64 2c 20 70 4c 65 66 74 2c 20 69 48  Rowid, pLeft, iH
34d57 65 69 67 68 74 29 3b 0a 20 20 7d 0a 0a 20 20 69  eight);.  }..  i
34d58 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
34d59 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64   ){.    rc = nod
34d5a 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
34d5b 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20 70 52   pRight);.    pR
34d5c 69 67 68 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  ight = 0;.  }.  
34d5d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
34d5e 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f  K ){.    rc = no
34d5f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
34d60 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 70 4c  , pLeft);.    pL
34d61 65 66 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 73 70  eft = 0;.  }..sp
34d62 6c 69 74 6e 6f 64 65 5f 6f 75 74 3a 0a 20 20 6e  litnode_out:.  n
34d63 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
34d64 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 6e 6f  e, pRight);.  no
34d65 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
34d66 2c 20 70 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69  , pLeft);.  sqli
34d67 74 65 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b  te3_free(aCell);
34d68 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
34d69 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 78 4c  .static int fixL
34d6a 65 61 66 50 61 72 65 6e 74 28 52 74 72 65 65 20  eafParent(Rtree 
34d6b 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
34d6c 64 65 20 2a 70 4c 65 61 66 29 7b 0a 20 20 69 6e  de *pLeaf){.  in
34d6d 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
34d6e 3b 0a 20 20 69 66 28 20 70 4c 65 61 66 2d 3e 69  ;.  if( pLeaf->i
34d6f 4e 6f 64 65 21 3d 31 20 26 26 20 70 4c 65 61 66  Node!=1 && pLeaf
34d70 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 29 7b 0a  ->pParent==0 ){.
34d71 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
34d72 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
34d73 52 65 61 64 50 61 72 65 6e 74 2c 20 31 2c 20 70  ReadParent, 1, p
34d74 4c 65 61 66 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Leaf->iNode);.  
34d75 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74    if( sqlite3_st
34d76 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ep(pRtree->pRead
34d77 50 61 72 65 6e 74 29 3d 3d 53 51 4c 49 54 45 5f  Parent)==SQLITE_
34d78 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 69 36 34  ROW ){.      i64
34d79 20 69 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65 33   iNode = sqlite3
34d7a 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52  _column_int64(pR
34d7b 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
34d7c 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 20  t, 0);.      rc 
34d7d 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
34d7e 74 72 65 65 2c 20 69 4e 6f 64 65 2c 20 30 2c 20  tree, iNode, 0, 
34d7f 26 70 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 29  &pLeaf->pParent)
34d80 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
34d81 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
34d82 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20  RROR;.    }.    
34d83 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
34d84 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
34d85 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  t);.    if( rc==
34d86 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
34d87 20 20 20 72 63 20 3d 20 66 69 78 4c 65 61 66 50     rc = fixLeafP
34d88 61 72 65 6e 74 28 70 52 74 72 65 65 2c 20 70 4c  arent(pRtree, pL
34d89 65 61 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20  eaf->pParent);. 
34d8a 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
34d8b 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
34d8c 69 6e 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52  int deleteCell(R
34d8d 74 72 65 65 20 2a 2c 20 52 74 72 65 65 4e 6f 64  tree *, RtreeNod
34d8e 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  e *, int, int);.
34d8f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f  .static int remo
34d90 76 65 4e 6f 64 65 28 52 74 72 65 65 20 2a 70 52  veNode(Rtree *pR
34d91 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
34d92 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 48 65 69  *pNode, int iHei
34d93 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ght){.  int rc;.
34d94 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61    RtreeNode *pPa
34d95 72 65 6e 74 3b 0a 20 20 69 6e 74 20 69 43 65 6c  rent;.  int iCel
34d96 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e  l;..  assert( pN
34d97 6f 64 65 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a  ode->nRef==1 );.
34d98 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65  .  /* Remove the
34d99 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 61   entry in the pa
34d9a 72 65 6e 74 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20  rent cell. */.  
34d9b 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65  iCell = nodePare
34d9c 6e 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20  ntIndex(pRtree, 
34d9d 70 4e 6f 64 65 29 3b 0a 20 20 70 50 61 72 65 6e  pNode);.  pParen
34d9e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65  t = pNode->pPare
34d9f 6e 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70 50 61  nt;.  pNode->pPa
34da0 72 65 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20  rent = 0;.  if( 
34da1 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
34da2 20 64 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72   deleteCell(pRtr
34da3 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 69 43 65  ee, pParent, iCe
34da4 6c 6c 2c 20 69 48 65 69 67 68 74 2b 31 29 29 20  ll, iHeight+1)) 
34da5 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b  .   || SQLITE_OK
34da6 21 3d 28 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65  !=(rc = nodeRele
34da7 61 73 65 28 70 52 74 72 65 65 2c 20 70 50 61 72  ase(pRtree, pPar
34da8 65 6e 74 29 29 0a 20 20 29 7b 0a 20 20 20 20 72  ent)).  ){.    r
34da9 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
34daa 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 78   /* Remove the x
34dab 78 78 5f 6e 6f 64 65 20 65 6e 74 72 79 2e 20 2a  xx_node entry. *
34dac 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  /.  sqlite3_bind
34dad 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
34dae 44 65 6c 65 74 65 4e 6f 64 65 2c 20 31 2c 20 70  DeleteNode, 1, p
34daf 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
34db0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74  sqlite3_step(pRt
34db1 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65  ree->pDeleteNode
34db2 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
34db3 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK!=(rc = sqlite
34db4 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e  3_reset(pRtree->
34db5 70 44 65 6c 65 74 65 4e 6f 64 65 29 29 20 29 7b  pDeleteNode)) ){
34db6 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
34db7 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65    }..  /* Remove
34db8 20 74 68 65 20 78 78 78 5f 70 61 72 65 6e 74 20   the xxx_parent 
34db9 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c 69  entry. */.  sqli
34dba 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
34dbb 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
34dbc 72 65 6e 74 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e  rent, 1, pNode->
34dbd 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65  iNode);.  sqlite
34dbe 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70  3_step(pRtree->p
34dbf 44 65 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a 20  DeleteParent);. 
34dc0 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
34dc1 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  (rc = sqlite3_re
34dc2 73 65 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c  set(pRtree->pDel
34dc3 65 74 65 50 61 72 65 6e 74 29 29 20 29 7b 0a 20  eteParent)) ){. 
34dc4 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
34dc5 7d 0a 20 20 0a 20 20 2f 2a 20 52 65 6d 6f 76 65  }.  .  /* Remove
34dc6 20 74 68 65 20 6e 6f 64 65 20 66 72 6f 6d 20 74   the node from t
34dc7 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73  he in-memory has
34dc8 68 20 74 61 62 6c 65 20 61 6e 64 20 6c 69 6e 6b  h table and link
34dc9 20 69 74 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68   it into.  ** th
34dca 65 20 52 74 72 65 65 2e 70 44 65 6c 65 74 65 64  e Rtree.pDeleted
34dcb 20 6c 69 73 74 2e 20 49 74 73 20 63 6f 6e 74 65   list. Its conte
34dcc 6e 74 73 20 77 69 6c 6c 20 62 65 20 72 65 2d 69  nts will be re-i
34dcd 6e 73 65 72 74 65 64 20 6c 61 74 65 72 20 6f 6e  nserted later on
34dce 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 64 65 48 61 73  ..  */.  nodeHas
34dcf 68 44 65 6c 65 74 65 28 70 52 74 72 65 65 2c 20  hDelete(pRtree, 
34dd0 70 4e 6f 64 65 29 3b 0a 20 20 70 4e 6f 64 65 2d  pNode);.  pNode-
34dd1 3e 69 4e 6f 64 65 20 3d 20 69 48 65 69 67 68 74  >iNode = iHeight
34dd2 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74  ;.  pNode->pNext
34dd3 20 3d 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65   = pRtree->pDele
34dd4 74 65 64 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e 52  ted;.  pNode->nR
34dd5 65 66 2b 2b 3b 0a 20 20 70 52 74 72 65 65 2d 3e  ef++;.  pRtree->
34dd6 70 44 65 6c 65 74 65 64 20 3d 20 70 4e 6f 64 65  pDeleted = pNode
34dd7 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
34dd8 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63  TE_OK;.}..static
34dd9 20 76 6f 69 64 20 66 69 78 42 6f 75 6e 64 69 6e   void fixBoundin
34dda 67 42 6f 78 28 52 74 72 65 65 20 2a 70 52 74 72  gBox(Rtree *pRtr
34ddb 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
34ddc 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f  Node){.  RtreeNo
34ddd 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e  de *pParent = pN
34dde 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  ode->pParent;.  
34ddf 69 66 28 20 70 50 61 72 65 6e 74 20 29 7b 0a 20  if( pParent ){. 
34de0 20 20 20 69 6e 74 20 69 69 3b 20 0a 20 20 20 20     int ii; .    
34de1 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  int nCell = NCEL
34de2 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 52 74  L(pNode);.    Rt
34de3 72 65 65 43 65 6c 6c 20 62 6f 78 3b 20 20 20 20  reeCell box;    
34de4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34de5 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e 64          /* Bound
34de6 69 6e 67 20 62 6f 78 20 66 6f 72 20 70 4e 6f 64  ing box for pNod
34de7 65 20 2a 2f 0a 20 20 20 20 6e 6f 64 65 47 65 74  e */.    nodeGet
34de8 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f  Cell(pRtree, pNo
34de9 64 65 2c 20 30 2c 20 26 62 6f 78 29 3b 0a 20 20  de, 0, &box);.  
34dea 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 6e    for(ii=1; ii<n
34deb 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  Cell; ii++){.   
34dec 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c     RtreeCell cel
34ded 6c 3b 0a 20 20 20 20 20 20 6e 6f 64 65 47 65 74  l;.      nodeGet
34dee 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f  Cell(pRtree, pNo
34def 64 65 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a  de, ii, &cell);.
34df0 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28        cellUnion(
34df1 70 52 74 72 65 65 2c 20 26 62 6f 78 2c 20 26 63  pRtree, &box, &c
34df2 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ell);.    }.    
34df3 62 6f 78 2e 69 52 6f 77 69 64 20 3d 20 70 4e 6f  box.iRowid = pNo
34df4 64 65 2d 3e 69 4e 6f 64 65 3b 0a 20 20 20 20 69  de->iNode;.    i
34df5 69 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e  i = nodeParentIn
34df6 64 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  dex(pRtree, pNod
34df7 65 29 3b 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72  e);.    nodeOver
34df8 77 72 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65  writeCell(pRtree
34df9 2c 20 70 50 61 72 65 6e 74 2c 20 26 62 6f 78 2c  , pParent, &box,
34dfa 20 69 69 29 3b 0a 20 20 20 20 66 69 78 42 6f 75   ii);.    fixBou
34dfb 6e 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c  ndingBox(pRtree,
34dfc 20 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d   pParent);.  }.}
34dfd 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74  ../*.** Delete t
34dfe 68 65 20 63 65 6c 6c 20 61 74 20 69 6e 64 65 78  he cell at index
34dff 20 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20 70   iCell of node p
34e00 4e 6f 64 65 2e 20 41 66 74 65 72 20 72 65 6d 6f  Node. After remo
34e01 76 69 6e 67 20 74 68 65 0a 2a 2a 20 63 65 6c 6c  ving the.** cell
34e02 2c 20 61 64 6a 75 73 74 20 74 68 65 20 72 2d 74  , adjust the r-t
34e03 72 65 65 20 64 61 74 61 20 73 74 72 75 63 74 75  ree data structu
34e04 72 65 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a  re if required..
34e05 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65  */.static int de
34e06 6c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a  leteCell(Rtree *
34e07 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
34e08 65 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 43  e *pNode, int iC
34e09 65 6c 6c 2c 20 69 6e 74 20 69 48 65 69 67 68 74  ell, int iHeight
34e0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
34e0b 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
34e0c 72 63 20 3d 20 66 69 78 4c 65 61 66 50 61 72 65  rc = fixLeafPare
34e0d 6e 74 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  nt(pRtree, pNode
34e0e 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  )) ){.    return
34e0f 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52   rc;.  }..  /* R
34e10 65 6d 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 66  emove the cell f
34e11 72 6f 6d 20 74 68 65 20 6e 6f 64 65 2e 20 54 68  rom the node. Th
34e12 69 73 20 63 61 6c 6c 20 6a 75 73 74 20 6d 6f 76  is call just mov
34e13 65 73 20 62 79 74 65 73 20 61 72 6f 75 6e 64 0a  es bytes around.
34e14 20 20 2a 2a 20 74 68 65 20 69 6e 2d 6d 65 6d 6f    ** the in-memo
34e15 72 79 20 6e 6f 64 65 20 69 6d 61 67 65 2c 20 73  ry node image, s
34e16 6f 20 69 74 20 63 61 6e 6e 6f 74 20 66 61 69 6c  o it cannot fail
34e17 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 64 65 44 65 6c  ..  */.  nodeDel
34e18 65 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  eteCell(pRtree, 
34e19 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a 0a  pNode, iCell);..
34e1a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e 6f 64 65    /* If the node
34e1b 20 69 73 20 6e 6f 74 20 74 68 65 20 74 72 65 65   is not the tree
34e1c 20 72 6f 6f 74 20 61 6e 64 20 6e 6f 77 20 68 61   root and now ha
34e1d 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  s less than the 
34e1e 6d 69 6e 69 6d 75 6d 0a 20 20 2a 2a 20 6e 75 6d  minimum.  ** num
34e1f 62 65 72 20 6f 66 20 63 65 6c 6c 73 2c 20 72 65  ber of cells, re
34e20 6d 6f 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65  move it from the
34e21 20 74 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65   tree. Otherwise
34e22 2c 20 75 70 64 61 74 65 20 74 68 65 0a 20 20 2a  , update the.  *
34e23 2a 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61  * cell in the pa
34e24 72 65 6e 74 20 6e 6f 64 65 20 73 6f 20 74 68 61  rent node so tha
34e25 74 20 69 74 20 74 69 67 68 74 6c 79 20 63 6f 6e  t it tightly con
34e26 74 61 69 6e 73 20 74 68 65 20 75 70 64 61 74 65  tains the update
34e27 64 0a 20 20 2a 2a 20 6e 6f 64 65 2e 0a 20 20 2a  d.  ** node..  *
34e28 2f 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69  /.  if( pNode->i
34e29 4e 6f 64 65 21 3d 31 20 29 7b 0a 20 20 20 20 52  Node!=1 ){.    R
34e2a 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e  treeNode *pParen
34e2b 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65  t = pNode->pPare
34e2c 6e 74 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61  nt;.    if( (pPa
34e2d 72 65 6e 74 2d 3e 69 4e 6f 64 65 21 3d 31 20 7c  rent->iNode!=1 |
34e2e 7c 20 4e 43 45 4c 4c 28 70 50 61 72 65 6e 74 29  | NCELL(pParent)
34e2f 21 3d 31 29 20 0a 20 20 20 20 20 26 26 20 28 4e  !=1) .     && (N
34e30 43 45 4c 4c 28 70 4e 6f 64 65 29 3c 52 54 52 45  CELL(pNode)<RTRE
34e31 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65  E_MINCELLS(pRtre
34e32 65 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  e)).    ){.     
34e33 20 72 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65   rc = removeNode
34e34 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
34e35 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  iHeight);.    }e
34e36 6c 73 65 7b 0a 20 20 20 20 20 20 66 69 78 42 6f  lse{.      fixBo
34e37 75 6e 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65  undingBox(pRtree
34e38 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a  , pNode);.    }.
34e39 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
34e3a 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
34e3b 52 65 69 6e 73 65 72 74 28 0a 20 20 52 74 72 65  Reinsert(.  Rtre
34e3c 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74  e *pRtree, .  Rt
34e3d 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20  reeNode *pNode, 
34e3e 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
34e3f 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 48 65 69  ell, .  int iHei
34e40 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 61 4f  ght.){.  int *aO
34e41 72 64 65 72 3b 0a 20 20 69 6e 74 20 2a 61 53 70  rder;.  int *aSp
34e42 61 72 65 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c  are;.  RtreeCell
34e43 20 2a 61 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74   *aCell;.  float
34e44 20 2a 61 44 69 73 74 61 6e 63 65 3b 0a 20 20 69   *aDistance;.  i
34e45 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61  nt nCell;.  floa
34e46 74 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 52  t aCenterCoord[R
34e47 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49  TREE_MAX_DIMENSI
34e48 4f 4e 53 5d 3b 0a 20 20 69 6e 74 20 69 44 69 6d  ONS];.  int iDim
34e49 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e  ;.  int ii;.  in
34e4a 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
34e4b 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 61 43 65 6e  ;..  memset(aCen
34e4c 74 65 72 43 6f 6f 72 64 2c 20 30 2c 20 73 69 7a  terCoord, 0, siz
34e4d 65 6f 66 28 66 6c 6f 61 74 29 2a 52 54 52 45 45  eof(float)*RTREE
34e4e 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 29  _MAX_DIMENSIONS)
34e4f 3b 0a 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43 45  ;..  nCell = NCE
34e50 4c 4c 28 70 4e 6f 64 65 29 2b 31 3b 0a 0a 20 20  LL(pNode)+1;..  
34e51 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  /* Allocate the 
34e52 62 75 66 66 65 72 73 20 75 73 65 64 20 62 79 20  buffers used by 
34e53 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20  this operation. 
34e54 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  The allocation i
34e55 73 0a 20 20 2a 2a 20 72 65 6c 69 6e 71 75 69 73  s.  ** relinquis
34e56 68 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20  hed before this 
34e57 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
34e58 2e 0a 20 20 2a 2f 0a 20 20 61 43 65 6c 6c 20 3d  ..  */.  aCell =
34e59 20 28 52 74 72 65 65 43 65 6c 6c 20 2a 29 73 71   (RtreeCell *)sq
34e5a 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 65  lite3_malloc(nCe
34e5b 6c 6c 20 2a 20 28 0a 20 20 20 20 73 69 7a 65 6f  ll * (.    sizeo
34e5c 66 28 52 74 72 65 65 43 65 6c 6c 29 20 2b 20 20  f(RtreeCell) +  
34e5d 20 20 20 20 20 20 20 2f 2a 20 61 43 65 6c 6c 20         /* aCell 
34e5e 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a  array */.    siz
34e5f 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20 20 2b  eof(int)       +
34e60 20 20 20 20 20 20 20 20 20 2f 2a 20 61 4f 72 64           /* aOrd
34e61 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20  er array */.    
34e62 73 69 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20  sizeof(int)     
34e63 20 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 61    +         /* a
34e64 53 70 61 72 65 20 61 72 72 61 79 20 2a 2f 0a 20  Spare array */. 
34e65 20 20 20 73 69 7a 65 6f 66 28 66 6c 6f 61 74 29     sizeof(float)
34e66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34e67 2a 20 61 44 69 73 74 61 6e 63 65 20 61 72 72 61  * aDistance arra
34e68 79 20 2a 2f 0a 20 20 29 29 3b 0a 20 20 69 66 28  y */.  ));.  if(
34e69 20 21 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20 72   !aCell ){.    r
34e6a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
34e6b 45 4d 3b 0a 20 20 7d 0a 20 20 61 4f 72 64 65 72  EM;.  }.  aOrder
34e6c 20 20 20 20 3d 20 28 69 6e 74 20 2a 29 26 61 43      = (int *)&aC
34e6d 65 6c 6c 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 53  ell[nCell];.  aS
34e6e 70 61 72 65 20 20 20 20 3d 20 28 69 6e 74 20 2a  pare    = (int *
34e6f 29 26 61 4f 72 64 65 72 5b 6e 43 65 6c 6c 5d 3b  )&aOrder[nCell];
34e70 0a 20 20 61 44 69 73 74 61 6e 63 65 20 3d 20 28  .  aDistance = (
34e71 66 6c 6f 61 74 20 2a 29 26 61 53 70 61 72 65 5b  float *)&aSpare[
34e72 6e 43 65 6c 6c 5d 3b 0a 0a 20 20 66 6f 72 28 69  nCell];..  for(i
34e73 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
34e74 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 69  i++){.    if( ii
34e75 3d 3d 28 6e 43 65 6c 6c 2d 31 29 20 29 7b 0a 20  ==(nCell-1) ){. 
34e76 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 43 65       memcpy(&aCe
34e77 6c 6c 5b 69 69 5d 2c 20 70 43 65 6c 6c 2c 20 73  ll[ii], pCell, s
34e78 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
34e79 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
34e7a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
34e7b 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
34e7c 69 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a  i, &aCell[ii]);.
34e7d 20 20 20 20 7d 0a 20 20 20 20 61 4f 72 64 65 72      }.    aOrder
34e7e 5b 69 69 5d 20 3d 20 69 69 3b 0a 20 20 20 20 66  [ii] = ii;.    f
34e7f 6f 72 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c  or(iDim=0; iDim<
34e80 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44  pRtree->nDim; iD
34e81 69 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 61 43 65  im++){.      aCe
34e82 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20  nterCoord[iDim] 
34e83 2b 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b  += DCOORD(aCell[
34e84 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a  ii].aCoord[iDim*
34e85 32 5d 29 3b 0a 20 20 20 20 20 20 61 43 65 6e 74  2]);.      aCent
34e86 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d  erCoord[iDim] +=
34e87 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69   DCOORD(aCell[ii
34e88 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b  ].aCoord[iDim*2+
34e89 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  1]);.    }.  }. 
34e8a 20 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69 44 69   for(iDim=0; iDi
34e8b 6d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20  m<pRtree->nDim; 
34e8c 69 44 69 6d 2b 2b 29 7b 0a 20 20 20 20 61 43 65  iDim++){.    aCe
34e8d 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20  nterCoord[iDim] 
34e8e 3d 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69  = aCenterCoord[i
34e8f 44 69 6d 5d 2f 28 28 66 6c 6f 61 74 29 6e 43 65  Dim]/((float)nCe
34e90 6c 6c 2a 32 2e 30 29 3b 0a 20 20 7d 0a 0a 20 20  ll*2.0);.  }..  
34e91 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65  for(ii=0; ii<nCe
34e92 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61  ll; ii++){.    a
34e93 44 69 73 74 61 6e 63 65 5b 69 69 5d 20 3d 20 30  Distance[ii] = 0
34e94 2e 30 3b 0a 20 20 20 20 66 6f 72 28 69 44 69 6d  .0;.    for(iDim
34e95 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d  =0; iDim<pRtree-
34e96 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a  >nDim; iDim++){.
34e97 20 20 20 20 20 20 66 6c 6f 61 74 20 63 6f 6f 72        float coor
34e98 64 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c  d = DCOORD(aCell
34e99 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  [ii].aCoord[iDim
34e9a 2a 32 2b 31 5d 29 20 2d 20 0a 20 20 20 20 20 20  *2+1]) - .      
34e9b 20 20 20 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c      DCOORD(aCell
34e9c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  [ii].aCoord[iDim
34e9d 2a 32 5d 29 3b 0a 20 20 20 20 20 20 61 44 69 73  *2]);.      aDis
34e9e 74 61 6e 63 65 5b 69 69 5d 20 2b 3d 20 28 63 6f  tance[ii] += (co
34e9f 6f 72 64 2d 61 43 65 6e 74 65 72 43 6f 6f 72 64  ord-aCenterCoord
34ea0 5b 69 44 69 6d 5d 29 2a 28 63 6f 6f 72 64 2d 61  [iDim])*(coord-a
34ea1 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d  CenterCoord[iDim
34ea2 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ]);.    }.  }.. 
34ea3 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28   SortByDistance(
34ea4 61 4f 72 64 65 72 2c 20 6e 43 65 6c 6c 2c 20 61  aOrder, nCell, a
34ea5 44 69 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65  Distance, aSpare
34ea6 29 3b 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70 52  );.  nodeZero(pR
34ea7 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a 20  tree, pNode);.. 
34ea8 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53   for(ii=0; rc==S
34ea9 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 28  QLITE_OK && ii<(
34eaa 6e 43 65 6c 6c 2d 28 52 54 52 45 45 5f 4d 49 4e  nCell-(RTREE_MIN
34eab 43 45 4c 4c 53 28 70 52 74 72 65 65 29 2b 31 29  CELLS(pRtree)+1)
34eac 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74  ); ii++){.    Rt
34ead 72 65 65 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43  reeCell *p = &aC
34eae 65 6c 6c 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b  ell[aOrder[ii]];
34eaf 0a 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43  .    nodeInsertC
34eb0 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ell(pRtree, pNod
34eb1 65 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70  e, p);.    if( p
34eb2 2d 3e 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d  ->iRowid==pCell-
34eb3 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  >iRowid ){.     
34eb4 20 69 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20   if( iHeight==0 
34eb5 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
34eb6 72 6f 77 69 64 57 72 69 74 65 28 70 52 74 72 65  rowidWrite(pRtre
34eb7 65 2c 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70 4e  e, p->iRowid, pN
34eb8 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20  ode->iNode);.   
34eb9 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
34eba 20 20 72 63 20 3d 20 70 61 72 65 6e 74 57 72 69    rc = parentWri
34ebb 74 65 28 70 52 74 72 65 65 2c 20 70 2d 3e 69 52  te(pRtree, p->iR
34ebc 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f  owid, pNode->iNo
34ebd 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  de);.      }.   
34ebe 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d   }.  }.  if( rc=
34ebf 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
34ec0 20 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78    fixBoundingBox
34ec1 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
34ec2 0a 20 20 7d 0a 20 20 66 6f 72 28 3b 20 72 63 3d  .  }.  for(; rc=
34ec3 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69  =SQLITE_OK && ii
34ec4 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20  <nCell; ii++){. 
34ec5 20 20 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64     /* Find a nod
34ec6 65 20 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20  e to store this 
34ec7 63 65 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e  cell in. pNode->
34ec8 69 4e 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20  iNode currently 
34ec9 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20  contains.    ** 
34eca 74 68 65 20 68 65 69 67 68 74 20 6f 66 20 74 68  the height of th
34ecb 65 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 65  e sub-tree heade
34ecc 64 20 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20  d by the cell.. 
34ecd 20 20 20 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e     */.    RtreeN
34ece 6f 64 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20 20  ode *pInsert;.  
34ecf 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 20 3d    RtreeCell *p =
34ed0 20 26 61 43 65 6c 6c 5b 61 4f 72 64 65 72 5b 69   &aCell[aOrder[i
34ed1 69 5d 5d 3b 0a 20 20 20 20 72 63 20 3d 20 43 68  i]];.    rc = Ch
34ed2 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c  ooseLeaf(pRtree,
34ed3 20 70 2c 20 69 48 65 69 67 68 74 2c 20 26 70 49   p, iHeight, &pI
34ed4 6e 73 65 72 74 29 3b 0a 20 20 20 20 69 66 28 20  nsert);.    if( 
34ed5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
34ed6 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a  .      int rc2;.
34ed7 20 20 20 20 20 20 72 63 20 3d 20 72 74 72 65 65        rc = rtree
34ed8 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65  InsertCell(pRtre
34ed9 65 2c 20 70 49 6e 73 65 72 74 2c 20 70 2c 20 69  e, pInsert, p, i
34eda 48 65 69 67 68 74 29 3b 0a 20 20 20 20 20 20 72  Height);.      r
34edb 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65  c2 = nodeRelease
34edc 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74  (pRtree, pInsert
34edd 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
34ede 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
34edf 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a        rc = rc2;.
34ee0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
34ee1 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  }..  sqlite3_fre
34ee2 65 28 61 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75  e(aCell);.  retu
34ee3 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
34ee4 49 6e 73 65 72 74 20 63 65 6c 6c 20 70 43 65 6c  Insert cell pCel
34ee5 6c 20 69 6e 74 6f 20 6e 6f 64 65 20 70 4e 6f 64  l into node pNod
34ee6 65 2e 20 4e 6f 64 65 20 70 4e 6f 64 65 20 69 73  e. Node pNode is
34ee7 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 0a   the head of a .
34ee8 2a 2a 20 73 75 62 74 72 65 65 20 69 48 65 69 67  ** subtree iHeig
34ee9 68 74 20 68 69 67 68 20 28 6c 65 61 66 20 6e 6f  ht high (leaf no
34eea 64 65 73 20 68 61 76 65 20 69 48 65 69 67 68 74  des have iHeight
34eeb 3d 3d 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ==0)..*/.static 
34eec 69 6e 74 20 72 74 72 65 65 49 6e 73 65 72 74 43  int rtreeInsertC
34eed 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ell(.  Rtree *pR
34eee 74 72 65 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64  tree,.  RtreeNod
34eef 65 20 2a 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65  e *pNode,.  Rtre
34ef0 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20  eCell *pCell,.  
34ef1 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20  int iHeight.){. 
34ef2 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
34ef3 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 48 65 69 67  _OK;.  if( iHeig
34ef4 68 74 3e 30 20 29 7b 0a 20 20 20 20 52 74 72 65  ht>0 ){.    Rtre
34ef5 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64 20 3d 20  eNode *pChild = 
34ef6 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70  nodeHashLookup(p
34ef7 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52  Rtree, pCell->iR
34ef8 6f 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70  owid);.    if( p
34ef9 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e  Child ){.      n
34efa 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
34efb 65 2c 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65  e, pChild->pPare
34efc 6e 74 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52  nt);.      nodeR
34efd 65 66 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b  eference(pNode);
34efe 0a 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70  .      pChild->p
34eff 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a  Parent = pNode;.
34f00 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
34f01 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  nodeInsertCell(p
34f02 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43  Rtree, pNode, pC
34f03 65 6c 6c 29 20 29 7b 0a 23 69 66 20 56 41 52 49  ell) ){.#if VARI
34f04 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 52 45  ANT_RSTARTREE_RE
34f05 49 4e 53 45 52 54 0a 20 20 20 20 69 66 28 20 69  INSERT.    if( i
34f06 48 65 69 67 68 74 3c 3d 70 52 74 72 65 65 2d 3e  Height<=pRtree->
34f07 69 52 65 69 6e 73 65 72 74 48 65 69 67 68 74 20  iReinsertHeight 
34f08 7c 7c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d  || pNode->iNode=
34f09 3d 31 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  =1){.      rc = 
34f0a 53 70 6c 69 74 4e 6f 64 65 28 70 52 74 72 65 65  SplitNode(pRtree
34f0b 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20  , pNode, pCell, 
34f0c 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  iHeight);.    }e
34f0d 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 74 72 65  lse{.      pRtre
34f0e 65 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67  e->iReinsertHeig
34f0f 68 74 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20  ht = iHeight;.  
34f10 20 20 20 20 72 63 20 3d 20 52 65 69 6e 73 65 72      rc = Reinser
34f11 74 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  t(pRtree, pNode,
34f12 20 70 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29   pCell, iHeight)
34f13 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20  ;.    }.#else.  
34f14 20 20 72 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65    rc = SplitNode
34f15 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
34f16 70 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b  pCell, iHeight);
34f17 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b  .#endif.  }else{
34f18 0a 20 20 20 20 41 64 6a 75 73 74 54 72 65 65 28  .    AdjustTree(
34f19 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70  pRtree, pNode, p
34f1a 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69  Cell);.    if( i
34f1b 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  Height==0 ){.   
34f1c 20 20 20 72 63 20 3d 20 72 6f 77 69 64 57 72 69     rc = rowidWri
34f1d 74 65 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c  te(pRtree, pCell
34f1e 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d  ->iRowid, pNode-
34f1f 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c  >iNode);.    }el
34f20 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70  se{.      rc = p
34f21 61 72 65 6e 74 57 72 69 74 65 28 70 52 74 72 65  arentWrite(pRtre
34f22 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  e, pCell->iRowid
34f23 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  , pNode->iNode);
34f24 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
34f25 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69  urn rc;.}..stati
34f26 63 20 69 6e 74 20 72 65 69 6e 73 65 72 74 4e 6f  c int reinsertNo
34f27 64 65 43 6f 6e 74 65 6e 74 28 52 74 72 65 65 20  deContent(Rtree 
34f28 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
34f29 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e  de *pNode){.  in
34f2a 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t ii;.  int rc =
34f2b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
34f2c 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28  t nCell = NCELL(
34f2d 70 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69  pNode);..  for(i
34f2e 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
34f2f 4f 4b 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b 20  OK && ii<nCell; 
34f30 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65  ii++){.    Rtree
34f31 4e 6f 64 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20  Node *pInsert;. 
34f32 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c     RtreeCell cel
34f33 6c 3b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65  l;.    nodeGetCe
34f34 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
34f35 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a 0a 20  , ii, &cell);.. 
34f36 20 20 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64     /* Find a nod
34f37 65 20 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20  e to store this 
34f38 63 65 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e  cell in. pNode->
34f39 69 4e 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20  iNode currently 
34f3a 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20  contains.    ** 
34f3b 74 68 65 20 68 65 69 67 68 74 20 6f 66 20 74 68  the height of th
34f3c 65 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 65  e sub-tree heade
34f3d 64 20 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20  d by the cell.. 
34f3e 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 43     */.    rc = C
34f3f 68 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65  hooseLeaf(pRtree
34f40 2c 20 26 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e  , &cell, pNode->
34f41 69 4e 6f 64 65 2c 20 26 70 49 6e 73 65 72 74 29  iNode, &pInsert)
34f42 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
34f43 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
34f44 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20   int rc2;.      
34f45 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74  rc = rtreeInsert
34f46 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 49 6e  Cell(pRtree, pIn
34f47 73 65 72 74 2c 20 26 63 65 6c 6c 2c 20 70 4e 6f  sert, &cell, pNo
34f48 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
34f49 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65    rc2 = nodeRele
34f4a 61 73 65 28 70 52 74 72 65 65 2c 20 70 49 6e 73  ase(pRtree, pIns
34f4b 65 72 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ert);.      if( 
34f4c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
34f4d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63  .        rc = rc
34f4e 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  2;.      }.    }
34f4f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
34f50 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6c 65 63  ;.}../*.** Selec
34f51 74 20 61 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  t a currently un
34f52 75 73 65 64 20 72 6f 77 69 64 20 66 6f 72 20 61  used rowid for a
34f53 20 6e 65 77 20 72 2d 74 72 65 65 20 72 65 63 6f   new r-tree reco
34f54 72 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rd..*/.static in
34f55 74 20 6e 65 77 52 6f 77 69 64 28 52 74 72 65 65  t newRowid(Rtree
34f56 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20 2a 70   *pRtree, i64 *p
34f57 69 52 6f 77 69 64 29 7b 0a 20 20 69 6e 74 20 72  iRowid){.  int r
34f58 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  c;.  sqlite3_bin
34f59 64 5f 6e 75 6c 6c 28 70 52 74 72 65 65 2d 3e 70  d_null(pRtree->p
34f5a 57 72 69 74 65 52 6f 77 69 64 2c 20 31 29 3b 0a  WriteRowid, 1);.
34f5b 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e    sqlite3_bind_n
34f5c 75 6c 6c 28 70 52 74 72 65 65 2d 3e 70 57 72 69  ull(pRtree->pWri
34f5d 74 65 52 6f 77 69 64 2c 20 32 29 3b 0a 20 20 73  teRowid, 2);.  s
34f5e 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
34f5f 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29  ee->pWriteRowid)
34f60 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
34f61 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
34f62 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 2a  WriteRowid);.  *
34f63 70 69 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  piRowid = sqlite
34f64 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
34f65 77 69 64 28 70 52 74 72 65 65 2d 3e 64 62 29 3b  wid(pRtree->db);
34f66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
34f67 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
34f68 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 68 49  static int hashI
34f69 73 45 6d 70 74 79 28 52 74 72 65 65 20 2a 70 52  sEmpty(Rtree *pR
34f6a 74 72 65 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b  tree){.  int ii;
34f6b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
34f6c 48 41 53 48 53 49 5a 45 3b 20 69 69 2b 2b 29 7b  HASHSIZE; ii++){
34f6d 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 52  .    assert( !pR
34f6e 74 72 65 65 2d 3e 61 48 61 73 68 5b 69 69 5d 20  tree->aHash[ii] 
34f6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
34f70 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  1;.}.#endif../*.
34f71 2a 2a 20 54 68 65 20 78 55 70 64 61 74 65 20 6d  ** The xUpdate m
34f72 65 74 68 6f 64 20 66 6f 72 20 72 74 72 65 65 20  ethod for rtree 
34f73 6d 6f 64 75 6c 65 20 76 69 72 74 75 61 6c 20 74  module virtual t
34f74 61 62 6c 65 73 2e 0a 2a 2f 0a 69 6e 74 20 72 74  ables..*/.int rt
34f75 72 65 65 55 70 64 61 74 65 28 0a 20 20 73 71 6c  reeUpdate(.  sql
34f76 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
34f77 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20  , .  int nData, 
34f78 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
34f79 20 2a 2a 61 7a 44 61 74 61 2c 20 0a 20 20 73 71   **azData, .  sq
34f7a 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77  lite_int64 *pRow
34f7b 69 64 0a 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  id.){.  Rtree *p
34f7c 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
34f7d 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63  )pVtab;.  int rc
34f7e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
34f7f 20 72 74 72 65 65 52 65 66 65 72 65 6e 63 65 28   rtreeReference(
34f80 70 52 74 72 65 65 29 3b 0a 0a 20 20 61 73 73 65  pRtree);..  asse
34f81 72 74 28 6e 44 61 74 61 3e 3d 31 29 3b 0a 20 20  rt(nData>=1);.  
34f82 61 73 73 65 72 74 28 68 61 73 68 49 73 45 6d 70  assert(hashIsEmp
34f83 74 79 28 70 52 74 72 65 65 29 29 3b 0a 0a 20 20  ty(pRtree));..  
34f84 2f 2a 20 49 66 20 61 7a 44 61 74 61 5b 30 5d 20  /* If azData[0] 
34f85 69 73 20 6e 6f 74 20 61 6e 20 53 51 4c 20 4e 55  is not an SQL NU
34f86 4c 4c 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20  LL value, it is 
34f87 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61 0a 20  the rowid of a. 
34f88 20 2a 2a 20 72 65 63 6f 72 64 20 74 6f 20 64 65   ** record to de
34f89 6c 65 74 65 20 66 72 6f 6d 20 74 68 65 20 72 2d  lete from the r-
34f8a 74 72 65 65 20 74 61 62 6c 65 2e 20 54 68 65 20  tree table. The 
34f8b 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20  following block 
34f8c 64 6f 65 73 0a 20 20 2a 2a 20 6a 75 73 74 20 74  does.  ** just t
34f8d 68 61 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  hat..  */.  if( 
34f8e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
34f8f 70 65 28 61 7a 44 61 74 61 5b 30 5d 29 21 3d 53  pe(azData[0])!=S
34f90 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20  QLITE_NULL ){.  
34f91 20 20 69 36 34 20 69 44 65 6c 65 74 65 3b 20 20    i64 iDelete;  
34f92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34f93 20 54 68 65 20 72 6f 77 69 64 20 74 6f 20 64 65   The rowid to de
34f94 6c 65 74 65 20 2a 2f 0a 20 20 20 20 52 74 72 65  lete */.    Rtre
34f95 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b 20 20 20  eNode *pLeaf;   
34f96 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 66 20          /* Leaf 
34f97 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  node containing 
34f98 72 65 63 6f 72 64 20 69 44 65 6c 65 74 65 20 2a  record iDelete *
34f99 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 3b  /.    int iCell;
34f9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f9b 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 69 44    /* Index of iD
34f9c 65 6c 65 74 65 20 63 65 6c 6c 20 69 6e 20 70 4c  elete cell in pL
34f9d 65 61 66 20 2a 2f 0a 20 20 20 20 52 74 72 65 65  eaf */.    Rtree
34f9e 4e 6f 64 65 20 2a 70 52 6f 6f 74 3b 0a 0a 20 20  Node *pRoot;..  
34f9f 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 72 65    /* Obtain a re
34fa0 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 72  ference to the r
34fa1 6f 6f 74 20 6e 6f 64 65 20 74 6f 20 69 6e 69 74  oot node to init
34fa2 69 61 6c 69 73 65 20 52 74 72 65 65 2e 69 44 65  ialise Rtree.iDe
34fa3 70 74 68 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  pth */.    rc = 
34fa4 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
34fa5 65 65 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f 74  ee, 1, 0, &pRoot
34fa6 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 74 61 69  );..    /* Obtai
34fa7 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  n a reference to
34fa8 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 74   the leaf node t
34fa9 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
34faa 20 65 6e 74 72 79 20 0a 20 20 20 20 2a 2a 20 61   entry .    ** a
34fab 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c 65 74  bout to be delet
34fac 65 64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ed. .    */.    
34fad 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
34fae 4b 20 29 7b 0a 20 20 20 20 20 20 69 44 65 6c 65  K ){.      iDele
34faf 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  te = sqlite3_val
34fb0 75 65 5f 69 6e 74 36 34 28 61 7a 44 61 74 61 5b  ue_int64(azData[
34fb1 30 5d 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  0]);.      rc = 
34fb2 66 69 6e 64 4c 65 61 66 4e 6f 64 65 28 70 52 74  findLeafNode(pRt
34fb3 72 65 65 2c 20 69 44 65 6c 65 74 65 2c 20 26 70  ree, iDelete, &p
34fb4 4c 65 61 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Leaf);.    }..  
34fb5 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
34fb6 63 65 6c 6c 20 69 6e 20 71 75 65 73 74 69 6f 6e  cell in question
34fb7 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20 6e   from the leaf n
34fb8 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ode. */.    if( 
34fb9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
34fba 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a  .      int rc2;.
34fbb 20 20 20 20 20 20 69 43 65 6c 6c 20 3d 20 6e 6f        iCell = no
34fbc 64 65 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74  deRowidIndex(pRt
34fbd 72 65 65 2c 20 70 4c 65 61 66 2c 20 69 44 65 6c  ree, pLeaf, iDel
34fbe 65 74 65 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ete);.      rc =
34fbf 20 64 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72   deleteCell(pRtr
34fc0 65 65 2c 20 70 4c 65 61 66 2c 20 69 43 65 6c 6c  ee, pLeaf, iCell
34fc1 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32 20  , 0);.      rc2 
34fc2 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52  = nodeRelease(pR
34fc3 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20  tree, pLeaf);.  
34fc4 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
34fc5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
34fc6 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20   rc = rc2;.     
34fc7 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
34fc8 20 44 65 6c 65 74 65 20 74 68 65 20 63 6f 72 72   Delete the corr
34fc9 65 73 70 6f 6e 64 69 6e 67 20 65 6e 74 72 79 20  esponding entry 
34fca 69 6e 20 74 68 65 20 3c 72 74 72 65 65 3e 5f 72  in the <rtree>_r
34fcb 6f 77 69 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20  owid table. */. 
34fcc 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
34fcd 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
34fce 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
34fcf 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  (pRtree->pDelete
34fd0 52 6f 77 69 64 2c 20 31 2c 20 69 44 65 6c 65 74  Rowid, 1, iDelet
34fd1 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
34fd2 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70  3_step(pRtree->p
34fd3 44 65 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20  DeleteRowid);.  
34fd4 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
34fd5 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
34fd6 44 65 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20  DeleteRowid);.  
34fd7 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63    }..    /* Chec
34fd8 6b 20 69 66 20 74 68 65 20 72 6f 6f 74 20 6e 6f  k if the root no
34fd9 64 65 20 6e 6f 77 20 68 61 73 20 65 78 61 63 74  de now has exact
34fda 6c 79 20 6f 6e 65 20 63 68 69 6c 64 2e 20 49 66  ly one child. If
34fdb 20 73 6f 2c 20 72 65 6d 6f 76 65 0a 20 20 20 20   so, remove.    
34fdc 2a 2a 20 69 74 2c 20 73 63 68 65 64 75 6c 65 20  ** it, schedule 
34fdd 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
34fde 74 68 65 20 63 68 69 6c 64 20 66 6f 72 20 72 65  the child for re
34fdf 69 6e 73 65 72 74 69 6f 6e 20 61 6e 64 20 0a 20  insertion and . 
34fe0 20 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65     ** reduce the
34fe1 20 74 72 65 65 20 68 65 69 67 68 74 20 62 79 20   tree height by 
34fe2 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  one..    **.    
34fe3 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75 69 76  ** This is equiv
34fe4 61 6c 65 6e 74 20 74 6f 20 63 6f 70 79 69 6e 67  alent to copying
34fe5 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
34fe6 20 74 68 65 20 63 68 69 6c 64 20 69 6e 74 6f 0a   the child into.
34fe7 20 20 20 20 2a 2a 20 74 68 65 20 72 6f 6f 74 20      ** the root 
34fe8 6e 6f 64 65 20 28 74 68 65 20 6f 70 65 72 61 74  node (the operat
34fe9 69 6f 6e 20 74 68 61 74 20 47 75 74 6d 61 6e 27  ion that Gutman'
34fea 73 20 70 61 70 65 72 20 73 61 79 73 20 74 6f 20  s paper says to 
34feb 70 65 72 66 6f 72 6d 20 0a 20 20 20 20 2a 2a 20  perform .    ** 
34fec 69 6e 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f  in this scenario
34fed 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  )..    */.    if
34fee 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
34fef 26 26 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74  && pRtree->iDept
34ff0 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  h>0 ){.      if(
34ff1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
34ff2 26 20 4e 43 45 4c 4c 28 70 52 6f 6f 74 29 3d 3d  & NCELL(pRoot)==
34ff3 31 20 29 7b 0a 20 20 20 20 20 20 20 20 52 74 72  1 ){.        Rtr
34ff4 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64 3b 0a  eeNode *pChild;.
34ff5 20 20 20 20 20 20 20 20 69 36 34 20 69 43 68 69          i64 iChi
34ff6 6c 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69  ld = nodeGetRowi
34ff7 64 28 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 2c  d(pRtree, pRoot,
34ff8 20 30 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20   0);.        rc 
34ff9 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
34ffa 74 72 65 65 2c 20 69 43 68 69 6c 64 2c 20 70 52  tree, iChild, pR
34ffb 6f 6f 74 2c 20 26 70 43 68 69 6c 64 29 3b 0a 20  oot, &pChild);. 
34ffc 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
34ffd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
34ffe 20 20 20 20 20 20 72 63 20 3d 20 72 65 6d 6f 76        rc = remov
34fff 65 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 43  eNode(pRtree, pC
35000 68 69 6c 64 2c 20 70 52 74 72 65 65 2d 3e 69 44  hild, pRtree->iD
35001 65 70 74 68 2d 31 29 3b 0a 20 20 20 20 20 20 20  epth-1);.       
35002 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72   }.        if( r
35003 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
35004 20 20 20 20 20 20 20 20 20 20 70 52 74 72 65 65            pRtree
35005 2d 3e 69 44 65 70 74 68 2d 2d 3b 0a 20 20 20 20  ->iDepth--;.    
35006 20 20 20 20 20 20 77 72 69 74 65 49 6e 74 31 36        writeInt16
35007 28 70 52 6f 6f 74 2d 3e 7a 44 61 74 61 2c 20 70  (pRoot->zData, p
35008 52 74 72 65 65 2d 3e 69 44 65 70 74 68 29 3b 0a  Rtree->iDepth);.
35009 20 20 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d            pRoot-
3500a 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20  >isDirty = 1;.  
3500b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
3500c 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
3500d 2d 69 6e 73 65 72 74 20 74 68 65 20 63 6f 6e 74  -insert the cont
3500e 65 6e 74 73 20 6f 66 20 61 6e 79 20 75 6e 64 65  ents of any unde
3500f 72 66 75 6c 6c 20 6e 6f 64 65 73 20 72 65 6d 6f  rfull nodes remo
35010 76 65 64 20 66 72 6f 6d 20 74 68 65 20 74 72 65  ved from the tre
35011 65 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 4c  e. */.    for(pL
35012 65 61 66 3d 70 52 74 72 65 65 2d 3e 70 44 65 6c  eaf=pRtree->pDel
35013 65 74 65 64 3b 20 70 4c 65 61 66 3b 20 70 4c 65  eted; pLeaf; pLe
35014 61 66 3d 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  af=pRtree->pDele
35015 74 65 64 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ted){.      if( 
35016 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
35017 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65  .        rc = re
35018 69 6e 73 65 72 74 4e 6f 64 65 43 6f 6e 74 65 6e  insertNodeConten
35019 74 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 29  t(pRtree, pLeaf)
3501a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3501b 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64  pRtree->pDeleted
3501c 20 3d 20 70 4c 65 61 66 2d 3e 70 4e 65 78 74 3b   = pLeaf->pNext;
3501d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
3501e 72 65 65 28 70 4c 65 61 66 29 3b 0a 20 20 20 20  ree(pLeaf);.    
3501f 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73  }..    /* Releas
35020 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  e the reference 
35021 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65  to the root node
35022 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d  . */.    if( rc=
35023 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
35024 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c      rc = nodeRel
35025 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52 6f  ease(pRtree, pRo
35026 6f 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ot);.    }else{.
35027 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73        nodeReleas
35028 65 28 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29  e(pRtree, pRoot)
35029 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
3502a 2a 20 49 66 20 74 68 65 20 61 7a 44 61 74 61 5b  * If the azData[
3502b 5d 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73  ] array contains
3502c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 65   more than one e
3502d 6c 65 6d 65 6e 74 2c 20 65 6c 65 6d 65 6e 74 73  lement, elements
3502e 0a 20 20 2a 2a 20 28 61 7a 44 61 74 61 5b 32 5d  .  ** (azData[2]
3502f 2e 2e 61 7a 44 61 74 61 5b 61 72 67 63 2d 31 5d  ..azData[argc-1]
35030 29 20 63 6f 6e 74 61 69 6e 20 61 20 6e 65 77 20  ) contain a new 
35031 72 65 63 6f 72 64 20 74 6f 20 69 6e 73 65 72 74  record to insert
35032 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 72   into.  ** the r
35033 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e  -tree structure.
35034 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
35035 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 44 61  SQLITE_OK && nDa
35036 74 61 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49  ta>1 ){.    /* I
35037 6e 73 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f  nsert a new reco
35038 72 64 20 69 6e 74 6f 20 74 68 65 20 72 2d 74 72  rd into the r-tr
35039 65 65 20 2a 2f 0a 20 20 20 20 52 74 72 65 65 43  ee */.    RtreeC
3503a 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 69 6e  ell cell;.    in
3503b 74 20 69 69 3b 0a 20 20 20 20 52 74 72 65 65 4e  t ii;.    RtreeN
3503c 6f 64 65 20 2a 70 4c 65 61 66 3b 0a 0a 20 20 20  ode *pLeaf;..   
3503d 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65   /* Populate the
3503e 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 5d 20 61   cell.aCoord[] a
3503f 72 72 61 79 2e 20 54 68 65 20 66 69 72 73 74 20  rray. The first 
35040 63 6f 6f 72 64 69 6e 61 74 65 20 69 73 20 61 7a  coordinate is az
35041 44 61 74 61 5b 33 5d 2e 20 2a 2f 0a 20 20 20 20  Data[3]. */.    
35042 61 73 73 65 72 74 28 20 6e 44 61 74 61 3d 3d 28  assert( nData==(
35043 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 20 2b  pRtree->nDim*2 +
35044 20 33 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70   3) );.    if( p
35045 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70  Rtree->eCoordTyp
35046 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52  e==RTREE_COORD_R
35047 45 41 4c 33 32 20 29 7b 0a 20 20 20 20 20 20 66  EAL32 ){.      f
35048 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
35049 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
3504a 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 63 65  +=2){.        ce
3504b 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 20  ll.aCoord[ii].f 
3504c 3d 20 28 66 6c 6f 61 74 29 73 71 6c 69 74 65 33  = (float)sqlite3
3504d 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 7a  _value_double(az
3504e 44 61 74 61 5b 69 69 2b 33 5d 29 3b 0a 20 20 20  Data[ii+3]);.   
3504f 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64       cell.aCoord
35050 5b 69 69 2b 31 5d 2e 66 20 3d 20 28 66 6c 6f 61  [ii+1].f = (floa
35051 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t)sqlite3_value_
35052 64 6f 75 62 6c 65 28 61 7a 44 61 74 61 5b 69 69  double(azData[ii
35053 2b 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66  +4]);.        if
35054 28 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69  ( cell.aCoord[ii
35055 5d 2e 66 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b  ].f>cell.aCoord[
35056 69 69 2b 31 5d 2e 66 20 29 7b 0a 20 20 20 20 20  ii+1].f ){.     
35057 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
35058 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20  _CONSTRAINT;.   
35059 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73         goto cons
3505a 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20 20 20  traint;.        
3505b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  }.      }.    }e
3505c 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  lse{.      for(i
3505d 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d  i=0; ii<(pRtree-
3505e 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29  >nDim*2); ii+=2)
3505f 7b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61  {.        cell.a
35060 43 6f 6f 72 64 5b 69 69 5d 2e 69 20 3d 20 73 71  Coord[ii].i = sq
35061 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
35062 61 7a 44 61 74 61 5b 69 69 2b 33 5d 29 3b 0a 20  azData[ii+3]);. 
35063 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f         cell.aCoo
35064 72 64 5b 69 69 2b 31 5d 2e 69 20 3d 20 73 71 6c  rd[ii+1].i = sql
35065 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
35066 7a 44 61 74 61 5b 69 69 2b 34 5d 29 3b 0a 20 20  zData[ii+4]);.  
35067 20 20 20 20 20 20 69 66 28 20 63 65 6c 6c 2e 61        if( cell.a
35068 43 6f 6f 72 64 5b 69 69 5d 2e 69 3e 63 65 6c 6c  Coord[ii].i>cell
35069 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20  .aCoord[ii+1].i 
3506a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
3506b 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  = SQLITE_CONSTRA
3506c 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67  INT;.          g
3506d 6f 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a  oto constraint;.
3506e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3506f 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
35070 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 72  Figure out the r
35071 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20  owid of the new 
35072 72 6f 77 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  row. */.    if( 
35073 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
35074 70 65 28 61 7a 44 61 74 61 5b 32 5d 29 3d 3d 53  pe(azData[2])==S
35075 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20  QLITE_NULL ){.  
35076 20 20 20 20 72 63 20 3d 20 6e 65 77 52 6f 77 69      rc = newRowi
35077 64 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2e  d(pRtree, &cell.
35078 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c  iRowid);.    }el
35079 73 65 7b 0a 20 20 20 20 20 20 63 65 6c 6c 2e 69  se{.      cell.i
3507a 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  Rowid = sqlite3_
3507b 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 7a 44 61  value_int64(azDa
3507c 74 61 5b 32 5d 29 3b 0a 20 20 20 20 20 20 73 71  ta[2]);.      sq
3507d 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
3507e 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
3507f 77 69 64 2c 20 31 2c 20 63 65 6c 6c 2e 69 52 6f  wid, 1, cell.iRo
35080 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  wid);.      if( 
35081 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69  SQLITE_ROW==sqli
35082 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d  te3_step(pRtree-
35083 3e 70 52 65 61 64 52 6f 77 69 64 29 20 29 7b 0a  >pReadRowid) ){.
35084 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
35085 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52  reset(pRtree->pR
35086 65 61 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  eadRowid);.     
35087 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
35088 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20  ONSTRAINT;.     
35089 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61 69     goto constrai
3508a 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  nt;.      }.    
3508b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
3508c 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
3508d 61 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a  adRowid);.    }.
3508e 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3508f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
35090 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28  rc = ChooseLeaf(
35091 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 30  pRtree, &cell, 0
35092 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20 7d  , &pLeaf);.    }
35093 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
35094 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
35095 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 70  int rc2;.      p
35096 52 74 72 65 65 2d 3e 69 52 65 69 6e 73 65 72 74  Rtree->iReinsert
35097 48 65 69 67 68 74 20 3d 20 2d 31 3b 0a 20 20 20  Height = -1;.   
35098 20 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e 73     rc = rtreeIns
35099 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  ertCell(pRtree, 
3509a 70 4c 65 61 66 2c 20 26 63 65 6c 6c 2c 20 30 29  pLeaf, &cell, 0)
3509b 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f  ;.      rc2 = no
3509c 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
3509d 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20 20 20 20  , pLeaf);.      
3509e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3509f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
350a0 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20  = rc2;.      }. 
350a1 20 20 20 7d 0a 20 20 7d 0a 0a 63 6f 6e 73 74 72     }.  }..constr
350a2 61 69 6e 74 3a 0a 20 20 72 74 72 65 65 52 65 6c  aint:.  rtreeRel
350a3 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20 20  ease(pRtree);.  
350a4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
350a5 0a 2a 2a 20 54 68 65 20 78 52 65 6e 61 6d 65 20  .** The xRename 
350a6 6d 65 74 68 6f 64 20 66 6f 72 20 72 74 72 65 65  method for rtree
350a7 20 6d 6f 64 75 6c 65 20 76 69 72 74 75 61 6c 20   module virtual 
350a8 74 61 62 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  tables..*/.stati
350a9 63 20 69 6e 74 20 72 74 72 65 65 52 65 6e 61 6d  c int rtreeRenam
350aa 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  e(sqlite3_vtab *
350ab 70 56 74 61 62 2c 20 63 6f 6e 73 74 20 63 68 61  pVtab, const cha
350ac 72 20 2a 7a 4e 65 77 4e 61 6d 65 29 7b 0a 20 20  r *zNewName){.  
350ad 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20  Rtree *pRtree = 
350ae 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0a  (Rtree *)pVtab;.
350af 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
350b0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72 20  E_NOMEM;.  char 
350b1 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f  *zSql = sqlite3_
350b2 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 22 41 4c  mprintf(.    "AL
350b3 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71  TER TABLE %Q.'%q
350b4 5f 6e 6f 64 65 27 20 20 20 52 45 4e 41 4d 45 20  _node'   RENAME 
350b5 54 4f 20 5c 22 25 77 5f 6e 6f 64 65 5c 22 3b 22  TO \"%w_node\";"
350b6 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c  .    "ALTER TABL
350b7 45 20 25 51 2e 27 25 71 5f 70 61 72 65 6e 74 27  E %Q.'%q_parent'
350b8 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f   RENAME TO \"%w_
350b9 70 61 72 65 6e 74 5c 22 3b 22 0a 20 20 20 20 22  parent\";".    "
350ba 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27  ALTER TABLE %Q.'
350bb 25 71 5f 72 6f 77 69 64 27 20 20 52 45 4e 41 4d  %q_rowid'  RENAM
350bc 45 20 54 4f 20 5c 22 25 77 5f 72 6f 77 69 64 5c  E TO \"%w_rowid\
350bd 22 3b 22 0a 20 20 20 20 2c 20 70 52 74 72 65 65  ";".    , pRtree
350be 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a  ->zDb, pRtree->z
350bf 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 20 0a  Name, zNewName .
350c0 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44      , pRtree->zD
350c1 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65  b, pRtree->zName
350c2 2c 20 7a 4e 65 77 4e 61 6d 65 20 0a 20 20 20 20  , zNewName .    
350c3 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70  , pRtree->zDb, p
350c4 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e  Rtree->zName, zN
350c5 65 77 4e 61 6d 65 0a 20 20 29 3b 0a 20 20 69 66  ewName.  );.  if
350c6 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 63  ( zSql ){.    rc
350c7 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
350c8 70 52 74 72 65 65 2d 3e 64 62 2c 20 7a 53 71 6c  pRtree->db, zSql
350c9 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
350ca 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
350cb 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
350cc 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73   rc;.}..static s
350cd 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 72 74  qlite3_module rt
350ce 72 65 65 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20  reeModule = {.  
350cf 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
350d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56             /* iV
350d1 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 72 74 72 65  ersion */.  rtre
350d2 65 43 72 65 61 74 65 2c 20 20 20 20 20 20 20 20  eCreate,        
350d3 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61          /* xCrea
350d4 74 65 20 2d 20 63 72 65 61 74 65 20 61 20 74 61  te - create a ta
350d5 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65 43 6f  ble */.  rtreeCo
350d6 6e 6e 65 63 74 2c 20 20 20 20 20 20 20 20 20 20  nnect,          
350d7 20 20 20 20 20 2f 2a 20 78 43 6f 6e 6e 65 63 74       /* xConnect
350d8 20 2d 20 63 6f 6e 6e 65 63 74 20 74 6f 20 61 6e   - connect to an
350d9 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 20   existing table 
350da 2a 2f 0a 20 20 72 74 72 65 65 42 65 73 74 49 6e  */.  rtreeBestIn
350db 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  dex,            
350dc 20 2f 2a 20 78 42 65 73 74 49 6e 64 65 78 20 2d   /* xBestIndex -
350dd 20 44 65 74 65 72 6d 69 6e 65 20 73 65 61 72 63   Determine searc
350de 68 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20  h strategy */.  
350df 72 74 72 65 65 44 69 73 63 6f 6e 6e 65 63 74 2c  rtreeDisconnect,
350e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
350e1 44 69 73 63 6f 6e 6e 65 63 74 20 2d 20 44 69 73  Disconnect - Dis
350e2 63 6f 6e 6e 65 63 74 20 66 72 6f 6d 20 61 20 74  connect from a t
350e3 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65 44  able */.  rtreeD
350e4 65 73 74 72 6f 79 2c 20 20 20 20 20 20 20 20 20  estroy,         
350e5 20 20 20 20 20 20 2f 2a 20 78 44 65 73 74 72 6f        /* xDestro
350e6 79 20 2d 20 44 72 6f 70 20 61 20 74 61 62 6c 65  y - Drop a table
350e7 20 2a 2f 0a 20 20 72 74 72 65 65 4f 70 65 6e 2c   */.  rtreeOpen,
350e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
350e9 20 20 2f 2a 20 78 4f 70 65 6e 20 2d 20 6f 70 65    /* xOpen - ope
350ea 6e 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  n a cursor */.  
350eb 72 74 72 65 65 43 6c 6f 73 65 2c 20 20 20 20 20  rtreeClose,     
350ec 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
350ed 43 6c 6f 73 65 20 2d 20 63 6c 6f 73 65 20 61 20  Close - close a 
350ee 63 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65  cursor */.  rtre
350ef 65 46 69 6c 74 65 72 2c 20 20 20 20 20 20 20 20  eFilter,        
350f0 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 74          /* xFilt
350f1 65 72 20 2d 20 63 6f 6e 66 69 67 75 72 65 20 73  er - configure s
350f2 63 61 6e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  can constraints 
350f3 2a 2f 0a 20 20 72 74 72 65 65 4e 65 78 74 2c 20  */.  rtreeNext, 
350f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
350f5 20 2f 2a 20 78 4e 65 78 74 20 2d 20 61 64 76 61   /* xNext - adva
350f6 6e 63 65 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a  nce a cursor */.
350f7 20 20 72 74 72 65 65 45 6f 66 2c 20 20 20 20 20    rtreeEof,     
350f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
350f9 20 78 45 6f 66 20 2a 2f 0a 20 20 72 74 72 65 65   xEof */.  rtree
350fa 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20  Column,         
350fb 20 20 20 20 20 20 20 2f 2a 20 78 43 6f 6c 75 6d         /* xColum
350fc 6e 20 2d 20 72 65 61 64 20 64 61 74 61 20 2a 2f  n - read data */
350fd 0a 20 20 72 74 72 65 65 52 6f 77 69 64 2c 20 20  .  rtreeRowid,  
350fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
350ff 2a 20 78 52 6f 77 69 64 20 2d 20 72 65 61 64 20  * xRowid - read 
35100 64 61 74 61 20 2a 2f 0a 20 20 72 74 72 65 65 55  data */.  rtreeU
35101 70 64 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  pdate,          
35102 20 20 20 20 20 20 2f 2a 20 78 55 70 64 61 74 65        /* xUpdate
35103 20 2d 20 77 72 69 74 65 20 64 61 74 61 20 2a 2f   - write data */
35104 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
35105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35106 2a 20 78 42 65 67 69 6e 20 2d 20 62 65 67 69 6e  * xBegin - begin
35107 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a   transaction */.
35108 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
35109 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3510a 20 78 53 79 6e 63 20 2d 20 73 79 6e 63 20 74 72   xSync - sync tr
3510b 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30  ansaction */.  0
3510c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3510d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
3510e 6f 6d 6d 69 74 20 2d 20 63 6f 6d 6d 69 74 20 74  ommit - commit t
3510f 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
35110 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
35111 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
35112 52 6f 6c 6c 62 61 63 6b 20 2d 20 72 6f 6c 6c 62  Rollback - rollb
35113 61 63 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ack transaction 
35114 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
35115 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35116 20 2f 2a 20 78 46 69 6e 64 46 75 6e 63 74 69 6f   /* xFindFunctio
35117 6e 20 2d 20 66 75 6e 63 74 69 6f 6e 20 6f 76 65  n - function ove
35118 72 6c 6f 61 64 69 6e 67 20 2a 2f 0a 20 20 72 74  rloading */.  rt
35119 72 65 65 52 65 6e 61 6d 65 20 20 20 20 20 20 20  reeRename       
3511a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65            /* xRe
3511b 6e 61 6d 65 20 2d 20 72 65 6e 61 6d 65 20 74 68  name - rename th
3511c 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73  e table */.};..s
3511d 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 53  tatic int rtreeS
3511e 71 6c 49 6e 69 74 28 0a 20 20 52 74 72 65 65 20  qlInit(.  Rtree 
3511f 2a 70 52 74 72 65 65 2c 20 0a 20 20 73 71 6c 69  *pRtree, .  sqli
35120 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 6f 6e 73  te3 *db, .  cons
35121 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 0a 20 20  t char *zDb, .  
35122 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 65  const char *zPre
35123 66 69 78 2c 20 0a 20 20 69 6e 74 20 69 73 43 72  fix, .  int isCr
35124 65 61 74 65 0a 29 7b 0a 20 20 69 6e 74 20 72 63  eate.){.  int rc
35125 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
35126 20 23 64 65 66 69 6e 65 20 4e 5f 53 54 41 54 45   #define N_STATE
35127 4d 45 4e 54 20 39 0a 20 20 73 74 61 74 69 63 20  MENT 9.  static 
35128 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 53 71  const char *azSq
35129 6c 5b 4e 5f 53 54 41 54 45 4d 45 4e 54 5d 20 3d  l[N_STATEMENT] =
3512a 20 7b 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 61   {.    /* Read a
3512b 6e 64 20 77 72 69 74 65 20 74 68 65 20 78 78 78  nd write the xxx
3512c 5f 6e 6f 64 65 20 74 61 62 6c 65 20 2a 2f 0a 20  _node table */. 
3512d 20 20 20 22 53 45 4c 45 43 54 20 64 61 74 61 20     "SELECT data 
3512e 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 6e 6f  FROM '%q'.'%q_no
3512f 64 65 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f  de' WHERE nodeno
35130 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53   = :1",.    "INS
35131 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49  ERT OR REPLACE I
35132 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64  NTO '%q'.'%q_nod
35133 65 27 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a 32  e' VALUES(:1, :2
35134 29 22 2c 0a 20 20 20 20 22 44 45 4c 45 54 45 20  )",.    "DELETE 
35135 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 6e 6f  FROM '%q'.'%q_no
35136 64 65 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f  de' WHERE nodeno
35137 20 3d 20 3a 31 22 2c 0a 0a 20 20 20 20 2f 2a 20   = :1",..    /* 
35138 52 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74  Read and write t
35139 68 65 20 78 78 78 5f 72 6f 77 69 64 20 74 61 62  he xxx_rowid tab
3513a 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43  le */.    "SELEC
3513b 54 20 6e 6f 64 65 6e 6f 20 46 52 4f 4d 20 27 25  T nodeno FROM '%
3513c 71 27 2e 27 25 71 5f 72 6f 77 69 64 27 20 57 48  q'.'%q_rowid' WH
3513d 45 52 45 20 72 6f 77 69 64 20 3d 20 3a 31 22 2c  ERE rowid = :1",
3513e 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20  .    "INSERT OR 
3513f 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27 25 71  REPLACE INTO '%q
35140 27 2e 27 25 71 5f 72 6f 77 69 64 27 20 56 41 4c  '.'%q_rowid' VAL
35141 55 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20  UES(:1, :2)",.  
35142 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 27    "DELETE FROM '
35143 25 71 27 2e 27 25 71 5f 72 6f 77 69 64 27 20 57  %q'.'%q_rowid' W
35144 48 45 52 45 20 72 6f 77 69 64 20 3d 20 3a 31 22  HERE rowid = :1"
35145 2c 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 61  ,..    /* Read a
35146 6e 64 20 77 72 69 74 65 20 74 68 65 20 78 78 78  nd write the xxx
35147 5f 70 61 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f  _parent table */
35148 0a 20 20 20 20 22 53 45 4c 45 43 54 20 70 61 72  .    "SELECT par
35149 65 6e 74 6e 6f 64 65 20 46 52 4f 4d 20 27 25 71  entnode FROM '%q
3514a 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 20 57 48  '.'%q_parent' WH
3514b 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22  ERE nodeno = :1"
3514c 2c 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52  ,.    "INSERT OR
3514d 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27 25   REPLACE INTO '%
3514e 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 20 56  q'.'%q_parent' V
3514f 41 4c 55 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a  ALUES(:1, :2)",.
35150 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
35151 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65 6e 74   '%q'.'%q_parent
35152 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d  ' WHERE nodeno =
35153 20 3a 31 22 0a 20 20 7d 3b 0a 20 20 73 71 6c 69   :1".  };.  sqli
35154 74 65 33 5f 73 74 6d 74 20 2a 2a 61 70 70 53 74  te3_stmt **appSt
35155 6d 74 5b 4e 5f 53 54 41 54 45 4d 45 4e 54 5d 3b  mt[N_STATEMENT];
35156 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 70 52 74  .  int i;..  pRt
35157 72 65 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a 20  ree->db = db;.. 
35158 20 69 66 28 20 69 73 43 72 65 61 74 65 20 29 7b   if( isCreate ){
35159 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 72 65 61  .    char *zCrea
3515a 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  te = sqlite3_mpr
3515b 69 6e 74 66 28 0a 22 43 52 45 41 54 45 20 54 41  intf(."CREATE TA
3515c 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f  BLE \"%w\".\"%w_
3515d 6e 6f 64 65 5c 22 28 6e 6f 64 65 6e 6f 20 49 4e  node\"(nodeno IN
3515e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
3515f 59 2c 20 64 61 74 61 20 42 4c 4f 42 29 3b 22 0a  Y, data BLOB);".
35160 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c 22  "CREATE TABLE \"
35161 25 77 5c 22 2e 5c 22 25 77 5f 72 6f 77 69 64 5c  %w\".\"%w_rowid\
35162 22 28 72 6f 77 69 64 20 49 4e 54 45 47 45 52 20  "(rowid INTEGER 
35163 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 6e 6f 64  PRIMARY KEY, nod
35164 65 6e 6f 20 49 4e 54 45 47 45 52 29 3b 22 0a 22  eno INTEGER);"."
35165 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c 22 25  CREATE TABLE \"%
35166 77 5c 22 2e 5c 22 25 77 5f 70 61 72 65 6e 74 5c  w\".\"%w_parent\
35167 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52  "(nodeno INTEGER
35168 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 70 61   PRIMARY KEY, pa
35169 72 65 6e 74 6e 6f 64 65 20 49 4e 54 45 47 45 52  rentnode INTEGER
3516a 29 3b 22 0a 22 49 4e 53 45 52 54 20 49 4e 54 4f  );"."INSERT INTO
3516b 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20   '%q'.'%q_node' 
3516c 56 41 4c 55 45 53 28 31 2c 20 7a 65 72 6f 62 6c  VALUES(1, zerobl
3516d 6f 62 28 25 64 29 29 22 2c 0a 20 20 20 20 20 20  ob(%d))",.      
3516e 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 7a 44  zDb, zPrefix, zD
3516f 62 2c 20 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c  b, zPrefix, zDb,
35170 20 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a   zPrefix, zDb, z
35171 50 72 65 66 69 78 2c 20 70 52 74 72 65 65 2d 3e  Prefix, pRtree->
35172 69 4e 6f 64 65 53 69 7a 65 0a 20 20 20 20 29 3b  iNodeSize.    );
35173 0a 20 20 20 20 69 66 28 20 21 7a 43 72 65 61 74  .    if( !zCreat
35174 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  e ){.      retur
35175 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
35176 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
35177 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
35178 7a 43 72 65 61 74 65 2c 20 30 2c 20 30 2c 20 30  zCreate, 0, 0, 0
35179 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
3517a 72 65 65 28 7a 43 72 65 61 74 65 29 3b 0a 20 20  ree(zCreate);.  
3517b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3517c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
3517d 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
3517e 7d 0a 0a 20 20 61 70 70 53 74 6d 74 5b 30 5d 20  }..  appStmt[0] 
3517f 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65 61 64  = &pRtree->pRead
35180 4e 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b  Node;.  appStmt[
35181 31 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 57  1] = &pRtree->pW
35182 72 69 74 65 4e 6f 64 65 3b 0a 20 20 61 70 70 53  riteNode;.  appS
35183 74 6d 74 5b 32 5d 20 3d 20 26 70 52 74 72 65 65  tmt[2] = &pRtree
35184 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 3b 0a 20  ->pDeleteNode;. 
35185 20 61 70 70 53 74 6d 74 5b 33 5d 20 3d 20 26 70   appStmt[3] = &p
35186 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69  Rtree->pReadRowi
35187 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 34 5d 20  d;.  appStmt[4] 
35188 3d 20 26 70 52 74 72 65 65 2d 3e 70 57 72 69 74  = &pRtree->pWrit
35189 65 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74 6d  eRowid;.  appStm
3518a 74 5b 35 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[5] = &pRtree->
3518b 70 44 65 6c 65 74 65 52 6f 77 69 64 3b 0a 20 20  pDeleteRowid;.  
3518c 61 70 70 53 74 6d 74 5b 36 5d 20 3d 20 26 70 52  appStmt[6] = &pR
3518d 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
3518e 74 3b 0a 20 20 61 70 70 53 74 6d 74 5b 37 5d 20  t;.  appStmt[7] 
3518f 3d 20 26 70 52 74 72 65 65 2d 3e 70 57 72 69 74  = &pRtree->pWrit
35190 65 50 61 72 65 6e 74 3b 0a 20 20 61 70 70 53 74  eParent;.  appSt
35191 6d 74 5b 38 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[8] = &pRtree-
35192 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74 3b 0a  >pDeleteParent;.
35193 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f  .  for(i=0; i<N_
35194 53 54 41 54 45 4d 45 4e 54 20 26 26 20 72 63 3d  STATEMENT && rc=
35195 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 2b 2b 29  =SQLITE_OK; i++)
35196 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c  {.    char *zSql
35197 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
35198 74 66 28 61 7a 53 71 6c 5b 69 5d 2c 20 7a 44 62  tf(azSql[i], zDb
35199 2c 20 7a 50 72 65 66 69 78 29 3b 0a 20 20 20 20  , zPrefix);.    
3519a 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20  if( zSql ){.    
3519b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70    rc = sqlite3_p
3519c 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53  repare_v2(db, zS
3519d 71 6c 2c 20 2d 31 2c 20 61 70 70 53 74 6d 74 5b  ql, -1, appStmt[
3519e 69 5d 2c 20 30 29 3b 20 0a 20 20 20 20 7d 65 6c  i], 0); .    }el
3519f 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  se{.      rc = S
351a0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
351a1 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
351a2 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a  ree(zSql);.  }..
351a3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
351a4 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
351a5 6e 65 20 71 75 65 72 69 65 73 20 64 61 74 61 62  ne queries datab
351a6 61 73 65 20 68 61 6e 64 6c 65 20 64 62 20 66 6f  ase handle db fo
351a7 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20  r the page-size 
351a8 75 73 65 64 20 62 79 0a 2a 2a 20 64 61 74 61 62  used by.** datab
351a9 61 73 65 20 7a 44 62 2e 20 49 66 20 73 75 63 63  ase zDb. If succ
351aa 65 73 73 66 75 6c 2c 20 74 68 65 20 70 61 67 65  essful, the page
351ab 2d 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 69  -size in bytes i
351ac 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20  s written to.** 
351ad 2a 70 69 50 61 67 65 53 69 7a 65 20 61 6e 64 20  *piPageSize and 
351ae 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
351af 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61  ed. Otherwise, a
351b0 6e 64 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  nd an SQLite err
351b1 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 69 73 20 72  or .** code is r
351b2 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
351b3 69 63 20 69 6e 74 20 67 65 74 50 61 67 65 53 69  ic int getPageSi
351b4 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ze(sqlite3 *db, 
351b5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c  const char *zDb,
351b6 20 69 6e 74 20 2a 70 69 50 61 67 65 53 69 7a 65   int *piPageSize
351b7 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
351b8 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 63 68  LITE_NOMEM;.  ch
351b9 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 73 71 6c 69  ar *zSql;.  sqli
351ba 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20  te3_stmt *pStmt 
351bb 3d 20 30 3b 0a 0a 20 20 7a 53 71 6c 20 3d 20 73  = 0;..  zSql = s
351bc 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
351bd 50 52 41 47 4d 41 20 25 51 2e 70 61 67 65 5f 73  PRAGMA %Q.page_s
351be 69 7a 65 22 2c 20 7a 44 62 29 3b 0a 20 20 69 66  ize", zDb);.  if
351bf 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72  ( !zSql ){.    r
351c0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
351c1 45 4d 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  EM;.  }..  rc = 
351c2 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
351c3 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  v2(db, zSql, -1,
351c4 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 73   &pStmt, 0);.  s
351c5 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c  qlite3_free(zSql
351c6 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
351c7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
351c8 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
351c9 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  if( SQLITE_ROW==
351ca 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
351cb 6d 74 29 20 29 7b 0a 20 20 20 20 2a 70 69 50 61  mt) ){.    *piPa
351cc 67 65 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33  geSize = sqlite3
351cd 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53 74 6d  _column_int(pStm
351ce 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  t, 0);.  }.  ret
351cf 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
351d0 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
351d1 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /* .** This func
351d2 74 69 6f 6e 20 69 73 20 74 68 65 20 69 6d 70 6c  tion is the impl
351d3 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 62 6f  ementation of bo
351d4 74 68 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 20  th the xConnect 
351d5 61 6e 64 20 78 43 72 65 61 74 65 0a 2a 2a 20 6d  and xCreate.** m
351d6 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 72 2d  ethods of the r-
351d7 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
351d8 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 76  le..**.**   argv
351d9 5b 30 5d 20 20 20 2d 3e 20 6d 6f 64 75 6c 65 20  [0]   -> module 
351da 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67 76 5b 31  name.**   argv[1
351db 5d 20 20 20 2d 3e 20 64 61 74 61 62 61 73 65 20  ]   -> database 
351dc 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67 76 5b 32  name.**   argv[2
351dd 5d 20 20 20 2d 3e 20 74 61 62 6c 65 20 6e 61 6d  ]   -> table nam
351de 65 0a 2a 2a 20 20 20 61 72 67 76 5b 2e 2e 2e 5d  e.**   argv[...]
351df 20 2d 3e 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73   -> column names
351e0 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ....*/.static in
351e1 74 20 72 74 72 65 65 49 6e 69 74 28 0a 20 20 73  t rtreeInit(.  s
351e2 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
351e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
351e4 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
351e5 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76  onnection */.  v
351e6 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20  oid *pAux,      
351e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
351e8 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65     /* One of the
351e9 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 2a 20 63   RTREE_COORD_* c
351ea 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 69 6e  onstants */.  in
351eb 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68  t argc, const ch
351ec 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 20  ar *const*argv, 
351ed 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 73 20    /* Parameters 
351ee 74 6f 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  to CREATE TABLE 
351ef 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 73  statement */.  s
351f0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
351f1 56 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  Vtab,           
351f2 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 77 20 76     /* OUT: New v
351f3 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
351f4 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 2c 20    char **pzErr, 
351f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
351f6 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 72        /* OUT: Er
351f7 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 69 66 20  ror message, if 
351f8 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43  any */.  int isC
351f9 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20  reate           
351fa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
351fb 54 72 75 65 20 66 6f 72 20 78 43 72 65 61 74 65  True for xCreate
351fc 2c 20 66 61 6c 73 65 20 66 6f 72 20 78 43 6f 6e  , false for xCon
351fd 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  nect */.){.  int
351fe 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
351ff 0a 20 20 69 6e 74 20 69 50 61 67 65 53 69 7a 65  .  int iPageSize
35200 20 3d 20 30 3b 0a 20 20 52 74 72 65 65 20 2a 70   = 0;.  Rtree *p
35201 52 74 72 65 65 3b 0a 20 20 69 6e 74 20 6e 44 62  Rtree;.  int nDb
35202 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
35203 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69  * Length of stri
35204 6e 67 20 61 72 67 76 5b 31 5d 20 2a 2f 0a 20 20  ng argv[1] */.  
35205 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20  int nName;      
35206 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
35207 6f 66 20 73 74 72 69 6e 67 20 61 72 67 76 5b 32  of string argv[2
35208 5d 20 2a 2f 0a 20 20 69 6e 74 20 65 43 6f 6f 72  ] */.  int eCoor
35209 64 54 79 70 65 20 3d 20 28 69 6e 74 29 70 41 75  dType = (int)pAu
3520a 78 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  x;..  const char
3520b 20 2a 61 45 72 72 4d 73 67 5b 5d 20 3d 20 7b 0a   *aErrMsg[] = {.
3520c 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
3520d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3520e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3520f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 2a            /* 0 *
35210 2f 0a 20 20 20 20 22 57 72 6f 6e 67 20 6e 75 6d  /.    "Wrong num
35211 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66  ber of columns f
35212 6f 72 20 61 6e 20 72 74 72 65 65 20 74 61 62 6c  or an rtree tabl
35213 65 22 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 31  e",         /* 1
35214 20 2a 2f 0a 20 20 20 20 22 54 6f 6f 20 66 65 77   */.    "Too few
35215 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20   columns for an 
35216 72 74 72 65 65 20 74 61 62 6c 65 22 2c 20 20 20  rtree table",   
35217 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35218 20 32 20 2a 2f 0a 20 20 20 20 22 54 6f 6f 20 6d   2 */.    "Too m
35219 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20  any columns for 
3521a 61 6e 20 72 74 72 65 65 20 74 61 62 6c 65 22 20  an rtree table" 
3521b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3521c 2f 2a 20 33 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20  /* 3 */.  };..  
3521d 69 6e 74 20 69 45 72 72 20 3d 20 28 61 72 67 63  int iErr = (argc
3521e 3c 36 29 20 3f 20 32 20 3a 20 61 72 67 63 3e 28  <6) ? 2 : argc>(
3521f 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53  RTREE_MAX_DIMENS
35220 49 4f 4e 53 2a 32 2b 34 29 20 3f 20 33 20 3a 20  IONS*2+4) ? 3 : 
35221 61 72 67 63 25 32 3b 0a 20 20 69 66 28 20 61 45  argc%2;.  if( aE
35222 72 72 4d 73 67 5b 69 45 72 72 5d 20 29 7b 0a 20  rrMsg[iErr] ){. 
35223 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69     *pzErr = sqli
35224 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22  te3_mprintf("%s"
35225 2c 20 61 45 72 72 4d 73 67 5b 69 45 72 72 5d 29  , aErrMsg[iErr])
35226 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
35227 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
35228 20 20 72 63 20 3d 20 67 65 74 50 61 67 65 53 69    rc = getPageSi
35229 7a 65 28 64 62 2c 20 61 72 67 76 5b 31 5d 2c 20  ze(db, argv[1], 
3522a 26 69 50 61 67 65 53 69 7a 65 29 3b 0a 20 20 69  &iPageSize);.  i
3522b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3522c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
3522d 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c  c;.  }..  /* All
3522e 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  ocate the sqlite
3522f 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
35230 20 2a 2f 0a 20 20 6e 44 62 20 3d 20 73 74 72 6c   */.  nDb = strl
35231 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 6e  en(argv[1]);.  n
35232 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 61 72  Name = strlen(ar
35233 67 76 5b 32 5d 29 3b 0a 20 20 70 52 74 72 65 65  gv[2]);.  pRtree
35234 20 3d 20 28 52 74 72 65 65 20 2a 29 73 71 6c 69   = (Rtree *)sqli
35235 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
35236 66 28 52 74 72 65 65 29 2b 6e 44 62 2b 6e 4e 61  f(Rtree)+nDb+nNa
35237 6d 65 2b 32 29 3b 0a 20 20 69 66 28 20 21 70 52  me+2);.  if( !pR
35238 74 72 65 65 20 29 7b 0a 20 20 20 20 72 65 74 75  tree ){.    retu
35239 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
3523a 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 52  .  }.  memset(pR
3523b 74 72 65 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  tree, 0, sizeof(
3523c 52 74 72 65 65 29 2b 6e 44 62 2b 6e 4e 61 6d 65  Rtree)+nDb+nName
3523d 2b 32 29 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e  +2);.  pRtree->n
3523e 42 75 73 79 20 3d 20 31 3b 0a 20 20 70 52 74 72  Busy = 1;.  pRtr
3523f 65 65 2d 3e 62 61 73 65 2e 70 4d 6f 64 75 6c 65  ee->base.pModule
35240 20 3d 20 26 72 74 72 65 65 4d 6f 64 75 6c 65 3b   = &rtreeModule;
35241 0a 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 20 3d  .  pRtree->zDb =
35242 20 28 63 68 61 72 20 2a 29 26 70 52 74 72 65 65   (char *)&pRtree
35243 5b 31 5d 3b 0a 20 20 70 52 74 72 65 65 2d 3e 7a  [1];.  pRtree->z
35244 4e 61 6d 65 20 3d 20 26 70 52 74 72 65 65 2d 3e  Name = &pRtree->
35245 7a 44 62 5b 6e 44 62 2b 31 5d 3b 0a 20 20 70 52  zDb[nDb+1];.  pR
35246 74 72 65 65 2d 3e 6e 44 69 6d 20 3d 20 28 61 72  tree->nDim = (ar
35247 67 63 2d 34 29 2f 32 3b 0a 20 20 70 52 74 72 65  gc-4)/2;.  pRtre
35248 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
35249 20 3d 20 38 20 2b 20 70 52 74 72 65 65 2d 3e 6e   = 8 + pRtree->n
3524a 44 69 6d 2a 34 2a 32 3b 0a 20 20 70 52 74 72 65  Dim*4*2;.  pRtre
3524b 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 20 3d 20  e->eCoordType = 
3524c 65 43 6f 6f 72 64 54 79 70 65 3b 0a 20 20 6d 65  eCoordType;.  me
3524d 6d 63 70 79 28 70 52 74 72 65 65 2d 3e 7a 44 62  mcpy(pRtree->zDb
3524e 2c 20 61 72 67 76 5b 31 5d 2c 20 6e 44 62 29 3b  , argv[1], nDb);
3524f 0a 20 20 6d 65 6d 63 70 79 28 70 52 74 72 65 65  .  memcpy(pRtree
35250 2d 3e 7a 4e 61 6d 65 2c 20 61 72 67 76 5b 32 5d  ->zName, argv[2]
35251 2c 20 6e 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20  , nName);..  /* 
35252 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e  Figure out the n
35253 6f 64 65 20 73 69 7a 65 20 74 6f 20 75 73 65 2e  ode size to use.
35254 20 42 79 20 64 65 66 61 75 6c 74 2c 20 75 73 65   By default, use
35255 20 36 34 20 62 79 74 65 73 20 6c 65 73 73 20 74   64 bytes less t
35256 68 61 6e 0a 20 20 2a 2a 20 74 68 65 20 64 61 74  han.  ** the dat
35257 61 62 61 73 65 20 70 61 67 65 2d 73 69 7a 65 2e  abase page-size.
35258 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68   This ensures th
35259 61 74 20 65 61 63 68 20 6e 6f 64 65 20 69 73 20  at each node is 
3525a 73 74 6f 72 65 64 20 6f 6e 0a 20 20 2a 2a 20 61  stored on.  ** a
3525b 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
3525c 20 70 61 67 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   page..  **.  **
3525d 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 64   If the databasd
3525e 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 73 6f   page-size is so
3525f 20 6c 61 72 67 65 20 74 68 61 74 20 6d 6f 72 65   large that more
35260 20 74 68 61 6e 20 52 54 52 45 45 5f 4d 41 58 43   than RTREE_MAXC
35261 45 4c 4c 53 0a 20 20 2a 2a 20 65 6e 74 72 69 65  ELLS.  ** entrie
35262 73 20 77 6f 75 6c 64 20 66 69 74 20 69 6e 20 61  s would fit in a
35263 20 73 69 6e 67 6c 65 20 6e 6f 64 65 2c 20 75 73   single node, us
35264 65 20 61 20 73 6d 61 6c 6c 65 72 20 6e 6f 64 65  e a smaller node
35265 2d 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 70 52  -size..  */.  pR
35266 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20  tree->iNodeSize 
35267 3d 20 69 50 61 67 65 53 69 7a 65 2d 36 34 3b 0a  = iPageSize-64;.
35268 20 20 69 66 28 20 28 34 2b 70 52 74 72 65 65 2d    if( (4+pRtree-
35269 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52  >nBytesPerCell*R
3526a 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 29 3c 70  TREE_MAXCELLS)<p
3526b 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65  Rtree->iNodeSize
3526c 20 29 7b 0a 20 20 20 20 70 52 74 72 65 65 2d 3e   ){.    pRtree->
3526d 69 4e 6f 64 65 53 69 7a 65 20 3d 20 34 2b 70 52  iNodeSize = 4+pR
3526e 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43  tree->nBytesPerC
3526f 65 6c 6c 2a 52 54 52 45 45 5f 4d 41 58 43 45 4c  ell*RTREE_MAXCEL
35270 4c 53 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72  LS;.  }..  /* Cr
35271 65 61 74 65 2f 43 6f 6e 6e 65 63 74 20 74 6f 20  eate/Connect to 
35272 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72  the underlying r
35273 65 6c 61 74 69 6f 6e 61 6c 20 64 61 74 61 62 61  elational databa
35274 73 65 20 73 63 68 65 6d 61 2e 20 49 66 0a 20 20  se schema. If.  
35275 2a 2a 20 74 68 61 74 20 69 73 20 73 75 63 63 65  ** that is succe
35276 73 73 66 75 6c 2c 20 63 61 6c 6c 20 73 71 6c 69  ssful, call sqli
35277 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
35278 28 29 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 0a  () to configure.
35279 20 20 2a 2a 20 74 68 65 20 72 2d 74 72 65 65 20    ** the r-tree 
3527a 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 0a 20 20  table schema..  
3527b 2a 2f 0a 20 20 69 66 28 20 28 72 63 20 3d 20 72  */.  if( (rc = r
3527c 74 72 65 65 53 71 6c 49 6e 69 74 28 70 52 74 72  treeSqlInit(pRtr
3527d 65 65 2c 20 64 62 2c 20 61 72 67 76 5b 31 5d 2c  ee, db, argv[1],
3527e 20 61 72 67 76 5b 32 5d 2c 20 69 73 43 72 65 61   argv[2], isCrea
3527f 74 65 29 29 20 29 7b 0a 20 20 20 20 2a 70 7a 45  te)) ){.    *pzE
35280 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  rr = sqlite3_mpr
35281 69 6e 74 66 28 22 25 73 22 2c 20 73 71 6c 69 74  intf("%s", sqlit
35282 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a  e3_errmsg(db));.
35283 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61    }else{.    cha
35284 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  r *zSql = sqlite
35285 33 5f 6d 70 72 69 6e 74 66 28 22 43 52 45 41 54  3_mprintf("CREAT
35286 45 20 54 41 42 4c 45 20 78 28 25 73 22 2c 20 61  E TABLE x(%s", a
35287 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 63 68 61  rgv[3]);.    cha
35288 72 20 2a 7a 54 6d 70 3b 0a 20 20 20 20 69 6e 74  r *zTmp;.    int
35289 20 69 69 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d   ii;.    for(ii=
3528a 34 3b 20 7a 53 71 6c 20 26 26 20 69 69 3c 61 72  4; zSql && ii<ar
3528b 67 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  gc; ii++){.     
3528c 20 7a 54 6d 70 20 3d 20 7a 53 71 6c 3b 0a 20 20   zTmp = zSql;.  
3528d 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74      zSql = sqlit
3528e 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 2c 20  e3_mprintf("%s, 
3528f 25 73 22 2c 20 7a 54 6d 70 2c 20 61 72 67 76 5b  %s", zTmp, argv[
35290 69 69 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ii]);.      sqli
35291 74 65 33 5f 66 72 65 65 28 7a 54 6d 70 29 3b 0a  te3_free(zTmp);.
35292 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 53      }.    if( zS
35293 71 6c 20 29 7b 0a 20 20 20 20 20 20 7a 54 6d 70  ql ){.      zTmp
35294 20 3d 20 7a 53 71 6c 3b 0a 20 20 20 20 20 20 7a   = zSql;.      z
35295 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
35296 72 69 6e 74 66 28 22 25 73 29 3b 22 2c 20 7a 54  rintf("%s);", zT
35297 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  mp);.      sqlit
35298 65 33 5f 66 72 65 65 28 7a 54 6d 70 29 3b 0a 20  e3_free(zTmp);. 
35299 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 7a 53     }.    if( !zS
3529a 71 6c 20 7c 7c 20 73 71 6c 69 74 65 33 5f 64 65  ql || sqlite3_de
3529b 63 6c 61 72 65 5f 76 74 61 62 28 64 62 2c 20 7a  clare_vtab(db, z
3529c 53 71 6c 29 20 29 7b 0a 20 20 20 20 20 20 72 63  Sql) ){.      rc
3529d 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
3529e 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
3529f 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20  e3_free(zSql);. 
352a0 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   }..  if( rc==SQ
352a1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a  LITE_OK ){.    *
352a2 70 70 56 74 61 62 20 3d 20 28 73 71 6c 69 74 65  ppVtab = (sqlite
352a3 33 5f 76 74 61 62 20 2a 29 70 52 74 72 65 65 3b  3_vtab *)pRtree;
352a4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 74  .  }else{.    rt
352a5 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  reeRelease(pRtre
352a6 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
352a7 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49   rc;.}.../*.** I
352a8 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
352a9 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69   a scalar functi
352aa 6f 6e 20 74 68 61 74 20 64 65 63 6f 64 65 73 20  on that decodes 
352ab 72 2d 74 72 65 65 20 6e 6f 64 65 73 20 74 6f 0a  r-tree nodes to.
352ac 2a 2a 20 68 75 6d 61 6e 20 72 65 61 64 61 62 6c  ** human readabl
352ad 65 20 73 74 72 69 6e 67 73 2e 20 54 68 69 73 20  e strings. This 
352ae 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20  can be used for 
352af 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 61 6e  debugging and an
352b0 61 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  alysis..**.** Th
352b1 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  e scalar functio
352b2 6e 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75  n takes two argu
352b3 6d 65 6e 74 73 2c 20 61 20 62 6c 6f 62 20 6f 66  ments, a blob of
352b4 20 64 61 74 61 20 63 6f 6e 74 61 69 6e 69 6e 67   data containing
352b5 0a 2a 2a 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f  .** an r-tree no
352b6 64 65 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62  de, and the numb
352b7 65 72 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 73  er of dimensions
352b8 20 74 68 65 20 72 2d 74 72 65 65 20 69 6e 64 65   the r-tree inde
352b9 78 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 74 77  xes..** For a tw
352ba 6f 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 72 2d  o-dimensional r-
352bb 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 63  tree structure c
352bc 61 6c 6c 65 64 20 22 72 74 22 2c 20 74 6f 20 64  alled "rt", to d
352bd 65 73 65 72 69 61 6c 69 7a 65 0a 2a 2a 20 61 6c  eserialize.** al
352be 6c 20 6e 6f 64 65 73 2c 20 61 20 73 74 61 74 65  l nodes, a state
352bf 6d 65 6e 74 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a  ment like:.**.**
352c0 20 20 20 53 45 4c 45 43 54 20 72 74 72 65 65 6e     SELECT rtreen
352c1 6f 64 65 28 32 2c 20 64 61 74 61 29 20 46 52 4f  ode(2, data) FRO
352c2 4d 20 72 74 5f 6e 6f 64 65 3b 0a 2a 2a 0a 2a 2a  M rt_node;.**.**
352c3 20 54 68 65 20 68 75 6d 61 6e 20 72 65 61 64 61   The human reada
352c4 62 6c 65 20 73 74 72 69 6e 67 20 74 61 6b 65 73  ble string takes
352c5 20 74 68 65 20 66 6f 72 6d 20 6f 66 20 61 20 54   the form of a T
352c6 63 6c 20 6c 69 73 74 20 77 69 74 68 20 6f 6e 65  cl list with one
352c7 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 65 61  .** entry for ea
352c8 63 68 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72  ch cell in the r
352c9 2d 74 72 65 65 20 6e 6f 64 65 2e 20 45 61 63 68  -tree node. Each
352ca 20 65 6e 74 72 79 20 69 73 20 69 74 73 65 6c 66   entry is itself
352cb 20 61 0a 2a 2a 20 6c 69 73 74 2c 20 63 6f 6e 74   a.** list, cont
352cc 61 69 6e 69 6e 67 20 74 68 65 20 38 2d 62 79 74  aining the 8-byt
352cd 65 20 72 6f 77 69 64 2f 70 61 67 65 6e 6f 20 66  e rowid/pageno f
352ce 6f 6c 6c 6f 77 65 64 20 62 79 20 74 68 65 20 0a  ollowed by the .
352cf 2a 2a 20 3c 6e 75 6d 2d 64 69 6d 65 6e 73 69 6f  ** <num-dimensio
352d0 6e 3e 2a 32 20 63 6f 6f 72 64 69 6e 61 74 65 73  n>*2 coordinates
352d1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
352d2 20 72 74 72 65 65 6e 6f 64 65 28 73 71 6c 69 74   rtreenode(sqlit
352d3 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c  e3_context *ctx,
352d4 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74   int nArg, sqlit
352d5 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
352d6 29 7b 0a 20 20 63 68 61 72 20 2a 7a 54 65 78 74  ){.  char *zText
352d7 20 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64   = 0;.  RtreeNod
352d8 65 20 6e 6f 64 65 3b 0a 20 20 52 74 72 65 65 20  e node;.  Rtree 
352d9 74 72 65 65 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  tree;.  int ii;.
352da 0a 20 20 6d 65 6d 73 65 74 28 26 6e 6f 64 65 2c  .  memset(&node,
352db 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
352dc 4e 6f 64 65 29 29 3b 0a 20 20 6d 65 6d 73 65 74  Node));.  memset
352dd 28 26 74 72 65 65 2c 20 30 2c 20 73 69 7a 65 6f  (&tree, 0, sizeo
352de 66 28 52 74 72 65 65 29 29 3b 0a 20 20 74 72 65  f(Rtree));.  tre
352df 65 2e 6e 44 69 6d 20 3d 20 73 71 6c 69 74 65 33  e.nDim = sqlite3
352e0 5f 76 61 6c 75 65 5f 69 6e 74 28 61 70 41 72 67  _value_int(apArg
352e1 5b 30 5d 29 3b 0a 20 20 74 72 65 65 2e 6e 42 79  [0]);.  tree.nBy
352e2 74 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20 2b  tesPerCell = 8 +
352e3 20 38 20 2a 20 74 72 65 65 2e 6e 44 69 6d 3b 0a   8 * tree.nDim;.
352e4 20 20 6e 6f 64 65 2e 7a 44 61 74 61 20 3d 20 28    node.zData = (
352e5 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  u8 *)sqlite3_val
352e6 75 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 31 5d  ue_blob(apArg[1]
352e7 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
352e8 69 69 3c 4e 43 45 4c 4c 28 26 6e 6f 64 65 29 3b  ii<NCELL(&node);
352e9 20 69 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72   ii++){.    char
352ea 20 7a 43 65 6c 6c 5b 35 31 32 5d 3b 0a 20 20 20   zCell[512];.   
352eb 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 0a   int nCell = 0;.
352ec 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65      RtreeCell ce
352ed 6c 6c 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a  ll;.    int jj;.
352ee 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  .    nodeGetCell
352ef 28 26 74 72 65 65 2c 20 26 6e 6f 64 65 2c 20 69  (&tree, &node, i
352f0 69 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 73  i, &cell);.    s
352f1 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
352f2 35 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c  512-nCell,&zCell
352f3 5b 6e 43 65 6c 6c 5d 2c 22 25 64 22 2c 20 63 65  [nCell],"%d", ce
352f4 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20  ll.iRowid);.    
352f5 6e 43 65 6c 6c 20 3d 20 73 74 72 6c 65 6e 28 7a  nCell = strlen(z
352f6 43 65 6c 6c 29 3b 0a 20 20 20 20 66 6f 72 28 6a  Cell);.    for(j
352f7 6a 3d 30 3b 20 6a 6a 3c 74 72 65 65 2e 6e 44 69  j=0; jj<tree.nDi
352f8 6d 2a 32 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  m*2; jj++){.    
352f9 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
352fa 74 66 28 35 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43  tf(512-nCell,&zC
352fb 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 22 20 25 66 22  ell[nCell]," %f"
352fc 2c 28 64 6f 75 62 6c 65 29 63 65 6c 6c 2e 61 43  ,(double)cell.aC
352fd 6f 6f 72 64 5b 6a 6a 5d 2e 66 29 3b 0a 20 20 20  oord[jj].f);.   
352fe 20 20 20 6e 43 65 6c 6c 20 3d 20 73 74 72 6c 65     nCell = strle
352ff 6e 28 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a  n(zCell);.    }.
35300 0a 20 20 20 20 69 66 28 20 7a 54 65 78 74 20 29  .    if( zText )
35301 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54  {.      char *zT
35302 65 78 74 4e 65 77 20 3d 20 73 71 6c 69 74 65 33  extNew = sqlite3
35303 5f 6d 70 72 69 6e 74 66 28 22 25 73 20 7b 25 73  _mprintf("%s {%s
35304 7d 22 2c 20 7a 54 65 78 74 2c 20 7a 43 65 6c 6c  }", zText, zCell
35305 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
35306 5f 66 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20  _free(zText);.  
35307 20 20 20 20 7a 54 65 78 74 20 3d 20 7a 54 65 78      zText = zTex
35308 74 4e 65 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  tNew;.    }else{
35309 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20 73  .      zText = s
3530a 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
3530b 7b 25 73 7d 22 2c 20 7a 43 65 6c 6c 29 3b 0a 20  {%s}", zCell);. 
3530c 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 73 71     }.  }.  .  sq
3530d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
3530e 74 28 63 74 78 2c 20 7a 54 65 78 74 2c 20 2d 31  t(ctx, zText, -1
3530f 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b  , sqlite3_free);
35310 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
35311 72 74 72 65 65 64 65 70 74 68 28 73 71 6c 69 74  rtreedepth(sqlit
35312 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c  e3_context *ctx,
35313 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74   int nArg, sqlit
35314 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
35315 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
35316 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 70 41 72  _value_type(apAr
35317 67 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c  g[0])!=SQLITE_BL
35318 4f 42 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65  OB .   || sqlite
35319 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 70  3_value_bytes(ap
3531a 41 72 67 5b 30 5d 29 3c 32 0a 20 20 29 7b 0a 20  Arg[0])<2.  ){. 
3531b 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
3531c 74 5f 65 72 72 6f 72 28 63 74 78 2c 20 22 49 6e  t_error(ctx, "In
3531d 76 61 6c 69 64 20 61 72 67 75 6d 65 6e 74 20 74  valid argument t
3531e 6f 20 72 74 72 65 65 64 65 70 74 68 28 29 22 2c  o rtreedepth()",
3531f 20 2d 31 29 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a   -1); .  }else{.
35320 20 20 20 20 75 38 20 2a 7a 42 6c 6f 62 20 3d 20      u8 *zBlob = 
35321 28 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  (u8 *)sqlite3_va
35322 6c 75 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 30  lue_blob(apArg[0
35323 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ]);.    sqlite3_
35324 72 65 73 75 6c 74 5f 69 6e 74 28 63 74 78 2c 20  result_int(ctx, 
35325 72 65 61 64 49 6e 74 31 36 28 7a 42 6c 6f 62 29  readInt16(zBlob)
35326 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
35327 52 65 67 69 73 74 65 72 20 74 68 65 20 72 2d 74  Register the r-t
35328 72 65 65 20 6d 6f 64 75 6c 65 20 77 69 74 68 20  ree module with 
35329 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
3532a 64 62 2e 20 54 68 69 73 20 63 72 65 61 74 65 73  db. This creates
3532b 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20   the.** virtual 
3532c 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 22 72 74  table module "rt
3532d 72 65 65 22 20 61 6e 64 20 74 68 65 20 64 65 62  ree" and the deb
3532e 75 67 67 69 6e 67 2f 61 6e 61 6c 79 73 69 73 20  ugging/analysis 
3532f 73 63 61 6c 61 72 20 0a 2a 2a 20 66 75 6e 63 74  scalar .** funct
35330 69 6f 6e 20 22 72 74 72 65 65 6e 6f 64 65 22 2e  ion "rtreenode".
35331 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
35332 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 74  TE int sqlite3Rt
35333 72 65 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20  reeInit(sqlite3 
35334 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  *db){.  int rc =
35335 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69   SQLITE_OK;..  i
35336 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
35337 20 29 7b 0a 20 20 20 20 69 6e 74 20 75 74 66 38   ){.    int utf8
35338 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
35339 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3533a 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
3533b 28 64 62 2c 20 22 72 74 72 65 65 6e 6f 64 65 22  (db, "rtreenode"
3533c 2c 20 32 2c 20 75 74 66 38 2c 20 30 2c 20 72 74  , 2, utf8, 0, rt
3533d 72 65 65 6e 6f 64 65 2c 20 30 2c 20 30 29 3b 0a  reenode, 0, 0);.
3533e 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
3533f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
35340 6e 74 20 75 74 66 38 20 3d 20 53 51 4c 49 54 45  nt utf8 = SQLITE
35341 5f 55 54 46 38 3b 0a 20 20 20 20 72 63 20 3d 20  _UTF8;.    rc = 
35342 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
35343 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72 74 72  unction(db, "rtr
35344 65 65 64 65 70 74 68 22 2c 20 31 2c 20 75 74 66  eedepth", 1, utf
35345 38 2c 20 30 2c 72 74 72 65 65 64 65 70 74 68 2c  8, 0,rtreedepth,
35346 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66   0, 0);.  }.  if
35347 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
35348 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 63 20 3d  ){.    void *c =
35349 20 28 76 6f 69 64 20 2a 29 52 54 52 45 45 5f 43   (void *)RTREE_C
3534a 4f 4f 52 44 5f 52 45 41 4c 33 32 3b 0a 20 20 20  OORD_REAL32;.   
3534b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72   rc = sqlite3_cr
3534c 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64  eate_module_v2(d
3534d 62 2c 20 22 72 74 72 65 65 22 2c 20 26 72 74 72  b, "rtree", &rtr
3534e 65 65 4d 6f 64 75 6c 65 2c 20 63 2c 20 30 29 3b  eeModule, c, 0);
3534f 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
35350 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
35351 76 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20  void *c = (void 
35352 2a 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e  *)RTREE_COORD_IN
35353 54 33 32 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  T32;.    rc = sq
35354 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
35355 75 6c 65 5f 76 32 28 64 62 2c 20 22 72 74 72 65  ule_v2(db, "rtre
35356 65 5f 69 33 32 22 2c 20 26 72 74 72 65 65 4d 6f  e_i32", &rtreeMo
35357 64 75 6c 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d  dule, c, 0);.  }
35358 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
35359 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f  ..#if !SQLITE_CO
3535a 52 45 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  RE.SQLITE_API in
3535b 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73  t sqlite3_extens
3535c 69 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69  ion_init(.  sqli
3535d 74 65 33 20 2a 64 62 2c 0a 20 20 63 68 61 72 20  te3 *db,.  char 
3535e 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f  **pzErrMsg,.  co
3535f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
35360 72 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29  routines *pApi.)
35361 7b 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  {.  SQLITE_EXTEN
35362 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29  SION_INIT2(pApi)
35363 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
35364 33 52 74 72 65 65 49 6e 69 74 28 64 62 29 3b 0a  3RtreeInit(db);.
35365 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  }.#endif..#endif
35366 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
35367 2a 20 45 6e 64 20 6f 66 20 72 74 72 65 65 2e 63  * End of rtree.c
35368 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
35369 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3536a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3536b 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
3536c 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 69 63 75  * Begin file icu
3536d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
3536e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3536f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35370 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79  /./*.** 2007 May
35371 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   6.**.** The aut
35372 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
35373 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
35374 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
35375 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
35376 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
35377 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
35378 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
35379 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
3537a 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
3537b 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
3537c 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
3537d 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
3537e 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
3537f 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
35380 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
35381 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
35382 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
35383 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35384 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35385 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35386 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35387 2a 0a 2a 2a 20 24 49 64 3a 20 69 63 75 2e 63 2c  *.** $Id: icu.c,
35388 76 20 31 2e 37 20 32 30 30 37 2f 31 32 2f 31 33  v 1.7 2007/12/13
35389 20 32 31 3a 35 34 3a 31 31 20 64 72 68 20 45 78   21:54:11 drh Ex
3538a 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  p $.**.** This f
3538b 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
3538c 6e 20 69 6e 74 65 67 72 61 74 69 6f 6e 20 62 65  n integration be
3538d 74 77 65 65 6e 20 74 68 65 20 49 43 55 20 6c 69  tween the ICU li
3538e 62 72 61 72 79 20 0a 2a 2a 20 28 22 49 6e 74 65  brary .** ("Inte
3538f 72 6e 61 74 69 6f 6e 61 6c 20 43 6f 6d 70 6f 6e  rnational Compon
35390 65 6e 74 73 20 66 6f 72 20 55 6e 69 63 6f 64 65  ents for Unicode
35391 22 2c 20 61 6e 20 6f 70 65 6e 2d 73 6f 75 72 63  ", an open-sourc
35392 65 20 6c 69 62 72 61 72 79 20 0a 2a 2a 20 66 6f  e library .** fo
35393 72 20 68 61 6e 64 6c 69 6e 67 20 75 6e 69 63 6f  r handling unico
35394 64 65 20 64 61 74 61 29 20 61 6e 64 20 53 51 4c  de data) and SQL
35395 69 74 65 2e 20 54 68 65 20 69 6e 74 65 67 72 61  ite. The integra
35396 74 69 6f 6e 20 75 73 65 73 20 0a 2a 2a 20 49 43  tion uses .** IC
35397 55 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65  U to provide the
35398 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 20 53 51   following to SQ
35399 4c 69 74 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  Lite:.**.**   * 
3539a 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  An implementatio
3539b 6e 20 6f 66 20 74 68 65 20 53 51 4c 20 72 65 67  n of the SQL reg
3539c 65 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 28  exp() function (
3539d 61 6e 64 20 68 65 6e 63 65 20 52 45 47 45 58 50  and hence REGEXP
3539e 0a 2a 2a 20 20 20 20 20 6f 70 65 72 61 74 6f 72  .**     operator
3539f 29 20 75 73 69 6e 67 20 74 68 65 20 49 43 55 20  ) using the ICU 
353a0 75 72 65 67 65 78 5f 58 58 28 29 20 41 50 49 73  uregex_XX() APIs
353a1 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6d 70 6c  ..**.**   * Impl
353a2 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74  ementations of t
353a3 68 65 20 53 51 4c 20 73 63 61 6c 61 72 20 75 70  he SQL scalar up
353a4 70 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28  per() and lower(
353a5 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20  ) functions.**  
353a6 20 20 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70     for case mapp
353a7 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49  ing..**.**   * I
353a8 6e 74 65 67 72 61 74 69 6f 6e 20 6f 66 20 49 43  ntegration of IC
353a9 55 20 61 6e 64 20 53 51 4c 69 74 65 20 63 6f 6c  U and SQLite col
353aa 6c 61 74 69 6f 6e 20 73 65 71 65 6e 63 65 73 2e  lation seqences.
353ab 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d  .**.**   * An im
353ac 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
353ad 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
353ae 72 20 74 68 61 74 20 75 73 65 73 20 49 43 55 20  r that uses ICU 
353af 74 6f 20 0a 2a 2a 20 20 20 20 20 70 72 6f 76 69  to .**     provi
353b0 64 65 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64  de case-independ
353b1 65 6e 74 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2f  ent matching..*/
353b2 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
353b3 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
353b4 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
353b5 41 42 4c 45 5f 49 43 55 29 0a 0a 2f 2a 20 49 6e  ABLE_ICU)../* In
353b6 63 6c 75 64 65 20 49 43 55 20 68 65 61 64 65 72  clude ICU header
353b7 73 20 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 75  s */.#include <u
353b8 6e 69 63 6f 64 65 2f 75 74 79 70 65 73 2e 68 3e  nicode/utypes.h>
353b9 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f  .#include <unico
353ba 64 65 2f 75 72 65 67 65 78 2e 68 3e 0a 23 69 6e  de/uregex.h>.#in
353bb 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
353bc 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75  string.h>.#inclu
353bd 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 63 6f 6c  de <unicode/ucol
353be 2e 68 3e 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  .h>...#ifndef SQ
353bf 4c 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49  LITE_CORE.  SQLI
353c0 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
353c1 54 31 0a 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a  T1.#else.#endif.
353c2 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c  ./*.** Maximum l
353c3 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 29  ength (in bytes)
353c4 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20   of the pattern 
353c5 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f  in a LIKE or GLO
353c6 42 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  B.** operator..*
353c7 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
353c8 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52  _MAX_LIKE_PATTER
353c9 4e 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e  N_LENGTH.# defin
353ca 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  e SQLITE_MAX_LIK
353cb 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
353cc 20 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f   50000.#endif../
353cd 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 6f 66 20  *.** Version of 
353ce 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 74  sqlite3_free() t
353cf 68 61 74 20 69 73 20 61 6c 77 61 79 73 20 61 20  hat is always a 
353d0 66 75 6e 63 74 69 6f 6e 2c 20 6e 65 76 65 72 20  function, never 
353d1 61 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 73 74 61 74  a macro..*/.stat
353d2 69 63 20 76 6f 69 64 20 78 46 72 65 65 28 76 6f  ic void xFree(vo
353d3 69 64 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  id *p){.  sqlite
353d4 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  3_free(p);.}../*
353d5 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20  .** Compare two 
353d6 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f  UTF-8 strings fo
353d7 72 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 65  r equality where
353d8 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e   the first strin
353d9 67 20 69 73 0a 2a 2a 20 61 20 22 4c 49 4b 45 22  g is.** a "LIKE"
353da 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 52 65 74   expression. Ret
353db 75 72 6e 20 74 72 75 65 20 28 31 29 20 69 66 20  urn true (1) if 
353dc 74 68 65 79 20 61 72 65 20 74 68 65 20 73 61 6d  they are the sam
353dd 65 20 61 6e 64 20 0a 2a 2a 20 66 61 6c 73 65 20  e and .** false 
353de 28 30 29 20 69 66 20 74 68 65 79 20 61 72 65 20  (0) if they are 
353df 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2f 0a 73 74  different..*/.st
353e0 61 74 69 63 20 69 6e 74 20 69 63 75 4c 69 6b 65  atic int icuLike
353e1 43 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74  Compare(.  const
353e2 20 75 69 6e 74 38 5f 74 20 2a 7a 50 61 74 74 65   uint8_t *zPatte
353e3 72 6e 2c 20 20 20 2f 2a 20 4c 49 4b 45 20 70 61  rn,   /* LIKE pa
353e4 74 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ttern */.  const
353e5 20 75 69 6e 74 38 5f 74 20 2a 7a 53 74 72 69 6e   uint8_t *zStrin
353e6 67 2c 20 20 20 20 2f 2a 20 54 68 65 20 55 54 46  g,    /* The UTF
353e7 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d  -8 string to com
353e8 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a  pare against */.
353e9 20 20 63 6f 6e 73 74 20 55 43 68 61 72 33 32 20    const UChar32 
353ea 75 45 73 63 20 20 20 20 20 20 20 20 20 2f 2a 20  uEsc         /* 
353eb 54 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61  The escape chara
353ec 63 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 74 61  cter */.){.  sta
353ed 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 4d 41  tic const int MA
353ee 54 43 48 5f 4f 4e 45 20 3d 20 28 55 43 68 61 72  TCH_ONE = (UChar
353ef 33 32 29 27 5f 27 3b 0a 20 20 73 74 61 74 69 63  32)'_';.  static
353f0 20 63 6f 6e 73 74 20 69 6e 74 20 4d 41 54 43 48   const int MATCH
353f1 5f 41 4c 4c 20 3d 20 28 55 43 68 61 72 33 32 29  _ALL = (UChar32)
353f2 27 25 27 3b 0a 0a 20 20 69 6e 74 20 69 50 61 74  '%';..  int iPat
353f3 74 65 72 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  tern = 0;       
353f4 2f 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65 20  /* Current byte 
353f5 69 6e 64 65 78 20 69 6e 20 7a 50 61 74 74 65 72  index in zPatter
353f6 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 72 69  n */.  int iStri
353f7 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  ng = 0;        /
353f8 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65 20 69  * Current byte i
353f9 6e 64 65 78 20 69 6e 20 7a 53 74 72 69 6e 67 20  ndex in zString 
353fa 2a 2f 0a 0a 20 20 69 6e 74 20 70 72 65 76 45 73  */..  int prevEs
353fb 63 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a  cape = 0;     /*
353fc 20 54 72 75 65 20 69 66 20 74 68 65 20 70 72 65   True if the pre
353fd 76 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 20  vious character 
353fe 77 61 73 20 75 45 73 63 20 2a 2f 0a 0a 20 20 77  was uEsc */..  w
353ff 68 69 6c 65 28 20 7a 50 61 74 74 65 72 6e 5b 69  hile( zPattern[i
35400 50 61 74 74 65 72 6e 5d 21 3d 30 20 29 7b 0a 0a  Pattern]!=0 ){..
35401 20 20 20 20 2f 2a 20 52 65 61 64 20 28 61 6e 64      /* Read (and
35402 20 63 6f 6e 73 75 6d 65 29 20 74 68 65 20 6e 65   consume) the ne
35403 78 74 20 63 68 61 72 61 63 74 65 72 20 66 72 6f  xt character fro
35404 6d 20 74 68 65 20 69 6e 70 75 74 20 70 61 74 74  m the input patt
35405 65 72 6e 2e 20 2a 2f 0a 20 20 20 20 55 43 68 61  ern. */.    UCha
35406 72 33 32 20 75 50 61 74 74 65 72 6e 3b 0a 20 20  r32 uPattern;.  
35407 20 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41 46 45    U8_NEXT_UNSAFE
35408 28 7a 50 61 74 74 65 72 6e 2c 20 69 50 61 74 74  (zPattern, iPatt
35409 65 72 6e 2c 20 75 50 61 74 74 65 72 6e 29 3b 0a  ern, uPattern);.
3540a 20 20 20 20 61 73 73 65 72 74 28 75 50 61 74 74      assert(uPatt
3540b 65 72 6e 21 3d 30 29 3b 0a 0a 20 20 20 20 2f 2a  ern!=0);..    /*
3540c 20 54 68 65 72 65 20 61 72 65 20 6e 6f 77 20 34   There are now 4
3540d 20 70 6f 73 73 69 62 69 6c 69 74 69 65 73 3a 0a   possibilities:.
3540e 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
3540f 20 20 31 2e 20 75 50 61 74 74 65 72 6e 20 69 73    1. uPattern is
35410 20 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61   an unescaped ma
35411 74 63 68 2d 61 6c 6c 20 63 68 61 72 61 63 74 65  tch-all characte
35412 72 20 22 25 22 2c 0a 20 20 20 20 2a 2a 20 20 20  r "%",.    **   
35413 20 20 32 2e 20 75 50 61 74 74 65 72 6e 20 69 73    2. uPattern is
35414 20 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61   an unescaped ma
35415 74 63 68 2d 6f 6e 65 20 63 68 61 72 61 63 74 65  tch-one characte
35416 72 20 22 5f 22 2c 0a 20 20 20 20 2a 2a 20 20 20  r "_",.    **   
35417 20 20 33 2e 20 75 50 61 74 74 65 72 6e 20 69 73    3. uPattern is
35418 20 61 6e 20 75 6e 65 73 63 61 70 65 64 20 65 73   an unescaped es
35419 63 61 70 65 20 63 68 61 72 61 63 74 65 72 2c 20  cape character, 
3541a 6f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 34 2e  or.    **     4.
3541b 20 75 50 61 74 74 65 72 6e 20 69 73 20 74 6f 20   uPattern is to 
3541c 62 65 20 68 61 6e 64 6c 65 64 20 61 73 20 61 6e  be handled as an
3541d 20 6f 72 64 69 6e 61 72 79 20 63 68 61 72 61 63   ordinary charac
3541e 74 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ter.    */.    i
3541f 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26  f( !prevEscape &
35420 26 20 75 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43  & uPattern==MATC
35421 48 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 20 20 2f  H_ALL ){.      /
35422 2a 20 43 61 73 65 20 31 2e 20 2a 2f 0a 20 20 20  * Case 1. */.   
35423 20 20 20 75 69 6e 74 38 5f 74 20 63 3b 0a 0a 20     uint8_t c;.. 
35424 20 20 20 20 20 2f 2a 20 53 6b 69 70 20 61 6e 79       /* Skip any
35425 20 4d 41 54 43 48 5f 41 4c 4c 20 6f 72 20 4d 41   MATCH_ALL or MA
35426 54 43 48 5f 4f 4e 45 20 63 68 61 72 61 63 74 65  TCH_ONE characte
35427 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61  rs that follow a
35428 0a 20 20 20 20 20 20 2a 2a 20 4d 41 54 43 48 5f  .      ** MATCH_
35429 41 4c 4c 2e 20 46 6f 72 20 65 61 63 68 20 4d 41  ALL. For each MA
3542a 54 43 48 5f 4f 4e 45 2c 20 73 6b 69 70 20 6f 6e  TCH_ONE, skip on
3542b 65 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74  e character in t
3542c 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 65 73  he .      ** tes
3542d 74 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 20 20  t string..      
3542e 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
3542f 28 63 3d 7a 50 61 74 74 65 72 6e 5b 69 50 61 74  (c=zPattern[iPat
35430 74 65 72 6e 5d 29 20 3d 3d 20 4d 41 54 43 48 5f  tern]) == MATCH_
35431 41 4c 4c 20 7c 7c 20 63 20 3d 3d 20 4d 41 54 43  ALL || c == MATC
35432 48 5f 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20  H_ONE ){.       
35433 20 69 66 28 20 63 3d 3d 4d 41 54 43 48 5f 4f 4e   if( c==MATCH_ON
35434 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  E ){.          i
35435 66 28 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69  f( zString[iStri
35436 6e 67 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ng]==0 ) return 
35437 30 3b 0a 20 20 20 20 20 20 20 20 20 20 55 38 5f  0;.          U8_
35438 46 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74  FWD_1_UNSAFE(zSt
35439 72 69 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a  ring, iString);.
3543a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3543b 20 20 69 50 61 74 74 65 72 6e 2b 2b 3b 0a 20 20    iPattern++;.  
3543c 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
3543d 20 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65   zPattern[iPatte
3543e 72 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  rn]==0 ) return 
3543f 31 3b 0a 0a 20 20 20 20 20 20 77 68 69 6c 65 28  1;..      while(
35440 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67   zString[iString
35441 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ] ){.        if(
35442 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28   icuLikeCompare(
35443 26 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65  &zPattern[iPatte
35444 72 6e 5d 2c 20 26 7a 53 74 72 69 6e 67 5b 69 53  rn], &zString[iS
35445 74 72 69 6e 67 5d 2c 20 75 45 73 63 29 20 29 7b  tring], uEsc) ){
35446 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
35447 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 1;.        }. 
35448 20 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f         U8_FWD_1_
35449 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20  UNSAFE(zString, 
3544a 69 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20  iString);.      
3544b 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  }.      return 0
3544c 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ;..    }else if(
3544d 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26 20   !prevEscape && 
3544e 75 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43 48 5f  uPattern==MATCH_
3544f 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ONE ){.      /* 
35450 43 61 73 65 20 32 2e 20 2a 2f 0a 20 20 20 20 20  Case 2. */.     
35451 20 69 66 28 20 7a 53 74 72 69 6e 67 5b 69 53 74   if( zString[iSt
35452 72 69 6e 67 5d 3d 3d 30 20 29 20 72 65 74 75 72  ring]==0 ) retur
35453 6e 20 30 3b 0a 20 20 20 20 20 20 55 38 5f 46 57  n 0;.      U8_FW
35454 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72 69  D_1_UNSAFE(zStri
35455 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 0a 20  ng, iString);.. 
35456 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72     }else if( !pr
35457 65 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74  evEscape && uPat
35458 74 65 72 6e 3d 3d 75 45 73 63 29 7b 0a 20 20 20  tern==uEsc){.   
35459 20 20 20 2f 2a 20 43 61 73 65 20 33 2e 20 2a 2f     /* Case 3. */
3545a 0a 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70  .      prevEscap
3545b 65 20 3d 20 31 3b 0a 0a 20 20 20 20 7d 65 6c 73  e = 1;..    }els
3545c 65 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65  e{.      /* Case
3545d 20 34 2e 20 2a 2f 0a 20 20 20 20 20 20 55 43 68   4. */.      UCh
3545e 61 72 33 32 20 75 53 74 72 69 6e 67 3b 0a 20 20  ar32 uString;.  
3545f 20 20 20 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41      U8_NEXT_UNSA
35460 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72  FE(zString, iStr
35461 69 6e 67 2c 20 75 53 74 72 69 6e 67 29 3b 0a 20  ing, uString);. 
35462 20 20 20 20 20 75 53 74 72 69 6e 67 20 3d 20 75       uString = u
35463 5f 66 6f 6c 64 43 61 73 65 28 75 53 74 72 69 6e  _foldCase(uStrin
35464 67 2c 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f 44  g, U_FOLD_CASE_D
35465 45 46 41 55 4c 54 29 3b 0a 20 20 20 20 20 20 75  EFAULT);.      u
35466 50 61 74 74 65 72 6e 20 3d 20 75 5f 66 6f 6c 64  Pattern = u_fold
35467 43 61 73 65 28 75 50 61 74 74 65 72 6e 2c 20 55  Case(uPattern, U
35468 5f 46 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55  _FOLD_CASE_DEFAU
35469 4c 54 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75  LT);.      if( u
3546a 53 74 72 69 6e 67 21 3d 75 50 61 74 74 65 72 6e  String!=uPattern
3546b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
3546c 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
3546d 20 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d      prevEscape =
3546e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
3546f 20 72 65 74 75 72 6e 20 7a 53 74 72 69 6e 67 5b   return zString[
35470 69 53 74 72 69 6e 67 5d 3d 3d 30 3b 0a 7d 0a 0a  iString]==0;.}..
35471 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
35472 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b 65  tion of the like
35473 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  () SQL function.
35474 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20    This function 
35475 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68  implements.** th
35476 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 20  e build-in LIKE 
35477 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 66  operator.  The f
35478 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
35479 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
3547a 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e 20   the.** pattern 
3547b 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61  and the second a
3547c 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73  rgument is the s
3547d 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65 20  tring.  So, the 
3547e 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a  SQL statements:.
3547f 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c 49  **.**       A LI
35480 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d  KE B.**.** is im
35481 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69 6b  plemented as lik
35482 65 28 42 2c 20 41 29 2e 20 49 66 20 74 68 65 72  e(B, A). If ther
35483 65 20 69 73 20 61 6e 20 65 73 63 61 70 65 20 63  e is an escape c
35484 68 61 72 61 63 74 65 72 20 45 2c 20 0a 2a 2a 0a  haracter E, .**.
35485 2a 2a 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20  **       A LIKE 
35486 42 20 45 53 43 41 50 45 20 45 0a 2a 2a 0a 2a 2a  B ESCAPE E.**.**
35487 20 69 73 20 6d 61 70 70 65 64 20 74 6f 20 6c 69   is mapped to li
35488 6b 65 28 42 2c 20 41 2c 20 45 29 2e 0a 2a 2f 0a  ke(B, A, E)..*/.
35489 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 4c  static void icuL
3548a 69 6b 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  ikeFunc(.  sqlit
3548b 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
3548c 65 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63  ext, .  int argc
3548d 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
3548e 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
3548f 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
35490 61 72 20 2a 7a 41 20 3d 20 73 71 6c 69 74 65 33  ar *zA = sqlite3
35491 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
35492 5b 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e  [0]);.  const un
35493 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 42 20  signed char *zB 
35494 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
35495 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
35496 20 55 43 68 61 72 33 32 20 75 45 73 63 20 3d 20   UChar32 uEsc = 
35497 30 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74  0;..  /* Limit t
35498 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
35499 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61   LIKE or GLOB pa
3549a 74 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70  ttern to avoid p
3549b 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20  roblems.  ** of 
3549c 64 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61  deep recursion a
3549d 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20  nd N*N behavior 
3549e 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72  in patternCompar
3549f 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  e()..  */.  if( 
354a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
354a1 74 65 73 28 61 72 67 76 5b 30 5d 29 3e 53 51 4c  tes(argv[0])>SQL
354a2 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54  ITE_MAX_LIKE_PAT
354a3 54 45 52 4e 5f 4c 45 4e 47 54 48 20 29 7b 0a 20  TERN_LENGTH ){. 
354a4 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
354a5 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
354a6 20 22 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70   "LIKE or GLOB p
354a7 61 74 74 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c  attern too compl
354a8 65 78 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65  ex", -1);.    re
354a9 74 75 72 6e 3b 0a 20 20 7d 0a 0a 0a 20 20 69 66  turn;.  }...  if
354aa 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20  ( argc==3 ){.   
354ab 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63   /* The escape c
354ac 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20  haracter string 
354ad 6d 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20  must consist of 
354ae 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63  a single UTF-8 c
354af 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a  haracter..    **
354b0 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
354b1 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20  rn an error..   
354b2 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 3d 20   */.    int nE= 
354b3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
354b4 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  tes(argv[2]);.  
354b5 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
354b6 20 63 68 61 72 20 2a 7a 45 20 3d 20 73 71 6c 69   char *zE = sqli
354b7 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
354b8 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 6e 74  rgv[2]);.    int
354b9 20 69 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20   i = 0;.    if( 
354ba 7a 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  zE==0 ) return;.
354bb 20 20 20 20 55 38 5f 4e 45 58 54 28 7a 45 2c 20      U8_NEXT(zE, 
354bc 69 2c 20 6e 45 2c 20 75 45 73 63 29 3b 0a 20 20  i, nE, uEsc);.  
354bd 20 20 69 66 28 20 69 21 3d 6e 45 29 7b 0a 20 20    if( i!=nE){.  
354be 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
354bf 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
354c0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 45 53  , .          "ES
354c1 43 41 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20  CAPE expression 
354c2 6d 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65  must be a single
354c3 20 63 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29   character", -1)
354c4 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
354c5 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
354c6 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a 20 20 20   zA && zB ){.   
354c7 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
354c8 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 69 63 75  int(context, icu
354c9 4c 69 6b 65 43 6f 6d 70 61 72 65 28 7a 41 2c 20  LikeCompare(zA, 
354ca 7a 42 2c 20 75 45 73 63 29 29 3b 0a 20 20 7d 0a  zB, uEsc));.  }.
354cb 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
354cc 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
354cd 20 77 68 65 6e 20 61 6e 20 49 43 55 20 66 75 6e   when an ICU fun
354ce 63 74 69 6f 6e 20 63 61 6c 6c 65 64 20 66 72 6f  ction called fro
354cf 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20  m within.** the 
354d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
354d1 66 20 61 6e 20 53 51 4c 20 73 63 61 6c 61 72 20  f an SQL scalar 
354d2 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
354d3 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a   an error..**.**
354d4 20 54 68 65 20 73 63 61 6c 61 72 20 66 75 6e 63   The scalar func
354d5 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 70 61 73  tion context pas
354d6 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
354d7 20 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a   argument is .**
354d8 20 6c 6f 61 64 65 64 20 77 69 74 68 20 61 6e 20   loaded with an 
354d9 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 61  error message ba
354da 73 65 64 20 6f 6e 20 74 68 65 20 66 6f 6c 6c 6f  sed on the follo
354db 77 69 6e 67 20 74 77 6f 20 61 72 67 73 2e 0a 2a  wing two args..*
354dc 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
354dd 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 0a  uFunctionError(.
354de 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
354df 74 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 2f  t *pCtx,       /
354e0 2a 20 53 51 4c 69 74 65 20 73 63 61 6c 61 72 20  * SQLite scalar 
354e1 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74  function context
354e2 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
354e3 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
354e4 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 49 43     /* Name of IC
354e5 55 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  U function that 
354e6 66 61 69 6c 65 64 20 2a 2f 0a 20 20 55 45 72 72  failed */.  UErr
354e7 6f 72 43 6f 64 65 20 65 20 20 20 20 20 20 20 20  orCode e        
354e8 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
354e9 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20  r code returned 
354ea 62 79 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20  by ICU function 
354eb 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 7a 42 75  */.){.  char zBu
354ec 66 5b 31 32 38 5d 3b 0a 20 20 73 71 6c 69 74 65  f[128];.  sqlite
354ed 33 5f 73 6e 70 72 69 6e 74 66 28 31 32 38 2c 20  3_snprintf(128, 
354ee 7a 42 75 66 2c 20 22 49 43 55 20 65 72 72 6f 72  zBuf, "ICU error
354ef 3a 20 25 73 28 29 3a 20 25 73 22 2c 20 7a 4e 61  : %s(): %s", zNa
354f0 6d 65 2c 20 75 5f 65 72 72 6f 72 4e 61 6d 65 28  me, u_errorName(
354f1 65 29 29 3b 0a 20 20 7a 42 75 66 5b 31 32 37 5d  e));.  zBuf[127]
354f2 20 3d 20 27 5c 30 27 3b 0a 20 20 73 71 6c 69 74   = '\0';.  sqlit
354f3 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
354f4 70 43 74 78 2c 20 7a 42 75 66 2c 20 2d 31 29 3b  pCtx, zBuf, -1);
354f5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  .}../*.** Functi
354f6 6f 6e 20 74 6f 20 64 65 6c 65 74 65 20 63 6f 6d  on to delete com
354f7 70 69 6c 65 64 20 72 65 67 65 78 70 20 6f 62 6a  piled regexp obj
354f8 65 63 74 73 2e 20 52 65 67 69 73 74 65 72 65 64  ects. Registered
354f9 20 61 73 0a 2a 2a 20 61 20 64 65 73 74 72 75 63   as.** a destruc
354fa 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  tor function wit
354fb 68 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  h sqlite3_set_au
354fc 78 64 61 74 61 28 29 2e 0a 2a 2f 0a 73 74 61 74  xdata()..*/.stat
354fd 69 63 20 76 6f 69 64 20 69 63 75 52 65 67 65 78  ic void icuRegex
354fe 70 44 65 6c 65 74 65 28 76 6f 69 64 20 2a 70 29  pDelete(void *p)
354ff 7b 0a 20 20 55 52 65 67 75 6c 61 72 45 78 70 72  {.  URegularExpr
35500 65 73 73 69 6f 6e 20 2a 70 45 78 70 72 20 3d 20  ession *pExpr = 
35501 28 55 52 65 67 75 6c 61 72 45 78 70 72 65 73 73  (URegularExpress
35502 69 6f 6e 20 2a 29 70 3b 0a 20 20 75 72 65 67 65  ion *)p;.  urege
35503 78 5f 63 6c 6f 73 65 28 70 45 78 70 72 29 3b 0a  x_close(pExpr);.
35504 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
35505 6e 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74  ntation of SQLit
35506 65 20 52 45 47 45 58 50 20 6f 70 65 72 61 74 6f  e REGEXP operato
35507 72 2e 20 54 68 69 73 20 73 63 61 6c 61 72 20 66  r. This scalar f
35508 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 0a 2a 2a  unction takes.**
35509 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20   two arguments. 
3550a 54 68 65 20 66 69 72 73 74 20 69 73 20 61 20 72  The first is a r
3550b 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  egular expressio
3550c 6e 20 70 61 74 74 65 72 6e 20 74 6f 20 63 6f 6d  n pattern to com
3550d 70 69 6c 65 0a 2a 2a 20 74 68 65 20 73 65 63 6f  pile.** the seco
3550e 6e 64 20 69 73 20 61 20 73 74 72 69 6e 67 20 74  nd is a string t
3550f 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20  o match against 
35510 74 68 61 74 20 70 61 74 74 65 72 6e 2e 20 49 66  that pattern. If
35511 20 65 69 74 68 65 72 20 0a 2a 2a 20 61 72 67 75   either .** argu
35512 6d 65 6e 74 20 69 73 20 61 6e 20 53 51 4c 20 4e  ment is an SQL N
35513 55 4c 4c 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 49  ULL, then NULL I
35514 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65  s returned. Othe
35515 72 77 69 73 65 2c 20 74 68 65 20 72 65 73 75 6c  rwise, the resul
35516 74 0a 2a 2a 20 69 73 20 31 20 69 66 20 74 68 65  t.** is 1 if the
35517 20 73 74 72 69 6e 67 20 6d 61 74 63 68 65 73 20   string matches 
35518 74 68 65 20 70 61 74 74 65 72 6e 2c 20 6f 72 20  the pattern, or 
35519 30 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a  0 otherwise..**.
3551a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 70 73 20 74  ** SQLite maps t
3551b 68 65 20 72 65 67 65 78 70 28 29 20 66 75 6e 63  he regexp() func
3551c 74 69 6f 6e 20 74 6f 20 74 68 65 20 72 65 67 65  tion to the rege
3551d 78 70 28 29 20 6f 70 65 72 61 74 6f 72 20 73 75  xp() operator su
3551e 63 68 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 66  ch.** that the f
3551f 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 61 72 65  ollowing two are
35520 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a   equivalent:.**.
35521 2a 2a 20 20 20 20 20 7a 53 74 72 69 6e 67 20 52  **     zString R
35522 45 47 45 58 50 20 7a 50 61 74 74 65 72 6e 0a 2a  EGEXP zPattern.*
35523 2a 20 20 20 20 20 72 65 67 65 78 70 28 7a 50 61  *     regexp(zPa
35524 74 74 65 72 6e 2c 20 7a 53 74 72 69 6e 67 29 0a  ttern, zString).
35525 2a 2a 0a 2a 2a 20 55 73 65 73 20 74 68 65 20 66  **.** Uses the f
35526 6f 6c 6c 6f 77 69 6e 67 20 49 43 55 20 72 65 67  ollowing ICU reg
35527 65 78 70 20 41 50 49 73 3a 0a 2a 2a 0a 2a 2a 20  exp APIs:.**.** 
35528 20 20 20 20 75 72 65 67 65 78 5f 6f 70 65 6e 28      uregex_open(
35529 29 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f  ).**     uregex_
3552a 6d 61 74 63 68 65 73 28 29 0a 2a 2a 20 20 20 20  matches().**    
3552b 20 75 72 65 67 65 78 5f 63 6c 6f 73 65 28 29 0a   uregex_close().
3552c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
3552d 63 75 52 65 67 65 78 70 46 75 6e 63 28 73 71 6c  cuRegexpFunc(sql
3552e 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c  ite3_context *p,
3552f 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74   int nArg, sqlit
35530 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
35531 29 7b 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20  ){.  UErrorCode 
35532 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f  status = U_ZERO_
35533 45 52 52 4f 52 3b 0a 20 20 55 52 65 67 75 6c 61  ERROR;.  URegula
35534 72 45 78 70 72 65 73 73 69 6f 6e 20 2a 70 45 78  rExpression *pEx
35535 70 72 3b 0a 20 20 55 42 6f 6f 6c 20 72 65 73 3b  pr;.  UBool res;
35536 0a 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a  .  const UChar *
35537 7a 53 74 72 69 6e 67 20 3d 20 73 71 6c 69 74 65  zString = sqlite
35538 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 61  3_value_text16(a
35539 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20  pArg[1]);..  /* 
3553a 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64  If the left hand
3553b 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 67   side of the reg
3553c 65 78 70 20 6f 70 65 72 61 74 6f 72 20 69 73 20  exp operator is 
3553d 4e 55 4c 4c 2c 20 0a 20 20 2a 2a 20 74 68 65 6e  NULL, .  ** then
3553e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
3553f 6c 73 6f 20 4e 55 4c 4c 2e 20 0a 20 20 2a 2f 0a  lso NULL. .  */.
35540 20 20 69 66 28 20 21 7a 53 74 72 69 6e 67 20 29    if( !zString )
35541 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
35542 7d 0a 0a 20 20 70 45 78 70 72 20 3d 20 73 71 6c  }..  pExpr = sql
35543 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
35544 28 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 21 70  (p, 0);.  if( !p
35545 45 78 70 72 20 29 7b 0a 20 20 20 20 63 6f 6e 73  Expr ){.    cons
35546 74 20 55 43 68 61 72 20 2a 7a 50 61 74 74 65 72  t UChar *zPatter
35547 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
35548 65 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 30  e_text16(apArg[0
35549 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 50 61  ]);.    if( !zPa
3554a 74 74 65 72 6e 20 29 7b 0a 20 20 20 20 20 20 72  ttern ){.      r
3554b 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
3554c 20 70 45 78 70 72 20 3d 20 75 72 65 67 65 78 5f   pExpr = uregex_
3554d 6f 70 65 6e 28 7a 50 61 74 74 65 72 6e 2c 20 2d  open(zPattern, -
3554e 31 2c 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73  1, 0, 0, &status
3554f 29 3b 0a 0a 20 20 20 20 69 66 28 20 55 5f 53 55  );..    if( U_SU
35550 43 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b  CCESS(status) ){
35551 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
35552 65 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30 2c  et_auxdata(p, 0,
35553 20 70 45 78 70 72 2c 20 69 63 75 52 65 67 65 78   pExpr, icuRegex
35554 70 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 65  pDelete);.    }e
35555 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
35556 74 28 21 70 45 78 70 72 29 3b 0a 20 20 20 20 20  t(!pExpr);.     
35557 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f   icuFunctionErro
35558 72 28 70 2c 20 22 75 72 65 67 65 78 5f 6f 70 65  r(p, "uregex_ope
35559 6e 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20  n", status);.   
3555a 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
3555b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 66 69  .  }..  /* Confi
3555c 67 75 72 65 20 74 68 65 20 74 65 78 74 20 74 68  gure the text th
3555d 61 74 20 74 68 65 20 72 65 67 75 6c 61 72 20 65  at the regular e
3555e 78 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61 74  xpression operat
3555f 65 73 20 6f 6e 2e 20 2a 2f 0a 20 20 75 72 65 67  es on. */.  ureg
35560 65 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72  ex_setText(pExpr
35561 2c 20 7a 53 74 72 69 6e 67 2c 20 2d 31 2c 20 26  , zString, -1, &
35562 73 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21  status);.  if( !
35563 55 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73  U_SUCCESS(status
35564 29 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63  ) ){.    icuFunc
35565 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72  tionError(p, "ur
35566 65 67 65 78 5f 73 65 74 54 65 78 74 22 2c 20 73  egex_setText", s
35567 74 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75  tatus);.    retu
35568 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74  rn;.  }..  /* At
35569 74 65 6d 70 74 20 74 68 65 20 6d 61 74 63 68 20  tempt the match 
3556a 2a 2f 0a 20 20 72 65 73 20 3d 20 75 72 65 67 65  */.  res = urege
3556b 78 5f 6d 61 74 63 68 65 73 28 70 45 78 70 72 2c  x_matches(pExpr,
3556c 20 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20   0, &status);.  
3556d 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73  if( !U_SUCCESS(s
3556e 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63  tatus) ){.    ic
3556f 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70  uFunctionError(p
35570 2c 20 22 75 72 65 67 65 78 5f 6d 61 74 63 68 65  , "uregex_matche
35571 73 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20  s", status);.   
35572 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
35573 2f 2a 20 53 65 74 20 74 68 65 20 74 65 78 74 20  /* Set the text 
35574 74 68 61 74 20 74 68 65 20 72 65 67 75 6c 61 72  that the regular
35575 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 70 65 72   expression oper
35576 61 74 65 73 20 6f 6e 20 74 6f 20 61 20 4e 55 4c  ates on to a NUL
35577 4c 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 2e 20  L.  ** pointer. 
35578 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c  This is not real
35579 6c 79 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75  ly necessary, bu
3557a 74 20 69 74 20 69 73 20 74 69 64 69 65 72 20 74  t it is tidier t
3557b 68 61 6e 20 0a 20 20 2a 2a 20 6c 65 61 76 69 6e  han .  ** leavin
3557c 67 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  g the regular ex
3557d 70 72 65 73 73 69 6f 6e 20 6f 62 6a 65 63 74 20  pression object 
3557e 63 6f 6e 66 69 67 75 72 65 64 20 77 69 74 68 20  configured with 
3557f 61 6e 20 69 6e 76 61 6c 69 64 0a 20 20 2a 2a 20  an invalid.  ** 
35580 70 6f 69 6e 74 65 72 20 61 66 74 65 72 20 74 68  pointer after th
35581 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
35582 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 75 72 65 67  rns..  */.  ureg
35583 65 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72  ex_setText(pExpr
35584 2c 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29  , 0, 0, &status)
35585 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 31  ;..  /* Return 1
35586 20 6f 72 20 30 2e 20 2a 2f 0a 20 20 73 71 6c 69   or 0. */.  sqli
35587 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 70  te3_result_int(p
35588 2c 20 72 65 73 20 3f 20 31 20 3a 20 30 29 3b 0a  , res ? 1 : 0);.
35589 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
3558a 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 63 61 6c  ntations of scal
3558b 61 72 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72  ar functions for
3558c 20 63 61 73 65 20 6d 61 70 70 69 6e 67 20 2d 20   case mapping - 
3558d 75 70 70 65 72 28 29 20 61 6e 64 20 0a 2a 2a 20  upper() and .** 
3558e 6c 6f 77 65 72 28 29 2e 20 46 75 6e 63 74 69 6f  lower(). Functio
3558f 6e 20 75 70 70 65 72 28 29 20 63 6f 6e 76 65 72  n upper() conver
35590 74 73 20 69 74 73 20 69 6e 70 75 74 20 74 6f 20  ts its input to 
35591 75 70 70 65 72 2d 63 61 73 65 20 28 41 42 43 29  upper-case (ABC)
35592 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 6c 6f  ..** Function lo
35593 77 65 72 28 29 20 63 6f 6e 76 65 72 74 73 20 74  wer() converts t
35594 6f 20 6c 6f 77 65 72 2d 63 61 73 65 20 28 61 62  o lower-case (ab
35595 63 29 2e 0a 2a 2a 0a 2a 2a 20 49 43 55 20 70 72  c)..**.** ICU pr
35596 6f 76 69 64 65 73 20 74 77 6f 20 74 79 70 65 73  ovides two types
35597 20 6f 66 20 63 61 73 65 20 6d 61 70 70 69 6e 67   of case mapping
35598 2c 20 22 67 65 6e 65 72 61 6c 22 20 63 61 73 65  , "general" case
35599 20 6d 61 70 70 69 6e 67 20 61 6e 64 0a 2a 2a 20   mapping and.** 
3559a 22 6c 61 6e 67 75 61 67 65 20 73 70 65 63 69 66  "language specif
3559b 69 63 22 2e 20 52 65 66 65 72 20 74 6f 20 49 43  ic". Refer to IC
3559c 55 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  U documentation 
3559d 66 6f 72 20 74 68 65 20 64 69 66 66 65 72 65 6e  for the differen
3559e 63 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 74  ces.** between t
3559f 68 65 20 74 77 6f 2e 0a 2a 2a 0a 2a 2a 20 54 6f  he two..**.** To
355a0 20 75 74 69 6c 69 73 65 20 22 67 65 6e 65 72 61   utilise "genera
355a1 6c 22 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c  l" case mapping,
355a2 20 74 68 65 20 75 70 70 65 72 28 29 20 6f 72 20   the upper() or 
355a3 6c 6f 77 65 72 28 29 20 73 63 61 6c 61 72 20 0a  lower() scalar .
355a4 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  ** functions are
355a5 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 6f 6e   invoked with on
355a6 65 20 61 72 67 75 6d 65 6e 74 3a 0a 2a 2a 0a 2a  e argument:.**.*
355a7 2a 20 20 20 20 20 75 70 70 65 72 28 27 41 42 43  *     upper('ABC
355a8 27 29 20 2d 3e 20 27 61 62 63 27 0a 2a 2a 20 20  ') -> 'abc'.**  
355a9 20 20 20 6c 6f 77 65 72 28 27 61 62 63 27 29 20     lower('abc') 
355aa 2d 3e 20 27 41 42 43 27 0a 2a 2a 0a 2a 2a 20 54  -> 'ABC'.**.** T
355ab 6f 20 61 63 63 65 73 73 20 49 43 55 20 22 6c 61  o access ICU "la
355ac 6e 67 75 61 67 65 20 73 70 65 63 69 66 69 63 22  nguage specific"
355ad 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 75   case mapping, u
355ae 70 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28  pper() or lower(
355af 29 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 69  ).** should be i
355b0 6e 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20  nvoked with two 
355b1 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 73  arguments. The s
355b2 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
355b3 73 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66  s the name.** of
355b4 20 74 68 65 20 6c 6f 63 61 6c 65 20 74 6f 20 75   the locale to u
355b5 73 65 2e 20 50 61 73 73 69 6e 67 20 61 6e 20 65  se. Passing an e
355b6 6d 70 74 79 20 73 74 72 69 6e 67 20 28 22 22 29  mpty string ("")
355b7 20 6f 72 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c   or SQL NULL val
355b8 75 65 0a 2a 2a 20 61 73 20 74 68 65 20 73 65 63  ue.** as the sec
355b9 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
355ba 74 68 65 20 73 61 6d 65 20 61 73 20 69 6e 76 6f  the same as invo
355bb 6b 69 6e 67 20 74 68 65 20 31 20 61 72 67 75 6d  king the 1 argum
355bc 65 6e 74 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f  ent version.** o
355bd 66 20 75 70 70 65 72 28 29 20 6f 72 20 6c 6f 77  f upper() or low
355be 65 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  er()..**.**     
355bf 6c 6f 77 65 72 28 27 49 27 2c 20 27 65 6e 5f 75  lower('I', 'en_u
355c0 73 27 29 20 2d 3e 20 27 69 27 0a 2a 2a 20 20 20  s') -> 'i'.**   
355c1 20 20 6c 6f 77 65 72 28 27 49 27 2c 20 27 74 72    lower('I', 'tr
355c2 5f 74 72 27 29 20 2d 3e 20 27 c4 b1 27 20 28 73  _tr') -> '..' (s
355c3 6d 61 6c 6c 20 64 6f 74 6c 65 73 73 20 69 29 0a  mall dotless i).
355c4 2a 2a 0a 2a 2a 20 68 74 74 70 3a 2f 2f 77 77 77  **.** http://www
355c5 2e 69 63 75 2d 70 72 6f 6a 65 63 74 2e 6f 72 67  .icu-project.org
355c6 2f 75 73 65 72 67 75 69 64 65 2f 70 6f 73 69 78  /userguide/posix
355c7 2e 68 74 6d 6c 23 63 61 73 65 5f 6d 61 70 70 69  .html#case_mappi
355c8 6e 67 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ngs.*/.static vo
355c9 69 64 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  id icuCaseFunc16
355ca 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
355cb 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73   *p, int nArg, s
355cc 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
355cd 70 41 72 67 29 7b 0a 20 20 63 6f 6e 73 74 20 55  pArg){.  const U
355ce 43 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20 20  Char *zInput;.  
355cf 55 43 68 61 72 20 2a 7a 4f 75 74 70 75 74 3b 0a  UChar *zOutput;.
355d0 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a 20 20    int nInput;.  
355d1 69 6e 74 20 6e 4f 75 74 70 75 74 3b 0a 0a 20 20  int nOutput;..  
355d2 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75  UErrorCode statu
355d3 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52  s = U_ZERO_ERROR
355d4 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
355d5 7a 4c 6f 63 61 6c 65 20 3d 20 30 3b 0a 0a 20 20  zLocale = 0;..  
355d6 61 73 73 65 72 74 28 6e 41 72 67 3d 3d 31 20 7c  assert(nArg==1 |
355d7 7c 20 6e 41 72 67 3d 3d 32 29 3b 0a 20 20 69 66  | nArg==2);.  if
355d8 28 20 6e 41 72 67 3d 3d 32 20 29 7b 0a 20 20 20  ( nArg==2 ){.   
355d9 20 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73   zLocale = (cons
355da 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
355db 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72  _value_text(apAr
355dc 67 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 7a 49  g[1]);.  }..  zI
355dd 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76  nput = sqlite3_v
355de 61 6c 75 65 5f 74 65 78 74 31 36 28 61 70 41 72  alue_text16(apAr
355df 67 5b 30 5d 29 3b 0a 20 20 69 66 28 20 21 7a 49  g[0]);.  if( !zI
355e0 6e 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75  nput ){.    retu
355e1 72 6e 3b 0a 20 20 7d 0a 20 20 6e 49 6e 70 75 74  rn;.  }.  nInput
355e2 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
355e3 5f 62 79 74 65 73 31 36 28 61 70 41 72 67 5b 30  _bytes16(apArg[0
355e4 5d 29 3b 0a 0a 20 20 6e 4f 75 74 70 75 74 20 3d  ]);..  nOutput =
355e5 20 6e 49 6e 70 75 74 20 2a 20 32 20 2b 20 32 3b   nInput * 2 + 2;
355e6 0a 20 20 7a 4f 75 74 70 75 74 20 3d 20 73 71 6c  .  zOutput = sql
355e7 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4f 75 74  ite3_malloc(nOut
355e8 70 75 74 29 3b 0a 20 20 69 66 28 20 21 7a 4f 75  put);.  if( !zOu
355e9 74 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75  tput ){.    retu
355ea 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73  rn;.  }..  if( s
355eb 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
355ec 28 70 29 20 29 7b 0a 20 20 20 20 75 5f 73 74 72  (p) ){.    u_str
355ed 54 6f 55 70 70 65 72 28 7a 4f 75 74 70 75 74 2c  ToUpper(zOutput,
355ee 20 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70   nOutput/2, zInp
355ef 75 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c  ut, nInput/2, zL
355f0 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b  ocale, &status);
355f1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 5f  .  }else{.    u_
355f2 73 74 72 54 6f 4c 6f 77 65 72 28 7a 4f 75 74 70  strToLower(zOutp
355f3 75 74 2c 20 6e 4f 75 74 70 75 74 2f 32 2c 20 7a  ut, nOutput/2, z
355f4 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2f 32 2c  Input, nInput/2,
355f5 20 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61 74 75   zLocale, &statu
355f6 73 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  s);.  }..  if( !
355f7 55 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73  U_SUCCESS(status
355f8 29 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63  ) ){.    icuFunc
355f9 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 5f  tionError(p, "u_
355fa 73 74 72 54 6f 4c 6f 77 65 72 28 29 2f 75 5f 73  strToLower()/u_s
355fb 74 72 54 6f 55 70 70 65 72 22 2c 20 73 74 61 74  trToUpper", stat
355fc 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  us);.    return;
355fd 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
355fe 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 70 2c  result_text16(p,
355ff 20 7a 4f 75 74 70 75 74 2c 20 2d 31 2c 20 78 46   zOutput, -1, xF
35600 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ree);.}../*.** C
35601 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
35602 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
35603 63 74 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20  ction. The pCtx 
35604 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20  argument points 
35605 74 6f 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f  to.** a UCollato
35606 72 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76  r structure prev
35607 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
35608 20 75 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e   using ucol_open
35609 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
3560a 69 64 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 44  id icuCollationD
3560b 65 6c 28 76 6f 69 64 20 2a 70 43 74 78 29 7b 0a  el(void *pCtx){.
3560c 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d    UCollator *p =
3560d 20 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43   (UCollator *)pC
3560e 74 78 3b 0a 20 20 75 63 6f 6c 5f 63 6c 6f 73 65  tx;.  ucol_close
3560f 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  (p);.}../*.** Co
35610 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
35611 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
35612 74 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61  tion. The pCtx a
35613 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74  rgument points t
35614 6f 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72  o.** a UCollator
35615 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69   structure previ
35616 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
35617 75 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28  using ucol_open(
35618 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
35619 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c   icuCollationCol
3561a 6c 28 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 2c  l(.  void *pCtx,
3561b 0a 20 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20  .  int nLeft,.  
3561c 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4c 65 66  const void *zLef
3561d 74 2c 0a 20 20 69 6e 74 20 6e 52 69 67 68 74 2c  t,.  int nRight,
3561e 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
3561f 52 69 67 68 74 0a 29 7b 0a 20 20 55 43 6f 6c 6c  Right.){.  UColl
35620 61 74 69 6f 6e 52 65 73 75 6c 74 20 72 65 73 3b  ationResult res;
35621 0a 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20  .  UCollator *p 
35622 3d 20 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70  = (UCollator *)p
35623 43 74 78 3b 0a 20 20 72 65 73 20 3d 20 75 63 6f  Ctx;.  res = uco
35624 6c 5f 73 74 72 63 6f 6c 6c 28 70 2c 20 28 55 43  l_strcoll(p, (UC
35625 68 61 72 20 2a 29 7a 4c 65 66 74 2c 20 6e 4c 65  har *)zLeft, nLe
35626 66 74 2f 32 2c 20 28 55 43 68 61 72 20 2a 29 7a  ft/2, (UChar *)z
35627 52 69 67 68 74 2c 20 6e 52 69 67 68 74 2f 32 29  Right, nRight/2)
35628 3b 0a 20 20 73 77 69 74 63 68 28 20 72 65 73 20  ;.  switch( res 
35629 29 7b 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c  ){.    case UCOL
3562a 5f 4c 45 53 53 3a 20 20 20 20 72 65 74 75 72 6e  _LESS:    return
3562b 20 2d 31 3b 0a 20 20 20 20 63 61 73 65 20 55 43   -1;.    case UC
3562c 4f 4c 5f 47 52 45 41 54 45 52 3a 20 72 65 74 75  OL_GREATER: retu
3562d 72 6e 20 2b 31 3b 0a 20 20 20 20 63 61 73 65 20  rn +1;.    case 
3562e 55 43 4f 4c 5f 45 51 55 41 4c 3a 20 20 20 72 65  UCOL_EQUAL:   re
3562f 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73  turn 0;.  }.  as
35630 73 65 72 74 28 21 22 55 6e 65 78 70 65 63 74 65  sert(!"Unexpecte
35631 64 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66  d return value f
35632 72 6f 6d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c  rom ucol_strcoll
35633 28 29 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  ()");.  return 0
35634 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
35635 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
35636 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
35637 20 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74   icu_load_collat
35638 69 6f 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ion()..**.** Thi
35639 73 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  s scalar functio
3563a 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64  n is used to add
3563b 20 49 43 55 20 63 6f 6c 6c 61 74 69 6f 6e 20 62   ICU collation b
3563c 61 73 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a  ased collation .
3563d 2a 2a 20 74 79 70 65 73 20 74 6f 20 61 6e 20 53  ** types to an S
3563e 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 63  QLite database c
3563f 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 74 20 69 73  onnection. It is
35640 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
35641 63 61 6c 6c 65 64 0a 2a 2a 20 61 73 20 66 6f 6c  called.** as fol
35642 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lows:.**.**     
35643 53 45 4c 45 43 54 20 69 63 75 5f 6c 6f 61 64 5f  SELECT icu_load_
35644 63 6f 6c 6c 61 74 69 6f 6e 28 3c 6c 6f 63 61 6c  collation(<local
35645 65 3e 2c 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e  e>, <collation-n
35646 61 6d 65 3e 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65  ame>);.**.** Whe
35647 72 65 20 3c 6c 6f 63 61 6c 65 3e 20 69 73 20 61  re <locale> is a
35648 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69   string containi
35649 6e 67 20 61 6e 20 49 43 55 20 6c 6f 63 61 6c 65  ng an ICU locale
3564a 20 69 64 65 6e 74 69 66 69 65 72 20 28 69 2e 65   identifier (i.e
3564b 2e 0a 2a 2a 20 22 65 6e 5f 41 55 22 2c 20 22 74  ..** "en_AU", "t
3564c 72 5f 54 52 22 20 65 74 63 2e 29 20 61 6e 64 20  r_TR" etc.) and 
3564d 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e  <collation-name>
3564e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
3564f 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e  the.** collation
35650 20 73 65 71 75 65 6e 63 65 20 74 6f 20 63 72 65   sequence to cre
35651 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ate..*/.static v
35652 6f 69 64 20 69 63 75 4c 6f 61 64 43 6f 6c 6c 61  oid icuLoadColla
35653 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  tion(.  sqlite3_
35654 63 6f 6e 74 65 78 74 20 2a 70 2c 20 0a 20 20 69  context *p, .  i
35655 6e 74 20 6e 41 72 67 2c 20 0a 20 20 73 71 6c 69  nt nArg, .  sqli
35656 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72  te3_value **apAr
35657 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  g.){.  sqlite3 *
35658 64 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29  db = (sqlite3 *)
35659 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
3565a 61 28 70 29 3b 0a 20 20 55 45 72 72 6f 72 43 6f  a(p);.  UErrorCo
3565b 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45  de status = U_ZE
3565c 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73  RO_ERROR;.  cons
3565d 74 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b  t char *zLocale;
3565e 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 65 20        /* Locale 
3565f 69 64 65 6e 74 69 66 69 65 72 20 2d 20 28 65 67  identifier - (eg
35660 2e 20 22 6a 70 5f 4a 50 22 29 20 2a 2f 0a 20 20  . "jp_JP") */.  
35661 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
35662 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  e;        /* SQL
35663 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   Collation seque
35664 6e 63 65 20 6e 61 6d 65 20 28 65 67 2e 20 22 6a  nce name (eg. "j
35665 61 70 61 6e 65 73 65 22 29 20 2a 2f 0a 20 20 55  apanese") */.  U
35666 43 6f 6c 6c 61 74 6f 72 20 2a 70 55 43 6f 6c 6c  Collator *pUColl
35667 61 74 6f 72 3b 20 20 20 20 2f 2a 20 49 43 55 20  ator;    /* ICU 
35668 6c 69 62 72 61 72 79 20 63 6f 6c 6c 61 74 69 6f  library collatio
35669 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e  n object */.  in
3566a 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
3566b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
3566c 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69  n code from sqli
3566d 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
3566e 74 69 6f 6e 5f 78 28 29 20 2a 2f 0a 0a 20 20 61  tion_x() */..  a
3566f 73 73 65 72 74 28 6e 41 72 67 3d 3d 32 29 3b 0a  ssert(nArg==2);.
35670 20 20 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e    zLocale = (con
35671 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
35672 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41  3_value_text(apA
35673 72 67 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20  rg[0]);.  zName 
35674 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
35675 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
35676 78 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20  xt(apArg[1]);.. 
35677 20 69 66 28 20 21 7a 4c 6f 63 61 6c 65 20 7c 7c   if( !zLocale ||
35678 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 72   !zName ){.    r
35679 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 55  eturn;.  }..  pU
3567a 43 6f 6c 6c 61 74 6f 72 20 3d 20 75 63 6f 6c 5f  Collator = ucol_
3567b 6f 70 65 6e 28 7a 4c 6f 63 61 6c 65 2c 20 26 73  open(zLocale, &s
3567c 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55  tatus);.  if( !U
3567d 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
3567e 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
3567f 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 63 6f  ionError(p, "uco
35680 6c 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75 73 29  l_open", status)
35681 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
35682 7d 0a 20 20 61 73 73 65 72 74 28 70 29 3b 0a 0a  }.  assert(p);..
35683 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63    rc = sqlite3_c
35684 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
35685 76 32 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 53 51  v2(db, zName, SQ
35686 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69  LITE_UTF16, (voi
35687 64 20 2a 29 70 55 43 6f 6c 6c 61 74 6f 72 2c 20  d *)pUCollator, 
35688 0a 20 20 20 20 20 20 69 63 75 43 6f 6c 6c 61 74  .      icuCollat
35689 69 6f 6e 43 6f 6c 6c 2c 20 69 63 75 43 6f 6c 6c  ionColl, icuColl
3568a 61 74 69 6f 6e 44 65 6c 0a 20 20 29 3b 0a 20 20  ationDel.  );.  
3568b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3568c 4b 20 29 7b 0a 20 20 20 20 75 63 6f 6c 5f 63 6c  K ){.    ucol_cl
3568d 6f 73 65 28 70 55 43 6f 6c 6c 61 74 6f 72 29 3b  ose(pUCollator);
3568e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3568f 75 6c 74 5f 65 72 72 6f 72 28 70 2c 20 22 45 72  ult_error(p, "Er
35690 72 6f 72 20 72 65 67 69 73 74 65 72 69 6e 67 20  ror registering 
35691 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69  collation functi
35692 6f 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 7d 0a  on", -1);.  }.}.
35693 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
35694 74 68 65 20 49 43 55 20 65 78 74 65 6e 73 69 6f  the ICU extensio
35695 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74 68  n functions with
35696 20 64 61 74 61 62 61 73 65 20 64 62 2e 0a 2a 2f   database db..*/
35697 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
35698 69 6e 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e  int sqlite3IcuIn
35699 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  it(sqlite3 *db){
3569a 0a 20 20 73 74 72 75 63 74 20 49 63 75 53 63 61  .  struct IcuSca
3569b 6c 61 72 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20  lar {.    const 
3569c 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
3569d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3569e 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20      /* Function 
3569f 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  name */.    int 
356a0 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  nArg;           
356a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
356a2 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
356a3 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  of arguments */.
356a4 20 20 20 20 69 6e 74 20 65 6e 63 3b 20 20 20 20      int enc;    
356a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
356a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
356a7 20 4f 70 74 69 6d 61 6c 20 74 65 78 74 20 65 6e   Optimal text en
356a8 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 76 6f  coding */.    vo
356a9 69 64 20 2a 70 43 6f 6e 74 65 78 74 3b 20 20 20  id *pContext;   
356aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
356ab 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
356ac 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 63  e3_user_data() c
356ad 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 76 6f  ontext */.    vo
356ae 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
356af 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
356b0 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
356b1 29 3b 0a 20 20 7d 20 73 63 61 6c 61 72 73 5b 5d  );.  } scalars[]
356b2 20 3d 20 7b 0a 20 20 20 20 7b 22 72 65 67 65 78   = {.    {"regex
356b3 70 22 2c 2d 31 2c 20 53 51 4c 49 54 45 5f 41 4e  p",-1, SQLITE_AN
356b4 59 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 69  Y,          0, i
356b5 63 75 52 65 67 65 78 70 46 75 6e 63 7d 2c 0a 0a  cuRegexpFunc},..
356b6 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 31      {"lower",  1
356b7 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20  , SQLITE_UTF16, 
356b8 20 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73         0, icuCas
356b9 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22  eFunc16},.    {"
356ba 6c 6f 77 65 72 22 2c 20 20 32 2c 20 53 51 4c 49  lower",  2, SQLI
356bb 54 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20 20  TE_UTF16,       
356bc 20 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31   0, icuCaseFunc1
356bd 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22  6},.    {"upper"
356be 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  ,  1, SQLITE_UTF
356bf 31 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20 69 63  16, (void*)1, ic
356c0 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20  uCaseFunc16},.  
356c1 20 20 7b 22 75 70 70 65 72 22 2c 20 20 32 2c 20    {"upper",  2, 
356c2 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76  SQLITE_UTF16, (v
356c3 6f 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46  oid*)1, icuCaseF
356c4 75 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c  unc16},..    {"l
356c5 6f 77 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54  ower",  1, SQLIT
356c6 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20  E_UTF8,         
356c7 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  0, icuCaseFunc16
356c8 7d 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c  },.    {"lower",
356c9 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38    2, SQLITE_UTF8
356ca 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75  ,         0, icu
356cb 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20  CaseFunc16},.   
356cc 20 7b 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53   {"upper",  1, S
356cd 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 28 76 6f  QLITE_UTF8,  (vo
356ce 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75  id*)1, icuCaseFu
356cf 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70  nc16},.    {"upp
356d0 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f  er",  2, SQLITE_
356d1 55 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c  UTF8,  (void*)1,
356d2 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c   icuCaseFunc16},
356d3 0a 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20  ..    {"like",  
356d4 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
356d5 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 4c           0, icuL
356d6 69 6b 65 46 75 6e 63 7d 2c 0a 20 20 20 20 7b 22  ikeFunc},.    {"
356d7 6c 69 6b 65 22 2c 20 20 20 33 2c 20 53 51 4c 49  like",   3, SQLI
356d8 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20  TE_UTF8,        
356d9 20 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d   0, icuLikeFunc}
356da 2c 0a 0a 20 20 20 20 7b 22 69 63 75 5f 6c 6f 61  ,..    {"icu_loa
356db 64 5f 63 6f 6c 6c 61 74 69 6f 6e 22 2c 20 20 32  d_collation",  2
356dc 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 28  , SQLITE_UTF8, (
356dd 76 6f 69 64 2a 29 64 62 2c 20 69 63 75 4c 6f 61  void*)db, icuLoa
356de 64 43 6f 6c 6c 61 74 69 6f 6e 7d 2c 0a 20 20 7d  dCollation},.  }
356df 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ;..  int rc = SQ
356e0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69  LITE_OK;.  int i
356e1 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63  ;..  for(i=0; rc
356e2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
356e3 3c 28 73 69 7a 65 6f 66 28 73 63 61 6c 61 72 73  <(sizeof(scalars
356e4 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  )/sizeof(struct 
356e5 49 63 75 53 63 61 6c 61 72 29 29 3b 20 69 2b 2b  IcuScalar)); i++
356e6 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 49 63  ){.    struct Ic
356e7 75 53 63 61 6c 61 72 20 2a 70 20 3d 20 26 73 63  uScalar *p = &sc
356e8 61 6c 61 72 73 5b 69 5d 3b 0a 20 20 20 20 72 63  alars[i];.    rc
356e9 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
356ea 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20  e_function(.    
356eb 20 20 20 20 64 62 2c 20 70 2d 3e 7a 4e 61 6d 65      db, p->zName
356ec 2c 20 70 2d 3e 6e 41 72 67 2c 20 70 2d 3e 65 6e  , p->nArg, p->en
356ed 63 2c 20 70 2d 3e 70 43 6f 6e 74 65 78 74 2c 20  c, p->pContext, 
356ee 70 2d 3e 78 46 75 6e 63 2c 20 30 2c 20 30 0a 20  p->xFunc, 0, 0. 
356ef 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74     );.  }..  ret
356f0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21  urn rc;.}..#if !
356f1 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49  SQLITE_CORE.SQLI
356f2 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
356f3 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
356f4 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
356f5 2c 20 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  , .  char **pzEr
356f6 72 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71  rMsg,.  const sq
356f7 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
356f8 65 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51  es *pApi.){.  SQ
356f9 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49  LITE_EXTENSION_I
356fa 4e 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74  NIT2(pApi).  ret
356fb 75 72 6e 20 73 71 6c 69 74 65 33 49 63 75 49 6e  urn sqlite3IcuIn
356fc 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66  it(db);.}.#endif
356fd 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ..#endif../*****
356fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
356ff 20 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a   icu.c *********
35700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35701 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35702 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
35703 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
35704 66 69 6c 65 20 66 74 73 33 5f 69 63 75 2e 63 20  file fts3_icu.c 
35705 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35706 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35707 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
35708 32 30 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a  2007 June 22.**.
35709 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
3570a 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
3570b 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
3570c 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
3570d 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
3570e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
3570f 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
35710 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
35711 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
35712 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
35713 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
35714 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
35715 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
35716 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
35717 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
35718 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
35719 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
3571a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
3571b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3571c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3571d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3571e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
3571f 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
35720 6e 74 73 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20  nts a tokenizer 
35721 66 6f 72 20 66 74 73 33 20 62 61 73 65 64 20 6f  for fts3 based o
35722 6e 20 74 68 65 20 49 43 55 20 6c 69 62 72 61 72  n the ICU librar
35723 79 2e 0a 2a 2a 20 0a 2a 2a 20 24 49 64 3a 20 66  y..** .** $Id: f
35724 74 73 33 5f 69 63 75 2e 63 2c 76 20 31 2e 33 20  ts3_icu.c,v 1.3 
35725 32 30 30 38 2f 30 39 2f 30 31 20 31 38 3a 33 34  2008/09/01 18:34
35726 3a 32 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :20 danielk1977 
35727 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64  Exp $.*/..#if !d
35728 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
35729 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
3572a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
3572b 33 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  3).#ifdef SQLITE
3572c 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 0a 0a 23 69  _ENABLE_ICU...#i
3572d 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f  nclude <unicode/
3572e 75 62 72 6b 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  ubrk.h>.#include
3572f 20 3c 75 6e 69 63 6f 64 65 2f 75 74 66 31 36 2e   <unicode/utf16.
35730 68 3e 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  h>..typedef stru
35731 63 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20  ct IcuTokenizer 
35732 49 63 75 54 6f 6b 65 6e 69 7a 65 72 3b 0a 74 79  IcuTokenizer;.ty
35733 70 65 64 65 66 20 73 74 72 75 63 74 20 49 63 75  pedef struct Icu
35734 43 75 72 73 6f 72 20 49 63 75 43 75 72 73 6f 72  Cursor IcuCursor
35735 3b 0a 0a 73 74 72 75 63 74 20 49 63 75 54 6f 6b  ;..struct IcuTok
35736 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74  enizer {.  sqlit
35737 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73  e3_tokenizer bas
35738 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 61  e;.  char *zLoca
35739 6c 65 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 49  le;.};..struct I
3573a 63 75 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  cuCursor {.  sql
3573b 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3573c 75 72 73 6f 72 20 62 61 73 65 3b 0a 0a 20 20 55  ursor base;..  U
3573d 42 72 65 61 6b 49 74 65 72 61 74 6f 72 20 2a 70  BreakIterator *p
3573e 49 74 65 72 3b 20 20 20 20 20 20 2f 2a 20 49 43  Iter;      /* IC
3573f 55 20 62 72 65 61 6b 2d 69 74 65 72 61 74 6f 72  U break-iterator
35740 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
35741 20 6e 43 68 61 72 3b 20 20 20 20 20 20 20 20 20   nChar;         
35742 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
35743 65 72 20 6f 66 20 55 43 68 61 72 20 65 6c 65 6d  er of UChar elem
35744 65 6e 74 73 20 69 6e 20 70 49 6e 70 75 74 20 2a  ents in pInput *
35745 2f 0a 20 20 55 43 68 61 72 20 2a 61 43 68 61 72  /.  UChar *aChar
35746 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35747 2f 2a 20 43 6f 70 79 20 6f 66 20 69 6e 70 75 74  /* Copy of input
35748 20 75 73 69 6e 67 20 75 74 66 2d 31 36 20 65 6e   using utf-16 en
35749 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  coding */.  int 
3574a 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20  *aOffset;       
3574b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
3574c 74 73 20 6f 66 20 65 61 63 68 20 63 68 61 72 61  ts of each chara
3574d 63 74 65 72 20 69 6e 20 75 74 66 2d 38 20 69 6e  cter in utf-8 in
3574e 70 75 74 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 42  put */..  int nB
3574f 75 66 66 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a  uffer;.  char *z
35750 42 75 66 66 65 72 3b 0a 0a 20 20 69 6e 74 20 69  Buffer;..  int i
35751 54 6f 6b 65 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  Token;.};../*.**
35752 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f   Create a new to
35753 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63 65  kenizer instance
35754 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35755 69 63 75 43 72 65 61 74 65 28 0a 20 20 69 6e 74  icuCreate(.  int
35756 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20   argc,          
35757 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35758 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
35759 6e 74 72 69 65 73 20 69 6e 20 61 72 67 76 5b 5d  ntries in argv[]
3575a 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3575b 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20   * const *argv, 
3575c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
3575d 6b 65 6e 69 7a 65 72 20 63 72 65 61 74 69 6f 6e  kenizer creation
3575e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
3575f 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
35760 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20  r **ppTokenizer 
35761 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65       /* OUT: Cre
35762 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a  ated tokenizer *
35763 2f 0a 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69  /.){.  IcuTokeni
35764 7a 65 72 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 20  zer *p;.  int n 
35765 3d 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63  = 0;..  if( argc
35766 3e 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74  >0 ){.    n = st
35767 72 6c 65 6e 28 61 72 67 76 5b 30 5d 29 2b 31 3b  rlen(argv[0])+1;
35768 0a 20 20 7d 0a 20 20 70 20 3d 20 28 49 63 75 54  .  }.  p = (IcuT
35769 6f 6b 65 6e 69 7a 65 72 20 2a 29 73 71 6c 69 74  okenizer *)sqlit
3576a 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
3576b 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 2b 6e  (IcuTokenizer)+n
3576c 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  );.  if( !p ){. 
3576d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3576e 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65  _NOMEM;.  }.  me
3576f 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f  mset(p, 0, sizeo
35770 66 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 29  f(IcuTokenizer))
35771 3b 0a 0a 20 20 69 66 28 20 6e 20 29 7b 0a 20 20  ;..  if( n ){.  
35772 20 20 70 2d 3e 7a 4c 6f 63 61 6c 65 20 3d 20 28    p->zLocale = (
35773 63 68 61 72 20 2a 29 26 70 5b 31 5d 3b 0a 20 20  char *)&p[1];.  
35774 20 20 6d 65 6d 63 70 79 28 70 2d 3e 7a 4c 6f 63    memcpy(p->zLoc
35775 61 6c 65 2c 20 61 72 67 76 5b 30 5d 2c 20 6e 29  ale, argv[0], n)
35776 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65  ;.  }..  *ppToke
35777 6e 69 7a 65 72 20 3d 20 28 73 71 6c 69 74 65 33  nizer = (sqlite3
35778 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 3b 0a  _tokenizer *)p;.
35779 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3577a 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
3577b 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65  stroy a tokenize
3577c 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  r.*/.static int 
3577d 69 63 75 44 65 73 74 72 6f 79 28 73 71 6c 69 74  icuDestroy(sqlit
3577e 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
3577f 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 49 63 75  okenizer){.  Icu
35780 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20 28  Tokenizer *p = (
35781 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 70  IcuTokenizer *)p
35782 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c  Tokenizer;.  sql
35783 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
35784 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
35785 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
35786 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65  re to begin toke
35787 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75  nizing a particu
35788 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65  lar string.  The
35789 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67   input.** string
3578a 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64   to be tokenized
3578b 20 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e 42   is pInput[0..nB
3578c 79 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72 73  ytes-1].  A curs
3578d 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e  or.** used to in
3578e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65  crementally toke
3578f 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67  nize this string
35790 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
35791 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a  .** *ppCursor..*
35792 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75  /.static int icu
35793 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  Open(.  sqlite3_
35794 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
35795 6e 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f  nizer,         /
35796 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  * The tokenizer 
35797 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
35798 2a 7a 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20  *zInput,        
35799 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
3579a 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20  nput string */. 
3579b 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20 20   int nInput,    
3579c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3579d 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
3579e 68 20 6f 66 20 7a 49 6e 70 75 74 20 69 6e 20 62  h of zInput in b
3579f 79 74 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ytes */.  sqlite
357a0 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
357a1 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20  or **ppCursor   
357a2 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a   /* OUT: Tokeniz
357a3 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a  ation cursor */.
357a4 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65  ){.  IcuTokenize
357a5 72 20 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e  r *p = (IcuToken
357a6 69 7a 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a 65  izer *)pTokenize
357a7 72 3b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a  r;.  IcuCursor *
357a8 70 43 73 72 3b 0a 0a 20 20 63 6f 6e 73 74 20 69  pCsr;..  const i
357a9 6e 74 33 32 5f 74 20 6f 70 74 20 3d 20 55 5f 46  nt32_t opt = U_F
357aa 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54  OLD_CASE_DEFAULT
357ab 3b 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73  ;.  UErrorCode s
357ac 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45  tatus = U_ZERO_E
357ad 52 52 4f 52 3b 0a 20 20 69 6e 74 20 6e 43 68 61  RROR;.  int nCha
357ae 72 3b 0a 0a 20 20 55 43 68 61 72 33 32 20 63 3b  r;..  UChar32 c;
357af 0a 20 20 69 6e 74 20 69 49 6e 70 75 74 20 3d 20  .  int iInput = 
357b0 30 3b 0a 20 20 69 6e 74 20 69 4f 75 74 20 3d 20  0;.  int iOut = 
357b1 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20  0;..  *ppCursor 
357b2 3d 20 30 3b 0a 0a 20 20 69 66 28 20 6e 49 6e 70  = 0;..  if( nInp
357b3 75 74 3c 30 20 29 7b 0a 20 20 20 20 6e 49 6e 70  ut<0 ){.    nInp
357b4 75 74 20 3d 20 73 74 72 6c 65 6e 28 7a 49 6e 70  ut = strlen(zInp
357b5 75 74 29 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72  ut);.  }.  nChar
357b6 20 3d 20 6e 49 6e 70 75 74 2b 31 3b 0a 20 20 70   = nInput+1;.  p
357b7 43 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f 72  Csr = (IcuCursor
357b8 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
357b9 63 28 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28  c(.      sizeof(
357ba 49 63 75 43 75 72 73 6f 72 29 20 2b 20 20 20 20  IcuCursor) +    
357bb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
357bc 63 75 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  cuCursor */.    
357bd 20 20 6e 43 68 61 72 20 2a 20 73 69 7a 65 6f 66    nChar * sizeof
357be 28 55 43 68 61 72 29 20 2b 20 20 20 20 20 20 20  (UChar) +       
357bf 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f       /* IcuCurso
357c0 72 2e 61 43 68 61 72 5b 5d 20 2a 2f 0a 20 20 20  r.aChar[] */.   
357c1 20 20 20 28 6e 43 68 61 72 2b 31 29 20 2a 20 73     (nChar+1) * s
357c2 69 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20  izeof(int)      
357c3 20 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73        /* IcuCurs
357c4 6f 72 2e 61 4f 66 66 73 65 74 5b 5d 20 2a 2f 0a  or.aOffset[] */.
357c5 20 20 29 3b 0a 20 20 69 66 28 20 21 70 43 73 72    );.  if( !pCsr
357c6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
357c7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
357c8 0a 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2c 20  .  memset(pCsr, 
357c9 30 2c 20 73 69 7a 65 6f 66 28 49 63 75 43 75 72  0, sizeof(IcuCur
357ca 73 6f 72 29 29 3b 0a 20 20 70 43 73 72 2d 3e 61  sor));.  pCsr->a
357cb 43 68 61 72 20 3d 20 28 55 43 68 61 72 20 2a 29  Char = (UChar *)
357cc 26 70 43 73 72 5b 31 5d 3b 0a 20 20 70 43 73 72  &pCsr[1];.  pCsr
357cd 2d 3e 61 4f 66 66 73 65 74 20 3d 20 28 69 6e 74  ->aOffset = (int
357ce 20 2a 29 26 70 43 73 72 2d 3e 61 43 68 61 72 5b   *)&pCsr->aChar[
357cf 6e 43 68 61 72 5d 3b 0a 0a 20 20 70 43 73 72 2d  nChar];..  pCsr-
357d0 3e 61 4f 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d  >aOffset[iOut] =
357d1 20 69 49 6e 70 75 74 3b 0a 20 20 55 38 5f 4e 45   iInput;.  U8_NE
357d2 58 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e 70 75  XT(zInput, iInpu
357d3 74 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b 20 0a  t, nInput, c); .
357d4 20 20 77 68 69 6c 65 28 20 63 3e 30 20 29 7b 0a    while( c>0 ){.
357d5 20 20 20 20 69 6e 74 20 69 73 45 72 72 6f 72 20      int isError 
357d6 3d 20 30 3b 0a 20 20 20 20 63 20 3d 20 75 5f 66  = 0;.    c = u_f
357d7 6f 6c 64 43 61 73 65 28 63 2c 20 6f 70 74 29 3b  oldCase(c, opt);
357d8 0a 20 20 20 20 55 31 36 5f 41 50 50 45 4e 44 28  .    U16_APPEND(
357d9 70 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75  pCsr->aChar, iOu
357da 74 2c 20 6e 43 68 61 72 2c 20 63 2c 20 69 73 45  t, nChar, c, isE
357db 72 72 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 69  rror);.    if( i
357dc 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20  sError ){.      
357dd 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73  sqlite3_free(pCs
357de 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  r);.      return
357df 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
357e0 20 20 20 7d 0a 20 20 20 20 70 43 73 72 2d 3e 61     }.    pCsr->a
357e1 4f 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d 20 69  Offset[iOut] = i
357e2 49 6e 70 75 74 3b 0a 0a 20 20 20 20 69 66 28 20  Input;..    if( 
357e3 69 49 6e 70 75 74 3c 6e 49 6e 70 75 74 20 29 7b  iInput<nInput ){
357e4 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28 7a  .      U8_NEXT(z
357e5 49 6e 70 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e  Input, iInput, n
357e6 49 6e 70 75 74 2c 20 63 29 3b 0a 20 20 20 20 7d  Input, c);.    }
357e7 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d 20  else{.      c = 
357e8 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  0;.    }.  }..  
357e9 70 43 73 72 2d 3e 70 49 74 65 72 20 3d 20 75 62  pCsr->pIter = ub
357ea 72 6b 5f 6f 70 65 6e 28 55 42 52 4b 5f 57 4f 52  rk_open(UBRK_WOR
357eb 44 2c 20 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 70  D, p->zLocale, p
357ec 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75 74  Csr->aChar, iOut
357ed 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69 66  , &status);.  if
357ee 28 20 21 55 5f 53 55 43 43 45 53 53 28 73 74 61  ( !U_SUCCESS(sta
357ef 74 75 73 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  tus) ){.    sqli
357f0 74 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a  te3_free(pCsr);.
357f1 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
357f2 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70  E_ERROR;.  }.  p
357f3 43 73 72 2d 3e 6e 43 68 61 72 20 3d 20 69 4f 75  Csr->nChar = iOu
357f4 74 3b 0a 0a 20 20 75 62 72 6b 5f 66 69 72 73 74  t;..  ubrk_first
357f5 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20  (pCsr->pIter);. 
357f6 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 28 73 71   *ppCursor = (sq
357f7 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
357f8 63 75 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a 20  cursor *)pCsr;. 
357f9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
357fa 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  K;.}../*.** Clos
357fb 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e  e a tokenization
357fc 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73   cursor previous
357fd 6c 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63  ly opened by a c
357fe 61 6c 6c 20 74 6f 20 69 63 75 4f 70 65 6e 28 29  all to icuOpen()
357ff 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35800 69 63 75 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  icuClose(sqlite3
35801 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
35802 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 49  r *pCursor){.  I
35803 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  cuCursor *pCsr =
35804 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70 43   (IcuCursor *)pC
35805 75 72 73 6f 72 3b 0a 20 20 75 62 72 6b 5f 63 6c  ursor;.  ubrk_cl
35806 6f 73 65 28 70 43 73 72 2d 3e 70 49 74 65 72 29  ose(pCsr->pIter)
35807 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
35808 28 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 29 3b  (pCsr->zBuffer);
35809 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3580a 70 43 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  pCsr);.  return 
3580b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
3580c 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
3580d 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  next token from 
3580e 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  a tokenization c
3580f 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  ursor..*/.static
35810 20 69 6e 74 20 69 63 75 4e 65 78 74 28 0a 20 20   int icuNext(.  
35811 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
35812 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  r_cursor *pCurso
35813 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65  r,  /* Cursor re
35814 74 75 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65  turned by simple
35815 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  Open */.  const 
35816 63 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20  char **ppToken, 
35817 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35818 20 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69   OUT: *ppToken i
35819 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74  s the token text
3581a 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74   */.  int *pnByt
3581b 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
3581c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
3581d 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  : Number of byte
3581e 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  s in token */.  
3581f 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73  int *piStartOffs
35820 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  et,             
35821 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 72      /* OUT: Star
35822 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74  ting offset of t
35823 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  oken */.  int *p
35824 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 20  iEndOffset,     
35825 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35826 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66   OUT: Ending off
35827 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  set of token */.
35828 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f    int *piPositio
35829 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
3582a 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f        /* OUT: Po
3582b 73 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f  sition integer o
3582c 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20  f token */.){.  
3582d 49 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20  IcuCursor *pCsr 
3582e 3d 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70  = (IcuCursor *)p
3582f 43 75 72 73 6f 72 3b 0a 0a 20 20 69 6e 74 20 69  Cursor;..  int i
35830 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e 74  Start = 0;.  int
35831 20 69 45 6e 64 20 3d 20 30 3b 0a 20 20 69 6e 74   iEnd = 0;.  int
35832 20 6e 42 79 74 65 20 3d 20 30 3b 0a 0a 20 20 77   nByte = 0;..  w
35833 68 69 6c 65 28 20 69 53 74 61 72 74 3d 3d 69 45  hile( iStart==iE
35834 6e 64 20 29 7b 0a 20 20 20 20 55 43 68 61 72 33  nd ){.    UChar3
35835 32 20 63 3b 0a 0a 20 20 20 20 69 53 74 61 72 74  2 c;..    iStart
35836 20 3d 20 75 62 72 6b 5f 63 75 72 72 65 6e 74 28   = ubrk_current(
35837 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20  pCsr->pIter);.  
35838 20 20 69 45 6e 64 20 3d 20 75 62 72 6b 5f 6e 65    iEnd = ubrk_ne
35839 78 74 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b  xt(pCsr->pIter);
3583a 0a 20 20 20 20 69 66 28 20 69 45 6e 64 3d 3d 55  .    if( iEnd==U
3583b 42 52 4b 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  BRK_DONE ){.    
3583c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3583d 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  DONE;.    }..   
3583e 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69   while( iStart<i
3583f 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  End ){.      int
35840 20 69 57 68 69 74 65 20 3d 20 69 53 74 61 72 74   iWhite = iStart
35841 3b 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28  ;.      U8_NEXT(
35842 70 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 57 68  pCsr->aChar, iWh
35843 69 74 65 2c 20 70 43 73 72 2d 3e 6e 43 68 61 72  ite, pCsr->nChar
35844 2c 20 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , c);.      if( 
35845 75 5f 69 73 73 70 61 63 65 28 63 29 20 29 7b 0a  u_isspace(c) ){.
35846 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d          iStart =
35847 20 69 57 68 69 74 65 3b 0a 20 20 20 20 20 20 7d   iWhite;.      }
35848 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72  else{.        br
35849 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
3584a 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 69 53   }.    assert(iS
3584b 74 61 72 74 3c 3d 69 45 6e 64 29 3b 0a 20 20 7d  tart<=iEnd);.  }
3584c 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 55 45 72  ..  do {.    UEr
3584d 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d  rorCode status =
3584e 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20   U_ZERO_ERROR;. 
3584f 20 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b 0a     if( nByte ){.
35850 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77        char *zNew
35851 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
35852 6f 63 28 70 43 73 72 2d 3e 7a 42 75 66 66 65 72  oc(pCsr->zBuffer
35853 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  , nByte);.      
35854 69 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20 20  if( !zNew ){.   
35855 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
35856 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
35857 7d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 7a 42  }.      pCsr->zB
35858 75 66 66 65 72 20 3d 20 7a 4e 65 77 3b 0a 20 20  uffer = zNew;.  
35859 20 20 20 20 70 43 73 72 2d 3e 6e 42 75 66 66 65      pCsr->nBuffe
3585a 72 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d  r = nByte;.    }
3585b 0a 0a 20 20 20 20 75 5f 73 74 72 54 6f 55 54 46  ..    u_strToUTF
3585c 38 28 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d  8(.        pCsr-
3585d 3e 7a 42 75 66 66 65 72 2c 20 70 43 73 72 2d 3e  >zBuffer, pCsr->
3585e 6e 42 75 66 66 65 72 2c 20 26 6e 42 79 74 65 2c  nBuffer, &nByte,
3585f 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 76 61      /* Output va
35860 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 70  rs */.        &p
35861 43 73 72 2d 3e 61 43 68 61 72 5b 69 53 74 61 72  Csr->aChar[iStar
35862 74 5d 2c 20 69 45 6e 64 2d 69 53 74 61 72 74 2c  t], iEnd-iStart,
35863 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20         /* Input 
35864 76 61 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  vars */.        
35865 26 73 74 61 74 75 73 20 20 20 20 20 20 20 20 20  &status         
35866 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35867 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
35868 75 74 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75  ut success/failu
35869 72 65 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 7d  re */.    );.  }
3586a 20 77 68 69 6c 65 28 20 6e 42 79 74 65 3e 70 43   while( nByte>pC
3586b 73 72 2d 3e 6e 42 75 66 66 65 72 20 29 3b 0a 0a  sr->nBuffer );..
3586c 20 20 2a 70 70 54 6f 6b 65 6e 20 3d 20 70 43 73    *ppToken = pCs
3586d 72 2d 3e 7a 42 75 66 66 65 72 3b 0a 20 20 2a 70  r->zBuffer;.  *p
3586e 6e 42 79 74 65 73 20 3d 20 6e 42 79 74 65 3b 0a  nBytes = nByte;.
3586f 20 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74    *piStartOffset
35870 20 3d 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74   = pCsr->aOffset
35871 5b 69 53 74 61 72 74 5d 3b 0a 20 20 2a 70 69 45  [iStart];.  *piE
35872 6e 64 4f 66 66 73 65 74 20 3d 20 70 43 73 72 2d  ndOffset = pCsr-
35873 3e 61 4f 66 66 73 65 74 5b 69 45 6e 64 5d 3b 0a  >aOffset[iEnd];.
35874 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20    *piPosition = 
35875 70 43 73 72 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a  pCsr->iToken++;.
35876 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
35877 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
35878 65 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65  e set of routine
35879 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
3587a 20 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65   the simple toke
3587b 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20  nizer.*/.static 
3587c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
3587d 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69  kenizer_module i
3587e 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  cuTokenizerModul
3587f 65 20 3d 20 7b 0a 20 20 30 2c 20 20 20 20 20 20  e = {.  0,      
35880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35881 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
35882 20 2a 2f 0a 20 20 69 63 75 43 72 65 61 74 65 2c   */.  icuCreate,
35883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35884 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 2a     /* xCreate  *
35885 2f 0a 20 20 69 63 75 44 65 73 74 72 6f 79 2c 20  /.  icuDestroy, 
35886 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35887 20 2f 2a 20 78 43 72 65 61 74 65 20 20 2a 2f 0a   /* xCreate  */.
35888 20 20 69 63 75 4f 70 65 6e 2c 20 20 20 20 20 20    icuOpen,      
35889 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3588a 2a 20 78 4f 70 65 6e 20 20 20 20 2a 2f 0a 20 20  * xOpen    */.  
3588b 69 63 75 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  icuClose,       
3588c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3588d 78 43 6c 6f 73 65 20 20 20 2a 2f 0a 20 20 69 63  xClose   */.  ic
3588e 75 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20 20  uNext,          
3588f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4e             /* xN
35890 65 78 74 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ext    */.};../*
35891 0a 2a 2a 20 53 65 74 20 2a 70 70 4d 6f 64 75 6c  .** Set *ppModul
35892 65 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  e to point at th
35893 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
35894 20 6f 66 20 74 68 65 20 49 43 55 20 74 6f 6b 65   of the ICU toke
35895 6e 69 7a 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nizer..*/.SQLITE
35896 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
35897 6c 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b 65  lite3Fts3IcuToke
35898 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73  nizerModule(.  s
35899 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3589a 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
3589b 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70  pModule.){.  *pp
3589c 4d 6f 64 75 6c 65 20 3d 20 26 69 63 75 54 6f 6b  Module = &icuTok
3589d 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a  enizerModule;.}.
3589e 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
3589f 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
358a0 5f 49 43 55 29 20 2a 2f 0a 23 65 6e 64 69 66 20  _ICU) */.#endif 
358a1 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
358a2 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
358a3 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
358a4 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a  E_FTS3) */../***
358a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
358a6 6f 66 20 66 74 73 33 5f 69 63 75 2e 63 20 2a 2a  of fts3_icu.c **
358a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
358a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
358a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a              **********/.